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;
50 /* Check for some hardcoded bad areas that early boot is not allowed to touch */
51 static inline int bad_addr(unsigned long *addrp, unsigned long size)
53 unsigned long addr = *addrp, last = addr + size;
55 /* various gunk below that needed for SMP startup */
57 *addrp = PAGE_ALIGN(0x8000);
61 /* direct mapping tables of the kernel */
62 if (last >= table_start<<PAGE_SHIFT && addr < table_end<<PAGE_SHIFT) {
63 *addrp = PAGE_ALIGN(table_end << PAGE_SHIFT);
68 #ifdef CONFIG_BLK_DEV_INITRD
69 if (boot_params.hdr.type_of_loader && boot_params.hdr.ramdisk_image) {
70 unsigned long ramdisk_image = boot_params.hdr.ramdisk_image;
71 unsigned long ramdisk_size = boot_params.hdr.ramdisk_size;
72 unsigned long ramdisk_end = ramdisk_image+ramdisk_size;
74 if (last >= ramdisk_image && addr < ramdisk_end) {
75 *addrp = PAGE_ALIGN(ramdisk_end);
81 if (last >= __pa_symbol(&_text) && addr < __pa_symbol(&_end)) {
82 *addrp = PAGE_ALIGN(__pa_symbol(&_end));
86 if (last >= ebda_addr && addr < ebda_addr + ebda_size) {
87 *addrp = PAGE_ALIGN(ebda_addr + ebda_size);
92 /* NUMA memory to node map */
93 if (last >= nodemap_addr && addr < nodemap_addr + nodemap_size) {
94 *addrp = nodemap_addr + nodemap_size;
98 /* XXX ramdisk image here? */
103 * This function checks if any part of the range <start,end> is mapped
107 e820_any_mapped(unsigned long start, unsigned long end, unsigned type)
111 for (i = 0; i < e820.nr_map; i++) {
112 struct e820entry *ei = &e820.map[i];
114 if (type && ei->type != type)
116 if (ei->addr >= end || ei->addr + ei->size <= start)
122 EXPORT_SYMBOL_GPL(e820_any_mapped);
125 * This function checks if the entire range <start,end> is mapped with type.
127 * Note: this function only works correct if the e820 table is sorted and
128 * not-overlapping, which is the case
130 int __init e820_all_mapped(unsigned long start, unsigned long end,
135 for (i = 0; i < e820.nr_map; i++) {
136 struct e820entry *ei = &e820.map[i];
138 if (type && ei->type != type)
140 /* is the region (part) in overlap with the current region ?*/
141 if (ei->addr >= end || ei->addr + ei->size <= start)
144 /* if the region is at the beginning of <start,end> we move
145 * start to the end of the region since it's ok until there
147 if (ei->addr <= start)
148 start = ei->addr + ei->size;
150 * if start is now at or beyond end, we're done, full
160 * Find a free area in a specific range.
162 unsigned long __init find_e820_area(unsigned long start, unsigned long end,
167 for (i = 0; i < e820.nr_map; i++) {
168 struct e820entry *ei = &e820.map[i];
169 unsigned long addr = ei->addr, last;
171 if (ei->type != E820_RAM)
175 if (addr > ei->addr + ei->size)
177 while (bad_addr(&addr, size) && addr+size <= ei->addr+ei->size)
179 last = PAGE_ALIGN(addr) + size;
180 if (last > ei->addr + ei->size)
190 * Find the highest page frame number we have available
192 unsigned long __init e820_end_of_ram(void)
194 unsigned long end_pfn;
196 end_pfn = find_max_pfn_with_active_regions();
198 if (end_pfn > end_pfn_map)
199 end_pfn_map = end_pfn;
200 if (end_pfn_map > MAXMEM>>PAGE_SHIFT)
201 end_pfn_map = MAXMEM>>PAGE_SHIFT;
202 if (end_pfn > end_user_pfn)
203 end_pfn = end_user_pfn;
204 if (end_pfn > end_pfn_map)
205 end_pfn = end_pfn_map;
207 printk(KERN_INFO "end_pfn_map = %lu\n", end_pfn_map);
212 * Mark e820 reserved areas as busy for the resource manager.
214 void __init e820_reserve_resources(struct resource *code_resource,
215 struct resource *data_resource, struct resource *bss_resource)
218 for (i = 0; i < e820.nr_map; i++) {
219 struct resource *res;
220 res = alloc_bootmem_low(sizeof(struct resource));
221 switch (e820.map[i].type) {
222 case E820_RAM: res->name = "System RAM"; break;
223 case E820_ACPI: res->name = "ACPI Tables"; break;
224 case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
225 default: res->name = "reserved";
227 res->start = e820.map[i].addr;
228 res->end = res->start + e820.map[i].size - 1;
229 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
230 request_resource(&iomem_resource, res);
231 if (e820.map[i].type == E820_RAM) {
233 * We don't know which RAM region contains kernel data,
234 * so we try it repeatedly and let the resource manager
237 request_resource(res, code_resource);
238 request_resource(res, data_resource);
239 request_resource(res, bss_resource);
241 if (crashk_res.start != crashk_res.end)
242 request_resource(res, &crashk_res);
249 * Find the ranges of physical addresses that do not correspond to
250 * e820 RAM areas and mark the corresponding pages as nosave for software
251 * suspend and suspend to RAM.
253 * This function requires the e820 map to be sorted and without any
254 * overlapping entries and assumes the first e820 area to be RAM.
256 void __init e820_mark_nosave_regions(void)
261 paddr = round_down(e820.map[0].addr + e820.map[0].size, PAGE_SIZE);
262 for (i = 1; i < e820.nr_map; i++) {
263 struct e820entry *ei = &e820.map[i];
265 if (paddr < ei->addr)
266 register_nosave_region(PFN_DOWN(paddr),
269 paddr = round_down(ei->addr + ei->size, PAGE_SIZE);
270 if (ei->type != E820_RAM)
271 register_nosave_region(PFN_UP(ei->addr),
274 if (paddr >= (end_pfn << PAGE_SHIFT))
280 * Finds an active region in the address range from start_pfn to end_pfn and
281 * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
283 static int __init e820_find_active_region(const struct e820entry *ei,
284 unsigned long start_pfn,
285 unsigned long end_pfn,
286 unsigned long *ei_startpfn,
287 unsigned long *ei_endpfn)
289 *ei_startpfn = round_up(ei->addr, PAGE_SIZE) >> PAGE_SHIFT;
290 *ei_endpfn = round_down(ei->addr + ei->size, PAGE_SIZE) >> PAGE_SHIFT;
292 /* Skip map entries smaller than a page */
293 if (*ei_startpfn >= *ei_endpfn)
296 /* Check if end_pfn_map should be updated */
297 if (ei->type != E820_RAM && *ei_endpfn > end_pfn_map)
298 end_pfn_map = *ei_endpfn;
300 /* Skip if map is outside the node */
301 if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
302 *ei_startpfn >= end_pfn)
305 /* Check for overlaps */
306 if (*ei_startpfn < start_pfn)
307 *ei_startpfn = start_pfn;
308 if (*ei_endpfn > end_pfn)
309 *ei_endpfn = end_pfn;
311 /* Obey end_user_pfn to save on memmap */
312 if (*ei_startpfn >= end_user_pfn)
314 if (*ei_endpfn > end_user_pfn)
315 *ei_endpfn = end_user_pfn;
320 /* Walk the e820 map and register active regions within a node */
322 e820_register_active_regions(int nid, unsigned long start_pfn,
323 unsigned long end_pfn)
325 unsigned long ei_startpfn;
326 unsigned long ei_endpfn;
329 for (i = 0; i < e820.nr_map; i++)
330 if (e820_find_active_region(&e820.map[i],
332 &ei_startpfn, &ei_endpfn))
333 add_active_range(nid, ei_startpfn, ei_endpfn);
337 * Add a memory region to the kernel e820 map.
339 void __init add_memory_region(unsigned long start, unsigned long size, int type)
344 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
348 e820.map[x].addr = start;
349 e820.map[x].size = size;
350 e820.map[x].type = type;
355 * Find the hole size (in bytes) in the memory range.
356 * @start: starting address of the memory range to scan
357 * @end: ending address of the memory range to scan
359 unsigned long __init e820_hole_size(unsigned long start, unsigned long end)
361 unsigned long start_pfn = start >> PAGE_SHIFT;
362 unsigned long end_pfn = end >> PAGE_SHIFT;
363 unsigned long ei_startpfn, ei_endpfn, ram = 0;
366 for (i = 0; i < e820.nr_map; i++) {
367 if (e820_find_active_region(&e820.map[i],
369 &ei_startpfn, &ei_endpfn))
370 ram += ei_endpfn - ei_startpfn;
372 return end - start - (ram << PAGE_SHIFT);
375 static void __init e820_print_map(char *who)
379 for (i = 0; i < e820.nr_map; i++) {
380 printk(KERN_INFO " %s: %016Lx - %016Lx ", who,
381 (unsigned long long) e820.map[i].addr,
383 (e820.map[i].addr + e820.map[i].size));
384 switch (e820.map[i].type) {
386 printk(KERN_CONT "(usable)\n");
389 printk(KERN_CONT "(reserved)\n");
392 printk(KERN_CONT "(ACPI data)\n");
395 printk(KERN_CONT "(ACPI NVS)\n");
398 printk(KERN_CONT "type %u\n", e820.map[i].type);
405 * Sanitize the BIOS e820 map.
407 * Some e820 responses include overlapping entries. The following
408 * replaces the original e820 map with a new one, removing overlaps.
411 static int __init sanitize_e820_map(struct e820entry *biosmap, char *pnr_map)
413 struct change_member {
414 struct e820entry *pbios; /* pointer to original bios entry */
415 unsigned long long addr; /* address for this change point */
417 static struct change_member change_point_list[2*E820MAX] __initdata;
418 static struct change_member *change_point[2*E820MAX] __initdata;
419 static struct e820entry *overlap_list[E820MAX] __initdata;
420 static struct e820entry new_bios[E820MAX] __initdata;
421 struct change_member *change_tmp;
422 unsigned long current_type, last_type;
423 unsigned long long last_addr;
424 int chgidx, still_changing;
427 int old_nr, new_nr, chg_nr;
431 Visually we're performing the following
432 (1,2,3,4 = memory types)...
434 Sample memory map (w/overlaps):
435 ____22__________________
436 ______________________4_
437 ____1111________________
438 _44_____________________
439 11111111________________
440 ____________________33__
441 ___________44___________
442 __________33333_________
443 ______________22________
444 ___________________2222_
445 _________111111111______
446 _____________________11_
447 _________________4______
449 Sanitized equivalent (no overlap):
450 1_______________________
451 _44_____________________
452 ___1____________________
453 ____22__________________
454 ______11________________
455 _________1______________
456 __________3_____________
457 ___________44___________
458 _____________33_________
459 _______________2________
460 ________________1_______
461 _________________4______
462 ___________________2____
463 ____________________33__
464 ______________________4_
467 /* if there's only one memory region, don't bother */
473 /* bail out if we find any unreasonable addresses in bios map */
474 for (i = 0; i < old_nr; i++)
475 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
478 /* create pointers for initial change-point information (for sorting) */
479 for (i = 0; i < 2 * old_nr; i++)
480 change_point[i] = &change_point_list[i];
482 /* record all known change-points (starting and ending addresses),
483 omitting those that are for empty memory regions */
485 for (i = 0; i < old_nr; i++) {
486 if (biosmap[i].size != 0) {
487 change_point[chgidx]->addr = biosmap[i].addr;
488 change_point[chgidx++]->pbios = &biosmap[i];
489 change_point[chgidx]->addr = biosmap[i].addr +
491 change_point[chgidx++]->pbios = &biosmap[i];
496 /* sort change-point list by memory addresses (low -> high) */
498 while (still_changing) {
500 for (i = 1; i < chg_nr; i++) {
501 unsigned long long curaddr, lastaddr;
502 unsigned long long curpbaddr, lastpbaddr;
504 curaddr = change_point[i]->addr;
505 lastaddr = change_point[i - 1]->addr;
506 curpbaddr = change_point[i]->pbios->addr;
507 lastpbaddr = change_point[i - 1]->pbios->addr;
510 * swap entries, when:
512 * curaddr > lastaddr or
513 * curaddr == lastaddr and curaddr == curpbaddr and
514 * lastaddr != lastpbaddr
516 if (curaddr < lastaddr ||
517 (curaddr == lastaddr && curaddr == curpbaddr &&
518 lastaddr != lastpbaddr)) {
519 change_tmp = change_point[i];
520 change_point[i] = change_point[i-1];
521 change_point[i-1] = change_tmp;
527 /* create a new bios memory map, removing overlaps */
528 overlap_entries = 0; /* number of entries in the overlap table */
529 new_bios_entry = 0; /* index for creating new bios map entries */
530 last_type = 0; /* start with undefined memory type */
531 last_addr = 0; /* start with 0 as last starting address */
533 /* loop through change-points, determining affect on the new bios map */
534 for (chgidx = 0; chgidx < chg_nr; chgidx++) {
535 /* keep track of all overlapping bios entries */
536 if (change_point[chgidx]->addr ==
537 change_point[chgidx]->pbios->addr) {
539 * add map entry to overlap list (> 1 entry
540 * implies an overlap)
542 overlap_list[overlap_entries++] =
543 change_point[chgidx]->pbios;
546 * remove entry from list (order independent,
549 for (i = 0; i < overlap_entries; i++) {
550 if (overlap_list[i] ==
551 change_point[chgidx]->pbios)
553 overlap_list[overlap_entries-1];
558 * if there are overlapping entries, decide which
559 * "type" to use (larger value takes precedence --
560 * 1=usable, 2,3,4,4+=unusable)
563 for (i = 0; i < overlap_entries; i++)
564 if (overlap_list[i]->type > current_type)
565 current_type = overlap_list[i]->type;
567 * continue building up new bios map based on this
570 if (current_type != last_type) {
571 if (last_type != 0) {
572 new_bios[new_bios_entry].size =
573 change_point[chgidx]->addr - last_addr;
575 * move forward only if the new size
578 if (new_bios[new_bios_entry].size != 0)
580 * no more space left for new
583 if (++new_bios_entry >= E820MAX)
586 if (current_type != 0) {
587 new_bios[new_bios_entry].addr =
588 change_point[chgidx]->addr;
589 new_bios[new_bios_entry].type = current_type;
590 last_addr = change_point[chgidx]->addr;
592 last_type = current_type;
595 /* retain count for new bios entries */
596 new_nr = new_bios_entry;
598 /* copy new bios mapping into original location */
599 memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
606 * Copy the BIOS e820 map into a safe place.
608 * Sanity-check it while we're at it..
610 * If we're lucky and live on a modern system, the setup code
611 * will have given us a memory map that we can use to properly
612 * set up memory. If we aren't, we'll fake a memory map.
614 static int __init copy_e820_map(struct e820entry *biosmap, int nr_map)
616 /* Only one memory region (or negative)? Ignore it */
621 unsigned long start = biosmap->addr;
622 unsigned long size = biosmap->size;
623 unsigned long end = start + size;
624 unsigned long type = biosmap->type;
626 /* Overflow in 64 bits? Ignore the memory map. */
630 add_memory_region(start, size, type);
631 } while (biosmap++, --nr_map);
635 static void early_panic(char *msg)
641 void __init setup_memory_region(void)
644 * Try to copy the BIOS-supplied E820-map.
646 * Otherwise fake a memory map; one section from 0k->640k,
647 * the next section from 1mb->appropriate_mem_k
649 sanitize_e820_map(boot_params.e820_map, &boot_params.e820_entries);
650 if (copy_e820_map(boot_params.e820_map, boot_params.e820_entries) < 0)
651 early_panic("Cannot find a valid memory map");
652 printk(KERN_INFO "BIOS-provided physical RAM map:\n");
653 e820_print_map("BIOS-e820");
656 static int __init parse_memopt(char *p)
660 end_user_pfn = memparse(p, &p);
661 end_user_pfn >>= PAGE_SHIFT;
664 early_param("mem", parse_memopt);
666 static int userdef __initdata;
668 static int __init parse_memmap_opt(char *p)
671 unsigned long long start_at, mem_size;
673 if (!strcmp(p, "exactmap")) {
674 #ifdef CONFIG_CRASH_DUMP
676 * If we are doing a crash dump, we still need to know
677 * the real mem size before original memory map is
680 e820_register_active_regions(0, 0, -1UL);
681 saved_max_pfn = e820_end_of_ram();
682 remove_all_active_ranges();
691 mem_size = memparse(p, &p);
697 start_at = memparse(p+1, &p);
698 add_memory_region(start_at, mem_size, E820_RAM);
699 } else if (*p == '#') {
700 start_at = memparse(p+1, &p);
701 add_memory_region(start_at, mem_size, E820_ACPI);
702 } else if (*p == '$') {
703 start_at = memparse(p+1, &p);
704 add_memory_region(start_at, mem_size, E820_RESERVED);
706 end_user_pfn = (mem_size >> PAGE_SHIFT);
708 return *p == '\0' ? 0 : -EINVAL;
710 early_param("memmap", parse_memmap_opt);
712 void __init finish_e820_parsing(void)
715 char nr = e820.nr_map;
717 if (sanitize_e820_map(e820.map, &nr) < 0)
718 early_panic("Invalid user supplied memory map");
721 printk(KERN_INFO "user-defined physical RAM map:\n");
722 e820_print_map("user");
726 unsigned long pci_mem_start = 0xaeedbabe;
727 EXPORT_SYMBOL(pci_mem_start);
730 * Search for the biggest gap in the low 32 bits of the e820
731 * memory space. We pass this space to PCI to assign MMIO resources
732 * for hotplug or unconfigured devices in.
733 * Hopefully the BIOS let enough space left.
735 __init void e820_setup_gap(void)
737 unsigned long gapstart, gapsize, round;
742 last = 0x100000000ull;
743 gapstart = 0x10000000;
747 unsigned long long start = e820.map[i].addr;
748 unsigned long long end = start + e820.map[i].size;
751 * Since "last" is at most 4GB, we know we'll
752 * fit in 32 bits if this condition is true
755 unsigned long gap = last - end;
768 gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
769 printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit "
771 KERN_ERR "PCI: Unassigned devices with 32bit resource "
772 "registers may break!\n");
776 * See how much we want to round up: start off with
777 * rounding to the next 1MB area.
780 while ((gapsize >> 4) > round)
782 /* Fun with two's complement */
783 pci_mem_start = (gapstart + round) & -round;
786 "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
787 pci_mem_start, gapstart, gapsize);
790 int __init arch_get_ram_range(int slot, u64 *addr, u64 *size)
794 if (slot < 0 || slot >= e820.nr_map)
796 for (i = slot; i < e820.nr_map; i++) {
797 if (e820.map[i].type != E820_RAM)
801 if (i == e820.nr_map || e820.map[i].addr > (max_pfn << PAGE_SHIFT))
803 *addr = e820.map[i].addr;
804 *size = min_t(u64, e820.map[i].size + e820.map[i].addr,
805 max_pfn << PAGE_SHIFT) - *addr;