e820.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  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 size, u64 align)
  657. {
  658. u64 addr;
  659. addr = __memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
  660. if (addr) {
  661. e820_update_range_saved(addr, size, E820_RAM, E820_RESERVED);
  662. printk(KERN_INFO "update e820_saved for early_reserve_e820\n");
  663. update_e820_saved();
  664. }
  665. return addr;
  666. }
  667. #ifdef CONFIG_X86_32
  668. # ifdef CONFIG_X86_PAE
  669. # define MAX_ARCH_PFN (1ULL<<(36-PAGE_SHIFT))
  670. # else
  671. # define MAX_ARCH_PFN (1ULL<<(32-PAGE_SHIFT))
  672. # endif
  673. #else /* CONFIG_X86_32 */
  674. # define MAX_ARCH_PFN MAXMEM>>PAGE_SHIFT
  675. #endif
  676. /*
  677. * Find the highest page frame number we have available
  678. */
  679. static unsigned long __init e820_end_pfn(unsigned long limit_pfn, unsigned type)
  680. {
  681. int i;
  682. unsigned long last_pfn = 0;
  683. unsigned long max_arch_pfn = MAX_ARCH_PFN;
  684. for (i = 0; i < e820.nr_map; i++) {
  685. struct e820entry *ei = &e820.map[i];
  686. unsigned long start_pfn;
  687. unsigned long end_pfn;
  688. if (ei->type != type)
  689. continue;
  690. start_pfn = ei->addr >> PAGE_SHIFT;
  691. end_pfn = (ei->addr + ei->size) >> PAGE_SHIFT;
  692. if (start_pfn >= limit_pfn)
  693. continue;
  694. if (end_pfn > limit_pfn) {
  695. last_pfn = limit_pfn;
  696. break;
  697. }
  698. if (end_pfn > last_pfn)
  699. last_pfn = end_pfn;
  700. }
  701. if (last_pfn > max_arch_pfn)
  702. last_pfn = max_arch_pfn;
  703. printk(KERN_INFO "last_pfn = %#lx max_arch_pfn = %#lx\n",
  704. last_pfn, max_arch_pfn);
  705. return last_pfn;
  706. }
  707. unsigned long __init e820_end_of_ram_pfn(void)
  708. {
  709. return e820_end_pfn(MAX_ARCH_PFN, E820_RAM);
  710. }
  711. unsigned long __init e820_end_of_low_ram_pfn(void)
  712. {
  713. return e820_end_pfn(1UL<<(32 - PAGE_SHIFT), E820_RAM);
  714. }
  715. static void early_panic(char *msg)
  716. {
  717. early_printk(msg);
  718. panic(msg);
  719. }
  720. static int userdef __initdata;
  721. /* "mem=nopentium" disables the 4MB page tables. */
  722. static int __init parse_memopt(char *p)
  723. {
  724. u64 mem_size;
  725. if (!p)
  726. return -EINVAL;
  727. if (!strcmp(p, "nopentium")) {
  728. #ifdef CONFIG_X86_32
  729. setup_clear_cpu_cap(X86_FEATURE_PSE);
  730. return 0;
  731. #else
  732. printk(KERN_WARNING "mem=nopentium ignored! (only supported on x86_32)\n");
  733. return -EINVAL;
  734. #endif
  735. }
  736. userdef = 1;
  737. mem_size = memparse(p, &p);
  738. /* don't remove all of memory when handling "mem={invalid}" param */
  739. if (mem_size == 0)
  740. return -EINVAL;
  741. e820_remove_range(mem_size, ULLONG_MAX - mem_size, E820_RAM, 1);
  742. return 0;
  743. }
  744. early_param("mem", parse_memopt);
  745. static int __init parse_memmap_opt(char *p)
  746. {
  747. char *oldp;
  748. u64 start_at, mem_size;
  749. if (!p)
  750. return -EINVAL;
  751. if (!strncmp(p, "exactmap", 8)) {
  752. #ifdef CONFIG_CRASH_DUMP
  753. /*
  754. * If we are doing a crash dump, we still need to know
  755. * the real mem size before original memory map is
  756. * reset.
  757. */
  758. saved_max_pfn = e820_end_of_ram_pfn();
  759. #endif
  760. e820.nr_map = 0;
  761. userdef = 1;
  762. return 0;
  763. }
  764. oldp = p;
  765. mem_size = memparse(p, &p);
  766. if (p == oldp)
  767. return -EINVAL;
  768. userdef = 1;
  769. if (*p == '@') {
  770. start_at = memparse(p+1, &p);
  771. e820_add_region(start_at, mem_size, E820_RAM);
  772. } else if (*p == '#') {
  773. start_at = memparse(p+1, &p);
  774. e820_add_region(start_at, mem_size, E820_ACPI);
  775. } else if (*p == '$') {
  776. start_at = memparse(p+1, &p);
  777. e820_add_region(start_at, mem_size, E820_RESERVED);
  778. } else
  779. e820_remove_range(mem_size, ULLONG_MAX - mem_size, E820_RAM, 1);
  780. return *p == '\0' ? 0 : -EINVAL;
  781. }
  782. early_param("memmap", parse_memmap_opt);
  783. void __init finish_e820_parsing(void)
  784. {
  785. if (userdef) {
  786. u32 nr = e820.nr_map;
  787. if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr) < 0)
  788. early_panic("Invalid user supplied memory map");
  789. e820.nr_map = nr;
  790. printk(KERN_INFO "user-defined physical RAM map:\n");
  791. e820_print_map("user");
  792. }
  793. }
  794. static inline const char *e820_type_to_string(int e820_type)
  795. {
  796. switch (e820_type) {
  797. case E820_RESERVED_KERN:
  798. case E820_RAM: return "System RAM";
  799. case E820_ACPI: return "ACPI Tables";
  800. case E820_NVS: return "ACPI Non-volatile Storage";
  801. case E820_UNUSABLE: return "Unusable memory";
  802. default: return "reserved";
  803. }
  804. }
  805. /*
  806. * Mark e820 reserved areas as busy for the resource manager.
  807. */
  808. static struct resource __initdata *e820_res;
  809. void __init e820_reserve_resources(void)
  810. {
  811. int i;
  812. struct resource *res;
  813. u64 end;
  814. res = alloc_bootmem(sizeof(struct resource) * e820.nr_map);
  815. e820_res = res;
  816. for (i = 0; i < e820.nr_map; i++) {
  817. end = e820.map[i].addr + e820.map[i].size - 1;
  818. if (end != (resource_size_t)end) {
  819. res++;
  820. continue;
  821. }
  822. res->name = e820_type_to_string(e820.map[i].type);
  823. res->start = e820.map[i].addr;
  824. res->end = end;
  825. res->flags = IORESOURCE_MEM;
  826. /*
  827. * don't register the region that could be conflicted with
  828. * pci device BAR resource and insert them later in
  829. * pcibios_resource_survey()
  830. */
  831. if (e820.map[i].type != E820_RESERVED || res->start < (1ULL<<20)) {
  832. res->flags |= IORESOURCE_BUSY;
  833. insert_resource(&iomem_resource, res);
  834. }
  835. res++;
  836. }
  837. for (i = 0; i < e820_saved.nr_map; i++) {
  838. struct e820entry *entry = &e820_saved.map[i];
  839. firmware_map_add_early(entry->addr,
  840. entry->addr + entry->size - 1,
  841. e820_type_to_string(entry->type));
  842. }
  843. }
  844. /* How much should we pad RAM ending depending on where it is? */
  845. static unsigned long ram_alignment(resource_size_t pos)
  846. {
  847. unsigned long mb = pos >> 20;
  848. /* To 64kB in the first megabyte */
  849. if (!mb)
  850. return 64*1024;
  851. /* To 1MB in the first 16MB */
  852. if (mb < 16)
  853. return 1024*1024;
  854. /* To 64MB for anything above that */
  855. return 64*1024*1024;
  856. }
  857. #define MAX_RESOURCE_SIZE ((resource_size_t)-1)
  858. void __init e820_reserve_resources_late(void)
  859. {
  860. int i;
  861. struct resource *res;
  862. res = e820_res;
  863. for (i = 0; i < e820.nr_map; i++) {
  864. if (!res->parent && res->end)
  865. insert_resource_expand_to_fit(&iomem_resource, res);
  866. res++;
  867. }
  868. /*
  869. * Try to bump up RAM regions to reasonable boundaries to
  870. * avoid stolen RAM:
  871. */
  872. for (i = 0; i < e820.nr_map; i++) {
  873. struct e820entry *entry = &e820.map[i];
  874. u64 start, end;
  875. if (entry->type != E820_RAM)
  876. continue;
  877. start = entry->addr + entry->size;
  878. end = round_up(start, ram_alignment(start)) - 1;
  879. if (end > MAX_RESOURCE_SIZE)
  880. end = MAX_RESOURCE_SIZE;
  881. if (start >= end)
  882. continue;
  883. printk(KERN_DEBUG "reserve RAM buffer: %016llx - %016llx ",
  884. start, end);
  885. reserve_region_with_split(&iomem_resource, start, end,
  886. "RAM buffer");
  887. }
  888. }
  889. char *__init default_machine_specific_memory_setup(void)
  890. {
  891. char *who = "BIOS-e820";
  892. u32 new_nr;
  893. /*
  894. * Try to copy the BIOS-supplied E820-map.
  895. *
  896. * Otherwise fake a memory map; one section from 0k->640k,
  897. * the next section from 1mb->appropriate_mem_k
  898. */
  899. new_nr = boot_params.e820_entries;
  900. sanitize_e820_map(boot_params.e820_map,
  901. ARRAY_SIZE(boot_params.e820_map),
  902. &new_nr);
  903. boot_params.e820_entries = new_nr;
  904. if (append_e820_map(boot_params.e820_map, boot_params.e820_entries)
  905. < 0) {
  906. u64 mem_size;
  907. /* compare results from other methods and take the greater */
  908. if (boot_params.alt_mem_k
  909. < boot_params.screen_info.ext_mem_k) {
  910. mem_size = boot_params.screen_info.ext_mem_k;
  911. who = "BIOS-88";
  912. } else {
  913. mem_size = boot_params.alt_mem_k;
  914. who = "BIOS-e801";
  915. }
  916. e820.nr_map = 0;
  917. e820_add_region(0, LOWMEMSIZE(), E820_RAM);
  918. e820_add_region(HIGH_MEMORY, mem_size << 10, E820_RAM);
  919. }
  920. /* In case someone cares... */
  921. return who;
  922. }
  923. void __init setup_memory_map(void)
  924. {
  925. char *who;
  926. who = x86_init.resources.memory_setup();
  927. memcpy(&e820_saved, &e820, sizeof(struct e820map));
  928. printk(KERN_INFO "BIOS-provided physical RAM map:\n");
  929. e820_print_map(who);
  930. }
  931. void __init memblock_x86_fill(void)
  932. {
  933. int i;
  934. u64 end;
  935. /*
  936. * EFI may have more than 128 entries
  937. * We are safe to enable resizing, beause memblock_x86_fill()
  938. * is rather later for x86
  939. */
  940. memblock_allow_resize();
  941. for (i = 0; i < e820.nr_map; i++) {
  942. struct e820entry *ei = &e820.map[i];
  943. end = ei->addr + ei->size;
  944. if (end != (resource_size_t)end)
  945. continue;
  946. if (ei->type != E820_RAM && ei->type != E820_RESERVED_KERN)
  947. continue;
  948. memblock_add(ei->addr, ei->size);
  949. }
  950. memblock_dump_all();
  951. }
  952. void __init memblock_find_dma_reserve(void)
  953. {
  954. #ifdef CONFIG_X86_64
  955. u64 nr_pages = 0, nr_free_pages = 0;
  956. unsigned long start_pfn, end_pfn;
  957. phys_addr_t start, end;
  958. int i;
  959. u64 u;
  960. /*
  961. * need to find out used area below MAX_DMA_PFN
  962. * need to use memblock to get free size in [0, MAX_DMA_PFN]
  963. * at first, and assume boot_mem will not take below MAX_DMA_PFN
  964. */
  965. for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, NULL) {
  966. start_pfn = min_t(unsigned long, start_pfn, MAX_DMA_PFN);
  967. end_pfn = min_t(unsigned long, end_pfn, MAX_DMA_PFN);
  968. nr_pages += end_pfn - start_pfn;
  969. }
  970. for_each_free_mem_range(u, MAX_NUMNODES, &start, &end, NULL) {
  971. start_pfn = min_t(unsigned long, PFN_UP(start), MAX_DMA_PFN);
  972. end_pfn = min_t(unsigned long, PFN_DOWN(end), MAX_DMA_PFN);
  973. if (start_pfn < end_pfn)
  974. nr_free_pages += end_pfn - start_pfn;
  975. }
  976. set_dma_reserve(nr_pages - nr_free_pages);
  977. #endif
  978. }