blob: c3bdc0aeabe2f988ea24871891260c48ed65c759 [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;
Mihai Caraman66c98972012-06-25 02:26:26 +000060 gva_t eaddr;
61 u32 val, lpid;
Scott Wood73196cd2011-12-20 15:34:47 +000062 unsigned long flags;
63
64 ts = get_tlb_ts(gtlbe);
65 tid = get_tlb_tid(gtlbe);
66 lpid = vcpu_e500->vcpu.kvm->arch.lpid;
67
68 /* We search the host TLB to invalidate its shadow TLB entry */
69 val = (tid << 16) | ts;
70 eaddr = get_tlb_eaddr(gtlbe);
71
72 local_irq_save(flags);
73
74 mtspr(SPRN_MAS6, val);
75 mtspr(SPRN_MAS5, MAS5_SGS | lpid);
76
77 asm volatile("tlbsx 0, %[eaddr]\n" : : [eaddr] "r" (eaddr));
78 val = mfspr(SPRN_MAS1);
79 if (val & MAS1_VALID) {
80 mtspr(SPRN_MAS1, val & ~MAS1_VALID);
81 asm volatile("tlbwe");
82 }
83 mtspr(SPRN_MAS5, 0);
84 /* NOTE: tlbsx also updates mas8, so clear it for host tlbwe */
85 mtspr(SPRN_MAS8, 0);
86 isync();
87
88 local_irq_restore(flags);
89}
90
91void kvmppc_e500_tlbil_all(struct kvmppc_vcpu_e500 *vcpu_e500)
92{
93 unsigned long flags;
94
95 local_irq_save(flags);
96 mtspr(SPRN_MAS5, MAS5_SGS | vcpu_e500->vcpu.kvm->arch.lpid);
97 asm volatile("tlbilxlpid");
98 mtspr(SPRN_MAS5, 0);
99 local_irq_restore(flags);
100}
101
102void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid)
103{
104 vcpu->arch.pid = pid;
105}
106
107void kvmppc_mmu_msr_notify(struct kvm_vcpu *vcpu, u32 old_msr)
108{
109}
110
111void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
112{
113 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
114
115 kvmppc_booke_vcpu_load(vcpu, cpu);
116
117 mtspr(SPRN_LPID, vcpu->kvm->arch.lpid);
118 mtspr(SPRN_EPCR, vcpu->arch.shadow_epcr);
119 mtspr(SPRN_GPIR, vcpu->vcpu_id);
120 mtspr(SPRN_MSRP, vcpu->arch.shadow_msrp);
121 mtspr(SPRN_EPLC, vcpu->arch.eplc);
122 mtspr(SPRN_EPSC, vcpu->arch.epsc);
123
124 mtspr(SPRN_GIVPR, vcpu->arch.ivpr);
125 mtspr(SPRN_GIVOR2, vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE]);
126 mtspr(SPRN_GIVOR8, vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL]);
127 mtspr(SPRN_GSPRG0, (unsigned long)vcpu->arch.shared->sprg0);
128 mtspr(SPRN_GSPRG1, (unsigned long)vcpu->arch.shared->sprg1);
129 mtspr(SPRN_GSPRG2, (unsigned long)vcpu->arch.shared->sprg2);
130 mtspr(SPRN_GSPRG3, (unsigned long)vcpu->arch.shared->sprg3);
131
132 mtspr(SPRN_GSRR0, vcpu->arch.shared->srr0);
133 mtspr(SPRN_GSRR1, vcpu->arch.shared->srr1);
134
135 mtspr(SPRN_GEPR, vcpu->arch.epr);
136 mtspr(SPRN_GDEAR, vcpu->arch.shared->dar);
137 mtspr(SPRN_GESR, vcpu->arch.shared->esr);
138
139 if (vcpu->arch.oldpir != mfspr(SPRN_PIR))
140 kvmppc_e500_tlbil_all(vcpu_e500);
141
142 kvmppc_load_guest_fp(vcpu);
143}
144
145void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
146{
147 vcpu->arch.eplc = mfspr(SPRN_EPLC);
148 vcpu->arch.epsc = mfspr(SPRN_EPSC);
149
150 vcpu->arch.shared->sprg0 = mfspr(SPRN_GSPRG0);
151 vcpu->arch.shared->sprg1 = mfspr(SPRN_GSPRG1);
152 vcpu->arch.shared->sprg2 = mfspr(SPRN_GSPRG2);
153 vcpu->arch.shared->sprg3 = mfspr(SPRN_GSPRG3);
154
155 vcpu->arch.shared->srr0 = mfspr(SPRN_GSRR0);
156 vcpu->arch.shared->srr1 = mfspr(SPRN_GSRR1);
157
158 vcpu->arch.epr = mfspr(SPRN_GEPR);
159 vcpu->arch.shared->dar = mfspr(SPRN_GDEAR);
160 vcpu->arch.shared->esr = mfspr(SPRN_GESR);
161
162 vcpu->arch.oldpir = mfspr(SPRN_PIR);
163
164 kvmppc_booke_vcpu_put(vcpu);
165}
166
167int kvmppc_core_check_processor_compat(void)
168{
169 int r;
170
171 if (strcmp(cur_cpu_spec->cpu_name, "e500mc") == 0)
172 r = 0;
173 else if (strcmp(cur_cpu_spec->cpu_name, "e5500") == 0)
174 r = 0;
Mihai Caramanea17a972013-04-11 00:03:13 +0000175 else if (strcmp(cur_cpu_spec->cpu_name, "e6500") == 0)
176 r = 0;
Scott Wood73196cd2011-12-20 15:34:47 +0000177 else
178 r = -ENOTSUPP;
179
180 return r;
181}
182
183int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu)
184{
185 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
186
187 vcpu->arch.shadow_epcr = SPRN_EPCR_DSIGS | SPRN_EPCR_DGTMI | \
188 SPRN_EPCR_DUVD;
Mihai Caramanc7ba7772012-06-25 02:26:19 +0000189#ifdef CONFIG_64BIT
190 vcpu->arch.shadow_epcr |= SPRN_EPCR_ICM;
191#endif
Scott Wood73196cd2011-12-20 15:34:47 +0000192 vcpu->arch.shadow_msrp = MSRP_UCLEP | MSRP_DEP | MSRP_PMMP;
193 vcpu->arch.eplc = EPC_EGS | (vcpu->kvm->arch.lpid << EPC_ELPID_SHIFT);
194 vcpu->arch.epsc = vcpu->arch.eplc;
195
196 vcpu->arch.pvr = mfspr(SPRN_PVR);
197 vcpu_e500->svr = mfspr(SPRN_SVR);
198
199 vcpu->arch.cpu_type = KVM_CPU_E500MC;
200
201 return 0;
202}
203
204void kvmppc_core_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
205{
206 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
207
208 sregs->u.e.features |= KVM_SREGS_E_ARCH206_MMU | KVM_SREGS_E_PM |
209 KVM_SREGS_E_PC;
210 sregs->u.e.impl_id = KVM_SREGS_E_IMPL_FSL;
211
212 sregs->u.e.impl.fsl.features = 0;
213 sregs->u.e.impl.fsl.svr = vcpu_e500->svr;
214 sregs->u.e.impl.fsl.hid0 = vcpu_e500->hid0;
215 sregs->u.e.impl.fsl.mcar = vcpu_e500->mcar;
216
217 kvmppc_get_sregs_e500_tlb(vcpu, sregs);
218
219 sregs->u.e.ivor_high[3] =
220 vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR];
221 sregs->u.e.ivor_high[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL];
222 sregs->u.e.ivor_high[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL_CRIT];
223
224 kvmppc_get_sregs_ivor(vcpu, sregs);
225}
226
227int kvmppc_core_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
228{
229 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
230 int ret;
231
232 if (sregs->u.e.impl_id == KVM_SREGS_E_IMPL_FSL) {
233 vcpu_e500->svr = sregs->u.e.impl.fsl.svr;
234 vcpu_e500->hid0 = sregs->u.e.impl.fsl.hid0;
235 vcpu_e500->mcar = sregs->u.e.impl.fsl.mcar;
236 }
237
238 ret = kvmppc_set_sregs_e500_tlb(vcpu, sregs);
239 if (ret < 0)
240 return ret;
241
242 if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
243 return 0;
244
245 if (sregs->u.e.features & KVM_SREGS_E_PM) {
246 vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR] =
247 sregs->u.e.ivor_high[3];
248 }
249
250 if (sregs->u.e.features & KVM_SREGS_E_PC) {
251 vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL] =
252 sregs->u.e.ivor_high[4];
253 vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL_CRIT] =
254 sregs->u.e.ivor_high[5];
255 }
256
257 return kvmppc_set_sregs_ivor(vcpu, sregs);
258}
259
Mihai Caraman35b299e2013-04-11 00:03:07 +0000260int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id,
261 union kvmppc_one_reg *val)
262{
Mihai Caramana85d2aa2013-04-11 00:03:08 +0000263 int r = kvmppc_get_one_reg_e500_tlb(vcpu, id, val);
264 return r;
Mihai Caraman35b299e2013-04-11 00:03:07 +0000265}
266
267int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id,
268 union kvmppc_one_reg *val)
269{
Mihai Caramana85d2aa2013-04-11 00:03:08 +0000270 int r = kvmppc_set_one_reg_e500_tlb(vcpu, id, val);
271 return r;
Mihai Caraman35b299e2013-04-11 00:03:07 +0000272}
273
Scott Wood73196cd2011-12-20 15:34:47 +0000274struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
275{
276 struct kvmppc_vcpu_e500 *vcpu_e500;
277 struct kvm_vcpu *vcpu;
278 int err;
279
280 vcpu_e500 = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
281 if (!vcpu_e500) {
282 err = -ENOMEM;
283 goto out;
284 }
285 vcpu = &vcpu_e500->vcpu;
286
287 /* Invalid PIR value -- this LPID dosn't have valid state on any cpu */
288 vcpu->arch.oldpir = 0xffffffff;
289
290 err = kvm_vcpu_init(vcpu, kvm, id);
291 if (err)
292 goto free_vcpu;
293
294 err = kvmppc_e500_tlb_init(vcpu_e500);
295 if (err)
296 goto uninit_vcpu;
297
298 vcpu->arch.shared = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
299 if (!vcpu->arch.shared)
300 goto uninit_tlb;
301
302 return vcpu;
303
304uninit_tlb:
305 kvmppc_e500_tlb_uninit(vcpu_e500);
306uninit_vcpu:
307 kvm_vcpu_uninit(vcpu);
308
309free_vcpu:
310 kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
311out:
312 return ERR_PTR(err);
313}
314
315void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
316{
317 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
318
319 free_page((unsigned long)vcpu->arch.shared);
320 kvmppc_e500_tlb_uninit(vcpu_e500);
321 kvm_vcpu_uninit(vcpu);
322 kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
323}
324
325int kvmppc_core_init_vm(struct kvm *kvm)
326{
327 int lpid;
328
329 lpid = kvmppc_alloc_lpid();
330 if (lpid < 0)
331 return lpid;
332
333 kvm->arch.lpid = lpid;
334 return 0;
335}
336
337void kvmppc_core_destroy_vm(struct kvm *kvm)
338{
339 kvmppc_free_lpid(kvm->arch.lpid);
340}
341
342static int __init kvmppc_e500mc_init(void)
343{
344 int r;
345
346 r = kvmppc_booke_init();
347 if (r)
348 return r;
349
350 kvmppc_init_lpid(64);
351 kvmppc_claim_lpid(0); /* host */
352
353 return kvm_init(NULL, sizeof(struct kvmppc_vcpu_e500), 0, THIS_MODULE);
354}
355
356static void __exit kvmppc_e500mc_exit(void)
357{
358 kvmppc_booke_exit();
359}
360
361module_init(kvmppc_e500mc_init);
362module_exit(kvmppc_e500mc_exit);