upgrade to linux 2.6.9-1.11_FC2
[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 /*
144  * Find the highest page frame number we have available
145  */
146 unsigned long __init e820_end_of_ram(void)
147 {
148         int i;
149         unsigned long end_pfn = 0;
150         
151         for (i = 0; i < e820.nr_map; i++) {
152                 struct e820entry *ei = &e820.map[i]; 
153                 unsigned long start, end;
154
155                 start = round_up(ei->addr, PAGE_SIZE); 
156                 end = round_down(ei->addr + ei->size, PAGE_SIZE); 
157                 if (start >= end)
158                         continue;
159                 if (ei->type == E820_RAM) { 
160                 if (end > end_pfn<<PAGE_SHIFT)
161                         end_pfn = end>>PAGE_SHIFT;
162                 } else { 
163                         if (end > end_pfn_map<<PAGE_SHIFT) 
164                                 end_pfn_map = end>>PAGE_SHIFT;
165                 } 
166         }
167
168         if (end_pfn > end_pfn_map) 
169                 end_pfn_map = end_pfn;
170         if (end_pfn_map > MAXMEM>>PAGE_SHIFT)
171                 end_pfn_map = MAXMEM>>PAGE_SHIFT;
172         if (end_pfn > end_user_pfn)
173                 end_pfn = end_user_pfn;
174         if (end_pfn > end_pfn_map) 
175                 end_pfn = end_pfn_map; 
176
177         return end_pfn; 
178 }
179
180 /* 
181  * Mark e820 reserved areas as busy for the resource manager.
182  */
183 void __init e820_reserve_resources(void)
184 {
185         int i;
186         for (i = 0; i < e820.nr_map; i++) {
187                 struct resource *res;
188                 res = alloc_bootmem_low(sizeof(struct resource));
189                 switch (e820.map[i].type) {
190                 case E820_RAM:  res->name = "System RAM"; break;
191                 case E820_ACPI: res->name = "ACPI Tables"; break;
192                 case E820_NVS:  res->name = "ACPI Non-volatile Storage"; break;
193                 default:        res->name = "reserved";
194                 }
195                 res->start = e820.map[i].addr;
196                 res->end = res->start + e820.map[i].size - 1;
197                 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
198                 request_resource(&iomem_resource, res);
199                 if (e820.map[i].type == E820_RAM) {
200                         /*
201                          *  We don't know which RAM region contains kernel data,
202                          *  so we try it repeatedly and let the resource manager
203                          *  test it.
204                          */
205                         request_resource(res, &code_resource);
206                         request_resource(res, &data_resource);
207                 }
208         }
209 }
210
211 /* 
212  * Add a memory region to the kernel e820 map.
213  */ 
214 void __init add_memory_region(unsigned long start, unsigned long size, int type)
215 {
216         int x = e820.nr_map;
217
218         if (x == E820MAX) {
219                 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
220                 return;
221         }
222
223         e820.map[x].addr = start;
224         e820.map[x].size = size;
225         e820.map[x].type = type;
226         e820.nr_map++;
227 }
228
229 void __init e820_print_map(char *who)
230 {
231         int i;
232
233         for (i = 0; i < e820.nr_map; i++) {
234                 printk(" %s: %016Lx - %016Lx ", who,
235                         (unsigned long long) e820.map[i].addr,
236                         (unsigned long long) (e820.map[i].addr + e820.map[i].size));
237                 switch (e820.map[i].type) {
238                 case E820_RAM:  printk("(usable)\n");
239                                 break;
240                 case E820_RESERVED:
241                                 printk("(reserved)\n");
242                                 break;
243                 case E820_ACPI:
244                                 printk("(ACPI data)\n");
245                                 break;
246                 case E820_NVS:
247                                 printk("(ACPI NVS)\n");
248                                 break;
249                 default:        printk("type %u\n", e820.map[i].type);
250                                 break;
251                 }
252         }
253 }
254
255 /*
256  * Sanitize the BIOS e820 map.
257  *
258  * Some e820 responses include overlapping entries.  The following 
259  * replaces the original e820 map with a new one, removing overlaps.
260  *
261  */
262 static int __init sanitize_e820_map(struct e820entry * biosmap, char * pnr_map)
263 {
264         struct change_member {
265                 struct e820entry *pbios; /* pointer to original bios entry */
266                 unsigned long long addr; /* address for this change point */
267         };
268         static struct change_member change_point_list[2*E820MAX] __initdata;
269         static struct change_member *change_point[2*E820MAX] __initdata;
270         static struct e820entry *overlap_list[E820MAX] __initdata;
271         static struct e820entry new_bios[E820MAX] __initdata;
272         struct change_member *change_tmp;
273         unsigned long current_type, last_type;
274         unsigned long long last_addr;
275         int chgidx, still_changing;
276         int overlap_entries;
277         int new_bios_entry;
278         int old_nr, new_nr;
279         int i;
280
281         /*
282                 Visually we're performing the following (1,2,3,4 = memory types)...
283
284                 Sample memory map (w/overlaps):
285                    ____22__________________
286                    ______________________4_
287                    ____1111________________
288                    _44_____________________
289                    11111111________________
290                    ____________________33__
291                    ___________44___________
292                    __________33333_________
293                    ______________22________
294                    ___________________2222_
295                    _________111111111______
296                    _____________________11_
297                    _________________4______
298
299                 Sanitized equivalent (no overlap):
300                    1_______________________
301                    _44_____________________
302                    ___1____________________
303                    ____22__________________
304                    ______11________________
305                    _________1______________
306                    __________3_____________
307                    ___________44___________
308                    _____________33_________
309                    _______________2________
310                    ________________1_______
311                    _________________4______
312                    ___________________2____
313                    ____________________33__
314                    ______________________4_
315         */
316
317         /* if there's only one memory region, don't bother */
318         if (*pnr_map < 2)
319                 return -1;
320
321         old_nr = *pnr_map;
322
323         /* bail out if we find any unreasonable addresses in bios map */
324         for (i=0; i<old_nr; i++)
325                 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
326                         return -1;
327
328         /* create pointers for initial change-point information (for sorting) */
329         for (i=0; i < 2*old_nr; i++)
330                 change_point[i] = &change_point_list[i];
331
332         /* record all known change-points (starting and ending addresses) */
333         chgidx = 0;
334         for (i=0; i < old_nr; i++)      {
335                 change_point[chgidx]->addr = biosmap[i].addr;
336                 change_point[chgidx++]->pbios = &biosmap[i];
337                 change_point[chgidx]->addr = biosmap[i].addr + biosmap[i].size;
338                 change_point[chgidx++]->pbios = &biosmap[i];
339         }
340
341         /* sort change-point list by memory addresses (low -> high) */
342         still_changing = 1;
343         while (still_changing)  {
344                 still_changing = 0;
345                 for (i=1; i < 2*old_nr; i++)  {
346                         /* if <current_addr> > <last_addr>, swap */
347                         /* or, if current=<start_addr> & last=<end_addr>, swap */
348                         if ((change_point[i]->addr < change_point[i-1]->addr) ||
349                                 ((change_point[i]->addr == change_point[i-1]->addr) &&
350                                  (change_point[i]->addr == change_point[i]->pbios->addr) &&
351                                  (change_point[i-1]->addr != change_point[i-1]->pbios->addr))
352                            )
353                         {
354                                 change_tmp = change_point[i];
355                                 change_point[i] = change_point[i-1];
356                                 change_point[i-1] = change_tmp;
357                                 still_changing=1;
358                         }
359                 }
360         }
361
362         /* create a new bios memory map, removing overlaps */
363         overlap_entries=0;       /* number of entries in the overlap table */
364         new_bios_entry=0;        /* index for creating new bios map entries */
365         last_type = 0;           /* start with undefined memory type */
366         last_addr = 0;           /* start with 0 as last starting address */
367         /* loop through change-points, determining affect on the new bios map */
368         for (chgidx=0; chgidx < 2*old_nr; chgidx++)
369         {
370                 /* keep track of all overlapping bios entries */
371                 if (change_point[chgidx]->addr == change_point[chgidx]->pbios->addr)
372                 {
373                         /* add map entry to overlap list (> 1 entry implies an overlap) */
374                         overlap_list[overlap_entries++]=change_point[chgidx]->pbios;
375                 }
376                 else
377                 {
378                         /* remove entry from list (order independent, so swap with last) */
379                         for (i=0; i<overlap_entries; i++)
380                         {
381                                 if (overlap_list[i] == change_point[chgidx]->pbios)
382                                         overlap_list[i] = overlap_list[overlap_entries-1];
383                         }
384                         overlap_entries--;
385                 }
386                 /* if there are overlapping entries, decide which "type" to use */
387                 /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */
388                 current_type = 0;
389                 for (i=0; i<overlap_entries; i++)
390                         if (overlap_list[i]->type > current_type)
391                                 current_type = overlap_list[i]->type;
392                 /* continue building up new bios map based on this information */
393                 if (current_type != last_type)  {
394                         if (last_type != 0)      {
395                                 new_bios[new_bios_entry].size =
396                                         change_point[chgidx]->addr - last_addr;
397                                 /* move forward only if the new size was non-zero */
398                                 if (new_bios[new_bios_entry].size != 0)
399                                         if (++new_bios_entry >= E820MAX)
400                                                 break;  /* no more space left for new bios entries */
401                         }
402                         if (current_type != 0)  {
403                                 new_bios[new_bios_entry].addr = change_point[chgidx]->addr;
404                                 new_bios[new_bios_entry].type = current_type;
405                                 last_addr=change_point[chgidx]->addr;
406                         }
407                         last_type = current_type;
408                 }
409         }
410         new_nr = new_bios_entry;   /* retain count for new bios entries */
411
412         /* copy new bios mapping into original location */
413         memcpy(biosmap, new_bios, new_nr*sizeof(struct e820entry));
414         *pnr_map = new_nr;
415
416         return 0;
417 }
418
419 /*
420  * Copy the BIOS e820 map into a safe place.
421  *
422  * Sanity-check it while we're at it..
423  *
424  * If we're lucky and live on a modern system, the setup code
425  * will have given us a memory map that we can use to properly
426  * set up memory.  If we aren't, we'll fake a memory map.
427  *
428  * We check to see that the memory map contains at least 2 elements
429  * before we'll use it, because the detection code in setup.S may
430  * not be perfect and most every PC known to man has two memory
431  * regions: one from 0 to 640k, and one from 1mb up.  (The IBM
432  * thinkpad 560x, for example, does not cooperate with the memory
433  * detection code.)
434  */
435 static int __init copy_e820_map(struct e820entry * biosmap, int nr_map)
436 {
437         /* Only one memory region (or negative)? Ignore it */
438         if (nr_map < 2)
439                 return -1;
440
441         do {
442                 unsigned long start = biosmap->addr;
443                 unsigned long size = biosmap->size;
444                 unsigned long end = start + size;
445                 unsigned long type = biosmap->type;
446
447                 /* Overflow in 64 bits? Ignore the memory map. */
448                 if (start > end)
449                         return -1;
450
451                 /*
452                  * Some BIOSes claim RAM in the 640k - 1M region.
453                  * Not right. Fix it up.
454                  * 
455                  * This should be removed on Hammer which is supposed to not
456                  * have non e820 covered ISA mappings there, but I had some strange
457                  * problems so it stays for now.  -AK
458                  */
459                 if (type == E820_RAM) {
460                         if (start < 0x100000ULL && end > 0xA0000ULL) {
461                                 if (start < 0xA0000ULL)
462                                         add_memory_region(start, 0xA0000ULL-start, type);
463                                 if (end <= 0x100000ULL)
464                                         continue;
465                                 start = 0x100000ULL;
466                                 size = end - start;
467                         }
468                 }
469
470                 add_memory_region(start, size, type);
471         } while (biosmap++,--nr_map);
472         return 0;
473 }
474
475 void __init setup_memory_region(void)
476 {
477         char *who = "BIOS-e820";
478
479         /*
480          * Try to copy the BIOS-supplied E820-map.
481          *
482          * Otherwise fake a memory map; one section from 0k->640k,
483          * the next section from 1mb->appropriate_mem_k
484          */
485         sanitize_e820_map(E820_MAP, &E820_MAP_NR);
486         if (copy_e820_map(E820_MAP, E820_MAP_NR) < 0) {
487                 unsigned long mem_size;
488
489                 /* compare results from other methods and take the greater */
490                 if (ALT_MEM_K < EXT_MEM_K) {
491                         mem_size = EXT_MEM_K;
492                         who = "BIOS-88";
493                 } else {
494                         mem_size = ALT_MEM_K;
495                         who = "BIOS-e801";
496                 }
497
498                 e820.nr_map = 0;
499                 add_memory_region(0, LOWMEMSIZE(), E820_RAM);
500                 add_memory_region(HIGH_MEMORY, mem_size << 10, E820_RAM);
501         }
502         printk(KERN_INFO "BIOS-provided physical RAM map:\n");
503         e820_print_map(who);
504 }
505
506 void __init parse_memopt(char *p, char **from) 
507
508         /*
509          * mem=XXX[kKmM] limits kernel memory to XXX+1MB
510          *
511          * It would be more logical to count from 0 instead of from
512          * HIGH_MEMORY, but we keep that for now for i386 compatibility. 
513          *      
514          * No support for custom mapping like i386.  The reason is
515          * that we need to read the e820 map anyways to handle the
516          * ACPI mappings in the direct map.  Also on x86-64 there
517          * should be always a good e820 map. This is only an upper
518          * limit, you cannot force usage of memory not in e820.
519          *
520          * -AK
521                          */
522         end_user_pfn = memparse(p, from) + HIGH_MEMORY;
523         end_user_pfn >>= PAGE_SHIFT;    
524
525