Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Scott Wood | 73196cd3 | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 2 | /* |
Mihai Caraman | c7ba777 | 2012-06-25 02:26:19 +0000 | [diff] [blame] | 3 | * Copyright (C) 2010,2012 Freescale Semiconductor, Inc. All rights reserved. |
Scott Wood | 73196cd3 | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 4 | * |
| 5 | * Author: Varun Sethi, <varun.sethi@freescale.com> |
| 6 | * |
| 7 | * Description: |
| 8 | * This file is derived from arch/powerpc/kvm/e500.c, |
| 9 | * by Yu Liu <yu.liu@freescale.com>. |
Scott Wood | 73196cd3 | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/kvm_host.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/err.h> |
| 15 | #include <linux/export.h> |
Alexander Graf | 398a76c | 2013-12-09 13:53:42 +0100 | [diff] [blame] | 16 | #include <linux/miscdevice.h> |
| 17 | #include <linux/module.h> |
Scott Wood | 73196cd3 | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 18 | |
| 19 | #include <asm/reg.h> |
| 20 | #include <asm/cputable.h> |
Scott Wood | 73196cd3 | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 21 | #include <asm/kvm_ppc.h> |
| 22 | #include <asm/dbell.h> |
| 23 | |
| 24 | #include "booke.h" |
| 25 | #include "e500.h" |
| 26 | |
| 27 | void kvmppc_set_pending_interrupt(struct kvm_vcpu *vcpu, enum int_class type) |
| 28 | { |
| 29 | enum ppc_dbell dbell_type; |
| 30 | unsigned long tag; |
| 31 | |
| 32 | switch (type) { |
| 33 | case INT_CLASS_NONCRIT: |
| 34 | dbell_type = PPC_G_DBELL; |
| 35 | break; |
| 36 | case INT_CLASS_CRIT: |
| 37 | dbell_type = PPC_G_DBELL_CRIT; |
| 38 | break; |
| 39 | case INT_CLASS_MC: |
| 40 | dbell_type = PPC_G_DBELL_MC; |
| 41 | break; |
| 42 | default: |
| 43 | WARN_ONCE(1, "%s: unknown int type %d\n", __func__, type); |
| 44 | return; |
| 45 | } |
| 46 | |
Mihai Caraman | 188e267 | 2014-09-01 12:01:58 +0300 | [diff] [blame] | 47 | preempt_disable(); |
| 48 | tag = PPC_DBELL_LPID(get_lpid(vcpu)) | vcpu->vcpu_id; |
Scott Wood | 73196cd3 | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 49 | mb(); |
| 50 | ppc_msgsnd(dbell_type, 0, tag); |
Mihai Caraman | 188e267 | 2014-09-01 12:01:58 +0300 | [diff] [blame] | 51 | preempt_enable(); |
Scott Wood | 73196cd3 | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /* gtlbe must not be mapped by more than one host tlb entry */ |
| 55 | void kvmppc_e500_tlbil_one(struct kvmppc_vcpu_e500 *vcpu_e500, |
| 56 | struct kvm_book3e_206_tlb_entry *gtlbe) |
| 57 | { |
| 58 | unsigned int tid, ts; |
Mihai Caraman | 66c9897 | 2012-06-25 02:26:26 +0000 | [diff] [blame] | 59 | gva_t eaddr; |
Mihai Caraman | 188e267 | 2014-09-01 12:01:58 +0300 | [diff] [blame] | 60 | u32 val; |
Scott Wood | 73196cd3 | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 61 | unsigned long flags; |
| 62 | |
| 63 | ts = get_tlb_ts(gtlbe); |
| 64 | tid = get_tlb_tid(gtlbe); |
Scott Wood | 73196cd3 | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 65 | |
| 66 | /* We search the host TLB to invalidate its shadow TLB entry */ |
| 67 | val = (tid << 16) | ts; |
| 68 | eaddr = get_tlb_eaddr(gtlbe); |
| 69 | |
| 70 | local_irq_save(flags); |
| 71 | |
| 72 | mtspr(SPRN_MAS6, val); |
Mihai Caraman | 188e267 | 2014-09-01 12:01:58 +0300 | [diff] [blame] | 73 | mtspr(SPRN_MAS5, MAS5_SGS | get_lpid(&vcpu_e500->vcpu)); |
Scott Wood | 73196cd3 | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 74 | |
| 75 | asm volatile("tlbsx 0, %[eaddr]\n" : : [eaddr] "r" (eaddr)); |
| 76 | val = mfspr(SPRN_MAS1); |
| 77 | if (val & MAS1_VALID) { |
| 78 | mtspr(SPRN_MAS1, val & ~MAS1_VALID); |
| 79 | asm volatile("tlbwe"); |
| 80 | } |
| 81 | mtspr(SPRN_MAS5, 0); |
| 82 | /* NOTE: tlbsx also updates mas8, so clear it for host tlbwe */ |
| 83 | mtspr(SPRN_MAS8, 0); |
| 84 | isync(); |
| 85 | |
| 86 | local_irq_restore(flags); |
| 87 | } |
| 88 | |
| 89 | void kvmppc_e500_tlbil_all(struct kvmppc_vcpu_e500 *vcpu_e500) |
| 90 | { |
| 91 | unsigned long flags; |
| 92 | |
| 93 | local_irq_save(flags); |
Mihai Caraman | 188e267 | 2014-09-01 12:01:58 +0300 | [diff] [blame] | 94 | mtspr(SPRN_MAS5, MAS5_SGS | get_lpid(&vcpu_e500->vcpu)); |
Scott Wood | 73196cd3 | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 95 | asm volatile("tlbilxlpid"); |
| 96 | mtspr(SPRN_MAS5, 0); |
| 97 | local_irq_restore(flags); |
| 98 | } |
| 99 | |
| 100 | void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid) |
| 101 | { |
| 102 | vcpu->arch.pid = pid; |
| 103 | } |
| 104 | |
| 105 | void kvmppc_mmu_msr_notify(struct kvm_vcpu *vcpu, u32 old_msr) |
| 106 | { |
| 107 | } |
| 108 | |
Mihai Caraman | 188e267 | 2014-09-01 12:01:58 +0300 | [diff] [blame] | 109 | /* We use two lpids per VM */ |
Mihai Caraman | 1f0eeb7 | 2014-06-18 10:15:22 +0300 | [diff] [blame] | 110 | static DEFINE_PER_CPU(struct kvm_vcpu *[KVMPPC_NR_LPIDS], last_vcpu_of_lpid); |
Scott Wood | c5e6cb0 | 2013-02-18 18:13:09 +0000 | [diff] [blame] | 111 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 112 | static void kvmppc_core_vcpu_load_e500mc(struct kvm_vcpu *vcpu, int cpu) |
Scott Wood | 73196cd3 | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 113 | { |
| 114 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
| 115 | |
| 116 | kvmppc_booke_vcpu_load(vcpu, cpu); |
| 117 | |
Mihai Caraman | 188e267 | 2014-09-01 12:01:58 +0300 | [diff] [blame] | 118 | mtspr(SPRN_LPID, get_lpid(vcpu)); |
Scott Wood | 73196cd3 | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 119 | mtspr(SPRN_EPCR, vcpu->arch.shadow_epcr); |
| 120 | mtspr(SPRN_GPIR, vcpu->vcpu_id); |
| 121 | mtspr(SPRN_MSRP, vcpu->arch.shadow_msrp); |
Mihai Caraman | 188e267 | 2014-09-01 12:01:58 +0300 | [diff] [blame] | 122 | vcpu->arch.eplc = EPC_EGS | (get_lpid(vcpu) << EPC_ELPID_SHIFT); |
| 123 | vcpu->arch.epsc = vcpu->arch.eplc; |
Scott Wood | 73196cd3 | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 124 | mtspr(SPRN_EPLC, vcpu->arch.eplc); |
| 125 | mtspr(SPRN_EPSC, vcpu->arch.epsc); |
| 126 | |
| 127 | mtspr(SPRN_GIVPR, vcpu->arch.ivpr); |
| 128 | mtspr(SPRN_GIVOR2, vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE]); |
| 129 | mtspr(SPRN_GIVOR8, vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL]); |
| 130 | mtspr(SPRN_GSPRG0, (unsigned long)vcpu->arch.shared->sprg0); |
| 131 | mtspr(SPRN_GSPRG1, (unsigned long)vcpu->arch.shared->sprg1); |
| 132 | mtspr(SPRN_GSPRG2, (unsigned long)vcpu->arch.shared->sprg2); |
| 133 | mtspr(SPRN_GSPRG3, (unsigned long)vcpu->arch.shared->sprg3); |
| 134 | |
| 135 | mtspr(SPRN_GSRR0, vcpu->arch.shared->srr0); |
| 136 | mtspr(SPRN_GSRR1, vcpu->arch.shared->srr1); |
| 137 | |
| 138 | mtspr(SPRN_GEPR, vcpu->arch.epr); |
| 139 | mtspr(SPRN_GDEAR, vcpu->arch.shared->dar); |
| 140 | mtspr(SPRN_GESR, vcpu->arch.shared->esr); |
| 141 | |
Scott Wood | c5e6cb0 | 2013-02-18 18:13:09 +0000 | [diff] [blame] | 142 | if (vcpu->arch.oldpir != mfspr(SPRN_PIR) || |
Christoph Lameter | 69111ba | 2014-10-21 15:23:25 -0500 | [diff] [blame] | 143 | __this_cpu_read(last_vcpu_of_lpid[get_lpid(vcpu)]) != vcpu) { |
Scott Wood | 73196cd3 | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 144 | kvmppc_e500_tlbil_all(vcpu_e500); |
Christoph Lameter | 69111ba | 2014-10-21 15:23:25 -0500 | [diff] [blame] | 145 | __this_cpu_write(last_vcpu_of_lpid[get_lpid(vcpu)], vcpu); |
Scott Wood | c5e6cb0 | 2013-02-18 18:13:09 +0000 | [diff] [blame] | 146 | } |
Scott Wood | 73196cd3 | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 149 | static void kvmppc_core_vcpu_put_e500mc(struct kvm_vcpu *vcpu) |
Scott Wood | 73196cd3 | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 150 | { |
| 151 | vcpu->arch.eplc = mfspr(SPRN_EPLC); |
| 152 | vcpu->arch.epsc = mfspr(SPRN_EPSC); |
| 153 | |
| 154 | vcpu->arch.shared->sprg0 = mfspr(SPRN_GSPRG0); |
| 155 | vcpu->arch.shared->sprg1 = mfspr(SPRN_GSPRG1); |
| 156 | vcpu->arch.shared->sprg2 = mfspr(SPRN_GSPRG2); |
| 157 | vcpu->arch.shared->sprg3 = mfspr(SPRN_GSPRG3); |
| 158 | |
| 159 | vcpu->arch.shared->srr0 = mfspr(SPRN_GSRR0); |
| 160 | vcpu->arch.shared->srr1 = mfspr(SPRN_GSRR1); |
| 161 | |
| 162 | vcpu->arch.epr = mfspr(SPRN_GEPR); |
| 163 | vcpu->arch.shared->dar = mfspr(SPRN_GDEAR); |
| 164 | vcpu->arch.shared->esr = mfspr(SPRN_GESR); |
| 165 | |
| 166 | vcpu->arch.oldpir = mfspr(SPRN_PIR); |
| 167 | |
| 168 | kvmppc_booke_vcpu_put(vcpu); |
| 169 | } |
| 170 | |
| 171 | int kvmppc_core_check_processor_compat(void) |
| 172 | { |
| 173 | int r; |
| 174 | |
| 175 | if (strcmp(cur_cpu_spec->cpu_name, "e500mc") == 0) |
| 176 | r = 0; |
| 177 | else if (strcmp(cur_cpu_spec->cpu_name, "e5500") == 0) |
| 178 | r = 0; |
Mihai Caraman | d2ca32a | 2014-09-01 12:01:59 +0300 | [diff] [blame] | 179 | #ifdef CONFIG_ALTIVEC |
| 180 | /* |
Adam Buchbinder | 446957b | 2016-02-24 10:51:11 -0800 | [diff] [blame] | 181 | * Since guests have the privilege to enable AltiVec, we need AltiVec |
Mihai Caraman | d2ca32a | 2014-09-01 12:01:59 +0300 | [diff] [blame] | 182 | * support in the host to save/restore their context. |
| 183 | * Don't use CPU_FTR_ALTIVEC to identify cores with AltiVec unit |
| 184 | * because it's cleared in the absence of CONFIG_ALTIVEC! |
| 185 | */ |
| 186 | else if (strcmp(cur_cpu_spec->cpu_name, "e6500") == 0) |
| 187 | r = 0; |
| 188 | #endif |
Scott Wood | 73196cd3 | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 189 | else |
| 190 | r = -ENOTSUPP; |
| 191 | |
| 192 | return r; |
| 193 | } |
| 194 | |
| 195 | int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu) |
| 196 | { |
| 197 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
| 198 | |
| 199 | vcpu->arch.shadow_epcr = SPRN_EPCR_DSIGS | SPRN_EPCR_DGTMI | \ |
| 200 | SPRN_EPCR_DUVD; |
Mihai Caraman | c7ba777 | 2012-06-25 02:26:19 +0000 | [diff] [blame] | 201 | #ifdef CONFIG_64BIT |
| 202 | vcpu->arch.shadow_epcr |= SPRN_EPCR_ICM; |
| 203 | #endif |
Bharat Bhushan | 37277b1 | 2014-08-06 12:08:53 +0530 | [diff] [blame] | 204 | vcpu->arch.shadow_msrp = MSRP_UCLEP | MSRP_PMMP; |
Scott Wood | 73196cd3 | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 205 | |
| 206 | vcpu->arch.pvr = mfspr(SPRN_PVR); |
| 207 | vcpu_e500->svr = mfspr(SPRN_SVR); |
| 208 | |
| 209 | vcpu->arch.cpu_type = KVM_CPU_E500MC; |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 214 | static int kvmppc_core_get_sregs_e500mc(struct kvm_vcpu *vcpu, |
| 215 | struct kvm_sregs *sregs) |
Scott Wood | 73196cd3 | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 216 | { |
| 217 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
| 218 | |
| 219 | sregs->u.e.features |= KVM_SREGS_E_ARCH206_MMU | KVM_SREGS_E_PM | |
| 220 | KVM_SREGS_E_PC; |
| 221 | sregs->u.e.impl_id = KVM_SREGS_E_IMPL_FSL; |
| 222 | |
| 223 | sregs->u.e.impl.fsl.features = 0; |
| 224 | sregs->u.e.impl.fsl.svr = vcpu_e500->svr; |
| 225 | sregs->u.e.impl.fsl.hid0 = vcpu_e500->hid0; |
| 226 | sregs->u.e.impl.fsl.mcar = vcpu_e500->mcar; |
| 227 | |
| 228 | kvmppc_get_sregs_e500_tlb(vcpu, sregs); |
| 229 | |
| 230 | sregs->u.e.ivor_high[3] = |
| 231 | vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR]; |
| 232 | sregs->u.e.ivor_high[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL]; |
| 233 | sregs->u.e.ivor_high[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL_CRIT]; |
| 234 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 235 | return kvmppc_get_sregs_ivor(vcpu, sregs); |
Scott Wood | 73196cd3 | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 238 | static int kvmppc_core_set_sregs_e500mc(struct kvm_vcpu *vcpu, |
| 239 | struct kvm_sregs *sregs) |
Scott Wood | 73196cd3 | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 240 | { |
| 241 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
| 242 | int ret; |
| 243 | |
| 244 | if (sregs->u.e.impl_id == KVM_SREGS_E_IMPL_FSL) { |
| 245 | vcpu_e500->svr = sregs->u.e.impl.fsl.svr; |
| 246 | vcpu_e500->hid0 = sregs->u.e.impl.fsl.hid0; |
| 247 | vcpu_e500->mcar = sregs->u.e.impl.fsl.mcar; |
| 248 | } |
| 249 | |
| 250 | ret = kvmppc_set_sregs_e500_tlb(vcpu, sregs); |
| 251 | if (ret < 0) |
| 252 | return ret; |
| 253 | |
| 254 | if (!(sregs->u.e.features & KVM_SREGS_E_IVOR)) |
| 255 | return 0; |
| 256 | |
| 257 | if (sregs->u.e.features & KVM_SREGS_E_PM) { |
| 258 | vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR] = |
| 259 | sregs->u.e.ivor_high[3]; |
| 260 | } |
| 261 | |
| 262 | if (sregs->u.e.features & KVM_SREGS_E_PC) { |
| 263 | vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL] = |
| 264 | sregs->u.e.ivor_high[4]; |
| 265 | vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL_CRIT] = |
| 266 | sregs->u.e.ivor_high[5]; |
| 267 | } |
| 268 | |
| 269 | return kvmppc_set_sregs_ivor(vcpu, sregs); |
| 270 | } |
| 271 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 272 | static int kvmppc_get_one_reg_e500mc(struct kvm_vcpu *vcpu, u64 id, |
| 273 | union kvmppc_one_reg *val) |
Mihai Caraman | 35b299e | 2013-04-11 00:03:07 +0000 | [diff] [blame] | 274 | { |
Bharat Bhushan | 28d2f42 | 2014-07-25 11:21:08 +0530 | [diff] [blame] | 275 | int r = 0; |
| 276 | |
| 277 | switch (id) { |
| 278 | case KVM_REG_PPC_SPRG9: |
| 279 | *val = get_reg_val(id, vcpu->arch.sprg9); |
| 280 | break; |
| 281 | default: |
| 282 | r = kvmppc_get_one_reg_e500_tlb(vcpu, id, val); |
| 283 | } |
| 284 | |
Mihai Caraman | a85d2aa | 2013-04-11 00:03:08 +0000 | [diff] [blame] | 285 | return r; |
Mihai Caraman | 35b299e | 2013-04-11 00:03:07 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 288 | static int kvmppc_set_one_reg_e500mc(struct kvm_vcpu *vcpu, u64 id, |
| 289 | union kvmppc_one_reg *val) |
Mihai Caraman | 35b299e | 2013-04-11 00:03:07 +0000 | [diff] [blame] | 290 | { |
Bharat Bhushan | 28d2f42 | 2014-07-25 11:21:08 +0530 | [diff] [blame] | 291 | int r = 0; |
| 292 | |
| 293 | switch (id) { |
| 294 | case KVM_REG_PPC_SPRG9: |
| 295 | vcpu->arch.sprg9 = set_reg_val(id, *val); |
| 296 | break; |
| 297 | default: |
| 298 | r = kvmppc_set_one_reg_e500_tlb(vcpu, id, val); |
| 299 | } |
| 300 | |
Mihai Caraman | a85d2aa | 2013-04-11 00:03:08 +0000 | [diff] [blame] | 301 | return r; |
Mihai Caraman | 35b299e | 2013-04-11 00:03:07 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 304 | static struct kvm_vcpu *kvmppc_core_vcpu_create_e500mc(struct kvm *kvm, |
| 305 | unsigned int id) |
Scott Wood | 73196cd3 | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 306 | { |
| 307 | struct kvmppc_vcpu_e500 *vcpu_e500; |
| 308 | struct kvm_vcpu *vcpu; |
| 309 | int err; |
| 310 | |
| 311 | vcpu_e500 = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
| 312 | if (!vcpu_e500) { |
| 313 | err = -ENOMEM; |
| 314 | goto out; |
| 315 | } |
| 316 | vcpu = &vcpu_e500->vcpu; |
| 317 | |
| 318 | /* Invalid PIR value -- this LPID dosn't have valid state on any cpu */ |
| 319 | vcpu->arch.oldpir = 0xffffffff; |
| 320 | |
| 321 | err = kvm_vcpu_init(vcpu, kvm, id); |
| 322 | if (err) |
| 323 | goto free_vcpu; |
| 324 | |
| 325 | err = kvmppc_e500_tlb_init(vcpu_e500); |
| 326 | if (err) |
| 327 | goto uninit_vcpu; |
| 328 | |
| 329 | vcpu->arch.shared = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO); |
Dan Carpenter | 50a1a25 | 2017-07-13 10:38:49 +0300 | [diff] [blame] | 330 | if (!vcpu->arch.shared) { |
| 331 | err = -ENOMEM; |
Scott Wood | 73196cd3 | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 332 | goto uninit_tlb; |
Dan Carpenter | 50a1a25 | 2017-07-13 10:38:49 +0300 | [diff] [blame] | 333 | } |
Scott Wood | 73196cd3 | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 334 | |
| 335 | return vcpu; |
| 336 | |
| 337 | uninit_tlb: |
| 338 | kvmppc_e500_tlb_uninit(vcpu_e500); |
| 339 | uninit_vcpu: |
| 340 | kvm_vcpu_uninit(vcpu); |
| 341 | |
| 342 | free_vcpu: |
| 343 | kmem_cache_free(kvm_vcpu_cache, vcpu_e500); |
| 344 | out: |
| 345 | return ERR_PTR(err); |
| 346 | } |
| 347 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 348 | static void kvmppc_core_vcpu_free_e500mc(struct kvm_vcpu *vcpu) |
Scott Wood | 73196cd3 | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 349 | { |
| 350 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
| 351 | |
| 352 | free_page((unsigned long)vcpu->arch.shared); |
| 353 | kvmppc_e500_tlb_uninit(vcpu_e500); |
| 354 | kvm_vcpu_uninit(vcpu); |
| 355 | kmem_cache_free(kvm_vcpu_cache, vcpu_e500); |
| 356 | } |
| 357 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 358 | static int kvmppc_core_init_vm_e500mc(struct kvm *kvm) |
Scott Wood | 73196cd3 | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 359 | { |
| 360 | int lpid; |
| 361 | |
| 362 | lpid = kvmppc_alloc_lpid(); |
| 363 | if (lpid < 0) |
| 364 | return lpid; |
| 365 | |
Mihai Caraman | 188e267 | 2014-09-01 12:01:58 +0300 | [diff] [blame] | 366 | /* |
| 367 | * Use two lpids per VM on cores with two threads like e6500. Use |
| 368 | * even numbers to speedup vcpu lpid computation with consecutive lpids |
| 369 | * per VM. vm1 will use lpids 2 and 3, vm2 lpids 4 and 5, and so on. |
| 370 | */ |
| 371 | if (threads_per_core == 2) |
| 372 | lpid <<= 1; |
| 373 | |
Scott Wood | 73196cd3 | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 374 | kvm->arch.lpid = lpid; |
| 375 | return 0; |
| 376 | } |
| 377 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 378 | static void kvmppc_core_destroy_vm_e500mc(struct kvm *kvm) |
Scott Wood | 73196cd3 | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 379 | { |
Mihai Caraman | 188e267 | 2014-09-01 12:01:58 +0300 | [diff] [blame] | 380 | int lpid = kvm->arch.lpid; |
| 381 | |
| 382 | if (threads_per_core == 2) |
| 383 | lpid >>= 1; |
| 384 | |
| 385 | kvmppc_free_lpid(lpid); |
Scott Wood | 73196cd3 | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 388 | static struct kvmppc_ops kvm_ops_e500mc = { |
| 389 | .get_sregs = kvmppc_core_get_sregs_e500mc, |
| 390 | .set_sregs = kvmppc_core_set_sregs_e500mc, |
| 391 | .get_one_reg = kvmppc_get_one_reg_e500mc, |
| 392 | .set_one_reg = kvmppc_set_one_reg_e500mc, |
| 393 | .vcpu_load = kvmppc_core_vcpu_load_e500mc, |
| 394 | .vcpu_put = kvmppc_core_vcpu_put_e500mc, |
| 395 | .vcpu_create = kvmppc_core_vcpu_create_e500mc, |
| 396 | .vcpu_free = kvmppc_core_vcpu_free_e500mc, |
| 397 | .mmu_destroy = kvmppc_mmu_destroy_e500, |
| 398 | .init_vm = kvmppc_core_init_vm_e500mc, |
| 399 | .destroy_vm = kvmppc_core_destroy_vm_e500mc, |
| 400 | .emulate_op = kvmppc_core_emulate_op_e500, |
| 401 | .emulate_mtspr = kvmppc_core_emulate_mtspr_e500, |
| 402 | .emulate_mfspr = kvmppc_core_emulate_mfspr_e500, |
| 403 | }; |
| 404 | |
Scott Wood | 73196cd3 | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 405 | static int __init kvmppc_e500mc_init(void) |
| 406 | { |
| 407 | int r; |
| 408 | |
| 409 | r = kvmppc_booke_init(); |
| 410 | if (r) |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 411 | goto err_out; |
Scott Wood | 73196cd3 | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 412 | |
Mihai Caraman | 188e267 | 2014-09-01 12:01:58 +0300 | [diff] [blame] | 413 | /* |
| 414 | * Use two lpids per VM on dual threaded processors like e6500 |
| 415 | * to workarround the lack of tlb write conditional instruction. |
| 416 | * Expose half the number of available hardware lpids to the lpid |
| 417 | * allocator. |
| 418 | */ |
| 419 | kvmppc_init_lpid(KVMPPC_NR_LPIDS/threads_per_core); |
Scott Wood | 73196cd3 | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 420 | kvmppc_claim_lpid(0); /* host */ |
| 421 | |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 422 | r = kvm_init(NULL, sizeof(struct kvmppc_vcpu_e500), 0, THIS_MODULE); |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 423 | if (r) |
| 424 | goto err_out; |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 425 | kvm_ops_e500mc.owner = THIS_MODULE; |
| 426 | kvmppc_pr_ops = &kvm_ops_e500mc; |
| 427 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 428 | err_out: |
| 429 | return r; |
Scott Wood | 73196cd3 | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | static void __exit kvmppc_e500mc_exit(void) |
| 433 | { |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 434 | kvmppc_pr_ops = NULL; |
Scott Wood | 73196cd3 | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 435 | kvmppc_booke_exit(); |
| 436 | } |
| 437 | |
| 438 | module_init(kvmppc_e500mc_init); |
| 439 | module_exit(kvmppc_e500mc_exit); |
Alexander Graf | 398a76c | 2013-12-09 13:53:42 +0100 | [diff] [blame] | 440 | MODULE_ALIAS_MISCDEV(KVM_MINOR); |
| 441 | MODULE_ALIAS("devname:kvm"); |