aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYinghai Lu <yinghai@kernel.org>2010-02-10 04:20:24 -0500
committerH. Peter Anvin <hpa@zytor.com>2010-02-12 12:42:38 -0500
commita678c2be75773e112f6d656a22a7f1645c4dbd6c (patch)
tree5c2220110d7733d935eb2e5fa3d84e3b43d0ac20
parentdb8f77c889542b09457b8b97efb311343c99a75d (diff)
x86: Separate early_res related code from e820.c
... to make e820.c smaller. -v2: fix 32bit compiling with MAX_DMA32_PFN Signed-off-by: Yinghai Lu <yinghai@kernel.org> LKML-Reference: <1265793639-15071-21-git-send-email-yinghai@kernel.org> Signed-off-by: H. Peter Anvin <hpa@zytor.com>
-rw-r--r--arch/x86/include/asm/e820.h13
-rw-r--r--arch/x86/include/asm/early_res.h20
-rw-r--r--arch/x86/kernel/Makefile2
-rw-r--r--arch/x86/kernel/e820.c541
-rw-r--r--arch/x86/kernel/early_res.c538
5 files changed, 561 insertions, 553 deletions
diff --git a/arch/x86/include/asm/e820.h b/arch/x86/include/asm/e820.h
index 7d72e5fb7008..efad699a2c22 100644
--- a/arch/x86/include/asm/e820.h
+++ b/arch/x86/include/asm/e820.h
@@ -109,19 +109,8 @@ static inline void early_memtest(unsigned long start, unsigned long end)
109 109
110extern unsigned long end_user_pfn; 110extern unsigned long end_user_pfn;
111 111
112extern u64 find_e820_area(u64 start, u64 end, u64 size, u64 align);
113extern u64 find_e820_area_size(u64 start, u64 *sizep, u64 align);
114extern void reserve_early(u64 start, u64 end, char *name);
115extern void reserve_early_overlap_ok(u64 start, u64 end, char *name);
116extern void free_early(u64 start, u64 end);
117extern void early_res_to_bootmem(u64 start, u64 end);
118extern u64 early_reserve_e820(u64 startt, u64 sizet, u64 align); 112extern u64 early_reserve_e820(u64 startt, u64 sizet, u64 align);
119 113#include <asm/early_res.h>
120void reserve_early_without_check(u64 start, u64 end, char *name);
121u64 find_early_area(u64 ei_start, u64 ei_last, u64 start, u64 end,
122 u64 size, u64 align);
123#include <linux/range.h>
124int get_free_all_memory_range(struct range **rangep, int nodeid);
125 114
126extern unsigned long e820_end_of_ram_pfn(void); 115extern unsigned long e820_end_of_ram_pfn(void);
127extern unsigned long e820_end_of_low_ram_pfn(void); 116extern unsigned long e820_end_of_low_ram_pfn(void);
diff --git a/arch/x86/include/asm/early_res.h b/arch/x86/include/asm/early_res.h
new file mode 100644
index 000000000000..2d43b166782d
--- /dev/null
+++ b/arch/x86/include/asm/early_res.h
@@ -0,0 +1,20 @@
1#ifndef _ASM_X86_EARLY_RES_H
2#define _ASM_X86_EARLY_RES_H
3#ifdef __KERNEL__
4
5extern u64 find_e820_area(u64 start, u64 end, u64 size, u64 align);
6extern u64 find_e820_area_size(u64 start, u64 *sizep, u64 align);
7extern void reserve_early(u64 start, u64 end, char *name);
8extern void reserve_early_overlap_ok(u64 start, u64 end, char *name);
9extern void free_early(u64 start, u64 end);
10extern void early_res_to_bootmem(u64 start, u64 end);
11
12void reserve_early_without_check(u64 start, u64 end, char *name);
13u64 find_early_area(u64 ei_start, u64 ei_last, u64 start, u64 end,
14 u64 size, u64 align);
15#include <linux/range.h>
16int get_free_all_memory_range(struct range **rangep, int nodeid);
17
18#endif /* __KERNEL__ */
19
20#endif /* _ASM_X86_EARLY_RES_H */
diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile
index d87f09bc5a52..f5fb9f0b6277 100644
--- a/arch/x86/kernel/Makefile
+++ b/arch/x86/kernel/Makefile
@@ -38,7 +38,7 @@ obj-$(CONFIG_X86_32) += probe_roms_32.o
38obj-$(CONFIG_X86_32) += sys_i386_32.o i386_ksyms_32.o 38obj-$(CONFIG_X86_32) += sys_i386_32.o i386_ksyms_32.o
39obj-$(CONFIG_X86_64) += sys_x86_64.o x8664_ksyms_64.o 39obj-$(CONFIG_X86_64) += sys_x86_64.o x8664_ksyms_64.o
40obj-$(CONFIG_X86_64) += syscall_64.o vsyscall_64.o 40obj-$(CONFIG_X86_64) += syscall_64.o vsyscall_64.o
41obj-y += bootflag.o e820.o 41obj-y += bootflag.o e820.o early_res.o
42obj-y += pci-dma.o quirks.o i8237.o topology.o kdebugfs.o 42obj-y += pci-dma.o quirks.o i8237.o topology.o kdebugfs.o
43obj-y += alternative.o i8253.o pci-nommu.o hw_breakpoint.o 43obj-y += alternative.o i8253.o pci-nommu.o hw_breakpoint.o
44obj-y += tsc.o io_delay.o rtc.o 44obj-y += tsc.o io_delay.o rtc.o
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
741struct early_res {
742 u64 start, end;
743 char name[15];
744 char overlap_ok;
745};
746static struct early_res early_res_x[MAX_EARLY_RES_X] __initdata;
747
748static int max_early_res __initdata = MAX_EARLY_RES_X;
749static struct early_res *early_res __initdata = &early_res_x[0];
750static int early_res_count __initdata;
751
752static 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 */
771static 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 */
795static 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
852static 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 */
895void __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
901static 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 */
951void __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
962void __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
981void __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
996static 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
1036int __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
1083void __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 */
1123static 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;
1129again:
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 */
1141static 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;
1147again:
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 */
1179u64 __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
1199out:
1200 return -1ULL;
1201}
1202
1203/*
1204 * Find a free area with specified alignment in a specific range.
1205 */
1206u64 __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 */
1234u64 __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 */
1267u64 __init early_reserve_e820(u64 startt, u64 sizet, u64 align) 728u64 __init early_reserve_e820(u64 startt, u64 sizet, u64 align)
diff --git a/arch/x86/kernel/early_res.c b/arch/x86/kernel/early_res.c
new file mode 100644
index 000000000000..1cf2c2f9ea68
--- /dev/null
+++ b/arch/x86/kernel/early_res.c
@@ -0,0 +1,538 @@
1/*
2 * early_res, could be used to replace bootmem
3 */
4#include <linux/kernel.h>
5#include <linux/types.h>
6#include <linux/init.h>
7#include <linux/bootmem.h>
8#include <linux/mm.h>
9
10#include <asm/e820.h>
11#include <asm/early_res.h>
12#include <asm/proto.h>
13
14/*
15 * Early reserved memory areas.
16 */
17/*
18 * need to make sure this one is bigger enough before
19 * find_e820_area could be used
20 */
21#define MAX_EARLY_RES_X 32
22
23struct early_res {
24 u64 start, end;
25 char name[15];
26 char overlap_ok;
27};
28static struct early_res early_res_x[MAX_EARLY_RES_X] __initdata;
29
30static int max_early_res __initdata = MAX_EARLY_RES_X;
31static struct early_res *early_res __initdata = &early_res_x[0];
32static int early_res_count __initdata;
33
34static int __init find_overlapped_early(u64 start, u64 end)
35{
36 int i;
37 struct early_res *r;
38
39 for (i = 0; i < max_early_res && early_res[i].end; i++) {
40 r = &early_res[i];
41 if (end > r->start && start < r->end)
42 break;
43 }
44
45 return i;
46}
47
48/*
49 * Drop the i-th range from the early reservation map,
50 * by copying any higher ranges down one over it, and
51 * clearing what had been the last slot.
52 */
53static void __init drop_range(int i)
54{
55 int j;
56
57 for (j = i + 1; j < max_early_res && early_res[j].end; j++)
58 ;
59
60 memmove(&early_res[i], &early_res[i + 1],
61 (j - 1 - i) * sizeof(struct early_res));
62
63 early_res[j - 1].end = 0;
64 early_res_count--;
65}
66
67/*
68 * Split any existing ranges that:
69 * 1) are marked 'overlap_ok', and
70 * 2) overlap with the stated range [start, end)
71 * into whatever portion (if any) of the existing range is entirely
72 * below or entirely above the stated range. Drop the portion
73 * of the existing range that overlaps with the stated range,
74 * which will allow the caller of this routine to then add that
75 * stated range without conflicting with any existing range.
76 */
77static void __init drop_overlaps_that_are_ok(u64 start, u64 end)
78{
79 int i;
80 struct early_res *r;
81 u64 lower_start, lower_end;
82 u64 upper_start, upper_end;
83 char name[15];
84
85 for (i = 0; i < max_early_res && early_res[i].end; i++) {
86 r = &early_res[i];
87
88 /* Continue past non-overlapping ranges */
89 if (end <= r->start || start >= r->end)
90 continue;
91
92 /*
93 * Leave non-ok overlaps as is; let caller
94 * panic "Overlapping early reservations"
95 * when it hits this overlap.
96 */
97 if (!r->overlap_ok)
98 return;
99
100 /*
101 * We have an ok overlap. We will drop it from the early
102 * reservation map, and add back in any non-overlapping
103 * portions (lower or upper) as separate, overlap_ok,
104 * non-overlapping ranges.
105 */
106
107 /* 1. Note any non-overlapping (lower or upper) ranges. */
108 strncpy(name, r->name, sizeof(name) - 1);
109
110 lower_start = lower_end = 0;
111 upper_start = upper_end = 0;
112 if (r->start < start) {
113 lower_start = r->start;
114 lower_end = start;
115 }
116 if (r->end > end) {
117 upper_start = end;
118 upper_end = r->end;
119 }
120
121 /* 2. Drop the original ok overlapping range */
122 drop_range(i);
123
124 i--; /* resume for-loop on copied down entry */
125
126 /* 3. Add back in any non-overlapping ranges. */
127 if (lower_end)
128 reserve_early_overlap_ok(lower_start, lower_end, name);
129 if (upper_end)
130 reserve_early_overlap_ok(upper_start, upper_end, name);
131 }
132}
133
134static void __init __reserve_early(u64 start, u64 end, char *name,
135 int overlap_ok)
136{
137 int i;
138 struct early_res *r;
139
140 i = find_overlapped_early(start, end);
141 if (i >= max_early_res)
142 panic("Too many early reservations");
143 r = &early_res[i];
144 if (r->end)
145 panic("Overlapping early reservations "
146 "%llx-%llx %s to %llx-%llx %s\n",
147 start, end - 1, name ? name : "", r->start,
148 r->end - 1, r->name);
149 r->start = start;
150 r->end = end;
151 r->overlap_ok = overlap_ok;
152 if (name)
153 strncpy(r->name, name, sizeof(r->name) - 1);
154 early_res_count++;
155}
156
157/*
158 * A few early reservtations come here.
159 *
160 * The 'overlap_ok' in the name of this routine does -not- mean it
161 * is ok for these reservations to overlap an earlier reservation.
162 * Rather it means that it is ok for subsequent reservations to
163 * overlap this one.
164 *
165 * Use this entry point to reserve early ranges when you are doing
166 * so out of "Paranoia", reserving perhaps more memory than you need,
167 * just in case, and don't mind a subsequent overlapping reservation
168 * that is known to be needed.
169 *
170 * The drop_overlaps_that_are_ok() call here isn't really needed.
171 * It would be needed if we had two colliding 'overlap_ok'
172 * reservations, so that the second such would not panic on the
173 * overlap with the first. We don't have any such as of this
174 * writing, but might as well tolerate such if it happens in
175 * the future.
176 */
177void __init reserve_early_overlap_ok(u64 start, u64 end, char *name)
178{
179 drop_overlaps_that_are_ok(start, end);
180 __reserve_early(start, end, name, 1);
181}
182
183static void __init __check_and_double_early_res(u64 start)
184{
185 u64 end, size, mem;
186 struct early_res *new;
187
188 /* do we have enough slots left ? */
189 if ((max_early_res - early_res_count) > max(max_early_res/8, 2))
190 return;
191
192 /* double it */
193 end = max_pfn_mapped << PAGE_SHIFT;
194 size = sizeof(struct early_res) * max_early_res * 2;
195 mem = find_e820_area(start, end, size, sizeof(struct early_res));
196
197 if (mem == -1ULL)
198 panic("can not find more space for early_res array");
199
200 new = __va(mem);
201 /* save the first one for own */
202 new[0].start = mem;
203 new[0].end = mem + size;
204 new[0].overlap_ok = 0;
205 /* copy old to new */
206 if (early_res == early_res_x) {
207 memcpy(&new[1], &early_res[0],
208 sizeof(struct early_res) * max_early_res);
209 memset(&new[max_early_res+1], 0,
210 sizeof(struct early_res) * (max_early_res - 1));
211 early_res_count++;
212 } else {
213 memcpy(&new[1], &early_res[1],
214 sizeof(struct early_res) * (max_early_res - 1));
215 memset(&new[max_early_res], 0,
216 sizeof(struct early_res) * max_early_res);
217 }
218 memset(&early_res[0], 0, sizeof(struct early_res) * max_early_res);
219 early_res = new;
220 max_early_res *= 2;
221 printk(KERN_DEBUG "early_res array is doubled to %d at [%llx - %llx]\n",
222 max_early_res, mem, mem + size - 1);
223}
224
225/*
226 * Most early reservations come here.
227 *
228 * We first have drop_overlaps_that_are_ok() drop any pre-existing
229 * 'overlap_ok' ranges, so that we can then reserve this memory
230 * range without risk of panic'ing on an overlapping overlap_ok
231 * early reservation.
232 */
233void __init reserve_early(u64 start, u64 end, char *name)
234{
235 if (start >= end)
236 return;
237
238 __check_and_double_early_res(end);
239
240 drop_overlaps_that_are_ok(start, end);
241 __reserve_early(start, end, name, 0);
242}
243
244void __init reserve_early_without_check(u64 start, u64 end, char *name)
245{
246 struct early_res *r;
247
248 if (start >= end)
249 return;
250
251 __check_and_double_early_res(end);
252
253 r = &early_res[early_res_count];
254
255 r->start = start;
256 r->end = end;
257 r->overlap_ok = 0;
258 if (name)
259 strncpy(r->name, name, sizeof(r->name) - 1);
260 early_res_count++;
261}
262
263void __init free_early(u64 start, u64 end)
264{
265 struct early_res *r;
266 int i;
267
268 i = find_overlapped_early(start, end);
269 r = &early_res[i];
270 if (i >= max_early_res || r->end != end || r->start != start)
271 panic("free_early on not reserved area: %llx-%llx!",
272 start, end - 1);
273
274 drop_range(i);
275}
276
277#ifdef CONFIG_NO_BOOTMEM
278static void __init subtract_early_res(struct range *range, int az)
279{
280 int i, count;
281 u64 final_start, final_end;
282 int idx = 0;
283
284 count = 0;
285 for (i = 0; i < max_early_res && early_res[i].end; i++)
286 count++;
287
288 /* need to skip first one ?*/
289 if (early_res != early_res_x)
290 idx = 1;
291
292#define DEBUG_PRINT_EARLY_RES 1
293
294#if DEBUG_PRINT_EARLY_RES
295 printk(KERN_INFO "Subtract (%d early reservations)\n", count);
296#endif
297 for (i = idx; i < count; i++) {
298 struct early_res *r = &early_res[i];
299#if DEBUG_PRINT_EARLY_RES
300 printk(KERN_INFO " #%d [%010llx - %010llx] %15s\n", i,
301 r->start, r->end, r->name);
302#endif
303 final_start = PFN_DOWN(r->start);
304 final_end = PFN_UP(r->end);
305 if (final_start >= final_end)
306 continue;
307 subtract_range(range, az, final_start, final_end);
308 }
309
310}
311
312int __init get_free_all_memory_range(struct range **rangep, int nodeid)
313{
314 int i, count;
315 u64 start = 0, end;
316 u64 size;
317 u64 mem;
318 struct range *range;
319 int nr_range;
320
321 count = 0;
322 for (i = 0; i < max_early_res && early_res[i].end; i++)
323 count++;
324
325 count *= 2;
326
327 size = sizeof(struct range) * count;
328#ifdef MAX_DMA32_PFN
329 if (max_pfn_mapped > MAX_DMA32_PFN)
330 start = MAX_DMA32_PFN << PAGE_SHIFT;
331#endif
332 end = max_pfn_mapped << PAGE_SHIFT;
333 mem = find_e820_area(start, end, size, sizeof(struct range));
334 if (mem == -1ULL)
335 panic("can not find more space for range free");
336
337 range = __va(mem);
338 /* use early_node_map[] and early_res to get range array at first */
339 memset(range, 0, size);
340 nr_range = 0;
341
342 /* need to go over early_node_map to find out good range for node */
343 nr_range = add_from_early_node_map(range, count, nr_range, nodeid);
344 subtract_early_res(range, count);
345 nr_range = clean_sort_range(range, count);
346
347 /* need to clear it ? */
348 if (nodeid == MAX_NUMNODES) {
349 memset(&early_res[0], 0,
350 sizeof(struct early_res) * max_early_res);
351 early_res = NULL;
352 max_early_res = 0;
353 }
354
355 *rangep = range;
356 return nr_range;
357}
358#else
359void __init early_res_to_bootmem(u64 start, u64 end)
360{
361 int i, count;
362 u64 final_start, final_end;
363 int idx = 0;
364
365 count = 0;
366 for (i = 0; i < max_early_res && early_res[i].end; i++)
367 count++;
368
369 /* need to skip first one ?*/
370 if (early_res != early_res_x)
371 idx = 1;
372
373 printk(KERN_INFO "(%d/%d early reservations) ==> bootmem [%010llx - %010llx]\n",
374 count - idx, max_early_res, start, end);
375 for (i = idx; i < count; i++) {
376 struct early_res *r = &early_res[i];
377 printk(KERN_INFO " #%d [%010llx - %010llx] %16s", i,
378 r->start, r->end, r->name);
379 final_start = max(start, r->start);
380 final_end = min(end, r->end);
381 if (final_start >= final_end) {
382 printk(KERN_CONT "\n");
383 continue;
384 }
385 printk(KERN_CONT " ==> [%010llx - %010llx]\n",
386 final_start, final_end);
387 reserve_bootmem_generic(final_start, final_end - final_start,
388 BOOTMEM_DEFAULT);
389 }
390 /* clear them */
391 memset(&early_res[0], 0, sizeof(struct early_res) * max_early_res);
392 early_res = NULL;
393 max_early_res = 0;
394 early_res_count = 0;
395}
396#endif
397
398/* Check for already reserved areas */
399static inline int __init bad_addr(u64 *addrp, u64 size, u64 align)
400{
401 int i;
402 u64 addr = *addrp;
403 int changed = 0;
404 struct early_res *r;
405again:
406 i = find_overlapped_early(addr, addr + size);
407 r = &early_res[i];
408 if (i < max_early_res && r->end) {
409 *addrp = addr = round_up(r->end, align);
410 changed = 1;
411 goto again;
412 }
413 return changed;
414}
415
416/* Check for already reserved areas */
417static inline int __init bad_addr_size(u64 *addrp, u64 *sizep, u64 align)
418{
419 int i;
420 u64 addr = *addrp, last;
421 u64 size = *sizep;
422 int changed = 0;
423again:
424 last = addr + size;
425 for (i = 0; i < max_early_res && early_res[i].end; i++) {
426 struct early_res *r = &early_res[i];
427 if (last > r->start && addr < r->start) {
428 size = r->start - addr;
429 changed = 1;
430 goto again;
431 }
432 if (last > r->end && addr < r->end) {
433 addr = round_up(r->end, align);
434 size = last - addr;
435 changed = 1;
436 goto again;
437 }
438 if (last <= r->end && addr >= r->start) {
439 (*sizep)++;
440 return 0;
441 }
442 }
443 if (changed) {
444 *addrp = addr;
445 *sizep = size;
446 }
447 return changed;
448}
449
450/*
451 * Find a free area with specified alignment in a specific range.
452 * only with the area.between start to end is active range from early_node_map
453 * so they are good as RAM
454 */
455u64 __init find_early_area(u64 ei_start, u64 ei_last, u64 start, u64 end,
456 u64 size, u64 align)
457{
458 u64 addr, last;
459
460 addr = round_up(ei_start, align);
461 if (addr < start)
462 addr = round_up(start, align);
463 if (addr >= ei_last)
464 goto out;
465 while (bad_addr(&addr, size, align) && addr+size <= ei_last)
466 ;
467 last = addr + size;
468 if (last > ei_last)
469 goto out;
470 if (last > end)
471 goto out;
472
473 return addr;
474
475out:
476 return -1ULL;
477}
478
479/*
480 * Find a free area with specified alignment in a specific range.
481 */
482u64 __init find_e820_area(u64 start, u64 end, u64 size, u64 align)
483{
484 int i;
485
486 for (i = 0; i < e820.nr_map; i++) {
487 struct e820entry *ei = &e820.map[i];
488 u64 addr;
489 u64 ei_start, ei_last;
490
491 if (ei->type != E820_RAM)
492 continue;
493
494 ei_last = ei->addr + ei->size;
495 ei_start = ei->addr;
496 addr = find_early_area(ei_start, ei_last, start, end,
497 size, align);
498
499 if (addr == -1ULL)
500 continue;
501
502 return addr;
503 }
504 return -1ULL;
505}
506
507/*
508 * Find next free range after *start
509 */
510u64 __init find_e820_area_size(u64 start, u64 *sizep, u64 align)
511{
512 int i;
513
514 for (i = 0; i < e820.nr_map; i++) {
515 struct e820entry *ei = &e820.map[i];
516 u64 addr, last;
517 u64 ei_last;
518
519 if (ei->type != E820_RAM)
520 continue;
521 addr = round_up(ei->addr, align);
522 ei_last = ei->addr + ei->size;
523 if (addr < start)
524 addr = round_up(start, align);
525 if (addr >= ei_last)
526 continue;
527 *sizep = ei_last - addr;
528 while (bad_addr_size(&addr, sizep, align) &&
529 addr + *sizep <= ei_last)
530 ;
531 last = addr + *sizep;
532 if (last > ei_last)
533 continue;
534 return addr;
535 }
536
537 return -1ULL;
538}