e820.c 18 KB

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