blob: 17e456279224652ce86c5c404130bdc45a702fe7 [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>
Alexander Graf398a76c2013-12-09 13:53:42 +010019#include <linux/miscdevice.h>
20#include <linux/module.h>
Scott Wood73196cd2011-12-20 15:34:47 +000021
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
31void 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 */
58void kvmppc_e500_tlbil_one(struct kvmppc_vcpu_e500 *vcpu_e500,
59 struct kvm_book3e_206_tlb_entry *gtlbe)
60{
61 unsigned int tid, ts;
Mihai Caraman66c98972012-06-25 02:26:26 +000062 gva_t eaddr;
63 u32 val, lpid;
Scott Wood73196cd2011-12-20 15:34:47 +000064 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
93void 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
104void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid)
105{
106 vcpu->arch.pid = pid;
107}
108
109void kvmppc_mmu_msr_notify(struct kvm_vcpu *vcpu, u32 old_msr)
110{
111}
112
Scott Woodc5e6cb02013-02-18 18:13:09 +0000113static DEFINE_PER_CPU(struct kvm_vcpu *, last_vcpu_on_cpu);
114
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530115static void kvmppc_core_vcpu_load_e500mc(struct kvm_vcpu *vcpu, int cpu)
Scott Wood73196cd2011-12-20 15:34:47 +0000116{
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
Scott Woodc5e6cb02013-02-18 18:13:09 +0000143 if (vcpu->arch.oldpir != mfspr(SPRN_PIR) ||
144 __get_cpu_var(last_vcpu_on_cpu) != vcpu) {
Scott Wood73196cd2011-12-20 15:34:47 +0000145 kvmppc_e500_tlbil_all(vcpu_e500);
Scott Woodc5e6cb02013-02-18 18:13:09 +0000146 __get_cpu_var(last_vcpu_on_cpu) = vcpu;
147 }
Scott Wood73196cd2011-12-20 15:34:47 +0000148
149 kvmppc_load_guest_fp(vcpu);
150}
151
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530152static void kvmppc_core_vcpu_put_e500mc(struct kvm_vcpu *vcpu)
Scott Wood73196cd2011-12-20 15:34:47 +0000153{
154 vcpu->arch.eplc = mfspr(SPRN_EPLC);
155 vcpu->arch.epsc = mfspr(SPRN_EPSC);
156
157 vcpu->arch.shared->sprg0 = mfspr(SPRN_GSPRG0);
158 vcpu->arch.shared->sprg1 = mfspr(SPRN_GSPRG1);
159 vcpu->arch.shared->sprg2 = mfspr(SPRN_GSPRG2);
160 vcpu->arch.shared->sprg3 = mfspr(SPRN_GSPRG3);
161
162 vcpu->arch.shared->srr0 = mfspr(SPRN_GSRR0);
163 vcpu->arch.shared->srr1 = mfspr(SPRN_GSRR1);
164
165 vcpu->arch.epr = mfspr(SPRN_GEPR);
166 vcpu->arch.shared->dar = mfspr(SPRN_GDEAR);
167 vcpu->arch.shared->esr = mfspr(SPRN_GESR);
168
169 vcpu->arch.oldpir = mfspr(SPRN_PIR);
170
171 kvmppc_booke_vcpu_put(vcpu);
172}
173
174int kvmppc_core_check_processor_compat(void)
175{
176 int r;
177
178 if (strcmp(cur_cpu_spec->cpu_name, "e500mc") == 0)
179 r = 0;
180 else if (strcmp(cur_cpu_spec->cpu_name, "e5500") == 0)
181 r = 0;
182 else
183 r = -ENOTSUPP;
184
185 return r;
186}
187
188int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu)
189{
190 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
191
192 vcpu->arch.shadow_epcr = SPRN_EPCR_DSIGS | SPRN_EPCR_DGTMI | \
193 SPRN_EPCR_DUVD;
Mihai Caramanc7ba7772012-06-25 02:26:19 +0000194#ifdef CONFIG_64BIT
195 vcpu->arch.shadow_epcr |= SPRN_EPCR_ICM;
196#endif
Scott Wood73196cd2011-12-20 15:34:47 +0000197 vcpu->arch.shadow_msrp = MSRP_UCLEP | MSRP_DEP | MSRP_PMMP;
198 vcpu->arch.eplc = EPC_EGS | (vcpu->kvm->arch.lpid << EPC_ELPID_SHIFT);
199 vcpu->arch.epsc = vcpu->arch.eplc;
200
201 vcpu->arch.pvr = mfspr(SPRN_PVR);
202 vcpu_e500->svr = mfspr(SPRN_SVR);
203
204 vcpu->arch.cpu_type = KVM_CPU_E500MC;
205
206 return 0;
207}
208
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530209static int kvmppc_core_get_sregs_e500mc(struct kvm_vcpu *vcpu,
210 struct kvm_sregs *sregs)
Scott Wood73196cd2011-12-20 15:34:47 +0000211{
212 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
213
214 sregs->u.e.features |= KVM_SREGS_E_ARCH206_MMU | KVM_SREGS_E_PM |
215 KVM_SREGS_E_PC;
216 sregs->u.e.impl_id = KVM_SREGS_E_IMPL_FSL;
217
218 sregs->u.e.impl.fsl.features = 0;
219 sregs->u.e.impl.fsl.svr = vcpu_e500->svr;
220 sregs->u.e.impl.fsl.hid0 = vcpu_e500->hid0;
221 sregs->u.e.impl.fsl.mcar = vcpu_e500->mcar;
222
223 kvmppc_get_sregs_e500_tlb(vcpu, sregs);
224
225 sregs->u.e.ivor_high[3] =
226 vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR];
227 sregs->u.e.ivor_high[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL];
228 sregs->u.e.ivor_high[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL_CRIT];
229
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530230 return kvmppc_get_sregs_ivor(vcpu, sregs);
Scott Wood73196cd2011-12-20 15:34:47 +0000231}
232
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530233static int kvmppc_core_set_sregs_e500mc(struct kvm_vcpu *vcpu,
234 struct kvm_sregs *sregs)
Scott Wood73196cd2011-12-20 15:34:47 +0000235{
236 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
237 int ret;
238
239 if (sregs->u.e.impl_id == KVM_SREGS_E_IMPL_FSL) {
240 vcpu_e500->svr = sregs->u.e.impl.fsl.svr;
241 vcpu_e500->hid0 = sregs->u.e.impl.fsl.hid0;
242 vcpu_e500->mcar = sregs->u.e.impl.fsl.mcar;
243 }
244
245 ret = kvmppc_set_sregs_e500_tlb(vcpu, sregs);
246 if (ret < 0)
247 return ret;
248
249 if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
250 return 0;
251
252 if (sregs->u.e.features & KVM_SREGS_E_PM) {
253 vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR] =
254 sregs->u.e.ivor_high[3];
255 }
256
257 if (sregs->u.e.features & KVM_SREGS_E_PC) {
258 vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL] =
259 sregs->u.e.ivor_high[4];
260 vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL_CRIT] =
261 sregs->u.e.ivor_high[5];
262 }
263
264 return kvmppc_set_sregs_ivor(vcpu, sregs);
265}
266
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530267static int kvmppc_get_one_reg_e500mc(struct kvm_vcpu *vcpu, u64 id,
268 union kvmppc_one_reg *val)
Mihai Caraman35b299e2013-04-11 00:03:07 +0000269{
Mihai Caramana85d2aa2013-04-11 00:03:08 +0000270 int r = kvmppc_get_one_reg_e500_tlb(vcpu, id, val);
271 return r;
Mihai Caraman35b299e2013-04-11 00:03:07 +0000272}
273
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530274static int kvmppc_set_one_reg_e500mc(struct kvm_vcpu *vcpu, u64 id,
275 union kvmppc_one_reg *val)
Mihai Caraman35b299e2013-04-11 00:03:07 +0000276{
Mihai Caramana85d2aa2013-04-11 00:03:08 +0000277 int r = kvmppc_set_one_reg_e500_tlb(vcpu, id, val);
278 return r;
Mihai Caraman35b299e2013-04-11 00:03:07 +0000279}
280
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530281static struct kvm_vcpu *kvmppc_core_vcpu_create_e500mc(struct kvm *kvm,
282 unsigned int id)
Scott Wood73196cd2011-12-20 15:34:47 +0000283{
284 struct kvmppc_vcpu_e500 *vcpu_e500;
285 struct kvm_vcpu *vcpu;
286 int err;
287
288 vcpu_e500 = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
289 if (!vcpu_e500) {
290 err = -ENOMEM;
291 goto out;
292 }
293 vcpu = &vcpu_e500->vcpu;
294
295 /* Invalid PIR value -- this LPID dosn't have valid state on any cpu */
296 vcpu->arch.oldpir = 0xffffffff;
297
298 err = kvm_vcpu_init(vcpu, kvm, id);
299 if (err)
300 goto free_vcpu;
301
302 err = kvmppc_e500_tlb_init(vcpu_e500);
303 if (err)
304 goto uninit_vcpu;
305
306 vcpu->arch.shared = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
307 if (!vcpu->arch.shared)
308 goto uninit_tlb;
309
310 return vcpu;
311
312uninit_tlb:
313 kvmppc_e500_tlb_uninit(vcpu_e500);
314uninit_vcpu:
315 kvm_vcpu_uninit(vcpu);
316
317free_vcpu:
318 kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
319out:
320 return ERR_PTR(err);
321}
322
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530323static void kvmppc_core_vcpu_free_e500mc(struct kvm_vcpu *vcpu)
Scott Wood73196cd2011-12-20 15:34:47 +0000324{
325 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
326
327 free_page((unsigned long)vcpu->arch.shared);
328 kvmppc_e500_tlb_uninit(vcpu_e500);
329 kvm_vcpu_uninit(vcpu);
330 kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
331}
332
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530333static int kvmppc_core_init_vm_e500mc(struct kvm *kvm)
Scott Wood73196cd2011-12-20 15:34:47 +0000334{
335 int lpid;
336
337 lpid = kvmppc_alloc_lpid();
338 if (lpid < 0)
339 return lpid;
340
341 kvm->arch.lpid = lpid;
342 return 0;
343}
344
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530345static void kvmppc_core_destroy_vm_e500mc(struct kvm *kvm)
Scott Wood73196cd2011-12-20 15:34:47 +0000346{
347 kvmppc_free_lpid(kvm->arch.lpid);
348}
349
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530350static struct kvmppc_ops kvm_ops_e500mc = {
351 .get_sregs = kvmppc_core_get_sregs_e500mc,
352 .set_sregs = kvmppc_core_set_sregs_e500mc,
353 .get_one_reg = kvmppc_get_one_reg_e500mc,
354 .set_one_reg = kvmppc_set_one_reg_e500mc,
355 .vcpu_load = kvmppc_core_vcpu_load_e500mc,
356 .vcpu_put = kvmppc_core_vcpu_put_e500mc,
357 .vcpu_create = kvmppc_core_vcpu_create_e500mc,
358 .vcpu_free = kvmppc_core_vcpu_free_e500mc,
359 .mmu_destroy = kvmppc_mmu_destroy_e500,
360 .init_vm = kvmppc_core_init_vm_e500mc,
361 .destroy_vm = kvmppc_core_destroy_vm_e500mc,
362 .emulate_op = kvmppc_core_emulate_op_e500,
363 .emulate_mtspr = kvmppc_core_emulate_mtspr_e500,
364 .emulate_mfspr = kvmppc_core_emulate_mfspr_e500,
365};
366
Scott Wood73196cd2011-12-20 15:34:47 +0000367static int __init kvmppc_e500mc_init(void)
368{
369 int r;
370
371 r = kvmppc_booke_init();
372 if (r)
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530373 goto err_out;
Scott Wood73196cd2011-12-20 15:34:47 +0000374
375 kvmppc_init_lpid(64);
376 kvmppc_claim_lpid(0); /* host */
377
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +0530378 r = kvm_init(NULL, sizeof(struct kvmppc_vcpu_e500), 0, THIS_MODULE);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530379 if (r)
380 goto err_out;
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +0530381 kvm_ops_e500mc.owner = THIS_MODULE;
382 kvmppc_pr_ops = &kvm_ops_e500mc;
383
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530384err_out:
385 return r;
Scott Wood73196cd2011-12-20 15:34:47 +0000386}
387
388static void __exit kvmppc_e500mc_exit(void)
389{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +0530390 kvmppc_pr_ops = NULL;
Scott Wood73196cd2011-12-20 15:34:47 +0000391 kvmppc_booke_exit();
392}
393
394module_init(kvmppc_e500mc_init);
395module_exit(kvmppc_e500mc_exit);
Alexander Graf398a76c2013-12-09 13:53:42 +0100396MODULE_ALIAS_MISCDEV(KVM_MINOR);
397MODULE_ALIAS("devname:kvm");