perf: Fix unexported generic perf_arch_fetch_caller_regs
[linux-2.6/cjktty.git] / arch / x86 / kernel / e820.c
blob740b440fbd730d056c5b197a9c3da84362cb3b2a
1 /*
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/pfn.h>
16 #include <linux/suspend.h>
17 #include <linux/firmware-map.h>
19 #include <asm/e820.h>
20 #include <asm/proto.h>
21 #include <asm/setup.h>
24 * The e820 map is the map that gets modified e.g. with command line parameters
25 * and that is also registered with modifications in the kernel resource tree
26 * with the iomem_resource as parent.
28 * The e820_saved is directly saved after the BIOS-provided memory map is
29 * copied. It doesn't get modified afterwards. It's registered for the
30 * /sys/firmware/memmap interface.
32 * That memory map is not modified and is used as base for kexec. The kexec'd
33 * kernel should get the same memory map as the firmware provides. Then the
34 * user can e.g. boot the original kernel with mem=1G while still booting the
35 * next kernel with full memory.
37 struct e820map e820;
38 struct e820map e820_saved;
40 /* For PCI or other memory-mapped resources */
41 unsigned long pci_mem_start = 0xaeedbabe;
42 #ifdef CONFIG_PCI
43 EXPORT_SYMBOL(pci_mem_start);
44 #endif
47 * This function checks if any part of the range <start,end> is mapped
48 * with type.
50 int
51 e820_any_mapped(u64 start, u64 end, unsigned type)
53 int i;
55 for (i = 0; i < e820.nr_map; i++) {
56 struct e820entry *ei = &e820.map[i];
58 if (type && ei->type != type)
59 continue;
60 if (ei->addr >= end || ei->addr + ei->size <= start)
61 continue;
62 return 1;
64 return 0;
66 EXPORT_SYMBOL_GPL(e820_any_mapped);
69 * This function checks if the entire range <start,end> is mapped with type.
71 * Note: this function only works correct if the e820 table is sorted and
72 * not-overlapping, which is the case
74 int __init e820_all_mapped(u64 start, u64 end, unsigned type)
76 int i;
78 for (i = 0; i < e820.nr_map; i++) {
79 struct e820entry *ei = &e820.map[i];
81 if (type && ei->type != type)
82 continue;
83 /* is the region (part) in overlap with the current region ?*/
84 if (ei->addr >= end || ei->addr + ei->size <= start)
85 continue;
87 /* if the region is at the beginning of <start,end> we move
88 * start to the end of the region since it's ok until there
90 if (ei->addr <= start)
91 start = ei->addr + ei->size;
93 * if start is now at or beyond end, we're done, full
94 * coverage
96 if (start >= end)
97 return 1;
99 return 0;
103 * Add a memory region to the kernel e820 map.
105 static void __init __e820_add_region(struct e820map *e820x, u64 start, u64 size,
106 int type)
108 int x = e820x->nr_map;
110 if (x >= ARRAY_SIZE(e820x->map)) {
111 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
112 return;
115 e820x->map[x].addr = start;
116 e820x->map[x].size = size;
117 e820x->map[x].type = type;
118 e820x->nr_map++;
121 void __init e820_add_region(u64 start, u64 size, int type)
123 __e820_add_region(&e820, start, size, type);
126 static void __init e820_print_type(u32 type)
128 switch (type) {
129 case E820_RAM:
130 case E820_RESERVED_KERN:
131 printk(KERN_CONT "(usable)");
132 break;
133 case E820_RESERVED:
134 printk(KERN_CONT "(reserved)");
135 break;
136 case E820_ACPI:
137 printk(KERN_CONT "(ACPI data)");
138 break;
139 case E820_NVS:
140 printk(KERN_CONT "(ACPI NVS)");
141 break;
142 case E820_UNUSABLE:
143 printk(KERN_CONT "(unusable)");
144 break;
145 default:
146 printk(KERN_CONT "type %u", type);
147 break;
151 void __init e820_print_map(char *who)
153 int i;
155 for (i = 0; i < e820.nr_map; i++) {
156 printk(KERN_INFO " %s: %016Lx - %016Lx ", who,
157 (unsigned long long) e820.map[i].addr,
158 (unsigned long long)
159 (e820.map[i].addr + e820.map[i].size));
160 e820_print_type(e820.map[i].type);
161 printk(KERN_CONT "\n");
166 * Sanitize the BIOS e820 map.
168 * Some e820 responses include overlapping entries. The following
169 * replaces the original e820 map with a new one, removing overlaps,
170 * and resolving conflicting memory types in favor of highest
171 * numbered type.
173 * The input parameter biosmap points to an array of 'struct
174 * e820entry' which on entry has elements in the range [0, *pnr_map)
175 * valid, and which has space for up to max_nr_map entries.
176 * On return, the resulting sanitized e820 map entries will be in
177 * overwritten in the same location, starting at biosmap.
179 * The integer pointed to by pnr_map must be valid on entry (the
180 * current number of valid entries located at biosmap) and will
181 * be updated on return, with the new number of valid entries
182 * (something no more than max_nr_map.)
184 * The return value from sanitize_e820_map() is zero if it
185 * successfully 'sanitized' the map entries passed in, and is -1
186 * if it did nothing, which can happen if either of (1) it was
187 * only passed one map entry, or (2) any of the input map entries
188 * were invalid (start + size < start, meaning that the size was
189 * so big the described memory range wrapped around through zero.)
191 * Visually we're performing the following
192 * (1,2,3,4 = memory types)...
194 * Sample memory map (w/overlaps):
195 * ____22__________________
196 * ______________________4_
197 * ____1111________________
198 * _44_____________________
199 * 11111111________________
200 * ____________________33__
201 * ___________44___________
202 * __________33333_________
203 * ______________22________
204 * ___________________2222_
205 * _________111111111______
206 * _____________________11_
207 * _________________4______
209 * Sanitized equivalent (no overlap):
210 * 1_______________________
211 * _44_____________________
212 * ___1____________________
213 * ____22__________________
214 * ______11________________
215 * _________1______________
216 * __________3_____________
217 * ___________44___________
218 * _____________33_________
219 * _______________2________
220 * ________________1_______
221 * _________________4______
222 * ___________________2____
223 * ____________________33__
224 * ______________________4_
227 int __init sanitize_e820_map(struct e820entry *biosmap, int max_nr_map,
228 u32 *pnr_map)
230 struct change_member {
231 struct e820entry *pbios; /* pointer to original bios entry */
232 unsigned long long addr; /* address for this change point */
234 static struct change_member change_point_list[2*E820_X_MAX] __initdata;
235 static struct change_member *change_point[2*E820_X_MAX] __initdata;
236 static struct e820entry *overlap_list[E820_X_MAX] __initdata;
237 static struct e820entry new_bios[E820_X_MAX] __initdata;
238 struct change_member *change_tmp;
239 unsigned long current_type, last_type;
240 unsigned long long last_addr;
241 int chgidx, still_changing;
242 int overlap_entries;
243 int new_bios_entry;
244 int old_nr, new_nr, chg_nr;
245 int i;
247 /* if there's only one memory region, don't bother */
248 if (*pnr_map < 2)
249 return -1;
251 old_nr = *pnr_map;
252 BUG_ON(old_nr > max_nr_map);
254 /* bail out if we find any unreasonable addresses in bios map */
255 for (i = 0; i < old_nr; i++)
256 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
257 return -1;
259 /* create pointers for initial change-point information (for sorting) */
260 for (i = 0; i < 2 * old_nr; i++)
261 change_point[i] = &change_point_list[i];
263 /* record all known change-points (starting and ending addresses),
264 omitting those that are for empty memory regions */
265 chgidx = 0;
266 for (i = 0; i < old_nr; i++) {
267 if (biosmap[i].size != 0) {
268 change_point[chgidx]->addr = biosmap[i].addr;
269 change_point[chgidx++]->pbios = &biosmap[i];
270 change_point[chgidx]->addr = biosmap[i].addr +
271 biosmap[i].size;
272 change_point[chgidx++]->pbios = &biosmap[i];
275 chg_nr = chgidx;
277 /* sort change-point list by memory addresses (low -> high) */
278 still_changing = 1;
279 while (still_changing) {
280 still_changing = 0;
281 for (i = 1; i < chg_nr; i++) {
282 unsigned long long curaddr, lastaddr;
283 unsigned long long curpbaddr, lastpbaddr;
285 curaddr = change_point[i]->addr;
286 lastaddr = change_point[i - 1]->addr;
287 curpbaddr = change_point[i]->pbios->addr;
288 lastpbaddr = change_point[i - 1]->pbios->addr;
291 * swap entries, when:
293 * curaddr > lastaddr or
294 * curaddr == lastaddr and curaddr == curpbaddr and
295 * lastaddr != lastpbaddr
297 if (curaddr < lastaddr ||
298 (curaddr == lastaddr && curaddr == curpbaddr &&
299 lastaddr != lastpbaddr)) {
300 change_tmp = change_point[i];
301 change_point[i] = change_point[i-1];
302 change_point[i-1] = change_tmp;
303 still_changing = 1;
308 /* create a new bios memory map, removing overlaps */
309 overlap_entries = 0; /* number of entries in the overlap table */
310 new_bios_entry = 0; /* index for creating new bios map entries */
311 last_type = 0; /* start with undefined memory type */
312 last_addr = 0; /* start with 0 as last starting address */
314 /* loop through change-points, determining affect on the new bios map */
315 for (chgidx = 0; chgidx < chg_nr; chgidx++) {
316 /* keep track of all overlapping bios entries */
317 if (change_point[chgidx]->addr ==
318 change_point[chgidx]->pbios->addr) {
320 * add map entry to overlap list (> 1 entry
321 * implies an overlap)
323 overlap_list[overlap_entries++] =
324 change_point[chgidx]->pbios;
325 } else {
327 * remove entry from list (order independent,
328 * so swap with last)
330 for (i = 0; i < overlap_entries; i++) {
331 if (overlap_list[i] ==
332 change_point[chgidx]->pbios)
333 overlap_list[i] =
334 overlap_list[overlap_entries-1];
336 overlap_entries--;
339 * if there are overlapping entries, decide which
340 * "type" to use (larger value takes precedence --
341 * 1=usable, 2,3,4,4+=unusable)
343 current_type = 0;
344 for (i = 0; i < overlap_entries; i++)
345 if (overlap_list[i]->type > current_type)
346 current_type = overlap_list[i]->type;
348 * continue building up new bios map based on this
349 * information
351 if (current_type != last_type) {
352 if (last_type != 0) {
353 new_bios[new_bios_entry].size =
354 change_point[chgidx]->addr - last_addr;
356 * move forward only if the new size
357 * was non-zero
359 if (new_bios[new_bios_entry].size != 0)
361 * no more space left for new
362 * bios entries ?
364 if (++new_bios_entry >= max_nr_map)
365 break;
367 if (current_type != 0) {
368 new_bios[new_bios_entry].addr =
369 change_point[chgidx]->addr;
370 new_bios[new_bios_entry].type = current_type;
371 last_addr = change_point[chgidx]->addr;
373 last_type = current_type;
376 /* retain count for new bios entries */
377 new_nr = new_bios_entry;
379 /* copy new bios mapping into original location */
380 memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
381 *pnr_map = new_nr;
383 return 0;
386 static int __init __append_e820_map(struct e820entry *biosmap, int nr_map)
388 while (nr_map) {
389 u64 start = biosmap->addr;
390 u64 size = biosmap->size;
391 u64 end = start + size;
392 u32 type = biosmap->type;
394 /* Overflow in 64 bits? Ignore the memory map. */
395 if (start > end)
396 return -1;
398 e820_add_region(start, size, type);
400 biosmap++;
401 nr_map--;
403 return 0;
407 * Copy the BIOS e820 map into a safe place.
409 * Sanity-check it while we're at it..
411 * If we're lucky and live on a modern system, the setup code
412 * will have given us a memory map that we can use to properly
413 * set up memory. If we aren't, we'll fake a memory map.
415 static int __init append_e820_map(struct e820entry *biosmap, int nr_map)
417 /* Only one memory region (or negative)? Ignore it */
418 if (nr_map < 2)
419 return -1;
421 return __append_e820_map(biosmap, nr_map);
424 static u64 __init __e820_update_range(struct e820map *e820x, u64 start,
425 u64 size, unsigned old_type,
426 unsigned new_type)
428 u64 end;
429 unsigned int i;
430 u64 real_updated_size = 0;
432 BUG_ON(old_type == new_type);
434 if (size > (ULLONG_MAX - start))
435 size = ULLONG_MAX - start;
437 end = start + size;
438 printk(KERN_DEBUG "e820 update range: %016Lx - %016Lx ",
439 (unsigned long long) start,
440 (unsigned long long) end);
441 e820_print_type(old_type);
442 printk(KERN_CONT " ==> ");
443 e820_print_type(new_type);
444 printk(KERN_CONT "\n");
446 for (i = 0; i < e820x->nr_map; i++) {
447 struct e820entry *ei = &e820x->map[i];
448 u64 final_start, final_end;
449 u64 ei_end;
451 if (ei->type != old_type)
452 continue;
454 ei_end = ei->addr + ei->size;
455 /* totally covered by new range? */
456 if (ei->addr >= start && ei_end <= end) {
457 ei->type = new_type;
458 real_updated_size += ei->size;
459 continue;
462 /* new range is totally covered? */
463 if (ei->addr < start && ei_end > end) {
464 __e820_add_region(e820x, start, size, new_type);
465 __e820_add_region(e820x, end, ei_end - end, ei->type);
466 ei->size = start - ei->addr;
467 real_updated_size += size;
468 continue;
471 /* partially covered */
472 final_start = max(start, ei->addr);
473 final_end = min(end, ei_end);
474 if (final_start >= final_end)
475 continue;
477 __e820_add_region(e820x, final_start, final_end - final_start,
478 new_type);
480 real_updated_size += final_end - final_start;
483 * left range could be head or tail, so need to update
484 * size at first.
486 ei->size -= final_end - final_start;
487 if (ei->addr < final_start)
488 continue;
489 ei->addr = final_end;
491 return real_updated_size;
494 u64 __init e820_update_range(u64 start, u64 size, unsigned old_type,
495 unsigned new_type)
497 return __e820_update_range(&e820, start, size, old_type, new_type);
500 static u64 __init e820_update_range_saved(u64 start, u64 size,
501 unsigned old_type, unsigned new_type)
503 return __e820_update_range(&e820_saved, start, size, old_type,
504 new_type);
507 /* make e820 not cover the range */
508 u64 __init e820_remove_range(u64 start, u64 size, unsigned old_type,
509 int checktype)
511 int i;
512 u64 end;
513 u64 real_removed_size = 0;
515 if (size > (ULLONG_MAX - start))
516 size = ULLONG_MAX - start;
518 end = start + size;
519 printk(KERN_DEBUG "e820 remove range: %016Lx - %016Lx ",
520 (unsigned long long) start,
521 (unsigned long long) end);
522 e820_print_type(old_type);
523 printk(KERN_CONT "\n");
525 for (i = 0; i < e820.nr_map; i++) {
526 struct e820entry *ei = &e820.map[i];
527 u64 final_start, final_end;
529 if (checktype && ei->type != old_type)
530 continue;
531 /* totally covered? */
532 if (ei->addr >= start &&
533 (ei->addr + ei->size) <= (start + size)) {
534 real_removed_size += ei->size;
535 memset(ei, 0, sizeof(struct e820entry));
536 continue;
538 /* partially covered */
539 final_start = max(start, ei->addr);
540 final_end = min(start + size, ei->addr + ei->size);
541 if (final_start >= final_end)
542 continue;
543 real_removed_size += final_end - final_start;
545 ei->size -= final_end - final_start;
546 if (ei->addr < final_start)
547 continue;
548 ei->addr = final_end;
550 return real_removed_size;
553 void __init update_e820(void)
555 u32 nr_map;
557 nr_map = e820.nr_map;
558 if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr_map))
559 return;
560 e820.nr_map = nr_map;
561 printk(KERN_INFO "modified physical RAM map:\n");
562 e820_print_map("modified");
564 static void __init update_e820_saved(void)
566 u32 nr_map;
568 nr_map = e820_saved.nr_map;
569 if (sanitize_e820_map(e820_saved.map, ARRAY_SIZE(e820_saved.map), &nr_map))
570 return;
571 e820_saved.nr_map = nr_map;
573 #define MAX_GAP_END 0x100000000ull
575 * Search for a gap in the e820 memory space from start_addr to end_addr.
577 __init int e820_search_gap(unsigned long *gapstart, unsigned long *gapsize,
578 unsigned long start_addr, unsigned long long end_addr)
580 unsigned long long last;
581 int i = e820.nr_map;
582 int found = 0;
584 last = (end_addr && end_addr < MAX_GAP_END) ? end_addr : MAX_GAP_END;
586 while (--i >= 0) {
587 unsigned long long start = e820.map[i].addr;
588 unsigned long long end = start + e820.map[i].size;
590 if (end < start_addr)
591 continue;
594 * Since "last" is at most 4GB, we know we'll
595 * fit in 32 bits if this condition is true
597 if (last > end) {
598 unsigned long gap = last - end;
600 if (gap >= *gapsize) {
601 *gapsize = gap;
602 *gapstart = end;
603 found = 1;
606 if (start < last)
607 last = start;
609 return found;
613 * Search for the biggest gap in the low 32 bits of the e820
614 * memory space. We pass this space to PCI to assign MMIO resources
615 * for hotplug or unconfigured devices in.
616 * Hopefully the BIOS let enough space left.
618 __init void e820_setup_gap(void)
620 unsigned long gapstart, gapsize;
621 int found;
623 gapstart = 0x10000000;
624 gapsize = 0x400000;
625 found = e820_search_gap(&gapstart, &gapsize, 0, MAX_GAP_END);
627 #ifdef CONFIG_X86_64
628 if (!found) {
629 gapstart = (max_pfn << PAGE_SHIFT) + 1024*1024;
630 printk(KERN_ERR
631 "PCI: Warning: Cannot find a gap in the 32bit address range\n"
632 "PCI: Unassigned devices with 32bit resource registers may break!\n");
634 #endif
637 * e820_reserve_resources_late protect stolen RAM already
639 pci_mem_start = gapstart;
641 printk(KERN_INFO
642 "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
643 pci_mem_start, gapstart, gapsize);
647 * Because of the size limitation of struct boot_params, only first
648 * 128 E820 memory entries are passed to kernel via
649 * boot_params.e820_map, others are passed via SETUP_E820_EXT node of
650 * linked list of struct setup_data, which is parsed here.
652 void __init parse_e820_ext(struct setup_data *sdata, unsigned long pa_data)
654 u32 map_len;
655 int entries;
656 struct e820entry *extmap;
658 entries = sdata->len / sizeof(struct e820entry);
659 map_len = sdata->len + sizeof(struct setup_data);
660 if (map_len > PAGE_SIZE)
661 sdata = early_ioremap(pa_data, map_len);
662 extmap = (struct e820entry *)(sdata->data);
663 __append_e820_map(extmap, entries);
664 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
665 if (map_len > PAGE_SIZE)
666 early_iounmap(sdata, map_len);
667 printk(KERN_INFO "extended physical RAM map:\n");
668 e820_print_map("extended");
671 #if defined(CONFIG_X86_64) || \
672 (defined(CONFIG_X86_32) && defined(CONFIG_HIBERNATION))
674 * Find the ranges of physical addresses that do not correspond to
675 * e820 RAM areas and mark the corresponding pages as nosave for
676 * hibernation (32 bit) or software suspend and suspend to RAM (64 bit).
678 * This function requires the e820 map to be sorted and without any
679 * overlapping entries and assumes the first e820 area to be RAM.
681 void __init e820_mark_nosave_regions(unsigned long limit_pfn)
683 int i;
684 unsigned long pfn;
686 pfn = PFN_DOWN(e820.map[0].addr + e820.map[0].size);
687 for (i = 1; i < e820.nr_map; i++) {
688 struct e820entry *ei = &e820.map[i];
690 if (pfn < PFN_UP(ei->addr))
691 register_nosave_region(pfn, PFN_UP(ei->addr));
693 pfn = PFN_DOWN(ei->addr + ei->size);
694 if (ei->type != E820_RAM && ei->type != E820_RESERVED_KERN)
695 register_nosave_region(PFN_UP(ei->addr), pfn);
697 if (pfn >= limit_pfn)
698 break;
701 #endif
703 #ifdef CONFIG_HIBERNATION
705 * Mark ACPI NVS memory region, so that we can save/restore it during
706 * hibernation and the subsequent resume.
708 static int __init e820_mark_nvs_memory(void)
710 int i;
712 for (i = 0; i < e820.nr_map; i++) {
713 struct e820entry *ei = &e820.map[i];
715 if (ei->type == E820_NVS)
716 hibernate_nvs_register(ei->addr, ei->size);
719 return 0;
721 core_initcall(e820_mark_nvs_memory);
722 #endif
725 * Find a free area with specified alignment in a specific range.
727 u64 __init find_e820_area(u64 start, u64 end, u64 size, u64 align)
729 int i;
731 for (i = 0; i < e820.nr_map; i++) {
732 struct e820entry *ei = &e820.map[i];
733 u64 addr;
734 u64 ei_start, ei_last;
736 if (ei->type != E820_RAM)
737 continue;
739 ei_last = ei->addr + ei->size;
740 ei_start = ei->addr;
741 addr = find_early_area(ei_start, ei_last, start, end,
742 size, align);
744 if (addr != -1ULL)
745 return addr;
747 return -1ULL;
750 u64 __init find_fw_memmap_area(u64 start, u64 end, u64 size, u64 align)
752 return find_e820_area(start, end, size, align);
755 u64 __init get_max_mapped(void)
757 u64 end = max_pfn_mapped;
759 end <<= PAGE_SHIFT;
761 return end;
764 * Find next free range after *start
766 u64 __init find_e820_area_size(u64 start, u64 *sizep, u64 align)
768 int i;
770 for (i = 0; i < e820.nr_map; i++) {
771 struct e820entry *ei = &e820.map[i];
772 u64 addr;
773 u64 ei_start, ei_last;
775 if (ei->type != E820_RAM)
776 continue;
778 ei_last = ei->addr + ei->size;
779 ei_start = ei->addr;
780 addr = find_early_area_size(ei_start, ei_last, start,
781 sizep, align);
783 if (addr != -1ULL)
784 return addr;
787 return -1ULL;
791 * pre allocated 4k and reserved it in e820
793 u64 __init early_reserve_e820(u64 startt, u64 sizet, u64 align)
795 u64 size = 0;
796 u64 addr;
797 u64 start;
799 for (start = startt; ; start += size) {
800 start = find_e820_area_size(start, &size, align);
801 if (!(start + 1))
802 return 0;
803 if (size >= sizet)
804 break;
807 #ifdef CONFIG_X86_32
808 if (start >= MAXMEM)
809 return 0;
810 if (start + size > MAXMEM)
811 size = MAXMEM - start;
812 #endif
814 addr = round_down(start + size - sizet, align);
815 if (addr < start)
816 return 0;
817 e820_update_range(addr, sizet, E820_RAM, E820_RESERVED);
818 e820_update_range_saved(addr, sizet, E820_RAM, E820_RESERVED);
819 printk(KERN_INFO "update e820 for early_reserve_e820\n");
820 update_e820();
821 update_e820_saved();
823 return addr;
826 #ifdef CONFIG_X86_32
827 # ifdef CONFIG_X86_PAE
828 # define MAX_ARCH_PFN (1ULL<<(36-PAGE_SHIFT))
829 # else
830 # define MAX_ARCH_PFN (1ULL<<(32-PAGE_SHIFT))
831 # endif
832 #else /* CONFIG_X86_32 */
833 # define MAX_ARCH_PFN MAXMEM>>PAGE_SHIFT
834 #endif
837 * Find the highest page frame number we have available
839 static unsigned long __init e820_end_pfn(unsigned long limit_pfn, unsigned type)
841 int i;
842 unsigned long last_pfn = 0;
843 unsigned long max_arch_pfn = MAX_ARCH_PFN;
845 for (i = 0; i < e820.nr_map; i++) {
846 struct e820entry *ei = &e820.map[i];
847 unsigned long start_pfn;
848 unsigned long end_pfn;
850 if (ei->type != type)
851 continue;
853 start_pfn = ei->addr >> PAGE_SHIFT;
854 end_pfn = (ei->addr + ei->size) >> PAGE_SHIFT;
856 if (start_pfn >= limit_pfn)
857 continue;
858 if (end_pfn > limit_pfn) {
859 last_pfn = limit_pfn;
860 break;
862 if (end_pfn > last_pfn)
863 last_pfn = end_pfn;
866 if (last_pfn > max_arch_pfn)
867 last_pfn = max_arch_pfn;
869 printk(KERN_INFO "last_pfn = %#lx max_arch_pfn = %#lx\n",
870 last_pfn, max_arch_pfn);
871 return last_pfn;
873 unsigned long __init e820_end_of_ram_pfn(void)
875 return e820_end_pfn(MAX_ARCH_PFN, E820_RAM);
878 unsigned long __init e820_end_of_low_ram_pfn(void)
880 return e820_end_pfn(1UL<<(32 - PAGE_SHIFT), E820_RAM);
883 * Finds an active region in the address range from start_pfn to last_pfn and
884 * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
886 int __init e820_find_active_region(const struct e820entry *ei,
887 unsigned long start_pfn,
888 unsigned long last_pfn,
889 unsigned long *ei_startpfn,
890 unsigned long *ei_endpfn)
892 u64 align = PAGE_SIZE;
894 *ei_startpfn = round_up(ei->addr, align) >> PAGE_SHIFT;
895 *ei_endpfn = round_down(ei->addr + ei->size, align) >> PAGE_SHIFT;
897 /* Skip map entries smaller than a page */
898 if (*ei_startpfn >= *ei_endpfn)
899 return 0;
901 /* Skip if map is outside the node */
902 if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
903 *ei_startpfn >= last_pfn)
904 return 0;
906 /* Check for overlaps */
907 if (*ei_startpfn < start_pfn)
908 *ei_startpfn = start_pfn;
909 if (*ei_endpfn > last_pfn)
910 *ei_endpfn = last_pfn;
912 return 1;
915 /* Walk the e820 map and register active regions within a node */
916 void __init e820_register_active_regions(int nid, unsigned long start_pfn,
917 unsigned long last_pfn)
919 unsigned long ei_startpfn;
920 unsigned long ei_endpfn;
921 int i;
923 for (i = 0; i < e820.nr_map; i++)
924 if (e820_find_active_region(&e820.map[i],
925 start_pfn, last_pfn,
926 &ei_startpfn, &ei_endpfn))
927 add_active_range(nid, ei_startpfn, ei_endpfn);
931 * Find the hole size (in bytes) in the memory range.
932 * @start: starting address of the memory range to scan
933 * @end: ending address of the memory range to scan
935 u64 __init e820_hole_size(u64 start, u64 end)
937 unsigned long start_pfn = start >> PAGE_SHIFT;
938 unsigned long last_pfn = end >> PAGE_SHIFT;
939 unsigned long ei_startpfn, ei_endpfn, ram = 0;
940 int i;
942 for (i = 0; i < e820.nr_map; i++) {
943 if (e820_find_active_region(&e820.map[i],
944 start_pfn, last_pfn,
945 &ei_startpfn, &ei_endpfn))
946 ram += ei_endpfn - ei_startpfn;
948 return end - start - ((u64)ram << PAGE_SHIFT);
951 static void early_panic(char *msg)
953 early_printk(msg);
954 panic(msg);
957 static int userdef __initdata;
959 /* "mem=nopentium" disables the 4MB page tables. */
960 static int __init parse_memopt(char *p)
962 u64 mem_size;
964 if (!p)
965 return -EINVAL;
967 #ifdef CONFIG_X86_32
968 if (!strcmp(p, "nopentium")) {
969 setup_clear_cpu_cap(X86_FEATURE_PSE);
970 return 0;
972 #endif
974 userdef = 1;
975 mem_size = memparse(p, &p);
976 e820_remove_range(mem_size, ULLONG_MAX - mem_size, E820_RAM, 1);
978 return 0;
980 early_param("mem", parse_memopt);
982 static int __init parse_memmap_opt(char *p)
984 char *oldp;
985 u64 start_at, mem_size;
987 if (!p)
988 return -EINVAL;
990 if (!strncmp(p, "exactmap", 8)) {
991 #ifdef CONFIG_CRASH_DUMP
993 * If we are doing a crash dump, we still need to know
994 * the real mem size before original memory map is
995 * reset.
997 saved_max_pfn = e820_end_of_ram_pfn();
998 #endif
999 e820.nr_map = 0;
1000 userdef = 1;
1001 return 0;
1004 oldp = p;
1005 mem_size = memparse(p, &p);
1006 if (p == oldp)
1007 return -EINVAL;
1009 userdef = 1;
1010 if (*p == '@') {
1011 start_at = memparse(p+1, &p);
1012 e820_add_region(start_at, mem_size, E820_RAM);
1013 } else if (*p == '#') {
1014 start_at = memparse(p+1, &p);
1015 e820_add_region(start_at, mem_size, E820_ACPI);
1016 } else if (*p == '$') {
1017 start_at = memparse(p+1, &p);
1018 e820_add_region(start_at, mem_size, E820_RESERVED);
1019 } else
1020 e820_remove_range(mem_size, ULLONG_MAX - mem_size, E820_RAM, 1);
1022 return *p == '\0' ? 0 : -EINVAL;
1024 early_param("memmap", parse_memmap_opt);
1026 void __init finish_e820_parsing(void)
1028 if (userdef) {
1029 u32 nr = e820.nr_map;
1031 if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr) < 0)
1032 early_panic("Invalid user supplied memory map");
1033 e820.nr_map = nr;
1035 printk(KERN_INFO "user-defined physical RAM map:\n");
1036 e820_print_map("user");
1040 static inline const char *e820_type_to_string(int e820_type)
1042 switch (e820_type) {
1043 case E820_RESERVED_KERN:
1044 case E820_RAM: return "System RAM";
1045 case E820_ACPI: return "ACPI Tables";
1046 case E820_NVS: return "ACPI Non-volatile Storage";
1047 case E820_UNUSABLE: return "Unusable memory";
1048 default: return "reserved";
1053 * Mark e820 reserved areas as busy for the resource manager.
1055 static struct resource __initdata *e820_res;
1056 void __init e820_reserve_resources(void)
1058 int i;
1059 struct resource *res;
1060 u64 end;
1062 res = alloc_bootmem(sizeof(struct resource) * e820.nr_map);
1063 e820_res = res;
1064 for (i = 0; i < e820.nr_map; i++) {
1065 end = e820.map[i].addr + e820.map[i].size - 1;
1066 if (end != (resource_size_t)end) {
1067 res++;
1068 continue;
1070 res->name = e820_type_to_string(e820.map[i].type);
1071 res->start = e820.map[i].addr;
1072 res->end = end;
1074 res->flags = IORESOURCE_MEM;
1077 * don't register the region that could be conflicted with
1078 * pci device BAR resource and insert them later in
1079 * pcibios_resource_survey()
1081 if (e820.map[i].type != E820_RESERVED || res->start < (1ULL<<20)) {
1082 res->flags |= IORESOURCE_BUSY;
1083 insert_resource(&iomem_resource, res);
1085 res++;
1088 for (i = 0; i < e820_saved.nr_map; i++) {
1089 struct e820entry *entry = &e820_saved.map[i];
1090 firmware_map_add_early(entry->addr,
1091 entry->addr + entry->size - 1,
1092 e820_type_to_string(entry->type));
1096 /* How much should we pad RAM ending depending on where it is? */
1097 static unsigned long ram_alignment(resource_size_t pos)
1099 unsigned long mb = pos >> 20;
1101 /* To 64kB in the first megabyte */
1102 if (!mb)
1103 return 64*1024;
1105 /* To 1MB in the first 16MB */
1106 if (mb < 16)
1107 return 1024*1024;
1109 /* To 64MB for anything above that */
1110 return 64*1024*1024;
1113 #define MAX_RESOURCE_SIZE ((resource_size_t)-1)
1115 void __init e820_reserve_resources_late(void)
1117 int i;
1118 struct resource *res;
1120 res = e820_res;
1121 for (i = 0; i < e820.nr_map; i++) {
1122 if (!res->parent && res->end)
1123 insert_resource_expand_to_fit(&iomem_resource, res);
1124 res++;
1128 * Try to bump up RAM regions to reasonable boundaries to
1129 * avoid stolen RAM:
1131 for (i = 0; i < e820.nr_map; i++) {
1132 struct e820entry *entry = &e820.map[i];
1133 u64 start, end;
1135 if (entry->type != E820_RAM)
1136 continue;
1137 start = entry->addr + entry->size;
1138 end = round_up(start, ram_alignment(start)) - 1;
1139 if (end > MAX_RESOURCE_SIZE)
1140 end = MAX_RESOURCE_SIZE;
1141 if (start >= end)
1142 continue;
1143 printk(KERN_DEBUG "reserve RAM buffer: %016llx - %016llx ",
1144 start, end);
1145 reserve_region_with_split(&iomem_resource, start, end,
1146 "RAM buffer");
1150 char *__init default_machine_specific_memory_setup(void)
1152 char *who = "BIOS-e820";
1153 u32 new_nr;
1155 * Try to copy the BIOS-supplied E820-map.
1157 * Otherwise fake a memory map; one section from 0k->640k,
1158 * the next section from 1mb->appropriate_mem_k
1160 new_nr = boot_params.e820_entries;
1161 sanitize_e820_map(boot_params.e820_map,
1162 ARRAY_SIZE(boot_params.e820_map),
1163 &new_nr);
1164 boot_params.e820_entries = new_nr;
1165 if (append_e820_map(boot_params.e820_map, boot_params.e820_entries)
1166 < 0) {
1167 u64 mem_size;
1169 /* compare results from other methods and take the greater */
1170 if (boot_params.alt_mem_k
1171 < boot_params.screen_info.ext_mem_k) {
1172 mem_size = boot_params.screen_info.ext_mem_k;
1173 who = "BIOS-88";
1174 } else {
1175 mem_size = boot_params.alt_mem_k;
1176 who = "BIOS-e801";
1179 e820.nr_map = 0;
1180 e820_add_region(0, LOWMEMSIZE(), E820_RAM);
1181 e820_add_region(HIGH_MEMORY, mem_size << 10, E820_RAM);
1184 /* In case someone cares... */
1185 return who;
1188 void __init setup_memory_map(void)
1190 char *who;
1192 who = x86_init.resources.memory_setup();
1193 memcpy(&e820_saved, &e820, sizeof(struct e820map));
1194 printk(KERN_INFO "BIOS-provided physical RAM map:\n");
1195 e820_print_map(who);