blob: 321c931f691cf77dc89d5a848bb43b1c3603cad5 [file] [log] [blame]
Alexander Graf0d8dc682009-10-30 05:47:11 +00001/*
2 * Copyright (C) 2009 SUSE Linux Products GmbH. All rights reserved.
3 *
4 * Authors:
5 * Alexander Graf <agraf@suse.de>
6 * Kevin Wolf <mail@kevin-wolf.de>
7 *
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.
11 *
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.
16 *
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.
20 */
21
22#include <linux/kvm_host.h>
Alexander Graf3b249152010-06-21 15:25:19 +020023#include <linux/hash.h>
Alexander Graf0d8dc682009-10-30 05:47:11 +000024
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>
Alexander Graf82fdee72010-08-02 11:38:54 +020031#include "trace.h"
Alexander Graf0d8dc682009-10-30 05:47:11 +000032
33#define PTE_SIZE 12
34#define VSID_ALL 0
35
Alexander Graffef093be2010-06-30 15:18:46 +020036void kvmppc_mmu_invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
Alexander Graf0d8dc682009-10-30 05:47:11 +000037{
Alexander Graf0d8dc682009-10-30 05:47:11 +000038 ppc_md.hpte_invalidate(pte->slot, pte->host_va,
39 MMU_PAGE_4K, MMU_SEGSIZE_256M,
40 false);
Alexander Graf0d8dc682009-10-30 05:47:11 +000041}
42
43/* We keep 512 gvsid->hvsid entries, mapping the guest ones to the array using
44 * a hash, so we don't waste cycles on looping */
45static u16 kvmppc_sid_hash(struct kvm_vcpu *vcpu, u64 gvsid)
46{
Alexander Graf3b249152010-06-21 15:25:19 +020047 return hash_64(gvsid, SID_MAP_BITS);
Alexander Graf0d8dc682009-10-30 05:47:11 +000048}
49
Alexander Graf0d8dc682009-10-30 05:47:11 +000050static struct kvmppc_sid_map *find_sid_vsid(struct kvm_vcpu *vcpu, u64 gvsid)
51{
52 struct kvmppc_sid_map *map;
53 u16 sid_map_mask;
54
Alexander Graf666e7252010-07-29 14:47:43 +020055 if (vcpu->arch.shared->msr & MSR_PR)
Alexander Graf0d8dc682009-10-30 05:47:11 +000056 gvsid |= VSID_PR;
57
58 sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
59 map = &to_book3s(vcpu)->sid_map[sid_map_mask];
Alexander Grafc22c3192010-08-02 13:38:18 +020060 if (map->valid && (map->guest_vsid == gvsid)) {
Alexander Graf928d78b2010-08-02 21:25:33 +020061 trace_kvm_book3s_slb_found(gvsid, map->host_vsid);
Alexander Graf0d8dc682009-10-30 05:47:11 +000062 return map;
63 }
64
65 map = &to_book3s(vcpu)->sid_map[SID_MAP_MASK - sid_map_mask];
Alexander Grafc22c3192010-08-02 13:38:18 +020066 if (map->valid && (map->guest_vsid == gvsid)) {
Alexander Graf928d78b2010-08-02 21:25:33 +020067 trace_kvm_book3s_slb_found(gvsid, map->host_vsid);
Alexander Graf0d8dc682009-10-30 05:47:11 +000068 return map;
69 }
70
Alexander Graf928d78b2010-08-02 21:25:33 +020071 trace_kvm_book3s_slb_fail(sid_map_mask, gvsid);
Alexander Graf0d8dc682009-10-30 05:47:11 +000072 return NULL;
73}
74
75int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte)
76{
77 pfn_t hpaddr;
78 ulong hash, hpteg, va;
79 u64 vsid;
80 int ret;
81 int rflags = 0x192;
82 int vflags = 0;
83 int attempt = 0;
84 struct kvmppc_sid_map *map;
85
86 /* Get host physical address for gpa */
Alexander Grafe8508942010-07-29 14:47:54 +020087 hpaddr = kvmppc_gfn_to_pfn(vcpu, orig_pte->raddr >> PAGE_SHIFT);
Gleb Natapov49451382010-07-29 15:04:18 +020088 if (is_error_pfn(hpaddr)) {
Alexander Grafaf7b4d12010-04-20 02:49:46 +020089 printk(KERN_INFO "Couldn't get guest page for gfn %lx!\n", orig_pte->eaddr);
Alexander Graf0d8dc682009-10-30 05:47:11 +000090 return -EINVAL;
91 }
92 hpaddr <<= PAGE_SHIFT;
Alexander Grafe8508942010-07-29 14:47:54 +020093 hpaddr |= orig_pte->raddr & (~0xfffULL & ~PAGE_MASK);
Alexander Graf0d8dc682009-10-30 05:47:11 +000094
95 /* and write the mapping ea -> hpa into the pt */
96 vcpu->arch.mmu.esid_to_vsid(vcpu, orig_pte->eaddr >> SID_SHIFT, &vsid);
97 map = find_sid_vsid(vcpu, vsid);
98 if (!map) {
Alexander Grafac214672010-04-20 02:49:50 +020099 ret = kvmppc_mmu_map_segment(vcpu, orig_pte->eaddr);
100 WARN_ON(ret < 0);
Alexander Graf0d8dc682009-10-30 05:47:11 +0000101 map = find_sid_vsid(vcpu, vsid);
102 }
Alexander Grafac214672010-04-20 02:49:50 +0200103 if (!map) {
104 printk(KERN_ERR "KVM: Segment map for 0x%llx (0x%lx) failed\n",
105 vsid, orig_pte->eaddr);
106 WARN_ON(true);
107 return -EINVAL;
108 }
Alexander Graf0d8dc682009-10-30 05:47:11 +0000109
110 vsid = map->host_vsid;
111 va = hpt_va(orig_pte->eaddr, vsid, MMU_SEGSIZE_256M);
112
113 if (!orig_pte->may_write)
114 rflags |= HPTE_R_PP;
115 else
116 mark_page_dirty(vcpu->kvm, orig_pte->raddr >> PAGE_SHIFT);
117
118 if (!orig_pte->may_execute)
119 rflags |= HPTE_R_N;
120
121 hash = hpt_hash(va, PTE_SIZE, MMU_SEGSIZE_256M);
122
123map_again:
124 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
125
126 /* In case we tried normal mapping already, let's nuke old entries */
127 if (attempt > 1)
128 if (ppc_md.hpte_remove(hpteg) < 0)
129 return -1;
130
131 ret = ppc_md.hpte_insert(hpteg, va, hpaddr, rflags, vflags, MMU_PAGE_4K, MMU_SEGSIZE_256M);
132
133 if (ret < 0) {
134 /* If we couldn't map a primary PTE, try a secondary */
Alexander Graf0d8dc682009-10-30 05:47:11 +0000135 hash = ~hash;
Alexander Graf20a340a2010-02-19 11:00:46 +0100136 vflags ^= HPTE_V_SECONDARY;
Alexander Graf0d8dc682009-10-30 05:47:11 +0000137 attempt++;
Alexander Graf0d8dc682009-10-30 05:47:11 +0000138 goto map_again;
139 } else {
Alexander Graffef093be2010-06-30 15:18:46 +0200140 struct hpte_cache *pte = kvmppc_mmu_hpte_cache_next(vcpu);
Alexander Graf0d8dc682009-10-30 05:47:11 +0000141
Alexander Graf82fdee72010-08-02 11:38:54 +0200142 trace_kvm_book3s_64_mmu_map(rflags, hpteg, va, hpaddr, orig_pte);
Alexander Graf0d8dc682009-10-30 05:47:11 +0000143
Alexander Grafa1eda282010-03-24 21:48:34 +0100144 /* The ppc_md code may give us a secondary entry even though we
145 asked for a primary. Fix up. */
146 if ((ret & _PTEIDX_SECONDARY) && !(vflags & HPTE_V_SECONDARY)) {
147 hash = ~hash;
148 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
149 }
150
Alexander Graf0d8dc682009-10-30 05:47:11 +0000151 pte->slot = hpteg + (ret & 7);
152 pte->host_va = va;
153 pte->pte = *orig_pte;
154 pte->pfn = hpaddr >> PAGE_SHIFT;
Alexander Graffef093be2010-06-30 15:18:46 +0200155
156 kvmppc_mmu_hpte_cache_map(vcpu, pte);
Alexander Graf0d8dc682009-10-30 05:47:11 +0000157 }
158
159 return 0;
160}
161
162static struct kvmppc_sid_map *create_sid_map(struct kvm_vcpu *vcpu, u64 gvsid)
163{
164 struct kvmppc_sid_map *map;
165 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
166 u16 sid_map_mask;
167 static int backwards_map = 0;
168
Alexander Graf666e7252010-07-29 14:47:43 +0200169 if (vcpu->arch.shared->msr & MSR_PR)
Alexander Graf0d8dc682009-10-30 05:47:11 +0000170 gvsid |= VSID_PR;
171
172 /* We might get collisions that trap in preceding order, so let's
173 map them differently */
174
175 sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
176 if (backwards_map)
177 sid_map_mask = SID_MAP_MASK - sid_map_mask;
178
179 map = &to_book3s(vcpu)->sid_map[sid_map_mask];
180
181 /* Make sure we're taking the other map next time */
182 backwards_map = !backwards_map;
183
184 /* Uh-oh ... out of mappings. Let's flush! */
185 if (vcpu_book3s->vsid_next == vcpu_book3s->vsid_max) {
186 vcpu_book3s->vsid_next = vcpu_book3s->vsid_first;
187 memset(vcpu_book3s->sid_map, 0,
188 sizeof(struct kvmppc_sid_map) * SID_MAP_NUM);
189 kvmppc_mmu_pte_flush(vcpu, 0, 0);
190 kvmppc_mmu_flush_segments(vcpu);
191 }
192 map->host_vsid = vcpu_book3s->vsid_next++;
193
194 map->guest_vsid = gvsid;
195 map->valid = true;
196
Alexander Graf928d78b2010-08-02 21:25:33 +0200197 trace_kvm_book3s_slb_map(sid_map_mask, gvsid, map->host_vsid);
Alexander Graf5156f272010-04-20 02:49:52 +0200198
Alexander Graf0d8dc682009-10-30 05:47:11 +0000199 return map;
200}
201
202static int kvmppc_mmu_next_segment(struct kvm_vcpu *vcpu, ulong esid)
203{
204 int i;
205 int max_slb_size = 64;
206 int found_inval = -1;
207 int r;
208
Alexander Grafc7f38f42010-04-16 00:11:40 +0200209 if (!to_svcpu(vcpu)->slb_max)
210 to_svcpu(vcpu)->slb_max = 1;
Alexander Graf0d8dc682009-10-30 05:47:11 +0000211
212 /* Are we overwriting? */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200213 for (i = 1; i < to_svcpu(vcpu)->slb_max; i++) {
214 if (!(to_svcpu(vcpu)->slb[i].esid & SLB_ESID_V))
Alexander Graf0d8dc682009-10-30 05:47:11 +0000215 found_inval = i;
Alexander Grafc7f38f42010-04-16 00:11:40 +0200216 else if ((to_svcpu(vcpu)->slb[i].esid & ESID_MASK) == esid)
Alexander Graf0d8dc682009-10-30 05:47:11 +0000217 return i;
218 }
219
220 /* Found a spare entry that was invalidated before */
221 if (found_inval > 0)
222 return found_inval;
223
224 /* No spare invalid entry, so create one */
225
226 if (mmu_slb_size < 64)
227 max_slb_size = mmu_slb_size;
228
229 /* Overflowing -> purge */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200230 if ((to_svcpu(vcpu)->slb_max) == max_slb_size)
Alexander Graf0d8dc682009-10-30 05:47:11 +0000231 kvmppc_mmu_flush_segments(vcpu);
232
Alexander Grafc7f38f42010-04-16 00:11:40 +0200233 r = to_svcpu(vcpu)->slb_max;
234 to_svcpu(vcpu)->slb_max++;
Alexander Graf0d8dc682009-10-30 05:47:11 +0000235
236 return r;
237}
238
239int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr)
240{
241 u64 esid = eaddr >> SID_SHIFT;
242 u64 slb_esid = (eaddr & ESID_MASK) | SLB_ESID_V;
243 u64 slb_vsid = SLB_VSID_USER;
244 u64 gvsid;
245 int slb_index;
246 struct kvmppc_sid_map *map;
247
248 slb_index = kvmppc_mmu_next_segment(vcpu, eaddr & ESID_MASK);
249
250 if (vcpu->arch.mmu.esid_to_vsid(vcpu, esid, &gvsid)) {
251 /* Invalidate an entry */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200252 to_svcpu(vcpu)->slb[slb_index].esid = 0;
Alexander Graf0d8dc682009-10-30 05:47:11 +0000253 return -ENOENT;
254 }
255
256 map = find_sid_vsid(vcpu, gvsid);
257 if (!map)
258 map = create_sid_map(vcpu, gvsid);
259
260 map->guest_esid = esid;
261
262 slb_vsid |= (map->host_vsid << 12);
263 slb_vsid &= ~SLB_VSID_KP;
264 slb_esid |= slb_index;
265
Alexander Grafc7f38f42010-04-16 00:11:40 +0200266 to_svcpu(vcpu)->slb[slb_index].esid = slb_esid;
267 to_svcpu(vcpu)->slb[slb_index].vsid = slb_vsid;
Alexander Graf0d8dc682009-10-30 05:47:11 +0000268
Alexander Graf928d78b2010-08-02 21:25:33 +0200269 trace_kvm_book3s_slbmte(slb_vsid, slb_esid);
Alexander Graf0d8dc682009-10-30 05:47:11 +0000270
271 return 0;
272}
273
274void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu)
275{
Alexander Grafc7f38f42010-04-16 00:11:40 +0200276 to_svcpu(vcpu)->slb_max = 1;
277 to_svcpu(vcpu)->slb[0].esid = 0;
Alexander Graf0d8dc682009-10-30 05:47:11 +0000278}
279
280void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
281{
Alexander Graffef093be2010-06-30 15:18:46 +0200282 kvmppc_mmu_hpte_destroy(vcpu);
Alexander Graf9cc5e952010-04-16 00:11:45 +0200283 __destroy_context(to_book3s(vcpu)->context_id);
284}
285
286int kvmppc_mmu_init(struct kvm_vcpu *vcpu)
287{
288 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
289 int err;
290
291 err = __init_new_context();
292 if (err < 0)
293 return -1;
294 vcpu3s->context_id = err;
295
296 vcpu3s->vsid_max = ((vcpu3s->context_id + 1) << USER_ESID_BITS) - 1;
297 vcpu3s->vsid_first = vcpu3s->context_id << USER_ESID_BITS;
298 vcpu3s->vsid_next = vcpu3s->vsid_first;
299
Alexander Graffef093be2010-06-30 15:18:46 +0200300 kvmppc_mmu_hpte_init(vcpu);
301
Alexander Graf9cc5e952010-04-16 00:11:45 +0200302 return 0;
Alexander Graf0d8dc682009-10-30 05:47:11 +0000303}