e820_64.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Handle the memory map.
  3. * The functions here do the job until bootmem takes over.
  4. *
  5. * Getting sanitize_e820_map() in sync with i386 version by applying change:
  6. * - Provisions for empty E820 memory regions (reported by certain BIOSes).
  7. * Alex Achenbach <xela@slit.de>, December 2002.
  8. * Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
  9. *
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/types.h>
  13. #include <linux/init.h>
  14. #include <linux/bootmem.h>
  15. #include <linux/ioport.h>
  16. #include <linux/string.h>
  17. #include <linux/kexec.h>
  18. #include <linux/module.h>
  19. #include <linux/mm.h>
  20. #include <linux/suspend.h>
  21. #include <linux/pfn.h>
  22. #include <linux/pci.h>
  23. #include <asm/pgtable.h>
  24. #include <asm/page.h>
  25. #include <asm/e820.h>
  26. #include <asm/proto.h>
  27. #include <asm/setup.h>
  28. #include <asm/sections.h>
  29. #include <asm/kdebug.h>
  30. #include <asm/trampoline.h>
  31. /*
  32. * PFN of last memory page.
  33. */
  34. unsigned long end_pfn;
  35. /*
  36. * end_pfn only includes RAM, while max_pfn_mapped includes all e820 entries.
  37. * The direct mapping extends to max_pfn_mapped, so that we can directly access
  38. * apertures, ACPI and other tables without having to play with fixmaps.
  39. */
  40. unsigned long max_pfn_mapped;
  41. /*
  42. * Last pfn which the user wants to use.
  43. */
  44. static unsigned long __initdata end_user_pfn = MAXMEM>>PAGE_SHIFT;
  45. /*
  46. * Find the highest page frame number we have available
  47. */
  48. unsigned long __init e820_end_of_ram(void)
  49. {
  50. unsigned long last_pfn;
  51. last_pfn = find_max_pfn_with_active_regions();
  52. if (last_pfn > max_pfn_mapped)
  53. max_pfn_mapped = last_pfn;
  54. if (max_pfn_mapped > MAXMEM>>PAGE_SHIFT)
  55. max_pfn_mapped = MAXMEM>>PAGE_SHIFT;
  56. if (last_pfn > end_user_pfn)
  57. last_pfn = end_user_pfn;
  58. if (last_pfn > max_pfn_mapped)
  59. last_pfn = max_pfn_mapped;
  60. printk(KERN_INFO "max_pfn_mapped = %lu\n", max_pfn_mapped);
  61. return last_pfn;
  62. }
  63. /*
  64. * Mark e820 reserved areas as busy for the resource manager.
  65. */
  66. void __init e820_reserve_resources(void)
  67. {
  68. int i;
  69. struct resource *res;
  70. res = alloc_bootmem_low(sizeof(struct resource) * e820.nr_map);
  71. for (i = 0; i < e820.nr_map; i++) {
  72. switch (e820.map[i].type) {
  73. case E820_RAM: res->name = "System RAM"; break;
  74. case E820_ACPI: res->name = "ACPI Tables"; break;
  75. case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
  76. default: res->name = "reserved";
  77. }
  78. res->start = e820.map[i].addr;
  79. res->end = res->start + e820.map[i].size - 1;
  80. res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  81. insert_resource(&iomem_resource, res);
  82. res++;
  83. }
  84. }
  85. /*
  86. * Find the ranges of physical addresses that do not correspond to
  87. * e820 RAM areas and mark the corresponding pages as nosave for software
  88. * suspend and suspend to RAM.
  89. *
  90. * This function requires the e820 map to be sorted and without any
  91. * overlapping entries and assumes the first e820 area to be RAM.
  92. */
  93. void __init e820_mark_nosave_regions(void)
  94. {
  95. int i;
  96. unsigned long paddr;
  97. paddr = round_down(e820.map[0].addr + e820.map[0].size, PAGE_SIZE);
  98. for (i = 1; i < e820.nr_map; i++) {
  99. struct e820entry *ei = &e820.map[i];
  100. if (paddr < ei->addr)
  101. register_nosave_region(PFN_DOWN(paddr),
  102. PFN_UP(ei->addr));
  103. paddr = round_down(ei->addr + ei->size, PAGE_SIZE);
  104. if (ei->type != E820_RAM)
  105. register_nosave_region(PFN_UP(ei->addr),
  106. PFN_DOWN(paddr));
  107. if (paddr >= (end_pfn << PAGE_SHIFT))
  108. break;
  109. }
  110. }
  111. /*
  112. * Finds an active region in the address range from start_pfn to last_pfn and
  113. * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
  114. */
  115. static int __init e820_find_active_region(const struct e820entry *ei,
  116. unsigned long start_pfn,
  117. unsigned long last_pfn,
  118. unsigned long *ei_startpfn,
  119. unsigned long *ei_endpfn)
  120. {
  121. *ei_startpfn = round_up(ei->addr, PAGE_SIZE) >> PAGE_SHIFT;
  122. *ei_endpfn = round_down(ei->addr + ei->size, PAGE_SIZE) >> PAGE_SHIFT;
  123. /* Skip map entries smaller than a page */
  124. if (*ei_startpfn >= *ei_endpfn)
  125. return 0;
  126. /* Check if max_pfn_mapped should be updated */
  127. if (ei->type != E820_RAM && *ei_endpfn > max_pfn_mapped)
  128. max_pfn_mapped = *ei_endpfn;
  129. /* Skip if map is outside the node */
  130. if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
  131. *ei_startpfn >= last_pfn)
  132. return 0;
  133. /* Check for overlaps */
  134. if (*ei_startpfn < start_pfn)
  135. *ei_startpfn = start_pfn;
  136. if (*ei_endpfn > last_pfn)
  137. *ei_endpfn = last_pfn;
  138. /* Obey end_user_pfn to save on memmap */
  139. if (*ei_startpfn >= end_user_pfn)
  140. return 0;
  141. if (*ei_endpfn > end_user_pfn)
  142. *ei_endpfn = end_user_pfn;
  143. return 1;
  144. }
  145. /* Walk the e820 map and register active regions within a node */
  146. void __init
  147. e820_register_active_regions(int nid, unsigned long start_pfn,
  148. unsigned long last_pfn)
  149. {
  150. unsigned long ei_startpfn;
  151. unsigned long ei_endpfn;
  152. int i;
  153. for (i = 0; i < e820.nr_map; i++)
  154. if (e820_find_active_region(&e820.map[i],
  155. start_pfn, last_pfn,
  156. &ei_startpfn, &ei_endpfn))
  157. add_active_range(nid, ei_startpfn, ei_endpfn);
  158. }
  159. /*
  160. * Find the hole size (in bytes) in the memory range.
  161. * @start: starting address of the memory range to scan
  162. * @end: ending address of the memory range to scan
  163. */
  164. unsigned long __init e820_hole_size(unsigned long start, unsigned long end)
  165. {
  166. unsigned long start_pfn = start >> PAGE_SHIFT;
  167. unsigned long last_pfn = end >> PAGE_SHIFT;
  168. unsigned long ei_startpfn, ei_endpfn, ram = 0;
  169. int i;
  170. for (i = 0; i < e820.nr_map; i++) {
  171. if (e820_find_active_region(&e820.map[i],
  172. start_pfn, last_pfn,
  173. &ei_startpfn, &ei_endpfn))
  174. ram += ei_endpfn - ei_startpfn;
  175. }
  176. return end - start - (ram << PAGE_SHIFT);
  177. }
  178. static void early_panic(char *msg)
  179. {
  180. early_printk(msg);
  181. panic(msg);
  182. }
  183. /* We're not void only for x86 32-bit compat */
  184. char *__init machine_specific_memory_setup(void)
  185. {
  186. char *who = "BIOS-e820";
  187. int new_nr;
  188. /*
  189. * Try to copy the BIOS-supplied E820-map.
  190. *
  191. * Otherwise fake a memory map; one section from 0k->640k,
  192. * the next section from 1mb->appropriate_mem_k
  193. */
  194. new_nr = boot_params.e820_entries;
  195. sanitize_e820_map(boot_params.e820_map,
  196. ARRAY_SIZE(boot_params.e820_map),
  197. &new_nr);
  198. boot_params.e820_entries = new_nr;
  199. if (copy_e820_map(boot_params.e820_map, boot_params.e820_entries) < 0)
  200. early_panic("Cannot find a valid memory map");
  201. printk(KERN_INFO "BIOS-provided physical RAM map:\n");
  202. e820_print_map(who);
  203. /* In case someone cares... */
  204. return who;
  205. }
  206. static int __init parse_memopt(char *p)
  207. {
  208. if (!p)
  209. return -EINVAL;
  210. end_user_pfn = memparse(p, &p);
  211. end_user_pfn >>= PAGE_SHIFT;
  212. return 0;
  213. }
  214. early_param("mem", parse_memopt);
  215. static int userdef __initdata;
  216. static int __init parse_memmap_opt(char *p)
  217. {
  218. char *oldp;
  219. unsigned long long start_at, mem_size;
  220. if (!strcmp(p, "exactmap")) {
  221. #ifdef CONFIG_CRASH_DUMP
  222. /*
  223. * If we are doing a crash dump, we still need to know
  224. * the real mem size before original memory map is
  225. * reset.
  226. */
  227. e820_register_active_regions(0, 0, -1UL);
  228. saved_max_pfn = e820_end_of_ram();
  229. remove_all_active_ranges();
  230. #endif
  231. max_pfn_mapped = 0;
  232. e820.nr_map = 0;
  233. userdef = 1;
  234. return 0;
  235. }
  236. oldp = p;
  237. mem_size = memparse(p, &p);
  238. if (p == oldp)
  239. return -EINVAL;
  240. userdef = 1;
  241. if (*p == '@') {
  242. start_at = memparse(p+1, &p);
  243. add_memory_region(start_at, mem_size, E820_RAM);
  244. } else if (*p == '#') {
  245. start_at = memparse(p+1, &p);
  246. add_memory_region(start_at, mem_size, E820_ACPI);
  247. } else if (*p == '$') {
  248. start_at = memparse(p+1, &p);
  249. add_memory_region(start_at, mem_size, E820_RESERVED);
  250. } else {
  251. end_user_pfn = (mem_size >> PAGE_SHIFT);
  252. }
  253. return *p == '\0' ? 0 : -EINVAL;
  254. }
  255. early_param("memmap", parse_memmap_opt);
  256. void __init finish_e820_parsing(void)
  257. {
  258. if (userdef) {
  259. int nr = e820.nr_map;
  260. if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr) < 0)
  261. early_panic("Invalid user supplied memory map");
  262. e820.nr_map = nr;
  263. printk(KERN_INFO "user-defined physical RAM map:\n");
  264. e820_print_map("user");
  265. }
  266. }
  267. int __init arch_get_ram_range(int slot, u64 *addr, u64 *size)
  268. {
  269. int i;
  270. if (slot < 0 || slot >= e820.nr_map)
  271. return -1;
  272. for (i = slot; i < e820.nr_map; i++) {
  273. if (e820.map[i].type != E820_RAM)
  274. continue;
  275. break;
  276. }
  277. if (i == e820.nr_map || e820.map[i].addr > (max_pfn << PAGE_SHIFT))
  278. return -1;
  279. *addr = e820.map[i].addr;
  280. *size = min_t(u64, e820.map[i].size + e820.map[i].addr,
  281. max_pfn << PAGE_SHIFT) - *addr;
  282. return i + 1;
  283. }