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>
13 #include <linux/suspend.h>
15 #include <asm/pgtable.h>
18 #include <asm/setup.h>
21 struct change_member {
22 struct e820entry *pbios; /* pointer to original bios entry */
23 unsigned long long addr; /* address for this change point */
25 static struct change_member change_point_list[2*E820MAX] __initdata;
26 static struct change_member *change_point[2*E820MAX] __initdata;
27 static struct e820entry *overlap_list[E820MAX] __initdata;
28 static struct e820entry new_bios[E820MAX] __initdata;
29 /* For PCI or other memory-mapped resources */
30 unsigned long pci_mem_start = 0x10000000;
32 EXPORT_SYMBOL(pci_mem_start);
34 extern int user_defined_memmap;
36 static struct resource system_rom_resource = {
40 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
43 static struct resource extension_rom_resource = {
44 .name = "Extension ROM",
47 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
50 static struct resource adapter_rom_resources[] = { {
51 .name = "Adapter ROM",
54 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
56 .name = "Adapter ROM",
59 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
61 .name = "Adapter ROM",
64 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
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
82 static struct resource video_rom_resource = {
86 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
89 #define ROMSIGNATURE 0xaa55
91 static int __init romsignature(const unsigned char *rom)
93 const unsigned short * const ptr = (const unsigned short *)rom;
96 return probe_kernel_address(ptr, sig) == 0 && sig == ROMSIGNATURE;
99 static int __init romchecksum(const unsigned char *rom, unsigned long length)
101 unsigned char sum, c;
103 for (sum = 0; length && probe_kernel_address(rom++, c) == 0; length--)
105 return !length && !sum;
108 static void __init probe_roms(void)
110 const unsigned char *rom;
111 unsigned long start, length, upper;
116 upper = adapter_rom_resources[0].start;
117 for (start = video_rom_resource.start; start < upper; start += 2048) {
118 rom = isa_bus_to_virt(start);
119 if (!romsignature(rom))
122 video_rom_resource.start = start;
124 if (probe_kernel_address(rom + 2, c) != 0)
127 /* 0 < length <= 0x7f * 512, historically */
130 /* if checksum okay, trust length byte */
131 if (length && romchecksum(rom, length))
132 video_rom_resource.end = start + length - 1;
134 request_resource(&iomem_resource, &video_rom_resource);
138 start = (video_rom_resource.end + 1 + 2047) & ~2047UL;
143 request_resource(&iomem_resource, &system_rom_resource);
144 upper = system_rom_resource.start;
146 /* check for extension rom (ignore length byte!) */
147 rom = isa_bus_to_virt(extension_rom_resource.start);
148 if (romsignature(rom)) {
149 length = extension_rom_resource.end - extension_rom_resource.start + 1;
150 if (romchecksum(rom, length)) {
151 request_resource(&iomem_resource, &extension_rom_resource);
152 upper = extension_rom_resource.start;
156 /* check for adapter roms on 2k boundaries */
157 for (i = 0; i < ARRAY_SIZE(adapter_rom_resources) && start < upper; start += 2048) {
158 rom = isa_bus_to_virt(start);
159 if (!romsignature(rom))
162 if (probe_kernel_address(rom + 2, c) != 0)
165 /* 0 < length <= 0x7f * 512, historically */
168 /* but accept any length that fits if checksum okay */
169 if (!length || start + length > upper || !romchecksum(rom, length))
172 adapter_rom_resources[i].start = start;
173 adapter_rom_resources[i].end = start + length - 1;
174 request_resource(&iomem_resource, &adapter_rom_resources[i]);
176 start = adapter_rom_resources[i++].end & ~2047UL;
181 * Request address space for all standard RAM and ROM resources
182 * and also for regions reported as reserved by the e820.
184 void __init legacy_init_iomem_resources(struct resource *code_resource,
185 struct resource *data_resource,
186 struct resource *bss_resource)
191 for (i = 0; i < e820.nr_map; i++) {
192 struct resource *res;
193 #ifndef CONFIG_RESOURCES_64BIT
194 if (e820.map[i].addr + e820.map[i].size > 0x100000000ULL)
197 res = kzalloc(sizeof(struct resource), GFP_ATOMIC);
198 switch (e820.map[i].type) {
199 case E820_RAM: res->name = "System RAM"; break;
200 case E820_ACPI: res->name = "ACPI Tables"; break;
201 case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
202 default: res->name = "reserved";
204 res->start = e820.map[i].addr;
205 res->end = res->start + e820.map[i].size - 1;
206 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
207 if (request_resource(&iomem_resource, res)) {
211 if (e820.map[i].type == E820_RAM) {
213 * We don't know which RAM region contains kernel data,
214 * so we try it repeatedly and let the resource manager
217 request_resource(res, code_resource);
218 request_resource(res, data_resource);
219 request_resource(res, bss_resource);
221 if (crashk_res.start != crashk_res.end)
222 request_resource(res, &crashk_res);
228 #if defined(CONFIG_PM) && defined(CONFIG_HIBERNATION)
230 * e820_mark_nosave_regions - Find the ranges of physical addresses that do not
231 * correspond to e820 RAM areas and mark the corresponding pages as nosave for
234 * This function requires the e820 map to be sorted and without any
235 * overlapping entries and assumes the first e820 area to be RAM.
237 void __init e820_mark_nosave_regions(void)
242 pfn = PFN_DOWN(e820.map[0].addr + e820.map[0].size);
243 for (i = 1; i < e820.nr_map; i++) {
244 struct e820entry *ei = &e820.map[i];
246 if (pfn < PFN_UP(ei->addr))
247 register_nosave_region(pfn, PFN_UP(ei->addr));
249 pfn = PFN_DOWN(ei->addr + ei->size);
250 if (ei->type != E820_RAM)
251 register_nosave_region(PFN_UP(ei->addr), pfn);
253 if (pfn >= max_low_pfn)
259 void __init add_memory_region(unsigned long long start,
260 unsigned long long size, int type)
268 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
272 e820.map[x].addr = start;
273 e820.map[x].size = size;
274 e820.map[x].type = type;
277 } /* add_memory_region */
280 * Sanitize the BIOS e820 map.
282 * Some e820 responses include overlapping entries. The following
283 * replaces the original e820 map with a new one, removing overlaps.
286 int __init sanitize_e820_map(struct e820entry * biosmap, char * pnr_map)
288 struct change_member *change_tmp;
289 unsigned long current_type, last_type;
290 unsigned long long last_addr;
291 int chgidx, still_changing;
294 int old_nr, new_nr, chg_nr;
298 Visually we're performing the following (1,2,3,4 = memory types)...
300 Sample memory map (w/overlaps):
301 ____22__________________
302 ______________________4_
303 ____1111________________
304 _44_____________________
305 11111111________________
306 ____________________33__
307 ___________44___________
308 __________33333_________
309 ______________22________
310 ___________________2222_
311 _________111111111______
312 _____________________11_
313 _________________4______
315 Sanitized equivalent (no overlap):
316 1_______________________
317 _44_____________________
318 ___1____________________
319 ____22__________________
320 ______11________________
321 _________1______________
322 __________3_____________
323 ___________44___________
324 _____________33_________
325 _______________2________
326 ________________1_______
327 _________________4______
328 ___________________2____
329 ____________________33__
330 ______________________4_
332 /* if there's only one memory region, don't bother */
339 /* bail out if we find any unreasonable addresses in bios map */
340 for (i=0; i<old_nr; i++)
341 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr) {
345 /* create pointers for initial change-point information (for sorting) */
346 for (i=0; i < 2*old_nr; i++)
347 change_point[i] = &change_point_list[i];
349 /* record all known change-points (starting and ending addresses),
350 omitting those that are for empty memory regions */
352 for (i=0; i < old_nr; i++) {
353 if (biosmap[i].size != 0) {
354 change_point[chgidx]->addr = biosmap[i].addr;
355 change_point[chgidx++]->pbios = &biosmap[i];
356 change_point[chgidx]->addr = biosmap[i].addr + biosmap[i].size;
357 change_point[chgidx++]->pbios = &biosmap[i];
360 chg_nr = chgidx; /* true number of change-points */
362 /* sort change-point list by memory addresses (low -> high) */
364 while (still_changing) {
366 for (i=1; i < chg_nr; i++) {
367 /* if <current_addr> > <last_addr>, swap */
368 /* or, if current=<start_addr> & last=<end_addr>, swap */
369 if ((change_point[i]->addr < change_point[i-1]->addr) ||
370 ((change_point[i]->addr == change_point[i-1]->addr) &&
371 (change_point[i]->addr == change_point[i]->pbios->addr) &&
372 (change_point[i-1]->addr != change_point[i-1]->pbios->addr))
375 change_tmp = change_point[i];
376 change_point[i] = change_point[i-1];
377 change_point[i-1] = change_tmp;
383 /* create a new bios memory map, removing overlaps */
384 overlap_entries=0; /* number of entries in the overlap table */
385 new_bios_entry=0; /* index for creating new bios map entries */
386 last_type = 0; /* start with undefined memory type */
387 last_addr = 0; /* start with 0 as last starting address */
388 /* loop through change-points, determining affect on the new bios map */
389 for (chgidx=0; chgidx < chg_nr; chgidx++)
391 /* keep track of all overlapping bios entries */
392 if (change_point[chgidx]->addr == change_point[chgidx]->pbios->addr)
394 /* add map entry to overlap list (> 1 entry implies an overlap) */
395 overlap_list[overlap_entries++]=change_point[chgidx]->pbios;
399 /* remove entry from list (order independent, so swap with last) */
400 for (i=0; i<overlap_entries; i++)
402 if (overlap_list[i] == change_point[chgidx]->pbios)
403 overlap_list[i] = overlap_list[overlap_entries-1];
407 /* if there are overlapping entries, decide which "type" to use */
408 /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */
410 for (i=0; i<overlap_entries; i++)
411 if (overlap_list[i]->type > current_type)
412 current_type = overlap_list[i]->type;
413 /* continue building up new bios map based on this information */
414 if (current_type != last_type) {
415 if (last_type != 0) {
416 new_bios[new_bios_entry].size =
417 change_point[chgidx]->addr - last_addr;
418 /* move forward only if the new size was non-zero */
419 if (new_bios[new_bios_entry].size != 0)
420 if (++new_bios_entry >= E820MAX)
421 break; /* no more space left for new bios entries */
423 if (current_type != 0) {
424 new_bios[new_bios_entry].addr = change_point[chgidx]->addr;
425 new_bios[new_bios_entry].type = current_type;
426 last_addr=change_point[chgidx]->addr;
428 last_type = current_type;
431 new_nr = new_bios_entry; /* retain count for new bios entries */
433 /* copy new bios mapping into original location */
434 memcpy(biosmap, new_bios, new_nr*sizeof(struct e820entry));
441 * Copy the BIOS e820 map into a safe place.
443 * Sanity-check it while we're at it..
445 * If we're lucky and live on a modern system, the setup code
446 * will have given us a memory map that we can use to properly
447 * set up memory. If we aren't, we'll fake a memory map.
449 * We check to see that the memory map contains at least 2 elements
450 * before we'll use it, because the detection code in setup.S may
451 * not be perfect and most every PC known to man has two memory
452 * regions: one from 0 to 640k, and one from 1mb up. (The IBM
453 * thinkpad 560x, for example, does not cooperate with the memory
456 int __init copy_e820_map(struct e820entry * biosmap, int nr_map)
458 /* Only one memory region (or negative)? Ignore it */
463 unsigned long long start = biosmap->addr;
464 unsigned long long size = biosmap->size;
465 unsigned long long end = start + size;
466 unsigned long type = biosmap->type;
468 /* Overflow in 64 bits? Ignore the memory map. */
473 * Some BIOSes claim RAM in the 640k - 1M region.
474 * Not right. Fix it up.
476 if (type == E820_RAM) {
477 if (start < 0x100000ULL && end > 0xA0000ULL) {
478 if (start < 0xA0000ULL)
479 add_memory_region(start, 0xA0000ULL-start, type);
480 if (end <= 0x100000ULL)
486 add_memory_region(start, size, type);
487 } while (biosmap++,--nr_map);
492 * Callback for efi_memory_walk.
495 efi_find_max_pfn(unsigned long start, unsigned long end, void *arg)
497 unsigned long *max_pfn = arg, pfn;
500 pfn = PFN_UP(end -1);
508 efi_memory_present_wrapper(unsigned long start, unsigned long end, void *arg)
510 memory_present(0, PFN_UP(start), PFN_DOWN(end));
515 * Find the highest page frame number we have available
517 void __init find_max_pfn(void)
523 efi_memmap_walk(efi_find_max_pfn, &max_pfn);
524 efi_memmap_walk(efi_memory_present_wrapper, NULL);
528 for (i = 0; i < e820.nr_map; i++) {
529 unsigned long start, end;
531 if (e820.map[i].type != E820_RAM)
533 start = PFN_UP(e820.map[i].addr);
534 end = PFN_DOWN(e820.map[i].addr + e820.map[i].size);
539 memory_present(0, start, end);
544 * Free all available memory for boot time allocation. Used
545 * as a callback function by efi_memory_walk()
549 free_available_memory(unsigned long start, unsigned long end, void *arg)
551 /* check max_low_pfn */
552 if (start >= (max_low_pfn << PAGE_SHIFT))
554 if (end >= (max_low_pfn << PAGE_SHIFT))
555 end = max_low_pfn << PAGE_SHIFT;
557 free_bootmem(start, end - start);
562 * Register fully available low RAM pages with the bootmem allocator.
564 void __init register_bootmem_low_pages(unsigned long max_low_pfn)
569 efi_memmap_walk(free_available_memory, NULL);
572 for (i = 0; i < e820.nr_map; i++) {
573 unsigned long curr_pfn, last_pfn, size;
575 * Reserve usable low memory
577 if (e820.map[i].type != E820_RAM)
580 * We are rounding up the start address of usable memory:
582 curr_pfn = PFN_UP(e820.map[i].addr);
583 if (curr_pfn >= max_low_pfn)
586 * ... and at the end of the usable range downwards:
588 last_pfn = PFN_DOWN(e820.map[i].addr + e820.map[i].size);
590 if (last_pfn > max_low_pfn)
591 last_pfn = max_low_pfn;
594 * .. finally, did all the rounding and playing
595 * around just make the area go away?
597 if (last_pfn <= curr_pfn)
600 size = last_pfn - curr_pfn;
601 free_bootmem(PFN_PHYS(curr_pfn), PFN_PHYS(size));
605 void __init e820_register_memory(void)
607 unsigned long gapstart, gapsize, round;
608 unsigned long long last;
612 * Search for the biggest gap in the low 32 bits of the e820
615 last = 0x100000000ull;
616 gapstart = 0x10000000;
620 unsigned long long start = e820.map[i].addr;
621 unsigned long long end = start + e820.map[i].size;
624 * Since "last" is at most 4GB, we know we'll
625 * fit in 32 bits if this condition is true
628 unsigned long gap = last - end;
640 * See how much we want to round up: start off with
641 * rounding to the next 1MB area.
644 while ((gapsize >> 4) > round)
646 /* Fun with two's complement */
647 pci_mem_start = (gapstart + round) & -round;
649 printk("Allocating PCI resources starting at %08lx (gap: %08lx:%08lx)\n",
650 pci_mem_start, gapstart, gapsize);
653 void __init print_memory_map(char *who)
657 for (i = 0; i < e820.nr_map; i++) {
658 printk(" %s: %016Lx - %016Lx ", who,
660 e820.map[i].addr + e820.map[i].size);
661 switch (e820.map[i].type) {
662 case E820_RAM: printk("(usable)\n");
665 printk("(reserved)\n");
668 printk("(ACPI data)\n");
671 printk("(ACPI NVS)\n");
673 default: printk("type %u\n", e820.map[i].type);
679 static __init __always_inline void efi_limit_regions(unsigned long long size)
681 unsigned long long current_addr = 0;
682 efi_memory_desc_t *md, *next_md;
688 for (p = p1, i = 0; p < memmap.map_end; p += memmap.desc_size, i++) {
691 current_addr = md->phys_addr +
692 PFN_PHYS(md->num_pages);
693 if (is_available_memory(md)) {
694 if (md->phys_addr >= size) continue;
695 memcpy(next_md, md, memmap.desc_size);
696 if (current_addr >= size) {
697 next_md->num_pages -=
698 PFN_UP(current_addr-size);
700 p1 += memmap.desc_size;
703 } else if ((md->attribute & EFI_MEMORY_RUNTIME) ==
704 EFI_MEMORY_RUNTIME) {
705 /* In order to make runtime services
706 * available we have to include runtime
707 * memory regions in memory map */
708 memcpy(next_md, md, memmap.desc_size);
709 p1 += memmap.desc_size;
715 memmap.map_end = memmap.map +
716 (memmap.nr_map * memmap.desc_size);
719 void __init limit_regions(unsigned long long size)
721 unsigned long long current_addr;
724 print_memory_map("limit_regions start");
726 efi_limit_regions(size);
729 for (i = 0; i < e820.nr_map; i++) {
730 current_addr = e820.map[i].addr + e820.map[i].size;
731 if (current_addr < size)
734 if (e820.map[i].type != E820_RAM)
737 if (e820.map[i].addr >= size) {
739 * This region starts past the end of the
740 * requested size, skip it completely.
745 e820.map[i].size -= current_addr - size;
747 print_memory_map("limit_regions endfor");
750 print_memory_map("limit_regions endfunc");
754 * This function checks if any part of the range <start,end> is mapped
758 e820_any_mapped(u64 start, u64 end, unsigned type)
761 for (i = 0; i < e820.nr_map; i++) {
762 const struct e820entry *ei = &e820.map[i];
763 if (type && ei->type != type)
765 if (ei->addr >= end || ei->addr + ei->size <= start)
771 EXPORT_SYMBOL_GPL(e820_any_mapped);
774 * This function checks if the entire range <start,end> is mapped with type.
776 * Note: this function only works correct if the e820 table is sorted and
777 * not-overlapping, which is the case
780 e820_all_mapped(unsigned long s, unsigned long e, unsigned type)
785 for (i = 0; i < e820.nr_map; i++) {
786 struct e820entry *ei = &e820.map[i];
787 if (type && ei->type != type)
789 /* is the region (part) in overlap with the current region ?*/
790 if (ei->addr >= end || ei->addr + ei->size <= start)
792 /* if the region is at the beginning of <start,end> we move
793 * start to the end of the region since it's ok until there
795 if (ei->addr <= start)
796 start = ei->addr + ei->size;
797 /* if start is now at or beyond end, we're done, full
800 return 1; /* we're done */
805 static int __init parse_memmap(char *arg)
810 if (strcmp(arg, "exactmap") == 0) {
811 #ifdef CONFIG_CRASH_DUMP
812 /* If we are doing a crash dump, we
813 * still need to know the real mem
814 * size before original memory map is
818 saved_max_pfn = max_pfn;
821 user_defined_memmap = 1;
823 /* If the user specifies memory size, we
824 * limit the BIOS-provided memory map to
825 * that size. exactmap can be used to specify
826 * the exact map. mem=number can be used to
827 * trim the existing memory map.
829 unsigned long long start_at, mem_size;
831 mem_size = memparse(arg, &arg);
833 start_at = memparse(arg+1, &arg);
834 add_memory_region(start_at, mem_size, E820_RAM);
835 } else if (*arg == '#') {
836 start_at = memparse(arg+1, &arg);
837 add_memory_region(start_at, mem_size, E820_ACPI);
838 } else if (*arg == '$') {
839 start_at = memparse(arg+1, &arg);
840 add_memory_region(start_at, mem_size, E820_RESERVED);
842 limit_regions(mem_size);
843 user_defined_memmap = 1;
848 early_param("memmap", parse_memmap);