e820_64.c 23 KB

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