memory.c 3.3 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. struct biosregs ireg, oreg;
  20. struct e820entry *desc = boot_params.e820_map;
  21. static struct e820entry buf; /* static so it is zeroed */
  22. initregs(&ireg);
  23. ireg.ax = 0xe820;
  24. ireg.cx = sizeof buf;
  25. ireg.edx = SMAP;
  26. ireg.di = (size_t)&buf;
  27. /*
  28. * Note: at least one BIOS is known which assumes that the
  29. * buffer pointed to by one e820 call is the same one as
  30. * the previous call, and only changes modified fields. Therefore,
  31. * we use a temporary buffer and copy the results entry by entry.
  32. *
  33. * This routine deliberately does not try to account for
  34. * ACPI 3+ extended attributes. This is because there are
  35. * BIOSes in the field which report zero for the valid bit for
  36. * all ranges, and we don't currently make any use of the
  37. * other attribute bits. Revisit this if we see the extended
  38. * attribute bits deployed in a meaningful way in the future.
  39. */
  40. do {
  41. intcall(0x15, &ireg, &oreg);
  42. ireg.ebx = oreg.ebx; /* for next iteration... */
  43. /* BIOSes which terminate the chain with CF = 1 as opposed
  44. to %ebx = 0 don't always report the SMAP signature on
  45. the final, failing, probe. */
  46. if (oreg.eflags & X86_EFLAGS_CF)
  47. break;
  48. /* Some BIOSes stop returning SMAP in the middle of
  49. the search loop. We don't know exactly how the BIOS
  50. screwed up the map at that point, we might have a
  51. partial map, the full map, or complete garbage, so
  52. just return failure. */
  53. if (oreg.eax != SMAP) {
  54. count = 0;
  55. break;
  56. }
  57. *desc++ = buf;
  58. count++;
  59. } while (ireg.ebx && count < ARRAY_SIZE(boot_params.e820_map));
  60. return boot_params.e820_entries = count;
  61. }
  62. static int detect_memory_e801(void)
  63. {
  64. struct biosregs ireg, oreg;
  65. initregs(&ireg);
  66. ireg.ax = 0xe801;
  67. intcall(0x15, &ireg, &oreg);
  68. if (oreg.eflags & X86_EFLAGS_CF)
  69. return -1;
  70. /* Do we really need to do this? */
  71. if (oreg.cx || oreg.dx) {
  72. oreg.ax = oreg.cx;
  73. oreg.bx = oreg.dx;
  74. }
  75. if (oreg.ax > 15*1024) {
  76. return -1; /* Bogus! */
  77. } else if (oreg.ax == 15*1024) {
  78. boot_params.alt_mem_k = (oreg.bx << 6) + oreg.ax;
  79. } else {
  80. /*
  81. * This ignores memory above 16MB if we have a memory
  82. * hole there. If someone actually finds a machine
  83. * with a memory hole at 16MB and no support for
  84. * 0E820h they should probably generate a fake e820
  85. * map.
  86. */
  87. boot_params.alt_mem_k = oreg.ax;
  88. }
  89. return 0;
  90. }
  91. static int detect_memory_88(void)
  92. {
  93. struct biosregs ireg, oreg;
  94. initregs(&ireg);
  95. ireg.ah = 0x88;
  96. intcall(0x15, &ireg, &oreg);
  97. boot_params.screen_info.ext_mem_k = oreg.ax;
  98. return -(oreg.eflags & X86_EFLAGS_CF); /* 0 or -1 */
  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. }