2 * Handle the memory map.
3 * The functions here do the job until bootmem takes over.
4 * $Id: e820.c,v 1.4 2002/09/19 19:25:32 ak Exp $
6 * Getting sanitize_e820_map() in sync with i386 version by applying change:
7 * - Provisions for empty E820 memory regions (reported by certain BIOSes).
8 * Alex Achenbach <xela@slit.de>, December 2002.
9 * Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
12 #include <linux/config.h>
13 #include <linux/kernel.h>
14 #include <linux/types.h>
15 #include <linux/init.h>
16 #include <linux/bootmem.h>
17 #include <linux/ioport.h>
18 #include <linux/string.h>
19 #include <linux/kexec.h>
20 #include <linux/module.h>
24 #include <asm/proto.h>
25 #include <asm/bootsetup.h>
26 #include <asm/sections.h>
27 #include <xen/interface/memory.h>
29 unsigned long pci_mem_start = 0xaeedbabe;
32 * PFN of last memory page.
34 unsigned long end_pfn;
35 EXPORT_SYMBOL(end_pfn);
36 unsigned long end_user_pfn = MAXMEM>>PAGE_SHIFT;
37 unsigned long end_pfn_map;
40 * Add a memory region to the kernel e820 map.
42 void __init add_memory_region(unsigned long start, unsigned long size, int type)
47 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
51 e820.map[x].addr = start;
52 e820.map[x].size = size;
53 e820.map[x].type = type;
60 * end_pfn only includes RAM, while end_pfn_map includes all e820 entries.
61 * The direct mapping extends to end_pfn_map, so that we can directly access
62 * apertures, ACPI and other tables without having to play with fixmaps.
66 * Last pfn which the user wants to use.
69 extern struct resource code_resource, data_resource;
71 /* Check for some hardcoded bad areas that early boot is not allowed to touch */
72 static inline int bad_addr(unsigned long *addrp, unsigned long size)
74 unsigned long addr = *addrp, last = addr + size;
76 /* various gunk below that needed for SMP startup */
82 /* direct mapping tables of the kernel */
83 if (last >= table_start<<PAGE_SHIFT && addr < table_end<<PAGE_SHIFT) {
84 *addrp = table_end << PAGE_SHIFT;
89 #ifdef CONFIG_BLK_DEV_INITRD
90 if (LOADER_TYPE && INITRD_START && last >= INITRD_START &&
91 addr < INITRD_START+INITRD_SIZE) {
92 *addrp = INITRD_START + INITRD_SIZE;
96 /* kernel code + 640k memory hole (later should not be needed, but
97 be paranoid for now) */
98 if (last >= 640*1024 && addr < __pa_symbol(&_end)) {
99 *addrp = __pa_symbol(&_end);
102 /* XXX ramdisk image here? */
106 int __init e820_mapped(unsigned long start, unsigned long end, unsigned type)
109 for (i = 0; i < e820.nr_map; i++) {
110 struct e820entry *ei = &e820.map[i];
111 if (type && ei->type != type)
113 if (ei->addr >= end || ei->addr + ei->size <= start)
121 * Find a free area in a specific range.
123 unsigned long __init find_e820_area(unsigned long start, unsigned long end, unsigned size)
126 for (i = 0; i < e820.nr_map; i++) {
127 struct e820entry *ei = &e820.map[i];
128 unsigned long addr = ei->addr, last;
129 if (ei->type != E820_RAM)
133 if (addr > ei->addr + ei->size)
135 while (bad_addr(&addr, size) && addr+size < ei->addr + ei->size)
138 if (last > ei->addr + ei->size)
148 * Free bootmem based on the e820 table for a node.
150 void __init e820_bootmem_free(pg_data_t *pgdat, unsigned long start,unsigned long end)
153 for (i = 0; i < e820.nr_map; i++) {
154 struct e820entry *ei = &e820.map[i];
155 unsigned long last, addr;
157 if (ei->type != E820_RAM ||
158 ei->addr+ei->size <= start ||
162 addr = round_up(ei->addr, PAGE_SIZE);
166 last = round_down(ei->addr + ei->size, PAGE_SIZE);
170 if (last > addr && last-addr >= PAGE_SIZE)
171 free_bootmem_node(pgdat, addr, last-addr);
176 * Find the highest page frame number we have available
178 unsigned long __init e820_end_of_ram(void)
181 unsigned long end_pfn = 0;
183 for (i = 0; i < e820.nr_map; i++) {
184 struct e820entry *ei = &e820.map[i];
185 unsigned long start, end;
187 start = round_up(ei->addr, PAGE_SIZE);
188 end = round_down(ei->addr + ei->size, PAGE_SIZE);
191 if (ei->type == E820_RAM) {
192 if (end > end_pfn<<PAGE_SHIFT)
193 end_pfn = end>>PAGE_SHIFT;
195 if (end > end_pfn_map<<PAGE_SHIFT)
196 end_pfn_map = end>>PAGE_SHIFT;
200 if (end_pfn > end_pfn_map)
201 end_pfn_map = end_pfn;
202 if (end_pfn_map > MAXMEM>>PAGE_SHIFT)
203 end_pfn_map = MAXMEM>>PAGE_SHIFT;
204 if (end_pfn > end_user_pfn)
205 end_pfn = end_user_pfn;
206 if (end_pfn > end_pfn_map)
207 end_pfn = end_pfn_map;
213 * Compute how much memory is missing in a range.
214 * Unlike the other functions in this file the arguments are in page numbers.
217 e820_hole_size(unsigned long start_pfn, unsigned long end_pfn)
219 unsigned long ram = 0;
220 unsigned long start = start_pfn << PAGE_SHIFT;
221 unsigned long end = end_pfn << PAGE_SHIFT;
223 for (i = 0; i < e820.nr_map; i++) {
224 struct e820entry *ei = &e820.map[i];
225 unsigned long last, addr;
227 if (ei->type != E820_RAM ||
228 ei->addr+ei->size <= start ||
232 addr = round_up(ei->addr, PAGE_SIZE);
236 last = round_down(ei->addr + ei->size, PAGE_SIZE);
243 return ((end - start) - ram) >> PAGE_SHIFT;
247 * Mark e820 reserved areas as busy for the resource manager.
249 void __init e820_reserve_resources(void)
252 for (i = 0; i < e820.nr_map; i++) {
253 struct resource *res;
254 res = alloc_bootmem_low(sizeof(struct resource));
255 switch (e820.map[i].type) {
256 case E820_RAM: res->name = "System RAM"; break;
257 case E820_ACPI: res->name = "ACPI Tables"; break;
258 case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
259 default: res->name = "reserved";
261 res->start = e820.map[i].addr;
262 res->end = res->start + e820.map[i].size - 1;
263 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
264 request_resource(&iomem_resource, res);
265 if (e820.map[i].type == E820_RAM) {
267 * We don't know which RAM region contains kernel data,
268 * so we try it repeatedly and let the resource manager
271 request_resource(res, &code_resource);
272 request_resource(res, &data_resource);
274 request_resource(res, &crashk_res);
279 #endif /* CONFIG_XEN */
281 void __init e820_print_map(char *who)
285 for (i = 0; i < e820.nr_map; i++) {
286 printk(" %s: %016Lx - %016Lx ", who,
287 (unsigned long long) e820.map[i].addr,
288 (unsigned long long) (e820.map[i].addr + e820.map[i].size));
289 switch (e820.map[i].type) {
290 case E820_RAM: printk("(usable)\n");
293 printk("(reserved)\n");
296 printk("(ACPI data)\n");
299 printk("(ACPI NVS)\n");
301 default: printk("type %u\n", e820.map[i].type);
309 * Sanitize the BIOS e820 map.
311 * Some e820 responses include overlapping entries. The following
312 * replaces the original e820 map with a new one, removing overlaps.
315 static int __init sanitize_e820_map(struct e820entry * biosmap, char * pnr_map)
317 struct change_member {
318 struct e820entry *pbios; /* pointer to original bios entry */
319 unsigned long long addr; /* address for this change point */
321 static struct change_member change_point_list[2*E820MAX] __initdata;
322 static struct change_member *change_point[2*E820MAX] __initdata;
323 static struct e820entry *overlap_list[E820MAX] __initdata;
324 static struct e820entry new_bios[E820MAX] __initdata;
325 struct change_member *change_tmp;
326 unsigned long current_type, last_type;
327 unsigned long long last_addr;
328 int chgidx, still_changing;
331 int old_nr, new_nr, chg_nr;
335 Visually we're performing the following (1,2,3,4 = memory types)...
337 Sample memory map (w/overlaps):
338 ____22__________________
339 ______________________4_
340 ____1111________________
341 _44_____________________
342 11111111________________
343 ____________________33__
344 ___________44___________
345 __________33333_________
346 ______________22________
347 ___________________2222_
348 _________111111111______
349 _____________________11_
350 _________________4______
352 Sanitized equivalent (no overlap):
353 1_______________________
354 _44_____________________
355 ___1____________________
356 ____22__________________
357 ______11________________
358 _________1______________
359 __________3_____________
360 ___________44___________
361 _____________33_________
362 _______________2________
363 ________________1_______
364 _________________4______
365 ___________________2____
366 ____________________33__
367 ______________________4_
370 /* if there's only one memory region, don't bother */
376 /* bail out if we find any unreasonable addresses in bios map */
377 for (i=0; i<old_nr; i++)
378 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
381 /* create pointers for initial change-point information (for sorting) */
382 for (i=0; i < 2*old_nr; i++)
383 change_point[i] = &change_point_list[i];
385 /* record all known change-points (starting and ending addresses),
386 omitting those that are for empty memory regions */
388 for (i=0; i < old_nr; i++) {
389 if (biosmap[i].size != 0) {
390 change_point[chgidx]->addr = biosmap[i].addr;
391 change_point[chgidx++]->pbios = &biosmap[i];
392 change_point[chgidx]->addr = biosmap[i].addr + biosmap[i].size;
393 change_point[chgidx++]->pbios = &biosmap[i];
398 /* sort change-point list by memory addresses (low -> high) */
400 while (still_changing) {
402 for (i=1; i < chg_nr; i++) {
403 /* if <current_addr> > <last_addr>, swap */
404 /* or, if current=<start_addr> & last=<end_addr>, swap */
405 if ((change_point[i]->addr < change_point[i-1]->addr) ||
406 ((change_point[i]->addr == change_point[i-1]->addr) &&
407 (change_point[i]->addr == change_point[i]->pbios->addr) &&
408 (change_point[i-1]->addr != change_point[i-1]->pbios->addr))
411 change_tmp = change_point[i];
412 change_point[i] = change_point[i-1];
413 change_point[i-1] = change_tmp;
419 /* create a new bios memory map, removing overlaps */
420 overlap_entries=0; /* number of entries in the overlap table */
421 new_bios_entry=0; /* index for creating new bios map entries */
422 last_type = 0; /* start with undefined memory type */
423 last_addr = 0; /* start with 0 as last starting address */
424 /* loop through change-points, determining affect on the new bios map */
425 for (chgidx=0; chgidx < chg_nr; chgidx++)
427 /* keep track of all overlapping bios entries */
428 if (change_point[chgidx]->addr == change_point[chgidx]->pbios->addr)
430 /* add map entry to overlap list (> 1 entry implies an overlap) */
431 overlap_list[overlap_entries++]=change_point[chgidx]->pbios;
435 /* remove entry from list (order independent, so swap with last) */
436 for (i=0; i<overlap_entries; i++)
438 if (overlap_list[i] == change_point[chgidx]->pbios)
439 overlap_list[i] = overlap_list[overlap_entries-1];
443 /* if there are overlapping entries, decide which "type" to use */
444 /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */
446 for (i=0; i<overlap_entries; i++)
447 if (overlap_list[i]->type > current_type)
448 current_type = overlap_list[i]->type;
449 /* continue building up new bios map based on this information */
450 if (current_type != last_type) {
451 if (last_type != 0) {
452 new_bios[new_bios_entry].size =
453 change_point[chgidx]->addr - last_addr;
454 /* move forward only if the new size was non-zero */
455 if (new_bios[new_bios_entry].size != 0)
456 if (++new_bios_entry >= E820MAX)
457 break; /* no more space left for new bios entries */
459 if (current_type != 0) {
460 new_bios[new_bios_entry].addr = change_point[chgidx]->addr;
461 new_bios[new_bios_entry].type = current_type;
462 last_addr=change_point[chgidx]->addr;
464 last_type = current_type;
467 new_nr = new_bios_entry; /* retain count for new bios entries */
469 /* copy new bios mapping into original location */
470 memcpy(biosmap, new_bios, new_nr*sizeof(struct e820entry));
477 * Copy the BIOS e820 map into a safe place.
479 * Sanity-check it while we're at it..
481 * If we're lucky and live on a modern system, the setup code
482 * will have given us a memory map that we can use to properly
483 * set up memory. If we aren't, we'll fake a memory map.
485 * We check to see that the memory map contains at least 2 elements
486 * before we'll use it, because the detection code in setup.S may
487 * not be perfect and most every PC known to man has two memory
488 * regions: one from 0 to 640k, and one from 1mb up. (The IBM
489 * thinkpad 560x, for example, does not cooperate with the memory
492 static int __init copy_e820_map(struct e820entry * biosmap, int nr_map)
494 /* Only one memory region (or negative)? Ignore it */
499 unsigned long start = biosmap->addr;
500 unsigned long size = biosmap->size;
501 unsigned long end = start + size;
502 unsigned long type = biosmap->type;
504 /* Overflow in 64 bits? Ignore the memory map. */
509 * Some BIOSes claim RAM in the 640k - 1M region.
510 * Not right. Fix it up.
512 * This should be removed on Hammer which is supposed to not
513 * have non e820 covered ISA mappings there, but I had some strange
514 * problems so it stays for now. -AK
516 if (type == E820_RAM) {
517 if (start < 0x100000ULL && end > 0xA0000ULL) {
518 if (start < 0xA0000ULL)
519 add_memory_region(start, 0xA0000ULL-start, type);
520 if (end <= 0x100000ULL)
527 add_memory_region(start, size, type);
528 } while (biosmap++,--nr_map);
532 void __init setup_memory_region(void)
534 char *who = "BIOS-e820";
537 * Try to copy the BIOS-supplied E820-map.
539 * Otherwise fake a memory map; one section from 0k->640k,
540 * the next section from 1mb->appropriate_mem_k
542 sanitize_e820_map(E820_MAP, &E820_MAP_NR);
543 if (copy_e820_map(E820_MAP, E820_MAP_NR) < 0) {
544 unsigned long mem_size;
546 /* compare results from other methods and take the greater */
547 if (ALT_MEM_K < EXT_MEM_K) {
548 mem_size = EXT_MEM_K;
551 mem_size = ALT_MEM_K;
556 add_memory_region(0, LOWMEMSIZE(), E820_RAM);
557 add_memory_region(HIGH_MEMORY, mem_size << 10, E820_RAM);
559 printk(KERN_INFO "BIOS-provided physical RAM map:\n");
563 #else /* CONFIG_XEN */
565 extern unsigned long xen_override_max_pfn;
566 extern union xen_start_info_union xen_start_info_union;
568 unsigned long __init e820_end_of_ram(void)
570 unsigned long max_end_pfn;
572 if (xen_override_max_pfn == 0) {
573 max_end_pfn = xen_start_info->nr_pages;
574 /* Default 8MB slack (to balance backend allocations). */
575 max_end_pfn += 8 << (20 - PAGE_SHIFT);
576 } else if (xen_override_max_pfn > xen_start_info->nr_pages) {
577 max_end_pfn = xen_override_max_pfn;
579 max_end_pfn = xen_start_info->nr_pages;
586 e820_hole_size(unsigned long start_pfn, unsigned long end_pfn)
591 void __init e820_reserve_resources(void)
594 struct dom0_memory_map_entry *map;
595 unsigned long gapstart, gapsize, round, last;
598 if (!(xen_start_info->flags & SIF_INITDOMAIN))
601 map = alloc_bootmem_low_pages(PAGE_SIZE);
602 op.cmd = DOM0_PHYSICAL_MEMORY_MAP;
603 op.u.physical_memory_map.memory_map = map;
604 op.u.physical_memory_map.max_map_entries =
605 PAGE_SIZE / sizeof(struct dom0_memory_map_entry);
606 BUG_ON(HYPERVISOR_dom0_op(&op));
608 last = 0x100000000ULL;
609 gapstart = 0x10000000;
612 for (i = op.u.physical_memory_map.nr_map_entries - 1; i >= 0; i--) {
613 struct resource *res;
615 if ((last > map[i].end) && ((last - map[i].end) > gapsize)) {
616 gapsize = last - map[i].end;
617 gapstart = map[i].end;
620 if (map[i].start < last)
623 if (map[i].end > 0x100000000ULL)
625 res = alloc_bootmem_low(sizeof(struct resource));
626 res->name = map[i].is_ram ? "System RAM" : "reserved";
627 res->start = map[i].start;
628 res->end = map[i].end - 1;
629 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
630 request_resource(&iomem_resource, res);
633 free_bootmem(__pa(map), PAGE_SIZE);
636 gapstart = HYPERVISOR_memory_op(XENMEM_maximum_ram_page, NULL);
637 gapstart = (gapstart << PAGE_SHIFT) + 1024*1024;
638 printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit address range\n"
639 KERN_ERR "PCI: Unassigned devices with 32bit resource registers may break!\n");
643 * See how much we want to round up: start off with
644 * rounding to the next 1MB area.
647 while ((gapsize >> 4) > round)
649 /* Fun with two's complement */
650 pci_mem_start = (gapstart + round) & -round;
652 printk(KERN_INFO "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
653 pci_mem_start, gapstart, gapsize);
658 void __init parse_memopt(char *p, char **from)
660 end_user_pfn = memparse(p, from);
661 end_user_pfn >>= PAGE_SHIFT;
662 xen_override_max_pfn = (unsigned long) end_user_pfn;
665 void __init parse_memmapopt(char *p, char **from)
667 unsigned long long start_at, mem_size;
669 mem_size = memparse(p, from);
672 start_at = memparse(p+1, from);
673 add_memory_region(start_at, mem_size, E820_RAM);
674 } else if (*p == '#') {
675 start_at = memparse(p+1, from);
676 add_memory_region(start_at, mem_size, E820_ACPI);
677 } else if (*p == '$') {
678 start_at = memparse(p+1, from);
679 add_memory_region(start_at, mem_size, E820_RESERVED);
681 end_user_pfn = (mem_size >> PAGE_SHIFT);
687 * Search for the biggest gap in the low 32 bits of the e820
688 * memory space. We pass this space to PCI to assign MMIO resources
689 * for hotplug or unconfigured devices in.
690 * Hopefully the BIOS let enough space left.
692 __init void e820_setup_gap(void)
695 unsigned long gapstart, gapsize;
700 last = 0x100000000ull;
701 gapstart = 0x10000000;
705 unsigned long long start = e820.map[i].addr;
706 unsigned long long end = start + e820.map[i].size;
709 * Since "last" is at most 4GB, we know we'll
710 * fit in 32 bits if this condition is true
713 unsigned long gap = last - end;
726 gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
727 printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit address range\n"
728 KERN_ERR "PCI: Unassigned devices with 32bit resource registers may break!\n");
732 * Start allocating dynamic PCI memory a bit into the gap,
733 * aligned up to the nearest megabyte.
735 * Question: should we try to pad it up a bit (do something
736 * like " + (gapsize >> 3)" in there too?). We now have the
739 pci_mem_start = (gapstart + 0xfffff) & ~0xfffff;
741 printk(KERN_INFO "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
742 pci_mem_start, gapstart, gapsize);