[PATCH] x86: fix amd64-agp aperture validation
[pandora-kernel.git] / arch / i386 / kernel / e820.c
1 #include <linux/kernel.h>
2 #include <linux/types.h>
3 #include <linux/init.h>
4 #include <linux/bootmem.h>
5 #include <linux/ioport.h>
6 #include <linux/string.h>
7 #include <linux/kexec.h>
8 #include <linux/module.h>
9 #include <linux/mm.h>
10 #include <linux/efi.h>
11 #include <linux/pfn.h>
12 #include <linux/uaccess.h>
13
14 #include <asm/pgtable.h>
15 #include <asm/page.h>
16 #include <asm/e820.h>
17 #include <asm/setup.h>
18
19 #ifdef CONFIG_EFI
20 int efi_enabled = 0;
21 EXPORT_SYMBOL(efi_enabled);
22 #endif
23
24 struct e820map e820;
25 struct change_member {
26         struct e820entry *pbios; /* pointer to original bios entry */
27         unsigned long long addr; /* address for this change point */
28 };
29 static struct change_member change_point_list[2*E820MAX] __initdata;
30 static struct change_member *change_point[2*E820MAX] __initdata;
31 static struct e820entry *overlap_list[E820MAX] __initdata;
32 static struct e820entry new_bios[E820MAX] __initdata;
33 /* For PCI or other memory-mapped resources */
34 unsigned long pci_mem_start = 0x10000000;
35 #ifdef CONFIG_PCI
36 EXPORT_SYMBOL(pci_mem_start);
37 #endif
38 extern int user_defined_memmap;
39 struct resource data_resource = {
40         .name   = "Kernel data",
41         .start  = 0,
42         .end    = 0,
43         .flags  = IORESOURCE_BUSY | IORESOURCE_MEM
44 };
45
46 struct resource code_resource = {
47         .name   = "Kernel code",
48         .start  = 0,
49         .end    = 0,
50         .flags  = IORESOURCE_BUSY | IORESOURCE_MEM
51 };
52
53 static struct resource system_rom_resource = {
54         .name   = "System ROM",
55         .start  = 0xf0000,
56         .end    = 0xfffff,
57         .flags  = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
58 };
59
60 static struct resource extension_rom_resource = {
61         .name   = "Extension ROM",
62         .start  = 0xe0000,
63         .end    = 0xeffff,
64         .flags  = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
65 };
66
67 static struct resource adapter_rom_resources[] = { {
68         .name   = "Adapter ROM",
69         .start  = 0xc8000,
70         .end    = 0,
71         .flags  = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
72 }, {
73         .name   = "Adapter ROM",
74         .start  = 0,
75         .end    = 0,
76         .flags  = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
77 }, {
78         .name   = "Adapter ROM",
79         .start  = 0,
80         .end    = 0,
81         .flags  = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
82 }, {
83         .name   = "Adapter ROM",
84         .start  = 0,
85         .end    = 0,
86         .flags  = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
87 }, {
88         .name   = "Adapter ROM",
89         .start  = 0,
90         .end    = 0,
91         .flags  = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
92 }, {
93         .name   = "Adapter ROM",
94         .start  = 0,
95         .end    = 0,
96         .flags  = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
97 } };
98
99 static struct resource video_rom_resource = {
100         .name   = "Video ROM",
101         .start  = 0xc0000,
102         .end    = 0xc7fff,
103         .flags  = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
104 };
105
106 static struct resource video_ram_resource = {
107         .name   = "Video RAM area",
108         .start  = 0xa0000,
109         .end    = 0xbffff,
110         .flags  = IORESOURCE_BUSY | IORESOURCE_MEM
111 };
112
113 static struct resource standard_io_resources[] = { {
114         .name   = "dma1",
115         .start  = 0x0000,
116         .end    = 0x001f,
117         .flags  = IORESOURCE_BUSY | IORESOURCE_IO
118 }, {
119         .name   = "pic1",
120         .start  = 0x0020,
121         .end    = 0x0021,
122         .flags  = IORESOURCE_BUSY | IORESOURCE_IO
123 }, {
124         .name   = "timer0",
125         .start  = 0x0040,
126         .end    = 0x0043,
127         .flags  = IORESOURCE_BUSY | IORESOURCE_IO
128 }, {
129         .name   = "timer1",
130         .start  = 0x0050,
131         .end    = 0x0053,
132         .flags  = IORESOURCE_BUSY | IORESOURCE_IO
133 }, {
134         .name   = "keyboard",
135         .start  = 0x0060,
136         .end    = 0x006f,
137         .flags  = IORESOURCE_BUSY | IORESOURCE_IO
138 }, {
139         .name   = "dma page reg",
140         .start  = 0x0080,
141         .end    = 0x008f,
142         .flags  = IORESOURCE_BUSY | IORESOURCE_IO
143 }, {
144         .name   = "pic2",
145         .start  = 0x00a0,
146         .end    = 0x00a1,
147         .flags  = IORESOURCE_BUSY | IORESOURCE_IO
148 }, {
149         .name   = "dma2",
150         .start  = 0x00c0,
151         .end    = 0x00df,
152         .flags  = IORESOURCE_BUSY | IORESOURCE_IO
153 }, {
154         .name   = "fpu",
155         .start  = 0x00f0,
156         .end    = 0x00ff,
157         .flags  = IORESOURCE_BUSY | IORESOURCE_IO
158 } };
159
160 #define ROMSIGNATURE 0xaa55
161
162 static int __init romsignature(const unsigned char *rom)
163 {
164         const unsigned short * const ptr = (const unsigned short *)rom;
165         unsigned short sig;
166
167         return probe_kernel_address(ptr, sig) == 0 && sig == ROMSIGNATURE;
168 }
169
170 static int __init romchecksum(const unsigned char *rom, unsigned long length)
171 {
172         unsigned char sum, c;
173
174         for (sum = 0; length && probe_kernel_address(rom++, c) == 0; length--)
175                 sum += c;
176         return !length && !sum;
177 }
178
179 static void __init probe_roms(void)
180 {
181         const unsigned char *rom;
182         unsigned long start, length, upper;
183         unsigned char c;
184         int i;
185
186         /* video rom */
187         upper = adapter_rom_resources[0].start;
188         for (start = video_rom_resource.start; start < upper; start += 2048) {
189                 rom = isa_bus_to_virt(start);
190                 if (!romsignature(rom))
191                         continue;
192
193                 video_rom_resource.start = start;
194
195                 if (probe_kernel_address(rom + 2, c) != 0)
196                         continue;
197
198                 /* 0 < length <= 0x7f * 512, historically */
199                 length = c * 512;
200
201                 /* if checksum okay, trust length byte */
202                 if (length && romchecksum(rom, length))
203                         video_rom_resource.end = start + length - 1;
204
205                 request_resource(&iomem_resource, &video_rom_resource);
206                 break;
207         }
208
209         start = (video_rom_resource.end + 1 + 2047) & ~2047UL;
210         if (start < upper)
211                 start = upper;
212
213         /* system rom */
214         request_resource(&iomem_resource, &system_rom_resource);
215         upper = system_rom_resource.start;
216
217         /* check for extension rom (ignore length byte!) */
218         rom = isa_bus_to_virt(extension_rom_resource.start);
219         if (romsignature(rom)) {
220                 length = extension_rom_resource.end - extension_rom_resource.start + 1;
221                 if (romchecksum(rom, length)) {
222                         request_resource(&iomem_resource, &extension_rom_resource);
223                         upper = extension_rom_resource.start;
224                 }
225         }
226
227         /* check for adapter roms on 2k boundaries */
228         for (i = 0; i < ARRAY_SIZE(adapter_rom_resources) && start < upper; start += 2048) {
229                 rom = isa_bus_to_virt(start);
230                 if (!romsignature(rom))
231                         continue;
232
233                 if (probe_kernel_address(rom + 2, c) != 0)
234                         continue;
235
236                 /* 0 < length <= 0x7f * 512, historically */
237                 length = c * 512;
238
239                 /* but accept any length that fits if checksum okay */
240                 if (!length || start + length > upper || !romchecksum(rom, length))
241                         continue;
242
243                 adapter_rom_resources[i].start = start;
244                 adapter_rom_resources[i].end = start + length - 1;
245                 request_resource(&iomem_resource, &adapter_rom_resources[i]);
246
247                 start = adapter_rom_resources[i++].end & ~2047UL;
248         }
249 }
250
251 /*
252  * Request address space for all standard RAM and ROM resources
253  * and also for regions reported as reserved by the e820.
254  */
255 static void __init
256 legacy_init_iomem_resources(struct resource *code_resource, struct resource *data_resource)
257 {
258         int i;
259
260         probe_roms();
261         for (i = 0; i < e820.nr_map; i++) {
262                 struct resource *res;
263 #ifndef CONFIG_RESOURCES_64BIT
264                 if (e820.map[i].addr + e820.map[i].size > 0x100000000ULL)
265                         continue;
266 #endif
267                 res = kzalloc(sizeof(struct resource), GFP_ATOMIC);
268                 switch (e820.map[i].type) {
269                 case E820_RAM:  res->name = "System RAM"; break;
270                 case E820_ACPI: res->name = "ACPI Tables"; break;
271                 case E820_NVS:  res->name = "ACPI Non-volatile Storage"; break;
272                 default:        res->name = "reserved";
273                 }
274                 res->start = e820.map[i].addr;
275                 res->end = res->start + e820.map[i].size - 1;
276                 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
277                 if (request_resource(&iomem_resource, res)) {
278                         kfree(res);
279                         continue;
280                 }
281                 if (e820.map[i].type == E820_RAM) {
282                         /*
283                          *  We don't know which RAM region contains kernel data,
284                          *  so we try it repeatedly and let the resource manager
285                          *  test it.
286                          */
287                         request_resource(res, code_resource);
288                         request_resource(res, data_resource);
289 #ifdef CONFIG_KEXEC
290                         request_resource(res, &crashk_res);
291 #endif
292                 }
293         }
294 }
295
296 /*
297  * Request address space for all standard resources
298  *
299  * This is called just before pcibios_init(), which is also a
300  * subsys_initcall, but is linked in later (in arch/i386/pci/common.c).
301  */
302 static int __init request_standard_resources(void)
303 {
304         int i;
305
306         printk("Setting up standard PCI resources\n");
307         if (efi_enabled)
308                 efi_initialize_iomem_resources(&code_resource, &data_resource);
309         else
310                 legacy_init_iomem_resources(&code_resource, &data_resource);
311
312         /* EFI systems may still have VGA */
313         request_resource(&iomem_resource, &video_ram_resource);
314
315         /* request I/O space for devices used on all i[345]86 PCs */
316         for (i = 0; i < ARRAY_SIZE(standard_io_resources); i++)
317                 request_resource(&ioport_resource, &standard_io_resources[i]);
318         return 0;
319 }
320
321 subsys_initcall(request_standard_resources);
322
323 void __init add_memory_region(unsigned long long start,
324                               unsigned long long size, int type)
325 {
326         int x;
327
328         if (!efi_enabled) {
329                 x = e820.nr_map;
330
331                 if (x == E820MAX) {
332                     printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
333                     return;
334                 }
335
336                 e820.map[x].addr = start;
337                 e820.map[x].size = size;
338                 e820.map[x].type = type;
339                 e820.nr_map++;
340         }
341 } /* add_memory_region */
342
343 /*
344  * Sanitize the BIOS e820 map.
345  *
346  * Some e820 responses include overlapping entries.  The following
347  * replaces the original e820 map with a new one, removing overlaps.
348  *
349  */
350 int __init sanitize_e820_map(struct e820entry * biosmap, char * pnr_map)
351 {
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         printk("sanitize start\n");
397         /* if there's only one memory region, don't bother */
398         if (*pnr_map < 2) {
399                 printk("sanitize bail 0\n");
400                 return -1;
401         }
402
403         old_nr = *pnr_map;
404
405         /* bail out if we find any unreasonable addresses in bios map */
406         for (i=0; i<old_nr; i++)
407                 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr) {
408                         printk("sanitize bail 1\n");
409                         return -1;
410                 }
411
412         /* create pointers for initial change-point information (for sorting) */
413         for (i=0; i < 2*old_nr; i++)
414                 change_point[i] = &change_point_list[i];
415
416         /* record all known change-points (starting and ending addresses),
417            omitting those that are for empty memory regions */
418         chgidx = 0;
419         for (i=0; i < old_nr; i++)      {
420                 if (biosmap[i].size != 0) {
421                         change_point[chgidx]->addr = biosmap[i].addr;
422                         change_point[chgidx++]->pbios = &biosmap[i];
423                         change_point[chgidx]->addr = biosmap[i].addr + biosmap[i].size;
424                         change_point[chgidx++]->pbios = &biosmap[i];
425                 }
426         }
427         chg_nr = chgidx;        /* true number of change-points */
428
429         /* sort change-point list by memory addresses (low -> high) */
430         still_changing = 1;
431         while (still_changing)  {
432                 still_changing = 0;
433                 for (i=1; i < chg_nr; i++)  {
434                         /* if <current_addr> > <last_addr>, swap */
435                         /* or, if current=<start_addr> & last=<end_addr>, swap */
436                         if ((change_point[i]->addr < change_point[i-1]->addr) ||
437                                 ((change_point[i]->addr == change_point[i-1]->addr) &&
438                                  (change_point[i]->addr == change_point[i]->pbios->addr) &&
439                                  (change_point[i-1]->addr != change_point[i-1]->pbios->addr))
440                            )
441                         {
442                                 change_tmp = change_point[i];
443                                 change_point[i] = change_point[i-1];
444                                 change_point[i-1] = change_tmp;
445                                 still_changing=1;
446                         }
447                 }
448         }
449
450         /* create a new bios memory map, removing overlaps */
451         overlap_entries=0;       /* number of entries in the overlap table */
452         new_bios_entry=0;        /* index for creating new bios map entries */
453         last_type = 0;           /* start with undefined memory type */
454         last_addr = 0;           /* start with 0 as last starting address */
455         /* loop through change-points, determining affect on the new bios map */
456         for (chgidx=0; chgidx < chg_nr; chgidx++)
457         {
458                 /* keep track of all overlapping bios entries */
459                 if (change_point[chgidx]->addr == change_point[chgidx]->pbios->addr)
460                 {
461                         /* add map entry to overlap list (> 1 entry implies an overlap) */
462                         overlap_list[overlap_entries++]=change_point[chgidx]->pbios;
463                 }
464                 else
465                 {
466                         /* remove entry from list (order independent, so swap with last) */
467                         for (i=0; i<overlap_entries; i++)
468                         {
469                                 if (overlap_list[i] == change_point[chgidx]->pbios)
470                                         overlap_list[i] = overlap_list[overlap_entries-1];
471                         }
472                         overlap_entries--;
473                 }
474                 /* if there are overlapping entries, decide which "type" to use */
475                 /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */
476                 current_type = 0;
477                 for (i=0; i<overlap_entries; i++)
478                         if (overlap_list[i]->type > current_type)
479                                 current_type = overlap_list[i]->type;
480                 /* continue building up new bios map based on this information */
481                 if (current_type != last_type)  {
482                         if (last_type != 0)      {
483                                 new_bios[new_bios_entry].size =
484                                         change_point[chgidx]->addr - last_addr;
485                                 /* move forward only if the new size was non-zero */
486                                 if (new_bios[new_bios_entry].size != 0)
487                                         if (++new_bios_entry >= E820MAX)
488                                                 break;  /* no more space left for new bios entries */
489                         }
490                         if (current_type != 0)  {
491                                 new_bios[new_bios_entry].addr = change_point[chgidx]->addr;
492                                 new_bios[new_bios_entry].type = current_type;
493                                 last_addr=change_point[chgidx]->addr;
494                         }
495                         last_type = current_type;
496                 }
497         }
498         new_nr = new_bios_entry;   /* retain count for new bios entries */
499
500         /* copy new bios mapping into original location */
501         memcpy(biosmap, new_bios, new_nr*sizeof(struct e820entry));
502         *pnr_map = new_nr;
503
504         printk("sanitize end\n");
505         return 0;
506 }
507
508 /*
509  * Copy the BIOS e820 map into a safe place.
510  *
511  * Sanity-check it while we're at it..
512  *
513  * If we're lucky and live on a modern system, the setup code
514  * will have given us a memory map that we can use to properly
515  * set up memory.  If we aren't, we'll fake a memory map.
516  *
517  * We check to see that the memory map contains at least 2 elements
518  * before we'll use it, because the detection code in setup.S may
519  * not be perfect and most every PC known to man has two memory
520  * regions: one from 0 to 640k, and one from 1mb up.  (The IBM
521  * thinkpad 560x, for example, does not cooperate with the memory
522  * detection code.)
523  */
524 int __init copy_e820_map(struct e820entry * biosmap, int nr_map)
525 {
526         /* Only one memory region (or negative)? Ignore it */
527         if (nr_map < 2)
528                 return -1;
529
530         do {
531                 unsigned long long start = biosmap->addr;
532                 unsigned long long size = biosmap->size;
533                 unsigned long long end = start + size;
534                 unsigned long type = biosmap->type;
535                 printk("copy_e820_map() start: %016Lx size: %016Lx end: %016Lx type: %ld\n", start, size, end, 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                 if (type == E820_RAM) {
546                         printk("copy_e820_map() type is E820_RAM\n");
547                         if (start < 0x100000ULL && end > 0xA0000ULL) {
548                                 printk("copy_e820_map() lies in range...\n");
549                                 if (start < 0xA0000ULL) {
550                                         printk("copy_e820_map() start < 0xA0000ULL\n");
551                                         add_memory_region(start, 0xA0000ULL-start, type);
552                                 }
553                                 if (end <= 0x100000ULL) {
554                                         printk("copy_e820_map() end <= 0x100000ULL\n");
555                                         continue;
556                                 }
557                                 start = 0x100000ULL;
558                                 size = end - start;
559                         }
560                 }
561                 add_memory_region(start, size, type);
562         } while (biosmap++,--nr_map);
563         return 0;
564 }
565
566 /*
567  * Callback for efi_memory_walk.
568  */
569 static int __init
570 efi_find_max_pfn(unsigned long start, unsigned long end, void *arg)
571 {
572         unsigned long *max_pfn = arg, pfn;
573
574         if (start < end) {
575                 pfn = PFN_UP(end -1);
576                 if (pfn > *max_pfn)
577                         *max_pfn = pfn;
578         }
579         return 0;
580 }
581
582 static int __init
583 efi_memory_present_wrapper(unsigned long start, unsigned long end, void *arg)
584 {
585         memory_present(0, PFN_UP(start), PFN_DOWN(end));
586         return 0;
587 }
588
589 /*
590  * Find the highest page frame number we have available
591  */
592 void __init find_max_pfn(void)
593 {
594         int i;
595
596         max_pfn = 0;
597         if (efi_enabled) {
598                 efi_memmap_walk(efi_find_max_pfn, &max_pfn);
599                 efi_memmap_walk(efi_memory_present_wrapper, NULL);
600                 return;
601         }
602
603         for (i = 0; i < e820.nr_map; i++) {
604                 unsigned long start, end;
605                 /* RAM? */
606                 if (e820.map[i].type != E820_RAM)
607                         continue;
608                 start = PFN_UP(e820.map[i].addr);
609                 end = PFN_DOWN(e820.map[i].addr + e820.map[i].size);
610                 if (start >= end)
611                         continue;
612                 if (end > max_pfn)
613                         max_pfn = end;
614                 memory_present(0, start, end);
615         }
616 }
617
618 /*
619  * Free all available memory for boot time allocation.  Used
620  * as a callback function by efi_memory_walk()
621  */
622
623 static int __init
624 free_available_memory(unsigned long start, unsigned long end, void *arg)
625 {
626         /* check max_low_pfn */
627         if (start >= (max_low_pfn << PAGE_SHIFT))
628                 return 0;
629         if (end >= (max_low_pfn << PAGE_SHIFT))
630                 end = max_low_pfn << PAGE_SHIFT;
631         if (start < end)
632                 free_bootmem(start, end - start);
633
634         return 0;
635 }
636 /*
637  * Register fully available low RAM pages with the bootmem allocator.
638  */
639 void __init register_bootmem_low_pages(unsigned long max_low_pfn)
640 {
641         int i;
642
643         if (efi_enabled) {
644                 efi_memmap_walk(free_available_memory, NULL);
645                 return;
646         }
647         for (i = 0; i < e820.nr_map; i++) {
648                 unsigned long curr_pfn, last_pfn, size;
649                 /*
650                  * Reserve usable low memory
651                  */
652                 if (e820.map[i].type != E820_RAM)
653                         continue;
654                 /*
655                  * We are rounding up the start address of usable memory:
656                  */
657                 curr_pfn = PFN_UP(e820.map[i].addr);
658                 if (curr_pfn >= max_low_pfn)
659                         continue;
660                 /*
661                  * ... and at the end of the usable range downwards:
662                  */
663                 last_pfn = PFN_DOWN(e820.map[i].addr + e820.map[i].size);
664
665                 if (last_pfn > max_low_pfn)
666                         last_pfn = max_low_pfn;
667
668                 /*
669                  * .. finally, did all the rounding and playing
670                  * around just make the area go away?
671                  */
672                 if (last_pfn <= curr_pfn)
673                         continue;
674
675                 size = last_pfn - curr_pfn;
676                 free_bootmem(PFN_PHYS(curr_pfn), PFN_PHYS(size));
677         }
678 }
679
680 void __init e820_register_memory(void)
681 {
682         unsigned long gapstart, gapsize, round;
683         unsigned long long last;
684         int i;
685
686         /*
687          * Search for the bigest gap in the low 32 bits of the e820
688          * memory space.
689          */
690         last = 0x100000000ull;
691         gapstart = 0x10000000;
692         gapsize = 0x400000;
693         i = e820.nr_map;
694         while (--i >= 0) {
695                 unsigned long long start = e820.map[i].addr;
696                 unsigned long long end = start + e820.map[i].size;
697
698                 /*
699                  * Since "last" is at most 4GB, we know we'll
700                  * fit in 32 bits if this condition is true
701                  */
702                 if (last > end) {
703                         unsigned long gap = last - end;
704
705                         if (gap > gapsize) {
706                                 gapsize = gap;
707                                 gapstart = end;
708                         }
709                 }
710                 if (start < last)
711                         last = start;
712         }
713
714         /*
715          * See how much we want to round up: start off with
716          * rounding to the next 1MB area.
717          */
718         round = 0x100000;
719         while ((gapsize >> 4) > round)
720                 round += round;
721         /* Fun with two's complement */
722         pci_mem_start = (gapstart + round) & -round;
723
724         printk("Allocating PCI resources starting at %08lx (gap: %08lx:%08lx)\n",
725                 pci_mem_start, gapstart, gapsize);
726 }
727
728 void __init print_memory_map(char *who)
729 {
730         int i;
731
732         for (i = 0; i < e820.nr_map; i++) {
733                 printk(" %s: %016Lx - %016Lx ", who,
734                         e820.map[i].addr,
735                         e820.map[i].addr + e820.map[i].size);
736                 switch (e820.map[i].type) {
737                 case E820_RAM:  printk("(usable)\n");
738                                 break;
739                 case E820_RESERVED:
740                                 printk("(reserved)\n");
741                                 break;
742                 case E820_ACPI:
743                                 printk("(ACPI data)\n");
744                                 break;
745                 case E820_NVS:
746                                 printk("(ACPI NVS)\n");
747                                 break;
748                 default:        printk("type %lu\n", e820.map[i].type);
749                                 break;
750                 }
751         }
752 }
753
754 static __init __always_inline void efi_limit_regions(unsigned long long size)
755 {
756         unsigned long long current_addr = 0;
757         efi_memory_desc_t *md, *next_md;
758         void *p, *p1;
759         int i, j;
760
761         j = 0;
762         p1 = memmap.map;
763         for (p = p1, i = 0; p < memmap.map_end; p += memmap.desc_size, i++) {
764                 md = p;
765                 next_md = p1;
766                 current_addr = md->phys_addr +
767                         PFN_PHYS(md->num_pages);
768                 if (is_available_memory(md)) {
769                         if (md->phys_addr >= size) continue;
770                         memcpy(next_md, md, memmap.desc_size);
771                         if (current_addr >= size) {
772                                 next_md->num_pages -=
773                                         PFN_UP(current_addr-size);
774                         }
775                         p1 += memmap.desc_size;
776                         next_md = p1;
777                         j++;
778                 } else if ((md->attribute & EFI_MEMORY_RUNTIME) ==
779                            EFI_MEMORY_RUNTIME) {
780                         /* In order to make runtime services
781                          * available we have to include runtime
782                          * memory regions in memory map */
783                         memcpy(next_md, md, memmap.desc_size);
784                         p1 += memmap.desc_size;
785                         next_md = p1;
786                         j++;
787                 }
788         }
789         memmap.nr_map = j;
790         memmap.map_end = memmap.map +
791                 (memmap.nr_map * memmap.desc_size);
792 }
793
794 void __init limit_regions(unsigned long long size)
795 {
796         unsigned long long current_addr;
797         int i;
798
799         print_memory_map("limit_regions start");
800         if (efi_enabled) {
801                 efi_limit_regions(size);
802                 return;
803         }
804         for (i = 0; i < e820.nr_map; i++) {
805                 current_addr = e820.map[i].addr + e820.map[i].size;
806                 if (current_addr < size)
807                         continue;
808
809                 if (e820.map[i].type != E820_RAM)
810                         continue;
811
812                 if (e820.map[i].addr >= size) {
813                         /*
814                          * This region starts past the end of the
815                          * requested size, skip it completely.
816                          */
817                         e820.nr_map = i;
818                 } else {
819                         e820.nr_map = i + 1;
820                         e820.map[i].size -= current_addr - size;
821                 }
822                 print_memory_map("limit_regions endfor");
823                 return;
824         }
825         print_memory_map("limit_regions endfunc");
826 }
827
828 /*
829  * This function checks if any part of the range <start,end> is mapped
830  * with type.
831  */
832 int
833 e820_any_mapped(u64 start, u64 end, unsigned type)
834 {
835         int i;
836         for (i = 0; i < e820.nr_map; i++) {
837                 const struct e820entry *ei = &e820.map[i];
838                 if (type && ei->type != type)
839                         continue;
840                 if (ei->addr >= end || ei->addr + ei->size <= start)
841                         continue;
842                 return 1;
843         }
844         return 0;
845 }
846 EXPORT_SYMBOL_GPL(e820_any_mapped);
847
848  /*
849   * This function checks if the entire range <start,end> is mapped with type.
850   *
851   * Note: this function only works correct if the e820 table is sorted and
852   * not-overlapping, which is the case
853   */
854 int __init
855 e820_all_mapped(unsigned long s, unsigned long e, unsigned type)
856 {
857         u64 start = s;
858         u64 end = e;
859         int i;
860         for (i = 0; i < e820.nr_map; i++) {
861                 struct e820entry *ei = &e820.map[i];
862                 if (type && ei->type != type)
863                         continue;
864                 /* is the region (part) in overlap with the current region ?*/
865                 if (ei->addr >= end || ei->addr + ei->size <= start)
866                         continue;
867                 /* if the region is at the beginning of <start,end> we move
868                  * start to the end of the region since it's ok until there
869                  */
870                 if (ei->addr <= start)
871                         start = ei->addr + ei->size;
872                 /* if start is now at or beyond end, we're done, full
873                  * coverage */
874                 if (start >= end)
875                         return 1; /* we're done */
876         }
877         return 0;
878 }
879
880 static int __init parse_memmap(char *arg)
881 {
882         if (!arg)
883                 return -EINVAL;
884
885         if (strcmp(arg, "exactmap") == 0) {
886 #ifdef CONFIG_CRASH_DUMP
887                 /* If we are doing a crash dump, we
888                  * still need to know the real mem
889                  * size before original memory map is
890                  * reset.
891                  */
892                 find_max_pfn();
893                 saved_max_pfn = max_pfn;
894 #endif
895                 e820.nr_map = 0;
896                 user_defined_memmap = 1;
897         } else {
898                 /* If the user specifies memory size, we
899                  * limit the BIOS-provided memory map to
900                  * that size. exactmap can be used to specify
901                  * the exact map. mem=number can be used to
902                  * trim the existing memory map.
903                  */
904                 unsigned long long start_at, mem_size;
905
906                 mem_size = memparse(arg, &arg);
907                 if (*arg == '@') {
908                         start_at = memparse(arg+1, &arg);
909                         add_memory_region(start_at, mem_size, E820_RAM);
910                 } else if (*arg == '#') {
911                         start_at = memparse(arg+1, &arg);
912                         add_memory_region(start_at, mem_size, E820_ACPI);
913                 } else if (*arg == '$') {
914                         start_at = memparse(arg+1, &arg);
915                         add_memory_region(start_at, mem_size, E820_RESERVED);
916                 } else {
917                         limit_regions(mem_size);
918                         user_defined_memmap = 1;
919                 }
920         }
921         return 0;
922 }
923 early_param("memmap", parse_memmap);