e820.c 19 KB

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