e820.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  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. /*
  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. e820_add_region(start, size, type);
  355. } while (biosmap++, --nr_map);
  356. return 0;
  357. }
  358. u64 __init e820_update_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. e820_add_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. void __init early_res_to_bootmem(u64 start, u64 end)
  553. {
  554. int i;
  555. u64 final_start, final_end;
  556. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
  557. struct early_res *r = &early_res[i];
  558. final_start = max(start, r->start);
  559. final_end = min(end, r->end);
  560. if (final_start >= final_end)
  561. continue;
  562. printk(KERN_INFO " early res: %d [%llx-%llx] %s\n", i,
  563. final_start, final_end - 1, r->name);
  564. reserve_bootmem_generic(final_start, final_end - final_start,
  565. BOOTMEM_DEFAULT);
  566. }
  567. }
  568. /* Check for already reserved areas */
  569. static inline int __init bad_addr(u64 *addrp, u64 size, u64 align)
  570. {
  571. int i;
  572. u64 addr = *addrp;
  573. int changed = 0;
  574. struct early_res *r;
  575. again:
  576. i = find_overlapped_early(addr, addr + size);
  577. r = &early_res[i];
  578. if (i < MAX_EARLY_RES && r->end) {
  579. *addrp = addr = round_up(r->end, align);
  580. changed = 1;
  581. goto again;
  582. }
  583. return changed;
  584. }
  585. /* Check for already reserved areas */
  586. static inline int __init bad_addr_size(u64 *addrp, u64 *sizep, u64 align)
  587. {
  588. int i;
  589. u64 addr = *addrp, last;
  590. u64 size = *sizep;
  591. int changed = 0;
  592. again:
  593. last = addr + size;
  594. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
  595. struct early_res *r = &early_res[i];
  596. if (last > r->start && addr < r->start) {
  597. size = r->start - addr;
  598. changed = 1;
  599. goto again;
  600. }
  601. if (last > r->end && addr < r->end) {
  602. addr = round_up(r->end, align);
  603. size = last - addr;
  604. changed = 1;
  605. goto again;
  606. }
  607. if (last <= r->end && addr >= r->start) {
  608. (*sizep)++;
  609. return 0;
  610. }
  611. }
  612. if (changed) {
  613. *addrp = addr;
  614. *sizep = size;
  615. }
  616. return changed;
  617. }
  618. /*
  619. * Find a free area with specified alignment in a specific range.
  620. */
  621. u64 __init find_e820_area(u64 start, u64 end, u64 size, u64 align)
  622. {
  623. int i;
  624. for (i = 0; i < e820.nr_map; i++) {
  625. struct e820entry *ei = &e820.map[i];
  626. u64 addr, last;
  627. u64 ei_last;
  628. if (ei->type != E820_RAM)
  629. continue;
  630. addr = round_up(ei->addr, align);
  631. ei_last = ei->addr + ei->size;
  632. if (addr < start)
  633. addr = round_up(start, align);
  634. if (addr >= ei_last)
  635. continue;
  636. while (bad_addr(&addr, size, align) && addr+size <= ei_last)
  637. ;
  638. last = addr + size;
  639. if (last > ei_last)
  640. continue;
  641. if (last > end)
  642. continue;
  643. return addr;
  644. }
  645. return -1ULL;
  646. }
  647. /*
  648. * Find next free range after *start
  649. */
  650. u64 __init find_e820_area_size(u64 start, u64 *sizep, u64 align)
  651. {
  652. int i;
  653. for (i = 0; i < e820.nr_map; i++) {
  654. struct e820entry *ei = &e820.map[i];
  655. u64 addr, last;
  656. u64 ei_last;
  657. if (ei->type != E820_RAM)
  658. continue;
  659. addr = round_up(ei->addr, align);
  660. ei_last = ei->addr + ei->size;
  661. if (addr < start)
  662. addr = round_up(start, align);
  663. if (addr >= ei_last)
  664. continue;
  665. *sizep = ei_last - addr;
  666. while (bad_addr_size(&addr, sizep, align) &&
  667. addr + *sizep <= ei_last)
  668. ;
  669. last = addr + *sizep;
  670. if (last > ei_last)
  671. continue;
  672. return addr;
  673. }
  674. return -1UL;
  675. }
  676. /*
  677. * pre allocated 4k and reserved it in e820
  678. */
  679. u64 __init early_reserve_e820(u64 startt, u64 sizet, u64 align)
  680. {
  681. u64 size = 0;
  682. u64 addr;
  683. u64 start;
  684. start = startt;
  685. while (size < sizet)
  686. start = find_e820_area_size(start, &size, align);
  687. if (size < sizet)
  688. return 0;
  689. addr = round_down(start + size - sizet, align);
  690. e820_update_range(addr, sizet, E820_RAM, E820_RESERVED);
  691. printk(KERN_INFO "update e820 for early_reserve_e820\n");
  692. update_e820();
  693. return addr;
  694. }
  695. #ifdef CONFIG_X86_32
  696. # ifdef CONFIG_X86_PAE
  697. # define MAX_ARCH_PFN (1ULL<<(36-PAGE_SHIFT))
  698. # else
  699. # define MAX_ARCH_PFN (1ULL<<(32-PAGE_SHIFT))
  700. # endif
  701. #else /* CONFIG_X86_32 */
  702. # define MAX_ARCH_PFN MAXMEM>>PAGE_SHIFT
  703. #endif
  704. /*
  705. * Last pfn which the user wants to use.
  706. */
  707. unsigned long __initdata end_user_pfn = MAX_ARCH_PFN;
  708. /*
  709. * Find the highest page frame number we have available
  710. */
  711. unsigned long __init e820_end_of_ram(void)
  712. {
  713. unsigned long last_pfn;
  714. unsigned long max_arch_pfn = MAX_ARCH_PFN;
  715. last_pfn = find_max_pfn_with_active_regions();
  716. if (last_pfn > max_arch_pfn)
  717. last_pfn = max_arch_pfn;
  718. if (last_pfn > end_user_pfn)
  719. last_pfn = end_user_pfn;
  720. printk(KERN_INFO "last_pfn = %lu max_arch_pfn = %lu\n",
  721. last_pfn, max_arch_pfn);
  722. return last_pfn;
  723. }
  724. /*
  725. * Finds an active region in the address range from start_pfn to last_pfn and
  726. * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
  727. */
  728. int __init e820_find_active_region(const struct e820entry *ei,
  729. unsigned long start_pfn,
  730. unsigned long last_pfn,
  731. unsigned long *ei_startpfn,
  732. unsigned long *ei_endpfn)
  733. {
  734. u64 align = PAGE_SIZE;
  735. *ei_startpfn = round_up(ei->addr, align) >> PAGE_SHIFT;
  736. *ei_endpfn = round_down(ei->addr + ei->size, align) >> PAGE_SHIFT;
  737. /* Skip map entries smaller than a page */
  738. if (*ei_startpfn >= *ei_endpfn)
  739. return 0;
  740. /* Skip if map is outside the node */
  741. if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
  742. *ei_startpfn >= last_pfn)
  743. return 0;
  744. /* Check for overlaps */
  745. if (*ei_startpfn < start_pfn)
  746. *ei_startpfn = start_pfn;
  747. if (*ei_endpfn > last_pfn)
  748. *ei_endpfn = last_pfn;
  749. /* Obey end_user_pfn to save on memmap */
  750. if (*ei_startpfn >= end_user_pfn)
  751. return 0;
  752. if (*ei_endpfn > end_user_pfn)
  753. *ei_endpfn = end_user_pfn;
  754. return 1;
  755. }
  756. /* Walk the e820 map and register active regions within a node */
  757. void __init e820_register_active_regions(int nid, unsigned long start_pfn,
  758. unsigned long last_pfn)
  759. {
  760. unsigned long ei_startpfn;
  761. unsigned long ei_endpfn;
  762. int i;
  763. for (i = 0; i < e820.nr_map; i++)
  764. if (e820_find_active_region(&e820.map[i],
  765. start_pfn, last_pfn,
  766. &ei_startpfn, &ei_endpfn))
  767. add_active_range(nid, ei_startpfn, ei_endpfn);
  768. }
  769. /*
  770. * Find the hole size (in bytes) in the memory range.
  771. * @start: starting address of the memory range to scan
  772. * @end: ending address of the memory range to scan
  773. */
  774. u64 __init e820_hole_size(u64 start, u64 end)
  775. {
  776. unsigned long start_pfn = start >> PAGE_SHIFT;
  777. unsigned long last_pfn = end >> PAGE_SHIFT;
  778. unsigned long ei_startpfn, ei_endpfn, ram = 0;
  779. int i;
  780. for (i = 0; i < e820.nr_map; i++) {
  781. if (e820_find_active_region(&e820.map[i],
  782. start_pfn, last_pfn,
  783. &ei_startpfn, &ei_endpfn))
  784. ram += ei_endpfn - ei_startpfn;
  785. }
  786. return end - start - ((u64)ram << PAGE_SHIFT);
  787. }
  788. static void early_panic(char *msg)
  789. {
  790. early_printk(msg);
  791. panic(msg);
  792. }
  793. /* "mem=nopentium" disables the 4MB page tables. */
  794. static int __init parse_memopt(char *p)
  795. {
  796. u64 mem_size;
  797. if (!p)
  798. return -EINVAL;
  799. #ifdef CONFIG_X86_32
  800. if (!strcmp(p, "nopentium")) {
  801. setup_clear_cpu_cap(X86_FEATURE_PSE);
  802. return 0;
  803. }
  804. #endif
  805. mem_size = memparse(p, &p);
  806. end_user_pfn = mem_size>>PAGE_SHIFT;
  807. return 0;
  808. }
  809. early_param("mem", parse_memopt);
  810. static int userdef __initdata;
  811. static int __init parse_memmap_opt(char *p)
  812. {
  813. char *oldp;
  814. u64 start_at, mem_size;
  815. if (!strcmp(p, "exactmap")) {
  816. #ifdef CONFIG_CRASH_DUMP
  817. /*
  818. * If we are doing a crash dump, we still need to know
  819. * the real mem size before original memory map is
  820. * reset.
  821. */
  822. e820_register_active_regions(0, 0, -1UL);
  823. saved_max_pfn = e820_end_of_ram();
  824. remove_all_active_ranges();
  825. #endif
  826. e820.nr_map = 0;
  827. userdef = 1;
  828. return 0;
  829. }
  830. oldp = p;
  831. mem_size = memparse(p, &p);
  832. if (p == oldp)
  833. return -EINVAL;
  834. userdef = 1;
  835. if (*p == '@') {
  836. start_at = memparse(p+1, &p);
  837. e820_add_region(start_at, mem_size, E820_RAM);
  838. } else if (*p == '#') {
  839. start_at = memparse(p+1, &p);
  840. e820_add_region(start_at, mem_size, E820_ACPI);
  841. } else if (*p == '$') {
  842. start_at = memparse(p+1, &p);
  843. e820_add_region(start_at, mem_size, E820_RESERVED);
  844. } else {
  845. end_user_pfn = (mem_size >> PAGE_SHIFT);
  846. }
  847. return *p == '\0' ? 0 : -EINVAL;
  848. }
  849. early_param("memmap", parse_memmap_opt);
  850. void __init finish_e820_parsing(void)
  851. {
  852. if (userdef) {
  853. int nr = e820.nr_map;
  854. if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr) < 0)
  855. early_panic("Invalid user supplied memory map");
  856. e820.nr_map = nr;
  857. printk(KERN_INFO "user-defined physical RAM map:\n");
  858. e820_print_map("user");
  859. }
  860. }