454934990672f50b8cae6703816ef6e25e5a91d2
[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
52         tag = PPC_DBELL_LPID(vcpu->kvm->arch.lpid) | vcpu->vcpu_id;
53         mb();
54         ppc_msgsnd(dbell_type, 0, tag);
55 }
56
57 /* gtlbe must not be mapped by more than one host tlb entry */
58 void kvmppc_e500_tlbil_one(struct kvmppc_vcpu_e500 *vcpu_e500,
59                            struct kvm_book3e_206_tlb_entry *gtlbe)
60 {
61         unsigned int tid, ts;
62         gva_t eaddr;
63         u32 val, lpid;
64         unsigned long flags;
65
66         ts = get_tlb_ts(gtlbe);
67         tid = get_tlb_tid(gtlbe);
68         lpid = vcpu_e500->vcpu.kvm->arch.lpid;
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 | lpid);
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 | vcpu_e500->vcpu.kvm->arch.lpid);
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 static DEFINE_PER_CPU(struct kvm_vcpu *[KVMPPC_NR_LPIDS], last_vcpu_of_lpid);
114
115 static void kvmppc_core_vcpu_load_e500mc(struct kvm_vcpu *vcpu, int cpu)
116 {
117         struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
118
119         kvmppc_booke_vcpu_load(vcpu, cpu);
120
121         mtspr(SPRN_LPID, vcpu->kvm->arch.lpid);
122         mtspr(SPRN_EPCR, vcpu->arch.shadow_epcr);
123         mtspr(SPRN_GPIR, vcpu->vcpu_id);
124         mtspr(SPRN_MSRP, vcpu->arch.shadow_msrp);
125         mtspr(SPRN_EPLC, vcpu->arch.eplc);
126         mtspr(SPRN_EPSC, vcpu->arch.epsc);
127
128         mtspr(SPRN_GIVPR, vcpu->arch.ivpr);
129         mtspr(SPRN_GIVOR2, vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE]);
130         mtspr(SPRN_GIVOR8, vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL]);
131         mtspr(SPRN_GSPRG0, (unsigned long)vcpu->arch.shared->sprg0);
132         mtspr(SPRN_GSPRG1, (unsigned long)vcpu->arch.shared->sprg1);
133         mtspr(SPRN_GSPRG2, (unsigned long)vcpu->arch.shared->sprg2);
134         mtspr(SPRN_GSPRG3, (unsigned long)vcpu->arch.shared->sprg3);
135
136         mtspr(SPRN_GSRR0, vcpu->arch.shared->srr0);
137         mtspr(SPRN_GSRR1, vcpu->arch.shared->srr1);
138
139         mtspr(SPRN_GEPR, vcpu->arch.epr);
140         mtspr(SPRN_GDEAR, vcpu->arch.shared->dar);
141         mtspr(SPRN_GESR, vcpu->arch.shared->esr);
142
143         if (vcpu->arch.oldpir != mfspr(SPRN_PIR) ||
144             __get_cpu_var(last_vcpu_of_lpid)[vcpu->kvm->arch.lpid] != vcpu) {
145                 kvmppc_e500_tlbil_all(vcpu_e500);
146                 __get_cpu_var(last_vcpu_of_lpid)[vcpu->kvm->arch.lpid] = vcpu;
147         }
148 }
149
150 static void kvmppc_core_vcpu_put_e500mc(struct kvm_vcpu *vcpu)
151 {
152         vcpu->arch.eplc = mfspr(SPRN_EPLC);
153         vcpu->arch.epsc = mfspr(SPRN_EPSC);
154
155         vcpu->arch.shared->sprg0 = mfspr(SPRN_GSPRG0);
156         vcpu->arch.shared->sprg1 = mfspr(SPRN_GSPRG1);
157         vcpu->arch.shared->sprg2 = mfspr(SPRN_GSPRG2);
158         vcpu->arch.shared->sprg3 = mfspr(SPRN_GSPRG3);
159
160         vcpu->arch.shared->srr0 = mfspr(SPRN_GSRR0);
161         vcpu->arch.shared->srr1 = mfspr(SPRN_GSRR1);
162
163         vcpu->arch.epr = mfspr(SPRN_GEPR);
164         vcpu->arch.shared->dar = mfspr(SPRN_GDEAR);
165         vcpu->arch.shared->esr = mfspr(SPRN_GESR);
166
167         vcpu->arch.oldpir = mfspr(SPRN_PIR);
168
169         kvmppc_booke_vcpu_put(vcpu);
170 }
171
172 int kvmppc_core_check_processor_compat(void)
173 {
174         int r;
175
176         if (strcmp(cur_cpu_spec->cpu_name, "e500mc") == 0)
177                 r = 0;
178         else if (strcmp(cur_cpu_spec->cpu_name, "e5500") == 0)
179                 r = 0;
180         else
181                 r = -ENOTSUPP;
182
183         return r;
184 }
185
186 int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu)
187 {
188         struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
189
190         vcpu->arch.shadow_epcr = SPRN_EPCR_DSIGS | SPRN_EPCR_DGTMI | \
191                                  SPRN_EPCR_DUVD;
192 #ifdef CONFIG_64BIT
193         vcpu->arch.shadow_epcr |= SPRN_EPCR_ICM;
194 #endif
195         vcpu->arch.shadow_msrp = MSRP_UCLEP | MSRP_PMMP;
196         vcpu->arch.eplc = EPC_EGS | (vcpu->kvm->arch.lpid << EPC_ELPID_SHIFT);
197         vcpu->arch.epsc = vcpu->arch.eplc;
198
199         vcpu->arch.pvr = mfspr(SPRN_PVR);
200         vcpu_e500->svr = mfspr(SPRN_SVR);
201
202         vcpu->arch.cpu_type = KVM_CPU_E500MC;
203
204         return 0;
205 }
206
207 static int kvmppc_core_get_sregs_e500mc(struct kvm_vcpu *vcpu,
208                                         struct kvm_sregs *sregs)
209 {
210         struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
211
212         sregs->u.e.features |= KVM_SREGS_E_ARCH206_MMU | KVM_SREGS_E_PM |
213                                KVM_SREGS_E_PC;
214         sregs->u.e.impl_id = KVM_SREGS_E_IMPL_FSL;
215
216         sregs->u.e.impl.fsl.features = 0;
217         sregs->u.e.impl.fsl.svr = vcpu_e500->svr;
218         sregs->u.e.impl.fsl.hid0 = vcpu_e500->hid0;
219         sregs->u.e.impl.fsl.mcar = vcpu_e500->mcar;
220
221         kvmppc_get_sregs_e500_tlb(vcpu, sregs);
222
223         sregs->u.e.ivor_high[3] =
224                 vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR];
225         sregs->u.e.ivor_high[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL];
226         sregs->u.e.ivor_high[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL_CRIT];
227
228         return kvmppc_get_sregs_ivor(vcpu, sregs);
229 }
230
231 static int kvmppc_core_set_sregs_e500mc(struct kvm_vcpu *vcpu,
232                                         struct kvm_sregs *sregs)
233 {
234         struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
235         int ret;
236
237         if (sregs->u.e.impl_id == KVM_SREGS_E_IMPL_FSL) {
238                 vcpu_e500->svr = sregs->u.e.impl.fsl.svr;
239                 vcpu_e500->hid0 = sregs->u.e.impl.fsl.hid0;
240                 vcpu_e500->mcar = sregs->u.e.impl.fsl.mcar;
241         }
242
243         ret = kvmppc_set_sregs_e500_tlb(vcpu, sregs);
244         if (ret < 0)
245                 return ret;
246
247         if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
248                 return 0;
249
250         if (sregs->u.e.features & KVM_SREGS_E_PM) {
251                 vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR] =
252                         sregs->u.e.ivor_high[3];
253         }
254
255         if (sregs->u.e.features & KVM_SREGS_E_PC) {
256                 vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL] =
257                         sregs->u.e.ivor_high[4];
258                 vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL_CRIT] =
259                         sregs->u.e.ivor_high[5];
260         }
261
262         return kvmppc_set_sregs_ivor(vcpu, sregs);
263 }
264
265 static int kvmppc_get_one_reg_e500mc(struct kvm_vcpu *vcpu, u64 id,
266                               union kvmppc_one_reg *val)
267 {
268         int r = 0;
269
270         switch (id) {
271         case KVM_REG_PPC_SPRG9:
272                 *val = get_reg_val(id, vcpu->arch.sprg9);
273                 break;
274         default:
275                 r = kvmppc_get_one_reg_e500_tlb(vcpu, id, val);
276         }
277
278         return r;
279 }
280
281 static int kvmppc_set_one_reg_e500mc(struct kvm_vcpu *vcpu, u64 id,
282                               union kvmppc_one_reg *val)
283 {
284         int r = 0;
285
286         switch (id) {
287         case KVM_REG_PPC_SPRG9:
288                 vcpu->arch.sprg9 = set_reg_val(id, *val);
289                 break;
290         default:
291                 r = kvmppc_set_one_reg_e500_tlb(vcpu, id, val);
292         }
293
294         return r;
295 }
296
297 static struct kvm_vcpu *kvmppc_core_vcpu_create_e500mc(struct kvm *kvm,
298                                                        unsigned int id)
299 {
300         struct kvmppc_vcpu_e500 *vcpu_e500;
301         struct kvm_vcpu *vcpu;
302         int err;
303
304         vcpu_e500 = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
305         if (!vcpu_e500) {
306                 err = -ENOMEM;
307                 goto out;
308         }
309         vcpu = &vcpu_e500->vcpu;
310
311         /* Invalid PIR value -- this LPID dosn't have valid state on any cpu */
312         vcpu->arch.oldpir = 0xffffffff;
313
314         err = kvm_vcpu_init(vcpu, kvm, id);
315         if (err)
316                 goto free_vcpu;
317
318         err = kvmppc_e500_tlb_init(vcpu_e500);
319         if (err)
320                 goto uninit_vcpu;
321
322         vcpu->arch.shared = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
323         if (!vcpu->arch.shared)
324                 goto uninit_tlb;
325
326         return vcpu;
327
328 uninit_tlb:
329         kvmppc_e500_tlb_uninit(vcpu_e500);
330 uninit_vcpu:
331         kvm_vcpu_uninit(vcpu);
332
333 free_vcpu:
334         kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
335 out:
336         return ERR_PTR(err);
337 }
338
339 static void kvmppc_core_vcpu_free_e500mc(struct kvm_vcpu *vcpu)
340 {
341         struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
342
343         free_page((unsigned long)vcpu->arch.shared);
344         kvmppc_e500_tlb_uninit(vcpu_e500);
345         kvm_vcpu_uninit(vcpu);
346         kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
347 }
348
349 static int kvmppc_core_init_vm_e500mc(struct kvm *kvm)
350 {
351         int lpid;
352
353         lpid = kvmppc_alloc_lpid();
354         if (lpid < 0)
355                 return lpid;
356
357         kvm->arch.lpid = lpid;
358         return 0;
359 }
360
361 static void kvmppc_core_destroy_vm_e500mc(struct kvm *kvm)
362 {
363         kvmppc_free_lpid(kvm->arch.lpid);
364 }
365
366 static struct kvmppc_ops kvm_ops_e500mc = {
367         .get_sregs = kvmppc_core_get_sregs_e500mc,
368         .set_sregs = kvmppc_core_set_sregs_e500mc,
369         .get_one_reg = kvmppc_get_one_reg_e500mc,
370         .set_one_reg = kvmppc_set_one_reg_e500mc,
371         .vcpu_load   = kvmppc_core_vcpu_load_e500mc,
372         .vcpu_put    = kvmppc_core_vcpu_put_e500mc,
373         .vcpu_create = kvmppc_core_vcpu_create_e500mc,
374         .vcpu_free   = kvmppc_core_vcpu_free_e500mc,
375         .mmu_destroy  = kvmppc_mmu_destroy_e500,
376         .init_vm = kvmppc_core_init_vm_e500mc,
377         .destroy_vm = kvmppc_core_destroy_vm_e500mc,
378         .emulate_op = kvmppc_core_emulate_op_e500,
379         .emulate_mtspr = kvmppc_core_emulate_mtspr_e500,
380         .emulate_mfspr = kvmppc_core_emulate_mfspr_e500,
381 };
382
383 static int __init kvmppc_e500mc_init(void)
384 {
385         int r;
386
387         r = kvmppc_booke_init();
388         if (r)
389                 goto err_out;
390
391         kvmppc_init_lpid(64);
392         kvmppc_claim_lpid(0); /* host */
393
394         r = kvm_init(NULL, sizeof(struct kvmppc_vcpu_e500), 0, THIS_MODULE);
395         if (r)
396                 goto err_out;
397         kvm_ops_e500mc.owner = THIS_MODULE;
398         kvmppc_pr_ops = &kvm_ops_e500mc;
399
400 err_out:
401         return r;
402 }
403
404 static void __exit kvmppc_e500mc_exit(void)
405 {
406         kvmppc_pr_ops = NULL;
407         kvmppc_booke_exit();
408 }
409
410 module_init(kvmppc_e500mc_init);
411 module_exit(kvmppc_e500mc_exit);
412 MODULE_ALIAS_MISCDEV(KVM_MINOR);
413 MODULE_ALIAS("devname:kvm");