e820_64.c 23 KB

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