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