blob: 41cb0017e757a1d8ccd83360624d9f1a40189426 [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
Alexander Graf77419092010-06-30 15:18:45 +0200117 if (pte->pte.may_write)
118 kvm_release_pfn_dirty(pte->pfn);
119 else
120 kvm_release_pfn_clean(pte->pfn);
121
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000122 spin_unlock(&vcpu3s->mmu_lock);
Alexander Grafe7c1d142010-08-02 21:24:48 +0200123
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000124 vcpu3s->hpte_cache_count--;
Alexander Graf2e0908a2010-07-29 15:04:17 +0200125 call_rcu(&pte->rcu_head, free_pte_rcu);
Alexander Graf77419092010-06-30 15:18:45 +0200126}
127
128static void kvmppc_mmu_pte_flush_all(struct kvm_vcpu *vcpu)
129{
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000130 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
Alexander Graf77419092010-06-30 15:18:45 +0200131 struct hpte_cache *pte;
Alexander Graf2e0908a2010-07-29 15:04:17 +0200132 struct hlist_node *node;
Alexander Graf77419092010-06-30 15:18:45 +0200133 int i;
134
Alexander Graf2e0908a2010-07-29 15:04:17 +0200135 rcu_read_lock();
136
Alexander Graf77419092010-06-30 15:18:45 +0200137 for (i = 0; i < HPTEG_HASH_NUM_VPTE_LONG; i++) {
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000138 struct hlist_head *list = &vcpu3s->hpte_hash_vpte_long[i];
Alexander Graf77419092010-06-30 15:18:45 +0200139
Alexander Graf2e0908a2010-07-29 15:04:17 +0200140 hlist_for_each_entry_rcu(pte, node, list, list_vpte_long)
Alexander Graf77419092010-06-30 15:18:45 +0200141 invalidate_pte(vcpu, pte);
142 }
Alexander Graf2e0908a2010-07-29 15:04:17 +0200143
144 rcu_read_unlock();
Alexander Graf77419092010-06-30 15:18:45 +0200145}
146
147static void kvmppc_mmu_pte_flush_page(struct kvm_vcpu *vcpu, ulong guest_ea)
148{
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000149 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
Alexander Graf77419092010-06-30 15:18:45 +0200150 struct hlist_head *list;
Alexander Graf2e0908a2010-07-29 15:04:17 +0200151 struct hlist_node *node;
Alexander Graf77419092010-06-30 15:18:45 +0200152 struct hpte_cache *pte;
153
154 /* Find the list of entries in the map */
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000155 list = &vcpu3s->hpte_hash_pte[kvmppc_mmu_hash_pte(guest_ea)];
Alexander Graf77419092010-06-30 15:18:45 +0200156
Alexander Graf2e0908a2010-07-29 15:04:17 +0200157 rcu_read_lock();
158
Alexander Graf77419092010-06-30 15:18:45 +0200159 /* Check the list for matching entries and invalidate */
Alexander Graf2e0908a2010-07-29 15:04:17 +0200160 hlist_for_each_entry_rcu(pte, node, list, list_pte)
Alexander Graf77419092010-06-30 15:18:45 +0200161 if ((pte->pte.eaddr & ~0xfffUL) == guest_ea)
162 invalidate_pte(vcpu, pte);
Alexander Graf2e0908a2010-07-29 15:04:17 +0200163
164 rcu_read_unlock();
Alexander Graf77419092010-06-30 15:18:45 +0200165}
166
Alexander Graf2d27fc52010-07-29 15:04:19 +0200167static void kvmppc_mmu_pte_flush_long(struct kvm_vcpu *vcpu, ulong guest_ea)
168{
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000169 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
Alexander Graf2d27fc52010-07-29 15:04:19 +0200170 struct hlist_head *list;
171 struct hlist_node *node;
172 struct hpte_cache *pte;
173
174 /* Find the list of entries in the map */
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000175 list = &vcpu3s->hpte_hash_pte_long[
Alexander Graf2d27fc52010-07-29 15:04:19 +0200176 kvmppc_mmu_hash_pte_long(guest_ea)];
177
178 rcu_read_lock();
179
180 /* Check the list for matching entries and invalidate */
181 hlist_for_each_entry_rcu(pte, node, list, list_pte_long)
182 if ((pte->pte.eaddr & 0x0ffff000UL) == guest_ea)
183 invalidate_pte(vcpu, pte);
184
185 rcu_read_unlock();
186}
187
Alexander Graf77419092010-06-30 15:18:45 +0200188void kvmppc_mmu_pte_flush(struct kvm_vcpu *vcpu, ulong guest_ea, ulong ea_mask)
189{
Alexander Grafc60b4cf2010-08-02 13:40:30 +0200190 trace_kvm_book3s_mmu_flush("", vcpu, guest_ea, ea_mask);
Alexander Graf77419092010-06-30 15:18:45 +0200191 guest_ea &= ea_mask;
192
193 switch (ea_mask) {
194 case ~0xfffUL:
195 kvmppc_mmu_pte_flush_page(vcpu, guest_ea);
196 break;
197 case 0x0ffff000:
Alexander Graf2d27fc52010-07-29 15:04:19 +0200198 kvmppc_mmu_pte_flush_long(vcpu, guest_ea);
Alexander Graf77419092010-06-30 15:18:45 +0200199 break;
200 case 0:
201 /* Doing a complete flush -> start from scratch */
202 kvmppc_mmu_pte_flush_all(vcpu);
203 break;
204 default:
205 WARN_ON(1);
206 break;
207 }
208}
209
210/* Flush with mask 0xfffffffff */
211static void kvmppc_mmu_pte_vflush_short(struct kvm_vcpu *vcpu, u64 guest_vp)
212{
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000213 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
Alexander Graf77419092010-06-30 15:18:45 +0200214 struct hlist_head *list;
Alexander Graf2e0908a2010-07-29 15:04:17 +0200215 struct hlist_node *node;
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 */
Alexander Graf2e0908a2010-07-29 15:04:17 +0200224 hlist_for_each_entry_rcu(pte, node, 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
231/* Flush with mask 0xffffff000 */
232static void kvmppc_mmu_pte_vflush_long(struct kvm_vcpu *vcpu, u64 guest_vp)
233{
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000234 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
Alexander Graf77419092010-06-30 15:18:45 +0200235 struct hlist_head *list;
Alexander Graf2e0908a2010-07-29 15:04:17 +0200236 struct hlist_node *node;
Alexander Graf77419092010-06-30 15:18:45 +0200237 struct hpte_cache *pte;
238 u64 vp_mask = 0xffffff000ULL;
239
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000240 list = &vcpu3s->hpte_hash_vpte_long[
Alexander Graf77419092010-06-30 15:18:45 +0200241 kvmppc_mmu_hash_vpte_long(guest_vp)];
242
Alexander Graf2e0908a2010-07-29 15:04:17 +0200243 rcu_read_lock();
244
Alexander Graf77419092010-06-30 15:18:45 +0200245 /* Check the list for matching entries and invalidate */
Alexander Graf2e0908a2010-07-29 15:04:17 +0200246 hlist_for_each_entry_rcu(pte, node, list, list_vpte_long)
Alexander Graf77419092010-06-30 15:18:45 +0200247 if ((pte->pte.vpage & vp_mask) == guest_vp)
248 invalidate_pte(vcpu, pte);
Alexander Graf2e0908a2010-07-29 15:04:17 +0200249
250 rcu_read_unlock();
Alexander Graf77419092010-06-30 15:18:45 +0200251}
252
253void kvmppc_mmu_pte_vflush(struct kvm_vcpu *vcpu, u64 guest_vp, u64 vp_mask)
254{
Alexander Grafc60b4cf2010-08-02 13:40:30 +0200255 trace_kvm_book3s_mmu_flush("v", vcpu, guest_vp, vp_mask);
Alexander Graf77419092010-06-30 15:18:45 +0200256 guest_vp &= vp_mask;
257
258 switch(vp_mask) {
259 case 0xfffffffffULL:
260 kvmppc_mmu_pte_vflush_short(vcpu, guest_vp);
261 break;
262 case 0xffffff000ULL:
263 kvmppc_mmu_pte_vflush_long(vcpu, guest_vp);
264 break;
265 default:
266 WARN_ON(1);
267 return;
268 }
269}
270
271void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end)
272{
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000273 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
Alexander Graf2e0908a2010-07-29 15:04:17 +0200274 struct hlist_node *node;
Alexander Graf77419092010-06-30 15:18:45 +0200275 struct hpte_cache *pte;
276 int i;
277
Alexander Grafc60b4cf2010-08-02 13:40:30 +0200278 trace_kvm_book3s_mmu_flush("p", vcpu, pa_start, pa_end);
Alexander Graf77419092010-06-30 15:18:45 +0200279
Alexander Graf2e0908a2010-07-29 15:04:17 +0200280 rcu_read_lock();
281
Alexander Graf77419092010-06-30 15:18:45 +0200282 for (i = 0; i < HPTEG_HASH_NUM_VPTE_LONG; i++) {
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000283 struct hlist_head *list = &vcpu3s->hpte_hash_vpte_long[i];
Alexander Graf77419092010-06-30 15:18:45 +0200284
Alexander Graf2e0908a2010-07-29 15:04:17 +0200285 hlist_for_each_entry_rcu(pte, node, list, list_vpte_long)
Alexander Graf77419092010-06-30 15:18:45 +0200286 if ((pte->pte.raddr >= pa_start) &&
287 (pte->pte.raddr < pa_end))
288 invalidate_pte(vcpu, pte);
289 }
Alexander Graf2e0908a2010-07-29 15:04:17 +0200290
291 rcu_read_unlock();
Alexander Graf77419092010-06-30 15:18:45 +0200292}
293
294struct hpte_cache *kvmppc_mmu_hpte_cache_next(struct kvm_vcpu *vcpu)
295{
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000296 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
Alexander Graf77419092010-06-30 15:18:45 +0200297 struct hpte_cache *pte;
298
299 pte = kmem_cache_zalloc(hpte_cache, GFP_KERNEL);
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000300 vcpu3s->hpte_cache_count++;
Alexander Graf77419092010-06-30 15:18:45 +0200301
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000302 if (vcpu3s->hpte_cache_count == HPTEG_CACHE_NUM)
Alexander Graf77419092010-06-30 15:18:45 +0200303 kvmppc_mmu_pte_flush_all(vcpu);
304
305 return pte;
306}
307
308void kvmppc_mmu_hpte_destroy(struct kvm_vcpu *vcpu)
309{
310 kvmppc_mmu_pte_flush(vcpu, 0, 0);
311}
312
313static void kvmppc_mmu_hpte_init_hash(struct hlist_head *hash_list, int len)
314{
315 int i;
316
317 for (i = 0; i < len; i++)
318 INIT_HLIST_HEAD(&hash_list[i]);
319}
320
321int kvmppc_mmu_hpte_init(struct kvm_vcpu *vcpu)
322{
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000323 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
Alexander Graf77419092010-06-30 15:18:45 +0200324
Paul Mackerrasc4befc52011-06-29 00:17:33 +0000325 /* init hpte lookup hashes */
326 kvmppc_mmu_hpte_init_hash(vcpu3s->hpte_hash_pte,
327 ARRAY_SIZE(vcpu3s->hpte_hash_pte));
328 kvmppc_mmu_hpte_init_hash(vcpu3s->hpte_hash_pte_long,
329 ARRAY_SIZE(vcpu3s->hpte_hash_pte_long));
330 kvmppc_mmu_hpte_init_hash(vcpu3s->hpte_hash_vpte,
331 ARRAY_SIZE(vcpu3s->hpte_hash_vpte));
332 kvmppc_mmu_hpte_init_hash(vcpu3s->hpte_hash_vpte_long,
333 ARRAY_SIZE(vcpu3s->hpte_hash_vpte_long));
334
335 spin_lock_init(&vcpu3s->mmu_lock);
Alexander Graf2e0908a2010-07-29 15:04:17 +0200336
Alexander Graf77419092010-06-30 15:18:45 +0200337 return 0;
338}
339
340int kvmppc_mmu_hpte_sysinit(void)
341{
342 /* init hpte slab cache */
343 hpte_cache = kmem_cache_create("kvm-spt", sizeof(struct hpte_cache),
344 sizeof(struct hpte_cache), 0, NULL);
345
346 return 0;
347}
348
349void kvmppc_mmu_hpte_sysexit(void)
350{
351 kmem_cache_destroy(hpte_cache);
352}