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