e820.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483
  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;
  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. * Early reserved memory areas.
  645. */
  646. #define MAX_EARLY_RES 20
  647. struct early_res {
  648. u64 start, end;
  649. char name[16];
  650. char overlap_ok;
  651. };
  652. static struct early_res early_res[MAX_EARLY_RES] __initdata = {
  653. { 0, PAGE_SIZE, "BIOS data page" }, /* BIOS data page */
  654. {}
  655. };
  656. static int __init find_overlapped_early(u64 start, u64 end)
  657. {
  658. int i;
  659. struct early_res *r;
  660. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
  661. r = &early_res[i];
  662. if (end > r->start && start < r->end)
  663. break;
  664. }
  665. return i;
  666. }
  667. /*
  668. * Drop the i-th range from the early reservation map,
  669. * by copying any higher ranges down one over it, and
  670. * clearing what had been the last slot.
  671. */
  672. static void __init drop_range(int i)
  673. {
  674. int j;
  675. for (j = i + 1; j < MAX_EARLY_RES && early_res[j].end; j++)
  676. ;
  677. memmove(&early_res[i], &early_res[i + 1],
  678. (j - 1 - i) * sizeof(struct early_res));
  679. early_res[j - 1].end = 0;
  680. }
  681. /*
  682. * Split any existing ranges that:
  683. * 1) are marked 'overlap_ok', and
  684. * 2) overlap with the stated range [start, end)
  685. * into whatever portion (if any) of the existing range is entirely
  686. * below or entirely above the stated range. Drop the portion
  687. * of the existing range that overlaps with the stated range,
  688. * which will allow the caller of this routine to then add that
  689. * stated range without conflicting with any existing range.
  690. */
  691. static void __init drop_overlaps_that_are_ok(u64 start, u64 end)
  692. {
  693. int i;
  694. struct early_res *r;
  695. u64 lower_start, lower_end;
  696. u64 upper_start, upper_end;
  697. char name[16];
  698. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
  699. r = &early_res[i];
  700. /* Continue past non-overlapping ranges */
  701. if (end <= r->start || start >= r->end)
  702. continue;
  703. /*
  704. * Leave non-ok overlaps as is; let caller
  705. * panic "Overlapping early reservations"
  706. * when it hits this overlap.
  707. */
  708. if (!r->overlap_ok)
  709. return;
  710. /*
  711. * We have an ok overlap. We will drop it from the early
  712. * reservation map, and add back in any non-overlapping
  713. * portions (lower or upper) as separate, overlap_ok,
  714. * non-overlapping ranges.
  715. */
  716. /* 1. Note any non-overlapping (lower or upper) ranges. */
  717. strncpy(name, r->name, sizeof(name) - 1);
  718. lower_start = lower_end = 0;
  719. upper_start = upper_end = 0;
  720. if (r->start < start) {
  721. lower_start = r->start;
  722. lower_end = start;
  723. }
  724. if (r->end > end) {
  725. upper_start = end;
  726. upper_end = r->end;
  727. }
  728. /* 2. Drop the original ok overlapping range */
  729. drop_range(i);
  730. i--; /* resume for-loop on copied down entry */
  731. /* 3. Add back in any non-overlapping ranges. */
  732. if (lower_end)
  733. reserve_early_overlap_ok(lower_start, lower_end, name);
  734. if (upper_end)
  735. reserve_early_overlap_ok(upper_start, upper_end, name);
  736. }
  737. }
  738. static void __init __reserve_early(u64 start, u64 end, char *name,
  739. int overlap_ok)
  740. {
  741. int i;
  742. struct early_res *r;
  743. i = find_overlapped_early(start, end);
  744. if (i >= MAX_EARLY_RES)
  745. panic("Too many early reservations");
  746. r = &early_res[i];
  747. if (r->end)
  748. panic("Overlapping early reservations "
  749. "%llx-%llx %s to %llx-%llx %s\n",
  750. start, end - 1, name?name:"", r->start,
  751. r->end - 1, r->name);
  752. r->start = start;
  753. r->end = end;
  754. r->overlap_ok = overlap_ok;
  755. if (name)
  756. strncpy(r->name, name, sizeof(r->name) - 1);
  757. }
  758. /*
  759. * A few early reservtations come here.
  760. *
  761. * The 'overlap_ok' in the name of this routine does -not- mean it
  762. * is ok for these reservations to overlap an earlier reservation.
  763. * Rather it means that it is ok for subsequent reservations to
  764. * overlap this one.
  765. *
  766. * Use this entry point to reserve early ranges when you are doing
  767. * so out of "Paranoia", reserving perhaps more memory than you need,
  768. * just in case, and don't mind a subsequent overlapping reservation
  769. * that is known to be needed.
  770. *
  771. * The drop_overlaps_that_are_ok() call here isn't really needed.
  772. * It would be needed if we had two colliding 'overlap_ok'
  773. * reservations, so that the second such would not panic on the
  774. * overlap with the first. We don't have any such as of this
  775. * writing, but might as well tolerate such if it happens in
  776. * the future.
  777. */
  778. void __init reserve_early_overlap_ok(u64 start, u64 end, char *name)
  779. {
  780. drop_overlaps_that_are_ok(start, end);
  781. __reserve_early(start, end, name, 1);
  782. }
  783. /*
  784. * Most early reservations come here.
  785. *
  786. * We first have drop_overlaps_that_are_ok() drop any pre-existing
  787. * 'overlap_ok' ranges, so that we can then reserve this memory
  788. * range without risk of panic'ing on an overlapping overlap_ok
  789. * early reservation.
  790. */
  791. void __init reserve_early(u64 start, u64 end, char *name)
  792. {
  793. if (start >= end)
  794. return;
  795. drop_overlaps_that_are_ok(start, end);
  796. __reserve_early(start, end, name, 0);
  797. }
  798. void __init free_early(u64 start, u64 end)
  799. {
  800. struct early_res *r;
  801. int i;
  802. i = find_overlapped_early(start, end);
  803. r = &early_res[i];
  804. if (i >= MAX_EARLY_RES || r->end != end || r->start != start)
  805. panic("free_early on not reserved area: %llx-%llx!",
  806. start, end - 1);
  807. drop_range(i);
  808. }
  809. void __init early_res_to_bootmem(u64 start, u64 end)
  810. {
  811. int i, count;
  812. u64 final_start, final_end;
  813. count = 0;
  814. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++)
  815. count++;
  816. printk(KERN_INFO "(%d early reservations) ==> bootmem [%010llx - %010llx]\n",
  817. count, start, end);
  818. for (i = 0; i < count; i++) {
  819. struct early_res *r = &early_res[i];
  820. printk(KERN_INFO " #%d [%010llx - %010llx] %16s", i,
  821. r->start, r->end, r->name);
  822. final_start = max(start, r->start);
  823. final_end = min(end, r->end);
  824. if (final_start >= final_end) {
  825. printk(KERN_CONT "\n");
  826. continue;
  827. }
  828. printk(KERN_CONT " ==> [%010llx - %010llx]\n",
  829. final_start, final_end);
  830. reserve_bootmem_generic(final_start, final_end - final_start,
  831. BOOTMEM_DEFAULT);
  832. }
  833. }
  834. /* Check for already reserved areas */
  835. static inline int __init bad_addr(u64 *addrp, u64 size, u64 align)
  836. {
  837. int i;
  838. u64 addr = *addrp;
  839. int changed = 0;
  840. struct early_res *r;
  841. again:
  842. i = find_overlapped_early(addr, addr + size);
  843. r = &early_res[i];
  844. if (i < MAX_EARLY_RES && r->end) {
  845. *addrp = addr = round_up(r->end, align);
  846. changed = 1;
  847. goto again;
  848. }
  849. return changed;
  850. }
  851. /* Check for already reserved areas */
  852. static inline int __init bad_addr_size(u64 *addrp, u64 *sizep, u64 align)
  853. {
  854. int i;
  855. u64 addr = *addrp, last;
  856. u64 size = *sizep;
  857. int changed = 0;
  858. again:
  859. last = addr + size;
  860. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
  861. struct early_res *r = &early_res[i];
  862. if (last > r->start && addr < r->start) {
  863. size = r->start - addr;
  864. changed = 1;
  865. goto again;
  866. }
  867. if (last > r->end && addr < r->end) {
  868. addr = round_up(r->end, align);
  869. size = last - addr;
  870. changed = 1;
  871. goto again;
  872. }
  873. if (last <= r->end && addr >= r->start) {
  874. (*sizep)++;
  875. return 0;
  876. }
  877. }
  878. if (changed) {
  879. *addrp = addr;
  880. *sizep = size;
  881. }
  882. return changed;
  883. }
  884. /*
  885. * Find a free area with specified alignment in a specific range.
  886. */
  887. u64 __init find_e820_area(u64 start, u64 end, u64 size, u64 align)
  888. {
  889. int i;
  890. for (i = 0; i < e820.nr_map; i++) {
  891. struct e820entry *ei = &e820.map[i];
  892. u64 addr, last;
  893. u64 ei_last;
  894. if (ei->type != E820_RAM)
  895. continue;
  896. addr = round_up(ei->addr, align);
  897. ei_last = ei->addr + ei->size;
  898. if (addr < start)
  899. addr = round_up(start, align);
  900. if (addr >= ei_last)
  901. continue;
  902. while (bad_addr(&addr, size, align) && addr+size <= ei_last)
  903. ;
  904. last = addr + size;
  905. if (last > ei_last)
  906. continue;
  907. if (last > end)
  908. continue;
  909. return addr;
  910. }
  911. return -1ULL;
  912. }
  913. /*
  914. * Find next free range after *start
  915. */
  916. u64 __init find_e820_area_size(u64 start, u64 *sizep, u64 align)
  917. {
  918. int i;
  919. for (i = 0; i < e820.nr_map; i++) {
  920. struct e820entry *ei = &e820.map[i];
  921. u64 addr, last;
  922. u64 ei_last;
  923. if (ei->type != E820_RAM)
  924. continue;
  925. addr = round_up(ei->addr, align);
  926. ei_last = ei->addr + ei->size;
  927. if (addr < start)
  928. addr = round_up(start, align);
  929. if (addr >= ei_last)
  930. continue;
  931. *sizep = ei_last - addr;
  932. while (bad_addr_size(&addr, sizep, align) &&
  933. addr + *sizep <= ei_last)
  934. ;
  935. last = addr + *sizep;
  936. if (last > ei_last)
  937. continue;
  938. return addr;
  939. }
  940. return -1ULL;
  941. }
  942. /*
  943. * pre allocated 4k and reserved it in e820
  944. */
  945. u64 __init early_reserve_e820(u64 startt, u64 sizet, u64 align)
  946. {
  947. u64 size = 0;
  948. u64 addr;
  949. u64 start;
  950. for (start = startt; ; start += size) {
  951. start = find_e820_area_size(start, &size, align);
  952. if (!(start + 1))
  953. return 0;
  954. if (size >= sizet)
  955. break;
  956. }
  957. #ifdef CONFIG_X86_32
  958. if (start >= MAXMEM)
  959. return 0;
  960. if (start + size > MAXMEM)
  961. size = MAXMEM - start;
  962. #endif
  963. addr = round_down(start + size - sizet, align);
  964. if (addr < start)
  965. return 0;
  966. e820_update_range(addr, sizet, E820_RAM, E820_RESERVED);
  967. e820_update_range_saved(addr, sizet, E820_RAM, E820_RESERVED);
  968. printk(KERN_INFO "update e820 for early_reserve_e820\n");
  969. update_e820();
  970. update_e820_saved();
  971. return addr;
  972. }
  973. #ifdef CONFIG_X86_32
  974. # ifdef CONFIG_X86_PAE
  975. # define MAX_ARCH_PFN (1ULL<<(36-PAGE_SHIFT))
  976. # else
  977. # define MAX_ARCH_PFN (1ULL<<(32-PAGE_SHIFT))
  978. # endif
  979. #else /* CONFIG_X86_32 */
  980. # define MAX_ARCH_PFN MAXMEM>>PAGE_SHIFT
  981. #endif
  982. /*
  983. * Find the highest page frame number we have available
  984. */
  985. static unsigned long __init e820_end_pfn(unsigned long limit_pfn, unsigned type)
  986. {
  987. int i;
  988. unsigned long last_pfn = 0;
  989. unsigned long max_arch_pfn = MAX_ARCH_PFN;
  990. for (i = 0; i < e820.nr_map; i++) {
  991. struct e820entry *ei = &e820.map[i];
  992. unsigned long start_pfn;
  993. unsigned long end_pfn;
  994. if (ei->type != type)
  995. continue;
  996. start_pfn = ei->addr >> PAGE_SHIFT;
  997. end_pfn = (ei->addr + ei->size) >> PAGE_SHIFT;
  998. if (start_pfn >= limit_pfn)
  999. continue;
  1000. if (end_pfn > limit_pfn) {
  1001. last_pfn = limit_pfn;
  1002. break;
  1003. }
  1004. if (end_pfn > last_pfn)
  1005. last_pfn = end_pfn;
  1006. }
  1007. if (last_pfn > max_arch_pfn)
  1008. last_pfn = max_arch_pfn;
  1009. printk(KERN_INFO "last_pfn = %#lx max_arch_pfn = %#lx\n",
  1010. last_pfn, max_arch_pfn);
  1011. return last_pfn;
  1012. }
  1013. unsigned long __init e820_end_of_ram_pfn(void)
  1014. {
  1015. return e820_end_pfn(MAX_ARCH_PFN, E820_RAM);
  1016. }
  1017. unsigned long __init e820_end_of_low_ram_pfn(void)
  1018. {
  1019. return e820_end_pfn(1UL<<(32 - PAGE_SHIFT), E820_RAM);
  1020. }
  1021. /*
  1022. * Finds an active region in the address range from start_pfn to last_pfn and
  1023. * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
  1024. */
  1025. int __init e820_find_active_region(const struct e820entry *ei,
  1026. unsigned long start_pfn,
  1027. unsigned long last_pfn,
  1028. unsigned long *ei_startpfn,
  1029. unsigned long *ei_endpfn)
  1030. {
  1031. u64 align = PAGE_SIZE;
  1032. *ei_startpfn = round_up(ei->addr, align) >> PAGE_SHIFT;
  1033. *ei_endpfn = round_down(ei->addr + ei->size, align) >> PAGE_SHIFT;
  1034. /* Skip map entries smaller than a page */
  1035. if (*ei_startpfn >= *ei_endpfn)
  1036. return 0;
  1037. /* Skip if map is outside the node */
  1038. if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
  1039. *ei_startpfn >= last_pfn)
  1040. return 0;
  1041. /* Check for overlaps */
  1042. if (*ei_startpfn < start_pfn)
  1043. *ei_startpfn = start_pfn;
  1044. if (*ei_endpfn > last_pfn)
  1045. *ei_endpfn = last_pfn;
  1046. return 1;
  1047. }
  1048. /* Walk the e820 map and register active regions within a node */
  1049. void __init e820_register_active_regions(int nid, unsigned long start_pfn,
  1050. unsigned long last_pfn)
  1051. {
  1052. unsigned long ei_startpfn;
  1053. unsigned long ei_endpfn;
  1054. int i;
  1055. for (i = 0; i < e820.nr_map; i++)
  1056. if (e820_find_active_region(&e820.map[i],
  1057. start_pfn, last_pfn,
  1058. &ei_startpfn, &ei_endpfn))
  1059. add_active_range(nid, ei_startpfn, ei_endpfn);
  1060. }
  1061. /*
  1062. * Find the hole size (in bytes) in the memory range.
  1063. * @start: starting address of the memory range to scan
  1064. * @end: ending address of the memory range to scan
  1065. */
  1066. u64 __init e820_hole_size(u64 start, u64 end)
  1067. {
  1068. unsigned long start_pfn = start >> PAGE_SHIFT;
  1069. unsigned long last_pfn = end >> PAGE_SHIFT;
  1070. unsigned long ei_startpfn, ei_endpfn, ram = 0;
  1071. int i;
  1072. for (i = 0; i < e820.nr_map; i++) {
  1073. if (e820_find_active_region(&e820.map[i],
  1074. start_pfn, last_pfn,
  1075. &ei_startpfn, &ei_endpfn))
  1076. ram += ei_endpfn - ei_startpfn;
  1077. }
  1078. return end - start - ((u64)ram << PAGE_SHIFT);
  1079. }
  1080. static void early_panic(char *msg)
  1081. {
  1082. early_printk(msg);
  1083. panic(msg);
  1084. }
  1085. static int userdef __initdata;
  1086. /* "mem=nopentium" disables the 4MB page tables. */
  1087. static int __init parse_memopt(char *p)
  1088. {
  1089. u64 mem_size;
  1090. if (!p)
  1091. return -EINVAL;
  1092. #ifdef CONFIG_X86_32
  1093. if (!strcmp(p, "nopentium")) {
  1094. setup_clear_cpu_cap(X86_FEATURE_PSE);
  1095. return 0;
  1096. }
  1097. #endif
  1098. userdef = 1;
  1099. mem_size = memparse(p, &p);
  1100. e820_remove_range(mem_size, ULLONG_MAX - mem_size, E820_RAM, 1);
  1101. return 0;
  1102. }
  1103. early_param("mem", parse_memopt);
  1104. static int __init parse_memmap_opt(char *p)
  1105. {
  1106. char *oldp;
  1107. u64 start_at, mem_size;
  1108. if (!p)
  1109. return -EINVAL;
  1110. if (!strncmp(p, "exactmap", 8)) {
  1111. #ifdef CONFIG_CRASH_DUMP
  1112. /*
  1113. * If we are doing a crash dump, we still need to know
  1114. * the real mem size before original memory map is
  1115. * reset.
  1116. */
  1117. saved_max_pfn = e820_end_of_ram_pfn();
  1118. #endif
  1119. e820.nr_map = 0;
  1120. userdef = 1;
  1121. return 0;
  1122. }
  1123. oldp = p;
  1124. mem_size = memparse(p, &p);
  1125. if (p == oldp)
  1126. return -EINVAL;
  1127. userdef = 1;
  1128. if (*p == '@') {
  1129. start_at = memparse(p+1, &p);
  1130. e820_add_region(start_at, mem_size, E820_RAM);
  1131. } else if (*p == '#') {
  1132. start_at = memparse(p+1, &p);
  1133. e820_add_region(start_at, mem_size, E820_ACPI);
  1134. } else if (*p == '$') {
  1135. start_at = memparse(p+1, &p);
  1136. e820_add_region(start_at, mem_size, E820_RESERVED);
  1137. } else
  1138. e820_remove_range(mem_size, ULLONG_MAX - mem_size, E820_RAM, 1);
  1139. return *p == '\0' ? 0 : -EINVAL;
  1140. }
  1141. early_param("memmap", parse_memmap_opt);
  1142. void __init finish_e820_parsing(void)
  1143. {
  1144. if (userdef) {
  1145. u32 nr = e820.nr_map;
  1146. if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr) < 0)
  1147. early_panic("Invalid user supplied memory map");
  1148. e820.nr_map = nr;
  1149. printk(KERN_INFO "user-defined physical RAM map:\n");
  1150. e820_print_map("user");
  1151. }
  1152. }
  1153. static inline const char *e820_type_to_string(int e820_type)
  1154. {
  1155. switch (e820_type) {
  1156. case E820_RESERVED_KERN:
  1157. case E820_RAM: return "System RAM";
  1158. case E820_ACPI: return "ACPI Tables";
  1159. case E820_NVS: return "ACPI Non-volatile Storage";
  1160. case E820_UNUSABLE: return "Unusable memory";
  1161. default: return "reserved";
  1162. }
  1163. }
  1164. /*
  1165. * Mark e820 reserved areas as busy for the resource manager.
  1166. */
  1167. static struct resource __initdata *e820_res;
  1168. void __init e820_reserve_resources(void)
  1169. {
  1170. int i;
  1171. struct resource *res;
  1172. u64 end;
  1173. res = alloc_bootmem_low(sizeof(struct resource) * e820.nr_map);
  1174. e820_res = res;
  1175. for (i = 0; i < e820.nr_map; i++) {
  1176. end = e820.map[i].addr + e820.map[i].size - 1;
  1177. if (end != (resource_size_t)end) {
  1178. res++;
  1179. continue;
  1180. }
  1181. res->name = e820_type_to_string(e820.map[i].type);
  1182. res->start = e820.map[i].addr;
  1183. res->end = end;
  1184. res->flags = IORESOURCE_MEM;
  1185. /*
  1186. * don't register the region that could be conflicted with
  1187. * pci device BAR resource and insert them later in
  1188. * pcibios_resource_survey()
  1189. */
  1190. if (e820.map[i].type != E820_RESERVED || res->start < (1ULL<<20)) {
  1191. res->flags |= IORESOURCE_BUSY;
  1192. insert_resource(&iomem_resource, res);
  1193. }
  1194. res++;
  1195. }
  1196. for (i = 0; i < e820_saved.nr_map; i++) {
  1197. struct e820entry *entry = &e820_saved.map[i];
  1198. firmware_map_add_early(entry->addr,
  1199. entry->addr + entry->size - 1,
  1200. e820_type_to_string(entry->type));
  1201. }
  1202. }
  1203. /* How much should we pad RAM ending depending on where it is? */
  1204. static unsigned long ram_alignment(resource_size_t pos)
  1205. {
  1206. unsigned long mb = pos >> 20;
  1207. /* To 64kB in the first megabyte */
  1208. if (!mb)
  1209. return 64*1024;
  1210. /* To 1MB in the first 16MB */
  1211. if (mb < 16)
  1212. return 1024*1024;
  1213. /* To 32MB for anything above that */
  1214. return 32*1024*1024;
  1215. }
  1216. #define MAX_RESOURCE_SIZE ((resource_size_t)-1)
  1217. void __init e820_reserve_resources_late(void)
  1218. {
  1219. int i;
  1220. struct resource *res;
  1221. res = e820_res;
  1222. for (i = 0; i < e820.nr_map; i++) {
  1223. if (!res->parent && res->end)
  1224. insert_resource_expand_to_fit(&iomem_resource, res);
  1225. res++;
  1226. }
  1227. /*
  1228. * Try to bump up RAM regions to reasonable boundaries to
  1229. * avoid stolen RAM:
  1230. */
  1231. for (i = 0; i < e820.nr_map; i++) {
  1232. struct e820entry *entry = &e820.map[i];
  1233. u64 start, end;
  1234. if (entry->type != E820_RAM)
  1235. continue;
  1236. start = entry->addr + entry->size;
  1237. end = round_up(start, ram_alignment(start)) - 1;
  1238. if (end > MAX_RESOURCE_SIZE)
  1239. end = MAX_RESOURCE_SIZE;
  1240. if (start >= end)
  1241. continue;
  1242. reserve_region_with_split(&iomem_resource, start, end,
  1243. "RAM buffer");
  1244. }
  1245. }
  1246. char *__init default_machine_specific_memory_setup(void)
  1247. {
  1248. char *who = "BIOS-e820";
  1249. u32 new_nr;
  1250. /*
  1251. * Try to copy the BIOS-supplied E820-map.
  1252. *
  1253. * Otherwise fake a memory map; one section from 0k->640k,
  1254. * the next section from 1mb->appropriate_mem_k
  1255. */
  1256. new_nr = boot_params.e820_entries;
  1257. sanitize_e820_map(boot_params.e820_map,
  1258. ARRAY_SIZE(boot_params.e820_map),
  1259. &new_nr);
  1260. boot_params.e820_entries = new_nr;
  1261. if (append_e820_map(boot_params.e820_map, boot_params.e820_entries)
  1262. < 0) {
  1263. u64 mem_size;
  1264. /* compare results from other methods and take the greater */
  1265. if (boot_params.alt_mem_k
  1266. < boot_params.screen_info.ext_mem_k) {
  1267. mem_size = boot_params.screen_info.ext_mem_k;
  1268. who = "BIOS-88";
  1269. } else {
  1270. mem_size = boot_params.alt_mem_k;
  1271. who = "BIOS-e801";
  1272. }
  1273. e820.nr_map = 0;
  1274. e820_add_region(0, LOWMEMSIZE(), E820_RAM);
  1275. e820_add_region(HIGH_MEMORY, mem_size << 10, E820_RAM);
  1276. }
  1277. /* In case someone cares... */
  1278. return who;
  1279. }
  1280. char *__init __attribute__((weak)) machine_specific_memory_setup(void)
  1281. {
  1282. if (x86_quirks->arch_memory_setup) {
  1283. char *who = x86_quirks->arch_memory_setup();
  1284. if (who)
  1285. return who;
  1286. }
  1287. return default_machine_specific_memory_setup();
  1288. }
  1289. /* Overridden in paravirt.c if CONFIG_PARAVIRT */
  1290. char * __init __attribute__((weak)) memory_setup(void)
  1291. {
  1292. return machine_specific_memory_setup();
  1293. }
  1294. void __init setup_memory_map(void)
  1295. {
  1296. char *who;
  1297. who = memory_setup();
  1298. memcpy(&e820_saved, &e820, sizeof(struct e820map));
  1299. printk(KERN_INFO "BIOS-provided physical RAM map:\n");
  1300. e820_print_map(who);
  1301. }