blob: ce79ac33e8d3b92db09aa279a54dd1443023dd65 [file] [log] [blame]
Thomas Gleixnerd94d71c2019-05-29 07:12:40 -07001// SPDX-License-Identifier: GPL-2.0-only
Alexander Graf77419092010-06-30 15:18:45 +02002/*
3 * Copyright (C) 2010 SUSE Linux Products GmbH. All rights reserved.
4 *
5 * Authors:
6 * Alexander Graf <agraf@suse.de>
Alexander Graf77419092010-06-30 15:18:45 +02007 */
8
9#include <linux/kvm_host.h>
10#include <linux/hash.h>
11#include <linux/slab.h>
Ingo Molnarb2d09102017-02-04 01:27:20 +010012#include <linux/rculist.h>
Alexander Graf77419092010-06-30 15:18:45 +020013
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.V72c12532013-10-07 22:17:57 +053020#include "trace_pr.h"
Paul Mackerrasc4befc52011-06-29 00:17:33 +000021
Alexander Graf77419092010-06-30 15:18:45 +020022#define PTE_SIZE 12
23
Alexander Graf77419092010-06-30 15:18:45 +020024static struct kmem_cache *hpte_cache;
25
26static inline u64 kvmppc_mmu_hash_pte(u64 eaddr)
27{
28 return hash_64(eaddr >> PTE_SIZE, HPTEG_HASH_BITS_PTE);
29}
30
Alexander Graf2d27fc52010-07-29 15:04:19 +020031static 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 Graf77419092010-06-30 15:18:45 +020037static inline u64 kvmppc_mmu_hash_vpte(u64 vpage)
38{
39 return hash_64(vpage & 0xfffffffffULL, HPTEG_HASH_BITS_VPTE);
40}
41
42static 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 Mackerrasa4a0f252013-09-20 14:52:44 +100048#ifdef CONFIG_PPC_BOOK3S_64
49static 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 Graf77419092010-06-30 15:18:45 +020056void kvmppc_mmu_hpte_cache_map(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
57{
58 u64 index;
Paul Mackerrasc4befc52011-06-29 00:17:33 +000059 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
Alexander Graf77419092010-06-30 15:18:45 +020060
Alexander Graf4c4eea72010-08-02 12:51:07 +020061 trace_kvm_book3s_mmu_map(pte);
62
Paul Mackerrasc4befc52011-06-29 00:17:33 +000063 spin_lock(&vcpu3s->mmu_lock);
Alexander Graf2e0908a2010-07-29 15:04:17 +020064
Alexander Graf77419092010-06-30 15:18:45 +020065 /* Add to ePTE list */
66 index = kvmppc_mmu_hash_pte(pte->pte.eaddr);
Paul Mackerrasc4befc52011-06-29 00:17:33 +000067 hlist_add_head_rcu(&pte->list_pte, &vcpu3s->hpte_hash_pte[index]);
Alexander Graf77419092010-06-30 15:18:45 +020068
Alexander Graf2d27fc52010-07-29 15:04:19 +020069 /* 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 Mackerrasc4befc52011-06-29 00:17:33 +000072 &vcpu3s->hpte_hash_pte_long[index]);
Alexander Graf2d27fc52010-07-29 15:04:19 +020073
Alexander Graf77419092010-06-30 15:18:45 +020074 /* Add to vPTE list */
75 index = kvmppc_mmu_hash_vpte(pte->pte.vpage);
Paul Mackerrasc4befc52011-06-29 00:17:33 +000076 hlist_add_head_rcu(&pte->list_vpte, &vcpu3s->hpte_hash_vpte[index]);
Alexander Graf77419092010-06-30 15:18:45 +020077
78 /* Add to vPTE_long list */
79 index = kvmppc_mmu_hash_vpte_long(pte->pte.vpage);
Alexander Graf2e0908a2010-07-29 15:04:17 +020080 hlist_add_head_rcu(&pte->list_vpte_long,
Paul Mackerrasc4befc52011-06-29 00:17:33 +000081 &vcpu3s->hpte_hash_vpte_long[index]);
Alexander Graf2e0908a2010-07-29 15:04:17 +020082
Paul Mackerrasa4a0f252013-09-20 14:52:44 +100083#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 Mackerrasd78bca72013-09-20 14:52:52 +100090 vcpu3s->hpte_cache_count++;
91
Paul Mackerrasc4befc52011-06-29 00:17:33 +000092 spin_unlock(&vcpu3s->mmu_lock);
Alexander Graf2e0908a2010-07-29 15:04:17 +020093}
94
95static 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 Graf77419092010-06-30 15:18:45 +020099}
100
101static void invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
102{
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000103 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
104
Alexander Graf8696ee42010-08-02 12:55:19 +0200105 trace_kvm_book3s_mmu_invalidate(pte);
Alexander Graf77419092010-06-30 15:18:45 +0200106
107 /* Different for 32 and 64 bit */
108 kvmppc_mmu_invalidate_pte(vcpu, pte);
109
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000110 spin_lock(&vcpu3s->mmu_lock);
Alexander Graf2e0908a2010-07-29 15:04:17 +0200111
Alexander Grafe7c1d142010-08-02 21:24:48 +0200112 /* pte already invalidated in between? */
113 if (hlist_unhashed(&pte->list_pte)) {
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000114 spin_unlock(&vcpu3s->mmu_lock);
Alexander Grafe7c1d142010-08-02 21:24:48 +0200115 return;
116 }
117
Alexander Graf2e0908a2010-07-29 15:04:17 +0200118 hlist_del_init_rcu(&pte->list_pte);
Alexander Graf2d27fc52010-07-29 15:04:19 +0200119 hlist_del_init_rcu(&pte->list_pte_long);
Alexander Graf2e0908a2010-07-29 15:04:17 +0200120 hlist_del_init_rcu(&pte->list_vpte);
121 hlist_del_init_rcu(&pte->list_vpte_long);
Paul Mackerrasa4a0f252013-09-20 14:52:44 +1000122#ifdef CONFIG_PPC_BOOK3S_64
123 hlist_del_init_rcu(&pte->list_vpte_64k);
124#endif
Paul Mackerrasd78bca72013-09-20 14:52:52 +1000125 vcpu3s->hpte_cache_count--;
Alexander Graf2e0908a2010-07-29 15:04:17 +0200126
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000127 spin_unlock(&vcpu3s->mmu_lock);
Alexander Grafe7c1d142010-08-02 21:24:48 +0200128
Alexander Graf2e0908a2010-07-29 15:04:17 +0200129 call_rcu(&pte->rcu_head, free_pte_rcu);
Alexander Graf77419092010-06-30 15:18:45 +0200130}
131
132static void kvmppc_mmu_pte_flush_all(struct kvm_vcpu *vcpu)
133{
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000134 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
Alexander Graf77419092010-06-30 15:18:45 +0200135 struct hpte_cache *pte;
Alexander Graf77419092010-06-30 15:18:45 +0200136 int i;
137
Alexander Graf2e0908a2010-07-29 15:04:17 +0200138 rcu_read_lock();
139
Alexander Graf77419092010-06-30 15:18:45 +0200140 for (i = 0; i < HPTEG_HASH_NUM_VPTE_LONG; i++) {
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000141 struct hlist_head *list = &vcpu3s->hpte_hash_vpte_long[i];
Alexander Graf77419092010-06-30 15:18:45 +0200142
Sasha Levinb67bfe02013-02-27 17:06:00 -0800143 hlist_for_each_entry_rcu(pte, list, list_vpte_long)
Alexander Graf77419092010-06-30 15:18:45 +0200144 invalidate_pte(vcpu, pte);
145 }
Alexander Graf2e0908a2010-07-29 15:04:17 +0200146
147 rcu_read_unlock();
Alexander Graf77419092010-06-30 15:18:45 +0200148}
149
150static void kvmppc_mmu_pte_flush_page(struct kvm_vcpu *vcpu, ulong guest_ea)
151{
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000152 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
Alexander Graf77419092010-06-30 15:18:45 +0200153 struct hlist_head *list;
Alexander Graf77419092010-06-30 15:18:45 +0200154 struct hpte_cache *pte;
155
156 /* Find the list of entries in the map */
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000157 list = &vcpu3s->hpte_hash_pte[kvmppc_mmu_hash_pte(guest_ea)];
Alexander Graf77419092010-06-30 15:18:45 +0200158
Alexander Graf2e0908a2010-07-29 15:04:17 +0200159 rcu_read_lock();
160
Alexander Graf77419092010-06-30 15:18:45 +0200161 /* Check the list for matching entries and invalidate */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800162 hlist_for_each_entry_rcu(pte, list, list_pte)
Alexander Graf77419092010-06-30 15:18:45 +0200163 if ((pte->pte.eaddr & ~0xfffUL) == guest_ea)
164 invalidate_pte(vcpu, pte);
Alexander Graf2e0908a2010-07-29 15:04:17 +0200165
166 rcu_read_unlock();
Alexander Graf77419092010-06-30 15:18:45 +0200167}
168
Alexander Graf2d27fc52010-07-29 15:04:19 +0200169static void kvmppc_mmu_pte_flush_long(struct kvm_vcpu *vcpu, ulong guest_ea)
170{
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000171 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
Alexander Graf2d27fc52010-07-29 15:04:19 +0200172 struct hlist_head *list;
Alexander Graf2d27fc52010-07-29 15:04:19 +0200173 struct hpte_cache *pte;
174
175 /* Find the list of entries in the map */
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000176 list = &vcpu3s->hpte_hash_pte_long[
Alexander Graf2d27fc52010-07-29 15:04:19 +0200177 kvmppc_mmu_hash_pte_long(guest_ea)];
178
179 rcu_read_lock();
180
181 /* Check the list for matching entries and invalidate */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800182 hlist_for_each_entry_rcu(pte, list, list_pte_long)
Alexander Graf2d27fc52010-07-29 15:04:19 +0200183 if ((pte->pte.eaddr & 0x0ffff000UL) == guest_ea)
184 invalidate_pte(vcpu, pte);
185
186 rcu_read_unlock();
187}
188
Alexander Graf77419092010-06-30 15:18:45 +0200189void kvmppc_mmu_pte_flush(struct kvm_vcpu *vcpu, ulong guest_ea, ulong ea_mask)
190{
Alexander Grafc60b4cf2010-08-02 13:40:30 +0200191 trace_kvm_book3s_mmu_flush("", vcpu, guest_ea, ea_mask);
Alexander Graf77419092010-06-30 15:18:45 +0200192 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 Graf2d27fc52010-07-29 15:04:19 +0200199 kvmppc_mmu_pte_flush_long(vcpu, guest_ea);
Alexander Graf77419092010-06-30 15:18:45 +0200200 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 */
212static void kvmppc_mmu_pte_vflush_short(struct kvm_vcpu *vcpu, u64 guest_vp)
213{
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000214 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
Alexander Graf77419092010-06-30 15:18:45 +0200215 struct hlist_head *list;
Alexander Graf77419092010-06-30 15:18:45 +0200216 struct hpte_cache *pte;
217 u64 vp_mask = 0xfffffffffULL;
218
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000219 list = &vcpu3s->hpte_hash_vpte[kvmppc_mmu_hash_vpte(guest_vp)];
Alexander Graf77419092010-06-30 15:18:45 +0200220
Alexander Graf2e0908a2010-07-29 15:04:17 +0200221 rcu_read_lock();
222
Alexander Graf77419092010-06-30 15:18:45 +0200223 /* Check the list for matching entries and invalidate */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800224 hlist_for_each_entry_rcu(pte, list, list_vpte)
Alexander Graf77419092010-06-30 15:18:45 +0200225 if ((pte->pte.vpage & vp_mask) == guest_vp)
226 invalidate_pte(vcpu, pte);
Alexander Graf2e0908a2010-07-29 15:04:17 +0200227
228 rcu_read_unlock();
Alexander Graf77419092010-06-30 15:18:45 +0200229}
230
Paul Mackerrasa4a0f252013-09-20 14:52:44 +1000231#ifdef CONFIG_PPC_BOOK3S_64
232/* Flush with mask 0xffffffff0 */
233static 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 Graf77419092010-06-30 15:18:45 +0200254/* Flush with mask 0xffffff000 */
255static void kvmppc_mmu_pte_vflush_long(struct kvm_vcpu *vcpu, u64 guest_vp)
256{
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000257 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
Alexander Graf77419092010-06-30 15:18:45 +0200258 struct hlist_head *list;
Alexander Graf77419092010-06-30 15:18:45 +0200259 struct hpte_cache *pte;
260 u64 vp_mask = 0xffffff000ULL;
261
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000262 list = &vcpu3s->hpte_hash_vpte_long[
Alexander Graf77419092010-06-30 15:18:45 +0200263 kvmppc_mmu_hash_vpte_long(guest_vp)];
264
Alexander Graf2e0908a2010-07-29 15:04:17 +0200265 rcu_read_lock();
266
Alexander Graf77419092010-06-30 15:18:45 +0200267 /* Check the list for matching entries and invalidate */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800268 hlist_for_each_entry_rcu(pte, list, list_vpte_long)
Alexander Graf77419092010-06-30 15:18:45 +0200269 if ((pte->pte.vpage & vp_mask) == guest_vp)
270 invalidate_pte(vcpu, pte);
Alexander Graf2e0908a2010-07-29 15:04:17 +0200271
272 rcu_read_unlock();
Alexander Graf77419092010-06-30 15:18:45 +0200273}
274
275void kvmppc_mmu_pte_vflush(struct kvm_vcpu *vcpu, u64 guest_vp, u64 vp_mask)
276{
Alexander Grafc60b4cf2010-08-02 13:40:30 +0200277 trace_kvm_book3s_mmu_flush("v", vcpu, guest_vp, vp_mask);
Alexander Graf77419092010-06-30 15:18:45 +0200278 guest_vp &= vp_mask;
279
280 switch(vp_mask) {
281 case 0xfffffffffULL:
282 kvmppc_mmu_pte_vflush_short(vcpu, guest_vp);
283 break;
Paul Mackerrasa4a0f252013-09-20 14:52:44 +1000284#ifdef CONFIG_PPC_BOOK3S_64
285 case 0xffffffff0ULL:
286 kvmppc_mmu_pte_vflush_64k(vcpu, guest_vp);
287 break;
288#endif
Alexander Graf77419092010-06-30 15:18:45 +0200289 case 0xffffff000ULL:
290 kvmppc_mmu_pte_vflush_long(vcpu, guest_vp);
291 break;
292 default:
293 WARN_ON(1);
294 return;
295 }
296}
297
298void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end)
299{
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000300 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
Alexander Graf77419092010-06-30 15:18:45 +0200301 struct hpte_cache *pte;
302 int i;
303
Alexander Grafc60b4cf2010-08-02 13:40:30 +0200304 trace_kvm_book3s_mmu_flush("p", vcpu, pa_start, pa_end);
Alexander Graf77419092010-06-30 15:18:45 +0200305
Alexander Graf2e0908a2010-07-29 15:04:17 +0200306 rcu_read_lock();
307
Alexander Graf77419092010-06-30 15:18:45 +0200308 for (i = 0; i < HPTEG_HASH_NUM_VPTE_LONG; i++) {
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000309 struct hlist_head *list = &vcpu3s->hpte_hash_vpte_long[i];
Alexander Graf77419092010-06-30 15:18:45 +0200310
Sasha Levinb67bfe02013-02-27 17:06:00 -0800311 hlist_for_each_entry_rcu(pte, list, list_vpte_long)
Alexander Graf77419092010-06-30 15:18:45 +0200312 if ((pte->pte.raddr >= pa_start) &&
313 (pte->pte.raddr < pa_end))
314 invalidate_pte(vcpu, pte);
315 }
Alexander Graf2e0908a2010-07-29 15:04:17 +0200316
317 rcu_read_unlock();
Alexander Graf77419092010-06-30 15:18:45 +0200318}
319
320struct hpte_cache *kvmppc_mmu_hpte_cache_next(struct kvm_vcpu *vcpu)
321{
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000322 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
Alexander Graf77419092010-06-30 15:18:45 +0200323 struct hpte_cache *pte;
324
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000325 if (vcpu3s->hpte_cache_count == HPTEG_CACHE_NUM)
Alexander Graf77419092010-06-30 15:18:45 +0200326 kvmppc_mmu_pte_flush_all(vcpu);
327
Paul Mackerrasd78bca72013-09-20 14:52:52 +1000328 pte = kmem_cache_zalloc(hpte_cache, GFP_KERNEL);
329
Alexander Graf77419092010-06-30 15:18:45 +0200330 return pte;
331}
332
Paul Mackerrasd78bca72013-09-20 14:52:52 +1000333void kvmppc_mmu_hpte_cache_free(struct hpte_cache *pte)
334{
335 kmem_cache_free(hpte_cache, pte);
336}
337
Alexander Graf77419092010-06-30 15:18:45 +0200338void kvmppc_mmu_hpte_destroy(struct kvm_vcpu *vcpu)
339{
340 kvmppc_mmu_pte_flush(vcpu, 0, 0);
341}
342
343static 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
351int kvmppc_mmu_hpte_init(struct kvm_vcpu *vcpu)
352{
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000353 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
Alexander Graf77419092010-06-30 15:18:45 +0200354
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000355 /* 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 Mackerrasa4a0f252013-09-20 14:52:44 +1000364#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 Mackerrasc4befc52011-06-29 00:17:33 +0000368
369 spin_lock_init(&vcpu3s->mmu_lock);
Alexander Graf2e0908a2010-07-29 15:04:17 +0200370
Alexander Graf77419092010-06-30 15:18:45 +0200371 return 0;
372}
373
374int 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
383void kvmppc_mmu_hpte_sysexit(void)
384{
385 kmem_cache_destroy(hpte_cache);
386}