Thomas Gleixner | d94d71c | 2019-05-29 07:12:40 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2010 SUSE Linux Products GmbH. All rights reserved. |
| 4 | * |
| 5 | * Authors: |
| 6 | * Alexander Graf <agraf@suse.de> |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/kvm_host.h> |
| 10 | #include <linux/hash.h> |
| 11 | #include <linux/slab.h> |
Ingo Molnar | b2d0910 | 2017-02-04 01:27:20 +0100 | [diff] [blame] | 12 | #include <linux/rculist.h> |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 13 | |
| 14 | #include <asm/kvm_ppc.h> |
| 15 | #include <asm/kvm_book3s.h> |
| 16 | #include <asm/machdep.h> |
| 17 | #include <asm/mmu_context.h> |
| 18 | #include <asm/hw_irq.h> |
| 19 | |
Aneesh Kumar K.V | 72c1253 | 2013-10-07 22:17:57 +0530 | [diff] [blame] | 20 | #include "trace_pr.h" |
Paul Mackerras | c4befc5 | 2011-06-29 00:17:33 +0000 | [diff] [blame] | 21 | |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 22 | #define PTE_SIZE 12 |
| 23 | |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 24 | static struct kmem_cache *hpte_cache; |
| 25 | |
| 26 | static inline u64 kvmppc_mmu_hash_pte(u64 eaddr) |
| 27 | { |
| 28 | return hash_64(eaddr >> PTE_SIZE, HPTEG_HASH_BITS_PTE); |
| 29 | } |
| 30 | |
Alexander Graf | 2d27fc5 | 2010-07-29 15:04:19 +0200 | [diff] [blame] | 31 | static inline u64 kvmppc_mmu_hash_pte_long(u64 eaddr) |
| 32 | { |
| 33 | return hash_64((eaddr & 0x0ffff000) >> PTE_SIZE, |
| 34 | HPTEG_HASH_BITS_PTE_LONG); |
| 35 | } |
| 36 | |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 37 | static inline u64 kvmppc_mmu_hash_vpte(u64 vpage) |
| 38 | { |
| 39 | return hash_64(vpage & 0xfffffffffULL, HPTEG_HASH_BITS_VPTE); |
| 40 | } |
| 41 | |
| 42 | static inline u64 kvmppc_mmu_hash_vpte_long(u64 vpage) |
| 43 | { |
| 44 | return hash_64((vpage & 0xffffff000ULL) >> 12, |
| 45 | HPTEG_HASH_BITS_VPTE_LONG); |
| 46 | } |
| 47 | |
Paul Mackerras | a4a0f25 | 2013-09-20 14:52:44 +1000 | [diff] [blame] | 48 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 49 | static inline u64 kvmppc_mmu_hash_vpte_64k(u64 vpage) |
| 50 | { |
| 51 | return hash_64((vpage & 0xffffffff0ULL) >> 4, |
| 52 | HPTEG_HASH_BITS_VPTE_64K); |
| 53 | } |
| 54 | #endif |
| 55 | |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 56 | void kvmppc_mmu_hpte_cache_map(struct kvm_vcpu *vcpu, struct hpte_cache *pte) |
| 57 | { |
| 58 | u64 index; |
Paul Mackerras | c4befc5 | 2011-06-29 00:17:33 +0000 | [diff] [blame] | 59 | struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu); |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 60 | |
Alexander Graf | 4c4eea7 | 2010-08-02 12:51:07 +0200 | [diff] [blame] | 61 | trace_kvm_book3s_mmu_map(pte); |
| 62 | |
Paul Mackerras | c4befc5 | 2011-06-29 00:17:33 +0000 | [diff] [blame] | 63 | spin_lock(&vcpu3s->mmu_lock); |
Alexander Graf | 2e0908a | 2010-07-29 15:04:17 +0200 | [diff] [blame] | 64 | |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 65 | /* Add to ePTE list */ |
| 66 | index = kvmppc_mmu_hash_pte(pte->pte.eaddr); |
Paul Mackerras | c4befc5 | 2011-06-29 00:17:33 +0000 | [diff] [blame] | 67 | hlist_add_head_rcu(&pte->list_pte, &vcpu3s->hpte_hash_pte[index]); |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 68 | |
Alexander Graf | 2d27fc5 | 2010-07-29 15:04:19 +0200 | [diff] [blame] | 69 | /* Add to ePTE_long list */ |
| 70 | index = kvmppc_mmu_hash_pte_long(pte->pte.eaddr); |
| 71 | hlist_add_head_rcu(&pte->list_pte_long, |
Paul Mackerras | c4befc5 | 2011-06-29 00:17:33 +0000 | [diff] [blame] | 72 | &vcpu3s->hpte_hash_pte_long[index]); |
Alexander Graf | 2d27fc5 | 2010-07-29 15:04:19 +0200 | [diff] [blame] | 73 | |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 74 | /* Add to vPTE list */ |
| 75 | index = kvmppc_mmu_hash_vpte(pte->pte.vpage); |
Paul Mackerras | c4befc5 | 2011-06-29 00:17:33 +0000 | [diff] [blame] | 76 | hlist_add_head_rcu(&pte->list_vpte, &vcpu3s->hpte_hash_vpte[index]); |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 77 | |
| 78 | /* Add to vPTE_long list */ |
| 79 | index = kvmppc_mmu_hash_vpte_long(pte->pte.vpage); |
Alexander Graf | 2e0908a | 2010-07-29 15:04:17 +0200 | [diff] [blame] | 80 | hlist_add_head_rcu(&pte->list_vpte_long, |
Paul Mackerras | c4befc5 | 2011-06-29 00:17:33 +0000 | [diff] [blame] | 81 | &vcpu3s->hpte_hash_vpte_long[index]); |
Alexander Graf | 2e0908a | 2010-07-29 15:04:17 +0200 | [diff] [blame] | 82 | |
Paul Mackerras | a4a0f25 | 2013-09-20 14:52:44 +1000 | [diff] [blame] | 83 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 84 | /* Add to vPTE_64k list */ |
| 85 | index = kvmppc_mmu_hash_vpte_64k(pte->pte.vpage); |
| 86 | hlist_add_head_rcu(&pte->list_vpte_64k, |
| 87 | &vcpu3s->hpte_hash_vpte_64k[index]); |
| 88 | #endif |
| 89 | |
Paul Mackerras | d78bca7 | 2013-09-20 14:52:52 +1000 | [diff] [blame] | 90 | vcpu3s->hpte_cache_count++; |
| 91 | |
Paul Mackerras | c4befc5 | 2011-06-29 00:17:33 +0000 | [diff] [blame] | 92 | spin_unlock(&vcpu3s->mmu_lock); |
Alexander Graf | 2e0908a | 2010-07-29 15:04:17 +0200 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static void free_pte_rcu(struct rcu_head *head) |
| 96 | { |
| 97 | struct hpte_cache *pte = container_of(head, struct hpte_cache, rcu_head); |
| 98 | kmem_cache_free(hpte_cache, pte); |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | static void invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte) |
| 102 | { |
Paul Mackerras | c4befc5 | 2011-06-29 00:17:33 +0000 | [diff] [blame] | 103 | struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu); |
| 104 | |
Alexander Graf | 8696ee4 | 2010-08-02 12:55:19 +0200 | [diff] [blame] | 105 | trace_kvm_book3s_mmu_invalidate(pte); |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 106 | |
| 107 | /* Different for 32 and 64 bit */ |
| 108 | kvmppc_mmu_invalidate_pte(vcpu, pte); |
| 109 | |
Paul Mackerras | c4befc5 | 2011-06-29 00:17:33 +0000 | [diff] [blame] | 110 | spin_lock(&vcpu3s->mmu_lock); |
Alexander Graf | 2e0908a | 2010-07-29 15:04:17 +0200 | [diff] [blame] | 111 | |
Alexander Graf | e7c1d14 | 2010-08-02 21:24:48 +0200 | [diff] [blame] | 112 | /* pte already invalidated in between? */ |
| 113 | if (hlist_unhashed(&pte->list_pte)) { |
Paul Mackerras | c4befc5 | 2011-06-29 00:17:33 +0000 | [diff] [blame] | 114 | spin_unlock(&vcpu3s->mmu_lock); |
Alexander Graf | e7c1d14 | 2010-08-02 21:24:48 +0200 | [diff] [blame] | 115 | return; |
| 116 | } |
| 117 | |
Alexander Graf | 2e0908a | 2010-07-29 15:04:17 +0200 | [diff] [blame] | 118 | hlist_del_init_rcu(&pte->list_pte); |
Alexander Graf | 2d27fc5 | 2010-07-29 15:04:19 +0200 | [diff] [blame] | 119 | hlist_del_init_rcu(&pte->list_pte_long); |
Alexander Graf | 2e0908a | 2010-07-29 15:04:17 +0200 | [diff] [blame] | 120 | hlist_del_init_rcu(&pte->list_vpte); |
| 121 | hlist_del_init_rcu(&pte->list_vpte_long); |
Paul Mackerras | a4a0f25 | 2013-09-20 14:52:44 +1000 | [diff] [blame] | 122 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 123 | hlist_del_init_rcu(&pte->list_vpte_64k); |
| 124 | #endif |
Paul Mackerras | d78bca7 | 2013-09-20 14:52:52 +1000 | [diff] [blame] | 125 | vcpu3s->hpte_cache_count--; |
Alexander Graf | 2e0908a | 2010-07-29 15:04:17 +0200 | [diff] [blame] | 126 | |
Paul Mackerras | c4befc5 | 2011-06-29 00:17:33 +0000 | [diff] [blame] | 127 | spin_unlock(&vcpu3s->mmu_lock); |
Alexander Graf | e7c1d14 | 2010-08-02 21:24:48 +0200 | [diff] [blame] | 128 | |
Alexander Graf | 2e0908a | 2010-07-29 15:04:17 +0200 | [diff] [blame] | 129 | call_rcu(&pte->rcu_head, free_pte_rcu); |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static void kvmppc_mmu_pte_flush_all(struct kvm_vcpu *vcpu) |
| 133 | { |
Paul Mackerras | c4befc5 | 2011-06-29 00:17:33 +0000 | [diff] [blame] | 134 | struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu); |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 135 | struct hpte_cache *pte; |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 136 | int i; |
| 137 | |
Alexander Graf | 2e0908a | 2010-07-29 15:04:17 +0200 | [diff] [blame] | 138 | rcu_read_lock(); |
| 139 | |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 140 | for (i = 0; i < HPTEG_HASH_NUM_VPTE_LONG; i++) { |
Paul Mackerras | c4befc5 | 2011-06-29 00:17:33 +0000 | [diff] [blame] | 141 | struct hlist_head *list = &vcpu3s->hpte_hash_vpte_long[i]; |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 142 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 143 | hlist_for_each_entry_rcu(pte, list, list_vpte_long) |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 144 | invalidate_pte(vcpu, pte); |
| 145 | } |
Alexander Graf | 2e0908a | 2010-07-29 15:04:17 +0200 | [diff] [blame] | 146 | |
| 147 | rcu_read_unlock(); |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | static void kvmppc_mmu_pte_flush_page(struct kvm_vcpu *vcpu, ulong guest_ea) |
| 151 | { |
Paul Mackerras | c4befc5 | 2011-06-29 00:17:33 +0000 | [diff] [blame] | 152 | struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu); |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 153 | struct hlist_head *list; |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 154 | struct hpte_cache *pte; |
| 155 | |
| 156 | /* Find the list of entries in the map */ |
Paul Mackerras | c4befc5 | 2011-06-29 00:17:33 +0000 | [diff] [blame] | 157 | list = &vcpu3s->hpte_hash_pte[kvmppc_mmu_hash_pte(guest_ea)]; |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 158 | |
Alexander Graf | 2e0908a | 2010-07-29 15:04:17 +0200 | [diff] [blame] | 159 | rcu_read_lock(); |
| 160 | |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 161 | /* Check the list for matching entries and invalidate */ |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 162 | hlist_for_each_entry_rcu(pte, list, list_pte) |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 163 | if ((pte->pte.eaddr & ~0xfffUL) == guest_ea) |
| 164 | invalidate_pte(vcpu, pte); |
Alexander Graf | 2e0908a | 2010-07-29 15:04:17 +0200 | [diff] [blame] | 165 | |
| 166 | rcu_read_unlock(); |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 167 | } |
| 168 | |
Alexander Graf | 2d27fc5 | 2010-07-29 15:04:19 +0200 | [diff] [blame] | 169 | static void kvmppc_mmu_pte_flush_long(struct kvm_vcpu *vcpu, ulong guest_ea) |
| 170 | { |
Paul Mackerras | c4befc5 | 2011-06-29 00:17:33 +0000 | [diff] [blame] | 171 | struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu); |
Alexander Graf | 2d27fc5 | 2010-07-29 15:04:19 +0200 | [diff] [blame] | 172 | struct hlist_head *list; |
Alexander Graf | 2d27fc5 | 2010-07-29 15:04:19 +0200 | [diff] [blame] | 173 | struct hpte_cache *pte; |
| 174 | |
| 175 | /* Find the list of entries in the map */ |
Paul Mackerras | c4befc5 | 2011-06-29 00:17:33 +0000 | [diff] [blame] | 176 | list = &vcpu3s->hpte_hash_pte_long[ |
Alexander Graf | 2d27fc5 | 2010-07-29 15:04:19 +0200 | [diff] [blame] | 177 | kvmppc_mmu_hash_pte_long(guest_ea)]; |
| 178 | |
| 179 | rcu_read_lock(); |
| 180 | |
| 181 | /* Check the list for matching entries and invalidate */ |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 182 | hlist_for_each_entry_rcu(pte, list, list_pte_long) |
Alexander Graf | 2d27fc5 | 2010-07-29 15:04:19 +0200 | [diff] [blame] | 183 | if ((pte->pte.eaddr & 0x0ffff000UL) == guest_ea) |
| 184 | invalidate_pte(vcpu, pte); |
| 185 | |
| 186 | rcu_read_unlock(); |
| 187 | } |
| 188 | |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 189 | void kvmppc_mmu_pte_flush(struct kvm_vcpu *vcpu, ulong guest_ea, ulong ea_mask) |
| 190 | { |
Alexander Graf | c60b4cf | 2010-08-02 13:40:30 +0200 | [diff] [blame] | 191 | trace_kvm_book3s_mmu_flush("", vcpu, guest_ea, ea_mask); |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 192 | guest_ea &= ea_mask; |
| 193 | |
| 194 | switch (ea_mask) { |
| 195 | case ~0xfffUL: |
| 196 | kvmppc_mmu_pte_flush_page(vcpu, guest_ea); |
| 197 | break; |
| 198 | case 0x0ffff000: |
Alexander Graf | 2d27fc5 | 2010-07-29 15:04:19 +0200 | [diff] [blame] | 199 | kvmppc_mmu_pte_flush_long(vcpu, guest_ea); |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 200 | break; |
| 201 | case 0: |
| 202 | /* Doing a complete flush -> start from scratch */ |
| 203 | kvmppc_mmu_pte_flush_all(vcpu); |
| 204 | break; |
| 205 | default: |
| 206 | WARN_ON(1); |
| 207 | break; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | /* Flush with mask 0xfffffffff */ |
| 212 | static void kvmppc_mmu_pte_vflush_short(struct kvm_vcpu *vcpu, u64 guest_vp) |
| 213 | { |
Paul Mackerras | c4befc5 | 2011-06-29 00:17:33 +0000 | [diff] [blame] | 214 | struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu); |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 215 | struct hlist_head *list; |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 216 | struct hpte_cache *pte; |
| 217 | u64 vp_mask = 0xfffffffffULL; |
| 218 | |
Paul Mackerras | c4befc5 | 2011-06-29 00:17:33 +0000 | [diff] [blame] | 219 | list = &vcpu3s->hpte_hash_vpte[kvmppc_mmu_hash_vpte(guest_vp)]; |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 220 | |
Alexander Graf | 2e0908a | 2010-07-29 15:04:17 +0200 | [diff] [blame] | 221 | rcu_read_lock(); |
| 222 | |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 223 | /* Check the list for matching entries and invalidate */ |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 224 | hlist_for_each_entry_rcu(pte, list, list_vpte) |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 225 | if ((pte->pte.vpage & vp_mask) == guest_vp) |
| 226 | invalidate_pte(vcpu, pte); |
Alexander Graf | 2e0908a | 2010-07-29 15:04:17 +0200 | [diff] [blame] | 227 | |
| 228 | rcu_read_unlock(); |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 229 | } |
| 230 | |
Paul Mackerras | a4a0f25 | 2013-09-20 14:52:44 +1000 | [diff] [blame] | 231 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 232 | /* Flush with mask 0xffffffff0 */ |
| 233 | static void kvmppc_mmu_pte_vflush_64k(struct kvm_vcpu *vcpu, u64 guest_vp) |
| 234 | { |
| 235 | struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu); |
| 236 | struct hlist_head *list; |
| 237 | struct hpte_cache *pte; |
| 238 | u64 vp_mask = 0xffffffff0ULL; |
| 239 | |
| 240 | list = &vcpu3s->hpte_hash_vpte_64k[ |
| 241 | kvmppc_mmu_hash_vpte_64k(guest_vp)]; |
| 242 | |
| 243 | rcu_read_lock(); |
| 244 | |
| 245 | /* Check the list for matching entries and invalidate */ |
| 246 | hlist_for_each_entry_rcu(pte, list, list_vpte_64k) |
| 247 | if ((pte->pte.vpage & vp_mask) == guest_vp) |
| 248 | invalidate_pte(vcpu, pte); |
| 249 | |
| 250 | rcu_read_unlock(); |
| 251 | } |
| 252 | #endif |
| 253 | |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 254 | /* Flush with mask 0xffffff000 */ |
| 255 | static void kvmppc_mmu_pte_vflush_long(struct kvm_vcpu *vcpu, u64 guest_vp) |
| 256 | { |
Paul Mackerras | c4befc5 | 2011-06-29 00:17:33 +0000 | [diff] [blame] | 257 | struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu); |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 258 | struct hlist_head *list; |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 259 | struct hpte_cache *pte; |
| 260 | u64 vp_mask = 0xffffff000ULL; |
| 261 | |
Paul Mackerras | c4befc5 | 2011-06-29 00:17:33 +0000 | [diff] [blame] | 262 | list = &vcpu3s->hpte_hash_vpte_long[ |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 263 | kvmppc_mmu_hash_vpte_long(guest_vp)]; |
| 264 | |
Alexander Graf | 2e0908a | 2010-07-29 15:04:17 +0200 | [diff] [blame] | 265 | rcu_read_lock(); |
| 266 | |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 267 | /* Check the list for matching entries and invalidate */ |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 268 | hlist_for_each_entry_rcu(pte, list, list_vpte_long) |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 269 | if ((pte->pte.vpage & vp_mask) == guest_vp) |
| 270 | invalidate_pte(vcpu, pte); |
Alexander Graf | 2e0908a | 2010-07-29 15:04:17 +0200 | [diff] [blame] | 271 | |
| 272 | rcu_read_unlock(); |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | void kvmppc_mmu_pte_vflush(struct kvm_vcpu *vcpu, u64 guest_vp, u64 vp_mask) |
| 276 | { |
Alexander Graf | c60b4cf | 2010-08-02 13:40:30 +0200 | [diff] [blame] | 277 | trace_kvm_book3s_mmu_flush("v", vcpu, guest_vp, vp_mask); |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 278 | guest_vp &= vp_mask; |
| 279 | |
| 280 | switch(vp_mask) { |
| 281 | case 0xfffffffffULL: |
| 282 | kvmppc_mmu_pte_vflush_short(vcpu, guest_vp); |
| 283 | break; |
Paul Mackerras | a4a0f25 | 2013-09-20 14:52:44 +1000 | [diff] [blame] | 284 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 285 | case 0xffffffff0ULL: |
| 286 | kvmppc_mmu_pte_vflush_64k(vcpu, guest_vp); |
| 287 | break; |
| 288 | #endif |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 289 | case 0xffffff000ULL: |
| 290 | kvmppc_mmu_pte_vflush_long(vcpu, guest_vp); |
| 291 | break; |
| 292 | default: |
| 293 | WARN_ON(1); |
| 294 | return; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end) |
| 299 | { |
Paul Mackerras | c4befc5 | 2011-06-29 00:17:33 +0000 | [diff] [blame] | 300 | struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu); |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 301 | struct hpte_cache *pte; |
| 302 | int i; |
| 303 | |
Alexander Graf | c60b4cf | 2010-08-02 13:40:30 +0200 | [diff] [blame] | 304 | trace_kvm_book3s_mmu_flush("p", vcpu, pa_start, pa_end); |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 305 | |
Alexander Graf | 2e0908a | 2010-07-29 15:04:17 +0200 | [diff] [blame] | 306 | rcu_read_lock(); |
| 307 | |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 308 | for (i = 0; i < HPTEG_HASH_NUM_VPTE_LONG; i++) { |
Paul Mackerras | c4befc5 | 2011-06-29 00:17:33 +0000 | [diff] [blame] | 309 | struct hlist_head *list = &vcpu3s->hpte_hash_vpte_long[i]; |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 310 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 311 | hlist_for_each_entry_rcu(pte, list, list_vpte_long) |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 312 | if ((pte->pte.raddr >= pa_start) && |
| 313 | (pte->pte.raddr < pa_end)) |
| 314 | invalidate_pte(vcpu, pte); |
| 315 | } |
Alexander Graf | 2e0908a | 2010-07-29 15:04:17 +0200 | [diff] [blame] | 316 | |
| 317 | rcu_read_unlock(); |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | struct hpte_cache *kvmppc_mmu_hpte_cache_next(struct kvm_vcpu *vcpu) |
| 321 | { |
Paul Mackerras | c4befc5 | 2011-06-29 00:17:33 +0000 | [diff] [blame] | 322 | struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu); |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 323 | struct hpte_cache *pte; |
| 324 | |
Paul Mackerras | c4befc5 | 2011-06-29 00:17:33 +0000 | [diff] [blame] | 325 | if (vcpu3s->hpte_cache_count == HPTEG_CACHE_NUM) |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 326 | kvmppc_mmu_pte_flush_all(vcpu); |
| 327 | |
Paul Mackerras | d78bca7 | 2013-09-20 14:52:52 +1000 | [diff] [blame] | 328 | pte = kmem_cache_zalloc(hpte_cache, GFP_KERNEL); |
| 329 | |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 330 | return pte; |
| 331 | } |
| 332 | |
Paul Mackerras | d78bca7 | 2013-09-20 14:52:52 +1000 | [diff] [blame] | 333 | void kvmppc_mmu_hpte_cache_free(struct hpte_cache *pte) |
| 334 | { |
| 335 | kmem_cache_free(hpte_cache, pte); |
| 336 | } |
| 337 | |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 338 | void kvmppc_mmu_hpte_destroy(struct kvm_vcpu *vcpu) |
| 339 | { |
| 340 | kvmppc_mmu_pte_flush(vcpu, 0, 0); |
| 341 | } |
| 342 | |
| 343 | static void kvmppc_mmu_hpte_init_hash(struct hlist_head *hash_list, int len) |
| 344 | { |
| 345 | int i; |
| 346 | |
| 347 | for (i = 0; i < len; i++) |
| 348 | INIT_HLIST_HEAD(&hash_list[i]); |
| 349 | } |
| 350 | |
| 351 | int kvmppc_mmu_hpte_init(struct kvm_vcpu *vcpu) |
| 352 | { |
Paul Mackerras | c4befc5 | 2011-06-29 00:17:33 +0000 | [diff] [blame] | 353 | struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu); |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 354 | |
Paul Mackerras | c4befc5 | 2011-06-29 00:17:33 +0000 | [diff] [blame] | 355 | /* init hpte lookup hashes */ |
| 356 | kvmppc_mmu_hpte_init_hash(vcpu3s->hpte_hash_pte, |
| 357 | ARRAY_SIZE(vcpu3s->hpte_hash_pte)); |
| 358 | kvmppc_mmu_hpte_init_hash(vcpu3s->hpte_hash_pte_long, |
| 359 | ARRAY_SIZE(vcpu3s->hpte_hash_pte_long)); |
| 360 | kvmppc_mmu_hpte_init_hash(vcpu3s->hpte_hash_vpte, |
| 361 | ARRAY_SIZE(vcpu3s->hpte_hash_vpte)); |
| 362 | kvmppc_mmu_hpte_init_hash(vcpu3s->hpte_hash_vpte_long, |
| 363 | ARRAY_SIZE(vcpu3s->hpte_hash_vpte_long)); |
Paul Mackerras | a4a0f25 | 2013-09-20 14:52:44 +1000 | [diff] [blame] | 364 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 365 | kvmppc_mmu_hpte_init_hash(vcpu3s->hpte_hash_vpte_64k, |
| 366 | ARRAY_SIZE(vcpu3s->hpte_hash_vpte_64k)); |
| 367 | #endif |
Paul Mackerras | c4befc5 | 2011-06-29 00:17:33 +0000 | [diff] [blame] | 368 | |
| 369 | spin_lock_init(&vcpu3s->mmu_lock); |
Alexander Graf | 2e0908a | 2010-07-29 15:04:17 +0200 | [diff] [blame] | 370 | |
Alexander Graf | 7741909 | 2010-06-30 15:18:45 +0200 | [diff] [blame] | 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | int kvmppc_mmu_hpte_sysinit(void) |
| 375 | { |
| 376 | /* init hpte slab cache */ |
| 377 | hpte_cache = kmem_cache_create("kvm-spt", sizeof(struct hpte_cache), |
| 378 | sizeof(struct hpte_cache), 0, NULL); |
| 379 | |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | void kvmppc_mmu_hpte_sysexit(void) |
| 384 | { |
| 385 | kmem_cache_destroy(hpte_cache); |
| 386 | } |