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