e820_64.c 21 KB

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