blob: da8b13c4b776fb5e3116d24ee2f380bed524ec72 [file] [log] [blame]
Alexander Graf77419092010-06-30 15:18:45 +02001/*
2 * Copyright (C) 2010 SUSE Linux Products GmbH. All rights reserved.
3 *
4 * Authors:
5 * Alexander Graf <agraf@suse.de>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License, version 2, as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20
21#include <linux/kvm_host.h>
22#include <linux/hash.h>
23#include <linux/slab.h>
24
25#include <asm/kvm_ppc.h>
26#include <asm/kvm_book3s.h>
27#include <asm/machdep.h>
28#include <asm/mmu_context.h>
29#include <asm/hw_irq.h>
30
Paul Mackerrasc4befc52011-06-29 00:17:33 +000031#include "trace.h"
32
Alexander Graf77419092010-06-30 15:18:45 +020033#define PTE_SIZE 12
34
Alexander Graf77419092010-06-30 15:18:45 +020035static struct kmem_cache *hpte_cache;
36
37static inline u64 kvmppc_mmu_hash_pte(u64 eaddr)
38{
39 return hash_64(eaddr >> PTE_SIZE, HPTEG_HASH_BITS_PTE);
40}
41
Alexander Graf2d27fc52010-07-29 15:04:19 +020042static inline u64 kvmppc_mmu_hash_pte_long(u64 eaddr)
43{
44 return hash_64((eaddr & 0x0ffff000) >> PTE_SIZE,
45 HPTEG_HASH_BITS_PTE_LONG);
46}
47
Alexander Graf77419092010-06-30 15:18:45 +020048static inline u64 kvmppc_mmu_hash_vpte(u64 vpage)
49{
50 return hash_64(vpage & 0xfffffffffULL, HPTEG_HASH_BITS_VPTE);
51}
52
53static inline u64 kvmppc_mmu_hash_vpte_long(u64 vpage)
54{
55 return hash_64((vpage & 0xffffff000ULL) >> 12,
56 HPTEG_HASH_BITS_VPTE_LONG);
57}
58
59void kvmppc_mmu_hpte_cache_map(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
60{
61 u64 index;
Paul Mackerrasc4befc52011-06-29 00:17:33 +000062 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
Alexander Graf77419092010-06-30 15:18:45 +020063
Alexander Graf4c4eea72010-08-02 12:51:07 +020064 trace_kvm_book3s_mmu_map(pte);
65
Paul Mackerrasc4befc52011-06-29 00:17:33 +000066 spin_lock(&vcpu3s->mmu_lock);
Alexander Graf2e0908a2010-07-29 15:04:17 +020067
Alexander Graf77419092010-06-30 15:18:45 +020068 /* Add to ePTE list */
69 index = kvmppc_mmu_hash_pte(pte->pte.eaddr);
Paul Mackerrasc4befc52011-06-29 00:17:33 +000070 hlist_add_head_rcu(&pte->list_pte, &vcpu3s->hpte_hash_pte[index]);
Alexander Graf77419092010-06-30 15:18:45 +020071
Alexander Graf2d27fc52010-07-29 15:04:19 +020072 /* Add to ePTE_long list */
73 index = kvmppc_mmu_hash_pte_long(pte->pte.eaddr);
74 hlist_add_head_rcu(&pte->list_pte_long,
Paul Mackerrasc4befc52011-06-29 00:17:33 +000075 &vcpu3s->hpte_hash_pte_long[index]);
Alexander Graf2d27fc52010-07-29 15:04:19 +020076
Alexander Graf77419092010-06-30 15:18:45 +020077 /* Add to vPTE list */
78 index = kvmppc_mmu_hash_vpte(pte->pte.vpage);
Paul Mackerrasc4befc52011-06-29 00:17:33 +000079 hlist_add_head_rcu(&pte->list_vpte, &vcpu3s->hpte_hash_vpte[index]);
Alexander Graf77419092010-06-30 15:18:45 +020080
81 /* Add to vPTE_long list */
82 index = kvmppc_mmu_hash_vpte_long(pte->pte.vpage);
Alexander Graf2e0908a2010-07-29 15:04:17 +020083 hlist_add_head_rcu(&pte->list_vpte_long,
Paul Mackerrasc4befc52011-06-29 00:17:33 +000084 &vcpu3s->hpte_hash_vpte_long[index]);
Alexander Graf2e0908a2010-07-29 15:04:17 +020085
Paul Mackerrasc4befc52011-06-29 00:17:33 +000086 spin_unlock(&vcpu3s->mmu_lock);
Alexander Graf2e0908a2010-07-29 15:04:17 +020087}
88
89static void free_pte_rcu(struct rcu_head *head)
90{
91 struct hpte_cache *pte = container_of(head, struct hpte_cache, rcu_head);
92 kmem_cache_free(hpte_cache, pte);
Alexander Graf77419092010-06-30 15:18:45 +020093}
94
95static void invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
96{
Paul Mackerrasc4befc52011-06-29 00:17:33 +000097 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
98
Alexander Graf8696ee42010-08-02 12:55:19 +020099 trace_kvm_book3s_mmu_invalidate(pte);
Alexander Graf77419092010-06-30 15:18:45 +0200100
101 /* Different for 32 and 64 bit */
102 kvmppc_mmu_invalidate_pte(vcpu, pte);
103
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000104 spin_lock(&vcpu3s->mmu_lock);
Alexander Graf2e0908a2010-07-29 15:04:17 +0200105
Alexander Grafe7c1d142010-08-02 21:24:48 +0200106 /* pte already invalidated in between? */
107 if (hlist_unhashed(&pte->list_pte)) {
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000108 spin_unlock(&vcpu3s->mmu_lock);
Alexander Grafe7c1d142010-08-02 21:24:48 +0200109 return;
110 }
111
Alexander Graf2e0908a2010-07-29 15:04:17 +0200112 hlist_del_init_rcu(&pte->list_pte);
Alexander Graf2d27fc52010-07-29 15:04:19 +0200113 hlist_del_init_rcu(&pte->list_pte_long);
Alexander Graf2e0908a2010-07-29 15:04:17 +0200114 hlist_del_init_rcu(&pte->list_vpte);
115 hlist_del_init_rcu(&pte->list_vpte_long);
116
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000117 spin_unlock(&vcpu3s->mmu_lock);
Alexander Grafe7c1d142010-08-02 21:24:48 +0200118
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000119 vcpu3s->hpte_cache_count--;
Alexander Graf2e0908a2010-07-29 15:04:17 +0200120 call_rcu(&pte->rcu_head, free_pte_rcu);
Alexander Graf77419092010-06-30 15:18:45 +0200121}
122
123static void kvmppc_mmu_pte_flush_all(struct kvm_vcpu *vcpu)
124{
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000125 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
Alexander Graf77419092010-06-30 15:18:45 +0200126 struct hpte_cache *pte;
Alexander Graf77419092010-06-30 15:18:45 +0200127 int i;
128
Alexander Graf2e0908a2010-07-29 15:04:17 +0200129 rcu_read_lock();
130
Alexander Graf77419092010-06-30 15:18:45 +0200131 for (i = 0; i < HPTEG_HASH_NUM_VPTE_LONG; i++) {
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000132 struct hlist_head *list = &vcpu3s->hpte_hash_vpte_long[i];
Alexander Graf77419092010-06-30 15:18:45 +0200133
Sasha Levinb67bfe02013-02-27 17:06:00 -0800134 hlist_for_each_entry_rcu(pte, list, list_vpte_long)
Alexander Graf77419092010-06-30 15:18:45 +0200135 invalidate_pte(vcpu, pte);
136 }
Alexander Graf2e0908a2010-07-29 15:04:17 +0200137
138 rcu_read_unlock();
Alexander Graf77419092010-06-30 15:18:45 +0200139}
140
141static void kvmppc_mmu_pte_flush_page(struct kvm_vcpu *vcpu, ulong guest_ea)
142{
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000143 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
Alexander Graf77419092010-06-30 15:18:45 +0200144 struct hlist_head *list;
Alexander Graf77419092010-06-30 15:18:45 +0200145 struct hpte_cache *pte;
146
147 /* Find the list of entries in the map */
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000148 list = &vcpu3s->hpte_hash_pte[kvmppc_mmu_hash_pte(guest_ea)];
Alexander Graf77419092010-06-30 15:18:45 +0200149
Alexander Graf2e0908a2010-07-29 15:04:17 +0200150 rcu_read_lock();
151
Alexander Graf77419092010-06-30 15:18:45 +0200152 /* Check the list for matching entries and invalidate */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800153 hlist_for_each_entry_rcu(pte, list, list_pte)
Alexander Graf77419092010-06-30 15:18:45 +0200154 if ((pte->pte.eaddr & ~0xfffUL) == guest_ea)
155 invalidate_pte(vcpu, pte);
Alexander Graf2e0908a2010-07-29 15:04:17 +0200156
157 rcu_read_unlock();
Alexander Graf77419092010-06-30 15:18:45 +0200158}
159
Alexander Graf2d27fc52010-07-29 15:04:19 +0200160static void kvmppc_mmu_pte_flush_long(struct kvm_vcpu *vcpu, ulong guest_ea)
161{
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000162 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
Alexander Graf2d27fc52010-07-29 15:04:19 +0200163 struct hlist_head *list;
Alexander Graf2d27fc52010-07-29 15:04:19 +0200164 struct hpte_cache *pte;
165
166 /* Find the list of entries in the map */
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000167 list = &vcpu3s->hpte_hash_pte_long[
Alexander Graf2d27fc52010-07-29 15:04:19 +0200168 kvmppc_mmu_hash_pte_long(guest_ea)];
169
170 rcu_read_lock();
171
172 /* Check the list for matching entries and invalidate */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800173 hlist_for_each_entry_rcu(pte, list, list_pte_long)
Alexander Graf2d27fc52010-07-29 15:04:19 +0200174 if ((pte->pte.eaddr & 0x0ffff000UL) == guest_ea)
175 invalidate_pte(vcpu, pte);
176
177 rcu_read_unlock();
178}
179
Alexander Graf77419092010-06-30 15:18:45 +0200180void kvmppc_mmu_pte_flush(struct kvm_vcpu *vcpu, ulong guest_ea, ulong ea_mask)
181{
Alexander Grafc60b4cf2010-08-02 13:40:30 +0200182 trace_kvm_book3s_mmu_flush("", vcpu, guest_ea, ea_mask);
Alexander Graf77419092010-06-30 15:18:45 +0200183 guest_ea &= ea_mask;
184
185 switch (ea_mask) {
186 case ~0xfffUL:
187 kvmppc_mmu_pte_flush_page(vcpu, guest_ea);
188 break;
189 case 0x0ffff000:
Alexander Graf2d27fc52010-07-29 15:04:19 +0200190 kvmppc_mmu_pte_flush_long(vcpu, guest_ea);
Alexander Graf77419092010-06-30 15:18:45 +0200191 break;
192 case 0:
193 /* Doing a complete flush -> start from scratch */
194 kvmppc_mmu_pte_flush_all(vcpu);
195 break;
196 default:
197 WARN_ON(1);
198 break;
199 }
200}
201
202/* Flush with mask 0xfffffffff */
203static void kvmppc_mmu_pte_vflush_short(struct kvm_vcpu *vcpu, u64 guest_vp)
204{
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000205 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
Alexander Graf77419092010-06-30 15:18:45 +0200206 struct hlist_head *list;
Alexander Graf77419092010-06-30 15:18:45 +0200207 struct hpte_cache *pte;
208 u64 vp_mask = 0xfffffffffULL;
209
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000210 list = &vcpu3s->hpte_hash_vpte[kvmppc_mmu_hash_vpte(guest_vp)];
Alexander Graf77419092010-06-30 15:18:45 +0200211
Alexander Graf2e0908a2010-07-29 15:04:17 +0200212 rcu_read_lock();
213
Alexander Graf77419092010-06-30 15:18:45 +0200214 /* Check the list for matching entries and invalidate */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800215 hlist_for_each_entry_rcu(pte, list, list_vpte)
Alexander Graf77419092010-06-30 15:18:45 +0200216 if ((pte->pte.vpage & vp_mask) == guest_vp)
217 invalidate_pte(vcpu, pte);
Alexander Graf2e0908a2010-07-29 15:04:17 +0200218
219 rcu_read_unlock();
Alexander Graf77419092010-06-30 15:18:45 +0200220}
221
222/* Flush with mask 0xffffff000 */
223static void kvmppc_mmu_pte_vflush_long(struct kvm_vcpu *vcpu, u64 guest_vp)
224{
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000225 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
Alexander Graf77419092010-06-30 15:18:45 +0200226 struct hlist_head *list;
Alexander Graf77419092010-06-30 15:18:45 +0200227 struct hpte_cache *pte;
228 u64 vp_mask = 0xffffff000ULL;
229
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000230 list = &vcpu3s->hpte_hash_vpte_long[
Alexander Graf77419092010-06-30 15:18:45 +0200231 kvmppc_mmu_hash_vpte_long(guest_vp)];
232
Alexander Graf2e0908a2010-07-29 15:04:17 +0200233 rcu_read_lock();
234
Alexander Graf77419092010-06-30 15:18:45 +0200235 /* Check the list for matching entries and invalidate */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800236 hlist_for_each_entry_rcu(pte, list, list_vpte_long)
Alexander Graf77419092010-06-30 15:18:45 +0200237 if ((pte->pte.vpage & vp_mask) == guest_vp)
238 invalidate_pte(vcpu, pte);
Alexander Graf2e0908a2010-07-29 15:04:17 +0200239
240 rcu_read_unlock();
Alexander Graf77419092010-06-30 15:18:45 +0200241}
242
243void kvmppc_mmu_pte_vflush(struct kvm_vcpu *vcpu, u64 guest_vp, u64 vp_mask)
244{
Alexander Grafc60b4cf2010-08-02 13:40:30 +0200245 trace_kvm_book3s_mmu_flush("v", vcpu, guest_vp, vp_mask);
Alexander Graf77419092010-06-30 15:18:45 +0200246 guest_vp &= vp_mask;
247
248 switch(vp_mask) {
249 case 0xfffffffffULL:
250 kvmppc_mmu_pte_vflush_short(vcpu, guest_vp);
251 break;
252 case 0xffffff000ULL:
253 kvmppc_mmu_pte_vflush_long(vcpu, guest_vp);
254 break;
255 default:
256 WARN_ON(1);
257 return;
258 }
259}
260
261void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end)
262{
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000263 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
Alexander Graf77419092010-06-30 15:18:45 +0200264 struct hpte_cache *pte;
265 int i;
266
Alexander Grafc60b4cf2010-08-02 13:40:30 +0200267 trace_kvm_book3s_mmu_flush("p", vcpu, pa_start, pa_end);
Alexander Graf77419092010-06-30 15:18:45 +0200268
Alexander Graf2e0908a2010-07-29 15:04:17 +0200269 rcu_read_lock();
270
Alexander Graf77419092010-06-30 15:18:45 +0200271 for (i = 0; i < HPTEG_HASH_NUM_VPTE_LONG; i++) {
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000272 struct hlist_head *list = &vcpu3s->hpte_hash_vpte_long[i];
Alexander Graf77419092010-06-30 15:18:45 +0200273
Sasha Levinb67bfe02013-02-27 17:06:00 -0800274 hlist_for_each_entry_rcu(pte, list, list_vpte_long)
Alexander Graf77419092010-06-30 15:18:45 +0200275 if ((pte->pte.raddr >= pa_start) &&
276 (pte->pte.raddr < pa_end))
277 invalidate_pte(vcpu, pte);
278 }
Alexander Graf2e0908a2010-07-29 15:04:17 +0200279
280 rcu_read_unlock();
Alexander Graf77419092010-06-30 15:18:45 +0200281}
282
283struct hpte_cache *kvmppc_mmu_hpte_cache_next(struct kvm_vcpu *vcpu)
284{
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000285 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
Alexander Graf77419092010-06-30 15:18:45 +0200286 struct hpte_cache *pte;
287
288 pte = kmem_cache_zalloc(hpte_cache, GFP_KERNEL);
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000289 vcpu3s->hpte_cache_count++;
Alexander Graf77419092010-06-30 15:18:45 +0200290
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000291 if (vcpu3s->hpte_cache_count == HPTEG_CACHE_NUM)
Alexander Graf77419092010-06-30 15:18:45 +0200292 kvmppc_mmu_pte_flush_all(vcpu);
293
294 return pte;
295}
296
297void kvmppc_mmu_hpte_destroy(struct kvm_vcpu *vcpu)
298{
299 kvmppc_mmu_pte_flush(vcpu, 0, 0);
300}
301
302static void kvmppc_mmu_hpte_init_hash(struct hlist_head *hash_list, int len)
303{
304 int i;
305
306 for (i = 0; i < len; i++)
307 INIT_HLIST_HEAD(&hash_list[i]);
308}
309
310int kvmppc_mmu_hpte_init(struct kvm_vcpu *vcpu)
311{
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000312 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
Alexander Graf77419092010-06-30 15:18:45 +0200313
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000314 /* init hpte lookup hashes */
315 kvmppc_mmu_hpte_init_hash(vcpu3s->hpte_hash_pte,
316 ARRAY_SIZE(vcpu3s->hpte_hash_pte));
317 kvmppc_mmu_hpte_init_hash(vcpu3s->hpte_hash_pte_long,
318 ARRAY_SIZE(vcpu3s->hpte_hash_pte_long));
319 kvmppc_mmu_hpte_init_hash(vcpu3s->hpte_hash_vpte,
320 ARRAY_SIZE(vcpu3s->hpte_hash_vpte));
321 kvmppc_mmu_hpte_init_hash(vcpu3s->hpte_hash_vpte_long,
322 ARRAY_SIZE(vcpu3s->hpte_hash_vpte_long));
323
324 spin_lock_init(&vcpu3s->mmu_lock);
Alexander Graf2e0908a2010-07-29 15:04:17 +0200325
Alexander Graf77419092010-06-30 15:18:45 +0200326 return 0;
327}
328
329int kvmppc_mmu_hpte_sysinit(void)
330{
331 /* init hpte slab cache */
332 hpte_cache = kmem_cache_create("kvm-spt", sizeof(struct hpte_cache),
333 sizeof(struct hpte_cache), 0, NULL);
334
335 return 0;
336}
337
338void kvmppc_mmu_hpte_sysexit(void)
339{
340 kmem_cache_destroy(hpte_cache);
341}