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