Merge HEAD from master.kernel.org:/home/rmk/linux-2.6-serial.git
[pandora-kernel.git] / arch / x86_64 / kernel / e820.c
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 $
5  *
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>
10  *
11  */
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>
24
25 extern char _end[];
26
27 /* 
28  * PFN of last memory page.
29  */
30 unsigned long end_pfn; 
31
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; 
38
39 /* 
40  * Last pfn which the user wants to use.
41  */
42 unsigned long end_user_pfn = MAXMEM>>PAGE_SHIFT;  
43
44 extern struct resource code_resource, data_resource;
45
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)
48
49         unsigned long addr = *addrp, last = addr + size; 
50
51         /* various gunk below that needed for SMP startup */
52         if (addr < 0x8000) { 
53                 *addrp = 0x8000;
54                 return 1; 
55         }
56
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;
61         } 
62
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;
69         } 
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;
76         }
77         /* XXX ramdisk image here? */ 
78         return 0;
79
80
81 int __init e820_mapped(unsigned long start, unsigned long end, unsigned type) 
82
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; 
91         } 
92         return 0;
93 }
94
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) 
99
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)
111                         ;
112                 last = addr + size;
113                 if (last > ei->addr + ei->size)
114                         continue;
115                 if (last > end) 
116                         continue;
117                 return addr; 
118         } 
119         return -1UL;            
120
121
122 /* 
123  * Free bootmem based on the e820 table for a node.
124  */
125 void __init e820_bootmem_free(pg_data_t *pgdat, unsigned long start,unsigned long end)
126 {
127         int i;
128         for (i = 0; i < e820.nr_map; i++) {
129                 struct e820entry *ei = &e820.map[i]; 
130                 unsigned long last, addr;
131
132                 if (ei->type != E820_RAM || 
133                     ei->addr+ei->size <= start || 
134                     ei->addr > end)
135                         continue;
136
137                 addr = round_up(ei->addr, PAGE_SIZE);
138                 if (addr < start) 
139                         addr = start;
140
141                 last = round_down(ei->addr + ei->size, PAGE_SIZE); 
142                 if (last >= end)
143                         last = end; 
144
145                 if (last > addr && last-addr >= PAGE_SIZE)
146                         free_bootmem_node(pgdat, addr, last-addr);
147         }
148 }
149
150 /*
151  * Find the highest page frame number we have available
152  */
153 unsigned long __init e820_end_of_ram(void)
154 {
155         int i;
156         unsigned long end_pfn = 0;
157         
158         for (i = 0; i < e820.nr_map; i++) {
159                 struct e820entry *ei = &e820.map[i]; 
160                 unsigned long start, end;
161
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;
172                 } 
173         }
174
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; 
183
184         return end_pfn; 
185 }
186
187 /* 
188  * Compute how much memory is missing in a range.
189  * Unlike the other functions in this file the arguments are in page numbers.
190  */
191 unsigned long __init
192 e820_hole_size(unsigned long start_pfn, unsigned long end_pfn)
193 {
194         unsigned long ram = 0;
195         unsigned long start = start_pfn << PAGE_SHIFT;
196         unsigned long end = end_pfn << PAGE_SHIFT;
197         int i;
198         for (i = 0; i < e820.nr_map; i++) {
199                 struct e820entry *ei = &e820.map[i];
200                 unsigned long last, addr;
201
202                 if (ei->type != E820_RAM ||
203                     ei->addr+ei->size <= start ||
204                     ei->addr >= end)
205                         continue;
206
207                 addr = round_up(ei->addr, PAGE_SIZE);
208                 if (addr < start)
209                         addr = start;
210
211                 last = round_down(ei->addr + ei->size, PAGE_SIZE);
212                 if (last >= end)
213                         last = end;
214
215                 if (last > addr)
216                         ram += last - addr;
217         }
218         return ((end - start) - ram) >> PAGE_SHIFT;
219 }
220
221 /*
222  * Mark e820 reserved areas as busy for the resource manager.
223  */
224 void __init e820_reserve_resources(void)
225 {
226         int i;
227         for (i = 0; i < e820.nr_map; i++) {
228                 struct resource *res;
229                 res = alloc_bootmem_low(sizeof(struct resource));
230                 switch (e820.map[i].type) {
231                 case E820_RAM:  res->name = "System RAM"; break;
232                 case E820_ACPI: res->name = "ACPI Tables"; break;
233                 case E820_NVS:  res->name = "ACPI Non-volatile Storage"; break;
234                 default:        res->name = "reserved";
235                 }
236                 res->start = e820.map[i].addr;
237                 res->end = res->start + e820.map[i].size - 1;
238                 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
239                 request_resource(&iomem_resource, res);
240                 if (e820.map[i].type == E820_RAM) {
241                         /*
242                          *  We don't know which RAM region contains kernel data,
243                          *  so we try it repeatedly and let the resource manager
244                          *  test it.
245                          */
246                         request_resource(res, &code_resource);
247                         request_resource(res, &data_resource);
248 #ifdef CONFIG_KEXEC
249                         request_resource(res, &crashk_res);
250 #endif
251                 }
252         }
253 }
254
255 /* 
256  * Add a memory region to the kernel e820 map.
257  */ 
258 void __init add_memory_region(unsigned long start, unsigned long size, int type)
259 {
260         int x = e820.nr_map;
261
262         if (x == E820MAX) {
263                 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
264                 return;
265         }
266
267         e820.map[x].addr = start;
268         e820.map[x].size = size;
269         e820.map[x].type = type;
270         e820.nr_map++;
271 }
272
273 void __init e820_print_map(char *who)
274 {
275         int i;
276
277         for (i = 0; i < e820.nr_map; i++) {
278                 printk(" %s: %016Lx - %016Lx ", who,
279                         (unsigned long long) e820.map[i].addr,
280                         (unsigned long long) (e820.map[i].addr + e820.map[i].size));
281                 switch (e820.map[i].type) {
282                 case E820_RAM:  printk("(usable)\n");
283                                 break;
284                 case E820_RESERVED:
285                                 printk("(reserved)\n");
286                                 break;
287                 case E820_ACPI:
288                                 printk("(ACPI data)\n");
289                                 break;
290                 case E820_NVS:
291                                 printk("(ACPI NVS)\n");
292                                 break;
293                 default:        printk("type %u\n", e820.map[i].type);
294                                 break;
295                 }
296         }
297 }
298
299 /*
300  * Sanitize the BIOS e820 map.
301  *
302  * Some e820 responses include overlapping entries.  The following 
303  * replaces the original e820 map with a new one, removing overlaps.
304  *
305  */
306 static int __init sanitize_e820_map(struct e820entry * biosmap, char * pnr_map)
307 {
308         struct change_member {
309                 struct e820entry *pbios; /* pointer to original bios entry */
310                 unsigned long long addr; /* address for this change point */
311         };
312         static struct change_member change_point_list[2*E820MAX] __initdata;
313         static struct change_member *change_point[2*E820MAX] __initdata;
314         static struct e820entry *overlap_list[E820MAX] __initdata;
315         static struct e820entry new_bios[E820MAX] __initdata;
316         struct change_member *change_tmp;
317         unsigned long current_type, last_type;
318         unsigned long long last_addr;
319         int chgidx, still_changing;
320         int overlap_entries;
321         int new_bios_entry;
322         int old_nr, new_nr, chg_nr;
323         int i;
324
325         /*
326                 Visually we're performing the following (1,2,3,4 = memory types)...
327
328                 Sample memory map (w/overlaps):
329                    ____22__________________
330                    ______________________4_
331                    ____1111________________
332                    _44_____________________
333                    11111111________________
334                    ____________________33__
335                    ___________44___________
336                    __________33333_________
337                    ______________22________
338                    ___________________2222_
339                    _________111111111______
340                    _____________________11_
341                    _________________4______
342
343                 Sanitized equivalent (no overlap):
344                    1_______________________
345                    _44_____________________
346                    ___1____________________
347                    ____22__________________
348                    ______11________________
349                    _________1______________
350                    __________3_____________
351                    ___________44___________
352                    _____________33_________
353                    _______________2________
354                    ________________1_______
355                    _________________4______
356                    ___________________2____
357                    ____________________33__
358                    ______________________4_
359         */
360
361         /* if there's only one memory region, don't bother */
362         if (*pnr_map < 2)
363                 return -1;
364
365         old_nr = *pnr_map;
366
367         /* bail out if we find any unreasonable addresses in bios map */
368         for (i=0; i<old_nr; i++)
369                 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
370                         return -1;
371
372         /* create pointers for initial change-point information (for sorting) */
373         for (i=0; i < 2*old_nr; i++)
374                 change_point[i] = &change_point_list[i];
375
376         /* record all known change-points (starting and ending addresses),
377            omitting those that are for empty memory regions */
378         chgidx = 0;
379         for (i=0; i < old_nr; i++)      {
380                 if (biosmap[i].size != 0) {
381                         change_point[chgidx]->addr = biosmap[i].addr;
382                         change_point[chgidx++]->pbios = &biosmap[i];
383                         change_point[chgidx]->addr = biosmap[i].addr + biosmap[i].size;
384                         change_point[chgidx++]->pbios = &biosmap[i];
385                 }
386         }
387         chg_nr = chgidx;
388
389         /* sort change-point list by memory addresses (low -> high) */
390         still_changing = 1;
391         while (still_changing)  {
392                 still_changing = 0;
393                 for (i=1; i < chg_nr; i++)  {
394                         /* if <current_addr> > <last_addr>, swap */
395                         /* or, if current=<start_addr> & last=<end_addr>, swap */
396                         if ((change_point[i]->addr < change_point[i-1]->addr) ||
397                                 ((change_point[i]->addr == change_point[i-1]->addr) &&
398                                  (change_point[i]->addr == change_point[i]->pbios->addr) &&
399                                  (change_point[i-1]->addr != change_point[i-1]->pbios->addr))
400                            )
401                         {
402                                 change_tmp = change_point[i];
403                                 change_point[i] = change_point[i-1];
404                                 change_point[i-1] = change_tmp;
405                                 still_changing=1;
406                         }
407                 }
408         }
409
410         /* create a new bios memory map, removing overlaps */
411         overlap_entries=0;       /* number of entries in the overlap table */
412         new_bios_entry=0;        /* index for creating new bios map entries */
413         last_type = 0;           /* start with undefined memory type */
414         last_addr = 0;           /* start with 0 as last starting address */
415         /* loop through change-points, determining affect on the new bios map */
416         for (chgidx=0; chgidx < chg_nr; chgidx++)
417         {
418                 /* keep track of all overlapping bios entries */
419                 if (change_point[chgidx]->addr == change_point[chgidx]->pbios->addr)
420                 {
421                         /* add map entry to overlap list (> 1 entry implies an overlap) */
422                         overlap_list[overlap_entries++]=change_point[chgidx]->pbios;
423                 }
424                 else
425                 {
426                         /* remove entry from list (order independent, so swap with last) */
427                         for (i=0; i<overlap_entries; i++)
428                         {
429                                 if (overlap_list[i] == change_point[chgidx]->pbios)
430                                         overlap_list[i] = overlap_list[overlap_entries-1];
431                         }
432                         overlap_entries--;
433                 }
434                 /* if there are overlapping entries, decide which "type" to use */
435                 /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */
436                 current_type = 0;
437                 for (i=0; i<overlap_entries; i++)
438                         if (overlap_list[i]->type > current_type)
439                                 current_type = overlap_list[i]->type;
440                 /* continue building up new bios map based on this information */
441                 if (current_type != last_type)  {
442                         if (last_type != 0)      {
443                                 new_bios[new_bios_entry].size =
444                                         change_point[chgidx]->addr - last_addr;
445                                 /* move forward only if the new size was non-zero */
446                                 if (new_bios[new_bios_entry].size != 0)
447                                         if (++new_bios_entry >= E820MAX)
448                                                 break;  /* no more space left for new bios entries */
449                         }
450                         if (current_type != 0)  {
451                                 new_bios[new_bios_entry].addr = change_point[chgidx]->addr;
452                                 new_bios[new_bios_entry].type = current_type;
453                                 last_addr=change_point[chgidx]->addr;
454                         }
455                         last_type = current_type;
456                 }
457         }
458         new_nr = new_bios_entry;   /* retain count for new bios entries */
459
460         /* copy new bios mapping into original location */
461         memcpy(biosmap, new_bios, new_nr*sizeof(struct e820entry));
462         *pnr_map = new_nr;
463
464         return 0;
465 }
466
467 /*
468  * Copy the BIOS e820 map into a safe place.
469  *
470  * Sanity-check it while we're at it..
471  *
472  * If we're lucky and live on a modern system, the setup code
473  * will have given us a memory map that we can use to properly
474  * set up memory.  If we aren't, we'll fake a memory map.
475  *
476  * We check to see that the memory map contains at least 2 elements
477  * before we'll use it, because the detection code in setup.S may
478  * not be perfect and most every PC known to man has two memory
479  * regions: one from 0 to 640k, and one from 1mb up.  (The IBM
480  * thinkpad 560x, for example, does not cooperate with the memory
481  * detection code.)
482  */
483 static int __init copy_e820_map(struct e820entry * biosmap, int nr_map)
484 {
485         /* Only one memory region (or negative)? Ignore it */
486         if (nr_map < 2)
487                 return -1;
488
489         do {
490                 unsigned long start = biosmap->addr;
491                 unsigned long size = biosmap->size;
492                 unsigned long end = start + size;
493                 unsigned long type = biosmap->type;
494
495                 /* Overflow in 64 bits? Ignore the memory map. */
496                 if (start > end)
497                         return -1;
498
499                 /*
500                  * Some BIOSes claim RAM in the 640k - 1M region.
501                  * Not right. Fix it up.
502                  * 
503                  * This should be removed on Hammer which is supposed to not
504                  * have non e820 covered ISA mappings there, but I had some strange
505                  * problems so it stays for now.  -AK
506                  */
507                 if (type == E820_RAM) {
508                         if (start < 0x100000ULL && end > 0xA0000ULL) {
509                                 if (start < 0xA0000ULL)
510                                         add_memory_region(start, 0xA0000ULL-start, type);
511                                 if (end <= 0x100000ULL)
512                                         continue;
513                                 start = 0x100000ULL;
514                                 size = end - start;
515                         }
516                 }
517
518                 add_memory_region(start, size, type);
519         } while (biosmap++,--nr_map);
520         return 0;
521 }
522
523 void __init setup_memory_region(void)
524 {
525         char *who = "BIOS-e820";
526
527         /*
528          * Try to copy the BIOS-supplied E820-map.
529          *
530          * Otherwise fake a memory map; one section from 0k->640k,
531          * the next section from 1mb->appropriate_mem_k
532          */
533         sanitize_e820_map(E820_MAP, &E820_MAP_NR);
534         if (copy_e820_map(E820_MAP, E820_MAP_NR) < 0) {
535                 unsigned long mem_size;
536
537                 /* compare results from other methods and take the greater */
538                 if (ALT_MEM_K < EXT_MEM_K) {
539                         mem_size = EXT_MEM_K;
540                         who = "BIOS-88";
541                 } else {
542                         mem_size = ALT_MEM_K;
543                         who = "BIOS-e801";
544                 }
545
546                 e820.nr_map = 0;
547                 add_memory_region(0, LOWMEMSIZE(), E820_RAM);
548                 add_memory_region(HIGH_MEMORY, mem_size << 10, E820_RAM);
549         }
550         printk(KERN_INFO "BIOS-provided physical RAM map:\n");
551         e820_print_map(who);
552 }
553
554 void __init parse_memopt(char *p, char **from) 
555
556         end_user_pfn = memparse(p, from);
557         end_user_pfn >>= PAGE_SHIFT;    
558
559
560 unsigned long pci_mem_start = 0xaeedbabe;
561
562 /*
563  * Search for the biggest gap in the low 32 bits of the e820
564  * memory space.  We pass this space to PCI to assign MMIO resources
565  * for hotplug or unconfigured devices in.
566  * Hopefully the BIOS let enough space left.
567  */
568 __init void e820_setup_gap(void)
569 {
570         unsigned long gapstart, gapsize;
571         unsigned long last;
572         int i;
573         int found = 0;
574
575         last = 0x100000000ull;
576         gapstart = 0x10000000;
577         gapsize = 0x400000;
578         i = e820.nr_map;
579         while (--i >= 0) {
580                 unsigned long long start = e820.map[i].addr;
581                 unsigned long long end = start + e820.map[i].size;
582
583                 /*
584                  * Since "last" is at most 4GB, we know we'll
585                  * fit in 32 bits if this condition is true
586                  */
587                 if (last > end) {
588                         unsigned long gap = last - end;
589
590                         if (gap > gapsize) {
591                                 gapsize = gap;
592                                 gapstart = end;
593                                 found = 1;
594                         }
595                 }
596                 if (start < last)
597                         last = start;
598         }
599
600         if (!found) {
601                 gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
602                 printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit address range\n"
603                        KERN_ERR "PCI: Unassigned devices with 32bit resource registers may break!\n");
604         }
605
606         /*
607          * Start allocating dynamic PCI memory a bit into the gap,
608          * aligned up to the nearest megabyte.
609          *
610          * Question: should we try to pad it up a bit (do something
611          * like " + (gapsize >> 3)" in there too?). We now have the
612          * technology.
613          */
614         pci_mem_start = (gapstart + 0xfffff) & ~0xfffff;
615
616         printk(KERN_INFO "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
617                 pci_mem_start, gapstart, gapsize);
618 }