KVM: PPC: e500mc: Add support for single threaded vcpus on e6500 core
[firefly-linux-kernel-4.4.55.git] / arch / powerpc / kvm / e500mc.c
1 /*
2  * Copyright (C) 2010,2012 Freescale Semiconductor, Inc. All rights reserved.
3  *
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 #include <linux/miscdevice.h>
20 #include <linux/module.h>
21
22 #include <asm/reg.h>
23 #include <asm/cputable.h>
24 #include <asm/tlbflush.h>
25 #include <asm/kvm_ppc.h>
26 #include <asm/dbell.h>
27
28 #include "booke.h"
29 #include "e500.h"
30
31 void kvmppc_set_pending_interrupt(struct kvm_vcpu *vcpu, enum int_class type)
32 {
33         enum ppc_dbell dbell_type;
34         unsigned long tag;
35
36         switch (type) {
37         case INT_CLASS_NONCRIT:
38                 dbell_type = PPC_G_DBELL;
39                 break;
40         case INT_CLASS_CRIT:
41                 dbell_type = PPC_G_DBELL_CRIT;
42                 break;
43         case INT_CLASS_MC:
44                 dbell_type = PPC_G_DBELL_MC;
45                 break;
46         default:
47                 WARN_ONCE(1, "%s: unknown int type %d\n", __func__, type);
48                 return;
49         }
50
51         preempt_disable();
52         tag = PPC_DBELL_LPID(get_lpid(vcpu)) | vcpu->vcpu_id;
53         mb();
54         ppc_msgsnd(dbell_type, 0, tag);
55         preempt_enable();
56 }
57
58 /* gtlbe must not be mapped by more than one host tlb entry */
59 void kvmppc_e500_tlbil_one(struct kvmppc_vcpu_e500 *vcpu_e500,
60                            struct kvm_book3e_206_tlb_entry *gtlbe)
61 {
62         unsigned int tid, ts;
63         gva_t eaddr;
64         u32 val;
65         unsigned long flags;
66
67         ts = get_tlb_ts(gtlbe);
68         tid = get_tlb_tid(gtlbe);
69
70         /* We search the host TLB to invalidate its shadow TLB entry */
71         val = (tid << 16) | ts;
72         eaddr = get_tlb_eaddr(gtlbe);
73
74         local_irq_save(flags);
75
76         mtspr(SPRN_MAS6, val);
77         mtspr(SPRN_MAS5, MAS5_SGS | get_lpid(&vcpu_e500->vcpu));
78
79         asm volatile("tlbsx 0, %[eaddr]\n" : : [eaddr] "r" (eaddr));
80         val = mfspr(SPRN_MAS1);
81         if (val & MAS1_VALID) {
82                 mtspr(SPRN_MAS1, val & ~MAS1_VALID);
83                 asm volatile("tlbwe");
84         }
85         mtspr(SPRN_MAS5, 0);
86         /* NOTE: tlbsx also updates mas8, so clear it for host tlbwe */
87         mtspr(SPRN_MAS8, 0);
88         isync();
89
90         local_irq_restore(flags);
91 }
92
93 void kvmppc_e500_tlbil_all(struct kvmppc_vcpu_e500 *vcpu_e500)
94 {
95         unsigned long flags;
96
97         local_irq_save(flags);
98         mtspr(SPRN_MAS5, MAS5_SGS | get_lpid(&vcpu_e500->vcpu));
99         asm volatile("tlbilxlpid");
100         mtspr(SPRN_MAS5, 0);
101         local_irq_restore(flags);
102 }
103
104 void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid)
105 {
106         vcpu->arch.pid = pid;
107 }
108
109 void kvmppc_mmu_msr_notify(struct kvm_vcpu *vcpu, u32 old_msr)
110 {
111 }
112
113 /* We use two lpids per VM */
114 static DEFINE_PER_CPU(struct kvm_vcpu *[KVMPPC_NR_LPIDS], last_vcpu_of_lpid);
115
116 static void kvmppc_core_vcpu_load_e500mc(struct kvm_vcpu *vcpu, int cpu)
117 {
118         struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
119
120         kvmppc_booke_vcpu_load(vcpu, cpu);
121
122         mtspr(SPRN_LPID, get_lpid(vcpu));
123         mtspr(SPRN_EPCR, vcpu->arch.shadow_epcr);
124         mtspr(SPRN_GPIR, vcpu->vcpu_id);
125         mtspr(SPRN_MSRP, vcpu->arch.shadow_msrp);
126         vcpu->arch.eplc = EPC_EGS | (get_lpid(vcpu) << EPC_ELPID_SHIFT);
127         vcpu->arch.epsc = vcpu->arch.eplc;
128         mtspr(SPRN_EPLC, vcpu->arch.eplc);
129         mtspr(SPRN_EPSC, vcpu->arch.epsc);
130
131         mtspr(SPRN_GIVPR, vcpu->arch.ivpr);
132         mtspr(SPRN_GIVOR2, vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE]);
133         mtspr(SPRN_GIVOR8, vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL]);
134         mtspr(SPRN_GSPRG0, (unsigned long)vcpu->arch.shared->sprg0);
135         mtspr(SPRN_GSPRG1, (unsigned long)vcpu->arch.shared->sprg1);
136         mtspr(SPRN_GSPRG2, (unsigned long)vcpu->arch.shared->sprg2);
137         mtspr(SPRN_GSPRG3, (unsigned long)vcpu->arch.shared->sprg3);
138
139         mtspr(SPRN_GSRR0, vcpu->arch.shared->srr0);
140         mtspr(SPRN_GSRR1, vcpu->arch.shared->srr1);
141
142         mtspr(SPRN_GEPR, vcpu->arch.epr);
143         mtspr(SPRN_GDEAR, vcpu->arch.shared->dar);
144         mtspr(SPRN_GESR, vcpu->arch.shared->esr);
145
146         if (vcpu->arch.oldpir != mfspr(SPRN_PIR) ||
147             __get_cpu_var(last_vcpu_of_lpid)[get_lpid(vcpu)] != vcpu) {
148                 kvmppc_e500_tlbil_all(vcpu_e500);
149                 __get_cpu_var(last_vcpu_of_lpid)[get_lpid(vcpu)] = vcpu;
150         }
151 }
152
153 static void kvmppc_core_vcpu_put_e500mc(struct kvm_vcpu *vcpu)
154 {
155         vcpu->arch.eplc = mfspr(SPRN_EPLC);
156         vcpu->arch.epsc = mfspr(SPRN_EPSC);
157
158         vcpu->arch.shared->sprg0 = mfspr(SPRN_GSPRG0);
159         vcpu->arch.shared->sprg1 = mfspr(SPRN_GSPRG1);
160         vcpu->arch.shared->sprg2 = mfspr(SPRN_GSPRG2);
161         vcpu->arch.shared->sprg3 = mfspr(SPRN_GSPRG3);
162
163         vcpu->arch.shared->srr0 = mfspr(SPRN_GSRR0);
164         vcpu->arch.shared->srr1 = mfspr(SPRN_GSRR1);
165
166         vcpu->arch.epr = mfspr(SPRN_GEPR);
167         vcpu->arch.shared->dar = mfspr(SPRN_GDEAR);
168         vcpu->arch.shared->esr = mfspr(SPRN_GESR);
169
170         vcpu->arch.oldpir = mfspr(SPRN_PIR);
171
172         kvmppc_booke_vcpu_put(vcpu);
173 }
174
175 int kvmppc_core_check_processor_compat(void)
176 {
177         int r;
178
179         if (strcmp(cur_cpu_spec->cpu_name, "e500mc") == 0)
180                 r = 0;
181         else if (strcmp(cur_cpu_spec->cpu_name, "e5500") == 0)
182                 r = 0;
183         else
184                 r = -ENOTSUPP;
185
186         return r;
187 }
188
189 int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu)
190 {
191         struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
192
193         vcpu->arch.shadow_epcr = SPRN_EPCR_DSIGS | SPRN_EPCR_DGTMI | \
194                                  SPRN_EPCR_DUVD;
195 #ifdef CONFIG_64BIT
196         vcpu->arch.shadow_epcr |= SPRN_EPCR_ICM;
197 #endif
198         vcpu->arch.shadow_msrp = MSRP_UCLEP | MSRP_PMMP;
199
200         vcpu->arch.pvr = mfspr(SPRN_PVR);
201         vcpu_e500->svr = mfspr(SPRN_SVR);
202
203         vcpu->arch.cpu_type = KVM_CPU_E500MC;
204
205         return 0;
206 }
207
208 static int kvmppc_core_get_sregs_e500mc(struct kvm_vcpu *vcpu,
209                                         struct kvm_sregs *sregs)
210 {
211         struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
212
213         sregs->u.e.features |= KVM_SREGS_E_ARCH206_MMU | KVM_SREGS_E_PM |
214                                KVM_SREGS_E_PC;
215         sregs->u.e.impl_id = KVM_SREGS_E_IMPL_FSL;
216
217         sregs->u.e.impl.fsl.features = 0;
218         sregs->u.e.impl.fsl.svr = vcpu_e500->svr;
219         sregs->u.e.impl.fsl.hid0 = vcpu_e500->hid0;
220         sregs->u.e.impl.fsl.mcar = vcpu_e500->mcar;
221
222         kvmppc_get_sregs_e500_tlb(vcpu, sregs);
223
224         sregs->u.e.ivor_high[3] =
225                 vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR];
226         sregs->u.e.ivor_high[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL];
227         sregs->u.e.ivor_high[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL_CRIT];
228
229         return kvmppc_get_sregs_ivor(vcpu, sregs);
230 }
231
232 static int kvmppc_core_set_sregs_e500mc(struct kvm_vcpu *vcpu,
233                                         struct kvm_sregs *sregs)
234 {
235         struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
236         int ret;
237
238         if (sregs->u.e.impl_id == KVM_SREGS_E_IMPL_FSL) {
239                 vcpu_e500->svr = sregs->u.e.impl.fsl.svr;
240                 vcpu_e500->hid0 = sregs->u.e.impl.fsl.hid0;
241                 vcpu_e500->mcar = sregs->u.e.impl.fsl.mcar;
242         }
243
244         ret = kvmppc_set_sregs_e500_tlb(vcpu, sregs);
245         if (ret < 0)
246                 return ret;
247
248         if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
249                 return 0;
250
251         if (sregs->u.e.features & KVM_SREGS_E_PM) {
252                 vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR] =
253                         sregs->u.e.ivor_high[3];
254         }
255
256         if (sregs->u.e.features & KVM_SREGS_E_PC) {
257                 vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL] =
258                         sregs->u.e.ivor_high[4];
259                 vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL_CRIT] =
260                         sregs->u.e.ivor_high[5];
261         }
262
263         return kvmppc_set_sregs_ivor(vcpu, sregs);
264 }
265
266 static int kvmppc_get_one_reg_e500mc(struct kvm_vcpu *vcpu, u64 id,
267                               union kvmppc_one_reg *val)
268 {
269         int r = 0;
270
271         switch (id) {
272         case KVM_REG_PPC_SPRG9:
273                 *val = get_reg_val(id, vcpu->arch.sprg9);
274                 break;
275         default:
276                 r = kvmppc_get_one_reg_e500_tlb(vcpu, id, val);
277         }
278
279         return r;
280 }
281
282 static int kvmppc_set_one_reg_e500mc(struct kvm_vcpu *vcpu, u64 id,
283                               union kvmppc_one_reg *val)
284 {
285         int r = 0;
286
287         switch (id) {
288         case KVM_REG_PPC_SPRG9:
289                 vcpu->arch.sprg9 = set_reg_val(id, *val);
290                 break;
291         default:
292                 r = kvmppc_set_one_reg_e500_tlb(vcpu, id, val);
293         }
294
295         return r;
296 }
297
298 static struct kvm_vcpu *kvmppc_core_vcpu_create_e500mc(struct kvm *kvm,
299                                                        unsigned int id)
300 {
301         struct kvmppc_vcpu_e500 *vcpu_e500;
302         struct kvm_vcpu *vcpu;
303         int err;
304
305         vcpu_e500 = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
306         if (!vcpu_e500) {
307                 err = -ENOMEM;
308                 goto out;
309         }
310         vcpu = &vcpu_e500->vcpu;
311
312         /* Invalid PIR value -- this LPID dosn't have valid state on any cpu */
313         vcpu->arch.oldpir = 0xffffffff;
314
315         err = kvm_vcpu_init(vcpu, kvm, id);
316         if (err)
317                 goto free_vcpu;
318
319         err = kvmppc_e500_tlb_init(vcpu_e500);
320         if (err)
321                 goto uninit_vcpu;
322
323         vcpu->arch.shared = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
324         if (!vcpu->arch.shared)
325                 goto uninit_tlb;
326
327         return vcpu;
328
329 uninit_tlb:
330         kvmppc_e500_tlb_uninit(vcpu_e500);
331 uninit_vcpu:
332         kvm_vcpu_uninit(vcpu);
333
334 free_vcpu:
335         kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
336 out:
337         return ERR_PTR(err);
338 }
339
340 static void kvmppc_core_vcpu_free_e500mc(struct kvm_vcpu *vcpu)
341 {
342         struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
343
344         free_page((unsigned long)vcpu->arch.shared);
345         kvmppc_e500_tlb_uninit(vcpu_e500);
346         kvm_vcpu_uninit(vcpu);
347         kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
348 }
349
350 static int kvmppc_core_init_vm_e500mc(struct kvm *kvm)
351 {
352         int lpid;
353
354         lpid = kvmppc_alloc_lpid();
355         if (lpid < 0)
356                 return lpid;
357
358         /*
359          * Use two lpids per VM on cores with two threads like e6500. Use
360          * even numbers to speedup vcpu lpid computation with consecutive lpids
361          * per VM. vm1 will use lpids 2 and 3, vm2 lpids 4 and 5, and so on.
362          */
363         if (threads_per_core == 2)
364                 lpid <<= 1;
365
366         kvm->arch.lpid = lpid;
367         return 0;
368 }
369
370 static void kvmppc_core_destroy_vm_e500mc(struct kvm *kvm)
371 {
372         int lpid = kvm->arch.lpid;
373
374         if (threads_per_core == 2)
375                 lpid >>= 1;
376
377         kvmppc_free_lpid(lpid);
378 }
379
380 static struct kvmppc_ops kvm_ops_e500mc = {
381         .get_sregs = kvmppc_core_get_sregs_e500mc,
382         .set_sregs = kvmppc_core_set_sregs_e500mc,
383         .get_one_reg = kvmppc_get_one_reg_e500mc,
384         .set_one_reg = kvmppc_set_one_reg_e500mc,
385         .vcpu_load   = kvmppc_core_vcpu_load_e500mc,
386         .vcpu_put    = kvmppc_core_vcpu_put_e500mc,
387         .vcpu_create = kvmppc_core_vcpu_create_e500mc,
388         .vcpu_free   = kvmppc_core_vcpu_free_e500mc,
389         .mmu_destroy  = kvmppc_mmu_destroy_e500,
390         .init_vm = kvmppc_core_init_vm_e500mc,
391         .destroy_vm = kvmppc_core_destroy_vm_e500mc,
392         .emulate_op = kvmppc_core_emulate_op_e500,
393         .emulate_mtspr = kvmppc_core_emulate_mtspr_e500,
394         .emulate_mfspr = kvmppc_core_emulate_mfspr_e500,
395 };
396
397 static int __init kvmppc_e500mc_init(void)
398 {
399         int r;
400
401         r = kvmppc_booke_init();
402         if (r)
403                 goto err_out;
404
405         /*
406          * Use two lpids per VM on dual threaded processors like e6500
407          * to workarround the lack of tlb write conditional instruction.
408          * Expose half the number of available hardware lpids to the lpid
409          * allocator.
410          */
411         kvmppc_init_lpid(KVMPPC_NR_LPIDS/threads_per_core);
412         kvmppc_claim_lpid(0); /* host */
413
414         r = kvm_init(NULL, sizeof(struct kvmppc_vcpu_e500), 0, THIS_MODULE);
415         if (r)
416                 goto err_out;
417         kvm_ops_e500mc.owner = THIS_MODULE;
418         kvmppc_pr_ops = &kvm_ops_e500mc;
419
420 err_out:
421         return r;
422 }
423
424 static void __exit kvmppc_e500mc_exit(void)
425 {
426         kvmppc_pr_ops = NULL;
427         kvmppc_booke_exit();
428 }
429
430 module_init(kvmppc_e500mc_init);
431 module_exit(kvmppc_e500mc_exit);
432 MODULE_ALIAS_MISCDEV(KVM_MINOR);
433 MODULE_ALIAS("devname:kvm");