Commit | Line | Data |
---|---|---|
b79cd8f1 YL |
1 | /* |
2 | * Handle the memory map. | |
3 | * The functions here do the job until bootmem takes over. | |
4 | * | |
5 | * Getting sanitize_e820_map() in sync with i386 version by applying change: | |
6 | * - Provisions for empty E820 memory regions (reported by certain BIOSes). | |
7 | * Alex Achenbach <xela@slit.de>, December 2002. | |
8 | * Venkatesh Pallipadi <venkatesh.pallipadi@intel.com> | |
9 | * | |
10 | */ | |
11 | #include <linux/kernel.h> | |
12 | #include <linux/types.h> | |
13 | #include <linux/init.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> | |
bf62f398 | 21 | #include <linux/suspend.h> |
b79cd8f1 YL |
22 | |
23 | #include <asm/pgtable.h> | |
24 | #include <asm/page.h> | |
25 | #include <asm/e820.h> | |
a4c81cf6 | 26 | #include <asm/proto.h> |
b79cd8f1 | 27 | #include <asm/setup.h> |
a4c81cf6 | 28 | #include <asm/trampoline.h> |
b79cd8f1 YL |
29 | |
30 | struct e820map e820; | |
31 | ||
32 | /* For PCI or other memory-mapped resources */ | |
33 | unsigned long pci_mem_start = 0xaeedbabe; | |
34 | #ifdef CONFIG_PCI | |
35 | EXPORT_SYMBOL(pci_mem_start); | |
36 | #endif | |
37 | ||
38 | /* | |
39 | * This function checks if any part of the range <start,end> is mapped | |
40 | * with type. | |
41 | */ | |
42 | int | |
43 | e820_any_mapped(u64 start, u64 end, unsigned type) | |
44 | { | |
45 | int i; | |
46 | ||
47 | for (i = 0; i < e820.nr_map; i++) { | |
48 | struct e820entry *ei = &e820.map[i]; | |
49 | ||
50 | if (type && ei->type != type) | |
51 | continue; | |
52 | if (ei->addr >= end || ei->addr + ei->size <= start) | |
53 | continue; | |
54 | return 1; | |
55 | } | |
56 | return 0; | |
57 | } | |
58 | EXPORT_SYMBOL_GPL(e820_any_mapped); | |
59 | ||
60 | /* | |
61 | * This function checks if the entire range <start,end> is mapped with type. | |
62 | * | |
63 | * Note: this function only works correct if the e820 table is sorted and | |
64 | * not-overlapping, which is the case | |
65 | */ | |
66 | int __init e820_all_mapped(u64 start, u64 end, unsigned type) | |
67 | { | |
68 | int i; | |
69 | ||
70 | for (i = 0; i < e820.nr_map; i++) { | |
71 | struct e820entry *ei = &e820.map[i]; | |
72 | ||
73 | if (type && ei->type != type) | |
74 | continue; | |
75 | /* is the region (part) in overlap with the current region ?*/ | |
76 | if (ei->addr >= end || ei->addr + ei->size <= start) | |
77 | continue; | |
78 | ||
79 | /* if the region is at the beginning of <start,end> we move | |
80 | * start to the end of the region since it's ok until there | |
81 | */ | |
82 | if (ei->addr <= start) | |
83 | start = ei->addr + ei->size; | |
84 | /* | |
85 | * if start is now at or beyond end, we're done, full | |
86 | * coverage | |
87 | */ | |
88 | if (start >= end) | |
89 | return 1; | |
90 | } | |
91 | return 0; | |
92 | } | |
93 | ||
94 | /* | |
95 | * Add a memory region to the kernel e820 map. | |
96 | */ | |
97 | void __init add_memory_region(u64 start, u64 size, int type) | |
98 | { | |
99 | int x = e820.nr_map; | |
100 | ||
c3965bd1 | 101 | if (x == ARRAY_SIZE(e820.map)) { |
b79cd8f1 YL |
102 | printk(KERN_ERR "Ooops! Too many entries in the memory map!\n"); |
103 | return; | |
104 | } | |
105 | ||
106 | e820.map[x].addr = start; | |
107 | e820.map[x].size = size; | |
108 | e820.map[x].type = type; | |
109 | e820.nr_map++; | |
110 | } | |
111 | ||
112 | void __init e820_print_map(char *who) | |
113 | { | |
114 | int i; | |
115 | ||
116 | for (i = 0; i < e820.nr_map; i++) { | |
117 | printk(KERN_INFO " %s: %016Lx - %016Lx ", who, | |
118 | (unsigned long long) e820.map[i].addr, | |
119 | (unsigned long long) | |
120 | (e820.map[i].addr + e820.map[i].size)); | |
121 | switch (e820.map[i].type) { | |
122 | case E820_RAM: | |
123 | printk(KERN_CONT "(usable)\n"); | |
124 | break; | |
125 | case E820_RESERVED: | |
126 | printk(KERN_CONT "(reserved)\n"); | |
127 | break; | |
128 | case E820_ACPI: | |
129 | printk(KERN_CONT "(ACPI data)\n"); | |
130 | break; | |
131 | case E820_NVS: | |
132 | printk(KERN_CONT "(ACPI NVS)\n"); | |
133 | break; | |
134 | default: | |
135 | printk(KERN_CONT "type %u\n", e820.map[i].type); | |
136 | break; | |
137 | } | |
138 | } | |
139 | } | |
140 | ||
141 | /* | |
142 | * Sanitize the BIOS e820 map. | |
143 | * | |
144 | * Some e820 responses include overlapping entries. The following | |
5b7eb2e9 PJ |
145 | * replaces the original e820 map with a new one, removing overlaps, |
146 | * and resolving conflicting memory types in favor of highest | |
147 | * numbered type. | |
b79cd8f1 | 148 | * |
5b7eb2e9 PJ |
149 | * The input parameter biosmap points to an array of 'struct |
150 | * e820entry' which on entry has elements in the range [0, *pnr_map) | |
151 | * valid, and which has space for up to max_nr_map entries. | |
152 | * On return, the resulting sanitized e820 map entries will be in | |
153 | * overwritten in the same location, starting at biosmap. | |
154 | * | |
155 | * The integer pointed to by pnr_map must be valid on entry (the | |
156 | * current number of valid entries located at biosmap) and will | |
157 | * be updated on return, with the new number of valid entries | |
158 | * (something no more than max_nr_map.) | |
159 | * | |
160 | * The return value from sanitize_e820_map() is zero if it | |
161 | * successfully 'sanitized' the map entries passed in, and is -1 | |
162 | * if it did nothing, which can happen if either of (1) it was | |
163 | * only passed one map entry, or (2) any of the input map entries | |
164 | * were invalid (start + size < start, meaning that the size was | |
165 | * so big the described memory range wrapped around through zero.) | |
166 | * | |
167 | * Visually we're performing the following | |
168 | * (1,2,3,4 = memory types)... | |
169 | * | |
170 | * Sample memory map (w/overlaps): | |
171 | * ____22__________________ | |
172 | * ______________________4_ | |
173 | * ____1111________________ | |
174 | * _44_____________________ | |
175 | * 11111111________________ | |
176 | * ____________________33__ | |
177 | * ___________44___________ | |
178 | * __________33333_________ | |
179 | * ______________22________ | |
180 | * ___________________2222_ | |
181 | * _________111111111______ | |
182 | * _____________________11_ | |
183 | * _________________4______ | |
184 | * | |
185 | * Sanitized equivalent (no overlap): | |
186 | * 1_______________________ | |
187 | * _44_____________________ | |
188 | * ___1____________________ | |
189 | * ____22__________________ | |
190 | * ______11________________ | |
191 | * _________1______________ | |
192 | * __________3_____________ | |
193 | * ___________44___________ | |
194 | * _____________33_________ | |
195 | * _______________2________ | |
196 | * ________________1_______ | |
197 | * _________________4______ | |
198 | * ___________________2____ | |
199 | * ____________________33__ | |
200 | * ______________________4_ | |
b79cd8f1 | 201 | */ |
5b7eb2e9 | 202 | |
c3965bd1 | 203 | int __init sanitize_e820_map(struct e820entry *biosmap, int max_nr_map, |
6e9bcc79 | 204 | int *pnr_map) |
b79cd8f1 YL |
205 | { |
206 | struct change_member { | |
207 | struct e820entry *pbios; /* pointer to original bios entry */ | |
208 | unsigned long long addr; /* address for this change point */ | |
209 | }; | |
028b7858 PJ |
210 | static struct change_member change_point_list[2*E820_X_MAX] __initdata; |
211 | static struct change_member *change_point[2*E820_X_MAX] __initdata; | |
212 | static struct e820entry *overlap_list[E820_X_MAX] __initdata; | |
213 | static struct e820entry new_bios[E820_X_MAX] __initdata; | |
b79cd8f1 YL |
214 | struct change_member *change_tmp; |
215 | unsigned long current_type, last_type; | |
216 | unsigned long long last_addr; | |
217 | int chgidx, still_changing; | |
218 | int overlap_entries; | |
219 | int new_bios_entry; | |
220 | int old_nr, new_nr, chg_nr; | |
221 | int i; | |
222 | ||
b79cd8f1 YL |
223 | /* if there's only one memory region, don't bother */ |
224 | if (*pnr_map < 2) | |
225 | return -1; | |
226 | ||
227 | old_nr = *pnr_map; | |
6e9bcc79 | 228 | BUG_ON(old_nr > max_nr_map); |
b79cd8f1 YL |
229 | |
230 | /* bail out if we find any unreasonable addresses in bios map */ | |
231 | for (i = 0; i < old_nr; i++) | |
232 | if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr) | |
233 | return -1; | |
234 | ||
235 | /* create pointers for initial change-point information (for sorting) */ | |
236 | for (i = 0; i < 2 * old_nr; i++) | |
237 | change_point[i] = &change_point_list[i]; | |
238 | ||
239 | /* record all known change-points (starting and ending addresses), | |
240 | omitting those that are for empty memory regions */ | |
241 | chgidx = 0; | |
242 | for (i = 0; i < old_nr; i++) { | |
243 | if (biosmap[i].size != 0) { | |
244 | change_point[chgidx]->addr = biosmap[i].addr; | |
245 | change_point[chgidx++]->pbios = &biosmap[i]; | |
246 | change_point[chgidx]->addr = biosmap[i].addr + | |
247 | biosmap[i].size; | |
248 | change_point[chgidx++]->pbios = &biosmap[i]; | |
249 | } | |
250 | } | |
251 | chg_nr = chgidx; | |
252 | ||
253 | /* sort change-point list by memory addresses (low -> high) */ | |
254 | still_changing = 1; | |
255 | while (still_changing) { | |
256 | still_changing = 0; | |
257 | for (i = 1; i < chg_nr; i++) { | |
258 | unsigned long long curaddr, lastaddr; | |
259 | unsigned long long curpbaddr, lastpbaddr; | |
260 | ||
261 | curaddr = change_point[i]->addr; | |
262 | lastaddr = change_point[i - 1]->addr; | |
263 | curpbaddr = change_point[i]->pbios->addr; | |
264 | lastpbaddr = change_point[i - 1]->pbios->addr; | |
265 | ||
266 | /* | |
267 | * swap entries, when: | |
268 | * | |
269 | * curaddr > lastaddr or | |
270 | * curaddr == lastaddr and curaddr == curpbaddr and | |
271 | * lastaddr != lastpbaddr | |
272 | */ | |
273 | if (curaddr < lastaddr || | |
274 | (curaddr == lastaddr && curaddr == curpbaddr && | |
275 | lastaddr != lastpbaddr)) { | |
276 | change_tmp = change_point[i]; | |
277 | change_point[i] = change_point[i-1]; | |
278 | change_point[i-1] = change_tmp; | |
279 | still_changing = 1; | |
280 | } | |
281 | } | |
282 | } | |
283 | ||
284 | /* create a new bios memory map, removing overlaps */ | |
285 | overlap_entries = 0; /* number of entries in the overlap table */ | |
286 | new_bios_entry = 0; /* index for creating new bios map entries */ | |
287 | last_type = 0; /* start with undefined memory type */ | |
288 | last_addr = 0; /* start with 0 as last starting address */ | |
289 | ||
290 | /* loop through change-points, determining affect on the new bios map */ | |
291 | for (chgidx = 0; chgidx < chg_nr; chgidx++) { | |
292 | /* keep track of all overlapping bios entries */ | |
293 | if (change_point[chgidx]->addr == | |
294 | change_point[chgidx]->pbios->addr) { | |
295 | /* | |
296 | * add map entry to overlap list (> 1 entry | |
297 | * implies an overlap) | |
298 | */ | |
299 | overlap_list[overlap_entries++] = | |
300 | change_point[chgidx]->pbios; | |
301 | } else { | |
302 | /* | |
303 | * remove entry from list (order independent, | |
304 | * so swap with last) | |
305 | */ | |
306 | for (i = 0; i < overlap_entries; i++) { | |
307 | if (overlap_list[i] == | |
308 | change_point[chgidx]->pbios) | |
309 | overlap_list[i] = | |
310 | overlap_list[overlap_entries-1]; | |
311 | } | |
312 | overlap_entries--; | |
313 | } | |
314 | /* | |
315 | * if there are overlapping entries, decide which | |
316 | * "type" to use (larger value takes precedence -- | |
317 | * 1=usable, 2,3,4,4+=unusable) | |
318 | */ | |
319 | current_type = 0; | |
320 | for (i = 0; i < overlap_entries; i++) | |
321 | if (overlap_list[i]->type > current_type) | |
322 | current_type = overlap_list[i]->type; | |
323 | /* | |
324 | * continue building up new bios map based on this | |
325 | * information | |
326 | */ | |
327 | if (current_type != last_type) { | |
328 | if (last_type != 0) { | |
329 | new_bios[new_bios_entry].size = | |
330 | change_point[chgidx]->addr - last_addr; | |
331 | /* | |
332 | * move forward only if the new size | |
333 | * was non-zero | |
334 | */ | |
335 | if (new_bios[new_bios_entry].size != 0) | |
336 | /* | |
337 | * no more space left for new | |
338 | * bios entries ? | |
339 | */ | |
c3965bd1 | 340 | if (++new_bios_entry >= max_nr_map) |
b79cd8f1 YL |
341 | break; |
342 | } | |
343 | if (current_type != 0) { | |
344 | new_bios[new_bios_entry].addr = | |
345 | change_point[chgidx]->addr; | |
346 | new_bios[new_bios_entry].type = current_type; | |
347 | last_addr = change_point[chgidx]->addr; | |
348 | } | |
349 | last_type = current_type; | |
350 | } | |
351 | } | |
352 | /* retain count for new bios entries */ | |
353 | new_nr = new_bios_entry; | |
354 | ||
355 | /* copy new bios mapping into original location */ | |
356 | memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry)); | |
357 | *pnr_map = new_nr; | |
358 | ||
359 | return 0; | |
360 | } | |
361 | ||
362 | /* | |
363 | * Copy the BIOS e820 map into a safe place. | |
364 | * | |
365 | * Sanity-check it while we're at it.. | |
366 | * | |
367 | * If we're lucky and live on a modern system, the setup code | |
368 | * will have given us a memory map that we can use to properly | |
369 | * set up memory. If we aren't, we'll fake a memory map. | |
370 | */ | |
371 | int __init copy_e820_map(struct e820entry *biosmap, int nr_map) | |
372 | { | |
373 | /* Only one memory region (or negative)? Ignore it */ | |
374 | if (nr_map < 2) | |
375 | return -1; | |
376 | ||
377 | do { | |
378 | u64 start = biosmap->addr; | |
379 | u64 size = biosmap->size; | |
380 | u64 end = start + size; | |
381 | u32 type = biosmap->type; | |
382 | ||
383 | /* Overflow in 64 bits? Ignore the memory map. */ | |
384 | if (start > end) | |
385 | return -1; | |
386 | ||
387 | add_memory_region(start, size, type); | |
388 | } while (biosmap++, --nr_map); | |
389 | return 0; | |
390 | } | |
391 | ||
392 | u64 __init update_memory_range(u64 start, u64 size, unsigned old_type, | |
393 | unsigned new_type) | |
394 | { | |
395 | int i; | |
396 | u64 real_updated_size = 0; | |
397 | ||
398 | BUG_ON(old_type == new_type); | |
399 | ||
400 | for (i = 0; i < e820.nr_map; i++) { | |
401 | struct e820entry *ei = &e820.map[i]; | |
402 | u64 final_start, final_end; | |
403 | if (ei->type != old_type) | |
404 | continue; | |
405 | /* totally covered? */ | |
406 | if (ei->addr >= start && | |
407 | (ei->addr + ei->size) <= (start + size)) { | |
408 | ei->type = new_type; | |
409 | real_updated_size += ei->size; | |
410 | continue; | |
411 | } | |
412 | /* partially covered */ | |
413 | final_start = max(start, ei->addr); | |
414 | final_end = min(start + size, ei->addr + ei->size); | |
415 | if (final_start >= final_end) | |
416 | continue; | |
417 | add_memory_region(final_start, final_end - final_start, | |
418 | new_type); | |
419 | real_updated_size += final_end - final_start; | |
420 | } | |
421 | return real_updated_size; | |
422 | } | |
423 | ||
424 | void __init update_e820(void) | |
425 | { | |
6e9bcc79 | 426 | int nr_map; |
b79cd8f1 YL |
427 | |
428 | nr_map = e820.nr_map; | |
c3965bd1 | 429 | if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr_map)) |
b79cd8f1 YL |
430 | return; |
431 | e820.nr_map = nr_map; | |
432 | printk(KERN_INFO "modified physical RAM map:\n"); | |
433 | e820_print_map("modified"); | |
434 | } | |
435 | ||
436 | /* | |
437 | * Search for the biggest gap in the low 32 bits of the e820 | |
438 | * memory space. We pass this space to PCI to assign MMIO resources | |
439 | * for hotplug or unconfigured devices in. | |
440 | * Hopefully the BIOS let enough space left. | |
441 | */ | |
442 | __init void e820_setup_gap(void) | |
443 | { | |
444 | unsigned long gapstart, gapsize, round; | |
445 | unsigned long long last; | |
446 | int i; | |
447 | int found = 0; | |
448 | ||
449 | last = 0x100000000ull; | |
450 | gapstart = 0x10000000; | |
451 | gapsize = 0x400000; | |
452 | i = e820.nr_map; | |
453 | while (--i >= 0) { | |
454 | unsigned long long start = e820.map[i].addr; | |
455 | unsigned long long end = start + e820.map[i].size; | |
456 | ||
457 | /* | |
458 | * Since "last" is at most 4GB, we know we'll | |
459 | * fit in 32 bits if this condition is true | |
460 | */ | |
461 | if (last > end) { | |
462 | unsigned long gap = last - end; | |
463 | ||
464 | if (gap > gapsize) { | |
465 | gapsize = gap; | |
466 | gapstart = end; | |
467 | found = 1; | |
468 | } | |
469 | } | |
470 | if (start < last) | |
471 | last = start; | |
472 | } | |
473 | ||
474 | #ifdef CONFIG_X86_64 | |
475 | if (!found) { | |
476 | gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024; | |
477 | printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit " | |
478 | "address range\n" | |
479 | KERN_ERR "PCI: Unassigned devices with 32bit resource " | |
480 | "registers may break!\n"); | |
481 | } | |
482 | #endif | |
483 | ||
484 | /* | |
485 | * See how much we want to round up: start off with | |
486 | * rounding to the next 1MB area. | |
487 | */ | |
488 | round = 0x100000; | |
489 | while ((gapsize >> 4) > round) | |
490 | round += round; | |
491 | /* Fun with two's complement */ | |
492 | pci_mem_start = (gapstart + round) & -round; | |
493 | ||
494 | printk(KERN_INFO | |
495 | "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n", | |
496 | pci_mem_start, gapstart, gapsize); | |
497 | } | |
498 | ||
bf62f398 YL |
499 | #if defined(CONFIG_X86_64) || \ |
500 | (defined(CONFIG_X86_32) && defined(CONFIG_HIBERNATION)) | |
501 | /** | |
502 | * Find the ranges of physical addresses that do not correspond to | |
503 | * e820 RAM areas and mark the corresponding pages as nosave for | |
504 | * hibernation (32 bit) or software suspend and suspend to RAM (64 bit). | |
505 | * | |
506 | * This function requires the e820 map to be sorted and without any | |
507 | * overlapping entries and assumes the first e820 area to be RAM. | |
508 | */ | |
509 | void __init e820_mark_nosave_regions(unsigned long limit_pfn) | |
510 | { | |
511 | int i; | |
512 | unsigned long pfn; | |
513 | ||
514 | pfn = PFN_DOWN(e820.map[0].addr + e820.map[0].size); | |
515 | for (i = 1; i < e820.nr_map; i++) { | |
516 | struct e820entry *ei = &e820.map[i]; | |
517 | ||
518 | if (pfn < PFN_UP(ei->addr)) | |
519 | register_nosave_region(pfn, PFN_UP(ei->addr)); | |
520 | ||
521 | pfn = PFN_DOWN(ei->addr + ei->size); | |
522 | if (ei->type != E820_RAM) | |
523 | register_nosave_region(PFN_UP(ei->addr), pfn); | |
524 | ||
525 | if (pfn >= limit_pfn) | |
526 | break; | |
527 | } | |
528 | } | |
529 | #endif | |
a4c81cf6 YL |
530 | |
531 | /* | |
532 | * Early reserved memory areas. | |
533 | */ | |
534 | #define MAX_EARLY_RES 20 | |
535 | ||
536 | struct early_res { | |
537 | u64 start, end; | |
538 | char name[16]; | |
539 | }; | |
540 | static struct early_res early_res[MAX_EARLY_RES] __initdata = { | |
541 | { 0, PAGE_SIZE, "BIOS data page" }, /* BIOS data page */ | |
542 | #if defined(CONFIG_X86_64) && defined(CONFIG_X86_TRAMPOLINE) | |
543 | { TRAMPOLINE_BASE, TRAMPOLINE_BASE + 2 * PAGE_SIZE, "TRAMPOLINE" }, | |
544 | #endif | |
545 | #if defined(CONFIG_X86_32) && defined(CONFIG_SMP) | |
546 | /* | |
547 | * But first pinch a few for the stack/trampoline stuff | |
548 | * FIXME: Don't need the extra page at 4K, but need to fix | |
549 | * trampoline before removing it. (see the GDT stuff) | |
550 | */ | |
551 | { PAGE_SIZE, PAGE_SIZE + PAGE_SIZE, "EX TRAMPOLINE" }, | |
552 | /* | |
553 | * Has to be in very low memory so we can execute | |
554 | * real-mode AP code. | |
555 | */ | |
556 | { TRAMPOLINE_BASE, TRAMPOLINE_BASE + PAGE_SIZE, "TRAMPOLINE" }, | |
557 | #endif | |
558 | {} | |
559 | }; | |
560 | ||
561 | void __init reserve_early(u64 start, u64 end, char *name) | |
562 | { | |
563 | int i; | |
564 | struct early_res *r; | |
565 | for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) { | |
566 | r = &early_res[i]; | |
567 | if (end > r->start && start < r->end) | |
568 | panic("Overlapping early reservations %llx-%llx %s to %llx-%llx %s\n", | |
569 | start, end - 1, name?name:"", r->start, | |
570 | r->end - 1, r->name); | |
571 | } | |
572 | if (i >= MAX_EARLY_RES) | |
573 | panic("Too many early reservations"); | |
574 | r = &early_res[i]; | |
575 | r->start = start; | |
576 | r->end = end; | |
577 | if (name) | |
578 | strncpy(r->name, name, sizeof(r->name) - 1); | |
579 | } | |
580 | ||
581 | void __init free_early(u64 start, u64 end) | |
582 | { | |
583 | struct early_res *r; | |
584 | int i, j; | |
585 | ||
586 | for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) { | |
587 | r = &early_res[i]; | |
588 | if (start == r->start && end == r->end) | |
589 | break; | |
590 | } | |
591 | if (i >= MAX_EARLY_RES || !early_res[i].end) | |
592 | panic("free_early on not reserved area: %llx-%llx!", | |
593 | start, end); | |
594 | ||
595 | for (j = i + 1; j < MAX_EARLY_RES && early_res[j].end; j++) | |
596 | ; | |
597 | ||
598 | memmove(&early_res[i], &early_res[i + 1], | |
599 | (j - 1 - i) * sizeof(struct early_res)); | |
600 | ||
601 | early_res[j - 1].end = 0; | |
602 | } | |
603 | ||
604 | void __init early_res_to_bootmem(u64 start, u64 end) | |
605 | { | |
606 | int i; | |
607 | u64 final_start, final_end; | |
608 | for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) { | |
609 | struct early_res *r = &early_res[i]; | |
610 | final_start = max(start, r->start); | |
611 | final_end = min(end, r->end); | |
612 | if (final_start >= final_end) | |
613 | continue; | |
614 | printk(KERN_INFO " early res: %d [%llx-%llx] %s\n", i, | |
615 | final_start, final_end - 1, r->name); | |
616 | #ifdef CONFIG_X86_64 | |
617 | reserve_bootmem_generic(final_start, final_end - final_start); | |
618 | #else | |
619 | reserve_bootmem(final_start, final_end - final_start, | |
620 | BOOTMEM_DEFAULT); | |
621 | #endif | |
622 | } | |
623 | } | |
624 | ||
625 | /* Check for already reserved areas */ | |
626 | static inline int __init bad_addr(u64 *addrp, u64 size, u64 align) | |
627 | { | |
628 | int i; | |
629 | u64 addr = *addrp, last; | |
630 | int changed = 0; | |
631 | again: | |
632 | last = addr + size; | |
633 | for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) { | |
634 | struct early_res *r = &early_res[i]; | |
635 | if (last >= r->start && addr < r->end) { | |
636 | *addrp = addr = round_up(r->end, align); | |
637 | changed = 1; | |
638 | goto again; | |
639 | } | |
640 | } | |
641 | return changed; | |
642 | } | |
643 | ||
644 | /* Check for already reserved areas */ | |
645 | static inline int __init bad_addr_size(u64 *addrp, u64 *sizep, u64 align) | |
646 | { | |
647 | int i; | |
648 | u64 addr = *addrp, last; | |
649 | u64 size = *sizep; | |
650 | int changed = 0; | |
651 | again: | |
652 | last = addr + size; | |
653 | for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) { | |
654 | struct early_res *r = &early_res[i]; | |
655 | if (last > r->start && addr < r->start) { | |
656 | size = r->start - addr; | |
657 | changed = 1; | |
658 | goto again; | |
659 | } | |
660 | if (last > r->end && addr < r->end) { | |
661 | addr = round_up(r->end, align); | |
662 | size = last - addr; | |
663 | changed = 1; | |
664 | goto again; | |
665 | } | |
666 | if (last <= r->end && addr >= r->start) { | |
667 | (*sizep)++; | |
668 | return 0; | |
669 | } | |
670 | } | |
671 | if (changed) { | |
672 | *addrp = addr; | |
673 | *sizep = size; | |
674 | } | |
675 | return changed; | |
676 | } | |
677 | ||
678 | /* | |
679 | * Find a free area with specified alignment in a specific range. | |
680 | */ | |
681 | u64 __init find_e820_area(u64 start, u64 end, u64 size, u64 align) | |
682 | { | |
683 | int i; | |
684 | ||
685 | for (i = 0; i < e820.nr_map; i++) { | |
686 | struct e820entry *ei = &e820.map[i]; | |
687 | u64 addr, last; | |
688 | u64 ei_last; | |
689 | ||
690 | if (ei->type != E820_RAM) | |
691 | continue; | |
692 | addr = round_up(ei->addr, align); | |
693 | ei_last = ei->addr + ei->size; | |
694 | if (addr < start) | |
695 | addr = round_up(start, align); | |
696 | if (addr >= ei_last) | |
697 | continue; | |
698 | while (bad_addr(&addr, size, align) && addr+size <= ei_last) | |
699 | ; | |
700 | last = addr + size; | |
701 | if (last > ei_last) | |
702 | continue; | |
703 | if (last > end) | |
704 | continue; | |
705 | return addr; | |
706 | } | |
707 | return -1ULL; | |
708 | } | |
709 | ||
710 | /* | |
711 | * Find next free range after *start | |
712 | */ | |
713 | u64 __init find_e820_area_size(u64 start, u64 *sizep, u64 align) | |
714 | { | |
715 | int i; | |
716 | ||
717 | for (i = 0; i < e820.nr_map; i++) { | |
718 | struct e820entry *ei = &e820.map[i]; | |
719 | u64 addr, last; | |
720 | u64 ei_last; | |
721 | ||
722 | if (ei->type != E820_RAM) | |
723 | continue; | |
724 | addr = round_up(ei->addr, align); | |
725 | ei_last = ei->addr + ei->size; | |
726 | if (addr < start) | |
727 | addr = round_up(start, align); | |
728 | if (addr >= ei_last) | |
729 | continue; | |
730 | *sizep = ei_last - addr; | |
731 | while (bad_addr_size(&addr, sizep, align) && | |
732 | addr + *sizep <= ei_last) | |
733 | ; | |
734 | last = addr + *sizep; | |
735 | if (last > ei_last) | |
736 | continue; | |
737 | return addr; | |
738 | } | |
739 | return -1UL; | |
740 | ||
741 | } | |
2944e16b YL |
742 | |
743 | /* | |
744 | * pre allocated 4k and reserved it in e820 | |
745 | */ | |
746 | u64 __init early_reserve_e820(u64 startt, u64 sizet, u64 align) | |
747 | { | |
748 | u64 size = 0; | |
749 | u64 addr; | |
750 | u64 start; | |
751 | ||
752 | start = startt; | |
753 | while (size < sizet) | |
754 | start = find_e820_area_size(start, &size, align); | |
755 | ||
756 | if (size < sizet) | |
757 | return 0; | |
758 | ||
759 | addr = round_down(start + size - sizet, align); | |
760 | update_memory_range(addr, sizet, E820_RAM, E820_RESERVED); | |
761 | printk(KERN_INFO "update e820 for early_reserve_e820\n"); | |
762 | update_e820(); | |
763 | ||
764 | return addr; | |
765 | } | |
766 | ||
ee0c80fa YL |
767 | #ifdef CONFIG_X86_32 |
768 | # ifdef CONFIG_X86_PAE | |
769 | # define MAX_ARCH_PFN (1ULL<<(36-PAGE_SHIFT)) | |
770 | # else | |
771 | # define MAX_ARCH_PFN (1ULL<<(32-PAGE_SHIFT)) | |
772 | # endif | |
773 | #else /* CONFIG_X86_32 */ | |
774 | # define MAX_ARCH_PFN MAXMEM<<PAGE_SHIFT | |
775 | #endif | |
776 | ||
777 | /* | |
778 | * Last pfn which the user wants to use. | |
779 | */ | |
780 | unsigned long __initdata end_user_pfn = MAX_ARCH_PFN; | |
781 | ||
782 | /* | |
783 | * Find the highest page frame number we have available | |
784 | */ | |
785 | unsigned long __init e820_end_of_ram(void) | |
786 | { | |
787 | unsigned long last_pfn; | |
788 | unsigned long max_arch_pfn = MAX_ARCH_PFN; | |
789 | ||
790 | last_pfn = find_max_pfn_with_active_regions(); | |
791 | ||
792 | if (last_pfn > max_arch_pfn) | |
793 | last_pfn = max_arch_pfn; | |
794 | if (last_pfn > end_user_pfn) | |
795 | last_pfn = end_user_pfn; | |
796 | ||
797 | printk(KERN_INFO "last_pfn = %lu max_arch_pfn = %lu\n", | |
798 | last_pfn, max_arch_pfn); | |
799 | return last_pfn; | |
800 | } | |
801 | ||
802 | /* | |
803 | * Finds an active region in the address range from start_pfn to last_pfn and | |
804 | * returns its range in ei_startpfn and ei_endpfn for the e820 entry. | |
805 | */ | |
806 | int __init e820_find_active_region(const struct e820entry *ei, | |
807 | unsigned long start_pfn, | |
808 | unsigned long last_pfn, | |
809 | unsigned long *ei_startpfn, | |
810 | unsigned long *ei_endpfn) | |
811 | { | |
812 | u64 align = PAGE_SIZE; | |
813 | ||
814 | *ei_startpfn = round_up(ei->addr, align) >> PAGE_SHIFT; | |
815 | *ei_endpfn = round_down(ei->addr + ei->size, align) >> PAGE_SHIFT; | |
816 | ||
817 | /* Skip map entries smaller than a page */ | |
818 | if (*ei_startpfn >= *ei_endpfn) | |
819 | return 0; | |
820 | ||
821 | /* Skip if map is outside the node */ | |
822 | if (ei->type != E820_RAM || *ei_endpfn <= start_pfn || | |
823 | *ei_startpfn >= last_pfn) | |
824 | return 0; | |
825 | ||
826 | /* Check for overlaps */ | |
827 | if (*ei_startpfn < start_pfn) | |
828 | *ei_startpfn = start_pfn; | |
829 | if (*ei_endpfn > last_pfn) | |
830 | *ei_endpfn = last_pfn; | |
831 | ||
832 | /* Obey end_user_pfn to save on memmap */ | |
833 | if (*ei_startpfn >= end_user_pfn) | |
834 | return 0; | |
835 | if (*ei_endpfn > end_user_pfn) | |
836 | *ei_endpfn = end_user_pfn; | |
837 | ||
838 | return 1; | |
839 | } | |
840 | ||
841 | /* Walk the e820 map and register active regions within a node */ | |
842 | void __init e820_register_active_regions(int nid, unsigned long start_pfn, | |
843 | unsigned long last_pfn) | |
844 | { | |
845 | unsigned long ei_startpfn; | |
846 | unsigned long ei_endpfn; | |
847 | int i; | |
848 | ||
849 | for (i = 0; i < e820.nr_map; i++) | |
850 | if (e820_find_active_region(&e820.map[i], | |
851 | start_pfn, last_pfn, | |
852 | &ei_startpfn, &ei_endpfn)) | |
853 | add_active_range(nid, ei_startpfn, ei_endpfn); | |
854 | } | |
855 | ||
856 | /* | |
857 | * Find the hole size (in bytes) in the memory range. | |
858 | * @start: starting address of the memory range to scan | |
859 | * @end: ending address of the memory range to scan | |
860 | */ | |
861 | u64 __init e820_hole_size(u64 start, u64 end) | |
862 | { | |
863 | unsigned long start_pfn = start >> PAGE_SHIFT; | |
864 | unsigned long last_pfn = end >> PAGE_SHIFT; | |
865 | unsigned long ei_startpfn, ei_endpfn, ram = 0; | |
866 | int i; | |
867 | ||
868 | for (i = 0; i < e820.nr_map; i++) { | |
869 | if (e820_find_active_region(&e820.map[i], | |
870 | start_pfn, last_pfn, | |
871 | &ei_startpfn, &ei_endpfn)) | |
872 | ram += ei_endpfn - ei_startpfn; | |
873 | } | |
874 | return end - start - ((u64)ram << PAGE_SHIFT); | |
875 | } |