blob: db97ee3a08278354134f9d4e9614b7fff677374a [file] [log] [blame]
Scott Wood73196cd2011-12-20 15:34:47 +00001/*
Mihai Caramanc7ba7772012-06-25 02:26:19 +00002 * Copyright (C) 2010,2012 Freescale Semiconductor, Inc. All rights reserved.
Scott Wood73196cd2011-12-20 15:34:47 +00003 *
4 * Author: Varun Sethi, <varun.sethi@freescale.com>
5 *
6 * Description:
7 * This file is derived from arch/powerpc/kvm/e500.c,
8 * by Yu Liu <yu.liu@freescale.com>.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License, version 2, as
12 * published by the Free Software Foundation.
13 */
14
15#include <linux/kvm_host.h>
16#include <linux/slab.h>
17#include <linux/err.h>
18#include <linux/export.h>
19
20#include <asm/reg.h>
21#include <asm/cputable.h>
22#include <asm/tlbflush.h>
23#include <asm/kvm_ppc.h>
24#include <asm/dbell.h>
25
26#include "booke.h"
27#include "e500.h"
28
29void kvmppc_set_pending_interrupt(struct kvm_vcpu *vcpu, enum int_class type)
30{
31 enum ppc_dbell dbell_type;
32 unsigned long tag;
33
34 switch (type) {
35 case INT_CLASS_NONCRIT:
36 dbell_type = PPC_G_DBELL;
37 break;
38 case INT_CLASS_CRIT:
39 dbell_type = PPC_G_DBELL_CRIT;
40 break;
41 case INT_CLASS_MC:
42 dbell_type = PPC_G_DBELL_MC;
43 break;
44 default:
45 WARN_ONCE(1, "%s: unknown int type %d\n", __func__, type);
46 return;
47 }
48
49
50 tag = PPC_DBELL_LPID(vcpu->kvm->arch.lpid) | vcpu->vcpu_id;
51 mb();
52 ppc_msgsnd(dbell_type, 0, tag);
53}
54
55/* gtlbe must not be mapped by more than one host tlb entry */
56void kvmppc_e500_tlbil_one(struct kvmppc_vcpu_e500 *vcpu_e500,
57 struct kvm_book3e_206_tlb_entry *gtlbe)
58{
59 unsigned int tid, ts;
60 u32 val, eaddr, lpid;
61 unsigned long flags;
62
63 ts = get_tlb_ts(gtlbe);
64 tid = get_tlb_tid(gtlbe);
65 lpid = vcpu_e500->vcpu.kvm->arch.lpid;
66
67 /* We search the host TLB to invalidate its shadow TLB entry */
68 val = (tid << 16) | ts;
69 eaddr = get_tlb_eaddr(gtlbe);
70
71 local_irq_save(flags);
72
73 mtspr(SPRN_MAS6, val);
74 mtspr(SPRN_MAS5, MAS5_SGS | lpid);
75
76 asm volatile("tlbsx 0, %[eaddr]\n" : : [eaddr] "r" (eaddr));
77 val = mfspr(SPRN_MAS1);
78 if (val & MAS1_VALID) {
79 mtspr(SPRN_MAS1, val & ~MAS1_VALID);
80 asm volatile("tlbwe");
81 }
82 mtspr(SPRN_MAS5, 0);
83 /* NOTE: tlbsx also updates mas8, so clear it for host tlbwe */
84 mtspr(SPRN_MAS8, 0);
85 isync();
86
87 local_irq_restore(flags);
88}
89
90void kvmppc_e500_tlbil_all(struct kvmppc_vcpu_e500 *vcpu_e500)
91{
92 unsigned long flags;
93
94 local_irq_save(flags);
95 mtspr(SPRN_MAS5, MAS5_SGS | vcpu_e500->vcpu.kvm->arch.lpid);
96 asm volatile("tlbilxlpid");
97 mtspr(SPRN_MAS5, 0);
98 local_irq_restore(flags);
99}
100
101void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid)
102{
103 vcpu->arch.pid = pid;
104}
105
106void kvmppc_mmu_msr_notify(struct kvm_vcpu *vcpu, u32 old_msr)
107{
108}
109
110void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
111{
112 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
113
114 kvmppc_booke_vcpu_load(vcpu, cpu);
115
116 mtspr(SPRN_LPID, vcpu->kvm->arch.lpid);
117 mtspr(SPRN_EPCR, vcpu->arch.shadow_epcr);
118 mtspr(SPRN_GPIR, vcpu->vcpu_id);
119 mtspr(SPRN_MSRP, vcpu->arch.shadow_msrp);
120 mtspr(SPRN_EPLC, vcpu->arch.eplc);
121 mtspr(SPRN_EPSC, vcpu->arch.epsc);
122
123 mtspr(SPRN_GIVPR, vcpu->arch.ivpr);
124 mtspr(SPRN_GIVOR2, vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE]);
125 mtspr(SPRN_GIVOR8, vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL]);
126 mtspr(SPRN_GSPRG0, (unsigned long)vcpu->arch.shared->sprg0);
127 mtspr(SPRN_GSPRG1, (unsigned long)vcpu->arch.shared->sprg1);
128 mtspr(SPRN_GSPRG2, (unsigned long)vcpu->arch.shared->sprg2);
129 mtspr(SPRN_GSPRG3, (unsigned long)vcpu->arch.shared->sprg3);
130
131 mtspr(SPRN_GSRR0, vcpu->arch.shared->srr0);
132 mtspr(SPRN_GSRR1, vcpu->arch.shared->srr1);
133
134 mtspr(SPRN_GEPR, vcpu->arch.epr);
135 mtspr(SPRN_GDEAR, vcpu->arch.shared->dar);
136 mtspr(SPRN_GESR, vcpu->arch.shared->esr);
137
138 if (vcpu->arch.oldpir != mfspr(SPRN_PIR))
139 kvmppc_e500_tlbil_all(vcpu_e500);
140
141 kvmppc_load_guest_fp(vcpu);
142}
143
144void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
145{
146 vcpu->arch.eplc = mfspr(SPRN_EPLC);
147 vcpu->arch.epsc = mfspr(SPRN_EPSC);
148
149 vcpu->arch.shared->sprg0 = mfspr(SPRN_GSPRG0);
150 vcpu->arch.shared->sprg1 = mfspr(SPRN_GSPRG1);
151 vcpu->arch.shared->sprg2 = mfspr(SPRN_GSPRG2);
152 vcpu->arch.shared->sprg3 = mfspr(SPRN_GSPRG3);
153
154 vcpu->arch.shared->srr0 = mfspr(SPRN_GSRR0);
155 vcpu->arch.shared->srr1 = mfspr(SPRN_GSRR1);
156
157 vcpu->arch.epr = mfspr(SPRN_GEPR);
158 vcpu->arch.shared->dar = mfspr(SPRN_GDEAR);
159 vcpu->arch.shared->esr = mfspr(SPRN_GESR);
160
161 vcpu->arch.oldpir = mfspr(SPRN_PIR);
162
163 kvmppc_booke_vcpu_put(vcpu);
164}
165
166int kvmppc_core_check_processor_compat(void)
167{
168 int r;
169
170 if (strcmp(cur_cpu_spec->cpu_name, "e500mc") == 0)
171 r = 0;
172 else if (strcmp(cur_cpu_spec->cpu_name, "e5500") == 0)
173 r = 0;
174 else
175 r = -ENOTSUPP;
176
177 return r;
178}
179
180int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu)
181{
182 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
183
184 vcpu->arch.shadow_epcr = SPRN_EPCR_DSIGS | SPRN_EPCR_DGTMI | \
185 SPRN_EPCR_DUVD;
Mihai Caramanc7ba7772012-06-25 02:26:19 +0000186#ifdef CONFIG_64BIT
187 vcpu->arch.shadow_epcr |= SPRN_EPCR_ICM;
188#endif
Scott Wood73196cd2011-12-20 15:34:47 +0000189 vcpu->arch.shadow_msrp = MSRP_UCLEP | MSRP_DEP | MSRP_PMMP;
190 vcpu->arch.eplc = EPC_EGS | (vcpu->kvm->arch.lpid << EPC_ELPID_SHIFT);
191 vcpu->arch.epsc = vcpu->arch.eplc;
192
193 vcpu->arch.pvr = mfspr(SPRN_PVR);
194 vcpu_e500->svr = mfspr(SPRN_SVR);
195
196 vcpu->arch.cpu_type = KVM_CPU_E500MC;
197
198 return 0;
199}
200
201void kvmppc_core_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
202{
203 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
204
205 sregs->u.e.features |= KVM_SREGS_E_ARCH206_MMU | KVM_SREGS_E_PM |
206 KVM_SREGS_E_PC;
207 sregs->u.e.impl_id = KVM_SREGS_E_IMPL_FSL;
208
209 sregs->u.e.impl.fsl.features = 0;
210 sregs->u.e.impl.fsl.svr = vcpu_e500->svr;
211 sregs->u.e.impl.fsl.hid0 = vcpu_e500->hid0;
212 sregs->u.e.impl.fsl.mcar = vcpu_e500->mcar;
213
214 kvmppc_get_sregs_e500_tlb(vcpu, sregs);
215
216 sregs->u.e.ivor_high[3] =
217 vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR];
218 sregs->u.e.ivor_high[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL];
219 sregs->u.e.ivor_high[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL_CRIT];
220
221 kvmppc_get_sregs_ivor(vcpu, sregs);
222}
223
224int kvmppc_core_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
225{
226 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
227 int ret;
228
229 if (sregs->u.e.impl_id == KVM_SREGS_E_IMPL_FSL) {
230 vcpu_e500->svr = sregs->u.e.impl.fsl.svr;
231 vcpu_e500->hid0 = sregs->u.e.impl.fsl.hid0;
232 vcpu_e500->mcar = sregs->u.e.impl.fsl.mcar;
233 }
234
235 ret = kvmppc_set_sregs_e500_tlb(vcpu, sregs);
236 if (ret < 0)
237 return ret;
238
239 if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
240 return 0;
241
242 if (sregs->u.e.features & KVM_SREGS_E_PM) {
243 vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR] =
244 sregs->u.e.ivor_high[3];
245 }
246
247 if (sregs->u.e.features & KVM_SREGS_E_PC) {
248 vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL] =
249 sregs->u.e.ivor_high[4];
250 vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL_CRIT] =
251 sregs->u.e.ivor_high[5];
252 }
253
254 return kvmppc_set_sregs_ivor(vcpu, sregs);
255}
256
257struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
258{
259 struct kvmppc_vcpu_e500 *vcpu_e500;
260 struct kvm_vcpu *vcpu;
261 int err;
262
263 vcpu_e500 = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
264 if (!vcpu_e500) {
265 err = -ENOMEM;
266 goto out;
267 }
268 vcpu = &vcpu_e500->vcpu;
269
270 /* Invalid PIR value -- this LPID dosn't have valid state on any cpu */
271 vcpu->arch.oldpir = 0xffffffff;
272
273 err = kvm_vcpu_init(vcpu, kvm, id);
274 if (err)
275 goto free_vcpu;
276
277 err = kvmppc_e500_tlb_init(vcpu_e500);
278 if (err)
279 goto uninit_vcpu;
280
281 vcpu->arch.shared = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
282 if (!vcpu->arch.shared)
283 goto uninit_tlb;
284
285 return vcpu;
286
287uninit_tlb:
288 kvmppc_e500_tlb_uninit(vcpu_e500);
289uninit_vcpu:
290 kvm_vcpu_uninit(vcpu);
291
292free_vcpu:
293 kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
294out:
295 return ERR_PTR(err);
296}
297
298void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
299{
300 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
301
302 free_page((unsigned long)vcpu->arch.shared);
303 kvmppc_e500_tlb_uninit(vcpu_e500);
304 kvm_vcpu_uninit(vcpu);
305 kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
306}
307
308int kvmppc_core_init_vm(struct kvm *kvm)
309{
310 int lpid;
311
312 lpid = kvmppc_alloc_lpid();
313 if (lpid < 0)
314 return lpid;
315
316 kvm->arch.lpid = lpid;
317 return 0;
318}
319
320void kvmppc_core_destroy_vm(struct kvm *kvm)
321{
322 kvmppc_free_lpid(kvm->arch.lpid);
323}
324
325static int __init kvmppc_e500mc_init(void)
326{
327 int r;
328
329 r = kvmppc_booke_init();
330 if (r)
331 return r;
332
333 kvmppc_init_lpid(64);
334 kvmppc_claim_lpid(0); /* host */
335
336 return kvm_init(NULL, sizeof(struct kvmppc_vcpu_e500), 0, THIS_MODULE);
337}
338
339static void __exit kvmppc_e500mc_exit(void)
340{
341 kvmppc_booke_exit();
342}
343
344module_init(kvmppc_e500mc_init);
345module_exit(kvmppc_e500mc_exit);