1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2010 SUSE Linux Products GmbH. All rights reserved.
6 * Alexander Graf <agraf@suse.de>
9 #include <linux/kvm_host.h>
10 #include <linux/hash.h>
11 #include <linux/slab.h>
12 #include <linux/rculist.h>
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>
24 static struct kmem_cache
*hpte_cache
;
26 static inline u64
kvmppc_mmu_hash_pte(u64 eaddr
)
28 return hash_64(eaddr
>> PTE_SIZE
, HPTEG_HASH_BITS_PTE
);
31 static inline u64
kvmppc_mmu_hash_pte_long(u64 eaddr
)
33 return hash_64((eaddr
& 0x0ffff000) >> PTE_SIZE
,
34 HPTEG_HASH_BITS_PTE_LONG
);
37 static inline u64
kvmppc_mmu_hash_vpte(u64 vpage
)
39 return hash_64(vpage
& 0xfffffffffULL
, HPTEG_HASH_BITS_VPTE
);
42 static inline u64
kvmppc_mmu_hash_vpte_long(u64 vpage
)
44 return hash_64((vpage
& 0xffffff000ULL
) >> 12,
45 HPTEG_HASH_BITS_VPTE_LONG
);
48 #ifdef CONFIG_PPC_BOOK3S_64
49 static inline u64
kvmppc_mmu_hash_vpte_64k(u64 vpage
)
51 return hash_64((vpage
& 0xffffffff0ULL
) >> 4,
52 HPTEG_HASH_BITS_VPTE_64K
);
56 void kvmppc_mmu_hpte_cache_map(struct kvm_vcpu
*vcpu
, struct hpte_cache
*pte
)
59 struct kvmppc_vcpu_book3s
*vcpu3s
= to_book3s(vcpu
);
61 trace_kvm_book3s_mmu_map(pte
);
63 spin_lock(&vcpu3s
->mmu_lock
);
65 /* Add to ePTE list */
66 index
= kvmppc_mmu_hash_pte(pte
->pte
.eaddr
);
67 hlist_add_head_rcu(&pte
->list_pte
, &vcpu3s
->hpte_hash_pte
[index
]);
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
,
72 &vcpu3s
->hpte_hash_pte_long
[index
]);
74 /* Add to vPTE list */
75 index
= kvmppc_mmu_hash_vpte(pte
->pte
.vpage
);
76 hlist_add_head_rcu(&pte
->list_vpte
, &vcpu3s
->hpte_hash_vpte
[index
]);
78 /* Add to vPTE_long list */
79 index
= kvmppc_mmu_hash_vpte_long(pte
->pte
.vpage
);
80 hlist_add_head_rcu(&pte
->list_vpte_long
,
81 &vcpu3s
->hpte_hash_vpte_long
[index
]);
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
]);
90 vcpu3s
->hpte_cache_count
++;
92 spin_unlock(&vcpu3s
->mmu_lock
);
95 static void free_pte_rcu(struct rcu_head
*head
)
97 struct hpte_cache
*pte
= container_of(head
, struct hpte_cache
, rcu_head
);
98 kmem_cache_free(hpte_cache
, pte
);
101 static void invalidate_pte(struct kvm_vcpu
*vcpu
, struct hpte_cache
*pte
)
103 struct kvmppc_vcpu_book3s
*vcpu3s
= to_book3s(vcpu
);
105 trace_kvm_book3s_mmu_invalidate(pte
);
107 /* Different for 32 and 64 bit */
108 kvmppc_mmu_invalidate_pte(vcpu
, pte
);
110 spin_lock(&vcpu3s
->mmu_lock
);
112 /* pte already invalidated in between? */
113 if (hlist_unhashed(&pte
->list_pte
)) {
114 spin_unlock(&vcpu3s
->mmu_lock
);
118 hlist_del_init_rcu(&pte
->list_pte
);
119 hlist_del_init_rcu(&pte
->list_pte_long
);
120 hlist_del_init_rcu(&pte
->list_vpte
);
121 hlist_del_init_rcu(&pte
->list_vpte_long
);
122 #ifdef CONFIG_PPC_BOOK3S_64
123 hlist_del_init_rcu(&pte
->list_vpte_64k
);
125 vcpu3s
->hpte_cache_count
--;
127 spin_unlock(&vcpu3s
->mmu_lock
);
129 call_rcu(&pte
->rcu_head
, free_pte_rcu
);
132 static void kvmppc_mmu_pte_flush_all(struct kvm_vcpu
*vcpu
)
134 struct kvmppc_vcpu_book3s
*vcpu3s
= to_book3s(vcpu
);
135 struct hpte_cache
*pte
;
140 for (i
= 0; i
< HPTEG_HASH_NUM_VPTE_LONG
; i
++) {
141 struct hlist_head
*list
= &vcpu3s
->hpte_hash_vpte_long
[i
];
143 hlist_for_each_entry_rcu(pte
, list
, list_vpte_long
)
144 invalidate_pte(vcpu
, pte
);
150 static void kvmppc_mmu_pte_flush_page(struct kvm_vcpu
*vcpu
, ulong guest_ea
)
152 struct kvmppc_vcpu_book3s
*vcpu3s
= to_book3s(vcpu
);
153 struct hlist_head
*list
;
154 struct hpte_cache
*pte
;
156 /* Find the list of entries in the map */
157 list
= &vcpu3s
->hpte_hash_pte
[kvmppc_mmu_hash_pte(guest_ea
)];
161 /* Check the list for matching entries and invalidate */
162 hlist_for_each_entry_rcu(pte
, list
, list_pte
)
163 if ((pte
->pte
.eaddr
& ~0xfffUL
) == guest_ea
)
164 invalidate_pte(vcpu
, pte
);
169 static void kvmppc_mmu_pte_flush_long(struct kvm_vcpu
*vcpu
, ulong guest_ea
)
171 struct kvmppc_vcpu_book3s
*vcpu3s
= to_book3s(vcpu
);
172 struct hlist_head
*list
;
173 struct hpte_cache
*pte
;
175 /* Find the list of entries in the map */
176 list
= &vcpu3s
->hpte_hash_pte_long
[
177 kvmppc_mmu_hash_pte_long(guest_ea
)];
181 /* Check the list for matching entries and invalidate */
182 hlist_for_each_entry_rcu(pte
, list
, list_pte_long
)
183 if ((pte
->pte
.eaddr
& 0x0ffff000UL
) == guest_ea
)
184 invalidate_pte(vcpu
, pte
);
189 void kvmppc_mmu_pte_flush(struct kvm_vcpu
*vcpu
, ulong guest_ea
, ulong ea_mask
)
191 trace_kvm_book3s_mmu_flush("", vcpu
, guest_ea
, ea_mask
);
196 kvmppc_mmu_pte_flush_page(vcpu
, guest_ea
);
199 kvmppc_mmu_pte_flush_long(vcpu
, guest_ea
);
202 /* Doing a complete flush -> start from scratch */
203 kvmppc_mmu_pte_flush_all(vcpu
);
211 /* Flush with mask 0xfffffffff */
212 static void kvmppc_mmu_pte_vflush_short(struct kvm_vcpu
*vcpu
, u64 guest_vp
)
214 struct kvmppc_vcpu_book3s
*vcpu3s
= to_book3s(vcpu
);
215 struct hlist_head
*list
;
216 struct hpte_cache
*pte
;
217 u64 vp_mask
= 0xfffffffffULL
;
219 list
= &vcpu3s
->hpte_hash_vpte
[kvmppc_mmu_hash_vpte(guest_vp
)];
223 /* Check the list for matching entries and invalidate */
224 hlist_for_each_entry_rcu(pte
, list
, list_vpte
)
225 if ((pte
->pte
.vpage
& vp_mask
) == guest_vp
)
226 invalidate_pte(vcpu
, pte
);
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
)
235 struct kvmppc_vcpu_book3s
*vcpu3s
= to_book3s(vcpu
);
236 struct hlist_head
*list
;
237 struct hpte_cache
*pte
;
238 u64 vp_mask
= 0xffffffff0ULL
;
240 list
= &vcpu3s
->hpte_hash_vpte_64k
[
241 kvmppc_mmu_hash_vpte_64k(guest_vp
)];
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
);
254 /* Flush with mask 0xffffff000 */
255 static void kvmppc_mmu_pte_vflush_long(struct kvm_vcpu
*vcpu
, u64 guest_vp
)
257 struct kvmppc_vcpu_book3s
*vcpu3s
= to_book3s(vcpu
);
258 struct hlist_head
*list
;
259 struct hpte_cache
*pte
;
260 u64 vp_mask
= 0xffffff000ULL
;
262 list
= &vcpu3s
->hpte_hash_vpte_long
[
263 kvmppc_mmu_hash_vpte_long(guest_vp
)];
267 /* Check the list for matching entries and invalidate */
268 hlist_for_each_entry_rcu(pte
, list
, list_vpte_long
)
269 if ((pte
->pte
.vpage
& vp_mask
) == guest_vp
)
270 invalidate_pte(vcpu
, pte
);
275 void kvmppc_mmu_pte_vflush(struct kvm_vcpu
*vcpu
, u64 guest_vp
, u64 vp_mask
)
277 trace_kvm_book3s_mmu_flush("v", vcpu
, guest_vp
, vp_mask
);
282 kvmppc_mmu_pte_vflush_short(vcpu
, guest_vp
);
284 #ifdef CONFIG_PPC_BOOK3S_64
286 kvmppc_mmu_pte_vflush_64k(vcpu
, guest_vp
);
290 kvmppc_mmu_pte_vflush_long(vcpu
, guest_vp
);
298 void kvmppc_mmu_pte_pflush(struct kvm_vcpu
*vcpu
, ulong pa_start
, ulong pa_end
)
300 struct kvmppc_vcpu_book3s
*vcpu3s
= to_book3s(vcpu
);
301 struct hpte_cache
*pte
;
304 trace_kvm_book3s_mmu_flush("p", vcpu
, pa_start
, pa_end
);
308 for (i
= 0; i
< HPTEG_HASH_NUM_VPTE_LONG
; i
++) {
309 struct hlist_head
*list
= &vcpu3s
->hpte_hash_vpte_long
[i
];
311 hlist_for_each_entry_rcu(pte
, list
, list_vpte_long
)
312 if ((pte
->pte
.raddr
>= pa_start
) &&
313 (pte
->pte
.raddr
< pa_end
))
314 invalidate_pte(vcpu
, pte
);
320 struct hpte_cache
*kvmppc_mmu_hpte_cache_next(struct kvm_vcpu
*vcpu
)
322 struct kvmppc_vcpu_book3s
*vcpu3s
= to_book3s(vcpu
);
323 struct hpte_cache
*pte
;
325 if (vcpu3s
->hpte_cache_count
== HPTEG_CACHE_NUM
)
326 kvmppc_mmu_pte_flush_all(vcpu
);
328 pte
= kmem_cache_zalloc(hpte_cache
, GFP_KERNEL
);
333 void kvmppc_mmu_hpte_cache_free(struct hpte_cache
*pte
)
335 kmem_cache_free(hpte_cache
, pte
);
338 void kvmppc_mmu_hpte_destroy(struct kvm_vcpu
*vcpu
)
340 kvmppc_mmu_pte_flush(vcpu
, 0, 0);
343 static void kvmppc_mmu_hpte_init_hash(struct hlist_head
*hash_list
, int len
)
347 for (i
= 0; i
< len
; i
++)
348 INIT_HLIST_HEAD(&hash_list
[i
]);
351 int kvmppc_mmu_hpte_init(struct kvm_vcpu
*vcpu
)
353 struct kvmppc_vcpu_book3s
*vcpu3s
= to_book3s(vcpu
);
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
));
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
));
369 spin_lock_init(&vcpu3s
->mmu_lock
);
374 int kvmppc_mmu_hpte_sysinit(void)
376 /* init hpte slab cache */
377 hpte_cache
= kmem_cache_create("kvm-spt", sizeof(struct hpte_cache
),
378 sizeof(struct hpte_cache
), 0, NULL
);
383 void kvmppc_mmu_hpte_sysexit(void)
385 kmem_cache_destroy(hpte_cache
);