memory.c 2.4 KB

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