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