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