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 |
Scott Wood | 73196cd | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 5 | * Scott Wood, scottwood@freescale.com |
Scott Wood | 4f802fe | 2011-12-20 15:34:37 +0000 | [diff] [blame] | 6 | * Ashish Kalra, ashish.kalra@freescale.com |
Scott Wood | 73196cd | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 7 | * Varun Sethi, varun.sethi@freescale.com |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 8 | * |
| 9 | * Description: |
| 10 | * This file is based on arch/powerpc/kvm/44x_tlb.c, |
| 11 | * by Hollis Blanchard <hollisb@us.ibm.com>. |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License, version 2, as |
| 15 | * published by the Free Software Foundation. |
| 16 | */ |
| 17 | |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 18 | #include <linux/kernel.h> |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 19 | #include <linux/types.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 21 | #include <linux/string.h> |
| 22 | #include <linux/kvm.h> |
| 23 | #include <linux/kvm_host.h> |
| 24 | #include <linux/highmem.h> |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 25 | #include <linux/log2.h> |
| 26 | #include <linux/uaccess.h> |
| 27 | #include <linux/sched.h> |
| 28 | #include <linux/rwsem.h> |
| 29 | #include <linux/vmalloc.h> |
Alexander Graf | 95325e6 | 2011-09-20 01:31:48 +0200 | [diff] [blame] | 30 | #include <linux/hugetlb.h> |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 31 | #include <asm/kvm_ppc.h> |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 32 | |
Scott Wood | 29a5a6f | 2011-12-20 15:34:29 +0000 | [diff] [blame] | 33 | #include "e500.h" |
Marcelo Tosatti | 46f43c6 | 2009-06-18 11:47:27 -0300 | [diff] [blame] | 34 | #include "trace.h" |
Scott Wood | 49ea069 | 2011-03-28 15:01:24 -0500 | [diff] [blame] | 35 | #include "timing.h" |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 36 | |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 37 | #define to_htlb1_esel(esel) (host_tlb_params[1].entries - (esel) - 1) |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 38 | |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 39 | static struct kvmppc_e500_tlb_params host_tlb_params[E500_TLB_NUM]; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 40 | |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 41 | static inline unsigned int gtlb0_get_next_victim( |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 42 | struct kvmppc_vcpu_e500 *vcpu_e500) |
| 43 | { |
| 44 | unsigned int victim; |
| 45 | |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame] | 46 | victim = vcpu_e500->gtlb_nv[0]++; |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 47 | if (unlikely(vcpu_e500->gtlb_nv[0] >= vcpu_e500->gtlb_params[0].ways)) |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame] | 48 | vcpu_e500->gtlb_nv[0] = 0; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 49 | |
| 50 | return victim; |
| 51 | } |
| 52 | |
| 53 | static inline unsigned int tlb1_max_shadow_size(void) |
| 54 | { |
Scott Wood | a4cd8b2 | 2011-06-14 18:34:41 -0500 | [diff] [blame] | 55 | /* reserve one entry for magic page */ |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 56 | return host_tlb_params[1].entries - tlbcam_index - 1; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 57 | } |
| 58 | |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 59 | static inline int tlbe_is_writable(struct kvm_book3e_206_tlb_entry *tlbe) |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 60 | { |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 61 | return tlbe->mas7_3 & (MAS3_SW|MAS3_UW); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static inline u32 e500_shadow_mas3_attrib(u32 mas3, int usermode) |
| 65 | { |
| 66 | /* Mask off reserved bits. */ |
| 67 | mas3 &= MAS3_ATTRIB_MASK; |
| 68 | |
Scott Wood | 73196cd | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 69 | #ifndef CONFIG_KVM_BOOKE_HV |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 70 | if (!usermode) { |
| 71 | /* Guest is in supervisor mode, |
| 72 | * so we need to translate guest |
| 73 | * supervisor permissions into user permissions. */ |
| 74 | mas3 &= ~E500_TLB_USER_PERM_MASK; |
| 75 | mas3 |= (mas3 & E500_TLB_SUPER_PERM_MASK) << 1; |
| 76 | } |
Scott Wood | 73196cd | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 77 | mas3 |= E500_TLB_SUPER_PERM_MASK; |
| 78 | #endif |
| 79 | return mas3; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static inline u32 e500_shadow_mas2_attrib(u32 mas2, int usermode) |
| 83 | { |
Liu Yu | 046a48b | 2009-03-17 16:57:46 +0800 | [diff] [blame] | 84 | #ifdef CONFIG_SMP |
| 85 | return (mas2 & MAS2_ATTRIB_MASK) | MAS2_M; |
| 86 | #else |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 87 | return mas2 & MAS2_ATTRIB_MASK; |
Liu Yu | 046a48b | 2009-03-17 16:57:46 +0800 | [diff] [blame] | 88 | #endif |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /* |
| 92 | * writing shadow tlb entry to host TLB |
| 93 | */ |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 94 | static inline void __write_host_tlbe(struct kvm_book3e_206_tlb_entry *stlbe, |
| 95 | uint32_t mas0) |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 96 | { |
Scott Wood | 0ef309956 | 2011-06-14 18:34:35 -0500 | [diff] [blame] | 97 | unsigned long flags; |
| 98 | |
| 99 | local_irq_save(flags); |
| 100 | mtspr(SPRN_MAS0, mas0); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 101 | mtspr(SPRN_MAS1, stlbe->mas1); |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 102 | mtspr(SPRN_MAS2, (unsigned long)stlbe->mas2); |
| 103 | mtspr(SPRN_MAS3, (u32)stlbe->mas7_3); |
| 104 | mtspr(SPRN_MAS7, (u32)(stlbe->mas7_3 >> 32)); |
Scott Wood | 73196cd | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 105 | #ifdef CONFIG_KVM_BOOKE_HV |
| 106 | mtspr(SPRN_MAS8, stlbe->mas8); |
| 107 | #endif |
Scott Wood | 0ef309956 | 2011-06-14 18:34:35 -0500 | [diff] [blame] | 108 | asm volatile("isync; tlbwe" : : : "memory"); |
Scott Wood | 73196cd | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 109 | |
| 110 | #ifdef CONFIG_KVM_BOOKE_HV |
| 111 | /* Must clear mas8 for other host tlbwe's */ |
| 112 | mtspr(SPRN_MAS8, 0); |
| 113 | isync(); |
| 114 | #endif |
Scott Wood | 0ef309956 | 2011-06-14 18:34:35 -0500 | [diff] [blame] | 115 | local_irq_restore(flags); |
Liu Yu | d37b1a0 | 2011-12-20 14:42:56 +0000 | [diff] [blame] | 116 | |
| 117 | trace_kvm_booke206_stlb_write(mas0, stlbe->mas8, stlbe->mas1, |
| 118 | stlbe->mas2, stlbe->mas7_3); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 119 | } |
| 120 | |
Scott Wood | 57013524 | 2011-11-29 10:40:23 +0000 | [diff] [blame] | 121 | /* |
| 122 | * Acquire a mas0 with victim hint, as if we just took a TLB miss. |
| 123 | * |
| 124 | * We don't care about the address we're searching for, other than that it's |
| 125 | * in the right set and is not present in the TLB. Using a zero PID and a |
| 126 | * userspace address means we don't have to set and then restore MAS5, or |
| 127 | * calculate a proper MAS6 value. |
| 128 | */ |
| 129 | static u32 get_host_mas0(unsigned long eaddr) |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 130 | { |
Scott Wood | 57013524 | 2011-11-29 10:40:23 +0000 | [diff] [blame] | 131 | unsigned long flags; |
| 132 | u32 mas0; |
| 133 | |
| 134 | local_irq_save(flags); |
| 135 | mtspr(SPRN_MAS6, 0); |
| 136 | asm volatile("tlbsx 0, %0" : : "b" (eaddr & ~CONFIG_PAGE_OFFSET)); |
| 137 | mas0 = mfspr(SPRN_MAS0); |
| 138 | local_irq_restore(flags); |
| 139 | |
| 140 | return mas0; |
| 141 | } |
| 142 | |
| 143 | /* sesel is for tlb1 only */ |
| 144 | static inline void write_host_tlbe(struct kvmppc_vcpu_e500 *vcpu_e500, |
| 145 | int tlbsel, int sesel, struct kvm_book3e_206_tlb_entry *stlbe) |
| 146 | { |
| 147 | u32 mas0; |
| 148 | |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 149 | if (tlbsel == 0) { |
Scott Wood | 57013524 | 2011-11-29 10:40:23 +0000 | [diff] [blame] | 150 | mas0 = get_host_mas0(stlbe->mas2); |
| 151 | __write_host_tlbe(stlbe, mas0); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 152 | } else { |
Scott Wood | 0ef309956 | 2011-06-14 18:34:35 -0500 | [diff] [blame] | 153 | __write_host_tlbe(stlbe, |
| 154 | MAS0_TLBSEL(1) | |
Scott Wood | 57013524 | 2011-11-29 10:40:23 +0000 | [diff] [blame] | 155 | MAS0_ESEL(to_htlb1_esel(sesel))); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 156 | } |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 157 | } |
| 158 | |
Alexander Graf | bf7ca4b | 2012-02-15 23:40:00 +0000 | [diff] [blame^] | 159 | #ifdef CONFIG_KVM_E500V2 |
Scott Wood | a4cd8b2 | 2011-06-14 18:34:41 -0500 | [diff] [blame] | 160 | void kvmppc_map_magic(struct kvm_vcpu *vcpu) |
| 161 | { |
Liu Yu | dd9ebf1f | 2011-06-14 18:35:14 -0500 | [diff] [blame] | 162 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 163 | struct kvm_book3e_206_tlb_entry magic; |
Scott Wood | a4cd8b2 | 2011-06-14 18:34:41 -0500 | [diff] [blame] | 164 | ulong shared_page = ((ulong)vcpu->arch.shared) & PAGE_MASK; |
Liu Yu | dd9ebf1f | 2011-06-14 18:35:14 -0500 | [diff] [blame] | 165 | unsigned int stid; |
Scott Wood | a4cd8b2 | 2011-06-14 18:34:41 -0500 | [diff] [blame] | 166 | pfn_t pfn; |
| 167 | |
| 168 | pfn = (pfn_t)virt_to_phys((void *)shared_page) >> PAGE_SHIFT; |
| 169 | get_page(pfn_to_page(pfn)); |
| 170 | |
Liu Yu | dd9ebf1f | 2011-06-14 18:35:14 -0500 | [diff] [blame] | 171 | preempt_disable(); |
| 172 | stid = kvmppc_e500_get_sid(vcpu_e500, 0, 0, 0, 0); |
| 173 | |
| 174 | magic.mas1 = MAS1_VALID | MAS1_TS | MAS1_TID(stid) | |
Scott Wood | a4cd8b2 | 2011-06-14 18:34:41 -0500 | [diff] [blame] | 175 | MAS1_TSIZE(BOOK3E_PAGESZ_4K); |
| 176 | magic.mas2 = vcpu->arch.magic_page_ea | MAS2_M; |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 177 | magic.mas7_3 = ((u64)pfn << PAGE_SHIFT) | |
| 178 | MAS3_SW | MAS3_SR | MAS3_UW | MAS3_UR; |
Liu Yu | d37b1a0 | 2011-12-20 14:42:56 +0000 | [diff] [blame] | 179 | magic.mas8 = 0; |
Scott Wood | a4cd8b2 | 2011-06-14 18:34:41 -0500 | [diff] [blame] | 180 | |
| 181 | __write_host_tlbe(&magic, MAS0_TLBSEL(1) | MAS0_ESEL(tlbcam_index)); |
Liu Yu | dd9ebf1f | 2011-06-14 18:35:14 -0500 | [diff] [blame] | 182 | preempt_enable(); |
Scott Wood | a4cd8b2 | 2011-06-14 18:34:41 -0500 | [diff] [blame] | 183 | } |
Scott Wood | 8fdd21a2 | 2011-12-20 15:34:34 +0000 | [diff] [blame] | 184 | #endif |
Liu Yu | dd9ebf1f | 2011-06-14 18:35:14 -0500 | [diff] [blame] | 185 | |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 186 | static void inval_gtlbe_on_host(struct kvmppc_vcpu_e500 *vcpu_e500, |
| 187 | int tlbsel, int esel) |
Liu Yu | dd9ebf1f | 2011-06-14 18:35:14 -0500 | [diff] [blame] | 188 | { |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 189 | struct kvm_book3e_206_tlb_entry *gtlbe = |
| 190 | get_entry(vcpu_e500, tlbsel, esel); |
Liu Yu | dd9ebf1f | 2011-06-14 18:35:14 -0500 | [diff] [blame] | 191 | |
Scott Wood | 4f802fe | 2011-12-20 15:34:37 +0000 | [diff] [blame] | 192 | if (tlbsel == 1 && |
| 193 | vcpu_e500->gtlb_priv[1][esel].ref.flags & E500_TLB_BITMAP) { |
| 194 | u64 tmp = vcpu_e500->g2h_tlb1_map[esel]; |
| 195 | int hw_tlb_indx; |
| 196 | unsigned long flags; |
| 197 | |
| 198 | local_irq_save(flags); |
| 199 | while (tmp) { |
| 200 | hw_tlb_indx = __ilog2_u64(tmp & -tmp); |
| 201 | mtspr(SPRN_MAS0, |
| 202 | MAS0_TLBSEL(1) | |
| 203 | MAS0_ESEL(to_htlb1_esel(hw_tlb_indx))); |
| 204 | mtspr(SPRN_MAS1, 0); |
| 205 | asm volatile("tlbwe"); |
| 206 | vcpu_e500->h2g_tlb1_rmap[hw_tlb_indx] = 0; |
| 207 | tmp &= tmp - 1; |
| 208 | } |
| 209 | mb(); |
| 210 | vcpu_e500->g2h_tlb1_map[esel] = 0; |
| 211 | vcpu_e500->gtlb_priv[1][esel].ref.flags &= ~E500_TLB_BITMAP; |
| 212 | local_irq_restore(flags); |
| 213 | |
Scott Wood | 8fdd21a2 | 2011-12-20 15:34:34 +0000 | [diff] [blame] | 214 | return; |
Liu Yu | dd9ebf1f | 2011-06-14 18:35:14 -0500 | [diff] [blame] | 215 | } |
| 216 | |
Scott Wood | 8fdd21a2 | 2011-12-20 15:34:34 +0000 | [diff] [blame] | 217 | /* Guest tlbe is backed by at most one host tlbe per shadow pid. */ |
| 218 | kvmppc_e500_tlbil_one(vcpu_e500, gtlbe); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 219 | } |
| 220 | |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 221 | static int tlb0_set_base(gva_t addr, int sets, int ways) |
| 222 | { |
| 223 | int set_base; |
| 224 | |
| 225 | set_base = (addr >> PAGE_SHIFT) & (sets - 1); |
| 226 | set_base *= ways; |
| 227 | |
| 228 | return set_base; |
| 229 | } |
| 230 | |
| 231 | static int gtlb0_set_base(struct kvmppc_vcpu_e500 *vcpu_e500, gva_t addr) |
| 232 | { |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 233 | return tlb0_set_base(addr, vcpu_e500->gtlb_params[0].sets, |
| 234 | vcpu_e500->gtlb_params[0].ways); |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 235 | } |
| 236 | |
Scott Wood | b590497 | 2011-11-08 18:23:30 -0600 | [diff] [blame] | 237 | static unsigned int get_tlb_esel(struct kvm_vcpu *vcpu, int tlbsel) |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 238 | { |
Scott Wood | b590497 | 2011-11-08 18:23:30 -0600 | [diff] [blame] | 239 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
| 240 | int esel = get_tlb_esel_bit(vcpu); |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 241 | |
| 242 | if (tlbsel == 0) { |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 243 | esel &= vcpu_e500->gtlb_params[0].ways - 1; |
Scott Wood | b590497 | 2011-11-08 18:23:30 -0600 | [diff] [blame] | 244 | esel += gtlb0_set_base(vcpu_e500, vcpu->arch.shared->mas2); |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 245 | } else { |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 246 | esel &= vcpu_e500->gtlb_params[tlbsel].entries - 1; |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | return esel; |
| 250 | } |
| 251 | |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 252 | /* Search the guest TLB for a matching entry. */ |
| 253 | static int kvmppc_e500_tlb_index(struct kvmppc_vcpu_e500 *vcpu_e500, |
| 254 | gva_t eaddr, int tlbsel, unsigned int pid, int as) |
| 255 | { |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 256 | int size = vcpu_e500->gtlb_params[tlbsel].entries; |
| 257 | unsigned int set_base, offset; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 258 | int i; |
| 259 | |
Scott Wood | 1aee47a | 2011-06-14 18:35:20 -0500 | [diff] [blame] | 260 | if (tlbsel == 0) { |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 261 | set_base = gtlb0_set_base(vcpu_e500, eaddr); |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 262 | size = vcpu_e500->gtlb_params[0].ways; |
Scott Wood | 1aee47a | 2011-06-14 18:35:20 -0500 | [diff] [blame] | 263 | } else { |
| 264 | set_base = 0; |
| 265 | } |
| 266 | |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 267 | offset = vcpu_e500->gtlb_offset[tlbsel]; |
| 268 | |
Scott Wood | 1aee47a | 2011-06-14 18:35:20 -0500 | [diff] [blame] | 269 | for (i = 0; i < size; i++) { |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 270 | struct kvm_book3e_206_tlb_entry *tlbe = |
| 271 | &vcpu_e500->gtlb_arch[offset + set_base + i]; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 272 | unsigned int tid; |
| 273 | |
| 274 | if (eaddr < get_tlb_eaddr(tlbe)) |
| 275 | continue; |
| 276 | |
| 277 | if (eaddr > get_tlb_end(tlbe)) |
| 278 | continue; |
| 279 | |
| 280 | tid = get_tlb_tid(tlbe); |
| 281 | if (tid && (tid != pid)) |
| 282 | continue; |
| 283 | |
| 284 | if (!get_tlb_v(tlbe)) |
| 285 | continue; |
| 286 | |
| 287 | if (get_tlb_ts(tlbe) != as && as != -1) |
| 288 | continue; |
| 289 | |
Scott Wood | 1aee47a | 2011-06-14 18:35:20 -0500 | [diff] [blame] | 290 | return set_base + i; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | return -1; |
| 294 | } |
| 295 | |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 296 | static inline void kvmppc_e500_ref_setup(struct tlbe_ref *ref, |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 297 | struct kvm_book3e_206_tlb_entry *gtlbe, |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 298 | pfn_t pfn) |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 299 | { |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 300 | ref->pfn = pfn; |
| 301 | ref->flags = E500_TLB_VALID; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 302 | |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame] | 303 | if (tlbe_is_writable(gtlbe)) |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 304 | ref->flags |= E500_TLB_DIRTY; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 305 | } |
| 306 | |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 307 | static inline void kvmppc_e500_ref_release(struct tlbe_ref *ref) |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 308 | { |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 309 | if (ref->flags & E500_TLB_VALID) { |
| 310 | if (ref->flags & E500_TLB_DIRTY) |
| 311 | kvm_release_pfn_dirty(ref->pfn); |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame] | 312 | else |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 313 | kvm_release_pfn_clean(ref->pfn); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 314 | |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 315 | ref->flags = 0; |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame] | 316 | } |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 317 | } |
| 318 | |
Scott Wood | 4f802fe | 2011-12-20 15:34:37 +0000 | [diff] [blame] | 319 | static void clear_tlb1_bitmap(struct kvmppc_vcpu_e500 *vcpu_e500) |
| 320 | { |
| 321 | if (vcpu_e500->g2h_tlb1_map) |
| 322 | memset(vcpu_e500->g2h_tlb1_map, |
| 323 | sizeof(u64) * vcpu_e500->gtlb_params[1].entries, 0); |
| 324 | if (vcpu_e500->h2g_tlb1_rmap) |
| 325 | memset(vcpu_e500->h2g_tlb1_rmap, |
| 326 | sizeof(unsigned int) * host_tlb_params[1].entries, 0); |
| 327 | } |
| 328 | |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 329 | static void clear_tlb_privs(struct kvmppc_vcpu_e500 *vcpu_e500) |
| 330 | { |
| 331 | int tlbsel = 0; |
| 332 | int i; |
| 333 | |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 334 | for (i = 0; i < vcpu_e500->gtlb_params[tlbsel].entries; i++) { |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 335 | struct tlbe_ref *ref = |
| 336 | &vcpu_e500->gtlb_priv[tlbsel][i].ref; |
| 337 | kvmppc_e500_ref_release(ref); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | static void clear_tlb_refs(struct kvmppc_vcpu_e500 *vcpu_e500) |
| 342 | { |
| 343 | int stlbsel = 1; |
| 344 | int i; |
| 345 | |
Scott Wood | 8fdd21a2 | 2011-12-20 15:34:34 +0000 | [diff] [blame] | 346 | kvmppc_e500_tlbil_all(vcpu_e500); |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 347 | |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 348 | for (i = 0; i < host_tlb_params[stlbsel].entries; i++) { |
| 349 | struct tlbe_ref *ref = |
| 350 | &vcpu_e500->tlb_refs[stlbsel][i]; |
| 351 | kvmppc_e500_ref_release(ref); |
| 352 | } |
| 353 | |
| 354 | clear_tlb_privs(vcpu_e500); |
| 355 | } |
| 356 | |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 357 | static inline void kvmppc_e500_deliver_tlb_miss(struct kvm_vcpu *vcpu, |
| 358 | unsigned int eaddr, int as) |
| 359 | { |
| 360 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
Scott Wood | 8fdd21a2 | 2011-12-20 15:34:34 +0000 | [diff] [blame] | 361 | unsigned int victim, tsized; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 362 | int tlbsel; |
| 363 | |
Liu Yu | fb2838d | 2009-01-14 10:47:37 -0600 | [diff] [blame] | 364 | /* since we only have two TLBs, only lower bit is used. */ |
Scott Wood | b590497 | 2011-11-08 18:23:30 -0600 | [diff] [blame] | 365 | tlbsel = (vcpu->arch.shared->mas4 >> 28) & 0x1; |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 366 | victim = (tlbsel == 0) ? gtlb0_get_next_victim(vcpu_e500) : 0; |
Scott Wood | b590497 | 2011-11-08 18:23:30 -0600 | [diff] [blame] | 367 | tsized = (vcpu->arch.shared->mas4 >> 7) & 0x1f; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 368 | |
Scott Wood | b590497 | 2011-11-08 18:23:30 -0600 | [diff] [blame] | 369 | vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim) |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame] | 370 | | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]); |
Scott Wood | b590497 | 2011-11-08 18:23:30 -0600 | [diff] [blame] | 371 | vcpu->arch.shared->mas1 = MAS1_VALID | (as ? MAS1_TS : 0) |
Scott Wood | 8fdd21a2 | 2011-12-20 15:34:34 +0000 | [diff] [blame] | 372 | | MAS1_TID(get_tlbmiss_tid(vcpu)) |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 373 | | MAS1_TSIZE(tsized); |
Scott Wood | b590497 | 2011-11-08 18:23:30 -0600 | [diff] [blame] | 374 | vcpu->arch.shared->mas2 = (eaddr & MAS2_EPN) |
| 375 | | (vcpu->arch.shared->mas4 & MAS2_ATTRIB_MASK); |
| 376 | vcpu->arch.shared->mas7_3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3; |
| 377 | vcpu->arch.shared->mas6 = (vcpu->arch.shared->mas6 & MAS6_SPID1) |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 378 | | (get_cur_pid(vcpu) << 16) |
| 379 | | (as ? MAS6_SAS : 0); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 380 | } |
| 381 | |
Scott Wood | 3bf3cdc | 2011-08-18 15:25:14 -0500 | [diff] [blame] | 382 | /* TID must be supplied by the caller */ |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 383 | static inline void kvmppc_e500_setup_stlbe( |
Scott Wood | 8fdd21a2 | 2011-12-20 15:34:34 +0000 | [diff] [blame] | 384 | struct kvm_vcpu *vcpu, |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 385 | struct kvm_book3e_206_tlb_entry *gtlbe, |
| 386 | int tsize, struct tlbe_ref *ref, u64 gvaddr, |
| 387 | struct kvm_book3e_206_tlb_entry *stlbe) |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame] | 388 | { |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 389 | pfn_t pfn = ref->pfn; |
Scott Wood | 8fdd21a2 | 2011-12-20 15:34:34 +0000 | [diff] [blame] | 390 | u32 pr = vcpu->arch.shared->msr & MSR_PR; |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 391 | |
| 392 | BUG_ON(!(ref->flags & E500_TLB_VALID)); |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame] | 393 | |
Scott Wood | 8fdd21a2 | 2011-12-20 15:34:34 +0000 | [diff] [blame] | 394 | /* Force IPROT=0 for all guest mappings. */ |
| 395 | stlbe->mas1 = MAS1_TSIZE(tsize) | get_tlb_sts(gtlbe) | MAS1_VALID; |
| 396 | stlbe->mas2 = (gvaddr & MAS2_EPN) | |
| 397 | e500_shadow_mas2_attrib(gtlbe->mas2, pr); |
| 398 | stlbe->mas7_3 = ((u64)pfn << PAGE_SHIFT) | |
| 399 | e500_shadow_mas3_attrib(gtlbe->mas7_3, pr); |
Scott Wood | 73196cd | 2011-12-20 15:34:47 +0000 | [diff] [blame] | 400 | |
| 401 | #ifdef CONFIG_KVM_BOOKE_HV |
| 402 | stlbe->mas8 = MAS8_TGS | vcpu->kvm->arch.lpid; |
| 403 | #endif |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame] | 404 | } |
| 405 | |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 406 | static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500, |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 407 | u64 gvaddr, gfn_t gfn, struct kvm_book3e_206_tlb_entry *gtlbe, |
Scott Wood | 57013524 | 2011-11-29 10:40:23 +0000 | [diff] [blame] | 408 | int tlbsel, struct kvm_book3e_206_tlb_entry *stlbe, |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 409 | struct tlbe_ref *ref) |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 410 | { |
Scott Wood | 9973d54 | 2011-06-14 18:34:39 -0500 | [diff] [blame] | 411 | struct kvm_memory_slot *slot; |
Scott Wood | 9973d54 | 2011-06-14 18:34:39 -0500 | [diff] [blame] | 412 | unsigned long pfn, hva; |
| 413 | int pfnmap = 0; |
| 414 | int tsize = BOOK3E_PAGESZ_4K; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 415 | |
Scott Wood | 59c1f4e | 2011-06-14 18:34:37 -0500 | [diff] [blame] | 416 | /* |
| 417 | * Translate guest physical to true physical, acquiring |
| 418 | * a page reference if it is normal, non-reserved memory. |
Scott Wood | 9973d54 | 2011-06-14 18:34:39 -0500 | [diff] [blame] | 419 | * |
| 420 | * gfn_to_memslot() must succeed because otherwise we wouldn't |
| 421 | * have gotten this far. Eventually we should just pass the slot |
| 422 | * pointer through from the first lookup. |
Scott Wood | 59c1f4e | 2011-06-14 18:34:37 -0500 | [diff] [blame] | 423 | */ |
Scott Wood | 9973d54 | 2011-06-14 18:34:39 -0500 | [diff] [blame] | 424 | slot = gfn_to_memslot(vcpu_e500->vcpu.kvm, gfn); |
| 425 | hva = gfn_to_hva_memslot(slot, gfn); |
| 426 | |
| 427 | if (tlbsel == 1) { |
| 428 | struct vm_area_struct *vma; |
| 429 | down_read(¤t->mm->mmap_sem); |
| 430 | |
| 431 | vma = find_vma(current->mm, hva); |
| 432 | if (vma && hva >= vma->vm_start && |
| 433 | (vma->vm_flags & VM_PFNMAP)) { |
| 434 | /* |
| 435 | * This VMA is a physically contiguous region (e.g. |
| 436 | * /dev/mem) that bypasses normal Linux page |
| 437 | * management. Find the overlap between the |
| 438 | * vma and the memslot. |
| 439 | */ |
| 440 | |
| 441 | unsigned long start, end; |
| 442 | unsigned long slot_start, slot_end; |
| 443 | |
| 444 | pfnmap = 1; |
| 445 | |
| 446 | start = vma->vm_pgoff; |
| 447 | end = start + |
| 448 | ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT); |
| 449 | |
| 450 | pfn = start + ((hva - vma->vm_start) >> PAGE_SHIFT); |
| 451 | |
| 452 | slot_start = pfn - (gfn - slot->base_gfn); |
| 453 | slot_end = slot_start + slot->npages; |
| 454 | |
| 455 | if (start < slot_start) |
| 456 | start = slot_start; |
| 457 | if (end > slot_end) |
| 458 | end = slot_end; |
| 459 | |
| 460 | tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >> |
| 461 | MAS1_TSIZE_SHIFT; |
| 462 | |
| 463 | /* |
| 464 | * e500 doesn't implement the lowest tsize bit, |
| 465 | * or 1K pages. |
| 466 | */ |
| 467 | tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1); |
| 468 | |
| 469 | /* |
| 470 | * Now find the largest tsize (up to what the guest |
| 471 | * requested) that will cover gfn, stay within the |
| 472 | * range, and for which gfn and pfn are mutually |
| 473 | * aligned. |
| 474 | */ |
| 475 | |
| 476 | for (; tsize > BOOK3E_PAGESZ_4K; tsize -= 2) { |
| 477 | unsigned long gfn_start, gfn_end, tsize_pages; |
| 478 | tsize_pages = 1 << (tsize - 2); |
| 479 | |
| 480 | gfn_start = gfn & ~(tsize_pages - 1); |
| 481 | gfn_end = gfn_start + tsize_pages; |
| 482 | |
| 483 | if (gfn_start + pfn - gfn < start) |
| 484 | continue; |
| 485 | if (gfn_end + pfn - gfn > end) |
| 486 | continue; |
| 487 | if ((gfn & (tsize_pages - 1)) != |
| 488 | (pfn & (tsize_pages - 1))) |
| 489 | continue; |
| 490 | |
| 491 | gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1); |
| 492 | pfn &= ~(tsize_pages - 1); |
| 493 | break; |
| 494 | } |
Alexander Graf | 95325e6 | 2011-09-20 01:31:48 +0200 | [diff] [blame] | 495 | } else if (vma && hva >= vma->vm_start && |
| 496 | (vma->vm_flags & VM_HUGETLB)) { |
| 497 | unsigned long psize = vma_kernel_pagesize(vma); |
| 498 | |
| 499 | tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >> |
| 500 | MAS1_TSIZE_SHIFT; |
| 501 | |
| 502 | /* |
| 503 | * Take the largest page size that satisfies both host |
| 504 | * and guest mapping |
| 505 | */ |
| 506 | tsize = min(__ilog2(psize) - 10, tsize); |
| 507 | |
| 508 | /* |
| 509 | * e500 doesn't implement the lowest tsize bit, |
| 510 | * or 1K pages. |
| 511 | */ |
| 512 | tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1); |
Scott Wood | 9973d54 | 2011-06-14 18:34:39 -0500 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | up_read(¤t->mm->mmap_sem); |
| 516 | } |
| 517 | |
| 518 | if (likely(!pfnmap)) { |
Alexander Graf | 95325e6 | 2011-09-20 01:31:48 +0200 | [diff] [blame] | 519 | unsigned long tsize_pages = 1 << (tsize + 10 - PAGE_SHIFT); |
Scott Wood | 9973d54 | 2011-06-14 18:34:39 -0500 | [diff] [blame] | 520 | pfn = gfn_to_pfn_memslot(vcpu_e500->vcpu.kvm, slot, gfn); |
| 521 | if (is_error_pfn(pfn)) { |
| 522 | printk(KERN_ERR "Couldn't get real page for gfn %lx!\n", |
| 523 | (long)gfn); |
| 524 | kvm_release_pfn_clean(pfn); |
| 525 | return; |
| 526 | } |
Alexander Graf | 95325e6 | 2011-09-20 01:31:48 +0200 | [diff] [blame] | 527 | |
| 528 | /* Align guest and physical address to page map boundaries */ |
| 529 | pfn &= ~(tsize_pages - 1); |
| 530 | gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 531 | } |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 532 | |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 533 | /* Drop old ref and setup new one. */ |
| 534 | kvmppc_e500_ref_release(ref); |
| 535 | kvmppc_e500_ref_setup(ref, gtlbe, pfn); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 536 | |
Scott Wood | 8fdd21a2 | 2011-12-20 15:34:34 +0000 | [diff] [blame] | 537 | kvmppc_e500_setup_stlbe(&vcpu_e500->vcpu, gtlbe, tsize, |
| 538 | ref, gvaddr, stlbe); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | /* XXX only map the one-one case, for now use TLB0 */ |
Scott Wood | 57013524 | 2011-11-29 10:40:23 +0000 | [diff] [blame] | 542 | static void kvmppc_e500_tlb0_map(struct kvmppc_vcpu_e500 *vcpu_e500, |
| 543 | int esel, |
| 544 | struct kvm_book3e_206_tlb_entry *stlbe) |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 545 | { |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 546 | struct kvm_book3e_206_tlb_entry *gtlbe; |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 547 | struct tlbe_ref *ref; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 548 | |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 549 | gtlbe = get_entry(vcpu_e500, 0, esel); |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 550 | ref = &vcpu_e500->gtlb_priv[0][esel].ref; |
| 551 | |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 552 | kvmppc_e500_shadow_map(vcpu_e500, get_tlb_eaddr(gtlbe), |
| 553 | get_tlb_raddr(gtlbe) >> PAGE_SHIFT, |
Scott Wood | 57013524 | 2011-11-29 10:40:23 +0000 | [diff] [blame] | 554 | gtlbe, 0, stlbe, ref); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | /* Caller must ensure that the specified guest TLB entry is safe to insert into |
| 558 | * the shadow TLB. */ |
| 559 | /* XXX for both one-one and one-to-many , for now use TLB1 */ |
| 560 | static int kvmppc_e500_tlb1_map(struct kvmppc_vcpu_e500 *vcpu_e500, |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 561 | u64 gvaddr, gfn_t gfn, struct kvm_book3e_206_tlb_entry *gtlbe, |
Scott Wood | 4f802fe | 2011-12-20 15:34:37 +0000 | [diff] [blame] | 562 | struct kvm_book3e_206_tlb_entry *stlbe, int esel) |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 563 | { |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 564 | struct tlbe_ref *ref; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 565 | unsigned int victim; |
| 566 | |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 567 | victim = vcpu_e500->host_tlb1_nv++; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 568 | |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 569 | if (unlikely(vcpu_e500->host_tlb1_nv >= tlb1_max_shadow_size())) |
| 570 | vcpu_e500->host_tlb1_nv = 0; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 571 | |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 572 | ref = &vcpu_e500->tlb_refs[1][victim]; |
Scott Wood | 57013524 | 2011-11-29 10:40:23 +0000 | [diff] [blame] | 573 | kvmppc_e500_shadow_map(vcpu_e500, gvaddr, gfn, gtlbe, 1, stlbe, ref); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 574 | |
Scott Wood | 4f802fe | 2011-12-20 15:34:37 +0000 | [diff] [blame] | 575 | vcpu_e500->g2h_tlb1_map[esel] |= (u64)1 << victim; |
| 576 | vcpu_e500->gtlb_priv[1][esel].ref.flags |= E500_TLB_BITMAP; |
| 577 | if (vcpu_e500->h2g_tlb1_rmap[victim]) { |
| 578 | unsigned int idx = vcpu_e500->h2g_tlb1_rmap[victim]; |
| 579 | vcpu_e500->g2h_tlb1_map[idx] &= ~(1ULL << victim); |
| 580 | } |
| 581 | vcpu_e500->h2g_tlb1_rmap[victim] = esel; |
| 582 | |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 583 | return victim; |
| 584 | } |
| 585 | |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame] | 586 | static inline int kvmppc_e500_gtlbe_invalidate( |
| 587 | struct kvmppc_vcpu_e500 *vcpu_e500, |
| 588 | int tlbsel, int esel) |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 589 | { |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 590 | struct kvm_book3e_206_tlb_entry *gtlbe = |
| 591 | get_entry(vcpu_e500, tlbsel, esel); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 592 | |
| 593 | if (unlikely(get_tlb_iprot(gtlbe))) |
| 594 | return -1; |
| 595 | |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 596 | gtlbe->mas1 = 0; |
| 597 | |
| 598 | return 0; |
| 599 | } |
| 600 | |
Liu Yu | b0a1835 | 2009-02-17 16:52:08 +0800 | [diff] [blame] | 601 | int kvmppc_e500_emul_mt_mmucsr0(struct kvmppc_vcpu_e500 *vcpu_e500, ulong value) |
| 602 | { |
| 603 | int esel; |
| 604 | |
| 605 | if (value & MMUCSR0_TLB0FI) |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 606 | for (esel = 0; esel < vcpu_e500->gtlb_params[0].entries; esel++) |
Liu Yu | b0a1835 | 2009-02-17 16:52:08 +0800 | [diff] [blame] | 607 | kvmppc_e500_gtlbe_invalidate(vcpu_e500, 0, esel); |
| 608 | if (value & MMUCSR0_TLB1FI) |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 609 | for (esel = 0; esel < vcpu_e500->gtlb_params[1].entries; esel++) |
Liu Yu | b0a1835 | 2009-02-17 16:52:08 +0800 | [diff] [blame] | 610 | kvmppc_e500_gtlbe_invalidate(vcpu_e500, 1, esel); |
| 611 | |
Liu Yu | dd9ebf1f | 2011-06-14 18:35:14 -0500 | [diff] [blame] | 612 | /* Invalidate all vcpu id mappings */ |
Scott Wood | 8fdd21a2 | 2011-12-20 15:34:34 +0000 | [diff] [blame] | 613 | kvmppc_e500_tlbil_all(vcpu_e500); |
Liu Yu | b0a1835 | 2009-02-17 16:52:08 +0800 | [diff] [blame] | 614 | |
| 615 | return EMULATE_DONE; |
| 616 | } |
| 617 | |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 618 | int kvmppc_e500_emul_tlbivax(struct kvm_vcpu *vcpu, int ra, int rb) |
| 619 | { |
| 620 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
| 621 | unsigned int ia; |
| 622 | int esel, tlbsel; |
| 623 | gva_t ea; |
| 624 | |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 625 | 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] | 626 | |
| 627 | ia = (ea >> 2) & 0x1; |
| 628 | |
Liu Yu | fb2838d | 2009-01-14 10:47:37 -0600 | [diff] [blame] | 629 | /* since we only have two TLBs, only lower bit is used. */ |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 630 | tlbsel = (ea >> 3) & 0x1; |
| 631 | |
| 632 | if (ia) { |
| 633 | /* invalidate all entries */ |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 634 | for (esel = 0; esel < vcpu_e500->gtlb_params[tlbsel].entries; |
| 635 | esel++) |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 636 | kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel); |
| 637 | } else { |
| 638 | ea &= 0xfffff000; |
| 639 | esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, |
| 640 | get_cur_pid(vcpu), -1); |
| 641 | if (esel >= 0) |
| 642 | kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel); |
| 643 | } |
| 644 | |
Liu Yu | dd9ebf1f | 2011-06-14 18:35:14 -0500 | [diff] [blame] | 645 | /* Invalidate all vcpu id mappings */ |
Scott Wood | 8fdd21a2 | 2011-12-20 15:34:34 +0000 | [diff] [blame] | 646 | kvmppc_e500_tlbil_all(vcpu_e500); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 647 | |
| 648 | return EMULATE_DONE; |
| 649 | } |
| 650 | |
Scott Wood | ab9fc40 | 2011-12-20 15:34:39 +0000 | [diff] [blame] | 651 | static void tlbilx_all(struct kvmppc_vcpu_e500 *vcpu_e500, int tlbsel, |
| 652 | int pid, int rt) |
| 653 | { |
| 654 | struct kvm_book3e_206_tlb_entry *tlbe; |
| 655 | int tid, esel; |
| 656 | |
| 657 | /* invalidate all entries */ |
| 658 | for (esel = 0; esel < vcpu_e500->gtlb_params[tlbsel].entries; esel++) { |
| 659 | tlbe = get_entry(vcpu_e500, tlbsel, esel); |
| 660 | tid = get_tlb_tid(tlbe); |
| 661 | if (rt == 0 || tid == pid) { |
| 662 | inval_gtlbe_on_host(vcpu_e500, tlbsel, esel); |
| 663 | kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel); |
| 664 | } |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | static void tlbilx_one(struct kvmppc_vcpu_e500 *vcpu_e500, int pid, |
| 669 | int ra, int rb) |
| 670 | { |
| 671 | int tlbsel, esel; |
| 672 | gva_t ea; |
| 673 | |
| 674 | ea = kvmppc_get_gpr(&vcpu_e500->vcpu, rb); |
| 675 | if (ra) |
| 676 | ea += kvmppc_get_gpr(&vcpu_e500->vcpu, ra); |
| 677 | |
| 678 | for (tlbsel = 0; tlbsel < 2; tlbsel++) { |
| 679 | esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, -1); |
| 680 | if (esel >= 0) { |
| 681 | inval_gtlbe_on_host(vcpu_e500, tlbsel, esel); |
| 682 | kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel); |
| 683 | break; |
| 684 | } |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | int kvmppc_e500_emul_tlbilx(struct kvm_vcpu *vcpu, int rt, int ra, int rb) |
| 689 | { |
| 690 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
| 691 | int pid = get_cur_spid(vcpu); |
| 692 | |
| 693 | if (rt == 0 || rt == 1) { |
| 694 | tlbilx_all(vcpu_e500, 0, pid, rt); |
| 695 | tlbilx_all(vcpu_e500, 1, pid, rt); |
| 696 | } else if (rt == 3) { |
| 697 | tlbilx_one(vcpu_e500, pid, ra, rb); |
| 698 | } |
| 699 | |
| 700 | return EMULATE_DONE; |
| 701 | } |
| 702 | |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 703 | int kvmppc_e500_emul_tlbre(struct kvm_vcpu *vcpu) |
| 704 | { |
| 705 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
| 706 | int tlbsel, esel; |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 707 | struct kvm_book3e_206_tlb_entry *gtlbe; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 708 | |
Scott Wood | b590497 | 2011-11-08 18:23:30 -0600 | [diff] [blame] | 709 | tlbsel = get_tlb_tlbsel(vcpu); |
| 710 | esel = get_tlb_esel(vcpu, tlbsel); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 711 | |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 712 | gtlbe = get_entry(vcpu_e500, tlbsel, esel); |
Scott Wood | b590497 | 2011-11-08 18:23:30 -0600 | [diff] [blame] | 713 | vcpu->arch.shared->mas0 &= ~MAS0_NV(~0); |
| 714 | vcpu->arch.shared->mas0 |= MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]); |
| 715 | vcpu->arch.shared->mas1 = gtlbe->mas1; |
| 716 | vcpu->arch.shared->mas2 = gtlbe->mas2; |
| 717 | vcpu->arch.shared->mas7_3 = gtlbe->mas7_3; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 718 | |
| 719 | return EMULATE_DONE; |
| 720 | } |
| 721 | |
| 722 | int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *vcpu, int rb) |
| 723 | { |
| 724 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
Scott Wood | b590497 | 2011-11-08 18:23:30 -0600 | [diff] [blame] | 725 | int as = !!get_cur_sas(vcpu); |
| 726 | unsigned int pid = get_cur_spid(vcpu); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 727 | int esel, tlbsel; |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 728 | struct kvm_book3e_206_tlb_entry *gtlbe = NULL; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 729 | gva_t ea; |
| 730 | |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 731 | ea = kvmppc_get_gpr(vcpu, rb); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 732 | |
| 733 | for (tlbsel = 0; tlbsel < 2; tlbsel++) { |
| 734 | esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, as); |
| 735 | if (esel >= 0) { |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 736 | gtlbe = get_entry(vcpu_e500, tlbsel, esel); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 737 | break; |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | if (gtlbe) { |
Scott Wood | 303b7c9 | 2011-08-18 15:25:23 -0500 | [diff] [blame] | 742 | esel &= vcpu_e500->gtlb_params[tlbsel].ways - 1; |
| 743 | |
Scott Wood | b590497 | 2011-11-08 18:23:30 -0600 | [diff] [blame] | 744 | vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(esel) |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame] | 745 | | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]); |
Scott Wood | b590497 | 2011-11-08 18:23:30 -0600 | [diff] [blame] | 746 | vcpu->arch.shared->mas1 = gtlbe->mas1; |
| 747 | vcpu->arch.shared->mas2 = gtlbe->mas2; |
| 748 | vcpu->arch.shared->mas7_3 = gtlbe->mas7_3; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 749 | } else { |
| 750 | int victim; |
| 751 | |
Liu Yu | fb2838d | 2009-01-14 10:47:37 -0600 | [diff] [blame] | 752 | /* since we only have two TLBs, only lower bit is used. */ |
Scott Wood | b590497 | 2011-11-08 18:23:30 -0600 | [diff] [blame] | 753 | tlbsel = vcpu->arch.shared->mas4 >> 28 & 0x1; |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 754 | victim = (tlbsel == 0) ? gtlb0_get_next_victim(vcpu_e500) : 0; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 755 | |
Scott Wood | b590497 | 2011-11-08 18:23:30 -0600 | [diff] [blame] | 756 | vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel) |
| 757 | | MAS0_ESEL(victim) |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame] | 758 | | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]); |
Scott Wood | b590497 | 2011-11-08 18:23:30 -0600 | [diff] [blame] | 759 | vcpu->arch.shared->mas1 = |
| 760 | (vcpu->arch.shared->mas6 & MAS6_SPID0) |
| 761 | | (vcpu->arch.shared->mas6 & (MAS6_SAS ? MAS1_TS : 0)) |
| 762 | | (vcpu->arch.shared->mas4 & MAS4_TSIZED(~0)); |
| 763 | vcpu->arch.shared->mas2 &= MAS2_EPN; |
| 764 | vcpu->arch.shared->mas2 |= vcpu->arch.shared->mas4 & |
| 765 | MAS2_ATTRIB_MASK; |
| 766 | vcpu->arch.shared->mas7_3 &= MAS3_U0 | MAS3_U1 | |
| 767 | MAS3_U2 | MAS3_U3; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 768 | } |
| 769 | |
Scott Wood | 49ea069 | 2011-03-28 15:01:24 -0500 | [diff] [blame] | 770 | kvmppc_set_exit_type(vcpu, EMULATED_TLBSX_EXITS); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 771 | return EMULATE_DONE; |
| 772 | } |
| 773 | |
Scott Wood | 57013524 | 2011-11-29 10:40:23 +0000 | [diff] [blame] | 774 | /* sesel is for tlb1 only */ |
Scott Wood | 3bf3cdc | 2011-08-18 15:25:14 -0500 | [diff] [blame] | 775 | static void write_stlbe(struct kvmppc_vcpu_e500 *vcpu_e500, |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 776 | struct kvm_book3e_206_tlb_entry *gtlbe, |
| 777 | struct kvm_book3e_206_tlb_entry *stlbe, |
Scott Wood | 3bf3cdc | 2011-08-18 15:25:14 -0500 | [diff] [blame] | 778 | int stlbsel, int sesel) |
| 779 | { |
| 780 | int stid; |
| 781 | |
| 782 | preempt_disable(); |
Scott Wood | 8fdd21a2 | 2011-12-20 15:34:34 +0000 | [diff] [blame] | 783 | stid = kvmppc_e500_get_tlb_stid(&vcpu_e500->vcpu, gtlbe); |
Scott Wood | 3bf3cdc | 2011-08-18 15:25:14 -0500 | [diff] [blame] | 784 | |
| 785 | stlbe->mas1 |= MAS1_TID(stid); |
| 786 | write_host_tlbe(vcpu_e500, stlbsel, sesel, stlbe); |
| 787 | preempt_enable(); |
| 788 | } |
| 789 | |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 790 | int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *vcpu) |
| 791 | { |
| 792 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
Scott Wood | 8fdd21a2 | 2011-12-20 15:34:34 +0000 | [diff] [blame] | 793 | struct kvm_book3e_206_tlb_entry *gtlbe, stlbe; |
| 794 | int tlbsel, esel, stlbsel, sesel; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 795 | |
Scott Wood | b590497 | 2011-11-08 18:23:30 -0600 | [diff] [blame] | 796 | tlbsel = get_tlb_tlbsel(vcpu); |
| 797 | esel = get_tlb_esel(vcpu, tlbsel); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 798 | |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 799 | gtlbe = get_entry(vcpu_e500, tlbsel, esel); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 800 | |
Liu Yu | dd9ebf1f | 2011-06-14 18:35:14 -0500 | [diff] [blame] | 801 | if (get_tlb_v(gtlbe)) |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 802 | inval_gtlbe_on_host(vcpu_e500, tlbsel, esel); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 803 | |
Scott Wood | b590497 | 2011-11-08 18:23:30 -0600 | [diff] [blame] | 804 | gtlbe->mas1 = vcpu->arch.shared->mas1; |
| 805 | gtlbe->mas2 = vcpu->arch.shared->mas2; |
| 806 | gtlbe->mas7_3 = vcpu->arch.shared->mas7_3; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 807 | |
Liu Yu | d37b1a0 | 2011-12-20 14:42:56 +0000 | [diff] [blame] | 808 | trace_kvm_booke206_gtlb_write(vcpu->arch.shared->mas0, gtlbe->mas1, |
| 809 | gtlbe->mas2, gtlbe->mas7_3); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 810 | |
| 811 | /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */ |
| 812 | if (tlbe_is_host_safe(vcpu, gtlbe)) { |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame] | 813 | u64 eaddr; |
| 814 | u64 raddr; |
| 815 | |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 816 | switch (tlbsel) { |
| 817 | case 0: |
| 818 | /* TLB0 */ |
| 819 | gtlbe->mas1 &= ~MAS1_TSIZE(~0); |
Liu Yu | 0cfb50e | 2009-06-05 14:54:29 +0800 | [diff] [blame] | 820 | gtlbe->mas1 |= MAS1_TSIZE(BOOK3E_PAGESZ_4K); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 821 | |
| 822 | stlbsel = 0; |
Scott Wood | 57013524 | 2011-11-29 10:40:23 +0000 | [diff] [blame] | 823 | kvmppc_e500_tlb0_map(vcpu_e500, esel, &stlbe); |
| 824 | sesel = 0; /* unused */ |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 825 | |
| 826 | break; |
| 827 | |
| 828 | case 1: |
| 829 | /* TLB1 */ |
| 830 | eaddr = get_tlb_eaddr(gtlbe); |
| 831 | raddr = get_tlb_raddr(gtlbe); |
| 832 | |
| 833 | /* Create a 4KB mapping on the host. |
| 834 | * If the guest wanted a large page, |
| 835 | * only the first 4KB is mapped here and the rest |
| 836 | * are mapped on the fly. */ |
| 837 | stlbsel = 1; |
| 838 | sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr, |
Scott Wood | 4f802fe | 2011-12-20 15:34:37 +0000 | [diff] [blame] | 839 | raddr >> PAGE_SHIFT, gtlbe, &stlbe, esel); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 840 | break; |
| 841 | |
| 842 | default: |
| 843 | BUG(); |
| 844 | } |
Scott Wood | 3bf3cdc | 2011-08-18 15:25:14 -0500 | [diff] [blame] | 845 | |
| 846 | write_stlbe(vcpu_e500, gtlbe, &stlbe, stlbsel, sesel); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 847 | } |
| 848 | |
Scott Wood | 49ea069 | 2011-03-28 15:01:24 -0500 | [diff] [blame] | 849 | kvmppc_set_exit_type(vcpu, EMULATED_TLBWE_EXITS); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 850 | return EMULATE_DONE; |
| 851 | } |
| 852 | |
Scott Wood | 8fdd21a2 | 2011-12-20 15:34:34 +0000 | [diff] [blame] | 853 | static int kvmppc_e500_tlb_search(struct kvm_vcpu *vcpu, |
| 854 | gva_t eaddr, unsigned int pid, int as) |
| 855 | { |
| 856 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
| 857 | int esel, tlbsel; |
| 858 | |
| 859 | for (tlbsel = 0; tlbsel < 2; tlbsel++) { |
| 860 | esel = kvmppc_e500_tlb_index(vcpu_e500, eaddr, tlbsel, pid, as); |
| 861 | if (esel >= 0) |
| 862 | return index_of(tlbsel, esel); |
| 863 | } |
| 864 | |
| 865 | return -1; |
| 866 | } |
| 867 | |
| 868 | /* 'linear_address' is actually an encoding of AS|PID|EADDR . */ |
| 869 | int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu, |
| 870 | struct kvm_translation *tr) |
| 871 | { |
| 872 | int index; |
| 873 | gva_t eaddr; |
| 874 | u8 pid; |
| 875 | u8 as; |
| 876 | |
| 877 | eaddr = tr->linear_address; |
| 878 | pid = (tr->linear_address >> 32) & 0xff; |
| 879 | as = (tr->linear_address >> 40) & 0x1; |
| 880 | |
| 881 | index = kvmppc_e500_tlb_search(vcpu, eaddr, pid, as); |
| 882 | if (index < 0) { |
| 883 | tr->valid = 0; |
| 884 | return 0; |
| 885 | } |
| 886 | |
| 887 | tr->physical_address = kvmppc_mmu_xlate(vcpu, index, eaddr); |
| 888 | /* XXX what does "writeable" and "usermode" even mean? */ |
| 889 | tr->valid = 1; |
| 890 | |
| 891 | return 0; |
| 892 | } |
| 893 | |
| 894 | |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 895 | int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr) |
| 896 | { |
Alexander Graf | 666e725 | 2010-07-29 14:47:43 +0200 | [diff] [blame] | 897 | unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 898 | |
| 899 | return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as); |
| 900 | } |
| 901 | |
| 902 | int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr) |
| 903 | { |
Alexander Graf | 666e725 | 2010-07-29 14:47:43 +0200 | [diff] [blame] | 904 | unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 905 | |
| 906 | return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as); |
| 907 | } |
| 908 | |
| 909 | void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu) |
| 910 | { |
Alexander Graf | 666e725 | 2010-07-29 14:47:43 +0200 | [diff] [blame] | 911 | unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 912 | |
| 913 | kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.pc, as); |
| 914 | } |
| 915 | |
| 916 | void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu) |
| 917 | { |
Alexander Graf | 666e725 | 2010-07-29 14:47:43 +0200 | [diff] [blame] | 918 | unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 919 | |
| 920 | kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.fault_dear, as); |
| 921 | } |
| 922 | |
| 923 | gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int index, |
| 924 | gva_t eaddr) |
| 925 | { |
| 926 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 927 | struct kvm_book3e_206_tlb_entry *gtlbe; |
| 928 | u64 pgmask; |
| 929 | |
| 930 | gtlbe = get_entry(vcpu_e500, tlbsel_of(index), esel_of(index)); |
| 931 | pgmask = get_tlb_bytes(gtlbe) - 1; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 932 | |
| 933 | return get_tlb_raddr(gtlbe) | (eaddr & pgmask); |
| 934 | } |
| 935 | |
| 936 | void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu) |
| 937 | { |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 eaddr, gpa_t gpaddr, |
| 941 | unsigned int index) |
| 942 | { |
| 943 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame] | 944 | struct tlbe_priv *priv; |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 945 | struct kvm_book3e_206_tlb_entry *gtlbe, stlbe; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 946 | int tlbsel = tlbsel_of(index); |
| 947 | int esel = esel_of(index); |
| 948 | int stlbsel, sesel; |
| 949 | |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 950 | gtlbe = get_entry(vcpu_e500, tlbsel, esel); |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame] | 951 | |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 952 | switch (tlbsel) { |
| 953 | case 0: |
| 954 | stlbsel = 0; |
Scott Wood | 57013524 | 2011-11-29 10:40:23 +0000 | [diff] [blame] | 955 | sesel = 0; /* unused */ |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 956 | priv = &vcpu_e500->gtlb_priv[tlbsel][esel]; |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame] | 957 | |
Scott Wood | 8fdd21a2 | 2011-12-20 15:34:34 +0000 | [diff] [blame] | 958 | kvmppc_e500_setup_stlbe(vcpu, gtlbe, BOOK3E_PAGESZ_4K, |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 959 | &priv->ref, eaddr, &stlbe); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 960 | break; |
| 961 | |
| 962 | case 1: { |
| 963 | gfn_t gfn = gpaddr >> PAGE_SHIFT; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 964 | |
| 965 | stlbsel = 1; |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame] | 966 | sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr, gfn, |
Scott Wood | 4f802fe | 2011-12-20 15:34:37 +0000 | [diff] [blame] | 967 | gtlbe, &stlbe, esel); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 968 | break; |
| 969 | } |
| 970 | |
| 971 | default: |
| 972 | BUG(); |
| 973 | break; |
| 974 | } |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame] | 975 | |
Scott Wood | 3bf3cdc | 2011-08-18 15:25:14 -0500 | [diff] [blame] | 976 | write_stlbe(vcpu_e500, gtlbe, &stlbe, stlbsel, sesel); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 977 | } |
| 978 | |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 979 | static void free_gtlb(struct kvmppc_vcpu_e500 *vcpu_e500) |
| 980 | { |
| 981 | int i; |
| 982 | |
Scott Wood | 4f802fe | 2011-12-20 15:34:37 +0000 | [diff] [blame] | 983 | clear_tlb1_bitmap(vcpu_e500); |
| 984 | kfree(vcpu_e500->g2h_tlb1_map); |
| 985 | |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 986 | clear_tlb_refs(vcpu_e500); |
| 987 | kfree(vcpu_e500->gtlb_priv[0]); |
| 988 | kfree(vcpu_e500->gtlb_priv[1]); |
| 989 | |
| 990 | if (vcpu_e500->shared_tlb_pages) { |
| 991 | vfree((void *)(round_down((uintptr_t)vcpu_e500->gtlb_arch, |
| 992 | PAGE_SIZE))); |
| 993 | |
| 994 | for (i = 0; i < vcpu_e500->num_shared_tlb_pages; i++) { |
| 995 | set_page_dirty_lock(vcpu_e500->shared_tlb_pages[i]); |
| 996 | put_page(vcpu_e500->shared_tlb_pages[i]); |
| 997 | } |
| 998 | |
| 999 | vcpu_e500->num_shared_tlb_pages = 0; |
| 1000 | vcpu_e500->shared_tlb_pages = NULL; |
| 1001 | } else { |
| 1002 | kfree(vcpu_e500->gtlb_arch); |
| 1003 | } |
| 1004 | |
| 1005 | vcpu_e500->gtlb_arch = NULL; |
| 1006 | } |
| 1007 | |
Scott Wood | 8fdd21a2 | 2011-12-20 15:34:34 +0000 | [diff] [blame] | 1008 | void kvmppc_get_sregs_e500_tlb(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) |
| 1009 | { |
| 1010 | sregs->u.e.mas0 = vcpu->arch.shared->mas0; |
| 1011 | sregs->u.e.mas1 = vcpu->arch.shared->mas1; |
| 1012 | sregs->u.e.mas2 = vcpu->arch.shared->mas2; |
| 1013 | sregs->u.e.mas7_3 = vcpu->arch.shared->mas7_3; |
| 1014 | sregs->u.e.mas4 = vcpu->arch.shared->mas4; |
| 1015 | sregs->u.e.mas6 = vcpu->arch.shared->mas6; |
| 1016 | |
| 1017 | sregs->u.e.mmucfg = vcpu->arch.mmucfg; |
| 1018 | sregs->u.e.tlbcfg[0] = vcpu->arch.tlbcfg[0]; |
| 1019 | sregs->u.e.tlbcfg[1] = vcpu->arch.tlbcfg[1]; |
| 1020 | sregs->u.e.tlbcfg[2] = 0; |
| 1021 | sregs->u.e.tlbcfg[3] = 0; |
| 1022 | } |
| 1023 | |
| 1024 | int kvmppc_set_sregs_e500_tlb(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) |
| 1025 | { |
| 1026 | if (sregs->u.e.features & KVM_SREGS_E_ARCH206_MMU) { |
| 1027 | vcpu->arch.shared->mas0 = sregs->u.e.mas0; |
| 1028 | vcpu->arch.shared->mas1 = sregs->u.e.mas1; |
| 1029 | vcpu->arch.shared->mas2 = sregs->u.e.mas2; |
| 1030 | vcpu->arch.shared->mas7_3 = sregs->u.e.mas7_3; |
| 1031 | vcpu->arch.shared->mas4 = sregs->u.e.mas4; |
| 1032 | vcpu->arch.shared->mas6 = sregs->u.e.mas6; |
| 1033 | } |
| 1034 | |
| 1035 | return 0; |
| 1036 | } |
| 1037 | |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 1038 | int kvm_vcpu_ioctl_config_tlb(struct kvm_vcpu *vcpu, |
| 1039 | struct kvm_config_tlb *cfg) |
| 1040 | { |
| 1041 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
| 1042 | struct kvm_book3e_206_tlb_params params; |
| 1043 | char *virt; |
| 1044 | struct page **pages; |
| 1045 | struct tlbe_priv *privs[2] = {}; |
Scott Wood | 4f802fe | 2011-12-20 15:34:37 +0000 | [diff] [blame] | 1046 | u64 *g2h_bitmap = NULL; |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 1047 | size_t array_len; |
| 1048 | u32 sets; |
| 1049 | int num_pages, ret, i; |
| 1050 | |
| 1051 | if (cfg->mmu_type != KVM_MMU_FSL_BOOKE_NOHV) |
| 1052 | return -EINVAL; |
| 1053 | |
| 1054 | if (copy_from_user(¶ms, (void __user *)(uintptr_t)cfg->params, |
| 1055 | sizeof(params))) |
| 1056 | return -EFAULT; |
| 1057 | |
| 1058 | if (params.tlb_sizes[1] > 64) |
| 1059 | return -EINVAL; |
| 1060 | if (params.tlb_ways[1] != params.tlb_sizes[1]) |
| 1061 | return -EINVAL; |
| 1062 | if (params.tlb_sizes[2] != 0 || params.tlb_sizes[3] != 0) |
| 1063 | return -EINVAL; |
| 1064 | if (params.tlb_ways[2] != 0 || params.tlb_ways[3] != 0) |
| 1065 | return -EINVAL; |
| 1066 | |
| 1067 | if (!is_power_of_2(params.tlb_ways[0])) |
| 1068 | return -EINVAL; |
| 1069 | |
| 1070 | sets = params.tlb_sizes[0] >> ilog2(params.tlb_ways[0]); |
| 1071 | if (!is_power_of_2(sets)) |
| 1072 | return -EINVAL; |
| 1073 | |
| 1074 | array_len = params.tlb_sizes[0] + params.tlb_sizes[1]; |
| 1075 | array_len *= sizeof(struct kvm_book3e_206_tlb_entry); |
| 1076 | |
| 1077 | if (cfg->array_len < array_len) |
| 1078 | return -EINVAL; |
| 1079 | |
| 1080 | num_pages = DIV_ROUND_UP(cfg->array + array_len - 1, PAGE_SIZE) - |
| 1081 | cfg->array / PAGE_SIZE; |
| 1082 | pages = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL); |
| 1083 | if (!pages) |
| 1084 | return -ENOMEM; |
| 1085 | |
| 1086 | ret = get_user_pages_fast(cfg->array, num_pages, 1, pages); |
| 1087 | if (ret < 0) |
| 1088 | goto err_pages; |
| 1089 | |
| 1090 | if (ret != num_pages) { |
| 1091 | num_pages = ret; |
| 1092 | ret = -EFAULT; |
| 1093 | goto err_put_page; |
| 1094 | } |
| 1095 | |
| 1096 | virt = vmap(pages, num_pages, VM_MAP, PAGE_KERNEL); |
| 1097 | if (!virt) |
| 1098 | goto err_put_page; |
| 1099 | |
| 1100 | privs[0] = kzalloc(sizeof(struct tlbe_priv) * params.tlb_sizes[0], |
| 1101 | GFP_KERNEL); |
| 1102 | privs[1] = kzalloc(sizeof(struct tlbe_priv) * params.tlb_sizes[1], |
| 1103 | GFP_KERNEL); |
| 1104 | |
| 1105 | if (!privs[0] || !privs[1]) |
| 1106 | goto err_put_page; |
| 1107 | |
Scott Wood | 4f802fe | 2011-12-20 15:34:37 +0000 | [diff] [blame] | 1108 | g2h_bitmap = kzalloc(sizeof(u64) * params.tlb_sizes[1], |
| 1109 | GFP_KERNEL); |
| 1110 | if (!g2h_bitmap) |
| 1111 | goto err_put_page; |
| 1112 | |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 1113 | free_gtlb(vcpu_e500); |
| 1114 | |
| 1115 | vcpu_e500->gtlb_priv[0] = privs[0]; |
| 1116 | vcpu_e500->gtlb_priv[1] = privs[1]; |
Scott Wood | 4f802fe | 2011-12-20 15:34:37 +0000 | [diff] [blame] | 1117 | vcpu_e500->g2h_tlb1_map = g2h_bitmap; |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 1118 | |
| 1119 | vcpu_e500->gtlb_arch = (struct kvm_book3e_206_tlb_entry *) |
| 1120 | (virt + (cfg->array & (PAGE_SIZE - 1))); |
| 1121 | |
| 1122 | vcpu_e500->gtlb_params[0].entries = params.tlb_sizes[0]; |
| 1123 | vcpu_e500->gtlb_params[1].entries = params.tlb_sizes[1]; |
| 1124 | |
| 1125 | vcpu_e500->gtlb_offset[0] = 0; |
| 1126 | vcpu_e500->gtlb_offset[1] = params.tlb_sizes[0]; |
| 1127 | |
Scott Wood | 8fdd21a2 | 2011-12-20 15:34:34 +0000 | [diff] [blame] | 1128 | vcpu->arch.mmucfg = mfspr(SPRN_MMUCFG) & ~MMUCFG_LPIDSIZE; |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 1129 | |
Scott Wood | 8fdd21a2 | 2011-12-20 15:34:34 +0000 | [diff] [blame] | 1130 | vcpu->arch.tlbcfg[0] &= ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC); |
| 1131 | if (params.tlb_sizes[0] <= 2048) |
| 1132 | vcpu->arch.tlbcfg[0] |= params.tlb_sizes[0]; |
| 1133 | vcpu->arch.tlbcfg[0] |= params.tlb_ways[0] << TLBnCFG_ASSOC_SHIFT; |
| 1134 | |
| 1135 | vcpu->arch.tlbcfg[1] &= ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC); |
| 1136 | vcpu->arch.tlbcfg[1] |= params.tlb_sizes[1]; |
| 1137 | vcpu->arch.tlbcfg[1] |= params.tlb_ways[1] << TLBnCFG_ASSOC_SHIFT; |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 1138 | |
| 1139 | vcpu_e500->shared_tlb_pages = pages; |
| 1140 | vcpu_e500->num_shared_tlb_pages = num_pages; |
| 1141 | |
| 1142 | vcpu_e500->gtlb_params[0].ways = params.tlb_ways[0]; |
| 1143 | vcpu_e500->gtlb_params[0].sets = sets; |
| 1144 | |
| 1145 | vcpu_e500->gtlb_params[1].ways = params.tlb_sizes[1]; |
| 1146 | vcpu_e500->gtlb_params[1].sets = 1; |
| 1147 | |
| 1148 | return 0; |
| 1149 | |
| 1150 | err_put_page: |
| 1151 | kfree(privs[0]); |
| 1152 | kfree(privs[1]); |
| 1153 | |
| 1154 | for (i = 0; i < num_pages; i++) |
| 1155 | put_page(pages[i]); |
| 1156 | |
| 1157 | err_pages: |
| 1158 | kfree(pages); |
| 1159 | return ret; |
| 1160 | } |
| 1161 | |
| 1162 | int kvm_vcpu_ioctl_dirty_tlb(struct kvm_vcpu *vcpu, |
| 1163 | struct kvm_dirty_tlb *dirty) |
| 1164 | { |
| 1165 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
| 1166 | |
| 1167 | clear_tlb_refs(vcpu_e500); |
| 1168 | return 0; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *vcpu_e500) |
| 1172 | { |
Scott Wood | 8fdd21a2 | 2011-12-20 15:34:34 +0000 | [diff] [blame] | 1173 | struct kvm_vcpu *vcpu = &vcpu_e500->vcpu; |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 1174 | int entry_size = sizeof(struct kvm_book3e_206_tlb_entry); |
| 1175 | int entries = KVM_E500_TLB0_SIZE + KVM_E500_TLB1_SIZE; |
| 1176 | |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 1177 | host_tlb_params[0].entries = mfspr(SPRN_TLB0CFG) & TLBnCFG_N_ENTRY; |
| 1178 | host_tlb_params[1].entries = mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY; |
| 1179 | |
| 1180 | /* |
| 1181 | * This should never happen on real e500 hardware, but is |
| 1182 | * architecturally possible -- e.g. in some weird nested |
| 1183 | * virtualization case. |
| 1184 | */ |
| 1185 | if (host_tlb_params[0].entries == 0 || |
| 1186 | host_tlb_params[1].entries == 0) { |
| 1187 | pr_err("%s: need to know host tlb size\n", __func__); |
| 1188 | return -ENODEV; |
| 1189 | } |
| 1190 | |
| 1191 | host_tlb_params[0].ways = (mfspr(SPRN_TLB0CFG) & TLBnCFG_ASSOC) >> |
| 1192 | TLBnCFG_ASSOC_SHIFT; |
| 1193 | host_tlb_params[1].ways = host_tlb_params[1].entries; |
| 1194 | |
| 1195 | if (!is_power_of_2(host_tlb_params[0].entries) || |
| 1196 | !is_power_of_2(host_tlb_params[0].ways) || |
| 1197 | host_tlb_params[0].entries < host_tlb_params[0].ways || |
| 1198 | host_tlb_params[0].ways == 0) { |
| 1199 | pr_err("%s: bad tlb0 host config: %u entries %u ways\n", |
| 1200 | __func__, host_tlb_params[0].entries, |
| 1201 | host_tlb_params[0].ways); |
| 1202 | return -ENODEV; |
| 1203 | } |
| 1204 | |
| 1205 | host_tlb_params[0].sets = |
| 1206 | host_tlb_params[0].entries / host_tlb_params[0].ways; |
| 1207 | host_tlb_params[1].sets = 1; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 1208 | |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 1209 | vcpu_e500->gtlb_params[0].entries = KVM_E500_TLB0_SIZE; |
| 1210 | vcpu_e500->gtlb_params[1].entries = KVM_E500_TLB1_SIZE; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 1211 | |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 1212 | vcpu_e500->gtlb_params[0].ways = KVM_E500_TLB0_WAY_NUM; |
| 1213 | vcpu_e500->gtlb_params[0].sets = |
| 1214 | KVM_E500_TLB0_SIZE / KVM_E500_TLB0_WAY_NUM; |
| 1215 | |
| 1216 | vcpu_e500->gtlb_params[1].ways = KVM_E500_TLB1_SIZE; |
| 1217 | vcpu_e500->gtlb_params[1].sets = 1; |
| 1218 | |
| 1219 | vcpu_e500->gtlb_arch = kmalloc(entries * entry_size, GFP_KERNEL); |
| 1220 | if (!vcpu_e500->gtlb_arch) |
| 1221 | return -ENOMEM; |
| 1222 | |
| 1223 | vcpu_e500->gtlb_offset[0] = 0; |
| 1224 | vcpu_e500->gtlb_offset[1] = KVM_E500_TLB0_SIZE; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 1225 | |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 1226 | vcpu_e500->tlb_refs[0] = |
| 1227 | kzalloc(sizeof(struct tlbe_ref) * host_tlb_params[0].entries, |
| 1228 | GFP_KERNEL); |
| 1229 | if (!vcpu_e500->tlb_refs[0]) |
| 1230 | goto err; |
Liu Yu | 08b7fa9 | 2011-06-14 18:34:59 -0500 | [diff] [blame] | 1231 | |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 1232 | vcpu_e500->tlb_refs[1] = |
| 1233 | kzalloc(sizeof(struct tlbe_ref) * host_tlb_params[1].entries, |
| 1234 | GFP_KERNEL); |
| 1235 | if (!vcpu_e500->tlb_refs[1]) |
| 1236 | goto err; |
| 1237 | |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 1238 | vcpu_e500->gtlb_priv[0] = kzalloc(sizeof(struct tlbe_ref) * |
| 1239 | vcpu_e500->gtlb_params[0].entries, |
| 1240 | GFP_KERNEL); |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 1241 | if (!vcpu_e500->gtlb_priv[0]) |
| 1242 | goto err; |
| 1243 | |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 1244 | vcpu_e500->gtlb_priv[1] = kzalloc(sizeof(struct tlbe_ref) * |
| 1245 | vcpu_e500->gtlb_params[1].entries, |
| 1246 | GFP_KERNEL); |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 1247 | if (!vcpu_e500->gtlb_priv[1]) |
| 1248 | goto err; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 1249 | |
Scott Wood | 4f802fe | 2011-12-20 15:34:37 +0000 | [diff] [blame] | 1250 | vcpu_e500->g2h_tlb1_map = kzalloc(sizeof(unsigned int) * |
| 1251 | vcpu_e500->gtlb_params[1].entries, |
| 1252 | GFP_KERNEL); |
| 1253 | if (!vcpu_e500->g2h_tlb1_map) |
| 1254 | goto err; |
| 1255 | |
| 1256 | vcpu_e500->h2g_tlb1_rmap = kzalloc(sizeof(unsigned int) * |
| 1257 | host_tlb_params[1].entries, |
| 1258 | GFP_KERNEL); |
| 1259 | if (!vcpu_e500->h2g_tlb1_rmap) |
| 1260 | goto err; |
| 1261 | |
Liu Yu | da15bf4 | 2010-01-22 19:36:53 +0800 | [diff] [blame] | 1262 | /* Init TLB configuration register */ |
Scott Wood | 8fdd21a2 | 2011-12-20 15:34:34 +0000 | [diff] [blame] | 1263 | vcpu->arch.tlbcfg[0] = mfspr(SPRN_TLB0CFG) & |
Scott Wood | 7b11dc9 | 2011-11-28 15:20:02 +0000 | [diff] [blame] | 1264 | ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC); |
Scott Wood | 8fdd21a2 | 2011-12-20 15:34:34 +0000 | [diff] [blame] | 1265 | vcpu->arch.tlbcfg[0] |= vcpu_e500->gtlb_params[0].entries; |
| 1266 | vcpu->arch.tlbcfg[0] |= |
Scott Wood | 7b11dc9 | 2011-11-28 15:20:02 +0000 | [diff] [blame] | 1267 | vcpu_e500->gtlb_params[0].ways << TLBnCFG_ASSOC_SHIFT; |
| 1268 | |
Scott Wood | 8fdd21a2 | 2011-12-20 15:34:34 +0000 | [diff] [blame] | 1269 | vcpu->arch.tlbcfg[1] = mfspr(SPRN_TLB1CFG) & |
Scott Wood | 7b11dc9 | 2011-11-28 15:20:02 +0000 | [diff] [blame] | 1270 | ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC); |
Scott Wood | 8fdd21a2 | 2011-12-20 15:34:34 +0000 | [diff] [blame] | 1271 | vcpu->arch.tlbcfg[0] |= vcpu_e500->gtlb_params[1].entries; |
| 1272 | vcpu->arch.tlbcfg[0] |= |
Scott Wood | 7b11dc9 | 2011-11-28 15:20:02 +0000 | [diff] [blame] | 1273 | vcpu_e500->gtlb_params[1].ways << TLBnCFG_ASSOC_SHIFT; |
Liu Yu | da15bf4 | 2010-01-22 19:36:53 +0800 | [diff] [blame] | 1274 | |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 1275 | return 0; |
| 1276 | |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 1277 | err: |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 1278 | free_gtlb(vcpu_e500); |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 1279 | kfree(vcpu_e500->tlb_refs[0]); |
| 1280 | kfree(vcpu_e500->tlb_refs[1]); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 1281 | return -1; |
| 1282 | } |
| 1283 | |
| 1284 | void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *vcpu_e500) |
| 1285 | { |
Scott Wood | dc83b8b | 2011-08-18 15:25:21 -0500 | [diff] [blame] | 1286 | free_gtlb(vcpu_e500); |
Scott Wood | 4f802fe | 2011-12-20 15:34:37 +0000 | [diff] [blame] | 1287 | kfree(vcpu_e500->h2g_tlb1_rmap); |
Scott Wood | 0164c0f | 2011-08-18 15:25:18 -0500 | [diff] [blame] | 1288 | kfree(vcpu_e500->tlb_refs[0]); |
| 1289 | kfree(vcpu_e500->tlb_refs[1]); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 1290 | } |