e820.c 23 KB

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