Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / arch / x86 / kernel / e820-xen.c
1 /*
2  * Handle the memory map.
3  * The functions here do the job until bootmem takes over.
4  *
5  *  Getting sanitize_e820_map() in sync with i386 version by applying change:
6  *  -  Provisions for empty E820 memory regions (reported by certain BIOSes).
7  *     Alex Achenbach <xela@slit.de>, December 2002.
8  *  Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
9  *
10  */
11 #include <linux/kernel.h>
12 #include <linux/types.h>
13 #include <linux/init.h>
14 #include <linux/crash_dump.h>
15 #include <linux/export.h>
16 #include <linux/bootmem.h>
17 #include <linux/pfn.h>
18 #include <linux/suspend.h>
19 #include <linux/acpi.h>
20 #include <linux/firmware-map.h>
21 #include <linux/memblock.h>
22 #include <linux/sort.h>
23
24 #include <asm/e820.h>
25 #include <asm/proto.h>
26 #include <asm/setup.h>
27 #include <xen/interface/memory.h>
28
29 /*
30  * The e820 map is the map that gets modified e.g. with command line parameters
31  * and that is also registered with modifications in the kernel resource tree
32  * with the iomem_resource as parent.
33  *
34  * The e820_saved is directly saved after the BIOS-provided memory map is
35  * copied. It doesn't get modified afterwards. It's registered for the
36  * /sys/firmware/memmap interface.
37  *
38  * That memory map is not modified and is used as base for kexec. The kexec'd
39  * kernel should get the same memory map as the firmware provides. Then the
40  * user can e.g. boot the original kernel with mem=1G while still booting the
41  * next kernel with full memory.
42  */
43 struct e820map e820;
44 #if !defined(CONFIG_XEN)
45 struct e820map e820_saved;
46 #elif defined(CONFIG_XEN_PRIVILEGED_GUEST)
47 struct e820map machine_e820;
48 # define e820_saved machine_e820
49 #else
50 # define machine_e820 e820
51 # define e820_saved e820
52 #endif
53
54 /* For PCI or other memory-mapped resources */
55 unsigned long pci_mem_start = 0xaeedbabe;
56 #ifdef CONFIG_PCI
57 EXPORT_SYMBOL(pci_mem_start);
58 #endif
59
60 /*
61  * This function checks if any part of the range <start,end> is mapped
62  * with type.
63  */
64 int
65 e820_any_mapped(u64 start, u64 end, unsigned type)
66 {
67         int i;
68
69 #ifndef CONFIG_XEN
70         for (i = 0; i < e820.nr_map; i++) {
71                 struct e820entry *ei = &e820.map[i];
72 #else
73         if (!is_initial_xendomain())
74                 return 0;
75         for (i = 0; i < machine_e820.nr_map; ++i) {
76                 const struct e820entry *ei = &machine_e820.map[i];
77 #endif
78
79                 if (type && ei->type != type)
80                         continue;
81                 if (ei->addr >= end || ei->addr + ei->size <= start)
82                         continue;
83                 return 1;
84         }
85         return 0;
86 }
87 EXPORT_SYMBOL_GPL(e820_any_mapped);
88
89 /*
90  * This function checks if the entire range <start,end> is mapped with type.
91  *
92  * Note: this function only works correct if the e820 table is sorted and
93  * not-overlapping, which is the case
94  */
95 int __init e820_all_mapped(u64 start, u64 end, unsigned type)
96 {
97         int i;
98
99 #ifndef CONFIG_XEN
100         for (i = 0; i < e820.nr_map; i++) {
101                 struct e820entry *ei = &e820.map[i];
102 #else
103         if (!is_initial_xendomain())
104                 return 0;
105         for (i = 0; i < machine_e820.nr_map; ++i) {
106                 const struct e820entry *ei = &machine_e820.map[i];
107 #endif
108
109                 if (type && ei->type != type)
110                         continue;
111                 /* is the region (part) in overlap with the current region ?*/
112                 if (ei->addr >= end || ei->addr + ei->size <= start)
113                         continue;
114
115                 /* if the region is at the beginning of <start,end> we move
116                  * start to the end of the region since it's ok until there
117                  */
118                 if (ei->addr <= start)
119                         start = ei->addr + ei->size;
120                 /*
121                  * if start is now at or beyond end, we're done, full
122                  * coverage
123                  */
124                 if (start >= end)
125                         return 1;
126         }
127         return 0;
128 }
129
130 /*
131  * Add a memory region to the kernel e820 map.
132  */
133 static void __init __e820_add_region(struct e820map *e820x, u64 start, u64 size,
134                                          int type)
135 {
136         int x = e820x->nr_map;
137
138         if (x >= ARRAY_SIZE(e820x->map)) {
139                 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
140                 return;
141         }
142
143         e820x->map[x].addr = start;
144         e820x->map[x].size = size;
145         e820x->map[x].type = type;
146         e820x->nr_map++;
147 }
148
149 void __init e820_add_region(u64 start, u64 size, int type)
150 {
151         __e820_add_region(&e820, start, size, type);
152 }
153
154 static void __init e820_print_type(u32 type)
155 {
156         switch (type) {
157         case E820_RAM:
158         case E820_RESERVED_KERN:
159                 printk(KERN_CONT "(usable)");
160                 break;
161         case E820_RESERVED:
162                 printk(KERN_CONT "(reserved)");
163                 break;
164         case E820_ACPI:
165                 printk(KERN_CONT "(ACPI data)");
166                 break;
167         case E820_NVS:
168                 printk(KERN_CONT "(ACPI NVS)");
169                 break;
170         case E820_UNUSABLE:
171                 printk(KERN_CONT "(unusable)");
172                 break;
173         default:
174                 printk(KERN_CONT "type %u", type);
175                 break;
176         }
177 }
178
179 static void __init _e820_print_map(const struct e820map *e820, const char *who)
180 {
181         int i;
182
183         for (i = 0; i < e820->nr_map; i++) {
184                 printk(KERN_INFO " %s: %016Lx - %016Lx ", who,
185                        (unsigned long long) e820->map[i].addr,
186                        (unsigned long long)
187                        (e820->map[i].addr + e820->map[i].size));
188                 e820_print_type(e820->map[i].type);
189                 printk(KERN_CONT "\n");
190         }
191 }
192
193 /*
194  * Sanitize the BIOS e820 map.
195  *
196  * Some e820 responses include overlapping entries. The following
197  * replaces the original e820 map with a new one, removing overlaps,
198  * and resolving conflicting memory types in favor of highest
199  * numbered type.
200  *
201  * The input parameter biosmap points to an array of 'struct
202  * e820entry' which on entry has elements in the range [0, *pnr_map)
203  * valid, and which has space for up to max_nr_map entries.
204  * On return, the resulting sanitized e820 map entries will be in
205  * overwritten in the same location, starting at biosmap.
206  *
207  * The integer pointed to by pnr_map must be valid on entry (the
208  * current number of valid entries located at biosmap) and will
209  * be updated on return, with the new number of valid entries
210  * (something no more than max_nr_map.)
211  *
212  * The return value from sanitize_e820_map() is zero if it
213  * successfully 'sanitized' the map entries passed in, and is -1
214  * if it did nothing, which can happen if either of (1) it was
215  * only passed one map entry, or (2) any of the input map entries
216  * were invalid (start + size < start, meaning that the size was
217  * so big the described memory range wrapped around through zero.)
218  *
219  *      Visually we're performing the following
220  *      (1,2,3,4 = memory types)...
221  *
222  *      Sample memory map (w/overlaps):
223  *         ____22__________________
224  *         ______________________4_
225  *         ____1111________________
226  *         _44_____________________
227  *         11111111________________
228  *         ____________________33__
229  *         ___________44___________
230  *         __________33333_________
231  *         ______________22________
232  *         ___________________2222_
233  *         _________111111111______
234  *         _____________________11_
235  *         _________________4______
236  *
237  *      Sanitized equivalent (no overlap):
238  *         1_______________________
239  *         _44_____________________
240  *         ___1____________________
241  *         ____22__________________
242  *         ______11________________
243  *         _________1______________
244  *         __________3_____________
245  *         ___________44___________
246  *         _____________33_________
247  *         _______________2________
248  *         ________________1_______
249  *         _________________4______
250  *         ___________________2____
251  *         ____________________33__
252  *         ______________________4_
253  */
254 struct change_member {
255         struct e820entry *pbios; /* pointer to original bios entry */
256         unsigned long long addr; /* address for this change point */
257 };
258
259 static int __init cpcompare(const void *a, const void *b)
260 {
261         struct change_member * const *app = a, * const *bpp = b;
262         const struct change_member *ap = *app, *bp = *bpp;
263
264         /*
265          * Inputs are pointers to two elements of change_point[].  If their
266          * addresses are unequal, their difference dominates.  If the addresses
267          * are equal, then consider one that represents the end of its region
268          * to be greater than one that does not.
269          */
270         if (ap->addr != bp->addr)
271                 return ap->addr > bp->addr ? 1 : -1;
272
273         return (ap->addr != ap->pbios->addr) - (bp->addr != bp->pbios->addr);
274 }
275
276 int __init sanitize_e820_map(struct e820entry *biosmap, int max_nr_map,
277                              u32 *pnr_map)
278 {
279         static struct change_member change_point_list[2*E820_X_MAX] __initdata;
280         static struct change_member *change_point[2*E820_X_MAX] __initdata;
281         static struct e820entry *overlap_list[E820_X_MAX] __initdata;
282         static struct e820entry new_bios[E820_X_MAX] __initdata;
283         unsigned long current_type, last_type;
284         unsigned long long last_addr;
285         int chgidx;
286         int overlap_entries;
287         int new_bios_entry;
288         int old_nr, new_nr, chg_nr;
289         int i;
290
291         /* if there's only one memory region, don't bother */
292 #ifdef CONFIG_XEN
293         if (*pnr_map == 1)
294                 return 0;
295 #endif
296         if (*pnr_map < 2)
297                 return -1;
298
299         old_nr = *pnr_map;
300         BUG_ON(old_nr > max_nr_map);
301
302         /* bail out if we find any unreasonable addresses in bios map */
303         for (i = 0; i < old_nr; i++)
304                 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
305                         return -1;
306
307         /* create pointers for initial change-point information (for sorting) */
308         for (i = 0; i < 2 * old_nr; i++)
309                 change_point[i] = &change_point_list[i];
310
311         /* record all known change-points (starting and ending addresses),
312            omitting those that are for empty memory regions */
313         chgidx = 0;
314         for (i = 0; i < old_nr; i++)    {
315                 if (biosmap[i].size != 0) {
316                         change_point[chgidx]->addr = biosmap[i].addr;
317                         change_point[chgidx++]->pbios = &biosmap[i];
318                         change_point[chgidx]->addr = biosmap[i].addr +
319                                 biosmap[i].size;
320                         change_point[chgidx++]->pbios = &biosmap[i];
321                 }
322         }
323         chg_nr = chgidx;
324
325         /* sort change-point list by memory addresses (low -> high) */
326         sort(change_point, chg_nr, sizeof *change_point, cpcompare, NULL);
327
328         /* create a new bios memory map, removing overlaps */
329         overlap_entries = 0;     /* number of entries in the overlap table */
330         new_bios_entry = 0;      /* index for creating new bios map entries */
331         last_type = 0;           /* start with undefined memory type */
332         last_addr = 0;           /* start with 0 as last starting address */
333
334         /* loop through change-points, determining affect on the new bios map */
335         for (chgidx = 0; chgidx < chg_nr; chgidx++) {
336                 /* keep track of all overlapping bios entries */
337                 if (change_point[chgidx]->addr ==
338                     change_point[chgidx]->pbios->addr) {
339                         /*
340                          * add map entry to overlap list (> 1 entry
341                          * implies an overlap)
342                          */
343                         overlap_list[overlap_entries++] =
344                                 change_point[chgidx]->pbios;
345                 } else {
346                         /*
347                          * remove entry from list (order independent,
348                          * so swap with last)
349                          */
350                         for (i = 0; i < overlap_entries; i++) {
351                                 if (overlap_list[i] ==
352                                     change_point[chgidx]->pbios)
353                                         overlap_list[i] =
354                                                 overlap_list[overlap_entries-1];
355                         }
356                         overlap_entries--;
357                 }
358                 /*
359                  * if there are overlapping entries, decide which
360                  * "type" to use (larger value takes precedence --
361                  * 1=usable, 2,3,4,4+=unusable)
362                  */
363                 current_type = 0;
364                 for (i = 0; i < overlap_entries; i++)
365                         if (overlap_list[i]->type > current_type)
366                                 current_type = overlap_list[i]->type;
367                 /*
368                  * continue building up new bios map based on this
369                  * information
370                  */
371                 if (current_type != last_type)  {
372                         if (last_type != 0)      {
373                                 new_bios[new_bios_entry].size =
374                                         change_point[chgidx]->addr - last_addr;
375                                 /*
376                                  * move forward only if the new size
377                                  * was non-zero
378                                  */
379                                 if (new_bios[new_bios_entry].size != 0)
380                                         /*
381                                          * no more space left for new
382                                          * bios entries ?
383                                          */
384                                         if (++new_bios_entry >= max_nr_map)
385                                                 break;
386                         }
387                         if (current_type != 0)  {
388                                 new_bios[new_bios_entry].addr =
389                                         change_point[chgidx]->addr;
390                                 new_bios[new_bios_entry].type = current_type;
391                                 last_addr = change_point[chgidx]->addr;
392                         }
393                         last_type = current_type;
394                 }
395         }
396         /* retain count for new bios entries */
397         new_nr = new_bios_entry;
398
399         /* copy new bios mapping into original location */
400         memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
401         *pnr_map = new_nr;
402
403         return 0;
404 }
405
406 static int __init __append_e820_map(struct e820entry *biosmap, int nr_map)
407 {
408         while (nr_map) {
409                 u64 start = biosmap->addr;
410                 u64 size = biosmap->size;
411                 u64 end = start + size;
412                 u32 type = biosmap->type;
413
414                 /* Overflow in 64 bits? Ignore the memory map. */
415                 if (start > end)
416                         return -1;
417
418                 e820_add_region(start, size, type);
419
420                 biosmap++;
421                 nr_map--;
422         }
423         return 0;
424 }
425
426 /*
427  * Copy the BIOS e820 map into a safe place.
428  *
429  * Sanity-check it while we're at it..
430  *
431  * If we're lucky and live on a modern system, the setup code
432  * will have given us a memory map that we can use to properly
433  * set up memory.  If we aren't, we'll fake a memory map.
434  */
435 static int __init append_e820_map(struct e820entry *biosmap, int nr_map)
436 {
437 #ifndef CONFIG_XEN
438         /* Only one memory region (or negative)? Ignore it */
439         if (nr_map < 2)
440                 return -1;
441 #else
442         BUG_ON(nr_map < 1);
443 #endif
444
445         return __append_e820_map(biosmap, nr_map);
446 }
447
448 static u64 __init __e820_update_range(struct e820map *e820x, u64 start,
449                                         u64 size, unsigned old_type,
450                                         unsigned new_type)
451 {
452         u64 end;
453         unsigned int i;
454         u64 real_updated_size = 0;
455
456         BUG_ON(old_type == new_type);
457
458         if (size > (ULLONG_MAX - start))
459                 size = ULLONG_MAX - start;
460
461         end = start + size;
462         printk(KERN_DEBUG "e820 update range: %016Lx - %016Lx ",
463                        (unsigned long long) start,
464                        (unsigned long long) end);
465         e820_print_type(old_type);
466         printk(KERN_CONT " ==> ");
467         e820_print_type(new_type);
468         printk(KERN_CONT "\n");
469
470         for (i = 0; i < e820x->nr_map; i++) {
471                 struct e820entry *ei = &e820x->map[i];
472                 u64 final_start, final_end;
473                 u64 ei_end;
474
475                 if (ei->type != old_type)
476                         continue;
477
478                 ei_end = ei->addr + ei->size;
479                 /* totally covered by new range? */
480                 if (ei->addr >= start && ei_end <= end) {
481                         ei->type = new_type;
482                         real_updated_size += ei->size;
483                         continue;
484                 }
485
486                 /* new range is totally covered? */
487                 if (ei->addr < start && ei_end > end) {
488                         __e820_add_region(e820x, start, size, new_type);
489                         __e820_add_region(e820x, end, ei_end - end, ei->type);
490                         ei->size = start - ei->addr;
491                         real_updated_size += size;
492                         continue;
493                 }
494
495                 /* partially covered */
496                 final_start = max(start, ei->addr);
497                 final_end = min(end, ei_end);
498                 if (final_start >= final_end)
499                         continue;
500
501                 __e820_add_region(e820x, final_start, final_end - final_start,
502                                   new_type);
503
504                 real_updated_size += final_end - final_start;
505
506                 /*
507                  * left range could be head or tail, so need to update
508                  * size at first.
509                  */
510                 ei->size -= final_end - final_start;
511                 if (ei->addr < final_start)
512                         continue;
513                 ei->addr = final_end;
514         }
515         return real_updated_size;
516 }
517
518 u64 __init e820_update_range(u64 start, u64 size, unsigned old_type,
519                              unsigned new_type)
520 {
521         return __e820_update_range(&e820, start, size, old_type, new_type);
522 }
523
524 #ifndef CONFIG_XEN_UNPRIVILEGED_GUEST
525 static u64 __init e820_update_range_saved(u64 start, u64 size,
526                                           unsigned old_type, unsigned new_type)
527 {
528 #ifdef CONFIG_XEN
529         if (!is_initial_xendomain())
530                 return 0;
531         return __e820_update_range(&machine_e820, phys_to_machine(start),
532                                    size, old_type, new_type);
533 #else
534         return __e820_update_range(&e820_saved, start, size, old_type,
535                                      new_type);
536 #endif
537 }
538 #endif
539
540 /* make e820 not cover the range */
541 u64 __init e820_remove_range(u64 start, u64 size, unsigned old_type,
542                              int checktype)
543 {
544         int i;
545         u64 end;
546         u64 real_removed_size = 0;
547
548         if (size > (ULLONG_MAX - start))
549                 size = ULLONG_MAX - start;
550
551         end = start + size;
552         printk(KERN_DEBUG "e820 remove range: %016Lx - %016Lx ",
553                        (unsigned long long) start,
554                        (unsigned long long) end);
555         if (checktype)
556                 e820_print_type(old_type);
557         printk(KERN_CONT "\n");
558
559         for (i = 0; i < e820.nr_map; i++) {
560                 struct e820entry *ei = &e820.map[i];
561                 u64 final_start, final_end;
562                 u64 ei_end;
563
564                 if (checktype && ei->type != old_type)
565                         continue;
566
567                 ei_end = ei->addr + ei->size;
568                 /* totally covered? */
569                 if (ei->addr >= start && ei_end <= end) {
570                         real_removed_size += ei->size;
571                         memset(ei, 0, sizeof(struct e820entry));
572                         continue;
573                 }
574
575                 /* new range is totally covered? */
576                 if (ei->addr < start && ei_end > end) {
577                         e820_add_region(end, ei_end - end, ei->type);
578                         ei->size = start - ei->addr;
579                         real_removed_size += size;
580                         continue;
581                 }
582
583                 /* partially covered */
584                 final_start = max(start, ei->addr);
585                 final_end = min(end, ei_end);
586                 if (final_start >= final_end)
587                         continue;
588                 real_removed_size += final_end - final_start;
589
590                 /*
591                  * left range could be head or tail, so need to update
592                  * size at first.
593                  */
594                 ei->size -= final_end - final_start;
595                 if (ei->addr < final_start)
596                         continue;
597                 ei->addr = final_end;
598         }
599         return real_removed_size;
600 }
601
602 void __init update_e820(void)
603 {
604         u32 nr_map;
605
606         nr_map = e820.nr_map;
607         if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr_map))
608                 return;
609         e820.nr_map = nr_map;
610         printk(KERN_INFO "modified physical RAM map:\n");
611         _e820_print_map(&e820, "modified");
612 }
613 #ifndef CONFIG_XEN_UNPRIVILEGED_GUEST
614 static void __init update_e820_saved(void)
615 {
616         u32 nr_map;
617
618         nr_map = e820_saved.nr_map;
619         if (sanitize_e820_map(e820_saved.map, ARRAY_SIZE(e820_saved.map), &nr_map))
620                 return;
621         e820_saved.nr_map = nr_map;
622 }
623 #endif
624
625 #ifdef CONFIG_XEN
626 #define e820 machine_e820
627 #endif
628
629 #define MAX_GAP_END 0x100000000ull
630 /*
631  * Search for a gap in the e820 memory space from start_addr to end_addr.
632  */
633 __init int e820_search_gap(unsigned long *gapstart, unsigned long *gapsize,
634                 unsigned long start_addr, unsigned long long end_addr)
635 {
636         unsigned long long last;
637         int i = e820.nr_map;
638         int found = 0;
639
640         last = (end_addr && end_addr < MAX_GAP_END) ? end_addr : MAX_GAP_END;
641 #ifdef CONFIG_X86_64
642         if (start_addr >= MAX_GAP_END)
643                 last = end_addr ?: (1UL << boot_cpu_data.x86_phys_bits);
644 #endif
645
646         while (--i >= 0) {
647                 unsigned long long start = e820.map[i].addr;
648                 unsigned long long end = start + e820.map[i].size;
649
650                 if (end < start_addr)
651                         continue;
652
653                 /*
654                  * Since "last" is at most 4GB, we know we'll
655                  * fit in 32 bits if this condition is true
656                  */
657                 if (last > end) {
658                         unsigned long gap = last - end;
659
660                         if (gap >= *gapsize) {
661                                 *gapsize = gap;
662                                 *gapstart = end;
663                                 found = 1;
664                         }
665                 }
666                 if (start < last)
667                         last = start;
668         }
669         return found;
670 }
671
672 /*
673  * Search for the biggest gap in the low 32 bits of the e820
674  * memory space.  We pass this space to PCI to assign MMIO resources
675  * for hotplug or unconfigured devices in.
676  * Hopefully the BIOS let enough space left.
677  */
678 __init void e820_setup_gap(void)
679 {
680         unsigned long gapstart, gapsize;
681         int found;
682
683         gapstart = 0x10000000;
684         gapsize = 0x400000;
685         found  = e820_search_gap(&gapstart, &gapsize, 0, MAX_GAP_END);
686
687 #ifdef CONFIG_X86_64
688         if (!found) {
689                 printk(KERN_ERR
690         "PCI: Warning: Cannot find a gap in the 32bit address range\n"
691         "PCI: Unassigned devices with 32bit resource registers may break!\n");
692                 found = e820_search_gap(&gapstart, &gapsize, MAX_GAP_END, 0);
693                 WARN_ON(!found);
694         }
695 #endif
696
697         /*
698          * e820_reserve_resources_late protect stolen RAM already
699          */
700         pci_mem_start = gapstart;
701
702         printk(KERN_INFO
703                "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
704                pci_mem_start, gapstart, gapsize);
705 }
706
707 #undef e820
708
709 #ifndef CONFIG_XEN
710 /**
711  * Because of the size limitation of struct boot_params, only first
712  * 128 E820 memory entries are passed to kernel via
713  * boot_params.e820_map, others are passed via SETUP_E820_EXT node of
714  * linked list of struct setup_data, which is parsed here.
715  */
716 void __init parse_e820_ext(struct setup_data *sdata)
717 {
718         int entries;
719         struct e820entry *extmap;
720
721         entries = sdata->len / sizeof(struct e820entry);
722         extmap = (struct e820entry *)(sdata->data);
723         __append_e820_map(extmap, entries);
724         sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
725         printk(KERN_INFO "extended physical RAM map:\n");
726         _e820_print_map(&e820, "extended");
727 }
728
729 #if defined(CONFIG_X86_64) || \
730         (defined(CONFIG_X86_32) && defined(CONFIG_HIBERNATION))
731 /**
732  * Find the ranges of physical addresses that do not correspond to
733  * e820 RAM areas and mark the corresponding pages as nosave for
734  * hibernation (32 bit) or software suspend and suspend to RAM (64 bit).
735  *
736  * This function requires the e820 map to be sorted and without any
737  * overlapping entries and assumes the first e820 area to be RAM.
738  */
739 void __init e820_mark_nosave_regions(unsigned long limit_pfn)
740 {
741         int i;
742         unsigned long pfn;
743
744         pfn = PFN_DOWN(e820.map[0].addr + e820.map[0].size);
745         for (i = 1; i < e820.nr_map; i++) {
746                 struct e820entry *ei = &e820.map[i];
747
748                 if (pfn < PFN_UP(ei->addr))
749                         register_nosave_region(pfn, PFN_UP(ei->addr));
750
751                 pfn = PFN_DOWN(ei->addr + ei->size);
752                 if (ei->type != E820_RAM && ei->type != E820_RESERVED_KERN)
753                         register_nosave_region(PFN_UP(ei->addr), pfn);
754
755                 if (pfn >= limit_pfn)
756                         break;
757         }
758 }
759 #endif
760
761 #ifdef CONFIG_ACPI
762 /**
763  * Mark ACPI NVS memory region, so that we can save/restore it during
764  * hibernation and the subsequent resume.
765  */
766 static int __init e820_mark_nvs_memory(void)
767 {
768         int i;
769
770         for (i = 0; i < e820.nr_map; i++) {
771                 struct e820entry *ei = &e820.map[i];
772
773                 if (ei->type == E820_NVS)
774                         acpi_nvs_register(ei->addr, ei->size);
775         }
776
777         return 0;
778 }
779 core_initcall(e820_mark_nvs_memory);
780 #endif
781 #endif
782
783 #ifndef CONFIG_XEN_UNPRIVILEGED_GUEST
784 /*
785  * pre allocated 4k and reserved it in memblock and e820_saved
786  */
787 u64 __init early_reserve_e820(u64 size, u64 align)
788 {
789         u64 addr;
790 #ifdef CONFIG_XEN
791         unsigned int order = get_order(size);
792         int rc;
793         unsigned long max_initmap_pfn;
794
795         if (!is_initial_xendomain())
796                 return 0;
797         size = PAGE_SIZE << order;
798         if (align < PAGE_SIZE)
799                 align = PAGE_SIZE;
800 #endif
801         addr = __memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
802         if (addr) {
803                 e820_update_range_saved(addr, size, E820_RAM, E820_RESERVED);
804                 printk(KERN_INFO "update e820_saved for early_reserve_e820\n");
805                 update_e820_saved();
806         }
807 #ifdef CONFIG_XEN
808         else
809                 return 0;
810         max_initmap_pfn = ALIGN(PFN_UP(__pa(xen_start_info->pt_base))
811                                        + xen_start_info->nr_pt_frames
812                                        + 1 + (1 << (19 - PAGE_SHIFT)),
813                                 1UL << (22 - PAGE_SHIFT));
814 #ifdef CONFIG_X86_32
815         if ((addr >> PAGE_SHIFT)
816             < max(max_initmap_pfn, max_pfn_mapped))
817                 rc = xen_create_contiguous_region((unsigned long)__va(addr),
818                                                   order, 32);
819 #else
820         if ((addr >> PAGE_SHIFT) < max_pfn_mapped)
821                 rc = xen_create_contiguous_region((unsigned long)__va(addr),
822                                                   order, 32);
823         else if ((addr >> PAGE_SHIFT) < max_initmap_pfn)
824                 rc = xen_create_contiguous_region(__START_KERNEL_map + addr,
825                                                   order, 32);
826 #endif
827         else
828                 rc = early_create_contiguous_region(addr >> PAGE_SHIFT,
829                                                     order, 32);
830         if (rc)
831                 return 0;
832 #endif
833
834         return addr;
835 }
836 #endif
837
838 #ifdef CONFIG_X86_32
839 # ifdef CONFIG_X86_PAE
840 #  define MAX_ARCH_PFN          (1ULL<<(40-PAGE_SHIFT))
841 # else
842 #  define MAX_ARCH_PFN          (1ULL<<(32-PAGE_SHIFT))
843 # endif
844 #else /* CONFIG_X86_32 */
845 # define MAX_ARCH_PFN MAXMEM>>PAGE_SHIFT
846 #endif
847
848 /*
849  * Find the highest page frame number we have available
850  */
851 static unsigned long __init e820_end_pfn(unsigned long limit_pfn, unsigned type)
852 {
853         int i;
854         unsigned long last_pfn = 0;
855         unsigned long max_arch_pfn = MAX_ARCH_PFN;
856
857         for (i = 0; i < e820.nr_map; i++) {
858                 struct e820entry *ei = &e820.map[i];
859                 unsigned long start_pfn;
860                 unsigned long end_pfn;
861
862                 if (ei->type != type)
863                         continue;
864
865                 start_pfn = ei->addr >> PAGE_SHIFT;
866                 end_pfn = (ei->addr + ei->size) >> PAGE_SHIFT;
867
868                 if (start_pfn >= limit_pfn)
869                         continue;
870                 if (end_pfn > limit_pfn) {
871                         last_pfn = limit_pfn;
872                         break;
873                 }
874                 if (end_pfn > last_pfn)
875                         last_pfn = end_pfn;
876         }
877
878         if (last_pfn > max_arch_pfn)
879                 last_pfn = max_arch_pfn;
880
881         printk(KERN_INFO "last_pfn = %#lx max_arch_pfn = %#lx\n",
882                          last_pfn, max_arch_pfn);
883         return last_pfn;
884 }
885 unsigned long __init e820_end_of_ram_pfn(void)
886 {
887         return e820_end_pfn(MAX_ARCH_PFN, E820_RAM);
888 }
889
890 unsigned long __init e820_end_of_low_ram_pfn(void)
891 {
892         return e820_end_pfn(1UL<<(32 - PAGE_SHIFT), E820_RAM);
893 }
894
895 static void early_panic(char *msg)
896 {
897         early_printk(msg);
898         panic(msg);
899 }
900
901 static int userdef __initdata;
902
903 /* "mem=nopentium" disables the 4MB page tables. */
904 static int __init parse_memopt(char *p)
905 {
906         u64 mem_size, current_end;
907         unsigned int i;
908
909         if (!p)
910                 return -EINVAL;
911
912 #ifndef CONFIG_XEN
913         if (!strcmp(p, "nopentium")) {
914 #ifdef CONFIG_X86_32
915                 setup_clear_cpu_cap(X86_FEATURE_PSE);
916                 return 0;
917 #else
918                 printk(KERN_WARNING "mem=nopentium ignored! (only supported on x86_32)\n");
919                 return -EINVAL;
920 #endif
921         }
922 #endif
923
924         userdef = 1;
925         mem_size = memparse(p, &p);
926         /* don't remove all of memory when handling "mem={invalid}" param */
927         if (mem_size == 0)
928                 return -EINVAL;
929 #ifdef CONFIG_XEN
930         /*
931          * A little less than 2% of available memory are needed for page
932          * tables, p2m map, and mem_map. Hence the maximum amount of memory
933          * we can potentially balloon up to can in no case exceed about 50
934          * times of what we've been given initially. Since even with that we
935          * won't be able to boot (due to various calculations done based on
936          * the total number of pages) we further restrict this to factor 32.
937          */
938         if ((mem_size >> (PAGE_SHIFT + 5)) > xen_start_info->nr_pages) {
939                 u64 size = (u64)xen_start_info->nr_pages << 5;
940
941                 pr_warn("mem=%Luk is invalid for an initial"
942                         " allocation of %luk, using %Luk\n",
943                         (unsigned long long)mem_size >> 10,
944                         xen_start_info->nr_pages << (PAGE_SHIFT - 10),
945                         (unsigned long long)size << (PAGE_SHIFT - 10));
946                 mem_size = size << PAGE_SHIFT;
947         }
948 #endif
949         e820_remove_range(mem_size, ULLONG_MAX - mem_size, E820_RAM, 1);
950
951         i = e820.nr_map - 1;
952         current_end = e820.map[i].addr + e820.map[i].size;
953         if (current_end < mem_size) {
954                 /*
955                  * The e820 map ends before our requested size so
956                  * extend the final entry to the requested address.
957                  */
958                 if (e820.map[i].type == E820_RAM)
959                         e820.map[i].size = mem_size - e820.map[i].addr;
960                 else
961                         e820_add_region(current_end, mem_size - current_end, E820_RAM);
962         }
963
964         return 0;
965 }
966 early_param("mem", parse_memopt);
967
968 #ifndef CONFIG_XEN
969 static int __init parse_memmap_opt(char *p)
970 {
971         char *oldp;
972         u64 start_at, mem_size;
973
974         if (!p)
975                 return -EINVAL;
976
977         if (!strncmp(p, "exactmap", 8)) {
978 #ifdef CONFIG_CRASH_DUMP
979                 /*
980                  * If we are doing a crash dump, we still need to know
981                  * the real mem size before original memory map is
982                  * reset.
983                  */
984                 saved_max_pfn = e820_end_of_ram_pfn();
985 #endif
986                 e820.nr_map = 0;
987                 userdef = 1;
988                 return 0;
989         }
990
991         oldp = p;
992         mem_size = memparse(p, &p);
993         if (p == oldp)
994                 return -EINVAL;
995
996         userdef = 1;
997         if (*p == '@') {
998                 start_at = memparse(p+1, &p);
999                 e820_add_region(start_at, mem_size, E820_RAM);
1000         } else if (*p == '#') {
1001                 start_at = memparse(p+1, &p);
1002                 e820_add_region(start_at, mem_size, E820_ACPI);
1003         } else if (*p == '$') {
1004                 start_at = memparse(p+1, &p);
1005                 e820_add_region(start_at, mem_size, E820_RESERVED);
1006         } else
1007                 e820_remove_range(mem_size, ULLONG_MAX - mem_size, E820_RAM, 1);
1008
1009         return *p == '\0' ? 0 : -EINVAL;
1010 }
1011 early_param("memmap", parse_memmap_opt);
1012 #endif
1013
1014 void __init finish_e820_parsing(void)
1015 {
1016         if (userdef) {
1017                 u32 nr = e820.nr_map;
1018
1019                 if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr) < 0)
1020                         early_panic("Invalid user supplied memory map");
1021                 e820.nr_map = nr;
1022
1023                 printk(KERN_INFO "user-defined physical RAM map:\n");
1024                 _e820_print_map(&e820, "user");
1025         }
1026 }
1027
1028 static inline const char *e820_type_to_string(int e820_type)
1029 {
1030         switch (e820_type) {
1031         case E820_RESERVED_KERN:
1032         case E820_RAM:  return "System RAM";
1033         case E820_ACPI: return "ACPI Tables";
1034         case E820_NVS:  return "ACPI Non-volatile Storage";
1035         case E820_UNUSABLE:     return "Unusable memory";
1036         default:        return "reserved";
1037         }
1038 }
1039
1040 #ifdef CONFIG_XEN
1041 #define e820 machine_e820
1042 #endif
1043
1044 /*
1045  * Mark e820 reserved areas as busy for the resource manager.
1046  */
1047 static struct resource __initdata *e820_res;
1048 void __init e820_reserve_resources(void)
1049 {
1050         int i;
1051         struct resource *res;
1052         u64 end;
1053
1054         res = alloc_bootmem(sizeof(struct resource) * e820.nr_map);
1055         e820_res = res;
1056         for (i = 0; i < e820.nr_map; i++) {
1057                 end = e820.map[i].addr + e820.map[i].size - 1;
1058                 if (end != (resource_size_t)end) {
1059                         res++;
1060                         continue;
1061                 }
1062                 res->name = e820_type_to_string(e820.map[i].type);
1063                 res->start = e820.map[i].addr;
1064                 res->end = end;
1065
1066                 res->flags = IORESOURCE_MEM;
1067
1068                 /*
1069                  * don't register the region that could be conflicted with
1070                  * pci device BAR resource and insert them later in
1071                  * pcibios_resource_survey()
1072                  */
1073                 if (e820.map[i].type != E820_RESERVED || res->start < (1ULL<<20)) {
1074                         if (e820.map[i].type != E820_NVS)
1075                                 res->flags |= IORESOURCE_BUSY;
1076                         insert_resource(&iomem_resource, res);
1077                 }
1078                 res++;
1079         }
1080
1081         for (i = 0; i < e820_saved.nr_map; i++) {
1082                 struct e820entry *entry = &e820_saved.map[i];
1083                 firmware_map_add_early(entry->addr,
1084                         entry->addr + entry->size - 1,
1085                         e820_type_to_string(entry->type));
1086         }
1087 }
1088
1089 /* How much should we pad RAM ending depending on where it is? */
1090 static unsigned long ram_alignment(resource_size_t pos)
1091 {
1092         unsigned long mb = pos >> 20;
1093
1094         /* To 64kB in the first megabyte */
1095         if (!mb)
1096                 return 64*1024;
1097
1098         /* To 1MB in the first 16MB */
1099         if (mb < 16)
1100                 return 1024*1024;
1101
1102         /* To 64MB for anything above that */
1103         return 64*1024*1024;
1104 }
1105
1106 #define MAX_RESOURCE_SIZE ((resource_size_t)-1)
1107
1108 void __init e820_reserve_resources_late(void)
1109 {
1110         int i;
1111         struct resource *res;
1112
1113         res = e820_res;
1114         for (i = 0; i < e820.nr_map; i++) {
1115                 if (!res->parent && res->end)
1116                         insert_resource_expand_to_fit(&iomem_resource, res);
1117                 res++;
1118         }
1119
1120         /*
1121          * Try to bump up RAM regions to reasonable boundaries to
1122          * avoid stolen RAM:
1123          */
1124         for (i = 0; i < e820.nr_map; i++) {
1125                 struct e820entry *entry = &e820.map[i];
1126                 u64 start, end;
1127
1128                 if (entry->type != E820_RAM)
1129                         continue;
1130                 start = entry->addr + entry->size;
1131                 end = round_up(start, ram_alignment(start)) - 1;
1132                 if (end > MAX_RESOURCE_SIZE)
1133                         end = MAX_RESOURCE_SIZE;
1134                 if (start >= end)
1135                         continue;
1136                 printk(KERN_DEBUG "reserve RAM buffer: %016llx - %016llx ",
1137                                start, end);
1138                 reserve_region_with_split(&iomem_resource, start, end,
1139                                           "RAM buffer");
1140         }
1141 }
1142
1143 #undef e820
1144
1145 char *__init default_machine_specific_memory_setup(void)
1146 {
1147         int rc, nr_map;
1148         unsigned long maxmem;
1149         struct xen_memory_map memmap;
1150         static struct e820entry __initdata map[E820MAX];
1151
1152         memmap.nr_entries = E820MAX;
1153         set_xen_guest_handle(memmap.buffer, map);
1154
1155         rc = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap);
1156         if (rc == -ENOSYS) {
1157                 memmap.nr_entries = 1;
1158                 map[0].addr = 0ULL;
1159                 map[0].size = PFN_PHYS((unsigned long long)xen_start_info->nr_pages);
1160                 /* 8MB slack (to balance backend allocations). */
1161                 map[0].size += 8ULL << 20;
1162                 map[0].type = E820_RAM;
1163                 rc = 0;
1164         }
1165         BUG_ON(rc);
1166
1167         nr_map = memmap.nr_entries;
1168         sanitize_e820_map(map, ARRAY_SIZE(map), &nr_map);
1169
1170         if (append_e820_map(map, nr_map) < 0)
1171                 BUG();
1172
1173 #ifdef CONFIG_XEN
1174         /* See the comment in parse_memopt(). */
1175         for (maxmem = rc = 0; rc < e820.nr_map; ++rc)
1176                 if (e820.map[rc].type == E820_RAM)
1177                         maxmem += e820.map[rc].size >> PAGE_SHIFT;
1178         if (is_initial_xendomain()) {
1179                 domid_t domid = DOMID_SELF;
1180
1181                 rc = HYPERVISOR_memory_op(XENMEM_maximum_reservation, &domid);
1182                 if (rc > 0 && maxmem > rc)
1183                         maxmem = rc;
1184         }
1185         if ((maxmem >> 5) > xen_start_info->nr_pages) {
1186                 unsigned long long size = (u64)xen_start_info->nr_pages << 5;
1187
1188                 pr_warn("maxmem of %luM is invalid for an initial"
1189                         " allocation of %luM, using %LuM\n",
1190                         maxmem >> (20 - PAGE_SHIFT),
1191                         xen_start_info->nr_pages >> (20 - PAGE_SHIFT),
1192                         size >> (20 - PAGE_SHIFT));
1193                 size <<= PAGE_SHIFT;
1194                 e820_remove_range(size, ULLONG_MAX - size, E820_RAM, 1);
1195         }
1196
1197         if (is_initial_xendomain()) {
1198                 memmap.nr_entries = E820MAX;
1199                 set_xen_guest_handle(memmap.buffer, machine_e820.map);
1200
1201                 if (HYPERVISOR_memory_op(XENMEM_machine_memory_map, &memmap))
1202                         BUG();
1203                 machine_e820.nr_map = memmap.nr_entries;
1204         }
1205 #endif
1206
1207         return "Xen";
1208 }
1209
1210 void __init setup_memory_map(void)
1211 {
1212         char *who;
1213
1214         who = x86_init.resources.memory_setup();
1215 #ifndef CONFIG_XEN_UNPRIVILEGED_GUEST
1216 #ifdef CONFIG_XEN
1217         if (is_initial_xendomain()) {
1218                 printk(KERN_INFO "Xen-provided machine memory map:\n");
1219                 _e820_print_map(&machine_e820, "BIOS");
1220         } else
1221 #endif
1222                 memcpy(&e820_saved, &e820, sizeof(struct e820map));
1223 #endif
1224         printk(KERN_INFO "Xen-provided physical RAM map:\n");
1225         _e820_print_map(&e820, who);
1226 }
1227
1228 void __init memblock_x86_fill(void)
1229 {
1230         int i;
1231         u64 end;
1232
1233         /*
1234          * EFI may have more than 128 entries
1235          * We are safe to enable resizing, beause memblock_x86_fill()
1236          * is rather later for x86
1237          */
1238         memblock_allow_resize();
1239
1240         for (i = 0; i < e820.nr_map; i++) {
1241                 struct e820entry *ei = &e820.map[i];
1242
1243                 end = ei->addr + ei->size;
1244                 if (end != (resource_size_t)end)
1245                         continue;
1246
1247                 if (ei->type != E820_RAM && ei->type != E820_RESERVED_KERN)
1248                         continue;
1249
1250                 memblock_add(ei->addr, ei->size);
1251         }
1252
1253 #ifdef CONFIG_XEN
1254         if (max_pfn > xen_start_info->nr_pages)
1255                 memblock_reserve(PFN_PHYS(xen_start_info->nr_pages),
1256                                  PFN_PHYS(max_pfn - xen_start_info->nr_pages));
1257 #endif
1258
1259         memblock_dump_all();
1260 }
1261
1262 void __init memblock_find_dma_reserve(void)
1263 {
1264 #if defined(CONFIG_X86_64) && !defined(CONFIG_XEN)
1265         u64 nr_pages = 0, nr_free_pages = 0;
1266         unsigned long start_pfn, end_pfn;
1267         phys_addr_t start, end;
1268         int i;
1269         u64 u;
1270
1271         /*
1272          * need to find out used area below MAX_DMA_PFN
1273          * need to use memblock to get free size in [0, MAX_DMA_PFN]
1274          * at first, and assume boot_mem will not take below MAX_DMA_PFN
1275          */
1276         for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, NULL) {
1277                 start_pfn = min_t(unsigned long, start_pfn, MAX_DMA_PFN);
1278                 end_pfn = min_t(unsigned long, end_pfn, MAX_DMA_PFN);
1279                 nr_pages += end_pfn - start_pfn;
1280         }
1281
1282         for_each_free_mem_range(u, MAX_NUMNODES, &start, &end, NULL) {
1283                 start_pfn = min_t(unsigned long, PFN_UP(start), MAX_DMA_PFN);
1284                 end_pfn = min_t(unsigned long, PFN_DOWN(end), MAX_DMA_PFN);
1285                 if (start_pfn < end_pfn)
1286                         nr_free_pages += end_pfn - start_pfn;
1287         }
1288
1289         set_dma_reserve(nr_pages - nr_free_pages);
1290 #endif
1291 }