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 memmove(&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(unsigned long start, unsigned long end)
112 unsigned long final_start, final_end;
113 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
114 struct early_res *r = &early_res[i];
115 final_start = max(start, r->start);
116 final_end = min(end, r->end);
117 if (final_start >= final_end)
119 printk(KERN_INFO " early res: %d [%lx-%lx] %s\n", i,
120 final_start, final_end - 1, r->name);
121 reserve_bootmem_generic(final_start, final_end - final_start);
125 /* Check for already reserved areas */
126 static inline int __init
127 bad_addr(unsigned long *addrp, unsigned long size, unsigned long align)
130 unsigned long addr = *addrp, last;
134 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
135 struct early_res *r = &early_res[i];
136 if (last >= r->start && addr < r->end) {
137 *addrp = addr = round_up(r->end, align);
145 /* Check for already reserved areas */
146 static inline int __init
147 bad_addr_size(unsigned long *addrp, unsigned long *sizep, unsigned long align)
150 unsigned long addr = *addrp, last;
151 unsigned long size = *sizep;
155 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
156 struct early_res *r = &early_res[i];
157 if (last > r->start && addr < r->start) {
158 size = r->start - addr;
162 if (last > r->end && addr < r->end) {
163 addr = round_up(r->end, align);
168 if (last <= r->end && addr >= r->start) {
180 * This function checks if any part of the range <start,end> is mapped
184 e820_any_mapped(unsigned long start, unsigned long end, unsigned type)
188 for (i = 0; i < e820.nr_map; i++) {
189 struct e820entry *ei = &e820.map[i];
191 if (type && ei->type != type)
193 if (ei->addr >= end || ei->addr + ei->size <= start)
199 EXPORT_SYMBOL_GPL(e820_any_mapped);
202 * This function checks if the entire range <start,end> is mapped with type.
204 * Note: this function only works correct if the e820 table is sorted and
205 * not-overlapping, which is the case
207 int __init e820_all_mapped(unsigned long start, unsigned long end,
212 for (i = 0; i < e820.nr_map; i++) {
213 struct e820entry *ei = &e820.map[i];
215 if (type && ei->type != type)
217 /* is the region (part) in overlap with the current region ?*/
218 if (ei->addr >= end || ei->addr + ei->size <= start)
221 /* if the region is at the beginning of <start,end> we move
222 * start to the end of the region since it's ok until there
224 if (ei->addr <= start)
225 start = ei->addr + ei->size;
227 * if start is now at or beyond end, we're done, full
237 * Find a free area with specified alignment in a specific range.
239 unsigned long __init find_e820_area(unsigned long start, unsigned long end,
240 unsigned long size, unsigned long align)
244 for (i = 0; i < e820.nr_map; i++) {
245 struct e820entry *ei = &e820.map[i];
246 unsigned long addr, last;
247 unsigned long ei_last;
249 if (ei->type != E820_RAM)
251 addr = round_up(ei->addr, align);
252 ei_last = ei->addr + ei->size;
254 addr = round_up(start, align);
257 while (bad_addr(&addr, size, align) && addr+size <= ei_last)
270 * Find next free range after *start
272 unsigned long __init find_e820_area_size(unsigned long start,
273 unsigned long *sizep,
278 for (i = 0; i < e820.nr_map; i++) {
279 struct e820entry *ei = &e820.map[i];
280 unsigned long addr, last;
281 unsigned long ei_last;
283 if (ei->type != E820_RAM)
285 addr = round_up(ei->addr, align);
286 ei_last = ei->addr + ei->size;
288 addr = round_up(start, align);
291 *sizep = ei_last - addr;
292 while (bad_addr_size(&addr, sizep, align) &&
293 addr + *sizep <= ei_last)
295 last = addr + *sizep;
304 * Find the highest page frame number we have available
306 unsigned long __init e820_end_of_ram(void)
308 unsigned long end_pfn;
310 end_pfn = find_max_pfn_with_active_regions();
312 if (end_pfn > max_pfn_mapped)
313 max_pfn_mapped = end_pfn;
314 if (max_pfn_mapped > MAXMEM>>PAGE_SHIFT)
315 max_pfn_mapped = MAXMEM>>PAGE_SHIFT;
316 if (end_pfn > end_user_pfn)
317 end_pfn = end_user_pfn;
318 if (end_pfn > max_pfn_mapped)
319 end_pfn = max_pfn_mapped;
321 printk(KERN_INFO "max_pfn_mapped = %lu\n", max_pfn_mapped);
326 * Mark e820 reserved areas as busy for the resource manager.
328 void __init e820_reserve_resources(void)
331 struct resource *res;
333 res = alloc_bootmem_low(sizeof(struct resource) * e820.nr_map);
334 for (i = 0; i < e820.nr_map; i++) {
335 switch (e820.map[i].type) {
336 case E820_RAM: res->name = "System RAM"; break;
337 case E820_ACPI: res->name = "ACPI Tables"; break;
338 case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
339 default: res->name = "reserved";
341 res->start = e820.map[i].addr;
342 res->end = res->start + e820.map[i].size - 1;
343 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
344 insert_resource(&iomem_resource, res);
350 * Find the ranges of physical addresses that do not correspond to
351 * e820 RAM areas and mark the corresponding pages as nosave for software
352 * suspend and suspend to RAM.
354 * This function requires the e820 map to be sorted and without any
355 * overlapping entries and assumes the first e820 area to be RAM.
357 void __init e820_mark_nosave_regions(void)
362 paddr = round_down(e820.map[0].addr + e820.map[0].size, PAGE_SIZE);
363 for (i = 1; i < e820.nr_map; i++) {
364 struct e820entry *ei = &e820.map[i];
366 if (paddr < ei->addr)
367 register_nosave_region(PFN_DOWN(paddr),
370 paddr = round_down(ei->addr + ei->size, PAGE_SIZE);
371 if (ei->type != E820_RAM)
372 register_nosave_region(PFN_UP(ei->addr),
375 if (paddr >= (end_pfn << PAGE_SHIFT))
381 * Finds an active region in the address range from start_pfn to end_pfn and
382 * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
384 static int __init e820_find_active_region(const struct e820entry *ei,
385 unsigned long start_pfn,
386 unsigned long end_pfn,
387 unsigned long *ei_startpfn,
388 unsigned long *ei_endpfn)
390 *ei_startpfn = round_up(ei->addr, PAGE_SIZE) >> PAGE_SHIFT;
391 *ei_endpfn = round_down(ei->addr + ei->size, PAGE_SIZE) >> PAGE_SHIFT;
393 /* Skip map entries smaller than a page */
394 if (*ei_startpfn >= *ei_endpfn)
397 /* Check if max_pfn_mapped should be updated */
398 if (ei->type != E820_RAM && *ei_endpfn > max_pfn_mapped)
399 max_pfn_mapped = *ei_endpfn;
401 /* Skip if map is outside the node */
402 if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
403 *ei_startpfn >= end_pfn)
406 /* Check for overlaps */
407 if (*ei_startpfn < start_pfn)
408 *ei_startpfn = start_pfn;
409 if (*ei_endpfn > end_pfn)
410 *ei_endpfn = end_pfn;
412 /* Obey end_user_pfn to save on memmap */
413 if (*ei_startpfn >= end_user_pfn)
415 if (*ei_endpfn > end_user_pfn)
416 *ei_endpfn = end_user_pfn;
421 /* Walk the e820 map and register active regions within a node */
423 e820_register_active_regions(int nid, unsigned long start_pfn,
424 unsigned long end_pfn)
426 unsigned long ei_startpfn;
427 unsigned long ei_endpfn;
430 for (i = 0; i < e820.nr_map; i++)
431 if (e820_find_active_region(&e820.map[i],
433 &ei_startpfn, &ei_endpfn))
434 add_active_range(nid, ei_startpfn, ei_endpfn);
438 * Add a memory region to the kernel e820 map.
440 void __init add_memory_region(unsigned long start, unsigned long size, int type)
445 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
449 e820.map[x].addr = start;
450 e820.map[x].size = size;
451 e820.map[x].type = type;
456 * Find the hole size (in bytes) in the memory range.
457 * @start: starting address of the memory range to scan
458 * @end: ending address of the memory range to scan
460 unsigned long __init e820_hole_size(unsigned long start, unsigned long end)
462 unsigned long start_pfn = start >> PAGE_SHIFT;
463 unsigned long end_pfn = end >> PAGE_SHIFT;
464 unsigned long ei_startpfn, ei_endpfn, ram = 0;
467 for (i = 0; i < e820.nr_map; i++) {
468 if (e820_find_active_region(&e820.map[i],
470 &ei_startpfn, &ei_endpfn))
471 ram += ei_endpfn - ei_startpfn;
473 return end - start - (ram << PAGE_SHIFT);
476 static void __init e820_print_map(char *who)
480 for (i = 0; i < e820.nr_map; i++) {
481 printk(KERN_INFO " %s: %016Lx - %016Lx ", who,
482 (unsigned long long) e820.map[i].addr,
484 (e820.map[i].addr + e820.map[i].size));
485 switch (e820.map[i].type) {
487 printk(KERN_CONT "(usable)\n");
490 printk(KERN_CONT "(reserved)\n");
493 printk(KERN_CONT "(ACPI data)\n");
496 printk(KERN_CONT "(ACPI NVS)\n");
499 printk(KERN_CONT "type %u\n", e820.map[i].type);
506 * Sanitize the BIOS e820 map.
508 * Some e820 responses include overlapping entries. The following
509 * replaces the original e820 map with a new one, removing overlaps.
512 static int __init sanitize_e820_map(struct e820entry *biosmap, char *pnr_map)
514 struct change_member {
515 struct e820entry *pbios; /* pointer to original bios entry */
516 unsigned long long addr; /* address for this change point */
518 static struct change_member change_point_list[2*E820MAX] __initdata;
519 static struct change_member *change_point[2*E820MAX] __initdata;
520 static struct e820entry *overlap_list[E820MAX] __initdata;
521 static struct e820entry new_bios[E820MAX] __initdata;
522 struct change_member *change_tmp;
523 unsigned long current_type, last_type;
524 unsigned long long last_addr;
525 int chgidx, still_changing;
528 int old_nr, new_nr, chg_nr;
532 Visually we're performing the following
533 (1,2,3,4 = memory types)...
535 Sample memory map (w/overlaps):
536 ____22__________________
537 ______________________4_
538 ____1111________________
539 _44_____________________
540 11111111________________
541 ____________________33__
542 ___________44___________
543 __________33333_________
544 ______________22________
545 ___________________2222_
546 _________111111111______
547 _____________________11_
548 _________________4______
550 Sanitized equivalent (no overlap):
551 1_______________________
552 _44_____________________
553 ___1____________________
554 ____22__________________
555 ______11________________
556 _________1______________
557 __________3_____________
558 ___________44___________
559 _____________33_________
560 _______________2________
561 ________________1_______
562 _________________4______
563 ___________________2____
564 ____________________33__
565 ______________________4_
568 /* if there's only one memory region, don't bother */
574 /* bail out if we find any unreasonable addresses in bios map */
575 for (i = 0; i < old_nr; i++)
576 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
579 /* create pointers for initial change-point information (for sorting) */
580 for (i = 0; i < 2 * old_nr; i++)
581 change_point[i] = &change_point_list[i];
583 /* record all known change-points (starting and ending addresses),
584 omitting those that are for empty memory regions */
586 for (i = 0; i < old_nr; i++) {
587 if (biosmap[i].size != 0) {
588 change_point[chgidx]->addr = biosmap[i].addr;
589 change_point[chgidx++]->pbios = &biosmap[i];
590 change_point[chgidx]->addr = biosmap[i].addr +
592 change_point[chgidx++]->pbios = &biosmap[i];
597 /* sort change-point list by memory addresses (low -> high) */
599 while (still_changing) {
601 for (i = 1; i < chg_nr; i++) {
602 unsigned long long curaddr, lastaddr;
603 unsigned long long curpbaddr, lastpbaddr;
605 curaddr = change_point[i]->addr;
606 lastaddr = change_point[i - 1]->addr;
607 curpbaddr = change_point[i]->pbios->addr;
608 lastpbaddr = change_point[i - 1]->pbios->addr;
611 * swap entries, when:
613 * curaddr > lastaddr or
614 * curaddr == lastaddr and curaddr == curpbaddr and
615 * lastaddr != lastpbaddr
617 if (curaddr < lastaddr ||
618 (curaddr == lastaddr && curaddr == curpbaddr &&
619 lastaddr != lastpbaddr)) {
620 change_tmp = change_point[i];
621 change_point[i] = change_point[i-1];
622 change_point[i-1] = change_tmp;
628 /* create a new bios memory map, removing overlaps */
629 overlap_entries = 0; /* number of entries in the overlap table */
630 new_bios_entry = 0; /* index for creating new bios map entries */
631 last_type = 0; /* start with undefined memory type */
632 last_addr = 0; /* start with 0 as last starting address */
634 /* loop through change-points, determining affect on the new bios map */
635 for (chgidx = 0; chgidx < chg_nr; chgidx++) {
636 /* keep track of all overlapping bios entries */
637 if (change_point[chgidx]->addr ==
638 change_point[chgidx]->pbios->addr) {
640 * add map entry to overlap list (> 1 entry
641 * implies an overlap)
643 overlap_list[overlap_entries++] =
644 change_point[chgidx]->pbios;
647 * remove entry from list (order independent,
650 for (i = 0; i < overlap_entries; i++) {
651 if (overlap_list[i] ==
652 change_point[chgidx]->pbios)
654 overlap_list[overlap_entries-1];
659 * if there are overlapping entries, decide which
660 * "type" to use (larger value takes precedence --
661 * 1=usable, 2,3,4,4+=unusable)
664 for (i = 0; i < overlap_entries; i++)
665 if (overlap_list[i]->type > current_type)
666 current_type = overlap_list[i]->type;
668 * continue building up new bios map based on this
671 if (current_type != last_type) {
672 if (last_type != 0) {
673 new_bios[new_bios_entry].size =
674 change_point[chgidx]->addr - last_addr;
676 * move forward only if the new size
679 if (new_bios[new_bios_entry].size != 0)
681 * no more space left for new
684 if (++new_bios_entry >= E820MAX)
687 if (current_type != 0) {
688 new_bios[new_bios_entry].addr =
689 change_point[chgidx]->addr;
690 new_bios[new_bios_entry].type = current_type;
691 last_addr = change_point[chgidx]->addr;
693 last_type = current_type;
696 /* retain count for new bios entries */
697 new_nr = new_bios_entry;
699 /* copy new bios mapping into original location */
700 memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
707 * Copy the BIOS e820 map into a safe place.
709 * Sanity-check it while we're at it..
711 * If we're lucky and live on a modern system, the setup code
712 * will have given us a memory map that we can use to properly
713 * set up memory. If we aren't, we'll fake a memory map.
715 static int __init copy_e820_map(struct e820entry *biosmap, int nr_map)
717 /* Only one memory region (or negative)? Ignore it */
722 u64 start = biosmap->addr;
723 u64 size = biosmap->size;
724 u64 end = start + size;
725 u32 type = biosmap->type;
727 /* Overflow in 64 bits? Ignore the memory map. */
731 add_memory_region(start, size, type);
732 } while (biosmap++, --nr_map);
736 static void early_panic(char *msg)
742 /* We're not void only for x86 32-bit compat */
743 char * __init machine_specific_memory_setup(void)
745 char *who = "BIOS-e820";
747 * Try to copy the BIOS-supplied E820-map.
749 * Otherwise fake a memory map; one section from 0k->640k,
750 * the next section from 1mb->appropriate_mem_k
752 sanitize_e820_map(boot_params.e820_map, &boot_params.e820_entries);
753 if (copy_e820_map(boot_params.e820_map, boot_params.e820_entries) < 0)
754 early_panic("Cannot find a valid memory map");
755 printk(KERN_INFO "BIOS-provided physical RAM map:\n");
758 /* In case someone cares... */
762 static int __init parse_memopt(char *p)
766 end_user_pfn = memparse(p, &p);
767 end_user_pfn >>= PAGE_SHIFT;
770 early_param("mem", parse_memopt);
772 static int userdef __initdata;
774 static int __init parse_memmap_opt(char *p)
777 unsigned long long start_at, mem_size;
779 if (!strcmp(p, "exactmap")) {
780 #ifdef CONFIG_CRASH_DUMP
782 * If we are doing a crash dump, we still need to know
783 * the real mem size before original memory map is
786 e820_register_active_regions(0, 0, -1UL);
787 saved_max_pfn = e820_end_of_ram();
788 remove_all_active_ranges();
797 mem_size = memparse(p, &p);
803 start_at = memparse(p+1, &p);
804 add_memory_region(start_at, mem_size, E820_RAM);
805 } else if (*p == '#') {
806 start_at = memparse(p+1, &p);
807 add_memory_region(start_at, mem_size, E820_ACPI);
808 } else if (*p == '$') {
809 start_at = memparse(p+1, &p);
810 add_memory_region(start_at, mem_size, E820_RESERVED);
812 end_user_pfn = (mem_size >> PAGE_SHIFT);
814 return *p == '\0' ? 0 : -EINVAL;
816 early_param("memmap", parse_memmap_opt);
818 void __init finish_e820_parsing(void)
821 char nr = e820.nr_map;
823 if (sanitize_e820_map(e820.map, &nr) < 0)
824 early_panic("Invalid user supplied memory map");
827 printk(KERN_INFO "user-defined physical RAM map:\n");
828 e820_print_map("user");
832 void __init update_memory_range(u64 start, u64 size, unsigned old_type,
837 BUG_ON(old_type == new_type);
839 for (i = 0; i < e820.nr_map; i++) {
840 struct e820entry *ei = &e820.map[i];
841 u64 final_start, final_end;
842 if (ei->type != old_type)
844 /* totally covered? */
845 if (ei->addr >= start && ei->size <= size) {
849 /* partially covered */
850 final_start = max(start, ei->addr);
851 final_end = min(start + size, ei->addr + ei->size);
852 if (final_start >= final_end)
854 add_memory_region(final_start, final_end - final_start,
859 void __init update_e820(void)
863 nr_map = e820.nr_map;
864 if (sanitize_e820_map(e820.map, &nr_map))
866 e820.nr_map = nr_map;
867 printk(KERN_INFO "modified physical RAM map:\n");
868 e820_print_map("modified");
871 unsigned long pci_mem_start = 0xaeedbabe;
872 EXPORT_SYMBOL(pci_mem_start);
875 * Search for the biggest gap in the low 32 bits of the e820
876 * memory space. We pass this space to PCI to assign MMIO resources
877 * for hotplug or unconfigured devices in.
878 * Hopefully the BIOS let enough space left.
880 __init void e820_setup_gap(void)
882 unsigned long gapstart, gapsize, round;
887 last = 0x100000000ull;
888 gapstart = 0x10000000;
892 unsigned long long start = e820.map[i].addr;
893 unsigned long long end = start + e820.map[i].size;
896 * Since "last" is at most 4GB, we know we'll
897 * fit in 32 bits if this condition is true
900 unsigned long gap = last - end;
913 gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
914 printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit "
916 KERN_ERR "PCI: Unassigned devices with 32bit resource "
917 "registers may break!\n");
921 * See how much we want to round up: start off with
922 * rounding to the next 1MB area.
925 while ((gapsize >> 4) > round)
927 /* Fun with two's complement */
928 pci_mem_start = (gapstart + round) & -round;
931 "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
932 pci_mem_start, gapstart, gapsize);
935 int __init arch_get_ram_range(int slot, u64 *addr, u64 *size)
939 if (slot < 0 || slot >= e820.nr_map)
941 for (i = slot; i < e820.nr_map; i++) {
942 if (e820.map[i].type != E820_RAM)
946 if (i == e820.nr_map || e820.map[i].addr > (max_pfn << PAGE_SHIFT))
948 *addr = e820.map[i].addr;
949 *size = min_t(u64, e820.map[i].size + e820.map[i].addr,
950 max_pfn << PAGE_SHIFT) - *addr;