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 unsigned long long start
= biosmap
->addr
;
461 unsigned long long size
= biosmap
->size
;
462 unsigned long long end
= start
+ size
;
463 unsigned long type
= biosmap
->type
;
465 /* Overflow in 64 bits? Ignore the memory map. */
470 * Some BIOSes claim RAM in the 640k - 1M region.
471 * Not right. Fix it up.
473 if (type
== E820_RAM
) {
474 if (start
< 0x100000ULL
&& end
> 0xA0000ULL
) {
475 if (start
< 0xA0000ULL
)
476 add_memory_region(start
, 0xA0000ULL
-start
, type
);
477 if (end
<= 0x100000ULL
)
483 add_memory_region(start
, size
, type
);
484 } while (biosmap
++,--nr_map
);
489 * Find the highest page frame number we have available
491 void __init
find_max_pfn(void)
497 for (i
= 0; i
< e820
.nr_map
; i
++) {
498 unsigned long start
, end
;
500 if (e820
.map
[i
].type
!= E820_RAM
)
502 start
= PFN_UP(e820
.map
[i
].addr
);
503 end
= PFN_DOWN(e820
.map
[i
].addr
+ e820
.map
[i
].size
);
508 memory_present(0, start
, end
);
513 * Register fully available low RAM pages with the bootmem allocator.
515 void __init
register_bootmem_low_pages(unsigned long max_low_pfn
)
519 for (i
= 0; i
< e820
.nr_map
; i
++) {
520 unsigned long curr_pfn
, last_pfn
, size
;
522 * Reserve usable low memory
524 if (e820
.map
[i
].type
!= E820_RAM
)
527 * We are rounding up the start address of usable memory:
529 curr_pfn
= PFN_UP(e820
.map
[i
].addr
);
530 if (curr_pfn
>= max_low_pfn
)
533 * ... and at the end of the usable range downwards:
535 last_pfn
= PFN_DOWN(e820
.map
[i
].addr
+ e820
.map
[i
].size
);
537 if (last_pfn
> max_low_pfn
)
538 last_pfn
= max_low_pfn
;
541 * .. finally, did all the rounding and playing
542 * around just make the area go away?
544 if (last_pfn
<= curr_pfn
)
547 size
= last_pfn
- curr_pfn
;
548 free_bootmem(PFN_PHYS(curr_pfn
), PFN_PHYS(size
));
552 void __init
e820_register_memory(void)
554 unsigned long gapstart
, gapsize
, round
;
555 unsigned long long last
;
559 * Search for the biggest gap in the low 32 bits of the e820
562 last
= 0x100000000ull
;
563 gapstart
= 0x10000000;
567 unsigned long long start
= e820
.map
[i
].addr
;
568 unsigned long long end
= start
+ e820
.map
[i
].size
;
571 * Since "last" is at most 4GB, we know we'll
572 * fit in 32 bits if this condition is true
575 unsigned long gap
= last
- end
;
587 * See how much we want to round up: start off with
588 * rounding to the next 1MB area.
591 while ((gapsize
>> 4) > round
)
593 /* Fun with two's complement */
594 pci_mem_start
= (gapstart
+ round
) & -round
;
596 printk("Allocating PCI resources starting at %08lx (gap: %08lx:%08lx)\n",
597 pci_mem_start
, gapstart
, gapsize
);
600 void __init
print_memory_map(char *who
)
604 for (i
= 0; i
< e820
.nr_map
; i
++) {
605 printk(" %s: %016Lx - %016Lx ", who
,
607 e820
.map
[i
].addr
+ e820
.map
[i
].size
);
608 switch (e820
.map
[i
].type
) {
609 case E820_RAM
: printk("(usable)\n");
612 printk("(reserved)\n");
615 printk("(ACPI data)\n");
618 printk("(ACPI NVS)\n");
620 default: printk("type %u\n", e820
.map
[i
].type
);
626 void __init
limit_regions(unsigned long long size
)
628 unsigned long long current_addr
;
631 print_memory_map("limit_regions start");
632 for (i
= 0; i
< e820
.nr_map
; i
++) {
633 current_addr
= e820
.map
[i
].addr
+ e820
.map
[i
].size
;
634 if (current_addr
< size
)
637 if (e820
.map
[i
].type
!= E820_RAM
)
640 if (e820
.map
[i
].addr
>= size
) {
642 * This region starts past the end of the
643 * requested size, skip it completely.
648 e820
.map
[i
].size
-= current_addr
- size
;
650 print_memory_map("limit_regions endfor");
653 print_memory_map("limit_regions endfunc");
657 * This function checks if any part of the range <start,end> is mapped
661 e820_any_mapped(u64 start
, u64 end
, unsigned type
)
664 for (i
= 0; i
< e820
.nr_map
; i
++) {
665 const struct e820entry
*ei
= &e820
.map
[i
];
666 if (type
&& ei
->type
!= type
)
668 if (ei
->addr
>= end
|| ei
->addr
+ ei
->size
<= start
)
674 EXPORT_SYMBOL_GPL(e820_any_mapped
);
677 * This function checks if the entire range <start,end> is mapped with type.
679 * Note: this function only works correct if the e820 table is sorted and
680 * not-overlapping, which is the case
683 e820_all_mapped(unsigned long s
, unsigned long e
, unsigned type
)
688 for (i
= 0; i
< e820
.nr_map
; i
++) {
689 struct e820entry
*ei
= &e820
.map
[i
];
690 if (type
&& ei
->type
!= type
)
692 /* is the region (part) in overlap with the current region ?*/
693 if (ei
->addr
>= end
|| ei
->addr
+ ei
->size
<= start
)
695 /* if the region is at the beginning of <start,end> we move
696 * start to the end of the region since it's ok until there
698 if (ei
->addr
<= start
)
699 start
= ei
->addr
+ ei
->size
;
700 /* if start is now at or beyond end, we're done, full
703 return 1; /* we're done */
708 static int __init
parse_memmap(char *arg
)
713 if (strcmp(arg
, "exactmap") == 0) {
714 #ifdef CONFIG_CRASH_DUMP
715 /* If we are doing a crash dump, we
716 * still need to know the real mem
717 * size before original memory map is
721 saved_max_pfn
= max_pfn
;
724 user_defined_memmap
= 1;
726 /* If the user specifies memory size, we
727 * limit the BIOS-provided memory map to
728 * that size. exactmap can be used to specify
729 * the exact map. mem=number can be used to
730 * trim the existing memory map.
732 unsigned long long start_at
, mem_size
;
734 mem_size
= memparse(arg
, &arg
);
736 start_at
= memparse(arg
+1, &arg
);
737 add_memory_region(start_at
, mem_size
, E820_RAM
);
738 } else if (*arg
== '#') {
739 start_at
= memparse(arg
+1, &arg
);
740 add_memory_region(start_at
, mem_size
, E820_ACPI
);
741 } else if (*arg
== '$') {
742 start_at
= memparse(arg
+1, &arg
);
743 add_memory_region(start_at
, mem_size
, E820_RESERVED
);
745 limit_regions(mem_size
);
746 user_defined_memmap
= 1;
751 early_param("memmap", parse_memmap
);
752 void __init
update_e820(void)
756 nr_map
= e820
.nr_map
;
757 if (sanitize_e820_map(e820
.map
, &nr_map
))
759 e820
.nr_map
= nr_map
;
760 printk(KERN_INFO
"modified physical RAM map:\n");
761 print_memory_map("modified");