e820.c 27 KB

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