e820_64.c 19 KB

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