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