e820.c 33 KB

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