e820_32.c 24 KB

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