e820.c 28 KB

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