2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2010 Andreas Tobler
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/kernel.h>
35 #include <sys/rmlock.h>
36 #include <sys/mutex.h>
38 #include <sys/sysctl.h>
39 #include <sys/systm.h>
40 #include <sys/vmmeter.h>
42 #include <dev/ofw/openfirm.h>
43 #include <machine/ofw_machdep.h>
46 #include <vm/vm_param.h>
47 #include <vm/vm_kern.h>
48 #include <vm/vm_page.h>
49 #include <vm/vm_map.h>
50 #include <vm/vm_object.h>
51 #include <vm/vm_extern.h>
52 #include <vm/vm_pageout.h>
55 #include <powerpc/aim/mmu_oea64.h>
57 #include "phyp-hvcall.h"
59 #define MMU_PHYP_DEBUG 0
60 #define MMU_PHYP_ID "mmu_phyp: "
62 #define dprintf(fmt, ...) printf(fmt, ## __VA_ARGS__)
63 #define dprintf0(fmt, ...) dprintf(MMU_PHYP_ID fmt, ## __VA_ARGS__)
65 #define dprintf(fmt, args...) do { ; } while(0)
66 #define dprintf0(fmt, args...) do { ; } while(0)
69 static struct rmlock mphyp_eviction_lock;
72 * Kernel MMU interface
75 static void mphyp_install(void);
76 static void mphyp_bootstrap(vm_offset_t kernelstart,
77 vm_offset_t kernelend);
78 static void mphyp_cpu_bootstrap(int ap);
79 static void *mphyp_dump_pmap(void *ctx, void *buf,
81 static int64_t mphyp_pte_synch(struct pvo_entry *pvo);
82 static int64_t mphyp_pte_clear(struct pvo_entry *pvo, uint64_t ptebit);
83 static int64_t mphyp_pte_unset(struct pvo_entry *pvo);
84 static int64_t mphyp_pte_insert(struct pvo_entry *pvo);
85 static int64_t mphyp_pte_unset_sp(struct pvo_entry *pvo);
86 static int64_t mphyp_pte_insert_sp(struct pvo_entry *pvo);
87 static int64_t mphyp_pte_replace_sp(struct pvo_entry *pvo);
89 static struct pmap_funcs mphyp_methods = {
90 .install = mphyp_install,
91 .bootstrap = mphyp_bootstrap,
92 .cpu_bootstrap = mphyp_cpu_bootstrap,
93 .dumpsys_dump_pmap = mphyp_dump_pmap,
96 static struct moea64_funcs mmu_phyp_funcs = {
97 .pte_synch = mphyp_pte_synch,
98 .pte_clear = mphyp_pte_clear,
99 .pte_unset = mphyp_pte_unset,
100 .pte_insert = mphyp_pte_insert,
101 .pte_unset_sp = mphyp_pte_unset_sp,
102 .pte_insert_sp = mphyp_pte_insert_sp,
103 .pte_replace_sp = mphyp_pte_replace_sp,
106 MMU_DEF_INHERIT(pseries_mmu, "mmu_phyp", mphyp_methods, oea64_mmu);
108 static int brokenkvm = 0;
109 static uint64_t final_pteg_count = 0;
112 print_kvm_bug_warning(void *data)
116 printf("WARNING: Running on a broken hypervisor that does "
117 "not support mandatory H_CLEAR_MOD and H_CLEAR_REF "
118 "hypercalls. Performance will be suboptimal.\n");
121 SYSINIT(kvmbugwarn1, SI_SUB_COPYRIGHT, SI_ORDER_THIRD + 1,
122 print_kvm_bug_warning, NULL);
123 SYSINIT(kvmbugwarn2, SI_SUB_LAST, SI_ORDER_THIRD + 1, print_kvm_bug_warning,
131 uint32_t nptlp, shift = 0, slb_encoding = 0;
132 uint32_t lp_size, lp_encoding;
133 phandle_t dev, node, root;
139 dev = OF_child(root);
141 res = OF_getprop(dev, "name", buf, sizeof(buf));
142 if (res > 0 && strcmp(buf, "cpus") == 0)
147 node = OF_child(dev);
150 res = OF_getprop(node, "device_type", buf, sizeof(buf));
151 if (res > 0 && strcmp(buf, "cpu") == 0)
153 node = OF_peer(node);
156 res = OF_getencprop(node, "ibm,pft-size", prop, sizeof(prop));
158 panic("mmu_phyp: unknown PFT size");
159 final_pteg_count = 1 << prop[1];
160 res = OF_getencprop(node, "ibm,slb-size", prop, sizeof(prop[0]));
163 dprintf0("slb-size=%i\n", n_slbs);
166 * Scan the large page size property for PAPR compatible machines.
167 * See PAPR D.5 Changes to Section 5.1.4, 'CPU Node Properties'
168 * for the encoding of the property.
171 len = OF_getproplen(node, "ibm,segment-page-sizes");
174 * We have to use a variable length array on the stack
175 * since we have very limited stack space.
177 pcell_t arr[len/sizeof(cell_t)];
178 res = OF_getencprop(node, "ibm,segment-page-sizes", arr,
185 slb_encoding = arr[idx + 1];
186 nptlp = arr[idx + 2];
188 dprintf0("Segment Page Size: "
189 "%uKB, slb_enc=0x%X: {size, encoding}[%u] =",
190 shift > 10? 1 << (shift-10) : 0,
191 slb_encoding, nptlp);
195 while (len > 0 && nptlp) {
197 lp_encoding = arr[idx+1];
199 dprintf(" {%uKB, 0x%X}",
200 lp_size > 10? 1 << (lp_size-10) : 0,
203 if (slb_encoding == SLBV_L && lp_encoding == 0)
206 if (slb_encoding == SLB_PGSZ_4K_4K &&
207 lp_encoding == LP_4K_16M)
208 moea64_has_lp_4k_16m = true;
215 if (has_lp && moea64_has_lp_4k_16m)
220 moea64_large_page_shift = shift;
221 moea64_large_page_size = 1ULL << lp_size;
222 moea64_large_page_mask = moea64_large_page_size - 1;
225 "Support for hugepages of %uKB detected\n",
226 moea64_large_page_shift > 10?
227 1 << (moea64_large_page_shift-10) : 0);
229 moea64_large_page_size = 0;
230 moea64_large_page_shift = 0;
231 moea64_large_page_mask = 0;
234 "Support for hugepages not found\n");
238 moea64_ops = &mmu_phyp_funcs;
244 mphyp_bootstrap(vm_offset_t kernelstart, vm_offset_t kernelend)
250 rm_init(&mphyp_eviction_lock, "pte eviction");
252 moea64_early_bootstrap(kernelstart, kernelend);
254 moea64_pteg_count = final_pteg_count / sizeof(struct lpteg);
256 /* Clear any old page table entries */
257 for (idx = 0; idx < moea64_pteg_count*8; idx++) {
258 phyp_pft_hcall(H_READ, 0, idx, 0, 0, &old.pte_hi,
259 &old.pte_lo, &old.pte_lo);
260 vsid = (old.pte_hi << (ADDR_API_SHFT64 - ADDR_PIDX_SHFT)) >> 28;
261 if (vsid == VSID_VRMA || vsid == 0 /* Older VRMA */)
264 if (old.pte_hi & LPTE_VALID)
265 phyp_hcall(H_REMOVE, 0, idx, 0);
268 moea64_mid_bootstrap(kernelstart, kernelend);
269 moea64_late_bootstrap(kernelstart, kernelend);
271 /* Test for broken versions of KVM that don't conform to the spec */
272 if (phyp_hcall(H_CLEAR_MOD, 0, 0) == H_FUNCTION)
277 mphyp_cpu_bootstrap(int ap)
279 struct slb *slb = PCPU_GET(aim.slb);
284 * Install kernel SLB entries
287 __asm __volatile ("slbia");
288 __asm __volatile ("slbmfee %0,%1; slbie %0;" : "=r"(seg0) : "r"(0));
289 for (i = 0; i < 64; i++) {
290 if (!(slb[i].slbe & SLBE_VALID))
293 __asm __volatile ("slbmte %0, %1" ::
294 "r"(slb[i].slbv), "r"(slb[i].slbe));
299 mphyp_pte_synch(struct pvo_entry *pvo)
304 __asm __volatile("ptesync");
305 phyp_pft_hcall(H_READ, 0, pvo->pvo_pte.slot, 0, 0, &pte.pte_hi,
307 if ((pte.pte_hi & LPTE_AVPN_MASK) !=
308 ((pvo->pvo_vpn >> (ADDR_API_SHFT64 - ADDR_PIDX_SHFT)) &
311 if (!(pte.pte_hi & LPTE_VALID))
314 return (pte.pte_lo & (LPTE_CHG | LPTE_REF));
318 mphyp_pte_clear(struct pvo_entry *pvo, uint64_t ptebit)
320 struct rm_priotracker track;
322 uint64_t ptelo, junk;
326 * This involves two steps (synch and clear) so we need the entry
327 * not to change in the middle. We are protected against deliberate
328 * unset by virtue of holding the pmap lock. Protection against
329 * incidental unset (page table eviction) comes from holding the
330 * shared eviction lock.
332 PMAP_LOCK_ASSERT(pvo->pvo_pmap, MA_OWNED);
333 rm_rlock(&mphyp_eviction_lock, &track);
335 refchg = mphyp_pte_synch(pvo);
337 rm_runlock(&mphyp_eviction_lock, &track);
343 * No way to clear either bit, which is total madness.
344 * Pessimistically claim that, once modified, it stays so
345 * forever and that it is never referenced.
347 rm_runlock(&mphyp_eviction_lock, &track);
348 return (refchg & ~LPTE_REF);
351 if (ptebit & LPTE_CHG) {
352 err = phyp_pft_hcall(H_CLEAR_MOD, 0, pvo->pvo_pte.slot, 0, 0,
353 &ptelo, &junk, &junk);
354 KASSERT(err == H_SUCCESS,
355 ("Error clearing page change bit: %d", err));
356 refchg |= (ptelo & LPTE_CHG);
358 if (ptebit & LPTE_REF) {
359 err = phyp_pft_hcall(H_CLEAR_REF, 0, pvo->pvo_pte.slot, 0, 0,
360 &ptelo, &junk, &junk);
361 KASSERT(err == H_SUCCESS,
362 ("Error clearing page reference bit: %d", err));
363 refchg |= (ptelo & LPTE_REF);
366 rm_runlock(&mphyp_eviction_lock, &track);
372 mphyp_pte_unset(struct pvo_entry *pvo)
378 PMAP_LOCK_ASSERT(pvo->pvo_pmap, MA_OWNED);
380 moea64_pte_from_pvo(pvo, &pte);
382 err = phyp_pft_hcall(H_REMOVE, H_AVPN, pvo->pvo_pte.slot,
383 pte.pte_hi & LPTE_AVPN_MASK, 0, &pte.pte_hi, &pte.pte_lo,
385 KASSERT(err == H_SUCCESS || err == H_NOT_FOUND,
386 ("Error removing page: %d", err));
388 if (err == H_NOT_FOUND) {
389 STAT_MOEA64(moea64_pte_overflow--);
393 return (pte.pte_lo & (LPTE_REF | LPTE_CHG));
397 mphyp_pte_spillable_ident(uintptr_t ptegbase, struct lpte *to_evict)
399 uint64_t slot, junk, k;
403 /* Start at a random slot */
406 for (j = 0; j < 8; j++) {
407 slot = ptegbase + (i + j) % 8;
408 phyp_pft_hcall(H_READ, 0, slot, 0, 0, &pt.pte_hi,
411 if ((pt.pte_hi & (LPTE_WIRED | LPTE_BIG)) != 0)
414 /* This is a candidate, so remember it */
417 /* Try to get a page that has not been used lately */
418 if (!(pt.pte_hi & LPTE_VALID) || !(pt.pte_lo & LPTE_REF)) {
419 memcpy(to_evict, &pt, sizeof(struct lpte));
427 phyp_pft_hcall(H_READ, 0, k, 0, 0, &to_evict->pte_hi,
428 &to_evict->pte_lo, &junk);
432 static __inline int64_t
433 mphyp_pte_insert_locked(struct pvo_entry *pvo, struct lpte *pte)
436 uint64_t index, junk;
440 * First try primary hash.
442 pvo->pvo_pte.slot &= ~7UL; /* Base slot address */
443 result = phyp_pft_hcall(H_ENTER, 0, pvo->pvo_pte.slot, pte->pte_hi,
444 pte->pte_lo, &index, &evicted.pte_lo, &junk);
445 if (result == H_SUCCESS) {
446 pvo->pvo_pte.slot = index;
449 KASSERT(result == H_PTEG_FULL, ("Page insertion error: %ld "
450 "(ptegidx: %#zx/%#lx, PTE %#lx/%#lx", result, pvo->pvo_pte.slot,
451 moea64_pteg_count, pte->pte_hi, pte->pte_lo));
454 * Next try secondary hash.
456 pvo->pvo_vaddr ^= PVO_HID;
457 pte->pte_hi ^= LPTE_HID;
458 pvo->pvo_pte.slot ^= (moea64_pteg_mask << 3);
460 result = phyp_pft_hcall(H_ENTER, 0, pvo->pvo_pte.slot,
461 pte->pte_hi, pte->pte_lo, &index, &evicted.pte_lo, &junk);
462 if (result == H_SUCCESS) {
463 pvo->pvo_pte.slot = index;
466 KASSERT(result == H_PTEG_FULL, ("Secondary page insertion error: %ld",
473 static __inline int64_t
474 mphyp_pte_evict_and_insert_locked(struct pvo_entry *pvo, struct lpte *pte)
477 uint64_t index, junk, lastptelo;
482 index = mphyp_pte_spillable_ident(pvo->pvo_pte.slot, &evicted);
484 /* Try other hash table? */
485 pvo->pvo_vaddr ^= PVO_HID;
486 pte->pte_hi ^= LPTE_HID;
487 pvo->pvo_pte.slot ^= (moea64_pteg_mask << 3);
488 index = mphyp_pte_spillable_ident(pvo->pvo_pte.slot, &evicted);
492 /* No freeable slots in either PTEG? We're hosed. */
493 rm_wunlock(&mphyp_eviction_lock);
494 panic("mphyp_pte_insert: overflow");
498 /* Victim acquired: update page before waving goodbye */
499 if (evicted.pte_hi & LPTE_VALID) {
500 result = phyp_pft_hcall(H_REMOVE, H_AVPN, index,
501 evicted.pte_hi & LPTE_AVPN_MASK, 0, &junk, &lastptelo,
503 STAT_MOEA64(moea64_pte_overflow++);
504 KASSERT(result == H_SUCCESS || result == H_NOT_FOUND,
505 ("Error evicting page: %d", (int)result));
511 result = phyp_pft_hcall(H_ENTER, H_EXACT, index, pte->pte_hi,
512 pte->pte_lo, &index, &evicted.pte_lo, &junk);
514 pvo->pvo_pte.slot = index;
515 if (result == H_SUCCESS)
518 rm_wunlock(&mphyp_eviction_lock);
519 panic("Page replacement error: %ld", result);
524 mphyp_pte_insert(struct pvo_entry *pvo)
526 struct rm_priotracker track;
530 PMAP_LOCK_ASSERT(pvo->pvo_pmap, MA_OWNED);
533 moea64_pte_from_pvo(pvo, &pte);
535 /* Make sure further insertion is locked out during evictions */
536 rm_rlock(&mphyp_eviction_lock, &track);
538 ret = mphyp_pte_insert_locked(pvo, &pte);
539 rm_runlock(&mphyp_eviction_lock, &track);
543 * Out of luck. Find a PTE to sacrifice.
546 /* Lock out all insertions for a bit */
547 rm_wlock(&mphyp_eviction_lock);
548 ret = mphyp_pte_evict_and_insert_locked(pvo, &pte);
549 rm_wunlock(&mphyp_eviction_lock); /* All clear */
556 mphyp_dump_pmap(void *ctx, void *buf, u_long *nbytes)
558 struct dump_context *dctx;
559 struct lpte p, *pbuf;
562 u_long ptex, ptex_end;
564 dctx = (struct dump_context *)ctx;
565 pbuf = (struct lpte *)buf;
568 ptex_end = ptex + dctx->blksz / sizeof(struct lpte);
569 ptex_end = MIN(ptex_end, dctx->ptex_end);
570 *nbytes = (ptex_end - ptex) * sizeof(struct lpte);
575 for (; ptex < ptex_end; ptex++) {
576 phyp_pft_hcall(H_READ, 0, ptex, 0, 0,
577 &p.pte_hi, &p.pte_lo, &junk);
586 mphyp_pte_unset_sp(struct pvo_entry *pvo)
589 uint64_t junk, refchg;
595 PMAP_LOCK_ASSERT(pm, MA_OWNED);
596 KASSERT((PVO_VADDR(pvo) & HPT_SP_MASK) == 0,
597 ("%s: va %#jx unaligned", __func__, (uintmax_t)PVO_VADDR(pvo)));
600 eva = PVO_VADDR(pvo) + HPT_SP_SIZE;
602 for (; pvo != NULL && PVO_VADDR(pvo) < eva;
603 pvo = RB_NEXT(pvo_tree, &pm->pmap_pvo, pvo)) {
604 moea64_pte_from_pvo(pvo, &pte);
606 err = phyp_pft_hcall(H_REMOVE, H_AVPN, pvo->pvo_pte.slot,
607 pte.pte_hi & LPTE_AVPN_MASK, 0, &pte.pte_hi, &pte.pte_lo,
609 KASSERT(err == H_SUCCESS || err == H_NOT_FOUND,
610 ("Error removing page: %d", err));
612 if (err == H_NOT_FOUND)
613 STAT_MOEA64(moea64_pte_overflow--);
614 refchg |= pte.pte_lo & (LPTE_REF | LPTE_CHG);
621 mphyp_pte_insert_sp(struct pvo_entry *pvo)
623 struct rm_priotracker track;
630 PMAP_LOCK_ASSERT(pm, MA_OWNED);
631 KASSERT((PVO_VADDR(pvo) & HPT_SP_MASK) == 0,
632 ("%s: va %#jx unaligned", __func__, (uintmax_t)PVO_VADDR(pvo)));
634 eva = PVO_VADDR(pvo) + HPT_SP_SIZE;
636 /* Make sure further insertion is locked out during evictions */
637 rm_rlock(&mphyp_eviction_lock, &track);
639 for (; pvo != NULL && PVO_VADDR(pvo) < eva;
640 pvo = RB_NEXT(pvo_tree, &pm->pmap_pvo, pvo)) {
642 moea64_pte_from_pvo(pvo, &pte);
644 ret = mphyp_pte_insert_locked(pvo, &pte);
647 * Out of luck. Find a PTE to sacrifice.
650 /* Lock out all insertions for a bit */
651 rm_runlock(&mphyp_eviction_lock, &track);
652 rm_wlock(&mphyp_eviction_lock);
653 mphyp_pte_evict_and_insert_locked(pvo, &pte);
654 rm_wunlock(&mphyp_eviction_lock); /* All clear */
655 rm_rlock(&mphyp_eviction_lock, &track);
659 rm_runlock(&mphyp_eviction_lock, &track);
664 mphyp_pte_replace_sp(struct pvo_entry *pvo)
668 refchg = mphyp_pte_unset_sp(pvo);
669 mphyp_pte_insert_sp(pvo);