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