e820_64.c 23 KB

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