18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#include <linux/kernel.h>
38c2ecf20Sopenharmony_ci#include <linux/init.h>
48c2ecf20Sopenharmony_ci#include <linux/memblock.h>
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <asm/setup.h>
78c2ecf20Sopenharmony_ci#include <asm/bios_ebda.h>
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci/*
108c2ecf20Sopenharmony_ci * This function reserves all conventional PC system BIOS related
118c2ecf20Sopenharmony_ci * firmware memory areas (some of which are data, some of which
128c2ecf20Sopenharmony_ci * are code), that must not be used by the kernel as available
138c2ecf20Sopenharmony_ci * RAM.
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * The BIOS places the EBDA/XBDA at the top of conventional
168c2ecf20Sopenharmony_ci * memory, and usually decreases the reported amount of
178c2ecf20Sopenharmony_ci * conventional memory (int 0x12) too.
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * This means that as a first approximation on most systems we can
208c2ecf20Sopenharmony_ci * guess the reserved BIOS area by looking at the low BIOS RAM size
218c2ecf20Sopenharmony_ci * value and assume that everything above that value (up to 1MB) is
228c2ecf20Sopenharmony_ci * reserved.
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci * But life in firmware country is not that simple:
258c2ecf20Sopenharmony_ci *
268c2ecf20Sopenharmony_ci * - This code also contains a quirk for Dell systems that neglect
278c2ecf20Sopenharmony_ci *   to reserve the EBDA area in the 'RAM size' value ...
288c2ecf20Sopenharmony_ci *
298c2ecf20Sopenharmony_ci * - The same quirk also avoids a problem with the AMD768MPX
308c2ecf20Sopenharmony_ci *   chipset: reserve a page before VGA to prevent PCI prefetch
318c2ecf20Sopenharmony_ci *   into it (errata #56). (Usually the page is reserved anyways,
328c2ecf20Sopenharmony_ci *   unless you have no PS/2 mouse plugged in.)
338c2ecf20Sopenharmony_ci *
348c2ecf20Sopenharmony_ci * - Plus paravirt systems don't have a reliable value in the
358c2ecf20Sopenharmony_ci *   'BIOS RAM size' pointer we can rely on, so we must quirk
368c2ecf20Sopenharmony_ci *   them too.
378c2ecf20Sopenharmony_ci *
388c2ecf20Sopenharmony_ci * Due to those various problems this function is deliberately
398c2ecf20Sopenharmony_ci * very conservative and tries to err on the side of reserving
408c2ecf20Sopenharmony_ci * too much, to not risk reserving too little.
418c2ecf20Sopenharmony_ci *
428c2ecf20Sopenharmony_ci * Losing a small amount of memory in the bottom megabyte is
438c2ecf20Sopenharmony_ci * rarely a problem, as long as we have enough memory to install
448c2ecf20Sopenharmony_ci * the SMP bootup trampoline which *must* be in this area.
458c2ecf20Sopenharmony_ci *
468c2ecf20Sopenharmony_ci * Using memory that is in use by the BIOS or by some DMA device
478c2ecf20Sopenharmony_ci * the BIOS didn't shut down *is* a big problem to the kernel,
488c2ecf20Sopenharmony_ci * obviously.
498c2ecf20Sopenharmony_ci */
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci#define BIOS_RAM_SIZE_KB_PTR	0x413
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci#define BIOS_START_MIN		0x20000U	/* 128K, less than this is insane */
548c2ecf20Sopenharmony_ci#define BIOS_START_MAX		0x9f000U	/* 640K, absolute maximum */
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_civoid __init reserve_bios_regions(void)
578c2ecf20Sopenharmony_ci{
588c2ecf20Sopenharmony_ci	unsigned int bios_start, ebda_start;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	/*
618c2ecf20Sopenharmony_ci	 * NOTE: In a paravirtual environment the BIOS reserved
628c2ecf20Sopenharmony_ci	 * area is absent. We'll just have to assume that the
638c2ecf20Sopenharmony_ci	 * paravirt case can handle memory setup correctly,
648c2ecf20Sopenharmony_ci	 * without our help.
658c2ecf20Sopenharmony_ci	 */
668c2ecf20Sopenharmony_ci	if (!x86_platform.legacy.reserve_bios_regions)
678c2ecf20Sopenharmony_ci		return;
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	/*
708c2ecf20Sopenharmony_ci	 * BIOS RAM size is encoded in kilobytes, convert it
718c2ecf20Sopenharmony_ci	 * to bytes to get a first guess at where the BIOS
728c2ecf20Sopenharmony_ci	 * firmware area starts:
738c2ecf20Sopenharmony_ci	 */
748c2ecf20Sopenharmony_ci	bios_start = *(unsigned short *)__va(BIOS_RAM_SIZE_KB_PTR);
758c2ecf20Sopenharmony_ci	bios_start <<= 10;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	/*
788c2ecf20Sopenharmony_ci	 * If bios_start is less than 128K, assume it is bogus
798c2ecf20Sopenharmony_ci	 * and bump it up to 640K.  Similarly, if bios_start is above 640K,
808c2ecf20Sopenharmony_ci	 * don't trust it.
818c2ecf20Sopenharmony_ci	 */
828c2ecf20Sopenharmony_ci	if (bios_start < BIOS_START_MIN || bios_start > BIOS_START_MAX)
838c2ecf20Sopenharmony_ci		bios_start = BIOS_START_MAX;
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	/* Get the start address of the EBDA page: */
868c2ecf20Sopenharmony_ci	ebda_start = get_bios_ebda();
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	/*
898c2ecf20Sopenharmony_ci	 * If the EBDA start address is sane and is below the BIOS region,
908c2ecf20Sopenharmony_ci	 * then also reserve everything from the EBDA start address up to
918c2ecf20Sopenharmony_ci	 * the BIOS region.
928c2ecf20Sopenharmony_ci	 */
938c2ecf20Sopenharmony_ci	if (ebda_start >= BIOS_START_MIN && ebda_start < bios_start)
948c2ecf20Sopenharmony_ci		bios_start = ebda_start;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	/* Reserve all memory between bios_start and the 1MB mark: */
978c2ecf20Sopenharmony_ci	memblock_reserve(bios_start, 0x100000 - bios_start);
988c2ecf20Sopenharmony_ci}
99