2 * Low level x86 E820 memory map handling functions.
4 * The firmware and bootloader passes us the "E820 table", which is the primary
5 * physical memory layout description available about x86 systems.
7 * The kernel takes the E820 memory layout and optionally modifies it with
8 * quirks and other tweaks, and feeds that into the generic Linux memory
9 * allocation code routines via a platform independent interface (memblock, etc.).
11 #include <linux/crash_dump.h>
12 #include <linux/bootmem.h>
13 #include <linux/suspend.h>
14 #include <linux/acpi.h>
15 #include <linux/firmware-map.h>
16 #include <linux/memblock.h>
17 #include <linux/sort.h>
19 #include <asm/e820/api.h>
20 #include <asm/setup.h>
23 * We organize the E820 table into two main data structures:
25 * - 'e820_table_firmware': the original firmware version passed to us by the
26 * bootloader - not modified by the kernel. We use this to:
28 * - inform the user about the firmware's notion of memory layout
29 * via /sys/firmware/memmap
31 * - the hibernation code uses it to generate a kernel-independent MD5
32 * fingerprint of the physical memory layout of a system.
34 * - kexec, which is a bootloader in disguise, uses the original E820
35 * layout to pass to the kexec-ed kernel. This way the original kernel
36 * can have a restricted E820 map while the kexec()-ed kexec-kernel
37 * can have access to full memory - etc.
39 * - 'e820_table': this is the main E820 table that is massaged by the
40 * low level x86 platform code, or modified by boot parameters, before
41 * passed on to higher level MM layers.
43 * Once the E820 map has been converted to the standard Linux memory layout
44 * information its role stops - modifying it has no effect and does not get
45 * re-propagated. So itsmain role is a temporary bootstrap storage of firmware
46 * specific memory layout data during early bootup.
48 static struct e820_table e820_table_init __initdata;
49 static struct e820_table e820_table_firmware_init __initdata;
51 struct e820_table *e820_table __refdata = &e820_table_init;
52 struct e820_table *e820_table_firmware __refdata = &e820_table_firmware_init;
54 /* For PCI or other memory-mapped resources */
55 unsigned long pci_mem_start = 0xaeedbabe;
57 EXPORT_SYMBOL(pci_mem_start);
61 * This function checks if any part of the range <start,end> is mapped
64 bool e820__mapped_any(u64 start, u64 end, enum e820_type type)
68 for (i = 0; i < e820_table->nr_entries; i++) {
69 struct e820_entry *entry = &e820_table->entries[i];
71 if (type && entry->type != type)
73 if (entry->addr >= end || entry->addr + entry->size <= start)
79 EXPORT_SYMBOL_GPL(e820__mapped_any);
82 * This function checks if the entire <start,end> range is mapped with 'type'.
84 * Note: this function only works correctly once the E820 table is sorted and
85 * not-overlapping (at least for the range specified), which is the case normally.
87 bool __init e820__mapped_all(u64 start, u64 end, enum e820_type type)
91 for (i = 0; i < e820_table->nr_entries; i++) {
92 struct e820_entry *entry = &e820_table->entries[i];
94 if (type && entry->type != type)
97 /* Is the region (part) in overlap with the current region? */
98 if (entry->addr >= end || entry->addr + entry->size <= start)
102 * If the region is at the beginning of <start,end> we move
103 * 'start' to the end of the region since it's ok until there
105 if (entry->addr <= start)
106 start = entry->addr + entry->size;
109 * If 'start' is now at or beyond 'end', we're done, full
110 * coverage of the desired range exists:
119 * Add a memory region to the kernel E820 map.
121 static void __init __e820__range_add(struct e820_table *table, u64 start, u64 size, enum e820_type type)
123 int x = table->nr_entries;
125 if (x >= ARRAY_SIZE(table->entries)) {
126 pr_err("e820: too many entries; ignoring [mem %#010llx-%#010llx]\n", start, start + size - 1);
130 table->entries[x].addr = start;
131 table->entries[x].size = size;
132 table->entries[x].type = type;
136 void __init e820__range_add(u64 start, u64 size, enum e820_type type)
138 __e820__range_add(e820_table, start, size, type);
141 static void __init e820_print_type(enum e820_type type)
144 case E820_TYPE_RAM: /* Fall through: */
145 case E820_TYPE_RESERVED_KERN: pr_cont("usable"); break;
146 case E820_TYPE_RESERVED: pr_cont("reserved"); break;
147 case E820_TYPE_ACPI: pr_cont("ACPI data"); break;
148 case E820_TYPE_NVS: pr_cont("ACPI NVS"); break;
149 case E820_TYPE_UNUSABLE: pr_cont("unusable"); break;
150 case E820_TYPE_PMEM: /* Fall through: */
151 case E820_TYPE_PRAM: pr_cont("persistent (type %u)", type); break;
152 default: pr_cont("type %u", type); break;
156 void __init e820__print_table(char *who)
160 for (i = 0; i < e820_table->nr_entries; i++) {
161 pr_info("%s: [mem %#018Lx-%#018Lx] ", who,
162 e820_table->entries[i].addr,
163 e820_table->entries[i].addr + e820_table->entries[i].size - 1);
165 e820_print_type(e820_table->entries[i].type);
171 * Sanitize an E820 map.
173 * Some E820 layouts include overlapping entries. The following
174 * replaces the original E820 map with a new one, removing overlaps,
175 * and resolving conflicting memory types in favor of highest
178 * The input parameter 'entries' points to an array of 'struct
179 * e820_entry' which on entry has elements in the range [0, *nr_entries)
180 * valid, and which has space for up to max_nr_entries entries.
181 * On return, the resulting sanitized E820 map entries will be in
182 * overwritten in the same location, starting at 'entries'.
184 * The integer pointed to by nr_entries must be valid on entry (the
185 * current number of valid entries located at 'entries'). If the
186 * sanitizing succeeds the *nr_entries will be updated with the new
187 * number of valid entries (something no more than max_nr_entries).
189 * The return value from e820__update_table() is zero if it
190 * successfully 'sanitized' the map entries passed in, and is -1
191 * if it did nothing, which can happen if either of (1) it was
192 * only passed one map entry, or (2) any of the input map entries
193 * were invalid (start + size < start, meaning that the size was
194 * so big the described memory range wrapped around through zero.)
196 * Visually we're performing the following
197 * (1,2,3,4 = memory types)...
199 * Sample memory map (w/overlaps):
200 * ____22__________________
201 * ______________________4_
202 * ____1111________________
203 * _44_____________________
204 * 11111111________________
205 * ____________________33__
206 * ___________44___________
207 * __________33333_________
208 * ______________22________
209 * ___________________2222_
210 * _________111111111______
211 * _____________________11_
212 * _________________4______
214 * Sanitized equivalent (no overlap):
215 * 1_______________________
216 * _44_____________________
217 * ___1____________________
218 * ____22__________________
219 * ______11________________
220 * _________1______________
221 * __________3_____________
222 * ___________44___________
223 * _____________33_________
224 * _______________2________
225 * ________________1_______
226 * _________________4______
227 * ___________________2____
228 * ____________________33__
229 * ______________________4_
231 struct change_member {
232 /* Pointer to the original entry: */
233 struct e820_entry *entry;
234 /* Address for this change point: */
235 unsigned long long addr;
238 static struct change_member change_point_list[2*E820_MAX_ENTRIES] __initdata;
239 static struct change_member *change_point[2*E820_MAX_ENTRIES] __initdata;
240 static struct e820_entry *overlap_list[E820_MAX_ENTRIES] __initdata;
241 static struct e820_entry new_entries[E820_MAX_ENTRIES] __initdata;
243 static int __init cpcompare(const void *a, const void *b)
245 struct change_member * const *app = a, * const *bpp = b;
246 const struct change_member *ap = *app, *bp = *bpp;
249 * Inputs are pointers to two elements of change_point[]. If their
250 * addresses are not equal, their difference dominates. If the addresses
251 * are equal, then consider one that represents the end of its region
252 * to be greater than one that does not.
254 if (ap->addr != bp->addr)
255 return ap->addr > bp->addr ? 1 : -1;
257 return (ap->addr != ap->entry->addr) - (bp->addr != bp->entry->addr);
260 int __init e820__update_table(struct e820_table *table)
262 struct e820_entry *entries = table->entries;
263 u32 max_nr_entries = ARRAY_SIZE(table->entries);
264 enum e820_type current_type, last_type;
265 unsigned long long last_addr;
266 u32 new_nr_entries, overlap_entries;
267 u32 i, chg_idx, chg_nr;
269 /* If there's only one memory region, don't bother: */
270 if (table->nr_entries < 2)
273 BUG_ON(table->nr_entries > max_nr_entries);
275 /* Bail out if we find any unreasonable addresses in the map: */
276 for (i = 0; i < table->nr_entries; i++) {
277 if (entries[i].addr + entries[i].size < entries[i].addr)
281 /* Create pointers for initial change-point information (for sorting): */
282 for (i = 0; i < 2 * table->nr_entries; i++)
283 change_point[i] = &change_point_list[i];
286 * Record all known change-points (starting and ending addresses),
287 * omitting empty memory regions:
290 for (i = 0; i < table->nr_entries; i++) {
291 if (entries[i].size != 0) {
292 change_point[chg_idx]->addr = entries[i].addr;
293 change_point[chg_idx++]->entry = &entries[i];
294 change_point[chg_idx]->addr = entries[i].addr + entries[i].size;
295 change_point[chg_idx++]->entry = &entries[i];
300 /* Sort change-point list by memory addresses (low -> high): */
301 sort(change_point, chg_nr, sizeof(*change_point), cpcompare, NULL);
303 /* Create a new memory map, removing overlaps: */
304 overlap_entries = 0; /* Number of entries in the overlap table */
305 new_nr_entries = 0; /* Index for creating new map entries */
306 last_type = 0; /* Start with undefined memory type */
307 last_addr = 0; /* Start with 0 as last starting address */
309 /* Loop through change-points, determining effect on the new map: */
310 for (chg_idx = 0; chg_idx < chg_nr; chg_idx++) {
311 /* Keep track of all overlapping entries */
312 if (change_point[chg_idx]->addr == change_point[chg_idx]->entry->addr) {
313 /* Add map entry to overlap list (> 1 entry implies an overlap) */
314 overlap_list[overlap_entries++] = change_point[chg_idx]->entry;
316 /* Remove entry from list (order independent, so swap with last): */
317 for (i = 0; i < overlap_entries; i++) {
318 if (overlap_list[i] == change_point[chg_idx]->entry)
319 overlap_list[i] = overlap_list[overlap_entries-1];
324 * If there are overlapping entries, decide which
325 * "type" to use (larger value takes precedence --
326 * 1=usable, 2,3,4,4+=unusable)
329 for (i = 0; i < overlap_entries; i++) {
330 if (overlap_list[i]->type > current_type)
331 current_type = overlap_list[i]->type;
334 /* Continue building up new map based on this information: */
335 if (current_type != last_type || current_type == E820_TYPE_PRAM) {
336 if (last_type != 0) {
337 new_entries[new_nr_entries].size = change_point[chg_idx]->addr - last_addr;
338 /* Move forward only if the new size was non-zero: */
339 if (new_entries[new_nr_entries].size != 0)
340 /* No more space left for new entries? */
341 if (++new_nr_entries >= max_nr_entries)
344 if (current_type != 0) {
345 new_entries[new_nr_entries].addr = change_point[chg_idx]->addr;
346 new_entries[new_nr_entries].type = current_type;
347 last_addr = change_point[chg_idx]->addr;
349 last_type = current_type;
353 /* Copy the new entries into the original location: */
354 memcpy(entries, new_entries, new_nr_entries*sizeof(*entries));
355 table->nr_entries = new_nr_entries;
360 static int __init __append_e820_table(struct boot_e820_entry *entries, u32 nr_entries)
362 struct boot_e820_entry *entry = entries;
365 u64 start = entry->addr;
366 u64 size = entry->size;
367 u64 end = start + size - 1;
368 u32 type = entry->type;
370 /* Ignore the entry on 64-bit overflow: */
371 if (start > end && likely(size))
374 e820__range_add(start, size, type);
383 * Copy the BIOS E820 map into a safe place.
385 * Sanity-check it while we're at it..
387 * If we're lucky and live on a modern system, the setup code
388 * will have given us a memory map that we can use to properly
389 * set up memory. If we aren't, we'll fake a memory map.
391 static int __init append_e820_table(struct boot_e820_entry *entries, u32 nr_entries)
393 /* Only one memory region (or negative)? Ignore it */
397 return __append_e820_table(entries, nr_entries);
401 __e820__range_update(struct e820_table *table, u64 start, u64 size, enum e820_type old_type, enum e820_type new_type)
405 u64 real_updated_size = 0;
407 BUG_ON(old_type == new_type);
409 if (size > (ULLONG_MAX - start))
410 size = ULLONG_MAX - start;
413 printk(KERN_DEBUG "e820: update [mem %#010Lx-%#010Lx] ", start, end - 1);
414 e820_print_type(old_type);
416 e820_print_type(new_type);
419 for (i = 0; i < table->nr_entries; i++) {
420 struct e820_entry *entry = &table->entries[i];
421 u64 final_start, final_end;
424 if (entry->type != old_type)
427 entry_end = entry->addr + entry->size;
429 /* Completely covered by new range? */
430 if (entry->addr >= start && entry_end <= end) {
431 entry->type = new_type;
432 real_updated_size += entry->size;
436 /* New range is completely covered? */
437 if (entry->addr < start && entry_end > end) {
438 __e820__range_add(table, start, size, new_type);
439 __e820__range_add(table, end, entry_end - end, entry->type);
440 entry->size = start - entry->addr;
441 real_updated_size += size;
445 /* Partially covered: */
446 final_start = max(start, entry->addr);
447 final_end = min(end, entry_end);
448 if (final_start >= final_end)
451 __e820__range_add(table, final_start, final_end - final_start, new_type);
453 real_updated_size += final_end - final_start;
456 * Left range could be head or tail, so need to update
459 entry->size -= final_end - final_start;
460 if (entry->addr < final_start)
463 entry->addr = final_end;
465 return real_updated_size;
468 u64 __init e820__range_update(u64 start, u64 size, enum e820_type old_type, enum e820_type new_type)
470 return __e820__range_update(e820_table, start, size, old_type, new_type);
473 static u64 __init e820__range_update_firmware(u64 start, u64 size, enum e820_type old_type, enum e820_type new_type)
475 return __e820__range_update(e820_table_firmware, start, size, old_type, new_type);
478 /* Remove a range of memory from the E820 table: */
479 u64 __init e820__range_remove(u64 start, u64 size, enum e820_type old_type, bool check_type)
483 u64 real_removed_size = 0;
485 if (size > (ULLONG_MAX - start))
486 size = ULLONG_MAX - start;
489 printk(KERN_DEBUG "e820: remove [mem %#010Lx-%#010Lx] ", start, end - 1);
491 e820_print_type(old_type);
494 for (i = 0; i < e820_table->nr_entries; i++) {
495 struct e820_entry *entry = &e820_table->entries[i];
496 u64 final_start, final_end;
499 if (check_type && entry->type != old_type)
502 entry_end = entry->addr + entry->size;
504 /* Completely covered? */
505 if (entry->addr >= start && entry_end <= end) {
506 real_removed_size += entry->size;
507 memset(entry, 0, sizeof(*entry));
511 /* Is the new range completely covered? */
512 if (entry->addr < start && entry_end > end) {
513 e820__range_add(end, entry_end - end, entry->type);
514 entry->size = start - entry->addr;
515 real_removed_size += size;
519 /* Partially covered: */
520 final_start = max(start, entry->addr);
521 final_end = min(end, entry_end);
522 if (final_start >= final_end)
525 real_removed_size += final_end - final_start;
528 * Left range could be head or tail, so need to update
531 entry->size -= final_end - final_start;
532 if (entry->addr < final_start)
535 entry->addr = final_end;
537 return real_removed_size;
540 void __init e820__update_table_print(void)
542 if (e820__update_table(e820_table))
545 pr_info("e820: modified physical RAM map:\n");
546 e820__print_table("modified");
549 static void __init e820__update_table_firmware(void)
551 e820__update_table(e820_table_firmware);
554 #define MAX_GAP_END 0x100000000ull
557 * Search for a gap in the E820 memory space from 0 to MAX_GAP_END (4GB).
559 static int __init e820_search_gap(unsigned long *gapstart, unsigned long *gapsize)
561 unsigned long long last = MAX_GAP_END;
562 int i = e820_table->nr_entries;
566 unsigned long long start = e820_table->entries[i].addr;
567 unsigned long long end = start + e820_table->entries[i].size;
570 * Since "last" is at most 4GB, we know we'll
571 * fit in 32 bits if this condition is true:
574 unsigned long gap = last - end;
576 if (gap >= *gapsize) {
589 * Search for the biggest gap in the low 32 bits of the E820
590 * memory space. We pass this space to the PCI subsystem, so
591 * that it can assign MMIO resources for hotplug or
592 * unconfigured devices in.
594 * Hopefully the BIOS let enough space left.
596 __init void e820__setup_pci_gap(void)
598 unsigned long gapstart, gapsize;
602 found = e820_search_gap(&gapstart, &gapsize);
606 gapstart = (max_pfn << PAGE_SHIFT) + 1024*1024;
608 "e820: Cannot find an available gap in the 32-bit address range\n"
609 "e820: PCI devices with unassigned 32-bit BARs may not work!\n");
611 gapstart = 0x10000000;
616 * e820__reserve_resources_late() protects stolen RAM already:
618 pci_mem_start = gapstart;
620 pr_info("e820: [mem %#010lx-%#010lx] available for PCI devices\n", gapstart, gapstart + gapsize - 1);
624 * Called late during init, in free_initmem().
626 * Initial e820_table and e820_table_firmware are largish __initdata arrays.
628 * Copy them to a (usually much smaller) dynamically allocated area that is
629 * sized precisely after the number of e820 entries.
631 * This is done after we've performed all the fixes and tweaks to the tables.
632 * All functions which modify them are __init functions, which won't exist
633 * after free_initmem().
635 __init void e820__reallocate_tables(void)
637 struct e820_table *n;
640 size = offsetof(struct e820_table, entries) + sizeof(struct e820_entry)*e820_table->nr_entries;
641 n = kmalloc(size, GFP_KERNEL);
643 memcpy(n, e820_table, size);
646 size = offsetof(struct e820_table, entries) + sizeof(struct e820_entry)*e820_table_firmware->nr_entries;
647 n = kmalloc(size, GFP_KERNEL);
649 memcpy(n, e820_table_firmware, size);
650 e820_table_firmware = n;
654 * Because of the small fixed size of struct boot_params, only the first
655 * 128 E820 memory entries are passed to the kernel via boot_params.e820_table,
656 * the remaining (if any) entries are passed via the SETUP_E820_EXT node of
657 * struct setup_data, which is parsed here.
659 void __init e820__memory_setup_extended(u64 phys_addr, u32 data_len)
662 struct boot_e820_entry *extmap;
663 struct setup_data *sdata;
665 sdata = early_memremap(phys_addr, data_len);
666 entries = sdata->len / sizeof(*extmap);
667 extmap = (struct boot_e820_entry *)(sdata->data);
669 __append_e820_table(extmap, entries);
670 e820__update_table(e820_table);
672 early_memunmap(sdata, data_len);
673 pr_info("e820: extended physical RAM map:\n");
674 e820__print_table("extended");
678 * Find the ranges of physical addresses that do not correspond to
679 * E820 RAM areas and register the corresponding pages as 'nosave' for
680 * hibernation (32-bit) or software suspend and suspend to RAM (64-bit).
682 * This function requires the E820 map to be sorted and without any
683 * overlapping entries.
685 void __init e820__register_nosave_regions(unsigned long limit_pfn)
688 unsigned long pfn = 0;
690 for (i = 0; i < e820_table->nr_entries; i++) {
691 struct e820_entry *entry = &e820_table->entries[i];
693 if (pfn < PFN_UP(entry->addr))
694 register_nosave_region(pfn, PFN_UP(entry->addr));
696 pfn = PFN_DOWN(entry->addr + entry->size);
698 if (entry->type != E820_TYPE_RAM && entry->type != E820_TYPE_RESERVED_KERN)
699 register_nosave_region(PFN_UP(entry->addr), pfn);
701 if (pfn >= limit_pfn)
708 * Register ACPI NVS memory regions, so that we can save/restore them during
709 * hibernation and the subsequent resume:
711 static int __init e820__register_nvs_regions(void)
715 for (i = 0; i < e820_table->nr_entries; i++) {
716 struct e820_entry *entry = &e820_table->entries[i];
718 if (entry->type == E820_TYPE_NVS)
719 acpi_nvs_register(entry->addr, entry->size);
724 core_initcall(e820__register_nvs_regions);
728 * Allocate the requested number of bytes with the requsted alignment
729 * and return (the physical address) to the caller. Also register this
730 * range in the 'firmware' E820 table as a reserved range.
732 * This allows kexec to fake a new mptable, as if it came from the real
735 u64 __init e820__memblock_alloc_reserved(u64 size, u64 align)
739 addr = __memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
741 e820__range_update_firmware(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
742 pr_info("e820: update e820_table_firmware for e820__memblock_alloc_reserved()\n");
743 e820__update_table_firmware();
750 # ifdef CONFIG_X86_PAE
751 # define MAX_ARCH_PFN (1ULL<<(36-PAGE_SHIFT))
753 # define MAX_ARCH_PFN (1ULL<<(32-PAGE_SHIFT))
755 #else /* CONFIG_X86_32 */
756 # define MAX_ARCH_PFN MAXMEM>>PAGE_SHIFT
760 * Find the highest page frame number we have available
762 static unsigned long __init e820_end_pfn(unsigned long limit_pfn, enum e820_type type)
765 unsigned long last_pfn = 0;
766 unsigned long max_arch_pfn = MAX_ARCH_PFN;
768 for (i = 0; i < e820_table->nr_entries; i++) {
769 struct e820_entry *entry = &e820_table->entries[i];
770 unsigned long start_pfn;
771 unsigned long end_pfn;
773 if (entry->type != type)
776 start_pfn = entry->addr >> PAGE_SHIFT;
777 end_pfn = (entry->addr + entry->size) >> PAGE_SHIFT;
779 if (start_pfn >= limit_pfn)
781 if (end_pfn > limit_pfn) {
782 last_pfn = limit_pfn;
785 if (end_pfn > last_pfn)
789 if (last_pfn > max_arch_pfn)
790 last_pfn = max_arch_pfn;
792 pr_info("e820: last_pfn = %#lx max_arch_pfn = %#lx\n",
793 last_pfn, max_arch_pfn);
797 unsigned long __init e820__end_of_ram_pfn(void)
799 return e820_end_pfn(MAX_ARCH_PFN, E820_TYPE_RAM);
802 unsigned long __init e820__end_of_low_ram_pfn(void)
804 return e820_end_pfn(1UL << (32 - PAGE_SHIFT), E820_TYPE_RAM);
807 static void __init early_panic(char *msg)
813 static int userdef __initdata;
815 /* The "mem=nopentium" boot option disables 4MB page tables on 32-bit kernels: */
816 static int __init parse_memopt(char *p)
823 if (!strcmp(p, "nopentium")) {
825 setup_clear_cpu_cap(X86_FEATURE_PSE);
828 pr_warn("mem=nopentium ignored! (only supported on x86_32)\n");
834 mem_size = memparse(p, &p);
836 /* Don't remove all memory when getting "mem={invalid}" parameter: */
840 e820__range_remove(mem_size, ULLONG_MAX - mem_size, E820_TYPE_RAM, 1);
844 early_param("mem", parse_memopt);
846 static int __init parse_memmap_one(char *p)
849 u64 start_at, mem_size;
854 if (!strncmp(p, "exactmap", 8)) {
855 #ifdef CONFIG_CRASH_DUMP
857 * If we are doing a crash dump, we still need to know
858 * the real memory size before the original memory map is
861 saved_max_pfn = e820__end_of_ram_pfn();
863 e820_table->nr_entries = 0;
869 mem_size = memparse(p, &p);
875 start_at = memparse(p+1, &p);
876 e820__range_add(start_at, mem_size, E820_TYPE_RAM);
877 } else if (*p == '#') {
878 start_at = memparse(p+1, &p);
879 e820__range_add(start_at, mem_size, E820_TYPE_ACPI);
880 } else if (*p == '$') {
881 start_at = memparse(p+1, &p);
882 e820__range_add(start_at, mem_size, E820_TYPE_RESERVED);
883 } else if (*p == '!') {
884 start_at = memparse(p+1, &p);
885 e820__range_add(start_at, mem_size, E820_TYPE_PRAM);
887 e820__range_remove(mem_size, ULLONG_MAX - mem_size, E820_TYPE_RAM, 1);
890 return *p == '\0' ? 0 : -EINVAL;
893 static int __init parse_memmap_opt(char *str)
896 char *k = strchr(str, ',');
901 parse_memmap_one(str);
907 early_param("memmap", parse_memmap_opt);
910 * Reserve all entries from the bootloader's extensible data nodes list,
911 * because if present we are going to use it later on to fetch e820
914 void __init e820__reserve_setup_data(void)
916 struct setup_data *data;
919 pa_data = boot_params.hdr.setup_data;
924 data = early_memremap(pa_data, sizeof(*data));
925 e820__range_update(pa_data, sizeof(*data)+data->len, E820_TYPE_RAM, E820_TYPE_RESERVED_KERN);
926 pa_data = data->next;
927 early_memunmap(data, sizeof(*data));
930 e820__update_table(e820_table);
932 memcpy(e820_table_firmware, e820_table, sizeof(*e820_table_firmware));
934 pr_info("extended physical RAM map:\n");
935 e820__print_table("reserve setup_data");
939 * Called after parse_early_param(), after early parameters (such as mem=)
940 * have been processed, in which case we already have an E820 table filled in
941 * via the parameter callback function(s), but it's not sorted and printed yet:
943 void __init e820__finish_early_params(void)
946 if (e820__update_table(e820_table) < 0)
947 early_panic("Invalid user supplied memory map");
949 pr_info("e820: user-defined physical RAM map:\n");
950 e820__print_table("user");
954 static const char *__init e820_type_to_string(struct e820_entry *entry)
956 switch (entry->type) {
957 case E820_TYPE_RESERVED_KERN: /* Fall-through: */
958 case E820_TYPE_RAM: return "System RAM";
959 case E820_TYPE_ACPI: return "ACPI Tables";
960 case E820_TYPE_NVS: return "ACPI Non-volatile Storage";
961 case E820_TYPE_UNUSABLE: return "Unusable memory";
962 case E820_TYPE_PRAM: return "Persistent Memory (legacy)";
963 case E820_TYPE_PMEM: return "Persistent Memory";
964 case E820_TYPE_RESERVED: return "Reserved";
965 default: return "Unknown E820 type";
969 static unsigned long __init e820_type_to_iomem_type(struct e820_entry *entry)
971 switch (entry->type) {
972 case E820_TYPE_RESERVED_KERN: /* Fall-through: */
973 case E820_TYPE_RAM: return IORESOURCE_SYSTEM_RAM;
974 case E820_TYPE_ACPI: /* Fall-through: */
975 case E820_TYPE_NVS: /* Fall-through: */
976 case E820_TYPE_UNUSABLE: /* Fall-through: */
977 case E820_TYPE_PRAM: /* Fall-through: */
978 case E820_TYPE_PMEM: /* Fall-through: */
979 case E820_TYPE_RESERVED: /* Fall-through: */
980 default: return IORESOURCE_MEM;
984 static unsigned long __init e820_type_to_iores_desc(struct e820_entry *entry)
986 switch (entry->type) {
987 case E820_TYPE_ACPI: return IORES_DESC_ACPI_TABLES;
988 case E820_TYPE_NVS: return IORES_DESC_ACPI_NV_STORAGE;
989 case E820_TYPE_PMEM: return IORES_DESC_PERSISTENT_MEMORY;
990 case E820_TYPE_PRAM: return IORES_DESC_PERSISTENT_MEMORY_LEGACY;
991 case E820_TYPE_RESERVED_KERN: /* Fall-through: */
992 case E820_TYPE_RAM: /* Fall-through: */
993 case E820_TYPE_UNUSABLE: /* Fall-through: */
994 case E820_TYPE_RESERVED: /* Fall-through: */
995 default: return IORES_DESC_NONE;
999 static bool __init do_mark_busy(enum e820_type type, struct resource *res)
1001 /* this is the legacy bios/dos rom-shadow + mmio region */
1002 if (res->start < (1ULL<<20))
1006 * Treat persistent memory like device memory, i.e. reserve it
1007 * for exclusive use of a driver
1010 case E820_TYPE_RESERVED:
1011 case E820_TYPE_PRAM:
1012 case E820_TYPE_PMEM:
1014 case E820_TYPE_RESERVED_KERN:
1016 case E820_TYPE_ACPI:
1018 case E820_TYPE_UNUSABLE:
1025 * Mark E820 reserved areas as busy for the resource manager:
1028 static struct resource __initdata *e820_res;
1030 void __init e820__reserve_resources(void)
1033 struct resource *res;
1036 res = alloc_bootmem(sizeof(*res) * e820_table->nr_entries);
1039 for (i = 0; i < e820_table->nr_entries; i++) {
1040 struct e820_entry *entry = e820_table->entries + i;
1042 end = entry->addr + entry->size - 1;
1043 if (end != (resource_size_t)end) {
1047 res->start = entry->addr;
1049 res->name = e820_type_to_string(entry);
1050 res->flags = e820_type_to_iomem_type(entry);
1051 res->desc = e820_type_to_iores_desc(entry);
1054 * Don't register the region that could be conflicted with
1055 * PCI device BAR resources and insert them later in
1056 * pcibios_resource_survey():
1058 if (do_mark_busy(entry->type, res)) {
1059 res->flags |= IORESOURCE_BUSY;
1060 insert_resource(&iomem_resource, res);
1065 for (i = 0; i < e820_table_firmware->nr_entries; i++) {
1066 struct e820_entry *entry = e820_table_firmware->entries + i;
1068 firmware_map_add_early(entry->addr, entry->addr + entry->size, e820_type_to_string(entry));
1073 * How much should we pad the end of RAM, depending on where it is?
1075 static unsigned long __init ram_alignment(resource_size_t pos)
1077 unsigned long mb = pos >> 20;
1079 /* To 64kB in the first megabyte */
1083 /* To 1MB in the first 16MB */
1087 /* To 64MB for anything above that */
1088 return 64*1024*1024;
1091 #define MAX_RESOURCE_SIZE ((resource_size_t)-1)
1093 void __init e820__reserve_resources_late(void)
1096 struct resource *res;
1099 for (i = 0; i < e820_table->nr_entries; i++) {
1100 if (!res->parent && res->end)
1101 insert_resource_expand_to_fit(&iomem_resource, res);
1106 * Try to bump up RAM regions to reasonable boundaries, to
1109 for (i = 0; i < e820_table->nr_entries; i++) {
1110 struct e820_entry *entry = &e820_table->entries[i];
1113 if (entry->type != E820_TYPE_RAM)
1116 start = entry->addr + entry->size;
1117 end = round_up(start, ram_alignment(start)) - 1;
1118 if (end > MAX_RESOURCE_SIZE)
1119 end = MAX_RESOURCE_SIZE;
1123 printk(KERN_DEBUG "e820: reserve RAM buffer [mem %#010llx-%#010llx]\n", start, end);
1124 reserve_region_with_split(&iomem_resource, start, end, "RAM buffer");
1129 * Pass the firmware (bootloader) E820 map to the kernel and process it:
1131 char *__init e820__memory_setup_default(void)
1133 char *who = "BIOS-e820";
1136 * Try to copy the BIOS-supplied E820-map.
1138 * Otherwise fake a memory map; one section from 0k->640k,
1139 * the next section from 1mb->appropriate_mem_k
1141 if (append_e820_table(boot_params.e820_table, boot_params.e820_entries) < 0) {
1144 /* Compare results from other methods and take the one that gives more RAM: */
1145 if (boot_params.alt_mem_k < boot_params.screen_info.ext_mem_k) {
1146 mem_size = boot_params.screen_info.ext_mem_k;
1149 mem_size = boot_params.alt_mem_k;
1153 e820_table->nr_entries = 0;
1154 e820__range_add(0, LOWMEMSIZE(), E820_TYPE_RAM);
1155 e820__range_add(HIGH_MEMORY, mem_size << 10, E820_TYPE_RAM);
1158 /* We just appended a lot of ranges, sanitize the table: */
1159 e820__update_table(e820_table);
1165 * Calls e820__memory_setup_default() in essence to pick up the firmware/bootloader
1166 * E820 map - with an optional platform quirk available for virtual platforms
1167 * to override this method of boot environment processing:
1169 void __init e820__memory_setup(void)
1173 /* This is a firmware interface ABI - make sure we don't break it: */
1174 BUILD_BUG_ON(sizeof(struct boot_e820_entry) != 20);
1176 who = x86_init.resources.memory_setup();
1178 memcpy(e820_table_firmware, e820_table, sizeof(*e820_table_firmware));
1180 pr_info("e820: BIOS-provided physical RAM map:\n");
1181 e820__print_table(who);
1184 void __init e820__memblock_setup(void)
1190 * The bootstrap memblock region count maximum is 128 entries
1191 * (INIT_MEMBLOCK_REGIONS), but EFI might pass us more E820 entries
1192 * than that - so allow memblock resizing.
1194 * This is safe, because this call happens pretty late during x86 setup,
1195 * so we know about reserved memory regions already. (This is important
1196 * so that memblock resizing does no stomp over reserved areas.)
1198 memblock_allow_resize();
1200 for (i = 0; i < e820_table->nr_entries; i++) {
1201 struct e820_entry *entry = &e820_table->entries[i];
1203 end = entry->addr + entry->size;
1204 if (end != (resource_size_t)end)
1207 if (entry->type != E820_TYPE_RAM && entry->type != E820_TYPE_RESERVED_KERN)
1210 memblock_add(entry->addr, entry->size);
1213 /* Throw away partial pages: */
1214 memblock_trim_memory(PAGE_SIZE);
1216 memblock_dump_all();