2 * Copyright (C) 2010 Andreas Tobler
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
30 #include <sys/kernel.h>
33 #include <sys/msgbuf.h>
34 #include <sys/mutex.h>
36 #include <sys/sysctl.h>
37 #include <sys/systm.h>
38 #include <sys/vmmeter.h>
40 #include <dev/ofw/openfirm.h>
41 #include <machine/ofw_machdep.h>
44 #include <vm/vm_param.h>
45 #include <vm/vm_kern.h>
46 #include <vm/vm_page.h>
47 #include <vm/vm_map.h>
48 #include <vm/vm_object.h>
49 #include <vm/vm_extern.h>
50 #include <vm/vm_pageout.h>
53 #include <powerpc/aim/mmu_oea64.h>
56 #include "moea64_if.h"
58 #include "phyp-hvcall.h"
63 * Kernel MMU interface
66 static void mphyp_bootstrap(mmu_t mmup, vm_offset_t kernelstart,
67 vm_offset_t kernelend);
68 static void mphyp_cpu_bootstrap(mmu_t mmup, int ap);
69 static void mphyp_pte_synch(mmu_t, uintptr_t pt, struct lpte *pvo_pt);
70 static void mphyp_pte_clear(mmu_t, uintptr_t pt, struct lpte *pvo_pt,
71 uint64_t vpn, u_int64_t ptebit);
72 static void mphyp_pte_unset(mmu_t, uintptr_t pt, struct lpte *pvo_pt,
74 static void mphyp_pte_change(mmu_t, uintptr_t pt, struct lpte *pvo_pt,
76 static int mphyp_pte_insert(mmu_t, u_int ptegidx, struct lpte *pvo_pt);
77 static uintptr_t mphyp_pvo_to_pte(mmu_t, const struct pvo_entry *pvo);
79 #define VSID_HASH_MASK 0x0000007fffffffffULL
82 static mmu_method_t mphyp_methods[] = {
83 MMUMETHOD(mmu_bootstrap, mphyp_bootstrap),
84 MMUMETHOD(mmu_cpu_bootstrap, mphyp_cpu_bootstrap),
86 MMUMETHOD(moea64_pte_synch, mphyp_pte_synch),
87 MMUMETHOD(moea64_pte_clear, mphyp_pte_clear),
88 MMUMETHOD(moea64_pte_unset, mphyp_pte_unset),
89 MMUMETHOD(moea64_pte_change, mphyp_pte_change),
90 MMUMETHOD(moea64_pte_insert, mphyp_pte_insert),
91 MMUMETHOD(moea64_pvo_to_pte, mphyp_pvo_to_pte),
96 MMU_DEF_INHERIT(pseries_mmu, "mmu_phyp", mphyp_methods, 0, oea64_mmu);
99 mphyp_bootstrap(mmu_t mmup, vm_offset_t kernelstart, vm_offset_t kernelend)
101 uint64_t final_pteg_count = 0;
104 uint32_t nptlp, shift = 0, slb_encoding = 0;
105 phandle_t dev, node, root;
108 moea64_early_bootstrap(mmup, kernelstart, kernelend);
112 dev = OF_child(root);
114 res = OF_getprop(dev, "name", buf, sizeof(buf));
115 if (res > 0 && strcmp(buf, "cpus") == 0)
120 node = OF_child(dev);
123 res = OF_getprop(node, "device_type", buf, sizeof(buf));
124 if (res > 0 && strcmp(buf, "cpu") == 0)
126 node = OF_peer(node);
129 res = OF_getprop(node, "ibm,pft-size", prop, sizeof(prop));
131 panic("mmu_phyp: unknown PFT size");
132 final_pteg_count = 1 << prop[1];
133 res = OF_getprop(node, "ibm,slb-size", prop, sizeof(prop[0]));
137 moea64_pteg_count = final_pteg_count / sizeof(struct lpteg);
140 * Scan the large page size property for PAPR compatible machines.
141 * See PAPR D.5 Changes to Section 5.1.4, 'CPU Node Properties'
142 * for the encoding of the property.
145 len = OF_getproplen(node, "ibm,segment-page-sizes");
148 * We have to use a variable length array on the stack
149 * since we have very limited stack space.
151 cell_t arr[len/sizeof(cell_t)];
152 res = OF_getprop(node, "ibm,segment-page-sizes", &arr,
158 slb_encoding = arr[idx + 1];
159 nptlp = arr[idx + 2];
162 while (len > 0 && nptlp) {
168 moea64_large_page_shift = shift;
169 moea64_large_page_size = 1 << shift;
172 moea64_mid_bootstrap(mmup, kernelstart, kernelend);
173 moea64_late_bootstrap(mmup, kernelstart, kernelend);
177 mphyp_cpu_bootstrap(mmu_t mmup, int ap)
179 struct slb *slb = PCPU_GET(slb);
184 * Install kernel SLB entries
187 __asm __volatile ("slbia");
188 __asm __volatile ("slbmfee %0,%1; slbie %0;" : "=r"(seg0) : "r"(0));
189 for (i = 0; i < 64; i++) {
190 if (!(slb[i].slbe & SLBE_VALID))
193 __asm __volatile ("slbmte %0, %1" ::
194 "r"(slb[i].slbv), "r"(slb[i].slbe));
199 mphyp_pte_synch(mmu_t mmu, uintptr_t slot, struct lpte *pvo_pt)
204 __asm __volatile("ptesync");
205 phyp_pft_hcall(H_READ, 0, slot, 0, 0, &pte.pte_hi, &pte.pte_lo,
208 pvo_pt->pte_lo |= pte.pte_lo & (LPTE_CHG | LPTE_REF);
212 mphyp_pte_clear(mmu_t mmu, uintptr_t slot, struct lpte *pvo_pt, uint64_t vpn,
216 if (ptebit & LPTE_CHG)
217 phyp_hcall(H_CLEAR_MOD, 0, slot);
218 if (ptebit & LPTE_REF)
219 phyp_hcall(H_CLEAR_REF, 0, slot);
223 mphyp_pte_unset(mmu_t mmu, uintptr_t slot, struct lpte *pvo_pt, uint64_t vpn)
229 err = phyp_pft_hcall(H_REMOVE, 1UL << 31, slot,
230 pvo_pt->pte_hi & LPTE_AVPN_MASK, 0, &pte.pte_hi, &pte.pte_lo,
232 KASSERT(err == H_SUCCESS, ("Error removing page: %d", err));
234 pvo_pt->pte_lo |= pte.pte_lo & (LPTE_CHG | LPTE_REF);
238 mphyp_pte_change(mmu_t mmu, uintptr_t slot, struct lpte *pvo_pt, uint64_t vpn)
241 uint64_t index, junk;
245 * NB: this is protected by the global table lock, so this two-step
246 * is safe, except for the scratch-page case. No CPUs on which we run
247 * this code should be using scratch pages.
249 KASSERT(!(pvo_pt->pte_hi & LPTE_LOCKED),
250 ("Locked pages not supported on PHYP"));
252 /* XXX: optimization using H_PROTECT for common case? */
253 mphyp_pte_unset(mmu, slot, pvo_pt, vpn);
254 result = phyp_pft_hcall(H_ENTER, H_EXACT, slot, pvo_pt->pte_hi,
255 pvo_pt->pte_lo, &index, &evicted.pte_lo, &junk);
256 if (result != H_SUCCESS)
257 panic("mphyp_pte_change() insertion failure: %ld\n", result);
261 mphyp_pte_spillable_ident(u_int ptegidx, struct lpte *to_evict)
263 uint64_t slot, junk, k;
267 /* Start at a random slot */
270 for (j = 0; j < 8; j++) {
271 slot = (ptegidx << 3) + (i + j) % 8;
272 phyp_pft_hcall(H_READ, 0, slot, 0, 0, &pt.pte_hi, &pt.pte_lo,
275 if (pt.pte_hi & LPTE_SWBITS)
278 /* This is a candidate, so remember it */
281 /* Try to get a page that has not been used lately */
282 if (!(pt.pte_lo & LPTE_REF)) {
283 memcpy(to_evict, &pt, sizeof(struct lpte));
288 phyp_pft_hcall(H_READ, 0, slot, 0, 0, &to_evict->pte_hi,
289 &to_evict->pte_lo, &junk);
294 mphyp_pte_insert(mmu_t mmu, u_int ptegidx, struct lpte *pvo_pt)
298 struct pvo_entry *pvo;
299 uint64_t index, junk;
302 /* Check for locked pages, which we can't support on this system */
303 KASSERT(!(pvo_pt->pte_hi & LPTE_LOCKED),
304 ("Locked pages not supported on PHYP"));
307 pvo_pt->pte_hi |= LPTE_VALID;
308 pvo_pt->pte_hi &= ~LPTE_HID;
312 * First try primary hash.
314 pteg_bktidx = ptegidx;
315 result = phyp_pft_hcall(H_ENTER, 0, pteg_bktidx << 3, pvo_pt->pte_hi,
316 pvo_pt->pte_lo, &index, &evicted.pte_lo, &junk);
317 if (result == H_SUCCESS)
318 return (index & 0x07);
319 KASSERT(result == H_PTEG_FULL, ("Page insertion error: %ld "
320 "(ptegidx: %#x/%#x, PTE %#lx/%#lx", result, ptegidx,
321 moea64_pteg_count, pvo_pt->pte_hi, pvo_pt->pte_lo));
324 * Next try secondary hash.
326 pteg_bktidx ^= moea64_pteg_mask;
327 pvo_pt->pte_hi |= LPTE_HID;
328 result = phyp_pft_hcall(H_ENTER, 0, pteg_bktidx << 3,
329 pvo_pt->pte_hi, pvo_pt->pte_lo, &index, &evicted.pte_lo, &junk);
330 if (result == H_SUCCESS)
331 return (index & 0x07);
332 KASSERT(result == H_PTEG_FULL, ("Secondary page insertion error: %ld",
336 * Out of luck. Find a PTE to sacrifice.
338 pteg_bktidx = ptegidx;
339 index = mphyp_pte_spillable_ident(pteg_bktidx, &evicted);
341 pteg_bktidx ^= moea64_pteg_mask;
342 index = mphyp_pte_spillable_ident(pteg_bktidx, &evicted);
346 /* No freeable slots in either PTEG? We're hosed. */
347 panic("mphyp_pte_insert: overflow");
351 if (pteg_bktidx == ptegidx)
352 pvo_pt->pte_hi &= ~LPTE_HID;
354 pvo_pt->pte_hi |= LPTE_HID;
357 * Synchronize the sacrifice PTE with its PVO, then mark both
358 * invalid. The PVO will be reused when/if the VM system comes
359 * here after a fault.
362 if (evicted.pte_hi & LPTE_HID)
363 pteg_bktidx ^= moea64_pteg_mask; /* PTEs indexed by primary */
365 LIST_FOREACH(pvo, &moea64_pvo_table[pteg_bktidx], pvo_olink) {
366 if (pvo->pvo_pte.lpte.pte_hi == evicted.pte_hi) {
367 KASSERT(pvo->pvo_pte.lpte.pte_hi & LPTE_VALID,
368 ("Invalid PVO for valid PTE!"));
369 mphyp_pte_unset(mmu, index, &pvo->pvo_pte.lpte,
371 PVO_PTEGIDX_CLR(pvo);
372 moea64_pte_overflow++;
377 KASSERT(pvo->pvo_pte.lpte.pte_hi == evicted.pte_hi,
378 ("Unable to find PVO for spilled PTE"));
383 result = phyp_pft_hcall(H_ENTER, H_EXACT, index, pvo_pt->pte_hi,
384 pvo_pt->pte_lo, &index, &evicted.pte_lo, &junk);
385 if (result == H_SUCCESS)
386 return (index & 0x07);
388 panic("Page replacement error: %ld", result);
392 static __inline u_int
393 va_to_pteg(uint64_t vsid, vm_offset_t addr, int large)
398 shift = large ? moea64_large_page_shift : ADDR_PIDX_SHFT;
399 hash = (vsid & VSID_HASH_MASK) ^ (((uint64_t)addr & ADDR_PIDX) >>
401 return (hash & moea64_pteg_mask);
405 mphyp_pvo_to_pte(mmu_t mmu, const struct pvo_entry *pvo)
410 /* If the PTEG index is not set, then there is no page table entry */
411 if (!PVO_PTEGIDX_ISSET(pvo))
414 vsid = PVO_VSID(pvo);
415 ptegidx = va_to_pteg(vsid, PVO_VADDR(pvo), pvo->pvo_vaddr & PVO_LARGE);
418 * We can find the actual pte entry without searching by grabbing
419 * the PTEG index from 3 unused bits in pvo_vaddr and by
420 * noticing the HID bit.
422 if (pvo->pvo_pte.lpte.pte_hi & LPTE_HID)
423 ptegidx ^= moea64_pteg_mask;
425 return ((ptegidx << 3) | PVO_PTEGIDX_GET(pvo));