e820.c 28 KB

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