e820_32.c 22 KB

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