]> Pileus Git - ~andy/linux/blob - arch/x86/kernel/e820_32.c
x86: fix trimming e820 with MTRR holes. - fix
[~andy/linux] / arch / x86 / kernel / e820_32.c
1 #include <linux/kernel.h>
2 #include <linux/types.h>
3 #include <linux/init.h>
4 #include <linux/bootmem.h>
5 #include <linux/ioport.h>
6 #include <linux/string.h>
7 #include <linux/kexec.h>
8 #include <linux/module.h>
9 #include <linux/mm.h>
10 #include <linux/pfn.h>
11 #include <linux/uaccess.h>
12 #include <linux/suspend.h>
13
14 #include <asm/pgtable.h>
15 #include <asm/page.h>
16 #include <asm/e820.h>
17 #include <asm/setup.h>
18
19 struct e820map e820;
20 struct change_member {
21         struct e820entry *pbios; /* pointer to original bios entry */
22         unsigned long long addr; /* address for this change point */
23 };
24 static struct change_member change_point_list[2*E820MAX] __initdata;
25 static struct change_member *change_point[2*E820MAX] __initdata;
26 static struct e820entry *overlap_list[E820MAX] __initdata;
27 static struct e820entry new_bios[E820MAX] __initdata;
28 /* For PCI or other memory-mapped resources */
29 unsigned long pci_mem_start = 0x10000000;
30 #ifdef CONFIG_PCI
31 EXPORT_SYMBOL(pci_mem_start);
32 #endif
33
34 static struct resource system_rom_resource = {
35         .name   = "System ROM",
36         .start  = 0xf0000,
37         .end    = 0xfffff,
38         .flags  = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
39 };
40
41 static struct resource extension_rom_resource = {
42         .name   = "Extension ROM",
43         .start  = 0xe0000,
44         .end    = 0xeffff,
45         .flags  = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
46 };
47
48 static struct resource adapter_rom_resources[] = { {
49         .name   = "Adapter ROM",
50         .start  = 0xc8000,
51         .end    = 0,
52         .flags  = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
53 }, {
54         .name   = "Adapter ROM",
55         .start  = 0,
56         .end    = 0,
57         .flags  = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
58 }, {
59         .name   = "Adapter ROM",
60         .start  = 0,
61         .end    = 0,
62         .flags  = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
63 }, {
64         .name   = "Adapter ROM",
65         .start  = 0,
66         .end    = 0,
67         .flags  = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
68 }, {
69         .name   = "Adapter ROM",
70         .start  = 0,
71         .end    = 0,
72         .flags  = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
73 }, {
74         .name   = "Adapter ROM",
75         .start  = 0,
76         .end    = 0,
77         .flags  = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
78 } };
79
80 static struct resource video_rom_resource = {
81         .name   = "Video ROM",
82         .start  = 0xc0000,
83         .end    = 0xc7fff,
84         .flags  = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
85 };
86
87 #define ROMSIGNATURE 0xaa55
88
89 static int __init romsignature(const unsigned char *rom)
90 {
91         const unsigned short * const ptr = (const unsigned short *)rom;
92         unsigned short sig;
93
94         return probe_kernel_address(ptr, sig) == 0 && sig == ROMSIGNATURE;
95 }
96
97 static int __init romchecksum(const unsigned char *rom, unsigned long length)
98 {
99         unsigned char sum, c;
100
101         for (sum = 0; length && probe_kernel_address(rom++, c) == 0; length--)
102                 sum += c;
103         return !length && !sum;
104 }
105
106 static void __init probe_roms(void)
107 {
108         const unsigned char *rom;
109         unsigned long start, length, upper;
110         unsigned char c;
111         int i;
112
113         /* video rom */
114         upper = adapter_rom_resources[0].start;
115         for (start = video_rom_resource.start; start < upper; start += 2048) {
116                 rom = isa_bus_to_virt(start);
117                 if (!romsignature(rom))
118                         continue;
119
120                 video_rom_resource.start = start;
121
122                 if (probe_kernel_address(rom + 2, c) != 0)
123                         continue;
124
125                 /* 0 < length <= 0x7f * 512, historically */
126                 length = c * 512;
127
128                 /* if checksum okay, trust length byte */
129                 if (length && romchecksum(rom, length))
130                         video_rom_resource.end = start + length - 1;
131
132                 request_resource(&iomem_resource, &video_rom_resource);
133                 break;
134         }
135
136         start = (video_rom_resource.end + 1 + 2047) & ~2047UL;
137         if (start < upper)
138                 start = upper;
139
140         /* system rom */
141         request_resource(&iomem_resource, &system_rom_resource);
142         upper = system_rom_resource.start;
143
144         /* check for extension rom (ignore length byte!) */
145         rom = isa_bus_to_virt(extension_rom_resource.start);
146         if (romsignature(rom)) {
147                 length = extension_rom_resource.end - extension_rom_resource.start + 1;
148                 if (romchecksum(rom, length)) {
149                         request_resource(&iomem_resource, &extension_rom_resource);
150                         upper = extension_rom_resource.start;
151                 }
152         }
153
154         /* check for adapter roms on 2k boundaries */
155         for (i = 0; i < ARRAY_SIZE(adapter_rom_resources) && start < upper; start += 2048) {
156                 rom = isa_bus_to_virt(start);
157                 if (!romsignature(rom))
158                         continue;
159
160                 if (probe_kernel_address(rom + 2, c) != 0)
161                         continue;
162
163                 /* 0 < length <= 0x7f * 512, historically */
164                 length = c * 512;
165
166                 /* but accept any length that fits if checksum okay */
167                 if (!length || start + length > upper || !romchecksum(rom, length))
168                         continue;
169
170                 adapter_rom_resources[i].start = start;
171                 adapter_rom_resources[i].end = start + length - 1;
172                 request_resource(&iomem_resource, &adapter_rom_resources[i]);
173
174                 start = adapter_rom_resources[i++].end & ~2047UL;
175         }
176 }
177
178 /*
179  * Request address space for all standard RAM and ROM resources
180  * and also for regions reported as reserved by the e820.
181  */
182 void __init init_iomem_resources(struct resource *code_resource,
183                 struct resource *data_resource,
184                 struct resource *bss_resource)
185 {
186         int i;
187
188         probe_roms();
189         for (i = 0; i < e820.nr_map; i++) {
190                 struct resource *res;
191 #ifndef CONFIG_RESOURCES_64BIT
192                 if (e820.map[i].addr + e820.map[i].size > 0x100000000ULL)
193                         continue;
194 #endif
195                 res = kzalloc(sizeof(struct resource), GFP_ATOMIC);
196                 switch (e820.map[i].type) {
197                 case E820_RAM:  res->name = "System RAM"; break;
198                 case E820_ACPI: res->name = "ACPI Tables"; break;
199                 case E820_NVS:  res->name = "ACPI Non-volatile Storage"; break;
200                 default:        res->name = "reserved";
201                 }
202                 res->start = e820.map[i].addr;
203                 res->end = res->start + e820.map[i].size - 1;
204                 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
205                 if (request_resource(&iomem_resource, res)) {
206                         kfree(res);
207                         continue;
208                 }
209                 if (e820.map[i].type == E820_RAM) {
210                         /*
211                          *  We don't know which RAM region contains kernel data,
212                          *  so we try it repeatedly and let the resource manager
213                          *  test it.
214                          */
215                         request_resource(res, code_resource);
216                         request_resource(res, data_resource);
217                         request_resource(res, bss_resource);
218 #ifdef CONFIG_KEXEC
219                         if (crashk_res.start != crashk_res.end)
220                                 request_resource(res, &crashk_res);
221 #endif
222                 }
223         }
224 }
225
226 #if defined(CONFIG_PM) && defined(CONFIG_HIBERNATION)
227 /**
228  * e820_mark_nosave_regions - Find the ranges of physical addresses that do not
229  * correspond to e820 RAM areas and mark the corresponding pages as nosave for
230  * hibernation.
231  *
232  * This function requires the e820 map to be sorted and without any
233  * overlapping entries and assumes the first e820 area to be RAM.
234  */
235 void __init e820_mark_nosave_regions(void)
236 {
237         int i;
238         unsigned long pfn;
239
240         pfn = PFN_DOWN(e820.map[0].addr + e820.map[0].size);
241         for (i = 1; i < e820.nr_map; i++) {
242                 struct e820entry *ei = &e820.map[i];
243
244                 if (pfn < PFN_UP(ei->addr))
245                         register_nosave_region(pfn, PFN_UP(ei->addr));
246
247                 pfn = PFN_DOWN(ei->addr + ei->size);
248                 if (ei->type != E820_RAM)
249                         register_nosave_region(PFN_UP(ei->addr), pfn);
250
251                 if (pfn >= max_low_pfn)
252                         break;
253         }
254 }
255 #endif
256
257 void __init add_memory_region(unsigned long long start,
258                               unsigned long long size, int type)
259 {
260         int x;
261
262         x = e820.nr_map;
263
264         if (x == E820MAX) {
265                 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
266                 return;
267         }
268
269         e820.map[x].addr = start;
270         e820.map[x].size = size;
271         e820.map[x].type = type;
272         e820.nr_map++;
273 } /* add_memory_region */
274
275 /*
276  * Sanitize the BIOS e820 map.
277  *
278  * Some e820 responses include overlapping entries.  The following
279  * replaces the original e820 map with a new one, removing overlaps.
280  *
281  */
282 int __init sanitize_e820_map(struct e820entry * biosmap, char * pnr_map)
283 {
284         struct change_member *change_tmp;
285         unsigned long current_type, last_type;
286         unsigned long long last_addr;
287         int chgidx, still_changing;
288         int overlap_entries;
289         int new_bios_entry;
290         int old_nr, new_nr, chg_nr;
291         int i;
292
293         /*
294                 Visually we're performing the following (1,2,3,4 = memory types)...
295
296                 Sample memory map (w/overlaps):
297                    ____22__________________
298                    ______________________4_
299                    ____1111________________
300                    _44_____________________
301                    11111111________________
302                    ____________________33__
303                    ___________44___________
304                    __________33333_________
305                    ______________22________
306                    ___________________2222_
307                    _________111111111______
308                    _____________________11_
309                    _________________4______
310
311                 Sanitized equivalent (no overlap):
312                    1_______________________
313                    _44_____________________
314                    ___1____________________
315                    ____22__________________
316                    ______11________________
317                    _________1______________
318                    __________3_____________
319                    ___________44___________
320                    _____________33_________
321                    _______________2________
322                    ________________1_______
323                    _________________4______
324                    ___________________2____
325                    ____________________33__
326                    ______________________4_
327         */
328         /* if there's only one memory region, don't bother */
329         if (*pnr_map < 2) {
330                 return -1;
331         }
332
333         old_nr = *pnr_map;
334
335         /* bail out if we find any unreasonable addresses in bios map */
336         for (i=0; i<old_nr; i++)
337                 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr) {
338                         return -1;
339                 }
340
341         /* create pointers for initial change-point information (for sorting) */
342         for (i=0; i < 2*old_nr; i++)
343                 change_point[i] = &change_point_list[i];
344
345         /* record all known change-points (starting and ending addresses),
346            omitting those that are for empty memory regions */
347         chgidx = 0;
348         for (i=0; i < old_nr; i++)      {
349                 if (biosmap[i].size != 0) {
350                         change_point[chgidx]->addr = biosmap[i].addr;
351                         change_point[chgidx++]->pbios = &biosmap[i];
352                         change_point[chgidx]->addr = biosmap[i].addr + biosmap[i].size;
353                         change_point[chgidx++]->pbios = &biosmap[i];
354                 }
355         }
356         chg_nr = chgidx;        /* true number of change-points */
357
358         /* sort change-point list by memory addresses (low -> high) */
359         still_changing = 1;
360         while (still_changing)  {
361                 still_changing = 0;
362                 for (i=1; i < chg_nr; i++)  {
363                         /* if <current_addr> > <last_addr>, swap */
364                         /* or, if current=<start_addr> & last=<end_addr>, swap */
365                         if ((change_point[i]->addr < change_point[i-1]->addr) ||
366                                 ((change_point[i]->addr == change_point[i-1]->addr) &&
367                                  (change_point[i]->addr == change_point[i]->pbios->addr) &&
368                                  (change_point[i-1]->addr != change_point[i-1]->pbios->addr))
369                            )
370                         {
371                                 change_tmp = change_point[i];
372                                 change_point[i] = change_point[i-1];
373                                 change_point[i-1] = change_tmp;
374                                 still_changing=1;
375                         }
376                 }
377         }
378
379         /* create a new bios memory map, removing overlaps */
380         overlap_entries=0;       /* number of entries in the overlap table */
381         new_bios_entry=0;        /* index for creating new bios map entries */
382         last_type = 0;           /* start with undefined memory type */
383         last_addr = 0;           /* start with 0 as last starting address */
384         /* loop through change-points, determining affect on the new bios map */
385         for (chgidx=0; chgidx < chg_nr; chgidx++)
386         {
387                 /* keep track of all overlapping bios entries */
388                 if (change_point[chgidx]->addr == change_point[chgidx]->pbios->addr)
389                 {
390                         /* add map entry to overlap list (> 1 entry implies an overlap) */
391                         overlap_list[overlap_entries++]=change_point[chgidx]->pbios;
392                 }
393                 else
394                 {
395                         /* remove entry from list (order independent, so swap with last) */
396                         for (i=0; i<overlap_entries; i++)
397                         {
398                                 if (overlap_list[i] == change_point[chgidx]->pbios)
399                                         overlap_list[i] = overlap_list[overlap_entries-1];
400                         }
401                         overlap_entries--;
402                 }
403                 /* if there are overlapping entries, decide which "type" to use */
404                 /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */
405                 current_type = 0;
406                 for (i=0; i<overlap_entries; i++)
407                         if (overlap_list[i]->type > current_type)
408                                 current_type = overlap_list[i]->type;
409                 /* continue building up new bios map based on this information */
410                 if (current_type != last_type)  {
411                         if (last_type != 0)      {
412                                 new_bios[new_bios_entry].size =
413                                         change_point[chgidx]->addr - last_addr;
414                                 /* move forward only if the new size was non-zero */
415                                 if (new_bios[new_bios_entry].size != 0)
416                                         if (++new_bios_entry >= E820MAX)
417                                                 break;  /* no more space left for new bios entries */
418                         }
419                         if (current_type != 0)  {
420                                 new_bios[new_bios_entry].addr = change_point[chgidx]->addr;
421                                 new_bios[new_bios_entry].type = current_type;
422                                 last_addr=change_point[chgidx]->addr;
423                         }
424                         last_type = current_type;
425                 }
426         }
427         new_nr = new_bios_entry;   /* retain count for new bios entries */
428
429         /* copy new bios mapping into original location */
430         memcpy(biosmap, new_bios, new_nr*sizeof(struct e820entry));
431         *pnr_map = new_nr;
432
433         return 0;
434 }
435
436 /*
437  * Copy the BIOS e820 map into a safe place.
438  *
439  * Sanity-check it while we're at it..
440  *
441  * If we're lucky and live on a modern system, the setup code
442  * will have given us a memory map that we can use to properly
443  * set up memory.  If we aren't, we'll fake a memory map.
444  *
445  * We check to see that the memory map contains at least 2 elements
446  * before we'll use it, because the detection code in setup.S may
447  * not be perfect and most every PC known to man has two memory
448  * regions: one from 0 to 640k, and one from 1mb up.  (The IBM
449  * thinkpad 560x, for example, does not cooperate with the memory
450  * detection code.)
451  */
452 int __init copy_e820_map(struct e820entry *biosmap, int nr_map)
453 {
454         /* Only one memory region (or negative)? Ignore it */
455         if (nr_map < 2)
456                 return -1;
457
458         do {
459                 u64 start = biosmap->addr;
460                 u64 size = biosmap->size;
461                 u64 end = start + size;
462                 u32 type = biosmap->type;
463
464                 /* Overflow in 64 bits? Ignore the memory map. */
465                 if (start > end)
466                         return -1;
467
468                 add_memory_region(start, size, type);
469         } while (biosmap++, --nr_map);
470
471         return 0;
472 }
473
474 /*
475  * Find the highest page frame number we have available
476  */
477 void __init propagate_e820_map(void)
478 {
479         int i;
480
481         max_pfn = 0;
482
483         for (i = 0; i < e820.nr_map; i++) {
484                 unsigned long start, end;
485                 /* RAM? */
486                 if (e820.map[i].type != E820_RAM)
487                         continue;
488                 start = PFN_UP(e820.map[i].addr);
489                 end = PFN_DOWN(e820.map[i].addr + e820.map[i].size);
490                 if (start >= end)
491                         continue;
492                 if (end > max_pfn)
493                         max_pfn = end;
494                 memory_present(0, start, end);
495         }
496 }
497
498 /*
499  * Register fully available low RAM pages with the bootmem allocator.
500  */
501 void __init register_bootmem_low_pages(unsigned long max_low_pfn)
502 {
503         int i;
504
505         for (i = 0; i < e820.nr_map; i++) {
506                 unsigned long curr_pfn, last_pfn, size;
507                 /*
508                  * Reserve usable low memory
509                  */
510                 if (e820.map[i].type != E820_RAM)
511                         continue;
512                 /*
513                  * We are rounding up the start address of usable memory:
514                  */
515                 curr_pfn = PFN_UP(e820.map[i].addr);
516                 if (curr_pfn >= max_low_pfn)
517                         continue;
518                 /*
519                  * ... and at the end of the usable range downwards:
520                  */
521                 last_pfn = PFN_DOWN(e820.map[i].addr + e820.map[i].size);
522
523                 if (last_pfn > max_low_pfn)
524                         last_pfn = max_low_pfn;
525
526                 /*
527                  * .. finally, did all the rounding and playing
528                  * around just make the area go away?
529                  */
530                 if (last_pfn <= curr_pfn)
531                         continue;
532
533                 size = last_pfn - curr_pfn;
534                 free_bootmem(PFN_PHYS(curr_pfn), PFN_PHYS(size));
535         }
536 }
537
538 void __init e820_register_memory(void)
539 {
540         unsigned long gapstart, gapsize, round;
541         unsigned long long last;
542         int i;
543
544         /*
545          * Search for the biggest gap in the low 32 bits of the e820
546          * memory space.
547          */
548         last = 0x100000000ull;
549         gapstart = 0x10000000;
550         gapsize = 0x400000;
551         i = e820.nr_map;
552         while (--i >= 0) {
553                 unsigned long long start = e820.map[i].addr;
554                 unsigned long long end = start + e820.map[i].size;
555
556                 /*
557                  * Since "last" is at most 4GB, we know we'll
558                  * fit in 32 bits if this condition is true
559                  */
560                 if (last > end) {
561                         unsigned long gap = last - end;
562
563                         if (gap > gapsize) {
564                                 gapsize = gap;
565                                 gapstart = end;
566                         }
567                 }
568                 if (start < last)
569                         last = start;
570         }
571
572         /*
573          * See how much we want to round up: start off with
574          * rounding to the next 1MB area.
575          */
576         round = 0x100000;
577         while ((gapsize >> 4) > round)
578                 round += round;
579         /* Fun with two's complement */
580         pci_mem_start = (gapstart + round) & -round;
581
582         printk("Allocating PCI resources starting at %08lx (gap: %08lx:%08lx)\n",
583                 pci_mem_start, gapstart, gapsize);
584 }
585
586 static void __init print_memory_map(char *who)
587 {
588         int i;
589
590         for (i = 0; i < e820.nr_map; i++) {
591                 printk(" %s: %016Lx - %016Lx ", who,
592                         e820.map[i].addr,
593                         e820.map[i].addr + e820.map[i].size);
594                 switch (e820.map[i].type) {
595                 case E820_RAM:  printk("(usable)\n");
596                                 break;
597                 case E820_RESERVED:
598                                 printk("(reserved)\n");
599                                 break;
600                 case E820_ACPI:
601                                 printk("(ACPI data)\n");
602                                 break;
603                 case E820_NVS:
604                                 printk("(ACPI NVS)\n");
605                                 break;
606                 default:        printk("type %u\n", e820.map[i].type);
607                                 break;
608                 }
609         }
610 }
611
612 void __init limit_regions(unsigned long long size)
613 {
614         unsigned long long current_addr;
615         int i;
616
617         print_memory_map("limit_regions start");
618         for (i = 0; i < e820.nr_map; i++) {
619                 current_addr = e820.map[i].addr + e820.map[i].size;
620                 if (current_addr < size)
621                         continue;
622
623                 if (e820.map[i].type != E820_RAM)
624                         continue;
625
626                 if (e820.map[i].addr >= size) {
627                         /*
628                          * This region starts past the end of the
629                          * requested size, skip it completely.
630                          */
631                         e820.nr_map = i;
632                 } else {
633                         e820.nr_map = i + 1;
634                         e820.map[i].size -= current_addr - size;
635                 }
636                 print_memory_map("limit_regions endfor");
637                 return;
638         }
639         print_memory_map("limit_regions endfunc");
640 }
641
642 /*
643  * This function checks if any part of the range <start,end> is mapped
644  * with type.
645  */
646 int
647 e820_any_mapped(u64 start, u64 end, unsigned type)
648 {
649         int i;
650         for (i = 0; i < e820.nr_map; i++) {
651                 const struct e820entry *ei = &e820.map[i];
652                 if (type && ei->type != type)
653                         continue;
654                 if (ei->addr >= end || ei->addr + ei->size <= start)
655                         continue;
656                 return 1;
657         }
658         return 0;
659 }
660 EXPORT_SYMBOL_GPL(e820_any_mapped);
661
662  /*
663   * This function checks if the entire range <start,end> is mapped with type.
664   *
665   * Note: this function only works correct if the e820 table is sorted and
666   * not-overlapping, which is the case
667   */
668 int __init
669 e820_all_mapped(unsigned long s, unsigned long e, unsigned type)
670 {
671         u64 start = s;
672         u64 end = e;
673         int i;
674         for (i = 0; i < e820.nr_map; i++) {
675                 struct e820entry *ei = &e820.map[i];
676                 if (type && ei->type != type)
677                         continue;
678                 /* is the region (part) in overlap with the current region ?*/
679                 if (ei->addr >= end || ei->addr + ei->size <= start)
680                         continue;
681                 /* if the region is at the beginning of <start,end> we move
682                  * start to the end of the region since it's ok until there
683                  */
684                 if (ei->addr <= start)
685                         start = ei->addr + ei->size;
686                 /* if start is now at or beyond end, we're done, full
687                  * coverage */
688                 if (start >= end)
689                         return 1; /* we're done */
690         }
691         return 0;
692 }
693
694 /* Overridden in paravirt.c if CONFIG_PARAVIRT */
695 char * __init __attribute__((weak)) memory_setup(void)
696 {
697         return machine_specific_memory_setup();
698 }
699
700 void __init setup_memory_map(void)
701 {
702         printk(KERN_INFO "BIOS-provided physical RAM map:\n");
703         print_memory_map(memory_setup());
704 }
705
706 static int __initdata user_defined_memmap;
707
708 /*
709  * "mem=nopentium" disables the 4MB page tables.
710  * "mem=XXX[kKmM]" defines a memory region from HIGH_MEM
711  * to <mem>, overriding the bios size.
712  * "memmap=XXX[KkmM]@XXX[KkmM]" defines a memory region from
713  * <start> to <start>+<mem>, overriding the bios size.
714  *
715  * HPA tells me bootloaders need to parse mem=, so no new
716  * option should be mem=  [also see Documentation/i386/boot.txt]
717  */
718 static int __init parse_mem(char *arg)
719 {
720         if (!arg)
721                 return -EINVAL;
722
723         if (strcmp(arg, "nopentium") == 0) {
724                 setup_clear_cpu_cap(X86_FEATURE_PSE);
725         } else {
726                 /* If the user specifies memory size, we
727                  * limit the BIOS-provided memory map to
728                  * that size. exactmap can be used to specify
729                  * the exact map. mem=number can be used to
730                  * trim the existing memory map.
731                  */
732                 unsigned long long mem_size;
733
734                 mem_size = memparse(arg, &arg);
735                 limit_regions(mem_size);
736                 user_defined_memmap = 1;
737         }
738         return 0;
739 }
740 early_param("mem", parse_mem);
741
742 static int __init parse_memmap(char *arg)
743 {
744         if (!arg)
745                 return -EINVAL;
746
747         if (strcmp(arg, "exactmap") == 0) {
748 #ifdef CONFIG_CRASH_DUMP
749                 /* If we are doing a crash dump, we
750                  * still need to know the real mem
751                  * size before original memory map is
752                  * reset.
753                  */
754                 propagate_e820_map();
755                 saved_max_pfn = max_pfn;
756 #endif
757                 e820.nr_map = 0;
758                 user_defined_memmap = 1;
759         } else {
760                 /* If the user specifies memory size, we
761                  * limit the BIOS-provided memory map to
762                  * that size. exactmap can be used to specify
763                  * the exact map. mem=number can be used to
764                  * trim the existing memory map.
765                  */
766                 unsigned long long start_at, mem_size;
767
768                 mem_size = memparse(arg, &arg);
769                 if (*arg == '@') {
770                         start_at = memparse(arg+1, &arg);
771                         add_memory_region(start_at, mem_size, E820_RAM);
772                 } else if (*arg == '#') {
773                         start_at = memparse(arg+1, &arg);
774                         add_memory_region(start_at, mem_size, E820_ACPI);
775                 } else if (*arg == '$') {
776                         start_at = memparse(arg+1, &arg);
777                         add_memory_region(start_at, mem_size, E820_RESERVED);
778                 } else {
779                         limit_regions(mem_size);
780                         user_defined_memmap = 1;
781                 }
782         }
783         return 0;
784 }
785 early_param("memmap", parse_memmap);
786 u64 __init update_memory_range(u64 start, u64 size, unsigned old_type,
787                                 unsigned new_type)
788 {
789         int i;
790         u64 real_updated_size = 0;
791
792         BUG_ON(old_type == new_type);
793
794         for (i = 0; i < e820.nr_map; i++) {
795                 struct e820entry *ei = &e820.map[i];
796                 u64 final_start, final_end;
797                 if (ei->type != old_type)
798                         continue;
799                 /* totally covered? */
800                 if (ei->addr >= start &&
801                     (ei->addr + ei->size) <= (start + size)) {
802                         ei->type = new_type;
803                         real_updated_size += ei->size;
804                         continue;
805                 }
806                 /* partially covered */
807                 final_start = max(start, ei->addr);
808                 final_end = min(start + size, ei->addr + ei->size);
809                 if (final_start >= final_end)
810                         continue;
811                 add_memory_region(final_start, final_end - final_start,
812                                          new_type);
813                 real_updated_size += final_end - final_start;
814         }
815
816         return real_updated_size;
817 }
818
819 void __init finish_e820_parsing(void)
820 {
821         if (user_defined_memmap) {
822                 printk(KERN_INFO "user-defined physical RAM map:\n");
823                 print_memory_map("user");
824         }
825 }
826
827 void __init update_e820(void)
828 {
829         u8 nr_map;
830
831         nr_map = e820.nr_map;
832         if (sanitize_e820_map(e820.map, &nr_map))
833                 return;
834         e820.nr_map = nr_map;
835         printk(KERN_INFO "modified physical RAM map:\n");
836         print_memory_map("modified");
837 }