e820_64.c 24 KB

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