xref: /openbmc/linux/arch/x86/kernel/ebda.c (revision edce2121)
1f2d85299SLuis R. Rodriguez #include <linux/kernel.h>
2f2d85299SLuis R. Rodriguez #include <linux/init.h>
3f2d85299SLuis R. Rodriguez #include <linux/memblock.h>
4f2d85299SLuis R. Rodriguez 
5f2d85299SLuis R. Rodriguez #include <asm/setup.h>
6f2d85299SLuis R. Rodriguez #include <asm/bios_ebda.h>
7f2d85299SLuis R. Rodriguez 
8f2d85299SLuis R. Rodriguez /*
9edce2121SIngo Molnar  * This function reserves all conventional PC system BIOS related
10edce2121SIngo Molnar  * firmware memory areas (some of which are data, some of which
11edce2121SIngo Molnar  * are code), that must not be used by the kernel as available
12edce2121SIngo Molnar  * RAM.
13edce2121SIngo Molnar  *
14f2d85299SLuis R. Rodriguez  * The BIOS places the EBDA/XBDA at the top of conventional
15f2d85299SLuis R. Rodriguez  * memory, and usually decreases the reported amount of
16edce2121SIngo Molnar  * conventional memory (int 0x12) too.
17f2d85299SLuis R. Rodriguez  *
18edce2121SIngo Molnar  * This means that as a first approximation on most systems we can
19edce2121SIngo Molnar  * guess the reserved BIOS area by looking at the low BIOS RAM size
20edce2121SIngo Molnar  * value and assume that everything above that value (up to 1MB) is
21edce2121SIngo Molnar  * reserved.
22edce2121SIngo Molnar  *
23edce2121SIngo Molnar  * But life in firmware country is not that simple:
24edce2121SIngo Molnar  *
25edce2121SIngo Molnar  * - This code also contains a quirk for Dell systems that neglect
26edce2121SIngo Molnar  *   to reserve the EBDA area in the 'RAM size' value ...
27edce2121SIngo Molnar  *
28edce2121SIngo Molnar  * - The same quirk also avoids a problem with the AMD768MPX
29edce2121SIngo Molnar  *   chipset: reserve a page before VGA to prevent PCI prefetch
30edce2121SIngo Molnar  *   into it (errata #56). (Usually the page is reserved anyways,
31edce2121SIngo Molnar  *   unless you have no PS/2 mouse plugged in.)
32edce2121SIngo Molnar  *
33edce2121SIngo Molnar  * - Plus paravirt systems don't have a reliable value in the
34edce2121SIngo Molnar  *   'BIOS RAM size' pointer we can rely on, so we must quirk
35edce2121SIngo Molnar  *   them too.
36edce2121SIngo Molnar  *
37edce2121SIngo Molnar  * Due to those various problems this function is deliberately
38edce2121SIngo Molnar  * very conservative and tries to err on the side of reserving
39edce2121SIngo Molnar  * too much, to not risk reserving too little.
40edce2121SIngo Molnar  *
41edce2121SIngo Molnar  * Losing a small amount of memory in the bottom megabyte is
42edce2121SIngo Molnar  * rarely a problem, as long as we have enough memory to install
43edce2121SIngo Molnar  * the SMP bootup trampoline which *must* be in this area.
44edce2121SIngo Molnar  *
45edce2121SIngo Molnar  * Using memory that is in use by the BIOS or by some DMA device
46edce2121SIngo Molnar  * the BIOS didn't shut down *is* a big problem to the kernel,
47edce2121SIngo Molnar  * obviously.
48f2d85299SLuis R. Rodriguez  */
49f2d85299SLuis R. Rodriguez 
50edce2121SIngo Molnar #define BIOS_RAM_SIZE_KB_PTR	0x413
51f2d85299SLuis R. Rodriguez 
52edce2121SIngo Molnar #define BIOS_START_MIN		0x20000U	/* 128K, less than this is insane */
53edce2121SIngo Molnar #define BIOS_START_MAX		0x9f000U	/* 640K, absolute maximum */
54edce2121SIngo Molnar 
55edce2121SIngo Molnar void __init reserve_bios_regions(void)
56f2d85299SLuis R. Rodriguez {
57edce2121SIngo Molnar 	unsigned int bios_start, ebda_start;
58f2d85299SLuis R. Rodriguez 
59f2d85299SLuis R. Rodriguez 	/*
60edce2121SIngo Molnar 	 * NOTE: In a paravirtual environment the BIOS reserved
61edce2121SIngo Molnar 	 * area is absent. We'll just have to assume that the
62edce2121SIngo Molnar 	 * paravirt case can handle memory setup correctly,
63edce2121SIngo Molnar 	 * without our help.
64f2d85299SLuis R. Rodriguez 	 */
65edce2121SIngo Molnar 	if (!x86_platform.legacy.reserve_bios_regions)
66f2d85299SLuis R. Rodriguez 		return;
67f2d85299SLuis R. Rodriguez 
68edce2121SIngo Molnar 	/* Get the start address of the EBDA page: */
69edce2121SIngo Molnar 	ebda_start = get_bios_ebda();
70f2d85299SLuis R. Rodriguez 
71f2d85299SLuis R. Rodriguez 	/*
72edce2121SIngo Molnar 	 * Quirk: some old Dells seem to have a 4k EBDA without
73edce2121SIngo Molnar 	 * reporting so in their BIOS RAM size value, so just
74edce2121SIngo Molnar 	 * consider the memory above 640K to be off limits
75edce2121SIngo Molnar 	 * (bugzilla 2990).
76edce2121SIngo Molnar 	 *
77edce2121SIngo Molnar 	 * We detect this case by filtering for nonsensical EBDA
78edce2121SIngo Molnar 	 * addresses below 128K, where we can assume that they
79edce2121SIngo Molnar 	 * are bogus and bump it up to a fixed 640K value:
80f2d85299SLuis R. Rodriguez 	 */
81edce2121SIngo Molnar 	if (ebda_start < BIOS_START_MIN)
82edce2121SIngo Molnar 		ebda_start = BIOS_START_MAX;
83f2d85299SLuis R. Rodriguez 
84edce2121SIngo Molnar 	/*
85edce2121SIngo Molnar 	 * BIOS RAM size is encoded in kilobytes, convert it
86edce2121SIngo Molnar 	 * to bytes to get a first guess at where the BIOS
87edce2121SIngo Molnar 	 * firmware area starts:
88edce2121SIngo Molnar 	 */
89edce2121SIngo Molnar 	bios_start = *(unsigned short *)__va(BIOS_RAM_SIZE_KB_PTR);
90edce2121SIngo Molnar 	bios_start <<= 10;
91f2d85299SLuis R. Rodriguez 
92edce2121SIngo Molnar 	/*
93edce2121SIngo Molnar 	 * If bios_start is less than 128K, assume it is bogus
94edce2121SIngo Molnar 	 * and bump it up to 640K:
95edce2121SIngo Molnar 	 */
96edce2121SIngo Molnar 	if (bios_start < BIOS_START_MIN)
97edce2121SIngo Molnar 		bios_start = BIOS_START_MAX;
98f2d85299SLuis R. Rodriguez 
99edce2121SIngo Molnar 	/*
100edce2121SIngo Molnar 	 * Use the lower of the bios_start and ebda_start
101edce2121SIngo Molnar 	 * as the starting point, but don't allow it to
102edce2121SIngo Molnar 	 * go beyond 640K:
103edce2121SIngo Molnar 	 */
104edce2121SIngo Molnar 	bios_start = min(bios_start, ebda_start);
105edce2121SIngo Molnar 	bios_start = min(bios_start, BIOS_START_MAX);
106f2d85299SLuis R. Rodriguez 
107edce2121SIngo Molnar 	/* Reserve all memory between bios_start and the 1MB mark: */
108edce2121SIngo Molnar 	memblock_reserve(bios_start, 0x100000 - bios_start);
109f2d85299SLuis R. Rodriguez }
110