e820.c 28 KB

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