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