Commit | Line | Data |
---|---|---|
b79cd8f1 | 1 | /* |
640e1b38 | 2 | * Low level x86 E820 memory map handling functions. |
b79cd8f1 | 3 | * |
640e1b38 IM |
4 | * The firmware and bootloader passes us the "E820 table", which is the primary |
5 | * physical memory layout description available about x86 systems. | |
b79cd8f1 | 6 | * |
640e1b38 IM |
7 | * The kernel takes the E820 memory layout and optionally modifies it with |
8 | * quirks and other tweaks, and feeds that into the generic Linux memory | |
9 | * allocation code routines via a platform independent interface (memblock, etc.). | |
b79cd8f1 | 10 | */ |
93a72052 | 11 | #include <linux/crash_dump.h> |
b79cd8f1 | 12 | #include <linux/bootmem.h> |
bf62f398 | 13 | #include <linux/suspend.h> |
976513db | 14 | #include <linux/acpi.h> |
5dfcf14d | 15 | #include <linux/firmware-map.h> |
72d7c3b3 | 16 | #include <linux/memblock.h> |
d1bbdd66 | 17 | #include <linux/sort.h> |
b79cd8f1 | 18 | |
66441bd3 | 19 | #include <asm/e820/api.h> |
b79cd8f1 YL |
20 | #include <asm/setup.h> |
21 | ||
5dfcf14d | 22 | /* |
12df216c | 23 | * We organize the E820 table into three main data structures: |
544a0f47 | 24 | * |
12df216c CY |
25 | * - 'e820_table_firmware': the original firmware version passed to us by the |
26 | * bootloader - not modified by the kernel. It is composed of two parts: | |
27 | * the first 128 E820 memory entries in boot_params.e820_table and the remaining | |
28 | * (if any) entries of the SETUP_E820_EXT nodes. We use this to: | |
544a0f47 IM |
29 | * |
30 | * - inform the user about the firmware's notion of memory layout | |
31 | * via /sys/firmware/memmap | |
32 | * | |
33 | * - the hibernation code uses it to generate a kernel-independent MD5 | |
34 | * fingerprint of the physical memory layout of a system. | |
35 | * | |
12df216c CY |
36 | * - 'e820_table_kexec': a slightly modified (by the kernel) firmware version |
37 | * passed to us by the bootloader - the major difference between | |
38 | * e820_table_firmware[] and this one is that, the latter marks the setup_data | |
39 | * list created by the EFI boot stub as reserved, so that kexec can reuse the | |
40 | * setup_data information in the second kernel. Besides, e820_table_kexec[] | |
41 | * might also be modified by the kexec itself to fake a mptable. | |
42 | * We use this to: | |
43 | * | |
640e1b38 | 44 | * - kexec, which is a bootloader in disguise, uses the original E820 |
544a0f47 | 45 | * layout to pass to the kexec-ed kernel. This way the original kernel |
640e1b38 | 46 | * can have a restricted E820 map while the kexec()-ed kexec-kernel |
544a0f47 IM |
47 | * can have access to full memory - etc. |
48 | * | |
640e1b38 | 49 | * - 'e820_table': this is the main E820 table that is massaged by the |
544a0f47 IM |
50 | * low level x86 platform code, or modified by boot parameters, before |
51 | * passed on to higher level MM layers. | |
52 | * | |
640e1b38 | 53 | * Once the E820 map has been converted to the standard Linux memory layout |
544a0f47 IM |
54 | * information its role stops - modifying it has no effect and does not get |
55 | * re-propagated. So itsmain role is a temporary bootstrap storage of firmware | |
56 | * specific memory layout data during early bootup. | |
5dfcf14d | 57 | */ |
544a0f47 | 58 | static struct e820_table e820_table_init __initdata; |
a09bae0f | 59 | static struct e820_table e820_table_kexec_init __initdata; |
12df216c | 60 | static struct e820_table e820_table_firmware_init __initdata; |
544a0f47 IM |
61 | |
62 | struct e820_table *e820_table __refdata = &e820_table_init; | |
a09bae0f | 63 | struct e820_table *e820_table_kexec __refdata = &e820_table_kexec_init; |
12df216c | 64 | struct e820_table *e820_table_firmware __refdata = &e820_table_firmware_init; |
b79cd8f1 YL |
65 | |
66 | /* For PCI or other memory-mapped resources */ | |
67 | unsigned long pci_mem_start = 0xaeedbabe; | |
68 | #ifdef CONFIG_PCI | |
69 | EXPORT_SYMBOL(pci_mem_start); | |
70 | #endif | |
71 | ||
72 | /* | |
73 | * This function checks if any part of the range <start,end> is mapped | |
74 | * with type. | |
75 | */ | |
81b3e090 | 76 | bool e820__mapped_any(u64 start, u64 end, enum e820_type type) |
b79cd8f1 YL |
77 | { |
78 | int i; | |
79 | ||
bf495573 | 80 | for (i = 0; i < e820_table->nr_entries; i++) { |
e5540f87 | 81 | struct e820_entry *entry = &e820_table->entries[i]; |
b79cd8f1 | 82 | |
e5540f87 | 83 | if (type && entry->type != type) |
b79cd8f1 | 84 | continue; |
e5540f87 | 85 | if (entry->addr >= end || entry->addr + entry->size <= start) |
b79cd8f1 YL |
86 | continue; |
87 | return 1; | |
88 | } | |
89 | return 0; | |
90 | } | |
3bce64f0 | 91 | EXPORT_SYMBOL_GPL(e820__mapped_any); |
b79cd8f1 YL |
92 | |
93 | /* | |
640e1b38 | 94 | * This function checks if the entire <start,end> range is mapped with 'type'. |
b79cd8f1 | 95 | * |
640e1b38 IM |
96 | * Note: this function only works correctly once the E820 table is sorted and |
97 | * not-overlapping (at least for the range specified), which is the case normally. | |
b79cd8f1 | 98 | */ |
d68baa3f TL |
99 | static struct e820_entry *__e820__mapped_all(u64 start, u64 end, |
100 | enum e820_type type) | |
b79cd8f1 YL |
101 | { |
102 | int i; | |
103 | ||
bf495573 | 104 | for (i = 0; i < e820_table->nr_entries; i++) { |
e5540f87 | 105 | struct e820_entry *entry = &e820_table->entries[i]; |
b79cd8f1 | 106 | |
e5540f87 | 107 | if (type && entry->type != type) |
b79cd8f1 | 108 | continue; |
640e1b38 IM |
109 | |
110 | /* Is the region (part) in overlap with the current region? */ | |
e5540f87 | 111 | if (entry->addr >= end || entry->addr + entry->size <= start) |
b79cd8f1 YL |
112 | continue; |
113 | ||
640e1b38 IM |
114 | /* |
115 | * If the region is at the beginning of <start,end> we move | |
116 | * 'start' to the end of the region since it's ok until there | |
b79cd8f1 | 117 | */ |
e5540f87 IM |
118 | if (entry->addr <= start) |
119 | start = entry->addr + entry->size; | |
640e1b38 | 120 | |
b79cd8f1 | 121 | /* |
640e1b38 IM |
122 | * If 'start' is now at or beyond 'end', we're done, full |
123 | * coverage of the desired range exists: | |
b79cd8f1 YL |
124 | */ |
125 | if (start >= end) | |
d68baa3f | 126 | return entry; |
b79cd8f1 | 127 | } |
d68baa3f TL |
128 | |
129 | return NULL; | |
130 | } | |
131 | ||
132 | /* | |
133 | * This function checks if the entire range <start,end> is mapped with type. | |
134 | */ | |
135 | bool __init e820__mapped_all(u64 start, u64 end, enum e820_type type) | |
136 | { | |
137 | return __e820__mapped_all(start, end, type); | |
138 | } | |
139 | ||
140 | /* | |
141 | * This function returns the type associated with the range <start,end>. | |
142 | */ | |
143 | int e820__get_entry_type(u64 start, u64 end) | |
144 | { | |
145 | struct e820_entry *entry = __e820__mapped_all(start, end, 0); | |
146 | ||
147 | return entry ? entry->type : -EINVAL; | |
b79cd8f1 YL |
148 | } |
149 | ||
150 | /* | |
640e1b38 | 151 | * Add a memory region to the kernel E820 map. |
b79cd8f1 | 152 | */ |
6afc03b8 | 153 | static void __init __e820__range_add(struct e820_table *table, u64 start, u64 size, enum e820_type type) |
b79cd8f1 | 154 | { |
bf495573 | 155 | int x = table->nr_entries; |
b79cd8f1 | 156 | |
bf495573 | 157 | if (x >= ARRAY_SIZE(table->entries)) { |
01259ef1 | 158 | pr_err("e820: too many entries; ignoring [mem %#010llx-%#010llx]\n", start, start + size - 1); |
b79cd8f1 YL |
159 | return; |
160 | } | |
161 | ||
bf495573 IM |
162 | table->entries[x].addr = start; |
163 | table->entries[x].size = size; | |
164 | table->entries[x].type = type; | |
165 | table->nr_entries++; | |
773e673d YL |
166 | } |
167 | ||
6afc03b8 | 168 | void __init e820__range_add(u64 start, u64 size, enum e820_type type) |
773e673d | 169 | { |
ab6bc04c | 170 | __e820__range_add(e820_table, start, size, type); |
b79cd8f1 YL |
171 | } |
172 | ||
6afc03b8 | 173 | static void __init e820_print_type(enum e820_type type) |
c61cf4cf YL |
174 | { |
175 | switch (type) { | |
09821ff1 IM |
176 | case E820_TYPE_RAM: /* Fall through: */ |
177 | case E820_TYPE_RESERVED_KERN: pr_cont("usable"); break; | |
178 | case E820_TYPE_RESERVED: pr_cont("reserved"); break; | |
179 | case E820_TYPE_ACPI: pr_cont("ACPI data"); break; | |
180 | case E820_TYPE_NVS: pr_cont("ACPI NVS"); break; | |
181 | case E820_TYPE_UNUSABLE: pr_cont("unusable"); break; | |
182 | case E820_TYPE_PMEM: /* Fall through: */ | |
183 | case E820_TYPE_PRAM: pr_cont("persistent (type %u)", type); break; | |
01259ef1 | 184 | default: pr_cont("type %u", type); break; |
c61cf4cf YL |
185 | } |
186 | } | |
187 | ||
be0c3f0f | 188 | void __init e820__print_table(char *who) |
b79cd8f1 YL |
189 | { |
190 | int i; | |
191 | ||
bf495573 | 192 | for (i = 0; i < e820_table->nr_entries; i++) { |
01259ef1 | 193 | pr_info("%s: [mem %#018Lx-%#018Lx] ", who, |
640e1b38 IM |
194 | e820_table->entries[i].addr, |
195 | e820_table->entries[i].addr + e820_table->entries[i].size - 1); | |
196 | ||
bf495573 | 197 | e820_print_type(e820_table->entries[i].type); |
01259ef1 | 198 | pr_cont("\n"); |
b79cd8f1 YL |
199 | } |
200 | } | |
201 | ||
202 | /* | |
9a02fd0f | 203 | * Sanitize an E820 map. |
b79cd8f1 | 204 | * |
9a02fd0f | 205 | * Some E820 layouts include overlapping entries. The following |
640e1b38 | 206 | * replaces the original E820 map with a new one, removing overlaps, |
5b7eb2e9 PJ |
207 | * and resolving conflicting memory types in favor of highest |
208 | * numbered type. | |
b79cd8f1 | 209 | * |
9a02fd0f IM |
210 | * The input parameter 'entries' points to an array of 'struct |
211 | * e820_entry' which on entry has elements in the range [0, *nr_entries) | |
212 | * valid, and which has space for up to max_nr_entries entries. | |
640e1b38 | 213 | * On return, the resulting sanitized E820 map entries will be in |
9a02fd0f | 214 | * overwritten in the same location, starting at 'entries'. |
5b7eb2e9 | 215 | * |
9a02fd0f IM |
216 | * The integer pointed to by nr_entries must be valid on entry (the |
217 | * current number of valid entries located at 'entries'). If the | |
218 | * sanitizing succeeds the *nr_entries will be updated with the new | |
219 | * number of valid entries (something no more than max_nr_entries). | |
5b7eb2e9 | 220 | * |
f52355a9 | 221 | * The return value from e820__update_table() is zero if it |
5b7eb2e9 PJ |
222 | * successfully 'sanitized' the map entries passed in, and is -1 |
223 | * if it did nothing, which can happen if either of (1) it was | |
224 | * only passed one map entry, or (2) any of the input map entries | |
225 | * were invalid (start + size < start, meaning that the size was | |
226 | * so big the described memory range wrapped around through zero.) | |
227 | * | |
228 | * Visually we're performing the following | |
229 | * (1,2,3,4 = memory types)... | |
230 | * | |
231 | * Sample memory map (w/overlaps): | |
232 | * ____22__________________ | |
233 | * ______________________4_ | |
234 | * ____1111________________ | |
235 | * _44_____________________ | |
236 | * 11111111________________ | |
237 | * ____________________33__ | |
238 | * ___________44___________ | |
239 | * __________33333_________ | |
240 | * ______________22________ | |
241 | * ___________________2222_ | |
242 | * _________111111111______ | |
243 | * _____________________11_ | |
244 | * _________________4______ | |
245 | * | |
246 | * Sanitized equivalent (no overlap): | |
247 | * 1_______________________ | |
248 | * _44_____________________ | |
249 | * ___1____________________ | |
250 | * ____22__________________ | |
251 | * ______11________________ | |
252 | * _________1______________ | |
253 | * __________3_____________ | |
254 | * ___________44___________ | |
255 | * _____________33_________ | |
256 | * _______________2________ | |
257 | * ________________1_______ | |
258 | * _________________4______ | |
259 | * ___________________2____ | |
260 | * ____________________33__ | |
261 | * ______________________4_ | |
b79cd8f1 | 262 | */ |
d1bbdd66 | 263 | struct change_member { |
9a02fd0f IM |
264 | /* Pointer to the original entry: */ |
265 | struct e820_entry *entry; | |
640e1b38 IM |
266 | /* Address for this change point: */ |
267 | unsigned long long addr; | |
d1bbdd66 MD |
268 | }; |
269 | ||
441ac2f3 IM |
270 | static struct change_member change_point_list[2*E820_MAX_ENTRIES] __initdata; |
271 | static struct change_member *change_point[2*E820_MAX_ENTRIES] __initdata; | |
272 | static struct e820_entry *overlap_list[E820_MAX_ENTRIES] __initdata; | |
273 | static struct e820_entry new_entries[E820_MAX_ENTRIES] __initdata; | |
274 | ||
d1bbdd66 MD |
275 | static int __init cpcompare(const void *a, const void *b) |
276 | { | |
277 | struct change_member * const *app = a, * const *bpp = b; | |
278 | const struct change_member *ap = *app, *bp = *bpp; | |
279 | ||
280 | /* | |
281 | * Inputs are pointers to two elements of change_point[]. If their | |
640e1b38 | 282 | * addresses are not equal, their difference dominates. If the addresses |
d1bbdd66 MD |
283 | * are equal, then consider one that represents the end of its region |
284 | * to be greater than one that does not. | |
285 | */ | |
286 | if (ap->addr != bp->addr) | |
287 | return ap->addr > bp->addr ? 1 : -1; | |
288 | ||
9a02fd0f | 289 | return (ap->addr != ap->entry->addr) - (bp->addr != bp->entry->addr); |
d1bbdd66 | 290 | } |
5b7eb2e9 | 291 | |
441ac2f3 | 292 | int __init e820__update_table(struct e820_table *table) |
b79cd8f1 | 293 | { |
441ac2f3 IM |
294 | struct e820_entry *entries = table->entries; |
295 | u32 max_nr_entries = ARRAY_SIZE(table->entries); | |
6afc03b8 | 296 | enum e820_type current_type, last_type; |
b79cd8f1 | 297 | unsigned long long last_addr; |
441ac2f3 IM |
298 | u32 new_nr_entries, overlap_entries; |
299 | u32 i, chg_idx, chg_nr; | |
b79cd8f1 | 300 | |
640e1b38 | 301 | /* If there's only one memory region, don't bother: */ |
441ac2f3 | 302 | if (table->nr_entries < 2) |
b79cd8f1 YL |
303 | return -1; |
304 | ||
441ac2f3 | 305 | BUG_ON(table->nr_entries > max_nr_entries); |
b79cd8f1 | 306 | |
9a02fd0f | 307 | /* Bail out if we find any unreasonable addresses in the map: */ |
441ac2f3 | 308 | for (i = 0; i < table->nr_entries; i++) { |
9a02fd0f | 309 | if (entries[i].addr + entries[i].size < entries[i].addr) |
b79cd8f1 | 310 | return -1; |
640e1b38 | 311 | } |
b79cd8f1 | 312 | |
640e1b38 | 313 | /* Create pointers for initial change-point information (for sorting): */ |
441ac2f3 | 314 | for (i = 0; i < 2 * table->nr_entries; i++) |
b79cd8f1 YL |
315 | change_point[i] = &change_point_list[i]; |
316 | ||
640e1b38 IM |
317 | /* |
318 | * Record all known change-points (starting and ending addresses), | |
319 | * omitting empty memory regions: | |
320 | */ | |
441ac2f3 IM |
321 | chg_idx = 0; |
322 | for (i = 0; i < table->nr_entries; i++) { | |
9a02fd0f | 323 | if (entries[i].size != 0) { |
441ac2f3 IM |
324 | change_point[chg_idx]->addr = entries[i].addr; |
325 | change_point[chg_idx++]->entry = &entries[i]; | |
326 | change_point[chg_idx]->addr = entries[i].addr + entries[i].size; | |
327 | change_point[chg_idx++]->entry = &entries[i]; | |
b79cd8f1 YL |
328 | } |
329 | } | |
441ac2f3 | 330 | chg_nr = chg_idx; |
b79cd8f1 | 331 | |
640e1b38 | 332 | /* Sort change-point list by memory addresses (low -> high): */ |
d88961b5 | 333 | sort(change_point, chg_nr, sizeof(*change_point), cpcompare, NULL); |
b79cd8f1 | 334 | |
9a02fd0f | 335 | /* Create a new memory map, removing overlaps: */ |
640e1b38 | 336 | overlap_entries = 0; /* Number of entries in the overlap table */ |
9a02fd0f | 337 | new_nr_entries = 0; /* Index for creating new map entries */ |
640e1b38 IM |
338 | last_type = 0; /* Start with undefined memory type */ |
339 | last_addr = 0; /* Start with 0 as last starting address */ | |
b79cd8f1 | 340 | |
9a02fd0f | 341 | /* Loop through change-points, determining effect on the new map: */ |
441ac2f3 | 342 | for (chg_idx = 0; chg_idx < chg_nr; chg_idx++) { |
9a02fd0f | 343 | /* Keep track of all overlapping entries */ |
441ac2f3 | 344 | if (change_point[chg_idx]->addr == change_point[chg_idx]->entry->addr) { |
640e1b38 | 345 | /* Add map entry to overlap list (> 1 entry implies an overlap) */ |
441ac2f3 | 346 | overlap_list[overlap_entries++] = change_point[chg_idx]->entry; |
b79cd8f1 | 347 | } else { |
640e1b38 | 348 | /* Remove entry from list (order independent, so swap with last): */ |
b79cd8f1 | 349 | for (i = 0; i < overlap_entries; i++) { |
441ac2f3 | 350 | if (overlap_list[i] == change_point[chg_idx]->entry) |
640e1b38 | 351 | overlap_list[i] = overlap_list[overlap_entries-1]; |
b79cd8f1 YL |
352 | } |
353 | overlap_entries--; | |
354 | } | |
355 | /* | |
640e1b38 | 356 | * If there are overlapping entries, decide which |
b79cd8f1 YL |
357 | * "type" to use (larger value takes precedence -- |
358 | * 1=usable, 2,3,4,4+=unusable) | |
359 | */ | |
360 | current_type = 0; | |
640e1b38 | 361 | for (i = 0; i < overlap_entries; i++) { |
b79cd8f1 YL |
362 | if (overlap_list[i]->type > current_type) |
363 | current_type = overlap_list[i]->type; | |
640e1b38 IM |
364 | } |
365 | ||
9a02fd0f | 366 | /* Continue building up new map based on this information: */ |
09821ff1 | 367 | if (current_type != last_type || current_type == E820_TYPE_PRAM) { |
b79cd8f1 | 368 | if (last_type != 0) { |
441ac2f3 | 369 | new_entries[new_nr_entries].size = change_point[chg_idx]->addr - last_addr; |
640e1b38 | 370 | /* Move forward only if the new size was non-zero: */ |
9a02fd0f IM |
371 | if (new_entries[new_nr_entries].size != 0) |
372 | /* No more space left for new entries? */ | |
373 | if (++new_nr_entries >= max_nr_entries) | |
b79cd8f1 YL |
374 | break; |
375 | } | |
376 | if (current_type != 0) { | |
441ac2f3 | 377 | new_entries[new_nr_entries].addr = change_point[chg_idx]->addr; |
9a02fd0f | 378 | new_entries[new_nr_entries].type = current_type; |
441ac2f3 | 379 | last_addr = change_point[chg_idx]->addr; |
b79cd8f1 YL |
380 | } |
381 | last_type = current_type; | |
382 | } | |
383 | } | |
640e1b38 | 384 | |
9a02fd0f | 385 | /* Copy the new entries into the original location: */ |
441ac2f3 IM |
386 | memcpy(entries, new_entries, new_nr_entries*sizeof(*entries)); |
387 | table->nr_entries = new_nr_entries; | |
b79cd8f1 YL |
388 | |
389 | return 0; | |
390 | } | |
391 | ||
7410aa1c | 392 | static int __init __append_e820_table(struct boot_e820_entry *entries, u32 nr_entries) |
8c5beb50 | 393 | { |
7410aa1c | 394 | struct boot_e820_entry *entry = entries; |
9a02fd0f IM |
395 | |
396 | while (nr_entries) { | |
397 | u64 start = entry->addr; | |
398 | u64 size = entry->size; | |
3ec97965 | 399 | u64 end = start + size - 1; |
9a02fd0f | 400 | u32 type = entry->type; |
8c5beb50 | 401 | |
640e1b38 | 402 | /* Ignore the entry on 64-bit overflow: */ |
3ec97965 | 403 | if (start > end && likely(size)) |
8c5beb50 HY |
404 | return -1; |
405 | ||
ab6bc04c | 406 | e820__range_add(start, size, type); |
8c5beb50 | 407 | |
9a02fd0f IM |
408 | entry++; |
409 | nr_entries--; | |
8c5beb50 HY |
410 | } |
411 | return 0; | |
412 | } | |
413 | ||
b79cd8f1 | 414 | /* |
640e1b38 | 415 | * Copy the BIOS E820 map into a safe place. |
b79cd8f1 YL |
416 | * |
417 | * Sanity-check it while we're at it.. | |
418 | * | |
419 | * If we're lucky and live on a modern system, the setup code | |
420 | * will have given us a memory map that we can use to properly | |
421 | * set up memory. If we aren't, we'll fake a memory map. | |
422 | */ | |
7410aa1c | 423 | static int __init append_e820_table(struct boot_e820_entry *entries, u32 nr_entries) |
b79cd8f1 YL |
424 | { |
425 | /* Only one memory region (or negative)? Ignore it */ | |
9a02fd0f | 426 | if (nr_entries < 2) |
b79cd8f1 YL |
427 | return -1; |
428 | ||
9a02fd0f | 429 | return __append_e820_table(entries, nr_entries); |
b79cd8f1 YL |
430 | } |
431 | ||
640e1b38 | 432 | static u64 __init |
6afc03b8 | 433 | __e820__range_update(struct e820_table *table, u64 start, u64 size, enum e820_type old_type, enum e820_type new_type) |
b79cd8f1 | 434 | { |
78a8b35b | 435 | u64 end; |
773e673d | 436 | unsigned int i; |
b79cd8f1 YL |
437 | u64 real_updated_size = 0; |
438 | ||
439 | BUG_ON(old_type == new_type); | |
440 | ||
232b957a YL |
441 | if (size > (ULLONG_MAX - start)) |
442 | size = ULLONG_MAX - start; | |
443 | ||
78a8b35b | 444 | end = start + size; |
e22af0be | 445 | printk(KERN_DEBUG "e820: update [mem %#010Lx-%#010Lx] ", start, end - 1); |
c61cf4cf | 446 | e820_print_type(old_type); |
01259ef1 | 447 | pr_cont(" ==> "); |
c61cf4cf | 448 | e820_print_type(new_type); |
01259ef1 | 449 | pr_cont("\n"); |
c61cf4cf | 450 | |
bf495573 | 451 | for (i = 0; i < table->nr_entries; i++) { |
e5540f87 | 452 | struct e820_entry *entry = &table->entries[i]; |
b79cd8f1 | 453 | u64 final_start, final_end; |
e5540f87 | 454 | u64 entry_end; |
78a8b35b | 455 | |
e5540f87 | 456 | if (entry->type != old_type) |
b79cd8f1 | 457 | continue; |
78a8b35b | 458 | |
e5540f87 | 459 | entry_end = entry->addr + entry->size; |
640e1b38 IM |
460 | |
461 | /* Completely covered by new range? */ | |
e5540f87 IM |
462 | if (entry->addr >= start && entry_end <= end) { |
463 | entry->type = new_type; | |
464 | real_updated_size += entry->size; | |
b79cd8f1 YL |
465 | continue; |
466 | } | |
78a8b35b | 467 | |
640e1b38 | 468 | /* New range is completely covered? */ |
e5540f87 | 469 | if (entry->addr < start && entry_end > end) { |
ab6bc04c IM |
470 | __e820__range_add(table, start, size, new_type); |
471 | __e820__range_add(table, end, entry_end - end, entry->type); | |
e5540f87 | 472 | entry->size = start - entry->addr; |
78a8b35b YL |
473 | real_updated_size += size; |
474 | continue; | |
475 | } | |
476 | ||
640e1b38 | 477 | /* Partially covered: */ |
e5540f87 IM |
478 | final_start = max(start, entry->addr); |
479 | final_end = min(end, entry_end); | |
b79cd8f1 YL |
480 | if (final_start >= final_end) |
481 | continue; | |
5c0e6f03 | 482 | |
ab6bc04c | 483 | __e820__range_add(table, final_start, final_end - final_start, new_type); |
5c0e6f03 | 484 | |
b79cd8f1 | 485 | real_updated_size += final_end - final_start; |
976dd4dc | 486 | |
773e673d | 487 | /* |
640e1b38 IM |
488 | * Left range could be head or tail, so need to update |
489 | * its size first: | |
773e673d | 490 | */ |
e5540f87 IM |
491 | entry->size -= final_end - final_start; |
492 | if (entry->addr < final_start) | |
976dd4dc | 493 | continue; |
640e1b38 | 494 | |
e5540f87 | 495 | entry->addr = final_end; |
b79cd8f1 YL |
496 | } |
497 | return real_updated_size; | |
498 | } | |
499 | ||
6afc03b8 | 500 | u64 __init e820__range_update(u64 start, u64 size, enum e820_type old_type, enum e820_type new_type) |
fc9036ea | 501 | { |
ab6bc04c | 502 | return __e820__range_update(e820_table, start, size, old_type, new_type); |
fc9036ea YL |
503 | } |
504 | ||
a09bae0f | 505 | static u64 __init e820__range_update_kexec(u64 start, u64 size, enum e820_type old_type, enum e820_type new_type) |
fc9036ea | 506 | { |
a09bae0f | 507 | return __e820__range_update(e820_table_kexec, start, size, old_type, new_type); |
fc9036ea YL |
508 | } |
509 | ||
640e1b38 | 510 | /* Remove a range of memory from the E820 table: */ |
81b3e090 | 511 | u64 __init e820__range_remove(u64 start, u64 size, enum e820_type old_type, bool check_type) |
7a1fd986 YL |
512 | { |
513 | int i; | |
1b5576e6 | 514 | u64 end; |
7a1fd986 YL |
515 | u64 real_removed_size = 0; |
516 | ||
232b957a YL |
517 | if (size > (ULLONG_MAX - start)) |
518 | size = ULLONG_MAX - start; | |
519 | ||
1b5576e6 | 520 | end = start + size; |
e22af0be | 521 | printk(KERN_DEBUG "e820: remove [mem %#010Lx-%#010Lx] ", start, end - 1); |
81b3e090 | 522 | if (check_type) |
9f3a5f52 | 523 | e820_print_type(old_type); |
01259ef1 | 524 | pr_cont("\n"); |
1b5576e6 | 525 | |
bf495573 | 526 | for (i = 0; i < e820_table->nr_entries; i++) { |
e5540f87 | 527 | struct e820_entry *entry = &e820_table->entries[i]; |
7a1fd986 | 528 | u64 final_start, final_end; |
e5540f87 | 529 | u64 entry_end; |
7a1fd986 | 530 | |
81b3e090 | 531 | if (check_type && entry->type != old_type) |
7a1fd986 | 532 | continue; |
9f3a5f52 | 533 | |
e5540f87 | 534 | entry_end = entry->addr + entry->size; |
640e1b38 IM |
535 | |
536 | /* Completely covered? */ | |
e5540f87 IM |
537 | if (entry->addr >= start && entry_end <= end) { |
538 | real_removed_size += entry->size; | |
d88961b5 | 539 | memset(entry, 0, sizeof(*entry)); |
7a1fd986 YL |
540 | continue; |
541 | } | |
9f3a5f52 | 542 | |
640e1b38 | 543 | /* Is the new range completely covered? */ |
e5540f87 | 544 | if (entry->addr < start && entry_end > end) { |
ab6bc04c | 545 | e820__range_add(end, entry_end - end, entry->type); |
e5540f87 | 546 | entry->size = start - entry->addr; |
9f3a5f52 YL |
547 | real_removed_size += size; |
548 | continue; | |
549 | } | |
550 | ||
640e1b38 | 551 | /* Partially covered: */ |
e5540f87 IM |
552 | final_start = max(start, entry->addr); |
553 | final_end = min(end, entry_end); | |
7a1fd986 YL |
554 | if (final_start >= final_end) |
555 | continue; | |
640e1b38 | 556 | |
7a1fd986 YL |
557 | real_removed_size += final_end - final_start; |
558 | ||
9f3a5f52 | 559 | /* |
640e1b38 IM |
560 | * Left range could be head or tail, so need to update |
561 | * the size first: | |
9f3a5f52 | 562 | */ |
e5540f87 IM |
563 | entry->size -= final_end - final_start; |
564 | if (entry->addr < final_start) | |
7a1fd986 | 565 | continue; |
640e1b38 | 566 | |
e5540f87 | 567 | entry->addr = final_end; |
7a1fd986 YL |
568 | } |
569 | return real_removed_size; | |
570 | } | |
571 | ||
6464d294 | 572 | void __init e820__update_table_print(void) |
b79cd8f1 | 573 | { |
f9748fa0 | 574 | if (e820__update_table(e820_table)) |
b79cd8f1 | 575 | return; |
640e1b38 | 576 | |
01259ef1 | 577 | pr_info("e820: modified physical RAM map:\n"); |
be0c3f0f | 578 | e820__print_table("modified"); |
b79cd8f1 | 579 | } |
640e1b38 | 580 | |
a09bae0f | 581 | static void __init e820__update_table_kexec(void) |
fc9036ea | 582 | { |
a09bae0f | 583 | e820__update_table(e820_table_kexec); |
fc9036ea | 584 | } |
640e1b38 | 585 | |
fd6493e1 | 586 | #define MAX_GAP_END 0x100000000ull |
640e1b38 | 587 | |
b79cd8f1 | 588 | /* |
640e1b38 | 589 | * Search for a gap in the E820 memory space from 0 to MAX_GAP_END (4GB). |
b79cd8f1 | 590 | */ |
640e1b38 | 591 | static int __init e820_search_gap(unsigned long *gapstart, unsigned long *gapsize) |
b79cd8f1 | 592 | { |
b4ed1d15 | 593 | unsigned long long last = MAX_GAP_END; |
bf495573 | 594 | int i = e820_table->nr_entries; |
b79cd8f1 YL |
595 | int found = 0; |
596 | ||
b79cd8f1 | 597 | while (--i >= 0) { |
bf495573 IM |
598 | unsigned long long start = e820_table->entries[i].addr; |
599 | unsigned long long end = start + e820_table->entries[i].size; | |
b79cd8f1 YL |
600 | |
601 | /* | |
602 | * Since "last" is at most 4GB, we know we'll | |
640e1b38 | 603 | * fit in 32 bits if this condition is true: |
b79cd8f1 YL |
604 | */ |
605 | if (last > end) { | |
606 | unsigned long gap = last - end; | |
607 | ||
3381959d AK |
608 | if (gap >= *gapsize) { |
609 | *gapsize = gap; | |
610 | *gapstart = end; | |
b79cd8f1 YL |
611 | found = 1; |
612 | } | |
613 | } | |
614 | if (start < last) | |
615 | last = start; | |
616 | } | |
3381959d AK |
617 | return found; |
618 | } | |
619 | ||
620 | /* | |
640e1b38 IM |
621 | * Search for the biggest gap in the low 32 bits of the E820 |
622 | * memory space. We pass this space to the PCI subsystem, so | |
623 | * that it can assign MMIO resources for hotplug or | |
624 | * unconfigured devices in. | |
625 | * | |
3381959d AK |
626 | * Hopefully the BIOS let enough space left. |
627 | */ | |
2df908ba | 628 | __init void e820__setup_pci_gap(void) |
3381959d | 629 | { |
5d423ccd | 630 | unsigned long gapstart, gapsize; |
3381959d AK |
631 | int found; |
632 | ||
3381959d | 633 | gapsize = 0x400000; |
b4ed1d15 | 634 | found = e820_search_gap(&gapstart, &gapsize); |
b79cd8f1 | 635 | |
b79cd8f1 | 636 | if (!found) { |
c19a5f35 | 637 | #ifdef CONFIG_X86_64 |
c987d12f | 638 | gapstart = (max_pfn << PAGE_SHIFT) + 1024*1024; |
01259ef1 | 639 | pr_err( |
640e1b38 IM |
640 | "e820: Cannot find an available gap in the 32-bit address range\n" |
641 | "e820: PCI devices with unassigned 32-bit BARs may not work!\n"); | |
c19a5f35 AB |
642 | #else |
643 | gapstart = 0x10000000; | |
b79cd8f1 | 644 | #endif |
c19a5f35 | 645 | } |
b79cd8f1 YL |
646 | |
647 | /* | |
1506c8dc | 648 | * e820__reserve_resources_late() protects stolen RAM already: |
b79cd8f1 | 649 | */ |
5d423ccd | 650 | pci_mem_start = gapstart; |
b79cd8f1 | 651 | |
01259ef1 | 652 | pr_info("e820: [mem %#010lx-%#010lx] available for PCI devices\n", gapstart, gapstart + gapsize - 1); |
b79cd8f1 YL |
653 | } |
654 | ||
47533968 DV |
655 | /* |
656 | * Called late during init, in free_initmem(). | |
657 | * | |
a09bae0f | 658 | * Initial e820_table and e820_table_kexec are largish __initdata arrays. |
640e1b38 IM |
659 | * |
660 | * Copy them to a (usually much smaller) dynamically allocated area that is | |
661 | * sized precisely after the number of e820 entries. | |
662 | * | |
663 | * This is done after we've performed all the fixes and tweaks to the tables. | |
664 | * All functions which modify them are __init functions, which won't exist | |
665 | * after free_initmem(). | |
47533968 | 666 | */ |
0c6fc11a | 667 | __init void e820__reallocate_tables(void) |
47533968 | 668 | { |
61a50101 | 669 | struct e820_table *n; |
47533968 DV |
670 | int size; |
671 | ||
640e1b38 | 672 | size = offsetof(struct e820_table, entries) + sizeof(struct e820_entry)*e820_table->nr_entries; |
47533968 DV |
673 | n = kmalloc(size, GFP_KERNEL); |
674 | BUG_ON(!n); | |
61a50101 IM |
675 | memcpy(n, e820_table, size); |
676 | e820_table = n; | |
47533968 | 677 | |
a09bae0f | 678 | size = offsetof(struct e820_table, entries) + sizeof(struct e820_entry)*e820_table_kexec->nr_entries; |
47533968 DV |
679 | n = kmalloc(size, GFP_KERNEL); |
680 | BUG_ON(!n); | |
a09bae0f CY |
681 | memcpy(n, e820_table_kexec, size); |
682 | e820_table_kexec = n; | |
12df216c CY |
683 | |
684 | size = offsetof(struct e820_table, entries) + sizeof(struct e820_entry)*e820_table_firmware->nr_entries; | |
685 | n = kmalloc(size, GFP_KERNEL); | |
686 | BUG_ON(!n); | |
687 | memcpy(n, e820_table_firmware, size); | |
688 | e820_table_firmware = n; | |
47533968 DV |
689 | } |
690 | ||
640e1b38 IM |
691 | /* |
692 | * Because of the small fixed size of struct boot_params, only the first | |
693 | * 128 E820 memory entries are passed to the kernel via boot_params.e820_table, | |
694 | * the remaining (if any) entries are passed via the SETUP_E820_EXT node of | |
695 | * struct setup_data, which is parsed here. | |
8c5beb50 | 696 | */ |
914053c0 | 697 | void __init e820__memory_setup_extended(u64 phys_addr, u32 data_len) |
8c5beb50 | 698 | { |
8c5beb50 | 699 | int entries; |
7410aa1c | 700 | struct boot_e820_entry *extmap; |
30e46b57 | 701 | struct setup_data *sdata; |
8c5beb50 | 702 | |
30e46b57 | 703 | sdata = early_memremap(phys_addr, data_len); |
d88961b5 | 704 | entries = sdata->len / sizeof(*extmap); |
7410aa1c | 705 | extmap = (struct boot_e820_entry *)(sdata->data); |
640e1b38 | 706 | |
61a50101 | 707 | __append_e820_table(extmap, entries); |
f9748fa0 | 708 | e820__update_table(e820_table); |
640e1b38 | 709 | |
a09bae0f | 710 | memcpy(e820_table_kexec, e820_table, sizeof(*e820_table_kexec)); |
12df216c | 711 | memcpy(e820_table_firmware, e820_table, sizeof(*e820_table_firmware)); |
b7a67e02 | 712 | |
8d4a40bc | 713 | early_memunmap(sdata, data_len); |
01259ef1 | 714 | pr_info("e820: extended physical RAM map:\n"); |
be0c3f0f | 715 | e820__print_table("extended"); |
8c5beb50 HY |
716 | } |
717 | ||
090d7171 | 718 | /* |
bf62f398 | 719 | * Find the ranges of physical addresses that do not correspond to |
090d7171 | 720 | * E820 RAM areas and register the corresponding pages as 'nosave' for |
640e1b38 | 721 | * hibernation (32-bit) or software suspend and suspend to RAM (64-bit). |
bf62f398 | 722 | * |
640e1b38 | 723 | * This function requires the E820 map to be sorted and without any |
84779575 | 724 | * overlapping entries. |
bf62f398 | 725 | */ |
090d7171 | 726 | void __init e820__register_nosave_regions(unsigned long limit_pfn) |
bf62f398 YL |
727 | { |
728 | int i; | |
84779575 | 729 | unsigned long pfn = 0; |
bf62f398 | 730 | |
bf495573 | 731 | for (i = 0; i < e820_table->nr_entries; i++) { |
e5540f87 | 732 | struct e820_entry *entry = &e820_table->entries[i]; |
bf62f398 | 733 | |
e5540f87 IM |
734 | if (pfn < PFN_UP(entry->addr)) |
735 | register_nosave_region(pfn, PFN_UP(entry->addr)); | |
bf62f398 | 736 | |
e5540f87 | 737 | pfn = PFN_DOWN(entry->addr + entry->size); |
ec776ef6 | 738 | |
09821ff1 | 739 | if (entry->type != E820_TYPE_RAM && entry->type != E820_TYPE_RESERVED_KERN) |
e5540f87 | 740 | register_nosave_region(PFN_UP(entry->addr), pfn); |
bf62f398 YL |
741 | |
742 | if (pfn >= limit_pfn) | |
743 | break; | |
744 | } | |
745 | } | |
a4c81cf6 | 746 | |
b54ac6d2 | 747 | #ifdef CONFIG_ACPI |
640e1b38 IM |
748 | /* |
749 | * Register ACPI NVS memory regions, so that we can save/restore them during | |
750 | * hibernation and the subsequent resume: | |
b69edc76 | 751 | */ |
090d7171 | 752 | static int __init e820__register_nvs_regions(void) |
b69edc76 RW |
753 | { |
754 | int i; | |
755 | ||
bf495573 | 756 | for (i = 0; i < e820_table->nr_entries; i++) { |
e5540f87 | 757 | struct e820_entry *entry = &e820_table->entries[i]; |
b69edc76 | 758 | |
09821ff1 | 759 | if (entry->type == E820_TYPE_NVS) |
e5540f87 | 760 | acpi_nvs_register(entry->addr, entry->size); |
b69edc76 RW |
761 | } |
762 | ||
763 | return 0; | |
764 | } | |
090d7171 | 765 | core_initcall(e820__register_nvs_regions); |
b69edc76 RW |
766 | #endif |
767 | ||
2944e16b | 768 | /* |
5da217ca IM |
769 | * Allocate the requested number of bytes with the requsted alignment |
770 | * and return (the physical address) to the caller. Also register this | |
a09bae0f | 771 | * range in the 'kexec' E820 table as a reserved range. |
5da217ca IM |
772 | * |
773 | * This allows kexec to fake a new mptable, as if it came from the real | |
774 | * system. | |
2944e16b | 775 | */ |
5da217ca | 776 | u64 __init e820__memblock_alloc_reserved(u64 size, u64 align) |
2944e16b | 777 | { |
2944e16b | 778 | u64 addr; |
2944e16b | 779 | |
ab5d140b TH |
780 | addr = __memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE); |
781 | if (addr) { | |
a09bae0f CY |
782 | e820__range_update_kexec(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED); |
783 | pr_info("e820: update e820_table_kexec for e820__memblock_alloc_reserved()\n"); | |
784 | e820__update_table_kexec(); | |
61438766 | 785 | } |
2944e16b | 786 | |
2944e16b YL |
787 | return addr; |
788 | } | |
789 | ||
ee0c80fa YL |
790 | #ifdef CONFIG_X86_32 |
791 | # ifdef CONFIG_X86_PAE | |
792 | # define MAX_ARCH_PFN (1ULL<<(36-PAGE_SHIFT)) | |
793 | # else | |
794 | # define MAX_ARCH_PFN (1ULL<<(32-PAGE_SHIFT)) | |
795 | # endif | |
796 | #else /* CONFIG_X86_32 */ | |
bd70e522 | 797 | # define MAX_ARCH_PFN MAXMEM>>PAGE_SHIFT |
ee0c80fa YL |
798 | #endif |
799 | ||
ee0c80fa YL |
800 | /* |
801 | * Find the highest page frame number we have available | |
802 | */ | |
6afc03b8 | 803 | static unsigned long __init e820_end_pfn(unsigned long limit_pfn, enum e820_type type) |
ee0c80fa | 804 | { |
2dc807b3 YL |
805 | int i; |
806 | unsigned long last_pfn = 0; | |
ee0c80fa YL |
807 | unsigned long max_arch_pfn = MAX_ARCH_PFN; |
808 | ||
bf495573 | 809 | for (i = 0; i < e820_table->nr_entries; i++) { |
e5540f87 | 810 | struct e820_entry *entry = &e820_table->entries[i]; |
f361a450 | 811 | unsigned long start_pfn; |
2dc807b3 YL |
812 | unsigned long end_pfn; |
813 | ||
e5540f87 | 814 | if (entry->type != type) |
c22d4c18 | 815 | continue; |
c22d4c18 | 816 | |
e5540f87 IM |
817 | start_pfn = entry->addr >> PAGE_SHIFT; |
818 | end_pfn = (entry->addr + entry->size) >> PAGE_SHIFT; | |
f361a450 YL |
819 | |
820 | if (start_pfn >= limit_pfn) | |
821 | continue; | |
822 | if (end_pfn > limit_pfn) { | |
823 | last_pfn = limit_pfn; | |
824 | break; | |
825 | } | |
2dc807b3 YL |
826 | if (end_pfn > last_pfn) |
827 | last_pfn = end_pfn; | |
828 | } | |
ee0c80fa YL |
829 | |
830 | if (last_pfn > max_arch_pfn) | |
831 | last_pfn = max_arch_pfn; | |
ee0c80fa | 832 | |
01259ef1 | 833 | pr_info("e820: last_pfn = %#lx max_arch_pfn = %#lx\n", |
ee0c80fa YL |
834 | last_pfn, max_arch_pfn); |
835 | return last_pfn; | |
836 | } | |
640e1b38 | 837 | |
0c6fc11a | 838 | unsigned long __init e820__end_of_ram_pfn(void) |
f361a450 | 839 | { |
09821ff1 | 840 | return e820_end_pfn(MAX_ARCH_PFN, E820_TYPE_RAM); |
f361a450 | 841 | } |
ee0c80fa | 842 | |
0c6fc11a | 843 | unsigned long __init e820__end_of_low_ram_pfn(void) |
f361a450 | 844 | { |
09821ff1 | 845 | return e820_end_pfn(1UL << (32 - PAGE_SHIFT), E820_TYPE_RAM); |
f361a450 | 846 | } |
ee0c80fa | 847 | |
8c2103f2 | 848 | static void __init early_panic(char *msg) |
ab4a465e YL |
849 | { |
850 | early_printk(msg); | |
851 | panic(msg); | |
852 | } | |
853 | ||
69a7704d YL |
854 | static int userdef __initdata; |
855 | ||
640e1b38 | 856 | /* The "mem=nopentium" boot option disables 4MB page tables on 32-bit kernels: */ |
ab4a465e YL |
857 | static int __init parse_memopt(char *p) |
858 | { | |
859 | u64 mem_size; | |
860 | ||
861 | if (!p) | |
862 | return -EINVAL; | |
863 | ||
ab4a465e | 864 | if (!strcmp(p, "nopentium")) { |
9a6d44b9 | 865 | #ifdef CONFIG_X86_32 |
ab4a465e YL |
866 | setup_clear_cpu_cap(X86_FEATURE_PSE); |
867 | return 0; | |
9a6d44b9 | 868 | #else |
01259ef1 | 869 | pr_warn("mem=nopentium ignored! (only supported on x86_32)\n"); |
9a6d44b9 | 870 | return -EINVAL; |
ab4a465e | 871 | #endif |
9a6d44b9 | 872 | } |
ab4a465e | 873 | |
69a7704d | 874 | userdef = 1; |
ab4a465e | 875 | mem_size = memparse(p, &p); |
640e1b38 IM |
876 | |
877 | /* Don't remove all memory when getting "mem={invalid}" parameter: */ | |
77eed821 KM |
878 | if (mem_size == 0) |
879 | return -EINVAL; | |
640e1b38 | 880 | |
09821ff1 | 881 | e820__range_remove(mem_size, ULLONG_MAX - mem_size, E820_TYPE_RAM, 1); |
611dfd78 | 882 | |
ab4a465e YL |
883 | return 0; |
884 | } | |
885 | early_param("mem", parse_memopt); | |
886 | ||
9710f581 | 887 | static int __init parse_memmap_one(char *p) |
ab4a465e YL |
888 | { |
889 | char *oldp; | |
890 | u64 start_at, mem_size; | |
891 | ||
a737abd1 CG |
892 | if (!p) |
893 | return -EINVAL; | |
894 | ||
d6be118a | 895 | if (!strncmp(p, "exactmap", 8)) { |
ab4a465e YL |
896 | #ifdef CONFIG_CRASH_DUMP |
897 | /* | |
898 | * If we are doing a crash dump, we still need to know | |
640e1b38 | 899 | * the real memory size before the original memory map is |
ab4a465e YL |
900 | * reset. |
901 | */ | |
0c6fc11a | 902 | saved_max_pfn = e820__end_of_ram_pfn(); |
ab4a465e | 903 | #endif |
bf495573 | 904 | e820_table->nr_entries = 0; |
ab4a465e YL |
905 | userdef = 1; |
906 | return 0; | |
907 | } | |
908 | ||
909 | oldp = p; | |
910 | mem_size = memparse(p, &p); | |
911 | if (p == oldp) | |
912 | return -EINVAL; | |
913 | ||
914 | userdef = 1; | |
915 | if (*p == '@') { | |
916 | start_at = memparse(p+1, &p); | |
09821ff1 | 917 | e820__range_add(start_at, mem_size, E820_TYPE_RAM); |
ab4a465e YL |
918 | } else if (*p == '#') { |
919 | start_at = memparse(p+1, &p); | |
09821ff1 | 920 | e820__range_add(start_at, mem_size, E820_TYPE_ACPI); |
ab4a465e YL |
921 | } else if (*p == '$') { |
922 | start_at = memparse(p+1, &p); | |
09821ff1 | 923 | e820__range_add(start_at, mem_size, E820_TYPE_RESERVED); |
ec776ef6 CH |
924 | } else if (*p == '!') { |
925 | start_at = memparse(p+1, &p); | |
09821ff1 | 926 | e820__range_add(start_at, mem_size, E820_TYPE_PRAM); |
640e1b38 | 927 | } else { |
09821ff1 | 928 | e820__range_remove(mem_size, ULLONG_MAX - mem_size, E820_TYPE_RAM, 1); |
640e1b38 | 929 | } |
7b479bec | 930 | |
ab4a465e YL |
931 | return *p == '\0' ? 0 : -EINVAL; |
932 | } | |
640e1b38 | 933 | |
9710f581 YL |
934 | static int __init parse_memmap_opt(char *str) |
935 | { | |
936 | while (str) { | |
937 | char *k = strchr(str, ','); | |
938 | ||
939 | if (k) | |
940 | *k++ = 0; | |
941 | ||
942 | parse_memmap_one(str); | |
943 | str = k; | |
944 | } | |
945 | ||
946 | return 0; | |
947 | } | |
ab4a465e YL |
948 | early_param("memmap", parse_memmap_opt); |
949 | ||
1a127034 IM |
950 | /* |
951 | * Reserve all entries from the bootloader's extensible data nodes list, | |
952 | * because if present we are going to use it later on to fetch e820 | |
953 | * entries from it: | |
954 | */ | |
955 | void __init e820__reserve_setup_data(void) | |
da92139b IM |
956 | { |
957 | struct setup_data *data; | |
958 | u64 pa_data; | |
959 | ||
960 | pa_data = boot_params.hdr.setup_data; | |
961 | if (!pa_data) | |
962 | return; | |
963 | ||
964 | while (pa_data) { | |
965 | data = early_memremap(pa_data, sizeof(*data)); | |
09821ff1 | 966 | e820__range_update(pa_data, sizeof(*data)+data->len, E820_TYPE_RAM, E820_TYPE_RESERVED_KERN); |
a09bae0f | 967 | e820__range_update_kexec(pa_data, sizeof(*data)+data->len, E820_TYPE_RAM, E820_TYPE_RESERVED_KERN); |
da92139b IM |
968 | pa_data = data->next; |
969 | early_memunmap(data, sizeof(*data)); | |
970 | } | |
971 | ||
f9748fa0 | 972 | e820__update_table(e820_table); |
a09bae0f | 973 | e820__update_table(e820_table_kexec); |
1a127034 IM |
974 | |
975 | pr_info("extended physical RAM map:\n"); | |
be0c3f0f | 976 | e820__print_table("reserve setup_data"); |
da92139b IM |
977 | } |
978 | ||
9641bdaf IM |
979 | /* |
980 | * Called after parse_early_param(), after early parameters (such as mem=) | |
981 | * have been processed, in which case we already have an E820 table filled in | |
982 | * via the parameter callback function(s), but it's not sorted and printed yet: | |
983 | */ | |
984 | void __init e820__finish_early_params(void) | |
ab4a465e YL |
985 | { |
986 | if (userdef) { | |
f9748fa0 | 987 | if (e820__update_table(e820_table) < 0) |
ab4a465e | 988 | early_panic("Invalid user supplied memory map"); |
ab4a465e | 989 | |
01259ef1 | 990 | pr_info("e820: user-defined physical RAM map:\n"); |
be0c3f0f | 991 | e820__print_table("user"); |
ab4a465e YL |
992 | } |
993 | } | |
41c094fd | 994 | |
c594761d | 995 | static const char *__init e820_type_to_string(struct e820_entry *entry) |
5dfcf14d | 996 | { |
c594761d | 997 | switch (entry->type) { |
09821ff1 IM |
998 | case E820_TYPE_RESERVED_KERN: /* Fall-through: */ |
999 | case E820_TYPE_RAM: return "System RAM"; | |
1000 | case E820_TYPE_ACPI: return "ACPI Tables"; | |
1001 | case E820_TYPE_NVS: return "ACPI Non-volatile Storage"; | |
1002 | case E820_TYPE_UNUSABLE: return "Unusable memory"; | |
1003 | case E820_TYPE_PRAM: return "Persistent Memory (legacy)"; | |
1004 | case E820_TYPE_PMEM: return "Persistent Memory"; | |
c5231a57 IM |
1005 | case E820_TYPE_RESERVED: return "Reserved"; |
1006 | default: return "Unknown E820 type"; | |
5dfcf14d BW |
1007 | } |
1008 | } | |
1009 | ||
c594761d | 1010 | static unsigned long __init e820_type_to_iomem_type(struct e820_entry *entry) |
f33b14a4 | 1011 | { |
c594761d | 1012 | switch (entry->type) { |
09821ff1 IM |
1013 | case E820_TYPE_RESERVED_KERN: /* Fall-through: */ |
1014 | case E820_TYPE_RAM: return IORESOURCE_SYSTEM_RAM; | |
1015 | case E820_TYPE_ACPI: /* Fall-through: */ | |
1016 | case E820_TYPE_NVS: /* Fall-through: */ | |
1017 | case E820_TYPE_UNUSABLE: /* Fall-through: */ | |
1018 | case E820_TYPE_PRAM: /* Fall-through: */ | |
1019 | case E820_TYPE_PMEM: /* Fall-through: */ | |
c5231a57 | 1020 | case E820_TYPE_RESERVED: /* Fall-through: */ |
09821ff1 | 1021 | default: return IORESOURCE_MEM; |
f33b14a4 TK |
1022 | } |
1023 | } | |
1024 | ||
c594761d | 1025 | static unsigned long __init e820_type_to_iores_desc(struct e820_entry *entry) |
f33b14a4 | 1026 | { |
c594761d | 1027 | switch (entry->type) { |
09821ff1 IM |
1028 | case E820_TYPE_ACPI: return IORES_DESC_ACPI_TABLES; |
1029 | case E820_TYPE_NVS: return IORES_DESC_ACPI_NV_STORAGE; | |
1030 | case E820_TYPE_PMEM: return IORES_DESC_PERSISTENT_MEMORY; | |
1031 | case E820_TYPE_PRAM: return IORES_DESC_PERSISTENT_MEMORY_LEGACY; | |
1032 | case E820_TYPE_RESERVED_KERN: /* Fall-through: */ | |
1033 | case E820_TYPE_RAM: /* Fall-through: */ | |
1034 | case E820_TYPE_UNUSABLE: /* Fall-through: */ | |
c5231a57 | 1035 | case E820_TYPE_RESERVED: /* Fall-through: */ |
09821ff1 | 1036 | default: return IORES_DESC_NONE; |
f33b14a4 TK |
1037 | } |
1038 | } | |
1039 | ||
c5231a57 | 1040 | static bool __init do_mark_busy(enum e820_type type, struct resource *res) |
ad5fb870 DW |
1041 | { |
1042 | /* this is the legacy bios/dos rom-shadow + mmio region */ | |
1043 | if (res->start < (1ULL<<20)) | |
1044 | return true; | |
1045 | ||
1046 | /* | |
1047 | * Treat persistent memory like device memory, i.e. reserve it | |
1048 | * for exclusive use of a driver | |
1049 | */ | |
1050 | switch (type) { | |
09821ff1 IM |
1051 | case E820_TYPE_RESERVED: |
1052 | case E820_TYPE_PRAM: | |
1053 | case E820_TYPE_PMEM: | |
ad5fb870 | 1054 | return false; |
c5231a57 IM |
1055 | case E820_TYPE_RESERVED_KERN: |
1056 | case E820_TYPE_RAM: | |
1057 | case E820_TYPE_ACPI: | |
1058 | case E820_TYPE_NVS: | |
1059 | case E820_TYPE_UNUSABLE: | |
ad5fb870 DW |
1060 | default: |
1061 | return true; | |
1062 | } | |
1063 | } | |
1064 | ||
41c094fd | 1065 | /* |
640e1b38 | 1066 | * Mark E820 reserved areas as busy for the resource manager: |
41c094fd | 1067 | */ |
640e1b38 | 1068 | |
a5444d15 | 1069 | static struct resource __initdata *e820_res; |
640e1b38 | 1070 | |
1506c8dc | 1071 | void __init e820__reserve_resources(void) |
41c094fd YL |
1072 | { |
1073 | int i; | |
58f7c988 | 1074 | struct resource *res; |
a5444d15 | 1075 | u64 end; |
41c094fd | 1076 | |
c594761d | 1077 | res = alloc_bootmem(sizeof(*res) * e820_table->nr_entries); |
58f7c988 | 1078 | e820_res = res; |
c594761d | 1079 | |
bf495573 | 1080 | for (i = 0; i < e820_table->nr_entries; i++) { |
c594761d IM |
1081 | struct e820_entry *entry = e820_table->entries + i; |
1082 | ||
1083 | end = entry->addr + entry->size - 1; | |
8308c54d | 1084 | if (end != (resource_size_t)end) { |
41c094fd YL |
1085 | res++; |
1086 | continue; | |
1087 | } | |
c594761d IM |
1088 | res->start = entry->addr; |
1089 | res->end = end; | |
1090 | res->name = e820_type_to_string(entry); | |
1091 | res->flags = e820_type_to_iomem_type(entry); | |
1092 | res->desc = e820_type_to_iores_desc(entry); | |
a5444d15 IM |
1093 | |
1094 | /* | |
1506c8dc IM |
1095 | * Don't register the region that could be conflicted with |
1096 | * PCI device BAR resources and insert them later in | |
1097 | * pcibios_resource_survey(): | |
a5444d15 | 1098 | */ |
c594761d | 1099 | if (do_mark_busy(entry->type, res)) { |
1f987577 | 1100 | res->flags |= IORESOURCE_BUSY; |
58f7c988 | 1101 | insert_resource(&iomem_resource, res); |
1f987577 | 1102 | } |
41c094fd YL |
1103 | res++; |
1104 | } | |
5dfcf14d | 1105 | |
12df216c CY |
1106 | /* Expose the bootloader-provided memory layout to the sysfs. */ |
1107 | for (i = 0; i < e820_table_firmware->nr_entries; i++) { | |
1108 | struct e820_entry *entry = e820_table_firmware->entries + i; | |
640e1b38 | 1109 | |
c594761d | 1110 | firmware_map_add_early(entry->addr, entry->addr + entry->size, e820_type_to_string(entry)); |
5dfcf14d | 1111 | } |
41c094fd YL |
1112 | } |
1113 | ||
1506c8dc IM |
1114 | /* |
1115 | * How much should we pad the end of RAM, depending on where it is? | |
1116 | */ | |
8c2103f2 | 1117 | static unsigned long __init ram_alignment(resource_size_t pos) |
45fbe3ee LT |
1118 | { |
1119 | unsigned long mb = pos >> 20; | |
1120 | ||
1121 | /* To 64kB in the first megabyte */ | |
1122 | if (!mb) | |
1123 | return 64*1024; | |
1124 | ||
1125 | /* To 1MB in the first 16MB */ | |
1126 | if (mb < 16) | |
1127 | return 1024*1024; | |
1128 | ||
15b812f1 YL |
1129 | /* To 64MB for anything above that */ |
1130 | return 64*1024*1024; | |
45fbe3ee LT |
1131 | } |
1132 | ||
7c5371c4 YL |
1133 | #define MAX_RESOURCE_SIZE ((resource_size_t)-1) |
1134 | ||
1506c8dc | 1135 | void __init e820__reserve_resources_late(void) |
58f7c988 YL |
1136 | { |
1137 | int i; | |
1138 | struct resource *res; | |
1139 | ||
1140 | res = e820_res; | |
bf495573 | 1141 | for (i = 0; i < e820_table->nr_entries; i++) { |
a5444d15 | 1142 | if (!res->parent && res->end) |
1f987577 | 1143 | insert_resource_expand_to_fit(&iomem_resource, res); |
58f7c988 YL |
1144 | res++; |
1145 | } | |
45fbe3ee LT |
1146 | |
1147 | /* | |
640e1b38 | 1148 | * Try to bump up RAM regions to reasonable boundaries, to |
45fbe3ee LT |
1149 | * avoid stolen RAM: |
1150 | */ | |
bf495573 IM |
1151 | for (i = 0; i < e820_table->nr_entries; i++) { |
1152 | struct e820_entry *entry = &e820_table->entries[i]; | |
7c5371c4 | 1153 | u64 start, end; |
45fbe3ee | 1154 | |
09821ff1 | 1155 | if (entry->type != E820_TYPE_RAM) |
45fbe3ee | 1156 | continue; |
640e1b38 | 1157 | |
45fbe3ee | 1158 | start = entry->addr + entry->size; |
7c5371c4 YL |
1159 | end = round_up(start, ram_alignment(start)) - 1; |
1160 | if (end > MAX_RESOURCE_SIZE) | |
1161 | end = MAX_RESOURCE_SIZE; | |
1162 | if (start >= end) | |
45fbe3ee | 1163 | continue; |
640e1b38 | 1164 | |
e22af0be | 1165 | printk(KERN_DEBUG "e820: reserve RAM buffer [mem %#010llx-%#010llx]\n", start, end); |
640e1b38 | 1166 | reserve_region_with_split(&iomem_resource, start, end, "RAM buffer"); |
45fbe3ee | 1167 | } |
58f7c988 YL |
1168 | } |
1169 | ||
640e1b38 IM |
1170 | /* |
1171 | * Pass the firmware (bootloader) E820 map to the kernel and process it: | |
1172 | */ | |
103e2063 | 1173 | char *__init e820__memory_setup_default(void) |
064d25f1 YL |
1174 | { |
1175 | char *who = "BIOS-e820"; | |
640e1b38 | 1176 | |
064d25f1 YL |
1177 | /* |
1178 | * Try to copy the BIOS-supplied E820-map. | |
1179 | * | |
1180 | * Otherwise fake a memory map; one section from 0k->640k, | |
1181 | * the next section from 1mb->appropriate_mem_k | |
1182 | */ | |
640e1b38 | 1183 | if (append_e820_table(boot_params.e820_table, boot_params.e820_entries) < 0) { |
95a71a45 | 1184 | u64 mem_size; |
064d25f1 | 1185 | |
640e1b38 IM |
1186 | /* Compare results from other methods and take the one that gives more RAM: */ |
1187 | if (boot_params.alt_mem_k < boot_params.screen_info.ext_mem_k) { | |
064d25f1 YL |
1188 | mem_size = boot_params.screen_info.ext_mem_k; |
1189 | who = "BIOS-88"; | |
1190 | } else { | |
1191 | mem_size = boot_params.alt_mem_k; | |
1192 | who = "BIOS-e801"; | |
1193 | } | |
1194 | ||
bf495573 | 1195 | e820_table->nr_entries = 0; |
09821ff1 IM |
1196 | e820__range_add(0, LOWMEMSIZE(), E820_TYPE_RAM); |
1197 | e820__range_add(HIGH_MEMORY, mem_size << 10, E820_TYPE_RAM); | |
064d25f1 YL |
1198 | } |
1199 | ||
7410aa1c IM |
1200 | /* We just appended a lot of ranges, sanitize the table: */ |
1201 | e820__update_table(e820_table); | |
1202 | ||
064d25f1 YL |
1203 | return who; |
1204 | } | |
1205 | ||
103e2063 IM |
1206 | /* |
1207 | * Calls e820__memory_setup_default() in essence to pick up the firmware/bootloader | |
1208 | * E820 map - with an optional platform quirk available for virtual platforms | |
1209 | * to override this method of boot environment processing: | |
1210 | */ | |
1211 | void __init e820__memory_setup(void) | |
064d25f1 | 1212 | { |
0be15526 YL |
1213 | char *who; |
1214 | ||
09c51513 | 1215 | /* This is a firmware interface ABI - make sure we don't break it: */ |
7410aa1c | 1216 | BUILD_BUG_ON(sizeof(struct boot_e820_entry) != 20); |
09c51513 | 1217 | |
6b18ae3e | 1218 | who = x86_init.resources.memory_setup(); |
640e1b38 | 1219 | |
a09bae0f | 1220 | memcpy(e820_table_kexec, e820_table, sizeof(*e820_table_kexec)); |
12df216c | 1221 | memcpy(e820_table_firmware, e820_table, sizeof(*e820_table_firmware)); |
640e1b38 | 1222 | |
01259ef1 | 1223 | pr_info("e820: BIOS-provided physical RAM map:\n"); |
be0c3f0f | 1224 | e820__print_table(who); |
064d25f1 | 1225 | } |
72d7c3b3 | 1226 | |
4918e228 | 1227 | void __init e820__memblock_setup(void) |
72d7c3b3 YL |
1228 | { |
1229 | int i; | |
1230 | u64 end; | |
1231 | ||
1232 | /* | |
4918e228 IM |
1233 | * The bootstrap memblock region count maximum is 128 entries |
1234 | * (INIT_MEMBLOCK_REGIONS), but EFI might pass us more E820 entries | |
1235 | * than that - so allow memblock resizing. | |
1236 | * | |
1237 | * This is safe, because this call happens pretty late during x86 setup, | |
1238 | * so we know about reserved memory regions already. (This is important | |
1239 | * so that memblock resizing does no stomp over reserved areas.) | |
72d7c3b3 | 1240 | */ |
1aadc056 | 1241 | memblock_allow_resize(); |
72d7c3b3 | 1242 | |
bf495573 | 1243 | for (i = 0; i < e820_table->nr_entries; i++) { |
e5540f87 | 1244 | struct e820_entry *entry = &e820_table->entries[i]; |
72d7c3b3 | 1245 | |
e5540f87 | 1246 | end = entry->addr + entry->size; |
72d7c3b3 YL |
1247 | if (end != (resource_size_t)end) |
1248 | continue; | |
1249 | ||
09821ff1 | 1250 | if (entry->type != E820_TYPE_RAM && entry->type != E820_TYPE_RESERVED_KERN) |
72d7c3b3 YL |
1251 | continue; |
1252 | ||
e5540f87 | 1253 | memblock_add(entry->addr, entry->size); |
72d7c3b3 YL |
1254 | } |
1255 | ||
4918e228 | 1256 | /* Throw away partial pages: */ |
6ede1fd3 YL |
1257 | memblock_trim_memory(PAGE_SIZE); |
1258 | ||
72d7c3b3 YL |
1259 | memblock_dump_all(); |
1260 | } |