projects/linux·blob master · refs

1878 lines · 48.7 KiB

1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Firmware Assisted dump: A robust mechanism to get reliable kernel crash
4 * dump with assistance from firmware. This approach does not use kexec,
5 * instead firmware assists in booting the kdump kernel while preserving
6 * memory contents. The most of the code implementation has been adapted
7 * from phyp assisted dump implementation written by Linas Vepstas and
8 * Manish Ahuja
9 *
10 * Copyright 2011 IBM Corporation
11 * Author: Mahesh Salgaonkar <[email protected]>
12 */
13
14#undef DEBUG
15#define pr_fmt(fmt) "fadump: " fmt
16
17#include <linux/string.h>
18#include <linux/memblock.h>
19#include <linux/delay.h>
20#include <linux/seq_file.h>
21#include <linux/crash_dump.h>
22#include <linux/kobject.h>
23#include <linux/sysfs.h>
24#include <linux/slab.h>
25#include <linux/cma.h>
26#include <linux/hugetlb.h>
27#include <linux/debugfs.h>
28#include <linux/of.h>
29#include <linux/of_fdt.h>
30
31#include <asm/page.h>
32#include <asm/fadump.h>
33#include <asm/fadump-internal.h>
34#include <asm/setup.h>
35#include <asm/interrupt.h>
36#include <asm/prom.h>
37
38/*
39 * The CPU who acquired the lock to trigger the fadump crash should
40 * wait for other CPUs to enter.
41 *
42 * The timeout is in milliseconds.
43 */
44#define CRASH_TIMEOUT 500
45
46static struct fw_dump fw_dump;
47
48static void __init fadump_reserve_crash_area(u64 base);
49
50#ifndef CONFIG_PRESERVE_FA_DUMP
51
52static struct kobject *fadump_kobj;
53
54static atomic_t cpus_in_fadump;
55static DEFINE_MUTEX(fadump_mutex);
56
57#define RESERVED_RNGS_SZ 16384 /* 16K - 128 entries */
58#define RESERVED_RNGS_CNT (RESERVED_RNGS_SZ / \
59 sizeof(struct fadump_memory_range))
60static struct fadump_memory_range rngs[RESERVED_RNGS_CNT];
61static struct fadump_mrange_info
62reserved_mrange_info = { "reserved", rngs, RESERVED_RNGS_SZ, 0, RESERVED_RNGS_CNT, true };
63
64static void __init early_init_dt_scan_reserved_ranges(unsigned long node);
65
66#ifdef CONFIG_CMA
67static struct cma *fadump_cma;
68
69/*
70 * fadump_cma_init() - Initialize CMA area from a fadump reserved memory
71 *
72 * This function initializes CMA area from fadump reserved memory.
73 * The total size of fadump reserved memory covers for boot memory size
74 * + cpu data size + hpte size and metadata.
75 * Initialize only the area equivalent to boot memory size for CMA use.
76 * The remaining portion of fadump reserved memory will be not given
77 * to CMA and pages for those will stay reserved. boot memory size is
78 * aligned per CMA requirement to satisy cma_init_reserved_mem() call.
79 * But for some reason even if it fails we still have the memory reservation
80 * with us and we can still continue doing fadump.
81 */
82void __init fadump_cma_init(void)
83{
84 unsigned long long base, size, end;
85 int rc;
86
87 if (!fw_dump.fadump_supported || !fw_dump.fadump_enabled ||
88 fw_dump.dump_active)
89 return;
90 /*
91 * Do not use CMA if user has provided fadump=nocma kernel parameter.
92 */
93 if (fw_dump.nocma || !fw_dump.boot_memory_size)
94 return;
95
96 /*
97 * [base, end) should be reserved during early init in
98 * fadump_reserve_mem(). No need to check this here as
99 * cma_init_reserved_mem() already checks for overlap.
100 * Here we give the aligned chunk of this reserved memory to CMA.
101 */
102 base = fw_dump.reserve_dump_area_start;
103 size = fw_dump.boot_memory_size;
104 end = base + size;
105
106 base = ALIGN(base, CMA_MIN_ALIGNMENT_BYTES);
107 end = ALIGN_DOWN(end, CMA_MIN_ALIGNMENT_BYTES);
108 size = end - base;
109
110 if (end <= base) {
111 pr_warn("%s: Too less memory to give to CMA\n", __func__);
112 return;
113 }
114
115 rc = cma_init_reserved_mem(base, size, 0, "fadump_cma", &fadump_cma);
116 if (rc) {
117 pr_err("Failed to init cma area for firmware-assisted dump,%d\n", rc);
118 /*
119 * Though the CMA init has failed we still have memory
120 * reservation with us. The reserved memory will be
121 * blocked from production system usage. Hence return 1,
122 * so that we can continue with fadump.
123 */
124 return;
125 }
126
127 /*
128 * If CMA activation fails, keep the pages reserved, instead of
129 * exposing them to buddy allocator. Same as 'fadump=nocma' case.
130 */
131 cma_reserve_pages_on_error(fadump_cma);
132
133 /*
134 * So we now have successfully initialized cma area for fadump.
135 */
136 pr_info("Initialized [0x%llx, %luMB] cma area from [0x%lx, %luMB] "
137 "bytes of memory reserved for firmware-assisted dump\n",
138 cma_get_base(fadump_cma), cma_get_size(fadump_cma) >> 20,
139 fw_dump.reserve_dump_area_start,
140 fw_dump.boot_memory_size >> 20);
141 return;
142}
143#endif /* CONFIG_CMA */
144
145/*
146 * Additional parameters meant for capture kernel are placed in a dedicated area.
147 * If this is capture kernel boot, append these parameters to bootargs.
148 */
149void __init fadump_append_bootargs(void)
150{
151 char *append_args;
152 size_t len;
153
154 if (!fw_dump.dump_active || !fw_dump.param_area_supported || !fw_dump.param_area)
155 return;
156
157 if (fw_dump.param_area < fw_dump.boot_mem_top) {
158 if (memblock_reserve(fw_dump.param_area, COMMAND_LINE_SIZE)) {
159 pr_warn("WARNING: Can't use additional parameters area!\n");
160 fw_dump.param_area = 0;
161 return;
162 }
163 }
164
165 append_args = (char *)fw_dump.param_area;
166 len = strlen(boot_command_line);
167
168 /*
169 * Too late to fail even if cmdline size exceeds. Truncate additional parameters
170 * to cmdline size and proceed anyway.
171 */
172 if (len + strlen(append_args) >= COMMAND_LINE_SIZE - 1)
173 pr_warn("WARNING: Appending parameters exceeds cmdline size. Truncating!\n");
174
175 pr_debug("Cmdline: %s\n", boot_command_line);
176 snprintf(boot_command_line + len, COMMAND_LINE_SIZE - len, " %s", append_args);
177 pr_info("Updated cmdline: %s\n", boot_command_line);
178}
179
180/* Scan the Firmware Assisted dump configuration details. */
181int __init early_init_dt_scan_fw_dump(unsigned long node, const char *uname,
182 int depth, void *data)
183{
184 if (depth == 0) {
185 early_init_dt_scan_reserved_ranges(node);
186 return 0;
187 }
188
189 if (depth != 1)
190 return 0;
191
192 if (strcmp(uname, "rtas") == 0) {
193 rtas_fadump_dt_scan(&fw_dump, node);
194 return 1;
195 }
196
197 if (strcmp(uname, "ibm,opal") == 0) {
198 opal_fadump_dt_scan(&fw_dump, node);
199 return 1;
200 }
201
202 return 0;
203}
204
205/*
206 * If fadump is registered, check if the memory provided
207 * falls within boot memory area and reserved memory area.
208 */
209int is_fadump_memory_area(u64 addr, unsigned long size)
210{
211 u64 d_start, d_end;
212
213 if (!fw_dump.dump_registered)
214 return 0;
215
216 if (!size)
217 return 0;
218
219 d_start = fw_dump.reserve_dump_area_start;
220 d_end = d_start + fw_dump.reserve_dump_area_size;
221 if (((addr + size) > d_start) && (addr <= d_end))
222 return 1;
223
224 return (addr <= fw_dump.boot_mem_top);
225}
226
227int should_fadump_crash(void)
228{
229 if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr)
230 return 0;
231 return 1;
232}
233
234int is_fadump_active(void)
235{
236 return fw_dump.dump_active;
237}
238
239/*
240 * Returns true, if there are no holes in memory area between d_start to d_end,
241 * false otherwise.
242 */
243static bool is_fadump_mem_area_contiguous(u64 d_start, u64 d_end)
244{
245 phys_addr_t reg_start, reg_end;
246 bool ret = false;
247 u64 i, start, end;
248
249 for_each_mem_range(i, &reg_start, &reg_end) {
250 start = max_t(u64, d_start, reg_start);
251 end = min_t(u64, d_end, reg_end);
252 if (d_start < end) {
253 /* Memory hole from d_start to start */
254 if (start > d_start)
255 break;
256
257 if (end == d_end) {
258 ret = true;
259 break;
260 }
261
262 d_start = end + 1;
263 }
264 }
265
266 return ret;
267}
268
269/*
270 * Returns true, if there are no holes in reserved memory area,
271 * false otherwise.
272 */
273bool is_fadump_reserved_mem_contiguous(void)
274{
275 u64 d_start, d_end;
276
277 d_start = fw_dump.reserve_dump_area_start;
278 d_end = d_start + fw_dump.reserve_dump_area_size;
279 return is_fadump_mem_area_contiguous(d_start, d_end);
280}
281
282/* Print firmware assisted dump configurations for debugging purpose. */
283static void __init fadump_show_config(void)
284{
285 int i;
286
287 pr_debug("Support for firmware-assisted dump (fadump): %s\n",
288 (fw_dump.fadump_supported ? "present" : "no support"));
289
290 if (!fw_dump.fadump_supported)
291 return;
292
293 pr_debug("Fadump enabled : %s\n", str_yes_no(fw_dump.fadump_enabled));
294 pr_debug("Dump Active : %s\n", str_yes_no(fw_dump.dump_active));
295 pr_debug("Dump section sizes:\n");
296 pr_debug(" CPU state data size: %lx\n", fw_dump.cpu_state_data_size);
297 pr_debug(" HPTE region size : %lx\n", fw_dump.hpte_region_size);
298 pr_debug(" Boot memory size : %lx\n", fw_dump.boot_memory_size);
299 pr_debug(" Boot memory top : %llx\n", fw_dump.boot_mem_top);
300 pr_debug("Boot memory regions cnt: %llx\n", fw_dump.boot_mem_regs_cnt);
301 for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) {
302 pr_debug("[%03d] base = %llx, size = %llx\n", i,
303 fw_dump.boot_mem_addr[i], fw_dump.boot_mem_sz[i]);
304 }
305}
306
307/**
308 * fadump_calculate_reserve_size(): reserve variable boot area 5% of System RAM
309 *
310 * Function to find the largest memory size we need to reserve during early
311 * boot process. This will be the size of the memory that is required for a
312 * kernel to boot successfully.
313 *
314 * This function has been taken from phyp-assisted dump feature implementation.
315 *
316 * returns larger of 256MB or 5% rounded down to multiples of 256MB.
317 *
318 * TODO: Come up with better approach to find out more accurate memory size
319 * that is required for a kernel to boot successfully.
320 *
321 */
322static __init u64 fadump_calculate_reserve_size(void)
323{
324 u64 base, size, bootmem_min;
325 int ret;
326
327 if (fw_dump.reserve_bootvar)
328 pr_warn("'fadump_reserve_mem=' parameter is deprecated in favor of 'crashkernel=' parameter.\n");
329
330 /*
331 * Check if the size is specified through crashkernel= cmdline
332 * option. If yes, then use that but ignore base as fadump reserves
333 * memory at a predefined offset.
334 */
335 ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
336 &size, &base, NULL, NULL, NULL);
337 if (ret == 0 && size > 0) {
338 unsigned long max_size;
339
340 if (fw_dump.reserve_bootvar)
341 pr_info("Using 'crashkernel=' parameter for memory reservation.\n");
342
343 fw_dump.reserve_bootvar = (unsigned long)size;
344
345 /*
346 * Adjust if the boot memory size specified is above
347 * the upper limit.
348 */
349 max_size = memblock_phys_mem_size() / MAX_BOOT_MEM_RATIO;
350 if (fw_dump.reserve_bootvar > max_size) {
351 fw_dump.reserve_bootvar = max_size;
352 pr_info("Adjusted boot memory size to %luMB\n",
353 (fw_dump.reserve_bootvar >> 20));
354 }
355
356 return fw_dump.reserve_bootvar;
357 } else if (fw_dump.reserve_bootvar) {
358 /*
359 * 'fadump_reserve_mem=' is being used to reserve memory
360 * for firmware-assisted dump.
361 */
362 return fw_dump.reserve_bootvar;
363 }
364
365 /* divide by 20 to get 5% of value */
366 size = memblock_phys_mem_size() / 20;
367
368 /* round it down in multiples of 256 */
369 size = size & ~0x0FFFFFFFUL;
370
371 /* Truncate to memory_limit. We don't want to over reserve the memory.*/
372 if (memory_limit && size > memory_limit)
373 size = memory_limit;
374
375 bootmem_min = fw_dump.ops->fadump_get_bootmem_min();
376 return (size > bootmem_min ? size : bootmem_min);
377}
378
379/*
380 * Calculate the total memory size required to be reserved for
381 * firmware-assisted dump registration.
382 */
383static unsigned long __init get_fadump_area_size(void)
384{
385 unsigned long size = 0;
386
387 size += fw_dump.cpu_state_data_size;
388 size += fw_dump.hpte_region_size;
389 /*
390 * Account for pagesize alignment of boot memory area destination address.
391 * This faciliates in mmap reading of first kernel's memory.
392 */
393 size = PAGE_ALIGN(size);
394 size += fw_dump.boot_memory_size;
395 size += sizeof(struct fadump_crash_info_header);
396
397 /* This is to hold kernel metadata on platforms that support it */
398 size += (fw_dump.ops->fadump_get_metadata_size ?
399 fw_dump.ops->fadump_get_metadata_size() : 0);
400 return size;
401}
402
403static int __init add_boot_mem_region(unsigned long rstart,
404 unsigned long rsize)
405{
406 int max_boot_mem_rgns = fw_dump.ops->fadump_max_boot_mem_rgns();
407 int i = fw_dump.boot_mem_regs_cnt++;
408
409 if (fw_dump.boot_mem_regs_cnt > max_boot_mem_rgns) {
410 fw_dump.boot_mem_regs_cnt = max_boot_mem_rgns;
411 return 0;
412 }
413
414 pr_debug("Added boot memory range[%d] [%#016lx-%#016lx)\n",
415 i, rstart, (rstart + rsize));
416 fw_dump.boot_mem_addr[i] = rstart;
417 fw_dump.boot_mem_sz[i] = rsize;
418 return 1;
419}
420
421/*
422 * Firmware usually has a hard limit on the data it can copy per region.
423 * Honour that by splitting a memory range into multiple regions.
424 */
425static int __init add_boot_mem_regions(unsigned long mstart,
426 unsigned long msize)
427{
428 unsigned long rstart, rsize, max_size;
429 int ret = 1;
430
431 rstart = mstart;
432 max_size = fw_dump.max_copy_size ? fw_dump.max_copy_size : msize;
433 while (msize) {
434 if (msize > max_size)
435 rsize = max_size;
436 else
437 rsize = msize;
438
439 ret = add_boot_mem_region(rstart, rsize);
440 if (!ret)
441 break;
442
443 msize -= rsize;
444 rstart += rsize;
445 }
446
447 return ret;
448}
449
450static int __init fadump_get_boot_mem_regions(void)
451{
452 unsigned long size, cur_size, hole_size, last_end;
453 unsigned long mem_size = fw_dump.boot_memory_size;
454 phys_addr_t reg_start, reg_end;
455 int ret = 1;
456 u64 i;
457
458 fw_dump.boot_mem_regs_cnt = 0;
459
460 last_end = 0;
461 hole_size = 0;
462 cur_size = 0;
463 for_each_mem_range(i, &reg_start, &reg_end) {
464 size = reg_end - reg_start;
465 hole_size += (reg_start - last_end);
466
467 if ((cur_size + size) >= mem_size) {
468 size = (mem_size - cur_size);
469 ret = add_boot_mem_regions(reg_start, size);
470 break;
471 }
472
473 mem_size -= size;
474 cur_size += size;
475 ret = add_boot_mem_regions(reg_start, size);
476 if (!ret)
477 break;
478
479 last_end = reg_end;
480 }
481 fw_dump.boot_mem_top = PAGE_ALIGN(fw_dump.boot_memory_size + hole_size);
482
483 return ret;
484}
485
486/*
487 * Returns true, if the given range overlaps with reserved memory ranges
488 * starting at idx. Also, updates idx to index of overlapping memory range
489 * with the given memory range.
490 * False, otherwise.
491 */
492static bool __init overlaps_reserved_ranges(u64 base, u64 end, int *idx)
493{
494 bool ret = false;
495 int i;
496
497 for (i = *idx; i < reserved_mrange_info.mem_range_cnt; i++) {
498 u64 rbase = reserved_mrange_info.mem_ranges[i].base;
499 u64 rend = rbase + reserved_mrange_info.mem_ranges[i].size;
500
501 if (end <= rbase)
502 break;
503
504 if ((end > rbase) && (base < rend)) {
505 *idx = i;
506 ret = true;
507 break;
508 }
509 }
510
511 return ret;
512}
513
514/*
515 * Locate a suitable memory area to reserve memory for FADump. While at it,
516 * lookup reserved-ranges & avoid overlap with them, as they are used by F/W.
517 */
518static u64 __init fadump_locate_reserve_mem(u64 base, u64 size)
519{
520 struct fadump_memory_range *mrngs;
521 phys_addr_t mstart, mend;
522 int idx = 0;
523 u64 i, ret = 0;
524
525 mrngs = reserved_mrange_info.mem_ranges;
526 for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE,
527 &mstart, &mend, NULL) {
528 pr_debug("%llu) mstart: %llx, mend: %llx, base: %llx\n",
529 i, mstart, mend, base);
530
531 if (mstart > base)
532 base = PAGE_ALIGN(mstart);
533
534 while ((mend > base) && ((mend - base) >= size)) {
535 if (!overlaps_reserved_ranges(base, base+size, &idx)) {
536 ret = base;
537 goto out;
538 }
539
540 base = mrngs[idx].base + mrngs[idx].size;
541 base = PAGE_ALIGN(base);
542 }
543 }
544
545out:
546 return ret;
547}
548
549int __init fadump_reserve_mem(void)
550{
551 u64 base, size, mem_boundary, bootmem_min;
552 int ret = 1;
553
554 if (!fw_dump.fadump_enabled)
555 return 0;
556
557 if (!fw_dump.fadump_supported) {
558 pr_info("Firmware-Assisted Dump is not supported on this hardware\n");
559 goto error_out;
560 }
561
562 /*
563 * Initialize boot memory size
564 * If dump is active then we have already calculated the size during
565 * first kernel.
566 */
567 if (!fw_dump.dump_active) {
568 fw_dump.boot_memory_size =
569 PAGE_ALIGN(fadump_calculate_reserve_size());
570
571 bootmem_min = fw_dump.ops->fadump_get_bootmem_min();
572 if (fw_dump.boot_memory_size < bootmem_min) {
573 pr_err("Can't enable fadump with boot memory size (0x%lx) less than 0x%llx\n",
574 fw_dump.boot_memory_size, bootmem_min);
575 goto error_out;
576 }
577
578 if (!fadump_get_boot_mem_regions()) {
579 pr_err("Too many holes in boot memory area to enable fadump\n");
580 goto error_out;
581 }
582 }
583
584 if (memory_limit)
585 mem_boundary = memory_limit;
586 else
587 mem_boundary = memblock_end_of_DRAM();
588
589 base = fw_dump.boot_mem_top;
590 size = get_fadump_area_size();
591 fw_dump.reserve_dump_area_size = size;
592 if (fw_dump.dump_active) {
593 pr_info("Firmware-assisted dump is active.\n");
594
595#ifdef CONFIG_HUGETLB_PAGE
596 /*
597 * FADump capture kernel doesn't care much about hugepages.
598 * In fact, handling hugepages in capture kernel is asking for
599 * trouble. So, disable HugeTLB support when fadump is active.
600 */
601 hugetlb_disabled = true;
602#endif
603 /*
604 * If last boot has crashed then reserve all the memory
605 * above boot memory size so that we don't touch it until
606 * dump is written to disk by userspace tool. This memory
607 * can be released for general use by invalidating fadump.
608 */
609 fadump_reserve_crash_area(base);
610
611 pr_debug("fadumphdr_addr = %#016lx\n", fw_dump.fadumphdr_addr);
612 pr_debug("Reserve dump area start address: 0x%lx\n",
613 fw_dump.reserve_dump_area_start);
614 } else {
615 /*
616 * Reserve memory at an offset closer to bottom of the RAM to
617 * minimize the impact of memory hot-remove operation.
618 */
619 base = fadump_locate_reserve_mem(base, size);
620
621 if (!base || (base + size > mem_boundary)) {
622 pr_err("Failed to find memory chunk for reservation!\n");
623 goto error_out;
624 }
625 fw_dump.reserve_dump_area_start = base;
626
627 /*
628 * Calculate the kernel metadata address and register it with
629 * f/w if the platform supports.
630 */
631 if (fw_dump.ops->fadump_setup_metadata &&
632 (fw_dump.ops->fadump_setup_metadata(&fw_dump) < 0))
633 goto error_out;
634
635 if (memblock_reserve(base, size)) {
636 pr_err("Failed to reserve memory!\n");
637 goto error_out;
638 }
639
640 pr_info("Reserved %lldMB of memory at %#016llx (System RAM: %lldMB)\n",
641 (size >> 20), base, (memblock_phys_mem_size() >> 20));
642 }
643
644 return ret;
645error_out:
646 fw_dump.fadump_enabled = 0;
647 fw_dump.reserve_dump_area_size = 0;
648 return 0;
649}
650
651/* Look for fadump= cmdline option. */
652static int __init early_fadump_param(char *p)
653{
654 if (!p)
655 return 1;
656
657 if (strncmp(p, "on", 2) == 0)
658 fw_dump.fadump_enabled = 1;
659 else if (strncmp(p, "off", 3) == 0)
660 fw_dump.fadump_enabled = 0;
661 else if (strncmp(p, "nocma", 5) == 0) {
662 fw_dump.fadump_enabled = 1;
663 fw_dump.nocma = 1;
664 }
665
666 return 0;
667}
668early_param("fadump", early_fadump_param);
669
670/*
671 * Look for fadump_reserve_mem= cmdline option
672 * TODO: Remove references to 'fadump_reserve_mem=' parameter,
673 * the sooner 'crashkernel=' parameter is accustomed to.
674 */
675static int __init early_fadump_reserve_mem(char *p)
676{
677 if (p)
678 fw_dump.reserve_bootvar = memparse(p, &p);
679 return 0;
680}
681early_param("fadump_reserve_mem", early_fadump_reserve_mem);
682
683void crash_fadump(struct pt_regs *regs, const char *str)
684{
685 unsigned int msecs;
686 struct fadump_crash_info_header *fdh = NULL;
687 int old_cpu, this_cpu;
688 /* Do not include first CPU */
689 unsigned int ncpus = num_online_cpus() - 1;
690
691 if (!should_fadump_crash())
692 return;
693
694 /*
695 * old_cpu == -1 means this is the first CPU which has come here,
696 * go ahead and trigger fadump.
697 *
698 * old_cpu != -1 means some other CPU has already on its way
699 * to trigger fadump, just keep looping here.
700 */
701 this_cpu = smp_processor_id();
702 old_cpu = cmpxchg(&crashing_cpu, -1, this_cpu);
703
704 if (old_cpu != -1) {
705 atomic_inc(&cpus_in_fadump);
706
707 /*
708 * We can't loop here indefinitely. Wait as long as fadump
709 * is in force. If we race with fadump un-registration this
710 * loop will break and then we go down to normal panic path
711 * and reboot. If fadump is in force the first crashing
712 * cpu will definitely trigger fadump.
713 */
714 while (fw_dump.dump_registered)
715 cpu_relax();
716 return;
717 }
718
719 fdh = __va(fw_dump.fadumphdr_addr);
720 fdh->crashing_cpu = crashing_cpu;
721 crash_save_vmcoreinfo();
722
723 if (regs)
724 fdh->regs = *regs;
725 else
726 ppc_save_regs(&fdh->regs);
727
728 fdh->cpu_mask = *cpu_online_mask;
729
730 /*
731 * If we came in via system reset, wait a while for the secondary
732 * CPUs to enter.
733 */
734 if (TRAP(&(fdh->regs)) == INTERRUPT_SYSTEM_RESET) {
735 msecs = CRASH_TIMEOUT;
736 while ((atomic_read(&cpus_in_fadump) < ncpus) && (--msecs > 0))
737 mdelay(1);
738 }
739
740 fw_dump.ops->fadump_trigger(fdh, str);
741}
742
743u32 *__init fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs)
744{
745 struct elf_prstatus prstatus;
746
747 memset(&prstatus, 0, sizeof(prstatus));
748 /*
749 * FIXME: How do i get PID? Do I really need it?
750 * prstatus.pr_pid = ????
751 */
752 elf_core_copy_regs(&prstatus.pr_reg, regs);
753 buf = append_elf_note(buf, NN_PRSTATUS, NT_PRSTATUS,
754 &prstatus, sizeof(prstatus));
755 return buf;
756}
757
758void __init fadump_update_elfcore_header(char *bufp)
759{
760 struct elf_phdr *phdr;
761
762 bufp += sizeof(struct elfhdr);
763
764 /* First note is a place holder for cpu notes info. */
765 phdr = (struct elf_phdr *)bufp;
766
767 if (phdr->p_type == PT_NOTE) {
768 phdr->p_paddr = __pa(fw_dump.cpu_notes_buf_vaddr);
769 phdr->p_offset = phdr->p_paddr;
770 phdr->p_filesz = fw_dump.cpu_notes_buf_size;
771 phdr->p_memsz = fw_dump.cpu_notes_buf_size;
772 }
773 return;
774}
775
776static void *__init fadump_alloc_buffer(unsigned long size)
777{
778 return alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
779}
780
781static void fadump_free_buffer(unsigned long vaddr, unsigned long size)
782{
783 free_pages_exact((void *)vaddr, size);
784}
785
786s32 __init fadump_setup_cpu_notes_buf(u32 num_cpus)
787{
788 /* Allocate buffer to hold cpu crash notes. */
789 fw_dump.cpu_notes_buf_size = num_cpus * sizeof(note_buf_t);
790 fw_dump.cpu_notes_buf_size = PAGE_ALIGN(fw_dump.cpu_notes_buf_size);
791 fw_dump.cpu_notes_buf_vaddr =
792 (unsigned long)fadump_alloc_buffer(fw_dump.cpu_notes_buf_size);
793 if (!fw_dump.cpu_notes_buf_vaddr) {
794 pr_err("Failed to allocate %ld bytes for CPU notes buffer\n",
795 fw_dump.cpu_notes_buf_size);
796 return -ENOMEM;
797 }
798
799 pr_debug("Allocated buffer for cpu notes of size %ld at 0x%lx\n",
800 fw_dump.cpu_notes_buf_size,
801 fw_dump.cpu_notes_buf_vaddr);
802 return 0;
803}
804
805void fadump_free_cpu_notes_buf(void)
806{
807 if (!fw_dump.cpu_notes_buf_vaddr)
808 return;
809
810 fadump_free_buffer(fw_dump.cpu_notes_buf_vaddr,
811 fw_dump.cpu_notes_buf_size);
812 fw_dump.cpu_notes_buf_vaddr = 0;
813 fw_dump.cpu_notes_buf_size = 0;
814}
815
816static void fadump_free_mem_ranges(struct fadump_mrange_info *mrange_info)
817{
818 if (mrange_info->is_static) {
819 mrange_info->mem_range_cnt = 0;
820 return;
821 }
822
823 kfree(mrange_info->mem_ranges);
824 memset((void *)((u64)mrange_info + RNG_NAME_SZ), 0,
825 (sizeof(struct fadump_mrange_info) - RNG_NAME_SZ));
826}
827
828/*
829 * Allocate or reallocate mem_ranges array in incremental units
830 * of PAGE_SIZE.
831 */
832static int fadump_alloc_mem_ranges(struct fadump_mrange_info *mrange_info)
833{
834 struct fadump_memory_range *new_array;
835 u64 new_size;
836
837 new_size = mrange_info->mem_ranges_sz + PAGE_SIZE;
838 pr_debug("Allocating %llu bytes of memory for %s memory ranges\n",
839 new_size, mrange_info->name);
840
841 new_array = krealloc(mrange_info->mem_ranges, new_size, GFP_KERNEL);
842 if (new_array == NULL) {
843 pr_err("Insufficient memory for setting up %s memory ranges\n",
844 mrange_info->name);
845 fadump_free_mem_ranges(mrange_info);
846 return -ENOMEM;
847 }
848
849 mrange_info->mem_ranges = new_array;
850 mrange_info->mem_ranges_sz = new_size;
851 mrange_info->max_mem_ranges = (new_size /
852 sizeof(struct fadump_memory_range));
853 return 0;
854}
855static inline int fadump_add_mem_range(struct fadump_mrange_info *mrange_info,
856 u64 base, u64 end)
857{
858 struct fadump_memory_range *mem_ranges = mrange_info->mem_ranges;
859 bool is_adjacent = false;
860 u64 start, size;
861
862 if (base == end)
863 return 0;
864
865 /*
866 * Fold adjacent memory ranges to bring down the memory ranges/
867 * PT_LOAD segments count.
868 */
869 if (mrange_info->mem_range_cnt) {
870 start = mem_ranges[mrange_info->mem_range_cnt - 1].base;
871 size = mem_ranges[mrange_info->mem_range_cnt - 1].size;
872
873 /*
874 * Boot memory area needs separate PT_LOAD segment(s) as it
875 * is moved to a different location at the time of crash.
876 * So, fold only if the region is not boot memory area.
877 */
878 if ((start + size) == base && start >= fw_dump.boot_mem_top)
879 is_adjacent = true;
880 }
881 if (!is_adjacent) {
882 /* resize the array on reaching the limit */
883 if (mrange_info->mem_range_cnt == mrange_info->max_mem_ranges) {
884 int ret;
885
886 if (mrange_info->is_static) {
887 pr_err("Reached array size limit for %s memory ranges\n",
888 mrange_info->name);
889 return -ENOSPC;
890 }
891
892 ret = fadump_alloc_mem_ranges(mrange_info);
893 if (ret)
894 return ret;
895
896 /* Update to the new resized array */
897 mem_ranges = mrange_info->mem_ranges;
898 }
899
900 start = base;
901 mem_ranges[mrange_info->mem_range_cnt].base = start;
902 mrange_info->mem_range_cnt++;
903 }
904
905 mem_ranges[mrange_info->mem_range_cnt - 1].size = (end - start);
906 pr_debug("%s_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n",
907 mrange_info->name, (mrange_info->mem_range_cnt - 1),
908 start, end - 1, (end - start));
909 return 0;
910}
911
912static int fadump_init_elfcore_header(char *bufp)
913{
914 struct elfhdr *elf;
915
916 elf = (struct elfhdr *) bufp;
917 bufp += sizeof(struct elfhdr);
918 memcpy(elf->e_ident, ELFMAG, SELFMAG);
919 elf->e_ident[EI_CLASS] = ELF_CLASS;
920 elf->e_ident[EI_DATA] = ELF_DATA;
921 elf->e_ident[EI_VERSION] = EV_CURRENT;
922 elf->e_ident[EI_OSABI] = ELF_OSABI;
923 memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
924 elf->e_type = ET_CORE;
925 elf->e_machine = ELF_ARCH;
926 elf->e_version = EV_CURRENT;
927 elf->e_entry = 0;
928 elf->e_phoff = sizeof(struct elfhdr);
929 elf->e_shoff = 0;
930
931 if (IS_ENABLED(CONFIG_PPC64_ELF_ABI_V2))
932 elf->e_flags = 2;
933 else if (IS_ENABLED(CONFIG_PPC64_ELF_ABI_V1))
934 elf->e_flags = 1;
935 else
936 elf->e_flags = 0;
937
938 elf->e_ehsize = sizeof(struct elfhdr);
939 elf->e_phentsize = sizeof(struct elf_phdr);
940 elf->e_phnum = 0;
941 elf->e_shentsize = 0;
942 elf->e_shnum = 0;
943 elf->e_shstrndx = 0;
944
945 return 0;
946}
947
948/*
949 * If the given physical address falls within the boot memory region then
950 * return the relocated address that points to the dump region reserved
951 * for saving initial boot memory contents.
952 */
953static inline unsigned long fadump_relocate(unsigned long paddr)
954{
955 unsigned long raddr, rstart, rend, rlast, hole_size;
956 int i;
957
958 hole_size = 0;
959 rlast = 0;
960 raddr = paddr;
961 for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) {
962 rstart = fw_dump.boot_mem_addr[i];
963 rend = rstart + fw_dump.boot_mem_sz[i];
964 hole_size += (rstart - rlast);
965
966 if (paddr >= rstart && paddr < rend) {
967 raddr += fw_dump.boot_mem_dest_addr - hole_size;
968 break;
969 }
970
971 rlast = rend;
972 }
973
974 pr_debug("vmcoreinfo: paddr = 0x%lx, raddr = 0x%lx\n", paddr, raddr);
975 return raddr;
976}
977
978static void __init populate_elf_pt_load(struct elf_phdr *phdr, u64 start,
979 u64 size, unsigned long long offset)
980{
981 phdr->p_align = 0;
982 phdr->p_memsz = size;
983 phdr->p_filesz = size;
984 phdr->p_paddr = start;
985 phdr->p_offset = offset;
986 phdr->p_type = PT_LOAD;
987 phdr->p_flags = PF_R|PF_W|PF_X;
988 phdr->p_vaddr = (unsigned long)__va(start);
989}
990
991static void __init fadump_populate_elfcorehdr(struct fadump_crash_info_header *fdh)
992{
993 char *bufp;
994 struct elfhdr *elf;
995 struct elf_phdr *phdr;
996 u64 boot_mem_dest_offset;
997 unsigned long long i, ra_start, ra_end, ra_size, mstart, mend;
998
999 bufp = (char *) fw_dump.elfcorehdr_addr;
1000 fadump_init_elfcore_header(bufp);
1001 elf = (struct elfhdr *)bufp;
1002 bufp += sizeof(struct elfhdr);
1003
1004 /*
1005 * Set up ELF PT_NOTE, a placeholder for CPU notes information.
1006 * The notes info will be populated later by platform-specific code.
1007 * Hence, this PT_NOTE will always be the first ELF note.
1008 *
1009 * NOTE: Any new ELF note addition should be placed after this note.
1010 */
1011 phdr = (struct elf_phdr *)bufp;
1012 bufp += sizeof(struct elf_phdr);
1013 phdr->p_type = PT_NOTE;
1014 phdr->p_flags = 0;
1015 phdr->p_vaddr = 0;
1016 phdr->p_align = 0;
1017 phdr->p_offset = 0;
1018 phdr->p_paddr = 0;
1019 phdr->p_filesz = 0;
1020 phdr->p_memsz = 0;
1021 /* Increment number of program headers. */
1022 (elf->e_phnum)++;
1023
1024 /* setup ELF PT_NOTE for vmcoreinfo */
1025 phdr = (struct elf_phdr *)bufp;
1026 bufp += sizeof(struct elf_phdr);
1027 phdr->p_type = PT_NOTE;
1028 phdr->p_flags = 0;
1029 phdr->p_vaddr = 0;
1030 phdr->p_align = 0;
1031 phdr->p_paddr = phdr->p_offset = fdh->vmcoreinfo_raddr;
1032 phdr->p_memsz = phdr->p_filesz = fdh->vmcoreinfo_size;
1033 /* Increment number of program headers. */
1034 (elf->e_phnum)++;
1035
1036 /*
1037 * Setup PT_LOAD sections. first include boot memory regions
1038 * and then add rest of the memory regions.
1039 */
1040 boot_mem_dest_offset = fw_dump.boot_mem_dest_addr;
1041 for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) {
1042 phdr = (struct elf_phdr *)bufp;
1043 bufp += sizeof(struct elf_phdr);
1044 populate_elf_pt_load(phdr, fw_dump.boot_mem_addr[i],
1045 fw_dump.boot_mem_sz[i],
1046 boot_mem_dest_offset);
1047 /* Increment number of program headers. */
1048 (elf->e_phnum)++;
1049 boot_mem_dest_offset += fw_dump.boot_mem_sz[i];
1050 }
1051
1052 /* Memory reserved for fadump in first kernel */
1053 ra_start = fw_dump.reserve_dump_area_start;
1054 ra_size = get_fadump_area_size();
1055 ra_end = ra_start + ra_size;
1056
1057 phdr = (struct elf_phdr *)bufp;
1058 for_each_mem_range(i, &mstart, &mend) {
1059 /* Boot memory regions already added, skip them now */
1060 if (mstart < fw_dump.boot_mem_top) {
1061 if (mend > fw_dump.boot_mem_top)
1062 mstart = fw_dump.boot_mem_top;
1063 else
1064 continue;
1065 }
1066
1067 /* Handle memblock regions overlaps with fadump reserved area */
1068 if ((ra_start < mend) && (ra_end > mstart)) {
1069 if ((mstart < ra_start) && (mend > ra_end)) {
1070 populate_elf_pt_load(phdr, mstart, ra_start - mstart, mstart);
1071 /* Increment number of program headers. */
1072 (elf->e_phnum)++;
1073 bufp += sizeof(struct elf_phdr);
1074 phdr = (struct elf_phdr *)bufp;
1075 populate_elf_pt_load(phdr, ra_end, mend - ra_end, ra_end);
1076 } else if (mstart < ra_start) {
1077 populate_elf_pt_load(phdr, mstart, ra_start - mstart, mstart);
1078 } else if (ra_end < mend) {
1079 populate_elf_pt_load(phdr, ra_end, mend - ra_end, ra_end);
1080 }
1081 } else {
1082 /* No overlap with fadump reserved memory region */
1083 populate_elf_pt_load(phdr, mstart, mend - mstart, mstart);
1084 }
1085
1086 /* Increment number of program headers. */
1087 (elf->e_phnum)++;
1088 bufp += sizeof(struct elf_phdr);
1089 phdr = (struct elf_phdr *) bufp;
1090 }
1091}
1092
1093static unsigned long init_fadump_header(unsigned long addr)
1094{
1095 struct fadump_crash_info_header *fdh;
1096
1097 if (!addr)
1098 return 0;
1099
1100 fdh = __va(addr);
1101 addr += sizeof(struct fadump_crash_info_header);
1102
1103 memset(fdh, 0, sizeof(struct fadump_crash_info_header));
1104 fdh->magic_number = FADUMP_CRASH_INFO_MAGIC;
1105 fdh->version = FADUMP_HEADER_VERSION;
1106 /* We will set the crashing cpu id in crash_fadump() during crash. */
1107 fdh->crashing_cpu = FADUMP_CPU_UNKNOWN;
1108
1109 /*
1110 * The physical address and size of vmcoreinfo are required in the
1111 * second kernel to prepare elfcorehdr.
1112 */
1113 fdh->vmcoreinfo_raddr = fadump_relocate(paddr_vmcoreinfo_note());
1114 fdh->vmcoreinfo_size = VMCOREINFO_NOTE_SIZE;
1115
1116
1117 fdh->pt_regs_sz = sizeof(struct pt_regs);
1118 /*
1119 * When LPAR is terminated by PYHP, ensure all possible CPUs'
1120 * register data is processed while exporting the vmcore.
1121 */
1122 fdh->cpu_mask = *cpu_possible_mask;
1123 fdh->cpu_mask_sz = sizeof(struct cpumask);
1124
1125 return addr;
1126}
1127
1128static int register_fadump(void)
1129{
1130 unsigned long addr;
1131
1132 /*
1133 * If no memory is reserved then we can not register for firmware-
1134 * assisted dump.
1135 */
1136 if (!fw_dump.reserve_dump_area_size)
1137 return -ENODEV;
1138
1139 addr = fw_dump.fadumphdr_addr;
1140
1141 /* Initialize fadump crash info header. */
1142 addr = init_fadump_header(addr);
1143
1144 /* register the future kernel dump with firmware. */
1145 pr_debug("Registering for firmware-assisted kernel dump...\n");
1146 return fw_dump.ops->fadump_register(&fw_dump);
1147}
1148
1149void fadump_cleanup(void)
1150{
1151 if (!fw_dump.fadump_supported)
1152 return;
1153
1154 /* Invalidate the registration only if dump is active. */
1155 if (fw_dump.dump_active) {
1156 pr_debug("Invalidating firmware-assisted dump registration\n");
1157 fw_dump.ops->fadump_invalidate(&fw_dump);
1158 } else if (fw_dump.dump_registered) {
1159 /* Un-register Firmware-assisted dump if it was registered. */
1160 fw_dump.ops->fadump_unregister(&fw_dump);
1161 }
1162
1163 if (fw_dump.ops->fadump_cleanup)
1164 fw_dump.ops->fadump_cleanup(&fw_dump);
1165}
1166
1167static void fadump_free_reserved_memory(unsigned long start_pfn,
1168 unsigned long end_pfn)
1169{
1170 unsigned long pfn;
1171 unsigned long time_limit = jiffies + HZ;
1172
1173 pr_info("freeing reserved memory (0x%llx - 0x%llx)\n",
1174 PFN_PHYS(start_pfn), PFN_PHYS(end_pfn));
1175
1176 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
1177 free_reserved_page(pfn_to_page(pfn));
1178
1179 if (time_after(jiffies, time_limit)) {
1180 cond_resched();
1181 time_limit = jiffies + HZ;
1182 }
1183 }
1184}
1185
1186/*
1187 * Skip memory holes and free memory that was actually reserved.
1188 */
1189static void fadump_release_reserved_area(u64 start, u64 end)
1190{
1191 unsigned long reg_spfn, reg_epfn;
1192 u64 tstart, tend, spfn, epfn;
1193 int i;
1194
1195 spfn = PHYS_PFN(start);
1196 epfn = PHYS_PFN(end);
1197
1198 for_each_mem_pfn_range(i, MAX_NUMNODES, &reg_spfn, &reg_epfn, NULL) {
1199 tstart = max_t(u64, spfn, reg_spfn);
1200 tend = min_t(u64, epfn, reg_epfn);
1201
1202 if (tstart < tend) {
1203 fadump_free_reserved_memory(tstart, tend);
1204
1205 if (tend == epfn)
1206 break;
1207
1208 spfn = tend;
1209 }
1210 }
1211}
1212
1213/*
1214 * Sort the mem ranges in-place and merge adjacent ranges
1215 * to minimize the memory ranges count.
1216 */
1217static void sort_and_merge_mem_ranges(struct fadump_mrange_info *mrange_info)
1218{
1219 struct fadump_memory_range *mem_ranges;
1220 u64 base, size;
1221 int i, j, idx;
1222
1223 if (!reserved_mrange_info.mem_range_cnt)
1224 return;
1225
1226 /* Sort the memory ranges */
1227 mem_ranges = mrange_info->mem_ranges;
1228 for (i = 0; i < mrange_info->mem_range_cnt; i++) {
1229 idx = i;
1230 for (j = (i + 1); j < mrange_info->mem_range_cnt; j++) {
1231 if (mem_ranges[idx].base > mem_ranges[j].base)
1232 idx = j;
1233 }
1234 if (idx != i)
1235 swap(mem_ranges[idx], mem_ranges[i]);
1236 }
1237
1238 /* Merge adjacent reserved ranges */
1239 idx = 0;
1240 for (i = 1; i < mrange_info->mem_range_cnt; i++) {
1241 base = mem_ranges[i-1].base;
1242 size = mem_ranges[i-1].size;
1243 if (mem_ranges[i].base == (base + size))
1244 mem_ranges[idx].size += mem_ranges[i].size;
1245 else {
1246 idx++;
1247 if (i == idx)
1248 continue;
1249
1250 mem_ranges[idx] = mem_ranges[i];
1251 }
1252 }
1253 mrange_info->mem_range_cnt = idx + 1;
1254}
1255
1256/*
1257 * Scan reserved-ranges to consider them while reserving/releasing
1258 * memory for FADump.
1259 */
1260static void __init early_init_dt_scan_reserved_ranges(unsigned long node)
1261{
1262 const __be32 *prop;
1263 int len, ret = -1;
1264 unsigned long i;
1265
1266 /* reserved-ranges already scanned */
1267 if (reserved_mrange_info.mem_range_cnt != 0)
1268 return;
1269
1270 prop = of_get_flat_dt_prop(node, "reserved-ranges", &len);
1271 if (!prop)
1272 return;
1273
1274 /*
1275 * Each reserved range is an (address,size) pair, 2 cells each,
1276 * totalling 4 cells per range.
1277 */
1278 for (i = 0; i < len / (sizeof(*prop) * 4); i++) {
1279 u64 base, size;
1280
1281 base = of_read_number(prop + (i * 4) + 0, 2);
1282 size = of_read_number(prop + (i * 4) + 2, 2);
1283
1284 if (size) {
1285 ret = fadump_add_mem_range(&reserved_mrange_info,
1286 base, base + size);
1287 if (ret < 0) {
1288 pr_warn("some reserved ranges are ignored!\n");
1289 break;
1290 }
1291 }
1292 }
1293
1294 /* Compact reserved ranges */
1295 sort_and_merge_mem_ranges(&reserved_mrange_info);
1296}
1297
1298/*
1299 * Release the memory that was reserved during early boot to preserve the
1300 * crash'ed kernel's memory contents except reserved dump area (permanent
1301 * reservation) and reserved ranges used by F/W. The released memory will
1302 * be available for general use.
1303 */
1304static void fadump_release_memory(u64 begin, u64 end)
1305{
1306 u64 ra_start, ra_end, tstart;
1307 int i, ret;
1308
1309 ra_start = fw_dump.reserve_dump_area_start;
1310 ra_end = ra_start + fw_dump.reserve_dump_area_size;
1311
1312 /*
1313 * If reserved ranges array limit is hit, overwrite the last reserved
1314 * memory range with reserved dump area to ensure it is excluded from
1315 * the memory being released (reused for next FADump registration).
1316 */
1317 if (reserved_mrange_info.mem_range_cnt ==
1318 reserved_mrange_info.max_mem_ranges)
1319 reserved_mrange_info.mem_range_cnt--;
1320
1321 ret = fadump_add_mem_range(&reserved_mrange_info, ra_start, ra_end);
1322 if (ret != 0)
1323 return;
1324
1325 /* Get the reserved ranges list in order first. */
1326 sort_and_merge_mem_ranges(&reserved_mrange_info);
1327
1328 /* Exclude reserved ranges and release remaining memory */
1329 tstart = begin;
1330 for (i = 0; i < reserved_mrange_info.mem_range_cnt; i++) {
1331 ra_start = reserved_mrange_info.mem_ranges[i].base;
1332 ra_end = ra_start + reserved_mrange_info.mem_ranges[i].size;
1333
1334 if (tstart >= ra_end)
1335 continue;
1336
1337 if (tstart < ra_start)
1338 fadump_release_reserved_area(tstart, ra_start);
1339 tstart = ra_end;
1340 }
1341
1342 if (tstart < end)
1343 fadump_release_reserved_area(tstart, end);
1344}
1345
1346static void fadump_free_elfcorehdr_buf(void)
1347{
1348 if (fw_dump.elfcorehdr_addr == 0 || fw_dump.elfcorehdr_size == 0)
1349 return;
1350
1351 /*
1352 * Before freeing the memory of `elfcorehdr`, reset the global
1353 * `elfcorehdr_addr` to prevent modules like `vmcore` from accessing
1354 * invalid memory.
1355 */
1356 elfcorehdr_addr = ELFCORE_ADDR_ERR;
1357 fadump_free_buffer(fw_dump.elfcorehdr_addr, fw_dump.elfcorehdr_size);
1358 fw_dump.elfcorehdr_addr = 0;
1359 fw_dump.elfcorehdr_size = 0;
1360}
1361
1362static void fadump_invalidate_release_mem(void)
1363{
1364 scoped_guard(mutex, &fadump_mutex) {
1365 if (!fw_dump.dump_active)
1366 return;
1367 fadump_cleanup();
1368 }
1369
1370 fadump_free_elfcorehdr_buf();
1371 fadump_release_memory(fw_dump.boot_mem_top, memblock_end_of_DRAM());
1372 fadump_free_cpu_notes_buf();
1373
1374 /*
1375 * Setup kernel metadata and initialize the kernel dump
1376 * memory structure for FADump re-registration.
1377 */
1378 if (fw_dump.ops->fadump_setup_metadata &&
1379 (fw_dump.ops->fadump_setup_metadata(&fw_dump) < 0))
1380 pr_warn("Failed to setup kernel metadata!\n");
1381 fw_dump.ops->fadump_init_mem_struct(&fw_dump);
1382}
1383
1384static ssize_t release_mem_store(struct kobject *kobj,
1385 struct kobj_attribute *attr,
1386 const char *buf, size_t count)
1387{
1388 int input = -1;
1389
1390 if (!fw_dump.dump_active)
1391 return -EPERM;
1392
1393 if (kstrtoint(buf, 0, &input))
1394 return -EINVAL;
1395
1396 if (input == 1) {
1397 /*
1398 * Take away the '/proc/vmcore'. We are releasing the dump
1399 * memory, hence it will not be valid anymore.
1400 */
1401#ifdef CONFIG_PROC_VMCORE
1402 vmcore_cleanup();
1403#endif
1404 fadump_invalidate_release_mem();
1405
1406 } else
1407 return -EINVAL;
1408 return count;
1409}
1410
1411/* Release the reserved memory and disable the FADump */
1412static void __init unregister_fadump(void)
1413{
1414 fadump_cleanup();
1415 fadump_release_memory(fw_dump.reserve_dump_area_start,
1416 fw_dump.reserve_dump_area_size);
1417 fw_dump.fadump_enabled = 0;
1418 kobject_put(fadump_kobj);
1419}
1420
1421static ssize_t enabled_show(struct kobject *kobj,
1422 struct kobj_attribute *attr,
1423 char *buf)
1424{
1425 return sysfs_emit(buf, "%d\n", fw_dump.fadump_enabled);
1426}
1427
1428/*
1429 * /sys/kernel/fadump/hotplug_ready sysfs node returns 1, which inidcates
1430 * to usersapce that fadump re-registration is not required on memory
1431 * hotplug events.
1432 */
1433static ssize_t hotplug_ready_show(struct kobject *kobj,
1434 struct kobj_attribute *attr,
1435 char *buf)
1436{
1437 return sysfs_emit(buf, "%d\n", 1);
1438}
1439
1440static ssize_t mem_reserved_show(struct kobject *kobj,
1441 struct kobj_attribute *attr,
1442 char *buf)
1443{
1444 return sysfs_emit(buf, "%ld\n", fw_dump.reserve_dump_area_size);
1445}
1446
1447static ssize_t registered_show(struct kobject *kobj,
1448 struct kobj_attribute *attr,
1449 char *buf)
1450{
1451 return sysfs_emit(buf, "%d\n", fw_dump.dump_registered);
1452}
1453
1454static ssize_t bootargs_append_show(struct kobject *kobj,
1455 struct kobj_attribute *attr,
1456 char *buf)
1457{
1458 return sysfs_emit(buf, "%s\n", (char *)__va(fw_dump.param_area));
1459}
1460
1461static ssize_t bootargs_append_store(struct kobject *kobj,
1462 struct kobj_attribute *attr,
1463 const char *buf, size_t count)
1464{
1465 char *params;
1466
1467 if (!fw_dump.fadump_enabled || fw_dump.dump_active)
1468 return -EPERM;
1469
1470 if (count >= COMMAND_LINE_SIZE)
1471 return -EINVAL;
1472
1473 /*
1474 * Fail here instead of handling this scenario with
1475 * some silly workaround in capture kernel.
1476 */
1477 if (saved_command_line_len + count >= COMMAND_LINE_SIZE) {
1478 pr_err("Appending parameters exceeds cmdline size!\n");
1479 return -ENOSPC;
1480 }
1481
1482 params = __va(fw_dump.param_area);
1483 strscpy_pad(params, buf, COMMAND_LINE_SIZE);
1484 /* Remove newline character at the end. */
1485 if (params[count-1] == '\n')
1486 params[count-1] = '\0';
1487
1488 return count;
1489}
1490
1491static ssize_t registered_store(struct kobject *kobj,
1492 struct kobj_attribute *attr,
1493 const char *buf, size_t count)
1494{
1495 int ret = 0;
1496 int input = -1;
1497
1498 if (!fw_dump.fadump_enabled || fw_dump.dump_active)
1499 return -EPERM;
1500
1501 if (kstrtoint(buf, 0, &input))
1502 return -EINVAL;
1503
1504 mutex_lock(&fadump_mutex);
1505
1506 switch (input) {
1507 case 0:
1508 if (fw_dump.dump_registered == 0) {
1509 goto unlock_out;
1510 }
1511
1512 /* Un-register Firmware-assisted dump */
1513 pr_debug("Un-register firmware-assisted dump\n");
1514 fw_dump.ops->fadump_unregister(&fw_dump);
1515 break;
1516 case 1:
1517 if (fw_dump.dump_registered == 1) {
1518 /* Un-register Firmware-assisted dump */
1519 fw_dump.ops->fadump_unregister(&fw_dump);
1520 }
1521 /* Register Firmware-assisted dump */
1522 ret = register_fadump();
1523 break;
1524 default:
1525 ret = -EINVAL;
1526 break;
1527 }
1528
1529unlock_out:
1530 mutex_unlock(&fadump_mutex);
1531 return ret < 0 ? ret : count;
1532}
1533
1534static int fadump_region_show(struct seq_file *m, void *private)
1535{
1536 if (!fw_dump.fadump_enabled)
1537 return 0;
1538
1539 mutex_lock(&fadump_mutex);
1540 fw_dump.ops->fadump_region_show(&fw_dump, m);
1541 mutex_unlock(&fadump_mutex);
1542 return 0;
1543}
1544
1545static struct kobj_attribute release_attr = __ATTR_WO(release_mem);
1546static struct kobj_attribute enable_attr = __ATTR_RO(enabled);
1547static struct kobj_attribute register_attr = __ATTR_RW(registered);
1548static struct kobj_attribute mem_reserved_attr = __ATTR_RO(mem_reserved);
1549static struct kobj_attribute hotplug_ready_attr = __ATTR_RO(hotplug_ready);
1550static struct kobj_attribute bootargs_append_attr = __ATTR_RW(bootargs_append);
1551
1552static struct attribute *fadump_attrs[] = {
1553 &enable_attr.attr,
1554 &register_attr.attr,
1555 &mem_reserved_attr.attr,
1556 &hotplug_ready_attr.attr,
1557 NULL,
1558};
1559
1560ATTRIBUTE_GROUPS(fadump);
1561
1562DEFINE_SHOW_ATTRIBUTE(fadump_region);
1563
1564static void __init fadump_init_files(void)
1565{
1566 int rc = 0;
1567
1568 fadump_kobj = kobject_create_and_add("fadump", kernel_kobj);
1569 if (!fadump_kobj) {
1570 pr_err("failed to create fadump kobject\n");
1571 return;
1572 }
1573
1574 if (fw_dump.param_area) {
1575 rc = sysfs_create_file(fadump_kobj, &bootargs_append_attr.attr);
1576 if (rc)
1577 pr_err("unable to create bootargs_append sysfs file (%d)\n", rc);
1578 }
1579
1580 debugfs_create_file("fadump_region", 0444, arch_debugfs_dir, NULL,
1581 &fadump_region_fops);
1582
1583 if (fw_dump.dump_active) {
1584 rc = sysfs_create_file(fadump_kobj, &release_attr.attr);
1585 if (rc)
1586 pr_err("unable to create release_mem sysfs file (%d)\n",
1587 rc);
1588 }
1589
1590 rc = sysfs_create_groups(fadump_kobj, fadump_groups);
1591 if (rc) {
1592 pr_err("sysfs group creation failed (%d), unregistering FADump",
1593 rc);
1594 unregister_fadump();
1595 return;
1596 }
1597
1598 /*
1599 * The FADump sysfs are moved from kernel_kobj to fadump_kobj need to
1600 * create symlink at old location to maintain backward compatibility.
1601 *
1602 * - fadump_enabled -> fadump/enabled
1603 * - fadump_registered -> fadump/registered
1604 * - fadump_release_mem -> fadump/release_mem
1605 */
1606 rc = compat_only_sysfs_link_entry_to_kobj(kernel_kobj, fadump_kobj,
1607 "enabled", "fadump_enabled");
1608 if (rc) {
1609 pr_err("unable to create fadump_enabled symlink (%d)", rc);
1610 return;
1611 }
1612
1613 rc = compat_only_sysfs_link_entry_to_kobj(kernel_kobj, fadump_kobj,
1614 "registered",
1615 "fadump_registered");
1616 if (rc) {
1617 pr_err("unable to create fadump_registered symlink (%d)", rc);
1618 sysfs_remove_link(kernel_kobj, "fadump_enabled");
1619 return;
1620 }
1621
1622 if (fw_dump.dump_active) {
1623 rc = compat_only_sysfs_link_entry_to_kobj(kernel_kobj,
1624 fadump_kobj,
1625 "release_mem",
1626 "fadump_release_mem");
1627 if (rc)
1628 pr_err("unable to create fadump_release_mem symlink (%d)",
1629 rc);
1630 }
1631 return;
1632}
1633
1634static int __init fadump_setup_elfcorehdr_buf(void)
1635{
1636 int elf_phdr_cnt;
1637 unsigned long elfcorehdr_size;
1638
1639 /*
1640 * Program header for CPU notes comes first, followed by one for
1641 * vmcoreinfo, and the remaining program headers correspond to
1642 * memory regions.
1643 */
1644 elf_phdr_cnt = 2 + fw_dump.boot_mem_regs_cnt + memblock_num_regions(memory);
1645 elfcorehdr_size = sizeof(struct elfhdr) + (elf_phdr_cnt * sizeof(struct elf_phdr));
1646 elfcorehdr_size = PAGE_ALIGN(elfcorehdr_size);
1647
1648 fw_dump.elfcorehdr_addr = (u64)fadump_alloc_buffer(elfcorehdr_size);
1649 if (!fw_dump.elfcorehdr_addr) {
1650 pr_err("Failed to allocate %lu bytes for elfcorehdr\n",
1651 elfcorehdr_size);
1652 return -ENOMEM;
1653 }
1654 fw_dump.elfcorehdr_size = elfcorehdr_size;
1655 return 0;
1656}
1657
1658/*
1659 * Check if the fadump header of crashed kernel is compatible with fadump kernel.
1660 *
1661 * It checks the magic number, endianness, and size of non-primitive type
1662 * members of fadump header to ensure safe dump collection.
1663 */
1664static bool __init is_fadump_header_compatible(struct fadump_crash_info_header *fdh)
1665{
1666 if (fdh->magic_number == FADUMP_CRASH_INFO_MAGIC_OLD) {
1667 pr_err("Old magic number, can't process the dump.\n");
1668 return false;
1669 }
1670
1671 if (fdh->magic_number != FADUMP_CRASH_INFO_MAGIC) {
1672 if (fdh->magic_number == swab64(FADUMP_CRASH_INFO_MAGIC))
1673 pr_err("Endianness mismatch between the crashed and fadump kernels.\n");
1674 else
1675 pr_err("Fadump header is corrupted.\n");
1676
1677 return false;
1678 }
1679
1680 /*
1681 * Dump collection is not safe if the size of non-primitive type members
1682 * of the fadump header do not match between crashed and fadump kernel.
1683 */
1684 if (fdh->pt_regs_sz != sizeof(struct pt_regs) ||
1685 fdh->cpu_mask_sz != sizeof(struct cpumask)) {
1686 pr_err("Fadump header size mismatch.\n");
1687 return false;
1688 }
1689
1690 return true;
1691}
1692
1693static void __init fadump_process(void)
1694{
1695 struct fadump_crash_info_header *fdh;
1696
1697 fdh = (struct fadump_crash_info_header *) __va(fw_dump.fadumphdr_addr);
1698 if (!fdh) {
1699 pr_err("Crash info header is empty.\n");
1700 goto err_out;
1701 }
1702
1703 /* Avoid processing the dump if fadump header isn't compatible */
1704 if (!is_fadump_header_compatible(fdh))
1705 goto err_out;
1706
1707 /* Allocate buffer for elfcorehdr */
1708 if (fadump_setup_elfcorehdr_buf())
1709 goto err_out;
1710
1711 fadump_populate_elfcorehdr(fdh);
1712
1713 /* Let platform update the CPU notes in elfcorehdr */
1714 if (fw_dump.ops->fadump_process(&fw_dump) < 0)
1715 goto err_out;
1716
1717 /*
1718 * elfcorehdr is now ready to be exported.
1719 *
1720 * set elfcorehdr_addr so that vmcore module will export the
1721 * elfcorehdr through '/proc/vmcore'.
1722 */
1723 elfcorehdr_addr = virt_to_phys((void *)fw_dump.elfcorehdr_addr);
1724 return;
1725
1726err_out:
1727 fadump_invalidate_release_mem();
1728}
1729
1730/*
1731 * Reserve memory to store additional parameters to be passed
1732 * for fadump/capture kernel.
1733 */
1734void __init fadump_setup_param_area(void)
1735{
1736 phys_addr_t range_start, range_end;
1737
1738 if (!fw_dump.fadump_enabled)
1739 return;
1740
1741 if (!fw_dump.param_area_supported || fw_dump.dump_active)
1742 return;
1743
1744 /* This memory can't be used by PFW or bootloader as it is shared across kernels */
1745 if (early_radix_enabled()) {
1746 /*
1747 * Anywhere in the upper half should be good enough as all memory
1748 * is accessible in real mode.
1749 */
1750 range_start = memblock_end_of_DRAM() / 2;
1751 range_end = memblock_end_of_DRAM();
1752 } else {
1753 /*
1754 * Memory range for passing additional parameters for HASH MMU
1755 * must meet the following conditions:
1756 * 1. The first memory block size must be higher than the
1757 * minimum RMA (MIN_RMA) size. Bootloader can use memory
1758 * upto RMA size. So it should be avoided.
1759 * 2. The range should be between MIN_RMA and RMA size (ppc64_rma_size)
1760 * 3. It must not overlap with the fadump reserved area.
1761 */
1762 if (ppc64_rma_size < MIN_RMA)
1763 return;
1764
1765 range_start = MIN_RMA;
1766 range_end = min(ppc64_rma_size, fw_dump.boot_mem_top);
1767 }
1768
1769 fw_dump.param_area = memblock_phys_alloc_range(COMMAND_LINE_SIZE,
1770 COMMAND_LINE_SIZE,
1771 range_start,
1772 range_end);
1773 if (!fw_dump.param_area) {
1774 pr_warn("WARNING: Could not setup area to pass additional parameters!\n");
1775 return;
1776 }
1777
1778 memset((void *)fw_dump.param_area, 0, COMMAND_LINE_SIZE);
1779}
1780
1781/*
1782 * Prepare for firmware-assisted dump.
1783 */
1784int __init setup_fadump(void)
1785{
1786 if (!fw_dump.fadump_supported)
1787 return 0;
1788
1789 fadump_init_files();
1790 fadump_show_config();
1791
1792 if (!fw_dump.fadump_enabled)
1793 return 1;
1794
1795 /*
1796 * If dump data is available then see if it is valid and prepare for
1797 * saving it to the disk.
1798 */
1799 if (fw_dump.dump_active) {
1800 fadump_process();
1801 }
1802 /* Initialize the kernel dump memory structure and register with f/w */
1803 else if (fw_dump.reserve_dump_area_size) {
1804 fw_dump.ops->fadump_init_mem_struct(&fw_dump);
1805 register_fadump();
1806 }
1807
1808 /*
1809 * In case of panic, fadump is triggered via ppc_panic_event()
1810 * panic notifier. Setting crash_kexec_post_notifiers to 'true'
1811 * lets panic() function take crash friendly path before panic
1812 * notifiers are invoked.
1813 */
1814 crash_kexec_post_notifiers = true;
1815
1816 return 1;
1817}
1818/*
1819 * Use subsys_initcall_sync() here because there is dependency with
1820 * crash_save_vmcoreinfo_init(), which must run first to ensure vmcoreinfo initialization
1821 * is done before registering with f/w.
1822 */
1823subsys_initcall_sync(setup_fadump);
1824#else /* !CONFIG_PRESERVE_FA_DUMP */
1825
1826/* Scan the Firmware Assisted dump configuration details. */
1827int __init early_init_dt_scan_fw_dump(unsigned long node, const char *uname,
1828 int depth, void *data)
1829{
1830 if ((depth != 1) || (strcmp(uname, "ibm,opal") != 0))
1831 return 0;
1832
1833 opal_fadump_dt_scan(&fw_dump, node);
1834 return 1;
1835}
1836
1837/*
1838 * When dump is active but PRESERVE_FA_DUMP is enabled on the kernel,
1839 * preserve crash data. The subsequent memory preserving kernel boot
1840 * is likely to process this crash data.
1841 */
1842int __init fadump_reserve_mem(void)
1843{
1844 if (fw_dump.dump_active) {
1845 /*
1846 * If last boot has crashed then reserve all the memory
1847 * above boot memory to preserve crash data.
1848 */
1849 pr_info("Preserving crash data for processing in next boot.\n");
1850 fadump_reserve_crash_area(fw_dump.boot_mem_top);
1851 } else
1852 pr_debug("FADump-aware kernel..\n");
1853
1854 return 1;
1855}
1856#endif /* CONFIG_PRESERVE_FA_DUMP */
1857
1858/* Preserve everything above the base address */
1859static void __init fadump_reserve_crash_area(u64 base)
1860{
1861 u64 i, mstart, mend, msize;
1862
1863 for_each_mem_range(i, &mstart, &mend) {
1864 msize = mend - mstart;
1865
1866 if ((mstart + msize) < base)
1867 continue;
1868
1869 if (mstart < base) {
1870 msize -= (base - mstart);
1871 mstart = base;
1872 }
1873
1874 pr_info("Reserving %lluMB of memory at %#016llx for preserving crash data",
1875 (msize >> 20), mstart);
1876 memblock_reserve(mstart, msize);
1877 }
1878}