]> Pileus Git - ~andy/linux/blob - arch/powerpc/kvm/e500.c
KVM: PPC: e500: rename e500_tlb.h to e500.h
[~andy/linux] / arch / powerpc / kvm / e500.c
1 /*
2  * Copyright (C) 2008-2011 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>
16 #include <linux/slab.h>
17 #include <linux/err.h>
18 #include <linux/export.h>
19
20 #include <asm/reg.h>
21 #include <asm/cputable.h>
22 #include <asm/tlbflush.h>
23 #include <asm/kvm_e500.h>
24 #include <asm/kvm_ppc.h>
25
26 #include "booke.h"
27 #include "e500.h"
28
29 void kvmppc_core_load_host_debugstate(struct kvm_vcpu *vcpu)
30 {
31 }
32
33 void kvmppc_core_load_guest_debugstate(struct kvm_vcpu *vcpu)
34 {
35 }
36
37 void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
38 {
39         kvmppc_booke_vcpu_load(vcpu, cpu);
40         kvmppc_e500_tlb_load(vcpu, cpu);
41 }
42
43 void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
44 {
45         kvmppc_e500_tlb_put(vcpu);
46
47 #ifdef CONFIG_SPE
48         if (vcpu->arch.shadow_msr & MSR_SPE)
49                 kvmppc_vcpu_disable_spe(vcpu);
50 #endif
51
52         kvmppc_booke_vcpu_put(vcpu);
53 }
54
55 int kvmppc_core_check_processor_compat(void)
56 {
57         int r;
58
59         if (strcmp(cur_cpu_spec->cpu_name, "e500v2") == 0)
60                 r = 0;
61         else
62                 r = -ENOTSUPP;
63
64         return r;
65 }
66
67 int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu)
68 {
69         struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
70
71         kvmppc_e500_tlb_setup(vcpu_e500);
72
73         /* Registers init */
74         vcpu->arch.pvr = mfspr(SPRN_PVR);
75         vcpu_e500->svr = mfspr(SPRN_SVR);
76
77         vcpu->arch.cpu_type = KVM_CPU_E500V2;
78
79         return 0;
80 }
81
82 /* 'linear_address' is actually an encoding of AS|PID|EADDR . */
83 int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu,
84                                struct kvm_translation *tr)
85 {
86         int index;
87         gva_t eaddr;
88         u8 pid;
89         u8 as;
90
91         eaddr = tr->linear_address;
92         pid = (tr->linear_address >> 32) & 0xff;
93         as = (tr->linear_address >> 40) & 0x1;
94
95         index = kvmppc_e500_tlb_search(vcpu, eaddr, pid, as);
96         if (index < 0) {
97                 tr->valid = 0;
98                 return 0;
99         }
100
101         tr->physical_address = kvmppc_mmu_xlate(vcpu, index, eaddr);
102         /* XXX what does "writeable" and "usermode" even mean? */
103         tr->valid = 1;
104
105         return 0;
106 }
107
108 void kvmppc_core_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
109 {
110         struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
111
112         sregs->u.e.features |= KVM_SREGS_E_ARCH206_MMU | KVM_SREGS_E_SPE |
113                                KVM_SREGS_E_PM;
114         sregs->u.e.impl_id = KVM_SREGS_E_IMPL_FSL;
115
116         sregs->u.e.impl.fsl.features = 0;
117         sregs->u.e.impl.fsl.svr = vcpu_e500->svr;
118         sregs->u.e.impl.fsl.hid0 = vcpu_e500->hid0;
119         sregs->u.e.impl.fsl.mcar = vcpu_e500->mcar;
120
121         sregs->u.e.mas0 = vcpu->arch.shared->mas0;
122         sregs->u.e.mas1 = vcpu->arch.shared->mas1;
123         sregs->u.e.mas2 = vcpu->arch.shared->mas2;
124         sregs->u.e.mas7_3 = vcpu->arch.shared->mas7_3;
125         sregs->u.e.mas4 = vcpu->arch.shared->mas4;
126         sregs->u.e.mas6 = vcpu->arch.shared->mas6;
127
128         sregs->u.e.mmucfg = mfspr(SPRN_MMUCFG);
129         sregs->u.e.tlbcfg[0] = vcpu_e500->tlb0cfg;
130         sregs->u.e.tlbcfg[1] = vcpu_e500->tlb1cfg;
131         sregs->u.e.tlbcfg[2] = 0;
132         sregs->u.e.tlbcfg[3] = 0;
133
134         sregs->u.e.ivor_high[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_UNAVAIL];
135         sregs->u.e.ivor_high[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_DATA];
136         sregs->u.e.ivor_high[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_ROUND];
137         sregs->u.e.ivor_high[3] =
138                 vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR];
139
140         kvmppc_get_sregs_ivor(vcpu, sregs);
141 }
142
143 int kvmppc_core_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
144 {
145         struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
146
147         if (sregs->u.e.impl_id == KVM_SREGS_E_IMPL_FSL) {
148                 vcpu_e500->svr = sregs->u.e.impl.fsl.svr;
149                 vcpu_e500->hid0 = sregs->u.e.impl.fsl.hid0;
150                 vcpu_e500->mcar = sregs->u.e.impl.fsl.mcar;
151         }
152
153         if (sregs->u.e.features & KVM_SREGS_E_ARCH206_MMU) {
154                 vcpu->arch.shared->mas0 = sregs->u.e.mas0;
155                 vcpu->arch.shared->mas1 = sregs->u.e.mas1;
156                 vcpu->arch.shared->mas2 = sregs->u.e.mas2;
157                 vcpu->arch.shared->mas7_3 = sregs->u.e.mas7_3;
158                 vcpu->arch.shared->mas4 = sregs->u.e.mas4;
159                 vcpu->arch.shared->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
182 struct 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
203         vcpu->arch.shared = (void*)__get_free_page(GFP_KERNEL|__GFP_ZERO);
204         if (!vcpu->arch.shared)
205                 goto uninit_tlb;
206
207         return vcpu;
208
209 uninit_tlb:
210         kvmppc_e500_tlb_uninit(vcpu_e500);
211 uninit_vcpu:
212         kvm_vcpu_uninit(vcpu);
213 free_vcpu:
214         kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
215 out:
216         return ERR_PTR(err);
217 }
218
219 void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
220 {
221         struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
222
223         free_page((unsigned long)vcpu->arch.shared);
224         kvm_vcpu_uninit(vcpu);
225         kvmppc_e500_tlb_uninit(vcpu_e500);
226         kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
227 }
228
229 int kvmppc_core_init_vm(struct kvm *kvm)
230 {
231         return 0;
232 }
233
234 void kvmppc_core_destroy_vm(struct kvm *kvm)
235 {
236 }
237
238 static int __init kvmppc_e500_init(void)
239 {
240         int r, i;
241         unsigned long ivor[3];
242         unsigned long max_ivor = 0;
243
244         r = kvmppc_core_check_processor_compat();
245         if (r)
246                 return r;
247
248         r = kvmppc_booke_init();
249         if (r)
250                 return r;
251
252         /* copy extra E500 exception handlers */
253         ivor[0] = mfspr(SPRN_IVOR32);
254         ivor[1] = mfspr(SPRN_IVOR33);
255         ivor[2] = mfspr(SPRN_IVOR34);
256         for (i = 0; i < 3; i++) {
257                 if (ivor[i] > max_ivor)
258                         max_ivor = ivor[i];
259
260                 memcpy((void *)kvmppc_booke_handlers + ivor[i],
261                        kvmppc_handlers_start + (i + 16) * kvmppc_handler_len,
262                        kvmppc_handler_len);
263         }
264         flush_icache_range(kvmppc_booke_handlers,
265                         kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
266
267         return kvm_init(NULL, sizeof(struct kvmppc_vcpu_e500), 0, THIS_MODULE);
268 }
269
270 static void __exit kvmppc_e500_exit(void)
271 {
272         kvmppc_booke_exit();
273 }
274
275 module_init(kvmppc_e500_init);
276 module_exit(kvmppc_e500_exit);