[PATCH] w1: fix CRC calculation on bigendian platforms.
[linux-2.6/verdex.git] / arch / x86_64 / kernel / e820.c
blob6ded3a50dfe63682174470acc3e78e2cfdbfd8b5
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 <linux/kexec.h>
20 #include <asm/page.h>
21 #include <asm/e820.h>
22 #include <asm/proto.h>
23 #include <asm/bootsetup.h>
25 extern char _end[];
27 /*
28 * PFN of last memory page.
30 unsigned long end_pfn;
32 /*
33 * end_pfn only includes RAM, while end_pfn_map includes all e820 entries.
34 * The direct mapping extends to end_pfn_map, so that we can directly access
35 * apertures, ACPI and other tables without having to play with fixmaps.
36 */
37 unsigned long end_pfn_map;
39 /*
40 * Last pfn which the user wants to use.
42 unsigned long end_user_pfn = MAXMEM>>PAGE_SHIFT;
44 extern struct resource code_resource, data_resource;
46 /* Check for some hardcoded bad areas that early boot is not allowed to touch */
47 static inline int bad_addr(unsigned long *addrp, unsigned long size)
49 unsigned long addr = *addrp, last = addr + size;
51 /* various gunk below that needed for SMP startup */
52 if (addr < 0x8000) {
53 *addrp = 0x8000;
54 return 1;
57 /* direct mapping tables of the kernel */
58 if (last >= table_start<<PAGE_SHIFT && addr < table_end<<PAGE_SHIFT) {
59 *addrp = table_end << PAGE_SHIFT;
60 return 1;
63 /* initrd */
64 #ifdef CONFIG_BLK_DEV_INITRD
65 if (LOADER_TYPE && INITRD_START && last >= INITRD_START &&
66 addr < INITRD_START+INITRD_SIZE) {
67 *addrp = INITRD_START + INITRD_SIZE;
68 return 1;
70 #endif
71 /* kernel code + 640k memory hole (later should not be needed, but
72 be paranoid for now) */
73 if (last >= 640*1024 && addr < __pa_symbol(&_end)) {
74 *addrp = __pa_symbol(&_end);
75 return 1;
77 /* XXX ramdisk image here? */
78 return 0;
81 int __init e820_mapped(unsigned long start, unsigned long end, unsigned type)
83 int i;
84 for (i = 0; i < e820.nr_map; i++) {
85 struct e820entry *ei = &e820.map[i];
86 if (type && ei->type != type)
87 continue;
88 if (ei->addr >= end || ei->addr + ei->size < start)
89 continue;
90 return 1;
92 return 0;
95 /*
96 * Find a free area in a specific range.
97 */
98 unsigned long __init find_e820_area(unsigned long start, unsigned long end, unsigned size)
100 int i;
101 for (i = 0; i < e820.nr_map; i++) {
102 struct e820entry *ei = &e820.map[i];
103 unsigned long addr = ei->addr, last;
104 if (ei->type != E820_RAM)
105 continue;
106 if (addr < start)
107 addr = start;
108 if (addr > ei->addr + ei->size)
109 continue;
110 while (bad_addr(&addr, size) && addr+size < ei->addr + ei->size)
112 last = addr + size;
113 if (last > ei->addr + ei->size)
114 continue;
115 if (last > end)
116 continue;
117 return addr;
119 return -1UL;
123 * Free bootmem based on the e820 table for a node.
125 void __init e820_bootmem_free(pg_data_t *pgdat, unsigned long start,unsigned long end)
127 int i;
128 for (i = 0; i < e820.nr_map; i++) {
129 struct e820entry *ei = &e820.map[i];
130 unsigned long last, addr;
132 if (ei->type != E820_RAM ||
133 ei->addr+ei->size <= start ||
134 ei->addr > end)
135 continue;
137 addr = round_up(ei->addr, PAGE_SIZE);
138 if (addr < start)
139 addr = start;
141 last = round_down(ei->addr + ei->size, PAGE_SIZE);
142 if (last >= end)
143 last = end;
145 if (last > addr && last-addr >= PAGE_SIZE)
146 free_bootmem_node(pgdat, addr, last-addr);
151 * Find the highest page frame number we have available
153 unsigned long __init e820_end_of_ram(void)
155 int i;
156 unsigned long end_pfn = 0;
158 for (i = 0; i < e820.nr_map; i++) {
159 struct e820entry *ei = &e820.map[i];
160 unsigned long start, end;
162 start = round_up(ei->addr, PAGE_SIZE);
163 end = round_down(ei->addr + ei->size, PAGE_SIZE);
164 if (start >= end)
165 continue;
166 if (ei->type == E820_RAM) {
167 if (end > end_pfn<<PAGE_SHIFT)
168 end_pfn = end>>PAGE_SHIFT;
169 } else {
170 if (end > end_pfn_map<<PAGE_SHIFT)
171 end_pfn_map = end>>PAGE_SHIFT;
175 if (end_pfn > end_pfn_map)
176 end_pfn_map = end_pfn;
177 if (end_pfn_map > MAXMEM>>PAGE_SHIFT)
178 end_pfn_map = MAXMEM>>PAGE_SHIFT;
179 if (end_pfn > end_user_pfn)
180 end_pfn = end_user_pfn;
181 if (end_pfn > end_pfn_map)
182 end_pfn = end_pfn_map;
184 return end_pfn;
188 * Mark e820 reserved areas as busy for the resource manager.
190 void __init e820_reserve_resources(void)
192 int i;
193 for (i = 0; i < e820.nr_map; i++) {
194 struct resource *res;
195 res = alloc_bootmem_low(sizeof(struct resource));
196 switch (e820.map[i].type) {
197 case E820_RAM: res->name = "System RAM"; break;
198 case E820_ACPI: res->name = "ACPI Tables"; break;
199 case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
200 default: res->name = "reserved";
202 res->start = e820.map[i].addr;
203 res->end = res->start + e820.map[i].size - 1;
204 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
205 request_resource(&iomem_resource, res);
206 if (e820.map[i].type == E820_RAM) {
208 * We don't know which RAM region contains kernel data,
209 * so we try it repeatedly and let the resource manager
210 * test it.
212 request_resource(res, &code_resource);
213 request_resource(res, &data_resource);
214 #ifdef CONFIG_KEXEC
215 request_resource(res, &crashk_res);
216 #endif
222 * Add a memory region to the kernel e820 map.
224 void __init add_memory_region(unsigned long start, unsigned long size, int type)
226 int x = e820.nr_map;
228 if (x == E820MAX) {
229 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
230 return;
233 e820.map[x].addr = start;
234 e820.map[x].size = size;
235 e820.map[x].type = type;
236 e820.nr_map++;
239 void __init e820_print_map(char *who)
241 int i;
243 for (i = 0; i < e820.nr_map; i++) {
244 printk(" %s: %016Lx - %016Lx ", who,
245 (unsigned long long) e820.map[i].addr,
246 (unsigned long long) (e820.map[i].addr + e820.map[i].size));
247 switch (e820.map[i].type) {
248 case E820_RAM: printk("(usable)\n");
249 break;
250 case E820_RESERVED:
251 printk("(reserved)\n");
252 break;
253 case E820_ACPI:
254 printk("(ACPI data)\n");
255 break;
256 case E820_NVS:
257 printk("(ACPI NVS)\n");
258 break;
259 default: printk("type %u\n", e820.map[i].type);
260 break;
266 * Sanitize the BIOS e820 map.
268 * Some e820 responses include overlapping entries. The following
269 * replaces the original e820 map with a new one, removing overlaps.
272 static int __init sanitize_e820_map(struct e820entry * biosmap, char * pnr_map)
274 struct change_member {
275 struct e820entry *pbios; /* pointer to original bios entry */
276 unsigned long long addr; /* address for this change point */
278 static struct change_member change_point_list[2*E820MAX] __initdata;
279 static struct change_member *change_point[2*E820MAX] __initdata;
280 static struct e820entry *overlap_list[E820MAX] __initdata;
281 static struct e820entry new_bios[E820MAX] __initdata;
282 struct change_member *change_tmp;
283 unsigned long current_type, last_type;
284 unsigned long long last_addr;
285 int chgidx, still_changing;
286 int overlap_entries;
287 int new_bios_entry;
288 int old_nr, new_nr, chg_nr;
289 int i;
292 Visually we're performing the following (1,2,3,4 = memory types)...
294 Sample memory map (w/overlaps):
295 ____22__________________
296 ______________________4_
297 ____1111________________
298 _44_____________________
299 11111111________________
300 ____________________33__
301 ___________44___________
302 __________33333_________
303 ______________22________
304 ___________________2222_
305 _________111111111______
306 _____________________11_
307 _________________4______
309 Sanitized equivalent (no overlap):
310 1_______________________
311 _44_____________________
312 ___1____________________
313 ____22__________________
314 ______11________________
315 _________1______________
316 __________3_____________
317 ___________44___________
318 _____________33_________
319 _______________2________
320 ________________1_______
321 _________________4______
322 ___________________2____
323 ____________________33__
324 ______________________4_
327 /* if there's only one memory region, don't bother */
328 if (*pnr_map < 2)
329 return -1;
331 old_nr = *pnr_map;
333 /* bail out if we find any unreasonable addresses in bios map */
334 for (i=0; i<old_nr; i++)
335 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
336 return -1;
338 /* create pointers for initial change-point information (for sorting) */
339 for (i=0; i < 2*old_nr; i++)
340 change_point[i] = &change_point_list[i];
342 /* record all known change-points (starting and ending addresses),
343 omitting those that are for empty memory regions */
344 chgidx = 0;
345 for (i=0; i < old_nr; i++) {
346 if (biosmap[i].size != 0) {
347 change_point[chgidx]->addr = biosmap[i].addr;
348 change_point[chgidx++]->pbios = &biosmap[i];
349 change_point[chgidx]->addr = biosmap[i].addr + biosmap[i].size;
350 change_point[chgidx++]->pbios = &biosmap[i];
353 chg_nr = chgidx;
355 /* sort change-point list by memory addresses (low -> high) */
356 still_changing = 1;
357 while (still_changing) {
358 still_changing = 0;
359 for (i=1; i < chg_nr; i++) {
360 /* if <current_addr> > <last_addr>, swap */
361 /* or, if current=<start_addr> & last=<end_addr>, swap */
362 if ((change_point[i]->addr < change_point[i-1]->addr) ||
363 ((change_point[i]->addr == change_point[i-1]->addr) &&
364 (change_point[i]->addr == change_point[i]->pbios->addr) &&
365 (change_point[i-1]->addr != change_point[i-1]->pbios->addr))
368 change_tmp = change_point[i];
369 change_point[i] = change_point[i-1];
370 change_point[i-1] = change_tmp;
371 still_changing=1;
376 /* create a new bios memory map, removing overlaps */
377 overlap_entries=0; /* number of entries in the overlap table */
378 new_bios_entry=0; /* index for creating new bios map entries */
379 last_type = 0; /* start with undefined memory type */
380 last_addr = 0; /* start with 0 as last starting address */
381 /* loop through change-points, determining affect on the new bios map */
382 for (chgidx=0; chgidx < chg_nr; chgidx++)
384 /* keep track of all overlapping bios entries */
385 if (change_point[chgidx]->addr == change_point[chgidx]->pbios->addr)
387 /* add map entry to overlap list (> 1 entry implies an overlap) */
388 overlap_list[overlap_entries++]=change_point[chgidx]->pbios;
390 else
392 /* remove entry from list (order independent, so swap with last) */
393 for (i=0; i<overlap_entries; i++)
395 if (overlap_list[i] == change_point[chgidx]->pbios)
396 overlap_list[i] = overlap_list[overlap_entries-1];
398 overlap_entries--;
400 /* if there are overlapping entries, decide which "type" to use */
401 /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */
402 current_type = 0;
403 for (i=0; i<overlap_entries; i++)
404 if (overlap_list[i]->type > current_type)
405 current_type = overlap_list[i]->type;
406 /* continue building up new bios map based on this information */
407 if (current_type != last_type) {
408 if (last_type != 0) {
409 new_bios[new_bios_entry].size =
410 change_point[chgidx]->addr - last_addr;
411 /* move forward only if the new size was non-zero */
412 if (new_bios[new_bios_entry].size != 0)
413 if (++new_bios_entry >= E820MAX)
414 break; /* no more space left for new bios entries */
416 if (current_type != 0) {
417 new_bios[new_bios_entry].addr = change_point[chgidx]->addr;
418 new_bios[new_bios_entry].type = current_type;
419 last_addr=change_point[chgidx]->addr;
421 last_type = current_type;
424 new_nr = new_bios_entry; /* retain count for new bios entries */
426 /* copy new bios mapping into original location */
427 memcpy(biosmap, new_bios, new_nr*sizeof(struct e820entry));
428 *pnr_map = new_nr;
430 return 0;
434 * Copy the BIOS e820 map into a safe place.
436 * Sanity-check it while we're at it..
438 * If we're lucky and live on a modern system, the setup code
439 * will have given us a memory map that we can use to properly
440 * set up memory. If we aren't, we'll fake a memory map.
442 * We check to see that the memory map contains at least 2 elements
443 * before we'll use it, because the detection code in setup.S may
444 * not be perfect and most every PC known to man has two memory
445 * regions: one from 0 to 640k, and one from 1mb up. (The IBM
446 * thinkpad 560x, for example, does not cooperate with the memory
447 * detection code.)
449 static int __init copy_e820_map(struct e820entry * biosmap, int nr_map)
451 /* Only one memory region (or negative)? Ignore it */
452 if (nr_map < 2)
453 return -1;
455 do {
456 unsigned long start = biosmap->addr;
457 unsigned long size = biosmap->size;
458 unsigned long end = start + size;
459 unsigned long type = biosmap->type;
461 /* Overflow in 64 bits? Ignore the memory map. */
462 if (start > end)
463 return -1;
466 * Some BIOSes claim RAM in the 640k - 1M region.
467 * Not right. Fix it up.
469 * This should be removed on Hammer which is supposed to not
470 * have non e820 covered ISA mappings there, but I had some strange
471 * problems so it stays for now. -AK
473 if (type == E820_RAM) {
474 if (start < 0x100000ULL && end > 0xA0000ULL) {
475 if (start < 0xA0000ULL)
476 add_memory_region(start, 0xA0000ULL-start, type);
477 if (end <= 0x100000ULL)
478 continue;
479 start = 0x100000ULL;
480 size = end - start;
484 add_memory_region(start, size, type);
485 } while (biosmap++,--nr_map);
486 return 0;
489 void __init setup_memory_region(void)
491 char *who = "BIOS-e820";
494 * Try to copy the BIOS-supplied E820-map.
496 * Otherwise fake a memory map; one section from 0k->640k,
497 * the next section from 1mb->appropriate_mem_k
499 sanitize_e820_map(E820_MAP, &E820_MAP_NR);
500 if (copy_e820_map(E820_MAP, E820_MAP_NR) < 0) {
501 unsigned long mem_size;
503 /* compare results from other methods and take the greater */
504 if (ALT_MEM_K < EXT_MEM_K) {
505 mem_size = EXT_MEM_K;
506 who = "BIOS-88";
507 } else {
508 mem_size = ALT_MEM_K;
509 who = "BIOS-e801";
512 e820.nr_map = 0;
513 add_memory_region(0, LOWMEMSIZE(), E820_RAM);
514 add_memory_region(HIGH_MEMORY, mem_size << 10, E820_RAM);
516 printk(KERN_INFO "BIOS-provided physical RAM map:\n");
517 e820_print_map(who);
520 void __init parse_memopt(char *p, char **from)
522 end_user_pfn = memparse(p, from);
523 end_user_pfn >>= PAGE_SHIFT;
526 unsigned long pci_mem_start = 0xaeedbabe;
529 * Search for the biggest gap in the low 32 bits of the e820
530 * memory space. We pass this space to PCI to assign MMIO resources
531 * for hotplug or unconfigured devices in.
532 * Hopefully the BIOS let enough space left.
534 __init void e820_setup_gap(void)
536 unsigned long gapstart, gapsize;
537 unsigned long last;
538 int i;
539 int found = 0;
541 last = 0x100000000ull;
542 gapstart = 0x10000000;
543 gapsize = 0x400000;
544 i = e820.nr_map;
545 while (--i >= 0) {
546 unsigned long long start = e820.map[i].addr;
547 unsigned long long end = start + e820.map[i].size;
550 * Since "last" is at most 4GB, we know we'll
551 * fit in 32 bits if this condition is true
553 if (last > end) {
554 unsigned long gap = last - end;
556 if (gap > gapsize) {
557 gapsize = gap;
558 gapstart = end;
559 found = 1;
562 if (start < last)
563 last = start;
566 if (!found) {
567 gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
568 printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit address range\n"
569 KERN_ERR "PCI: Unassigned devices with 32bit resource registers may break!\n");
573 * Start allocating dynamic PCI memory a bit into the gap,
574 * aligned up to the nearest megabyte.
576 * Question: should we try to pad it up a bit (do something
577 * like " + (gapsize >> 3)" in there too?). We now have the
578 * technology.
580 pci_mem_start = (gapstart + 0xfffff) & ~0xfffff;
582 printk(KERN_INFO "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
583 pci_mem_start, gapstart, gapsize);