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>
13 #include <asm/pgtable.h>
19 EXPORT_SYMBOL(efi_enabled);
23 struct change_member {
24 struct e820entry *pbios; /* pointer to original bios entry */
25 unsigned long long addr; /* address for this change point */
27 static struct change_member change_point_list[2*E820MAX] __initdata;
28 static struct change_member *change_point[2*E820MAX] __initdata;
29 static struct e820entry *overlap_list[E820MAX] __initdata;
30 static struct e820entry new_bios[E820MAX] __initdata;
31 /* For PCI or other memory-mapped resources */
32 unsigned long pci_mem_start = 0x10000000;
34 EXPORT_SYMBOL(pci_mem_start);
36 extern int user_defined_memmap;
37 struct resource data_resource = {
38 .name = "Kernel data",
41 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
44 struct resource code_resource = {
45 .name = "Kernel code",
48 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
51 static struct resource system_rom_resource = {
55 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
58 static struct resource extension_rom_resource = {
59 .name = "Extension ROM",
62 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
65 static struct resource adapter_rom_resources[] = { {
66 .name = "Adapter ROM",
69 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
71 .name = "Adapter ROM",
74 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
76 .name = "Adapter ROM",
79 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
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
97 static struct resource video_rom_resource = {
101 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
104 static struct resource video_ram_resource = {
105 .name = "Video RAM area",
108 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
111 static struct resource standard_io_resources[] = { {
115 .flags = IORESOURCE_BUSY | IORESOURCE_IO
120 .flags = IORESOURCE_BUSY | IORESOURCE_IO
125 .flags = IORESOURCE_BUSY | IORESOURCE_IO
130 .flags = IORESOURCE_BUSY | IORESOURCE_IO
135 .flags = IORESOURCE_BUSY | IORESOURCE_IO
137 .name = "dma page reg",
140 .flags = IORESOURCE_BUSY | IORESOURCE_IO
145 .flags = IORESOURCE_BUSY | IORESOURCE_IO
150 .flags = IORESOURCE_BUSY | IORESOURCE_IO
155 .flags = IORESOURCE_BUSY | IORESOURCE_IO
158 #define romsignature(x) (*(unsigned short *)(x) == 0xaa55)
160 static int __init romchecksum(unsigned char *rom, unsigned long length)
162 unsigned char *p, sum = 0;
164 for (p = rom; p < rom + length; p++)
169 static void __init probe_roms(void)
171 unsigned long start, length, upper;
176 upper = adapter_rom_resources[0].start;
177 for (start = video_rom_resource.start; start < upper; start += 2048) {
178 rom = isa_bus_to_virt(start);
179 if (!romsignature(rom))
182 video_rom_resource.start = start;
184 /* 0 < length <= 0x7f * 512, historically */
185 length = rom[2] * 512;
187 /* if checksum okay, trust length byte */
188 if (length && romchecksum(rom, length))
189 video_rom_resource.end = start + length - 1;
191 request_resource(&iomem_resource, &video_rom_resource);
195 start = (video_rom_resource.end + 1 + 2047) & ~2047UL;
200 request_resource(&iomem_resource, &system_rom_resource);
201 upper = system_rom_resource.start;
203 /* check for extension rom (ignore length byte!) */
204 rom = isa_bus_to_virt(extension_rom_resource.start);
205 if (romsignature(rom)) {
206 length = extension_rom_resource.end - extension_rom_resource.start + 1;
207 if (romchecksum(rom, length)) {
208 request_resource(&iomem_resource, &extension_rom_resource);
209 upper = extension_rom_resource.start;
213 /* check for adapter roms on 2k boundaries */
214 for (i = 0; i < ARRAY_SIZE(adapter_rom_resources) && start < upper; start += 2048) {
215 rom = isa_bus_to_virt(start);
216 if (!romsignature(rom))
219 /* 0 < length <= 0x7f * 512, historically */
220 length = rom[2] * 512;
222 /* but accept any length that fits if checksum okay */
223 if (!length || start + length > upper || !romchecksum(rom, length))
226 adapter_rom_resources[i].start = start;
227 adapter_rom_resources[i].end = start + length - 1;
228 request_resource(&iomem_resource, &adapter_rom_resources[i]);
230 start = adapter_rom_resources[i++].end & ~2047UL;
235 * Request address space for all standard RAM and ROM resources
236 * and also for regions reported as reserved by the e820.
239 legacy_init_iomem_resources(struct resource *code_resource, struct resource *data_resource)
244 for (i = 0; i < e820.nr_map; i++) {
245 struct resource *res;
246 #ifndef CONFIG_RESOURCES_64BIT
247 if (e820.map[i].addr + e820.map[i].size > 0x100000000ULL)
250 res = kzalloc(sizeof(struct resource), GFP_ATOMIC);
251 switch (e820.map[i].type) {
252 case E820_RAM: res->name = "System RAM"; break;
253 case E820_ACPI: res->name = "ACPI Tables"; break;
254 case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
255 default: res->name = "reserved";
257 res->start = e820.map[i].addr;
258 res->end = res->start + e820.map[i].size - 1;
259 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
260 if (request_resource(&iomem_resource, res)) {
264 if (e820.map[i].type == E820_RAM) {
266 * We don't know which RAM region contains kernel data,
267 * so we try it repeatedly and let the resource manager
270 request_resource(res, code_resource);
271 request_resource(res, data_resource);
273 request_resource(res, &crashk_res);
280 * Request address space for all standard resources
282 * This is called just before pcibios_init(), which is also a
283 * subsys_initcall, but is linked in later (in arch/i386/pci/common.c).
285 static int __init request_standard_resources(void)
289 printk("Setting up standard PCI resources\n");
291 efi_initialize_iomem_resources(&code_resource, &data_resource);
293 legacy_init_iomem_resources(&code_resource, &data_resource);
295 /* EFI systems may still have VGA */
296 request_resource(&iomem_resource, &video_ram_resource);
298 /* request I/O space for devices used on all i[345]86 PCs */
299 for (i = 0; i < ARRAY_SIZE(standard_io_resources); i++)
300 request_resource(&ioport_resource, &standard_io_resources[i]);
304 subsys_initcall(request_standard_resources);
306 void __init add_memory_region(unsigned long long start,
307 unsigned long long size, int type)
315 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
319 e820.map[x].addr = start;
320 e820.map[x].size = size;
321 e820.map[x].type = type;
324 } /* add_memory_region */
327 * Sanitize the BIOS e820 map.
329 * Some e820 responses include overlapping entries. The following
330 * replaces the original e820 map with a new one, removing overlaps.
333 int __init sanitize_e820_map(struct e820entry * biosmap, char * pnr_map)
335 struct change_member *change_tmp;
336 unsigned long current_type, last_type;
337 unsigned long long last_addr;
338 int chgidx, still_changing;
341 int old_nr, new_nr, chg_nr;
345 Visually we're performing the following (1,2,3,4 = memory types)...
347 Sample memory map (w/overlaps):
348 ____22__________________
349 ______________________4_
350 ____1111________________
351 _44_____________________
352 11111111________________
353 ____________________33__
354 ___________44___________
355 __________33333_________
356 ______________22________
357 ___________________2222_
358 _________111111111______
359 _____________________11_
360 _________________4______
362 Sanitized equivalent (no overlap):
363 1_______________________
364 _44_____________________
365 ___1____________________
366 ____22__________________
367 ______11________________
368 _________1______________
369 __________3_____________
370 ___________44___________
371 _____________33_________
372 _______________2________
373 ________________1_______
374 _________________4______
375 ___________________2____
376 ____________________33__
377 ______________________4_
379 printk("sanitize start\n");
380 /* if there's only one memory region, don't bother */
382 printk("sanitize bail 0\n");
388 /* bail out if we find any unreasonable addresses in bios map */
389 for (i=0; i<old_nr; i++)
390 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr) {
391 printk("sanitize bail 1\n");
395 /* create pointers for initial change-point information (for sorting) */
396 for (i=0; i < 2*old_nr; i++)
397 change_point[i] = &change_point_list[i];
399 /* record all known change-points (starting and ending addresses),
400 omitting those that are for empty memory regions */
402 for (i=0; i < old_nr; i++) {
403 if (biosmap[i].size != 0) {
404 change_point[chgidx]->addr = biosmap[i].addr;
405 change_point[chgidx++]->pbios = &biosmap[i];
406 change_point[chgidx]->addr = biosmap[i].addr + biosmap[i].size;
407 change_point[chgidx++]->pbios = &biosmap[i];
410 chg_nr = chgidx; /* true number of change-points */
412 /* sort change-point list by memory addresses (low -> high) */
414 while (still_changing) {
416 for (i=1; i < chg_nr; i++) {
417 /* if <current_addr> > <last_addr>, swap */
418 /* or, if current=<start_addr> & last=<end_addr>, swap */
419 if ((change_point[i]->addr < change_point[i-1]->addr) ||
420 ((change_point[i]->addr == change_point[i-1]->addr) &&
421 (change_point[i]->addr == change_point[i]->pbios->addr) &&
422 (change_point[i-1]->addr != change_point[i-1]->pbios->addr))
425 change_tmp = change_point[i];
426 change_point[i] = change_point[i-1];
427 change_point[i-1] = change_tmp;
433 /* create a new bios memory map, removing overlaps */
434 overlap_entries=0; /* number of entries in the overlap table */
435 new_bios_entry=0; /* index for creating new bios map entries */
436 last_type = 0; /* start with undefined memory type */
437 last_addr = 0; /* start with 0 as last starting address */
438 /* loop through change-points, determining affect on the new bios map */
439 for (chgidx=0; chgidx < chg_nr; chgidx++)
441 /* keep track of all overlapping bios entries */
442 if (change_point[chgidx]->addr == change_point[chgidx]->pbios->addr)
444 /* add map entry to overlap list (> 1 entry implies an overlap) */
445 overlap_list[overlap_entries++]=change_point[chgidx]->pbios;
449 /* remove entry from list (order independent, so swap with last) */
450 for (i=0; i<overlap_entries; i++)
452 if (overlap_list[i] == change_point[chgidx]->pbios)
453 overlap_list[i] = overlap_list[overlap_entries-1];
457 /* if there are overlapping entries, decide which "type" to use */
458 /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */
460 for (i=0; i<overlap_entries; i++)
461 if (overlap_list[i]->type > current_type)
462 current_type = overlap_list[i]->type;
463 /* continue building up new bios map based on this information */
464 if (current_type != last_type) {
465 if (last_type != 0) {
466 new_bios[new_bios_entry].size =
467 change_point[chgidx]->addr - last_addr;
468 /* move forward only if the new size was non-zero */
469 if (new_bios[new_bios_entry].size != 0)
470 if (++new_bios_entry >= E820MAX)
471 break; /* no more space left for new bios entries */
473 if (current_type != 0) {
474 new_bios[new_bios_entry].addr = change_point[chgidx]->addr;
475 new_bios[new_bios_entry].type = current_type;
476 last_addr=change_point[chgidx]->addr;
478 last_type = current_type;
481 new_nr = new_bios_entry; /* retain count for new bios entries */
483 /* copy new bios mapping into original location */
484 memcpy(biosmap, new_bios, new_nr*sizeof(struct e820entry));
487 printk("sanitize end\n");
492 * Copy the BIOS e820 map into a safe place.
494 * Sanity-check it while we're at it..
496 * If we're lucky and live on a modern system, the setup code
497 * will have given us a memory map that we can use to properly
498 * set up memory. If we aren't, we'll fake a memory map.
500 * We check to see that the memory map contains at least 2 elements
501 * before we'll use it, because the detection code in setup.S may
502 * not be perfect and most every PC known to man has two memory
503 * regions: one from 0 to 640k, and one from 1mb up. (The IBM
504 * thinkpad 560x, for example, does not cooperate with the memory
507 int __init copy_e820_map(struct e820entry * biosmap, int nr_map)
509 /* Only one memory region (or negative)? Ignore it */
514 unsigned long long start = biosmap->addr;
515 unsigned long long size = biosmap->size;
516 unsigned long long end = start + size;
517 unsigned long type = biosmap->type;
518 printk("copy_e820_map() start: %016Lx size: %016Lx end: %016Lx type: %ld\n", start, size, end, type);
520 /* Overflow in 64 bits? Ignore the memory map. */
525 * Some BIOSes claim RAM in the 640k - 1M region.
526 * Not right. Fix it up.
528 if (type == E820_RAM) {
529 printk("copy_e820_map() type is E820_RAM\n");
530 if (start < 0x100000ULL && end > 0xA0000ULL) {
531 printk("copy_e820_map() lies in range...\n");
532 if (start < 0xA0000ULL) {
533 printk("copy_e820_map() start < 0xA0000ULL\n");
534 add_memory_region(start, 0xA0000ULL-start, type);
536 if (end <= 0x100000ULL) {
537 printk("copy_e820_map() end <= 0x100000ULL\n");
544 add_memory_region(start, size, type);
545 } while (biosmap++,--nr_map);
550 * Callback for efi_memory_walk.
553 efi_find_max_pfn(unsigned long start, unsigned long end, void *arg)
555 unsigned long *max_pfn = arg, pfn;
558 pfn = PFN_UP(end -1);
566 efi_memory_present_wrapper(unsigned long start, unsigned long end, void *arg)
568 memory_present(0, PFN_UP(start), PFN_DOWN(end));
573 * Find the highest page frame number we have available
575 void __init find_max_pfn(void)
581 efi_memmap_walk(efi_find_max_pfn, &max_pfn);
582 efi_memmap_walk(efi_memory_present_wrapper, NULL);
586 for (i = 0; i < e820.nr_map; i++) {
587 unsigned long start, end;
589 if (e820.map[i].type != E820_RAM)
591 start = PFN_UP(e820.map[i].addr);
592 end = PFN_DOWN(e820.map[i].addr + e820.map[i].size);
597 memory_present(0, start, end);
602 * Free all available memory for boot time allocation. Used
603 * as a callback function by efi_memory_walk()
607 free_available_memory(unsigned long start, unsigned long end, void *arg)
609 /* check max_low_pfn */
610 if (start >= (max_low_pfn << PAGE_SHIFT))
612 if (end >= (max_low_pfn << PAGE_SHIFT))
613 end = max_low_pfn << PAGE_SHIFT;
615 free_bootmem(start, end - start);
620 * Register fully available low RAM pages with the bootmem allocator.
622 void __init register_bootmem_low_pages(unsigned long max_low_pfn)
627 efi_memmap_walk(free_available_memory, NULL);
630 for (i = 0; i < e820.nr_map; i++) {
631 unsigned long curr_pfn, last_pfn, size;
633 * Reserve usable low memory
635 if (e820.map[i].type != E820_RAM)
638 * We are rounding up the start address of usable memory:
640 curr_pfn = PFN_UP(e820.map[i].addr);
641 if (curr_pfn >= max_low_pfn)
644 * ... and at the end of the usable range downwards:
646 last_pfn = PFN_DOWN(e820.map[i].addr + e820.map[i].size);
648 if (last_pfn > max_low_pfn)
649 last_pfn = max_low_pfn;
652 * .. finally, did all the rounding and playing
653 * around just make the area go away?
655 if (last_pfn <= curr_pfn)
658 size = last_pfn - curr_pfn;
659 free_bootmem(PFN_PHYS(curr_pfn), PFN_PHYS(size));
663 void __init register_memory(void)
665 unsigned long gapstart, gapsize, round;
666 unsigned long long last;
670 * Search for the bigest gap in the low 32 bits of the e820
673 last = 0x100000000ull;
674 gapstart = 0x10000000;
678 unsigned long long start = e820.map[i].addr;
679 unsigned long long end = start + e820.map[i].size;
682 * Since "last" is at most 4GB, we know we'll
683 * fit in 32 bits if this condition is true
686 unsigned long gap = last - end;
698 * See how much we want to round up: start off with
699 * rounding to the next 1MB area.
702 while ((gapsize >> 4) > round)
704 /* Fun with two's complement */
705 pci_mem_start = (gapstart + round) & -round;
707 printk("Allocating PCI resources starting at %08lx (gap: %08lx:%08lx)\n",
708 pci_mem_start, gapstart, gapsize);
711 void __init print_memory_map(char *who)
715 for (i = 0; i < e820.nr_map; i++) {
716 printk(" %s: %016Lx - %016Lx ", who,
718 e820.map[i].addr + e820.map[i].size);
719 switch (e820.map[i].type) {
720 case E820_RAM: printk("(usable)\n");
723 printk("(reserved)\n");
726 printk("(ACPI data)\n");
729 printk("(ACPI NVS)\n");
731 default: printk("type %lu\n", e820.map[i].type);
737 void __init limit_regions(unsigned long long size)
739 unsigned long long current_addr = 0;
742 print_memory_map("limit_regions start");
744 efi_memory_desc_t *md;
747 for (p = memmap.map, i = 0; p < memmap.map_end;
748 p += memmap.desc_size, i++) {
750 current_addr = md->phys_addr + (md->num_pages << 12);
751 if (md->type == EFI_CONVENTIONAL_MEMORY) {
752 if (current_addr >= size) {
754 (((current_addr-size) + PAGE_SIZE-1) >> PAGE_SHIFT);
755 memmap.nr_map = i + 1;
761 for (i = 0; i < e820.nr_map; i++) {
762 current_addr = e820.map[i].addr + e820.map[i].size;
763 if (current_addr < size)
766 if (e820.map[i].type != E820_RAM)
769 if (e820.map[i].addr >= size) {
771 * This region starts past the end of the
772 * requested size, skip it completely.
777 e820.map[i].size -= current_addr - size;
779 print_memory_map("limit_regions endfor");
782 print_memory_map("limit_regions endfunc");
786 * This function checks if the entire range <start,end> is mapped with type.
788 * Note: this function only works correct if the e820 table is sorted and
789 * not-overlapping, which is the case
792 e820_all_mapped(unsigned long s, unsigned long e, unsigned type)
797 for (i = 0; i < e820.nr_map; i++) {
798 struct e820entry *ei = &e820.map[i];
799 if (type && ei->type != type)
801 /* is the region (part) in overlap with the current region ?*/
802 if (ei->addr >= end || ei->addr + ei->size <= start)
804 /* if the region is at the beginning of <start,end> we move
805 * start to the end of the region since it's ok until there
807 if (ei->addr <= start)
808 start = ei->addr + ei->size;
809 /* if start is now at or beyond end, we're done, full
812 return 1; /* we're done */
817 static int __init parse_memmap(char *arg)
822 if (strcmp(arg, "exactmap") == 0) {
823 #ifdef CONFIG_CRASH_DUMP
824 /* If we are doing a crash dump, we
825 * still need to know the real mem
826 * size before original memory map is
830 saved_max_pfn = max_pfn;
833 user_defined_memmap = 1;
835 /* If the user specifies memory size, we
836 * limit the BIOS-provided memory map to
837 * that size. exactmap can be used to specify
838 * the exact map. mem=number can be used to
839 * trim the existing memory map.
841 unsigned long long start_at, mem_size;
843 mem_size = memparse(arg, &arg);
845 start_at = memparse(arg+1, &arg);
846 add_memory_region(start_at, mem_size, E820_RAM);
847 } else if (*arg == '#') {
848 start_at = memparse(arg+1, &arg);
849 add_memory_region(start_at, mem_size, E820_ACPI);
850 } else if (*arg == '$') {
851 start_at = memparse(arg+1, &arg);
852 add_memory_region(start_at, mem_size, E820_RESERVED);
854 limit_regions(mem_size);
855 user_defined_memmap = 1;
860 early_param("memmap", parse_memmap);