e820.c 35 KB

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