ebda.c 3.2 KB

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