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