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