GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / arch / powerpc / kvm / book3s_64_mmu_host.c
blob384179a5002b9f2bdad7c188651689550edfde5b
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>
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>
32 #define PTE_SIZE 12
33 #define VSID_ALL 0
35 /* #define DEBUG_MMU */
36 /* #define DEBUG_SLB */
38 #ifdef DEBUG_MMU
39 #define dprintk_mmu(a, ...) printk(KERN_INFO a, __VA_ARGS__)
40 #else
41 #define dprintk_mmu(a, ...) do { } while(0)
42 #endif
44 #ifdef DEBUG_SLB
45 #define dprintk_slb(a, ...) printk(KERN_INFO a, __VA_ARGS__)
46 #else
47 #define dprintk_slb(a, ...) do { } while(0)
48 #endif
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,
54 false);
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;
67 u16 sid_map_mask;
69 if (vcpu->arch.msr & MSR_PR)
70 gvsid |= VSID_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);
77 return map;
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);
84 return map;
87 dprintk_slb("SLB: Searching %d/%d: 0x%llx -> not found\n",
88 sid_map_mask, SID_MAP_MASK - sid_map_mask, gvsid);
89 return NULL;
92 int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte)
94 pfn_t hpaddr;
95 ulong hash, hpteg, va;
96 u64 vsid;
97 int ret;
98 int rflags = 0x192;
99 int vflags = 0;
100 int attempt = 0;
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);
107 return -EINVAL;
109 hpaddr <<= PAGE_SHIFT;
110 #if PAGE_SHIFT == 12
111 #elif PAGE_SHIFT == 16
112 hpaddr |= orig_pte->raddr & 0xf000;
113 #else
114 #error Unknown page size
115 #endif
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);
120 if (!map) {
121 ret = kvmppc_mmu_map_segment(vcpu, orig_pte->eaddr);
122 WARN_ON(ret < 0);
123 map = find_sid_vsid(vcpu, vsid);
125 if (!map) {
126 printk(KERN_ERR "KVM: Segment map for 0x%llx (0x%lx) failed\n",
127 vsid, orig_pte->eaddr);
128 WARN_ON(true);
129 return -EINVAL;
132 vsid = map->host_vsid;
133 va = hpt_va(orig_pte->eaddr, vsid, MMU_SEGSIZE_256M);
135 if (!orig_pte->may_write)
136 rflags |= HPTE_R_PP;
137 else
138 mark_page_dirty(vcpu->kvm, orig_pte->raddr >> PAGE_SHIFT);
140 if (!orig_pte->may_execute)
141 rflags |= HPTE_R_N;
143 hash = hpt_hash(va, PTE_SIZE, MMU_SEGSIZE_256M);
145 map_again:
146 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
148 /* In case we tried normal mapping already, let's nuke old entries */
149 if (attempt > 1)
150 if (ppc_md.hpte_remove(hpteg) < 0)
151 return -1;
153 ret = ppc_md.hpte_insert(hpteg, va, hpaddr, rflags, vflags, MMU_PAGE_4K, MMU_SEGSIZE_256M);
155 if (ret < 0) {
156 /* If we couldn't map a primary PTE, try a secondary */
157 hash = ~hash;
158 vflags ^= HPTE_V_SECONDARY;
159 attempt++;
160 goto map_again;
161 } else {
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)) {
172 hash = ~hash;
173 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
176 pte->slot = hpteg + (ret & 7);
177 pte->host_va = va;
178 pte->pte = *orig_pte;
179 pte->pfn = hpaddr >> PAGE_SHIFT;
181 kvmppc_mmu_hpte_cache_map(vcpu, pte);
184 return 0;
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);
191 u16 sid_map_mask;
192 static int backwards_map = 0;
194 if (vcpu->arch.msr & MSR_PR)
195 gvsid |= VSID_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);
201 if (backwards_map)
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;
220 map->valid = true;
222 dprintk_slb("SLB: New mapping at %d: 0x%llx -> 0x%llx\n",
223 sid_map_mask, gvsid, map->host_vsid);
225 return map;
228 static int kvmppc_mmu_next_segment(struct kvm_vcpu *vcpu, ulong esid)
230 int i;
231 int max_slb_size = 64;
232 int found_inval = -1;
233 int r;
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))
241 found_inval = i;
242 else if ((to_svcpu(vcpu)->slb[i].esid & ESID_MASK) == esid)
243 return i;
246 /* Found a spare entry that was invalidated before */
247 if (found_inval > 0)
248 return found_inval;
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++;
262 return r;
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;
270 u64 gvsid;
271 int slb_index;
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;
279 return -ENOENT;
282 map = find_sid_vsid(vcpu, gvsid);
283 if (!map)
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);
297 return 0;
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);
315 int err;
317 err = __init_new_context();
318 if (err < 0)
319 return -1;
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);
328 return 0;