e820.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. #include <linux/kernel.h>
  2. #include <linux/types.h>
  3. #include <linux/init.h>
  4. #include <linux/bootmem.h>
  5. #include <linux/ioport.h>
  6. #include <linux/string.h>
  7. #include <linux/kexec.h>
  8. #include <linux/module.h>
  9. #include <linux/mm.h>
  10. #include <linux/efi.h>
  11. #include <linux/pfn.h>
  12. #include <asm/pgtable.h>
  13. #include <asm/page.h>
  14. #include <asm/e820.h>
  15. #ifdef CONFIG_EFI
  16. int efi_enabled = 0;
  17. EXPORT_SYMBOL(efi_enabled);
  18. #endif
  19. struct e820map e820;
  20. struct change_member {
  21. struct e820entry *pbios; /* pointer to original bios entry */
  22. unsigned long long addr; /* address for this change point */
  23. };
  24. static struct change_member change_point_list[2*E820MAX] __initdata;
  25. static struct change_member *change_point[2*E820MAX] __initdata;
  26. static struct e820entry *overlap_list[E820MAX] __initdata;
  27. static struct e820entry new_bios[E820MAX] __initdata;
  28. /* For PCI or other memory-mapped resources */
  29. unsigned long pci_mem_start = 0x10000000;
  30. #ifdef CONFIG_PCI
  31. EXPORT_SYMBOL(pci_mem_start);
  32. #endif
  33. struct resource data_resource = {
  34. .name = "Kernel data",
  35. .start = 0,
  36. .end = 0,
  37. .flags = IORESOURCE_BUSY | IORESOURCE_MEM
  38. };
  39. struct resource code_resource = {
  40. .name = "Kernel code",
  41. .start = 0,
  42. .end = 0,
  43. .flags = IORESOURCE_BUSY | IORESOURCE_MEM
  44. };
  45. static struct resource system_rom_resource = {
  46. .name = "System ROM",
  47. .start = 0xf0000,
  48. .end = 0xfffff,
  49. .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
  50. };
  51. static struct resource extension_rom_resource = {
  52. .name = "Extension ROM",
  53. .start = 0xe0000,
  54. .end = 0xeffff,
  55. .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
  56. };
  57. static struct resource adapter_rom_resources[] = { {
  58. .name = "Adapter ROM",
  59. .start = 0xc8000,
  60. .end = 0,
  61. .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
  62. }, {
  63. .name = "Adapter ROM",
  64. .start = 0,
  65. .end = 0,
  66. .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
  67. }, {
  68. .name = "Adapter ROM",
  69. .start = 0,
  70. .end = 0,
  71. .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
  72. }, {
  73. .name = "Adapter ROM",
  74. .start = 0,
  75. .end = 0,
  76. .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
  77. }, {
  78. .name = "Adapter ROM",
  79. .start = 0,
  80. .end = 0,
  81. .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
  82. }, {
  83. .name = "Adapter ROM",
  84. .start = 0,
  85. .end = 0,
  86. .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
  87. } };
  88. static struct resource video_rom_resource = {
  89. .name = "Video ROM",
  90. .start = 0xc0000,
  91. .end = 0xc7fff,
  92. .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
  93. };
  94. static struct resource video_ram_resource = {
  95. .name = "Video RAM area",
  96. .start = 0xa0000,
  97. .end = 0xbffff,
  98. .flags = IORESOURCE_BUSY | IORESOURCE_MEM
  99. };
  100. static struct resource standard_io_resources[] = { {
  101. .name = "dma1",
  102. .start = 0x0000,
  103. .end = 0x001f,
  104. .flags = IORESOURCE_BUSY | IORESOURCE_IO
  105. }, {
  106. .name = "pic1",
  107. .start = 0x0020,
  108. .end = 0x0021,
  109. .flags = IORESOURCE_BUSY | IORESOURCE_IO
  110. }, {
  111. .name = "timer0",
  112. .start = 0x0040,
  113. .end = 0x0043,
  114. .flags = IORESOURCE_BUSY | IORESOURCE_IO
  115. }, {
  116. .name = "timer1",
  117. .start = 0x0050,
  118. .end = 0x0053,
  119. .flags = IORESOURCE_BUSY | IORESOURCE_IO
  120. }, {
  121. .name = "keyboard",
  122. .start = 0x0060,
  123. .end = 0x006f,
  124. .flags = IORESOURCE_BUSY | IORESOURCE_IO
  125. }, {
  126. .name = "dma page reg",
  127. .start = 0x0080,
  128. .end = 0x008f,
  129. .flags = IORESOURCE_BUSY | IORESOURCE_IO
  130. }, {
  131. .name = "pic2",
  132. .start = 0x00a0,
  133. .end = 0x00a1,
  134. .flags = IORESOURCE_BUSY | IORESOURCE_IO
  135. }, {
  136. .name = "dma2",
  137. .start = 0x00c0,
  138. .end = 0x00df,
  139. .flags = IORESOURCE_BUSY | IORESOURCE_IO
  140. }, {
  141. .name = "fpu",
  142. .start = 0x00f0,
  143. .end = 0x00ff,
  144. .flags = IORESOURCE_BUSY | IORESOURCE_IO
  145. } };
  146. #define romsignature(x) (*(unsigned short *)(x) == 0xaa55)
  147. static int __init romchecksum(unsigned char *rom, unsigned long length)
  148. {
  149. unsigned char *p, sum = 0;
  150. for (p = rom; p < rom + length; p++)
  151. sum += *p;
  152. return sum == 0;
  153. }
  154. static void __init probe_roms(void)
  155. {
  156. unsigned long start, length, upper;
  157. unsigned char *rom;
  158. int i;
  159. /* video rom */
  160. upper = adapter_rom_resources[0].start;
  161. for (start = video_rom_resource.start; start < upper; start += 2048) {
  162. rom = isa_bus_to_virt(start);
  163. if (!romsignature(rom))
  164. continue;
  165. video_rom_resource.start = start;
  166. /* 0 < length <= 0x7f * 512, historically */
  167. length = rom[2] * 512;
  168. /* if checksum okay, trust length byte */
  169. if (length && romchecksum(rom, length))
  170. video_rom_resource.end = start + length - 1;
  171. request_resource(&iomem_resource, &video_rom_resource);
  172. break;
  173. }
  174. start = (video_rom_resource.end + 1 + 2047) & ~2047UL;
  175. if (start < upper)
  176. start = upper;
  177. /* system rom */
  178. request_resource(&iomem_resource, &system_rom_resource);
  179. upper = system_rom_resource.start;
  180. /* check for extension rom (ignore length byte!) */
  181. rom = isa_bus_to_virt(extension_rom_resource.start);
  182. if (romsignature(rom)) {
  183. length = extension_rom_resource.end - extension_rom_resource.start + 1;
  184. if (romchecksum(rom, length)) {
  185. request_resource(&iomem_resource, &extension_rom_resource);
  186. upper = extension_rom_resource.start;
  187. }
  188. }
  189. /* check for adapter roms on 2k boundaries */
  190. for (i = 0; i < ARRAY_SIZE(adapter_rom_resources) && start < upper; start += 2048) {
  191. rom = isa_bus_to_virt(start);
  192. if (!romsignature(rom))
  193. continue;
  194. /* 0 < length <= 0x7f * 512, historically */
  195. length = rom[2] * 512;
  196. /* but accept any length that fits if checksum okay */
  197. if (!length || start + length > upper || !romchecksum(rom, length))
  198. continue;
  199. adapter_rom_resources[i].start = start;
  200. adapter_rom_resources[i].end = start + length - 1;
  201. request_resource(&iomem_resource, &adapter_rom_resources[i]);
  202. start = adapter_rom_resources[i++].end & ~2047UL;
  203. }
  204. }
  205. /*
  206. * Request address space for all standard RAM and ROM resources
  207. * and also for regions reported as reserved by the e820.
  208. */
  209. static void __init
  210. legacy_init_iomem_resources(struct resource *code_resource, struct resource *data_resource)
  211. {
  212. int i;
  213. probe_roms();
  214. for (i = 0; i < e820.nr_map; i++) {
  215. struct resource *res;
  216. #ifndef CONFIG_RESOURCES_64BIT
  217. if (e820.map[i].addr + e820.map[i].size > 0x100000000ULL)
  218. continue;
  219. #endif
  220. res = kzalloc(sizeof(struct resource), GFP_ATOMIC);
  221. switch (e820.map[i].type) {
  222. case E820_RAM: res->name = "System RAM"; break;
  223. case E820_ACPI: res->name = "ACPI Tables"; break;
  224. case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
  225. default: res->name = "reserved";
  226. }
  227. res->start = e820.map[i].addr;
  228. res->end = res->start + e820.map[i].size - 1;
  229. res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  230. if (request_resource(&iomem_resource, res)) {
  231. kfree(res);
  232. continue;
  233. }
  234. if (e820.map[i].type == E820_RAM) {
  235. /*
  236. * We don't know which RAM region contains kernel data,
  237. * so we try it repeatedly and let the resource manager
  238. * test it.
  239. */
  240. request_resource(res, code_resource);
  241. request_resource(res, data_resource);
  242. #ifdef CONFIG_KEXEC
  243. request_resource(res, &crashk_res);
  244. #endif
  245. }
  246. }
  247. }
  248. /*
  249. * Request address space for all standard resources
  250. *
  251. * This is called just before pcibios_init(), which is also a
  252. * subsys_initcall, but is linked in later (in arch/i386/pci/common.c).
  253. */
  254. static int __init request_standard_resources(void)
  255. {
  256. int i;
  257. printk("Setting up standard PCI resources\n");
  258. if (efi_enabled)
  259. efi_initialize_iomem_resources(&code_resource, &data_resource);
  260. else
  261. legacy_init_iomem_resources(&code_resource, &data_resource);
  262. /* EFI systems may still have VGA */
  263. request_resource(&iomem_resource, &video_ram_resource);
  264. /* request I/O space for devices used on all i[345]86 PCs */
  265. for (i = 0; i < ARRAY_SIZE(standard_io_resources); i++)
  266. request_resource(&ioport_resource, &standard_io_resources[i]);
  267. return 0;
  268. }
  269. subsys_initcall(request_standard_resources);
  270. void __init add_memory_region(unsigned long long start,
  271. unsigned long long size, int type)
  272. {
  273. int x;
  274. if (!efi_enabled) {
  275. x = e820.nr_map;
  276. if (x == E820MAX) {
  277. printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
  278. return;
  279. }
  280. e820.map[x].addr = start;
  281. e820.map[x].size = size;
  282. e820.map[x].type = type;
  283. e820.nr_map++;
  284. }
  285. } /* add_memory_region */
  286. /*
  287. * Sanitize the BIOS e820 map.
  288. *
  289. * Some e820 responses include overlapping entries. The following
  290. * replaces the original e820 map with a new one, removing overlaps.
  291. *
  292. */
  293. int __init sanitize_e820_map(struct e820entry * biosmap, char * pnr_map)
  294. {
  295. struct change_member *change_tmp;
  296. unsigned long current_type, last_type;
  297. unsigned long long last_addr;
  298. int chgidx, still_changing;
  299. int overlap_entries;
  300. int new_bios_entry;
  301. int old_nr, new_nr, chg_nr;
  302. int i;
  303. /*
  304. Visually we're performing the following (1,2,3,4 = memory types)...
  305. Sample memory map (w/overlaps):
  306. ____22__________________
  307. ______________________4_
  308. ____1111________________
  309. _44_____________________
  310. 11111111________________
  311. ____________________33__
  312. ___________44___________
  313. __________33333_________
  314. ______________22________
  315. ___________________2222_
  316. _________111111111______
  317. _____________________11_
  318. _________________4______
  319. Sanitized equivalent (no overlap):
  320. 1_______________________
  321. _44_____________________
  322. ___1____________________
  323. ____22__________________
  324. ______11________________
  325. _________1______________
  326. __________3_____________
  327. ___________44___________
  328. _____________33_________
  329. _______________2________
  330. ________________1_______
  331. _________________4______
  332. ___________________2____
  333. ____________________33__
  334. ______________________4_
  335. */
  336. printk("sanitize start\n");
  337. /* if there's only one memory region, don't bother */
  338. if (*pnr_map < 2) {
  339. printk("sanitize bail 0\n");
  340. return -1;
  341. }
  342. old_nr = *pnr_map;
  343. /* bail out if we find any unreasonable addresses in bios map */
  344. for (i=0; i<old_nr; i++)
  345. if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr) {
  346. printk("sanitize bail 1\n");
  347. return -1;
  348. }
  349. /* create pointers for initial change-point information (for sorting) */
  350. for (i=0; i < 2*old_nr; i++)
  351. change_point[i] = &change_point_list[i];
  352. /* record all known change-points (starting and ending addresses),
  353. omitting those that are for empty memory regions */
  354. chgidx = 0;
  355. for (i=0; i < old_nr; i++) {
  356. if (biosmap[i].size != 0) {
  357. change_point[chgidx]->addr = biosmap[i].addr;
  358. change_point[chgidx++]->pbios = &biosmap[i];
  359. change_point[chgidx]->addr = biosmap[i].addr + biosmap[i].size;
  360. change_point[chgidx++]->pbios = &biosmap[i];
  361. }
  362. }
  363. chg_nr = chgidx; /* true number of change-points */
  364. /* sort change-point list by memory addresses (low -> high) */
  365. still_changing = 1;
  366. while (still_changing) {
  367. still_changing = 0;
  368. for (i=1; i < chg_nr; i++) {
  369. /* if <current_addr> > <last_addr>, swap */
  370. /* or, if current=<start_addr> & last=<end_addr>, swap */
  371. if ((change_point[i]->addr < change_point[i-1]->addr) ||
  372. ((change_point[i]->addr == change_point[i-1]->addr) &&
  373. (change_point[i]->addr == change_point[i]->pbios->addr) &&
  374. (change_point[i-1]->addr != change_point[i-1]->pbios->addr))
  375. )
  376. {
  377. change_tmp = change_point[i];
  378. change_point[i] = change_point[i-1];
  379. change_point[i-1] = change_tmp;
  380. still_changing=1;
  381. }
  382. }
  383. }
  384. /* create a new bios memory map, removing overlaps */
  385. overlap_entries=0; /* number of entries in the overlap table */
  386. new_bios_entry=0; /* index for creating new bios map entries */
  387. last_type = 0; /* start with undefined memory type */
  388. last_addr = 0; /* start with 0 as last starting address */
  389. /* loop through change-points, determining affect on the new bios map */
  390. for (chgidx=0; chgidx < chg_nr; chgidx++)
  391. {
  392. /* keep track of all overlapping bios entries */
  393. if (change_point[chgidx]->addr == change_point[chgidx]->pbios->addr)
  394. {
  395. /* add map entry to overlap list (> 1 entry implies an overlap) */
  396. overlap_list[overlap_entries++]=change_point[chgidx]->pbios;
  397. }
  398. else
  399. {
  400. /* remove entry from list (order independent, so swap with last) */
  401. for (i=0; i<overlap_entries; i++)
  402. {
  403. if (overlap_list[i] == change_point[chgidx]->pbios)
  404. overlap_list[i] = overlap_list[overlap_entries-1];
  405. }
  406. overlap_entries--;
  407. }
  408. /* if there are overlapping entries, decide which "type" to use */
  409. /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */
  410. current_type = 0;
  411. for (i=0; i<overlap_entries; i++)
  412. if (overlap_list[i]->type > current_type)
  413. current_type = overlap_list[i]->type;
  414. /* continue building up new bios map based on this information */
  415. if (current_type != last_type) {
  416. if (last_type != 0) {
  417. new_bios[new_bios_entry].size =
  418. change_point[chgidx]->addr - last_addr;
  419. /* move forward only if the new size was non-zero */
  420. if (new_bios[new_bios_entry].size != 0)
  421. if (++new_bios_entry >= E820MAX)
  422. break; /* no more space left for new bios entries */
  423. }
  424. if (current_type != 0) {
  425. new_bios[new_bios_entry].addr = change_point[chgidx]->addr;
  426. new_bios[new_bios_entry].type = current_type;
  427. last_addr=change_point[chgidx]->addr;
  428. }
  429. last_type = current_type;
  430. }
  431. }
  432. new_nr = new_bios_entry; /* retain count for new bios entries */
  433. /* copy new bios mapping into original location */
  434. memcpy(biosmap, new_bios, new_nr*sizeof(struct e820entry));
  435. *pnr_map = new_nr;
  436. printk("sanitize end\n");
  437. return 0;
  438. }
  439. /*
  440. * Copy the BIOS e820 map into a safe place.
  441. *
  442. * Sanity-check it while we're at it..
  443. *
  444. * If we're lucky and live on a modern system, the setup code
  445. * will have given us a memory map that we can use to properly
  446. * set up memory. If we aren't, we'll fake a memory map.
  447. *
  448. * We check to see that the memory map contains at least 2 elements
  449. * before we'll use it, because the detection code in setup.S may
  450. * not be perfect and most every PC known to man has two memory
  451. * regions: one from 0 to 640k, and one from 1mb up. (The IBM
  452. * thinkpad 560x, for example, does not cooperate with the memory
  453. * detection code.)
  454. */
  455. int __init copy_e820_map(struct e820entry * biosmap, int nr_map)
  456. {
  457. /* Only one memory region (or negative)? Ignore it */
  458. if (nr_map < 2)
  459. return -1;
  460. do {
  461. unsigned long long start = biosmap->addr;
  462. unsigned long long size = biosmap->size;
  463. unsigned long long end = start + size;
  464. unsigned long type = biosmap->type;
  465. printk("copy_e820_map() start: %016Lx size: %016Lx end: %016Lx type: %ld\n", start, size, end, type);
  466. /* Overflow in 64 bits? Ignore the memory map. */
  467. if (start > end)
  468. return -1;
  469. /*
  470. * Some BIOSes claim RAM in the 640k - 1M region.
  471. * Not right. Fix it up.
  472. */
  473. if (type == E820_RAM) {
  474. printk("copy_e820_map() type is E820_RAM\n");
  475. if (start < 0x100000ULL && end > 0xA0000ULL) {
  476. printk("copy_e820_map() lies in range...\n");
  477. if (start < 0xA0000ULL) {
  478. printk("copy_e820_map() start < 0xA0000ULL\n");
  479. add_memory_region(start, 0xA0000ULL-start, type);
  480. }
  481. if (end <= 0x100000ULL) {
  482. printk("copy_e820_map() end <= 0x100000ULL\n");
  483. continue;
  484. }
  485. start = 0x100000ULL;
  486. size = end - start;
  487. }
  488. }
  489. add_memory_region(start, size, type);
  490. } while (biosmap++,--nr_map);
  491. return 0;
  492. }
  493. /*
  494. * Callback for efi_memory_walk.
  495. */
  496. static int __init
  497. efi_find_max_pfn(unsigned long start, unsigned long end, void *arg)
  498. {
  499. unsigned long *max_pfn = arg, pfn;
  500. if (start < end) {
  501. pfn = PFN_UP(end -1);
  502. if (pfn > *max_pfn)
  503. *max_pfn = pfn;
  504. }
  505. return 0;
  506. }
  507. static int __init
  508. efi_memory_present_wrapper(unsigned long start, unsigned long end, void *arg)
  509. {
  510. memory_present(0, PFN_UP(start), PFN_DOWN(end));
  511. return 0;
  512. }
  513. /*
  514. * Find the highest page frame number we have available
  515. */
  516. void __init find_max_pfn(void)
  517. {
  518. int i;
  519. max_pfn = 0;
  520. if (efi_enabled) {
  521. efi_memmap_walk(efi_find_max_pfn, &max_pfn);
  522. efi_memmap_walk(efi_memory_present_wrapper, NULL);
  523. return;
  524. }
  525. for (i = 0; i < e820.nr_map; i++) {
  526. unsigned long start, end;
  527. /* RAM? */
  528. if (e820.map[i].type != E820_RAM)
  529. continue;
  530. start = PFN_UP(e820.map[i].addr);
  531. end = PFN_DOWN(e820.map[i].addr + e820.map[i].size);
  532. if (start >= end)
  533. continue;
  534. if (end > max_pfn)
  535. max_pfn = end;
  536. memory_present(0, start, end);
  537. }
  538. }
  539. /*
  540. * Free all available memory for boot time allocation. Used
  541. * as a callback function by efi_memory_walk()
  542. */
  543. static int __init
  544. free_available_memory(unsigned long start, unsigned long end, void *arg)
  545. {
  546. /* check max_low_pfn */
  547. if (start >= (max_low_pfn << PAGE_SHIFT))
  548. return 0;
  549. if (end >= (max_low_pfn << PAGE_SHIFT))
  550. end = max_low_pfn << PAGE_SHIFT;
  551. if (start < end)
  552. free_bootmem(start, end - start);
  553. return 0;
  554. }
  555. /*
  556. * Register fully available low RAM pages with the bootmem allocator.
  557. */
  558. void __init register_bootmem_low_pages(unsigned long max_low_pfn)
  559. {
  560. int i;
  561. if (efi_enabled) {
  562. efi_memmap_walk(free_available_memory, NULL);
  563. return;
  564. }
  565. for (i = 0; i < e820.nr_map; i++) {
  566. unsigned long curr_pfn, last_pfn, size;
  567. /*
  568. * Reserve usable low memory
  569. */
  570. if (e820.map[i].type != E820_RAM)
  571. continue;
  572. /*
  573. * We are rounding up the start address of usable memory:
  574. */
  575. curr_pfn = PFN_UP(e820.map[i].addr);
  576. if (curr_pfn >= max_low_pfn)
  577. continue;
  578. /*
  579. * ... and at the end of the usable range downwards:
  580. */
  581. last_pfn = PFN_DOWN(e820.map[i].addr + e820.map[i].size);
  582. if (last_pfn > max_low_pfn)
  583. last_pfn = max_low_pfn;
  584. /*
  585. * .. finally, did all the rounding and playing
  586. * around just make the area go away?
  587. */
  588. if (last_pfn <= curr_pfn)
  589. continue;
  590. size = last_pfn - curr_pfn;
  591. free_bootmem(PFN_PHYS(curr_pfn), PFN_PHYS(size));
  592. }
  593. }
  594. void __init register_memory(void)
  595. {
  596. unsigned long gapstart, gapsize, round;
  597. unsigned long long last;
  598. int i;
  599. /*
  600. * Search for the bigest gap in the low 32 bits of the e820
  601. * memory space.
  602. */
  603. last = 0x100000000ull;
  604. gapstart = 0x10000000;
  605. gapsize = 0x400000;
  606. i = e820.nr_map;
  607. while (--i >= 0) {
  608. unsigned long long start = e820.map[i].addr;
  609. unsigned long long end = start + e820.map[i].size;
  610. /*
  611. * Since "last" is at most 4GB, we know we'll
  612. * fit in 32 bits if this condition is true
  613. */
  614. if (last > end) {
  615. unsigned long gap = last - end;
  616. if (gap > gapsize) {
  617. gapsize = gap;
  618. gapstart = end;
  619. }
  620. }
  621. if (start < last)
  622. last = start;
  623. }
  624. /*
  625. * See how much we want to round up: start off with
  626. * rounding to the next 1MB area.
  627. */
  628. round = 0x100000;
  629. while ((gapsize >> 4) > round)
  630. round += round;
  631. /* Fun with two's complement */
  632. pci_mem_start = (gapstart + round) & -round;
  633. printk("Allocating PCI resources starting at %08lx (gap: %08lx:%08lx)\n",
  634. pci_mem_start, gapstart, gapsize);
  635. }