e820_64.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. /*
  2. * Handle the memory map.
  3. * The functions here do the job until bootmem takes over.
  4. *
  5. * Getting sanitize_e820_map() in sync with i386 version by applying change:
  6. * - Provisions for empty E820 memory regions (reported by certain BIOSes).
  7. * Alex Achenbach <xela@slit.de>, December 2002.
  8. * Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
  9. *
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/types.h>
  13. #include <linux/init.h>
  14. #include <linux/bootmem.h>
  15. #include <linux/ioport.h>
  16. #include <linux/string.h>
  17. #include <linux/kexec.h>
  18. #include <linux/module.h>
  19. #include <linux/mm.h>
  20. #include <linux/suspend.h>
  21. #include <linux/pfn.h>
  22. #include <asm/pgtable.h>
  23. #include <asm/page.h>
  24. #include <asm/e820.h>
  25. #include <asm/proto.h>
  26. #include <asm/setup.h>
  27. #include <asm/sections.h>
  28. #include <asm/kdebug.h>
  29. #include <asm/trampoline.h>
  30. struct e820map e820;
  31. /*
  32. * PFN of last memory page.
  33. */
  34. unsigned long end_pfn;
  35. /*
  36. * end_pfn only includes RAM, while max_pfn_mapped includes all e820 entries.
  37. * The direct mapping extends to max_pfn_mapped, so that we can directly access
  38. * apertures, ACPI and other tables without having to play with fixmaps.
  39. */
  40. unsigned long max_pfn_mapped;
  41. /*
  42. * Last pfn which the user wants to use.
  43. */
  44. static unsigned long __initdata end_user_pfn = MAXMEM>>PAGE_SHIFT;
  45. /*
  46. * Early reserved memory areas.
  47. */
  48. #define MAX_EARLY_RES 20
  49. struct early_res {
  50. unsigned long start, end;
  51. char name[16];
  52. };
  53. static struct early_res early_res[MAX_EARLY_RES] __initdata = {
  54. { 0, PAGE_SIZE, "BIOS data page" }, /* BIOS data page */
  55. #ifdef CONFIG_X86_TRAMPOLINE
  56. { TRAMPOLINE_BASE, TRAMPOLINE_BASE + 2 * PAGE_SIZE, "TRAMPOLINE" },
  57. #endif
  58. {}
  59. };
  60. void __init reserve_early(unsigned long start, unsigned long end, char *name)
  61. {
  62. int i;
  63. struct early_res *r;
  64. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
  65. r = &early_res[i];
  66. if (end > r->start && start < r->end)
  67. panic("Overlapping early reservations %lx-%lx %s to %lx-%lx %s\n",
  68. start, end - 1, name?name:"", r->start, r->end - 1, r->name);
  69. }
  70. if (i >= MAX_EARLY_RES)
  71. panic("Too many early reservations");
  72. r = &early_res[i];
  73. r->start = start;
  74. r->end = end;
  75. if (name)
  76. strncpy(r->name, name, sizeof(r->name) - 1);
  77. }
  78. void __init free_early(unsigned long start, unsigned long end)
  79. {
  80. struct early_res *r;
  81. int i, j;
  82. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
  83. r = &early_res[i];
  84. if (start == r->start && end == r->end)
  85. break;
  86. }
  87. if (i >= MAX_EARLY_RES || !early_res[i].end)
  88. panic("free_early on not reserved area: %lx-%lx!", start, end);
  89. for (j = i + 1; j < MAX_EARLY_RES && early_res[j].end; j++)
  90. ;
  91. memmove(&early_res[i], &early_res[i + 1],
  92. (j - 1 - i) * sizeof(struct early_res));
  93. early_res[j - 1].end = 0;
  94. }
  95. void __init early_res_to_bootmem(unsigned long start, unsigned long end)
  96. {
  97. int i;
  98. unsigned long final_start, final_end;
  99. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
  100. struct early_res *r = &early_res[i];
  101. final_start = max(start, r->start);
  102. final_end = min(end, r->end);
  103. if (final_start >= final_end)
  104. continue;
  105. printk(KERN_INFO " early res: %d [%lx-%lx] %s\n", i,
  106. final_start, final_end - 1, r->name);
  107. reserve_bootmem_generic(final_start, final_end - final_start,
  108. BOOTMEM_DEFAULT);
  109. }
  110. }
  111. /* Check for already reserved areas */
  112. static inline int __init
  113. bad_addr(unsigned long *addrp, unsigned long size, unsigned long align)
  114. {
  115. int i;
  116. unsigned long addr = *addrp, last;
  117. int changed = 0;
  118. again:
  119. last = addr + size;
  120. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
  121. struct early_res *r = &early_res[i];
  122. if (last >= r->start && addr < r->end) {
  123. *addrp = addr = round_up(r->end, align);
  124. changed = 1;
  125. goto again;
  126. }
  127. }
  128. return changed;
  129. }
  130. /* Check for already reserved areas */
  131. static inline int __init
  132. bad_addr_size(unsigned long *addrp, unsigned long *sizep, unsigned long align)
  133. {
  134. int i;
  135. unsigned long addr = *addrp, last;
  136. unsigned long size = *sizep;
  137. int changed = 0;
  138. again:
  139. last = addr + size;
  140. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
  141. struct early_res *r = &early_res[i];
  142. if (last > r->start && addr < r->start) {
  143. size = r->start - addr;
  144. changed = 1;
  145. goto again;
  146. }
  147. if (last > r->end && addr < r->end) {
  148. addr = round_up(r->end, align);
  149. size = last - addr;
  150. changed = 1;
  151. goto again;
  152. }
  153. if (last <= r->end && addr >= r->start) {
  154. (*sizep)++;
  155. return 0;
  156. }
  157. }
  158. if (changed) {
  159. *addrp = addr;
  160. *sizep = size;
  161. }
  162. return changed;
  163. }
  164. /*
  165. * This function checks if any part of the range <start,end> is mapped
  166. * with type.
  167. */
  168. int
  169. e820_any_mapped(unsigned long start, unsigned long end, unsigned type)
  170. {
  171. int i;
  172. for (i = 0; i < e820.nr_map; i++) {
  173. struct e820entry *ei = &e820.map[i];
  174. if (type && ei->type != type)
  175. continue;
  176. if (ei->addr >= end || ei->addr + ei->size <= start)
  177. continue;
  178. return 1;
  179. }
  180. return 0;
  181. }
  182. EXPORT_SYMBOL_GPL(e820_any_mapped);
  183. /*
  184. * This function checks if the entire range <start,end> is mapped with type.
  185. *
  186. * Note: this function only works correct if the e820 table is sorted and
  187. * not-overlapping, which is the case
  188. */
  189. int __init e820_all_mapped(unsigned long start, unsigned long end,
  190. unsigned type)
  191. {
  192. int i;
  193. for (i = 0; i < e820.nr_map; i++) {
  194. struct e820entry *ei = &e820.map[i];
  195. if (type && ei->type != type)
  196. continue;
  197. /* is the region (part) in overlap with the current region ?*/
  198. if (ei->addr >= end || ei->addr + ei->size <= start)
  199. continue;
  200. /* if the region is at the beginning of <start,end> we move
  201. * start to the end of the region since it's ok until there
  202. */
  203. if (ei->addr <= start)
  204. start = ei->addr + ei->size;
  205. /*
  206. * if start is now at or beyond end, we're done, full
  207. * coverage
  208. */
  209. if (start >= end)
  210. return 1;
  211. }
  212. return 0;
  213. }
  214. /*
  215. * Find a free area with specified alignment in a specific range.
  216. */
  217. unsigned long __init find_e820_area(unsigned long start, unsigned long end,
  218. unsigned long size, unsigned long align)
  219. {
  220. int i;
  221. for (i = 0; i < e820.nr_map; i++) {
  222. struct e820entry *ei = &e820.map[i];
  223. unsigned long addr, last;
  224. unsigned long ei_last;
  225. if (ei->type != E820_RAM)
  226. continue;
  227. addr = round_up(ei->addr, align);
  228. ei_last = ei->addr + ei->size;
  229. if (addr < start)
  230. addr = round_up(start, align);
  231. if (addr >= ei_last)
  232. continue;
  233. while (bad_addr(&addr, size, align) && addr+size <= ei_last)
  234. ;
  235. last = addr + size;
  236. if (last > ei_last)
  237. continue;
  238. if (last > end)
  239. continue;
  240. return addr;
  241. }
  242. return -1UL;
  243. }
  244. /*
  245. * Find next free range after *start
  246. */
  247. unsigned long __init find_e820_area_size(unsigned long start,
  248. unsigned long *sizep,
  249. unsigned long align)
  250. {
  251. int i;
  252. for (i = 0; i < e820.nr_map; i++) {
  253. struct e820entry *ei = &e820.map[i];
  254. unsigned long addr, last;
  255. unsigned long ei_last;
  256. if (ei->type != E820_RAM)
  257. continue;
  258. addr = round_up(ei->addr, align);
  259. ei_last = ei->addr + ei->size;
  260. if (addr < start)
  261. addr = round_up(start, align);
  262. if (addr >= ei_last)
  263. continue;
  264. *sizep = ei_last - addr;
  265. while (bad_addr_size(&addr, sizep, align) &&
  266. addr + *sizep <= ei_last)
  267. ;
  268. last = addr + *sizep;
  269. if (last > ei_last)
  270. continue;
  271. return addr;
  272. }
  273. return -1UL;
  274. }
  275. /*
  276. * Find the highest page frame number we have available
  277. */
  278. unsigned long __init e820_end_of_ram(void)
  279. {
  280. unsigned long end_pfn;
  281. end_pfn = find_max_pfn_with_active_regions();
  282. if (end_pfn > max_pfn_mapped)
  283. max_pfn_mapped = end_pfn;
  284. if (max_pfn_mapped > MAXMEM>>PAGE_SHIFT)
  285. max_pfn_mapped = MAXMEM>>PAGE_SHIFT;
  286. if (end_pfn > end_user_pfn)
  287. end_pfn = end_user_pfn;
  288. if (end_pfn > max_pfn_mapped)
  289. end_pfn = max_pfn_mapped;
  290. printk(KERN_INFO "max_pfn_mapped = %lu\n", max_pfn_mapped);
  291. return end_pfn;
  292. }
  293. /*
  294. * Mark e820 reserved areas as busy for the resource manager.
  295. */
  296. void __init e820_reserve_resources(void)
  297. {
  298. int i;
  299. struct resource *res;
  300. res = alloc_bootmem_low(sizeof(struct resource) * e820.nr_map);
  301. for (i = 0; i < e820.nr_map; i++) {
  302. switch (e820.map[i].type) {
  303. case E820_RAM: res->name = "System RAM"; break;
  304. case E820_ACPI: res->name = "ACPI Tables"; break;
  305. case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
  306. default: res->name = "reserved";
  307. }
  308. res->start = e820.map[i].addr;
  309. res->end = res->start + e820.map[i].size - 1;
  310. res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  311. insert_resource(&iomem_resource, res);
  312. res++;
  313. }
  314. }
  315. /*
  316. * Find the ranges of physical addresses that do not correspond to
  317. * e820 RAM areas and mark the corresponding pages as nosave for software
  318. * suspend and suspend to RAM.
  319. *
  320. * This function requires the e820 map to be sorted and without any
  321. * overlapping entries and assumes the first e820 area to be RAM.
  322. */
  323. void __init e820_mark_nosave_regions(void)
  324. {
  325. int i;
  326. unsigned long paddr;
  327. paddr = round_down(e820.map[0].addr + e820.map[0].size, PAGE_SIZE);
  328. for (i = 1; i < e820.nr_map; i++) {
  329. struct e820entry *ei = &e820.map[i];
  330. if (paddr < ei->addr)
  331. register_nosave_region(PFN_DOWN(paddr),
  332. PFN_UP(ei->addr));
  333. paddr = round_down(ei->addr + ei->size, PAGE_SIZE);
  334. if (ei->type != E820_RAM)
  335. register_nosave_region(PFN_UP(ei->addr),
  336. PFN_DOWN(paddr));
  337. if (paddr >= (end_pfn << PAGE_SHIFT))
  338. break;
  339. }
  340. }
  341. /*
  342. * Finds an active region in the address range from start_pfn to end_pfn and
  343. * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
  344. */
  345. static int __init e820_find_active_region(const struct e820entry *ei,
  346. unsigned long start_pfn,
  347. unsigned long end_pfn,
  348. unsigned long *ei_startpfn,
  349. unsigned long *ei_endpfn)
  350. {
  351. *ei_startpfn = round_up(ei->addr, PAGE_SIZE) >> PAGE_SHIFT;
  352. *ei_endpfn = round_down(ei->addr + ei->size, PAGE_SIZE) >> PAGE_SHIFT;
  353. /* Skip map entries smaller than a page */
  354. if (*ei_startpfn >= *ei_endpfn)
  355. return 0;
  356. /* Check if max_pfn_mapped should be updated */
  357. if (ei->type != E820_RAM && *ei_endpfn > max_pfn_mapped)
  358. max_pfn_mapped = *ei_endpfn;
  359. /* Skip if map is outside the node */
  360. if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
  361. *ei_startpfn >= end_pfn)
  362. return 0;
  363. /* Check for overlaps */
  364. if (*ei_startpfn < start_pfn)
  365. *ei_startpfn = start_pfn;
  366. if (*ei_endpfn > end_pfn)
  367. *ei_endpfn = end_pfn;
  368. /* Obey end_user_pfn to save on memmap */
  369. if (*ei_startpfn >= end_user_pfn)
  370. return 0;
  371. if (*ei_endpfn > end_user_pfn)
  372. *ei_endpfn = end_user_pfn;
  373. return 1;
  374. }
  375. /* Walk the e820 map and register active regions within a node */
  376. void __init
  377. e820_register_active_regions(int nid, unsigned long start_pfn,
  378. unsigned long end_pfn)
  379. {
  380. unsigned long ei_startpfn;
  381. unsigned long ei_endpfn;
  382. int i;
  383. for (i = 0; i < e820.nr_map; i++)
  384. if (e820_find_active_region(&e820.map[i],
  385. start_pfn, end_pfn,
  386. &ei_startpfn, &ei_endpfn))
  387. add_active_range(nid, ei_startpfn, ei_endpfn);
  388. }
  389. /*
  390. * Add a memory region to the kernel e820 map.
  391. */
  392. void __init add_memory_region(unsigned long start, unsigned long size, int type)
  393. {
  394. int x = e820.nr_map;
  395. if (x == E820MAX) {
  396. printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
  397. return;
  398. }
  399. e820.map[x].addr = start;
  400. e820.map[x].size = size;
  401. e820.map[x].type = type;
  402. e820.nr_map++;
  403. }
  404. /*
  405. * Find the hole size (in bytes) in the memory range.
  406. * @start: starting address of the memory range to scan
  407. * @end: ending address of the memory range to scan
  408. */
  409. unsigned long __init e820_hole_size(unsigned long start, unsigned long end)
  410. {
  411. unsigned long start_pfn = start >> PAGE_SHIFT;
  412. unsigned long end_pfn = end >> PAGE_SHIFT;
  413. unsigned long ei_startpfn, ei_endpfn, ram = 0;
  414. int i;
  415. for (i = 0; i < e820.nr_map; i++) {
  416. if (e820_find_active_region(&e820.map[i],
  417. start_pfn, end_pfn,
  418. &ei_startpfn, &ei_endpfn))
  419. ram += ei_endpfn - ei_startpfn;
  420. }
  421. return end - start - (ram << PAGE_SHIFT);
  422. }
  423. static void __init e820_print_map(char *who)
  424. {
  425. int i;
  426. for (i = 0; i < e820.nr_map; i++) {
  427. printk(KERN_INFO " %s: %016Lx - %016Lx ", who,
  428. (unsigned long long) e820.map[i].addr,
  429. (unsigned long long)
  430. (e820.map[i].addr + e820.map[i].size));
  431. switch (e820.map[i].type) {
  432. case E820_RAM:
  433. printk(KERN_CONT "(usable)\n");
  434. break;
  435. case E820_RESERVED:
  436. printk(KERN_CONT "(reserved)\n");
  437. break;
  438. case E820_ACPI:
  439. printk(KERN_CONT "(ACPI data)\n");
  440. break;
  441. case E820_NVS:
  442. printk(KERN_CONT "(ACPI NVS)\n");
  443. break;
  444. default:
  445. printk(KERN_CONT "type %u\n", e820.map[i].type);
  446. break;
  447. }
  448. }
  449. }
  450. /*
  451. * Sanitize the BIOS e820 map.
  452. *
  453. * Some e820 responses include overlapping entries. The following
  454. * replaces the original e820 map with a new one, removing overlaps.
  455. *
  456. */
  457. static int __init sanitize_e820_map(struct e820entry *biosmap, char *pnr_map)
  458. {
  459. struct change_member {
  460. struct e820entry *pbios; /* pointer to original bios entry */
  461. unsigned long long addr; /* address for this change point */
  462. };
  463. static struct change_member change_point_list[2*E820MAX] __initdata;
  464. static struct change_member *change_point[2*E820MAX] __initdata;
  465. static struct e820entry *overlap_list[E820MAX] __initdata;
  466. static struct e820entry new_bios[E820MAX] __initdata;
  467. struct change_member *change_tmp;
  468. unsigned long current_type, last_type;
  469. unsigned long long last_addr;
  470. int chgidx, still_changing;
  471. int overlap_entries;
  472. int new_bios_entry;
  473. int old_nr, new_nr, chg_nr;
  474. int i;
  475. /*
  476. Visually we're performing the following
  477. (1,2,3,4 = memory types)...
  478. Sample memory map (w/overlaps):
  479. ____22__________________
  480. ______________________4_
  481. ____1111________________
  482. _44_____________________
  483. 11111111________________
  484. ____________________33__
  485. ___________44___________
  486. __________33333_________
  487. ______________22________
  488. ___________________2222_
  489. _________111111111______
  490. _____________________11_
  491. _________________4______
  492. Sanitized equivalent (no overlap):
  493. 1_______________________
  494. _44_____________________
  495. ___1____________________
  496. ____22__________________
  497. ______11________________
  498. _________1______________
  499. __________3_____________
  500. ___________44___________
  501. _____________33_________
  502. _______________2________
  503. ________________1_______
  504. _________________4______
  505. ___________________2____
  506. ____________________33__
  507. ______________________4_
  508. */
  509. /* if there's only one memory region, don't bother */
  510. if (*pnr_map < 2)
  511. return -1;
  512. old_nr = *pnr_map;
  513. /* bail out if we find any unreasonable addresses in bios map */
  514. for (i = 0; i < old_nr; i++)
  515. if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
  516. return -1;
  517. /* create pointers for initial change-point information (for sorting) */
  518. for (i = 0; i < 2 * old_nr; i++)
  519. change_point[i] = &change_point_list[i];
  520. /* record all known change-points (starting and ending addresses),
  521. omitting those that are for empty memory regions */
  522. chgidx = 0;
  523. for (i = 0; i < old_nr; i++) {
  524. if (biosmap[i].size != 0) {
  525. change_point[chgidx]->addr = biosmap[i].addr;
  526. change_point[chgidx++]->pbios = &biosmap[i];
  527. change_point[chgidx]->addr = biosmap[i].addr +
  528. biosmap[i].size;
  529. change_point[chgidx++]->pbios = &biosmap[i];
  530. }
  531. }
  532. chg_nr = chgidx;
  533. /* sort change-point list by memory addresses (low -> high) */
  534. still_changing = 1;
  535. while (still_changing) {
  536. still_changing = 0;
  537. for (i = 1; i < chg_nr; i++) {
  538. unsigned long long curaddr, lastaddr;
  539. unsigned long long curpbaddr, lastpbaddr;
  540. curaddr = change_point[i]->addr;
  541. lastaddr = change_point[i - 1]->addr;
  542. curpbaddr = change_point[i]->pbios->addr;
  543. lastpbaddr = change_point[i - 1]->pbios->addr;
  544. /*
  545. * swap entries, when:
  546. *
  547. * curaddr > lastaddr or
  548. * curaddr == lastaddr and curaddr == curpbaddr and
  549. * lastaddr != lastpbaddr
  550. */
  551. if (curaddr < lastaddr ||
  552. (curaddr == lastaddr && curaddr == curpbaddr &&
  553. lastaddr != lastpbaddr)) {
  554. change_tmp = change_point[i];
  555. change_point[i] = change_point[i-1];
  556. change_point[i-1] = change_tmp;
  557. still_changing = 1;
  558. }
  559. }
  560. }
  561. /* create a new bios memory map, removing overlaps */
  562. overlap_entries = 0; /* number of entries in the overlap table */
  563. new_bios_entry = 0; /* index for creating new bios map entries */
  564. last_type = 0; /* start with undefined memory type */
  565. last_addr = 0; /* start with 0 as last starting address */
  566. /* loop through change-points, determining affect on the new bios map */
  567. for (chgidx = 0; chgidx < chg_nr; chgidx++) {
  568. /* keep track of all overlapping bios entries */
  569. if (change_point[chgidx]->addr ==
  570. change_point[chgidx]->pbios->addr) {
  571. /*
  572. * add map entry to overlap list (> 1 entry
  573. * implies an overlap)
  574. */
  575. overlap_list[overlap_entries++] =
  576. change_point[chgidx]->pbios;
  577. } else {
  578. /*
  579. * remove entry from list (order independent,
  580. * so swap with last)
  581. */
  582. for (i = 0; i < overlap_entries; i++) {
  583. if (overlap_list[i] ==
  584. change_point[chgidx]->pbios)
  585. overlap_list[i] =
  586. overlap_list[overlap_entries-1];
  587. }
  588. overlap_entries--;
  589. }
  590. /*
  591. * if there are overlapping entries, decide which
  592. * "type" to use (larger value takes precedence --
  593. * 1=usable, 2,3,4,4+=unusable)
  594. */
  595. current_type = 0;
  596. for (i = 0; i < overlap_entries; i++)
  597. if (overlap_list[i]->type > current_type)
  598. current_type = overlap_list[i]->type;
  599. /*
  600. * continue building up new bios map based on this
  601. * information
  602. */
  603. if (current_type != last_type) {
  604. if (last_type != 0) {
  605. new_bios[new_bios_entry].size =
  606. change_point[chgidx]->addr - last_addr;
  607. /*
  608. * move forward only if the new size
  609. * was non-zero
  610. */
  611. if (new_bios[new_bios_entry].size != 0)
  612. /*
  613. * no more space left for new
  614. * bios entries ?
  615. */
  616. if (++new_bios_entry >= E820MAX)
  617. break;
  618. }
  619. if (current_type != 0) {
  620. new_bios[new_bios_entry].addr =
  621. change_point[chgidx]->addr;
  622. new_bios[new_bios_entry].type = current_type;
  623. last_addr = change_point[chgidx]->addr;
  624. }
  625. last_type = current_type;
  626. }
  627. }
  628. /* retain count for new bios entries */
  629. new_nr = new_bios_entry;
  630. /* copy new bios mapping into original location */
  631. memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
  632. *pnr_map = new_nr;
  633. return 0;
  634. }
  635. /*
  636. * Copy the BIOS e820 map into a safe place.
  637. *
  638. * Sanity-check it while we're at it..
  639. *
  640. * If we're lucky and live on a modern system, the setup code
  641. * will have given us a memory map that we can use to properly
  642. * set up memory. If we aren't, we'll fake a memory map.
  643. */
  644. static int __init copy_e820_map(struct e820entry *biosmap, int nr_map)
  645. {
  646. /* Only one memory region (or negative)? Ignore it */
  647. if (nr_map < 2)
  648. return -1;
  649. do {
  650. u64 start = biosmap->addr;
  651. u64 size = biosmap->size;
  652. u64 end = start + size;
  653. u32 type = biosmap->type;
  654. /* Overflow in 64 bits? Ignore the memory map. */
  655. if (start > end)
  656. return -1;
  657. add_memory_region(start, size, type);
  658. } while (biosmap++, --nr_map);
  659. return 0;
  660. }
  661. static void early_panic(char *msg)
  662. {
  663. early_printk(msg);
  664. panic(msg);
  665. }
  666. /* We're not void only for x86 32-bit compat */
  667. char * __init machine_specific_memory_setup(void)
  668. {
  669. char *who = "BIOS-e820";
  670. /*
  671. * Try to copy the BIOS-supplied E820-map.
  672. *
  673. * Otherwise fake a memory map; one section from 0k->640k,
  674. * the next section from 1mb->appropriate_mem_k
  675. */
  676. sanitize_e820_map(boot_params.e820_map, &boot_params.e820_entries);
  677. if (copy_e820_map(boot_params.e820_map, boot_params.e820_entries) < 0)
  678. early_panic("Cannot find a valid memory map");
  679. printk(KERN_INFO "BIOS-provided physical RAM map:\n");
  680. e820_print_map(who);
  681. /* In case someone cares... */
  682. return who;
  683. }
  684. static int __init parse_memopt(char *p)
  685. {
  686. if (!p)
  687. return -EINVAL;
  688. end_user_pfn = memparse(p, &p);
  689. end_user_pfn >>= PAGE_SHIFT;
  690. return 0;
  691. }
  692. early_param("mem", parse_memopt);
  693. static int userdef __initdata;
  694. static int __init parse_memmap_opt(char *p)
  695. {
  696. char *oldp;
  697. unsigned long long start_at, mem_size;
  698. if (!strcmp(p, "exactmap")) {
  699. #ifdef CONFIG_CRASH_DUMP
  700. /*
  701. * If we are doing a crash dump, we still need to know
  702. * the real mem size before original memory map is
  703. * reset.
  704. */
  705. e820_register_active_regions(0, 0, -1UL);
  706. saved_max_pfn = e820_end_of_ram();
  707. remove_all_active_ranges();
  708. #endif
  709. max_pfn_mapped = 0;
  710. e820.nr_map = 0;
  711. userdef = 1;
  712. return 0;
  713. }
  714. oldp = p;
  715. mem_size = memparse(p, &p);
  716. if (p == oldp)
  717. return -EINVAL;
  718. userdef = 1;
  719. if (*p == '@') {
  720. start_at = memparse(p+1, &p);
  721. add_memory_region(start_at, mem_size, E820_RAM);
  722. } else if (*p == '#') {
  723. start_at = memparse(p+1, &p);
  724. add_memory_region(start_at, mem_size, E820_ACPI);
  725. } else if (*p == '$') {
  726. start_at = memparse(p+1, &p);
  727. add_memory_region(start_at, mem_size, E820_RESERVED);
  728. } else {
  729. end_user_pfn = (mem_size >> PAGE_SHIFT);
  730. }
  731. return *p == '\0' ? 0 : -EINVAL;
  732. }
  733. early_param("memmap", parse_memmap_opt);
  734. void __init finish_e820_parsing(void)
  735. {
  736. if (userdef) {
  737. char nr = e820.nr_map;
  738. if (sanitize_e820_map(e820.map, &nr) < 0)
  739. early_panic("Invalid user supplied memory map");
  740. e820.nr_map = nr;
  741. printk(KERN_INFO "user-defined physical RAM map:\n");
  742. e820_print_map("user");
  743. }
  744. }
  745. void __init update_memory_range(u64 start, u64 size, unsigned old_type,
  746. unsigned new_type)
  747. {
  748. int i;
  749. BUG_ON(old_type == new_type);
  750. for (i = 0; i < e820.nr_map; i++) {
  751. struct e820entry *ei = &e820.map[i];
  752. u64 final_start, final_end;
  753. if (ei->type != old_type)
  754. continue;
  755. /* totally covered? */
  756. if (ei->addr >= start && ei->size <= size) {
  757. ei->type = new_type;
  758. continue;
  759. }
  760. /* partially covered */
  761. final_start = max(start, ei->addr);
  762. final_end = min(start + size, ei->addr + ei->size);
  763. if (final_start >= final_end)
  764. continue;
  765. add_memory_region(final_start, final_end - final_start,
  766. new_type);
  767. }
  768. }
  769. void __init update_e820(void)
  770. {
  771. u8 nr_map;
  772. nr_map = e820.nr_map;
  773. if (sanitize_e820_map(e820.map, &nr_map))
  774. return;
  775. e820.nr_map = nr_map;
  776. printk(KERN_INFO "modified physical RAM map:\n");
  777. e820_print_map("modified");
  778. }
  779. unsigned long pci_mem_start = 0xaeedbabe;
  780. EXPORT_SYMBOL(pci_mem_start);
  781. /*
  782. * Search for the biggest gap in the low 32 bits of the e820
  783. * memory space. We pass this space to PCI to assign MMIO resources
  784. * for hotplug or unconfigured devices in.
  785. * Hopefully the BIOS let enough space left.
  786. */
  787. __init void e820_setup_gap(void)
  788. {
  789. unsigned long gapstart, gapsize, round;
  790. unsigned long last;
  791. int i;
  792. int found = 0;
  793. last = 0x100000000ull;
  794. gapstart = 0x10000000;
  795. gapsize = 0x400000;
  796. i = e820.nr_map;
  797. while (--i >= 0) {
  798. unsigned long long start = e820.map[i].addr;
  799. unsigned long long end = start + e820.map[i].size;
  800. /*
  801. * Since "last" is at most 4GB, we know we'll
  802. * fit in 32 bits if this condition is true
  803. */
  804. if (last > end) {
  805. unsigned long gap = last - end;
  806. if (gap > gapsize) {
  807. gapsize = gap;
  808. gapstart = end;
  809. found = 1;
  810. }
  811. }
  812. if (start < last)
  813. last = start;
  814. }
  815. if (!found) {
  816. gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
  817. printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit "
  818. "address range\n"
  819. KERN_ERR "PCI: Unassigned devices with 32bit resource "
  820. "registers may break!\n");
  821. }
  822. /*
  823. * See how much we want to round up: start off with
  824. * rounding to the next 1MB area.
  825. */
  826. round = 0x100000;
  827. while ((gapsize >> 4) > round)
  828. round += round;
  829. /* Fun with two's complement */
  830. pci_mem_start = (gapstart + round) & -round;
  831. printk(KERN_INFO
  832. "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
  833. pci_mem_start, gapstart, gapsize);
  834. }
  835. int __init arch_get_ram_range(int slot, u64 *addr, u64 *size)
  836. {
  837. int i;
  838. if (slot < 0 || slot >= e820.nr_map)
  839. return -1;
  840. for (i = slot; i < e820.nr_map; i++) {
  841. if (e820.map[i].type != E820_RAM)
  842. continue;
  843. break;
  844. }
  845. if (i == e820.nr_map || e820.map[i].addr > (max_pfn << PAGE_SHIFT))
  846. return -1;
  847. *addr = e820.map[i].addr;
  848. *size = min_t(u64, e820.map[i].size + e820.map[i].addr,
  849. max_pfn << PAGE_SHIFT) - *addr;
  850. return i + 1;
  851. }