2 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Matt Thomas <matt@3am-software.com> of Allegro Networks, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
30 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
31 * Copyright (C) 1995, 1996 TooLs GmbH.
32 * All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by TooLs GmbH.
45 * 4. The name of TooLs GmbH may not be used to endorse or promote products
46 * derived from this software without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
49 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
50 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
51 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
53 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
54 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
55 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
56 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
57 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 * $NetBSD: pmap.c,v 1.28 2000/03/26 20:42:36 kleink Exp $
62 * Copyright (C) 2001 Benno Rice.
63 * All rights reserved.
65 * Redistribution and use in source and binary forms, with or without
66 * modification, are permitted provided that the following conditions
68 * 1. Redistributions of source code must retain the above copyright
69 * notice, this list of conditions and the following disclaimer.
70 * 2. Redistributions in binary form must reproduce the above copyright
71 * notice, this list of conditions and the following disclaimer in the
72 * documentation and/or other materials provided with the distribution.
74 * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
75 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
76 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
77 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
78 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
79 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
80 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
81 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
82 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
83 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
86 #include <sys/cdefs.h>
87 __FBSDID("$FreeBSD$");
90 * Native 64-bit page table operations for running without a hypervisor.
93 #include <sys/param.h>
94 #include <sys/kernel.h>
97 #include <sys/mutex.h>
99 #include <sys/sched.h>
100 #include <sys/sysctl.h>
101 #include <sys/systm.h>
102 #include <sys/rwlock.h>
103 #include <sys/endian.h>
108 #include <vm/vm_param.h>
109 #include <vm/vm_kern.h>
110 #include <vm/vm_page.h>
111 #include <vm/vm_map.h>
112 #include <vm/vm_object.h>
113 #include <vm/vm_extern.h>
114 #include <vm/vm_pageout.h>
116 #include <machine/md_var.h>
117 #include <machine/mmuvar.h>
119 #include "mmu_oea64.h"
121 #include "moea64_if.h"
123 #define PTESYNC() __asm __volatile("ptesync");
124 #define TLBSYNC() __asm __volatile("tlbsync; ptesync");
125 #define SYNC() __asm __volatile("sync");
126 #define EIEIO() __asm __volatile("eieio");
128 #define VSID_HASH_MASK 0x0000007fffffffffULL
131 TLBIE(uint64_t vpn) {
132 #ifndef __powerpc64__
133 register_t vpn_hi, vpn_lo;
135 register_t scratch, intr;
138 static volatile u_int tlbie_lock = 0;
140 vpn <<= ADDR_PIDX_SHFT;
141 vpn &= ~(0xffffULL << 48);
143 /* Hobo spinlock: we need stronger guarantees than mutexes provide */
144 while (!atomic_cmpset_int(&tlbie_lock, 0, 1));
145 isync(); /* Flush instruction queue once lock acquired */
148 __asm __volatile("tlbie %0" :: "r"(vpn) : "memory");
149 __asm __volatile("eieio; tlbsync; ptesync" ::: "memory");
151 vpn_hi = (uint32_t)(vpn >> 32);
152 vpn_lo = (uint32_t)vpn;
154 intr = intr_disable();
169 : "=r"(msr), "=r"(scratch) : "r"(vpn_hi), "r"(vpn_lo), "r"(32), "r"(1)
174 /* No barriers or special ops -- taken care of by ptesync above */
178 #define DISABLE_TRANS(msr) msr = mfmsr(); mtmsr(msr & ~PSL_DR)
179 #define ENABLE_TRANS(msr) mtmsr(msr)
184 static volatile struct lpte *moea64_pteg_table;
185 static struct rwlock moea64_eviction_lock;
190 static int moea64_pte_insert_native(mmu_t, struct pvo_entry *);
191 static int64_t moea64_pte_synch_native(mmu_t, struct pvo_entry *);
192 static int64_t moea64_pte_clear_native(mmu_t, struct pvo_entry *, uint64_t);
193 static int64_t moea64_pte_replace_native(mmu_t, struct pvo_entry *, int);
194 static int64_t moea64_pte_unset_native(mmu_t mmu, struct pvo_entry *);
199 static void moea64_bootstrap_native(mmu_t mmup,
200 vm_offset_t kernelstart, vm_offset_t kernelend);
201 static void moea64_cpu_bootstrap_native(mmu_t, int ap);
202 static void tlbia(void);
204 static mmu_method_t moea64_native_methods[] = {
205 /* Internal interfaces */
206 MMUMETHOD(mmu_bootstrap, moea64_bootstrap_native),
207 MMUMETHOD(mmu_cpu_bootstrap, moea64_cpu_bootstrap_native),
209 MMUMETHOD(moea64_pte_synch, moea64_pte_synch_native),
210 MMUMETHOD(moea64_pte_clear, moea64_pte_clear_native),
211 MMUMETHOD(moea64_pte_unset, moea64_pte_unset_native),
212 MMUMETHOD(moea64_pte_replace, moea64_pte_replace_native),
213 MMUMETHOD(moea64_pte_insert, moea64_pte_insert_native),
218 MMU_DEF_INHERIT(oea64_mmu_native, MMU_TYPE_G5, moea64_native_methods,
222 moea64_pte_synch_native(mmu_t mmu, struct pvo_entry *pvo)
224 volatile struct lpte *pt = moea64_pteg_table + pvo->pvo_pte.slot;
225 struct lpte properpt;
228 PMAP_LOCK_ASSERT(pvo->pvo_pmap, MA_OWNED);
230 moea64_pte_from_pvo(pvo, &properpt);
232 rw_rlock(&moea64_eviction_lock);
233 if ((be64toh(pt->pte_hi) & LPTE_AVPN_MASK) !=
234 (properpt.pte_hi & LPTE_AVPN_MASK)) {
236 rw_runlock(&moea64_eviction_lock);
241 ptelo = be64toh(pt->pte_lo);
243 rw_runlock(&moea64_eviction_lock);
245 return (ptelo & (LPTE_REF | LPTE_CHG));
249 moea64_pte_clear_native(mmu_t mmu, struct pvo_entry *pvo, uint64_t ptebit)
251 volatile struct lpte *pt = moea64_pteg_table + pvo->pvo_pte.slot;
252 struct lpte properpt;
255 PMAP_LOCK_ASSERT(pvo->pvo_pmap, MA_OWNED);
257 moea64_pte_from_pvo(pvo, &properpt);
259 rw_rlock(&moea64_eviction_lock);
260 if ((be64toh(pt->pte_hi) & LPTE_AVPN_MASK) !=
261 (properpt.pte_hi & LPTE_AVPN_MASK)) {
263 rw_runlock(&moea64_eviction_lock);
267 if (ptebit == LPTE_REF) {
268 /* See "Resetting the Reference Bit" in arch manual */
270 /* 2-step here safe: precision is not guaranteed */
271 ptelo = be64toh(pt->pte_lo);
273 /* One-byte store to avoid touching the C bit */
274 ((volatile uint8_t *)(&pt->pte_lo))[6] =
275 #if BYTE_ORDER == BIG_ENDIAN
276 ((uint8_t *)(&properpt.pte_lo))[6];
278 ((uint8_t *)(&properpt.pte_lo))[1];
280 rw_runlock(&moea64_eviction_lock);
286 rw_runlock(&moea64_eviction_lock);
287 ptelo = moea64_pte_unset_native(mmu, pvo);
288 moea64_pte_insert_native(mmu, pvo);
291 return (ptelo & (LPTE_REF | LPTE_CHG));
295 moea64_pte_unset_native(mmu_t mmu, struct pvo_entry *pvo)
297 volatile struct lpte *pt = moea64_pteg_table + pvo->pvo_pte.slot;
298 struct lpte properpt;
301 moea64_pte_from_pvo(pvo, &properpt);
303 rw_rlock(&moea64_eviction_lock);
304 if ((be64toh(pt->pte_hi & LPTE_AVPN_MASK)) !=
305 (properpt.pte_hi & LPTE_AVPN_MASK)) {
307 moea64_pte_overflow--;
308 rw_runlock(&moea64_eviction_lock);
313 * Invalidate the pte, briefly locking it to collect RC bits. No
314 * atomics needed since this is protected against eviction by the lock.
318 pt->pte_hi = be64toh((pt->pte_hi & ~LPTE_VALID) | LPTE_LOCKED);
321 ptelo = be64toh(pt->pte_lo);
322 *((volatile int32_t *)(&pt->pte_hi) + 1) = 0; /* Release lock */
324 rw_runlock(&moea64_eviction_lock);
326 /* Keep statistics */
329 return (ptelo & (LPTE_CHG | LPTE_REF));
333 moea64_pte_replace_native(mmu_t mmu, struct pvo_entry *pvo, int flags)
335 volatile struct lpte *pt = moea64_pteg_table + pvo->pvo_pte.slot;
336 struct lpte properpt;
340 /* Just some software bits changing. */
341 moea64_pte_from_pvo(pvo, &properpt);
343 rw_rlock(&moea64_eviction_lock);
344 if ((be64toh(pt->pte_hi) & LPTE_AVPN_MASK) !=
345 (properpt.pte_hi & LPTE_AVPN_MASK)) {
346 rw_runlock(&moea64_eviction_lock);
349 pt->pte_hi = htobe64(properpt.pte_hi);
350 ptelo = be64toh(pt->pte_lo);
351 rw_runlock(&moea64_eviction_lock);
353 /* Otherwise, need reinsertion and deletion */
354 ptelo = moea64_pte_unset_native(mmu, pvo);
355 moea64_pte_insert_native(mmu, pvo);
362 moea64_cpu_bootstrap_native(mmu_t mmup, int ap)
366 struct slb *slb = PCPU_GET(slb);
371 * Initialize segment registers and MMU
374 mtmsr(mfmsr() & ~PSL_DR & ~PSL_IR);
377 * Install kernel SLB entries
381 __asm __volatile ("slbia");
382 __asm __volatile ("slbmfee %0,%1; slbie %0;" : "=r"(seg0) :
385 for (i = 0; i < n_slbs; i++) {
386 if (!(slb[i].slbe & SLBE_VALID))
389 __asm __volatile ("slbmte %0, %1" ::
390 "r"(slb[i].slbv), "r"(slb[i].slbe));
393 for (i = 0; i < 16; i++)
394 mtsrin(i << ADDR_SR_SHFT, kernel_pmap->pm_sr[i]);
401 __asm __volatile ("ptesync; mtsdr1 %0; isync"
402 :: "r"((uintptr_t)moea64_pteg_table
403 | (uintptr_t)(flsl(moea64_pteg_mask >> 11))));
408 moea64_bootstrap_native(mmu_t mmup, vm_offset_t kernelstart,
409 vm_offset_t kernelend)
416 moea64_early_bootstrap(mmup, kernelstart, kernelend);
419 * Allocate PTEG table.
422 size = moea64_pteg_count * sizeof(struct lpteg);
423 CTR2(KTR_PMAP, "moea64_bootstrap: %d PTEGs, %d bytes",
424 moea64_pteg_count, size);
425 rw_init(&moea64_eviction_lock, "pte eviction");
428 * We now need to allocate memory. This memory, to be allocated,
429 * has to reside in a page table. The page table we are about to
430 * allocate. We don't have BAT. So drop to data real mode for a minute
431 * as a measure of last resort. We do this a couple times.
434 moea64_pteg_table = (struct lpte *)moea64_bootstrap_alloc(size, size);
436 bzero(__DEVOLATILE(void *, moea64_pteg_table), moea64_pteg_count *
437 sizeof(struct lpteg));
440 CTR1(KTR_PMAP, "moea64_bootstrap: PTEG table at %p", moea64_pteg_table);
442 moea64_mid_bootstrap(mmup, kernelstart, kernelend);
445 * Add a mapping for the page table itself if there is no direct map.
447 if (!hw_direct_map) {
448 size = moea64_pteg_count * sizeof(struct lpteg);
449 off = (vm_offset_t)(moea64_pteg_table);
451 for (pa = off; pa < off + size; pa += PAGE_SIZE)
456 /* Bring up virtual memory */
457 moea64_late_bootstrap(mmup, kernelstart, kernelend);
464 #ifndef __powerpc64__
465 register_t msr, scratch;
468 i = 0xc00; /* IS = 11 */
469 switch (mfpvr() >> 16) {
478 i = 0; /* IS not supported */
484 for (; i < 0x200000; i += 0x00001000) {
486 __asm __volatile("tlbiel %0" :: "r"(i));
499 : "=r"(msr), "=r"(scratch) : "r"(i), "r"(1));
508 atomic_pte_lock(volatile struct lpte *pte, uint64_t bitmask, uint64_t *oldhi)
514 * Note: in principle, if just the locked bit were set here, we
515 * could avoid needing the eviction lock. However, eviction occurs
516 * so rarely that it isn't worth bothering about in practice.
520 "1:\tlwarx %1, 0, %3\n\t" /* load old value */
521 "and. %0,%1,%4\n\t" /* check if any bits set */
522 "bne 2f\n\t" /* exit if any set */
523 "stwcx. %5, 0, %3\n\t" /* attempt to store */
524 "bne- 1b\n\t" /* spin if failed */
525 "li %0, 1\n\t" /* success - retval = 1 */
526 "b 3f\n\t" /* we've succeeded */
528 "stwcx. %1, 0, %3\n\t" /* clear reservation (74xx) */
529 "li %0, 0\n\t" /* failure - retval = 0 */
531 : "=&r" (ret), "=&r"(oldhihalf), "=m" (pte->pte_hi)
532 : "r" ((volatile char *)&pte->pte_hi + 4),
533 "r" ((uint32_t)bitmask), "r" ((uint32_t)LPTE_LOCKED),
535 : "cr0", "cr1", "cr2", "memory");
537 *oldhi = (pte->pte_hi & 0xffffffff00000000ULL) | oldhihalf;
543 moea64_insert_to_pteg_native(struct lpte *pvo_pt, uintptr_t slotbase,
546 volatile struct lpte *pt;
547 uint64_t oldptehi, va;
551 /* Start at a random slot */
553 for (j = 0; j < 8; j++) {
554 k = slotbase + (i + j) % 8;
555 pt = &moea64_pteg_table[k];
556 /* Invalidate and seize lock only if no bits in mask set */
557 if (atomic_pte_lock(pt, mask, &oldptehi)) /* Lock obtained */
564 if (oldptehi & LPTE_VALID) {
565 KASSERT(!(oldptehi & LPTE_WIRED), ("Unmapped wired entry"));
567 * Need to invalidate old entry completely: see
568 * "Modifying a Page Table Entry". Need to reconstruct
569 * the virtual address for the outgoing entry to do that.
571 if (oldptehi & LPTE_BIG)
572 va = oldptehi >> moea64_large_page_shift;
574 va = oldptehi >> ADDR_PIDX_SHFT;
575 if (oldptehi & LPTE_HID)
576 va = (((k >> 3) ^ moea64_pteg_mask) ^ va) &
579 va = ((k >> 3) ^ va) & VSID_HASH_MASK;
580 va |= (oldptehi & LPTE_AVPN_MASK) <<
581 (ADDR_API_SHFT64 - ADDR_PIDX_SHFT);
585 moea64_pte_overflow++;
589 * Update the PTE as per "Adding a Page Table Entry". Lock is released
590 * by setting the high doubleworld.
592 pt->pte_lo = htobe64(pvo_pt->pte_lo);
594 pt->pte_hi = htobe64(pvo_pt->pte_hi);
597 /* Keep statistics */
604 moea64_pte_insert_native(mmu_t mmu, struct pvo_entry *pvo)
606 struct lpte insertpt;
610 moea64_pte_from_pvo(pvo, &insertpt);
612 /* Make sure further insertion is locked out during evictions */
613 rw_rlock(&moea64_eviction_lock);
616 * First try primary hash.
618 pvo->pvo_pte.slot &= ~7ULL; /* Base slot address */
619 slot = moea64_insert_to_pteg_native(&insertpt, pvo->pvo_pte.slot,
620 LPTE_VALID | LPTE_WIRED | LPTE_LOCKED);
622 rw_runlock(&moea64_eviction_lock);
623 pvo->pvo_pte.slot = slot;
628 * Now try secondary hash.
630 pvo->pvo_vaddr ^= PVO_HID;
631 insertpt.pte_hi ^= LPTE_HID;
632 pvo->pvo_pte.slot ^= (moea64_pteg_mask << 3);
633 slot = moea64_insert_to_pteg_native(&insertpt, pvo->pvo_pte.slot,
634 LPTE_VALID | LPTE_WIRED | LPTE_LOCKED);
636 rw_runlock(&moea64_eviction_lock);
637 pvo->pvo_pte.slot = slot;
642 * Out of luck. Find a PTE to sacrifice.
645 /* Lock out all insertions for a bit */
646 if (!rw_try_upgrade(&moea64_eviction_lock)) {
647 rw_runlock(&moea64_eviction_lock);
648 rw_wlock(&moea64_eviction_lock);
651 slot = moea64_insert_to_pteg_native(&insertpt, pvo->pvo_pte.slot,
652 LPTE_WIRED | LPTE_LOCKED);
654 rw_wunlock(&moea64_eviction_lock);
655 pvo->pvo_pte.slot = slot;
659 /* Try other hash table. Now we're getting desperate... */
660 pvo->pvo_vaddr ^= PVO_HID;
661 insertpt.pte_hi ^= LPTE_HID;
662 pvo->pvo_pte.slot ^= (moea64_pteg_mask << 3);
663 slot = moea64_insert_to_pteg_native(&insertpt, pvo->pvo_pte.slot,
664 LPTE_WIRED | LPTE_LOCKED);
666 rw_wunlock(&moea64_eviction_lock);
667 pvo->pvo_pte.slot = slot;
671 /* No freeable slots in either PTEG? We're hosed. */
672 rw_wunlock(&moea64_eviction_lock);
673 panic("moea64_pte_insert: overflow");