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.shared->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 = kvmppc_gfn_to_pfn(vcpu, orig_pte->raddr >> PAGE_SHIFT);
105 if (is_error_pfn(hpaddr)) {
106 printk(KERN_INFO "Couldn't get guest page for gfn %lx!\n", orig_pte->eaddr);
109 hpaddr <<= PAGE_SHIFT;
110 hpaddr |= orig_pte->raddr & (~0xfffULL & ~PAGE_MASK);
112 /* and write the mapping ea -> hpa into the pt */
113 vcpu->arch.mmu.esid_to_vsid(vcpu, orig_pte->eaddr >> SID_SHIFT, &vsid);
114 map = find_sid_vsid(vcpu, vsid);
116 ret = kvmppc_mmu_map_segment(vcpu, orig_pte->eaddr);
118 map = find_sid_vsid(vcpu, vsid);
121 printk(KERN_ERR "KVM: Segment map for 0x%llx (0x%lx) failed\n",
122 vsid, orig_pte->eaddr);
127 vsid = map->host_vsid;
128 va = hpt_va(orig_pte->eaddr, vsid, MMU_SEGSIZE_256M);
130 if (!orig_pte->may_write)
133 mark_page_dirty(vcpu->kvm, orig_pte->raddr >> PAGE_SHIFT);
135 if (!orig_pte->may_execute)
138 hash = hpt_hash(va, PTE_SIZE, MMU_SEGSIZE_256M);
141 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
143 /* In case we tried normal mapping already, let's nuke old entries */
145 if (ppc_md.hpte_remove(hpteg) < 0)
148 ret = ppc_md.hpte_insert(hpteg, va, hpaddr, rflags, vflags, MMU_PAGE_4K, MMU_SEGSIZE_256M);
151 /* If we couldn't map a primary PTE, try a secondary */
153 vflags ^= HPTE_V_SECONDARY;
157 struct hpte_cache *pte = kvmppc_mmu_hpte_cache_next(vcpu);
159 dprintk_mmu("KVM: %c%c Map 0x%lx: [%lx] 0x%lx (0x%llx) -> %lx\n",
160 ((rflags & HPTE_R_PP) == 3) ? '-' : 'w',
161 (rflags & HPTE_R_N) ? '-' : 'x',
162 orig_pte->eaddr, hpteg, va, orig_pte->vpage, hpaddr);
164 /* The ppc_md code may give us a secondary entry even though we
165 asked for a primary. Fix up. */
166 if ((ret & _PTEIDX_SECONDARY) && !(vflags & HPTE_V_SECONDARY)) {
168 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
171 pte->slot = hpteg + (ret & 7);
173 pte->pte = *orig_pte;
174 pte->pfn = hpaddr >> PAGE_SHIFT;
176 kvmppc_mmu_hpte_cache_map(vcpu, pte);
182 static struct kvmppc_sid_map *create_sid_map(struct kvm_vcpu *vcpu, u64 gvsid)
184 struct kvmppc_sid_map *map;
185 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
187 static int backwards_map = 0;
189 if (vcpu->arch.shared->msr & MSR_PR)
192 /* We might get collisions that trap in preceding order, so let's
193 map them differently */
195 sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
197 sid_map_mask = SID_MAP_MASK - sid_map_mask;
199 map = &to_book3s(vcpu)->sid_map[sid_map_mask];
201 /* Make sure we're taking the other map next time */
202 backwards_map = !backwards_map;
204 /* Uh-oh ... out of mappings. Let's flush! */
205 if (vcpu_book3s->vsid_next == vcpu_book3s->vsid_max) {
206 vcpu_book3s->vsid_next = vcpu_book3s->vsid_first;
207 memset(vcpu_book3s->sid_map, 0,
208 sizeof(struct kvmppc_sid_map) * SID_MAP_NUM);
209 kvmppc_mmu_pte_flush(vcpu, 0, 0);
210 kvmppc_mmu_flush_segments(vcpu);
212 map->host_vsid = vcpu_book3s->vsid_next++;
214 map->guest_vsid = gvsid;
217 dprintk_slb("SLB: New mapping at %d: 0x%llx -> 0x%llx\n",
218 sid_map_mask, gvsid, map->host_vsid);
223 static int kvmppc_mmu_next_segment(struct kvm_vcpu *vcpu, ulong esid)
226 int max_slb_size = 64;
227 int found_inval = -1;
230 if (!to_svcpu(vcpu)->slb_max)
231 to_svcpu(vcpu)->slb_max = 1;
233 /* Are we overwriting? */
234 for (i = 1; i < to_svcpu(vcpu)->slb_max; i++) {
235 if (!(to_svcpu(vcpu)->slb[i].esid & SLB_ESID_V))
237 else if ((to_svcpu(vcpu)->slb[i].esid & ESID_MASK) == esid)
241 /* Found a spare entry that was invalidated before */
245 /* No spare invalid entry, so create one */
247 if (mmu_slb_size < 64)
248 max_slb_size = mmu_slb_size;
250 /* Overflowing -> purge */
251 if ((to_svcpu(vcpu)->slb_max) == max_slb_size)
252 kvmppc_mmu_flush_segments(vcpu);
254 r = to_svcpu(vcpu)->slb_max;
255 to_svcpu(vcpu)->slb_max++;
260 int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr)
262 u64 esid = eaddr >> SID_SHIFT;
263 u64 slb_esid = (eaddr & ESID_MASK) | SLB_ESID_V;
264 u64 slb_vsid = SLB_VSID_USER;
267 struct kvmppc_sid_map *map;
269 slb_index = kvmppc_mmu_next_segment(vcpu, eaddr & ESID_MASK);
271 if (vcpu->arch.mmu.esid_to_vsid(vcpu, esid, &gvsid)) {
272 /* Invalidate an entry */
273 to_svcpu(vcpu)->slb[slb_index].esid = 0;
277 map = find_sid_vsid(vcpu, gvsid);
279 map = create_sid_map(vcpu, gvsid);
281 map->guest_esid = esid;
283 slb_vsid |= (map->host_vsid << 12);
284 slb_vsid &= ~SLB_VSID_KP;
285 slb_esid |= slb_index;
287 to_svcpu(vcpu)->slb[slb_index].esid = slb_esid;
288 to_svcpu(vcpu)->slb[slb_index].vsid = slb_vsid;
290 dprintk_slb("slbmte %#llx, %#llx\n", slb_vsid, slb_esid);
295 void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu)
297 to_svcpu(vcpu)->slb_max = 1;
298 to_svcpu(vcpu)->slb[0].esid = 0;
301 void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
303 kvmppc_mmu_hpte_destroy(vcpu);
304 __destroy_context(to_book3s(vcpu)->context_id);
307 int kvmppc_mmu_init(struct kvm_vcpu *vcpu)
309 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
312 err = __init_new_context();
315 vcpu3s->context_id = err;
317 vcpu3s->vsid_max = ((vcpu3s->context_id + 1) << USER_ESID_BITS) - 1;
318 vcpu3s->vsid_first = vcpu3s->context_id << USER_ESID_BITS;
319 vcpu3s->vsid_next = vcpu3s->vsid_first;
321 kvmppc_mmu_hpte_init(vcpu);