e820.c 29 KB

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