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