e820.c 28 KB

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