memory.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* -*- linux-c -*- ------------------------------------------------------- *
  2. *
  3. * Copyright (C) 1991, 1992 Linus Torvalds
  4. * Copyright 2007 rPath, Inc. - All Rights Reserved
  5. *
  6. * This file is part of the Linux kernel, and is made available under
  7. * the terms of the GNU General Public License version 2.
  8. *
  9. * ----------------------------------------------------------------------- */
  10. /*
  11. * Memory detection code
  12. */
  13. #include "boot.h"
  14. #define SMAP 0x534d4150 /* ASCII "SMAP" */
  15. static int detect_memory_e820(void)
  16. {
  17. int count = 0;
  18. u32 next = 0;
  19. u32 size, id;
  20. u8 err;
  21. struct e820entry *desc = boot_params.e820_map;
  22. do {
  23. size = sizeof(struct e820entry);
  24. /* Important: %edx is clobbered by some BIOSes,
  25. so it must be either used for the error output
  26. or explicitly marked clobbered. */
  27. asm("int $0x15; setc %0"
  28. : "=d" (err), "+b" (next), "=a" (id), "+c" (size),
  29. "=m" (*desc)
  30. : "D" (desc), "d" (SMAP), "a" (0xe820));
  31. /* BIOSes which terminate the chain with CF = 1 as opposed
  32. to %ebx = 0 don't always report the SMAP signature on
  33. the final, failing, probe. */
  34. if (err)
  35. break;
  36. /* Some BIOSes stop returning SMAP in the middle of
  37. the search loop. We don't know exactly how the BIOS
  38. screwed up the map at that point, we might have a
  39. partial map, the full map, or complete garbage, so
  40. just return failure. */
  41. if (id != SMAP) {
  42. count = 0;
  43. break;
  44. }
  45. count++;
  46. desc++;
  47. } while (next && count < ARRAY_SIZE(boot_params.e820_map));
  48. return boot_params.e820_entries = count;
  49. }
  50. static int detect_memory_e801(void)
  51. {
  52. u16 ax, bx, cx, dx;
  53. u8 err;
  54. bx = cx = dx = 0;
  55. ax = 0xe801;
  56. asm("stc; int $0x15; setc %0"
  57. : "=m" (err), "+a" (ax), "+b" (bx), "+c" (cx), "+d" (dx));
  58. if (err)
  59. return -1;
  60. /* Do we really need to do this? */
  61. if (cx || dx) {
  62. ax = cx;
  63. bx = dx;
  64. }
  65. if (ax > 15*1024)
  66. return -1; /* Bogus! */
  67. /* This ignores memory above 16MB if we have a memory hole
  68. there. If someone actually finds a machine with a memory
  69. hole at 16MB and no support for 0E820h they should probably
  70. generate a fake e820 map. */
  71. boot_params.alt_mem_k = (ax == 15*1024) ? (dx << 6)+ax : ax;
  72. return 0;
  73. }
  74. static int detect_memory_88(void)
  75. {
  76. u16 ax;
  77. u8 err;
  78. ax = 0x8800;
  79. asm("stc; int $0x15; setc %0" : "=bcdm" (err), "+a" (ax));
  80. boot_params.screen_info.ext_mem_k = ax;
  81. return -err;
  82. }
  83. int detect_memory(void)
  84. {
  85. int err = -1;
  86. if (detect_memory_e820() > 0)
  87. err = 0;
  88. if (!detect_memory_e801())
  89. err = 0;
  90. if (!detect_memory_88())
  91. err = 0;
  92. return err;
  93. }