e820_64.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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/suspend.h>
  21. #include <linux/pfn.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/sections.h>
  28. #include <asm/kdebug.h>
  29. struct e820map e820;
  30. /*
  31. * PFN of last memory page.
  32. */
  33. unsigned long end_pfn;
  34. /*
  35. * end_pfn only includes RAM, while end_pfn_map includes all e820 entries.
  36. * The direct mapping extends to end_pfn_map, so that we can directly access
  37. * apertures, ACPI and other tables without having to play with fixmaps.
  38. */
  39. unsigned long end_pfn_map;
  40. /*
  41. * Last pfn which the user wants to use.
  42. */
  43. static unsigned long __initdata end_user_pfn = MAXMEM>>PAGE_SHIFT;
  44. /*
  45. * Early reserved memory areas.
  46. */
  47. #define MAX_EARLY_RES 20
  48. struct early_res {
  49. unsigned long start, end;
  50. char name[16];
  51. };
  52. static struct early_res early_res[MAX_EARLY_RES] __initdata = {
  53. { 0, PAGE_SIZE, "BIOS data page" }, /* BIOS data page */
  54. #ifdef CONFIG_SMP
  55. { SMP_TRAMPOLINE_BASE, SMP_TRAMPOLINE_BASE + 2*PAGE_SIZE, "SMP_TRAMPOLINE" },
  56. #endif
  57. {}
  58. };
  59. void __init reserve_early(unsigned long start, unsigned long end, char *name)
  60. {
  61. int i;
  62. struct early_res *r;
  63. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
  64. r = &early_res[i];
  65. if (end > r->start && start < r->end)
  66. panic("Overlapping early reservations %lx-%lx %s to %lx-%lx %s\n",
  67. start, end - 1, name?name:"", r->start, r->end - 1, r->name);
  68. }
  69. if (i >= MAX_EARLY_RES)
  70. panic("Too many early reservations");
  71. r = &early_res[i];
  72. r->start = start;
  73. r->end = end;
  74. if (name)
  75. strncpy(r->name, name, sizeof(r->name) - 1);
  76. }
  77. void __init early_res_to_bootmem(void)
  78. {
  79. int i;
  80. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
  81. struct early_res *r = &early_res[i];
  82. printk(KERN_INFO "early res: %d [%lx-%lx] %s\n", i,
  83. r->start, r->end - 1, r->name);
  84. reserve_bootmem_generic(r->start, r->end - r->start);
  85. }
  86. }
  87. /* Check for already reserved areas */
  88. static inline int
  89. bad_addr(unsigned long *addrp, unsigned long size, unsigned long align)
  90. {
  91. int i;
  92. unsigned long addr = *addrp, last;
  93. int changed = 0;
  94. again:
  95. last = addr + size;
  96. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
  97. struct early_res *r = &early_res[i];
  98. if (last >= r->start && addr < r->end) {
  99. *addrp = addr = round_up(r->end, align);
  100. changed = 1;
  101. goto again;
  102. }
  103. }
  104. return changed;
  105. }
  106. /*
  107. * This function checks if any part of the range <start,end> is mapped
  108. * with type.
  109. */
  110. int
  111. e820_any_mapped(unsigned long start, unsigned long end, unsigned type)
  112. {
  113. int i;
  114. for (i = 0; i < e820.nr_map; i++) {
  115. struct e820entry *ei = &e820.map[i];
  116. if (type && ei->type != type)
  117. continue;
  118. if (ei->addr >= end || ei->addr + ei->size <= start)
  119. continue;
  120. return 1;
  121. }
  122. return 0;
  123. }
  124. EXPORT_SYMBOL_GPL(e820_any_mapped);
  125. /*
  126. * This function checks if the entire range <start,end> is mapped with type.
  127. *
  128. * Note: this function only works correct if the e820 table is sorted and
  129. * not-overlapping, which is the case
  130. */
  131. int __init e820_all_mapped(unsigned long start, unsigned long end,
  132. unsigned type)
  133. {
  134. int i;
  135. for (i = 0; i < e820.nr_map; i++) {
  136. struct e820entry *ei = &e820.map[i];
  137. if (type && ei->type != type)
  138. continue;
  139. /* is the region (part) in overlap with the current region ?*/
  140. if (ei->addr >= end || ei->addr + ei->size <= start)
  141. continue;
  142. /* if the region is at the beginning of <start,end> we move
  143. * start to the end of the region since it's ok until there
  144. */
  145. if (ei->addr <= start)
  146. start = ei->addr + ei->size;
  147. /*
  148. * if start is now at or beyond end, we're done, full
  149. * coverage
  150. */
  151. if (start >= end)
  152. return 1;
  153. }
  154. return 0;
  155. }
  156. /*
  157. * Find a free area with specified alignment in a specific range.
  158. */
  159. unsigned long __init find_e820_area(unsigned long start, unsigned long end,
  160. unsigned long size, unsigned long align)
  161. {
  162. int i;
  163. for (i = 0; i < e820.nr_map; i++) {
  164. struct e820entry *ei = &e820.map[i];
  165. unsigned long addr, last;
  166. unsigned long ei_last;
  167. if (ei->type != E820_RAM)
  168. continue;
  169. addr = round_up(ei->addr, align);
  170. ei_last = ei->addr + ei->size;
  171. if (addr < start)
  172. addr = round_up(start, align);
  173. if (addr > ei_last)
  174. continue;
  175. while (bad_addr(&addr, size, align) && addr+size <= ei_last)
  176. ;
  177. last = addr + size;
  178. if (last > ei_last)
  179. continue;
  180. if (last > end)
  181. continue;
  182. return addr;
  183. }
  184. return -1UL;
  185. }
  186. /*
  187. * Find the highest page frame number we have available
  188. */
  189. unsigned long __init e820_end_of_ram(void)
  190. {
  191. unsigned long end_pfn;
  192. end_pfn = find_max_pfn_with_active_regions();
  193. if (end_pfn > end_pfn_map)
  194. end_pfn_map = end_pfn;
  195. if (end_pfn_map > MAXMEM>>PAGE_SHIFT)
  196. end_pfn_map = MAXMEM>>PAGE_SHIFT;
  197. if (end_pfn > end_user_pfn)
  198. end_pfn = end_user_pfn;
  199. if (end_pfn > end_pfn_map)
  200. end_pfn = end_pfn_map;
  201. printk(KERN_INFO "end_pfn_map = %lu\n", end_pfn_map);
  202. return end_pfn;
  203. }
  204. /*
  205. * Mark e820 reserved areas as busy for the resource manager.
  206. */
  207. void __init e820_reserve_resources(void)
  208. {
  209. int i;
  210. for (i = 0; i < e820.nr_map; i++) {
  211. struct resource *res;
  212. res = alloc_bootmem_low(sizeof(struct resource));
  213. switch (e820.map[i].type) {
  214. case E820_RAM: res->name = "System RAM"; break;
  215. case E820_ACPI: res->name = "ACPI Tables"; break;
  216. case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
  217. default: res->name = "reserved";
  218. }
  219. res->start = e820.map[i].addr;
  220. res->end = res->start + e820.map[i].size - 1;
  221. res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  222. insert_resource(&iomem_resource, res);
  223. }
  224. }
  225. /*
  226. * Find the ranges of physical addresses that do not correspond to
  227. * e820 RAM areas and mark the corresponding pages as nosave for software
  228. * suspend and suspend to RAM.
  229. *
  230. * This function requires the e820 map to be sorted and without any
  231. * overlapping entries and assumes the first e820 area to be RAM.
  232. */
  233. void __init e820_mark_nosave_regions(void)
  234. {
  235. int i;
  236. unsigned long paddr;
  237. paddr = round_down(e820.map[0].addr + e820.map[0].size, PAGE_SIZE);
  238. for (i = 1; i < e820.nr_map; i++) {
  239. struct e820entry *ei = &e820.map[i];
  240. if (paddr < ei->addr)
  241. register_nosave_region(PFN_DOWN(paddr),
  242. PFN_UP(ei->addr));
  243. paddr = round_down(ei->addr + ei->size, PAGE_SIZE);
  244. if (ei->type != E820_RAM)
  245. register_nosave_region(PFN_UP(ei->addr),
  246. PFN_DOWN(paddr));
  247. if (paddr >= (end_pfn << PAGE_SHIFT))
  248. break;
  249. }
  250. }
  251. /*
  252. * Finds an active region in the address range from start_pfn to end_pfn and
  253. * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
  254. */
  255. static int __init e820_find_active_region(const struct e820entry *ei,
  256. unsigned long start_pfn,
  257. unsigned long end_pfn,
  258. unsigned long *ei_startpfn,
  259. unsigned long *ei_endpfn)
  260. {
  261. *ei_startpfn = round_up(ei->addr, PAGE_SIZE) >> PAGE_SHIFT;
  262. *ei_endpfn = round_down(ei->addr + ei->size, PAGE_SIZE) >> PAGE_SHIFT;
  263. /* Skip map entries smaller than a page */
  264. if (*ei_startpfn >= *ei_endpfn)
  265. return 0;
  266. /* Check if end_pfn_map should be updated */
  267. if (ei->type != E820_RAM && *ei_endpfn > end_pfn_map)
  268. end_pfn_map = *ei_endpfn;
  269. /* Skip if map is outside the node */
  270. if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
  271. *ei_startpfn >= end_pfn)
  272. return 0;
  273. /* Check for overlaps */
  274. if (*ei_startpfn < start_pfn)
  275. *ei_startpfn = start_pfn;
  276. if (*ei_endpfn > end_pfn)
  277. *ei_endpfn = end_pfn;
  278. /* Obey end_user_pfn to save on memmap */
  279. if (*ei_startpfn >= end_user_pfn)
  280. return 0;
  281. if (*ei_endpfn > end_user_pfn)
  282. *ei_endpfn = end_user_pfn;
  283. return 1;
  284. }
  285. /* Walk the e820 map and register active regions within a node */
  286. void __init
  287. e820_register_active_regions(int nid, unsigned long start_pfn,
  288. unsigned long end_pfn)
  289. {
  290. unsigned long ei_startpfn;
  291. unsigned long ei_endpfn;
  292. int i;
  293. for (i = 0; i < e820.nr_map; i++)
  294. if (e820_find_active_region(&e820.map[i],
  295. start_pfn, end_pfn,
  296. &ei_startpfn, &ei_endpfn))
  297. add_active_range(nid, ei_startpfn, ei_endpfn);
  298. }
  299. /*
  300. * Add a memory region to the kernel e820 map.
  301. */
  302. void __init add_memory_region(unsigned long start, unsigned long size, int type)
  303. {
  304. int x = e820.nr_map;
  305. if (x == E820MAX) {
  306. printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
  307. return;
  308. }
  309. e820.map[x].addr = start;
  310. e820.map[x].size = size;
  311. e820.map[x].type = type;
  312. e820.nr_map++;
  313. }
  314. /*
  315. * Find the hole size (in bytes) in the memory range.
  316. * @start: starting address of the memory range to scan
  317. * @end: ending address of the memory range to scan
  318. */
  319. unsigned long __init e820_hole_size(unsigned long start, unsigned long end)
  320. {
  321. unsigned long start_pfn = start >> PAGE_SHIFT;
  322. unsigned long end_pfn = end >> PAGE_SHIFT;
  323. unsigned long ei_startpfn, ei_endpfn, ram = 0;
  324. int i;
  325. for (i = 0; i < e820.nr_map; i++) {
  326. if (e820_find_active_region(&e820.map[i],
  327. start_pfn, end_pfn,
  328. &ei_startpfn, &ei_endpfn))
  329. ram += ei_endpfn - ei_startpfn;
  330. }
  331. return end - start - (ram << PAGE_SHIFT);
  332. }
  333. static void __init e820_print_map(char *who)
  334. {
  335. int i;
  336. for (i = 0; i < e820.nr_map; i++) {
  337. printk(KERN_INFO " %s: %016Lx - %016Lx ", who,
  338. (unsigned long long) e820.map[i].addr,
  339. (unsigned long long)
  340. (e820.map[i].addr + e820.map[i].size));
  341. switch (e820.map[i].type) {
  342. case E820_RAM:
  343. printk(KERN_CONT "(usable)\n");
  344. break;
  345. case E820_RESERVED:
  346. printk(KERN_CONT "(reserved)\n");
  347. break;
  348. case E820_ACPI:
  349. printk(KERN_CONT "(ACPI data)\n");
  350. break;
  351. case E820_NVS:
  352. printk(KERN_CONT "(ACPI NVS)\n");
  353. break;
  354. default:
  355. printk(KERN_CONT "type %u\n", e820.map[i].type);
  356. break;
  357. }
  358. }
  359. }
  360. /*
  361. * Sanitize the BIOS e820 map.
  362. *
  363. * Some e820 responses include overlapping entries. The following
  364. * replaces the original e820 map with a new one, removing overlaps.
  365. *
  366. */
  367. static int __init sanitize_e820_map(struct e820entry *biosmap, char *pnr_map)
  368. {
  369. struct change_member {
  370. struct e820entry *pbios; /* pointer to original bios entry */
  371. unsigned long long addr; /* address for this change point */
  372. };
  373. static struct change_member change_point_list[2*E820MAX] __initdata;
  374. static struct change_member *change_point[2*E820MAX] __initdata;
  375. static struct e820entry *overlap_list[E820MAX] __initdata;
  376. static struct e820entry new_bios[E820MAX] __initdata;
  377. struct change_member *change_tmp;
  378. unsigned long current_type, last_type;
  379. unsigned long long last_addr;
  380. int chgidx, still_changing;
  381. int overlap_entries;
  382. int new_bios_entry;
  383. int old_nr, new_nr, chg_nr;
  384. int i;
  385. /*
  386. Visually we're performing the following
  387. (1,2,3,4 = memory types)...
  388. Sample memory map (w/overlaps):
  389. ____22__________________
  390. ______________________4_
  391. ____1111________________
  392. _44_____________________
  393. 11111111________________
  394. ____________________33__
  395. ___________44___________
  396. __________33333_________
  397. ______________22________
  398. ___________________2222_
  399. _________111111111______
  400. _____________________11_
  401. _________________4______
  402. Sanitized equivalent (no overlap):
  403. 1_______________________
  404. _44_____________________
  405. ___1____________________
  406. ____22__________________
  407. ______11________________
  408. _________1______________
  409. __________3_____________
  410. ___________44___________
  411. _____________33_________
  412. _______________2________
  413. ________________1_______
  414. _________________4______
  415. ___________________2____
  416. ____________________33__
  417. ______________________4_
  418. */
  419. /* if there's only one memory region, don't bother */
  420. if (*pnr_map < 2)
  421. return -1;
  422. old_nr = *pnr_map;
  423. /* bail out if we find any unreasonable addresses in bios map */
  424. for (i = 0; i < old_nr; i++)
  425. if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
  426. return -1;
  427. /* create pointers for initial change-point information (for sorting) */
  428. for (i = 0; i < 2 * old_nr; i++)
  429. change_point[i] = &change_point_list[i];
  430. /* record all known change-points (starting and ending addresses),
  431. omitting those that are for empty memory regions */
  432. chgidx = 0;
  433. for (i = 0; i < old_nr; i++) {
  434. if (biosmap[i].size != 0) {
  435. change_point[chgidx]->addr = biosmap[i].addr;
  436. change_point[chgidx++]->pbios = &biosmap[i];
  437. change_point[chgidx]->addr = biosmap[i].addr +
  438. biosmap[i].size;
  439. change_point[chgidx++]->pbios = &biosmap[i];
  440. }
  441. }
  442. chg_nr = chgidx;
  443. /* sort change-point list by memory addresses (low -> high) */
  444. still_changing = 1;
  445. while (still_changing) {
  446. still_changing = 0;
  447. for (i = 1; i < chg_nr; i++) {
  448. unsigned long long curaddr, lastaddr;
  449. unsigned long long curpbaddr, lastpbaddr;
  450. curaddr = change_point[i]->addr;
  451. lastaddr = change_point[i - 1]->addr;
  452. curpbaddr = change_point[i]->pbios->addr;
  453. lastpbaddr = change_point[i - 1]->pbios->addr;
  454. /*
  455. * swap entries, when:
  456. *
  457. * curaddr > lastaddr or
  458. * curaddr == lastaddr and curaddr == curpbaddr and
  459. * lastaddr != lastpbaddr
  460. */
  461. if (curaddr < lastaddr ||
  462. (curaddr == lastaddr && curaddr == curpbaddr &&
  463. lastaddr != lastpbaddr)) {
  464. change_tmp = change_point[i];
  465. change_point[i] = change_point[i-1];
  466. change_point[i-1] = change_tmp;
  467. still_changing = 1;
  468. }
  469. }
  470. }
  471. /* create a new bios memory map, removing overlaps */
  472. overlap_entries = 0; /* number of entries in the overlap table */
  473. new_bios_entry = 0; /* index for creating new bios map entries */
  474. last_type = 0; /* start with undefined memory type */
  475. last_addr = 0; /* start with 0 as last starting address */
  476. /* loop through change-points, determining affect on the new bios map */
  477. for (chgidx = 0; chgidx < chg_nr; chgidx++) {
  478. /* keep track of all overlapping bios entries */
  479. if (change_point[chgidx]->addr ==
  480. change_point[chgidx]->pbios->addr) {
  481. /*
  482. * add map entry to overlap list (> 1 entry
  483. * implies an overlap)
  484. */
  485. overlap_list[overlap_entries++] =
  486. change_point[chgidx]->pbios;
  487. } else {
  488. /*
  489. * remove entry from list (order independent,
  490. * so swap with last)
  491. */
  492. for (i = 0; i < overlap_entries; i++) {
  493. if (overlap_list[i] ==
  494. change_point[chgidx]->pbios)
  495. overlap_list[i] =
  496. overlap_list[overlap_entries-1];
  497. }
  498. overlap_entries--;
  499. }
  500. /*
  501. * if there are overlapping entries, decide which
  502. * "type" to use (larger value takes precedence --
  503. * 1=usable, 2,3,4,4+=unusable)
  504. */
  505. current_type = 0;
  506. for (i = 0; i < overlap_entries; i++)
  507. if (overlap_list[i]->type > current_type)
  508. current_type = overlap_list[i]->type;
  509. /*
  510. * continue building up new bios map based on this
  511. * information
  512. */
  513. if (current_type != last_type) {
  514. if (last_type != 0) {
  515. new_bios[new_bios_entry].size =
  516. change_point[chgidx]->addr - last_addr;
  517. /*
  518. * move forward only if the new size
  519. * was non-zero
  520. */
  521. if (new_bios[new_bios_entry].size != 0)
  522. /*
  523. * no more space left for new
  524. * bios entries ?
  525. */
  526. if (++new_bios_entry >= E820MAX)
  527. break;
  528. }
  529. if (current_type != 0) {
  530. new_bios[new_bios_entry].addr =
  531. change_point[chgidx]->addr;
  532. new_bios[new_bios_entry].type = current_type;
  533. last_addr = change_point[chgidx]->addr;
  534. }
  535. last_type = current_type;
  536. }
  537. }
  538. /* retain count for new bios entries */
  539. new_nr = new_bios_entry;
  540. /* copy new bios mapping into original location */
  541. memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
  542. *pnr_map = new_nr;
  543. return 0;
  544. }
  545. /*
  546. * Copy the BIOS e820 map into a safe place.
  547. *
  548. * Sanity-check it while we're at it..
  549. *
  550. * If we're lucky and live on a modern system, the setup code
  551. * will have given us a memory map that we can use to properly
  552. * set up memory. If we aren't, we'll fake a memory map.
  553. */
  554. static int __init copy_e820_map(struct e820entry *biosmap, int nr_map)
  555. {
  556. /* Only one memory region (or negative)? Ignore it */
  557. if (nr_map < 2)
  558. return -1;
  559. do {
  560. unsigned long start = biosmap->addr;
  561. unsigned long size = biosmap->size;
  562. unsigned long end = start + size;
  563. unsigned long type = biosmap->type;
  564. /* Overflow in 64 bits? Ignore the memory map. */
  565. if (start > end)
  566. return -1;
  567. add_memory_region(start, size, type);
  568. } while (biosmap++, --nr_map);
  569. return 0;
  570. }
  571. static void early_panic(char *msg)
  572. {
  573. early_printk(msg);
  574. panic(msg);
  575. }
  576. /* We're not void only for x86 32-bit compat */
  577. char * __init machine_specific_memory_setup(void)
  578. {
  579. char *who = "BIOS-e820";
  580. /*
  581. * Try to copy the BIOS-supplied E820-map.
  582. *
  583. * Otherwise fake a memory map; one section from 0k->640k,
  584. * the next section from 1mb->appropriate_mem_k
  585. */
  586. sanitize_e820_map(boot_params.e820_map, &boot_params.e820_entries);
  587. if (copy_e820_map(boot_params.e820_map, boot_params.e820_entries) < 0)
  588. early_panic("Cannot find a valid memory map");
  589. printk(KERN_INFO "BIOS-provided physical RAM map:\n");
  590. e820_print_map(who);
  591. /* In case someone cares... */
  592. return who;
  593. }
  594. static int __init parse_memopt(char *p)
  595. {
  596. if (!p)
  597. return -EINVAL;
  598. end_user_pfn = memparse(p, &p);
  599. end_user_pfn >>= PAGE_SHIFT;
  600. return 0;
  601. }
  602. early_param("mem", parse_memopt);
  603. static int userdef __initdata;
  604. static int __init parse_memmap_opt(char *p)
  605. {
  606. char *oldp;
  607. unsigned long long start_at, mem_size;
  608. if (!strcmp(p, "exactmap")) {
  609. #ifdef CONFIG_CRASH_DUMP
  610. /*
  611. * If we are doing a crash dump, we still need to know
  612. * the real mem size before original memory map is
  613. * reset.
  614. */
  615. e820_register_active_regions(0, 0, -1UL);
  616. saved_max_pfn = e820_end_of_ram();
  617. remove_all_active_ranges();
  618. #endif
  619. end_pfn_map = 0;
  620. e820.nr_map = 0;
  621. userdef = 1;
  622. return 0;
  623. }
  624. oldp = p;
  625. mem_size = memparse(p, &p);
  626. if (p == oldp)
  627. return -EINVAL;
  628. userdef = 1;
  629. if (*p == '@') {
  630. start_at = memparse(p+1, &p);
  631. add_memory_region(start_at, mem_size, E820_RAM);
  632. } else if (*p == '#') {
  633. start_at = memparse(p+1, &p);
  634. add_memory_region(start_at, mem_size, E820_ACPI);
  635. } else if (*p == '$') {
  636. start_at = memparse(p+1, &p);
  637. add_memory_region(start_at, mem_size, E820_RESERVED);
  638. } else {
  639. end_user_pfn = (mem_size >> PAGE_SHIFT);
  640. }
  641. return *p == '\0' ? 0 : -EINVAL;
  642. }
  643. early_param("memmap", parse_memmap_opt);
  644. void __init finish_e820_parsing(void)
  645. {
  646. if (userdef) {
  647. char nr = e820.nr_map;
  648. if (sanitize_e820_map(e820.map, &nr) < 0)
  649. early_panic("Invalid user supplied memory map");
  650. e820.nr_map = nr;
  651. printk(KERN_INFO "user-defined physical RAM map:\n");
  652. e820_print_map("user");
  653. }
  654. }
  655. void __init update_memory_range(u64 start, u64 size, unsigned old_type,
  656. unsigned new_type)
  657. {
  658. int i;
  659. BUG_ON(old_type == new_type);
  660. for (i = 0; i < e820.nr_map; i++) {
  661. struct e820entry *ei = &e820.map[i];
  662. u64 final_start, final_end;
  663. if (ei->type != old_type)
  664. continue;
  665. /* totally covered? */
  666. if (ei->addr >= start && ei->size <= size) {
  667. ei->type = new_type;
  668. continue;
  669. }
  670. /* partially covered */
  671. final_start = max(start, ei->addr);
  672. final_end = min(start + size, ei->addr + ei->size);
  673. if (final_start >= final_end)
  674. continue;
  675. add_memory_region(final_start, final_end - final_start,
  676. new_type);
  677. }
  678. }
  679. void __init update_e820(void)
  680. {
  681. u8 nr_map;
  682. nr_map = e820.nr_map;
  683. if (sanitize_e820_map(e820.map, &nr_map))
  684. return;
  685. e820.nr_map = nr_map;
  686. printk(KERN_INFO "modified physical RAM map:\n");
  687. e820_print_map("modified");
  688. }
  689. unsigned long pci_mem_start = 0xaeedbabe;
  690. EXPORT_SYMBOL(pci_mem_start);
  691. /*
  692. * Search for the biggest gap in the low 32 bits of the e820
  693. * memory space. We pass this space to PCI to assign MMIO resources
  694. * for hotplug or unconfigured devices in.
  695. * Hopefully the BIOS let enough space left.
  696. */
  697. __init void e820_setup_gap(void)
  698. {
  699. unsigned long gapstart, gapsize, round;
  700. unsigned long last;
  701. int i;
  702. int found = 0;
  703. last = 0x100000000ull;
  704. gapstart = 0x10000000;
  705. gapsize = 0x400000;
  706. i = e820.nr_map;
  707. while (--i >= 0) {
  708. unsigned long long start = e820.map[i].addr;
  709. unsigned long long end = start + e820.map[i].size;
  710. /*
  711. * Since "last" is at most 4GB, we know we'll
  712. * fit in 32 bits if this condition is true
  713. */
  714. if (last > end) {
  715. unsigned long gap = last - end;
  716. if (gap > gapsize) {
  717. gapsize = gap;
  718. gapstart = end;
  719. found = 1;
  720. }
  721. }
  722. if (start < last)
  723. last = start;
  724. }
  725. if (!found) {
  726. gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
  727. printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit "
  728. "address range\n"
  729. KERN_ERR "PCI: Unassigned devices with 32bit resource "
  730. "registers may break!\n");
  731. }
  732. /*
  733. * See how much we want to round up: start off with
  734. * rounding to the next 1MB area.
  735. */
  736. round = 0x100000;
  737. while ((gapsize >> 4) > round)
  738. round += round;
  739. /* Fun with two's complement */
  740. pci_mem_start = (gapstart + round) & -round;
  741. printk(KERN_INFO
  742. "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
  743. pci_mem_start, gapstart, gapsize);
  744. }
  745. int __init arch_get_ram_range(int slot, u64 *addr, u64 *size)
  746. {
  747. int i;
  748. if (slot < 0 || slot >= e820.nr_map)
  749. return -1;
  750. for (i = slot; i < e820.nr_map; i++) {
  751. if (e820.map[i].type != E820_RAM)
  752. continue;
  753. break;
  754. }
  755. if (i == e820.nr_map || e820.map[i].addr > (max_pfn << PAGE_SHIFT))
  756. return -1;
  757. *addr = e820.map[i].addr;
  758. *size = min_t(u64, e820.map[i].size + e820.map[i].addr,
  759. max_pfn << PAGE_SHIFT) - *addr;
  760. return i + 1;
  761. }