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