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 /* 69edce2121SIngo Molnar * BIOS RAM size is encoded in kilobytes, convert it 70edce2121SIngo Molnar * to bytes to get a first guess at where the BIOS 71edce2121SIngo Molnar * firmware area starts: 72edce2121SIngo Molnar */ 73edce2121SIngo Molnar bios_start = *(unsigned short *)__va(BIOS_RAM_SIZE_KB_PTR); 74edce2121SIngo Molnar bios_start <<= 10; 75f2d85299SLuis R. Rodriguez 76edce2121SIngo Molnar /* 77edce2121SIngo Molnar * If bios_start is less than 128K, assume it is bogus 786a79296cSAndy Lutomirski * and bump it up to 640K. Similarly, if bios_start is above 640K, 796a79296cSAndy Lutomirski * don't trust it. 80edce2121SIngo Molnar */ 816a79296cSAndy Lutomirski if (bios_start < BIOS_START_MIN || bios_start > BIOS_START_MAX) 82edce2121SIngo Molnar bios_start = BIOS_START_MAX; 83f2d85299SLuis R. Rodriguez 846a79296cSAndy Lutomirski /* Get the start address of the EBDA page: */ 856a79296cSAndy Lutomirski ebda_start = get_bios_ebda(); 866a79296cSAndy Lutomirski 87edce2121SIngo Molnar /* 886a79296cSAndy Lutomirski * If the EBDA start address is sane and is below the BIOS region, 896a79296cSAndy Lutomirski * then also reserve everything from the EBDA start address up to 906a79296cSAndy Lutomirski * the BIOS region. 91edce2121SIngo Molnar */ 926a79296cSAndy Lutomirski if (ebda_start >= BIOS_START_MIN && ebda_start < bios_start) 936a79296cSAndy Lutomirski bios_start = ebda_start; 94f2d85299SLuis R. Rodriguez 95edce2121SIngo Molnar /* Reserve all memory between bios_start and the 1MB mark: */ 96edce2121SIngo Molnar memblock_reserve(bios_start, 0x100000 - bios_start); 97f2d85299SLuis R. Rodriguez } 98