// SPDX-License-Identifier: GPL-2.01#include <linux/kernel.h>2#include <linux/init.h>3#include <linux/memblock.h>45#include <asm/setup.h>6#include <asm/bios_ebda.h>78/*9* This function reserves all conventional PC system BIOS related10* firmware memory areas (some of which are data, some of which11* are code), that must not be used by the kernel as available12* RAM.13*14* The BIOS places the EBDA/XBDA at the top of conventional15* memory, and usually decreases the reported amount of16* conventional memory (int 0x12) too.17*18* This means that as a first approximation on most systems we can19* guess the reserved BIOS area by looking at the low BIOS RAM size20* value and assume that everything above that value (up to 1MB) is21* reserved.22*23* But life in firmware country is not that simple:24*25* - This code also contains a quirk for Dell systems that neglect26* to reserve the EBDA area in the 'RAM size' value ...27*28* - The same quirk also avoids a problem with the AMD768MPX29* chipset: reserve a page before VGA to prevent PCI prefetch30* into it (errata #56). (Usually the page is reserved anyways,31* unless you have no PS/2 mouse plugged in.)32*33* - Plus paravirt systems don't have a reliable value in the34* 'BIOS RAM size' pointer we can rely on, so we must quirk35* them too.36*37* Due to those various problems this function is deliberately38* very conservative and tries to err on the side of reserving39* too much, to not risk reserving too little.40*41* Losing a small amount of memory in the bottom megabyte is42* rarely a problem, as long as we have enough memory to install43* the SMP bootup trampoline which *must* be in this area.44*45* Using memory that is in use by the BIOS or by some DMA device46* the BIOS didn't shut down *is* a big problem to the kernel,47* obviously.48*/4950#define BIOS_RAM_SIZE_KB_PTR 0x4135152#define BIOS_START_MIN 0x20000U /* 128K, less than this is insane */53#define BIOS_START_MAX 0x9f000U /* 640K, absolute maximum */5455void __init reserve_bios_regions(void)56{57unsigned int bios_start, ebda_start;5859/*60* NOTE: In a paravirtual environment the BIOS reserved61* area is absent. We'll just have to assume that the62* paravirt case can handle memory setup correctly,63* without our help.64*/65if (!x86_platform.legacy.reserve_bios_regions)66return;6768/*69* BIOS RAM size is encoded in kilobytes, convert it70* to bytes to get a first guess at where the BIOS71* firmware area starts:72*/73bios_start = *(unsigned short *)__va(BIOS_RAM_SIZE_KB_PTR);74bios_start <<= 10;7576/*77* If bios_start is less than 128K, assume it is bogus78* and bump it up to 640K. Similarly, if bios_start is above 640K,79* don't trust it.80*/81if (bios_start < BIOS_START_MIN || bios_start > BIOS_START_MAX)82bios_start = BIOS_START_MAX;8384/* Get the start address of the EBDA page: */85ebda_start = get_bios_ebda();8687/*88* If the EBDA start address is sane and is below the BIOS region,89* then also reserve everything from the EBDA start address up to90* the BIOS region.91*/92if (ebda_start >= BIOS_START_MIN && ebda_start < bios_start)93bios_start = ebda_start;9495/* Reserve all memory between bios_start and the 1MB mark: */96memblock_reserve(bios_start, 0x100000 - bios_start);97}9899100