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;
37 EXPORT_SYMBOL(end_pfn);
40 * end_pfn only includes RAM, while end_pfn_map includes all e820 entries.
41 * The direct mapping extends to end_pfn_map, so that we can directly access
42 * apertures, ACPI and other tables without having to play with fixmaps.
44 unsigned long end_pfn_map;
47 * Last pfn which the user wants to use.
49 static unsigned long __initdata end_user_pfn = MAXMEM>>PAGE_SHIFT;
51 extern struct resource code_resource, data_resource, bss_resource;
53 /* Check for some hardcoded bad areas that early boot is not allowed to touch */
54 static inline int bad_addr(unsigned long *addrp, unsigned long size)
56 unsigned long addr = *addrp, last = addr + size;
58 /* various gunk below that needed for SMP startup */
60 *addrp = PAGE_ALIGN(0x8000);
64 /* direct mapping tables of the kernel */
65 if (last >= table_start<<PAGE_SHIFT && addr < table_end<<PAGE_SHIFT) {
66 *addrp = PAGE_ALIGN(table_end << PAGE_SHIFT);
71 #ifdef CONFIG_BLK_DEV_INITRD
72 if (boot_params.hdr.type_of_loader && boot_params.hdr.ramdisk_image) {
73 unsigned long ramdisk_image = boot_params.hdr.ramdisk_image;
74 unsigned long ramdisk_size = boot_params.hdr.ramdisk_size;
75 unsigned long ramdisk_end = ramdisk_image+ramdisk_size;
77 if (last >= ramdisk_image && addr < ramdisk_end) {
78 *addrp = PAGE_ALIGN(ramdisk_end);
84 if (last >= __pa_symbol(&_text) && addr < __pa_symbol(&_end)) {
85 *addrp = PAGE_ALIGN(__pa_symbol(&_end));
89 if (last >= ebda_addr && addr < ebda_addr + ebda_size) {
90 *addrp = PAGE_ALIGN(ebda_addr + ebda_size);
95 /* NUMA memory to node map */
96 if (last >= nodemap_addr && addr < nodemap_addr + nodemap_size) {
97 *addrp = nodemap_addr + nodemap_size;
101 /* XXX ramdisk image here? */
106 * This function checks if any part of the range <start,end> is mapped
110 e820_any_mapped(unsigned long start, unsigned long end, unsigned type)
114 for (i = 0; i < e820.nr_map; i++) {
115 struct e820entry *ei = &e820.map[i];
117 if (type && ei->type != type)
119 if (ei->addr >= end || ei->addr + ei->size <= start)
125 EXPORT_SYMBOL_GPL(e820_any_mapped);
128 * This function checks if the entire range <start,end> is mapped with type.
130 * Note: this function only works correct if the e820 table is sorted and
131 * not-overlapping, which is the case
133 int __init e820_all_mapped(unsigned long start, unsigned long end,
138 for (i = 0; i < e820.nr_map; i++) {
139 struct e820entry *ei = &e820.map[i];
141 if (type && ei->type != type)
143 /* is the region (part) in overlap with the current region ?*/
144 if (ei->addr >= end || ei->addr + ei->size <= start)
147 /* if the region is at the beginning of <start,end> we move
148 * start to the end of the region since it's ok until there
150 if (ei->addr <= start)
151 start = ei->addr + ei->size;
153 * if start is now at or beyond end, we're done, full
163 * Find a free area in a specific range.
165 unsigned long __init find_e820_area(unsigned long start, unsigned long end,
170 for (i = 0; i < e820.nr_map; i++) {
171 struct e820entry *ei = &e820.map[i];
172 unsigned long addr = ei->addr, last;
174 if (ei->type != E820_RAM)
178 if (addr > ei->addr + ei->size)
180 while (bad_addr(&addr, size) && addr+size <= ei->addr+ei->size)
182 last = PAGE_ALIGN(addr) + size;
183 if (last > ei->addr + ei->size)
193 * Find the highest page frame number we have available
195 unsigned long __init e820_end_of_ram(void)
197 unsigned long end_pfn;
199 end_pfn = find_max_pfn_with_active_regions();
201 if (end_pfn > end_pfn_map)
202 end_pfn_map = end_pfn;
203 if (end_pfn_map > MAXMEM>>PAGE_SHIFT)
204 end_pfn_map = MAXMEM>>PAGE_SHIFT;
205 if (end_pfn > end_user_pfn)
206 end_pfn = end_user_pfn;
207 if (end_pfn > end_pfn_map)
208 end_pfn = end_pfn_map;
210 printk(KERN_INFO "end_pfn_map = %lu\n", end_pfn_map);
215 * Mark e820 reserved areas as busy for the resource manager.
217 void __init e820_reserve_resources(void)
220 for (i = 0; i < e820.nr_map; i++) {
221 struct resource *res;
222 res = alloc_bootmem_low(sizeof(struct resource));
223 switch (e820.map[i].type) {
224 case E820_RAM: res->name = "System RAM"; break;
225 case E820_ACPI: res->name = "ACPI Tables"; break;
226 case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
227 default: res->name = "reserved";
229 res->start = e820.map[i].addr;
230 res->end = res->start + e820.map[i].size - 1;
231 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
232 request_resource(&iomem_resource, res);
233 if (e820.map[i].type == E820_RAM) {
235 * We don't know which RAM region contains kernel data,
236 * so we try it repeatedly and let the resource manager
239 request_resource(res, &code_resource);
240 request_resource(res, &data_resource);
241 request_resource(res, &bss_resource);
243 if (crashk_res.start != crashk_res.end)
244 request_resource(res, &crashk_res);
251 * Find the ranges of physical addresses that do not correspond to
252 * e820 RAM areas and mark the corresponding pages as nosave for software
253 * suspend and suspend to RAM.
255 * This function requires the e820 map to be sorted and without any
256 * overlapping entries and assumes the first e820 area to be RAM.
258 void __init e820_mark_nosave_regions(void)
263 paddr = round_down(e820.map[0].addr + e820.map[0].size, PAGE_SIZE);
264 for (i = 1; i < e820.nr_map; i++) {
265 struct e820entry *ei = &e820.map[i];
267 if (paddr < ei->addr)
268 register_nosave_region(PFN_DOWN(paddr),
271 paddr = round_down(ei->addr + ei->size, PAGE_SIZE);
272 if (ei->type != E820_RAM)
273 register_nosave_region(PFN_UP(ei->addr),
276 if (paddr >= (end_pfn << PAGE_SHIFT))
282 * Finds an active region in the address range from start_pfn to end_pfn and
283 * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
285 static int __init e820_find_active_region(const struct e820entry *ei,
286 unsigned long start_pfn,
287 unsigned long end_pfn,
288 unsigned long *ei_startpfn,
289 unsigned long *ei_endpfn)
291 *ei_startpfn = round_up(ei->addr, PAGE_SIZE) >> PAGE_SHIFT;
292 *ei_endpfn = round_down(ei->addr + ei->size, PAGE_SIZE) >> PAGE_SHIFT;
294 /* Skip map entries smaller than a page */
295 if (*ei_startpfn >= *ei_endpfn)
298 /* Check if end_pfn_map should be updated */
299 if (ei->type != E820_RAM && *ei_endpfn > end_pfn_map)
300 end_pfn_map = *ei_endpfn;
302 /* Skip if map is outside the node */
303 if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
304 *ei_startpfn >= end_pfn)
307 /* Check for overlaps */
308 if (*ei_startpfn < start_pfn)
309 *ei_startpfn = start_pfn;
310 if (*ei_endpfn > end_pfn)
311 *ei_endpfn = end_pfn;
313 /* Obey end_user_pfn to save on memmap */
314 if (*ei_startpfn >= end_user_pfn)
316 if (*ei_endpfn > end_user_pfn)
317 *ei_endpfn = end_user_pfn;
322 /* Walk the e820 map and register active regions within a node */
324 e820_register_active_regions(int nid, unsigned long start_pfn,
325 unsigned long end_pfn)
327 unsigned long ei_startpfn;
328 unsigned long ei_endpfn;
331 for (i = 0; i < e820.nr_map; i++)
332 if (e820_find_active_region(&e820.map[i],
334 &ei_startpfn, &ei_endpfn))
335 add_active_range(nid, ei_startpfn, ei_endpfn);
339 * Add a memory region to the kernel e820 map.
341 void __init add_memory_region(unsigned long start, unsigned long size, int type)
346 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
350 e820.map[x].addr = start;
351 e820.map[x].size = size;
352 e820.map[x].type = type;
357 * Find the hole size (in bytes) in the memory range.
358 * @start: starting address of the memory range to scan
359 * @end: ending address of the memory range to scan
361 unsigned long __init e820_hole_size(unsigned long start, unsigned long end)
363 unsigned long start_pfn = start >> PAGE_SHIFT;
364 unsigned long end_pfn = end >> PAGE_SHIFT;
365 unsigned long ei_startpfn, ei_endpfn, ram = 0;
368 for (i = 0; i < e820.nr_map; i++) {
369 if (e820_find_active_region(&e820.map[i],
371 &ei_startpfn, &ei_endpfn))
372 ram += ei_endpfn - ei_startpfn;
374 return end - start - (ram << PAGE_SHIFT);
377 void __init e820_print_map(char *who)
381 for (i = 0; i < e820.nr_map; i++) {
382 printk(KERN_INFO " %s: %016Lx - %016Lx ", who,
383 (unsigned long long) e820.map[i].addr,
385 (e820.map[i].addr + e820.map[i].size));
386 switch (e820.map[i].type) {
388 printk(KERN_CONT "(usable)\n");
391 printk(KERN_CONT "(reserved)\n");
394 printk(KERN_CONT "(ACPI data)\n");
397 printk(KERN_CONT "(ACPI NVS)\n");
400 printk(KERN_CONT "type %u\n", e820.map[i].type);
407 * Sanitize the BIOS e820 map.
409 * Some e820 responses include overlapping entries. The following
410 * replaces the original e820 map with a new one, removing overlaps.
413 static int __init sanitize_e820_map(struct e820entry *biosmap, char *pnr_map)
415 struct change_member {
416 struct e820entry *pbios; /* pointer to original bios entry */
417 unsigned long long addr; /* address for this change point */
419 static struct change_member change_point_list[2*E820MAX] __initdata;
420 static struct change_member *change_point[2*E820MAX] __initdata;
421 static struct e820entry *overlap_list[E820MAX] __initdata;
422 static struct e820entry new_bios[E820MAX] __initdata;
423 struct change_member *change_tmp;
424 unsigned long current_type, last_type;
425 unsigned long long last_addr;
426 int chgidx, still_changing;
429 int old_nr, new_nr, chg_nr;
433 Visually we're performing the following
434 (1,2,3,4 = memory types)...
436 Sample memory map (w/overlaps):
437 ____22__________________
438 ______________________4_
439 ____1111________________
440 _44_____________________
441 11111111________________
442 ____________________33__
443 ___________44___________
444 __________33333_________
445 ______________22________
446 ___________________2222_
447 _________111111111______
448 _____________________11_
449 _________________4______
451 Sanitized equivalent (no overlap):
452 1_______________________
453 _44_____________________
454 ___1____________________
455 ____22__________________
456 ______11________________
457 _________1______________
458 __________3_____________
459 ___________44___________
460 _____________33_________
461 _______________2________
462 ________________1_______
463 _________________4______
464 ___________________2____
465 ____________________33__
466 ______________________4_
469 /* if there's only one memory region, don't bother */
475 /* bail out if we find any unreasonable addresses in bios map */
476 for (i = 0; i < old_nr; i++)
477 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
480 /* create pointers for initial change-point information (for sorting) */
481 for (i = 0; i < 2 * old_nr; i++)
482 change_point[i] = &change_point_list[i];
484 /* record all known change-points (starting and ending addresses),
485 omitting those that are for empty memory regions */
487 for (i = 0; i < old_nr; i++) {
488 if (biosmap[i].size != 0) {
489 change_point[chgidx]->addr = biosmap[i].addr;
490 change_point[chgidx++]->pbios = &biosmap[i];
491 change_point[chgidx]->addr = biosmap[i].addr +
493 change_point[chgidx++]->pbios = &biosmap[i];
498 /* sort change-point list by memory addresses (low -> high) */
500 while (still_changing) {
502 for (i = 1; i < chg_nr; i++) {
503 unsigned long long curaddr, lastaddr;
504 unsigned long long curpbaddr, lastpbaddr;
506 curaddr = change_point[i]->addr;
507 lastaddr = change_point[i - 1]->addr;
508 curpbaddr = change_point[i]->pbios->addr;
509 lastpbaddr = change_point[i - 1]->pbios->addr;
512 * swap entries, when:
514 * curaddr > lastaddr or
515 * curaddr == lastaddr and curaddr == curpbaddr and
516 * lastaddr != lastpbaddr
518 if (curaddr < lastaddr ||
519 (curaddr == lastaddr && curaddr == curpbaddr &&
520 lastaddr != lastpbaddr)) {
521 change_tmp = change_point[i];
522 change_point[i] = change_point[i-1];
523 change_point[i-1] = change_tmp;
529 /* create a new bios memory map, removing overlaps */
530 overlap_entries = 0; /* number of entries in the overlap table */
531 new_bios_entry = 0; /* index for creating new bios map entries */
532 last_type = 0; /* start with undefined memory type */
533 last_addr = 0; /* start with 0 as last starting address */
535 /* loop through change-points, determining affect on the new bios map */
536 for (chgidx = 0; chgidx < chg_nr; chgidx++) {
537 /* keep track of all overlapping bios entries */
538 if (change_point[chgidx]->addr ==
539 change_point[chgidx]->pbios->addr) {
541 * add map entry to overlap list (> 1 entry
542 * implies an overlap)
544 overlap_list[overlap_entries++] =
545 change_point[chgidx]->pbios;
548 * remove entry from list (order independent,
551 for (i = 0; i < overlap_entries; i++) {
552 if (overlap_list[i] ==
553 change_point[chgidx]->pbios)
555 overlap_list[overlap_entries-1];
560 * if there are overlapping entries, decide which
561 * "type" to use (larger value takes precedence --
562 * 1=usable, 2,3,4,4+=unusable)
565 for (i = 0; i < overlap_entries; i++)
566 if (overlap_list[i]->type > current_type)
567 current_type = overlap_list[i]->type;
569 * continue building up new bios map based on this
572 if (current_type != last_type) {
573 if (last_type != 0) {
574 new_bios[new_bios_entry].size =
575 change_point[chgidx]->addr - last_addr;
577 * move forward only if the new size
580 if (new_bios[new_bios_entry].size != 0)
582 * no more space left for new
585 if (++new_bios_entry >= E820MAX)
588 if (current_type != 0) {
589 new_bios[new_bios_entry].addr =
590 change_point[chgidx]->addr;
591 new_bios[new_bios_entry].type = current_type;
592 last_addr = change_point[chgidx]->addr;
594 last_type = current_type;
597 /* retain count for new bios entries */
598 new_nr = new_bios_entry;
600 /* copy new bios mapping into original location */
601 memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
608 * Copy the BIOS e820 map into a safe place.
610 * Sanity-check it while we're at it..
612 * If we're lucky and live on a modern system, the setup code
613 * will have given us a memory map that we can use to properly
614 * set up memory. If we aren't, we'll fake a memory map.
616 static int __init copy_e820_map(struct e820entry *biosmap, int nr_map)
618 /* Only one memory region (or negative)? Ignore it */
623 unsigned long start = biosmap->addr;
624 unsigned long size = biosmap->size;
625 unsigned long end = start + size;
626 unsigned long type = biosmap->type;
628 /* Overflow in 64 bits? Ignore the memory map. */
632 add_memory_region(start, size, type);
633 } while (biosmap++, --nr_map);
637 void early_panic(char *msg)
643 void __init setup_memory_region(void)
646 * Try to copy the BIOS-supplied E820-map.
648 * Otherwise fake a memory map; one section from 0k->640k,
649 * the next section from 1mb->appropriate_mem_k
651 sanitize_e820_map(boot_params.e820_map, &boot_params.e820_entries);
652 if (copy_e820_map(boot_params.e820_map, boot_params.e820_entries) < 0)
653 early_panic("Cannot find a valid memory map");
654 printk(KERN_INFO "BIOS-provided physical RAM map:\n");
655 e820_print_map("BIOS-e820");
658 static int __init parse_memopt(char *p)
662 end_user_pfn = memparse(p, &p);
663 end_user_pfn >>= PAGE_SHIFT;
666 early_param("mem", parse_memopt);
668 static int userdef __initdata;
670 static int __init parse_memmap_opt(char *p)
673 unsigned long long start_at, mem_size;
675 if (!strcmp(p, "exactmap")) {
676 #ifdef CONFIG_CRASH_DUMP
678 * If we are doing a crash dump, we still need to know
679 * the real mem size before original memory map is
682 e820_register_active_regions(0, 0, -1UL);
683 saved_max_pfn = e820_end_of_ram();
684 remove_all_active_ranges();
693 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 printk(KERN_INFO "user-defined physical RAM map:\n");
716 e820_print_map("user");
720 unsigned long pci_mem_start = 0xaeedbabe;
721 EXPORT_SYMBOL(pci_mem_start);
724 * Search for the biggest gap in the low 32 bits of the e820
725 * memory space. We pass this space to PCI to assign MMIO resources
726 * for hotplug or unconfigured devices in.
727 * Hopefully the BIOS let enough space left.
729 __init void e820_setup_gap(void)
731 unsigned long gapstart, gapsize, round;
736 last = 0x100000000ull;
737 gapstart = 0x10000000;
741 unsigned long long start = e820.map[i].addr;
742 unsigned long long end = start + e820.map[i].size;
745 * Since "last" is at most 4GB, we know we'll
746 * fit in 32 bits if this condition is true
749 unsigned long gap = last - end;
762 gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
763 printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit "
765 KERN_ERR "PCI: Unassigned devices with 32bit resource "
766 "registers may break!\n");
770 * See how much we want to round up: start off with
771 * rounding to the next 1MB area.
774 while ((gapsize >> 4) > round)
776 /* Fun with two's complement */
777 pci_mem_start = (gapstart + round) & -round;
780 "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
781 pci_mem_start, gapstart, gapsize);
784 int __init arch_get_ram_range(int slot, u64 *addr, u64 *size)
788 if (slot < 0 || slot >= e820.nr_map)
790 for (i = slot; i < e820.nr_map; i++) {
791 if (e820.map[i].type != E820_RAM)
795 if (i == e820.nr_map || e820.map[i].addr > (max_pfn << PAGE_SHIFT))
797 *addr = e820.map[i].addr;
798 *size = min_t(u64, e820.map[i].size + e820.map[i].addr,
799 max_pfn << PAGE_SHIFT) - *addr;