e820.c 19 KB

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