powerpc/fadump: use static allocation for reserved memory ranges
[linux/fpc-iii.git] / arch / powerpc / kernel / fadump.c
blob679277b28aef017b26fc890a5be560cf2b613f10
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
10 * Copyright 2011 IBM Corporation
11 * Author: Mahesh Salgaonkar <mahesh@linux.vnet.ibm.com>
14 #undef DEBUG
15 #define pr_fmt(fmt) "fadump: " fmt
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>
28 #include <asm/debugfs.h>
29 #include <asm/page.h>
30 #include <asm/prom.h>
31 #include <asm/fadump.h>
32 #include <asm/fadump-internal.h>
33 #include <asm/setup.h>
35 static struct fw_dump fw_dump;
37 static void __init fadump_reserve_crash_area(u64 base);
39 struct kobject *fadump_kobj;
41 #ifndef CONFIG_PRESERVE_FA_DUMP
42 static DEFINE_MUTEX(fadump_mutex);
43 struct fadump_mrange_info crash_mrange_info = { "crash", NULL, 0, 0, 0, false };
45 #define RESERVED_RNGS_SZ 16384 /* 16K - 128 entries */
46 #define RESERVED_RNGS_CNT (RESERVED_RNGS_SZ / \
47 sizeof(struct fadump_memory_range))
48 static struct fadump_memory_range rngs[RESERVED_RNGS_CNT];
49 struct fadump_mrange_info reserved_mrange_info = { "reserved", rngs,
50 RESERVED_RNGS_SZ, 0,
51 RESERVED_RNGS_CNT, true };
53 static void __init early_init_dt_scan_reserved_ranges(unsigned long node);
55 #ifdef CONFIG_CMA
56 static struct cma *fadump_cma;
59 * fadump_cma_init() - Initialize CMA area from a fadump reserved memory
61 * This function initializes CMA area from fadump reserved memory.
62 * The total size of fadump reserved memory covers for boot memory size
63 * + cpu data size + hpte size and metadata.
64 * Initialize only the area equivalent to boot memory size for CMA use.
65 * The reamining portion of fadump reserved memory will be not given
66 * to CMA and pages for thoes will stay reserved. boot memory size is
67 * aligned per CMA requirement to satisy cma_init_reserved_mem() call.
68 * But for some reason even if it fails we still have the memory reservation
69 * with us and we can still continue doing fadump.
71 int __init fadump_cma_init(void)
73 unsigned long long base, size;
74 int rc;
76 if (!fw_dump.fadump_enabled)
77 return 0;
80 * Do not use CMA if user has provided fadump=nocma kernel parameter.
81 * Return 1 to continue with fadump old behaviour.
83 if (fw_dump.nocma)
84 return 1;
86 base = fw_dump.reserve_dump_area_start;
87 size = fw_dump.boot_memory_size;
89 if (!size)
90 return 0;
92 rc = cma_init_reserved_mem(base, size, 0, "fadump_cma", &fadump_cma);
93 if (rc) {
94 pr_err("Failed to init cma area for firmware-assisted dump,%d\n", rc);
96 * Though the CMA init has failed we still have memory
97 * reservation with us. The reserved memory will be
98 * blocked from production system usage. Hence return 1,
99 * so that we can continue with fadump.
101 return 1;
105 * So we now have successfully initialized cma area for fadump.
107 pr_info("Initialized 0x%lx bytes cma area at %ldMB from 0x%lx "
108 "bytes of memory reserved for firmware-assisted dump\n",
109 cma_get_size(fadump_cma),
110 (unsigned long)cma_get_base(fadump_cma) >> 20,
111 fw_dump.reserve_dump_area_size);
112 return 1;
114 #else
115 static int __init fadump_cma_init(void) { return 1; }
116 #endif /* CONFIG_CMA */
118 /* Scan the Firmware Assisted dump configuration details. */
119 int __init early_init_dt_scan_fw_dump(unsigned long node, const char *uname,
120 int depth, void *data)
122 if (depth == 0) {
123 early_init_dt_scan_reserved_ranges(node);
124 return 0;
127 if (depth != 1)
128 return 0;
130 if (strcmp(uname, "rtas") == 0) {
131 rtas_fadump_dt_scan(&fw_dump, node);
132 return 1;
135 if (strcmp(uname, "ibm,opal") == 0) {
136 opal_fadump_dt_scan(&fw_dump, node);
137 return 1;
140 return 0;
144 * If fadump is registered, check if the memory provided
145 * falls within boot memory area and reserved memory area.
147 int is_fadump_memory_area(u64 addr, unsigned long size)
149 u64 d_start, d_end;
151 if (!fw_dump.dump_registered)
152 return 0;
154 if (!size)
155 return 0;
157 d_start = fw_dump.reserve_dump_area_start;
158 d_end = d_start + fw_dump.reserve_dump_area_size;
159 if (((addr + size) > d_start) && (addr <= d_end))
160 return 1;
162 return (addr <= fw_dump.boot_mem_top);
165 int should_fadump_crash(void)
167 if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr)
168 return 0;
169 return 1;
172 int is_fadump_active(void)
174 return fw_dump.dump_active;
178 * Returns true, if there are no holes in memory area between d_start to d_end,
179 * false otherwise.
181 static bool is_fadump_mem_area_contiguous(u64 d_start, u64 d_end)
183 struct memblock_region *reg;
184 bool ret = false;
185 u64 start, end;
187 for_each_memblock(memory, reg) {
188 start = max_t(u64, d_start, reg->base);
189 end = min_t(u64, d_end, (reg->base + reg->size));
190 if (d_start < end) {
191 /* Memory hole from d_start to start */
192 if (start > d_start)
193 break;
195 if (end == d_end) {
196 ret = true;
197 break;
200 d_start = end + 1;
204 return ret;
208 * Returns true, if there are no holes in boot memory area,
209 * false otherwise.
211 bool is_fadump_boot_mem_contiguous(void)
213 unsigned long d_start, d_end;
214 bool ret = false;
215 int i;
217 for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) {
218 d_start = fw_dump.boot_mem_addr[i];
219 d_end = d_start + fw_dump.boot_mem_sz[i];
221 ret = is_fadump_mem_area_contiguous(d_start, d_end);
222 if (!ret)
223 break;
226 return ret;
230 * Returns true, if there are no holes in reserved memory area,
231 * false otherwise.
233 bool is_fadump_reserved_mem_contiguous(void)
235 u64 d_start, d_end;
237 d_start = fw_dump.reserve_dump_area_start;
238 d_end = d_start + fw_dump.reserve_dump_area_size;
239 return is_fadump_mem_area_contiguous(d_start, d_end);
242 /* Print firmware assisted dump configurations for debugging purpose. */
243 static void fadump_show_config(void)
245 int i;
247 pr_debug("Support for firmware-assisted dump (fadump): %s\n",
248 (fw_dump.fadump_supported ? "present" : "no support"));
250 if (!fw_dump.fadump_supported)
251 return;
253 pr_debug("Fadump enabled : %s\n",
254 (fw_dump.fadump_enabled ? "yes" : "no"));
255 pr_debug("Dump Active : %s\n",
256 (fw_dump.dump_active ? "yes" : "no"));
257 pr_debug("Dump section sizes:\n");
258 pr_debug(" CPU state data size: %lx\n", fw_dump.cpu_state_data_size);
259 pr_debug(" HPTE region size : %lx\n", fw_dump.hpte_region_size);
260 pr_debug(" Boot memory size : %lx\n", fw_dump.boot_memory_size);
261 pr_debug(" Boot memory top : %llx\n", fw_dump.boot_mem_top);
262 pr_debug("Boot memory regions cnt: %llx\n", fw_dump.boot_mem_regs_cnt);
263 for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) {
264 pr_debug("[%03d] base = %llx, size = %llx\n", i,
265 fw_dump.boot_mem_addr[i], fw_dump.boot_mem_sz[i]);
270 * fadump_calculate_reserve_size(): reserve variable boot area 5% of System RAM
272 * Function to find the largest memory size we need to reserve during early
273 * boot process. This will be the size of the memory that is required for a
274 * kernel to boot successfully.
276 * This function has been taken from phyp-assisted dump feature implementation.
278 * returns larger of 256MB or 5% rounded down to multiples of 256MB.
280 * TODO: Come up with better approach to find out more accurate memory size
281 * that is required for a kernel to boot successfully.
284 static inline u64 fadump_calculate_reserve_size(void)
286 u64 base, size, bootmem_min;
287 int ret;
289 if (fw_dump.reserve_bootvar)
290 pr_warn("'fadump_reserve_mem=' parameter is deprecated in favor of 'crashkernel=' parameter.\n");
293 * Check if the size is specified through crashkernel= cmdline
294 * option. If yes, then use that but ignore base as fadump reserves
295 * memory at a predefined offset.
297 ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
298 &size, &base);
299 if (ret == 0 && size > 0) {
300 unsigned long max_size;
302 if (fw_dump.reserve_bootvar)
303 pr_info("Using 'crashkernel=' parameter for memory reservation.\n");
305 fw_dump.reserve_bootvar = (unsigned long)size;
308 * Adjust if the boot memory size specified is above
309 * the upper limit.
311 max_size = memblock_phys_mem_size() / MAX_BOOT_MEM_RATIO;
312 if (fw_dump.reserve_bootvar > max_size) {
313 fw_dump.reserve_bootvar = max_size;
314 pr_info("Adjusted boot memory size to %luMB\n",
315 (fw_dump.reserve_bootvar >> 20));
318 return fw_dump.reserve_bootvar;
319 } else if (fw_dump.reserve_bootvar) {
321 * 'fadump_reserve_mem=' is being used to reserve memory
322 * for firmware-assisted dump.
324 return fw_dump.reserve_bootvar;
327 /* divide by 20 to get 5% of value */
328 size = memblock_phys_mem_size() / 20;
330 /* round it down in multiples of 256 */
331 size = size & ~0x0FFFFFFFUL;
333 /* Truncate to memory_limit. We don't want to over reserve the memory.*/
334 if (memory_limit && size > memory_limit)
335 size = memory_limit;
337 bootmem_min = fw_dump.ops->fadump_get_bootmem_min();
338 return (size > bootmem_min ? size : bootmem_min);
342 * Calculate the total memory size required to be reserved for
343 * firmware-assisted dump registration.
345 static unsigned long get_fadump_area_size(void)
347 unsigned long size = 0;
349 size += fw_dump.cpu_state_data_size;
350 size += fw_dump.hpte_region_size;
351 size += fw_dump.boot_memory_size;
352 size += sizeof(struct fadump_crash_info_header);
353 size += sizeof(struct elfhdr); /* ELF core header.*/
354 size += sizeof(struct elf_phdr); /* place holder for cpu notes */
355 /* Program headers for crash memory regions. */
356 size += sizeof(struct elf_phdr) * (memblock_num_regions(memory) + 2);
358 size = PAGE_ALIGN(size);
360 /* This is to hold kernel metadata on platforms that support it */
361 size += (fw_dump.ops->fadump_get_metadata_size ?
362 fw_dump.ops->fadump_get_metadata_size() : 0);
363 return size;
366 static int __init add_boot_mem_region(unsigned long rstart,
367 unsigned long rsize)
369 int i = fw_dump.boot_mem_regs_cnt++;
371 if (fw_dump.boot_mem_regs_cnt > FADUMP_MAX_MEM_REGS) {
372 fw_dump.boot_mem_regs_cnt = FADUMP_MAX_MEM_REGS;
373 return 0;
376 pr_debug("Added boot memory range[%d] [%#016lx-%#016lx)\n",
377 i, rstart, (rstart + rsize));
378 fw_dump.boot_mem_addr[i] = rstart;
379 fw_dump.boot_mem_sz[i] = rsize;
380 return 1;
384 * Firmware usually has a hard limit on the data it can copy per region.
385 * Honour that by splitting a memory range into multiple regions.
387 static int __init add_boot_mem_regions(unsigned long mstart,
388 unsigned long msize)
390 unsigned long rstart, rsize, max_size;
391 int ret = 1;
393 rstart = mstart;
394 max_size = fw_dump.max_copy_size ? fw_dump.max_copy_size : msize;
395 while (msize) {
396 if (msize > max_size)
397 rsize = max_size;
398 else
399 rsize = msize;
401 ret = add_boot_mem_region(rstart, rsize);
402 if (!ret)
403 break;
405 msize -= rsize;
406 rstart += rsize;
409 return ret;
412 static int __init fadump_get_boot_mem_regions(void)
414 unsigned long base, size, cur_size, hole_size, last_end;
415 unsigned long mem_size = fw_dump.boot_memory_size;
416 struct memblock_region *reg;
417 int ret = 1;
419 fw_dump.boot_mem_regs_cnt = 0;
421 last_end = 0;
422 hole_size = 0;
423 cur_size = 0;
424 for_each_memblock(memory, reg) {
425 base = reg->base;
426 size = reg->size;
427 hole_size += (base - last_end);
429 if ((cur_size + size) >= mem_size) {
430 size = (mem_size - cur_size);
431 ret = add_boot_mem_regions(base, size);
432 break;
435 mem_size -= size;
436 cur_size += size;
437 ret = add_boot_mem_regions(base, size);
438 if (!ret)
439 break;
441 last_end = base + size;
443 fw_dump.boot_mem_top = PAGE_ALIGN(fw_dump.boot_memory_size + hole_size);
445 return ret;
448 int __init fadump_reserve_mem(void)
450 u64 base, size, mem_boundary, bootmem_min, align = PAGE_SIZE;
451 bool is_memblock_bottom_up = memblock_bottom_up();
452 int ret = 1;
454 if (!fw_dump.fadump_enabled)
455 return 0;
457 if (!fw_dump.fadump_supported) {
458 pr_info("Firmware-Assisted Dump is not supported on this hardware\n");
459 goto error_out;
463 * Initialize boot memory size
464 * If dump is active then we have already calculated the size during
465 * first kernel.
467 if (!fw_dump.dump_active) {
468 fw_dump.boot_memory_size =
469 PAGE_ALIGN(fadump_calculate_reserve_size());
470 #ifdef CONFIG_CMA
471 if (!fw_dump.nocma) {
472 align = FADUMP_CMA_ALIGNMENT;
473 fw_dump.boot_memory_size =
474 ALIGN(fw_dump.boot_memory_size, align);
476 #endif
478 bootmem_min = fw_dump.ops->fadump_get_bootmem_min();
479 if (fw_dump.boot_memory_size < bootmem_min) {
480 pr_err("Can't enable fadump with boot memory size (0x%lx) less than 0x%llx\n",
481 fw_dump.boot_memory_size, bootmem_min);
482 goto error_out;
485 if (!fadump_get_boot_mem_regions()) {
486 pr_err("Too many holes in boot memory area to enable fadump\n");
487 goto error_out;
492 * Calculate the memory boundary.
493 * If memory_limit is less than actual memory boundary then reserve
494 * the memory for fadump beyond the memory_limit and adjust the
495 * memory_limit accordingly, so that the running kernel can run with
496 * specified memory_limit.
498 if (memory_limit && memory_limit < memblock_end_of_DRAM()) {
499 size = get_fadump_area_size();
500 if ((memory_limit + size) < memblock_end_of_DRAM())
501 memory_limit += size;
502 else
503 memory_limit = memblock_end_of_DRAM();
504 printk(KERN_INFO "Adjusted memory_limit for firmware-assisted"
505 " dump, now %#016llx\n", memory_limit);
507 if (memory_limit)
508 mem_boundary = memory_limit;
509 else
510 mem_boundary = memblock_end_of_DRAM();
512 base = fw_dump.boot_mem_top;
513 size = get_fadump_area_size();
514 fw_dump.reserve_dump_area_size = size;
515 if (fw_dump.dump_active) {
516 pr_info("Firmware-assisted dump is active.\n");
518 #ifdef CONFIG_HUGETLB_PAGE
520 * FADump capture kernel doesn't care much about hugepages.
521 * In fact, handling hugepages in capture kernel is asking for
522 * trouble. So, disable HugeTLB support when fadump is active.
524 hugetlb_disabled = true;
525 #endif
527 * If last boot has crashed then reserve all the memory
528 * above boot memory size so that we don't touch it until
529 * dump is written to disk by userspace tool. This memory
530 * can be released for general use by invalidating fadump.
532 fadump_reserve_crash_area(base);
534 pr_debug("fadumphdr_addr = %#016lx\n", fw_dump.fadumphdr_addr);
535 pr_debug("Reserve dump area start address: 0x%lx\n",
536 fw_dump.reserve_dump_area_start);
537 } else {
539 * Reserve memory at an offset closer to bottom of the RAM to
540 * minimize the impact of memory hot-remove operation.
542 memblock_set_bottom_up(true);
543 base = memblock_find_in_range(base, mem_boundary, size, align);
545 /* Restore the previous allocation mode */
546 memblock_set_bottom_up(is_memblock_bottom_up);
548 if (!base) {
549 pr_err("Failed to find memory chunk for reservation!\n");
550 goto error_out;
552 fw_dump.reserve_dump_area_start = base;
555 * Calculate the kernel metadata address and register it with
556 * f/w if the platform supports.
558 if (fw_dump.ops->fadump_setup_metadata &&
559 (fw_dump.ops->fadump_setup_metadata(&fw_dump) < 0))
560 goto error_out;
562 if (memblock_reserve(base, size)) {
563 pr_err("Failed to reserve memory!\n");
564 goto error_out;
567 pr_info("Reserved %lldMB of memory at %#016llx (System RAM: %lldMB)\n",
568 (size >> 20), base, (memblock_phys_mem_size() >> 20));
570 ret = fadump_cma_init();
573 return ret;
574 error_out:
575 fw_dump.fadump_enabled = 0;
576 return 0;
579 /* Look for fadump= cmdline option. */
580 static int __init early_fadump_param(char *p)
582 if (!p)
583 return 1;
585 if (strncmp(p, "on", 2) == 0)
586 fw_dump.fadump_enabled = 1;
587 else if (strncmp(p, "off", 3) == 0)
588 fw_dump.fadump_enabled = 0;
589 else if (strncmp(p, "nocma", 5) == 0) {
590 fw_dump.fadump_enabled = 1;
591 fw_dump.nocma = 1;
594 return 0;
596 early_param("fadump", early_fadump_param);
599 * Look for fadump_reserve_mem= cmdline option
600 * TODO: Remove references to 'fadump_reserve_mem=' parameter,
601 * the sooner 'crashkernel=' parameter is accustomed to.
603 static int __init early_fadump_reserve_mem(char *p)
605 if (p)
606 fw_dump.reserve_bootvar = memparse(p, &p);
607 return 0;
609 early_param("fadump_reserve_mem", early_fadump_reserve_mem);
611 void crash_fadump(struct pt_regs *regs, const char *str)
613 struct fadump_crash_info_header *fdh = NULL;
614 int old_cpu, this_cpu;
616 if (!should_fadump_crash())
617 return;
620 * old_cpu == -1 means this is the first CPU which has come here,
621 * go ahead and trigger fadump.
623 * old_cpu != -1 means some other CPU has already on it's way
624 * to trigger fadump, just keep looping here.
626 this_cpu = smp_processor_id();
627 old_cpu = cmpxchg(&crashing_cpu, -1, this_cpu);
629 if (old_cpu != -1) {
631 * We can't loop here indefinitely. Wait as long as fadump
632 * is in force. If we race with fadump un-registration this
633 * loop will break and then we go down to normal panic path
634 * and reboot. If fadump is in force the first crashing
635 * cpu will definitely trigger fadump.
637 while (fw_dump.dump_registered)
638 cpu_relax();
639 return;
642 fdh = __va(fw_dump.fadumphdr_addr);
643 fdh->crashing_cpu = crashing_cpu;
644 crash_save_vmcoreinfo();
646 if (regs)
647 fdh->regs = *regs;
648 else
649 ppc_save_regs(&fdh->regs);
651 fdh->online_mask = *cpu_online_mask;
653 fw_dump.ops->fadump_trigger(fdh, str);
656 u32 *fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs)
658 struct elf_prstatus prstatus;
660 memset(&prstatus, 0, sizeof(prstatus));
662 * FIXME: How do i get PID? Do I really need it?
663 * prstatus.pr_pid = ????
665 elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
666 buf = append_elf_note(buf, CRASH_CORE_NOTE_NAME, NT_PRSTATUS,
667 &prstatus, sizeof(prstatus));
668 return buf;
671 void fadump_update_elfcore_header(char *bufp)
673 struct elfhdr *elf;
674 struct elf_phdr *phdr;
676 elf = (struct elfhdr *)bufp;
677 bufp += sizeof(struct elfhdr);
679 /* First note is a place holder for cpu notes info. */
680 phdr = (struct elf_phdr *)bufp;
682 if (phdr->p_type == PT_NOTE) {
683 phdr->p_paddr = __pa(fw_dump.cpu_notes_buf_vaddr);
684 phdr->p_offset = phdr->p_paddr;
685 phdr->p_filesz = fw_dump.cpu_notes_buf_size;
686 phdr->p_memsz = fw_dump.cpu_notes_buf_size;
688 return;
691 static void *fadump_alloc_buffer(unsigned long size)
693 unsigned long count, i;
694 struct page *page;
695 void *vaddr;
697 vaddr = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
698 if (!vaddr)
699 return NULL;
701 count = PAGE_ALIGN(size) / PAGE_SIZE;
702 page = virt_to_page(vaddr);
703 for (i = 0; i < count; i++)
704 mark_page_reserved(page + i);
705 return vaddr;
708 static void fadump_free_buffer(unsigned long vaddr, unsigned long size)
710 free_reserved_area((void *)vaddr, (void *)(vaddr + size), -1, NULL);
713 s32 fadump_setup_cpu_notes_buf(u32 num_cpus)
715 /* Allocate buffer to hold cpu crash notes. */
716 fw_dump.cpu_notes_buf_size = num_cpus * sizeof(note_buf_t);
717 fw_dump.cpu_notes_buf_size = PAGE_ALIGN(fw_dump.cpu_notes_buf_size);
718 fw_dump.cpu_notes_buf_vaddr =
719 (unsigned long)fadump_alloc_buffer(fw_dump.cpu_notes_buf_size);
720 if (!fw_dump.cpu_notes_buf_vaddr) {
721 pr_err("Failed to allocate %ld bytes for CPU notes buffer\n",
722 fw_dump.cpu_notes_buf_size);
723 return -ENOMEM;
726 pr_debug("Allocated buffer for cpu notes of size %ld at 0x%lx\n",
727 fw_dump.cpu_notes_buf_size,
728 fw_dump.cpu_notes_buf_vaddr);
729 return 0;
732 void fadump_free_cpu_notes_buf(void)
734 if (!fw_dump.cpu_notes_buf_vaddr)
735 return;
737 fadump_free_buffer(fw_dump.cpu_notes_buf_vaddr,
738 fw_dump.cpu_notes_buf_size);
739 fw_dump.cpu_notes_buf_vaddr = 0;
740 fw_dump.cpu_notes_buf_size = 0;
743 static void fadump_free_mem_ranges(struct fadump_mrange_info *mrange_info)
745 if (mrange_info->is_static) {
746 mrange_info->mem_range_cnt = 0;
747 return;
750 kfree(mrange_info->mem_ranges);
751 memset((void *)((u64)mrange_info + RNG_NAME_SZ), 0,
752 (sizeof(struct fadump_mrange_info) - RNG_NAME_SZ));
756 * Allocate or reallocate mem_ranges array in incremental units
757 * of PAGE_SIZE.
759 static int fadump_alloc_mem_ranges(struct fadump_mrange_info *mrange_info)
761 struct fadump_memory_range *new_array;
762 u64 new_size;
764 new_size = mrange_info->mem_ranges_sz + PAGE_SIZE;
765 pr_debug("Allocating %llu bytes of memory for %s memory ranges\n",
766 new_size, mrange_info->name);
768 new_array = krealloc(mrange_info->mem_ranges, new_size, GFP_KERNEL);
769 if (new_array == NULL) {
770 pr_err("Insufficient memory for setting up %s memory ranges\n",
771 mrange_info->name);
772 fadump_free_mem_ranges(mrange_info);
773 return -ENOMEM;
776 mrange_info->mem_ranges = new_array;
777 mrange_info->mem_ranges_sz = new_size;
778 mrange_info->max_mem_ranges = (new_size /
779 sizeof(struct fadump_memory_range));
780 return 0;
783 static inline int fadump_add_mem_range(struct fadump_mrange_info *mrange_info,
784 u64 base, u64 end)
786 struct fadump_memory_range *mem_ranges = mrange_info->mem_ranges;
787 bool is_adjacent = false;
788 u64 start, size;
790 if (base == end)
791 return 0;
794 * Fold adjacent memory ranges to bring down the memory ranges/
795 * PT_LOAD segments count.
797 if (mrange_info->mem_range_cnt) {
798 start = mem_ranges[mrange_info->mem_range_cnt - 1].base;
799 size = mem_ranges[mrange_info->mem_range_cnt - 1].size;
801 if ((start + size) == base)
802 is_adjacent = true;
804 if (!is_adjacent) {
805 /* resize the array on reaching the limit */
806 if (mrange_info->mem_range_cnt == mrange_info->max_mem_ranges) {
807 int ret;
809 if (mrange_info->is_static) {
810 pr_err("Reached array size limit for %s memory ranges\n",
811 mrange_info->name);
812 return -ENOSPC;
815 ret = fadump_alloc_mem_ranges(mrange_info);
816 if (ret)
817 return ret;
819 /* Update to the new resized array */
820 mem_ranges = mrange_info->mem_ranges;
823 start = base;
824 mem_ranges[mrange_info->mem_range_cnt].base = start;
825 mrange_info->mem_range_cnt++;
828 mem_ranges[mrange_info->mem_range_cnt - 1].size = (end - start);
829 pr_debug("%s_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n",
830 mrange_info->name, (mrange_info->mem_range_cnt - 1),
831 start, end - 1, (end - start));
832 return 0;
835 static int fadump_exclude_reserved_area(u64 start, u64 end)
837 u64 ra_start, ra_end;
838 int ret = 0;
840 ra_start = fw_dump.reserve_dump_area_start;
841 ra_end = ra_start + fw_dump.reserve_dump_area_size;
843 if ((ra_start < end) && (ra_end > start)) {
844 if ((start < ra_start) && (end > ra_end)) {
845 ret = fadump_add_mem_range(&crash_mrange_info,
846 start, ra_start);
847 if (ret)
848 return ret;
850 ret = fadump_add_mem_range(&crash_mrange_info,
851 ra_end, end);
852 } else if (start < ra_start) {
853 ret = fadump_add_mem_range(&crash_mrange_info,
854 start, ra_start);
855 } else if (ra_end < end) {
856 ret = fadump_add_mem_range(&crash_mrange_info,
857 ra_end, end);
859 } else
860 ret = fadump_add_mem_range(&crash_mrange_info, start, end);
862 return ret;
865 static int fadump_init_elfcore_header(char *bufp)
867 struct elfhdr *elf;
869 elf = (struct elfhdr *) bufp;
870 bufp += sizeof(struct elfhdr);
871 memcpy(elf->e_ident, ELFMAG, SELFMAG);
872 elf->e_ident[EI_CLASS] = ELF_CLASS;
873 elf->e_ident[EI_DATA] = ELF_DATA;
874 elf->e_ident[EI_VERSION] = EV_CURRENT;
875 elf->e_ident[EI_OSABI] = ELF_OSABI;
876 memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
877 elf->e_type = ET_CORE;
878 elf->e_machine = ELF_ARCH;
879 elf->e_version = EV_CURRENT;
880 elf->e_entry = 0;
881 elf->e_phoff = sizeof(struct elfhdr);
882 elf->e_shoff = 0;
883 #if defined(_CALL_ELF)
884 elf->e_flags = _CALL_ELF;
885 #else
886 elf->e_flags = 0;
887 #endif
888 elf->e_ehsize = sizeof(struct elfhdr);
889 elf->e_phentsize = sizeof(struct elf_phdr);
890 elf->e_phnum = 0;
891 elf->e_shentsize = 0;
892 elf->e_shnum = 0;
893 elf->e_shstrndx = 0;
895 return 0;
899 * Traverse through memblock structure and setup crash memory ranges. These
900 * ranges will be used create PT_LOAD program headers in elfcore header.
902 static int fadump_setup_crash_memory_ranges(void)
904 struct memblock_region *reg;
905 u64 start, end;
906 int i, ret;
908 pr_debug("Setup crash memory ranges.\n");
909 crash_mrange_info.mem_range_cnt = 0;
912 * Boot memory region(s) registered with firmware are moved to
913 * different location at the time of crash. Create separate program
914 * header(s) for this memory chunk(s) with the correct offset.
916 for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) {
917 start = fw_dump.boot_mem_addr[i];
918 end = start + fw_dump.boot_mem_sz[i];
919 ret = fadump_add_mem_range(&crash_mrange_info, start, end);
920 if (ret)
921 return ret;
924 for_each_memblock(memory, reg) {
925 start = (u64)reg->base;
926 end = start + (u64)reg->size;
929 * skip the memory chunk that is already added
930 * (0 through boot_memory_top).
932 if (start < fw_dump.boot_mem_top) {
933 if (end > fw_dump.boot_mem_top)
934 start = fw_dump.boot_mem_top;
935 else
936 continue;
939 /* add this range excluding the reserved dump area. */
940 ret = fadump_exclude_reserved_area(start, end);
941 if (ret)
942 return ret;
945 return 0;
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.
953 static inline unsigned long fadump_relocate(unsigned long paddr)
955 unsigned long raddr, rstart, rend, rlast, hole_size;
956 int i;
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);
966 if (paddr >= rstart && paddr < rend) {
967 raddr += fw_dump.boot_mem_dest_addr - hole_size;
968 break;
971 rlast = rend;
974 pr_debug("vmcoreinfo: paddr = 0x%lx, raddr = 0x%lx\n", paddr, raddr);
975 return raddr;
978 static int fadump_create_elfcore_headers(char *bufp)
980 unsigned long long raddr, offset;
981 struct elf_phdr *phdr;
982 struct elfhdr *elf;
983 int i, j;
985 fadump_init_elfcore_header(bufp);
986 elf = (struct elfhdr *)bufp;
987 bufp += sizeof(struct elfhdr);
990 * setup ELF PT_NOTE, place holder for cpu notes info. The notes info
991 * will be populated during second kernel boot after crash. Hence
992 * this PT_NOTE will always be the first elf note.
994 * NOTE: Any new ELF note addition should be placed after this note.
996 phdr = (struct elf_phdr *)bufp;
997 bufp += sizeof(struct elf_phdr);
998 phdr->p_type = PT_NOTE;
999 phdr->p_flags = 0;
1000 phdr->p_vaddr = 0;
1001 phdr->p_align = 0;
1003 phdr->p_offset = 0;
1004 phdr->p_paddr = 0;
1005 phdr->p_filesz = 0;
1006 phdr->p_memsz = 0;
1008 (elf->e_phnum)++;
1010 /* setup ELF PT_NOTE for vmcoreinfo */
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;
1018 phdr->p_paddr = fadump_relocate(paddr_vmcoreinfo_note());
1019 phdr->p_offset = phdr->p_paddr;
1020 phdr->p_memsz = phdr->p_filesz = VMCOREINFO_NOTE_SIZE;
1022 /* Increment number of program headers. */
1023 (elf->e_phnum)++;
1025 /* setup PT_LOAD sections. */
1026 j = 0;
1027 offset = 0;
1028 raddr = fw_dump.boot_mem_addr[0];
1029 for (i = 0; i < crash_mrange_info.mem_range_cnt; i++) {
1030 u64 mbase, msize;
1032 mbase = crash_mrange_info.mem_ranges[i].base;
1033 msize = crash_mrange_info.mem_ranges[i].size;
1034 if (!msize)
1035 continue;
1037 phdr = (struct elf_phdr *)bufp;
1038 bufp += sizeof(struct elf_phdr);
1039 phdr->p_type = PT_LOAD;
1040 phdr->p_flags = PF_R|PF_W|PF_X;
1041 phdr->p_offset = mbase;
1043 if (mbase == raddr) {
1045 * The entire real memory region will be moved by
1046 * firmware to the specified destination_address.
1047 * Hence set the correct offset.
1049 phdr->p_offset = fw_dump.boot_mem_dest_addr + offset;
1050 if (j < (fw_dump.boot_mem_regs_cnt - 1)) {
1051 offset += fw_dump.boot_mem_sz[j];
1052 raddr = fw_dump.boot_mem_addr[++j];
1056 phdr->p_paddr = mbase;
1057 phdr->p_vaddr = (unsigned long)__va(mbase);
1058 phdr->p_filesz = msize;
1059 phdr->p_memsz = msize;
1060 phdr->p_align = 0;
1062 /* Increment number of program headers. */
1063 (elf->e_phnum)++;
1065 return 0;
1068 static unsigned long init_fadump_header(unsigned long addr)
1070 struct fadump_crash_info_header *fdh;
1072 if (!addr)
1073 return 0;
1075 fdh = __va(addr);
1076 addr += sizeof(struct fadump_crash_info_header);
1078 memset(fdh, 0, sizeof(struct fadump_crash_info_header));
1079 fdh->magic_number = FADUMP_CRASH_INFO_MAGIC;
1080 fdh->elfcorehdr_addr = addr;
1081 /* We will set the crashing cpu id in crash_fadump() during crash. */
1082 fdh->crashing_cpu = FADUMP_CPU_UNKNOWN;
1084 return addr;
1087 static int register_fadump(void)
1089 unsigned long addr;
1090 void *vaddr;
1091 int ret;
1094 * If no memory is reserved then we can not register for firmware-
1095 * assisted dump.
1097 if (!fw_dump.reserve_dump_area_size)
1098 return -ENODEV;
1100 ret = fadump_setup_crash_memory_ranges();
1101 if (ret)
1102 return ret;
1104 addr = fw_dump.fadumphdr_addr;
1106 /* Initialize fadump crash info header. */
1107 addr = init_fadump_header(addr);
1108 vaddr = __va(addr);
1110 pr_debug("Creating ELF core headers at %#016lx\n", addr);
1111 fadump_create_elfcore_headers(vaddr);
1113 /* register the future kernel dump with firmware. */
1114 pr_debug("Registering for firmware-assisted kernel dump...\n");
1115 return fw_dump.ops->fadump_register(&fw_dump);
1118 void fadump_cleanup(void)
1120 if (!fw_dump.fadump_supported)
1121 return;
1123 /* Invalidate the registration only if dump is active. */
1124 if (fw_dump.dump_active) {
1125 pr_debug("Invalidating firmware-assisted dump registration\n");
1126 fw_dump.ops->fadump_invalidate(&fw_dump);
1127 } else if (fw_dump.dump_registered) {
1128 /* Un-register Firmware-assisted dump if it was registered. */
1129 fw_dump.ops->fadump_unregister(&fw_dump);
1130 fadump_free_mem_ranges(&crash_mrange_info);
1133 if (fw_dump.ops->fadump_cleanup)
1134 fw_dump.ops->fadump_cleanup(&fw_dump);
1137 static void fadump_free_reserved_memory(unsigned long start_pfn,
1138 unsigned long end_pfn)
1140 unsigned long pfn;
1141 unsigned long time_limit = jiffies + HZ;
1143 pr_info("freeing reserved memory (0x%llx - 0x%llx)\n",
1144 PFN_PHYS(start_pfn), PFN_PHYS(end_pfn));
1146 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
1147 free_reserved_page(pfn_to_page(pfn));
1149 if (time_after(jiffies, time_limit)) {
1150 cond_resched();
1151 time_limit = jiffies + HZ;
1157 * Skip memory holes and free memory that was actually reserved.
1159 static void fadump_release_reserved_area(u64 start, u64 end)
1161 u64 tstart, tend, spfn, epfn;
1162 struct memblock_region *reg;
1164 spfn = PHYS_PFN(start);
1165 epfn = PHYS_PFN(end);
1166 for_each_memblock(memory, reg) {
1167 tstart = max_t(u64, spfn, memblock_region_memory_base_pfn(reg));
1168 tend = min_t(u64, epfn, memblock_region_memory_end_pfn(reg));
1169 if (tstart < tend) {
1170 fadump_free_reserved_memory(tstart, tend);
1172 if (tend == epfn)
1173 break;
1175 spfn = tend;
1181 * Sort the mem ranges in-place and merge adjacent ranges
1182 * to minimize the memory ranges count.
1184 static void sort_and_merge_mem_ranges(struct fadump_mrange_info *mrange_info)
1186 struct fadump_memory_range *mem_ranges;
1187 struct fadump_memory_range tmp_range;
1188 u64 base, size;
1189 int i, j, idx;
1191 if (!reserved_mrange_info.mem_range_cnt)
1192 return;
1194 /* Sort the memory ranges */
1195 mem_ranges = mrange_info->mem_ranges;
1196 for (i = 0; i < mrange_info->mem_range_cnt; i++) {
1197 idx = i;
1198 for (j = (i + 1); j < mrange_info->mem_range_cnt; j++) {
1199 if (mem_ranges[idx].base > mem_ranges[j].base)
1200 idx = j;
1202 if (idx != i) {
1203 tmp_range = mem_ranges[idx];
1204 mem_ranges[idx] = mem_ranges[i];
1205 mem_ranges[i] = tmp_range;
1209 /* Merge adjacent reserved ranges */
1210 idx = 0;
1211 for (i = 1; i < mrange_info->mem_range_cnt; i++) {
1212 base = mem_ranges[i-1].base;
1213 size = mem_ranges[i-1].size;
1214 if (mem_ranges[i].base == (base + size))
1215 mem_ranges[idx].size += mem_ranges[i].size;
1216 else {
1217 idx++;
1218 if (i == idx)
1219 continue;
1221 mem_ranges[idx] = mem_ranges[i];
1224 mrange_info->mem_range_cnt = idx + 1;
1228 * Scan reserved-ranges to consider them while reserving/releasing
1229 * memory for FADump.
1231 static void __init early_init_dt_scan_reserved_ranges(unsigned long node)
1233 const __be32 *prop;
1234 int len, ret = -1;
1235 unsigned long i;
1237 /* reserved-ranges already scanned */
1238 if (reserved_mrange_info.mem_range_cnt != 0)
1239 return;
1241 prop = of_get_flat_dt_prop(node, "reserved-ranges", &len);
1242 if (!prop)
1243 return;
1246 * Each reserved range is an (address,size) pair, 2 cells each,
1247 * totalling 4 cells per range.
1249 for (i = 0; i < len / (sizeof(*prop) * 4); i++) {
1250 u64 base, size;
1252 base = of_read_number(prop + (i * 4) + 0, 2);
1253 size = of_read_number(prop + (i * 4) + 2, 2);
1255 if (size) {
1256 ret = fadump_add_mem_range(&reserved_mrange_info,
1257 base, base + size);
1258 if (ret < 0) {
1259 pr_warn("some reserved ranges are ignored!\n");
1260 break;
1265 /* Compact reserved ranges */
1266 sort_and_merge_mem_ranges(&reserved_mrange_info);
1270 * Release the memory that was reserved during early boot to preserve the
1271 * crash'ed kernel's memory contents except reserved dump area (permanent
1272 * reservation) and reserved ranges used by F/W. The released memory will
1273 * be available for general use.
1275 static void fadump_release_memory(u64 begin, u64 end)
1277 u64 ra_start, ra_end, tstart;
1278 int i, ret;
1280 ra_start = fw_dump.reserve_dump_area_start;
1281 ra_end = ra_start + fw_dump.reserve_dump_area_size;
1284 * If reserved ranges array limit is hit, overwrite the last reserved
1285 * memory range with reserved dump area to ensure it is excluded from
1286 * the memory being released (reused for next FADump registration).
1288 if (reserved_mrange_info.mem_range_cnt ==
1289 reserved_mrange_info.max_mem_ranges)
1290 reserved_mrange_info.mem_range_cnt--;
1292 ret = fadump_add_mem_range(&reserved_mrange_info, ra_start, ra_end);
1293 if (ret != 0)
1294 return;
1296 /* Get the reserved ranges list in order first. */
1297 sort_and_merge_mem_ranges(&reserved_mrange_info);
1299 /* Exclude reserved ranges and release remaining memory */
1300 tstart = begin;
1301 for (i = 0; i < reserved_mrange_info.mem_range_cnt; i++) {
1302 ra_start = reserved_mrange_info.mem_ranges[i].base;
1303 ra_end = ra_start + reserved_mrange_info.mem_ranges[i].size;
1305 if (tstart >= ra_end)
1306 continue;
1308 if (tstart < ra_start)
1309 fadump_release_reserved_area(tstart, ra_start);
1310 tstart = ra_end;
1313 if (tstart < end)
1314 fadump_release_reserved_area(tstart, end);
1317 static void fadump_invalidate_release_mem(void)
1319 mutex_lock(&fadump_mutex);
1320 if (!fw_dump.dump_active) {
1321 mutex_unlock(&fadump_mutex);
1322 return;
1325 fadump_cleanup();
1326 mutex_unlock(&fadump_mutex);
1328 fadump_release_memory(fw_dump.boot_mem_top, memblock_end_of_DRAM());
1329 fadump_free_cpu_notes_buf();
1332 * Setup kernel metadata and initialize the kernel dump
1333 * memory structure for FADump re-registration.
1335 if (fw_dump.ops->fadump_setup_metadata &&
1336 (fw_dump.ops->fadump_setup_metadata(&fw_dump) < 0))
1337 pr_warn("Failed to setup kernel metadata!\n");
1338 fw_dump.ops->fadump_init_mem_struct(&fw_dump);
1341 static ssize_t release_mem_store(struct kobject *kobj,
1342 struct kobj_attribute *attr,
1343 const char *buf, size_t count)
1345 int input = -1;
1347 if (!fw_dump.dump_active)
1348 return -EPERM;
1350 if (kstrtoint(buf, 0, &input))
1351 return -EINVAL;
1353 if (input == 1) {
1355 * Take away the '/proc/vmcore'. We are releasing the dump
1356 * memory, hence it will not be valid anymore.
1358 #ifdef CONFIG_PROC_VMCORE
1359 vmcore_cleanup();
1360 #endif
1361 fadump_invalidate_release_mem();
1363 } else
1364 return -EINVAL;
1365 return count;
1368 /* Release the reserved memory and disable the FADump */
1369 static void unregister_fadump(void)
1371 fadump_cleanup();
1372 fadump_release_memory(fw_dump.reserve_dump_area_start,
1373 fw_dump.reserve_dump_area_size);
1374 fw_dump.fadump_enabled = 0;
1375 kobject_put(fadump_kobj);
1378 static ssize_t enabled_show(struct kobject *kobj,
1379 struct kobj_attribute *attr,
1380 char *buf)
1382 return sprintf(buf, "%d\n", fw_dump.fadump_enabled);
1385 static ssize_t mem_reserved_show(struct kobject *kobj,
1386 struct kobj_attribute *attr,
1387 char *buf)
1389 return sprintf(buf, "%ld\n", fw_dump.reserve_dump_area_size);
1392 static ssize_t registered_show(struct kobject *kobj,
1393 struct kobj_attribute *attr,
1394 char *buf)
1396 return sprintf(buf, "%d\n", fw_dump.dump_registered);
1399 static ssize_t registered_store(struct kobject *kobj,
1400 struct kobj_attribute *attr,
1401 const char *buf, size_t count)
1403 int ret = 0;
1404 int input = -1;
1406 if (!fw_dump.fadump_enabled || fw_dump.dump_active)
1407 return -EPERM;
1409 if (kstrtoint(buf, 0, &input))
1410 return -EINVAL;
1412 mutex_lock(&fadump_mutex);
1414 switch (input) {
1415 case 0:
1416 if (fw_dump.dump_registered == 0) {
1417 goto unlock_out;
1420 /* Un-register Firmware-assisted dump */
1421 pr_debug("Un-register firmware-assisted dump\n");
1422 fw_dump.ops->fadump_unregister(&fw_dump);
1423 break;
1424 case 1:
1425 if (fw_dump.dump_registered == 1) {
1426 /* Un-register Firmware-assisted dump */
1427 fw_dump.ops->fadump_unregister(&fw_dump);
1429 /* Register Firmware-assisted dump */
1430 ret = register_fadump();
1431 break;
1432 default:
1433 ret = -EINVAL;
1434 break;
1437 unlock_out:
1438 mutex_unlock(&fadump_mutex);
1439 return ret < 0 ? ret : count;
1442 static int fadump_region_show(struct seq_file *m, void *private)
1444 if (!fw_dump.fadump_enabled)
1445 return 0;
1447 mutex_lock(&fadump_mutex);
1448 fw_dump.ops->fadump_region_show(&fw_dump, m);
1449 mutex_unlock(&fadump_mutex);
1450 return 0;
1453 static struct kobj_attribute release_attr = __ATTR_WO(release_mem);
1454 static struct kobj_attribute enable_attr = __ATTR_RO(enabled);
1455 static struct kobj_attribute register_attr = __ATTR_RW(registered);
1456 static struct kobj_attribute mem_reserved_attr = __ATTR_RO(mem_reserved);
1458 static struct attribute *fadump_attrs[] = {
1459 &enable_attr.attr,
1460 &register_attr.attr,
1461 &mem_reserved_attr.attr,
1462 NULL,
1465 ATTRIBUTE_GROUPS(fadump);
1467 DEFINE_SHOW_ATTRIBUTE(fadump_region);
1469 static void fadump_init_files(void)
1471 int rc = 0;
1473 fadump_kobj = kobject_create_and_add("fadump", kernel_kobj);
1474 if (!fadump_kobj) {
1475 pr_err("failed to create fadump kobject\n");
1476 return;
1479 debugfs_create_file("fadump_region", 0444, powerpc_debugfs_root, NULL,
1480 &fadump_region_fops);
1482 if (fw_dump.dump_active) {
1483 rc = sysfs_create_file(fadump_kobj, &release_attr.attr);
1484 if (rc)
1485 pr_err("unable to create release_mem sysfs file (%d)\n",
1486 rc);
1489 rc = sysfs_create_groups(fadump_kobj, fadump_groups);
1490 if (rc) {
1491 pr_err("sysfs group creation failed (%d), unregistering FADump",
1492 rc);
1493 unregister_fadump();
1494 return;
1498 * The FADump sysfs are moved from kernel_kobj to fadump_kobj need to
1499 * create symlink at old location to maintain backward compatibility.
1501 * - fadump_enabled -> fadump/enabled
1502 * - fadump_registered -> fadump/registered
1503 * - fadump_release_mem -> fadump/release_mem
1505 rc = compat_only_sysfs_link_entry_to_kobj(kernel_kobj, fadump_kobj,
1506 "enabled", "fadump_enabled");
1507 if (rc) {
1508 pr_err("unable to create fadump_enabled symlink (%d)", rc);
1509 return;
1512 rc = compat_only_sysfs_link_entry_to_kobj(kernel_kobj, fadump_kobj,
1513 "registered",
1514 "fadump_registered");
1515 if (rc) {
1516 pr_err("unable to create fadump_registered symlink (%d)", rc);
1517 sysfs_remove_link(kernel_kobj, "fadump_enabled");
1518 return;
1521 if (fw_dump.dump_active) {
1522 rc = compat_only_sysfs_link_entry_to_kobj(kernel_kobj,
1523 fadump_kobj,
1524 "release_mem",
1525 "fadump_release_mem");
1526 if (rc)
1527 pr_err("unable to create fadump_release_mem symlink (%d)",
1528 rc);
1530 return;
1534 * Prepare for firmware-assisted dump.
1536 int __init setup_fadump(void)
1538 if (!fw_dump.fadump_supported)
1539 return 0;
1541 fadump_init_files();
1542 fadump_show_config();
1544 if (!fw_dump.fadump_enabled)
1545 return 1;
1548 * If dump data is available then see if it is valid and prepare for
1549 * saving it to the disk.
1551 if (fw_dump.dump_active) {
1553 * if dump process fails then invalidate the registration
1554 * and release memory before proceeding for re-registration.
1556 if (fw_dump.ops->fadump_process(&fw_dump) < 0)
1557 fadump_invalidate_release_mem();
1559 /* Initialize the kernel dump memory structure for FAD registration. */
1560 else if (fw_dump.reserve_dump_area_size)
1561 fw_dump.ops->fadump_init_mem_struct(&fw_dump);
1563 return 1;
1565 subsys_initcall(setup_fadump);
1566 #else /* !CONFIG_PRESERVE_FA_DUMP */
1568 /* Scan the Firmware Assisted dump configuration details. */
1569 int __init early_init_dt_scan_fw_dump(unsigned long node, const char *uname,
1570 int depth, void *data)
1572 if ((depth != 1) || (strcmp(uname, "ibm,opal") != 0))
1573 return 0;
1575 opal_fadump_dt_scan(&fw_dump, node);
1576 return 1;
1580 * When dump is active but PRESERVE_FA_DUMP is enabled on the kernel,
1581 * preserve crash data. The subsequent memory preserving kernel boot
1582 * is likely to process this crash data.
1584 int __init fadump_reserve_mem(void)
1586 if (fw_dump.dump_active) {
1588 * If last boot has crashed then reserve all the memory
1589 * above boot memory to preserve crash data.
1591 pr_info("Preserving crash data for processing in next boot.\n");
1592 fadump_reserve_crash_area(fw_dump.boot_mem_top);
1593 } else
1594 pr_debug("FADump-aware kernel..\n");
1596 return 1;
1598 #endif /* CONFIG_PRESERVE_FA_DUMP */
1600 /* Preserve everything above the base address */
1601 static void __init fadump_reserve_crash_area(u64 base)
1603 struct memblock_region *reg;
1604 u64 mstart, msize;
1606 for_each_memblock(memory, reg) {
1607 mstart = reg->base;
1608 msize = reg->size;
1610 if ((mstart + msize) < base)
1611 continue;
1613 if (mstart < base) {
1614 msize -= (base - mstart);
1615 mstart = base;
1618 pr_info("Reserving %lluMB of memory at %#016llx for preserving crash data",
1619 (msize >> 20), mstart);
1620 memblock_reserve(mstart, msize);
1624 unsigned long __init arch_reserved_kernel_pages(void)
1626 return memblock_reserved_size() / PAGE_SIZE;