Home | History | Annotate | Line # | Download | only in booke
e500_tlb.c revision 1.15
      1 /*	$NetBSD: e500_tlb.c,v 1.15 2014/12/28 14:13:56 nonaka Exp $	*/
      2 /*-
      3  * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
      4  * All rights reserved.
      5  *
      6  * This code is derived from software contributed to The NetBSD Foundation
      7  * by Raytheon BBN Technologies Corp and Defense Advanced Research Projects
      8  * Agency and which was developed by Matt Thomas of 3am Software Foundry.
      9  *
     10  * This material is based upon work supported by the Defense Advanced Research
     11  * Projects Agency and Space and Naval Warfare Systems Center, Pacific, under
     12  * Contract No. N66001-09-C-2073.
     13  * Approved for Public Release, Distribution Unlimited
     14  *
     15  * Redistribution and use in source and binary forms, with or without
     16  * modification, are permitted provided that the following conditions
     17  * are met:
     18  * 1. Redistributions of source code must retain the above copyright
     19  *    notice, this list of conditions and the following disclaimer.
     20  * 2. Redistributions in binary form must reproduce the above copyright
     21  *    notice, this list of conditions and the following disclaimer in the
     22  *    documentation and/or other materials provided with the distribution.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     25  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     26  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     27  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     28  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     32  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     34  * POSSIBILITY OF SUCH DAMAGE.
     35  */
     36 
     37 #include "opt_ppcparam.h"
     38 
     39 #define	__PMAP_PRIVATE
     40 
     41 #include <sys/cdefs.h>
     42 
     43 __KERNEL_RCSID(0, "$NetBSD: e500_tlb.c,v 1.15 2014/12/28 14:13:56 nonaka Exp $");
     44 
     45 #include <sys/param.h>
     46 
     47 #include <uvm/uvm_extern.h>
     48 
     49 #include <powerpc/spr.h>
     50 #include <powerpc/booke/spr.h>
     51 #include <powerpc/booke/cpuvar.h>
     52 #include <powerpc/booke/e500reg.h>
     53 #include <powerpc/booke/e500var.h>
     54 #include <powerpc/booke/pmap.h>
     55 
     56 struct e500_tlb {
     57 	vaddr_t tlb_va;
     58 	uint32_t tlb_pte;
     59 	uint32_t tlb_asid;
     60 	vsize_t tlb_size;
     61 };
     62 
     63 struct e500_hwtlb {
     64 	uint32_t hwtlb_mas0;
     65 	uint32_t hwtlb_mas1;
     66 	uint32_t hwtlb_mas2;
     67 	uint32_t hwtlb_mas3;
     68 };
     69 
     70 struct e500_xtlb {
     71 	struct e500_tlb e_tlb;
     72 	struct e500_hwtlb e_hwtlb;
     73 	u_long e_refcnt;
     74 };
     75 
     76 static struct e500_tlb1 {
     77 	uint32_t tlb1_maxsize;
     78 	uint32_t tlb1_minsize;
     79 	u_int tlb1_numentries;
     80 	u_int tlb1_numfree;
     81 	u_int tlb1_freelist[32];
     82 	struct e500_xtlb tlb1_entries[32];
     83 } e500_tlb1;
     84 
     85 static inline register_t mftlb0cfg(void) __pure;
     86 static inline register_t mftlb1cfg(void) __pure;
     87 
     88 static inline register_t
     89 mftlb0cfg(void)
     90 {
     91 	register_t tlb0cfg;
     92 	__asm("mfspr %0, %1" : "=r"(tlb0cfg) : "n"(SPR_TLB0CFG));
     93 	return tlb0cfg;
     94 }
     95 
     96 static inline register_t
     97 mftlb1cfg(void)
     98 {
     99 	register_t tlb1cfg;
    100 	__asm("mfspr %0, %1" : "=r"(tlb1cfg) : "n"(SPR_TLB1CFG));
    101 	return tlb1cfg;
    102 }
    103 
    104 static struct e500_tlb
    105 hwtlb_to_tlb(const struct e500_hwtlb hwtlb)
    106 {
    107 	struct e500_tlb tlb;
    108 	register_t prot_mask;
    109 	u_int prot_shift;
    110 
    111 	tlb.tlb_va = MAS2_EPN & hwtlb.hwtlb_mas2;
    112 	tlb.tlb_size = 1024 << (2 * MASX_TSIZE_GET(hwtlb.hwtlb_mas1));
    113 	tlb.tlb_asid = MASX_TID_GET(hwtlb.hwtlb_mas1);
    114 	tlb.tlb_pte = (hwtlb.hwtlb_mas2 & MAS2_WIMGE)
    115 	    | (hwtlb.hwtlb_mas3 & MAS3_RPN);
    116 	if (hwtlb.hwtlb_mas1 & MAS1_TS) {
    117 		prot_mask = MAS3_UX|MAS3_UW|MAS3_UR;
    118 		prot_shift = PTE_RWX_SHIFT - 1;
    119 	} else {
    120 		prot_mask = MAS3_SX|MAS3_SW|MAS3_SR;
    121 		prot_shift = PTE_RWX_SHIFT;
    122 	}
    123 	tlb.tlb_pte |= (prot_mask & hwtlb.hwtlb_mas3) << prot_shift;
    124 	return tlb;
    125 }
    126 
    127 static inline struct e500_hwtlb
    128 hwtlb_read(uint32_t mas0, u_int slot)
    129 {
    130 	struct e500_hwtlb hwtlb;
    131 	register_t tlbcfg;
    132 
    133 	if (__predict_true(mas0 == MAS0_TLBSEL_TLB0)) {
    134 		tlbcfg = mftlb0cfg();
    135 	} else if (mas0 == MAS0_TLBSEL_TLB1) {
    136 		tlbcfg = mftlb1cfg();
    137 	} else {
    138 		panic("%s:%d: unexpected MAS0 %#" PRIx32,
    139 		    __func__, __LINE__, mas0);
    140 	}
    141 
    142 	/*
    143 	 * ESEL is the way we want to look up.
    144 	 * If tlbassoc is the same as tlbentries (like in TLB1) then the TLB is
    145 	 * fully associative, the entire slot is placed into ESEL.  If tlbassoc
    146 	 * is less than the number of tlb entries, the slot is split in two
    147 	 * fields.  Since the TLB is M rows by N ways, the lowers bits are for
    148 	 * row (MAS2[EPN]) and the upper for the way (MAS1[ESEL]).
    149 	 */
    150 	const u_int tlbassoc = TLBCFG_ASSOC(tlbcfg);
    151 	const u_int tlbentries = TLBCFG_NENTRY(tlbcfg);
    152 	const u_int esel_shift =
    153 	    __builtin_clz(tlbassoc) - __builtin_clz(tlbentries);
    154 
    155 	/*
    156 	 * Disable interrupts since we don't want anyone else mucking with
    157 	 * the MMU Assist registers
    158 	 */
    159 	const register_t msr = wrtee(0);
    160 	const register_t saved_mas0 = mfspr(SPR_MAS0);
    161 	mtspr(SPR_MAS0, mas0 | MAS0_ESEL_MAKE(slot >> esel_shift));
    162 
    163 	if (__predict_true(tlbassoc > tlbentries))
    164 		mtspr(SPR_MAS2, slot << PAGE_SHIFT);
    165 
    166 	/*
    167 	 * Now select the entry and grab its contents.
    168 	 */
    169 	__asm volatile("tlbre");
    170 
    171 	hwtlb.hwtlb_mas0 = mfspr(SPR_MAS0);
    172 	hwtlb.hwtlb_mas1 = mfspr(SPR_MAS1);
    173 	hwtlb.hwtlb_mas2 = mfspr(SPR_MAS2);
    174 	hwtlb.hwtlb_mas3 = mfspr(SPR_MAS3);
    175 
    176 	mtspr(SPR_MAS0, saved_mas0);
    177 	wrtee(msr);	/* restore interrupts */
    178 
    179 	return hwtlb;
    180 }
    181 
    182 static inline void
    183 hwtlb_write(const struct e500_hwtlb hwtlb, bool needs_sync)
    184 {
    185 	const register_t msr = wrtee(0);
    186 	const uint32_t saved_mas0 = mfspr(SPR_MAS0);
    187 
    188 	/*
    189 	 * Need to always write MAS0 and MAS1
    190 	 */
    191 	mtspr(SPR_MAS0, hwtlb.hwtlb_mas0);
    192 	mtspr(SPR_MAS1, hwtlb.hwtlb_mas1);
    193 
    194 	/*
    195 	 * Only write the VPN/WIMGE if this is in TLB0 or if a valid mapping.
    196 	 */
    197 	if ((hwtlb.hwtlb_mas0 & MAS0_TLBSEL) == MAS0_TLBSEL_TLB0
    198 	    || (hwtlb.hwtlb_mas1 & MAS1_V)) {
    199 		mtspr(SPR_MAS2, hwtlb.hwtlb_mas2);
    200 	}
    201 	/*
    202 	 * Only need to write the RPN/prot if we are dealing with a valid
    203 	 * mapping.
    204 	 */
    205 	if (hwtlb.hwtlb_mas1 & MAS1_V) {
    206 		mtspr(SPR_MAS3, hwtlb.hwtlb_mas3);
    207 		//mtspr(SPR_MAS7, 0);
    208 	}
    209 
    210 #if 0
    211 	printf("%s->[%x,%x,%x,%x]\n",
    212 	    __func__,
    213 	    hwtlb.hwtlb_mas0, hwtlb.hwtlb_mas1,
    214 	    hwtlb.hwtlb_mas2, hwtlb.hwtlb_mas3);
    215 #endif
    216 	__asm volatile("tlbwe");
    217 	if (needs_sync) {
    218 		__asm volatile("tlbsync\n\tisync\n\tsync");
    219 	}
    220 
    221 	mtspr(SPR_MAS0, saved_mas0);
    222 	wrtee(msr);
    223 }
    224 
    225 static struct e500_hwtlb
    226 tlb_to_hwtlb(const struct e500_tlb tlb)
    227 {
    228 	struct e500_hwtlb hwtlb;
    229 
    230 	KASSERT(trunc_page(tlb.tlb_va) == tlb.tlb_va);
    231 	KASSERT(tlb.tlb_size != 0);
    232 	KASSERT((tlb.tlb_size & (tlb.tlb_size - 1)) == 0);
    233 	const uint32_t prot_mask = tlb.tlb_pte & PTE_RWX_MASK;
    234 	if (__predict_true(tlb.tlb_size == PAGE_SIZE)) {
    235 		hwtlb.hwtlb_mas0 = 0;
    236 		hwtlb.hwtlb_mas1 = MAS1_V | MASX_TSIZE_MAKE(1);
    237 		/*
    238 		 * A non-zero ASID means this is a user page so mark it as
    239 		 * being in the user's address space.
    240 		 */
    241 		if (tlb.tlb_asid) {
    242 			hwtlb.hwtlb_mas1 |= MAS1_TS
    243 			    | MASX_TID_MAKE(tlb.tlb_asid);
    244 			hwtlb.hwtlb_mas3 = (prot_mask >> (PTE_RWX_SHIFT - 1))
    245 			    | ((prot_mask & ~PTE_xX) >> PTE_RWX_SHIFT);
    246 			KASSERT(prot_mask & PTE_xR);
    247 			KASSERT(hwtlb.hwtlb_mas3 & MAS3_UR);
    248 			CTASSERT(MAS3_UR == (PTE_xR >> (PTE_RWX_SHIFT - 1)));
    249 			CTASSERT(MAS3_SR == (PTE_xR >> PTE_RWX_SHIFT));
    250 		} else {
    251 			hwtlb.hwtlb_mas3 = prot_mask >> PTE_RWX_SHIFT;
    252 		}
    253 		if (tlb.tlb_pte & PTE_UNMODIFIED)
    254 			hwtlb.hwtlb_mas3 &= ~(MAS3_UW|MAS3_SW);
    255 		if (tlb.tlb_pte & PTE_UNSYNCED)
    256 			hwtlb.hwtlb_mas3 &= ~(MAS3_UX|MAS3_SX);
    257 	} else {
    258 		KASSERT(tlb.tlb_asid == 0);
    259 		KASSERT((tlb.tlb_size & 0xaaaaa7ff) == 0);
    260 		u_int cntlz = __builtin_clz(tlb.tlb_size);
    261 		KASSERT(cntlz & 1);
    262 		KASSERT(cntlz <= 19);
    263 		hwtlb.hwtlb_mas0 = MAS0_TLBSEL_TLB1;
    264 		/*
    265 		 * TSIZE is defined (4^TSIZE) Kbytes except a TSIZE of 0 is not
    266 		 * allowed.  So 1K would be 0x00000400 giving 21 leading zero
    267 		 * bits.  Subtracting the leading number of zero bits from 21
    268 		 * and dividing by 2 gives us the number that the MMU wants.
    269 		 */
    270 		hwtlb.hwtlb_mas1 = MASX_TSIZE_MAKE(((31 - 10) - cntlz) / 2)
    271 		    | MAS1_IPROT | MAS1_V;
    272 		hwtlb.hwtlb_mas3 = prot_mask >> PTE_RWX_SHIFT;
    273 	}
    274 	/* We are done with MAS1, on to MAS2 ... */
    275 	hwtlb.hwtlb_mas2 = tlb.tlb_va | (tlb.tlb_pte & PTE_WIMGE_MASK);
    276 	hwtlb.hwtlb_mas3 |= tlb.tlb_pte & PTE_RPN_MASK;
    277 
    278 	return hwtlb;
    279 }
    280 
    281 void *
    282 e500_tlb1_fetch(size_t slot)
    283 {
    284 	struct e500_tlb1 * const tlb1 = &e500_tlb1;
    285 
    286 	return &tlb1->tlb1_entries[slot].e_hwtlb;
    287 }
    288 
    289 void
    290 e500_tlb1_sync(void)
    291 {
    292 	struct e500_tlb1 * const tlb1 = &e500_tlb1;
    293 	for (u_int slot = 1; slot < tlb1->tlb1_numentries; slot++) {
    294 		const struct e500_hwtlb * const new_hwtlb =
    295 		    &tlb1->tlb1_entries[slot].e_hwtlb;
    296 		const struct e500_hwtlb old_hwtlb =
    297 		    hwtlb_read(MAS0_TLBSEL_TLB1, slot);
    298 #define CHANGED(n,o,f)	((n)->f != (o).f)
    299 		bool mas1_changed_p = CHANGED(new_hwtlb, old_hwtlb, hwtlb_mas1);
    300 		bool mas2_changed_p = CHANGED(new_hwtlb, old_hwtlb, hwtlb_mas2);
    301 		bool mas3_changed_p = CHANGED(new_hwtlb, old_hwtlb, hwtlb_mas3);
    302 #undef CHANGED
    303 		bool new_valid_p = (new_hwtlb->hwtlb_mas1 & MAS1_V) != 0;
    304 		bool old_valid_p = (old_hwtlb.hwtlb_mas1 & MAS1_V) != 0;
    305 		if ((new_valid_p || old_valid_p)
    306 		    && (mas1_changed_p
    307 			|| (new_valid_p
    308 			    && (mas2_changed_p || mas3_changed_p))))
    309 			hwtlb_write(*new_hwtlb, true);
    310 	}
    311 }
    312 
    313 static int
    314 e500_alloc_tlb1_entry(void)
    315 {
    316 	struct e500_tlb1 * const tlb1 = &e500_tlb1;
    317 
    318 	if (tlb1->tlb1_numfree == 0)
    319 		return -1;
    320 	const u_int slot = tlb1->tlb1_freelist[--tlb1->tlb1_numfree];
    321 	KASSERT((tlb1->tlb1_entries[slot].e_hwtlb.hwtlb_mas1 & MAS1_V) == 0);
    322 	tlb1->tlb1_entries[slot].e_hwtlb.hwtlb_mas0 =
    323 	    MAS0_TLBSEL_TLB1 | __SHIFTIN(slot, MAS0_ESEL);
    324 	return (int)slot;
    325 }
    326 
    327 static void
    328 e500_free_tlb1_entry(struct e500_xtlb *xtlb, u_int slot, bool needs_sync)
    329 {
    330 	struct e500_tlb1 * const tlb1 = &e500_tlb1;
    331 	KASSERT(slot < tlb1->tlb1_numentries);
    332 	KASSERT(&tlb1->tlb1_entries[slot] == xtlb);
    333 
    334 	KASSERT(xtlb->e_hwtlb.hwtlb_mas0 == (MAS0_TLBSEL_TLB1|__SHIFTIN(slot, MAS0_ESEL)));
    335 	xtlb->e_hwtlb.hwtlb_mas1 &= ~(MAS1_V|MAS1_IPROT);
    336 	hwtlb_write(xtlb->e_hwtlb, needs_sync);
    337 
    338 	const register_t msr = wrtee(0);
    339 	tlb1->tlb1_freelist[tlb1->tlb1_numfree++] = slot;
    340 	wrtee(msr);
    341 }
    342 
    343 static tlb_asid_t
    344 e500_tlb_get_asid(void)
    345 {
    346 	return mfspr(SPR_PID0);
    347 }
    348 
    349 static void
    350 e500_tlb_set_asid(tlb_asid_t asid)
    351 {
    352 	mtspr(SPR_PID0, asid);
    353 }
    354 
    355 static void
    356 e500_tlb_invalidate_all(void)
    357 {
    358 	/*
    359 	 * This does a flash invalidate of all entries in TLB0.
    360 	 * We don't touch TLB1 since we don't expect those to be volatile.
    361 	 */
    362 #if 1
    363 	__asm volatile("tlbivax\t0, %0" :: "b"(4));	/* INV_ALL */
    364 	__asm volatile("tlbsync\n\tisync\n\tsync");
    365 #else
    366 	mtspr(SPR_MMUCSR0, MMUCSR0_TLB0_FI);
    367 	while (mfspr(SPR_MMUCSR0) != 0)
    368 		;
    369 #endif
    370 }
    371 
    372 static void
    373 e500_tlb_invalidate_globals(void)
    374 {
    375 	const size_t tlbassoc = TLBCFG_ASSOC(mftlb0cfg());
    376 	const size_t tlbentries = TLBCFG_NENTRY(mftlb0cfg());
    377 	const size_t max_epn = (tlbentries / tlbassoc) << PAGE_SHIFT;
    378 	const vaddr_t kstack_lo = (uintptr_t)curlwp->l_addr;
    379 	const vaddr_t kstack_hi = kstack_lo + USPACE - 1;
    380 	const vaddr_t epn_kstack_lo = kstack_lo & (max_epn - 1);
    381 	const vaddr_t epn_kstack_hi = kstack_hi & (max_epn - 1);
    382 
    383 	const register_t msr = wrtee(0);
    384 	for (size_t assoc = 0; assoc < tlbassoc; assoc++) {
    385 		mtspr(SPR_MAS0, MAS0_ESEL_MAKE(assoc) | MAS0_TLBSEL_TLB0);
    386 		for (size_t epn = 0; epn < max_epn; epn += PAGE_SIZE) {
    387 			mtspr(SPR_MAS2, epn);
    388 			__asm volatile("tlbre");
    389 			uint32_t mas1 = mfspr(SPR_MAS1);
    390 
    391 			/*
    392 			 * Make sure this is a valid kernel entry first.
    393 			 */
    394 			if ((mas1 & (MAS1_V|MAS1_TID|MAS1_TS)) != MAS1_V)
    395 				continue;
    396 
    397 			/*
    398 			 * We have a valid kernel TLB entry.  But if it matches
    399 			 * the stack we are currently running on, it would
    400 			 * unwise to invalidate it.  First see if the epn
    401 			 * overlaps the stack.  If it does then get the
    402 			 * VA and see if it really is part of the stack.
    403 			 */
    404 			if (epn_kstack_lo < epn_kstack_hi
    405 			    ? (epn_kstack_lo <= epn && epn <= epn_kstack_hi)
    406 			    : (epn <= epn_kstack_hi || epn_kstack_lo <= epn)) {
    407 				const uint32_t mas2_epn =
    408 				    mfspr(SPR_MAS2) & MAS2_EPN;
    409 				if (kstack_lo <= mas2_epn
    410 				    && mas2_epn <= kstack_hi)
    411 					continue;
    412 			}
    413 			mtspr(SPR_MAS1, mas1 ^ MAS1_V);
    414 			__asm volatile("tlbwe");
    415 		}
    416 	}
    417 	__asm volatile("isync\n\tsync");
    418 	wrtee(msr);
    419 }
    420 
    421 static void
    422 e500_tlb_invalidate_asids(tlb_asid_t asid_lo, tlb_asid_t asid_hi)
    423 {
    424 	const size_t tlbassoc = TLBCFG_ASSOC(mftlb0cfg());
    425 	const size_t tlbentries = TLBCFG_NENTRY(mftlb0cfg());
    426 	const size_t max_epn = (tlbentries / tlbassoc) << PAGE_SHIFT;
    427 
    428 	asid_lo = __SHIFTIN(asid_lo, MAS1_TID);
    429 	asid_hi = __SHIFTIN(asid_hi, MAS1_TID);
    430 
    431 	const register_t msr = wrtee(0);
    432 	for (size_t assoc = 0; assoc < tlbassoc; assoc++) {
    433 		mtspr(SPR_MAS0, MAS0_ESEL_MAKE(assoc) | MAS0_TLBSEL_TLB0);
    434 		for (size_t epn = 0; epn < max_epn; epn += PAGE_SIZE) {
    435 			mtspr(SPR_MAS2, epn);
    436 			__asm volatile("tlbre");
    437 			const uint32_t mas1 = mfspr(SPR_MAS1);
    438 			/*
    439 			 * If this is a valid entry for AS space 1 and
    440 			 * its asid matches the constraints of the caller,
    441 			 * clear its valid bit.
    442 			 */
    443 			if ((mas1 & (MAS1_V|MAS1_TS)) == (MAS1_V|MAS1_TS)
    444 			    && asid_lo <= (mas1 & MAS1_TID)
    445 			    && (mas1 & MAS1_TID) <= asid_hi) {
    446 				mtspr(SPR_MAS1, mas1 ^ MAS1_V);
    447 #if 0
    448 				printf("%s[%zu,%zu]->[%x]\n",
    449 				    __func__, assoc, epn, mas1);
    450 #endif
    451 				__asm volatile("tlbwe");
    452 			}
    453 		}
    454 	}
    455 	__asm volatile("isync\n\tsync");
    456 	wrtee(msr);
    457 }
    458 
    459 static u_int
    460 e500_tlb_record_asids(u_long *bitmap)
    461 {
    462 	const size_t tlbassoc = TLBCFG_ASSOC(mftlb0cfg());
    463 	const size_t tlbentries = TLBCFG_NENTRY(mftlb0cfg());
    464 	const size_t max_epn = (tlbentries / tlbassoc) << PAGE_SHIFT;
    465 	const size_t nbits = 8 * sizeof(bitmap[0]);
    466 	u_int found = 0;
    467 
    468 	const register_t msr = wrtee(0);
    469 	for (size_t assoc = 0; assoc < tlbassoc; assoc++) {
    470 		mtspr(SPR_MAS0, MAS0_ESEL_MAKE(assoc) | MAS0_TLBSEL_TLB0);
    471 		for (size_t epn = 0; epn < max_epn; epn += PAGE_SIZE) {
    472 			mtspr(SPR_MAS2, epn);
    473 			__asm volatile("tlbre");
    474 			const uint32_t mas1 = mfspr(SPR_MAS1);
    475 			/*
    476 			 * If this is a valid entry for AS space 1 and
    477 			 * its asid matches the constraints of the caller,
    478 			 * clear its valid bit.
    479 			 */
    480 			if ((mas1 & (MAS1_V|MAS1_TS)) == (MAS1_V|MAS1_TS)) {
    481 				const uint32_t asid = MASX_TID_GET(mas1);
    482 				const u_int i = asid / nbits;
    483 				const u_long mask = 1UL << (asid & (nbits - 1));
    484 				if ((bitmap[i] & mask) == 0) {
    485 					bitmap[i] |= mask;
    486 					found++;
    487 				}
    488 			}
    489 		}
    490 	}
    491 	wrtee(msr);
    492 
    493 	return found;
    494 }
    495 
    496 static void
    497 e500_tlb_invalidate_addr(vaddr_t va, tlb_asid_t asid)
    498 {
    499 	KASSERT((va & PAGE_MASK) == 0);
    500 	/*
    501 	 * Bits 60 & 61 have meaning
    502 	 */
    503 	if (asid == KERNEL_PID) {
    504 		/*
    505 		 * For data accesses, the context-synchronizing instruction
    506 		 * before tlbwe or tlbivax ensures that all memory accesses
    507 		 * due to preceding instructions have completed to a point
    508 		 * at which they have reported all exceptions they will cause.
    509 		 */
    510 		__asm volatile("isync");
    511 	}
    512 	__asm volatile("tlbivax\t0, %0" :: "b"(va));
    513 	__asm volatile("tlbsync");
    514 	__asm volatile("tlbsync");	/* Why? */
    515 	if (asid == KERNEL_PID) {
    516 		/*
    517 		 * The context-synchronizing instruction after tlbwe or tlbivax
    518 		 * ensures that subsequent accesses (data and instruction) use
    519 		 * the updated value in any TLB entries affected.
    520 		 */
    521 		__asm volatile("isync\n\tsync");
    522 	}
    523 }
    524 
    525 static bool
    526 e500_tlb_update_addr(vaddr_t va, tlb_asid_t asid, pt_entry_t pte, bool insert)
    527 {
    528 	struct e500_hwtlb hwtlb = tlb_to_hwtlb(
    529 	    (struct e500_tlb){ .tlb_va = va, .tlb_asid = asid,
    530 		.tlb_size = PAGE_SIZE, .tlb_pte = pte,});
    531 
    532 	register_t msr = wrtee(0);
    533 	mtspr(SPR_MAS6, asid ? __SHIFTIN(asid, MAS6_SPID0) | MAS6_SAS : 0);
    534 	__asm volatile("tlbsx 0, %0" :: "b"(va));
    535 	register_t mas1 = mfspr(SPR_MAS1);
    536 	if ((mas1 & MAS1_V) == 0) {
    537 		if (!insert) {
    538 			wrtee(msr);
    539 #if 0
    540 			printf("%s(%#lx,%#x,%#x,%x)<no update>\n",
    541 			    __func__, va, asid, pte, insert);
    542 #endif
    543 			return false;
    544 		}
    545 		mtspr(SPR_MAS1, hwtlb.hwtlb_mas1);
    546 	}
    547 	mtspr(SPR_MAS2, hwtlb.hwtlb_mas2);
    548 	mtspr(SPR_MAS3, hwtlb.hwtlb_mas3);
    549 	//mtspr(SPR_MAS7, 0);
    550 	__asm volatile("tlbwe");
    551 	if (asid == KERNEL_PID)
    552 		__asm volatile("isync\n\tsync");
    553 	wrtee(msr);
    554 #if 0
    555 	if (asid)
    556 	printf("%s(%#lx,%#x,%#x,%x)->[%x,%x,%x]\n",
    557 	    __func__, va, asid, pte, insert,
    558 	    hwtlb.hwtlb_mas1, hwtlb.hwtlb_mas2, hwtlb.hwtlb_mas3);
    559 #endif
    560 	return (mas1 & MAS1_V) != 0;
    561 }
    562 
    563 static void
    564 e500_tlb_write_entry(size_t index, const struct tlbmask *tlb)
    565 {
    566 }
    567 
    568 static void
    569 e500_tlb_read_entry(size_t index, struct tlbmask *tlb)
    570 {
    571 }
    572 
    573 static void
    574 e500_tlb_dump(void (*pr)(const char *, ...))
    575 {
    576 	const size_t tlbassoc = TLBCFG_ASSOC(mftlb0cfg());
    577 	const size_t tlbentries = TLBCFG_NENTRY(mftlb0cfg());
    578 	const size_t max_epn = (tlbentries / tlbassoc) << PAGE_SHIFT;
    579 	const uint32_t saved_mas0 = mfspr(SPR_MAS0);
    580 	size_t valid = 0;
    581 
    582 	if (pr == NULL)
    583 		pr = printf;
    584 
    585 	const register_t msr = wrtee(0);
    586 	for (size_t assoc = 0; assoc < tlbassoc; assoc++) {
    587 		struct e500_hwtlb hwtlb;
    588 		hwtlb.hwtlb_mas0 = MAS0_ESEL_MAKE(assoc) | MAS0_TLBSEL_TLB0;
    589 		mtspr(SPR_MAS0, hwtlb.hwtlb_mas0);
    590 		for (size_t epn = 0; epn < max_epn; epn += PAGE_SIZE) {
    591 			mtspr(SPR_MAS2, epn);
    592 			__asm volatile("tlbre");
    593 			hwtlb.hwtlb_mas1 = mfspr(SPR_MAS1);
    594 			/*
    595 			 * If this is a valid entry for AS space 1 and
    596 			 * its asid matches the constraints of the caller,
    597 			 * clear its valid bit.
    598 			 */
    599 			if (hwtlb.hwtlb_mas1 & MAS1_V) {
    600 				hwtlb.hwtlb_mas2 = mfspr(SPR_MAS2);
    601 				hwtlb.hwtlb_mas3 = mfspr(SPR_MAS3);
    602 				struct e500_tlb tlb = hwtlb_to_tlb(hwtlb);
    603 				(*pr)("[%zu,%zu]->[%x,%x,%x]",
    604 				    assoc, atop(epn),
    605 				    hwtlb.hwtlb_mas1,
    606 				    hwtlb.hwtlb_mas2,
    607 				    hwtlb.hwtlb_mas3);
    608 				(*pr)(": VA=%#lx size=4KB asid=%u pte=%x",
    609 				    tlb.tlb_va, tlb.tlb_asid, tlb.tlb_pte);
    610 				(*pr)(" (RPN=%#x,%s%s%s%s%s,%s%s%s%s%s)\n",
    611 				    tlb.tlb_pte & PTE_RPN_MASK,
    612 				    tlb.tlb_pte & PTE_xR ? "R" : "",
    613 				    tlb.tlb_pte & PTE_xW ? "W" : "",
    614 				    tlb.tlb_pte & PTE_UNMODIFIED ? "*" : "",
    615 				    tlb.tlb_pte & PTE_xX ? "X" : "",
    616 				    tlb.tlb_pte & PTE_UNSYNCED ? "*" : "",
    617 				    tlb.tlb_pte & PTE_W ? "W" : "",
    618 				    tlb.tlb_pte & PTE_I ? "I" : "",
    619 				    tlb.tlb_pte & PTE_M ? "M" : "",
    620 				    tlb.tlb_pte & PTE_G ? "G" : "",
    621 				    tlb.tlb_pte & PTE_E ? "E" : "");
    622 				valid++;
    623 			}
    624 		}
    625 	}
    626 	mtspr(SPR_MAS0, saved_mas0);
    627 	wrtee(msr);
    628 	(*pr)("%s: %zu valid entries\n", __func__, valid);
    629 }
    630 
    631 static void
    632 e500_tlb_walk(void *ctx, bool (*func)(void *, vaddr_t, uint32_t, uint32_t))
    633 {
    634 	const size_t tlbassoc = TLBCFG_ASSOC(mftlb0cfg());
    635 	const size_t tlbentries = TLBCFG_NENTRY(mftlb0cfg());
    636 	const size_t max_epn = (tlbentries / tlbassoc) << PAGE_SHIFT;
    637 	const uint32_t saved_mas0 = mfspr(SPR_MAS0);
    638 
    639 	const register_t msr = wrtee(0);
    640 	for (size_t assoc = 0; assoc < tlbassoc; assoc++) {
    641 		struct e500_hwtlb hwtlb;
    642 		hwtlb.hwtlb_mas0 = MAS0_ESEL_MAKE(assoc) | MAS0_TLBSEL_TLB0;
    643 		mtspr(SPR_MAS0, hwtlb.hwtlb_mas0);
    644 		for (size_t epn = 0; epn < max_epn; epn += PAGE_SIZE) {
    645 			mtspr(SPR_MAS2, epn);
    646 			__asm volatile("tlbre");
    647 			hwtlb.hwtlb_mas1 = mfspr(SPR_MAS1);
    648 			if (hwtlb.hwtlb_mas1 & MAS1_V) {
    649 				hwtlb.hwtlb_mas2 = mfspr(SPR_MAS2);
    650 				hwtlb.hwtlb_mas3 = mfspr(SPR_MAS3);
    651 				struct e500_tlb tlb = hwtlb_to_tlb(hwtlb);
    652 				if (!(*func)(ctx, tlb.tlb_va, tlb.tlb_asid,
    653 				    tlb.tlb_pte))
    654 					break;
    655 			}
    656 		}
    657 	}
    658 	mtspr(SPR_MAS0, saved_mas0);
    659 	wrtee(msr);
    660 }
    661 
    662 static struct e500_xtlb *
    663 e500_tlb_lookup_xtlb_pa(vaddr_t pa, u_int *slotp)
    664 {
    665 	struct e500_tlb1 * const tlb1 = &e500_tlb1;
    666 	struct e500_xtlb *xtlb = tlb1->tlb1_entries;
    667 
    668 	/*
    669 	 * See if we have a TLB entry for the pa.
    670 	 */
    671 	for (u_int i = 0; i < tlb1->tlb1_numentries; i++, xtlb++) {
    672 		psize_t mask = ~(xtlb->e_tlb.tlb_size - 1);
    673 		if ((xtlb->e_hwtlb.hwtlb_mas1 & MAS1_V)
    674 		    && ((pa ^ xtlb->e_tlb.tlb_pte) & mask) == 0) {
    675 			if (slotp != NULL)
    676 				*slotp = i;
    677 			return xtlb;
    678 		}
    679 	}
    680 
    681 	return NULL;
    682 }
    683 
    684 struct e500_xtlb *
    685 e500_tlb_lookup_xtlb(vaddr_t va, u_int *slotp)
    686 {
    687 	struct e500_tlb1 * const tlb1 = &e500_tlb1;
    688 	struct e500_xtlb *xtlb = tlb1->tlb1_entries;
    689 
    690 	/*
    691 	 * See if we have a TLB entry for the va.
    692 	 */
    693 	for (u_int i = 0; i < tlb1->tlb1_numentries; i++, xtlb++) {
    694 		vsize_t mask = ~(xtlb->e_tlb.tlb_size - 1);
    695 		if ((xtlb->e_hwtlb.hwtlb_mas1 & MAS1_V)
    696 		    && ((va ^ xtlb->e_tlb.tlb_va) & mask) == 0) {
    697 			if (slotp != NULL)
    698 				*slotp = i;
    699 			return xtlb;
    700 		}
    701 	}
    702 
    703 	return NULL;
    704 }
    705 
    706 static struct e500_xtlb *
    707 e500_tlb_lookup_xtlb2(vaddr_t va, vsize_t len)
    708 {
    709 	struct e500_tlb1 * const tlb1 = &e500_tlb1;
    710 	struct e500_xtlb *xtlb = tlb1->tlb1_entries;
    711 
    712 	/*
    713 	 * See if we have a TLB entry for the pa.
    714 	 */
    715 	for (u_int i = 0; i < tlb1->tlb1_numentries; i++, xtlb++) {
    716 		vsize_t mask = ~(xtlb->e_tlb.tlb_size - 1);
    717 		if ((xtlb->e_hwtlb.hwtlb_mas1 & MAS1_V)
    718 		    && ((va ^ xtlb->e_tlb.tlb_va) & mask) == 0
    719 		    && (((va + len - 1) ^ va) & mask) == 0) {
    720 			return xtlb;
    721 		}
    722 	}
    723 
    724 	return NULL;
    725 }
    726 
    727 static void *
    728 e500_tlb_mapiodev(paddr_t pa, psize_t len, bool prefetchable)
    729 {
    730 	struct e500_xtlb * const xtlb = e500_tlb_lookup_xtlb_pa(pa, NULL);
    731 
    732 	/*
    733 	 * See if we have a TLB entry for the pa.  If completely falls within
    734 	 * mark the reference and return the pa.  But only if the tlb entry
    735 	 * is not cacheable.
    736 	 */
    737 	if (xtlb
    738 	    && (prefetchable
    739 		|| (xtlb->e_tlb.tlb_pte & PTE_WIG) == (PTE_I|PTE_G))) {
    740 		xtlb->e_refcnt++;
    741 		return (void *) (xtlb->e_tlb.tlb_va
    742 		    + pa - (xtlb->e_tlb.tlb_pte & PTE_RPN_MASK));
    743 	}
    744 	return NULL;
    745 }
    746 
    747 static void
    748 e500_tlb_unmapiodev(vaddr_t va, vsize_t len)
    749 {
    750 	if (va < VM_MIN_KERNEL_ADDRESS || VM_MAX_KERNEL_ADDRESS <= va) {
    751 		struct e500_xtlb * const xtlb = e500_tlb_lookup_xtlb(va, NULL);
    752 		if (xtlb)
    753 			xtlb->e_refcnt--;
    754 	}
    755 }
    756 
    757 static int
    758 e500_tlb_ioreserve(vaddr_t va, vsize_t len, pt_entry_t pte)
    759 {
    760 	struct e500_tlb1 * const tlb1 = &e500_tlb1;
    761 	struct e500_xtlb *xtlb;
    762 
    763 	KASSERT(len & 0x55555000);
    764 	KASSERT((len & ~0x55555000) == 0);
    765 	KASSERT(len >= PAGE_SIZE);
    766 	KASSERT((len & (len - 1)) == 0);
    767 	KASSERT((va & (len - 1)) == 0);
    768 	KASSERT(((pte & PTE_RPN_MASK) & (len - 1)) == 0);
    769 
    770 	if ((xtlb = e500_tlb_lookup_xtlb2(va, len)) != NULL) {
    771 		psize_t mask = ~(xtlb->e_tlb.tlb_size - 1);
    772 		KASSERT(len <= xtlb->e_tlb.tlb_size);
    773 		KASSERT((pte & mask) == (xtlb->e_tlb.tlb_pte & mask));
    774 		xtlb->e_refcnt++;
    775 		return 0;
    776 	}
    777 
    778 	const int slot = e500_alloc_tlb1_entry();
    779 	if (slot < 0)
    780 		return ENOMEM;
    781 
    782 	xtlb = &tlb1->tlb1_entries[slot];
    783 	xtlb->e_tlb.tlb_va = va;
    784 	xtlb->e_tlb.tlb_size = len;
    785 	xtlb->e_tlb.tlb_pte = pte;
    786 	xtlb->e_tlb.tlb_asid = KERNEL_PID;
    787 
    788 	xtlb->e_hwtlb = tlb_to_hwtlb(xtlb->e_tlb);
    789 	xtlb->e_hwtlb.hwtlb_mas0 |= __SHIFTIN(slot, MAS0_ESEL);
    790 	hwtlb_write(xtlb->e_hwtlb, true);
    791 	return 0;
    792 }
    793 
    794 static int
    795 e500_tlb_iorelease(vaddr_t va)
    796 {
    797 	u_int slot;
    798 	struct e500_xtlb * const xtlb = e500_tlb_lookup_xtlb(va, &slot);
    799 
    800 	if (xtlb == NULL)
    801 		return ENOENT;
    802 
    803 	if (xtlb->e_refcnt)
    804 		return EBUSY;
    805 
    806 	e500_free_tlb1_entry(xtlb, slot, true);
    807 
    808 	return 0;
    809 }
    810 
    811 static u_int
    812 e500_tlbmemmap(paddr_t memstart, psize_t memsize, struct e500_tlb1 *tlb1)
    813 {
    814 	u_int slotmask = 0;
    815 	u_int slots = 0, nextslot = 0;
    816 	KASSERT(tlb1->tlb1_numfree > 1);
    817 	KASSERT(((memstart + memsize - 1) & -memsize) == memstart);
    818 	for (paddr_t lastaddr = memstart; 0 < memsize; ) {
    819 		u_int cnt = __builtin_clz(memsize);
    820 		psize_t size = min(1UL << (31 - (cnt | 1)), tlb1->tlb1_maxsize);
    821 		slots += memsize / size;
    822 		if (slots > 4)
    823 			panic("%s: %d: can't map memory (%#lx) into TLB1: %s",
    824 			    __func__, __LINE__, memsize, "too fragmented");
    825 		if (slots > tlb1->tlb1_numfree - 1)
    826 			panic("%s: %d: can't map memory (%#lx) into TLB1: %s",
    827 			    __func__, __LINE__, memsize,
    828 			    "insufficent TLB entries");
    829 		for (; nextslot < slots; nextslot++) {
    830 			const u_int freeslot = e500_alloc_tlb1_entry();
    831 			struct e500_xtlb * const xtlb =
    832 			    &tlb1->tlb1_entries[freeslot];
    833 			xtlb->e_tlb.tlb_asid = KERNEL_PID;
    834 			xtlb->e_tlb.tlb_size = size;
    835 			xtlb->e_tlb.tlb_va = lastaddr;
    836 			xtlb->e_tlb.tlb_pte = lastaddr
    837 			    | PTE_M | PTE_xX | PTE_xW | PTE_xR;
    838 			lastaddr += size;
    839 			memsize -= size;
    840 			slotmask |= 1 << (31 - freeslot); /* clz friendly */
    841 		}
    842 	}
    843 
    844 	return nextslot;
    845 }
    846 
    847 static const struct tlb_md_ops e500_tlb_ops = {
    848 	.md_tlb_get_asid = e500_tlb_get_asid,
    849 	.md_tlb_set_asid = e500_tlb_set_asid,
    850 	.md_tlb_invalidate_all = e500_tlb_invalidate_all,
    851 	.md_tlb_invalidate_globals = e500_tlb_invalidate_globals,
    852 	.md_tlb_invalidate_asids = e500_tlb_invalidate_asids,
    853 	.md_tlb_invalidate_addr = e500_tlb_invalidate_addr,
    854 	.md_tlb_update_addr = e500_tlb_update_addr,
    855 	.md_tlb_record_asids = e500_tlb_record_asids,
    856 	.md_tlb_write_entry = e500_tlb_write_entry,
    857 	.md_tlb_read_entry = e500_tlb_read_entry,
    858 	.md_tlb_dump = e500_tlb_dump,
    859 	.md_tlb_walk = e500_tlb_walk,
    860 };
    861 
    862 static const struct tlb_md_io_ops e500_tlb_io_ops = {
    863 	.md_tlb_mapiodev = e500_tlb_mapiodev,
    864 	.md_tlb_unmapiodev = e500_tlb_unmapiodev,
    865 	.md_tlb_ioreserve = e500_tlb_ioreserve,
    866 	.md_tlb_iorelease = e500_tlb_iorelease,
    867 };
    868 
    869 void
    870 e500_tlb_init(vaddr_t endkernel, psize_t memsize)
    871 {
    872 	struct e500_tlb1 * const tlb1 = &e500_tlb1;
    873 
    874 #if 0
    875 	register_t mmucfg = mfspr(SPR_MMUCFG);
    876 	register_t mas4 = mfspr(SPR_MAS4);
    877 #endif
    878 
    879 	const uint32_t tlb1cfg = mftlb1cfg();
    880 	tlb1->tlb1_numentries = TLBCFG_NENTRY(tlb1cfg);
    881 	KASSERT(tlb1->tlb1_numentries <= __arraycount(tlb1->tlb1_entries));
    882 	/*
    883 	 * Limit maxsize to 1G since 4G isn't really useful to us.
    884 	 */
    885 	tlb1->tlb1_minsize = 1024 << (2 * TLBCFG_MINSIZE(tlb1cfg));
    886 	tlb1->tlb1_maxsize = 1024 << (2 * min(10, TLBCFG_MAXSIZE(tlb1cfg)));
    887 
    888 #ifdef VERBOSE_INITPPC
    889 	printf(" tlb1cfg=%#x numentries=%u minsize=%#xKB maxsize=%#xKB",
    890 	    tlb1cfg, tlb1->tlb1_numentries, tlb1->tlb1_minsize >> 10,
    891 	    tlb1->tlb1_maxsize >> 10);
    892 #endif
    893 
    894 	/*
    895 	 * Let's see what's in TLB1 and we need to invalidate any entry that
    896 	 * would fit within the kernel's mapped address space.
    897 	 */
    898 	psize_t memmapped = 0;
    899 	for (u_int i = 0; i < tlb1->tlb1_numentries; i++) {
    900 		struct e500_xtlb * const xtlb = &tlb1->tlb1_entries[i];
    901 
    902 		xtlb->e_hwtlb = hwtlb_read(MAS0_TLBSEL_TLB1, i);
    903 
    904 		if ((xtlb->e_hwtlb.hwtlb_mas1 & MAS1_V) == 0) {
    905 			tlb1->tlb1_freelist[tlb1->tlb1_numfree++] = i;
    906 #ifdef VERBOSE_INITPPC
    907 			printf(" TLB1[%u]=<unused>", i);
    908 #endif
    909 			continue;
    910 		}
    911 
    912 		xtlb->e_tlb = hwtlb_to_tlb(xtlb->e_hwtlb);
    913 #ifdef VERBOSE_INITPPC
    914 		printf(" TLB1[%u]=<%#lx,%#lx,%#x,%#x>",
    915 		    i, xtlb->e_tlb.tlb_va, xtlb->e_tlb.tlb_size,
    916 		    xtlb->e_tlb.tlb_asid, xtlb->e_tlb.tlb_pte);
    917 #endif
    918 		if ((VM_MIN_KERNEL_ADDRESS <= xtlb->e_tlb.tlb_va
    919 		    && xtlb->e_tlb.tlb_va < VM_MAX_KERNEL_ADDRESS)
    920 		    || (xtlb->e_tlb.tlb_va < VM_MIN_KERNEL_ADDRESS
    921 		        && VM_MIN_KERNEL_ADDRESS <
    922 			   xtlb->e_tlb.tlb_va + xtlb->e_tlb.tlb_size)) {
    923 #ifdef VERBOSE_INITPPC
    924 			printf("free");
    925 #endif
    926 			e500_free_tlb1_entry(xtlb, i, false);
    927 #ifdef VERBOSE_INITPPC
    928 			printf("d");
    929 #endif
    930 			continue;
    931 		}
    932 		if ((xtlb->e_hwtlb.hwtlb_mas1 & MAS1_IPROT) == 0) {
    933 			xtlb->e_hwtlb.hwtlb_mas1 |= MAS1_IPROT;
    934 			hwtlb_write(xtlb->e_hwtlb, false);
    935 #ifdef VERBOSE_INITPPC
    936 			printf("+iprot");
    937 #endif
    938 		}
    939 		if (xtlb->e_tlb.tlb_pte & PTE_I)
    940 			continue;
    941 
    942 		if (xtlb->e_tlb.tlb_va == 0
    943 		    || xtlb->e_tlb.tlb_va + xtlb->e_tlb.tlb_size <= memsize) {
    944 			memmapped += xtlb->e_tlb.tlb_size;
    945 			/*
    946 			 * Let make sure main memory is setup so it's memory
    947 			 * coherent.  For some reason u-boot doesn't set it up
    948 			 * that way.
    949 			 */
    950 			if ((xtlb->e_hwtlb.hwtlb_mas2 & MAS2_M) == 0) {
    951 				xtlb->e_hwtlb.hwtlb_mas2 |= MAS2_M;
    952 				hwtlb_write(xtlb->e_hwtlb, true);
    953 			}
    954 		}
    955 	}
    956 
    957 	cpu_md_ops.md_tlb_ops = &e500_tlb_ops;
    958 	cpu_md_ops.md_tlb_io_ops = &e500_tlb_io_ops;
    959 
    960 	if (__predict_false(memmapped < memsize)) {
    961 		/*
    962 		 * Let's see how many TLB entries are needed to map memory.
    963 		 */
    964 		u_int slotmask = e500_tlbmemmap(0, memsize, tlb1);
    965 
    966 		/*
    967 		 * To map main memory into the TLB, we need to flush any
    968 		 * existing entries from the TLB that overlap the virtual
    969 		 * address space needed to map physical memory.  That may
    970 		 * include the entries for the pages currently used by the
    971 		 * stack or that we are executing.  So to avoid problems, we
    972 		 * are going to temporarily map the kernel and stack into AS 1,
    973 		 * switch to it, and clear out the TLB entries from AS 0,
    974 		 * install the new TLB entries to map memory, and then switch
    975 		 * back to AS 0 and free the temp entry used for AS1.
    976 		 */
    977 		u_int b = __builtin_clz(endkernel);
    978 
    979 		/*
    980 		 * If the kernel doesn't end on a clean power of 2, we need
    981 		 * to round the size up (by decrementing the number of leading
    982 		 * zero bits).  If the size isn't a power of 4KB, decrement
    983 		 * again to make it one.
    984 		 */
    985 		if (endkernel & (endkernel - 1))
    986 			b--;
    987 		if ((b & 1) == 0)
    988 			b--;
    989 
    990 		/*
    991 		 * Create a TLB1 mapping for the kernel in AS1.
    992 		 */
    993 		const u_int kslot = e500_alloc_tlb1_entry();
    994 		struct e500_xtlb * const kxtlb = &tlb1->tlb1_entries[kslot];
    995 		kxtlb->e_tlb.tlb_va = 0;
    996 		kxtlb->e_tlb.tlb_size = 1UL << (31 - b);
    997 		kxtlb->e_tlb.tlb_pte = PTE_M|PTE_xR|PTE_xW|PTE_xX;
    998 		kxtlb->e_tlb.tlb_asid = KERNEL_PID;
    999 
   1000 		kxtlb->e_hwtlb = tlb_to_hwtlb(kxtlb->e_tlb);
   1001 		kxtlb->e_hwtlb.hwtlb_mas0 |= __SHIFTIN(kslot, MAS0_ESEL);
   1002 		kxtlb->e_hwtlb.hwtlb_mas1 |= MAS1_TS;
   1003 		hwtlb_write(kxtlb->e_hwtlb, true);
   1004 
   1005 		/*
   1006 		 * Now that we have a TLB mapping in AS1 for the kernel and its
   1007 		 * stack, we switch to AS1 to cleanup the TLB mappings for TLB0.
   1008 		 */
   1009 		const register_t saved_msr = mfmsr();
   1010 		mtmsr(saved_msr | PSL_DS | PSL_IS);
   1011 		__asm volatile("isync");
   1012 
   1013 		/*
   1014 		 *** Invalidate all the TLB0 entries.
   1015 		 */
   1016 		e500_tlb_invalidate_all();
   1017 
   1018 		/*
   1019 		 *** Now let's see if we have any entries in TLB1 that would
   1020 		 *** overlap the ones we are about to install.  If so, nuke 'em.
   1021 		 */
   1022 		for (u_int i = 0; i < tlb1->tlb1_numentries; i++) {
   1023 			struct e500_xtlb * const xtlb = &tlb1->tlb1_entries[i];
   1024 			struct e500_hwtlb * const hwtlb = &xtlb->e_hwtlb;
   1025 			if ((hwtlb->hwtlb_mas1 & (MAS1_V|MAS1_TS)) == MAS1_V
   1026 			    && (hwtlb->hwtlb_mas2 & MAS2_EPN) < memsize) {
   1027 				e500_free_tlb1_entry(xtlb, i, false);
   1028 			}
   1029 		}
   1030 
   1031 		/*
   1032 		 *** Now we can add the TLB entries that will map physical
   1033 		 *** memory.  If bit 0 [MSB] in slotmask is set, then tlb
   1034 		 *** entry 0 contains a mapping for physical memory...
   1035 		 */
   1036 		struct e500_xtlb *entries = tlb1->tlb1_entries;
   1037 		while (slotmask != 0) {
   1038 			const u_int slot = __builtin_clz(slotmask);
   1039 			hwtlb_write(entries[slot].e_hwtlb, false);
   1040 			entries += slot + 1;
   1041 			slotmask <<= slot + 1;
   1042 		}
   1043 
   1044 		/*
   1045 		 *** Synchronize the TLB and the instruction stream.
   1046 		 */
   1047 		__asm volatile("tlbsync");
   1048 		__asm volatile("isync");
   1049 
   1050 		/*
   1051 		 *** Switch back to AS 0.
   1052 		 */
   1053 		mtmsr(saved_msr);
   1054 		__asm volatile("isync");
   1055 
   1056 		/*
   1057 		 * Free the temporary TLB1 entry.
   1058 		 */
   1059 		e500_free_tlb1_entry(kxtlb, kslot, true);
   1060 	}
   1061 
   1062 	/*
   1063 	 * Finally set the MAS4 defaults.
   1064 	 */
   1065 	mtspr(SPR_MAS4, MAS4_TSIZED_4KB | MAS4_MD);
   1066 
   1067 	/*
   1068 	 * Invalidate all the TLB0 entries.
   1069 	 */
   1070 	e500_tlb_invalidate_all();
   1071 }
   1072 
   1073 void
   1074 e500_tlb_minimize(vaddr_t endkernel)
   1075 {
   1076 #ifdef PMAP_MINIMALTLB
   1077 	struct e500_tlb1 * const tlb1 = &e500_tlb1;
   1078 	extern uint32_t _fdata[];
   1079 
   1080 	u_int slot;
   1081 
   1082 	paddr_t boot_page = cpu_read_4(GUR_BPTR);
   1083 	if (boot_page & BPTR_EN) {
   1084 		/*
   1085 		 * shift it to an address
   1086 		 */
   1087 		boot_page = (boot_page & BPTR_BOOT_PAGE) << PAGE_SHIFT;
   1088 		pmap_kvptefill(boot_page, boot_page + NBPG,
   1089 		    PTE_M | PTE_xR | PTE_xW | PTE_xX);
   1090 	}
   1091 
   1092 
   1093 	KASSERT(endkernel - (uintptr_t)_fdata < 0x400000);
   1094 	KASSERT((uintptr_t)_fdata == 0x400000);
   1095 
   1096 	struct e500_xtlb *xtlb = e500_tlb_lookup_xtlb(endkernel, &slot);
   1097 
   1098 	KASSERT(xtlb == e500_tlb_lookup_xtlb2(0, endkernel));
   1099 	const u_int tmp_slot = e500_alloc_tlb1_entry();
   1100 	KASSERT(tmp_slot != (u_int) -1);
   1101 
   1102 	struct e500_xtlb * const tmp_xtlb = &tlb1->tlb1_entries[tmp_slot];
   1103 	tmp_xtlb->e_tlb = xtlb->e_tlb;
   1104 	tmp_xtlb->e_hwtlb = tlb_to_hwtlb(tmp_xtlb->e_tlb);
   1105 	tmp_xtlb->e_hwtlb.hwtlb_mas1 |= MAS1_TS;
   1106 	KASSERT((tmp_xtlb->e_hwtlb.hwtlb_mas0 & MAS0_TLBSEL) == MAS0_TLBSEL_TLB1);
   1107 	tmp_xtlb->e_hwtlb.hwtlb_mas0 |= __SHIFTIN(tmp_slot, MAS0_ESEL);
   1108 	hwtlb_write(tmp_xtlb->e_hwtlb, true);
   1109 
   1110 	const u_int text_slot = e500_alloc_tlb1_entry();
   1111 	KASSERT(text_slot != (u_int)-1);
   1112 	struct e500_xtlb * const text_xtlb = &tlb1->tlb1_entries[text_slot];
   1113 	text_xtlb->e_tlb.tlb_va = 0;
   1114 	text_xtlb->e_tlb.tlb_size = 0x400000;
   1115 	text_xtlb->e_tlb.tlb_pte = PTE_M | PTE_xR | PTE_xX | text_xtlb->e_tlb.tlb_va;
   1116 	text_xtlb->e_tlb.tlb_asid = 0;
   1117 	text_xtlb->e_hwtlb = tlb_to_hwtlb(text_xtlb->e_tlb);
   1118 	KASSERT((text_xtlb->e_hwtlb.hwtlb_mas0 & MAS0_TLBSEL) == MAS0_TLBSEL_TLB1);
   1119 	text_xtlb->e_hwtlb.hwtlb_mas0 |= __SHIFTIN(text_slot, MAS0_ESEL);
   1120 
   1121 	const u_int data_slot = e500_alloc_tlb1_entry();
   1122 	KASSERT(data_slot != (u_int)-1);
   1123 	struct e500_xtlb * const data_xtlb = &tlb1->tlb1_entries[data_slot];
   1124 	data_xtlb->e_tlb.tlb_va = 0x400000;
   1125 	data_xtlb->e_tlb.tlb_size = 0x400000;
   1126 	data_xtlb->e_tlb.tlb_pte = PTE_M | PTE_xR | PTE_xW | data_xtlb->e_tlb.tlb_va;
   1127 	data_xtlb->e_tlb.tlb_asid = 0;
   1128 	data_xtlb->e_hwtlb = tlb_to_hwtlb(data_xtlb->e_tlb);
   1129 	KASSERT((data_xtlb->e_hwtlb.hwtlb_mas0 & MAS0_TLBSEL) == MAS0_TLBSEL_TLB1);
   1130 	data_xtlb->e_hwtlb.hwtlb_mas0 |= __SHIFTIN(data_slot, MAS0_ESEL);
   1131 
   1132 	const register_t msr = mfmsr();
   1133 	const register_t ts_msr = (msr | PSL_DS | PSL_IS) & ~PSL_EE;
   1134 
   1135 	__asm __volatile(
   1136 		"mtmsr	%[ts_msr]"	"\n\t"
   1137 		"sync"			"\n\t"
   1138 		"isync"
   1139 	    ::	[ts_msr] "r" (ts_msr));
   1140 
   1141 #if 0
   1142 	hwtlb_write(text_xtlb->e_hwtlb, false);
   1143 	hwtlb_write(data_xtlb->e_hwtlb, false);
   1144 	e500_free_tlb1_entry(xtlb, slot, true);
   1145 #endif
   1146 
   1147 	__asm __volatile(
   1148 		"mtmsr	%[msr]"		"\n\t"
   1149 		"sync"			"\n\t"
   1150 		"isync"
   1151 	    ::	[msr] "r" (msr));
   1152 
   1153 	e500_free_tlb1_entry(tmp_xtlb, tmp_slot, true);
   1154 #endif	/* PMAP_MINIMALTLB */
   1155 }
   1156