x86_32: trim memory by updating e820
[wrt350n-kernel.git] / arch / x86 / kernel / e820_32.c
blob4e16ef4a2659f38f18d613289527f415d9ad92f1
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>
14 #include <asm/pgtable.h>
15 #include <asm/page.h>
16 #include <asm/e820.h>
17 #include <asm/setup.h>
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 */
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 extern int user_defined_memmap;
35 static struct resource system_rom_resource = {
36 .name = "System ROM",
37 .start = 0xf0000,
38 .end = 0xfffff,
39 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
42 static struct resource extension_rom_resource = {
43 .name = "Extension ROM",
44 .start = 0xe0000,
45 .end = 0xeffff,
46 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
49 static struct resource adapter_rom_resources[] = { {
50 .name = "Adapter ROM",
51 .start = 0xc8000,
52 .end = 0,
53 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
54 }, {
55 .name = "Adapter ROM",
56 .start = 0,
57 .end = 0,
58 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
59 }, {
60 .name = "Adapter ROM",
61 .start = 0,
62 .end = 0,
63 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
64 }, {
65 .name = "Adapter ROM",
66 .start = 0,
67 .end = 0,
68 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
69 }, {
70 .name = "Adapter ROM",
71 .start = 0,
72 .end = 0,
73 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
74 }, {
75 .name = "Adapter ROM",
76 .start = 0,
77 .end = 0,
78 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
79 } };
81 static struct resource video_rom_resource = {
82 .name = "Video ROM",
83 .start = 0xc0000,
84 .end = 0xc7fff,
85 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
88 #define ROMSIGNATURE 0xaa55
90 static int __init romsignature(const unsigned char *rom)
92 const unsigned short * const ptr = (const unsigned short *)rom;
93 unsigned short sig;
95 return probe_kernel_address(ptr, sig) == 0 && sig == ROMSIGNATURE;
98 static int __init romchecksum(const unsigned char *rom, unsigned long length)
100 unsigned char sum, c;
102 for (sum = 0; length && probe_kernel_address(rom++, c) == 0; length--)
103 sum += c;
104 return !length && !sum;
107 static void __init probe_roms(void)
109 const unsigned char *rom;
110 unsigned long start, length, upper;
111 unsigned char c;
112 int i;
114 /* video rom */
115 upper = adapter_rom_resources[0].start;
116 for (start = video_rom_resource.start; start < upper; start += 2048) {
117 rom = isa_bus_to_virt(start);
118 if (!romsignature(rom))
119 continue;
121 video_rom_resource.start = start;
123 if (probe_kernel_address(rom + 2, c) != 0)
124 continue;
126 /* 0 < length <= 0x7f * 512, historically */
127 length = c * 512;
129 /* if checksum okay, trust length byte */
130 if (length && romchecksum(rom, length))
131 video_rom_resource.end = start + length - 1;
133 request_resource(&iomem_resource, &video_rom_resource);
134 break;
137 start = (video_rom_resource.end + 1 + 2047) & ~2047UL;
138 if (start < upper)
139 start = upper;
141 /* system rom */
142 request_resource(&iomem_resource, &system_rom_resource);
143 upper = system_rom_resource.start;
145 /* check for extension rom (ignore length byte!) */
146 rom = isa_bus_to_virt(extension_rom_resource.start);
147 if (romsignature(rom)) {
148 length = extension_rom_resource.end - extension_rom_resource.start + 1;
149 if (romchecksum(rom, length)) {
150 request_resource(&iomem_resource, &extension_rom_resource);
151 upper = extension_rom_resource.start;
155 /* check for adapter roms on 2k boundaries */
156 for (i = 0; i < ARRAY_SIZE(adapter_rom_resources) && start < upper; start += 2048) {
157 rom = isa_bus_to_virt(start);
158 if (!romsignature(rom))
159 continue;
161 if (probe_kernel_address(rom + 2, c) != 0)
162 continue;
164 /* 0 < length <= 0x7f * 512, historically */
165 length = c * 512;
167 /* but accept any length that fits if checksum okay */
168 if (!length || start + length > upper || !romchecksum(rom, length))
169 continue;
171 adapter_rom_resources[i].start = start;
172 adapter_rom_resources[i].end = start + length - 1;
173 request_resource(&iomem_resource, &adapter_rom_resources[i]);
175 start = adapter_rom_resources[i++].end & ~2047UL;
180 * Request address space for all standard RAM and ROM resources
181 * and also for regions reported as reserved by the e820.
183 void __init init_iomem_resources(struct resource *code_resource,
184 struct resource *data_resource,
185 struct resource *bss_resource)
187 int i;
189 probe_roms();
190 for (i = 0; i < e820.nr_map; i++) {
191 struct resource *res;
192 #ifndef CONFIG_RESOURCES_64BIT
193 if (e820.map[i].addr + e820.map[i].size > 0x100000000ULL)
194 continue;
195 #endif
196 res = kzalloc(sizeof(struct resource), GFP_ATOMIC);
197 switch (e820.map[i].type) {
198 case E820_RAM: res->name = "System RAM"; break;
199 case E820_ACPI: res->name = "ACPI Tables"; break;
200 case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
201 default: res->name = "reserved";
203 res->start = e820.map[i].addr;
204 res->end = res->start + e820.map[i].size - 1;
205 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
206 if (request_resource(&iomem_resource, res)) {
207 kfree(res);
208 continue;
210 if (e820.map[i].type == E820_RAM) {
212 * We don't know which RAM region contains kernel data,
213 * so we try it repeatedly and let the resource manager
214 * test it.
216 request_resource(res, code_resource);
217 request_resource(res, data_resource);
218 request_resource(res, bss_resource);
219 #ifdef CONFIG_KEXEC
220 if (crashk_res.start != crashk_res.end)
221 request_resource(res, &crashk_res);
222 #endif
227 #if defined(CONFIG_PM) && defined(CONFIG_HIBERNATION)
229 * e820_mark_nosave_regions - Find the ranges of physical addresses that do not
230 * correspond to e820 RAM areas and mark the corresponding pages as nosave for
231 * hibernation.
233 * This function requires the e820 map to be sorted and without any
234 * overlapping entries and assumes the first e820 area to be RAM.
236 void __init e820_mark_nosave_regions(void)
238 int i;
239 unsigned long pfn;
241 pfn = PFN_DOWN(e820.map[0].addr + e820.map[0].size);
242 for (i = 1; i < e820.nr_map; i++) {
243 struct e820entry *ei = &e820.map[i];
245 if (pfn < PFN_UP(ei->addr))
246 register_nosave_region(pfn, PFN_UP(ei->addr));
248 pfn = PFN_DOWN(ei->addr + ei->size);
249 if (ei->type != E820_RAM)
250 register_nosave_region(PFN_UP(ei->addr), pfn);
252 if (pfn >= max_low_pfn)
253 break;
256 #endif
258 void __init add_memory_region(unsigned long long start,
259 unsigned long long size, int type)
261 int x;
263 x = e820.nr_map;
265 if (x == E820MAX) {
266 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
267 return;
270 e820.map[x].addr = start;
271 e820.map[x].size = size;
272 e820.map[x].type = type;
273 e820.nr_map++;
274 } /* add_memory_region */
277 * Sanitize the BIOS e820 map.
279 * Some e820 responses include overlapping entries. The following
280 * replaces the original e820 map with a new one, removing overlaps.
283 int __init sanitize_e820_map(struct e820entry * biosmap, char * pnr_map)
285 struct change_member *change_tmp;
286 unsigned long current_type, last_type;
287 unsigned long long last_addr;
288 int chgidx, still_changing;
289 int overlap_entries;
290 int new_bios_entry;
291 int old_nr, new_nr, chg_nr;
292 int i;
295 Visually we're performing the following (1,2,3,4 = memory types)...
297 Sample memory map (w/overlaps):
298 ____22__________________
299 ______________________4_
300 ____1111________________
301 _44_____________________
302 11111111________________
303 ____________________33__
304 ___________44___________
305 __________33333_________
306 ______________22________
307 ___________________2222_
308 _________111111111______
309 _____________________11_
310 _________________4______
312 Sanitized equivalent (no overlap):
313 1_______________________
314 _44_____________________
315 ___1____________________
316 ____22__________________
317 ______11________________
318 _________1______________
319 __________3_____________
320 ___________44___________
321 _____________33_________
322 _______________2________
323 ________________1_______
324 _________________4______
325 ___________________2____
326 ____________________33__
327 ______________________4_
329 /* if there's only one memory region, don't bother */
330 if (*pnr_map < 2) {
331 return -1;
334 old_nr = *pnr_map;
336 /* bail out if we find any unreasonable addresses in bios map */
337 for (i=0; i<old_nr; i++)
338 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr) {
339 return -1;
342 /* create pointers for initial change-point information (for sorting) */
343 for (i=0; i < 2*old_nr; i++)
344 change_point[i] = &change_point_list[i];
346 /* record all known change-points (starting and ending addresses),
347 omitting those that are for empty memory regions */
348 chgidx = 0;
349 for (i=0; i < old_nr; i++) {
350 if (biosmap[i].size != 0) {
351 change_point[chgidx]->addr = biosmap[i].addr;
352 change_point[chgidx++]->pbios = &biosmap[i];
353 change_point[chgidx]->addr = biosmap[i].addr + biosmap[i].size;
354 change_point[chgidx++]->pbios = &biosmap[i];
357 chg_nr = chgidx; /* true number of change-points */
359 /* sort change-point list by memory addresses (low -> high) */
360 still_changing = 1;
361 while (still_changing) {
362 still_changing = 0;
363 for (i=1; i < chg_nr; i++) {
364 /* if <current_addr> > <last_addr>, swap */
365 /* or, if current=<start_addr> & last=<end_addr>, swap */
366 if ((change_point[i]->addr < change_point[i-1]->addr) ||
367 ((change_point[i]->addr == change_point[i-1]->addr) &&
368 (change_point[i]->addr == change_point[i]->pbios->addr) &&
369 (change_point[i-1]->addr != change_point[i-1]->pbios->addr))
372 change_tmp = change_point[i];
373 change_point[i] = change_point[i-1];
374 change_point[i-1] = change_tmp;
375 still_changing=1;
380 /* create a new bios memory map, removing overlaps */
381 overlap_entries=0; /* number of entries in the overlap table */
382 new_bios_entry=0; /* index for creating new bios map entries */
383 last_type = 0; /* start with undefined memory type */
384 last_addr = 0; /* start with 0 as last starting address */
385 /* loop through change-points, determining affect on the new bios map */
386 for (chgidx=0; chgidx < chg_nr; chgidx++)
388 /* keep track of all overlapping bios entries */
389 if (change_point[chgidx]->addr == change_point[chgidx]->pbios->addr)
391 /* add map entry to overlap list (> 1 entry implies an overlap) */
392 overlap_list[overlap_entries++]=change_point[chgidx]->pbios;
394 else
396 /* remove entry from list (order independent, so swap with last) */
397 for (i=0; i<overlap_entries; i++)
399 if (overlap_list[i] == change_point[chgidx]->pbios)
400 overlap_list[i] = overlap_list[overlap_entries-1];
402 overlap_entries--;
404 /* if there are overlapping entries, decide which "type" to use */
405 /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */
406 current_type = 0;
407 for (i=0; i<overlap_entries; i++)
408 if (overlap_list[i]->type > current_type)
409 current_type = overlap_list[i]->type;
410 /* continue building up new bios map based on this information */
411 if (current_type != last_type) {
412 if (last_type != 0) {
413 new_bios[new_bios_entry].size =
414 change_point[chgidx]->addr - last_addr;
415 /* move forward only if the new size was non-zero */
416 if (new_bios[new_bios_entry].size != 0)
417 if (++new_bios_entry >= E820MAX)
418 break; /* no more space left for new bios entries */
420 if (current_type != 0) {
421 new_bios[new_bios_entry].addr = change_point[chgidx]->addr;
422 new_bios[new_bios_entry].type = current_type;
423 last_addr=change_point[chgidx]->addr;
425 last_type = current_type;
428 new_nr = new_bios_entry; /* retain count for new bios entries */
430 /* copy new bios mapping into original location */
431 memcpy(biosmap, new_bios, new_nr*sizeof(struct e820entry));
432 *pnr_map = new_nr;
434 return 0;
438 * Copy the BIOS e820 map into a safe place.
440 * Sanity-check it while we're at it..
442 * If we're lucky and live on a modern system, the setup code
443 * will have given us a memory map that we can use to properly
444 * set up memory. If we aren't, we'll fake a memory map.
446 * We check to see that the memory map contains at least 2 elements
447 * before we'll use it, because the detection code in setup.S may
448 * not be perfect and most every PC known to man has two memory
449 * regions: one from 0 to 640k, and one from 1mb up. (The IBM
450 * thinkpad 560x, for example, does not cooperate with the memory
451 * detection code.)
453 int __init copy_e820_map(struct e820entry * biosmap, int nr_map)
455 /* Only one memory region (or negative)? Ignore it */
456 if (nr_map < 2)
457 return -1;
459 do {
460 unsigned long long start = biosmap->addr;
461 unsigned long long size = biosmap->size;
462 unsigned long long end = start + size;
463 unsigned long type = biosmap->type;
465 /* Overflow in 64 bits? Ignore the memory map. */
466 if (start > end)
467 return -1;
470 * Some BIOSes claim RAM in the 640k - 1M region.
471 * Not right. Fix it up.
473 if (type == E820_RAM) {
474 if (start < 0x100000ULL && end > 0xA0000ULL) {
475 if (start < 0xA0000ULL)
476 add_memory_region(start, 0xA0000ULL-start, type);
477 if (end <= 0x100000ULL)
478 continue;
479 start = 0x100000ULL;
480 size = end - start;
483 add_memory_region(start, size, type);
484 } while (biosmap++,--nr_map);
485 return 0;
489 * Find the highest page frame number we have available
491 void __init find_max_pfn(void)
493 int i;
495 max_pfn = 0;
497 for (i = 0; i < e820.nr_map; i++) {
498 unsigned long start, end;
499 /* RAM? */
500 if (e820.map[i].type != E820_RAM)
501 continue;
502 start = PFN_UP(e820.map[i].addr);
503 end = PFN_DOWN(e820.map[i].addr + e820.map[i].size);
504 if (start >= end)
505 continue;
506 if (end > max_pfn)
507 max_pfn = end;
508 memory_present(0, start, end);
513 * Register fully available low RAM pages with the bootmem allocator.
515 void __init register_bootmem_low_pages(unsigned long max_low_pfn)
517 int i;
519 for (i = 0; i < e820.nr_map; i++) {
520 unsigned long curr_pfn, last_pfn, size;
522 * Reserve usable low memory
524 if (e820.map[i].type != E820_RAM)
525 continue;
527 * We are rounding up the start address of usable memory:
529 curr_pfn = PFN_UP(e820.map[i].addr);
530 if (curr_pfn >= max_low_pfn)
531 continue;
533 * ... and at the end of the usable range downwards:
535 last_pfn = PFN_DOWN(e820.map[i].addr + e820.map[i].size);
537 if (last_pfn > max_low_pfn)
538 last_pfn = max_low_pfn;
541 * .. finally, did all the rounding and playing
542 * around just make the area go away?
544 if (last_pfn <= curr_pfn)
545 continue;
547 size = last_pfn - curr_pfn;
548 free_bootmem(PFN_PHYS(curr_pfn), PFN_PHYS(size));
552 void __init e820_register_memory(void)
554 unsigned long gapstart, gapsize, round;
555 unsigned long long last;
556 int i;
559 * Search for the biggest gap in the low 32 bits of the e820
560 * memory space.
562 last = 0x100000000ull;
563 gapstart = 0x10000000;
564 gapsize = 0x400000;
565 i = e820.nr_map;
566 while (--i >= 0) {
567 unsigned long long start = e820.map[i].addr;
568 unsigned long long end = start + e820.map[i].size;
571 * Since "last" is at most 4GB, we know we'll
572 * fit in 32 bits if this condition is true
574 if (last > end) {
575 unsigned long gap = last - end;
577 if (gap > gapsize) {
578 gapsize = gap;
579 gapstart = end;
582 if (start < last)
583 last = start;
587 * See how much we want to round up: start off with
588 * rounding to the next 1MB area.
590 round = 0x100000;
591 while ((gapsize >> 4) > round)
592 round += round;
593 /* Fun with two's complement */
594 pci_mem_start = (gapstart + round) & -round;
596 printk("Allocating PCI resources starting at %08lx (gap: %08lx:%08lx)\n",
597 pci_mem_start, gapstart, gapsize);
600 void __init print_memory_map(char *who)
602 int i;
604 for (i = 0; i < e820.nr_map; i++) {
605 printk(" %s: %016Lx - %016Lx ", who,
606 e820.map[i].addr,
607 e820.map[i].addr + e820.map[i].size);
608 switch (e820.map[i].type) {
609 case E820_RAM: printk("(usable)\n");
610 break;
611 case E820_RESERVED:
612 printk("(reserved)\n");
613 break;
614 case E820_ACPI:
615 printk("(ACPI data)\n");
616 break;
617 case E820_NVS:
618 printk("(ACPI NVS)\n");
619 break;
620 default: printk("type %u\n", e820.map[i].type);
621 break;
626 void __init limit_regions(unsigned long long size)
628 unsigned long long current_addr;
629 int i;
631 print_memory_map("limit_regions start");
632 for (i = 0; i < e820.nr_map; i++) {
633 current_addr = e820.map[i].addr + e820.map[i].size;
634 if (current_addr < size)
635 continue;
637 if (e820.map[i].type != E820_RAM)
638 continue;
640 if (e820.map[i].addr >= size) {
642 * This region starts past the end of the
643 * requested size, skip it completely.
645 e820.nr_map = i;
646 } else {
647 e820.nr_map = i + 1;
648 e820.map[i].size -= current_addr - size;
650 print_memory_map("limit_regions endfor");
651 return;
653 print_memory_map("limit_regions endfunc");
657 * This function checks if any part of the range <start,end> is mapped
658 * with type.
661 e820_any_mapped(u64 start, u64 end, unsigned type)
663 int i;
664 for (i = 0; i < e820.nr_map; i++) {
665 const struct e820entry *ei = &e820.map[i];
666 if (type && ei->type != type)
667 continue;
668 if (ei->addr >= end || ei->addr + ei->size <= start)
669 continue;
670 return 1;
672 return 0;
674 EXPORT_SYMBOL_GPL(e820_any_mapped);
677 * This function checks if the entire range <start,end> is mapped with type.
679 * Note: this function only works correct if the e820 table is sorted and
680 * not-overlapping, which is the case
682 int __init
683 e820_all_mapped(unsigned long s, unsigned long e, unsigned type)
685 u64 start = s;
686 u64 end = e;
687 int i;
688 for (i = 0; i < e820.nr_map; i++) {
689 struct e820entry *ei = &e820.map[i];
690 if (type && ei->type != type)
691 continue;
692 /* is the region (part) in overlap with the current region ?*/
693 if (ei->addr >= end || ei->addr + ei->size <= start)
694 continue;
695 /* if the region is at the beginning of <start,end> we move
696 * start to the end of the region since it's ok until there
698 if (ei->addr <= start)
699 start = ei->addr + ei->size;
700 /* if start is now at or beyond end, we're done, full
701 * coverage */
702 if (start >= end)
703 return 1; /* we're done */
705 return 0;
708 static int __init parse_memmap(char *arg)
710 if (!arg)
711 return -EINVAL;
713 if (strcmp(arg, "exactmap") == 0) {
714 #ifdef CONFIG_CRASH_DUMP
715 /* If we are doing a crash dump, we
716 * still need to know the real mem
717 * size before original memory map is
718 * reset.
720 find_max_pfn();
721 saved_max_pfn = max_pfn;
722 #endif
723 e820.nr_map = 0;
724 user_defined_memmap = 1;
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.
732 unsigned long long start_at, mem_size;
734 mem_size = memparse(arg, &arg);
735 if (*arg == '@') {
736 start_at = memparse(arg+1, &arg);
737 add_memory_region(start_at, mem_size, E820_RAM);
738 } else if (*arg == '#') {
739 start_at = memparse(arg+1, &arg);
740 add_memory_region(start_at, mem_size, E820_ACPI);
741 } else if (*arg == '$') {
742 start_at = memparse(arg+1, &arg);
743 add_memory_region(start_at, mem_size, E820_RESERVED);
744 } else {
745 limit_regions(mem_size);
746 user_defined_memmap = 1;
749 return 0;
751 early_param("memmap", parse_memmap);
752 void __init update_e820(void)
754 u8 nr_map;
756 nr_map = e820.nr_map;
757 if (sanitize_e820_map(e820.map, &nr_map))
758 return;
759 e820.nr_map = nr_map;
760 printk(KERN_INFO "modified physical RAM map:\n");
761 print_memory_map("modified");