Commit | Line | Data |
---|---|---|
eb39c880 MS |
1 | /* |
2 | * Firmware Assisted dump: A robust mechanism to get reliable kernel crash | |
3 | * dump with assistance from firmware. This approach does not use kexec, | |
4 | * instead firmware assists in booting the kdump kernel while preserving | |
5 | * memory contents. The most of the code implementation has been adapted | |
6 | * from phyp assisted dump implementation written by Linas Vepstas and | |
7 | * Manish Ahuja | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, write to the Free Software | |
21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
22 | * | |
23 | * Copyright 2011 IBM Corporation | |
24 | * Author: Mahesh Salgaonkar <mahesh@linux.vnet.ibm.com> | |
25 | */ | |
26 | ||
27 | #undef DEBUG | |
28 | #define pr_fmt(fmt) "fadump: " fmt | |
29 | ||
30 | #include <linux/string.h> | |
31 | #include <linux/memblock.h> | |
3ccc00a7 | 32 | #include <linux/delay.h> |
3ccc00a7 | 33 | #include <linux/seq_file.h> |
2df173d9 | 34 | #include <linux/crash_dump.h> |
b500afff MS |
35 | #include <linux/kobject.h> |
36 | #include <linux/sysfs.h> | |
a5818313 | 37 | #include <linux/slab.h> |
a4e92ce8 | 38 | #include <linux/cma.h> |
eb39c880 | 39 | |
7644d581 | 40 | #include <asm/debugfs.h> |
eb39c880 MS |
41 | #include <asm/page.h> |
42 | #include <asm/prom.h> | |
43 | #include <asm/rtas.h> | |
44 | #include <asm/fadump.h> | |
cad3c834 | 45 | #include <asm/setup.h> |
eb39c880 MS |
46 | |
47 | static struct fw_dump fw_dump; | |
3ccc00a7 MS |
48 | static struct fadump_mem_struct fdm; |
49 | static const struct fadump_mem_struct *fdm_active; | |
a4e92ce8 MS |
50 | #ifdef CONFIG_CMA |
51 | static struct cma *fadump_cma; | |
52 | #endif | |
3ccc00a7 MS |
53 | |
54 | static DEFINE_MUTEX(fadump_mutex); | |
1bd6a1c4 HB |
55 | struct fad_crash_memory_ranges *crash_memory_ranges; |
56 | int crash_memory_ranges_size; | |
2df173d9 | 57 | int crash_mem_ranges; |
1bd6a1c4 | 58 | int max_crash_mem_ranges; |
eb39c880 | 59 | |
a4e92ce8 MS |
60 | #ifdef CONFIG_CMA |
61 | /* | |
62 | * fadump_cma_init() - Initialize CMA area from a fadump reserved memory | |
63 | * | |
64 | * This function initializes CMA area from fadump reserved memory. | |
65 | * The total size of fadump reserved memory covers for boot memory size | |
66 | * + cpu data size + hpte size and metadata. | |
67 | * Initialize only the area equivalent to boot memory size for CMA use. | |
68 | * The reamining portion of fadump reserved memory will be not given | |
69 | * to CMA and pages for thoes will stay reserved. boot memory size is | |
70 | * aligned per CMA requirement to satisy cma_init_reserved_mem() call. | |
71 | * But for some reason even if it fails we still have the memory reservation | |
72 | * with us and we can still continue doing fadump. | |
73 | */ | |
74 | int __init fadump_cma_init(void) | |
75 | { | |
76 | unsigned long long base, size; | |
77 | int rc; | |
78 | ||
79 | if (!fw_dump.fadump_enabled) | |
80 | return 0; | |
81 | ||
82 | /* | |
83 | * Do not use CMA if user has provided fadump=nocma kernel parameter. | |
84 | * Return 1 to continue with fadump old behaviour. | |
85 | */ | |
86 | if (fw_dump.nocma) | |
87 | return 1; | |
88 | ||
89 | base = fw_dump.reserve_dump_area_start; | |
90 | size = fw_dump.boot_memory_size; | |
91 | ||
92 | if (!size) | |
93 | return 0; | |
94 | ||
95 | rc = cma_init_reserved_mem(base, size, 0, "fadump_cma", &fadump_cma); | |
96 | if (rc) { | |
97 | pr_err("Failed to init cma area for firmware-assisted dump,%d\n", rc); | |
98 | /* | |
99 | * Though the CMA init has failed we still have memory | |
100 | * reservation with us. The reserved memory will be | |
101 | * blocked from production system usage. Hence return 1, | |
102 | * so that we can continue with fadump. | |
103 | */ | |
104 | return 1; | |
105 | } | |
106 | ||
107 | /* | |
108 | * So we now have successfully initialized cma area for fadump. | |
109 | */ | |
110 | pr_info("Initialized 0x%lx bytes cma area at %ldMB from 0x%lx " | |
111 | "bytes of memory reserved for firmware-assisted dump\n", | |
112 | cma_get_size(fadump_cma), | |
113 | (unsigned long)cma_get_base(fadump_cma) >> 20, | |
114 | fw_dump.reserve_dump_area_size); | |
115 | return 1; | |
116 | } | |
117 | #else | |
118 | static int __init fadump_cma_init(void) { return 1; } | |
119 | #endif /* CONFIG_CMA */ | |
120 | ||
eb39c880 MS |
121 | /* Scan the Firmware Assisted dump configuration details. */ |
122 | int __init early_init_dt_scan_fw_dump(unsigned long node, | |
123 | const char *uname, int depth, void *data) | |
124 | { | |
9d0c4dfe | 125 | const __be32 *sections; |
eb39c880 | 126 | int i, num_sections; |
9d0c4dfe | 127 | int size; |
408cddd9 | 128 | const __be32 *token; |
eb39c880 MS |
129 | |
130 | if (depth != 1 || strcmp(uname, "rtas") != 0) | |
131 | return 0; | |
132 | ||
133 | /* | |
134 | * Check if Firmware Assisted dump is supported. if yes, check | |
135 | * if dump has been initiated on last reboot. | |
136 | */ | |
137 | token = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump", NULL); | |
138 | if (!token) | |
a7d04317 | 139 | return 1; |
eb39c880 MS |
140 | |
141 | fw_dump.fadump_supported = 1; | |
408cddd9 | 142 | fw_dump.ibm_configure_kernel_dump = be32_to_cpu(*token); |
eb39c880 MS |
143 | |
144 | /* | |
145 | * The 'ibm,kernel-dump' rtas node is present only if there is | |
146 | * dump data waiting for us. | |
147 | */ | |
3ccc00a7 MS |
148 | fdm_active = of_get_flat_dt_prop(node, "ibm,kernel-dump", NULL); |
149 | if (fdm_active) | |
eb39c880 MS |
150 | fw_dump.dump_active = 1; |
151 | ||
152 | /* Get the sizes required to store dump data for the firmware provided | |
153 | * dump sections. | |
154 | * For each dump section type supported, a 32bit cell which defines | |
155 | * the ID of a supported section followed by two 32 bit cells which | |
156 | * gives teh size of the section in bytes. | |
157 | */ | |
158 | sections = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump-sizes", | |
159 | &size); | |
160 | ||
161 | if (!sections) | |
a7d04317 | 162 | return 1; |
eb39c880 MS |
163 | |
164 | num_sections = size / (3 * sizeof(u32)); | |
165 | ||
166 | for (i = 0; i < num_sections; i++, sections += 3) { | |
167 | u32 type = (u32)of_read_number(sections, 1); | |
168 | ||
169 | switch (type) { | |
170 | case FADUMP_CPU_STATE_DATA: | |
171 | fw_dump.cpu_state_data_size = | |
172 | of_read_ulong(§ions[1], 2); | |
173 | break; | |
174 | case FADUMP_HPTE_REGION: | |
175 | fw_dump.hpte_region_size = | |
176 | of_read_ulong(§ions[1], 2); | |
177 | break; | |
178 | } | |
179 | } | |
a7d04317 | 180 | |
eb39c880 MS |
181 | return 1; |
182 | } | |
183 | ||
eae0dfcc HB |
184 | /* |
185 | * If fadump is registered, check if the memory provided | |
186 | * falls within boot memory area. | |
187 | */ | |
188 | int is_fadump_boot_memory_area(u64 addr, ulong size) | |
189 | { | |
190 | if (!fw_dump.dump_registered) | |
191 | return 0; | |
192 | ||
193 | return (addr + size) > RMA_START && addr <= fw_dump.boot_memory_size; | |
194 | } | |
195 | ||
6fcd6baa NP |
196 | int should_fadump_crash(void) |
197 | { | |
198 | if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr) | |
199 | return 0; | |
200 | return 1; | |
201 | } | |
202 | ||
3ccc00a7 MS |
203 | int is_fadump_active(void) |
204 | { | |
205 | return fw_dump.dump_active; | |
206 | } | |
207 | ||
a5a05b91 HB |
208 | /* |
209 | * Returns 1, if there are no holes in boot memory area, | |
210 | * 0 otherwise. | |
211 | */ | |
212 | static int is_boot_memory_area_contiguous(void) | |
213 | { | |
214 | struct memblock_region *reg; | |
215 | unsigned long tstart, tend; | |
216 | unsigned long start_pfn = PHYS_PFN(RMA_START); | |
217 | unsigned long end_pfn = PHYS_PFN(RMA_START + fw_dump.boot_memory_size); | |
218 | unsigned int ret = 0; | |
219 | ||
220 | for_each_memblock(memory, reg) { | |
221 | tstart = max(start_pfn, memblock_region_memory_base_pfn(reg)); | |
222 | tend = min(end_pfn, memblock_region_memory_end_pfn(reg)); | |
223 | if (tstart < tend) { | |
224 | /* Memory hole from start_pfn to tstart */ | |
225 | if (tstart > start_pfn) | |
226 | break; | |
227 | ||
228 | if (tend == end_pfn) { | |
229 | ret = 1; | |
230 | break; | |
231 | } | |
232 | ||
233 | start_pfn = tend + 1; | |
234 | } | |
235 | } | |
236 | ||
237 | return ret; | |
238 | } | |
239 | ||
f86593be MS |
240 | /* |
241 | * Returns true, if there are no holes in reserved memory area, | |
242 | * false otherwise. | |
243 | */ | |
244 | static bool is_reserved_memory_area_contiguous(void) | |
245 | { | |
246 | struct memblock_region *reg; | |
247 | unsigned long start, end; | |
248 | unsigned long d_start = fw_dump.reserve_dump_area_start; | |
249 | unsigned long d_end = d_start + fw_dump.reserve_dump_area_size; | |
250 | ||
251 | for_each_memblock(memory, reg) { | |
252 | start = max(d_start, (unsigned long)reg->base); | |
253 | end = min(d_end, (unsigned long)(reg->base + reg->size)); | |
254 | if (d_start < end) { | |
255 | /* Memory hole from d_start to start */ | |
256 | if (start > d_start) | |
257 | break; | |
258 | ||
259 | if (end == d_end) | |
260 | return true; | |
261 | ||
262 | d_start = end + 1; | |
263 | } | |
264 | } | |
265 | ||
266 | return false; | |
267 | } | |
268 | ||
3ccc00a7 MS |
269 | /* Print firmware assisted dump configurations for debugging purpose. */ |
270 | static void fadump_show_config(void) | |
271 | { | |
272 | pr_debug("Support for firmware-assisted dump (fadump): %s\n", | |
273 | (fw_dump.fadump_supported ? "present" : "no support")); | |
274 | ||
275 | if (!fw_dump.fadump_supported) | |
276 | return; | |
277 | ||
278 | pr_debug("Fadump enabled : %s\n", | |
279 | (fw_dump.fadump_enabled ? "yes" : "no")); | |
280 | pr_debug("Dump Active : %s\n", | |
281 | (fw_dump.dump_active ? "yes" : "no")); | |
282 | pr_debug("Dump section sizes:\n"); | |
283 | pr_debug(" CPU state data size: %lx\n", fw_dump.cpu_state_data_size); | |
284 | pr_debug(" HPTE region size : %lx\n", fw_dump.hpte_region_size); | |
285 | pr_debug("Boot memory size : %lx\n", fw_dump.boot_memory_size); | |
286 | } | |
287 | ||
288 | static unsigned long init_fadump_mem_struct(struct fadump_mem_struct *fdm, | |
289 | unsigned long addr) | |
290 | { | |
291 | if (!fdm) | |
292 | return 0; | |
293 | ||
294 | memset(fdm, 0, sizeof(struct fadump_mem_struct)); | |
295 | addr = addr & PAGE_MASK; | |
296 | ||
408cddd9 HB |
297 | fdm->header.dump_format_version = cpu_to_be32(0x00000001); |
298 | fdm->header.dump_num_sections = cpu_to_be16(3); | |
3ccc00a7 MS |
299 | fdm->header.dump_status_flag = 0; |
300 | fdm->header.offset_first_dump_section = | |
408cddd9 | 301 | cpu_to_be32((u32)offsetof(struct fadump_mem_struct, cpu_state_data)); |
3ccc00a7 MS |
302 | |
303 | /* | |
304 | * Fields for disk dump option. | |
305 | * We are not using disk dump option, hence set these fields to 0. | |
306 | */ | |
307 | fdm->header.dd_block_size = 0; | |
308 | fdm->header.dd_block_offset = 0; | |
309 | fdm->header.dd_num_blocks = 0; | |
310 | fdm->header.dd_offset_disk_path = 0; | |
311 | ||
312 | /* set 0 to disable an automatic dump-reboot. */ | |
313 | fdm->header.max_time_auto = 0; | |
314 | ||
315 | /* Kernel dump sections */ | |
316 | /* cpu state data section. */ | |
408cddd9 HB |
317 | fdm->cpu_state_data.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG); |
318 | fdm->cpu_state_data.source_data_type = cpu_to_be16(FADUMP_CPU_STATE_DATA); | |
3ccc00a7 | 319 | fdm->cpu_state_data.source_address = 0; |
408cddd9 HB |
320 | fdm->cpu_state_data.source_len = cpu_to_be64(fw_dump.cpu_state_data_size); |
321 | fdm->cpu_state_data.destination_address = cpu_to_be64(addr); | |
3ccc00a7 MS |
322 | addr += fw_dump.cpu_state_data_size; |
323 | ||
324 | /* hpte region section */ | |
408cddd9 HB |
325 | fdm->hpte_region.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG); |
326 | fdm->hpte_region.source_data_type = cpu_to_be16(FADUMP_HPTE_REGION); | |
3ccc00a7 | 327 | fdm->hpte_region.source_address = 0; |
408cddd9 HB |
328 | fdm->hpte_region.source_len = cpu_to_be64(fw_dump.hpte_region_size); |
329 | fdm->hpte_region.destination_address = cpu_to_be64(addr); | |
3ccc00a7 MS |
330 | addr += fw_dump.hpte_region_size; |
331 | ||
332 | /* RMA region section */ | |
408cddd9 HB |
333 | fdm->rmr_region.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG); |
334 | fdm->rmr_region.source_data_type = cpu_to_be16(FADUMP_REAL_MODE_REGION); | |
335 | fdm->rmr_region.source_address = cpu_to_be64(RMA_START); | |
336 | fdm->rmr_region.source_len = cpu_to_be64(fw_dump.boot_memory_size); | |
337 | fdm->rmr_region.destination_address = cpu_to_be64(addr); | |
3ccc00a7 MS |
338 | addr += fw_dump.boot_memory_size; |
339 | ||
340 | return addr; | |
341 | } | |
342 | ||
eb39c880 MS |
343 | /** |
344 | * fadump_calculate_reserve_size(): reserve variable boot area 5% of System RAM | |
345 | * | |
346 | * Function to find the largest memory size we need to reserve during early | |
347 | * boot process. This will be the size of the memory that is required for a | |
348 | * kernel to boot successfully. | |
349 | * | |
350 | * This function has been taken from phyp-assisted dump feature implementation. | |
351 | * | |
352 | * returns larger of 256MB or 5% rounded down to multiples of 256MB. | |
353 | * | |
354 | * TODO: Come up with better approach to find out more accurate memory size | |
355 | * that is required for a kernel to boot successfully. | |
356 | * | |
357 | */ | |
358 | static inline unsigned long fadump_calculate_reserve_size(void) | |
359 | { | |
11550dc0 HB |
360 | int ret; |
361 | unsigned long long base, size; | |
eb39c880 | 362 | |
81d9eca5 HB |
363 | if (fw_dump.reserve_bootvar) |
364 | pr_warn("'fadump_reserve_mem=' parameter is deprecated in favor of 'crashkernel=' parameter.\n"); | |
365 | ||
eb39c880 | 366 | /* |
11550dc0 | 367 | * Check if the size is specified through crashkernel= cmdline |
e7467dc6 HB |
368 | * option. If yes, then use that but ignore base as fadump reserves |
369 | * memory at a predefined offset. | |
eb39c880 | 370 | */ |
11550dc0 HB |
371 | ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(), |
372 | &size, &base); | |
373 | if (ret == 0 && size > 0) { | |
48a316e3 HB |
374 | unsigned long max_size; |
375 | ||
81d9eca5 HB |
376 | if (fw_dump.reserve_bootvar) |
377 | pr_info("Using 'crashkernel=' parameter for memory reservation.\n"); | |
378 | ||
11550dc0 | 379 | fw_dump.reserve_bootvar = (unsigned long)size; |
48a316e3 HB |
380 | |
381 | /* | |
382 | * Adjust if the boot memory size specified is above | |
383 | * the upper limit. | |
384 | */ | |
385 | max_size = memblock_phys_mem_size() / MAX_BOOT_MEM_RATIO; | |
386 | if (fw_dump.reserve_bootvar > max_size) { | |
387 | fw_dump.reserve_bootvar = max_size; | |
388 | pr_info("Adjusted boot memory size to %luMB\n", | |
389 | (fw_dump.reserve_bootvar >> 20)); | |
390 | } | |
391 | ||
eb39c880 | 392 | return fw_dump.reserve_bootvar; |
81d9eca5 HB |
393 | } else if (fw_dump.reserve_bootvar) { |
394 | /* | |
395 | * 'fadump_reserve_mem=' is being used to reserve memory | |
396 | * for firmware-assisted dump. | |
397 | */ | |
398 | return fw_dump.reserve_bootvar; | |
11550dc0 | 399 | } |
eb39c880 MS |
400 | |
401 | /* divide by 20 to get 5% of value */ | |
48a316e3 | 402 | size = memblock_phys_mem_size() / 20; |
eb39c880 MS |
403 | |
404 | /* round it down in multiples of 256 */ | |
405 | size = size & ~0x0FFFFFFFUL; | |
406 | ||
407 | /* Truncate to memory_limit. We don't want to over reserve the memory.*/ | |
408 | if (memory_limit && size > memory_limit) | |
409 | size = memory_limit; | |
410 | ||
411 | return (size > MIN_BOOT_MEM ? size : MIN_BOOT_MEM); | |
412 | } | |
413 | ||
414 | /* | |
415 | * Calculate the total memory size required to be reserved for | |
416 | * firmware-assisted dump registration. | |
417 | */ | |
418 | static unsigned long get_fadump_area_size(void) | |
419 | { | |
420 | unsigned long size = 0; | |
421 | ||
422 | size += fw_dump.cpu_state_data_size; | |
423 | size += fw_dump.hpte_region_size; | |
424 | size += fw_dump.boot_memory_size; | |
2df173d9 MS |
425 | size += sizeof(struct fadump_crash_info_header); |
426 | size += sizeof(struct elfhdr); /* ELF core header.*/ | |
ebaeb5ae | 427 | size += sizeof(struct elf_phdr); /* place holder for cpu notes */ |
2df173d9 MS |
428 | /* Program headers for crash memory regions. */ |
429 | size += sizeof(struct elf_phdr) * (memblock_num_regions(memory) + 2); | |
eb39c880 MS |
430 | |
431 | size = PAGE_ALIGN(size); | |
432 | return size; | |
433 | } | |
434 | ||
b71a693d MS |
435 | static void __init fadump_reserve_crash_area(unsigned long base, |
436 | unsigned long size) | |
437 | { | |
438 | struct memblock_region *reg; | |
439 | unsigned long mstart, mend, msize; | |
440 | ||
441 | for_each_memblock(memory, reg) { | |
442 | mstart = max_t(unsigned long, base, reg->base); | |
443 | mend = reg->base + reg->size; | |
444 | mend = min(base + size, mend); | |
445 | ||
446 | if (mstart < mend) { | |
447 | msize = mend - mstart; | |
448 | memblock_reserve(mstart, msize); | |
449 | pr_info("Reserved %ldMB of memory at %#016lx for saving crash dump\n", | |
450 | (msize >> 20), mstart); | |
451 | } | |
452 | } | |
453 | } | |
454 | ||
eb39c880 MS |
455 | int __init fadump_reserve_mem(void) |
456 | { | |
457 | unsigned long base, size, memory_boundary; | |
458 | ||
459 | if (!fw_dump.fadump_enabled) | |
460 | return 0; | |
461 | ||
462 | if (!fw_dump.fadump_supported) { | |
463 | printk(KERN_INFO "Firmware-assisted dump is not supported on" | |
464 | " this hardware\n"); | |
465 | fw_dump.fadump_enabled = 0; | |
466 | return 0; | |
467 | } | |
3ccc00a7 MS |
468 | /* |
469 | * Initialize boot memory size | |
470 | * If dump is active then we have already calculated the size during | |
471 | * first kernel. | |
472 | */ | |
473 | if (fdm_active) | |
408cddd9 | 474 | fw_dump.boot_memory_size = be64_to_cpu(fdm_active->rmr_region.source_len); |
a4e92ce8 | 475 | else { |
3ccc00a7 | 476 | fw_dump.boot_memory_size = fadump_calculate_reserve_size(); |
a4e92ce8 MS |
477 | #ifdef CONFIG_CMA |
478 | if (!fw_dump.nocma) | |
479 | fw_dump.boot_memory_size = | |
480 | ALIGN(fw_dump.boot_memory_size, | |
481 | FADUMP_CMA_ALIGNMENT); | |
482 | #endif | |
483 | } | |
eb39c880 MS |
484 | |
485 | /* | |
486 | * Calculate the memory boundary. | |
487 | * If memory_limit is less than actual memory boundary then reserve | |
488 | * the memory for fadump beyond the memory_limit and adjust the | |
489 | * memory_limit accordingly, so that the running kernel can run with | |
490 | * specified memory_limit. | |
491 | */ | |
492 | if (memory_limit && memory_limit < memblock_end_of_DRAM()) { | |
493 | size = get_fadump_area_size(); | |
494 | if ((memory_limit + size) < memblock_end_of_DRAM()) | |
495 | memory_limit += size; | |
496 | else | |
497 | memory_limit = memblock_end_of_DRAM(); | |
498 | printk(KERN_INFO "Adjusted memory_limit for firmware-assisted" | |
a84fcd46 | 499 | " dump, now %#016llx\n", memory_limit); |
eb39c880 MS |
500 | } |
501 | if (memory_limit) | |
502 | memory_boundary = memory_limit; | |
503 | else | |
504 | memory_boundary = memblock_end_of_DRAM(); | |
505 | ||
506 | if (fw_dump.dump_active) { | |
b71a693d MS |
507 | pr_info("Firmware-assisted dump is active.\n"); |
508 | ||
85975387 HB |
509 | #ifdef CONFIG_HUGETLB_PAGE |
510 | /* | |
511 | * FADump capture kernel doesn't care much about hugepages. | |
512 | * In fact, handling hugepages in capture kernel is asking for | |
513 | * trouble. So, disable HugeTLB support when fadump is active. | |
514 | */ | |
515 | hugetlb_disabled = true; | |
516 | #endif | |
eb39c880 MS |
517 | /* |
518 | * If last boot has crashed then reserve all the memory | |
519 | * above boot_memory_size so that we don't touch it until | |
520 | * dump is written to disk by userspace tool. This memory | |
521 | * will be released for general use once the dump is saved. | |
522 | */ | |
523 | base = fw_dump.boot_memory_size; | |
524 | size = memory_boundary - base; | |
b71a693d | 525 | fadump_reserve_crash_area(base, size); |
2df173d9 MS |
526 | |
527 | fw_dump.fadumphdr_addr = | |
408cddd9 HB |
528 | be64_to_cpu(fdm_active->rmr_region.destination_address) + |
529 | be64_to_cpu(fdm_active->rmr_region.source_len); | |
a4e92ce8 MS |
530 | pr_debug("fadumphdr_addr = %pa\n", &fw_dump.fadumphdr_addr); |
531 | fw_dump.reserve_dump_area_start = base; | |
532 | fw_dump.reserve_dump_area_size = size; | |
eb39c880 | 533 | } else { |
eb39c880 | 534 | size = get_fadump_area_size(); |
f6e6bedb HB |
535 | |
536 | /* | |
537 | * Reserve memory at an offset closer to bottom of the RAM to | |
538 | * minimize the impact of memory hot-remove operation. We can't | |
539 | * use memblock_find_in_range() here since it doesn't allocate | |
540 | * from bottom to top. | |
541 | */ | |
542 | for (base = fw_dump.boot_memory_size; | |
543 | base <= (memory_boundary - size); | |
544 | base += size) { | |
545 | if (memblock_is_region_memory(base, size) && | |
546 | !memblock_is_region_reserved(base, size)) | |
547 | break; | |
548 | } | |
549 | if ((base > (memory_boundary - size)) || | |
550 | memblock_reserve(base, size)) { | |
551 | pr_err("Failed to reserve memory\n"); | |
552 | return 0; | |
553 | } | |
554 | ||
555 | pr_info("Reserved %ldMB of memory at %ldMB for firmware-" | |
556 | "assisted dump (System RAM: %ldMB)\n", | |
557 | (unsigned long)(size >> 20), | |
558 | (unsigned long)(base >> 20), | |
559 | (unsigned long)(memblock_phys_mem_size() >> 20)); | |
f6e6bedb | 560 | |
a4e92ce8 MS |
561 | fw_dump.reserve_dump_area_start = base; |
562 | fw_dump.reserve_dump_area_size = size; | |
563 | return fadump_cma_init(); | |
564 | } | |
eb39c880 MS |
565 | return 1; |
566 | } | |
567 | ||
1e76609c SD |
568 | unsigned long __init arch_reserved_kernel_pages(void) |
569 | { | |
570 | return memblock_reserved_size() / PAGE_SIZE; | |
571 | } | |
572 | ||
eb39c880 MS |
573 | /* Look for fadump= cmdline option. */ |
574 | static int __init early_fadump_param(char *p) | |
575 | { | |
576 | if (!p) | |
577 | return 1; | |
578 | ||
579 | if (strncmp(p, "on", 2) == 0) | |
580 | fw_dump.fadump_enabled = 1; | |
581 | else if (strncmp(p, "off", 3) == 0) | |
582 | fw_dump.fadump_enabled = 0; | |
a4e92ce8 MS |
583 | else if (strncmp(p, "nocma", 5) == 0) { |
584 | fw_dump.fadump_enabled = 1; | |
585 | fw_dump.nocma = 1; | |
586 | } | |
eb39c880 MS |
587 | |
588 | return 0; | |
589 | } | |
590 | early_param("fadump", early_fadump_param); | |
591 | ||
81d9eca5 HB |
592 | /* |
593 | * Look for fadump_reserve_mem= cmdline option | |
594 | * TODO: Remove references to 'fadump_reserve_mem=' parameter, | |
595 | * the sooner 'crashkernel=' parameter is accustomed to. | |
596 | */ | |
597 | static int __init early_fadump_reserve_mem(char *p) | |
598 | { | |
599 | if (p) | |
600 | fw_dump.reserve_bootvar = memparse(p, &p); | |
601 | return 0; | |
602 | } | |
603 | early_param("fadump_reserve_mem", early_fadump_reserve_mem); | |
604 | ||
98b8cd7f | 605 | static int register_fw_dump(struct fadump_mem_struct *fdm) |
3ccc00a7 | 606 | { |
98b8cd7f | 607 | int rc, err; |
3ccc00a7 MS |
608 | unsigned int wait_time; |
609 | ||
610 | pr_debug("Registering for firmware-assisted kernel dump...\n"); | |
611 | ||
612 | /* TODO: Add upper time limit for the delay */ | |
613 | do { | |
614 | rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL, | |
615 | FADUMP_REGISTER, fdm, | |
616 | sizeof(struct fadump_mem_struct)); | |
617 | ||
618 | wait_time = rtas_busy_delay_time(rc); | |
619 | if (wait_time) | |
620 | mdelay(wait_time); | |
621 | ||
622 | } while (wait_time); | |
623 | ||
98b8cd7f | 624 | err = -EIO; |
3ccc00a7 | 625 | switch (rc) { |
98b8cd7f MS |
626 | default: |
627 | pr_err("Failed to register. Unknown Error(%d).\n", rc); | |
628 | break; | |
3ccc00a7 MS |
629 | case -1: |
630 | printk(KERN_ERR "Failed to register firmware-assisted kernel" | |
631 | " dump. Hardware Error(%d).\n", rc); | |
632 | break; | |
633 | case -3: | |
a5a05b91 | 634 | if (!is_boot_memory_area_contiguous()) |
f86593be MS |
635 | pr_err("Can't have holes in boot memory area while registering fadump\n"); |
636 | else if (!is_reserved_memory_area_contiguous()) | |
637 | pr_err("Can't have holes in reserved memory area while" | |
638 | " registering fadump\n"); | |
a5a05b91 | 639 | |
3ccc00a7 MS |
640 | printk(KERN_ERR "Failed to register firmware-assisted kernel" |
641 | " dump. Parameter Error(%d).\n", rc); | |
98b8cd7f | 642 | err = -EINVAL; |
3ccc00a7 MS |
643 | break; |
644 | case -9: | |
645 | printk(KERN_ERR "firmware-assisted kernel dump is already " | |
646 | " registered."); | |
647 | fw_dump.dump_registered = 1; | |
98b8cd7f | 648 | err = -EEXIST; |
3ccc00a7 MS |
649 | break; |
650 | case 0: | |
651 | printk(KERN_INFO "firmware-assisted kernel dump registration" | |
652 | " is successful\n"); | |
653 | fw_dump.dump_registered = 1; | |
98b8cd7f | 654 | err = 0; |
3ccc00a7 MS |
655 | break; |
656 | } | |
98b8cd7f | 657 | return err; |
3ccc00a7 MS |
658 | } |
659 | ||
ebaeb5ae MS |
660 | void crash_fadump(struct pt_regs *regs, const char *str) |
661 | { | |
662 | struct fadump_crash_info_header *fdh = NULL; | |
f2a5e8f0 | 663 | int old_cpu, this_cpu; |
ebaeb5ae | 664 | |
6fcd6baa | 665 | if (!should_fadump_crash()) |
ebaeb5ae MS |
666 | return; |
667 | ||
f2a5e8f0 MS |
668 | /* |
669 | * old_cpu == -1 means this is the first CPU which has come here, | |
670 | * go ahead and trigger fadump. | |
671 | * | |
672 | * old_cpu != -1 means some other CPU has already on it's way | |
673 | * to trigger fadump, just keep looping here. | |
674 | */ | |
675 | this_cpu = smp_processor_id(); | |
676 | old_cpu = cmpxchg(&crashing_cpu, -1, this_cpu); | |
677 | ||
678 | if (old_cpu != -1) { | |
679 | /* | |
680 | * We can't loop here indefinitely. Wait as long as fadump | |
681 | * is in force. If we race with fadump un-registration this | |
682 | * loop will break and then we go down to normal panic path | |
683 | * and reboot. If fadump is in force the first crashing | |
684 | * cpu will definitely trigger fadump. | |
685 | */ | |
686 | while (fw_dump.dump_registered) | |
687 | cpu_relax(); | |
688 | return; | |
689 | } | |
690 | ||
ebaeb5ae | 691 | fdh = __va(fw_dump.fadumphdr_addr); |
ebaeb5ae MS |
692 | fdh->crashing_cpu = crashing_cpu; |
693 | crash_save_vmcoreinfo(); | |
694 | ||
695 | if (regs) | |
696 | fdh->regs = *regs; | |
697 | else | |
698 | ppc_save_regs(&fdh->regs); | |
699 | ||
a0512164 | 700 | fdh->online_mask = *cpu_online_mask; |
ebaeb5ae MS |
701 | |
702 | /* Call ibm,os-term rtas call to trigger firmware assisted dump */ | |
703 | rtas_os_term((char *)str); | |
704 | } | |
705 | ||
706 | #define GPR_MASK 0xffffff0000000000 | |
707 | static inline int fadump_gpr_index(u64 id) | |
708 | { | |
709 | int i = -1; | |
710 | char str[3]; | |
711 | ||
712 | if ((id & GPR_MASK) == REG_ID("GPR")) { | |
713 | /* get the digits at the end */ | |
714 | id &= ~GPR_MASK; | |
715 | id >>= 24; | |
716 | str[2] = '\0'; | |
717 | str[1] = id & 0xff; | |
718 | str[0] = (id >> 8) & 0xff; | |
719 | sscanf(str, "%d", &i); | |
720 | if (i > 31) | |
721 | i = -1; | |
722 | } | |
723 | return i; | |
724 | } | |
725 | ||
726 | static inline void fadump_set_regval(struct pt_regs *regs, u64 reg_id, | |
727 | u64 reg_val) | |
728 | { | |
729 | int i; | |
730 | ||
731 | i = fadump_gpr_index(reg_id); | |
732 | if (i >= 0) | |
733 | regs->gpr[i] = (unsigned long)reg_val; | |
734 | else if (reg_id == REG_ID("NIA")) | |
735 | regs->nip = (unsigned long)reg_val; | |
736 | else if (reg_id == REG_ID("MSR")) | |
737 | regs->msr = (unsigned long)reg_val; | |
738 | else if (reg_id == REG_ID("CTR")) | |
739 | regs->ctr = (unsigned long)reg_val; | |
740 | else if (reg_id == REG_ID("LR")) | |
741 | regs->link = (unsigned long)reg_val; | |
742 | else if (reg_id == REG_ID("XER")) | |
743 | regs->xer = (unsigned long)reg_val; | |
744 | else if (reg_id == REG_ID("CR")) | |
745 | regs->ccr = (unsigned long)reg_val; | |
746 | else if (reg_id == REG_ID("DAR")) | |
747 | regs->dar = (unsigned long)reg_val; | |
748 | else if (reg_id == REG_ID("DSISR")) | |
749 | regs->dsisr = (unsigned long)reg_val; | |
750 | } | |
751 | ||
752 | static struct fadump_reg_entry* | |
753 | fadump_read_registers(struct fadump_reg_entry *reg_entry, struct pt_regs *regs) | |
754 | { | |
755 | memset(regs, 0, sizeof(struct pt_regs)); | |
756 | ||
408cddd9 HB |
757 | while (be64_to_cpu(reg_entry->reg_id) != REG_ID("CPUEND")) { |
758 | fadump_set_regval(regs, be64_to_cpu(reg_entry->reg_id), | |
759 | be64_to_cpu(reg_entry->reg_value)); | |
ebaeb5ae MS |
760 | reg_entry++; |
761 | } | |
762 | reg_entry++; | |
763 | return reg_entry; | |
764 | } | |
765 | ||
ebaeb5ae MS |
766 | static u32 *fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs) |
767 | { | |
768 | struct elf_prstatus prstatus; | |
769 | ||
770 | memset(&prstatus, 0, sizeof(prstatus)); | |
771 | /* | |
772 | * FIXME: How do i get PID? Do I really need it? | |
773 | * prstatus.pr_pid = ???? | |
774 | */ | |
775 | elf_core_copy_kernel_regs(&prstatus.pr_reg, regs); | |
22bd0177 HB |
776 | buf = append_elf_note(buf, CRASH_CORE_NOTE_NAME, NT_PRSTATUS, |
777 | &prstatus, sizeof(prstatus)); | |
ebaeb5ae MS |
778 | return buf; |
779 | } | |
780 | ||
781 | static void fadump_update_elfcore_header(char *bufp) | |
782 | { | |
783 | struct elfhdr *elf; | |
784 | struct elf_phdr *phdr; | |
785 | ||
786 | elf = (struct elfhdr *)bufp; | |
787 | bufp += sizeof(struct elfhdr); | |
788 | ||
789 | /* First note is a place holder for cpu notes info. */ | |
790 | phdr = (struct elf_phdr *)bufp; | |
791 | ||
792 | if (phdr->p_type == PT_NOTE) { | |
793 | phdr->p_paddr = fw_dump.cpu_notes_buf; | |
794 | phdr->p_offset = phdr->p_paddr; | |
795 | phdr->p_filesz = fw_dump.cpu_notes_buf_size; | |
796 | phdr->p_memsz = fw_dump.cpu_notes_buf_size; | |
797 | } | |
798 | return; | |
799 | } | |
800 | ||
801 | static void *fadump_cpu_notes_buf_alloc(unsigned long size) | |
802 | { | |
803 | void *vaddr; | |
804 | struct page *page; | |
805 | unsigned long order, count, i; | |
806 | ||
807 | order = get_order(size); | |
808 | vaddr = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, order); | |
809 | if (!vaddr) | |
810 | return NULL; | |
811 | ||
812 | count = 1 << order; | |
813 | page = virt_to_page(vaddr); | |
814 | for (i = 0; i < count; i++) | |
815 | SetPageReserved(page + i); | |
816 | return vaddr; | |
817 | } | |
818 | ||
819 | static void fadump_cpu_notes_buf_free(unsigned long vaddr, unsigned long size) | |
820 | { | |
821 | struct page *page; | |
822 | unsigned long order, count, i; | |
823 | ||
824 | order = get_order(size); | |
825 | count = 1 << order; | |
826 | page = virt_to_page(vaddr); | |
827 | for (i = 0; i < count; i++) | |
828 | ClearPageReserved(page + i); | |
829 | __free_pages(page, order); | |
830 | } | |
831 | ||
832 | /* | |
833 | * Read CPU state dump data and convert it into ELF notes. | |
834 | * The CPU dump starts with magic number "REGSAVE". NumCpusOffset should be | |
835 | * used to access the data to allow for additional fields to be added without | |
836 | * affecting compatibility. Each list of registers for a CPU starts with | |
837 | * "CPUSTRT" and ends with "CPUEND". Each register entry is of 16 bytes, | |
838 | * 8 Byte ASCII identifier and 8 Byte register value. The register entry | |
839 | * with identifier "CPUSTRT" and "CPUEND" contains 4 byte cpu id as part | |
840 | * of register value. For more details refer to PAPR document. | |
841 | * | |
842 | * Only for the crashing cpu we ignore the CPU dump data and get exact | |
843 | * state from fadump crash info structure populated by first kernel at the | |
844 | * time of crash. | |
845 | */ | |
846 | static int __init fadump_build_cpu_notes(const struct fadump_mem_struct *fdm) | |
847 | { | |
848 | struct fadump_reg_save_area_header *reg_header; | |
849 | struct fadump_reg_entry *reg_entry; | |
850 | struct fadump_crash_info_header *fdh = NULL; | |
851 | void *vaddr; | |
852 | unsigned long addr; | |
853 | u32 num_cpus, *note_buf; | |
854 | struct pt_regs regs; | |
855 | int i, rc = 0, cpu = 0; | |
856 | ||
857 | if (!fdm->cpu_state_data.bytes_dumped) | |
858 | return -EINVAL; | |
859 | ||
408cddd9 | 860 | addr = be64_to_cpu(fdm->cpu_state_data.destination_address); |
ebaeb5ae MS |
861 | vaddr = __va(addr); |
862 | ||
863 | reg_header = vaddr; | |
408cddd9 | 864 | if (be64_to_cpu(reg_header->magic_number) != REGSAVE_AREA_MAGIC) { |
ebaeb5ae MS |
865 | printk(KERN_ERR "Unable to read register save area.\n"); |
866 | return -ENOENT; | |
867 | } | |
868 | pr_debug("--------CPU State Data------------\n"); | |
408cddd9 HB |
869 | pr_debug("Magic Number: %llx\n", be64_to_cpu(reg_header->magic_number)); |
870 | pr_debug("NumCpuOffset: %x\n", be32_to_cpu(reg_header->num_cpu_offset)); | |
ebaeb5ae | 871 | |
408cddd9 HB |
872 | vaddr += be32_to_cpu(reg_header->num_cpu_offset); |
873 | num_cpus = be32_to_cpu(*((__be32 *)(vaddr))); | |
ebaeb5ae MS |
874 | pr_debug("NumCpus : %u\n", num_cpus); |
875 | vaddr += sizeof(u32); | |
876 | reg_entry = (struct fadump_reg_entry *)vaddr; | |
877 | ||
878 | /* Allocate buffer to hold cpu crash notes. */ | |
879 | fw_dump.cpu_notes_buf_size = num_cpus * sizeof(note_buf_t); | |
880 | fw_dump.cpu_notes_buf_size = PAGE_ALIGN(fw_dump.cpu_notes_buf_size); | |
881 | note_buf = fadump_cpu_notes_buf_alloc(fw_dump.cpu_notes_buf_size); | |
882 | if (!note_buf) { | |
883 | printk(KERN_ERR "Failed to allocate 0x%lx bytes for " | |
884 | "cpu notes buffer\n", fw_dump.cpu_notes_buf_size); | |
885 | return -ENOMEM; | |
886 | } | |
887 | fw_dump.cpu_notes_buf = __pa(note_buf); | |
888 | ||
889 | pr_debug("Allocated buffer for cpu notes of size %ld at %p\n", | |
890 | (num_cpus * sizeof(note_buf_t)), note_buf); | |
891 | ||
892 | if (fw_dump.fadumphdr_addr) | |
893 | fdh = __va(fw_dump.fadumphdr_addr); | |
894 | ||
895 | for (i = 0; i < num_cpus; i++) { | |
408cddd9 | 896 | if (be64_to_cpu(reg_entry->reg_id) != REG_ID("CPUSTRT")) { |
ebaeb5ae MS |
897 | printk(KERN_ERR "Unable to read CPU state data\n"); |
898 | rc = -ENOENT; | |
899 | goto error_out; | |
900 | } | |
901 | /* Lower 4 bytes of reg_value contains logical cpu id */ | |
408cddd9 | 902 | cpu = be64_to_cpu(reg_entry->reg_value) & FADUMP_CPU_ID_MASK; |
a0512164 | 903 | if (fdh && !cpumask_test_cpu(cpu, &fdh->online_mask)) { |
ebaeb5ae MS |
904 | SKIP_TO_NEXT_CPU(reg_entry); |
905 | continue; | |
906 | } | |
907 | pr_debug("Reading register data for cpu %d...\n", cpu); | |
908 | if (fdh && fdh->crashing_cpu == cpu) { | |
909 | regs = fdh->regs; | |
910 | note_buf = fadump_regs_to_elf_notes(note_buf, ®s); | |
911 | SKIP_TO_NEXT_CPU(reg_entry); | |
912 | } else { | |
913 | reg_entry++; | |
914 | reg_entry = fadump_read_registers(reg_entry, ®s); | |
915 | note_buf = fadump_regs_to_elf_notes(note_buf, ®s); | |
916 | } | |
917 | } | |
22bd0177 | 918 | final_note(note_buf); |
ebaeb5ae | 919 | |
b717d985 RS |
920 | if (fdh) { |
921 | pr_debug("Updating elfcore header (%llx) with cpu notes\n", | |
ebaeb5ae | 922 | fdh->elfcorehdr_addr); |
b717d985 RS |
923 | fadump_update_elfcore_header((char *)__va(fdh->elfcorehdr_addr)); |
924 | } | |
ebaeb5ae MS |
925 | return 0; |
926 | ||
927 | error_out: | |
928 | fadump_cpu_notes_buf_free((unsigned long)__va(fw_dump.cpu_notes_buf), | |
929 | fw_dump.cpu_notes_buf_size); | |
930 | fw_dump.cpu_notes_buf = 0; | |
931 | fw_dump.cpu_notes_buf_size = 0; | |
932 | return rc; | |
933 | ||
934 | } | |
935 | ||
2df173d9 MS |
936 | /* |
937 | * Validate and process the dump data stored by firmware before exporting | |
938 | * it through '/proc/vmcore'. | |
939 | */ | |
940 | static int __init process_fadump(const struct fadump_mem_struct *fdm_active) | |
941 | { | |
942 | struct fadump_crash_info_header *fdh; | |
ebaeb5ae | 943 | int rc = 0; |
2df173d9 MS |
944 | |
945 | if (!fdm_active || !fw_dump.fadumphdr_addr) | |
946 | return -EINVAL; | |
947 | ||
948 | /* Check if the dump data is valid. */ | |
408cddd9 | 949 | if ((be16_to_cpu(fdm_active->header.dump_status_flag) == FADUMP_ERROR_FLAG) || |
ebaeb5ae | 950 | (fdm_active->cpu_state_data.error_flags != 0) || |
2df173d9 MS |
951 | (fdm_active->rmr_region.error_flags != 0)) { |
952 | printk(KERN_ERR "Dump taken by platform is not valid\n"); | |
953 | return -EINVAL; | |
954 | } | |
ebaeb5ae MS |
955 | if ((fdm_active->rmr_region.bytes_dumped != |
956 | fdm_active->rmr_region.source_len) || | |
957 | !fdm_active->cpu_state_data.bytes_dumped) { | |
2df173d9 MS |
958 | printk(KERN_ERR "Dump taken by platform is incomplete\n"); |
959 | return -EINVAL; | |
960 | } | |
961 | ||
962 | /* Validate the fadump crash info header */ | |
963 | fdh = __va(fw_dump.fadumphdr_addr); | |
964 | if (fdh->magic_number != FADUMP_CRASH_INFO_MAGIC) { | |
965 | printk(KERN_ERR "Crash info header is not valid.\n"); | |
966 | return -EINVAL; | |
967 | } | |
968 | ||
ebaeb5ae MS |
969 | rc = fadump_build_cpu_notes(fdm_active); |
970 | if (rc) | |
971 | return rc; | |
972 | ||
2df173d9 MS |
973 | /* |
974 | * We are done validating dump info and elfcore header is now ready | |
975 | * to be exported. set elfcorehdr_addr so that vmcore module will | |
976 | * export the elfcore header through '/proc/vmcore'. | |
977 | */ | |
978 | elfcorehdr_addr = fdh->elfcorehdr_addr; | |
979 | ||
980 | return 0; | |
981 | } | |
982 | ||
1bd6a1c4 HB |
983 | static void free_crash_memory_ranges(void) |
984 | { | |
985 | kfree(crash_memory_ranges); | |
986 | crash_memory_ranges = NULL; | |
987 | crash_memory_ranges_size = 0; | |
988 | max_crash_mem_ranges = 0; | |
989 | } | |
990 | ||
991 | /* | |
992 | * Allocate or reallocate crash memory ranges array in incremental units | |
993 | * of PAGE_SIZE. | |
994 | */ | |
995 | static int allocate_crash_memory_ranges(void) | |
996 | { | |
997 | struct fad_crash_memory_ranges *new_array; | |
998 | u64 new_size; | |
999 | ||
1000 | new_size = crash_memory_ranges_size + PAGE_SIZE; | |
1001 | pr_debug("Allocating %llu bytes of memory for crash memory ranges\n", | |
1002 | new_size); | |
1003 | ||
1004 | new_array = krealloc(crash_memory_ranges, new_size, GFP_KERNEL); | |
1005 | if (new_array == NULL) { | |
1006 | pr_err("Insufficient memory for setting up crash memory ranges\n"); | |
1007 | free_crash_memory_ranges(); | |
1008 | return -ENOMEM; | |
1009 | } | |
1010 | ||
1011 | crash_memory_ranges = new_array; | |
1012 | crash_memory_ranges_size = new_size; | |
1013 | max_crash_mem_ranges = (new_size / | |
1014 | sizeof(struct fad_crash_memory_ranges)); | |
1015 | return 0; | |
1016 | } | |
1017 | ||
1018 | static inline int fadump_add_crash_memory(unsigned long long base, | |
1019 | unsigned long long end) | |
2df173d9 | 1020 | { |
ced1bf52 HB |
1021 | u64 start, size; |
1022 | bool is_adjacent = false; | |
1023 | ||
2df173d9 | 1024 | if (base == end) |
1bd6a1c4 HB |
1025 | return 0; |
1026 | ||
ced1bf52 HB |
1027 | /* |
1028 | * Fold adjacent memory ranges to bring down the memory ranges/ | |
1029 | * PT_LOAD segments count. | |
1030 | */ | |
1031 | if (crash_mem_ranges) { | |
1032 | start = crash_memory_ranges[crash_mem_ranges - 1].base; | |
1033 | size = crash_memory_ranges[crash_mem_ranges - 1].size; | |
1bd6a1c4 | 1034 | |
ced1bf52 HB |
1035 | if ((start + size) == base) |
1036 | is_adjacent = true; | |
1037 | } | |
1038 | if (!is_adjacent) { | |
1039 | /* resize the array on reaching the limit */ | |
1040 | if (crash_mem_ranges == max_crash_mem_ranges) { | |
1041 | int ret; | |
1042 | ||
1043 | ret = allocate_crash_memory_ranges(); | |
1044 | if (ret) | |
1045 | return ret; | |
1046 | } | |
1047 | ||
1048 | start = base; | |
1049 | crash_memory_ranges[crash_mem_ranges].base = start; | |
1050 | crash_mem_ranges++; | |
1bd6a1c4 | 1051 | } |
2df173d9 | 1052 | |
ced1bf52 | 1053 | crash_memory_ranges[crash_mem_ranges - 1].size = (end - start); |
2df173d9 | 1054 | pr_debug("crash_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n", |
ced1bf52 | 1055 | (crash_mem_ranges - 1), start, end - 1, (end - start)); |
1bd6a1c4 | 1056 | return 0; |
2df173d9 MS |
1057 | } |
1058 | ||
1bd6a1c4 | 1059 | static int fadump_exclude_reserved_area(unsigned long long start, |
2df173d9 MS |
1060 | unsigned long long end) |
1061 | { | |
1062 | unsigned long long ra_start, ra_end; | |
1bd6a1c4 | 1063 | int ret = 0; |
2df173d9 MS |
1064 | |
1065 | ra_start = fw_dump.reserve_dump_area_start; | |
1066 | ra_end = ra_start + fw_dump.reserve_dump_area_size; | |
1067 | ||
1068 | if ((ra_start < end) && (ra_end > start)) { | |
1069 | if ((start < ra_start) && (end > ra_end)) { | |
1bd6a1c4 HB |
1070 | ret = fadump_add_crash_memory(start, ra_start); |
1071 | if (ret) | |
1072 | return ret; | |
1073 | ||
1074 | ret = fadump_add_crash_memory(ra_end, end); | |
2df173d9 | 1075 | } else if (start < ra_start) { |
1bd6a1c4 | 1076 | ret = fadump_add_crash_memory(start, ra_start); |
2df173d9 | 1077 | } else if (ra_end < end) { |
1bd6a1c4 | 1078 | ret = fadump_add_crash_memory(ra_end, end); |
2df173d9 MS |
1079 | } |
1080 | } else | |
1bd6a1c4 HB |
1081 | ret = fadump_add_crash_memory(start, end); |
1082 | ||
1083 | return ret; | |
2df173d9 MS |
1084 | } |
1085 | ||
1086 | static int fadump_init_elfcore_header(char *bufp) | |
1087 | { | |
1088 | struct elfhdr *elf; | |
1089 | ||
1090 | elf = (struct elfhdr *) bufp; | |
1091 | bufp += sizeof(struct elfhdr); | |
1092 | memcpy(elf->e_ident, ELFMAG, SELFMAG); | |
1093 | elf->e_ident[EI_CLASS] = ELF_CLASS; | |
1094 | elf->e_ident[EI_DATA] = ELF_DATA; | |
1095 | elf->e_ident[EI_VERSION] = EV_CURRENT; | |
1096 | elf->e_ident[EI_OSABI] = ELF_OSABI; | |
1097 | memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD); | |
1098 | elf->e_type = ET_CORE; | |
1099 | elf->e_machine = ELF_ARCH; | |
1100 | elf->e_version = EV_CURRENT; | |
1101 | elf->e_entry = 0; | |
1102 | elf->e_phoff = sizeof(struct elfhdr); | |
1103 | elf->e_shoff = 0; | |
d8bced27 DA |
1104 | #if defined(_CALL_ELF) |
1105 | elf->e_flags = _CALL_ELF; | |
1106 | #else | |
1107 | elf->e_flags = 0; | |
1108 | #endif | |
2df173d9 MS |
1109 | elf->e_ehsize = sizeof(struct elfhdr); |
1110 | elf->e_phentsize = sizeof(struct elf_phdr); | |
1111 | elf->e_phnum = 0; | |
1112 | elf->e_shentsize = 0; | |
1113 | elf->e_shnum = 0; | |
1114 | elf->e_shstrndx = 0; | |
1115 | ||
1116 | return 0; | |
1117 | } | |
1118 | ||
1119 | /* | |
1120 | * Traverse through memblock structure and setup crash memory ranges. These | |
1121 | * ranges will be used create PT_LOAD program headers in elfcore header. | |
1122 | */ | |
1bd6a1c4 | 1123 | static int fadump_setup_crash_memory_ranges(void) |
2df173d9 MS |
1124 | { |
1125 | struct memblock_region *reg; | |
1126 | unsigned long long start, end; | |
1bd6a1c4 | 1127 | int ret; |
2df173d9 MS |
1128 | |
1129 | pr_debug("Setup crash memory ranges.\n"); | |
1130 | crash_mem_ranges = 0; | |
ced1bf52 | 1131 | |
2df173d9 MS |
1132 | /* |
1133 | * add the first memory chunk (RMA_START through boot_memory_size) as | |
1134 | * a separate memory chunk. The reason is, at the time crash firmware | |
1135 | * will move the content of this memory chunk to different location | |
1136 | * specified during fadump registration. We need to create a separate | |
1137 | * program header for this chunk with the correct offset. | |
1138 | */ | |
1bd6a1c4 HB |
1139 | ret = fadump_add_crash_memory(RMA_START, fw_dump.boot_memory_size); |
1140 | if (ret) | |
1141 | return ret; | |
2df173d9 MS |
1142 | |
1143 | for_each_memblock(memory, reg) { | |
1144 | start = (unsigned long long)reg->base; | |
1145 | end = start + (unsigned long long)reg->size; | |
a77af552 HB |
1146 | |
1147 | /* | |
1148 | * skip the first memory chunk that is already added (RMA_START | |
1149 | * through boot_memory_size). This logic needs a relook if and | |
1150 | * when RMA_START changes to a non-zero value. | |
1151 | */ | |
1152 | BUILD_BUG_ON(RMA_START != 0); | |
1153 | if (start < fw_dump.boot_memory_size) { | |
1154 | if (end > fw_dump.boot_memory_size) | |
1155 | start = fw_dump.boot_memory_size; | |
1156 | else | |
1157 | continue; | |
1158 | } | |
2df173d9 MS |
1159 | |
1160 | /* add this range excluding the reserved dump area. */ | |
1bd6a1c4 HB |
1161 | ret = fadump_exclude_reserved_area(start, end); |
1162 | if (ret) | |
1163 | return ret; | |
2df173d9 | 1164 | } |
1bd6a1c4 HB |
1165 | |
1166 | return 0; | |
2df173d9 MS |
1167 | } |
1168 | ||
d34c5f26 MS |
1169 | /* |
1170 | * If the given physical address falls within the boot memory region then | |
1171 | * return the relocated address that points to the dump region reserved | |
1172 | * for saving initial boot memory contents. | |
1173 | */ | |
1174 | static inline unsigned long fadump_relocate(unsigned long paddr) | |
1175 | { | |
1176 | if (paddr > RMA_START && paddr < fw_dump.boot_memory_size) | |
408cddd9 | 1177 | return be64_to_cpu(fdm.rmr_region.destination_address) + paddr; |
d34c5f26 MS |
1178 | else |
1179 | return paddr; | |
1180 | } | |
1181 | ||
2df173d9 MS |
1182 | static int fadump_create_elfcore_headers(char *bufp) |
1183 | { | |
1184 | struct elfhdr *elf; | |
1185 | struct elf_phdr *phdr; | |
1186 | int i; | |
1187 | ||
1188 | fadump_init_elfcore_header(bufp); | |
1189 | elf = (struct elfhdr *)bufp; | |
1190 | bufp += sizeof(struct elfhdr); | |
1191 | ||
ebaeb5ae MS |
1192 | /* |
1193 | * setup ELF PT_NOTE, place holder for cpu notes info. The notes info | |
1194 | * will be populated during second kernel boot after crash. Hence | |
1195 | * this PT_NOTE will always be the first elf note. | |
1196 | * | |
1197 | * NOTE: Any new ELF note addition should be placed after this note. | |
1198 | */ | |
1199 | phdr = (struct elf_phdr *)bufp; | |
1200 | bufp += sizeof(struct elf_phdr); | |
1201 | phdr->p_type = PT_NOTE; | |
1202 | phdr->p_flags = 0; | |
1203 | phdr->p_vaddr = 0; | |
1204 | phdr->p_align = 0; | |
1205 | ||
1206 | phdr->p_offset = 0; | |
1207 | phdr->p_paddr = 0; | |
1208 | phdr->p_filesz = 0; | |
1209 | phdr->p_memsz = 0; | |
1210 | ||
1211 | (elf->e_phnum)++; | |
1212 | ||
d34c5f26 MS |
1213 | /* setup ELF PT_NOTE for vmcoreinfo */ |
1214 | phdr = (struct elf_phdr *)bufp; | |
1215 | bufp += sizeof(struct elf_phdr); | |
1216 | phdr->p_type = PT_NOTE; | |
1217 | phdr->p_flags = 0; | |
1218 | phdr->p_vaddr = 0; | |
1219 | phdr->p_align = 0; | |
1220 | ||
1221 | phdr->p_paddr = fadump_relocate(paddr_vmcoreinfo_note()); | |
1222 | phdr->p_offset = phdr->p_paddr; | |
5203f499 | 1223 | phdr->p_memsz = phdr->p_filesz = VMCOREINFO_NOTE_SIZE; |
d34c5f26 MS |
1224 | |
1225 | /* Increment number of program headers. */ | |
1226 | (elf->e_phnum)++; | |
1227 | ||
2df173d9 MS |
1228 | /* setup PT_LOAD sections. */ |
1229 | ||
1230 | for (i = 0; i < crash_mem_ranges; i++) { | |
1231 | unsigned long long mbase, msize; | |
1232 | mbase = crash_memory_ranges[i].base; | |
1233 | msize = crash_memory_ranges[i].size; | |
1234 | ||
1235 | if (!msize) | |
1236 | continue; | |
1237 | ||
1238 | phdr = (struct elf_phdr *)bufp; | |
1239 | bufp += sizeof(struct elf_phdr); | |
1240 | phdr->p_type = PT_LOAD; | |
1241 | phdr->p_flags = PF_R|PF_W|PF_X; | |
1242 | phdr->p_offset = mbase; | |
1243 | ||
1244 | if (mbase == RMA_START) { | |
1245 | /* | |
1246 | * The entire RMA region will be moved by firmware | |
1247 | * to the specified destination_address. Hence set | |
1248 | * the correct offset. | |
1249 | */ | |
408cddd9 | 1250 | phdr->p_offset = be64_to_cpu(fdm.rmr_region.destination_address); |
2df173d9 MS |
1251 | } |
1252 | ||
1253 | phdr->p_paddr = mbase; | |
1254 | phdr->p_vaddr = (unsigned long)__va(mbase); | |
1255 | phdr->p_filesz = msize; | |
1256 | phdr->p_memsz = msize; | |
1257 | phdr->p_align = 0; | |
1258 | ||
1259 | /* Increment number of program headers. */ | |
1260 | (elf->e_phnum)++; | |
1261 | } | |
1262 | return 0; | |
1263 | } | |
1264 | ||
1265 | static unsigned long init_fadump_header(unsigned long addr) | |
1266 | { | |
1267 | struct fadump_crash_info_header *fdh; | |
1268 | ||
1269 | if (!addr) | |
1270 | return 0; | |
1271 | ||
1272 | fw_dump.fadumphdr_addr = addr; | |
1273 | fdh = __va(addr); | |
1274 | addr += sizeof(struct fadump_crash_info_header); | |
1275 | ||
1276 | memset(fdh, 0, sizeof(struct fadump_crash_info_header)); | |
1277 | fdh->magic_number = FADUMP_CRASH_INFO_MAGIC; | |
1278 | fdh->elfcorehdr_addr = addr; | |
ebaeb5ae MS |
1279 | /* We will set the crashing cpu id in crash_fadump() during crash. */ |
1280 | fdh->crashing_cpu = CPU_UNKNOWN; | |
2df173d9 MS |
1281 | |
1282 | return addr; | |
1283 | } | |
1284 | ||
98b8cd7f | 1285 | static int register_fadump(void) |
3ccc00a7 | 1286 | { |
2df173d9 MS |
1287 | unsigned long addr; |
1288 | void *vaddr; | |
1bd6a1c4 | 1289 | int ret; |
2df173d9 | 1290 | |
3ccc00a7 MS |
1291 | /* |
1292 | * If no memory is reserved then we can not register for firmware- | |
1293 | * assisted dump. | |
1294 | */ | |
1295 | if (!fw_dump.reserve_dump_area_size) | |
98b8cd7f | 1296 | return -ENODEV; |
3ccc00a7 | 1297 | |
1bd6a1c4 HB |
1298 | ret = fadump_setup_crash_memory_ranges(); |
1299 | if (ret) | |
1300 | return ret; | |
2df173d9 | 1301 | |
408cddd9 | 1302 | addr = be64_to_cpu(fdm.rmr_region.destination_address) + be64_to_cpu(fdm.rmr_region.source_len); |
2df173d9 MS |
1303 | /* Initialize fadump crash info header. */ |
1304 | addr = init_fadump_header(addr); | |
1305 | vaddr = __va(addr); | |
1306 | ||
1307 | pr_debug("Creating ELF core headers at %#016lx\n", addr); | |
1308 | fadump_create_elfcore_headers(vaddr); | |
1309 | ||
3ccc00a7 | 1310 | /* register the future kernel dump with firmware. */ |
98b8cd7f | 1311 | return register_fw_dump(&fdm); |
3ccc00a7 MS |
1312 | } |
1313 | ||
1314 | static int fadump_unregister_dump(struct fadump_mem_struct *fdm) | |
1315 | { | |
1316 | int rc = 0; | |
1317 | unsigned int wait_time; | |
1318 | ||
1319 | pr_debug("Un-register firmware-assisted dump\n"); | |
1320 | ||
1321 | /* TODO: Add upper time limit for the delay */ | |
1322 | do { | |
1323 | rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL, | |
1324 | FADUMP_UNREGISTER, fdm, | |
1325 | sizeof(struct fadump_mem_struct)); | |
1326 | ||
1327 | wait_time = rtas_busy_delay_time(rc); | |
1328 | if (wait_time) | |
1329 | mdelay(wait_time); | |
1330 | } while (wait_time); | |
1331 | ||
1332 | if (rc) { | |
1333 | printk(KERN_ERR "Failed to un-register firmware-assisted dump." | |
1334 | " unexpected error(%d).\n", rc); | |
1335 | return rc; | |
1336 | } | |
1337 | fw_dump.dump_registered = 0; | |
1338 | return 0; | |
1339 | } | |
1340 | ||
a4e92ce8 | 1341 | static int fadump_invalidate_dump(const struct fadump_mem_struct *fdm) |
b500afff MS |
1342 | { |
1343 | int rc = 0; | |
1344 | unsigned int wait_time; | |
1345 | ||
1346 | pr_debug("Invalidating firmware-assisted dump registration\n"); | |
1347 | ||
1348 | /* TODO: Add upper time limit for the delay */ | |
1349 | do { | |
1350 | rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL, | |
1351 | FADUMP_INVALIDATE, fdm, | |
1352 | sizeof(struct fadump_mem_struct)); | |
1353 | ||
1354 | wait_time = rtas_busy_delay_time(rc); | |
1355 | if (wait_time) | |
1356 | mdelay(wait_time); | |
1357 | } while (wait_time); | |
1358 | ||
1359 | if (rc) { | |
4a03749f | 1360 | pr_err("Failed to invalidate firmware-assisted dump registration. Unexpected error (%d).\n", rc); |
b5b1cfc5 | 1361 | return rc; |
b500afff MS |
1362 | } |
1363 | fw_dump.dump_active = 0; | |
1364 | fdm_active = NULL; | |
1365 | return 0; | |
1366 | } | |
1367 | ||
1368 | void fadump_cleanup(void) | |
1369 | { | |
1370 | /* Invalidate the registration only if dump is active. */ | |
1371 | if (fw_dump.dump_active) { | |
a4e92ce8 MS |
1372 | /* pass the same memory dump structure provided by platform */ |
1373 | fadump_invalidate_dump(fdm_active); | |
722cde76 MS |
1374 | } else if (fw_dump.dump_registered) { |
1375 | /* Un-register Firmware-assisted dump if it was registered. */ | |
1376 | fadump_unregister_dump(&fdm); | |
1bd6a1c4 | 1377 | free_crash_memory_ranges(); |
b500afff MS |
1378 | } |
1379 | } | |
1380 | ||
68fa6478 HB |
1381 | static void fadump_free_reserved_memory(unsigned long start_pfn, |
1382 | unsigned long end_pfn) | |
1383 | { | |
1384 | unsigned long pfn; | |
1385 | unsigned long time_limit = jiffies + HZ; | |
1386 | ||
1387 | pr_info("freeing reserved memory (0x%llx - 0x%llx)\n", | |
1388 | PFN_PHYS(start_pfn), PFN_PHYS(end_pfn)); | |
1389 | ||
1390 | for (pfn = start_pfn; pfn < end_pfn; pfn++) { | |
1391 | free_reserved_page(pfn_to_page(pfn)); | |
1392 | ||
1393 | if (time_after(jiffies, time_limit)) { | |
1394 | cond_resched(); | |
1395 | time_limit = jiffies + HZ; | |
1396 | } | |
1397 | } | |
1398 | } | |
1399 | ||
1400 | /* | |
1401 | * Skip memory holes and free memory that was actually reserved. | |
1402 | */ | |
1403 | static void fadump_release_reserved_area(unsigned long start, unsigned long end) | |
1404 | { | |
1405 | struct memblock_region *reg; | |
1406 | unsigned long tstart, tend; | |
1407 | unsigned long start_pfn = PHYS_PFN(start); | |
1408 | unsigned long end_pfn = PHYS_PFN(end); | |
1409 | ||
1410 | for_each_memblock(memory, reg) { | |
1411 | tstart = max(start_pfn, memblock_region_memory_base_pfn(reg)); | |
1412 | tend = min(end_pfn, memblock_region_memory_end_pfn(reg)); | |
1413 | if (tstart < tend) { | |
1414 | fadump_free_reserved_memory(tstart, tend); | |
1415 | ||
1416 | if (tend == end_pfn) | |
1417 | break; | |
1418 | ||
1419 | start_pfn = tend + 1; | |
1420 | } | |
1421 | } | |
1422 | } | |
1423 | ||
b500afff MS |
1424 | /* |
1425 | * Release the memory that was reserved in early boot to preserve the memory | |
1426 | * contents. The released memory will be available for general use. | |
1427 | */ | |
1428 | static void fadump_release_memory(unsigned long begin, unsigned long end) | |
1429 | { | |
b500afff MS |
1430 | unsigned long ra_start, ra_end; |
1431 | ||
1432 | ra_start = fw_dump.reserve_dump_area_start; | |
1433 | ra_end = ra_start + fw_dump.reserve_dump_area_size; | |
1434 | ||
68fa6478 HB |
1435 | /* |
1436 | * exclude the dump reserve area. Will reuse it for next | |
1437 | * fadump registration. | |
1438 | */ | |
1439 | if (begin < ra_end && end > ra_start) { | |
1440 | if (begin < ra_start) | |
1441 | fadump_release_reserved_area(begin, ra_start); | |
1442 | if (end > ra_end) | |
1443 | fadump_release_reserved_area(ra_end, end); | |
1444 | } else | |
1445 | fadump_release_reserved_area(begin, end); | |
b500afff MS |
1446 | } |
1447 | ||
1448 | static void fadump_invalidate_release_mem(void) | |
1449 | { | |
1450 | unsigned long reserved_area_start, reserved_area_end; | |
1451 | unsigned long destination_address; | |
1452 | ||
1453 | mutex_lock(&fadump_mutex); | |
1454 | if (!fw_dump.dump_active) { | |
1455 | mutex_unlock(&fadump_mutex); | |
1456 | return; | |
1457 | } | |
1458 | ||
408cddd9 | 1459 | destination_address = be64_to_cpu(fdm_active->cpu_state_data.destination_address); |
b500afff MS |
1460 | fadump_cleanup(); |
1461 | mutex_unlock(&fadump_mutex); | |
1462 | ||
1463 | /* | |
1464 | * Save the current reserved memory bounds we will require them | |
1465 | * later for releasing the memory for general use. | |
1466 | */ | |
1467 | reserved_area_start = fw_dump.reserve_dump_area_start; | |
1468 | reserved_area_end = reserved_area_start + | |
1469 | fw_dump.reserve_dump_area_size; | |
1470 | /* | |
1471 | * Setup reserve_dump_area_start and its size so that we can | |
1472 | * reuse this reserved memory for Re-registration. | |
1473 | */ | |
1474 | fw_dump.reserve_dump_area_start = destination_address; | |
1475 | fw_dump.reserve_dump_area_size = get_fadump_area_size(); | |
1476 | ||
1477 | fadump_release_memory(reserved_area_start, reserved_area_end); | |
1478 | if (fw_dump.cpu_notes_buf) { | |
1479 | fadump_cpu_notes_buf_free( | |
1480 | (unsigned long)__va(fw_dump.cpu_notes_buf), | |
1481 | fw_dump.cpu_notes_buf_size); | |
1482 | fw_dump.cpu_notes_buf = 0; | |
1483 | fw_dump.cpu_notes_buf_size = 0; | |
1484 | } | |
1485 | /* Initialize the kernel dump memory structure for FAD registration. */ | |
1486 | init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start); | |
1487 | } | |
1488 | ||
1489 | static ssize_t fadump_release_memory_store(struct kobject *kobj, | |
1490 | struct kobj_attribute *attr, | |
1491 | const char *buf, size_t count) | |
1492 | { | |
dcdc4679 MS |
1493 | int input = -1; |
1494 | ||
b500afff MS |
1495 | if (!fw_dump.dump_active) |
1496 | return -EPERM; | |
1497 | ||
dcdc4679 MS |
1498 | if (kstrtoint(buf, 0, &input)) |
1499 | return -EINVAL; | |
1500 | ||
1501 | if (input == 1) { | |
b500afff MS |
1502 | /* |
1503 | * Take away the '/proc/vmcore'. We are releasing the dump | |
1504 | * memory, hence it will not be valid anymore. | |
1505 | */ | |
2685f826 | 1506 | #ifdef CONFIG_PROC_VMCORE |
b500afff | 1507 | vmcore_cleanup(); |
2685f826 | 1508 | #endif |
b500afff MS |
1509 | fadump_invalidate_release_mem(); |
1510 | ||
1511 | } else | |
1512 | return -EINVAL; | |
1513 | return count; | |
1514 | } | |
1515 | ||
3ccc00a7 MS |
1516 | static ssize_t fadump_enabled_show(struct kobject *kobj, |
1517 | struct kobj_attribute *attr, | |
1518 | char *buf) | |
1519 | { | |
1520 | return sprintf(buf, "%d\n", fw_dump.fadump_enabled); | |
1521 | } | |
1522 | ||
1523 | static ssize_t fadump_register_show(struct kobject *kobj, | |
1524 | struct kobj_attribute *attr, | |
1525 | char *buf) | |
1526 | { | |
1527 | return sprintf(buf, "%d\n", fw_dump.dump_registered); | |
1528 | } | |
1529 | ||
1530 | static ssize_t fadump_register_store(struct kobject *kobj, | |
1531 | struct kobj_attribute *attr, | |
1532 | const char *buf, size_t count) | |
1533 | { | |
1534 | int ret = 0; | |
dcdc4679 | 1535 | int input = -1; |
3ccc00a7 MS |
1536 | |
1537 | if (!fw_dump.fadump_enabled || fdm_active) | |
1538 | return -EPERM; | |
1539 | ||
dcdc4679 MS |
1540 | if (kstrtoint(buf, 0, &input)) |
1541 | return -EINVAL; | |
1542 | ||
3ccc00a7 MS |
1543 | mutex_lock(&fadump_mutex); |
1544 | ||
dcdc4679 MS |
1545 | switch (input) { |
1546 | case 0: | |
3ccc00a7 | 1547 | if (fw_dump.dump_registered == 0) { |
3ccc00a7 MS |
1548 | goto unlock_out; |
1549 | } | |
1550 | /* Un-register Firmware-assisted dump */ | |
1551 | fadump_unregister_dump(&fdm); | |
1552 | break; | |
dcdc4679 | 1553 | case 1: |
3ccc00a7 | 1554 | if (fw_dump.dump_registered == 1) { |
0823c68b HB |
1555 | /* Un-register Firmware-assisted dump */ |
1556 | fadump_unregister_dump(&fdm); | |
3ccc00a7 MS |
1557 | } |
1558 | /* Register Firmware-assisted dump */ | |
98b8cd7f | 1559 | ret = register_fadump(); |
3ccc00a7 MS |
1560 | break; |
1561 | default: | |
1562 | ret = -EINVAL; | |
1563 | break; | |
1564 | } | |
1565 | ||
1566 | unlock_out: | |
1567 | mutex_unlock(&fadump_mutex); | |
1568 | return ret < 0 ? ret : count; | |
1569 | } | |
1570 | ||
1571 | static int fadump_region_show(struct seq_file *m, void *private) | |
1572 | { | |
1573 | const struct fadump_mem_struct *fdm_ptr; | |
1574 | ||
1575 | if (!fw_dump.fadump_enabled) | |
1576 | return 0; | |
1577 | ||
b500afff | 1578 | mutex_lock(&fadump_mutex); |
3ccc00a7 MS |
1579 | if (fdm_active) |
1580 | fdm_ptr = fdm_active; | |
b500afff MS |
1581 | else { |
1582 | mutex_unlock(&fadump_mutex); | |
3ccc00a7 | 1583 | fdm_ptr = &fdm; |
b500afff | 1584 | } |
3ccc00a7 MS |
1585 | |
1586 | seq_printf(m, | |
1587 | "CPU : [%#016llx-%#016llx] %#llx bytes, " | |
1588 | "Dumped: %#llx\n", | |
408cddd9 HB |
1589 | be64_to_cpu(fdm_ptr->cpu_state_data.destination_address), |
1590 | be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) + | |
1591 | be64_to_cpu(fdm_ptr->cpu_state_data.source_len) - 1, | |
1592 | be64_to_cpu(fdm_ptr->cpu_state_data.source_len), | |
1593 | be64_to_cpu(fdm_ptr->cpu_state_data.bytes_dumped)); | |
3ccc00a7 MS |
1594 | seq_printf(m, |
1595 | "HPTE: [%#016llx-%#016llx] %#llx bytes, " | |
1596 | "Dumped: %#llx\n", | |
408cddd9 HB |
1597 | be64_to_cpu(fdm_ptr->hpte_region.destination_address), |
1598 | be64_to_cpu(fdm_ptr->hpte_region.destination_address) + | |
1599 | be64_to_cpu(fdm_ptr->hpte_region.source_len) - 1, | |
1600 | be64_to_cpu(fdm_ptr->hpte_region.source_len), | |
1601 | be64_to_cpu(fdm_ptr->hpte_region.bytes_dumped)); | |
3ccc00a7 MS |
1602 | seq_printf(m, |
1603 | "DUMP: [%#016llx-%#016llx] %#llx bytes, " | |
1604 | "Dumped: %#llx\n", | |
408cddd9 HB |
1605 | be64_to_cpu(fdm_ptr->rmr_region.destination_address), |
1606 | be64_to_cpu(fdm_ptr->rmr_region.destination_address) + | |
1607 | be64_to_cpu(fdm_ptr->rmr_region.source_len) - 1, | |
1608 | be64_to_cpu(fdm_ptr->rmr_region.source_len), | |
1609 | be64_to_cpu(fdm_ptr->rmr_region.bytes_dumped)); | |
3ccc00a7 MS |
1610 | |
1611 | if (!fdm_active || | |
1612 | (fw_dump.reserve_dump_area_start == | |
408cddd9 | 1613 | be64_to_cpu(fdm_ptr->cpu_state_data.destination_address))) |
b500afff | 1614 | goto out; |
3ccc00a7 MS |
1615 | |
1616 | /* Dump is active. Show reserved memory region. */ | |
1617 | seq_printf(m, | |
1618 | " : [%#016llx-%#016llx] %#llx bytes, " | |
1619 | "Dumped: %#llx\n", | |
1620 | (unsigned long long)fw_dump.reserve_dump_area_start, | |
408cddd9 HB |
1621 | be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) - 1, |
1622 | be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) - | |
3ccc00a7 | 1623 | fw_dump.reserve_dump_area_start, |
408cddd9 | 1624 | be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) - |
3ccc00a7 | 1625 | fw_dump.reserve_dump_area_start); |
b500afff MS |
1626 | out: |
1627 | if (fdm_active) | |
1628 | mutex_unlock(&fadump_mutex); | |
3ccc00a7 MS |
1629 | return 0; |
1630 | } | |
1631 | ||
b500afff MS |
1632 | static struct kobj_attribute fadump_release_attr = __ATTR(fadump_release_mem, |
1633 | 0200, NULL, | |
1634 | fadump_release_memory_store); | |
3ccc00a7 MS |
1635 | static struct kobj_attribute fadump_attr = __ATTR(fadump_enabled, |
1636 | 0444, fadump_enabled_show, | |
1637 | NULL); | |
1638 | static struct kobj_attribute fadump_register_attr = __ATTR(fadump_registered, | |
1639 | 0644, fadump_register_show, | |
1640 | fadump_register_store); | |
1641 | ||
f6cee260 | 1642 | DEFINE_SHOW_ATTRIBUTE(fadump_region); |
3ccc00a7 MS |
1643 | |
1644 | static void fadump_init_files(void) | |
1645 | { | |
1646 | struct dentry *debugfs_file; | |
1647 | int rc = 0; | |
1648 | ||
1649 | rc = sysfs_create_file(kernel_kobj, &fadump_attr.attr); | |
1650 | if (rc) | |
1651 | printk(KERN_ERR "fadump: unable to create sysfs file" | |
1652 | " fadump_enabled (%d)\n", rc); | |
1653 | ||
1654 | rc = sysfs_create_file(kernel_kobj, &fadump_register_attr.attr); | |
1655 | if (rc) | |
1656 | printk(KERN_ERR "fadump: unable to create sysfs file" | |
1657 | " fadump_registered (%d)\n", rc); | |
1658 | ||
1659 | debugfs_file = debugfs_create_file("fadump_region", 0444, | |
1660 | powerpc_debugfs_root, NULL, | |
1661 | &fadump_region_fops); | |
1662 | if (!debugfs_file) | |
1663 | printk(KERN_ERR "fadump: unable to create debugfs file" | |
1664 | " fadump_region\n"); | |
b500afff MS |
1665 | |
1666 | if (fw_dump.dump_active) { | |
1667 | rc = sysfs_create_file(kernel_kobj, &fadump_release_attr.attr); | |
1668 | if (rc) | |
1669 | printk(KERN_ERR "fadump: unable to create sysfs file" | |
1670 | " fadump_release_mem (%d)\n", rc); | |
1671 | } | |
3ccc00a7 MS |
1672 | return; |
1673 | } | |
1674 | ||
1675 | /* | |
1676 | * Prepare for firmware-assisted dump. | |
1677 | */ | |
1678 | int __init setup_fadump(void) | |
1679 | { | |
1680 | if (!fw_dump.fadump_enabled) | |
1681 | return 0; | |
1682 | ||
1683 | if (!fw_dump.fadump_supported) { | |
1684 | printk(KERN_ERR "Firmware-assisted dump is not supported on" | |
1685 | " this hardware\n"); | |
1686 | return 0; | |
1687 | } | |
1688 | ||
1689 | fadump_show_config(); | |
2df173d9 MS |
1690 | /* |
1691 | * If dump data is available then see if it is valid and prepare for | |
1692 | * saving it to the disk. | |
1693 | */ | |
b500afff MS |
1694 | if (fw_dump.dump_active) { |
1695 | /* | |
1696 | * if dump process fails then invalidate the registration | |
1697 | * and release memory before proceeding for re-registration. | |
1698 | */ | |
1699 | if (process_fadump(fdm_active) < 0) | |
1700 | fadump_invalidate_release_mem(); | |
1701 | } | |
3ccc00a7 | 1702 | /* Initialize the kernel dump memory structure for FAD registration. */ |
2df173d9 | 1703 | else if (fw_dump.reserve_dump_area_size) |
3ccc00a7 MS |
1704 | init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start); |
1705 | fadump_init_files(); | |
1706 | ||
1707 | return 1; | |
1708 | } | |
1709 | subsys_initcall(setup_fadump); |