e820_64.c 8.1 KB

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