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