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 static struct rmlock mphyp_eviction_lock;
65 * Kernel MMU interface
68 static void mphyp_bootstrap(mmu_t mmup, vm_offset_t kernelstart,
69 vm_offset_t kernelend);
70 static void mphyp_cpu_bootstrap(mmu_t mmup, int ap);
71 static int64_t mphyp_pte_synch(mmu_t, struct pvo_entry *pvo);
72 static int64_t mphyp_pte_clear(mmu_t, struct pvo_entry *pvo, uint64_t ptebit);
73 static int64_t mphyp_pte_unset(mmu_t, struct pvo_entry *pvo);
74 static int mphyp_pte_insert(mmu_t, struct pvo_entry *pvo);
76 static mmu_method_t mphyp_methods[] = {
77 MMUMETHOD(mmu_bootstrap, mphyp_bootstrap),
78 MMUMETHOD(mmu_cpu_bootstrap, mphyp_cpu_bootstrap),
80 MMUMETHOD(moea64_pte_synch, mphyp_pte_synch),
81 MMUMETHOD(moea64_pte_clear, mphyp_pte_clear),
82 MMUMETHOD(moea64_pte_unset, mphyp_pte_unset),
83 MMUMETHOD(moea64_pte_insert, mphyp_pte_insert),
85 /* XXX: pmap_copy_page, pmap_init_page with H_PAGE_INIT */
90 MMU_DEF_INHERIT(pseries_mmu, "mmu_phyp", mphyp_methods, 0, oea64_mmu);
92 static int brokenkvm = 0;
95 print_kvm_bug_warning(void *data)
99 printf("WARNING: Running on a broken hypervisor that does "
100 "not support mandatory H_CLEAR_MOD and H_CLEAR_REF "
101 "hypercalls. Performance will be suboptimal.\n");
104 SYSINIT(kvmbugwarn1, SI_SUB_COPYRIGHT, SI_ORDER_THIRD + 1,
105 print_kvm_bug_warning, NULL);
106 SYSINIT(kvmbugwarn2, SI_SUB_LAST, SI_ORDER_THIRD + 1, print_kvm_bug_warning,
110 mphyp_bootstrap(mmu_t mmup, vm_offset_t kernelstart, vm_offset_t kernelend)
112 uint64_t final_pteg_count = 0;
115 uint32_t nptlp, shift = 0, slb_encoding = 0;
116 uint32_t lp_size, lp_encoding;
119 phandle_t dev, node, root;
122 rm_init(&mphyp_eviction_lock, "pte eviction");
124 moea64_early_bootstrap(mmup, kernelstart, kernelend);
128 dev = OF_child(root);
130 res = OF_getprop(dev, "name", buf, sizeof(buf));
131 if (res > 0 && strcmp(buf, "cpus") == 0)
136 node = OF_child(dev);
139 res = OF_getprop(node, "device_type", buf, sizeof(buf));
140 if (res > 0 && strcmp(buf, "cpu") == 0)
142 node = OF_peer(node);
145 res = OF_getencprop(node, "ibm,pft-size", prop, sizeof(prop));
147 panic("mmu_phyp: unknown PFT size");
148 final_pteg_count = 1 << prop[1];
149 res = OF_getencprop(node, "ibm,slb-size", prop, sizeof(prop[0]));
153 moea64_pteg_count = final_pteg_count / sizeof(struct lpteg);
155 /* Clear any old page table entries */
156 for (idx = 0; idx < moea64_pteg_count*8; idx++) {
157 phyp_pft_hcall(H_READ, 0, idx, 0, 0, &old.pte_hi,
158 &old.pte_lo, &old.pte_lo);
159 vsid = (old.pte_hi << (ADDR_API_SHFT64 - ADDR_PIDX_SHFT)) >> 28;
160 if (vsid == VSID_VRMA || vsid == 0 /* Older VRMA */)
163 if (old.pte_hi & LPTE_VALID)
164 phyp_hcall(H_REMOVE, 0, idx, 0);
168 * Scan the large page size property for PAPR compatible machines.
169 * See PAPR D.5 Changes to Section 5.1.4, 'CPU Node Properties'
170 * for the encoding of the property.
173 len = OF_getproplen(node, "ibm,segment-page-sizes");
176 * We have to use a variable length array on the stack
177 * since we have very limited stack space.
179 pcell_t arr[len/sizeof(cell_t)];
180 res = OF_getencprop(node, "ibm,segment-page-sizes", arr,
186 slb_encoding = arr[idx + 1];
187 nptlp = arr[idx + 2];
190 while (len > 0 && nptlp) {
192 lp_encoding = arr[idx+1];
193 if (slb_encoding == SLBV_L && lp_encoding == 0)
200 if (nptlp && slb_encoding == SLBV_L && lp_encoding == 0)
205 panic("Standard large pages (SLB[L] = 1, PTE[LP] = 0) "
206 "not supported by this system. Please enable huge "
207 "page backing if running under PowerKVM.");
209 moea64_large_page_shift = shift;
210 moea64_large_page_size = 1ULL << lp_size;
213 moea64_mid_bootstrap(mmup, kernelstart, kernelend);
214 moea64_late_bootstrap(mmup, kernelstart, kernelend);
216 /* Test for broken versions of KVM that don't conform to the spec */
217 if (phyp_hcall(H_CLEAR_MOD, 0, 0) == H_FUNCTION)
222 mphyp_cpu_bootstrap(mmu_t mmup, int ap)
224 struct slb *slb = PCPU_GET(aim.slb);
229 * Install kernel SLB entries
232 __asm __volatile ("slbia");
233 __asm __volatile ("slbmfee %0,%1; slbie %0;" : "=r"(seg0) : "r"(0));
234 for (i = 0; i < 64; i++) {
235 if (!(slb[i].slbe & SLBE_VALID))
238 __asm __volatile ("slbmte %0, %1" ::
239 "r"(slb[i].slbv), "r"(slb[i].slbe));
244 mphyp_pte_synch(mmu_t mmu, struct pvo_entry *pvo)
249 __asm __volatile("ptesync");
250 phyp_pft_hcall(H_READ, 0, pvo->pvo_pte.slot, 0, 0, &pte.pte_hi,
252 if ((pte.pte_hi & LPTE_AVPN_MASK) !=
253 ((pvo->pvo_vpn >> (ADDR_API_SHFT64 - ADDR_PIDX_SHFT)) &
256 if (!(pte.pte_hi & LPTE_VALID))
259 return (pte.pte_lo & (LPTE_CHG | LPTE_REF));
263 mphyp_pte_clear(mmu_t mmu, struct pvo_entry *pvo, uint64_t ptebit)
265 struct rm_priotracker track;
267 uint64_t ptelo, junk;
271 * This involves two steps (synch and clear) so we need the entry
272 * not to change in the middle. We are protected against deliberate
273 * unset by virtue of holding the pmap lock. Protection against
274 * incidental unset (page table eviction) comes from holding the
275 * shared eviction lock.
277 PMAP_LOCK_ASSERT(pvo->pvo_pmap, MA_OWNED);
278 rm_rlock(&mphyp_eviction_lock, &track);
280 refchg = mphyp_pte_synch(mmu, pvo);
282 rm_runlock(&mphyp_eviction_lock, &track);
288 * No way to clear either bit, which is total madness.
289 * Pessimistically claim that, once modified, it stays so
290 * forever and that it is never referenced.
292 rm_runlock(&mphyp_eviction_lock, &track);
293 return (refchg & ~LPTE_REF);
296 if (ptebit & LPTE_CHG) {
297 err = phyp_pft_hcall(H_CLEAR_MOD, 0, pvo->pvo_pte.slot, 0, 0,
298 &ptelo, &junk, &junk);
299 KASSERT(err == H_SUCCESS,
300 ("Error clearing page change bit: %d", err));
301 refchg |= (ptelo & LPTE_CHG);
303 if (ptebit & LPTE_REF) {
304 err = phyp_pft_hcall(H_CLEAR_REF, 0, pvo->pvo_pte.slot, 0, 0,
305 &ptelo, &junk, &junk);
306 KASSERT(err == H_SUCCESS,
307 ("Error clearing page reference bit: %d", err));
308 refchg |= (ptelo & LPTE_REF);
311 rm_runlock(&mphyp_eviction_lock, &track);
317 mphyp_pte_unset(mmu_t mmu, struct pvo_entry *pvo)
323 PMAP_LOCK_ASSERT(pvo->pvo_pmap, MA_OWNED);
325 moea64_pte_from_pvo(pvo, &pte);
327 err = phyp_pft_hcall(H_REMOVE, H_AVPN, pvo->pvo_pte.slot,
328 pte.pte_hi & LPTE_AVPN_MASK, 0, &pte.pte_hi, &pte.pte_lo,
330 KASSERT(err == H_SUCCESS || err == H_NOT_FOUND,
331 ("Error removing page: %d", err));
333 if (err == H_NOT_FOUND) {
334 moea64_pte_overflow--;
338 return (pte.pte_lo & (LPTE_REF | LPTE_CHG));
342 mphyp_pte_spillable_ident(uintptr_t ptegbase, struct lpte *to_evict)
344 uint64_t slot, junk, k;
348 /* Start at a random slot */
351 for (j = 0; j < 8; j++) {
352 slot = ptegbase + (i + j) % 8;
353 phyp_pft_hcall(H_READ, 0, slot, 0, 0, &pt.pte_hi,
356 if (pt.pte_hi & LPTE_WIRED)
359 /* This is a candidate, so remember it */
362 /* Try to get a page that has not been used lately */
363 if (!(pt.pte_hi & LPTE_VALID) || !(pt.pte_lo & LPTE_REF)) {
364 memcpy(to_evict, &pt, sizeof(struct lpte));
372 phyp_pft_hcall(H_READ, 0, k, 0, 0, &to_evict->pte_hi,
373 &to_evict->pte_lo, &junk);
378 mphyp_pte_insert(mmu_t mmu, struct pvo_entry *pvo)
380 struct rm_priotracker track;
382 struct lpte evicted, pte;
383 uint64_t index, junk, lastptelo;
385 PMAP_LOCK_ASSERT(pvo->pvo_pmap, MA_OWNED);
388 moea64_pte_from_pvo(pvo, &pte);
391 /* Make sure further insertion is locked out during evictions */
392 rm_rlock(&mphyp_eviction_lock, &track);
395 * First try primary hash.
397 pvo->pvo_pte.slot &= ~7UL; /* Base slot address */
398 result = phyp_pft_hcall(H_ENTER, 0, pvo->pvo_pte.slot, pte.pte_hi,
399 pte.pte_lo, &index, &evicted.pte_lo, &junk);
400 if (result == H_SUCCESS) {
401 rm_runlock(&mphyp_eviction_lock, &track);
402 pvo->pvo_pte.slot = index;
405 KASSERT(result == H_PTEG_FULL, ("Page insertion error: %ld "
406 "(ptegidx: %#zx/%#x, PTE %#lx/%#lx", result, pvo->pvo_pte.slot,
407 moea64_pteg_count, pte.pte_hi, pte.pte_lo));
410 * Next try secondary hash.
412 pvo->pvo_vaddr ^= PVO_HID;
413 pte.pte_hi ^= LPTE_HID;
414 pvo->pvo_pte.slot ^= (moea64_pteg_mask << 3);
416 result = phyp_pft_hcall(H_ENTER, 0, pvo->pvo_pte.slot,
417 pte.pte_hi, pte.pte_lo, &index, &evicted.pte_lo, &junk);
418 if (result == H_SUCCESS) {
419 rm_runlock(&mphyp_eviction_lock, &track);
420 pvo->pvo_pte.slot = index;
423 KASSERT(result == H_PTEG_FULL, ("Secondary page insertion error: %ld",
427 * Out of luck. Find a PTE to sacrifice.
430 /* Lock out all insertions for a bit */
431 rm_runlock(&mphyp_eviction_lock, &track);
432 rm_wlock(&mphyp_eviction_lock);
434 index = mphyp_pte_spillable_ident(pvo->pvo_pte.slot, &evicted);
436 /* Try other hash table? */
437 pvo->pvo_vaddr ^= PVO_HID;
438 pte.pte_hi ^= LPTE_HID;
439 pvo->pvo_pte.slot ^= (moea64_pteg_mask << 3);
440 index = mphyp_pte_spillable_ident(pvo->pvo_pte.slot, &evicted);
444 /* No freeable slots in either PTEG? We're hosed. */
445 rm_wunlock(&mphyp_eviction_lock);
446 panic("mphyp_pte_insert: overflow");
450 /* Victim acquired: update page before waving goodbye */
451 if (evicted.pte_hi & LPTE_VALID) {
452 result = phyp_pft_hcall(H_REMOVE, H_AVPN, index,
453 evicted.pte_hi & LPTE_AVPN_MASK, 0, &junk, &lastptelo,
455 moea64_pte_overflow++;
456 KASSERT(result == H_SUCCESS,
457 ("Error evicting page: %d", (int)result));
463 result = phyp_pft_hcall(H_ENTER, H_EXACT, index, pte.pte_hi,
464 pte.pte_lo, &index, &evicted.pte_lo, &junk);
465 rm_wunlock(&mphyp_eviction_lock); /* All clear */
467 pvo->pvo_pte.slot = index;
468 if (result == H_SUCCESS)
471 panic("Page replacement error: %ld", result);