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