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/pfn.h>
21 #include <linux/suspend.h>
22 #include <linux/firmware-map.h>
24 #include <asm/pgtable.h>
27 #include <asm/proto.h>
28 #include <asm/setup.h>
29 #include <asm/trampoline.h>
32 * The e820 map is the map that gets modified e.g. with command line parameters
33 * and that is also registered with modifications in the kernel resource tree
34 * with the iomem_resource as parent.
36 * The e820_saved is directly saved after the BIOS-provided memory map is
37 * copied. It doesn't get modified afterwards. It's registered for the
38 * /sys/firmware/memmap interface.
40 * That memory map is not modified and is used as base for kexec. The kexec'd
41 * kernel should get the same memory map as the firmware provides. Then the
42 * user can e.g. boot the original kernel with mem=1G while still booting the
43 * next kernel with full memory.
46 struct e820map e820_saved;
48 /* For PCI or other memory-mapped resources */
49 unsigned long pci_mem_start = 0xaeedbabe;
51 EXPORT_SYMBOL(pci_mem_start);
55 * This function checks if any part of the range <start,end> is mapped
59 e820_any_mapped(u64 start, u64 end, unsigned type)
63 for (i = 0; i < e820.nr_map; i++) {
64 struct e820entry *ei = &e820.map[i];
66 if (type && ei->type != type)
68 if (ei->addr >= end || ei->addr + ei->size <= start)
74 EXPORT_SYMBOL_GPL(e820_any_mapped);
77 * This function checks if the entire range <start,end> is mapped with type.
79 * Note: this function only works correct if the e820 table is sorted and
80 * not-overlapping, which is the case
82 int __init e820_all_mapped(u64 start, u64 end, unsigned type)
86 for (i = 0; i < e820.nr_map; i++) {
87 struct e820entry *ei = &e820.map[i];
89 if (type && ei->type != type)
91 /* is the region (part) in overlap with the current region ?*/
92 if (ei->addr >= end || ei->addr + ei->size <= start)
95 /* if the region is at the beginning of <start,end> we move
96 * start to the end of the region since it's ok until there
98 if (ei->addr <= start)
99 start = ei->addr + ei->size;
101 * if start is now at or beyond end, we're done, full
111 * Add a memory region to the kernel e820 map.
113 void __init e820_add_region(u64 start, u64 size, int type)
117 if (x == ARRAY_SIZE(e820.map)) {
118 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
122 e820.map[x].addr = start;
123 e820.map[x].size = size;
124 e820.map[x].type = type;
128 void __init e820_print_map(char *who)
132 for (i = 0; i < e820.nr_map; i++) {
133 printk(KERN_INFO " %s: %016Lx - %016Lx ", who,
134 (unsigned long long) e820.map[i].addr,
136 (e820.map[i].addr + e820.map[i].size));
137 switch (e820.map[i].type) {
139 case E820_RESERVED_KERN:
140 printk(KERN_CONT "(usable)\n");
143 printk(KERN_CONT "(reserved)\n");
146 printk(KERN_CONT "(ACPI data)\n");
149 printk(KERN_CONT "(ACPI NVS)\n");
152 printk(KERN_CONT "type %u\n", e820.map[i].type);
159 * Sanitize the BIOS e820 map.
161 * Some e820 responses include overlapping entries. The following
162 * replaces the original e820 map with a new one, removing overlaps,
163 * and resolving conflicting memory types in favor of highest
166 * The input parameter biosmap points to an array of 'struct
167 * e820entry' which on entry has elements in the range [0, *pnr_map)
168 * valid, and which has space for up to max_nr_map entries.
169 * On return, the resulting sanitized e820 map entries will be in
170 * overwritten in the same location, starting at biosmap.
172 * The integer pointed to by pnr_map must be valid on entry (the
173 * current number of valid entries located at biosmap) and will
174 * be updated on return, with the new number of valid entries
175 * (something no more than max_nr_map.)
177 * The return value from sanitize_e820_map() is zero if it
178 * successfully 'sanitized' the map entries passed in, and is -1
179 * if it did nothing, which can happen if either of (1) it was
180 * only passed one map entry, or (2) any of the input map entries
181 * were invalid (start + size < start, meaning that the size was
182 * so big the described memory range wrapped around through zero.)
184 * Visually we're performing the following
185 * (1,2,3,4 = memory types)...
187 * Sample memory map (w/overlaps):
188 * ____22__________________
189 * ______________________4_
190 * ____1111________________
191 * _44_____________________
192 * 11111111________________
193 * ____________________33__
194 * ___________44___________
195 * __________33333_________
196 * ______________22________
197 * ___________________2222_
198 * _________111111111______
199 * _____________________11_
200 * _________________4______
202 * Sanitized equivalent (no overlap):
203 * 1_______________________
204 * _44_____________________
205 * ___1____________________
206 * ____22__________________
207 * ______11________________
208 * _________1______________
209 * __________3_____________
210 * ___________44___________
211 * _____________33_________
212 * _______________2________
213 * ________________1_______
214 * _________________4______
215 * ___________________2____
216 * ____________________33__
217 * ______________________4_
220 int __init sanitize_e820_map(struct e820entry *biosmap, int max_nr_map,
223 struct change_member {
224 struct e820entry *pbios; /* pointer to original bios entry */
225 unsigned long long addr; /* address for this change point */
227 static struct change_member change_point_list[2*E820_X_MAX] __initdata;
228 static struct change_member *change_point[2*E820_X_MAX] __initdata;
229 static struct e820entry *overlap_list[E820_X_MAX] __initdata;
230 static struct e820entry new_bios[E820_X_MAX] __initdata;
231 struct change_member *change_tmp;
232 unsigned long current_type, last_type;
233 unsigned long long last_addr;
234 int chgidx, still_changing;
237 int old_nr, new_nr, chg_nr;
240 /* if there's only one memory region, don't bother */
245 BUG_ON(old_nr > max_nr_map);
247 /* bail out if we find any unreasonable addresses in bios map */
248 for (i = 0; i < old_nr; i++)
249 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
252 /* create pointers for initial change-point information (for sorting) */
253 for (i = 0; i < 2 * old_nr; i++)
254 change_point[i] = &change_point_list[i];
256 /* record all known change-points (starting and ending addresses),
257 omitting those that are for empty memory regions */
259 for (i = 0; i < old_nr; i++) {
260 if (biosmap[i].size != 0) {
261 change_point[chgidx]->addr = biosmap[i].addr;
262 change_point[chgidx++]->pbios = &biosmap[i];
263 change_point[chgidx]->addr = biosmap[i].addr +
265 change_point[chgidx++]->pbios = &biosmap[i];
270 /* sort change-point list by memory addresses (low -> high) */
272 while (still_changing) {
274 for (i = 1; i < chg_nr; i++) {
275 unsigned long long curaddr, lastaddr;
276 unsigned long long curpbaddr, lastpbaddr;
278 curaddr = change_point[i]->addr;
279 lastaddr = change_point[i - 1]->addr;
280 curpbaddr = change_point[i]->pbios->addr;
281 lastpbaddr = change_point[i - 1]->pbios->addr;
284 * swap entries, when:
286 * curaddr > lastaddr or
287 * curaddr == lastaddr and curaddr == curpbaddr and
288 * lastaddr != lastpbaddr
290 if (curaddr < lastaddr ||
291 (curaddr == lastaddr && curaddr == curpbaddr &&
292 lastaddr != lastpbaddr)) {
293 change_tmp = change_point[i];
294 change_point[i] = change_point[i-1];
295 change_point[i-1] = change_tmp;
301 /* create a new bios memory map, removing overlaps */
302 overlap_entries = 0; /* number of entries in the overlap table */
303 new_bios_entry = 0; /* index for creating new bios map entries */
304 last_type = 0; /* start with undefined memory type */
305 last_addr = 0; /* start with 0 as last starting address */
307 /* loop through change-points, determining affect on the new bios map */
308 for (chgidx = 0; chgidx < chg_nr; chgidx++) {
309 /* keep track of all overlapping bios entries */
310 if (change_point[chgidx]->addr ==
311 change_point[chgidx]->pbios->addr) {
313 * add map entry to overlap list (> 1 entry
314 * implies an overlap)
316 overlap_list[overlap_entries++] =
317 change_point[chgidx]->pbios;
320 * remove entry from list (order independent,
323 for (i = 0; i < overlap_entries; i++) {
324 if (overlap_list[i] ==
325 change_point[chgidx]->pbios)
327 overlap_list[overlap_entries-1];
332 * if there are overlapping entries, decide which
333 * "type" to use (larger value takes precedence --
334 * 1=usable, 2,3,4,4+=unusable)
337 for (i = 0; i < overlap_entries; i++)
338 if (overlap_list[i]->type > current_type)
339 current_type = overlap_list[i]->type;
341 * continue building up new bios map based on this
344 if (current_type != last_type) {
345 if (last_type != 0) {
346 new_bios[new_bios_entry].size =
347 change_point[chgidx]->addr - last_addr;
349 * move forward only if the new size
352 if (new_bios[new_bios_entry].size != 0)
354 * no more space left for new
357 if (++new_bios_entry >= max_nr_map)
360 if (current_type != 0) {
361 new_bios[new_bios_entry].addr =
362 change_point[chgidx]->addr;
363 new_bios[new_bios_entry].type = current_type;
364 last_addr = change_point[chgidx]->addr;
366 last_type = current_type;
369 /* retain count for new bios entries */
370 new_nr = new_bios_entry;
372 /* copy new bios mapping into original location */
373 memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
379 static int __init __append_e820_map(struct e820entry *biosmap, int nr_map)
382 u64 start = biosmap->addr;
383 u64 size = biosmap->size;
384 u64 end = start + size;
385 u32 type = biosmap->type;
387 /* Overflow in 64 bits? Ignore the memory map. */
391 e820_add_region(start, size, type);
400 * Copy the BIOS e820 map into a safe place.
402 * Sanity-check it while we're at it..
404 * If we're lucky and live on a modern system, the setup code
405 * will have given us a memory map that we can use to properly
406 * set up memory. If we aren't, we'll fake a memory map.
408 static int __init append_e820_map(struct e820entry *biosmap, int nr_map)
410 /* Only one memory region (or negative)? Ignore it */
414 return __append_e820_map(biosmap, nr_map);
417 u64 __init e820_update_range(u64 start, u64 size, unsigned old_type,
421 u64 real_updated_size = 0;
423 BUG_ON(old_type == new_type);
425 if (size > (ULLONG_MAX - start))
426 size = ULLONG_MAX - start;
428 for (i = 0; i < e820.nr_map; i++) {
429 struct e820entry *ei = &e820.map[i];
430 u64 final_start, final_end;
431 if (ei->type != old_type)
433 /* totally covered? */
434 if (ei->addr >= start &&
435 (ei->addr + ei->size) <= (start + size)) {
437 real_updated_size += ei->size;
440 /* partially covered */
441 final_start = max(start, ei->addr);
442 final_end = min(start + size, ei->addr + ei->size);
443 if (final_start >= final_end)
445 e820_add_region(final_start, final_end - final_start,
447 real_updated_size += final_end - final_start;
449 ei->size -= final_end - final_start;
450 if (ei->addr < final_start)
452 ei->addr = final_end;
454 return real_updated_size;
457 /* make e820 not cover the range */
458 u64 __init e820_remove_range(u64 start, u64 size, unsigned old_type,
462 u64 real_removed_size = 0;
464 if (size > (ULLONG_MAX - start))
465 size = ULLONG_MAX - start;
467 for (i = 0; i < e820.nr_map; i++) {
468 struct e820entry *ei = &e820.map[i];
469 u64 final_start, final_end;
471 if (checktype && ei->type != old_type)
473 /* totally covered? */
474 if (ei->addr >= start &&
475 (ei->addr + ei->size) <= (start + size)) {
476 real_removed_size += ei->size;
477 memset(ei, 0, sizeof(struct e820entry));
480 /* partially covered */
481 final_start = max(start, ei->addr);
482 final_end = min(start + size, ei->addr + ei->size);
483 if (final_start >= final_end)
485 real_removed_size += final_end - final_start;
487 ei->size -= final_end - final_start;
488 if (ei->addr < final_start)
490 ei->addr = final_end;
492 return real_removed_size;
495 void __init update_e820(void)
499 nr_map = e820.nr_map;
500 if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr_map))
502 e820.nr_map = nr_map;
503 printk(KERN_INFO "modified physical RAM map:\n");
504 e820_print_map("modified");
506 #define MAX_GAP_END 0x100000000ull
508 * Search for a gap in the e820 memory space from start_addr to end_addr.
510 __init int e820_search_gap(unsigned long *gapstart, unsigned long *gapsize,
511 unsigned long start_addr, unsigned long long end_addr)
513 unsigned long long last;
517 last = (end_addr && end_addr < MAX_GAP_END) ? end_addr : MAX_GAP_END;
520 unsigned long long start = e820.map[i].addr;
521 unsigned long long end = start + e820.map[i].size;
523 if (end < start_addr)
527 * Since "last" is at most 4GB, we know we'll
528 * fit in 32 bits if this condition is true
531 unsigned long gap = last - end;
533 if (gap >= *gapsize) {
546 * Search for the biggest gap in the low 32 bits of the e820
547 * memory space. We pass this space to PCI to assign MMIO resources
548 * for hotplug or unconfigured devices in.
549 * Hopefully the BIOS let enough space left.
551 __init void e820_setup_gap(void)
553 unsigned long gapstart, gapsize, round;
556 gapstart = 0x10000000;
558 found = e820_search_gap(&gapstart, &gapsize, 0, MAX_GAP_END);
562 gapstart = (max_pfn << PAGE_SHIFT) + 1024*1024;
563 printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit "
565 KERN_ERR "PCI: Unassigned devices with 32bit resource "
566 "registers may break!\n");
571 * See how much we want to round up: start off with
572 * rounding to the next 1MB area.
575 while ((gapsize >> 4) > round)
577 /* Fun with two's complement */
578 pci_mem_start = (gapstart + round) & -round;
581 "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
582 pci_mem_start, gapstart, gapsize);
586 * Because of the size limitation of struct boot_params, only first
587 * 128 E820 memory entries are passed to kernel via
588 * boot_params.e820_map, others are passed via SETUP_E820_EXT node of
589 * linked list of struct setup_data, which is parsed here.
591 void __init parse_e820_ext(struct setup_data *sdata, unsigned long pa_data)
595 struct e820entry *extmap;
597 entries = sdata->len / sizeof(struct e820entry);
598 map_len = sdata->len + sizeof(struct setup_data);
599 if (map_len > PAGE_SIZE)
600 sdata = early_ioremap(pa_data, map_len);
601 extmap = (struct e820entry *)(sdata->data);
602 __append_e820_map(extmap, entries);
603 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
604 if (map_len > PAGE_SIZE)
605 early_iounmap(sdata, map_len);
606 printk(KERN_INFO "extended physical RAM map:\n");
607 e820_print_map("extended");
610 #if defined(CONFIG_X86_64) || \
611 (defined(CONFIG_X86_32) && defined(CONFIG_HIBERNATION))
613 * Find the ranges of physical addresses that do not correspond to
614 * e820 RAM areas and mark the corresponding pages as nosave for
615 * hibernation (32 bit) or software suspend and suspend to RAM (64 bit).
617 * This function requires the e820 map to be sorted and without any
618 * overlapping entries and assumes the first e820 area to be RAM.
620 void __init e820_mark_nosave_regions(unsigned long limit_pfn)
625 pfn = PFN_DOWN(e820.map[0].addr + e820.map[0].size);
626 for (i = 1; i < e820.nr_map; i++) {
627 struct e820entry *ei = &e820.map[i];
629 if (pfn < PFN_UP(ei->addr))
630 register_nosave_region(pfn, PFN_UP(ei->addr));
632 pfn = PFN_DOWN(ei->addr + ei->size);
633 if (ei->type != E820_RAM && ei->type != E820_RESERVED_KERN)
634 register_nosave_region(PFN_UP(ei->addr), pfn);
636 if (pfn >= limit_pfn)
643 * Early reserved memory areas.
645 #define MAX_EARLY_RES 20
652 static struct early_res early_res[MAX_EARLY_RES] __initdata = {
653 { 0, PAGE_SIZE, "BIOS data page" }, /* BIOS data page */
654 #if defined(CONFIG_X86_64) && defined(CONFIG_X86_TRAMPOLINE)
655 { TRAMPOLINE_BASE, TRAMPOLINE_BASE + 2 * PAGE_SIZE, "TRAMPOLINE" },
657 #if defined(CONFIG_X86_32) && defined(CONFIG_SMP)
659 * But first pinch a few for the stack/trampoline stuff
660 * FIXME: Don't need the extra page at 4K, but need to fix
661 * trampoline before removing it. (see the GDT stuff)
663 { PAGE_SIZE, PAGE_SIZE + PAGE_SIZE, "EX TRAMPOLINE" },
665 * Has to be in very low memory so we can execute
668 { TRAMPOLINE_BASE, TRAMPOLINE_BASE + PAGE_SIZE, "TRAMPOLINE" },
673 static int __init find_overlapped_early(u64 start, u64 end)
678 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
680 if (end > r->start && start < r->end)
688 * Drop the i-th range from the early reservation map,
689 * by copying any higher ranges down one over it, and
690 * clearing what had been the last slot.
692 static void __init drop_range(int i)
696 for (j = i + 1; j < MAX_EARLY_RES && early_res[j].end; j++)
699 memmove(&early_res[i], &early_res[i + 1],
700 (j - 1 - i) * sizeof(struct early_res));
702 early_res[j - 1].end = 0;
706 * Split any existing ranges that:
707 * 1) are marked 'overlap_ok', and
708 * 2) overlap with the stated range [start, end)
709 * into whatever portion (if any) of the existing range is entirely
710 * below or entirely above the stated range. Drop the portion
711 * of the existing range that overlaps with the stated range,
712 * which will allow the caller of this routine to then add that
713 * stated range without conflicting with any existing range.
715 static void __init drop_overlaps_that_are_ok(u64 start, u64 end)
719 u64 lower_start, lower_end;
720 u64 upper_start, upper_end;
723 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
726 /* Continue past non-overlapping ranges */
727 if (end <= r->start || start >= r->end)
731 * Leave non-ok overlaps as is; let caller
732 * panic "Overlapping early reservations"
733 * when it hits this overlap.
739 * We have an ok overlap. We will drop it from the early
740 * reservation map, and add back in any non-overlapping
741 * portions (lower or upper) as separate, overlap_ok,
742 * non-overlapping ranges.
745 /* 1. Note any non-overlapping (lower or upper) ranges. */
746 strncpy(name, r->name, sizeof(name) - 1);
748 lower_start = lower_end = 0;
749 upper_start = upper_end = 0;
750 if (r->start < start) {
751 lower_start = r->start;
759 /* 2. Drop the original ok overlapping range */
762 i--; /* resume for-loop on copied down entry */
764 /* 3. Add back in any non-overlapping ranges. */
766 reserve_early_overlap_ok(lower_start, lower_end, name);
768 reserve_early_overlap_ok(upper_start, upper_end, name);
772 static void __init __reserve_early(u64 start, u64 end, char *name,
778 i = find_overlapped_early(start, end);
779 if (i >= MAX_EARLY_RES)
780 panic("Too many early reservations");
783 panic("Overlapping early reservations "
784 "%llx-%llx %s to %llx-%llx %s\n",
785 start, end - 1, name?name:"", r->start,
786 r->end - 1, r->name);
789 r->overlap_ok = overlap_ok;
791 strncpy(r->name, name, sizeof(r->name) - 1);
795 * A few early reservtations come here.
797 * The 'overlap_ok' in the name of this routine does -not- mean it
798 * is ok for these reservations to overlap an earlier reservation.
799 * Rather it means that it is ok for subsequent reservations to
802 * Use this entry point to reserve early ranges when you are doing
803 * so out of "Paranoia", reserving perhaps more memory than you need,
804 * just in case, and don't mind a subsequent overlapping reservation
805 * that is known to be needed.
807 * The drop_overlaps_that_are_ok() call here isn't really needed.
808 * It would be needed if we had two colliding 'overlap_ok'
809 * reservations, so that the second such would not panic on the
810 * overlap with the first. We don't have any such as of this
811 * writing, but might as well tolerate such if it happens in
814 void __init reserve_early_overlap_ok(u64 start, u64 end, char *name)
816 drop_overlaps_that_are_ok(start, end);
817 __reserve_early(start, end, name, 1);
821 * Most early reservations come here.
823 * We first have drop_overlaps_that_are_ok() drop any pre-existing
824 * 'overlap_ok' ranges, so that we can then reserve this memory
825 * range without risk of panic'ing on an overlapping overlap_ok
828 void __init reserve_early(u64 start, u64 end, char *name)
830 drop_overlaps_that_are_ok(start, end);
831 __reserve_early(start, end, name, 0);
834 void __init free_early(u64 start, u64 end)
839 i = find_overlapped_early(start, end);
841 if (i >= MAX_EARLY_RES || r->end != end || r->start != start)
842 panic("free_early on not reserved area: %llx-%llx!",
848 void __init early_res_to_bootmem(u64 start, u64 end)
851 u64 final_start, final_end;
854 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++)
857 printk(KERN_INFO "(%d early reservations) ==> bootmem\n", count);
858 for (i = 0; i < count; i++) {
859 struct early_res *r = &early_res[i];
860 printk(KERN_INFO " #%d [%010llx - %010llx] %16s", i,
861 r->start, r->end, r->name);
862 final_start = max(start, r->start);
863 final_end = min(end, r->end);
864 if (final_start >= final_end) {
865 printk(KERN_CONT "\n");
868 printk(KERN_CONT " ==> [%010llx - %010llx]\n",
869 final_start, final_end);
870 reserve_bootmem_generic(final_start, final_end - final_start,
875 /* Check for already reserved areas */
876 static inline int __init bad_addr(u64 *addrp, u64 size, u64 align)
883 i = find_overlapped_early(addr, addr + size);
885 if (i < MAX_EARLY_RES && r->end) {
886 *addrp = addr = round_up(r->end, align);
893 /* Check for already reserved areas */
894 static inline int __init bad_addr_size(u64 *addrp, u64 *sizep, u64 align)
897 u64 addr = *addrp, last;
902 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
903 struct early_res *r = &early_res[i];
904 if (last > r->start && addr < r->start) {
905 size = r->start - addr;
909 if (last > r->end && addr < r->end) {
910 addr = round_up(r->end, align);
915 if (last <= r->end && addr >= r->start) {
928 * Find a free area with specified alignment in a specific range.
930 u64 __init find_e820_area(u64 start, u64 end, u64 size, u64 align)
934 for (i = 0; i < e820.nr_map; i++) {
935 struct e820entry *ei = &e820.map[i];
939 if (ei->type != E820_RAM)
941 addr = round_up(ei->addr, align);
942 ei_last = ei->addr + ei->size;
944 addr = round_up(start, align);
947 while (bad_addr(&addr, size, align) && addr+size <= ei_last)
960 * Find next free range after *start
962 u64 __init find_e820_area_size(u64 start, u64 *sizep, u64 align)
966 for (i = 0; i < e820.nr_map; i++) {
967 struct e820entry *ei = &e820.map[i];
971 if (ei->type != E820_RAM)
973 addr = round_up(ei->addr, align);
974 ei_last = ei->addr + ei->size;
976 addr = round_up(start, align);
979 *sizep = ei_last - addr;
980 while (bad_addr_size(&addr, sizep, align) &&
981 addr + *sizep <= ei_last)
983 last = addr + *sizep;
993 * pre allocated 4k and reserved it in e820
995 u64 __init early_reserve_e820(u64 startt, u64 sizet, u64 align)
1002 while (size < sizet)
1003 start = find_e820_area_size(start, &size, align);
1008 addr = round_down(start + size - sizet, align);
1009 e820_update_range(addr, sizet, E820_RAM, E820_RESERVED);
1010 printk(KERN_INFO "update e820 for early_reserve_e820\n");
1016 #ifdef CONFIG_X86_32
1017 # ifdef CONFIG_X86_PAE
1018 # define MAX_ARCH_PFN (1ULL<<(36-PAGE_SHIFT))
1020 # define MAX_ARCH_PFN (1ULL<<(32-PAGE_SHIFT))
1022 #else /* CONFIG_X86_32 */
1023 # define MAX_ARCH_PFN MAXMEM>>PAGE_SHIFT
1027 * Last pfn which the user wants to use.
1029 unsigned long __initdata end_user_pfn = MAX_ARCH_PFN;
1032 * Find the highest page frame number we have available
1034 unsigned long __init e820_end_of_ram(void)
1036 unsigned long last_pfn;
1037 unsigned long max_arch_pfn = MAX_ARCH_PFN;
1039 last_pfn = find_max_pfn_with_active_regions();
1041 if (last_pfn > max_arch_pfn)
1042 last_pfn = max_arch_pfn;
1043 if (last_pfn > end_user_pfn)
1044 last_pfn = end_user_pfn;
1046 printk(KERN_INFO "last_pfn = %#lx max_arch_pfn = %#lx\n",
1047 last_pfn, max_arch_pfn);
1052 * Finds an active region in the address range from start_pfn to last_pfn and
1053 * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
1055 int __init e820_find_active_region(const struct e820entry *ei,
1056 unsigned long start_pfn,
1057 unsigned long last_pfn,
1058 unsigned long *ei_startpfn,
1059 unsigned long *ei_endpfn)
1061 u64 align = PAGE_SIZE;
1063 *ei_startpfn = round_up(ei->addr, align) >> PAGE_SHIFT;
1064 *ei_endpfn = round_down(ei->addr + ei->size, align) >> PAGE_SHIFT;
1066 /* Skip map entries smaller than a page */
1067 if (*ei_startpfn >= *ei_endpfn)
1070 /* Skip if map is outside the node */
1071 if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
1072 *ei_startpfn >= last_pfn)
1075 /* Check for overlaps */
1076 if (*ei_startpfn < start_pfn)
1077 *ei_startpfn = start_pfn;
1078 if (*ei_endpfn > last_pfn)
1079 *ei_endpfn = last_pfn;
1081 /* Obey end_user_pfn to save on memmap */
1082 if (*ei_startpfn >= end_user_pfn)
1084 if (*ei_endpfn > end_user_pfn)
1085 *ei_endpfn = end_user_pfn;
1090 /* Walk the e820 map and register active regions within a node */
1091 void __init e820_register_active_regions(int nid, unsigned long start_pfn,
1092 unsigned long last_pfn)
1094 unsigned long ei_startpfn;
1095 unsigned long ei_endpfn;
1098 for (i = 0; i < e820.nr_map; i++)
1099 if (e820_find_active_region(&e820.map[i],
1100 start_pfn, last_pfn,
1101 &ei_startpfn, &ei_endpfn))
1102 add_active_range(nid, ei_startpfn, ei_endpfn);
1106 * Find the hole size (in bytes) in the memory range.
1107 * @start: starting address of the memory range to scan
1108 * @end: ending address of the memory range to scan
1110 u64 __init e820_hole_size(u64 start, u64 end)
1112 unsigned long start_pfn = start >> PAGE_SHIFT;
1113 unsigned long last_pfn = end >> PAGE_SHIFT;
1114 unsigned long ei_startpfn, ei_endpfn, ram = 0;
1117 for (i = 0; i < e820.nr_map; i++) {
1118 if (e820_find_active_region(&e820.map[i],
1119 start_pfn, last_pfn,
1120 &ei_startpfn, &ei_endpfn))
1121 ram += ei_endpfn - ei_startpfn;
1123 return end - start - ((u64)ram << PAGE_SHIFT);
1126 static void early_panic(char *msg)
1132 /* "mem=nopentium" disables the 4MB page tables. */
1133 static int __init parse_memopt(char *p)
1140 #ifdef CONFIG_X86_32
1141 if (!strcmp(p, "nopentium")) {
1142 setup_clear_cpu_cap(X86_FEATURE_PSE);
1147 mem_size = memparse(p, &p);
1148 end_user_pfn = mem_size>>PAGE_SHIFT;
1149 e820_update_range(mem_size, ULLONG_MAX - mem_size,
1150 E820_RAM, E820_RESERVED);
1154 early_param("mem", parse_memopt);
1156 static int userdef __initdata;
1158 static int __init parse_memmap_opt(char *p)
1161 u64 start_at, mem_size;
1163 if (!strcmp(p, "exactmap")) {
1164 #ifdef CONFIG_CRASH_DUMP
1166 * If we are doing a crash dump, we still need to know
1167 * the real mem size before original memory map is
1170 e820_register_active_regions(0, 0, -1UL);
1171 saved_max_pfn = e820_end_of_ram();
1172 remove_all_active_ranges();
1180 mem_size = memparse(p, &p);
1186 start_at = memparse(p+1, &p);
1187 e820_add_region(start_at, mem_size, E820_RAM);
1188 } else if (*p == '#') {
1189 start_at = memparse(p+1, &p);
1190 e820_add_region(start_at, mem_size, E820_ACPI);
1191 } else if (*p == '$') {
1192 start_at = memparse(p+1, &p);
1193 e820_add_region(start_at, mem_size, E820_RESERVED);
1195 end_user_pfn = (mem_size >> PAGE_SHIFT);
1196 e820_update_range(mem_size, ULLONG_MAX - mem_size,
1197 E820_RAM, E820_RESERVED);
1199 return *p == '\0' ? 0 : -EINVAL;
1201 early_param("memmap", parse_memmap_opt);
1203 void __init finish_e820_parsing(void)
1206 int nr = e820.nr_map;
1208 if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr) < 0)
1209 early_panic("Invalid user supplied memory map");
1212 printk(KERN_INFO "user-defined physical RAM map:\n");
1213 e820_print_map("user");
1217 static inline const char *e820_type_to_string(int e820_type)
1219 switch (e820_type) {
1220 case E820_RESERVED_KERN:
1221 case E820_RAM: return "System RAM";
1222 case E820_ACPI: return "ACPI Tables";
1223 case E820_NVS: return "ACPI Non-volatile Storage";
1224 default: return "reserved";
1229 * Mark e820 reserved areas as busy for the resource manager.
1231 void __init e820_reserve_resources(void)
1234 struct resource *res;
1237 res = alloc_bootmem_low(sizeof(struct resource) * e820.nr_map);
1238 for (i = 0; i < e820.nr_map; i++) {
1239 end = e820.map[i].addr + e820.map[i].size - 1;
1240 #ifndef CONFIG_RESOURCES_64BIT
1241 if (end > 0x100000000ULL) {
1246 res->name = e820_type_to_string(e820.map[i].type);
1247 res->start = e820.map[i].addr;
1250 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
1251 insert_resource(&iomem_resource, res);
1255 for (i = 0; i < e820_saved.nr_map; i++) {
1256 struct e820entry *entry = &e820_saved.map[i];
1257 firmware_map_add_early(entry->addr,
1258 entry->addr + entry->size - 1,
1259 e820_type_to_string(entry->type));
1263 char *__init default_machine_specific_memory_setup(void)
1265 char *who = "BIOS-e820";
1268 * Try to copy the BIOS-supplied E820-map.
1270 * Otherwise fake a memory map; one section from 0k->640k,
1271 * the next section from 1mb->appropriate_mem_k
1273 new_nr = boot_params.e820_entries;
1274 sanitize_e820_map(boot_params.e820_map,
1275 ARRAY_SIZE(boot_params.e820_map),
1277 boot_params.e820_entries = new_nr;
1278 if (append_e820_map(boot_params.e820_map, boot_params.e820_entries)
1282 /* compare results from other methods and take the greater */
1283 if (boot_params.alt_mem_k
1284 < boot_params.screen_info.ext_mem_k) {
1285 mem_size = boot_params.screen_info.ext_mem_k;
1288 mem_size = boot_params.alt_mem_k;
1293 e820_add_region(0, LOWMEMSIZE(), E820_RAM);
1294 e820_add_region(HIGH_MEMORY, mem_size << 10, E820_RAM);
1297 /* In case someone cares... */
1301 char *__init __attribute__((weak)) machine_specific_memory_setup(void)
1303 return default_machine_specific_memory_setup();
1306 /* Overridden in paravirt.c if CONFIG_PARAVIRT */
1307 char * __init __attribute__((weak)) memory_setup(void)
1309 return machine_specific_memory_setup();
1312 void __init setup_memory_map(void)
1316 who = memory_setup();
1317 memcpy(&e820_saved, &e820, sizeof(struct e820map));
1318 printk(KERN_INFO "BIOS-provided physical RAM map:\n");
1319 e820_print_map(who);
1322 #ifdef CONFIG_X86_64
1323 int __init arch_get_ram_range(int slot, u64 *addr, u64 *size)
1327 if (slot < 0 || slot >= e820.nr_map)
1329 for (i = slot; i < e820.nr_map; i++) {
1330 if (e820.map[i].type != E820_RAM)
1334 if (i == e820.nr_map || e820.map[i].addr > (max_pfn << PAGE_SHIFT))
1336 *addr = e820.map[i].addr;
1337 *size = min_t(u64, e820.map[i].size + e820.map[i].addr,
1338 max_pfn << PAGE_SHIFT) - *addr;