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 void kvmppc_mmu_invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
52 ppc_md.hpte_invalidate(pte->slot, pte->host_va,
53 MMU_PAGE_4K, MMU_SEGSIZE_256M,
57 /* We keep 512 gvsid->hvsid entries, mapping the guest ones to the array using
58 * a hash, so we don't waste cycles on looping */
59 static u16 kvmppc_sid_hash(struct kvm_vcpu *vcpu, u64 gvsid)
61 return hash_64(gvsid, SID_MAP_BITS);
64 static struct kvmppc_sid_map *find_sid_vsid(struct kvm_vcpu *vcpu, u64 gvsid)
66 struct kvmppc_sid_map *map;
69 if (vcpu->arch.msr & MSR_PR)
72 sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
73 map = &to_book3s(vcpu)->sid_map[sid_map_mask];
74 if (map->guest_vsid == gvsid) {
75 dprintk_slb("SLB: Searching: 0x%llx -> 0x%llx\n",
76 gvsid, map->host_vsid);
80 map = &to_book3s(vcpu)->sid_map[SID_MAP_MASK - sid_map_mask];
81 if (map->guest_vsid == gvsid) {
82 dprintk_slb("SLB: Searching 0x%llx -> 0x%llx\n",
83 gvsid, map->host_vsid);
87 dprintk_slb("SLB: Searching %d/%d: 0x%llx -> not found\n",
88 sid_map_mask, SID_MAP_MASK - sid_map_mask, gvsid);
92 int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte)
95 ulong hash, hpteg, va;
101 struct kvmppc_sid_map *map;
103 /* Get host physical address for gpa */
104 hpaddr = gfn_to_pfn(vcpu->kvm, orig_pte->raddr >> PAGE_SHIFT);
105 if (kvm_is_error_hva(hpaddr)) {
106 printk(KERN_INFO "Couldn't get guest page for gfn %lx!\n", orig_pte->eaddr);
109 hpaddr <<= PAGE_SHIFT;
111 #elif PAGE_SHIFT == 16
112 hpaddr |= orig_pte->raddr & 0xf000;
114 #error Unknown page size
117 /* and write the mapping ea -> hpa into the pt */
118 vcpu->arch.mmu.esid_to_vsid(vcpu, orig_pte->eaddr >> SID_SHIFT, &vsid);
119 map = find_sid_vsid(vcpu, vsid);
121 ret = kvmppc_mmu_map_segment(vcpu, orig_pte->eaddr);
123 map = find_sid_vsid(vcpu, vsid);
126 printk(KERN_ERR "KVM: Segment map for 0x%llx (0x%lx) failed\n",
127 vsid, orig_pte->eaddr);
132 vsid = map->host_vsid;
133 va = hpt_va(orig_pte->eaddr, vsid, MMU_SEGSIZE_256M);
135 if (!orig_pte->may_write)
138 mark_page_dirty(vcpu->kvm, orig_pte->raddr >> PAGE_SHIFT);
140 if (!orig_pte->may_execute)
143 hash = hpt_hash(va, PTE_SIZE, MMU_SEGSIZE_256M);
146 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
148 /* In case we tried normal mapping already, let's nuke old entries */
150 if (ppc_md.hpte_remove(hpteg) < 0)
153 ret = ppc_md.hpte_insert(hpteg, va, hpaddr, rflags, vflags, MMU_PAGE_4K, MMU_SEGSIZE_256M);
156 /* If we couldn't map a primary PTE, try a secondary */
158 vflags ^= HPTE_V_SECONDARY;
162 struct hpte_cache *pte = kvmppc_mmu_hpte_cache_next(vcpu);
164 dprintk_mmu("KVM: %c%c Map 0x%lx: [%lx] 0x%lx (0x%llx) -> %lx\n",
165 ((rflags & HPTE_R_PP) == 3) ? '-' : 'w',
166 (rflags & HPTE_R_N) ? '-' : 'x',
167 orig_pte->eaddr, hpteg, va, orig_pte->vpage, hpaddr);
169 /* The ppc_md code may give us a secondary entry even though we
170 asked for a primary. Fix up. */
171 if ((ret & _PTEIDX_SECONDARY) && !(vflags & HPTE_V_SECONDARY)) {
173 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
176 pte->slot = hpteg + (ret & 7);
178 pte->pte = *orig_pte;
179 pte->pfn = hpaddr >> PAGE_SHIFT;
181 kvmppc_mmu_hpte_cache_map(vcpu, pte);
187 static struct kvmppc_sid_map *create_sid_map(struct kvm_vcpu *vcpu, u64 gvsid)
189 struct kvmppc_sid_map *map;
190 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
192 static int backwards_map = 0;
194 if (vcpu->arch.msr & MSR_PR)
197 /* We might get collisions that trap in preceding order, so let's
198 map them differently */
200 sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
202 sid_map_mask = SID_MAP_MASK - sid_map_mask;
204 map = &to_book3s(vcpu)->sid_map[sid_map_mask];
206 /* Make sure we're taking the other map next time */
207 backwards_map = !backwards_map;
209 /* Uh-oh ... out of mappings. Let's flush! */
210 if (vcpu_book3s->vsid_next == vcpu_book3s->vsid_max) {
211 vcpu_book3s->vsid_next = vcpu_book3s->vsid_first;
212 memset(vcpu_book3s->sid_map, 0,
213 sizeof(struct kvmppc_sid_map) * SID_MAP_NUM);
214 kvmppc_mmu_pte_flush(vcpu, 0, 0);
215 kvmppc_mmu_flush_segments(vcpu);
217 map->host_vsid = vcpu_book3s->vsid_next++;
219 map->guest_vsid = gvsid;
222 dprintk_slb("SLB: New mapping at %d: 0x%llx -> 0x%llx\n",
223 sid_map_mask, gvsid, map->host_vsid);
228 static int kvmppc_mmu_next_segment(struct kvm_vcpu *vcpu, ulong esid)
231 int max_slb_size = 64;
232 int found_inval = -1;
235 if (!to_svcpu(vcpu)->slb_max)
236 to_svcpu(vcpu)->slb_max = 1;
238 /* Are we overwriting? */
239 for (i = 1; i < to_svcpu(vcpu)->slb_max; i++) {
240 if (!(to_svcpu(vcpu)->slb[i].esid & SLB_ESID_V))
242 else if ((to_svcpu(vcpu)->slb[i].esid & ESID_MASK) == esid)
246 /* Found a spare entry that was invalidated before */
250 /* No spare invalid entry, so create one */
252 if (mmu_slb_size < 64)
253 max_slb_size = mmu_slb_size;
255 /* Overflowing -> purge */
256 if ((to_svcpu(vcpu)->slb_max) == max_slb_size)
257 kvmppc_mmu_flush_segments(vcpu);
259 r = to_svcpu(vcpu)->slb_max;
260 to_svcpu(vcpu)->slb_max++;
265 int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr)
267 u64 esid = eaddr >> SID_SHIFT;
268 u64 slb_esid = (eaddr & ESID_MASK) | SLB_ESID_V;
269 u64 slb_vsid = SLB_VSID_USER;
272 struct kvmppc_sid_map *map;
274 slb_index = kvmppc_mmu_next_segment(vcpu, eaddr & ESID_MASK);
276 if (vcpu->arch.mmu.esid_to_vsid(vcpu, esid, &gvsid)) {
277 /* Invalidate an entry */
278 to_svcpu(vcpu)->slb[slb_index].esid = 0;
282 map = find_sid_vsid(vcpu, gvsid);
284 map = create_sid_map(vcpu, gvsid);
286 map->guest_esid = esid;
288 slb_vsid |= (map->host_vsid << 12);
289 slb_vsid &= ~SLB_VSID_KP;
290 slb_esid |= slb_index;
292 to_svcpu(vcpu)->slb[slb_index].esid = slb_esid;
293 to_svcpu(vcpu)->slb[slb_index].vsid = slb_vsid;
295 dprintk_slb("slbmte %#llx, %#llx\n", slb_vsid, slb_esid);
300 void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu)
302 to_svcpu(vcpu)->slb_max = 1;
303 to_svcpu(vcpu)->slb[0].esid = 0;
306 void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
308 kvmppc_mmu_hpte_destroy(vcpu);
309 __destroy_context(to_book3s(vcpu)->context_id);
312 int kvmppc_mmu_init(struct kvm_vcpu *vcpu)
314 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
317 err = __init_new_context();
320 vcpu3s->context_id = err;
322 vcpu3s->vsid_max = ((vcpu3s->context_id + 1) << USER_ESID_BITS) - 1;
323 vcpu3s->vsid_first = vcpu3s->context_id << USER_ESID_BITS;
324 vcpu3s->vsid_next = vcpu3s->vsid_first;
326 kvmppc_mmu_hpte_init(vcpu);