xref: /onnv-gate/usr/src/psm/stand/boot/sparc/common/sun4x_standalloc.c (revision 5648:161f8007cab9)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*5648Ssetje  * Common Development and Distribution License (the "License").
6*5648Ssetje  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*5648Ssetje  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/saio.h>
300Sstevel@tonic-gate #include <sys/sysmacros.h>
310Sstevel@tonic-gate #include <sys/promif.h>
320Sstevel@tonic-gate #include <sys/bootconf.h>
330Sstevel@tonic-gate #include <sys/salib.h>
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #define	NIL		0
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #ifdef DEBUG
380Sstevel@tonic-gate static int	resalloc_debug = 1;
390Sstevel@tonic-gate #else /* DEBUG */
400Sstevel@tonic-gate static int	resalloc_debug = 0;
410Sstevel@tonic-gate #endif /* DEBUG */
420Sstevel@tonic-gate #define	dprintf	if (resalloc_debug) printf
430Sstevel@tonic-gate 
440Sstevel@tonic-gate extern struct memlist	*vfreelistp, *pfreelistp;
450Sstevel@tonic-gate extern	void		reset_alloc(void);
460Sstevel@tonic-gate extern	void		alloc_segment(caddr_t);
470Sstevel@tonic-gate 
480Sstevel@tonic-gate caddr_t		memlistpage;
490Sstevel@tonic-gate caddr_t		le_page;
500Sstevel@tonic-gate caddr_t		ie_page;
510Sstevel@tonic-gate caddr_t 	scratchmemp;
520Sstevel@tonic-gate extern int	pagesize;
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #define	N_FREELIST	20	/* keep the largest 20 free regions */
550Sstevel@tonic-gate static size_t	free_size[N_FREELIST];
560Sstevel@tonic-gate static caddr_t	free_addr[N_FREELIST];
570Sstevel@tonic-gate 
580Sstevel@tonic-gate /*
590Sstevel@tonic-gate  * OBP sets up a 1:1 mapping of virtual to physical in the range 8KB-10MB.  The
600Sstevel@tonic-gate  * standalone is free to use any or all of this during its lifetime.
610Sstevel@tonic-gate  * Unfortunately, some platforms (Serengeti and LW8) can't use the full range.
620Sstevel@tonic-gate  * See 4799331 for more details.  Limited platforms can use up to
630Sstevel@tonic-gate  * MAPPEDMEM_MINTOP; everyone else can use up to MAPPEDMEM_FULLTOP.
640Sstevel@tonic-gate  * resalloc_init makes the determination as to how much the machine being booted
650Sstevel@tonic-gate  * can use.
660Sstevel@tonic-gate  *
670Sstevel@tonic-gate  * But wait!  There's more!  resalloc handles three types of allocations: Two
680Sstevel@tonic-gate  * flavors of RES_BOOTSCRATCH (RES_BOOTSCRATCH and RES_BOOTSCRATCH_NOFAIL), and
690Sstevel@tonic-gate  * one of RES_CHILDVIRT.  RES_CHILDVIRT is handled by prom_alloc, and is boring.
700Sstevel@tonic-gate  * We handle RES_BOOTSCRATCH allocations ourselves using the portion of the 1:1
710Sstevel@tonic-gate  * range not consumed by boot.  The unconsumed range is subdivided into two
720Sstevel@tonic-gate  * portions - the general area from top_resvmem to top_bootmem and the reserved
730Sstevel@tonic-gate  * area from above memlistpage to top_resvmem.  Both RES_BOOTSCRATCH flavors are
740Sstevel@tonic-gate  * satisfied by the general area until said area is exhausted, at which point
750Sstevel@tonic-gate  * RES_BOOTSCRATCH allocations return failure.  RES_BOOTSCRATCH_NOFAIL
760Sstevel@tonic-gate  * allocations can't fail, so we'll try to satisfy them from the reserved area
770Sstevel@tonic-gate  * if the general area is full.  If we still can't satisfy the nofail
780Sstevel@tonic-gate  * allocation, we'll call prom_panic.
790Sstevel@tonic-gate  *
800Sstevel@tonic-gate  * This whole boot memory allocation thing needs some serious rethinking.
810Sstevel@tonic-gate  *
820Sstevel@tonic-gate  * Memory layout:
830Sstevel@tonic-gate  *
840Sstevel@tonic-gate  *	|-------| top_bootmem
850Sstevel@tonic-gate  *	|	| } MAPPEDMEM_FULLTOP (only on non-serengeti, lw8)
860Sstevel@tonic-gate  *	|	| } MAPPEDMEM_MINTOP
870Sstevel@tonic-gate  *	|-------| top_resvmem/scratchmemp
880Sstevel@tonic-gate  *	|	| } MAPPEDMEM_RESERVE
890Sstevel@tonic-gate  *	|-------| scratchresvp
900Sstevel@tonic-gate  *	|	| } one page
910Sstevel@tonic-gate  *	|-------| memlistpage (at roundup(_end, pagesize))
920Sstevel@tonic-gate  *	|-------| _end
930Sstevel@tonic-gate  *	| boot  |
940Sstevel@tonic-gate  *	:	:
950Sstevel@tonic-gate  *
960Sstevel@tonic-gate  */
970Sstevel@tonic-gate 
980Sstevel@tonic-gate #define	MAPPEDMEM_RESERVE	(512*1024)	/* reserved for NOFAIL allocs */
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate #define	MAPPEDMEM_MINTOP	(caddr_t)(6*1024*1024)
1010Sstevel@tonic-gate #define	MAPPEDMEM_FULLTOP	(caddr_t)(10*1024*1024)
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate static caddr_t top_bootmem = MAPPEDMEM_MINTOP;
1040Sstevel@tonic-gate static caddr_t top_resvmem, scratchresvp;
1050Sstevel@tonic-gate 
106*5648Ssetje /*
107*5648Ssetje  * with newboot, boot goes away when it launches the client,
108*5648Ssetje  * so we can safely extend bootmem on sg, and give it back
109*5648Ssetje  * before we die.
110*5648Ssetje  */
111*5648Ssetje int is_sg;
112*5648Ssetje caddr_t sg_addr;
113*5648Ssetje size_t sg_len;
114*5648Ssetje 
1150Sstevel@tonic-gate static int
impl_name(char * buf,size_t bufsz)1160Sstevel@tonic-gate impl_name(char *buf, size_t bufsz)
1170Sstevel@tonic-gate {
118789Sahrens 	pnode_t n = prom_rootnode();
1190Sstevel@tonic-gate 	size_t len = prom_getproplen(n, "name");
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	if (len == 0 || len >= bufsz)
1220Sstevel@tonic-gate 		return (-1);
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	(void) prom_getprop(n, "name", buf);
1250Sstevel@tonic-gate 	buf[len] = '\0';
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	return (0);
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate static caddr_t
vpage_from_freelist(size_t bytes)1310Sstevel@tonic-gate vpage_from_freelist(size_t bytes)
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate 	caddr_t v;
1340Sstevel@tonic-gate 	int i;
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	/* find first region which fits */
1370Sstevel@tonic-gate 	for (i = 0; i < N_FREELIST && free_size[i] < bytes; i++)
1380Sstevel@tonic-gate 		continue;
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	if (i == N_FREELIST) {
1410Sstevel@tonic-gate 		dprintf("boot: failed to allocate %lu bytes from scratch "
1420Sstevel@tonic-gate 		    "memory\n", bytes);
1430Sstevel@tonic-gate 		return (NULL);
1440Sstevel@tonic-gate 	}
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	v = free_addr[i];
1470Sstevel@tonic-gate 	free_addr[i] += bytes;
1480Sstevel@tonic-gate 	free_size[i] -= bytes;
1490Sstevel@tonic-gate 	dprintf("reuse freed temp scratch:  bytes = %lu at %p\n", bytes,
1500Sstevel@tonic-gate 	    (void *)v);
1510Sstevel@tonic-gate 	return (v);
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate /*
1550Sstevel@tonic-gate  *	This routine will find the next PAGESIZE chunk in the
1560Sstevel@tonic-gate  *	low MAPPEDMEM_MINTOP.  It is analogous to valloc(). It is only for boot
1570Sstevel@tonic-gate  *	scratch memory, because child scratch memory goes up in
1580Sstevel@tonic-gate  *	the the high memory.  We just need to verify that the
1590Sstevel@tonic-gate  *	pages are on the list.  The calling routine will actually
1600Sstevel@tonic-gate  *	remove them.
1610Sstevel@tonic-gate  */
1620Sstevel@tonic-gate static caddr_t
get_low_vpage(size_t numpages,enum RESOURCES type)1630Sstevel@tonic-gate get_low_vpage(size_t numpages, enum RESOURCES type)
1640Sstevel@tonic-gate {
1650Sstevel@tonic-gate 	size_t bytes;
1660Sstevel@tonic-gate 	caddr_t v;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	if (!numpages)
1690Sstevel@tonic-gate 		return (0);
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	/* We know the page is mapped because the 1st MAPPEDMEM_MINTOP is 1:1 */
1720Sstevel@tonic-gate 	bytes = numpages * pagesize;
1730Sstevel@tonic-gate 	if (scratchmemp + bytes <= top_bootmem) {
1740Sstevel@tonic-gate 		v = scratchmemp;
1750Sstevel@tonic-gate 		scratchmemp += bytes;
1760Sstevel@tonic-gate 		return (v);
1770Sstevel@tonic-gate 	}
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	/*
1800Sstevel@tonic-gate 	 * If we run out of scratch memory, look in the freelist
1810Sstevel@tonic-gate 	 */
1820Sstevel@tonic-gate 	if ((v = vpage_from_freelist(bytes)) != NULL)
1830Sstevel@tonic-gate 		return (v);
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	/*
1860Sstevel@tonic-gate 	 * Try really hard for allocations that can't fail.  Look in the area
1870Sstevel@tonic-gate 	 * that we've reserved for them.
1880Sstevel@tonic-gate 	 */
1890Sstevel@tonic-gate 	if (type == RES_BOOTSCRATCH_NOFAIL) {
1900Sstevel@tonic-gate 		if (scratchresvp + bytes <= top_resvmem) {
1910Sstevel@tonic-gate 			v = scratchresvp;
1920Sstevel@tonic-gate 			scratchresvp += bytes;
1930Sstevel@tonic-gate 			dprintf("using %lu bytes of reserved mem (%lu left)\n",
1940Sstevel@tonic-gate 			    bytes, top_resvmem - scratchresvp);
1950Sstevel@tonic-gate 			return (v);
1960Sstevel@tonic-gate 		} else {
1970Sstevel@tonic-gate 			printf("boot: failed to allocate %lu bytes from "
1980Sstevel@tonic-gate 			    "reserved scratch memory\n", bytes);
1990Sstevel@tonic-gate 			prom_panic("boot: scratch memory overflow.\n");
2000Sstevel@tonic-gate 		}
2010Sstevel@tonic-gate 	}
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	return (NULL);
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate void
resalloc_init(void)2070Sstevel@tonic-gate resalloc_init(void)
2080Sstevel@tonic-gate {
2090Sstevel@tonic-gate 	char iarch[128];
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	if (impl_name(iarch, sizeof (iarch)) < 0) {
2120Sstevel@tonic-gate 		dprintf("boot: resalloc_init: failed to read iarch\n");
2130Sstevel@tonic-gate 		return;
2140Sstevel@tonic-gate 	}
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	dprintf("boot: resalloc_init: got iarch %s\n", iarch);
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	/*
2190Sstevel@tonic-gate 	 * Some versions of SG/LW8 firmware can actually handle the entire 10MB,
2200Sstevel@tonic-gate 	 * but we don't have the ability to check for the firmware version here.
2210Sstevel@tonic-gate 	 */
2220Sstevel@tonic-gate 	if (strcmp(iarch, "SUNW,Sun-Fire") == 0 ||
223*5648Ssetje 	    strcmp(iarch, "SUNW,Netra-T12") == 0) {
224*5648Ssetje 		is_sg = 1;
225*5648Ssetje 		sg_addr = MAPPEDMEM_MINTOP;
226*5648Ssetje 		sg_len = MAPPEDMEM_FULLTOP - MAPPEDMEM_MINTOP;
227*5648Ssetje 		if (prom_alloc(sg_addr, sg_len, 1) != sg_addr)
228*5648Ssetje 			prom_panic("can't extend sg bootmem");
229*5648Ssetje 	}
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	top_bootmem = MAPPEDMEM_FULLTOP;
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	dprintf("boot: resalloc_init: boosted top_bootmem to %p\n",
2340Sstevel@tonic-gate 	    (void *)top_bootmem);
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate caddr_t
resalloc(enum RESOURCES type,size_t bytes,caddr_t virthint,int align)2380Sstevel@tonic-gate resalloc(enum RESOURCES type, size_t bytes, caddr_t virthint, int align)
2390Sstevel@tonic-gate {
2400Sstevel@tonic-gate 	caddr_t	vaddr;
2410Sstevel@tonic-gate 	long pmap = 0;
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	if (memlistpage == (caddr_t)0)
2440Sstevel@tonic-gate 		reset_alloc();
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	if (bytes == 0)
2470Sstevel@tonic-gate 		return ((caddr_t)0);
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	/* extend request to fill a page */
2500Sstevel@tonic-gate 	bytes = roundup(bytes, pagesize);
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	dprintf("resalloc:  bytes = %lu\n", bytes);
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	switch (type) {
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	/*
2570Sstevel@tonic-gate 	 * even V2 PROMs never bother to indicate whether the
2580Sstevel@tonic-gate 	 * first MAPPEDMEM_MINTOP is taken or not.  So we do it all here.
2590Sstevel@tonic-gate 	 * Smart PROM or no smart PROM.
2600Sstevel@tonic-gate 	 */
2610Sstevel@tonic-gate 	case RES_BOOTSCRATCH:
2620Sstevel@tonic-gate 	case RES_BOOTSCRATCH_NOFAIL:
2630Sstevel@tonic-gate 		vaddr = get_low_vpage((bytes/pagesize), type);
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 		if (resalloc_debug) {
2660Sstevel@tonic-gate 			dprintf("vaddr = %p, paddr = %lx\n", (void *)vaddr,
2670Sstevel@tonic-gate 			    ptob(pmap));
2680Sstevel@tonic-gate 			print_memlist(vfreelistp);
2690Sstevel@tonic-gate 			print_memlist(pfreelistp);
2700Sstevel@tonic-gate 		}
2710Sstevel@tonic-gate 		return (vaddr);
2720Sstevel@tonic-gate 		/*NOTREACHED*/
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	case RES_CHILDVIRT:
2750Sstevel@tonic-gate 		vaddr = (caddr_t)prom_alloc(virthint, bytes, align);
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 		if (vaddr == (caddr_t)virthint)
2780Sstevel@tonic-gate 			return (vaddr);
2790Sstevel@tonic-gate 		printf("Alloc of 0x%lx bytes at 0x%p refused.\n",
280*5648Ssetje 		    bytes, (void *)virthint);
2810Sstevel@tonic-gate 		return ((caddr_t)0);
2820Sstevel@tonic-gate 		/*NOTREACHED*/
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	default:
2850Sstevel@tonic-gate 		printf("Bad resurce type\n");
2860Sstevel@tonic-gate 		return ((caddr_t)0);
2870Sstevel@tonic-gate 	}
2880Sstevel@tonic-gate }
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate #ifdef	lint
2910Sstevel@tonic-gate static char _end[1];	/* defined by the linker! */
2920Sstevel@tonic-gate #endif	/* lint */
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate void
reset_alloc(void)2950Sstevel@tonic-gate reset_alloc(void)
2960Sstevel@tonic-gate {
2970Sstevel@tonic-gate 	extern char _end[];
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	/* Cannot be called multiple times */
3000Sstevel@tonic-gate 	if (memlistpage != (caddr_t)0)
3010Sstevel@tonic-gate 		return;
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	/*
3040Sstevel@tonic-gate 	 *  Due to kernel history and ease of programming, we
3050Sstevel@tonic-gate 	 *  want to keep everything private to /boot BELOW MAPPEDMEM_MINTOP.
3060Sstevel@tonic-gate 	 *  In this way, the kernel can just snarf it all when
3070Sstevel@tonic-gate 	 *  when it is ready, and not worry about snarfing lists.
3080Sstevel@tonic-gate 	 */
3090Sstevel@tonic-gate 	memlistpage = (caddr_t)roundup((uintptr_t)_end, pagesize);
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	/*
3120Sstevel@tonic-gate 	 *  This next is for scratch memory only
3130Sstevel@tonic-gate 	 *  We only need 1 page in memlistpage for now
3140Sstevel@tonic-gate 	 */
3150Sstevel@tonic-gate 	scratchresvp = (caddr_t)(memlistpage + pagesize);
3160Sstevel@tonic-gate 	scratchmemp = top_resvmem = scratchresvp + MAPPEDMEM_RESERVE;
3170Sstevel@tonic-gate 	le_page = (caddr_t)(scratchmemp + pagesize);
3180Sstevel@tonic-gate 	ie_page = (caddr_t)(le_page + pagesize);
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	bzero(memlistpage, pagesize);
3210Sstevel@tonic-gate 	bzero(scratchmemp, pagesize);
3220Sstevel@tonic-gate 	dprintf("memlistpage = %p\n", (void *)memlistpage);
3230Sstevel@tonic-gate 	dprintf("le_page = %p\n", (void *)le_page);
3240Sstevel@tonic-gate }
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate void
resfree(enum RESOURCES type,caddr_t virtaddr,size_t size)3270Sstevel@tonic-gate resfree(enum RESOURCES type, caddr_t virtaddr, size_t size)
3280Sstevel@tonic-gate {
3290Sstevel@tonic-gate 	int i;
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	/* make sure this is boot scratch memory */
3320Sstevel@tonic-gate 	switch (type) {
3330Sstevel@tonic-gate 	case RES_BOOTSCRATCH:
3340Sstevel@tonic-gate 		if (virtaddr + size > top_bootmem)
3350Sstevel@tonic-gate 			return;
3360Sstevel@tonic-gate 		break;
3370Sstevel@tonic-gate 	default:
3380Sstevel@tonic-gate 		return;
3390Sstevel@tonic-gate 	}
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	/*
3420Sstevel@tonic-gate 	 * Add this to the end of the free list
3430Sstevel@tonic-gate 	 * NOTE: This relies on the fact that KRTLD calls BOP_FREE
3440Sstevel@tonic-gate 	 *	from largest to smallest chunks.
3450Sstevel@tonic-gate 	 */
3460Sstevel@tonic-gate 	for (i = 0; i < N_FREELIST && free_size[i]; i++)
3470Sstevel@tonic-gate 		;
3480Sstevel@tonic-gate 	if (i == N_FREELIST)
3490Sstevel@tonic-gate 		return;
3500Sstevel@tonic-gate 	free_size[i] = size;
3510Sstevel@tonic-gate 	free_addr[i] = virtaddr;
3520Sstevel@tonic-gate }
353