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