treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / arch / powerpc / kvm / book3s_64_mmu.c
blob599133256a9541f33b3dd210699c02c21bcaa682
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
4 * Copyright SUSE Linux Products GmbH 2009
6 * Authors: Alexander Graf <agraf@suse.de>
7 */
9 #include <linux/types.h>
10 #include <linux/string.h>
11 #include <linux/kvm.h>
12 #include <linux/kvm_host.h>
13 #include <linux/highmem.h>
15 #include <asm/kvm_ppc.h>
16 #include <asm/kvm_book3s.h>
17 #include <asm/book3s/64/mmu-hash.h>
19 /* #define DEBUG_MMU */
21 #ifdef DEBUG_MMU
22 #define dprintk(X...) printk(KERN_INFO X)
23 #else
24 #define dprintk(X...) do { } while(0)
25 #endif
27 static struct kvmppc_slb *kvmppc_mmu_book3s_64_find_slbe(
28 struct kvm_vcpu *vcpu,
29 gva_t eaddr)
31 int i;
32 u64 esid = GET_ESID(eaddr);
33 u64 esid_1t = GET_ESID_1T(eaddr);
35 for (i = 0; i < vcpu->arch.slb_nr; i++) {
36 u64 cmp_esid = esid;
38 if (!vcpu->arch.slb[i].valid)
39 continue;
41 if (vcpu->arch.slb[i].tb)
42 cmp_esid = esid_1t;
44 if (vcpu->arch.slb[i].esid == cmp_esid)
45 return &vcpu->arch.slb[i];
48 dprintk("KVM: No SLB entry found for 0x%lx [%llx | %llx]\n",
49 eaddr, esid, esid_1t);
50 for (i = 0; i < vcpu->arch.slb_nr; i++) {
51 if (vcpu->arch.slb[i].vsid)
52 dprintk(" %d: %c%c%c %llx %llx\n", i,
53 vcpu->arch.slb[i].valid ? 'v' : ' ',
54 vcpu->arch.slb[i].large ? 'l' : ' ',
55 vcpu->arch.slb[i].tb ? 't' : ' ',
56 vcpu->arch.slb[i].esid,
57 vcpu->arch.slb[i].vsid);
60 return NULL;
63 static int kvmppc_slb_sid_shift(struct kvmppc_slb *slbe)
65 return slbe->tb ? SID_SHIFT_1T : SID_SHIFT;
68 static u64 kvmppc_slb_offset_mask(struct kvmppc_slb *slbe)
70 return (1ul << kvmppc_slb_sid_shift(slbe)) - 1;
73 static u64 kvmppc_slb_calc_vpn(struct kvmppc_slb *slb, gva_t eaddr)
75 eaddr &= kvmppc_slb_offset_mask(slb);
77 return (eaddr >> VPN_SHIFT) |
78 ((slb->vsid) << (kvmppc_slb_sid_shift(slb) - VPN_SHIFT));
81 static u64 kvmppc_mmu_book3s_64_ea_to_vp(struct kvm_vcpu *vcpu, gva_t eaddr,
82 bool data)
84 struct kvmppc_slb *slb;
86 slb = kvmppc_mmu_book3s_64_find_slbe(vcpu, eaddr);
87 if (!slb)
88 return 0;
90 return kvmppc_slb_calc_vpn(slb, eaddr);
93 static int mmu_pagesize(int mmu_pg)
95 switch (mmu_pg) {
96 case MMU_PAGE_64K:
97 return 16;
98 case MMU_PAGE_16M:
99 return 24;
101 return 12;
104 static int kvmppc_mmu_book3s_64_get_pagesize(struct kvmppc_slb *slbe)
106 return mmu_pagesize(slbe->base_page_size);
109 static u32 kvmppc_mmu_book3s_64_get_page(struct kvmppc_slb *slbe, gva_t eaddr)
111 int p = kvmppc_mmu_book3s_64_get_pagesize(slbe);
113 return ((eaddr & kvmppc_slb_offset_mask(slbe)) >> p);
116 static hva_t kvmppc_mmu_book3s_64_get_pteg(struct kvm_vcpu *vcpu,
117 struct kvmppc_slb *slbe, gva_t eaddr,
118 bool second)
120 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
121 u64 hash, pteg, htabsize;
122 u32 ssize;
123 hva_t r;
124 u64 vpn;
126 htabsize = ((1 << ((vcpu_book3s->sdr1 & 0x1f) + 11)) - 1);
128 vpn = kvmppc_slb_calc_vpn(slbe, eaddr);
129 ssize = slbe->tb ? MMU_SEGSIZE_1T : MMU_SEGSIZE_256M;
130 hash = hpt_hash(vpn, kvmppc_mmu_book3s_64_get_pagesize(slbe), ssize);
131 if (second)
132 hash = ~hash;
133 hash &= ((1ULL << 39ULL) - 1ULL);
134 hash &= htabsize;
135 hash <<= 7ULL;
137 pteg = vcpu_book3s->sdr1 & 0xfffffffffffc0000ULL;
138 pteg |= hash;
140 dprintk("MMU: page=0x%x sdr1=0x%llx pteg=0x%llx vsid=0x%llx\n",
141 page, vcpu_book3s->sdr1, pteg, slbe->vsid);
143 /* When running a PAPR guest, SDR1 contains a HVA address instead
144 of a GPA */
145 if (vcpu->arch.papr_enabled)
146 r = pteg;
147 else
148 r = gfn_to_hva(vcpu->kvm, pteg >> PAGE_SHIFT);
150 if (kvm_is_error_hva(r))
151 return r;
152 return r | (pteg & ~PAGE_MASK);
155 static u64 kvmppc_mmu_book3s_64_get_avpn(struct kvmppc_slb *slbe, gva_t eaddr)
157 int p = kvmppc_mmu_book3s_64_get_pagesize(slbe);
158 u64 avpn;
160 avpn = kvmppc_mmu_book3s_64_get_page(slbe, eaddr);
161 avpn |= slbe->vsid << (kvmppc_slb_sid_shift(slbe) - p);
163 if (p < 16)
164 avpn >>= ((80 - p) - 56) - 8; /* 16 - p */
165 else
166 avpn <<= p - 16;
168 return avpn;
172 * Return page size encoded in the second word of a HPTE, or
173 * -1 for an invalid encoding for the base page size indicated by
174 * the SLB entry. This doesn't handle mixed pagesize segments yet.
176 static int decode_pagesize(struct kvmppc_slb *slbe, u64 r)
178 switch (slbe->base_page_size) {
179 case MMU_PAGE_64K:
180 if ((r & 0xf000) == 0x1000)
181 return MMU_PAGE_64K;
182 break;
183 case MMU_PAGE_16M:
184 if ((r & 0xff000) == 0)
185 return MMU_PAGE_16M;
186 break;
188 return -1;
191 static int kvmppc_mmu_book3s_64_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
192 struct kvmppc_pte *gpte, bool data,
193 bool iswrite)
195 struct kvmppc_slb *slbe;
196 hva_t ptegp;
197 u64 pteg[16];
198 u64 avpn = 0;
199 u64 v, r;
200 u64 v_val, v_mask;
201 u64 eaddr_mask;
202 int i;
203 u8 pp, key = 0;
204 bool found = false;
205 bool second = false;
206 int pgsize;
207 ulong mp_ea = vcpu->arch.magic_page_ea;
209 /* Magic page override */
210 if (unlikely(mp_ea) &&
211 unlikely((eaddr & ~0xfffULL) == (mp_ea & ~0xfffULL)) &&
212 !(kvmppc_get_msr(vcpu) & MSR_PR)) {
213 gpte->eaddr = eaddr;
214 gpte->vpage = kvmppc_mmu_book3s_64_ea_to_vp(vcpu, eaddr, data);
215 gpte->raddr = vcpu->arch.magic_page_pa | (gpte->raddr & 0xfff);
216 gpte->raddr &= KVM_PAM;
217 gpte->may_execute = true;
218 gpte->may_read = true;
219 gpte->may_write = true;
220 gpte->page_size = MMU_PAGE_4K;
221 gpte->wimg = HPTE_R_M;
223 return 0;
226 slbe = kvmppc_mmu_book3s_64_find_slbe(vcpu, eaddr);
227 if (!slbe)
228 goto no_seg_found;
230 avpn = kvmppc_mmu_book3s_64_get_avpn(slbe, eaddr);
231 v_val = avpn & HPTE_V_AVPN;
233 if (slbe->tb)
234 v_val |= SLB_VSID_B_1T;
235 if (slbe->large)
236 v_val |= HPTE_V_LARGE;
237 v_val |= HPTE_V_VALID;
239 v_mask = SLB_VSID_B | HPTE_V_AVPN | HPTE_V_LARGE | HPTE_V_VALID |
240 HPTE_V_SECONDARY;
242 pgsize = slbe->large ? MMU_PAGE_16M : MMU_PAGE_4K;
244 mutex_lock(&vcpu->kvm->arch.hpt_mutex);
246 do_second:
247 ptegp = kvmppc_mmu_book3s_64_get_pteg(vcpu, slbe, eaddr, second);
248 if (kvm_is_error_hva(ptegp))
249 goto no_page_found;
251 if(copy_from_user(pteg, (void __user *)ptegp, sizeof(pteg))) {
252 printk_ratelimited(KERN_ERR
253 "KVM: Can't copy data from 0x%lx!\n", ptegp);
254 goto no_page_found;
257 if ((kvmppc_get_msr(vcpu) & MSR_PR) && slbe->Kp)
258 key = 4;
259 else if (!(kvmppc_get_msr(vcpu) & MSR_PR) && slbe->Ks)
260 key = 4;
262 for (i=0; i<16; i+=2) {
263 u64 pte0 = be64_to_cpu(pteg[i]);
264 u64 pte1 = be64_to_cpu(pteg[i + 1]);
266 /* Check all relevant fields of 1st dword */
267 if ((pte0 & v_mask) == v_val) {
268 /* If large page bit is set, check pgsize encoding */
269 if (slbe->large &&
270 (vcpu->arch.hflags & BOOK3S_HFLAG_MULTI_PGSIZE)) {
271 pgsize = decode_pagesize(slbe, pte1);
272 if (pgsize < 0)
273 continue;
275 found = true;
276 break;
280 if (!found) {
281 if (second)
282 goto no_page_found;
283 v_val |= HPTE_V_SECONDARY;
284 second = true;
285 goto do_second;
288 v = be64_to_cpu(pteg[i]);
289 r = be64_to_cpu(pteg[i+1]);
290 pp = (r & HPTE_R_PP) | key;
291 if (r & HPTE_R_PP0)
292 pp |= 8;
294 gpte->eaddr = eaddr;
295 gpte->vpage = kvmppc_mmu_book3s_64_ea_to_vp(vcpu, eaddr, data);
297 eaddr_mask = (1ull << mmu_pagesize(pgsize)) - 1;
298 gpte->raddr = (r & HPTE_R_RPN & ~eaddr_mask) | (eaddr & eaddr_mask);
299 gpte->page_size = pgsize;
300 gpte->may_execute = ((r & HPTE_R_N) ? false : true);
301 if (unlikely(vcpu->arch.disable_kernel_nx) &&
302 !(kvmppc_get_msr(vcpu) & MSR_PR))
303 gpte->may_execute = true;
304 gpte->may_read = false;
305 gpte->may_write = false;
306 gpte->wimg = r & HPTE_R_WIMG;
308 switch (pp) {
309 case 0:
310 case 1:
311 case 2:
312 case 6:
313 gpte->may_write = true;
314 /* fall through */
315 case 3:
316 case 5:
317 case 7:
318 case 10:
319 gpte->may_read = true;
320 break;
323 dprintk("KVM MMU: Translated 0x%lx [0x%llx] -> 0x%llx "
324 "-> 0x%lx\n",
325 eaddr, avpn, gpte->vpage, gpte->raddr);
327 /* Update PTE R and C bits, so the guest's swapper knows we used the
328 * page */
329 if (gpte->may_read && !(r & HPTE_R_R)) {
331 * Set the accessed flag.
332 * We have to write this back with a single byte write
333 * because another vcpu may be accessing this on
334 * non-PAPR platforms such as mac99, and this is
335 * what real hardware does.
337 char __user *addr = (char __user *) (ptegp + (i + 1) * sizeof(u64));
338 r |= HPTE_R_R;
339 put_user(r >> 8, addr + 6);
341 if (iswrite && gpte->may_write && !(r & HPTE_R_C)) {
342 /* Set the dirty flag */
343 /* Use a single byte write */
344 char __user *addr = (char __user *) (ptegp + (i + 1) * sizeof(u64));
345 r |= HPTE_R_C;
346 put_user(r, addr + 7);
349 mutex_unlock(&vcpu->kvm->arch.hpt_mutex);
351 if (!gpte->may_read || (iswrite && !gpte->may_write))
352 return -EPERM;
353 return 0;
355 no_page_found:
356 mutex_unlock(&vcpu->kvm->arch.hpt_mutex);
357 return -ENOENT;
359 no_seg_found:
360 dprintk("KVM MMU: Trigger segment fault\n");
361 return -EINVAL;
364 static void kvmppc_mmu_book3s_64_slbmte(struct kvm_vcpu *vcpu, u64 rs, u64 rb)
366 u64 esid, esid_1t;
367 int slb_nr;
368 struct kvmppc_slb *slbe;
370 dprintk("KVM MMU: slbmte(0x%llx, 0x%llx)\n", rs, rb);
372 esid = GET_ESID(rb);
373 esid_1t = GET_ESID_1T(rb);
374 slb_nr = rb & 0xfff;
376 if (slb_nr > vcpu->arch.slb_nr)
377 return;
379 slbe = &vcpu->arch.slb[slb_nr];
381 slbe->large = (rs & SLB_VSID_L) ? 1 : 0;
382 slbe->tb = (rs & SLB_VSID_B_1T) ? 1 : 0;
383 slbe->esid = slbe->tb ? esid_1t : esid;
384 slbe->vsid = (rs & ~SLB_VSID_B) >> (kvmppc_slb_sid_shift(slbe) - 16);
385 slbe->valid = (rb & SLB_ESID_V) ? 1 : 0;
386 slbe->Ks = (rs & SLB_VSID_KS) ? 1 : 0;
387 slbe->Kp = (rs & SLB_VSID_KP) ? 1 : 0;
388 slbe->nx = (rs & SLB_VSID_N) ? 1 : 0;
389 slbe->class = (rs & SLB_VSID_C) ? 1 : 0;
391 slbe->base_page_size = MMU_PAGE_4K;
392 if (slbe->large) {
393 if (vcpu->arch.hflags & BOOK3S_HFLAG_MULTI_PGSIZE) {
394 switch (rs & SLB_VSID_LP) {
395 case SLB_VSID_LP_00:
396 slbe->base_page_size = MMU_PAGE_16M;
397 break;
398 case SLB_VSID_LP_01:
399 slbe->base_page_size = MMU_PAGE_64K;
400 break;
402 } else
403 slbe->base_page_size = MMU_PAGE_16M;
406 slbe->orige = rb & (ESID_MASK | SLB_ESID_V);
407 slbe->origv = rs;
409 /* Map the new segment */
410 kvmppc_mmu_map_segment(vcpu, esid << SID_SHIFT);
413 static int kvmppc_mmu_book3s_64_slbfee(struct kvm_vcpu *vcpu, gva_t eaddr,
414 ulong *ret_slb)
416 struct kvmppc_slb *slbe = kvmppc_mmu_book3s_64_find_slbe(vcpu, eaddr);
418 if (slbe) {
419 *ret_slb = slbe->origv;
420 return 0;
422 *ret_slb = 0;
423 return -ENOENT;
426 static u64 kvmppc_mmu_book3s_64_slbmfee(struct kvm_vcpu *vcpu, u64 slb_nr)
428 struct kvmppc_slb *slbe;
430 if (slb_nr > vcpu->arch.slb_nr)
431 return 0;
433 slbe = &vcpu->arch.slb[slb_nr];
435 return slbe->orige;
438 static u64 kvmppc_mmu_book3s_64_slbmfev(struct kvm_vcpu *vcpu, u64 slb_nr)
440 struct kvmppc_slb *slbe;
442 if (slb_nr > vcpu->arch.slb_nr)
443 return 0;
445 slbe = &vcpu->arch.slb[slb_nr];
447 return slbe->origv;
450 static void kvmppc_mmu_book3s_64_slbie(struct kvm_vcpu *vcpu, u64 ea)
452 struct kvmppc_slb *slbe;
453 u64 seg_size;
455 dprintk("KVM MMU: slbie(0x%llx)\n", ea);
457 slbe = kvmppc_mmu_book3s_64_find_slbe(vcpu, ea);
459 if (!slbe)
460 return;
462 dprintk("KVM MMU: slbie(0x%llx, 0x%llx)\n", ea, slbe->esid);
464 slbe->valid = false;
465 slbe->orige = 0;
466 slbe->origv = 0;
468 seg_size = 1ull << kvmppc_slb_sid_shift(slbe);
469 kvmppc_mmu_flush_segment(vcpu, ea & ~(seg_size - 1), seg_size);
472 static void kvmppc_mmu_book3s_64_slbia(struct kvm_vcpu *vcpu)
474 int i;
476 dprintk("KVM MMU: slbia()\n");
478 for (i = 1; i < vcpu->arch.slb_nr; i++) {
479 vcpu->arch.slb[i].valid = false;
480 vcpu->arch.slb[i].orige = 0;
481 vcpu->arch.slb[i].origv = 0;
484 if (kvmppc_get_msr(vcpu) & MSR_IR) {
485 kvmppc_mmu_flush_segments(vcpu);
486 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
490 static void kvmppc_mmu_book3s_64_mtsrin(struct kvm_vcpu *vcpu, u32 srnum,
491 ulong value)
493 u64 rb = 0, rs = 0;
496 * According to Book3 2.01 mtsrin is implemented as:
498 * The SLB entry specified by (RB)32:35 is loaded from register
499 * RS, as follows.
501 * SLBE Bit Source SLB Field
503 * 0:31 0x0000_0000 ESID-0:31
504 * 32:35 (RB)32:35 ESID-32:35
505 * 36 0b1 V
506 * 37:61 0x00_0000|| 0b0 VSID-0:24
507 * 62:88 (RS)37:63 VSID-25:51
508 * 89:91 (RS)33:35 Ks Kp N
509 * 92 (RS)36 L ((RS)36 must be 0b0)
510 * 93 0b0 C
513 dprintk("KVM MMU: mtsrin(0x%x, 0x%lx)\n", srnum, value);
515 /* ESID = srnum */
516 rb |= (srnum & 0xf) << 28;
517 /* Set the valid bit */
518 rb |= 1 << 27;
519 /* Index = ESID */
520 rb |= srnum;
522 /* VSID = VSID */
523 rs |= (value & 0xfffffff) << 12;
524 /* flags = flags */
525 rs |= ((value >> 28) & 0x7) << 9;
527 kvmppc_mmu_book3s_64_slbmte(vcpu, rs, rb);
530 static void kvmppc_mmu_book3s_64_tlbie(struct kvm_vcpu *vcpu, ulong va,
531 bool large)
533 u64 mask = 0xFFFFFFFFFULL;
534 long i;
535 struct kvm_vcpu *v;
537 dprintk("KVM MMU: tlbie(0x%lx)\n", va);
540 * The tlbie instruction changed behaviour starting with
541 * POWER6. POWER6 and later don't have the large page flag
542 * in the instruction but in the RB value, along with bits
543 * indicating page and segment sizes.
545 if (vcpu->arch.hflags & BOOK3S_HFLAG_NEW_TLBIE) {
546 /* POWER6 or later */
547 if (va & 1) { /* L bit */
548 if ((va & 0xf000) == 0x1000)
549 mask = 0xFFFFFFFF0ULL; /* 64k page */
550 else
551 mask = 0xFFFFFF000ULL; /* 16M page */
553 } else {
554 /* older processors, e.g. PPC970 */
555 if (large)
556 mask = 0xFFFFFF000ULL;
558 /* flush this VA on all vcpus */
559 kvm_for_each_vcpu(i, v, vcpu->kvm)
560 kvmppc_mmu_pte_vflush(v, va >> 12, mask);
563 #ifdef CONFIG_PPC_64K_PAGES
564 static int segment_contains_magic_page(struct kvm_vcpu *vcpu, ulong esid)
566 ulong mp_ea = vcpu->arch.magic_page_ea;
568 return mp_ea && !(kvmppc_get_msr(vcpu) & MSR_PR) &&
569 (mp_ea >> SID_SHIFT) == esid;
571 #endif
573 static int kvmppc_mmu_book3s_64_esid_to_vsid(struct kvm_vcpu *vcpu, ulong esid,
574 u64 *vsid)
576 ulong ea = esid << SID_SHIFT;
577 struct kvmppc_slb *slb;
578 u64 gvsid = esid;
579 ulong mp_ea = vcpu->arch.magic_page_ea;
580 int pagesize = MMU_PAGE_64K;
581 u64 msr = kvmppc_get_msr(vcpu);
583 if (msr & (MSR_DR|MSR_IR)) {
584 slb = kvmppc_mmu_book3s_64_find_slbe(vcpu, ea);
585 if (slb) {
586 gvsid = slb->vsid;
587 pagesize = slb->base_page_size;
588 if (slb->tb) {
589 gvsid <<= SID_SHIFT_1T - SID_SHIFT;
590 gvsid |= esid & ((1ul << (SID_SHIFT_1T - SID_SHIFT)) - 1);
591 gvsid |= VSID_1T;
596 switch (msr & (MSR_DR|MSR_IR)) {
597 case 0:
598 gvsid = VSID_REAL | esid;
599 break;
600 case MSR_IR:
601 gvsid |= VSID_REAL_IR;
602 break;
603 case MSR_DR:
604 gvsid |= VSID_REAL_DR;
605 break;
606 case MSR_DR|MSR_IR:
607 if (!slb)
608 goto no_slb;
610 break;
611 default:
612 BUG();
613 break;
616 #ifdef CONFIG_PPC_64K_PAGES
618 * Mark this as a 64k segment if the host is using
619 * 64k pages, the host MMU supports 64k pages and
620 * the guest segment page size is >= 64k,
621 * but not if this segment contains the magic page.
623 if (pagesize >= MMU_PAGE_64K &&
624 mmu_psize_defs[MMU_PAGE_64K].shift &&
625 !segment_contains_magic_page(vcpu, esid))
626 gvsid |= VSID_64K;
627 #endif
629 if (kvmppc_get_msr(vcpu) & MSR_PR)
630 gvsid |= VSID_PR;
632 *vsid = gvsid;
633 return 0;
635 no_slb:
636 /* Catch magic page case */
637 if (unlikely(mp_ea) &&
638 unlikely(esid == (mp_ea >> SID_SHIFT)) &&
639 !(kvmppc_get_msr(vcpu) & MSR_PR)) {
640 *vsid = VSID_REAL | esid;
641 return 0;
644 return -EINVAL;
647 static bool kvmppc_mmu_book3s_64_is_dcbz32(struct kvm_vcpu *vcpu)
649 return (to_book3s(vcpu)->hid[5] & 0x80);
652 void kvmppc_mmu_book3s_64_init(struct kvm_vcpu *vcpu)
654 struct kvmppc_mmu *mmu = &vcpu->arch.mmu;
656 mmu->mfsrin = NULL;
657 mmu->mtsrin = kvmppc_mmu_book3s_64_mtsrin;
658 mmu->slbmte = kvmppc_mmu_book3s_64_slbmte;
659 mmu->slbmfee = kvmppc_mmu_book3s_64_slbmfee;
660 mmu->slbmfev = kvmppc_mmu_book3s_64_slbmfev;
661 mmu->slbfee = kvmppc_mmu_book3s_64_slbfee;
662 mmu->slbie = kvmppc_mmu_book3s_64_slbie;
663 mmu->slbia = kvmppc_mmu_book3s_64_slbia;
664 mmu->xlate = kvmppc_mmu_book3s_64_xlate;
665 mmu->tlbie = kvmppc_mmu_book3s_64_tlbie;
666 mmu->esid_to_vsid = kvmppc_mmu_book3s_64_esid_to_vsid;
667 mmu->ea_to_vp = kvmppc_mmu_book3s_64_ea_to_vp;
668 mmu->is_dcbz32 = kvmppc_mmu_book3s_64_is_dcbz32;
670 vcpu->arch.hflags |= BOOK3S_HFLAG_SLB;