e820.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  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. /* if there's only one memory region, don't bother */
  351. if (*pnr_map < 2) {
  352. return -1;
  353. }
  354. old_nr = *pnr_map;
  355. /* bail out if we find any unreasonable addresses in bios map */
  356. for (i=0; i<old_nr; i++)
  357. if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr) {
  358. return -1;
  359. }
  360. /* create pointers for initial change-point information (for sorting) */
  361. for (i=0; i < 2*old_nr; i++)
  362. change_point[i] = &change_point_list[i];
  363. /* record all known change-points (starting and ending addresses),
  364. omitting those that are for empty memory regions */
  365. chgidx = 0;
  366. for (i=0; i < old_nr; i++) {
  367. if (biosmap[i].size != 0) {
  368. change_point[chgidx]->addr = biosmap[i].addr;
  369. change_point[chgidx++]->pbios = &biosmap[i];
  370. change_point[chgidx]->addr = biosmap[i].addr + biosmap[i].size;
  371. change_point[chgidx++]->pbios = &biosmap[i];
  372. }
  373. }
  374. chg_nr = chgidx; /* true number of change-points */
  375. /* sort change-point list by memory addresses (low -> high) */
  376. still_changing = 1;
  377. while (still_changing) {
  378. still_changing = 0;
  379. for (i=1; i < chg_nr; i++) {
  380. /* if <current_addr> > <last_addr>, swap */
  381. /* or, if current=<start_addr> & last=<end_addr>, swap */
  382. if ((change_point[i]->addr < change_point[i-1]->addr) ||
  383. ((change_point[i]->addr == change_point[i-1]->addr) &&
  384. (change_point[i]->addr == change_point[i]->pbios->addr) &&
  385. (change_point[i-1]->addr != change_point[i-1]->pbios->addr))
  386. )
  387. {
  388. change_tmp = change_point[i];
  389. change_point[i] = change_point[i-1];
  390. change_point[i-1] = change_tmp;
  391. still_changing=1;
  392. }
  393. }
  394. }
  395. /* create a new bios memory map, removing overlaps */
  396. overlap_entries=0; /* number of entries in the overlap table */
  397. new_bios_entry=0; /* index for creating new bios map entries */
  398. last_type = 0; /* start with undefined memory type */
  399. last_addr = 0; /* start with 0 as last starting address */
  400. /* loop through change-points, determining affect on the new bios map */
  401. for (chgidx=0; chgidx < chg_nr; chgidx++)
  402. {
  403. /* keep track of all overlapping bios entries */
  404. if (change_point[chgidx]->addr == change_point[chgidx]->pbios->addr)
  405. {
  406. /* add map entry to overlap list (> 1 entry implies an overlap) */
  407. overlap_list[overlap_entries++]=change_point[chgidx]->pbios;
  408. }
  409. else
  410. {
  411. /* remove entry from list (order independent, so swap with last) */
  412. for (i=0; i<overlap_entries; i++)
  413. {
  414. if (overlap_list[i] == change_point[chgidx]->pbios)
  415. overlap_list[i] = overlap_list[overlap_entries-1];
  416. }
  417. overlap_entries--;
  418. }
  419. /* if there are overlapping entries, decide which "type" to use */
  420. /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */
  421. current_type = 0;
  422. for (i=0; i<overlap_entries; i++)
  423. if (overlap_list[i]->type > current_type)
  424. current_type = overlap_list[i]->type;
  425. /* continue building up new bios map based on this information */
  426. if (current_type != last_type) {
  427. if (last_type != 0) {
  428. new_bios[new_bios_entry].size =
  429. change_point[chgidx]->addr - last_addr;
  430. /* move forward only if the new size was non-zero */
  431. if (new_bios[new_bios_entry].size != 0)
  432. if (++new_bios_entry >= E820MAX)
  433. break; /* no more space left for new bios entries */
  434. }
  435. if (current_type != 0) {
  436. new_bios[new_bios_entry].addr = change_point[chgidx]->addr;
  437. new_bios[new_bios_entry].type = current_type;
  438. last_addr=change_point[chgidx]->addr;
  439. }
  440. last_type = current_type;
  441. }
  442. }
  443. new_nr = new_bios_entry; /* retain count for new bios entries */
  444. /* copy new bios mapping into original location */
  445. memcpy(biosmap, new_bios, new_nr*sizeof(struct e820entry));
  446. *pnr_map = new_nr;
  447. return 0;
  448. }
  449. /*
  450. * Copy the BIOS e820 map into a safe place.
  451. *
  452. * Sanity-check it while we're at it..
  453. *
  454. * If we're lucky and live on a modern system, the setup code
  455. * will have given us a memory map that we can use to properly
  456. * set up memory. If we aren't, we'll fake a memory map.
  457. *
  458. * We check to see that the memory map contains at least 2 elements
  459. * before we'll use it, because the detection code in setup.S may
  460. * not be perfect and most every PC known to man has two memory
  461. * regions: one from 0 to 640k, and one from 1mb up. (The IBM
  462. * thinkpad 560x, for example, does not cooperate with the memory
  463. * detection code.)
  464. */
  465. int __init copy_e820_map(struct e820entry * biosmap, int nr_map)
  466. {
  467. /* Only one memory region (or negative)? Ignore it */
  468. if (nr_map < 2)
  469. return -1;
  470. do {
  471. unsigned long long start = biosmap->addr;
  472. unsigned long long size = biosmap->size;
  473. unsigned long long end = start + size;
  474. unsigned long type = biosmap->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. if (start < 0x100000ULL && end > 0xA0000ULL) {
  484. if (start < 0xA0000ULL)
  485. add_memory_region(start, 0xA0000ULL-start, type);
  486. if (end <= 0x100000ULL)
  487. continue;
  488. start = 0x100000ULL;
  489. size = end - start;
  490. }
  491. }
  492. add_memory_region(start, size, type);
  493. } while (biosmap++,--nr_map);
  494. return 0;
  495. }
  496. /*
  497. * Callback for efi_memory_walk.
  498. */
  499. static int __init
  500. efi_find_max_pfn(unsigned long start, unsigned long end, void *arg)
  501. {
  502. unsigned long *max_pfn = arg, pfn;
  503. if (start < end) {
  504. pfn = PFN_UP(end -1);
  505. if (pfn > *max_pfn)
  506. *max_pfn = pfn;
  507. }
  508. return 0;
  509. }
  510. static int __init
  511. efi_memory_present_wrapper(unsigned long start, unsigned long end, void *arg)
  512. {
  513. memory_present(0, PFN_UP(start), PFN_DOWN(end));
  514. return 0;
  515. }
  516. /*
  517. * Find the highest page frame number we have available
  518. */
  519. void __init find_max_pfn(void)
  520. {
  521. int i;
  522. max_pfn = 0;
  523. if (efi_enabled) {
  524. efi_memmap_walk(efi_find_max_pfn, &max_pfn);
  525. efi_memmap_walk(efi_memory_present_wrapper, NULL);
  526. return;
  527. }
  528. for (i = 0; i < e820.nr_map; i++) {
  529. unsigned long start, end;
  530. /* RAM? */
  531. if (e820.map[i].type != E820_RAM)
  532. continue;
  533. start = PFN_UP(e820.map[i].addr);
  534. end = PFN_DOWN(e820.map[i].addr + e820.map[i].size);
  535. if (start >= end)
  536. continue;
  537. if (end > max_pfn)
  538. max_pfn = end;
  539. memory_present(0, start, end);
  540. }
  541. }
  542. /*
  543. * Free all available memory for boot time allocation. Used
  544. * as a callback function by efi_memory_walk()
  545. */
  546. static int __init
  547. free_available_memory(unsigned long start, unsigned long end, void *arg)
  548. {
  549. /* check max_low_pfn */
  550. if (start >= (max_low_pfn << PAGE_SHIFT))
  551. return 0;
  552. if (end >= (max_low_pfn << PAGE_SHIFT))
  553. end = max_low_pfn << PAGE_SHIFT;
  554. if (start < end)
  555. free_bootmem(start, end - start);
  556. return 0;
  557. }
  558. /*
  559. * Register fully available low RAM pages with the bootmem allocator.
  560. */
  561. void __init register_bootmem_low_pages(unsigned long max_low_pfn)
  562. {
  563. int i;
  564. if (efi_enabled) {
  565. efi_memmap_walk(free_available_memory, NULL);
  566. return;
  567. }
  568. for (i = 0; i < e820.nr_map; i++) {
  569. unsigned long curr_pfn, last_pfn, size;
  570. /*
  571. * Reserve usable low memory
  572. */
  573. if (e820.map[i].type != E820_RAM)
  574. continue;
  575. /*
  576. * We are rounding up the start address of usable memory:
  577. */
  578. curr_pfn = PFN_UP(e820.map[i].addr);
  579. if (curr_pfn >= max_low_pfn)
  580. continue;
  581. /*
  582. * ... and at the end of the usable range downwards:
  583. */
  584. last_pfn = PFN_DOWN(e820.map[i].addr + e820.map[i].size);
  585. if (last_pfn > max_low_pfn)
  586. last_pfn = max_low_pfn;
  587. /*
  588. * .. finally, did all the rounding and playing
  589. * around just make the area go away?
  590. */
  591. if (last_pfn <= curr_pfn)
  592. continue;
  593. size = last_pfn - curr_pfn;
  594. free_bootmem(PFN_PHYS(curr_pfn), PFN_PHYS(size));
  595. }
  596. }
  597. void __init e820_register_memory(void)
  598. {
  599. unsigned long gapstart, gapsize, round;
  600. unsigned long long last;
  601. int i;
  602. /*
  603. * Search for the bigest gap in the low 32 bits of the e820
  604. * memory space.
  605. */
  606. last = 0x100000000ull;
  607. gapstart = 0x10000000;
  608. gapsize = 0x400000;
  609. i = e820.nr_map;
  610. while (--i >= 0) {
  611. unsigned long long start = e820.map[i].addr;
  612. unsigned long long end = start + e820.map[i].size;
  613. /*
  614. * Since "last" is at most 4GB, we know we'll
  615. * fit in 32 bits if this condition is true
  616. */
  617. if (last > end) {
  618. unsigned long gap = last - end;
  619. if (gap > gapsize) {
  620. gapsize = gap;
  621. gapstart = end;
  622. }
  623. }
  624. if (start < last)
  625. last = start;
  626. }
  627. /*
  628. * See how much we want to round up: start off with
  629. * rounding to the next 1MB area.
  630. */
  631. round = 0x100000;
  632. while ((gapsize >> 4) > round)
  633. round += round;
  634. /* Fun with two's complement */
  635. pci_mem_start = (gapstart + round) & -round;
  636. printk("Allocating PCI resources starting at %08lx (gap: %08lx:%08lx)\n",
  637. pci_mem_start, gapstart, gapsize);
  638. }
  639. void __init print_memory_map(char *who)
  640. {
  641. int i;
  642. for (i = 0; i < e820.nr_map; i++) {
  643. printk(" %s: %016Lx - %016Lx ", who,
  644. e820.map[i].addr,
  645. e820.map[i].addr + e820.map[i].size);
  646. switch (e820.map[i].type) {
  647. case E820_RAM: printk("(usable)\n");
  648. break;
  649. case E820_RESERVED:
  650. printk("(reserved)\n");
  651. break;
  652. case E820_ACPI:
  653. printk("(ACPI data)\n");
  654. break;
  655. case E820_NVS:
  656. printk("(ACPI NVS)\n");
  657. break;
  658. default: printk("type %u\n", e820.map[i].type);
  659. break;
  660. }
  661. }
  662. }
  663. static __init __always_inline void efi_limit_regions(unsigned long long size)
  664. {
  665. unsigned long long current_addr = 0;
  666. efi_memory_desc_t *md, *next_md;
  667. void *p, *p1;
  668. int i, j;
  669. j = 0;
  670. p1 = memmap.map;
  671. for (p = p1, i = 0; p < memmap.map_end; p += memmap.desc_size, i++) {
  672. md = p;
  673. next_md = p1;
  674. current_addr = md->phys_addr +
  675. PFN_PHYS(md->num_pages);
  676. if (is_available_memory(md)) {
  677. if (md->phys_addr >= size) continue;
  678. memcpy(next_md, md, memmap.desc_size);
  679. if (current_addr >= size) {
  680. next_md->num_pages -=
  681. PFN_UP(current_addr-size);
  682. }
  683. p1 += memmap.desc_size;
  684. next_md = p1;
  685. j++;
  686. } else if ((md->attribute & EFI_MEMORY_RUNTIME) ==
  687. EFI_MEMORY_RUNTIME) {
  688. /* In order to make runtime services
  689. * available we have to include runtime
  690. * memory regions in memory map */
  691. memcpy(next_md, md, memmap.desc_size);
  692. p1 += memmap.desc_size;
  693. next_md = p1;
  694. j++;
  695. }
  696. }
  697. memmap.nr_map = j;
  698. memmap.map_end = memmap.map +
  699. (memmap.nr_map * memmap.desc_size);
  700. }
  701. void __init limit_regions(unsigned long long size)
  702. {
  703. unsigned long long current_addr;
  704. int i;
  705. print_memory_map("limit_regions start");
  706. if (efi_enabled) {
  707. efi_limit_regions(size);
  708. return;
  709. }
  710. for (i = 0; i < e820.nr_map; i++) {
  711. current_addr = e820.map[i].addr + e820.map[i].size;
  712. if (current_addr < size)
  713. continue;
  714. if (e820.map[i].type != E820_RAM)
  715. continue;
  716. if (e820.map[i].addr >= size) {
  717. /*
  718. * This region starts past the end of the
  719. * requested size, skip it completely.
  720. */
  721. e820.nr_map = i;
  722. } else {
  723. e820.nr_map = i + 1;
  724. e820.map[i].size -= current_addr - size;
  725. }
  726. print_memory_map("limit_regions endfor");
  727. return;
  728. }
  729. print_memory_map("limit_regions endfunc");
  730. }
  731. /*
  732. * This function checks if any part of the range <start,end> is mapped
  733. * with type.
  734. */
  735. int
  736. e820_any_mapped(u64 start, u64 end, unsigned type)
  737. {
  738. int i;
  739. for (i = 0; i < e820.nr_map; i++) {
  740. const struct e820entry *ei = &e820.map[i];
  741. if (type && ei->type != type)
  742. continue;
  743. if (ei->addr >= end || ei->addr + ei->size <= start)
  744. continue;
  745. return 1;
  746. }
  747. return 0;
  748. }
  749. EXPORT_SYMBOL_GPL(e820_any_mapped);
  750. /*
  751. * This function checks if the entire range <start,end> is mapped with type.
  752. *
  753. * Note: this function only works correct if the e820 table is sorted and
  754. * not-overlapping, which is the case
  755. */
  756. int __init
  757. e820_all_mapped(unsigned long s, unsigned long e, unsigned type)
  758. {
  759. u64 start = s;
  760. u64 end = e;
  761. int i;
  762. for (i = 0; i < e820.nr_map; i++) {
  763. struct e820entry *ei = &e820.map[i];
  764. if (type && ei->type != type)
  765. continue;
  766. /* is the region (part) in overlap with the current region ?*/
  767. if (ei->addr >= end || ei->addr + ei->size <= start)
  768. continue;
  769. /* if the region is at the beginning of <start,end> we move
  770. * start to the end of the region since it's ok until there
  771. */
  772. if (ei->addr <= start)
  773. start = ei->addr + ei->size;
  774. /* if start is now at or beyond end, we're done, full
  775. * coverage */
  776. if (start >= end)
  777. return 1; /* we're done */
  778. }
  779. return 0;
  780. }
  781. static int __init parse_memmap(char *arg)
  782. {
  783. if (!arg)
  784. return -EINVAL;
  785. if (strcmp(arg, "exactmap") == 0) {
  786. #ifdef CONFIG_CRASH_DUMP
  787. /* If we are doing a crash dump, we
  788. * still need to know the real mem
  789. * size before original memory map is
  790. * reset.
  791. */
  792. find_max_pfn();
  793. saved_max_pfn = max_pfn;
  794. #endif
  795. e820.nr_map = 0;
  796. user_defined_memmap = 1;
  797. } else {
  798. /* If the user specifies memory size, we
  799. * limit the BIOS-provided memory map to
  800. * that size. exactmap can be used to specify
  801. * the exact map. mem=number can be used to
  802. * trim the existing memory map.
  803. */
  804. unsigned long long start_at, mem_size;
  805. mem_size = memparse(arg, &arg);
  806. if (*arg == '@') {
  807. start_at = memparse(arg+1, &arg);
  808. add_memory_region(start_at, mem_size, E820_RAM);
  809. } else if (*arg == '#') {
  810. start_at = memparse(arg+1, &arg);
  811. add_memory_region(start_at, mem_size, E820_ACPI);
  812. } else if (*arg == '$') {
  813. start_at = memparse(arg+1, &arg);
  814. add_memory_region(start_at, mem_size, E820_RESERVED);
  815. } else {
  816. limit_regions(mem_size);
  817. user_defined_memmap = 1;
  818. }
  819. }
  820. return 0;
  821. }
  822. early_param("memmap", parse_memmap);