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/bootmem.h>
15 #include <linux/ioport.h>
16 #include <linux/string.h>
17 #include <linux/kexec.h>
18 #include <linux/module.h>
20 #include <linux/suspend.h>
21 #include <linux/pfn.h>
23 #include <asm/pgtable.h>
26 #include <asm/proto.h>
27 #include <asm/setup.h>
28 #include <asm/sections.h>
29 #include <asm/kdebug.h>
34 * PFN of last memory page.
36 unsigned long end_pfn;
39 * end_pfn only includes RAM, while end_pfn_map includes all e820 entries.
40 * The direct mapping extends to end_pfn_map, so that we can directly access
41 * apertures, ACPI and other tables without having to play with fixmaps.
43 unsigned long end_pfn_map;
46 * Last pfn which the user wants to use.
48 static unsigned long __initdata end_user_pfn = MAXMEM>>PAGE_SHIFT;
51 * Early reserved memory areas.
53 #define MAX_EARLY_RES 20
56 unsigned long start, end;
59 static struct early_res early_res[MAX_EARLY_RES] __initdata = {
60 { 0, PAGE_SIZE, "BIOS data page" }, /* BIOS data page */
62 { SMP_TRAMPOLINE_BASE, SMP_TRAMPOLINE_BASE + 2*PAGE_SIZE, "SMP_TRAMPOLINE" },
67 void __init reserve_early(unsigned long start, unsigned long end, char *name)
71 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
73 if (end > r->start && start < r->end)
74 panic("Overlapping early reservations %lx-%lx %s to %lx-%lx %s\n",
75 start, end - 1, name?name:"", r->start, r->end - 1, r->name);
77 if (i >= MAX_EARLY_RES)
78 panic("Too many early reservations");
83 strncpy(r->name, name, sizeof(r->name) - 1);
86 void __init early_res_to_bootmem(void)
89 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
90 struct early_res *r = &early_res[i];
91 printk(KERN_INFO "early res: %d [%lx-%lx] %s\n", i,
92 r->start, r->end - 1, r->name);
93 reserve_bootmem_generic(r->start, r->end - r->start);
97 /* Check for already reserved areas */
99 bad_addr(unsigned long *addrp, unsigned long size, unsigned long align)
102 unsigned long addr = *addrp, last;
106 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
107 struct early_res *r = &early_res[i];
108 if (last >= r->start && addr < r->end) {
109 *addrp = addr = round_up(r->end, align);
117 /* Check for already reserved areas */
119 bad_addr_size(unsigned long *addrp, unsigned long *sizep, unsigned long align)
122 unsigned long addr = *addrp, last;
123 unsigned long size = *sizep;
127 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
128 struct early_res *r = &early_res[i];
129 if (last > r->start && addr < r->start) {
130 size = r->start - addr;
134 if (last > r->end && addr < r->end) {
135 addr = round_up(r->end, align);
140 if (last <= r->end && addr >= r->start) {
152 * This function checks if any part of the range <start,end> is mapped
156 e820_any_mapped(unsigned long start, unsigned long end, unsigned type)
160 for (i = 0; i < e820.nr_map; i++) {
161 struct e820entry *ei = &e820.map[i];
163 if (type && ei->type != type)
165 if (ei->addr >= end || ei->addr + ei->size <= start)
171 EXPORT_SYMBOL_GPL(e820_any_mapped);
174 * This function checks if the entire range <start,end> is mapped with type.
176 * Note: this function only works correct if the e820 table is sorted and
177 * not-overlapping, which is the case
179 int __init e820_all_mapped(unsigned long start, unsigned long end,
184 for (i = 0; i < e820.nr_map; i++) {
185 struct e820entry *ei = &e820.map[i];
187 if (type && ei->type != type)
189 /* is the region (part) in overlap with the current region ?*/
190 if (ei->addr >= end || ei->addr + ei->size <= start)
193 /* if the region is at the beginning of <start,end> we move
194 * start to the end of the region since it's ok until there
196 if (ei->addr <= start)
197 start = ei->addr + ei->size;
199 * if start is now at or beyond end, we're done, full
209 * Find a free area with specified alignment in a specific range.
211 unsigned long __init find_e820_area(unsigned long start, unsigned long end,
212 unsigned long size, unsigned long align)
216 for (i = 0; i < e820.nr_map; i++) {
217 struct e820entry *ei = &e820.map[i];
218 unsigned long addr, last;
219 unsigned long ei_last;
221 if (ei->type != E820_RAM)
223 addr = round_up(ei->addr, align);
224 ei_last = ei->addr + ei->size;
226 addr = round_up(start, align);
229 while (bad_addr(&addr, size, align) && addr+size <= ei_last)
242 * Find next free range after *start
244 unsigned long __init find_e820_area_size(unsigned long start, unsigned long *sizep, unsigned long align)
248 for (i = 0; i < e820.nr_map; i++) {
249 struct e820entry *ei = &e820.map[i];
250 unsigned long addr, last;
251 unsigned long ei_last;
253 if (ei->type != E820_RAM)
255 addr = round_up(ei->addr, align);
256 ei_last = ei->addr + ei->size;
257 // printk(KERN_DEBUG "find_e820_area_size : e820 %d [%llx, %lx]\n", i, ei->addr, ei_last);
259 addr = round_up(start, align);
260 // printk(KERN_DEBUG "find_e820_area_size : 0 [%lx, %lx]\n", addr, ei_last);
263 *sizep = ei_last - addr;
264 while (bad_addr_size(&addr, sizep, align) && addr+ *sizep <= ei_last)
266 last = addr + *sizep;
267 // printk(KERN_DEBUG "find_e820_area_size : 1 [%lx, %lx]\n", addr, last);
276 * Find the highest page frame number we have available
278 unsigned long __init e820_end_of_ram(void)
280 unsigned long end_pfn;
282 end_pfn = find_max_pfn_with_active_regions();
284 if (end_pfn > end_pfn_map)
285 end_pfn_map = end_pfn;
286 if (end_pfn_map > MAXMEM>>PAGE_SHIFT)
287 end_pfn_map = MAXMEM>>PAGE_SHIFT;
288 if (end_pfn > end_user_pfn)
289 end_pfn = end_user_pfn;
290 if (end_pfn > end_pfn_map)
291 end_pfn = end_pfn_map;
293 printk(KERN_INFO "end_pfn_map = %lu\n", end_pfn_map);
298 * Mark e820 reserved areas as busy for the resource manager.
300 void __init e820_reserve_resources(void)
303 for (i = 0; i < e820.nr_map; i++) {
304 struct resource *res;
305 res = alloc_bootmem_low(sizeof(struct resource));
306 switch (e820.map[i].type) {
307 case E820_RAM: res->name = "System RAM"; break;
308 case E820_ACPI: res->name = "ACPI Tables"; break;
309 case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
310 default: res->name = "reserved";
312 res->start = e820.map[i].addr;
313 res->end = res->start + e820.map[i].size - 1;
314 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
315 insert_resource(&iomem_resource, res);
320 * Find the ranges of physical addresses that do not correspond to
321 * e820 RAM areas and mark the corresponding pages as nosave for software
322 * suspend and suspend to RAM.
324 * This function requires the e820 map to be sorted and without any
325 * overlapping entries and assumes the first e820 area to be RAM.
327 void __init e820_mark_nosave_regions(void)
332 paddr = round_down(e820.map[0].addr + e820.map[0].size, PAGE_SIZE);
333 for (i = 1; i < e820.nr_map; i++) {
334 struct e820entry *ei = &e820.map[i];
336 if (paddr < ei->addr)
337 register_nosave_region(PFN_DOWN(paddr),
340 paddr = round_down(ei->addr + ei->size, PAGE_SIZE);
341 if (ei->type != E820_RAM)
342 register_nosave_region(PFN_UP(ei->addr),
345 if (paddr >= (end_pfn << PAGE_SHIFT))
351 * Finds an active region in the address range from start_pfn to end_pfn and
352 * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
354 static int __init e820_find_active_region(const struct e820entry *ei,
355 unsigned long start_pfn,
356 unsigned long end_pfn,
357 unsigned long *ei_startpfn,
358 unsigned long *ei_endpfn)
360 *ei_startpfn = round_up(ei->addr, PAGE_SIZE) >> PAGE_SHIFT;
361 *ei_endpfn = round_down(ei->addr + ei->size, PAGE_SIZE) >> PAGE_SHIFT;
363 /* Skip map entries smaller than a page */
364 if (*ei_startpfn >= *ei_endpfn)
367 /* Check if end_pfn_map should be updated */
368 if (ei->type != E820_RAM && *ei_endpfn > end_pfn_map)
369 end_pfn_map = *ei_endpfn;
371 /* Skip if map is outside the node */
372 if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
373 *ei_startpfn >= end_pfn)
376 /* Check for overlaps */
377 if (*ei_startpfn < start_pfn)
378 *ei_startpfn = start_pfn;
379 if (*ei_endpfn > end_pfn)
380 *ei_endpfn = end_pfn;
382 /* Obey end_user_pfn to save on memmap */
383 if (*ei_startpfn >= end_user_pfn)
385 if (*ei_endpfn > end_user_pfn)
386 *ei_endpfn = end_user_pfn;
391 /* Walk the e820 map and register active regions within a node */
393 e820_register_active_regions(int nid, unsigned long start_pfn,
394 unsigned long end_pfn)
396 unsigned long ei_startpfn;
397 unsigned long ei_endpfn;
400 for (i = 0; i < e820.nr_map; i++)
401 if (e820_find_active_region(&e820.map[i],
403 &ei_startpfn, &ei_endpfn))
404 add_active_range(nid, ei_startpfn, ei_endpfn);
408 * Add a memory region to the kernel e820 map.
410 void __init add_memory_region(unsigned long start, unsigned long size, int type)
415 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
419 e820.map[x].addr = start;
420 e820.map[x].size = size;
421 e820.map[x].type = type;
426 * Find the hole size (in bytes) in the memory range.
427 * @start: starting address of the memory range to scan
428 * @end: ending address of the memory range to scan
430 unsigned long __init e820_hole_size(unsigned long start, unsigned long end)
432 unsigned long start_pfn = start >> PAGE_SHIFT;
433 unsigned long end_pfn = end >> PAGE_SHIFT;
434 unsigned long ei_startpfn, ei_endpfn, ram = 0;
437 for (i = 0; i < e820.nr_map; i++) {
438 if (e820_find_active_region(&e820.map[i],
440 &ei_startpfn, &ei_endpfn))
441 ram += ei_endpfn - ei_startpfn;
443 return end - start - (ram << PAGE_SHIFT);
446 static void __init e820_print_map(char *who)
450 for (i = 0; i < e820.nr_map; i++) {
451 printk(KERN_INFO " %s: %016Lx - %016Lx ", who,
452 (unsigned long long) e820.map[i].addr,
454 (e820.map[i].addr + e820.map[i].size));
455 switch (e820.map[i].type) {
457 printk(KERN_CONT "(usable)\n");
460 printk(KERN_CONT "(reserved)\n");
463 printk(KERN_CONT "(ACPI data)\n");
466 printk(KERN_CONT "(ACPI NVS)\n");
469 printk(KERN_CONT "type %u\n", e820.map[i].type);
476 * Sanitize the BIOS e820 map.
478 * Some e820 responses include overlapping entries. The following
479 * replaces the original e820 map with a new one, removing overlaps.
482 static int __init sanitize_e820_map(struct e820entry *biosmap, char *pnr_map)
484 struct change_member {
485 struct e820entry *pbios; /* pointer to original bios entry */
486 unsigned long long addr; /* address for this change point */
488 static struct change_member change_point_list[2*E820MAX] __initdata;
489 static struct change_member *change_point[2*E820MAX] __initdata;
490 static struct e820entry *overlap_list[E820MAX] __initdata;
491 static struct e820entry new_bios[E820MAX] __initdata;
492 struct change_member *change_tmp;
493 unsigned long current_type, last_type;
494 unsigned long long last_addr;
495 int chgidx, still_changing;
498 int old_nr, new_nr, chg_nr;
502 Visually we're performing the following
503 (1,2,3,4 = memory types)...
505 Sample memory map (w/overlaps):
506 ____22__________________
507 ______________________4_
508 ____1111________________
509 _44_____________________
510 11111111________________
511 ____________________33__
512 ___________44___________
513 __________33333_________
514 ______________22________
515 ___________________2222_
516 _________111111111______
517 _____________________11_
518 _________________4______
520 Sanitized equivalent (no overlap):
521 1_______________________
522 _44_____________________
523 ___1____________________
524 ____22__________________
525 ______11________________
526 _________1______________
527 __________3_____________
528 ___________44___________
529 _____________33_________
530 _______________2________
531 ________________1_______
532 _________________4______
533 ___________________2____
534 ____________________33__
535 ______________________4_
538 /* if there's only one memory region, don't bother */
544 /* bail out if we find any unreasonable addresses in bios map */
545 for (i = 0; i < old_nr; i++)
546 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
549 /* create pointers for initial change-point information (for sorting) */
550 for (i = 0; i < 2 * old_nr; i++)
551 change_point[i] = &change_point_list[i];
553 /* record all known change-points (starting and ending addresses),
554 omitting those that are for empty memory regions */
556 for (i = 0; i < old_nr; i++) {
557 if (biosmap[i].size != 0) {
558 change_point[chgidx]->addr = biosmap[i].addr;
559 change_point[chgidx++]->pbios = &biosmap[i];
560 change_point[chgidx]->addr = biosmap[i].addr +
562 change_point[chgidx++]->pbios = &biosmap[i];
567 /* sort change-point list by memory addresses (low -> high) */
569 while (still_changing) {
571 for (i = 1; i < chg_nr; i++) {
572 unsigned long long curaddr, lastaddr;
573 unsigned long long curpbaddr, lastpbaddr;
575 curaddr = change_point[i]->addr;
576 lastaddr = change_point[i - 1]->addr;
577 curpbaddr = change_point[i]->pbios->addr;
578 lastpbaddr = change_point[i - 1]->pbios->addr;
581 * swap entries, when:
583 * curaddr > lastaddr or
584 * curaddr == lastaddr and curaddr == curpbaddr and
585 * lastaddr != lastpbaddr
587 if (curaddr < lastaddr ||
588 (curaddr == lastaddr && curaddr == curpbaddr &&
589 lastaddr != lastpbaddr)) {
590 change_tmp = change_point[i];
591 change_point[i] = change_point[i-1];
592 change_point[i-1] = change_tmp;
598 /* create a new bios memory map, removing overlaps */
599 overlap_entries = 0; /* number of entries in the overlap table */
600 new_bios_entry = 0; /* index for creating new bios map entries */
601 last_type = 0; /* start with undefined memory type */
602 last_addr = 0; /* start with 0 as last starting address */
604 /* loop through change-points, determining affect on the new bios map */
605 for (chgidx = 0; chgidx < chg_nr; chgidx++) {
606 /* keep track of all overlapping bios entries */
607 if (change_point[chgidx]->addr ==
608 change_point[chgidx]->pbios->addr) {
610 * add map entry to overlap list (> 1 entry
611 * implies an overlap)
613 overlap_list[overlap_entries++] =
614 change_point[chgidx]->pbios;
617 * remove entry from list (order independent,
620 for (i = 0; i < overlap_entries; i++) {
621 if (overlap_list[i] ==
622 change_point[chgidx]->pbios)
624 overlap_list[overlap_entries-1];
629 * if there are overlapping entries, decide which
630 * "type" to use (larger value takes precedence --
631 * 1=usable, 2,3,4,4+=unusable)
634 for (i = 0; i < overlap_entries; i++)
635 if (overlap_list[i]->type > current_type)
636 current_type = overlap_list[i]->type;
638 * continue building up new bios map based on this
641 if (current_type != last_type) {
642 if (last_type != 0) {
643 new_bios[new_bios_entry].size =
644 change_point[chgidx]->addr - last_addr;
646 * move forward only if the new size
649 if (new_bios[new_bios_entry].size != 0)
651 * no more space left for new
654 if (++new_bios_entry >= E820MAX)
657 if (current_type != 0) {
658 new_bios[new_bios_entry].addr =
659 change_point[chgidx]->addr;
660 new_bios[new_bios_entry].type = current_type;
661 last_addr = change_point[chgidx]->addr;
663 last_type = current_type;
666 /* retain count for new bios entries */
667 new_nr = new_bios_entry;
669 /* copy new bios mapping into original location */
670 memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
677 * Copy the BIOS e820 map into a safe place.
679 * Sanity-check it while we're at it..
681 * If we're lucky and live on a modern system, the setup code
682 * will have given us a memory map that we can use to properly
683 * set up memory. If we aren't, we'll fake a memory map.
685 static int __init copy_e820_map(struct e820entry *biosmap, int nr_map)
687 /* Only one memory region (or negative)? Ignore it */
692 u64 start = biosmap->addr;
693 u64 size = biosmap->size;
694 u64 end = start + size;
695 u32 type = biosmap->type;
697 /* Overflow in 64 bits? Ignore the memory map. */
701 add_memory_region(start, size, type);
702 } while (biosmap++, --nr_map);
706 static void early_panic(char *msg)
712 /* We're not void only for x86 32-bit compat */
713 char * __init machine_specific_memory_setup(void)
715 char *who = "BIOS-e820";
717 * Try to copy the BIOS-supplied E820-map.
719 * Otherwise fake a memory map; one section from 0k->640k,
720 * the next section from 1mb->appropriate_mem_k
722 sanitize_e820_map(boot_params.e820_map, &boot_params.e820_entries);
723 if (copy_e820_map(boot_params.e820_map, boot_params.e820_entries) < 0)
724 early_panic("Cannot find a valid memory map");
725 printk(KERN_INFO "BIOS-provided physical RAM map:\n");
728 /* In case someone cares... */
732 static int __init parse_memopt(char *p)
736 end_user_pfn = memparse(p, &p);
737 end_user_pfn >>= PAGE_SHIFT;
740 early_param("mem", parse_memopt);
742 static int userdef __initdata;
744 static int __init parse_memmap_opt(char *p)
747 unsigned long long start_at, mem_size;
749 if (!strcmp(p, "exactmap")) {
750 #ifdef CONFIG_CRASH_DUMP
752 * If we are doing a crash dump, we still need to know
753 * the real mem size before original memory map is
756 e820_register_active_regions(0, 0, -1UL);
757 saved_max_pfn = e820_end_of_ram();
758 remove_all_active_ranges();
767 mem_size = memparse(p, &p);
773 start_at = memparse(p+1, &p);
774 add_memory_region(start_at, mem_size, E820_RAM);
775 } else if (*p == '#') {
776 start_at = memparse(p+1, &p);
777 add_memory_region(start_at, mem_size, E820_ACPI);
778 } else if (*p == '$') {
779 start_at = memparse(p+1, &p);
780 add_memory_region(start_at, mem_size, E820_RESERVED);
782 end_user_pfn = (mem_size >> PAGE_SHIFT);
784 return *p == '\0' ? 0 : -EINVAL;
786 early_param("memmap", parse_memmap_opt);
788 void __init finish_e820_parsing(void)
791 char nr = e820.nr_map;
793 if (sanitize_e820_map(e820.map, &nr) < 0)
794 early_panic("Invalid user supplied memory map");
797 printk(KERN_INFO "user-defined physical RAM map:\n");
798 e820_print_map("user");
802 void __init update_memory_range(u64 start, u64 size, unsigned old_type,
807 BUG_ON(old_type == new_type);
809 for (i = 0; i < e820.nr_map; i++) {
810 struct e820entry *ei = &e820.map[i];
811 u64 final_start, final_end;
812 if (ei->type != old_type)
814 /* totally covered? */
815 if (ei->addr >= start && ei->size <= size) {
819 /* partially covered */
820 final_start = max(start, ei->addr);
821 final_end = min(start + size, ei->addr + ei->size);
822 if (final_start >= final_end)
824 add_memory_region(final_start, final_end - final_start,
829 void __init update_e820(void)
833 nr_map = e820.nr_map;
834 if (sanitize_e820_map(e820.map, &nr_map))
836 e820.nr_map = nr_map;
837 printk(KERN_INFO "modified physical RAM map:\n");
838 e820_print_map("modified");
841 unsigned long pci_mem_start = 0xaeedbabe;
842 EXPORT_SYMBOL(pci_mem_start);
845 * Search for the biggest gap in the low 32 bits of the e820
846 * memory space. We pass this space to PCI to assign MMIO resources
847 * for hotplug or unconfigured devices in.
848 * Hopefully the BIOS let enough space left.
850 __init void e820_setup_gap(void)
852 unsigned long gapstart, gapsize, round;
857 last = 0x100000000ull;
858 gapstart = 0x10000000;
862 unsigned long long start = e820.map[i].addr;
863 unsigned long long end = start + e820.map[i].size;
866 * Since "last" is at most 4GB, we know we'll
867 * fit in 32 bits if this condition is true
870 unsigned long gap = last - end;
883 gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
884 printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit "
886 KERN_ERR "PCI: Unassigned devices with 32bit resource "
887 "registers may break!\n");
891 * See how much we want to round up: start off with
892 * rounding to the next 1MB area.
895 while ((gapsize >> 4) > round)
897 /* Fun with two's complement */
898 pci_mem_start = (gapstart + round) & -round;
901 "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
902 pci_mem_start, gapstart, gapsize);
905 int __init arch_get_ram_range(int slot, u64 *addr, u64 *size)
909 if (slot < 0 || slot >= e820.nr_map)
911 for (i = slot; i < e820.nr_map; i++) {
912 if (e820.map[i].type != E820_RAM)
916 if (i == e820.nr_map || e820.map[i].addr > (max_pfn << PAGE_SHIFT))
918 *addr = e820.map[i].addr;
919 *size = min_t(u64, e820.map[i].size + e820.map[i].addr,
920 max_pfn << PAGE_SHIFT) - *addr;