2 * Handle the memory map.
3 * The functions here do the job until bootmem takes over.
5 * Getting sanitize_e820_map() in sync with i386 version by applying change:
6 * - Provisions for empty E820 memory regions (reported by certain BIOSes).
7 * Alex Achenbach <xela@slit.de>, December 2002.
8 * Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
11 #include <linux/kernel.h>
12 #include <linux/types.h>
13 #include <linux/init.h>
14 #include <linux/crash_dump.h>
15 #include <linux/export.h>
16 #include <linux/bootmem.h>
17 #include <linux/pfn.h>
18 #include <linux/suspend.h>
19 #include <linux/acpi.h>
20 #include <linux/firmware-map.h>
21 #include <linux/memblock.h>
22 #include <linux/sort.h>
25 #include <asm/proto.h>
26 #include <asm/setup.h>
27 #include <xen/interface/memory.h>
30 * The e820 map is the map that gets modified e.g. with command line parameters
31 * and that is also registered with modifications in the kernel resource tree
32 * with the iomem_resource as parent.
34 * The e820_saved is directly saved after the BIOS-provided memory map is
35 * copied. It doesn't get modified afterwards. It's registered for the
36 * /sys/firmware/memmap interface.
38 * That memory map is not modified and is used as base for kexec. The kexec'd
39 * kernel should get the same memory map as the firmware provides. Then the
40 * user can e.g. boot the original kernel with mem=1G while still booting the
41 * next kernel with full memory.
44 #if !defined(CONFIG_XEN)
45 struct e820map e820_saved;
46 #elif defined(CONFIG_XEN_PRIVILEGED_GUEST)
47 struct e820map machine_e820;
48 # define e820_saved machine_e820
50 # define machine_e820 e820
51 # define e820_saved e820
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
65 e820_any_mapped(u64 start, u64 end, unsigned type)
70 for (i = 0; i < e820.nr_map; i++) {
71 struct e820entry *ei = &e820.map[i];
73 if (!is_initial_xendomain())
75 for (i = 0; i < machine_e820.nr_map; ++i) {
76 const struct e820entry *ei = &machine_e820.map[i];
79 if (type && ei->type != type)
81 if (ei->addr >= end || ei->addr + ei->size <= start)
87 EXPORT_SYMBOL_GPL(e820_any_mapped);
90 * This function checks if the entire range <start,end> is mapped with type.
92 * Note: this function only works correct if the e820 table is sorted and
93 * not-overlapping, which is the case
95 int __init e820_all_mapped(u64 start, u64 end, unsigned type)
100 for (i = 0; i < e820.nr_map; i++) {
101 struct e820entry *ei = &e820.map[i];
103 if (!is_initial_xendomain())
105 for (i = 0; i < machine_e820.nr_map; ++i) {
106 const struct e820entry *ei = &machine_e820.map[i];
109 if (type && ei->type != type)
111 /* is the region (part) in overlap with the current region ?*/
112 if (ei->addr >= end || ei->addr + ei->size <= start)
115 /* if the region is at the beginning of <start,end> we move
116 * start to the end of the region since it's ok until there
118 if (ei->addr <= start)
119 start = ei->addr + ei->size;
121 * if start is now at or beyond end, we're done, full
131 * Add a memory region to the kernel e820 map.
133 static void __init __e820_add_region(struct e820map *e820x, u64 start, u64 size,
136 int x = e820x->nr_map;
138 if (x >= ARRAY_SIZE(e820x->map)) {
139 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
143 e820x->map[x].addr = start;
144 e820x->map[x].size = size;
145 e820x->map[x].type = type;
149 void __init e820_add_region(u64 start, u64 size, int type)
151 __e820_add_region(&e820, start, size, type);
154 static void __init e820_print_type(u32 type)
158 case E820_RESERVED_KERN:
159 printk(KERN_CONT "(usable)");
162 printk(KERN_CONT "(reserved)");
165 printk(KERN_CONT "(ACPI data)");
168 printk(KERN_CONT "(ACPI NVS)");
171 printk(KERN_CONT "(unusable)");
174 printk(KERN_CONT "type %u", type);
179 static void __init _e820_print_map(const struct e820map *e820, const char *who)
183 for (i = 0; i < e820->nr_map; i++) {
184 printk(KERN_INFO " %s: %016Lx - %016Lx ", who,
185 (unsigned long long) e820->map[i].addr,
187 (e820->map[i].addr + e820->map[i].size));
188 e820_print_type(e820->map[i].type);
189 printk(KERN_CONT "\n");
194 * Sanitize the BIOS e820 map.
196 * Some e820 responses include overlapping entries. The following
197 * replaces the original e820 map with a new one, removing overlaps,
198 * and resolving conflicting memory types in favor of highest
201 * The input parameter biosmap points to an array of 'struct
202 * e820entry' which on entry has elements in the range [0, *pnr_map)
203 * valid, and which has space for up to max_nr_map entries.
204 * On return, the resulting sanitized e820 map entries will be in
205 * overwritten in the same location, starting at biosmap.
207 * The integer pointed to by pnr_map must be valid on entry (the
208 * current number of valid entries located at biosmap) and will
209 * be updated on return, with the new number of valid entries
210 * (something no more than max_nr_map.)
212 * The return value from sanitize_e820_map() is zero if it
213 * successfully 'sanitized' the map entries passed in, and is -1
214 * if it did nothing, which can happen if either of (1) it was
215 * only passed one map entry, or (2) any of the input map entries
216 * were invalid (start + size < start, meaning that the size was
217 * so big the described memory range wrapped around through zero.)
219 * Visually we're performing the following
220 * (1,2,3,4 = memory types)...
222 * Sample memory map (w/overlaps):
223 * ____22__________________
224 * ______________________4_
225 * ____1111________________
226 * _44_____________________
227 * 11111111________________
228 * ____________________33__
229 * ___________44___________
230 * __________33333_________
231 * ______________22________
232 * ___________________2222_
233 * _________111111111______
234 * _____________________11_
235 * _________________4______
237 * Sanitized equivalent (no overlap):
238 * 1_______________________
239 * _44_____________________
240 * ___1____________________
241 * ____22__________________
242 * ______11________________
243 * _________1______________
244 * __________3_____________
245 * ___________44___________
246 * _____________33_________
247 * _______________2________
248 * ________________1_______
249 * _________________4______
250 * ___________________2____
251 * ____________________33__
252 * ______________________4_
254 struct change_member {
255 struct e820entry *pbios; /* pointer to original bios entry */
256 unsigned long long addr; /* address for this change point */
259 static int __init cpcompare(const void *a, const void *b)
261 struct change_member * const *app = a, * const *bpp = b;
262 const struct change_member *ap = *app, *bp = *bpp;
265 * Inputs are pointers to two elements of change_point[]. If their
266 * addresses are unequal, their difference dominates. If the addresses
267 * are equal, then consider one that represents the end of its region
268 * to be greater than one that does not.
270 if (ap->addr != bp->addr)
271 return ap->addr > bp->addr ? 1 : -1;
273 return (ap->addr != ap->pbios->addr) - (bp->addr != bp->pbios->addr);
276 int __init sanitize_e820_map(struct e820entry *biosmap, int max_nr_map,
279 static struct change_member change_point_list[2*E820_X_MAX] __initdata;
280 static struct change_member *change_point[2*E820_X_MAX] __initdata;
281 static struct e820entry *overlap_list[E820_X_MAX] __initdata;
282 static struct e820entry new_bios[E820_X_MAX] __initdata;
283 unsigned long current_type, last_type;
284 unsigned long long last_addr;
288 int old_nr, new_nr, chg_nr;
291 /* if there's only one memory region, don't bother */
300 BUG_ON(old_nr > max_nr_map);
302 /* bail out if we find any unreasonable addresses in bios map */
303 for (i = 0; i < old_nr; i++)
304 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
307 /* create pointers for initial change-point information (for sorting) */
308 for (i = 0; i < 2 * old_nr; i++)
309 change_point[i] = &change_point_list[i];
311 /* record all known change-points (starting and ending addresses),
312 omitting those that are for empty memory regions */
314 for (i = 0; i < old_nr; i++) {
315 if (biosmap[i].size != 0) {
316 change_point[chgidx]->addr = biosmap[i].addr;
317 change_point[chgidx++]->pbios = &biosmap[i];
318 change_point[chgidx]->addr = biosmap[i].addr +
320 change_point[chgidx++]->pbios = &biosmap[i];
325 /* sort change-point list by memory addresses (low -> high) */
326 sort(change_point, chg_nr, sizeof *change_point, cpcompare, NULL);
328 /* create a new bios memory map, removing overlaps */
329 overlap_entries = 0; /* number of entries in the overlap table */
330 new_bios_entry = 0; /* index for creating new bios map entries */
331 last_type = 0; /* start with undefined memory type */
332 last_addr = 0; /* start with 0 as last starting address */
334 /* loop through change-points, determining affect on the new bios map */
335 for (chgidx = 0; chgidx < chg_nr; chgidx++) {
336 /* keep track of all overlapping bios entries */
337 if (change_point[chgidx]->addr ==
338 change_point[chgidx]->pbios->addr) {
340 * add map entry to overlap list (> 1 entry
341 * implies an overlap)
343 overlap_list[overlap_entries++] =
344 change_point[chgidx]->pbios;
347 * remove entry from list (order independent,
350 for (i = 0; i < overlap_entries; i++) {
351 if (overlap_list[i] ==
352 change_point[chgidx]->pbios)
354 overlap_list[overlap_entries-1];
359 * if there are overlapping entries, decide which
360 * "type" to use (larger value takes precedence --
361 * 1=usable, 2,3,4,4+=unusable)
364 for (i = 0; i < overlap_entries; i++)
365 if (overlap_list[i]->type > current_type)
366 current_type = overlap_list[i]->type;
368 * continue building up new bios map based on this
371 if (current_type != last_type) {
372 if (last_type != 0) {
373 new_bios[new_bios_entry].size =
374 change_point[chgidx]->addr - last_addr;
376 * move forward only if the new size
379 if (new_bios[new_bios_entry].size != 0)
381 * no more space left for new
384 if (++new_bios_entry >= max_nr_map)
387 if (current_type != 0) {
388 new_bios[new_bios_entry].addr =
389 change_point[chgidx]->addr;
390 new_bios[new_bios_entry].type = current_type;
391 last_addr = change_point[chgidx]->addr;
393 last_type = current_type;
396 /* retain count for new bios entries */
397 new_nr = new_bios_entry;
399 /* copy new bios mapping into original location */
400 memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
406 static int __init __append_e820_map(struct e820entry *biosmap, int nr_map)
409 u64 start = biosmap->addr;
410 u64 size = biosmap->size;
411 u64 end = start + size;
412 u32 type = biosmap->type;
414 /* Overflow in 64 bits? Ignore the memory map. */
418 e820_add_region(start, size, type);
427 * Copy the BIOS e820 map into a safe place.
429 * Sanity-check it while we're at it..
431 * If we're lucky and live on a modern system, the setup code
432 * will have given us a memory map that we can use to properly
433 * set up memory. If we aren't, we'll fake a memory map.
435 static int __init append_e820_map(struct e820entry *biosmap, int nr_map)
438 /* Only one memory region (or negative)? Ignore it */
445 return __append_e820_map(biosmap, nr_map);
448 static u64 __init __e820_update_range(struct e820map *e820x, u64 start,
449 u64 size, unsigned old_type,
454 u64 real_updated_size = 0;
456 BUG_ON(old_type == new_type);
458 if (size > (ULLONG_MAX - start))
459 size = ULLONG_MAX - start;
462 printk(KERN_DEBUG "e820 update range: %016Lx - %016Lx ",
463 (unsigned long long) start,
464 (unsigned long long) end);
465 e820_print_type(old_type);
466 printk(KERN_CONT " ==> ");
467 e820_print_type(new_type);
468 printk(KERN_CONT "\n");
470 for (i = 0; i < e820x->nr_map; i++) {
471 struct e820entry *ei = &e820x->map[i];
472 u64 final_start, final_end;
475 if (ei->type != old_type)
478 ei_end = ei->addr + ei->size;
479 /* totally covered by new range? */
480 if (ei->addr >= start && ei_end <= end) {
482 real_updated_size += ei->size;
486 /* new range is totally covered? */
487 if (ei->addr < start && ei_end > end) {
488 __e820_add_region(e820x, start, size, new_type);
489 __e820_add_region(e820x, end, ei_end - end, ei->type);
490 ei->size = start - ei->addr;
491 real_updated_size += size;
495 /* partially covered */
496 final_start = max(start, ei->addr);
497 final_end = min(end, ei_end);
498 if (final_start >= final_end)
501 __e820_add_region(e820x, final_start, final_end - final_start,
504 real_updated_size += final_end - final_start;
507 * left range could be head or tail, so need to update
510 ei->size -= final_end - final_start;
511 if (ei->addr < final_start)
513 ei->addr = final_end;
515 return real_updated_size;
518 u64 __init e820_update_range(u64 start, u64 size, unsigned old_type,
521 return __e820_update_range(&e820, start, size, old_type, new_type);
524 #ifndef CONFIG_XEN_UNPRIVILEGED_GUEST
525 static u64 __init e820_update_range_saved(u64 start, u64 size,
526 unsigned old_type, unsigned new_type)
529 if (!is_initial_xendomain())
531 return __e820_update_range(&machine_e820, phys_to_machine(start),
532 size, old_type, new_type);
534 return __e820_update_range(&e820_saved, start, size, old_type,
540 /* make e820 not cover the range */
541 u64 __init e820_remove_range(u64 start, u64 size, unsigned old_type,
546 u64 real_removed_size = 0;
548 if (size > (ULLONG_MAX - start))
549 size = ULLONG_MAX - start;
552 printk(KERN_DEBUG "e820 remove range: %016Lx - %016Lx ",
553 (unsigned long long) start,
554 (unsigned long long) end);
556 e820_print_type(old_type);
557 printk(KERN_CONT "\n");
559 for (i = 0; i < e820.nr_map; i++) {
560 struct e820entry *ei = &e820.map[i];
561 u64 final_start, final_end;
564 if (checktype && ei->type != old_type)
567 ei_end = ei->addr + ei->size;
568 /* totally covered? */
569 if (ei->addr >= start && ei_end <= end) {
570 real_removed_size += ei->size;
571 memset(ei, 0, sizeof(struct e820entry));
575 /* new range is totally covered? */
576 if (ei->addr < start && ei_end > end) {
577 e820_add_region(end, ei_end - end, ei->type);
578 ei->size = start - ei->addr;
579 real_removed_size += size;
583 /* partially covered */
584 final_start = max(start, ei->addr);
585 final_end = min(end, ei_end);
586 if (final_start >= final_end)
588 real_removed_size += final_end - final_start;
591 * left range could be head or tail, so need to update
594 ei->size -= final_end - final_start;
595 if (ei->addr < final_start)
597 ei->addr = final_end;
599 return real_removed_size;
602 void __init update_e820(void)
606 nr_map = e820.nr_map;
607 if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr_map))
609 e820.nr_map = nr_map;
610 printk(KERN_INFO "modified physical RAM map:\n");
611 _e820_print_map(&e820, "modified");
613 #ifndef CONFIG_XEN_UNPRIVILEGED_GUEST
614 static void __init update_e820_saved(void)
618 nr_map = e820_saved.nr_map;
619 if (sanitize_e820_map(e820_saved.map, ARRAY_SIZE(e820_saved.map), &nr_map))
621 e820_saved.nr_map = nr_map;
626 #define e820 machine_e820
629 #define MAX_GAP_END 0x100000000ull
631 * Search for a gap in the e820 memory space from start_addr to end_addr.
633 __init int e820_search_gap(unsigned long *gapstart, unsigned long *gapsize,
634 unsigned long start_addr, unsigned long long end_addr)
636 unsigned long long last;
640 last = (end_addr && end_addr < MAX_GAP_END) ? end_addr : MAX_GAP_END;
642 if (start_addr >= MAX_GAP_END)
643 last = end_addr ?: (1UL << boot_cpu_data.x86_phys_bits);
647 unsigned long long start = e820.map[i].addr;
648 unsigned long long end = start + e820.map[i].size;
650 if (end < start_addr)
654 * Since "last" is at most 4GB, we know we'll
655 * fit in 32 bits if this condition is true
658 unsigned long gap = last - end;
660 if (gap >= *gapsize) {
673 * Search for the biggest gap in the low 32 bits of the e820
674 * memory space. We pass this space to PCI to assign MMIO resources
675 * for hotplug or unconfigured devices in.
676 * Hopefully the BIOS let enough space left.
678 __init void e820_setup_gap(void)
680 unsigned long gapstart, gapsize;
683 gapstart = 0x10000000;
685 found = e820_search_gap(&gapstart, &gapsize, 0, MAX_GAP_END);
690 "PCI: Warning: Cannot find a gap in the 32bit address range\n"
691 "PCI: Unassigned devices with 32bit resource registers may break!\n");
692 found = e820_search_gap(&gapstart, &gapsize, MAX_GAP_END, 0);
698 * e820_reserve_resources_late protect stolen RAM already
700 pci_mem_start = gapstart;
703 "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
704 pci_mem_start, gapstart, gapsize);
711 * Because of the size limitation of struct boot_params, only first
712 * 128 E820 memory entries are passed to kernel via
713 * boot_params.e820_map, others are passed via SETUP_E820_EXT node of
714 * linked list of struct setup_data, which is parsed here.
716 void __init parse_e820_ext(struct setup_data *sdata)
719 struct e820entry *extmap;
721 entries = sdata->len / sizeof(struct e820entry);
722 extmap = (struct e820entry *)(sdata->data);
723 __append_e820_map(extmap, entries);
724 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
725 printk(KERN_INFO "extended physical RAM map:\n");
726 _e820_print_map(&e820, "extended");
729 #if defined(CONFIG_X86_64) || \
730 (defined(CONFIG_X86_32) && defined(CONFIG_HIBERNATION))
732 * Find the ranges of physical addresses that do not correspond to
733 * e820 RAM areas and mark the corresponding pages as nosave for
734 * hibernation (32 bit) or software suspend and suspend to RAM (64 bit).
736 * This function requires the e820 map to be sorted and without any
737 * overlapping entries and assumes the first e820 area to be RAM.
739 void __init e820_mark_nosave_regions(unsigned long limit_pfn)
744 pfn = PFN_DOWN(e820.map[0].addr + e820.map[0].size);
745 for (i = 1; i < e820.nr_map; i++) {
746 struct e820entry *ei = &e820.map[i];
748 if (pfn < PFN_UP(ei->addr))
749 register_nosave_region(pfn, PFN_UP(ei->addr));
751 pfn = PFN_DOWN(ei->addr + ei->size);
752 if (ei->type != E820_RAM && ei->type != E820_RESERVED_KERN)
753 register_nosave_region(PFN_UP(ei->addr), pfn);
755 if (pfn >= limit_pfn)
763 * Mark ACPI NVS memory region, so that we can save/restore it during
764 * hibernation and the subsequent resume.
766 static int __init e820_mark_nvs_memory(void)
770 for (i = 0; i < e820.nr_map; i++) {
771 struct e820entry *ei = &e820.map[i];
773 if (ei->type == E820_NVS)
774 acpi_nvs_register(ei->addr, ei->size);
779 core_initcall(e820_mark_nvs_memory);
783 #ifndef CONFIG_XEN_UNPRIVILEGED_GUEST
785 * pre allocated 4k and reserved it in memblock and e820_saved
787 u64 __init early_reserve_e820(u64 size, u64 align)
791 unsigned int order = get_order(size);
793 unsigned long max_initmap_pfn;
795 if (!is_initial_xendomain())
797 size = PAGE_SIZE << order;
798 if (align < PAGE_SIZE)
801 addr = __memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
803 e820_update_range_saved(addr, size, E820_RAM, E820_RESERVED);
804 printk(KERN_INFO "update e820_saved for early_reserve_e820\n");
810 max_initmap_pfn = ALIGN(PFN_UP(__pa(xen_start_info->pt_base))
811 + xen_start_info->nr_pt_frames
812 + 1 + (1 << (19 - PAGE_SHIFT)),
813 1UL << (22 - PAGE_SHIFT));
815 if ((addr >> PAGE_SHIFT)
816 < max(max_initmap_pfn, max_pfn_mapped))
817 rc = xen_create_contiguous_region((unsigned long)__va(addr),
820 if ((addr >> PAGE_SHIFT) < max_pfn_mapped)
821 rc = xen_create_contiguous_region((unsigned long)__va(addr),
823 else if ((addr >> PAGE_SHIFT) < max_initmap_pfn)
824 rc = xen_create_contiguous_region(__START_KERNEL_map + addr,
828 rc = early_create_contiguous_region(addr >> PAGE_SHIFT,
839 # ifdef CONFIG_X86_PAE
840 # define MAX_ARCH_PFN (1ULL<<(40-PAGE_SHIFT))
842 # define MAX_ARCH_PFN (1ULL<<(32-PAGE_SHIFT))
844 #else /* CONFIG_X86_32 */
845 # define MAX_ARCH_PFN MAXMEM>>PAGE_SHIFT
849 * Find the highest page frame number we have available
851 static unsigned long __init e820_end_pfn(unsigned long limit_pfn, unsigned type)
854 unsigned long last_pfn = 0;
855 unsigned long max_arch_pfn = MAX_ARCH_PFN;
857 for (i = 0; i < e820.nr_map; i++) {
858 struct e820entry *ei = &e820.map[i];
859 unsigned long start_pfn;
860 unsigned long end_pfn;
862 if (ei->type != type)
865 start_pfn = ei->addr >> PAGE_SHIFT;
866 end_pfn = (ei->addr + ei->size) >> PAGE_SHIFT;
868 if (start_pfn >= limit_pfn)
870 if (end_pfn > limit_pfn) {
871 last_pfn = limit_pfn;
874 if (end_pfn > last_pfn)
878 if (last_pfn > max_arch_pfn)
879 last_pfn = max_arch_pfn;
881 printk(KERN_INFO "last_pfn = %#lx max_arch_pfn = %#lx\n",
882 last_pfn, max_arch_pfn);
885 unsigned long __init e820_end_of_ram_pfn(void)
887 return e820_end_pfn(MAX_ARCH_PFN, E820_RAM);
890 unsigned long __init e820_end_of_low_ram_pfn(void)
892 return e820_end_pfn(1UL<<(32 - PAGE_SHIFT), E820_RAM);
895 static void early_panic(char *msg)
901 static int userdef __initdata;
903 /* "mem=nopentium" disables the 4MB page tables. */
904 static int __init parse_memopt(char *p)
906 u64 mem_size, current_end;
913 if (!strcmp(p, "nopentium")) {
915 setup_clear_cpu_cap(X86_FEATURE_PSE);
918 printk(KERN_WARNING "mem=nopentium ignored! (only supported on x86_32)\n");
925 mem_size = memparse(p, &p);
926 /* don't remove all of memory when handling "mem={invalid}" param */
931 * A little less than 2% of available memory are needed for page
932 * tables, p2m map, and mem_map. Hence the maximum amount of memory
933 * we can potentially balloon up to can in no case exceed about 50
934 * times of what we've been given initially. Since even with that we
935 * won't be able to boot (due to various calculations done based on
936 * the total number of pages) we further restrict this to factor 32.
938 if ((mem_size >> (PAGE_SHIFT + 5)) > xen_start_info->nr_pages) {
939 u64 size = (u64)xen_start_info->nr_pages << 5;
941 pr_warn("mem=%Luk is invalid for an initial"
942 " allocation of %luk, using %Luk\n",
943 (unsigned long long)mem_size >> 10,
944 xen_start_info->nr_pages << (PAGE_SHIFT - 10),
945 (unsigned long long)size << (PAGE_SHIFT - 10));
946 mem_size = size << PAGE_SHIFT;
949 e820_remove_range(mem_size, ULLONG_MAX - mem_size, E820_RAM, 1);
952 current_end = e820.map[i].addr + e820.map[i].size;
953 if (current_end < mem_size) {
955 * The e820 map ends before our requested size so
956 * extend the final entry to the requested address.
958 if (e820.map[i].type == E820_RAM)
959 e820.map[i].size = mem_size - e820.map[i].addr;
961 e820_add_region(current_end, mem_size - current_end, E820_RAM);
966 early_param("mem", parse_memopt);
969 static int __init parse_memmap_opt(char *p)
972 u64 start_at, mem_size;
977 if (!strncmp(p, "exactmap", 8)) {
978 #ifdef CONFIG_CRASH_DUMP
980 * If we are doing a crash dump, we still need to know
981 * the real mem size before original memory map is
984 saved_max_pfn = e820_end_of_ram_pfn();
992 mem_size = memparse(p, &p);
998 start_at = memparse(p+1, &p);
999 e820_add_region(start_at, mem_size, E820_RAM);
1000 } else if (*p == '#') {
1001 start_at = memparse(p+1, &p);
1002 e820_add_region(start_at, mem_size, E820_ACPI);
1003 } else if (*p == '$') {
1004 start_at = memparse(p+1, &p);
1005 e820_add_region(start_at, mem_size, E820_RESERVED);
1007 e820_remove_range(mem_size, ULLONG_MAX - mem_size, E820_RAM, 1);
1009 return *p == '\0' ? 0 : -EINVAL;
1011 early_param("memmap", parse_memmap_opt);
1014 void __init finish_e820_parsing(void)
1017 u32 nr = e820.nr_map;
1019 if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr) < 0)
1020 early_panic("Invalid user supplied memory map");
1023 printk(KERN_INFO "user-defined physical RAM map:\n");
1024 _e820_print_map(&e820, "user");
1028 static inline const char *e820_type_to_string(int e820_type)
1030 switch (e820_type) {
1031 case E820_RESERVED_KERN:
1032 case E820_RAM: return "System RAM";
1033 case E820_ACPI: return "ACPI Tables";
1034 case E820_NVS: return "ACPI Non-volatile Storage";
1035 case E820_UNUSABLE: return "Unusable memory";
1036 default: return "reserved";
1041 #define e820 machine_e820
1045 * Mark e820 reserved areas as busy for the resource manager.
1047 static struct resource __initdata *e820_res;
1048 void __init e820_reserve_resources(void)
1051 struct resource *res;
1054 res = alloc_bootmem(sizeof(struct resource) * e820.nr_map);
1056 for (i = 0; i < e820.nr_map; i++) {
1057 end = e820.map[i].addr + e820.map[i].size - 1;
1058 if (end != (resource_size_t)end) {
1062 res->name = e820_type_to_string(e820.map[i].type);
1063 res->start = e820.map[i].addr;
1066 res->flags = IORESOURCE_MEM;
1069 * don't register the region that could be conflicted with
1070 * pci device BAR resource and insert them later in
1071 * pcibios_resource_survey()
1073 if (e820.map[i].type != E820_RESERVED || res->start < (1ULL<<20)) {
1074 if (e820.map[i].type != E820_NVS)
1075 res->flags |= IORESOURCE_BUSY;
1076 insert_resource(&iomem_resource, res);
1081 for (i = 0; i < e820_saved.nr_map; i++) {
1082 struct e820entry *entry = &e820_saved.map[i];
1083 firmware_map_add_early(entry->addr,
1084 entry->addr + entry->size - 1,
1085 e820_type_to_string(entry->type));
1089 /* How much should we pad RAM ending depending on where it is? */
1090 static unsigned long ram_alignment(resource_size_t pos)
1092 unsigned long mb = pos >> 20;
1094 /* To 64kB in the first megabyte */
1098 /* To 1MB in the first 16MB */
1102 /* To 64MB for anything above that */
1103 return 64*1024*1024;
1106 #define MAX_RESOURCE_SIZE ((resource_size_t)-1)
1108 void __init e820_reserve_resources_late(void)
1111 struct resource *res;
1114 for (i = 0; i < e820.nr_map; i++) {
1115 if (!res->parent && res->end)
1116 insert_resource_expand_to_fit(&iomem_resource, res);
1121 * Try to bump up RAM regions to reasonable boundaries to
1124 for (i = 0; i < e820.nr_map; i++) {
1125 struct e820entry *entry = &e820.map[i];
1128 if (entry->type != E820_RAM)
1130 start = entry->addr + entry->size;
1131 end = round_up(start, ram_alignment(start)) - 1;
1132 if (end > MAX_RESOURCE_SIZE)
1133 end = MAX_RESOURCE_SIZE;
1136 printk(KERN_DEBUG "reserve RAM buffer: %016llx - %016llx ",
1138 reserve_region_with_split(&iomem_resource, start, end,
1145 char *__init default_machine_specific_memory_setup(void)
1148 unsigned long maxmem;
1149 struct xen_memory_map memmap;
1150 static struct e820entry __initdata map[E820MAX];
1152 memmap.nr_entries = E820MAX;
1153 set_xen_guest_handle(memmap.buffer, map);
1155 rc = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap);
1156 if (rc == -ENOSYS) {
1157 memmap.nr_entries = 1;
1159 map[0].size = PFN_PHYS((unsigned long long)xen_start_info->nr_pages);
1160 /* 8MB slack (to balance backend allocations). */
1161 map[0].size += 8ULL << 20;
1162 map[0].type = E820_RAM;
1167 nr_map = memmap.nr_entries;
1168 sanitize_e820_map(map, ARRAY_SIZE(map), &nr_map);
1170 if (append_e820_map(map, nr_map) < 0)
1174 /* See the comment in parse_memopt(). */
1175 for (maxmem = rc = 0; rc < e820.nr_map; ++rc)
1176 if (e820.map[rc].type == E820_RAM)
1177 maxmem += e820.map[rc].size >> PAGE_SHIFT;
1178 if (is_initial_xendomain()) {
1179 domid_t domid = DOMID_SELF;
1181 rc = HYPERVISOR_memory_op(XENMEM_maximum_reservation, &domid);
1182 if (rc > 0 && maxmem > rc)
1185 if ((maxmem >> 5) > xen_start_info->nr_pages) {
1186 unsigned long long size = (u64)xen_start_info->nr_pages << 5;
1188 pr_warn("maxmem of %luM is invalid for an initial"
1189 " allocation of %luM, using %LuM\n",
1190 maxmem >> (20 - PAGE_SHIFT),
1191 xen_start_info->nr_pages >> (20 - PAGE_SHIFT),
1192 size >> (20 - PAGE_SHIFT));
1193 size <<= PAGE_SHIFT;
1194 e820_remove_range(size, ULLONG_MAX - size, E820_RAM, 1);
1197 if (is_initial_xendomain()) {
1198 memmap.nr_entries = E820MAX;
1199 set_xen_guest_handle(memmap.buffer, machine_e820.map);
1201 if (HYPERVISOR_memory_op(XENMEM_machine_memory_map, &memmap))
1203 machine_e820.nr_map = memmap.nr_entries;
1210 void __init setup_memory_map(void)
1214 who = x86_init.resources.memory_setup();
1215 #ifndef CONFIG_XEN_UNPRIVILEGED_GUEST
1217 if (is_initial_xendomain()) {
1218 printk(KERN_INFO "Xen-provided machine memory map:\n");
1219 _e820_print_map(&machine_e820, "BIOS");
1222 memcpy(&e820_saved, &e820, sizeof(struct e820map));
1224 printk(KERN_INFO "Xen-provided physical RAM map:\n");
1225 _e820_print_map(&e820, who);
1228 void __init memblock_x86_fill(void)
1234 * EFI may have more than 128 entries
1235 * We are safe to enable resizing, beause memblock_x86_fill()
1236 * is rather later for x86
1238 memblock_allow_resize();
1240 for (i = 0; i < e820.nr_map; i++) {
1241 struct e820entry *ei = &e820.map[i];
1243 end = ei->addr + ei->size;
1244 if (end != (resource_size_t)end)
1247 if (ei->type != E820_RAM && ei->type != E820_RESERVED_KERN)
1250 memblock_add(ei->addr, ei->size);
1254 if (max_pfn > xen_start_info->nr_pages)
1255 memblock_reserve(PFN_PHYS(xen_start_info->nr_pages),
1256 PFN_PHYS(max_pfn - xen_start_info->nr_pages));
1259 memblock_dump_all();
1262 void __init memblock_find_dma_reserve(void)
1264 #if defined(CONFIG_X86_64) && !defined(CONFIG_XEN)
1265 u64 nr_pages = 0, nr_free_pages = 0;
1266 unsigned long start_pfn, end_pfn;
1267 phys_addr_t start, end;
1272 * need to find out used area below MAX_DMA_PFN
1273 * need to use memblock to get free size in [0, MAX_DMA_PFN]
1274 * at first, and assume boot_mem will not take below MAX_DMA_PFN
1276 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, NULL) {
1277 start_pfn = min_t(unsigned long, start_pfn, MAX_DMA_PFN);
1278 end_pfn = min_t(unsigned long, end_pfn, MAX_DMA_PFN);
1279 nr_pages += end_pfn - start_pfn;
1282 for_each_free_mem_range(u, MAX_NUMNODES, &start, &end, NULL) {
1283 start_pfn = min_t(unsigned long, PFN_UP(start), MAX_DMA_PFN);
1284 end_pfn = min_t(unsigned long, PFN_DOWN(end), MAX_DMA_PFN);
1285 if (start_pfn < end_pfn)
1286 nr_free_pages += end_pfn - start_pfn;
1289 set_dma_reserve(nr_pages - nr_free_pages);