- 2.6.17 port work build breaks, but the patch set is relativly stable
[linux-flexiantxendom0-3.2.10.git] / arch / x86_64 / kernel / e820-xen.c
1 /* 
2  * Handle the memory map.
3  * The functions here do the job until bootmem takes over.
4  * $Id: e820.c,v 1.4 2002/09/19 19:25:32 ak Exp $
5  *
6  *  Getting sanitize_e820_map() in sync with i386 version by applying change:
7  *  -  Provisions for empty E820 memory regions (reported by certain BIOSes).
8  *     Alex Achenbach <xela@slit.de>, December 2002.
9  *  Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
10  *
11  */
12 #include <linux/config.h>
13 #include <linux/kernel.h>
14 #include <linux/types.h>
15 #include <linux/init.h>
16 #include <linux/bootmem.h>
17 #include <linux/ioport.h>
18 #include <linux/string.h>
19 #include <linux/kexec.h>
20 #include <linux/module.h>
21
22 #include <asm/page.h>
23 #include <asm/e820.h>
24 #include <asm/proto.h>
25 #include <asm/bootsetup.h>
26 #include <asm/sections.h>
27 #include <xen/interface/memory.h>
28
29 unsigned long pci_mem_start = 0xaeedbabe;
30
31 /* 
32  * PFN of last memory page.
33  */
34 unsigned long end_pfn; 
35 EXPORT_SYMBOL(end_pfn);
36 unsigned long end_user_pfn = MAXMEM>>PAGE_SHIFT;  
37 unsigned long end_pfn_map; 
38
39 /* 
40  * Add a memory region to the kernel e820 map.
41  */ 
42 void __init add_memory_region(unsigned long start, unsigned long size, int type)
43 {
44         int x = e820.nr_map;
45
46         if (x == E820MAX) {
47                 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
48                 return;
49         }
50
51         e820.map[x].addr = start;
52         e820.map[x].size = size;
53         e820.map[x].type = type;
54         e820.nr_map++;
55 }
56
57 #ifndef CONFIG_XEN
58
59 /* 
60  * end_pfn only includes RAM, while end_pfn_map includes all e820 entries.
61  * The direct mapping extends to end_pfn_map, so that we can directly access
62  * apertures, ACPI and other tables without having to play with fixmaps.
63  */ 
64
65 /* 
66  * Last pfn which the user wants to use.
67  */
68
69 extern struct resource code_resource, data_resource;
70
71 /* Check for some hardcoded bad areas that early boot is not allowed to touch */ 
72 static inline int bad_addr(unsigned long *addrp, unsigned long size)
73
74         unsigned long addr = *addrp, last = addr + size; 
75
76         /* various gunk below that needed for SMP startup */
77         if (addr < 0x8000) { 
78                 *addrp = 0x8000;
79                 return 1; 
80         }
81
82         /* direct mapping tables of the kernel */
83         if (last >= table_start<<PAGE_SHIFT && addr < table_end<<PAGE_SHIFT) { 
84                 *addrp = table_end << PAGE_SHIFT; 
85                 return 1;
86         } 
87
88         /* initrd */ 
89 #ifdef CONFIG_BLK_DEV_INITRD
90         if (LOADER_TYPE && INITRD_START && last >= INITRD_START && 
91             addr < INITRD_START+INITRD_SIZE) { 
92                 *addrp = INITRD_START + INITRD_SIZE; 
93                 return 1;
94         } 
95 #endif
96         /* kernel code + 640k memory hole (later should not be needed, but 
97            be paranoid for now) */
98         if (last >= 640*1024 && addr < __pa_symbol(&_end)) { 
99                 *addrp = __pa_symbol(&_end);
100                 return 1;
101         }
102         /* XXX ramdisk image here? */ 
103         return 0;
104
105
106 int __init e820_mapped(unsigned long start, unsigned long end, unsigned type) 
107
108         int i;
109         for (i = 0; i < e820.nr_map; i++) { 
110                 struct e820entry *ei = &e820.map[i]; 
111                 if (type && ei->type != type) 
112                         continue;
113                 if (ei->addr >= end || ei->addr + ei->size <= start)
114                         continue; 
115                 return 1; 
116         } 
117         return 0;
118 }
119
120 /* 
121  * Find a free area in a specific range. 
122  */ 
123 unsigned long __init find_e820_area(unsigned long start, unsigned long end, unsigned size) 
124
125         int i; 
126         for (i = 0; i < e820.nr_map; i++) { 
127                 struct e820entry *ei = &e820.map[i]; 
128                 unsigned long addr = ei->addr, last; 
129                 if (ei->type != E820_RAM) 
130                         continue; 
131                 if (addr < start) 
132                         addr = start;
133                 if (addr > ei->addr + ei->size) 
134                         continue; 
135                 while (bad_addr(&addr, size) && addr+size < ei->addr + ei->size)
136                         ;
137                 last = addr + size;
138                 if (last > ei->addr + ei->size)
139                         continue;
140                 if (last > end) 
141                         continue;
142                 return addr; 
143         } 
144         return -1UL;            
145
146
147 /* 
148  * Free bootmem based on the e820 table for a node.
149  */
150 void __init e820_bootmem_free(pg_data_t *pgdat, unsigned long start,unsigned long end)
151 {
152         int i;
153         for (i = 0; i < e820.nr_map; i++) {
154                 struct e820entry *ei = &e820.map[i]; 
155                 unsigned long last, addr;
156
157                 if (ei->type != E820_RAM || 
158                     ei->addr+ei->size <= start || 
159                     ei->addr >= end)
160                         continue;
161
162                 addr = round_up(ei->addr, PAGE_SIZE);
163                 if (addr < start) 
164                         addr = start;
165
166                 last = round_down(ei->addr + ei->size, PAGE_SIZE); 
167                 if (last >= end)
168                         last = end; 
169
170                 if (last > addr && last-addr >= PAGE_SIZE)
171                         free_bootmem_node(pgdat, addr, last-addr);
172         }
173 }
174
175 /*
176  * Find the highest page frame number we have available
177  */
178 unsigned long __init e820_end_of_ram(void)
179 {
180         int i;
181         unsigned long end_pfn = 0;
182         
183         for (i = 0; i < e820.nr_map; i++) {
184                 struct e820entry *ei = &e820.map[i]; 
185                 unsigned long start, end;
186
187                 start = round_up(ei->addr, PAGE_SIZE); 
188                 end = round_down(ei->addr + ei->size, PAGE_SIZE); 
189                 if (start >= end)
190                         continue;
191                 if (ei->type == E820_RAM) { 
192                 if (end > end_pfn<<PAGE_SHIFT)
193                         end_pfn = end>>PAGE_SHIFT;
194                 } else { 
195                         if (end > end_pfn_map<<PAGE_SHIFT) 
196                                 end_pfn_map = end>>PAGE_SHIFT;
197                 } 
198         }
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         return end_pfn; 
210 }
211
212 /* 
213  * Compute how much memory is missing in a range.
214  * Unlike the other functions in this file the arguments are in page numbers.
215  */
216 unsigned long __init
217 e820_hole_size(unsigned long start_pfn, unsigned long end_pfn)
218 {
219         unsigned long ram = 0;
220         unsigned long start = start_pfn << PAGE_SHIFT;
221         unsigned long end = end_pfn << PAGE_SHIFT;
222         int i;
223         for (i = 0; i < e820.nr_map; i++) {
224                 struct e820entry *ei = &e820.map[i];
225                 unsigned long last, addr;
226
227                 if (ei->type != E820_RAM ||
228                     ei->addr+ei->size <= start ||
229                     ei->addr >= end)
230                         continue;
231
232                 addr = round_up(ei->addr, PAGE_SIZE);
233                 if (addr < start)
234                         addr = start;
235
236                 last = round_down(ei->addr + ei->size, PAGE_SIZE);
237                 if (last >= end)
238                         last = end;
239
240                 if (last > addr)
241                         ram += last - addr;
242         }
243         return ((end - start) - ram) >> PAGE_SHIFT;
244 }
245
246 /*
247  * Mark e820 reserved areas as busy for the resource manager.
248  */
249 void __init e820_reserve_resources(void)
250 {
251         int i;
252         for (i = 0; i < e820.nr_map; i++) {
253                 struct resource *res;
254                 res = alloc_bootmem_low(sizeof(struct resource));
255                 switch (e820.map[i].type) {
256                 case E820_RAM:  res->name = "System RAM"; break;
257                 case E820_ACPI: res->name = "ACPI Tables"; break;
258                 case E820_NVS:  res->name = "ACPI Non-volatile Storage"; break;
259                 default:        res->name = "reserved";
260                 }
261                 res->start = e820.map[i].addr;
262                 res->end = res->start + e820.map[i].size - 1;
263                 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
264                 request_resource(&iomem_resource, res);
265                 if (e820.map[i].type == E820_RAM) {
266                         /*
267                          *  We don't know which RAM region contains kernel data,
268                          *  so we try it repeatedly and let the resource manager
269                          *  test it.
270                          */
271                         request_resource(res, &code_resource);
272                         request_resource(res, &data_resource);
273 #ifdef CONFIG_KEXEC
274                         request_resource(res, &crashk_res);
275 #endif
276                 }
277         }
278 }
279 #endif /* CONFIG_XEN */
280
281 void __init e820_print_map(char *who)
282 {
283         int i;
284
285         for (i = 0; i < e820.nr_map; i++) {
286                 printk(" %s: %016Lx - %016Lx ", who,
287                         (unsigned long long) e820.map[i].addr,
288                         (unsigned long long) (e820.map[i].addr + e820.map[i].size));
289                 switch (e820.map[i].type) {
290                 case E820_RAM:  printk("(usable)\n");
291                                 break;
292                 case E820_RESERVED:
293                                 printk("(reserved)\n");
294                                 break;
295                 case E820_ACPI:
296                                 printk("(ACPI data)\n");
297                                 break;
298                 case E820_NVS:
299                                 printk("(ACPI NVS)\n");
300                                 break;
301                 default:        printk("type %u\n", e820.map[i].type);
302                                 break;
303                 }
304         }
305 }
306
307 #ifndef CONFIG_XEN
308 /*
309  * Sanitize the BIOS e820 map.
310  *
311  * Some e820 responses include overlapping entries.  The following 
312  * replaces the original e820 map with a new one, removing overlaps.
313  *
314  */
315 static int __init sanitize_e820_map(struct e820entry * biosmap, char * pnr_map)
316 {
317         struct change_member {
318                 struct e820entry *pbios; /* pointer to original bios entry */
319                 unsigned long long addr; /* address for this change point */
320         };
321         static struct change_member change_point_list[2*E820MAX] __initdata;
322         static struct change_member *change_point[2*E820MAX] __initdata;
323         static struct e820entry *overlap_list[E820MAX] __initdata;
324         static struct e820entry new_bios[E820MAX] __initdata;
325         struct change_member *change_tmp;
326         unsigned long current_type, last_type;
327         unsigned long long last_addr;
328         int chgidx, still_changing;
329         int overlap_entries;
330         int new_bios_entry;
331         int old_nr, new_nr, chg_nr;
332         int i;
333
334         /*
335                 Visually we're performing the following (1,2,3,4 = memory types)...
336
337                 Sample memory map (w/overlaps):
338                    ____22__________________
339                    ______________________4_
340                    ____1111________________
341                    _44_____________________
342                    11111111________________
343                    ____________________33__
344                    ___________44___________
345                    __________33333_________
346                    ______________22________
347                    ___________________2222_
348                    _________111111111______
349                    _____________________11_
350                    _________________4______
351
352                 Sanitized equivalent (no overlap):
353                    1_______________________
354                    _44_____________________
355                    ___1____________________
356                    ____22__________________
357                    ______11________________
358                    _________1______________
359                    __________3_____________
360                    ___________44___________
361                    _____________33_________
362                    _______________2________
363                    ________________1_______
364                    _________________4______
365                    ___________________2____
366                    ____________________33__
367                    ______________________4_
368         */
369
370         /* if there's only one memory region, don't bother */
371         if (*pnr_map < 2)
372                 return -1;
373
374         old_nr = *pnr_map;
375
376         /* bail out if we find any unreasonable addresses in bios map */
377         for (i=0; i<old_nr; i++)
378                 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
379                         return -1;
380
381         /* create pointers for initial change-point information (for sorting) */
382         for (i=0; i < 2*old_nr; i++)
383                 change_point[i] = &change_point_list[i];
384
385         /* record all known change-points (starting and ending addresses),
386            omitting those that are for empty memory regions */
387         chgidx = 0;
388         for (i=0; i < old_nr; i++)      {
389                 if (biosmap[i].size != 0) {
390                         change_point[chgidx]->addr = biosmap[i].addr;
391                         change_point[chgidx++]->pbios = &biosmap[i];
392                         change_point[chgidx]->addr = biosmap[i].addr + biosmap[i].size;
393                         change_point[chgidx++]->pbios = &biosmap[i];
394                 }
395         }
396         chg_nr = chgidx;
397
398         /* sort change-point list by memory addresses (low -> high) */
399         still_changing = 1;
400         while (still_changing)  {
401                 still_changing = 0;
402                 for (i=1; i < chg_nr; i++)  {
403                         /* if <current_addr> > <last_addr>, swap */
404                         /* or, if current=<start_addr> & last=<end_addr>, swap */
405                         if ((change_point[i]->addr < change_point[i-1]->addr) ||
406                                 ((change_point[i]->addr == change_point[i-1]->addr) &&
407                                  (change_point[i]->addr == change_point[i]->pbios->addr) &&
408                                  (change_point[i-1]->addr != change_point[i-1]->pbios->addr))
409                            )
410                         {
411                                 change_tmp = change_point[i];
412                                 change_point[i] = change_point[i-1];
413                                 change_point[i-1] = change_tmp;
414                                 still_changing=1;
415                         }
416                 }
417         }
418
419         /* create a new bios memory map, removing overlaps */
420         overlap_entries=0;       /* number of entries in the overlap table */
421         new_bios_entry=0;        /* index for creating new bios map entries */
422         last_type = 0;           /* start with undefined memory type */
423         last_addr = 0;           /* start with 0 as last starting address */
424         /* loop through change-points, determining affect on the new bios map */
425         for (chgidx=0; chgidx < chg_nr; chgidx++)
426         {
427                 /* keep track of all overlapping bios entries */
428                 if (change_point[chgidx]->addr == change_point[chgidx]->pbios->addr)
429                 {
430                         /* add map entry to overlap list (> 1 entry implies an overlap) */
431                         overlap_list[overlap_entries++]=change_point[chgidx]->pbios;
432                 }
433                 else
434                 {
435                         /* remove entry from list (order independent, so swap with last) */
436                         for (i=0; i<overlap_entries; i++)
437                         {
438                                 if (overlap_list[i] == change_point[chgidx]->pbios)
439                                         overlap_list[i] = overlap_list[overlap_entries-1];
440                         }
441                         overlap_entries--;
442                 }
443                 /* if there are overlapping entries, decide which "type" to use */
444                 /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */
445                 current_type = 0;
446                 for (i=0; i<overlap_entries; i++)
447                         if (overlap_list[i]->type > current_type)
448                                 current_type = overlap_list[i]->type;
449                 /* continue building up new bios map based on this information */
450                 if (current_type != last_type)  {
451                         if (last_type != 0)      {
452                                 new_bios[new_bios_entry].size =
453                                         change_point[chgidx]->addr - last_addr;
454                                 /* move forward only if the new size was non-zero */
455                                 if (new_bios[new_bios_entry].size != 0)
456                                         if (++new_bios_entry >= E820MAX)
457                                                 break;  /* no more space left for new bios entries */
458                         }
459                         if (current_type != 0)  {
460                                 new_bios[new_bios_entry].addr = change_point[chgidx]->addr;
461                                 new_bios[new_bios_entry].type = current_type;
462                                 last_addr=change_point[chgidx]->addr;
463                         }
464                         last_type = current_type;
465                 }
466         }
467         new_nr = new_bios_entry;   /* retain count for new bios entries */
468
469         /* copy new bios mapping into original location */
470         memcpy(biosmap, new_bios, new_nr*sizeof(struct e820entry));
471         *pnr_map = new_nr;
472
473         return 0;
474 }
475
476 /*
477  * Copy the BIOS e820 map into a safe place.
478  *
479  * Sanity-check it while we're at it..
480  *
481  * If we're lucky and live on a modern system, the setup code
482  * will have given us a memory map that we can use to properly
483  * set up memory.  If we aren't, we'll fake a memory map.
484  *
485  * We check to see that the memory map contains at least 2 elements
486  * before we'll use it, because the detection code in setup.S may
487  * not be perfect and most every PC known to man has two memory
488  * regions: one from 0 to 640k, and one from 1mb up.  (The IBM
489  * thinkpad 560x, for example, does not cooperate with the memory
490  * detection code.)
491  */
492 static int __init copy_e820_map(struct e820entry * biosmap, int nr_map)
493 {
494         /* Only one memory region (or negative)? Ignore it */
495         if (nr_map < 2)
496                 return -1;
497
498         do {
499                 unsigned long start = biosmap->addr;
500                 unsigned long size = biosmap->size;
501                 unsigned long end = start + size;
502                 unsigned long type = biosmap->type;
503
504                 /* Overflow in 64 bits? Ignore the memory map. */
505                 if (start > end)
506                         return -1;
507
508                 /*
509                  * Some BIOSes claim RAM in the 640k - 1M region.
510                  * Not right. Fix it up.
511                  * 
512                  * This should be removed on Hammer which is supposed to not
513                  * have non e820 covered ISA mappings there, but I had some strange
514                  * problems so it stays for now.  -AK
515                  */
516                 if (type == E820_RAM) {
517                         if (start < 0x100000ULL && end > 0xA0000ULL) {
518                                 if (start < 0xA0000ULL)
519                                         add_memory_region(start, 0xA0000ULL-start, type);
520                                 if (end <= 0x100000ULL)
521                                         continue;
522                                 start = 0x100000ULL;
523                                 size = end - start;
524                         }
525                 }
526
527                 add_memory_region(start, size, type);
528         } while (biosmap++,--nr_map);
529         return 0;
530 }
531
532 void __init setup_memory_region(void)
533 {
534         char *who = "BIOS-e820";
535
536         /*
537          * Try to copy the BIOS-supplied E820-map.
538          *
539          * Otherwise fake a memory map; one section from 0k->640k,
540          * the next section from 1mb->appropriate_mem_k
541          */
542         sanitize_e820_map(E820_MAP, &E820_MAP_NR);
543         if (copy_e820_map(E820_MAP, E820_MAP_NR) < 0) {
544                 unsigned long mem_size;
545
546                 /* compare results from other methods and take the greater */
547                 if (ALT_MEM_K < EXT_MEM_K) {
548                         mem_size = EXT_MEM_K;
549                         who = "BIOS-88";
550                 } else {
551                         mem_size = ALT_MEM_K;
552                         who = "BIOS-e801";
553                 }
554
555                 e820.nr_map = 0;
556                 add_memory_region(0, LOWMEMSIZE(), E820_RAM);
557                 add_memory_region(HIGH_MEMORY, mem_size << 10, E820_RAM);
558         }
559         printk(KERN_INFO "BIOS-provided physical RAM map:\n");
560         e820_print_map(who);
561 }
562
563 #else  /* CONFIG_XEN */
564
565 extern unsigned long xen_override_max_pfn;
566 extern union xen_start_info_union xen_start_info_union;
567
568 unsigned long __init e820_end_of_ram(void)
569 {
570         unsigned long max_end_pfn;
571
572         if (xen_override_max_pfn == 0) {
573                 max_end_pfn = xen_start_info->nr_pages;
574                 /* Default 8MB slack (to balance backend allocations). */
575                 max_end_pfn += 8 << (20 - PAGE_SHIFT);
576         } else if (xen_override_max_pfn > xen_start_info->nr_pages) {
577                 max_end_pfn = xen_override_max_pfn;
578         } else {
579                 max_end_pfn = xen_start_info->nr_pages;
580         }
581
582         return max_end_pfn;
583 }
584
585 unsigned long __init
586 e820_hole_size(unsigned long start_pfn, unsigned long end_pfn)
587 {
588         return 0;
589 }
590
591 void __init e820_reserve_resources(void) 
592 {
593         dom0_op_t op;
594         struct dom0_memory_map_entry *map;
595         unsigned long gapstart, gapsize, round, last;
596         int i, found = 0;
597
598         if (!(xen_start_info->flags & SIF_INITDOMAIN))
599                 return;
600
601         map = alloc_bootmem_low_pages(PAGE_SIZE);
602         op.cmd = DOM0_PHYSICAL_MEMORY_MAP;
603         op.u.physical_memory_map.memory_map = map;
604         op.u.physical_memory_map.max_map_entries =
605                 PAGE_SIZE / sizeof(struct dom0_memory_map_entry);
606         BUG_ON(HYPERVISOR_dom0_op(&op));
607
608         last = 0x100000000ULL;
609         gapstart = 0x10000000;
610         gapsize = 0x400000;
611
612         for (i = op.u.physical_memory_map.nr_map_entries - 1; i >= 0; i--) {
613                 struct resource *res;
614
615                 if ((last > map[i].end) && ((last - map[i].end) > gapsize)) {
616                         gapsize = last - map[i].end;
617                         gapstart = map[i].end;
618                         found = 1;
619                 }
620                 if (map[i].start < last)
621                         last = map[i].start;
622
623                 if (map[i].end > 0x100000000ULL)
624                         continue;
625                 res = alloc_bootmem_low(sizeof(struct resource));
626                 res->name = map[i].is_ram ? "System RAM" : "reserved";
627                 res->start = map[i].start;
628                 res->end = map[i].end - 1;
629                 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
630                 request_resource(&iomem_resource, res);
631         }
632
633         free_bootmem(__pa(map), PAGE_SIZE);
634
635         if (!found) {
636                 gapstart = HYPERVISOR_memory_op(XENMEM_maximum_ram_page, NULL);
637                 gapstart = (gapstart << PAGE_SHIFT) + 1024*1024;
638                 printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit address range\n"
639                        KERN_ERR "PCI: Unassigned devices with 32bit resource registers may break!\n");
640         }
641
642         /*
643          * See how much we want to round up: start off with
644          * rounding to the next 1MB area.
645          */
646         round = 0x100000;
647         while ((gapsize >> 4) > round)
648                 round += round;
649         /* Fun with two's complement */
650         pci_mem_start = (gapstart + round) & -round;
651
652         printk(KERN_INFO "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
653                 pci_mem_start, gapstart, gapsize);
654 }
655
656 #endif
657
658 void __init parse_memopt(char *p, char **from) 
659
660         end_user_pfn = memparse(p, from);
661         end_user_pfn >>= PAGE_SHIFT;    
662         xen_override_max_pfn = (unsigned long) end_user_pfn;
663
664
665 void __init parse_memmapopt(char *p, char **from)
666 {
667         unsigned long long start_at, mem_size;
668
669         mem_size = memparse(p, from);
670         p = *from;
671         if (*p == '@') {
672                 start_at = memparse(p+1, from);
673                 add_memory_region(start_at, mem_size, E820_RAM);
674         } else if (*p == '#') {
675                 start_at = memparse(p+1, from);
676                 add_memory_region(start_at, mem_size, E820_ACPI);
677         } else if (*p == '$') {
678                 start_at = memparse(p+1, from);
679                 add_memory_region(start_at, mem_size, E820_RESERVED);
680         } else {
681                 end_user_pfn = (mem_size >> PAGE_SHIFT);
682         }
683         p = *from;
684 }
685
686 /*
687  * Search for the biggest gap in the low 32 bits of the e820
688  * memory space.  We pass this space to PCI to assign MMIO resources
689  * for hotplug or unconfigured devices in.
690  * Hopefully the BIOS let enough space left.
691  */
692 __init void e820_setup_gap(void)
693 {
694 #ifndef CONFIG_XEN
695         unsigned long gapstart, gapsize;
696         unsigned long last;
697         int i;
698         int found = 0;
699
700         last = 0x100000000ull;
701         gapstart = 0x10000000;
702         gapsize = 0x400000;
703         i = e820.nr_map;
704         while (--i >= 0) {
705                 unsigned long long start = e820.map[i].addr;
706                 unsigned long long end = start + e820.map[i].size;
707
708                 /*
709                  * Since "last" is at most 4GB, we know we'll
710                  * fit in 32 bits if this condition is true
711                  */
712                 if (last > end) {
713                         unsigned long gap = last - end;
714
715                         if (gap > gapsize) {
716                                 gapsize = gap;
717                                 gapstart = end;
718                                 found = 1;
719                         }
720                 }
721                 if (start < last)
722                         last = start;
723         }
724
725         if (!found) {
726                 gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
727                 printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit address range\n"
728                        KERN_ERR "PCI: Unassigned devices with 32bit resource registers may break!\n");
729         }
730
731         /*
732          * Start allocating dynamic PCI memory a bit into the gap,
733          * aligned up to the nearest megabyte.
734          *
735          * Question: should we try to pad it up a bit (do something
736          * like " + (gapsize >> 3)" in there too?). We now have the
737          * technology.
738          */
739         pci_mem_start = (gapstart + 0xfffff) & ~0xfffff;
740
741         printk(KERN_INFO "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
742                 pci_mem_start, gapstart, gapsize);
743 #endif
744 }