Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 1 | /* |
Scott Wood | 4cd35f67 | 2011-06-14 18:34:31 -0500 | [diff] [blame] | 2 | * Copyright (C) 2008-2011 Freescale Semiconductor, Inc. All rights reserved. |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 16 | #include <linux/slab.h> |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 17 | #include <linux/err.h> |
Scott Wood | fae9dbb | 2011-12-20 14:43:45 +0000 | [diff] [blame] | 18 | #include <linux/export.h> |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 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 | |
Hollis Blanchard | bb3a8a1 | 2009-01-03 16:23:13 -0600 | [diff] [blame] | 26 | #include "booke.h" |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 27 | #include "e500_tlb.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_e500_tlb_load(vcpu, cpu); |
| 40 | } |
| 41 | |
| 42 | void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu) |
| 43 | { |
| 44 | kvmppc_e500_tlb_put(vcpu); |
Scott Wood | 4cd35f67 | 2011-06-14 18:34:31 -0500 | [diff] [blame] | 45 | |
| 46 | #ifdef CONFIG_SPE |
| 47 | if (vcpu->arch.shadow_msr & MSR_SPE) |
| 48 | kvmppc_vcpu_disable_spe(vcpu); |
| 49 | #endif |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | int kvmppc_core_check_processor_compat(void) |
| 53 | { |
| 54 | int r; |
| 55 | |
| 56 | if (strcmp(cur_cpu_spec->cpu_name, "e500v2") == 0) |
| 57 | r = 0; |
| 58 | else |
| 59 | r = -ENOTSUPP; |
| 60 | |
| 61 | return r; |
| 62 | } |
| 63 | |
| 64 | int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu) |
| 65 | { |
| 66 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
| 67 | |
| 68 | kvmppc_e500_tlb_setup(vcpu_e500); |
| 69 | |
Liu Yu | a9040f2 | 2010-01-22 18:50:30 +0800 | [diff] [blame] | 70 | /* Registers init */ |
| 71 | vcpu->arch.pvr = mfspr(SPRN_PVR); |
Scott Wood | 90d34b0 | 2011-03-29 16:49:10 -0500 | [diff] [blame] | 72 | vcpu_e500->svr = mfspr(SPRN_SVR); |
Liu Yu | a9040f2 | 2010-01-22 18:50:30 +0800 | [diff] [blame] | 73 | |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 74 | vcpu->arch.cpu_type = KVM_CPU_E500V2; |
| 75 | |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 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 | |
Scott Wood | 5ce941e | 2011-04-27 17:24:21 -0500 | [diff] [blame] | 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 | |
Scott Wood | b590497 | 2011-11-08 18:23:30 -0600 | [diff] [blame^] | 118 | sregs->u.e.mas0 = vcpu->arch.shared->mas0; |
| 119 | sregs->u.e.mas1 = vcpu->arch.shared->mas1; |
| 120 | sregs->u.e.mas2 = vcpu->arch.shared->mas2; |
| 121 | sregs->u.e.mas7_3 = vcpu->arch.shared->mas7_3; |
| 122 | sregs->u.e.mas4 = vcpu->arch.shared->mas4; |
| 123 | sregs->u.e.mas6 = vcpu->arch.shared->mas6; |
Scott Wood | 5ce941e | 2011-04-27 17:24:21 -0500 | [diff] [blame] | 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) { |
Scott Wood | b590497 | 2011-11-08 18:23:30 -0600 | [diff] [blame^] | 151 | vcpu->arch.shared->mas0 = sregs->u.e.mas0; |
| 152 | vcpu->arch.shared->mas1 = sregs->u.e.mas1; |
| 153 | vcpu->arch.shared->mas2 = sregs->u.e.mas2; |
| 154 | vcpu->arch.shared->mas7_3 = sregs->u.e.mas7_3; |
| 155 | vcpu->arch.shared->mas4 = sregs->u.e.mas4; |
| 156 | vcpu->arch.shared->mas6 = sregs->u.e.mas6; |
Scott Wood | 5ce941e | 2011-04-27 17:24:21 -0500 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | if (!(sregs->u.e.features & KVM_SREGS_E_IVOR)) |
| 160 | return 0; |
| 161 | |
| 162 | if (sregs->u.e.features & KVM_SREGS_E_SPE) { |
| 163 | vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_UNAVAIL] = |
| 164 | sregs->u.e.ivor_high[0]; |
| 165 | vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_DATA] = |
| 166 | sregs->u.e.ivor_high[1]; |
| 167 | vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_ROUND] = |
| 168 | sregs->u.e.ivor_high[2]; |
| 169 | } |
| 170 | |
| 171 | if (sregs->u.e.features & KVM_SREGS_E_PM) { |
| 172 | vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR] = |
| 173 | sregs->u.e.ivor_high[3]; |
| 174 | } |
| 175 | |
| 176 | return kvmppc_set_sregs_ivor(vcpu, sregs); |
| 177 | } |
| 178 | |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 179 | struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id) |
| 180 | { |
| 181 | struct kvmppc_vcpu_e500 *vcpu_e500; |
| 182 | struct kvm_vcpu *vcpu; |
| 183 | int err; |
| 184 | |
| 185 | vcpu_e500 = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
| 186 | if (!vcpu_e500) { |
| 187 | err = -ENOMEM; |
| 188 | goto out; |
| 189 | } |
| 190 | |
| 191 | vcpu = &vcpu_e500->vcpu; |
| 192 | err = kvm_vcpu_init(vcpu, kvm, id); |
| 193 | if (err) |
| 194 | goto free_vcpu; |
| 195 | |
| 196 | err = kvmppc_e500_tlb_init(vcpu_e500); |
| 197 | if (err) |
| 198 | goto uninit_vcpu; |
| 199 | |
Alexander Graf | 96bc451 | 2010-07-29 14:47:42 +0200 | [diff] [blame] | 200 | vcpu->arch.shared = (void*)__get_free_page(GFP_KERNEL|__GFP_ZERO); |
| 201 | if (!vcpu->arch.shared) |
| 202 | goto uninit_tlb; |
| 203 | |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 204 | return vcpu; |
| 205 | |
Alexander Graf | 96bc451 | 2010-07-29 14:47:42 +0200 | [diff] [blame] | 206 | uninit_tlb: |
| 207 | kvmppc_e500_tlb_uninit(vcpu_e500); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 208 | uninit_vcpu: |
| 209 | kvm_vcpu_uninit(vcpu); |
| 210 | free_vcpu: |
| 211 | kmem_cache_free(kvm_vcpu_cache, vcpu_e500); |
| 212 | out: |
| 213 | return ERR_PTR(err); |
| 214 | } |
| 215 | |
| 216 | void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu) |
| 217 | { |
| 218 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
| 219 | |
Alexander Graf | 96bc451 | 2010-07-29 14:47:42 +0200 | [diff] [blame] | 220 | free_page((unsigned long)vcpu->arch.shared); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 221 | kvm_vcpu_uninit(vcpu); |
Scott Wood | f22e2f0 | 2010-10-05 14:22:41 -0500 | [diff] [blame] | 222 | kvmppc_e500_tlb_uninit(vcpu_e500); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 223 | kmem_cache_free(kvm_vcpu_cache, vcpu_e500); |
| 224 | } |
| 225 | |
Stephen Rothwell | 2986b8c | 2009-06-02 11:46:14 +1000 | [diff] [blame] | 226 | static int __init kvmppc_e500_init(void) |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 227 | { |
Hollis Blanchard | bb3a8a1 | 2009-01-03 16:23:13 -0600 | [diff] [blame] | 228 | int r, i; |
| 229 | unsigned long ivor[3]; |
| 230 | unsigned long max_ivor = 0; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 231 | |
| 232 | r = kvmppc_booke_init(); |
| 233 | if (r) |
| 234 | return r; |
| 235 | |
Hollis Blanchard | bb3a8a1 | 2009-01-03 16:23:13 -0600 | [diff] [blame] | 236 | /* copy extra E500 exception handlers */ |
| 237 | ivor[0] = mfspr(SPRN_IVOR32); |
| 238 | ivor[1] = mfspr(SPRN_IVOR33); |
| 239 | ivor[2] = mfspr(SPRN_IVOR34); |
| 240 | for (i = 0; i < 3; i++) { |
| 241 | if (ivor[i] > max_ivor) |
| 242 | max_ivor = ivor[i]; |
| 243 | |
| 244 | memcpy((void *)kvmppc_booke_handlers + ivor[i], |
| 245 | kvmppc_handlers_start + (i + 16) * kvmppc_handler_len, |
| 246 | kvmppc_handler_len); |
| 247 | } |
| 248 | flush_icache_range(kvmppc_booke_handlers, |
| 249 | kvmppc_booke_handlers + max_ivor + kvmppc_handler_len); |
| 250 | |
Avi Kivity | 0ee75be | 2010-04-28 15:39:01 +0300 | [diff] [blame] | 251 | return kvm_init(NULL, sizeof(struct kvmppc_vcpu_e500), 0, THIS_MODULE); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 252 | } |
| 253 | |
Jean Delvare | a06cdb5 | 2010-05-18 09:34:12 +0200 | [diff] [blame] | 254 | static void __exit kvmppc_e500_exit(void) |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 255 | { |
| 256 | kvmppc_booke_exit(); |
| 257 | } |
| 258 | |
| 259 | module_init(kvmppc_e500_init); |
| 260 | module_exit(kvmppc_e500_exit); |