e820.c 28 KB

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