blob: 318dbc61ba447e7352107de95daa59053e191d16 [file] [log] [blame]
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001/*
2 * Copyright (C) 2008 Freescale Semiconductor, Inc. All rights reserved.
3 *
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);
44}
45
46int kvmppc_core_check_processor_compat(void)
47{
48 int r;
49
50 if (strcmp(cur_cpu_spec->cpu_name, "e500v2") == 0)
51 r = 0;
52 else
53 r = -ENOTSUPP;
54
55 return r;
56}
57
58int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu)
59{
60 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
61
62 kvmppc_e500_tlb_setup(vcpu_e500);
63
Liu Yua9040f22010-01-22 18:50:30 +080064 /* Registers init */
65 vcpu->arch.pvr = mfspr(SPRN_PVR);
Scott Wood90d34b02011-03-29 16:49:10 -050066 vcpu_e500->svr = mfspr(SPRN_SVR);
Liu Yua9040f22010-01-22 18:50:30 +080067
68 /* Since booke kvm only support one core, update all vcpus' PIR to 0 */
69 vcpu->vcpu_id = 0;
70
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060071 return 0;
72}
73
74/* 'linear_address' is actually an encoding of AS|PID|EADDR . */
75int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu,
76 struct kvm_translation *tr)
77{
78 int index;
79 gva_t eaddr;
80 u8 pid;
81 u8 as;
82
83 eaddr = tr->linear_address;
84 pid = (tr->linear_address >> 32) & 0xff;
85 as = (tr->linear_address >> 40) & 0x1;
86
87 index = kvmppc_e500_tlb_search(vcpu, eaddr, pid, as);
88 if (index < 0) {
89 tr->valid = 0;
90 return 0;
91 }
92
93 tr->physical_address = kvmppc_mmu_xlate(vcpu, index, eaddr);
94 /* XXX what does "writeable" and "usermode" even mean? */
95 tr->valid = 1;
96
97 return 0;
98}
99
Scott Wood5ce941e2011-04-27 17:24:21 -0500100void kvmppc_core_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
101{
102 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
103
104 sregs->u.e.features |= KVM_SREGS_E_ARCH206_MMU | KVM_SREGS_E_SPE |
105 KVM_SREGS_E_PM;
106 sregs->u.e.impl_id = KVM_SREGS_E_IMPL_FSL;
107
108 sregs->u.e.impl.fsl.features = 0;
109 sregs->u.e.impl.fsl.svr = vcpu_e500->svr;
110 sregs->u.e.impl.fsl.hid0 = vcpu_e500->hid0;
111 sregs->u.e.impl.fsl.mcar = vcpu_e500->mcar;
112
113 sregs->u.e.mas0 = vcpu_e500->mas0;
114 sregs->u.e.mas1 = vcpu_e500->mas1;
115 sregs->u.e.mas2 = vcpu_e500->mas2;
116 sregs->u.e.mas7_3 = ((u64)vcpu_e500->mas7 << 32) | vcpu_e500->mas3;
117 sregs->u.e.mas4 = vcpu_e500->mas4;
118 sregs->u.e.mas6 = vcpu_e500->mas6;
119
120 sregs->u.e.mmucfg = mfspr(SPRN_MMUCFG);
121 sregs->u.e.tlbcfg[0] = vcpu_e500->tlb0cfg;
122 sregs->u.e.tlbcfg[1] = vcpu_e500->tlb1cfg;
123 sregs->u.e.tlbcfg[2] = 0;
124 sregs->u.e.tlbcfg[3] = 0;
125
126 sregs->u.e.ivor_high[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_UNAVAIL];
127 sregs->u.e.ivor_high[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_DATA];
128 sregs->u.e.ivor_high[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_ROUND];
129 sregs->u.e.ivor_high[3] =
130 vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR];
131
132 kvmppc_get_sregs_ivor(vcpu, sregs);
133}
134
135int kvmppc_core_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
136{
137 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
138
139 if (sregs->u.e.impl_id == KVM_SREGS_E_IMPL_FSL) {
140 vcpu_e500->svr = sregs->u.e.impl.fsl.svr;
141 vcpu_e500->hid0 = sregs->u.e.impl.fsl.hid0;
142 vcpu_e500->mcar = sregs->u.e.impl.fsl.mcar;
143 }
144
145 if (sregs->u.e.features & KVM_SREGS_E_ARCH206_MMU) {
146 vcpu_e500->mas0 = sregs->u.e.mas0;
147 vcpu_e500->mas1 = sregs->u.e.mas1;
148 vcpu_e500->mas2 = sregs->u.e.mas2;
149 vcpu_e500->mas7 = sregs->u.e.mas7_3 >> 32;
150 vcpu_e500->mas3 = (u32)sregs->u.e.mas7_3;
151 vcpu_e500->mas4 = sregs->u.e.mas4;
152 vcpu_e500->mas6 = sregs->u.e.mas6;
153 }
154
155 if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
156 return 0;
157
158 if (sregs->u.e.features & KVM_SREGS_E_SPE) {
159 vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_UNAVAIL] =
160 sregs->u.e.ivor_high[0];
161 vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_DATA] =
162 sregs->u.e.ivor_high[1];
163 vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_ROUND] =
164 sregs->u.e.ivor_high[2];
165 }
166
167 if (sregs->u.e.features & KVM_SREGS_E_PM) {
168 vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR] =
169 sregs->u.e.ivor_high[3];
170 }
171
172 return kvmppc_set_sregs_ivor(vcpu, sregs);
173}
174
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600175struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
176{
177 struct kvmppc_vcpu_e500 *vcpu_e500;
178 struct kvm_vcpu *vcpu;
179 int err;
180
181 vcpu_e500 = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
182 if (!vcpu_e500) {
183 err = -ENOMEM;
184 goto out;
185 }
186
187 vcpu = &vcpu_e500->vcpu;
188 err = kvm_vcpu_init(vcpu, kvm, id);
189 if (err)
190 goto free_vcpu;
191
192 err = kvmppc_e500_tlb_init(vcpu_e500);
193 if (err)
194 goto uninit_vcpu;
195
Alexander Graf96bc4512010-07-29 14:47:42 +0200196 vcpu->arch.shared = (void*)__get_free_page(GFP_KERNEL|__GFP_ZERO);
197 if (!vcpu->arch.shared)
198 goto uninit_tlb;
199
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600200 return vcpu;
201
Alexander Graf96bc4512010-07-29 14:47:42 +0200202uninit_tlb:
203 kvmppc_e500_tlb_uninit(vcpu_e500);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600204uninit_vcpu:
205 kvm_vcpu_uninit(vcpu);
206free_vcpu:
207 kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
208out:
209 return ERR_PTR(err);
210}
211
212void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
213{
214 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
215
Alexander Graf96bc4512010-07-29 14:47:42 +0200216 free_page((unsigned long)vcpu->arch.shared);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600217 kvm_vcpu_uninit(vcpu);
Scott Woodf22e2f02010-10-05 14:22:41 -0500218 kvmppc_e500_tlb_uninit(vcpu_e500);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600219 kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
220}
221
Stephen Rothwell2986b8c2009-06-02 11:46:14 +1000222static int __init kvmppc_e500_init(void)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600223{
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600224 int r, i;
225 unsigned long ivor[3];
226 unsigned long max_ivor = 0;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600227
228 r = kvmppc_booke_init();
229 if (r)
230 return r;
231
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600232 /* copy extra E500 exception handlers */
233 ivor[0] = mfspr(SPRN_IVOR32);
234 ivor[1] = mfspr(SPRN_IVOR33);
235 ivor[2] = mfspr(SPRN_IVOR34);
236 for (i = 0; i < 3; i++) {
237 if (ivor[i] > max_ivor)
238 max_ivor = ivor[i];
239
240 memcpy((void *)kvmppc_booke_handlers + ivor[i],
241 kvmppc_handlers_start + (i + 16) * kvmppc_handler_len,
242 kvmppc_handler_len);
243 }
244 flush_icache_range(kvmppc_booke_handlers,
245 kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
246
Avi Kivity0ee75be2010-04-28 15:39:01 +0300247 return kvm_init(NULL, sizeof(struct kvmppc_vcpu_e500), 0, THIS_MODULE);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600248}
249
Jean Delvarea06cdb52010-05-18 09:34:12 +0200250static void __exit kvmppc_e500_exit(void)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600251{
252 kvmppc_booke_exit();
253}
254
255module_init(kvmppc_e500_init);
256module_exit(kvmppc_e500_exit);