aboutsummaryrefslogtreecommitdiffstats
path: root/arch/x86/kernel/e820_32.c
diff options
context:
space:
mode:
Diffstat (limited to 'arch/x86/kernel/e820_32.c')
-rw-r--r--arch/x86/kernel/e820_32.c411
1 files changed, 5 insertions, 406 deletions
diff --git a/arch/x86/kernel/e820_32.c b/arch/x86/kernel/e820_32.c
index 751d517d802c..dfe25751038b 100644
--- a/arch/x86/kernel/e820_32.c
+++ b/arch/x86/kernel/e820_32.c
@@ -16,21 +16,6 @@
16#include <asm/e820.h> 16#include <asm/e820.h>
17#include <asm/setup.h> 17#include <asm/setup.h>
18 18
19struct e820map e820;
20struct change_member {
21 struct e820entry *pbios; /* pointer to original bios entry */
22 unsigned long long addr; /* address for this change point */
23};
24static struct change_member change_point_list[2*E820MAX] __initdata;
25static struct change_member *change_point[2*E820MAX] __initdata;
26static struct e820entry *overlap_list[E820MAX] __initdata;
27static struct e820entry new_bios[E820MAX] __initdata;
28/* For PCI or other memory-mapped resources */
29unsigned long pci_mem_start = 0x10000000;
30#ifdef CONFIG_PCI
31EXPORT_SYMBOL(pci_mem_start);
32#endif
33
34static struct resource system_rom_resource = { 19static struct resource system_rom_resource = {
35 .name = "System ROM", 20 .name = "System ROM",
36 .start = 0xf0000, 21 .start = 0xf0000,
@@ -254,223 +239,6 @@ void __init e820_mark_nosave_regions(void)
254} 239}
255#endif 240#endif
256 241
257void __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 */
282int __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 */
452int __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/* 242/*
475 * Find the highest page frame number we have available 243 * Find the highest page frame number we have available
476 */ 244 */
@@ -535,86 +303,12 @@ void __init register_bootmem_low_pages(unsigned long max_low_pfn)
535 } 303 }
536} 304}
537 305
538void __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
586static 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
612void __init limit_regions(unsigned long long size) 306void __init limit_regions(unsigned long long size)
613{ 307{
614 unsigned long long current_addr; 308 unsigned long long current_addr;
615 int i; 309 int i;
616 310
617 print_memory_map("limit_regions start"); 311 e820_print_map("limit_regions start");
618 for (i = 0; i < e820.nr_map; i++) { 312 for (i = 0; i < e820.nr_map; i++) {
619 current_addr = e820.map[i].addr + e820.map[i].size; 313 current_addr = e820.map[i].addr + e820.map[i].size;
620 if (current_addr < size) 314 if (current_addr < size)
@@ -633,62 +327,10 @@ void __init limit_regions(unsigned long long size)
633 e820.nr_map = i + 1; 327 e820.nr_map = i + 1;
634 e820.map[i].size -= current_addr - size; 328 e820.map[i].size -= current_addr - size;
635 } 329 }
636 print_memory_map("limit_regions endfor"); 330 e820_print_map("limit_regions endfor");
637 return; 331 return;
638 } 332 }
639 print_memory_map("limit_regions endfunc"); 333 e820_print_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 */
646int
647e820_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}
660EXPORT_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 */
668int __init
669e820_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} 334}
693 335
694/* Overridden in paravirt.c if CONFIG_PARAVIRT */ 336/* Overridden in paravirt.c if CONFIG_PARAVIRT */
@@ -700,7 +342,7 @@ char * __init __attribute__((weak)) memory_setup(void)
700void __init setup_memory_map(void) 342void __init setup_memory_map(void)
701{ 343{
702 printk(KERN_INFO "BIOS-provided physical RAM map:\n"); 344 printk(KERN_INFO "BIOS-provided physical RAM map:\n");
703 print_memory_map(memory_setup()); 345 e820_print_map(memory_setup());
704} 346}
705 347
706static int __initdata user_defined_memmap; 348static int __initdata user_defined_memmap;
@@ -783,55 +425,12 @@ static int __init parse_memmap(char *arg)
783 return 0; 425 return 0;
784} 426}
785early_param("memmap", parse_memmap); 427early_param("memmap", parse_memmap);
786u64 __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 428
819void __init finish_e820_parsing(void) 429void __init finish_e820_parsing(void)
820{ 430{
821 if (user_defined_memmap) { 431 if (user_defined_memmap) {
822 printk(KERN_INFO "user-defined physical RAM map:\n"); 432 printk(KERN_INFO "user-defined physical RAM map:\n");
823 print_memory_map("user"); 433 e820_print_map("user");
824 } 434 }
825} 435}
826 436
827void __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}