]> bbs.cooldavid.org Git - net-next-2.6.git/blob - arch/x86/kernel/e820_32.c
Use extended crashkernel command line on i386
[net-next-2.6.git] / arch / x86 / kernel / e820_32.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                         if (crashk_res.start != crashk_res.end)
292                                 request_resource(res, &crashk_res);
293 #endif
294                 }
295         }
296 }
297
298 /*
299  * Request address space for all standard resources
300  *
301  * This is called just before pcibios_init(), which is also a
302  * subsys_initcall, but is linked in later (in arch/i386/pci/common.c).
303  */
304 static int __init request_standard_resources(void)
305 {
306         int i;
307
308         printk("Setting up standard PCI resources\n");
309         if (efi_enabled)
310                 efi_initialize_iomem_resources(&code_resource, &data_resource);
311         else
312                 legacy_init_iomem_resources(&code_resource, &data_resource);
313
314         /* EFI systems may still have VGA */
315         request_resource(&iomem_resource, &video_ram_resource);
316
317         /* request I/O space for devices used on all i[345]86 PCs */
318         for (i = 0; i < ARRAY_SIZE(standard_io_resources); i++)
319                 request_resource(&ioport_resource, &standard_io_resources[i]);
320         return 0;
321 }
322
323 subsys_initcall(request_standard_resources);
324
325 #if defined(CONFIG_PM) && defined(CONFIG_HIBERNATION)
326 /**
327  * e820_mark_nosave_regions - Find the ranges of physical addresses that do not
328  * correspond to e820 RAM areas and mark the corresponding pages as nosave for
329  * hibernation.
330  *
331  * This function requires the e820 map to be sorted and without any
332  * overlapping entries and assumes the first e820 area to be RAM.
333  */
334 void __init e820_mark_nosave_regions(void)
335 {
336         int i;
337         unsigned long pfn;
338
339         pfn = PFN_DOWN(e820.map[0].addr + e820.map[0].size);
340         for (i = 1; i < e820.nr_map; i++) {
341                 struct e820entry *ei = &e820.map[i];
342
343                 if (pfn < PFN_UP(ei->addr))
344                         register_nosave_region(pfn, PFN_UP(ei->addr));
345
346                 pfn = PFN_DOWN(ei->addr + ei->size);
347                 if (ei->type != E820_RAM)
348                         register_nosave_region(PFN_UP(ei->addr), pfn);
349
350                 if (pfn >= max_low_pfn)
351                         break;
352         }
353 }
354 #endif
355
356 void __init add_memory_region(unsigned long long start,
357                               unsigned long long size, int type)
358 {
359         int x;
360
361         if (!efi_enabled) {
362                 x = e820.nr_map;
363
364                 if (x == E820MAX) {
365                     printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
366                     return;
367                 }
368
369                 e820.map[x].addr = start;
370                 e820.map[x].size = size;
371                 e820.map[x].type = type;
372                 e820.nr_map++;
373         }
374 } /* add_memory_region */
375
376 /*
377  * Sanitize the BIOS e820 map.
378  *
379  * Some e820 responses include overlapping entries.  The following
380  * replaces the original e820 map with a new one, removing overlaps.
381  *
382  */
383 int __init sanitize_e820_map(struct e820entry * biosmap, char * pnr_map)
384 {
385         struct change_member *change_tmp;
386         unsigned long current_type, last_type;
387         unsigned long long last_addr;
388         int chgidx, still_changing;
389         int overlap_entries;
390         int new_bios_entry;
391         int old_nr, new_nr, chg_nr;
392         int i;
393
394         /*
395                 Visually we're performing the following (1,2,3,4 = memory types)...
396
397                 Sample memory map (w/overlaps):
398                    ____22__________________
399                    ______________________4_
400                    ____1111________________
401                    _44_____________________
402                    11111111________________
403                    ____________________33__
404                    ___________44___________
405                    __________33333_________
406                    ______________22________
407                    ___________________2222_
408                    _________111111111______
409                    _____________________11_
410                    _________________4______
411
412                 Sanitized equivalent (no overlap):
413                    1_______________________
414                    _44_____________________
415                    ___1____________________
416                    ____22__________________
417                    ______11________________
418                    _________1______________
419                    __________3_____________
420                    ___________44___________
421                    _____________33_________
422                    _______________2________
423                    ________________1_______
424                    _________________4______
425                    ___________________2____
426                    ____________________33__
427                    ______________________4_
428         */
429         /* if there's only one memory region, don't bother */
430         if (*pnr_map < 2) {
431                 return -1;
432         }
433
434         old_nr = *pnr_map;
435
436         /* bail out if we find any unreasonable addresses in bios map */
437         for (i=0; i<old_nr; i++)
438                 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr) {
439                         return -1;
440                 }
441
442         /* create pointers for initial change-point information (for sorting) */
443         for (i=0; i < 2*old_nr; i++)
444                 change_point[i] = &change_point_list[i];
445
446         /* record all known change-points (starting and ending addresses),
447            omitting those that are for empty memory regions */
448         chgidx = 0;
449         for (i=0; i < old_nr; i++)      {
450                 if (biosmap[i].size != 0) {
451                         change_point[chgidx]->addr = biosmap[i].addr;
452                         change_point[chgidx++]->pbios = &biosmap[i];
453                         change_point[chgidx]->addr = biosmap[i].addr + biosmap[i].size;
454                         change_point[chgidx++]->pbios = &biosmap[i];
455                 }
456         }
457         chg_nr = chgidx;        /* true number of change-points */
458
459         /* sort change-point list by memory addresses (low -> high) */
460         still_changing = 1;
461         while (still_changing)  {
462                 still_changing = 0;
463                 for (i=1; i < chg_nr; i++)  {
464                         /* if <current_addr> > <last_addr>, swap */
465                         /* or, if current=<start_addr> & last=<end_addr>, swap */
466                         if ((change_point[i]->addr < change_point[i-1]->addr) ||
467                                 ((change_point[i]->addr == change_point[i-1]->addr) &&
468                                  (change_point[i]->addr == change_point[i]->pbios->addr) &&
469                                  (change_point[i-1]->addr != change_point[i-1]->pbios->addr))
470                            )
471                         {
472                                 change_tmp = change_point[i];
473                                 change_point[i] = change_point[i-1];
474                                 change_point[i-1] = change_tmp;
475                                 still_changing=1;
476                         }
477                 }
478         }
479
480         /* create a new bios memory map, removing overlaps */
481         overlap_entries=0;       /* number of entries in the overlap table */
482         new_bios_entry=0;        /* index for creating new bios map entries */
483         last_type = 0;           /* start with undefined memory type */
484         last_addr = 0;           /* start with 0 as last starting address */
485         /* loop through change-points, determining affect on the new bios map */
486         for (chgidx=0; chgidx < chg_nr; chgidx++)
487         {
488                 /* keep track of all overlapping bios entries */
489                 if (change_point[chgidx]->addr == change_point[chgidx]->pbios->addr)
490                 {
491                         /* add map entry to overlap list (> 1 entry implies an overlap) */
492                         overlap_list[overlap_entries++]=change_point[chgidx]->pbios;
493                 }
494                 else
495                 {
496                         /* remove entry from list (order independent, so swap with last) */
497                         for (i=0; i<overlap_entries; i++)
498                         {
499                                 if (overlap_list[i] == change_point[chgidx]->pbios)
500                                         overlap_list[i] = overlap_list[overlap_entries-1];
501                         }
502                         overlap_entries--;
503                 }
504                 /* if there are overlapping entries, decide which "type" to use */
505                 /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */
506                 current_type = 0;
507                 for (i=0; i<overlap_entries; i++)
508                         if (overlap_list[i]->type > current_type)
509                                 current_type = overlap_list[i]->type;
510                 /* continue building up new bios map based on this information */
511                 if (current_type != last_type)  {
512                         if (last_type != 0)      {
513                                 new_bios[new_bios_entry].size =
514                                         change_point[chgidx]->addr - last_addr;
515                                 /* move forward only if the new size was non-zero */
516                                 if (new_bios[new_bios_entry].size != 0)
517                                         if (++new_bios_entry >= E820MAX)
518                                                 break;  /* no more space left for new bios entries */
519                         }
520                         if (current_type != 0)  {
521                                 new_bios[new_bios_entry].addr = change_point[chgidx]->addr;
522                                 new_bios[new_bios_entry].type = current_type;
523                                 last_addr=change_point[chgidx]->addr;
524                         }
525                         last_type = current_type;
526                 }
527         }
528         new_nr = new_bios_entry;   /* retain count for new bios entries */
529
530         /* copy new bios mapping into original location */
531         memcpy(biosmap, new_bios, new_nr*sizeof(struct e820entry));
532         *pnr_map = new_nr;
533
534         return 0;
535 }
536
537 /*
538  * Copy the BIOS e820 map into a safe place.
539  *
540  * Sanity-check it while we're at it..
541  *
542  * If we're lucky and live on a modern system, the setup code
543  * will have given us a memory map that we can use to properly
544  * set up memory.  If we aren't, we'll fake a memory map.
545  *
546  * We check to see that the memory map contains at least 2 elements
547  * before we'll use it, because the detection code in setup.S may
548  * not be perfect and most every PC known to man has two memory
549  * regions: one from 0 to 640k, and one from 1mb up.  (The IBM
550  * thinkpad 560x, for example, does not cooperate with the memory
551  * detection code.)
552  */
553 int __init copy_e820_map(struct e820entry * biosmap, int nr_map)
554 {
555         /* Only one memory region (or negative)? Ignore it */
556         if (nr_map < 2)
557                 return -1;
558
559         do {
560                 unsigned long long start = biosmap->addr;
561                 unsigned long long size = biosmap->size;
562                 unsigned long long end = start + size;
563                 unsigned long type = biosmap->type;
564
565                 /* Overflow in 64 bits? Ignore the memory map. */
566                 if (start > end)
567                         return -1;
568
569                 /*
570                  * Some BIOSes claim RAM in the 640k - 1M region.
571                  * Not right. Fix it up.
572                  */
573                 if (type == E820_RAM) {
574                         if (start < 0x100000ULL && end > 0xA0000ULL) {
575                                 if (start < 0xA0000ULL)
576                                         add_memory_region(start, 0xA0000ULL-start, type);
577                                 if (end <= 0x100000ULL)
578                                         continue;
579                                 start = 0x100000ULL;
580                                 size = end - start;
581                         }
582                 }
583                 add_memory_region(start, size, type);
584         } while (biosmap++,--nr_map);
585         return 0;
586 }
587
588 /*
589  * Callback for efi_memory_walk.
590  */
591 static int __init
592 efi_find_max_pfn(unsigned long start, unsigned long end, void *arg)
593 {
594         unsigned long *max_pfn = arg, pfn;
595
596         if (start < end) {
597                 pfn = PFN_UP(end -1);
598                 if (pfn > *max_pfn)
599                         *max_pfn = pfn;
600         }
601         return 0;
602 }
603
604 static int __init
605 efi_memory_present_wrapper(unsigned long start, unsigned long end, void *arg)
606 {
607         memory_present(0, PFN_UP(start), PFN_DOWN(end));
608         return 0;
609 }
610
611 /*
612  * Find the highest page frame number we have available
613  */
614 void __init find_max_pfn(void)
615 {
616         int i;
617
618         max_pfn = 0;
619         if (efi_enabled) {
620                 efi_memmap_walk(efi_find_max_pfn, &max_pfn);
621                 efi_memmap_walk(efi_memory_present_wrapper, NULL);
622                 return;
623         }
624
625         for (i = 0; i < e820.nr_map; i++) {
626                 unsigned long start, end;
627                 /* RAM? */
628                 if (e820.map[i].type != E820_RAM)
629                         continue;
630                 start = PFN_UP(e820.map[i].addr);
631                 end = PFN_DOWN(e820.map[i].addr + e820.map[i].size);
632                 if (start >= end)
633                         continue;
634                 if (end > max_pfn)
635                         max_pfn = end;
636                 memory_present(0, start, end);
637         }
638 }
639
640 /*
641  * Free all available memory for boot time allocation.  Used
642  * as a callback function by efi_memory_walk()
643  */
644
645 static int __init
646 free_available_memory(unsigned long start, unsigned long end, void *arg)
647 {
648         /* check max_low_pfn */
649         if (start >= (max_low_pfn << PAGE_SHIFT))
650                 return 0;
651         if (end >= (max_low_pfn << PAGE_SHIFT))
652                 end = max_low_pfn << PAGE_SHIFT;
653         if (start < end)
654                 free_bootmem(start, end - start);
655
656         return 0;
657 }
658 /*
659  * Register fully available low RAM pages with the bootmem allocator.
660  */
661 void __init register_bootmem_low_pages(unsigned long max_low_pfn)
662 {
663         int i;
664
665         if (efi_enabled) {
666                 efi_memmap_walk(free_available_memory, NULL);
667                 return;
668         }
669         for (i = 0; i < e820.nr_map; i++) {
670                 unsigned long curr_pfn, last_pfn, size;
671                 /*
672                  * Reserve usable low memory
673                  */
674                 if (e820.map[i].type != E820_RAM)
675                         continue;
676                 /*
677                  * We are rounding up the start address of usable memory:
678                  */
679                 curr_pfn = PFN_UP(e820.map[i].addr);
680                 if (curr_pfn >= max_low_pfn)
681                         continue;
682                 /*
683                  * ... and at the end of the usable range downwards:
684                  */
685                 last_pfn = PFN_DOWN(e820.map[i].addr + e820.map[i].size);
686
687                 if (last_pfn > max_low_pfn)
688                         last_pfn = max_low_pfn;
689
690                 /*
691                  * .. finally, did all the rounding and playing
692                  * around just make the area go away?
693                  */
694                 if (last_pfn <= curr_pfn)
695                         continue;
696
697                 size = last_pfn - curr_pfn;
698                 free_bootmem(PFN_PHYS(curr_pfn), PFN_PHYS(size));
699         }
700 }
701
702 void __init e820_register_memory(void)
703 {
704         unsigned long gapstart, gapsize, round;
705         unsigned long long last;
706         int i;
707
708         /*
709          * Search for the bigest gap in the low 32 bits of the e820
710          * memory space.
711          */
712         last = 0x100000000ull;
713         gapstart = 0x10000000;
714         gapsize = 0x400000;
715         i = e820.nr_map;
716         while (--i >= 0) {
717                 unsigned long long start = e820.map[i].addr;
718                 unsigned long long end = start + e820.map[i].size;
719
720                 /*
721                  * Since "last" is at most 4GB, we know we'll
722                  * fit in 32 bits if this condition is true
723                  */
724                 if (last > end) {
725                         unsigned long gap = last - end;
726
727                         if (gap > gapsize) {
728                                 gapsize = gap;
729                                 gapstart = end;
730                         }
731                 }
732                 if (start < last)
733                         last = start;
734         }
735
736         /*
737          * See how much we want to round up: start off with
738          * rounding to the next 1MB area.
739          */
740         round = 0x100000;
741         while ((gapsize >> 4) > round)
742                 round += round;
743         /* Fun with two's complement */
744         pci_mem_start = (gapstart + round) & -round;
745
746         printk("Allocating PCI resources starting at %08lx (gap: %08lx:%08lx)\n",
747                 pci_mem_start, gapstart, gapsize);
748 }
749
750 void __init print_memory_map(char *who)
751 {
752         int i;
753
754         for (i = 0; i < e820.nr_map; i++) {
755                 printk(" %s: %016Lx - %016Lx ", who,
756                         e820.map[i].addr,
757                         e820.map[i].addr + e820.map[i].size);
758                 switch (e820.map[i].type) {
759                 case E820_RAM:  printk("(usable)\n");
760                                 break;
761                 case E820_RESERVED:
762                                 printk("(reserved)\n");
763                                 break;
764                 case E820_ACPI:
765                                 printk("(ACPI data)\n");
766                                 break;
767                 case E820_NVS:
768                                 printk("(ACPI NVS)\n");
769                                 break;
770                 default:        printk("type %u\n", e820.map[i].type);
771                                 break;
772                 }
773         }
774 }
775
776 static __init __always_inline void efi_limit_regions(unsigned long long size)
777 {
778         unsigned long long current_addr = 0;
779         efi_memory_desc_t *md, *next_md;
780         void *p, *p1;
781         int i, j;
782
783         j = 0;
784         p1 = memmap.map;
785         for (p = p1, i = 0; p < memmap.map_end; p += memmap.desc_size, i++) {
786                 md = p;
787                 next_md = p1;
788                 current_addr = md->phys_addr +
789                         PFN_PHYS(md->num_pages);
790                 if (is_available_memory(md)) {
791                         if (md->phys_addr >= size) continue;
792                         memcpy(next_md, md, memmap.desc_size);
793                         if (current_addr >= size) {
794                                 next_md->num_pages -=
795                                         PFN_UP(current_addr-size);
796                         }
797                         p1 += memmap.desc_size;
798                         next_md = p1;
799                         j++;
800                 } else if ((md->attribute & EFI_MEMORY_RUNTIME) ==
801                            EFI_MEMORY_RUNTIME) {
802                         /* In order to make runtime services
803                          * available we have to include runtime
804                          * memory regions in memory map */
805                         memcpy(next_md, md, memmap.desc_size);
806                         p1 += memmap.desc_size;
807                         next_md = p1;
808                         j++;
809                 }
810         }
811         memmap.nr_map = j;
812         memmap.map_end = memmap.map +
813                 (memmap.nr_map * memmap.desc_size);
814 }
815
816 void __init limit_regions(unsigned long long size)
817 {
818         unsigned long long current_addr;
819         int i;
820
821         print_memory_map("limit_regions start");
822         if (efi_enabled) {
823                 efi_limit_regions(size);
824                 return;
825         }
826         for (i = 0; i < e820.nr_map; i++) {
827                 current_addr = e820.map[i].addr + e820.map[i].size;
828                 if (current_addr < size)
829                         continue;
830
831                 if (e820.map[i].type != E820_RAM)
832                         continue;
833
834                 if (e820.map[i].addr >= size) {
835                         /*
836                          * This region starts past the end of the
837                          * requested size, skip it completely.
838                          */
839                         e820.nr_map = i;
840                 } else {
841                         e820.nr_map = i + 1;
842                         e820.map[i].size -= current_addr - size;
843                 }
844                 print_memory_map("limit_regions endfor");
845                 return;
846         }
847         print_memory_map("limit_regions endfunc");
848 }
849
850 /*
851  * This function checks if any part of the range <start,end> is mapped
852  * with type.
853  */
854 int
855 e820_any_mapped(u64 start, u64 end, unsigned type)
856 {
857         int i;
858         for (i = 0; i < e820.nr_map; i++) {
859                 const struct e820entry *ei = &e820.map[i];
860                 if (type && ei->type != type)
861                         continue;
862                 if (ei->addr >= end || ei->addr + ei->size <= start)
863                         continue;
864                 return 1;
865         }
866         return 0;
867 }
868 EXPORT_SYMBOL_GPL(e820_any_mapped);
869
870  /*
871   * This function checks if the entire range <start,end> is mapped with type.
872   *
873   * Note: this function only works correct if the e820 table is sorted and
874   * not-overlapping, which is the case
875   */
876 int __init
877 e820_all_mapped(unsigned long s, unsigned long e, unsigned type)
878 {
879         u64 start = s;
880         u64 end = e;
881         int i;
882         for (i = 0; i < e820.nr_map; i++) {
883                 struct e820entry *ei = &e820.map[i];
884                 if (type && ei->type != type)
885                         continue;
886                 /* is the region (part) in overlap with the current region ?*/
887                 if (ei->addr >= end || ei->addr + ei->size <= start)
888                         continue;
889                 /* if the region is at the beginning of <start,end> we move
890                  * start to the end of the region since it's ok until there
891                  */
892                 if (ei->addr <= start)
893                         start = ei->addr + ei->size;
894                 /* if start is now at or beyond end, we're done, full
895                  * coverage */
896                 if (start >= end)
897                         return 1; /* we're done */
898         }
899         return 0;
900 }
901
902 static int __init parse_memmap(char *arg)
903 {
904         if (!arg)
905                 return -EINVAL;
906
907         if (strcmp(arg, "exactmap") == 0) {
908 #ifdef CONFIG_CRASH_DUMP
909                 /* If we are doing a crash dump, we
910                  * still need to know the real mem
911                  * size before original memory map is
912                  * reset.
913                  */
914                 find_max_pfn();
915                 saved_max_pfn = max_pfn;
916 #endif
917                 e820.nr_map = 0;
918                 user_defined_memmap = 1;
919         } else {
920                 /* If the user specifies memory size, we
921                  * limit the BIOS-provided memory map to
922                  * that size. exactmap can be used to specify
923                  * the exact map. mem=number can be used to
924                  * trim the existing memory map.
925                  */
926                 unsigned long long start_at, mem_size;
927
928                 mem_size = memparse(arg, &arg);
929                 if (*arg == '@') {
930                         start_at = memparse(arg+1, &arg);
931                         add_memory_region(start_at, mem_size, E820_RAM);
932                 } else if (*arg == '#') {
933                         start_at = memparse(arg+1, &arg);
934                         add_memory_region(start_at, mem_size, E820_ACPI);
935                 } else if (*arg == '$') {
936                         start_at = memparse(arg+1, &arg);
937                         add_memory_region(start_at, mem_size, E820_RESERVED);
938                 } else {
939                         limit_regions(mem_size);
940                         user_defined_memmap = 1;
941                 }
942         }
943         return 0;
944 }
945 early_param("memmap", parse_memmap);