e820_32.c 20 KB

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