1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/kvm_host.h>
16#include <linux/slab.h>
17#include <linux/err.h>
18#include <linux/export.h>
19#include <linux/module.h>
20#include <linux/miscdevice.h>
21
22#include <asm/reg.h>
23#include <asm/cputable.h>
24#include <asm/tlbflush.h>
25#include <asm/kvm_ppc.h>
26
27#include "../mm/mmu_decl.h"
28#include "booke.h"
29#include "e500.h"
30
31struct id {
32 unsigned long val;
33 struct id **pentry;
34};
35
36#define NUM_TIDS 256
37
38
39
40
41
42
43
44
45
46
47struct vcpu_id_table {
48 struct id id[2][NUM_TIDS][2];
49};
50
51
52
53
54
55
56struct pcpu_id_table {
57 struct id *entry[NUM_TIDS];
58};
59
60static DEFINE_PER_CPU(struct pcpu_id_table, pcpu_sids);
61
62
63
64static DEFINE_PER_CPU(unsigned long, pcpu_last_used_sid);
65
66
67
68
69
70
71
72
73
74static inline int local_sid_setup_one(struct id *entry)
75{
76 unsigned long sid;
77 int ret = -1;
78
79 sid = ++(__get_cpu_var(pcpu_last_used_sid));
80 if (sid < NUM_TIDS) {
81 __get_cpu_var(pcpu_sids).entry[sid] = entry;
82 entry->val = sid;
83 entry->pentry = &__get_cpu_var(pcpu_sids).entry[sid];
84 ret = sid;
85 }
86
87
88
89
90
91
92
93
94 WARN_ON(sid > NUM_TIDS);
95
96 return ret;
97}
98
99
100
101
102
103
104
105
106
107
108static inline int local_sid_lookup(struct id *entry)
109{
110 if (entry && entry->val != 0 &&
111 __get_cpu_var(pcpu_sids).entry[entry->val] == entry &&
112 entry->pentry == &__get_cpu_var(pcpu_sids).entry[entry->val])
113 return entry->val;
114 return -1;
115}
116
117
118static inline void local_sid_destroy_all(void)
119{
120 __get_cpu_var(pcpu_last_used_sid) = 0;
121 memset(&__get_cpu_var(pcpu_sids), 0, sizeof(__get_cpu_var(pcpu_sids)));
122}
123
124static void *kvmppc_e500_id_table_alloc(struct kvmppc_vcpu_e500 *vcpu_e500)
125{
126 vcpu_e500->idt = kzalloc(sizeof(struct vcpu_id_table), GFP_KERNEL);
127 return vcpu_e500->idt;
128}
129
130static void kvmppc_e500_id_table_free(struct kvmppc_vcpu_e500 *vcpu_e500)
131{
132 kfree(vcpu_e500->idt);
133 vcpu_e500->idt = NULL;
134}
135
136
137
138
139
140static void kvmppc_e500_recalc_shadow_pid(struct kvmppc_vcpu_e500 *vcpu_e500)
141{
142 preempt_disable();
143 vcpu_e500->vcpu.arch.shadow_pid = kvmppc_e500_get_sid(vcpu_e500,
144 get_cur_as(&vcpu_e500->vcpu),
145 get_cur_pid(&vcpu_e500->vcpu),
146 get_cur_pr(&vcpu_e500->vcpu), 1);
147 vcpu_e500->vcpu.arch.shadow_pid1 = kvmppc_e500_get_sid(vcpu_e500,
148 get_cur_as(&vcpu_e500->vcpu), 0,
149 get_cur_pr(&vcpu_e500->vcpu), 1);
150 preempt_enable();
151}
152
153
154static void kvmppc_e500_id_table_reset_all(struct kvmppc_vcpu_e500 *vcpu_e500)
155{
156 memset(vcpu_e500->idt, 0, sizeof(struct vcpu_id_table));
157
158
159 kvmppc_e500_recalc_shadow_pid(vcpu_e500);
160}
161
162
163static inline void kvmppc_e500_id_table_reset_one(
164 struct kvmppc_vcpu_e500 *vcpu_e500,
165 int as, int pid, int pr)
166{
167 struct vcpu_id_table *idt = vcpu_e500->idt;
168
169 BUG_ON(as >= 2);
170 BUG_ON(pid >= NUM_TIDS);
171 BUG_ON(pr >= 2);
172
173 idt->id[as][pid][pr].val = 0;
174 idt->id[as][pid][pr].pentry = NULL;
175
176
177 kvmppc_e500_recalc_shadow_pid(vcpu_e500);
178}
179
180
181
182
183
184
185
186
187
188
189unsigned int kvmppc_e500_get_sid(struct kvmppc_vcpu_e500 *vcpu_e500,
190 unsigned int as, unsigned int gid,
191 unsigned int pr, int avoid_recursion)
192{
193 struct vcpu_id_table *idt = vcpu_e500->idt;
194 int sid;
195
196 BUG_ON(as >= 2);
197 BUG_ON(gid >= NUM_TIDS);
198 BUG_ON(pr >= 2);
199
200 sid = local_sid_lookup(&idt->id[as][gid][pr]);
201
202 while (sid <= 0) {
203
204 sid = local_sid_setup_one(&idt->id[as][gid][pr]);
205 if (sid <= 0) {
206 _tlbil_all();
207 local_sid_destroy_all();
208 }
209
210
211 if (!avoid_recursion)
212 kvmppc_e500_recalc_shadow_pid(vcpu_e500);
213 }
214
215 return sid;
216}
217
218unsigned int kvmppc_e500_get_tlb_stid(struct kvm_vcpu *vcpu,
219 struct kvm_book3e_206_tlb_entry *gtlbe)
220{
221 return kvmppc_e500_get_sid(to_e500(vcpu), get_tlb_ts(gtlbe),
222 get_tlb_tid(gtlbe), get_cur_pr(vcpu), 0);
223}
224
225void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid)
226{
227 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
228
229 if (vcpu->arch.pid != pid) {
230 vcpu_e500->pid[0] = vcpu->arch.pid = pid;
231 kvmppc_e500_recalc_shadow_pid(vcpu_e500);
232 }
233}
234
235
236void kvmppc_e500_tlbil_one(struct kvmppc_vcpu_e500 *vcpu_e500,
237 struct kvm_book3e_206_tlb_entry *gtlbe)
238{
239 struct vcpu_id_table *idt = vcpu_e500->idt;
240 unsigned int pr, tid, ts, pid;
241 u32 val, eaddr;
242 unsigned long flags;
243
244 ts = get_tlb_ts(gtlbe);
245 tid = get_tlb_tid(gtlbe);
246
247 preempt_disable();
248
249
250 for (pr = 0; pr < 2; pr++) {
251
252
253
254
255
256
257
258
259
260 pid = local_sid_lookup(&idt->id[ts][tid][pr]);
261 if (pid <= 0) {
262 kvmppc_e500_id_table_reset_one(vcpu_e500, ts, tid, pr);
263 continue;
264 }
265
266
267
268
269
270
271
272 val = (pid << MAS6_SPID_SHIFT) | MAS6_SAS;
273 eaddr = get_tlb_eaddr(gtlbe);
274
275 local_irq_save(flags);
276
277 mtspr(SPRN_MAS6, val);
278 asm volatile("tlbsx 0, %[eaddr]" : : [eaddr] "r" (eaddr));
279 val = mfspr(SPRN_MAS1);
280 if (val & MAS1_VALID) {
281 mtspr(SPRN_MAS1, val & ~MAS1_VALID);
282 asm volatile("tlbwe");
283 }
284
285 local_irq_restore(flags);
286 }
287
288 preempt_enable();
289}
290
291void kvmppc_e500_tlbil_all(struct kvmppc_vcpu_e500 *vcpu_e500)
292{
293 kvmppc_e500_id_table_reset_all(vcpu_e500);
294}
295
296void kvmppc_mmu_msr_notify(struct kvm_vcpu *vcpu, u32 old_msr)
297{
298
299 kvmppc_e500_recalc_shadow_pid(to_e500(vcpu));
300}
301
302void kvmppc_core_load_host_debugstate(struct kvm_vcpu *vcpu)
303{
304}
305
306void kvmppc_core_load_guest_debugstate(struct kvm_vcpu *vcpu)
307{
308}
309
310static void kvmppc_core_vcpu_load_e500(struct kvm_vcpu *vcpu, int cpu)
311{
312 kvmppc_booke_vcpu_load(vcpu, cpu);
313
314
315 kvmppc_e500_recalc_shadow_pid(to_e500(vcpu));
316}
317
318static void kvmppc_core_vcpu_put_e500(struct kvm_vcpu *vcpu)
319{
320#ifdef CONFIG_SPE
321 if (vcpu->arch.shadow_msr & MSR_SPE)
322 kvmppc_vcpu_disable_spe(vcpu);
323#endif
324
325 kvmppc_booke_vcpu_put(vcpu);
326}
327
328int kvmppc_core_check_processor_compat(void)
329{
330 int r;
331
332 if (strcmp(cur_cpu_spec->cpu_name, "e500v2") == 0)
333 r = 0;
334 else
335 r = -ENOTSUPP;
336
337 return r;
338}
339
340static void kvmppc_e500_tlb_setup(struct kvmppc_vcpu_e500 *vcpu_e500)
341{
342 struct kvm_book3e_206_tlb_entry *tlbe;
343
344
345 tlbe = get_entry(vcpu_e500, 1, 0);
346 tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOK3E_PAGESZ_256M);
347 tlbe->mas2 = 0;
348 tlbe->mas7_3 = E500_TLB_SUPER_PERM_MASK;
349
350
351 tlbe = get_entry(vcpu_e500, 1, 1);
352 tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOK3E_PAGESZ_4K);
353 tlbe->mas2 = (0xe0004500 & 0xFFFFF000) | MAS2_I | MAS2_G;
354 tlbe->mas7_3 = (0xe0004500 & 0xFFFFF000) | E500_TLB_SUPER_PERM_MASK;
355}
356
357int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu)
358{
359 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
360
361 kvmppc_e500_tlb_setup(vcpu_e500);
362
363
364 vcpu->arch.pvr = mfspr(SPRN_PVR);
365 vcpu_e500->svr = mfspr(SPRN_SVR);
366
367 vcpu->arch.cpu_type = KVM_CPU_E500V2;
368
369 return 0;
370}
371
372static int kvmppc_core_get_sregs_e500(struct kvm_vcpu *vcpu,
373 struct kvm_sregs *sregs)
374{
375 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
376
377 sregs->u.e.features |= KVM_SREGS_E_ARCH206_MMU | KVM_SREGS_E_SPE |
378 KVM_SREGS_E_PM;
379 sregs->u.e.impl_id = KVM_SREGS_E_IMPL_FSL;
380
381 sregs->u.e.impl.fsl.features = 0;
382 sregs->u.e.impl.fsl.svr = vcpu_e500->svr;
383 sregs->u.e.impl.fsl.hid0 = vcpu_e500->hid0;
384 sregs->u.e.impl.fsl.mcar = vcpu_e500->mcar;
385
386 sregs->u.e.ivor_high[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_UNAVAIL];
387 sregs->u.e.ivor_high[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_DATA];
388 sregs->u.e.ivor_high[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_ROUND];
389 sregs->u.e.ivor_high[3] =
390 vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR];
391
392 kvmppc_get_sregs_ivor(vcpu, sregs);
393 kvmppc_get_sregs_e500_tlb(vcpu, sregs);
394 return 0;
395}
396
397static int kvmppc_core_set_sregs_e500(struct kvm_vcpu *vcpu,
398 struct kvm_sregs *sregs)
399{
400 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
401 int ret;
402
403 if (sregs->u.e.impl_id == KVM_SREGS_E_IMPL_FSL) {
404 vcpu_e500->svr = sregs->u.e.impl.fsl.svr;
405 vcpu_e500->hid0 = sregs->u.e.impl.fsl.hid0;
406 vcpu_e500->mcar = sregs->u.e.impl.fsl.mcar;
407 }
408
409 ret = kvmppc_set_sregs_e500_tlb(vcpu, sregs);
410 if (ret < 0)
411 return ret;
412
413 if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
414 return 0;
415
416 if (sregs->u.e.features & KVM_SREGS_E_SPE) {
417 vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_UNAVAIL] =
418 sregs->u.e.ivor_high[0];
419 vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_DATA] =
420 sregs->u.e.ivor_high[1];
421 vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_ROUND] =
422 sregs->u.e.ivor_high[2];
423 }
424
425 if (sregs->u.e.features & KVM_SREGS_E_PM) {
426 vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR] =
427 sregs->u.e.ivor_high[3];
428 }
429
430 return kvmppc_set_sregs_ivor(vcpu, sregs);
431}
432
433static int kvmppc_get_one_reg_e500(struct kvm_vcpu *vcpu, u64 id,
434 union kvmppc_one_reg *val)
435{
436 int r = kvmppc_get_one_reg_e500_tlb(vcpu, id, val);
437 return r;
438}
439
440static int kvmppc_set_one_reg_e500(struct kvm_vcpu *vcpu, u64 id,
441 union kvmppc_one_reg *val)
442{
443 int r = kvmppc_get_one_reg_e500_tlb(vcpu, id, val);
444 return r;
445}
446
447static struct kvm_vcpu *kvmppc_core_vcpu_create_e500(struct kvm *kvm,
448 unsigned int id)
449{
450 struct kvmppc_vcpu_e500 *vcpu_e500;
451 struct kvm_vcpu *vcpu;
452 int err;
453
454 vcpu_e500 = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
455 if (!vcpu_e500) {
456 err = -ENOMEM;
457 goto out;
458 }
459
460 vcpu = &vcpu_e500->vcpu;
461 err = kvm_vcpu_init(vcpu, kvm, id);
462 if (err)
463 goto free_vcpu;
464
465 if (kvmppc_e500_id_table_alloc(vcpu_e500) == NULL)
466 goto uninit_vcpu;
467
468 err = kvmppc_e500_tlb_init(vcpu_e500);
469 if (err)
470 goto uninit_id;
471
472 vcpu->arch.shared = (void*)__get_free_page(GFP_KERNEL|__GFP_ZERO);
473 if (!vcpu->arch.shared)
474 goto uninit_tlb;
475
476 return vcpu;
477
478uninit_tlb:
479 kvmppc_e500_tlb_uninit(vcpu_e500);
480uninit_id:
481 kvmppc_e500_id_table_free(vcpu_e500);
482uninit_vcpu:
483 kvm_vcpu_uninit(vcpu);
484free_vcpu:
485 kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
486out:
487 return ERR_PTR(err);
488}
489
490static void kvmppc_core_vcpu_free_e500(struct kvm_vcpu *vcpu)
491{
492 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
493
494 free_page((unsigned long)vcpu->arch.shared);
495 kvmppc_e500_tlb_uninit(vcpu_e500);
496 kvmppc_e500_id_table_free(vcpu_e500);
497 kvm_vcpu_uninit(vcpu);
498 kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
499}
500
501static int kvmppc_core_init_vm_e500(struct kvm *kvm)
502{
503 return 0;
504}
505
506static void kvmppc_core_destroy_vm_e500(struct kvm *kvm)
507{
508}
509
510static struct kvmppc_ops kvm_ops_e500 = {
511 .get_sregs = kvmppc_core_get_sregs_e500,
512 .set_sregs = kvmppc_core_set_sregs_e500,
513 .get_one_reg = kvmppc_get_one_reg_e500,
514 .set_one_reg = kvmppc_set_one_reg_e500,
515 .vcpu_load = kvmppc_core_vcpu_load_e500,
516 .vcpu_put = kvmppc_core_vcpu_put_e500,
517 .vcpu_create = kvmppc_core_vcpu_create_e500,
518 .vcpu_free = kvmppc_core_vcpu_free_e500,
519 .mmu_destroy = kvmppc_mmu_destroy_e500,
520 .init_vm = kvmppc_core_init_vm_e500,
521 .destroy_vm = kvmppc_core_destroy_vm_e500,
522 .emulate_op = kvmppc_core_emulate_op_e500,
523 .emulate_mtspr = kvmppc_core_emulate_mtspr_e500,
524 .emulate_mfspr = kvmppc_core_emulate_mfspr_e500,
525};
526
527static int __init kvmppc_e500_init(void)
528{
529 int r, i;
530 unsigned long ivor[3];
531
532 unsigned long *handler = &kvmppc_booke_handler_addr[16];
533 unsigned long handler_len;
534 unsigned long max_ivor = 0;
535
536 r = kvmppc_core_check_processor_compat();
537 if (r)
538 goto err_out;
539
540 r = kvmppc_booke_init();
541 if (r)
542 goto err_out;
543
544
545 ivor[0] = mfspr(SPRN_IVOR32);
546 ivor[1] = mfspr(SPRN_IVOR33);
547 ivor[2] = mfspr(SPRN_IVOR34);
548 for (i = 0; i < 3; i++) {
549 if (ivor[i] > ivor[max_ivor])
550 max_ivor = i;
551
552 handler_len = handler[i + 1] - handler[i];
553 memcpy((void *)kvmppc_booke_handlers + ivor[i],
554 (void *)handler[i], handler_len);
555 }
556 handler_len = handler[max_ivor + 1] - handler[max_ivor];
557 flush_icache_range(kvmppc_booke_handlers, kvmppc_booke_handlers +
558 ivor[max_ivor] + handler_len);
559
560 r = kvm_init(NULL, sizeof(struct kvmppc_vcpu_e500), 0, THIS_MODULE);
561 if (r)
562 goto err_out;
563 kvm_ops_e500.owner = THIS_MODULE;
564 kvmppc_pr_ops = &kvm_ops_e500;
565
566err_out:
567 return r;
568}
569
570static void __exit kvmppc_e500_exit(void)
571{
572 kvmppc_pr_ops = NULL;
573 kvmppc_booke_exit();
574}
575
576module_init(kvmppc_e500_init);
577module_exit(kvmppc_e500_exit);
578MODULE_ALIAS_MISCDEV(KVM_MINOR);
579MODULE_ALIAS("devname:kvm");
580