OMAPDSS: VENC: fix NULL pointer dereference in DSS2 VENC sysfs debug attr on OMAP4
[zen-stable.git] / arch / powerpc / kvm / e500.c
blob8c0d45a6faf7f49db9aef9c9a495c1ef20d155d8
1 /*
2 * Copyright (C) 2008-2011 Freescale Semiconductor, Inc. All rights reserved.
4 * Author: Yu Liu, <yu.liu@freescale.com>
6 * Description:
7 * This file is derived from arch/powerpc/kvm/44x.c,
8 * by Hollis Blanchard <hollisb@us.ibm.com>.
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.
15 #include <linux/kvm_host.h>
16 #include <linux/slab.h>
17 #include <linux/err.h>
18 #include <linux/export.h>
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>
26 #include "booke.h"
27 #include "e500_tlb.h"
29 void kvmppc_core_load_host_debugstate(struct kvm_vcpu *vcpu)
33 void kvmppc_core_load_guest_debugstate(struct kvm_vcpu *vcpu)
37 void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
39 kvmppc_e500_tlb_load(vcpu, cpu);
42 void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
44 kvmppc_e500_tlb_put(vcpu);
46 #ifdef CONFIG_SPE
47 if (vcpu->arch.shadow_msr & MSR_SPE)
48 kvmppc_vcpu_disable_spe(vcpu);
49 #endif
52 int kvmppc_core_check_processor_compat(void)
54 int r;
56 if (strcmp(cur_cpu_spec->cpu_name, "e500v2") == 0)
57 r = 0;
58 else
59 r = -ENOTSUPP;
61 return r;
64 int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu)
66 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
68 kvmppc_e500_tlb_setup(vcpu_e500);
70 /* Registers init */
71 vcpu->arch.pvr = mfspr(SPRN_PVR);
72 vcpu_e500->svr = mfspr(SPRN_SVR);
74 /* Since booke kvm only support one core, update all vcpus' PIR to 0 */
75 vcpu->vcpu_id = 0;
77 vcpu->arch.cpu_type = KVM_CPU_E500V2;
79 return 0;
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)
86 int index;
87 gva_t eaddr;
88 u8 pid;
89 u8 as;
91 eaddr = tr->linear_address;
92 pid = (tr->linear_address >> 32) & 0xff;
93 as = (tr->linear_address >> 40) & 0x1;
95 index = kvmppc_e500_tlb_search(vcpu, eaddr, pid, as);
96 if (index < 0) {
97 tr->valid = 0;
98 return 0;
101 tr->physical_address = kvmppc_mmu_xlate(vcpu, index, eaddr);
102 /* XXX what does "writeable" and "usermode" even mean? */
103 tr->valid = 1;
105 return 0;
108 void kvmppc_core_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
110 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
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;
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;
121 sregs->u.e.mas0 = vcpu_e500->mas0;
122 sregs->u.e.mas1 = vcpu_e500->mas1;
123 sregs->u.e.mas2 = vcpu_e500->mas2;
124 sregs->u.e.mas7_3 = ((u64)vcpu_e500->mas7 << 32) | vcpu_e500->mas3;
125 sregs->u.e.mas4 = vcpu_e500->mas4;
126 sregs->u.e.mas6 = vcpu_e500->mas6;
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;
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];
140 kvmppc_get_sregs_ivor(vcpu, sregs);
143 int kvmppc_core_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
145 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
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;
153 if (sregs->u.e.features & KVM_SREGS_E_ARCH206_MMU) {
154 vcpu_e500->mas0 = sregs->u.e.mas0;
155 vcpu_e500->mas1 = sregs->u.e.mas1;
156 vcpu_e500->mas2 = sregs->u.e.mas2;
157 vcpu_e500->mas7 = sregs->u.e.mas7_3 >> 32;
158 vcpu_e500->mas3 = (u32)sregs->u.e.mas7_3;
159 vcpu_e500->mas4 = sregs->u.e.mas4;
160 vcpu_e500->mas6 = sregs->u.e.mas6;
163 if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
164 return 0;
166 if (sregs->u.e.features & KVM_SREGS_E_SPE) {
167 vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_UNAVAIL] =
168 sregs->u.e.ivor_high[0];
169 vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_DATA] =
170 sregs->u.e.ivor_high[1];
171 vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_ROUND] =
172 sregs->u.e.ivor_high[2];
175 if (sregs->u.e.features & KVM_SREGS_E_PM) {
176 vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR] =
177 sregs->u.e.ivor_high[3];
180 return kvmppc_set_sregs_ivor(vcpu, sregs);
183 struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
185 struct kvmppc_vcpu_e500 *vcpu_e500;
186 struct kvm_vcpu *vcpu;
187 int err;
189 vcpu_e500 = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
190 if (!vcpu_e500) {
191 err = -ENOMEM;
192 goto out;
195 vcpu = &vcpu_e500->vcpu;
196 err = kvm_vcpu_init(vcpu, kvm, id);
197 if (err)
198 goto free_vcpu;
200 err = kvmppc_e500_tlb_init(vcpu_e500);
201 if (err)
202 goto uninit_vcpu;
204 vcpu->arch.shared = (void*)__get_free_page(GFP_KERNEL|__GFP_ZERO);
205 if (!vcpu->arch.shared)
206 goto uninit_tlb;
208 return vcpu;
210 uninit_tlb:
211 kvmppc_e500_tlb_uninit(vcpu_e500);
212 uninit_vcpu:
213 kvm_vcpu_uninit(vcpu);
214 free_vcpu:
215 kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
216 out:
217 return ERR_PTR(err);
220 void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
222 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
224 free_page((unsigned long)vcpu->arch.shared);
225 kvm_vcpu_uninit(vcpu);
226 kvmppc_e500_tlb_uninit(vcpu_e500);
227 kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
230 static int __init kvmppc_e500_init(void)
232 int r, i;
233 unsigned long ivor[3];
234 unsigned long max_ivor = 0;
236 r = kvmppc_booke_init();
237 if (r)
238 return r;
240 /* copy extra E500 exception handlers */
241 ivor[0] = mfspr(SPRN_IVOR32);
242 ivor[1] = mfspr(SPRN_IVOR33);
243 ivor[2] = mfspr(SPRN_IVOR34);
244 for (i = 0; i < 3; i++) {
245 if (ivor[i] > max_ivor)
246 max_ivor = ivor[i];
248 memcpy((void *)kvmppc_booke_handlers + ivor[i],
249 kvmppc_handlers_start + (i + 16) * kvmppc_handler_len,
250 kvmppc_handler_len);
252 flush_icache_range(kvmppc_booke_handlers,
253 kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
255 return kvm_init(NULL, sizeof(struct kvmppc_vcpu_e500), 0, THIS_MODULE);
258 static void __exit kvmppc_e500_exit(void)
260 kvmppc_booke_exit();
263 module_init(kvmppc_e500_init);
264 module_exit(kvmppc_e500_exit);