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