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>
30 #include <asm/trampoline.h>
35 * PFN of last memory page.
37 unsigned long end_pfn;
40 * end_pfn only includes RAM, while max_pfn_mapped includes all e820 entries.
41 * The direct mapping extends to max_pfn_mapped, so that we can directly access
42 * apertures, ACPI and other tables without having to play with fixmaps.
44 unsigned long max_pfn_mapped;
47 * Last pfn which the user wants to use.
49 static unsigned long __initdata end_user_pfn = MAXMEM>>PAGE_SHIFT;
52 * Early reserved memory areas.
54 #define MAX_EARLY_RES 20
57 unsigned long start, end;
60 static struct early_res early_res[MAX_EARLY_RES] __initdata = {
61 { 0, PAGE_SIZE, "BIOS data page" }, /* BIOS data page */
62 #ifdef CONFIG_X86_TRAMPOLINE
63 { TRAMPOLINE_BASE, TRAMPOLINE_BASE + 2 * PAGE_SIZE, "TRAMPOLINE" },
68 void __init reserve_early(unsigned long start, unsigned long end, char *name)
72 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
74 if (end > r->start && start < r->end)
75 panic("Overlapping early reservations %lx-%lx %s to %lx-%lx %s\n",
76 start, end - 1, name?name:"", r->start, r->end - 1, r->name);
78 if (i >= MAX_EARLY_RES)
79 panic("Too many early reservations");
84 strncpy(r->name, name, sizeof(r->name) - 1);
87 void __init free_early(unsigned long start, unsigned long end)
92 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
94 if (start == r->start && end == r->end)
97 if (i >= MAX_EARLY_RES || !early_res[i].end)
98 panic("free_early on not reserved area: %lx-%lx!", start, end);
100 for (j = i + 1; j < MAX_EARLY_RES && early_res[j].end; j++)
103 memcpy(&early_res[i], &early_res[i + 1],
104 (j - 1 - i) * sizeof(struct early_res));
106 early_res[j - 1].end = 0;
109 void __init early_res_to_bootmem(void)
112 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
113 struct early_res *r = &early_res[i];
114 printk(KERN_INFO "early res: %d [%lx-%lx] %s\n", i,
115 r->start, r->end - 1, r->name);
116 reserve_bootmem_generic(r->start, r->end - r->start);
120 /* Check for already reserved areas */
121 static inline int __init
122 bad_addr(unsigned long *addrp, unsigned long size, unsigned long align)
125 unsigned long addr = *addrp, last;
129 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
130 struct early_res *r = &early_res[i];
131 if (last >= r->start && addr < r->end) {
132 *addrp = addr = round_up(r->end, align);
140 /* Check for already reserved areas */
141 static inline int __init
142 bad_addr_size(unsigned long *addrp, unsigned long *sizep, unsigned long align)
145 unsigned long addr = *addrp, last;
146 unsigned long size = *sizep;
150 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
151 struct early_res *r = &early_res[i];
152 if (last > r->start && addr < r->start) {
153 size = r->start - addr;
157 if (last > r->end && addr < r->end) {
158 addr = round_up(r->end, align);
163 if (last <= r->end && addr >= r->start) {
175 * This function checks if any part of the range <start,end> is mapped
179 e820_any_mapped(unsigned long start, unsigned long end, unsigned type)
183 for (i = 0; i < e820.nr_map; i++) {
184 struct e820entry *ei = &e820.map[i];
186 if (type && ei->type != type)
188 if (ei->addr >= end || ei->addr + ei->size <= start)
194 EXPORT_SYMBOL_GPL(e820_any_mapped);
197 * This function checks if the entire range <start,end> is mapped with type.
199 * Note: this function only works correct if the e820 table is sorted and
200 * not-overlapping, which is the case
202 int __init e820_all_mapped(unsigned long start, unsigned long end,
207 for (i = 0; i < e820.nr_map; i++) {
208 struct e820entry *ei = &e820.map[i];
210 if (type && ei->type != type)
212 /* is the region (part) in overlap with the current region ?*/
213 if (ei->addr >= end || ei->addr + ei->size <= start)
216 /* if the region is at the beginning of <start,end> we move
217 * start to the end of the region since it's ok until there
219 if (ei->addr <= start)
220 start = ei->addr + ei->size;
222 * if start is now at or beyond end, we're done, full
232 * Find a free area with specified alignment in a specific range.
234 unsigned long __init find_e820_area(unsigned long start, unsigned long end,
235 unsigned long size, unsigned long align)
239 for (i = 0; i < e820.nr_map; i++) {
240 struct e820entry *ei = &e820.map[i];
241 unsigned long addr, last;
242 unsigned long ei_last;
244 if (ei->type != E820_RAM)
246 addr = round_up(ei->addr, align);
247 ei_last = ei->addr + ei->size;
249 addr = round_up(start, align);
252 while (bad_addr(&addr, size, align) && addr+size <= ei_last)
265 * Find next free range after *start
267 unsigned long __init find_e820_area_size(unsigned long start,
268 unsigned long *sizep,
273 for (i = 0; i < e820.nr_map; i++) {
274 struct e820entry *ei = &e820.map[i];
275 unsigned long addr, last;
276 unsigned long ei_last;
278 if (ei->type != E820_RAM)
280 addr = round_up(ei->addr, align);
281 ei_last = ei->addr + ei->size;
283 addr = round_up(start, align);
286 *sizep = ei_last - addr;
287 while (bad_addr_size(&addr, sizep, align) &&
288 addr + *sizep <= ei_last)
290 last = addr + *sizep;
299 * Find the highest page frame number we have available
301 unsigned long __init e820_end_of_ram(void)
303 unsigned long end_pfn;
305 end_pfn = find_max_pfn_with_active_regions();
307 if (end_pfn > max_pfn_mapped)
308 max_pfn_mapped = end_pfn;
309 if (max_pfn_mapped > MAXMEM>>PAGE_SHIFT)
310 max_pfn_mapped = MAXMEM>>PAGE_SHIFT;
311 if (end_pfn > end_user_pfn)
312 end_pfn = end_user_pfn;
313 if (end_pfn > max_pfn_mapped)
314 end_pfn = max_pfn_mapped;
316 printk(KERN_INFO "max_pfn_mapped = %lu\n", max_pfn_mapped);
321 * Mark e820 reserved areas as busy for the resource manager.
323 void __init e820_reserve_resources(void)
326 struct resource *res;
328 res = alloc_bootmem_low(sizeof(struct resource) * e820.nr_map);
329 for (i = 0; i < e820.nr_map; i++) {
330 switch (e820.map[i].type) {
331 case E820_RAM: res->name = "System RAM"; break;
332 case E820_ACPI: res->name = "ACPI Tables"; break;
333 case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
334 default: res->name = "reserved";
336 res->start = e820.map[i].addr;
337 res->end = res->start + e820.map[i].size - 1;
338 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
339 insert_resource(&iomem_resource, res);
345 * Find the ranges of physical addresses that do not correspond to
346 * e820 RAM areas and mark the corresponding pages as nosave for software
347 * suspend and suspend to RAM.
349 * This function requires the e820 map to be sorted and without any
350 * overlapping entries and assumes the first e820 area to be RAM.
352 void __init e820_mark_nosave_regions(void)
357 paddr = round_down(e820.map[0].addr + e820.map[0].size, PAGE_SIZE);
358 for (i = 1; i < e820.nr_map; i++) {
359 struct e820entry *ei = &e820.map[i];
361 if (paddr < ei->addr)
362 register_nosave_region(PFN_DOWN(paddr),
365 paddr = round_down(ei->addr + ei->size, PAGE_SIZE);
366 if (ei->type != E820_RAM)
367 register_nosave_region(PFN_UP(ei->addr),
370 if (paddr >= (end_pfn << PAGE_SHIFT))
376 * Finds an active region in the address range from start_pfn to end_pfn and
377 * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
379 static int __init e820_find_active_region(const struct e820entry *ei,
380 unsigned long start_pfn,
381 unsigned long end_pfn,
382 unsigned long *ei_startpfn,
383 unsigned long *ei_endpfn)
385 *ei_startpfn = round_up(ei->addr, PAGE_SIZE) >> PAGE_SHIFT;
386 *ei_endpfn = round_down(ei->addr + ei->size, PAGE_SIZE) >> PAGE_SHIFT;
388 /* Skip map entries smaller than a page */
389 if (*ei_startpfn >= *ei_endpfn)
392 /* Check if max_pfn_mapped should be updated */
393 if (ei->type != E820_RAM && *ei_endpfn > max_pfn_mapped)
394 max_pfn_mapped = *ei_endpfn;
396 /* Skip if map is outside the node */
397 if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
398 *ei_startpfn >= end_pfn)
401 /* Check for overlaps */
402 if (*ei_startpfn < start_pfn)
403 *ei_startpfn = start_pfn;
404 if (*ei_endpfn > end_pfn)
405 *ei_endpfn = end_pfn;
407 /* Obey end_user_pfn to save on memmap */
408 if (*ei_startpfn >= end_user_pfn)
410 if (*ei_endpfn > end_user_pfn)
411 *ei_endpfn = end_user_pfn;
416 /* Walk the e820 map and register active regions within a node */
418 e820_register_active_regions(int nid, unsigned long start_pfn,
419 unsigned long end_pfn)
421 unsigned long ei_startpfn;
422 unsigned long ei_endpfn;
425 for (i = 0; i < e820.nr_map; i++)
426 if (e820_find_active_region(&e820.map[i],
428 &ei_startpfn, &ei_endpfn))
429 add_active_range(nid, ei_startpfn, ei_endpfn);
433 * Add a memory region to the kernel e820 map.
435 void __init add_memory_region(unsigned long start, unsigned long size, int type)
440 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
444 e820.map[x].addr = start;
445 e820.map[x].size = size;
446 e820.map[x].type = type;
451 * Find the hole size (in bytes) in the memory range.
452 * @start: starting address of the memory range to scan
453 * @end: ending address of the memory range to scan
455 unsigned long __init e820_hole_size(unsigned long start, unsigned long end)
457 unsigned long start_pfn = start >> PAGE_SHIFT;
458 unsigned long end_pfn = end >> PAGE_SHIFT;
459 unsigned long ei_startpfn, ei_endpfn, ram = 0;
462 for (i = 0; i < e820.nr_map; i++) {
463 if (e820_find_active_region(&e820.map[i],
465 &ei_startpfn, &ei_endpfn))
466 ram += ei_endpfn - ei_startpfn;
468 return end - start - (ram << PAGE_SHIFT);
471 static void __init e820_print_map(char *who)
475 for (i = 0; i < e820.nr_map; i++) {
476 printk(KERN_INFO " %s: %016Lx - %016Lx ", who,
477 (unsigned long long) e820.map[i].addr,
479 (e820.map[i].addr + e820.map[i].size));
480 switch (e820.map[i].type) {
482 printk(KERN_CONT "(usable)\n");
485 printk(KERN_CONT "(reserved)\n");
488 printk(KERN_CONT "(ACPI data)\n");
491 printk(KERN_CONT "(ACPI NVS)\n");
494 printk(KERN_CONT "type %u\n", e820.map[i].type);
501 * Sanitize the BIOS e820 map.
503 * Some e820 responses include overlapping entries. The following
504 * replaces the original e820 map with a new one, removing overlaps.
507 static int __init sanitize_e820_map(struct e820entry *biosmap, char *pnr_map)
509 struct change_member {
510 struct e820entry *pbios; /* pointer to original bios entry */
511 unsigned long long addr; /* address for this change point */
513 static struct change_member change_point_list[2*E820MAX] __initdata;
514 static struct change_member *change_point[2*E820MAX] __initdata;
515 static struct e820entry *overlap_list[E820MAX] __initdata;
516 static struct e820entry new_bios[E820MAX] __initdata;
517 struct change_member *change_tmp;
518 unsigned long current_type, last_type;
519 unsigned long long last_addr;
520 int chgidx, still_changing;
523 int old_nr, new_nr, chg_nr;
527 Visually we're performing the following
528 (1,2,3,4 = memory types)...
530 Sample memory map (w/overlaps):
531 ____22__________________
532 ______________________4_
533 ____1111________________
534 _44_____________________
535 11111111________________
536 ____________________33__
537 ___________44___________
538 __________33333_________
539 ______________22________
540 ___________________2222_
541 _________111111111______
542 _____________________11_
543 _________________4______
545 Sanitized equivalent (no overlap):
546 1_______________________
547 _44_____________________
548 ___1____________________
549 ____22__________________
550 ______11________________
551 _________1______________
552 __________3_____________
553 ___________44___________
554 _____________33_________
555 _______________2________
556 ________________1_______
557 _________________4______
558 ___________________2____
559 ____________________33__
560 ______________________4_
563 /* if there's only one memory region, don't bother */
569 /* bail out if we find any unreasonable addresses in bios map */
570 for (i = 0; i < old_nr; i++)
571 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
574 /* create pointers for initial change-point information (for sorting) */
575 for (i = 0; i < 2 * old_nr; i++)
576 change_point[i] = &change_point_list[i];
578 /* record all known change-points (starting and ending addresses),
579 omitting those that are for empty memory regions */
581 for (i = 0; i < old_nr; i++) {
582 if (biosmap[i].size != 0) {
583 change_point[chgidx]->addr = biosmap[i].addr;
584 change_point[chgidx++]->pbios = &biosmap[i];
585 change_point[chgidx]->addr = biosmap[i].addr +
587 change_point[chgidx++]->pbios = &biosmap[i];
592 /* sort change-point list by memory addresses (low -> high) */
594 while (still_changing) {
596 for (i = 1; i < chg_nr; i++) {
597 unsigned long long curaddr, lastaddr;
598 unsigned long long curpbaddr, lastpbaddr;
600 curaddr = change_point[i]->addr;
601 lastaddr = change_point[i - 1]->addr;
602 curpbaddr = change_point[i]->pbios->addr;
603 lastpbaddr = change_point[i - 1]->pbios->addr;
606 * swap entries, when:
608 * curaddr > lastaddr or
609 * curaddr == lastaddr and curaddr == curpbaddr and
610 * lastaddr != lastpbaddr
612 if (curaddr < lastaddr ||
613 (curaddr == lastaddr && curaddr == curpbaddr &&
614 lastaddr != lastpbaddr)) {
615 change_tmp = change_point[i];
616 change_point[i] = change_point[i-1];
617 change_point[i-1] = change_tmp;
623 /* create a new bios memory map, removing overlaps */
624 overlap_entries = 0; /* number of entries in the overlap table */
625 new_bios_entry = 0; /* index for creating new bios map entries */
626 last_type = 0; /* start with undefined memory type */
627 last_addr = 0; /* start with 0 as last starting address */
629 /* loop through change-points, determining affect on the new bios map */
630 for (chgidx = 0; chgidx < chg_nr; chgidx++) {
631 /* keep track of all overlapping bios entries */
632 if (change_point[chgidx]->addr ==
633 change_point[chgidx]->pbios->addr) {
635 * add map entry to overlap list (> 1 entry
636 * implies an overlap)
638 overlap_list[overlap_entries++] =
639 change_point[chgidx]->pbios;
642 * remove entry from list (order independent,
645 for (i = 0; i < overlap_entries; i++) {
646 if (overlap_list[i] ==
647 change_point[chgidx]->pbios)
649 overlap_list[overlap_entries-1];
654 * if there are overlapping entries, decide which
655 * "type" to use (larger value takes precedence --
656 * 1=usable, 2,3,4,4+=unusable)
659 for (i = 0; i < overlap_entries; i++)
660 if (overlap_list[i]->type > current_type)
661 current_type = overlap_list[i]->type;
663 * continue building up new bios map based on this
666 if (current_type != last_type) {
667 if (last_type != 0) {
668 new_bios[new_bios_entry].size =
669 change_point[chgidx]->addr - last_addr;
671 * move forward only if the new size
674 if (new_bios[new_bios_entry].size != 0)
676 * no more space left for new
679 if (++new_bios_entry >= E820MAX)
682 if (current_type != 0) {
683 new_bios[new_bios_entry].addr =
684 change_point[chgidx]->addr;
685 new_bios[new_bios_entry].type = current_type;
686 last_addr = change_point[chgidx]->addr;
688 last_type = current_type;
691 /* retain count for new bios entries */
692 new_nr = new_bios_entry;
694 /* copy new bios mapping into original location */
695 memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
702 * Copy the BIOS e820 map into a safe place.
704 * Sanity-check it while we're at it..
706 * If we're lucky and live on a modern system, the setup code
707 * will have given us a memory map that we can use to properly
708 * set up memory. If we aren't, we'll fake a memory map.
710 static int __init copy_e820_map(struct e820entry *biosmap, int nr_map)
712 /* Only one memory region (or negative)? Ignore it */
717 u64 start = biosmap->addr;
718 u64 size = biosmap->size;
719 u64 end = start + size;
720 u32 type = biosmap->type;
722 /* Overflow in 64 bits? Ignore the memory map. */
726 add_memory_region(start, size, type);
727 } while (biosmap++, --nr_map);
731 static void early_panic(char *msg)
737 /* We're not void only for x86 32-bit compat */
738 char * __init machine_specific_memory_setup(void)
740 char *who = "BIOS-e820";
742 * Try to copy the BIOS-supplied E820-map.
744 * Otherwise fake a memory map; one section from 0k->640k,
745 * the next section from 1mb->appropriate_mem_k
747 sanitize_e820_map(boot_params.e820_map, &boot_params.e820_entries);
748 if (copy_e820_map(boot_params.e820_map, boot_params.e820_entries) < 0)
749 early_panic("Cannot find a valid memory map");
750 printk(KERN_INFO "BIOS-provided physical RAM map:\n");
753 /* In case someone cares... */
757 static int __init parse_memopt(char *p)
761 end_user_pfn = memparse(p, &p);
762 end_user_pfn >>= PAGE_SHIFT;
765 early_param("mem", parse_memopt);
767 static int userdef __initdata;
769 static int __init parse_memmap_opt(char *p)
772 unsigned long long start_at, mem_size;
774 if (!strcmp(p, "exactmap")) {
775 #ifdef CONFIG_CRASH_DUMP
777 * If we are doing a crash dump, we still need to know
778 * the real mem size before original memory map is
781 e820_register_active_regions(0, 0, -1UL);
782 saved_max_pfn = e820_end_of_ram();
783 remove_all_active_ranges();
792 mem_size = memparse(p, &p);
798 start_at = memparse(p+1, &p);
799 add_memory_region(start_at, mem_size, E820_RAM);
800 } else if (*p == '#') {
801 start_at = memparse(p+1, &p);
802 add_memory_region(start_at, mem_size, E820_ACPI);
803 } else if (*p == '$') {
804 start_at = memparse(p+1, &p);
805 add_memory_region(start_at, mem_size, E820_RESERVED);
807 end_user_pfn = (mem_size >> PAGE_SHIFT);
809 return *p == '\0' ? 0 : -EINVAL;
811 early_param("memmap", parse_memmap_opt);
813 void __init finish_e820_parsing(void)
816 char nr = e820.nr_map;
818 if (sanitize_e820_map(e820.map, &nr) < 0)
819 early_panic("Invalid user supplied memory map");
822 printk(KERN_INFO "user-defined physical RAM map:\n");
823 e820_print_map("user");
827 void __init update_memory_range(u64 start, u64 size, unsigned old_type,
832 BUG_ON(old_type == new_type);
834 for (i = 0; i < e820.nr_map; i++) {
835 struct e820entry *ei = &e820.map[i];
836 u64 final_start, final_end;
837 if (ei->type != old_type)
839 /* totally covered? */
840 if (ei->addr >= start && ei->size <= size) {
844 /* partially covered */
845 final_start = max(start, ei->addr);
846 final_end = min(start + size, ei->addr + ei->size);
847 if (final_start >= final_end)
849 add_memory_region(final_start, final_end - final_start,
854 void __init update_e820(void)
858 nr_map = e820.nr_map;
859 if (sanitize_e820_map(e820.map, &nr_map))
861 e820.nr_map = nr_map;
862 printk(KERN_INFO "modified physical RAM map:\n");
863 e820_print_map("modified");
866 unsigned long pci_mem_start = 0xaeedbabe;
867 EXPORT_SYMBOL(pci_mem_start);
870 * Search for the biggest gap in the low 32 bits of the e820
871 * memory space. We pass this space to PCI to assign MMIO resources
872 * for hotplug or unconfigured devices in.
873 * Hopefully the BIOS let enough space left.
875 __init void e820_setup_gap(void)
877 unsigned long gapstart, gapsize, round;
882 last = 0x100000000ull;
883 gapstart = 0x10000000;
887 unsigned long long start = e820.map[i].addr;
888 unsigned long long end = start + e820.map[i].size;
891 * Since "last" is at most 4GB, we know we'll
892 * fit in 32 bits if this condition is true
895 unsigned long gap = last - end;
908 gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
909 printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit "
911 KERN_ERR "PCI: Unassigned devices with 32bit resource "
912 "registers may break!\n");
916 * See how much we want to round up: start off with
917 * rounding to the next 1MB area.
920 while ((gapsize >> 4) > round)
922 /* Fun with two's complement */
923 pci_mem_start = (gapstart + round) & -round;
926 "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
927 pci_mem_start, gapstart, gapsize);
930 int __init arch_get_ram_range(int slot, u64 *addr, u64 *size)
934 if (slot < 0 || slot >= e820.nr_map)
936 for (i = slot; i < e820.nr_map; i++) {
937 if (e820.map[i].type != E820_RAM)
941 if (i == e820.nr_map || e820.map[i].addr > (max_pfn << PAGE_SHIFT))
943 *addr = e820.map[i].addr;
944 *size = min_t(u64, e820.map[i].size + e820.map[i].addr,
945 max_pfn << PAGE_SHIFT) - *addr;