e820.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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. void __init reserve_early(u64 start, u64 end, char *name)
  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. panic("Overlapping early reservations %llx-%llx %s to %llx-%llx %s\n",
  516. start, end - 1, name?name:"", r->start,
  517. r->end - 1, r->name);
  518. }
  519. if (i >= MAX_EARLY_RES)
  520. panic("Too many early reservations");
  521. r = &early_res[i];
  522. r->start = start;
  523. r->end = end;
  524. if (name)
  525. strncpy(r->name, name, sizeof(r->name) - 1);
  526. }
  527. void __init free_early(u64 start, u64 end)
  528. {
  529. struct early_res *r;
  530. int i, j;
  531. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
  532. r = &early_res[i];
  533. if (start == r->start && end == r->end)
  534. break;
  535. }
  536. if (i >= MAX_EARLY_RES || !early_res[i].end)
  537. panic("free_early on not reserved area: %llx-%llx!",
  538. start, end);
  539. for (j = i + 1; j < MAX_EARLY_RES && early_res[j].end; j++)
  540. ;
  541. memmove(&early_res[i], &early_res[i + 1],
  542. (j - 1 - i) * sizeof(struct early_res));
  543. early_res[j - 1].end = 0;
  544. }
  545. void __init early_res_to_bootmem(u64 start, u64 end)
  546. {
  547. int i;
  548. u64 final_start, final_end;
  549. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
  550. struct early_res *r = &early_res[i];
  551. final_start = max(start, r->start);
  552. final_end = min(end, r->end);
  553. if (final_start >= final_end)
  554. continue;
  555. printk(KERN_INFO " early res: %d [%llx-%llx] %s\n", i,
  556. final_start, final_end - 1, r->name);
  557. #ifdef CONFIG_X86_64
  558. reserve_bootmem_generic(final_start, final_end - final_start);
  559. #else
  560. reserve_bootmem(final_start, final_end - final_start,
  561. BOOTMEM_DEFAULT);
  562. #endif
  563. }
  564. }
  565. /* Check for already reserved areas */
  566. static inline int __init bad_addr(u64 *addrp, u64 size, u64 align)
  567. {
  568. int i;
  569. u64 addr = *addrp, last;
  570. int changed = 0;
  571. again:
  572. last = addr + size;
  573. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
  574. struct early_res *r = &early_res[i];
  575. if (last >= r->start && addr < r->end) {
  576. *addrp = addr = round_up(r->end, align);
  577. changed = 1;
  578. goto again;
  579. }
  580. }
  581. return changed;
  582. }
  583. /* Check for already reserved areas */
  584. static inline int __init bad_addr_size(u64 *addrp, u64 *sizep, u64 align)
  585. {
  586. int i;
  587. u64 addr = *addrp, last;
  588. u64 size = *sizep;
  589. int changed = 0;
  590. again:
  591. last = addr + size;
  592. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
  593. struct early_res *r = &early_res[i];
  594. if (last > r->start && addr < r->start) {
  595. size = r->start - addr;
  596. changed = 1;
  597. goto again;
  598. }
  599. if (last > r->end && addr < r->end) {
  600. addr = round_up(r->end, align);
  601. size = last - addr;
  602. changed = 1;
  603. goto again;
  604. }
  605. if (last <= r->end && addr >= r->start) {
  606. (*sizep)++;
  607. return 0;
  608. }
  609. }
  610. if (changed) {
  611. *addrp = addr;
  612. *sizep = size;
  613. }
  614. return changed;
  615. }
  616. /*
  617. * Find a free area with specified alignment in a specific range.
  618. */
  619. u64 __init find_e820_area(u64 start, u64 end, u64 size, u64 align)
  620. {
  621. int i;
  622. for (i = 0; i < e820.nr_map; i++) {
  623. struct e820entry *ei = &e820.map[i];
  624. u64 addr, last;
  625. u64 ei_last;
  626. if (ei->type != E820_RAM)
  627. continue;
  628. addr = round_up(ei->addr, align);
  629. ei_last = ei->addr + ei->size;
  630. if (addr < start)
  631. addr = round_up(start, align);
  632. if (addr >= ei_last)
  633. continue;
  634. while (bad_addr(&addr, size, align) && addr+size <= ei_last)
  635. ;
  636. last = addr + size;
  637. if (last > ei_last)
  638. continue;
  639. if (last > end)
  640. continue;
  641. return addr;
  642. }
  643. return -1ULL;
  644. }
  645. /*
  646. * Find next free range after *start
  647. */
  648. u64 __init find_e820_area_size(u64 start, u64 *sizep, u64 align)
  649. {
  650. int i;
  651. for (i = 0; i < e820.nr_map; i++) {
  652. struct e820entry *ei = &e820.map[i];
  653. u64 addr, last;
  654. u64 ei_last;
  655. if (ei->type != E820_RAM)
  656. continue;
  657. addr = round_up(ei->addr, align);
  658. ei_last = ei->addr + ei->size;
  659. if (addr < start)
  660. addr = round_up(start, align);
  661. if (addr >= ei_last)
  662. continue;
  663. *sizep = ei_last - addr;
  664. while (bad_addr_size(&addr, sizep, align) &&
  665. addr + *sizep <= ei_last)
  666. ;
  667. last = addr + *sizep;
  668. if (last > ei_last)
  669. continue;
  670. return addr;
  671. }
  672. return -1UL;
  673. }
  674. /*
  675. * pre allocated 4k and reserved it in e820
  676. */
  677. u64 __init early_reserve_e820(u64 startt, u64 sizet, u64 align)
  678. {
  679. u64 size = 0;
  680. u64 addr;
  681. u64 start;
  682. start = startt;
  683. while (size < sizet)
  684. start = find_e820_area_size(start, &size, align);
  685. if (size < sizet)
  686. return 0;
  687. addr = round_down(start + size - sizet, align);
  688. update_memory_range(addr, sizet, E820_RAM, E820_RESERVED);
  689. printk(KERN_INFO "update e820 for early_reserve_e820\n");
  690. update_e820();
  691. return addr;
  692. }
  693. #ifdef CONFIG_X86_32
  694. # ifdef CONFIG_X86_PAE
  695. # define MAX_ARCH_PFN (1ULL<<(36-PAGE_SHIFT))
  696. # else
  697. # define MAX_ARCH_PFN (1ULL<<(32-PAGE_SHIFT))
  698. # endif
  699. #else /* CONFIG_X86_32 */
  700. # define MAX_ARCH_PFN MAXMEM<<PAGE_SHIFT
  701. #endif
  702. /*
  703. * Last pfn which the user wants to use.
  704. */
  705. unsigned long __initdata end_user_pfn = MAX_ARCH_PFN;
  706. /*
  707. * Find the highest page frame number we have available
  708. */
  709. unsigned long __init e820_end_of_ram(void)
  710. {
  711. unsigned long last_pfn;
  712. unsigned long max_arch_pfn = MAX_ARCH_PFN;
  713. last_pfn = find_max_pfn_with_active_regions();
  714. if (last_pfn > max_arch_pfn)
  715. last_pfn = max_arch_pfn;
  716. if (last_pfn > end_user_pfn)
  717. last_pfn = end_user_pfn;
  718. printk(KERN_INFO "last_pfn = %lu max_arch_pfn = %lu\n",
  719. last_pfn, max_arch_pfn);
  720. return last_pfn;
  721. }
  722. /*
  723. * Finds an active region in the address range from start_pfn to last_pfn and
  724. * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
  725. */
  726. int __init e820_find_active_region(const struct e820entry *ei,
  727. unsigned long start_pfn,
  728. unsigned long last_pfn,
  729. unsigned long *ei_startpfn,
  730. unsigned long *ei_endpfn)
  731. {
  732. u64 align = PAGE_SIZE;
  733. *ei_startpfn = round_up(ei->addr, align) >> PAGE_SHIFT;
  734. *ei_endpfn = round_down(ei->addr + ei->size, align) >> PAGE_SHIFT;
  735. /* Skip map entries smaller than a page */
  736. if (*ei_startpfn >= *ei_endpfn)
  737. return 0;
  738. /* Skip if map is outside the node */
  739. if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
  740. *ei_startpfn >= last_pfn)
  741. return 0;
  742. /* Check for overlaps */
  743. if (*ei_startpfn < start_pfn)
  744. *ei_startpfn = start_pfn;
  745. if (*ei_endpfn > last_pfn)
  746. *ei_endpfn = last_pfn;
  747. /* Obey end_user_pfn to save on memmap */
  748. if (*ei_startpfn >= end_user_pfn)
  749. return 0;
  750. if (*ei_endpfn > end_user_pfn)
  751. *ei_endpfn = end_user_pfn;
  752. return 1;
  753. }
  754. /* Walk the e820 map and register active regions within a node */
  755. void __init e820_register_active_regions(int nid, unsigned long start_pfn,
  756. unsigned long last_pfn)
  757. {
  758. unsigned long ei_startpfn;
  759. unsigned long ei_endpfn;
  760. int i;
  761. for (i = 0; i < e820.nr_map; i++)
  762. if (e820_find_active_region(&e820.map[i],
  763. start_pfn, last_pfn,
  764. &ei_startpfn, &ei_endpfn))
  765. add_active_range(nid, ei_startpfn, ei_endpfn);
  766. }
  767. /*
  768. * Find the hole size (in bytes) in the memory range.
  769. * @start: starting address of the memory range to scan
  770. * @end: ending address of the memory range to scan
  771. */
  772. u64 __init e820_hole_size(u64 start, u64 end)
  773. {
  774. unsigned long start_pfn = start >> PAGE_SHIFT;
  775. unsigned long last_pfn = end >> PAGE_SHIFT;
  776. unsigned long ei_startpfn, ei_endpfn, ram = 0;
  777. int i;
  778. for (i = 0; i < e820.nr_map; i++) {
  779. if (e820_find_active_region(&e820.map[i],
  780. start_pfn, last_pfn,
  781. &ei_startpfn, &ei_endpfn))
  782. ram += ei_endpfn - ei_startpfn;
  783. }
  784. return end - start - ((u64)ram << PAGE_SHIFT);
  785. }