e820_32.c 21 KB

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