1*b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2f2d85299SLuis R. Rodriguez #include <linux/kernel.h>
3f2d85299SLuis R. Rodriguez #include <linux/init.h>
4f2d85299SLuis R. Rodriguez #include <linux/memblock.h>
5f2d85299SLuis R. Rodriguez
6f2d85299SLuis R. Rodriguez #include <asm/setup.h>
7f2d85299SLuis R. Rodriguez #include <asm/bios_ebda.h>
8f2d85299SLuis R. Rodriguez
9f2d85299SLuis R. Rodriguez /*
10edce2121SIngo Molnar * This function reserves all conventional PC system BIOS related
11edce2121SIngo Molnar * firmware memory areas (some of which are data, some of which
12edce2121SIngo Molnar * are code), that must not be used by the kernel as available
13edce2121SIngo Molnar * RAM.
14edce2121SIngo Molnar *
15f2d85299SLuis R. Rodriguez * The BIOS places the EBDA/XBDA at the top of conventional
16f2d85299SLuis R. Rodriguez * memory, and usually decreases the reported amount of
17edce2121SIngo Molnar * conventional memory (int 0x12) too.
18f2d85299SLuis R. Rodriguez *
19edce2121SIngo Molnar * This means that as a first approximation on most systems we can
20edce2121SIngo Molnar * guess the reserved BIOS area by looking at the low BIOS RAM size
21edce2121SIngo Molnar * value and assume that everything above that value (up to 1MB) is
22edce2121SIngo Molnar * reserved.
23edce2121SIngo Molnar *
24edce2121SIngo Molnar * But life in firmware country is not that simple:
25edce2121SIngo Molnar *
26edce2121SIngo Molnar * - This code also contains a quirk for Dell systems that neglect
27edce2121SIngo Molnar * to reserve the EBDA area in the 'RAM size' value ...
28edce2121SIngo Molnar *
29edce2121SIngo Molnar * - The same quirk also avoids a problem with the AMD768MPX
30edce2121SIngo Molnar * chipset: reserve a page before VGA to prevent PCI prefetch
31edce2121SIngo Molnar * into it (errata #56). (Usually the page is reserved anyways,
32edce2121SIngo Molnar * unless you have no PS/2 mouse plugged in.)
33edce2121SIngo Molnar *
34edce2121SIngo Molnar * - Plus paravirt systems don't have a reliable value in the
35edce2121SIngo Molnar * 'BIOS RAM size' pointer we can rely on, so we must quirk
36edce2121SIngo Molnar * them too.
37edce2121SIngo Molnar *
38edce2121SIngo Molnar * Due to those various problems this function is deliberately
39edce2121SIngo Molnar * very conservative and tries to err on the side of reserving
40edce2121SIngo Molnar * too much, to not risk reserving too little.
41edce2121SIngo Molnar *
42edce2121SIngo Molnar * Losing a small amount of memory in the bottom megabyte is
43edce2121SIngo Molnar * rarely a problem, as long as we have enough memory to install
44edce2121SIngo Molnar * the SMP bootup trampoline which *must* be in this area.
45edce2121SIngo Molnar *
46edce2121SIngo Molnar * Using memory that is in use by the BIOS or by some DMA device
47edce2121SIngo Molnar * the BIOS didn't shut down *is* a big problem to the kernel,
48edce2121SIngo Molnar * obviously.
49f2d85299SLuis R. Rodriguez */
50f2d85299SLuis R. Rodriguez
51edce2121SIngo Molnar #define BIOS_RAM_SIZE_KB_PTR 0x413
52f2d85299SLuis R. Rodriguez
53edce2121SIngo Molnar #define BIOS_START_MIN 0x20000U /* 128K, less than this is insane */
54edce2121SIngo Molnar #define BIOS_START_MAX 0x9f000U /* 640K, absolute maximum */
55edce2121SIngo Molnar
reserve_bios_regions(void)56edce2121SIngo Molnar void __init reserve_bios_regions(void)
57f2d85299SLuis R. Rodriguez {
58edce2121SIngo Molnar unsigned int bios_start, ebda_start;
59f2d85299SLuis R. Rodriguez
60f2d85299SLuis R. Rodriguez /*
61edce2121SIngo Molnar * NOTE: In a paravirtual environment the BIOS reserved
62edce2121SIngo Molnar * area is absent. We'll just have to assume that the
63edce2121SIngo Molnar * paravirt case can handle memory setup correctly,
64edce2121SIngo Molnar * without our help.
65f2d85299SLuis R. Rodriguez */
66edce2121SIngo Molnar if (!x86_platform.legacy.reserve_bios_regions)
67f2d85299SLuis R. Rodriguez return;
68f2d85299SLuis R. Rodriguez
69edce2121SIngo Molnar /*
70edce2121SIngo Molnar * BIOS RAM size is encoded in kilobytes, convert it
71edce2121SIngo Molnar * to bytes to get a first guess at where the BIOS
72edce2121SIngo Molnar * firmware area starts:
73edce2121SIngo Molnar */
74edce2121SIngo Molnar bios_start = *(unsigned short *)__va(BIOS_RAM_SIZE_KB_PTR);
75edce2121SIngo Molnar bios_start <<= 10;
76f2d85299SLuis R. Rodriguez
77edce2121SIngo Molnar /*
78edce2121SIngo Molnar * If bios_start is less than 128K, assume it is bogus
796a79296cSAndy Lutomirski * and bump it up to 640K. Similarly, if bios_start is above 640K,
806a79296cSAndy Lutomirski * don't trust it.
81edce2121SIngo Molnar */
826a79296cSAndy Lutomirski if (bios_start < BIOS_START_MIN || bios_start > BIOS_START_MAX)
83edce2121SIngo Molnar bios_start = BIOS_START_MAX;
84f2d85299SLuis R. Rodriguez
856a79296cSAndy Lutomirski /* Get the start address of the EBDA page: */
866a79296cSAndy Lutomirski ebda_start = get_bios_ebda();
876a79296cSAndy Lutomirski
88edce2121SIngo Molnar /*
896a79296cSAndy Lutomirski * If the EBDA start address is sane and is below the BIOS region,
906a79296cSAndy Lutomirski * then also reserve everything from the EBDA start address up to
916a79296cSAndy Lutomirski * the BIOS region.
92edce2121SIngo Molnar */
936a79296cSAndy Lutomirski if (ebda_start >= BIOS_START_MIN && ebda_start < bios_start)
946a79296cSAndy Lutomirski bios_start = ebda_start;
95f2d85299SLuis R. Rodriguez
96edce2121SIngo Molnar /* Reserve all memory between bios_start and the 1MB mark: */
97edce2121SIngo Molnar memblock_reserve(bios_start, 0x100000 - bios_start);
98f2d85299SLuis R. Rodriguez }
99