2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD AND BSD-4-Clause
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matt Thomas <matt@3am-software.com> of Allegro Networks, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
33 * Copyright (C) 1995, 1996 TooLs GmbH.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by TooLs GmbH.
47 * 4. The name of TooLs GmbH may not be used to endorse or promote products
48 * derived from this software without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
51 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
54 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
55 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
56 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
57 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
58 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
59 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 * $NetBSD: pmap.c,v 1.28 2000/03/26 20:42:36 kleink Exp $
64 * Copyright (C) 2001 Benno Rice.
65 * All rights reserved.
67 * Redistribution and use in source and binary forms, with or without
68 * modification, are permitted provided that the following conditions
70 * 1. Redistributions of source code must retain the above copyright
71 * notice, this list of conditions and the following disclaimer.
72 * 2. Redistributions in binary form must reproduce the above copyright
73 * notice, this list of conditions and the following disclaimer in the
74 * documentation and/or other materials provided with the distribution.
76 * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
77 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
78 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
79 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
80 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
81 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
82 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
83 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
84 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
85 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
88 #include <sys/cdefs.h>
89 __FBSDID("$FreeBSD$");
92 * Native 64-bit page table operations for running without a hypervisor.
95 #include <sys/param.h>
96 #include <sys/kernel.h>
99 #include <sys/mutex.h>
100 #include <sys/proc.h>
101 #include <sys/sched.h>
102 #include <sys/sysctl.h>
103 #include <sys/systm.h>
104 #include <sys/rwlock.h>
105 #include <sys/endian.h>
110 #include <vm/vm_param.h>
111 #include <vm/vm_kern.h>
112 #include <vm/vm_page.h>
113 #include <vm/vm_map.h>
114 #include <vm/vm_object.h>
115 #include <vm/vm_extern.h>
116 #include <vm/vm_pageout.h>
118 #include <machine/cpu.h>
119 #include <machine/hid.h>
120 #include <machine/md_var.h>
121 #include <machine/mmuvar.h>
123 #include "mmu_oea64.h"
125 #define PTESYNC() __asm __volatile("ptesync");
126 #define TLBSYNC() __asm __volatile("tlbsync; ptesync");
127 #define SYNC() __asm __volatile("sync");
128 #define EIEIO() __asm __volatile("eieio");
130 #define VSID_HASH_MASK 0x0000007fffffffffULL
132 /* POWER9 only permits a 64k partition table size. */
133 #define PART_SIZE 0x10000
135 /* Actual page sizes (to be used with tlbie, when L=0) */
139 #define LPTE_KERNEL_VSID_BIT (KERNEL_VSID_BIT << \
140 (16 - (ADDR_API_SHFT64 - ADDR_PIDX_SHFT)))
142 /* Abbreviated Virtual Address Page - high bits */
143 #define LPTE_AVA_PGNHI_MASK 0x0000000000000F80ULL
144 #define LPTE_AVA_PGNHI_SHIFT 7
146 /* Effective Address Page - low bits */
147 #define EA_PAGELO_MASK 0x7ffULL
148 #define EA_PAGELO_SHIFT 11
150 static bool moea64_crop_tlbie;
151 static bool moea64_need_lock;
154 * The tlbie instruction has two forms: an old one used by PowerISA
155 * 2.03 and prior, and a newer one used by PowerISA 2.06 and later.
156 * We need to support both.
159 TLBIE(uint64_t vpn, uint64_t oldptehi)
161 #ifndef __powerpc64__
162 register_t vpn_hi, vpn_lo;
164 register_t scratch, intr;
167 static volatile u_int tlbie_lock = 0;
168 bool need_lock = moea64_need_lock;
170 vpn <<= ADDR_PIDX_SHFT;
172 /* Hobo spinlock: we need stronger guarantees than mutexes provide */
174 while (!atomic_cmpset_int(&tlbie_lock, 0, 1));
175 isync(); /* Flush instruction queue once lock acquired */
177 if (moea64_crop_tlbie) {
178 vpn &= ~(0xffffULL << 48);
180 if ((oldptehi & LPTE_BIG) != 0)
181 __asm __volatile("tlbie %0, 1" :: "r"(vpn) :
184 __asm __volatile("tlbie %0, 0" :: "r"(vpn) :
186 __asm __volatile("eieio; tlbsync; ptesync" :::
195 * If this page has LPTE_BIG set and is from userspace, then
196 * it must be a superpage with 4KB base/16MB actual page size.
198 if ((oldptehi & LPTE_BIG) != 0 &&
199 (oldptehi & LPTE_KERNEL_VSID_BIT) == 0)
203 * Explicitly clobber r0. The tlbie instruction has two forms: an old
204 * one used by PowerISA 2.03 and prior, and a newer one used by PowerISA
205 * 2.06 (maybe 2.05?) and later. We need to support both, and it just
206 * so happens that since we use 4k pages we can simply zero out r0, and
207 * clobber it, and the assembler will interpret the single-operand form
208 * of tlbie as having RB set, and everything else as 0. The RS operand
209 * in the newer form is in the same position as the L(page size) bit of
210 * the old form, so a slong as RS is 0, we're good on both sides.
212 __asm __volatile("li 0, 0 \n tlbie %0, 0" :: "r"(vpn) : "r0", "memory");
213 __asm __volatile("eieio; tlbsync; ptesync" ::: "memory");
217 vpn_hi = (uint32_t)(vpn >> 32);
218 vpn_lo = (uint32_t)vpn;
220 intr = intr_disable();
235 : "=r"(msr), "=r"(scratch) : "r"(vpn_hi), "r"(vpn_lo), "r"(32), "r"(1)
240 /* No barriers or special ops -- taken care of by ptesync above */
245 #define DISABLE_TRANS(msr) msr = mfmsr(); mtmsr(msr & ~PSL_DR)
246 #define ENABLE_TRANS(msr) mtmsr(msr)
251 static volatile struct lpte *moea64_pteg_table;
252 static struct rwlock moea64_eviction_lock;
254 static volatile struct pate *moea64_part_table;
259 static void *moea64_dump_pmap_native(void *ctx, void *buf,
265 static int64_t moea64_pte_insert_native(struct pvo_entry *);
266 static int64_t moea64_pte_synch_native(struct pvo_entry *);
267 static int64_t moea64_pte_clear_native(struct pvo_entry *, uint64_t);
268 static int64_t moea64_pte_replace_native(struct pvo_entry *, int);
269 static int64_t moea64_pte_unset_native(struct pvo_entry *);
270 static int64_t moea64_pte_insert_sp_native(struct pvo_entry *);
271 static int64_t moea64_pte_unset_sp_native(struct pvo_entry *);
272 static int64_t moea64_pte_replace_sp_native(struct pvo_entry *);
277 static void moea64_bootstrap_native(
278 vm_offset_t kernelstart, vm_offset_t kernelend);
279 static void moea64_cpu_bootstrap_native(int ap);
280 static void tlbia(void);
281 static void moea64_install_native(void);
283 static struct pmap_funcs moea64_native_methods = {
284 .install = moea64_install_native,
286 /* Internal interfaces */
287 .bootstrap = moea64_bootstrap_native,
288 .cpu_bootstrap = moea64_cpu_bootstrap_native,
289 .dumpsys_dump_pmap = moea64_dump_pmap_native,
292 static struct moea64_funcs moea64_native_funcs = {
293 .pte_synch = moea64_pte_synch_native,
294 .pte_clear = moea64_pte_clear_native,
295 .pte_unset = moea64_pte_unset_native,
296 .pte_replace = moea64_pte_replace_native,
297 .pte_insert = moea64_pte_insert_native,
298 .pte_insert_sp = moea64_pte_insert_sp_native,
299 .pte_unset_sp = moea64_pte_unset_sp_native,
300 .pte_replace_sp = moea64_pte_replace_sp_native,
303 MMU_DEF_INHERIT(oea64_mmu_native, MMU_TYPE_G5, moea64_native_methods, oea64_mmu);
306 moea64_install_native(void)
309 /* Install the MOEA64 ops. */
310 moea64_ops = &moea64_native_funcs;
316 moea64_pte_synch_native(struct pvo_entry *pvo)
318 volatile struct lpte *pt = moea64_pteg_table + pvo->pvo_pte.slot;
319 uint64_t ptelo, pvo_ptevpn;
321 PMAP_LOCK_ASSERT(pvo->pvo_pmap, MA_OWNED);
323 pvo_ptevpn = moea64_pte_vpn_from_pvo_vpn(pvo);
325 rw_rlock(&moea64_eviction_lock);
326 if ((be64toh(pt->pte_hi) & LPTE_AVPN_MASK) != pvo_ptevpn) {
328 rw_runlock(&moea64_eviction_lock);
333 ptelo = be64toh(pt->pte_lo);
335 rw_runlock(&moea64_eviction_lock);
337 return (ptelo & (LPTE_REF | LPTE_CHG));
341 moea64_pte_clear_native(struct pvo_entry *pvo, uint64_t ptebit)
343 volatile struct lpte *pt = moea64_pteg_table + pvo->pvo_pte.slot;
344 struct lpte properpt;
347 PMAP_LOCK_ASSERT(pvo->pvo_pmap, MA_OWNED);
349 moea64_pte_from_pvo(pvo, &properpt);
351 rw_rlock(&moea64_eviction_lock);
352 if ((be64toh(pt->pte_hi) & LPTE_AVPN_MASK) !=
353 (properpt.pte_hi & LPTE_AVPN_MASK)) {
355 rw_runlock(&moea64_eviction_lock);
359 if (ptebit == LPTE_REF) {
360 /* See "Resetting the Reference Bit" in arch manual */
362 /* 2-step here safe: precision is not guaranteed */
363 ptelo = be64toh(pt->pte_lo);
365 /* One-byte store to avoid touching the C bit */
366 ((volatile uint8_t *)(&pt->pte_lo))[6] =
367 #if BYTE_ORDER == BIG_ENDIAN
368 ((uint8_t *)(&properpt.pte_lo))[6];
370 ((uint8_t *)(&properpt.pte_lo))[1];
372 rw_runlock(&moea64_eviction_lock);
375 TLBIE(pvo->pvo_vpn, properpt.pte_hi);
378 rw_runlock(&moea64_eviction_lock);
379 ptelo = moea64_pte_unset_native(pvo);
380 moea64_pte_insert_native(pvo);
383 return (ptelo & (LPTE_REF | LPTE_CHG));
386 static __always_inline int64_t
387 moea64_pte_unset_locked(volatile struct lpte *pt, uint64_t vpn)
389 uint64_t ptelo, ptehi;
392 * Invalidate the pte, briefly locking it to collect RC bits. No
393 * atomics needed since this is protected against eviction by the lock.
397 ptehi = (be64toh(pt->pte_hi) & ~LPTE_VALID) | LPTE_LOCKED;
398 pt->pte_hi = htobe64(ptehi);
401 ptelo = be64toh(pt->pte_lo);
402 *((volatile int32_t *)(&pt->pte_hi) + 1) = 0; /* Release lock */
405 /* Keep statistics */
406 STAT_MOEA64(moea64_pte_valid--);
408 return (ptelo & (LPTE_CHG | LPTE_REF));
412 moea64_pte_unset_native(struct pvo_entry *pvo)
414 volatile struct lpte *pt = moea64_pteg_table + pvo->pvo_pte.slot;
418 pvo_ptevpn = moea64_pte_vpn_from_pvo_vpn(pvo);
420 rw_rlock(&moea64_eviction_lock);
422 if ((be64toh(pt->pte_hi) & LPTE_AVPN_MASK) != pvo_ptevpn) {
424 STAT_MOEA64(moea64_pte_overflow--);
427 ret = moea64_pte_unset_locked(pt, pvo->pvo_vpn);
429 rw_runlock(&moea64_eviction_lock);
435 moea64_pte_replace_inval_native(struct pvo_entry *pvo,
436 volatile struct lpte *pt)
438 struct lpte properpt;
439 uint64_t ptelo, ptehi;
441 moea64_pte_from_pvo(pvo, &properpt);
443 rw_rlock(&moea64_eviction_lock);
444 if ((be64toh(pt->pte_hi) & LPTE_AVPN_MASK) !=
445 (properpt.pte_hi & LPTE_AVPN_MASK)) {
447 STAT_MOEA64(moea64_pte_overflow--);
448 rw_runlock(&moea64_eviction_lock);
453 * Replace the pte, briefly locking it to collect RC bits. No
454 * atomics needed since this is protected against eviction by the lock.
458 ptehi = (be64toh(pt->pte_hi) & ~LPTE_VALID) | LPTE_LOCKED;
459 pt->pte_hi = htobe64(ptehi);
461 TLBIE(pvo->pvo_vpn, ptehi);
462 ptelo = be64toh(pt->pte_lo);
464 pt->pte_lo = htobe64(properpt.pte_lo);
466 pt->pte_hi = htobe64(properpt.pte_hi); /* Release lock */
469 rw_runlock(&moea64_eviction_lock);
471 return (ptelo & (LPTE_CHG | LPTE_REF));
475 moea64_pte_replace_native(struct pvo_entry *pvo, int flags)
477 volatile struct lpte *pt = moea64_pteg_table + pvo->pvo_pte.slot;
478 struct lpte properpt;
482 /* Just some software bits changing. */
483 moea64_pte_from_pvo(pvo, &properpt);
485 rw_rlock(&moea64_eviction_lock);
486 if ((be64toh(pt->pte_hi) & LPTE_AVPN_MASK) !=
487 (properpt.pte_hi & LPTE_AVPN_MASK)) {
488 rw_runlock(&moea64_eviction_lock);
491 pt->pte_hi = htobe64(properpt.pte_hi);
492 ptelo = be64toh(pt->pte_lo);
493 rw_runlock(&moea64_eviction_lock);
495 /* Otherwise, need reinsertion and deletion */
496 ptelo = moea64_pte_replace_inval_native(pvo, pt);
503 moea64_cpu_bootstrap_native(int ap)
507 struct slb *slb = PCPU_GET(aim.slb);
512 * Initialize segment registers and MMU
515 mtmsr(mfmsr() & ~PSL_DR & ~PSL_IR);
517 switch(mfpvr() >> 16) {
519 mtspr(SPR_HID0, mfspr(SPR_HID0) & ~HID0_RADIX);
524 * Install kernel SLB entries
528 __asm __volatile ("slbia");
529 __asm __volatile ("slbmfee %0,%1; slbie %0;" : "=r"(seg0) :
532 for (i = 0; i < n_slbs; i++) {
533 if (!(slb[i].slbe & SLBE_VALID))
536 __asm __volatile ("slbmte %0, %1" ::
537 "r"(slb[i].slbv), "r"(slb[i].slbe));
540 for (i = 0; i < 16; i++)
541 mtsrin(i << ADDR_SR_SHFT, kernel_pmap->pm_sr[i]);
548 if (cpu_features2 & PPC_FEATURE2_ARCH_3_00)
550 ((uintptr_t)moea64_part_table & ~DMAP_BASE_ADDRESS) |
551 flsl((PART_SIZE >> 12) - 1));
553 __asm __volatile ("ptesync; mtsdr1 %0; isync"
554 :: "r"(((uintptr_t)moea64_pteg_table & ~DMAP_BASE_ADDRESS)
555 | (uintptr_t)(flsl(moea64_pteg_mask >> 11))));
560 moea64_bootstrap_native(vm_offset_t kernelstart, vm_offset_t kernelend)
567 moea64_early_bootstrap(kernelstart, kernelend);
569 switch (mfpvr() >> 16) {
571 moea64_need_lock = false;
579 moea64_crop_tlbie = true;
581 moea64_need_lock = true;
584 * Allocate PTEG table.
587 size = moea64_pteg_count * sizeof(struct lpteg);
588 CTR2(KTR_PMAP, "moea64_bootstrap: %lu PTEGs, %lu bytes",
589 moea64_pteg_count, size);
590 rw_init(&moea64_eviction_lock, "pte eviction");
593 * We now need to allocate memory. This memory, to be allocated,
594 * has to reside in a page table. The page table we are about to
595 * allocate. We don't have BAT. So drop to data real mode for a minute
596 * as a measure of last resort. We do this a couple times.
599 * PTEG table must be aligned on a 256k boundary, but can be placed
600 * anywhere with that alignment on POWER ISA 3+ systems. On earlier
601 * systems, offset addition is done by the CPU with bitwise OR rather
602 * than addition, so the table must also be aligned on a boundary of
603 * its own size. Pick the larger of the two, which works on all
606 moea64_pteg_table = (struct lpte *)moea64_bootstrap_alloc(size,
607 MAX(256*1024, size));
610 (struct lpte *)PHYS_TO_DMAP((vm_offset_t)moea64_pteg_table);
611 /* Allocate partition table (ISA 3.0). */
612 if (cpu_features2 & PPC_FEATURE2_ARCH_3_00) {
614 (struct pate *)moea64_bootstrap_alloc(PART_SIZE, PART_SIZE);
616 (struct pate *)PHYS_TO_DMAP((vm_offset_t)moea64_part_table);
619 bzero(__DEVOLATILE(void *, moea64_pteg_table), moea64_pteg_count *
620 sizeof(struct lpteg));
621 if (cpu_features2 & PPC_FEATURE2_ARCH_3_00) {
622 bzero(__DEVOLATILE(void *, moea64_part_table), PART_SIZE);
623 moea64_part_table[0].pagetab = htobe64(
624 (DMAP_TO_PHYS((vm_offset_t)moea64_pteg_table)) |
625 (uintptr_t)(flsl((moea64_pteg_count - 1) >> 11)));
629 CTR1(KTR_PMAP, "moea64_bootstrap: PTEG table at %p", moea64_pteg_table);
631 moea64_mid_bootstrap(kernelstart, kernelend);
634 * Add a mapping for the page table itself if there is no direct map.
636 if (!hw_direct_map) {
637 size = moea64_pteg_count * sizeof(struct lpteg);
638 off = (vm_offset_t)(moea64_pteg_table);
640 for (pa = off; pa < off + size; pa += PAGE_SIZE)
645 /* Bring up virtual memory */
646 moea64_late_bootstrap(kernelstart, kernelend);
653 #ifndef __powerpc64__
654 register_t msr, scratch;
657 i = 0xc00; /* IS = 11 */
658 switch (mfpvr() >> 16) {
667 i = 0; /* IS not supported */
673 for (; i < 0x400000; i += 0x00001000) {
675 __asm __volatile("tlbiel %0" :: "r"(i));
688 : "=r"(msr), "=r"(scratch) : "r"(i), "r"(1));
697 atomic_pte_lock(volatile struct lpte *pte, uint64_t bitmask, uint64_t *oldhi)
707 * Note: in principle, if just the locked bit were set here, we
708 * could avoid needing the eviction lock. However, eviction occurs
709 * so rarely that it isn't worth bothering about in practice.
713 * Note: Success of this sequence has the side effect of invalidating
714 * the PTE, as we are setting it to LPTE_LOCKED and discarding the
715 * other bits, including LPTE_V.
718 "1:\tldarx %1, 0, %3\n\t" /* load old value */
719 "and. %0,%1,%4\n\t" /* check if any bits set */
720 "bne 2f\n\t" /* exit if any set */
721 "stdcx. %5, 0, %3\n\t" /* attempt to store */
722 "bne- 1b\n\t" /* spin if failed */
723 "li %0, 1\n\t" /* success - retval = 1 */
724 "b 3f\n\t" /* we've succeeded */
726 "stdcx. %1, 0, %3\n\t" /* clear reservation (74xx) */
727 "li %0, 0\n\t" /* failure - retval = 0 */
729 : "=&r" (ret), "=&r"(temp), "=m" (pte->pte_hi)
730 : "r" ((volatile char *)&pte->pte_hi),
731 "r" (htobe64(bitmask)), "r" (htobe64(LPTE_LOCKED)),
733 : "cr0", "cr1", "cr2", "memory");
734 *oldhi = be64toh(temp);
737 * This code is used on bridge mode only.
740 "1:\tlwarx %1, 0, %3\n\t" /* load old value */
741 "and. %0,%1,%4\n\t" /* check if any bits set */
742 "bne 2f\n\t" /* exit if any set */
743 "stwcx. %5, 0, %3\n\t" /* attempt to store */
744 "bne- 1b\n\t" /* spin if failed */
745 "li %0, 1\n\t" /* success - retval = 1 */
746 "b 3f\n\t" /* we've succeeded */
748 "stwcx. %1, 0, %3\n\t" /* clear reservation (74xx) */
749 "li %0, 0\n\t" /* failure - retval = 0 */
751 : "=&r" (ret), "=&r"(oldhihalf), "=m" (pte->pte_hi)
752 : "r" ((volatile char *)&pte->pte_hi + 4),
753 "r" ((uint32_t)bitmask), "r" ((uint32_t)LPTE_LOCKED),
755 : "cr0", "cr1", "cr2", "memory");
757 *oldhi = (pte->pte_hi & 0xffffffff00000000ULL) | oldhihalf;
764 moea64_insert_to_pteg_native(struct lpte *pvo_pt, uintptr_t slotbase,
767 volatile struct lpte *pt;
768 uint64_t oldptehi, va;
772 /* Start at a random slot */
774 for (j = 0; j < 8; j++) {
775 k = slotbase + (i + j) % 8;
776 pt = &moea64_pteg_table[k];
777 /* Invalidate and seize lock only if no bits in mask set */
778 if (atomic_pte_lock(pt, mask, &oldptehi)) /* Lock obtained */
785 if (oldptehi & LPTE_VALID) {
786 KASSERT(!(oldptehi & LPTE_WIRED), ("Unmapped wired entry"));
788 * Need to invalidate old entry completely: see
789 * "Modifying a Page Table Entry". Need to reconstruct
790 * the virtual address for the outgoing entry to do that.
792 va = oldptehi >> (ADDR_SR_SHFT - ADDR_API_SHFT64);
793 if (oldptehi & LPTE_HID)
794 va = (((k >> 3) ^ moea64_pteg_mask) ^ va) &
795 (ADDR_PIDX >> ADDR_PIDX_SHFT);
797 va = ((k >> 3) ^ va) & (ADDR_PIDX >> ADDR_PIDX_SHFT);
798 va |= (oldptehi & LPTE_AVPN_MASK) <<
799 (ADDR_API_SHFT64 - ADDR_PIDX_SHFT);
802 STAT_MOEA64(moea64_pte_valid--);
803 STAT_MOEA64(moea64_pte_overflow++);
807 * Update the PTE as per "Adding a Page Table Entry". Lock is released
808 * by setting the high doubleworld.
810 pt->pte_lo = htobe64(pvo_pt->pte_lo);
812 pt->pte_hi = htobe64(pvo_pt->pte_hi);
815 /* Keep statistics */
816 STAT_MOEA64(moea64_pte_valid++);
821 static __always_inline int64_t
822 moea64_pte_insert_locked(struct pvo_entry *pvo, struct lpte *insertpt,
828 * First try primary hash.
830 slot = moea64_insert_to_pteg_native(insertpt, pvo->pvo_pte.slot,
831 mask | LPTE_WIRED | LPTE_LOCKED);
833 pvo->pvo_pte.slot = slot;
838 * Now try secondary hash.
840 pvo->pvo_vaddr ^= PVO_HID;
841 insertpt->pte_hi ^= LPTE_HID;
842 pvo->pvo_pte.slot ^= (moea64_pteg_mask << 3);
843 slot = moea64_insert_to_pteg_native(insertpt, pvo->pvo_pte.slot,
844 mask | LPTE_WIRED | LPTE_LOCKED);
846 pvo->pvo_pte.slot = slot;
854 moea64_pte_insert_native(struct pvo_entry *pvo)
856 struct lpte insertpt;
860 moea64_pte_from_pvo(pvo, &insertpt);
862 /* Make sure further insertion is locked out during evictions */
863 rw_rlock(&moea64_eviction_lock);
865 pvo->pvo_pte.slot &= ~7ULL; /* Base slot address */
866 ret = moea64_pte_insert_locked(pvo, &insertpt, LPTE_VALID);
869 * Out of luck. Find a PTE to sacrifice.
872 /* Lock out all insertions for a bit */
873 if (!rw_try_upgrade(&moea64_eviction_lock)) {
874 rw_runlock(&moea64_eviction_lock);
875 rw_wlock(&moea64_eviction_lock);
877 /* Don't evict large pages */
878 ret = moea64_pte_insert_locked(pvo, &insertpt, LPTE_BIG);
879 rw_wunlock(&moea64_eviction_lock);
880 /* No freeable slots in either PTEG? We're hosed. */
882 panic("moea64_pte_insert: overflow");
884 rw_runlock(&moea64_eviction_lock);
890 moea64_dump_pmap_native(void *ctx, void *buf, u_long *nbytes)
892 struct dump_context *dctx;
893 u_long ptex, ptex_end;
895 dctx = (struct dump_context *)ctx;
897 ptex_end = ptex + dctx->blksz / sizeof(struct lpte);
898 ptex_end = MIN(ptex_end, dctx->ptex_end);
899 *nbytes = (ptex_end - ptex) * sizeof(struct lpte);
904 dctx->ptex = ptex_end;
905 return (__DEVOLATILE(struct lpte *, moea64_pteg_table) + ptex);
908 static __always_inline uint64_t
909 moea64_vpn_from_pte(uint64_t ptehi, uintptr_t slot)
911 uint64_t pgn, pgnlo, vsid;
913 vsid = (ptehi & LPTE_AVA_MASK) >> LPTE_VSID_SHIFT;
914 if ((ptehi & LPTE_HID) != 0)
915 slot ^= (moea64_pteg_mask << 3);
916 pgnlo = ((vsid & VSID_HASH_MASK) ^ (slot >> 3)) & EA_PAGELO_MASK;
917 pgn = ((ptehi & LPTE_AVA_PGNHI_MASK) << (EA_PAGELO_SHIFT -
918 LPTE_AVA_PGNHI_SHIFT)) | pgnlo;
919 return ((vsid << 16) | pgn);
922 static __always_inline int64_t
923 moea64_pte_unset_sp_locked(struct pvo_entry *pvo)
925 volatile struct lpte *pt;
926 uint64_t ptehi, refchg, vpn;
930 eva = PVO_VADDR(pvo) + HPT_SP_SIZE;
932 for (; pvo != NULL && PVO_VADDR(pvo) < eva;
933 pvo = RB_NEXT(pvo_tree, &pvo->pvo_pmap->pmap_pvo, pvo)) {
934 pt = moea64_pteg_table + pvo->pvo_pte.slot;
935 ptehi = be64toh(pt->pte_hi);
936 if ((ptehi & LPTE_AVPN_MASK) !=
937 moea64_pte_vpn_from_pvo_vpn(pvo)) {
938 /* Evicted: invalidate new entry */
939 STAT_MOEA64(moea64_pte_overflow--);
940 vpn = moea64_vpn_from_pte(ptehi, pvo->pvo_pte.slot);
941 CTR1(KTR_PMAP, "Evicted page in pte_unset_sp: vpn=%jx",
943 /* Assume evicted page was modified */
948 refchg |= moea64_pte_unset_locked(pt, vpn);
955 moea64_pte_unset_sp_native(struct pvo_entry *pvo)
959 PMAP_LOCK_ASSERT(pvo->pvo_pmap, MA_OWNED);
960 KASSERT((PVO_VADDR(pvo) & HPT_SP_MASK) == 0,
961 ("%s: va %#jx unaligned", __func__, (uintmax_t)PVO_VADDR(pvo)));
963 rw_rlock(&moea64_eviction_lock);
964 refchg = moea64_pte_unset_sp_locked(pvo);
965 rw_runlock(&moea64_eviction_lock);
970 static __always_inline int64_t
971 moea64_pte_insert_sp_locked(struct pvo_entry *pvo)
973 struct lpte insertpt;
977 eva = PVO_VADDR(pvo) + HPT_SP_SIZE;
979 for (; pvo != NULL && PVO_VADDR(pvo) < eva;
980 pvo = RB_NEXT(pvo_tree, &pvo->pvo_pmap->pmap_pvo, pvo)) {
981 moea64_pte_from_pvo(pvo, &insertpt);
982 pvo->pvo_pte.slot &= ~7ULL; /* Base slot address */
984 ret = moea64_pte_insert_locked(pvo, &insertpt, LPTE_VALID);
986 /* Lock out all insertions for a bit */
987 if (!rw_try_upgrade(&moea64_eviction_lock)) {
988 rw_runlock(&moea64_eviction_lock);
989 rw_wlock(&moea64_eviction_lock);
991 /* Don't evict large pages */
992 ret = moea64_pte_insert_locked(pvo, &insertpt,
994 rw_downgrade(&moea64_eviction_lock);
995 /* No freeable slots in either PTEG? We're hosed. */
997 panic("moea64_pte_insert_sp: overflow");
1005 moea64_pte_insert_sp_native(struct pvo_entry *pvo)
1007 PMAP_LOCK_ASSERT(pvo->pvo_pmap, MA_OWNED);
1008 KASSERT((PVO_VADDR(pvo) & HPT_SP_MASK) == 0,
1009 ("%s: va %#jx unaligned", __func__, (uintmax_t)PVO_VADDR(pvo)));
1011 rw_rlock(&moea64_eviction_lock);
1012 moea64_pte_insert_sp_locked(pvo);
1013 rw_runlock(&moea64_eviction_lock);
1019 moea64_pte_replace_sp_native(struct pvo_entry *pvo)
1023 PMAP_LOCK_ASSERT(pvo->pvo_pmap, MA_OWNED);
1024 KASSERT((PVO_VADDR(pvo) & HPT_SP_MASK) == 0,
1025 ("%s: va %#jx unaligned", __func__, (uintmax_t)PVO_VADDR(pvo)));
1027 rw_rlock(&moea64_eviction_lock);
1028 refchg = moea64_pte_unset_sp_locked(pvo);
1029 moea64_pte_insert_sp_locked(pvo);
1030 rw_runlock(&moea64_eviction_lock);