diff options
Diffstat (limited to 'arch/x86/kernel/e820.c')
-rw-r--r-- | arch/x86/kernel/e820.c | 370 |
1 files changed, 58 insertions, 312 deletions
diff --git a/arch/x86/kernel/e820.c b/arch/x86/kernel/e820.c index d17d482a04f4..7bca3c6a02fb 100644 --- a/arch/x86/kernel/e820.c +++ b/arch/x86/kernel/e820.c | |||
@@ -12,21 +12,13 @@ | |||
12 | #include <linux/types.h> | 12 | #include <linux/types.h> |
13 | #include <linux/init.h> | 13 | #include <linux/init.h> |
14 | #include <linux/bootmem.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/pfn.h> | 15 | #include <linux/pfn.h> |
21 | #include <linux/suspend.h> | 16 | #include <linux/suspend.h> |
22 | #include <linux/firmware-map.h> | 17 | #include <linux/firmware-map.h> |
23 | 18 | ||
24 | #include <asm/pgtable.h> | ||
25 | #include <asm/page.h> | ||
26 | #include <asm/e820.h> | 19 | #include <asm/e820.h> |
27 | #include <asm/proto.h> | 20 | #include <asm/proto.h> |
28 | #include <asm/setup.h> | 21 | #include <asm/setup.h> |
29 | #include <asm/trampoline.h> | ||
30 | 22 | ||
31 | /* | 23 | /* |
32 | * The e820 map is the map that gets modified e.g. with command line parameters | 24 | * The e820 map is the map that gets modified e.g. with command line parameters |
@@ -517,31 +509,55 @@ u64 __init e820_remove_range(u64 start, u64 size, unsigned old_type, | |||
517 | int checktype) | 509 | int checktype) |
518 | { | 510 | { |
519 | int i; | 511 | int i; |
512 | u64 end; | ||
520 | u64 real_removed_size = 0; | 513 | u64 real_removed_size = 0; |
521 | 514 | ||
522 | if (size > (ULLONG_MAX - start)) | 515 | if (size > (ULLONG_MAX - start)) |
523 | size = ULLONG_MAX - start; | 516 | size = ULLONG_MAX - start; |
524 | 517 | ||
518 | end = start + size; | ||
519 | printk(KERN_DEBUG "e820 remove range: %016Lx - %016Lx ", | ||
520 | (unsigned long long) start, | ||
521 | (unsigned long long) end); | ||
522 | if (checktype) | ||
523 | e820_print_type(old_type); | ||
524 | printk(KERN_CONT "\n"); | ||
525 | |||
525 | for (i = 0; i < e820.nr_map; i++) { | 526 | for (i = 0; i < e820.nr_map; i++) { |
526 | struct e820entry *ei = &e820.map[i]; | 527 | struct e820entry *ei = &e820.map[i]; |
527 | u64 final_start, final_end; | 528 | u64 final_start, final_end; |
529 | u64 ei_end; | ||
528 | 530 | ||
529 | if (checktype && ei->type != old_type) | 531 | if (checktype && ei->type != old_type) |
530 | continue; | 532 | continue; |
533 | |||
534 | ei_end = ei->addr + ei->size; | ||
531 | /* totally covered? */ | 535 | /* totally covered? */ |
532 | if (ei->addr >= start && | 536 | if (ei->addr >= start && ei_end <= end) { |
533 | (ei->addr + ei->size) <= (start + size)) { | ||
534 | real_removed_size += ei->size; | 537 | real_removed_size += ei->size; |
535 | memset(ei, 0, sizeof(struct e820entry)); | 538 | memset(ei, 0, sizeof(struct e820entry)); |
536 | continue; | 539 | continue; |
537 | } | 540 | } |
541 | |||
542 | /* new range is totally covered? */ | ||
543 | if (ei->addr < start && ei_end > end) { | ||
544 | e820_add_region(end, ei_end - end, ei->type); | ||
545 | ei->size = start - ei->addr; | ||
546 | real_removed_size += size; | ||
547 | continue; | ||
548 | } | ||
549 | |||
538 | /* partially covered */ | 550 | /* partially covered */ |
539 | final_start = max(start, ei->addr); | 551 | final_start = max(start, ei->addr); |
540 | final_end = min(start + size, ei->addr + ei->size); | 552 | final_end = min(end, ei_end); |
541 | if (final_start >= final_end) | 553 | if (final_start >= final_end) |
542 | continue; | 554 | continue; |
543 | real_removed_size += final_end - final_start; | 555 | real_removed_size += final_end - final_start; |
544 | 556 | ||
557 | /* | ||
558 | * left range could be head or tail, so need to update | ||
559 | * size at first. | ||
560 | */ | ||
545 | ei->size -= final_end - final_start; | 561 | ei->size -= final_end - final_start; |
546 | if (ei->addr < final_start) | 562 | if (ei->addr < final_start) |
547 | continue; | 563 | continue; |
@@ -722,310 +738,44 @@ core_initcall(e820_mark_nvs_memory); | |||
722 | #endif | 738 | #endif |
723 | 739 | ||
724 | /* | 740 | /* |
725 | * Early reserved memory areas. | 741 | * Find a free area with specified alignment in a specific range. |
726 | */ | ||
727 | #define MAX_EARLY_RES 20 | ||
728 | |||
729 | struct early_res { | ||
730 | u64 start, end; | ||
731 | char name[16]; | ||
732 | char overlap_ok; | ||
733 | }; | ||
734 | static struct early_res early_res[MAX_EARLY_RES] __initdata = { | ||
735 | { 0, PAGE_SIZE, "BIOS data page" }, /* BIOS data page */ | ||
736 | {} | ||
737 | }; | ||
738 | |||
739 | static int __init find_overlapped_early(u64 start, u64 end) | ||
740 | { | ||
741 | int i; | ||
742 | struct early_res *r; | ||
743 | |||
744 | for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) { | ||
745 | r = &early_res[i]; | ||
746 | if (end > r->start && start < r->end) | ||
747 | break; | ||
748 | } | ||
749 | |||
750 | return i; | ||
751 | } | ||
752 | |||
753 | /* | ||
754 | * Drop the i-th range from the early reservation map, | ||
755 | * by copying any higher ranges down one over it, and | ||
756 | * clearing what had been the last slot. | ||
757 | */ | ||
758 | static void __init drop_range(int i) | ||
759 | { | ||
760 | int j; | ||
761 | |||
762 | for (j = i + 1; j < MAX_EARLY_RES && early_res[j].end; j++) | ||
763 | ; | ||
764 | |||
765 | memmove(&early_res[i], &early_res[i + 1], | ||
766 | (j - 1 - i) * sizeof(struct early_res)); | ||
767 | |||
768 | early_res[j - 1].end = 0; | ||
769 | } | ||
770 | |||
771 | /* | ||
772 | * Split any existing ranges that: | ||
773 | * 1) are marked 'overlap_ok', and | ||
774 | * 2) overlap with the stated range [start, end) | ||
775 | * into whatever portion (if any) of the existing range is entirely | ||
776 | * below or entirely above the stated range. Drop the portion | ||
777 | * of the existing range that overlaps with the stated range, | ||
778 | * which will allow the caller of this routine to then add that | ||
779 | * stated range without conflicting with any existing range. | ||
780 | */ | 742 | */ |
781 | static void __init drop_overlaps_that_are_ok(u64 start, u64 end) | 743 | u64 __init find_e820_area(u64 start, u64 end, u64 size, u64 align) |
782 | { | 744 | { |
783 | int i; | 745 | int i; |
784 | struct early_res *r; | ||
785 | u64 lower_start, lower_end; | ||
786 | u64 upper_start, upper_end; | ||
787 | char name[16]; | ||
788 | 746 | ||
789 | for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) { | 747 | for (i = 0; i < e820.nr_map; i++) { |
790 | r = &early_res[i]; | 748 | struct e820entry *ei = &e820.map[i]; |
749 | u64 addr; | ||
750 | u64 ei_start, ei_last; | ||
791 | 751 | ||
792 | /* Continue past non-overlapping ranges */ | 752 | if (ei->type != E820_RAM) |
793 | if (end <= r->start || start >= r->end) | ||
794 | continue; | 753 | continue; |
795 | 754 | ||
796 | /* | 755 | ei_last = ei->addr + ei->size; |
797 | * Leave non-ok overlaps as is; let caller | 756 | ei_start = ei->addr; |
798 | * panic "Overlapping early reservations" | 757 | addr = find_early_area(ei_start, ei_last, start, end, |
799 | * when it hits this overlap. | 758 | size, align); |
800 | */ | ||
801 | if (!r->overlap_ok) | ||
802 | return; | ||
803 | |||
804 | /* | ||
805 | * We have an ok overlap. We will drop it from the early | ||
806 | * reservation map, and add back in any non-overlapping | ||
807 | * portions (lower or upper) as separate, overlap_ok, | ||
808 | * non-overlapping ranges. | ||
809 | */ | ||
810 | |||
811 | /* 1. Note any non-overlapping (lower or upper) ranges. */ | ||
812 | strncpy(name, r->name, sizeof(name) - 1); | ||
813 | |||
814 | lower_start = lower_end = 0; | ||
815 | upper_start = upper_end = 0; | ||
816 | if (r->start < start) { | ||
817 | lower_start = r->start; | ||
818 | lower_end = start; | ||
819 | } | ||
820 | if (r->end > end) { | ||
821 | upper_start = end; | ||
822 | upper_end = r->end; | ||
823 | } | ||
824 | |||
825 | /* 2. Drop the original ok overlapping range */ | ||
826 | drop_range(i); | ||
827 | |||
828 | i--; /* resume for-loop on copied down entry */ | ||
829 | |||
830 | /* 3. Add back in any non-overlapping ranges. */ | ||
831 | if (lower_end) | ||
832 | reserve_early_overlap_ok(lower_start, lower_end, name); | ||
833 | if (upper_end) | ||
834 | reserve_early_overlap_ok(upper_start, upper_end, name); | ||
835 | } | ||
836 | } | ||
837 | |||
838 | static void __init __reserve_early(u64 start, u64 end, char *name, | ||
839 | int overlap_ok) | ||
840 | { | ||
841 | int i; | ||
842 | struct early_res *r; | ||
843 | |||
844 | i = find_overlapped_early(start, end); | ||
845 | if (i >= MAX_EARLY_RES) | ||
846 | panic("Too many early reservations"); | ||
847 | r = &early_res[i]; | ||
848 | if (r->end) | ||
849 | panic("Overlapping early reservations " | ||
850 | "%llx-%llx %s to %llx-%llx %s\n", | ||
851 | start, end - 1, name?name:"", r->start, | ||
852 | r->end - 1, r->name); | ||
853 | r->start = start; | ||
854 | r->end = end; | ||
855 | r->overlap_ok = overlap_ok; | ||
856 | if (name) | ||
857 | strncpy(r->name, name, sizeof(r->name) - 1); | ||
858 | } | ||
859 | |||
860 | /* | ||
861 | * A few early reservtations come here. | ||
862 | * | ||
863 | * The 'overlap_ok' in the name of this routine does -not- mean it | ||
864 | * is ok for these reservations to overlap an earlier reservation. | ||
865 | * Rather it means that it is ok for subsequent reservations to | ||
866 | * overlap this one. | ||
867 | * | ||
868 | * Use this entry point to reserve early ranges when you are doing | ||
869 | * so out of "Paranoia", reserving perhaps more memory than you need, | ||
870 | * just in case, and don't mind a subsequent overlapping reservation | ||
871 | * that is known to be needed. | ||
872 | * | ||
873 | * The drop_overlaps_that_are_ok() call here isn't really needed. | ||
874 | * It would be needed if we had two colliding 'overlap_ok' | ||
875 | * reservations, so that the second such would not panic on the | ||
876 | * overlap with the first. We don't have any such as of this | ||
877 | * writing, but might as well tolerate such if it happens in | ||
878 | * the future. | ||
879 | */ | ||
880 | void __init reserve_early_overlap_ok(u64 start, u64 end, char *name) | ||
881 | { | ||
882 | drop_overlaps_that_are_ok(start, end); | ||
883 | __reserve_early(start, end, name, 1); | ||
884 | } | ||
885 | |||
886 | /* | ||
887 | * Most early reservations come here. | ||
888 | * | ||
889 | * We first have drop_overlaps_that_are_ok() drop any pre-existing | ||
890 | * 'overlap_ok' ranges, so that we can then reserve this memory | ||
891 | * range without risk of panic'ing on an overlapping overlap_ok | ||
892 | * early reservation. | ||
893 | */ | ||
894 | void __init reserve_early(u64 start, u64 end, char *name) | ||
895 | { | ||
896 | if (start >= end) | ||
897 | return; | ||
898 | |||
899 | drop_overlaps_that_are_ok(start, end); | ||
900 | __reserve_early(start, end, name, 0); | ||
901 | } | ||
902 | |||
903 | void __init free_early(u64 start, u64 end) | ||
904 | { | ||
905 | struct early_res *r; | ||
906 | int i; | ||
907 | |||
908 | i = find_overlapped_early(start, end); | ||
909 | r = &early_res[i]; | ||
910 | if (i >= MAX_EARLY_RES || r->end != end || r->start != start) | ||
911 | panic("free_early on not reserved area: %llx-%llx!", | ||
912 | start, end - 1); | ||
913 | |||
914 | drop_range(i); | ||
915 | } | ||
916 | 759 | ||
917 | void __init early_res_to_bootmem(u64 start, u64 end) | 760 | if (addr != -1ULL) |
918 | { | 761 | return addr; |
919 | int i, count; | ||
920 | u64 final_start, final_end; | ||
921 | |||
922 | count = 0; | ||
923 | for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) | ||
924 | count++; | ||
925 | |||
926 | printk(KERN_INFO "(%d early reservations) ==> bootmem [%010llx - %010llx]\n", | ||
927 | count, start, end); | ||
928 | for (i = 0; i < count; i++) { | ||
929 | struct early_res *r = &early_res[i]; | ||
930 | printk(KERN_INFO " #%d [%010llx - %010llx] %16s", i, | ||
931 | r->start, r->end, r->name); | ||
932 | final_start = max(start, r->start); | ||
933 | final_end = min(end, r->end); | ||
934 | if (final_start >= final_end) { | ||
935 | printk(KERN_CONT "\n"); | ||
936 | continue; | ||
937 | } | ||
938 | printk(KERN_CONT " ==> [%010llx - %010llx]\n", | ||
939 | final_start, final_end); | ||
940 | reserve_bootmem_generic(final_start, final_end - final_start, | ||
941 | BOOTMEM_DEFAULT); | ||
942 | } | 762 | } |
763 | return -1ULL; | ||
943 | } | 764 | } |
944 | 765 | ||
945 | /* Check for already reserved areas */ | 766 | u64 __init find_fw_memmap_area(u64 start, u64 end, u64 size, u64 align) |
946 | static inline int __init bad_addr(u64 *addrp, u64 size, u64 align) | ||
947 | { | ||
948 | int i; | ||
949 | u64 addr = *addrp; | ||
950 | int changed = 0; | ||
951 | struct early_res *r; | ||
952 | again: | ||
953 | i = find_overlapped_early(addr, addr + size); | ||
954 | r = &early_res[i]; | ||
955 | if (i < MAX_EARLY_RES && r->end) { | ||
956 | *addrp = addr = round_up(r->end, align); | ||
957 | changed = 1; | ||
958 | goto again; | ||
959 | } | ||
960 | return changed; | ||
961 | } | ||
962 | |||
963 | /* Check for already reserved areas */ | ||
964 | static inline int __init bad_addr_size(u64 *addrp, u64 *sizep, u64 align) | ||
965 | { | 767 | { |
966 | int i; | 768 | return find_e820_area(start, end, size, align); |
967 | u64 addr = *addrp, last; | ||
968 | u64 size = *sizep; | ||
969 | int changed = 0; | ||
970 | again: | ||
971 | last = addr + size; | ||
972 | for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) { | ||
973 | struct early_res *r = &early_res[i]; | ||
974 | if (last > r->start && addr < r->start) { | ||
975 | size = r->start - addr; | ||
976 | changed = 1; | ||
977 | goto again; | ||
978 | } | ||
979 | if (last > r->end && addr < r->end) { | ||
980 | addr = round_up(r->end, align); | ||
981 | size = last - addr; | ||
982 | changed = 1; | ||
983 | goto again; | ||
984 | } | ||
985 | if (last <= r->end && addr >= r->start) { | ||
986 | (*sizep)++; | ||
987 | return 0; | ||
988 | } | ||
989 | } | ||
990 | if (changed) { | ||
991 | *addrp = addr; | ||
992 | *sizep = size; | ||
993 | } | ||
994 | return changed; | ||
995 | } | 769 | } |
996 | 770 | ||
997 | /* | 771 | u64 __init get_max_mapped(void) |
998 | * Find a free area with specified alignment in a specific range. | ||
999 | */ | ||
1000 | u64 __init find_e820_area(u64 start, u64 end, u64 size, u64 align) | ||
1001 | { | 772 | { |
1002 | int i; | 773 | u64 end = max_pfn_mapped; |
1003 | 774 | ||
1004 | for (i = 0; i < e820.nr_map; i++) { | 775 | end <<= PAGE_SHIFT; |
1005 | struct e820entry *ei = &e820.map[i]; | ||
1006 | u64 addr, last; | ||
1007 | u64 ei_last; | ||
1008 | 776 | ||
1009 | if (ei->type != E820_RAM) | 777 | return end; |
1010 | continue; | ||
1011 | addr = round_up(ei->addr, align); | ||
1012 | ei_last = ei->addr + ei->size; | ||
1013 | if (addr < start) | ||
1014 | addr = round_up(start, align); | ||
1015 | if (addr >= ei_last) | ||
1016 | continue; | ||
1017 | while (bad_addr(&addr, size, align) && addr+size <= ei_last) | ||
1018 | ; | ||
1019 | last = addr + size; | ||
1020 | if (last > ei_last) | ||
1021 | continue; | ||
1022 | if (last > end) | ||
1023 | continue; | ||
1024 | return addr; | ||
1025 | } | ||
1026 | return -1ULL; | ||
1027 | } | 778 | } |
1028 | |||
1029 | /* | 779 | /* |
1030 | * Find next free range after *start | 780 | * Find next free range after *start |
1031 | */ | 781 | */ |
@@ -1035,25 +785,19 @@ u64 __init find_e820_area_size(u64 start, u64 *sizep, u64 align) | |||
1035 | 785 | ||
1036 | for (i = 0; i < e820.nr_map; i++) { | 786 | for (i = 0; i < e820.nr_map; i++) { |
1037 | struct e820entry *ei = &e820.map[i]; | 787 | struct e820entry *ei = &e820.map[i]; |
1038 | u64 addr, last; | 788 | u64 addr; |
1039 | u64 ei_last; | 789 | u64 ei_start, ei_last; |
1040 | 790 | ||
1041 | if (ei->type != E820_RAM) | 791 | if (ei->type != E820_RAM) |
1042 | continue; | 792 | continue; |
1043 | addr = round_up(ei->addr, align); | 793 | |
1044 | ei_last = ei->addr + ei->size; | 794 | ei_last = ei->addr + ei->size; |
1045 | if (addr < start) | 795 | ei_start = ei->addr; |
1046 | addr = round_up(start, align); | 796 | addr = find_early_area_size(ei_start, ei_last, start, |
1047 | if (addr >= ei_last) | 797 | sizep, align); |
1048 | continue; | 798 | |
1049 | *sizep = ei_last - addr; | 799 | if (addr != -1ULL) |
1050 | while (bad_addr_size(&addr, sizep, align) && | 800 | return addr; |
1051 | addr + *sizep <= ei_last) | ||
1052 | ; | ||
1053 | last = addr + *sizep; | ||
1054 | if (last > ei_last) | ||
1055 | continue; | ||
1056 | return addr; | ||
1057 | } | 801 | } |
1058 | 802 | ||
1059 | return -1ULL; | 803 | return -1ULL; |
@@ -1412,6 +1156,8 @@ void __init e820_reserve_resources_late(void) | |||
1412 | end = MAX_RESOURCE_SIZE; | 1156 | end = MAX_RESOURCE_SIZE; |
1413 | if (start >= end) | 1157 | if (start >= end) |
1414 | continue; | 1158 | continue; |
1159 | printk(KERN_DEBUG "reserve RAM buffer: %016llx - %016llx ", | ||
1160 | start, end); | ||
1415 | reserve_region_with_split(&iomem_resource, start, end, | 1161 | reserve_region_with_split(&iomem_resource, start, end, |
1416 | "RAM buffer"); | 1162 | "RAM buffer"); |
1417 | } | 1163 | } |