xref: /titanic_51/usr/src/uts/sun4/vm/sfmmu.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
30*7c478bd9Sstevel@tonic-gate #include <vm/hat.h>
31*7c478bd9Sstevel@tonic-gate #include <vm/hat_sfmmu.h>
32*7c478bd9Sstevel@tonic-gate #include <vm/page.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/pte.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/mman.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/machparam.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/mmu.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/cpu.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/lgrp.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
50*7c478bd9Sstevel@tonic-gate #include <vm/as.h>
51*7c478bd9Sstevel@tonic-gate #include <vm/seg.h>
52*7c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
53*7c478bd9Sstevel@tonic-gate #include <vm/seg_kp.h>
54*7c478bd9Sstevel@tonic-gate #include <vm/seg_kpm.h>
55*7c478bd9Sstevel@tonic-gate #include <vm/rm.h>
56*7c478bd9Sstevel@tonic-gate #include <vm/vm_dep.h>
57*7c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
58*7c478bd9Sstevel@tonic-gate #include <sys/vm_machparam.h>
59*7c478bd9Sstevel@tonic-gate #include <sys/promif.h>
60*7c478bd9Sstevel@tonic-gate #include <sys/prom_isa.h>
61*7c478bd9Sstevel@tonic-gate #include <sys/prom_plat.h>
62*7c478bd9Sstevel@tonic-gate #include <sys/prom_debug.h>
63*7c478bd9Sstevel@tonic-gate #include <sys/privregs.h>
64*7c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
65*7c478bd9Sstevel@tonic-gate #include <sys/memlist.h>
66*7c478bd9Sstevel@tonic-gate #include <sys/memlist_plat.h>
67*7c478bd9Sstevel@tonic-gate #include <sys/cpu_module.h>
68*7c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
69*7c478bd9Sstevel@tonic-gate #include <sys/kdi.h>
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate /*
72*7c478bd9Sstevel@tonic-gate  * Static routines
73*7c478bd9Sstevel@tonic-gate  */
74*7c478bd9Sstevel@tonic-gate static void	sfmmu_map_prom_mappings(struct translation *, size_t);
75*7c478bd9Sstevel@tonic-gate static struct translation *read_prom_mappings(size_t *);
76*7c478bd9Sstevel@tonic-gate static void	sfmmu_reloc_trap_handler(void *, void *, size_t);
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate /*
79*7c478bd9Sstevel@tonic-gate  * External routines
80*7c478bd9Sstevel@tonic-gate  */
81*7c478bd9Sstevel@tonic-gate extern void sfmmu_remap_kernel(void);
82*7c478bd9Sstevel@tonic-gate extern void sfmmu_patch_utsb(void);
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate /*
85*7c478bd9Sstevel@tonic-gate  * Global Data:
86*7c478bd9Sstevel@tonic-gate  */
87*7c478bd9Sstevel@tonic-gate extern caddr_t	textva, datava;
88*7c478bd9Sstevel@tonic-gate extern tte_t	ktext_tte, kdata_tte;	/* ttes for kernel text and data */
89*7c478bd9Sstevel@tonic-gate extern int	enable_bigktsb;
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate uint64_t memsegspa = (uintptr_t)MSEG_NULLPTR_PA; /* memsegs physical linkage */
92*7c478bd9Sstevel@tonic-gate uint64_t memseg_phash[N_MEM_SLOTS];	/* use physical memseg addresses */
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate int	sfmmu_kern_mapped = 0;
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate /*
97*7c478bd9Sstevel@tonic-gate  * DMMU primary context register for the kernel context. Machine specific code
98*7c478bd9Sstevel@tonic-gate  * inserts correct page size codes when necessary
99*7c478bd9Sstevel@tonic-gate  */
100*7c478bd9Sstevel@tonic-gate uint64_t kcontextreg = KCONTEXT;
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate /* Extern Global Data */
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate extern int page_relocate_ready;
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate /*
107*7c478bd9Sstevel@tonic-gate  * Controls the logic which enables the use of the
108*7c478bd9Sstevel@tonic-gate  * QUAD_LDD_PHYS ASI for TSB accesses.
109*7c478bd9Sstevel@tonic-gate  */
110*7c478bd9Sstevel@tonic-gate extern int	ktsb_phys;
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate /*
113*7c478bd9Sstevel@tonic-gate  * Global Routines called from within:
114*7c478bd9Sstevel@tonic-gate  *	usr/src/uts/sun4u
115*7c478bd9Sstevel@tonic-gate  *	usr/src/uts/sfmmu
116*7c478bd9Sstevel@tonic-gate  *	usr/src/uts/sun
117*7c478bd9Sstevel@tonic-gate  */
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate pfn_t
120*7c478bd9Sstevel@tonic-gate va_to_pfn(void *vaddr)
121*7c478bd9Sstevel@tonic-gate {
122*7c478bd9Sstevel@tonic-gate 	u_longlong_t physaddr;
123*7c478bd9Sstevel@tonic-gate 	int mode, valid;
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 	if (tba_taken_over)
126*7c478bd9Sstevel@tonic-gate 		return (hat_getpfnum(kas.a_hat, (caddr_t)vaddr));
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 	if ((prom_translate_virt(vaddr, &valid, &physaddr, &mode) != -1) &&
129*7c478bd9Sstevel@tonic-gate 	    (valid == -1)) {
130*7c478bd9Sstevel@tonic-gate 		return ((pfn_t)(physaddr >> MMU_PAGESHIFT));
131*7c478bd9Sstevel@tonic-gate 	}
132*7c478bd9Sstevel@tonic-gate 	return (PFN_INVALID);
133*7c478bd9Sstevel@tonic-gate }
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate uint64_t
136*7c478bd9Sstevel@tonic-gate va_to_pa(void *vaddr)
137*7c478bd9Sstevel@tonic-gate {
138*7c478bd9Sstevel@tonic-gate 	pfn_t pfn;
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 	if ((pfn = va_to_pfn(vaddr)) == PFN_INVALID)
141*7c478bd9Sstevel@tonic-gate 		return ((uint64_t)-1);
142*7c478bd9Sstevel@tonic-gate 	return (((uint64_t)pfn << MMU_PAGESHIFT) |
143*7c478bd9Sstevel@tonic-gate 		((uint64_t)vaddr & MMU_PAGEOFFSET));
144*7c478bd9Sstevel@tonic-gate }
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate void
147*7c478bd9Sstevel@tonic-gate hat_kern_setup(void)
148*7c478bd9Sstevel@tonic-gate {
149*7c478bd9Sstevel@tonic-gate 	struct translation *trans_root;
150*7c478bd9Sstevel@tonic-gate 	size_t ntrans_root;
151*7c478bd9Sstevel@tonic-gate 	extern void startup_fixup_physavail(void);
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 	/*
154*7c478bd9Sstevel@tonic-gate 	 * These are the steps we take to take over the mmu from the prom.
155*7c478bd9Sstevel@tonic-gate 	 *
156*7c478bd9Sstevel@tonic-gate 	 * (1)	Read the prom's mappings through the translation property.
157*7c478bd9Sstevel@tonic-gate 	 * (2)	Remap the kernel text and kernel data with 2 locked 4MB ttes.
158*7c478bd9Sstevel@tonic-gate 	 *	Create the the hmeblks for these 2 ttes at this time.
159*7c478bd9Sstevel@tonic-gate 	 * (3)	Create hat structures for all other prom mappings.  Since the
160*7c478bd9Sstevel@tonic-gate 	 *	kernel text and data hme_blks have already been created we
161*7c478bd9Sstevel@tonic-gate 	 *	skip the equivalent prom's mappings.
162*7c478bd9Sstevel@tonic-gate 	 * (4)	Initialize the tsb and its corresponding hardware regs.
163*7c478bd9Sstevel@tonic-gate 	 * (5)	Take over the trap table (currently in startup).
164*7c478bd9Sstevel@tonic-gate 	 * (6)	Up to this point it is possible the prom required some of its
165*7c478bd9Sstevel@tonic-gate 	 *	locked tte's.  Now that we own the trap table we remove them.
166*7c478bd9Sstevel@tonic-gate 	 */
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate 	ktsb_pbase = va_to_pa(ktsb_base);
169*7c478bd9Sstevel@tonic-gate 	ktsb4m_pbase = va_to_pa(ktsb4m_base);
170*7c478bd9Sstevel@tonic-gate 	PRM_DEBUG(ktsb_pbase);
171*7c478bd9Sstevel@tonic-gate 	PRM_DEBUG(ktsb4m_pbase);
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 	sfmmu_setup_4lp();
174*7c478bd9Sstevel@tonic-gate 	sfmmu_patch_ktsb();
175*7c478bd9Sstevel@tonic-gate 	sfmmu_patch_utsb();
176*7c478bd9Sstevel@tonic-gate 	sfmmu_patch_mmu_asi(ktsb_phys);
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 	sfmmu_init_tsbs();
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 	if (kpm_enable) {
181*7c478bd9Sstevel@tonic-gate 		sfmmu_kpm_patch_tlbm();
182*7c478bd9Sstevel@tonic-gate 		if (kpm_smallpages == 0) {
183*7c478bd9Sstevel@tonic-gate 			sfmmu_kpm_patch_tsbm();
184*7c478bd9Sstevel@tonic-gate 		}
185*7c478bd9Sstevel@tonic-gate 	}
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate 	/*
188*7c478bd9Sstevel@tonic-gate 	 * The 8K-indexed kernel TSB space is used to hold
189*7c478bd9Sstevel@tonic-gate 	 * translations below...
190*7c478bd9Sstevel@tonic-gate 	 */
191*7c478bd9Sstevel@tonic-gate 	trans_root = read_prom_mappings(&ntrans_root);
192*7c478bd9Sstevel@tonic-gate 	sfmmu_remap_kernel();
193*7c478bd9Sstevel@tonic-gate 	startup_fixup_physavail();
194*7c478bd9Sstevel@tonic-gate 	mmu_init_kernel_pgsz(kas.a_hat);
195*7c478bd9Sstevel@tonic-gate 	sfmmu_map_prom_mappings(trans_root, ntrans_root);
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 	/*
198*7c478bd9Sstevel@tonic-gate 	 * We invalidate 8K kernel TSB because we used it in
199*7c478bd9Sstevel@tonic-gate 	 * sfmmu_map_prom_mappings()
200*7c478bd9Sstevel@tonic-gate 	 */
201*7c478bd9Sstevel@tonic-gate 	sfmmu_inv_tsb(ktsb_base, ktsb_sz);
202*7c478bd9Sstevel@tonic-gate 	sfmmu_inv_tsb(ktsb4m_base, ktsb4m_sz);
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate 	sfmmu_init_ktsbinfo();
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate 	sfmmu_kern_mapped = 1;
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate 	/*
210*7c478bd9Sstevel@tonic-gate 	 * hments have been created for mapped pages, and thus we're ready
211*7c478bd9Sstevel@tonic-gate 	 * for kmdb to start using its own trap table.  It walks the hments
212*7c478bd9Sstevel@tonic-gate 	 * to resolve TLB misses, and can't be used until they're ready.
213*7c478bd9Sstevel@tonic-gate 	 */
214*7c478bd9Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
215*7c478bd9Sstevel@tonic-gate 		kdi_dvec_vmready();
216*7c478bd9Sstevel@tonic-gate }
217*7c478bd9Sstevel@tonic-gate 
218*7c478bd9Sstevel@tonic-gate /*
219*7c478bd9Sstevel@tonic-gate  * Macro used below to convert the prom's 32-bit high and low fields into
220*7c478bd9Sstevel@tonic-gate  * a value appropriate for the 64-bit kernel.
221*7c478bd9Sstevel@tonic-gate  */
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate #define	COMBINE(hi, lo) (((uint64_t)(uint32_t)(hi) << 32) | (uint32_t)(lo))
224*7c478bd9Sstevel@tonic-gate 
225*7c478bd9Sstevel@tonic-gate /*
226*7c478bd9Sstevel@tonic-gate  * This function traverses the prom mapping list and creates equivalent
227*7c478bd9Sstevel@tonic-gate  * mappings in the sfmmu mapping hash.
228*7c478bd9Sstevel@tonic-gate  */
229*7c478bd9Sstevel@tonic-gate static void
230*7c478bd9Sstevel@tonic-gate sfmmu_map_prom_mappings(struct translation *trans_root, size_t ntrans_root)
231*7c478bd9Sstevel@tonic-gate {
232*7c478bd9Sstevel@tonic-gate 	struct translation *promt;
233*7c478bd9Sstevel@tonic-gate 	tte_t	tte, oldtte, *ttep;
234*7c478bd9Sstevel@tonic-gate 	pfn_t	pfn, oldpfn, basepfn;
235*7c478bd9Sstevel@tonic-gate 	caddr_t vaddr;
236*7c478bd9Sstevel@tonic-gate 	size_t	size, offset;
237*7c478bd9Sstevel@tonic-gate 	unsigned long i;
238*7c478bd9Sstevel@tonic-gate 	uint_t	attr;
239*7c478bd9Sstevel@tonic-gate 	page_t *pp;
240*7c478bd9Sstevel@tonic-gate 	extern struct memlist *virt_avail;
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate 	ttep = &tte;
243*7c478bd9Sstevel@tonic-gate 	for (i = 0, promt = trans_root; i < ntrans_root; i++, promt++) {
244*7c478bd9Sstevel@tonic-gate 		ASSERT(promt->tte_hi != 0);
245*7c478bd9Sstevel@tonic-gate 		ASSERT32(promt->virt_hi == 0 && promt->size_hi == 0);
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate 		/*
248*7c478bd9Sstevel@tonic-gate 		 * hack until we get rid of map-for-unix
249*7c478bd9Sstevel@tonic-gate 		 */
250*7c478bd9Sstevel@tonic-gate 		if (COMBINE(promt->virt_hi, promt->virt_lo) < KERNELBASE)
251*7c478bd9Sstevel@tonic-gate 			continue;
252*7c478bd9Sstevel@tonic-gate 
253*7c478bd9Sstevel@tonic-gate 		ttep->tte_inthi = promt->tte_hi;
254*7c478bd9Sstevel@tonic-gate 		ttep->tte_intlo = promt->tte_lo;
255*7c478bd9Sstevel@tonic-gate 		attr = PROC_DATA | HAT_NOSYNC;
256*7c478bd9Sstevel@tonic-gate #if defined(TTE_IS_GLOBAL)
257*7c478bd9Sstevel@tonic-gate 		if (TTE_IS_GLOBAL(ttep)) {
258*7c478bd9Sstevel@tonic-gate 			/*
259*7c478bd9Sstevel@tonic-gate 			 * The prom better not use global translations
260*7c478bd9Sstevel@tonic-gate 			 * because a user process might use the same
261*7c478bd9Sstevel@tonic-gate 			 * virtual addresses
262*7c478bd9Sstevel@tonic-gate 			 */
263*7c478bd9Sstevel@tonic-gate 			cmn_err(CE_PANIC, "map_prom: global translation");
264*7c478bd9Sstevel@tonic-gate 			TTE_SET_LOFLAGS(ttep, TTE_GLB_INT, 0);
265*7c478bd9Sstevel@tonic-gate 		}
266*7c478bd9Sstevel@tonic-gate #endif
267*7c478bd9Sstevel@tonic-gate 		if (TTE_IS_LOCKED(ttep)) {
268*7c478bd9Sstevel@tonic-gate 			/* clear the lock bits */
269*7c478bd9Sstevel@tonic-gate 			TTE_CLR_LOCKED(ttep);
270*7c478bd9Sstevel@tonic-gate 		}
271*7c478bd9Sstevel@tonic-gate 		attr |= (TTE_IS_VCACHEABLE(ttep)) ? 0 : SFMMU_UNCACHEVTTE;
272*7c478bd9Sstevel@tonic-gate 		attr |= (TTE_IS_PCACHEABLE(ttep)) ? 0 : SFMMU_UNCACHEPTTE;
273*7c478bd9Sstevel@tonic-gate 		attr |= (TTE_IS_SIDEFFECT(ttep)) ? SFMMU_SIDEFFECT : 0;
274*7c478bd9Sstevel@tonic-gate 		attr |= (TTE_IS_IE(ttep)) ? HAT_STRUCTURE_LE : 0;
275*7c478bd9Sstevel@tonic-gate 
276*7c478bd9Sstevel@tonic-gate 		size = COMBINE(promt->size_hi, promt->size_lo);
277*7c478bd9Sstevel@tonic-gate 		offset = 0;
278*7c478bd9Sstevel@tonic-gate 		basepfn = TTE_TO_PFN((caddr_t)COMBINE(promt->virt_hi,
279*7c478bd9Sstevel@tonic-gate 		    promt->virt_lo), ttep);
280*7c478bd9Sstevel@tonic-gate 		while (size) {
281*7c478bd9Sstevel@tonic-gate 			vaddr = (caddr_t)(COMBINE(promt->virt_hi,
282*7c478bd9Sstevel@tonic-gate 			    promt->virt_lo) + offset);
283*7c478bd9Sstevel@tonic-gate 
284*7c478bd9Sstevel@tonic-gate 			/*
285*7c478bd9Sstevel@tonic-gate 			 * make sure address is not in virt-avail list
286*7c478bd9Sstevel@tonic-gate 			 */
287*7c478bd9Sstevel@tonic-gate 			if (address_in_memlist(virt_avail, (uint64_t)vaddr,
288*7c478bd9Sstevel@tonic-gate 			    size)) {
289*7c478bd9Sstevel@tonic-gate 				cmn_err(CE_PANIC, "map_prom: inconsistent "
290*7c478bd9Sstevel@tonic-gate 				    "translation/avail lists");
291*7c478bd9Sstevel@tonic-gate 			}
292*7c478bd9Sstevel@tonic-gate 
293*7c478bd9Sstevel@tonic-gate 			pfn = basepfn + mmu_btop(offset);
294*7c478bd9Sstevel@tonic-gate 			if (pf_is_memory(pfn)) {
295*7c478bd9Sstevel@tonic-gate 				if (attr & SFMMU_UNCACHEPTTE) {
296*7c478bd9Sstevel@tonic-gate 					cmn_err(CE_PANIC, "map_prom: "
297*7c478bd9Sstevel@tonic-gate 					    "uncached prom memory page");
298*7c478bd9Sstevel@tonic-gate 				}
299*7c478bd9Sstevel@tonic-gate 			} else {
300*7c478bd9Sstevel@tonic-gate 				if (!(attr & SFMMU_SIDEFFECT)) {
301*7c478bd9Sstevel@tonic-gate 					cmn_err(CE_PANIC, "map_prom: prom "
302*7c478bd9Sstevel@tonic-gate 					    "i/o page without side-effect");
303*7c478bd9Sstevel@tonic-gate 				}
304*7c478bd9Sstevel@tonic-gate 			}
305*7c478bd9Sstevel@tonic-gate 			oldpfn = sfmmu_vatopfn(vaddr, KHATID, &oldtte);
306*7c478bd9Sstevel@tonic-gate 			ASSERT(oldpfn != PFN_SUSPENDED);
307*7c478bd9Sstevel@tonic-gate 			ASSERT(page_relocate_ready == 0);
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate 			if (oldpfn != PFN_INVALID) {
310*7c478bd9Sstevel@tonic-gate 				/*
311*7c478bd9Sstevel@tonic-gate 				 * mapping already exists.
312*7c478bd9Sstevel@tonic-gate 				 * Verify they are equal
313*7c478bd9Sstevel@tonic-gate 				 */
314*7c478bd9Sstevel@tonic-gate 				if (pfn != oldpfn) {
315*7c478bd9Sstevel@tonic-gate 					cmn_err(CE_PANIC, "map_prom: mapping "
316*7c478bd9Sstevel@tonic-gate 					    "conflict (va=0x%p pfn=%p, "
317*7c478bd9Sstevel@tonic-gate 					    "oldpfn=%p)",
318*7c478bd9Sstevel@tonic-gate 					    (void *)vaddr, (void *)pfn,
319*7c478bd9Sstevel@tonic-gate 					    (void *)oldpfn);
320*7c478bd9Sstevel@tonic-gate 				}
321*7c478bd9Sstevel@tonic-gate 				size -= MMU_PAGESIZE;
322*7c478bd9Sstevel@tonic-gate 				offset += MMU_PAGESIZE;
323*7c478bd9Sstevel@tonic-gate 				continue;
324*7c478bd9Sstevel@tonic-gate 			}
325*7c478bd9Sstevel@tonic-gate 
326*7c478bd9Sstevel@tonic-gate 			pp = page_numtopp_nolock(pfn);
327*7c478bd9Sstevel@tonic-gate 			if ((pp != NULL) && PP_ISFREE((page_t *)pp)) {
328*7c478bd9Sstevel@tonic-gate 				cmn_err(CE_PANIC, "map_prom: "
329*7c478bd9Sstevel@tonic-gate 				    "prom-mapped page (va 0x%p, pfn 0x%p) "
330*7c478bd9Sstevel@tonic-gate 				    "on free list", (void *)vaddr, (void *)pfn);
331*7c478bd9Sstevel@tonic-gate 			}
332*7c478bd9Sstevel@tonic-gate 
333*7c478bd9Sstevel@tonic-gate 			sfmmu_memtte(ttep, pfn, attr, TTE8K);
334*7c478bd9Sstevel@tonic-gate 			sfmmu_tteload(kas.a_hat, ttep, vaddr, pp,
335*7c478bd9Sstevel@tonic-gate 			    HAT_LOAD_LOCK | SFMMU_NO_TSBLOAD);
336*7c478bd9Sstevel@tonic-gate 			size -= MMU_PAGESIZE;
337*7c478bd9Sstevel@tonic-gate 			offset += MMU_PAGESIZE;
338*7c478bd9Sstevel@tonic-gate 		}
339*7c478bd9Sstevel@tonic-gate 	}
340*7c478bd9Sstevel@tonic-gate }
341*7c478bd9Sstevel@tonic-gate 
342*7c478bd9Sstevel@tonic-gate #undef COMBINE	/* local to previous routine */
343*7c478bd9Sstevel@tonic-gate 
344*7c478bd9Sstevel@tonic-gate /*
345*7c478bd9Sstevel@tonic-gate  * This routine reads in the "translations" property in to a buffer and
346*7c478bd9Sstevel@tonic-gate  * returns a pointer to this buffer and the number of translations.
347*7c478bd9Sstevel@tonic-gate  */
348*7c478bd9Sstevel@tonic-gate static struct translation *
349*7c478bd9Sstevel@tonic-gate read_prom_mappings(size_t *ntransrootp)
350*7c478bd9Sstevel@tonic-gate {
351*7c478bd9Sstevel@tonic-gate 	char *prop = "translations";
352*7c478bd9Sstevel@tonic-gate 	size_t translen;
353*7c478bd9Sstevel@tonic-gate 	dnode_t node;
354*7c478bd9Sstevel@tonic-gate 	struct translation *transroot;
355*7c478bd9Sstevel@tonic-gate 
356*7c478bd9Sstevel@tonic-gate 	/*
357*7c478bd9Sstevel@tonic-gate 	 * the "translations" property is associated with the mmu node
358*7c478bd9Sstevel@tonic-gate 	 */
359*7c478bd9Sstevel@tonic-gate 	node = (dnode_t)prom_getphandle(prom_mmu_ihandle());
360*7c478bd9Sstevel@tonic-gate 
361*7c478bd9Sstevel@tonic-gate 	/*
362*7c478bd9Sstevel@tonic-gate 	 * We use the TSB space to read in the prom mappings.  This space
363*7c478bd9Sstevel@tonic-gate 	 * is currently not being used because we haven't taken over the
364*7c478bd9Sstevel@tonic-gate 	 * trap table yet.  It should be big enough to hold the mappings.
365*7c478bd9Sstevel@tonic-gate 	 */
366*7c478bd9Sstevel@tonic-gate 	if ((translen = prom_getproplen(node, prop)) == -1)
367*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "no translations property");
368*7c478bd9Sstevel@tonic-gate 	*ntransrootp = translen / sizeof (*transroot);
369*7c478bd9Sstevel@tonic-gate 	translen = roundup(translen, MMU_PAGESIZE);
370*7c478bd9Sstevel@tonic-gate 	PRM_DEBUG(translen);
371*7c478bd9Sstevel@tonic-gate 	if (translen > TSB_BYTES(ktsb_szcode))
372*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "not enough space for translations");
373*7c478bd9Sstevel@tonic-gate 
374*7c478bd9Sstevel@tonic-gate 	transroot = (struct translation *)ktsb_base;
375*7c478bd9Sstevel@tonic-gate 	ASSERT(transroot);
376*7c478bd9Sstevel@tonic-gate 	if (prom_getprop(node, prop, (caddr_t)transroot) == -1) {
377*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "translations getprop failed");
378*7c478bd9Sstevel@tonic-gate 	}
379*7c478bd9Sstevel@tonic-gate 	return (transroot);
380*7c478bd9Sstevel@tonic-gate }
381*7c478bd9Sstevel@tonic-gate 
382*7c478bd9Sstevel@tonic-gate /*
383*7c478bd9Sstevel@tonic-gate  * Init routine of the nucleus data memory allocator.
384*7c478bd9Sstevel@tonic-gate  *
385*7c478bd9Sstevel@tonic-gate  * The nucleus data memory allocator is organized in ecache_alignsize'd
386*7c478bd9Sstevel@tonic-gate  * memory chunks. Memory allocated by ndata_alloc() will never be freed.
387*7c478bd9Sstevel@tonic-gate  *
388*7c478bd9Sstevel@tonic-gate  * The ndata argument is used as header of the ndata freelist.
389*7c478bd9Sstevel@tonic-gate  * Other freelist nodes are placed in the nucleus memory itself
390*7c478bd9Sstevel@tonic-gate  * at the beginning of a free memory chunk. Therefore a freelist
391*7c478bd9Sstevel@tonic-gate  * node (struct memlist) must fit into the smallest allocatable
392*7c478bd9Sstevel@tonic-gate  * memory chunk (ecache_alignsize bytes).
393*7c478bd9Sstevel@tonic-gate  *
394*7c478bd9Sstevel@tonic-gate  * The memory interval [base, end] passed to ndata_alloc_init() must be
395*7c478bd9Sstevel@tonic-gate  * bzero'd to allow the allocator to return bzero'd memory easily.
396*7c478bd9Sstevel@tonic-gate  */
397*7c478bd9Sstevel@tonic-gate void
398*7c478bd9Sstevel@tonic-gate ndata_alloc_init(struct memlist *ndata, uintptr_t base, uintptr_t end)
399*7c478bd9Sstevel@tonic-gate {
400*7c478bd9Sstevel@tonic-gate 	ASSERT(sizeof (struct memlist) <= ecache_alignsize);
401*7c478bd9Sstevel@tonic-gate 
402*7c478bd9Sstevel@tonic-gate 	base = roundup(base, ecache_alignsize);
403*7c478bd9Sstevel@tonic-gate 	end = end - end % ecache_alignsize;
404*7c478bd9Sstevel@tonic-gate 
405*7c478bd9Sstevel@tonic-gate 	ASSERT(base < end);
406*7c478bd9Sstevel@tonic-gate 
407*7c478bd9Sstevel@tonic-gate 	ndata->address = base;
408*7c478bd9Sstevel@tonic-gate 	ndata->size = end - base;
409*7c478bd9Sstevel@tonic-gate 	ndata->next = NULL;
410*7c478bd9Sstevel@tonic-gate 	ndata->prev = NULL;
411*7c478bd9Sstevel@tonic-gate }
412*7c478bd9Sstevel@tonic-gate 
413*7c478bd9Sstevel@tonic-gate /*
414*7c478bd9Sstevel@tonic-gate  * Deliver the size of the largest free memory chunk.
415*7c478bd9Sstevel@tonic-gate  */
416*7c478bd9Sstevel@tonic-gate size_t
417*7c478bd9Sstevel@tonic-gate ndata_maxsize(struct memlist *ndata)
418*7c478bd9Sstevel@tonic-gate {
419*7c478bd9Sstevel@tonic-gate 	size_t chunksize = ndata->size;
420*7c478bd9Sstevel@tonic-gate 
421*7c478bd9Sstevel@tonic-gate 	while ((ndata = ndata->next) != NULL) {
422*7c478bd9Sstevel@tonic-gate 		if (chunksize < ndata->size)
423*7c478bd9Sstevel@tonic-gate 			chunksize = ndata->size;
424*7c478bd9Sstevel@tonic-gate 	}
425*7c478bd9Sstevel@tonic-gate 
426*7c478bd9Sstevel@tonic-gate 	return (chunksize);
427*7c478bd9Sstevel@tonic-gate }
428*7c478bd9Sstevel@tonic-gate 
429*7c478bd9Sstevel@tonic-gate /*
430*7c478bd9Sstevel@tonic-gate  * This is a special function to figure out if the memory chunk needed
431*7c478bd9Sstevel@tonic-gate  * for the page structs can fit in the nucleus or not. If it fits the
432*7c478bd9Sstevel@tonic-gate  * function calculates and returns the possible remaining ndata size
433*7c478bd9Sstevel@tonic-gate  * in the last element if the size needed for page structs would be
434*7c478bd9Sstevel@tonic-gate  * allocated from the nucleus.
435*7c478bd9Sstevel@tonic-gate  */
436*7c478bd9Sstevel@tonic-gate size_t
437*7c478bd9Sstevel@tonic-gate ndata_spare(struct memlist *ndata, size_t wanted, size_t alignment)
438*7c478bd9Sstevel@tonic-gate {
439*7c478bd9Sstevel@tonic-gate 	struct memlist *frlist;
440*7c478bd9Sstevel@tonic-gate 	uintptr_t base;
441*7c478bd9Sstevel@tonic-gate 	uintptr_t end;
442*7c478bd9Sstevel@tonic-gate 
443*7c478bd9Sstevel@tonic-gate 	for (frlist = ndata; frlist != NULL; frlist = frlist->next) {
444*7c478bd9Sstevel@tonic-gate 		base = roundup(frlist->address, alignment);
445*7c478bd9Sstevel@tonic-gate 		end = roundup(base + wanted, ecache_alignsize);
446*7c478bd9Sstevel@tonic-gate 
447*7c478bd9Sstevel@tonic-gate 		if (end <= frlist->address + frlist->size) {
448*7c478bd9Sstevel@tonic-gate 			if (frlist->next == NULL)
449*7c478bd9Sstevel@tonic-gate 				return (frlist->address + frlist->size - end);
450*7c478bd9Sstevel@tonic-gate 
451*7c478bd9Sstevel@tonic-gate 			while (frlist->next != NULL)
452*7c478bd9Sstevel@tonic-gate 				frlist = frlist->next;
453*7c478bd9Sstevel@tonic-gate 
454*7c478bd9Sstevel@tonic-gate 			return (frlist->size);
455*7c478bd9Sstevel@tonic-gate 		}
456*7c478bd9Sstevel@tonic-gate 	}
457*7c478bd9Sstevel@tonic-gate 
458*7c478bd9Sstevel@tonic-gate 	return (0);
459*7c478bd9Sstevel@tonic-gate }
460*7c478bd9Sstevel@tonic-gate 
461*7c478bd9Sstevel@tonic-gate /*
462*7c478bd9Sstevel@tonic-gate  * Allocate the last properly aligned memory chunk.
463*7c478bd9Sstevel@tonic-gate  * This function is called when no more large nucleus memory chunks
464*7c478bd9Sstevel@tonic-gate  * will be allocated.  The remaining free nucleus memory at the end
465*7c478bd9Sstevel@tonic-gate  * of the nucleus can be added to the phys_avail list.
466*7c478bd9Sstevel@tonic-gate  */
467*7c478bd9Sstevel@tonic-gate void *
468*7c478bd9Sstevel@tonic-gate ndata_extra_base(struct memlist *ndata, size_t alignment)
469*7c478bd9Sstevel@tonic-gate {
470*7c478bd9Sstevel@tonic-gate 	uintptr_t base;
471*7c478bd9Sstevel@tonic-gate 	size_t wasteage = 0;
472*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
473*7c478bd9Sstevel@tonic-gate 	static int called = 0;
474*7c478bd9Sstevel@tonic-gate 
475*7c478bd9Sstevel@tonic-gate 	if (called++ > 0)
476*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "ndata_extra_base() called more than once");
477*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
478*7c478bd9Sstevel@tonic-gate 
479*7c478bd9Sstevel@tonic-gate 	/*
480*7c478bd9Sstevel@tonic-gate 	 * The alignment needs to be a multiple of ecache_alignsize.
481*7c478bd9Sstevel@tonic-gate 	 */
482*7c478bd9Sstevel@tonic-gate 	ASSERT((alignment % ecache_alignsize) ==  0);
483*7c478bd9Sstevel@tonic-gate 
484*7c478bd9Sstevel@tonic-gate 	while (ndata->next != NULL) {
485*7c478bd9Sstevel@tonic-gate 		wasteage += ndata->size;
486*7c478bd9Sstevel@tonic-gate 		ndata = ndata->next;
487*7c478bd9Sstevel@tonic-gate 	}
488*7c478bd9Sstevel@tonic-gate 
489*7c478bd9Sstevel@tonic-gate 	base = roundup(ndata->address, alignment);
490*7c478bd9Sstevel@tonic-gate 
491*7c478bd9Sstevel@tonic-gate 	if (base >= ndata->address + ndata->size)
492*7c478bd9Sstevel@tonic-gate 		return (NULL);
493*7c478bd9Sstevel@tonic-gate 
494*7c478bd9Sstevel@tonic-gate 	if (base == ndata->address) {
495*7c478bd9Sstevel@tonic-gate 		if (ndata->prev != NULL)
496*7c478bd9Sstevel@tonic-gate 			ndata->prev->next = NULL;
497*7c478bd9Sstevel@tonic-gate 		else
498*7c478bd9Sstevel@tonic-gate 			ndata->size = 0;
499*7c478bd9Sstevel@tonic-gate 
500*7c478bd9Sstevel@tonic-gate 		bzero((void *)base, sizeof (struct memlist));
501*7c478bd9Sstevel@tonic-gate 
502*7c478bd9Sstevel@tonic-gate 	} else {
503*7c478bd9Sstevel@tonic-gate 		ndata->size = base - ndata->address;
504*7c478bd9Sstevel@tonic-gate 		wasteage += ndata->size;
505*7c478bd9Sstevel@tonic-gate 	}
506*7c478bd9Sstevel@tonic-gate 	PRM_DEBUG(wasteage);
507*7c478bd9Sstevel@tonic-gate 
508*7c478bd9Sstevel@tonic-gate 	return ((void *)base);
509*7c478bd9Sstevel@tonic-gate }
510*7c478bd9Sstevel@tonic-gate 
511*7c478bd9Sstevel@tonic-gate /*
512*7c478bd9Sstevel@tonic-gate  * Select the best matching buffer, avoid memory fragmentation.
513*7c478bd9Sstevel@tonic-gate  */
514*7c478bd9Sstevel@tonic-gate static struct memlist *
515*7c478bd9Sstevel@tonic-gate ndata_select_chunk(struct memlist *ndata, size_t wanted, size_t alignment)
516*7c478bd9Sstevel@tonic-gate {
517*7c478bd9Sstevel@tonic-gate 	struct memlist *fnd_below = NULL;
518*7c478bd9Sstevel@tonic-gate 	struct memlist *fnd_above = NULL;
519*7c478bd9Sstevel@tonic-gate 	struct memlist *fnd_unused = NULL;
520*7c478bd9Sstevel@tonic-gate 	struct memlist *frlist;
521*7c478bd9Sstevel@tonic-gate 	uintptr_t base;
522*7c478bd9Sstevel@tonic-gate 	uintptr_t end;
523*7c478bd9Sstevel@tonic-gate 	size_t below;
524*7c478bd9Sstevel@tonic-gate 	size_t above;
525*7c478bd9Sstevel@tonic-gate 	size_t unused;
526*7c478bd9Sstevel@tonic-gate 	size_t best_below = ULONG_MAX;
527*7c478bd9Sstevel@tonic-gate 	size_t best_above = ULONG_MAX;
528*7c478bd9Sstevel@tonic-gate 	size_t best_unused = ULONG_MAX;
529*7c478bd9Sstevel@tonic-gate 
530*7c478bd9Sstevel@tonic-gate 	ASSERT(ndata != NULL);
531*7c478bd9Sstevel@tonic-gate 
532*7c478bd9Sstevel@tonic-gate 	/*
533*7c478bd9Sstevel@tonic-gate 	 * Look for the best matching buffer, avoid memory fragmentation.
534*7c478bd9Sstevel@tonic-gate 	 * The following strategy is used, try to find
535*7c478bd9Sstevel@tonic-gate 	 *   1. an exact fitting buffer
536*7c478bd9Sstevel@tonic-gate 	 *   2. avoid wasting any space below the buffer, take first
537*7c478bd9Sstevel@tonic-gate 	 *	fitting buffer
538*7c478bd9Sstevel@tonic-gate 	 *   3. avoid wasting any space above the buffer, take first
539*7c478bd9Sstevel@tonic-gate 	 *	fitting buffer
540*7c478bd9Sstevel@tonic-gate 	 *   4. avoid wasting space, take first fitting buffer
541*7c478bd9Sstevel@tonic-gate 	 *   5. take the last buffer in chain
542*7c478bd9Sstevel@tonic-gate 	 */
543*7c478bd9Sstevel@tonic-gate 	for (frlist = ndata; frlist != NULL; frlist = frlist->next) {
544*7c478bd9Sstevel@tonic-gate 		base = roundup(frlist->address, alignment);
545*7c478bd9Sstevel@tonic-gate 		end = roundup(base + wanted, ecache_alignsize);
546*7c478bd9Sstevel@tonic-gate 
547*7c478bd9Sstevel@tonic-gate 		if (end > frlist->address + frlist->size)
548*7c478bd9Sstevel@tonic-gate 			continue;
549*7c478bd9Sstevel@tonic-gate 
550*7c478bd9Sstevel@tonic-gate 		below = (base - frlist->address) / ecache_alignsize;
551*7c478bd9Sstevel@tonic-gate 		above = (frlist->address + frlist->size - end) /
552*7c478bd9Sstevel@tonic-gate 		    ecache_alignsize;
553*7c478bd9Sstevel@tonic-gate 		unused = below + above;
554*7c478bd9Sstevel@tonic-gate 
555*7c478bd9Sstevel@tonic-gate 		if (unused == 0)
556*7c478bd9Sstevel@tonic-gate 			return (frlist);
557*7c478bd9Sstevel@tonic-gate 
558*7c478bd9Sstevel@tonic-gate 		if (frlist->next == NULL)
559*7c478bd9Sstevel@tonic-gate 			break;
560*7c478bd9Sstevel@tonic-gate 
561*7c478bd9Sstevel@tonic-gate 		if (below < best_below) {
562*7c478bd9Sstevel@tonic-gate 			best_below = below;
563*7c478bd9Sstevel@tonic-gate 			fnd_below = frlist;
564*7c478bd9Sstevel@tonic-gate 		}
565*7c478bd9Sstevel@tonic-gate 
566*7c478bd9Sstevel@tonic-gate 		if (above < best_above) {
567*7c478bd9Sstevel@tonic-gate 			best_above = above;
568*7c478bd9Sstevel@tonic-gate 			fnd_above = frlist;
569*7c478bd9Sstevel@tonic-gate 		}
570*7c478bd9Sstevel@tonic-gate 
571*7c478bd9Sstevel@tonic-gate 		if (unused < best_unused) {
572*7c478bd9Sstevel@tonic-gate 			best_unused = unused;
573*7c478bd9Sstevel@tonic-gate 			fnd_unused = frlist;
574*7c478bd9Sstevel@tonic-gate 		}
575*7c478bd9Sstevel@tonic-gate 	}
576*7c478bd9Sstevel@tonic-gate 
577*7c478bd9Sstevel@tonic-gate 	if (best_below == 0)
578*7c478bd9Sstevel@tonic-gate 		return (fnd_below);
579*7c478bd9Sstevel@tonic-gate 	if (best_above == 0)
580*7c478bd9Sstevel@tonic-gate 		return (fnd_above);
581*7c478bd9Sstevel@tonic-gate 	if (best_unused < ULONG_MAX)
582*7c478bd9Sstevel@tonic-gate 		return (fnd_unused);
583*7c478bd9Sstevel@tonic-gate 
584*7c478bd9Sstevel@tonic-gate 	return (frlist);
585*7c478bd9Sstevel@tonic-gate }
586*7c478bd9Sstevel@tonic-gate 
587*7c478bd9Sstevel@tonic-gate /*
588*7c478bd9Sstevel@tonic-gate  * Nucleus data memory allocator.
589*7c478bd9Sstevel@tonic-gate  * The granularity of the allocator is ecache_alignsize.
590*7c478bd9Sstevel@tonic-gate  * See also comment for ndata_alloc_init().
591*7c478bd9Sstevel@tonic-gate  */
592*7c478bd9Sstevel@tonic-gate void *
593*7c478bd9Sstevel@tonic-gate ndata_alloc(struct memlist *ndata, size_t wanted, size_t alignment)
594*7c478bd9Sstevel@tonic-gate {
595*7c478bd9Sstevel@tonic-gate 	struct memlist *found;
596*7c478bd9Sstevel@tonic-gate 	struct memlist *fnd_above;
597*7c478bd9Sstevel@tonic-gate 	uintptr_t base;
598*7c478bd9Sstevel@tonic-gate 	uintptr_t end;
599*7c478bd9Sstevel@tonic-gate 	size_t below;
600*7c478bd9Sstevel@tonic-gate 	size_t above;
601*7c478bd9Sstevel@tonic-gate 
602*7c478bd9Sstevel@tonic-gate 	/*
603*7c478bd9Sstevel@tonic-gate 	 * Look for the best matching buffer, avoid memory fragmentation.
604*7c478bd9Sstevel@tonic-gate 	 */
605*7c478bd9Sstevel@tonic-gate 	if ((found = ndata_select_chunk(ndata, wanted, alignment)) == NULL)
606*7c478bd9Sstevel@tonic-gate 		return (NULL);
607*7c478bd9Sstevel@tonic-gate 
608*7c478bd9Sstevel@tonic-gate 	/*
609*7c478bd9Sstevel@tonic-gate 	 * Allocate the nucleus data buffer.
610*7c478bd9Sstevel@tonic-gate 	 */
611*7c478bd9Sstevel@tonic-gate 	base = roundup(found->address, alignment);
612*7c478bd9Sstevel@tonic-gate 	end = roundup(base + wanted, ecache_alignsize);
613*7c478bd9Sstevel@tonic-gate 	ASSERT(end <= found->address + found->size);
614*7c478bd9Sstevel@tonic-gate 
615*7c478bd9Sstevel@tonic-gate 	below = base - found->address;
616*7c478bd9Sstevel@tonic-gate 	above = found->address + found->size - end;
617*7c478bd9Sstevel@tonic-gate 	ASSERT(above == 0 || (above % ecache_alignsize) == 0);
618*7c478bd9Sstevel@tonic-gate 
619*7c478bd9Sstevel@tonic-gate 	if (below >= ecache_alignsize) {
620*7c478bd9Sstevel@tonic-gate 		/*
621*7c478bd9Sstevel@tonic-gate 		 * There is free memory below the allocated memory chunk.
622*7c478bd9Sstevel@tonic-gate 		 */
623*7c478bd9Sstevel@tonic-gate 		found->size = below - below % ecache_alignsize;
624*7c478bd9Sstevel@tonic-gate 
625*7c478bd9Sstevel@tonic-gate 		if (above) {
626*7c478bd9Sstevel@tonic-gate 			fnd_above = (struct memlist *)end;
627*7c478bd9Sstevel@tonic-gate 			fnd_above->address = end;
628*7c478bd9Sstevel@tonic-gate 			fnd_above->size = above;
629*7c478bd9Sstevel@tonic-gate 
630*7c478bd9Sstevel@tonic-gate 			if ((fnd_above->next = found->next) != NULL)
631*7c478bd9Sstevel@tonic-gate 				found->next->prev = fnd_above;
632*7c478bd9Sstevel@tonic-gate 			fnd_above->prev = found;
633*7c478bd9Sstevel@tonic-gate 			found->next = fnd_above;
634*7c478bd9Sstevel@tonic-gate 		}
635*7c478bd9Sstevel@tonic-gate 
636*7c478bd9Sstevel@tonic-gate 		return ((void *)base);
637*7c478bd9Sstevel@tonic-gate 	}
638*7c478bd9Sstevel@tonic-gate 
639*7c478bd9Sstevel@tonic-gate 	if (found->prev == NULL) {
640*7c478bd9Sstevel@tonic-gate 		/*
641*7c478bd9Sstevel@tonic-gate 		 * The first chunk (ndata) is selected.
642*7c478bd9Sstevel@tonic-gate 		 */
643*7c478bd9Sstevel@tonic-gate 		ASSERT(found == ndata);
644*7c478bd9Sstevel@tonic-gate 		if (above) {
645*7c478bd9Sstevel@tonic-gate 			found->address = end;
646*7c478bd9Sstevel@tonic-gate 			found->size = above;
647*7c478bd9Sstevel@tonic-gate 		} else if (found->next != NULL) {
648*7c478bd9Sstevel@tonic-gate 			found->address = found->next->address;
649*7c478bd9Sstevel@tonic-gate 			found->size = found->next->size;
650*7c478bd9Sstevel@tonic-gate 			if ((found->next = found->next->next) != NULL)
651*7c478bd9Sstevel@tonic-gate 				found->next->prev = found;
652*7c478bd9Sstevel@tonic-gate 
653*7c478bd9Sstevel@tonic-gate 			bzero((void *)found->address, sizeof (struct memlist));
654*7c478bd9Sstevel@tonic-gate 		} else {
655*7c478bd9Sstevel@tonic-gate 			found->address = end;
656*7c478bd9Sstevel@tonic-gate 			found->size = 0;
657*7c478bd9Sstevel@tonic-gate 		}
658*7c478bd9Sstevel@tonic-gate 
659*7c478bd9Sstevel@tonic-gate 		return ((void *)base);
660*7c478bd9Sstevel@tonic-gate 	}
661*7c478bd9Sstevel@tonic-gate 
662*7c478bd9Sstevel@tonic-gate 	/*
663*7c478bd9Sstevel@tonic-gate 	 * Not the first chunk.
664*7c478bd9Sstevel@tonic-gate 	 */
665*7c478bd9Sstevel@tonic-gate 	if (above) {
666*7c478bd9Sstevel@tonic-gate 		fnd_above = (struct memlist *)end;
667*7c478bd9Sstevel@tonic-gate 		fnd_above->address = end;
668*7c478bd9Sstevel@tonic-gate 		fnd_above->size = above;
669*7c478bd9Sstevel@tonic-gate 
670*7c478bd9Sstevel@tonic-gate 		if ((fnd_above->next = found->next) != NULL)
671*7c478bd9Sstevel@tonic-gate 			fnd_above->next->prev = fnd_above;
672*7c478bd9Sstevel@tonic-gate 		fnd_above->prev = found->prev;
673*7c478bd9Sstevel@tonic-gate 		found->prev->next = fnd_above;
674*7c478bd9Sstevel@tonic-gate 
675*7c478bd9Sstevel@tonic-gate 	} else {
676*7c478bd9Sstevel@tonic-gate 		if ((found->prev->next = found->next) != NULL)
677*7c478bd9Sstevel@tonic-gate 			found->next->prev = found->prev;
678*7c478bd9Sstevel@tonic-gate 	}
679*7c478bd9Sstevel@tonic-gate 
680*7c478bd9Sstevel@tonic-gate 	bzero((void *)found->address, sizeof (struct memlist));
681*7c478bd9Sstevel@tonic-gate 
682*7c478bd9Sstevel@tonic-gate 	return ((void *)base);
683*7c478bd9Sstevel@tonic-gate }
684*7c478bd9Sstevel@tonic-gate 
685*7c478bd9Sstevel@tonic-gate /*
686*7c478bd9Sstevel@tonic-gate  * Size the kernel TSBs based upon the amount of physical
687*7c478bd9Sstevel@tonic-gate  * memory in the system.
688*7c478bd9Sstevel@tonic-gate  */
689*7c478bd9Sstevel@tonic-gate static void
690*7c478bd9Sstevel@tonic-gate calc_tsb_sizes(pgcnt_t npages)
691*7c478bd9Sstevel@tonic-gate {
692*7c478bd9Sstevel@tonic-gate 	PRM_DEBUG(npages);
693*7c478bd9Sstevel@tonic-gate 
694*7c478bd9Sstevel@tonic-gate 	if (npages <= TSB_FREEMEM_MIN) {
695*7c478bd9Sstevel@tonic-gate 		ktsb_szcode = TSB_128K_SZCODE;
696*7c478bd9Sstevel@tonic-gate 		enable_bigktsb = 0;
697*7c478bd9Sstevel@tonic-gate 	} else if (npages <= TSB_FREEMEM_LARGE / 2) {
698*7c478bd9Sstevel@tonic-gate 		ktsb_szcode = TSB_256K_SZCODE;
699*7c478bd9Sstevel@tonic-gate 		enable_bigktsb = 0;
700*7c478bd9Sstevel@tonic-gate 	} else if (npages <= TSB_FREEMEM_LARGE) {
701*7c478bd9Sstevel@tonic-gate 		ktsb_szcode = TSB_512K_SZCODE;
702*7c478bd9Sstevel@tonic-gate 		enable_bigktsb = 0;
703*7c478bd9Sstevel@tonic-gate 	} else if (npages <= TSB_FREEMEM_LARGE * 2 ||
704*7c478bd9Sstevel@tonic-gate 	    enable_bigktsb == 0) {
705*7c478bd9Sstevel@tonic-gate 		ktsb_szcode = TSB_1M_SZCODE;
706*7c478bd9Sstevel@tonic-gate 		enable_bigktsb = 0;
707*7c478bd9Sstevel@tonic-gate 	} else {
708*7c478bd9Sstevel@tonic-gate 		ktsb_szcode = highbit(npages - 1);
709*7c478bd9Sstevel@tonic-gate 		ktsb_szcode -= TSB_START_SIZE;
710*7c478bd9Sstevel@tonic-gate 		ktsb_szcode = MAX(ktsb_szcode, MIN_BIGKTSB_SZCODE);
711*7c478bd9Sstevel@tonic-gate 		ktsb_szcode = MIN(ktsb_szcode, MAX_BIGKTSB_SZCODE);
712*7c478bd9Sstevel@tonic-gate 	}
713*7c478bd9Sstevel@tonic-gate 
714*7c478bd9Sstevel@tonic-gate 	/*
715*7c478bd9Sstevel@tonic-gate 	 * We choose the TSB to hold kernel 4M mappings to have twice
716*7c478bd9Sstevel@tonic-gate 	 * the reach as the primary kernel TSB since this TSB will
717*7c478bd9Sstevel@tonic-gate 	 * potentially (currently) be shared by both mappings to all of
718*7c478bd9Sstevel@tonic-gate 	 * physical memory plus user TSBs.  Since the current
719*7c478bd9Sstevel@tonic-gate 	 * limit on primary kernel TSB size is 16MB this will top out
720*7c478bd9Sstevel@tonic-gate 	 * at 64K which we can certainly afford.
721*7c478bd9Sstevel@tonic-gate 	 */
722*7c478bd9Sstevel@tonic-gate 	ktsb4m_szcode = ktsb_szcode - (MMU_PAGESHIFT4M - MMU_PAGESHIFT) + 1;
723*7c478bd9Sstevel@tonic-gate 	if (ktsb4m_szcode < TSB_MIN_SZCODE)
724*7c478bd9Sstevel@tonic-gate 		ktsb4m_szcode = TSB_MIN_SZCODE;
725*7c478bd9Sstevel@tonic-gate 
726*7c478bd9Sstevel@tonic-gate 	ktsb_sz = TSB_BYTES(ktsb_szcode);	/* kernel 8K tsb size */
727*7c478bd9Sstevel@tonic-gate 	ktsb4m_sz = TSB_BYTES(ktsb4m_szcode);	/* kernel 4M tsb size */
728*7c478bd9Sstevel@tonic-gate }
729*7c478bd9Sstevel@tonic-gate 
730*7c478bd9Sstevel@tonic-gate /*
731*7c478bd9Sstevel@tonic-gate  * Allocate kernel TSBs from nucleus data memory.
732*7c478bd9Sstevel@tonic-gate  * The function return 0 on success and -1 on failure.
733*7c478bd9Sstevel@tonic-gate  */
734*7c478bd9Sstevel@tonic-gate int
735*7c478bd9Sstevel@tonic-gate ndata_alloc_tsbs(struct memlist *ndata, pgcnt_t npages)
736*7c478bd9Sstevel@tonic-gate {
737*7c478bd9Sstevel@tonic-gate 	/*
738*7c478bd9Sstevel@tonic-gate 	 * Size the kernel TSBs based upon the amount of physical
739*7c478bd9Sstevel@tonic-gate 	 * memory in the system.
740*7c478bd9Sstevel@tonic-gate 	 */
741*7c478bd9Sstevel@tonic-gate 	calc_tsb_sizes(npages);
742*7c478bd9Sstevel@tonic-gate 
743*7c478bd9Sstevel@tonic-gate 	/*
744*7c478bd9Sstevel@tonic-gate 	 * Allocate the 8K kernel TSB if it belongs inside the nucleus.
745*7c478bd9Sstevel@tonic-gate 	 */
746*7c478bd9Sstevel@tonic-gate 	if (enable_bigktsb == 0) {
747*7c478bd9Sstevel@tonic-gate 		if ((ktsb_base = ndata_alloc(ndata, ktsb_sz, ktsb_sz)) == NULL)
748*7c478bd9Sstevel@tonic-gate 			return (-1);
749*7c478bd9Sstevel@tonic-gate 		ASSERT(!((uintptr_t)ktsb_base & (ktsb_sz - 1)));
750*7c478bd9Sstevel@tonic-gate 
751*7c478bd9Sstevel@tonic-gate 		PRM_DEBUG(ktsb_base);
752*7c478bd9Sstevel@tonic-gate 		PRM_DEBUG(ktsb_sz);
753*7c478bd9Sstevel@tonic-gate 		PRM_DEBUG(ktsb_szcode);
754*7c478bd9Sstevel@tonic-gate 	}
755*7c478bd9Sstevel@tonic-gate 
756*7c478bd9Sstevel@tonic-gate 	/*
757*7c478bd9Sstevel@tonic-gate 	 * Next, allocate 4M kernel TSB from the nucleus since it's small.
758*7c478bd9Sstevel@tonic-gate 	 */
759*7c478bd9Sstevel@tonic-gate 	if ((ktsb4m_base = ndata_alloc(ndata, ktsb4m_sz, ktsb4m_sz)) == NULL)
760*7c478bd9Sstevel@tonic-gate 		return (-1);
761*7c478bd9Sstevel@tonic-gate 	ASSERT(!((uintptr_t)ktsb4m_base & (ktsb4m_sz - 1)));
762*7c478bd9Sstevel@tonic-gate 
763*7c478bd9Sstevel@tonic-gate 	PRM_DEBUG(ktsb4m_base);
764*7c478bd9Sstevel@tonic-gate 	PRM_DEBUG(ktsb4m_sz);
765*7c478bd9Sstevel@tonic-gate 	PRM_DEBUG(ktsb4m_szcode);
766*7c478bd9Sstevel@tonic-gate 
767*7c478bd9Sstevel@tonic-gate 	return (0);
768*7c478bd9Sstevel@tonic-gate }
769*7c478bd9Sstevel@tonic-gate 
770*7c478bd9Sstevel@tonic-gate /*
771*7c478bd9Sstevel@tonic-gate  * Allocate hat structs from the nucleus data memory.
772*7c478bd9Sstevel@tonic-gate  */
773*7c478bd9Sstevel@tonic-gate int
774*7c478bd9Sstevel@tonic-gate ndata_alloc_hat(struct memlist *ndata, pgcnt_t npages, pgcnt_t kpm_npages)
775*7c478bd9Sstevel@tonic-gate {
776*7c478bd9Sstevel@tonic-gate 	size_t 	ctx_sz;
777*7c478bd9Sstevel@tonic-gate 	size_t	mml_alloc_sz;
778*7c478bd9Sstevel@tonic-gate 	size_t	cb_alloc_sz;
779*7c478bd9Sstevel@tonic-gate 	int	max_nucuhme_buckets = MAX_NUCUHME_BUCKETS;
780*7c478bd9Sstevel@tonic-gate 	int	max_nuckhme_buckets = MAX_NUCKHME_BUCKETS;
781*7c478bd9Sstevel@tonic-gate 	ulong_t hme_buckets;
782*7c478bd9Sstevel@tonic-gate 
783*7c478bd9Sstevel@tonic-gate 	if (enable_bigktsb) {
784*7c478bd9Sstevel@tonic-gate 		ASSERT((max_nucuhme_buckets + max_nuckhme_buckets) *
785*7c478bd9Sstevel@tonic-gate 		    sizeof (struct hmehash_bucket) <=
786*7c478bd9Sstevel@tonic-gate 			TSB_BYTES(TSB_1M_SZCODE));
787*7c478bd9Sstevel@tonic-gate 
788*7c478bd9Sstevel@tonic-gate 		max_nucuhme_buckets *= 2;
789*7c478bd9Sstevel@tonic-gate 		max_nuckhme_buckets *= 2;
790*7c478bd9Sstevel@tonic-gate 	}
791*7c478bd9Sstevel@tonic-gate 
792*7c478bd9Sstevel@tonic-gate 	/*
793*7c478bd9Sstevel@tonic-gate 	 * Allocate ctx structures
794*7c478bd9Sstevel@tonic-gate 	 *
795*7c478bd9Sstevel@tonic-gate 	 * based on v_proc to calculate how many ctx structures
796*7c478bd9Sstevel@tonic-gate 	 * is not possible;
797*7c478bd9Sstevel@tonic-gate 	 * use whatever module_setup() assigned to nctxs
798*7c478bd9Sstevel@tonic-gate 	 */
799*7c478bd9Sstevel@tonic-gate 	PRM_DEBUG(nctxs);
800*7c478bd9Sstevel@tonic-gate 	ctx_sz = nctxs * sizeof (struct ctx);
801*7c478bd9Sstevel@tonic-gate 	if ((ctxs = ndata_alloc(ndata, ctx_sz, sizeof (struct ctx))) == NULL)
802*7c478bd9Sstevel@tonic-gate 		return (-1);
803*7c478bd9Sstevel@tonic-gate 
804*7c478bd9Sstevel@tonic-gate 	PRM_DEBUG(ctxs);
805*7c478bd9Sstevel@tonic-gate 
806*7c478bd9Sstevel@tonic-gate 	/*
807*7c478bd9Sstevel@tonic-gate 	 * The number of buckets in the hme hash tables
808*7c478bd9Sstevel@tonic-gate 	 * is a power of 2 such that the average hash chain length is
809*7c478bd9Sstevel@tonic-gate 	 * HMENT_HASHAVELEN.  The number of buckets for the user hash is
810*7c478bd9Sstevel@tonic-gate 	 * a function of physical memory and a predefined overmapping factor.
811*7c478bd9Sstevel@tonic-gate 	 * The number of buckets for the kernel hash is a function of
812*7c478bd9Sstevel@tonic-gate 	 * physical memory only.
813*7c478bd9Sstevel@tonic-gate 	 */
814*7c478bd9Sstevel@tonic-gate 	hme_buckets = (npages * HMEHASH_FACTOR) /
815*7c478bd9Sstevel@tonic-gate 		(HMENT_HASHAVELEN * (HMEBLK_SPAN(TTE8K) >> MMU_PAGESHIFT));
816*7c478bd9Sstevel@tonic-gate 
817*7c478bd9Sstevel@tonic-gate 	uhmehash_num = (int)MIN(hme_buckets, MAX_UHME_BUCKETS);
818*7c478bd9Sstevel@tonic-gate 
819*7c478bd9Sstevel@tonic-gate 	if (uhmehash_num > USER_BUCKETS_THRESHOLD) {
820*7c478bd9Sstevel@tonic-gate 		/*
821*7c478bd9Sstevel@tonic-gate 		 * if uhmehash_num is not power of 2 round it down to the
822*7c478bd9Sstevel@tonic-gate 		 *  next power of 2.
823*7c478bd9Sstevel@tonic-gate 		 */
824*7c478bd9Sstevel@tonic-gate 		uint_t align = 1 << (highbit(uhmehash_num - 1) - 1);
825*7c478bd9Sstevel@tonic-gate 		uhmehash_num = P2ALIGN(uhmehash_num, align);
826*7c478bd9Sstevel@tonic-gate 	} else
827*7c478bd9Sstevel@tonic-gate 		uhmehash_num = 1 << highbit(uhmehash_num - 1);
828*7c478bd9Sstevel@tonic-gate 
829*7c478bd9Sstevel@tonic-gate 	hme_buckets = npages / (HMEBLK_SPAN(TTE8K) >> MMU_PAGESHIFT);
830*7c478bd9Sstevel@tonic-gate 	khmehash_num = (int)MIN(hme_buckets, MAX_KHME_BUCKETS);
831*7c478bd9Sstevel@tonic-gate 	khmehash_num = 1 << highbit(khmehash_num - 1);
832*7c478bd9Sstevel@tonic-gate 	khmehash_num = MAX(khmehash_num, MIN_KHME_BUCKETS);
833*7c478bd9Sstevel@tonic-gate 
834*7c478bd9Sstevel@tonic-gate 	if ((khmehash_num > max_nuckhme_buckets) ||
835*7c478bd9Sstevel@tonic-gate 		(uhmehash_num > max_nucuhme_buckets)) {
836*7c478bd9Sstevel@tonic-gate 		khme_hash = NULL;
837*7c478bd9Sstevel@tonic-gate 		uhme_hash = NULL;
838*7c478bd9Sstevel@tonic-gate 	} else {
839*7c478bd9Sstevel@tonic-gate 		size_t hmehash_sz = (uhmehash_num + khmehash_num) *
840*7c478bd9Sstevel@tonic-gate 		    sizeof (struct hmehash_bucket);
841*7c478bd9Sstevel@tonic-gate 
842*7c478bd9Sstevel@tonic-gate 		if ((khme_hash = ndata_alloc(ndata, hmehash_sz,
843*7c478bd9Sstevel@tonic-gate 		    ecache_alignsize)) != NULL)
844*7c478bd9Sstevel@tonic-gate 			uhme_hash = &khme_hash[khmehash_num];
845*7c478bd9Sstevel@tonic-gate 		else
846*7c478bd9Sstevel@tonic-gate 			uhme_hash = NULL;
847*7c478bd9Sstevel@tonic-gate 
848*7c478bd9Sstevel@tonic-gate 		PRM_DEBUG(hmehash_sz);
849*7c478bd9Sstevel@tonic-gate 	}
850*7c478bd9Sstevel@tonic-gate 
851*7c478bd9Sstevel@tonic-gate 	PRM_DEBUG(khme_hash);
852*7c478bd9Sstevel@tonic-gate 	PRM_DEBUG(khmehash_num);
853*7c478bd9Sstevel@tonic-gate 	PRM_DEBUG(uhme_hash);
854*7c478bd9Sstevel@tonic-gate 	PRM_DEBUG(uhmehash_num);
855*7c478bd9Sstevel@tonic-gate 
856*7c478bd9Sstevel@tonic-gate 	/*
857*7c478bd9Sstevel@tonic-gate 	 * For the page mapping list mutex array we allocate one mutex
858*7c478bd9Sstevel@tonic-gate 	 * for every 128 pages (1 MB) with a minimum of 64 entries and
859*7c478bd9Sstevel@tonic-gate 	 * a maximum of 8K entries. For the initial computation npages
860*7c478bd9Sstevel@tonic-gate 	 * is rounded up (ie. 1 << highbit(npages * 1.5 / 128))
861*7c478bd9Sstevel@tonic-gate 	 *
862*7c478bd9Sstevel@tonic-gate 	 * mml_shift is roughly log2(mml_table_sz) + 3 for MLIST_HASH
863*7c478bd9Sstevel@tonic-gate 	 *
864*7c478bd9Sstevel@tonic-gate 	 * It is not required that this be allocated from the nucleus,
865*7c478bd9Sstevel@tonic-gate 	 * but it is desirable.  So we first allocate from the nucleus
866*7c478bd9Sstevel@tonic-gate 	 * everything that must be there.  Having done so, if mml_table
867*7c478bd9Sstevel@tonic-gate 	 * will fit within what remains of the nucleus then it will be
868*7c478bd9Sstevel@tonic-gate 	 * allocated here.  If not, set mml_table to NULL, which will cause
869*7c478bd9Sstevel@tonic-gate 	 * startup_memlist() to BOP_ALLOC() space for it after our return...
870*7c478bd9Sstevel@tonic-gate 	 */
871*7c478bd9Sstevel@tonic-gate 	mml_table_sz = 1 << highbit((npages * 3) / 256);
872*7c478bd9Sstevel@tonic-gate 	if (mml_table_sz < 64)
873*7c478bd9Sstevel@tonic-gate 		mml_table_sz = 64;
874*7c478bd9Sstevel@tonic-gate 	else if (mml_table_sz > 8192)
875*7c478bd9Sstevel@tonic-gate 		mml_table_sz = 8192;
876*7c478bd9Sstevel@tonic-gate 	mml_shift = highbit(mml_table_sz) + 3;
877*7c478bd9Sstevel@tonic-gate 
878*7c478bd9Sstevel@tonic-gate 	PRM_DEBUG(mml_table_sz);
879*7c478bd9Sstevel@tonic-gate 	PRM_DEBUG(mml_shift);
880*7c478bd9Sstevel@tonic-gate 
881*7c478bd9Sstevel@tonic-gate 	mml_alloc_sz = mml_table_sz * sizeof (kmutex_t);
882*7c478bd9Sstevel@tonic-gate 
883*7c478bd9Sstevel@tonic-gate 	mml_table = ndata_alloc(ndata, mml_alloc_sz, ecache_alignsize);
884*7c478bd9Sstevel@tonic-gate 
885*7c478bd9Sstevel@tonic-gate 	PRM_DEBUG(mml_table);
886*7c478bd9Sstevel@tonic-gate 
887*7c478bd9Sstevel@tonic-gate 	cb_alloc_sz = sfmmu_max_cb_id * sizeof (struct sfmmu_callback);
888*7c478bd9Sstevel@tonic-gate 	PRM_DEBUG(cb_alloc_sz);
889*7c478bd9Sstevel@tonic-gate 	sfmmu_cb_table = ndata_alloc(ndata, cb_alloc_sz, ecache_alignsize);
890*7c478bd9Sstevel@tonic-gate 	PRM_DEBUG(sfmmu_cb_table);
891*7c478bd9Sstevel@tonic-gate 
892*7c478bd9Sstevel@tonic-gate 	/*
893*7c478bd9Sstevel@tonic-gate 	 * For the kpm_page mutex array we allocate one mutex every 16
894*7c478bd9Sstevel@tonic-gate 	 * kpm pages (64MB). In smallpage mode we allocate one mutex
895*7c478bd9Sstevel@tonic-gate 	 * every 8K pages. The minimum is set to 64 entries and the
896*7c478bd9Sstevel@tonic-gate 	 * maximum to 8K entries.
897*7c478bd9Sstevel@tonic-gate 	 *
898*7c478bd9Sstevel@tonic-gate 	 * It is not required that this be allocated from the nucleus,
899*7c478bd9Sstevel@tonic-gate 	 * but it is desirable.  So we first allocate from the nucleus
900*7c478bd9Sstevel@tonic-gate 	 * everything that must be there.  Having done so, if kpmp_table
901*7c478bd9Sstevel@tonic-gate 	 * or kpmp_stable will fit within what remains of the nucleus
902*7c478bd9Sstevel@tonic-gate 	 * then it will be allocated here.  If not, startup_memlist()
903*7c478bd9Sstevel@tonic-gate 	 * will use BOP_ALLOC() space for it after our return...
904*7c478bd9Sstevel@tonic-gate 	 */
905*7c478bd9Sstevel@tonic-gate 	if (kpm_enable) {
906*7c478bd9Sstevel@tonic-gate 		size_t	kpmp_alloc_sz;
907*7c478bd9Sstevel@tonic-gate 
908*7c478bd9Sstevel@tonic-gate 		if (kpm_smallpages == 0) {
909*7c478bd9Sstevel@tonic-gate 			kpmp_shift = highbit(sizeof (kpm_page_t)) - 1;
910*7c478bd9Sstevel@tonic-gate 			kpmp_table_sz = 1 << highbit(kpm_npages / 16);
911*7c478bd9Sstevel@tonic-gate 			kpmp_table_sz = (kpmp_table_sz < 64) ? 64 :
912*7c478bd9Sstevel@tonic-gate 			    ((kpmp_table_sz > 8192) ? 8192 : kpmp_table_sz);
913*7c478bd9Sstevel@tonic-gate 			kpmp_alloc_sz = kpmp_table_sz * sizeof (kpm_hlk_t);
914*7c478bd9Sstevel@tonic-gate 
915*7c478bd9Sstevel@tonic-gate 			kpmp_table = ndata_alloc(ndata, kpmp_alloc_sz,
916*7c478bd9Sstevel@tonic-gate 			    ecache_alignsize);
917*7c478bd9Sstevel@tonic-gate 
918*7c478bd9Sstevel@tonic-gate 			PRM_DEBUG(kpmp_table);
919*7c478bd9Sstevel@tonic-gate 			PRM_DEBUG(kpmp_table_sz);
920*7c478bd9Sstevel@tonic-gate 
921*7c478bd9Sstevel@tonic-gate 			kpmp_stable_sz = 0;
922*7c478bd9Sstevel@tonic-gate 			kpmp_stable = NULL;
923*7c478bd9Sstevel@tonic-gate 		} else {
924*7c478bd9Sstevel@tonic-gate 			ASSERT(kpm_pgsz == PAGESIZE);
925*7c478bd9Sstevel@tonic-gate 			kpmp_shift = highbit(sizeof (kpm_shlk_t)) + 1;
926*7c478bd9Sstevel@tonic-gate 			kpmp_stable_sz = 1 << highbit(kpm_npages / 8192);
927*7c478bd9Sstevel@tonic-gate 			kpmp_stable_sz = (kpmp_stable_sz < 64) ? 64 :
928*7c478bd9Sstevel@tonic-gate 			    ((kpmp_stable_sz > 8192) ? 8192 : kpmp_stable_sz);
929*7c478bd9Sstevel@tonic-gate 			kpmp_alloc_sz = kpmp_stable_sz * sizeof (kpm_shlk_t);
930*7c478bd9Sstevel@tonic-gate 
931*7c478bd9Sstevel@tonic-gate 			kpmp_stable = ndata_alloc(ndata, kpmp_alloc_sz,
932*7c478bd9Sstevel@tonic-gate 			    ecache_alignsize);
933*7c478bd9Sstevel@tonic-gate 
934*7c478bd9Sstevel@tonic-gate 			PRM_DEBUG(kpmp_stable);
935*7c478bd9Sstevel@tonic-gate 			PRM_DEBUG(kpmp_stable_sz);
936*7c478bd9Sstevel@tonic-gate 
937*7c478bd9Sstevel@tonic-gate 			kpmp_table_sz = 0;
938*7c478bd9Sstevel@tonic-gate 			kpmp_table = NULL;
939*7c478bd9Sstevel@tonic-gate 		}
940*7c478bd9Sstevel@tonic-gate 		PRM_DEBUG(kpmp_shift);
941*7c478bd9Sstevel@tonic-gate 	}
942*7c478bd9Sstevel@tonic-gate 
943*7c478bd9Sstevel@tonic-gate 	return (0);
944*7c478bd9Sstevel@tonic-gate }
945*7c478bd9Sstevel@tonic-gate 
946*7c478bd9Sstevel@tonic-gate caddr_t
947*7c478bd9Sstevel@tonic-gate alloc_hme_buckets(caddr_t base, int pagesize)
948*7c478bd9Sstevel@tonic-gate {
949*7c478bd9Sstevel@tonic-gate 	size_t hmehash_sz = (uhmehash_num + khmehash_num) *
950*7c478bd9Sstevel@tonic-gate 	sizeof (struct hmehash_bucket);
951*7c478bd9Sstevel@tonic-gate 
952*7c478bd9Sstevel@tonic-gate 	ASSERT(khme_hash == NULL);
953*7c478bd9Sstevel@tonic-gate 	ASSERT(uhme_hash == NULL);
954*7c478bd9Sstevel@tonic-gate 
955*7c478bd9Sstevel@tonic-gate 	/* If no pagesize specified, use default MMU pagesize */
956*7c478bd9Sstevel@tonic-gate 	if (!pagesize)
957*7c478bd9Sstevel@tonic-gate 		pagesize = MMU_PAGESIZE;
958*7c478bd9Sstevel@tonic-gate 
959*7c478bd9Sstevel@tonic-gate 	/*
960*7c478bd9Sstevel@tonic-gate 	 * If we start aligned and ask for a multiple of a pagesize, and OBP
961*7c478bd9Sstevel@tonic-gate 	 * supports large pages, we will then use mappings of the largest size
962*7c478bd9Sstevel@tonic-gate 	 * possible for the BOP_ALLOC, possibly saving us tens of thousands of
963*7c478bd9Sstevel@tonic-gate 	 * TLB miss-induced traversals of the TSBs and/or the HME hashes...
964*7c478bd9Sstevel@tonic-gate 	 */
965*7c478bd9Sstevel@tonic-gate 	base = (caddr_t)roundup((uintptr_t)base, pagesize);
966*7c478bd9Sstevel@tonic-gate 	hmehash_sz = roundup(hmehash_sz, pagesize);
967*7c478bd9Sstevel@tonic-gate 
968*7c478bd9Sstevel@tonic-gate 	khme_hash = (struct hmehash_bucket *)BOP_ALLOC(bootops, base,
969*7c478bd9Sstevel@tonic-gate 		hmehash_sz, pagesize);
970*7c478bd9Sstevel@tonic-gate 
971*7c478bd9Sstevel@tonic-gate 	if ((caddr_t)khme_hash != base)
972*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "Cannot bop_alloc hme hash buckets.");
973*7c478bd9Sstevel@tonic-gate 
974*7c478bd9Sstevel@tonic-gate 	uhme_hash = (struct hmehash_bucket *)((caddr_t)khme_hash +
975*7c478bd9Sstevel@tonic-gate 		khmehash_num * sizeof (struct hmehash_bucket));
976*7c478bd9Sstevel@tonic-gate 	base += hmehash_sz;
977*7c478bd9Sstevel@tonic-gate 	return (base);
978*7c478bd9Sstevel@tonic-gate }
979*7c478bd9Sstevel@tonic-gate 
980*7c478bd9Sstevel@tonic-gate /*
981*7c478bd9Sstevel@tonic-gate  * This function bop allocs the kernel TSB.
982*7c478bd9Sstevel@tonic-gate  */
983*7c478bd9Sstevel@tonic-gate caddr_t
984*7c478bd9Sstevel@tonic-gate sfmmu_ktsb_alloc(caddr_t tsbbase)
985*7c478bd9Sstevel@tonic-gate {
986*7c478bd9Sstevel@tonic-gate 	caddr_t vaddr;
987*7c478bd9Sstevel@tonic-gate 
988*7c478bd9Sstevel@tonic-gate 	if (enable_bigktsb) {
989*7c478bd9Sstevel@tonic-gate 		ktsb_base = (caddr_t)roundup((uintptr_t)tsbbase, ktsb_sz);
990*7c478bd9Sstevel@tonic-gate 		vaddr = (caddr_t)BOP_ALLOC(bootops, ktsb_base, ktsb_sz,
991*7c478bd9Sstevel@tonic-gate 		    ktsb_sz);
992*7c478bd9Sstevel@tonic-gate 		if (vaddr != ktsb_base)
993*7c478bd9Sstevel@tonic-gate 			cmn_err(CE_PANIC, "sfmmu_ktsb_alloc: can't alloc"
994*7c478bd9Sstevel@tonic-gate 			    " bigktsb");
995*7c478bd9Sstevel@tonic-gate 		ktsb_base = vaddr;
996*7c478bd9Sstevel@tonic-gate 		tsbbase = ktsb_base + ktsb_sz;
997*7c478bd9Sstevel@tonic-gate 		PRM_DEBUG(ktsb_base);
998*7c478bd9Sstevel@tonic-gate 		PRM_DEBUG(tsbbase);
999*7c478bd9Sstevel@tonic-gate 	}
1000*7c478bd9Sstevel@tonic-gate 	return (tsbbase);
1001*7c478bd9Sstevel@tonic-gate }
1002*7c478bd9Sstevel@tonic-gate 
1003*7c478bd9Sstevel@tonic-gate /*
1004*7c478bd9Sstevel@tonic-gate  * Moves code assembled outside of the trap table into the trap
1005*7c478bd9Sstevel@tonic-gate  * table taking care to relocate relative branches to code outside
1006*7c478bd9Sstevel@tonic-gate  * of the trap handler.
1007*7c478bd9Sstevel@tonic-gate  */
1008*7c478bd9Sstevel@tonic-gate static void
1009*7c478bd9Sstevel@tonic-gate sfmmu_reloc_trap_handler(void *tablep, void *start, size_t count)
1010*7c478bd9Sstevel@tonic-gate {
1011*7c478bd9Sstevel@tonic-gate 	size_t i;
1012*7c478bd9Sstevel@tonic-gate 	uint32_t *src;
1013*7c478bd9Sstevel@tonic-gate 	uint32_t *dst;
1014*7c478bd9Sstevel@tonic-gate 	uint32_t inst;
1015*7c478bd9Sstevel@tonic-gate 	int op, op2;
1016*7c478bd9Sstevel@tonic-gate 	int32_t offset;
1017*7c478bd9Sstevel@tonic-gate 	int disp;
1018*7c478bd9Sstevel@tonic-gate 
1019*7c478bd9Sstevel@tonic-gate 	src = start;
1020*7c478bd9Sstevel@tonic-gate 	dst = tablep;
1021*7c478bd9Sstevel@tonic-gate 	offset = src - dst;
1022*7c478bd9Sstevel@tonic-gate 	for (src = start, i = 0; i < count; i++, src++, dst++) {
1023*7c478bd9Sstevel@tonic-gate 		inst = *dst = *src;
1024*7c478bd9Sstevel@tonic-gate 		op = (inst >> 30) & 0x2;
1025*7c478bd9Sstevel@tonic-gate 		if (op == 1) {
1026*7c478bd9Sstevel@tonic-gate 			/* call */
1027*7c478bd9Sstevel@tonic-gate 			disp = ((int32_t)inst << 2) >> 2; /* sign-extend */
1028*7c478bd9Sstevel@tonic-gate 			if (disp + i >= 0 && disp + i < count)
1029*7c478bd9Sstevel@tonic-gate 				continue;
1030*7c478bd9Sstevel@tonic-gate 			disp += offset;
1031*7c478bd9Sstevel@tonic-gate 			inst = 0x40000000u | (disp & 0x3fffffffu);
1032*7c478bd9Sstevel@tonic-gate 			*dst = inst;
1033*7c478bd9Sstevel@tonic-gate 		} else if (op == 0) {
1034*7c478bd9Sstevel@tonic-gate 			/* branch or sethi */
1035*7c478bd9Sstevel@tonic-gate 			op2 = (inst >> 22) & 0x7;
1036*7c478bd9Sstevel@tonic-gate 
1037*7c478bd9Sstevel@tonic-gate 			switch (op2) {
1038*7c478bd9Sstevel@tonic-gate 			case 0x3: /* BPr */
1039*7c478bd9Sstevel@tonic-gate 				disp = (((inst >> 20) & 0x3) << 14) |
1040*7c478bd9Sstevel@tonic-gate 				    (inst & 0x3fff);
1041*7c478bd9Sstevel@tonic-gate 				disp = (disp << 16) >> 16; /* sign-extend */
1042*7c478bd9Sstevel@tonic-gate 				if (disp + i >= 0 && disp + i < count)
1043*7c478bd9Sstevel@tonic-gate 					continue;
1044*7c478bd9Sstevel@tonic-gate 				disp += offset;
1045*7c478bd9Sstevel@tonic-gate 				if (((disp << 16) >> 16) != disp)
1046*7c478bd9Sstevel@tonic-gate 					cmn_err(CE_PANIC, "bad reloc");
1047*7c478bd9Sstevel@tonic-gate 				inst &= ~0x303fff;
1048*7c478bd9Sstevel@tonic-gate 				inst |= (disp & 0x3fff);
1049*7c478bd9Sstevel@tonic-gate 				inst |= (disp & 0xc000) << 6;
1050*7c478bd9Sstevel@tonic-gate 				break;
1051*7c478bd9Sstevel@tonic-gate 
1052*7c478bd9Sstevel@tonic-gate 			case 0x2: /* Bicc */
1053*7c478bd9Sstevel@tonic-gate 				disp = ((int32_t)inst << 10) >> 10;
1054*7c478bd9Sstevel@tonic-gate 				if (disp + i >= 0 && disp + i < count)
1055*7c478bd9Sstevel@tonic-gate 					continue;
1056*7c478bd9Sstevel@tonic-gate 				disp += offset;
1057*7c478bd9Sstevel@tonic-gate 				if (((disp << 10) >> 10) != disp)
1058*7c478bd9Sstevel@tonic-gate 					cmn_err(CE_PANIC, "bad reloc");
1059*7c478bd9Sstevel@tonic-gate 				inst &= ~0x3fffff;
1060*7c478bd9Sstevel@tonic-gate 				inst |= (disp & 0x3fffff);
1061*7c478bd9Sstevel@tonic-gate 				break;
1062*7c478bd9Sstevel@tonic-gate 
1063*7c478bd9Sstevel@tonic-gate 			case 0x1: /* Bpcc */
1064*7c478bd9Sstevel@tonic-gate 				disp = ((int32_t)inst << 13) >> 13;
1065*7c478bd9Sstevel@tonic-gate 				if (disp + i >= 0 && disp + i < count)
1066*7c478bd9Sstevel@tonic-gate 					continue;
1067*7c478bd9Sstevel@tonic-gate 				disp += offset;
1068*7c478bd9Sstevel@tonic-gate 				if (((disp << 13) >> 13) != disp)
1069*7c478bd9Sstevel@tonic-gate 					cmn_err(CE_PANIC, "bad reloc");
1070*7c478bd9Sstevel@tonic-gate 				inst &= ~0x7ffff;
1071*7c478bd9Sstevel@tonic-gate 				inst |= (disp & 0x7ffffu);
1072*7c478bd9Sstevel@tonic-gate 				break;
1073*7c478bd9Sstevel@tonic-gate 			}
1074*7c478bd9Sstevel@tonic-gate 			*dst = inst;
1075*7c478bd9Sstevel@tonic-gate 		}
1076*7c478bd9Sstevel@tonic-gate 	}
1077*7c478bd9Sstevel@tonic-gate 	flush_instr_mem(tablep, count * sizeof (uint32_t));
1078*7c478bd9Sstevel@tonic-gate }
1079*7c478bd9Sstevel@tonic-gate 
1080*7c478bd9Sstevel@tonic-gate /*
1081*7c478bd9Sstevel@tonic-gate  * Routine to allocate a large page to use in the TSB caches.
1082*7c478bd9Sstevel@tonic-gate  */
1083*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1084*7c478bd9Sstevel@tonic-gate static page_t *
1085*7c478bd9Sstevel@tonic-gate sfmmu_tsb_page_create(void *addr, size_t size, int vmflag, void *arg)
1086*7c478bd9Sstevel@tonic-gate {
1087*7c478bd9Sstevel@tonic-gate 	int pgflags;
1088*7c478bd9Sstevel@tonic-gate 
1089*7c478bd9Sstevel@tonic-gate 	pgflags = PG_EXCL;
1090*7c478bd9Sstevel@tonic-gate 	if ((vmflag & VM_NOSLEEP) == 0)
1091*7c478bd9Sstevel@tonic-gate 		pgflags |= PG_WAIT;
1092*7c478bd9Sstevel@tonic-gate 	if (vmflag & VM_PANIC)
1093*7c478bd9Sstevel@tonic-gate 		pgflags |= PG_PANIC;
1094*7c478bd9Sstevel@tonic-gate 	if (vmflag & VM_PUSHPAGE)
1095*7c478bd9Sstevel@tonic-gate 		pgflags |= PG_PUSHPAGE;
1096*7c478bd9Sstevel@tonic-gate 
1097*7c478bd9Sstevel@tonic-gate 	return (page_create_va_large(&kvp, (u_offset_t)(uintptr_t)addr, size,
1098*7c478bd9Sstevel@tonic-gate 	    pgflags, &kvseg, addr, arg));
1099*7c478bd9Sstevel@tonic-gate }
1100*7c478bd9Sstevel@tonic-gate 
1101*7c478bd9Sstevel@tonic-gate /*
1102*7c478bd9Sstevel@tonic-gate  * Allocate a large page to back the virtual address range
1103*7c478bd9Sstevel@tonic-gate  * [addr, addr + size).  If addr is NULL, allocate the virtual address
1104*7c478bd9Sstevel@tonic-gate  * space as well.
1105*7c478bd9Sstevel@tonic-gate  */
1106*7c478bd9Sstevel@tonic-gate static void *
1107*7c478bd9Sstevel@tonic-gate sfmmu_tsb_xalloc(vmem_t *vmp, void *inaddr, size_t size, int vmflag,
1108*7c478bd9Sstevel@tonic-gate     uint_t attr, page_t *(*page_create_func)(void *, size_t, int, void *),
1109*7c478bd9Sstevel@tonic-gate     void *pcarg)
1110*7c478bd9Sstevel@tonic-gate {
1111*7c478bd9Sstevel@tonic-gate 	page_t *ppl;
1112*7c478bd9Sstevel@tonic-gate 	page_t *rootpp;
1113*7c478bd9Sstevel@tonic-gate 	caddr_t addr = inaddr;
1114*7c478bd9Sstevel@tonic-gate 	pgcnt_t npages = btopr(size);
1115*7c478bd9Sstevel@tonic-gate 	page_t **ppa;
1116*7c478bd9Sstevel@tonic-gate 	int i = 0;
1117*7c478bd9Sstevel@tonic-gate 
1118*7c478bd9Sstevel@tonic-gate 	/*
1119*7c478bd9Sstevel@tonic-gate 	 * Assuming that only TSBs will call this with size > PAGESIZE
1120*7c478bd9Sstevel@tonic-gate 	 * There is no reason why this couldn't be expanded to 8k pages as
1121*7c478bd9Sstevel@tonic-gate 	 * well, or other page sizes in the future .... but for now, we
1122*7c478bd9Sstevel@tonic-gate 	 * only support fixed sized page requests.
1123*7c478bd9Sstevel@tonic-gate 	 */
1124*7c478bd9Sstevel@tonic-gate 	if ((inaddr == NULL) && ((addr = vmem_xalloc(vmp, size, size, 0, 0,
1125*7c478bd9Sstevel@tonic-gate 	    NULL, NULL, vmflag)) == NULL))
1126*7c478bd9Sstevel@tonic-gate 		return (NULL);
1127*7c478bd9Sstevel@tonic-gate 
1128*7c478bd9Sstevel@tonic-gate 	/* If we ever don't want TSB slab-sized pages, this will panic */
1129*7c478bd9Sstevel@tonic-gate 	ASSERT(((uintptr_t)addr & (tsb_slab_size - 1)) == 0);
1130*7c478bd9Sstevel@tonic-gate 
1131*7c478bd9Sstevel@tonic-gate 	if (page_resv(npages, vmflag & VM_KMFLAGS) == 0) {
1132*7c478bd9Sstevel@tonic-gate 		if (inaddr == NULL)
1133*7c478bd9Sstevel@tonic-gate 			vmem_xfree(vmp, addr, size);
1134*7c478bd9Sstevel@tonic-gate 		return (NULL);
1135*7c478bd9Sstevel@tonic-gate 	}
1136*7c478bd9Sstevel@tonic-gate 
1137*7c478bd9Sstevel@tonic-gate 	ppl = page_create_func(addr, size, vmflag, pcarg);
1138*7c478bd9Sstevel@tonic-gate 	if (ppl == NULL) {
1139*7c478bd9Sstevel@tonic-gate 		if (inaddr == NULL)
1140*7c478bd9Sstevel@tonic-gate 			vmem_xfree(vmp, addr, size);
1141*7c478bd9Sstevel@tonic-gate 		page_unresv(npages);
1142*7c478bd9Sstevel@tonic-gate 		return (NULL);
1143*7c478bd9Sstevel@tonic-gate 	}
1144*7c478bd9Sstevel@tonic-gate 
1145*7c478bd9Sstevel@tonic-gate 	rootpp = ppl;
1146*7c478bd9Sstevel@tonic-gate 	ppa = kmem_zalloc(npages * sizeof (page_t *), KM_SLEEP);
1147*7c478bd9Sstevel@tonic-gate 	while (ppl != NULL) {
1148*7c478bd9Sstevel@tonic-gate 		page_t *pp = ppl;
1149*7c478bd9Sstevel@tonic-gate 		ppa[i++] = pp;
1150*7c478bd9Sstevel@tonic-gate 		page_sub(&ppl, pp);
1151*7c478bd9Sstevel@tonic-gate 		ASSERT(page_iolock_assert(pp));
1152*7c478bd9Sstevel@tonic-gate 		page_io_unlock(pp);
1153*7c478bd9Sstevel@tonic-gate 	}
1154*7c478bd9Sstevel@tonic-gate 
1155*7c478bd9Sstevel@tonic-gate 	/*
1156*7c478bd9Sstevel@tonic-gate 	 * Load the locked entry.  It's OK to preload the entry into
1157*7c478bd9Sstevel@tonic-gate 	 * the TSB since we now support large mappings in the kernel TSB.
1158*7c478bd9Sstevel@tonic-gate 	 */
1159*7c478bd9Sstevel@tonic-gate 	hat_memload_array(kas.a_hat, (caddr_t)rootpp->p_offset, size,
1160*7c478bd9Sstevel@tonic-gate 	    ppa, (PROT_ALL & ~PROT_USER) | HAT_NOSYNC | attr, HAT_LOAD_LOCK);
1161*7c478bd9Sstevel@tonic-gate 
1162*7c478bd9Sstevel@tonic-gate 	for (--i; i >= 0; --i) {
1163*7c478bd9Sstevel@tonic-gate 		(void) page_pp_lock(ppa[i], 0, 1);
1164*7c478bd9Sstevel@tonic-gate 		page_unlock(ppa[i]);
1165*7c478bd9Sstevel@tonic-gate 	}
1166*7c478bd9Sstevel@tonic-gate 
1167*7c478bd9Sstevel@tonic-gate 	kmem_free(ppa, npages * sizeof (page_t *));
1168*7c478bd9Sstevel@tonic-gate 	return (addr);
1169*7c478bd9Sstevel@tonic-gate }
1170*7c478bd9Sstevel@tonic-gate 
1171*7c478bd9Sstevel@tonic-gate /* Called to import new spans into the TSB vmem arenas */
1172*7c478bd9Sstevel@tonic-gate void *
1173*7c478bd9Sstevel@tonic-gate sfmmu_tsb_segkmem_alloc(vmem_t *vmp, size_t size, int vmflag)
1174*7c478bd9Sstevel@tonic-gate {
1175*7c478bd9Sstevel@tonic-gate 	lgrp_id_t lgrpid = LGRP_NONE;
1176*7c478bd9Sstevel@tonic-gate 
1177*7c478bd9Sstevel@tonic-gate 	if (tsb_lgrp_affinity) {
1178*7c478bd9Sstevel@tonic-gate 		/*
1179*7c478bd9Sstevel@tonic-gate 		 * Search for the vmp->lgrpid mapping by brute force;
1180*7c478bd9Sstevel@tonic-gate 		 * some day vmp will have an lgrp, until then we have
1181*7c478bd9Sstevel@tonic-gate 		 * to do this the hard way.
1182*7c478bd9Sstevel@tonic-gate 		 */
1183*7c478bd9Sstevel@tonic-gate 		for (lgrpid = 0; lgrpid < NLGRPS_MAX &&
1184*7c478bd9Sstevel@tonic-gate 		    vmp != kmem_tsb_default_arena[lgrpid]; lgrpid++);
1185*7c478bd9Sstevel@tonic-gate 		if (lgrpid == NLGRPS_MAX)
1186*7c478bd9Sstevel@tonic-gate 			lgrpid = LGRP_NONE;
1187*7c478bd9Sstevel@tonic-gate 	}
1188*7c478bd9Sstevel@tonic-gate 
1189*7c478bd9Sstevel@tonic-gate 	return (sfmmu_tsb_xalloc(vmp, NULL, size, vmflag, 0,
1190*7c478bd9Sstevel@tonic-gate 	    sfmmu_tsb_page_create, lgrpid != LGRP_NONE? &lgrpid : NULL));
1191*7c478bd9Sstevel@tonic-gate }
1192*7c478bd9Sstevel@tonic-gate 
1193*7c478bd9Sstevel@tonic-gate /* Called to free spans from the TSB vmem arenas */
1194*7c478bd9Sstevel@tonic-gate void
1195*7c478bd9Sstevel@tonic-gate sfmmu_tsb_segkmem_free(vmem_t *vmp, void *inaddr, size_t size)
1196*7c478bd9Sstevel@tonic-gate {
1197*7c478bd9Sstevel@tonic-gate 	page_t *pp;
1198*7c478bd9Sstevel@tonic-gate 	caddr_t addr = inaddr;
1199*7c478bd9Sstevel@tonic-gate 	caddr_t eaddr;
1200*7c478bd9Sstevel@tonic-gate 	pgcnt_t npages = btopr(size);
1201*7c478bd9Sstevel@tonic-gate 	pgcnt_t pgs_left = npages;
1202*7c478bd9Sstevel@tonic-gate 	page_t *rootpp = NULL;
1203*7c478bd9Sstevel@tonic-gate 
1204*7c478bd9Sstevel@tonic-gate 	ASSERT(((uintptr_t)addr & (tsb_slab_size - 1)) == 0);
1205*7c478bd9Sstevel@tonic-gate 
1206*7c478bd9Sstevel@tonic-gate 	hat_unload(kas.a_hat, addr, size, HAT_UNLOAD_UNLOCK);
1207*7c478bd9Sstevel@tonic-gate 
1208*7c478bd9Sstevel@tonic-gate 	for (eaddr = addr + size; addr < eaddr; addr += PAGESIZE) {
1209*7c478bd9Sstevel@tonic-gate 		pp = page_lookup(&kvp, (u_offset_t)(uintptr_t)addr, SE_EXCL);
1210*7c478bd9Sstevel@tonic-gate 		if (pp == NULL)
1211*7c478bd9Sstevel@tonic-gate 			panic("sfmmu_tsb_segkmem_free: page not found");
1212*7c478bd9Sstevel@tonic-gate 
1213*7c478bd9Sstevel@tonic-gate 		ASSERT(PAGE_EXCL(pp));
1214*7c478bd9Sstevel@tonic-gate 		page_pp_unlock(pp, 0, 1);
1215*7c478bd9Sstevel@tonic-gate 
1216*7c478bd9Sstevel@tonic-gate 		if (rootpp == NULL)
1217*7c478bd9Sstevel@tonic-gate 			rootpp = pp;
1218*7c478bd9Sstevel@tonic-gate 		if (--pgs_left == 0) {
1219*7c478bd9Sstevel@tonic-gate 			/*
1220*7c478bd9Sstevel@tonic-gate 			 * similar logic to segspt_free_pages, but we know we
1221*7c478bd9Sstevel@tonic-gate 			 * have one large page.
1222*7c478bd9Sstevel@tonic-gate 			 */
1223*7c478bd9Sstevel@tonic-gate 			page_destroy_pages(rootpp);
1224*7c478bd9Sstevel@tonic-gate 		}
1225*7c478bd9Sstevel@tonic-gate 	}
1226*7c478bd9Sstevel@tonic-gate 	page_unresv(npages);
1227*7c478bd9Sstevel@tonic-gate 
1228*7c478bd9Sstevel@tonic-gate 	if (vmp != NULL)
1229*7c478bd9Sstevel@tonic-gate 		vmem_xfree(vmp, inaddr, size);
1230*7c478bd9Sstevel@tonic-gate }
1231