e820.c 37 KB

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