xref: /illumos-gate/usr/src/boot/i386/libi386/biosmem.c (revision 22028508)
1*22028508SToomas Soome /*
2*22028508SToomas Soome  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3*22028508SToomas Soome  * All rights reserved.
4*22028508SToomas Soome  *
5*22028508SToomas Soome  * Redistribution and use in source and binary forms, with or without
6*22028508SToomas Soome  * modification, are permitted provided that the following conditions
7*22028508SToomas Soome  * are met:
8*22028508SToomas Soome  * 1. Redistributions of source code must retain the above copyright
9*22028508SToomas Soome  *    notice, this list of conditions and the following disclaimer.
10*22028508SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
11*22028508SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
12*22028508SToomas Soome  *    documentation and/or other materials provided with the distribution.
13*22028508SToomas Soome  *
14*22028508SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*22028508SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*22028508SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*22028508SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*22028508SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*22028508SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*22028508SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*22028508SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*22028508SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*22028508SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*22028508SToomas Soome  * SUCH DAMAGE.
25*22028508SToomas Soome  */
26*22028508SToomas Soome 
27*22028508SToomas Soome #include <sys/cdefs.h>
28*22028508SToomas Soome 
29*22028508SToomas Soome /*
30*22028508SToomas Soome  * Obtain memory configuration information from the BIOS
31*22028508SToomas Soome  */
32*22028508SToomas Soome #include <stand.h>
33*22028508SToomas Soome #include <machine/pc/bios.h>
34*22028508SToomas Soome #include "bootstrap.h"
35*22028508SToomas Soome #include "libi386.h"
36*22028508SToomas Soome #include "btxv86.h"
37*22028508SToomas Soome #include "smbios.h"
38*22028508SToomas Soome 
39*22028508SToomas Soome vm_offset_t	memtop, memtop_copyin, high_heap_base;
40*22028508SToomas Soome uint32_t	bios_basemem, bios_extmem, high_heap_size;
41*22028508SToomas Soome 
42*22028508SToomas Soome static struct bios_smap_xattr smap;
43*22028508SToomas Soome 
44*22028508SToomas Soome /*
45*22028508SToomas Soome  * Used to track which method was used to set BIOS memory
46*22028508SToomas Soome  * regions.
47*22028508SToomas Soome  */
48*22028508SToomas Soome static uint8_t b_bios_probed;
49*22028508SToomas Soome #define	B_BASEMEM_E820	0x1
50*22028508SToomas Soome #define	B_BASEMEM_12	0x2
51*22028508SToomas Soome #define	B_EXTMEM_E820	0x4
52*22028508SToomas Soome #define	B_EXTMEM_E801	0x8
53*22028508SToomas Soome #define	B_EXTMEM_8800	0x10
54*22028508SToomas Soome 
55*22028508SToomas Soome /*
56*22028508SToomas Soome  * The minimum amount of memory to reserve in bios_extmem for the heap.
57*22028508SToomas Soome  */
58*22028508SToomas Soome #define	HEAP_MIN	(64 * 1024 * 1024)
59*22028508SToomas Soome 
60*22028508SToomas Soome /*
61*22028508SToomas Soome  * Products in this list need quirks to detect
62*22028508SToomas Soome  * memory correctly. You need both maker and product as
63*22028508SToomas Soome  * reported by smbios.
64*22028508SToomas Soome  */
65*22028508SToomas Soome /* e820 might not return useful extended memory */
66*22028508SToomas Soome #define	BQ_DISTRUST_E820_EXTMEM	0x1
67*22028508SToomas Soome struct bios_getmem_quirks {
68*22028508SToomas Soome 	const char *bios_vendor;
69*22028508SToomas Soome 	const char *maker;
70*22028508SToomas Soome 	const char *product;
71*22028508SToomas Soome 	int quirk;
72*22028508SToomas Soome };
73*22028508SToomas Soome 
74*22028508SToomas Soome static struct bios_getmem_quirks quirks[] = {
75*22028508SToomas Soome 	{"coreboot", "Acer", "Peppy", BQ_DISTRUST_E820_EXTMEM},
76*22028508SToomas Soome 	{NULL, NULL, NULL, 0}
77*22028508SToomas Soome };
78*22028508SToomas Soome 
79*22028508SToomas Soome static int
bios_getquirks(void)80*22028508SToomas Soome bios_getquirks(void)
81*22028508SToomas Soome {
82*22028508SToomas Soome 	int i;
83*22028508SToomas Soome 
84*22028508SToomas Soome 	for (i = 0; quirks[i].quirk != 0; ++i) {
85*22028508SToomas Soome 		if (smbios_match(quirks[i].bios_vendor, quirks[i].maker,
86*22028508SToomas Soome 		    quirks[i].product))
87*22028508SToomas Soome 			return (quirks[i].quirk);
88*22028508SToomas Soome 	}
89*22028508SToomas Soome 
90*22028508SToomas Soome 	return (0);
91*22028508SToomas Soome }
92*22028508SToomas Soome 
93*22028508SToomas Soome void
bios_getmem(void)94*22028508SToomas Soome bios_getmem(void)
95*22028508SToomas Soome {
96*22028508SToomas Soome 	uint64_t size;
97*22028508SToomas Soome 
98*22028508SToomas Soome 	/* Parse system memory map */
99*22028508SToomas Soome 	v86.ebx = 0;
100*22028508SToomas Soome 	do {
101*22028508SToomas Soome 		v86.ctl = V86_FLAGS;
102*22028508SToomas Soome 		v86.addr = 0x15;		/* int 0x15 function 0xe820 */
103*22028508SToomas Soome 		v86.eax = 0xe820;
104*22028508SToomas Soome 		v86.ecx = sizeof (struct bios_smap_xattr);
105*22028508SToomas Soome 		v86.edx = SMAP_SIG;
106*22028508SToomas Soome 		v86.es = VTOPSEG(&smap);
107*22028508SToomas Soome 		v86.edi = VTOPOFF(&smap);
108*22028508SToomas Soome 		v86int();
109*22028508SToomas Soome 		if ((V86_CY(v86.efl)) || (v86.eax != SMAP_SIG))
110*22028508SToomas Soome 			break;
111*22028508SToomas Soome 		/* look for a low-memory segment that's large enough */
112*22028508SToomas Soome 		if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base == 0) &&
113*22028508SToomas Soome 		    (smap.length >= (512 * 1024))) {
114*22028508SToomas Soome 			bios_basemem = smap.length;
115*22028508SToomas Soome 			b_bios_probed |= B_BASEMEM_E820;
116*22028508SToomas Soome 		}
117*22028508SToomas Soome 
118*22028508SToomas Soome 		/* look for the first segment in 'extended' memory */
119*22028508SToomas Soome 		if ((smap.type == SMAP_TYPE_MEMORY) &&
120*22028508SToomas Soome 		    (smap.base == 0x100000) &&
121*22028508SToomas Soome 		    !(bios_getquirks() & BQ_DISTRUST_E820_EXTMEM)) {
122*22028508SToomas Soome 			bios_extmem = smap.length;
123*22028508SToomas Soome 			b_bios_probed |= B_EXTMEM_E820;
124*22028508SToomas Soome 		}
125*22028508SToomas Soome 
126*22028508SToomas Soome 		/*
127*22028508SToomas Soome 		 * Look for the highest segment in 'extended' memory beyond
128*22028508SToomas Soome 		 * 1MB but below 4GB.
129*22028508SToomas Soome 		 */
130*22028508SToomas Soome 		if ((smap.type == SMAP_TYPE_MEMORY) &&
131*22028508SToomas Soome 		    (smap.base > 0x100000) &&
132*22028508SToomas Soome 		    (smap.base < 0x100000000ull)) {
133*22028508SToomas Soome 			size = smap.length;
134*22028508SToomas Soome 
135*22028508SToomas Soome 			/*
136*22028508SToomas Soome 			 * If this segment crosses the 4GB boundary,
137*22028508SToomas Soome 			 * truncate it.
138*22028508SToomas Soome 			 */
139*22028508SToomas Soome 			if (smap.base + size > 0x100000000ull)
140*22028508SToomas Soome 				size = 0x100000000ull - smap.base;
141*22028508SToomas Soome 
142*22028508SToomas Soome 			/*
143*22028508SToomas Soome 			 * To make maximum space for the kernel and the modules,
144*22028508SToomas Soome 			 * set heap to use highest HEAP_MIN bytes below 4GB.
145*22028508SToomas Soome 			 */
146*22028508SToomas Soome 			if (high_heap_base < smap.base && size >= HEAP_MIN) {
147*22028508SToomas Soome 				high_heap_base = smap.base + size - HEAP_MIN;
148*22028508SToomas Soome 				high_heap_size = HEAP_MIN;
149*22028508SToomas Soome 			}
150*22028508SToomas Soome 		}
151*22028508SToomas Soome 	} while (v86.ebx != 0);
152*22028508SToomas Soome 
153*22028508SToomas Soome 	/* Fall back to the old compatibility function for base memory */
154*22028508SToomas Soome 	if (bios_basemem == 0) {
155*22028508SToomas Soome 		v86.ctl = 0;
156*22028508SToomas Soome 		v86.addr = 0x12;		/* int 0x12 */
157*22028508SToomas Soome 		v86int();
158*22028508SToomas Soome 
159*22028508SToomas Soome 		bios_basemem = (v86.eax & 0xffff) * 1024;
160*22028508SToomas Soome 		b_bios_probed |= B_BASEMEM_12;
161*22028508SToomas Soome 	}
162*22028508SToomas Soome 
163*22028508SToomas Soome 	/*
164*22028508SToomas Soome 	 * Fall back through several compatibility functions for extended
165*22028508SToomas Soome 	 * memory.
166*22028508SToomas Soome 	 */
167*22028508SToomas Soome 	if (bios_extmem == 0) {
168*22028508SToomas Soome 		v86.ctl = V86_FLAGS;
169*22028508SToomas Soome 		v86.addr = 0x15;		/* int 0x15 function 0xe801 */
170*22028508SToomas Soome 		v86.eax = 0xe801;
171*22028508SToomas Soome 		v86int();
172*22028508SToomas Soome 		if (!(V86_CY(v86.efl))) {
173*22028508SToomas Soome 			/*
174*22028508SToomas Soome 			 * Clear high_heap; it may end up overlapping
175*22028508SToomas Soome 			 * with the segment we're determining here.
176*22028508SToomas Soome 			 * Let the default "steal stuff from top of
177*22028508SToomas Soome 			 * bios_extmem" code below pick up on it.
178*22028508SToomas Soome 			 */
179*22028508SToomas Soome 			high_heap_size = 0;
180*22028508SToomas Soome 			high_heap_base = 0;
181*22028508SToomas Soome 
182*22028508SToomas Soome 			/*
183*22028508SToomas Soome 			 * %cx is the number of 1KiB blocks between 1..16MiB.
184*22028508SToomas Soome 			 * It can only be up to 0x3c00; if it's smaller then
185*22028508SToomas Soome 			 * there's a PC AT memory hole so we can't treat
186*22028508SToomas Soome 			 * it as contiguous.
187*22028508SToomas Soome 			 */
188*22028508SToomas Soome 			bios_extmem = (v86.ecx & 0xffff) * 1024;
189*22028508SToomas Soome 			if (bios_extmem == (1024 * 0x3c00))
190*22028508SToomas Soome 				bios_extmem += (v86.edx & 0xffff) * 64 * 1024;
191*22028508SToomas Soome 
192*22028508SToomas Soome 			/* truncate bios_extmem */
193*22028508SToomas Soome 			if (bios_extmem > 0x3ff00000)
194*22028508SToomas Soome 				bios_extmem = 0x3ff00000;
195*22028508SToomas Soome 
196*22028508SToomas Soome 			b_bios_probed |= B_EXTMEM_E801;
197*22028508SToomas Soome 		}
198*22028508SToomas Soome 	}
199*22028508SToomas Soome 	if (bios_extmem == 0) {
200*22028508SToomas Soome 		v86.ctl = 0;
201*22028508SToomas Soome 		v86.addr = 0x15;		/* int 0x15 function 0x88 */
202*22028508SToomas Soome 		v86.eax = 0x8800;
203*22028508SToomas Soome 		v86int();
204*22028508SToomas Soome 		bios_extmem = (v86.eax & 0xffff) * 1024;
205*22028508SToomas Soome 		b_bios_probed |= B_EXTMEM_8800;
206*22028508SToomas Soome 	}
207*22028508SToomas Soome 
208*22028508SToomas Soome 	/* Set memtop to actual top of memory */
209*22028508SToomas Soome 	if (high_heap_size != 0) {
210*22028508SToomas Soome 		memtop = memtop_copyin = high_heap_base;
211*22028508SToomas Soome 	} else {
212*22028508SToomas Soome 		memtop = memtop_copyin = 0x100000 + bios_extmem;
213*22028508SToomas Soome 	}
214*22028508SToomas Soome 
215*22028508SToomas Soome 	/*
216*22028508SToomas Soome 	 * If we have extended memory and did not find a suitable heap
217*22028508SToomas Soome 	 * region in the SMAP, use the last HEAP_MIN of 'extended' memory as a
218*22028508SToomas Soome 	 * high heap candidate.
219*22028508SToomas Soome 	 */
220*22028508SToomas Soome 	if (bios_extmem >= HEAP_MIN && high_heap_size < HEAP_MIN) {
221*22028508SToomas Soome 		high_heap_size = HEAP_MIN;
222*22028508SToomas Soome 		high_heap_base = memtop - HEAP_MIN;
223*22028508SToomas Soome 		memtop = memtop_copyin = high_heap_base;
224*22028508SToomas Soome 	}
225*22028508SToomas Soome }
226*22028508SToomas Soome 
227*22028508SToomas Soome static int
command_biosmem(int argc __unused,char * argv[]__unused)228*22028508SToomas Soome command_biosmem(int argc __unused, char *argv[] __unused)
229*22028508SToomas Soome {
230*22028508SToomas Soome 	int bq = bios_getquirks();
231*22028508SToomas Soome 
232*22028508SToomas Soome 	printf("bios_basemem: 0x%llx\n", (unsigned long long)bios_basemem);
233*22028508SToomas Soome 	printf("bios_extmem: 0x%llx\n", (unsigned long long)bios_extmem);
234*22028508SToomas Soome 	printf("memtop: 0x%llx\n", (unsigned long long)memtop);
235*22028508SToomas Soome 	printf("high_heap_base: 0x%llx\n", (unsigned long long)high_heap_base);
236*22028508SToomas Soome 	printf("high_heap_size: 0x%llx\n", (unsigned long long)high_heap_size);
237*22028508SToomas Soome 	printf("bios_quirks: 0x%02x", bq);
238*22028508SToomas Soome 	if (bq & BQ_DISTRUST_E820_EXTMEM)
239*22028508SToomas Soome 		printf(" BQ_DISTRUST_E820_EXTMEM");
240*22028508SToomas Soome 	printf("\n");
241*22028508SToomas Soome 	printf("b_bios_probed: 0x%02x", (int)b_bios_probed);
242*22028508SToomas Soome 	if (b_bios_probed & B_BASEMEM_E820)
243*22028508SToomas Soome 		printf(" B_BASEMEM_E820");
244*22028508SToomas Soome 	if (b_bios_probed & B_BASEMEM_12)
245*22028508SToomas Soome 		printf(" B_BASEMEM_12");
246*22028508SToomas Soome 	if (b_bios_probed & B_EXTMEM_E820)
247*22028508SToomas Soome 		printf(" B_EXTMEM_E820");
248*22028508SToomas Soome 	if (b_bios_probed & B_EXTMEM_E801)
249*22028508SToomas Soome 		printf(" B_EXTMEM_E801");
250*22028508SToomas Soome 	if (b_bios_probed & B_EXTMEM_8800)
251*22028508SToomas Soome 		printf(" B_EXTMEM_8800");
252*22028508SToomas Soome 	printf("\n");
253*22028508SToomas Soome 
254*22028508SToomas Soome 	return (CMD_OK);
255*22028508SToomas Soome }
256*22028508SToomas Soome 
257*22028508SToomas Soome COMMAND_SET(biosmem, "biosmem", "show BIOS memory setup", command_biosmem);
258