e820_32.c 24 KB

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