e820.c 28 KB

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