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>
30 * PFN of last memory page.
32 unsigned long end_pfn;
33 EXPORT_SYMBOL(end_pfn);
36 * end_pfn only includes RAM, while end_pfn_map includes all e820 entries.
37 * The direct mapping extends to end_pfn_map, so that we can directly access
38 * apertures, ACPI and other tables without having to play with fixmaps.
40 unsigned long end_pfn_map;
43 * Last pfn which the user wants to use.
45 unsigned long end_user_pfn = MAXMEM>>PAGE_SHIFT;
47 extern struct resource code_resource, data_resource;
49 /* Check for some hardcoded bad areas that early boot is not allowed to touch */
50 static inline int bad_addr(unsigned long *addrp, unsigned long size)
52 unsigned long addr = *addrp, last = addr + size;
54 /* various gunk below that needed for SMP startup */
60 /* direct mapping tables of the kernel */
61 if (last >= table_start<<PAGE_SHIFT && addr < table_end<<PAGE_SHIFT) {
62 *addrp = table_end << PAGE_SHIFT;
67 #ifdef CONFIG_BLK_DEV_INITRD
68 if (LOADER_TYPE && INITRD_START && last >= INITRD_START &&
69 addr < INITRD_START+INITRD_SIZE) {
70 *addrp = INITRD_START + INITRD_SIZE;
74 /* kernel code + 640k memory hole (later should not be needed, but
75 be paranoid for now) */
76 if (last >= 640*1024 && addr < __pa_symbol(&_end)) {
77 *addrp = __pa_symbol(&_end);
80 /* XXX ramdisk image here? */
84 int __init e820_mapped(unsigned long start, unsigned long end, unsigned type)
87 for (i = 0; i < e820.nr_map; i++) {
88 struct e820entry *ei = &e820.map[i];
89 if (type && ei->type != type)
91 if (ei->addr >= end || ei->addr + ei->size <= start)
99 * Find a free area in a specific range.
101 unsigned long __init find_e820_area(unsigned long start, unsigned long end, unsigned size)
104 for (i = 0; i < e820.nr_map; i++) {
105 struct e820entry *ei = &e820.map[i];
106 unsigned long addr = ei->addr, last;
107 if (ei->type != E820_RAM)
111 if (addr > ei->addr + ei->size)
113 while (bad_addr(&addr, size) && addr+size < ei->addr + ei->size)
116 if (last > ei->addr + ei->size)
126 * Free bootmem based on the e820 table for a node.
128 void __init e820_bootmem_free(pg_data_t *pgdat, unsigned long start,unsigned long end)
131 for (i = 0; i < e820.nr_map; i++) {
132 struct e820entry *ei = &e820.map[i];
133 unsigned long last, addr;
135 if (ei->type != E820_RAM ||
136 ei->addr+ei->size <= start ||
140 addr = round_up(ei->addr, PAGE_SIZE);
144 last = round_down(ei->addr + ei->size, PAGE_SIZE);
148 if (last > addr && last-addr >= PAGE_SIZE)
149 free_bootmem_node(pgdat, addr, last-addr);
154 * Find the highest page frame number we have available
156 unsigned long __init e820_end_of_ram(void)
159 unsigned long end_pfn = 0;
161 for (i = 0; i < e820.nr_map; i++) {
162 struct e820entry *ei = &e820.map[i];
163 unsigned long start, end;
165 start = round_up(ei->addr, PAGE_SIZE);
166 end = round_down(ei->addr + ei->size, PAGE_SIZE);
169 if (ei->type == E820_RAM) {
170 if (end > end_pfn<<PAGE_SHIFT)
171 end_pfn = end>>PAGE_SHIFT;
173 if (end > end_pfn_map<<PAGE_SHIFT)
174 end_pfn_map = end>>PAGE_SHIFT;
178 if (end_pfn > end_pfn_map)
179 end_pfn_map = end_pfn;
180 if (end_pfn_map > MAXMEM>>PAGE_SHIFT)
181 end_pfn_map = MAXMEM>>PAGE_SHIFT;
182 if (end_pfn > end_user_pfn)
183 end_pfn = end_user_pfn;
184 if (end_pfn > end_pfn_map)
185 end_pfn = end_pfn_map;
191 * Compute how much memory is missing in a range.
192 * Unlike the other functions in this file the arguments are in page numbers.
195 e820_hole_size(unsigned long start_pfn, unsigned long end_pfn)
197 unsigned long ram = 0;
198 unsigned long start = start_pfn << PAGE_SHIFT;
199 unsigned long end = end_pfn << PAGE_SHIFT;
201 for (i = 0; i < e820.nr_map; i++) {
202 struct e820entry *ei = &e820.map[i];
203 unsigned long last, addr;
205 if (ei->type != E820_RAM ||
206 ei->addr+ei->size <= start ||
210 addr = round_up(ei->addr, PAGE_SIZE);
214 last = round_down(ei->addr + ei->size, PAGE_SIZE);
221 return ((end - start) - ram) >> PAGE_SHIFT;
225 * Mark e820 reserved areas as busy for the resource manager.
227 void __init e820_reserve_resources(void)
230 for (i = 0; i < e820.nr_map; i++) {
231 struct resource *res;
232 res = alloc_bootmem_low(sizeof(struct resource));
233 switch (e820.map[i].type) {
234 case E820_RAM: res->name = "System RAM"; break;
235 case E820_ACPI: res->name = "ACPI Tables"; break;
236 case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
237 default: res->name = "reserved";
239 res->start = e820.map[i].addr;
240 res->end = res->start + e820.map[i].size - 1;
241 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
242 request_resource(&iomem_resource, res);
243 if (e820.map[i].type == E820_RAM) {
245 * We don't know which RAM region contains kernel data,
246 * so we try it repeatedly and let the resource manager
249 request_resource(res, &code_resource);
250 request_resource(res, &data_resource);
252 request_resource(res, &crashk_res);
259 * Add a memory region to the kernel e820 map.
261 void __init add_memory_region(unsigned long start, unsigned long size, int type)
266 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
270 e820.map[x].addr = start;
271 e820.map[x].size = size;
272 e820.map[x].type = type;
276 void __init e820_print_map(char *who)
280 for (i = 0; i < e820.nr_map; i++) {
281 printk(" %s: %016Lx - %016Lx ", who,
282 (unsigned long long) e820.map[i].addr,
283 (unsigned long long) (e820.map[i].addr + e820.map[i].size));
284 switch (e820.map[i].type) {
285 case E820_RAM: printk("(usable)\n");
288 printk("(reserved)\n");
291 printk("(ACPI data)\n");
294 printk("(ACPI NVS)\n");
296 default: printk("type %u\n", e820.map[i].type);
303 * Sanitize the BIOS e820 map.
305 * Some e820 responses include overlapping entries. The following
306 * replaces the original e820 map with a new one, removing overlaps.
309 static int __init sanitize_e820_map(struct e820entry * biosmap, char * pnr_map)
311 struct change_member {
312 struct e820entry *pbios; /* pointer to original bios entry */
313 unsigned long long addr; /* address for this change point */
315 static struct change_member change_point_list[2*E820MAX] __initdata;
316 static struct change_member *change_point[2*E820MAX] __initdata;
317 static struct e820entry *overlap_list[E820MAX] __initdata;
318 static struct e820entry new_bios[E820MAX] __initdata;
319 struct change_member *change_tmp;
320 unsigned long current_type, last_type;
321 unsigned long long last_addr;
322 int chgidx, still_changing;
325 int old_nr, new_nr, chg_nr;
329 Visually we're performing the following (1,2,3,4 = memory types)...
331 Sample memory map (w/overlaps):
332 ____22__________________
333 ______________________4_
334 ____1111________________
335 _44_____________________
336 11111111________________
337 ____________________33__
338 ___________44___________
339 __________33333_________
340 ______________22________
341 ___________________2222_
342 _________111111111______
343 _____________________11_
344 _________________4______
346 Sanitized equivalent (no overlap):
347 1_______________________
348 _44_____________________
349 ___1____________________
350 ____22__________________
351 ______11________________
352 _________1______________
353 __________3_____________
354 ___________44___________
355 _____________33_________
356 _______________2________
357 ________________1_______
358 _________________4______
359 ___________________2____
360 ____________________33__
361 ______________________4_
364 /* if there's only one memory region, don't bother */
370 /* bail out if we find any unreasonable addresses in bios map */
371 for (i=0; i<old_nr; i++)
372 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
375 /* create pointers for initial change-point information (for sorting) */
376 for (i=0; i < 2*old_nr; i++)
377 change_point[i] = &change_point_list[i];
379 /* record all known change-points (starting and ending addresses),
380 omitting those that are for empty memory regions */
382 for (i=0; i < old_nr; i++) {
383 if (biosmap[i].size != 0) {
384 change_point[chgidx]->addr = biosmap[i].addr;
385 change_point[chgidx++]->pbios = &biosmap[i];
386 change_point[chgidx]->addr = biosmap[i].addr + biosmap[i].size;
387 change_point[chgidx++]->pbios = &biosmap[i];
392 /* sort change-point list by memory addresses (low -> high) */
394 while (still_changing) {
396 for (i=1; i < chg_nr; i++) {
397 /* if <current_addr> > <last_addr>, swap */
398 /* or, if current=<start_addr> & last=<end_addr>, swap */
399 if ((change_point[i]->addr < change_point[i-1]->addr) ||
400 ((change_point[i]->addr == change_point[i-1]->addr) &&
401 (change_point[i]->addr == change_point[i]->pbios->addr) &&
402 (change_point[i-1]->addr != change_point[i-1]->pbios->addr))
405 change_tmp = change_point[i];
406 change_point[i] = change_point[i-1];
407 change_point[i-1] = change_tmp;
413 /* create a new bios memory map, removing overlaps */
414 overlap_entries=0; /* number of entries in the overlap table */
415 new_bios_entry=0; /* index for creating new bios map entries */
416 last_type = 0; /* start with undefined memory type */
417 last_addr = 0; /* start with 0 as last starting address */
418 /* loop through change-points, determining affect on the new bios map */
419 for (chgidx=0; chgidx < chg_nr; chgidx++)
421 /* keep track of all overlapping bios entries */
422 if (change_point[chgidx]->addr == change_point[chgidx]->pbios->addr)
424 /* add map entry to overlap list (> 1 entry implies an overlap) */
425 overlap_list[overlap_entries++]=change_point[chgidx]->pbios;
429 /* remove entry from list (order independent, so swap with last) */
430 for (i=0; i<overlap_entries; i++)
432 if (overlap_list[i] == change_point[chgidx]->pbios)
433 overlap_list[i] = overlap_list[overlap_entries-1];
437 /* if there are overlapping entries, decide which "type" to use */
438 /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */
440 for (i=0; i<overlap_entries; i++)
441 if (overlap_list[i]->type > current_type)
442 current_type = overlap_list[i]->type;
443 /* continue building up new bios map based on this information */
444 if (current_type != last_type) {
445 if (last_type != 0) {
446 new_bios[new_bios_entry].size =
447 change_point[chgidx]->addr - last_addr;
448 /* move forward only if the new size was non-zero */
449 if (new_bios[new_bios_entry].size != 0)
450 if (++new_bios_entry >= E820MAX)
451 break; /* no more space left for new bios entries */
453 if (current_type != 0) {
454 new_bios[new_bios_entry].addr = change_point[chgidx]->addr;
455 new_bios[new_bios_entry].type = current_type;
456 last_addr=change_point[chgidx]->addr;
458 last_type = current_type;
461 new_nr = new_bios_entry; /* retain count for new bios entries */
463 /* copy new bios mapping into original location */
464 memcpy(biosmap, new_bios, new_nr*sizeof(struct e820entry));
471 * Copy the BIOS e820 map into a safe place.
473 * Sanity-check it while we're at it..
475 * If we're lucky and live on a modern system, the setup code
476 * will have given us a memory map that we can use to properly
477 * set up memory. If we aren't, we'll fake a memory map.
479 * We check to see that the memory map contains at least 2 elements
480 * before we'll use it, because the detection code in setup.S may
481 * not be perfect and most every PC known to man has two memory
482 * regions: one from 0 to 640k, and one from 1mb up. (The IBM
483 * thinkpad 560x, for example, does not cooperate with the memory
486 static int __init copy_e820_map(struct e820entry * biosmap, int nr_map)
488 /* Only one memory region (or negative)? Ignore it */
493 unsigned long start = biosmap->addr;
494 unsigned long size = biosmap->size;
495 unsigned long end = start + size;
496 unsigned long type = biosmap->type;
498 /* Overflow in 64 bits? Ignore the memory map. */
503 * Some BIOSes claim RAM in the 640k - 1M region.
504 * Not right. Fix it up.
506 * This should be removed on Hammer which is supposed to not
507 * have non e820 covered ISA mappings there, but I had some strange
508 * problems so it stays for now. -AK
510 if (type == E820_RAM) {
511 if (start < 0x100000ULL && end > 0xA0000ULL) {
512 if (start < 0xA0000ULL)
513 add_memory_region(start, 0xA0000ULL-start, type);
514 if (end <= 0x100000ULL)
521 add_memory_region(start, size, type);
522 } while (biosmap++,--nr_map);
526 void __init setup_memory_region(void)
528 char *who = "BIOS-e820";
531 * Try to copy the BIOS-supplied E820-map.
533 * Otherwise fake a memory map; one section from 0k->640k,
534 * the next section from 1mb->appropriate_mem_k
536 sanitize_e820_map(E820_MAP, &E820_MAP_NR);
537 if (copy_e820_map(E820_MAP, E820_MAP_NR) < 0) {
538 unsigned long mem_size;
540 /* compare results from other methods and take the greater */
541 if (ALT_MEM_K < EXT_MEM_K) {
542 mem_size = EXT_MEM_K;
545 mem_size = ALT_MEM_K;
550 add_memory_region(0, LOWMEMSIZE(), E820_RAM);
551 add_memory_region(HIGH_MEMORY, mem_size << 10, E820_RAM);
553 printk(KERN_INFO "BIOS-provided physical RAM map:\n");
557 void __init parse_memopt(char *p, char **from)
559 end_user_pfn = memparse(p, from);
560 end_user_pfn >>= PAGE_SHIFT;
563 unsigned long pci_mem_start = 0xaeedbabe;
566 * Search for the biggest gap in the low 32 bits of the e820
567 * memory space. We pass this space to PCI to assign MMIO resources
568 * for hotplug or unconfigured devices in.
569 * Hopefully the BIOS let enough space left.
571 __init void e820_setup_gap(void)
573 unsigned long gapstart, gapsize, round;
578 last = 0x100000000ull;
579 gapstart = 0x10000000;
583 unsigned long long start = e820.map[i].addr;
584 unsigned long long end = start + e820.map[i].size;
587 * Since "last" is at most 4GB, we know we'll
588 * fit in 32 bits if this condition is true
591 unsigned long gap = last - end;
604 gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
605 printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit address range\n"
606 KERN_ERR "PCI: Unassigned devices with 32bit resource registers may break!\n");
610 * See how much we want to round up: start off with
611 * rounding to the next 1MB area.
614 while ((gapsize >> 4) > round)
616 /* Fun with two's complement */
617 pci_mem_start = (gapstart + round) & -round;
619 printk(KERN_INFO "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
620 pci_mem_start, gapstart, gapsize);