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