e820.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  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 add_memory_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. /*
  333. * Copy the BIOS e820 map into a safe place.
  334. *
  335. * Sanity-check it while we're at it..
  336. *
  337. * If we're lucky and live on a modern system, the setup code
  338. * will have given us a memory map that we can use to properly
  339. * set up memory. If we aren't, we'll fake a memory map.
  340. */
  341. int __init copy_e820_map(struct e820entry *biosmap, int nr_map)
  342. {
  343. /* Only one memory region (or negative)? Ignore it */
  344. if (nr_map < 2)
  345. return -1;
  346. do {
  347. u64 start = biosmap->addr;
  348. u64 size = biosmap->size;
  349. u64 end = start + size;
  350. u32 type = biosmap->type;
  351. /* Overflow in 64 bits? Ignore the memory map. */
  352. if (start > end)
  353. return -1;
  354. add_memory_region(start, size, type);
  355. } while (biosmap++, --nr_map);
  356. return 0;
  357. }
  358. u64 __init update_memory_range(u64 start, u64 size, unsigned old_type,
  359. unsigned new_type)
  360. {
  361. int i;
  362. u64 real_updated_size = 0;
  363. BUG_ON(old_type == new_type);
  364. for (i = 0; i < e820.nr_map; i++) {
  365. struct e820entry *ei = &e820.map[i];
  366. u64 final_start, final_end;
  367. if (ei->type != old_type)
  368. continue;
  369. /* totally covered? */
  370. if (ei->addr >= start &&
  371. (ei->addr + ei->size) <= (start + size)) {
  372. ei->type = new_type;
  373. real_updated_size += ei->size;
  374. continue;
  375. }
  376. /* partially covered */
  377. final_start = max(start, ei->addr);
  378. final_end = min(start + size, ei->addr + ei->size);
  379. if (final_start >= final_end)
  380. continue;
  381. add_memory_region(final_start, final_end - final_start,
  382. new_type);
  383. real_updated_size += final_end - final_start;
  384. }
  385. return real_updated_size;
  386. }
  387. void __init update_e820(void)
  388. {
  389. int nr_map;
  390. nr_map = e820.nr_map;
  391. if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr_map))
  392. return;
  393. e820.nr_map = nr_map;
  394. printk(KERN_INFO "modified physical RAM map:\n");
  395. e820_print_map("modified");
  396. }
  397. /*
  398. * Search for the biggest gap in the low 32 bits of the e820
  399. * memory space. We pass this space to PCI to assign MMIO resources
  400. * for hotplug or unconfigured devices in.
  401. * Hopefully the BIOS let enough space left.
  402. */
  403. __init void e820_setup_gap(void)
  404. {
  405. unsigned long gapstart, gapsize, round;
  406. unsigned long long last;
  407. int i;
  408. int found = 0;
  409. last = 0x100000000ull;
  410. gapstart = 0x10000000;
  411. gapsize = 0x400000;
  412. i = e820.nr_map;
  413. while (--i >= 0) {
  414. unsigned long long start = e820.map[i].addr;
  415. unsigned long long end = start + e820.map[i].size;
  416. /*
  417. * Since "last" is at most 4GB, we know we'll
  418. * fit in 32 bits if this condition is true
  419. */
  420. if (last > end) {
  421. unsigned long gap = last - end;
  422. if (gap > gapsize) {
  423. gapsize = gap;
  424. gapstart = end;
  425. found = 1;
  426. }
  427. }
  428. if (start < last)
  429. last = start;
  430. }
  431. #ifdef CONFIG_X86_64
  432. if (!found) {
  433. gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
  434. printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit "
  435. "address range\n"
  436. KERN_ERR "PCI: Unassigned devices with 32bit resource "
  437. "registers may break!\n");
  438. }
  439. #endif
  440. /*
  441. * See how much we want to round up: start off with
  442. * rounding to the next 1MB area.
  443. */
  444. round = 0x100000;
  445. while ((gapsize >> 4) > round)
  446. round += round;
  447. /* Fun with two's complement */
  448. pci_mem_start = (gapstart + round) & -round;
  449. printk(KERN_INFO
  450. "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
  451. pci_mem_start, gapstart, gapsize);
  452. }
  453. #if defined(CONFIG_X86_64) || \
  454. (defined(CONFIG_X86_32) && defined(CONFIG_HIBERNATION))
  455. /**
  456. * Find the ranges of physical addresses that do not correspond to
  457. * e820 RAM areas and mark the corresponding pages as nosave for
  458. * hibernation (32 bit) or software suspend and suspend to RAM (64 bit).
  459. *
  460. * This function requires the e820 map to be sorted and without any
  461. * overlapping entries and assumes the first e820 area to be RAM.
  462. */
  463. void __init e820_mark_nosave_regions(unsigned long limit_pfn)
  464. {
  465. int i;
  466. unsigned long pfn;
  467. pfn = PFN_DOWN(e820.map[0].addr + e820.map[0].size);
  468. for (i = 1; i < e820.nr_map; i++) {
  469. struct e820entry *ei = &e820.map[i];
  470. if (pfn < PFN_UP(ei->addr))
  471. register_nosave_region(pfn, PFN_UP(ei->addr));
  472. pfn = PFN_DOWN(ei->addr + ei->size);
  473. if (ei->type != E820_RAM)
  474. register_nosave_region(PFN_UP(ei->addr), pfn);
  475. if (pfn >= limit_pfn)
  476. break;
  477. }
  478. }
  479. #endif
  480. /*
  481. * Early reserved memory areas.
  482. */
  483. #define MAX_EARLY_RES 20
  484. struct early_res {
  485. u64 start, end;
  486. char name[16];
  487. };
  488. static struct early_res early_res[MAX_EARLY_RES] __initdata = {
  489. { 0, PAGE_SIZE, "BIOS data page" }, /* BIOS data page */
  490. #if defined(CONFIG_X86_64) && defined(CONFIG_X86_TRAMPOLINE)
  491. { TRAMPOLINE_BASE, TRAMPOLINE_BASE + 2 * PAGE_SIZE, "TRAMPOLINE" },
  492. #endif
  493. #if defined(CONFIG_X86_32) && defined(CONFIG_SMP)
  494. /*
  495. * But first pinch a few for the stack/trampoline stuff
  496. * FIXME: Don't need the extra page at 4K, but need to fix
  497. * trampoline before removing it. (see the GDT stuff)
  498. */
  499. { PAGE_SIZE, PAGE_SIZE + PAGE_SIZE, "EX TRAMPOLINE" },
  500. /*
  501. * Has to be in very low memory so we can execute
  502. * real-mode AP code.
  503. */
  504. { TRAMPOLINE_BASE, TRAMPOLINE_BASE + PAGE_SIZE, "TRAMPOLINE" },
  505. #endif
  506. {}
  507. };
  508. static int __init find_overlapped_early(u64 start, u64 end)
  509. {
  510. int i;
  511. struct early_res *r;
  512. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
  513. r = &early_res[i];
  514. if (end > r->start && start < r->end)
  515. break;
  516. }
  517. return i;
  518. }
  519. void __init reserve_early(u64 start, u64 end, char *name)
  520. {
  521. int i;
  522. struct early_res *r;
  523. i = find_overlapped_early(start, end);
  524. if (i >= MAX_EARLY_RES)
  525. panic("Too many early reservations");
  526. r = &early_res[i];
  527. if (r->end)
  528. panic("Overlapping early reservations "
  529. "%llx-%llx %s to %llx-%llx %s\n",
  530. start, end - 1, name?name:"", r->start,
  531. r->end - 1, r->name);
  532. r->start = start;
  533. r->end = end;
  534. if (name)
  535. strncpy(r->name, name, sizeof(r->name) - 1);
  536. }
  537. void __init free_early(u64 start, u64 end)
  538. {
  539. struct early_res *r;
  540. int i, j;
  541. i = find_overlapped_early(start, end);
  542. r = &early_res[i];
  543. if (i >= MAX_EARLY_RES || r->end != end || r->start != start)
  544. panic("free_early on not reserved area: %llx-%llx!",
  545. start, end - 1);
  546. for (j = i + 1; j < MAX_EARLY_RES && early_res[j].end; j++)
  547. ;
  548. memmove(&early_res[i], &early_res[i + 1],
  549. (j - 1 - i) * sizeof(struct early_res));
  550. early_res[j - 1].end = 0;
  551. }
  552. int __init page_is_reserved_early(unsigned long pagenr)
  553. {
  554. u64 start = (u64)pagenr << PAGE_SHIFT;
  555. int i;
  556. struct early_res *r;
  557. i = find_overlapped_early(start, start + PAGE_SIZE);
  558. r = &early_res[i];
  559. return (i < MAX_EARLY_RES && r->end);
  560. }
  561. void __init early_res_to_bootmem(u64 start, u64 end)
  562. {
  563. int i;
  564. u64 final_start, final_end;
  565. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
  566. struct early_res *r = &early_res[i];
  567. final_start = max(start, r->start);
  568. final_end = min(end, r->end);
  569. if (final_start >= final_end)
  570. continue;
  571. printk(KERN_INFO " early res: %d [%llx-%llx] %s\n", i,
  572. final_start, final_end - 1, r->name);
  573. reserve_bootmem_generic(final_start, final_end - final_start,
  574. BOOTMEM_DEFAULT);
  575. }
  576. }
  577. /* Check for already reserved areas */
  578. static inline int __init bad_addr(u64 *addrp, u64 size, u64 align)
  579. {
  580. int i;
  581. u64 addr = *addrp;
  582. int changed = 0;
  583. struct early_res *r;
  584. again:
  585. i = find_overlapped_early(addr, addr + size);
  586. r = &early_res[i];
  587. if (i < MAX_EARLY_RES && r->end) {
  588. *addrp = addr = round_up(r->end, align);
  589. changed = 1;
  590. goto again;
  591. }
  592. return changed;
  593. }
  594. /* Check for already reserved areas */
  595. static inline int __init bad_addr_size(u64 *addrp, u64 *sizep, u64 align)
  596. {
  597. int i;
  598. u64 addr = *addrp, last;
  599. u64 size = *sizep;
  600. int changed = 0;
  601. again:
  602. last = addr + size;
  603. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
  604. struct early_res *r = &early_res[i];
  605. if (last > r->start && addr < r->start) {
  606. size = r->start - addr;
  607. changed = 1;
  608. goto again;
  609. }
  610. if (last > r->end && addr < r->end) {
  611. addr = round_up(r->end, align);
  612. size = last - addr;
  613. changed = 1;
  614. goto again;
  615. }
  616. if (last <= r->end && addr >= r->start) {
  617. (*sizep)++;
  618. return 0;
  619. }
  620. }
  621. if (changed) {
  622. *addrp = addr;
  623. *sizep = size;
  624. }
  625. return changed;
  626. }
  627. /*
  628. * Find a free area with specified alignment in a specific range.
  629. */
  630. u64 __init find_e820_area(u64 start, u64 end, u64 size, u64 align)
  631. {
  632. int i;
  633. for (i = 0; i < e820.nr_map; i++) {
  634. struct e820entry *ei = &e820.map[i];
  635. u64 addr, last;
  636. u64 ei_last;
  637. if (ei->type != E820_RAM)
  638. continue;
  639. addr = round_up(ei->addr, align);
  640. ei_last = ei->addr + ei->size;
  641. if (addr < start)
  642. addr = round_up(start, align);
  643. if (addr >= ei_last)
  644. continue;
  645. while (bad_addr(&addr, size, align) && addr+size <= ei_last)
  646. ;
  647. last = addr + size;
  648. if (last > ei_last)
  649. continue;
  650. if (last > end)
  651. continue;
  652. return addr;
  653. }
  654. return -1ULL;
  655. }
  656. /*
  657. * Find next free range after *start
  658. */
  659. u64 __init find_e820_area_size(u64 start, u64 *sizep, u64 align)
  660. {
  661. int i;
  662. for (i = 0; i < e820.nr_map; i++) {
  663. struct e820entry *ei = &e820.map[i];
  664. u64 addr, last;
  665. u64 ei_last;
  666. if (ei->type != E820_RAM)
  667. continue;
  668. addr = round_up(ei->addr, align);
  669. ei_last = ei->addr + ei->size;
  670. if (addr < start)
  671. addr = round_up(start, align);
  672. if (addr >= ei_last)
  673. continue;
  674. *sizep = ei_last - addr;
  675. while (bad_addr_size(&addr, sizep, align) &&
  676. addr + *sizep <= ei_last)
  677. ;
  678. last = addr + *sizep;
  679. if (last > ei_last)
  680. continue;
  681. return addr;
  682. }
  683. return -1UL;
  684. }
  685. /*
  686. * pre allocated 4k and reserved it in e820
  687. */
  688. u64 __init early_reserve_e820(u64 startt, u64 sizet, u64 align)
  689. {
  690. u64 size = 0;
  691. u64 addr;
  692. u64 start;
  693. start = startt;
  694. while (size < sizet)
  695. start = find_e820_area_size(start, &size, align);
  696. if (size < sizet)
  697. return 0;
  698. addr = round_down(start + size - sizet, align);
  699. update_memory_range(addr, sizet, E820_RAM, E820_RESERVED);
  700. printk(KERN_INFO "update e820 for early_reserve_e820\n");
  701. update_e820();
  702. return addr;
  703. }
  704. #ifdef CONFIG_X86_32
  705. # ifdef CONFIG_X86_PAE
  706. # define MAX_ARCH_PFN (1ULL<<(36-PAGE_SHIFT))
  707. # else
  708. # define MAX_ARCH_PFN (1ULL<<(32-PAGE_SHIFT))
  709. # endif
  710. #else /* CONFIG_X86_32 */
  711. # define MAX_ARCH_PFN MAXMEM>>PAGE_SHIFT
  712. #endif
  713. /*
  714. * Last pfn which the user wants to use.
  715. */
  716. unsigned long __initdata end_user_pfn = MAX_ARCH_PFN;
  717. /*
  718. * Find the highest page frame number we have available
  719. */
  720. unsigned long __init e820_end_of_ram(void)
  721. {
  722. unsigned long last_pfn;
  723. unsigned long max_arch_pfn = MAX_ARCH_PFN;
  724. last_pfn = find_max_pfn_with_active_regions();
  725. if (last_pfn > max_arch_pfn)
  726. last_pfn = max_arch_pfn;
  727. if (last_pfn > end_user_pfn)
  728. last_pfn = end_user_pfn;
  729. printk(KERN_INFO "last_pfn = %lu max_arch_pfn = %lu\n",
  730. last_pfn, max_arch_pfn);
  731. return last_pfn;
  732. }
  733. /*
  734. * Finds an active region in the address range from start_pfn to last_pfn and
  735. * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
  736. */
  737. int __init e820_find_active_region(const struct e820entry *ei,
  738. unsigned long start_pfn,
  739. unsigned long last_pfn,
  740. unsigned long *ei_startpfn,
  741. unsigned long *ei_endpfn)
  742. {
  743. u64 align = PAGE_SIZE;
  744. *ei_startpfn = round_up(ei->addr, align) >> PAGE_SHIFT;
  745. *ei_endpfn = round_down(ei->addr + ei->size, align) >> PAGE_SHIFT;
  746. /* Skip map entries smaller than a page */
  747. if (*ei_startpfn >= *ei_endpfn)
  748. return 0;
  749. /* Skip if map is outside the node */
  750. if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
  751. *ei_startpfn >= last_pfn)
  752. return 0;
  753. /* Check for overlaps */
  754. if (*ei_startpfn < start_pfn)
  755. *ei_startpfn = start_pfn;
  756. if (*ei_endpfn > last_pfn)
  757. *ei_endpfn = last_pfn;
  758. /* Obey end_user_pfn to save on memmap */
  759. if (*ei_startpfn >= end_user_pfn)
  760. return 0;
  761. if (*ei_endpfn > end_user_pfn)
  762. *ei_endpfn = end_user_pfn;
  763. return 1;
  764. }
  765. /* Walk the e820 map and register active regions within a node */
  766. void __init e820_register_active_regions(int nid, unsigned long start_pfn,
  767. unsigned long last_pfn)
  768. {
  769. unsigned long ei_startpfn;
  770. unsigned long ei_endpfn;
  771. int i;
  772. for (i = 0; i < e820.nr_map; i++)
  773. if (e820_find_active_region(&e820.map[i],
  774. start_pfn, last_pfn,
  775. &ei_startpfn, &ei_endpfn))
  776. add_active_range(nid, ei_startpfn, ei_endpfn);
  777. }
  778. /*
  779. * Find the hole size (in bytes) in the memory range.
  780. * @start: starting address of the memory range to scan
  781. * @end: ending address of the memory range to scan
  782. */
  783. u64 __init e820_hole_size(u64 start, u64 end)
  784. {
  785. unsigned long start_pfn = start >> PAGE_SHIFT;
  786. unsigned long last_pfn = end >> PAGE_SHIFT;
  787. unsigned long ei_startpfn, ei_endpfn, ram = 0;
  788. int i;
  789. for (i = 0; i < e820.nr_map; i++) {
  790. if (e820_find_active_region(&e820.map[i],
  791. start_pfn, last_pfn,
  792. &ei_startpfn, &ei_endpfn))
  793. ram += ei_endpfn - ei_startpfn;
  794. }
  795. return end - start - ((u64)ram << PAGE_SHIFT);
  796. }
  797. static void early_panic(char *msg)
  798. {
  799. early_printk(msg);
  800. panic(msg);
  801. }
  802. /* "mem=nopentium" disables the 4MB page tables. */
  803. static int __init parse_memopt(char *p)
  804. {
  805. u64 mem_size;
  806. if (!p)
  807. return -EINVAL;
  808. #ifdef CONFIG_X86_32
  809. if (!strcmp(p, "nopentium")) {
  810. setup_clear_cpu_cap(X86_FEATURE_PSE);
  811. return 0;
  812. }
  813. #endif
  814. mem_size = memparse(p, &p);
  815. end_user_pfn = mem_size>>PAGE_SHIFT;
  816. return 0;
  817. }
  818. early_param("mem", parse_memopt);
  819. static int userdef __initdata;
  820. static int __init parse_memmap_opt(char *p)
  821. {
  822. char *oldp;
  823. u64 start_at, mem_size;
  824. if (!strcmp(p, "exactmap")) {
  825. #ifdef CONFIG_CRASH_DUMP
  826. /*
  827. * If we are doing a crash dump, we still need to know
  828. * the real mem size before original memory map is
  829. * reset.
  830. */
  831. e820_register_active_regions(0, 0, -1UL);
  832. saved_max_pfn = e820_end_of_ram();
  833. remove_all_active_ranges();
  834. #endif
  835. e820.nr_map = 0;
  836. userdef = 1;
  837. return 0;
  838. }
  839. oldp = p;
  840. mem_size = memparse(p, &p);
  841. if (p == oldp)
  842. return -EINVAL;
  843. userdef = 1;
  844. if (*p == '@') {
  845. start_at = memparse(p+1, &p);
  846. add_memory_region(start_at, mem_size, E820_RAM);
  847. } else if (*p == '#') {
  848. start_at = memparse(p+1, &p);
  849. add_memory_region(start_at, mem_size, E820_ACPI);
  850. } else if (*p == '$') {
  851. start_at = memparse(p+1, &p);
  852. add_memory_region(start_at, mem_size, E820_RESERVED);
  853. } else {
  854. end_user_pfn = (mem_size >> PAGE_SHIFT);
  855. }
  856. return *p == '\0' ? 0 : -EINVAL;
  857. }
  858. early_param("memmap", parse_memmap_opt);
  859. void __init finish_e820_parsing(void)
  860. {
  861. if (userdef) {
  862. int nr = e820.nr_map;
  863. if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr) < 0)
  864. early_panic("Invalid user supplied memory map");
  865. e820.nr_map = nr;
  866. printk(KERN_INFO "user-defined physical RAM map:\n");
  867. e820_print_map("user");
  868. }
  869. }