e820.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449
  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/ioport.h>
  16. #include <linux/string.h>
  17. #include <linux/kexec.h>
  18. #include <linux/module.h>
  19. #include <linux/mm.h>
  20. #include <linux/pfn.h>
  21. #include <linux/suspend.h>
  22. #include <linux/firmware-map.h>
  23. #include <asm/pgtable.h>
  24. #include <asm/page.h>
  25. #include <asm/e820.h>
  26. #include <asm/proto.h>
  27. #include <asm/setup.h>
  28. #include <asm/trampoline.h>
  29. /*
  30. * The e820 map is the map that gets modified e.g. with command line parameters
  31. * and that is also registered with modifications in the kernel resource tree
  32. * with the iomem_resource as parent.
  33. *
  34. * The e820_saved is directly saved after the BIOS-provided memory map is
  35. * copied. It doesn't get modified afterwards. It's registered for the
  36. * /sys/firmware/memmap interface.
  37. *
  38. * That memory map is not modified and is used as base for kexec. The kexec'd
  39. * kernel should get the same memory map as the firmware provides. Then the
  40. * user can e.g. boot the original kernel with mem=1G while still booting the
  41. * next kernel with full memory.
  42. */
  43. struct e820map e820;
  44. struct e820map e820_saved;
  45. /* For PCI or other memory-mapped resources */
  46. unsigned long pci_mem_start = 0xaeedbabe;
  47. #ifdef CONFIG_PCI
  48. EXPORT_SYMBOL(pci_mem_start);
  49. #endif
  50. /*
  51. * This function checks if any part of the range <start,end> is mapped
  52. * with type.
  53. */
  54. int
  55. e820_any_mapped(u64 start, u64 end, unsigned type)
  56. {
  57. int i;
  58. for (i = 0; i < e820.nr_map; i++) {
  59. struct e820entry *ei = &e820.map[i];
  60. if (type && ei->type != type)
  61. continue;
  62. if (ei->addr >= end || ei->addr + ei->size <= start)
  63. continue;
  64. return 1;
  65. }
  66. return 0;
  67. }
  68. EXPORT_SYMBOL_GPL(e820_any_mapped);
  69. /*
  70. * This function checks if the entire range <start,end> is mapped with type.
  71. *
  72. * Note: this function only works correct if the e820 table is sorted and
  73. * not-overlapping, which is the case
  74. */
  75. int __init e820_all_mapped(u64 start, u64 end, unsigned type)
  76. {
  77. int i;
  78. for (i = 0; i < e820.nr_map; i++) {
  79. struct e820entry *ei = &e820.map[i];
  80. if (type && ei->type != type)
  81. continue;
  82. /* is the region (part) in overlap with the current region ?*/
  83. if (ei->addr >= end || ei->addr + ei->size <= start)
  84. continue;
  85. /* if the region is at the beginning of <start,end> we move
  86. * start to the end of the region since it's ok until there
  87. */
  88. if (ei->addr <= start)
  89. start = ei->addr + ei->size;
  90. /*
  91. * if start is now at or beyond end, we're done, full
  92. * coverage
  93. */
  94. if (start >= end)
  95. return 1;
  96. }
  97. return 0;
  98. }
  99. /*
  100. * Add a memory region to the kernel e820 map.
  101. */
  102. static void __init __e820_add_region(struct e820map *e820x, u64 start, u64 size,
  103. int type)
  104. {
  105. int x = e820x->nr_map;
  106. if (x == ARRAY_SIZE(e820x->map)) {
  107. printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
  108. return;
  109. }
  110. e820x->map[x].addr = start;
  111. e820x->map[x].size = size;
  112. e820x->map[x].type = type;
  113. e820x->nr_map++;
  114. }
  115. void __init e820_add_region(u64 start, u64 size, int type)
  116. {
  117. __e820_add_region(&e820, start, size, type);
  118. }
  119. static void __init e820_print_type(u32 type)
  120. {
  121. switch (type) {
  122. case E820_RAM:
  123. case E820_RESERVED_KERN:
  124. printk(KERN_CONT "(usable)");
  125. break;
  126. case E820_RESERVED:
  127. printk(KERN_CONT "(reserved)");
  128. break;
  129. case E820_ACPI:
  130. printk(KERN_CONT "(ACPI data)");
  131. break;
  132. case E820_NVS:
  133. printk(KERN_CONT "(ACPI NVS)");
  134. break;
  135. case E820_UNUSABLE:
  136. printk(KERN_CONT "(unusable)");
  137. break;
  138. default:
  139. printk(KERN_CONT "type %u", type);
  140. break;
  141. }
  142. }
  143. void __init e820_print_map(char *who)
  144. {
  145. int i;
  146. for (i = 0; i < e820.nr_map; i++) {
  147. printk(KERN_INFO " %s: %016Lx - %016Lx ", who,
  148. (unsigned long long) e820.map[i].addr,
  149. (unsigned long long)
  150. (e820.map[i].addr + e820.map[i].size));
  151. e820_print_type(e820.map[i].type);
  152. printk(KERN_CONT "\n");
  153. }
  154. }
  155. /*
  156. * Sanitize the BIOS e820 map.
  157. *
  158. * Some e820 responses include overlapping entries. The following
  159. * replaces the original e820 map with a new one, removing overlaps,
  160. * and resolving conflicting memory types in favor of highest
  161. * numbered type.
  162. *
  163. * The input parameter biosmap points to an array of 'struct
  164. * e820entry' which on entry has elements in the range [0, *pnr_map)
  165. * valid, and which has space for up to max_nr_map entries.
  166. * On return, the resulting sanitized e820 map entries will be in
  167. * overwritten in the same location, starting at biosmap.
  168. *
  169. * The integer pointed to by pnr_map must be valid on entry (the
  170. * current number of valid entries located at biosmap) and will
  171. * be updated on return, with the new number of valid entries
  172. * (something no more than max_nr_map.)
  173. *
  174. * The return value from sanitize_e820_map() is zero if it
  175. * successfully 'sanitized' the map entries passed in, and is -1
  176. * if it did nothing, which can happen if either of (1) it was
  177. * only passed one map entry, or (2) any of the input map entries
  178. * were invalid (start + size < start, meaning that the size was
  179. * so big the described memory range wrapped around through zero.)
  180. *
  181. * Visually we're performing the following
  182. * (1,2,3,4 = memory types)...
  183. *
  184. * Sample memory map (w/overlaps):
  185. * ____22__________________
  186. * ______________________4_
  187. * ____1111________________
  188. * _44_____________________
  189. * 11111111________________
  190. * ____________________33__
  191. * ___________44___________
  192. * __________33333_________
  193. * ______________22________
  194. * ___________________2222_
  195. * _________111111111______
  196. * _____________________11_
  197. * _________________4______
  198. *
  199. * Sanitized equivalent (no overlap):
  200. * 1_______________________
  201. * _44_____________________
  202. * ___1____________________
  203. * ____22__________________
  204. * ______11________________
  205. * _________1______________
  206. * __________3_____________
  207. * ___________44___________
  208. * _____________33_________
  209. * _______________2________
  210. * ________________1_______
  211. * _________________4______
  212. * ___________________2____
  213. * ____________________33__
  214. * ______________________4_
  215. */
  216. int __init sanitize_e820_map(struct e820entry *biosmap, int max_nr_map,
  217. u32 *pnr_map)
  218. {
  219. struct change_member {
  220. struct e820entry *pbios; /* pointer to original bios entry */
  221. unsigned long long addr; /* address for this change point */
  222. };
  223. static struct change_member change_point_list[2*E820_X_MAX] __initdata;
  224. static struct change_member *change_point[2*E820_X_MAX] __initdata;
  225. static struct e820entry *overlap_list[E820_X_MAX] __initdata;
  226. static struct e820entry new_bios[E820_X_MAX] __initdata;
  227. struct change_member *change_tmp;
  228. unsigned long current_type, last_type;
  229. unsigned long long last_addr;
  230. int chgidx, still_changing;
  231. int overlap_entries;
  232. int new_bios_entry;
  233. int old_nr, new_nr, chg_nr;
  234. int i;
  235. /* if there's only one memory region, don't bother */
  236. if (*pnr_map < 2)
  237. return -1;
  238. old_nr = *pnr_map;
  239. BUG_ON(old_nr > max_nr_map);
  240. /* bail out if we find any unreasonable addresses in bios map */
  241. for (i = 0; i < old_nr; i++)
  242. if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
  243. return -1;
  244. /* create pointers for initial change-point information (for sorting) */
  245. for (i = 0; i < 2 * old_nr; i++)
  246. change_point[i] = &change_point_list[i];
  247. /* record all known change-points (starting and ending addresses),
  248. omitting those that are for empty memory regions */
  249. chgidx = 0;
  250. for (i = 0; i < old_nr; i++) {
  251. if (biosmap[i].size != 0) {
  252. change_point[chgidx]->addr = biosmap[i].addr;
  253. change_point[chgidx++]->pbios = &biosmap[i];
  254. change_point[chgidx]->addr = biosmap[i].addr +
  255. biosmap[i].size;
  256. change_point[chgidx++]->pbios = &biosmap[i];
  257. }
  258. }
  259. chg_nr = chgidx;
  260. /* sort change-point list by memory addresses (low -> high) */
  261. still_changing = 1;
  262. while (still_changing) {
  263. still_changing = 0;
  264. for (i = 1; i < chg_nr; i++) {
  265. unsigned long long curaddr, lastaddr;
  266. unsigned long long curpbaddr, lastpbaddr;
  267. curaddr = change_point[i]->addr;
  268. lastaddr = change_point[i - 1]->addr;
  269. curpbaddr = change_point[i]->pbios->addr;
  270. lastpbaddr = change_point[i - 1]->pbios->addr;
  271. /*
  272. * swap entries, when:
  273. *
  274. * curaddr > lastaddr or
  275. * curaddr == lastaddr and curaddr == curpbaddr and
  276. * lastaddr != lastpbaddr
  277. */
  278. if (curaddr < lastaddr ||
  279. (curaddr == lastaddr && curaddr == curpbaddr &&
  280. lastaddr != lastpbaddr)) {
  281. change_tmp = change_point[i];
  282. change_point[i] = change_point[i-1];
  283. change_point[i-1] = change_tmp;
  284. still_changing = 1;
  285. }
  286. }
  287. }
  288. /* create a new bios memory map, removing overlaps */
  289. overlap_entries = 0; /* number of entries in the overlap table */
  290. new_bios_entry = 0; /* index for creating new bios map entries */
  291. last_type = 0; /* start with undefined memory type */
  292. last_addr = 0; /* start with 0 as last starting address */
  293. /* loop through change-points, determining affect on the new bios map */
  294. for (chgidx = 0; chgidx < chg_nr; chgidx++) {
  295. /* keep track of all overlapping bios entries */
  296. if (change_point[chgidx]->addr ==
  297. change_point[chgidx]->pbios->addr) {
  298. /*
  299. * add map entry to overlap list (> 1 entry
  300. * implies an overlap)
  301. */
  302. overlap_list[overlap_entries++] =
  303. change_point[chgidx]->pbios;
  304. } else {
  305. /*
  306. * remove entry from list (order independent,
  307. * so swap with last)
  308. */
  309. for (i = 0; i < overlap_entries; i++) {
  310. if (overlap_list[i] ==
  311. change_point[chgidx]->pbios)
  312. overlap_list[i] =
  313. overlap_list[overlap_entries-1];
  314. }
  315. overlap_entries--;
  316. }
  317. /*
  318. * if there are overlapping entries, decide which
  319. * "type" to use (larger value takes precedence --
  320. * 1=usable, 2,3,4,4+=unusable)
  321. */
  322. current_type = 0;
  323. for (i = 0; i < overlap_entries; i++)
  324. if (overlap_list[i]->type > current_type)
  325. current_type = overlap_list[i]->type;
  326. /*
  327. * continue building up new bios map based on this
  328. * information
  329. */
  330. if (current_type != last_type) {
  331. if (last_type != 0) {
  332. new_bios[new_bios_entry].size =
  333. change_point[chgidx]->addr - last_addr;
  334. /*
  335. * move forward only if the new size
  336. * was non-zero
  337. */
  338. if (new_bios[new_bios_entry].size != 0)
  339. /*
  340. * no more space left for new
  341. * bios entries ?
  342. */
  343. if (++new_bios_entry >= max_nr_map)
  344. break;
  345. }
  346. if (current_type != 0) {
  347. new_bios[new_bios_entry].addr =
  348. change_point[chgidx]->addr;
  349. new_bios[new_bios_entry].type = current_type;
  350. last_addr = change_point[chgidx]->addr;
  351. }
  352. last_type = current_type;
  353. }
  354. }
  355. /* retain count for new bios entries */
  356. new_nr = new_bios_entry;
  357. /* copy new bios mapping into original location */
  358. memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
  359. *pnr_map = new_nr;
  360. return 0;
  361. }
  362. static int __init __append_e820_map(struct e820entry *biosmap, int nr_map)
  363. {
  364. while (nr_map) {
  365. u64 start = biosmap->addr;
  366. u64 size = biosmap->size;
  367. u64 end = start + size;
  368. u32 type = biosmap->type;
  369. /* Overflow in 64 bits? Ignore the memory map. */
  370. if (start > end)
  371. return -1;
  372. e820_add_region(start, size, type);
  373. biosmap++;
  374. nr_map--;
  375. }
  376. return 0;
  377. }
  378. /*
  379. * Copy the BIOS e820 map into a safe place.
  380. *
  381. * Sanity-check it while we're at it..
  382. *
  383. * If we're lucky and live on a modern system, the setup code
  384. * will have given us a memory map that we can use to properly
  385. * set up memory. If we aren't, we'll fake a memory map.
  386. */
  387. static int __init append_e820_map(struct e820entry *biosmap, int nr_map)
  388. {
  389. /* Only one memory region (or negative)? Ignore it */
  390. if (nr_map < 2)
  391. return -1;
  392. return __append_e820_map(biosmap, nr_map);
  393. }
  394. static u64 __init __e820_update_range(struct e820map *e820x, u64 start,
  395. u64 size, unsigned old_type,
  396. unsigned new_type)
  397. {
  398. u64 end;
  399. unsigned int i;
  400. u64 real_updated_size = 0;
  401. BUG_ON(old_type == new_type);
  402. if (size > (ULLONG_MAX - start))
  403. size = ULLONG_MAX - start;
  404. end = start + size;
  405. printk(KERN_DEBUG "e820 update range: %016Lx - %016Lx ",
  406. (unsigned long long) start,
  407. (unsigned long long) end);
  408. e820_print_type(old_type);
  409. printk(KERN_CONT " ==> ");
  410. e820_print_type(new_type);
  411. printk(KERN_CONT "\n");
  412. for (i = 0; i < e820x->nr_map; i++) {
  413. struct e820entry *ei = &e820x->map[i];
  414. u64 final_start, final_end;
  415. u64 ei_end;
  416. if (ei->type != old_type)
  417. continue;
  418. ei_end = ei->addr + ei->size;
  419. /* totally covered by new range? */
  420. if (ei->addr >= start && ei_end <= end) {
  421. ei->type = new_type;
  422. real_updated_size += ei->size;
  423. continue;
  424. }
  425. /* new range is totally covered? */
  426. if (ei->addr < start && ei_end > end) {
  427. __e820_add_region(e820x, start, size, new_type);
  428. __e820_add_region(e820x, end, ei_end - end, ei->type);
  429. ei->size = start - ei->addr;
  430. real_updated_size += size;
  431. continue;
  432. }
  433. /* partially covered */
  434. final_start = max(start, ei->addr);
  435. final_end = min(end, ei_end);
  436. if (final_start >= final_end)
  437. continue;
  438. __e820_add_region(e820x, final_start, final_end - final_start,
  439. new_type);
  440. real_updated_size += final_end - final_start;
  441. /*
  442. * left range could be head or tail, so need to update
  443. * size at first.
  444. */
  445. ei->size -= final_end - final_start;
  446. if (ei->addr < final_start)
  447. continue;
  448. ei->addr = final_end;
  449. }
  450. return real_updated_size;
  451. }
  452. u64 __init e820_update_range(u64 start, u64 size, unsigned old_type,
  453. unsigned new_type)
  454. {
  455. return __e820_update_range(&e820, start, size, old_type, new_type);
  456. }
  457. static u64 __init e820_update_range_saved(u64 start, u64 size,
  458. unsigned old_type, unsigned new_type)
  459. {
  460. return __e820_update_range(&e820_saved, start, size, old_type,
  461. new_type);
  462. }
  463. /* make e820 not cover the range */
  464. u64 __init e820_remove_range(u64 start, u64 size, unsigned old_type,
  465. int checktype)
  466. {
  467. int i;
  468. u64 real_removed_size = 0;
  469. if (size > (ULLONG_MAX - start))
  470. size = ULLONG_MAX - start;
  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, round;
  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 "PCI: Warning: Cannot find a gap in the 32bit "
  564. "address range\n"
  565. KERN_ERR "PCI: Unassigned devices with 32bit resource "
  566. "registers may break!\n");
  567. }
  568. #endif
  569. /*
  570. * See how much we want to round up: start off with
  571. * rounding to the next 1MB area.
  572. */
  573. round = 0x100000;
  574. while ((gapsize >> 4) > round)
  575. round += round;
  576. /* Fun with two's complement */
  577. pci_mem_start = (gapstart + round) & -round;
  578. printk(KERN_INFO
  579. "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
  580. pci_mem_start, gapstart, gapsize);
  581. }
  582. /**
  583. * Because of the size limitation of struct boot_params, only first
  584. * 128 E820 memory entries are passed to kernel via
  585. * boot_params.e820_map, others are passed via SETUP_E820_EXT node of
  586. * linked list of struct setup_data, which is parsed here.
  587. */
  588. void __init parse_e820_ext(struct setup_data *sdata, unsigned long pa_data)
  589. {
  590. u32 map_len;
  591. int entries;
  592. struct e820entry *extmap;
  593. entries = sdata->len / sizeof(struct e820entry);
  594. map_len = sdata->len + sizeof(struct setup_data);
  595. if (map_len > PAGE_SIZE)
  596. sdata = early_ioremap(pa_data, map_len);
  597. extmap = (struct e820entry *)(sdata->data);
  598. __append_e820_map(extmap, entries);
  599. sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
  600. if (map_len > PAGE_SIZE)
  601. early_iounmap(sdata, map_len);
  602. printk(KERN_INFO "extended physical RAM map:\n");
  603. e820_print_map("extended");
  604. }
  605. #if defined(CONFIG_X86_64) || \
  606. (defined(CONFIG_X86_32) && defined(CONFIG_HIBERNATION))
  607. /**
  608. * Find the ranges of physical addresses that do not correspond to
  609. * e820 RAM areas and mark the corresponding pages as nosave for
  610. * hibernation (32 bit) or software suspend and suspend to RAM (64 bit).
  611. *
  612. * This function requires the e820 map to be sorted and without any
  613. * overlapping entries and assumes the first e820 area to be RAM.
  614. */
  615. void __init e820_mark_nosave_regions(unsigned long limit_pfn)
  616. {
  617. int i;
  618. unsigned long pfn;
  619. pfn = PFN_DOWN(e820.map[0].addr + e820.map[0].size);
  620. for (i = 1; i < e820.nr_map; i++) {
  621. struct e820entry *ei = &e820.map[i];
  622. if (pfn < PFN_UP(ei->addr))
  623. register_nosave_region(pfn, PFN_UP(ei->addr));
  624. pfn = PFN_DOWN(ei->addr + ei->size);
  625. if (ei->type != E820_RAM && ei->type != E820_RESERVED_KERN)
  626. register_nosave_region(PFN_UP(ei->addr), pfn);
  627. if (pfn >= limit_pfn)
  628. break;
  629. }
  630. }
  631. #endif
  632. #ifdef CONFIG_HIBERNATION
  633. /**
  634. * Mark ACPI NVS memory region, so that we can save/restore it during
  635. * hibernation and the subsequent resume.
  636. */
  637. static int __init e820_mark_nvs_memory(void)
  638. {
  639. int i;
  640. for (i = 0; i < e820.nr_map; i++) {
  641. struct e820entry *ei = &e820.map[i];
  642. if (ei->type == E820_NVS)
  643. hibernate_nvs_register(ei->addr, ei->size);
  644. }
  645. return 0;
  646. }
  647. core_initcall(e820_mark_nvs_memory);
  648. #endif
  649. /*
  650. * Early reserved memory areas.
  651. */
  652. #define MAX_EARLY_RES 20
  653. struct early_res {
  654. u64 start, end;
  655. char name[16];
  656. char overlap_ok;
  657. };
  658. static struct early_res early_res[MAX_EARLY_RES] __initdata = {
  659. { 0, PAGE_SIZE, "BIOS data page" }, /* BIOS data page */
  660. {}
  661. };
  662. static int __init find_overlapped_early(u64 start, u64 end)
  663. {
  664. int i;
  665. struct early_res *r;
  666. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
  667. r = &early_res[i];
  668. if (end > r->start && start < r->end)
  669. break;
  670. }
  671. return i;
  672. }
  673. /*
  674. * Drop the i-th range from the early reservation map,
  675. * by copying any higher ranges down one over it, and
  676. * clearing what had been the last slot.
  677. */
  678. static void __init drop_range(int i)
  679. {
  680. int j;
  681. for (j = i + 1; j < MAX_EARLY_RES && early_res[j].end; j++)
  682. ;
  683. memmove(&early_res[i], &early_res[i + 1],
  684. (j - 1 - i) * sizeof(struct early_res));
  685. early_res[j - 1].end = 0;
  686. }
  687. /*
  688. * Split any existing ranges that:
  689. * 1) are marked 'overlap_ok', and
  690. * 2) overlap with the stated range [start, end)
  691. * into whatever portion (if any) of the existing range is entirely
  692. * below or entirely above the stated range. Drop the portion
  693. * of the existing range that overlaps with the stated range,
  694. * which will allow the caller of this routine to then add that
  695. * stated range without conflicting with any existing range.
  696. */
  697. static void __init drop_overlaps_that_are_ok(u64 start, u64 end)
  698. {
  699. int i;
  700. struct early_res *r;
  701. u64 lower_start, lower_end;
  702. u64 upper_start, upper_end;
  703. char name[16];
  704. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
  705. r = &early_res[i];
  706. /* Continue past non-overlapping ranges */
  707. if (end <= r->start || start >= r->end)
  708. continue;
  709. /*
  710. * Leave non-ok overlaps as is; let caller
  711. * panic "Overlapping early reservations"
  712. * when it hits this overlap.
  713. */
  714. if (!r->overlap_ok)
  715. return;
  716. /*
  717. * We have an ok overlap. We will drop it from the early
  718. * reservation map, and add back in any non-overlapping
  719. * portions (lower or upper) as separate, overlap_ok,
  720. * non-overlapping ranges.
  721. */
  722. /* 1. Note any non-overlapping (lower or upper) ranges. */
  723. strncpy(name, r->name, sizeof(name) - 1);
  724. lower_start = lower_end = 0;
  725. upper_start = upper_end = 0;
  726. if (r->start < start) {
  727. lower_start = r->start;
  728. lower_end = start;
  729. }
  730. if (r->end > end) {
  731. upper_start = end;
  732. upper_end = r->end;
  733. }
  734. /* 2. Drop the original ok overlapping range */
  735. drop_range(i);
  736. i--; /* resume for-loop on copied down entry */
  737. /* 3. Add back in any non-overlapping ranges. */
  738. if (lower_end)
  739. reserve_early_overlap_ok(lower_start, lower_end, name);
  740. if (upper_end)
  741. reserve_early_overlap_ok(upper_start, upper_end, name);
  742. }
  743. }
  744. static void __init __reserve_early(u64 start, u64 end, char *name,
  745. int overlap_ok)
  746. {
  747. int i;
  748. struct early_res *r;
  749. i = find_overlapped_early(start, end);
  750. if (i >= MAX_EARLY_RES)
  751. panic("Too many early reservations");
  752. r = &early_res[i];
  753. if (r->end)
  754. panic("Overlapping early reservations "
  755. "%llx-%llx %s to %llx-%llx %s\n",
  756. start, end - 1, name?name:"", r->start,
  757. r->end - 1, r->name);
  758. r->start = start;
  759. r->end = end;
  760. r->overlap_ok = overlap_ok;
  761. if (name)
  762. strncpy(r->name, name, sizeof(r->name) - 1);
  763. }
  764. /*
  765. * A few early reservtations come here.
  766. *
  767. * The 'overlap_ok' in the name of this routine does -not- mean it
  768. * is ok for these reservations to overlap an earlier reservation.
  769. * Rather it means that it is ok for subsequent reservations to
  770. * overlap this one.
  771. *
  772. * Use this entry point to reserve early ranges when you are doing
  773. * so out of "Paranoia", reserving perhaps more memory than you need,
  774. * just in case, and don't mind a subsequent overlapping reservation
  775. * that is known to be needed.
  776. *
  777. * The drop_overlaps_that_are_ok() call here isn't really needed.
  778. * It would be needed if we had two colliding 'overlap_ok'
  779. * reservations, so that the second such would not panic on the
  780. * overlap with the first. We don't have any such as of this
  781. * writing, but might as well tolerate such if it happens in
  782. * the future.
  783. */
  784. void __init reserve_early_overlap_ok(u64 start, u64 end, char *name)
  785. {
  786. drop_overlaps_that_are_ok(start, end);
  787. __reserve_early(start, end, name, 1);
  788. }
  789. /*
  790. * Most early reservations come here.
  791. *
  792. * We first have drop_overlaps_that_are_ok() drop any pre-existing
  793. * 'overlap_ok' ranges, so that we can then reserve this memory
  794. * range without risk of panic'ing on an overlapping overlap_ok
  795. * early reservation.
  796. */
  797. void __init reserve_early(u64 start, u64 end, char *name)
  798. {
  799. if (start >= end)
  800. return;
  801. drop_overlaps_that_are_ok(start, end);
  802. __reserve_early(start, end, name, 0);
  803. }
  804. void __init free_early(u64 start, u64 end)
  805. {
  806. struct early_res *r;
  807. int i;
  808. i = find_overlapped_early(start, end);
  809. r = &early_res[i];
  810. if (i >= MAX_EARLY_RES || r->end != end || r->start != start)
  811. panic("free_early on not reserved area: %llx-%llx!",
  812. start, end - 1);
  813. drop_range(i);
  814. }
  815. void __init early_res_to_bootmem(u64 start, u64 end)
  816. {
  817. int i, count;
  818. u64 final_start, final_end;
  819. count = 0;
  820. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++)
  821. count++;
  822. printk(KERN_INFO "(%d early reservations) ==> bootmem [%010llx - %010llx]\n",
  823. count, start, end);
  824. for (i = 0; i < count; i++) {
  825. struct early_res *r = &early_res[i];
  826. printk(KERN_INFO " #%d [%010llx - %010llx] %16s", i,
  827. r->start, r->end, r->name);
  828. final_start = max(start, r->start);
  829. final_end = min(end, r->end);
  830. if (final_start >= final_end) {
  831. printk(KERN_CONT "\n");
  832. continue;
  833. }
  834. printk(KERN_CONT " ==> [%010llx - %010llx]\n",
  835. final_start, final_end);
  836. reserve_bootmem_generic(final_start, final_end - final_start,
  837. BOOTMEM_DEFAULT);
  838. }
  839. }
  840. /* Check for already reserved areas */
  841. static inline int __init bad_addr(u64 *addrp, u64 size, u64 align)
  842. {
  843. int i;
  844. u64 addr = *addrp;
  845. int changed = 0;
  846. struct early_res *r;
  847. again:
  848. i = find_overlapped_early(addr, addr + size);
  849. r = &early_res[i];
  850. if (i < MAX_EARLY_RES && r->end) {
  851. *addrp = addr = round_up(r->end, align);
  852. changed = 1;
  853. goto again;
  854. }
  855. return changed;
  856. }
  857. /* Check for already reserved areas */
  858. static inline int __init bad_addr_size(u64 *addrp, u64 *sizep, u64 align)
  859. {
  860. int i;
  861. u64 addr = *addrp, last;
  862. u64 size = *sizep;
  863. int changed = 0;
  864. again:
  865. last = addr + size;
  866. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
  867. struct early_res *r = &early_res[i];
  868. if (last > r->start && addr < r->start) {
  869. size = r->start - addr;
  870. changed = 1;
  871. goto again;
  872. }
  873. if (last > r->end && addr < r->end) {
  874. addr = round_up(r->end, align);
  875. size = last - addr;
  876. changed = 1;
  877. goto again;
  878. }
  879. if (last <= r->end && addr >= r->start) {
  880. (*sizep)++;
  881. return 0;
  882. }
  883. }
  884. if (changed) {
  885. *addrp = addr;
  886. *sizep = size;
  887. }
  888. return changed;
  889. }
  890. /*
  891. * Find a free area with specified alignment in a specific range.
  892. */
  893. u64 __init find_e820_area(u64 start, u64 end, u64 size, u64 align)
  894. {
  895. int i;
  896. for (i = 0; i < e820.nr_map; i++) {
  897. struct e820entry *ei = &e820.map[i];
  898. u64 addr, last;
  899. u64 ei_last;
  900. if (ei->type != E820_RAM)
  901. continue;
  902. addr = round_up(ei->addr, align);
  903. ei_last = ei->addr + ei->size;
  904. if (addr < start)
  905. addr = round_up(start, align);
  906. if (addr >= ei_last)
  907. continue;
  908. while (bad_addr(&addr, size, align) && addr+size <= ei_last)
  909. ;
  910. last = addr + size;
  911. if (last > ei_last)
  912. continue;
  913. if (last > end)
  914. continue;
  915. return addr;
  916. }
  917. return -1ULL;
  918. }
  919. /*
  920. * Find next free range after *start
  921. */
  922. u64 __init find_e820_area_size(u64 start, u64 *sizep, u64 align)
  923. {
  924. int i;
  925. for (i = 0; i < e820.nr_map; i++) {
  926. struct e820entry *ei = &e820.map[i];
  927. u64 addr, last;
  928. u64 ei_last;
  929. if (ei->type != E820_RAM)
  930. continue;
  931. addr = round_up(ei->addr, align);
  932. ei_last = ei->addr + ei->size;
  933. if (addr < start)
  934. addr = round_up(start, align);
  935. if (addr >= ei_last)
  936. continue;
  937. *sizep = ei_last - addr;
  938. while (bad_addr_size(&addr, sizep, align) &&
  939. addr + *sizep <= ei_last)
  940. ;
  941. last = addr + *sizep;
  942. if (last > ei_last)
  943. continue;
  944. return addr;
  945. }
  946. return -1ULL;
  947. }
  948. /*
  949. * pre allocated 4k and reserved it in e820
  950. */
  951. u64 __init early_reserve_e820(u64 startt, u64 sizet, u64 align)
  952. {
  953. u64 size = 0;
  954. u64 addr;
  955. u64 start;
  956. start = startt;
  957. while (size < sizet && (start + 1))
  958. start = find_e820_area_size(start, &size, align);
  959. if (size < sizet)
  960. return 0;
  961. #ifdef CONFIG_X86_32
  962. if (start >= MAXMEM)
  963. return 0;
  964. if (start + size > MAXMEM)
  965. size = MAXMEM - start;
  966. #endif
  967. addr = round_down(start + size - sizet, align);
  968. if (addr < start)
  969. return 0;
  970. e820_update_range(addr, sizet, E820_RAM, E820_RESERVED);
  971. e820_update_range_saved(addr, sizet, E820_RAM, E820_RESERVED);
  972. printk(KERN_INFO "update e820 for early_reserve_e820\n");
  973. update_e820();
  974. update_e820_saved();
  975. return addr;
  976. }
  977. #ifdef CONFIG_X86_32
  978. # ifdef CONFIG_X86_PAE
  979. # define MAX_ARCH_PFN (1ULL<<(36-PAGE_SHIFT))
  980. # else
  981. # define MAX_ARCH_PFN (1ULL<<(32-PAGE_SHIFT))
  982. # endif
  983. #else /* CONFIG_X86_32 */
  984. # define MAX_ARCH_PFN MAXMEM>>PAGE_SHIFT
  985. #endif
  986. /*
  987. * Find the highest page frame number we have available
  988. */
  989. static unsigned long __init e820_end_pfn(unsigned long limit_pfn, unsigned type)
  990. {
  991. int i;
  992. unsigned long last_pfn = 0;
  993. unsigned long max_arch_pfn = MAX_ARCH_PFN;
  994. for (i = 0; i < e820.nr_map; i++) {
  995. struct e820entry *ei = &e820.map[i];
  996. unsigned long start_pfn;
  997. unsigned long end_pfn;
  998. if (ei->type != type)
  999. continue;
  1000. start_pfn = ei->addr >> PAGE_SHIFT;
  1001. end_pfn = (ei->addr + ei->size) >> PAGE_SHIFT;
  1002. if (start_pfn >= limit_pfn)
  1003. continue;
  1004. if (end_pfn > limit_pfn) {
  1005. last_pfn = limit_pfn;
  1006. break;
  1007. }
  1008. if (end_pfn > last_pfn)
  1009. last_pfn = end_pfn;
  1010. }
  1011. if (last_pfn > max_arch_pfn)
  1012. last_pfn = max_arch_pfn;
  1013. printk(KERN_INFO "last_pfn = %#lx max_arch_pfn = %#lx\n",
  1014. last_pfn, max_arch_pfn);
  1015. return last_pfn;
  1016. }
  1017. unsigned long __init e820_end_of_ram_pfn(void)
  1018. {
  1019. return e820_end_pfn(MAX_ARCH_PFN, E820_RAM);
  1020. }
  1021. unsigned long __init e820_end_of_low_ram_pfn(void)
  1022. {
  1023. return e820_end_pfn(1UL<<(32 - PAGE_SHIFT), E820_RAM);
  1024. }
  1025. /*
  1026. * Finds an active region in the address range from start_pfn to last_pfn and
  1027. * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
  1028. */
  1029. int __init e820_find_active_region(const struct e820entry *ei,
  1030. unsigned long start_pfn,
  1031. unsigned long last_pfn,
  1032. unsigned long *ei_startpfn,
  1033. unsigned long *ei_endpfn)
  1034. {
  1035. u64 align = PAGE_SIZE;
  1036. *ei_startpfn = round_up(ei->addr, align) >> PAGE_SHIFT;
  1037. *ei_endpfn = round_down(ei->addr + ei->size, align) >> PAGE_SHIFT;
  1038. /* Skip map entries smaller than a page */
  1039. if (*ei_startpfn >= *ei_endpfn)
  1040. return 0;
  1041. /* Skip if map is outside the node */
  1042. if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
  1043. *ei_startpfn >= last_pfn)
  1044. return 0;
  1045. /* Check for overlaps */
  1046. if (*ei_startpfn < start_pfn)
  1047. *ei_startpfn = start_pfn;
  1048. if (*ei_endpfn > last_pfn)
  1049. *ei_endpfn = last_pfn;
  1050. return 1;
  1051. }
  1052. /* Walk the e820 map and register active regions within a node */
  1053. void __init e820_register_active_regions(int nid, unsigned long start_pfn,
  1054. unsigned long last_pfn)
  1055. {
  1056. unsigned long ei_startpfn;
  1057. unsigned long ei_endpfn;
  1058. int i;
  1059. for (i = 0; i < e820.nr_map; i++)
  1060. if (e820_find_active_region(&e820.map[i],
  1061. start_pfn, last_pfn,
  1062. &ei_startpfn, &ei_endpfn))
  1063. add_active_range(nid, ei_startpfn, ei_endpfn);
  1064. }
  1065. /*
  1066. * Find the hole size (in bytes) in the memory range.
  1067. * @start: starting address of the memory range to scan
  1068. * @end: ending address of the memory range to scan
  1069. */
  1070. u64 __init e820_hole_size(u64 start, u64 end)
  1071. {
  1072. unsigned long start_pfn = start >> PAGE_SHIFT;
  1073. unsigned long last_pfn = end >> PAGE_SHIFT;
  1074. unsigned long ei_startpfn, ei_endpfn, ram = 0;
  1075. int i;
  1076. for (i = 0; i < e820.nr_map; i++) {
  1077. if (e820_find_active_region(&e820.map[i],
  1078. start_pfn, last_pfn,
  1079. &ei_startpfn, &ei_endpfn))
  1080. ram += ei_endpfn - ei_startpfn;
  1081. }
  1082. return end - start - ((u64)ram << PAGE_SHIFT);
  1083. }
  1084. static void early_panic(char *msg)
  1085. {
  1086. early_printk(msg);
  1087. panic(msg);
  1088. }
  1089. static int userdef __initdata;
  1090. /* "mem=nopentium" disables the 4MB page tables. */
  1091. static int __init parse_memopt(char *p)
  1092. {
  1093. u64 mem_size;
  1094. if (!p)
  1095. return -EINVAL;
  1096. #ifdef CONFIG_X86_32
  1097. if (!strcmp(p, "nopentium")) {
  1098. setup_clear_cpu_cap(X86_FEATURE_PSE);
  1099. return 0;
  1100. }
  1101. #endif
  1102. userdef = 1;
  1103. mem_size = memparse(p, &p);
  1104. e820_remove_range(mem_size, ULLONG_MAX - mem_size, E820_RAM, 1);
  1105. return 0;
  1106. }
  1107. early_param("mem", parse_memopt);
  1108. static int __init parse_memmap_opt(char *p)
  1109. {
  1110. char *oldp;
  1111. u64 start_at, mem_size;
  1112. if (!p)
  1113. return -EINVAL;
  1114. if (!strncmp(p, "exactmap", 8)) {
  1115. #ifdef CONFIG_CRASH_DUMP
  1116. /*
  1117. * If we are doing a crash dump, we still need to know
  1118. * the real mem size before original memory map is
  1119. * reset.
  1120. */
  1121. saved_max_pfn = e820_end_of_ram_pfn();
  1122. #endif
  1123. e820.nr_map = 0;
  1124. userdef = 1;
  1125. return 0;
  1126. }
  1127. oldp = p;
  1128. mem_size = memparse(p, &p);
  1129. if (p == oldp)
  1130. return -EINVAL;
  1131. userdef = 1;
  1132. if (*p == '@') {
  1133. start_at = memparse(p+1, &p);
  1134. e820_add_region(start_at, mem_size, E820_RAM);
  1135. } else if (*p == '#') {
  1136. start_at = memparse(p+1, &p);
  1137. e820_add_region(start_at, mem_size, E820_ACPI);
  1138. } else if (*p == '$') {
  1139. start_at = memparse(p+1, &p);
  1140. e820_add_region(start_at, mem_size, E820_RESERVED);
  1141. } else
  1142. e820_remove_range(mem_size, ULLONG_MAX - mem_size, E820_RAM, 1);
  1143. return *p == '\0' ? 0 : -EINVAL;
  1144. }
  1145. early_param("memmap", parse_memmap_opt);
  1146. void __init finish_e820_parsing(void)
  1147. {
  1148. if (userdef) {
  1149. u32 nr = e820.nr_map;
  1150. if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr) < 0)
  1151. early_panic("Invalid user supplied memory map");
  1152. e820.nr_map = nr;
  1153. printk(KERN_INFO "user-defined physical RAM map:\n");
  1154. e820_print_map("user");
  1155. }
  1156. }
  1157. static inline const char *e820_type_to_string(int e820_type)
  1158. {
  1159. switch (e820_type) {
  1160. case E820_RESERVED_KERN:
  1161. case E820_RAM: return "System RAM";
  1162. case E820_ACPI: return "ACPI Tables";
  1163. case E820_NVS: return "ACPI Non-volatile Storage";
  1164. case E820_UNUSABLE: return "Unusable memory";
  1165. default: return "reserved";
  1166. }
  1167. }
  1168. /*
  1169. * Mark e820 reserved areas as busy for the resource manager.
  1170. */
  1171. static struct resource __initdata *e820_res;
  1172. void __init e820_reserve_resources(void)
  1173. {
  1174. int i;
  1175. struct resource *res;
  1176. u64 end;
  1177. res = alloc_bootmem_low(sizeof(struct resource) * e820.nr_map);
  1178. e820_res = res;
  1179. for (i = 0; i < e820.nr_map; i++) {
  1180. end = e820.map[i].addr + e820.map[i].size - 1;
  1181. if (end != (resource_size_t)end) {
  1182. res++;
  1183. continue;
  1184. }
  1185. res->name = e820_type_to_string(e820.map[i].type);
  1186. res->start = e820.map[i].addr;
  1187. res->end = end;
  1188. res->flags = IORESOURCE_MEM;
  1189. /*
  1190. * don't register the region that could be conflicted with
  1191. * pci device BAR resource and insert them later in
  1192. * pcibios_resource_survey()
  1193. */
  1194. if (e820.map[i].type != E820_RESERVED || res->start < (1ULL<<20)) {
  1195. res->flags |= IORESOURCE_BUSY;
  1196. insert_resource(&iomem_resource, res);
  1197. }
  1198. res++;
  1199. }
  1200. for (i = 0; i < e820_saved.nr_map; i++) {
  1201. struct e820entry *entry = &e820_saved.map[i];
  1202. firmware_map_add_early(entry->addr,
  1203. entry->addr + entry->size - 1,
  1204. e820_type_to_string(entry->type));
  1205. }
  1206. }
  1207. void __init e820_reserve_resources_late(void)
  1208. {
  1209. int i;
  1210. struct resource *res;
  1211. res = e820_res;
  1212. for (i = 0; i < e820.nr_map; i++) {
  1213. if (!res->parent && res->end)
  1214. insert_resource_expand_to_fit(&iomem_resource, res);
  1215. res++;
  1216. }
  1217. }
  1218. char *__init default_machine_specific_memory_setup(void)
  1219. {
  1220. char *who = "BIOS-e820";
  1221. u32 new_nr;
  1222. /*
  1223. * Try to copy the BIOS-supplied E820-map.
  1224. *
  1225. * Otherwise fake a memory map; one section from 0k->640k,
  1226. * the next section from 1mb->appropriate_mem_k
  1227. */
  1228. new_nr = boot_params.e820_entries;
  1229. sanitize_e820_map(boot_params.e820_map,
  1230. ARRAY_SIZE(boot_params.e820_map),
  1231. &new_nr);
  1232. boot_params.e820_entries = new_nr;
  1233. if (append_e820_map(boot_params.e820_map, boot_params.e820_entries)
  1234. < 0) {
  1235. u64 mem_size;
  1236. /* compare results from other methods and take the greater */
  1237. if (boot_params.alt_mem_k
  1238. < boot_params.screen_info.ext_mem_k) {
  1239. mem_size = boot_params.screen_info.ext_mem_k;
  1240. who = "BIOS-88";
  1241. } else {
  1242. mem_size = boot_params.alt_mem_k;
  1243. who = "BIOS-e801";
  1244. }
  1245. e820.nr_map = 0;
  1246. e820_add_region(0, LOWMEMSIZE(), E820_RAM);
  1247. e820_add_region(HIGH_MEMORY, mem_size << 10, E820_RAM);
  1248. }
  1249. /* In case someone cares... */
  1250. return who;
  1251. }
  1252. char *__init __attribute__((weak)) machine_specific_memory_setup(void)
  1253. {
  1254. if (x86_quirks->arch_memory_setup) {
  1255. char *who = x86_quirks->arch_memory_setup();
  1256. if (who)
  1257. return who;
  1258. }
  1259. return default_machine_specific_memory_setup();
  1260. }
  1261. /* Overridden in paravirt.c if CONFIG_PARAVIRT */
  1262. char * __init __attribute__((weak)) memory_setup(void)
  1263. {
  1264. return machine_specific_memory_setup();
  1265. }
  1266. void __init setup_memory_map(void)
  1267. {
  1268. char *who;
  1269. who = memory_setup();
  1270. memcpy(&e820_saved, &e820, sizeof(struct e820map));
  1271. printk(KERN_INFO "BIOS-provided physical RAM map:\n");
  1272. e820_print_map(who);
  1273. }