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>
20 EXPORT_SYMBOL(efi_enabled);
24 struct change_member {
25 struct e820entry *pbios; /* pointer to original bios entry */
26 unsigned long long addr; /* address for this change point */
28 static struct change_member change_point_list[2*E820MAX] __initdata;
29 static struct change_member *change_point[2*E820MAX] __initdata;
30 static struct e820entry *overlap_list[E820MAX] __initdata;
31 static struct e820entry new_bios[E820MAX] __initdata;
32 /* For PCI or other memory-mapped resources */
33 unsigned long pci_mem_start = 0x10000000;
35 EXPORT_SYMBOL(pci_mem_start);
37 extern int user_defined_memmap;
38 struct resource data_resource = {
39 .name = "Kernel data",
42 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
45 struct resource code_resource = {
46 .name = "Kernel code",
49 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
52 static struct resource system_rom_resource = {
56 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
59 static struct resource extension_rom_resource = {
60 .name = "Extension ROM",
63 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
66 static struct resource adapter_rom_resources[] = { {
67 .name = "Adapter ROM",
70 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
72 .name = "Adapter ROM",
75 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
77 .name = "Adapter ROM",
80 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
82 .name = "Adapter ROM",
85 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
87 .name = "Adapter ROM",
90 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
92 .name = "Adapter ROM",
95 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
98 static struct resource video_rom_resource = {
102 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
105 static struct resource video_ram_resource = {
106 .name = "Video RAM area",
109 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
112 static struct resource standard_io_resources[] = { {
116 .flags = IORESOURCE_BUSY | IORESOURCE_IO
121 .flags = IORESOURCE_BUSY | IORESOURCE_IO
126 .flags = IORESOURCE_BUSY | IORESOURCE_IO
131 .flags = IORESOURCE_BUSY | IORESOURCE_IO
136 .flags = IORESOURCE_BUSY | IORESOURCE_IO
138 .name = "dma page reg",
141 .flags = IORESOURCE_BUSY | IORESOURCE_IO
146 .flags = IORESOURCE_BUSY | IORESOURCE_IO
151 .flags = IORESOURCE_BUSY | IORESOURCE_IO
156 .flags = IORESOURCE_BUSY | IORESOURCE_IO
159 static int romsignature(const unsigned char *x)
163 if (probe_kernel_address((const unsigned short *)x, sig) == 0)
164 ret = (sig == 0xaa55);
168 static int __init romchecksum(unsigned char *rom, unsigned long length)
170 unsigned char *p, sum = 0;
172 for (p = rom; p < rom + length; p++)
177 static void __init probe_roms(void)
179 unsigned long start, length, upper;
184 upper = adapter_rom_resources[0].start;
185 for (start = video_rom_resource.start; start < upper; start += 2048) {
186 rom = isa_bus_to_virt(start);
187 if (!romsignature(rom))
190 video_rom_resource.start = start;
192 /* 0 < length <= 0x7f * 512, historically */
193 length = rom[2] * 512;
195 /* if checksum okay, trust length byte */
196 if (length && romchecksum(rom, length))
197 video_rom_resource.end = start + length - 1;
199 request_resource(&iomem_resource, &video_rom_resource);
203 start = (video_rom_resource.end + 1 + 2047) & ~2047UL;
208 request_resource(&iomem_resource, &system_rom_resource);
209 upper = system_rom_resource.start;
211 /* check for extension rom (ignore length byte!) */
212 rom = isa_bus_to_virt(extension_rom_resource.start);
213 if (romsignature(rom)) {
214 length = extension_rom_resource.end - extension_rom_resource.start + 1;
215 if (romchecksum(rom, length)) {
216 request_resource(&iomem_resource, &extension_rom_resource);
217 upper = extension_rom_resource.start;
221 /* check for adapter roms on 2k boundaries */
222 for (i = 0; i < ARRAY_SIZE(adapter_rom_resources) && start < upper; start += 2048) {
223 rom = isa_bus_to_virt(start);
224 if (!romsignature(rom))
227 /* 0 < length <= 0x7f * 512, historically */
228 length = rom[2] * 512;
230 /* but accept any length that fits if checksum okay */
231 if (!length || start + length > upper || !romchecksum(rom, length))
234 adapter_rom_resources[i].start = start;
235 adapter_rom_resources[i].end = start + length - 1;
236 request_resource(&iomem_resource, &adapter_rom_resources[i]);
238 start = adapter_rom_resources[i++].end & ~2047UL;
243 * Request address space for all standard RAM and ROM resources
244 * and also for regions reported as reserved by the e820.
247 legacy_init_iomem_resources(struct resource *code_resource, struct resource *data_resource)
252 for (i = 0; i < e820.nr_map; i++) {
253 struct resource *res;
254 #ifndef CONFIG_RESOURCES_64BIT
255 if (e820.map[i].addr + e820.map[i].size > 0x100000000ULL)
258 res = kzalloc(sizeof(struct resource), GFP_ATOMIC);
259 switch (e820.map[i].type) {
260 case E820_RAM: res->name = "System RAM"; break;
261 case E820_ACPI: res->name = "ACPI Tables"; break;
262 case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
263 default: res->name = "reserved";
265 res->start = e820.map[i].addr;
266 res->end = res->start + e820.map[i].size - 1;
267 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
268 if (request_resource(&iomem_resource, res)) {
272 if (e820.map[i].type == E820_RAM) {
274 * We don't know which RAM region contains kernel data,
275 * so we try it repeatedly and let the resource manager
278 request_resource(res, code_resource);
279 request_resource(res, data_resource);
281 request_resource(res, &crashk_res);
288 * Request address space for all standard resources
290 * This is called just before pcibios_init(), which is also a
291 * subsys_initcall, but is linked in later (in arch/i386/pci/common.c).
293 static int __init request_standard_resources(void)
297 printk("Setting up standard PCI resources\n");
299 efi_initialize_iomem_resources(&code_resource, &data_resource);
301 legacy_init_iomem_resources(&code_resource, &data_resource);
303 /* EFI systems may still have VGA */
304 request_resource(&iomem_resource, &video_ram_resource);
306 /* request I/O space for devices used on all i[345]86 PCs */
307 for (i = 0; i < ARRAY_SIZE(standard_io_resources); i++)
308 request_resource(&ioport_resource, &standard_io_resources[i]);
312 subsys_initcall(request_standard_resources);
314 void __init add_memory_region(unsigned long long start,
315 unsigned long long size, int type)
323 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
327 e820.map[x].addr = start;
328 e820.map[x].size = size;
329 e820.map[x].type = type;
332 } /* add_memory_region */
335 * Sanitize the BIOS e820 map.
337 * Some e820 responses include overlapping entries. The following
338 * replaces the original e820 map with a new one, removing overlaps.
341 int __init sanitize_e820_map(struct e820entry * biosmap, char * pnr_map)
343 struct change_member *change_tmp;
344 unsigned long current_type, last_type;
345 unsigned long long last_addr;
346 int chgidx, still_changing;
349 int old_nr, new_nr, chg_nr;
353 Visually we're performing the following (1,2,3,4 = memory types)...
355 Sample memory map (w/overlaps):
356 ____22__________________
357 ______________________4_
358 ____1111________________
359 _44_____________________
360 11111111________________
361 ____________________33__
362 ___________44___________
363 __________33333_________
364 ______________22________
365 ___________________2222_
366 _________111111111______
367 _____________________11_
368 _________________4______
370 Sanitized equivalent (no overlap):
371 1_______________________
372 _44_____________________
373 ___1____________________
374 ____22__________________
375 ______11________________
376 _________1______________
377 __________3_____________
378 ___________44___________
379 _____________33_________
380 _______________2________
381 ________________1_______
382 _________________4______
383 ___________________2____
384 ____________________33__
385 ______________________4_
387 printk("sanitize start\n");
388 /* if there's only one memory region, don't bother */
390 printk("sanitize bail 0\n");
396 /* bail out if we find any unreasonable addresses in bios map */
397 for (i=0; i<old_nr; i++)
398 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr) {
399 printk("sanitize bail 1\n");
403 /* create pointers for initial change-point information (for sorting) */
404 for (i=0; i < 2*old_nr; i++)
405 change_point[i] = &change_point_list[i];
407 /* record all known change-points (starting and ending addresses),
408 omitting those that are for empty memory regions */
410 for (i=0; i < old_nr; i++) {
411 if (biosmap[i].size != 0) {
412 change_point[chgidx]->addr = biosmap[i].addr;
413 change_point[chgidx++]->pbios = &biosmap[i];
414 change_point[chgidx]->addr = biosmap[i].addr + biosmap[i].size;
415 change_point[chgidx++]->pbios = &biosmap[i];
418 chg_nr = chgidx; /* true number of change-points */
420 /* sort change-point list by memory addresses (low -> high) */
422 while (still_changing) {
424 for (i=1; i < chg_nr; i++) {
425 /* if <current_addr> > <last_addr>, swap */
426 /* or, if current=<start_addr> & last=<end_addr>, swap */
427 if ((change_point[i]->addr < change_point[i-1]->addr) ||
428 ((change_point[i]->addr == change_point[i-1]->addr) &&
429 (change_point[i]->addr == change_point[i]->pbios->addr) &&
430 (change_point[i-1]->addr != change_point[i-1]->pbios->addr))
433 change_tmp = change_point[i];
434 change_point[i] = change_point[i-1];
435 change_point[i-1] = change_tmp;
441 /* create a new bios memory map, removing overlaps */
442 overlap_entries=0; /* number of entries in the overlap table */
443 new_bios_entry=0; /* index for creating new bios map entries */
444 last_type = 0; /* start with undefined memory type */
445 last_addr = 0; /* start with 0 as last starting address */
446 /* loop through change-points, determining affect on the new bios map */
447 for (chgidx=0; chgidx < chg_nr; chgidx++)
449 /* keep track of all overlapping bios entries */
450 if (change_point[chgidx]->addr == change_point[chgidx]->pbios->addr)
452 /* add map entry to overlap list (> 1 entry implies an overlap) */
453 overlap_list[overlap_entries++]=change_point[chgidx]->pbios;
457 /* remove entry from list (order independent, so swap with last) */
458 for (i=0; i<overlap_entries; i++)
460 if (overlap_list[i] == change_point[chgidx]->pbios)
461 overlap_list[i] = overlap_list[overlap_entries-1];
465 /* if there are overlapping entries, decide which "type" to use */
466 /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */
468 for (i=0; i<overlap_entries; i++)
469 if (overlap_list[i]->type > current_type)
470 current_type = overlap_list[i]->type;
471 /* continue building up new bios map based on this information */
472 if (current_type != last_type) {
473 if (last_type != 0) {
474 new_bios[new_bios_entry].size =
475 change_point[chgidx]->addr - last_addr;
476 /* move forward only if the new size was non-zero */
477 if (new_bios[new_bios_entry].size != 0)
478 if (++new_bios_entry >= E820MAX)
479 break; /* no more space left for new bios entries */
481 if (current_type != 0) {
482 new_bios[new_bios_entry].addr = change_point[chgidx]->addr;
483 new_bios[new_bios_entry].type = current_type;
484 last_addr=change_point[chgidx]->addr;
486 last_type = current_type;
489 new_nr = new_bios_entry; /* retain count for new bios entries */
491 /* copy new bios mapping into original location */
492 memcpy(biosmap, new_bios, new_nr*sizeof(struct e820entry));
495 printk("sanitize end\n");
500 * Copy the BIOS e820 map into a safe place.
502 * Sanity-check it while we're at it..
504 * If we're lucky and live on a modern system, the setup code
505 * will have given us a memory map that we can use to properly
506 * set up memory. If we aren't, we'll fake a memory map.
508 * We check to see that the memory map contains at least 2 elements
509 * before we'll use it, because the detection code in setup.S may
510 * not be perfect and most every PC known to man has two memory
511 * regions: one from 0 to 640k, and one from 1mb up. (The IBM
512 * thinkpad 560x, for example, does not cooperate with the memory
515 int __init copy_e820_map(struct e820entry * biosmap, int nr_map)
517 /* Only one memory region (or negative)? Ignore it */
522 unsigned long long start = biosmap->addr;
523 unsigned long long size = biosmap->size;
524 unsigned long long end = start + size;
525 unsigned long type = biosmap->type;
526 printk("copy_e820_map() start: %016Lx size: %016Lx end: %016Lx type: %ld\n", start, size, end, type);
528 /* Overflow in 64 bits? Ignore the memory map. */
533 * Some BIOSes claim RAM in the 640k - 1M region.
534 * Not right. Fix it up.
536 if (type == E820_RAM) {
537 printk("copy_e820_map() type is E820_RAM\n");
538 if (start < 0x100000ULL && end > 0xA0000ULL) {
539 printk("copy_e820_map() lies in range...\n");
540 if (start < 0xA0000ULL) {
541 printk("copy_e820_map() start < 0xA0000ULL\n");
542 add_memory_region(start, 0xA0000ULL-start, type);
544 if (end <= 0x100000ULL) {
545 printk("copy_e820_map() end <= 0x100000ULL\n");
552 add_memory_region(start, size, type);
553 } while (biosmap++,--nr_map);
558 * Callback for efi_memory_walk.
561 efi_find_max_pfn(unsigned long start, unsigned long end, void *arg)
563 unsigned long *max_pfn = arg, pfn;
566 pfn = PFN_UP(end -1);
574 efi_memory_present_wrapper(unsigned long start, unsigned long end, void *arg)
576 memory_present(0, PFN_UP(start), PFN_DOWN(end));
581 * Find the highest page frame number we have available
583 void __init find_max_pfn(void)
589 efi_memmap_walk(efi_find_max_pfn, &max_pfn);
590 efi_memmap_walk(efi_memory_present_wrapper, NULL);
594 for (i = 0; i < e820.nr_map; i++) {
595 unsigned long start, end;
597 if (e820.map[i].type != E820_RAM)
599 start = PFN_UP(e820.map[i].addr);
600 end = PFN_DOWN(e820.map[i].addr + e820.map[i].size);
605 memory_present(0, start, end);
610 * Free all available memory for boot time allocation. Used
611 * as a callback function by efi_memory_walk()
615 free_available_memory(unsigned long start, unsigned long end, void *arg)
617 /* check max_low_pfn */
618 if (start >= (max_low_pfn << PAGE_SHIFT))
620 if (end >= (max_low_pfn << PAGE_SHIFT))
621 end = max_low_pfn << PAGE_SHIFT;
623 free_bootmem(start, end - start);
628 * Register fully available low RAM pages with the bootmem allocator.
630 void __init register_bootmem_low_pages(unsigned long max_low_pfn)
635 efi_memmap_walk(free_available_memory, NULL);
638 for (i = 0; i < e820.nr_map; i++) {
639 unsigned long curr_pfn, last_pfn, size;
641 * Reserve usable low memory
643 if (e820.map[i].type != E820_RAM)
646 * We are rounding up the start address of usable memory:
648 curr_pfn = PFN_UP(e820.map[i].addr);
649 if (curr_pfn >= max_low_pfn)
652 * ... and at the end of the usable range downwards:
654 last_pfn = PFN_DOWN(e820.map[i].addr + e820.map[i].size);
656 if (last_pfn > max_low_pfn)
657 last_pfn = max_low_pfn;
660 * .. finally, did all the rounding and playing
661 * around just make the area go away?
663 if (last_pfn <= curr_pfn)
666 size = last_pfn - curr_pfn;
667 free_bootmem(PFN_PHYS(curr_pfn), PFN_PHYS(size));
671 void __init register_memory(void)
673 unsigned long gapstart, gapsize, round;
674 unsigned long long last;
678 * Search for the bigest gap in the low 32 bits of the e820
681 last = 0x100000000ull;
682 gapstart = 0x10000000;
686 unsigned long long start = e820.map[i].addr;
687 unsigned long long end = start + e820.map[i].size;
690 * Since "last" is at most 4GB, we know we'll
691 * fit in 32 bits if this condition is true
694 unsigned long gap = last - end;
706 * See how much we want to round up: start off with
707 * rounding to the next 1MB area.
710 while ((gapsize >> 4) > round)
712 /* Fun with two's complement */
713 pci_mem_start = (gapstart + round) & -round;
715 printk("Allocating PCI resources starting at %08lx (gap: %08lx:%08lx)\n",
716 pci_mem_start, gapstart, gapsize);
719 void __init print_memory_map(char *who)
723 for (i = 0; i < e820.nr_map; i++) {
724 printk(" %s: %016Lx - %016Lx ", who,
726 e820.map[i].addr + e820.map[i].size);
727 switch (e820.map[i].type) {
728 case E820_RAM: printk("(usable)\n");
731 printk("(reserved)\n");
734 printk("(ACPI data)\n");
737 printk("(ACPI NVS)\n");
739 default: printk("type %lu\n", e820.map[i].type);
745 static __init __always_inline void efi_limit_regions(unsigned long long size)
747 unsigned long long current_addr = 0;
748 efi_memory_desc_t *md, *next_md;
754 for (p = p1, i = 0; p < memmap.map_end; p += memmap.desc_size, i++) {
757 current_addr = md->phys_addr +
758 PFN_PHYS(md->num_pages);
759 if (is_available_memory(md)) {
760 if (md->phys_addr >= size) continue;
761 memcpy(next_md, md, memmap.desc_size);
762 if (current_addr >= size) {
763 next_md->num_pages -=
764 PFN_UP(current_addr-size);
766 p1 += memmap.desc_size;
769 } else if ((md->attribute & EFI_MEMORY_RUNTIME) ==
770 EFI_MEMORY_RUNTIME) {
771 /* In order to make runtime services
772 * available we have to include runtime
773 * memory regions in memory map */
774 memcpy(next_md, md, memmap.desc_size);
775 p1 += memmap.desc_size;
781 memmap.map_end = memmap.map +
782 (memmap.nr_map * memmap.desc_size);
785 void __init limit_regions(unsigned long long size)
787 unsigned long long current_addr;
790 print_memory_map("limit_regions start");
792 efi_limit_regions(size);
795 for (i = 0; i < e820.nr_map; i++) {
796 current_addr = e820.map[i].addr + e820.map[i].size;
797 if (current_addr < size)
800 if (e820.map[i].type != E820_RAM)
803 if (e820.map[i].addr >= size) {
805 * This region starts past the end of the
806 * requested size, skip it completely.
811 e820.map[i].size -= current_addr - size;
813 print_memory_map("limit_regions endfor");
816 print_memory_map("limit_regions endfunc");
820 * This function checks if the entire range <start,end> is mapped with type.
822 * Note: this function only works correct if the e820 table is sorted and
823 * not-overlapping, which is the case
826 e820_all_mapped(unsigned long s, unsigned long e, unsigned type)
831 for (i = 0; i < e820.nr_map; i++) {
832 struct e820entry *ei = &e820.map[i];
833 if (type && ei->type != type)
835 /* is the region (part) in overlap with the current region ?*/
836 if (ei->addr >= end || ei->addr + ei->size <= start)
838 /* if the region is at the beginning of <start,end> we move
839 * start to the end of the region since it's ok until there
841 if (ei->addr <= start)
842 start = ei->addr + ei->size;
843 /* if start is now at or beyond end, we're done, full
846 return 1; /* we're done */
851 static int __init parse_memmap(char *arg)
856 if (strcmp(arg, "exactmap") == 0) {
857 #ifdef CONFIG_CRASH_DUMP
858 /* If we are doing a crash dump, we
859 * still need to know the real mem
860 * size before original memory map is
864 saved_max_pfn = max_pfn;
867 user_defined_memmap = 1;
869 /* If the user specifies memory size, we
870 * limit the BIOS-provided memory map to
871 * that size. exactmap can be used to specify
872 * the exact map. mem=number can be used to
873 * trim the existing memory map.
875 unsigned long long start_at, mem_size;
877 mem_size = memparse(arg, &arg);
879 start_at = memparse(arg+1, &arg);
880 add_memory_region(start_at, mem_size, E820_RAM);
881 } else if (*arg == '#') {
882 start_at = memparse(arg+1, &arg);
883 add_memory_region(start_at, mem_size, E820_ACPI);
884 } else if (*arg == '$') {
885 start_at = memparse(arg+1, &arg);
886 add_memory_region(start_at, mem_size, E820_RESERVED);
888 limit_regions(mem_size);
889 user_defined_memmap = 1;
894 early_param("memmap", parse_memmap);