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 extern struct resource code_resource, data_resource, bss_resource;
52 /* Check for some hardcoded bad areas that early boot is not allowed to touch */
53 static inline int bad_addr(unsigned long *addrp, unsigned long size)
55 unsigned long addr = *addrp, last = addr + size;
57 /* various gunk below that needed for SMP startup */
59 *addrp = PAGE_ALIGN(0x8000);
63 /* direct mapping tables of the kernel */
64 if (last >= table_start<<PAGE_SHIFT && addr < table_end<<PAGE_SHIFT) {
65 *addrp = PAGE_ALIGN(table_end << PAGE_SHIFT);
70 #ifdef CONFIG_BLK_DEV_INITRD
71 if (boot_params.hdr.type_of_loader && boot_params.hdr.ramdisk_image) {
72 unsigned long ramdisk_image = boot_params.hdr.ramdisk_image;
73 unsigned long ramdisk_size = boot_params.hdr.ramdisk_size;
74 unsigned long ramdisk_end = ramdisk_image+ramdisk_size;
76 if (last >= ramdisk_image && addr < ramdisk_end) {
77 *addrp = PAGE_ALIGN(ramdisk_end);
83 if (last >= __pa_symbol(&_text) && addr < __pa_symbol(&_end)) {
84 *addrp = PAGE_ALIGN(__pa_symbol(&_end));
88 if (last >= ebda_addr && addr < ebda_addr + ebda_size) {
89 *addrp = PAGE_ALIGN(ebda_addr + ebda_size);
94 /* NUMA memory to node map */
95 if (last >= nodemap_addr && addr < nodemap_addr + nodemap_size) {
96 *addrp = nodemap_addr + nodemap_size;
100 /* XXX ramdisk image here? */
105 * This function checks if any part of the range <start,end> is mapped
109 e820_any_mapped(unsigned long start, unsigned long end, unsigned type)
113 for (i = 0; i < e820.nr_map; i++) {
114 struct e820entry *ei = &e820.map[i];
116 if (type && ei->type != type)
118 if (ei->addr >= end || ei->addr + ei->size <= start)
124 EXPORT_SYMBOL_GPL(e820_any_mapped);
127 * This function checks if the entire range <start,end> is mapped with type.
129 * Note: this function only works correct if the e820 table is sorted and
130 * not-overlapping, which is the case
132 int __init e820_all_mapped(unsigned long start, unsigned long end,
137 for (i = 0; i < e820.nr_map; i++) {
138 struct e820entry *ei = &e820.map[i];
140 if (type && ei->type != type)
142 /* is the region (part) in overlap with the current region ?*/
143 if (ei->addr >= end || ei->addr + ei->size <= start)
146 /* if the region is at the beginning of <start,end> we move
147 * start to the end of the region since it's ok until there
149 if (ei->addr <= start)
150 start = ei->addr + ei->size;
152 * if start is now at or beyond end, we're done, full
162 * Find a free area in a specific range.
164 unsigned long __init find_e820_area(unsigned long start, unsigned long end,
169 for (i = 0; i < e820.nr_map; i++) {
170 struct e820entry *ei = &e820.map[i];
171 unsigned long addr = ei->addr, last;
173 if (ei->type != E820_RAM)
177 if (addr > ei->addr + ei->size)
179 while (bad_addr(&addr, size) && addr+size <= ei->addr+ei->size)
181 last = PAGE_ALIGN(addr) + size;
182 if (last > ei->addr + ei->size)
192 * Find the highest page frame number we have available
194 unsigned long __init e820_end_of_ram(void)
196 unsigned long end_pfn;
198 end_pfn = find_max_pfn_with_active_regions();
200 if (end_pfn > end_pfn_map)
201 end_pfn_map = end_pfn;
202 if (end_pfn_map > MAXMEM>>PAGE_SHIFT)
203 end_pfn_map = MAXMEM>>PAGE_SHIFT;
204 if (end_pfn > end_user_pfn)
205 end_pfn = end_user_pfn;
206 if (end_pfn > end_pfn_map)
207 end_pfn = end_pfn_map;
209 printk(KERN_INFO "end_pfn_map = %lu\n", end_pfn_map);
214 * Mark e820 reserved areas as busy for the resource manager.
216 void __init e820_reserve_resources(void)
219 for (i = 0; i < e820.nr_map; i++) {
220 struct resource *res;
221 res = alloc_bootmem_low(sizeof(struct resource));
222 switch (e820.map[i].type) {
223 case E820_RAM: res->name = "System RAM"; break;
224 case E820_ACPI: res->name = "ACPI Tables"; break;
225 case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
226 default: res->name = "reserved";
228 res->start = e820.map[i].addr;
229 res->end = res->start + e820.map[i].size - 1;
230 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
231 request_resource(&iomem_resource, res);
232 if (e820.map[i].type == E820_RAM) {
234 * We don't know which RAM region contains kernel data,
235 * so we try it repeatedly and let the resource manager
238 request_resource(res, &code_resource);
239 request_resource(res, &data_resource);
240 request_resource(res, &bss_resource);
242 if (crashk_res.start != crashk_res.end)
243 request_resource(res, &crashk_res);
250 * Find the ranges of physical addresses that do not correspond to
251 * e820 RAM areas and mark the corresponding pages as nosave for software
252 * suspend and suspend to RAM.
254 * This function requires the e820 map to be sorted and without any
255 * overlapping entries and assumes the first e820 area to be RAM.
257 void __init e820_mark_nosave_regions(void)
262 paddr = round_down(e820.map[0].addr + e820.map[0].size, PAGE_SIZE);
263 for (i = 1; i < e820.nr_map; i++) {
264 struct e820entry *ei = &e820.map[i];
266 if (paddr < ei->addr)
267 register_nosave_region(PFN_DOWN(paddr),
270 paddr = round_down(ei->addr + ei->size, PAGE_SIZE);
271 if (ei->type != E820_RAM)
272 register_nosave_region(PFN_UP(ei->addr),
275 if (paddr >= (end_pfn << PAGE_SHIFT))
281 * Finds an active region in the address range from start_pfn to end_pfn and
282 * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
284 static int __init e820_find_active_region(const struct e820entry *ei,
285 unsigned long start_pfn,
286 unsigned long end_pfn,
287 unsigned long *ei_startpfn,
288 unsigned long *ei_endpfn)
290 *ei_startpfn = round_up(ei->addr, PAGE_SIZE) >> PAGE_SHIFT;
291 *ei_endpfn = round_down(ei->addr + ei->size, PAGE_SIZE) >> PAGE_SHIFT;
293 /* Skip map entries smaller than a page */
294 if (*ei_startpfn >= *ei_endpfn)
297 /* Check if end_pfn_map should be updated */
298 if (ei->type != E820_RAM && *ei_endpfn > end_pfn_map)
299 end_pfn_map = *ei_endpfn;
301 /* Skip if map is outside the node */
302 if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
303 *ei_startpfn >= end_pfn)
306 /* Check for overlaps */
307 if (*ei_startpfn < start_pfn)
308 *ei_startpfn = start_pfn;
309 if (*ei_endpfn > end_pfn)
310 *ei_endpfn = end_pfn;
312 /* Obey end_user_pfn to save on memmap */
313 if (*ei_startpfn >= end_user_pfn)
315 if (*ei_endpfn > end_user_pfn)
316 *ei_endpfn = end_user_pfn;
321 /* Walk the e820 map and register active regions within a node */
323 e820_register_active_regions(int nid, unsigned long start_pfn,
324 unsigned long end_pfn)
326 unsigned long ei_startpfn;
327 unsigned long ei_endpfn;
330 for (i = 0; i < e820.nr_map; i++)
331 if (e820_find_active_region(&e820.map[i],
333 &ei_startpfn, &ei_endpfn))
334 add_active_range(nid, ei_startpfn, ei_endpfn);
338 * Add a memory region to the kernel e820 map.
340 void __init add_memory_region(unsigned long start, unsigned long size, int type)
345 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
349 e820.map[x].addr = start;
350 e820.map[x].size = size;
351 e820.map[x].type = type;
356 * Find the hole size (in bytes) in the memory range.
357 * @start: starting address of the memory range to scan
358 * @end: ending address of the memory range to scan
360 unsigned long __init e820_hole_size(unsigned long start, unsigned long end)
362 unsigned long start_pfn = start >> PAGE_SHIFT;
363 unsigned long end_pfn = end >> PAGE_SHIFT;
364 unsigned long ei_startpfn, ei_endpfn, ram = 0;
367 for (i = 0; i < e820.nr_map; i++) {
368 if (e820_find_active_region(&e820.map[i],
370 &ei_startpfn, &ei_endpfn))
371 ram += ei_endpfn - ei_startpfn;
373 return end - start - (ram << PAGE_SHIFT);
376 void __init e820_print_map(char *who)
380 for (i = 0; i < e820.nr_map; i++) {
381 printk(KERN_INFO " %s: %016Lx - %016Lx ", who,
382 (unsigned long long) e820.map[i].addr,
384 (e820.map[i].addr + e820.map[i].size));
385 switch (e820.map[i].type) {
387 printk(KERN_CONT "(usable)\n");
390 printk(KERN_CONT "(reserved)\n");
393 printk(KERN_CONT "(ACPI data)\n");
396 printk(KERN_CONT "(ACPI NVS)\n");
399 printk(KERN_CONT "type %u\n", e820.map[i].type);
406 * Sanitize the BIOS e820 map.
408 * Some e820 responses include overlapping entries. The following
409 * replaces the original e820 map with a new one, removing overlaps.
412 static int __init sanitize_e820_map(struct e820entry *biosmap, char *pnr_map)
414 struct change_member {
415 struct e820entry *pbios; /* pointer to original bios entry */
416 unsigned long long addr; /* address for this change point */
418 static struct change_member change_point_list[2*E820MAX] __initdata;
419 static struct change_member *change_point[2*E820MAX] __initdata;
420 static struct e820entry *overlap_list[E820MAX] __initdata;
421 static struct e820entry new_bios[E820MAX] __initdata;
422 struct change_member *change_tmp;
423 unsigned long current_type, last_type;
424 unsigned long long last_addr;
425 int chgidx, still_changing;
428 int old_nr, new_nr, chg_nr;
432 Visually we're performing the following
433 (1,2,3,4 = memory types)...
435 Sample memory map (w/overlaps):
436 ____22__________________
437 ______________________4_
438 ____1111________________
439 _44_____________________
440 11111111________________
441 ____________________33__
442 ___________44___________
443 __________33333_________
444 ______________22________
445 ___________________2222_
446 _________111111111______
447 _____________________11_
448 _________________4______
450 Sanitized equivalent (no overlap):
451 1_______________________
452 _44_____________________
453 ___1____________________
454 ____22__________________
455 ______11________________
456 _________1______________
457 __________3_____________
458 ___________44___________
459 _____________33_________
460 _______________2________
461 ________________1_______
462 _________________4______
463 ___________________2____
464 ____________________33__
465 ______________________4_
468 /* if there's only one memory region, don't bother */
474 /* bail out if we find any unreasonable addresses in bios map */
475 for (i = 0; i < old_nr; i++)
476 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
479 /* create pointers for initial change-point information (for sorting) */
480 for (i = 0; i < 2 * old_nr; i++)
481 change_point[i] = &change_point_list[i];
483 /* record all known change-points (starting and ending addresses),
484 omitting those that are for empty memory regions */
486 for (i = 0; i < old_nr; i++) {
487 if (biosmap[i].size != 0) {
488 change_point[chgidx]->addr = biosmap[i].addr;
489 change_point[chgidx++]->pbios = &biosmap[i];
490 change_point[chgidx]->addr = biosmap[i].addr +
492 change_point[chgidx++]->pbios = &biosmap[i];
497 /* sort change-point list by memory addresses (low -> high) */
499 while (still_changing) {
501 for (i = 1; i < chg_nr; i++) {
502 unsigned long long curaddr, lastaddr;
503 unsigned long long curpbaddr, lastpbaddr;
505 curaddr = change_point[i]->addr;
506 lastaddr = change_point[i - 1]->addr;
507 curpbaddr = change_point[i]->pbios->addr;
508 lastpbaddr = change_point[i - 1]->pbios->addr;
511 * swap entries, when:
513 * curaddr > lastaddr or
514 * curaddr == lastaddr and curaddr == curpbaddr and
515 * lastaddr != lastpbaddr
517 if (curaddr < lastaddr ||
518 (curaddr == lastaddr && curaddr == curpbaddr &&
519 lastaddr != lastpbaddr)) {
520 change_tmp = change_point[i];
521 change_point[i] = change_point[i-1];
522 change_point[i-1] = change_tmp;
528 /* create a new bios memory map, removing overlaps */
529 overlap_entries = 0; /* number of entries in the overlap table */
530 new_bios_entry = 0; /* index for creating new bios map entries */
531 last_type = 0; /* start with undefined memory type */
532 last_addr = 0; /* start with 0 as last starting address */
534 /* loop through change-points, determining affect on the new bios map */
535 for (chgidx = 0; chgidx < chg_nr; chgidx++) {
536 /* keep track of all overlapping bios entries */
537 if (change_point[chgidx]->addr ==
538 change_point[chgidx]->pbios->addr) {
540 * add map entry to overlap list (> 1 entry
541 * implies an overlap)
543 overlap_list[overlap_entries++] =
544 change_point[chgidx]->pbios;
547 * remove entry from list (order independent,
550 for (i = 0; i < overlap_entries; i++) {
551 if (overlap_list[i] ==
552 change_point[chgidx]->pbios)
554 overlap_list[overlap_entries-1];
559 * if there are overlapping entries, decide which
560 * "type" to use (larger value takes precedence --
561 * 1=usable, 2,3,4,4+=unusable)
564 for (i = 0; i < overlap_entries; i++)
565 if (overlap_list[i]->type > current_type)
566 current_type = overlap_list[i]->type;
568 * continue building up new bios map based on this
571 if (current_type != last_type) {
572 if (last_type != 0) {
573 new_bios[new_bios_entry].size =
574 change_point[chgidx]->addr - last_addr;
576 * move forward only if the new size
579 if (new_bios[new_bios_entry].size != 0)
581 * no more space left for new
584 if (++new_bios_entry >= E820MAX)
587 if (current_type != 0) {
588 new_bios[new_bios_entry].addr =
589 change_point[chgidx]->addr;
590 new_bios[new_bios_entry].type = current_type;
591 last_addr = change_point[chgidx]->addr;
593 last_type = current_type;
596 /* retain count for new bios entries */
597 new_nr = new_bios_entry;
599 /* copy new bios mapping into original location */
600 memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
607 * Copy the BIOS e820 map into a safe place.
609 * Sanity-check it while we're at it..
611 * If we're lucky and live on a modern system, the setup code
612 * will have given us a memory map that we can use to properly
613 * set up memory. If we aren't, we'll fake a memory map.
615 static int __init copy_e820_map(struct e820entry *biosmap, int nr_map)
617 /* Only one memory region (or negative)? Ignore it */
622 unsigned long start = biosmap->addr;
623 unsigned long size = biosmap->size;
624 unsigned long end = start + size;
625 unsigned long type = biosmap->type;
627 /* Overflow in 64 bits? Ignore the memory map. */
631 add_memory_region(start, size, type);
632 } while (biosmap++, --nr_map);
636 void early_panic(char *msg)
642 void __init setup_memory_region(void)
645 * Try to copy the BIOS-supplied E820-map.
647 * Otherwise fake a memory map; one section from 0k->640k,
648 * the next section from 1mb->appropriate_mem_k
650 sanitize_e820_map(boot_params.e820_map, &boot_params.e820_entries);
651 if (copy_e820_map(boot_params.e820_map, boot_params.e820_entries) < 0)
652 early_panic("Cannot find a valid memory map");
653 printk(KERN_INFO "BIOS-provided physical RAM map:\n");
654 e820_print_map("BIOS-e820");
657 static int __init parse_memopt(char *p)
661 end_user_pfn = memparse(p, &p);
662 end_user_pfn >>= PAGE_SHIFT;
665 early_param("mem", parse_memopt);
667 static int userdef __initdata;
669 static int __init parse_memmap_opt(char *p)
672 unsigned long long start_at, mem_size;
674 if (!strcmp(p, "exactmap")) {
675 #ifdef CONFIG_CRASH_DUMP
677 * If we are doing a crash dump, we still need to know
678 * the real mem size before original memory map is
681 e820_register_active_regions(0, 0, -1UL);
682 saved_max_pfn = e820_end_of_ram();
683 remove_all_active_ranges();
692 mem_size = memparse(p, &p);
696 start_at = memparse(p+1, &p);
697 add_memory_region(start_at, mem_size, E820_RAM);
698 } else if (*p == '#') {
699 start_at = memparse(p+1, &p);
700 add_memory_region(start_at, mem_size, E820_ACPI);
701 } else if (*p == '$') {
702 start_at = memparse(p+1, &p);
703 add_memory_region(start_at, mem_size, E820_RESERVED);
705 end_user_pfn = (mem_size >> PAGE_SHIFT);
707 return *p == '\0' ? 0 : -EINVAL;
709 early_param("memmap", parse_memmap_opt);
711 void __init finish_e820_parsing(void)
714 printk(KERN_INFO "user-defined physical RAM map:\n");
715 e820_print_map("user");
719 unsigned long pci_mem_start = 0xaeedbabe;
720 EXPORT_SYMBOL(pci_mem_start);
723 * Search for the biggest gap in the low 32 bits of the e820
724 * memory space. We pass this space to PCI to assign MMIO resources
725 * for hotplug or unconfigured devices in.
726 * Hopefully the BIOS let enough space left.
728 __init void e820_setup_gap(void)
730 unsigned long gapstart, gapsize, round;
735 last = 0x100000000ull;
736 gapstart = 0x10000000;
740 unsigned long long start = e820.map[i].addr;
741 unsigned long long end = start + e820.map[i].size;
744 * Since "last" is at most 4GB, we know we'll
745 * fit in 32 bits if this condition is true
748 unsigned long gap = last - end;
761 gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
762 printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit "
764 KERN_ERR "PCI: Unassigned devices with 32bit resource "
765 "registers may break!\n");
769 * See how much we want to round up: start off with
770 * rounding to the next 1MB area.
773 while ((gapsize >> 4) > round)
775 /* Fun with two's complement */
776 pci_mem_start = (gapstart + round) & -round;
779 "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
780 pci_mem_start, gapstart, gapsize);
783 int __init arch_get_ram_range(int slot, u64 *addr, u64 *size)
787 if (slot < 0 || slot >= e820.nr_map)
789 for (i = slot; i < e820.nr_map; i++) {
790 if (e820.map[i].type != E820_RAM)
794 if (i == e820.nr_map || e820.map[i].addr > (max_pfn << PAGE_SHIFT))
796 *addr = e820.map[i].addr;
797 *size = min_t(u64, e820.map[i].size + e820.map[i].addr,
798 max_pfn << PAGE_SHIFT) - *addr;