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/efi.h>
11 #include <linux/pfn.h>
12 #include <linux/uaccess.h>
14 #include <asm/pgtable.h>
19 #include <asm/hypervisor.h>
20 #include <xen/interface/physdev.h>
21 #include <xen/interface/memory.h>
22 #include <xen/features.h>
23 #include <xen/xencons.h>
28 EXPORT_SYMBOL(efi_enabled);
32 struct e820map machine_e820;
35 e820_setup_gap(struct e820entry *e820, int nr_map);
38 struct change_member {
39 struct e820entry *pbios; /* pointer to original bios entry */
40 unsigned long long addr; /* address for this change point */
42 static struct change_member change_point_list[2*E820MAX] __initdata;
43 static struct change_member *change_point[2*E820MAX] __initdata;
44 static struct e820entry *overlap_list[E820MAX] __initdata;
45 static struct e820entry new_bios[E820MAX] __initdata;
46 /* For PCI or other memory-mapped resources */
47 unsigned long pci_mem_start = 0x10000000;
49 EXPORT_SYMBOL(pci_mem_start);
51 extern int user_defined_memmap;
52 struct resource data_resource = {
53 .name = "Kernel data",
56 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
59 struct resource code_resource = {
60 .name = "Kernel code",
63 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
66 static struct resource system_rom_resource = {
70 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
73 static struct resource extension_rom_resource = {
74 .name = "Extension ROM",
77 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
80 static struct resource adapter_rom_resources[] = { {
81 .name = "Adapter ROM",
84 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
86 .name = "Adapter ROM",
89 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
91 .name = "Adapter ROM",
94 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
96 .name = "Adapter ROM",
99 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
101 .name = "Adapter ROM",
104 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
106 .name = "Adapter ROM",
109 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
112 static struct resource video_rom_resource = {
116 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
119 static struct resource video_ram_resource = {
120 .name = "Video RAM area",
123 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
126 static struct resource standard_io_resources[] = { {
130 .flags = IORESOURCE_BUSY | IORESOURCE_IO
135 .flags = IORESOURCE_BUSY | IORESOURCE_IO
140 .flags = IORESOURCE_BUSY | IORESOURCE_IO
145 .flags = IORESOURCE_BUSY | IORESOURCE_IO
150 .flags = IORESOURCE_BUSY | IORESOURCE_IO
152 .name = "dma page reg",
155 .flags = IORESOURCE_BUSY | IORESOURCE_IO
160 .flags = IORESOURCE_BUSY | IORESOURCE_IO
165 .flags = IORESOURCE_BUSY | IORESOURCE_IO
170 .flags = IORESOURCE_BUSY | IORESOURCE_IO
173 static int romsignature(const unsigned char *x)
177 if (probe_kernel_address((const unsigned short *)x, sig) == 0)
178 ret = (sig == 0xaa55);
182 static int __init romchecksum(unsigned char *rom, unsigned long length)
184 unsigned char *p, sum = 0;
186 for (p = rom; p < rom + length; p++)
191 static void __init probe_roms(void)
193 unsigned long start, length, upper;
198 /* Nothing to do if not running in dom0. */
199 if (!is_initial_xendomain())
204 upper = adapter_rom_resources[0].start;
205 for (start = video_rom_resource.start; start < upper; start += 2048) {
206 rom = isa_bus_to_virt(start);
207 if (!romsignature(rom))
210 video_rom_resource.start = start;
212 /* 0 < length <= 0x7f * 512, historically */
213 length = rom[2] * 512;
215 /* if checksum okay, trust length byte */
216 if (length && romchecksum(rom, length))
217 video_rom_resource.end = start + length - 1;
219 request_resource(&iomem_resource, &video_rom_resource);
223 start = (video_rom_resource.end + 1 + 2047) & ~2047UL;
228 request_resource(&iomem_resource, &system_rom_resource);
229 upper = system_rom_resource.start;
231 /* check for extension rom (ignore length byte!) */
232 rom = isa_bus_to_virt(extension_rom_resource.start);
233 if (romsignature(rom)) {
234 length = extension_rom_resource.end - extension_rom_resource.start + 1;
235 if (romchecksum(rom, length)) {
236 request_resource(&iomem_resource, &extension_rom_resource);
237 upper = extension_rom_resource.start;
241 /* check for adapter roms on 2k boundaries */
242 for (i = 0; i < ARRAY_SIZE(adapter_rom_resources) && start < upper; start += 2048) {
243 rom = isa_bus_to_virt(start);
244 if (!romsignature(rom))
247 /* 0 < length <= 0x7f * 512, historically */
248 length = rom[2] * 512;
250 /* but accept any length that fits if checksum okay */
251 if (!length || start + length > upper || !romchecksum(rom, length))
254 adapter_rom_resources[i].start = start;
255 adapter_rom_resources[i].end = start + length - 1;
256 request_resource(&iomem_resource, &adapter_rom_resources[i]);
258 start = adapter_rom_resources[i++].end & ~2047UL;
263 * Request address space for all standard RAM and ROM resources
264 * and also for regions reported as reserved by the e820.
267 legacy_init_iomem_resources(struct resource *code_resource, struct resource *data_resource)
270 struct e820entry *map = e820.map;
271 int nr_map = e820.nr_map;
272 #ifdef CONFIG_XEN_PRIVILEGED_GUEST
273 struct xen_memory_map memmap;
275 map = machine_e820.map;
276 memmap.nr_entries = E820MAX;
278 set_xen_guest_handle(memmap.buffer, map);
280 if(HYPERVISOR_memory_op(XENMEM_machine_memory_map, &memmap))
282 machine_e820.nr_map = memmap.nr_entries;
283 nr_map = memmap.nr_entries;
284 e820_setup_gap(map, memmap.nr_entries);
288 for (i = 0; i < nr_map; i++) {
289 struct resource *res;
290 #ifndef CONFIG_RESOURCES_64BIT
291 if (map[i].addr + map[i].size > 0x100000000ULL)
294 res = kzalloc(sizeof(struct resource), GFP_ATOMIC);
295 switch (map[i].type) {
296 case E820_RAM: res->name = "System RAM"; break;
297 case E820_ACPI: res->name = "ACPI Tables"; break;
298 case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
299 default: res->name = "reserved";
301 res->start = map[i].addr;
302 res->end = res->start + map[i].size - 1;
303 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
304 if (request_resource(&iomem_resource, res)) {
308 if (map[i].type == E820_RAM) {
310 * We don't know which RAM region contains kernel data,
311 * so we try it repeatedly and let the resource manager
315 request_resource(res, code_resource);
316 request_resource(res, data_resource);
319 request_resource(res, &crashk_res);
326 * Request address space for all standard resources
328 * This is called just before pcibios_init(), which is also a
329 * subsys_initcall, but is linked in later (in arch/i386/pci/common.c).
331 static int __init request_standard_resources(void)
335 printk("Setting up standard PCI resources\n");
337 /* Nothing to do if not running in dom0. */
338 if (!is_initial_xendomain())
342 efi_initialize_iomem_resources(&code_resource, &data_resource);
344 legacy_init_iomem_resources(&code_resource, &data_resource);
346 /* EFI systems may still have VGA */
347 request_resource(&iomem_resource, &video_ram_resource);
349 /* request I/O space for devices used on all i[345]86 PCs */
350 for (i = 0; i < ARRAY_SIZE(standard_io_resources); i++)
351 request_resource(&ioport_resource, &standard_io_resources[i]);
355 subsys_initcall(request_standard_resources);
357 void __init add_memory_region(unsigned long long start,
358 unsigned long long size, int type)
366 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
370 e820.map[x].addr = start;
371 e820.map[x].size = size;
372 e820.map[x].type = type;
375 } /* add_memory_region */
378 * Sanitize the BIOS e820 map.
380 * Some e820 responses include overlapping entries. The following
381 * replaces the original e820 map with a new one, removing overlaps.
384 int __init sanitize_e820_map(struct e820entry * biosmap, char * pnr_map)
386 struct change_member *change_tmp;
387 unsigned long current_type, last_type;
388 unsigned long long last_addr;
389 int chgidx, still_changing;
392 int old_nr, new_nr, chg_nr;
396 Visually we're performing the following (1,2,3,4 = memory types)...
398 Sample memory map (w/overlaps):
399 ____22__________________
400 ______________________4_
401 ____1111________________
402 _44_____________________
403 11111111________________
404 ____________________33__
405 ___________44___________
406 __________33333_________
407 ______________22________
408 ___________________2222_
409 _________111111111______
410 _____________________11_
411 _________________4______
413 Sanitized equivalent (no overlap):
414 1_______________________
415 _44_____________________
416 ___1____________________
417 ____22__________________
418 ______11________________
419 _________1______________
420 __________3_____________
421 ___________44___________
422 _____________33_________
423 _______________2________
424 ________________1_______
425 _________________4______
426 ___________________2____
427 ____________________33__
428 ______________________4_
430 printk("sanitize start\n");
431 /* if there's only one memory region, don't bother */
433 printk("sanitize bail 0\n");
439 /* bail out if we find any unreasonable addresses in bios map */
440 for (i=0; i<old_nr; i++)
441 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr) {
442 printk("sanitize bail 1\n");
446 /* create pointers for initial change-point information (for sorting) */
447 for (i=0; i < 2*old_nr; i++)
448 change_point[i] = &change_point_list[i];
450 /* record all known change-points (starting and ending addresses),
451 omitting those that are for empty memory regions */
453 for (i=0; i < old_nr; i++) {
454 if (biosmap[i].size != 0) {
455 change_point[chgidx]->addr = biosmap[i].addr;
456 change_point[chgidx++]->pbios = &biosmap[i];
457 change_point[chgidx]->addr = biosmap[i].addr + biosmap[i].size;
458 change_point[chgidx++]->pbios = &biosmap[i];
461 chg_nr = chgidx; /* true number of change-points */
463 /* sort change-point list by memory addresses (low -> high) */
465 while (still_changing) {
467 for (i=1; i < chg_nr; i++) {
468 /* if <current_addr> > <last_addr>, swap */
469 /* or, if current=<start_addr> & last=<end_addr>, swap */
470 if ((change_point[i]->addr < change_point[i-1]->addr) ||
471 ((change_point[i]->addr == change_point[i-1]->addr) &&
472 (change_point[i]->addr == change_point[i]->pbios->addr) &&
473 (change_point[i-1]->addr != change_point[i-1]->pbios->addr))
476 change_tmp = change_point[i];
477 change_point[i] = change_point[i-1];
478 change_point[i-1] = change_tmp;
484 /* create a new bios memory map, removing overlaps */
485 overlap_entries=0; /* number of entries in the overlap table */
486 new_bios_entry=0; /* index for creating new bios map entries */
487 last_type = 0; /* start with undefined memory type */
488 last_addr = 0; /* start with 0 as last starting address */
489 /* loop through change-points, determining affect on the new bios map */
490 for (chgidx=0; chgidx < chg_nr; chgidx++)
492 /* keep track of all overlapping bios entries */
493 if (change_point[chgidx]->addr == change_point[chgidx]->pbios->addr)
495 /* add map entry to overlap list (> 1 entry implies an overlap) */
496 overlap_list[overlap_entries++]=change_point[chgidx]->pbios;
500 /* remove entry from list (order independent, so swap with last) */
501 for (i=0; i<overlap_entries; i++)
503 if (overlap_list[i] == change_point[chgidx]->pbios)
504 overlap_list[i] = overlap_list[overlap_entries-1];
508 /* if there are overlapping entries, decide which "type" to use */
509 /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */
511 for (i=0; i<overlap_entries; i++)
512 if (overlap_list[i]->type > current_type)
513 current_type = overlap_list[i]->type;
514 /* continue building up new bios map based on this information */
515 if (current_type != last_type) {
516 if (last_type != 0) {
517 new_bios[new_bios_entry].size =
518 change_point[chgidx]->addr - last_addr;
519 /* move forward only if the new size was non-zero */
520 if (new_bios[new_bios_entry].size != 0)
521 if (++new_bios_entry >= E820MAX)
522 break; /* no more space left for new bios entries */
524 if (current_type != 0) {
525 new_bios[new_bios_entry].addr = change_point[chgidx]->addr;
526 new_bios[new_bios_entry].type = current_type;
527 last_addr=change_point[chgidx]->addr;
529 last_type = current_type;
532 new_nr = new_bios_entry; /* retain count for new bios entries */
534 /* copy new bios mapping into original location */
535 memcpy(biosmap, new_bios, new_nr*sizeof(struct e820entry));
538 printk("sanitize end\n");
543 * Copy the BIOS e820 map into a safe place.
545 * Sanity-check it while we're at it..
547 * If we're lucky and live on a modern system, the setup code
548 * will have given us a memory map that we can use to properly
549 * set up memory. If we aren't, we'll fake a memory map.
551 * We check to see that the memory map contains at least 2 elements
552 * before we'll use it, because the detection code in setup.S may
553 * not be perfect and most every PC known to man has two memory
554 * regions: one from 0 to 640k, and one from 1mb up. (The IBM
555 * thinkpad 560x, for example, does not cooperate with the memory
558 int __init copy_e820_map(struct e820entry * biosmap, int nr_map)
561 /* Only one memory region (or negative)? Ignore it */
568 unsigned long long start = biosmap->addr;
569 unsigned long long size = biosmap->size;
570 unsigned long long end = start + size;
571 unsigned long type = biosmap->type;
572 printk("copy_e820_map() start: %016Lx size: %016Lx end: %016Lx type: %ld\n", start, size, end, type);
574 /* Overflow in 64 bits? Ignore the memory map. */
580 * Some BIOSes claim RAM in the 640k - 1M region.
581 * Not right. Fix it up.
583 if (type == E820_RAM) {
584 printk("copy_e820_map() type is E820_RAM\n");
585 if (start < 0x100000ULL && end > 0xA0000ULL) {
586 printk("copy_e820_map() lies in range...\n");
587 if (start < 0xA0000ULL) {
588 printk("copy_e820_map() start < 0xA0000ULL\n");
589 add_memory_region(start, 0xA0000ULL-start, type);
591 if (end <= 0x100000ULL) {
592 printk("copy_e820_map() end <= 0x100000ULL\n");
600 add_memory_region(start, size, type);
601 } while (biosmap++,--nr_map);
606 * Callback for efi_memory_walk.
609 efi_find_max_pfn(unsigned long start, unsigned long end, void *arg)
611 unsigned long *max_pfn = arg, pfn;
614 pfn = PFN_UP(end -1);
622 efi_memory_present_wrapper(unsigned long start, unsigned long end, void *arg)
624 memory_present(0, PFN_UP(start), PFN_DOWN(end));
629 * Find the highest page frame number we have available
631 void __init find_max_pfn(void)
637 efi_memmap_walk(efi_find_max_pfn, &max_pfn);
638 efi_memmap_walk(efi_memory_present_wrapper, NULL);
642 for (i = 0; i < e820.nr_map; i++) {
643 unsigned long start, end;
645 if (e820.map[i].type != E820_RAM)
647 start = PFN_UP(e820.map[i].addr);
648 end = PFN_DOWN(e820.map[i].addr + e820.map[i].size);
653 memory_present(0, start, end);
658 * Free all available memory for boot time allocation. Used
659 * as a callback function by efi_memory_walk()
663 free_available_memory(unsigned long start, unsigned long end, void *arg)
665 /* check max_low_pfn */
666 if (start >= (max_low_pfn << PAGE_SHIFT))
668 if (end >= (max_low_pfn << PAGE_SHIFT))
669 end = max_low_pfn << PAGE_SHIFT;
671 free_bootmem(start, end - start);
676 * Register fully available low RAM pages with the bootmem allocator.
678 void __init register_bootmem_low_pages(unsigned long max_low_pfn)
683 efi_memmap_walk(free_available_memory, NULL);
686 for (i = 0; i < e820.nr_map; i++) {
687 unsigned long curr_pfn, last_pfn, size;
689 * Reserve usable low memory
691 if (e820.map[i].type != E820_RAM)
694 * We are rounding up the start address of usable memory:
696 curr_pfn = PFN_UP(e820.map[i].addr);
697 if (curr_pfn >= max_low_pfn)
700 * ... and at the end of the usable range downwards:
702 last_pfn = PFN_DOWN(e820.map[i].addr + e820.map[i].size);
706 * Truncate to the number of actual pages currently
709 if (last_pfn > xen_start_info->nr_pages)
710 last_pfn = xen_start_info->nr_pages;
713 if (last_pfn > max_low_pfn)
714 last_pfn = max_low_pfn;
717 * .. finally, did all the rounding and playing
718 * around just make the area go away?
720 if (last_pfn <= curr_pfn)
723 size = last_pfn - curr_pfn;
724 free_bootmem(PFN_PHYS(curr_pfn), PFN_PHYS(size));
729 * Locate a unused range of the physical address space below 4G which
730 * can be used for PCI mappings.
733 e820_setup_gap(struct e820entry *e820, int nr_map)
735 unsigned long gapstart, gapsize, round;
736 unsigned long long last;
740 * Search for the bigest gap in the low 32 bits of the e820
743 last = 0x100000000ull;
744 gapstart = 0x10000000;
748 unsigned long long start = e820[i].addr;
749 unsigned long long end = start + e820[i].size;
752 * Since "last" is at most 4GB, we know we'll
753 * fit in 32 bits if this condition is true
756 unsigned long gap = last - end;
768 * See how much we want to round up: start off with
769 * rounding to the next 1MB area.
772 while ((gapsize >> 4) > round)
774 /* Fun with two's complement */
775 pci_mem_start = (gapstart + round) & -round;
777 printk("Allocating PCI resources starting at %08lx (gap: %08lx:%08lx)\n",
778 pci_mem_start, gapstart, gapsize);
781 void __init e820_register_memory(void)
784 e820_setup_gap(e820.map, e820.nr_map);
788 void __init print_memory_map(char *who)
792 for (i = 0; i < e820.nr_map; i++) {
793 printk(" %s: %016Lx - %016Lx ", who,
795 e820.map[i].addr + e820.map[i].size);
796 switch (e820.map[i].type) {
797 case E820_RAM: printk("(usable)\n");
800 printk("(reserved)\n");
803 printk("(ACPI data)\n");
806 printk("(ACPI NVS)\n");
808 default: printk("type %lu\n", e820.map[i].type);
814 static __init __always_inline void efi_limit_regions(unsigned long long size)
816 unsigned long long current_addr = 0;
817 efi_memory_desc_t *md, *next_md;
823 for (p = p1, i = 0; p < memmap.map_end; p += memmap.desc_size, i++) {
826 current_addr = md->phys_addr +
827 PFN_PHYS(md->num_pages);
828 if (is_available_memory(md)) {
829 if (md->phys_addr >= size) continue;
830 memcpy(next_md, md, memmap.desc_size);
831 if (current_addr >= size) {
832 next_md->num_pages -=
833 PFN_UP(current_addr-size);
835 p1 += memmap.desc_size;
838 } else if ((md->attribute & EFI_MEMORY_RUNTIME) ==
839 EFI_MEMORY_RUNTIME) {
840 /* In order to make runtime services
841 * available we have to include runtime
842 * memory regions in memory map */
843 memcpy(next_md, md, memmap.desc_size);
844 p1 += memmap.desc_size;
850 memmap.map_end = memmap.map +
851 (memmap.nr_map * memmap.desc_size);
854 void __init limit_regions(unsigned long long size)
856 unsigned long long current_addr = 0;
859 print_memory_map("limit_regions start");
861 efi_limit_regions(size);
864 for (i = 0; i < e820.nr_map; i++) {
865 current_addr = e820.map[i].addr + e820.map[i].size;
866 if (current_addr < size)
869 if (e820.map[i].type != E820_RAM)
872 if (e820.map[i].addr >= size) {
874 * This region starts past the end of the
875 * requested size, skip it completely.
880 e820.map[i].size -= current_addr - size;
882 print_memory_map("limit_regions endfor");
886 if (i==e820.nr_map && current_addr < size) {
888 * The e820 map finished before our requested size so
889 * extend the final entry to the requested address.
892 if (e820.map[i].type == E820_RAM)
893 e820.map[i].size -= current_addr - size;
895 add_memory_region(current_addr, size - current_addr, E820_RAM);
898 print_memory_map("limit_regions endfunc");
902 * This function checks if the entire range <start,end> is mapped with type.
904 * Note: this function only works correct if the e820 table is sorted and
905 * not-overlapping, which is the case
908 e820_all_mapped(unsigned long s, unsigned long e, unsigned type)
913 for (i = 0; i < e820.nr_map; i++) {
914 struct e820entry *ei = &e820.map[i];
915 if (type && ei->type != type)
917 /* is the region (part) in overlap with the current region ?*/
918 if (ei->addr >= end || ei->addr + ei->size <= start)
920 /* if the region is at the beginning of <start,end> we move
921 * start to the end of the region since it's ok until there
923 if (ei->addr <= start)
924 start = ei->addr + ei->size;
925 /* if start is now at or beyond end, we're done, full
928 return 1; /* we're done */
933 static int __init parse_memmap(char *arg)
938 if (strcmp(arg, "exactmap") == 0) {
939 #ifdef CONFIG_CRASH_DUMP
940 /* If we are doing a crash dump, we
941 * still need to know the real mem
942 * size before original memory map is
946 saved_max_pfn = max_pfn;
949 user_defined_memmap = 1;
951 /* If the user specifies memory size, we
952 * limit the BIOS-provided memory map to
953 * that size. exactmap can be used to specify
954 * the exact map. mem=number can be used to
955 * trim the existing memory map.
957 unsigned long long start_at, mem_size;
959 mem_size = memparse(arg, &arg);
961 start_at = memparse(arg+1, &arg);
962 add_memory_region(start_at, mem_size, E820_RAM);
963 } else if (*arg == '#') {
964 start_at = memparse(arg+1, &arg);
965 add_memory_region(start_at, mem_size, E820_ACPI);
966 } else if (*arg == '$') {
967 start_at = memparse(arg+1, &arg);
968 add_memory_region(start_at, mem_size, E820_RESERVED);
970 limit_regions(mem_size);
971 user_defined_memmap = 1;
976 early_param("memmap", parse_memmap);