[PATCH] Avoid console spam with ext3 aborted journal.
[linux-2.6/verdex.git] / arch / x86_64 / kernel / e820.c
blob7c154dfff64aa406b15294236eee46a738837ee8
1 /*
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 <asm/page.h>
20 #include <asm/e820.h>
21 #include <asm/proto.h>
22 #include <asm/bootsetup.h>
24 extern char _end[];
26 /*
27 * PFN of last memory page.
29 unsigned long end_pfn;
31 /*
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.
35 */
36 unsigned long end_pfn_map;
38 /*
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 */
51 if (addr < 0x8000) {
52 *addrp = 0x8000;
53 return 1;
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;
59 return 1;
62 /* initrd */
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;
67 return 1;
69 #endif
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);
74 return 1;
76 /* XXX ramdisk image here? */
77 return 0;
80 int __init e820_mapped(unsigned long start, unsigned long end, unsigned type)
82 int i;
83 for (i = 0; i < e820.nr_map; i++) {
84 struct e820entry *ei = &e820.map[i];
85 if (type && ei->type != type)
86 continue;
87 if (ei->addr >= end || ei->addr + ei->size < start)
88 continue;
89 return 1;
91 return 0;
94 /*
95 * Find a free area in a specific range.
96 */
97 unsigned long __init find_e820_area(unsigned long start, unsigned long end, unsigned size)
99 int i;
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)
104 continue;
105 if (addr < start)
106 addr = start;
107 if (addr > ei->addr + ei->size)
108 continue;
109 while (bad_addr(&addr, size) && addr+size < ei->addr + ei->size)
111 last = addr + size;
112 if (last > ei->addr + ei->size)
113 continue;
114 if (last > end)
115 continue;
116 return addr;
118 return -1UL;
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)
126 int i;
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 ||
133 ei->addr > end)
134 continue;
136 addr = round_up(ei->addr, PAGE_SIZE);
137 if (addr < start)
138 addr = start;
140 last = round_down(ei->addr + ei->size, PAGE_SIZE);
141 if (last >= end)
142 last = end;
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)
154 int i;
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);
163 if (start >= end)
164 continue;
165 if (ei->type == E820_RAM) {
166 if (end > end_pfn<<PAGE_SHIFT)
167 end_pfn = end>>PAGE_SHIFT;
168 } else {
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;
183 return end_pfn;
187 * Mark e820 reserved areas as busy for the resource manager.
189 void __init e820_reserve_resources(void)
191 int i;
192 for (i = 0; i < e820.nr_map; i++) {
193 struct resource *res;
194 if (e820.map[i].addr + e820.map[i].size > 0x100000000ULL)
195 continue;
196 res = alloc_bootmem_low(sizeof(struct resource));
197 switch (e820.map[i].type) {
198 case E820_RAM: res->name = "System RAM"; break;
199 case E820_ACPI: res->name = "ACPI Tables"; break;
200 case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
201 default: res->name = "reserved";
203 res->start = e820.map[i].addr;
204 res->end = res->start + e820.map[i].size - 1;
205 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
206 request_resource(&iomem_resource, res);
207 if (e820.map[i].type == E820_RAM) {
209 * We don't know which RAM region contains kernel data,
210 * so we try it repeatedly and let the resource manager
211 * test it.
213 request_resource(res, &code_resource);
214 request_resource(res, &data_resource);
220 * Add a memory region to the kernel e820 map.
222 void __init add_memory_region(unsigned long start, unsigned long size, int type)
224 int x = e820.nr_map;
226 if (x == E820MAX) {
227 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
228 return;
231 e820.map[x].addr = start;
232 e820.map[x].size = size;
233 e820.map[x].type = type;
234 e820.nr_map++;
237 void __init e820_print_map(char *who)
239 int i;
241 for (i = 0; i < e820.nr_map; i++) {
242 printk(" %s: %016Lx - %016Lx ", who,
243 (unsigned long long) e820.map[i].addr,
244 (unsigned long long) (e820.map[i].addr + e820.map[i].size));
245 switch (e820.map[i].type) {
246 case E820_RAM: printk("(usable)\n");
247 break;
248 case E820_RESERVED:
249 printk("(reserved)\n");
250 break;
251 case E820_ACPI:
252 printk("(ACPI data)\n");
253 break;
254 case E820_NVS:
255 printk("(ACPI NVS)\n");
256 break;
257 default: printk("type %u\n", e820.map[i].type);
258 break;
264 * Sanitize the BIOS e820 map.
266 * Some e820 responses include overlapping entries. The following
267 * replaces the original e820 map with a new one, removing overlaps.
270 static int __init sanitize_e820_map(struct e820entry * biosmap, char * pnr_map)
272 struct change_member {
273 struct e820entry *pbios; /* pointer to original bios entry */
274 unsigned long long addr; /* address for this change point */
276 static struct change_member change_point_list[2*E820MAX] __initdata;
277 static struct change_member *change_point[2*E820MAX] __initdata;
278 static struct e820entry *overlap_list[E820MAX] __initdata;
279 static struct e820entry new_bios[E820MAX] __initdata;
280 struct change_member *change_tmp;
281 unsigned long current_type, last_type;
282 unsigned long long last_addr;
283 int chgidx, still_changing;
284 int overlap_entries;
285 int new_bios_entry;
286 int old_nr, new_nr, chg_nr;
287 int i;
290 Visually we're performing the following (1,2,3,4 = memory types)...
292 Sample memory map (w/overlaps):
293 ____22__________________
294 ______________________4_
295 ____1111________________
296 _44_____________________
297 11111111________________
298 ____________________33__
299 ___________44___________
300 __________33333_________
301 ______________22________
302 ___________________2222_
303 _________111111111______
304 _____________________11_
305 _________________4______
307 Sanitized equivalent (no overlap):
308 1_______________________
309 _44_____________________
310 ___1____________________
311 ____22__________________
312 ______11________________
313 _________1______________
314 __________3_____________
315 ___________44___________
316 _____________33_________
317 _______________2________
318 ________________1_______
319 _________________4______
320 ___________________2____
321 ____________________33__
322 ______________________4_
325 /* if there's only one memory region, don't bother */
326 if (*pnr_map < 2)
327 return -1;
329 old_nr = *pnr_map;
331 /* bail out if we find any unreasonable addresses in bios map */
332 for (i=0; i<old_nr; i++)
333 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
334 return -1;
336 /* create pointers for initial change-point information (for sorting) */
337 for (i=0; i < 2*old_nr; i++)
338 change_point[i] = &change_point_list[i];
340 /* record all known change-points (starting and ending addresses),
341 omitting those that are for empty memory regions */
342 chgidx = 0;
343 for (i=0; i < old_nr; i++) {
344 if (biosmap[i].size != 0) {
345 change_point[chgidx]->addr = biosmap[i].addr;
346 change_point[chgidx++]->pbios = &biosmap[i];
347 change_point[chgidx]->addr = biosmap[i].addr + biosmap[i].size;
348 change_point[chgidx++]->pbios = &biosmap[i];
351 chg_nr = chgidx;
353 /* sort change-point list by memory addresses (low -> high) */
354 still_changing = 1;
355 while (still_changing) {
356 still_changing = 0;
357 for (i=1; i < chg_nr; i++) {
358 /* if <current_addr> > <last_addr>, swap */
359 /* or, if current=<start_addr> & last=<end_addr>, swap */
360 if ((change_point[i]->addr < change_point[i-1]->addr) ||
361 ((change_point[i]->addr == change_point[i-1]->addr) &&
362 (change_point[i]->addr == change_point[i]->pbios->addr) &&
363 (change_point[i-1]->addr != change_point[i-1]->pbios->addr))
366 change_tmp = change_point[i];
367 change_point[i] = change_point[i-1];
368 change_point[i-1] = change_tmp;
369 still_changing=1;
374 /* create a new bios memory map, removing overlaps */
375 overlap_entries=0; /* number of entries in the overlap table */
376 new_bios_entry=0; /* index for creating new bios map entries */
377 last_type = 0; /* start with undefined memory type */
378 last_addr = 0; /* start with 0 as last starting address */
379 /* loop through change-points, determining affect on the new bios map */
380 for (chgidx=0; chgidx < chg_nr; chgidx++)
382 /* keep track of all overlapping bios entries */
383 if (change_point[chgidx]->addr == change_point[chgidx]->pbios->addr)
385 /* add map entry to overlap list (> 1 entry implies an overlap) */
386 overlap_list[overlap_entries++]=change_point[chgidx]->pbios;
388 else
390 /* remove entry from list (order independent, so swap with last) */
391 for (i=0; i<overlap_entries; i++)
393 if (overlap_list[i] == change_point[chgidx]->pbios)
394 overlap_list[i] = overlap_list[overlap_entries-1];
396 overlap_entries--;
398 /* if there are overlapping entries, decide which "type" to use */
399 /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */
400 current_type = 0;
401 for (i=0; i<overlap_entries; i++)
402 if (overlap_list[i]->type > current_type)
403 current_type = overlap_list[i]->type;
404 /* continue building up new bios map based on this information */
405 if (current_type != last_type) {
406 if (last_type != 0) {
407 new_bios[new_bios_entry].size =
408 change_point[chgidx]->addr - last_addr;
409 /* move forward only if the new size was non-zero */
410 if (new_bios[new_bios_entry].size != 0)
411 if (++new_bios_entry >= E820MAX)
412 break; /* no more space left for new bios entries */
414 if (current_type != 0) {
415 new_bios[new_bios_entry].addr = change_point[chgidx]->addr;
416 new_bios[new_bios_entry].type = current_type;
417 last_addr=change_point[chgidx]->addr;
419 last_type = current_type;
422 new_nr = new_bios_entry; /* retain count for new bios entries */
424 /* copy new bios mapping into original location */
425 memcpy(biosmap, new_bios, new_nr*sizeof(struct e820entry));
426 *pnr_map = new_nr;
428 return 0;
432 * Copy the BIOS e820 map into a safe place.
434 * Sanity-check it while we're at it..
436 * If we're lucky and live on a modern system, the setup code
437 * will have given us a memory map that we can use to properly
438 * set up memory. If we aren't, we'll fake a memory map.
440 * We check to see that the memory map contains at least 2 elements
441 * before we'll use it, because the detection code in setup.S may
442 * not be perfect and most every PC known to man has two memory
443 * regions: one from 0 to 640k, and one from 1mb up. (The IBM
444 * thinkpad 560x, for example, does not cooperate with the memory
445 * detection code.)
447 static int __init copy_e820_map(struct e820entry * biosmap, int nr_map)
449 /* Only one memory region (or negative)? Ignore it */
450 if (nr_map < 2)
451 return -1;
453 do {
454 unsigned long start = biosmap->addr;
455 unsigned long size = biosmap->size;
456 unsigned long end = start + size;
457 unsigned long type = biosmap->type;
459 /* Overflow in 64 bits? Ignore the memory map. */
460 if (start > end)
461 return -1;
464 * Some BIOSes claim RAM in the 640k - 1M region.
465 * Not right. Fix it up.
467 * This should be removed on Hammer which is supposed to not
468 * have non e820 covered ISA mappings there, but I had some strange
469 * problems so it stays for now. -AK
471 if (type == E820_RAM) {
472 if (start < 0x100000ULL && end > 0xA0000ULL) {
473 if (start < 0xA0000ULL)
474 add_memory_region(start, 0xA0000ULL-start, type);
475 if (end <= 0x100000ULL)
476 continue;
477 start = 0x100000ULL;
478 size = end - start;
482 add_memory_region(start, size, type);
483 } while (biosmap++,--nr_map);
484 return 0;
487 void __init setup_memory_region(void)
489 char *who = "BIOS-e820";
492 * Try to copy the BIOS-supplied E820-map.
494 * Otherwise fake a memory map; one section from 0k->640k,
495 * the next section from 1mb->appropriate_mem_k
497 sanitize_e820_map(E820_MAP, &E820_MAP_NR);
498 if (copy_e820_map(E820_MAP, E820_MAP_NR) < 0) {
499 unsigned long mem_size;
501 /* compare results from other methods and take the greater */
502 if (ALT_MEM_K < EXT_MEM_K) {
503 mem_size = EXT_MEM_K;
504 who = "BIOS-88";
505 } else {
506 mem_size = ALT_MEM_K;
507 who = "BIOS-e801";
510 e820.nr_map = 0;
511 add_memory_region(0, LOWMEMSIZE(), E820_RAM);
512 add_memory_region(HIGH_MEMORY, mem_size << 10, E820_RAM);
514 printk(KERN_INFO "BIOS-provided physical RAM map:\n");
515 e820_print_map(who);
518 void __init parse_memopt(char *p, char **from)
520 end_user_pfn = memparse(p, from);
521 end_user_pfn >>= PAGE_SHIFT;
524 unsigned long pci_mem_start = 0xaeedbabe;
527 * Search for the biggest gap in the low 32 bits of the e820
528 * memory space. We pass this space to PCI to assign MMIO resources
529 * for hotplug or unconfigured devices in.
530 * Hopefully the BIOS let enough space left.
532 __init void e820_setup_gap(void)
534 unsigned long gapstart, gapsize;
535 unsigned long last;
536 int i;
537 int found = 0;
539 last = 0x100000000ull;
540 gapstart = 0x10000000;
541 gapsize = 0x400000;
542 i = e820.nr_map;
543 while (--i >= 0) {
544 unsigned long long start = e820.map[i].addr;
545 unsigned long long end = start + e820.map[i].size;
548 * Since "last" is at most 4GB, we know we'll
549 * fit in 32 bits if this condition is true
551 if (last > end) {
552 unsigned long gap = last - end;
554 if (gap > gapsize) {
555 gapsize = gap;
556 gapstart = end;
557 found = 1;
560 if (start < last)
561 last = start;
564 if (!found) {
565 gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
566 printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit address range\n"
567 KERN_ERR "PCI: Unassigned devices with 32bit resource registers may break!\n");
571 * Start allocating dynamic PCI memory a bit into the gap,
572 * aligned up to the nearest megabyte.
574 * Question: should we try to pad it up a bit (do something
575 * like " + (gapsize >> 3)" in there too?). We now have the
576 * technology.
578 pci_mem_start = (gapstart + 0xfffff) & ~0xfffff;
580 printk(KERN_INFO "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
581 pci_mem_start, gapstart, gapsize);