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 int64_t mphyp_pte_synch(mmu_t, struct pvo_entry *pvo);
82 static int64_t mphyp_pte_clear(mmu_t, struct pvo_entry *pvo, uint64_t ptebit);
83 static int64_t mphyp_pte_unset(mmu_t, struct pvo_entry *pvo);
84 static int mphyp_pte_insert(mmu_t, struct pvo_entry *pvo);
86 static mmu_method_t mphyp_methods[] = {
87 MMUMETHOD(mmu_bootstrap, mphyp_bootstrap),
88 MMUMETHOD(mmu_cpu_bootstrap, mphyp_cpu_bootstrap),
90 MMUMETHOD(moea64_pte_synch, mphyp_pte_synch),
91 MMUMETHOD(moea64_pte_clear, mphyp_pte_clear),
92 MMUMETHOD(moea64_pte_unset, mphyp_pte_unset),
93 MMUMETHOD(moea64_pte_insert, mphyp_pte_insert),
95 /* XXX: pmap_copy_page, pmap_init_page with H_PAGE_INIT */
100 MMU_DEF_INHERIT(pseries_mmu, "mmu_phyp", mphyp_methods, 0, oea64_mmu);
102 static int brokenkvm = 0;
105 print_kvm_bug_warning(void *data)
109 printf("WARNING: Running on a broken hypervisor that does "
110 "not support mandatory H_CLEAR_MOD and H_CLEAR_REF "
111 "hypercalls. Performance will be suboptimal.\n");
114 SYSINIT(kvmbugwarn1, SI_SUB_COPYRIGHT, SI_ORDER_THIRD + 1,
115 print_kvm_bug_warning, NULL);
116 SYSINIT(kvmbugwarn2, SI_SUB_LAST, SI_ORDER_THIRD + 1, print_kvm_bug_warning,
120 mphyp_bootstrap(mmu_t mmup, vm_offset_t kernelstart, vm_offset_t kernelend)
122 uint64_t final_pteg_count = 0;
125 uint32_t nptlp, shift = 0, slb_encoding = 0;
126 uint32_t lp_size, lp_encoding;
129 phandle_t dev, node, root;
132 rm_init(&mphyp_eviction_lock, "pte eviction");
134 moea64_early_bootstrap(mmup, kernelstart, kernelend);
138 dev = OF_child(root);
140 res = OF_getprop(dev, "name", buf, sizeof(buf));
141 if (res > 0 && strcmp(buf, "cpus") == 0)
146 node = OF_child(dev);
149 res = OF_getprop(node, "device_type", buf, sizeof(buf));
150 if (res > 0 && strcmp(buf, "cpu") == 0)
152 node = OF_peer(node);
155 res = OF_getencprop(node, "ibm,pft-size", prop, sizeof(prop));
157 panic("mmu_phyp: unknown PFT size");
158 final_pteg_count = 1 << prop[1];
159 res = OF_getencprop(node, "ibm,slb-size", prop, sizeof(prop[0]));
162 dprintf0("slb-size=%i\n", n_slbs);
164 moea64_pteg_count = final_pteg_count / sizeof(struct lpteg);
166 /* Clear any old page table entries */
167 for (idx = 0; idx < moea64_pteg_count*8; idx++) {
168 phyp_pft_hcall(H_READ, 0, idx, 0, 0, &old.pte_hi,
169 &old.pte_lo, &old.pte_lo);
170 vsid = (old.pte_hi << (ADDR_API_SHFT64 - ADDR_PIDX_SHFT)) >> 28;
171 if (vsid == VSID_VRMA || vsid == 0 /* Older VRMA */)
174 if (old.pte_hi & LPTE_VALID)
175 phyp_hcall(H_REMOVE, 0, idx, 0);
179 * Scan the large page size property for PAPR compatible machines.
180 * See PAPR D.5 Changes to Section 5.1.4, 'CPU Node Properties'
181 * for the encoding of the property.
184 len = OF_getproplen(node, "ibm,segment-page-sizes");
187 * We have to use a variable length array on the stack
188 * since we have very limited stack space.
190 pcell_t arr[len/sizeof(cell_t)];
191 res = OF_getencprop(node, "ibm,segment-page-sizes", arr,
197 slb_encoding = arr[idx + 1];
198 nptlp = arr[idx + 2];
200 dprintf0("Segment Page Size: "
201 "%uKB, slb_enc=0x%X: {size, encoding}[%u] =",
202 shift > 10? 1 << (shift-10) : 0,
203 slb_encoding, nptlp);
207 while (len > 0 && nptlp) {
209 lp_encoding = arr[idx+1];
211 dprintf(" {%uKB, 0x%X}",
212 lp_size > 10? 1 << (lp_size-10) : 0,
215 if (slb_encoding == SLBV_L && lp_encoding == 0)
223 if (nptlp && slb_encoding == SLBV_L && lp_encoding == 0)
228 moea64_large_page_shift = shift;
229 moea64_large_page_size = 1ULL << lp_size;
230 moea64_large_page_mask = moea64_large_page_size - 1;
233 "Support for hugepages of %uKB detected\n",
234 moea64_large_page_shift > 10?
235 1 << (moea64_large_page_shift-10) : 0);
237 moea64_large_page_size = 0;
238 moea64_large_page_shift = 0;
239 moea64_large_page_mask = 0;
242 "Support for hugepages not found\n");
246 moea64_mid_bootstrap(mmup, kernelstart, kernelend);
247 moea64_late_bootstrap(mmup, kernelstart, kernelend);
249 /* Test for broken versions of KVM that don't conform to the spec */
250 if (phyp_hcall(H_CLEAR_MOD, 0, 0) == H_FUNCTION)
255 mphyp_cpu_bootstrap(mmu_t mmup, int ap)
257 struct slb *slb = PCPU_GET(aim.slb);
262 * Install kernel SLB entries
265 __asm __volatile ("slbia");
266 __asm __volatile ("slbmfee %0,%1; slbie %0;" : "=r"(seg0) : "r"(0));
267 for (i = 0; i < 64; i++) {
268 if (!(slb[i].slbe & SLBE_VALID))
271 __asm __volatile ("slbmte %0, %1" ::
272 "r"(slb[i].slbv), "r"(slb[i].slbe));
277 mphyp_pte_synch(mmu_t mmu, struct pvo_entry *pvo)
282 __asm __volatile("ptesync");
283 phyp_pft_hcall(H_READ, 0, pvo->pvo_pte.slot, 0, 0, &pte.pte_hi,
285 if ((pte.pte_hi & LPTE_AVPN_MASK) !=
286 ((pvo->pvo_vpn >> (ADDR_API_SHFT64 - ADDR_PIDX_SHFT)) &
289 if (!(pte.pte_hi & LPTE_VALID))
292 return (pte.pte_lo & (LPTE_CHG | LPTE_REF));
296 mphyp_pte_clear(mmu_t mmu, struct pvo_entry *pvo, uint64_t ptebit)
298 struct rm_priotracker track;
300 uint64_t ptelo, junk;
304 * This involves two steps (synch and clear) so we need the entry
305 * not to change in the middle. We are protected against deliberate
306 * unset by virtue of holding the pmap lock. Protection against
307 * incidental unset (page table eviction) comes from holding the
308 * shared eviction lock.
310 PMAP_LOCK_ASSERT(pvo->pvo_pmap, MA_OWNED);
311 rm_rlock(&mphyp_eviction_lock, &track);
313 refchg = mphyp_pte_synch(mmu, pvo);
315 rm_runlock(&mphyp_eviction_lock, &track);
321 * No way to clear either bit, which is total madness.
322 * Pessimistically claim that, once modified, it stays so
323 * forever and that it is never referenced.
325 rm_runlock(&mphyp_eviction_lock, &track);
326 return (refchg & ~LPTE_REF);
329 if (ptebit & LPTE_CHG) {
330 err = phyp_pft_hcall(H_CLEAR_MOD, 0, pvo->pvo_pte.slot, 0, 0,
331 &ptelo, &junk, &junk);
332 KASSERT(err == H_SUCCESS,
333 ("Error clearing page change bit: %d", err));
334 refchg |= (ptelo & LPTE_CHG);
336 if (ptebit & LPTE_REF) {
337 err = phyp_pft_hcall(H_CLEAR_REF, 0, pvo->pvo_pte.slot, 0, 0,
338 &ptelo, &junk, &junk);
339 KASSERT(err == H_SUCCESS,
340 ("Error clearing page reference bit: %d", err));
341 refchg |= (ptelo & LPTE_REF);
344 rm_runlock(&mphyp_eviction_lock, &track);
350 mphyp_pte_unset(mmu_t mmu, struct pvo_entry *pvo)
356 PMAP_LOCK_ASSERT(pvo->pvo_pmap, MA_OWNED);
358 moea64_pte_from_pvo(pvo, &pte);
360 err = phyp_pft_hcall(H_REMOVE, H_AVPN, pvo->pvo_pte.slot,
361 pte.pte_hi & LPTE_AVPN_MASK, 0, &pte.pte_hi, &pte.pte_lo,
363 KASSERT(err == H_SUCCESS || err == H_NOT_FOUND,
364 ("Error removing page: %d", err));
366 if (err == H_NOT_FOUND) {
367 STAT_MOEA64(moea64_pte_overflow--);
371 return (pte.pte_lo & (LPTE_REF | LPTE_CHG));
375 mphyp_pte_spillable_ident(uintptr_t ptegbase, struct lpte *to_evict)
377 uint64_t slot, junk, k;
381 /* Start at a random slot */
384 for (j = 0; j < 8; j++) {
385 slot = ptegbase + (i + j) % 8;
386 phyp_pft_hcall(H_READ, 0, slot, 0, 0, &pt.pte_hi,
389 if (pt.pte_hi & LPTE_WIRED)
392 /* This is a candidate, so remember it */
395 /* Try to get a page that has not been used lately */
396 if (!(pt.pte_hi & LPTE_VALID) || !(pt.pte_lo & LPTE_REF)) {
397 memcpy(to_evict, &pt, sizeof(struct lpte));
405 phyp_pft_hcall(H_READ, 0, k, 0, 0, &to_evict->pte_hi,
406 &to_evict->pte_lo, &junk);
411 mphyp_pte_insert(mmu_t mmu, struct pvo_entry *pvo)
413 struct rm_priotracker track;
415 struct lpte evicted, pte;
416 uint64_t index, junk, lastptelo;
418 PMAP_LOCK_ASSERT(pvo->pvo_pmap, MA_OWNED);
421 moea64_pte_from_pvo(pvo, &pte);
424 /* Make sure further insertion is locked out during evictions */
425 rm_rlock(&mphyp_eviction_lock, &track);
428 * First try primary hash.
430 pvo->pvo_pte.slot &= ~7UL; /* Base slot address */
431 result = phyp_pft_hcall(H_ENTER, 0, pvo->pvo_pte.slot, pte.pte_hi,
432 pte.pte_lo, &index, &evicted.pte_lo, &junk);
433 if (result == H_SUCCESS) {
434 rm_runlock(&mphyp_eviction_lock, &track);
435 pvo->pvo_pte.slot = index;
438 KASSERT(result == H_PTEG_FULL, ("Page insertion error: %ld "
439 "(ptegidx: %#zx/%#lx, PTE %#lx/%#lx", result, pvo->pvo_pte.slot,
440 moea64_pteg_count, pte.pte_hi, pte.pte_lo));
443 * Next try secondary hash.
445 pvo->pvo_vaddr ^= PVO_HID;
446 pte.pte_hi ^= LPTE_HID;
447 pvo->pvo_pte.slot ^= (moea64_pteg_mask << 3);
449 result = phyp_pft_hcall(H_ENTER, 0, pvo->pvo_pte.slot,
450 pte.pte_hi, pte.pte_lo, &index, &evicted.pte_lo, &junk);
451 if (result == H_SUCCESS) {
452 rm_runlock(&mphyp_eviction_lock, &track);
453 pvo->pvo_pte.slot = index;
456 KASSERT(result == H_PTEG_FULL, ("Secondary page insertion error: %ld",
460 * Out of luck. Find a PTE to sacrifice.
463 /* Lock out all insertions for a bit */
464 rm_runlock(&mphyp_eviction_lock, &track);
465 rm_wlock(&mphyp_eviction_lock);
467 index = mphyp_pte_spillable_ident(pvo->pvo_pte.slot, &evicted);
469 /* Try other hash table? */
470 pvo->pvo_vaddr ^= PVO_HID;
471 pte.pte_hi ^= LPTE_HID;
472 pvo->pvo_pte.slot ^= (moea64_pteg_mask << 3);
473 index = mphyp_pte_spillable_ident(pvo->pvo_pte.slot, &evicted);
477 /* No freeable slots in either PTEG? We're hosed. */
478 rm_wunlock(&mphyp_eviction_lock);
479 panic("mphyp_pte_insert: overflow");
483 /* Victim acquired: update page before waving goodbye */
484 if (evicted.pte_hi & LPTE_VALID) {
485 result = phyp_pft_hcall(H_REMOVE, H_AVPN, index,
486 evicted.pte_hi & LPTE_AVPN_MASK, 0, &junk, &lastptelo,
488 STAT_MOEA64(moea64_pte_overflow++);
489 KASSERT(result == H_SUCCESS || result == H_NOT_FOUND,
490 ("Error evicting page: %d", (int)result));
496 result = phyp_pft_hcall(H_ENTER, H_EXACT, index, pte.pte_hi,
497 pte.pte_lo, &index, &evicted.pte_lo, &junk);
498 rm_wunlock(&mphyp_eviction_lock); /* All clear */
500 pvo->pvo_pte.slot = index;
501 if (result == H_SUCCESS)
504 panic("Page replacement error: %ld", result);