e820.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  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/crash_dump.h>
  15. #include <linux/export.h>
  16. #include <linux/bootmem.h>
  17. #include <linux/pfn.h>
  18. #include <linux/suspend.h>
  19. #include <linux/acpi.h>
  20. #include <linux/firmware-map.h>
  21. #include <linux/memblock.h>
  22. #include <linux/sort.h>
  23. #include <asm/e820.h>
  24. #include <asm/proto.h>
  25. #include <asm/setup.h>
  26. /*
  27. * The e820 map is the map that gets modified e.g. with command line parameters
  28. * and that is also registered with modifications in the kernel resource tree
  29. * with the iomem_resource as parent.
  30. *
  31. * The e820_saved is directly saved after the BIOS-provided memory map is
  32. * copied. It doesn't get modified afterwards. It's registered for the
  33. * /sys/firmware/memmap interface.
  34. *
  35. * That memory map is not modified and is used as base for kexec. The kexec'd
  36. * kernel should get the same memory map as the firmware provides. Then the
  37. * user can e.g. boot the original kernel with mem=1G while still booting the
  38. * next kernel with full memory.
  39. */
  40. struct e820map e820;
  41. struct e820map e820_saved;
  42. /* For PCI or other memory-mapped resources */
  43. unsigned long pci_mem_start = 0xaeedbabe;
  44. #ifdef CONFIG_PCI
  45. EXPORT_SYMBOL(pci_mem_start);
  46. #endif
  47. /*
  48. * This function checks if any part of the range <start,end> is mapped
  49. * with type.
  50. */
  51. int
  52. e820_any_mapped(u64 start, u64 end, unsigned type)
  53. {
  54. int i;
  55. for (i = 0; i < e820.nr_map; i++) {
  56. struct e820entry *ei = &e820.map[i];
  57. if (type && ei->type != type)
  58. continue;
  59. if (ei->addr >= end || ei->addr + ei->size <= start)
  60. continue;
  61. return 1;
  62. }
  63. return 0;
  64. }
  65. EXPORT_SYMBOL_GPL(e820_any_mapped);
  66. /*
  67. * This function checks if the entire range <start,end> is mapped with type.
  68. *
  69. * Note: this function only works correct if the e820 table is sorted and
  70. * not-overlapping, which is the case
  71. */
  72. int __init e820_all_mapped(u64 start, u64 end, unsigned type)
  73. {
  74. int i;
  75. for (i = 0; i < e820.nr_map; i++) {
  76. struct e820entry *ei = &e820.map[i];
  77. if (type && ei->type != type)
  78. continue;
  79. /* is the region (part) in overlap with the current region ?*/
  80. if (ei->addr >= end || ei->addr + ei->size <= start)
  81. continue;
  82. /* if the region is at the beginning of <start,end> we move
  83. * start to the end of the region since it's ok until there
  84. */
  85. if (ei->addr <= start)
  86. start = ei->addr + ei->size;
  87. /*
  88. * if start is now at or beyond end, we're done, full
  89. * coverage
  90. */
  91. if (start >= end)
  92. return 1;
  93. }
  94. return 0;
  95. }
  96. /*
  97. * Add a memory region to the kernel e820 map.
  98. */
  99. static void __init __e820_add_region(struct e820map *e820x, u64 start, u64 size,
  100. int type)
  101. {
  102. int x = e820x->nr_map;
  103. if (x >= ARRAY_SIZE(e820x->map)) {
  104. printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
  105. return;
  106. }
  107. e820x->map[x].addr = start;
  108. e820x->map[x].size = size;
  109. e820x->map[x].type = type;
  110. e820x->nr_map++;
  111. }
  112. void __init e820_add_region(u64 start, u64 size, int type)
  113. {
  114. __e820_add_region(&e820, start, size, type);
  115. }
  116. static void __init e820_print_type(u32 type)
  117. {
  118. switch (type) {
  119. case E820_RAM:
  120. case E820_RESERVED_KERN:
  121. printk(KERN_CONT "(usable)");
  122. break;
  123. case E820_RESERVED:
  124. printk(KERN_CONT "(reserved)");
  125. break;
  126. case E820_ACPI:
  127. printk(KERN_CONT "(ACPI data)");
  128. break;
  129. case E820_NVS:
  130. printk(KERN_CONT "(ACPI NVS)");
  131. break;
  132. case E820_UNUSABLE:
  133. printk(KERN_CONT "(unusable)");
  134. break;
  135. default:
  136. printk(KERN_CONT "type %u", type);
  137. break;
  138. }
  139. }
  140. void __init e820_print_map(char *who)
  141. {
  142. int i;
  143. for (i = 0; i < e820.nr_map; i++) {
  144. printk(KERN_INFO " %s: %016Lx - %016Lx ", who,
  145. (unsigned long long) e820.map[i].addr,
  146. (unsigned long long)
  147. (e820.map[i].addr + e820.map[i].size));
  148. e820_print_type(e820.map[i].type);
  149. printk(KERN_CONT "\n");
  150. }
  151. }
  152. /*
  153. * Sanitize the BIOS e820 map.
  154. *
  155. * Some e820 responses include overlapping entries. The following
  156. * replaces the original e820 map with a new one, removing overlaps,
  157. * and resolving conflicting memory types in favor of highest
  158. * numbered type.
  159. *
  160. * The input parameter biosmap points to an array of 'struct
  161. * e820entry' which on entry has elements in the range [0, *pnr_map)
  162. * valid, and which has space for up to max_nr_map entries.
  163. * On return, the resulting sanitized e820 map entries will be in
  164. * overwritten in the same location, starting at biosmap.
  165. *
  166. * The integer pointed to by pnr_map must be valid on entry (the
  167. * current number of valid entries located at biosmap) and will
  168. * be updated on return, with the new number of valid entries
  169. * (something no more than max_nr_map.)
  170. *
  171. * The return value from sanitize_e820_map() is zero if it
  172. * successfully 'sanitized' the map entries passed in, and is -1
  173. * if it did nothing, which can happen if either of (1) it was
  174. * only passed one map entry, or (2) any of the input map entries
  175. * were invalid (start + size < start, meaning that the size was
  176. * so big the described memory range wrapped around through zero.)
  177. *
  178. * Visually we're performing the following
  179. * (1,2,3,4 = memory types)...
  180. *
  181. * Sample memory map (w/overlaps):
  182. * ____22__________________
  183. * ______________________4_
  184. * ____1111________________
  185. * _44_____________________
  186. * 11111111________________
  187. * ____________________33__
  188. * ___________44___________
  189. * __________33333_________
  190. * ______________22________
  191. * ___________________2222_
  192. * _________111111111______
  193. * _____________________11_
  194. * _________________4______
  195. *
  196. * Sanitized equivalent (no overlap):
  197. * 1_______________________
  198. * _44_____________________
  199. * ___1____________________
  200. * ____22__________________
  201. * ______11________________
  202. * _________1______________
  203. * __________3_____________
  204. * ___________44___________
  205. * _____________33_________
  206. * _______________2________
  207. * ________________1_______
  208. * _________________4______
  209. * ___________________2____
  210. * ____________________33__
  211. * ______________________4_
  212. */
  213. struct change_member {
  214. struct e820entry *pbios; /* pointer to original bios entry */
  215. unsigned long long addr; /* address for this change point */
  216. };
  217. static int __init cpcompare(const void *a, const void *b)
  218. {
  219. struct change_member * const *app = a, * const *bpp = b;
  220. const struct change_member *ap = *app, *bp = *bpp;
  221. /*
  222. * Inputs are pointers to two elements of change_point[]. If their
  223. * addresses are unequal, their difference dominates. If the addresses
  224. * are equal, then consider one that represents the end of its region
  225. * to be greater than one that does not.
  226. */
  227. if (ap->addr != bp->addr)
  228. return ap->addr > bp->addr ? 1 : -1;
  229. return (ap->addr != ap->pbios->addr) - (bp->addr != bp->pbios->addr);
  230. }
  231. int __init sanitize_e820_map(struct e820entry *biosmap, int max_nr_map,
  232. u32 *pnr_map)
  233. {
  234. static struct change_member change_point_list[2*E820_X_MAX] __initdata;
  235. static struct change_member *change_point[2*E820_X_MAX] __initdata;
  236. static struct e820entry *overlap_list[E820_X_MAX] __initdata;
  237. static struct e820entry new_bios[E820_X_MAX] __initdata;
  238. unsigned long current_type, last_type;
  239. unsigned long long last_addr;
  240. int chgidx;
  241. int overlap_entries;
  242. int new_bios_entry;
  243. int old_nr, new_nr, chg_nr;
  244. int i;
  245. /* if there's only one memory region, don't bother */
  246. if (*pnr_map < 2)
  247. return -1;
  248. old_nr = *pnr_map;
  249. BUG_ON(old_nr > max_nr_map);
  250. /* bail out if we find any unreasonable addresses in bios map */
  251. for (i = 0; i < old_nr; i++)
  252. if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
  253. return -1;
  254. /* create pointers for initial change-point information (for sorting) */
  255. for (i = 0; i < 2 * old_nr; i++)
  256. change_point[i] = &change_point_list[i];
  257. /* record all known change-points (starting and ending addresses),
  258. omitting those that are for empty memory regions */
  259. chgidx = 0;
  260. for (i = 0; i < old_nr; i++) {
  261. if (biosmap[i].size != 0) {
  262. change_point[chgidx]->addr = biosmap[i].addr;
  263. change_point[chgidx++]->pbios = &biosmap[i];
  264. change_point[chgidx]->addr = biosmap[i].addr +
  265. biosmap[i].size;
  266. change_point[chgidx++]->pbios = &biosmap[i];
  267. }
  268. }
  269. chg_nr = chgidx;
  270. /* sort change-point list by memory addresses (low -> high) */
  271. sort(change_point, chg_nr, sizeof *change_point, cpcompare, NULL);
  272. /* create a new bios memory map, removing overlaps */
  273. overlap_entries = 0; /* number of entries in the overlap table */
  274. new_bios_entry = 0; /* index for creating new bios map entries */
  275. last_type = 0; /* start with undefined memory type */
  276. last_addr = 0; /* start with 0 as last starting address */
  277. /* loop through change-points, determining affect on the new bios map */
  278. for (chgidx = 0; chgidx < chg_nr; chgidx++) {
  279. /* keep track of all overlapping bios entries */
  280. if (change_point[chgidx]->addr ==
  281. change_point[chgidx]->pbios->addr) {
  282. /*
  283. * add map entry to overlap list (> 1 entry
  284. * implies an overlap)
  285. */
  286. overlap_list[overlap_entries++] =
  287. change_point[chgidx]->pbios;
  288. } else {
  289. /*
  290. * remove entry from list (order independent,
  291. * so swap with last)
  292. */
  293. for (i = 0; i < overlap_entries; i++) {
  294. if (overlap_list[i] ==
  295. change_point[chgidx]->pbios)
  296. overlap_list[i] =
  297. overlap_list[overlap_entries-1];
  298. }
  299. overlap_entries--;
  300. }
  301. /*
  302. * if there are overlapping entries, decide which
  303. * "type" to use (larger value takes precedence --
  304. * 1=usable, 2,3,4,4+=unusable)
  305. */
  306. current_type = 0;
  307. for (i = 0; i < overlap_entries; i++)
  308. if (overlap_list[i]->type > current_type)
  309. current_type = overlap_list[i]->type;
  310. /*
  311. * continue building up new bios map based on this
  312. * information
  313. */
  314. if (current_type != last_type) {
  315. if (last_type != 0) {
  316. new_bios[new_bios_entry].size =
  317. change_point[chgidx]->addr - last_addr;
  318. /*
  319. * move forward only if the new size
  320. * was non-zero
  321. */
  322. if (new_bios[new_bios_entry].size != 0)
  323. /*
  324. * no more space left for new
  325. * bios entries ?
  326. */
  327. if (++new_bios_entry >= max_nr_map)
  328. break;
  329. }
  330. if (current_type != 0) {
  331. new_bios[new_bios_entry].addr =
  332. change_point[chgidx]->addr;
  333. new_bios[new_bios_entry].type = current_type;
  334. last_addr = change_point[chgidx]->addr;
  335. }
  336. last_type = current_type;
  337. }
  338. }
  339. /* retain count for new bios entries */
  340. new_nr = new_bios_entry;
  341. /* copy new bios mapping into original location */
  342. memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
  343. *pnr_map = new_nr;
  344. return 0;
  345. }
  346. static int __init __append_e820_map(struct e820entry *biosmap, int nr_map)
  347. {
  348. while (nr_map) {
  349. u64 start = biosmap->addr;
  350. u64 size = biosmap->size;
  351. u64 end = start + size;
  352. u32 type = biosmap->type;
  353. /* Overflow in 64 bits? Ignore the memory map. */
  354. if (start > end)
  355. return -1;
  356. e820_add_region(start, size, type);
  357. biosmap++;
  358. nr_map--;
  359. }
  360. return 0;
  361. }
  362. /*
  363. * Copy the BIOS e820 map into a safe place.
  364. *
  365. * Sanity-check it while we're at it..
  366. *
  367. * If we're lucky and live on a modern system, the setup code
  368. * will have given us a memory map that we can use to properly
  369. * set up memory. If we aren't, we'll fake a memory map.
  370. */
  371. static int __init append_e820_map(struct e820entry *biosmap, int nr_map)
  372. {
  373. /* Only one memory region (or negative)? Ignore it */
  374. if (nr_map < 2)
  375. return -1;
  376. return __append_e820_map(biosmap, nr_map);
  377. }
  378. static u64 __init __e820_update_range(struct e820map *e820x, u64 start,
  379. u64 size, unsigned old_type,
  380. unsigned new_type)
  381. {
  382. u64 end;
  383. unsigned int i;
  384. u64 real_updated_size = 0;
  385. BUG_ON(old_type == new_type);
  386. if (size > (ULLONG_MAX - start))
  387. size = ULLONG_MAX - start;
  388. end = start + size;
  389. printk(KERN_DEBUG "e820 update range: %016Lx - %016Lx ",
  390. (unsigned long long) start,
  391. (unsigned long long) end);
  392. e820_print_type(old_type);
  393. printk(KERN_CONT " ==> ");
  394. e820_print_type(new_type);
  395. printk(KERN_CONT "\n");
  396. for (i = 0; i < e820x->nr_map; i++) {
  397. struct e820entry *ei = &e820x->map[i];
  398. u64 final_start, final_end;
  399. u64 ei_end;
  400. if (ei->type != old_type)
  401. continue;
  402. ei_end = ei->addr + ei->size;
  403. /* totally covered by new range? */
  404. if (ei->addr >= start && ei_end <= end) {
  405. ei->type = new_type;
  406. real_updated_size += ei->size;
  407. continue;
  408. }
  409. /* new range is totally covered? */
  410. if (ei->addr < start && ei_end > end) {
  411. __e820_add_region(e820x, start, size, new_type);
  412. __e820_add_region(e820x, end, ei_end - end, ei->type);
  413. ei->size = start - ei->addr;
  414. real_updated_size += size;
  415. continue;
  416. }
  417. /* partially covered */
  418. final_start = max(start, ei->addr);
  419. final_end = min(end, ei_end);
  420. if (final_start >= final_end)
  421. continue;
  422. __e820_add_region(e820x, final_start, final_end - final_start,
  423. new_type);
  424. real_updated_size += final_end - final_start;
  425. /*
  426. * left range could be head or tail, so need to update
  427. * size at first.
  428. */
  429. ei->size -= final_end - final_start;
  430. if (ei->addr < final_start)
  431. continue;
  432. ei->addr = final_end;
  433. }
  434. return real_updated_size;
  435. }
  436. u64 __init e820_update_range(u64 start, u64 size, unsigned old_type,
  437. unsigned new_type)
  438. {
  439. return __e820_update_range(&e820, start, size, old_type, new_type);
  440. }
  441. static u64 __init e820_update_range_saved(u64 start, u64 size,
  442. unsigned old_type, unsigned new_type)
  443. {
  444. return __e820_update_range(&e820_saved, start, size, old_type,
  445. new_type);
  446. }
  447. /* make e820 not cover the range */
  448. u64 __init e820_remove_range(u64 start, u64 size, unsigned old_type,
  449. int checktype)
  450. {
  451. int i;
  452. u64 end;
  453. u64 real_removed_size = 0;
  454. if (size > (ULLONG_MAX - start))
  455. size = ULLONG_MAX - start;
  456. end = start + size;
  457. printk(KERN_DEBUG "e820 remove range: %016Lx - %016Lx ",
  458. (unsigned long long) start,
  459. (unsigned long long) end);
  460. if (checktype)
  461. e820_print_type(old_type);
  462. printk(KERN_CONT "\n");
  463. for (i = 0; i < e820.nr_map; i++) {
  464. struct e820entry *ei = &e820.map[i];
  465. u64 final_start, final_end;
  466. u64 ei_end;
  467. if (checktype && ei->type != old_type)
  468. continue;
  469. ei_end = ei->addr + ei->size;
  470. /* totally covered? */
  471. if (ei->addr >= start && ei_end <= end) {
  472. real_removed_size += ei->size;
  473. memset(ei, 0, sizeof(struct e820entry));
  474. continue;
  475. }
  476. /* new range is totally covered? */
  477. if (ei->addr < start && ei_end > end) {
  478. e820_add_region(end, ei_end - end, ei->type);
  479. ei->size = start - ei->addr;
  480. real_removed_size += size;
  481. continue;
  482. }
  483. /* partially covered */
  484. final_start = max(start, ei->addr);
  485. final_end = min(end, ei_end);
  486. if (final_start >= final_end)
  487. continue;
  488. real_removed_size += final_end - final_start;
  489. /*
  490. * left range could be head or tail, so need to update
  491. * size at first.
  492. */
  493. ei->size -= final_end - final_start;
  494. if (ei->addr < final_start)
  495. continue;
  496. ei->addr = final_end;
  497. }
  498. return real_removed_size;
  499. }
  500. void __init update_e820(void)
  501. {
  502. u32 nr_map;
  503. nr_map = e820.nr_map;
  504. if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr_map))
  505. return;
  506. e820.nr_map = nr_map;
  507. printk(KERN_INFO "modified physical RAM map:\n");
  508. e820_print_map("modified");
  509. }
  510. static void __init update_e820_saved(void)
  511. {
  512. u32 nr_map;
  513. nr_map = e820_saved.nr_map;
  514. if (sanitize_e820_map(e820_saved.map, ARRAY_SIZE(e820_saved.map), &nr_map))
  515. return;
  516. e820_saved.nr_map = nr_map;
  517. }
  518. #define MAX_GAP_END 0x100000000ull
  519. /*
  520. * Search for a gap in the e820 memory space from start_addr to end_addr.
  521. */
  522. __init int e820_search_gap(unsigned long *gapstart, unsigned long *gapsize,
  523. unsigned long start_addr, unsigned long long end_addr)
  524. {
  525. unsigned long long last;
  526. int i = e820.nr_map;
  527. int found = 0;
  528. last = (end_addr && end_addr < MAX_GAP_END) ? end_addr : MAX_GAP_END;
  529. while (--i >= 0) {
  530. unsigned long long start = e820.map[i].addr;
  531. unsigned long long end = start + e820.map[i].size;
  532. if (end < start_addr)
  533. continue;
  534. /*
  535. * Since "last" is at most 4GB, we know we'll
  536. * fit in 32 bits if this condition is true
  537. */
  538. if (last > end) {
  539. unsigned long gap = last - end;
  540. if (gap >= *gapsize) {
  541. *gapsize = gap;
  542. *gapstart = end;
  543. found = 1;
  544. }
  545. }
  546. if (start < last)
  547. last = start;
  548. }
  549. return found;
  550. }
  551. /*
  552. * Search for the biggest gap in the low 32 bits of the e820
  553. * memory space. We pass this space to PCI to assign MMIO resources
  554. * for hotplug or unconfigured devices in.
  555. * Hopefully the BIOS let enough space left.
  556. */
  557. __init void e820_setup_gap(void)
  558. {
  559. unsigned long gapstart, gapsize;
  560. int found;
  561. gapstart = 0x10000000;
  562. gapsize = 0x400000;
  563. found = e820_search_gap(&gapstart, &gapsize, 0, MAX_GAP_END);
  564. #ifdef CONFIG_X86_64
  565. if (!found) {
  566. gapstart = (max_pfn << PAGE_SHIFT) + 1024*1024;
  567. printk(KERN_ERR
  568. "PCI: Warning: Cannot find a gap in the 32bit address range\n"
  569. "PCI: Unassigned devices with 32bit resource registers may break!\n");
  570. }
  571. #endif
  572. /*
  573. * e820_reserve_resources_late protect stolen RAM already
  574. */
  575. pci_mem_start = gapstart;
  576. printk(KERN_INFO
  577. "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
  578. pci_mem_start, gapstart, gapsize);
  579. }
  580. /**
  581. * Because of the size limitation of struct boot_params, only first
  582. * 128 E820 memory entries are passed to kernel via
  583. * boot_params.e820_map, others are passed via SETUP_E820_EXT node of
  584. * linked list of struct setup_data, which is parsed here.
  585. */
  586. void __init parse_e820_ext(struct setup_data *sdata)
  587. {
  588. int entries;
  589. struct e820entry *extmap;
  590. entries = sdata->len / sizeof(struct e820entry);
  591. extmap = (struct e820entry *)(sdata->data);
  592. __append_e820_map(extmap, entries);
  593. sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
  594. printk(KERN_INFO "extended physical RAM map:\n");
  595. e820_print_map("extended");
  596. }
  597. #if defined(CONFIG_X86_64) || \
  598. (defined(CONFIG_X86_32) && defined(CONFIG_HIBERNATION))
  599. /**
  600. * Find the ranges of physical addresses that do not correspond to
  601. * e820 RAM areas and mark the corresponding pages as nosave for
  602. * hibernation (32 bit) or software suspend and suspend to RAM (64 bit).
  603. *
  604. * This function requires the e820 map to be sorted and without any
  605. * overlapping entries and assumes the first e820 area to be RAM.
  606. */
  607. void __init e820_mark_nosave_regions(unsigned long limit_pfn)
  608. {
  609. int i;
  610. unsigned long pfn;
  611. pfn = PFN_DOWN(e820.map[0].addr + e820.map[0].size);
  612. for (i = 1; i < e820.nr_map; i++) {
  613. struct e820entry *ei = &e820.map[i];
  614. if (pfn < PFN_UP(ei->addr))
  615. register_nosave_region(pfn, PFN_UP(ei->addr));
  616. pfn = PFN_DOWN(ei->addr + ei->size);
  617. if (ei->type != E820_RAM && ei->type != E820_RESERVED_KERN)
  618. register_nosave_region(PFN_UP(ei->addr), pfn);
  619. if (pfn >= limit_pfn)
  620. break;
  621. }
  622. }
  623. #endif
  624. #ifdef CONFIG_ACPI
  625. /**
  626. * Mark ACPI NVS memory region, so that we can save/restore it during
  627. * hibernation and the subsequent resume.
  628. */
  629. static int __init e820_mark_nvs_memory(void)
  630. {
  631. int i;
  632. for (i = 0; i < e820.nr_map; i++) {
  633. struct e820entry *ei = &e820.map[i];
  634. if (ei->type == E820_NVS)
  635. acpi_nvs_register(ei->addr, ei->size);
  636. }
  637. return 0;
  638. }
  639. core_initcall(e820_mark_nvs_memory);
  640. #endif
  641. /*
  642. * pre allocated 4k and reserved it in memblock and e820_saved
  643. */
  644. u64 __init early_reserve_e820(u64 size, u64 align)
  645. {
  646. u64 addr;
  647. addr = __memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
  648. if (addr) {
  649. e820_update_range_saved(addr, size, E820_RAM, E820_RESERVED);
  650. printk(KERN_INFO "update e820_saved for early_reserve_e820\n");
  651. update_e820_saved();
  652. }
  653. return addr;
  654. }
  655. #ifdef CONFIG_X86_32
  656. # ifdef CONFIG_X86_PAE
  657. # define MAX_ARCH_PFN (1ULL<<(36-PAGE_SHIFT))
  658. # else
  659. # define MAX_ARCH_PFN (1ULL<<(32-PAGE_SHIFT))
  660. # endif
  661. #else /* CONFIG_X86_32 */
  662. # define MAX_ARCH_PFN MAXMEM>>PAGE_SHIFT
  663. #endif
  664. /*
  665. * Find the highest page frame number we have available
  666. */
  667. static unsigned long __init e820_end_pfn(unsigned long limit_pfn, unsigned type)
  668. {
  669. int i;
  670. unsigned long last_pfn = 0;
  671. unsigned long max_arch_pfn = MAX_ARCH_PFN;
  672. for (i = 0; i < e820.nr_map; i++) {
  673. struct e820entry *ei = &e820.map[i];
  674. unsigned long start_pfn;
  675. unsigned long end_pfn;
  676. if (ei->type != type)
  677. continue;
  678. start_pfn = ei->addr >> PAGE_SHIFT;
  679. end_pfn = (ei->addr + ei->size) >> PAGE_SHIFT;
  680. if (start_pfn >= limit_pfn)
  681. continue;
  682. if (end_pfn > limit_pfn) {
  683. last_pfn = limit_pfn;
  684. break;
  685. }
  686. if (end_pfn > last_pfn)
  687. last_pfn = end_pfn;
  688. }
  689. if (last_pfn > max_arch_pfn)
  690. last_pfn = max_arch_pfn;
  691. printk(KERN_INFO "last_pfn = %#lx max_arch_pfn = %#lx\n",
  692. last_pfn, max_arch_pfn);
  693. return last_pfn;
  694. }
  695. unsigned long __init e820_end_of_ram_pfn(void)
  696. {
  697. return e820_end_pfn(MAX_ARCH_PFN, E820_RAM);
  698. }
  699. unsigned long __init e820_end_of_low_ram_pfn(void)
  700. {
  701. return e820_end_pfn(1UL<<(32 - PAGE_SHIFT), E820_RAM);
  702. }
  703. static void early_panic(char *msg)
  704. {
  705. early_printk(msg);
  706. panic(msg);
  707. }
  708. static int userdef __initdata;
  709. /* "mem=nopentium" disables the 4MB page tables. */
  710. static int __init parse_memopt(char *p)
  711. {
  712. u64 mem_size;
  713. if (!p)
  714. return -EINVAL;
  715. if (!strcmp(p, "nopentium")) {
  716. #ifdef CONFIG_X86_32
  717. setup_clear_cpu_cap(X86_FEATURE_PSE);
  718. return 0;
  719. #else
  720. printk(KERN_WARNING "mem=nopentium ignored! (only supported on x86_32)\n");
  721. return -EINVAL;
  722. #endif
  723. }
  724. userdef = 1;
  725. mem_size = memparse(p, &p);
  726. /* don't remove all of memory when handling "mem={invalid}" param */
  727. if (mem_size == 0)
  728. return -EINVAL;
  729. e820_remove_range(mem_size, ULLONG_MAX - mem_size, E820_RAM, 1);
  730. return 0;
  731. }
  732. early_param("mem", parse_memopt);
  733. static int __init parse_memmap_opt(char *p)
  734. {
  735. char *oldp;
  736. u64 start_at, mem_size;
  737. if (!p)
  738. return -EINVAL;
  739. if (!strncmp(p, "exactmap", 8)) {
  740. #ifdef CONFIG_CRASH_DUMP
  741. /*
  742. * If we are doing a crash dump, we still need to know
  743. * the real mem size before original memory map is
  744. * reset.
  745. */
  746. saved_max_pfn = e820_end_of_ram_pfn();
  747. #endif
  748. e820.nr_map = 0;
  749. userdef = 1;
  750. return 0;
  751. }
  752. oldp = p;
  753. mem_size = memparse(p, &p);
  754. if (p == oldp)
  755. return -EINVAL;
  756. userdef = 1;
  757. if (*p == '@') {
  758. start_at = memparse(p+1, &p);
  759. e820_add_region(start_at, mem_size, E820_RAM);
  760. } else if (*p == '#') {
  761. start_at = memparse(p+1, &p);
  762. e820_add_region(start_at, mem_size, E820_ACPI);
  763. } else if (*p == '$') {
  764. start_at = memparse(p+1, &p);
  765. e820_add_region(start_at, mem_size, E820_RESERVED);
  766. } else
  767. e820_remove_range(mem_size, ULLONG_MAX - mem_size, E820_RAM, 1);
  768. return *p == '\0' ? 0 : -EINVAL;
  769. }
  770. early_param("memmap", parse_memmap_opt);
  771. void __init finish_e820_parsing(void)
  772. {
  773. if (userdef) {
  774. u32 nr = e820.nr_map;
  775. if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr) < 0)
  776. early_panic("Invalid user supplied memory map");
  777. e820.nr_map = nr;
  778. printk(KERN_INFO "user-defined physical RAM map:\n");
  779. e820_print_map("user");
  780. }
  781. }
  782. static inline const char *e820_type_to_string(int e820_type)
  783. {
  784. switch (e820_type) {
  785. case E820_RESERVED_KERN:
  786. case E820_RAM: return "System RAM";
  787. case E820_ACPI: return "ACPI Tables";
  788. case E820_NVS: return "ACPI Non-volatile Storage";
  789. case E820_UNUSABLE: return "Unusable memory";
  790. default: return "reserved";
  791. }
  792. }
  793. /*
  794. * Mark e820 reserved areas as busy for the resource manager.
  795. */
  796. static struct resource __initdata *e820_res;
  797. void __init e820_reserve_resources(void)
  798. {
  799. int i;
  800. struct resource *res;
  801. u64 end;
  802. res = alloc_bootmem(sizeof(struct resource) * e820.nr_map);
  803. e820_res = res;
  804. for (i = 0; i < e820.nr_map; i++) {
  805. end = e820.map[i].addr + e820.map[i].size - 1;
  806. if (end != (resource_size_t)end) {
  807. res++;
  808. continue;
  809. }
  810. res->name = e820_type_to_string(e820.map[i].type);
  811. res->start = e820.map[i].addr;
  812. res->end = end;
  813. res->flags = IORESOURCE_MEM;
  814. /*
  815. * don't register the region that could be conflicted with
  816. * pci device BAR resource and insert them later in
  817. * pcibios_resource_survey()
  818. */
  819. if (e820.map[i].type != E820_RESERVED || res->start < (1ULL<<20)) {
  820. res->flags |= IORESOURCE_BUSY;
  821. insert_resource(&iomem_resource, res);
  822. }
  823. res++;
  824. }
  825. for (i = 0; i < e820_saved.nr_map; i++) {
  826. struct e820entry *entry = &e820_saved.map[i];
  827. firmware_map_add_early(entry->addr,
  828. entry->addr + entry->size - 1,
  829. e820_type_to_string(entry->type));
  830. }
  831. }
  832. /* How much should we pad RAM ending depending on where it is? */
  833. static unsigned long ram_alignment(resource_size_t pos)
  834. {
  835. unsigned long mb = pos >> 20;
  836. /* To 64kB in the first megabyte */
  837. if (!mb)
  838. return 64*1024;
  839. /* To 1MB in the first 16MB */
  840. if (mb < 16)
  841. return 1024*1024;
  842. /* To 64MB for anything above that */
  843. return 64*1024*1024;
  844. }
  845. #define MAX_RESOURCE_SIZE ((resource_size_t)-1)
  846. void __init e820_reserve_resources_late(void)
  847. {
  848. int i;
  849. struct resource *res;
  850. res = e820_res;
  851. for (i = 0; i < e820.nr_map; i++) {
  852. if (!res->parent && res->end)
  853. insert_resource_expand_to_fit(&iomem_resource, res);
  854. res++;
  855. }
  856. /*
  857. * Try to bump up RAM regions to reasonable boundaries to
  858. * avoid stolen RAM:
  859. */
  860. for (i = 0; i < e820.nr_map; i++) {
  861. struct e820entry *entry = &e820.map[i];
  862. u64 start, end;
  863. if (entry->type != E820_RAM)
  864. continue;
  865. start = entry->addr + entry->size;
  866. end = round_up(start, ram_alignment(start)) - 1;
  867. if (end > MAX_RESOURCE_SIZE)
  868. end = MAX_RESOURCE_SIZE;
  869. if (start >= end)
  870. continue;
  871. printk(KERN_DEBUG "reserve RAM buffer: %016llx - %016llx ",
  872. start, end);
  873. reserve_region_with_split(&iomem_resource, start, end,
  874. "RAM buffer");
  875. }
  876. }
  877. char *__init default_machine_specific_memory_setup(void)
  878. {
  879. char *who = "BIOS-e820";
  880. u32 new_nr;
  881. /*
  882. * Try to copy the BIOS-supplied E820-map.
  883. *
  884. * Otherwise fake a memory map; one section from 0k->640k,
  885. * the next section from 1mb->appropriate_mem_k
  886. */
  887. new_nr = boot_params.e820_entries;
  888. sanitize_e820_map(boot_params.e820_map,
  889. ARRAY_SIZE(boot_params.e820_map),
  890. &new_nr);
  891. boot_params.e820_entries = new_nr;
  892. if (append_e820_map(boot_params.e820_map, boot_params.e820_entries)
  893. < 0) {
  894. u64 mem_size;
  895. /* compare results from other methods and take the greater */
  896. if (boot_params.alt_mem_k
  897. < boot_params.screen_info.ext_mem_k) {
  898. mem_size = boot_params.screen_info.ext_mem_k;
  899. who = "BIOS-88";
  900. } else {
  901. mem_size = boot_params.alt_mem_k;
  902. who = "BIOS-e801";
  903. }
  904. e820.nr_map = 0;
  905. e820_add_region(0, LOWMEMSIZE(), E820_RAM);
  906. e820_add_region(HIGH_MEMORY, mem_size << 10, E820_RAM);
  907. }
  908. /* In case someone cares... */
  909. return who;
  910. }
  911. void __init setup_memory_map(void)
  912. {
  913. char *who;
  914. who = x86_init.resources.memory_setup();
  915. memcpy(&e820_saved, &e820, sizeof(struct e820map));
  916. printk(KERN_INFO "BIOS-provided physical RAM map:\n");
  917. e820_print_map(who);
  918. }
  919. void __init memblock_x86_fill(void)
  920. {
  921. int i;
  922. u64 end;
  923. /*
  924. * EFI may have more than 128 entries
  925. * We are safe to enable resizing, beause memblock_x86_fill()
  926. * is rather later for x86
  927. */
  928. memblock_allow_resize();
  929. for (i = 0; i < e820.nr_map; i++) {
  930. struct e820entry *ei = &e820.map[i];
  931. end = ei->addr + ei->size;
  932. if (end != (resource_size_t)end)
  933. continue;
  934. if (ei->type != E820_RAM && ei->type != E820_RESERVED_KERN)
  935. continue;
  936. memblock_add(ei->addr, ei->size);
  937. }
  938. /* throw away partial pages */
  939. memblock_trim_memory(PAGE_SIZE);
  940. memblock_dump_all();
  941. }
  942. void __init memblock_find_dma_reserve(void)
  943. {
  944. #ifdef CONFIG_X86_64
  945. u64 nr_pages = 0, nr_free_pages = 0;
  946. unsigned long start_pfn, end_pfn;
  947. phys_addr_t start, end;
  948. int i;
  949. u64 u;
  950. /*
  951. * need to find out used area below MAX_DMA_PFN
  952. * need to use memblock to get free size in [0, MAX_DMA_PFN]
  953. * at first, and assume boot_mem will not take below MAX_DMA_PFN
  954. */
  955. for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, NULL) {
  956. start_pfn = min_t(unsigned long, start_pfn, MAX_DMA_PFN);
  957. end_pfn = min_t(unsigned long, end_pfn, MAX_DMA_PFN);
  958. nr_pages += end_pfn - start_pfn;
  959. }
  960. for_each_free_mem_range(u, MAX_NUMNODES, &start, &end, NULL) {
  961. start_pfn = min_t(unsigned long, PFN_UP(start), MAX_DMA_PFN);
  962. end_pfn = min_t(unsigned long, PFN_DOWN(end), MAX_DMA_PFN);
  963. if (start_pfn < end_pfn)
  964. nr_free_pages += end_pfn - start_pfn;
  965. }
  966. set_dma_reserve(nr_pages - nr_free_pages);
  967. #endif
  968. }