1 #include <linux/kernel.h>
2 #include <linux/types.h>
3 #include <linux/init.h>
4 #include <linux/bootmem.h>
5 #include <linux/ioport.h>
6 #include <linux/string.h>
7 #include <linux/kexec.h>
8 #include <linux/module.h>
10 #include <linux/pfn.h>
11 #include <linux/uaccess.h>
12 #include <linux/suspend.h>
14 #include <asm/pgtable.h>
17 #include <asm/setup.h>
20 struct change_member {
21 struct e820entry *pbios; /* pointer to original bios entry */
22 unsigned long long addr; /* address for this change point */
24 static struct change_member change_point_list[2*E820MAX] __initdata;
25 static struct change_member *change_point[2*E820MAX] __initdata;
26 static struct e820entry *overlap_list[E820MAX] __initdata;
27 static struct e820entry new_bios[E820MAX] __initdata;
28 /* For PCI or other memory-mapped resources */
29 unsigned long pci_mem_start = 0x10000000;
31 EXPORT_SYMBOL(pci_mem_start);
34 static struct resource system_rom_resource = {
38 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
41 static struct resource extension_rom_resource = {
42 .name = "Extension ROM",
45 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
48 static struct resource adapter_rom_resources[] = { {
49 .name = "Adapter ROM",
52 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
54 .name = "Adapter ROM",
57 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
59 .name = "Adapter ROM",
62 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
64 .name = "Adapter ROM",
67 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
69 .name = "Adapter ROM",
72 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
74 .name = "Adapter ROM",
77 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
80 static struct resource video_rom_resource = {
84 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
87 #define ROMSIGNATURE 0xaa55
89 static int __init romsignature(const unsigned char *rom)
91 const unsigned short * const ptr = (const unsigned short *)rom;
94 return probe_kernel_address(ptr, sig) == 0 && sig == ROMSIGNATURE;
97 static int __init romchecksum(const unsigned char *rom, unsigned long length)
101 for (sum = 0; length && probe_kernel_address(rom++, c) == 0; length--)
103 return !length && !sum;
106 static void __init probe_roms(void)
108 const unsigned char *rom;
109 unsigned long start, length, upper;
114 upper = adapter_rom_resources[0].start;
115 for (start = video_rom_resource.start; start < upper; start += 2048) {
116 rom = isa_bus_to_virt(start);
117 if (!romsignature(rom))
120 video_rom_resource.start = start;
122 if (probe_kernel_address(rom + 2, c) != 0)
125 /* 0 < length <= 0x7f * 512, historically */
128 /* if checksum okay, trust length byte */
129 if (length && romchecksum(rom, length))
130 video_rom_resource.end = start + length - 1;
132 request_resource(&iomem_resource, &video_rom_resource);
136 start = (video_rom_resource.end + 1 + 2047) & ~2047UL;
141 request_resource(&iomem_resource, &system_rom_resource);
142 upper = system_rom_resource.start;
144 /* check for extension rom (ignore length byte!) */
145 rom = isa_bus_to_virt(extension_rom_resource.start);
146 if (romsignature(rom)) {
147 length = extension_rom_resource.end - extension_rom_resource.start + 1;
148 if (romchecksum(rom, length)) {
149 request_resource(&iomem_resource, &extension_rom_resource);
150 upper = extension_rom_resource.start;
154 /* check for adapter roms on 2k boundaries */
155 for (i = 0; i < ARRAY_SIZE(adapter_rom_resources) && start < upper; start += 2048) {
156 rom = isa_bus_to_virt(start);
157 if (!romsignature(rom))
160 if (probe_kernel_address(rom + 2, c) != 0)
163 /* 0 < length <= 0x7f * 512, historically */
166 /* but accept any length that fits if checksum okay */
167 if (!length || start + length > upper || !romchecksum(rom, length))
170 adapter_rom_resources[i].start = start;
171 adapter_rom_resources[i].end = start + length - 1;
172 request_resource(&iomem_resource, &adapter_rom_resources[i]);
174 start = adapter_rom_resources[i++].end & ~2047UL;
179 * Request address space for all standard RAM and ROM resources
180 * and also for regions reported as reserved by the e820.
182 void __init init_iomem_resources(struct resource *code_resource,
183 struct resource *data_resource,
184 struct resource *bss_resource)
189 for (i = 0; i < e820.nr_map; i++) {
190 struct resource *res;
191 #ifndef CONFIG_RESOURCES_64BIT
192 if (e820.map[i].addr + e820.map[i].size > 0x100000000ULL)
195 res = kzalloc(sizeof(struct resource), GFP_ATOMIC);
196 switch (e820.map[i].type) {
197 case E820_RAM: res->name = "System RAM"; break;
198 case E820_ACPI: res->name = "ACPI Tables"; break;
199 case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
200 default: res->name = "reserved";
202 res->start = e820.map[i].addr;
203 res->end = res->start + e820.map[i].size - 1;
204 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
205 if (request_resource(&iomem_resource, res)) {
209 if (e820.map[i].type == E820_RAM) {
211 * We don't know which RAM region contains kernel data,
212 * so we try it repeatedly and let the resource manager
215 request_resource(res, code_resource);
216 request_resource(res, data_resource);
217 request_resource(res, bss_resource);
219 if (crashk_res.start != crashk_res.end)
220 request_resource(res, &crashk_res);
226 #if defined(CONFIG_PM) && defined(CONFIG_HIBERNATION)
228 * e820_mark_nosave_regions - Find the ranges of physical addresses that do not
229 * correspond to e820 RAM areas and mark the corresponding pages as nosave for
232 * This function requires the e820 map to be sorted and without any
233 * overlapping entries and assumes the first e820 area to be RAM.
235 void __init e820_mark_nosave_regions(void)
240 pfn = PFN_DOWN(e820.map[0].addr + e820.map[0].size);
241 for (i = 1; i < e820.nr_map; i++) {
242 struct e820entry *ei = &e820.map[i];
244 if (pfn < PFN_UP(ei->addr))
245 register_nosave_region(pfn, PFN_UP(ei->addr));
247 pfn = PFN_DOWN(ei->addr + ei->size);
248 if (ei->type != E820_RAM)
249 register_nosave_region(PFN_UP(ei->addr), pfn);
251 if (pfn >= max_low_pfn)
257 void __init add_memory_region(unsigned long long start,
258 unsigned long long size, int type)
265 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
269 e820.map[x].addr = start;
270 e820.map[x].size = size;
271 e820.map[x].type = type;
273 } /* add_memory_region */
276 * Sanitize the BIOS e820 map.
278 * Some e820 responses include overlapping entries. The following
279 * replaces the original e820 map with a new one, removing overlaps.
282 int __init sanitize_e820_map(struct e820entry * biosmap, char * pnr_map)
284 struct change_member *change_tmp;
285 unsigned long current_type, last_type;
286 unsigned long long last_addr;
287 int chgidx, still_changing;
290 int old_nr, new_nr, chg_nr;
294 Visually we're performing the following (1,2,3,4 = memory types)...
296 Sample memory map (w/overlaps):
297 ____22__________________
298 ______________________4_
299 ____1111________________
300 _44_____________________
301 11111111________________
302 ____________________33__
303 ___________44___________
304 __________33333_________
305 ______________22________
306 ___________________2222_
307 _________111111111______
308 _____________________11_
309 _________________4______
311 Sanitized equivalent (no overlap):
312 1_______________________
313 _44_____________________
314 ___1____________________
315 ____22__________________
316 ______11________________
317 _________1______________
318 __________3_____________
319 ___________44___________
320 _____________33_________
321 _______________2________
322 ________________1_______
323 _________________4______
324 ___________________2____
325 ____________________33__
326 ______________________4_
328 /* if there's only one memory region, don't bother */
335 /* bail out if we find any unreasonable addresses in bios map */
336 for (i=0; i<old_nr; i++)
337 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr) {
341 /* create pointers for initial change-point information (for sorting) */
342 for (i=0; i < 2*old_nr; i++)
343 change_point[i] = &change_point_list[i];
345 /* record all known change-points (starting and ending addresses),
346 omitting those that are for empty memory regions */
348 for (i=0; i < old_nr; i++) {
349 if (biosmap[i].size != 0) {
350 change_point[chgidx]->addr = biosmap[i].addr;
351 change_point[chgidx++]->pbios = &biosmap[i];
352 change_point[chgidx]->addr = biosmap[i].addr + biosmap[i].size;
353 change_point[chgidx++]->pbios = &biosmap[i];
356 chg_nr = chgidx; /* true number of change-points */
358 /* sort change-point list by memory addresses (low -> high) */
360 while (still_changing) {
362 for (i=1; i < chg_nr; i++) {
363 /* if <current_addr> > <last_addr>, swap */
364 /* or, if current=<start_addr> & last=<end_addr>, swap */
365 if ((change_point[i]->addr < change_point[i-1]->addr) ||
366 ((change_point[i]->addr == change_point[i-1]->addr) &&
367 (change_point[i]->addr == change_point[i]->pbios->addr) &&
368 (change_point[i-1]->addr != change_point[i-1]->pbios->addr))
371 change_tmp = change_point[i];
372 change_point[i] = change_point[i-1];
373 change_point[i-1] = change_tmp;
379 /* create a new bios memory map, removing overlaps */
380 overlap_entries=0; /* number of entries in the overlap table */
381 new_bios_entry=0; /* index for creating new bios map entries */
382 last_type = 0; /* start with undefined memory type */
383 last_addr = 0; /* start with 0 as last starting address */
384 /* loop through change-points, determining affect on the new bios map */
385 for (chgidx=0; chgidx < chg_nr; chgidx++)
387 /* keep track of all overlapping bios entries */
388 if (change_point[chgidx]->addr == change_point[chgidx]->pbios->addr)
390 /* add map entry to overlap list (> 1 entry implies an overlap) */
391 overlap_list[overlap_entries++]=change_point[chgidx]->pbios;
395 /* remove entry from list (order independent, so swap with last) */
396 for (i=0; i<overlap_entries; i++)
398 if (overlap_list[i] == change_point[chgidx]->pbios)
399 overlap_list[i] = overlap_list[overlap_entries-1];
403 /* if there are overlapping entries, decide which "type" to use */
404 /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */
406 for (i=0; i<overlap_entries; i++)
407 if (overlap_list[i]->type > current_type)
408 current_type = overlap_list[i]->type;
409 /* continue building up new bios map based on this information */
410 if (current_type != last_type) {
411 if (last_type != 0) {
412 new_bios[new_bios_entry].size =
413 change_point[chgidx]->addr - last_addr;
414 /* move forward only if the new size was non-zero */
415 if (new_bios[new_bios_entry].size != 0)
416 if (++new_bios_entry >= E820MAX)
417 break; /* no more space left for new bios entries */
419 if (current_type != 0) {
420 new_bios[new_bios_entry].addr = change_point[chgidx]->addr;
421 new_bios[new_bios_entry].type = current_type;
422 last_addr=change_point[chgidx]->addr;
424 last_type = current_type;
427 new_nr = new_bios_entry; /* retain count for new bios entries */
429 /* copy new bios mapping into original location */
430 memcpy(biosmap, new_bios, new_nr*sizeof(struct e820entry));
437 * Copy the BIOS e820 map into a safe place.
439 * Sanity-check it while we're at it..
441 * If we're lucky and live on a modern system, the setup code
442 * will have given us a memory map that we can use to properly
443 * set up memory. If we aren't, we'll fake a memory map.
445 * We check to see that the memory map contains at least 2 elements
446 * before we'll use it, because the detection code in setup.S may
447 * not be perfect and most every PC known to man has two memory
448 * regions: one from 0 to 640k, and one from 1mb up. (The IBM
449 * thinkpad 560x, for example, does not cooperate with the memory
452 int __init copy_e820_map(struct e820entry *biosmap, int nr_map)
454 /* Only one memory region (or negative)? Ignore it */
459 u64 start = biosmap->addr;
460 u64 size = biosmap->size;
461 u64 end = start + size;
462 u32 type = biosmap->type;
464 /* Overflow in 64 bits? Ignore the memory map. */
468 add_memory_region(start, size, type);
469 } while (biosmap++, --nr_map);
475 * Find the highest page frame number we have available
477 void __init propagate_e820_map(void)
483 for (i = 0; i < e820.nr_map; i++) {
484 unsigned long start, end;
486 if (e820.map[i].type != E820_RAM)
488 start = PFN_UP(e820.map[i].addr);
489 end = PFN_DOWN(e820.map[i].addr + e820.map[i].size);
494 memory_present(0, start, end);
499 * Register fully available low RAM pages with the bootmem allocator.
501 void __init register_bootmem_low_pages(unsigned long max_low_pfn)
505 for (i = 0; i < e820.nr_map; i++) {
506 unsigned long curr_pfn, last_pfn, size;
508 * Reserve usable low memory
510 if (e820.map[i].type != E820_RAM)
513 * We are rounding up the start address of usable memory:
515 curr_pfn = PFN_UP(e820.map[i].addr);
516 if (curr_pfn >= max_low_pfn)
519 * ... and at the end of the usable range downwards:
521 last_pfn = PFN_DOWN(e820.map[i].addr + e820.map[i].size);
523 if (last_pfn > max_low_pfn)
524 last_pfn = max_low_pfn;
527 * .. finally, did all the rounding and playing
528 * around just make the area go away?
530 if (last_pfn <= curr_pfn)
533 size = last_pfn - curr_pfn;
534 free_bootmem(PFN_PHYS(curr_pfn), PFN_PHYS(size));
538 void __init e820_register_memory(void)
540 unsigned long gapstart, gapsize, round;
541 unsigned long long last;
545 * Search for the biggest gap in the low 32 bits of the e820
548 last = 0x100000000ull;
549 gapstart = 0x10000000;
553 unsigned long long start = e820.map[i].addr;
554 unsigned long long end = start + e820.map[i].size;
557 * Since "last" is at most 4GB, we know we'll
558 * fit in 32 bits if this condition is true
561 unsigned long gap = last - end;
573 * See how much we want to round up: start off with
574 * rounding to the next 1MB area.
577 while ((gapsize >> 4) > round)
579 /* Fun with two's complement */
580 pci_mem_start = (gapstart + round) & -round;
582 printk("Allocating PCI resources starting at %08lx (gap: %08lx:%08lx)\n",
583 pci_mem_start, gapstart, gapsize);
586 static void __init print_memory_map(char *who)
590 for (i = 0; i < e820.nr_map; i++) {
591 printk(" %s: %016Lx - %016Lx ", who,
593 e820.map[i].addr + e820.map[i].size);
594 switch (e820.map[i].type) {
595 case E820_RAM: printk("(usable)\n");
598 printk("(reserved)\n");
601 printk("(ACPI data)\n");
604 printk("(ACPI NVS)\n");
606 default: printk("type %u\n", e820.map[i].type);
612 void __init limit_regions(unsigned long long size)
614 unsigned long long current_addr;
617 print_memory_map("limit_regions start");
618 for (i = 0; i < e820.nr_map; i++) {
619 current_addr = e820.map[i].addr + e820.map[i].size;
620 if (current_addr < size)
623 if (e820.map[i].type != E820_RAM)
626 if (e820.map[i].addr >= size) {
628 * This region starts past the end of the
629 * requested size, skip it completely.
634 e820.map[i].size -= current_addr - size;
636 print_memory_map("limit_regions endfor");
639 print_memory_map("limit_regions endfunc");
643 * This function checks if any part of the range <start,end> is mapped
647 e820_any_mapped(u64 start, u64 end, unsigned type)
650 for (i = 0; i < e820.nr_map; i++) {
651 const struct e820entry *ei = &e820.map[i];
652 if (type && ei->type != type)
654 if (ei->addr >= end || ei->addr + ei->size <= start)
660 EXPORT_SYMBOL_GPL(e820_any_mapped);
663 * This function checks if the entire range <start,end> is mapped with type.
665 * Note: this function only works correct if the e820 table is sorted and
666 * not-overlapping, which is the case
669 e820_all_mapped(unsigned long s, unsigned long e, unsigned type)
674 for (i = 0; i < e820.nr_map; i++) {
675 struct e820entry *ei = &e820.map[i];
676 if (type && ei->type != type)
678 /* is the region (part) in overlap with the current region ?*/
679 if (ei->addr >= end || ei->addr + ei->size <= start)
681 /* if the region is at the beginning of <start,end> we move
682 * start to the end of the region since it's ok until there
684 if (ei->addr <= start)
685 start = ei->addr + ei->size;
686 /* if start is now at or beyond end, we're done, full
689 return 1; /* we're done */
694 /* Overridden in paravirt.c if CONFIG_PARAVIRT */
695 char * __init __attribute__((weak)) memory_setup(void)
697 return machine_specific_memory_setup();
700 void __init setup_memory_map(void)
702 printk(KERN_INFO "BIOS-provided physical RAM map:\n");
703 print_memory_map(memory_setup());
706 static int __initdata user_defined_memmap;
709 * "mem=nopentium" disables the 4MB page tables.
710 * "mem=XXX[kKmM]" defines a memory region from HIGH_MEM
711 * to <mem>, overriding the bios size.
712 * "memmap=XXX[KkmM]@XXX[KkmM]" defines a memory region from
713 * <start> to <start>+<mem>, overriding the bios size.
715 * HPA tells me bootloaders need to parse mem=, so no new
716 * option should be mem= [also see Documentation/i386/boot.txt]
718 static int __init parse_mem(char *arg)
723 if (strcmp(arg, "nopentium") == 0) {
724 setup_clear_cpu_cap(X86_FEATURE_PSE);
726 /* If the user specifies memory size, we
727 * limit the BIOS-provided memory map to
728 * that size. exactmap can be used to specify
729 * the exact map. mem=number can be used to
730 * trim the existing memory map.
732 unsigned long long mem_size;
734 mem_size = memparse(arg, &arg);
735 limit_regions(mem_size);
736 user_defined_memmap = 1;
740 early_param("mem", parse_mem);
742 static int __init parse_memmap(char *arg)
747 if (strcmp(arg, "exactmap") == 0) {
748 #ifdef CONFIG_CRASH_DUMP
749 /* If we are doing a crash dump, we
750 * still need to know the real mem
751 * size before original memory map is
754 propagate_e820_map();
755 saved_max_pfn = max_pfn;
758 user_defined_memmap = 1;
760 /* If the user specifies memory size, we
761 * limit the BIOS-provided memory map to
762 * that size. exactmap can be used to specify
763 * the exact map. mem=number can be used to
764 * trim the existing memory map.
766 unsigned long long start_at, mem_size;
768 mem_size = memparse(arg, &arg);
770 start_at = memparse(arg+1, &arg);
771 add_memory_region(start_at, mem_size, E820_RAM);
772 } else if (*arg == '#') {
773 start_at = memparse(arg+1, &arg);
774 add_memory_region(start_at, mem_size, E820_ACPI);
775 } else if (*arg == '$') {
776 start_at = memparse(arg+1, &arg);
777 add_memory_region(start_at, mem_size, E820_RESERVED);
779 limit_regions(mem_size);
780 user_defined_memmap = 1;
785 early_param("memmap", parse_memmap);
786 u64 __init update_memory_range(u64 start, u64 size, unsigned old_type,
790 u64 real_updated_size = 0;
792 BUG_ON(old_type == new_type);
794 for (i = 0; i < e820.nr_map; i++) {
795 struct e820entry *ei = &e820.map[i];
796 u64 final_start, final_end;
797 if (ei->type != old_type)
799 /* totally covered? */
800 if (ei->addr >= start && ei->size <= size) {
802 real_updated_size += ei->size;
805 /* partially covered */
806 final_start = max(start, ei->addr);
807 final_end = min(start + size, ei->addr + ei->size);
808 if (final_start >= final_end)
810 add_memory_region(final_start, final_end - final_start,
812 real_updated_size += final_end - final_start;
815 return real_updated_size;
818 void __init finish_e820_parsing(void)
820 if (user_defined_memmap) {
821 printk(KERN_INFO "user-defined physical RAM map:\n");
822 print_memory_map("user");
826 void __init update_e820(void)
830 nr_map = e820.nr_map;
831 if (sanitize_e820_map(e820.map, &nr_map))
833 e820.nr_map = nr_map;
834 printk(KERN_INFO "modified physical RAM map:\n");
835 print_memory_map("modified");