e820_32.c 22 KB

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