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>
17 #include <asm/setup.h>
21 EXPORT_SYMBOL(efi_enabled
);
25 struct change_member
{
26 struct e820entry
*pbios
; /* pointer to original bios entry */
27 unsigned long long addr
; /* address for this change point */
29 static struct change_member change_point_list
[2*E820MAX
] __initdata
;
30 static struct change_member
*change_point
[2*E820MAX
] __initdata
;
31 static struct e820entry
*overlap_list
[E820MAX
] __initdata
;
32 static struct e820entry new_bios
[E820MAX
] __initdata
;
33 /* For PCI or other memory-mapped resources */
34 unsigned long pci_mem_start
= 0x10000000;
36 EXPORT_SYMBOL(pci_mem_start
);
38 extern int user_defined_memmap
;
39 struct resource data_resource
= {
40 .name
= "Kernel data",
43 .flags
= IORESOURCE_BUSY
| IORESOURCE_MEM
46 struct resource code_resource
= {
47 .name
= "Kernel code",
50 .flags
= IORESOURCE_BUSY
| IORESOURCE_MEM
53 static struct resource system_rom_resource
= {
57 .flags
= IORESOURCE_BUSY
| IORESOURCE_READONLY
| IORESOURCE_MEM
60 static struct resource extension_rom_resource
= {
61 .name
= "Extension ROM",
64 .flags
= IORESOURCE_BUSY
| IORESOURCE_READONLY
| IORESOURCE_MEM
67 static struct resource adapter_rom_resources
[] = { {
68 .name
= "Adapter ROM",
71 .flags
= IORESOURCE_BUSY
| IORESOURCE_READONLY
| IORESOURCE_MEM
73 .name
= "Adapter ROM",
76 .flags
= IORESOURCE_BUSY
| IORESOURCE_READONLY
| IORESOURCE_MEM
78 .name
= "Adapter ROM",
81 .flags
= IORESOURCE_BUSY
| IORESOURCE_READONLY
| IORESOURCE_MEM
83 .name
= "Adapter ROM",
86 .flags
= IORESOURCE_BUSY
| IORESOURCE_READONLY
| IORESOURCE_MEM
88 .name
= "Adapter ROM",
91 .flags
= IORESOURCE_BUSY
| IORESOURCE_READONLY
| IORESOURCE_MEM
93 .name
= "Adapter ROM",
96 .flags
= IORESOURCE_BUSY
| IORESOURCE_READONLY
| IORESOURCE_MEM
99 static struct resource video_rom_resource
= {
103 .flags
= IORESOURCE_BUSY
| IORESOURCE_READONLY
| IORESOURCE_MEM
106 static struct resource video_ram_resource
= {
107 .name
= "Video RAM area",
110 .flags
= IORESOURCE_BUSY
| IORESOURCE_MEM
113 static struct resource standard_io_resources
[] = { {
117 .flags
= IORESOURCE_BUSY
| IORESOURCE_IO
122 .flags
= IORESOURCE_BUSY
| IORESOURCE_IO
127 .flags
= IORESOURCE_BUSY
| IORESOURCE_IO
132 .flags
= IORESOURCE_BUSY
| IORESOURCE_IO
137 .flags
= IORESOURCE_BUSY
| IORESOURCE_IO
139 .name
= "dma page reg",
142 .flags
= IORESOURCE_BUSY
| IORESOURCE_IO
147 .flags
= IORESOURCE_BUSY
| IORESOURCE_IO
152 .flags
= IORESOURCE_BUSY
| IORESOURCE_IO
157 .flags
= IORESOURCE_BUSY
| IORESOURCE_IO
160 #define ROMSIGNATURE 0xaa55
162 static int __init
romsignature(const unsigned char *rom
)
164 const unsigned short * const ptr
= (const unsigned short *)rom
;
167 return probe_kernel_address(ptr
, sig
) == 0 && sig
== ROMSIGNATURE
;
170 static int __init
romchecksum(const unsigned char *rom
, unsigned long length
)
172 unsigned char sum
, c
;
174 for (sum
= 0; length
&& probe_kernel_address(rom
++, c
) == 0; length
--)
176 return !length
&& !sum
;
179 static void __init
probe_roms(void)
181 const unsigned char *rom
;
182 unsigned long start
, length
, upper
;
187 upper
= adapter_rom_resources
[0].start
;
188 for (start
= video_rom_resource
.start
; start
< upper
; start
+= 2048) {
189 rom
= isa_bus_to_virt(start
);
190 if (!romsignature(rom
))
193 video_rom_resource
.start
= start
;
195 if (probe_kernel_address(rom
+ 2, c
) != 0)
198 /* 0 < length <= 0x7f * 512, historically */
201 /* if checksum okay, trust length byte */
202 if (length
&& romchecksum(rom
, length
))
203 video_rom_resource
.end
= start
+ length
- 1;
205 request_resource(&iomem_resource
, &video_rom_resource
);
209 start
= (video_rom_resource
.end
+ 1 + 2047) & ~2047UL;
214 request_resource(&iomem_resource
, &system_rom_resource
);
215 upper
= system_rom_resource
.start
;
217 /* check for extension rom (ignore length byte!) */
218 rom
= isa_bus_to_virt(extension_rom_resource
.start
);
219 if (romsignature(rom
)) {
220 length
= extension_rom_resource
.end
- extension_rom_resource
.start
+ 1;
221 if (romchecksum(rom
, length
)) {
222 request_resource(&iomem_resource
, &extension_rom_resource
);
223 upper
= extension_rom_resource
.start
;
227 /* check for adapter roms on 2k boundaries */
228 for (i
= 0; i
< ARRAY_SIZE(adapter_rom_resources
) && start
< upper
; start
+= 2048) {
229 rom
= isa_bus_to_virt(start
);
230 if (!romsignature(rom
))
233 if (probe_kernel_address(rom
+ 2, c
) != 0)
236 /* 0 < length <= 0x7f * 512, historically */
239 /* but accept any length that fits if checksum okay */
240 if (!length
|| start
+ length
> upper
|| !romchecksum(rom
, length
))
243 adapter_rom_resources
[i
].start
= start
;
244 adapter_rom_resources
[i
].end
= start
+ length
- 1;
245 request_resource(&iomem_resource
, &adapter_rom_resources
[i
]);
247 start
= adapter_rom_resources
[i
++].end
& ~2047UL;
252 * Request address space for all standard RAM and ROM resources
253 * and also for regions reported as reserved by the e820.
256 legacy_init_iomem_resources(struct resource
*code_resource
, struct resource
*data_resource
)
261 for (i
= 0; i
< e820
.nr_map
; i
++) {
262 struct resource
*res
;
263 #ifndef CONFIG_RESOURCES_64BIT
264 if (e820
.map
[i
].addr
+ e820
.map
[i
].size
> 0x100000000ULL
)
267 res
= kzalloc(sizeof(struct resource
), GFP_ATOMIC
);
268 switch (e820
.map
[i
].type
) {
269 case E820_RAM
: res
->name
= "System RAM"; break;
270 case E820_ACPI
: res
->name
= "ACPI Tables"; break;
271 case E820_NVS
: res
->name
= "ACPI Non-volatile Storage"; break;
272 default: res
->name
= "reserved";
274 res
->start
= e820
.map
[i
].addr
;
275 res
->end
= res
->start
+ e820
.map
[i
].size
- 1;
276 res
->flags
= IORESOURCE_MEM
| IORESOURCE_BUSY
;
277 if (request_resource(&iomem_resource
, res
)) {
281 if (e820
.map
[i
].type
== E820_RAM
) {
283 * We don't know which RAM region contains kernel data,
284 * so we try it repeatedly and let the resource manager
287 request_resource(res
, code_resource
);
288 request_resource(res
, data_resource
);
290 request_resource(res
, &crashk_res
);
297 * Request address space for all standard resources
299 * This is called just before pcibios_init(), which is also a
300 * subsys_initcall, but is linked in later (in arch/i386/pci/common.c).
302 static int __init
request_standard_resources(void)
306 printk("Setting up standard PCI resources\n");
308 efi_initialize_iomem_resources(&code_resource
, &data_resource
);
310 legacy_init_iomem_resources(&code_resource
, &data_resource
);
312 /* EFI systems may still have VGA */
313 request_resource(&iomem_resource
, &video_ram_resource
);
315 /* request I/O space for devices used on all i[345]86 PCs */
316 for (i
= 0; i
< ARRAY_SIZE(standard_io_resources
); i
++)
317 request_resource(&ioport_resource
, &standard_io_resources
[i
]);
321 subsys_initcall(request_standard_resources
);
323 void __init
add_memory_region(unsigned long long start
,
324 unsigned long long size
, int type
)
332 printk(KERN_ERR
"Ooops! Too many entries in the memory map!\n");
336 e820
.map
[x
].addr
= start
;
337 e820
.map
[x
].size
= size
;
338 e820
.map
[x
].type
= type
;
341 } /* add_memory_region */
344 * Sanitize the BIOS e820 map.
346 * Some e820 responses include overlapping entries. The following
347 * replaces the original e820 map with a new one, removing overlaps.
350 int __init
sanitize_e820_map(struct e820entry
* biosmap
, char * pnr_map
)
352 struct change_member
*change_tmp
;
353 unsigned long current_type
, last_type
;
354 unsigned long long last_addr
;
355 int chgidx
, still_changing
;
358 int old_nr
, new_nr
, chg_nr
;
362 Visually we're performing the following (1,2,3,4 = memory types)...
364 Sample memory map (w/overlaps):
365 ____22__________________
366 ______________________4_
367 ____1111________________
368 _44_____________________
369 11111111________________
370 ____________________33__
371 ___________44___________
372 __________33333_________
373 ______________22________
374 ___________________2222_
375 _________111111111______
376 _____________________11_
377 _________________4______
379 Sanitized equivalent (no overlap):
380 1_______________________
381 _44_____________________
382 ___1____________________
383 ____22__________________
384 ______11________________
385 _________1______________
386 __________3_____________
387 ___________44___________
388 _____________33_________
389 _______________2________
390 ________________1_______
391 _________________4______
392 ___________________2____
393 ____________________33__
394 ______________________4_
396 /* if there's only one memory region, don't bother */
403 /* bail out if we find any unreasonable addresses in bios map */
404 for (i
=0; i
<old_nr
; i
++)
405 if (biosmap
[i
].addr
+ biosmap
[i
].size
< biosmap
[i
].addr
) {
409 /* create pointers for initial change-point information (for sorting) */
410 for (i
=0; i
< 2*old_nr
; i
++)
411 change_point
[i
] = &change_point_list
[i
];
413 /* record all known change-points (starting and ending addresses),
414 omitting those that are for empty memory regions */
416 for (i
=0; i
< old_nr
; i
++) {
417 if (biosmap
[i
].size
!= 0) {
418 change_point
[chgidx
]->addr
= biosmap
[i
].addr
;
419 change_point
[chgidx
++]->pbios
= &biosmap
[i
];
420 change_point
[chgidx
]->addr
= biosmap
[i
].addr
+ biosmap
[i
].size
;
421 change_point
[chgidx
++]->pbios
= &biosmap
[i
];
424 chg_nr
= chgidx
; /* true number of change-points */
426 /* sort change-point list by memory addresses (low -> high) */
428 while (still_changing
) {
430 for (i
=1; i
< chg_nr
; i
++) {
431 /* if <current_addr> > <last_addr>, swap */
432 /* or, if current=<start_addr> & last=<end_addr>, swap */
433 if ((change_point
[i
]->addr
< change_point
[i
-1]->addr
) ||
434 ((change_point
[i
]->addr
== change_point
[i
-1]->addr
) &&
435 (change_point
[i
]->addr
== change_point
[i
]->pbios
->addr
) &&
436 (change_point
[i
-1]->addr
!= change_point
[i
-1]->pbios
->addr
))
439 change_tmp
= change_point
[i
];
440 change_point
[i
] = change_point
[i
-1];
441 change_point
[i
-1] = change_tmp
;
447 /* create a new bios memory map, removing overlaps */
448 overlap_entries
=0; /* number of entries in the overlap table */
449 new_bios_entry
=0; /* index for creating new bios map entries */
450 last_type
= 0; /* start with undefined memory type */
451 last_addr
= 0; /* start with 0 as last starting address */
452 /* loop through change-points, determining affect on the new bios map */
453 for (chgidx
=0; chgidx
< chg_nr
; chgidx
++)
455 /* keep track of all overlapping bios entries */
456 if (change_point
[chgidx
]->addr
== change_point
[chgidx
]->pbios
->addr
)
458 /* add map entry to overlap list (> 1 entry implies an overlap) */
459 overlap_list
[overlap_entries
++]=change_point
[chgidx
]->pbios
;
463 /* remove entry from list (order independent, so swap with last) */
464 for (i
=0; i
<overlap_entries
; i
++)
466 if (overlap_list
[i
] == change_point
[chgidx
]->pbios
)
467 overlap_list
[i
] = overlap_list
[overlap_entries
-1];
471 /* if there are overlapping entries, decide which "type" to use */
472 /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */
474 for (i
=0; i
<overlap_entries
; i
++)
475 if (overlap_list
[i
]->type
> current_type
)
476 current_type
= overlap_list
[i
]->type
;
477 /* continue building up new bios map based on this information */
478 if (current_type
!= last_type
) {
479 if (last_type
!= 0) {
480 new_bios
[new_bios_entry
].size
=
481 change_point
[chgidx
]->addr
- last_addr
;
482 /* move forward only if the new size was non-zero */
483 if (new_bios
[new_bios_entry
].size
!= 0)
484 if (++new_bios_entry
>= E820MAX
)
485 break; /* no more space left for new bios entries */
487 if (current_type
!= 0) {
488 new_bios
[new_bios_entry
].addr
= change_point
[chgidx
]->addr
;
489 new_bios
[new_bios_entry
].type
= current_type
;
490 last_addr
=change_point
[chgidx
]->addr
;
492 last_type
= current_type
;
495 new_nr
= new_bios_entry
; /* retain count for new bios entries */
497 /* copy new bios mapping into original location */
498 memcpy(biosmap
, new_bios
, new_nr
*sizeof(struct e820entry
));
505 * Copy the BIOS e820 map into a safe place.
507 * Sanity-check it while we're at it..
509 * If we're lucky and live on a modern system, the setup code
510 * will have given us a memory map that we can use to properly
511 * set up memory. If we aren't, we'll fake a memory map.
513 * We check to see that the memory map contains at least 2 elements
514 * before we'll use it, because the detection code in setup.S may
515 * not be perfect and most every PC known to man has two memory
516 * regions: one from 0 to 640k, and one from 1mb up. (The IBM
517 * thinkpad 560x, for example, does not cooperate with the memory
520 int __init
copy_e820_map(struct e820entry
* biosmap
, int nr_map
)
522 /* Only one memory region (or negative)? Ignore it */
527 unsigned long long start
= biosmap
->addr
;
528 unsigned long long size
= biosmap
->size
;
529 unsigned long long end
= start
+ size
;
530 unsigned long type
= biosmap
->type
;
532 /* Overflow in 64 bits? Ignore the memory map. */
537 * Some BIOSes claim RAM in the 640k - 1M region.
538 * Not right. Fix it up.
540 if (type
== E820_RAM
) {
541 if (start
< 0x100000ULL
&& end
> 0xA0000ULL
) {
542 if (start
< 0xA0000ULL
)
543 add_memory_region(start
, 0xA0000ULL
-start
, type
);
544 if (end
<= 0x100000ULL
)
550 add_memory_region(start
, size
, type
);
551 } while (biosmap
++,--nr_map
);
556 * Callback for efi_memory_walk.
559 efi_find_max_pfn(unsigned long start
, unsigned long end
, void *arg
)
561 unsigned long *max_pfn
= arg
, pfn
;
564 pfn
= PFN_UP(end
-1);
572 efi_memory_present_wrapper(unsigned long start
, unsigned long end
, void *arg
)
574 memory_present(0, PFN_UP(start
), PFN_DOWN(end
));
579 * Find the highest page frame number we have available
581 void __init
find_max_pfn(void)
587 efi_memmap_walk(efi_find_max_pfn
, &max_pfn
);
588 efi_memmap_walk(efi_memory_present_wrapper
, NULL
);
592 for (i
= 0; i
< e820
.nr_map
; i
++) {
593 unsigned long start
, end
;
595 if (e820
.map
[i
].type
!= E820_RAM
)
597 start
= PFN_UP(e820
.map
[i
].addr
);
598 end
= PFN_DOWN(e820
.map
[i
].addr
+ e820
.map
[i
].size
);
603 memory_present(0, start
, end
);
608 * Free all available memory for boot time allocation. Used
609 * as a callback function by efi_memory_walk()
613 free_available_memory(unsigned long start
, unsigned long end
, void *arg
)
615 /* check max_low_pfn */
616 if (start
>= (max_low_pfn
<< PAGE_SHIFT
))
618 if (end
>= (max_low_pfn
<< PAGE_SHIFT
))
619 end
= max_low_pfn
<< PAGE_SHIFT
;
621 free_bootmem(start
, end
- start
);
626 * Register fully available low RAM pages with the bootmem allocator.
628 void __init
register_bootmem_low_pages(unsigned long max_low_pfn
)
633 efi_memmap_walk(free_available_memory
, NULL
);
636 for (i
= 0; i
< e820
.nr_map
; i
++) {
637 unsigned long curr_pfn
, last_pfn
, size
;
639 * Reserve usable low memory
641 if (e820
.map
[i
].type
!= E820_RAM
)
644 * We are rounding up the start address of usable memory:
646 curr_pfn
= PFN_UP(e820
.map
[i
].addr
);
647 if (curr_pfn
>= max_low_pfn
)
650 * ... and at the end of the usable range downwards:
652 last_pfn
= PFN_DOWN(e820
.map
[i
].addr
+ e820
.map
[i
].size
);
654 if (last_pfn
> max_low_pfn
)
655 last_pfn
= max_low_pfn
;
658 * .. finally, did all the rounding and playing
659 * around just make the area go away?
661 if (last_pfn
<= curr_pfn
)
664 size
= last_pfn
- curr_pfn
;
665 free_bootmem(PFN_PHYS(curr_pfn
), PFN_PHYS(size
));
669 void __init
e820_register_memory(void)
671 unsigned long gapstart
, gapsize
, round
;
672 unsigned long long last
;
676 * Search for the bigest gap in the low 32 bits of the e820
679 last
= 0x100000000ull
;
680 gapstart
= 0x10000000;
684 unsigned long long start
= e820
.map
[i
].addr
;
685 unsigned long long end
= start
+ e820
.map
[i
].size
;
688 * Since "last" is at most 4GB, we know we'll
689 * fit in 32 bits if this condition is true
692 unsigned long gap
= last
- end
;
704 * See how much we want to round up: start off with
705 * rounding to the next 1MB area.
708 while ((gapsize
>> 4) > round
)
710 /* Fun with two's complement */
711 pci_mem_start
= (gapstart
+ round
) & -round
;
713 printk("Allocating PCI resources starting at %08lx (gap: %08lx:%08lx)\n",
714 pci_mem_start
, gapstart
, gapsize
);
717 void __init
print_memory_map(char *who
)
721 for (i
= 0; i
< e820
.nr_map
; i
++) {
722 printk(" %s: %016Lx - %016Lx ", who
,
724 e820
.map
[i
].addr
+ e820
.map
[i
].size
);
725 switch (e820
.map
[i
].type
) {
726 case E820_RAM
: printk("(usable)\n");
729 printk("(reserved)\n");
732 printk("(ACPI data)\n");
735 printk("(ACPI NVS)\n");
737 default: printk("type %lu\n", e820
.map
[i
].type
);
743 static __init __always_inline
void efi_limit_regions(unsigned long long size
)
745 unsigned long long current_addr
= 0;
746 efi_memory_desc_t
*md
, *next_md
;
752 for (p
= p1
, i
= 0; p
< memmap
.map_end
; p
+= memmap
.desc_size
, i
++) {
755 current_addr
= md
->phys_addr
+
756 PFN_PHYS(md
->num_pages
);
757 if (is_available_memory(md
)) {
758 if (md
->phys_addr
>= size
) continue;
759 memcpy(next_md
, md
, memmap
.desc_size
);
760 if (current_addr
>= size
) {
761 next_md
->num_pages
-=
762 PFN_UP(current_addr
-size
);
764 p1
+= memmap
.desc_size
;
767 } else if ((md
->attribute
& EFI_MEMORY_RUNTIME
) ==
768 EFI_MEMORY_RUNTIME
) {
769 /* In order to make runtime services
770 * available we have to include runtime
771 * memory regions in memory map */
772 memcpy(next_md
, md
, memmap
.desc_size
);
773 p1
+= memmap
.desc_size
;
779 memmap
.map_end
= memmap
.map
+
780 (memmap
.nr_map
* memmap
.desc_size
);
783 void __init
limit_regions(unsigned long long size
)
785 unsigned long long current_addr
;
788 print_memory_map("limit_regions start");
790 efi_limit_regions(size
);
793 for (i
= 0; i
< e820
.nr_map
; i
++) {
794 current_addr
= e820
.map
[i
].addr
+ e820
.map
[i
].size
;
795 if (current_addr
< size
)
798 if (e820
.map
[i
].type
!= E820_RAM
)
801 if (e820
.map
[i
].addr
>= size
) {
803 * This region starts past the end of the
804 * requested size, skip it completely.
809 e820
.map
[i
].size
-= current_addr
- size
;
811 print_memory_map("limit_regions endfor");
814 print_memory_map("limit_regions endfunc");
818 * This function checks if any part of the range <start,end> is mapped
822 e820_any_mapped(u64 start
, u64 end
, unsigned type
)
825 for (i
= 0; i
< e820
.nr_map
; i
++) {
826 const struct e820entry
*ei
= &e820
.map
[i
];
827 if (type
&& ei
->type
!= type
)
829 if (ei
->addr
>= end
|| ei
->addr
+ ei
->size
<= start
)
835 EXPORT_SYMBOL_GPL(e820_any_mapped
);
838 * This function checks if the entire range <start,end> is mapped with type.
840 * Note: this function only works correct if the e820 table is sorted and
841 * not-overlapping, which is the case
844 e820_all_mapped(unsigned long s
, unsigned long e
, unsigned type
)
849 for (i
= 0; i
< e820
.nr_map
; i
++) {
850 struct e820entry
*ei
= &e820
.map
[i
];
851 if (type
&& ei
->type
!= type
)
853 /* is the region (part) in overlap with the current region ?*/
854 if (ei
->addr
>= end
|| ei
->addr
+ ei
->size
<= start
)
856 /* if the region is at the beginning of <start,end> we move
857 * start to the end of the region since it's ok until there
859 if (ei
->addr
<= start
)
860 start
= ei
->addr
+ ei
->size
;
861 /* if start is now at or beyond end, we're done, full
864 return 1; /* we're done */
869 static int __init
parse_memmap(char *arg
)
874 if (strcmp(arg
, "exactmap") == 0) {
875 #ifdef CONFIG_CRASH_DUMP
876 /* If we are doing a crash dump, we
877 * still need to know the real mem
878 * size before original memory map is
882 saved_max_pfn
= max_pfn
;
885 user_defined_memmap
= 1;
887 /* If the user specifies memory size, we
888 * limit the BIOS-provided memory map to
889 * that size. exactmap can be used to specify
890 * the exact map. mem=number can be used to
891 * trim the existing memory map.
893 unsigned long long start_at
, mem_size
;
895 mem_size
= memparse(arg
, &arg
);
897 start_at
= memparse(arg
+1, &arg
);
898 add_memory_region(start_at
, mem_size
, E820_RAM
);
899 } else if (*arg
== '#') {
900 start_at
= memparse(arg
+1, &arg
);
901 add_memory_region(start_at
, mem_size
, E820_ACPI
);
902 } else if (*arg
== '$') {
903 start_at
= memparse(arg
+1, &arg
);
904 add_memory_region(start_at
, mem_size
, E820_RESERVED
);
906 limit_regions(mem_size
);
907 user_defined_memmap
= 1;
912 early_param("memmap", parse_memmap
);