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>
58 #include "moea64_if.h"
60 #include "phyp-hvcall.h"
62 #define MMU_PHYP_DEBUG 0
63 #define MMU_PHYP_ID "mmu_phyp: "
65 #define dprintf(fmt, ...) printf(fmt, ## __VA_ARGS__)
66 #define dprintf0(fmt, ...) dprintf(MMU_PHYP_ID fmt, ## __VA_ARGS__)
68 #define dprintf(fmt, args...) do { ; } while(0)
69 #define dprintf0(fmt, args...) do { ; } while(0)
72 static struct rmlock mphyp_eviction_lock;
75 * Kernel MMU interface
78 static void mphyp_bootstrap(mmu_t mmup, vm_offset_t kernelstart,
79 vm_offset_t kernelend);
80 static void mphyp_cpu_bootstrap(mmu_t mmup, int ap);
81 static void *mphyp_dump_pmap(mmu_t mmu, void *ctx, void *buf,
83 static int64_t mphyp_pte_synch(mmu_t, struct pvo_entry *pvo);
84 static int64_t mphyp_pte_clear(mmu_t, struct pvo_entry *pvo, uint64_t ptebit);
85 static int64_t mphyp_pte_unset(mmu_t, struct pvo_entry *pvo);
86 static int mphyp_pte_insert(mmu_t, struct pvo_entry *pvo);
88 static mmu_method_t mphyp_methods[] = {
89 MMUMETHOD(mmu_bootstrap, mphyp_bootstrap),
90 MMUMETHOD(mmu_cpu_bootstrap, mphyp_cpu_bootstrap),
91 MMUMETHOD(mmu_dump_pmap, mphyp_dump_pmap),
93 MMUMETHOD(moea64_pte_synch, mphyp_pte_synch),
94 MMUMETHOD(moea64_pte_clear, mphyp_pte_clear),
95 MMUMETHOD(moea64_pte_unset, mphyp_pte_unset),
96 MMUMETHOD(moea64_pte_insert, mphyp_pte_insert),
98 /* XXX: pmap_copy_page, pmap_init_page with H_PAGE_INIT */
103 MMU_DEF_INHERIT(pseries_mmu, "mmu_phyp", mphyp_methods, 0, oea64_mmu);
105 static int brokenkvm = 0;
108 print_kvm_bug_warning(void *data)
112 printf("WARNING: Running on a broken hypervisor that does "
113 "not support mandatory H_CLEAR_MOD and H_CLEAR_REF "
114 "hypercalls. Performance will be suboptimal.\n");
117 SYSINIT(kvmbugwarn1, SI_SUB_COPYRIGHT, SI_ORDER_THIRD + 1,
118 print_kvm_bug_warning, NULL);
119 SYSINIT(kvmbugwarn2, SI_SUB_LAST, SI_ORDER_THIRD + 1, print_kvm_bug_warning,
123 mphyp_bootstrap(mmu_t mmup, vm_offset_t kernelstart, vm_offset_t kernelend)
125 uint64_t final_pteg_count = 0;
128 uint32_t nptlp, shift = 0, slb_encoding = 0;
129 uint32_t lp_size, lp_encoding;
132 phandle_t dev, node, root;
135 rm_init(&mphyp_eviction_lock, "pte eviction");
137 moea64_early_bootstrap(mmup, kernelstart, kernelend);
141 dev = OF_child(root);
143 res = OF_getprop(dev, "name", buf, sizeof(buf));
144 if (res > 0 && strcmp(buf, "cpus") == 0)
149 node = OF_child(dev);
152 res = OF_getprop(node, "device_type", buf, sizeof(buf));
153 if (res > 0 && strcmp(buf, "cpu") == 0)
155 node = OF_peer(node);
158 res = OF_getencprop(node, "ibm,pft-size", prop, sizeof(prop));
160 panic("mmu_phyp: unknown PFT size");
161 final_pteg_count = 1 << prop[1];
162 res = OF_getencprop(node, "ibm,slb-size", prop, sizeof(prop[0]));
165 dprintf0("slb-size=%i\n", n_slbs);
167 moea64_pteg_count = final_pteg_count / sizeof(struct lpteg);
169 /* Clear any old page table entries */
170 for (idx = 0; idx < moea64_pteg_count*8; idx++) {
171 phyp_pft_hcall(H_READ, 0, idx, 0, 0, &old.pte_hi,
172 &old.pte_lo, &old.pte_lo);
173 vsid = (old.pte_hi << (ADDR_API_SHFT64 - ADDR_PIDX_SHFT)) >> 28;
174 if (vsid == VSID_VRMA || vsid == 0 /* Older VRMA */)
177 if (old.pte_hi & LPTE_VALID)
178 phyp_hcall(H_REMOVE, 0, idx, 0);
182 * Scan the large page size property for PAPR compatible machines.
183 * See PAPR D.5 Changes to Section 5.1.4, 'CPU Node Properties'
184 * for the encoding of the property.
187 len = OF_getproplen(node, "ibm,segment-page-sizes");
190 * We have to use a variable length array on the stack
191 * since we have very limited stack space.
193 pcell_t arr[len/sizeof(cell_t)];
194 res = OF_getencprop(node, "ibm,segment-page-sizes", arr,
200 slb_encoding = arr[idx + 1];
201 nptlp = arr[idx + 2];
203 dprintf0("Segment Page Size: "
204 "%uKB, slb_enc=0x%X: {size, encoding}[%u] =",
205 shift > 10? 1 << (shift-10) : 0,
206 slb_encoding, nptlp);
210 while (len > 0 && nptlp) {
212 lp_encoding = arr[idx+1];
214 dprintf(" {%uKB, 0x%X}",
215 lp_size > 10? 1 << (lp_size-10) : 0,
218 if (slb_encoding == SLBV_L && lp_encoding == 0)
226 if (nptlp && slb_encoding == SLBV_L && lp_encoding == 0)
231 moea64_large_page_shift = shift;
232 moea64_large_page_size = 1ULL << lp_size;
233 moea64_large_page_mask = moea64_large_page_size - 1;
236 "Support for hugepages of %uKB detected\n",
237 moea64_large_page_shift > 10?
238 1 << (moea64_large_page_shift-10) : 0);
240 moea64_large_page_size = 0;
241 moea64_large_page_shift = 0;
242 moea64_large_page_mask = 0;
245 "Support for hugepages not found\n");
249 moea64_mid_bootstrap(mmup, kernelstart, kernelend);
250 moea64_late_bootstrap(mmup, kernelstart, kernelend);
252 /* Test for broken versions of KVM that don't conform to the spec */
253 if (phyp_hcall(H_CLEAR_MOD, 0, 0) == H_FUNCTION)
258 mphyp_cpu_bootstrap(mmu_t mmup, int ap)
260 struct slb *slb = PCPU_GET(aim.slb);
265 * Install kernel SLB entries
268 __asm __volatile ("slbia");
269 __asm __volatile ("slbmfee %0,%1; slbie %0;" : "=r"(seg0) : "r"(0));
270 for (i = 0; i < 64; i++) {
271 if (!(slb[i].slbe & SLBE_VALID))
274 __asm __volatile ("slbmte %0, %1" ::
275 "r"(slb[i].slbv), "r"(slb[i].slbe));
280 mphyp_pte_synch(mmu_t mmu, struct pvo_entry *pvo)
285 __asm __volatile("ptesync");
286 phyp_pft_hcall(H_READ, 0, pvo->pvo_pte.slot, 0, 0, &pte.pte_hi,
288 if ((pte.pte_hi & LPTE_AVPN_MASK) !=
289 ((pvo->pvo_vpn >> (ADDR_API_SHFT64 - ADDR_PIDX_SHFT)) &
292 if (!(pte.pte_hi & LPTE_VALID))
295 return (pte.pte_lo & (LPTE_CHG | LPTE_REF));
299 mphyp_pte_clear(mmu_t mmu, struct pvo_entry *pvo, uint64_t ptebit)
301 struct rm_priotracker track;
303 uint64_t ptelo, junk;
307 * This involves two steps (synch and clear) so we need the entry
308 * not to change in the middle. We are protected against deliberate
309 * unset by virtue of holding the pmap lock. Protection against
310 * incidental unset (page table eviction) comes from holding the
311 * shared eviction lock.
313 PMAP_LOCK_ASSERT(pvo->pvo_pmap, MA_OWNED);
314 rm_rlock(&mphyp_eviction_lock, &track);
316 refchg = mphyp_pte_synch(mmu, pvo);
318 rm_runlock(&mphyp_eviction_lock, &track);
324 * No way to clear either bit, which is total madness.
325 * Pessimistically claim that, once modified, it stays so
326 * forever and that it is never referenced.
328 rm_runlock(&mphyp_eviction_lock, &track);
329 return (refchg & ~LPTE_REF);
332 if (ptebit & LPTE_CHG) {
333 err = phyp_pft_hcall(H_CLEAR_MOD, 0, pvo->pvo_pte.slot, 0, 0,
334 &ptelo, &junk, &junk);
335 KASSERT(err == H_SUCCESS,
336 ("Error clearing page change bit: %d", err));
337 refchg |= (ptelo & LPTE_CHG);
339 if (ptebit & LPTE_REF) {
340 err = phyp_pft_hcall(H_CLEAR_REF, 0, pvo->pvo_pte.slot, 0, 0,
341 &ptelo, &junk, &junk);
342 KASSERT(err == H_SUCCESS,
343 ("Error clearing page reference bit: %d", err));
344 refchg |= (ptelo & LPTE_REF);
347 rm_runlock(&mphyp_eviction_lock, &track);
353 mphyp_pte_unset(mmu_t mmu, struct pvo_entry *pvo)
359 PMAP_LOCK_ASSERT(pvo->pvo_pmap, MA_OWNED);
361 moea64_pte_from_pvo(pvo, &pte);
363 err = phyp_pft_hcall(H_REMOVE, H_AVPN, pvo->pvo_pte.slot,
364 pte.pte_hi & LPTE_AVPN_MASK, 0, &pte.pte_hi, &pte.pte_lo,
366 KASSERT(err == H_SUCCESS || err == H_NOT_FOUND,
367 ("Error removing page: %d", err));
369 if (err == H_NOT_FOUND) {
370 STAT_MOEA64(moea64_pte_overflow--);
374 return (pte.pte_lo & (LPTE_REF | LPTE_CHG));
378 mphyp_pte_spillable_ident(uintptr_t ptegbase, struct lpte *to_evict)
380 uint64_t slot, junk, k;
384 /* Start at a random slot */
387 for (j = 0; j < 8; j++) {
388 slot = ptegbase + (i + j) % 8;
389 phyp_pft_hcall(H_READ, 0, slot, 0, 0, &pt.pte_hi,
392 if (pt.pte_hi & LPTE_WIRED)
395 /* This is a candidate, so remember it */
398 /* Try to get a page that has not been used lately */
399 if (!(pt.pte_hi & LPTE_VALID) || !(pt.pte_lo & LPTE_REF)) {
400 memcpy(to_evict, &pt, sizeof(struct lpte));
408 phyp_pft_hcall(H_READ, 0, k, 0, 0, &to_evict->pte_hi,
409 &to_evict->pte_lo, &junk);
414 mphyp_pte_insert(mmu_t mmu, struct pvo_entry *pvo)
416 struct rm_priotracker track;
418 struct lpte evicted, pte;
419 uint64_t index, junk, lastptelo;
421 PMAP_LOCK_ASSERT(pvo->pvo_pmap, MA_OWNED);
424 moea64_pte_from_pvo(pvo, &pte);
427 /* Make sure further insertion is locked out during evictions */
428 rm_rlock(&mphyp_eviction_lock, &track);
431 * First try primary hash.
433 pvo->pvo_pte.slot &= ~7UL; /* Base slot address */
434 result = phyp_pft_hcall(H_ENTER, 0, pvo->pvo_pte.slot, pte.pte_hi,
435 pte.pte_lo, &index, &evicted.pte_lo, &junk);
436 if (result == H_SUCCESS) {
437 rm_runlock(&mphyp_eviction_lock, &track);
438 pvo->pvo_pte.slot = index;
441 KASSERT(result == H_PTEG_FULL, ("Page insertion error: %ld "
442 "(ptegidx: %#zx/%#lx, PTE %#lx/%#lx", result, pvo->pvo_pte.slot,
443 moea64_pteg_count, pte.pte_hi, pte.pte_lo));
446 * Next try secondary hash.
448 pvo->pvo_vaddr ^= PVO_HID;
449 pte.pte_hi ^= LPTE_HID;
450 pvo->pvo_pte.slot ^= (moea64_pteg_mask << 3);
452 result = phyp_pft_hcall(H_ENTER, 0, pvo->pvo_pte.slot,
453 pte.pte_hi, pte.pte_lo, &index, &evicted.pte_lo, &junk);
454 if (result == H_SUCCESS) {
455 rm_runlock(&mphyp_eviction_lock, &track);
456 pvo->pvo_pte.slot = index;
459 KASSERT(result == H_PTEG_FULL, ("Secondary page insertion error: %ld",
463 * Out of luck. Find a PTE to sacrifice.
466 /* Lock out all insertions for a bit */
467 rm_runlock(&mphyp_eviction_lock, &track);
468 rm_wlock(&mphyp_eviction_lock);
470 index = mphyp_pte_spillable_ident(pvo->pvo_pte.slot, &evicted);
472 /* Try other hash table? */
473 pvo->pvo_vaddr ^= PVO_HID;
474 pte.pte_hi ^= LPTE_HID;
475 pvo->pvo_pte.slot ^= (moea64_pteg_mask << 3);
476 index = mphyp_pte_spillable_ident(pvo->pvo_pte.slot, &evicted);
480 /* No freeable slots in either PTEG? We're hosed. */
481 rm_wunlock(&mphyp_eviction_lock);
482 panic("mphyp_pte_insert: overflow");
486 /* Victim acquired: update page before waving goodbye */
487 if (evicted.pte_hi & LPTE_VALID) {
488 result = phyp_pft_hcall(H_REMOVE, H_AVPN, index,
489 evicted.pte_hi & LPTE_AVPN_MASK, 0, &junk, &lastptelo,
491 STAT_MOEA64(moea64_pte_overflow++);
492 KASSERT(result == H_SUCCESS || result == H_NOT_FOUND,
493 ("Error evicting page: %d", (int)result));
499 result = phyp_pft_hcall(H_ENTER, H_EXACT, index, pte.pte_hi,
500 pte.pte_lo, &index, &evicted.pte_lo, &junk);
501 rm_wunlock(&mphyp_eviction_lock); /* All clear */
503 pvo->pvo_pte.slot = index;
504 if (result == H_SUCCESS)
507 panic("Page replacement error: %ld", result);
512 mphyp_dump_pmap(mmu_t mmu, void *ctx, void *buf, u_long *nbytes)
514 struct dump_context *dctx;
515 struct lpte p, *pbuf;
518 u_long ptex, ptex_end;
520 dctx = (struct dump_context *)ctx;
521 pbuf = (struct lpte *)buf;
524 ptex_end = ptex + dctx->blksz / sizeof(struct lpte);
525 ptex_end = MIN(ptex_end, dctx->ptex_end);
526 *nbytes = (ptex_end - ptex) * sizeof(struct lpte);
531 for (; ptex < ptex_end; ptex++) {
532 phyp_pft_hcall(H_READ, 0, ptex, 0, 0,
533 &p.pte_hi, &p.pte_lo, &junk);