e820.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  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/crash_dump.h>
  15. #include <linux/export.h>
  16. #include <linux/bootmem.h>
  17. #include <linux/pfn.h>
  18. #include <linux/suspend.h>
  19. #include <linux/acpi.h>
  20. #include <linux/firmware-map.h>
  21. #include <linux/memblock.h>
  22. #include <linux/sort.h>
  23. #include <asm/e820.h>
  24. #include <asm/proto.h>
  25. #include <asm/setup.h>
  26. /*
  27. * The e820 map is the map that gets modified e.g. with command line parameters
  28. * and that is also registered with modifications in the kernel resource tree
  29. * with the iomem_resource as parent.
  30. *
  31. * The e820_saved is directly saved after the BIOS-provided memory map is
  32. * copied. It doesn't get modified afterwards. It's registered for the
  33. * /sys/firmware/memmap interface.
  34. *
  35. * That memory map is not modified and is used as base for kexec. The kexec'd
  36. * kernel should get the same memory map as the firmware provides. Then the
  37. * user can e.g. boot the original kernel with mem=1G while still booting the
  38. * next kernel with full memory.
  39. */
  40. struct e820map e820;
  41. struct e820map e820_saved;
  42. /* For PCI or other memory-mapped resources */
  43. unsigned long pci_mem_start = 0xaeedbabe;
  44. #ifdef CONFIG_PCI
  45. EXPORT_SYMBOL(pci_mem_start);
  46. #endif
  47. /*
  48. * This function checks if any part of the range <start,end> is mapped
  49. * with type.
  50. */
  51. int
  52. e820_any_mapped(u64 start, u64 end, unsigned type)
  53. {
  54. int i;
  55. for (i = 0; i < e820.nr_map; i++) {
  56. struct e820entry *ei = &e820.map[i];
  57. if (type && ei->type != type)
  58. continue;
  59. if (ei->addr >= end || ei->addr + ei->size <= start)
  60. continue;
  61. return 1;
  62. }
  63. return 0;
  64. }
  65. EXPORT_SYMBOL_GPL(e820_any_mapped);
  66. /*
  67. * This function checks if the entire range <start,end> is mapped with type.
  68. *
  69. * Note: this function only works correct if the e820 table is sorted and
  70. * not-overlapping, which is the case
  71. */
  72. int __init e820_all_mapped(u64 start, u64 end, unsigned type)
  73. {
  74. int i;
  75. for (i = 0; i < e820.nr_map; i++) {
  76. struct e820entry *ei = &e820.map[i];
  77. if (type && ei->type != type)
  78. continue;
  79. /* is the region (part) in overlap with the current region ?*/
  80. if (ei->addr >= end || ei->addr + ei->size <= start)
  81. continue;
  82. /* if the region is at the beginning of <start,end> we move
  83. * start to the end of the region since it's ok until there
  84. */
  85. if (ei->addr <= start)
  86. start = ei->addr + ei->size;
  87. /*
  88. * if start is now at or beyond end, we're done, full
  89. * coverage
  90. */
  91. if (start >= end)
  92. return 1;
  93. }
  94. return 0;
  95. }
  96. /*
  97. * Add a memory region to the kernel e820 map.
  98. */
  99. static void __init __e820_add_region(struct e820map *e820x, u64 start, u64 size,
  100. int type)
  101. {
  102. int x = e820x->nr_map;
  103. if (x >= ARRAY_SIZE(e820x->map)) {
  104. printk(KERN_ERR "e820: too many entries; ignoring [mem %#010llx-%#010llx]\n",
  105. (unsigned long long) start,
  106. (unsigned long long) (start + size - 1));
  107. return;
  108. }
  109. e820x->map[x].addr = start;
  110. e820x->map[x].size = size;
  111. e820x->map[x].type = type;
  112. e820x->nr_map++;
  113. }
  114. void __init e820_add_region(u64 start, u64 size, int type)
  115. {
  116. __e820_add_region(&e820, start, size, type);
  117. }
  118. static void __init e820_print_type(u32 type)
  119. {
  120. switch (type) {
  121. case E820_RAM:
  122. case E820_RESERVED_KERN:
  123. printk(KERN_CONT "usable");
  124. break;
  125. case E820_RESERVED:
  126. printk(KERN_CONT "reserved");
  127. break;
  128. case E820_ACPI:
  129. printk(KERN_CONT "ACPI data");
  130. break;
  131. case E820_NVS:
  132. printk(KERN_CONT "ACPI NVS");
  133. break;
  134. case E820_UNUSABLE:
  135. printk(KERN_CONT "unusable");
  136. break;
  137. case E820_PRAM:
  138. printk(KERN_CONT "persistent (type %u)", type);
  139. break;
  140. default:
  141. printk(KERN_CONT "type %u", type);
  142. break;
  143. }
  144. }
  145. void __init e820_print_map(char *who)
  146. {
  147. int i;
  148. for (i = 0; i < e820.nr_map; i++) {
  149. printk(KERN_INFO "%s: [mem %#018Lx-%#018Lx] ", who,
  150. (unsigned long long) e820.map[i].addr,
  151. (unsigned long long)
  152. (e820.map[i].addr + e820.map[i].size - 1));
  153. e820_print_type(e820.map[i].type);
  154. printk(KERN_CONT "\n");
  155. }
  156. }
  157. /*
  158. * Sanitize the BIOS e820 map.
  159. *
  160. * Some e820 responses include overlapping entries. The following
  161. * replaces the original e820 map with a new one, removing overlaps,
  162. * and resolving conflicting memory types in favor of highest
  163. * numbered type.
  164. *
  165. * The input parameter biosmap points to an array of 'struct
  166. * e820entry' which on entry has elements in the range [0, *pnr_map)
  167. * valid, and which has space for up to max_nr_map entries.
  168. * On return, the resulting sanitized e820 map entries will be in
  169. * overwritten in the same location, starting at biosmap.
  170. *
  171. * The integer pointed to by pnr_map must be valid on entry (the
  172. * current number of valid entries located at biosmap). If the
  173. * sanitizing succeeds the *pnr_map will be updated with the new
  174. * number of valid entries (something no more than max_nr_map).
  175. *
  176. * The return value from sanitize_e820_map() is zero if it
  177. * successfully 'sanitized' the map entries passed in, and is -1
  178. * if it did nothing, which can happen if either of (1) it was
  179. * only passed one map entry, or (2) any of the input map entries
  180. * were invalid (start + size < start, meaning that the size was
  181. * so big the described memory range wrapped around through zero.)
  182. *
  183. * Visually we're performing the following
  184. * (1,2,3,4 = memory types)...
  185. *
  186. * Sample memory map (w/overlaps):
  187. * ____22__________________
  188. * ______________________4_
  189. * ____1111________________
  190. * _44_____________________
  191. * 11111111________________
  192. * ____________________33__
  193. * ___________44___________
  194. * __________33333_________
  195. * ______________22________
  196. * ___________________2222_
  197. * _________111111111______
  198. * _____________________11_
  199. * _________________4______
  200. *
  201. * Sanitized equivalent (no overlap):
  202. * 1_______________________
  203. * _44_____________________
  204. * ___1____________________
  205. * ____22__________________
  206. * ______11________________
  207. * _________1______________
  208. * __________3_____________
  209. * ___________44___________
  210. * _____________33_________
  211. * _______________2________
  212. * ________________1_______
  213. * _________________4______
  214. * ___________________2____
  215. * ____________________33__
  216. * ______________________4_
  217. */
  218. struct change_member {
  219. struct e820entry *pbios; /* pointer to original bios entry */
  220. unsigned long long addr; /* address for this change point */
  221. };
  222. static int __init cpcompare(const void *a, const void *b)
  223. {
  224. struct change_member * const *app = a, * const *bpp = b;
  225. const struct change_member *ap = *app, *bp = *bpp;
  226. /*
  227. * Inputs are pointers to two elements of change_point[]. If their
  228. * addresses are unequal, their difference dominates. If the addresses
  229. * are equal, then consider one that represents the end of its region
  230. * to be greater than one that does not.
  231. */
  232. if (ap->addr != bp->addr)
  233. return ap->addr > bp->addr ? 1 : -1;
  234. return (ap->addr != ap->pbios->addr) - (bp->addr != bp->pbios->addr);
  235. }
  236. int __init sanitize_e820_map(struct e820entry *biosmap, int max_nr_map,
  237. u32 *pnr_map)
  238. {
  239. static struct change_member change_point_list[2*E820_X_MAX] __initdata;
  240. static struct change_member *change_point[2*E820_X_MAX] __initdata;
  241. static struct e820entry *overlap_list[E820_X_MAX] __initdata;
  242. static struct e820entry new_bios[E820_X_MAX] __initdata;
  243. unsigned long current_type, last_type;
  244. unsigned long long last_addr;
  245. int chgidx;
  246. int overlap_entries;
  247. int new_bios_entry;
  248. int old_nr, new_nr, chg_nr;
  249. int i;
  250. /* if there's only one memory region, don't bother */
  251. if (*pnr_map < 2)
  252. return -1;
  253. old_nr = *pnr_map;
  254. BUG_ON(old_nr > max_nr_map);
  255. /* bail out if we find any unreasonable addresses in bios map */
  256. for (i = 0; i < old_nr; i++)
  257. if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
  258. return -1;
  259. /* create pointers for initial change-point information (for sorting) */
  260. for (i = 0; i < 2 * old_nr; i++)
  261. change_point[i] = &change_point_list[i];
  262. /* record all known change-points (starting and ending addresses),
  263. omitting those that are for empty memory regions */
  264. chgidx = 0;
  265. for (i = 0; i < old_nr; i++) {
  266. if (biosmap[i].size != 0) {
  267. change_point[chgidx]->addr = biosmap[i].addr;
  268. change_point[chgidx++]->pbios = &biosmap[i];
  269. change_point[chgidx]->addr = biosmap[i].addr +
  270. biosmap[i].size;
  271. change_point[chgidx++]->pbios = &biosmap[i];
  272. }
  273. }
  274. chg_nr = chgidx;
  275. /* sort change-point list by memory addresses (low -> high) */
  276. sort(change_point, chg_nr, sizeof *change_point, cpcompare, NULL);
  277. /* create a new bios memory map, removing overlaps */
  278. overlap_entries = 0; /* number of entries in the overlap table */
  279. new_bios_entry = 0; /* index for creating new bios map entries */
  280. last_type = 0; /* start with undefined memory type */
  281. last_addr = 0; /* start with 0 as last starting address */
  282. /* loop through change-points, determining affect on the new bios map */
  283. for (chgidx = 0; chgidx < chg_nr; chgidx++) {
  284. /* keep track of all overlapping bios entries */
  285. if (change_point[chgidx]->addr ==
  286. change_point[chgidx]->pbios->addr) {
  287. /*
  288. * add map entry to overlap list (> 1 entry
  289. * implies an overlap)
  290. */
  291. overlap_list[overlap_entries++] =
  292. change_point[chgidx]->pbios;
  293. } else {
  294. /*
  295. * remove entry from list (order independent,
  296. * so swap with last)
  297. */
  298. for (i = 0; i < overlap_entries; i++) {
  299. if (overlap_list[i] ==
  300. change_point[chgidx]->pbios)
  301. overlap_list[i] =
  302. overlap_list[overlap_entries-1];
  303. }
  304. overlap_entries--;
  305. }
  306. /*
  307. * if there are overlapping entries, decide which
  308. * "type" to use (larger value takes precedence --
  309. * 1=usable, 2,3,4,4+=unusable)
  310. */
  311. current_type = 0;
  312. for (i = 0; i < overlap_entries; i++)
  313. if (overlap_list[i]->type > current_type)
  314. current_type = overlap_list[i]->type;
  315. /*
  316. * continue building up new bios map based on this
  317. * information
  318. */
  319. if (current_type != last_type || current_type == E820_PRAM) {
  320. if (last_type != 0) {
  321. new_bios[new_bios_entry].size =
  322. change_point[chgidx]->addr - last_addr;
  323. /*
  324. * move forward only if the new size
  325. * was non-zero
  326. */
  327. if (new_bios[new_bios_entry].size != 0)
  328. /*
  329. * no more space left for new
  330. * bios entries ?
  331. */
  332. if (++new_bios_entry >= max_nr_map)
  333. break;
  334. }
  335. if (current_type != 0) {
  336. new_bios[new_bios_entry].addr =
  337. change_point[chgidx]->addr;
  338. new_bios[new_bios_entry].type = current_type;
  339. last_addr = change_point[chgidx]->addr;
  340. }
  341. last_type = current_type;
  342. }
  343. }
  344. /* retain count for new bios entries */
  345. new_nr = new_bios_entry;
  346. /* copy new bios mapping into original location */
  347. memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
  348. *pnr_map = new_nr;
  349. return 0;
  350. }
  351. static int __init __append_e820_map(struct e820entry *biosmap, int nr_map)
  352. {
  353. while (nr_map) {
  354. u64 start = biosmap->addr;
  355. u64 size = biosmap->size;
  356. u64 end = start + size;
  357. u32 type = biosmap->type;
  358. /* Overflow in 64 bits? Ignore the memory map. */
  359. if (start > end)
  360. return -1;
  361. e820_add_region(start, size, type);
  362. biosmap++;
  363. nr_map--;
  364. }
  365. return 0;
  366. }
  367. /*
  368. * Copy the BIOS e820 map into a safe place.
  369. *
  370. * Sanity-check it while we're at it..
  371. *
  372. * If we're lucky and live on a modern system, the setup code
  373. * will have given us a memory map that we can use to properly
  374. * set up memory. If we aren't, we'll fake a memory map.
  375. */
  376. static int __init append_e820_map(struct e820entry *biosmap, int nr_map)
  377. {
  378. /* Only one memory region (or negative)? Ignore it */
  379. if (nr_map < 2)
  380. return -1;
  381. return __append_e820_map(biosmap, nr_map);
  382. }
  383. static u64 __init __e820_update_range(struct e820map *e820x, u64 start,
  384. u64 size, unsigned old_type,
  385. unsigned new_type)
  386. {
  387. u64 end;
  388. unsigned int i;
  389. u64 real_updated_size = 0;
  390. BUG_ON(old_type == new_type);
  391. if (size > (ULLONG_MAX - start))
  392. size = ULLONG_MAX - start;
  393. end = start + size;
  394. printk(KERN_DEBUG "e820: update [mem %#010Lx-%#010Lx] ",
  395. (unsigned long long) start, (unsigned long long) (end - 1));
  396. e820_print_type(old_type);
  397. printk(KERN_CONT " ==> ");
  398. e820_print_type(new_type);
  399. printk(KERN_CONT "\n");
  400. for (i = 0; i < e820x->nr_map; i++) {
  401. struct e820entry *ei = &e820x->map[i];
  402. u64 final_start, final_end;
  403. u64 ei_end;
  404. if (ei->type != old_type)
  405. continue;
  406. ei_end = ei->addr + ei->size;
  407. /* totally covered by new range? */
  408. if (ei->addr >= start && ei_end <= end) {
  409. ei->type = new_type;
  410. real_updated_size += ei->size;
  411. continue;
  412. }
  413. /* new range is totally covered? */
  414. if (ei->addr < start && ei_end > end) {
  415. __e820_add_region(e820x, start, size, new_type);
  416. __e820_add_region(e820x, end, ei_end - end, ei->type);
  417. ei->size = start - ei->addr;
  418. real_updated_size += size;
  419. continue;
  420. }
  421. /* partially covered */
  422. final_start = max(start, ei->addr);
  423. final_end = min(end, ei_end);
  424. if (final_start >= final_end)
  425. continue;
  426. __e820_add_region(e820x, final_start, final_end - final_start,
  427. new_type);
  428. real_updated_size += final_end - final_start;
  429. /*
  430. * left range could be head or tail, so need to update
  431. * size at first.
  432. */
  433. ei->size -= final_end - final_start;
  434. if (ei->addr < final_start)
  435. continue;
  436. ei->addr = final_end;
  437. }
  438. return real_updated_size;
  439. }
  440. u64 __init e820_update_range(u64 start, u64 size, unsigned old_type,
  441. unsigned new_type)
  442. {
  443. return __e820_update_range(&e820, start, size, old_type, new_type);
  444. }
  445. static u64 __init e820_update_range_saved(u64 start, u64 size,
  446. unsigned old_type, unsigned new_type)
  447. {
  448. return __e820_update_range(&e820_saved, start, size, old_type,
  449. new_type);
  450. }
  451. /* make e820 not cover the range */
  452. u64 __init e820_remove_range(u64 start, u64 size, unsigned old_type,
  453. int checktype)
  454. {
  455. int i;
  456. u64 end;
  457. u64 real_removed_size = 0;
  458. if (size > (ULLONG_MAX - start))
  459. size = ULLONG_MAX - start;
  460. end = start + size;
  461. printk(KERN_DEBUG "e820: remove [mem %#010Lx-%#010Lx] ",
  462. (unsigned long long) start, (unsigned long long) (end - 1));
  463. if (checktype)
  464. e820_print_type(old_type);
  465. printk(KERN_CONT "\n");
  466. for (i = 0; i < e820.nr_map; i++) {
  467. struct e820entry *ei = &e820.map[i];
  468. u64 final_start, final_end;
  469. u64 ei_end;
  470. if (checktype && ei->type != old_type)
  471. continue;
  472. ei_end = ei->addr + ei->size;
  473. /* totally covered? */
  474. if (ei->addr >= start && ei_end <= end) {
  475. real_removed_size += ei->size;
  476. memset(ei, 0, sizeof(struct e820entry));
  477. continue;
  478. }
  479. /* new range is totally covered? */
  480. if (ei->addr < start && ei_end > end) {
  481. e820_add_region(end, ei_end - end, ei->type);
  482. ei->size = start - ei->addr;
  483. real_removed_size += size;
  484. continue;
  485. }
  486. /* partially covered */
  487. final_start = max(start, ei->addr);
  488. final_end = min(end, ei_end);
  489. if (final_start >= final_end)
  490. continue;
  491. real_removed_size += final_end - final_start;
  492. /*
  493. * left range could be head or tail, so need to update
  494. * size at first.
  495. */
  496. ei->size -= final_end - final_start;
  497. if (ei->addr < final_start)
  498. continue;
  499. ei->addr = final_end;
  500. }
  501. return real_removed_size;
  502. }
  503. void __init update_e820(void)
  504. {
  505. if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map))
  506. return;
  507. printk(KERN_INFO "e820: modified physical RAM map:\n");
  508. e820_print_map("modified");
  509. }
  510. static void __init update_e820_saved(void)
  511. {
  512. sanitize_e820_map(e820_saved.map, ARRAY_SIZE(e820_saved.map),
  513. &e820_saved.nr_map);
  514. }
  515. #define MAX_GAP_END 0x100000000ull
  516. /*
  517. * Search for a gap in the e820 memory space from start_addr to end_addr.
  518. */
  519. __init int e820_search_gap(unsigned long *gapstart, unsigned long *gapsize,
  520. unsigned long start_addr, unsigned long long end_addr)
  521. {
  522. unsigned long long last;
  523. int i = e820.nr_map;
  524. int found = 0;
  525. last = (end_addr && end_addr < MAX_GAP_END) ? end_addr : MAX_GAP_END;
  526. while (--i >= 0) {
  527. unsigned long long start = e820.map[i].addr;
  528. unsigned long long end = start + e820.map[i].size;
  529. if (end < start_addr)
  530. continue;
  531. /*
  532. * Since "last" is at most 4GB, we know we'll
  533. * fit in 32 bits if this condition is true
  534. */
  535. if (last > end) {
  536. unsigned long gap = last - end;
  537. if (gap >= *gapsize) {
  538. *gapsize = gap;
  539. *gapstart = end;
  540. found = 1;
  541. }
  542. }
  543. if (start < last)
  544. last = start;
  545. }
  546. return found;
  547. }
  548. /*
  549. * Search for the biggest gap in the low 32 bits of the e820
  550. * memory space. We pass this space to PCI to assign MMIO resources
  551. * for hotplug or unconfigured devices in.
  552. * Hopefully the BIOS let enough space left.
  553. */
  554. __init void e820_setup_gap(void)
  555. {
  556. unsigned long gapstart, gapsize;
  557. int found;
  558. gapstart = 0x10000000;
  559. gapsize = 0x400000;
  560. found = e820_search_gap(&gapstart, &gapsize, 0, MAX_GAP_END);
  561. #ifdef CONFIG_X86_64
  562. if (!found) {
  563. gapstart = (max_pfn << PAGE_SHIFT) + 1024*1024;
  564. printk(KERN_ERR
  565. "e820: cannot find a gap in the 32bit address range\n"
  566. "e820: PCI devices with unassigned 32bit BARs may break!\n");
  567. }
  568. #endif
  569. /*
  570. * e820_reserve_resources_late protect stolen RAM already
  571. */
  572. pci_mem_start = gapstart;
  573. printk(KERN_INFO
  574. "e820: [mem %#010lx-%#010lx] available for PCI devices\n",
  575. gapstart, gapstart + gapsize - 1);
  576. }
  577. /**
  578. * Because of the size limitation of struct boot_params, only first
  579. * 128 E820 memory entries are passed to kernel via
  580. * boot_params.e820_map, others are passed via SETUP_E820_EXT node of
  581. * linked list of struct setup_data, which is parsed here.
  582. */
  583. void __init parse_e820_ext(u64 phys_addr, u32 data_len)
  584. {
  585. int entries;
  586. struct e820entry *extmap;
  587. struct setup_data *sdata;
  588. sdata = early_memremap(phys_addr, data_len);
  589. entries = sdata->len / sizeof(struct e820entry);
  590. extmap = (struct e820entry *)(sdata->data);
  591. __append_e820_map(extmap, entries);
  592. sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
  593. early_memunmap(sdata, data_len);
  594. printk(KERN_INFO "e820: extended physical RAM map:\n");
  595. e820_print_map("extended");
  596. }
  597. #if defined(CONFIG_X86_64) || \
  598. (defined(CONFIG_X86_32) && defined(CONFIG_HIBERNATION))
  599. /**
  600. * Find the ranges of physical addresses that do not correspond to
  601. * e820 RAM areas and mark the corresponding pages as nosave for
  602. * hibernation (32 bit) or software suspend and suspend to RAM (64 bit).
  603. *
  604. * This function requires the e820 map to be sorted and without any
  605. * overlapping entries.
  606. */
  607. void __init e820_mark_nosave_regions(unsigned long limit_pfn)
  608. {
  609. int i;
  610. unsigned long pfn = 0;
  611. for (i = 0; i < e820.nr_map; i++) {
  612. struct e820entry *ei = &e820.map[i];
  613. if (pfn < PFN_UP(ei->addr))
  614. register_nosave_region(pfn, PFN_UP(ei->addr));
  615. pfn = PFN_DOWN(ei->addr + ei->size);
  616. if (ei->type != E820_RAM && ei->type != E820_RESERVED_KERN)
  617. register_nosave_region(PFN_UP(ei->addr), pfn);
  618. if (pfn >= limit_pfn)
  619. break;
  620. }
  621. }
  622. #endif
  623. #ifdef CONFIG_ACPI
  624. /**
  625. * Mark ACPI NVS memory region, so that we can save/restore it during
  626. * hibernation and the subsequent resume.
  627. */
  628. static int __init e820_mark_nvs_memory(void)
  629. {
  630. int i;
  631. for (i = 0; i < e820.nr_map; i++) {
  632. struct e820entry *ei = &e820.map[i];
  633. if (ei->type == E820_NVS)
  634. acpi_nvs_register(ei->addr, ei->size);
  635. }
  636. return 0;
  637. }
  638. core_initcall(e820_mark_nvs_memory);
  639. #endif
  640. /*
  641. * pre allocated 4k and reserved it in memblock and e820_saved
  642. */
  643. u64 __init early_reserve_e820(u64 size, u64 align)
  644. {
  645. u64 addr;
  646. addr = __memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
  647. if (addr) {
  648. e820_update_range_saved(addr, size, E820_RAM, E820_RESERVED);
  649. printk(KERN_INFO "e820: update e820_saved for early_reserve_e820\n");
  650. update_e820_saved();
  651. }
  652. return addr;
  653. }
  654. #ifdef CONFIG_X86_32
  655. # ifdef CONFIG_X86_PAE
  656. # define MAX_ARCH_PFN (1ULL<<(36-PAGE_SHIFT))
  657. # else
  658. # define MAX_ARCH_PFN (1ULL<<(32-PAGE_SHIFT))
  659. # endif
  660. #else /* CONFIG_X86_32 */
  661. # define MAX_ARCH_PFN MAXMEM>>PAGE_SHIFT
  662. #endif
  663. /*
  664. * Find the highest page frame number we have available
  665. */
  666. static unsigned long __init e820_end_pfn(unsigned long limit_pfn)
  667. {
  668. int i;
  669. unsigned long last_pfn = 0;
  670. unsigned long max_arch_pfn = MAX_ARCH_PFN;
  671. for (i = 0; i < e820.nr_map; i++) {
  672. struct e820entry *ei = &e820.map[i];
  673. unsigned long start_pfn;
  674. unsigned long end_pfn;
  675. /*
  676. * Persistent memory is accounted as ram for purposes of
  677. * establishing max_pfn and mem_map.
  678. */
  679. if (ei->type != E820_RAM && ei->type != E820_PRAM)
  680. continue;
  681. start_pfn = ei->addr >> PAGE_SHIFT;
  682. end_pfn = (ei->addr + ei->size) >> PAGE_SHIFT;
  683. if (start_pfn >= limit_pfn)
  684. continue;
  685. if (end_pfn > limit_pfn) {
  686. last_pfn = limit_pfn;
  687. break;
  688. }
  689. if (end_pfn > last_pfn)
  690. last_pfn = end_pfn;
  691. }
  692. if (last_pfn > max_arch_pfn)
  693. last_pfn = max_arch_pfn;
  694. printk(KERN_INFO "e820: last_pfn = %#lx max_arch_pfn = %#lx\n",
  695. last_pfn, max_arch_pfn);
  696. return last_pfn;
  697. }
  698. unsigned long __init e820_end_of_ram_pfn(void)
  699. {
  700. return e820_end_pfn(MAX_ARCH_PFN);
  701. }
  702. unsigned long __init e820_end_of_low_ram_pfn(void)
  703. {
  704. return e820_end_pfn(1UL << (32-PAGE_SHIFT));
  705. }
  706. static void early_panic(char *msg)
  707. {
  708. early_printk(msg);
  709. panic(msg);
  710. }
  711. static int userdef __initdata;
  712. /* "mem=nopentium" disables the 4MB page tables. */
  713. static int __init parse_memopt(char *p)
  714. {
  715. u64 mem_size;
  716. if (!p)
  717. return -EINVAL;
  718. if (!strcmp(p, "nopentium")) {
  719. #ifdef CONFIG_X86_32
  720. setup_clear_cpu_cap(X86_FEATURE_PSE);
  721. return 0;
  722. #else
  723. printk(KERN_WARNING "mem=nopentium ignored! (only supported on x86_32)\n");
  724. return -EINVAL;
  725. #endif
  726. }
  727. userdef = 1;
  728. mem_size = memparse(p, &p);
  729. /* don't remove all of memory when handling "mem={invalid}" param */
  730. if (mem_size == 0)
  731. return -EINVAL;
  732. e820_remove_range(mem_size, ULLONG_MAX - mem_size, E820_RAM, 1);
  733. return 0;
  734. }
  735. early_param("mem", parse_memopt);
  736. static int __init parse_memmap_one(char *p)
  737. {
  738. char *oldp;
  739. u64 start_at, mem_size;
  740. if (!p)
  741. return -EINVAL;
  742. if (!strncmp(p, "exactmap", 8)) {
  743. #ifdef CONFIG_CRASH_DUMP
  744. /*
  745. * If we are doing a crash dump, we still need to know
  746. * the real mem size before original memory map is
  747. * reset.
  748. */
  749. saved_max_pfn = e820_end_of_ram_pfn();
  750. #endif
  751. e820.nr_map = 0;
  752. userdef = 1;
  753. return 0;
  754. }
  755. oldp = p;
  756. mem_size = memparse(p, &p);
  757. if (p == oldp)
  758. return -EINVAL;
  759. userdef = 1;
  760. if (*p == '@') {
  761. start_at = memparse(p+1, &p);
  762. e820_add_region(start_at, mem_size, E820_RAM);
  763. } else if (*p == '#') {
  764. start_at = memparse(p+1, &p);
  765. e820_add_region(start_at, mem_size, E820_ACPI);
  766. } else if (*p == '$') {
  767. start_at = memparse(p+1, &p);
  768. e820_add_region(start_at, mem_size, E820_RESERVED);
  769. } else if (*p == '!') {
  770. start_at = memparse(p+1, &p);
  771. e820_add_region(start_at, mem_size, E820_PRAM);
  772. } else
  773. e820_remove_range(mem_size, ULLONG_MAX - mem_size, E820_RAM, 1);
  774. return *p == '\0' ? 0 : -EINVAL;
  775. }
  776. static int __init parse_memmap_opt(char *str)
  777. {
  778. while (str) {
  779. char *k = strchr(str, ',');
  780. if (k)
  781. *k++ = 0;
  782. parse_memmap_one(str);
  783. str = k;
  784. }
  785. return 0;
  786. }
  787. early_param("memmap", parse_memmap_opt);
  788. void __init finish_e820_parsing(void)
  789. {
  790. if (userdef) {
  791. if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map),
  792. &e820.nr_map) < 0)
  793. early_panic("Invalid user supplied memory map");
  794. printk(KERN_INFO "e820: user-defined physical RAM map:\n");
  795. e820_print_map("user");
  796. }
  797. }
  798. static inline const char *e820_type_to_string(int e820_type)
  799. {
  800. switch (e820_type) {
  801. case E820_RESERVED_KERN:
  802. case E820_RAM: return "System RAM";
  803. case E820_ACPI: return "ACPI Tables";
  804. case E820_NVS: return "ACPI Non-volatile Storage";
  805. case E820_UNUSABLE: return "Unusable memory";
  806. case E820_PRAM: return "Persistent RAM";
  807. default: return "reserved";
  808. }
  809. }
  810. /*
  811. * Mark e820 reserved areas as busy for the resource manager.
  812. */
  813. static struct resource __initdata *e820_res;
  814. void __init e820_reserve_resources(void)
  815. {
  816. int i;
  817. struct resource *res;
  818. u64 end;
  819. res = alloc_bootmem(sizeof(struct resource) * e820.nr_map);
  820. e820_res = res;
  821. for (i = 0; i < e820.nr_map; i++) {
  822. end = e820.map[i].addr + e820.map[i].size - 1;
  823. if (end != (resource_size_t)end) {
  824. res++;
  825. continue;
  826. }
  827. res->name = e820_type_to_string(e820.map[i].type);
  828. res->start = e820.map[i].addr;
  829. res->end = end;
  830. res->flags = IORESOURCE_MEM;
  831. /*
  832. * don't register the region that could be conflicted with
  833. * pci device BAR resource and insert them later in
  834. * pcibios_resource_survey()
  835. */
  836. if (((e820.map[i].type != E820_RESERVED) &&
  837. (e820.map[i].type != E820_PRAM)) ||
  838. res->start < (1ULL<<20)) {
  839. res->flags |= IORESOURCE_BUSY;
  840. insert_resource(&iomem_resource, res);
  841. }
  842. res++;
  843. }
  844. for (i = 0; i < e820_saved.nr_map; i++) {
  845. struct e820entry *entry = &e820_saved.map[i];
  846. firmware_map_add_early(entry->addr,
  847. entry->addr + entry->size,
  848. e820_type_to_string(entry->type));
  849. }
  850. }
  851. /* How much should we pad RAM ending depending on where it is? */
  852. static unsigned long ram_alignment(resource_size_t pos)
  853. {
  854. unsigned long mb = pos >> 20;
  855. /* To 64kB in the first megabyte */
  856. if (!mb)
  857. return 64*1024;
  858. /* To 1MB in the first 16MB */
  859. if (mb < 16)
  860. return 1024*1024;
  861. /* To 64MB for anything above that */
  862. return 64*1024*1024;
  863. }
  864. #define MAX_RESOURCE_SIZE ((resource_size_t)-1)
  865. void __init e820_reserve_resources_late(void)
  866. {
  867. int i;
  868. struct resource *res;
  869. res = e820_res;
  870. for (i = 0; i < e820.nr_map; i++) {
  871. if (!res->parent && res->end)
  872. insert_resource_expand_to_fit(&iomem_resource, res);
  873. res++;
  874. }
  875. /*
  876. * Try to bump up RAM regions to reasonable boundaries to
  877. * avoid stolen RAM:
  878. */
  879. for (i = 0; i < e820.nr_map; i++) {
  880. struct e820entry *entry = &e820.map[i];
  881. u64 start, end;
  882. if (entry->type != E820_RAM)
  883. continue;
  884. start = entry->addr + entry->size;
  885. end = round_up(start, ram_alignment(start)) - 1;
  886. if (end > MAX_RESOURCE_SIZE)
  887. end = MAX_RESOURCE_SIZE;
  888. if (start >= end)
  889. continue;
  890. printk(KERN_DEBUG
  891. "e820: reserve RAM buffer [mem %#010llx-%#010llx]\n",
  892. start, end);
  893. reserve_region_with_split(&iomem_resource, start, end,
  894. "RAM buffer");
  895. }
  896. }
  897. char *__init default_machine_specific_memory_setup(void)
  898. {
  899. char *who = "BIOS-e820";
  900. u32 new_nr;
  901. /*
  902. * Try to copy the BIOS-supplied E820-map.
  903. *
  904. * Otherwise fake a memory map; one section from 0k->640k,
  905. * the next section from 1mb->appropriate_mem_k
  906. */
  907. new_nr = boot_params.e820_entries;
  908. sanitize_e820_map(boot_params.e820_map,
  909. ARRAY_SIZE(boot_params.e820_map),
  910. &new_nr);
  911. boot_params.e820_entries = new_nr;
  912. if (append_e820_map(boot_params.e820_map, boot_params.e820_entries)
  913. < 0) {
  914. u64 mem_size;
  915. /* compare results from other methods and take the greater */
  916. if (boot_params.alt_mem_k
  917. < boot_params.screen_info.ext_mem_k) {
  918. mem_size = boot_params.screen_info.ext_mem_k;
  919. who = "BIOS-88";
  920. } else {
  921. mem_size = boot_params.alt_mem_k;
  922. who = "BIOS-e801";
  923. }
  924. e820.nr_map = 0;
  925. e820_add_region(0, LOWMEMSIZE(), E820_RAM);
  926. e820_add_region(HIGH_MEMORY, mem_size << 10, E820_RAM);
  927. }
  928. /* In case someone cares... */
  929. return who;
  930. }
  931. void __init setup_memory_map(void)
  932. {
  933. char *who;
  934. who = x86_init.resources.memory_setup();
  935. memcpy(&e820_saved, &e820, sizeof(struct e820map));
  936. printk(KERN_INFO "e820: BIOS-provided physical RAM map:\n");
  937. e820_print_map(who);
  938. }
  939. void __init memblock_x86_fill(void)
  940. {
  941. int i;
  942. u64 end;
  943. /*
  944. * EFI may have more than 128 entries
  945. * We are safe to enable resizing, beause memblock_x86_fill()
  946. * is rather later for x86
  947. */
  948. memblock_allow_resize();
  949. for (i = 0; i < e820.nr_map; i++) {
  950. struct e820entry *ei = &e820.map[i];
  951. end = ei->addr + ei->size;
  952. if (end != (resource_size_t)end)
  953. continue;
  954. if (ei->type != E820_RAM && ei->type != E820_RESERVED_KERN)
  955. continue;
  956. memblock_add(ei->addr, ei->size);
  957. }
  958. /* throw away partial pages */
  959. memblock_trim_memory(PAGE_SIZE);
  960. memblock_dump_all();
  961. }
  962. void __init memblock_find_dma_reserve(void)
  963. {
  964. #ifdef CONFIG_X86_64
  965. u64 nr_pages = 0, nr_free_pages = 0;
  966. unsigned long start_pfn, end_pfn;
  967. phys_addr_t start, end;
  968. int i;
  969. u64 u;
  970. /*
  971. * need to find out used area below MAX_DMA_PFN
  972. * need to use memblock to get free size in [0, MAX_DMA_PFN]
  973. * at first, and assume boot_mem will not take below MAX_DMA_PFN
  974. */
  975. for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, NULL) {
  976. start_pfn = min(start_pfn, MAX_DMA_PFN);
  977. end_pfn = min(end_pfn, MAX_DMA_PFN);
  978. nr_pages += end_pfn - start_pfn;
  979. }
  980. for_each_free_mem_range(u, NUMA_NO_NODE, &start, &end, NULL) {
  981. start_pfn = min_t(unsigned long, PFN_UP(start), MAX_DMA_PFN);
  982. end_pfn = min_t(unsigned long, PFN_DOWN(end), MAX_DMA_PFN);
  983. if (start_pfn < end_pfn)
  984. nr_free_pages += end_pfn - start_pfn;
  985. }
  986. set_dma_reserve(nr_pages - nr_free_pages);
  987. #endif
  988. }