e820.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  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. if (size > (ULLONG_MAX - start))
  371. size = ULLONG_MAX - start;
  372. for (i = 0; i < e820.nr_map; i++) {
  373. struct e820entry *ei = &e820.map[i];
  374. u64 final_start, final_end;
  375. if (ei->type != old_type)
  376. continue;
  377. /* totally covered? */
  378. if (ei->addr >= start &&
  379. (ei->addr + ei->size) <= (start + size)) {
  380. ei->type = new_type;
  381. real_updated_size += ei->size;
  382. continue;
  383. }
  384. /* partially covered */
  385. final_start = max(start, ei->addr);
  386. final_end = min(start + size, ei->addr + ei->size);
  387. if (final_start >= final_end)
  388. continue;
  389. e820_add_region(final_start, final_end - final_start,
  390. new_type);
  391. real_updated_size += final_end - final_start;
  392. ei->size -= final_end - final_start;
  393. if (ei->addr < final_start)
  394. continue;
  395. ei->addr = final_end;
  396. }
  397. return real_updated_size;
  398. }
  399. /* make e820 not cover the range */
  400. u64 __init e820_remove_range(u64 start, u64 size, unsigned old_type,
  401. int checktype)
  402. {
  403. int i;
  404. u64 real_removed_size = 0;
  405. if (size > (ULLONG_MAX - start))
  406. size = ULLONG_MAX - start;
  407. for (i = 0; i < e820.nr_map; i++) {
  408. struct e820entry *ei = &e820.map[i];
  409. u64 final_start, final_end;
  410. if (checktype && ei->type != old_type)
  411. continue;
  412. /* totally covered? */
  413. if (ei->addr >= start &&
  414. (ei->addr + ei->size) <= (start + size)) {
  415. real_removed_size += ei->size;
  416. memset(ei, 0, sizeof(struct e820entry));
  417. continue;
  418. }
  419. /* partially covered */
  420. final_start = max(start, ei->addr);
  421. final_end = min(start + size, ei->addr + ei->size);
  422. if (final_start >= final_end)
  423. continue;
  424. real_removed_size += final_end - final_start;
  425. ei->size -= final_end - final_start;
  426. if (ei->addr < final_start)
  427. continue;
  428. ei->addr = final_end;
  429. }
  430. return real_removed_size;
  431. }
  432. void __init update_e820(void)
  433. {
  434. int nr_map;
  435. nr_map = e820.nr_map;
  436. if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr_map))
  437. return;
  438. e820.nr_map = nr_map;
  439. printk(KERN_INFO "modified physical RAM map:\n");
  440. e820_print_map("modified");
  441. }
  442. /*
  443. * Search for the biggest gap in the low 32 bits of the e820
  444. * memory space. We pass this space to PCI to assign MMIO resources
  445. * for hotplug or unconfigured devices in.
  446. * Hopefully the BIOS let enough space left.
  447. */
  448. __init void e820_setup_gap(void)
  449. {
  450. unsigned long gapstart, gapsize, round;
  451. unsigned long long last;
  452. int i;
  453. int found = 0;
  454. last = 0x100000000ull;
  455. gapstart = 0x10000000;
  456. gapsize = 0x400000;
  457. i = e820.nr_map;
  458. while (--i >= 0) {
  459. unsigned long long start = e820.map[i].addr;
  460. unsigned long long end = start + e820.map[i].size;
  461. /*
  462. * Since "last" is at most 4GB, we know we'll
  463. * fit in 32 bits if this condition is true
  464. */
  465. if (last > end) {
  466. unsigned long gap = last - end;
  467. if (gap > gapsize) {
  468. gapsize = gap;
  469. gapstart = end;
  470. found = 1;
  471. }
  472. }
  473. if (start < last)
  474. last = start;
  475. }
  476. #ifdef CONFIG_X86_64
  477. if (!found) {
  478. gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
  479. printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit "
  480. "address range\n"
  481. KERN_ERR "PCI: Unassigned devices with 32bit resource "
  482. "registers may break!\n");
  483. }
  484. #endif
  485. /*
  486. * See how much we want to round up: start off with
  487. * rounding to the next 1MB area.
  488. */
  489. round = 0x100000;
  490. while ((gapsize >> 4) > round)
  491. round += round;
  492. /* Fun with two's complement */
  493. pci_mem_start = (gapstart + round) & -round;
  494. printk(KERN_INFO
  495. "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
  496. pci_mem_start, gapstart, gapsize);
  497. }
  498. /**
  499. * Because of the size limitation of struct boot_params, only first
  500. * 128 E820 memory entries are passed to kernel via
  501. * boot_params.e820_map, others are passed via SETUP_E820_EXT node of
  502. * linked list of struct setup_data, which is parsed here.
  503. */
  504. void __init parse_e820_ext(struct setup_data *sdata, unsigned long pa_data)
  505. {
  506. u32 map_len;
  507. int entries;
  508. struct e820entry *extmap;
  509. entries = sdata->len / sizeof(struct e820entry);
  510. map_len = sdata->len + sizeof(struct setup_data);
  511. if (map_len > PAGE_SIZE)
  512. sdata = early_ioremap(pa_data, map_len);
  513. extmap = (struct e820entry *)(sdata->data);
  514. __copy_e820_map(extmap, entries);
  515. sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
  516. if (map_len > PAGE_SIZE)
  517. early_iounmap(sdata, map_len);
  518. printk(KERN_INFO "extended physical RAM map:\n");
  519. e820_print_map("extended");
  520. }
  521. #if defined(CONFIG_X86_64) || \
  522. (defined(CONFIG_X86_32) && defined(CONFIG_HIBERNATION))
  523. /**
  524. * Find the ranges of physical addresses that do not correspond to
  525. * e820 RAM areas and mark the corresponding pages as nosave for
  526. * hibernation (32 bit) or software suspend and suspend to RAM (64 bit).
  527. *
  528. * This function requires the e820 map to be sorted and without any
  529. * overlapping entries and assumes the first e820 area to be RAM.
  530. */
  531. void __init e820_mark_nosave_regions(unsigned long limit_pfn)
  532. {
  533. int i;
  534. unsigned long pfn;
  535. pfn = PFN_DOWN(e820.map[0].addr + e820.map[0].size);
  536. for (i = 1; i < e820.nr_map; i++) {
  537. struct e820entry *ei = &e820.map[i];
  538. if (pfn < PFN_UP(ei->addr))
  539. register_nosave_region(pfn, PFN_UP(ei->addr));
  540. pfn = PFN_DOWN(ei->addr + ei->size);
  541. if (ei->type != E820_RAM)
  542. register_nosave_region(PFN_UP(ei->addr), pfn);
  543. if (pfn >= limit_pfn)
  544. break;
  545. }
  546. }
  547. #endif
  548. /*
  549. * Early reserved memory areas.
  550. */
  551. #define MAX_EARLY_RES 20
  552. struct early_res {
  553. u64 start, end;
  554. char name[16];
  555. char overlap_ok;
  556. };
  557. static struct early_res early_res[MAX_EARLY_RES] __initdata = {
  558. { 0, PAGE_SIZE, "BIOS data page" }, /* BIOS data page */
  559. #if defined(CONFIG_X86_64) && defined(CONFIG_X86_TRAMPOLINE)
  560. { TRAMPOLINE_BASE, TRAMPOLINE_BASE + 2 * PAGE_SIZE, "TRAMPOLINE" },
  561. #endif
  562. #if defined(CONFIG_X86_32) && defined(CONFIG_SMP)
  563. /*
  564. * But first pinch a few for the stack/trampoline stuff
  565. * FIXME: Don't need the extra page at 4K, but need to fix
  566. * trampoline before removing it. (see the GDT stuff)
  567. */
  568. { PAGE_SIZE, PAGE_SIZE + PAGE_SIZE, "EX TRAMPOLINE" },
  569. /*
  570. * Has to be in very low memory so we can execute
  571. * real-mode AP code.
  572. */
  573. { TRAMPOLINE_BASE, TRAMPOLINE_BASE + PAGE_SIZE, "TRAMPOLINE" },
  574. #endif
  575. {}
  576. };
  577. static int __init find_overlapped_early(u64 start, u64 end)
  578. {
  579. int i;
  580. struct early_res *r;
  581. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
  582. r = &early_res[i];
  583. if (end > r->start && start < r->end)
  584. break;
  585. }
  586. return i;
  587. }
  588. /*
  589. * Drop the i-th range from the early reservation map,
  590. * by copying any higher ranges down one over it, and
  591. * clearing what had been the last slot.
  592. */
  593. static void __init drop_range(int i)
  594. {
  595. int j;
  596. for (j = i + 1; j < MAX_EARLY_RES && early_res[j].end; j++)
  597. ;
  598. memmove(&early_res[i], &early_res[i + 1],
  599. (j - 1 - i) * sizeof(struct early_res));
  600. early_res[j - 1].end = 0;
  601. }
  602. /*
  603. * Split any existing ranges that:
  604. * 1) are marked 'overlap_ok', and
  605. * 2) overlap with the stated range [start, end)
  606. * into whatever portion (if any) of the existing range is entirely
  607. * below or entirely above the stated range. Drop the portion
  608. * of the existing range that overlaps with the stated range,
  609. * which will allow the caller of this routine to then add that
  610. * stated range without conflicting with any existing range.
  611. */
  612. static void __init drop_overlaps_that_are_ok(u64 start, u64 end)
  613. {
  614. int i;
  615. struct early_res *r;
  616. u64 lower_start, lower_end;
  617. u64 upper_start, upper_end;
  618. char name[16];
  619. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
  620. r = &early_res[i];
  621. /* Continue past non-overlapping ranges */
  622. if (end <= r->start || start >= r->end)
  623. continue;
  624. /*
  625. * Leave non-ok overlaps as is; let caller
  626. * panic "Overlapping early reservations"
  627. * when it hits this overlap.
  628. */
  629. if (!r->overlap_ok)
  630. return;
  631. /*
  632. * We have an ok overlap. We will drop it from the early
  633. * reservation map, and add back in any non-overlapping
  634. * portions (lower or upper) as separate, overlap_ok,
  635. * non-overlapping ranges.
  636. */
  637. /* 1. Note any non-overlapping (lower or upper) ranges. */
  638. strncpy(name, r->name, sizeof(name) - 1);
  639. lower_start = lower_end = 0;
  640. upper_start = upper_end = 0;
  641. if (r->start < start) {
  642. lower_start = r->start;
  643. lower_end = start;
  644. }
  645. if (r->end > end) {
  646. upper_start = end;
  647. upper_end = r->end;
  648. }
  649. /* 2. Drop the original ok overlapping range */
  650. drop_range(i);
  651. i--; /* resume for-loop on copied down entry */
  652. /* 3. Add back in any non-overlapping ranges. */
  653. if (lower_end)
  654. reserve_early_overlap_ok(lower_start, lower_end, name);
  655. if (upper_end)
  656. reserve_early_overlap_ok(upper_start, upper_end, name);
  657. }
  658. }
  659. static void __init __reserve_early(u64 start, u64 end, char *name,
  660. int overlap_ok)
  661. {
  662. int i;
  663. struct early_res *r;
  664. i = find_overlapped_early(start, end);
  665. if (i >= MAX_EARLY_RES)
  666. panic("Too many early reservations");
  667. r = &early_res[i];
  668. if (r->end)
  669. panic("Overlapping early reservations "
  670. "%llx-%llx %s to %llx-%llx %s\n",
  671. start, end - 1, name?name:"", r->start,
  672. r->end - 1, r->name);
  673. r->start = start;
  674. r->end = end;
  675. r->overlap_ok = overlap_ok;
  676. if (name)
  677. strncpy(r->name, name, sizeof(r->name) - 1);
  678. }
  679. /*
  680. * A few early reservtations come here.
  681. *
  682. * The 'overlap_ok' in the name of this routine does -not- mean it
  683. * is ok for these reservations to overlap an earlier reservation.
  684. * Rather it means that it is ok for subsequent reservations to
  685. * overlap this one.
  686. *
  687. * Use this entry point to reserve early ranges when you are doing
  688. * so out of "Paranoia", reserving perhaps more memory than you need,
  689. * just in case, and don't mind a subsequent overlapping reservation
  690. * that is known to be needed.
  691. *
  692. * The drop_overlaps_that_are_ok() call here isn't really needed.
  693. * It would be needed if we had two colliding 'overlap_ok'
  694. * reservations, so that the second such would not panic on the
  695. * overlap with the first. We don't have any such as of this
  696. * writing, but might as well tolerate such if it happens in
  697. * the future.
  698. */
  699. void __init reserve_early_overlap_ok(u64 start, u64 end, char *name)
  700. {
  701. drop_overlaps_that_are_ok(start, end);
  702. __reserve_early(start, end, name, 1);
  703. }
  704. /*
  705. * Most early reservations come here.
  706. *
  707. * We first have drop_overlaps_that_are_ok() drop any pre-existing
  708. * 'overlap_ok' ranges, so that we can then reserve this memory
  709. * range without risk of panic'ing on an overlapping overlap_ok
  710. * early reservation.
  711. */
  712. void __init reserve_early(u64 start, u64 end, char *name)
  713. {
  714. drop_overlaps_that_are_ok(start, end);
  715. __reserve_early(start, end, name, 0);
  716. }
  717. void __init free_early(u64 start, u64 end)
  718. {
  719. struct early_res *r;
  720. int i;
  721. i = find_overlapped_early(start, end);
  722. r = &early_res[i];
  723. if (i >= MAX_EARLY_RES || r->end != end || r->start != start)
  724. panic("free_early on not reserved area: %llx-%llx!",
  725. start, end - 1);
  726. drop_range(i);
  727. }
  728. void __init early_res_to_bootmem(u64 start, u64 end)
  729. {
  730. int i;
  731. u64 final_start, final_end;
  732. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
  733. struct early_res *r = &early_res[i];
  734. final_start = max(start, r->start);
  735. final_end = min(end, r->end);
  736. if (final_start >= final_end)
  737. continue;
  738. printk(KERN_INFO " early res: %d [%llx-%llx] %s\n", i,
  739. final_start, final_end - 1, r->name);
  740. reserve_bootmem_generic(final_start, final_end - final_start,
  741. BOOTMEM_DEFAULT);
  742. }
  743. }
  744. /* Check for already reserved areas */
  745. static inline int __init bad_addr(u64 *addrp, u64 size, u64 align)
  746. {
  747. int i;
  748. u64 addr = *addrp;
  749. int changed = 0;
  750. struct early_res *r;
  751. again:
  752. i = find_overlapped_early(addr, addr + size);
  753. r = &early_res[i];
  754. if (i < MAX_EARLY_RES && r->end) {
  755. *addrp = addr = round_up(r->end, align);
  756. changed = 1;
  757. goto again;
  758. }
  759. return changed;
  760. }
  761. /* Check for already reserved areas */
  762. static inline int __init bad_addr_size(u64 *addrp, u64 *sizep, u64 align)
  763. {
  764. int i;
  765. u64 addr = *addrp, last;
  766. u64 size = *sizep;
  767. int changed = 0;
  768. again:
  769. last = addr + size;
  770. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
  771. struct early_res *r = &early_res[i];
  772. if (last > r->start && addr < r->start) {
  773. size = r->start - addr;
  774. changed = 1;
  775. goto again;
  776. }
  777. if (last > r->end && addr < r->end) {
  778. addr = round_up(r->end, align);
  779. size = last - addr;
  780. changed = 1;
  781. goto again;
  782. }
  783. if (last <= r->end && addr >= r->start) {
  784. (*sizep)++;
  785. return 0;
  786. }
  787. }
  788. if (changed) {
  789. *addrp = addr;
  790. *sizep = size;
  791. }
  792. return changed;
  793. }
  794. /*
  795. * Find a free area with specified alignment in a specific range.
  796. */
  797. u64 __init find_e820_area(u64 start, u64 end, u64 size, u64 align)
  798. {
  799. int i;
  800. for (i = 0; i < e820.nr_map; i++) {
  801. struct e820entry *ei = &e820.map[i];
  802. u64 addr, last;
  803. u64 ei_last;
  804. if (ei->type != E820_RAM)
  805. continue;
  806. addr = round_up(ei->addr, align);
  807. ei_last = ei->addr + ei->size;
  808. if (addr < start)
  809. addr = round_up(start, align);
  810. if (addr >= ei_last)
  811. continue;
  812. while (bad_addr(&addr, size, align) && addr+size <= ei_last)
  813. ;
  814. last = addr + size;
  815. if (last > ei_last)
  816. continue;
  817. if (last > end)
  818. continue;
  819. return addr;
  820. }
  821. return -1ULL;
  822. }
  823. /*
  824. * Find next free range after *start
  825. */
  826. u64 __init find_e820_area_size(u64 start, u64 *sizep, u64 align)
  827. {
  828. int i;
  829. for (i = 0; i < e820.nr_map; i++) {
  830. struct e820entry *ei = &e820.map[i];
  831. u64 addr, last;
  832. u64 ei_last;
  833. if (ei->type != E820_RAM)
  834. continue;
  835. addr = round_up(ei->addr, align);
  836. ei_last = ei->addr + ei->size;
  837. if (addr < start)
  838. addr = round_up(start, align);
  839. if (addr >= ei_last)
  840. continue;
  841. *sizep = ei_last - addr;
  842. while (bad_addr_size(&addr, sizep, align) &&
  843. addr + *sizep <= ei_last)
  844. ;
  845. last = addr + *sizep;
  846. if (last > ei_last)
  847. continue;
  848. return addr;
  849. }
  850. return -1UL;
  851. }
  852. /*
  853. * pre allocated 4k and reserved it in e820
  854. */
  855. u64 __init early_reserve_e820(u64 startt, u64 sizet, u64 align)
  856. {
  857. u64 size = 0;
  858. u64 addr;
  859. u64 start;
  860. start = startt;
  861. while (size < sizet)
  862. start = find_e820_area_size(start, &size, align);
  863. if (size < sizet)
  864. return 0;
  865. addr = round_down(start + size - sizet, align);
  866. e820_update_range(addr, sizet, E820_RAM, E820_RESERVED);
  867. printk(KERN_INFO "update e820 for early_reserve_e820\n");
  868. update_e820();
  869. return addr;
  870. }
  871. #ifdef CONFIG_X86_32
  872. # ifdef CONFIG_X86_PAE
  873. # define MAX_ARCH_PFN (1ULL<<(36-PAGE_SHIFT))
  874. # else
  875. # define MAX_ARCH_PFN (1ULL<<(32-PAGE_SHIFT))
  876. # endif
  877. #else /* CONFIG_X86_32 */
  878. # define MAX_ARCH_PFN MAXMEM>>PAGE_SHIFT
  879. #endif
  880. /*
  881. * Last pfn which the user wants to use.
  882. */
  883. unsigned long __initdata end_user_pfn = MAX_ARCH_PFN;
  884. /*
  885. * Find the highest page frame number we have available
  886. */
  887. unsigned long __init e820_end_of_ram(void)
  888. {
  889. unsigned long last_pfn;
  890. unsigned long max_arch_pfn = MAX_ARCH_PFN;
  891. last_pfn = find_max_pfn_with_active_regions();
  892. if (last_pfn > max_arch_pfn)
  893. last_pfn = max_arch_pfn;
  894. if (last_pfn > end_user_pfn)
  895. last_pfn = end_user_pfn;
  896. printk(KERN_INFO "last_pfn = 0x%lx max_arch_pfn = 0x%lx\n",
  897. last_pfn, max_arch_pfn);
  898. return last_pfn;
  899. }
  900. /*
  901. * Finds an active region in the address range from start_pfn to last_pfn and
  902. * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
  903. */
  904. int __init e820_find_active_region(const struct e820entry *ei,
  905. unsigned long start_pfn,
  906. unsigned long last_pfn,
  907. unsigned long *ei_startpfn,
  908. unsigned long *ei_endpfn)
  909. {
  910. u64 align = PAGE_SIZE;
  911. *ei_startpfn = round_up(ei->addr, align) >> PAGE_SHIFT;
  912. *ei_endpfn = round_down(ei->addr + ei->size, align) >> PAGE_SHIFT;
  913. /* Skip map entries smaller than a page */
  914. if (*ei_startpfn >= *ei_endpfn)
  915. return 0;
  916. /* Skip if map is outside the node */
  917. if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
  918. *ei_startpfn >= last_pfn)
  919. return 0;
  920. /* Check for overlaps */
  921. if (*ei_startpfn < start_pfn)
  922. *ei_startpfn = start_pfn;
  923. if (*ei_endpfn > last_pfn)
  924. *ei_endpfn = last_pfn;
  925. /* Obey end_user_pfn to save on memmap */
  926. if (*ei_startpfn >= end_user_pfn)
  927. return 0;
  928. if (*ei_endpfn > end_user_pfn)
  929. *ei_endpfn = end_user_pfn;
  930. return 1;
  931. }
  932. /* Walk the e820 map and register active regions within a node */
  933. void __init e820_register_active_regions(int nid, unsigned long start_pfn,
  934. unsigned long last_pfn)
  935. {
  936. unsigned long ei_startpfn;
  937. unsigned long ei_endpfn;
  938. int i;
  939. for (i = 0; i < e820.nr_map; i++)
  940. if (e820_find_active_region(&e820.map[i],
  941. start_pfn, last_pfn,
  942. &ei_startpfn, &ei_endpfn))
  943. add_active_range(nid, ei_startpfn, ei_endpfn);
  944. }
  945. /*
  946. * Find the hole size (in bytes) in the memory range.
  947. * @start: starting address of the memory range to scan
  948. * @end: ending address of the memory range to scan
  949. */
  950. u64 __init e820_hole_size(u64 start, u64 end)
  951. {
  952. unsigned long start_pfn = start >> PAGE_SHIFT;
  953. unsigned long last_pfn = end >> PAGE_SHIFT;
  954. unsigned long ei_startpfn, ei_endpfn, ram = 0;
  955. int i;
  956. for (i = 0; i < e820.nr_map; i++) {
  957. if (e820_find_active_region(&e820.map[i],
  958. start_pfn, last_pfn,
  959. &ei_startpfn, &ei_endpfn))
  960. ram += ei_endpfn - ei_startpfn;
  961. }
  962. return end - start - ((u64)ram << PAGE_SHIFT);
  963. }
  964. static void early_panic(char *msg)
  965. {
  966. early_printk(msg);
  967. panic(msg);
  968. }
  969. /* "mem=nopentium" disables the 4MB page tables. */
  970. static int __init parse_memopt(char *p)
  971. {
  972. u64 mem_size;
  973. if (!p)
  974. return -EINVAL;
  975. #ifdef CONFIG_X86_32
  976. if (!strcmp(p, "nopentium")) {
  977. setup_clear_cpu_cap(X86_FEATURE_PSE);
  978. return 0;
  979. }
  980. #endif
  981. mem_size = memparse(p, &p);
  982. end_user_pfn = mem_size>>PAGE_SHIFT;
  983. return 0;
  984. }
  985. early_param("mem", parse_memopt);
  986. static int userdef __initdata;
  987. static int __init parse_memmap_opt(char *p)
  988. {
  989. char *oldp;
  990. u64 start_at, mem_size;
  991. if (!strcmp(p, "exactmap")) {
  992. #ifdef CONFIG_CRASH_DUMP
  993. /*
  994. * If we are doing a crash dump, we still need to know
  995. * the real mem size before original memory map is
  996. * reset.
  997. */
  998. e820_register_active_regions(0, 0, -1UL);
  999. saved_max_pfn = e820_end_of_ram();
  1000. remove_all_active_ranges();
  1001. #endif
  1002. e820.nr_map = 0;
  1003. userdef = 1;
  1004. return 0;
  1005. }
  1006. oldp = p;
  1007. mem_size = memparse(p, &p);
  1008. if (p == oldp)
  1009. return -EINVAL;
  1010. userdef = 1;
  1011. if (*p == '@') {
  1012. start_at = memparse(p+1, &p);
  1013. e820_add_region(start_at, mem_size, E820_RAM);
  1014. } else if (*p == '#') {
  1015. start_at = memparse(p+1, &p);
  1016. e820_add_region(start_at, mem_size, E820_ACPI);
  1017. } else if (*p == '$') {
  1018. start_at = memparse(p+1, &p);
  1019. e820_add_region(start_at, mem_size, E820_RESERVED);
  1020. } else {
  1021. end_user_pfn = (mem_size >> PAGE_SHIFT);
  1022. }
  1023. return *p == '\0' ? 0 : -EINVAL;
  1024. }
  1025. early_param("memmap", parse_memmap_opt);
  1026. void __init finish_e820_parsing(void)
  1027. {
  1028. if (userdef) {
  1029. int nr = e820.nr_map;
  1030. if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr) < 0)
  1031. early_panic("Invalid user supplied memory map");
  1032. e820.nr_map = nr;
  1033. printk(KERN_INFO "user-defined physical RAM map:\n");
  1034. e820_print_map("user");
  1035. }
  1036. }
  1037. /*
  1038. * Mark e820 reserved areas as busy for the resource manager.
  1039. */
  1040. void __init e820_reserve_resources(void)
  1041. {
  1042. int i;
  1043. struct resource *res;
  1044. res = alloc_bootmem_low(sizeof(struct resource) * e820.nr_map);
  1045. for (i = 0; i < e820.nr_map; i++) {
  1046. switch (e820.map[i].type) {
  1047. case E820_RAM: res->name = "System RAM"; break;
  1048. case E820_ACPI: res->name = "ACPI Tables"; break;
  1049. case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
  1050. default: res->name = "reserved";
  1051. }
  1052. res->start = e820.map[i].addr;
  1053. res->end = res->start + e820.map[i].size - 1;
  1054. #ifndef CONFIG_RESOURCES_64BIT
  1055. if (res->end > 0x100000000ULL) {
  1056. res++;
  1057. continue;
  1058. }
  1059. #endif
  1060. res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  1061. insert_resource(&iomem_resource, res);
  1062. res++;
  1063. }
  1064. }
  1065. char *__init default_machine_specific_memory_setup(void)
  1066. {
  1067. char *who = "BIOS-e820";
  1068. int new_nr;
  1069. /*
  1070. * Try to copy the BIOS-supplied E820-map.
  1071. *
  1072. * Otherwise fake a memory map; one section from 0k->640k,
  1073. * the next section from 1mb->appropriate_mem_k
  1074. */
  1075. new_nr = boot_params.e820_entries;
  1076. sanitize_e820_map(boot_params.e820_map,
  1077. ARRAY_SIZE(boot_params.e820_map),
  1078. &new_nr);
  1079. boot_params.e820_entries = new_nr;
  1080. if (copy_e820_map(boot_params.e820_map, boot_params.e820_entries) < 0) {
  1081. u64 mem_size;
  1082. /* compare results from other methods and take the greater */
  1083. if (boot_params.alt_mem_k
  1084. < boot_params.screen_info.ext_mem_k) {
  1085. mem_size = boot_params.screen_info.ext_mem_k;
  1086. who = "BIOS-88";
  1087. } else {
  1088. mem_size = boot_params.alt_mem_k;
  1089. who = "BIOS-e801";
  1090. }
  1091. e820.nr_map = 0;
  1092. e820_add_region(0, LOWMEMSIZE(), E820_RAM);
  1093. e820_add_region(HIGH_MEMORY, mem_size << 10, E820_RAM);
  1094. }
  1095. /* In case someone cares... */
  1096. return who;
  1097. }
  1098. char *__init __attribute__((weak)) machine_specific_memory_setup(void)
  1099. {
  1100. return default_machine_specific_memory_setup();
  1101. }
  1102. /* Overridden in paravirt.c if CONFIG_PARAVIRT */
  1103. char * __init __attribute__((weak)) memory_setup(void)
  1104. {
  1105. return machine_specific_memory_setup();
  1106. }
  1107. void __init setup_memory_map(void)
  1108. {
  1109. printk(KERN_INFO "BIOS-provided physical RAM map:\n");
  1110. e820_print_map(memory_setup());
  1111. }
  1112. #ifdef CONFIG_X86_64
  1113. int __init arch_get_ram_range(int slot, u64 *addr, u64 *size)
  1114. {
  1115. int i;
  1116. if (slot < 0 || slot >= e820.nr_map)
  1117. return -1;
  1118. for (i = slot; i < e820.nr_map; i++) {
  1119. if (e820.map[i].type != E820_RAM)
  1120. continue;
  1121. break;
  1122. }
  1123. if (i == e820.nr_map || e820.map[i].addr > (max_pfn << PAGE_SHIFT))
  1124. return -1;
  1125. *addr = e820.map[i].addr;
  1126. *size = min_t(u64, e820.map[i].size + e820.map[i].addr,
  1127. max_pfn << PAGE_SHIFT) - *addr;
  1128. return i + 1;
  1129. }
  1130. #endif