e820.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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/pfn.h>
  21. #include <asm/pgtable.h>
  22. #include <asm/page.h>
  23. #include <asm/e820.h>
  24. #include <asm/setup.h>
  25. struct e820map e820;
  26. /* For PCI or other memory-mapped resources */
  27. unsigned long pci_mem_start = 0xaeedbabe;
  28. #ifdef CONFIG_PCI
  29. EXPORT_SYMBOL(pci_mem_start);
  30. #endif
  31. /*
  32. * This function checks if any part of the range <start,end> is mapped
  33. * with type.
  34. */
  35. int
  36. e820_any_mapped(u64 start, u64 end, unsigned type)
  37. {
  38. int i;
  39. for (i = 0; i < e820.nr_map; i++) {
  40. struct e820entry *ei = &e820.map[i];
  41. if (type && ei->type != type)
  42. continue;
  43. if (ei->addr >= end || ei->addr + ei->size <= start)
  44. continue;
  45. return 1;
  46. }
  47. return 0;
  48. }
  49. EXPORT_SYMBOL_GPL(e820_any_mapped);
  50. /*
  51. * This function checks if the entire range <start,end> is mapped with type.
  52. *
  53. * Note: this function only works correct if the e820 table is sorted and
  54. * not-overlapping, which is the case
  55. */
  56. int __init e820_all_mapped(u64 start, u64 end, unsigned type)
  57. {
  58. int i;
  59. for (i = 0; i < e820.nr_map; i++) {
  60. struct e820entry *ei = &e820.map[i];
  61. if (type && ei->type != type)
  62. continue;
  63. /* is the region (part) in overlap with the current region ?*/
  64. if (ei->addr >= end || ei->addr + ei->size <= start)
  65. continue;
  66. /* if the region is at the beginning of <start,end> we move
  67. * start to the end of the region since it's ok until there
  68. */
  69. if (ei->addr <= start)
  70. start = ei->addr + ei->size;
  71. /*
  72. * if start is now at or beyond end, we're done, full
  73. * coverage
  74. */
  75. if (start >= end)
  76. return 1;
  77. }
  78. return 0;
  79. }
  80. /*
  81. * Add a memory region to the kernel e820 map.
  82. */
  83. void __init add_memory_region(u64 start, u64 size, int type)
  84. {
  85. int x = e820.nr_map;
  86. if (x == ARRAY_SIZE(e820.map)) {
  87. printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
  88. return;
  89. }
  90. e820.map[x].addr = start;
  91. e820.map[x].size = size;
  92. e820.map[x].type = type;
  93. e820.nr_map++;
  94. }
  95. void __init e820_print_map(char *who)
  96. {
  97. int i;
  98. for (i = 0; i < e820.nr_map; i++) {
  99. printk(KERN_INFO " %s: %016Lx - %016Lx ", who,
  100. (unsigned long long) e820.map[i].addr,
  101. (unsigned long long)
  102. (e820.map[i].addr + e820.map[i].size));
  103. switch (e820.map[i].type) {
  104. case E820_RAM:
  105. printk(KERN_CONT "(usable)\n");
  106. break;
  107. case E820_RESERVED:
  108. printk(KERN_CONT "(reserved)\n");
  109. break;
  110. case E820_ACPI:
  111. printk(KERN_CONT "(ACPI data)\n");
  112. break;
  113. case E820_NVS:
  114. printk(KERN_CONT "(ACPI NVS)\n");
  115. break;
  116. default:
  117. printk(KERN_CONT "type %u\n", e820.map[i].type);
  118. break;
  119. }
  120. }
  121. }
  122. /*
  123. * Sanitize the BIOS e820 map.
  124. *
  125. * Some e820 responses include overlapping entries. The following
  126. * replaces the original e820 map with a new one, removing overlaps.
  127. *
  128. */
  129. int __init sanitize_e820_map(struct e820entry *biosmap, int max_nr_map,
  130. int *pnr_map)
  131. {
  132. struct change_member {
  133. struct e820entry *pbios; /* pointer to original bios entry */
  134. unsigned long long addr; /* address for this change point */
  135. };
  136. static struct change_member change_point_list[2*E820_X_MAX] __initdata;
  137. static struct change_member *change_point[2*E820_X_MAX] __initdata;
  138. static struct e820entry *overlap_list[E820_X_MAX] __initdata;
  139. static struct e820entry new_bios[E820_X_MAX] __initdata;
  140. struct change_member *change_tmp;
  141. unsigned long current_type, last_type;
  142. unsigned long long last_addr;
  143. int chgidx, still_changing;
  144. int overlap_entries;
  145. int new_bios_entry;
  146. int old_nr, new_nr, chg_nr;
  147. int i;
  148. /*
  149. Visually we're performing the following
  150. (1,2,3,4 = memory types)...
  151. Sample memory map (w/overlaps):
  152. ____22__________________
  153. ______________________4_
  154. ____1111________________
  155. _44_____________________
  156. 11111111________________
  157. ____________________33__
  158. ___________44___________
  159. __________33333_________
  160. ______________22________
  161. ___________________2222_
  162. _________111111111______
  163. _____________________11_
  164. _________________4______
  165. Sanitized equivalent (no overlap):
  166. 1_______________________
  167. _44_____________________
  168. ___1____________________
  169. ____22__________________
  170. ______11________________
  171. _________1______________
  172. __________3_____________
  173. ___________44___________
  174. _____________33_________
  175. _______________2________
  176. ________________1_______
  177. _________________4______
  178. ___________________2____
  179. ____________________33__
  180. ______________________4_
  181. */
  182. /* if there's only one memory region, don't bother */
  183. if (*pnr_map < 2)
  184. return -1;
  185. old_nr = *pnr_map;
  186. BUG_ON(old_nr > max_nr_map);
  187. /* bail out if we find any unreasonable addresses in bios map */
  188. for (i = 0; i < old_nr; i++)
  189. if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
  190. return -1;
  191. /* create pointers for initial change-point information (for sorting) */
  192. for (i = 0; i < 2 * old_nr; i++)
  193. change_point[i] = &change_point_list[i];
  194. /* record all known change-points (starting and ending addresses),
  195. omitting those that are for empty memory regions */
  196. chgidx = 0;
  197. for (i = 0; i < old_nr; i++) {
  198. if (biosmap[i].size != 0) {
  199. change_point[chgidx]->addr = biosmap[i].addr;
  200. change_point[chgidx++]->pbios = &biosmap[i];
  201. change_point[chgidx]->addr = biosmap[i].addr +
  202. biosmap[i].size;
  203. change_point[chgidx++]->pbios = &biosmap[i];
  204. }
  205. }
  206. chg_nr = chgidx;
  207. /* sort change-point list by memory addresses (low -> high) */
  208. still_changing = 1;
  209. while (still_changing) {
  210. still_changing = 0;
  211. for (i = 1; i < chg_nr; i++) {
  212. unsigned long long curaddr, lastaddr;
  213. unsigned long long curpbaddr, lastpbaddr;
  214. curaddr = change_point[i]->addr;
  215. lastaddr = change_point[i - 1]->addr;
  216. curpbaddr = change_point[i]->pbios->addr;
  217. lastpbaddr = change_point[i - 1]->pbios->addr;
  218. /*
  219. * swap entries, when:
  220. *
  221. * curaddr > lastaddr or
  222. * curaddr == lastaddr and curaddr == curpbaddr and
  223. * lastaddr != lastpbaddr
  224. */
  225. if (curaddr < lastaddr ||
  226. (curaddr == lastaddr && curaddr == curpbaddr &&
  227. lastaddr != lastpbaddr)) {
  228. change_tmp = change_point[i];
  229. change_point[i] = change_point[i-1];
  230. change_point[i-1] = change_tmp;
  231. still_changing = 1;
  232. }
  233. }
  234. }
  235. /* create a new bios memory map, removing overlaps */
  236. overlap_entries = 0; /* number of entries in the overlap table */
  237. new_bios_entry = 0; /* index for creating new bios map entries */
  238. last_type = 0; /* start with undefined memory type */
  239. last_addr = 0; /* start with 0 as last starting address */
  240. /* loop through change-points, determining affect on the new bios map */
  241. for (chgidx = 0; chgidx < chg_nr; chgidx++) {
  242. /* keep track of all overlapping bios entries */
  243. if (change_point[chgidx]->addr ==
  244. change_point[chgidx]->pbios->addr) {
  245. /*
  246. * add map entry to overlap list (> 1 entry
  247. * implies an overlap)
  248. */
  249. overlap_list[overlap_entries++] =
  250. change_point[chgidx]->pbios;
  251. } else {
  252. /*
  253. * remove entry from list (order independent,
  254. * so swap with last)
  255. */
  256. for (i = 0; i < overlap_entries; i++) {
  257. if (overlap_list[i] ==
  258. change_point[chgidx]->pbios)
  259. overlap_list[i] =
  260. overlap_list[overlap_entries-1];
  261. }
  262. overlap_entries--;
  263. }
  264. /*
  265. * if there are overlapping entries, decide which
  266. * "type" to use (larger value takes precedence --
  267. * 1=usable, 2,3,4,4+=unusable)
  268. */
  269. current_type = 0;
  270. for (i = 0; i < overlap_entries; i++)
  271. if (overlap_list[i]->type > current_type)
  272. current_type = overlap_list[i]->type;
  273. /*
  274. * continue building up new bios map based on this
  275. * information
  276. */
  277. if (current_type != last_type) {
  278. if (last_type != 0) {
  279. new_bios[new_bios_entry].size =
  280. change_point[chgidx]->addr - last_addr;
  281. /*
  282. * move forward only if the new size
  283. * was non-zero
  284. */
  285. if (new_bios[new_bios_entry].size != 0)
  286. /*
  287. * no more space left for new
  288. * bios entries ?
  289. */
  290. if (++new_bios_entry >= max_nr_map)
  291. break;
  292. }
  293. if (current_type != 0) {
  294. new_bios[new_bios_entry].addr =
  295. change_point[chgidx]->addr;
  296. new_bios[new_bios_entry].type = current_type;
  297. last_addr = change_point[chgidx]->addr;
  298. }
  299. last_type = current_type;
  300. }
  301. }
  302. /* retain count for new bios entries */
  303. new_nr = new_bios_entry;
  304. /* copy new bios mapping into original location */
  305. memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
  306. *pnr_map = new_nr;
  307. return 0;
  308. }
  309. /*
  310. * Copy the BIOS e820 map into a safe place.
  311. *
  312. * Sanity-check it while we're at it..
  313. *
  314. * If we're lucky and live on a modern system, the setup code
  315. * will have given us a memory map that we can use to properly
  316. * set up memory. If we aren't, we'll fake a memory map.
  317. */
  318. int __init copy_e820_map(struct e820entry *biosmap, int nr_map)
  319. {
  320. /* Only one memory region (or negative)? Ignore it */
  321. if (nr_map < 2)
  322. return -1;
  323. do {
  324. u64 start = biosmap->addr;
  325. u64 size = biosmap->size;
  326. u64 end = start + size;
  327. u32 type = biosmap->type;
  328. /* Overflow in 64 bits? Ignore the memory map. */
  329. if (start > end)
  330. return -1;
  331. add_memory_region(start, size, type);
  332. } while (biosmap++, --nr_map);
  333. return 0;
  334. }
  335. u64 __init update_memory_range(u64 start, u64 size, unsigned old_type,
  336. unsigned new_type)
  337. {
  338. int i;
  339. u64 real_updated_size = 0;
  340. BUG_ON(old_type == new_type);
  341. for (i = 0; i < e820.nr_map; i++) {
  342. struct e820entry *ei = &e820.map[i];
  343. u64 final_start, final_end;
  344. if (ei->type != old_type)
  345. continue;
  346. /* totally covered? */
  347. if (ei->addr >= start &&
  348. (ei->addr + ei->size) <= (start + size)) {
  349. ei->type = new_type;
  350. real_updated_size += ei->size;
  351. continue;
  352. }
  353. /* partially covered */
  354. final_start = max(start, ei->addr);
  355. final_end = min(start + size, ei->addr + ei->size);
  356. if (final_start >= final_end)
  357. continue;
  358. add_memory_region(final_start, final_end - final_start,
  359. new_type);
  360. real_updated_size += final_end - final_start;
  361. }
  362. return real_updated_size;
  363. }
  364. void __init update_e820(void)
  365. {
  366. int nr_map;
  367. nr_map = e820.nr_map;
  368. if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr_map))
  369. return;
  370. e820.nr_map = nr_map;
  371. printk(KERN_INFO "modified physical RAM map:\n");
  372. e820_print_map("modified");
  373. }
  374. /*
  375. * Search for the biggest gap in the low 32 bits of the e820
  376. * memory space. We pass this space to PCI to assign MMIO resources
  377. * for hotplug or unconfigured devices in.
  378. * Hopefully the BIOS let enough space left.
  379. */
  380. __init void e820_setup_gap(void)
  381. {
  382. unsigned long gapstart, gapsize, round;
  383. unsigned long long last;
  384. int i;
  385. int found = 0;
  386. last = 0x100000000ull;
  387. gapstart = 0x10000000;
  388. gapsize = 0x400000;
  389. i = e820.nr_map;
  390. while (--i >= 0) {
  391. unsigned long long start = e820.map[i].addr;
  392. unsigned long long end = start + e820.map[i].size;
  393. /*
  394. * Since "last" is at most 4GB, we know we'll
  395. * fit in 32 bits if this condition is true
  396. */
  397. if (last > end) {
  398. unsigned long gap = last - end;
  399. if (gap > gapsize) {
  400. gapsize = gap;
  401. gapstart = end;
  402. found = 1;
  403. }
  404. }
  405. if (start < last)
  406. last = start;
  407. }
  408. #ifdef CONFIG_X86_64
  409. if (!found) {
  410. gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
  411. printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit "
  412. "address range\n"
  413. KERN_ERR "PCI: Unassigned devices with 32bit resource "
  414. "registers may break!\n");
  415. }
  416. #endif
  417. /*
  418. * See how much we want to round up: start off with
  419. * rounding to the next 1MB area.
  420. */
  421. round = 0x100000;
  422. while ((gapsize >> 4) > round)
  423. round += round;
  424. /* Fun with two's complement */
  425. pci_mem_start = (gapstart + round) & -round;
  426. printk(KERN_INFO
  427. "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
  428. pci_mem_start, gapstart, gapsize);
  429. }