e820.c 29 KB

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