xref: /titanic_51/usr/src/uts/sun4/vm/sfmmu.c (revision b52a336e0ccd9ef29cd11bb3c7e530788483ee04)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51e2e7a75Shuah  * Common Development and Distribution License (the "License").
61e2e7a75Shuah  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2107d06da5SSurya Prakki 
227c478bd9Sstevel@tonic-gate /*
23*b52a336eSPavel Tatashin  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <vm/hat.h>
287c478bd9Sstevel@tonic-gate #include <vm/hat_sfmmu.h>
297c478bd9Sstevel@tonic-gate #include <vm/page.h>
307c478bd9Sstevel@tonic-gate #include <sys/pte.h>
317c478bd9Sstevel@tonic-gate #include <sys/systm.h>
327c478bd9Sstevel@tonic-gate #include <sys/mman.h>
337c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
347c478bd9Sstevel@tonic-gate #include <sys/machparam.h>
357c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
367c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
377c478bd9Sstevel@tonic-gate #include <sys/mmu.h>
387c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
397c478bd9Sstevel@tonic-gate #include <sys/cpu.h>
407c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
417c478bd9Sstevel@tonic-gate #include <sys/debug.h>
427c478bd9Sstevel@tonic-gate #include <sys/lgrp.h>
437c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
447c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
457c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
467c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
477c478bd9Sstevel@tonic-gate #include <vm/as.h>
487c478bd9Sstevel@tonic-gate #include <vm/seg.h>
497c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
507c478bd9Sstevel@tonic-gate #include <vm/seg_kp.h>
517c478bd9Sstevel@tonic-gate #include <vm/seg_kpm.h>
527c478bd9Sstevel@tonic-gate #include <vm/rm.h>
537c478bd9Sstevel@tonic-gate #include <vm/vm_dep.h>
547c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
557c478bd9Sstevel@tonic-gate #include <sys/vm_machparam.h>
567c478bd9Sstevel@tonic-gate #include <sys/promif.h>
577c478bd9Sstevel@tonic-gate #include <sys/prom_isa.h>
587c478bd9Sstevel@tonic-gate #include <sys/prom_plat.h>
597c478bd9Sstevel@tonic-gate #include <sys/prom_debug.h>
607c478bd9Sstevel@tonic-gate #include <sys/privregs.h>
617c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
627c478bd9Sstevel@tonic-gate #include <sys/memlist.h>
637c478bd9Sstevel@tonic-gate #include <sys/memlist_plat.h>
647c478bd9Sstevel@tonic-gate #include <sys/cpu_module.h>
657c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
667c478bd9Sstevel@tonic-gate #include <sys/kdi.h>
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /*
697c478bd9Sstevel@tonic-gate  * Static routines
707c478bd9Sstevel@tonic-gate  */
717c478bd9Sstevel@tonic-gate static void	sfmmu_map_prom_mappings(struct translation *, size_t);
727c478bd9Sstevel@tonic-gate static struct translation *read_prom_mappings(size_t *);
737c478bd9Sstevel@tonic-gate static void	sfmmu_reloc_trap_handler(void *, void *, size_t);
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * External routines
777c478bd9Sstevel@tonic-gate  */
787c478bd9Sstevel@tonic-gate extern void sfmmu_remap_kernel(void);
797c478bd9Sstevel@tonic-gate extern void sfmmu_patch_utsb(void);
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /*
827c478bd9Sstevel@tonic-gate  * Global Data:
837c478bd9Sstevel@tonic-gate  */
847c478bd9Sstevel@tonic-gate extern caddr_t	textva, datava;
857c478bd9Sstevel@tonic-gate extern tte_t	ktext_tte, kdata_tte;	/* ttes for kernel text and data */
867c478bd9Sstevel@tonic-gate extern int	enable_bigktsb;
87ca622e3aSsvemuri extern int	kmem64_smchunks;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate uint64_t memsegspa = (uintptr_t)MSEG_NULLPTR_PA; /* memsegs physical linkage */
907c478bd9Sstevel@tonic-gate uint64_t memseg_phash[N_MEM_SLOTS];	/* use physical memseg addresses */
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate int	sfmmu_kern_mapped = 0;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  * DMMU primary context register for the kernel context. Machine specific code
967c478bd9Sstevel@tonic-gate  * inserts correct page size codes when necessary
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate uint64_t kcontextreg = KCONTEXT;
997c478bd9Sstevel@tonic-gate 
1001e45ea5aSblakej #ifdef DEBUG
1011e45ea5aSblakej static int ndata_middle_hole_detected = 0;
1021e45ea5aSblakej #endif
1031e45ea5aSblakej 
1047c478bd9Sstevel@tonic-gate /* Extern Global Data */
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate extern int page_relocate_ready;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate /*
1097c478bd9Sstevel@tonic-gate  * Controls the logic which enables the use of the
1107c478bd9Sstevel@tonic-gate  * QUAD_LDD_PHYS ASI for TSB accesses.
1117c478bd9Sstevel@tonic-gate  */
1127c478bd9Sstevel@tonic-gate extern int	ktsb_phys;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate  * Global Routines called from within:
1167c478bd9Sstevel@tonic-gate  *	usr/src/uts/sun4u
1177c478bd9Sstevel@tonic-gate  *	usr/src/uts/sfmmu
1187c478bd9Sstevel@tonic-gate  *	usr/src/uts/sun
1197c478bd9Sstevel@tonic-gate  */
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate pfn_t
1227c478bd9Sstevel@tonic-gate va_to_pfn(void *vaddr)
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate 	u_longlong_t physaddr;
1257c478bd9Sstevel@tonic-gate 	int mode, valid;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	if (tba_taken_over)
1287c478bd9Sstevel@tonic-gate 		return (hat_getpfnum(kas.a_hat, (caddr_t)vaddr));
1297c478bd9Sstevel@tonic-gate 
130bb121940Sdp78419 #if !defined(C_OBP)
131ca622e3aSsvemuri 	if (!kmem64_smchunks &&
132ca622e3aSsvemuri 	    (caddr_t)vaddr >= kmem64_base && (caddr_t)vaddr < kmem64_end) {
133bb121940Sdp78419 		if (kmem64_pabase == (uint64_t)-1)
134bb121940Sdp78419 			prom_panic("va_to_pfn: kmem64_pabase not init");
135bb121940Sdp78419 		physaddr = kmem64_pabase + ((caddr_t)vaddr - kmem64_base);
136bb121940Sdp78419 		return ((pfn_t)physaddr >> MMU_PAGESHIFT);
137bb121940Sdp78419 	}
138bb121940Sdp78419 #endif	/* !C_OBP */
139bb121940Sdp78419 
1407c478bd9Sstevel@tonic-gate 	if ((prom_translate_virt(vaddr, &valid, &physaddr, &mode) != -1) &&
1417c478bd9Sstevel@tonic-gate 	    (valid == -1)) {
1427c478bd9Sstevel@tonic-gate 		return ((pfn_t)(physaddr >> MMU_PAGESHIFT));
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate 	return (PFN_INVALID);
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate uint64_t
1487c478bd9Sstevel@tonic-gate va_to_pa(void *vaddr)
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate 	pfn_t pfn;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	if ((pfn = va_to_pfn(vaddr)) == PFN_INVALID)
1537c478bd9Sstevel@tonic-gate 		return ((uint64_t)-1);
1547c478bd9Sstevel@tonic-gate 	return (((uint64_t)pfn << MMU_PAGESHIFT) |
1557c478bd9Sstevel@tonic-gate 	    ((uint64_t)vaddr & MMU_PAGEOFFSET));
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate void
1597c478bd9Sstevel@tonic-gate hat_kern_setup(void)
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate 	struct translation *trans_root;
1627c478bd9Sstevel@tonic-gate 	size_t ntrans_root;
1637c478bd9Sstevel@tonic-gate 	extern void startup_fixup_physavail(void);
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	/*
1667c478bd9Sstevel@tonic-gate 	 * These are the steps we take to take over the mmu from the prom.
1677c478bd9Sstevel@tonic-gate 	 *
1687c478bd9Sstevel@tonic-gate 	 * (1)	Read the prom's mappings through the translation property.
1697c478bd9Sstevel@tonic-gate 	 * (2)	Remap the kernel text and kernel data with 2 locked 4MB ttes.
1707c478bd9Sstevel@tonic-gate 	 *	Create the the hmeblks for these 2 ttes at this time.
1717c478bd9Sstevel@tonic-gate 	 * (3)	Create hat structures for all other prom mappings.  Since the
1727c478bd9Sstevel@tonic-gate 	 *	kernel text and data hme_blks have already been created we
1737c478bd9Sstevel@tonic-gate 	 *	skip the equivalent prom's mappings.
1747c478bd9Sstevel@tonic-gate 	 * (4)	Initialize the tsb and its corresponding hardware regs.
1757c478bd9Sstevel@tonic-gate 	 * (5)	Take over the trap table (currently in startup).
1767c478bd9Sstevel@tonic-gate 	 * (6)	Up to this point it is possible the prom required some of its
1777c478bd9Sstevel@tonic-gate 	 *	locked tte's.  Now that we own the trap table we remove them.
1787c478bd9Sstevel@tonic-gate 	 */
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	ktsb_pbase = va_to_pa(ktsb_base);
1817c478bd9Sstevel@tonic-gate 	ktsb4m_pbase = va_to_pa(ktsb4m_base);
1827c478bd9Sstevel@tonic-gate 	PRM_DEBUG(ktsb_pbase);
1837c478bd9Sstevel@tonic-gate 	PRM_DEBUG(ktsb4m_pbase);
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	sfmmu_patch_ktsb();
1867c478bd9Sstevel@tonic-gate 	sfmmu_patch_utsb();
1877c478bd9Sstevel@tonic-gate 	sfmmu_patch_mmu_asi(ktsb_phys);
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	sfmmu_init_tsbs();
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	if (kpm_enable) {
1927c478bd9Sstevel@tonic-gate 		sfmmu_kpm_patch_tlbm();
1937c478bd9Sstevel@tonic-gate 		if (kpm_smallpages == 0) {
1947c478bd9Sstevel@tonic-gate 			sfmmu_kpm_patch_tsbm();
1957c478bd9Sstevel@tonic-gate 		}
1967c478bd9Sstevel@tonic-gate 	}
1977c478bd9Sstevel@tonic-gate 
1987dacfc44Spaulsan 	if (!shctx_on) {
19905d3dc4bSpaulsan 		sfmmu_patch_shctx();
20005d3dc4bSpaulsan 	}
20105d3dc4bSpaulsan 
2027c478bd9Sstevel@tonic-gate 	/*
2037c478bd9Sstevel@tonic-gate 	 * The 8K-indexed kernel TSB space is used to hold
2047c478bd9Sstevel@tonic-gate 	 * translations below...
2057c478bd9Sstevel@tonic-gate 	 */
2067c478bd9Sstevel@tonic-gate 	trans_root = read_prom_mappings(&ntrans_root);
2077c478bd9Sstevel@tonic-gate 	sfmmu_remap_kernel();
2087c478bd9Sstevel@tonic-gate 	startup_fixup_physavail();
2097c478bd9Sstevel@tonic-gate 	mmu_init_kernel_pgsz(kas.a_hat);
2107c478bd9Sstevel@tonic-gate 	sfmmu_map_prom_mappings(trans_root, ntrans_root);
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	/*
2137c478bd9Sstevel@tonic-gate 	 * We invalidate 8K kernel TSB because we used it in
2147c478bd9Sstevel@tonic-gate 	 * sfmmu_map_prom_mappings()
2157c478bd9Sstevel@tonic-gate 	 */
2167c478bd9Sstevel@tonic-gate 	sfmmu_inv_tsb(ktsb_base, ktsb_sz);
2177c478bd9Sstevel@tonic-gate 	sfmmu_inv_tsb(ktsb4m_base, ktsb4m_sz);
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	sfmmu_init_ktsbinfo();
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	sfmmu_kern_mapped = 1;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	/*
2257c478bd9Sstevel@tonic-gate 	 * hments have been created for mapped pages, and thus we're ready
2267c478bd9Sstevel@tonic-gate 	 * for kmdb to start using its own trap table.  It walks the hments
2277c478bd9Sstevel@tonic-gate 	 * to resolve TLB misses, and can't be used until they're ready.
2287c478bd9Sstevel@tonic-gate 	 */
2297c478bd9Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
2307c478bd9Sstevel@tonic-gate 		kdi_dvec_vmready();
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate /*
2347c478bd9Sstevel@tonic-gate  * Macro used below to convert the prom's 32-bit high and low fields into
2357c478bd9Sstevel@tonic-gate  * a value appropriate for the 64-bit kernel.
2367c478bd9Sstevel@tonic-gate  */
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate #define	COMBINE(hi, lo) (((uint64_t)(uint32_t)(hi) << 32) | (uint32_t)(lo))
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate /*
241bb121940Sdp78419  * Track larges pages used.
242bb121940Sdp78419  * Provides observability for this feature on non-debug kernels.
243bb121940Sdp78419  */
244bb121940Sdp78419 ulong_t map_prom_lpcount[MMU_PAGE_SIZES];
245bb121940Sdp78419 
246bb121940Sdp78419 /*
2477c478bd9Sstevel@tonic-gate  * This function traverses the prom mapping list and creates equivalent
2487c478bd9Sstevel@tonic-gate  * mappings in the sfmmu mapping hash.
2497c478bd9Sstevel@tonic-gate  */
2507c478bd9Sstevel@tonic-gate static void
2517c478bd9Sstevel@tonic-gate sfmmu_map_prom_mappings(struct translation *trans_root, size_t ntrans_root)
2527c478bd9Sstevel@tonic-gate {
2537c478bd9Sstevel@tonic-gate 	struct translation *promt;
2547c478bd9Sstevel@tonic-gate 	tte_t	tte, oldtte, *ttep;
2557c478bd9Sstevel@tonic-gate 	pfn_t	pfn, oldpfn, basepfn;
2567c478bd9Sstevel@tonic-gate 	caddr_t vaddr;
2577c478bd9Sstevel@tonic-gate 	size_t	size, offset;
2587c478bd9Sstevel@tonic-gate 	unsigned long i;
2597c478bd9Sstevel@tonic-gate 	uint_t	attr;
2607c478bd9Sstevel@tonic-gate 	page_t *pp;
2617c478bd9Sstevel@tonic-gate 	extern struct memlist *virt_avail;
262d9aef8afSjesusm 	char buf[256];
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	ttep = &tte;
2657c478bd9Sstevel@tonic-gate 	for (i = 0, promt = trans_root; i < ntrans_root; i++, promt++) {
2667c478bd9Sstevel@tonic-gate 		ASSERT(promt->tte_hi != 0);
2677c478bd9Sstevel@tonic-gate 		ASSERT32(promt->virt_hi == 0 && promt->size_hi == 0);
2687c478bd9Sstevel@tonic-gate 
269bb121940Sdp78419 		vaddr = (caddr_t)COMBINE(promt->virt_hi, promt->virt_lo);
270bb121940Sdp78419 
2717c478bd9Sstevel@tonic-gate 		/*
2727c478bd9Sstevel@tonic-gate 		 * hack until we get rid of map-for-unix
2737c478bd9Sstevel@tonic-gate 		 */
274bb121940Sdp78419 		if (vaddr < (caddr_t)KERNELBASE)
2757c478bd9Sstevel@tonic-gate 			continue;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 		ttep->tte_inthi = promt->tte_hi;
2787c478bd9Sstevel@tonic-gate 		ttep->tte_intlo = promt->tte_lo;
2797c478bd9Sstevel@tonic-gate 		attr = PROC_DATA | HAT_NOSYNC;
2807c478bd9Sstevel@tonic-gate #if defined(TTE_IS_GLOBAL)
2817c478bd9Sstevel@tonic-gate 		if (TTE_IS_GLOBAL(ttep)) {
2827c478bd9Sstevel@tonic-gate 			/*
2837c478bd9Sstevel@tonic-gate 			 * The prom better not use global translations
2847c478bd9Sstevel@tonic-gate 			 * because a user process might use the same
2857c478bd9Sstevel@tonic-gate 			 * virtual addresses
2867c478bd9Sstevel@tonic-gate 			 */
287d9aef8afSjesusm 			prom_panic("sfmmu_map_prom_mappings: global"
288d9aef8afSjesusm 			    " translation");
2897c478bd9Sstevel@tonic-gate 			TTE_SET_LOFLAGS(ttep, TTE_GLB_INT, 0);
2907c478bd9Sstevel@tonic-gate 		}
2917c478bd9Sstevel@tonic-gate #endif
2927c478bd9Sstevel@tonic-gate 		if (TTE_IS_LOCKED(ttep)) {
2937c478bd9Sstevel@tonic-gate 			/* clear the lock bits */
2947c478bd9Sstevel@tonic-gate 			TTE_CLR_LOCKED(ttep);
2957c478bd9Sstevel@tonic-gate 		}
2967c478bd9Sstevel@tonic-gate 		attr |= (TTE_IS_VCACHEABLE(ttep)) ? 0 : SFMMU_UNCACHEVTTE;
2977c478bd9Sstevel@tonic-gate 		attr |= (TTE_IS_PCACHEABLE(ttep)) ? 0 : SFMMU_UNCACHEPTTE;
2987c478bd9Sstevel@tonic-gate 		attr |= (TTE_IS_SIDEFFECT(ttep)) ? SFMMU_SIDEFFECT : 0;
2997c478bd9Sstevel@tonic-gate 		attr |= (TTE_IS_IE(ttep)) ? HAT_STRUCTURE_LE : 0;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 		size = COMBINE(promt->size_hi, promt->size_lo);
3027c478bd9Sstevel@tonic-gate 		offset = 0;
3037c478bd9Sstevel@tonic-gate 		basepfn = TTE_TO_PFN((caddr_t)COMBINE(promt->virt_hi,
3047c478bd9Sstevel@tonic-gate 		    promt->virt_lo), ttep);
3057c478bd9Sstevel@tonic-gate 		while (size) {
3067c478bd9Sstevel@tonic-gate 			vaddr = (caddr_t)(COMBINE(promt->virt_hi,
3077c478bd9Sstevel@tonic-gate 			    promt->virt_lo) + offset);
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 			/*
3107c478bd9Sstevel@tonic-gate 			 * make sure address is not in virt-avail list
3117c478bd9Sstevel@tonic-gate 			 */
3127c478bd9Sstevel@tonic-gate 			if (address_in_memlist(virt_avail, (uint64_t)vaddr,
3137c478bd9Sstevel@tonic-gate 			    size)) {
314d9aef8afSjesusm 				prom_panic("sfmmu_map_prom_mappings:"
315d9aef8afSjesusm 				    " inconsistent translation/avail lists");
3167c478bd9Sstevel@tonic-gate 			}
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 			pfn = basepfn + mmu_btop(offset);
3197c478bd9Sstevel@tonic-gate 			if (pf_is_memory(pfn)) {
3207c478bd9Sstevel@tonic-gate 				if (attr & SFMMU_UNCACHEPTTE) {
321d9aef8afSjesusm 					prom_panic("sfmmu_map_prom_mappings:"
3227c478bd9Sstevel@tonic-gate 					    " uncached prom memory page");
3237c478bd9Sstevel@tonic-gate 				}
3247c478bd9Sstevel@tonic-gate 			} else {
3257c478bd9Sstevel@tonic-gate 				if (!(attr & SFMMU_SIDEFFECT)) {
326d9aef8afSjesusm 					prom_panic("sfmmu_map_prom_mappings:"
327d9aef8afSjesusm 					    " prom i/o page without"
328d9aef8afSjesusm 					    " side-effect");
3297c478bd9Sstevel@tonic-gate 				}
3307c478bd9Sstevel@tonic-gate 			}
331bb121940Sdp78419 
332bb121940Sdp78419 			/*
333bb121940Sdp78419 			 * skip kmem64 area
334bb121940Sdp78419 			 */
335ca622e3aSsvemuri 			if (!kmem64_smchunks &&
336ca622e3aSsvemuri 			    vaddr >= kmem64_base &&
337bb121940Sdp78419 			    vaddr < kmem64_aligned_end) {
338bb121940Sdp78419 #if !defined(C_OBP)
339d9aef8afSjesusm 				prom_panic("sfmmu_map_prom_mappings:"
340d9aef8afSjesusm 				    " unexpected kmem64 prom mapping");
341bb121940Sdp78419 #else	/* !C_OBP */
342bb121940Sdp78419 				size_t mapsz;
343bb121940Sdp78419 
344bb121940Sdp78419 				if (ptob(pfn) !=
345bb121940Sdp78419 				    kmem64_pabase + (vaddr - kmem64_base)) {
346d9aef8afSjesusm 					prom_panic("sfmmu_map_prom_mappings:"
347d9aef8afSjesusm 					    " unexpected kmem64 prom mapping");
348bb121940Sdp78419 				}
349bb121940Sdp78419 
350bb121940Sdp78419 				mapsz = kmem64_aligned_end - vaddr;
351bb121940Sdp78419 				if (mapsz >= size) {
352bb121940Sdp78419 					break;
353bb121940Sdp78419 				}
354bb121940Sdp78419 				size -= mapsz;
355bb121940Sdp78419 				offset += mapsz;
356bb121940Sdp78419 				continue;
357bb121940Sdp78419 #endif	/* !C_OBP */
358bb121940Sdp78419 			}
359bb121940Sdp78419 
3607c478bd9Sstevel@tonic-gate 			oldpfn = sfmmu_vatopfn(vaddr, KHATID, &oldtte);
3617c478bd9Sstevel@tonic-gate 			ASSERT(oldpfn != PFN_SUSPENDED);
3627c478bd9Sstevel@tonic-gate 			ASSERT(page_relocate_ready == 0);
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 			if (oldpfn != PFN_INVALID) {
3657c478bd9Sstevel@tonic-gate 				/*
3667c478bd9Sstevel@tonic-gate 				 * mapping already exists.
3677c478bd9Sstevel@tonic-gate 				 * Verify they are equal
3687c478bd9Sstevel@tonic-gate 				 */
3697c478bd9Sstevel@tonic-gate 				if (pfn != oldpfn) {
370d9aef8afSjesusm 					(void) snprintf(buf, sizeof (buf),
371d9aef8afSjesusm 					"sfmmu_map_prom_mappings: mapping"
372d9aef8afSjesusm 					" conflict (va = 0x%p, pfn = 0x%p,"
373d9aef8afSjesusm 					" oldpfn = 0x%p)", (void *)vaddr,
374d9aef8afSjesusm 					    (void *)pfn, (void *)oldpfn);
375d9aef8afSjesusm 					prom_panic(buf);
3767c478bd9Sstevel@tonic-gate 				}
3777c478bd9Sstevel@tonic-gate 				size -= MMU_PAGESIZE;
3787c478bd9Sstevel@tonic-gate 				offset += MMU_PAGESIZE;
3797c478bd9Sstevel@tonic-gate 				continue;
3807c478bd9Sstevel@tonic-gate 			}
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 			pp = page_numtopp_nolock(pfn);
3837c478bd9Sstevel@tonic-gate 			if ((pp != NULL) && PP_ISFREE((page_t *)pp)) {
384d9aef8afSjesusm 				(void) snprintf(buf, sizeof (buf),
385d9aef8afSjesusm 				"sfmmu_map_prom_mappings: prom-mapped"
386d9aef8afSjesusm 				" page (va = 0x%p, pfn = 0x%p) on free list",
387d9aef8afSjesusm 				    (void *)vaddr, (void *)pfn);
388d9aef8afSjesusm 				prom_panic(buf);
3897c478bd9Sstevel@tonic-gate 			}
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 			sfmmu_memtte(ttep, pfn, attr, TTE8K);
3927c478bd9Sstevel@tonic-gate 			sfmmu_tteload(kas.a_hat, ttep, vaddr, pp,
3937c478bd9Sstevel@tonic-gate 			    HAT_LOAD_LOCK | SFMMU_NO_TSBLOAD);
3947c478bd9Sstevel@tonic-gate 			size -= MMU_PAGESIZE;
3957c478bd9Sstevel@tonic-gate 			offset += MMU_PAGESIZE;
3967c478bd9Sstevel@tonic-gate 		}
3977c478bd9Sstevel@tonic-gate 	}
398bb121940Sdp78419 
399bb121940Sdp78419 	/*
400bb121940Sdp78419 	 * We claimed kmem64 from prom, so now we need to load tte.
401bb121940Sdp78419 	 */
402ca622e3aSsvemuri 	if (!kmem64_smchunks && kmem64_base != NULL) {
403bb121940Sdp78419 		pgcnt_t pages;
404bb121940Sdp78419 		size_t psize;
405bb121940Sdp78419 		int pszc;
406bb121940Sdp78419 
407bb121940Sdp78419 		pszc = kmem64_szc;
408bb121940Sdp78419 #ifdef sun4u
409bb121940Sdp78419 		if (pszc > TTE8K) {
410bb121940Sdp78419 			pszc = segkmem_lpszc;
411bb121940Sdp78419 		}
412bb121940Sdp78419 #endif	/* sun4u */
413bb121940Sdp78419 		psize = TTEBYTES(pszc);
414bb121940Sdp78419 		pages = btop(psize);
415bb121940Sdp78419 		basepfn = kmem64_pabase >> MMU_PAGESHIFT;
416bb121940Sdp78419 		vaddr = kmem64_base;
417bb121940Sdp78419 		while (vaddr < kmem64_end) {
418bb121940Sdp78419 			sfmmu_memtte(ttep, basepfn,
419bb121940Sdp78419 			    PROC_DATA | HAT_NOSYNC, pszc);
420bb121940Sdp78419 			sfmmu_tteload(kas.a_hat, ttep, vaddr, NULL,
421bb121940Sdp78419 			    HAT_LOAD_LOCK | SFMMU_NO_TSBLOAD);
422bb121940Sdp78419 			vaddr += psize;
423bb121940Sdp78419 			basepfn += pages;
424bb121940Sdp78419 		}
425bb121940Sdp78419 		map_prom_lpcount[pszc] =
426bb121940Sdp78419 		    ((caddr_t)P2ROUNDUP((uintptr_t)kmem64_end, psize) -
427bb121940Sdp78419 		    kmem64_base) >> TTE_PAGE_SHIFT(pszc);
428bb121940Sdp78419 	}
4297c478bd9Sstevel@tonic-gate }
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate #undef COMBINE	/* local to previous routine */
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate /*
4347c478bd9Sstevel@tonic-gate  * This routine reads in the "translations" property in to a buffer and
4357c478bd9Sstevel@tonic-gate  * returns a pointer to this buffer and the number of translations.
4367c478bd9Sstevel@tonic-gate  */
4377c478bd9Sstevel@tonic-gate static struct translation *
4387c478bd9Sstevel@tonic-gate read_prom_mappings(size_t *ntransrootp)
4397c478bd9Sstevel@tonic-gate {
4407c478bd9Sstevel@tonic-gate 	char *prop = "translations";
4417c478bd9Sstevel@tonic-gate 	size_t translen;
442fa9e4066Sahrens 	pnode_t node;
4437c478bd9Sstevel@tonic-gate 	struct translation *transroot;
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	/*
4467c478bd9Sstevel@tonic-gate 	 * the "translations" property is associated with the mmu node
4477c478bd9Sstevel@tonic-gate 	 */
448fa9e4066Sahrens 	node = (pnode_t)prom_getphandle(prom_mmu_ihandle());
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	/*
4517c478bd9Sstevel@tonic-gate 	 * We use the TSB space to read in the prom mappings.  This space
4527c478bd9Sstevel@tonic-gate 	 * is currently not being used because we haven't taken over the
4537c478bd9Sstevel@tonic-gate 	 * trap table yet.  It should be big enough to hold the mappings.
4547c478bd9Sstevel@tonic-gate 	 */
4557c478bd9Sstevel@tonic-gate 	if ((translen = prom_getproplen(node, prop)) == -1)
4567c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "no translations property");
4577c478bd9Sstevel@tonic-gate 	*ntransrootp = translen / sizeof (*transroot);
4587c478bd9Sstevel@tonic-gate 	translen = roundup(translen, MMU_PAGESIZE);
4597c478bd9Sstevel@tonic-gate 	PRM_DEBUG(translen);
4607c478bd9Sstevel@tonic-gate 	if (translen > TSB_BYTES(ktsb_szcode))
4617c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "not enough space for translations");
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	transroot = (struct translation *)ktsb_base;
4647c478bd9Sstevel@tonic-gate 	ASSERT(transroot);
4657c478bd9Sstevel@tonic-gate 	if (prom_getprop(node, prop, (caddr_t)transroot) == -1) {
4667c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "translations getprop failed");
4677c478bd9Sstevel@tonic-gate 	}
4687c478bd9Sstevel@tonic-gate 	return (transroot);
4697c478bd9Sstevel@tonic-gate }
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate /*
4727c478bd9Sstevel@tonic-gate  * Init routine of the nucleus data memory allocator.
4737c478bd9Sstevel@tonic-gate  *
4747c478bd9Sstevel@tonic-gate  * The nucleus data memory allocator is organized in ecache_alignsize'd
4757c478bd9Sstevel@tonic-gate  * memory chunks. Memory allocated by ndata_alloc() will never be freed.
4767c478bd9Sstevel@tonic-gate  *
4777c478bd9Sstevel@tonic-gate  * The ndata argument is used as header of the ndata freelist.
4787c478bd9Sstevel@tonic-gate  * Other freelist nodes are placed in the nucleus memory itself
4797c478bd9Sstevel@tonic-gate  * at the beginning of a free memory chunk. Therefore a freelist
4807c478bd9Sstevel@tonic-gate  * node (struct memlist) must fit into the smallest allocatable
4817c478bd9Sstevel@tonic-gate  * memory chunk (ecache_alignsize bytes).
4827c478bd9Sstevel@tonic-gate  *
4837c478bd9Sstevel@tonic-gate  * The memory interval [base, end] passed to ndata_alloc_init() must be
4847c478bd9Sstevel@tonic-gate  * bzero'd to allow the allocator to return bzero'd memory easily.
4857c478bd9Sstevel@tonic-gate  */
4867c478bd9Sstevel@tonic-gate void
4877c478bd9Sstevel@tonic-gate ndata_alloc_init(struct memlist *ndata, uintptr_t base, uintptr_t end)
4887c478bd9Sstevel@tonic-gate {
4897c478bd9Sstevel@tonic-gate 	ASSERT(sizeof (struct memlist) <= ecache_alignsize);
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	base = roundup(base, ecache_alignsize);
4927c478bd9Sstevel@tonic-gate 	end = end - end % ecache_alignsize;
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	ASSERT(base < end);
4957c478bd9Sstevel@tonic-gate 
49656f33205SJonathan Adams 	ndata->ml_address = base;
49756f33205SJonathan Adams 	ndata->ml_size = end - base;
49856f33205SJonathan Adams 	ndata->ml_next = NULL;
49956f33205SJonathan Adams 	ndata->ml_prev = NULL;
5007c478bd9Sstevel@tonic-gate }
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate /*
5037c478bd9Sstevel@tonic-gate  * Deliver the size of the largest free memory chunk.
5047c478bd9Sstevel@tonic-gate  */
5057c478bd9Sstevel@tonic-gate size_t
5067c478bd9Sstevel@tonic-gate ndata_maxsize(struct memlist *ndata)
5077c478bd9Sstevel@tonic-gate {
50856f33205SJonathan Adams 	size_t chunksize = ndata->ml_size;
5097c478bd9Sstevel@tonic-gate 
51056f33205SJonathan Adams 	while ((ndata = ndata->ml_next) != NULL) {
51156f33205SJonathan Adams 		if (chunksize < ndata->ml_size)
51256f33205SJonathan Adams 			chunksize = ndata->ml_size;
5137c478bd9Sstevel@tonic-gate 	}
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	return (chunksize);
5167c478bd9Sstevel@tonic-gate }
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate /*
5207c478bd9Sstevel@tonic-gate  * Allocate the last properly aligned memory chunk.
5217c478bd9Sstevel@tonic-gate  * This function is called when no more large nucleus memory chunks
5227c478bd9Sstevel@tonic-gate  * will be allocated.  The remaining free nucleus memory at the end
5237c478bd9Sstevel@tonic-gate  * of the nucleus can be added to the phys_avail list.
5247c478bd9Sstevel@tonic-gate  */
5257c478bd9Sstevel@tonic-gate void *
5261e45ea5aSblakej ndata_extra_base(struct memlist *ndata, size_t alignment, caddr_t endaddr)
5277c478bd9Sstevel@tonic-gate {
5287c478bd9Sstevel@tonic-gate 	uintptr_t base;
5297c478bd9Sstevel@tonic-gate 	size_t wasteage = 0;
5307c478bd9Sstevel@tonic-gate #ifdef	DEBUG
5317c478bd9Sstevel@tonic-gate 	static int called = 0;
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	if (called++ > 0)
5347c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "ndata_extra_base() called more than once");
5357c478bd9Sstevel@tonic-gate #endif /* DEBUG */
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	/*
5387c478bd9Sstevel@tonic-gate 	 * The alignment needs to be a multiple of ecache_alignsize.
5397c478bd9Sstevel@tonic-gate 	 */
5407c478bd9Sstevel@tonic-gate 	ASSERT((alignment % ecache_alignsize) ==  0);
5417c478bd9Sstevel@tonic-gate 
54256f33205SJonathan Adams 	while (ndata->ml_next != NULL) {
54356f33205SJonathan Adams 		wasteage += ndata->ml_size;
54456f33205SJonathan Adams 		ndata = ndata->ml_next;
5457c478bd9Sstevel@tonic-gate 	}
5467c478bd9Sstevel@tonic-gate 
54756f33205SJonathan Adams 	base = roundup(ndata->ml_address, alignment);
5487c478bd9Sstevel@tonic-gate 
54956f33205SJonathan Adams 	if (base >= ndata->ml_address + ndata->ml_size)
5507c478bd9Sstevel@tonic-gate 		return (NULL);
5517c478bd9Sstevel@tonic-gate 
55256f33205SJonathan Adams 	if ((caddr_t)(ndata->ml_address + ndata->ml_size) != endaddr) {
5531e45ea5aSblakej #ifdef DEBUG
5541e45ea5aSblakej 		ndata_middle_hole_detected = 1;	/* see if we hit this again */
5551e45ea5aSblakej #endif
5561e45ea5aSblakej 		return (NULL);
5571e45ea5aSblakej 	}
5581e45ea5aSblakej 
55956f33205SJonathan Adams 	if (base == ndata->ml_address) {
56056f33205SJonathan Adams 		if (ndata->ml_prev != NULL)
56156f33205SJonathan Adams 			ndata->ml_prev->ml_next = NULL;
5627c478bd9Sstevel@tonic-gate 		else
56356f33205SJonathan Adams 			ndata->ml_size = 0;
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 		bzero((void *)base, sizeof (struct memlist));
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	} else {
56856f33205SJonathan Adams 		ndata->ml_size = base - ndata->ml_address;
56956f33205SJonathan Adams 		wasteage += ndata->ml_size;
5707c478bd9Sstevel@tonic-gate 	}
5717c478bd9Sstevel@tonic-gate 	PRM_DEBUG(wasteage);
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	return ((void *)base);
5747c478bd9Sstevel@tonic-gate }
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate /*
5777c478bd9Sstevel@tonic-gate  * Select the best matching buffer, avoid memory fragmentation.
5787c478bd9Sstevel@tonic-gate  */
5797c478bd9Sstevel@tonic-gate static struct memlist *
5807c478bd9Sstevel@tonic-gate ndata_select_chunk(struct memlist *ndata, size_t wanted, size_t alignment)
5817c478bd9Sstevel@tonic-gate {
5827c478bd9Sstevel@tonic-gate 	struct memlist *fnd_below = NULL;
5837c478bd9Sstevel@tonic-gate 	struct memlist *fnd_above = NULL;
5847c478bd9Sstevel@tonic-gate 	struct memlist *fnd_unused = NULL;
5857c478bd9Sstevel@tonic-gate 	struct memlist *frlist;
5867c478bd9Sstevel@tonic-gate 	uintptr_t base;
5877c478bd9Sstevel@tonic-gate 	uintptr_t end;
5887c478bd9Sstevel@tonic-gate 	size_t below;
5897c478bd9Sstevel@tonic-gate 	size_t above;
5907c478bd9Sstevel@tonic-gate 	size_t unused;
5917c478bd9Sstevel@tonic-gate 	size_t best_below = ULONG_MAX;
5927c478bd9Sstevel@tonic-gate 	size_t best_above = ULONG_MAX;
5937c478bd9Sstevel@tonic-gate 	size_t best_unused = ULONG_MAX;
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	ASSERT(ndata != NULL);
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	/*
5987c478bd9Sstevel@tonic-gate 	 * Look for the best matching buffer, avoid memory fragmentation.
5997c478bd9Sstevel@tonic-gate 	 * The following strategy is used, try to find
6007c478bd9Sstevel@tonic-gate 	 *   1. an exact fitting buffer
6017c478bd9Sstevel@tonic-gate 	 *   2. avoid wasting any space below the buffer, take first
6027c478bd9Sstevel@tonic-gate 	 *	fitting buffer
6037c478bd9Sstevel@tonic-gate 	 *   3. avoid wasting any space above the buffer, take first
6047c478bd9Sstevel@tonic-gate 	 *	fitting buffer
6057c478bd9Sstevel@tonic-gate 	 *   4. avoid wasting space, take first fitting buffer
6067c478bd9Sstevel@tonic-gate 	 *   5. take the last buffer in chain
6077c478bd9Sstevel@tonic-gate 	 */
60856f33205SJonathan Adams 	for (frlist = ndata; frlist != NULL; frlist = frlist->ml_next) {
60956f33205SJonathan Adams 		base = roundup(frlist->ml_address, alignment);
6107c478bd9Sstevel@tonic-gate 		end = roundup(base + wanted, ecache_alignsize);
6117c478bd9Sstevel@tonic-gate 
61256f33205SJonathan Adams 		if (end > frlist->ml_address + frlist->ml_size)
6137c478bd9Sstevel@tonic-gate 			continue;
6147c478bd9Sstevel@tonic-gate 
61556f33205SJonathan Adams 		below = (base - frlist->ml_address) / ecache_alignsize;
61656f33205SJonathan Adams 		above = (frlist->ml_address + frlist->ml_size - end) /
6177c478bd9Sstevel@tonic-gate 		    ecache_alignsize;
6187c478bd9Sstevel@tonic-gate 		unused = below + above;
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 		if (unused == 0)
6217c478bd9Sstevel@tonic-gate 			return (frlist);
6227c478bd9Sstevel@tonic-gate 
62356f33205SJonathan Adams 		if (frlist->ml_next == NULL)
6247c478bd9Sstevel@tonic-gate 			break;
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 		if (below < best_below) {
6277c478bd9Sstevel@tonic-gate 			best_below = below;
6287c478bd9Sstevel@tonic-gate 			fnd_below = frlist;
6297c478bd9Sstevel@tonic-gate 		}
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 		if (above < best_above) {
6327c478bd9Sstevel@tonic-gate 			best_above = above;
6337c478bd9Sstevel@tonic-gate 			fnd_above = frlist;
6347c478bd9Sstevel@tonic-gate 		}
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 		if (unused < best_unused) {
6377c478bd9Sstevel@tonic-gate 			best_unused = unused;
6387c478bd9Sstevel@tonic-gate 			fnd_unused = frlist;
6397c478bd9Sstevel@tonic-gate 		}
6407c478bd9Sstevel@tonic-gate 	}
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	if (best_below == 0)
6437c478bd9Sstevel@tonic-gate 		return (fnd_below);
6447c478bd9Sstevel@tonic-gate 	if (best_above == 0)
6457c478bd9Sstevel@tonic-gate 		return (fnd_above);
6467c478bd9Sstevel@tonic-gate 	if (best_unused < ULONG_MAX)
6477c478bd9Sstevel@tonic-gate 		return (fnd_unused);
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	return (frlist);
6507c478bd9Sstevel@tonic-gate }
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate /*
6537c478bd9Sstevel@tonic-gate  * Nucleus data memory allocator.
6547c478bd9Sstevel@tonic-gate  * The granularity of the allocator is ecache_alignsize.
6557c478bd9Sstevel@tonic-gate  * See also comment for ndata_alloc_init().
6567c478bd9Sstevel@tonic-gate  */
6577c478bd9Sstevel@tonic-gate void *
6587c478bd9Sstevel@tonic-gate ndata_alloc(struct memlist *ndata, size_t wanted, size_t alignment)
6597c478bd9Sstevel@tonic-gate {
6607c478bd9Sstevel@tonic-gate 	struct memlist *found;
6617c478bd9Sstevel@tonic-gate 	struct memlist *fnd_above;
6627c478bd9Sstevel@tonic-gate 	uintptr_t base;
6637c478bd9Sstevel@tonic-gate 	uintptr_t end;
6647c478bd9Sstevel@tonic-gate 	size_t below;
6657c478bd9Sstevel@tonic-gate 	size_t above;
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	/*
6687c478bd9Sstevel@tonic-gate 	 * Look for the best matching buffer, avoid memory fragmentation.
6697c478bd9Sstevel@tonic-gate 	 */
6707c478bd9Sstevel@tonic-gate 	if ((found = ndata_select_chunk(ndata, wanted, alignment)) == NULL)
6717c478bd9Sstevel@tonic-gate 		return (NULL);
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	/*
6747c478bd9Sstevel@tonic-gate 	 * Allocate the nucleus data buffer.
6757c478bd9Sstevel@tonic-gate 	 */
67656f33205SJonathan Adams 	base = roundup(found->ml_address, alignment);
6777c478bd9Sstevel@tonic-gate 	end = roundup(base + wanted, ecache_alignsize);
67856f33205SJonathan Adams 	ASSERT(end <= found->ml_address + found->ml_size);
6797c478bd9Sstevel@tonic-gate 
68056f33205SJonathan Adams 	below = base - found->ml_address;
68156f33205SJonathan Adams 	above = found->ml_address + found->ml_size - end;
6827c478bd9Sstevel@tonic-gate 	ASSERT(above == 0 || (above % ecache_alignsize) == 0);
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	if (below >= ecache_alignsize) {
6857c478bd9Sstevel@tonic-gate 		/*
6867c478bd9Sstevel@tonic-gate 		 * There is free memory below the allocated memory chunk.
6877c478bd9Sstevel@tonic-gate 		 */
68856f33205SJonathan Adams 		found->ml_size = below - below % ecache_alignsize;
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 		if (above) {
6917c478bd9Sstevel@tonic-gate 			fnd_above = (struct memlist *)end;
69256f33205SJonathan Adams 			fnd_above->ml_address = end;
69356f33205SJonathan Adams 			fnd_above->ml_size = above;
6947c478bd9Sstevel@tonic-gate 
69556f33205SJonathan Adams 			if ((fnd_above->ml_next = found->ml_next) != NULL)
69656f33205SJonathan Adams 				found->ml_next->ml_prev = fnd_above;
69756f33205SJonathan Adams 			fnd_above->ml_prev = found;
69856f33205SJonathan Adams 			found->ml_next = fnd_above;
6997c478bd9Sstevel@tonic-gate 		}
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 		return ((void *)base);
7027c478bd9Sstevel@tonic-gate 	}
7037c478bd9Sstevel@tonic-gate 
70456f33205SJonathan Adams 	if (found->ml_prev == NULL) {
7057c478bd9Sstevel@tonic-gate 		/*
7067c478bd9Sstevel@tonic-gate 		 * The first chunk (ndata) is selected.
7077c478bd9Sstevel@tonic-gate 		 */
7087c478bd9Sstevel@tonic-gate 		ASSERT(found == ndata);
7097c478bd9Sstevel@tonic-gate 		if (above) {
71056f33205SJonathan Adams 			found->ml_address = end;
71156f33205SJonathan Adams 			found->ml_size = above;
71256f33205SJonathan Adams 		} else if (found->ml_next != NULL) {
71356f33205SJonathan Adams 			found->ml_address = found->ml_next->ml_address;
71456f33205SJonathan Adams 			found->ml_size = found->ml_next->ml_size;
71556f33205SJonathan Adams 			if ((found->ml_next = found->ml_next->ml_next) != NULL)
71656f33205SJonathan Adams 				found->ml_next->ml_prev = found;
7177c478bd9Sstevel@tonic-gate 
71856f33205SJonathan Adams 			bzero((void *)found->ml_address,
71956f33205SJonathan Adams 			    sizeof (struct memlist));
7207c478bd9Sstevel@tonic-gate 		} else {
72156f33205SJonathan Adams 			found->ml_address = end;
72256f33205SJonathan Adams 			found->ml_size = 0;
7237c478bd9Sstevel@tonic-gate 		}
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 		return ((void *)base);
7267c478bd9Sstevel@tonic-gate 	}
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 	/*
7297c478bd9Sstevel@tonic-gate 	 * Not the first chunk.
7307c478bd9Sstevel@tonic-gate 	 */
7317c478bd9Sstevel@tonic-gate 	if (above) {
7327c478bd9Sstevel@tonic-gate 		fnd_above = (struct memlist *)end;
73356f33205SJonathan Adams 		fnd_above->ml_address = end;
73456f33205SJonathan Adams 		fnd_above->ml_size = above;
7357c478bd9Sstevel@tonic-gate 
73656f33205SJonathan Adams 		if ((fnd_above->ml_next = found->ml_next) != NULL)
73756f33205SJonathan Adams 			fnd_above->ml_next->ml_prev = fnd_above;
73856f33205SJonathan Adams 		fnd_above->ml_prev = found->ml_prev;
73956f33205SJonathan Adams 		found->ml_prev->ml_next = fnd_above;
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	} else {
74256f33205SJonathan Adams 		if ((found->ml_prev->ml_next = found->ml_next) != NULL)
74356f33205SJonathan Adams 			found->ml_next->ml_prev = found->ml_prev;
7447c478bd9Sstevel@tonic-gate 	}
7457c478bd9Sstevel@tonic-gate 
74656f33205SJonathan Adams 	bzero((void *)found->ml_address, sizeof (struct memlist));
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	return ((void *)base);
7497c478bd9Sstevel@tonic-gate }
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate /*
7527c478bd9Sstevel@tonic-gate  * Size the kernel TSBs based upon the amount of physical
7537c478bd9Sstevel@tonic-gate  * memory in the system.
7547c478bd9Sstevel@tonic-gate  */
7557c478bd9Sstevel@tonic-gate static void
7567c478bd9Sstevel@tonic-gate calc_tsb_sizes(pgcnt_t npages)
7577c478bd9Sstevel@tonic-gate {
7587c478bd9Sstevel@tonic-gate 	PRM_DEBUG(npages);
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	if (npages <= TSB_FREEMEM_MIN) {
7617c478bd9Sstevel@tonic-gate 		ktsb_szcode = TSB_128K_SZCODE;
7627c478bd9Sstevel@tonic-gate 		enable_bigktsb = 0;
7637c478bd9Sstevel@tonic-gate 	} else if (npages <= TSB_FREEMEM_LARGE / 2) {
7647c478bd9Sstevel@tonic-gate 		ktsb_szcode = TSB_256K_SZCODE;
7657c478bd9Sstevel@tonic-gate 		enable_bigktsb = 0;
7667c478bd9Sstevel@tonic-gate 	} else if (npages <= TSB_FREEMEM_LARGE) {
7677c478bd9Sstevel@tonic-gate 		ktsb_szcode = TSB_512K_SZCODE;
7687c478bd9Sstevel@tonic-gate 		enable_bigktsb = 0;
7697c478bd9Sstevel@tonic-gate 	} else if (npages <= TSB_FREEMEM_LARGE * 2 ||
7707c478bd9Sstevel@tonic-gate 	    enable_bigktsb == 0) {
7717c478bd9Sstevel@tonic-gate 		ktsb_szcode = TSB_1M_SZCODE;
7727c478bd9Sstevel@tonic-gate 		enable_bigktsb = 0;
7737c478bd9Sstevel@tonic-gate 	} else {
7747c478bd9Sstevel@tonic-gate 		ktsb_szcode = highbit(npages - 1);
7757c478bd9Sstevel@tonic-gate 		ktsb_szcode -= TSB_START_SIZE;
7767c478bd9Sstevel@tonic-gate 		ktsb_szcode = MAX(ktsb_szcode, MIN_BIGKTSB_SZCODE);
7777c478bd9Sstevel@tonic-gate 		ktsb_szcode = MIN(ktsb_szcode, MAX_BIGKTSB_SZCODE);
7787c478bd9Sstevel@tonic-gate 	}
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 	/*
7817c478bd9Sstevel@tonic-gate 	 * We choose the TSB to hold kernel 4M mappings to have twice
7827c478bd9Sstevel@tonic-gate 	 * the reach as the primary kernel TSB since this TSB will
7837c478bd9Sstevel@tonic-gate 	 * potentially (currently) be shared by both mappings to all of
784bb121940Sdp78419 	 * physical memory plus user TSBs. If this TSB has to be in nucleus
785bb121940Sdp78419 	 * (only for Spitfire and Cheetah) limit its size to 64K.
7867c478bd9Sstevel@tonic-gate 	 */
787bb121940Sdp78419 	ktsb4m_szcode = highbit((2 * npages) / TTEPAGES(TTE4M) - 1);
788bb121940Sdp78419 	ktsb4m_szcode -= TSB_START_SIZE;
789bb121940Sdp78419 	ktsb4m_szcode = MAX(ktsb4m_szcode, TSB_MIN_SZCODE);
790bb121940Sdp78419 	ktsb4m_szcode = MIN(ktsb4m_szcode, TSB_SOFTSZ_MASK);
791bb121940Sdp78419 	if ((enable_bigktsb == 0 || ktsb_phys == 0) && ktsb4m_szcode >
792bb121940Sdp78419 	    TSB_64K_SZCODE) {
793bb121940Sdp78419 		ktsb4m_szcode = TSB_64K_SZCODE;
794bb121940Sdp78419 		max_bootlp_tteszc = TTE8K;
795bb121940Sdp78419 	}
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	ktsb_sz = TSB_BYTES(ktsb_szcode);	/* kernel 8K tsb size */
7987c478bd9Sstevel@tonic-gate 	ktsb4m_sz = TSB_BYTES(ktsb4m_szcode);	/* kernel 4M tsb size */
7997c478bd9Sstevel@tonic-gate }
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate /*
8027c478bd9Sstevel@tonic-gate  * Allocate kernel TSBs from nucleus data memory.
8037c478bd9Sstevel@tonic-gate  * The function return 0 on success and -1 on failure.
8047c478bd9Sstevel@tonic-gate  */
8057c478bd9Sstevel@tonic-gate int
8067c478bd9Sstevel@tonic-gate ndata_alloc_tsbs(struct memlist *ndata, pgcnt_t npages)
8077c478bd9Sstevel@tonic-gate {
8087c478bd9Sstevel@tonic-gate 	/*
809bb121940Sdp78419 	 * Set ktsb_phys to 1 if the processor supports ASI_QUAD_LDD_PHYS.
810bb121940Sdp78419 	 */
81107d06da5SSurya Prakki 	(void) sfmmu_setup_4lp();
812bb121940Sdp78419 
813bb121940Sdp78419 	/*
8147c478bd9Sstevel@tonic-gate 	 * Size the kernel TSBs based upon the amount of physical
8157c478bd9Sstevel@tonic-gate 	 * memory in the system.
8167c478bd9Sstevel@tonic-gate 	 */
8177c478bd9Sstevel@tonic-gate 	calc_tsb_sizes(npages);
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 	/*
8207c478bd9Sstevel@tonic-gate 	 * Allocate the 8K kernel TSB if it belongs inside the nucleus.
8217c478bd9Sstevel@tonic-gate 	 */
8227c478bd9Sstevel@tonic-gate 	if (enable_bigktsb == 0) {
8237c478bd9Sstevel@tonic-gate 		if ((ktsb_base = ndata_alloc(ndata, ktsb_sz, ktsb_sz)) == NULL)
8247c478bd9Sstevel@tonic-gate 			return (-1);
8257c478bd9Sstevel@tonic-gate 		ASSERT(!((uintptr_t)ktsb_base & (ktsb_sz - 1)));
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 		PRM_DEBUG(ktsb_base);
8287c478bd9Sstevel@tonic-gate 		PRM_DEBUG(ktsb_sz);
8297c478bd9Sstevel@tonic-gate 		PRM_DEBUG(ktsb_szcode);
8307c478bd9Sstevel@tonic-gate 	}
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	/*
8337c478bd9Sstevel@tonic-gate 	 * Next, allocate 4M kernel TSB from the nucleus since it's small.
8347c478bd9Sstevel@tonic-gate 	 */
835bb121940Sdp78419 	if (ktsb4m_szcode <= TSB_64K_SZCODE) {
836bb121940Sdp78419 
837bb121940Sdp78419 		ktsb4m_base = ndata_alloc(ndata, ktsb4m_sz, ktsb4m_sz);
838bb121940Sdp78419 		if (ktsb4m_base == NULL)
8397c478bd9Sstevel@tonic-gate 			return (-1);
8407c478bd9Sstevel@tonic-gate 		ASSERT(!((uintptr_t)ktsb4m_base & (ktsb4m_sz - 1)));
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 		PRM_DEBUG(ktsb4m_base);
8437c478bd9Sstevel@tonic-gate 		PRM_DEBUG(ktsb4m_sz);
8447c478bd9Sstevel@tonic-gate 		PRM_DEBUG(ktsb4m_szcode);
845bb121940Sdp78419 	}
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 	return (0);
8487c478bd9Sstevel@tonic-gate }
8497c478bd9Sstevel@tonic-gate 
850986fd29aSsetje size_t
851986fd29aSsetje calc_hmehash_sz(pgcnt_t npages)
8527c478bd9Sstevel@tonic-gate {
8537c478bd9Sstevel@tonic-gate 	ulong_t hme_buckets;
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	/*
8567c478bd9Sstevel@tonic-gate 	 * The number of buckets in the hme hash tables
8577c478bd9Sstevel@tonic-gate 	 * is a power of 2 such that the average hash chain length is
8587c478bd9Sstevel@tonic-gate 	 * HMENT_HASHAVELEN.  The number of buckets for the user hash is
8597c478bd9Sstevel@tonic-gate 	 * a function of physical memory and a predefined overmapping factor.
8607c478bd9Sstevel@tonic-gate 	 * The number of buckets for the kernel hash is a function of
8617c478bd9Sstevel@tonic-gate 	 * physical memory only.
8627c478bd9Sstevel@tonic-gate 	 */
8637c478bd9Sstevel@tonic-gate 	hme_buckets = (npages * HMEHASH_FACTOR) /
8647c478bd9Sstevel@tonic-gate 	    (HMENT_HASHAVELEN * (HMEBLK_SPAN(TTE8K) >> MMU_PAGESHIFT));
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	uhmehash_num = (int)MIN(hme_buckets, MAX_UHME_BUCKETS);
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	if (uhmehash_num > USER_BUCKETS_THRESHOLD) {
8697c478bd9Sstevel@tonic-gate 		/*
8707c478bd9Sstevel@tonic-gate 		 * if uhmehash_num is not power of 2 round it down to the
8717c478bd9Sstevel@tonic-gate 		 *  next power of 2.
8727c478bd9Sstevel@tonic-gate 		 */
8737c478bd9Sstevel@tonic-gate 		uint_t align = 1 << (highbit(uhmehash_num - 1) - 1);
8747c478bd9Sstevel@tonic-gate 		uhmehash_num = P2ALIGN(uhmehash_num, align);
8757c478bd9Sstevel@tonic-gate 	} else
8767c478bd9Sstevel@tonic-gate 		uhmehash_num = 1 << highbit(uhmehash_num - 1);
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 	hme_buckets = npages / (HMEBLK_SPAN(TTE8K) >> MMU_PAGESHIFT);
8797c478bd9Sstevel@tonic-gate 	khmehash_num = (int)MIN(hme_buckets, MAX_KHME_BUCKETS);
8807c478bd9Sstevel@tonic-gate 	khmehash_num = 1 << highbit(khmehash_num - 1);
8817c478bd9Sstevel@tonic-gate 	khmehash_num = MAX(khmehash_num, MIN_KHME_BUCKETS);
8827c478bd9Sstevel@tonic-gate 
883986fd29aSsetje 	return ((uhmehash_num + khmehash_num) * sizeof (struct hmehash_bucket));
8847c478bd9Sstevel@tonic-gate }
8857c478bd9Sstevel@tonic-gate 
886986fd29aSsetje caddr_t
887986fd29aSsetje alloc_hmehash(caddr_t alloc_base)
888986fd29aSsetje {
889986fd29aSsetje 	size_t khmehash_sz, uhmehash_sz;
890986fd29aSsetje 
891986fd29aSsetje 	khme_hash = (struct hmehash_bucket *)alloc_base;
892986fd29aSsetje 	khmehash_sz = khmehash_num * sizeof (struct hmehash_bucket);
893986fd29aSsetje 	alloc_base += khmehash_sz;
894986fd29aSsetje 
895986fd29aSsetje 	uhme_hash = (struct hmehash_bucket *)alloc_base;
896986fd29aSsetje 	uhmehash_sz = uhmehash_num * sizeof (struct hmehash_bucket);
897986fd29aSsetje 	alloc_base += uhmehash_sz;
898986fd29aSsetje 
8997c478bd9Sstevel@tonic-gate 	PRM_DEBUG(khme_hash);
9007c478bd9Sstevel@tonic-gate 	PRM_DEBUG(uhme_hash);
901986fd29aSsetje 
902986fd29aSsetje 	return (alloc_base);
903986fd29aSsetje }
904986fd29aSsetje 
905986fd29aSsetje /*
906986fd29aSsetje  * Allocate hat structs from the nucleus data memory.
907986fd29aSsetje  */
908986fd29aSsetje int
909*b52a336eSPavel Tatashin ndata_alloc_hat(struct memlist *ndata)
910986fd29aSsetje {
911986fd29aSsetje 	size_t	cb_alloc_sz;
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	cb_alloc_sz = sfmmu_max_cb_id * sizeof (struct sfmmu_callback);
9147c478bd9Sstevel@tonic-gate 	PRM_DEBUG(cb_alloc_sz);
9157c478bd9Sstevel@tonic-gate 	sfmmu_cb_table = ndata_alloc(ndata, cb_alloc_sz, ecache_alignsize);
916986fd29aSsetje 	if (sfmmu_cb_table == NULL)
917986fd29aSsetje 		return (-1);
9187c478bd9Sstevel@tonic-gate 	PRM_DEBUG(sfmmu_cb_table);
9197c478bd9Sstevel@tonic-gate 
920986fd29aSsetje 	return (0);
921986fd29aSsetje }
922986fd29aSsetje 
923986fd29aSsetje int
924986fd29aSsetje ndata_alloc_kpm(struct memlist *ndata, pgcnt_t kpm_npages)
925986fd29aSsetje {
926986fd29aSsetje 	size_t	kpmp_alloc_sz;
927986fd29aSsetje 
9287c478bd9Sstevel@tonic-gate 	/*
9297c478bd9Sstevel@tonic-gate 	 * For the kpm_page mutex array we allocate one mutex every 16
9307c478bd9Sstevel@tonic-gate 	 * kpm pages (64MB). In smallpage mode we allocate one mutex
9317c478bd9Sstevel@tonic-gate 	 * every 8K pages. The minimum is set to 64 entries and the
9327c478bd9Sstevel@tonic-gate 	 * maximum to 8K entries.
9337c478bd9Sstevel@tonic-gate 	 */
9347c478bd9Sstevel@tonic-gate 	if (kpm_smallpages == 0) {
9357c478bd9Sstevel@tonic-gate 		kpmp_shift = highbit(sizeof (kpm_page_t)) - 1;
9367c478bd9Sstevel@tonic-gate 		kpmp_table_sz = 1 << highbit(kpm_npages / 16);
9377c478bd9Sstevel@tonic-gate 		kpmp_table_sz = (kpmp_table_sz < 64) ? 64 :
9387c478bd9Sstevel@tonic-gate 		    ((kpmp_table_sz > 8192) ? 8192 : kpmp_table_sz);
9397c478bd9Sstevel@tonic-gate 		kpmp_alloc_sz = kpmp_table_sz * sizeof (kpm_hlk_t);
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 		kpmp_table = ndata_alloc(ndata, kpmp_alloc_sz,
9427c478bd9Sstevel@tonic-gate 		    ecache_alignsize);
943986fd29aSsetje 		if (kpmp_table == NULL)
944986fd29aSsetje 			return (-1);
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 		PRM_DEBUG(kpmp_table);
9477c478bd9Sstevel@tonic-gate 		PRM_DEBUG(kpmp_table_sz);
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 		kpmp_stable_sz = 0;
9507c478bd9Sstevel@tonic-gate 		kpmp_stable = NULL;
9517c478bd9Sstevel@tonic-gate 	} else {
9527c478bd9Sstevel@tonic-gate 		ASSERT(kpm_pgsz == PAGESIZE);
9537c478bd9Sstevel@tonic-gate 		kpmp_shift = highbit(sizeof (kpm_shlk_t)) + 1;
9547c478bd9Sstevel@tonic-gate 		kpmp_stable_sz = 1 << highbit(kpm_npages / 8192);
9557c478bd9Sstevel@tonic-gate 		kpmp_stable_sz = (kpmp_stable_sz < 64) ? 64 :
9567c478bd9Sstevel@tonic-gate 		    ((kpmp_stable_sz > 8192) ? 8192 : kpmp_stable_sz);
9577c478bd9Sstevel@tonic-gate 		kpmp_alloc_sz = kpmp_stable_sz * sizeof (kpm_shlk_t);
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 		kpmp_stable = ndata_alloc(ndata, kpmp_alloc_sz,
9607c478bd9Sstevel@tonic-gate 		    ecache_alignsize);
961986fd29aSsetje 		if (kpmp_stable == NULL)
962986fd29aSsetje 			return (-1);
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 		PRM_DEBUG(kpmp_stable);
9657c478bd9Sstevel@tonic-gate 		PRM_DEBUG(kpmp_stable_sz);
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 		kpmp_table_sz = 0;
9687c478bd9Sstevel@tonic-gate 		kpmp_table = NULL;
9697c478bd9Sstevel@tonic-gate 	}
9707c478bd9Sstevel@tonic-gate 	PRM_DEBUG(kpmp_shift);
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 	return (0);
9737c478bd9Sstevel@tonic-gate }
9747c478bd9Sstevel@tonic-gate 
975bb121940Sdp78419 /*
976bb121940Sdp78419  * This function bop allocs kernel TSBs.
9777c478bd9Sstevel@tonic-gate  */
9787c478bd9Sstevel@tonic-gate caddr_t
9797c478bd9Sstevel@tonic-gate sfmmu_ktsb_alloc(caddr_t tsbbase)
9807c478bd9Sstevel@tonic-gate {
9817c478bd9Sstevel@tonic-gate 	caddr_t vaddr;
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 	if (enable_bigktsb) {
9847c478bd9Sstevel@tonic-gate 		ktsb_base = (caddr_t)roundup((uintptr_t)tsbbase, ktsb_sz);
985986fd29aSsetje 		vaddr = prom_alloc(ktsb_base, ktsb_sz, ktsb_sz);
9867c478bd9Sstevel@tonic-gate 		if (vaddr != ktsb_base)
9877c478bd9Sstevel@tonic-gate 			cmn_err(CE_PANIC, "sfmmu_ktsb_alloc: can't alloc"
988bb121940Sdp78419 			    " 8K bigktsb");
9897c478bd9Sstevel@tonic-gate 		ktsb_base = vaddr;
9907c478bd9Sstevel@tonic-gate 		tsbbase = ktsb_base + ktsb_sz;
9917c478bd9Sstevel@tonic-gate 		PRM_DEBUG(ktsb_base);
9927c478bd9Sstevel@tonic-gate 		PRM_DEBUG(tsbbase);
9937c478bd9Sstevel@tonic-gate 	}
994bb121940Sdp78419 
995bb121940Sdp78419 	if (ktsb4m_szcode > TSB_64K_SZCODE) {
996bb121940Sdp78419 		ASSERT(ktsb_phys && enable_bigktsb);
997bb121940Sdp78419 		ktsb4m_base = (caddr_t)roundup((uintptr_t)tsbbase, ktsb4m_sz);
998bb121940Sdp78419 		vaddr = (caddr_t)BOP_ALLOC(bootops, ktsb4m_base, ktsb4m_sz,
999bb121940Sdp78419 		    ktsb4m_sz);
1000bb121940Sdp78419 		if (vaddr != ktsb4m_base)
1001bb121940Sdp78419 			cmn_err(CE_PANIC, "sfmmu_ktsb_alloc: can't alloc"
1002bb121940Sdp78419 			    " 4M bigktsb");
1003bb121940Sdp78419 		ktsb4m_base = vaddr;
1004bb121940Sdp78419 		tsbbase = ktsb4m_base + ktsb4m_sz;
1005bb121940Sdp78419 		PRM_DEBUG(ktsb4m_base);
1006bb121940Sdp78419 		PRM_DEBUG(tsbbase);
1007bb121940Sdp78419 	}
10087c478bd9Sstevel@tonic-gate 	return (tsbbase);
10097c478bd9Sstevel@tonic-gate }
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate /*
10127c478bd9Sstevel@tonic-gate  * Moves code assembled outside of the trap table into the trap
10137c478bd9Sstevel@tonic-gate  * table taking care to relocate relative branches to code outside
10147c478bd9Sstevel@tonic-gate  * of the trap handler.
10157c478bd9Sstevel@tonic-gate  */
10167c478bd9Sstevel@tonic-gate static void
10177c478bd9Sstevel@tonic-gate sfmmu_reloc_trap_handler(void *tablep, void *start, size_t count)
10187c478bd9Sstevel@tonic-gate {
10197c478bd9Sstevel@tonic-gate 	size_t i;
10207c478bd9Sstevel@tonic-gate 	uint32_t *src;
10217c478bd9Sstevel@tonic-gate 	uint32_t *dst;
10227c478bd9Sstevel@tonic-gate 	uint32_t inst;
10237c478bd9Sstevel@tonic-gate 	int op, op2;
10247c478bd9Sstevel@tonic-gate 	int32_t offset;
10257c478bd9Sstevel@tonic-gate 	int disp;
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 	src = start;
10287c478bd9Sstevel@tonic-gate 	dst = tablep;
10297c478bd9Sstevel@tonic-gate 	offset = src - dst;
10307c478bd9Sstevel@tonic-gate 	for (src = start, i = 0; i < count; i++, src++, dst++) {
10317c478bd9Sstevel@tonic-gate 		inst = *dst = *src;
10327c478bd9Sstevel@tonic-gate 		op = (inst >> 30) & 0x2;
10337c478bd9Sstevel@tonic-gate 		if (op == 1) {
10347c478bd9Sstevel@tonic-gate 			/* call */
10357c478bd9Sstevel@tonic-gate 			disp = ((int32_t)inst << 2) >> 2; /* sign-extend */
10367c478bd9Sstevel@tonic-gate 			if (disp + i >= 0 && disp + i < count)
10377c478bd9Sstevel@tonic-gate 				continue;
10387c478bd9Sstevel@tonic-gate 			disp += offset;
10397c478bd9Sstevel@tonic-gate 			inst = 0x40000000u | (disp & 0x3fffffffu);
10407c478bd9Sstevel@tonic-gate 			*dst = inst;
10417c478bd9Sstevel@tonic-gate 		} else if (op == 0) {
10427c478bd9Sstevel@tonic-gate 			/* branch or sethi */
10437c478bd9Sstevel@tonic-gate 			op2 = (inst >> 22) & 0x7;
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 			switch (op2) {
10467c478bd9Sstevel@tonic-gate 			case 0x3: /* BPr */
10477c478bd9Sstevel@tonic-gate 				disp = (((inst >> 20) & 0x3) << 14) |
10487c478bd9Sstevel@tonic-gate 				    (inst & 0x3fff);
10497c478bd9Sstevel@tonic-gate 				disp = (disp << 16) >> 16; /* sign-extend */
10507c478bd9Sstevel@tonic-gate 				if (disp + i >= 0 && disp + i < count)
10517c478bd9Sstevel@tonic-gate 					continue;
10527c478bd9Sstevel@tonic-gate 				disp += offset;
10537c478bd9Sstevel@tonic-gate 				if (((disp << 16) >> 16) != disp)
10547c478bd9Sstevel@tonic-gate 					cmn_err(CE_PANIC, "bad reloc");
10557c478bd9Sstevel@tonic-gate 				inst &= ~0x303fff;
10567c478bd9Sstevel@tonic-gate 				inst |= (disp & 0x3fff);
10577c478bd9Sstevel@tonic-gate 				inst |= (disp & 0xc000) << 6;
10587c478bd9Sstevel@tonic-gate 				break;
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 			case 0x2: /* Bicc */
10617c478bd9Sstevel@tonic-gate 				disp = ((int32_t)inst << 10) >> 10;
10627c478bd9Sstevel@tonic-gate 				if (disp + i >= 0 && disp + i < count)
10637c478bd9Sstevel@tonic-gate 					continue;
10647c478bd9Sstevel@tonic-gate 				disp += offset;
10657c478bd9Sstevel@tonic-gate 				if (((disp << 10) >> 10) != disp)
10667c478bd9Sstevel@tonic-gate 					cmn_err(CE_PANIC, "bad reloc");
10677c478bd9Sstevel@tonic-gate 				inst &= ~0x3fffff;
10687c478bd9Sstevel@tonic-gate 				inst |= (disp & 0x3fffff);
10697c478bd9Sstevel@tonic-gate 				break;
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 			case 0x1: /* Bpcc */
10727c478bd9Sstevel@tonic-gate 				disp = ((int32_t)inst << 13) >> 13;
10737c478bd9Sstevel@tonic-gate 				if (disp + i >= 0 && disp + i < count)
10747c478bd9Sstevel@tonic-gate 					continue;
10757c478bd9Sstevel@tonic-gate 				disp += offset;
10767c478bd9Sstevel@tonic-gate 				if (((disp << 13) >> 13) != disp)
10777c478bd9Sstevel@tonic-gate 					cmn_err(CE_PANIC, "bad reloc");
10787c478bd9Sstevel@tonic-gate 				inst &= ~0x7ffff;
10797c478bd9Sstevel@tonic-gate 				inst |= (disp & 0x7ffffu);
10807c478bd9Sstevel@tonic-gate 				break;
10817c478bd9Sstevel@tonic-gate 			}
10827c478bd9Sstevel@tonic-gate 			*dst = inst;
10837c478bd9Sstevel@tonic-gate 		}
10847c478bd9Sstevel@tonic-gate 	}
10857c478bd9Sstevel@tonic-gate 	flush_instr_mem(tablep, count * sizeof (uint32_t));
10867c478bd9Sstevel@tonic-gate }
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate /*
10897c478bd9Sstevel@tonic-gate  * Routine to allocate a large page to use in the TSB caches.
10907c478bd9Sstevel@tonic-gate  */
10917c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10927c478bd9Sstevel@tonic-gate static page_t *
10937c478bd9Sstevel@tonic-gate sfmmu_tsb_page_create(void *addr, size_t size, int vmflag, void *arg)
10947c478bd9Sstevel@tonic-gate {
10957c478bd9Sstevel@tonic-gate 	int pgflags;
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 	pgflags = PG_EXCL;
10987c478bd9Sstevel@tonic-gate 	if ((vmflag & VM_NOSLEEP) == 0)
10997c478bd9Sstevel@tonic-gate 		pgflags |= PG_WAIT;
11007c478bd9Sstevel@tonic-gate 	if (vmflag & VM_PANIC)
11017c478bd9Sstevel@tonic-gate 		pgflags |= PG_PANIC;
11027c478bd9Sstevel@tonic-gate 	if (vmflag & VM_PUSHPAGE)
11037c478bd9Sstevel@tonic-gate 		pgflags |= PG_PUSHPAGE;
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate 	return (page_create_va_large(&kvp, (u_offset_t)(uintptr_t)addr, size,
11067c478bd9Sstevel@tonic-gate 	    pgflags, &kvseg, addr, arg));
11077c478bd9Sstevel@tonic-gate }
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate /*
11107c478bd9Sstevel@tonic-gate  * Allocate a large page to back the virtual address range
11117c478bd9Sstevel@tonic-gate  * [addr, addr + size).  If addr is NULL, allocate the virtual address
11127c478bd9Sstevel@tonic-gate  * space as well.
11137c478bd9Sstevel@tonic-gate  */
11147c478bd9Sstevel@tonic-gate static void *
11157c478bd9Sstevel@tonic-gate sfmmu_tsb_xalloc(vmem_t *vmp, void *inaddr, size_t size, int vmflag,
11167c478bd9Sstevel@tonic-gate     uint_t attr, page_t *(*page_create_func)(void *, size_t, int, void *),
11177c478bd9Sstevel@tonic-gate     void *pcarg)
11187c478bd9Sstevel@tonic-gate {
11197c478bd9Sstevel@tonic-gate 	page_t *ppl;
11207c478bd9Sstevel@tonic-gate 	page_t *rootpp;
11217c478bd9Sstevel@tonic-gate 	caddr_t addr = inaddr;
11227c478bd9Sstevel@tonic-gate 	pgcnt_t npages = btopr(size);
11237c478bd9Sstevel@tonic-gate 	page_t **ppa;
11247c478bd9Sstevel@tonic-gate 	int i = 0;
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 	/*
11277c478bd9Sstevel@tonic-gate 	 * Assuming that only TSBs will call this with size > PAGESIZE
11287c478bd9Sstevel@tonic-gate 	 * There is no reason why this couldn't be expanded to 8k pages as
11297c478bd9Sstevel@tonic-gate 	 * well, or other page sizes in the future .... but for now, we
11307c478bd9Sstevel@tonic-gate 	 * only support fixed sized page requests.
11317c478bd9Sstevel@tonic-gate 	 */
11327c478bd9Sstevel@tonic-gate 	if ((inaddr == NULL) && ((addr = vmem_xalloc(vmp, size, size, 0, 0,
11337c478bd9Sstevel@tonic-gate 	    NULL, NULL, vmflag)) == NULL))
11347c478bd9Sstevel@tonic-gate 		return (NULL);
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 	if (page_resv(npages, vmflag & VM_KMFLAGS) == 0) {
11377c478bd9Sstevel@tonic-gate 		if (inaddr == NULL)
11387c478bd9Sstevel@tonic-gate 			vmem_xfree(vmp, addr, size);
11397c478bd9Sstevel@tonic-gate 		return (NULL);
11407c478bd9Sstevel@tonic-gate 	}
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 	ppl = page_create_func(addr, size, vmflag, pcarg);
11437c478bd9Sstevel@tonic-gate 	if (ppl == NULL) {
11447c478bd9Sstevel@tonic-gate 		if (inaddr == NULL)
11457c478bd9Sstevel@tonic-gate 			vmem_xfree(vmp, addr, size);
11467c478bd9Sstevel@tonic-gate 		page_unresv(npages);
11477c478bd9Sstevel@tonic-gate 		return (NULL);
11487c478bd9Sstevel@tonic-gate 	}
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 	rootpp = ppl;
11517c478bd9Sstevel@tonic-gate 	ppa = kmem_zalloc(npages * sizeof (page_t *), KM_SLEEP);
11527c478bd9Sstevel@tonic-gate 	while (ppl != NULL) {
11537c478bd9Sstevel@tonic-gate 		page_t *pp = ppl;
11547c478bd9Sstevel@tonic-gate 		ppa[i++] = pp;
11557c478bd9Sstevel@tonic-gate 		page_sub(&ppl, pp);
11567c478bd9Sstevel@tonic-gate 		ASSERT(page_iolock_assert(pp));
11577c478bd9Sstevel@tonic-gate 		page_io_unlock(pp);
11587c478bd9Sstevel@tonic-gate 	}
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate 	/*
11617c478bd9Sstevel@tonic-gate 	 * Load the locked entry.  It's OK to preload the entry into
11627c478bd9Sstevel@tonic-gate 	 * the TSB since we now support large mappings in the kernel TSB.
11637c478bd9Sstevel@tonic-gate 	 */
11647c478bd9Sstevel@tonic-gate 	hat_memload_array(kas.a_hat, (caddr_t)rootpp->p_offset, size,
11657c478bd9Sstevel@tonic-gate 	    ppa, (PROT_ALL & ~PROT_USER) | HAT_NOSYNC | attr, HAT_LOAD_LOCK);
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 	for (--i; i >= 0; --i) {
11687c478bd9Sstevel@tonic-gate 		(void) page_pp_lock(ppa[i], 0, 1);
11697c478bd9Sstevel@tonic-gate 		page_unlock(ppa[i]);
11707c478bd9Sstevel@tonic-gate 	}
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 	kmem_free(ppa, npages * sizeof (page_t *));
11737c478bd9Sstevel@tonic-gate 	return (addr);
11747c478bd9Sstevel@tonic-gate }
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate /* Called to import new spans into the TSB vmem arenas */
11777c478bd9Sstevel@tonic-gate void *
11787c478bd9Sstevel@tonic-gate sfmmu_tsb_segkmem_alloc(vmem_t *vmp, size_t size, int vmflag)
11797c478bd9Sstevel@tonic-gate {
11807c478bd9Sstevel@tonic-gate 	lgrp_id_t lgrpid = LGRP_NONE;
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate 	if (tsb_lgrp_affinity) {
11837c478bd9Sstevel@tonic-gate 		/*
11847c478bd9Sstevel@tonic-gate 		 * Search for the vmp->lgrpid mapping by brute force;
11857c478bd9Sstevel@tonic-gate 		 * some day vmp will have an lgrp, until then we have
11867c478bd9Sstevel@tonic-gate 		 * to do this the hard way.
11877c478bd9Sstevel@tonic-gate 		 */
11887c478bd9Sstevel@tonic-gate 		for (lgrpid = 0; lgrpid < NLGRPS_MAX &&
1189986fd29aSsetje 		    vmp != kmem_tsb_default_arena[lgrpid]; lgrpid++)
1190986fd29aSsetje 			;
11917c478bd9Sstevel@tonic-gate 		if (lgrpid == NLGRPS_MAX)
11927c478bd9Sstevel@tonic-gate 			lgrpid = LGRP_NONE;
11937c478bd9Sstevel@tonic-gate 	}
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 	return (sfmmu_tsb_xalloc(vmp, NULL, size, vmflag, 0,
11967c478bd9Sstevel@tonic-gate 	    sfmmu_tsb_page_create, lgrpid != LGRP_NONE? &lgrpid : NULL));
11977c478bd9Sstevel@tonic-gate }
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate /* Called to free spans from the TSB vmem arenas */
12007c478bd9Sstevel@tonic-gate void
12017c478bd9Sstevel@tonic-gate sfmmu_tsb_segkmem_free(vmem_t *vmp, void *inaddr, size_t size)
12027c478bd9Sstevel@tonic-gate {
12037c478bd9Sstevel@tonic-gate 	page_t *pp;
12047c478bd9Sstevel@tonic-gate 	caddr_t addr = inaddr;
12057c478bd9Sstevel@tonic-gate 	caddr_t eaddr;
12067c478bd9Sstevel@tonic-gate 	pgcnt_t npages = btopr(size);
12077c478bd9Sstevel@tonic-gate 	pgcnt_t pgs_left = npages;
12087c478bd9Sstevel@tonic-gate 	page_t *rootpp = NULL;
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 	hat_unload(kas.a_hat, addr, size, HAT_UNLOAD_UNLOCK);
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 	for (eaddr = addr + size; addr < eaddr; addr += PAGESIZE) {
12137c478bd9Sstevel@tonic-gate 		pp = page_lookup(&kvp, (u_offset_t)(uintptr_t)addr, SE_EXCL);
12147c478bd9Sstevel@tonic-gate 		if (pp == NULL)
12157c478bd9Sstevel@tonic-gate 			panic("sfmmu_tsb_segkmem_free: page not found");
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate 		ASSERT(PAGE_EXCL(pp));
12187c478bd9Sstevel@tonic-gate 		page_pp_unlock(pp, 0, 1);
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 		if (rootpp == NULL)
12217c478bd9Sstevel@tonic-gate 			rootpp = pp;
12227c478bd9Sstevel@tonic-gate 		if (--pgs_left == 0) {
12237c478bd9Sstevel@tonic-gate 			/*
12247c478bd9Sstevel@tonic-gate 			 * similar logic to segspt_free_pages, but we know we
12257c478bd9Sstevel@tonic-gate 			 * have one large page.
12267c478bd9Sstevel@tonic-gate 			 */
12277c478bd9Sstevel@tonic-gate 			page_destroy_pages(rootpp);
12287c478bd9Sstevel@tonic-gate 		}
12297c478bd9Sstevel@tonic-gate 	}
12307c478bd9Sstevel@tonic-gate 	page_unresv(npages);
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 	if (vmp != NULL)
12337c478bd9Sstevel@tonic-gate 		vmem_xfree(vmp, inaddr, size);
12347c478bd9Sstevel@tonic-gate }
1235