Merge branch 'for-linus' of git://git390.marist.edu/pub/scm/linux-2.6
[pandora-kernel.git] / 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
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
25 #include "booke.h"
26 #include "e500_tlb.h"
27
28 void kvmppc_core_load_host_debugstate(struct kvm_vcpu *vcpu)
29 {
30 }
31
32 void kvmppc_core_load_guest_debugstate(struct kvm_vcpu *vcpu)
33 {
34 }
35
36 void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
37 {
38         kvmppc_e500_tlb_load(vcpu, cpu);
39 }
40
41 void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
42 {
43         kvmppc_e500_tlb_put(vcpu);
44
45 #ifdef CONFIG_SPE
46         if (vcpu->arch.shadow_msr & MSR_SPE)
47                 kvmppc_vcpu_disable_spe(vcpu);
48 #endif
49 }
50
51 int 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
63 int 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
69         /* Registers init */
70         vcpu->arch.pvr = mfspr(SPRN_PVR);
71         vcpu_e500->svr = mfspr(SPRN_SVR);
72
73         /* Since booke kvm only support one core, update all vcpus' PIR to 0 */
74         vcpu->vcpu_id = 0;
75
76         return 0;
77 }
78
79 /* 'linear_address' is actually an encoding of AS|PID|EADDR . */
80 int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu,
81                                struct kvm_translation *tr)
82 {
83         int index;
84         gva_t eaddr;
85         u8 pid;
86         u8 as;
87
88         eaddr = tr->linear_address;
89         pid = (tr->linear_address >> 32) & 0xff;
90         as = (tr->linear_address >> 40) & 0x1;
91
92         index = kvmppc_e500_tlb_search(vcpu, eaddr, pid, as);
93         if (index < 0) {
94                 tr->valid = 0;
95                 return 0;
96         }
97
98         tr->physical_address = kvmppc_mmu_xlate(vcpu, index, eaddr);
99         /* XXX what does "writeable" and "usermode" even mean? */
100         tr->valid = 1;
101
102         return 0;
103 }
104
105 void kvmppc_core_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
106 {
107         struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
108
109         sregs->u.e.features |= KVM_SREGS_E_ARCH206_MMU | KVM_SREGS_E_SPE |
110                                KVM_SREGS_E_PM;
111         sregs->u.e.impl_id = KVM_SREGS_E_IMPL_FSL;
112
113         sregs->u.e.impl.fsl.features = 0;
114         sregs->u.e.impl.fsl.svr = vcpu_e500->svr;
115         sregs->u.e.impl.fsl.hid0 = vcpu_e500->hid0;
116         sregs->u.e.impl.fsl.mcar = vcpu_e500->mcar;
117
118         sregs->u.e.mas0 = vcpu_e500->mas0;
119         sregs->u.e.mas1 = vcpu_e500->mas1;
120         sregs->u.e.mas2 = vcpu_e500->mas2;
121         sregs->u.e.mas7_3 = ((u64)vcpu_e500->mas7 << 32) | vcpu_e500->mas3;
122         sregs->u.e.mas4 = vcpu_e500->mas4;
123         sregs->u.e.mas6 = vcpu_e500->mas6;
124
125         sregs->u.e.mmucfg = mfspr(SPRN_MMUCFG);
126         sregs->u.e.tlbcfg[0] = vcpu_e500->tlb0cfg;
127         sregs->u.e.tlbcfg[1] = vcpu_e500->tlb1cfg;
128         sregs->u.e.tlbcfg[2] = 0;
129         sregs->u.e.tlbcfg[3] = 0;
130
131         sregs->u.e.ivor_high[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_UNAVAIL];
132         sregs->u.e.ivor_high[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_DATA];
133         sregs->u.e.ivor_high[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_ROUND];
134         sregs->u.e.ivor_high[3] =
135                 vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR];
136
137         kvmppc_get_sregs_ivor(vcpu, sregs);
138 }
139
140 int kvmppc_core_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
141 {
142         struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
143
144         if (sregs->u.e.impl_id == KVM_SREGS_E_IMPL_FSL) {
145                 vcpu_e500->svr = sregs->u.e.impl.fsl.svr;
146                 vcpu_e500->hid0 = sregs->u.e.impl.fsl.hid0;
147                 vcpu_e500->mcar = sregs->u.e.impl.fsl.mcar;
148         }
149
150         if (sregs->u.e.features & KVM_SREGS_E_ARCH206_MMU) {
151                 vcpu_e500->mas0 = sregs->u.e.mas0;
152                 vcpu_e500->mas1 = sregs->u.e.mas1;
153                 vcpu_e500->mas2 = sregs->u.e.mas2;
154                 vcpu_e500->mas7 = sregs->u.e.mas7_3 >> 32;
155                 vcpu_e500->mas3 = (u32)sregs->u.e.mas7_3;
156                 vcpu_e500->mas4 = sregs->u.e.mas4;
157                 vcpu_e500->mas6 = sregs->u.e.mas6;
158         }
159
160         if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
161                 return 0;
162
163         if (sregs->u.e.features & KVM_SREGS_E_SPE) {
164                 vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_UNAVAIL] =
165                         sregs->u.e.ivor_high[0];
166                 vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_DATA] =
167                         sregs->u.e.ivor_high[1];
168                 vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_ROUND] =
169                         sregs->u.e.ivor_high[2];
170         }
171
172         if (sregs->u.e.features & KVM_SREGS_E_PM) {
173                 vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR] =
174                         sregs->u.e.ivor_high[3];
175         }
176
177         return kvmppc_set_sregs_ivor(vcpu, sregs);
178 }
179
180 struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
181 {
182         struct kvmppc_vcpu_e500 *vcpu_e500;
183         struct kvm_vcpu *vcpu;
184         int err;
185
186         vcpu_e500 = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
187         if (!vcpu_e500) {
188                 err = -ENOMEM;
189                 goto out;
190         }
191
192         vcpu = &vcpu_e500->vcpu;
193         err = kvm_vcpu_init(vcpu, kvm, id);
194         if (err)
195                 goto free_vcpu;
196
197         err = kvmppc_e500_tlb_init(vcpu_e500);
198         if (err)
199                 goto uninit_vcpu;
200
201         vcpu->arch.shared = (void*)__get_free_page(GFP_KERNEL|__GFP_ZERO);
202         if (!vcpu->arch.shared)
203                 goto uninit_tlb;
204
205         return vcpu;
206
207 uninit_tlb:
208         kvmppc_e500_tlb_uninit(vcpu_e500);
209 uninit_vcpu:
210         kvm_vcpu_uninit(vcpu);
211 free_vcpu:
212         kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
213 out:
214         return ERR_PTR(err);
215 }
216
217 void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
218 {
219         struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
220
221         free_page((unsigned long)vcpu->arch.shared);
222         kvm_vcpu_uninit(vcpu);
223         kvmppc_e500_tlb_uninit(vcpu_e500);
224         kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
225 }
226
227 static int __init kvmppc_e500_init(void)
228 {
229         int r, i;
230         unsigned long ivor[3];
231         unsigned long max_ivor = 0;
232
233         r = kvmppc_booke_init();
234         if (r)
235                 return r;
236
237         /* copy extra E500 exception handlers */
238         ivor[0] = mfspr(SPRN_IVOR32);
239         ivor[1] = mfspr(SPRN_IVOR33);
240         ivor[2] = mfspr(SPRN_IVOR34);
241         for (i = 0; i < 3; i++) {
242                 if (ivor[i] > max_ivor)
243                         max_ivor = ivor[i];
244
245                 memcpy((void *)kvmppc_booke_handlers + ivor[i],
246                        kvmppc_handlers_start + (i + 16) * kvmppc_handler_len,
247                        kvmppc_handler_len);
248         }
249         flush_icache_range(kvmppc_booke_handlers,
250                         kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
251
252         return kvm_init(NULL, sizeof(struct kvmppc_vcpu_e500), 0, THIS_MODULE);
253 }
254
255 static void __exit kvmppc_e500_exit(void)
256 {
257         kvmppc_booke_exit();
258 }
259
260 module_init(kvmppc_e500_init);
261 module_exit(kvmppc_e500_exit);