2 * Copyright (C) 2010 SUSE Linux Products GmbH. All rights reserved.
5 * Alexander Graf <agraf@suse.de>
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.
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.
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.
21 #include <linux/kvm_host.h>
22 #include <linux/hash.h>
23 #include <linux/slab.h>
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>
35 static struct kmem_cache
*hpte_cache
;
37 static inline u64
kvmppc_mmu_hash_pte(u64 eaddr
)
39 return hash_64(eaddr
>> PTE_SIZE
, HPTEG_HASH_BITS_PTE
);
42 static inline u64
kvmppc_mmu_hash_pte_long(u64 eaddr
)
44 return hash_64((eaddr
& 0x0ffff000) >> PTE_SIZE
,
45 HPTEG_HASH_BITS_PTE_LONG
);
48 static inline u64
kvmppc_mmu_hash_vpte(u64 vpage
)
50 return hash_64(vpage
& 0xfffffffffULL
, HPTEG_HASH_BITS_VPTE
);
53 static inline u64
kvmppc_mmu_hash_vpte_long(u64 vpage
)
55 return hash_64((vpage
& 0xffffff000ULL
) >> 12,
56 HPTEG_HASH_BITS_VPTE_LONG
);
59 void kvmppc_mmu_hpte_cache_map(struct kvm_vcpu
*vcpu
, struct hpte_cache
*pte
)
62 struct kvmppc_vcpu_book3s
*vcpu3s
= to_book3s(vcpu
);
64 trace_kvm_book3s_mmu_map(pte
);
66 spin_lock(&vcpu3s
->mmu_lock
);
68 /* Add to ePTE list */
69 index
= kvmppc_mmu_hash_pte(pte
->pte
.eaddr
);
70 hlist_add_head_rcu(&pte
->list_pte
, &vcpu3s
->hpte_hash_pte
[index
]);
72 /* Add to ePTE_long list */
73 index
= kvmppc_mmu_hash_pte_long(pte
->pte
.eaddr
);
74 hlist_add_head_rcu(&pte
->list_pte_long
,
75 &vcpu3s
->hpte_hash_pte_long
[index
]);
77 /* Add to vPTE list */
78 index
= kvmppc_mmu_hash_vpte(pte
->pte
.vpage
);
79 hlist_add_head_rcu(&pte
->list_vpte
, &vcpu3s
->hpte_hash_vpte
[index
]);
81 /* Add to vPTE_long list */
82 index
= kvmppc_mmu_hash_vpte_long(pte
->pte
.vpage
);
83 hlist_add_head_rcu(&pte
->list_vpte_long
,
84 &vcpu3s
->hpte_hash_vpte_long
[index
]);
86 spin_unlock(&vcpu3s
->mmu_lock
);
89 static void free_pte_rcu(struct rcu_head
*head
)
91 struct hpte_cache
*pte
= container_of(head
, struct hpte_cache
, rcu_head
);
92 kmem_cache_free(hpte_cache
, pte
);
95 static void invalidate_pte(struct kvm_vcpu
*vcpu
, struct hpte_cache
*pte
)
97 struct kvmppc_vcpu_book3s
*vcpu3s
= to_book3s(vcpu
);
99 trace_kvm_book3s_mmu_invalidate(pte
);
101 /* Different for 32 and 64 bit */
102 kvmppc_mmu_invalidate_pte(vcpu
, pte
);
104 spin_lock(&vcpu3s
->mmu_lock
);
106 /* pte already invalidated in between? */
107 if (hlist_unhashed(&pte
->list_pte
)) {
108 spin_unlock(&vcpu3s
->mmu_lock
);
112 hlist_del_init_rcu(&pte
->list_pte
);
113 hlist_del_init_rcu(&pte
->list_pte_long
);
114 hlist_del_init_rcu(&pte
->list_vpte
);
115 hlist_del_init_rcu(&pte
->list_vpte_long
);
117 spin_unlock(&vcpu3s
->mmu_lock
);
119 vcpu3s
->hpte_cache_count
--;
120 call_rcu(&pte
->rcu_head
, free_pte_rcu
);
123 static void kvmppc_mmu_pte_flush_all(struct kvm_vcpu
*vcpu
)
125 struct kvmppc_vcpu_book3s
*vcpu3s
= to_book3s(vcpu
);
126 struct hpte_cache
*pte
;
131 for (i
= 0; i
< HPTEG_HASH_NUM_VPTE_LONG
; i
++) {
132 struct hlist_head
*list
= &vcpu3s
->hpte_hash_vpte_long
[i
];
134 hlist_for_each_entry_rcu(pte
, list
, list_vpte_long
)
135 invalidate_pte(vcpu
, pte
);
141 static void kvmppc_mmu_pte_flush_page(struct kvm_vcpu
*vcpu
, ulong guest_ea
)
143 struct kvmppc_vcpu_book3s
*vcpu3s
= to_book3s(vcpu
);
144 struct hlist_head
*list
;
145 struct hpte_cache
*pte
;
147 /* Find the list of entries in the map */
148 list
= &vcpu3s
->hpte_hash_pte
[kvmppc_mmu_hash_pte(guest_ea
)];
152 /* Check the list for matching entries and invalidate */
153 hlist_for_each_entry_rcu(pte
, list
, list_pte
)
154 if ((pte
->pte
.eaddr
& ~0xfffUL
) == guest_ea
)
155 invalidate_pte(vcpu
, pte
);
160 static void kvmppc_mmu_pte_flush_long(struct kvm_vcpu
*vcpu
, ulong guest_ea
)
162 struct kvmppc_vcpu_book3s
*vcpu3s
= to_book3s(vcpu
);
163 struct hlist_head
*list
;
164 struct hpte_cache
*pte
;
166 /* Find the list of entries in the map */
167 list
= &vcpu3s
->hpte_hash_pte_long
[
168 kvmppc_mmu_hash_pte_long(guest_ea
)];
172 /* Check the list for matching entries and invalidate */
173 hlist_for_each_entry_rcu(pte
, list
, list_pte_long
)
174 if ((pte
->pte
.eaddr
& 0x0ffff000UL
) == guest_ea
)
175 invalidate_pte(vcpu
, pte
);
180 void kvmppc_mmu_pte_flush(struct kvm_vcpu
*vcpu
, ulong guest_ea
, ulong ea_mask
)
182 trace_kvm_book3s_mmu_flush("", vcpu
, guest_ea
, ea_mask
);
187 kvmppc_mmu_pte_flush_page(vcpu
, guest_ea
);
190 kvmppc_mmu_pte_flush_long(vcpu
, guest_ea
);
193 /* Doing a complete flush -> start from scratch */
194 kvmppc_mmu_pte_flush_all(vcpu
);
202 /* Flush with mask 0xfffffffff */
203 static void kvmppc_mmu_pte_vflush_short(struct kvm_vcpu
*vcpu
, u64 guest_vp
)
205 struct kvmppc_vcpu_book3s
*vcpu3s
= to_book3s(vcpu
);
206 struct hlist_head
*list
;
207 struct hpte_cache
*pte
;
208 u64 vp_mask
= 0xfffffffffULL
;
210 list
= &vcpu3s
->hpte_hash_vpte
[kvmppc_mmu_hash_vpte(guest_vp
)];
214 /* Check the list for matching entries and invalidate */
215 hlist_for_each_entry_rcu(pte
, list
, list_vpte
)
216 if ((pte
->pte
.vpage
& vp_mask
) == guest_vp
)
217 invalidate_pte(vcpu
, pte
);
222 /* Flush with mask 0xffffff000 */
223 static void kvmppc_mmu_pte_vflush_long(struct kvm_vcpu
*vcpu
, u64 guest_vp
)
225 struct kvmppc_vcpu_book3s
*vcpu3s
= to_book3s(vcpu
);
226 struct hlist_head
*list
;
227 struct hpte_cache
*pte
;
228 u64 vp_mask
= 0xffffff000ULL
;
230 list
= &vcpu3s
->hpte_hash_vpte_long
[
231 kvmppc_mmu_hash_vpte_long(guest_vp
)];
235 /* Check the list for matching entries and invalidate */
236 hlist_for_each_entry_rcu(pte
, list
, list_vpte_long
)
237 if ((pte
->pte
.vpage
& vp_mask
) == guest_vp
)
238 invalidate_pte(vcpu
, pte
);
243 void kvmppc_mmu_pte_vflush(struct kvm_vcpu
*vcpu
, u64 guest_vp
, u64 vp_mask
)
245 trace_kvm_book3s_mmu_flush("v", vcpu
, guest_vp
, vp_mask
);
250 kvmppc_mmu_pte_vflush_short(vcpu
, guest_vp
);
253 kvmppc_mmu_pte_vflush_long(vcpu
, guest_vp
);
261 void kvmppc_mmu_pte_pflush(struct kvm_vcpu
*vcpu
, ulong pa_start
, ulong pa_end
)
263 struct kvmppc_vcpu_book3s
*vcpu3s
= to_book3s(vcpu
);
264 struct hpte_cache
*pte
;
267 trace_kvm_book3s_mmu_flush("p", vcpu
, pa_start
, pa_end
);
271 for (i
= 0; i
< HPTEG_HASH_NUM_VPTE_LONG
; i
++) {
272 struct hlist_head
*list
= &vcpu3s
->hpte_hash_vpte_long
[i
];
274 hlist_for_each_entry_rcu(pte
, list
, list_vpte_long
)
275 if ((pte
->pte
.raddr
>= pa_start
) &&
276 (pte
->pte
.raddr
< pa_end
))
277 invalidate_pte(vcpu
, pte
);
283 struct hpte_cache
*kvmppc_mmu_hpte_cache_next(struct kvm_vcpu
*vcpu
)
285 struct kvmppc_vcpu_book3s
*vcpu3s
= to_book3s(vcpu
);
286 struct hpte_cache
*pte
;
288 pte
= kmem_cache_zalloc(hpte_cache
, GFP_KERNEL
);
289 vcpu3s
->hpte_cache_count
++;
291 if (vcpu3s
->hpte_cache_count
== HPTEG_CACHE_NUM
)
292 kvmppc_mmu_pte_flush_all(vcpu
);
297 void kvmppc_mmu_hpte_destroy(struct kvm_vcpu
*vcpu
)
299 kvmppc_mmu_pte_flush(vcpu
, 0, 0);
302 static void kvmppc_mmu_hpte_init_hash(struct hlist_head
*hash_list
, int len
)
306 for (i
= 0; i
< len
; i
++)
307 INIT_HLIST_HEAD(&hash_list
[i
]);
310 int kvmppc_mmu_hpte_init(struct kvm_vcpu
*vcpu
)
312 struct kvmppc_vcpu_book3s
*vcpu3s
= to_book3s(vcpu
);
314 /* 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
));
324 spin_lock_init(&vcpu3s
->mmu_lock
);
329 int kvmppc_mmu_hpte_sysinit(void)
331 /* init hpte slab cache */
332 hpte_cache
= kmem_cache_create("kvm-spt", sizeof(struct hpte_cache
),
333 sizeof(struct hpte_cache
), 0, NULL
);
338 void kvmppc_mmu_hpte_sysexit(void)
340 kmem_cache_destroy(hpte_cache
);