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