Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 1 | /* |
Scott Wood | 49ea069 | 2011-03-28 15:01:24 -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 based on arch/powerpc/kvm/44x_tlb.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/types.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/string.h> |
| 18 | #include <linux/kvm.h> |
| 19 | #include <linux/kvm_host.h> |
| 20 | #include <linux/highmem.h> |
| 21 | #include <asm/kvm_ppc.h> |
| 22 | #include <asm/kvm_e500.h> |
| 23 | |
Liu Yu | 9aa4dd5 | 2009-01-14 10:47:38 -0600 | [diff] [blame] | 24 | #include "../mm/mmu_decl.h" |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 25 | #include "e500_tlb.h" |
Marcelo Tosatti | 46f43c6 | 2009-06-18 11:47:27 -0300 | [diff] [blame] | 26 | #include "trace.h" |
Scott Wood | 49ea069 | 2011-03-28 15:01:24 -0500 | [diff] [blame] | 27 | #include "timing.h" |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 28 | |
| 29 | #define to_htlb1_esel(esel) (tlb1_entry_num - (esel) - 1) |
| 30 | |
| 31 | static unsigned int tlb1_entry_num; |
| 32 | |
| 33 | void kvmppc_dump_tlbs(struct kvm_vcpu *vcpu) |
| 34 | { |
| 35 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
| 36 | struct tlbe *tlbe; |
| 37 | int i, tlbsel; |
| 38 | |
| 39 | printk("| %8s | %8s | %8s | %8s | %8s |\n", |
| 40 | "nr", "mas1", "mas2", "mas3", "mas7"); |
| 41 | |
| 42 | for (tlbsel = 0; tlbsel < 2; tlbsel++) { |
| 43 | printk("Guest TLB%d:\n", tlbsel); |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 44 | for (i = 0; i < vcpu_e500->gtlb_size[tlbsel]; i++) { |
| 45 | tlbe = &vcpu_e500->gtlb_arch[tlbsel][i]; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 46 | if (tlbe->mas1 & MAS1_VALID) |
| 47 | printk(" G[%d][%3d] | %08X | %08X | %08X | %08X |\n", |
| 48 | tlbsel, i, tlbe->mas1, tlbe->mas2, |
| 49 | tlbe->mas3, tlbe->mas7); |
| 50 | } |
| 51 | } |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | static inline unsigned int tlb0_get_next_victim( |
| 55 | struct kvmppc_vcpu_e500 *vcpu_e500) |
| 56 | { |
| 57 | unsigned int victim; |
| 58 | |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 59 | victim = vcpu_e500->gtlb_nv[0]++; |
| 60 | if (unlikely(vcpu_e500->gtlb_nv[0] >= KVM_E500_TLB0_WAY_NUM)) |
| 61 | vcpu_e500->gtlb_nv[0] = 0; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 62 | |
| 63 | return victim; |
| 64 | } |
| 65 | |
| 66 | static inline unsigned int tlb1_max_shadow_size(void) |
| 67 | { |
Scott Wood | a4cd8b2 | 2011-06-14 18:34:41 -0500 | [diff] [blame] | 68 | /* reserve one entry for magic page */ |
| 69 | return tlb1_entry_num - tlbcam_index - 1; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static inline int tlbe_is_writable(struct tlbe *tlbe) |
| 73 | { |
| 74 | return tlbe->mas3 & (MAS3_SW|MAS3_UW); |
| 75 | } |
| 76 | |
| 77 | static inline u32 e500_shadow_mas3_attrib(u32 mas3, int usermode) |
| 78 | { |
| 79 | /* Mask off reserved bits. */ |
| 80 | mas3 &= MAS3_ATTRIB_MASK; |
| 81 | |
| 82 | if (!usermode) { |
| 83 | /* Guest is in supervisor mode, |
| 84 | * so we need to translate guest |
| 85 | * supervisor permissions into user permissions. */ |
| 86 | mas3 &= ~E500_TLB_USER_PERM_MASK; |
| 87 | mas3 |= (mas3 & E500_TLB_SUPER_PERM_MASK) << 1; |
| 88 | } |
| 89 | |
| 90 | return mas3 | E500_TLB_SUPER_PERM_MASK; |
| 91 | } |
| 92 | |
| 93 | static inline u32 e500_shadow_mas2_attrib(u32 mas2, int usermode) |
| 94 | { |
Liu Yu | 046a48b | 2009-03-17 16:57:46 +0800 | [diff] [blame] | 95 | #ifdef CONFIG_SMP |
| 96 | return (mas2 & MAS2_ATTRIB_MASK) | MAS2_M; |
| 97 | #else |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 98 | return mas2 & MAS2_ATTRIB_MASK; |
Liu Yu | 046a48b | 2009-03-17 16:57:46 +0800 | [diff] [blame] | 99 | #endif |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /* |
| 103 | * writing shadow tlb entry to host TLB |
| 104 | */ |
Scott Wood | 0ef309956 | 2011-06-14 18:34:35 -0500 | [diff] [blame] | 105 | static inline void __write_host_tlbe(struct tlbe *stlbe, uint32_t mas0) |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 106 | { |
Scott Wood | 0ef309956 | 2011-06-14 18:34:35 -0500 | [diff] [blame] | 107 | unsigned long flags; |
| 108 | |
| 109 | local_irq_save(flags); |
| 110 | mtspr(SPRN_MAS0, mas0); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 111 | mtspr(SPRN_MAS1, stlbe->mas1); |
| 112 | mtspr(SPRN_MAS2, stlbe->mas2); |
| 113 | mtspr(SPRN_MAS3, stlbe->mas3); |
| 114 | mtspr(SPRN_MAS7, stlbe->mas7); |
Scott Wood | 0ef309956 | 2011-06-14 18:34:35 -0500 | [diff] [blame] | 115 | asm volatile("isync; tlbwe" : : : "memory"); |
| 116 | local_irq_restore(flags); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static inline void write_host_tlbe(struct kvmppc_vcpu_e500 *vcpu_e500, |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 120 | int tlbsel, int esel, struct tlbe *stlbe) |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 121 | { |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 122 | if (tlbsel == 0) { |
Scott Wood | 0ef309956 | 2011-06-14 18:34:35 -0500 | [diff] [blame] | 123 | __write_host_tlbe(stlbe, |
| 124 | MAS0_TLBSEL(0) | |
| 125 | MAS0_ESEL(esel & (KVM_E500_TLB0_WAY_NUM - 1))); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 126 | } else { |
Scott Wood | 0ef309956 | 2011-06-14 18:34:35 -0500 | [diff] [blame] | 127 | __write_host_tlbe(stlbe, |
| 128 | MAS0_TLBSEL(1) | |
| 129 | MAS0_ESEL(to_htlb1_esel(esel))); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 130 | } |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 131 | trace_kvm_stlb_write(index_of(tlbsel, esel), stlbe->mas1, stlbe->mas2, |
| 132 | stlbe->mas3, stlbe->mas7); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 133 | } |
| 134 | |
Scott Wood | a4cd8b2 | 2011-06-14 18:34:41 -0500 | [diff] [blame] | 135 | void kvmppc_map_magic(struct kvm_vcpu *vcpu) |
| 136 | { |
| 137 | struct tlbe magic; |
| 138 | ulong shared_page = ((ulong)vcpu->arch.shared) & PAGE_MASK; |
| 139 | pfn_t pfn; |
| 140 | |
| 141 | pfn = (pfn_t)virt_to_phys((void *)shared_page) >> PAGE_SHIFT; |
| 142 | get_page(pfn_to_page(pfn)); |
| 143 | |
| 144 | magic.mas1 = MAS1_VALID | MAS1_TS | |
| 145 | MAS1_TSIZE(BOOK3E_PAGESZ_4K); |
| 146 | magic.mas2 = vcpu->arch.magic_page_ea | MAS2_M; |
| 147 | magic.mas3 = (pfn << PAGE_SHIFT) | |
| 148 | MAS3_SW | MAS3_SR | MAS3_UW | MAS3_UR; |
| 149 | magic.mas7 = pfn >> (32 - PAGE_SHIFT); |
| 150 | |
| 151 | __write_host_tlbe(&magic, MAS0_TLBSEL(1) | MAS0_ESEL(tlbcam_index)); |
| 152 | } |
| 153 | |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 154 | void kvmppc_e500_tlb_load(struct kvm_vcpu *vcpu, int cpu) |
| 155 | { |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | void kvmppc_e500_tlb_put(struct kvm_vcpu *vcpu) |
| 159 | { |
Liu Yu | 9aa4dd5 | 2009-01-14 10:47:38 -0600 | [diff] [blame] | 160 | _tlbil_all(); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | /* Search the guest TLB for a matching entry. */ |
| 164 | static int kvmppc_e500_tlb_index(struct kvmppc_vcpu_e500 *vcpu_e500, |
| 165 | gva_t eaddr, int tlbsel, unsigned int pid, int as) |
| 166 | { |
| 167 | int i; |
| 168 | |
| 169 | /* XXX Replace loop with fancy data structures. */ |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 170 | for (i = 0; i < vcpu_e500->gtlb_size[tlbsel]; i++) { |
| 171 | struct tlbe *tlbe = &vcpu_e500->gtlb_arch[tlbsel][i]; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 172 | unsigned int tid; |
| 173 | |
| 174 | if (eaddr < get_tlb_eaddr(tlbe)) |
| 175 | continue; |
| 176 | |
| 177 | if (eaddr > get_tlb_end(tlbe)) |
| 178 | continue; |
| 179 | |
| 180 | tid = get_tlb_tid(tlbe); |
| 181 | if (tid && (tid != pid)) |
| 182 | continue; |
| 183 | |
| 184 | if (!get_tlb_v(tlbe)) |
| 185 | continue; |
| 186 | |
| 187 | if (get_tlb_ts(tlbe) != as && as != -1) |
| 188 | continue; |
| 189 | |
| 190 | return i; |
| 191 | } |
| 192 | |
| 193 | return -1; |
| 194 | } |
| 195 | |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 196 | static inline void kvmppc_e500_priv_setup(struct tlbe_priv *priv, |
| 197 | struct tlbe *gtlbe, |
| 198 | pfn_t pfn) |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 199 | { |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 200 | priv->pfn = pfn; |
| 201 | priv->flags = E500_TLB_VALID; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 202 | |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 203 | if (tlbe_is_writable(gtlbe)) |
| 204 | priv->flags |= E500_TLB_DIRTY; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 205 | } |
| 206 | |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 207 | static inline void kvmppc_e500_priv_release(struct tlbe_priv *priv) |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 208 | { |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 209 | if (priv->flags & E500_TLB_VALID) { |
| 210 | if (priv->flags & E500_TLB_DIRTY) |
| 211 | kvm_release_pfn_dirty(priv->pfn); |
| 212 | else |
| 213 | kvm_release_pfn_clean(priv->pfn); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 214 | |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 215 | priv->flags = 0; |
| 216 | } |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | static void kvmppc_e500_tlb1_invalidate(struct kvmppc_vcpu_e500 *vcpu_e500, |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 220 | int esel) |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 221 | { |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 222 | mtspr(SPRN_MMUCSR0, MMUCSR0_TLB1FI); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | static inline void kvmppc_e500_deliver_tlb_miss(struct kvm_vcpu *vcpu, |
| 226 | unsigned int eaddr, int as) |
| 227 | { |
| 228 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
| 229 | unsigned int victim, pidsel, tsized; |
| 230 | int tlbsel; |
| 231 | |
Liu Yu | fb2838d | 2009-01-14 10:47:37 -0600 | [diff] [blame] | 232 | /* since we only have two TLBs, only lower bit is used. */ |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 233 | tlbsel = (vcpu_e500->mas4 >> 28) & 0x1; |
| 234 | victim = (tlbsel == 0) ? tlb0_get_next_victim(vcpu_e500) : 0; |
| 235 | pidsel = (vcpu_e500->mas4 >> 16) & 0xf; |
Liu Yu | 0cfb50e | 2009-06-05 14:54:29 +0800 | [diff] [blame] | 236 | tsized = (vcpu_e500->mas4 >> 7) & 0x1f; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 237 | |
| 238 | vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim) |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 239 | | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 240 | vcpu_e500->mas1 = MAS1_VALID | (as ? MAS1_TS : 0) |
| 241 | | MAS1_TID(vcpu_e500->pid[pidsel]) |
| 242 | | MAS1_TSIZE(tsized); |
| 243 | vcpu_e500->mas2 = (eaddr & MAS2_EPN) |
| 244 | | (vcpu_e500->mas4 & MAS2_ATTRIB_MASK); |
| 245 | vcpu_e500->mas3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3; |
| 246 | vcpu_e500->mas6 = (vcpu_e500->mas6 & MAS6_SPID1) |
| 247 | | (get_cur_pid(vcpu) << 16) |
| 248 | | (as ? MAS6_SAS : 0); |
| 249 | vcpu_e500->mas7 = 0; |
| 250 | } |
| 251 | |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 252 | static inline void kvmppc_e500_setup_stlbe(struct kvmppc_vcpu_e500 *vcpu_e500, |
| 253 | struct tlbe *gtlbe, int tsize, |
| 254 | struct tlbe_priv *priv, |
| 255 | u64 gvaddr, struct tlbe *stlbe) |
| 256 | { |
| 257 | pfn_t pfn = priv->pfn; |
| 258 | |
| 259 | /* Force TS=1 IPROT=0 for all guest mappings. */ |
| 260 | stlbe->mas1 = MAS1_TSIZE(tsize) |
| 261 | | MAS1_TID(get_tlb_tid(gtlbe)) | MAS1_TS | MAS1_VALID; |
| 262 | stlbe->mas2 = (gvaddr & MAS2_EPN) |
| 263 | | e500_shadow_mas2_attrib(gtlbe->mas2, |
| 264 | vcpu_e500->vcpu.arch.shared->msr & MSR_PR); |
| 265 | stlbe->mas3 = ((pfn << PAGE_SHIFT) & MAS3_RPN) |
| 266 | | e500_shadow_mas3_attrib(gtlbe->mas3, |
| 267 | vcpu_e500->vcpu.arch.shared->msr & MSR_PR); |
| 268 | stlbe->mas7 = (pfn >> (32 - PAGE_SHIFT)) & MAS7_RPN; |
| 269 | } |
| 270 | |
| 271 | |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 272 | static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500, |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 273 | u64 gvaddr, gfn_t gfn, struct tlbe *gtlbe, int tlbsel, int esel, |
| 274 | struct tlbe *stlbe) |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 275 | { |
Scott Wood | 9973d54 | 2011-06-14 18:34:39 -0500 | [diff] [blame] | 276 | struct kvm_memory_slot *slot; |
Scott Wood | 9973d54 | 2011-06-14 18:34:39 -0500 | [diff] [blame] | 277 | unsigned long pfn, hva; |
| 278 | int pfnmap = 0; |
| 279 | int tsize = BOOK3E_PAGESZ_4K; |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 280 | struct tlbe_priv *priv; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 281 | |
Scott Wood | 59c1f4e | 2011-06-14 18:34:37 -0500 | [diff] [blame] | 282 | /* |
| 283 | * Translate guest physical to true physical, acquiring |
| 284 | * a page reference if it is normal, non-reserved memory. |
Scott Wood | 9973d54 | 2011-06-14 18:34:39 -0500 | [diff] [blame] | 285 | * |
| 286 | * gfn_to_memslot() must succeed because otherwise we wouldn't |
| 287 | * have gotten this far. Eventually we should just pass the slot |
| 288 | * pointer through from the first lookup. |
Scott Wood | 59c1f4e | 2011-06-14 18:34:37 -0500 | [diff] [blame] | 289 | */ |
Scott Wood | 9973d54 | 2011-06-14 18:34:39 -0500 | [diff] [blame] | 290 | slot = gfn_to_memslot(vcpu_e500->vcpu.kvm, gfn); |
| 291 | hva = gfn_to_hva_memslot(slot, gfn); |
| 292 | |
| 293 | if (tlbsel == 1) { |
| 294 | struct vm_area_struct *vma; |
| 295 | down_read(¤t->mm->mmap_sem); |
| 296 | |
| 297 | vma = find_vma(current->mm, hva); |
| 298 | if (vma && hva >= vma->vm_start && |
| 299 | (vma->vm_flags & VM_PFNMAP)) { |
| 300 | /* |
| 301 | * This VMA is a physically contiguous region (e.g. |
| 302 | * /dev/mem) that bypasses normal Linux page |
| 303 | * management. Find the overlap between the |
| 304 | * vma and the memslot. |
| 305 | */ |
| 306 | |
| 307 | unsigned long start, end; |
| 308 | unsigned long slot_start, slot_end; |
| 309 | |
| 310 | pfnmap = 1; |
| 311 | |
| 312 | start = vma->vm_pgoff; |
| 313 | end = start + |
| 314 | ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT); |
| 315 | |
| 316 | pfn = start + ((hva - vma->vm_start) >> PAGE_SHIFT); |
| 317 | |
| 318 | slot_start = pfn - (gfn - slot->base_gfn); |
| 319 | slot_end = slot_start + slot->npages; |
| 320 | |
| 321 | if (start < slot_start) |
| 322 | start = slot_start; |
| 323 | if (end > slot_end) |
| 324 | end = slot_end; |
| 325 | |
| 326 | tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >> |
| 327 | MAS1_TSIZE_SHIFT; |
| 328 | |
| 329 | /* |
| 330 | * e500 doesn't implement the lowest tsize bit, |
| 331 | * or 1K pages. |
| 332 | */ |
| 333 | tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1); |
| 334 | |
| 335 | /* |
| 336 | * Now find the largest tsize (up to what the guest |
| 337 | * requested) that will cover gfn, stay within the |
| 338 | * range, and for which gfn and pfn are mutually |
| 339 | * aligned. |
| 340 | */ |
| 341 | |
| 342 | for (; tsize > BOOK3E_PAGESZ_4K; tsize -= 2) { |
| 343 | unsigned long gfn_start, gfn_end, tsize_pages; |
| 344 | tsize_pages = 1 << (tsize - 2); |
| 345 | |
| 346 | gfn_start = gfn & ~(tsize_pages - 1); |
| 347 | gfn_end = gfn_start + tsize_pages; |
| 348 | |
| 349 | if (gfn_start + pfn - gfn < start) |
| 350 | continue; |
| 351 | if (gfn_end + pfn - gfn > end) |
| 352 | continue; |
| 353 | if ((gfn & (tsize_pages - 1)) != |
| 354 | (pfn & (tsize_pages - 1))) |
| 355 | continue; |
| 356 | |
| 357 | gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1); |
| 358 | pfn &= ~(tsize_pages - 1); |
| 359 | break; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | up_read(¤t->mm->mmap_sem); |
| 364 | } |
| 365 | |
| 366 | if (likely(!pfnmap)) { |
| 367 | pfn = gfn_to_pfn_memslot(vcpu_e500->vcpu.kvm, slot, gfn); |
| 368 | if (is_error_pfn(pfn)) { |
| 369 | printk(KERN_ERR "Couldn't get real page for gfn %lx!\n", |
| 370 | (long)gfn); |
| 371 | kvm_release_pfn_clean(pfn); |
| 372 | return; |
| 373 | } |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 374 | } |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 375 | |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 376 | /* Drop old priv and setup new one. */ |
| 377 | priv = &vcpu_e500->gtlb_priv[tlbsel][esel]; |
| 378 | kvmppc_e500_priv_release(priv); |
| 379 | kvmppc_e500_priv_setup(priv, gtlbe, pfn); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 380 | |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 381 | kvmppc_e500_setup_stlbe(vcpu_e500, gtlbe, tsize, priv, gvaddr, stlbe); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | /* XXX only map the one-one case, for now use TLB0 */ |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 385 | static int kvmppc_e500_tlb0_map(struct kvmppc_vcpu_e500 *vcpu_e500, |
| 386 | int esel, struct tlbe *stlbe) |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 387 | { |
| 388 | struct tlbe *gtlbe; |
| 389 | |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 390 | gtlbe = &vcpu_e500->gtlb_arch[0][esel]; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 391 | |
| 392 | kvmppc_e500_shadow_map(vcpu_e500, get_tlb_eaddr(gtlbe), |
| 393 | get_tlb_raddr(gtlbe) >> PAGE_SHIFT, |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 394 | gtlbe, 0, esel, stlbe); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 395 | |
| 396 | return esel; |
| 397 | } |
| 398 | |
| 399 | /* Caller must ensure that the specified guest TLB entry is safe to insert into |
| 400 | * the shadow TLB. */ |
| 401 | /* XXX for both one-one and one-to-many , for now use TLB1 */ |
| 402 | static int kvmppc_e500_tlb1_map(struct kvmppc_vcpu_e500 *vcpu_e500, |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 403 | u64 gvaddr, gfn_t gfn, struct tlbe *gtlbe, struct tlbe *stlbe) |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 404 | { |
| 405 | unsigned int victim; |
| 406 | |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 407 | victim = vcpu_e500->gtlb_nv[1]++; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 408 | |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 409 | if (unlikely(vcpu_e500->gtlb_nv[1] >= tlb1_max_shadow_size())) |
| 410 | vcpu_e500->gtlb_nv[1] = 0; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 411 | |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 412 | kvmppc_e500_shadow_map(vcpu_e500, gvaddr, gfn, gtlbe, 1, victim, stlbe); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 413 | |
| 414 | return victim; |
| 415 | } |
| 416 | |
| 417 | /* Invalidate all guest kernel mappings when enter usermode, |
| 418 | * so that when they fault back in they will get the |
| 419 | * proper permission bits. */ |
| 420 | void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode) |
| 421 | { |
| 422 | if (usermode) { |
Liu Yu | 9aa4dd5 | 2009-01-14 10:47:38 -0600 | [diff] [blame] | 423 | _tlbil_all(); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 427 | static inline int kvmppc_e500_gtlbe_invalidate( |
| 428 | struct kvmppc_vcpu_e500 *vcpu_e500, |
| 429 | int tlbsel, int esel) |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 430 | { |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 431 | struct tlbe *gtlbe = &vcpu_e500->gtlb_arch[tlbsel][esel]; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 432 | |
| 433 | if (unlikely(get_tlb_iprot(gtlbe))) |
| 434 | return -1; |
| 435 | |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 436 | gtlbe->mas1 = 0; |
| 437 | |
| 438 | return 0; |
| 439 | } |
| 440 | |
Liu Yu | b0a1835 | 2009-02-17 16:52:08 +0800 | [diff] [blame] | 441 | int kvmppc_e500_emul_mt_mmucsr0(struct kvmppc_vcpu_e500 *vcpu_e500, ulong value) |
| 442 | { |
| 443 | int esel; |
| 444 | |
| 445 | if (value & MMUCSR0_TLB0FI) |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 446 | for (esel = 0; esel < vcpu_e500->gtlb_size[0]; esel++) |
Liu Yu | b0a1835 | 2009-02-17 16:52:08 +0800 | [diff] [blame] | 447 | kvmppc_e500_gtlbe_invalidate(vcpu_e500, 0, esel); |
| 448 | if (value & MMUCSR0_TLB1FI) |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 449 | for (esel = 0; esel < vcpu_e500->gtlb_size[1]; esel++) |
Liu Yu | b0a1835 | 2009-02-17 16:52:08 +0800 | [diff] [blame] | 450 | kvmppc_e500_gtlbe_invalidate(vcpu_e500, 1, esel); |
| 451 | |
| 452 | _tlbil_all(); |
| 453 | |
| 454 | return EMULATE_DONE; |
| 455 | } |
| 456 | |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 457 | int kvmppc_e500_emul_tlbivax(struct kvm_vcpu *vcpu, int ra, int rb) |
| 458 | { |
| 459 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
| 460 | unsigned int ia; |
| 461 | int esel, tlbsel; |
| 462 | gva_t ea; |
| 463 | |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 464 | ea = ((ra) ? kvmppc_get_gpr(vcpu, ra) : 0) + kvmppc_get_gpr(vcpu, rb); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 465 | |
| 466 | ia = (ea >> 2) & 0x1; |
| 467 | |
Liu Yu | fb2838d | 2009-01-14 10:47:37 -0600 | [diff] [blame] | 468 | /* since we only have two TLBs, only lower bit is used. */ |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 469 | tlbsel = (ea >> 3) & 0x1; |
| 470 | |
| 471 | if (ia) { |
| 472 | /* invalidate all entries */ |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 473 | for (esel = 0; esel < vcpu_e500->gtlb_size[tlbsel]; esel++) |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 474 | kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel); |
| 475 | } else { |
| 476 | ea &= 0xfffff000; |
| 477 | esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, |
| 478 | get_cur_pid(vcpu), -1); |
| 479 | if (esel >= 0) |
| 480 | kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel); |
| 481 | } |
| 482 | |
Liu Yu | 9aa4dd5 | 2009-01-14 10:47:38 -0600 | [diff] [blame] | 483 | _tlbil_all(); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 484 | |
| 485 | return EMULATE_DONE; |
| 486 | } |
| 487 | |
| 488 | int kvmppc_e500_emul_tlbre(struct kvm_vcpu *vcpu) |
| 489 | { |
| 490 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
| 491 | int tlbsel, esel; |
| 492 | struct tlbe *gtlbe; |
| 493 | |
| 494 | tlbsel = get_tlb_tlbsel(vcpu_e500); |
| 495 | esel = get_tlb_esel(vcpu_e500, tlbsel); |
| 496 | |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 497 | gtlbe = &vcpu_e500->gtlb_arch[tlbsel][esel]; |
Liu Yu | bc35cbc | 2009-03-17 16:57:45 +0800 | [diff] [blame] | 498 | vcpu_e500->mas0 &= ~MAS0_NV(~0); |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 499 | vcpu_e500->mas0 |= MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 500 | vcpu_e500->mas1 = gtlbe->mas1; |
| 501 | vcpu_e500->mas2 = gtlbe->mas2; |
| 502 | vcpu_e500->mas3 = gtlbe->mas3; |
| 503 | vcpu_e500->mas7 = gtlbe->mas7; |
| 504 | |
| 505 | return EMULATE_DONE; |
| 506 | } |
| 507 | |
| 508 | int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *vcpu, int rb) |
| 509 | { |
| 510 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
| 511 | int as = !!get_cur_sas(vcpu_e500); |
| 512 | unsigned int pid = get_cur_spid(vcpu_e500); |
| 513 | int esel, tlbsel; |
| 514 | struct tlbe *gtlbe = NULL; |
| 515 | gva_t ea; |
| 516 | |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 517 | ea = kvmppc_get_gpr(vcpu, rb); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 518 | |
| 519 | for (tlbsel = 0; tlbsel < 2; tlbsel++) { |
| 520 | esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, as); |
| 521 | if (esel >= 0) { |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 522 | gtlbe = &vcpu_e500->gtlb_arch[tlbsel][esel]; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 523 | break; |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | if (gtlbe) { |
| 528 | vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(esel) |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 529 | | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 530 | vcpu_e500->mas1 = gtlbe->mas1; |
| 531 | vcpu_e500->mas2 = gtlbe->mas2; |
| 532 | vcpu_e500->mas3 = gtlbe->mas3; |
| 533 | vcpu_e500->mas7 = gtlbe->mas7; |
| 534 | } else { |
| 535 | int victim; |
| 536 | |
Liu Yu | fb2838d | 2009-01-14 10:47:37 -0600 | [diff] [blame] | 537 | /* since we only have two TLBs, only lower bit is used. */ |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 538 | tlbsel = vcpu_e500->mas4 >> 28 & 0x1; |
| 539 | victim = (tlbsel == 0) ? tlb0_get_next_victim(vcpu_e500) : 0; |
| 540 | |
| 541 | vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim) |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 542 | | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 543 | vcpu_e500->mas1 = (vcpu_e500->mas6 & MAS6_SPID0) |
| 544 | | (vcpu_e500->mas6 & (MAS6_SAS ? MAS1_TS : 0)) |
| 545 | | (vcpu_e500->mas4 & MAS4_TSIZED(~0)); |
| 546 | vcpu_e500->mas2 &= MAS2_EPN; |
| 547 | vcpu_e500->mas2 |= vcpu_e500->mas4 & MAS2_ATTRIB_MASK; |
| 548 | vcpu_e500->mas3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3; |
| 549 | vcpu_e500->mas7 = 0; |
| 550 | } |
| 551 | |
Scott Wood | 49ea069 | 2011-03-28 15:01:24 -0500 | [diff] [blame] | 552 | kvmppc_set_exit_type(vcpu, EMULATED_TLBSX_EXITS); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 553 | return EMULATE_DONE; |
| 554 | } |
| 555 | |
| 556 | int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *vcpu) |
| 557 | { |
| 558 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 559 | struct tlbe *gtlbe; |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 560 | int tlbsel, esel; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 561 | |
| 562 | tlbsel = get_tlb_tlbsel(vcpu_e500); |
| 563 | esel = get_tlb_esel(vcpu_e500, tlbsel); |
| 564 | |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 565 | gtlbe = &vcpu_e500->gtlb_arch[tlbsel][esel]; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 566 | |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 567 | if (get_tlb_v(gtlbe) && tlbsel == 1) |
| 568 | kvmppc_e500_tlb1_invalidate(vcpu_e500, esel); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 569 | |
| 570 | gtlbe->mas1 = vcpu_e500->mas1; |
| 571 | gtlbe->mas2 = vcpu_e500->mas2; |
| 572 | gtlbe->mas3 = vcpu_e500->mas3; |
| 573 | gtlbe->mas7 = vcpu_e500->mas7; |
| 574 | |
Marcelo Tosatti | 46f43c6 | 2009-06-18 11:47:27 -0300 | [diff] [blame] | 575 | trace_kvm_gtlb_write(vcpu_e500->mas0, gtlbe->mas1, gtlbe->mas2, |
| 576 | gtlbe->mas3, gtlbe->mas7); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 577 | |
| 578 | /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */ |
| 579 | if (tlbe_is_host_safe(vcpu, gtlbe)) { |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 580 | struct tlbe stlbe; |
| 581 | int stlbsel, sesel; |
| 582 | u64 eaddr; |
| 583 | u64 raddr; |
| 584 | |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 585 | switch (tlbsel) { |
| 586 | case 0: |
| 587 | /* TLB0 */ |
| 588 | gtlbe->mas1 &= ~MAS1_TSIZE(~0); |
Liu Yu | 0cfb50e | 2009-06-05 14:54:29 +0800 | [diff] [blame] | 589 | gtlbe->mas1 |= MAS1_TSIZE(BOOK3E_PAGESZ_4K); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 590 | |
| 591 | stlbsel = 0; |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 592 | sesel = kvmppc_e500_tlb0_map(vcpu_e500, esel, &stlbe); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 593 | |
| 594 | break; |
| 595 | |
| 596 | case 1: |
| 597 | /* TLB1 */ |
| 598 | eaddr = get_tlb_eaddr(gtlbe); |
| 599 | raddr = get_tlb_raddr(gtlbe); |
| 600 | |
| 601 | /* Create a 4KB mapping on the host. |
| 602 | * If the guest wanted a large page, |
| 603 | * only the first 4KB is mapped here and the rest |
| 604 | * are mapped on the fly. */ |
| 605 | stlbsel = 1; |
| 606 | sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr, |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 607 | raddr >> PAGE_SHIFT, gtlbe, &stlbe); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 608 | break; |
| 609 | |
| 610 | default: |
| 611 | BUG(); |
| 612 | } |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 613 | write_host_tlbe(vcpu_e500, stlbsel, sesel, &stlbe); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 614 | } |
| 615 | |
Scott Wood | 49ea069 | 2011-03-28 15:01:24 -0500 | [diff] [blame] | 616 | kvmppc_set_exit_type(vcpu, EMULATED_TLBWE_EXITS); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 617 | return EMULATE_DONE; |
| 618 | } |
| 619 | |
| 620 | int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr) |
| 621 | { |
Alexander Graf | 666e725 | 2010-07-29 14:47:43 +0200 | [diff] [blame] | 622 | unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 623 | |
| 624 | return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as); |
| 625 | } |
| 626 | |
| 627 | int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr) |
| 628 | { |
Alexander Graf | 666e725 | 2010-07-29 14:47:43 +0200 | [diff] [blame] | 629 | unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 630 | |
| 631 | return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as); |
| 632 | } |
| 633 | |
| 634 | void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu) |
| 635 | { |
Alexander Graf | 666e725 | 2010-07-29 14:47:43 +0200 | [diff] [blame] | 636 | unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 637 | |
| 638 | kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.pc, as); |
| 639 | } |
| 640 | |
| 641 | void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu) |
| 642 | { |
Alexander Graf | 666e725 | 2010-07-29 14:47:43 +0200 | [diff] [blame] | 643 | unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 644 | |
| 645 | kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.fault_dear, as); |
| 646 | } |
| 647 | |
| 648 | gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int index, |
| 649 | gva_t eaddr) |
| 650 | { |
| 651 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
| 652 | struct tlbe *gtlbe = |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 653 | &vcpu_e500->gtlb_arch[tlbsel_of(index)][esel_of(index)]; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 654 | u64 pgmask = get_tlb_bytes(gtlbe) - 1; |
| 655 | |
| 656 | return get_tlb_raddr(gtlbe) | (eaddr & pgmask); |
| 657 | } |
| 658 | |
| 659 | void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu) |
| 660 | { |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 eaddr, gpa_t gpaddr, |
| 664 | unsigned int index) |
| 665 | { |
| 666 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 667 | struct tlbe_priv *priv; |
| 668 | struct tlbe *gtlbe, stlbe; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 669 | int tlbsel = tlbsel_of(index); |
| 670 | int esel = esel_of(index); |
| 671 | int stlbsel, sesel; |
| 672 | |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 673 | gtlbe = &vcpu_e500->gtlb_arch[tlbsel][esel]; |
| 674 | |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 675 | switch (tlbsel) { |
| 676 | case 0: |
| 677 | stlbsel = 0; |
| 678 | sesel = esel; |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 679 | priv = &vcpu_e500->gtlb_priv[stlbsel][sesel]; |
| 680 | |
| 681 | kvmppc_e500_setup_stlbe(vcpu_e500, gtlbe, BOOK3E_PAGESZ_4K, |
| 682 | priv, eaddr, &stlbe); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 683 | break; |
| 684 | |
| 685 | case 1: { |
| 686 | gfn_t gfn = gpaddr >> PAGE_SHIFT; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 687 | |
| 688 | stlbsel = 1; |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 689 | sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr, gfn, |
| 690 | gtlbe, &stlbe); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 691 | break; |
| 692 | } |
| 693 | |
| 694 | default: |
| 695 | BUG(); |
| 696 | break; |
| 697 | } |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 698 | |
| 699 | write_host_tlbe(vcpu_e500, stlbsel, sesel, &stlbe); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | int kvmppc_e500_tlb_search(struct kvm_vcpu *vcpu, |
| 703 | gva_t eaddr, unsigned int pid, int as) |
| 704 | { |
| 705 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
| 706 | int esel, tlbsel; |
| 707 | |
| 708 | for (tlbsel = 0; tlbsel < 2; tlbsel++) { |
| 709 | esel = kvmppc_e500_tlb_index(vcpu_e500, eaddr, tlbsel, pid, as); |
| 710 | if (esel >= 0) |
| 711 | return index_of(tlbsel, esel); |
| 712 | } |
| 713 | |
| 714 | return -1; |
| 715 | } |
| 716 | |
Scott Wood | 5ce941e | 2011-04-27 17:24:21 -0500 | [diff] [blame] | 717 | void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid) |
| 718 | { |
| 719 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
| 720 | |
| 721 | vcpu_e500->pid[0] = vcpu->arch.shadow_pid = |
| 722 | vcpu->arch.pid = pid; |
| 723 | } |
| 724 | |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 725 | void kvmppc_e500_tlb_setup(struct kvmppc_vcpu_e500 *vcpu_e500) |
| 726 | { |
| 727 | struct tlbe *tlbe; |
| 728 | |
| 729 | /* Insert large initial mapping for guest. */ |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 730 | tlbe = &vcpu_e500->gtlb_arch[1][0]; |
Liu Yu | 0cfb50e | 2009-06-05 14:54:29 +0800 | [diff] [blame] | 731 | tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOK3E_PAGESZ_256M); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 732 | tlbe->mas2 = 0; |
| 733 | tlbe->mas3 = E500_TLB_SUPER_PERM_MASK; |
| 734 | tlbe->mas7 = 0; |
| 735 | |
| 736 | /* 4K map for serial output. Used by kernel wrapper. */ |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 737 | tlbe = &vcpu_e500->gtlb_arch[1][1]; |
Liu Yu | 0cfb50e | 2009-06-05 14:54:29 +0800 | [diff] [blame] | 738 | tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOK3E_PAGESZ_4K); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 739 | tlbe->mas2 = (0xe0004500 & 0xFFFFF000) | MAS2_I | MAS2_G; |
| 740 | tlbe->mas3 = (0xe0004500 & 0xFFFFF000) | E500_TLB_SUPER_PERM_MASK; |
| 741 | tlbe->mas7 = 0; |
| 742 | } |
| 743 | |
| 744 | int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *vcpu_e500) |
| 745 | { |
| 746 | tlb1_entry_num = mfspr(SPRN_TLB1CFG) & 0xFFF; |
| 747 | |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 748 | vcpu_e500->gtlb_size[0] = KVM_E500_TLB0_SIZE; |
| 749 | vcpu_e500->gtlb_arch[0] = |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 750 | kzalloc(sizeof(struct tlbe) * KVM_E500_TLB0_SIZE, GFP_KERNEL); |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 751 | if (vcpu_e500->gtlb_arch[0] == NULL) |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 752 | goto err_out; |
| 753 | |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 754 | vcpu_e500->gtlb_size[1] = KVM_E500_TLB1_SIZE; |
| 755 | vcpu_e500->gtlb_arch[1] = |
| 756 | kzalloc(sizeof(struct tlbe) * KVM_E500_TLB1_SIZE, GFP_KERNEL); |
| 757 | if (vcpu_e500->gtlb_arch[1] == NULL) |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 758 | goto err_out_guest0; |
| 759 | |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 760 | vcpu_e500->gtlb_priv[0] = (struct tlbe_priv *) |
| 761 | kzalloc(sizeof(struct tlbe_priv) * KVM_E500_TLB0_SIZE, GFP_KERNEL); |
| 762 | if (vcpu_e500->gtlb_priv[0] == NULL) |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 763 | goto err_out_guest1; |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 764 | vcpu_e500->gtlb_priv[1] = (struct tlbe_priv *) |
| 765 | kzalloc(sizeof(struct tlbe_priv) * KVM_E500_TLB1_SIZE, GFP_KERNEL); |
| 766 | |
| 767 | if (vcpu_e500->gtlb_priv[1] == NULL) |
| 768 | goto err_out_priv0; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 769 | |
Liu Yu | da15bf4 | 2010-01-22 19:36:53 +0800 | [diff] [blame] | 770 | /* Init TLB configuration register */ |
| 771 | vcpu_e500->tlb0cfg = mfspr(SPRN_TLB0CFG) & ~0xfffUL; |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 772 | vcpu_e500->tlb0cfg |= vcpu_e500->gtlb_size[0]; |
Liu Yu | da15bf4 | 2010-01-22 19:36:53 +0800 | [diff] [blame] | 773 | vcpu_e500->tlb1cfg = mfspr(SPRN_TLB1CFG) & ~0xfffUL; |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 774 | vcpu_e500->tlb1cfg |= vcpu_e500->gtlb_size[1]; |
Liu Yu | da15bf4 | 2010-01-22 19:36:53 +0800 | [diff] [blame] | 775 | |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 776 | return 0; |
| 777 | |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 778 | err_out_priv0: |
| 779 | kfree(vcpu_e500->gtlb_priv[0]); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 780 | err_out_guest1: |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 781 | kfree(vcpu_e500->gtlb_arch[1]); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 782 | err_out_guest0: |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 783 | kfree(vcpu_e500->gtlb_arch[0]); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 784 | err_out: |
| 785 | return -1; |
| 786 | } |
| 787 | |
| 788 | void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *vcpu_e500) |
| 789 | { |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame^] | 790 | int stlbsel, i; |
| 791 | |
| 792 | /* release all privs */ |
| 793 | for (stlbsel = 0; stlbsel < 2; stlbsel++) |
| 794 | for (i = 0; i < vcpu_e500->gtlb_size[stlbsel]; i++) { |
| 795 | struct tlbe_priv *priv = |
| 796 | &vcpu_e500->gtlb_priv[stlbsel][i]; |
| 797 | kvmppc_e500_priv_release(priv); |
| 798 | } |
| 799 | |
| 800 | /* discard all guest mapping */ |
| 801 | _tlbil_all(); |
| 802 | |
| 803 | kfree(vcpu_e500->gtlb_arch[1]); |
| 804 | kfree(vcpu_e500->gtlb_arch[0]); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 805 | } |