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