e820_32.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  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. if (crashk_res.start != crashk_res.end)
  259. request_resource(res, &crashk_res);
  260. #endif
  261. }
  262. }
  263. }
  264. /*
  265. * Request address space for all standard resources
  266. *
  267. * This is called just before pcibios_init(), which is also a
  268. * subsys_initcall, but is linked in later (in arch/i386/pci/common.c).
  269. */
  270. static int __init request_standard_resources(void)
  271. {
  272. int i;
  273. printk("Setting up standard PCI resources\n");
  274. if (efi_enabled)
  275. efi_initialize_iomem_resources(&code_resource, &data_resource);
  276. else
  277. legacy_init_iomem_resources(&code_resource, &data_resource);
  278. /* EFI systems may still have VGA */
  279. request_resource(&iomem_resource, &video_ram_resource);
  280. /* request I/O space for devices used on all i[345]86 PCs */
  281. for (i = 0; i < ARRAY_SIZE(standard_io_resources); i++)
  282. request_resource(&ioport_resource, &standard_io_resources[i]);
  283. return 0;
  284. }
  285. subsys_initcall(request_standard_resources);
  286. #if defined(CONFIG_PM) && defined(CONFIG_HIBERNATION)
  287. /**
  288. * e820_mark_nosave_regions - Find the ranges of physical addresses that do not
  289. * correspond to e820 RAM areas and mark the corresponding pages as nosave for
  290. * hibernation.
  291. *
  292. * This function requires the e820 map to be sorted and without any
  293. * overlapping entries and assumes the first e820 area to be RAM.
  294. */
  295. void __init e820_mark_nosave_regions(void)
  296. {
  297. int i;
  298. unsigned long pfn;
  299. pfn = PFN_DOWN(e820.map[0].addr + e820.map[0].size);
  300. for (i = 1; i < e820.nr_map; i++) {
  301. struct e820entry *ei = &e820.map[i];
  302. if (pfn < PFN_UP(ei->addr))
  303. register_nosave_region(pfn, PFN_UP(ei->addr));
  304. pfn = PFN_DOWN(ei->addr + ei->size);
  305. if (ei->type != E820_RAM)
  306. register_nosave_region(PFN_UP(ei->addr), pfn);
  307. if (pfn >= max_low_pfn)
  308. break;
  309. }
  310. }
  311. #endif
  312. void __init add_memory_region(unsigned long long start,
  313. unsigned long long size, int type)
  314. {
  315. int x;
  316. if (!efi_enabled) {
  317. x = e820.nr_map;
  318. if (x == E820MAX) {
  319. printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
  320. return;
  321. }
  322. e820.map[x].addr = start;
  323. e820.map[x].size = size;
  324. e820.map[x].type = type;
  325. e820.nr_map++;
  326. }
  327. } /* add_memory_region */
  328. /*
  329. * Sanitize the BIOS e820 map.
  330. *
  331. * Some e820 responses include overlapping entries. The following
  332. * replaces the original e820 map with a new one, removing overlaps.
  333. *
  334. */
  335. int __init sanitize_e820_map(struct e820entry * biosmap, char * pnr_map)
  336. {
  337. struct change_member *change_tmp;
  338. unsigned long current_type, last_type;
  339. unsigned long long last_addr;
  340. int chgidx, still_changing;
  341. int overlap_entries;
  342. int new_bios_entry;
  343. int old_nr, new_nr, chg_nr;
  344. int i;
  345. /*
  346. Visually we're performing the following (1,2,3,4 = memory types)...
  347. Sample memory map (w/overlaps):
  348. ____22__________________
  349. ______________________4_
  350. ____1111________________
  351. _44_____________________
  352. 11111111________________
  353. ____________________33__
  354. ___________44___________
  355. __________33333_________
  356. ______________22________
  357. ___________________2222_
  358. _________111111111______
  359. _____________________11_
  360. _________________4______
  361. Sanitized equivalent (no overlap):
  362. 1_______________________
  363. _44_____________________
  364. ___1____________________
  365. ____22__________________
  366. ______11________________
  367. _________1______________
  368. __________3_____________
  369. ___________44___________
  370. _____________33_________
  371. _______________2________
  372. ________________1_______
  373. _________________4______
  374. ___________________2____
  375. ____________________33__
  376. ______________________4_
  377. */
  378. /* if there's only one memory region, don't bother */
  379. if (*pnr_map < 2) {
  380. return -1;
  381. }
  382. old_nr = *pnr_map;
  383. /* bail out if we find any unreasonable addresses in bios map */
  384. for (i=0; i<old_nr; i++)
  385. if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr) {
  386. return -1;
  387. }
  388. /* create pointers for initial change-point information (for sorting) */
  389. for (i=0; i < 2*old_nr; i++)
  390. change_point[i] = &change_point_list[i];
  391. /* record all known change-points (starting and ending addresses),
  392. omitting those that are for empty memory regions */
  393. chgidx = 0;
  394. for (i=0; i < old_nr; i++) {
  395. if (biosmap[i].size != 0) {
  396. change_point[chgidx]->addr = biosmap[i].addr;
  397. change_point[chgidx++]->pbios = &biosmap[i];
  398. change_point[chgidx]->addr = biosmap[i].addr + biosmap[i].size;
  399. change_point[chgidx++]->pbios = &biosmap[i];
  400. }
  401. }
  402. chg_nr = chgidx; /* true number of change-points */
  403. /* sort change-point list by memory addresses (low -> high) */
  404. still_changing = 1;
  405. while (still_changing) {
  406. still_changing = 0;
  407. for (i=1; i < chg_nr; i++) {
  408. /* if <current_addr> > <last_addr>, swap */
  409. /* or, if current=<start_addr> & last=<end_addr>, swap */
  410. if ((change_point[i]->addr < change_point[i-1]->addr) ||
  411. ((change_point[i]->addr == change_point[i-1]->addr) &&
  412. (change_point[i]->addr == change_point[i]->pbios->addr) &&
  413. (change_point[i-1]->addr != change_point[i-1]->pbios->addr))
  414. )
  415. {
  416. change_tmp = change_point[i];
  417. change_point[i] = change_point[i-1];
  418. change_point[i-1] = change_tmp;
  419. still_changing=1;
  420. }
  421. }
  422. }
  423. /* create a new bios memory map, removing overlaps */
  424. overlap_entries=0; /* number of entries in the overlap table */
  425. new_bios_entry=0; /* index for creating new bios map entries */
  426. last_type = 0; /* start with undefined memory type */
  427. last_addr = 0; /* start with 0 as last starting address */
  428. /* loop through change-points, determining affect on the new bios map */
  429. for (chgidx=0; chgidx < chg_nr; chgidx++)
  430. {
  431. /* keep track of all overlapping bios entries */
  432. if (change_point[chgidx]->addr == change_point[chgidx]->pbios->addr)
  433. {
  434. /* add map entry to overlap list (> 1 entry implies an overlap) */
  435. overlap_list[overlap_entries++]=change_point[chgidx]->pbios;
  436. }
  437. else
  438. {
  439. /* remove entry from list (order independent, so swap with last) */
  440. for (i=0; i<overlap_entries; i++)
  441. {
  442. if (overlap_list[i] == change_point[chgidx]->pbios)
  443. overlap_list[i] = overlap_list[overlap_entries-1];
  444. }
  445. overlap_entries--;
  446. }
  447. /* if there are overlapping entries, decide which "type" to use */
  448. /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */
  449. current_type = 0;
  450. for (i=0; i<overlap_entries; i++)
  451. if (overlap_list[i]->type > current_type)
  452. current_type = overlap_list[i]->type;
  453. /* continue building up new bios map based on this information */
  454. if (current_type != last_type) {
  455. if (last_type != 0) {
  456. new_bios[new_bios_entry].size =
  457. change_point[chgidx]->addr - last_addr;
  458. /* move forward only if the new size was non-zero */
  459. if (new_bios[new_bios_entry].size != 0)
  460. if (++new_bios_entry >= E820MAX)
  461. break; /* no more space left for new bios entries */
  462. }
  463. if (current_type != 0) {
  464. new_bios[new_bios_entry].addr = change_point[chgidx]->addr;
  465. new_bios[new_bios_entry].type = current_type;
  466. last_addr=change_point[chgidx]->addr;
  467. }
  468. last_type = current_type;
  469. }
  470. }
  471. new_nr = new_bios_entry; /* retain count for new bios entries */
  472. /* copy new bios mapping into original location */
  473. memcpy(biosmap, new_bios, new_nr*sizeof(struct e820entry));
  474. *pnr_map = new_nr;
  475. return 0;
  476. }
  477. /*
  478. * Copy the BIOS e820 map into a safe place.
  479. *
  480. * Sanity-check it while we're at it..
  481. *
  482. * If we're lucky and live on a modern system, the setup code
  483. * will have given us a memory map that we can use to properly
  484. * set up memory. If we aren't, we'll fake a memory map.
  485. *
  486. * We check to see that the memory map contains at least 2 elements
  487. * before we'll use it, because the detection code in setup.S may
  488. * not be perfect and most every PC known to man has two memory
  489. * regions: one from 0 to 640k, and one from 1mb up. (The IBM
  490. * thinkpad 560x, for example, does not cooperate with the memory
  491. * detection code.)
  492. */
  493. int __init copy_e820_map(struct e820entry * biosmap, int nr_map)
  494. {
  495. /* Only one memory region (or negative)? Ignore it */
  496. if (nr_map < 2)
  497. return -1;
  498. do {
  499. unsigned long long start = biosmap->addr;
  500. unsigned long long size = biosmap->size;
  501. unsigned long long end = start + size;
  502. unsigned long type = biosmap->type;
  503. /* Overflow in 64 bits? Ignore the memory map. */
  504. if (start > end)
  505. return -1;
  506. /*
  507. * Some BIOSes claim RAM in the 640k - 1M region.
  508. * Not right. Fix it up.
  509. */
  510. if (type == E820_RAM) {
  511. if (start < 0x100000ULL && end > 0xA0000ULL) {
  512. if (start < 0xA0000ULL)
  513. add_memory_region(start, 0xA0000ULL-start, type);
  514. if (end <= 0x100000ULL)
  515. continue;
  516. start = 0x100000ULL;
  517. size = end - start;
  518. }
  519. }
  520. add_memory_region(start, size, type);
  521. } while (biosmap++,--nr_map);
  522. return 0;
  523. }
  524. /*
  525. * Callback for efi_memory_walk.
  526. */
  527. static int __init
  528. efi_find_max_pfn(unsigned long start, unsigned long end, void *arg)
  529. {
  530. unsigned long *max_pfn = arg, pfn;
  531. if (start < end) {
  532. pfn = PFN_UP(end -1);
  533. if (pfn > *max_pfn)
  534. *max_pfn = pfn;
  535. }
  536. return 0;
  537. }
  538. static int __init
  539. efi_memory_present_wrapper(unsigned long start, unsigned long end, void *arg)
  540. {
  541. memory_present(0, PFN_UP(start), PFN_DOWN(end));
  542. return 0;
  543. }
  544. /*
  545. * Find the highest page frame number we have available
  546. */
  547. void __init find_max_pfn(void)
  548. {
  549. int i;
  550. max_pfn = 0;
  551. if (efi_enabled) {
  552. efi_memmap_walk(efi_find_max_pfn, &max_pfn);
  553. efi_memmap_walk(efi_memory_present_wrapper, NULL);
  554. return;
  555. }
  556. for (i = 0; i < e820.nr_map; i++) {
  557. unsigned long start, end;
  558. /* RAM? */
  559. if (e820.map[i].type != E820_RAM)
  560. continue;
  561. start = PFN_UP(e820.map[i].addr);
  562. end = PFN_DOWN(e820.map[i].addr + e820.map[i].size);
  563. if (start >= end)
  564. continue;
  565. if (end > max_pfn)
  566. max_pfn = end;
  567. memory_present(0, start, end);
  568. }
  569. }
  570. /*
  571. * Free all available memory for boot time allocation. Used
  572. * as a callback function by efi_memory_walk()
  573. */
  574. static int __init
  575. free_available_memory(unsigned long start, unsigned long end, void *arg)
  576. {
  577. /* check max_low_pfn */
  578. if (start >= (max_low_pfn << PAGE_SHIFT))
  579. return 0;
  580. if (end >= (max_low_pfn << PAGE_SHIFT))
  581. end = max_low_pfn << PAGE_SHIFT;
  582. if (start < end)
  583. free_bootmem(start, end - start);
  584. return 0;
  585. }
  586. /*
  587. * Register fully available low RAM pages with the bootmem allocator.
  588. */
  589. void __init register_bootmem_low_pages(unsigned long max_low_pfn)
  590. {
  591. int i;
  592. if (efi_enabled) {
  593. efi_memmap_walk(free_available_memory, NULL);
  594. return;
  595. }
  596. for (i = 0; i < e820.nr_map; i++) {
  597. unsigned long curr_pfn, last_pfn, size;
  598. /*
  599. * Reserve usable low memory
  600. */
  601. if (e820.map[i].type != E820_RAM)
  602. continue;
  603. /*
  604. * We are rounding up the start address of usable memory:
  605. */
  606. curr_pfn = PFN_UP(e820.map[i].addr);
  607. if (curr_pfn >= max_low_pfn)
  608. continue;
  609. /*
  610. * ... and at the end of the usable range downwards:
  611. */
  612. last_pfn = PFN_DOWN(e820.map[i].addr + e820.map[i].size);
  613. if (last_pfn > max_low_pfn)
  614. last_pfn = max_low_pfn;
  615. /*
  616. * .. finally, did all the rounding and playing
  617. * around just make the area go away?
  618. */
  619. if (last_pfn <= curr_pfn)
  620. continue;
  621. size = last_pfn - curr_pfn;
  622. free_bootmem(PFN_PHYS(curr_pfn), PFN_PHYS(size));
  623. }
  624. }
  625. void __init e820_register_memory(void)
  626. {
  627. unsigned long gapstart, gapsize, round;
  628. unsigned long long last;
  629. int i;
  630. /*
  631. * Search for the biggest gap in the low 32 bits of the e820
  632. * memory space.
  633. */
  634. last = 0x100000000ull;
  635. gapstart = 0x10000000;
  636. gapsize = 0x400000;
  637. i = e820.nr_map;
  638. while (--i >= 0) {
  639. unsigned long long start = e820.map[i].addr;
  640. unsigned long long end = start + e820.map[i].size;
  641. /*
  642. * Since "last" is at most 4GB, we know we'll
  643. * fit in 32 bits if this condition is true
  644. */
  645. if (last > end) {
  646. unsigned long gap = last - end;
  647. if (gap > gapsize) {
  648. gapsize = gap;
  649. gapstart = end;
  650. }
  651. }
  652. if (start < last)
  653. last = start;
  654. }
  655. /*
  656. * See how much we want to round up: start off with
  657. * rounding to the next 1MB area.
  658. */
  659. round = 0x100000;
  660. while ((gapsize >> 4) > round)
  661. round += round;
  662. /* Fun with two's complement */
  663. pci_mem_start = (gapstart + round) & -round;
  664. printk("Allocating PCI resources starting at %08lx (gap: %08lx:%08lx)\n",
  665. pci_mem_start, gapstart, gapsize);
  666. }
  667. void __init print_memory_map(char *who)
  668. {
  669. int i;
  670. for (i = 0; i < e820.nr_map; i++) {
  671. printk(" %s: %016Lx - %016Lx ", who,
  672. e820.map[i].addr,
  673. e820.map[i].addr + e820.map[i].size);
  674. switch (e820.map[i].type) {
  675. case E820_RAM: printk("(usable)\n");
  676. break;
  677. case E820_RESERVED:
  678. printk("(reserved)\n");
  679. break;
  680. case E820_ACPI:
  681. printk("(ACPI data)\n");
  682. break;
  683. case E820_NVS:
  684. printk("(ACPI NVS)\n");
  685. break;
  686. default: printk("type %u\n", e820.map[i].type);
  687. break;
  688. }
  689. }
  690. }
  691. static __init __always_inline void efi_limit_regions(unsigned long long size)
  692. {
  693. unsigned long long current_addr = 0;
  694. efi_memory_desc_t *md, *next_md;
  695. void *p, *p1;
  696. int i, j;
  697. j = 0;
  698. p1 = memmap.map;
  699. for (p = p1, i = 0; p < memmap.map_end; p += memmap.desc_size, i++) {
  700. md = p;
  701. next_md = p1;
  702. current_addr = md->phys_addr +
  703. PFN_PHYS(md->num_pages);
  704. if (is_available_memory(md)) {
  705. if (md->phys_addr >= size) continue;
  706. memcpy(next_md, md, memmap.desc_size);
  707. if (current_addr >= size) {
  708. next_md->num_pages -=
  709. PFN_UP(current_addr-size);
  710. }
  711. p1 += memmap.desc_size;
  712. next_md = p1;
  713. j++;
  714. } else if ((md->attribute & EFI_MEMORY_RUNTIME) ==
  715. EFI_MEMORY_RUNTIME) {
  716. /* In order to make runtime services
  717. * available we have to include runtime
  718. * memory regions in memory map */
  719. memcpy(next_md, md, memmap.desc_size);
  720. p1 += memmap.desc_size;
  721. next_md = p1;
  722. j++;
  723. }
  724. }
  725. memmap.nr_map = j;
  726. memmap.map_end = memmap.map +
  727. (memmap.nr_map * memmap.desc_size);
  728. }
  729. void __init limit_regions(unsigned long long size)
  730. {
  731. unsigned long long current_addr;
  732. int i;
  733. print_memory_map("limit_regions start");
  734. if (efi_enabled) {
  735. efi_limit_regions(size);
  736. return;
  737. }
  738. for (i = 0; i < e820.nr_map; i++) {
  739. current_addr = e820.map[i].addr + e820.map[i].size;
  740. if (current_addr < size)
  741. continue;
  742. if (e820.map[i].type != E820_RAM)
  743. continue;
  744. if (e820.map[i].addr >= size) {
  745. /*
  746. * This region starts past the end of the
  747. * requested size, skip it completely.
  748. */
  749. e820.nr_map = i;
  750. } else {
  751. e820.nr_map = i + 1;
  752. e820.map[i].size -= current_addr - size;
  753. }
  754. print_memory_map("limit_regions endfor");
  755. return;
  756. }
  757. print_memory_map("limit_regions endfunc");
  758. }
  759. /*
  760. * This function checks if any part of the range <start,end> is mapped
  761. * with type.
  762. */
  763. int
  764. e820_any_mapped(u64 start, u64 end, unsigned type)
  765. {
  766. int i;
  767. for (i = 0; i < e820.nr_map; i++) {
  768. const struct e820entry *ei = &e820.map[i];
  769. if (type && ei->type != type)
  770. continue;
  771. if (ei->addr >= end || ei->addr + ei->size <= start)
  772. continue;
  773. return 1;
  774. }
  775. return 0;
  776. }
  777. EXPORT_SYMBOL_GPL(e820_any_mapped);
  778. /*
  779. * This function checks if the entire range <start,end> is mapped with type.
  780. *
  781. * Note: this function only works correct if the e820 table is sorted and
  782. * not-overlapping, which is the case
  783. */
  784. int __init
  785. e820_all_mapped(unsigned long s, unsigned long e, unsigned type)
  786. {
  787. u64 start = s;
  788. u64 end = e;
  789. int i;
  790. for (i = 0; i < e820.nr_map; i++) {
  791. struct e820entry *ei = &e820.map[i];
  792. if (type && ei->type != type)
  793. continue;
  794. /* is the region (part) in overlap with the current region ?*/
  795. if (ei->addr >= end || ei->addr + ei->size <= start)
  796. continue;
  797. /* if the region is at the beginning of <start,end> we move
  798. * start to the end of the region since it's ok until there
  799. */
  800. if (ei->addr <= start)
  801. start = ei->addr + ei->size;
  802. /* if start is now at or beyond end, we're done, full
  803. * coverage */
  804. if (start >= end)
  805. return 1; /* we're done */
  806. }
  807. return 0;
  808. }
  809. static int __init parse_memmap(char *arg)
  810. {
  811. if (!arg)
  812. return -EINVAL;
  813. if (strcmp(arg, "exactmap") == 0) {
  814. #ifdef CONFIG_CRASH_DUMP
  815. /* If we are doing a crash dump, we
  816. * still need to know the real mem
  817. * size before original memory map is
  818. * reset.
  819. */
  820. find_max_pfn();
  821. saved_max_pfn = max_pfn;
  822. #endif
  823. e820.nr_map = 0;
  824. user_defined_memmap = 1;
  825. } else {
  826. /* If the user specifies memory size, we
  827. * limit the BIOS-provided memory map to
  828. * that size. exactmap can be used to specify
  829. * the exact map. mem=number can be used to
  830. * trim the existing memory map.
  831. */
  832. unsigned long long start_at, mem_size;
  833. mem_size = memparse(arg, &arg);
  834. if (*arg == '@') {
  835. start_at = memparse(arg+1, &arg);
  836. add_memory_region(start_at, mem_size, E820_RAM);
  837. } else if (*arg == '#') {
  838. start_at = memparse(arg+1, &arg);
  839. add_memory_region(start_at, mem_size, E820_ACPI);
  840. } else if (*arg == '$') {
  841. start_at = memparse(arg+1, &arg);
  842. add_memory_region(start_at, mem_size, E820_RESERVED);
  843. } else {
  844. limit_regions(mem_size);
  845. user_defined_memmap = 1;
  846. }
  847. }
  848. return 0;
  849. }
  850. early_param("memmap", parse_memmap);