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>
23 #include <linux/hash.h>
25 #include <asm/kvm_ppc.h>
26 #include <asm/kvm_book3s.h>
27 #include <asm/mmu-hash64.h>
28 #include <asm/machdep.h>
29 #include <asm/mmu_context.h>
30 #include <asm/hw_irq.h>
35 /* #define DEBUG_MMU */
36 /* #define DEBUG_SLB */
39 #define dprintk_mmu(a, ...) printk(KERN_INFO a, __VA_ARGS__)
41 #define dprintk_mmu(a, ...) do { } while(0)
45 #define dprintk_slb(a, ...) printk(KERN_INFO a, __VA_ARGS__)
47 #define dprintk_slb(a, ...) do { } while(0)
50 static void invalidate_pte(struct hpte_cache *pte)
52 dprintk_mmu("KVM: Flushing SPT: 0x%lx (0x%llx) -> 0x%llx\n",
53 pte->pte.eaddr, pte->pte.vpage, pte->host_va);
55 ppc_md.hpte_invalidate(pte->slot, pte->host_va,
56 MMU_PAGE_4K, MMU_SEGSIZE_256M,
60 if (pte->pte.may_write)
61 kvm_release_pfn_dirty(pte->pfn);
63 kvm_release_pfn_clean(pte->pfn);
66 void kvmppc_mmu_pte_flush(struct kvm_vcpu *vcpu, ulong guest_ea, ulong ea_mask)
70 dprintk_mmu("KVM: Flushing %d Shadow PTEs: 0x%lx & 0x%lx\n",
71 vcpu->arch.hpte_cache_offset, guest_ea, ea_mask);
72 BUG_ON(vcpu->arch.hpte_cache_offset > HPTEG_CACHE_NUM);
75 for (i = 0; i < vcpu->arch.hpte_cache_offset; i++) {
76 struct hpte_cache *pte;
78 pte = &vcpu->arch.hpte_cache[i];
82 if ((pte->pte.eaddr & ea_mask) == guest_ea) {
87 /* Doing a complete flush -> start from scratch */
89 vcpu->arch.hpte_cache_offset = 0;
92 void kvmppc_mmu_pte_vflush(struct kvm_vcpu *vcpu, u64 guest_vp, u64 vp_mask)
96 dprintk_mmu("KVM: Flushing %d Shadow vPTEs: 0x%llx & 0x%llx\n",
97 vcpu->arch.hpte_cache_offset, guest_vp, vp_mask);
98 BUG_ON(vcpu->arch.hpte_cache_offset > HPTEG_CACHE_NUM);
101 for (i = 0; i < vcpu->arch.hpte_cache_offset; i++) {
102 struct hpte_cache *pte;
104 pte = &vcpu->arch.hpte_cache[i];
108 if ((pte->pte.vpage & vp_mask) == guest_vp) {
114 void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end)
118 dprintk_mmu("KVM: Flushing %d Shadow pPTEs: 0x%lx & 0x%lx\n",
119 vcpu->arch.hpte_cache_offset, pa_start, pa_end);
120 BUG_ON(vcpu->arch.hpte_cache_offset > HPTEG_CACHE_NUM);
122 for (i = 0; i < vcpu->arch.hpte_cache_offset; i++) {
123 struct hpte_cache *pte;
125 pte = &vcpu->arch.hpte_cache[i];
129 if ((pte->pte.raddr >= pa_start) &&
130 (pte->pte.raddr < pa_end)) {
136 static int kvmppc_mmu_hpte_cache_next(struct kvm_vcpu *vcpu)
138 if (vcpu->arch.hpte_cache_offset == HPTEG_CACHE_NUM)
139 kvmppc_mmu_pte_flush(vcpu, 0, 0);
141 return vcpu->arch.hpte_cache_offset++;
144 /* We keep 512 gvsid->hvsid entries, mapping the guest ones to the array using
145 * a hash, so we don't waste cycles on looping */
146 static u16 kvmppc_sid_hash(struct kvm_vcpu *vcpu, u64 gvsid)
148 return hash_64(gvsid, SID_MAP_BITS);
151 static struct kvmppc_sid_map *find_sid_vsid(struct kvm_vcpu *vcpu, u64 gvsid)
153 struct kvmppc_sid_map *map;
156 if (vcpu->arch.msr & MSR_PR)
159 sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
160 map = &to_book3s(vcpu)->sid_map[sid_map_mask];
161 if (map->guest_vsid == gvsid) {
162 dprintk_slb("SLB: Searching: 0x%llx -> 0x%llx\n",
163 gvsid, map->host_vsid);
167 map = &to_book3s(vcpu)->sid_map[SID_MAP_MASK - sid_map_mask];
168 if (map->guest_vsid == gvsid) {
169 dprintk_slb("SLB: Searching 0x%llx -> 0x%llx\n",
170 gvsid, map->host_vsid);
174 dprintk_slb("SLB: Searching %d/%d: 0x%llx -> not found\n",
175 sid_map_mask, SID_MAP_MASK - sid_map_mask, gvsid);
179 int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte)
182 ulong hash, hpteg, va;
188 struct kvmppc_sid_map *map;
190 /* Get host physical address for gpa */
191 hpaddr = gfn_to_pfn(vcpu->kvm, orig_pte->raddr >> PAGE_SHIFT);
192 if (kvm_is_error_hva(hpaddr)) {
193 printk(KERN_INFO "Couldn't get guest page for gfn %lx!\n", orig_pte->eaddr);
196 hpaddr <<= PAGE_SHIFT;
198 #elif PAGE_SHIFT == 16
199 hpaddr |= orig_pte->raddr & 0xf000;
201 #error Unknown page size
204 /* and write the mapping ea -> hpa into the pt */
205 vcpu->arch.mmu.esid_to_vsid(vcpu, orig_pte->eaddr >> SID_SHIFT, &vsid);
206 map = find_sid_vsid(vcpu, vsid);
208 ret = kvmppc_mmu_map_segment(vcpu, orig_pte->eaddr);
210 map = find_sid_vsid(vcpu, vsid);
213 printk(KERN_ERR "KVM: Segment map for 0x%llx (0x%lx) failed\n",
214 vsid, orig_pte->eaddr);
219 vsid = map->host_vsid;
220 va = hpt_va(orig_pte->eaddr, vsid, MMU_SEGSIZE_256M);
222 if (!orig_pte->may_write)
225 mark_page_dirty(vcpu->kvm, orig_pte->raddr >> PAGE_SHIFT);
227 if (!orig_pte->may_execute)
230 hash = hpt_hash(va, PTE_SIZE, MMU_SEGSIZE_256M);
233 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
235 /* In case we tried normal mapping already, let's nuke old entries */
237 if (ppc_md.hpte_remove(hpteg) < 0)
240 ret = ppc_md.hpte_insert(hpteg, va, hpaddr, rflags, vflags, MMU_PAGE_4K, MMU_SEGSIZE_256M);
243 /* If we couldn't map a primary PTE, try a secondary */
245 vflags ^= HPTE_V_SECONDARY;
249 int hpte_id = kvmppc_mmu_hpte_cache_next(vcpu);
250 struct hpte_cache *pte = &vcpu->arch.hpte_cache[hpte_id];
252 dprintk_mmu("KVM: %c%c Map 0x%lx: [%lx] 0x%lx (0x%llx) -> %lx\n",
253 ((rflags & HPTE_R_PP) == 3) ? '-' : 'w',
254 (rflags & HPTE_R_N) ? '-' : 'x',
255 orig_pte->eaddr, hpteg, va, orig_pte->vpage, hpaddr);
257 /* The ppc_md code may give us a secondary entry even though we
258 asked for a primary. Fix up. */
259 if ((ret & _PTEIDX_SECONDARY) && !(vflags & HPTE_V_SECONDARY)) {
261 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
264 pte->slot = hpteg + (ret & 7);
266 pte->pte = *orig_pte;
267 pte->pfn = hpaddr >> PAGE_SHIFT;
273 static struct kvmppc_sid_map *create_sid_map(struct kvm_vcpu *vcpu, u64 gvsid)
275 struct kvmppc_sid_map *map;
276 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
278 static int backwards_map = 0;
280 if (vcpu->arch.msr & MSR_PR)
283 /* We might get collisions that trap in preceding order, so let's
284 map them differently */
286 sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
288 sid_map_mask = SID_MAP_MASK - sid_map_mask;
290 map = &to_book3s(vcpu)->sid_map[sid_map_mask];
292 /* Make sure we're taking the other map next time */
293 backwards_map = !backwards_map;
295 /* Uh-oh ... out of mappings. Let's flush! */
296 if (vcpu_book3s->vsid_next == vcpu_book3s->vsid_max) {
297 vcpu_book3s->vsid_next = vcpu_book3s->vsid_first;
298 memset(vcpu_book3s->sid_map, 0,
299 sizeof(struct kvmppc_sid_map) * SID_MAP_NUM);
300 kvmppc_mmu_pte_flush(vcpu, 0, 0);
301 kvmppc_mmu_flush_segments(vcpu);
303 map->host_vsid = vcpu_book3s->vsid_next++;
305 map->guest_vsid = gvsid;
308 dprintk_slb("SLB: New mapping at %d: 0x%llx -> 0x%llx\n",
309 sid_map_mask, gvsid, map->host_vsid);
314 static int kvmppc_mmu_next_segment(struct kvm_vcpu *vcpu, ulong esid)
317 int max_slb_size = 64;
318 int found_inval = -1;
321 if (!to_svcpu(vcpu)->slb_max)
322 to_svcpu(vcpu)->slb_max = 1;
324 /* Are we overwriting? */
325 for (i = 1; i < to_svcpu(vcpu)->slb_max; i++) {
326 if (!(to_svcpu(vcpu)->slb[i].esid & SLB_ESID_V))
328 else if ((to_svcpu(vcpu)->slb[i].esid & ESID_MASK) == esid)
332 /* Found a spare entry that was invalidated before */
336 /* No spare invalid entry, so create one */
338 if (mmu_slb_size < 64)
339 max_slb_size = mmu_slb_size;
341 /* Overflowing -> purge */
342 if ((to_svcpu(vcpu)->slb_max) == max_slb_size)
343 kvmppc_mmu_flush_segments(vcpu);
345 r = to_svcpu(vcpu)->slb_max;
346 to_svcpu(vcpu)->slb_max++;
351 int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr)
353 u64 esid = eaddr >> SID_SHIFT;
354 u64 slb_esid = (eaddr & ESID_MASK) | SLB_ESID_V;
355 u64 slb_vsid = SLB_VSID_USER;
358 struct kvmppc_sid_map *map;
360 slb_index = kvmppc_mmu_next_segment(vcpu, eaddr & ESID_MASK);
362 if (vcpu->arch.mmu.esid_to_vsid(vcpu, esid, &gvsid)) {
363 /* Invalidate an entry */
364 to_svcpu(vcpu)->slb[slb_index].esid = 0;
368 map = find_sid_vsid(vcpu, gvsid);
370 map = create_sid_map(vcpu, gvsid);
372 map->guest_esid = esid;
374 slb_vsid |= (map->host_vsid << 12);
375 slb_vsid &= ~SLB_VSID_KP;
376 slb_esid |= slb_index;
378 to_svcpu(vcpu)->slb[slb_index].esid = slb_esid;
379 to_svcpu(vcpu)->slb[slb_index].vsid = slb_vsid;
381 dprintk_slb("slbmte %#llx, %#llx\n", slb_vsid, slb_esid);
386 void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu)
388 to_svcpu(vcpu)->slb_max = 1;
389 to_svcpu(vcpu)->slb[0].esid = 0;
392 void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
394 kvmppc_mmu_pte_flush(vcpu, 0, 0);
395 __destroy_context(to_book3s(vcpu)->context_id);
398 int kvmppc_mmu_init(struct kvm_vcpu *vcpu)
400 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
403 err = __init_new_context();
406 vcpu3s->context_id = err;
408 vcpu3s->vsid_max = ((vcpu3s->context_id + 1) << USER_ESID_BITS) - 1;
409 vcpu3s->vsid_first = vcpu3s->context_id << USER_ESID_BITS;
410 vcpu3s->vsid_next = vcpu3s->vsid_first;