2 * Copyright (C) 2009 SUSE Linux Products GmbH. All rights reserved.
5 * Alexander Graf <agraf@suse.de>
6 * Kevin Wolf <mail@kevin-wolf.de>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License, version 2, as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <linux/kvm_host.h>
24 #include <asm/kvm_ppc.h>
25 #include <asm/kvm_book3s.h>
26 #include <asm/mmu-hash64.h>
27 #include <asm/machdep.h>
28 #include <asm/mmu_context.h>
29 #include <asm/hw_irq.h>
34 /* #define DEBUG_MMU */
35 /* #define DEBUG_SLB */
38 #define dprintk_mmu(a, ...) printk(KERN_INFO a, __VA_ARGS__)
40 #define dprintk_mmu(a, ...) do { } while(0)
44 #define dprintk_slb(a, ...) printk(KERN_INFO a, __VA_ARGS__)
46 #define dprintk_slb(a, ...) do { } while(0)
49 static void invalidate_pte(struct hpte_cache *pte)
51 dprintk_mmu("KVM: Flushing SPT %d: 0x%llx (0x%llx) -> 0x%llx\n",
52 i, pte->pte.eaddr, pte->pte.vpage, pte->host_va);
54 ppc_md.hpte_invalidate(pte->slot, pte->host_va,
55 MMU_PAGE_4K, MMU_SEGSIZE_256M,
59 if (pte->pte.may_write)
60 kvm_release_pfn_dirty(pte->pfn);
62 kvm_release_pfn_clean(pte->pfn);
65 void kvmppc_mmu_pte_flush(struct kvm_vcpu *vcpu, ulong guest_ea, ulong ea_mask)
69 dprintk_mmu("KVM: Flushing %d Shadow PTEs: 0x%llx & 0x%llx\n",
70 vcpu->arch.hpte_cache_offset, guest_ea, ea_mask);
71 BUG_ON(vcpu->arch.hpte_cache_offset > HPTEG_CACHE_NUM);
74 for (i = 0; i < vcpu->arch.hpte_cache_offset; i++) {
75 struct hpte_cache *pte;
77 pte = &vcpu->arch.hpte_cache[i];
81 if ((pte->pte.eaddr & ea_mask) == guest_ea) {
86 /* Doing a complete flush -> start from scratch */
88 vcpu->arch.hpte_cache_offset = 0;
91 void kvmppc_mmu_pte_vflush(struct kvm_vcpu *vcpu, u64 guest_vp, u64 vp_mask)
95 dprintk_mmu("KVM: Flushing %d Shadow vPTEs: 0x%llx & 0x%llx\n",
96 vcpu->arch.hpte_cache_offset, guest_vp, vp_mask);
97 BUG_ON(vcpu->arch.hpte_cache_offset > HPTEG_CACHE_NUM);
100 for (i = 0; i < vcpu->arch.hpte_cache_offset; i++) {
101 struct hpte_cache *pte;
103 pte = &vcpu->arch.hpte_cache[i];
107 if ((pte->pte.vpage & vp_mask) == guest_vp) {
113 void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end)
117 dprintk_mmu("KVM: Flushing %d Shadow pPTEs: 0x%llx & 0x%llx\n",
118 vcpu->arch.hpte_cache_offset, guest_pa, pa_mask);
119 BUG_ON(vcpu->arch.hpte_cache_offset > HPTEG_CACHE_NUM);
121 for (i = 0; i < vcpu->arch.hpte_cache_offset; i++) {
122 struct hpte_cache *pte;
124 pte = &vcpu->arch.hpte_cache[i];
128 if ((pte->pte.raddr >= pa_start) &&
129 (pte->pte.raddr < pa_end)) {
135 struct kvmppc_pte *kvmppc_mmu_find_pte(struct kvm_vcpu *vcpu, u64 ea, bool data)
140 guest_vp = vcpu->arch.mmu.ea_to_vp(vcpu, ea, false);
141 for (i=0; i<vcpu->arch.hpte_cache_offset; i++) {
142 struct hpte_cache *pte;
144 pte = &vcpu->arch.hpte_cache[i];
148 if (pte->pte.vpage == guest_vp)
155 static int kvmppc_mmu_hpte_cache_next(struct kvm_vcpu *vcpu)
157 if (vcpu->arch.hpte_cache_offset == HPTEG_CACHE_NUM)
158 kvmppc_mmu_pte_flush(vcpu, 0, 0);
160 return vcpu->arch.hpte_cache_offset++;
163 /* We keep 512 gvsid->hvsid entries, mapping the guest ones to the array using
164 * a hash, so we don't waste cycles on looping */
165 static u16 kvmppc_sid_hash(struct kvm_vcpu *vcpu, u64 gvsid)
167 return (u16)(((gvsid >> (SID_MAP_BITS * 7)) & SID_MAP_MASK) ^
168 ((gvsid >> (SID_MAP_BITS * 6)) & SID_MAP_MASK) ^
169 ((gvsid >> (SID_MAP_BITS * 5)) & SID_MAP_MASK) ^
170 ((gvsid >> (SID_MAP_BITS * 4)) & SID_MAP_MASK) ^
171 ((gvsid >> (SID_MAP_BITS * 3)) & SID_MAP_MASK) ^
172 ((gvsid >> (SID_MAP_BITS * 2)) & SID_MAP_MASK) ^
173 ((gvsid >> (SID_MAP_BITS * 1)) & SID_MAP_MASK) ^
174 ((gvsid >> (SID_MAP_BITS * 0)) & SID_MAP_MASK));
178 static struct kvmppc_sid_map *find_sid_vsid(struct kvm_vcpu *vcpu, u64 gvsid)
180 struct kvmppc_sid_map *map;
183 if (vcpu->arch.msr & MSR_PR)
186 sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
187 map = &to_book3s(vcpu)->sid_map[sid_map_mask];
188 if (map->guest_vsid == gvsid) {
189 dprintk_slb("SLB: Searching 0x%llx -> 0x%llx\n",
190 gvsid, map->host_vsid);
194 map = &to_book3s(vcpu)->sid_map[SID_MAP_MASK - sid_map_mask];
195 if (map->guest_vsid == gvsid) {
196 dprintk_slb("SLB: Searching 0x%llx -> 0x%llx\n",
197 gvsid, map->host_vsid);
201 dprintk_slb("SLB: Searching 0x%llx -> not found\n", gvsid);
205 int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte)
208 ulong hash, hpteg, va;
214 struct kvmppc_sid_map *map;
216 /* Get host physical address for gpa */
217 hpaddr = gfn_to_pfn(vcpu->kvm, orig_pte->raddr >> PAGE_SHIFT);
218 if (kvm_is_error_hva(hpaddr)) {
219 printk(KERN_INFO "Couldn't get guest page for gfn %lx!\n", orig_pte->eaddr);
222 hpaddr <<= PAGE_SHIFT;
224 #elif PAGE_SHIFT == 16
225 hpaddr |= orig_pte->raddr & 0xf000;
227 #error Unknown page size
230 /* and write the mapping ea -> hpa into the pt */
231 vcpu->arch.mmu.esid_to_vsid(vcpu, orig_pte->eaddr >> SID_SHIFT, &vsid);
232 map = find_sid_vsid(vcpu, vsid);
234 kvmppc_mmu_map_segment(vcpu, orig_pte->eaddr);
235 map = find_sid_vsid(vcpu, vsid);
239 vsid = map->host_vsid;
240 va = hpt_va(orig_pte->eaddr, vsid, MMU_SEGSIZE_256M);
242 if (!orig_pte->may_write)
245 mark_page_dirty(vcpu->kvm, orig_pte->raddr >> PAGE_SHIFT);
247 if (!orig_pte->may_execute)
250 hash = hpt_hash(va, PTE_SIZE, MMU_SEGSIZE_256M);
253 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
255 /* In case we tried normal mapping already, let's nuke old entries */
257 if (ppc_md.hpte_remove(hpteg) < 0)
260 ret = ppc_md.hpte_insert(hpteg, va, hpaddr, rflags, vflags, MMU_PAGE_4K, MMU_SEGSIZE_256M);
263 /* If we couldn't map a primary PTE, try a secondary */
265 vflags ^= HPTE_V_SECONDARY;
269 int hpte_id = kvmppc_mmu_hpte_cache_next(vcpu);
270 struct hpte_cache *pte = &vcpu->arch.hpte_cache[hpte_id];
272 dprintk_mmu("KVM: %c%c Map 0x%llx: [%lx] 0x%lx (0x%llx) -> %lx\n",
273 ((rflags & HPTE_R_PP) == 3) ? '-' : 'w',
274 (rflags & HPTE_R_N) ? '-' : 'x',
275 orig_pte->eaddr, hpteg, va, orig_pte->vpage, hpaddr);
277 /* The ppc_md code may give us a secondary entry even though we
278 asked for a primary. Fix up. */
279 if ((ret & _PTEIDX_SECONDARY) && !(vflags & HPTE_V_SECONDARY)) {
281 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
284 pte->slot = hpteg + (ret & 7);
286 pte->pte = *orig_pte;
287 pte->pfn = hpaddr >> PAGE_SHIFT;
293 static struct kvmppc_sid_map *create_sid_map(struct kvm_vcpu *vcpu, u64 gvsid)
295 struct kvmppc_sid_map *map;
296 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
298 static int backwards_map = 0;
300 if (vcpu->arch.msr & MSR_PR)
303 /* We might get collisions that trap in preceding order, so let's
304 map them differently */
306 sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
308 sid_map_mask = SID_MAP_MASK - sid_map_mask;
310 map = &to_book3s(vcpu)->sid_map[sid_map_mask];
312 /* Make sure we're taking the other map next time */
313 backwards_map = !backwards_map;
315 /* Uh-oh ... out of mappings. Let's flush! */
316 if (vcpu_book3s->vsid_next == vcpu_book3s->vsid_max) {
317 vcpu_book3s->vsid_next = vcpu_book3s->vsid_first;
318 memset(vcpu_book3s->sid_map, 0,
319 sizeof(struct kvmppc_sid_map) * SID_MAP_NUM);
320 kvmppc_mmu_pte_flush(vcpu, 0, 0);
321 kvmppc_mmu_flush_segments(vcpu);
323 map->host_vsid = vcpu_book3s->vsid_next++;
325 map->guest_vsid = gvsid;
331 static int kvmppc_mmu_next_segment(struct kvm_vcpu *vcpu, ulong esid)
334 int max_slb_size = 64;
335 int found_inval = -1;
338 if (!to_svcpu(vcpu)->slb_max)
339 to_svcpu(vcpu)->slb_max = 1;
341 /* Are we overwriting? */
342 for (i = 1; i < to_svcpu(vcpu)->slb_max; i++) {
343 if (!(to_svcpu(vcpu)->slb[i].esid & SLB_ESID_V))
345 else if ((to_svcpu(vcpu)->slb[i].esid & ESID_MASK) == esid)
349 /* Found a spare entry that was invalidated before */
353 /* No spare invalid entry, so create one */
355 if (mmu_slb_size < 64)
356 max_slb_size = mmu_slb_size;
358 /* Overflowing -> purge */
359 if ((to_svcpu(vcpu)->slb_max) == max_slb_size)
360 kvmppc_mmu_flush_segments(vcpu);
362 r = to_svcpu(vcpu)->slb_max;
363 to_svcpu(vcpu)->slb_max++;
368 int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr)
370 u64 esid = eaddr >> SID_SHIFT;
371 u64 slb_esid = (eaddr & ESID_MASK) | SLB_ESID_V;
372 u64 slb_vsid = SLB_VSID_USER;
375 struct kvmppc_sid_map *map;
377 slb_index = kvmppc_mmu_next_segment(vcpu, eaddr & ESID_MASK);
379 if (vcpu->arch.mmu.esid_to_vsid(vcpu, esid, &gvsid)) {
380 /* Invalidate an entry */
381 to_svcpu(vcpu)->slb[slb_index].esid = 0;
385 map = find_sid_vsid(vcpu, gvsid);
387 map = create_sid_map(vcpu, gvsid);
389 map->guest_esid = esid;
391 slb_vsid |= (map->host_vsid << 12);
392 slb_vsid &= ~SLB_VSID_KP;
393 slb_esid |= slb_index;
395 to_svcpu(vcpu)->slb[slb_index].esid = slb_esid;
396 to_svcpu(vcpu)->slb[slb_index].vsid = slb_vsid;
398 dprintk_slb("slbmte %#llx, %#llx\n", slb_vsid, slb_esid);
403 void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu)
405 to_svcpu(vcpu)->slb_max = 1;
406 to_svcpu(vcpu)->slb[0].esid = 0;
409 void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
411 kvmppc_mmu_pte_flush(vcpu, 0, 0);
412 __destroy_context(to_book3s(vcpu)->context_id);
415 int kvmppc_mmu_init(struct kvm_vcpu *vcpu)
417 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
420 err = __init_new_context();
423 vcpu3s->context_id = err;
425 vcpu3s->vsid_max = ((vcpu3s->context_id + 1) << USER_ESID_BITS) - 1;
426 vcpu3s->vsid_first = vcpu3s->context_id << USER_ESID_BITS;
427 vcpu3s->vsid_next = vcpu3s->vsid_first;