2 * Handle the memory map.
3 * The functions here do the job until bootmem takes over.
5 * Getting sanitize_e820_map() in sync with i386 version by applying change:
6 * - Provisions for empty E820 memory regions (reported by certain BIOSes).
7 * Alex Achenbach <xela@slit.de>, December 2002.
8 * Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
11 #include <linux/kernel.h>
12 #include <linux/types.h>
13 #include <linux/init.h>
14 #include <linux/bootmem.h>
15 #include <linux/ioport.h>
16 #include <linux/string.h>
17 #include <linux/kexec.h>
18 #include <linux/module.h>
20 #include <linux/suspend.h>
21 #include <linux/pfn.h>
23 #include <asm/pgtable.h>
26 #include <asm/proto.h>
27 #include <asm/setup.h>
28 #include <asm/sections.h>
29 #include <asm/kdebug.h>
34 * PFN of last memory page.
36 unsigned long end_pfn
;
39 * end_pfn only includes RAM, while end_pfn_map includes all e820 entries.
40 * The direct mapping extends to end_pfn_map, so that we can directly access
41 * apertures, ACPI and other tables without having to play with fixmaps.
43 unsigned long end_pfn_map
;
46 * Last pfn which the user wants to use.
48 static unsigned long __initdata end_user_pfn
= MAXMEM
>>PAGE_SHIFT
;
51 * Early reserved memory areas.
53 #define MAX_EARLY_RES 20
56 unsigned long start
, end
;
59 static struct early_res early_res
[MAX_EARLY_RES
] __initdata
= {
60 { 0, PAGE_SIZE
, "BIOS data page" }, /* BIOS data page */
62 { SMP_TRAMPOLINE_BASE
, SMP_TRAMPOLINE_BASE
+ 2*PAGE_SIZE
, "SMP_TRAMPOLINE" },
67 void __init
reserve_early(unsigned long start
, unsigned long end
, char *name
)
71 for (i
= 0; i
< MAX_EARLY_RES
&& early_res
[i
].end
; i
++) {
73 if (end
> r
->start
&& start
< r
->end
)
74 panic("Overlapping early reservations %lx-%lx %s to %lx-%lx %s\n",
75 start
, end
- 1, name
?name
:"", r
->start
, r
->end
- 1, r
->name
);
77 if (i
>= MAX_EARLY_RES
)
78 panic("Too many early reservations");
83 strncpy(r
->name
, name
, sizeof(r
->name
) - 1);
86 void __init
early_res_to_bootmem(void)
89 for (i
= 0; i
< MAX_EARLY_RES
&& early_res
[i
].end
; i
++) {
90 struct early_res
*r
= &early_res
[i
];
91 printk(KERN_INFO
"early res: %d [%lx-%lx] %s\n", i
,
92 r
->start
, r
->end
- 1, r
->name
);
93 reserve_bootmem_generic(r
->start
, r
->end
- r
->start
);
97 /* Check for already reserved areas */
98 static inline int bad_addr(unsigned long *addrp
, unsigned long size
)
101 unsigned long addr
= *addrp
, last
;
105 for (i
= 0; i
< MAX_EARLY_RES
&& early_res
[i
].end
; i
++) {
106 struct early_res
*r
= &early_res
[i
];
107 if (last
>= r
->start
&& addr
< r
->end
) {
108 *addrp
= addr
= r
->end
;
117 * This function checks if any part of the range <start,end> is mapped
121 e820_any_mapped(unsigned long start
, unsigned long end
, unsigned type
)
125 for (i
= 0; i
< e820
.nr_map
; i
++) {
126 struct e820entry
*ei
= &e820
.map
[i
];
128 if (type
&& ei
->type
!= type
)
130 if (ei
->addr
>= end
|| ei
->addr
+ ei
->size
<= start
)
136 EXPORT_SYMBOL_GPL(e820_any_mapped
);
139 * This function checks if the entire range <start,end> is mapped with type.
141 * Note: this function only works correct if the e820 table is sorted and
142 * not-overlapping, which is the case
144 int __init
e820_all_mapped(unsigned long start
, unsigned long end
,
149 for (i
= 0; i
< e820
.nr_map
; i
++) {
150 struct e820entry
*ei
= &e820
.map
[i
];
152 if (type
&& ei
->type
!= type
)
154 /* is the region (part) in overlap with the current region ?*/
155 if (ei
->addr
>= end
|| ei
->addr
+ ei
->size
<= start
)
158 /* if the region is at the beginning of <start,end> we move
159 * start to the end of the region since it's ok until there
161 if (ei
->addr
<= start
)
162 start
= ei
->addr
+ ei
->size
;
164 * if start is now at or beyond end, we're done, full
174 * Find a free area with specified alignment in a specific range.
176 unsigned long __init
find_e820_area(unsigned long start
, unsigned long end
,
177 unsigned size
, unsigned long align
)
180 unsigned long mask
= ~(align
- 1);
182 for (i
= 0; i
< e820
.nr_map
; i
++) {
183 struct e820entry
*ei
= &e820
.map
[i
];
184 unsigned long addr
= ei
->addr
, last
;
186 if (ei
->type
!= E820_RAM
)
190 if (addr
> ei
->addr
+ ei
->size
)
192 while (bad_addr(&addr
, size
) && addr
+size
<= ei
->addr
+ei
->size
)
194 addr
= (addr
+ align
- 1) & mask
;
196 if (last
> ei
->addr
+ ei
->size
)
206 * Find the highest page frame number we have available
208 unsigned long __init
e820_end_of_ram(void)
210 unsigned long end_pfn
;
212 end_pfn
= find_max_pfn_with_active_regions();
214 if (end_pfn
> end_pfn_map
)
215 end_pfn_map
= end_pfn
;
216 if (end_pfn_map
> MAXMEM
>>PAGE_SHIFT
)
217 end_pfn_map
= MAXMEM
>>PAGE_SHIFT
;
218 if (end_pfn
> end_user_pfn
)
219 end_pfn
= end_user_pfn
;
220 if (end_pfn
> end_pfn_map
)
221 end_pfn
= end_pfn_map
;
223 printk(KERN_INFO
"end_pfn_map = %lu\n", end_pfn_map
);
228 * Mark e820 reserved areas as busy for the resource manager.
230 void __init
e820_reserve_resources(struct resource
*code_resource
,
231 struct resource
*data_resource
, struct resource
*bss_resource
)
234 for (i
= 0; i
< e820
.nr_map
; i
++) {
235 struct resource
*res
;
236 res
= alloc_bootmem_low(sizeof(struct resource
));
237 switch (e820
.map
[i
].type
) {
238 case E820_RAM
: res
->name
= "System RAM"; break;
239 case E820_ACPI
: res
->name
= "ACPI Tables"; break;
240 case E820_NVS
: res
->name
= "ACPI Non-volatile Storage"; break;
241 default: res
->name
= "reserved";
243 res
->start
= e820
.map
[i
].addr
;
244 res
->end
= res
->start
+ e820
.map
[i
].size
- 1;
245 res
->flags
= IORESOURCE_MEM
| IORESOURCE_BUSY
;
246 request_resource(&iomem_resource
, res
);
247 if (e820
.map
[i
].type
== E820_RAM
) {
249 * We don't know which RAM region contains kernel data,
250 * so we try it repeatedly and let the resource manager
253 request_resource(res
, code_resource
);
254 request_resource(res
, data_resource
);
255 request_resource(res
, bss_resource
);
257 if (crashk_res
.start
!= crashk_res
.end
)
258 request_resource(res
, &crashk_res
);
265 * Find the ranges of physical addresses that do not correspond to
266 * e820 RAM areas and mark the corresponding pages as nosave for software
267 * suspend and suspend to RAM.
269 * This function requires the e820 map to be sorted and without any
270 * overlapping entries and assumes the first e820 area to be RAM.
272 void __init
e820_mark_nosave_regions(void)
277 paddr
= round_down(e820
.map
[0].addr
+ e820
.map
[0].size
, PAGE_SIZE
);
278 for (i
= 1; i
< e820
.nr_map
; i
++) {
279 struct e820entry
*ei
= &e820
.map
[i
];
281 if (paddr
< ei
->addr
)
282 register_nosave_region(PFN_DOWN(paddr
),
285 paddr
= round_down(ei
->addr
+ ei
->size
, PAGE_SIZE
);
286 if (ei
->type
!= E820_RAM
)
287 register_nosave_region(PFN_UP(ei
->addr
),
290 if (paddr
>= (end_pfn
<< PAGE_SHIFT
))
296 * Finds an active region in the address range from start_pfn to end_pfn and
297 * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
299 static int __init
e820_find_active_region(const struct e820entry
*ei
,
300 unsigned long start_pfn
,
301 unsigned long end_pfn
,
302 unsigned long *ei_startpfn
,
303 unsigned long *ei_endpfn
)
305 *ei_startpfn
= round_up(ei
->addr
, PAGE_SIZE
) >> PAGE_SHIFT
;
306 *ei_endpfn
= round_down(ei
->addr
+ ei
->size
, PAGE_SIZE
) >> PAGE_SHIFT
;
308 /* Skip map entries smaller than a page */
309 if (*ei_startpfn
>= *ei_endpfn
)
312 /* Check if end_pfn_map should be updated */
313 if (ei
->type
!= E820_RAM
&& *ei_endpfn
> end_pfn_map
)
314 end_pfn_map
= *ei_endpfn
;
316 /* Skip if map is outside the node */
317 if (ei
->type
!= E820_RAM
|| *ei_endpfn
<= start_pfn
||
318 *ei_startpfn
>= end_pfn
)
321 /* Check for overlaps */
322 if (*ei_startpfn
< start_pfn
)
323 *ei_startpfn
= start_pfn
;
324 if (*ei_endpfn
> end_pfn
)
325 *ei_endpfn
= end_pfn
;
327 /* Obey end_user_pfn to save on memmap */
328 if (*ei_startpfn
>= end_user_pfn
)
330 if (*ei_endpfn
> end_user_pfn
)
331 *ei_endpfn
= end_user_pfn
;
336 /* Walk the e820 map and register active regions within a node */
338 e820_register_active_regions(int nid
, unsigned long start_pfn
,
339 unsigned long end_pfn
)
341 unsigned long ei_startpfn
;
342 unsigned long ei_endpfn
;
345 for (i
= 0; i
< e820
.nr_map
; i
++)
346 if (e820_find_active_region(&e820
.map
[i
],
348 &ei_startpfn
, &ei_endpfn
))
349 add_active_range(nid
, ei_startpfn
, ei_endpfn
);
353 * Add a memory region to the kernel e820 map.
355 void __init
add_memory_region(unsigned long start
, unsigned long size
, int type
)
360 printk(KERN_ERR
"Ooops! Too many entries in the memory map!\n");
364 e820
.map
[x
].addr
= start
;
365 e820
.map
[x
].size
= size
;
366 e820
.map
[x
].type
= type
;
371 * Find the hole size (in bytes) in the memory range.
372 * @start: starting address of the memory range to scan
373 * @end: ending address of the memory range to scan
375 unsigned long __init
e820_hole_size(unsigned long start
, unsigned long end
)
377 unsigned long start_pfn
= start
>> PAGE_SHIFT
;
378 unsigned long end_pfn
= end
>> PAGE_SHIFT
;
379 unsigned long ei_startpfn
, ei_endpfn
, ram
= 0;
382 for (i
= 0; i
< e820
.nr_map
; i
++) {
383 if (e820_find_active_region(&e820
.map
[i
],
385 &ei_startpfn
, &ei_endpfn
))
386 ram
+= ei_endpfn
- ei_startpfn
;
388 return end
- start
- (ram
<< PAGE_SHIFT
);
391 static void __init
e820_print_map(char *who
)
395 for (i
= 0; i
< e820
.nr_map
; i
++) {
396 printk(KERN_INFO
" %s: %016Lx - %016Lx ", who
,
397 (unsigned long long) e820
.map
[i
].addr
,
399 (e820
.map
[i
].addr
+ e820
.map
[i
].size
));
400 switch (e820
.map
[i
].type
) {
402 printk(KERN_CONT
"(usable)\n");
405 printk(KERN_CONT
"(reserved)\n");
408 printk(KERN_CONT
"(ACPI data)\n");
411 printk(KERN_CONT
"(ACPI NVS)\n");
414 printk(KERN_CONT
"type %u\n", e820
.map
[i
].type
);
421 * Sanitize the BIOS e820 map.
423 * Some e820 responses include overlapping entries. The following
424 * replaces the original e820 map with a new one, removing overlaps.
427 static int __init
sanitize_e820_map(struct e820entry
*biosmap
, char *pnr_map
)
429 struct change_member
{
430 struct e820entry
*pbios
; /* pointer to original bios entry */
431 unsigned long long addr
; /* address for this change point */
433 static struct change_member change_point_list
[2*E820MAX
] __initdata
;
434 static struct change_member
*change_point
[2*E820MAX
] __initdata
;
435 static struct e820entry
*overlap_list
[E820MAX
] __initdata
;
436 static struct e820entry new_bios
[E820MAX
] __initdata
;
437 struct change_member
*change_tmp
;
438 unsigned long current_type
, last_type
;
439 unsigned long long last_addr
;
440 int chgidx
, still_changing
;
443 int old_nr
, new_nr
, chg_nr
;
447 Visually we're performing the following
448 (1,2,3,4 = memory types)...
450 Sample memory map (w/overlaps):
451 ____22__________________
452 ______________________4_
453 ____1111________________
454 _44_____________________
455 11111111________________
456 ____________________33__
457 ___________44___________
458 __________33333_________
459 ______________22________
460 ___________________2222_
461 _________111111111______
462 _____________________11_
463 _________________4______
465 Sanitized equivalent (no overlap):
466 1_______________________
467 _44_____________________
468 ___1____________________
469 ____22__________________
470 ______11________________
471 _________1______________
472 __________3_____________
473 ___________44___________
474 _____________33_________
475 _______________2________
476 ________________1_______
477 _________________4______
478 ___________________2____
479 ____________________33__
480 ______________________4_
483 /* if there's only one memory region, don't bother */
489 /* bail out if we find any unreasonable addresses in bios map */
490 for (i
= 0; i
< old_nr
; i
++)
491 if (biosmap
[i
].addr
+ biosmap
[i
].size
< biosmap
[i
].addr
)
494 /* create pointers for initial change-point information (for sorting) */
495 for (i
= 0; i
< 2 * old_nr
; i
++)
496 change_point
[i
] = &change_point_list
[i
];
498 /* record all known change-points (starting and ending addresses),
499 omitting those that are for empty memory regions */
501 for (i
= 0; i
< old_nr
; i
++) {
502 if (biosmap
[i
].size
!= 0) {
503 change_point
[chgidx
]->addr
= biosmap
[i
].addr
;
504 change_point
[chgidx
++]->pbios
= &biosmap
[i
];
505 change_point
[chgidx
]->addr
= biosmap
[i
].addr
+
507 change_point
[chgidx
++]->pbios
= &biosmap
[i
];
512 /* sort change-point list by memory addresses (low -> high) */
514 while (still_changing
) {
516 for (i
= 1; i
< chg_nr
; i
++) {
517 unsigned long long curaddr
, lastaddr
;
518 unsigned long long curpbaddr
, lastpbaddr
;
520 curaddr
= change_point
[i
]->addr
;
521 lastaddr
= change_point
[i
- 1]->addr
;
522 curpbaddr
= change_point
[i
]->pbios
->addr
;
523 lastpbaddr
= change_point
[i
- 1]->pbios
->addr
;
526 * swap entries, when:
528 * curaddr > lastaddr or
529 * curaddr == lastaddr and curaddr == curpbaddr and
530 * lastaddr != lastpbaddr
532 if (curaddr
< lastaddr
||
533 (curaddr
== lastaddr
&& curaddr
== curpbaddr
&&
534 lastaddr
!= lastpbaddr
)) {
535 change_tmp
= change_point
[i
];
536 change_point
[i
] = change_point
[i
-1];
537 change_point
[i
-1] = change_tmp
;
543 /* create a new bios memory map, removing overlaps */
544 overlap_entries
= 0; /* number of entries in the overlap table */
545 new_bios_entry
= 0; /* index for creating new bios map entries */
546 last_type
= 0; /* start with undefined memory type */
547 last_addr
= 0; /* start with 0 as last starting address */
549 /* loop through change-points, determining affect on the new bios map */
550 for (chgidx
= 0; chgidx
< chg_nr
; chgidx
++) {
551 /* keep track of all overlapping bios entries */
552 if (change_point
[chgidx
]->addr
==
553 change_point
[chgidx
]->pbios
->addr
) {
555 * add map entry to overlap list (> 1 entry
556 * implies an overlap)
558 overlap_list
[overlap_entries
++] =
559 change_point
[chgidx
]->pbios
;
562 * remove entry from list (order independent,
565 for (i
= 0; i
< overlap_entries
; i
++) {
566 if (overlap_list
[i
] ==
567 change_point
[chgidx
]->pbios
)
569 overlap_list
[overlap_entries
-1];
574 * if there are overlapping entries, decide which
575 * "type" to use (larger value takes precedence --
576 * 1=usable, 2,3,4,4+=unusable)
579 for (i
= 0; i
< overlap_entries
; i
++)
580 if (overlap_list
[i
]->type
> current_type
)
581 current_type
= overlap_list
[i
]->type
;
583 * continue building up new bios map based on this
586 if (current_type
!= last_type
) {
587 if (last_type
!= 0) {
588 new_bios
[new_bios_entry
].size
=
589 change_point
[chgidx
]->addr
- last_addr
;
591 * move forward only if the new size
594 if (new_bios
[new_bios_entry
].size
!= 0)
596 * no more space left for new
599 if (++new_bios_entry
>= E820MAX
)
602 if (current_type
!= 0) {
603 new_bios
[new_bios_entry
].addr
=
604 change_point
[chgidx
]->addr
;
605 new_bios
[new_bios_entry
].type
= current_type
;
606 last_addr
= change_point
[chgidx
]->addr
;
608 last_type
= current_type
;
611 /* retain count for new bios entries */
612 new_nr
= new_bios_entry
;
614 /* copy new bios mapping into original location */
615 memcpy(biosmap
, new_bios
, new_nr
* sizeof(struct e820entry
));
622 * Copy the BIOS e820 map into a safe place.
624 * Sanity-check it while we're at it..
626 * If we're lucky and live on a modern system, the setup code
627 * will have given us a memory map that we can use to properly
628 * set up memory. If we aren't, we'll fake a memory map.
630 static int __init
copy_e820_map(struct e820entry
*biosmap
, int nr_map
)
632 /* Only one memory region (or negative)? Ignore it */
637 unsigned long start
= biosmap
->addr
;
638 unsigned long size
= biosmap
->size
;
639 unsigned long end
= start
+ size
;
640 unsigned long type
= biosmap
->type
;
642 /* Overflow in 64 bits? Ignore the memory map. */
646 add_memory_region(start
, size
, type
);
647 } while (biosmap
++, --nr_map
);
651 static void early_panic(char *msg
)
657 /* We're not void only for x86 32-bit compat */
658 char * __init
machine_specific_memory_setup(void)
660 char *who
= "BIOS-e820";
662 * Try to copy the BIOS-supplied E820-map.
664 * Otherwise fake a memory map; one section from 0k->640k,
665 * the next section from 1mb->appropriate_mem_k
667 sanitize_e820_map(boot_params
.e820_map
, &boot_params
.e820_entries
);
668 if (copy_e820_map(boot_params
.e820_map
, boot_params
.e820_entries
) < 0)
669 early_panic("Cannot find a valid memory map");
670 printk(KERN_INFO
"BIOS-provided physical RAM map:\n");
673 /* In case someone cares... */
677 static int __init
parse_memopt(char *p
)
681 end_user_pfn
= memparse(p
, &p
);
682 end_user_pfn
>>= PAGE_SHIFT
;
685 early_param("mem", parse_memopt
);
687 static int userdef __initdata
;
689 static int __init
parse_memmap_opt(char *p
)
692 unsigned long long start_at
, mem_size
;
694 if (!strcmp(p
, "exactmap")) {
695 #ifdef CONFIG_CRASH_DUMP
697 * If we are doing a crash dump, we still need to know
698 * the real mem size before original memory map is
701 e820_register_active_regions(0, 0, -1UL);
702 saved_max_pfn
= e820_end_of_ram();
703 remove_all_active_ranges();
712 mem_size
= memparse(p
, &p
);
718 start_at
= memparse(p
+1, &p
);
719 add_memory_region(start_at
, mem_size
, E820_RAM
);
720 } else if (*p
== '#') {
721 start_at
= memparse(p
+1, &p
);
722 add_memory_region(start_at
, mem_size
, E820_ACPI
);
723 } else if (*p
== '$') {
724 start_at
= memparse(p
+1, &p
);
725 add_memory_region(start_at
, mem_size
, E820_RESERVED
);
727 end_user_pfn
= (mem_size
>> PAGE_SHIFT
);
729 return *p
== '\0' ? 0 : -EINVAL
;
731 early_param("memmap", parse_memmap_opt
);
733 void __init
finish_e820_parsing(void)
736 char nr
= e820
.nr_map
;
738 if (sanitize_e820_map(e820
.map
, &nr
) < 0)
739 early_panic("Invalid user supplied memory map");
742 printk(KERN_INFO
"user-defined physical RAM map:\n");
743 e820_print_map("user");
747 void __init
update_e820(void)
751 nr_map
= e820
.nr_map
;
752 if (sanitize_e820_map(e820
.map
, &nr_map
))
754 e820
.nr_map
= nr_map
;
755 printk(KERN_INFO
"modified physical RAM map:\n");
756 e820_print_map("modified");
759 unsigned long pci_mem_start
= 0xaeedbabe;
760 EXPORT_SYMBOL(pci_mem_start
);
763 * Search for the biggest gap in the low 32 bits of the e820
764 * memory space. We pass this space to PCI to assign MMIO resources
765 * for hotplug or unconfigured devices in.
766 * Hopefully the BIOS let enough space left.
768 __init
void e820_setup_gap(void)
770 unsigned long gapstart
, gapsize
, round
;
775 last
= 0x100000000ull
;
776 gapstart
= 0x10000000;
780 unsigned long long start
= e820
.map
[i
].addr
;
781 unsigned long long end
= start
+ e820
.map
[i
].size
;
784 * Since "last" is at most 4GB, we know we'll
785 * fit in 32 bits if this condition is true
788 unsigned long gap
= last
- end
;
801 gapstart
= (end_pfn
<< PAGE_SHIFT
) + 1024*1024;
802 printk(KERN_ERR
"PCI: Warning: Cannot find a gap in the 32bit "
804 KERN_ERR
"PCI: Unassigned devices with 32bit resource "
805 "registers may break!\n");
809 * See how much we want to round up: start off with
810 * rounding to the next 1MB area.
813 while ((gapsize
>> 4) > round
)
815 /* Fun with two's complement */
816 pci_mem_start
= (gapstart
+ round
) & -round
;
819 "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
820 pci_mem_start
, gapstart
, gapsize
);
823 int __init
arch_get_ram_range(int slot
, u64
*addr
, u64
*size
)
827 if (slot
< 0 || slot
>= e820
.nr_map
)
829 for (i
= slot
; i
< e820
.nr_map
; i
++) {
830 if (e820
.map
[i
].type
!= E820_RAM
)
834 if (i
== e820
.nr_map
|| e820
.map
[i
].addr
> (max_pfn
<< PAGE_SHIFT
))
836 *addr
= e820
.map
[i
].addr
;
837 *size
= min_t(u64
, e820
.map
[i
].size
+ e820
.map
[i
].addr
,
838 max_pfn
<< PAGE_SHIFT
) - *addr
;