xref: /onnv-gate/usr/src/uts/sun4/vm/sfmmu.c (revision 12908:80a39220b451)
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
52241Shuah  * Common Development and Distribution License (the "License").
62241Shuah  * 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  */
2111311SSurya.Prakki@Sun.COM 
220Sstevel@tonic-gate /*
23*12908SPavel.Tatashin@Sun.COM  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <vm/hat.h>
280Sstevel@tonic-gate #include <vm/hat_sfmmu.h>
290Sstevel@tonic-gate #include <vm/page.h>
300Sstevel@tonic-gate #include <sys/pte.h>
310Sstevel@tonic-gate #include <sys/systm.h>
320Sstevel@tonic-gate #include <sys/mman.h>
330Sstevel@tonic-gate #include <sys/sysmacros.h>
340Sstevel@tonic-gate #include <sys/machparam.h>
350Sstevel@tonic-gate #include <sys/vtrace.h>
360Sstevel@tonic-gate #include <sys/kmem.h>
370Sstevel@tonic-gate #include <sys/mmu.h>
380Sstevel@tonic-gate #include <sys/cmn_err.h>
390Sstevel@tonic-gate #include <sys/cpu.h>
400Sstevel@tonic-gate #include <sys/cpuvar.h>
410Sstevel@tonic-gate #include <sys/debug.h>
420Sstevel@tonic-gate #include <sys/lgrp.h>
430Sstevel@tonic-gate #include <sys/archsystm.h>
440Sstevel@tonic-gate #include <sys/machsystm.h>
450Sstevel@tonic-gate #include <sys/vmsystm.h>
460Sstevel@tonic-gate #include <sys/bitmap.h>
470Sstevel@tonic-gate #include <vm/as.h>
480Sstevel@tonic-gate #include <vm/seg.h>
490Sstevel@tonic-gate #include <vm/seg_kmem.h>
500Sstevel@tonic-gate #include <vm/seg_kp.h>
510Sstevel@tonic-gate #include <vm/seg_kpm.h>
520Sstevel@tonic-gate #include <vm/rm.h>
530Sstevel@tonic-gate #include <vm/vm_dep.h>
540Sstevel@tonic-gate #include <sys/t_lock.h>
550Sstevel@tonic-gate #include <sys/vm_machparam.h>
560Sstevel@tonic-gate #include <sys/promif.h>
570Sstevel@tonic-gate #include <sys/prom_isa.h>
580Sstevel@tonic-gate #include <sys/prom_plat.h>
590Sstevel@tonic-gate #include <sys/prom_debug.h>
600Sstevel@tonic-gate #include <sys/privregs.h>
610Sstevel@tonic-gate #include <sys/bootconf.h>
620Sstevel@tonic-gate #include <sys/memlist.h>
630Sstevel@tonic-gate #include <sys/memlist_plat.h>
640Sstevel@tonic-gate #include <sys/cpu_module.h>
650Sstevel@tonic-gate #include <sys/reboot.h>
660Sstevel@tonic-gate #include <sys/kdi.h>
670Sstevel@tonic-gate 
680Sstevel@tonic-gate /*
690Sstevel@tonic-gate  * Static routines
700Sstevel@tonic-gate  */
710Sstevel@tonic-gate static void	sfmmu_map_prom_mappings(struct translation *, size_t);
720Sstevel@tonic-gate static struct translation *read_prom_mappings(size_t *);
730Sstevel@tonic-gate static void	sfmmu_reloc_trap_handler(void *, void *, size_t);
740Sstevel@tonic-gate 
750Sstevel@tonic-gate /*
760Sstevel@tonic-gate  * External routines
770Sstevel@tonic-gate  */
780Sstevel@tonic-gate extern void sfmmu_remap_kernel(void);
790Sstevel@tonic-gate extern void sfmmu_patch_utsb(void);
800Sstevel@tonic-gate 
810Sstevel@tonic-gate /*
820Sstevel@tonic-gate  * Global Data:
830Sstevel@tonic-gate  */
840Sstevel@tonic-gate extern caddr_t	textva, datava;
850Sstevel@tonic-gate extern tte_t	ktext_tte, kdata_tte;	/* ttes for kernel text and data */
860Sstevel@tonic-gate extern int	enable_bigktsb;
877218Ssvemuri extern int	kmem64_smchunks;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate uint64_t memsegspa = (uintptr_t)MSEG_NULLPTR_PA; /* memsegs physical linkage */
900Sstevel@tonic-gate uint64_t memseg_phash[N_MEM_SLOTS];	/* use physical memseg addresses */
910Sstevel@tonic-gate 
920Sstevel@tonic-gate int	sfmmu_kern_mapped = 0;
930Sstevel@tonic-gate 
940Sstevel@tonic-gate /*
950Sstevel@tonic-gate  * DMMU primary context register for the kernel context. Machine specific code
960Sstevel@tonic-gate  * inserts correct page size codes when necessary
970Sstevel@tonic-gate  */
980Sstevel@tonic-gate uint64_t kcontextreg = KCONTEXT;
990Sstevel@tonic-gate 
1004104Sblakej #ifdef DEBUG
1014104Sblakej static int ndata_middle_hole_detected = 0;
1024104Sblakej #endif
1034104Sblakej 
1040Sstevel@tonic-gate /* Extern Global Data */
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate extern int page_relocate_ready;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate /*
1090Sstevel@tonic-gate  * Controls the logic which enables the use of the
1100Sstevel@tonic-gate  * QUAD_LDD_PHYS ASI for TSB accesses.
1110Sstevel@tonic-gate  */
1120Sstevel@tonic-gate extern int	ktsb_phys;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate /*
1150Sstevel@tonic-gate  * Global Routines called from within:
1160Sstevel@tonic-gate  *	usr/src/uts/sun4u
1170Sstevel@tonic-gate  *	usr/src/uts/sfmmu
1180Sstevel@tonic-gate  *	usr/src/uts/sun
1190Sstevel@tonic-gate  */
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate pfn_t
va_to_pfn(void * vaddr)1220Sstevel@tonic-gate va_to_pfn(void *vaddr)
1230Sstevel@tonic-gate {
1240Sstevel@tonic-gate 	u_longlong_t physaddr;
1250Sstevel@tonic-gate 	int mode, valid;
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	if (tba_taken_over)
1280Sstevel@tonic-gate 		return (hat_getpfnum(kas.a_hat, (caddr_t)vaddr));
1290Sstevel@tonic-gate 
1303764Sdp78419 #if !defined(C_OBP)
1317218Ssvemuri 	if (!kmem64_smchunks &&
1327218Ssvemuri 	    (caddr_t)vaddr >= kmem64_base && (caddr_t)vaddr < kmem64_end) {
1333764Sdp78419 		if (kmem64_pabase == (uint64_t)-1)
1343764Sdp78419 			prom_panic("va_to_pfn: kmem64_pabase not init");
1353764Sdp78419 		physaddr = kmem64_pabase + ((caddr_t)vaddr - kmem64_base);
1363764Sdp78419 		return ((pfn_t)physaddr >> MMU_PAGESHIFT);
1373764Sdp78419 	}
1383764Sdp78419 #endif	/* !C_OBP */
1393764Sdp78419 
1400Sstevel@tonic-gate 	if ((prom_translate_virt(vaddr, &valid, &physaddr, &mode) != -1) &&
1410Sstevel@tonic-gate 	    (valid == -1)) {
1420Sstevel@tonic-gate 		return ((pfn_t)(physaddr >> MMU_PAGESHIFT));
1430Sstevel@tonic-gate 	}
1440Sstevel@tonic-gate 	return (PFN_INVALID);
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate uint64_t
va_to_pa(void * vaddr)1480Sstevel@tonic-gate va_to_pa(void *vaddr)
1490Sstevel@tonic-gate {
1500Sstevel@tonic-gate 	pfn_t pfn;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	if ((pfn = va_to_pfn(vaddr)) == PFN_INVALID)
1530Sstevel@tonic-gate 		return ((uint64_t)-1);
1540Sstevel@tonic-gate 	return (((uint64_t)pfn << MMU_PAGESHIFT) |
1555075Spaulsan 	    ((uint64_t)vaddr & MMU_PAGEOFFSET));
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate void
hat_kern_setup(void)1590Sstevel@tonic-gate hat_kern_setup(void)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate 	struct translation *trans_root;
1620Sstevel@tonic-gate 	size_t ntrans_root;
1630Sstevel@tonic-gate 	extern void startup_fixup_physavail(void);
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	/*
1660Sstevel@tonic-gate 	 * These are the steps we take to take over the mmu from the prom.
1670Sstevel@tonic-gate 	 *
1680Sstevel@tonic-gate 	 * (1)	Read the prom's mappings through the translation property.
1690Sstevel@tonic-gate 	 * (2)	Remap the kernel text and kernel data with 2 locked 4MB ttes.
1700Sstevel@tonic-gate 	 *	Create the the hmeblks for these 2 ttes at this time.
1710Sstevel@tonic-gate 	 * (3)	Create hat structures for all other prom mappings.  Since the
1720Sstevel@tonic-gate 	 *	kernel text and data hme_blks have already been created we
1730Sstevel@tonic-gate 	 *	skip the equivalent prom's mappings.
1740Sstevel@tonic-gate 	 * (4)	Initialize the tsb and its corresponding hardware regs.
1750Sstevel@tonic-gate 	 * (5)	Take over the trap table (currently in startup).
1760Sstevel@tonic-gate 	 * (6)	Up to this point it is possible the prom required some of its
1770Sstevel@tonic-gate 	 *	locked tte's.  Now that we own the trap table we remove them.
1780Sstevel@tonic-gate 	 */
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	ktsb_pbase = va_to_pa(ktsb_base);
1810Sstevel@tonic-gate 	ktsb4m_pbase = va_to_pa(ktsb4m_base);
1820Sstevel@tonic-gate 	PRM_DEBUG(ktsb_pbase);
1830Sstevel@tonic-gate 	PRM_DEBUG(ktsb4m_pbase);
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	sfmmu_patch_ktsb();
1860Sstevel@tonic-gate 	sfmmu_patch_utsb();
1870Sstevel@tonic-gate 	sfmmu_patch_mmu_asi(ktsb_phys);
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	sfmmu_init_tsbs();
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	if (kpm_enable) {
1920Sstevel@tonic-gate 		sfmmu_kpm_patch_tlbm();
1930Sstevel@tonic-gate 		if (kpm_smallpages == 0) {
1940Sstevel@tonic-gate 			sfmmu_kpm_patch_tsbm();
1950Sstevel@tonic-gate 		}
1960Sstevel@tonic-gate 	}
1970Sstevel@tonic-gate 
1985075Spaulsan 	if (!shctx_on) {
1994528Spaulsan 		sfmmu_patch_shctx();
2004528Spaulsan 	}
2014528Spaulsan 
2020Sstevel@tonic-gate 	/*
2030Sstevel@tonic-gate 	 * The 8K-indexed kernel TSB space is used to hold
2040Sstevel@tonic-gate 	 * translations below...
2050Sstevel@tonic-gate 	 */
2060Sstevel@tonic-gate 	trans_root = read_prom_mappings(&ntrans_root);
2070Sstevel@tonic-gate 	sfmmu_remap_kernel();
2080Sstevel@tonic-gate 	startup_fixup_physavail();
2090Sstevel@tonic-gate 	mmu_init_kernel_pgsz(kas.a_hat);
2100Sstevel@tonic-gate 	sfmmu_map_prom_mappings(trans_root, ntrans_root);
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	/*
2130Sstevel@tonic-gate 	 * We invalidate 8K kernel TSB because we used it in
2140Sstevel@tonic-gate 	 * sfmmu_map_prom_mappings()
2150Sstevel@tonic-gate 	 */
2160Sstevel@tonic-gate 	sfmmu_inv_tsb(ktsb_base, ktsb_sz);
2170Sstevel@tonic-gate 	sfmmu_inv_tsb(ktsb4m_base, ktsb4m_sz);
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	sfmmu_init_ktsbinfo();
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	sfmmu_kern_mapped = 1;
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	/*
2250Sstevel@tonic-gate 	 * hments have been created for mapped pages, and thus we're ready
2260Sstevel@tonic-gate 	 * for kmdb to start using its own trap table.  It walks the hments
2270Sstevel@tonic-gate 	 * to resolve TLB misses, and can't be used until they're ready.
2280Sstevel@tonic-gate 	 */
2290Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
2300Sstevel@tonic-gate 		kdi_dvec_vmready();
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate /*
2340Sstevel@tonic-gate  * Macro used below to convert the prom's 32-bit high and low fields into
2350Sstevel@tonic-gate  * a value appropriate for the 64-bit kernel.
2360Sstevel@tonic-gate  */
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate #define	COMBINE(hi, lo) (((uint64_t)(uint32_t)(hi) << 32) | (uint32_t)(lo))
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate /*
2413764Sdp78419  * Track larges pages used.
2423764Sdp78419  * Provides observability for this feature on non-debug kernels.
2433764Sdp78419  */
2443764Sdp78419 ulong_t map_prom_lpcount[MMU_PAGE_SIZES];
2453764Sdp78419 
2463764Sdp78419 /*
2470Sstevel@tonic-gate  * This function traverses the prom mapping list and creates equivalent
2480Sstevel@tonic-gate  * mappings in the sfmmu mapping hash.
2490Sstevel@tonic-gate  */
2500Sstevel@tonic-gate static void
sfmmu_map_prom_mappings(struct translation * trans_root,size_t ntrans_root)2510Sstevel@tonic-gate sfmmu_map_prom_mappings(struct translation *trans_root, size_t ntrans_root)
2520Sstevel@tonic-gate {
2530Sstevel@tonic-gate 	struct translation *promt;
2540Sstevel@tonic-gate 	tte_t	tte, oldtte, *ttep;
2550Sstevel@tonic-gate 	pfn_t	pfn, oldpfn, basepfn;
2560Sstevel@tonic-gate 	caddr_t vaddr;
2570Sstevel@tonic-gate 	size_t	size, offset;
2580Sstevel@tonic-gate 	unsigned long i;
2590Sstevel@tonic-gate 	uint_t	attr;
2600Sstevel@tonic-gate 	page_t *pp;
2610Sstevel@tonic-gate 	extern struct memlist *virt_avail;
2624610Sjesusm 	char buf[256];
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	ttep = &tte;
2650Sstevel@tonic-gate 	for (i = 0, promt = trans_root; i < ntrans_root; i++, promt++) {
2660Sstevel@tonic-gate 		ASSERT(promt->tte_hi != 0);
2670Sstevel@tonic-gate 		ASSERT32(promt->virt_hi == 0 && promt->size_hi == 0);
2680Sstevel@tonic-gate 
2693764Sdp78419 		vaddr = (caddr_t)COMBINE(promt->virt_hi, promt->virt_lo);
2703764Sdp78419 
2710Sstevel@tonic-gate 		/*
2720Sstevel@tonic-gate 		 * hack until we get rid of map-for-unix
2730Sstevel@tonic-gate 		 */
2743764Sdp78419 		if (vaddr < (caddr_t)KERNELBASE)
2750Sstevel@tonic-gate 			continue;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 		ttep->tte_inthi = promt->tte_hi;
2780Sstevel@tonic-gate 		ttep->tte_intlo = promt->tte_lo;
2790Sstevel@tonic-gate 		attr = PROC_DATA | HAT_NOSYNC;
2800Sstevel@tonic-gate #if defined(TTE_IS_GLOBAL)
2810Sstevel@tonic-gate 		if (TTE_IS_GLOBAL(ttep)) {
2820Sstevel@tonic-gate 			/*
2830Sstevel@tonic-gate 			 * The prom better not use global translations
2840Sstevel@tonic-gate 			 * because a user process might use the same
2850Sstevel@tonic-gate 			 * virtual addresses
2860Sstevel@tonic-gate 			 */
2874610Sjesusm 			prom_panic("sfmmu_map_prom_mappings: global"
2884610Sjesusm 			    " translation");
2890Sstevel@tonic-gate 			TTE_SET_LOFLAGS(ttep, TTE_GLB_INT, 0);
2900Sstevel@tonic-gate 		}
2910Sstevel@tonic-gate #endif
2920Sstevel@tonic-gate 		if (TTE_IS_LOCKED(ttep)) {
2930Sstevel@tonic-gate 			/* clear the lock bits */
2940Sstevel@tonic-gate 			TTE_CLR_LOCKED(ttep);
2950Sstevel@tonic-gate 		}
2960Sstevel@tonic-gate 		attr |= (TTE_IS_VCACHEABLE(ttep)) ? 0 : SFMMU_UNCACHEVTTE;
2970Sstevel@tonic-gate 		attr |= (TTE_IS_PCACHEABLE(ttep)) ? 0 : SFMMU_UNCACHEPTTE;
2980Sstevel@tonic-gate 		attr |= (TTE_IS_SIDEFFECT(ttep)) ? SFMMU_SIDEFFECT : 0;
2990Sstevel@tonic-gate 		attr |= (TTE_IS_IE(ttep)) ? HAT_STRUCTURE_LE : 0;
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 		size = COMBINE(promt->size_hi, promt->size_lo);
3020Sstevel@tonic-gate 		offset = 0;
3030Sstevel@tonic-gate 		basepfn = TTE_TO_PFN((caddr_t)COMBINE(promt->virt_hi,
3040Sstevel@tonic-gate 		    promt->virt_lo), ttep);
3050Sstevel@tonic-gate 		while (size) {
3060Sstevel@tonic-gate 			vaddr = (caddr_t)(COMBINE(promt->virt_hi,
3070Sstevel@tonic-gate 			    promt->virt_lo) + offset);
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 			/*
3100Sstevel@tonic-gate 			 * make sure address is not in virt-avail list
3110Sstevel@tonic-gate 			 */
3120Sstevel@tonic-gate 			if (address_in_memlist(virt_avail, (uint64_t)vaddr,
3130Sstevel@tonic-gate 			    size)) {
3144610Sjesusm 				prom_panic("sfmmu_map_prom_mappings:"
3154610Sjesusm 				    " inconsistent translation/avail lists");
3160Sstevel@tonic-gate 			}
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 			pfn = basepfn + mmu_btop(offset);
3190Sstevel@tonic-gate 			if (pf_is_memory(pfn)) {
3200Sstevel@tonic-gate 				if (attr & SFMMU_UNCACHEPTTE) {
3214610Sjesusm 					prom_panic("sfmmu_map_prom_mappings:"
3224610Sjesusm 					    " uncached prom memory page");
3230Sstevel@tonic-gate 				}
3240Sstevel@tonic-gate 			} else {
3250Sstevel@tonic-gate 				if (!(attr & SFMMU_SIDEFFECT)) {
3264610Sjesusm 					prom_panic("sfmmu_map_prom_mappings:"
3274610Sjesusm 					    " prom i/o page without"
3284610Sjesusm 					    " side-effect");
3290Sstevel@tonic-gate 				}
3300Sstevel@tonic-gate 			}
3313764Sdp78419 
3323764Sdp78419 			/*
3333764Sdp78419 			 * skip kmem64 area
3343764Sdp78419 			 */
3357218Ssvemuri 			if (!kmem64_smchunks &&
3367218Ssvemuri 			    vaddr >= kmem64_base &&
3373764Sdp78419 			    vaddr < kmem64_aligned_end) {
3383764Sdp78419 #if !defined(C_OBP)
3394610Sjesusm 				prom_panic("sfmmu_map_prom_mappings:"
3404610Sjesusm 				    " unexpected kmem64 prom mapping");
3413764Sdp78419 #else	/* !C_OBP */
3423764Sdp78419 				size_t mapsz;
3433764Sdp78419 
3443764Sdp78419 				if (ptob(pfn) !=
3453764Sdp78419 				    kmem64_pabase + (vaddr - kmem64_base)) {
3464610Sjesusm 					prom_panic("sfmmu_map_prom_mappings:"
3474610Sjesusm 					    " unexpected kmem64 prom mapping");
3483764Sdp78419 				}
3493764Sdp78419 
3503764Sdp78419 				mapsz = kmem64_aligned_end - vaddr;
3513764Sdp78419 				if (mapsz >= size) {
3523764Sdp78419 					break;
3533764Sdp78419 				}
3543764Sdp78419 				size -= mapsz;
3553764Sdp78419 				offset += mapsz;
3563764Sdp78419 				continue;
3573764Sdp78419 #endif	/* !C_OBP */
3583764Sdp78419 			}
3593764Sdp78419 
3600Sstevel@tonic-gate 			oldpfn = sfmmu_vatopfn(vaddr, KHATID, &oldtte);
3610Sstevel@tonic-gate 			ASSERT(oldpfn != PFN_SUSPENDED);
3620Sstevel@tonic-gate 			ASSERT(page_relocate_ready == 0);
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 			if (oldpfn != PFN_INVALID) {
3650Sstevel@tonic-gate 				/*
3660Sstevel@tonic-gate 				 * mapping already exists.
3670Sstevel@tonic-gate 				 * Verify they are equal
3680Sstevel@tonic-gate 				 */
3690Sstevel@tonic-gate 				if (pfn != oldpfn) {
3704610Sjesusm 					(void) snprintf(buf, sizeof (buf),
3714610Sjesusm 					"sfmmu_map_prom_mappings: mapping"
3724610Sjesusm 					" conflict (va = 0x%p, pfn = 0x%p,"
3734610Sjesusm 					" oldpfn = 0x%p)", (void *)vaddr,
3744610Sjesusm 					    (void *)pfn, (void *)oldpfn);
3754610Sjesusm 					prom_panic(buf);
3760Sstevel@tonic-gate 				}
3770Sstevel@tonic-gate 				size -= MMU_PAGESIZE;
3780Sstevel@tonic-gate 				offset += MMU_PAGESIZE;
3790Sstevel@tonic-gate 				continue;
3800Sstevel@tonic-gate 			}
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 			pp = page_numtopp_nolock(pfn);
3830Sstevel@tonic-gate 			if ((pp != NULL) && PP_ISFREE((page_t *)pp)) {
3844610Sjesusm 				(void) snprintf(buf, sizeof (buf),
3854610Sjesusm 				"sfmmu_map_prom_mappings: prom-mapped"
3864610Sjesusm 				" page (va = 0x%p, pfn = 0x%p) on free list",
3874610Sjesusm 				    (void *)vaddr, (void *)pfn);
3884610Sjesusm 				prom_panic(buf);
3890Sstevel@tonic-gate 			}
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 			sfmmu_memtte(ttep, pfn, attr, TTE8K);
3920Sstevel@tonic-gate 			sfmmu_tteload(kas.a_hat, ttep, vaddr, pp,
3930Sstevel@tonic-gate 			    HAT_LOAD_LOCK | SFMMU_NO_TSBLOAD);
3940Sstevel@tonic-gate 			size -= MMU_PAGESIZE;
3950Sstevel@tonic-gate 			offset += MMU_PAGESIZE;
3960Sstevel@tonic-gate 		}
3970Sstevel@tonic-gate 	}
3983764Sdp78419 
3993764Sdp78419 	/*
4003764Sdp78419 	 * We claimed kmem64 from prom, so now we need to load tte.
4013764Sdp78419 	 */
4027218Ssvemuri 	if (!kmem64_smchunks && kmem64_base != NULL) {
4033764Sdp78419 		pgcnt_t pages;
4043764Sdp78419 		size_t psize;
4053764Sdp78419 		int pszc;
4063764Sdp78419 
4073764Sdp78419 		pszc = kmem64_szc;
4083764Sdp78419 #ifdef sun4u
4093764Sdp78419 		if (pszc > TTE8K) {
4103764Sdp78419 			pszc = segkmem_lpszc;
4113764Sdp78419 		}
4123764Sdp78419 #endif	/* sun4u */
4133764Sdp78419 		psize = TTEBYTES(pszc);
4143764Sdp78419 		pages = btop(psize);
4153764Sdp78419 		basepfn = kmem64_pabase >> MMU_PAGESHIFT;
4163764Sdp78419 		vaddr = kmem64_base;
4173764Sdp78419 		while (vaddr < kmem64_end) {
4183764Sdp78419 			sfmmu_memtte(ttep, basepfn,
4193764Sdp78419 			    PROC_DATA | HAT_NOSYNC, pszc);
4203764Sdp78419 			sfmmu_tteload(kas.a_hat, ttep, vaddr, NULL,
4213764Sdp78419 			    HAT_LOAD_LOCK | SFMMU_NO_TSBLOAD);
4223764Sdp78419 			vaddr += psize;
4233764Sdp78419 			basepfn += pages;
4243764Sdp78419 		}
4253764Sdp78419 		map_prom_lpcount[pszc] =
4263764Sdp78419 		    ((caddr_t)P2ROUNDUP((uintptr_t)kmem64_end, psize) -
4274610Sjesusm 		    kmem64_base) >> TTE_PAGE_SHIFT(pszc);
4283764Sdp78419 	}
4290Sstevel@tonic-gate }
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate #undef COMBINE	/* local to previous routine */
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate /*
4340Sstevel@tonic-gate  * This routine reads in the "translations" property in to a buffer and
4350Sstevel@tonic-gate  * returns a pointer to this buffer and the number of translations.
4360Sstevel@tonic-gate  */
4370Sstevel@tonic-gate static struct translation *
read_prom_mappings(size_t * ntransrootp)4380Sstevel@tonic-gate read_prom_mappings(size_t *ntransrootp)
4390Sstevel@tonic-gate {
4400Sstevel@tonic-gate 	char *prop = "translations";
4410Sstevel@tonic-gate 	size_t translen;
442789Sahrens 	pnode_t node;
4430Sstevel@tonic-gate 	struct translation *transroot;
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 	/*
4460Sstevel@tonic-gate 	 * the "translations" property is associated with the mmu node
4470Sstevel@tonic-gate 	 */
448789Sahrens 	node = (pnode_t)prom_getphandle(prom_mmu_ihandle());
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	/*
4510Sstevel@tonic-gate 	 * We use the TSB space to read in the prom mappings.  This space
4520Sstevel@tonic-gate 	 * is currently not being used because we haven't taken over the
4530Sstevel@tonic-gate 	 * trap table yet.  It should be big enough to hold the mappings.
4540Sstevel@tonic-gate 	 */
4550Sstevel@tonic-gate 	if ((translen = prom_getproplen(node, prop)) == -1)
4560Sstevel@tonic-gate 		cmn_err(CE_PANIC, "no translations property");
4570Sstevel@tonic-gate 	*ntransrootp = translen / sizeof (*transroot);
4580Sstevel@tonic-gate 	translen = roundup(translen, MMU_PAGESIZE);
4590Sstevel@tonic-gate 	PRM_DEBUG(translen);
4600Sstevel@tonic-gate 	if (translen > TSB_BYTES(ktsb_szcode))
4610Sstevel@tonic-gate 		cmn_err(CE_PANIC, "not enough space for translations");
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	transroot = (struct translation *)ktsb_base;
4640Sstevel@tonic-gate 	ASSERT(transroot);
4650Sstevel@tonic-gate 	if (prom_getprop(node, prop, (caddr_t)transroot) == -1) {
4660Sstevel@tonic-gate 		cmn_err(CE_PANIC, "translations getprop failed");
4670Sstevel@tonic-gate 	}
4680Sstevel@tonic-gate 	return (transroot);
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate /*
4720Sstevel@tonic-gate  * Init routine of the nucleus data memory allocator.
4730Sstevel@tonic-gate  *
4740Sstevel@tonic-gate  * The nucleus data memory allocator is organized in ecache_alignsize'd
4750Sstevel@tonic-gate  * memory chunks. Memory allocated by ndata_alloc() will never be freed.
4760Sstevel@tonic-gate  *
4770Sstevel@tonic-gate  * The ndata argument is used as header of the ndata freelist.
4780Sstevel@tonic-gate  * Other freelist nodes are placed in the nucleus memory itself
4790Sstevel@tonic-gate  * at the beginning of a free memory chunk. Therefore a freelist
4800Sstevel@tonic-gate  * node (struct memlist) must fit into the smallest allocatable
4810Sstevel@tonic-gate  * memory chunk (ecache_alignsize bytes).
4820Sstevel@tonic-gate  *
4830Sstevel@tonic-gate  * The memory interval [base, end] passed to ndata_alloc_init() must be
4840Sstevel@tonic-gate  * bzero'd to allow the allocator to return bzero'd memory easily.
4850Sstevel@tonic-gate  */
4860Sstevel@tonic-gate void
ndata_alloc_init(struct memlist * ndata,uintptr_t base,uintptr_t end)4870Sstevel@tonic-gate ndata_alloc_init(struct memlist *ndata, uintptr_t base, uintptr_t end)
4880Sstevel@tonic-gate {
4890Sstevel@tonic-gate 	ASSERT(sizeof (struct memlist) <= ecache_alignsize);
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	base = roundup(base, ecache_alignsize);
4920Sstevel@tonic-gate 	end = end - end % ecache_alignsize;
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	ASSERT(base < end);
4950Sstevel@tonic-gate 
49611474SJonathan.Adams@Sun.COM 	ndata->ml_address = base;
49711474SJonathan.Adams@Sun.COM 	ndata->ml_size = end - base;
49811474SJonathan.Adams@Sun.COM 	ndata->ml_next = NULL;
49911474SJonathan.Adams@Sun.COM 	ndata->ml_prev = NULL;
5000Sstevel@tonic-gate }
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate /*
5030Sstevel@tonic-gate  * Deliver the size of the largest free memory chunk.
5040Sstevel@tonic-gate  */
5050Sstevel@tonic-gate size_t
ndata_maxsize(struct memlist * ndata)5060Sstevel@tonic-gate ndata_maxsize(struct memlist *ndata)
5070Sstevel@tonic-gate {
50811474SJonathan.Adams@Sun.COM 	size_t chunksize = ndata->ml_size;
5090Sstevel@tonic-gate 
51011474SJonathan.Adams@Sun.COM 	while ((ndata = ndata->ml_next) != NULL) {
51111474SJonathan.Adams@Sun.COM 		if (chunksize < ndata->ml_size)
51211474SJonathan.Adams@Sun.COM 			chunksize = ndata->ml_size;
5130Sstevel@tonic-gate 	}
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	return (chunksize);
5160Sstevel@tonic-gate }
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate /*
5200Sstevel@tonic-gate  * Allocate the last properly aligned memory chunk.
5210Sstevel@tonic-gate  * This function is called when no more large nucleus memory chunks
5220Sstevel@tonic-gate  * will be allocated.  The remaining free nucleus memory at the end
5230Sstevel@tonic-gate  * of the nucleus can be added to the phys_avail list.
5240Sstevel@tonic-gate  */
5250Sstevel@tonic-gate void *
ndata_extra_base(struct memlist * ndata,size_t alignment,caddr_t endaddr)5264104Sblakej ndata_extra_base(struct memlist *ndata, size_t alignment, caddr_t endaddr)
5270Sstevel@tonic-gate {
5280Sstevel@tonic-gate 	uintptr_t base;
5290Sstevel@tonic-gate 	size_t wasteage = 0;
5300Sstevel@tonic-gate #ifdef	DEBUG
5310Sstevel@tonic-gate 	static int called = 0;
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	if (called++ > 0)
5340Sstevel@tonic-gate 		cmn_err(CE_PANIC, "ndata_extra_base() called more than once");
5350Sstevel@tonic-gate #endif /* DEBUG */
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	/*
5380Sstevel@tonic-gate 	 * The alignment needs to be a multiple of ecache_alignsize.
5390Sstevel@tonic-gate 	 */
5400Sstevel@tonic-gate 	ASSERT((alignment % ecache_alignsize) ==  0);
5410Sstevel@tonic-gate 
54211474SJonathan.Adams@Sun.COM 	while (ndata->ml_next != NULL) {
54311474SJonathan.Adams@Sun.COM 		wasteage += ndata->ml_size;
54411474SJonathan.Adams@Sun.COM 		ndata = ndata->ml_next;
5450Sstevel@tonic-gate 	}
5460Sstevel@tonic-gate 
54711474SJonathan.Adams@Sun.COM 	base = roundup(ndata->ml_address, alignment);
5480Sstevel@tonic-gate 
54911474SJonathan.Adams@Sun.COM 	if (base >= ndata->ml_address + ndata->ml_size)
5500Sstevel@tonic-gate 		return (NULL);
5510Sstevel@tonic-gate 
55211474SJonathan.Adams@Sun.COM 	if ((caddr_t)(ndata->ml_address + ndata->ml_size) != endaddr) {
5534104Sblakej #ifdef DEBUG
5544104Sblakej 		ndata_middle_hole_detected = 1;	/* see if we hit this again */
5554104Sblakej #endif
5564104Sblakej 		return (NULL);
5574104Sblakej 	}
5584104Sblakej 
55911474SJonathan.Adams@Sun.COM 	if (base == ndata->ml_address) {
56011474SJonathan.Adams@Sun.COM 		if (ndata->ml_prev != NULL)
56111474SJonathan.Adams@Sun.COM 			ndata->ml_prev->ml_next = NULL;
5620Sstevel@tonic-gate 		else
56311474SJonathan.Adams@Sun.COM 			ndata->ml_size = 0;
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 		bzero((void *)base, sizeof (struct memlist));
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	} else {
56811474SJonathan.Adams@Sun.COM 		ndata->ml_size = base - ndata->ml_address;
56911474SJonathan.Adams@Sun.COM 		wasteage += ndata->ml_size;
5700Sstevel@tonic-gate 	}
5710Sstevel@tonic-gate 	PRM_DEBUG(wasteage);
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	return ((void *)base);
5740Sstevel@tonic-gate }
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate /*
5770Sstevel@tonic-gate  * Select the best matching buffer, avoid memory fragmentation.
5780Sstevel@tonic-gate  */
5790Sstevel@tonic-gate static struct memlist *
ndata_select_chunk(struct memlist * ndata,size_t wanted,size_t alignment)5800Sstevel@tonic-gate ndata_select_chunk(struct memlist *ndata, size_t wanted, size_t alignment)
5810Sstevel@tonic-gate {
5820Sstevel@tonic-gate 	struct memlist *fnd_below = NULL;
5830Sstevel@tonic-gate 	struct memlist *fnd_above = NULL;
5840Sstevel@tonic-gate 	struct memlist *fnd_unused = NULL;
5850Sstevel@tonic-gate 	struct memlist *frlist;
5860Sstevel@tonic-gate 	uintptr_t base;
5870Sstevel@tonic-gate 	uintptr_t end;
5880Sstevel@tonic-gate 	size_t below;
5890Sstevel@tonic-gate 	size_t above;
5900Sstevel@tonic-gate 	size_t unused;
5910Sstevel@tonic-gate 	size_t best_below = ULONG_MAX;
5920Sstevel@tonic-gate 	size_t best_above = ULONG_MAX;
5930Sstevel@tonic-gate 	size_t best_unused = ULONG_MAX;
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	ASSERT(ndata != NULL);
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	/*
5980Sstevel@tonic-gate 	 * Look for the best matching buffer, avoid memory fragmentation.
5990Sstevel@tonic-gate 	 * The following strategy is used, try to find
6000Sstevel@tonic-gate 	 *   1. an exact fitting buffer
6010Sstevel@tonic-gate 	 *   2. avoid wasting any space below the buffer, take first
6020Sstevel@tonic-gate 	 *	fitting buffer
6030Sstevel@tonic-gate 	 *   3. avoid wasting any space above the buffer, take first
6040Sstevel@tonic-gate 	 *	fitting buffer
6050Sstevel@tonic-gate 	 *   4. avoid wasting space, take first fitting buffer
6060Sstevel@tonic-gate 	 *   5. take the last buffer in chain
6070Sstevel@tonic-gate 	 */
60811474SJonathan.Adams@Sun.COM 	for (frlist = ndata; frlist != NULL; frlist = frlist->ml_next) {
60911474SJonathan.Adams@Sun.COM 		base = roundup(frlist->ml_address, alignment);
6100Sstevel@tonic-gate 		end = roundup(base + wanted, ecache_alignsize);
6110Sstevel@tonic-gate 
61211474SJonathan.Adams@Sun.COM 		if (end > frlist->ml_address + frlist->ml_size)
6130Sstevel@tonic-gate 			continue;
6140Sstevel@tonic-gate 
61511474SJonathan.Adams@Sun.COM 		below = (base - frlist->ml_address) / ecache_alignsize;
61611474SJonathan.Adams@Sun.COM 		above = (frlist->ml_address + frlist->ml_size - end) /
6170Sstevel@tonic-gate 		    ecache_alignsize;
6180Sstevel@tonic-gate 		unused = below + above;
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 		if (unused == 0)
6210Sstevel@tonic-gate 			return (frlist);
6220Sstevel@tonic-gate 
62311474SJonathan.Adams@Sun.COM 		if (frlist->ml_next == NULL)
6240Sstevel@tonic-gate 			break;
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 		if (below < best_below) {
6270Sstevel@tonic-gate 			best_below = below;
6280Sstevel@tonic-gate 			fnd_below = frlist;
6290Sstevel@tonic-gate 		}
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 		if (above < best_above) {
6320Sstevel@tonic-gate 			best_above = above;
6330Sstevel@tonic-gate 			fnd_above = frlist;
6340Sstevel@tonic-gate 		}
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 		if (unused < best_unused) {
6370Sstevel@tonic-gate 			best_unused = unused;
6380Sstevel@tonic-gate 			fnd_unused = frlist;
6390Sstevel@tonic-gate 		}
6400Sstevel@tonic-gate 	}
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	if (best_below == 0)
6430Sstevel@tonic-gate 		return (fnd_below);
6440Sstevel@tonic-gate 	if (best_above == 0)
6450Sstevel@tonic-gate 		return (fnd_above);
6460Sstevel@tonic-gate 	if (best_unused < ULONG_MAX)
6470Sstevel@tonic-gate 		return (fnd_unused);
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 	return (frlist);
6500Sstevel@tonic-gate }
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate /*
6530Sstevel@tonic-gate  * Nucleus data memory allocator.
6540Sstevel@tonic-gate  * The granularity of the allocator is ecache_alignsize.
6550Sstevel@tonic-gate  * See also comment for ndata_alloc_init().
6560Sstevel@tonic-gate  */
6570Sstevel@tonic-gate void *
ndata_alloc(struct memlist * ndata,size_t wanted,size_t alignment)6580Sstevel@tonic-gate ndata_alloc(struct memlist *ndata, size_t wanted, size_t alignment)
6590Sstevel@tonic-gate {
6600Sstevel@tonic-gate 	struct memlist *found;
6610Sstevel@tonic-gate 	struct memlist *fnd_above;
6620Sstevel@tonic-gate 	uintptr_t base;
6630Sstevel@tonic-gate 	uintptr_t end;
6640Sstevel@tonic-gate 	size_t below;
6650Sstevel@tonic-gate 	size_t above;
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 	/*
6680Sstevel@tonic-gate 	 * Look for the best matching buffer, avoid memory fragmentation.
6690Sstevel@tonic-gate 	 */
6700Sstevel@tonic-gate 	if ((found = ndata_select_chunk(ndata, wanted, alignment)) == NULL)
6710Sstevel@tonic-gate 		return (NULL);
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	/*
6740Sstevel@tonic-gate 	 * Allocate the nucleus data buffer.
6750Sstevel@tonic-gate 	 */
67611474SJonathan.Adams@Sun.COM 	base = roundup(found->ml_address, alignment);
6770Sstevel@tonic-gate 	end = roundup(base + wanted, ecache_alignsize);
67811474SJonathan.Adams@Sun.COM 	ASSERT(end <= found->ml_address + found->ml_size);
6790Sstevel@tonic-gate 
68011474SJonathan.Adams@Sun.COM 	below = base - found->ml_address;
68111474SJonathan.Adams@Sun.COM 	above = found->ml_address + found->ml_size - end;
6820Sstevel@tonic-gate 	ASSERT(above == 0 || (above % ecache_alignsize) == 0);
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	if (below >= ecache_alignsize) {
6850Sstevel@tonic-gate 		/*
6860Sstevel@tonic-gate 		 * There is free memory below the allocated memory chunk.
6870Sstevel@tonic-gate 		 */
68811474SJonathan.Adams@Sun.COM 		found->ml_size = below - below % ecache_alignsize;
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 		if (above) {
6910Sstevel@tonic-gate 			fnd_above = (struct memlist *)end;
69211474SJonathan.Adams@Sun.COM 			fnd_above->ml_address = end;
69311474SJonathan.Adams@Sun.COM 			fnd_above->ml_size = above;
6940Sstevel@tonic-gate 
69511474SJonathan.Adams@Sun.COM 			if ((fnd_above->ml_next = found->ml_next) != NULL)
69611474SJonathan.Adams@Sun.COM 				found->ml_next->ml_prev = fnd_above;
69711474SJonathan.Adams@Sun.COM 			fnd_above->ml_prev = found;
69811474SJonathan.Adams@Sun.COM 			found->ml_next = fnd_above;
6990Sstevel@tonic-gate 		}
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 		return ((void *)base);
7020Sstevel@tonic-gate 	}
7030Sstevel@tonic-gate 
70411474SJonathan.Adams@Sun.COM 	if (found->ml_prev == NULL) {
7050Sstevel@tonic-gate 		/*
7060Sstevel@tonic-gate 		 * The first chunk (ndata) is selected.
7070Sstevel@tonic-gate 		 */
7080Sstevel@tonic-gate 		ASSERT(found == ndata);
7090Sstevel@tonic-gate 		if (above) {
71011474SJonathan.Adams@Sun.COM 			found->ml_address = end;
71111474SJonathan.Adams@Sun.COM 			found->ml_size = above;
71211474SJonathan.Adams@Sun.COM 		} else if (found->ml_next != NULL) {
71311474SJonathan.Adams@Sun.COM 			found->ml_address = found->ml_next->ml_address;
71411474SJonathan.Adams@Sun.COM 			found->ml_size = found->ml_next->ml_size;
71511474SJonathan.Adams@Sun.COM 			if ((found->ml_next = found->ml_next->ml_next) != NULL)
71611474SJonathan.Adams@Sun.COM 				found->ml_next->ml_prev = found;
7170Sstevel@tonic-gate 
71811474SJonathan.Adams@Sun.COM 			bzero((void *)found->ml_address,
71911474SJonathan.Adams@Sun.COM 			    sizeof (struct memlist));
7200Sstevel@tonic-gate 		} else {
72111474SJonathan.Adams@Sun.COM 			found->ml_address = end;
72211474SJonathan.Adams@Sun.COM 			found->ml_size = 0;
7230Sstevel@tonic-gate 		}
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 		return ((void *)base);
7260Sstevel@tonic-gate 	}
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 	/*
7290Sstevel@tonic-gate 	 * Not the first chunk.
7300Sstevel@tonic-gate 	 */
7310Sstevel@tonic-gate 	if (above) {
7320Sstevel@tonic-gate 		fnd_above = (struct memlist *)end;
73311474SJonathan.Adams@Sun.COM 		fnd_above->ml_address = end;
73411474SJonathan.Adams@Sun.COM 		fnd_above->ml_size = above;
7350Sstevel@tonic-gate 
73611474SJonathan.Adams@Sun.COM 		if ((fnd_above->ml_next = found->ml_next) != NULL)
73711474SJonathan.Adams@Sun.COM 			fnd_above->ml_next->ml_prev = fnd_above;
73811474SJonathan.Adams@Sun.COM 		fnd_above->ml_prev = found->ml_prev;
73911474SJonathan.Adams@Sun.COM 		found->ml_prev->ml_next = fnd_above;
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 	} else {
74211474SJonathan.Adams@Sun.COM 		if ((found->ml_prev->ml_next = found->ml_next) != NULL)
74311474SJonathan.Adams@Sun.COM 			found->ml_next->ml_prev = found->ml_prev;
7440Sstevel@tonic-gate 	}
7450Sstevel@tonic-gate 
74611474SJonathan.Adams@Sun.COM 	bzero((void *)found->ml_address, sizeof (struct memlist));
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 	return ((void *)base);
7490Sstevel@tonic-gate }
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate /*
7520Sstevel@tonic-gate  * Size the kernel TSBs based upon the amount of physical
7530Sstevel@tonic-gate  * memory in the system.
7540Sstevel@tonic-gate  */
7550Sstevel@tonic-gate static void
calc_tsb_sizes(pgcnt_t npages)7560Sstevel@tonic-gate calc_tsb_sizes(pgcnt_t npages)
7570Sstevel@tonic-gate {
7580Sstevel@tonic-gate 	PRM_DEBUG(npages);
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 	if (npages <= TSB_FREEMEM_MIN) {
7610Sstevel@tonic-gate 		ktsb_szcode = TSB_128K_SZCODE;
7620Sstevel@tonic-gate 		enable_bigktsb = 0;
7630Sstevel@tonic-gate 	} else if (npages <= TSB_FREEMEM_LARGE / 2) {
7640Sstevel@tonic-gate 		ktsb_szcode = TSB_256K_SZCODE;
7650Sstevel@tonic-gate 		enable_bigktsb = 0;
7660Sstevel@tonic-gate 	} else if (npages <= TSB_FREEMEM_LARGE) {
7670Sstevel@tonic-gate 		ktsb_szcode = TSB_512K_SZCODE;
7680Sstevel@tonic-gate 		enable_bigktsb = 0;
7690Sstevel@tonic-gate 	} else if (npages <= TSB_FREEMEM_LARGE * 2 ||
7700Sstevel@tonic-gate 	    enable_bigktsb == 0) {
7710Sstevel@tonic-gate 		ktsb_szcode = TSB_1M_SZCODE;
7720Sstevel@tonic-gate 		enable_bigktsb = 0;
7730Sstevel@tonic-gate 	} else {
7740Sstevel@tonic-gate 		ktsb_szcode = highbit(npages - 1);
7750Sstevel@tonic-gate 		ktsb_szcode -= TSB_START_SIZE;
7760Sstevel@tonic-gate 		ktsb_szcode = MAX(ktsb_szcode, MIN_BIGKTSB_SZCODE);
7770Sstevel@tonic-gate 		ktsb_szcode = MIN(ktsb_szcode, MAX_BIGKTSB_SZCODE);
7780Sstevel@tonic-gate 	}
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	/*
7810Sstevel@tonic-gate 	 * We choose the TSB to hold kernel 4M mappings to have twice
7820Sstevel@tonic-gate 	 * the reach as the primary kernel TSB since this TSB will
7830Sstevel@tonic-gate 	 * potentially (currently) be shared by both mappings to all of
7843764Sdp78419 	 * physical memory plus user TSBs. If this TSB has to be in nucleus
7853764Sdp78419 	 * (only for Spitfire and Cheetah) limit its size to 64K.
7860Sstevel@tonic-gate 	 */
7873764Sdp78419 	ktsb4m_szcode = highbit((2 * npages) / TTEPAGES(TTE4M) - 1);
7883764Sdp78419 	ktsb4m_szcode -= TSB_START_SIZE;
7893764Sdp78419 	ktsb4m_szcode = MAX(ktsb4m_szcode, TSB_MIN_SZCODE);
7903764Sdp78419 	ktsb4m_szcode = MIN(ktsb4m_szcode, TSB_SOFTSZ_MASK);
7913764Sdp78419 	if ((enable_bigktsb == 0 || ktsb_phys == 0) && ktsb4m_szcode >
7923764Sdp78419 	    TSB_64K_SZCODE) {
7933764Sdp78419 		ktsb4m_szcode = TSB_64K_SZCODE;
7943764Sdp78419 		max_bootlp_tteszc = TTE8K;
7953764Sdp78419 	}
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 	ktsb_sz = TSB_BYTES(ktsb_szcode);	/* kernel 8K tsb size */
7980Sstevel@tonic-gate 	ktsb4m_sz = TSB_BYTES(ktsb4m_szcode);	/* kernel 4M tsb size */
7990Sstevel@tonic-gate }
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate /*
8020Sstevel@tonic-gate  * Allocate kernel TSBs from nucleus data memory.
8030Sstevel@tonic-gate  * The function return 0 on success and -1 on failure.
8040Sstevel@tonic-gate  */
8050Sstevel@tonic-gate int
ndata_alloc_tsbs(struct memlist * ndata,pgcnt_t npages)8060Sstevel@tonic-gate ndata_alloc_tsbs(struct memlist *ndata, pgcnt_t npages)
8070Sstevel@tonic-gate {
8080Sstevel@tonic-gate 	/*
8093764Sdp78419 	 * Set ktsb_phys to 1 if the processor supports ASI_QUAD_LDD_PHYS.
8103764Sdp78419 	 */
81111311SSurya.Prakki@Sun.COM 	(void) sfmmu_setup_4lp();
8123764Sdp78419 
8133764Sdp78419 	/*
8140Sstevel@tonic-gate 	 * Size the kernel TSBs based upon the amount of physical
8150Sstevel@tonic-gate 	 * memory in the system.
8160Sstevel@tonic-gate 	 */
8170Sstevel@tonic-gate 	calc_tsb_sizes(npages);
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 	/*
8200Sstevel@tonic-gate 	 * Allocate the 8K kernel TSB if it belongs inside the nucleus.
8210Sstevel@tonic-gate 	 */
8220Sstevel@tonic-gate 	if (enable_bigktsb == 0) {
8230Sstevel@tonic-gate 		if ((ktsb_base = ndata_alloc(ndata, ktsb_sz, ktsb_sz)) == NULL)
8240Sstevel@tonic-gate 			return (-1);
8250Sstevel@tonic-gate 		ASSERT(!((uintptr_t)ktsb_base & (ktsb_sz - 1)));
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 		PRM_DEBUG(ktsb_base);
8280Sstevel@tonic-gate 		PRM_DEBUG(ktsb_sz);
8290Sstevel@tonic-gate 		PRM_DEBUG(ktsb_szcode);
8300Sstevel@tonic-gate 	}
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 	/*
8330Sstevel@tonic-gate 	 * Next, allocate 4M kernel TSB from the nucleus since it's small.
8340Sstevel@tonic-gate 	 */
8353764Sdp78419 	if (ktsb4m_szcode <= TSB_64K_SZCODE) {
8360Sstevel@tonic-gate 
8373764Sdp78419 		ktsb4m_base = ndata_alloc(ndata, ktsb4m_sz, ktsb4m_sz);
8383764Sdp78419 		if (ktsb4m_base == NULL)
8393764Sdp78419 			return (-1);
8403764Sdp78419 		ASSERT(!((uintptr_t)ktsb4m_base & (ktsb4m_sz - 1)));
8413764Sdp78419 
8423764Sdp78419 		PRM_DEBUG(ktsb4m_base);
8433764Sdp78419 		PRM_DEBUG(ktsb4m_sz);
8443764Sdp78419 		PRM_DEBUG(ktsb4m_szcode);
8453764Sdp78419 	}
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate 	return (0);
8480Sstevel@tonic-gate }
8490Sstevel@tonic-gate 
8505648Ssetje size_t
calc_hmehash_sz(pgcnt_t npages)8515648Ssetje calc_hmehash_sz(pgcnt_t npages)
8520Sstevel@tonic-gate {
8530Sstevel@tonic-gate 	ulong_t hme_buckets;
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 	/*
8560Sstevel@tonic-gate 	 * The number of buckets in the hme hash tables
8570Sstevel@tonic-gate 	 * is a power of 2 such that the average hash chain length is
8580Sstevel@tonic-gate 	 * HMENT_HASHAVELEN.  The number of buckets for the user hash is
8590Sstevel@tonic-gate 	 * a function of physical memory and a predefined overmapping factor.
8600Sstevel@tonic-gate 	 * The number of buckets for the kernel hash is a function of
8610Sstevel@tonic-gate 	 * physical memory only.
8620Sstevel@tonic-gate 	 */
8630Sstevel@tonic-gate 	hme_buckets = (npages * HMEHASH_FACTOR) /
8645075Spaulsan 	    (HMENT_HASHAVELEN * (HMEBLK_SPAN(TTE8K) >> MMU_PAGESHIFT));
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 	uhmehash_num = (int)MIN(hme_buckets, MAX_UHME_BUCKETS);
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate 	if (uhmehash_num > USER_BUCKETS_THRESHOLD) {
8690Sstevel@tonic-gate 		/*
8700Sstevel@tonic-gate 		 * if uhmehash_num is not power of 2 round it down to the
8710Sstevel@tonic-gate 		 *  next power of 2.
8720Sstevel@tonic-gate 		 */
8730Sstevel@tonic-gate 		uint_t align = 1 << (highbit(uhmehash_num - 1) - 1);
8740Sstevel@tonic-gate 		uhmehash_num = P2ALIGN(uhmehash_num, align);
8750Sstevel@tonic-gate 	} else
8760Sstevel@tonic-gate 		uhmehash_num = 1 << highbit(uhmehash_num - 1);
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 	hme_buckets = npages / (HMEBLK_SPAN(TTE8K) >> MMU_PAGESHIFT);
8790Sstevel@tonic-gate 	khmehash_num = (int)MIN(hme_buckets, MAX_KHME_BUCKETS);
8800Sstevel@tonic-gate 	khmehash_num = 1 << highbit(khmehash_num - 1);
8810Sstevel@tonic-gate 	khmehash_num = MAX(khmehash_num, MIN_KHME_BUCKETS);
8820Sstevel@tonic-gate 
8835648Ssetje 	return ((uhmehash_num + khmehash_num) * sizeof (struct hmehash_bucket));
8845648Ssetje }
8855648Ssetje 
8865648Ssetje caddr_t
alloc_hmehash(caddr_t alloc_base)8875648Ssetje alloc_hmehash(caddr_t alloc_base)
8885648Ssetje {
8895648Ssetje 	size_t khmehash_sz, uhmehash_sz;
8900Sstevel@tonic-gate 
8915648Ssetje 	khme_hash = (struct hmehash_bucket *)alloc_base;
8925648Ssetje 	khmehash_sz = khmehash_num * sizeof (struct hmehash_bucket);
8935648Ssetje 	alloc_base += khmehash_sz;
8940Sstevel@tonic-gate 
8955648Ssetje 	uhme_hash = (struct hmehash_bucket *)alloc_base;
8965648Ssetje 	uhmehash_sz = uhmehash_num * sizeof (struct hmehash_bucket);
8975648Ssetje 	alloc_base += uhmehash_sz;
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate 	PRM_DEBUG(khme_hash);
9000Sstevel@tonic-gate 	PRM_DEBUG(uhme_hash);
9015648Ssetje 
9025648Ssetje 	return (alloc_base);
9035648Ssetje }
9045648Ssetje 
9055648Ssetje /*
9065648Ssetje  * Allocate hat structs from the nucleus data memory.
9075648Ssetje  */
9085648Ssetje int
ndata_alloc_hat(struct memlist * ndata)909*12908SPavel.Tatashin@Sun.COM ndata_alloc_hat(struct memlist *ndata)
9105648Ssetje {
9115648Ssetje 	size_t	cb_alloc_sz;
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate 	cb_alloc_sz = sfmmu_max_cb_id * sizeof (struct sfmmu_callback);
9140Sstevel@tonic-gate 	PRM_DEBUG(cb_alloc_sz);
9150Sstevel@tonic-gate 	sfmmu_cb_table = ndata_alloc(ndata, cb_alloc_sz, ecache_alignsize);
9165648Ssetje 	if (sfmmu_cb_table == NULL)
9175648Ssetje 		return (-1);
9180Sstevel@tonic-gate 	PRM_DEBUG(sfmmu_cb_table);
9190Sstevel@tonic-gate 
9205648Ssetje 	return (0);
9215648Ssetje }
9225648Ssetje 
9235648Ssetje int
ndata_alloc_kpm(struct memlist * ndata,pgcnt_t kpm_npages)9245648Ssetje ndata_alloc_kpm(struct memlist *ndata, pgcnt_t kpm_npages)
9255648Ssetje {
9265648Ssetje 	size_t	kpmp_alloc_sz;
9275648Ssetje 
9280Sstevel@tonic-gate 	/*
9290Sstevel@tonic-gate 	 * For the kpm_page mutex array we allocate one mutex every 16
9300Sstevel@tonic-gate 	 * kpm pages (64MB). In smallpage mode we allocate one mutex
9310Sstevel@tonic-gate 	 * every 8K pages. The minimum is set to 64 entries and the
9320Sstevel@tonic-gate 	 * maximum to 8K entries.
9330Sstevel@tonic-gate 	 */
9345648Ssetje 	if (kpm_smallpages == 0) {
9355648Ssetje 		kpmp_shift = highbit(sizeof (kpm_page_t)) - 1;
9365648Ssetje 		kpmp_table_sz = 1 << highbit(kpm_npages / 16);
9375648Ssetje 		kpmp_table_sz = (kpmp_table_sz < 64) ? 64 :
9385648Ssetje 		    ((kpmp_table_sz > 8192) ? 8192 : kpmp_table_sz);
9395648Ssetje 		kpmp_alloc_sz = kpmp_table_sz * sizeof (kpm_hlk_t);
9400Sstevel@tonic-gate 
9415648Ssetje 		kpmp_table = ndata_alloc(ndata, kpmp_alloc_sz,
9425648Ssetje 		    ecache_alignsize);
9435648Ssetje 		if (kpmp_table == NULL)
9445648Ssetje 			return (-1);
9450Sstevel@tonic-gate 
9465648Ssetje 		PRM_DEBUG(kpmp_table);
9475648Ssetje 		PRM_DEBUG(kpmp_table_sz);
9480Sstevel@tonic-gate 
9495648Ssetje 		kpmp_stable_sz = 0;
9505648Ssetje 		kpmp_stable = NULL;
9515648Ssetje 	} else {
9525648Ssetje 		ASSERT(kpm_pgsz == PAGESIZE);
9535648Ssetje 		kpmp_shift = highbit(sizeof (kpm_shlk_t)) + 1;
9545648Ssetje 		kpmp_stable_sz = 1 << highbit(kpm_npages / 8192);
9555648Ssetje 		kpmp_stable_sz = (kpmp_stable_sz < 64) ? 64 :
9565648Ssetje 		    ((kpmp_stable_sz > 8192) ? 8192 : kpmp_stable_sz);
9575648Ssetje 		kpmp_alloc_sz = kpmp_stable_sz * sizeof (kpm_shlk_t);
9580Sstevel@tonic-gate 
9595648Ssetje 		kpmp_stable = ndata_alloc(ndata, kpmp_alloc_sz,
9605648Ssetje 		    ecache_alignsize);
9615648Ssetje 		if (kpmp_stable == NULL)
9625648Ssetje 			return (-1);
9630Sstevel@tonic-gate 
9645648Ssetje 		PRM_DEBUG(kpmp_stable);
9655648Ssetje 		PRM_DEBUG(kpmp_stable_sz);
9660Sstevel@tonic-gate 
9675648Ssetje 		kpmp_table_sz = 0;
9685648Ssetje 		kpmp_table = NULL;
9690Sstevel@tonic-gate 	}
9705648Ssetje 	PRM_DEBUG(kpmp_shift);
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 	return (0);
9730Sstevel@tonic-gate }
9740Sstevel@tonic-gate 
9753764Sdp78419 /*
9763764Sdp78419  * This function bop allocs kernel TSBs.
9770Sstevel@tonic-gate  */
9780Sstevel@tonic-gate caddr_t
sfmmu_ktsb_alloc(caddr_t tsbbase)9790Sstevel@tonic-gate sfmmu_ktsb_alloc(caddr_t tsbbase)
9800Sstevel@tonic-gate {
9810Sstevel@tonic-gate 	caddr_t vaddr;
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 	if (enable_bigktsb) {
9840Sstevel@tonic-gate 		ktsb_base = (caddr_t)roundup((uintptr_t)tsbbase, ktsb_sz);
9855648Ssetje 		vaddr = prom_alloc(ktsb_base, ktsb_sz, ktsb_sz);
9860Sstevel@tonic-gate 		if (vaddr != ktsb_base)
9870Sstevel@tonic-gate 			cmn_err(CE_PANIC, "sfmmu_ktsb_alloc: can't alloc"
9883764Sdp78419 			    " 8K bigktsb");
9890Sstevel@tonic-gate 		ktsb_base = vaddr;
9900Sstevel@tonic-gate 		tsbbase = ktsb_base + ktsb_sz;
9910Sstevel@tonic-gate 		PRM_DEBUG(ktsb_base);
9920Sstevel@tonic-gate 		PRM_DEBUG(tsbbase);
9930Sstevel@tonic-gate 	}
9943764Sdp78419 
9953764Sdp78419 	if (ktsb4m_szcode > TSB_64K_SZCODE) {
9963764Sdp78419 		ASSERT(ktsb_phys && enable_bigktsb);
9973764Sdp78419 		ktsb4m_base = (caddr_t)roundup((uintptr_t)tsbbase, ktsb4m_sz);
9983764Sdp78419 		vaddr = (caddr_t)BOP_ALLOC(bootops, ktsb4m_base, ktsb4m_sz,
9993764Sdp78419 		    ktsb4m_sz);
10003764Sdp78419 		if (vaddr != ktsb4m_base)
10013764Sdp78419 			cmn_err(CE_PANIC, "sfmmu_ktsb_alloc: can't alloc"
10023764Sdp78419 			    " 4M bigktsb");
10033764Sdp78419 		ktsb4m_base = vaddr;
10043764Sdp78419 		tsbbase = ktsb4m_base + ktsb4m_sz;
10053764Sdp78419 		PRM_DEBUG(ktsb4m_base);
10063764Sdp78419 		PRM_DEBUG(tsbbase);
10073764Sdp78419 	}
10080Sstevel@tonic-gate 	return (tsbbase);
10090Sstevel@tonic-gate }
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate /*
10120Sstevel@tonic-gate  * Moves code assembled outside of the trap table into the trap
10130Sstevel@tonic-gate  * table taking care to relocate relative branches to code outside
10140Sstevel@tonic-gate  * of the trap handler.
10150Sstevel@tonic-gate  */
10160Sstevel@tonic-gate static void
sfmmu_reloc_trap_handler(void * tablep,void * start,size_t count)10170Sstevel@tonic-gate sfmmu_reloc_trap_handler(void *tablep, void *start, size_t count)
10180Sstevel@tonic-gate {
10190Sstevel@tonic-gate 	size_t i;
10200Sstevel@tonic-gate 	uint32_t *src;
10210Sstevel@tonic-gate 	uint32_t *dst;
10220Sstevel@tonic-gate 	uint32_t inst;
10230Sstevel@tonic-gate 	int op, op2;
10240Sstevel@tonic-gate 	int32_t offset;
10250Sstevel@tonic-gate 	int disp;
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate 	src = start;
10280Sstevel@tonic-gate 	dst = tablep;
10290Sstevel@tonic-gate 	offset = src - dst;
10300Sstevel@tonic-gate 	for (src = start, i = 0; i < count; i++, src++, dst++) {
10310Sstevel@tonic-gate 		inst = *dst = *src;
10320Sstevel@tonic-gate 		op = (inst >> 30) & 0x2;
10330Sstevel@tonic-gate 		if (op == 1) {
10340Sstevel@tonic-gate 			/* call */
10350Sstevel@tonic-gate 			disp = ((int32_t)inst << 2) >> 2; /* sign-extend */
10360Sstevel@tonic-gate 			if (disp + i >= 0 && disp + i < count)
10370Sstevel@tonic-gate 				continue;
10380Sstevel@tonic-gate 			disp += offset;
10390Sstevel@tonic-gate 			inst = 0x40000000u | (disp & 0x3fffffffu);
10400Sstevel@tonic-gate 			*dst = inst;
10410Sstevel@tonic-gate 		} else if (op == 0) {
10420Sstevel@tonic-gate 			/* branch or sethi */
10430Sstevel@tonic-gate 			op2 = (inst >> 22) & 0x7;
10440Sstevel@tonic-gate 
10450Sstevel@tonic-gate 			switch (op2) {
10460Sstevel@tonic-gate 			case 0x3: /* BPr */
10470Sstevel@tonic-gate 				disp = (((inst >> 20) & 0x3) << 14) |
10480Sstevel@tonic-gate 				    (inst & 0x3fff);
10490Sstevel@tonic-gate 				disp = (disp << 16) >> 16; /* sign-extend */
10500Sstevel@tonic-gate 				if (disp + i >= 0 && disp + i < count)
10510Sstevel@tonic-gate 					continue;
10520Sstevel@tonic-gate 				disp += offset;
10530Sstevel@tonic-gate 				if (((disp << 16) >> 16) != disp)
10540Sstevel@tonic-gate 					cmn_err(CE_PANIC, "bad reloc");
10550Sstevel@tonic-gate 				inst &= ~0x303fff;
10560Sstevel@tonic-gate 				inst |= (disp & 0x3fff);
10570Sstevel@tonic-gate 				inst |= (disp & 0xc000) << 6;
10580Sstevel@tonic-gate 				break;
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 			case 0x2: /* Bicc */
10610Sstevel@tonic-gate 				disp = ((int32_t)inst << 10) >> 10;
10620Sstevel@tonic-gate 				if (disp + i >= 0 && disp + i < count)
10630Sstevel@tonic-gate 					continue;
10640Sstevel@tonic-gate 				disp += offset;
10650Sstevel@tonic-gate 				if (((disp << 10) >> 10) != disp)
10660Sstevel@tonic-gate 					cmn_err(CE_PANIC, "bad reloc");
10670Sstevel@tonic-gate 				inst &= ~0x3fffff;
10680Sstevel@tonic-gate 				inst |= (disp & 0x3fffff);
10690Sstevel@tonic-gate 				break;
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate 			case 0x1: /* Bpcc */
10720Sstevel@tonic-gate 				disp = ((int32_t)inst << 13) >> 13;
10730Sstevel@tonic-gate 				if (disp + i >= 0 && disp + i < count)
10740Sstevel@tonic-gate 					continue;
10750Sstevel@tonic-gate 				disp += offset;
10760Sstevel@tonic-gate 				if (((disp << 13) >> 13) != disp)
10770Sstevel@tonic-gate 					cmn_err(CE_PANIC, "bad reloc");
10780Sstevel@tonic-gate 				inst &= ~0x7ffff;
10790Sstevel@tonic-gate 				inst |= (disp & 0x7ffffu);
10800Sstevel@tonic-gate 				break;
10810Sstevel@tonic-gate 			}
10820Sstevel@tonic-gate 			*dst = inst;
10830Sstevel@tonic-gate 		}
10840Sstevel@tonic-gate 	}
10850Sstevel@tonic-gate 	flush_instr_mem(tablep, count * sizeof (uint32_t));
10860Sstevel@tonic-gate }
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate /*
10890Sstevel@tonic-gate  * Routine to allocate a large page to use in the TSB caches.
10900Sstevel@tonic-gate  */
10910Sstevel@tonic-gate /*ARGSUSED*/
10920Sstevel@tonic-gate static page_t *
sfmmu_tsb_page_create(void * addr,size_t size,int vmflag,void * arg)10930Sstevel@tonic-gate sfmmu_tsb_page_create(void *addr, size_t size, int vmflag, void *arg)
10940Sstevel@tonic-gate {
10950Sstevel@tonic-gate 	int pgflags;
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate 	pgflags = PG_EXCL;
10980Sstevel@tonic-gate 	if ((vmflag & VM_NOSLEEP) == 0)
10990Sstevel@tonic-gate 		pgflags |= PG_WAIT;
11000Sstevel@tonic-gate 	if (vmflag & VM_PANIC)
11010Sstevel@tonic-gate 		pgflags |= PG_PANIC;
11020Sstevel@tonic-gate 	if (vmflag & VM_PUSHPAGE)
11030Sstevel@tonic-gate 		pgflags |= PG_PUSHPAGE;
11040Sstevel@tonic-gate 
11050Sstevel@tonic-gate 	return (page_create_va_large(&kvp, (u_offset_t)(uintptr_t)addr, size,
11060Sstevel@tonic-gate 	    pgflags, &kvseg, addr, arg));
11070Sstevel@tonic-gate }
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate /*
11100Sstevel@tonic-gate  * Allocate a large page to back the virtual address range
11110Sstevel@tonic-gate  * [addr, addr + size).  If addr is NULL, allocate the virtual address
11120Sstevel@tonic-gate  * space as well.
11130Sstevel@tonic-gate  */
11140Sstevel@tonic-gate static void *
sfmmu_tsb_xalloc(vmem_t * vmp,void * inaddr,size_t size,int vmflag,uint_t attr,page_t * (* page_create_func)(void *,size_t,int,void *),void * pcarg)11150Sstevel@tonic-gate sfmmu_tsb_xalloc(vmem_t *vmp, void *inaddr, size_t size, int vmflag,
11160Sstevel@tonic-gate     uint_t attr, page_t *(*page_create_func)(void *, size_t, int, void *),
11170Sstevel@tonic-gate     void *pcarg)
11180Sstevel@tonic-gate {
11190Sstevel@tonic-gate 	page_t *ppl;
11200Sstevel@tonic-gate 	page_t *rootpp;
11210Sstevel@tonic-gate 	caddr_t addr = inaddr;
11220Sstevel@tonic-gate 	pgcnt_t npages = btopr(size);
11230Sstevel@tonic-gate 	page_t **ppa;
11240Sstevel@tonic-gate 	int i = 0;
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate 	/*
11270Sstevel@tonic-gate 	 * Assuming that only TSBs will call this with size > PAGESIZE
11280Sstevel@tonic-gate 	 * There is no reason why this couldn't be expanded to 8k pages as
11290Sstevel@tonic-gate 	 * well, or other page sizes in the future .... but for now, we
11300Sstevel@tonic-gate 	 * only support fixed sized page requests.
11310Sstevel@tonic-gate 	 */
11320Sstevel@tonic-gate 	if ((inaddr == NULL) && ((addr = vmem_xalloc(vmp, size, size, 0, 0,
11330Sstevel@tonic-gate 	    NULL, NULL, vmflag)) == NULL))
11340Sstevel@tonic-gate 		return (NULL);
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate 	if (page_resv(npages, vmflag & VM_KMFLAGS) == 0) {
11370Sstevel@tonic-gate 		if (inaddr == NULL)
11380Sstevel@tonic-gate 			vmem_xfree(vmp, addr, size);
11390Sstevel@tonic-gate 		return (NULL);
11400Sstevel@tonic-gate 	}
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate 	ppl = page_create_func(addr, size, vmflag, pcarg);
11430Sstevel@tonic-gate 	if (ppl == NULL) {
11440Sstevel@tonic-gate 		if (inaddr == NULL)
11450Sstevel@tonic-gate 			vmem_xfree(vmp, addr, size);
11460Sstevel@tonic-gate 		page_unresv(npages);
11470Sstevel@tonic-gate 		return (NULL);
11480Sstevel@tonic-gate 	}
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 	rootpp = ppl;
11510Sstevel@tonic-gate 	ppa = kmem_zalloc(npages * sizeof (page_t *), KM_SLEEP);
11520Sstevel@tonic-gate 	while (ppl != NULL) {
11530Sstevel@tonic-gate 		page_t *pp = ppl;
11540Sstevel@tonic-gate 		ppa[i++] = pp;
11550Sstevel@tonic-gate 		page_sub(&ppl, pp);
11560Sstevel@tonic-gate 		ASSERT(page_iolock_assert(pp));
11570Sstevel@tonic-gate 		page_io_unlock(pp);
11580Sstevel@tonic-gate 	}
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate 	/*
11610Sstevel@tonic-gate 	 * Load the locked entry.  It's OK to preload the entry into
11620Sstevel@tonic-gate 	 * the TSB since we now support large mappings in the kernel TSB.
11630Sstevel@tonic-gate 	 */
11640Sstevel@tonic-gate 	hat_memload_array(kas.a_hat, (caddr_t)rootpp->p_offset, size,
11650Sstevel@tonic-gate 	    ppa, (PROT_ALL & ~PROT_USER) | HAT_NOSYNC | attr, HAT_LOAD_LOCK);
11660Sstevel@tonic-gate 
11670Sstevel@tonic-gate 	for (--i; i >= 0; --i) {
11680Sstevel@tonic-gate 		(void) page_pp_lock(ppa[i], 0, 1);
11690Sstevel@tonic-gate 		page_unlock(ppa[i]);
11700Sstevel@tonic-gate 	}
11710Sstevel@tonic-gate 
11720Sstevel@tonic-gate 	kmem_free(ppa, npages * sizeof (page_t *));
11730Sstevel@tonic-gate 	return (addr);
11740Sstevel@tonic-gate }
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate /* Called to import new spans into the TSB vmem arenas */
11770Sstevel@tonic-gate void *
sfmmu_tsb_segkmem_alloc(vmem_t * vmp,size_t size,int vmflag)11780Sstevel@tonic-gate sfmmu_tsb_segkmem_alloc(vmem_t *vmp, size_t size, int vmflag)
11790Sstevel@tonic-gate {
11800Sstevel@tonic-gate 	lgrp_id_t lgrpid = LGRP_NONE;
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 	if (tsb_lgrp_affinity) {
11830Sstevel@tonic-gate 		/*
11840Sstevel@tonic-gate 		 * Search for the vmp->lgrpid mapping by brute force;
11850Sstevel@tonic-gate 		 * some day vmp will have an lgrp, until then we have
11860Sstevel@tonic-gate 		 * to do this the hard way.
11870Sstevel@tonic-gate 		 */
11880Sstevel@tonic-gate 		for (lgrpid = 0; lgrpid < NLGRPS_MAX &&
11895648Ssetje 		    vmp != kmem_tsb_default_arena[lgrpid]; lgrpid++)
11905648Ssetje 			;
11910Sstevel@tonic-gate 		if (lgrpid == NLGRPS_MAX)
11920Sstevel@tonic-gate 			lgrpid = LGRP_NONE;
11930Sstevel@tonic-gate 	}
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 	return (sfmmu_tsb_xalloc(vmp, NULL, size, vmflag, 0,
11960Sstevel@tonic-gate 	    sfmmu_tsb_page_create, lgrpid != LGRP_NONE? &lgrpid : NULL));
11970Sstevel@tonic-gate }
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate /* Called to free spans from the TSB vmem arenas */
12000Sstevel@tonic-gate void
sfmmu_tsb_segkmem_free(vmem_t * vmp,void * inaddr,size_t size)12010Sstevel@tonic-gate sfmmu_tsb_segkmem_free(vmem_t *vmp, void *inaddr, size_t size)
12020Sstevel@tonic-gate {
12030Sstevel@tonic-gate 	page_t *pp;
12040Sstevel@tonic-gate 	caddr_t addr = inaddr;
12050Sstevel@tonic-gate 	caddr_t eaddr;
12060Sstevel@tonic-gate 	pgcnt_t npages = btopr(size);
12070Sstevel@tonic-gate 	pgcnt_t pgs_left = npages;
12080Sstevel@tonic-gate 	page_t *rootpp = NULL;
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate 	hat_unload(kas.a_hat, addr, size, HAT_UNLOAD_UNLOCK);
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate 	for (eaddr = addr + size; addr < eaddr; addr += PAGESIZE) {
12130Sstevel@tonic-gate 		pp = page_lookup(&kvp, (u_offset_t)(uintptr_t)addr, SE_EXCL);
12140Sstevel@tonic-gate 		if (pp == NULL)
12150Sstevel@tonic-gate 			panic("sfmmu_tsb_segkmem_free: page not found");
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate 		ASSERT(PAGE_EXCL(pp));
12180Sstevel@tonic-gate 		page_pp_unlock(pp, 0, 1);
12190Sstevel@tonic-gate 
12200Sstevel@tonic-gate 		if (rootpp == NULL)
12210Sstevel@tonic-gate 			rootpp = pp;
12220Sstevel@tonic-gate 		if (--pgs_left == 0) {
12230Sstevel@tonic-gate 			/*
12240Sstevel@tonic-gate 			 * similar logic to segspt_free_pages, but we know we
12250Sstevel@tonic-gate 			 * have one large page.
12260Sstevel@tonic-gate 			 */
12270Sstevel@tonic-gate 			page_destroy_pages(rootpp);
12280Sstevel@tonic-gate 		}
12290Sstevel@tonic-gate 	}
12300Sstevel@tonic-gate 	page_unresv(npages);
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate 	if (vmp != NULL)
12330Sstevel@tonic-gate 		vmem_xfree(vmp, inaddr, size);
12340Sstevel@tonic-gate }
1235