e820.c 19 KB

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