e820.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338
  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. u64 last_addr = 0;
  655. for (i = 0; i < e820_table->nr_entries; i++) {
  656. struct e820_entry *entry = &e820_table->entries[i];
  657. if (entry->type != E820_TYPE_RAM && entry->type != E820_TYPE_RESERVED_KERN)
  658. continue;
  659. if (last_addr < entry->addr)
  660. register_nosave_region(PFN_DOWN(last_addr), PFN_UP(entry->addr));
  661. last_addr = entry->addr + entry->size;
  662. }
  663. register_nosave_region(PFN_DOWN(last_addr), limit_pfn);
  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 requested 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_table(e820_table_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_ram_pfn(unsigned long limit_pfn)
  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 != E820_TYPE_RAM &&
  723. entry->type != E820_TYPE_ACPI)
  724. continue;
  725. start_pfn = entry->addr >> PAGE_SHIFT;
  726. end_pfn = (entry->addr + entry->size) >> PAGE_SHIFT;
  727. if (start_pfn >= limit_pfn)
  728. continue;
  729. if (end_pfn > limit_pfn) {
  730. last_pfn = limit_pfn;
  731. break;
  732. }
  733. if (end_pfn > last_pfn)
  734. last_pfn = end_pfn;
  735. }
  736. if (last_pfn > max_arch_pfn)
  737. last_pfn = max_arch_pfn;
  738. pr_info("last_pfn = %#lx max_arch_pfn = %#lx\n",
  739. last_pfn, max_arch_pfn);
  740. return last_pfn;
  741. }
  742. unsigned long __init e820__end_of_ram_pfn(void)
  743. {
  744. return e820__end_ram_pfn(MAX_ARCH_PFN);
  745. }
  746. unsigned long __init e820__end_of_low_ram_pfn(void)
  747. {
  748. return e820__end_ram_pfn(1UL << (32 - PAGE_SHIFT));
  749. }
  750. static void __init early_panic(char *msg)
  751. {
  752. early_printk(msg);
  753. panic(msg);
  754. }
  755. static int userdef __initdata;
  756. /* The "mem=nopentium" boot option disables 4MB page tables on 32-bit kernels: */
  757. static int __init parse_memopt(char *p)
  758. {
  759. u64 mem_size;
  760. if (!p)
  761. return -EINVAL;
  762. if (!strcmp(p, "nopentium")) {
  763. #ifdef CONFIG_X86_32
  764. setup_clear_cpu_cap(X86_FEATURE_PSE);
  765. return 0;
  766. #else
  767. pr_warn("mem=nopentium ignored! (only supported on x86_32)\n");
  768. return -EINVAL;
  769. #endif
  770. }
  771. userdef = 1;
  772. mem_size = memparse(p, &p);
  773. /* Don't remove all memory when getting "mem={invalid}" parameter: */
  774. if (mem_size == 0)
  775. return -EINVAL;
  776. e820__range_remove(mem_size, ULLONG_MAX - mem_size, E820_TYPE_RAM, 1);
  777. #ifdef CONFIG_MEMORY_HOTPLUG
  778. max_mem_size = mem_size;
  779. #endif
  780. return 0;
  781. }
  782. early_param("mem", parse_memopt);
  783. static int __init parse_memmap_one(char *p)
  784. {
  785. char *oldp;
  786. u64 start_at, mem_size;
  787. if (!p)
  788. return -EINVAL;
  789. if (!strncmp(p, "exactmap", 8)) {
  790. e820_table->nr_entries = 0;
  791. userdef = 1;
  792. return 0;
  793. }
  794. oldp = p;
  795. mem_size = memparse(p, &p);
  796. if (p == oldp)
  797. return -EINVAL;
  798. userdef = 1;
  799. if (*p == '@') {
  800. start_at = memparse(p+1, &p);
  801. e820__range_add(start_at, mem_size, E820_TYPE_RAM);
  802. } else if (*p == '#') {
  803. start_at = memparse(p+1, &p);
  804. e820__range_add(start_at, mem_size, E820_TYPE_ACPI);
  805. } else if (*p == '$') {
  806. start_at = memparse(p+1, &p);
  807. e820__range_add(start_at, mem_size, E820_TYPE_RESERVED);
  808. } else if (*p == '!') {
  809. start_at = memparse(p+1, &p);
  810. e820__range_add(start_at, mem_size, E820_TYPE_PRAM);
  811. } else if (*p == '%') {
  812. enum e820_type from = 0, to = 0;
  813. start_at = memparse(p + 1, &p);
  814. if (*p == '-')
  815. from = simple_strtoull(p + 1, &p, 0);
  816. if (*p == '+')
  817. to = simple_strtoull(p + 1, &p, 0);
  818. if (*p != '\0')
  819. return -EINVAL;
  820. if (from && to)
  821. e820__range_update(start_at, mem_size, from, to);
  822. else if (to)
  823. e820__range_add(start_at, mem_size, to);
  824. else if (from)
  825. e820__range_remove(start_at, mem_size, from, 1);
  826. else
  827. e820__range_remove(start_at, mem_size, 0, 0);
  828. } else {
  829. e820__range_remove(mem_size, ULLONG_MAX - mem_size, E820_TYPE_RAM, 1);
  830. }
  831. return *p == '\0' ? 0 : -EINVAL;
  832. }
  833. static int __init parse_memmap_opt(char *str)
  834. {
  835. while (str) {
  836. char *k = strchr(str, ',');
  837. if (k)
  838. *k++ = 0;
  839. parse_memmap_one(str);
  840. str = k;
  841. }
  842. return 0;
  843. }
  844. early_param("memmap", parse_memmap_opt);
  845. /*
  846. * Reserve all entries from the bootloader's extensible data nodes list,
  847. * because if present we are going to use it later on to fetch e820
  848. * entries from it:
  849. */
  850. void __init e820__reserve_setup_data(void)
  851. {
  852. struct setup_indirect *indirect;
  853. struct setup_data *data;
  854. u64 pa_data, pa_next;
  855. u32 len;
  856. pa_data = boot_params.hdr.setup_data;
  857. if (!pa_data)
  858. return;
  859. while (pa_data) {
  860. data = early_memremap(pa_data, sizeof(*data));
  861. if (!data) {
  862. pr_warn("e820: failed to memremap setup_data entry\n");
  863. return;
  864. }
  865. len = sizeof(*data);
  866. pa_next = data->next;
  867. e820__range_update(pa_data, sizeof(*data)+data->len, E820_TYPE_RAM, E820_TYPE_RESERVED_KERN);
  868. if (data->type == SETUP_INDIRECT) {
  869. len += data->len;
  870. early_memunmap(data, sizeof(*data));
  871. data = early_memremap(pa_data, len);
  872. if (!data) {
  873. pr_warn("e820: failed to memremap indirect setup_data\n");
  874. return;
  875. }
  876. indirect = (struct setup_indirect *)data->data;
  877. if (indirect->type != SETUP_INDIRECT)
  878. e820__range_update(indirect->addr, indirect->len,
  879. E820_TYPE_RAM, E820_TYPE_RESERVED_KERN);
  880. }
  881. pa_data = pa_next;
  882. early_memunmap(data, len);
  883. }
  884. e820__update_table(e820_table);
  885. pr_info("extended physical RAM map:\n");
  886. e820__print_table("reserve setup_data");
  887. }
  888. /*
  889. * Called after parse_early_param(), after early parameters (such as mem=)
  890. * have been processed, in which case we already have an E820 table filled in
  891. * via the parameter callback function(s), but it's not sorted and printed yet:
  892. */
  893. void __init e820__finish_early_params(void)
  894. {
  895. if (userdef) {
  896. if (e820__update_table(e820_table) < 0)
  897. early_panic("Invalid user supplied memory map");
  898. pr_info("user-defined physical RAM map:\n");
  899. e820__print_table("user");
  900. }
  901. }
  902. static const char *__init e820_type_to_string(struct e820_entry *entry)
  903. {
  904. switch (entry->type) {
  905. case E820_TYPE_RESERVED_KERN: /* Fall-through: */
  906. case E820_TYPE_RAM: return "System RAM";
  907. case E820_TYPE_ACPI: return "ACPI Tables";
  908. case E820_TYPE_NVS: return "ACPI Non-volatile Storage";
  909. case E820_TYPE_UNUSABLE: return "Unusable memory";
  910. case E820_TYPE_PRAM: return "Persistent Memory (legacy)";
  911. case E820_TYPE_PMEM: return "Persistent Memory";
  912. case E820_TYPE_RESERVED: return "Reserved";
  913. case E820_TYPE_SOFT_RESERVED: return "Soft Reserved";
  914. default: return "Unknown E820 type";
  915. }
  916. }
  917. static unsigned long __init e820_type_to_iomem_type(struct e820_entry *entry)
  918. {
  919. switch (entry->type) {
  920. case E820_TYPE_RESERVED_KERN: /* Fall-through: */
  921. case E820_TYPE_RAM: return IORESOURCE_SYSTEM_RAM;
  922. case E820_TYPE_ACPI: /* Fall-through: */
  923. case E820_TYPE_NVS: /* Fall-through: */
  924. case E820_TYPE_UNUSABLE: /* Fall-through: */
  925. case E820_TYPE_PRAM: /* Fall-through: */
  926. case E820_TYPE_PMEM: /* Fall-through: */
  927. case E820_TYPE_RESERVED: /* Fall-through: */
  928. case E820_TYPE_SOFT_RESERVED: /* Fall-through: */
  929. default: return IORESOURCE_MEM;
  930. }
  931. }
  932. static unsigned long __init e820_type_to_iores_desc(struct e820_entry *entry)
  933. {
  934. switch (entry->type) {
  935. case E820_TYPE_ACPI: return IORES_DESC_ACPI_TABLES;
  936. case E820_TYPE_NVS: return IORES_DESC_ACPI_NV_STORAGE;
  937. case E820_TYPE_PMEM: return IORES_DESC_PERSISTENT_MEMORY;
  938. case E820_TYPE_PRAM: return IORES_DESC_PERSISTENT_MEMORY_LEGACY;
  939. case E820_TYPE_RESERVED: return IORES_DESC_RESERVED;
  940. case E820_TYPE_SOFT_RESERVED: return IORES_DESC_SOFT_RESERVED;
  941. case E820_TYPE_RESERVED_KERN: /* Fall-through: */
  942. case E820_TYPE_RAM: /* Fall-through: */
  943. case E820_TYPE_UNUSABLE: /* Fall-through: */
  944. default: return IORES_DESC_NONE;
  945. }
  946. }
  947. static bool __init do_mark_busy(enum e820_type type, struct resource *res)
  948. {
  949. /* this is the legacy bios/dos rom-shadow + mmio region */
  950. if (res->start < (1ULL<<20))
  951. return true;
  952. /*
  953. * Treat persistent memory and other special memory ranges like
  954. * device memory, i.e. reserve it for exclusive use of a driver
  955. */
  956. switch (type) {
  957. case E820_TYPE_RESERVED:
  958. case E820_TYPE_SOFT_RESERVED:
  959. case E820_TYPE_PRAM:
  960. case E820_TYPE_PMEM:
  961. return false;
  962. case E820_TYPE_RESERVED_KERN:
  963. case E820_TYPE_RAM:
  964. case E820_TYPE_ACPI:
  965. case E820_TYPE_NVS:
  966. case E820_TYPE_UNUSABLE:
  967. default:
  968. return true;
  969. }
  970. }
  971. /*
  972. * Mark E820 reserved areas as busy for the resource manager:
  973. */
  974. static struct resource __initdata *e820_res;
  975. void __init e820__reserve_resources(void)
  976. {
  977. int i;
  978. struct resource *res;
  979. u64 end;
  980. res = memblock_alloc(sizeof(*res) * e820_table->nr_entries,
  981. SMP_CACHE_BYTES);
  982. if (!res)
  983. panic("%s: Failed to allocate %zu bytes\n", __func__,
  984. sizeof(*res) * e820_table->nr_entries);
  985. e820_res = res;
  986. for (i = 0; i < e820_table->nr_entries; i++) {
  987. struct e820_entry *entry = e820_table->entries + i;
  988. end = entry->addr + entry->size - 1;
  989. if (end != (resource_size_t)end) {
  990. res++;
  991. continue;
  992. }
  993. res->start = entry->addr;
  994. res->end = end;
  995. res->name = e820_type_to_string(entry);
  996. res->flags = e820_type_to_iomem_type(entry);
  997. res->desc = e820_type_to_iores_desc(entry);
  998. /*
  999. * Don't register the region that could be conflicted with
  1000. * PCI device BAR resources and insert them later in
  1001. * pcibios_resource_survey():
  1002. */
  1003. if (do_mark_busy(entry->type, res)) {
  1004. res->flags |= IORESOURCE_BUSY;
  1005. insert_resource(&iomem_resource, res);
  1006. }
  1007. res++;
  1008. }
  1009. /* Expose the bootloader-provided memory layout to the sysfs. */
  1010. for (i = 0; i < e820_table_firmware->nr_entries; i++) {
  1011. struct e820_entry *entry = e820_table_firmware->entries + i;
  1012. firmware_map_add_early(entry->addr, entry->addr + entry->size, e820_type_to_string(entry));
  1013. }
  1014. }
  1015. /*
  1016. * How much should we pad the end of RAM, depending on where it is?
  1017. */
  1018. static unsigned long __init ram_alignment(resource_size_t pos)
  1019. {
  1020. unsigned long mb = pos >> 20;
  1021. /* To 64kB in the first megabyte */
  1022. if (!mb)
  1023. return 64*1024;
  1024. /* To 1MB in the first 16MB */
  1025. if (mb < 16)
  1026. return 1024*1024;
  1027. /* To 64MB for anything above that */
  1028. return 64*1024*1024;
  1029. }
  1030. #define MAX_RESOURCE_SIZE ((resource_size_t)-1)
  1031. void __init e820__reserve_resources_late(void)
  1032. {
  1033. int i;
  1034. struct resource *res;
  1035. res = e820_res;
  1036. for (i = 0; i < e820_table->nr_entries; i++) {
  1037. if (!res->parent && res->end)
  1038. insert_resource_expand_to_fit(&iomem_resource, res);
  1039. res++;
  1040. }
  1041. /*
  1042. * Try to bump up RAM regions to reasonable boundaries, to
  1043. * avoid stolen RAM:
  1044. */
  1045. for (i = 0; i < e820_table->nr_entries; i++) {
  1046. struct e820_entry *entry = &e820_table->entries[i];
  1047. u64 start, end;
  1048. if (entry->type != E820_TYPE_RAM)
  1049. continue;
  1050. start = entry->addr + entry->size;
  1051. end = round_up(start, ram_alignment(start)) - 1;
  1052. if (end > MAX_RESOURCE_SIZE)
  1053. end = MAX_RESOURCE_SIZE;
  1054. if (start >= end)
  1055. continue;
  1056. printk(KERN_DEBUG "e820: reserve RAM buffer [mem %#010llx-%#010llx]\n", start, end);
  1057. reserve_region_with_split(&iomem_resource, start, end, "RAM buffer");
  1058. }
  1059. }
  1060. /*
  1061. * Pass the firmware (bootloader) E820 map to the kernel and process it:
  1062. */
  1063. char *__init e820__memory_setup_default(void)
  1064. {
  1065. char *who = "BIOS-e820";
  1066. /*
  1067. * Try to copy the BIOS-supplied E820-map.
  1068. *
  1069. * Otherwise fake a memory map; one section from 0k->640k,
  1070. * the next section from 1mb->appropriate_mem_k
  1071. */
  1072. if (append_e820_table(boot_params.e820_table, boot_params.e820_entries) < 0) {
  1073. u64 mem_size;
  1074. /* Compare results from other methods and take the one that gives more RAM: */
  1075. if (boot_params.alt_mem_k < boot_params.screen_info.ext_mem_k) {
  1076. mem_size = boot_params.screen_info.ext_mem_k;
  1077. who = "BIOS-88";
  1078. } else {
  1079. mem_size = boot_params.alt_mem_k;
  1080. who = "BIOS-e801";
  1081. }
  1082. e820_table->nr_entries = 0;
  1083. e820__range_add(0, LOWMEMSIZE(), E820_TYPE_RAM);
  1084. e820__range_add(HIGH_MEMORY, mem_size << 10, E820_TYPE_RAM);
  1085. }
  1086. /* We just appended a lot of ranges, sanitize the table: */
  1087. e820__update_table(e820_table);
  1088. return who;
  1089. }
  1090. /*
  1091. * Calls e820__memory_setup_default() in essence to pick up the firmware/bootloader
  1092. * E820 map - with an optional platform quirk available for virtual platforms
  1093. * to override this method of boot environment processing:
  1094. */
  1095. void __init e820__memory_setup(void)
  1096. {
  1097. char *who;
  1098. /* This is a firmware interface ABI - make sure we don't break it: */
  1099. BUILD_BUG_ON(sizeof(struct boot_e820_entry) != 20);
  1100. who = x86_init.resources.memory_setup();
  1101. memcpy(e820_table_kexec, e820_table, sizeof(*e820_table_kexec));
  1102. memcpy(e820_table_firmware, e820_table, sizeof(*e820_table_firmware));
  1103. pr_info("BIOS-provided physical RAM map:\n");
  1104. e820__print_table(who);
  1105. }
  1106. void __init e820__memblock_setup(void)
  1107. {
  1108. int i;
  1109. u64 end;
  1110. /*
  1111. * The bootstrap memblock region count maximum is 128 entries
  1112. * (INIT_MEMBLOCK_REGIONS), but EFI might pass us more E820 entries
  1113. * than that - so allow memblock resizing.
  1114. *
  1115. * This is safe, because this call happens pretty late during x86 setup,
  1116. * so we know about reserved memory regions already. (This is important
  1117. * so that memblock resizing does no stomp over reserved areas.)
  1118. */
  1119. memblock_allow_resize();
  1120. for (i = 0; i < e820_table->nr_entries; i++) {
  1121. struct e820_entry *entry = &e820_table->entries[i];
  1122. end = entry->addr + entry->size;
  1123. if (end != (resource_size_t)end)
  1124. continue;
  1125. if (entry->type == E820_TYPE_SOFT_RESERVED)
  1126. memblock_reserve(entry->addr, entry->size);
  1127. if (entry->type != E820_TYPE_RAM && entry->type != E820_TYPE_RESERVED_KERN)
  1128. continue;
  1129. memblock_add(entry->addr, entry->size);
  1130. }
  1131. /* Throw away partial pages: */
  1132. memblock_trim_memory(PAGE_SIZE);
  1133. memblock_dump_all();
  1134. }