Merge to Fedora kernel-2.6.7-1.441
[linux-2.6.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 #include <linux/config.h>
7 #include <linux/kernel.h>
8 #include <linux/types.h>
9 #include <linux/init.h>
10 #include <linux/bootmem.h>
11 #include <linux/ioport.h>
12 #include <linux/string.h>
13 #include <asm/page.h>
14 #include <asm/e820.h>
15 #include <asm/proto.h>
16 #include <asm/bootsetup.h>
17
18 extern char _end[];
19
20 /* 
21  * PFN of last memory page.
22  */
23 unsigned long end_pfn; 
24
25 /* 
26  * end_pfn only includes RAM, while end_pfn_map includes all e820 entries.
27  * The direct mapping extends to end_pfn_map, so that we can directly access
28  * apertures, ACPI and other tables without having to play with fixmaps.
29  */ 
30 unsigned long end_pfn_map; 
31
32 /* 
33  * Last pfn which the user wants to use.
34  */
35 unsigned long end_user_pfn = MAXMEM>>PAGE_SHIFT;  
36
37 extern struct resource code_resource, data_resource;
38
39 /* Check for some hardcoded bad areas that early boot is not allowed to touch */ 
40 static inline int bad_addr(unsigned long *addrp, unsigned long size)
41
42         unsigned long addr = *addrp, last = addr + size; 
43
44         /* various gunk below that needed for SMP startup */
45         if (addr < 0x8000) { 
46                 *addrp = 0x8000;
47                 return 1; 
48         }
49
50         /* direct mapping tables of the kernel */
51         if (last >= table_start<<PAGE_SHIFT && addr < table_end<<PAGE_SHIFT) { 
52                 *addrp = table_end << PAGE_SHIFT; 
53                 return 1;
54         } 
55
56         /* initrd */ 
57 #ifdef CONFIG_BLK_DEV_INITRD
58         if (LOADER_TYPE && INITRD_START && last >= INITRD_START && 
59             addr < INITRD_START+INITRD_SIZE) { 
60                 *addrp = INITRD_START + INITRD_SIZE; 
61                 return 1;
62         } 
63 #endif
64         /* kernel code + 640k memory hole (later should not be needed, but 
65            be paranoid for now) */
66         if (last >= 640*1024 && addr < __pa_symbol(&_end)) { 
67                 *addrp = __pa_symbol(&_end);
68                 return 1;
69         }
70         /* XXX ramdisk image here? */ 
71         return 0;
72
73
74 int __init e820_mapped(unsigned long start, unsigned long end, unsigned type) 
75
76         int i;
77         for (i = 0; i < e820.nr_map; i++) { 
78                 struct e820entry *ei = &e820.map[i]; 
79                 if (type && ei->type != type) 
80                         continue;
81                 if (ei->addr >= end || ei->addr + ei->size < start) 
82                         continue; 
83                 return 1; 
84         } 
85         return 0;
86 }
87
88 /* 
89  * Find a free area in a specific range. 
90  */ 
91 unsigned long __init find_e820_area(unsigned long start, unsigned long end, unsigned size) 
92
93         int i; 
94         for (i = 0; i < e820.nr_map; i++) { 
95                 struct e820entry *ei = &e820.map[i]; 
96                 unsigned long addr = ei->addr, last; 
97                 if (ei->type != E820_RAM) 
98                         continue; 
99                 if (addr < start) 
100                         addr = start;
101                 if (addr > ei->addr + ei->size) 
102                         continue; 
103                 while (bad_addr(&addr, size) && addr+size < ei->addr + ei->size)
104                         ;
105                 last = addr + size;
106                 if (last > ei->addr + ei->size)
107                         continue;
108                 if (last > end) 
109                         continue;
110                 return addr; 
111         } 
112         return -1UL;            
113
114
115 /* 
116  * Free bootmem based on the e820 table for a node.
117  */
118 void __init e820_bootmem_free(pg_data_t *pgdat, unsigned long start,unsigned long end)
119 {
120         int i;
121         for (i = 0; i < e820.nr_map; i++) {
122                 struct e820entry *ei = &e820.map[i]; 
123                 unsigned long last, addr;
124
125                 if (ei->type != E820_RAM || 
126                     ei->addr+ei->size <= start || 
127                     ei->addr > end)
128                         continue;
129
130                 addr = round_up(ei->addr, PAGE_SIZE);
131                 if (addr < start) 
132                         addr = start;
133
134                 last = round_down(ei->addr + ei->size, PAGE_SIZE); 
135                 if (last >= end)
136                         last = end; 
137
138                 if (last > addr && last-addr >= PAGE_SIZE)
139                         free_bootmem_node(pgdat, addr, last-addr);
140         }
141 }
142
143 int __get_cpu_vendor(void)
144 {
145         char v[16];
146         int cpuid_level;
147         memset(v, 0, 16);
148         cpuid(0x00000000, &cpuid_level,
149                 (int *)&v[0],
150                 (int *)&v[8],
151                 (int *)&v[4]);
152         if (!strcmp(v, "GenuineIntel"))
153                 return X86_VENDOR_INTEL;
154         else if (!strcmp(v, "AuthenticAMD"))
155                 return X86_VENDOR_AMD;
156         else if (!strcmp(v, "CyrixInstead"))
157                 return X86_VENDOR_CYRIX;
158 //      else if (!strcmp(v, "Geode by NSC"))
159 //              return X86_VENDOR_NSC;
160         else if (!strcmp(v, "UMC UMC UMC "))
161                 return X86_VENDOR_UMC;
162         else if (!strcmp(v, "CentaurHauls"))
163                 return X86_VENDOR_CENTAUR;
164         else if (!strcmp(v, "NexGenDriven"))
165                 return X86_VENDOR_NEXGEN;
166         else if (!strcmp(v, "RiseRiseRise"))
167                 return X86_VENDOR_RISE;
168         else if (!strcmp(v, "GenuineTMx86") ||
169                  !strcmp(v, "TransmetaCPU"))
170                 return X86_VENDOR_TRANSMETA;
171 //      else if (!strcmp(v, "SiS SiS SiS "))
172 //              return X86_VENDOR_SIS;
173         else
174                 return X86_VENDOR_UNKNOWN;
175 }
176
177
178 /*
179  * Find the highest page frame number we have available
180  */
181 unsigned long __init e820_end_of_ram(void)
182 {
183         int i;
184         unsigned long end_pfn = 0;
185         
186         for (i = 0; i < e820.nr_map; i++) {
187                 struct e820entry *ei = &e820.map[i]; 
188                 unsigned long start, end;
189
190                 start = round_up(ei->addr, PAGE_SIZE); 
191                 end = round_down(ei->addr + ei->size, PAGE_SIZE); 
192                 if (__get_cpu_vendor() != X86_VENDOR_AMD)
193                         end = round_down(min(end, 0xffffffff),PAGE_SIZE);
194                 
195                 if (start >= end)
196                         continue;
197                 if (ei->type == E820_RAM) { 
198                 if (end > end_pfn<<PAGE_SHIFT)
199                         end_pfn = end>>PAGE_SHIFT;
200                 } else { 
201                         if (end > end_pfn_map<<PAGE_SHIFT) 
202                                 end_pfn_map = end>>PAGE_SHIFT;
203                 } 
204         }
205
206         if (end_pfn > end_pfn_map) 
207                 end_pfn_map = end_pfn;
208         if (end_pfn_map > MAXMEM>>PAGE_SHIFT)
209                 end_pfn_map = MAXMEM>>PAGE_SHIFT;
210         if (end_pfn > end_user_pfn)
211                 end_pfn = end_user_pfn;
212         if (end_pfn > end_pfn_map) 
213                 end_pfn = end_pfn_map; 
214
215         return end_pfn; 
216 }
217
218 /* 
219  * Mark e820 reserved areas as busy for the resource manager.
220  */
221 void __init e820_reserve_resources(void)
222 {
223         int i;
224         for (i = 0; i < e820.nr_map; i++) {
225                 struct resource *res;
226                 if (e820.map[i].addr + e820.map[i].size > 0x100000000ULL)
227                         continue;
228                 res = alloc_bootmem_low(sizeof(struct resource));
229                 switch (e820.map[i].type) {
230                 case E820_RAM:  res->name = "System RAM"; break;
231                 case E820_ACPI: res->name = "ACPI Tables"; break;
232                 case E820_NVS:  res->name = "ACPI Non-volatile Storage"; break;
233                 default:        res->name = "reserved";
234                 }
235                 res->start = e820.map[i].addr;
236                 res->end = res->start + e820.map[i].size - 1;
237                 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
238                 request_resource(&iomem_resource, res);
239                 if (e820.map[i].type == E820_RAM) {
240                         /*
241                          *  We don't know which RAM region contains kernel data,
242                          *  so we try it repeatedly and let the resource manager
243                          *  test it.
244                          */
245                         request_resource(res, &code_resource);
246                         request_resource(res, &data_resource);
247                 }
248         }
249 }
250
251 /* 
252  * Add a memory region to the kernel e820 map.
253  */ 
254 void __init add_memory_region(unsigned long start, unsigned long size, int type)
255 {
256         int x = e820.nr_map;
257
258         if (x == E820MAX) {
259                 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
260                 return;
261         }
262
263         e820.map[x].addr = start;
264         e820.map[x].size = size;
265         e820.map[x].type = type;
266         e820.nr_map++;
267 }
268
269 void __init e820_print_map(char *who)
270 {
271         int i;
272
273         for (i = 0; i < e820.nr_map; i++) {
274                 printk(" %s: %016Lx - %016Lx ", who,
275                         (unsigned long long) e820.map[i].addr,
276                         (unsigned long long) (e820.map[i].addr + e820.map[i].size));
277                 switch (e820.map[i].type) {
278                 case E820_RAM:  printk("(usable)\n");
279                                 break;
280                 case E820_RESERVED:
281                                 printk("(reserved)\n");
282                                 break;
283                 case E820_ACPI:
284                                 printk("(ACPI data)\n");
285                                 break;
286                 case E820_NVS:
287                                 printk("(ACPI NVS)\n");
288                                 break;
289                 default:        printk("type %u\n", e820.map[i].type);
290                                 break;
291                 }
292         }
293 }
294
295 /*
296  * Sanitize the BIOS e820 map.
297  *
298  * Some e820 responses include overlapping entries.  The following 
299  * replaces the original e820 map with a new one, removing overlaps.
300  *
301  */
302 static int __init sanitize_e820_map(struct e820entry * biosmap, char * pnr_map)
303 {
304         struct change_member {
305                 struct e820entry *pbios; /* pointer to original bios entry */
306                 unsigned long long addr; /* address for this change point */
307         };
308         static struct change_member change_point_list[2*E820MAX] __initdata;
309         static struct change_member *change_point[2*E820MAX] __initdata;
310         static struct e820entry *overlap_list[E820MAX] __initdata;
311         static struct e820entry new_bios[E820MAX] __initdata;
312         struct change_member *change_tmp;
313         unsigned long current_type, last_type;
314         unsigned long long last_addr;
315         int chgidx, still_changing;
316         int overlap_entries;
317         int new_bios_entry;
318         int old_nr, new_nr;
319         int i;
320
321         /*
322                 Visually we're performing the following (1,2,3,4 = memory types)...
323
324                 Sample memory map (w/overlaps):
325                    ____22__________________
326                    ______________________4_
327                    ____1111________________
328                    _44_____________________
329                    11111111________________
330                    ____________________33__
331                    ___________44___________
332                    __________33333_________
333                    ______________22________
334                    ___________________2222_
335                    _________111111111______
336                    _____________________11_
337                    _________________4______
338
339                 Sanitized equivalent (no overlap):
340                    1_______________________
341                    _44_____________________
342                    ___1____________________
343                    ____22__________________
344                    ______11________________
345                    _________1______________
346                    __________3_____________
347                    ___________44___________
348                    _____________33_________
349                    _______________2________
350                    ________________1_______
351                    _________________4______
352                    ___________________2____
353                    ____________________33__
354                    ______________________4_
355         */
356
357         /* if there's only one memory region, don't bother */
358         if (*pnr_map < 2)
359                 return -1;
360
361         old_nr = *pnr_map;
362
363         /* bail out if we find any unreasonable addresses in bios map */
364         for (i=0; i<old_nr; i++)
365                 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
366                         return -1;
367
368         /* create pointers for initial change-point information (for sorting) */
369         for (i=0; i < 2*old_nr; i++)
370                 change_point[i] = &change_point_list[i];
371
372         /* record all known change-points (starting and ending addresses) */
373         chgidx = 0;
374         for (i=0; i < old_nr; i++)      {
375                 change_point[chgidx]->addr = biosmap[i].addr;
376                 change_point[chgidx++]->pbios = &biosmap[i];
377                 change_point[chgidx]->addr = biosmap[i].addr + biosmap[i].size;
378                 change_point[chgidx++]->pbios = &biosmap[i];
379         }
380
381         /* sort change-point list by memory addresses (low -> high) */
382         still_changing = 1;
383         while (still_changing)  {
384                 still_changing = 0;
385                 for (i=1; i < 2*old_nr; i++)  {
386                         /* if <current_addr> > <last_addr>, swap */
387                         /* or, if current=<start_addr> & last=<end_addr>, swap */
388                         if ((change_point[i]->addr < change_point[i-1]->addr) ||
389                                 ((change_point[i]->addr == change_point[i-1]->addr) &&
390                                  (change_point[i]->addr == change_point[i]->pbios->addr) &&
391                                  (change_point[i-1]->addr != change_point[i-1]->pbios->addr))
392                            )
393                         {
394                                 change_tmp = change_point[i];
395                                 change_point[i] = change_point[i-1];
396                                 change_point[i-1] = change_tmp;
397                                 still_changing=1;
398                         }
399                 }
400         }
401
402         /* create a new bios memory map, removing overlaps */
403         overlap_entries=0;       /* number of entries in the overlap table */
404         new_bios_entry=0;        /* index for creating new bios map entries */
405         last_type = 0;           /* start with undefined memory type */
406         last_addr = 0;           /* start with 0 as last starting address */
407         /* loop through change-points, determining affect on the new bios map */
408         for (chgidx=0; chgidx < 2*old_nr; chgidx++)
409         {
410                 /* keep track of all overlapping bios entries */
411                 if (change_point[chgidx]->addr == change_point[chgidx]->pbios->addr)
412                 {
413                         /* add map entry to overlap list (> 1 entry implies an overlap) */
414                         overlap_list[overlap_entries++]=change_point[chgidx]->pbios;
415                 }
416                 else
417                 {
418                         /* remove entry from list (order independent, so swap with last) */
419                         for (i=0; i<overlap_entries; i++)
420                         {
421                                 if (overlap_list[i] == change_point[chgidx]->pbios)
422                                         overlap_list[i] = overlap_list[overlap_entries-1];
423                         }
424                         overlap_entries--;
425                 }
426                 /* if there are overlapping entries, decide which "type" to use */
427                 /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */
428                 current_type = 0;
429                 for (i=0; i<overlap_entries; i++)
430                         if (overlap_list[i]->type > current_type)
431                                 current_type = overlap_list[i]->type;
432                 /* continue building up new bios map based on this information */
433                 if (current_type != last_type)  {
434                         if (last_type != 0)      {
435                                 new_bios[new_bios_entry].size =
436                                         change_point[chgidx]->addr - last_addr;
437                                 /* move forward only if the new size was non-zero */
438                                 if (new_bios[new_bios_entry].size != 0)
439                                         if (++new_bios_entry >= E820MAX)
440                                                 break;  /* no more space left for new bios entries */
441                         }
442                         if (current_type != 0)  {
443                                 new_bios[new_bios_entry].addr = change_point[chgidx]->addr;
444                                 new_bios[new_bios_entry].type = current_type;
445                                 last_addr=change_point[chgidx]->addr;
446                         }
447                         last_type = current_type;
448                 }
449         }
450         new_nr = new_bios_entry;   /* retain count for new bios entries */
451
452         /* copy new bios mapping into original location */
453         memcpy(biosmap, new_bios, new_nr*sizeof(struct e820entry));
454         *pnr_map = new_nr;
455
456         return 0;
457 }
458
459 /*
460  * Copy the BIOS e820 map into a safe place.
461  *
462  * Sanity-check it while we're at it..
463  *
464  * If we're lucky and live on a modern system, the setup code
465  * will have given us a memory map that we can use to properly
466  * set up memory.  If we aren't, we'll fake a memory map.
467  *
468  * We check to see that the memory map contains at least 2 elements
469  * before we'll use it, because the detection code in setup.S may
470  * not be perfect and most every PC known to man has two memory
471  * regions: one from 0 to 640k, and one from 1mb up.  (The IBM
472  * thinkpad 560x, for example, does not cooperate with the memory
473  * detection code.)
474  */
475 static int __init copy_e820_map(struct e820entry * biosmap, int nr_map)
476 {
477         /* Only one memory region (or negative)? Ignore it */
478         if (nr_map < 2)
479                 return -1;
480
481         do {
482                 unsigned long start = biosmap->addr;
483                 unsigned long size = biosmap->size;
484                 unsigned long end = start + size;
485                 unsigned long type = biosmap->type;
486
487                 /* Overflow in 64 bits? Ignore the memory map. */
488                 if (start > end)
489                         return -1;
490
491                 /*
492                  * Some BIOSes claim RAM in the 640k - 1M region.
493                  * Not right. Fix it up.
494                  * 
495                  * This should be removed on Hammer which is supposed to not
496                  * have non e820 covered ISA mappings there, but I had some strange
497                  * problems so it stays for now.  -AK
498                  */
499                 if (type == E820_RAM) {
500                         if (start < 0x100000ULL && end > 0xA0000ULL) {
501                                 if (start < 0xA0000ULL)
502                                         add_memory_region(start, 0xA0000ULL-start, type);
503                                 if (end <= 0x100000ULL)
504                                         continue;
505                                 start = 0x100000ULL;
506                                 size = end - start;
507                         }
508                 }
509
510                 add_memory_region(start, size, type);
511         } while (biosmap++,--nr_map);
512         return 0;
513 }
514
515 void __init setup_memory_region(void)
516 {
517         char *who = "BIOS-e820";
518
519         /*
520          * Try to copy the BIOS-supplied E820-map.
521          *
522          * Otherwise fake a memory map; one section from 0k->640k,
523          * the next section from 1mb->appropriate_mem_k
524          */
525         sanitize_e820_map(E820_MAP, &E820_MAP_NR);
526         if (copy_e820_map(E820_MAP, E820_MAP_NR) < 0) {
527                 unsigned long mem_size;
528
529                 /* compare results from other methods and take the greater */
530                 if (ALT_MEM_K < EXT_MEM_K) {
531                         mem_size = EXT_MEM_K;
532                         who = "BIOS-88";
533                 } else {
534                         mem_size = ALT_MEM_K;
535                         who = "BIOS-e801";
536                 }
537
538                 e820.nr_map = 0;
539                 add_memory_region(0, LOWMEMSIZE(), E820_RAM);
540                 add_memory_region(HIGH_MEMORY, mem_size << 10, E820_RAM);
541         }
542         printk(KERN_INFO "BIOS-provided physical RAM map:\n");
543         e820_print_map(who);
544 }
545
546 void __init parse_memopt(char *p, char **from) 
547
548         /*
549          * mem=XXX[kKmM] limits kernel memory to XXX+1MB
550          *
551          * It would be more logical to count from 0 instead of from
552          * HIGH_MEMORY, but we keep that for now for i386 compatibility. 
553          *      
554          * No support for custom mapping like i386.  The reason is
555          * that we need to read the e820 map anyways to handle the
556          * ACPI mappings in the direct map.  Also on x86-64 there
557          * should be always a good e820 map. This is only an upper
558          * limit, you cannot force usage of memory not in e820.
559          *
560          * -AK
561                          */
562         end_user_pfn = memparse(p, from) + HIGH_MEMORY;
563         end_user_pfn >>= PAGE_SHIFT;    
564
565