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>
29 * PFN of last memory page.
31 unsigned long end_pfn;
32 EXPORT_SYMBOL(end_pfn);
35 * end_pfn only includes RAM, while end_pfn_map includes all e820 entries.
36 * The direct mapping extends to end_pfn_map, so that we can directly access
37 * apertures, ACPI and other tables without having to play with fixmaps.
39 unsigned long end_pfn_map;
42 * Last pfn which the user wants to use.
44 unsigned long end_user_pfn = MAXMEM>>PAGE_SHIFT;
46 extern struct resource code_resource, data_resource;
48 /* Check for some hardcoded bad areas that early boot is not allowed to touch */
49 static inline int bad_addr(unsigned long *addrp, unsigned long size)
51 unsigned long addr = *addrp, last = addr + size;
53 /* various gunk below that needed for SMP startup */
59 /* direct mapping tables of the kernel */
60 if (last >= table_start<<PAGE_SHIFT && addr < table_end<<PAGE_SHIFT) {
61 *addrp = table_end << PAGE_SHIFT;
66 #ifdef CONFIG_BLK_DEV_INITRD
67 if (LOADER_TYPE && INITRD_START && last >= INITRD_START &&
68 addr < INITRD_START+INITRD_SIZE) {
69 *addrp = INITRD_START + INITRD_SIZE;
73 /* kernel code + 640k memory hole (later should not be needed, but
74 be paranoid for now) */
75 if (last >= 640*1024 && addr < __pa_symbol(&_end)) {
76 *addrp = __pa_symbol(&_end);
79 /* XXX ramdisk image here? */
84 * This function checks if any part of the range <start,end> is mapped
88 e820_any_mapped(unsigned long start, unsigned long end, unsigned type)
91 for (i = 0; i < e820.nr_map; i++) {
92 struct e820entry *ei = &e820.map[i];
93 if (type && ei->type != type)
95 if (ei->addr >= end || ei->addr + ei->size <= start)
103 * This function checks if the entire range <start,end> is mapped with type.
105 * Note: this function only works correct if the e820 table is sorted and
106 * not-overlapping, which is the case
108 int __init e820_all_mapped(unsigned long start, unsigned long end, unsigned type)
111 for (i = 0; i < e820.nr_map; i++) {
112 struct e820entry *ei = &e820.map[i];
113 if (type && ei->type != type)
115 /* is the region (part) in overlap with the current region ?*/
116 if (ei->addr >= end || ei->addr + ei->size <= start)
119 /* if the region is at the beginning of <start,end> we move
120 * start to the end of the region since it's ok until there
122 if (ei->addr <= start)
123 start = ei->addr + ei->size;
124 /* if start is now at or beyond end, we're done, full coverage */
126 return 1; /* we're done */
132 * Find a free area in a specific range.
134 unsigned long __init find_e820_area(unsigned long start, unsigned long end, unsigned size)
137 for (i = 0; i < e820.nr_map; i++) {
138 struct e820entry *ei = &e820.map[i];
139 unsigned long addr = ei->addr, last;
140 if (ei->type != E820_RAM)
144 if (addr > ei->addr + ei->size)
146 while (bad_addr(&addr, size) && addr+size < ei->addr + ei->size)
149 if (last > ei->addr + ei->size)
159 * Free bootmem based on the e820 table for a node.
161 void __init e820_bootmem_free(pg_data_t *pgdat, unsigned long start,unsigned long end)
164 for (i = 0; i < e820.nr_map; i++) {
165 struct e820entry *ei = &e820.map[i];
166 unsigned long last, addr;
168 if (ei->type != E820_RAM ||
169 ei->addr+ei->size <= start ||
173 addr = round_up(ei->addr, PAGE_SIZE);
177 last = round_down(ei->addr + ei->size, PAGE_SIZE);
181 if (last > addr && last-addr >= PAGE_SIZE)
182 free_bootmem_node(pgdat, addr, last-addr);
187 * Find the highest page frame number we have available
189 unsigned long __init e820_end_of_ram(void)
192 unsigned long end_pfn = 0;
194 for (i = 0; i < e820.nr_map; i++) {
195 struct e820entry *ei = &e820.map[i];
196 unsigned long start, end;
198 start = round_up(ei->addr, PAGE_SIZE);
199 end = round_down(ei->addr + ei->size, PAGE_SIZE);
202 if (ei->type == E820_RAM) {
203 if (end > end_pfn<<PAGE_SHIFT)
204 end_pfn = end>>PAGE_SHIFT;
206 if (end > end_pfn_map<<PAGE_SHIFT)
207 end_pfn_map = end>>PAGE_SHIFT;
211 if (end_pfn > end_pfn_map)
212 end_pfn_map = end_pfn;
213 if (end_pfn_map > MAXMEM>>PAGE_SHIFT)
214 end_pfn_map = MAXMEM>>PAGE_SHIFT;
215 if (end_pfn > end_user_pfn)
216 end_pfn = end_user_pfn;
217 if (end_pfn > end_pfn_map)
218 end_pfn = end_pfn_map;
224 * Compute how much memory is missing in a range.
225 * Unlike the other functions in this file the arguments are in page numbers.
228 e820_hole_size(unsigned long start_pfn, unsigned long end_pfn)
230 unsigned long ram = 0;
231 unsigned long start = start_pfn << PAGE_SHIFT;
232 unsigned long end = end_pfn << PAGE_SHIFT;
234 for (i = 0; i < e820.nr_map; i++) {
235 struct e820entry *ei = &e820.map[i];
236 unsigned long last, addr;
238 if (ei->type != E820_RAM ||
239 ei->addr+ei->size <= start ||
243 addr = round_up(ei->addr, PAGE_SIZE);
247 last = round_down(ei->addr + ei->size, PAGE_SIZE);
254 return ((end - start) - ram) >> PAGE_SHIFT;
258 * Mark e820 reserved areas as busy for the resource manager.
260 void __init e820_reserve_resources(void)
263 for (i = 0; i < e820.nr_map; i++) {
264 struct resource *res;
265 res = alloc_bootmem_low(sizeof(struct resource));
266 switch (e820.map[i].type) {
267 case E820_RAM: res->name = "System RAM"; break;
268 case E820_ACPI: res->name = "ACPI Tables"; break;
269 case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
270 default: res->name = "reserved";
272 res->start = e820.map[i].addr;
273 res->end = res->start + e820.map[i].size - 1;
274 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
275 request_resource(&iomem_resource, res);
276 if (e820.map[i].type == E820_RAM) {
278 * We don't know which RAM region contains kernel data,
279 * so we try it repeatedly and let the resource manager
282 request_resource(res, &code_resource);
283 request_resource(res, &data_resource);
285 request_resource(res, &crashk_res);
292 * Add a memory region to the kernel e820 map.
294 void __init add_memory_region(unsigned long start, unsigned long size, int type)
299 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
303 e820.map[x].addr = start;
304 e820.map[x].size = size;
305 e820.map[x].type = type;
309 void __init e820_print_map(char *who)
313 for (i = 0; i < e820.nr_map; i++) {
314 printk(" %s: %016Lx - %016Lx ", who,
315 (unsigned long long) e820.map[i].addr,
316 (unsigned long long) (e820.map[i].addr + e820.map[i].size));
317 switch (e820.map[i].type) {
318 case E820_RAM: printk("(usable)\n");
321 printk("(reserved)\n");
324 printk("(ACPI data)\n");
327 printk("(ACPI NVS)\n");
329 default: printk("type %u\n", e820.map[i].type);
336 * Sanitize the BIOS e820 map.
338 * Some e820 responses include overlapping entries. The following
339 * replaces the original e820 map with a new one, removing overlaps.
342 static int __init sanitize_e820_map(struct e820entry * biosmap, char * pnr_map)
344 struct change_member {
345 struct e820entry *pbios; /* pointer to original bios entry */
346 unsigned long long addr; /* address for this change point */
348 static struct change_member change_point_list[2*E820MAX] __initdata;
349 static struct change_member *change_point[2*E820MAX] __initdata;
350 static struct e820entry *overlap_list[E820MAX] __initdata;
351 static struct e820entry new_bios[E820MAX] __initdata;
352 struct change_member *change_tmp;
353 unsigned long current_type, last_type;
354 unsigned long long last_addr;
355 int chgidx, still_changing;
358 int old_nr, new_nr, chg_nr;
362 Visually we're performing the following (1,2,3,4 = memory types)...
364 Sample memory map (w/overlaps):
365 ____22__________________
366 ______________________4_
367 ____1111________________
368 _44_____________________
369 11111111________________
370 ____________________33__
371 ___________44___________
372 __________33333_________
373 ______________22________
374 ___________________2222_
375 _________111111111______
376 _____________________11_
377 _________________4______
379 Sanitized equivalent (no overlap):
380 1_______________________
381 _44_____________________
382 ___1____________________
383 ____22__________________
384 ______11________________
385 _________1______________
386 __________3_____________
387 ___________44___________
388 _____________33_________
389 _______________2________
390 ________________1_______
391 _________________4______
392 ___________________2____
393 ____________________33__
394 ______________________4_
397 /* if there's only one memory region, don't bother */
403 /* bail out if we find any unreasonable addresses in bios map */
404 for (i=0; i<old_nr; i++)
405 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
408 /* create pointers for initial change-point information (for sorting) */
409 for (i=0; i < 2*old_nr; i++)
410 change_point[i] = &change_point_list[i];
412 /* record all known change-points (starting and ending addresses),
413 omitting those that are for empty memory regions */
415 for (i=0; i < old_nr; i++) {
416 if (biosmap[i].size != 0) {
417 change_point[chgidx]->addr = biosmap[i].addr;
418 change_point[chgidx++]->pbios = &biosmap[i];
419 change_point[chgidx]->addr = biosmap[i].addr + biosmap[i].size;
420 change_point[chgidx++]->pbios = &biosmap[i];
425 /* sort change-point list by memory addresses (low -> high) */
427 while (still_changing) {
429 for (i=1; i < chg_nr; i++) {
430 /* if <current_addr> > <last_addr>, swap */
431 /* or, if current=<start_addr> & last=<end_addr>, swap */
432 if ((change_point[i]->addr < change_point[i-1]->addr) ||
433 ((change_point[i]->addr == change_point[i-1]->addr) &&
434 (change_point[i]->addr == change_point[i]->pbios->addr) &&
435 (change_point[i-1]->addr != change_point[i-1]->pbios->addr))
438 change_tmp = change_point[i];
439 change_point[i] = change_point[i-1];
440 change_point[i-1] = change_tmp;
446 /* create a new bios memory map, removing overlaps */
447 overlap_entries=0; /* number of entries in the overlap table */
448 new_bios_entry=0; /* index for creating new bios map entries */
449 last_type = 0; /* start with undefined memory type */
450 last_addr = 0; /* start with 0 as last starting address */
451 /* loop through change-points, determining affect on the new bios map */
452 for (chgidx=0; chgidx < chg_nr; chgidx++)
454 /* keep track of all overlapping bios entries */
455 if (change_point[chgidx]->addr == change_point[chgidx]->pbios->addr)
457 /* add map entry to overlap list (> 1 entry implies an overlap) */
458 overlap_list[overlap_entries++]=change_point[chgidx]->pbios;
462 /* remove entry from list (order independent, so swap with last) */
463 for (i=0; i<overlap_entries; i++)
465 if (overlap_list[i] == change_point[chgidx]->pbios)
466 overlap_list[i] = overlap_list[overlap_entries-1];
470 /* if there are overlapping entries, decide which "type" to use */
471 /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */
473 for (i=0; i<overlap_entries; i++)
474 if (overlap_list[i]->type > current_type)
475 current_type = overlap_list[i]->type;
476 /* continue building up new bios map based on this information */
477 if (current_type != last_type) {
478 if (last_type != 0) {
479 new_bios[new_bios_entry].size =
480 change_point[chgidx]->addr - last_addr;
481 /* move forward only if the new size was non-zero */
482 if (new_bios[new_bios_entry].size != 0)
483 if (++new_bios_entry >= E820MAX)
484 break; /* no more space left for new bios entries */
486 if (current_type != 0) {
487 new_bios[new_bios_entry].addr = change_point[chgidx]->addr;
488 new_bios[new_bios_entry].type = current_type;
489 last_addr=change_point[chgidx]->addr;
491 last_type = current_type;
494 new_nr = new_bios_entry; /* retain count for new bios entries */
496 /* copy new bios mapping into original location */
497 memcpy(biosmap, new_bios, new_nr*sizeof(struct e820entry));
504 * Copy the BIOS e820 map into a safe place.
506 * Sanity-check it while we're at it..
508 * If we're lucky and live on a modern system, the setup code
509 * will have given us a memory map that we can use to properly
510 * set up memory. If we aren't, we'll fake a memory map.
512 * We check to see that the memory map contains at least 2 elements
513 * before we'll use it, because the detection code in setup.S may
514 * not be perfect and most every PC known to man has two memory
515 * regions: one from 0 to 640k, and one from 1mb up. (The IBM
516 * thinkpad 560x, for example, does not cooperate with the memory
519 static int __init copy_e820_map(struct e820entry * biosmap, int nr_map)
521 /* Only one memory region (or negative)? Ignore it */
526 unsigned long start = biosmap->addr;
527 unsigned long size = biosmap->size;
528 unsigned long end = start + size;
529 unsigned long type = biosmap->type;
531 /* Overflow in 64 bits? Ignore the memory map. */
536 * Some BIOSes claim RAM in the 640k - 1M region.
537 * Not right. Fix it up.
539 * This should be removed on Hammer which is supposed to not
540 * have non e820 covered ISA mappings there, but I had some strange
541 * problems so it stays for now. -AK
543 if (type == E820_RAM) {
544 if (start < 0x100000ULL && end > 0xA0000ULL) {
545 if (start < 0xA0000ULL)
546 add_memory_region(start, 0xA0000ULL-start, type);
547 if (end <= 0x100000ULL)
554 add_memory_region(start, size, type);
555 } while (biosmap++,--nr_map);
559 void __init setup_memory_region(void)
561 char *who = "BIOS-e820";
564 * Try to copy the BIOS-supplied E820-map.
566 * Otherwise fake a memory map; one section from 0k->640k,
567 * the next section from 1mb->appropriate_mem_k
569 sanitize_e820_map(E820_MAP, &E820_MAP_NR);
570 if (copy_e820_map(E820_MAP, E820_MAP_NR) < 0) {
571 unsigned long mem_size;
573 /* compare results from other methods and take the greater */
574 if (ALT_MEM_K < EXT_MEM_K) {
575 mem_size = EXT_MEM_K;
578 mem_size = ALT_MEM_K;
583 add_memory_region(0, LOWMEMSIZE(), E820_RAM);
584 add_memory_region(HIGH_MEMORY, mem_size << 10, E820_RAM);
586 printk(KERN_INFO "BIOS-provided physical RAM map:\n");
590 void __init parse_memopt(char *p, char **from)
592 end_user_pfn = memparse(p, from);
593 end_user_pfn >>= PAGE_SHIFT;
596 void __init parse_memmapopt(char *p, char **from)
598 unsigned long long start_at, mem_size;
600 mem_size = memparse(p, from);
603 start_at = memparse(p+1, from);
604 add_memory_region(start_at, mem_size, E820_RAM);
605 } else if (*p == '#') {
606 start_at = memparse(p+1, from);
607 add_memory_region(start_at, mem_size, E820_ACPI);
608 } else if (*p == '$') {
609 start_at = memparse(p+1, from);
610 add_memory_region(start_at, mem_size, E820_RESERVED);
612 end_user_pfn = (mem_size >> PAGE_SHIFT);
617 unsigned long pci_mem_start = 0xaeedbabe;
620 * Search for the biggest gap in the low 32 bits of the e820
621 * memory space. We pass this space to PCI to assign MMIO resources
622 * for hotplug or unconfigured devices in.
623 * Hopefully the BIOS let enough space left.
625 __init void e820_setup_gap(void)
627 unsigned long gapstart, gapsize, round;
632 last = 0x100000000ull;
633 gapstart = 0x10000000;
637 unsigned long long start = e820.map[i].addr;
638 unsigned long long end = start + e820.map[i].size;
641 * Since "last" is at most 4GB, we know we'll
642 * fit in 32 bits if this condition is true
645 unsigned long gap = last - end;
658 gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
659 printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit address range\n"
660 KERN_ERR "PCI: Unassigned devices with 32bit resource registers may break!\n");
664 * See how much we want to round up: start off with
665 * rounding to the next 1MB area.
668 while ((gapsize >> 4) > round)
670 /* Fun with two's complement */
671 pci_mem_start = (gapstart + round) & -round;
673 printk(KERN_INFO "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
674 pci_mem_start, gapstart, gapsize);