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