Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

KVM: PPC: e500: refactor core-specific TLB code

The PID handling is e500v1/v2-specific, and is moved to e500.c.

The MMU sregs code and kvmppc_core_vcpu_translate will be shared with
e500mc, and is moved from e500.c to e500_tlb.c.

Partially based on patches from Liu Yu <yu.liu@freescale.com>.

Signed-off-by: Scott Wood <scottwood@freescale.com>
[agraf: fix bisectability]
Signed-off-by: Alexander Graf <agraf@suse.de>
Signed-off-by: Avi Kivity <avi@redhat.com>

authored by

Scott Wood and committed by
Avi Kivity
8fdd21a2 52e1718c

+474 -415
+2
arch/powerpc/include/asm/kvm_host.h
··· 426 426 ulong fault_esr; 427 427 ulong queued_dear; 428 428 ulong queued_esr; 429 + u32 tlbcfg[4]; 430 + u32 mmucfg; 429 431 #endif 430 432 gpa_t paddr_accessed; 431 433
+305 -52
arch/powerpc/kvm/e500.c
··· 22 22 #include <asm/tlbflush.h> 23 23 #include <asm/kvm_ppc.h> 24 24 25 + #include "../mm/mmu_decl.h" 25 26 #include "booke.h" 26 27 #include "e500.h" 28 + 29 + struct id { 30 + unsigned long val; 31 + struct id **pentry; 32 + }; 33 + 34 + #define NUM_TIDS 256 35 + 36 + /* 37 + * This table provide mappings from: 38 + * (guestAS,guestTID,guestPR) --> ID of physical cpu 39 + * guestAS [0..1] 40 + * guestTID [0..255] 41 + * guestPR [0..1] 42 + * ID [1..255] 43 + * Each vcpu keeps one vcpu_id_table. 44 + */ 45 + struct vcpu_id_table { 46 + struct id id[2][NUM_TIDS][2]; 47 + }; 48 + 49 + /* 50 + * This table provide reversed mappings of vcpu_id_table: 51 + * ID --> address of vcpu_id_table item. 52 + * Each physical core has one pcpu_id_table. 53 + */ 54 + struct pcpu_id_table { 55 + struct id *entry[NUM_TIDS]; 56 + }; 57 + 58 + static DEFINE_PER_CPU(struct pcpu_id_table, pcpu_sids); 59 + 60 + /* This variable keeps last used shadow ID on local core. 61 + * The valid range of shadow ID is [1..255] */ 62 + static DEFINE_PER_CPU(unsigned long, pcpu_last_used_sid); 63 + 64 + /* 65 + * Allocate a free shadow id and setup a valid sid mapping in given entry. 66 + * A mapping is only valid when vcpu_id_table and pcpu_id_table are match. 67 + * 68 + * The caller must have preemption disabled, and keep it that way until 69 + * it has finished with the returned shadow id (either written into the 70 + * TLB or arch.shadow_pid, or discarded). 71 + */ 72 + static inline int local_sid_setup_one(struct id *entry) 73 + { 74 + unsigned long sid; 75 + int ret = -1; 76 + 77 + sid = ++(__get_cpu_var(pcpu_last_used_sid)); 78 + if (sid < NUM_TIDS) { 79 + __get_cpu_var(pcpu_sids).entry[sid] = entry; 80 + entry->val = sid; 81 + entry->pentry = &__get_cpu_var(pcpu_sids).entry[sid]; 82 + ret = sid; 83 + } 84 + 85 + /* 86 + * If sid == NUM_TIDS, we've run out of sids. We return -1, and 87 + * the caller will invalidate everything and start over. 88 + * 89 + * sid > NUM_TIDS indicates a race, which we disable preemption to 90 + * avoid. 91 + */ 92 + WARN_ON(sid > NUM_TIDS); 93 + 94 + return ret; 95 + } 96 + 97 + /* 98 + * Check if given entry contain a valid shadow id mapping. 99 + * An ID mapping is considered valid only if 100 + * both vcpu and pcpu know this mapping. 101 + * 102 + * The caller must have preemption disabled, and keep it that way until 103 + * it has finished with the returned shadow id (either written into the 104 + * TLB or arch.shadow_pid, or discarded). 105 + */ 106 + static inline int local_sid_lookup(struct id *entry) 107 + { 108 + if (entry && entry->val != 0 && 109 + __get_cpu_var(pcpu_sids).entry[entry->val] == entry && 110 + entry->pentry == &__get_cpu_var(pcpu_sids).entry[entry->val]) 111 + return entry->val; 112 + return -1; 113 + } 114 + 115 + /* Invalidate all id mappings on local core -- call with preempt disabled */ 116 + static inline void local_sid_destroy_all(void) 117 + { 118 + __get_cpu_var(pcpu_last_used_sid) = 0; 119 + memset(&__get_cpu_var(pcpu_sids), 0, sizeof(__get_cpu_var(pcpu_sids))); 120 + } 121 + 122 + static void *kvmppc_e500_id_table_alloc(struct kvmppc_vcpu_e500 *vcpu_e500) 123 + { 124 + vcpu_e500->idt = kzalloc(sizeof(struct vcpu_id_table), GFP_KERNEL); 125 + return vcpu_e500->idt; 126 + } 127 + 128 + static void kvmppc_e500_id_table_free(struct kvmppc_vcpu_e500 *vcpu_e500) 129 + { 130 + kfree(vcpu_e500->idt); 131 + vcpu_e500->idt = NULL; 132 + } 133 + 134 + /* Map guest pid to shadow. 135 + * We use PID to keep shadow of current guest non-zero PID, 136 + * and use PID1 to keep shadow of guest zero PID. 137 + * So that guest tlbe with TID=0 can be accessed at any time */ 138 + static void kvmppc_e500_recalc_shadow_pid(struct kvmppc_vcpu_e500 *vcpu_e500) 139 + { 140 + preempt_disable(); 141 + vcpu_e500->vcpu.arch.shadow_pid = kvmppc_e500_get_sid(vcpu_e500, 142 + get_cur_as(&vcpu_e500->vcpu), 143 + get_cur_pid(&vcpu_e500->vcpu), 144 + get_cur_pr(&vcpu_e500->vcpu), 1); 145 + vcpu_e500->vcpu.arch.shadow_pid1 = kvmppc_e500_get_sid(vcpu_e500, 146 + get_cur_as(&vcpu_e500->vcpu), 0, 147 + get_cur_pr(&vcpu_e500->vcpu), 1); 148 + preempt_enable(); 149 + } 150 + 151 + /* Invalidate all mappings on vcpu */ 152 + static void kvmppc_e500_id_table_reset_all(struct kvmppc_vcpu_e500 *vcpu_e500) 153 + { 154 + memset(vcpu_e500->idt, 0, sizeof(struct vcpu_id_table)); 155 + 156 + /* Update shadow pid when mappings are changed */ 157 + kvmppc_e500_recalc_shadow_pid(vcpu_e500); 158 + } 159 + 160 + /* Invalidate one ID mapping on vcpu */ 161 + static inline void kvmppc_e500_id_table_reset_one( 162 + struct kvmppc_vcpu_e500 *vcpu_e500, 163 + int as, int pid, int pr) 164 + { 165 + struct vcpu_id_table *idt = vcpu_e500->idt; 166 + 167 + BUG_ON(as >= 2); 168 + BUG_ON(pid >= NUM_TIDS); 169 + BUG_ON(pr >= 2); 170 + 171 + idt->id[as][pid][pr].val = 0; 172 + idt->id[as][pid][pr].pentry = NULL; 173 + 174 + /* Update shadow pid when mappings are changed */ 175 + kvmppc_e500_recalc_shadow_pid(vcpu_e500); 176 + } 177 + 178 + /* 179 + * Map guest (vcpu,AS,ID,PR) to physical core shadow id. 180 + * This function first lookup if a valid mapping exists, 181 + * if not, then creates a new one. 182 + * 183 + * The caller must have preemption disabled, and keep it that way until 184 + * it has finished with the returned shadow id (either written into the 185 + * TLB or arch.shadow_pid, or discarded). 186 + */ 187 + unsigned int kvmppc_e500_get_sid(struct kvmppc_vcpu_e500 *vcpu_e500, 188 + unsigned int as, unsigned int gid, 189 + unsigned int pr, int avoid_recursion) 190 + { 191 + struct vcpu_id_table *idt = vcpu_e500->idt; 192 + int sid; 193 + 194 + BUG_ON(as >= 2); 195 + BUG_ON(gid >= NUM_TIDS); 196 + BUG_ON(pr >= 2); 197 + 198 + sid = local_sid_lookup(&idt->id[as][gid][pr]); 199 + 200 + while (sid <= 0) { 201 + /* No mapping yet */ 202 + sid = local_sid_setup_one(&idt->id[as][gid][pr]); 203 + if (sid <= 0) { 204 + _tlbil_all(); 205 + local_sid_destroy_all(); 206 + } 207 + 208 + /* Update shadow pid when mappings are changed */ 209 + if (!avoid_recursion) 210 + kvmppc_e500_recalc_shadow_pid(vcpu_e500); 211 + } 212 + 213 + return sid; 214 + } 215 + 216 + unsigned int kvmppc_e500_get_tlb_stid(struct kvm_vcpu *vcpu, 217 + struct kvm_book3e_206_tlb_entry *gtlbe) 218 + { 219 + return kvmppc_e500_get_sid(to_e500(vcpu), get_tlb_ts(gtlbe), 220 + get_tlb_tid(gtlbe), get_cur_pr(vcpu), 0); 221 + } 222 + 223 + void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid) 224 + { 225 + struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); 226 + 227 + if (vcpu->arch.pid != pid) { 228 + vcpu_e500->pid[0] = vcpu->arch.pid = pid; 229 + kvmppc_e500_recalc_shadow_pid(vcpu_e500); 230 + } 231 + } 232 + 233 + /* gtlbe must not be mapped by more than one host tlbe */ 234 + void kvmppc_e500_tlbil_one(struct kvmppc_vcpu_e500 *vcpu_e500, 235 + struct kvm_book3e_206_tlb_entry *gtlbe) 236 + { 237 + struct vcpu_id_table *idt = vcpu_e500->idt; 238 + unsigned int pr, tid, ts, pid; 239 + u32 val, eaddr; 240 + unsigned long flags; 241 + 242 + ts = get_tlb_ts(gtlbe); 243 + tid = get_tlb_tid(gtlbe); 244 + 245 + preempt_disable(); 246 + 247 + /* One guest ID may be mapped to two shadow IDs */ 248 + for (pr = 0; pr < 2; pr++) { 249 + /* 250 + * The shadow PID can have a valid mapping on at most one 251 + * host CPU. In the common case, it will be valid on this 252 + * CPU, in which case we do a local invalidation of the 253 + * specific address. 254 + * 255 + * If the shadow PID is not valid on the current host CPU, 256 + * we invalidate the entire shadow PID. 257 + */ 258 + pid = local_sid_lookup(&idt->id[ts][tid][pr]); 259 + if (pid <= 0) { 260 + kvmppc_e500_id_table_reset_one(vcpu_e500, ts, tid, pr); 261 + continue; 262 + } 263 + 264 + /* 265 + * The guest is invalidating a 4K entry which is in a PID 266 + * that has a valid shadow mapping on this host CPU. We 267 + * search host TLB to invalidate it's shadow TLB entry, 268 + * similar to __tlbil_va except that we need to look in AS1. 269 + */ 270 + val = (pid << MAS6_SPID_SHIFT) | MAS6_SAS; 271 + eaddr = get_tlb_eaddr(gtlbe); 272 + 273 + local_irq_save(flags); 274 + 275 + mtspr(SPRN_MAS6, val); 276 + asm volatile("tlbsx 0, %[eaddr]" : : [eaddr] "r" (eaddr)); 277 + val = mfspr(SPRN_MAS1); 278 + if (val & MAS1_VALID) { 279 + mtspr(SPRN_MAS1, val & ~MAS1_VALID); 280 + asm volatile("tlbwe"); 281 + } 282 + 283 + local_irq_restore(flags); 284 + } 285 + 286 + preempt_enable(); 287 + } 288 + 289 + void kvmppc_e500_tlbil_all(struct kvmppc_vcpu_e500 *vcpu_e500) 290 + { 291 + kvmppc_e500_id_table_reset_all(vcpu_e500); 292 + } 293 + 294 + void kvmppc_mmu_msr_notify(struct kvm_vcpu *vcpu, u32 old_msr) 295 + { 296 + /* Recalc shadow pid since MSR changes */ 297 + kvmppc_e500_recalc_shadow_pid(to_e500(vcpu)); 298 + } 27 299 28 300 void kvmppc_core_load_host_debugstate(struct kvm_vcpu *vcpu) 29 301 { ··· 308 36 void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu) 309 37 { 310 38 kvmppc_booke_vcpu_load(vcpu, cpu); 311 - kvmppc_e500_tlb_load(vcpu, cpu); 39 + 40 + /* Shadow PID may be expired on local core */ 41 + kvmppc_e500_recalc_shadow_pid(to_e500(vcpu)); 312 42 } 313 43 314 44 void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu) 315 45 { 316 - kvmppc_e500_tlb_put(vcpu); 317 - 318 46 #ifdef CONFIG_SPE 319 47 if (vcpu->arch.shadow_msr & MSR_SPE) 320 48 kvmppc_vcpu_disable_spe(vcpu); ··· 335 63 return r; 336 64 } 337 65 66 + static void kvmppc_e500_tlb_setup(struct kvmppc_vcpu_e500 *vcpu_e500) 67 + { 68 + struct kvm_book3e_206_tlb_entry *tlbe; 69 + 70 + /* Insert large initial mapping for guest. */ 71 + tlbe = get_entry(vcpu_e500, 1, 0); 72 + tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOK3E_PAGESZ_256M); 73 + tlbe->mas2 = 0; 74 + tlbe->mas7_3 = E500_TLB_SUPER_PERM_MASK; 75 + 76 + /* 4K map for serial output. Used by kernel wrapper. */ 77 + tlbe = get_entry(vcpu_e500, 1, 1); 78 + tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOK3E_PAGESZ_4K); 79 + tlbe->mas2 = (0xe0004500 & 0xFFFFF000) | MAS2_I | MAS2_G; 80 + tlbe->mas7_3 = (0xe0004500 & 0xFFFFF000) | E500_TLB_SUPER_PERM_MASK; 81 + } 82 + 338 83 int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu) 339 84 { 340 85 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); ··· 363 74 vcpu_e500->svr = mfspr(SPRN_SVR); 364 75 365 76 vcpu->arch.cpu_type = KVM_CPU_E500V2; 366 - 367 - return 0; 368 - } 369 - 370 - /* 'linear_address' is actually an encoding of AS|PID|EADDR . */ 371 - int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu, 372 - struct kvm_translation *tr) 373 - { 374 - int index; 375 - gva_t eaddr; 376 - u8 pid; 377 - u8 as; 378 - 379 - eaddr = tr->linear_address; 380 - pid = (tr->linear_address >> 32) & 0xff; 381 - as = (tr->linear_address >> 40) & 0x1; 382 - 383 - index = kvmppc_e500_tlb_search(vcpu, eaddr, pid, as); 384 - if (index < 0) { 385 - tr->valid = 0; 386 - return 0; 387 - } 388 - 389 - tr->physical_address = kvmppc_mmu_xlate(vcpu, index, eaddr); 390 - /* XXX what does "writeable" and "usermode" even mean? */ 391 - tr->valid = 1; 392 77 393 78 return 0; 394 79 } ··· 380 117 sregs->u.e.impl.fsl.hid0 = vcpu_e500->hid0; 381 118 sregs->u.e.impl.fsl.mcar = vcpu_e500->mcar; 382 119 383 - sregs->u.e.mas0 = vcpu->arch.shared->mas0; 384 - sregs->u.e.mas1 = vcpu->arch.shared->mas1; 385 - sregs->u.e.mas2 = vcpu->arch.shared->mas2; 386 - sregs->u.e.mas7_3 = vcpu->arch.shared->mas7_3; 387 - sregs->u.e.mas4 = vcpu->arch.shared->mas4; 388 - sregs->u.e.mas6 = vcpu->arch.shared->mas6; 389 - 390 - sregs->u.e.mmucfg = mfspr(SPRN_MMUCFG); 391 - sregs->u.e.tlbcfg[0] = vcpu_e500->tlb0cfg; 392 - sregs->u.e.tlbcfg[1] = vcpu_e500->tlb1cfg; 393 - sregs->u.e.tlbcfg[2] = 0; 394 - sregs->u.e.tlbcfg[3] = 0; 395 - 396 120 sregs->u.e.ivor_high[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_UNAVAIL]; 397 121 sregs->u.e.ivor_high[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_DATA]; 398 122 sregs->u.e.ivor_high[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_ROUND]; ··· 387 137 vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR]; 388 138 389 139 kvmppc_get_sregs_ivor(vcpu, sregs); 140 + kvmppc_get_sregs_e500_tlb(vcpu, sregs); 390 141 } 391 142 392 143 int kvmppc_core_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) 393 144 { 394 145 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); 146 + int ret; 395 147 396 148 if (sregs->u.e.impl_id == KVM_SREGS_E_IMPL_FSL) { 397 149 vcpu_e500->svr = sregs->u.e.impl.fsl.svr; ··· 401 149 vcpu_e500->mcar = sregs->u.e.impl.fsl.mcar; 402 150 } 403 151 404 - if (sregs->u.e.features & KVM_SREGS_E_ARCH206_MMU) { 405 - vcpu->arch.shared->mas0 = sregs->u.e.mas0; 406 - vcpu->arch.shared->mas1 = sregs->u.e.mas1; 407 - vcpu->arch.shared->mas2 = sregs->u.e.mas2; 408 - vcpu->arch.shared->mas7_3 = sregs->u.e.mas7_3; 409 - vcpu->arch.shared->mas4 = sregs->u.e.mas4; 410 - vcpu->arch.shared->mas6 = sregs->u.e.mas6; 411 - } 152 + ret = kvmppc_set_sregs_e500_tlb(vcpu, sregs); 153 + if (ret < 0) 154 + return ret; 412 155 413 156 if (!(sregs->u.e.features & KVM_SREGS_E_IVOR)) 414 157 return 0; ··· 442 195 if (err) 443 196 goto free_vcpu; 444 197 198 + if (kvmppc_e500_id_table_alloc(vcpu_e500) == NULL) 199 + goto uninit_vcpu; 200 + 445 201 err = kvmppc_e500_tlb_init(vcpu_e500); 446 202 if (err) 447 - goto uninit_vcpu; 203 + goto uninit_id; 448 204 449 205 vcpu->arch.shared = (void*)__get_free_page(GFP_KERNEL|__GFP_ZERO); 450 206 if (!vcpu->arch.shared) ··· 457 207 458 208 uninit_tlb: 459 209 kvmppc_e500_tlb_uninit(vcpu_e500); 210 + uninit_id: 211 + kvmppc_e500_id_table_free(vcpu_e500); 460 212 uninit_vcpu: 461 213 kvm_vcpu_uninit(vcpu); 462 214 free_vcpu: ··· 472 220 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); 473 221 474 222 free_page((unsigned long)vcpu->arch.shared); 475 - kvm_vcpu_uninit(vcpu); 476 223 kvmppc_e500_tlb_uninit(vcpu_e500); 224 + kvmppc_e500_id_table_free(vcpu_e500); 225 + kvm_vcpu_uninit(vcpu); 477 226 kmem_cache_free(kvm_vcpu_cache, vcpu_e500); 478 227 } 479 228
+49 -13
arch/powerpc/kvm/e500.h
··· 35 35 struct tlbe_ref ref; /* TLB0 only -- TLB1 uses tlb_refs */ 36 36 }; 37 37 38 + #ifdef CONFIG_KVM_E500 38 39 struct vcpu_id_table; 40 + #endif 39 41 40 42 struct kvmppc_e500_tlb_params { 41 43 int entries, ways, sets; ··· 72 70 struct tlbe_ref *tlb_refs[E500_TLB_NUM]; 73 71 unsigned int host_tlb1_nv; 74 72 75 - u32 host_pid[E500_PID_NUM]; 76 - u32 pid[E500_PID_NUM]; 77 73 u32 svr; 78 - 79 - /* vcpu id table */ 80 - struct vcpu_id_table *idt; 81 - 82 74 u32 l1csr0; 83 75 u32 l1csr1; 84 76 u32 hid0; 85 77 u32 hid1; 86 - u32 tlb0cfg; 87 - u32 tlb1cfg; 88 78 u64 mcar; 89 79 90 80 struct page **shared_tlb_pages; 91 81 int num_shared_tlb_pages; 82 + 83 + #ifdef CONFIG_KVM_E500 84 + u32 pid[E500_PID_NUM]; 85 + 86 + /* vcpu id table */ 87 + struct vcpu_id_table *idt; 88 + #endif 92 89 }; 93 90 94 91 static inline struct kvmppc_vcpu_e500 *to_e500(struct kvm_vcpu *vcpu) ··· 114 113 (MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3 \ 115 114 | E500_TLB_USER_PERM_MASK | E500_TLB_SUPER_PERM_MASK) 116 115 117 - extern void kvmppc_e500_tlb_put(struct kvm_vcpu *); 118 - extern void kvmppc_e500_tlb_load(struct kvm_vcpu *, int); 119 - extern void kvmppc_e500_tlb_setup(struct kvmppc_vcpu_e500 *); 120 - extern void kvmppc_e500_recalc_shadow_pid(struct kvmppc_vcpu_e500 *); 121 116 int kvmppc_e500_emul_mt_mmucsr0(struct kvmppc_vcpu_e500 *vcpu_e500, 122 117 ulong value); 123 118 int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *vcpu); 124 119 int kvmppc_e500_emul_tlbre(struct kvm_vcpu *vcpu); 125 120 int kvmppc_e500_emul_tlbivax(struct kvm_vcpu *vcpu, int ra, int rb); 126 121 int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *vcpu, int rb); 127 - int kvmppc_e500_tlb_search(struct kvm_vcpu *, gva_t, unsigned int, int); 128 122 int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *vcpu_e500); 129 123 void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *vcpu_e500); 130 124 131 125 void kvmppc_get_sregs_e500_tlb(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs); 132 126 int kvmppc_set_sregs_e500_tlb(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs); 127 + 128 + 129 + #ifdef CONFIG_KVM_E500 130 + unsigned int kvmppc_e500_get_sid(struct kvmppc_vcpu_e500 *vcpu_e500, 131 + unsigned int as, unsigned int gid, 132 + unsigned int pr, int avoid_recursion); 133 + #endif 133 134 134 135 /* TLB helper functions */ 135 136 static inline unsigned int ··· 184 181 get_tlb_iprot(const struct kvm_book3e_206_tlb_entry *tlbe) 185 182 { 186 183 return (tlbe->mas1 >> 30) & 0x1; 184 + } 185 + 186 + static inline unsigned int 187 + get_tlb_tsize(const struct kvm_book3e_206_tlb_entry *tlbe) 188 + { 189 + return (tlbe->mas1 & MAS1_TSIZE_MASK) >> MAS1_TSIZE_SHIFT; 187 190 } 188 191 189 192 static inline unsigned int get_cur_pid(struct kvm_vcpu *vcpu) ··· 256 247 257 248 return 1; 258 249 } 250 + 251 + static inline struct kvm_book3e_206_tlb_entry *get_entry( 252 + struct kvmppc_vcpu_e500 *vcpu_e500, int tlbsel, int entry) 253 + { 254 + int offset = vcpu_e500->gtlb_offset[tlbsel]; 255 + return &vcpu_e500->gtlb_arch[offset + entry]; 256 + } 257 + 258 + void kvmppc_e500_tlbil_one(struct kvmppc_vcpu_e500 *vcpu_e500, 259 + struct kvm_book3e_206_tlb_entry *gtlbe); 260 + void kvmppc_e500_tlbil_all(struct kvmppc_vcpu_e500 *vcpu_e500); 261 + 262 + #ifdef CONFIG_KVM_E500 263 + unsigned int kvmppc_e500_get_tlb_stid(struct kvm_vcpu *vcpu, 264 + struct kvm_book3e_206_tlb_entry *gtlbe); 265 + 266 + static inline unsigned int get_tlbmiss_tid(struct kvm_vcpu *vcpu) 267 + { 268 + struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); 269 + unsigned int tidseld = (vcpu->arch.shared->mas4 >> 16) & 0xf; 270 + 271 + return vcpu_e500->pid[tidseld]; 272 + } 273 + 274 + /* Force TS=1 for all guest mappings. */ 275 + #define get_tlb_sts(gtlbe) (MAS1_TS) 276 + #endif /* CONFIG_KVM_E500 */ 259 277 260 278 #endif /* KVM_E500_H */
+3 -3
arch/powerpc/kvm/e500_emulate.c
··· 174 174 kvmppc_set_gpr(vcpu, rt, val); 175 175 break; 176 176 case SPRN_TLB0CFG: 177 - kvmppc_set_gpr(vcpu, rt, vcpu_e500->tlb0cfg); break; 177 + kvmppc_set_gpr(vcpu, rt, vcpu->arch.tlbcfg[0]); break; 178 178 case SPRN_TLB1CFG: 179 - kvmppc_set_gpr(vcpu, rt, vcpu_e500->tlb1cfg); break; 179 + kvmppc_set_gpr(vcpu, rt, vcpu->arch.tlbcfg[1]); break; 180 180 case SPRN_L1CSR0: 181 181 kvmppc_set_gpr(vcpu, rt, vcpu_e500->l1csr0); break; 182 182 case SPRN_L1CSR1: ··· 192 192 kvmppc_set_gpr(vcpu, rt, 0); break; 193 193 194 194 case SPRN_MMUCFG: 195 - kvmppc_set_gpr(vcpu, rt, mfspr(SPRN_MMUCFG)); break; 195 + kvmppc_set_gpr(vcpu, rt, vcpu->arch.mmucfg); break; 196 196 197 197 /* extra exceptions */ 198 198 case SPRN_IVOR32:
+115 -347
arch/powerpc/kvm/e500_tlb.c
··· 27 27 #include <linux/hugetlb.h> 28 28 #include <asm/kvm_ppc.h> 29 29 30 - #include "../mm/mmu_decl.h" 31 30 #include "e500.h" 32 31 #include "trace.h" 33 32 #include "timing.h" 34 33 35 34 #define to_htlb1_esel(esel) (host_tlb_params[1].entries - (esel) - 1) 36 35 37 - struct id { 38 - unsigned long val; 39 - struct id **pentry; 40 - }; 41 - 42 - #define NUM_TIDS 256 43 - 44 - /* 45 - * This table provide mappings from: 46 - * (guestAS,guestTID,guestPR) --> ID of physical cpu 47 - * guestAS [0..1] 48 - * guestTID [0..255] 49 - * guestPR [0..1] 50 - * ID [1..255] 51 - * Each vcpu keeps one vcpu_id_table. 52 - */ 53 - struct vcpu_id_table { 54 - struct id id[2][NUM_TIDS][2]; 55 - }; 56 - 57 - /* 58 - * This table provide reversed mappings of vcpu_id_table: 59 - * ID --> address of vcpu_id_table item. 60 - * Each physical core has one pcpu_id_table. 61 - */ 62 - struct pcpu_id_table { 63 - struct id *entry[NUM_TIDS]; 64 - }; 65 - 66 - static DEFINE_PER_CPU(struct pcpu_id_table, pcpu_sids); 67 - 68 - /* This variable keeps last used shadow ID on local core. 69 - * The valid range of shadow ID is [1..255] */ 70 - static DEFINE_PER_CPU(unsigned long, pcpu_last_used_sid); 71 - 72 36 static struct kvmppc_e500_tlb_params host_tlb_params[E500_TLB_NUM]; 73 - 74 - static struct kvm_book3e_206_tlb_entry *get_entry( 75 - struct kvmppc_vcpu_e500 *vcpu_e500, int tlbsel, int entry) 76 - { 77 - int offset = vcpu_e500->gtlb_offset[tlbsel]; 78 - return &vcpu_e500->gtlb_arch[offset + entry]; 79 - } 80 - 81 - /* 82 - * Allocate a free shadow id and setup a valid sid mapping in given entry. 83 - * A mapping is only valid when vcpu_id_table and pcpu_id_table are match. 84 - * 85 - * The caller must have preemption disabled, and keep it that way until 86 - * it has finished with the returned shadow id (either written into the 87 - * TLB or arch.shadow_pid, or discarded). 88 - */ 89 - static inline int local_sid_setup_one(struct id *entry) 90 - { 91 - unsigned long sid; 92 - int ret = -1; 93 - 94 - sid = ++(__get_cpu_var(pcpu_last_used_sid)); 95 - if (sid < NUM_TIDS) { 96 - __get_cpu_var(pcpu_sids).entry[sid] = entry; 97 - entry->val = sid; 98 - entry->pentry = &__get_cpu_var(pcpu_sids).entry[sid]; 99 - ret = sid; 100 - } 101 - 102 - /* 103 - * If sid == NUM_TIDS, we've run out of sids. We return -1, and 104 - * the caller will invalidate everything and start over. 105 - * 106 - * sid > NUM_TIDS indicates a race, which we disable preemption to 107 - * avoid. 108 - */ 109 - WARN_ON(sid > NUM_TIDS); 110 - 111 - return ret; 112 - } 113 - 114 - /* 115 - * Check if given entry contain a valid shadow id mapping. 116 - * An ID mapping is considered valid only if 117 - * both vcpu and pcpu know this mapping. 118 - * 119 - * The caller must have preemption disabled, and keep it that way until 120 - * it has finished with the returned shadow id (either written into the 121 - * TLB or arch.shadow_pid, or discarded). 122 - */ 123 - static inline int local_sid_lookup(struct id *entry) 124 - { 125 - if (entry && entry->val != 0 && 126 - __get_cpu_var(pcpu_sids).entry[entry->val] == entry && 127 - entry->pentry == &__get_cpu_var(pcpu_sids).entry[entry->val]) 128 - return entry->val; 129 - return -1; 130 - } 131 - 132 - /* Invalidate all id mappings on local core -- call with preempt disabled */ 133 - static inline void local_sid_destroy_all(void) 134 - { 135 - __get_cpu_var(pcpu_last_used_sid) = 0; 136 - memset(&__get_cpu_var(pcpu_sids), 0, sizeof(__get_cpu_var(pcpu_sids))); 137 - } 138 - 139 - static void *kvmppc_e500_id_table_alloc(struct kvmppc_vcpu_e500 *vcpu_e500) 140 - { 141 - vcpu_e500->idt = kzalloc(sizeof(struct vcpu_id_table), GFP_KERNEL); 142 - return vcpu_e500->idt; 143 - } 144 - 145 - static void kvmppc_e500_id_table_free(struct kvmppc_vcpu_e500 *vcpu_e500) 146 - { 147 - kfree(vcpu_e500->idt); 148 - } 149 - 150 - /* Invalidate all mappings on vcpu */ 151 - static void kvmppc_e500_id_table_reset_all(struct kvmppc_vcpu_e500 *vcpu_e500) 152 - { 153 - memset(vcpu_e500->idt, 0, sizeof(struct vcpu_id_table)); 154 - 155 - /* Update shadow pid when mappings are changed */ 156 - kvmppc_e500_recalc_shadow_pid(vcpu_e500); 157 - } 158 - 159 - /* Invalidate one ID mapping on vcpu */ 160 - static inline void kvmppc_e500_id_table_reset_one( 161 - struct kvmppc_vcpu_e500 *vcpu_e500, 162 - int as, int pid, int pr) 163 - { 164 - struct vcpu_id_table *idt = vcpu_e500->idt; 165 - 166 - BUG_ON(as >= 2); 167 - BUG_ON(pid >= NUM_TIDS); 168 - BUG_ON(pr >= 2); 169 - 170 - idt->id[as][pid][pr].val = 0; 171 - idt->id[as][pid][pr].pentry = NULL; 172 - 173 - /* Update shadow pid when mappings are changed */ 174 - kvmppc_e500_recalc_shadow_pid(vcpu_e500); 175 - } 176 - 177 - /* 178 - * Map guest (vcpu,AS,ID,PR) to physical core shadow id. 179 - * This function first lookup if a valid mapping exists, 180 - * if not, then creates a new one. 181 - * 182 - * The caller must have preemption disabled, and keep it that way until 183 - * it has finished with the returned shadow id (either written into the 184 - * TLB or arch.shadow_pid, or discarded). 185 - */ 186 - static unsigned int kvmppc_e500_get_sid(struct kvmppc_vcpu_e500 *vcpu_e500, 187 - unsigned int as, unsigned int gid, 188 - unsigned int pr, int avoid_recursion) 189 - { 190 - struct vcpu_id_table *idt = vcpu_e500->idt; 191 - int sid; 192 - 193 - BUG_ON(as >= 2); 194 - BUG_ON(gid >= NUM_TIDS); 195 - BUG_ON(pr >= 2); 196 - 197 - sid = local_sid_lookup(&idt->id[as][gid][pr]); 198 - 199 - while (sid <= 0) { 200 - /* No mapping yet */ 201 - sid = local_sid_setup_one(&idt->id[as][gid][pr]); 202 - if (sid <= 0) { 203 - _tlbil_all(); 204 - local_sid_destroy_all(); 205 - } 206 - 207 - /* Update shadow pid when mappings are changed */ 208 - if (!avoid_recursion) 209 - kvmppc_e500_recalc_shadow_pid(vcpu_e500); 210 - } 211 - 212 - return sid; 213 - } 214 - 215 - /* Map guest pid to shadow. 216 - * We use PID to keep shadow of current guest non-zero PID, 217 - * and use PID1 to keep shadow of guest zero PID. 218 - * So that guest tlbe with TID=0 can be accessed at any time */ 219 - void kvmppc_e500_recalc_shadow_pid(struct kvmppc_vcpu_e500 *vcpu_e500) 220 - { 221 - preempt_disable(); 222 - vcpu_e500->vcpu.arch.shadow_pid = kvmppc_e500_get_sid(vcpu_e500, 223 - get_cur_as(&vcpu_e500->vcpu), 224 - get_cur_pid(&vcpu_e500->vcpu), 225 - get_cur_pr(&vcpu_e500->vcpu), 1); 226 - vcpu_e500->vcpu.arch.shadow_pid1 = kvmppc_e500_get_sid(vcpu_e500, 227 - get_cur_as(&vcpu_e500->vcpu), 0, 228 - get_cur_pr(&vcpu_e500->vcpu), 1); 229 - preempt_enable(); 230 - } 231 37 232 38 static inline unsigned int gtlb0_get_next_victim( 233 39 struct kvmppc_vcpu_e500 *vcpu_e500) ··· 142 336 } 143 337 } 144 338 339 + #ifdef CONFIG_KVM_E500 145 340 void kvmppc_map_magic(struct kvm_vcpu *vcpu) 146 341 { 147 342 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); ··· 167 360 __write_host_tlbe(&magic, MAS0_TLBSEL(1) | MAS0_ESEL(tlbcam_index)); 168 361 preempt_enable(); 169 362 } 170 - 171 - void kvmppc_e500_tlb_load(struct kvm_vcpu *vcpu, int cpu) 172 - { 173 - struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); 174 - 175 - /* Shadow PID may be expired on local core */ 176 - kvmppc_e500_recalc_shadow_pid(vcpu_e500); 177 - } 178 - 179 - void kvmppc_e500_tlb_put(struct kvm_vcpu *vcpu) 180 - { 181 - } 363 + #endif 182 364 183 365 static void inval_gtlbe_on_host(struct kvmppc_vcpu_e500 *vcpu_e500, 184 366 int tlbsel, int esel) 185 367 { 186 368 struct kvm_book3e_206_tlb_entry *gtlbe = 187 369 get_entry(vcpu_e500, tlbsel, esel); 188 - struct vcpu_id_table *idt = vcpu_e500->idt; 189 - unsigned int pr, tid, ts, pid; 190 - u32 val, eaddr; 191 - unsigned long flags; 192 370 193 - ts = get_tlb_ts(gtlbe); 194 - tid = get_tlb_tid(gtlbe); 195 - 196 - preempt_disable(); 197 - 198 - /* One guest ID may be mapped to two shadow IDs */ 199 - for (pr = 0; pr < 2; pr++) { 200 - /* 201 - * The shadow PID can have a valid mapping on at most one 202 - * host CPU. In the common case, it will be valid on this 203 - * CPU, in which case (for TLB0) we do a local invalidation 204 - * of the specific address. 205 - * 206 - * If the shadow PID is not valid on the current host CPU, or 207 - * if we're invalidating a TLB1 entry, we invalidate the 208 - * entire shadow PID. 209 - */ 210 - if (tlbsel == 1 || 211 - (pid = local_sid_lookup(&idt->id[ts][tid][pr])) <= 0) { 212 - kvmppc_e500_id_table_reset_one(vcpu_e500, ts, tid, pr); 213 - continue; 214 - } 215 - 216 - /* 217 - * The guest is invalidating a TLB0 entry which is in a PID 218 - * that has a valid shadow mapping on this host CPU. We 219 - * search host TLB0 to invalidate it's shadow TLB entry, 220 - * similar to __tlbil_va except that we need to look in AS1. 221 - */ 222 - val = (pid << MAS6_SPID_SHIFT) | MAS6_SAS; 223 - eaddr = get_tlb_eaddr(gtlbe); 224 - 225 - local_irq_save(flags); 226 - 227 - mtspr(SPRN_MAS6, val); 228 - asm volatile("tlbsx 0, %[eaddr]" : : [eaddr] "r" (eaddr)); 229 - val = mfspr(SPRN_MAS1); 230 - if (val & MAS1_VALID) { 231 - mtspr(SPRN_MAS1, val & ~MAS1_VALID); 232 - asm volatile("tlbwe"); 233 - } 234 - 235 - local_irq_restore(flags); 371 + if (tlbsel == 1) { 372 + kvmppc_e500_tlbil_all(vcpu_e500); 373 + return; 236 374 } 237 375 238 - preempt_enable(); 376 + /* Guest tlbe is backed by at most one host tlbe per shadow pid. */ 377 + kvmppc_e500_tlbil_one(vcpu_e500, gtlbe); 239 378 } 240 379 241 380 static int tlb0_set_base(gva_t addr, int sets, int ways) ··· 299 546 int stlbsel = 1; 300 547 int i; 301 548 302 - kvmppc_e500_id_table_reset_all(vcpu_e500); 549 + kvmppc_e500_tlbil_all(vcpu_e500); 303 550 304 551 for (i = 0; i < host_tlb_params[stlbsel].entries; i++) { 305 552 struct tlbe_ref *ref = ··· 314 561 unsigned int eaddr, int as) 315 562 { 316 563 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); 317 - unsigned int victim, pidsel, tsized; 564 + unsigned int victim, tsized; 318 565 int tlbsel; 319 566 320 567 /* since we only have two TLBs, only lower bit is used. */ 321 568 tlbsel = (vcpu->arch.shared->mas4 >> 28) & 0x1; 322 569 victim = (tlbsel == 0) ? gtlb0_get_next_victim(vcpu_e500) : 0; 323 - pidsel = (vcpu->arch.shared->mas4 >> 16) & 0xf; 324 570 tsized = (vcpu->arch.shared->mas4 >> 7) & 0x1f; 325 571 326 572 vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim) 327 573 | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]); 328 574 vcpu->arch.shared->mas1 = MAS1_VALID | (as ? MAS1_TS : 0) 329 - | MAS1_TID(vcpu_e500->pid[pidsel]) 575 + | MAS1_TID(get_tlbmiss_tid(vcpu)) 330 576 | MAS1_TSIZE(tsized); 331 577 vcpu->arch.shared->mas2 = (eaddr & MAS2_EPN) 332 578 | (vcpu->arch.shared->mas4 & MAS2_ATTRIB_MASK); ··· 337 585 338 586 /* TID must be supplied by the caller */ 339 587 static inline void kvmppc_e500_setup_stlbe( 340 - struct kvmppc_vcpu_e500 *vcpu_e500, 588 + struct kvm_vcpu *vcpu, 341 589 struct kvm_book3e_206_tlb_entry *gtlbe, 342 590 int tsize, struct tlbe_ref *ref, u64 gvaddr, 343 591 struct kvm_book3e_206_tlb_entry *stlbe) 344 592 { 345 593 pfn_t pfn = ref->pfn; 594 + u32 pr = vcpu->arch.shared->msr & MSR_PR; 346 595 347 596 BUG_ON(!(ref->flags & E500_TLB_VALID)); 348 597 349 - /* Force TS=1 IPROT=0 for all guest mappings. */ 350 - stlbe->mas1 = MAS1_TSIZE(tsize) | MAS1_TS | MAS1_VALID; 351 - stlbe->mas2 = (gvaddr & MAS2_EPN) 352 - | e500_shadow_mas2_attrib(gtlbe->mas2, 353 - vcpu_e500->vcpu.arch.shared->msr & MSR_PR); 354 - stlbe->mas7_3 = ((u64)pfn << PAGE_SHIFT) 355 - | e500_shadow_mas3_attrib(gtlbe->mas7_3, 356 - vcpu_e500->vcpu.arch.shared->msr & MSR_PR); 598 + /* Force IPROT=0 for all guest mappings. */ 599 + stlbe->mas1 = MAS1_TSIZE(tsize) | get_tlb_sts(gtlbe) | MAS1_VALID; 600 + stlbe->mas2 = (gvaddr & MAS2_EPN) | 601 + e500_shadow_mas2_attrib(gtlbe->mas2, pr); 602 + stlbe->mas7_3 = ((u64)pfn << PAGE_SHIFT) | 603 + e500_shadow_mas3_attrib(gtlbe->mas7_3, pr); 357 604 } 358 605 359 606 static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500, ··· 486 735 kvmppc_e500_ref_release(ref); 487 736 kvmppc_e500_ref_setup(ref, gtlbe, pfn); 488 737 489 - kvmppc_e500_setup_stlbe(vcpu_e500, gtlbe, tsize, ref, gvaddr, stlbe); 738 + kvmppc_e500_setup_stlbe(&vcpu_e500->vcpu, gtlbe, tsize, 739 + ref, gvaddr, stlbe); 490 740 } 491 741 492 742 /* XXX only map the one-one case, for now use TLB0 */ ··· 527 775 return victim; 528 776 } 529 777 530 - void kvmppc_mmu_msr_notify(struct kvm_vcpu *vcpu, u32 old_msr) 531 - { 532 - struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); 533 - 534 - /* Recalc shadow pid since MSR changes */ 535 - kvmppc_e500_recalc_shadow_pid(vcpu_e500); 536 - } 537 - 538 778 static inline int kvmppc_e500_gtlbe_invalidate( 539 779 struct kvmppc_vcpu_e500 *vcpu_e500, 540 780 int tlbsel, int esel) ··· 554 810 kvmppc_e500_gtlbe_invalidate(vcpu_e500, 1, esel); 555 811 556 812 /* Invalidate all vcpu id mappings */ 557 - kvmppc_e500_id_table_reset_all(vcpu_e500); 813 + kvmppc_e500_tlbil_all(vcpu_e500); 558 814 559 815 return EMULATE_DONE; 560 816 } ··· 587 843 } 588 844 589 845 /* Invalidate all vcpu id mappings */ 590 - kvmppc_e500_id_table_reset_all(vcpu_e500); 846 + kvmppc_e500_tlbil_all(vcpu_e500); 591 847 592 848 return EMULATE_DONE; 593 849 } ··· 672 928 int stid; 673 929 674 930 preempt_disable(); 675 - stid = kvmppc_e500_get_sid(vcpu_e500, get_tlb_ts(gtlbe), 676 - get_tlb_tid(gtlbe), 677 - get_cur_pr(&vcpu_e500->vcpu), 0); 931 + stid = kvmppc_e500_get_tlb_stid(&vcpu_e500->vcpu, gtlbe); 678 932 679 933 stlbe->mas1 |= MAS1_TID(stid); 680 934 write_host_tlbe(vcpu_e500, stlbsel, sesel, stlbe); ··· 682 940 int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *vcpu) 683 941 { 684 942 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); 685 - struct kvm_book3e_206_tlb_entry *gtlbe; 686 - int tlbsel, esel; 943 + struct kvm_book3e_206_tlb_entry *gtlbe, stlbe; 944 + int tlbsel, esel, stlbsel, sesel; 687 945 688 946 tlbsel = get_tlb_tlbsel(vcpu); 689 947 esel = get_tlb_esel(vcpu, tlbsel); ··· 702 960 703 961 /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */ 704 962 if (tlbe_is_host_safe(vcpu, gtlbe)) { 705 - struct kvm_book3e_206_tlb_entry stlbe; 706 - int stlbsel, sesel; 707 963 u64 eaddr; 708 964 u64 raddr; 709 965 ··· 728 988 * are mapped on the fly. */ 729 989 stlbsel = 1; 730 990 sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr, 731 - raddr >> PAGE_SHIFT, gtlbe, &stlbe); 991 + raddr >> PAGE_SHIFT, gtlbe, &stlbe); 732 992 break; 733 993 734 994 default: ··· 741 1001 kvmppc_set_exit_type(vcpu, EMULATED_TLBWE_EXITS); 742 1002 return EMULATE_DONE; 743 1003 } 1004 + 1005 + static int kvmppc_e500_tlb_search(struct kvm_vcpu *vcpu, 1006 + gva_t eaddr, unsigned int pid, int as) 1007 + { 1008 + struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); 1009 + int esel, tlbsel; 1010 + 1011 + for (tlbsel = 0; tlbsel < 2; tlbsel++) { 1012 + esel = kvmppc_e500_tlb_index(vcpu_e500, eaddr, tlbsel, pid, as); 1013 + if (esel >= 0) 1014 + return index_of(tlbsel, esel); 1015 + } 1016 + 1017 + return -1; 1018 + } 1019 + 1020 + /* 'linear_address' is actually an encoding of AS|PID|EADDR . */ 1021 + int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu, 1022 + struct kvm_translation *tr) 1023 + { 1024 + int index; 1025 + gva_t eaddr; 1026 + u8 pid; 1027 + u8 as; 1028 + 1029 + eaddr = tr->linear_address; 1030 + pid = (tr->linear_address >> 32) & 0xff; 1031 + as = (tr->linear_address >> 40) & 0x1; 1032 + 1033 + index = kvmppc_e500_tlb_search(vcpu, eaddr, pid, as); 1034 + if (index < 0) { 1035 + tr->valid = 0; 1036 + return 0; 1037 + } 1038 + 1039 + tr->physical_address = kvmppc_mmu_xlate(vcpu, index, eaddr); 1040 + /* XXX what does "writeable" and "usermode" even mean? */ 1041 + tr->valid = 1; 1042 + 1043 + return 0; 1044 + } 1045 + 744 1046 745 1047 int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr) 746 1048 { ··· 847 1065 sesel = 0; /* unused */ 848 1066 priv = &vcpu_e500->gtlb_priv[tlbsel][esel]; 849 1067 850 - kvmppc_e500_setup_stlbe(vcpu_e500, gtlbe, BOOK3E_PAGESZ_4K, 1068 + kvmppc_e500_setup_stlbe(vcpu, gtlbe, BOOK3E_PAGESZ_4K, 851 1069 &priv->ref, eaddr, &stlbe); 852 1070 break; 853 1071 ··· 866 1084 } 867 1085 868 1086 write_stlbe(vcpu_e500, gtlbe, &stlbe, stlbsel, sesel); 869 - } 870 - 871 - int kvmppc_e500_tlb_search(struct kvm_vcpu *vcpu, 872 - gva_t eaddr, unsigned int pid, int as) 873 - { 874 - struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); 875 - int esel, tlbsel; 876 - 877 - for (tlbsel = 0; tlbsel < 2; tlbsel++) { 878 - esel = kvmppc_e500_tlb_index(vcpu_e500, eaddr, tlbsel, pid, as); 879 - if (esel >= 0) 880 - return index_of(tlbsel, esel); 881 - } 882 - 883 - return -1; 884 - } 885 - 886 - void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid) 887 - { 888 - struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); 889 - 890 - if (vcpu->arch.pid != pid) { 891 - vcpu_e500->pid[0] = vcpu->arch.pid = pid; 892 - kvmppc_e500_recalc_shadow_pid(vcpu_e500); 893 - } 894 - } 895 - 896 - void kvmppc_e500_tlb_setup(struct kvmppc_vcpu_e500 *vcpu_e500) 897 - { 898 - struct kvm_book3e_206_tlb_entry *tlbe; 899 - 900 - /* Insert large initial mapping for guest. */ 901 - tlbe = get_entry(vcpu_e500, 1, 0); 902 - tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOK3E_PAGESZ_256M); 903 - tlbe->mas2 = 0; 904 - tlbe->mas7_3 = E500_TLB_SUPER_PERM_MASK; 905 - 906 - /* 4K map for serial output. Used by kernel wrapper. */ 907 - tlbe = get_entry(vcpu_e500, 1, 1); 908 - tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOK3E_PAGESZ_4K); 909 - tlbe->mas2 = (0xe0004500 & 0xFFFFF000) | MAS2_I | MAS2_G; 910 - tlbe->mas7_3 = (0xe0004500 & 0xFFFFF000) | E500_TLB_SUPER_PERM_MASK; 911 1087 } 912 1088 913 1089 static void free_gtlb(struct kvmppc_vcpu_e500 *vcpu_e500) ··· 892 1152 } 893 1153 894 1154 vcpu_e500->gtlb_arch = NULL; 1155 + } 1156 + 1157 + void kvmppc_get_sregs_e500_tlb(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) 1158 + { 1159 + sregs->u.e.mas0 = vcpu->arch.shared->mas0; 1160 + sregs->u.e.mas1 = vcpu->arch.shared->mas1; 1161 + sregs->u.e.mas2 = vcpu->arch.shared->mas2; 1162 + sregs->u.e.mas7_3 = vcpu->arch.shared->mas7_3; 1163 + sregs->u.e.mas4 = vcpu->arch.shared->mas4; 1164 + sregs->u.e.mas6 = vcpu->arch.shared->mas6; 1165 + 1166 + sregs->u.e.mmucfg = vcpu->arch.mmucfg; 1167 + sregs->u.e.tlbcfg[0] = vcpu->arch.tlbcfg[0]; 1168 + sregs->u.e.tlbcfg[1] = vcpu->arch.tlbcfg[1]; 1169 + sregs->u.e.tlbcfg[2] = 0; 1170 + sregs->u.e.tlbcfg[3] = 0; 1171 + } 1172 + 1173 + int kvmppc_set_sregs_e500_tlb(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) 1174 + { 1175 + if (sregs->u.e.features & KVM_SREGS_E_ARCH206_MMU) { 1176 + vcpu->arch.shared->mas0 = sregs->u.e.mas0; 1177 + vcpu->arch.shared->mas1 = sregs->u.e.mas1; 1178 + vcpu->arch.shared->mas2 = sregs->u.e.mas2; 1179 + vcpu->arch.shared->mas7_3 = sregs->u.e.mas7_3; 1180 + vcpu->arch.shared->mas4 = sregs->u.e.mas4; 1181 + vcpu->arch.shared->mas6 = sregs->u.e.mas6; 1182 + } 1183 + 1184 + return 0; 895 1185 } 896 1186 897 1187 int kvm_vcpu_ioctl_config_tlb(struct kvm_vcpu *vcpu, ··· 1007 1237 vcpu_e500->gtlb_offset[0] = 0; 1008 1238 vcpu_e500->gtlb_offset[1] = params.tlb_sizes[0]; 1009 1239 1010 - vcpu_e500->tlb0cfg &= ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC); 1011 - if (params.tlb_sizes[0] <= 2048) 1012 - vcpu_e500->tlb0cfg |= params.tlb_sizes[0]; 1013 - vcpu_e500->tlb0cfg |= params.tlb_ways[0] << TLBnCFG_ASSOC_SHIFT; 1240 + vcpu->arch.mmucfg = mfspr(SPRN_MMUCFG) & ~MMUCFG_LPIDSIZE; 1014 1241 1015 - vcpu_e500->tlb1cfg &= ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC); 1016 - vcpu_e500->tlb1cfg |= params.tlb_sizes[1]; 1017 - vcpu_e500->tlb1cfg |= params.tlb_ways[1] << TLBnCFG_ASSOC_SHIFT; 1242 + vcpu->arch.tlbcfg[0] &= ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC); 1243 + if (params.tlb_sizes[0] <= 2048) 1244 + vcpu->arch.tlbcfg[0] |= params.tlb_sizes[0]; 1245 + vcpu->arch.tlbcfg[0] |= params.tlb_ways[0] << TLBnCFG_ASSOC_SHIFT; 1246 + 1247 + vcpu->arch.tlbcfg[1] &= ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC); 1248 + vcpu->arch.tlbcfg[1] |= params.tlb_sizes[1]; 1249 + vcpu->arch.tlbcfg[1] |= params.tlb_ways[1] << TLBnCFG_ASSOC_SHIFT; 1018 1250 1019 1251 vcpu_e500->shared_tlb_pages = pages; 1020 1252 vcpu_e500->num_shared_tlb_pages = num_pages; ··· 1052 1280 1053 1281 int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *vcpu_e500) 1054 1282 { 1283 + struct kvm_vcpu *vcpu = &vcpu_e500->vcpu; 1055 1284 int entry_size = sizeof(struct kvm_book3e_206_tlb_entry); 1056 1285 int entries = KVM_E500_TLB0_SIZE + KVM_E500_TLB1_SIZE; 1057 1286 ··· 1129 1356 if (!vcpu_e500->gtlb_priv[1]) 1130 1357 goto err; 1131 1358 1132 - if (kvmppc_e500_id_table_alloc(vcpu_e500) == NULL) 1133 - goto err; 1134 - 1135 1359 /* Init TLB configuration register */ 1136 - vcpu_e500->tlb0cfg = mfspr(SPRN_TLB0CFG) & 1360 + vcpu->arch.tlbcfg[0] = mfspr(SPRN_TLB0CFG) & 1137 1361 ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC); 1138 - vcpu_e500->tlb0cfg |= vcpu_e500->gtlb_params[0].entries; 1139 - vcpu_e500->tlb0cfg |= 1362 + vcpu->arch.tlbcfg[0] |= vcpu_e500->gtlb_params[0].entries; 1363 + vcpu->arch.tlbcfg[0] |= 1140 1364 vcpu_e500->gtlb_params[0].ways << TLBnCFG_ASSOC_SHIFT; 1141 1365 1142 - vcpu_e500->tlb1cfg = mfspr(SPRN_TLB1CFG) & 1366 + vcpu->arch.tlbcfg[1] = mfspr(SPRN_TLB1CFG) & 1143 1367 ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC); 1144 - vcpu_e500->tlb0cfg |= vcpu_e500->gtlb_params[1].entries; 1145 - vcpu_e500->tlb0cfg |= 1368 + vcpu->arch.tlbcfg[0] |= vcpu_e500->gtlb_params[1].entries; 1369 + vcpu->arch.tlbcfg[0] |= 1146 1370 vcpu_e500->gtlb_params[1].ways << TLBnCFG_ASSOC_SHIFT; 1147 1371 1148 1372 return 0; ··· 1154 1384 void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *vcpu_e500) 1155 1385 { 1156 1386 free_gtlb(vcpu_e500); 1157 - kvmppc_e500_id_table_free(vcpu_e500); 1158 - 1159 1387 kfree(vcpu_e500->tlb_refs[0]); 1160 1388 kfree(vcpu_e500->tlb_refs[1]); 1161 1389 }