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