xref: /onnv-gate/usr/src/stand/lib/sa/memlist.c (revision 11474:857f9db4ef05)
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*11474SJonathan.Adams@Sun.COM  * Common Development and Distribution License (the "License").
6*11474SJonathan.Adams@Sun.COM  * 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*11474SJonathan.Adams@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/param.h>
280Sstevel@tonic-gate #include <sys/sysmacros.h>
290Sstevel@tonic-gate #include <sys/machparam.h>
300Sstevel@tonic-gate #include <sys/promif.h>
310Sstevel@tonic-gate #include <sys/bootconf.h>
320Sstevel@tonic-gate #include <sys/salib.h>
330Sstevel@tonic-gate 
340Sstevel@tonic-gate extern caddr_t memlistpage;
350Sstevel@tonic-gate 
360Sstevel@tonic-gate /* Always pts to the next free link in the headtable */
370Sstevel@tonic-gate /* i.e. it is always memlistpage+tableoffset */
380Sstevel@tonic-gate caddr_t tablep = NULL;
390Sstevel@tonic-gate static int table_freespace;
400Sstevel@tonic-gate 
410Sstevel@tonic-gate /*
420Sstevel@tonic-gate  *	Function prototypes
430Sstevel@tonic-gate  */
440Sstevel@tonic-gate extern void 		reset_alloc(void);
450Sstevel@tonic-gate 
460Sstevel@tonic-gate void
print_memlist(struct memlist * av)470Sstevel@tonic-gate print_memlist(struct memlist *av)
480Sstevel@tonic-gate {
490Sstevel@tonic-gate 	struct memlist *p = av;
500Sstevel@tonic-gate 
510Sstevel@tonic-gate 	while (p != NULL) {
520Sstevel@tonic-gate 		printf("addr = 0x%x:0x%x, size = 0x%x:0x%x\n",
53*11474SJonathan.Adams@Sun.COM 		    (uint_t)(p->ml_address >> 32), (uint_t)p->ml_address,
54*11474SJonathan.Adams@Sun.COM 		    (uint_t)(p->ml_size >> 32), (uint_t)p->ml_size);
55*11474SJonathan.Adams@Sun.COM 		p = p->ml_next;
560Sstevel@tonic-gate 	}
570Sstevel@tonic-gate 
580Sstevel@tonic-gate }
590Sstevel@tonic-gate 
600Sstevel@tonic-gate /* allocate room for n bytes, return 8-byte aligned address */
610Sstevel@tonic-gate void *
getlink(uint_t n)620Sstevel@tonic-gate getlink(uint_t n)
630Sstevel@tonic-gate {
640Sstevel@tonic-gate 	void *p;
650Sstevel@tonic-gate 	extern int pagesize;
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	if (memlistpage == NULL)
680Sstevel@tonic-gate 		reset_alloc();
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 	if (tablep == NULL) {
710Sstevel@tonic-gate 		/*
720Sstevel@tonic-gate 		 * Took the following 2 lines out of above test for
730Sstevel@tonic-gate 		 * memlistpage == null so we can initialize table_freespace
740Sstevel@tonic-gate 		 */
750Sstevel@tonic-gate 		table_freespace = pagesize - sizeof (struct bsys_mem);
760Sstevel@tonic-gate 		tablep = memlistpage + sizeof (struct bsys_mem);
770Sstevel@tonic-gate 		tablep = (caddr_t)roundup((uintptr_t)tablep, 8);
780Sstevel@tonic-gate 	}
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	if (n == 0)
810Sstevel@tonic-gate 		return (NULL);
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	n = roundup(n, 8);
840Sstevel@tonic-gate 	p = tablep;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	table_freespace -= n;
870Sstevel@tonic-gate 	tablep += n;
880Sstevel@tonic-gate 	if (table_freespace <= 0) {
890Sstevel@tonic-gate 		char buf[80];
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 		(void) sprintf(buf,
920Sstevel@tonic-gate 		    "Boot getlink(): no memlist space (need %d)\n", n);
930Sstevel@tonic-gate 		prom_panic(buf);
940Sstevel@tonic-gate 	}
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	return (p);
970Sstevel@tonic-gate }
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate /*
1010Sstevel@tonic-gate  * This is the number of memlist structures allocated in one shot. kept
1020Sstevel@tonic-gate  * to small number to reduce wastage of memory, it should not be too small
1030Sstevel@tonic-gate  * to slow down boot.
1040Sstevel@tonic-gate  */
1050Sstevel@tonic-gate #define		ALLOC_SZ	5
1060Sstevel@tonic-gate static struct memlist *free_memlist_ptr = NULL;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate /*
1090Sstevel@tonic-gate  * Free memory lists are maintained as simple single linked lists.
1100Sstevel@tonic-gate  * get_memlist_struct returns a memlist structure without initializing
1110Sstevel@tonic-gate  * any of the fields.  It is caller's responsibility to do that.
1120Sstevel@tonic-gate  */
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate struct memlist *
get_memlist_struct(void)1150Sstevel@tonic-gate get_memlist_struct(void)
1160Sstevel@tonic-gate {
1170Sstevel@tonic-gate 	struct memlist *ptr;
1180Sstevel@tonic-gate 	int i;
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	if (free_memlist_ptr == NULL) {
1210Sstevel@tonic-gate 		ptr = free_memlist_ptr = getlink(ALLOC_SZ *
1220Sstevel@tonic-gate 		    sizeof (struct memlist));
1230Sstevel@tonic-gate 		bzero(free_memlist_ptr, (ALLOC_SZ * sizeof (struct memlist)));
1240Sstevel@tonic-gate 		for (i = 0; i < ALLOC_SZ; i++)
125*11474SJonathan.Adams@Sun.COM 			ptr[i].ml_next = &ptr[i+1];
126*11474SJonathan.Adams@Sun.COM 		ptr[i-1].ml_next = NULL;
1270Sstevel@tonic-gate 	}
1280Sstevel@tonic-gate 	ptr = free_memlist_ptr;
129*11474SJonathan.Adams@Sun.COM 	free_memlist_ptr = ptr->ml_next;
1300Sstevel@tonic-gate 	return (ptr);
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate /*
1340Sstevel@tonic-gate  * Return memlist structure to free list.
1350Sstevel@tonic-gate  */
1360Sstevel@tonic-gate void
add_to_freelist(struct memlist * ptr)1370Sstevel@tonic-gate add_to_freelist(struct memlist *ptr)
1380Sstevel@tonic-gate {
1390Sstevel@tonic-gate 	struct memlist *tmp;
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	if (free_memlist_ptr == NULL) {
1420Sstevel@tonic-gate 		free_memlist_ptr = ptr;
1430Sstevel@tonic-gate 	} else {
144*11474SJonathan.Adams@Sun.COM 		for (tmp = free_memlist_ptr; tmp->ml_next; tmp = tmp->ml_next)
1450Sstevel@tonic-gate 			;
146*11474SJonathan.Adams@Sun.COM 		tmp->ml_next = ptr;
1470Sstevel@tonic-gate 	}
1480Sstevel@tonic-gate }
149