e820.c 23 KB

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