diff options
author | Yinghai Lu <yinghai@kernel.org> | 2010-02-10 04:20:24 -0500 |
---|---|---|
committer | H. Peter Anvin <hpa@zytor.com> | 2010-02-12 12:42:38 -0500 |
commit | a678c2be75773e112f6d656a22a7f1645c4dbd6c (patch) | |
tree | 5c2220110d7733d935eb2e5fa3d84e3b43d0ac20 /arch/x86/kernel/early_res.c | |
parent | db8f77c889542b09457b8b97efb311343c99a75d (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>
Diffstat (limited to 'arch/x86/kernel/early_res.c')
-rw-r--r-- | arch/x86/kernel/early_res.c | 538 |
1 files changed, 538 insertions, 0 deletions
diff --git a/arch/x86/kernel/early_res.c b/arch/x86/kernel/early_res.c new file mode 100644 index 00000000000..1cf2c2f9ea6 --- /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 | |||
23 | struct early_res { | ||
24 | u64 start, end; | ||
25 | char name[15]; | ||
26 | char overlap_ok; | ||
27 | }; | ||
28 | static struct early_res early_res_x[MAX_EARLY_RES_X] __initdata; | ||
29 | |||
30 | static int max_early_res __initdata = MAX_EARLY_RES_X; | ||
31 | static struct early_res *early_res __initdata = &early_res_x[0]; | ||
32 | static int early_res_count __initdata; | ||
33 | |||
34 | static 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 | */ | ||
53 | static 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 | */ | ||
77 | static 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 | |||
134 | static 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 | */ | ||
177 | void __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 | |||
183 | static 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 | */ | ||
233 | void __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 | |||
244 | void __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 | |||
263 | void __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 | ||
278 | static 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 | |||
312 | int __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 | ||
359 | void __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 */ | ||
399 | static 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; | ||
405 | again: | ||
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 */ | ||
417 | static 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; | ||
423 | again: | ||
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 | */ | ||
455 | u64 __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 | |||
475 | out: | ||
476 | return -1ULL; | ||
477 | } | ||
478 | |||
479 | /* | ||
480 | * Find a free area with specified alignment in a specific range. | ||
481 | */ | ||
482 | u64 __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 | */ | ||
510 | u64 __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 | } | ||