e820.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  1. /*
  2. * Low level x86 E820 memory map handling functions.
  3. *
  4. * The firmware and bootloader passes us the "E820 table", which is the primary
  5. * physical memory layout description available about x86 systems.
  6. *
  7. * The kernel takes the E820 memory layout and optionally modifies it with
  8. * quirks and other tweaks, and feeds that into the generic Linux memory
  9. * allocation code routines via a platform independent interface (memblock, etc.).
  10. */
  11. #include <linux/crash_dump.h>
  12. #include <linux/bootmem.h>
  13. #include <linux/suspend.h>
  14. #include <linux/acpi.h>
  15. #include <linux/firmware-map.h>
  16. #include <linux/memblock.h>
  17. #include <linux/sort.h>
  18. #include <asm/e820/api.h>
  19. #include <asm/setup.h>
  20. /*
  21. * We organize the E820 table into two main data structures:
  22. *
  23. * - 'e820_table_firmware': the original firmware version passed to us by the
  24. * bootloader - not modified by the kernel. We use this to:
  25. *
  26. * - inform the user about the firmware's notion of memory layout
  27. * via /sys/firmware/memmap
  28. *
  29. * - the hibernation code uses it to generate a kernel-independent MD5
  30. * fingerprint of the physical memory layout of a system.
  31. *
  32. * - kexec, which is a bootloader in disguise, uses the original E820
  33. * layout to pass to the kexec-ed kernel. This way the original kernel
  34. * can have a restricted E820 map while the kexec()-ed kexec-kernel
  35. * can have access to full memory - etc.
  36. *
  37. * - 'e820_table': this is the main E820 table that is massaged by the
  38. * low level x86 platform code, or modified by boot parameters, before
  39. * passed on to higher level MM layers.
  40. *
  41. * Once the E820 map has been converted to the standard Linux memory layout
  42. * information its role stops - modifying it has no effect and does not get
  43. * re-propagated. So itsmain role is a temporary bootstrap storage of firmware
  44. * specific memory layout data during early bootup.
  45. */
  46. static struct e820_table e820_table_init __initdata;
  47. static struct e820_table e820_table_firmware_init __initdata;
  48. struct e820_table *e820_table __refdata = &e820_table_init;
  49. struct e820_table *e820_table_firmware __refdata = &e820_table_firmware_init;
  50. /* For PCI or other memory-mapped resources */
  51. unsigned long pci_mem_start = 0xaeedbabe;
  52. #ifdef CONFIG_PCI
  53. EXPORT_SYMBOL(pci_mem_start);
  54. #endif
  55. /*
  56. * This function checks if any part of the range <start,end> is mapped
  57. * with type.
  58. */
  59. bool e820__mapped_any(u64 start, u64 end, enum e820_type type)
  60. {
  61. int i;
  62. for (i = 0; i < e820_table->nr_entries; i++) {
  63. struct e820_entry *entry = &e820_table->entries[i];
  64. if (type && entry->type != type)
  65. continue;
  66. if (entry->addr >= end || entry->addr + entry->size <= start)
  67. continue;
  68. return 1;
  69. }
  70. return 0;
  71. }
  72. EXPORT_SYMBOL_GPL(e820__mapped_any);
  73. /*
  74. * This function checks if the entire <start,end> range is mapped with 'type'.
  75. *
  76. * Note: this function only works correctly once the E820 table is sorted and
  77. * not-overlapping (at least for the range specified), which is the case normally.
  78. */
  79. bool __init e820__mapped_all(u64 start, u64 end, enum e820_type type)
  80. {
  81. int i;
  82. for (i = 0; i < e820_table->nr_entries; i++) {
  83. struct e820_entry *entry = &e820_table->entries[i];
  84. if (type && entry->type != type)
  85. continue;
  86. /* Is the region (part) in overlap with the current region? */
  87. if (entry->addr >= end || entry->addr + entry->size <= start)
  88. continue;
  89. /*
  90. * If the region is at the beginning of <start,end> we move
  91. * 'start' to the end of the region since it's ok until there
  92. */
  93. if (entry->addr <= start)
  94. start = entry->addr + entry->size;
  95. /*
  96. * If 'start' is now at or beyond 'end', we're done, full
  97. * coverage of the desired range exists:
  98. */
  99. if (start >= end)
  100. return 1;
  101. }
  102. return 0;
  103. }
  104. /*
  105. * Add a memory region to the kernel E820 map.
  106. */
  107. static void __init __e820__range_add(struct e820_table *table, u64 start, u64 size, enum e820_type type)
  108. {
  109. int x = table->nr_entries;
  110. if (x >= ARRAY_SIZE(table->entries)) {
  111. pr_err("e820: too many entries; ignoring [mem %#010llx-%#010llx]\n", start, start + size - 1);
  112. return;
  113. }
  114. table->entries[x].addr = start;
  115. table->entries[x].size = size;
  116. table->entries[x].type = type;
  117. table->nr_entries++;
  118. }
  119. void __init e820__range_add(u64 start, u64 size, enum e820_type type)
  120. {
  121. __e820__range_add(e820_table, start, size, type);
  122. }
  123. static void __init e820_print_type(enum e820_type type)
  124. {
  125. switch (type) {
  126. case E820_TYPE_RAM: /* Fall through: */
  127. case E820_TYPE_RESERVED_KERN: pr_cont("usable"); break;
  128. case E820_TYPE_RESERVED: pr_cont("reserved"); break;
  129. case E820_TYPE_ACPI: pr_cont("ACPI data"); break;
  130. case E820_TYPE_NVS: pr_cont("ACPI NVS"); break;
  131. case E820_TYPE_UNUSABLE: pr_cont("unusable"); break;
  132. case E820_TYPE_PMEM: /* Fall through: */
  133. case E820_TYPE_PRAM: pr_cont("persistent (type %u)", type); break;
  134. default: pr_cont("type %u", type); break;
  135. }
  136. }
  137. void __init e820__print_table(char *who)
  138. {
  139. int i;
  140. for (i = 0; i < e820_table->nr_entries; i++) {
  141. pr_info("%s: [mem %#018Lx-%#018Lx] ", who,
  142. e820_table->entries[i].addr,
  143. e820_table->entries[i].addr + e820_table->entries[i].size - 1);
  144. e820_print_type(e820_table->entries[i].type);
  145. pr_cont("\n");
  146. }
  147. }
  148. /*
  149. * Sanitize an E820 map.
  150. *
  151. * Some E820 layouts 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 'entries' points to an array of 'struct
  157. * e820_entry' which on entry has elements in the range [0, *nr_entries)
  158. * valid, and which has space for up to max_nr_entries entries.
  159. * On return, the resulting sanitized E820 map entries will be in
  160. * overwritten in the same location, starting at 'entries'.
  161. *
  162. * The integer pointed to by nr_entries must be valid on entry (the
  163. * current number of valid entries located at 'entries'). If the
  164. * sanitizing succeeds the *nr_entries will be updated with the new
  165. * number of valid entries (something no more than max_nr_entries).
  166. *
  167. * The return value from e820__update_table() 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. struct change_member {
  210. /* Pointer to the original entry: */
  211. struct e820_entry *entry;
  212. /* Address for this change point: */
  213. unsigned long long addr;
  214. };
  215. static struct change_member change_point_list[2*E820_MAX_ENTRIES] __initdata;
  216. static struct change_member *change_point[2*E820_MAX_ENTRIES] __initdata;
  217. static struct e820_entry *overlap_list[E820_MAX_ENTRIES] __initdata;
  218. static struct e820_entry new_entries[E820_MAX_ENTRIES] __initdata;
  219. static int __init cpcompare(const void *a, const void *b)
  220. {
  221. struct change_member * const *app = a, * const *bpp = b;
  222. const struct change_member *ap = *app, *bp = *bpp;
  223. /*
  224. * Inputs are pointers to two elements of change_point[]. If their
  225. * addresses are not equal, their difference dominates. If the addresses
  226. * are equal, then consider one that represents the end of its region
  227. * to be greater than one that does not.
  228. */
  229. if (ap->addr != bp->addr)
  230. return ap->addr > bp->addr ? 1 : -1;
  231. return (ap->addr != ap->entry->addr) - (bp->addr != bp->entry->addr);
  232. }
  233. int __init e820__update_table(struct e820_table *table)
  234. {
  235. struct e820_entry *entries = table->entries;
  236. u32 max_nr_entries = ARRAY_SIZE(table->entries);
  237. enum e820_type current_type, last_type;
  238. unsigned long long last_addr;
  239. u32 new_nr_entries, overlap_entries;
  240. u32 i, chg_idx, chg_nr;
  241. /* If there's only one memory region, don't bother: */
  242. if (table->nr_entries < 2)
  243. return -1;
  244. BUG_ON(table->nr_entries > max_nr_entries);
  245. /* Bail out if we find any unreasonable addresses in the map: */
  246. for (i = 0; i < table->nr_entries; i++) {
  247. if (entries[i].addr + entries[i].size < entries[i].addr)
  248. return -1;
  249. }
  250. /* Create pointers for initial change-point information (for sorting): */
  251. for (i = 0; i < 2 * table->nr_entries; i++)
  252. change_point[i] = &change_point_list[i];
  253. /*
  254. * Record all known change-points (starting and ending addresses),
  255. * omitting empty memory regions:
  256. */
  257. chg_idx = 0;
  258. for (i = 0; i < table->nr_entries; i++) {
  259. if (entries[i].size != 0) {
  260. change_point[chg_idx]->addr = entries[i].addr;
  261. change_point[chg_idx++]->entry = &entries[i];
  262. change_point[chg_idx]->addr = entries[i].addr + entries[i].size;
  263. change_point[chg_idx++]->entry = &entries[i];
  264. }
  265. }
  266. chg_nr = chg_idx;
  267. /* Sort change-point list by memory addresses (low -> high): */
  268. sort(change_point, chg_nr, sizeof(*change_point), cpcompare, NULL);
  269. /* Create a new memory map, removing overlaps: */
  270. overlap_entries = 0; /* Number of entries in the overlap table */
  271. new_nr_entries = 0; /* Index for creating new map entries */
  272. last_type = 0; /* Start with undefined memory type */
  273. last_addr = 0; /* Start with 0 as last starting address */
  274. /* Loop through change-points, determining effect on the new map: */
  275. for (chg_idx = 0; chg_idx < chg_nr; chg_idx++) {
  276. /* Keep track of all overlapping entries */
  277. if (change_point[chg_idx]->addr == change_point[chg_idx]->entry->addr) {
  278. /* Add map entry to overlap list (> 1 entry implies an overlap) */
  279. overlap_list[overlap_entries++] = change_point[chg_idx]->entry;
  280. } else {
  281. /* Remove entry from list (order independent, so swap with last): */
  282. for (i = 0; i < overlap_entries; i++) {
  283. if (overlap_list[i] == change_point[chg_idx]->entry)
  284. overlap_list[i] = overlap_list[overlap_entries-1];
  285. }
  286. overlap_entries--;
  287. }
  288. /*
  289. * If there are overlapping entries, decide which
  290. * "type" to use (larger value takes precedence --
  291. * 1=usable, 2,3,4,4+=unusable)
  292. */
  293. current_type = 0;
  294. for (i = 0; i < overlap_entries; i++) {
  295. if (overlap_list[i]->type > current_type)
  296. current_type = overlap_list[i]->type;
  297. }
  298. /* Continue building up new map based on this information: */
  299. if (current_type != last_type || current_type == E820_TYPE_PRAM) {
  300. if (last_type != 0) {
  301. new_entries[new_nr_entries].size = change_point[chg_idx]->addr - last_addr;
  302. /* Move forward only if the new size was non-zero: */
  303. if (new_entries[new_nr_entries].size != 0)
  304. /* No more space left for new entries? */
  305. if (++new_nr_entries >= max_nr_entries)
  306. break;
  307. }
  308. if (current_type != 0) {
  309. new_entries[new_nr_entries].addr = change_point[chg_idx]->addr;
  310. new_entries[new_nr_entries].type = current_type;
  311. last_addr = change_point[chg_idx]->addr;
  312. }
  313. last_type = current_type;
  314. }
  315. }
  316. /* Copy the new entries into the original location: */
  317. memcpy(entries, new_entries, new_nr_entries*sizeof(*entries));
  318. table->nr_entries = new_nr_entries;
  319. return 0;
  320. }
  321. static int __init __append_e820_table(struct boot_e820_entry *entries, u32 nr_entries)
  322. {
  323. struct boot_e820_entry *entry = entries;
  324. while (nr_entries) {
  325. u64 start = entry->addr;
  326. u64 size = entry->size;
  327. u64 end = start + size - 1;
  328. u32 type = entry->type;
  329. /* Ignore the entry on 64-bit overflow: */
  330. if (start > end && likely(size))
  331. return -1;
  332. e820__range_add(start, size, type);
  333. entry++;
  334. nr_entries--;
  335. }
  336. return 0;
  337. }
  338. /*
  339. * Copy the BIOS E820 map into a safe place.
  340. *
  341. * Sanity-check it while we're at it..
  342. *
  343. * If we're lucky and live on a modern system, the setup code
  344. * will have given us a memory map that we can use to properly
  345. * set up memory. If we aren't, we'll fake a memory map.
  346. */
  347. static int __init append_e820_table(struct boot_e820_entry *entries, u32 nr_entries)
  348. {
  349. /* Only one memory region (or negative)? Ignore it */
  350. if (nr_entries < 2)
  351. return -1;
  352. return __append_e820_table(entries, nr_entries);
  353. }
  354. static u64 __init
  355. __e820__range_update(struct e820_table *table, u64 start, u64 size, enum e820_type old_type, enum e820_type new_type)
  356. {
  357. u64 end;
  358. unsigned int i;
  359. u64 real_updated_size = 0;
  360. BUG_ON(old_type == new_type);
  361. if (size > (ULLONG_MAX - start))
  362. size = ULLONG_MAX - start;
  363. end = start + size;
  364. printk(KERN_DEBUG "e820: update [mem %#010Lx-%#010Lx] ", start, end - 1);
  365. e820_print_type(old_type);
  366. pr_cont(" ==> ");
  367. e820_print_type(new_type);
  368. pr_cont("\n");
  369. for (i = 0; i < table->nr_entries; i++) {
  370. struct e820_entry *entry = &table->entries[i];
  371. u64 final_start, final_end;
  372. u64 entry_end;
  373. if (entry->type != old_type)
  374. continue;
  375. entry_end = entry->addr + entry->size;
  376. /* Completely covered by new range? */
  377. if (entry->addr >= start && entry_end <= end) {
  378. entry->type = new_type;
  379. real_updated_size += entry->size;
  380. continue;
  381. }
  382. /* New range is completely covered? */
  383. if (entry->addr < start && entry_end > end) {
  384. __e820__range_add(table, start, size, new_type);
  385. __e820__range_add(table, end, entry_end - end, entry->type);
  386. entry->size = start - entry->addr;
  387. real_updated_size += size;
  388. continue;
  389. }
  390. /* Partially covered: */
  391. final_start = max(start, entry->addr);
  392. final_end = min(end, entry_end);
  393. if (final_start >= final_end)
  394. continue;
  395. __e820__range_add(table, final_start, final_end - final_start, new_type);
  396. real_updated_size += final_end - final_start;
  397. /*
  398. * Left range could be head or tail, so need to update
  399. * its size first:
  400. */
  401. entry->size -= final_end - final_start;
  402. if (entry->addr < final_start)
  403. continue;
  404. entry->addr = final_end;
  405. }
  406. return real_updated_size;
  407. }
  408. u64 __init e820__range_update(u64 start, u64 size, enum e820_type old_type, enum e820_type new_type)
  409. {
  410. return __e820__range_update(e820_table, start, size, old_type, new_type);
  411. }
  412. static u64 __init e820__range_update_firmware(u64 start, u64 size, enum e820_type old_type, enum e820_type new_type)
  413. {
  414. return __e820__range_update(e820_table_firmware, start, size, old_type, new_type);
  415. }
  416. /* Remove a range of memory from the E820 table: */
  417. u64 __init e820__range_remove(u64 start, u64 size, enum e820_type old_type, bool check_type)
  418. {
  419. int i;
  420. u64 end;
  421. u64 real_removed_size = 0;
  422. if (size > (ULLONG_MAX - start))
  423. size = ULLONG_MAX - start;
  424. end = start + size;
  425. printk(KERN_DEBUG "e820: remove [mem %#010Lx-%#010Lx] ", start, end - 1);
  426. if (check_type)
  427. e820_print_type(old_type);
  428. pr_cont("\n");
  429. for (i = 0; i < e820_table->nr_entries; i++) {
  430. struct e820_entry *entry = &e820_table->entries[i];
  431. u64 final_start, final_end;
  432. u64 entry_end;
  433. if (check_type && entry->type != old_type)
  434. continue;
  435. entry_end = entry->addr + entry->size;
  436. /* Completely covered? */
  437. if (entry->addr >= start && entry_end <= end) {
  438. real_removed_size += entry->size;
  439. memset(entry, 0, sizeof(*entry));
  440. continue;
  441. }
  442. /* Is the new range completely covered? */
  443. if (entry->addr < start && entry_end > end) {
  444. e820__range_add(end, entry_end - end, entry->type);
  445. entry->size = start - entry->addr;
  446. real_removed_size += size;
  447. continue;
  448. }
  449. /* Partially covered: */
  450. final_start = max(start, entry->addr);
  451. final_end = min(end, entry_end);
  452. if (final_start >= final_end)
  453. continue;
  454. real_removed_size += final_end - final_start;
  455. /*
  456. * Left range could be head or tail, so need to update
  457. * the size first:
  458. */
  459. entry->size -= final_end - final_start;
  460. if (entry->addr < final_start)
  461. continue;
  462. entry->addr = final_end;
  463. }
  464. return real_removed_size;
  465. }
  466. void __init e820__update_table_print(void)
  467. {
  468. if (e820__update_table(e820_table))
  469. return;
  470. pr_info("e820: modified physical RAM map:\n");
  471. e820__print_table("modified");
  472. }
  473. static void __init e820__update_table_firmware(void)
  474. {
  475. e820__update_table(e820_table_firmware);
  476. }
  477. #define MAX_GAP_END 0x100000000ull
  478. /*
  479. * Search for a gap in the E820 memory space from 0 to MAX_GAP_END (4GB).
  480. */
  481. static int __init e820_search_gap(unsigned long *gapstart, unsigned long *gapsize)
  482. {
  483. unsigned long long last = MAX_GAP_END;
  484. int i = e820_table->nr_entries;
  485. int found = 0;
  486. while (--i >= 0) {
  487. unsigned long long start = e820_table->entries[i].addr;
  488. unsigned long long end = start + e820_table->entries[i].size;
  489. /*
  490. * Since "last" is at most 4GB, we know we'll
  491. * fit in 32 bits if this condition is true:
  492. */
  493. if (last > end) {
  494. unsigned long gap = last - end;
  495. if (gap >= *gapsize) {
  496. *gapsize = gap;
  497. *gapstart = end;
  498. found = 1;
  499. }
  500. }
  501. if (start < last)
  502. last = start;
  503. }
  504. return found;
  505. }
  506. /*
  507. * Search for the biggest gap in the low 32 bits of the E820
  508. * memory space. We pass this space to the PCI subsystem, so
  509. * that it can assign MMIO resources for hotplug or
  510. * unconfigured devices in.
  511. *
  512. * Hopefully the BIOS let enough space left.
  513. */
  514. __init void e820__setup_pci_gap(void)
  515. {
  516. unsigned long gapstart, gapsize;
  517. int found;
  518. gapsize = 0x400000;
  519. found = e820_search_gap(&gapstart, &gapsize);
  520. if (!found) {
  521. #ifdef CONFIG_X86_64
  522. gapstart = (max_pfn << PAGE_SHIFT) + 1024*1024;
  523. pr_err(
  524. "e820: Cannot find an available gap in the 32-bit address range\n"
  525. "e820: PCI devices with unassigned 32-bit BARs may not work!\n");
  526. #else
  527. gapstart = 0x10000000;
  528. #endif
  529. }
  530. /*
  531. * e820__reserve_resources_late() protects stolen RAM already:
  532. */
  533. pci_mem_start = gapstart;
  534. pr_info("e820: [mem %#010lx-%#010lx] available for PCI devices\n", gapstart, gapstart + gapsize - 1);
  535. }
  536. /*
  537. * Called late during init, in free_initmem().
  538. *
  539. * Initial e820_table and e820_table_firmware are largish __initdata arrays.
  540. *
  541. * Copy them to a (usually much smaller) dynamically allocated area that is
  542. * sized precisely after the number of e820 entries.
  543. *
  544. * This is done after we've performed all the fixes and tweaks to the tables.
  545. * All functions which modify them are __init functions, which won't exist
  546. * after free_initmem().
  547. */
  548. __init void e820__reallocate_tables(void)
  549. {
  550. struct e820_table *n;
  551. int size;
  552. size = offsetof(struct e820_table, entries) + sizeof(struct e820_entry)*e820_table->nr_entries;
  553. n = kmalloc(size, GFP_KERNEL);
  554. BUG_ON(!n);
  555. memcpy(n, e820_table, size);
  556. e820_table = n;
  557. size = offsetof(struct e820_table, entries) + sizeof(struct e820_entry)*e820_table_firmware->nr_entries;
  558. n = kmalloc(size, GFP_KERNEL);
  559. BUG_ON(!n);
  560. memcpy(n, e820_table_firmware, size);
  561. e820_table_firmware = n;
  562. }
  563. /*
  564. * Because of the small fixed size of struct boot_params, only the first
  565. * 128 E820 memory entries are passed to the kernel via boot_params.e820_table,
  566. * the remaining (if any) entries are passed via the SETUP_E820_EXT node of
  567. * struct setup_data, which is parsed here.
  568. */
  569. void __init e820__memory_setup_extended(u64 phys_addr, u32 data_len)
  570. {
  571. int entries;
  572. struct boot_e820_entry *extmap;
  573. struct setup_data *sdata;
  574. sdata = early_memremap(phys_addr, data_len);
  575. entries = sdata->len / sizeof(*extmap);
  576. extmap = (struct boot_e820_entry *)(sdata->data);
  577. __append_e820_table(extmap, entries);
  578. e820__update_table(e820_table);
  579. early_memunmap(sdata, data_len);
  580. pr_info("e820: extended physical RAM map:\n");
  581. e820__print_table("extended");
  582. }
  583. /*
  584. * Find the ranges of physical addresses that do not correspond to
  585. * E820 RAM areas and register the corresponding pages as 'nosave' for
  586. * hibernation (32-bit) or software suspend and suspend to RAM (64-bit).
  587. *
  588. * This function requires the E820 map to be sorted and without any
  589. * overlapping entries.
  590. */
  591. void __init e820__register_nosave_regions(unsigned long limit_pfn)
  592. {
  593. int i;
  594. unsigned long pfn = 0;
  595. for (i = 0; i < e820_table->nr_entries; i++) {
  596. struct e820_entry *entry = &e820_table->entries[i];
  597. if (pfn < PFN_UP(entry->addr))
  598. register_nosave_region(pfn, PFN_UP(entry->addr));
  599. pfn = PFN_DOWN(entry->addr + entry->size);
  600. if (entry->type != E820_TYPE_RAM && entry->type != E820_TYPE_RESERVED_KERN)
  601. register_nosave_region(PFN_UP(entry->addr), pfn);
  602. if (pfn >= limit_pfn)
  603. break;
  604. }
  605. }
  606. #ifdef CONFIG_ACPI
  607. /*
  608. * Register ACPI NVS memory regions, so that we can save/restore them during
  609. * hibernation and the subsequent resume:
  610. */
  611. static int __init e820__register_nvs_regions(void)
  612. {
  613. int i;
  614. for (i = 0; i < e820_table->nr_entries; i++) {
  615. struct e820_entry *entry = &e820_table->entries[i];
  616. if (entry->type == E820_TYPE_NVS)
  617. acpi_nvs_register(entry->addr, entry->size);
  618. }
  619. return 0;
  620. }
  621. core_initcall(e820__register_nvs_regions);
  622. #endif
  623. /*
  624. * Allocate the requested number of bytes with the requsted alignment
  625. * and return (the physical address) to the caller. Also register this
  626. * range in the 'firmware' E820 table as a reserved range.
  627. *
  628. * This allows kexec to fake a new mptable, as if it came from the real
  629. * system.
  630. */
  631. u64 __init e820__memblock_alloc_reserved(u64 size, u64 align)
  632. {
  633. u64 addr;
  634. addr = __memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
  635. if (addr) {
  636. e820__range_update_firmware(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
  637. pr_info("e820: update e820_table_firmware for e820__memblock_alloc_reserved()\n");
  638. e820__update_table_firmware();
  639. }
  640. return addr;
  641. }
  642. #ifdef CONFIG_X86_32
  643. # ifdef CONFIG_X86_PAE
  644. # define MAX_ARCH_PFN (1ULL<<(36-PAGE_SHIFT))
  645. # else
  646. # define MAX_ARCH_PFN (1ULL<<(32-PAGE_SHIFT))
  647. # endif
  648. #else /* CONFIG_X86_32 */
  649. # define MAX_ARCH_PFN MAXMEM>>PAGE_SHIFT
  650. #endif
  651. /*
  652. * Find the highest page frame number we have available
  653. */
  654. static unsigned long __init e820_end_pfn(unsigned long limit_pfn, enum e820_type type)
  655. {
  656. int i;
  657. unsigned long last_pfn = 0;
  658. unsigned long max_arch_pfn = MAX_ARCH_PFN;
  659. for (i = 0; i < e820_table->nr_entries; i++) {
  660. struct e820_entry *entry = &e820_table->entries[i];
  661. unsigned long start_pfn;
  662. unsigned long end_pfn;
  663. if (entry->type != type)
  664. continue;
  665. start_pfn = entry->addr >> PAGE_SHIFT;
  666. end_pfn = (entry->addr + entry->size) >> PAGE_SHIFT;
  667. if (start_pfn >= limit_pfn)
  668. continue;
  669. if (end_pfn > limit_pfn) {
  670. last_pfn = limit_pfn;
  671. break;
  672. }
  673. if (end_pfn > last_pfn)
  674. last_pfn = end_pfn;
  675. }
  676. if (last_pfn > max_arch_pfn)
  677. last_pfn = max_arch_pfn;
  678. pr_info("e820: last_pfn = %#lx max_arch_pfn = %#lx\n",
  679. last_pfn, max_arch_pfn);
  680. return last_pfn;
  681. }
  682. unsigned long __init e820__end_of_ram_pfn(void)
  683. {
  684. return e820_end_pfn(MAX_ARCH_PFN, E820_TYPE_RAM);
  685. }
  686. unsigned long __init e820__end_of_low_ram_pfn(void)
  687. {
  688. return e820_end_pfn(1UL << (32 - PAGE_SHIFT), E820_TYPE_RAM);
  689. }
  690. static void __init early_panic(char *msg)
  691. {
  692. early_printk(msg);
  693. panic(msg);
  694. }
  695. static int userdef __initdata;
  696. /* The "mem=nopentium" boot option disables 4MB page tables on 32-bit kernels: */
  697. static int __init parse_memopt(char *p)
  698. {
  699. u64 mem_size;
  700. if (!p)
  701. return -EINVAL;
  702. if (!strcmp(p, "nopentium")) {
  703. #ifdef CONFIG_X86_32
  704. setup_clear_cpu_cap(X86_FEATURE_PSE);
  705. return 0;
  706. #else
  707. pr_warn("mem=nopentium ignored! (only supported on x86_32)\n");
  708. return -EINVAL;
  709. #endif
  710. }
  711. userdef = 1;
  712. mem_size = memparse(p, &p);
  713. /* Don't remove all memory when getting "mem={invalid}" parameter: */
  714. if (mem_size == 0)
  715. return -EINVAL;
  716. e820__range_remove(mem_size, ULLONG_MAX - mem_size, E820_TYPE_RAM, 1);
  717. return 0;
  718. }
  719. early_param("mem", parse_memopt);
  720. static int __init parse_memmap_one(char *p)
  721. {
  722. char *oldp;
  723. u64 start_at, mem_size;
  724. if (!p)
  725. return -EINVAL;
  726. if (!strncmp(p, "exactmap", 8)) {
  727. #ifdef CONFIG_CRASH_DUMP
  728. /*
  729. * If we are doing a crash dump, we still need to know
  730. * the real memory size before the original memory map is
  731. * reset.
  732. */
  733. saved_max_pfn = e820__end_of_ram_pfn();
  734. #endif
  735. e820_table->nr_entries = 0;
  736. userdef = 1;
  737. return 0;
  738. }
  739. oldp = p;
  740. mem_size = memparse(p, &p);
  741. if (p == oldp)
  742. return -EINVAL;
  743. userdef = 1;
  744. if (*p == '@') {
  745. start_at = memparse(p+1, &p);
  746. e820__range_add(start_at, mem_size, E820_TYPE_RAM);
  747. } else if (*p == '#') {
  748. start_at = memparse(p+1, &p);
  749. e820__range_add(start_at, mem_size, E820_TYPE_ACPI);
  750. } else if (*p == '$') {
  751. start_at = memparse(p+1, &p);
  752. e820__range_add(start_at, mem_size, E820_TYPE_RESERVED);
  753. } else if (*p == '!') {
  754. start_at = memparse(p+1, &p);
  755. e820__range_add(start_at, mem_size, E820_TYPE_PRAM);
  756. } else {
  757. e820__range_remove(mem_size, ULLONG_MAX - mem_size, E820_TYPE_RAM, 1);
  758. }
  759. return *p == '\0' ? 0 : -EINVAL;
  760. }
  761. static int __init parse_memmap_opt(char *str)
  762. {
  763. while (str) {
  764. char *k = strchr(str, ',');
  765. if (k)
  766. *k++ = 0;
  767. parse_memmap_one(str);
  768. str = k;
  769. }
  770. return 0;
  771. }
  772. early_param("memmap", parse_memmap_opt);
  773. /*
  774. * Reserve all entries from the bootloader's extensible data nodes list,
  775. * because if present we are going to use it later on to fetch e820
  776. * entries from it:
  777. */
  778. void __init e820__reserve_setup_data(void)
  779. {
  780. struct setup_data *data;
  781. u64 pa_data;
  782. pa_data = boot_params.hdr.setup_data;
  783. if (!pa_data)
  784. return;
  785. while (pa_data) {
  786. data = early_memremap(pa_data, sizeof(*data));
  787. e820__range_update(pa_data, sizeof(*data)+data->len, E820_TYPE_RAM, E820_TYPE_RESERVED_KERN);
  788. pa_data = data->next;
  789. early_memunmap(data, sizeof(*data));
  790. }
  791. e820__update_table(e820_table);
  792. memcpy(e820_table_firmware, e820_table, sizeof(*e820_table_firmware));
  793. pr_info("extended physical RAM map:\n");
  794. e820__print_table("reserve setup_data");
  795. }
  796. /*
  797. * Called after parse_early_param(), after early parameters (such as mem=)
  798. * have been processed, in which case we already have an E820 table filled in
  799. * via the parameter callback function(s), but it's not sorted and printed yet:
  800. */
  801. void __init e820__finish_early_params(void)
  802. {
  803. if (userdef) {
  804. if (e820__update_table(e820_table) < 0)
  805. early_panic("Invalid user supplied memory map");
  806. pr_info("e820: user-defined physical RAM map:\n");
  807. e820__print_table("user");
  808. }
  809. }
  810. static const char *__init e820_type_to_string(struct e820_entry *entry)
  811. {
  812. switch (entry->type) {
  813. case E820_TYPE_RESERVED_KERN: /* Fall-through: */
  814. case E820_TYPE_RAM: return "System RAM";
  815. case E820_TYPE_ACPI: return "ACPI Tables";
  816. case E820_TYPE_NVS: return "ACPI Non-volatile Storage";
  817. case E820_TYPE_UNUSABLE: return "Unusable memory";
  818. case E820_TYPE_PRAM: return "Persistent Memory (legacy)";
  819. case E820_TYPE_PMEM: return "Persistent Memory";
  820. case E820_TYPE_RESERVED: return "Reserved";
  821. default: return "Unknown E820 type";
  822. }
  823. }
  824. static unsigned long __init e820_type_to_iomem_type(struct e820_entry *entry)
  825. {
  826. switch (entry->type) {
  827. case E820_TYPE_RESERVED_KERN: /* Fall-through: */
  828. case E820_TYPE_RAM: return IORESOURCE_SYSTEM_RAM;
  829. case E820_TYPE_ACPI: /* Fall-through: */
  830. case E820_TYPE_NVS: /* Fall-through: */
  831. case E820_TYPE_UNUSABLE: /* Fall-through: */
  832. case E820_TYPE_PRAM: /* Fall-through: */
  833. case E820_TYPE_PMEM: /* Fall-through: */
  834. case E820_TYPE_RESERVED: /* Fall-through: */
  835. default: return IORESOURCE_MEM;
  836. }
  837. }
  838. static unsigned long __init e820_type_to_iores_desc(struct e820_entry *entry)
  839. {
  840. switch (entry->type) {
  841. case E820_TYPE_ACPI: return IORES_DESC_ACPI_TABLES;
  842. case E820_TYPE_NVS: return IORES_DESC_ACPI_NV_STORAGE;
  843. case E820_TYPE_PMEM: return IORES_DESC_PERSISTENT_MEMORY;
  844. case E820_TYPE_PRAM: return IORES_DESC_PERSISTENT_MEMORY_LEGACY;
  845. case E820_TYPE_RESERVED_KERN: /* Fall-through: */
  846. case E820_TYPE_RAM: /* Fall-through: */
  847. case E820_TYPE_UNUSABLE: /* Fall-through: */
  848. case E820_TYPE_RESERVED: /* Fall-through: */
  849. default: return IORES_DESC_NONE;
  850. }
  851. }
  852. static bool __init do_mark_busy(enum e820_type type, struct resource *res)
  853. {
  854. /* this is the legacy bios/dos rom-shadow + mmio region */
  855. if (res->start < (1ULL<<20))
  856. return true;
  857. /*
  858. * Treat persistent memory like device memory, i.e. reserve it
  859. * for exclusive use of a driver
  860. */
  861. switch (type) {
  862. case E820_TYPE_RESERVED:
  863. case E820_TYPE_PRAM:
  864. case E820_TYPE_PMEM:
  865. return false;
  866. case E820_TYPE_RESERVED_KERN:
  867. case E820_TYPE_RAM:
  868. case E820_TYPE_ACPI:
  869. case E820_TYPE_NVS:
  870. case E820_TYPE_UNUSABLE:
  871. default:
  872. return true;
  873. }
  874. }
  875. /*
  876. * Mark E820 reserved areas as busy for the resource manager:
  877. */
  878. static struct resource __initdata *e820_res;
  879. void __init e820__reserve_resources(void)
  880. {
  881. int i;
  882. struct resource *res;
  883. u64 end;
  884. res = alloc_bootmem(sizeof(*res) * e820_table->nr_entries);
  885. e820_res = res;
  886. for (i = 0; i < e820_table->nr_entries; i++) {
  887. struct e820_entry *entry = e820_table->entries + i;
  888. end = entry->addr + entry->size - 1;
  889. if (end != (resource_size_t)end) {
  890. res++;
  891. continue;
  892. }
  893. res->start = entry->addr;
  894. res->end = end;
  895. res->name = e820_type_to_string(entry);
  896. res->flags = e820_type_to_iomem_type(entry);
  897. res->desc = e820_type_to_iores_desc(entry);
  898. /*
  899. * Don't register the region that could be conflicted with
  900. * PCI device BAR resources and insert them later in
  901. * pcibios_resource_survey():
  902. */
  903. if (do_mark_busy(entry->type, res)) {
  904. res->flags |= IORESOURCE_BUSY;
  905. insert_resource(&iomem_resource, res);
  906. }
  907. res++;
  908. }
  909. for (i = 0; i < e820_table_firmware->nr_entries; i++) {
  910. struct e820_entry *entry = e820_table_firmware->entries + i;
  911. firmware_map_add_early(entry->addr, entry->addr + entry->size, e820_type_to_string(entry));
  912. }
  913. }
  914. /*
  915. * How much should we pad the end of RAM, depending on where it is?
  916. */
  917. static unsigned long __init ram_alignment(resource_size_t pos)
  918. {
  919. unsigned long mb = pos >> 20;
  920. /* To 64kB in the first megabyte */
  921. if (!mb)
  922. return 64*1024;
  923. /* To 1MB in the first 16MB */
  924. if (mb < 16)
  925. return 1024*1024;
  926. /* To 64MB for anything above that */
  927. return 64*1024*1024;
  928. }
  929. #define MAX_RESOURCE_SIZE ((resource_size_t)-1)
  930. void __init e820__reserve_resources_late(void)
  931. {
  932. int i;
  933. struct resource *res;
  934. res = e820_res;
  935. for (i = 0; i < e820_table->nr_entries; i++) {
  936. if (!res->parent && res->end)
  937. insert_resource_expand_to_fit(&iomem_resource, res);
  938. res++;
  939. }
  940. /*
  941. * Try to bump up RAM regions to reasonable boundaries, to
  942. * avoid stolen RAM:
  943. */
  944. for (i = 0; i < e820_table->nr_entries; i++) {
  945. struct e820_entry *entry = &e820_table->entries[i];
  946. u64 start, end;
  947. if (entry->type != E820_TYPE_RAM)
  948. continue;
  949. start = entry->addr + entry->size;
  950. end = round_up(start, ram_alignment(start)) - 1;
  951. if (end > MAX_RESOURCE_SIZE)
  952. end = MAX_RESOURCE_SIZE;
  953. if (start >= end)
  954. continue;
  955. printk(KERN_DEBUG "e820: reserve RAM buffer [mem %#010llx-%#010llx]\n", start, end);
  956. reserve_region_with_split(&iomem_resource, start, end, "RAM buffer");
  957. }
  958. }
  959. /*
  960. * Pass the firmware (bootloader) E820 map to the kernel and process it:
  961. */
  962. char *__init e820__memory_setup_default(void)
  963. {
  964. char *who = "BIOS-e820";
  965. /*
  966. * Try to copy the BIOS-supplied E820-map.
  967. *
  968. * Otherwise fake a memory map; one section from 0k->640k,
  969. * the next section from 1mb->appropriate_mem_k
  970. */
  971. if (append_e820_table(boot_params.e820_table, boot_params.e820_entries) < 0) {
  972. u64 mem_size;
  973. /* Compare results from other methods and take the one that gives more RAM: */
  974. if (boot_params.alt_mem_k < boot_params.screen_info.ext_mem_k) {
  975. mem_size = boot_params.screen_info.ext_mem_k;
  976. who = "BIOS-88";
  977. } else {
  978. mem_size = boot_params.alt_mem_k;
  979. who = "BIOS-e801";
  980. }
  981. e820_table->nr_entries = 0;
  982. e820__range_add(0, LOWMEMSIZE(), E820_TYPE_RAM);
  983. e820__range_add(HIGH_MEMORY, mem_size << 10, E820_TYPE_RAM);
  984. }
  985. /* We just appended a lot of ranges, sanitize the table: */
  986. e820__update_table(e820_table);
  987. return who;
  988. }
  989. /*
  990. * Calls e820__memory_setup_default() in essence to pick up the firmware/bootloader
  991. * E820 map - with an optional platform quirk available for virtual platforms
  992. * to override this method of boot environment processing:
  993. */
  994. void __init e820__memory_setup(void)
  995. {
  996. char *who;
  997. /* This is a firmware interface ABI - make sure we don't break it: */
  998. BUILD_BUG_ON(sizeof(struct boot_e820_entry) != 20);
  999. who = x86_init.resources.memory_setup();
  1000. memcpy(e820_table_firmware, e820_table, sizeof(*e820_table_firmware));
  1001. pr_info("e820: BIOS-provided physical RAM map:\n");
  1002. e820__print_table(who);
  1003. }
  1004. void __init e820__memblock_setup(void)
  1005. {
  1006. int i;
  1007. u64 end;
  1008. /*
  1009. * The bootstrap memblock region count maximum is 128 entries
  1010. * (INIT_MEMBLOCK_REGIONS), but EFI might pass us more E820 entries
  1011. * than that - so allow memblock resizing.
  1012. *
  1013. * This is safe, because this call happens pretty late during x86 setup,
  1014. * so we know about reserved memory regions already. (This is important
  1015. * so that memblock resizing does no stomp over reserved areas.)
  1016. */
  1017. memblock_allow_resize();
  1018. for (i = 0; i < e820_table->nr_entries; i++) {
  1019. struct e820_entry *entry = &e820_table->entries[i];
  1020. end = entry->addr + entry->size;
  1021. if (end != (resource_size_t)end)
  1022. continue;
  1023. if (entry->type != E820_TYPE_RAM && entry->type != E820_TYPE_RESERVED_KERN)
  1024. continue;
  1025. memblock_add(entry->addr, entry->size);
  1026. }
  1027. /* Throw away partial pages: */
  1028. memblock_trim_memory(PAGE_SIZE);
  1029. memblock_dump_all();
  1030. }