KVM: PPC: e500: merge <asm/kvm_e500.h> into arch/powerpc/kvm/e500.h
[platform/adaptation/renesas_rcar/renesas_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 #include <linux/export.h>
19
20 #include <asm/reg.h>
21 #include <asm/cputable.h>
22 #include <asm/tlbflush.h>
23 #include <asm/kvm_ppc.h>
24
25 #include "booke.h"
26 #include "e500.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_booke_vcpu_load(vcpu, cpu);
39         kvmppc_e500_tlb_load(vcpu, cpu);
40 }
41
42 void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
43 {
44         kvmppc_e500_tlb_put(vcpu);
45
46 #ifdef CONFIG_SPE
47         if (vcpu->arch.shadow_msr & MSR_SPE)
48                 kvmppc_vcpu_disable_spe(vcpu);
49 #endif
50
51         kvmppc_booke_vcpu_put(vcpu);
52 }
53
54 int kvmppc_core_check_processor_compat(void)
55 {
56         int r;
57
58         if (strcmp(cur_cpu_spec->cpu_name, "e500v2") == 0)
59                 r = 0;
60         else
61                 r = -ENOTSUPP;
62
63         return r;
64 }
65
66 int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu)
67 {
68         struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
69
70         kvmppc_e500_tlb_setup(vcpu_e500);
71
72         /* Registers init */
73         vcpu->arch.pvr = mfspr(SPRN_PVR);
74         vcpu_e500->svr = mfspr(SPRN_SVR);
75
76         vcpu->arch.cpu_type = KVM_CPU_E500V2;
77
78         return 0;
79 }
80
81 /* 'linear_address' is actually an encoding of AS|PID|EADDR . */
82 int 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
107 void 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->arch.shared->mas0;
121         sregs->u.e.mas1 = vcpu->arch.shared->mas1;
122         sregs->u.e.mas2 = vcpu->arch.shared->mas2;
123         sregs->u.e.mas7_3 = vcpu->arch.shared->mas7_3;
124         sregs->u.e.mas4 = vcpu->arch.shared->mas4;
125         sregs->u.e.mas6 = vcpu->arch.shared->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
142 int 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->arch.shared->mas0 = sregs->u.e.mas0;
154                 vcpu->arch.shared->mas1 = sregs->u.e.mas1;
155                 vcpu->arch.shared->mas2 = sregs->u.e.mas2;
156                 vcpu->arch.shared->mas7_3 = sregs->u.e.mas7_3;
157                 vcpu->arch.shared->mas4 = sregs->u.e.mas4;
158                 vcpu->arch.shared->mas6 = sregs->u.e.mas6;
159         }
160
161         if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
162                 return 0;
163
164         if (sregs->u.e.features & KVM_SREGS_E_SPE) {
165                 vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_UNAVAIL] =
166                         sregs->u.e.ivor_high[0];
167                 vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_DATA] =
168                         sregs->u.e.ivor_high[1];
169                 vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_ROUND] =
170                         sregs->u.e.ivor_high[2];
171         }
172
173         if (sregs->u.e.features & KVM_SREGS_E_PM) {
174                 vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR] =
175                         sregs->u.e.ivor_high[3];
176         }
177
178         return kvmppc_set_sregs_ivor(vcpu, sregs);
179 }
180
181 struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
182 {
183         struct kvmppc_vcpu_e500 *vcpu_e500;
184         struct kvm_vcpu *vcpu;
185         int err;
186
187         vcpu_e500 = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
188         if (!vcpu_e500) {
189                 err = -ENOMEM;
190                 goto out;
191         }
192
193         vcpu = &vcpu_e500->vcpu;
194         err = kvm_vcpu_init(vcpu, kvm, id);
195         if (err)
196                 goto free_vcpu;
197
198         err = kvmppc_e500_tlb_init(vcpu_e500);
199         if (err)
200                 goto uninit_vcpu;
201
202         vcpu->arch.shared = (void*)__get_free_page(GFP_KERNEL|__GFP_ZERO);
203         if (!vcpu->arch.shared)
204                 goto uninit_tlb;
205
206         return vcpu;
207
208 uninit_tlb:
209         kvmppc_e500_tlb_uninit(vcpu_e500);
210 uninit_vcpu:
211         kvm_vcpu_uninit(vcpu);
212 free_vcpu:
213         kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
214 out:
215         return ERR_PTR(err);
216 }
217
218 void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
219 {
220         struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
221
222         free_page((unsigned long)vcpu->arch.shared);
223         kvm_vcpu_uninit(vcpu);
224         kvmppc_e500_tlb_uninit(vcpu_e500);
225         kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
226 }
227
228 int kvmppc_core_init_vm(struct kvm *kvm)
229 {
230         return 0;
231 }
232
233 void kvmppc_core_destroy_vm(struct kvm *kvm)
234 {
235 }
236
237 static int __init kvmppc_e500_init(void)
238 {
239         int r, i;
240         unsigned long ivor[3];
241         unsigned long max_ivor = 0;
242
243         r = kvmppc_core_check_processor_compat();
244         if (r)
245                 return r;
246
247         r = kvmppc_booke_init();
248         if (r)
249                 return r;
250
251         /* copy extra E500 exception handlers */
252         ivor[0] = mfspr(SPRN_IVOR32);
253         ivor[1] = mfspr(SPRN_IVOR33);
254         ivor[2] = mfspr(SPRN_IVOR34);
255         for (i = 0; i < 3; i++) {
256                 if (ivor[i] > max_ivor)
257                         max_ivor = ivor[i];
258
259                 memcpy((void *)kvmppc_booke_handlers + ivor[i],
260                        kvmppc_handlers_start + (i + 16) * kvmppc_handler_len,
261                        kvmppc_handler_len);
262         }
263         flush_icache_range(kvmppc_booke_handlers,
264                         kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
265
266         return kvm_init(NULL, sizeof(struct kvmppc_vcpu_e500), 0, THIS_MODULE);
267 }
268
269 static void __exit kvmppc_e500_exit(void)
270 {
271         kvmppc_booke_exit();
272 }
273
274 module_init(kvmppc_e500_init);
275 module_exit(kvmppc_e500_exit);