e820.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 == E820MAX) {
  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, char *pnr_map)
  130. {
  131. struct change_member {
  132. struct e820entry *pbios; /* pointer to original bios entry */
  133. unsigned long long addr; /* address for this change point */
  134. };
  135. static struct change_member change_point_list[2*E820MAX] __initdata;
  136. static struct change_member *change_point[2*E820MAX] __initdata;
  137. static struct e820entry *overlap_list[E820MAX] __initdata;
  138. static struct e820entry new_bios[E820MAX] __initdata;
  139. struct change_member *change_tmp;
  140. unsigned long current_type, last_type;
  141. unsigned long long last_addr;
  142. int chgidx, still_changing;
  143. int overlap_entries;
  144. int new_bios_entry;
  145. int old_nr, new_nr, chg_nr;
  146. int i;
  147. /*
  148. Visually we're performing the following
  149. (1,2,3,4 = memory types)...
  150. Sample memory map (w/overlaps):
  151. ____22__________________
  152. ______________________4_
  153. ____1111________________
  154. _44_____________________
  155. 11111111________________
  156. ____________________33__
  157. ___________44___________
  158. __________33333_________
  159. ______________22________
  160. ___________________2222_
  161. _________111111111______
  162. _____________________11_
  163. _________________4______
  164. Sanitized equivalent (no overlap):
  165. 1_______________________
  166. _44_____________________
  167. ___1____________________
  168. ____22__________________
  169. ______11________________
  170. _________1______________
  171. __________3_____________
  172. ___________44___________
  173. _____________33_________
  174. _______________2________
  175. ________________1_______
  176. _________________4______
  177. ___________________2____
  178. ____________________33__
  179. ______________________4_
  180. */
  181. /* if there's only one memory region, don't bother */
  182. if (*pnr_map < 2)
  183. return -1;
  184. old_nr = *pnr_map;
  185. /* bail out if we find any unreasonable addresses in bios map */
  186. for (i = 0; i < old_nr; i++)
  187. if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
  188. return -1;
  189. /* create pointers for initial change-point information (for sorting) */
  190. for (i = 0; i < 2 * old_nr; i++)
  191. change_point[i] = &change_point_list[i];
  192. /* record all known change-points (starting and ending addresses),
  193. omitting those that are for empty memory regions */
  194. chgidx = 0;
  195. for (i = 0; i < old_nr; i++) {
  196. if (biosmap[i].size != 0) {
  197. change_point[chgidx]->addr = biosmap[i].addr;
  198. change_point[chgidx++]->pbios = &biosmap[i];
  199. change_point[chgidx]->addr = biosmap[i].addr +
  200. biosmap[i].size;
  201. change_point[chgidx++]->pbios = &biosmap[i];
  202. }
  203. }
  204. chg_nr = chgidx;
  205. /* sort change-point list by memory addresses (low -> high) */
  206. still_changing = 1;
  207. while (still_changing) {
  208. still_changing = 0;
  209. for (i = 1; i < chg_nr; i++) {
  210. unsigned long long curaddr, lastaddr;
  211. unsigned long long curpbaddr, lastpbaddr;
  212. curaddr = change_point[i]->addr;
  213. lastaddr = change_point[i - 1]->addr;
  214. curpbaddr = change_point[i]->pbios->addr;
  215. lastpbaddr = change_point[i - 1]->pbios->addr;
  216. /*
  217. * swap entries, when:
  218. *
  219. * curaddr > lastaddr or
  220. * curaddr == lastaddr and curaddr == curpbaddr and
  221. * lastaddr != lastpbaddr
  222. */
  223. if (curaddr < lastaddr ||
  224. (curaddr == lastaddr && curaddr == curpbaddr &&
  225. lastaddr != lastpbaddr)) {
  226. change_tmp = change_point[i];
  227. change_point[i] = change_point[i-1];
  228. change_point[i-1] = change_tmp;
  229. still_changing = 1;
  230. }
  231. }
  232. }
  233. /* create a new bios memory map, removing overlaps */
  234. overlap_entries = 0; /* number of entries in the overlap table */
  235. new_bios_entry = 0; /* index for creating new bios map entries */
  236. last_type = 0; /* start with undefined memory type */
  237. last_addr = 0; /* start with 0 as last starting address */
  238. /* loop through change-points, determining affect on the new bios map */
  239. for (chgidx = 0; chgidx < chg_nr; chgidx++) {
  240. /* keep track of all overlapping bios entries */
  241. if (change_point[chgidx]->addr ==
  242. change_point[chgidx]->pbios->addr) {
  243. /*
  244. * add map entry to overlap list (> 1 entry
  245. * implies an overlap)
  246. */
  247. overlap_list[overlap_entries++] =
  248. change_point[chgidx]->pbios;
  249. } else {
  250. /*
  251. * remove entry from list (order independent,
  252. * so swap with last)
  253. */
  254. for (i = 0; i < overlap_entries; i++) {
  255. if (overlap_list[i] ==
  256. change_point[chgidx]->pbios)
  257. overlap_list[i] =
  258. overlap_list[overlap_entries-1];
  259. }
  260. overlap_entries--;
  261. }
  262. /*
  263. * if there are overlapping entries, decide which
  264. * "type" to use (larger value takes precedence --
  265. * 1=usable, 2,3,4,4+=unusable)
  266. */
  267. current_type = 0;
  268. for (i = 0; i < overlap_entries; i++)
  269. if (overlap_list[i]->type > current_type)
  270. current_type = overlap_list[i]->type;
  271. /*
  272. * continue building up new bios map based on this
  273. * information
  274. */
  275. if (current_type != last_type) {
  276. if (last_type != 0) {
  277. new_bios[new_bios_entry].size =
  278. change_point[chgidx]->addr - last_addr;
  279. /*
  280. * move forward only if the new size
  281. * was non-zero
  282. */
  283. if (new_bios[new_bios_entry].size != 0)
  284. /*
  285. * no more space left for new
  286. * bios entries ?
  287. */
  288. if (++new_bios_entry >= E820MAX)
  289. break;
  290. }
  291. if (current_type != 0) {
  292. new_bios[new_bios_entry].addr =
  293. change_point[chgidx]->addr;
  294. new_bios[new_bios_entry].type = current_type;
  295. last_addr = change_point[chgidx]->addr;
  296. }
  297. last_type = current_type;
  298. }
  299. }
  300. /* retain count for new bios entries */
  301. new_nr = new_bios_entry;
  302. /* copy new bios mapping into original location */
  303. memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
  304. *pnr_map = new_nr;
  305. return 0;
  306. }
  307. /*
  308. * Copy the BIOS e820 map into a safe place.
  309. *
  310. * Sanity-check it while we're at it..
  311. *
  312. * If we're lucky and live on a modern system, the setup code
  313. * will have given us a memory map that we can use to properly
  314. * set up memory. If we aren't, we'll fake a memory map.
  315. */
  316. int __init copy_e820_map(struct e820entry *biosmap, int nr_map)
  317. {
  318. /* Only one memory region (or negative)? Ignore it */
  319. if (nr_map < 2)
  320. return -1;
  321. do {
  322. u64 start = biosmap->addr;
  323. u64 size = biosmap->size;
  324. u64 end = start + size;
  325. u32 type = biosmap->type;
  326. /* Overflow in 64 bits? Ignore the memory map. */
  327. if (start > end)
  328. return -1;
  329. add_memory_region(start, size, type);
  330. } while (biosmap++, --nr_map);
  331. return 0;
  332. }
  333. u64 __init update_memory_range(u64 start, u64 size, unsigned old_type,
  334. unsigned new_type)
  335. {
  336. int i;
  337. u64 real_updated_size = 0;
  338. BUG_ON(old_type == new_type);
  339. for (i = 0; i < e820.nr_map; i++) {
  340. struct e820entry *ei = &e820.map[i];
  341. u64 final_start, final_end;
  342. if (ei->type != old_type)
  343. continue;
  344. /* totally covered? */
  345. if (ei->addr >= start &&
  346. (ei->addr + ei->size) <= (start + size)) {
  347. ei->type = new_type;
  348. real_updated_size += ei->size;
  349. continue;
  350. }
  351. /* partially covered */
  352. final_start = max(start, ei->addr);
  353. final_end = min(start + size, ei->addr + ei->size);
  354. if (final_start >= final_end)
  355. continue;
  356. add_memory_region(final_start, final_end - final_start,
  357. new_type);
  358. real_updated_size += final_end - final_start;
  359. }
  360. return real_updated_size;
  361. }
  362. void __init update_e820(void)
  363. {
  364. u8 nr_map;
  365. nr_map = e820.nr_map;
  366. if (sanitize_e820_map(e820.map, &nr_map))
  367. return;
  368. e820.nr_map = nr_map;
  369. printk(KERN_INFO "modified physical RAM map:\n");
  370. e820_print_map("modified");
  371. }
  372. /*
  373. * Search for the biggest gap in the low 32 bits of the e820
  374. * memory space. We pass this space to PCI to assign MMIO resources
  375. * for hotplug or unconfigured devices in.
  376. * Hopefully the BIOS let enough space left.
  377. */
  378. __init void e820_setup_gap(void)
  379. {
  380. unsigned long gapstart, gapsize, round;
  381. unsigned long long last;
  382. int i;
  383. int found = 0;
  384. last = 0x100000000ull;
  385. gapstart = 0x10000000;
  386. gapsize = 0x400000;
  387. i = e820.nr_map;
  388. while (--i >= 0) {
  389. unsigned long long start = e820.map[i].addr;
  390. unsigned long long end = start + e820.map[i].size;
  391. /*
  392. * Since "last" is at most 4GB, we know we'll
  393. * fit in 32 bits if this condition is true
  394. */
  395. if (last > end) {
  396. unsigned long gap = last - end;
  397. if (gap > gapsize) {
  398. gapsize = gap;
  399. gapstart = end;
  400. found = 1;
  401. }
  402. }
  403. if (start < last)
  404. last = start;
  405. }
  406. #ifdef CONFIG_X86_64
  407. if (!found) {
  408. gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
  409. printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit "
  410. "address range\n"
  411. KERN_ERR "PCI: Unassigned devices with 32bit resource "
  412. "registers may break!\n");
  413. }
  414. #endif
  415. /*
  416. * See how much we want to round up: start off with
  417. * rounding to the next 1MB area.
  418. */
  419. round = 0x100000;
  420. while ((gapsize >> 4) > round)
  421. round += round;
  422. /* Fun with two's complement */
  423. pci_mem_start = (gapstart + round) & -round;
  424. printk(KERN_INFO
  425. "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
  426. pci_mem_start, gapstart, gapsize);
  427. }