xref: /onnv-gate/usr/src/uts/sun4/vm/sfmmu.c (revision 2241:592fbc504a44)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2241Shuah  * Common Development and Distribution License (the "License").
6*2241Shuah  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*2241Shuah  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <vm/hat.h>
300Sstevel@tonic-gate #include <vm/hat_sfmmu.h>
310Sstevel@tonic-gate #include <vm/page.h>
320Sstevel@tonic-gate #include <sys/pte.h>
330Sstevel@tonic-gate #include <sys/systm.h>
340Sstevel@tonic-gate #include <sys/mman.h>
350Sstevel@tonic-gate #include <sys/sysmacros.h>
360Sstevel@tonic-gate #include <sys/machparam.h>
370Sstevel@tonic-gate #include <sys/vtrace.h>
380Sstevel@tonic-gate #include <sys/kmem.h>
390Sstevel@tonic-gate #include <sys/mmu.h>
400Sstevel@tonic-gate #include <sys/cmn_err.h>
410Sstevel@tonic-gate #include <sys/cpu.h>
420Sstevel@tonic-gate #include <sys/cpuvar.h>
430Sstevel@tonic-gate #include <sys/debug.h>
440Sstevel@tonic-gate #include <sys/lgrp.h>
450Sstevel@tonic-gate #include <sys/archsystm.h>
460Sstevel@tonic-gate #include <sys/machsystm.h>
470Sstevel@tonic-gate #include <sys/vmsystm.h>
480Sstevel@tonic-gate #include <sys/bitmap.h>
490Sstevel@tonic-gate #include <vm/as.h>
500Sstevel@tonic-gate #include <vm/seg.h>
510Sstevel@tonic-gate #include <vm/seg_kmem.h>
520Sstevel@tonic-gate #include <vm/seg_kp.h>
530Sstevel@tonic-gate #include <vm/seg_kpm.h>
540Sstevel@tonic-gate #include <vm/rm.h>
550Sstevel@tonic-gate #include <vm/vm_dep.h>
560Sstevel@tonic-gate #include <sys/t_lock.h>
570Sstevel@tonic-gate #include <sys/vm_machparam.h>
580Sstevel@tonic-gate #include <sys/promif.h>
590Sstevel@tonic-gate #include <sys/prom_isa.h>
600Sstevel@tonic-gate #include <sys/prom_plat.h>
610Sstevel@tonic-gate #include <sys/prom_debug.h>
620Sstevel@tonic-gate #include <sys/privregs.h>
630Sstevel@tonic-gate #include <sys/bootconf.h>
640Sstevel@tonic-gate #include <sys/memlist.h>
650Sstevel@tonic-gate #include <sys/memlist_plat.h>
660Sstevel@tonic-gate #include <sys/cpu_module.h>
670Sstevel@tonic-gate #include <sys/reboot.h>
680Sstevel@tonic-gate #include <sys/kdi.h>
690Sstevel@tonic-gate 
700Sstevel@tonic-gate /*
710Sstevel@tonic-gate  * Static routines
720Sstevel@tonic-gate  */
730Sstevel@tonic-gate static void	sfmmu_map_prom_mappings(struct translation *, size_t);
740Sstevel@tonic-gate static struct translation *read_prom_mappings(size_t *);
750Sstevel@tonic-gate static void	sfmmu_reloc_trap_handler(void *, void *, size_t);
760Sstevel@tonic-gate 
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate  * External routines
790Sstevel@tonic-gate  */
800Sstevel@tonic-gate extern void sfmmu_remap_kernel(void);
810Sstevel@tonic-gate extern void sfmmu_patch_utsb(void);
820Sstevel@tonic-gate 
830Sstevel@tonic-gate /*
840Sstevel@tonic-gate  * Global Data:
850Sstevel@tonic-gate  */
860Sstevel@tonic-gate extern caddr_t	textva, datava;
870Sstevel@tonic-gate extern tte_t	ktext_tte, kdata_tte;	/* ttes for kernel text and data */
880Sstevel@tonic-gate extern int	enable_bigktsb;
890Sstevel@tonic-gate 
900Sstevel@tonic-gate uint64_t memsegspa = (uintptr_t)MSEG_NULLPTR_PA; /* memsegs physical linkage */
910Sstevel@tonic-gate uint64_t memseg_phash[N_MEM_SLOTS];	/* use physical memseg addresses */
920Sstevel@tonic-gate 
930Sstevel@tonic-gate int	sfmmu_kern_mapped = 0;
940Sstevel@tonic-gate 
950Sstevel@tonic-gate /*
960Sstevel@tonic-gate  * DMMU primary context register for the kernel context. Machine specific code
970Sstevel@tonic-gate  * inserts correct page size codes when necessary
980Sstevel@tonic-gate  */
990Sstevel@tonic-gate uint64_t kcontextreg = KCONTEXT;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate /* Extern Global Data */
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate extern int page_relocate_ready;
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate /*
1060Sstevel@tonic-gate  * Controls the logic which enables the use of the
1070Sstevel@tonic-gate  * QUAD_LDD_PHYS ASI for TSB accesses.
1080Sstevel@tonic-gate  */
1090Sstevel@tonic-gate extern int	ktsb_phys;
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate /*
1120Sstevel@tonic-gate  * Global Routines called from within:
1130Sstevel@tonic-gate  *	usr/src/uts/sun4u
1140Sstevel@tonic-gate  *	usr/src/uts/sfmmu
1150Sstevel@tonic-gate  *	usr/src/uts/sun
1160Sstevel@tonic-gate  */
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate pfn_t
1190Sstevel@tonic-gate va_to_pfn(void *vaddr)
1200Sstevel@tonic-gate {
1210Sstevel@tonic-gate 	u_longlong_t physaddr;
1220Sstevel@tonic-gate 	int mode, valid;
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	if (tba_taken_over)
1250Sstevel@tonic-gate 		return (hat_getpfnum(kas.a_hat, (caddr_t)vaddr));
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	if ((prom_translate_virt(vaddr, &valid, &physaddr, &mode) != -1) &&
1280Sstevel@tonic-gate 	    (valid == -1)) {
1290Sstevel@tonic-gate 		return ((pfn_t)(physaddr >> MMU_PAGESHIFT));
1300Sstevel@tonic-gate 	}
1310Sstevel@tonic-gate 	return (PFN_INVALID);
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate uint64_t
1350Sstevel@tonic-gate va_to_pa(void *vaddr)
1360Sstevel@tonic-gate {
1370Sstevel@tonic-gate 	pfn_t pfn;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	if ((pfn = va_to_pfn(vaddr)) == PFN_INVALID)
1400Sstevel@tonic-gate 		return ((uint64_t)-1);
1410Sstevel@tonic-gate 	return (((uint64_t)pfn << MMU_PAGESHIFT) |
1420Sstevel@tonic-gate 		((uint64_t)vaddr & MMU_PAGEOFFSET));
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate void
1460Sstevel@tonic-gate hat_kern_setup(void)
1470Sstevel@tonic-gate {
1480Sstevel@tonic-gate 	struct translation *trans_root;
1490Sstevel@tonic-gate 	size_t ntrans_root;
1500Sstevel@tonic-gate 	extern void startup_fixup_physavail(void);
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	/*
1530Sstevel@tonic-gate 	 * These are the steps we take to take over the mmu from the prom.
1540Sstevel@tonic-gate 	 *
1550Sstevel@tonic-gate 	 * (1)	Read the prom's mappings through the translation property.
1560Sstevel@tonic-gate 	 * (2)	Remap the kernel text and kernel data with 2 locked 4MB ttes.
1570Sstevel@tonic-gate 	 *	Create the the hmeblks for these 2 ttes at this time.
1580Sstevel@tonic-gate 	 * (3)	Create hat structures for all other prom mappings.  Since the
1590Sstevel@tonic-gate 	 *	kernel text and data hme_blks have already been created we
1600Sstevel@tonic-gate 	 *	skip the equivalent prom's mappings.
1610Sstevel@tonic-gate 	 * (4)	Initialize the tsb and its corresponding hardware regs.
1620Sstevel@tonic-gate 	 * (5)	Take over the trap table (currently in startup).
1630Sstevel@tonic-gate 	 * (6)	Up to this point it is possible the prom required some of its
1640Sstevel@tonic-gate 	 *	locked tte's.  Now that we own the trap table we remove them.
1650Sstevel@tonic-gate 	 */
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	ktsb_pbase = va_to_pa(ktsb_base);
1680Sstevel@tonic-gate 	ktsb4m_pbase = va_to_pa(ktsb4m_base);
1690Sstevel@tonic-gate 	PRM_DEBUG(ktsb_pbase);
1700Sstevel@tonic-gate 	PRM_DEBUG(ktsb4m_pbase);
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	sfmmu_setup_4lp();
1730Sstevel@tonic-gate 	sfmmu_patch_ktsb();
1740Sstevel@tonic-gate 	sfmmu_patch_utsb();
1750Sstevel@tonic-gate 	sfmmu_patch_mmu_asi(ktsb_phys);
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	sfmmu_init_tsbs();
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	if (kpm_enable) {
1800Sstevel@tonic-gate 		sfmmu_kpm_patch_tlbm();
1810Sstevel@tonic-gate 		if (kpm_smallpages == 0) {
1820Sstevel@tonic-gate 			sfmmu_kpm_patch_tsbm();
1830Sstevel@tonic-gate 		}
1840Sstevel@tonic-gate 	}
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	/*
1870Sstevel@tonic-gate 	 * The 8K-indexed kernel TSB space is used to hold
1880Sstevel@tonic-gate 	 * translations below...
1890Sstevel@tonic-gate 	 */
1900Sstevel@tonic-gate 	trans_root = read_prom_mappings(&ntrans_root);
1910Sstevel@tonic-gate 	sfmmu_remap_kernel();
1920Sstevel@tonic-gate 	startup_fixup_physavail();
1930Sstevel@tonic-gate 	mmu_init_kernel_pgsz(kas.a_hat);
1940Sstevel@tonic-gate 	sfmmu_map_prom_mappings(trans_root, ntrans_root);
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	/*
1970Sstevel@tonic-gate 	 * We invalidate 8K kernel TSB because we used it in
1980Sstevel@tonic-gate 	 * sfmmu_map_prom_mappings()
1990Sstevel@tonic-gate 	 */
2000Sstevel@tonic-gate 	sfmmu_inv_tsb(ktsb_base, ktsb_sz);
2010Sstevel@tonic-gate 	sfmmu_inv_tsb(ktsb4m_base, ktsb4m_sz);
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	sfmmu_init_ktsbinfo();
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	sfmmu_kern_mapped = 1;
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	/*
2090Sstevel@tonic-gate 	 * hments have been created for mapped pages, and thus we're ready
2100Sstevel@tonic-gate 	 * for kmdb to start using its own trap table.  It walks the hments
2110Sstevel@tonic-gate 	 * to resolve TLB misses, and can't be used until they're ready.
2120Sstevel@tonic-gate 	 */
2130Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
2140Sstevel@tonic-gate 		kdi_dvec_vmready();
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate /*
2180Sstevel@tonic-gate  * Macro used below to convert the prom's 32-bit high and low fields into
2190Sstevel@tonic-gate  * a value appropriate for the 64-bit kernel.
2200Sstevel@tonic-gate  */
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate #define	COMBINE(hi, lo) (((uint64_t)(uint32_t)(hi) << 32) | (uint32_t)(lo))
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate /*
2250Sstevel@tonic-gate  * This function traverses the prom mapping list and creates equivalent
2260Sstevel@tonic-gate  * mappings in the sfmmu mapping hash.
2270Sstevel@tonic-gate  */
2280Sstevel@tonic-gate static void
2290Sstevel@tonic-gate sfmmu_map_prom_mappings(struct translation *trans_root, size_t ntrans_root)
2300Sstevel@tonic-gate {
2310Sstevel@tonic-gate 	struct translation *promt;
2320Sstevel@tonic-gate 	tte_t	tte, oldtte, *ttep;
2330Sstevel@tonic-gate 	pfn_t	pfn, oldpfn, basepfn;
2340Sstevel@tonic-gate 	caddr_t vaddr;
2350Sstevel@tonic-gate 	size_t	size, offset;
2360Sstevel@tonic-gate 	unsigned long i;
2370Sstevel@tonic-gate 	uint_t	attr;
2380Sstevel@tonic-gate 	page_t *pp;
2390Sstevel@tonic-gate 	extern struct memlist *virt_avail;
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	ttep = &tte;
2420Sstevel@tonic-gate 	for (i = 0, promt = trans_root; i < ntrans_root; i++, promt++) {
2430Sstevel@tonic-gate 		ASSERT(promt->tte_hi != 0);
2440Sstevel@tonic-gate 		ASSERT32(promt->virt_hi == 0 && promt->size_hi == 0);
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 		/*
2470Sstevel@tonic-gate 		 * hack until we get rid of map-for-unix
2480Sstevel@tonic-gate 		 */
2490Sstevel@tonic-gate 		if (COMBINE(promt->virt_hi, promt->virt_lo) < KERNELBASE)
2500Sstevel@tonic-gate 			continue;
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 		ttep->tte_inthi = promt->tte_hi;
2530Sstevel@tonic-gate 		ttep->tte_intlo = promt->tte_lo;
2540Sstevel@tonic-gate 		attr = PROC_DATA | HAT_NOSYNC;
2550Sstevel@tonic-gate #if defined(TTE_IS_GLOBAL)
2560Sstevel@tonic-gate 		if (TTE_IS_GLOBAL(ttep)) {
2570Sstevel@tonic-gate 			/*
2580Sstevel@tonic-gate 			 * The prom better not use global translations
2590Sstevel@tonic-gate 			 * because a user process might use the same
2600Sstevel@tonic-gate 			 * virtual addresses
2610Sstevel@tonic-gate 			 */
2620Sstevel@tonic-gate 			cmn_err(CE_PANIC, "map_prom: global translation");
2630Sstevel@tonic-gate 			TTE_SET_LOFLAGS(ttep, TTE_GLB_INT, 0);
2640Sstevel@tonic-gate 		}
2650Sstevel@tonic-gate #endif
2660Sstevel@tonic-gate 		if (TTE_IS_LOCKED(ttep)) {
2670Sstevel@tonic-gate 			/* clear the lock bits */
2680Sstevel@tonic-gate 			TTE_CLR_LOCKED(ttep);
2690Sstevel@tonic-gate 		}
2700Sstevel@tonic-gate 		attr |= (TTE_IS_VCACHEABLE(ttep)) ? 0 : SFMMU_UNCACHEVTTE;
2710Sstevel@tonic-gate 		attr |= (TTE_IS_PCACHEABLE(ttep)) ? 0 : SFMMU_UNCACHEPTTE;
2720Sstevel@tonic-gate 		attr |= (TTE_IS_SIDEFFECT(ttep)) ? SFMMU_SIDEFFECT : 0;
2730Sstevel@tonic-gate 		attr |= (TTE_IS_IE(ttep)) ? HAT_STRUCTURE_LE : 0;
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 		size = COMBINE(promt->size_hi, promt->size_lo);
2760Sstevel@tonic-gate 		offset = 0;
2770Sstevel@tonic-gate 		basepfn = TTE_TO_PFN((caddr_t)COMBINE(promt->virt_hi,
2780Sstevel@tonic-gate 		    promt->virt_lo), ttep);
2790Sstevel@tonic-gate 		while (size) {
2800Sstevel@tonic-gate 			vaddr = (caddr_t)(COMBINE(promt->virt_hi,
2810Sstevel@tonic-gate 			    promt->virt_lo) + offset);
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 			/*
2840Sstevel@tonic-gate 			 * make sure address is not in virt-avail list
2850Sstevel@tonic-gate 			 */
2860Sstevel@tonic-gate 			if (address_in_memlist(virt_avail, (uint64_t)vaddr,
2870Sstevel@tonic-gate 			    size)) {
2880Sstevel@tonic-gate 				cmn_err(CE_PANIC, "map_prom: inconsistent "
2890Sstevel@tonic-gate 				    "translation/avail lists");
2900Sstevel@tonic-gate 			}
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 			pfn = basepfn + mmu_btop(offset);
2930Sstevel@tonic-gate 			if (pf_is_memory(pfn)) {
2940Sstevel@tonic-gate 				if (attr & SFMMU_UNCACHEPTTE) {
2950Sstevel@tonic-gate 					cmn_err(CE_PANIC, "map_prom: "
2960Sstevel@tonic-gate 					    "uncached prom memory page");
2970Sstevel@tonic-gate 				}
2980Sstevel@tonic-gate 			} else {
2990Sstevel@tonic-gate 				if (!(attr & SFMMU_SIDEFFECT)) {
3000Sstevel@tonic-gate 					cmn_err(CE_PANIC, "map_prom: prom "
3010Sstevel@tonic-gate 					    "i/o page without side-effect");
3020Sstevel@tonic-gate 				}
3030Sstevel@tonic-gate 			}
3040Sstevel@tonic-gate 			oldpfn = sfmmu_vatopfn(vaddr, KHATID, &oldtte);
3050Sstevel@tonic-gate 			ASSERT(oldpfn != PFN_SUSPENDED);
3060Sstevel@tonic-gate 			ASSERT(page_relocate_ready == 0);
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 			if (oldpfn != PFN_INVALID) {
3090Sstevel@tonic-gate 				/*
3100Sstevel@tonic-gate 				 * mapping already exists.
3110Sstevel@tonic-gate 				 * Verify they are equal
3120Sstevel@tonic-gate 				 */
3130Sstevel@tonic-gate 				if (pfn != oldpfn) {
3140Sstevel@tonic-gate 					cmn_err(CE_PANIC, "map_prom: mapping "
3150Sstevel@tonic-gate 					    "conflict (va=0x%p pfn=%p, "
3160Sstevel@tonic-gate 					    "oldpfn=%p)",
3170Sstevel@tonic-gate 					    (void *)vaddr, (void *)pfn,
3180Sstevel@tonic-gate 					    (void *)oldpfn);
3190Sstevel@tonic-gate 				}
3200Sstevel@tonic-gate 				size -= MMU_PAGESIZE;
3210Sstevel@tonic-gate 				offset += MMU_PAGESIZE;
3220Sstevel@tonic-gate 				continue;
3230Sstevel@tonic-gate 			}
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 			pp = page_numtopp_nolock(pfn);
3260Sstevel@tonic-gate 			if ((pp != NULL) && PP_ISFREE((page_t *)pp)) {
3270Sstevel@tonic-gate 				cmn_err(CE_PANIC, "map_prom: "
3280Sstevel@tonic-gate 				    "prom-mapped page (va 0x%p, pfn 0x%p) "
3290Sstevel@tonic-gate 				    "on free list", (void *)vaddr, (void *)pfn);
3300Sstevel@tonic-gate 			}
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 			sfmmu_memtte(ttep, pfn, attr, TTE8K);
3330Sstevel@tonic-gate 			sfmmu_tteload(kas.a_hat, ttep, vaddr, pp,
3340Sstevel@tonic-gate 			    HAT_LOAD_LOCK | SFMMU_NO_TSBLOAD);
3350Sstevel@tonic-gate 			size -= MMU_PAGESIZE;
3360Sstevel@tonic-gate 			offset += MMU_PAGESIZE;
3370Sstevel@tonic-gate 		}
3380Sstevel@tonic-gate 	}
3390Sstevel@tonic-gate }
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate #undef COMBINE	/* local to previous routine */
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate /*
3440Sstevel@tonic-gate  * This routine reads in the "translations" property in to a buffer and
3450Sstevel@tonic-gate  * returns a pointer to this buffer and the number of translations.
3460Sstevel@tonic-gate  */
3470Sstevel@tonic-gate static struct translation *
3480Sstevel@tonic-gate read_prom_mappings(size_t *ntransrootp)
3490Sstevel@tonic-gate {
3500Sstevel@tonic-gate 	char *prop = "translations";
3510Sstevel@tonic-gate 	size_t translen;
352789Sahrens 	pnode_t node;
3530Sstevel@tonic-gate 	struct translation *transroot;
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 	/*
3560Sstevel@tonic-gate 	 * the "translations" property is associated with the mmu node
3570Sstevel@tonic-gate 	 */
358789Sahrens 	node = (pnode_t)prom_getphandle(prom_mmu_ihandle());
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	/*
3610Sstevel@tonic-gate 	 * We use the TSB space to read in the prom mappings.  This space
3620Sstevel@tonic-gate 	 * is currently not being used because we haven't taken over the
3630Sstevel@tonic-gate 	 * trap table yet.  It should be big enough to hold the mappings.
3640Sstevel@tonic-gate 	 */
3650Sstevel@tonic-gate 	if ((translen = prom_getproplen(node, prop)) == -1)
3660Sstevel@tonic-gate 		cmn_err(CE_PANIC, "no translations property");
3670Sstevel@tonic-gate 	*ntransrootp = translen / sizeof (*transroot);
3680Sstevel@tonic-gate 	translen = roundup(translen, MMU_PAGESIZE);
3690Sstevel@tonic-gate 	PRM_DEBUG(translen);
3700Sstevel@tonic-gate 	if (translen > TSB_BYTES(ktsb_szcode))
3710Sstevel@tonic-gate 		cmn_err(CE_PANIC, "not enough space for translations");
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	transroot = (struct translation *)ktsb_base;
3740Sstevel@tonic-gate 	ASSERT(transroot);
3750Sstevel@tonic-gate 	if (prom_getprop(node, prop, (caddr_t)transroot) == -1) {
3760Sstevel@tonic-gate 		cmn_err(CE_PANIC, "translations getprop failed");
3770Sstevel@tonic-gate 	}
3780Sstevel@tonic-gate 	return (transroot);
3790Sstevel@tonic-gate }
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate /*
3820Sstevel@tonic-gate  * Init routine of the nucleus data memory allocator.
3830Sstevel@tonic-gate  *
3840Sstevel@tonic-gate  * The nucleus data memory allocator is organized in ecache_alignsize'd
3850Sstevel@tonic-gate  * memory chunks. Memory allocated by ndata_alloc() will never be freed.
3860Sstevel@tonic-gate  *
3870Sstevel@tonic-gate  * The ndata argument is used as header of the ndata freelist.
3880Sstevel@tonic-gate  * Other freelist nodes are placed in the nucleus memory itself
3890Sstevel@tonic-gate  * at the beginning of a free memory chunk. Therefore a freelist
3900Sstevel@tonic-gate  * node (struct memlist) must fit into the smallest allocatable
3910Sstevel@tonic-gate  * memory chunk (ecache_alignsize bytes).
3920Sstevel@tonic-gate  *
3930Sstevel@tonic-gate  * The memory interval [base, end] passed to ndata_alloc_init() must be
3940Sstevel@tonic-gate  * bzero'd to allow the allocator to return bzero'd memory easily.
3950Sstevel@tonic-gate  */
3960Sstevel@tonic-gate void
3970Sstevel@tonic-gate ndata_alloc_init(struct memlist *ndata, uintptr_t base, uintptr_t end)
3980Sstevel@tonic-gate {
3990Sstevel@tonic-gate 	ASSERT(sizeof (struct memlist) <= ecache_alignsize);
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 	base = roundup(base, ecache_alignsize);
4020Sstevel@tonic-gate 	end = end - end % ecache_alignsize;
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 	ASSERT(base < end);
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	ndata->address = base;
4070Sstevel@tonic-gate 	ndata->size = end - base;
4080Sstevel@tonic-gate 	ndata->next = NULL;
4090Sstevel@tonic-gate 	ndata->prev = NULL;
4100Sstevel@tonic-gate }
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate /*
4130Sstevel@tonic-gate  * Deliver the size of the largest free memory chunk.
4140Sstevel@tonic-gate  */
4150Sstevel@tonic-gate size_t
4160Sstevel@tonic-gate ndata_maxsize(struct memlist *ndata)
4170Sstevel@tonic-gate {
4180Sstevel@tonic-gate 	size_t chunksize = ndata->size;
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	while ((ndata = ndata->next) != NULL) {
4210Sstevel@tonic-gate 		if (chunksize < ndata->size)
4220Sstevel@tonic-gate 			chunksize = ndata->size;
4230Sstevel@tonic-gate 	}
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	return (chunksize);
4260Sstevel@tonic-gate }
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate /*
4290Sstevel@tonic-gate  * This is a special function to figure out if the memory chunk needed
4300Sstevel@tonic-gate  * for the page structs can fit in the nucleus or not. If it fits the
4310Sstevel@tonic-gate  * function calculates and returns the possible remaining ndata size
4320Sstevel@tonic-gate  * in the last element if the size needed for page structs would be
4330Sstevel@tonic-gate  * allocated from the nucleus.
4340Sstevel@tonic-gate  */
4350Sstevel@tonic-gate size_t
4360Sstevel@tonic-gate ndata_spare(struct memlist *ndata, size_t wanted, size_t alignment)
4370Sstevel@tonic-gate {
4380Sstevel@tonic-gate 	struct memlist *frlist;
4390Sstevel@tonic-gate 	uintptr_t base;
4400Sstevel@tonic-gate 	uintptr_t end;
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	for (frlist = ndata; frlist != NULL; frlist = frlist->next) {
4430Sstevel@tonic-gate 		base = roundup(frlist->address, alignment);
4440Sstevel@tonic-gate 		end = roundup(base + wanted, ecache_alignsize);
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 		if (end <= frlist->address + frlist->size) {
4470Sstevel@tonic-gate 			if (frlist->next == NULL)
4480Sstevel@tonic-gate 				return (frlist->address + frlist->size - end);
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 			while (frlist->next != NULL)
4510Sstevel@tonic-gate 				frlist = frlist->next;
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 			return (frlist->size);
4540Sstevel@tonic-gate 		}
4550Sstevel@tonic-gate 	}
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	return (0);
4580Sstevel@tonic-gate }
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate /*
4610Sstevel@tonic-gate  * Allocate the last properly aligned memory chunk.
4620Sstevel@tonic-gate  * This function is called when no more large nucleus memory chunks
4630Sstevel@tonic-gate  * will be allocated.  The remaining free nucleus memory at the end
4640Sstevel@tonic-gate  * of the nucleus can be added to the phys_avail list.
4650Sstevel@tonic-gate  */
4660Sstevel@tonic-gate void *
4670Sstevel@tonic-gate ndata_extra_base(struct memlist *ndata, size_t alignment)
4680Sstevel@tonic-gate {
4690Sstevel@tonic-gate 	uintptr_t base;
4700Sstevel@tonic-gate 	size_t wasteage = 0;
4710Sstevel@tonic-gate #ifdef	DEBUG
4720Sstevel@tonic-gate 	static int called = 0;
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	if (called++ > 0)
4750Sstevel@tonic-gate 		cmn_err(CE_PANIC, "ndata_extra_base() called more than once");
4760Sstevel@tonic-gate #endif /* DEBUG */
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	/*
4790Sstevel@tonic-gate 	 * The alignment needs to be a multiple of ecache_alignsize.
4800Sstevel@tonic-gate 	 */
4810Sstevel@tonic-gate 	ASSERT((alignment % ecache_alignsize) ==  0);
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	while (ndata->next != NULL) {
4840Sstevel@tonic-gate 		wasteage += ndata->size;
4850Sstevel@tonic-gate 		ndata = ndata->next;
4860Sstevel@tonic-gate 	}
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	base = roundup(ndata->address, alignment);
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	if (base >= ndata->address + ndata->size)
4910Sstevel@tonic-gate 		return (NULL);
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	if (base == ndata->address) {
4940Sstevel@tonic-gate 		if (ndata->prev != NULL)
4950Sstevel@tonic-gate 			ndata->prev->next = NULL;
4960Sstevel@tonic-gate 		else
4970Sstevel@tonic-gate 			ndata->size = 0;
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 		bzero((void *)base, sizeof (struct memlist));
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	} else {
5020Sstevel@tonic-gate 		ndata->size = base - ndata->address;
5030Sstevel@tonic-gate 		wasteage += ndata->size;
5040Sstevel@tonic-gate 	}
5050Sstevel@tonic-gate 	PRM_DEBUG(wasteage);
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	return ((void *)base);
5080Sstevel@tonic-gate }
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate /*
5110Sstevel@tonic-gate  * Select the best matching buffer, avoid memory fragmentation.
5120Sstevel@tonic-gate  */
5130Sstevel@tonic-gate static struct memlist *
5140Sstevel@tonic-gate ndata_select_chunk(struct memlist *ndata, size_t wanted, size_t alignment)
5150Sstevel@tonic-gate {
5160Sstevel@tonic-gate 	struct memlist *fnd_below = NULL;
5170Sstevel@tonic-gate 	struct memlist *fnd_above = NULL;
5180Sstevel@tonic-gate 	struct memlist *fnd_unused = NULL;
5190Sstevel@tonic-gate 	struct memlist *frlist;
5200Sstevel@tonic-gate 	uintptr_t base;
5210Sstevel@tonic-gate 	uintptr_t end;
5220Sstevel@tonic-gate 	size_t below;
5230Sstevel@tonic-gate 	size_t above;
5240Sstevel@tonic-gate 	size_t unused;
5250Sstevel@tonic-gate 	size_t best_below = ULONG_MAX;
5260Sstevel@tonic-gate 	size_t best_above = ULONG_MAX;
5270Sstevel@tonic-gate 	size_t best_unused = ULONG_MAX;
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	ASSERT(ndata != NULL);
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	/*
5320Sstevel@tonic-gate 	 * Look for the best matching buffer, avoid memory fragmentation.
5330Sstevel@tonic-gate 	 * The following strategy is used, try to find
5340Sstevel@tonic-gate 	 *   1. an exact fitting buffer
5350Sstevel@tonic-gate 	 *   2. avoid wasting any space below the buffer, take first
5360Sstevel@tonic-gate 	 *	fitting buffer
5370Sstevel@tonic-gate 	 *   3. avoid wasting any space above the buffer, take first
5380Sstevel@tonic-gate 	 *	fitting buffer
5390Sstevel@tonic-gate 	 *   4. avoid wasting space, take first fitting buffer
5400Sstevel@tonic-gate 	 *   5. take the last buffer in chain
5410Sstevel@tonic-gate 	 */
5420Sstevel@tonic-gate 	for (frlist = ndata; frlist != NULL; frlist = frlist->next) {
5430Sstevel@tonic-gate 		base = roundup(frlist->address, alignment);
5440Sstevel@tonic-gate 		end = roundup(base + wanted, ecache_alignsize);
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 		if (end > frlist->address + frlist->size)
5470Sstevel@tonic-gate 			continue;
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 		below = (base - frlist->address) / ecache_alignsize;
5500Sstevel@tonic-gate 		above = (frlist->address + frlist->size - end) /
5510Sstevel@tonic-gate 		    ecache_alignsize;
5520Sstevel@tonic-gate 		unused = below + above;
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 		if (unused == 0)
5550Sstevel@tonic-gate 			return (frlist);
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 		if (frlist->next == NULL)
5580Sstevel@tonic-gate 			break;
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 		if (below < best_below) {
5610Sstevel@tonic-gate 			best_below = below;
5620Sstevel@tonic-gate 			fnd_below = frlist;
5630Sstevel@tonic-gate 		}
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 		if (above < best_above) {
5660Sstevel@tonic-gate 			best_above = above;
5670Sstevel@tonic-gate 			fnd_above = frlist;
5680Sstevel@tonic-gate 		}
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 		if (unused < best_unused) {
5710Sstevel@tonic-gate 			best_unused = unused;
5720Sstevel@tonic-gate 			fnd_unused = frlist;
5730Sstevel@tonic-gate 		}
5740Sstevel@tonic-gate 	}
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	if (best_below == 0)
5770Sstevel@tonic-gate 		return (fnd_below);
5780Sstevel@tonic-gate 	if (best_above == 0)
5790Sstevel@tonic-gate 		return (fnd_above);
5800Sstevel@tonic-gate 	if (best_unused < ULONG_MAX)
5810Sstevel@tonic-gate 		return (fnd_unused);
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	return (frlist);
5840Sstevel@tonic-gate }
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate /*
5870Sstevel@tonic-gate  * Nucleus data memory allocator.
5880Sstevel@tonic-gate  * The granularity of the allocator is ecache_alignsize.
5890Sstevel@tonic-gate  * See also comment for ndata_alloc_init().
5900Sstevel@tonic-gate  */
5910Sstevel@tonic-gate void *
5920Sstevel@tonic-gate ndata_alloc(struct memlist *ndata, size_t wanted, size_t alignment)
5930Sstevel@tonic-gate {
5940Sstevel@tonic-gate 	struct memlist *found;
5950Sstevel@tonic-gate 	struct memlist *fnd_above;
5960Sstevel@tonic-gate 	uintptr_t base;
5970Sstevel@tonic-gate 	uintptr_t end;
5980Sstevel@tonic-gate 	size_t below;
5990Sstevel@tonic-gate 	size_t above;
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 	/*
6020Sstevel@tonic-gate 	 * Look for the best matching buffer, avoid memory fragmentation.
6030Sstevel@tonic-gate 	 */
6040Sstevel@tonic-gate 	if ((found = ndata_select_chunk(ndata, wanted, alignment)) == NULL)
6050Sstevel@tonic-gate 		return (NULL);
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	/*
6080Sstevel@tonic-gate 	 * Allocate the nucleus data buffer.
6090Sstevel@tonic-gate 	 */
6100Sstevel@tonic-gate 	base = roundup(found->address, alignment);
6110Sstevel@tonic-gate 	end = roundup(base + wanted, ecache_alignsize);
6120Sstevel@tonic-gate 	ASSERT(end <= found->address + found->size);
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	below = base - found->address;
6150Sstevel@tonic-gate 	above = found->address + found->size - end;
6160Sstevel@tonic-gate 	ASSERT(above == 0 || (above % ecache_alignsize) == 0);
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 	if (below >= ecache_alignsize) {
6190Sstevel@tonic-gate 		/*
6200Sstevel@tonic-gate 		 * There is free memory below the allocated memory chunk.
6210Sstevel@tonic-gate 		 */
6220Sstevel@tonic-gate 		found->size = below - below % ecache_alignsize;
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 		if (above) {
6250Sstevel@tonic-gate 			fnd_above = (struct memlist *)end;
6260Sstevel@tonic-gate 			fnd_above->address = end;
6270Sstevel@tonic-gate 			fnd_above->size = above;
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 			if ((fnd_above->next = found->next) != NULL)
6300Sstevel@tonic-gate 				found->next->prev = fnd_above;
6310Sstevel@tonic-gate 			fnd_above->prev = found;
6320Sstevel@tonic-gate 			found->next = fnd_above;
6330Sstevel@tonic-gate 		}
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 		return ((void *)base);
6360Sstevel@tonic-gate 	}
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	if (found->prev == NULL) {
6390Sstevel@tonic-gate 		/*
6400Sstevel@tonic-gate 		 * The first chunk (ndata) is selected.
6410Sstevel@tonic-gate 		 */
6420Sstevel@tonic-gate 		ASSERT(found == ndata);
6430Sstevel@tonic-gate 		if (above) {
6440Sstevel@tonic-gate 			found->address = end;
6450Sstevel@tonic-gate 			found->size = above;
6460Sstevel@tonic-gate 		} else if (found->next != NULL) {
6470Sstevel@tonic-gate 			found->address = found->next->address;
6480Sstevel@tonic-gate 			found->size = found->next->size;
6490Sstevel@tonic-gate 			if ((found->next = found->next->next) != NULL)
6500Sstevel@tonic-gate 				found->next->prev = found;
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 			bzero((void *)found->address, sizeof (struct memlist));
6530Sstevel@tonic-gate 		} else {
6540Sstevel@tonic-gate 			found->address = end;
6550Sstevel@tonic-gate 			found->size = 0;
6560Sstevel@tonic-gate 		}
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 		return ((void *)base);
6590Sstevel@tonic-gate 	}
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	/*
6620Sstevel@tonic-gate 	 * Not the first chunk.
6630Sstevel@tonic-gate 	 */
6640Sstevel@tonic-gate 	if (above) {
6650Sstevel@tonic-gate 		fnd_above = (struct memlist *)end;
6660Sstevel@tonic-gate 		fnd_above->address = end;
6670Sstevel@tonic-gate 		fnd_above->size = above;
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 		if ((fnd_above->next = found->next) != NULL)
6700Sstevel@tonic-gate 			fnd_above->next->prev = fnd_above;
6710Sstevel@tonic-gate 		fnd_above->prev = found->prev;
6720Sstevel@tonic-gate 		found->prev->next = fnd_above;
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 	} else {
6750Sstevel@tonic-gate 		if ((found->prev->next = found->next) != NULL)
6760Sstevel@tonic-gate 			found->next->prev = found->prev;
6770Sstevel@tonic-gate 	}
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 	bzero((void *)found->address, sizeof (struct memlist));
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	return ((void *)base);
6820Sstevel@tonic-gate }
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate /*
6850Sstevel@tonic-gate  * Size the kernel TSBs based upon the amount of physical
6860Sstevel@tonic-gate  * memory in the system.
6870Sstevel@tonic-gate  */
6880Sstevel@tonic-gate static void
6890Sstevel@tonic-gate calc_tsb_sizes(pgcnt_t npages)
6900Sstevel@tonic-gate {
6910Sstevel@tonic-gate 	PRM_DEBUG(npages);
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	if (npages <= TSB_FREEMEM_MIN) {
6940Sstevel@tonic-gate 		ktsb_szcode = TSB_128K_SZCODE;
6950Sstevel@tonic-gate 		enable_bigktsb = 0;
6960Sstevel@tonic-gate 	} else if (npages <= TSB_FREEMEM_LARGE / 2) {
6970Sstevel@tonic-gate 		ktsb_szcode = TSB_256K_SZCODE;
6980Sstevel@tonic-gate 		enable_bigktsb = 0;
6990Sstevel@tonic-gate 	} else if (npages <= TSB_FREEMEM_LARGE) {
7000Sstevel@tonic-gate 		ktsb_szcode = TSB_512K_SZCODE;
7010Sstevel@tonic-gate 		enable_bigktsb = 0;
7020Sstevel@tonic-gate 	} else if (npages <= TSB_FREEMEM_LARGE * 2 ||
7030Sstevel@tonic-gate 	    enable_bigktsb == 0) {
7040Sstevel@tonic-gate 		ktsb_szcode = TSB_1M_SZCODE;
7050Sstevel@tonic-gate 		enable_bigktsb = 0;
7060Sstevel@tonic-gate 	} else {
7070Sstevel@tonic-gate 		ktsb_szcode = highbit(npages - 1);
7080Sstevel@tonic-gate 		ktsb_szcode -= TSB_START_SIZE;
7090Sstevel@tonic-gate 		ktsb_szcode = MAX(ktsb_szcode, MIN_BIGKTSB_SZCODE);
7100Sstevel@tonic-gate 		ktsb_szcode = MIN(ktsb_szcode, MAX_BIGKTSB_SZCODE);
7110Sstevel@tonic-gate 	}
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	/*
7140Sstevel@tonic-gate 	 * We choose the TSB to hold kernel 4M mappings to have twice
7150Sstevel@tonic-gate 	 * the reach as the primary kernel TSB since this TSB will
7160Sstevel@tonic-gate 	 * potentially (currently) be shared by both mappings to all of
7170Sstevel@tonic-gate 	 * physical memory plus user TSBs.  Since the current
7180Sstevel@tonic-gate 	 * limit on primary kernel TSB size is 16MB this will top out
7190Sstevel@tonic-gate 	 * at 64K which we can certainly afford.
7200Sstevel@tonic-gate 	 */
7210Sstevel@tonic-gate 	ktsb4m_szcode = ktsb_szcode - (MMU_PAGESHIFT4M - MMU_PAGESHIFT) + 1;
7220Sstevel@tonic-gate 	if (ktsb4m_szcode < TSB_MIN_SZCODE)
7230Sstevel@tonic-gate 		ktsb4m_szcode = TSB_MIN_SZCODE;
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 	ktsb_sz = TSB_BYTES(ktsb_szcode);	/* kernel 8K tsb size */
7260Sstevel@tonic-gate 	ktsb4m_sz = TSB_BYTES(ktsb4m_szcode);	/* kernel 4M tsb size */
7270Sstevel@tonic-gate }
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate /*
7300Sstevel@tonic-gate  * Allocate kernel TSBs from nucleus data memory.
7310Sstevel@tonic-gate  * The function return 0 on success and -1 on failure.
7320Sstevel@tonic-gate  */
7330Sstevel@tonic-gate int
7340Sstevel@tonic-gate ndata_alloc_tsbs(struct memlist *ndata, pgcnt_t npages)
7350Sstevel@tonic-gate {
7360Sstevel@tonic-gate 	/*
7370Sstevel@tonic-gate 	 * Size the kernel TSBs based upon the amount of physical
7380Sstevel@tonic-gate 	 * memory in the system.
7390Sstevel@tonic-gate 	 */
7400Sstevel@tonic-gate 	calc_tsb_sizes(npages);
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	/*
7430Sstevel@tonic-gate 	 * Allocate the 8K kernel TSB if it belongs inside the nucleus.
7440Sstevel@tonic-gate 	 */
7450Sstevel@tonic-gate 	if (enable_bigktsb == 0) {
7460Sstevel@tonic-gate 		if ((ktsb_base = ndata_alloc(ndata, ktsb_sz, ktsb_sz)) == NULL)
7470Sstevel@tonic-gate 			return (-1);
7480Sstevel@tonic-gate 		ASSERT(!((uintptr_t)ktsb_base & (ktsb_sz - 1)));
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 		PRM_DEBUG(ktsb_base);
7510Sstevel@tonic-gate 		PRM_DEBUG(ktsb_sz);
7520Sstevel@tonic-gate 		PRM_DEBUG(ktsb_szcode);
7530Sstevel@tonic-gate 	}
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	/*
7560Sstevel@tonic-gate 	 * Next, allocate 4M kernel TSB from the nucleus since it's small.
7570Sstevel@tonic-gate 	 */
7580Sstevel@tonic-gate 	if ((ktsb4m_base = ndata_alloc(ndata, ktsb4m_sz, ktsb4m_sz)) == NULL)
7590Sstevel@tonic-gate 		return (-1);
7600Sstevel@tonic-gate 	ASSERT(!((uintptr_t)ktsb4m_base & (ktsb4m_sz - 1)));
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 	PRM_DEBUG(ktsb4m_base);
7630Sstevel@tonic-gate 	PRM_DEBUG(ktsb4m_sz);
7640Sstevel@tonic-gate 	PRM_DEBUG(ktsb4m_szcode);
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 	return (0);
7670Sstevel@tonic-gate }
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate /*
7700Sstevel@tonic-gate  * Allocate hat structs from the nucleus data memory.
7710Sstevel@tonic-gate  */
7720Sstevel@tonic-gate int
7730Sstevel@tonic-gate ndata_alloc_hat(struct memlist *ndata, pgcnt_t npages, pgcnt_t kpm_npages)
7740Sstevel@tonic-gate {
7750Sstevel@tonic-gate 	size_t	mml_alloc_sz;
7760Sstevel@tonic-gate 	size_t	cb_alloc_sz;
7770Sstevel@tonic-gate 	int	max_nucuhme_buckets = MAX_NUCUHME_BUCKETS;
7780Sstevel@tonic-gate 	int	max_nuckhme_buckets = MAX_NUCKHME_BUCKETS;
7790Sstevel@tonic-gate 	ulong_t hme_buckets;
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	if (enable_bigktsb) {
7820Sstevel@tonic-gate 		ASSERT((max_nucuhme_buckets + max_nuckhme_buckets) *
7830Sstevel@tonic-gate 		    sizeof (struct hmehash_bucket) <=
7840Sstevel@tonic-gate 			TSB_BYTES(TSB_1M_SZCODE));
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 		max_nucuhme_buckets *= 2;
7870Sstevel@tonic-gate 		max_nuckhme_buckets *= 2;
7880Sstevel@tonic-gate 	}
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 	/*
7910Sstevel@tonic-gate 	 * The number of buckets in the hme hash tables
7920Sstevel@tonic-gate 	 * is a power of 2 such that the average hash chain length is
7930Sstevel@tonic-gate 	 * HMENT_HASHAVELEN.  The number of buckets for the user hash is
7940Sstevel@tonic-gate 	 * a function of physical memory and a predefined overmapping factor.
7950Sstevel@tonic-gate 	 * The number of buckets for the kernel hash is a function of
7960Sstevel@tonic-gate 	 * physical memory only.
7970Sstevel@tonic-gate 	 */
7980Sstevel@tonic-gate 	hme_buckets = (npages * HMEHASH_FACTOR) /
7990Sstevel@tonic-gate 		(HMENT_HASHAVELEN * (HMEBLK_SPAN(TTE8K) >> MMU_PAGESHIFT));
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	uhmehash_num = (int)MIN(hme_buckets, MAX_UHME_BUCKETS);
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	if (uhmehash_num > USER_BUCKETS_THRESHOLD) {
8040Sstevel@tonic-gate 		/*
8050Sstevel@tonic-gate 		 * if uhmehash_num is not power of 2 round it down to the
8060Sstevel@tonic-gate 		 *  next power of 2.
8070Sstevel@tonic-gate 		 */
8080Sstevel@tonic-gate 		uint_t align = 1 << (highbit(uhmehash_num - 1) - 1);
8090Sstevel@tonic-gate 		uhmehash_num = P2ALIGN(uhmehash_num, align);
8100Sstevel@tonic-gate 	} else
8110Sstevel@tonic-gate 		uhmehash_num = 1 << highbit(uhmehash_num - 1);
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 	hme_buckets = npages / (HMEBLK_SPAN(TTE8K) >> MMU_PAGESHIFT);
8140Sstevel@tonic-gate 	khmehash_num = (int)MIN(hme_buckets, MAX_KHME_BUCKETS);
8150Sstevel@tonic-gate 	khmehash_num = 1 << highbit(khmehash_num - 1);
8160Sstevel@tonic-gate 	khmehash_num = MAX(khmehash_num, MIN_KHME_BUCKETS);
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 	if ((khmehash_num > max_nuckhme_buckets) ||
8190Sstevel@tonic-gate 		(uhmehash_num > max_nucuhme_buckets)) {
8200Sstevel@tonic-gate 		khme_hash = NULL;
8210Sstevel@tonic-gate 		uhme_hash = NULL;
8220Sstevel@tonic-gate 	} else {
8230Sstevel@tonic-gate 		size_t hmehash_sz = (uhmehash_num + khmehash_num) *
8240Sstevel@tonic-gate 		    sizeof (struct hmehash_bucket);
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 		if ((khme_hash = ndata_alloc(ndata, hmehash_sz,
8270Sstevel@tonic-gate 		    ecache_alignsize)) != NULL)
8280Sstevel@tonic-gate 			uhme_hash = &khme_hash[khmehash_num];
8290Sstevel@tonic-gate 		else
8300Sstevel@tonic-gate 			uhme_hash = NULL;
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 		PRM_DEBUG(hmehash_sz);
8330Sstevel@tonic-gate 	}
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 	PRM_DEBUG(khme_hash);
8360Sstevel@tonic-gate 	PRM_DEBUG(khmehash_num);
8370Sstevel@tonic-gate 	PRM_DEBUG(uhme_hash);
8380Sstevel@tonic-gate 	PRM_DEBUG(uhmehash_num);
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate 	/*
8410Sstevel@tonic-gate 	 * For the page mapping list mutex array we allocate one mutex
8420Sstevel@tonic-gate 	 * for every 128 pages (1 MB) with a minimum of 64 entries and
8430Sstevel@tonic-gate 	 * a maximum of 8K entries. For the initial computation npages
8440Sstevel@tonic-gate 	 * is rounded up (ie. 1 << highbit(npages * 1.5 / 128))
8450Sstevel@tonic-gate 	 *
8460Sstevel@tonic-gate 	 * mml_shift is roughly log2(mml_table_sz) + 3 for MLIST_HASH
8470Sstevel@tonic-gate 	 *
8480Sstevel@tonic-gate 	 * It is not required that this be allocated from the nucleus,
8490Sstevel@tonic-gate 	 * but it is desirable.  So we first allocate from the nucleus
8500Sstevel@tonic-gate 	 * everything that must be there.  Having done so, if mml_table
8510Sstevel@tonic-gate 	 * will fit within what remains of the nucleus then it will be
8520Sstevel@tonic-gate 	 * allocated here.  If not, set mml_table to NULL, which will cause
8530Sstevel@tonic-gate 	 * startup_memlist() to BOP_ALLOC() space for it after our return...
8540Sstevel@tonic-gate 	 */
8550Sstevel@tonic-gate 	mml_table_sz = 1 << highbit((npages * 3) / 256);
8560Sstevel@tonic-gate 	if (mml_table_sz < 64)
8570Sstevel@tonic-gate 		mml_table_sz = 64;
8580Sstevel@tonic-gate 	else if (mml_table_sz > 8192)
8590Sstevel@tonic-gate 		mml_table_sz = 8192;
8600Sstevel@tonic-gate 	mml_shift = highbit(mml_table_sz) + 3;
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 	PRM_DEBUG(mml_table_sz);
8630Sstevel@tonic-gate 	PRM_DEBUG(mml_shift);
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate 	mml_alloc_sz = mml_table_sz * sizeof (kmutex_t);
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 	mml_table = ndata_alloc(ndata, mml_alloc_sz, ecache_alignsize);
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate 	PRM_DEBUG(mml_table);
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 	cb_alloc_sz = sfmmu_max_cb_id * sizeof (struct sfmmu_callback);
8720Sstevel@tonic-gate 	PRM_DEBUG(cb_alloc_sz);
8730Sstevel@tonic-gate 	sfmmu_cb_table = ndata_alloc(ndata, cb_alloc_sz, ecache_alignsize);
8740Sstevel@tonic-gate 	PRM_DEBUG(sfmmu_cb_table);
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 	/*
8770Sstevel@tonic-gate 	 * For the kpm_page mutex array we allocate one mutex every 16
8780Sstevel@tonic-gate 	 * kpm pages (64MB). In smallpage mode we allocate one mutex
8790Sstevel@tonic-gate 	 * every 8K pages. The minimum is set to 64 entries and the
8800Sstevel@tonic-gate 	 * maximum to 8K entries.
8810Sstevel@tonic-gate 	 *
8820Sstevel@tonic-gate 	 * It is not required that this be allocated from the nucleus,
8830Sstevel@tonic-gate 	 * but it is desirable.  So we first allocate from the nucleus
8840Sstevel@tonic-gate 	 * everything that must be there.  Having done so, if kpmp_table
8850Sstevel@tonic-gate 	 * or kpmp_stable will fit within what remains of the nucleus
8860Sstevel@tonic-gate 	 * then it will be allocated here.  If not, startup_memlist()
8870Sstevel@tonic-gate 	 * will use BOP_ALLOC() space for it after our return...
8880Sstevel@tonic-gate 	 */
8890Sstevel@tonic-gate 	if (kpm_enable) {
8900Sstevel@tonic-gate 		size_t	kpmp_alloc_sz;
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 		if (kpm_smallpages == 0) {
8930Sstevel@tonic-gate 			kpmp_shift = highbit(sizeof (kpm_page_t)) - 1;
8940Sstevel@tonic-gate 			kpmp_table_sz = 1 << highbit(kpm_npages / 16);
8950Sstevel@tonic-gate 			kpmp_table_sz = (kpmp_table_sz < 64) ? 64 :
8960Sstevel@tonic-gate 			    ((kpmp_table_sz > 8192) ? 8192 : kpmp_table_sz);
8970Sstevel@tonic-gate 			kpmp_alloc_sz = kpmp_table_sz * sizeof (kpm_hlk_t);
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate 			kpmp_table = ndata_alloc(ndata, kpmp_alloc_sz,
9000Sstevel@tonic-gate 			    ecache_alignsize);
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 			PRM_DEBUG(kpmp_table);
9030Sstevel@tonic-gate 			PRM_DEBUG(kpmp_table_sz);
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate 			kpmp_stable_sz = 0;
9060Sstevel@tonic-gate 			kpmp_stable = NULL;
9070Sstevel@tonic-gate 		} else {
9080Sstevel@tonic-gate 			ASSERT(kpm_pgsz == PAGESIZE);
9090Sstevel@tonic-gate 			kpmp_shift = highbit(sizeof (kpm_shlk_t)) + 1;
9100Sstevel@tonic-gate 			kpmp_stable_sz = 1 << highbit(kpm_npages / 8192);
9110Sstevel@tonic-gate 			kpmp_stable_sz = (kpmp_stable_sz < 64) ? 64 :
9120Sstevel@tonic-gate 			    ((kpmp_stable_sz > 8192) ? 8192 : kpmp_stable_sz);
9130Sstevel@tonic-gate 			kpmp_alloc_sz = kpmp_stable_sz * sizeof (kpm_shlk_t);
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 			kpmp_stable = ndata_alloc(ndata, kpmp_alloc_sz,
9160Sstevel@tonic-gate 			    ecache_alignsize);
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 			PRM_DEBUG(kpmp_stable);
9190Sstevel@tonic-gate 			PRM_DEBUG(kpmp_stable_sz);
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 			kpmp_table_sz = 0;
9220Sstevel@tonic-gate 			kpmp_table = NULL;
9230Sstevel@tonic-gate 		}
9240Sstevel@tonic-gate 		PRM_DEBUG(kpmp_shift);
9250Sstevel@tonic-gate 	}
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate 	return (0);
9280Sstevel@tonic-gate }
9290Sstevel@tonic-gate 
9300Sstevel@tonic-gate caddr_t
9310Sstevel@tonic-gate alloc_hme_buckets(caddr_t base, int pagesize)
9320Sstevel@tonic-gate {
9330Sstevel@tonic-gate 	size_t hmehash_sz = (uhmehash_num + khmehash_num) *
9340Sstevel@tonic-gate 	sizeof (struct hmehash_bucket);
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate 	ASSERT(khme_hash == NULL);
9370Sstevel@tonic-gate 	ASSERT(uhme_hash == NULL);
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate 	/* If no pagesize specified, use default MMU pagesize */
9400Sstevel@tonic-gate 	if (!pagesize)
9410Sstevel@tonic-gate 		pagesize = MMU_PAGESIZE;
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate 	/*
9440Sstevel@tonic-gate 	 * If we start aligned and ask for a multiple of a pagesize, and OBP
9450Sstevel@tonic-gate 	 * supports large pages, we will then use mappings of the largest size
9460Sstevel@tonic-gate 	 * possible for the BOP_ALLOC, possibly saving us tens of thousands of
9470Sstevel@tonic-gate 	 * TLB miss-induced traversals of the TSBs and/or the HME hashes...
9480Sstevel@tonic-gate 	 */
9490Sstevel@tonic-gate 	base = (caddr_t)roundup((uintptr_t)base, pagesize);
9500Sstevel@tonic-gate 	hmehash_sz = roundup(hmehash_sz, pagesize);
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 	khme_hash = (struct hmehash_bucket *)BOP_ALLOC(bootops, base,
9530Sstevel@tonic-gate 		hmehash_sz, pagesize);
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 	if ((caddr_t)khme_hash != base)
9560Sstevel@tonic-gate 		cmn_err(CE_PANIC, "Cannot bop_alloc hme hash buckets.");
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate 	uhme_hash = (struct hmehash_bucket *)((caddr_t)khme_hash +
9590Sstevel@tonic-gate 		khmehash_num * sizeof (struct hmehash_bucket));
9600Sstevel@tonic-gate 	base += hmehash_sz;
9610Sstevel@tonic-gate 	return (base);
9620Sstevel@tonic-gate }
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate /*
9650Sstevel@tonic-gate  * This function bop allocs the kernel TSB.
9660Sstevel@tonic-gate  */
9670Sstevel@tonic-gate caddr_t
9680Sstevel@tonic-gate sfmmu_ktsb_alloc(caddr_t tsbbase)
9690Sstevel@tonic-gate {
9700Sstevel@tonic-gate 	caddr_t vaddr;
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 	if (enable_bigktsb) {
9730Sstevel@tonic-gate 		ktsb_base = (caddr_t)roundup((uintptr_t)tsbbase, ktsb_sz);
9740Sstevel@tonic-gate 		vaddr = (caddr_t)BOP_ALLOC(bootops, ktsb_base, ktsb_sz,
9750Sstevel@tonic-gate 		    ktsb_sz);
9760Sstevel@tonic-gate 		if (vaddr != ktsb_base)
9770Sstevel@tonic-gate 			cmn_err(CE_PANIC, "sfmmu_ktsb_alloc: can't alloc"
9780Sstevel@tonic-gate 			    " bigktsb");
9790Sstevel@tonic-gate 		ktsb_base = vaddr;
9800Sstevel@tonic-gate 		tsbbase = ktsb_base + ktsb_sz;
9810Sstevel@tonic-gate 		PRM_DEBUG(ktsb_base);
9820Sstevel@tonic-gate 		PRM_DEBUG(tsbbase);
9830Sstevel@tonic-gate 	}
9840Sstevel@tonic-gate 	return (tsbbase);
9850Sstevel@tonic-gate }
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate /*
9880Sstevel@tonic-gate  * Moves code assembled outside of the trap table into the trap
9890Sstevel@tonic-gate  * table taking care to relocate relative branches to code outside
9900Sstevel@tonic-gate  * of the trap handler.
9910Sstevel@tonic-gate  */
9920Sstevel@tonic-gate static void
9930Sstevel@tonic-gate sfmmu_reloc_trap_handler(void *tablep, void *start, size_t count)
9940Sstevel@tonic-gate {
9950Sstevel@tonic-gate 	size_t i;
9960Sstevel@tonic-gate 	uint32_t *src;
9970Sstevel@tonic-gate 	uint32_t *dst;
9980Sstevel@tonic-gate 	uint32_t inst;
9990Sstevel@tonic-gate 	int op, op2;
10000Sstevel@tonic-gate 	int32_t offset;
10010Sstevel@tonic-gate 	int disp;
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 	src = start;
10040Sstevel@tonic-gate 	dst = tablep;
10050Sstevel@tonic-gate 	offset = src - dst;
10060Sstevel@tonic-gate 	for (src = start, i = 0; i < count; i++, src++, dst++) {
10070Sstevel@tonic-gate 		inst = *dst = *src;
10080Sstevel@tonic-gate 		op = (inst >> 30) & 0x2;
10090Sstevel@tonic-gate 		if (op == 1) {
10100Sstevel@tonic-gate 			/* call */
10110Sstevel@tonic-gate 			disp = ((int32_t)inst << 2) >> 2; /* sign-extend */
10120Sstevel@tonic-gate 			if (disp + i >= 0 && disp + i < count)
10130Sstevel@tonic-gate 				continue;
10140Sstevel@tonic-gate 			disp += offset;
10150Sstevel@tonic-gate 			inst = 0x40000000u | (disp & 0x3fffffffu);
10160Sstevel@tonic-gate 			*dst = inst;
10170Sstevel@tonic-gate 		} else if (op == 0) {
10180Sstevel@tonic-gate 			/* branch or sethi */
10190Sstevel@tonic-gate 			op2 = (inst >> 22) & 0x7;
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 			switch (op2) {
10220Sstevel@tonic-gate 			case 0x3: /* BPr */
10230Sstevel@tonic-gate 				disp = (((inst >> 20) & 0x3) << 14) |
10240Sstevel@tonic-gate 				    (inst & 0x3fff);
10250Sstevel@tonic-gate 				disp = (disp << 16) >> 16; /* sign-extend */
10260Sstevel@tonic-gate 				if (disp + i >= 0 && disp + i < count)
10270Sstevel@tonic-gate 					continue;
10280Sstevel@tonic-gate 				disp += offset;
10290Sstevel@tonic-gate 				if (((disp << 16) >> 16) != disp)
10300Sstevel@tonic-gate 					cmn_err(CE_PANIC, "bad reloc");
10310Sstevel@tonic-gate 				inst &= ~0x303fff;
10320Sstevel@tonic-gate 				inst |= (disp & 0x3fff);
10330Sstevel@tonic-gate 				inst |= (disp & 0xc000) << 6;
10340Sstevel@tonic-gate 				break;
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate 			case 0x2: /* Bicc */
10370Sstevel@tonic-gate 				disp = ((int32_t)inst << 10) >> 10;
10380Sstevel@tonic-gate 				if (disp + i >= 0 && disp + i < count)
10390Sstevel@tonic-gate 					continue;
10400Sstevel@tonic-gate 				disp += offset;
10410Sstevel@tonic-gate 				if (((disp << 10) >> 10) != disp)
10420Sstevel@tonic-gate 					cmn_err(CE_PANIC, "bad reloc");
10430Sstevel@tonic-gate 				inst &= ~0x3fffff;
10440Sstevel@tonic-gate 				inst |= (disp & 0x3fffff);
10450Sstevel@tonic-gate 				break;
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 			case 0x1: /* Bpcc */
10480Sstevel@tonic-gate 				disp = ((int32_t)inst << 13) >> 13;
10490Sstevel@tonic-gate 				if (disp + i >= 0 && disp + i < count)
10500Sstevel@tonic-gate 					continue;
10510Sstevel@tonic-gate 				disp += offset;
10520Sstevel@tonic-gate 				if (((disp << 13) >> 13) != disp)
10530Sstevel@tonic-gate 					cmn_err(CE_PANIC, "bad reloc");
10540Sstevel@tonic-gate 				inst &= ~0x7ffff;
10550Sstevel@tonic-gate 				inst |= (disp & 0x7ffffu);
10560Sstevel@tonic-gate 				break;
10570Sstevel@tonic-gate 			}
10580Sstevel@tonic-gate 			*dst = inst;
10590Sstevel@tonic-gate 		}
10600Sstevel@tonic-gate 	}
10610Sstevel@tonic-gate 	flush_instr_mem(tablep, count * sizeof (uint32_t));
10620Sstevel@tonic-gate }
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate /*
10650Sstevel@tonic-gate  * Routine to allocate a large page to use in the TSB caches.
10660Sstevel@tonic-gate  */
10670Sstevel@tonic-gate /*ARGSUSED*/
10680Sstevel@tonic-gate static page_t *
10690Sstevel@tonic-gate sfmmu_tsb_page_create(void *addr, size_t size, int vmflag, void *arg)
10700Sstevel@tonic-gate {
10710Sstevel@tonic-gate 	int pgflags;
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate 	pgflags = PG_EXCL;
10740Sstevel@tonic-gate 	if ((vmflag & VM_NOSLEEP) == 0)
10750Sstevel@tonic-gate 		pgflags |= PG_WAIT;
10760Sstevel@tonic-gate 	if (vmflag & VM_PANIC)
10770Sstevel@tonic-gate 		pgflags |= PG_PANIC;
10780Sstevel@tonic-gate 	if (vmflag & VM_PUSHPAGE)
10790Sstevel@tonic-gate 		pgflags |= PG_PUSHPAGE;
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 	return (page_create_va_large(&kvp, (u_offset_t)(uintptr_t)addr, size,
10820Sstevel@tonic-gate 	    pgflags, &kvseg, addr, arg));
10830Sstevel@tonic-gate }
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate /*
10860Sstevel@tonic-gate  * Allocate a large page to back the virtual address range
10870Sstevel@tonic-gate  * [addr, addr + size).  If addr is NULL, allocate the virtual address
10880Sstevel@tonic-gate  * space as well.
10890Sstevel@tonic-gate  */
10900Sstevel@tonic-gate static void *
10910Sstevel@tonic-gate sfmmu_tsb_xalloc(vmem_t *vmp, void *inaddr, size_t size, int vmflag,
10920Sstevel@tonic-gate     uint_t attr, page_t *(*page_create_func)(void *, size_t, int, void *),
10930Sstevel@tonic-gate     void *pcarg)
10940Sstevel@tonic-gate {
10950Sstevel@tonic-gate 	page_t *ppl;
10960Sstevel@tonic-gate 	page_t *rootpp;
10970Sstevel@tonic-gate 	caddr_t addr = inaddr;
10980Sstevel@tonic-gate 	pgcnt_t npages = btopr(size);
10990Sstevel@tonic-gate 	page_t **ppa;
11000Sstevel@tonic-gate 	int i = 0;
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate 	/*
11030Sstevel@tonic-gate 	 * Assuming that only TSBs will call this with size > PAGESIZE
11040Sstevel@tonic-gate 	 * There is no reason why this couldn't be expanded to 8k pages as
11050Sstevel@tonic-gate 	 * well, or other page sizes in the future .... but for now, we
11060Sstevel@tonic-gate 	 * only support fixed sized page requests.
11070Sstevel@tonic-gate 	 */
11080Sstevel@tonic-gate 	if ((inaddr == NULL) && ((addr = vmem_xalloc(vmp, size, size, 0, 0,
11090Sstevel@tonic-gate 	    NULL, NULL, vmflag)) == NULL))
11100Sstevel@tonic-gate 		return (NULL);
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate 	/* If we ever don't want TSB slab-sized pages, this will panic */
11130Sstevel@tonic-gate 	ASSERT(((uintptr_t)addr & (tsb_slab_size - 1)) == 0);
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate 	if (page_resv(npages, vmflag & VM_KMFLAGS) == 0) {
11160Sstevel@tonic-gate 		if (inaddr == NULL)
11170Sstevel@tonic-gate 			vmem_xfree(vmp, addr, size);
11180Sstevel@tonic-gate 		return (NULL);
11190Sstevel@tonic-gate 	}
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 	ppl = page_create_func(addr, size, vmflag, pcarg);
11220Sstevel@tonic-gate 	if (ppl == NULL) {
11230Sstevel@tonic-gate 		if (inaddr == NULL)
11240Sstevel@tonic-gate 			vmem_xfree(vmp, addr, size);
11250Sstevel@tonic-gate 		page_unresv(npages);
11260Sstevel@tonic-gate 		return (NULL);
11270Sstevel@tonic-gate 	}
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate 	rootpp = ppl;
11300Sstevel@tonic-gate 	ppa = kmem_zalloc(npages * sizeof (page_t *), KM_SLEEP);
11310Sstevel@tonic-gate 	while (ppl != NULL) {
11320Sstevel@tonic-gate 		page_t *pp = ppl;
11330Sstevel@tonic-gate 		ppa[i++] = pp;
11340Sstevel@tonic-gate 		page_sub(&ppl, pp);
11350Sstevel@tonic-gate 		ASSERT(page_iolock_assert(pp));
11360Sstevel@tonic-gate 		page_io_unlock(pp);
11370Sstevel@tonic-gate 	}
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate 	/*
11400Sstevel@tonic-gate 	 * Load the locked entry.  It's OK to preload the entry into
11410Sstevel@tonic-gate 	 * the TSB since we now support large mappings in the kernel TSB.
11420Sstevel@tonic-gate 	 */
11430Sstevel@tonic-gate 	hat_memload_array(kas.a_hat, (caddr_t)rootpp->p_offset, size,
11440Sstevel@tonic-gate 	    ppa, (PROT_ALL & ~PROT_USER) | HAT_NOSYNC | attr, HAT_LOAD_LOCK);
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate 	for (--i; i >= 0; --i) {
11470Sstevel@tonic-gate 		(void) page_pp_lock(ppa[i], 0, 1);
11480Sstevel@tonic-gate 		page_unlock(ppa[i]);
11490Sstevel@tonic-gate 	}
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate 	kmem_free(ppa, npages * sizeof (page_t *));
11520Sstevel@tonic-gate 	return (addr);
11530Sstevel@tonic-gate }
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate /* Called to import new spans into the TSB vmem arenas */
11560Sstevel@tonic-gate void *
11570Sstevel@tonic-gate sfmmu_tsb_segkmem_alloc(vmem_t *vmp, size_t size, int vmflag)
11580Sstevel@tonic-gate {
11590Sstevel@tonic-gate 	lgrp_id_t lgrpid = LGRP_NONE;
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 	if (tsb_lgrp_affinity) {
11620Sstevel@tonic-gate 		/*
11630Sstevel@tonic-gate 		 * Search for the vmp->lgrpid mapping by brute force;
11640Sstevel@tonic-gate 		 * some day vmp will have an lgrp, until then we have
11650Sstevel@tonic-gate 		 * to do this the hard way.
11660Sstevel@tonic-gate 		 */
11670Sstevel@tonic-gate 		for (lgrpid = 0; lgrpid < NLGRPS_MAX &&
11680Sstevel@tonic-gate 		    vmp != kmem_tsb_default_arena[lgrpid]; lgrpid++);
11690Sstevel@tonic-gate 		if (lgrpid == NLGRPS_MAX)
11700Sstevel@tonic-gate 			lgrpid = LGRP_NONE;
11710Sstevel@tonic-gate 	}
11720Sstevel@tonic-gate 
11730Sstevel@tonic-gate 	return (sfmmu_tsb_xalloc(vmp, NULL, size, vmflag, 0,
11740Sstevel@tonic-gate 	    sfmmu_tsb_page_create, lgrpid != LGRP_NONE? &lgrpid : NULL));
11750Sstevel@tonic-gate }
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate /* Called to free spans from the TSB vmem arenas */
11780Sstevel@tonic-gate void
11790Sstevel@tonic-gate sfmmu_tsb_segkmem_free(vmem_t *vmp, void *inaddr, size_t size)
11800Sstevel@tonic-gate {
11810Sstevel@tonic-gate 	page_t *pp;
11820Sstevel@tonic-gate 	caddr_t addr = inaddr;
11830Sstevel@tonic-gate 	caddr_t eaddr;
11840Sstevel@tonic-gate 	pgcnt_t npages = btopr(size);
11850Sstevel@tonic-gate 	pgcnt_t pgs_left = npages;
11860Sstevel@tonic-gate 	page_t *rootpp = NULL;
11870Sstevel@tonic-gate 
11880Sstevel@tonic-gate 	ASSERT(((uintptr_t)addr & (tsb_slab_size - 1)) == 0);
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 	hat_unload(kas.a_hat, addr, size, HAT_UNLOAD_UNLOCK);
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 	for (eaddr = addr + size; addr < eaddr; addr += PAGESIZE) {
11930Sstevel@tonic-gate 		pp = page_lookup(&kvp, (u_offset_t)(uintptr_t)addr, SE_EXCL);
11940Sstevel@tonic-gate 		if (pp == NULL)
11950Sstevel@tonic-gate 			panic("sfmmu_tsb_segkmem_free: page not found");
11960Sstevel@tonic-gate 
11970Sstevel@tonic-gate 		ASSERT(PAGE_EXCL(pp));
11980Sstevel@tonic-gate 		page_pp_unlock(pp, 0, 1);
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 		if (rootpp == NULL)
12010Sstevel@tonic-gate 			rootpp = pp;
12020Sstevel@tonic-gate 		if (--pgs_left == 0) {
12030Sstevel@tonic-gate 			/*
12040Sstevel@tonic-gate 			 * similar logic to segspt_free_pages, but we know we
12050Sstevel@tonic-gate 			 * have one large page.
12060Sstevel@tonic-gate 			 */
12070Sstevel@tonic-gate 			page_destroy_pages(rootpp);
12080Sstevel@tonic-gate 		}
12090Sstevel@tonic-gate 	}
12100Sstevel@tonic-gate 	page_unresv(npages);
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate 	if (vmp != NULL)
12130Sstevel@tonic-gate 		vmem_xfree(vmp, inaddr, size);
12140Sstevel@tonic-gate }
1215