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