Adding support for MOXA ART SoC. Testing port of linux-2.6.32.60-moxart.
[linux-3.6.7-moxart.git] / arch / powerpc / kvm / book3s_64_mmu_host.c
blob0688b6b3958594fce84a62cfbd2f3564b5650781
1 /*
2 * Copyright (C) 2009 SUSE Linux Products GmbH. All rights reserved.
4 * Authors:
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>
30 #include "trace.h"
32 #define PTE_SIZE 12
34 void kvmppc_mmu_invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
36 ppc_md.hpte_invalidate(pte->slot, pte->host_va,
37 MMU_PAGE_4K, MMU_SEGSIZE_256M,
38 false);
41 /* We keep 512 gvsid->hvsid entries, mapping the guest ones to the array using
42 * a hash, so we don't waste cycles on looping */
43 static u16 kvmppc_sid_hash(struct kvm_vcpu *vcpu, u64 gvsid)
45 return (u16)(((gvsid >> (SID_MAP_BITS * 7)) & SID_MAP_MASK) ^
46 ((gvsid >> (SID_MAP_BITS * 6)) & SID_MAP_MASK) ^
47 ((gvsid >> (SID_MAP_BITS * 5)) & SID_MAP_MASK) ^
48 ((gvsid >> (SID_MAP_BITS * 4)) & SID_MAP_MASK) ^
49 ((gvsid >> (SID_MAP_BITS * 3)) & SID_MAP_MASK) ^
50 ((gvsid >> (SID_MAP_BITS * 2)) & SID_MAP_MASK) ^
51 ((gvsid >> (SID_MAP_BITS * 1)) & SID_MAP_MASK) ^
52 ((gvsid >> (SID_MAP_BITS * 0)) & SID_MAP_MASK));
56 static struct kvmppc_sid_map *find_sid_vsid(struct kvm_vcpu *vcpu, u64 gvsid)
58 struct kvmppc_sid_map *map;
59 u16 sid_map_mask;
61 if (vcpu->arch.shared->msr & MSR_PR)
62 gvsid |= VSID_PR;
64 sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
65 map = &to_book3s(vcpu)->sid_map[sid_map_mask];
66 if (map->valid && (map->guest_vsid == gvsid)) {
67 trace_kvm_book3s_slb_found(gvsid, map->host_vsid);
68 return map;
71 map = &to_book3s(vcpu)->sid_map[SID_MAP_MASK - sid_map_mask];
72 if (map->valid && (map->guest_vsid == gvsid)) {
73 trace_kvm_book3s_slb_found(gvsid, map->host_vsid);
74 return map;
77 trace_kvm_book3s_slb_fail(sid_map_mask, gvsid);
78 return NULL;
81 int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte)
83 pfn_t hpaddr;
84 ulong hash, hpteg, va;
85 u64 vsid;
86 int ret;
87 int rflags = 0x192;
88 int vflags = 0;
89 int attempt = 0;
90 struct kvmppc_sid_map *map;
91 int r = 0;
93 /* Get host physical address for gpa */
94 hpaddr = kvmppc_gfn_to_pfn(vcpu, orig_pte->raddr >> PAGE_SHIFT);
95 if (is_error_pfn(hpaddr)) {
96 printk(KERN_INFO "Couldn't get guest page for gfn %lx!\n", orig_pte->eaddr);
97 r = -EINVAL;
98 goto out;
100 hpaddr <<= PAGE_SHIFT;
101 hpaddr |= orig_pte->raddr & (~0xfffULL & ~PAGE_MASK);
103 /* and write the mapping ea -> hpa into the pt */
104 vcpu->arch.mmu.esid_to_vsid(vcpu, orig_pte->eaddr >> SID_SHIFT, &vsid);
105 map = find_sid_vsid(vcpu, vsid);
106 if (!map) {
107 ret = kvmppc_mmu_map_segment(vcpu, orig_pte->eaddr);
108 WARN_ON(ret < 0);
109 map = find_sid_vsid(vcpu, vsid);
111 if (!map) {
112 printk(KERN_ERR "KVM: Segment map for 0x%llx (0x%lx) failed\n",
113 vsid, orig_pte->eaddr);
114 WARN_ON(true);
115 r = -EINVAL;
116 goto out;
119 vsid = map->host_vsid;
120 va = hpt_va(orig_pte->eaddr, vsid, MMU_SEGSIZE_256M);
122 if (!orig_pte->may_write)
123 rflags |= HPTE_R_PP;
124 else
125 mark_page_dirty(vcpu->kvm, orig_pte->raddr >> PAGE_SHIFT);
127 if (!orig_pte->may_execute)
128 rflags |= HPTE_R_N;
129 else
130 kvmppc_mmu_flush_icache(hpaddr >> PAGE_SHIFT);
132 hash = hpt_hash(va, PTE_SIZE, MMU_SEGSIZE_256M);
134 map_again:
135 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
137 /* In case we tried normal mapping already, let's nuke old entries */
138 if (attempt > 1)
139 if (ppc_md.hpte_remove(hpteg) < 0) {
140 r = -1;
141 goto out;
144 ret = ppc_md.hpte_insert(hpteg, va, hpaddr, rflags, vflags, MMU_PAGE_4K, MMU_SEGSIZE_256M);
146 if (ret < 0) {
147 /* If we couldn't map a primary PTE, try a secondary */
148 hash = ~hash;
149 vflags ^= HPTE_V_SECONDARY;
150 attempt++;
151 goto map_again;
152 } else {
153 struct hpte_cache *pte = kvmppc_mmu_hpte_cache_next(vcpu);
155 trace_kvm_book3s_64_mmu_map(rflags, hpteg, va, hpaddr, orig_pte);
157 /* The ppc_md code may give us a secondary entry even though we
158 asked for a primary. Fix up. */
159 if ((ret & _PTEIDX_SECONDARY) && !(vflags & HPTE_V_SECONDARY)) {
160 hash = ~hash;
161 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
164 pte->slot = hpteg + (ret & 7);
165 pte->host_va = va;
166 pte->pte = *orig_pte;
167 pte->pfn = hpaddr >> PAGE_SHIFT;
169 kvmppc_mmu_hpte_cache_map(vcpu, pte);
172 out:
173 return r;
176 static struct kvmppc_sid_map *create_sid_map(struct kvm_vcpu *vcpu, u64 gvsid)
178 struct kvmppc_sid_map *map;
179 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
180 u16 sid_map_mask;
181 static int backwards_map = 0;
183 if (vcpu->arch.shared->msr & MSR_PR)
184 gvsid |= VSID_PR;
186 /* We might get collisions that trap in preceding order, so let's
187 map them differently */
189 sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
190 if (backwards_map)
191 sid_map_mask = SID_MAP_MASK - sid_map_mask;
193 map = &to_book3s(vcpu)->sid_map[sid_map_mask];
195 /* Make sure we're taking the other map next time */
196 backwards_map = !backwards_map;
198 /* Uh-oh ... out of mappings. Let's flush! */
199 if (vcpu_book3s->proto_vsid_next == vcpu_book3s->proto_vsid_max) {
200 vcpu_book3s->proto_vsid_next = vcpu_book3s->proto_vsid_first;
201 memset(vcpu_book3s->sid_map, 0,
202 sizeof(struct kvmppc_sid_map) * SID_MAP_NUM);
203 kvmppc_mmu_pte_flush(vcpu, 0, 0);
204 kvmppc_mmu_flush_segments(vcpu);
206 map->host_vsid = vsid_scramble(vcpu_book3s->proto_vsid_next++, 256M);
208 map->guest_vsid = gvsid;
209 map->valid = true;
211 trace_kvm_book3s_slb_map(sid_map_mask, gvsid, map->host_vsid);
213 return map;
216 static int kvmppc_mmu_next_segment(struct kvm_vcpu *vcpu, ulong esid)
218 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
219 int i;
220 int max_slb_size = 64;
221 int found_inval = -1;
222 int r;
224 if (!svcpu->slb_max)
225 svcpu->slb_max = 1;
227 /* Are we overwriting? */
228 for (i = 1; i < svcpu->slb_max; i++) {
229 if (!(svcpu->slb[i].esid & SLB_ESID_V))
230 found_inval = i;
231 else if ((svcpu->slb[i].esid & ESID_MASK) == esid) {
232 r = i;
233 goto out;
237 /* Found a spare entry that was invalidated before */
238 if (found_inval > 0) {
239 r = found_inval;
240 goto out;
243 /* No spare invalid entry, so create one */
245 if (mmu_slb_size < 64)
246 max_slb_size = mmu_slb_size;
248 /* Overflowing -> purge */
249 if ((svcpu->slb_max) == max_slb_size)
250 kvmppc_mmu_flush_segments(vcpu);
252 r = svcpu->slb_max;
253 svcpu->slb_max++;
255 out:
256 svcpu_put(svcpu);
257 return r;
260 int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr)
262 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
263 u64 esid = eaddr >> SID_SHIFT;
264 u64 slb_esid = (eaddr & ESID_MASK) | SLB_ESID_V;
265 u64 slb_vsid = SLB_VSID_USER;
266 u64 gvsid;
267 int slb_index;
268 struct kvmppc_sid_map *map;
269 int r = 0;
271 slb_index = kvmppc_mmu_next_segment(vcpu, eaddr & ESID_MASK);
273 if (vcpu->arch.mmu.esid_to_vsid(vcpu, esid, &gvsid)) {
274 /* Invalidate an entry */
275 svcpu->slb[slb_index].esid = 0;
276 r = -ENOENT;
277 goto out;
280 map = find_sid_vsid(vcpu, gvsid);
281 if (!map)
282 map = create_sid_map(vcpu, gvsid);
284 map->guest_esid = esid;
286 slb_vsid |= (map->host_vsid << 12);
287 slb_vsid &= ~SLB_VSID_KP;
288 slb_esid |= slb_index;
290 svcpu->slb[slb_index].esid = slb_esid;
291 svcpu->slb[slb_index].vsid = slb_vsid;
293 trace_kvm_book3s_slbmte(slb_vsid, slb_esid);
295 out:
296 svcpu_put(svcpu);
297 return r;
300 void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu)
302 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
303 svcpu->slb_max = 1;
304 svcpu->slb[0].esid = 0;
305 svcpu_put(svcpu);
308 void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
310 kvmppc_mmu_hpte_destroy(vcpu);
311 __destroy_context(to_book3s(vcpu)->context_id[0]);
314 int kvmppc_mmu_init(struct kvm_vcpu *vcpu)
316 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
317 int err;
319 err = __init_new_context();
320 if (err < 0)
321 return -1;
322 vcpu3s->context_id[0] = err;
324 vcpu3s->proto_vsid_max = ((vcpu3s->context_id[0] + 1)
325 << USER_ESID_BITS) - 1;
326 vcpu3s->proto_vsid_first = vcpu3s->context_id[0] << USER_ESID_BITS;
327 vcpu3s->proto_vsid_next = vcpu3s->proto_vsid_first;
329 kvmppc_mmu_hpte_init(vcpu);
331 return 0;