e820_64.c 23 KB

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