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>
21 #include <asm/proto.h>
22 #include <asm/bootsetup.h>
27 * PFN of last memory page.
29 unsigned long end_pfn;
32 * end_pfn only includes RAM, while end_pfn_map includes all e820 entries.
33 * The direct mapping extends to end_pfn_map, so that we can directly access
34 * apertures, ACPI and other tables without having to play with fixmaps.
36 unsigned long end_pfn_map;
39 * Last pfn which the user wants to use.
41 unsigned long end_user_pfn = MAXMEM>>PAGE_SHIFT;
43 extern struct resource code_resource, data_resource;
45 /* Check for some hardcoded bad areas that early boot is not allowed to touch */
46 static inline int bad_addr(unsigned long *addrp, unsigned long size)
48 unsigned long addr = *addrp, last = addr + size;
50 /* various gunk below that needed for SMP startup */
56 /* direct mapping tables of the kernel */
57 if (last >= table_start<<PAGE_SHIFT && addr < table_end<<PAGE_SHIFT) {
58 *addrp = table_end << PAGE_SHIFT;
63 #ifdef CONFIG_BLK_DEV_INITRD
64 if (LOADER_TYPE && INITRD_START && last >= INITRD_START &&
65 addr < INITRD_START+INITRD_SIZE) {
66 *addrp = INITRD_START + INITRD_SIZE;
70 /* kernel code + 640k memory hole (later should not be needed, but
71 be paranoid for now) */
72 if (last >= 640*1024 && addr < __pa_symbol(&_end)) {
73 *addrp = __pa_symbol(&_end);
76 /* XXX ramdisk image here? */
80 int __init e820_mapped(unsigned long start, unsigned long end, unsigned type)
83 for (i = 0; i < e820.nr_map; i++) {
84 struct e820entry *ei = &e820.map[i];
85 if (type && ei->type != type)
87 if (ei->addr >= end || ei->addr + ei->size < start)
95 * Find a free area in a specific range.
97 unsigned long __init find_e820_area(unsigned long start, unsigned long end, unsigned size)
100 for (i = 0; i < e820.nr_map; i++) {
101 struct e820entry *ei = &e820.map[i];
102 unsigned long addr = ei->addr, last;
103 if (ei->type != E820_RAM)
107 if (addr > ei->addr + ei->size)
109 while (bad_addr(&addr, size) && addr+size < ei->addr + ei->size)
112 if (last > ei->addr + ei->size)
122 * Free bootmem based on the e820 table for a node.
124 void __init e820_bootmem_free(pg_data_t *pgdat, unsigned long start,unsigned long end)
127 for (i = 0; i < e820.nr_map; i++) {
128 struct e820entry *ei = &e820.map[i];
129 unsigned long last, addr;
131 if (ei->type != E820_RAM ||
132 ei->addr+ei->size <= start ||
136 addr = round_up(ei->addr, PAGE_SIZE);
140 last = round_down(ei->addr + ei->size, PAGE_SIZE);
144 if (last > addr && last-addr >= PAGE_SIZE)
145 free_bootmem_node(pgdat, addr, last-addr);
150 * Find the highest page frame number we have available
152 unsigned long __init e820_end_of_ram(void)
155 unsigned long end_pfn = 0;
157 for (i = 0; i < e820.nr_map; i++) {
158 struct e820entry *ei = &e820.map[i];
159 unsigned long start, end;
161 start = round_up(ei->addr, PAGE_SIZE);
162 end = round_down(ei->addr + ei->size, PAGE_SIZE);
165 if (ei->type == E820_RAM) {
166 if (end > end_pfn<<PAGE_SHIFT)
167 end_pfn = end>>PAGE_SHIFT;
169 if (end > end_pfn_map<<PAGE_SHIFT)
170 end_pfn_map = end>>PAGE_SHIFT;
174 if (end_pfn > end_pfn_map)
175 end_pfn_map = end_pfn;
176 if (end_pfn_map > MAXMEM>>PAGE_SHIFT)
177 end_pfn_map = MAXMEM>>PAGE_SHIFT;
178 if (end_pfn > end_user_pfn)
179 end_pfn = end_user_pfn;
180 if (end_pfn > end_pfn_map)
181 end_pfn = end_pfn_map;
187 * Mark e820 reserved areas as busy for the resource manager.
189 void __init e820_reserve_resources(void)
192 for (i = 0; i < e820.nr_map; i++) {
193 struct resource *res;
194 res = alloc_bootmem_low(sizeof(struct resource));
195 switch (e820.map[i].type) {
196 case E820_RAM: res->name = "System RAM"; break;
197 case E820_ACPI: res->name = "ACPI Tables"; break;
198 case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
199 default: res->name = "reserved";
201 res->start = e820.map[i].addr;
202 res->end = res->start + e820.map[i].size - 1;
203 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
204 request_resource(&iomem_resource, res);
205 if (e820.map[i].type == E820_RAM) {
207 * We don't know which RAM region contains kernel data,
208 * so we try it repeatedly and let the resource manager
211 request_resource(res, &code_resource);
212 request_resource(res, &data_resource);
218 * Add a memory region to the kernel e820 map.
220 void __init add_memory_region(unsigned long start, unsigned long size, int type)
225 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
229 e820.map[x].addr = start;
230 e820.map[x].size = size;
231 e820.map[x].type = type;
235 void __init e820_print_map(char *who)
239 for (i = 0; i < e820.nr_map; i++) {
240 printk(" %s: %016Lx - %016Lx ", who,
241 (unsigned long long) e820.map[i].addr,
242 (unsigned long long) (e820.map[i].addr + e820.map[i].size));
243 switch (e820.map[i].type) {
244 case E820_RAM: printk("(usable)\n");
247 printk("(reserved)\n");
250 printk("(ACPI data)\n");
253 printk("(ACPI NVS)\n");
255 default: printk("type %u\n", e820.map[i].type);
262 * Sanitize the BIOS e820 map.
264 * Some e820 responses include overlapping entries. The following
265 * replaces the original e820 map with a new one, removing overlaps.
268 static int __init sanitize_e820_map(struct e820entry * biosmap, char * pnr_map)
270 struct change_member {
271 struct e820entry *pbios; /* pointer to original bios entry */
272 unsigned long long addr; /* address for this change point */
274 static struct change_member change_point_list[2*E820MAX] __initdata;
275 static struct change_member *change_point[2*E820MAX] __initdata;
276 static struct e820entry *overlap_list[E820MAX] __initdata;
277 static struct e820entry new_bios[E820MAX] __initdata;
278 struct change_member *change_tmp;
279 unsigned long current_type, last_type;
280 unsigned long long last_addr;
281 int chgidx, still_changing;
284 int old_nr, new_nr, chg_nr;
288 Visually we're performing the following (1,2,3,4 = memory types)...
290 Sample memory map (w/overlaps):
291 ____22__________________
292 ______________________4_
293 ____1111________________
294 _44_____________________
295 11111111________________
296 ____________________33__
297 ___________44___________
298 __________33333_________
299 ______________22________
300 ___________________2222_
301 _________111111111______
302 _____________________11_
303 _________________4______
305 Sanitized equivalent (no overlap):
306 1_______________________
307 _44_____________________
308 ___1____________________
309 ____22__________________
310 ______11________________
311 _________1______________
312 __________3_____________
313 ___________44___________
314 _____________33_________
315 _______________2________
316 ________________1_______
317 _________________4______
318 ___________________2____
319 ____________________33__
320 ______________________4_
323 /* if there's only one memory region, don't bother */
329 /* bail out if we find any unreasonable addresses in bios map */
330 for (i=0; i<old_nr; i++)
331 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
334 /* create pointers for initial change-point information (for sorting) */
335 for (i=0; i < 2*old_nr; i++)
336 change_point[i] = &change_point_list[i];
338 /* record all known change-points (starting and ending addresses),
339 omitting those that are for empty memory regions */
341 for (i=0; i < old_nr; i++) {
342 if (biosmap[i].size != 0) {
343 change_point[chgidx]->addr = biosmap[i].addr;
344 change_point[chgidx++]->pbios = &biosmap[i];
345 change_point[chgidx]->addr = biosmap[i].addr + biosmap[i].size;
346 change_point[chgidx++]->pbios = &biosmap[i];
351 /* sort change-point list by memory addresses (low -> high) */
353 while (still_changing) {
355 for (i=1; i < chg_nr; i++) {
356 /* if <current_addr> > <last_addr>, swap */
357 /* or, if current=<start_addr> & last=<end_addr>, swap */
358 if ((change_point[i]->addr < change_point[i-1]->addr) ||
359 ((change_point[i]->addr == change_point[i-1]->addr) &&
360 (change_point[i]->addr == change_point[i]->pbios->addr) &&
361 (change_point[i-1]->addr != change_point[i-1]->pbios->addr))
364 change_tmp = change_point[i];
365 change_point[i] = change_point[i-1];
366 change_point[i-1] = change_tmp;
372 /* create a new bios memory map, removing overlaps */
373 overlap_entries=0; /* number of entries in the overlap table */
374 new_bios_entry=0; /* index for creating new bios map entries */
375 last_type = 0; /* start with undefined memory type */
376 last_addr = 0; /* start with 0 as last starting address */
377 /* loop through change-points, determining affect on the new bios map */
378 for (chgidx=0; chgidx < chg_nr; chgidx++)
380 /* keep track of all overlapping bios entries */
381 if (change_point[chgidx]->addr == change_point[chgidx]->pbios->addr)
383 /* add map entry to overlap list (> 1 entry implies an overlap) */
384 overlap_list[overlap_entries++]=change_point[chgidx]->pbios;
388 /* remove entry from list (order independent, so swap with last) */
389 for (i=0; i<overlap_entries; i++)
391 if (overlap_list[i] == change_point[chgidx]->pbios)
392 overlap_list[i] = overlap_list[overlap_entries-1];
396 /* if there are overlapping entries, decide which "type" to use */
397 /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */
399 for (i=0; i<overlap_entries; i++)
400 if (overlap_list[i]->type > current_type)
401 current_type = overlap_list[i]->type;
402 /* continue building up new bios map based on this information */
403 if (current_type != last_type) {
404 if (last_type != 0) {
405 new_bios[new_bios_entry].size =
406 change_point[chgidx]->addr - last_addr;
407 /* move forward only if the new size was non-zero */
408 if (new_bios[new_bios_entry].size != 0)
409 if (++new_bios_entry >= E820MAX)
410 break; /* no more space left for new bios entries */
412 if (current_type != 0) {
413 new_bios[new_bios_entry].addr = change_point[chgidx]->addr;
414 new_bios[new_bios_entry].type = current_type;
415 last_addr=change_point[chgidx]->addr;
417 last_type = current_type;
420 new_nr = new_bios_entry; /* retain count for new bios entries */
422 /* copy new bios mapping into original location */
423 memcpy(biosmap, new_bios, new_nr*sizeof(struct e820entry));
430 * Copy the BIOS e820 map into a safe place.
432 * Sanity-check it while we're at it..
434 * If we're lucky and live on a modern system, the setup code
435 * will have given us a memory map that we can use to properly
436 * set up memory. If we aren't, we'll fake a memory map.
438 * We check to see that the memory map contains at least 2 elements
439 * before we'll use it, because the detection code in setup.S may
440 * not be perfect and most every PC known to man has two memory
441 * regions: one from 0 to 640k, and one from 1mb up. (The IBM
442 * thinkpad 560x, for example, does not cooperate with the memory
445 static int __init copy_e820_map(struct e820entry * biosmap, int nr_map)
447 /* Only one memory region (or negative)? Ignore it */
452 unsigned long start = biosmap->addr;
453 unsigned long size = biosmap->size;
454 unsigned long end = start + size;
455 unsigned long type = biosmap->type;
457 /* Overflow in 64 bits? Ignore the memory map. */
462 * Some BIOSes claim RAM in the 640k - 1M region.
463 * Not right. Fix it up.
465 * This should be removed on Hammer which is supposed to not
466 * have non e820 covered ISA mappings there, but I had some strange
467 * problems so it stays for now. -AK
469 if (type == E820_RAM) {
470 if (start < 0x100000ULL && end > 0xA0000ULL) {
471 if (start < 0xA0000ULL)
472 add_memory_region(start, 0xA0000ULL-start, type);
473 if (end <= 0x100000ULL)
480 add_memory_region(start, size, type);
481 } while (biosmap++,--nr_map);
485 void __init setup_memory_region(void)
487 char *who = "BIOS-e820";
490 * Try to copy the BIOS-supplied E820-map.
492 * Otherwise fake a memory map; one section from 0k->640k,
493 * the next section from 1mb->appropriate_mem_k
495 sanitize_e820_map(E820_MAP, &E820_MAP_NR);
496 if (copy_e820_map(E820_MAP, E820_MAP_NR) < 0) {
497 unsigned long mem_size;
499 /* compare results from other methods and take the greater */
500 if (ALT_MEM_K < EXT_MEM_K) {
501 mem_size = EXT_MEM_K;
504 mem_size = ALT_MEM_K;
509 add_memory_region(0, LOWMEMSIZE(), E820_RAM);
510 add_memory_region(HIGH_MEMORY, mem_size << 10, E820_RAM);
512 printk(KERN_INFO "BIOS-provided physical RAM map:\n");
516 void __init parse_memopt(char *p, char **from)
518 end_user_pfn = memparse(p, from);
519 end_user_pfn >>= PAGE_SHIFT;
522 unsigned long pci_mem_start = 0xaeedbabe;
525 * Search for the biggest gap in the low 32 bits of the e820
526 * memory space. We pass this space to PCI to assign MMIO resources
527 * for hotplug or unconfigured devices in.
528 * Hopefully the BIOS let enough space left.
530 __init void e820_setup_gap(void)
532 unsigned long gapstart, gapsize;
537 last = 0x100000000ull;
538 gapstart = 0x10000000;
542 unsigned long long start = e820.map[i].addr;
543 unsigned long long end = start + e820.map[i].size;
546 * Since "last" is at most 4GB, we know we'll
547 * fit in 32 bits if this condition is true
550 unsigned long gap = last - end;
563 gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
564 printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit address range\n"
565 KERN_ERR "PCI: Unassigned devices with 32bit resource registers may break!\n");
569 * Start allocating dynamic PCI memory a bit into the gap,
570 * aligned up to the nearest megabyte.
572 * Question: should we try to pad it up a bit (do something
573 * like " + (gapsize >> 3)" in there too?). We now have the
576 pci_mem_start = (gapstart + 0xfffff) & ~0xfffff;
578 printk(KERN_INFO "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
579 pci_mem_start, gapstart, gapsize);