e820.c 30 KB

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