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 | */ | |
d0be6bde | 97 | void __init e820_add_region(u64 start, u64 size, int type) |
b79cd8f1 YL |
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 | }; | |
157fabf0 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 | ||
8c5beb50 HY |
362 | static int __init __copy_e820_map(struct e820entry *biosmap, int nr_map) |
363 | { | |
364 | while (nr_map) { | |
365 | u64 start = biosmap->addr; | |
366 | u64 size = biosmap->size; | |
367 | u64 end = start + size; | |
368 | u32 type = biosmap->type; | |
369 | ||
370 | /* Overflow in 64 bits? Ignore the memory map. */ | |
371 | if (start > end) | |
372 | return -1; | |
373 | ||
374 | e820_add_region(start, size, type); | |
375 | ||
376 | biosmap++; | |
377 | nr_map--; | |
378 | } | |
379 | return 0; | |
380 | } | |
381 | ||
b79cd8f1 YL |
382 | /* |
383 | * Copy the BIOS e820 map into a safe place. | |
384 | * | |
385 | * Sanity-check it while we're at it.. | |
386 | * | |
387 | * If we're lucky and live on a modern system, the setup code | |
388 | * will have given us a memory map that we can use to properly | |
389 | * set up memory. If we aren't, we'll fake a memory map. | |
390 | */ | |
391 | int __init copy_e820_map(struct e820entry *biosmap, int nr_map) | |
392 | { | |
393 | /* Only one memory region (or negative)? Ignore it */ | |
394 | if (nr_map < 2) | |
395 | return -1; | |
396 | ||
8c5beb50 | 397 | return __copy_e820_map(biosmap, nr_map); |
b79cd8f1 YL |
398 | } |
399 | ||
d0be6bde | 400 | u64 __init e820_update_range(u64 start, u64 size, unsigned old_type, |
b79cd8f1 YL |
401 | unsigned new_type) |
402 | { | |
403 | int i; | |
404 | u64 real_updated_size = 0; | |
405 | ||
406 | BUG_ON(old_type == new_type); | |
407 | ||
408 | for (i = 0; i < e820.nr_map; i++) { | |
409 | struct e820entry *ei = &e820.map[i]; | |
410 | u64 final_start, final_end; | |
411 | if (ei->type != old_type) | |
412 | continue; | |
413 | /* totally covered? */ | |
414 | if (ei->addr >= start && | |
415 | (ei->addr + ei->size) <= (start + size)) { | |
416 | ei->type = new_type; | |
417 | real_updated_size += ei->size; | |
418 | continue; | |
419 | } | |
420 | /* partially covered */ | |
421 | final_start = max(start, ei->addr); | |
422 | final_end = min(start + size, ei->addr + ei->size); | |
423 | if (final_start >= final_end) | |
424 | continue; | |
d0be6bde | 425 | e820_add_region(final_start, final_end - final_start, |
b79cd8f1 YL |
426 | new_type); |
427 | real_updated_size += final_end - final_start; | |
428 | } | |
429 | return real_updated_size; | |
430 | } | |
431 | ||
7a1fd986 YL |
432 | /* make e820 not cover the range */ |
433 | u64 __init e820_remove_range(u64 start, u64 size, unsigned old_type, | |
434 | int checktype) | |
435 | { | |
436 | int i; | |
437 | u64 real_removed_size = 0; | |
438 | ||
439 | for (i = 0; i < e820.nr_map; i++) { | |
440 | struct e820entry *ei = &e820.map[i]; | |
441 | u64 final_start, final_end; | |
442 | ||
443 | if (checktype && ei->type != old_type) | |
444 | continue; | |
445 | /* totally covered? */ | |
446 | if (ei->addr >= start && | |
447 | (ei->addr + ei->size) <= (start + size)) { | |
448 | real_removed_size += ei->size; | |
449 | memset(ei, 0, sizeof(struct e820entry)); | |
450 | continue; | |
451 | } | |
452 | /* partially covered */ | |
453 | final_start = max(start, ei->addr); | |
454 | final_end = min(start + size, ei->addr + ei->size); | |
455 | if (final_start >= final_end) | |
456 | continue; | |
457 | real_removed_size += final_end - final_start; | |
458 | ||
459 | ei->size -= final_end - final_start; | |
460 | if (ei->addr < final_start) | |
461 | continue; | |
462 | ei->addr = final_end; | |
463 | } | |
464 | return real_removed_size; | |
465 | } | |
466 | ||
b79cd8f1 YL |
467 | void __init update_e820(void) |
468 | { | |
6e9bcc79 | 469 | int nr_map; |
b79cd8f1 YL |
470 | |
471 | nr_map = e820.nr_map; | |
c3965bd1 | 472 | if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr_map)) |
b79cd8f1 YL |
473 | return; |
474 | e820.nr_map = nr_map; | |
475 | printk(KERN_INFO "modified physical RAM map:\n"); | |
476 | e820_print_map("modified"); | |
477 | } | |
478 | ||
479 | /* | |
480 | * Search for the biggest gap in the low 32 bits of the e820 | |
481 | * memory space. We pass this space to PCI to assign MMIO resources | |
482 | * for hotplug or unconfigured devices in. | |
483 | * Hopefully the BIOS let enough space left. | |
484 | */ | |
485 | __init void e820_setup_gap(void) | |
486 | { | |
487 | unsigned long gapstart, gapsize, round; | |
488 | unsigned long long last; | |
489 | int i; | |
490 | int found = 0; | |
491 | ||
492 | last = 0x100000000ull; | |
493 | gapstart = 0x10000000; | |
494 | gapsize = 0x400000; | |
495 | i = e820.nr_map; | |
496 | while (--i >= 0) { | |
497 | unsigned long long start = e820.map[i].addr; | |
498 | unsigned long long end = start + e820.map[i].size; | |
499 | ||
500 | /* | |
501 | * Since "last" is at most 4GB, we know we'll | |
502 | * fit in 32 bits if this condition is true | |
503 | */ | |
504 | if (last > end) { | |
505 | unsigned long gap = last - end; | |
506 | ||
507 | if (gap > gapsize) { | |
508 | gapsize = gap; | |
509 | gapstart = end; | |
510 | found = 1; | |
511 | } | |
512 | } | |
513 | if (start < last) | |
514 | last = start; | |
515 | } | |
516 | ||
517 | #ifdef CONFIG_X86_64 | |
518 | if (!found) { | |
519 | gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024; | |
520 | printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit " | |
521 | "address range\n" | |
522 | KERN_ERR "PCI: Unassigned devices with 32bit resource " | |
523 | "registers may break!\n"); | |
524 | } | |
525 | #endif | |
526 | ||
527 | /* | |
528 | * See how much we want to round up: start off with | |
529 | * rounding to the next 1MB area. | |
530 | */ | |
531 | round = 0x100000; | |
532 | while ((gapsize >> 4) > round) | |
533 | round += round; | |
534 | /* Fun with two's complement */ | |
535 | pci_mem_start = (gapstart + round) & -round; | |
536 | ||
537 | printk(KERN_INFO | |
538 | "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n", | |
539 | pci_mem_start, gapstart, gapsize); | |
540 | } | |
541 | ||
8c5beb50 HY |
542 | /** |
543 | * Because of the size limitation of struct boot_params, only first | |
544 | * 128 E820 memory entries are passed to kernel via | |
545 | * boot_params.e820_map, others are passed via SETUP_E820_EXT node of | |
546 | * linked list of struct setup_data, which is parsed here. | |
547 | */ | |
548 | void __init parse_e820_ext(struct setup_data *sdata, unsigned long pa_data) | |
549 | { | |
550 | u32 map_len; | |
551 | int entries; | |
552 | struct e820entry *extmap; | |
553 | ||
554 | entries = sdata->len / sizeof(struct e820entry); | |
555 | map_len = sdata->len + sizeof(struct setup_data); | |
556 | if (map_len > PAGE_SIZE) | |
557 | sdata = early_ioremap(pa_data, map_len); | |
558 | extmap = (struct e820entry *)(sdata->data); | |
559 | __copy_e820_map(extmap, entries); | |
560 | sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map); | |
561 | if (map_len > PAGE_SIZE) | |
562 | early_iounmap(sdata, map_len); | |
563 | printk(KERN_INFO "extended physical RAM map:\n"); | |
564 | e820_print_map("extended"); | |
565 | } | |
566 | ||
bf62f398 YL |
567 | #if defined(CONFIG_X86_64) || \ |
568 | (defined(CONFIG_X86_32) && defined(CONFIG_HIBERNATION)) | |
569 | /** | |
570 | * Find the ranges of physical addresses that do not correspond to | |
571 | * e820 RAM areas and mark the corresponding pages as nosave for | |
572 | * hibernation (32 bit) or software suspend and suspend to RAM (64 bit). | |
573 | * | |
574 | * This function requires the e820 map to be sorted and without any | |
575 | * overlapping entries and assumes the first e820 area to be RAM. | |
576 | */ | |
577 | void __init e820_mark_nosave_regions(unsigned long limit_pfn) | |
578 | { | |
579 | int i; | |
580 | unsigned long pfn; | |
581 | ||
582 | pfn = PFN_DOWN(e820.map[0].addr + e820.map[0].size); | |
583 | for (i = 1; i < e820.nr_map; i++) { | |
584 | struct e820entry *ei = &e820.map[i]; | |
585 | ||
586 | if (pfn < PFN_UP(ei->addr)) | |
587 | register_nosave_region(pfn, PFN_UP(ei->addr)); | |
588 | ||
589 | pfn = PFN_DOWN(ei->addr + ei->size); | |
590 | if (ei->type != E820_RAM) | |
591 | register_nosave_region(PFN_UP(ei->addr), pfn); | |
592 | ||
593 | if (pfn >= limit_pfn) | |
594 | break; | |
595 | } | |
596 | } | |
597 | #endif | |
a4c81cf6 YL |
598 | |
599 | /* | |
600 | * Early reserved memory areas. | |
601 | */ | |
602 | #define MAX_EARLY_RES 20 | |
603 | ||
604 | struct early_res { | |
605 | u64 start, end; | |
606 | char name[16]; | |
607 | }; | |
608 | static struct early_res early_res[MAX_EARLY_RES] __initdata = { | |
609 | { 0, PAGE_SIZE, "BIOS data page" }, /* BIOS data page */ | |
610 | #if defined(CONFIG_X86_64) && defined(CONFIG_X86_TRAMPOLINE) | |
611 | { TRAMPOLINE_BASE, TRAMPOLINE_BASE + 2 * PAGE_SIZE, "TRAMPOLINE" }, | |
612 | #endif | |
613 | #if defined(CONFIG_X86_32) && defined(CONFIG_SMP) | |
614 | /* | |
615 | * But first pinch a few for the stack/trampoline stuff | |
616 | * FIXME: Don't need the extra page at 4K, but need to fix | |
617 | * trampoline before removing it. (see the GDT stuff) | |
618 | */ | |
619 | { PAGE_SIZE, PAGE_SIZE + PAGE_SIZE, "EX TRAMPOLINE" }, | |
620 | /* | |
621 | * Has to be in very low memory so we can execute | |
622 | * real-mode AP code. | |
623 | */ | |
624 | { TRAMPOLINE_BASE, TRAMPOLINE_BASE + PAGE_SIZE, "TRAMPOLINE" }, | |
625 | #endif | |
626 | {} | |
627 | }; | |
628 | ||
d3fbe5ea | 629 | static int __init find_overlapped_early(u64 start, u64 end) |
a4c81cf6 YL |
630 | { |
631 | int i; | |
632 | struct early_res *r; | |
d3fbe5ea | 633 | |
a4c81cf6 YL |
634 | for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) { |
635 | r = &early_res[i]; | |
636 | if (end > r->start && start < r->end) | |
d3fbe5ea | 637 | break; |
a4c81cf6 | 638 | } |
d3fbe5ea HY |
639 | |
640 | return i; | |
641 | } | |
642 | ||
643 | void __init reserve_early(u64 start, u64 end, char *name) | |
644 | { | |
645 | int i; | |
646 | struct early_res *r; | |
647 | ||
648 | i = find_overlapped_early(start, end); | |
a4c81cf6 YL |
649 | if (i >= MAX_EARLY_RES) |
650 | panic("Too many early reservations"); | |
651 | r = &early_res[i]; | |
d3fbe5ea HY |
652 | if (r->end) |
653 | panic("Overlapping early reservations " | |
654 | "%llx-%llx %s to %llx-%llx %s\n", | |
655 | start, end - 1, name?name:"", r->start, | |
656 | r->end - 1, r->name); | |
a4c81cf6 YL |
657 | r->start = start; |
658 | r->end = end; | |
659 | if (name) | |
660 | strncpy(r->name, name, sizeof(r->name) - 1); | |
661 | } | |
662 | ||
663 | void __init free_early(u64 start, u64 end) | |
664 | { | |
665 | struct early_res *r; | |
666 | int i, j; | |
667 | ||
d3fbe5ea HY |
668 | i = find_overlapped_early(start, end); |
669 | r = &early_res[i]; | |
670 | if (i >= MAX_EARLY_RES || r->end != end || r->start != start) | |
a4c81cf6 | 671 | panic("free_early on not reserved area: %llx-%llx!", |
d3fbe5ea | 672 | start, end - 1); |
a4c81cf6 YL |
673 | |
674 | for (j = i + 1; j < MAX_EARLY_RES && early_res[j].end; j++) | |
675 | ; | |
676 | ||
677 | memmove(&early_res[i], &early_res[i + 1], | |
678 | (j - 1 - i) * sizeof(struct early_res)); | |
679 | ||
680 | early_res[j - 1].end = 0; | |
681 | } | |
682 | ||
683 | void __init early_res_to_bootmem(u64 start, u64 end) | |
684 | { | |
685 | int i; | |
686 | u64 final_start, final_end; | |
687 | for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) { | |
688 | struct early_res *r = &early_res[i]; | |
689 | final_start = max(start, r->start); | |
690 | final_end = min(end, r->end); | |
691 | if (final_start >= final_end) | |
692 | continue; | |
693 | printk(KERN_INFO " early res: %d [%llx-%llx] %s\n", i, | |
694 | final_start, final_end - 1, r->name); | |
d2dbf343 | 695 | reserve_bootmem_generic(final_start, final_end - final_start, |
a4c81cf6 | 696 | BOOTMEM_DEFAULT); |
a4c81cf6 YL |
697 | } |
698 | } | |
699 | ||
700 | /* Check for already reserved areas */ | |
701 | static inline int __init bad_addr(u64 *addrp, u64 size, u64 align) | |
702 | { | |
703 | int i; | |
d3fbe5ea | 704 | u64 addr = *addrp; |
a4c81cf6 | 705 | int changed = 0; |
d3fbe5ea | 706 | struct early_res *r; |
a4c81cf6 | 707 | again: |
d3fbe5ea HY |
708 | i = find_overlapped_early(addr, addr + size); |
709 | r = &early_res[i]; | |
710 | if (i < MAX_EARLY_RES && r->end) { | |
711 | *addrp = addr = round_up(r->end, align); | |
712 | changed = 1; | |
713 | goto again; | |
a4c81cf6 YL |
714 | } |
715 | return changed; | |
716 | } | |
717 | ||
718 | /* Check for already reserved areas */ | |
719 | static inline int __init bad_addr_size(u64 *addrp, u64 *sizep, u64 align) | |
720 | { | |
721 | int i; | |
722 | u64 addr = *addrp, last; | |
723 | u64 size = *sizep; | |
724 | int changed = 0; | |
725 | again: | |
726 | last = addr + size; | |
727 | for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) { | |
728 | struct early_res *r = &early_res[i]; | |
729 | if (last > r->start && addr < r->start) { | |
730 | size = r->start - addr; | |
731 | changed = 1; | |
732 | goto again; | |
733 | } | |
734 | if (last > r->end && addr < r->end) { | |
735 | addr = round_up(r->end, align); | |
736 | size = last - addr; | |
737 | changed = 1; | |
738 | goto again; | |
739 | } | |
740 | if (last <= r->end && addr >= r->start) { | |
741 | (*sizep)++; | |
742 | return 0; | |
743 | } | |
744 | } | |
745 | if (changed) { | |
746 | *addrp = addr; | |
747 | *sizep = size; | |
748 | } | |
749 | return changed; | |
750 | } | |
751 | ||
752 | /* | |
753 | * Find a free area with specified alignment in a specific range. | |
754 | */ | |
755 | u64 __init find_e820_area(u64 start, u64 end, u64 size, u64 align) | |
756 | { | |
757 | int i; | |
758 | ||
759 | for (i = 0; i < e820.nr_map; i++) { | |
760 | struct e820entry *ei = &e820.map[i]; | |
761 | u64 addr, last; | |
762 | u64 ei_last; | |
763 | ||
764 | if (ei->type != E820_RAM) | |
765 | continue; | |
766 | addr = round_up(ei->addr, align); | |
767 | ei_last = ei->addr + ei->size; | |
768 | if (addr < start) | |
769 | addr = round_up(start, align); | |
770 | if (addr >= ei_last) | |
771 | continue; | |
772 | while (bad_addr(&addr, size, align) && addr+size <= ei_last) | |
773 | ; | |
774 | last = addr + size; | |
775 | if (last > ei_last) | |
776 | continue; | |
777 | if (last > end) | |
778 | continue; | |
779 | return addr; | |
780 | } | |
781 | return -1ULL; | |
782 | } | |
783 | ||
784 | /* | |
785 | * Find next free range after *start | |
786 | */ | |
787 | u64 __init find_e820_area_size(u64 start, u64 *sizep, u64 align) | |
788 | { | |
789 | int i; | |
790 | ||
791 | for (i = 0; i < e820.nr_map; i++) { | |
792 | struct e820entry *ei = &e820.map[i]; | |
793 | u64 addr, last; | |
794 | u64 ei_last; | |
795 | ||
796 | if (ei->type != E820_RAM) | |
797 | continue; | |
798 | addr = round_up(ei->addr, align); | |
799 | ei_last = ei->addr + ei->size; | |
800 | if (addr < start) | |
801 | addr = round_up(start, align); | |
802 | if (addr >= ei_last) | |
803 | continue; | |
804 | *sizep = ei_last - addr; | |
805 | while (bad_addr_size(&addr, sizep, align) && | |
806 | addr + *sizep <= ei_last) | |
807 | ; | |
808 | last = addr + *sizep; | |
809 | if (last > ei_last) | |
810 | continue; | |
811 | return addr; | |
812 | } | |
813 | return -1UL; | |
814 | ||
815 | } | |
2944e16b YL |
816 | |
817 | /* | |
818 | * pre allocated 4k and reserved it in e820 | |
819 | */ | |
820 | u64 __init early_reserve_e820(u64 startt, u64 sizet, u64 align) | |
821 | { | |
822 | u64 size = 0; | |
823 | u64 addr; | |
824 | u64 start; | |
825 | ||
826 | start = startt; | |
827 | while (size < sizet) | |
828 | start = find_e820_area_size(start, &size, align); | |
829 | ||
830 | if (size < sizet) | |
831 | return 0; | |
832 | ||
833 | addr = round_down(start + size - sizet, align); | |
d0be6bde | 834 | e820_update_range(addr, sizet, E820_RAM, E820_RESERVED); |
2944e16b YL |
835 | printk(KERN_INFO "update e820 for early_reserve_e820\n"); |
836 | update_e820(); | |
837 | ||
838 | return addr; | |
839 | } | |
840 | ||
ee0c80fa YL |
841 | #ifdef CONFIG_X86_32 |
842 | # ifdef CONFIG_X86_PAE | |
843 | # define MAX_ARCH_PFN (1ULL<<(36-PAGE_SHIFT)) | |
844 | # else | |
845 | # define MAX_ARCH_PFN (1ULL<<(32-PAGE_SHIFT)) | |
846 | # endif | |
847 | #else /* CONFIG_X86_32 */ | |
bd70e522 | 848 | # define MAX_ARCH_PFN MAXMEM>>PAGE_SHIFT |
ee0c80fa YL |
849 | #endif |
850 | ||
851 | /* | |
852 | * Last pfn which the user wants to use. | |
853 | */ | |
854 | unsigned long __initdata end_user_pfn = MAX_ARCH_PFN; | |
855 | ||
856 | /* | |
857 | * Find the highest page frame number we have available | |
858 | */ | |
859 | unsigned long __init e820_end_of_ram(void) | |
860 | { | |
861 | unsigned long last_pfn; | |
862 | unsigned long max_arch_pfn = MAX_ARCH_PFN; | |
863 | ||
864 | last_pfn = find_max_pfn_with_active_regions(); | |
865 | ||
866 | if (last_pfn > max_arch_pfn) | |
867 | last_pfn = max_arch_pfn; | |
868 | if (last_pfn > end_user_pfn) | |
869 | last_pfn = end_user_pfn; | |
870 | ||
871 | printk(KERN_INFO "last_pfn = %lu max_arch_pfn = %lu\n", | |
872 | last_pfn, max_arch_pfn); | |
873 | return last_pfn; | |
874 | } | |
875 | ||
876 | /* | |
877 | * Finds an active region in the address range from start_pfn to last_pfn and | |
878 | * returns its range in ei_startpfn and ei_endpfn for the e820 entry. | |
879 | */ | |
880 | int __init e820_find_active_region(const struct e820entry *ei, | |
881 | unsigned long start_pfn, | |
882 | unsigned long last_pfn, | |
883 | unsigned long *ei_startpfn, | |
884 | unsigned long *ei_endpfn) | |
885 | { | |
886 | u64 align = PAGE_SIZE; | |
887 | ||
888 | *ei_startpfn = round_up(ei->addr, align) >> PAGE_SHIFT; | |
889 | *ei_endpfn = round_down(ei->addr + ei->size, align) >> PAGE_SHIFT; | |
890 | ||
891 | /* Skip map entries smaller than a page */ | |
892 | if (*ei_startpfn >= *ei_endpfn) | |
893 | return 0; | |
894 | ||
895 | /* Skip if map is outside the node */ | |
896 | if (ei->type != E820_RAM || *ei_endpfn <= start_pfn || | |
897 | *ei_startpfn >= last_pfn) | |
898 | return 0; | |
899 | ||
900 | /* Check for overlaps */ | |
901 | if (*ei_startpfn < start_pfn) | |
902 | *ei_startpfn = start_pfn; | |
903 | if (*ei_endpfn > last_pfn) | |
904 | *ei_endpfn = last_pfn; | |
905 | ||
906 | /* Obey end_user_pfn to save on memmap */ | |
907 | if (*ei_startpfn >= end_user_pfn) | |
908 | return 0; | |
909 | if (*ei_endpfn > end_user_pfn) | |
910 | *ei_endpfn = end_user_pfn; | |
911 | ||
912 | return 1; | |
913 | } | |
914 | ||
915 | /* Walk the e820 map and register active regions within a node */ | |
916 | void __init e820_register_active_regions(int nid, unsigned long start_pfn, | |
917 | unsigned long last_pfn) | |
918 | { | |
919 | unsigned long ei_startpfn; | |
920 | unsigned long ei_endpfn; | |
921 | int i; | |
922 | ||
923 | for (i = 0; i < e820.nr_map; i++) | |
924 | if (e820_find_active_region(&e820.map[i], | |
925 | start_pfn, last_pfn, | |
926 | &ei_startpfn, &ei_endpfn)) | |
927 | add_active_range(nid, ei_startpfn, ei_endpfn); | |
928 | } | |
929 | ||
930 | /* | |
931 | * Find the hole size (in bytes) in the memory range. | |
932 | * @start: starting address of the memory range to scan | |
933 | * @end: ending address of the memory range to scan | |
934 | */ | |
935 | u64 __init e820_hole_size(u64 start, u64 end) | |
936 | { | |
937 | unsigned long start_pfn = start >> PAGE_SHIFT; | |
938 | unsigned long last_pfn = end >> PAGE_SHIFT; | |
939 | unsigned long ei_startpfn, ei_endpfn, ram = 0; | |
940 | int i; | |
941 | ||
942 | for (i = 0; i < e820.nr_map; i++) { | |
943 | if (e820_find_active_region(&e820.map[i], | |
944 | start_pfn, last_pfn, | |
945 | &ei_startpfn, &ei_endpfn)) | |
946 | ram += ei_endpfn - ei_startpfn; | |
947 | } | |
948 | return end - start - ((u64)ram << PAGE_SHIFT); | |
949 | } | |
ab4a465e YL |
950 | |
951 | static void early_panic(char *msg) | |
952 | { | |
953 | early_printk(msg); | |
954 | panic(msg); | |
955 | } | |
956 | ||
957 | /* "mem=nopentium" disables the 4MB page tables. */ | |
958 | static int __init parse_memopt(char *p) | |
959 | { | |
960 | u64 mem_size; | |
961 | ||
962 | if (!p) | |
963 | return -EINVAL; | |
964 | ||
965 | #ifdef CONFIG_X86_32 | |
966 | if (!strcmp(p, "nopentium")) { | |
967 | setup_clear_cpu_cap(X86_FEATURE_PSE); | |
968 | return 0; | |
969 | } | |
970 | #endif | |
971 | ||
972 | mem_size = memparse(p, &p); | |
973 | end_user_pfn = mem_size>>PAGE_SHIFT; | |
974 | return 0; | |
975 | } | |
976 | early_param("mem", parse_memopt); | |
977 | ||
978 | static int userdef __initdata; | |
979 | ||
980 | static int __init parse_memmap_opt(char *p) | |
981 | { | |
982 | char *oldp; | |
983 | u64 start_at, mem_size; | |
984 | ||
985 | if (!strcmp(p, "exactmap")) { | |
986 | #ifdef CONFIG_CRASH_DUMP | |
987 | /* | |
988 | * If we are doing a crash dump, we still need to know | |
989 | * the real mem size before original memory map is | |
990 | * reset. | |
991 | */ | |
992 | e820_register_active_regions(0, 0, -1UL); | |
993 | saved_max_pfn = e820_end_of_ram(); | |
994 | remove_all_active_ranges(); | |
995 | #endif | |
996 | e820.nr_map = 0; | |
997 | userdef = 1; | |
998 | return 0; | |
999 | } | |
1000 | ||
1001 | oldp = p; | |
1002 | mem_size = memparse(p, &p); | |
1003 | if (p == oldp) | |
1004 | return -EINVAL; | |
1005 | ||
1006 | userdef = 1; | |
1007 | if (*p == '@') { | |
1008 | start_at = memparse(p+1, &p); | |
d0be6bde | 1009 | e820_add_region(start_at, mem_size, E820_RAM); |
ab4a465e YL |
1010 | } else if (*p == '#') { |
1011 | start_at = memparse(p+1, &p); | |
d0be6bde | 1012 | e820_add_region(start_at, mem_size, E820_ACPI); |
ab4a465e YL |
1013 | } else if (*p == '$') { |
1014 | start_at = memparse(p+1, &p); | |
d0be6bde | 1015 | e820_add_region(start_at, mem_size, E820_RESERVED); |
ab4a465e YL |
1016 | } else { |
1017 | end_user_pfn = (mem_size >> PAGE_SHIFT); | |
1018 | } | |
1019 | return *p == '\0' ? 0 : -EINVAL; | |
1020 | } | |
1021 | early_param("memmap", parse_memmap_opt); | |
1022 | ||
1023 | void __init finish_e820_parsing(void) | |
1024 | { | |
1025 | if (userdef) { | |
1026 | int nr = e820.nr_map; | |
1027 | ||
1028 | if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr) < 0) | |
1029 | early_panic("Invalid user supplied memory map"); | |
1030 | e820.nr_map = nr; | |
1031 | ||
1032 | printk(KERN_INFO "user-defined physical RAM map:\n"); | |
1033 | e820_print_map("user"); | |
1034 | } | |
1035 | } | |
41c094fd YL |
1036 | |
1037 | /* | |
1038 | * Mark e820 reserved areas as busy for the resource manager. | |
1039 | */ | |
1040 | void __init e820_reserve_resources(void) | |
1041 | { | |
1042 | int i; | |
1043 | struct resource *res; | |
1044 | ||
1045 | res = alloc_bootmem_low(sizeof(struct resource) * e820.nr_map); | |
1046 | for (i = 0; i < e820.nr_map; i++) { | |
1047 | switch (e820.map[i].type) { | |
1048 | case E820_RAM: res->name = "System RAM"; break; | |
1049 | case E820_ACPI: res->name = "ACPI Tables"; break; | |
1050 | case E820_NVS: res->name = "ACPI Non-volatile Storage"; break; | |
1051 | default: res->name = "reserved"; | |
1052 | } | |
1053 | res->start = e820.map[i].addr; | |
1054 | res->end = res->start + e820.map[i].size - 1; | |
1055 | #ifndef CONFIG_RESOURCES_64BIT | |
1056 | if (res->end > 0x100000000ULL) { | |
1057 | res++; | |
1058 | continue; | |
1059 | } | |
1060 | #endif | |
1061 | res->flags = IORESOURCE_MEM | IORESOURCE_BUSY; | |
1062 | insert_resource(&iomem_resource, res); | |
1063 | res++; | |
1064 | } | |
1065 | } | |
1066 | ||
95a71a45 | 1067 | char *__init default_machine_specific_memory_setup(void) |
064d25f1 YL |
1068 | { |
1069 | char *who = "BIOS-e820"; | |
1070 | int new_nr; | |
1071 | /* | |
1072 | * Try to copy the BIOS-supplied E820-map. | |
1073 | * | |
1074 | * Otherwise fake a memory map; one section from 0k->640k, | |
1075 | * the next section from 1mb->appropriate_mem_k | |
1076 | */ | |
1077 | new_nr = boot_params.e820_entries; | |
1078 | sanitize_e820_map(boot_params.e820_map, | |
1079 | ARRAY_SIZE(boot_params.e820_map), | |
1080 | &new_nr); | |
1081 | boot_params.e820_entries = new_nr; | |
1082 | if (copy_e820_map(boot_params.e820_map, boot_params.e820_entries) < 0) { | |
95a71a45 | 1083 | u64 mem_size; |
064d25f1 YL |
1084 | |
1085 | /* compare results from other methods and take the greater */ | |
1086 | if (boot_params.alt_mem_k | |
1087 | < boot_params.screen_info.ext_mem_k) { | |
1088 | mem_size = boot_params.screen_info.ext_mem_k; | |
1089 | who = "BIOS-88"; | |
1090 | } else { | |
1091 | mem_size = boot_params.alt_mem_k; | |
1092 | who = "BIOS-e801"; | |
1093 | } | |
1094 | ||
1095 | e820.nr_map = 0; | |
1096 | e820_add_region(0, LOWMEMSIZE(), E820_RAM); | |
1097 | e820_add_region(HIGH_MEMORY, mem_size << 10, E820_RAM); | |
064d25f1 YL |
1098 | } |
1099 | ||
1100 | /* In case someone cares... */ | |
1101 | return who; | |
1102 | } | |
1103 | ||
95a71a45 YL |
1104 | char *__init __attribute__((weak)) machine_specific_memory_setup(void) |
1105 | { | |
1106 | return default_machine_specific_memory_setup(); | |
1107 | } | |
1108 | ||
064d25f1 YL |
1109 | /* Overridden in paravirt.c if CONFIG_PARAVIRT */ |
1110 | char * __init __attribute__((weak)) memory_setup(void) | |
1111 | { | |
1112 | return machine_specific_memory_setup(); | |
1113 | } | |
1114 | ||
1115 | void __init setup_memory_map(void) | |
1116 | { | |
1117 | printk(KERN_INFO "BIOS-provided physical RAM map:\n"); | |
1118 | e820_print_map(memory_setup()); | |
1119 | } | |
1120 | ||
1121 | #ifdef CONFIG_X86_64 | |
1122 | int __init arch_get_ram_range(int slot, u64 *addr, u64 *size) | |
1123 | { | |
1124 | int i; | |
41c094fd | 1125 | |
064d25f1 YL |
1126 | if (slot < 0 || slot >= e820.nr_map) |
1127 | return -1; | |
1128 | for (i = slot; i < e820.nr_map; i++) { | |
1129 | if (e820.map[i].type != E820_RAM) | |
1130 | continue; | |
1131 | break; | |
1132 | } | |
1133 | if (i == e820.nr_map || e820.map[i].addr > (max_pfn << PAGE_SHIFT)) | |
1134 | return -1; | |
1135 | *addr = e820.map[i].addr; | |
1136 | *size = min_t(u64, e820.map[i].size + e820.map[i].addr, | |
1137 | max_pfn << PAGE_SHIFT) - *addr; | |
1138 | return i + 1; | |
1139 | } | |
1140 | #endif |