x86, boot: add free_early to early reservation machanism
[linux-2.6] / arch / x86 / kernel / e820_64.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/bootmem.h>
15 #include <linux/ioport.h>
16 #include <linux/string.h>
17 #include <linux/kexec.h>
18 #include <linux/module.h>
19 #include <linux/mm.h>
20 #include <linux/suspend.h>
21 #include <linux/pfn.h>
22
23 #include <asm/pgtable.h>
24 #include <asm/page.h>
25 #include <asm/e820.h>
26 #include <asm/proto.h>
27 #include <asm/setup.h>
28 #include <asm/sections.h>
29 #include <asm/kdebug.h>
30 #include <asm/trampoline.h>
31
32 struct e820map e820;
33
34 /*
35  * PFN of last memory page.
36  */
37 unsigned long end_pfn;
38
39 /*
40  * end_pfn only includes RAM, while max_pfn_mapped includes all e820 entries.
41  * The direct mapping extends to max_pfn_mapped, so that we can directly access
42  * apertures, ACPI and other tables without having to play with fixmaps.
43  */
44 unsigned long max_pfn_mapped;
45
46 /*
47  * Last pfn which the user wants to use.
48  */
49 static unsigned long __initdata end_user_pfn = MAXMEM>>PAGE_SHIFT;
50
51 /*
52  * Early reserved memory areas.
53  */
54 #define MAX_EARLY_RES 20
55
56 struct early_res {
57         unsigned long start, end;
58         char name[16];
59 };
60 static struct early_res early_res[MAX_EARLY_RES] __initdata = {
61         { 0, PAGE_SIZE, "BIOS data page" },                     /* BIOS data page */
62 #ifdef CONFIG_X86_TRAMPOLINE
63         { TRAMPOLINE_BASE, TRAMPOLINE_BASE + 2 * PAGE_SIZE, "TRAMPOLINE" },
64 #endif
65         {}
66 };
67
68 void __init reserve_early(unsigned long start, unsigned long end, char *name)
69 {
70         int i;
71         struct early_res *r;
72         for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
73                 r = &early_res[i];
74                 if (end > r->start && start < r->end)
75                         panic("Overlapping early reservations %lx-%lx %s to %lx-%lx %s\n",
76                               start, end - 1, name?name:"", r->start, r->end - 1, r->name);
77         }
78         if (i >= MAX_EARLY_RES)
79                 panic("Too many early reservations");
80         r = &early_res[i];
81         r->start = start;
82         r->end = end;
83         if (name)
84                 strncpy(r->name, name, sizeof(r->name) - 1);
85 }
86
87 void __init free_early(unsigned long start, unsigned long end)
88 {
89         struct early_res *r;
90         int i, j;
91
92         for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
93                 r = &early_res[i];
94                 if (start == r->start && end == r->end)
95                         break;
96         }
97         if (i >= MAX_EARLY_RES || !early_res[i].end)
98                 panic("free_early on not reserved area: %lx-%lx!", start, end);
99
100         for (j = i + 1; j < MAX_EARLY_RES && early_res[j].end; j++)
101                 ;
102
103         memcpy(&early_res[i], &early_res[i + 1],
104                (j - 1 - i) * sizeof(struct early_res));
105
106         early_res[j - 1].end = 0;
107 }
108
109 void __init early_res_to_bootmem(void)
110 {
111         int i;
112         for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
113                 struct early_res *r = &early_res[i];
114                 printk(KERN_INFO "early res: %d [%lx-%lx] %s\n", i,
115                         r->start, r->end - 1, r->name);
116                 reserve_bootmem_generic(r->start, r->end - r->start);
117         }
118 }
119
120 /* Check for already reserved areas */
121 static inline int __init
122 bad_addr(unsigned long *addrp, unsigned long size, unsigned long align)
123 {
124         int i;
125         unsigned long addr = *addrp, last;
126         int changed = 0;
127 again:
128         last = addr + size;
129         for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
130                 struct early_res *r = &early_res[i];
131                 if (last >= r->start && addr < r->end) {
132                         *addrp = addr = round_up(r->end, align);
133                         changed = 1;
134                         goto again;
135                 }
136         }
137         return changed;
138 }
139
140 /* Check for already reserved areas */
141 static inline int __init
142 bad_addr_size(unsigned long *addrp, unsigned long *sizep, unsigned long align)
143 {
144         int i;
145         unsigned long addr = *addrp, last;
146         unsigned long size = *sizep;
147         int changed = 0;
148 again:
149         last = addr + size;
150         for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
151                 struct early_res *r = &early_res[i];
152                 if (last > r->start && addr < r->start) {
153                         size = r->start - addr;
154                         changed = 1;
155                         goto again;
156                 }
157                 if (last > r->end && addr < r->end) {
158                         addr = round_up(r->end, align);
159                         size = last - addr;
160                         changed = 1;
161                         goto again;
162                 }
163                 if (last <= r->end && addr >= r->start) {
164                         (*sizep)++;
165                         return 0;
166                 }
167         }
168         if (changed) {
169                 *addrp = addr;
170                 *sizep = size;
171         }
172         return changed;
173 }
174 /*
175  * This function checks if any part of the range <start,end> is mapped
176  * with type.
177  */
178 int
179 e820_any_mapped(unsigned long start, unsigned long end, unsigned type)
180 {
181         int i;
182
183         for (i = 0; i < e820.nr_map; i++) {
184                 struct e820entry *ei = &e820.map[i];
185
186                 if (type && ei->type != type)
187                         continue;
188                 if (ei->addr >= end || ei->addr + ei->size <= start)
189                         continue;
190                 return 1;
191         }
192         return 0;
193 }
194 EXPORT_SYMBOL_GPL(e820_any_mapped);
195
196 /*
197  * This function checks if the entire range <start,end> is mapped with type.
198  *
199  * Note: this function only works correct if the e820 table is sorted and
200  * not-overlapping, which is the case
201  */
202 int __init e820_all_mapped(unsigned long start, unsigned long end,
203                            unsigned type)
204 {
205         int i;
206
207         for (i = 0; i < e820.nr_map; i++) {
208                 struct e820entry *ei = &e820.map[i];
209
210                 if (type && ei->type != type)
211                         continue;
212                 /* is the region (part) in overlap with the current region ?*/
213                 if (ei->addr >= end || ei->addr + ei->size <= start)
214                         continue;
215
216                 /* if the region is at the beginning of <start,end> we move
217                  * start to the end of the region since it's ok until there
218                  */
219                 if (ei->addr <= start)
220                         start = ei->addr + ei->size;
221                 /*
222                  * if start is now at or beyond end, we're done, full
223                  * coverage
224                  */
225                 if (start >= end)
226                         return 1;
227         }
228         return 0;
229 }
230
231 /*
232  * Find a free area with specified alignment in a specific range.
233  */
234 unsigned long __init find_e820_area(unsigned long start, unsigned long end,
235                                     unsigned long size, unsigned long align)
236 {
237         int i;
238
239         for (i = 0; i < e820.nr_map; i++) {
240                 struct e820entry *ei = &e820.map[i];
241                 unsigned long addr, last;
242                 unsigned long ei_last;
243
244                 if (ei->type != E820_RAM)
245                         continue;
246                 addr = round_up(ei->addr, align);
247                 ei_last = ei->addr + ei->size;
248                 if (addr < start)
249                         addr = round_up(start, align);
250                 if (addr >= ei_last)
251                         continue;
252                 while (bad_addr(&addr, size, align) && addr+size <= ei_last)
253                         ;
254                 last = addr + size;
255                 if (last > ei_last)
256                         continue;
257                 if (last > end)
258                         continue;
259                 return addr;
260         }
261         return -1UL;
262 }
263
264 /*
265  * Find next free range after *start
266  */
267 unsigned long __init find_e820_area_size(unsigned long start,
268                                          unsigned long *sizep,
269                                          unsigned long align)
270 {
271         int i;
272
273         for (i = 0; i < e820.nr_map; i++) {
274                 struct e820entry *ei = &e820.map[i];
275                 unsigned long addr, last;
276                 unsigned long ei_last;
277
278                 if (ei->type != E820_RAM)
279                         continue;
280                 addr = round_up(ei->addr, align);
281                 ei_last = ei->addr + ei->size;
282                 if (addr < start)
283                         addr = round_up(start, align);
284                 if (addr >= ei_last)
285                         continue;
286                 *sizep = ei_last - addr;
287                 while (bad_addr_size(&addr, sizep, align) &&
288                         addr + *sizep <= ei_last)
289                         ;
290                 last = addr + *sizep;
291                 if (last > ei_last)
292                         continue;
293                 return addr;
294         }
295         return -1UL;
296
297 }
298 /*
299  * Find the highest page frame number we have available
300  */
301 unsigned long __init e820_end_of_ram(void)
302 {
303         unsigned long end_pfn;
304
305         end_pfn = find_max_pfn_with_active_regions();
306
307         if (end_pfn > max_pfn_mapped)
308                 max_pfn_mapped = end_pfn;
309         if (max_pfn_mapped > MAXMEM>>PAGE_SHIFT)
310                 max_pfn_mapped = MAXMEM>>PAGE_SHIFT;
311         if (end_pfn > end_user_pfn)
312                 end_pfn = end_user_pfn;
313         if (end_pfn > max_pfn_mapped)
314                 end_pfn = max_pfn_mapped;
315
316         printk(KERN_INFO "max_pfn_mapped = %lu\n", max_pfn_mapped);
317         return end_pfn;
318 }
319
320 /*
321  * Mark e820 reserved areas as busy for the resource manager.
322  */
323 void __init e820_reserve_resources(void)
324 {
325         int i;
326         struct resource *res;
327
328         res = alloc_bootmem_low(sizeof(struct resource) * e820.nr_map);
329         for (i = 0; i < e820.nr_map; i++) {
330                 switch (e820.map[i].type) {
331                 case E820_RAM:  res->name = "System RAM"; break;
332                 case E820_ACPI: res->name = "ACPI Tables"; break;
333                 case E820_NVS:  res->name = "ACPI Non-volatile Storage"; break;
334                 default:        res->name = "reserved";
335                 }
336                 res->start = e820.map[i].addr;
337                 res->end = res->start + e820.map[i].size - 1;
338                 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
339                 insert_resource(&iomem_resource, res);
340                 res++;
341         }
342 }
343
344 /*
345  * Find the ranges of physical addresses that do not correspond to
346  * e820 RAM areas and mark the corresponding pages as nosave for software
347  * suspend and suspend to RAM.
348  *
349  * This function requires the e820 map to be sorted and without any
350  * overlapping entries and assumes the first e820 area to be RAM.
351  */
352 void __init e820_mark_nosave_regions(void)
353 {
354         int i;
355         unsigned long paddr;
356
357         paddr = round_down(e820.map[0].addr + e820.map[0].size, PAGE_SIZE);
358         for (i = 1; i < e820.nr_map; i++) {
359                 struct e820entry *ei = &e820.map[i];
360
361                 if (paddr < ei->addr)
362                         register_nosave_region(PFN_DOWN(paddr),
363                                                 PFN_UP(ei->addr));
364
365                 paddr = round_down(ei->addr + ei->size, PAGE_SIZE);
366                 if (ei->type != E820_RAM)
367                         register_nosave_region(PFN_UP(ei->addr),
368                                                 PFN_DOWN(paddr));
369
370                 if (paddr >= (end_pfn << PAGE_SHIFT))
371                         break;
372         }
373 }
374
375 /*
376  * Finds an active region in the address range from start_pfn to end_pfn and
377  * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
378  */
379 static int __init e820_find_active_region(const struct e820entry *ei,
380                                           unsigned long start_pfn,
381                                           unsigned long end_pfn,
382                                           unsigned long *ei_startpfn,
383                                           unsigned long *ei_endpfn)
384 {
385         *ei_startpfn = round_up(ei->addr, PAGE_SIZE) >> PAGE_SHIFT;
386         *ei_endpfn = round_down(ei->addr + ei->size, PAGE_SIZE) >> PAGE_SHIFT;
387
388         /* Skip map entries smaller than a page */
389         if (*ei_startpfn >= *ei_endpfn)
390                 return 0;
391
392         /* Check if max_pfn_mapped should be updated */
393         if (ei->type != E820_RAM && *ei_endpfn > max_pfn_mapped)
394                 max_pfn_mapped = *ei_endpfn;
395
396         /* Skip if map is outside the node */
397         if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
398                                     *ei_startpfn >= end_pfn)
399                 return 0;
400
401         /* Check for overlaps */
402         if (*ei_startpfn < start_pfn)
403                 *ei_startpfn = start_pfn;
404         if (*ei_endpfn > end_pfn)
405                 *ei_endpfn = end_pfn;
406
407         /* Obey end_user_pfn to save on memmap */
408         if (*ei_startpfn >= end_user_pfn)
409                 return 0;
410         if (*ei_endpfn > end_user_pfn)
411                 *ei_endpfn = end_user_pfn;
412
413         return 1;
414 }
415
416 /* Walk the e820 map and register active regions within a node */
417 void __init
418 e820_register_active_regions(int nid, unsigned long start_pfn,
419                                                         unsigned long end_pfn)
420 {
421         unsigned long ei_startpfn;
422         unsigned long ei_endpfn;
423         int i;
424
425         for (i = 0; i < e820.nr_map; i++)
426                 if (e820_find_active_region(&e820.map[i],
427                                             start_pfn, end_pfn,
428                                             &ei_startpfn, &ei_endpfn))
429                         add_active_range(nid, ei_startpfn, ei_endpfn);
430 }
431
432 /*
433  * Add a memory region to the kernel e820 map.
434  */
435 void __init add_memory_region(unsigned long start, unsigned long size, int type)
436 {
437         int x = e820.nr_map;
438
439         if (x == E820MAX) {
440                 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
441                 return;
442         }
443
444         e820.map[x].addr = start;
445         e820.map[x].size = size;
446         e820.map[x].type = type;
447         e820.nr_map++;
448 }
449
450 /*
451  * Find the hole size (in bytes) in the memory range.
452  * @start: starting address of the memory range to scan
453  * @end: ending address of the memory range to scan
454  */
455 unsigned long __init e820_hole_size(unsigned long start, unsigned long end)
456 {
457         unsigned long start_pfn = start >> PAGE_SHIFT;
458         unsigned long end_pfn = end >> PAGE_SHIFT;
459         unsigned long ei_startpfn, ei_endpfn, ram = 0;
460         int i;
461
462         for (i = 0; i < e820.nr_map; i++) {
463                 if (e820_find_active_region(&e820.map[i],
464                                             start_pfn, end_pfn,
465                                             &ei_startpfn, &ei_endpfn))
466                         ram += ei_endpfn - ei_startpfn;
467         }
468         return end - start - (ram << PAGE_SHIFT);
469 }
470
471 static void __init e820_print_map(char *who)
472 {
473         int i;
474
475         for (i = 0; i < e820.nr_map; i++) {
476                 printk(KERN_INFO " %s: %016Lx - %016Lx ", who,
477                        (unsigned long long) e820.map[i].addr,
478                        (unsigned long long)
479                        (e820.map[i].addr + e820.map[i].size));
480                 switch (e820.map[i].type) {
481                 case E820_RAM:
482                         printk(KERN_CONT "(usable)\n");
483                         break;
484                 case E820_RESERVED:
485                         printk(KERN_CONT "(reserved)\n");
486                         break;
487                 case E820_ACPI:
488                         printk(KERN_CONT "(ACPI data)\n");
489                         break;
490                 case E820_NVS:
491                         printk(KERN_CONT "(ACPI NVS)\n");
492                         break;
493                 default:
494                         printk(KERN_CONT "type %u\n", e820.map[i].type);
495                         break;
496                 }
497         }
498 }
499
500 /*
501  * Sanitize the BIOS e820 map.
502  *
503  * Some e820 responses include overlapping entries. The following
504  * replaces the original e820 map with a new one, removing overlaps.
505  *
506  */
507 static int __init sanitize_e820_map(struct e820entry *biosmap, char *pnr_map)
508 {
509         struct change_member {
510                 struct e820entry *pbios; /* pointer to original bios entry */
511                 unsigned long long addr; /* address for this change point */
512         };
513         static struct change_member change_point_list[2*E820MAX] __initdata;
514         static struct change_member *change_point[2*E820MAX] __initdata;
515         static struct e820entry *overlap_list[E820MAX] __initdata;
516         static struct e820entry new_bios[E820MAX] __initdata;
517         struct change_member *change_tmp;
518         unsigned long current_type, last_type;
519         unsigned long long last_addr;
520         int chgidx, still_changing;
521         int overlap_entries;
522         int new_bios_entry;
523         int old_nr, new_nr, chg_nr;
524         int i;
525
526         /*
527                 Visually we're performing the following
528                 (1,2,3,4 = memory types)...
529
530                 Sample memory map (w/overlaps):
531                    ____22__________________
532                    ______________________4_
533                    ____1111________________
534                    _44_____________________
535                    11111111________________
536                    ____________________33__
537                    ___________44___________
538                    __________33333_________
539                    ______________22________
540                    ___________________2222_
541                    _________111111111______
542                    _____________________11_
543                    _________________4______
544
545                 Sanitized equivalent (no overlap):
546                    1_______________________
547                    _44_____________________
548                    ___1____________________
549                    ____22__________________
550                    ______11________________
551                    _________1______________
552                    __________3_____________
553                    ___________44___________
554                    _____________33_________
555                    _______________2________
556                    ________________1_______
557                    _________________4______
558                    ___________________2____
559                    ____________________33__
560                    ______________________4_
561         */
562
563         /* if there's only one memory region, don't bother */
564         if (*pnr_map < 2)
565                 return -1;
566
567         old_nr = *pnr_map;
568
569         /* bail out if we find any unreasonable addresses in bios map */
570         for (i = 0; i < old_nr; i++)
571                 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
572                         return -1;
573
574         /* create pointers for initial change-point information (for sorting) */
575         for (i = 0; i < 2 * old_nr; i++)
576                 change_point[i] = &change_point_list[i];
577
578         /* record all known change-points (starting and ending addresses),
579            omitting those that are for empty memory regions */
580         chgidx = 0;
581         for (i = 0; i < old_nr; i++)    {
582                 if (biosmap[i].size != 0) {
583                         change_point[chgidx]->addr = biosmap[i].addr;
584                         change_point[chgidx++]->pbios = &biosmap[i];
585                         change_point[chgidx]->addr = biosmap[i].addr +
586                                 biosmap[i].size;
587                         change_point[chgidx++]->pbios = &biosmap[i];
588                 }
589         }
590         chg_nr = chgidx;
591
592         /* sort change-point list by memory addresses (low -> high) */
593         still_changing = 1;
594         while (still_changing)  {
595                 still_changing = 0;
596                 for (i = 1; i < chg_nr; i++)  {
597                         unsigned long long curaddr, lastaddr;
598                         unsigned long long curpbaddr, lastpbaddr;
599
600                         curaddr = change_point[i]->addr;
601                         lastaddr = change_point[i - 1]->addr;
602                         curpbaddr = change_point[i]->pbios->addr;
603                         lastpbaddr = change_point[i - 1]->pbios->addr;
604
605                         /*
606                          * swap entries, when:
607                          *
608                          * curaddr > lastaddr or
609                          * curaddr == lastaddr and curaddr == curpbaddr and
610                          * lastaddr != lastpbaddr
611                          */
612                         if (curaddr < lastaddr ||
613                             (curaddr == lastaddr && curaddr == curpbaddr &&
614                              lastaddr != lastpbaddr)) {
615                                 change_tmp = change_point[i];
616                                 change_point[i] = change_point[i-1];
617                                 change_point[i-1] = change_tmp;
618                                 still_changing = 1;
619                         }
620                 }
621         }
622
623         /* create a new bios memory map, removing overlaps */
624         overlap_entries = 0;     /* number of entries in the overlap table */
625         new_bios_entry = 0;      /* index for creating new bios map entries */
626         last_type = 0;           /* start with undefined memory type */
627         last_addr = 0;           /* start with 0 as last starting address */
628
629         /* loop through change-points, determining affect on the new bios map */
630         for (chgidx = 0; chgidx < chg_nr; chgidx++) {
631                 /* keep track of all overlapping bios entries */
632                 if (change_point[chgidx]->addr ==
633                     change_point[chgidx]->pbios->addr) {
634                         /*
635                          * add map entry to overlap list (> 1 entry
636                          * implies an overlap)
637                          */
638                         overlap_list[overlap_entries++] =
639                                 change_point[chgidx]->pbios;
640                 } else {
641                         /*
642                          * remove entry from list (order independent,
643                          * so swap with last)
644                          */
645                         for (i = 0; i < overlap_entries; i++) {
646                                 if (overlap_list[i] ==
647                                     change_point[chgidx]->pbios)
648                                         overlap_list[i] =
649                                                 overlap_list[overlap_entries-1];
650                         }
651                         overlap_entries--;
652                 }
653                 /*
654                  * if there are overlapping entries, decide which
655                  * "type" to use (larger value takes precedence --
656                  * 1=usable, 2,3,4,4+=unusable)
657                  */
658                 current_type = 0;
659                 for (i = 0; i < overlap_entries; i++)
660                         if (overlap_list[i]->type > current_type)
661                                 current_type = overlap_list[i]->type;
662                 /*
663                  * continue building up new bios map based on this
664                  * information
665                  */
666                 if (current_type != last_type)  {
667                         if (last_type != 0)      {
668                                 new_bios[new_bios_entry].size =
669                                         change_point[chgidx]->addr - last_addr;
670                                 /*
671                                  * move forward only if the new size
672                                  * was non-zero
673                                  */
674                                 if (new_bios[new_bios_entry].size != 0)
675                                         /*
676                                          * no more space left for new
677                                          * bios entries ?
678                                          */
679                                         if (++new_bios_entry >= E820MAX)
680                                                 break;
681                         }
682                         if (current_type != 0)  {
683                                 new_bios[new_bios_entry].addr =
684                                         change_point[chgidx]->addr;
685                                 new_bios[new_bios_entry].type = current_type;
686                                 last_addr = change_point[chgidx]->addr;
687                         }
688                         last_type = current_type;
689                 }
690         }
691         /* retain count for new bios entries */
692         new_nr = new_bios_entry;
693
694         /* copy new bios mapping into original location */
695         memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
696         *pnr_map = new_nr;
697
698         return 0;
699 }
700
701 /*
702  * Copy the BIOS e820 map into a safe place.
703  *
704  * Sanity-check it while we're at it..
705  *
706  * If we're lucky and live on a modern system, the setup code
707  * will have given us a memory map that we can use to properly
708  * set up memory.  If we aren't, we'll fake a memory map.
709  */
710 static int __init copy_e820_map(struct e820entry *biosmap, int nr_map)
711 {
712         /* Only one memory region (or negative)? Ignore it */
713         if (nr_map < 2)
714                 return -1;
715
716         do {
717                 u64 start = biosmap->addr;
718                 u64 size = biosmap->size;
719                 u64 end = start + size;
720                 u32 type = biosmap->type;
721
722                 /* Overflow in 64 bits? Ignore the memory map. */
723                 if (start > end)
724                         return -1;
725
726                 add_memory_region(start, size, type);
727         } while (biosmap++, --nr_map);
728         return 0;
729 }
730
731 static void early_panic(char *msg)
732 {
733         early_printk(msg);
734         panic(msg);
735 }
736
737 /* We're not void only for x86 32-bit compat */
738 char * __init machine_specific_memory_setup(void)
739 {
740         char *who = "BIOS-e820";
741         /*
742          * Try to copy the BIOS-supplied E820-map.
743          *
744          * Otherwise fake a memory map; one section from 0k->640k,
745          * the next section from 1mb->appropriate_mem_k
746          */
747         sanitize_e820_map(boot_params.e820_map, &boot_params.e820_entries);
748         if (copy_e820_map(boot_params.e820_map, boot_params.e820_entries) < 0)
749                 early_panic("Cannot find a valid memory map");
750         printk(KERN_INFO "BIOS-provided physical RAM map:\n");
751         e820_print_map(who);
752
753         /* In case someone cares... */
754         return who;
755 }
756
757 static int __init parse_memopt(char *p)
758 {
759         if (!p)
760                 return -EINVAL;
761         end_user_pfn = memparse(p, &p);
762         end_user_pfn >>= PAGE_SHIFT;
763         return 0;
764 }
765 early_param("mem", parse_memopt);
766
767 static int userdef __initdata;
768
769 static int __init parse_memmap_opt(char *p)
770 {
771         char *oldp;
772         unsigned long long start_at, mem_size;
773
774         if (!strcmp(p, "exactmap")) {
775 #ifdef CONFIG_CRASH_DUMP
776                 /*
777                  * If we are doing a crash dump, we still need to know
778                  * the real mem size before original memory map is
779                  * reset.
780                  */
781                 e820_register_active_regions(0, 0, -1UL);
782                 saved_max_pfn = e820_end_of_ram();
783                 remove_all_active_ranges();
784 #endif
785                 max_pfn_mapped = 0;
786                 e820.nr_map = 0;
787                 userdef = 1;
788                 return 0;
789         }
790
791         oldp = p;
792         mem_size = memparse(p, &p);
793         if (p == oldp)
794                 return -EINVAL;
795
796         userdef = 1;
797         if (*p == '@') {
798                 start_at = memparse(p+1, &p);
799                 add_memory_region(start_at, mem_size, E820_RAM);
800         } else if (*p == '#') {
801                 start_at = memparse(p+1, &p);
802                 add_memory_region(start_at, mem_size, E820_ACPI);
803         } else if (*p == '$') {
804                 start_at = memparse(p+1, &p);
805                 add_memory_region(start_at, mem_size, E820_RESERVED);
806         } else {
807                 end_user_pfn = (mem_size >> PAGE_SHIFT);
808         }
809         return *p == '\0' ? 0 : -EINVAL;
810 }
811 early_param("memmap", parse_memmap_opt);
812
813 void __init finish_e820_parsing(void)
814 {
815         if (userdef) {
816                 char nr = e820.nr_map;
817
818                 if (sanitize_e820_map(e820.map, &nr) < 0)
819                         early_panic("Invalid user supplied memory map");
820                 e820.nr_map = nr;
821
822                 printk(KERN_INFO "user-defined physical RAM map:\n");
823                 e820_print_map("user");
824         }
825 }
826
827 void __init update_memory_range(u64 start, u64 size, unsigned old_type,
828                                 unsigned new_type)
829 {
830         int i;
831
832         BUG_ON(old_type == new_type);
833
834         for (i = 0; i < e820.nr_map; i++) {
835                 struct e820entry *ei = &e820.map[i];
836                 u64 final_start, final_end;
837                 if (ei->type != old_type)
838                         continue;
839                 /* totally covered? */
840                 if (ei->addr >= start && ei->size <= size) {
841                         ei->type = new_type;
842                         continue;
843                 }
844                 /* partially covered */
845                 final_start = max(start, ei->addr);
846                 final_end = min(start + size, ei->addr + ei->size);
847                 if (final_start >= final_end)
848                         continue;
849                 add_memory_region(final_start, final_end - final_start,
850                                          new_type);
851         }
852 }
853
854 void __init update_e820(void)
855 {
856         u8 nr_map;
857
858         nr_map = e820.nr_map;
859         if (sanitize_e820_map(e820.map, &nr_map))
860                 return;
861         e820.nr_map = nr_map;
862         printk(KERN_INFO "modified physical RAM map:\n");
863         e820_print_map("modified");
864 }
865
866 unsigned long pci_mem_start = 0xaeedbabe;
867 EXPORT_SYMBOL(pci_mem_start);
868
869 /*
870  * Search for the biggest gap in the low 32 bits of the e820
871  * memory space.  We pass this space to PCI to assign MMIO resources
872  * for hotplug or unconfigured devices in.
873  * Hopefully the BIOS let enough space left.
874  */
875 __init void e820_setup_gap(void)
876 {
877         unsigned long gapstart, gapsize, round;
878         unsigned long last;
879         int i;
880         int found = 0;
881
882         last = 0x100000000ull;
883         gapstart = 0x10000000;
884         gapsize = 0x400000;
885         i = e820.nr_map;
886         while (--i >= 0) {
887                 unsigned long long start = e820.map[i].addr;
888                 unsigned long long end = start + e820.map[i].size;
889
890                 /*
891                  * Since "last" is at most 4GB, we know we'll
892                  * fit in 32 bits if this condition is true
893                  */
894                 if (last > end) {
895                         unsigned long gap = last - end;
896
897                         if (gap > gapsize) {
898                                 gapsize = gap;
899                                 gapstart = end;
900                                 found = 1;
901                         }
902                 }
903                 if (start < last)
904                         last = start;
905         }
906
907         if (!found) {
908                 gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
909                 printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit "
910                        "address range\n"
911                        KERN_ERR "PCI: Unassigned devices with 32bit resource "
912                        "registers may break!\n");
913         }
914
915         /*
916          * See how much we want to round up: start off with
917          * rounding to the next 1MB area.
918          */
919         round = 0x100000;
920         while ((gapsize >> 4) > round)
921                 round += round;
922         /* Fun with two's complement */
923         pci_mem_start = (gapstart + round) & -round;
924
925         printk(KERN_INFO
926                "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
927                pci_mem_start, gapstart, gapsize);
928 }
929
930 int __init arch_get_ram_range(int slot, u64 *addr, u64 *size)
931 {
932         int i;
933
934         if (slot < 0 || slot >= e820.nr_map)
935                 return -1;
936         for (i = slot; i < e820.nr_map; i++) {
937                 if (e820.map[i].type != E820_RAM)
938                         continue;
939                 break;
940         }
941         if (i == e820.nr_map || e820.map[i].addr > (max_pfn << PAGE_SHIFT))
942                 return -1;
943         *addr = e820.map[i].addr;
944         *size = min_t(u64, e820.map[i].size + e820.map[i].addr,
945                 max_pfn << PAGE_SHIFT) - *addr;
946         return i + 1;
947 }