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/pfn.h>
11 #include <linux/uaccess.h>
12 #include <linux/suspend.h>
14 #include <asm/pgtable.h>
17 #include <asm/setup.h>
20 struct change_member {
21 struct e820entry *pbios; /* pointer to original bios entry */
22 unsigned long long addr; /* address for this change point */
24 static struct change_member change_point_list[2*E820MAX] __initdata;
25 static struct change_member *change_point[2*E820MAX] __initdata;
26 static struct e820entry *overlap_list[E820MAX] __initdata;
27 static struct e820entry new_bios[E820MAX] __initdata;
28 /* For PCI or other memory-mapped resources */
29 unsigned long pci_mem_start = 0x10000000;
31 EXPORT_SYMBOL(pci_mem_start);
33 extern int user_defined_memmap;
35 static struct resource system_rom_resource = {
39 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
42 static struct resource extension_rom_resource = {
43 .name = "Extension ROM",
46 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
49 static struct resource adapter_rom_resources[] = { {
50 .name = "Adapter ROM",
53 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
55 .name = "Adapter ROM",
58 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
60 .name = "Adapter ROM",
63 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
65 .name = "Adapter ROM",
68 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
70 .name = "Adapter ROM",
73 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
75 .name = "Adapter ROM",
78 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
81 static struct resource video_rom_resource = {
85 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
88 #define ROMSIGNATURE 0xaa55
90 static int __init romsignature(const unsigned char *rom)
92 const unsigned short * const ptr = (const unsigned short *)rom;
95 return probe_kernel_address(ptr, sig) == 0 && sig == ROMSIGNATURE;
98 static int __init romchecksum(const unsigned char *rom, unsigned long length)
100 unsigned char sum, c;
102 for (sum = 0; length && probe_kernel_address(rom++, c) == 0; length--)
104 return !length && !sum;
107 static void __init probe_roms(void)
109 const unsigned char *rom;
110 unsigned long start, length, upper;
115 upper = adapter_rom_resources[0].start;
116 for (start = video_rom_resource.start; start < upper; start += 2048) {
117 rom = isa_bus_to_virt(start);
118 if (!romsignature(rom))
121 video_rom_resource.start = start;
123 if (probe_kernel_address(rom + 2, c) != 0)
126 /* 0 < length <= 0x7f * 512, historically */
129 /* if checksum okay, trust length byte */
130 if (length && romchecksum(rom, length))
131 video_rom_resource.end = start + length - 1;
133 request_resource(&iomem_resource, &video_rom_resource);
137 start = (video_rom_resource.end + 1 + 2047) & ~2047UL;
142 request_resource(&iomem_resource, &system_rom_resource);
143 upper = system_rom_resource.start;
145 /* check for extension rom (ignore length byte!) */
146 rom = isa_bus_to_virt(extension_rom_resource.start);
147 if (romsignature(rom)) {
148 length = extension_rom_resource.end - extension_rom_resource.start + 1;
149 if (romchecksum(rom, length)) {
150 request_resource(&iomem_resource, &extension_rom_resource);
151 upper = extension_rom_resource.start;
155 /* check for adapter roms on 2k boundaries */
156 for (i = 0; i < ARRAY_SIZE(adapter_rom_resources) && start < upper; start += 2048) {
157 rom = isa_bus_to_virt(start);
158 if (!romsignature(rom))
161 if (probe_kernel_address(rom + 2, c) != 0)
164 /* 0 < length <= 0x7f * 512, historically */
167 /* but accept any length that fits if checksum okay */
168 if (!length || start + length > upper || !romchecksum(rom, length))
171 adapter_rom_resources[i].start = start;
172 adapter_rom_resources[i].end = start + length - 1;
173 request_resource(&iomem_resource, &adapter_rom_resources[i]);
175 start = adapter_rom_resources[i++].end & ~2047UL;
180 * Request address space for all standard RAM and ROM resources
181 * and also for regions reported as reserved by the e820.
183 void __init init_iomem_resources(struct resource *code_resource,
184 struct resource *data_resource,
185 struct resource *bss_resource)
190 for (i = 0; i < e820.nr_map; i++) {
191 struct resource *res;
192 #ifndef CONFIG_RESOURCES_64BIT
193 if (e820.map[i].addr + e820.map[i].size > 0x100000000ULL)
196 res = kzalloc(sizeof(struct resource), GFP_ATOMIC);
197 switch (e820.map[i].type) {
198 case E820_RAM: res->name = "System RAM"; break;
199 case E820_ACPI: res->name = "ACPI Tables"; break;
200 case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
201 default: res->name = "reserved";
203 res->start = e820.map[i].addr;
204 res->end = res->start + e820.map[i].size - 1;
205 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
206 if (request_resource(&iomem_resource, res)) {
210 if (e820.map[i].type == E820_RAM) {
212 * We don't know which RAM region contains kernel data,
213 * so we try it repeatedly and let the resource manager
216 request_resource(res, code_resource);
217 request_resource(res, data_resource);
218 request_resource(res, bss_resource);
220 if (crashk_res.start != crashk_res.end)
221 request_resource(res, &crashk_res);
227 #if defined(CONFIG_PM) && defined(CONFIG_HIBERNATION)
229 * e820_mark_nosave_regions - Find the ranges of physical addresses that do not
230 * correspond to e820 RAM areas and mark the corresponding pages as nosave for
233 * This function requires the e820 map to be sorted and without any
234 * overlapping entries and assumes the first e820 area to be RAM.
236 void __init e820_mark_nosave_regions(void)
241 pfn = PFN_DOWN(e820.map[0].addr + e820.map[0].size);
242 for (i = 1; i < e820.nr_map; i++) {
243 struct e820entry *ei = &e820.map[i];
245 if (pfn < PFN_UP(ei->addr))
246 register_nosave_region(pfn, PFN_UP(ei->addr));
248 pfn = PFN_DOWN(ei->addr + ei->size);
249 if (ei->type != E820_RAM)
250 register_nosave_region(PFN_UP(ei->addr), pfn);
252 if (pfn >= max_low_pfn)
258 void __init add_memory_region(unsigned long long start,
259 unsigned long long size, int type)
266 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
270 e820.map[x].addr = start;
271 e820.map[x].size = size;
272 e820.map[x].type = type;
274 } /* add_memory_region */
277 * Sanitize the BIOS e820 map.
279 * Some e820 responses include overlapping entries. The following
280 * replaces the original e820 map with a new one, removing overlaps.
283 int __init sanitize_e820_map(struct e820entry * biosmap, char * pnr_map)
285 struct change_member *change_tmp;
286 unsigned long current_type, last_type;
287 unsigned long long last_addr;
288 int chgidx, still_changing;
291 int old_nr, new_nr, chg_nr;
295 Visually we're performing the following (1,2,3,4 = memory types)...
297 Sample memory map (w/overlaps):
298 ____22__________________
299 ______________________4_
300 ____1111________________
301 _44_____________________
302 11111111________________
303 ____________________33__
304 ___________44___________
305 __________33333_________
306 ______________22________
307 ___________________2222_
308 _________111111111______
309 _____________________11_
310 _________________4______
312 Sanitized equivalent (no overlap):
313 1_______________________
314 _44_____________________
315 ___1____________________
316 ____22__________________
317 ______11________________
318 _________1______________
319 __________3_____________
320 ___________44___________
321 _____________33_________
322 _______________2________
323 ________________1_______
324 _________________4______
325 ___________________2____
326 ____________________33__
327 ______________________4_
329 /* if there's only one memory region, don't bother */
336 /* bail out if we find any unreasonable addresses in bios map */
337 for (i=0; i<old_nr; i++)
338 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr) {
342 /* create pointers for initial change-point information (for sorting) */
343 for (i=0; i < 2*old_nr; i++)
344 change_point[i] = &change_point_list[i];
346 /* record all known change-points (starting and ending addresses),
347 omitting those that are for empty memory regions */
349 for (i=0; i < old_nr; i++) {
350 if (biosmap[i].size != 0) {
351 change_point[chgidx]->addr = biosmap[i].addr;
352 change_point[chgidx++]->pbios = &biosmap[i];
353 change_point[chgidx]->addr = biosmap[i].addr + biosmap[i].size;
354 change_point[chgidx++]->pbios = &biosmap[i];
357 chg_nr = chgidx; /* true number of change-points */
359 /* sort change-point list by memory addresses (low -> high) */
361 while (still_changing) {
363 for (i=1; i < chg_nr; i++) {
364 /* if <current_addr> > <last_addr>, swap */
365 /* or, if current=<start_addr> & last=<end_addr>, swap */
366 if ((change_point[i]->addr < change_point[i-1]->addr) ||
367 ((change_point[i]->addr == change_point[i-1]->addr) &&
368 (change_point[i]->addr == change_point[i]->pbios->addr) &&
369 (change_point[i-1]->addr != change_point[i-1]->pbios->addr))
372 change_tmp = change_point[i];
373 change_point[i] = change_point[i-1];
374 change_point[i-1] = change_tmp;
380 /* create a new bios memory map, removing overlaps */
381 overlap_entries=0; /* number of entries in the overlap table */
382 new_bios_entry=0; /* index for creating new bios map entries */
383 last_type = 0; /* start with undefined memory type */
384 last_addr = 0; /* start with 0 as last starting address */
385 /* loop through change-points, determining affect on the new bios map */
386 for (chgidx=0; chgidx < chg_nr; chgidx++)
388 /* keep track of all overlapping bios entries */
389 if (change_point[chgidx]->addr == change_point[chgidx]->pbios->addr)
391 /* add map entry to overlap list (> 1 entry implies an overlap) */
392 overlap_list[overlap_entries++]=change_point[chgidx]->pbios;
396 /* remove entry from list (order independent, so swap with last) */
397 for (i=0; i<overlap_entries; i++)
399 if (overlap_list[i] == change_point[chgidx]->pbios)
400 overlap_list[i] = overlap_list[overlap_entries-1];
404 /* if there are overlapping entries, decide which "type" to use */
405 /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */
407 for (i=0; i<overlap_entries; i++)
408 if (overlap_list[i]->type > current_type)
409 current_type = overlap_list[i]->type;
410 /* continue building up new bios map based on this information */
411 if (current_type != last_type) {
412 if (last_type != 0) {
413 new_bios[new_bios_entry].size =
414 change_point[chgidx]->addr - last_addr;
415 /* move forward only if the new size was non-zero */
416 if (new_bios[new_bios_entry].size != 0)
417 if (++new_bios_entry >= E820MAX)
418 break; /* no more space left for new bios entries */
420 if (current_type != 0) {
421 new_bios[new_bios_entry].addr = change_point[chgidx]->addr;
422 new_bios[new_bios_entry].type = current_type;
423 last_addr=change_point[chgidx]->addr;
425 last_type = current_type;
428 new_nr = new_bios_entry; /* retain count for new bios entries */
430 /* copy new bios mapping into original location */
431 memcpy(biosmap, new_bios, new_nr*sizeof(struct e820entry));
438 * Copy the BIOS e820 map into a safe place.
440 * Sanity-check it while we're at it..
442 * If we're lucky and live on a modern system, the setup code
443 * will have given us a memory map that we can use to properly
444 * set up memory. If we aren't, we'll fake a memory map.
446 * We check to see that the memory map contains at least 2 elements
447 * before we'll use it, because the detection code in setup.S may
448 * not be perfect and most every PC known to man has two memory
449 * regions: one from 0 to 640k, and one from 1mb up. (The IBM
450 * thinkpad 560x, for example, does not cooperate with the memory
453 int __init copy_e820_map(struct e820entry *biosmap, int nr_map)
455 /* Only one memory region (or negative)? Ignore it */
460 u64 start = biosmap->addr;
461 u64 size = biosmap->size;
462 u64 end = start + size;
463 u32 type = biosmap->type;
465 /* Overflow in 64 bits? Ignore the memory map. */
469 add_memory_region(start, size, type);
470 } while (biosmap++, --nr_map);
476 * Find the highest page frame number we have available
478 void __init propagate_e820_map(void)
484 for (i = 0; i < e820.nr_map; i++) {
485 unsigned long start, end;
487 if (e820.map[i].type != E820_RAM)
489 start = PFN_UP(e820.map[i].addr);
490 end = PFN_DOWN(e820.map[i].addr + e820.map[i].size);
495 memory_present(0, start, end);
500 * Register fully available low RAM pages with the bootmem allocator.
502 void __init register_bootmem_low_pages(unsigned long max_low_pfn)
506 for (i = 0; i < e820.nr_map; i++) {
507 unsigned long curr_pfn, last_pfn, size;
509 * Reserve usable low memory
511 if (e820.map[i].type != E820_RAM)
514 * We are rounding up the start address of usable memory:
516 curr_pfn = PFN_UP(e820.map[i].addr);
517 if (curr_pfn >= max_low_pfn)
520 * ... and at the end of the usable range downwards:
522 last_pfn = PFN_DOWN(e820.map[i].addr + e820.map[i].size);
524 if (last_pfn > max_low_pfn)
525 last_pfn = max_low_pfn;
528 * .. finally, did all the rounding and playing
529 * around just make the area go away?
531 if (last_pfn <= curr_pfn)
534 size = last_pfn - curr_pfn;
535 free_bootmem(PFN_PHYS(curr_pfn), PFN_PHYS(size));
539 void __init e820_register_memory(void)
541 unsigned long gapstart, gapsize, round;
542 unsigned long long last;
546 * Search for the biggest gap in the low 32 bits of the e820
549 last = 0x100000000ull;
550 gapstart = 0x10000000;
554 unsigned long long start = e820.map[i].addr;
555 unsigned long long end = start + e820.map[i].size;
558 * Since "last" is at most 4GB, we know we'll
559 * fit in 32 bits if this condition is true
562 unsigned long gap = last - end;
574 * See how much we want to round up: start off with
575 * rounding to the next 1MB area.
578 while ((gapsize >> 4) > round)
580 /* Fun with two's complement */
581 pci_mem_start = (gapstart + round) & -round;
583 printk("Allocating PCI resources starting at %08lx (gap: %08lx:%08lx)\n",
584 pci_mem_start, gapstart, gapsize);
587 void __init print_memory_map(char *who)
591 for (i = 0; i < e820.nr_map; i++) {
592 printk(" %s: %016Lx - %016Lx ", who,
594 e820.map[i].addr + e820.map[i].size);
595 switch (e820.map[i].type) {
596 case E820_RAM: printk("(usable)\n");
599 printk("(reserved)\n");
602 printk("(ACPI data)\n");
605 printk("(ACPI NVS)\n");
607 default: printk("type %u\n", e820.map[i].type);
613 void __init limit_regions(unsigned long long size)
615 unsigned long long current_addr;
618 print_memory_map("limit_regions start");
619 for (i = 0; i < e820.nr_map; i++) {
620 current_addr = e820.map[i].addr + e820.map[i].size;
621 if (current_addr < size)
624 if (e820.map[i].type != E820_RAM)
627 if (e820.map[i].addr >= size) {
629 * This region starts past the end of the
630 * requested size, skip it completely.
635 e820.map[i].size -= current_addr - size;
637 print_memory_map("limit_regions endfor");
640 print_memory_map("limit_regions endfunc");
644 * This function checks if any part of the range <start,end> is mapped
648 e820_any_mapped(u64 start, u64 end, unsigned type)
651 for (i = 0; i < e820.nr_map; i++) {
652 const struct e820entry *ei = &e820.map[i];
653 if (type && ei->type != type)
655 if (ei->addr >= end || ei->addr + ei->size <= start)
661 EXPORT_SYMBOL_GPL(e820_any_mapped);
664 * This function checks if the entire range <start,end> is mapped with type.
666 * Note: this function only works correct if the e820 table is sorted and
667 * not-overlapping, which is the case
670 e820_all_mapped(unsigned long s, unsigned long e, unsigned type)
675 for (i = 0; i < e820.nr_map; i++) {
676 struct e820entry *ei = &e820.map[i];
677 if (type && ei->type != type)
679 /* is the region (part) in overlap with the current region ?*/
680 if (ei->addr >= end || ei->addr + ei->size <= start)
682 /* if the region is at the beginning of <start,end> we move
683 * start to the end of the region since it's ok until there
685 if (ei->addr <= start)
686 start = ei->addr + ei->size;
687 /* if start is now at or beyond end, we're done, full
690 return 1; /* we're done */
695 static int __init parse_memmap(char *arg)
700 if (strcmp(arg, "exactmap") == 0) {
701 #ifdef CONFIG_CRASH_DUMP
702 /* If we are doing a crash dump, we
703 * still need to know the real mem
704 * size before original memory map is
707 propagate_e820_map();
708 saved_max_pfn = max_pfn;
711 user_defined_memmap = 1;
713 /* If the user specifies memory size, we
714 * limit the BIOS-provided memory map to
715 * that size. exactmap can be used to specify
716 * the exact map. mem=number can be used to
717 * trim the existing memory map.
719 unsigned long long start_at, mem_size;
721 mem_size = memparse(arg, &arg);
723 start_at = memparse(arg+1, &arg);
724 add_memory_region(start_at, mem_size, E820_RAM);
725 } else if (*arg == '#') {
726 start_at = memparse(arg+1, &arg);
727 add_memory_region(start_at, mem_size, E820_ACPI);
728 } else if (*arg == '$') {
729 start_at = memparse(arg+1, &arg);
730 add_memory_region(start_at, mem_size, E820_RESERVED);
732 limit_regions(mem_size);
733 user_defined_memmap = 1;
738 early_param("memmap", parse_memmap);
739 void __init update_memory_range(u64 start, u64 size, unsigned old_type,
744 BUG_ON(old_type == new_type);
746 for (i = 0; i < e820.nr_map; i++) {
747 struct e820entry *ei = &e820.map[i];
748 u64 final_start, final_end;
749 if (ei->type != old_type)
751 /* totally covered? */
752 if (ei->addr >= start && ei->size <= size) {
756 /* partially covered */
757 final_start = max(start, ei->addr);
758 final_end = min(start + size, ei->addr + ei->size);
759 if (final_start >= final_end)
761 add_memory_region(final_start, final_end - final_start,
765 void __init update_e820(void)
769 nr_map = e820.nr_map;
770 if (sanitize_e820_map(e820.map, &nr_map))
772 e820.nr_map = nr_map;
773 printk(KERN_INFO "modified physical RAM map:\n");
774 print_memory_map("modified");