e820.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350
  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 itsmain 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 != 0) {
  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 != 0) {
  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. static u64 __init e820__range_update_kexec(u64 start, u64 size, enum e820_type old_type, enum e820_type new_type)
  468. {
  469. return __e820__range_update(e820_table_kexec, start, size, old_type, new_type);
  470. }
  471. /* Remove a range of memory from the E820 table: */
  472. u64 __init e820__range_remove(u64 start, u64 size, enum e820_type old_type, bool check_type)
  473. {
  474. int i;
  475. u64 end;
  476. u64 real_removed_size = 0;
  477. if (size > (ULLONG_MAX - start))
  478. size = ULLONG_MAX - start;
  479. end = start + size;
  480. printk(KERN_DEBUG "e820: remove [mem %#010Lx-%#010Lx] ", start, end - 1);
  481. if (check_type)
  482. e820_print_type(old_type);
  483. pr_cont("\n");
  484. for (i = 0; i < e820_table->nr_entries; i++) {
  485. struct e820_entry *entry = &e820_table->entries[i];
  486. u64 final_start, final_end;
  487. u64 entry_end;
  488. if (check_type && entry->type != old_type)
  489. continue;
  490. entry_end = entry->addr + entry->size;
  491. /* Completely covered? */
  492. if (entry->addr >= start && entry_end <= end) {
  493. real_removed_size += entry->size;
  494. memset(entry, 0, sizeof(*entry));
  495. continue;
  496. }
  497. /* Is the new range completely covered? */
  498. if (entry->addr < start && entry_end > end) {
  499. e820__range_add(end, entry_end - end, entry->type);
  500. entry->size = start - entry->addr;
  501. real_removed_size += size;
  502. continue;
  503. }
  504. /* Partially covered: */
  505. final_start = max(start, entry->addr);
  506. final_end = min(end, entry_end);
  507. if (final_start >= final_end)
  508. continue;
  509. real_removed_size += final_end - final_start;
  510. /*
  511. * Left range could be head or tail, so need to update
  512. * the size first:
  513. */
  514. entry->size -= final_end - final_start;
  515. if (entry->addr < final_start)
  516. continue;
  517. entry->addr = final_end;
  518. }
  519. return real_removed_size;
  520. }
  521. void __init e820__update_table_print(void)
  522. {
  523. if (e820__update_table(e820_table))
  524. return;
  525. pr_info("modified physical RAM map:\n");
  526. e820__print_table("modified");
  527. }
  528. static void __init e820__update_table_kexec(void)
  529. {
  530. e820__update_table(e820_table_kexec);
  531. }
  532. #define MAX_GAP_END 0x100000000ull
  533. /*
  534. * Search for a gap in the E820 memory space from 0 to MAX_GAP_END (4GB).
  535. */
  536. static int __init e820_search_gap(unsigned long *gapstart, unsigned long *gapsize)
  537. {
  538. unsigned long long last = MAX_GAP_END;
  539. int i = e820_table->nr_entries;
  540. int found = 0;
  541. while (--i >= 0) {
  542. unsigned long long start = e820_table->entries[i].addr;
  543. unsigned long long end = start + e820_table->entries[i].size;
  544. /*
  545. * Since "last" is at most 4GB, we know we'll
  546. * fit in 32 bits if this condition is true:
  547. */
  548. if (last > end) {
  549. unsigned long gap = last - end;
  550. if (gap >= *gapsize) {
  551. *gapsize = gap;
  552. *gapstart = end;
  553. found = 1;
  554. }
  555. }
  556. if (start < last)
  557. last = start;
  558. }
  559. return found;
  560. }
  561. /*
  562. * Search for the biggest gap in the low 32 bits of the E820
  563. * memory space. We pass this space to the PCI subsystem, so
  564. * that it can assign MMIO resources for hotplug or
  565. * unconfigured devices in.
  566. *
  567. * Hopefully the BIOS let enough space left.
  568. */
  569. __init void e820__setup_pci_gap(void)
  570. {
  571. unsigned long gapstart, gapsize;
  572. int found;
  573. gapsize = 0x400000;
  574. found = e820_search_gap(&gapstart, &gapsize);
  575. if (!found) {
  576. #ifdef CONFIG_X86_64
  577. gapstart = (max_pfn << PAGE_SHIFT) + 1024*1024;
  578. pr_err("Cannot find an available gap in the 32-bit address range\n");
  579. pr_err("PCI devices with unassigned 32-bit BARs may not work!\n");
  580. #else
  581. gapstart = 0x10000000;
  582. #endif
  583. }
  584. /*
  585. * e820__reserve_resources_late() protects stolen RAM already:
  586. */
  587. pci_mem_start = gapstart;
  588. pr_info("[mem %#010lx-%#010lx] available for PCI devices\n",
  589. gapstart, gapstart + gapsize - 1);
  590. }
  591. /*
  592. * Called late during init, in free_initmem().
  593. *
  594. * Initial e820_table and e820_table_kexec are largish __initdata arrays.
  595. *
  596. * Copy them to a (usually much smaller) dynamically allocated area that is
  597. * sized precisely after the number of e820 entries.
  598. *
  599. * This is done after we've performed all the fixes and tweaks to the tables.
  600. * All functions which modify them are __init functions, which won't exist
  601. * after free_initmem().
  602. */
  603. __init void e820__reallocate_tables(void)
  604. {
  605. struct e820_table *n;
  606. int size;
  607. size = offsetof(struct e820_table, entries) + sizeof(struct e820_entry)*e820_table->nr_entries;
  608. n = kmemdup(e820_table, size, GFP_KERNEL);
  609. BUG_ON(!n);
  610. e820_table = n;
  611. size = offsetof(struct e820_table, entries) + sizeof(struct e820_entry)*e820_table_kexec->nr_entries;
  612. n = kmemdup(e820_table_kexec, size, GFP_KERNEL);
  613. BUG_ON(!n);
  614. e820_table_kexec = n;
  615. size = offsetof(struct e820_table, entries) + sizeof(struct e820_entry)*e820_table_firmware->nr_entries;
  616. n = kmemdup(e820_table_firmware, size, GFP_KERNEL);
  617. BUG_ON(!n);
  618. e820_table_firmware = n;
  619. }
  620. /*
  621. * Because of the small fixed size of struct boot_params, only the first
  622. * 128 E820 memory entries are passed to the kernel via boot_params.e820_table,
  623. * the remaining (if any) entries are passed via the SETUP_E820_EXT node of
  624. * struct setup_data, which is parsed here.
  625. */
  626. void __init e820__memory_setup_extended(u64 phys_addr, u32 data_len)
  627. {
  628. int entries;
  629. struct boot_e820_entry *extmap;
  630. struct setup_data *sdata;
  631. sdata = early_memremap(phys_addr, data_len);
  632. entries = sdata->len / sizeof(*extmap);
  633. extmap = (struct boot_e820_entry *)(sdata->data);
  634. __append_e820_table(extmap, entries);
  635. e820__update_table(e820_table);
  636. memcpy(e820_table_kexec, e820_table, sizeof(*e820_table_kexec));
  637. memcpy(e820_table_firmware, e820_table, sizeof(*e820_table_firmware));
  638. early_memunmap(sdata, data_len);
  639. pr_info("extended physical RAM map:\n");
  640. e820__print_table("extended");
  641. }
  642. /*
  643. * Find the ranges of physical addresses that do not correspond to
  644. * E820 RAM areas and register the corresponding pages as 'nosave' for
  645. * hibernation (32-bit) or software suspend and suspend to RAM (64-bit).
  646. *
  647. * This function requires the E820 map to be sorted and without any
  648. * overlapping entries.
  649. */
  650. void __init e820__register_nosave_regions(unsigned long limit_pfn)
  651. {
  652. int i;
  653. unsigned long pfn = 0;
  654. for (i = 0; i < e820_table->nr_entries; i++) {
  655. struct e820_entry *entry = &e820_table->entries[i];
  656. if (pfn < PFN_UP(entry->addr))
  657. register_nosave_region(pfn, PFN_UP(entry->addr));
  658. pfn = PFN_DOWN(entry->addr + entry->size);
  659. if (entry->type != E820_TYPE_RAM && entry->type != E820_TYPE_RESERVED_KERN)
  660. register_nosave_region(PFN_UP(entry->addr), pfn);
  661. if (pfn >= limit_pfn)
  662. break;
  663. }
  664. }
  665. #ifdef CONFIG_ACPI
  666. /*
  667. * Register ACPI NVS memory regions, so that we can save/restore them during
  668. * hibernation and the subsequent resume:
  669. */
  670. static int __init e820__register_nvs_regions(void)
  671. {
  672. int i;
  673. for (i = 0; i < e820_table->nr_entries; i++) {
  674. struct e820_entry *entry = &e820_table->entries[i];
  675. if (entry->type == E820_TYPE_NVS)
  676. acpi_nvs_register(entry->addr, entry->size);
  677. }
  678. return 0;
  679. }
  680. core_initcall(e820__register_nvs_regions);
  681. #endif
  682. /*
  683. * Allocate the requested number of bytes with the requsted alignment
  684. * and return (the physical address) to the caller. Also register this
  685. * range in the 'kexec' E820 table as a reserved range.
  686. *
  687. * This allows kexec to fake a new mptable, as if it came from the real
  688. * system.
  689. */
  690. u64 __init e820__memblock_alloc_reserved(u64 size, u64 align)
  691. {
  692. u64 addr;
  693. addr = memblock_phys_alloc(size, align);
  694. if (addr) {
  695. e820__range_update_kexec(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
  696. pr_info("update e820_table_kexec for e820__memblock_alloc_reserved()\n");
  697. e820__update_table_kexec();
  698. }
  699. return addr;
  700. }
  701. #ifdef CONFIG_X86_32
  702. # ifdef CONFIG_X86_PAE
  703. # define MAX_ARCH_PFN (1ULL<<(36-PAGE_SHIFT))
  704. # else
  705. # define MAX_ARCH_PFN (1ULL<<(32-PAGE_SHIFT))
  706. # endif
  707. #else /* CONFIG_X86_32 */
  708. # define MAX_ARCH_PFN MAXMEM>>PAGE_SHIFT
  709. #endif
  710. /*
  711. * Find the highest page frame number we have available
  712. */
  713. static unsigned long __init e820_end_pfn(unsigned long limit_pfn, enum e820_type type)
  714. {
  715. int i;
  716. unsigned long last_pfn = 0;
  717. unsigned long max_arch_pfn = MAX_ARCH_PFN;
  718. for (i = 0; i < e820_table->nr_entries; i++) {
  719. struct e820_entry *entry = &e820_table->entries[i];
  720. unsigned long start_pfn;
  721. unsigned long end_pfn;
  722. if (entry->type != type)
  723. continue;
  724. start_pfn = entry->addr >> PAGE_SHIFT;
  725. end_pfn = (entry->addr + entry->size) >> PAGE_SHIFT;
  726. if (start_pfn >= limit_pfn)
  727. continue;
  728. if (end_pfn > limit_pfn) {
  729. last_pfn = limit_pfn;
  730. break;
  731. }
  732. if (end_pfn > last_pfn)
  733. last_pfn = end_pfn;
  734. }
  735. if (last_pfn > max_arch_pfn)
  736. last_pfn = max_arch_pfn;
  737. pr_info("last_pfn = %#lx max_arch_pfn = %#lx\n",
  738. last_pfn, max_arch_pfn);
  739. return last_pfn;
  740. }
  741. unsigned long __init e820__end_of_ram_pfn(void)
  742. {
  743. return e820_end_pfn(MAX_ARCH_PFN, E820_TYPE_RAM);
  744. }
  745. unsigned long __init e820__end_of_low_ram_pfn(void)
  746. {
  747. return e820_end_pfn(1UL << (32 - PAGE_SHIFT), E820_TYPE_RAM);
  748. }
  749. static void __init early_panic(char *msg)
  750. {
  751. early_printk(msg);
  752. panic(msg);
  753. }
  754. static int userdef __initdata;
  755. /* The "mem=nopentium" boot option disables 4MB page tables on 32-bit kernels: */
  756. static int __init parse_memopt(char *p)
  757. {
  758. u64 mem_size;
  759. if (!p)
  760. return -EINVAL;
  761. if (!strcmp(p, "nopentium")) {
  762. #ifdef CONFIG_X86_32
  763. setup_clear_cpu_cap(X86_FEATURE_PSE);
  764. return 0;
  765. #else
  766. pr_warn("mem=nopentium ignored! (only supported on x86_32)\n");
  767. return -EINVAL;
  768. #endif
  769. }
  770. userdef = 1;
  771. mem_size = memparse(p, &p);
  772. /* Don't remove all memory when getting "mem={invalid}" parameter: */
  773. if (mem_size == 0)
  774. return -EINVAL;
  775. e820__range_remove(mem_size, ULLONG_MAX - mem_size, E820_TYPE_RAM, 1);
  776. #ifdef CONFIG_MEMORY_HOTPLUG
  777. max_mem_size = mem_size;
  778. #endif
  779. return 0;
  780. }
  781. early_param("mem", parse_memopt);
  782. static int __init parse_memmap_one(char *p)
  783. {
  784. char *oldp;
  785. u64 start_at, mem_size;
  786. if (!p)
  787. return -EINVAL;
  788. if (!strncmp(p, "exactmap", 8)) {
  789. e820_table->nr_entries = 0;
  790. userdef = 1;
  791. return 0;
  792. }
  793. oldp = p;
  794. mem_size = memparse(p, &p);
  795. if (p == oldp)
  796. return -EINVAL;
  797. userdef = 1;
  798. if (*p == '@') {
  799. start_at = memparse(p+1, &p);
  800. e820__range_add(start_at, mem_size, E820_TYPE_RAM);
  801. } else if (*p == '#') {
  802. start_at = memparse(p+1, &p);
  803. e820__range_add(start_at, mem_size, E820_TYPE_ACPI);
  804. } else if (*p == '$') {
  805. start_at = memparse(p+1, &p);
  806. e820__range_add(start_at, mem_size, E820_TYPE_RESERVED);
  807. } else if (*p == '!') {
  808. start_at = memparse(p+1, &p);
  809. e820__range_add(start_at, mem_size, E820_TYPE_PRAM);
  810. } else if (*p == '%') {
  811. enum e820_type from = 0, to = 0;
  812. start_at = memparse(p + 1, &p);
  813. if (*p == '-')
  814. from = simple_strtoull(p + 1, &p, 0);
  815. if (*p == '+')
  816. to = simple_strtoull(p + 1, &p, 0);
  817. if (*p != '\0')
  818. return -EINVAL;
  819. if (from && to)
  820. e820__range_update(start_at, mem_size, from, to);
  821. else if (to)
  822. e820__range_add(start_at, mem_size, to);
  823. else if (from)
  824. e820__range_remove(start_at, mem_size, from, 1);
  825. else
  826. e820__range_remove(start_at, mem_size, 0, 0);
  827. } else {
  828. e820__range_remove(mem_size, ULLONG_MAX - mem_size, E820_TYPE_RAM, 1);
  829. }
  830. return *p == '\0' ? 0 : -EINVAL;
  831. }
  832. static int __init parse_memmap_opt(char *str)
  833. {
  834. while (str) {
  835. char *k = strchr(str, ',');
  836. if (k)
  837. *k++ = 0;
  838. parse_memmap_one(str);
  839. str = k;
  840. }
  841. return 0;
  842. }
  843. early_param("memmap", parse_memmap_opt);
  844. /*
  845. * Reserve all entries from the bootloader's extensible data nodes list,
  846. * because if present we are going to use it later on to fetch e820
  847. * entries from it:
  848. */
  849. void __init e820__reserve_setup_data(void)
  850. {
  851. struct setup_indirect *indirect;
  852. struct setup_data *data;
  853. u64 pa_data, pa_next;
  854. u32 len;
  855. pa_data = boot_params.hdr.setup_data;
  856. if (!pa_data)
  857. return;
  858. while (pa_data) {
  859. data = early_memremap(pa_data, sizeof(*data));
  860. if (!data) {
  861. pr_warn("e820: failed to memremap setup_data entry\n");
  862. return;
  863. }
  864. len = sizeof(*data);
  865. pa_next = data->next;
  866. e820__range_update(pa_data, sizeof(*data)+data->len, E820_TYPE_RAM, E820_TYPE_RESERVED_KERN);
  867. /*
  868. * SETUP_EFI is supplied by kexec and does not need to be
  869. * reserved.
  870. */
  871. if (data->type != SETUP_EFI)
  872. e820__range_update_kexec(pa_data,
  873. sizeof(*data) + data->len,
  874. E820_TYPE_RAM, E820_TYPE_RESERVED_KERN);
  875. if (data->type == SETUP_INDIRECT) {
  876. len += data->len;
  877. early_memunmap(data, sizeof(*data));
  878. data = early_memremap(pa_data, len);
  879. if (!data) {
  880. pr_warn("e820: failed to memremap indirect setup_data\n");
  881. return;
  882. }
  883. indirect = (struct setup_indirect *)data->data;
  884. if (indirect->type != SETUP_INDIRECT) {
  885. e820__range_update(indirect->addr, indirect->len,
  886. E820_TYPE_RAM, E820_TYPE_RESERVED_KERN);
  887. e820__range_update_kexec(indirect->addr, indirect->len,
  888. E820_TYPE_RAM, E820_TYPE_RESERVED_KERN);
  889. }
  890. }
  891. pa_data = pa_next;
  892. early_memunmap(data, len);
  893. }
  894. e820__update_table(e820_table);
  895. e820__update_table(e820_table_kexec);
  896. pr_info("extended physical RAM map:\n");
  897. e820__print_table("reserve setup_data");
  898. }
  899. /*
  900. * Called after parse_early_param(), after early parameters (such as mem=)
  901. * have been processed, in which case we already have an E820 table filled in
  902. * via the parameter callback function(s), but it's not sorted and printed yet:
  903. */
  904. void __init e820__finish_early_params(void)
  905. {
  906. if (userdef) {
  907. if (e820__update_table(e820_table) < 0)
  908. early_panic("Invalid user supplied memory map");
  909. pr_info("user-defined physical RAM map:\n");
  910. e820__print_table("user");
  911. }
  912. }
  913. static const char *__init e820_type_to_string(struct e820_entry *entry)
  914. {
  915. switch (entry->type) {
  916. case E820_TYPE_RESERVED_KERN: /* Fall-through: */
  917. case E820_TYPE_RAM: return "System RAM";
  918. case E820_TYPE_ACPI: return "ACPI Tables";
  919. case E820_TYPE_NVS: return "ACPI Non-volatile Storage";
  920. case E820_TYPE_UNUSABLE: return "Unusable memory";
  921. case E820_TYPE_PRAM: return "Persistent Memory (legacy)";
  922. case E820_TYPE_PMEM: return "Persistent Memory";
  923. case E820_TYPE_RESERVED: return "Reserved";
  924. case E820_TYPE_SOFT_RESERVED: return "Soft Reserved";
  925. default: return "Unknown E820 type";
  926. }
  927. }
  928. static unsigned long __init e820_type_to_iomem_type(struct e820_entry *entry)
  929. {
  930. switch (entry->type) {
  931. case E820_TYPE_RESERVED_KERN: /* Fall-through: */
  932. case E820_TYPE_RAM: return IORESOURCE_SYSTEM_RAM;
  933. case E820_TYPE_ACPI: /* Fall-through: */
  934. case E820_TYPE_NVS: /* Fall-through: */
  935. case E820_TYPE_UNUSABLE: /* Fall-through: */
  936. case E820_TYPE_PRAM: /* Fall-through: */
  937. case E820_TYPE_PMEM: /* Fall-through: */
  938. case E820_TYPE_RESERVED: /* Fall-through: */
  939. case E820_TYPE_SOFT_RESERVED: /* Fall-through: */
  940. default: return IORESOURCE_MEM;
  941. }
  942. }
  943. static unsigned long __init e820_type_to_iores_desc(struct e820_entry *entry)
  944. {
  945. switch (entry->type) {
  946. case E820_TYPE_ACPI: return IORES_DESC_ACPI_TABLES;
  947. case E820_TYPE_NVS: return IORES_DESC_ACPI_NV_STORAGE;
  948. case E820_TYPE_PMEM: return IORES_DESC_PERSISTENT_MEMORY;
  949. case E820_TYPE_PRAM: return IORES_DESC_PERSISTENT_MEMORY_LEGACY;
  950. case E820_TYPE_RESERVED: return IORES_DESC_RESERVED;
  951. case E820_TYPE_SOFT_RESERVED: return IORES_DESC_SOFT_RESERVED;
  952. case E820_TYPE_RESERVED_KERN: /* Fall-through: */
  953. case E820_TYPE_RAM: /* Fall-through: */
  954. case E820_TYPE_UNUSABLE: /* Fall-through: */
  955. default: return IORES_DESC_NONE;
  956. }
  957. }
  958. static bool __init do_mark_busy(enum e820_type type, struct resource *res)
  959. {
  960. /* this is the legacy bios/dos rom-shadow + mmio region */
  961. if (res->start < (1ULL<<20))
  962. return true;
  963. /*
  964. * Treat persistent memory and other special memory ranges like
  965. * device memory, i.e. reserve it for exclusive use of a driver
  966. */
  967. switch (type) {
  968. case E820_TYPE_RESERVED:
  969. case E820_TYPE_SOFT_RESERVED:
  970. case E820_TYPE_PRAM:
  971. case E820_TYPE_PMEM:
  972. return false;
  973. case E820_TYPE_RESERVED_KERN:
  974. case E820_TYPE_RAM:
  975. case E820_TYPE_ACPI:
  976. case E820_TYPE_NVS:
  977. case E820_TYPE_UNUSABLE:
  978. default:
  979. return true;
  980. }
  981. }
  982. /*
  983. * Mark E820 reserved areas as busy for the resource manager:
  984. */
  985. static struct resource __initdata *e820_res;
  986. void __init e820__reserve_resources(void)
  987. {
  988. int i;
  989. struct resource *res;
  990. u64 end;
  991. res = memblock_alloc(sizeof(*res) * e820_table->nr_entries,
  992. SMP_CACHE_BYTES);
  993. if (!res)
  994. panic("%s: Failed to allocate %zu bytes\n", __func__,
  995. sizeof(*res) * e820_table->nr_entries);
  996. e820_res = res;
  997. for (i = 0; i < e820_table->nr_entries; i++) {
  998. struct e820_entry *entry = e820_table->entries + i;
  999. end = entry->addr + entry->size - 1;
  1000. if (end != (resource_size_t)end) {
  1001. res++;
  1002. continue;
  1003. }
  1004. res->start = entry->addr;
  1005. res->end = end;
  1006. res->name = e820_type_to_string(entry);
  1007. res->flags = e820_type_to_iomem_type(entry);
  1008. res->desc = e820_type_to_iores_desc(entry);
  1009. /*
  1010. * Don't register the region that could be conflicted with
  1011. * PCI device BAR resources and insert them later in
  1012. * pcibios_resource_survey():
  1013. */
  1014. if (do_mark_busy(entry->type, res)) {
  1015. res->flags |= IORESOURCE_BUSY;
  1016. insert_resource(&iomem_resource, res);
  1017. }
  1018. res++;
  1019. }
  1020. /* Expose the bootloader-provided memory layout to the sysfs. */
  1021. for (i = 0; i < e820_table_firmware->nr_entries; i++) {
  1022. struct e820_entry *entry = e820_table_firmware->entries + i;
  1023. firmware_map_add_early(entry->addr, entry->addr + entry->size, e820_type_to_string(entry));
  1024. }
  1025. }
  1026. /*
  1027. * How much should we pad the end of RAM, depending on where it is?
  1028. */
  1029. static unsigned long __init ram_alignment(resource_size_t pos)
  1030. {
  1031. unsigned long mb = pos >> 20;
  1032. /* To 64kB in the first megabyte */
  1033. if (!mb)
  1034. return 64*1024;
  1035. /* To 1MB in the first 16MB */
  1036. if (mb < 16)
  1037. return 1024*1024;
  1038. /* To 64MB for anything above that */
  1039. return 64*1024*1024;
  1040. }
  1041. #define MAX_RESOURCE_SIZE ((resource_size_t)-1)
  1042. void __init e820__reserve_resources_late(void)
  1043. {
  1044. int i;
  1045. struct resource *res;
  1046. res = e820_res;
  1047. for (i = 0; i < e820_table->nr_entries; i++) {
  1048. if (!res->parent && res->end)
  1049. insert_resource_expand_to_fit(&iomem_resource, res);
  1050. res++;
  1051. }
  1052. /*
  1053. * Try to bump up RAM regions to reasonable boundaries, to
  1054. * avoid stolen RAM:
  1055. */
  1056. for (i = 0; i < e820_table->nr_entries; i++) {
  1057. struct e820_entry *entry = &e820_table->entries[i];
  1058. u64 start, end;
  1059. if (entry->type != E820_TYPE_RAM)
  1060. continue;
  1061. start = entry->addr + entry->size;
  1062. end = round_up(start, ram_alignment(start)) - 1;
  1063. if (end > MAX_RESOURCE_SIZE)
  1064. end = MAX_RESOURCE_SIZE;
  1065. if (start >= end)
  1066. continue;
  1067. printk(KERN_DEBUG "e820: reserve RAM buffer [mem %#010llx-%#010llx]\n", start, end);
  1068. reserve_region_with_split(&iomem_resource, start, end, "RAM buffer");
  1069. }
  1070. }
  1071. /*
  1072. * Pass the firmware (bootloader) E820 map to the kernel and process it:
  1073. */
  1074. char *__init e820__memory_setup_default(void)
  1075. {
  1076. char *who = "BIOS-e820";
  1077. /*
  1078. * Try to copy the BIOS-supplied E820-map.
  1079. *
  1080. * Otherwise fake a memory map; one section from 0k->640k,
  1081. * the next section from 1mb->appropriate_mem_k
  1082. */
  1083. if (append_e820_table(boot_params.e820_table, boot_params.e820_entries) < 0) {
  1084. u64 mem_size;
  1085. /* Compare results from other methods and take the one that gives more RAM: */
  1086. if (boot_params.alt_mem_k < boot_params.screen_info.ext_mem_k) {
  1087. mem_size = boot_params.screen_info.ext_mem_k;
  1088. who = "BIOS-88";
  1089. } else {
  1090. mem_size = boot_params.alt_mem_k;
  1091. who = "BIOS-e801";
  1092. }
  1093. e820_table->nr_entries = 0;
  1094. e820__range_add(0, LOWMEMSIZE(), E820_TYPE_RAM);
  1095. e820__range_add(HIGH_MEMORY, mem_size << 10, E820_TYPE_RAM);
  1096. }
  1097. /* We just appended a lot of ranges, sanitize the table: */
  1098. e820__update_table(e820_table);
  1099. return who;
  1100. }
  1101. /*
  1102. * Calls e820__memory_setup_default() in essence to pick up the firmware/bootloader
  1103. * E820 map - with an optional platform quirk available for virtual platforms
  1104. * to override this method of boot environment processing:
  1105. */
  1106. void __init e820__memory_setup(void)
  1107. {
  1108. char *who;
  1109. /* This is a firmware interface ABI - make sure we don't break it: */
  1110. BUILD_BUG_ON(sizeof(struct boot_e820_entry) != 20);
  1111. who = x86_init.resources.memory_setup();
  1112. memcpy(e820_table_kexec, e820_table, sizeof(*e820_table_kexec));
  1113. memcpy(e820_table_firmware, e820_table, sizeof(*e820_table_firmware));
  1114. pr_info("BIOS-provided physical RAM map:\n");
  1115. e820__print_table(who);
  1116. }
  1117. void __init e820__memblock_setup(void)
  1118. {
  1119. int i;
  1120. u64 end;
  1121. /*
  1122. * The bootstrap memblock region count maximum is 128 entries
  1123. * (INIT_MEMBLOCK_REGIONS), but EFI might pass us more E820 entries
  1124. * than that - so allow memblock resizing.
  1125. *
  1126. * This is safe, because this call happens pretty late during x86 setup,
  1127. * so we know about reserved memory regions already. (This is important
  1128. * so that memblock resizing does no stomp over reserved areas.)
  1129. */
  1130. memblock_allow_resize();
  1131. for (i = 0; i < e820_table->nr_entries; i++) {
  1132. struct e820_entry *entry = &e820_table->entries[i];
  1133. end = entry->addr + entry->size;
  1134. if (end != (resource_size_t)end)
  1135. continue;
  1136. if (entry->type == E820_TYPE_SOFT_RESERVED)
  1137. memblock_reserve(entry->addr, entry->size);
  1138. if (entry->type != E820_TYPE_RAM && entry->type != E820_TYPE_RESERVED_KERN)
  1139. continue;
  1140. memblock_add(entry->addr, entry->size);
  1141. }
  1142. /* Throw away partial pages: */
  1143. memblock_trim_memory(PAGE_SIZE);
  1144. memblock_dump_all();
  1145. }