memory.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* -*- linux-c -*- ------------------------------------------------------- *
  2. *
  3. * Copyright (C) 1991, 1992 Linus Torvalds
  4. * Copyright 2007 rPath, Inc. - All Rights Reserved
  5. * Copyright 2009 Intel Corporation; author H. Peter Anvin
  6. *
  7. * This file is part of the Linux kernel, and is made available under
  8. * the terms of the GNU General Public License version 2.
  9. *
  10. * ----------------------------------------------------------------------- */
  11. /*
  12. * Memory detection code
  13. */
  14. #include "boot.h"
  15. #define SMAP 0x534d4150 /* ASCII "SMAP" */
  16. static int detect_memory_e820(void)
  17. {
  18. int count = 0;
  19. u32 next = 0;
  20. u32 size, id, edi;
  21. u8 err;
  22. struct e820entry *desc = boot_params.e820_map;
  23. static struct e820entry buf; /* static so it is zeroed */
  24. /*
  25. * Note: at least one BIOS is known which assumes that the
  26. * buffer pointed to by one e820 call is the same one as
  27. * the previous call, and only changes modified fields. Therefore,
  28. * we use a temporary buffer and copy the results entry by entry.
  29. *
  30. * This routine deliberately does not try to account for
  31. * ACPI 3+ extended attributes. This is because there are
  32. * BIOSes in the field which report zero for the valid bit for
  33. * all ranges, and we don't currently make any use of the
  34. * other attribute bits. Revisit this if we see the extended
  35. * attribute bits deployed in a meaningful way in the future.
  36. */
  37. do {
  38. size = sizeof buf;
  39. /* Important: %edx and %esi are clobbered by some BIOSes,
  40. so they must be either used for the error output
  41. or explicitly marked clobbered. Given that, assume there
  42. is something out there clobbering %ebp and %edi, too. */
  43. asm("pushl %%ebp; int $0x15; popl %%ebp; setc %0"
  44. : "=d" (err), "+b" (next), "=a" (id), "+c" (size),
  45. "=D" (edi), "+m" (buf)
  46. : "D" (&buf), "d" (SMAP), "a" (0xe820)
  47. : "esi");
  48. /* BIOSes which terminate the chain with CF = 1 as opposed
  49. to %ebx = 0 don't always report the SMAP signature on
  50. the final, failing, probe. */
  51. if (err)
  52. break;
  53. /* Some BIOSes stop returning SMAP in the middle of
  54. the search loop. We don't know exactly how the BIOS
  55. screwed up the map at that point, we might have a
  56. partial map, the full map, or complete garbage, so
  57. just return failure. */
  58. if (id != SMAP) {
  59. count = 0;
  60. break;
  61. }
  62. *desc++ = buf;
  63. count++;
  64. } while (next && count < ARRAY_SIZE(boot_params.e820_map));
  65. return boot_params.e820_entries = count;
  66. }
  67. static int detect_memory_e801(void)
  68. {
  69. u16 ax, bx, cx, dx;
  70. u8 err;
  71. bx = cx = dx = 0;
  72. ax = 0xe801;
  73. asm("stc; int $0x15; setc %0"
  74. : "=m" (err), "+a" (ax), "+b" (bx), "+c" (cx), "+d" (dx));
  75. if (err)
  76. return -1;
  77. /* Do we really need to do this? */
  78. if (cx || dx) {
  79. ax = cx;
  80. bx = dx;
  81. }
  82. if (ax > 15*1024)
  83. return -1; /* Bogus! */
  84. /* This ignores memory above 16MB if we have a memory hole
  85. there. If someone actually finds a machine with a memory
  86. hole at 16MB and no support for 0E820h they should probably
  87. generate a fake e820 map. */
  88. boot_params.alt_mem_k = (ax == 15*1024) ? (dx << 6)+ax : ax;
  89. return 0;
  90. }
  91. static int detect_memory_88(void)
  92. {
  93. u16 ax;
  94. u8 err;
  95. ax = 0x8800;
  96. asm("stc; int $0x15; setc %0" : "=bcdm" (err), "+a" (ax));
  97. boot_params.screen_info.ext_mem_k = ax;
  98. return -err;
  99. }
  100. int detect_memory(void)
  101. {
  102. int err = -1;
  103. if (detect_memory_e820() > 0)
  104. err = 0;
  105. if (!detect_memory_e801())
  106. err = 0;
  107. if (!detect_memory_88())
  108. err = 0;
  109. return err;
  110. }