e820_64.c 21 KB

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