e820_64.c 21 KB

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