blob: 26d20903f2bc5c8cce1a94637f58ea9dea73a696 [file] [log] [blame]
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001/*
Scott Wood4cd35f62011-06-14 18:34:31 -05002 * Copyright (C) 2008-2011 Freescale Semiconductor, Inc. All rights reserved.
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06003 *
4 * Author: Yu Liu, <yu.liu@freescale.com>
5 *
6 * Description:
7 * This file is derived from arch/powerpc/kvm/44x.c,
8 * by Hollis Blanchard <hollisb@us.ibm.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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060017#include <linux/err.h>
18
19#include <asm/reg.h>
20#include <asm/cputable.h>
21#include <asm/tlbflush.h>
22#include <asm/kvm_e500.h>
23#include <asm/kvm_ppc.h>
24
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -060025#include "booke.h"
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060026#include "e500_tlb.h"
27
28void kvmppc_core_load_host_debugstate(struct kvm_vcpu *vcpu)
29{
30}
31
32void kvmppc_core_load_guest_debugstate(struct kvm_vcpu *vcpu)
33{
34}
35
36void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
37{
38 kvmppc_e500_tlb_load(vcpu, cpu);
39}
40
41void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
42{
43 kvmppc_e500_tlb_put(vcpu);
Scott Wood4cd35f62011-06-14 18:34:31 -050044
45#ifdef CONFIG_SPE
46 if (vcpu->arch.shadow_msr & MSR_SPE)
47 kvmppc_vcpu_disable_spe(vcpu);
48#endif
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060049}
50
51int kvmppc_core_check_processor_compat(void)
52{
53 int r;
54
55 if (strcmp(cur_cpu_spec->cpu_name, "e500v2") == 0)
56 r = 0;
57 else
58 r = -ENOTSUPP;
59
60 return r;
61}
62
63int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu)
64{
65 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
66
67 kvmppc_e500_tlb_setup(vcpu_e500);
68
Liu Yua9040f22010-01-22 18:50:30 +080069 /* Registers init */
70 vcpu->arch.pvr = mfspr(SPRN_PVR);
Scott Wood90d34b02011-03-29 16:49:10 -050071 vcpu_e500->svr = mfspr(SPRN_SVR);
Liu Yua9040f22010-01-22 18:50:30 +080072
73 /* Since booke kvm only support one core, update all vcpus' PIR to 0 */
74 vcpu->vcpu_id = 0;
75
Alexander Grafaf8f38b2011-08-10 13:57:08 +020076 vcpu->arch.cpu_type = KVM_CPU_E500V2;
77
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060078 return 0;
79}
80
81/* 'linear_address' is actually an encoding of AS|PID|EADDR . */
82int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu,
83 struct kvm_translation *tr)
84{
85 int index;
86 gva_t eaddr;
87 u8 pid;
88 u8 as;
89
90 eaddr = tr->linear_address;
91 pid = (tr->linear_address >> 32) & 0xff;
92 as = (tr->linear_address >> 40) & 0x1;
93
94 index = kvmppc_e500_tlb_search(vcpu, eaddr, pid, as);
95 if (index < 0) {
96 tr->valid = 0;
97 return 0;
98 }
99
100 tr->physical_address = kvmppc_mmu_xlate(vcpu, index, eaddr);
101 /* XXX what does "writeable" and "usermode" even mean? */
102 tr->valid = 1;
103
104 return 0;
105}
106
Scott Wood5ce941e2011-04-27 17:24:21 -0500107void kvmppc_core_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
108{
109 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
110
111 sregs->u.e.features |= KVM_SREGS_E_ARCH206_MMU | KVM_SREGS_E_SPE |
112 KVM_SREGS_E_PM;
113 sregs->u.e.impl_id = KVM_SREGS_E_IMPL_FSL;
114
115 sregs->u.e.impl.fsl.features = 0;
116 sregs->u.e.impl.fsl.svr = vcpu_e500->svr;
117 sregs->u.e.impl.fsl.hid0 = vcpu_e500->hid0;
118 sregs->u.e.impl.fsl.mcar = vcpu_e500->mcar;
119
120 sregs->u.e.mas0 = vcpu_e500->mas0;
121 sregs->u.e.mas1 = vcpu_e500->mas1;
122 sregs->u.e.mas2 = vcpu_e500->mas2;
123 sregs->u.e.mas7_3 = ((u64)vcpu_e500->mas7 << 32) | vcpu_e500->mas3;
124 sregs->u.e.mas4 = vcpu_e500->mas4;
125 sregs->u.e.mas6 = vcpu_e500->mas6;
126
127 sregs->u.e.mmucfg = mfspr(SPRN_MMUCFG);
128 sregs->u.e.tlbcfg[0] = vcpu_e500->tlb0cfg;
129 sregs->u.e.tlbcfg[1] = vcpu_e500->tlb1cfg;
130 sregs->u.e.tlbcfg[2] = 0;
131 sregs->u.e.tlbcfg[3] = 0;
132
133 sregs->u.e.ivor_high[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_UNAVAIL];
134 sregs->u.e.ivor_high[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_DATA];
135 sregs->u.e.ivor_high[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_ROUND];
136 sregs->u.e.ivor_high[3] =
137 vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR];
138
139 kvmppc_get_sregs_ivor(vcpu, sregs);
140}
141
142int kvmppc_core_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
143{
144 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
145
146 if (sregs->u.e.impl_id == KVM_SREGS_E_IMPL_FSL) {
147 vcpu_e500->svr = sregs->u.e.impl.fsl.svr;
148 vcpu_e500->hid0 = sregs->u.e.impl.fsl.hid0;
149 vcpu_e500->mcar = sregs->u.e.impl.fsl.mcar;
150 }
151
152 if (sregs->u.e.features & KVM_SREGS_E_ARCH206_MMU) {
153 vcpu_e500->mas0 = sregs->u.e.mas0;
154 vcpu_e500->mas1 = sregs->u.e.mas1;
155 vcpu_e500->mas2 = sregs->u.e.mas2;
156 vcpu_e500->mas7 = sregs->u.e.mas7_3 >> 32;
157 vcpu_e500->mas3 = (u32)sregs->u.e.mas7_3;
158 vcpu_e500->mas4 = sregs->u.e.mas4;
159 vcpu_e500->mas6 = sregs->u.e.mas6;
160 }
161
162 if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
163 return 0;
164
165 if (sregs->u.e.features & KVM_SREGS_E_SPE) {
166 vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_UNAVAIL] =
167 sregs->u.e.ivor_high[0];
168 vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_DATA] =
169 sregs->u.e.ivor_high[1];
170 vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_ROUND] =
171 sregs->u.e.ivor_high[2];
172 }
173
174 if (sregs->u.e.features & KVM_SREGS_E_PM) {
175 vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR] =
176 sregs->u.e.ivor_high[3];
177 }
178
179 return kvmppc_set_sregs_ivor(vcpu, sregs);
180}
181
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600182struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
183{
184 struct kvmppc_vcpu_e500 *vcpu_e500;
185 struct kvm_vcpu *vcpu;
186 int err;
187
188 vcpu_e500 = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
189 if (!vcpu_e500) {
190 err = -ENOMEM;
191 goto out;
192 }
193
194 vcpu = &vcpu_e500->vcpu;
195 err = kvm_vcpu_init(vcpu, kvm, id);
196 if (err)
197 goto free_vcpu;
198
199 err = kvmppc_e500_tlb_init(vcpu_e500);
200 if (err)
201 goto uninit_vcpu;
202
Alexander Graf96bc4512010-07-29 14:47:42 +0200203 vcpu->arch.shared = (void*)__get_free_page(GFP_KERNEL|__GFP_ZERO);
204 if (!vcpu->arch.shared)
205 goto uninit_tlb;
206
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600207 return vcpu;
208
Alexander Graf96bc4512010-07-29 14:47:42 +0200209uninit_tlb:
210 kvmppc_e500_tlb_uninit(vcpu_e500);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600211uninit_vcpu:
212 kvm_vcpu_uninit(vcpu);
213free_vcpu:
214 kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
215out:
216 return ERR_PTR(err);
217}
218
219void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
220{
221 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
222
Alexander Graf96bc4512010-07-29 14:47:42 +0200223 free_page((unsigned long)vcpu->arch.shared);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600224 kvm_vcpu_uninit(vcpu);
Scott Woodf22e2f02010-10-05 14:22:41 -0500225 kvmppc_e500_tlb_uninit(vcpu_e500);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600226 kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
227}
228
Stephen Rothwell2986b8c2009-06-02 11:46:14 +1000229static int __init kvmppc_e500_init(void)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600230{
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600231 int r, i;
232 unsigned long ivor[3];
233 unsigned long max_ivor = 0;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600234
235 r = kvmppc_booke_init();
236 if (r)
237 return r;
238
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600239 /* copy extra E500 exception handlers */
240 ivor[0] = mfspr(SPRN_IVOR32);
241 ivor[1] = mfspr(SPRN_IVOR33);
242 ivor[2] = mfspr(SPRN_IVOR34);
243 for (i = 0; i < 3; i++) {
244 if (ivor[i] > max_ivor)
245 max_ivor = ivor[i];
246
247 memcpy((void *)kvmppc_booke_handlers + ivor[i],
248 kvmppc_handlers_start + (i + 16) * kvmppc_handler_len,
249 kvmppc_handler_len);
250 }
251 flush_icache_range(kvmppc_booke_handlers,
252 kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
253
Avi Kivity0ee75be2010-04-28 15:39:01 +0300254 return kvm_init(NULL, sizeof(struct kvmppc_vcpu_e500), 0, THIS_MODULE);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600255}
256
Jean Delvarea06cdb52010-05-18 09:34:12 +0200257static void __exit kvmppc_e500_exit(void)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600258{
259 kvmppc_booke_exit();
260}
261
262module_init(kvmppc_e500_init);
263module_exit(kvmppc_e500_exit);