diff options
Diffstat (limited to 'arch/x86/kernel/e820.c')
-rw-r--r-- | arch/x86/kernel/e820.c | 541 |
1 files changed, 1 insertions, 540 deletions
diff --git a/arch/x86/kernel/e820.c b/arch/x86/kernel/e820.c index 4004f10285d1..82db4015604e 100644 --- a/arch/x86/kernel/e820.c +++ b/arch/x86/kernel/e820.c | |||
@@ -12,21 +12,14 @@ | |||
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> |
20 | #include <asm/early_res.h> | ||
27 | #include <asm/proto.h> | 21 | #include <asm/proto.h> |
28 | #include <asm/setup.h> | 22 | #include <asm/setup.h> |
29 | #include <asm/trampoline.h> | ||
30 | 23 | ||
31 | /* | 24 | /* |
32 | * The e820 map is the map that gets modified e.g. with command line parameters | 25 | * The e820 map is the map that gets modified e.g. with command line parameters |
@@ -730,538 +723,6 @@ core_initcall(e820_mark_nvs_memory); | |||
730 | #endif | 723 | #endif |
731 | 724 | ||
732 | /* | 725 | /* |
733 | * Early reserved memory areas. | ||
734 | */ | ||
735 | /* | ||
736 | * need to make sure this one is bigger enough before | ||
737 | * find_e820_area could be used | ||
738 | */ | ||
739 | #define MAX_EARLY_RES_X 32 | ||
740 | |||
741 | struct early_res { | ||
742 | u64 start, end; | ||
743 | char name[15]; | ||
744 | char overlap_ok; | ||
745 | }; | ||
746 | static struct early_res early_res_x[MAX_EARLY_RES_X] __initdata; | ||
747 | |||
748 | static int max_early_res __initdata = MAX_EARLY_RES_X; | ||
749 | static struct early_res *early_res __initdata = &early_res_x[0]; | ||
750 | static int early_res_count __initdata; | ||
751 | |||
752 | static int __init find_overlapped_early(u64 start, u64 end) | ||
753 | { | ||
754 | int i; | ||
755 | struct early_res *r; | ||
756 | |||
757 | for (i = 0; i < max_early_res && early_res[i].end; i++) { | ||
758 | r = &early_res[i]; | ||
759 | if (end > r->start && start < r->end) | ||
760 | break; | ||
761 | } | ||
762 | |||
763 | return i; | ||
764 | } | ||
765 | |||
766 | /* | ||
767 | * Drop the i-th range from the early reservation map, | ||
768 | * by copying any higher ranges down one over it, and | ||
769 | * clearing what had been the last slot. | ||
770 | */ | ||
771 | static void __init drop_range(int i) | ||
772 | { | ||
773 | int j; | ||
774 | |||
775 | for (j = i + 1; j < max_early_res && early_res[j].end; j++) | ||
776 | ; | ||
777 | |||
778 | memmove(&early_res[i], &early_res[i + 1], | ||
779 | (j - 1 - i) * sizeof(struct early_res)); | ||
780 | |||
781 | early_res[j - 1].end = 0; | ||
782 | early_res_count--; | ||
783 | } | ||
784 | |||
785 | /* | ||
786 | * Split any existing ranges that: | ||
787 | * 1) are marked 'overlap_ok', and | ||
788 | * 2) overlap with the stated range [start, end) | ||
789 | * into whatever portion (if any) of the existing range is entirely | ||
790 | * below or entirely above the stated range. Drop the portion | ||
791 | * of the existing range that overlaps with the stated range, | ||
792 | * which will allow the caller of this routine to then add that | ||
793 | * stated range without conflicting with any existing range. | ||
794 | */ | ||
795 | static void __init drop_overlaps_that_are_ok(u64 start, u64 end) | ||
796 | { | ||
797 | int i; | ||
798 | struct early_res *r; | ||
799 | u64 lower_start, lower_end; | ||
800 | u64 upper_start, upper_end; | ||
801 | char name[15]; | ||
802 | |||
803 | for (i = 0; i < max_early_res && early_res[i].end; i++) { | ||
804 | r = &early_res[i]; | ||
805 | |||
806 | /* Continue past non-overlapping ranges */ | ||
807 | if (end <= r->start || start >= r->end) | ||
808 | continue; | ||
809 | |||
810 | /* | ||
811 | * Leave non-ok overlaps as is; let caller | ||
812 | * panic "Overlapping early reservations" | ||
813 | * when it hits this overlap. | ||
814 | */ | ||
815 | if (!r->overlap_ok) | ||
816 | return; | ||
817 | |||
818 | /* | ||
819 | * We have an ok overlap. We will drop it from the early | ||
820 | * reservation map, and add back in any non-overlapping | ||
821 | * portions (lower or upper) as separate, overlap_ok, | ||
822 | * non-overlapping ranges. | ||
823 | */ | ||
824 | |||
825 | /* 1. Note any non-overlapping (lower or upper) ranges. */ | ||
826 | strncpy(name, r->name, sizeof(name) - 1); | ||
827 | |||
828 | lower_start = lower_end = 0; | ||
829 | upper_start = upper_end = 0; | ||
830 | if (r->start < start) { | ||
831 | lower_start = r->start; | ||
832 | lower_end = start; | ||
833 | } | ||
834 | if (r->end > end) { | ||
835 | upper_start = end; | ||
836 | upper_end = r->end; | ||
837 | } | ||
838 | |||
839 | /* 2. Drop the original ok overlapping range */ | ||
840 | drop_range(i); | ||
841 | |||
842 | i--; /* resume for-loop on copied down entry */ | ||
843 | |||
844 | /* 3. Add back in any non-overlapping ranges. */ | ||
845 | if (lower_end) | ||
846 | reserve_early_overlap_ok(lower_start, lower_end, name); | ||
847 | if (upper_end) | ||
848 | reserve_early_overlap_ok(upper_start, upper_end, name); | ||
849 | } | ||
850 | } | ||
851 | |||
852 | static void __init __reserve_early(u64 start, u64 end, char *name, | ||
853 | int overlap_ok) | ||
854 | { | ||
855 | int i; | ||
856 | struct early_res *r; | ||
857 | |||
858 | i = find_overlapped_early(start, end); | ||
859 | if (i >= max_early_res) | ||
860 | panic("Too many early reservations"); | ||
861 | r = &early_res[i]; | ||
862 | if (r->end) | ||
863 | panic("Overlapping early reservations " | ||
864 | "%llx-%llx %s to %llx-%llx %s\n", | ||
865 | start, end - 1, name?name:"", r->start, | ||
866 | r->end - 1, r->name); | ||
867 | r->start = start; | ||
868 | r->end = end; | ||
869 | r->overlap_ok = overlap_ok; | ||
870 | if (name) | ||
871 | strncpy(r->name, name, sizeof(r->name) - 1); | ||
872 | early_res_count++; | ||
873 | } | ||
874 | |||
875 | /* | ||
876 | * A few early reservtations come here. | ||
877 | * | ||
878 | * The 'overlap_ok' in the name of this routine does -not- mean it | ||
879 | * is ok for these reservations to overlap an earlier reservation. | ||
880 | * Rather it means that it is ok for subsequent reservations to | ||
881 | * overlap this one. | ||
882 | * | ||
883 | * Use this entry point to reserve early ranges when you are doing | ||
884 | * so out of "Paranoia", reserving perhaps more memory than you need, | ||
885 | * just in case, and don't mind a subsequent overlapping reservation | ||
886 | * that is known to be needed. | ||
887 | * | ||
888 | * The drop_overlaps_that_are_ok() call here isn't really needed. | ||
889 | * It would be needed if we had two colliding 'overlap_ok' | ||
890 | * reservations, so that the second such would not panic on the | ||
891 | * overlap with the first. We don't have any such as of this | ||
892 | * writing, but might as well tolerate such if it happens in | ||
893 | * the future. | ||
894 | */ | ||
895 | void __init reserve_early_overlap_ok(u64 start, u64 end, char *name) | ||
896 | { | ||
897 | drop_overlaps_that_are_ok(start, end); | ||
898 | __reserve_early(start, end, name, 1); | ||
899 | } | ||
900 | |||
901 | static void __init __check_and_double_early_res(u64 start) | ||
902 | { | ||
903 | u64 end, size, mem; | ||
904 | struct early_res *new; | ||
905 | |||
906 | /* do we have enough slots left ? */ | ||
907 | if ((max_early_res - early_res_count) > max(max_early_res/8, 2)) | ||
908 | return; | ||
909 | |||
910 | /* double it */ | ||
911 | end = max_pfn_mapped << PAGE_SHIFT; | ||
912 | size = sizeof(struct early_res) * max_early_res * 2; | ||
913 | mem = find_e820_area(start, end, size, sizeof(struct early_res)); | ||
914 | |||
915 | if (mem == -1ULL) | ||
916 | panic("can not find more space for early_res array"); | ||
917 | |||
918 | new = __va(mem); | ||
919 | /* save the first one for own */ | ||
920 | new[0].start = mem; | ||
921 | new[0].end = mem + size; | ||
922 | new[0].overlap_ok = 0; | ||
923 | /* copy old to new */ | ||
924 | if (early_res == early_res_x) { | ||
925 | memcpy(&new[1], &early_res[0], | ||
926 | sizeof(struct early_res) * max_early_res); | ||
927 | memset(&new[max_early_res+1], 0, | ||
928 | sizeof(struct early_res) * (max_early_res - 1)); | ||
929 | early_res_count++; | ||
930 | } else { | ||
931 | memcpy(&new[1], &early_res[1], | ||
932 | sizeof(struct early_res) * (max_early_res - 1)); | ||
933 | memset(&new[max_early_res], 0, | ||
934 | sizeof(struct early_res) * max_early_res); | ||
935 | } | ||
936 | memset(&early_res[0], 0, sizeof(struct early_res) * max_early_res); | ||
937 | early_res = new; | ||
938 | max_early_res *= 2; | ||
939 | printk(KERN_DEBUG "early_res array is doubled to %d at [%llx - %llx]\n", | ||
940 | max_early_res, mem, mem + size - 1); | ||
941 | } | ||
942 | |||
943 | /* | ||
944 | * Most early reservations come here. | ||
945 | * | ||
946 | * We first have drop_overlaps_that_are_ok() drop any pre-existing | ||
947 | * 'overlap_ok' ranges, so that we can then reserve this memory | ||
948 | * range without risk of panic'ing on an overlapping overlap_ok | ||
949 | * early reservation. | ||
950 | */ | ||
951 | void __init reserve_early(u64 start, u64 end, char *name) | ||
952 | { | ||
953 | if (start >= end) | ||
954 | return; | ||
955 | |||
956 | __check_and_double_early_res(end); | ||
957 | |||
958 | drop_overlaps_that_are_ok(start, end); | ||
959 | __reserve_early(start, end, name, 0); | ||
960 | } | ||
961 | |||
962 | void __init reserve_early_without_check(u64 start, u64 end, char *name) | ||
963 | { | ||
964 | struct early_res *r; | ||
965 | |||
966 | if (start >= end) | ||
967 | return; | ||
968 | |||
969 | __check_and_double_early_res(end); | ||
970 | |||
971 | r = &early_res[early_res_count]; | ||
972 | |||
973 | r->start = start; | ||
974 | r->end = end; | ||
975 | r->overlap_ok = 0; | ||
976 | if (name) | ||
977 | strncpy(r->name, name, sizeof(r->name) - 1); | ||
978 | early_res_count++; | ||
979 | } | ||
980 | |||
981 | void __init free_early(u64 start, u64 end) | ||
982 | { | ||
983 | struct early_res *r; | ||
984 | int i; | ||
985 | |||
986 | i = find_overlapped_early(start, end); | ||
987 | r = &early_res[i]; | ||
988 | if (i >= max_early_res || r->end != end || r->start != start) | ||
989 | panic("free_early on not reserved area: %llx-%llx!", | ||
990 | start, end - 1); | ||
991 | |||
992 | drop_range(i); | ||
993 | } | ||
994 | |||
995 | #ifdef CONFIG_NO_BOOTMEM | ||
996 | static void __init subtract_early_res(struct range *range, int az) | ||
997 | { | ||
998 | int i, count; | ||
999 | u64 final_start, final_end; | ||
1000 | int idx = 0; | ||
1001 | |||
1002 | count = 0; | ||
1003 | for (i = 0; i < max_early_res && early_res[i].end; i++) | ||
1004 | count++; | ||
1005 | |||
1006 | /* need to skip first one ?*/ | ||
1007 | if (early_res != early_res_x) | ||
1008 | idx = 1; | ||
1009 | |||
1010 | #if 1 | ||
1011 | printk(KERN_INFO "Subtract (%d early reservations)\n", count); | ||
1012 | #endif | ||
1013 | for (i = idx; i < count; i++) { | ||
1014 | struct early_res *r = &early_res[i]; | ||
1015 | #if 0 | ||
1016 | printk(KERN_INFO " #%d [%010llx - %010llx] %15s", i, | ||
1017 | r->start, r->end, r->name); | ||
1018 | #endif | ||
1019 | final_start = PFN_DOWN(r->start); | ||
1020 | final_end = PFN_UP(r->end); | ||
1021 | if (final_start >= final_end) { | ||
1022 | #if 0 | ||
1023 | printk(KERN_CONT "\n"); | ||
1024 | #endif | ||
1025 | continue; | ||
1026 | } | ||
1027 | #if 0 | ||
1028 | printk(KERN_CONT " subtract pfn [%010llx - %010llx]\n", | ||
1029 | final_start, final_end); | ||
1030 | #endif | ||
1031 | subtract_range(range, az, final_start, final_end); | ||
1032 | } | ||
1033 | |||
1034 | } | ||
1035 | |||
1036 | int __init get_free_all_memory_range(struct range **rangep, int nodeid) | ||
1037 | { | ||
1038 | int i, count; | ||
1039 | u64 start = 0, end; | ||
1040 | u64 size; | ||
1041 | u64 mem; | ||
1042 | struct range *range; | ||
1043 | int nr_range; | ||
1044 | |||
1045 | count = 0; | ||
1046 | for (i = 0; i < max_early_res && early_res[i].end; i++) | ||
1047 | count++; | ||
1048 | |||
1049 | count *= 2; | ||
1050 | |||
1051 | size = sizeof(struct range) * count; | ||
1052 | #ifdef MAX_DMA32_PFN | ||
1053 | if (max_pfn_mapped > MAX_DMA32_PFN) | ||
1054 | start = MAX_DMA32_PFN << PAGE_SHIFT; | ||
1055 | #endif | ||
1056 | end = max_pfn_mapped << PAGE_SHIFT; | ||
1057 | mem = find_e820_area(start, end, size, sizeof(struct range)); | ||
1058 | if (mem == -1ULL) | ||
1059 | panic("can not find more space for range free"); | ||
1060 | |||
1061 | range = __va(mem); | ||
1062 | /* use early_node_map[] and early_res to get range array at first */ | ||
1063 | memset(range, 0, size); | ||
1064 | nr_range = 0; | ||
1065 | |||
1066 | /* need to go over early_node_map to find out good range for node */ | ||
1067 | nr_range = add_from_early_node_map(range, count, nr_range, nodeid); | ||
1068 | subtract_early_res(range, count); | ||
1069 | nr_range = clean_sort_range(range, count); | ||
1070 | |||
1071 | /* need to clear it ? */ | ||
1072 | if (nodeid == MAX_NUMNODES) { | ||
1073 | memset(&early_res[0], 0, | ||
1074 | sizeof(struct early_res) * max_early_res); | ||
1075 | early_res = NULL; | ||
1076 | max_early_res = 0; | ||
1077 | } | ||
1078 | |||
1079 | *rangep = range; | ||
1080 | return nr_range; | ||
1081 | } | ||
1082 | #else | ||
1083 | void __init early_res_to_bootmem(u64 start, u64 end) | ||
1084 | { | ||
1085 | int i, count; | ||
1086 | u64 final_start, final_end; | ||
1087 | int idx = 0; | ||
1088 | |||
1089 | count = 0; | ||
1090 | for (i = 0; i < max_early_res && early_res[i].end; i++) | ||
1091 | count++; | ||
1092 | |||
1093 | /* need to skip first one ?*/ | ||
1094 | if (early_res != early_res_x) | ||
1095 | idx = 1; | ||
1096 | |||
1097 | printk(KERN_INFO "(%d/%d early reservations) ==> bootmem [%010llx - %010llx]\n", | ||
1098 | count - idx, max_early_res, start, end); | ||
1099 | for (i = idx; i < count; i++) { | ||
1100 | struct early_res *r = &early_res[i]; | ||
1101 | printk(KERN_INFO " #%d [%010llx - %010llx] %16s", i, | ||
1102 | r->start, r->end, r->name); | ||
1103 | final_start = max(start, r->start); | ||
1104 | final_end = min(end, r->end); | ||
1105 | if (final_start >= final_end) { | ||
1106 | printk(KERN_CONT "\n"); | ||
1107 | continue; | ||
1108 | } | ||
1109 | printk(KERN_CONT " ==> [%010llx - %010llx]\n", | ||
1110 | final_start, final_end); | ||
1111 | reserve_bootmem_generic(final_start, final_end - final_start, | ||
1112 | BOOTMEM_DEFAULT); | ||
1113 | } | ||
1114 | /* clear them */ | ||
1115 | memset(&early_res[0], 0, sizeof(struct early_res) * max_early_res); | ||
1116 | early_res = NULL; | ||
1117 | max_early_res = 0; | ||
1118 | early_res_count = 0; | ||
1119 | } | ||
1120 | #endif | ||
1121 | |||
1122 | /* Check for already reserved areas */ | ||
1123 | static inline int __init bad_addr(u64 *addrp, u64 size, u64 align) | ||
1124 | { | ||
1125 | int i; | ||
1126 | u64 addr = *addrp; | ||
1127 | int changed = 0; | ||
1128 | struct early_res *r; | ||
1129 | again: | ||
1130 | i = find_overlapped_early(addr, addr + size); | ||
1131 | r = &early_res[i]; | ||
1132 | if (i < max_early_res && r->end) { | ||
1133 | *addrp = addr = round_up(r->end, align); | ||
1134 | changed = 1; | ||
1135 | goto again; | ||
1136 | } | ||
1137 | return changed; | ||
1138 | } | ||
1139 | |||
1140 | /* Check for already reserved areas */ | ||
1141 | static inline int __init bad_addr_size(u64 *addrp, u64 *sizep, u64 align) | ||
1142 | { | ||
1143 | int i; | ||
1144 | u64 addr = *addrp, last; | ||
1145 | u64 size = *sizep; | ||
1146 | int changed = 0; | ||
1147 | again: | ||
1148 | last = addr + size; | ||
1149 | for (i = 0; i < max_early_res && early_res[i].end; i++) { | ||
1150 | struct early_res *r = &early_res[i]; | ||
1151 | if (last > r->start && addr < r->start) { | ||
1152 | size = r->start - addr; | ||
1153 | changed = 1; | ||
1154 | goto again; | ||
1155 | } | ||
1156 | if (last > r->end && addr < r->end) { | ||
1157 | addr = round_up(r->end, align); | ||
1158 | size = last - addr; | ||
1159 | changed = 1; | ||
1160 | goto again; | ||
1161 | } | ||
1162 | if (last <= r->end && addr >= r->start) { | ||
1163 | (*sizep)++; | ||
1164 | return 0; | ||
1165 | } | ||
1166 | } | ||
1167 | if (changed) { | ||
1168 | *addrp = addr; | ||
1169 | *sizep = size; | ||
1170 | } | ||
1171 | return changed; | ||
1172 | } | ||
1173 | |||
1174 | /* | ||
1175 | * Find a free area with specified alignment in a specific range. | ||
1176 | * only with the area.between start to end is active range from early_node_map | ||
1177 | * so they are good as RAM | ||
1178 | */ | ||
1179 | u64 __init find_early_area(u64 ei_start, u64 ei_last, u64 start, u64 end, | ||
1180 | u64 size, u64 align) | ||
1181 | { | ||
1182 | u64 addr, last; | ||
1183 | |||
1184 | addr = round_up(ei_start, align); | ||
1185 | if (addr < start) | ||
1186 | addr = round_up(start, align); | ||
1187 | if (addr >= ei_last) | ||
1188 | goto out; | ||
1189 | while (bad_addr(&addr, size, align) && addr+size <= ei_last) | ||
1190 | ; | ||
1191 | last = addr + size; | ||
1192 | if (last > ei_last) | ||
1193 | goto out; | ||
1194 | if (last > end) | ||
1195 | goto out; | ||
1196 | |||
1197 | return addr; | ||
1198 | |||
1199 | out: | ||
1200 | return -1ULL; | ||
1201 | } | ||
1202 | |||
1203 | /* | ||
1204 | * Find a free area with specified alignment in a specific range. | ||
1205 | */ | ||
1206 | u64 __init find_e820_area(u64 start, u64 end, u64 size, u64 align) | ||
1207 | { | ||
1208 | int i; | ||
1209 | |||
1210 | for (i = 0; i < e820.nr_map; i++) { | ||
1211 | struct e820entry *ei = &e820.map[i]; | ||
1212 | u64 addr; | ||
1213 | u64 ei_start, ei_last; | ||
1214 | |||
1215 | if (ei->type != E820_RAM) | ||
1216 | continue; | ||
1217 | |||
1218 | ei_last = ei->addr + ei->size; | ||
1219 | ei_start = ei->addr; | ||
1220 | addr = find_early_area(ei_start, ei_last, start, end, | ||
1221 | size, align); | ||
1222 | |||
1223 | if (addr == -1ULL) | ||
1224 | continue; | ||
1225 | |||
1226 | return addr; | ||
1227 | } | ||
1228 | return -1ULL; | ||
1229 | } | ||
1230 | |||
1231 | /* | ||
1232 | * Find next free range after *start | ||
1233 | */ | ||
1234 | u64 __init find_e820_area_size(u64 start, u64 *sizep, u64 align) | ||
1235 | { | ||
1236 | int i; | ||
1237 | |||
1238 | for (i = 0; i < e820.nr_map; i++) { | ||
1239 | struct e820entry *ei = &e820.map[i]; | ||
1240 | u64 addr, last; | ||
1241 | u64 ei_last; | ||
1242 | |||
1243 | if (ei->type != E820_RAM) | ||
1244 | continue; | ||
1245 | addr = round_up(ei->addr, align); | ||
1246 | ei_last = ei->addr + ei->size; | ||
1247 | if (addr < start) | ||
1248 | addr = round_up(start, align); | ||
1249 | if (addr >= ei_last) | ||
1250 | continue; | ||
1251 | *sizep = ei_last - addr; | ||
1252 | while (bad_addr_size(&addr, sizep, align) && | ||
1253 | addr + *sizep <= ei_last) | ||
1254 | ; | ||
1255 | last = addr + *sizep; | ||
1256 | if (last > ei_last) | ||
1257 | continue; | ||
1258 | return addr; | ||
1259 | } | ||
1260 | |||
1261 | return -1ULL; | ||
1262 | } | ||
1263 | |||
1264 | /* | ||
1265 | * pre allocated 4k and reserved it in e820 | 726 | * pre allocated 4k and reserved it in e820 |
1266 | */ | 727 | */ |
1267 | u64 __init early_reserve_e820(u64 startt, u64 sizet, u64 align) | 728 | u64 __init early_reserve_e820(u64 startt, u64 sizet, u64 align) |