11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2eb39c880SMahesh Salgaonkar /*
3eb39c880SMahesh Salgaonkar * Firmware Assisted dump: A robust mechanism to get reliable kernel crash
4eb39c880SMahesh Salgaonkar * dump with assistance from firmware. This approach does not use kexec,
5eb39c880SMahesh Salgaonkar * instead firmware assists in booting the kdump kernel while preserving
6eb39c880SMahesh Salgaonkar * memory contents. The most of the code implementation has been adapted
7eb39c880SMahesh Salgaonkar * from phyp assisted dump implementation written by Linas Vepstas and
8eb39c880SMahesh Salgaonkar * Manish Ahuja
9eb39c880SMahesh Salgaonkar *
10eb39c880SMahesh Salgaonkar * Copyright 2011 IBM Corporation
11eb39c880SMahesh Salgaonkar * Author: Mahesh Salgaonkar <mahesh@linux.vnet.ibm.com>
12eb39c880SMahesh Salgaonkar */
13eb39c880SMahesh Salgaonkar
14eb39c880SMahesh Salgaonkar #undef DEBUG
15eb39c880SMahesh Salgaonkar #define pr_fmt(fmt) "fadump: " fmt
16eb39c880SMahesh Salgaonkar
17eb39c880SMahesh Salgaonkar #include <linux/string.h>
18eb39c880SMahesh Salgaonkar #include <linux/memblock.h>
193ccc00a7SMahesh Salgaonkar #include <linux/delay.h>
203ccc00a7SMahesh Salgaonkar #include <linux/seq_file.h>
212df173d9SMahesh Salgaonkar #include <linux/crash_dump.h>
22b500afffSMahesh Salgaonkar #include <linux/kobject.h>
23b500afffSMahesh Salgaonkar #include <linux/sysfs.h>
24a5818313SHari Bathini #include <linux/slab.h>
25a4e92ce8SMahesh Salgaonkar #include <linux/cma.h>
2645d0ba52SChristophe Leroy #include <linux/hugetlb.h>
27dbf77fedSAneesh Kumar K.V #include <linux/debugfs.h>
28e6f6390aSChristophe Leroy #include <linux/of.h>
29e6f6390aSChristophe Leroy #include <linux/of_fdt.h>
30eb39c880SMahesh Salgaonkar
31eb39c880SMahesh Salgaonkar #include <asm/page.h>
32eb39c880SMahesh Salgaonkar #include <asm/fadump.h>
33ca986d7fSHari Bathini #include <asm/fadump-internal.h>
34cad3c834SStephen Rothwell #include <asm/setup.h>
35cbd3d5baSMichael Ellerman #include <asm/interrupt.h>
36eb39c880SMahesh Salgaonkar
37ba608c4fSSourabh Jain /*
38ba608c4fSSourabh Jain * The CPU who acquired the lock to trigger the fadump crash should
39ba608c4fSSourabh Jain * wait for other CPUs to enter.
40ba608c4fSSourabh Jain *
41ba608c4fSSourabh Jain * The timeout is in milliseconds.
42ba608c4fSSourabh Jain */
43ba608c4fSSourabh Jain #define CRASH_TIMEOUT 500
44ba608c4fSSourabh Jain
45eb39c880SMahesh Salgaonkar static struct fw_dump fw_dump;
463ccc00a7SMahesh Salgaonkar
47b2a815a5SHari Bathini static void __init fadump_reserve_crash_area(u64 base);
48b2a815a5SHari Bathini
49bec53196SHari Bathini #ifndef CONFIG_PRESERVE_FA_DUMP
505f987caeSMichael Ellerman
512e341f56SMichael Ellerman static struct kobject *fadump_kobj;
522e341f56SMichael Ellerman
535f987caeSMichael Ellerman static atomic_t cpus_in_fadump;
543ccc00a7SMahesh Salgaonkar static DEFINE_MUTEX(fadump_mutex);
555f987caeSMichael Ellerman
562e341f56SMichael Ellerman static struct fadump_mrange_info crash_mrange_info = { "crash", NULL, 0, 0, 0, false };
5702c04e37SHari Bathini
5802c04e37SHari Bathini #define RESERVED_RNGS_SZ 16384 /* 16K - 128 entries */
5902c04e37SHari Bathini #define RESERVED_RNGS_CNT (RESERVED_RNGS_SZ / \
6002c04e37SHari Bathini sizeof(struct fadump_memory_range))
6102c04e37SHari Bathini static struct fadump_memory_range rngs[RESERVED_RNGS_CNT];
622e341f56SMichael Ellerman static struct fadump_mrange_info
632e341f56SMichael Ellerman reserved_mrange_info = { "reserved", rngs, RESERVED_RNGS_SZ, 0, RESERVED_RNGS_CNT, true };
6402c04e37SHari Bathini
6502c04e37SHari Bathini static void __init early_init_dt_scan_reserved_ranges(unsigned long node);
66eb39c880SMahesh Salgaonkar
67a4e92ce8SMahesh Salgaonkar #ifdef CONFIG_CMA
680226e552SHari Bathini static struct cma *fadump_cma;
690226e552SHari Bathini
70a4e92ce8SMahesh Salgaonkar /*
71a4e92ce8SMahesh Salgaonkar * fadump_cma_init() - Initialize CMA area from a fadump reserved memory
72a4e92ce8SMahesh Salgaonkar *
73a4e92ce8SMahesh Salgaonkar * This function initializes CMA area from fadump reserved memory.
74a4e92ce8SMahesh Salgaonkar * The total size of fadump reserved memory covers for boot memory size
75a4e92ce8SMahesh Salgaonkar * + cpu data size + hpte size and metadata.
76a4e92ce8SMahesh Salgaonkar * Initialize only the area equivalent to boot memory size for CMA use.
77887f56a0SRandy Dunlap * The remaining portion of fadump reserved memory will be not given
78887f56a0SRandy Dunlap * to CMA and pages for those will stay reserved. boot memory size is
79a4e92ce8SMahesh Salgaonkar * aligned per CMA requirement to satisy cma_init_reserved_mem() call.
80a4e92ce8SMahesh Salgaonkar * But for some reason even if it fails we still have the memory reservation
81a4e92ce8SMahesh Salgaonkar * with us and we can still continue doing fadump.
82a4e92ce8SMahesh Salgaonkar */
fadump_cma_init(void)83*c5c1d1efSRitesh Harjani (IBM) void __init fadump_cma_init(void)
84a4e92ce8SMahesh Salgaonkar {
85a4e92ce8SMahesh Salgaonkar unsigned long long base, size;
86a4e92ce8SMahesh Salgaonkar int rc;
87a4e92ce8SMahesh Salgaonkar
8892f7cc84SRitesh Harjani (IBM) if (!fw_dump.fadump_supported || !fw_dump.fadump_enabled ||
8992f7cc84SRitesh Harjani (IBM) fw_dump.dump_active)
9092f7cc84SRitesh Harjani (IBM) return;
91a4e92ce8SMahesh Salgaonkar /*
92a4e92ce8SMahesh Salgaonkar * Do not use CMA if user has provided fadump=nocma kernel parameter.
93a4e92ce8SMahesh Salgaonkar */
9492f7cc84SRitesh Harjani (IBM) if (fw_dump.nocma || !fw_dump.boot_memory_size)
9592f7cc84SRitesh Harjani (IBM) return;
96a4e92ce8SMahesh Salgaonkar
97a4e92ce8SMahesh Salgaonkar base = fw_dump.reserve_dump_area_start;
98a4e92ce8SMahesh Salgaonkar size = fw_dump.boot_memory_size;
99a4e92ce8SMahesh Salgaonkar
100a4e92ce8SMahesh Salgaonkar rc = cma_init_reserved_mem(base, size, 0, "fadump_cma", &fadump_cma);
101a4e92ce8SMahesh Salgaonkar if (rc) {
102a4e92ce8SMahesh Salgaonkar pr_err("Failed to init cma area for firmware-assisted dump,%d\n", rc);
103a4e92ce8SMahesh Salgaonkar /*
104a4e92ce8SMahesh Salgaonkar * Though the CMA init has failed we still have memory
105a4e92ce8SMahesh Salgaonkar * reservation with us. The reserved memory will be
106a4e92ce8SMahesh Salgaonkar * blocked from production system usage. Hence return 1,
107a4e92ce8SMahesh Salgaonkar * so that we can continue with fadump.
108a4e92ce8SMahesh Salgaonkar */
10992f7cc84SRitesh Harjani (IBM) return;
110a4e92ce8SMahesh Salgaonkar }
111a4e92ce8SMahesh Salgaonkar
112a4e92ce8SMahesh Salgaonkar /*
113ee97347fSHari Bathini * If CMA activation fails, keep the pages reserved, instead of
114ee97347fSHari Bathini * exposing them to buddy allocator. Same as 'fadump=nocma' case.
115ee97347fSHari Bathini */
116ee97347fSHari Bathini cma_reserve_pages_on_error(fadump_cma);
117ee97347fSHari Bathini
118ee97347fSHari Bathini /*
119a4e92ce8SMahesh Salgaonkar * So we now have successfully initialized cma area for fadump.
120a4e92ce8SMahesh Salgaonkar */
121a4e92ce8SMahesh Salgaonkar pr_info("Initialized 0x%lx bytes cma area at %ldMB from 0x%lx "
122a4e92ce8SMahesh Salgaonkar "bytes of memory reserved for firmware-assisted dump\n",
123a4e92ce8SMahesh Salgaonkar cma_get_size(fadump_cma),
124a4e92ce8SMahesh Salgaonkar (unsigned long)cma_get_base(fadump_cma) >> 20,
125a4e92ce8SMahesh Salgaonkar fw_dump.reserve_dump_area_size);
126a4e92ce8SMahesh Salgaonkar }
127a4e92ce8SMahesh Salgaonkar #endif /* CONFIG_CMA */
128a4e92ce8SMahesh Salgaonkar
129eb39c880SMahesh Salgaonkar /* Scan the Firmware Assisted dump configuration details. */
early_init_dt_scan_fw_dump(unsigned long node,const char * uname,int depth,void * data)130f3512011SHari Bathini int __init early_init_dt_scan_fw_dump(unsigned long node, const char *uname,
131f3512011SHari Bathini int depth, void *data)
132eb39c880SMahesh Salgaonkar {
13302c04e37SHari Bathini if (depth == 0) {
13402c04e37SHari Bathini early_init_dt_scan_reserved_ranges(node);
13502c04e37SHari Bathini return 0;
13602c04e37SHari Bathini }
13702c04e37SHari Bathini
13841df5928SHari Bathini if (depth != 1)
139eb39c880SMahesh Salgaonkar return 0;
140eb39c880SMahesh Salgaonkar
14141df5928SHari Bathini if (strcmp(uname, "rtas") == 0) {
142d3833a70SHari Bathini rtas_fadump_dt_scan(&fw_dump, node);
143eb39c880SMahesh Salgaonkar return 1;
144eb39c880SMahesh Salgaonkar }
145eb39c880SMahesh Salgaonkar
14641df5928SHari Bathini if (strcmp(uname, "ibm,opal") == 0) {
14741df5928SHari Bathini opal_fadump_dt_scan(&fw_dump, node);
14841df5928SHari Bathini return 1;
14941df5928SHari Bathini }
15041df5928SHari Bathini
15141df5928SHari Bathini return 0;
15241df5928SHari Bathini }
15341df5928SHari Bathini
154eae0dfccSHari Bathini /*
155eae0dfccSHari Bathini * If fadump is registered, check if the memory provided
1560db6896fSMahesh Salgaonkar * falls within boot memory area and reserved memory area.
157eae0dfccSHari Bathini */
is_fadump_memory_area(u64 addr,unsigned long size)158becd91d9SHari Bathini int is_fadump_memory_area(u64 addr, unsigned long size)
159eae0dfccSHari Bathini {
160becd91d9SHari Bathini u64 d_start, d_end;
1610db6896fSMahesh Salgaonkar
162eae0dfccSHari Bathini if (!fw_dump.dump_registered)
163eae0dfccSHari Bathini return 0;
164eae0dfccSHari Bathini
165becd91d9SHari Bathini if (!size)
166becd91d9SHari Bathini return 0;
167becd91d9SHari Bathini
168becd91d9SHari Bathini d_start = fw_dump.reserve_dump_area_start;
169becd91d9SHari Bathini d_end = d_start + fw_dump.reserve_dump_area_size;
1700db6896fSMahesh Salgaonkar if (((addr + size) > d_start) && (addr <= d_end))
1710db6896fSMahesh Salgaonkar return 1;
1720db6896fSMahesh Salgaonkar
1737dee93a9SHari Bathini return (addr <= fw_dump.boot_mem_top);
174eae0dfccSHari Bathini }
175eae0dfccSHari Bathini
should_fadump_crash(void)1766fcd6baaSNicholas Piggin int should_fadump_crash(void)
1776fcd6baaSNicholas Piggin {
1786fcd6baaSNicholas Piggin if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr)
1796fcd6baaSNicholas Piggin return 0;
1806fcd6baaSNicholas Piggin return 1;
1816fcd6baaSNicholas Piggin }
1826fcd6baaSNicholas Piggin
is_fadump_active(void)1833ccc00a7SMahesh Salgaonkar int is_fadump_active(void)
1843ccc00a7SMahesh Salgaonkar {
1853ccc00a7SMahesh Salgaonkar return fw_dump.dump_active;
1863ccc00a7SMahesh Salgaonkar }
1873ccc00a7SMahesh Salgaonkar
188a5a05b91SHari Bathini /*
189961cf26aSHari Bathini * Returns true, if there are no holes in memory area between d_start to d_end,
190961cf26aSHari Bathini * false otherwise.
191a5a05b91SHari Bathini */
is_fadump_mem_area_contiguous(u64 d_start,u64 d_end)192961cf26aSHari Bathini static bool is_fadump_mem_area_contiguous(u64 d_start, u64 d_end)
193a5a05b91SHari Bathini {
194b10d6bcaSMike Rapoport phys_addr_t reg_start, reg_end;
195961cf26aSHari Bathini bool ret = false;
196b10d6bcaSMike Rapoport u64 i, start, end;
197a5a05b91SHari Bathini
198b10d6bcaSMike Rapoport for_each_mem_range(i, ®_start, ®_end) {
199b10d6bcaSMike Rapoport start = max_t(u64, d_start, reg_start);
200b10d6bcaSMike Rapoport end = min_t(u64, d_end, reg_end);
201961cf26aSHari Bathini if (d_start < end) {
202961cf26aSHari Bathini /* Memory hole from d_start to start */
203961cf26aSHari Bathini if (start > d_start)
204a5a05b91SHari Bathini break;
205a5a05b91SHari Bathini
206961cf26aSHari Bathini if (end == d_end) {
207961cf26aSHari Bathini ret = true;
208a5a05b91SHari Bathini break;
209a5a05b91SHari Bathini }
210a5a05b91SHari Bathini
211961cf26aSHari Bathini d_start = end + 1;
212a5a05b91SHari Bathini }
213a5a05b91SHari Bathini }
214a5a05b91SHari Bathini
215a5a05b91SHari Bathini return ret;
216a5a05b91SHari Bathini }
217a5a05b91SHari Bathini
218f86593beSMahesh Salgaonkar /*
219961cf26aSHari Bathini * Returns true, if there are no holes in boot memory area,
220961cf26aSHari Bathini * false otherwise.
221961cf26aSHari Bathini */
is_fadump_boot_mem_contiguous(void)2227f0ad11dSHari Bathini bool is_fadump_boot_mem_contiguous(void)
223961cf26aSHari Bathini {
2247dee93a9SHari Bathini unsigned long d_start, d_end;
2257dee93a9SHari Bathini bool ret = false;
2267dee93a9SHari Bathini int i;
2277dee93a9SHari Bathini
2287dee93a9SHari Bathini for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) {
2297dee93a9SHari Bathini d_start = fw_dump.boot_mem_addr[i];
2307dee93a9SHari Bathini d_end = d_start + fw_dump.boot_mem_sz[i];
2317dee93a9SHari Bathini
2327dee93a9SHari Bathini ret = is_fadump_mem_area_contiguous(d_start, d_end);
2337dee93a9SHari Bathini if (!ret)
2347dee93a9SHari Bathini break;
2357dee93a9SHari Bathini }
2367dee93a9SHari Bathini
2377dee93a9SHari Bathini return ret;
238961cf26aSHari Bathini }
239961cf26aSHari Bathini
240961cf26aSHari Bathini /*
241f86593beSMahesh Salgaonkar * Returns true, if there are no holes in reserved memory area,
242f86593beSMahesh Salgaonkar * false otherwise.
243f86593beSMahesh Salgaonkar */
is_fadump_reserved_mem_contiguous(void)2447f0ad11dSHari Bathini bool is_fadump_reserved_mem_contiguous(void)
245f86593beSMahesh Salgaonkar {
246961cf26aSHari Bathini u64 d_start, d_end;
247f86593beSMahesh Salgaonkar
248961cf26aSHari Bathini d_start = fw_dump.reserve_dump_area_start;
249961cf26aSHari Bathini d_end = d_start + fw_dump.reserve_dump_area_size;
250961cf26aSHari Bathini return is_fadump_mem_area_contiguous(d_start, d_end);
251f86593beSMahesh Salgaonkar }
252f86593beSMahesh Salgaonkar
2533ccc00a7SMahesh Salgaonkar /* Print firmware assisted dump configurations for debugging purpose. */
fadump_show_config(void)254d276960dSNick Child static void __init fadump_show_config(void)
2553ccc00a7SMahesh Salgaonkar {
2567dee93a9SHari Bathini int i;
2577dee93a9SHari Bathini
2583ccc00a7SMahesh Salgaonkar pr_debug("Support for firmware-assisted dump (fadump): %s\n",
2593ccc00a7SMahesh Salgaonkar (fw_dump.fadump_supported ? "present" : "no support"));
2603ccc00a7SMahesh Salgaonkar
2613ccc00a7SMahesh Salgaonkar if (!fw_dump.fadump_supported)
2623ccc00a7SMahesh Salgaonkar return;
2633ccc00a7SMahesh Salgaonkar
2643ccc00a7SMahesh Salgaonkar pr_debug("Fadump enabled : %s\n",
2653ccc00a7SMahesh Salgaonkar (fw_dump.fadump_enabled ? "yes" : "no"));
2663ccc00a7SMahesh Salgaonkar pr_debug("Dump Active : %s\n",
2673ccc00a7SMahesh Salgaonkar (fw_dump.dump_active ? "yes" : "no"));
2683ccc00a7SMahesh Salgaonkar pr_debug("Dump section sizes:\n");
2693ccc00a7SMahesh Salgaonkar pr_debug(" CPU state data size: %lx\n", fw_dump.cpu_state_data_size);
2703ccc00a7SMahesh Salgaonkar pr_debug(" HPTE region size : %lx\n", fw_dump.hpte_region_size);
2713ccc00a7SMahesh Salgaonkar pr_debug(" Boot memory size : %lx\n", fw_dump.boot_memory_size);
2727dee93a9SHari Bathini pr_debug(" Boot memory top : %llx\n", fw_dump.boot_mem_top);
2737dee93a9SHari Bathini pr_debug("Boot memory regions cnt: %llx\n", fw_dump.boot_mem_regs_cnt);
2747dee93a9SHari Bathini for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) {
2757dee93a9SHari Bathini pr_debug("[%03d] base = %llx, size = %llx\n", i,
2767dee93a9SHari Bathini fw_dump.boot_mem_addr[i], fw_dump.boot_mem_sz[i]);
2777dee93a9SHari Bathini }
2783ccc00a7SMahesh Salgaonkar }
2793ccc00a7SMahesh Salgaonkar
280eb39c880SMahesh Salgaonkar /**
281eb39c880SMahesh Salgaonkar * fadump_calculate_reserve_size(): reserve variable boot area 5% of System RAM
282eb39c880SMahesh Salgaonkar *
283eb39c880SMahesh Salgaonkar * Function to find the largest memory size we need to reserve during early
284eb39c880SMahesh Salgaonkar * boot process. This will be the size of the memory that is required for a
285eb39c880SMahesh Salgaonkar * kernel to boot successfully.
286eb39c880SMahesh Salgaonkar *
287eb39c880SMahesh Salgaonkar * This function has been taken from phyp-assisted dump feature implementation.
288eb39c880SMahesh Salgaonkar *
289eb39c880SMahesh Salgaonkar * returns larger of 256MB or 5% rounded down to multiples of 256MB.
290eb39c880SMahesh Salgaonkar *
291eb39c880SMahesh Salgaonkar * TODO: Come up with better approach to find out more accurate memory size
292eb39c880SMahesh Salgaonkar * that is required for a kernel to boot successfully.
293eb39c880SMahesh Salgaonkar *
294eb39c880SMahesh Salgaonkar */
fadump_calculate_reserve_size(void)295fbced154SNathan Chancellor static __init u64 fadump_calculate_reserve_size(void)
296eb39c880SMahesh Salgaonkar {
2977b1b3b48SHari Bathini u64 base, size, bootmem_min;
29811550dc0SHari Bathini int ret;
299eb39c880SMahesh Salgaonkar
30081d9eca5SHari Bathini if (fw_dump.reserve_bootvar)
30181d9eca5SHari Bathini pr_warn("'fadump_reserve_mem=' parameter is deprecated in favor of 'crashkernel=' parameter.\n");
30281d9eca5SHari Bathini
303eb39c880SMahesh Salgaonkar /*
30411550dc0SHari Bathini * Check if the size is specified through crashkernel= cmdline
305e7467dc6SHari Bathini * option. If yes, then use that but ignore base as fadump reserves
306e7467dc6SHari Bathini * memory at a predefined offset.
307eb39c880SMahesh Salgaonkar */
30811550dc0SHari Bathini ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
30911550dc0SHari Bathini &size, &base);
31011550dc0SHari Bathini if (ret == 0 && size > 0) {
31148a316e3SHari Bathini unsigned long max_size;
31248a316e3SHari Bathini
31381d9eca5SHari Bathini if (fw_dump.reserve_bootvar)
31481d9eca5SHari Bathini pr_info("Using 'crashkernel=' parameter for memory reservation.\n");
31581d9eca5SHari Bathini
31611550dc0SHari Bathini fw_dump.reserve_bootvar = (unsigned long)size;
31748a316e3SHari Bathini
31848a316e3SHari Bathini /*
31948a316e3SHari Bathini * Adjust if the boot memory size specified is above
32048a316e3SHari Bathini * the upper limit.
32148a316e3SHari Bathini */
32248a316e3SHari Bathini max_size = memblock_phys_mem_size() / MAX_BOOT_MEM_RATIO;
32348a316e3SHari Bathini if (fw_dump.reserve_bootvar > max_size) {
32448a316e3SHari Bathini fw_dump.reserve_bootvar = max_size;
32548a316e3SHari Bathini pr_info("Adjusted boot memory size to %luMB\n",
32648a316e3SHari Bathini (fw_dump.reserve_bootvar >> 20));
32748a316e3SHari Bathini }
32848a316e3SHari Bathini
329eb39c880SMahesh Salgaonkar return fw_dump.reserve_bootvar;
33081d9eca5SHari Bathini } else if (fw_dump.reserve_bootvar) {
33181d9eca5SHari Bathini /*
33281d9eca5SHari Bathini * 'fadump_reserve_mem=' is being used to reserve memory
33381d9eca5SHari Bathini * for firmware-assisted dump.
33481d9eca5SHari Bathini */
33581d9eca5SHari Bathini return fw_dump.reserve_bootvar;
33611550dc0SHari Bathini }
337eb39c880SMahesh Salgaonkar
338eb39c880SMahesh Salgaonkar /* divide by 20 to get 5% of value */
33948a316e3SHari Bathini size = memblock_phys_mem_size() / 20;
340eb39c880SMahesh Salgaonkar
341eb39c880SMahesh Salgaonkar /* round it down in multiples of 256 */
342eb39c880SMahesh Salgaonkar size = size & ~0x0FFFFFFFUL;
343eb39c880SMahesh Salgaonkar
344eb39c880SMahesh Salgaonkar /* Truncate to memory_limit. We don't want to over reserve the memory.*/
345eb39c880SMahesh Salgaonkar if (memory_limit && size > memory_limit)
346eb39c880SMahesh Salgaonkar size = memory_limit;
347eb39c880SMahesh Salgaonkar
3487b1b3b48SHari Bathini bootmem_min = fw_dump.ops->fadump_get_bootmem_min();
3497b1b3b48SHari Bathini return (size > bootmem_min ? size : bootmem_min);
350eb39c880SMahesh Salgaonkar }
351eb39c880SMahesh Salgaonkar
352eb39c880SMahesh Salgaonkar /*
353eb39c880SMahesh Salgaonkar * Calculate the total memory size required to be reserved for
354eb39c880SMahesh Salgaonkar * firmware-assisted dump registration.
355eb39c880SMahesh Salgaonkar */
get_fadump_area_size(void)356d276960dSNick Child static unsigned long __init get_fadump_area_size(void)
357eb39c880SMahesh Salgaonkar {
358eb39c880SMahesh Salgaonkar unsigned long size = 0;
359eb39c880SMahesh Salgaonkar
360eb39c880SMahesh Salgaonkar size += fw_dump.cpu_state_data_size;
361eb39c880SMahesh Salgaonkar size += fw_dump.hpte_region_size;
3629cf3b3a3SHari Bathini /*
3639cf3b3a3SHari Bathini * Account for pagesize alignment of boot memory area destination address.
3649cf3b3a3SHari Bathini * This faciliates in mmap reading of first kernel's memory.
3659cf3b3a3SHari Bathini */
3669cf3b3a3SHari Bathini size = PAGE_ALIGN(size);
367eb39c880SMahesh Salgaonkar size += fw_dump.boot_memory_size;
3682df173d9SMahesh Salgaonkar size += sizeof(struct fadump_crash_info_header);
3692df173d9SMahesh Salgaonkar size += sizeof(struct elfhdr); /* ELF core header.*/
370ebaeb5aeSMahesh Salgaonkar size += sizeof(struct elf_phdr); /* place holder for cpu notes */
3712df173d9SMahesh Salgaonkar /* Program headers for crash memory regions. */
3722df173d9SMahesh Salgaonkar size += sizeof(struct elf_phdr) * (memblock_num_regions(memory) + 2);
373eb39c880SMahesh Salgaonkar
374eb39c880SMahesh Salgaonkar size = PAGE_ALIGN(size);
375742a265aSHari Bathini
376742a265aSHari Bathini /* This is to hold kernel metadata on platforms that support it */
377742a265aSHari Bathini size += (fw_dump.ops->fadump_get_metadata_size ?
378742a265aSHari Bathini fw_dump.ops->fadump_get_metadata_size() : 0);
379eb39c880SMahesh Salgaonkar return size;
380eb39c880SMahesh Salgaonkar }
381eb39c880SMahesh Salgaonkar
add_boot_mem_region(unsigned long rstart,unsigned long rsize)3827dee93a9SHari Bathini static int __init add_boot_mem_region(unsigned long rstart,
3837dee93a9SHari Bathini unsigned long rsize)
3847dee93a9SHari Bathini {
3857dee93a9SHari Bathini int i = fw_dump.boot_mem_regs_cnt++;
3867dee93a9SHari Bathini
3877dee93a9SHari Bathini if (fw_dump.boot_mem_regs_cnt > FADUMP_MAX_MEM_REGS) {
3887dee93a9SHari Bathini fw_dump.boot_mem_regs_cnt = FADUMP_MAX_MEM_REGS;
3897dee93a9SHari Bathini return 0;
3907dee93a9SHari Bathini }
3917dee93a9SHari Bathini
3927dee93a9SHari Bathini pr_debug("Added boot memory range[%d] [%#016lx-%#016lx)\n",
3937dee93a9SHari Bathini i, rstart, (rstart + rsize));
3947dee93a9SHari Bathini fw_dump.boot_mem_addr[i] = rstart;
3957dee93a9SHari Bathini fw_dump.boot_mem_sz[i] = rsize;
3967dee93a9SHari Bathini return 1;
3977dee93a9SHari Bathini }
3987dee93a9SHari Bathini
3997dee93a9SHari Bathini /*
4007dee93a9SHari Bathini * Firmware usually has a hard limit on the data it can copy per region.
4017dee93a9SHari Bathini * Honour that by splitting a memory range into multiple regions.
4027dee93a9SHari Bathini */
add_boot_mem_regions(unsigned long mstart,unsigned long msize)4037dee93a9SHari Bathini static int __init add_boot_mem_regions(unsigned long mstart,
4047dee93a9SHari Bathini unsigned long msize)
4057dee93a9SHari Bathini {
4067dee93a9SHari Bathini unsigned long rstart, rsize, max_size;
4077dee93a9SHari Bathini int ret = 1;
4087dee93a9SHari Bathini
4097dee93a9SHari Bathini rstart = mstart;
4107dee93a9SHari Bathini max_size = fw_dump.max_copy_size ? fw_dump.max_copy_size : msize;
4117dee93a9SHari Bathini while (msize) {
4127dee93a9SHari Bathini if (msize > max_size)
4137dee93a9SHari Bathini rsize = max_size;
4147dee93a9SHari Bathini else
4157dee93a9SHari Bathini rsize = msize;
4167dee93a9SHari Bathini
4177dee93a9SHari Bathini ret = add_boot_mem_region(rstart, rsize);
4187dee93a9SHari Bathini if (!ret)
4197dee93a9SHari Bathini break;
4207dee93a9SHari Bathini
4217dee93a9SHari Bathini msize -= rsize;
4227dee93a9SHari Bathini rstart += rsize;
4237dee93a9SHari Bathini }
4247dee93a9SHari Bathini
4257dee93a9SHari Bathini return ret;
4267dee93a9SHari Bathini }
4277dee93a9SHari Bathini
fadump_get_boot_mem_regions(void)4287dee93a9SHari Bathini static int __init fadump_get_boot_mem_regions(void)
4297dee93a9SHari Bathini {
430b10d6bcaSMike Rapoport unsigned long size, cur_size, hole_size, last_end;
4317dee93a9SHari Bathini unsigned long mem_size = fw_dump.boot_memory_size;
432b10d6bcaSMike Rapoport phys_addr_t reg_start, reg_end;
4337dee93a9SHari Bathini int ret = 1;
434b10d6bcaSMike Rapoport u64 i;
4357dee93a9SHari Bathini
4367dee93a9SHari Bathini fw_dump.boot_mem_regs_cnt = 0;
4377dee93a9SHari Bathini
4387dee93a9SHari Bathini last_end = 0;
4397dee93a9SHari Bathini hole_size = 0;
4407dee93a9SHari Bathini cur_size = 0;
441b10d6bcaSMike Rapoport for_each_mem_range(i, ®_start, ®_end) {
442b10d6bcaSMike Rapoport size = reg_end - reg_start;
443b10d6bcaSMike Rapoport hole_size += (reg_start - last_end);
4447dee93a9SHari Bathini
4457dee93a9SHari Bathini if ((cur_size + size) >= mem_size) {
4467dee93a9SHari Bathini size = (mem_size - cur_size);
447b10d6bcaSMike Rapoport ret = add_boot_mem_regions(reg_start, size);
4487dee93a9SHari Bathini break;
4497dee93a9SHari Bathini }
4507dee93a9SHari Bathini
4517dee93a9SHari Bathini mem_size -= size;
4527dee93a9SHari Bathini cur_size += size;
453b10d6bcaSMike Rapoport ret = add_boot_mem_regions(reg_start, size);
4547dee93a9SHari Bathini if (!ret)
4557dee93a9SHari Bathini break;
4567dee93a9SHari Bathini
457b10d6bcaSMike Rapoport last_end = reg_end;
4587dee93a9SHari Bathini }
4597dee93a9SHari Bathini fw_dump.boot_mem_top = PAGE_ALIGN(fw_dump.boot_memory_size + hole_size);
4607dee93a9SHari Bathini
4617dee93a9SHari Bathini return ret;
4627dee93a9SHari Bathini }
4637dee93a9SHari Bathini
464140777a3SHari Bathini /*
465140777a3SHari Bathini * Returns true, if the given range overlaps with reserved memory ranges
466140777a3SHari Bathini * starting at idx. Also, updates idx to index of overlapping memory range
467140777a3SHari Bathini * with the given memory range.
468140777a3SHari Bathini * False, otherwise.
469140777a3SHari Bathini */
overlaps_reserved_ranges(u64 base,u64 end,int * idx)470d276960dSNick Child static bool __init overlaps_reserved_ranges(u64 base, u64 end, int *idx)
471140777a3SHari Bathini {
472140777a3SHari Bathini bool ret = false;
473140777a3SHari Bathini int i;
474140777a3SHari Bathini
475140777a3SHari Bathini for (i = *idx; i < reserved_mrange_info.mem_range_cnt; i++) {
476140777a3SHari Bathini u64 rbase = reserved_mrange_info.mem_ranges[i].base;
477140777a3SHari Bathini u64 rend = rbase + reserved_mrange_info.mem_ranges[i].size;
478140777a3SHari Bathini
479140777a3SHari Bathini if (end <= rbase)
480140777a3SHari Bathini break;
481140777a3SHari Bathini
482140777a3SHari Bathini if ((end > rbase) && (base < rend)) {
483140777a3SHari Bathini *idx = i;
484140777a3SHari Bathini ret = true;
485140777a3SHari Bathini break;
486140777a3SHari Bathini }
487140777a3SHari Bathini }
488140777a3SHari Bathini
489140777a3SHari Bathini return ret;
490140777a3SHari Bathini }
491140777a3SHari Bathini
492140777a3SHari Bathini /*
493140777a3SHari Bathini * Locate a suitable memory area to reserve memory for FADump. While at it,
494140777a3SHari Bathini * lookup reserved-ranges & avoid overlap with them, as they are used by F/W.
495140777a3SHari Bathini */
fadump_locate_reserve_mem(u64 base,u64 size)496140777a3SHari Bathini static u64 __init fadump_locate_reserve_mem(u64 base, u64 size)
497140777a3SHari Bathini {
498140777a3SHari Bathini struct fadump_memory_range *mrngs;
499140777a3SHari Bathini phys_addr_t mstart, mend;
500140777a3SHari Bathini int idx = 0;
501140777a3SHari Bathini u64 i, ret = 0;
502140777a3SHari Bathini
503140777a3SHari Bathini mrngs = reserved_mrange_info.mem_ranges;
504140777a3SHari Bathini for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE,
505140777a3SHari Bathini &mstart, &mend, NULL) {
506140777a3SHari Bathini pr_debug("%llu) mstart: %llx, mend: %llx, base: %llx\n",
507140777a3SHari Bathini i, mstart, mend, base);
508140777a3SHari Bathini
509140777a3SHari Bathini if (mstart > base)
510140777a3SHari Bathini base = PAGE_ALIGN(mstart);
511140777a3SHari Bathini
512140777a3SHari Bathini while ((mend > base) && ((mend - base) >= size)) {
513140777a3SHari Bathini if (!overlaps_reserved_ranges(base, base+size, &idx)) {
514140777a3SHari Bathini ret = base;
515140777a3SHari Bathini goto out;
516140777a3SHari Bathini }
517140777a3SHari Bathini
518140777a3SHari Bathini base = mrngs[idx].base + mrngs[idx].size;
519140777a3SHari Bathini base = PAGE_ALIGN(base);
520140777a3SHari Bathini }
521140777a3SHari Bathini }
522140777a3SHari Bathini
523140777a3SHari Bathini out:
524140777a3SHari Bathini return ret;
525140777a3SHari Bathini }
526140777a3SHari Bathini
fadump_reserve_mem(void)527eb39c880SMahesh Salgaonkar int __init fadump_reserve_mem(void)
528eb39c880SMahesh Salgaonkar {
529140777a3SHari Bathini u64 base, size, mem_boundary, bootmem_min;
5306abec12cSHari Bathini int ret = 1;
531eb39c880SMahesh Salgaonkar
532eb39c880SMahesh Salgaonkar if (!fw_dump.fadump_enabled)
533eb39c880SMahesh Salgaonkar return 0;
534eb39c880SMahesh Salgaonkar
535eb39c880SMahesh Salgaonkar if (!fw_dump.fadump_supported) {
5366abec12cSHari Bathini pr_info("Firmware-Assisted Dump is not supported on this hardware\n");
5376abec12cSHari Bathini goto error_out;
538eb39c880SMahesh Salgaonkar }
539742a265aSHari Bathini
5403ccc00a7SMahesh Salgaonkar /*
5413ccc00a7SMahesh Salgaonkar * Initialize boot memory size
5423ccc00a7SMahesh Salgaonkar * If dump is active then we have already calculated the size during
5433ccc00a7SMahesh Salgaonkar * first kernel.
5443ccc00a7SMahesh Salgaonkar */
545f3512011SHari Bathini if (!fw_dump.dump_active) {
5466abec12cSHari Bathini fw_dump.boot_memory_size =
5476abec12cSHari Bathini PAGE_ALIGN(fadump_calculate_reserve_size());
548a4e92ce8SMahesh Salgaonkar #ifdef CONFIG_CMA
549579ca1a2SHari Bathini if (!fw_dump.nocma) {
550a4e92ce8SMahesh Salgaonkar fw_dump.boot_memory_size =
551140777a3SHari Bathini ALIGN(fw_dump.boot_memory_size,
552e16faf26SDavid Hildenbrand CMA_MIN_ALIGNMENT_BYTES);
553579ca1a2SHari Bathini }
554a4e92ce8SMahesh Salgaonkar #endif
5557b1b3b48SHari Bathini
5567b1b3b48SHari Bathini bootmem_min = fw_dump.ops->fadump_get_bootmem_min();
5577b1b3b48SHari Bathini if (fw_dump.boot_memory_size < bootmem_min) {
5587b1b3b48SHari Bathini pr_err("Can't enable fadump with boot memory size (0x%lx) less than 0x%llx\n",
5597b1b3b48SHari Bathini fw_dump.boot_memory_size, bootmem_min);
5607b1b3b48SHari Bathini goto error_out;
5617b1b3b48SHari Bathini }
5627dee93a9SHari Bathini
5637dee93a9SHari Bathini if (!fadump_get_boot_mem_regions()) {
5647dee93a9SHari Bathini pr_err("Too many holes in boot memory area to enable fadump\n");
5657dee93a9SHari Bathini goto error_out;
5667dee93a9SHari Bathini }
567a4e92ce8SMahesh Salgaonkar }
568eb39c880SMahesh Salgaonkar
569eb39c880SMahesh Salgaonkar /*
570eb39c880SMahesh Salgaonkar * Calculate the memory boundary.
571eb39c880SMahesh Salgaonkar * If memory_limit is less than actual memory boundary then reserve
572eb39c880SMahesh Salgaonkar * the memory for fadump beyond the memory_limit and adjust the
573eb39c880SMahesh Salgaonkar * memory_limit accordingly, so that the running kernel can run with
574eb39c880SMahesh Salgaonkar * specified memory_limit.
575eb39c880SMahesh Salgaonkar */
576eb39c880SMahesh Salgaonkar if (memory_limit && memory_limit < memblock_end_of_DRAM()) {
577eb39c880SMahesh Salgaonkar size = get_fadump_area_size();
578eb39c880SMahesh Salgaonkar if ((memory_limit + size) < memblock_end_of_DRAM())
579eb39c880SMahesh Salgaonkar memory_limit += size;
580eb39c880SMahesh Salgaonkar else
581eb39c880SMahesh Salgaonkar memory_limit = memblock_end_of_DRAM();
582eb39c880SMahesh Salgaonkar printk(KERN_INFO "Adjusted memory_limit for firmware-assisted"
583a84fcd46SSuzuki Poulose " dump, now %#016llx\n", memory_limit);
584eb39c880SMahesh Salgaonkar }
585eb39c880SMahesh Salgaonkar if (memory_limit)
5866abec12cSHari Bathini mem_boundary = memory_limit;
587eb39c880SMahesh Salgaonkar else
5886abec12cSHari Bathini mem_boundary = memblock_end_of_DRAM();
589eb39c880SMahesh Salgaonkar
5907dee93a9SHari Bathini base = fw_dump.boot_mem_top;
5918255da95SHari Bathini size = get_fadump_area_size();
5928255da95SHari Bathini fw_dump.reserve_dump_area_size = size;
593eb39c880SMahesh Salgaonkar if (fw_dump.dump_active) {
594b71a693dSMahesh Salgaonkar pr_info("Firmware-assisted dump is active.\n");
595b71a693dSMahesh Salgaonkar
59685975387SHari Bathini #ifdef CONFIG_HUGETLB_PAGE
59785975387SHari Bathini /*
59885975387SHari Bathini * FADump capture kernel doesn't care much about hugepages.
59985975387SHari Bathini * In fact, handling hugepages in capture kernel is asking for
60085975387SHari Bathini * trouble. So, disable HugeTLB support when fadump is active.
60185975387SHari Bathini */
60285975387SHari Bathini hugetlb_disabled = true;
60385975387SHari Bathini #endif
604eb39c880SMahesh Salgaonkar /*
605eb39c880SMahesh Salgaonkar * If last boot has crashed then reserve all the memory
606b2a815a5SHari Bathini * above boot memory size so that we don't touch it until
607eb39c880SMahesh Salgaonkar * dump is written to disk by userspace tool. This memory
608b2a815a5SHari Bathini * can be released for general use by invalidating fadump.
609eb39c880SMahesh Salgaonkar */
610b2a815a5SHari Bathini fadump_reserve_crash_area(base);
6112df173d9SMahesh Salgaonkar
612f3512011SHari Bathini pr_debug("fadumphdr_addr = %#016lx\n", fw_dump.fadumphdr_addr);
613f3512011SHari Bathini pr_debug("Reserve dump area start address: 0x%lx\n",
614f3512011SHari Bathini fw_dump.reserve_dump_area_start);
6158255da95SHari Bathini } else {
6168255da95SHari Bathini /*
617f6e6bedbSHari Bathini * Reserve memory at an offset closer to bottom of the RAM to
618579ca1a2SHari Bathini * minimize the impact of memory hot-remove operation.
619f6e6bedbSHari Bathini */
620140777a3SHari Bathini base = fadump_locate_reserve_mem(base, size);
621f6e6bedbSHari Bathini
6229a2921e5SHari Bathini if (!base || (base + size > mem_boundary)) {
623742a265aSHari Bathini pr_err("Failed to find memory chunk for reservation!\n");
624742a265aSHari Bathini goto error_out;
625742a265aSHari Bathini }
626742a265aSHari Bathini fw_dump.reserve_dump_area_start = base;
627742a265aSHari Bathini
628742a265aSHari Bathini /*
629742a265aSHari Bathini * Calculate the kernel metadata address and register it with
630742a265aSHari Bathini * f/w if the platform supports.
631742a265aSHari Bathini */
632742a265aSHari Bathini if (fw_dump.ops->fadump_setup_metadata &&
633742a265aSHari Bathini (fw_dump.ops->fadump_setup_metadata(&fw_dump) < 0))
634742a265aSHari Bathini goto error_out;
635742a265aSHari Bathini
636742a265aSHari Bathini if (memblock_reserve(base, size)) {
6376abec12cSHari Bathini pr_err("Failed to reserve memory!\n");
6386abec12cSHari Bathini goto error_out;
6396abec12cSHari Bathini }
6406abec12cSHari Bathini
6416abec12cSHari Bathini pr_info("Reserved %lldMB of memory at %#016llx (System RAM: %lldMB)\n",
6426abec12cSHari Bathini (size >> 20), base, (memblock_phys_mem_size() >> 20));
643a4e92ce8SMahesh Salgaonkar }
6446abec12cSHari Bathini
6456abec12cSHari Bathini return ret;
6466abec12cSHari Bathini error_out:
6476abec12cSHari Bathini fw_dump.fadump_enabled = 0;
648d1eb75e0SSourabh Jain fw_dump.reserve_dump_area_size = 0;
6496abec12cSHari Bathini return 0;
650eb39c880SMahesh Salgaonkar }
651eb39c880SMahesh Salgaonkar
652eb39c880SMahesh Salgaonkar /* Look for fadump= cmdline option. */
early_fadump_param(char * p)653eb39c880SMahesh Salgaonkar static int __init early_fadump_param(char *p)
654eb39c880SMahesh Salgaonkar {
655eb39c880SMahesh Salgaonkar if (!p)
656eb39c880SMahesh Salgaonkar return 1;
657eb39c880SMahesh Salgaonkar
658eb39c880SMahesh Salgaonkar if (strncmp(p, "on", 2) == 0)
659eb39c880SMahesh Salgaonkar fw_dump.fadump_enabled = 1;
660eb39c880SMahesh Salgaonkar else if (strncmp(p, "off", 3) == 0)
661eb39c880SMahesh Salgaonkar fw_dump.fadump_enabled = 0;
662a4e92ce8SMahesh Salgaonkar else if (strncmp(p, "nocma", 5) == 0) {
663a4e92ce8SMahesh Salgaonkar fw_dump.fadump_enabled = 1;
664a4e92ce8SMahesh Salgaonkar fw_dump.nocma = 1;
665a4e92ce8SMahesh Salgaonkar }
666eb39c880SMahesh Salgaonkar
667eb39c880SMahesh Salgaonkar return 0;
668eb39c880SMahesh Salgaonkar }
669eb39c880SMahesh Salgaonkar early_param("fadump", early_fadump_param);
670eb39c880SMahesh Salgaonkar
67181d9eca5SHari Bathini /*
67281d9eca5SHari Bathini * Look for fadump_reserve_mem= cmdline option
67381d9eca5SHari Bathini * TODO: Remove references to 'fadump_reserve_mem=' parameter,
67481d9eca5SHari Bathini * the sooner 'crashkernel=' parameter is accustomed to.
67581d9eca5SHari Bathini */
early_fadump_reserve_mem(char * p)67681d9eca5SHari Bathini static int __init early_fadump_reserve_mem(char *p)
67781d9eca5SHari Bathini {
67881d9eca5SHari Bathini if (p)
67981d9eca5SHari Bathini fw_dump.reserve_bootvar = memparse(p, &p);
68081d9eca5SHari Bathini return 0;
68181d9eca5SHari Bathini }
68281d9eca5SHari Bathini early_param("fadump_reserve_mem", early_fadump_reserve_mem);
68381d9eca5SHari Bathini
crash_fadump(struct pt_regs * regs,const char * str)684ebaeb5aeSMahesh Salgaonkar void crash_fadump(struct pt_regs *regs, const char *str)
685ebaeb5aeSMahesh Salgaonkar {
686ba608c4fSSourabh Jain unsigned int msecs;
687ebaeb5aeSMahesh Salgaonkar struct fadump_crash_info_header *fdh = NULL;
688f2a5e8f0SMahesh Salgaonkar int old_cpu, this_cpu;
689ba608c4fSSourabh Jain /* Do not include first CPU */
690ba608c4fSSourabh Jain unsigned int ncpus = num_online_cpus() - 1;
691ebaeb5aeSMahesh Salgaonkar
6926fcd6baaSNicholas Piggin if (!should_fadump_crash())
693ebaeb5aeSMahesh Salgaonkar return;
694ebaeb5aeSMahesh Salgaonkar
695f2a5e8f0SMahesh Salgaonkar /*
696f2a5e8f0SMahesh Salgaonkar * old_cpu == -1 means this is the first CPU which has come here,
697f2a5e8f0SMahesh Salgaonkar * go ahead and trigger fadump.
698f2a5e8f0SMahesh Salgaonkar *
699f2a5e8f0SMahesh Salgaonkar * old_cpu != -1 means some other CPU has already on it's way
700f2a5e8f0SMahesh Salgaonkar * to trigger fadump, just keep looping here.
701f2a5e8f0SMahesh Salgaonkar */
702f2a5e8f0SMahesh Salgaonkar this_cpu = smp_processor_id();
703f2a5e8f0SMahesh Salgaonkar old_cpu = cmpxchg(&crashing_cpu, -1, this_cpu);
704f2a5e8f0SMahesh Salgaonkar
705f2a5e8f0SMahesh Salgaonkar if (old_cpu != -1) {
706ba608c4fSSourabh Jain atomic_inc(&cpus_in_fadump);
707ba608c4fSSourabh Jain
708f2a5e8f0SMahesh Salgaonkar /*
709f2a5e8f0SMahesh Salgaonkar * We can't loop here indefinitely. Wait as long as fadump
710f2a5e8f0SMahesh Salgaonkar * is in force. If we race with fadump un-registration this
711f2a5e8f0SMahesh Salgaonkar * loop will break and then we go down to normal panic path
712f2a5e8f0SMahesh Salgaonkar * and reboot. If fadump is in force the first crashing
713f2a5e8f0SMahesh Salgaonkar * cpu will definitely trigger fadump.
714f2a5e8f0SMahesh Salgaonkar */
715f2a5e8f0SMahesh Salgaonkar while (fw_dump.dump_registered)
716f2a5e8f0SMahesh Salgaonkar cpu_relax();
717f2a5e8f0SMahesh Salgaonkar return;
718f2a5e8f0SMahesh Salgaonkar }
719f2a5e8f0SMahesh Salgaonkar
720ebaeb5aeSMahesh Salgaonkar fdh = __va(fw_dump.fadumphdr_addr);
721ebaeb5aeSMahesh Salgaonkar fdh->crashing_cpu = crashing_cpu;
722ebaeb5aeSMahesh Salgaonkar crash_save_vmcoreinfo();
723ebaeb5aeSMahesh Salgaonkar
724ebaeb5aeSMahesh Salgaonkar if (regs)
725ebaeb5aeSMahesh Salgaonkar fdh->regs = *regs;
726ebaeb5aeSMahesh Salgaonkar else
727ebaeb5aeSMahesh Salgaonkar ppc_save_regs(&fdh->regs);
728ebaeb5aeSMahesh Salgaonkar
7296584cec0SHari Bathini fdh->cpu_mask = *cpu_online_mask;
730ebaeb5aeSMahesh Salgaonkar
731ba608c4fSSourabh Jain /*
732ba608c4fSSourabh Jain * If we came in via system reset, wait a while for the secondary
733ba608c4fSSourabh Jain * CPUs to enter.
734ba608c4fSSourabh Jain */
7357153d4bfSXiongwei Song if (TRAP(&(fdh->regs)) == INTERRUPT_SYSTEM_RESET) {
736ba608c4fSSourabh Jain msecs = CRASH_TIMEOUT;
737ba608c4fSSourabh Jain while ((atomic_read(&cpus_in_fadump) < ncpus) && (--msecs > 0))
738ba608c4fSSourabh Jain mdelay(1);
739ba608c4fSSourabh Jain }
740ba608c4fSSourabh Jain
74141a65d16SHari Bathini fw_dump.ops->fadump_trigger(fdh, str);
742ebaeb5aeSMahesh Salgaonkar }
743ebaeb5aeSMahesh Salgaonkar
fadump_regs_to_elf_notes(u32 * buf,struct pt_regs * regs)744d276960dSNick Child u32 *__init fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs)
745ebaeb5aeSMahesh Salgaonkar {
746ebaeb5aeSMahesh Salgaonkar struct elf_prstatus prstatus;
747ebaeb5aeSMahesh Salgaonkar
748ebaeb5aeSMahesh Salgaonkar memset(&prstatus, 0, sizeof(prstatus));
749ebaeb5aeSMahesh Salgaonkar /*
750ebaeb5aeSMahesh Salgaonkar * FIXME: How do i get PID? Do I really need it?
751ebaeb5aeSMahesh Salgaonkar * prstatus.pr_pid = ????
752ebaeb5aeSMahesh Salgaonkar */
7539554e908SBrian Gerst elf_core_copy_regs(&prstatus.pr_reg, regs);
75422bd0177SHari Bathini buf = append_elf_note(buf, CRASH_CORE_NOTE_NAME, NT_PRSTATUS,
755ebaeb5aeSMahesh Salgaonkar &prstatus, sizeof(prstatus));
756ebaeb5aeSMahesh Salgaonkar return buf;
757ebaeb5aeSMahesh Salgaonkar }
758ebaeb5aeSMahesh Salgaonkar
fadump_update_elfcore_header(char * bufp)759d276960dSNick Child void __init fadump_update_elfcore_header(char *bufp)
760ebaeb5aeSMahesh Salgaonkar {
761ebaeb5aeSMahesh Salgaonkar struct elf_phdr *phdr;
762ebaeb5aeSMahesh Salgaonkar
763ebaeb5aeSMahesh Salgaonkar bufp += sizeof(struct elfhdr);
764ebaeb5aeSMahesh Salgaonkar
765ebaeb5aeSMahesh Salgaonkar /* First note is a place holder for cpu notes info. */
766ebaeb5aeSMahesh Salgaonkar phdr = (struct elf_phdr *)bufp;
767ebaeb5aeSMahesh Salgaonkar
768ebaeb5aeSMahesh Salgaonkar if (phdr->p_type == PT_NOTE) {
769961cf26aSHari Bathini phdr->p_paddr = __pa(fw_dump.cpu_notes_buf_vaddr);
770ebaeb5aeSMahesh Salgaonkar phdr->p_offset = phdr->p_paddr;
771ebaeb5aeSMahesh Salgaonkar phdr->p_filesz = fw_dump.cpu_notes_buf_size;
772ebaeb5aeSMahesh Salgaonkar phdr->p_memsz = fw_dump.cpu_notes_buf_size;
773ebaeb5aeSMahesh Salgaonkar }
774ebaeb5aeSMahesh Salgaonkar return;
775ebaeb5aeSMahesh Salgaonkar }
776ebaeb5aeSMahesh Salgaonkar
fadump_alloc_buffer(unsigned long size)777d276960dSNick Child static void *__init fadump_alloc_buffer(unsigned long size)
778ebaeb5aeSMahesh Salgaonkar {
77972aa6517SHari Bathini unsigned long count, i;
780ebaeb5aeSMahesh Salgaonkar struct page *page;
78172aa6517SHari Bathini void *vaddr;
782ebaeb5aeSMahesh Salgaonkar
78372aa6517SHari Bathini vaddr = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
784ebaeb5aeSMahesh Salgaonkar if (!vaddr)
785ebaeb5aeSMahesh Salgaonkar return NULL;
786ebaeb5aeSMahesh Salgaonkar
78772aa6517SHari Bathini count = PAGE_ALIGN(size) / PAGE_SIZE;
788ebaeb5aeSMahesh Salgaonkar page = virt_to_page(vaddr);
789ebaeb5aeSMahesh Salgaonkar for (i = 0; i < count; i++)
79072aa6517SHari Bathini mark_page_reserved(page + i);
791ebaeb5aeSMahesh Salgaonkar return vaddr;
792ebaeb5aeSMahesh Salgaonkar }
793ebaeb5aeSMahesh Salgaonkar
fadump_free_buffer(unsigned long vaddr,unsigned long size)794961cf26aSHari Bathini static void fadump_free_buffer(unsigned long vaddr, unsigned long size)
795ebaeb5aeSMahesh Salgaonkar {
79672aa6517SHari Bathini free_reserved_area((void *)vaddr, (void *)(vaddr + size), -1, NULL);
797ebaeb5aeSMahesh Salgaonkar }
798ebaeb5aeSMahesh Salgaonkar
fadump_setup_cpu_notes_buf(u32 num_cpus)799d276960dSNick Child s32 __init fadump_setup_cpu_notes_buf(u32 num_cpus)
800961cf26aSHari Bathini {
801961cf26aSHari Bathini /* Allocate buffer to hold cpu crash notes. */
802961cf26aSHari Bathini fw_dump.cpu_notes_buf_size = num_cpus * sizeof(note_buf_t);
803961cf26aSHari Bathini fw_dump.cpu_notes_buf_size = PAGE_ALIGN(fw_dump.cpu_notes_buf_size);
804961cf26aSHari Bathini fw_dump.cpu_notes_buf_vaddr =
805961cf26aSHari Bathini (unsigned long)fadump_alloc_buffer(fw_dump.cpu_notes_buf_size);
806961cf26aSHari Bathini if (!fw_dump.cpu_notes_buf_vaddr) {
807961cf26aSHari Bathini pr_err("Failed to allocate %ld bytes for CPU notes buffer\n",
808961cf26aSHari Bathini fw_dump.cpu_notes_buf_size);
809961cf26aSHari Bathini return -ENOMEM;
810961cf26aSHari Bathini }
811961cf26aSHari Bathini
812961cf26aSHari Bathini pr_debug("Allocated buffer for cpu notes of size %ld at 0x%lx\n",
813961cf26aSHari Bathini fw_dump.cpu_notes_buf_size,
814961cf26aSHari Bathini fw_dump.cpu_notes_buf_vaddr);
815961cf26aSHari Bathini return 0;
816961cf26aSHari Bathini }
817961cf26aSHari Bathini
fadump_free_cpu_notes_buf(void)8187f0ad11dSHari Bathini void fadump_free_cpu_notes_buf(void)
819961cf26aSHari Bathini {
820961cf26aSHari Bathini if (!fw_dump.cpu_notes_buf_vaddr)
821961cf26aSHari Bathini return;
822961cf26aSHari Bathini
823961cf26aSHari Bathini fadump_free_buffer(fw_dump.cpu_notes_buf_vaddr,
824961cf26aSHari Bathini fw_dump.cpu_notes_buf_size);
825961cf26aSHari Bathini fw_dump.cpu_notes_buf_vaddr = 0;
826961cf26aSHari Bathini fw_dump.cpu_notes_buf_size = 0;
827961cf26aSHari Bathini }
828961cf26aSHari Bathini
fadump_free_mem_ranges(struct fadump_mrange_info * mrange_info)829e4fc48fbSHari Bathini static void fadump_free_mem_ranges(struct fadump_mrange_info *mrange_info)
8301bd6a1c4SHari Bathini {
83102c04e37SHari Bathini if (mrange_info->is_static) {
83202c04e37SHari Bathini mrange_info->mem_range_cnt = 0;
83302c04e37SHari Bathini return;
83402c04e37SHari Bathini }
83502c04e37SHari Bathini
836e4fc48fbSHari Bathini kfree(mrange_info->mem_ranges);
83702c04e37SHari Bathini memset((void *)((u64)mrange_info + RNG_NAME_SZ), 0,
83802c04e37SHari Bathini (sizeof(struct fadump_mrange_info) - RNG_NAME_SZ));
8391bd6a1c4SHari Bathini }
8401bd6a1c4SHari Bathini
8411bd6a1c4SHari Bathini /*
842e4fc48fbSHari Bathini * Allocate or reallocate mem_ranges array in incremental units
8431bd6a1c4SHari Bathini * of PAGE_SIZE.
8441bd6a1c4SHari Bathini */
fadump_alloc_mem_ranges(struct fadump_mrange_info * mrange_info)845e4fc48fbSHari Bathini static int fadump_alloc_mem_ranges(struct fadump_mrange_info *mrange_info)
8461bd6a1c4SHari Bathini {
847e4fc48fbSHari Bathini struct fadump_memory_range *new_array;
8481bd6a1c4SHari Bathini u64 new_size;
8491bd6a1c4SHari Bathini
850e4fc48fbSHari Bathini new_size = mrange_info->mem_ranges_sz + PAGE_SIZE;
851e4fc48fbSHari Bathini pr_debug("Allocating %llu bytes of memory for %s memory ranges\n",
852e4fc48fbSHari Bathini new_size, mrange_info->name);
8531bd6a1c4SHari Bathini
854e4fc48fbSHari Bathini new_array = krealloc(mrange_info->mem_ranges, new_size, GFP_KERNEL);
8551bd6a1c4SHari Bathini if (new_array == NULL) {
856e4fc48fbSHari Bathini pr_err("Insufficient memory for setting up %s memory ranges\n",
857e4fc48fbSHari Bathini mrange_info->name);
858e4fc48fbSHari Bathini fadump_free_mem_ranges(mrange_info);
8591bd6a1c4SHari Bathini return -ENOMEM;
8601bd6a1c4SHari Bathini }
8611bd6a1c4SHari Bathini
862e4fc48fbSHari Bathini mrange_info->mem_ranges = new_array;
863e4fc48fbSHari Bathini mrange_info->mem_ranges_sz = new_size;
864e4fc48fbSHari Bathini mrange_info->max_mem_ranges = (new_size /
865e4fc48fbSHari Bathini sizeof(struct fadump_memory_range));
8661bd6a1c4SHari Bathini return 0;
8671bd6a1c4SHari Bathini }
fadump_add_mem_range(struct fadump_mrange_info * mrange_info,u64 base,u64 end)868e4fc48fbSHari Bathini static inline int fadump_add_mem_range(struct fadump_mrange_info *mrange_info,
869e4fc48fbSHari Bathini u64 base, u64 end)
8702df173d9SMahesh Salgaonkar {
871e4fc48fbSHari Bathini struct fadump_memory_range *mem_ranges = mrange_info->mem_ranges;
872ced1bf52SHari Bathini bool is_adjacent = false;
873e4fc48fbSHari Bathini u64 start, size;
874ced1bf52SHari Bathini
8752df173d9SMahesh Salgaonkar if (base == end)
8761bd6a1c4SHari Bathini return 0;
8771bd6a1c4SHari Bathini
878ced1bf52SHari Bathini /*
879ced1bf52SHari Bathini * Fold adjacent memory ranges to bring down the memory ranges/
880ced1bf52SHari Bathini * PT_LOAD segments count.
881ced1bf52SHari Bathini */
882e4fc48fbSHari Bathini if (mrange_info->mem_range_cnt) {
883e4fc48fbSHari Bathini start = mem_ranges[mrange_info->mem_range_cnt - 1].base;
884e4fc48fbSHari Bathini size = mem_ranges[mrange_info->mem_range_cnt - 1].size;
885ced1bf52SHari Bathini
88615eb77f8SHari Bathini /*
88715eb77f8SHari Bathini * Boot memory area needs separate PT_LOAD segment(s) as it
88815eb77f8SHari Bathini * is moved to a different location at the time of crash.
88915eb77f8SHari Bathini * So, fold only if the region is not boot memory area.
89015eb77f8SHari Bathini */
89115eb77f8SHari Bathini if ((start + size) == base && start >= fw_dump.boot_mem_top)
892ced1bf52SHari Bathini is_adjacent = true;
893ced1bf52SHari Bathini }
894ced1bf52SHari Bathini if (!is_adjacent) {
895ced1bf52SHari Bathini /* resize the array on reaching the limit */
896e4fc48fbSHari Bathini if (mrange_info->mem_range_cnt == mrange_info->max_mem_ranges) {
8971bd6a1c4SHari Bathini int ret;
8981bd6a1c4SHari Bathini
89902c04e37SHari Bathini if (mrange_info->is_static) {
90002c04e37SHari Bathini pr_err("Reached array size limit for %s memory ranges\n",
90102c04e37SHari Bathini mrange_info->name);
90202c04e37SHari Bathini return -ENOSPC;
90302c04e37SHari Bathini }
90402c04e37SHari Bathini
905e4fc48fbSHari Bathini ret = fadump_alloc_mem_ranges(mrange_info);
9061bd6a1c4SHari Bathini if (ret)
9071bd6a1c4SHari Bathini return ret;
908e4fc48fbSHari Bathini
909e4fc48fbSHari Bathini /* Update to the new resized array */
910e4fc48fbSHari Bathini mem_ranges = mrange_info->mem_ranges;
9111bd6a1c4SHari Bathini }
9122df173d9SMahesh Salgaonkar
913ced1bf52SHari Bathini start = base;
914e4fc48fbSHari Bathini mem_ranges[mrange_info->mem_range_cnt].base = start;
915e4fc48fbSHari Bathini mrange_info->mem_range_cnt++;
916ced1bf52SHari Bathini }
917ced1bf52SHari Bathini
918e4fc48fbSHari Bathini mem_ranges[mrange_info->mem_range_cnt - 1].size = (end - start);
919e4fc48fbSHari Bathini pr_debug("%s_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n",
920e4fc48fbSHari Bathini mrange_info->name, (mrange_info->mem_range_cnt - 1),
921e4fc48fbSHari Bathini start, end - 1, (end - start));
9221bd6a1c4SHari Bathini return 0;
9232df173d9SMahesh Salgaonkar }
9242df173d9SMahesh Salgaonkar
fadump_exclude_reserved_area(u64 start,u64 end)925e4fc48fbSHari Bathini static int fadump_exclude_reserved_area(u64 start, u64 end)
9262df173d9SMahesh Salgaonkar {
927e4fc48fbSHari Bathini u64 ra_start, ra_end;
9281bd6a1c4SHari Bathini int ret = 0;
9292df173d9SMahesh Salgaonkar
9302df173d9SMahesh Salgaonkar ra_start = fw_dump.reserve_dump_area_start;
9312df173d9SMahesh Salgaonkar ra_end = ra_start + fw_dump.reserve_dump_area_size;
9322df173d9SMahesh Salgaonkar
9332df173d9SMahesh Salgaonkar if ((ra_start < end) && (ra_end > start)) {
9342df173d9SMahesh Salgaonkar if ((start < ra_start) && (end > ra_end)) {
935e4fc48fbSHari Bathini ret = fadump_add_mem_range(&crash_mrange_info,
936e4fc48fbSHari Bathini start, ra_start);
9371bd6a1c4SHari Bathini if (ret)
9381bd6a1c4SHari Bathini return ret;
9391bd6a1c4SHari Bathini
940e4fc48fbSHari Bathini ret = fadump_add_mem_range(&crash_mrange_info,
941e4fc48fbSHari Bathini ra_end, end);
9422df173d9SMahesh Salgaonkar } else if (start < ra_start) {
943e4fc48fbSHari Bathini ret = fadump_add_mem_range(&crash_mrange_info,
944e4fc48fbSHari Bathini start, ra_start);
9452df173d9SMahesh Salgaonkar } else if (ra_end < end) {
946e4fc48fbSHari Bathini ret = fadump_add_mem_range(&crash_mrange_info,
947e4fc48fbSHari Bathini ra_end, end);
9482df173d9SMahesh Salgaonkar }
9492df173d9SMahesh Salgaonkar } else
950e4fc48fbSHari Bathini ret = fadump_add_mem_range(&crash_mrange_info, start, end);
9511bd6a1c4SHari Bathini
9521bd6a1c4SHari Bathini return ret;
9532df173d9SMahesh Salgaonkar }
9542df173d9SMahesh Salgaonkar
fadump_init_elfcore_header(char * bufp)9552df173d9SMahesh Salgaonkar static int fadump_init_elfcore_header(char *bufp)
9562df173d9SMahesh Salgaonkar {
9572df173d9SMahesh Salgaonkar struct elfhdr *elf;
9582df173d9SMahesh Salgaonkar
9592df173d9SMahesh Salgaonkar elf = (struct elfhdr *) bufp;
9602df173d9SMahesh Salgaonkar bufp += sizeof(struct elfhdr);
9612df173d9SMahesh Salgaonkar memcpy(elf->e_ident, ELFMAG, SELFMAG);
9622df173d9SMahesh Salgaonkar elf->e_ident[EI_CLASS] = ELF_CLASS;
9632df173d9SMahesh Salgaonkar elf->e_ident[EI_DATA] = ELF_DATA;
9642df173d9SMahesh Salgaonkar elf->e_ident[EI_VERSION] = EV_CURRENT;
9652df173d9SMahesh Salgaonkar elf->e_ident[EI_OSABI] = ELF_OSABI;
9662df173d9SMahesh Salgaonkar memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
9672df173d9SMahesh Salgaonkar elf->e_type = ET_CORE;
9682df173d9SMahesh Salgaonkar elf->e_machine = ELF_ARCH;
9692df173d9SMahesh Salgaonkar elf->e_version = EV_CURRENT;
9702df173d9SMahesh Salgaonkar elf->e_entry = 0;
9712df173d9SMahesh Salgaonkar elf->e_phoff = sizeof(struct elfhdr);
9722df173d9SMahesh Salgaonkar elf->e_shoff = 0;
9735b89492cSChristophe Leroy
9745b89492cSChristophe Leroy if (IS_ENABLED(CONFIG_PPC64_ELF_ABI_V2))
9755b89492cSChristophe Leroy elf->e_flags = 2;
9765b89492cSChristophe Leroy else if (IS_ENABLED(CONFIG_PPC64_ELF_ABI_V1))
9775b89492cSChristophe Leroy elf->e_flags = 1;
9785b89492cSChristophe Leroy else
979d8bced27SDaniel Axtens elf->e_flags = 0;
9805b89492cSChristophe Leroy
9812df173d9SMahesh Salgaonkar elf->e_ehsize = sizeof(struct elfhdr);
9822df173d9SMahesh Salgaonkar elf->e_phentsize = sizeof(struct elf_phdr);
9832df173d9SMahesh Salgaonkar elf->e_phnum = 0;
9842df173d9SMahesh Salgaonkar elf->e_shentsize = 0;
9852df173d9SMahesh Salgaonkar elf->e_shnum = 0;
9862df173d9SMahesh Salgaonkar elf->e_shstrndx = 0;
9872df173d9SMahesh Salgaonkar
9882df173d9SMahesh Salgaonkar return 0;
9892df173d9SMahesh Salgaonkar }
9902df173d9SMahesh Salgaonkar
9912df173d9SMahesh Salgaonkar /*
9922df173d9SMahesh Salgaonkar * Traverse through memblock structure and setup crash memory ranges. These
9932df173d9SMahesh Salgaonkar * ranges will be used create PT_LOAD program headers in elfcore header.
9942df173d9SMahesh Salgaonkar */
fadump_setup_crash_memory_ranges(void)9951bd6a1c4SHari Bathini static int fadump_setup_crash_memory_ranges(void)
9962df173d9SMahesh Salgaonkar {
997b10d6bcaSMike Rapoport u64 i, start, end;
998b10d6bcaSMike Rapoport int ret;
9992df173d9SMahesh Salgaonkar
10002df173d9SMahesh Salgaonkar pr_debug("Setup crash memory ranges.\n");
1001e4fc48fbSHari Bathini crash_mrange_info.mem_range_cnt = 0;
1002ced1bf52SHari Bathini
10032df173d9SMahesh Salgaonkar /*
10047dee93a9SHari Bathini * Boot memory region(s) registered with firmware are moved to
10057dee93a9SHari Bathini * different location at the time of crash. Create separate program
10067dee93a9SHari Bathini * header(s) for this memory chunk(s) with the correct offset.
10072df173d9SMahesh Salgaonkar */
10087dee93a9SHari Bathini for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) {
10097dee93a9SHari Bathini start = fw_dump.boot_mem_addr[i];
10107dee93a9SHari Bathini end = start + fw_dump.boot_mem_sz[i];
10117dee93a9SHari Bathini ret = fadump_add_mem_range(&crash_mrange_info, start, end);
10121bd6a1c4SHari Bathini if (ret)
10131bd6a1c4SHari Bathini return ret;
10147dee93a9SHari Bathini }
10152df173d9SMahesh Salgaonkar
1016b10d6bcaSMike Rapoport for_each_mem_range(i, &start, &end) {
1017a77af552SHari Bathini /*
10187dee93a9SHari Bathini * skip the memory chunk that is already added
10197dee93a9SHari Bathini * (0 through boot_memory_top).
1020a77af552SHari Bathini */
10217dee93a9SHari Bathini if (start < fw_dump.boot_mem_top) {
10227dee93a9SHari Bathini if (end > fw_dump.boot_mem_top)
10237dee93a9SHari Bathini start = fw_dump.boot_mem_top;
1024a77af552SHari Bathini else
1025a77af552SHari Bathini continue;
1026a77af552SHari Bathini }
10272df173d9SMahesh Salgaonkar
10282df173d9SMahesh Salgaonkar /* add this range excluding the reserved dump area. */
10291bd6a1c4SHari Bathini ret = fadump_exclude_reserved_area(start, end);
10301bd6a1c4SHari Bathini if (ret)
10311bd6a1c4SHari Bathini return ret;
10322df173d9SMahesh Salgaonkar }
10331bd6a1c4SHari Bathini
10341bd6a1c4SHari Bathini return 0;
10352df173d9SMahesh Salgaonkar }
10362df173d9SMahesh Salgaonkar
1037d34c5f26SMahesh Salgaonkar /*
1038d34c5f26SMahesh Salgaonkar * If the given physical address falls within the boot memory region then
1039d34c5f26SMahesh Salgaonkar * return the relocated address that points to the dump region reserved
1040d34c5f26SMahesh Salgaonkar * for saving initial boot memory contents.
1041d34c5f26SMahesh Salgaonkar */
fadump_relocate(unsigned long paddr)1042d34c5f26SMahesh Salgaonkar static inline unsigned long fadump_relocate(unsigned long paddr)
1043d34c5f26SMahesh Salgaonkar {
10447dee93a9SHari Bathini unsigned long raddr, rstart, rend, rlast, hole_size;
10457dee93a9SHari Bathini int i;
10467dee93a9SHari Bathini
10477dee93a9SHari Bathini hole_size = 0;
10487dee93a9SHari Bathini rlast = 0;
10497dee93a9SHari Bathini raddr = paddr;
10507dee93a9SHari Bathini for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) {
10517dee93a9SHari Bathini rstart = fw_dump.boot_mem_addr[i];
10527dee93a9SHari Bathini rend = rstart + fw_dump.boot_mem_sz[i];
10537dee93a9SHari Bathini hole_size += (rstart - rlast);
10547dee93a9SHari Bathini
10557dee93a9SHari Bathini if (paddr >= rstart && paddr < rend) {
10567dee93a9SHari Bathini raddr += fw_dump.boot_mem_dest_addr - hole_size;
10577dee93a9SHari Bathini break;
10587dee93a9SHari Bathini }
10597dee93a9SHari Bathini
10607dee93a9SHari Bathini rlast = rend;
10617dee93a9SHari Bathini }
10627dee93a9SHari Bathini
10637dee93a9SHari Bathini pr_debug("vmcoreinfo: paddr = 0x%lx, raddr = 0x%lx\n", paddr, raddr);
10647dee93a9SHari Bathini return raddr;
1065d34c5f26SMahesh Salgaonkar }
1066d34c5f26SMahesh Salgaonkar
fadump_create_elfcore_headers(char * bufp)10672df173d9SMahesh Salgaonkar static int fadump_create_elfcore_headers(char *bufp)
10682df173d9SMahesh Salgaonkar {
10697dee93a9SHari Bathini unsigned long long raddr, offset;
10702df173d9SMahesh Salgaonkar struct elf_phdr *phdr;
10717dee93a9SHari Bathini struct elfhdr *elf;
10727dee93a9SHari Bathini int i, j;
10732df173d9SMahesh Salgaonkar
10742df173d9SMahesh Salgaonkar fadump_init_elfcore_header(bufp);
10752df173d9SMahesh Salgaonkar elf = (struct elfhdr *)bufp;
10762df173d9SMahesh Salgaonkar bufp += sizeof(struct elfhdr);
10772df173d9SMahesh Salgaonkar
1078ebaeb5aeSMahesh Salgaonkar /*
1079ebaeb5aeSMahesh Salgaonkar * setup ELF PT_NOTE, place holder for cpu notes info. The notes info
1080ebaeb5aeSMahesh Salgaonkar * will be populated during second kernel boot after crash. Hence
1081ebaeb5aeSMahesh Salgaonkar * this PT_NOTE will always be the first elf note.
1082ebaeb5aeSMahesh Salgaonkar *
1083ebaeb5aeSMahesh Salgaonkar * NOTE: Any new ELF note addition should be placed after this note.
1084ebaeb5aeSMahesh Salgaonkar */
1085ebaeb5aeSMahesh Salgaonkar phdr = (struct elf_phdr *)bufp;
1086ebaeb5aeSMahesh Salgaonkar bufp += sizeof(struct elf_phdr);
1087ebaeb5aeSMahesh Salgaonkar phdr->p_type = PT_NOTE;
1088ebaeb5aeSMahesh Salgaonkar phdr->p_flags = 0;
1089ebaeb5aeSMahesh Salgaonkar phdr->p_vaddr = 0;
1090ebaeb5aeSMahesh Salgaonkar phdr->p_align = 0;
1091ebaeb5aeSMahesh Salgaonkar
1092ebaeb5aeSMahesh Salgaonkar phdr->p_offset = 0;
1093ebaeb5aeSMahesh Salgaonkar phdr->p_paddr = 0;
1094ebaeb5aeSMahesh Salgaonkar phdr->p_filesz = 0;
1095ebaeb5aeSMahesh Salgaonkar phdr->p_memsz = 0;
1096ebaeb5aeSMahesh Salgaonkar
1097ebaeb5aeSMahesh Salgaonkar (elf->e_phnum)++;
1098ebaeb5aeSMahesh Salgaonkar
1099d34c5f26SMahesh Salgaonkar /* setup ELF PT_NOTE for vmcoreinfo */
1100d34c5f26SMahesh Salgaonkar phdr = (struct elf_phdr *)bufp;
1101d34c5f26SMahesh Salgaonkar bufp += sizeof(struct elf_phdr);
1102d34c5f26SMahesh Salgaonkar phdr->p_type = PT_NOTE;
1103d34c5f26SMahesh Salgaonkar phdr->p_flags = 0;
1104d34c5f26SMahesh Salgaonkar phdr->p_vaddr = 0;
1105d34c5f26SMahesh Salgaonkar phdr->p_align = 0;
1106d34c5f26SMahesh Salgaonkar
1107d34c5f26SMahesh Salgaonkar phdr->p_paddr = fadump_relocate(paddr_vmcoreinfo_note());
1108d34c5f26SMahesh Salgaonkar phdr->p_offset = phdr->p_paddr;
11095203f499SXunlei Pang phdr->p_memsz = phdr->p_filesz = VMCOREINFO_NOTE_SIZE;
1110d34c5f26SMahesh Salgaonkar
1111d34c5f26SMahesh Salgaonkar /* Increment number of program headers. */
1112d34c5f26SMahesh Salgaonkar (elf->e_phnum)++;
1113d34c5f26SMahesh Salgaonkar
11142df173d9SMahesh Salgaonkar /* setup PT_LOAD sections. */
11157dee93a9SHari Bathini j = 0;
11167dee93a9SHari Bathini offset = 0;
11177dee93a9SHari Bathini raddr = fw_dump.boot_mem_addr[0];
1118e4fc48fbSHari Bathini for (i = 0; i < crash_mrange_info.mem_range_cnt; i++) {
1119e4fc48fbSHari Bathini u64 mbase, msize;
11202df173d9SMahesh Salgaonkar
1121e4fc48fbSHari Bathini mbase = crash_mrange_info.mem_ranges[i].base;
1122e4fc48fbSHari Bathini msize = crash_mrange_info.mem_ranges[i].size;
11232df173d9SMahesh Salgaonkar if (!msize)
11242df173d9SMahesh Salgaonkar continue;
11252df173d9SMahesh Salgaonkar
11262df173d9SMahesh Salgaonkar phdr = (struct elf_phdr *)bufp;
11272df173d9SMahesh Salgaonkar bufp += sizeof(struct elf_phdr);
11282df173d9SMahesh Salgaonkar phdr->p_type = PT_LOAD;
11292df173d9SMahesh Salgaonkar phdr->p_flags = PF_R|PF_W|PF_X;
11302df173d9SMahesh Salgaonkar phdr->p_offset = mbase;
11312df173d9SMahesh Salgaonkar
11327dee93a9SHari Bathini if (mbase == raddr) {
11332df173d9SMahesh Salgaonkar /*
1134becd91d9SHari Bathini * The entire real memory region will be moved by
1135becd91d9SHari Bathini * firmware to the specified destination_address.
1136becd91d9SHari Bathini * Hence set the correct offset.
11372df173d9SMahesh Salgaonkar */
11387dee93a9SHari Bathini phdr->p_offset = fw_dump.boot_mem_dest_addr + offset;
11397dee93a9SHari Bathini if (j < (fw_dump.boot_mem_regs_cnt - 1)) {
11407dee93a9SHari Bathini offset += fw_dump.boot_mem_sz[j];
11417dee93a9SHari Bathini raddr = fw_dump.boot_mem_addr[++j];
11427dee93a9SHari Bathini }
11432df173d9SMahesh Salgaonkar }
11442df173d9SMahesh Salgaonkar
11452df173d9SMahesh Salgaonkar phdr->p_paddr = mbase;
11462df173d9SMahesh Salgaonkar phdr->p_vaddr = (unsigned long)__va(mbase);
11472df173d9SMahesh Salgaonkar phdr->p_filesz = msize;
11482df173d9SMahesh Salgaonkar phdr->p_memsz = msize;
11492df173d9SMahesh Salgaonkar phdr->p_align = 0;
11502df173d9SMahesh Salgaonkar
11512df173d9SMahesh Salgaonkar /* Increment number of program headers. */
11522df173d9SMahesh Salgaonkar (elf->e_phnum)++;
11532df173d9SMahesh Salgaonkar }
11542df173d9SMahesh Salgaonkar return 0;
11552df173d9SMahesh Salgaonkar }
11562df173d9SMahesh Salgaonkar
init_fadump_header(unsigned long addr)11572df173d9SMahesh Salgaonkar static unsigned long init_fadump_header(unsigned long addr)
11582df173d9SMahesh Salgaonkar {
11592df173d9SMahesh Salgaonkar struct fadump_crash_info_header *fdh;
11602df173d9SMahesh Salgaonkar
11612df173d9SMahesh Salgaonkar if (!addr)
11622df173d9SMahesh Salgaonkar return 0;
11632df173d9SMahesh Salgaonkar
11642df173d9SMahesh Salgaonkar fdh = __va(addr);
11652df173d9SMahesh Salgaonkar addr += sizeof(struct fadump_crash_info_header);
11662df173d9SMahesh Salgaonkar
11672df173d9SMahesh Salgaonkar memset(fdh, 0, sizeof(struct fadump_crash_info_header));
11682df173d9SMahesh Salgaonkar fdh->magic_number = FADUMP_CRASH_INFO_MAGIC;
11692df173d9SMahesh Salgaonkar fdh->elfcorehdr_addr = addr;
1170ebaeb5aeSMahesh Salgaonkar /* We will set the crashing cpu id in crash_fadump() during crash. */
11710226e552SHari Bathini fdh->crashing_cpu = FADUMP_CPU_UNKNOWN;
11726584cec0SHari Bathini /*
11736584cec0SHari Bathini * When LPAR is terminated by PYHP, ensure all possible CPUs'
11746584cec0SHari Bathini * register data is processed while exporting the vmcore.
11756584cec0SHari Bathini */
11766584cec0SHari Bathini fdh->cpu_mask = *cpu_possible_mask;
11772df173d9SMahesh Salgaonkar
11782df173d9SMahesh Salgaonkar return addr;
11792df173d9SMahesh Salgaonkar }
11802df173d9SMahesh Salgaonkar
register_fadump(void)118198b8cd7fSMichal Suchanek static int register_fadump(void)
11823ccc00a7SMahesh Salgaonkar {
11832df173d9SMahesh Salgaonkar unsigned long addr;
11842df173d9SMahesh Salgaonkar void *vaddr;
11851bd6a1c4SHari Bathini int ret;
11862df173d9SMahesh Salgaonkar
11873ccc00a7SMahesh Salgaonkar /*
11883ccc00a7SMahesh Salgaonkar * If no memory is reserved then we can not register for firmware-
11893ccc00a7SMahesh Salgaonkar * assisted dump.
11903ccc00a7SMahesh Salgaonkar */
11913ccc00a7SMahesh Salgaonkar if (!fw_dump.reserve_dump_area_size)
119298b8cd7fSMichal Suchanek return -ENODEV;
11933ccc00a7SMahesh Salgaonkar
11941bd6a1c4SHari Bathini ret = fadump_setup_crash_memory_ranges();
11951bd6a1c4SHari Bathini if (ret)
11961bd6a1c4SHari Bathini return ret;
11972df173d9SMahesh Salgaonkar
119841a65d16SHari Bathini addr = fw_dump.fadumphdr_addr;
119941a65d16SHari Bathini
12002df173d9SMahesh Salgaonkar /* Initialize fadump crash info header. */
12012df173d9SMahesh Salgaonkar addr = init_fadump_header(addr);
12022df173d9SMahesh Salgaonkar vaddr = __va(addr);
12032df173d9SMahesh Salgaonkar
12042df173d9SMahesh Salgaonkar pr_debug("Creating ELF core headers at %#016lx\n", addr);
12052df173d9SMahesh Salgaonkar fadump_create_elfcore_headers(vaddr);
12062df173d9SMahesh Salgaonkar
12073ccc00a7SMahesh Salgaonkar /* register the future kernel dump with firmware. */
120841a65d16SHari Bathini pr_debug("Registering for firmware-assisted kernel dump...\n");
120941a65d16SHari Bathini return fw_dump.ops->fadump_register(&fw_dump);
12103ccc00a7SMahesh Salgaonkar }
12113ccc00a7SMahesh Salgaonkar
fadump_cleanup(void)1212b500afffSMahesh Salgaonkar void fadump_cleanup(void)
1213b500afffSMahesh Salgaonkar {
12142790d01dSHari Bathini if (!fw_dump.fadump_supported)
12152790d01dSHari Bathini return;
12162790d01dSHari Bathini
1217b500afffSMahesh Salgaonkar /* Invalidate the registration only if dump is active. */
1218b500afffSMahesh Salgaonkar if (fw_dump.dump_active) {
1219f3512011SHari Bathini pr_debug("Invalidating firmware-assisted dump registration\n");
1220f3512011SHari Bathini fw_dump.ops->fadump_invalidate(&fw_dump);
1221722cde76SMahesh Salgaonkar } else if (fw_dump.dump_registered) {
1222722cde76SMahesh Salgaonkar /* Un-register Firmware-assisted dump if it was registered. */
122341a65d16SHari Bathini fw_dump.ops->fadump_unregister(&fw_dump);
1224e4fc48fbSHari Bathini fadump_free_mem_ranges(&crash_mrange_info);
1225b500afffSMahesh Salgaonkar }
12262790d01dSHari Bathini
12272790d01dSHari Bathini if (fw_dump.ops->fadump_cleanup)
12282790d01dSHari Bathini fw_dump.ops->fadump_cleanup(&fw_dump);
1229b500afffSMahesh Salgaonkar }
1230b500afffSMahesh Salgaonkar
fadump_free_reserved_memory(unsigned long start_pfn,unsigned long end_pfn)123168fa6478SHari Bathini static void fadump_free_reserved_memory(unsigned long start_pfn,
123268fa6478SHari Bathini unsigned long end_pfn)
123368fa6478SHari Bathini {
123468fa6478SHari Bathini unsigned long pfn;
123568fa6478SHari Bathini unsigned long time_limit = jiffies + HZ;
123668fa6478SHari Bathini
123768fa6478SHari Bathini pr_info("freeing reserved memory (0x%llx - 0x%llx)\n",
123868fa6478SHari Bathini PFN_PHYS(start_pfn), PFN_PHYS(end_pfn));
123968fa6478SHari Bathini
124068fa6478SHari Bathini for (pfn = start_pfn; pfn < end_pfn; pfn++) {
124168fa6478SHari Bathini free_reserved_page(pfn_to_page(pfn));
124268fa6478SHari Bathini
124368fa6478SHari Bathini if (time_after(jiffies, time_limit)) {
124468fa6478SHari Bathini cond_resched();
124568fa6478SHari Bathini time_limit = jiffies + HZ;
124668fa6478SHari Bathini }
124768fa6478SHari Bathini }
124868fa6478SHari Bathini }
124968fa6478SHari Bathini
125068fa6478SHari Bathini /*
125168fa6478SHari Bathini * Skip memory holes and free memory that was actually reserved.
125268fa6478SHari Bathini */
fadump_release_reserved_area(u64 start,u64 end)1253dda9dbfeSHari Bathini static void fadump_release_reserved_area(u64 start, u64 end)
125468fa6478SHari Bathini {
1255b10d6bcaSMike Rapoport unsigned long reg_spfn, reg_epfn;
1256b10d6bcaSMike Rapoport u64 tstart, tend, spfn, epfn;
1257b10d6bcaSMike Rapoport int i;
125868fa6478SHari Bathini
1259dda9dbfeSHari Bathini spfn = PHYS_PFN(start);
1260dda9dbfeSHari Bathini epfn = PHYS_PFN(end);
1261c9118e6cSMike Rapoport
1262c9118e6cSMike Rapoport for_each_mem_pfn_range(i, MAX_NUMNODES, ®_spfn, ®_epfn, NULL) {
1263c9118e6cSMike Rapoport tstart = max_t(u64, spfn, reg_spfn);
1264c9118e6cSMike Rapoport tend = min_t(u64, epfn, reg_epfn);
1265c9118e6cSMike Rapoport
126668fa6478SHari Bathini if (tstart < tend) {
126768fa6478SHari Bathini fadump_free_reserved_memory(tstart, tend);
126868fa6478SHari Bathini
1269dda9dbfeSHari Bathini if (tend == epfn)
127068fa6478SHari Bathini break;
127168fa6478SHari Bathini
1272dda9dbfeSHari Bathini spfn = tend;
127368fa6478SHari Bathini }
127468fa6478SHari Bathini }
127568fa6478SHari Bathini }
127668fa6478SHari Bathini
1277b500afffSMahesh Salgaonkar /*
1278dda9dbfeSHari Bathini * Sort the mem ranges in-place and merge adjacent ranges
1279dda9dbfeSHari Bathini * to minimize the memory ranges count.
1280b500afffSMahesh Salgaonkar */
sort_and_merge_mem_ranges(struct fadump_mrange_info * mrange_info)1281dda9dbfeSHari Bathini static void sort_and_merge_mem_ranges(struct fadump_mrange_info *mrange_info)
1282b500afffSMahesh Salgaonkar {
1283dda9dbfeSHari Bathini struct fadump_memory_range *mem_ranges;
1284dda9dbfeSHari Bathini u64 base, size;
1285dda9dbfeSHari Bathini int i, j, idx;
1286dda9dbfeSHari Bathini
1287dda9dbfeSHari Bathini if (!reserved_mrange_info.mem_range_cnt)
1288dda9dbfeSHari Bathini return;
1289dda9dbfeSHari Bathini
1290dda9dbfeSHari Bathini /* Sort the memory ranges */
1291dda9dbfeSHari Bathini mem_ranges = mrange_info->mem_ranges;
1292dda9dbfeSHari Bathini for (i = 0; i < mrange_info->mem_range_cnt; i++) {
1293dda9dbfeSHari Bathini idx = i;
1294dda9dbfeSHari Bathini for (j = (i + 1); j < mrange_info->mem_range_cnt; j++) {
1295dda9dbfeSHari Bathini if (mem_ranges[idx].base > mem_ranges[j].base)
1296dda9dbfeSHari Bathini idx = j;
1297dda9dbfeSHari Bathini }
129820776319SJiapeng Chong if (idx != i)
129920776319SJiapeng Chong swap(mem_ranges[idx], mem_ranges[i]);
1300dda9dbfeSHari Bathini }
1301dda9dbfeSHari Bathini
1302dda9dbfeSHari Bathini /* Merge adjacent reserved ranges */
1303dda9dbfeSHari Bathini idx = 0;
1304dda9dbfeSHari Bathini for (i = 1; i < mrange_info->mem_range_cnt; i++) {
1305dda9dbfeSHari Bathini base = mem_ranges[i-1].base;
1306dda9dbfeSHari Bathini size = mem_ranges[i-1].size;
1307dda9dbfeSHari Bathini if (mem_ranges[i].base == (base + size))
1308dda9dbfeSHari Bathini mem_ranges[idx].size += mem_ranges[i].size;
1309dda9dbfeSHari Bathini else {
1310dda9dbfeSHari Bathini idx++;
1311dda9dbfeSHari Bathini if (i == idx)
1312dda9dbfeSHari Bathini continue;
1313dda9dbfeSHari Bathini
1314dda9dbfeSHari Bathini mem_ranges[idx] = mem_ranges[i];
1315dda9dbfeSHari Bathini }
1316dda9dbfeSHari Bathini }
1317dda9dbfeSHari Bathini mrange_info->mem_range_cnt = idx + 1;
1318dda9dbfeSHari Bathini }
1319dda9dbfeSHari Bathini
1320dda9dbfeSHari Bathini /*
1321dda9dbfeSHari Bathini * Scan reserved-ranges to consider them while reserving/releasing
1322dda9dbfeSHari Bathini * memory for FADump.
1323dda9dbfeSHari Bathini */
early_init_dt_scan_reserved_ranges(unsigned long node)132402c04e37SHari Bathini static void __init early_init_dt_scan_reserved_ranges(unsigned long node)
1325dda9dbfeSHari Bathini {
1326dda9dbfeSHari Bathini const __be32 *prop;
1327dda9dbfeSHari Bathini int len, ret = -1;
1328dda9dbfeSHari Bathini unsigned long i;
1329dda9dbfeSHari Bathini
133002c04e37SHari Bathini /* reserved-ranges already scanned */
133102c04e37SHari Bathini if (reserved_mrange_info.mem_range_cnt != 0)
133202c04e37SHari Bathini return;
1333dda9dbfeSHari Bathini
133402c04e37SHari Bathini prop = of_get_flat_dt_prop(node, "reserved-ranges", &len);
1335dda9dbfeSHari Bathini if (!prop)
133602c04e37SHari Bathini return;
1337dda9dbfeSHari Bathini
1338dda9dbfeSHari Bathini /*
1339dda9dbfeSHari Bathini * Each reserved range is an (address,size) pair, 2 cells each,
1340dda9dbfeSHari Bathini * totalling 4 cells per range.
1341dda9dbfeSHari Bathini */
1342dda9dbfeSHari Bathini for (i = 0; i < len / (sizeof(*prop) * 4); i++) {
1343dda9dbfeSHari Bathini u64 base, size;
1344dda9dbfeSHari Bathini
1345dda9dbfeSHari Bathini base = of_read_number(prop + (i * 4) + 0, 2);
1346dda9dbfeSHari Bathini size = of_read_number(prop + (i * 4) + 2, 2);
1347dda9dbfeSHari Bathini
1348dda9dbfeSHari Bathini if (size) {
1349dda9dbfeSHari Bathini ret = fadump_add_mem_range(&reserved_mrange_info,
1350dda9dbfeSHari Bathini base, base + size);
1351dda9dbfeSHari Bathini if (ret < 0) {
1352dda9dbfeSHari Bathini pr_warn("some reserved ranges are ignored!\n");
1353dda9dbfeSHari Bathini break;
1354dda9dbfeSHari Bathini }
1355dda9dbfeSHari Bathini }
1356dda9dbfeSHari Bathini }
1357dda9dbfeSHari Bathini
135802c04e37SHari Bathini /* Compact reserved ranges */
135902c04e37SHari Bathini sort_and_merge_mem_ranges(&reserved_mrange_info);
1360dda9dbfeSHari Bathini }
1361dda9dbfeSHari Bathini
1362dda9dbfeSHari Bathini /*
1363dda9dbfeSHari Bathini * Release the memory that was reserved during early boot to preserve the
1364dda9dbfeSHari Bathini * crash'ed kernel's memory contents except reserved dump area (permanent
1365dda9dbfeSHari Bathini * reservation) and reserved ranges used by F/W. The released memory will
1366dda9dbfeSHari Bathini * be available for general use.
1367dda9dbfeSHari Bathini */
fadump_release_memory(u64 begin,u64 end)1368dda9dbfeSHari Bathini static void fadump_release_memory(u64 begin, u64 end)
1369dda9dbfeSHari Bathini {
1370dda9dbfeSHari Bathini u64 ra_start, ra_end, tstart;
1371dda9dbfeSHari Bathini int i, ret;
1372dda9dbfeSHari Bathini
1373b500afffSMahesh Salgaonkar ra_start = fw_dump.reserve_dump_area_start;
1374b500afffSMahesh Salgaonkar ra_end = ra_start + fw_dump.reserve_dump_area_size;
1375b500afffSMahesh Salgaonkar
1376b500afffSMahesh Salgaonkar /*
137702c04e37SHari Bathini * If reserved ranges array limit is hit, overwrite the last reserved
137802c04e37SHari Bathini * memory range with reserved dump area to ensure it is excluded from
137902c04e37SHari Bathini * the memory being released (reused for next FADump registration).
1380dda9dbfeSHari Bathini */
138102c04e37SHari Bathini if (reserved_mrange_info.mem_range_cnt ==
138202c04e37SHari Bathini reserved_mrange_info.max_mem_ranges)
138302c04e37SHari Bathini reserved_mrange_info.mem_range_cnt--;
1384dda9dbfeSHari Bathini
138502c04e37SHari Bathini ret = fadump_add_mem_range(&reserved_mrange_info, ra_start, ra_end);
138602c04e37SHari Bathini if (ret != 0)
1387dda9dbfeSHari Bathini return;
1388dda9dbfeSHari Bathini
1389dda9dbfeSHari Bathini /* Get the reserved ranges list in order first. */
1390dda9dbfeSHari Bathini sort_and_merge_mem_ranges(&reserved_mrange_info);
1391dda9dbfeSHari Bathini
1392dda9dbfeSHari Bathini /* Exclude reserved ranges and release remaining memory */
1393dda9dbfeSHari Bathini tstart = begin;
1394dda9dbfeSHari Bathini for (i = 0; i < reserved_mrange_info.mem_range_cnt; i++) {
1395dda9dbfeSHari Bathini ra_start = reserved_mrange_info.mem_ranges[i].base;
1396dda9dbfeSHari Bathini ra_end = ra_start + reserved_mrange_info.mem_ranges[i].size;
1397dda9dbfeSHari Bathini
1398dda9dbfeSHari Bathini if (tstart >= ra_end)
1399dda9dbfeSHari Bathini continue;
1400dda9dbfeSHari Bathini
1401dda9dbfeSHari Bathini if (tstart < ra_start)
1402dda9dbfeSHari Bathini fadump_release_reserved_area(tstart, ra_start);
1403dda9dbfeSHari Bathini tstart = ra_end;
1404dda9dbfeSHari Bathini }
1405dda9dbfeSHari Bathini
1406dda9dbfeSHari Bathini if (tstart < end)
1407dda9dbfeSHari Bathini fadump_release_reserved_area(tstart, end);
1408b500afffSMahesh Salgaonkar }
1409b500afffSMahesh Salgaonkar
fadump_invalidate_release_mem(void)1410b500afffSMahesh Salgaonkar static void fadump_invalidate_release_mem(void)
1411b500afffSMahesh Salgaonkar {
1412b500afffSMahesh Salgaonkar mutex_lock(&fadump_mutex);
1413b500afffSMahesh Salgaonkar if (!fw_dump.dump_active) {
1414b500afffSMahesh Salgaonkar mutex_unlock(&fadump_mutex);
1415b500afffSMahesh Salgaonkar return;
1416b500afffSMahesh Salgaonkar }
1417b500afffSMahesh Salgaonkar
1418b500afffSMahesh Salgaonkar fadump_cleanup();
1419b500afffSMahesh Salgaonkar mutex_unlock(&fadump_mutex);
1420b500afffSMahesh Salgaonkar
14217dee93a9SHari Bathini fadump_release_memory(fw_dump.boot_mem_top, memblock_end_of_DRAM());
1422961cf26aSHari Bathini fadump_free_cpu_notes_buf();
1423961cf26aSHari Bathini
1424a4e2e2caSHari Bathini /*
1425a4e2e2caSHari Bathini * Setup kernel metadata and initialize the kernel dump
1426a4e2e2caSHari Bathini * memory structure for FADump re-registration.
1427a4e2e2caSHari Bathini */
1428a4e2e2caSHari Bathini if (fw_dump.ops->fadump_setup_metadata &&
1429a4e2e2caSHari Bathini (fw_dump.ops->fadump_setup_metadata(&fw_dump) < 0))
1430a4e2e2caSHari Bathini pr_warn("Failed to setup kernel metadata!\n");
143141a65d16SHari Bathini fw_dump.ops->fadump_init_mem_struct(&fw_dump);
1432b500afffSMahesh Salgaonkar }
1433b500afffSMahesh Salgaonkar
release_mem_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)1434d418b19fSSourabh Jain static ssize_t release_mem_store(struct kobject *kobj,
1435b500afffSMahesh Salgaonkar struct kobj_attribute *attr,
1436b500afffSMahesh Salgaonkar const char *buf, size_t count)
1437b500afffSMahesh Salgaonkar {
1438dcdc4679SMichal Suchanek int input = -1;
1439dcdc4679SMichal Suchanek
1440b500afffSMahesh Salgaonkar if (!fw_dump.dump_active)
1441b500afffSMahesh Salgaonkar return -EPERM;
1442b500afffSMahesh Salgaonkar
1443dcdc4679SMichal Suchanek if (kstrtoint(buf, 0, &input))
1444dcdc4679SMichal Suchanek return -EINVAL;
1445dcdc4679SMichal Suchanek
1446dcdc4679SMichal Suchanek if (input == 1) {
1447b500afffSMahesh Salgaonkar /*
1448b500afffSMahesh Salgaonkar * Take away the '/proc/vmcore'. We are releasing the dump
1449b500afffSMahesh Salgaonkar * memory, hence it will not be valid anymore.
1450b500afffSMahesh Salgaonkar */
14512685f826SMichael Ellerman #ifdef CONFIG_PROC_VMCORE
1452b500afffSMahesh Salgaonkar vmcore_cleanup();
14532685f826SMichael Ellerman #endif
1454b500afffSMahesh Salgaonkar fadump_invalidate_release_mem();
1455b500afffSMahesh Salgaonkar
1456b500afffSMahesh Salgaonkar } else
1457b500afffSMahesh Salgaonkar return -EINVAL;
1458b500afffSMahesh Salgaonkar return count;
1459b500afffSMahesh Salgaonkar }
1460b500afffSMahesh Salgaonkar
1461d418b19fSSourabh Jain /* Release the reserved memory and disable the FADump */
unregister_fadump(void)1462d276960dSNick Child static void __init unregister_fadump(void)
1463d418b19fSSourabh Jain {
1464d418b19fSSourabh Jain fadump_cleanup();
1465d418b19fSSourabh Jain fadump_release_memory(fw_dump.reserve_dump_area_start,
1466d418b19fSSourabh Jain fw_dump.reserve_dump_area_size);
1467d418b19fSSourabh Jain fw_dump.fadump_enabled = 0;
1468d418b19fSSourabh Jain kobject_put(fadump_kobj);
1469d418b19fSSourabh Jain }
1470d418b19fSSourabh Jain
enabled_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)1471d418b19fSSourabh Jain static ssize_t enabled_show(struct kobject *kobj,
14723ccc00a7SMahesh Salgaonkar struct kobj_attribute *attr,
14733ccc00a7SMahesh Salgaonkar char *buf)
14743ccc00a7SMahesh Salgaonkar {
14753ccc00a7SMahesh Salgaonkar return sprintf(buf, "%d\n", fw_dump.fadump_enabled);
14763ccc00a7SMahesh Salgaonkar }
14773ccc00a7SMahesh Salgaonkar
mem_reserved_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)1478d8e73458SSourabh Jain static ssize_t mem_reserved_show(struct kobject *kobj,
1479d8e73458SSourabh Jain struct kobj_attribute *attr,
1480d8e73458SSourabh Jain char *buf)
1481d8e73458SSourabh Jain {
1482d8e73458SSourabh Jain return sprintf(buf, "%ld\n", fw_dump.reserve_dump_area_size);
1483d8e73458SSourabh Jain }
1484d8e73458SSourabh Jain
registered_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)1485d418b19fSSourabh Jain static ssize_t registered_show(struct kobject *kobj,
14863ccc00a7SMahesh Salgaonkar struct kobj_attribute *attr,
14873ccc00a7SMahesh Salgaonkar char *buf)
14883ccc00a7SMahesh Salgaonkar {
14893ccc00a7SMahesh Salgaonkar return sprintf(buf, "%d\n", fw_dump.dump_registered);
14903ccc00a7SMahesh Salgaonkar }
14913ccc00a7SMahesh Salgaonkar
registered_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)1492d418b19fSSourabh Jain static ssize_t registered_store(struct kobject *kobj,
14933ccc00a7SMahesh Salgaonkar struct kobj_attribute *attr,
14943ccc00a7SMahesh Salgaonkar const char *buf, size_t count)
14953ccc00a7SMahesh Salgaonkar {
14963ccc00a7SMahesh Salgaonkar int ret = 0;
1497dcdc4679SMichal Suchanek int input = -1;
14983ccc00a7SMahesh Salgaonkar
1499f3512011SHari Bathini if (!fw_dump.fadump_enabled || fw_dump.dump_active)
15003ccc00a7SMahesh Salgaonkar return -EPERM;
15013ccc00a7SMahesh Salgaonkar
1502dcdc4679SMichal Suchanek if (kstrtoint(buf, 0, &input))
1503dcdc4679SMichal Suchanek return -EINVAL;
1504dcdc4679SMichal Suchanek
15053ccc00a7SMahesh Salgaonkar mutex_lock(&fadump_mutex);
15063ccc00a7SMahesh Salgaonkar
1507dcdc4679SMichal Suchanek switch (input) {
1508dcdc4679SMichal Suchanek case 0:
15093ccc00a7SMahesh Salgaonkar if (fw_dump.dump_registered == 0) {
15103ccc00a7SMahesh Salgaonkar goto unlock_out;
15113ccc00a7SMahesh Salgaonkar }
1512f3512011SHari Bathini
15133ccc00a7SMahesh Salgaonkar /* Un-register Firmware-assisted dump */
151441a65d16SHari Bathini pr_debug("Un-register firmware-assisted dump\n");
151541a65d16SHari Bathini fw_dump.ops->fadump_unregister(&fw_dump);
15163ccc00a7SMahesh Salgaonkar break;
1517dcdc4679SMichal Suchanek case 1:
15183ccc00a7SMahesh Salgaonkar if (fw_dump.dump_registered == 1) {
15190823c68bSHari Bathini /* Un-register Firmware-assisted dump */
152041a65d16SHari Bathini fw_dump.ops->fadump_unregister(&fw_dump);
15213ccc00a7SMahesh Salgaonkar }
15223ccc00a7SMahesh Salgaonkar /* Register Firmware-assisted dump */
152398b8cd7fSMichal Suchanek ret = register_fadump();
15243ccc00a7SMahesh Salgaonkar break;
15253ccc00a7SMahesh Salgaonkar default:
15263ccc00a7SMahesh Salgaonkar ret = -EINVAL;
15273ccc00a7SMahesh Salgaonkar break;
15283ccc00a7SMahesh Salgaonkar }
15293ccc00a7SMahesh Salgaonkar
15303ccc00a7SMahesh Salgaonkar unlock_out:
15313ccc00a7SMahesh Salgaonkar mutex_unlock(&fadump_mutex);
15323ccc00a7SMahesh Salgaonkar return ret < 0 ? ret : count;
15333ccc00a7SMahesh Salgaonkar }
15343ccc00a7SMahesh Salgaonkar
fadump_region_show(struct seq_file * m,void * private)15353ccc00a7SMahesh Salgaonkar static int fadump_region_show(struct seq_file *m, void *private)
15363ccc00a7SMahesh Salgaonkar {
15373ccc00a7SMahesh Salgaonkar if (!fw_dump.fadump_enabled)
15383ccc00a7SMahesh Salgaonkar return 0;
15393ccc00a7SMahesh Salgaonkar
1540b500afffSMahesh Salgaonkar mutex_lock(&fadump_mutex);
154141a65d16SHari Bathini fw_dump.ops->fadump_region_show(&fw_dump, m);
1542b500afffSMahesh Salgaonkar mutex_unlock(&fadump_mutex);
15433ccc00a7SMahesh Salgaonkar return 0;
15443ccc00a7SMahesh Salgaonkar }
15453ccc00a7SMahesh Salgaonkar
1546d418b19fSSourabh Jain static struct kobj_attribute release_attr = __ATTR_WO(release_mem);
1547d418b19fSSourabh Jain static struct kobj_attribute enable_attr = __ATTR_RO(enabled);
1548d418b19fSSourabh Jain static struct kobj_attribute register_attr = __ATTR_RW(registered);
1549d8e73458SSourabh Jain static struct kobj_attribute mem_reserved_attr = __ATTR_RO(mem_reserved);
1550d418b19fSSourabh Jain
1551d418b19fSSourabh Jain static struct attribute *fadump_attrs[] = {
1552d418b19fSSourabh Jain &enable_attr.attr,
1553d418b19fSSourabh Jain ®ister_attr.attr,
1554d8e73458SSourabh Jain &mem_reserved_attr.attr,
1555d418b19fSSourabh Jain NULL,
1556d418b19fSSourabh Jain };
1557d418b19fSSourabh Jain
1558d418b19fSSourabh Jain ATTRIBUTE_GROUPS(fadump);
15593ccc00a7SMahesh Salgaonkar
1560f6cee260SYangtao Li DEFINE_SHOW_ATTRIBUTE(fadump_region);
15613ccc00a7SMahesh Salgaonkar
fadump_init_files(void)1562d276960dSNick Child static void __init fadump_init_files(void)
15633ccc00a7SMahesh Salgaonkar {
15643ccc00a7SMahesh Salgaonkar int rc = 0;
15653ccc00a7SMahesh Salgaonkar
1566d418b19fSSourabh Jain fadump_kobj = kobject_create_and_add("fadump", kernel_kobj);
1567d418b19fSSourabh Jain if (!fadump_kobj) {
1568d418b19fSSourabh Jain pr_err("failed to create fadump kobject\n");
1569d418b19fSSourabh Jain return;
1570d418b19fSSourabh Jain }
1571860286cfSGreg Kroah-Hartman
1572dbf77fedSAneesh Kumar K.V debugfs_create_file("fadump_region", 0444, arch_debugfs_dir, NULL,
15733ccc00a7SMahesh Salgaonkar &fadump_region_fops);
1574b500afffSMahesh Salgaonkar
1575b500afffSMahesh Salgaonkar if (fw_dump.dump_active) {
1576d418b19fSSourabh Jain rc = sysfs_create_file(fadump_kobj, &release_attr.attr);
1577b500afffSMahesh Salgaonkar if (rc)
1578d418b19fSSourabh Jain pr_err("unable to create release_mem sysfs file (%d)\n",
1579d418b19fSSourabh Jain rc);
1580d418b19fSSourabh Jain }
1581d418b19fSSourabh Jain
1582d418b19fSSourabh Jain rc = sysfs_create_groups(fadump_kobj, fadump_groups);
1583d418b19fSSourabh Jain if (rc) {
1584d418b19fSSourabh Jain pr_err("sysfs group creation failed (%d), unregistering FADump",
1585d418b19fSSourabh Jain rc);
1586d418b19fSSourabh Jain unregister_fadump();
1587d418b19fSSourabh Jain return;
1588d418b19fSSourabh Jain }
1589d418b19fSSourabh Jain
1590d418b19fSSourabh Jain /*
1591d418b19fSSourabh Jain * The FADump sysfs are moved from kernel_kobj to fadump_kobj need to
1592d418b19fSSourabh Jain * create symlink at old location to maintain backward compatibility.
1593d418b19fSSourabh Jain *
1594d418b19fSSourabh Jain * - fadump_enabled -> fadump/enabled
1595d418b19fSSourabh Jain * - fadump_registered -> fadump/registered
1596d418b19fSSourabh Jain * - fadump_release_mem -> fadump/release_mem
1597d418b19fSSourabh Jain */
1598d418b19fSSourabh Jain rc = compat_only_sysfs_link_entry_to_kobj(kernel_kobj, fadump_kobj,
1599d418b19fSSourabh Jain "enabled", "fadump_enabled");
1600d418b19fSSourabh Jain if (rc) {
1601d418b19fSSourabh Jain pr_err("unable to create fadump_enabled symlink (%d)", rc);
1602d418b19fSSourabh Jain return;
1603d418b19fSSourabh Jain }
1604d418b19fSSourabh Jain
1605d418b19fSSourabh Jain rc = compat_only_sysfs_link_entry_to_kobj(kernel_kobj, fadump_kobj,
1606d418b19fSSourabh Jain "registered",
1607d418b19fSSourabh Jain "fadump_registered");
1608d418b19fSSourabh Jain if (rc) {
1609d418b19fSSourabh Jain pr_err("unable to create fadump_registered symlink (%d)", rc);
1610d418b19fSSourabh Jain sysfs_remove_link(kernel_kobj, "fadump_enabled");
1611d418b19fSSourabh Jain return;
1612d418b19fSSourabh Jain }
1613d418b19fSSourabh Jain
1614d418b19fSSourabh Jain if (fw_dump.dump_active) {
1615d418b19fSSourabh Jain rc = compat_only_sysfs_link_entry_to_kobj(kernel_kobj,
1616d418b19fSSourabh Jain fadump_kobj,
1617d418b19fSSourabh Jain "release_mem",
1618d418b19fSSourabh Jain "fadump_release_mem");
1619d418b19fSSourabh Jain if (rc)
1620d418b19fSSourabh Jain pr_err("unable to create fadump_release_mem symlink (%d)",
1621d418b19fSSourabh Jain rc);
1622b500afffSMahesh Salgaonkar }
16233ccc00a7SMahesh Salgaonkar return;
16243ccc00a7SMahesh Salgaonkar }
16253ccc00a7SMahesh Salgaonkar
16263ccc00a7SMahesh Salgaonkar /*
16273ccc00a7SMahesh Salgaonkar * Prepare for firmware-assisted dump.
16283ccc00a7SMahesh Salgaonkar */
setup_fadump(void)16293ccc00a7SMahesh Salgaonkar int __init setup_fadump(void)
16303ccc00a7SMahesh Salgaonkar {
1631565f9bc0SMichal Suchanek if (!fw_dump.fadump_supported)
16323ccc00a7SMahesh Salgaonkar return 0;
16333ccc00a7SMahesh Salgaonkar
1634565f9bc0SMichal Suchanek fadump_init_files();
16353ccc00a7SMahesh Salgaonkar fadump_show_config();
1636565f9bc0SMichal Suchanek
1637565f9bc0SMichal Suchanek if (!fw_dump.fadump_enabled)
1638565f9bc0SMichal Suchanek return 1;
1639565f9bc0SMichal Suchanek
16402df173d9SMahesh Salgaonkar /*
16412df173d9SMahesh Salgaonkar * If dump data is available then see if it is valid and prepare for
16422df173d9SMahesh Salgaonkar * saving it to the disk.
16432df173d9SMahesh Salgaonkar */
1644b500afffSMahesh Salgaonkar if (fw_dump.dump_active) {
1645b500afffSMahesh Salgaonkar /*
1646b500afffSMahesh Salgaonkar * if dump process fails then invalidate the registration
1647b500afffSMahesh Salgaonkar * and release memory before proceeding for re-registration.
1648b500afffSMahesh Salgaonkar */
1649f3512011SHari Bathini if (fw_dump.ops->fadump_process(&fw_dump) < 0)
1650b500afffSMahesh Salgaonkar fadump_invalidate_release_mem();
1651b500afffSMahesh Salgaonkar }
1652607451ceSHari Bathini /* Initialize the kernel dump memory structure and register with f/w */
1653607451ceSHari Bathini else if (fw_dump.reserve_dump_area_size) {
165441a65d16SHari Bathini fw_dump.ops->fadump_init_mem_struct(&fw_dump);
1655607451ceSHari Bathini register_fadump();
1656607451ceSHari Bathini }
1657f3512011SHari Bathini
165806e629c2SHari Bathini /*
165906e629c2SHari Bathini * In case of panic, fadump is triggered via ppc_panic_event()
166006e629c2SHari Bathini * panic notifier. Setting crash_kexec_post_notifiers to 'true'
166106e629c2SHari Bathini * lets panic() function take crash friendly path before panic
166206e629c2SHari Bathini * notifiers are invoked.
166306e629c2SHari Bathini */
166406e629c2SHari Bathini crash_kexec_post_notifiers = true;
166506e629c2SHari Bathini
16663ccc00a7SMahesh Salgaonkar return 1;
16673ccc00a7SMahesh Salgaonkar }
1668607451ceSHari Bathini /*
1669607451ceSHari Bathini * Use subsys_initcall_sync() here because there is dependency with
16701fd02f66SJulia Lawall * crash_save_vmcoreinfo_init(), which must run first to ensure vmcoreinfo initialization
16711fd02f66SJulia Lawall * is done before registering with f/w.
1672607451ceSHari Bathini */
1673607451ceSHari Bathini subsys_initcall_sync(setup_fadump);
1674bec53196SHari Bathini #else /* !CONFIG_PRESERVE_FA_DUMP */
1675bec53196SHari Bathini
1676bec53196SHari Bathini /* Scan the Firmware Assisted dump configuration details. */
early_init_dt_scan_fw_dump(unsigned long node,const char * uname,int depth,void * data)1677bec53196SHari Bathini int __init early_init_dt_scan_fw_dump(unsigned long node, const char *uname,
1678bec53196SHari Bathini int depth, void *data)
1679bec53196SHari Bathini {
1680bec53196SHari Bathini if ((depth != 1) || (strcmp(uname, "ibm,opal") != 0))
1681bec53196SHari Bathini return 0;
1682bec53196SHari Bathini
1683bec53196SHari Bathini opal_fadump_dt_scan(&fw_dump, node);
1684bec53196SHari Bathini return 1;
1685bec53196SHari Bathini }
1686bec53196SHari Bathini
1687bec53196SHari Bathini /*
1688bec53196SHari Bathini * When dump is active but PRESERVE_FA_DUMP is enabled on the kernel,
1689bec53196SHari Bathini * preserve crash data. The subsequent memory preserving kernel boot
1690bec53196SHari Bathini * is likely to process this crash data.
1691bec53196SHari Bathini */
fadump_reserve_mem(void)1692bec53196SHari Bathini int __init fadump_reserve_mem(void)
1693bec53196SHari Bathini {
1694bec53196SHari Bathini if (fw_dump.dump_active) {
1695bec53196SHari Bathini /*
1696bec53196SHari Bathini * If last boot has crashed then reserve all the memory
1697bec53196SHari Bathini * above boot memory to preserve crash data.
1698bec53196SHari Bathini */
1699bec53196SHari Bathini pr_info("Preserving crash data for processing in next boot.\n");
1700bec53196SHari Bathini fadump_reserve_crash_area(fw_dump.boot_mem_top);
1701bec53196SHari Bathini } else
1702bec53196SHari Bathini pr_debug("FADump-aware kernel..\n");
1703bec53196SHari Bathini
1704bec53196SHari Bathini return 1;
1705bec53196SHari Bathini }
1706bec53196SHari Bathini #endif /* CONFIG_PRESERVE_FA_DUMP */
1707b2a815a5SHari Bathini
1708b2a815a5SHari Bathini /* Preserve everything above the base address */
fadump_reserve_crash_area(u64 base)1709b2a815a5SHari Bathini static void __init fadump_reserve_crash_area(u64 base)
1710b2a815a5SHari Bathini {
1711b10d6bcaSMike Rapoport u64 i, mstart, mend, msize;
1712b2a815a5SHari Bathini
1713b10d6bcaSMike Rapoport for_each_mem_range(i, &mstart, &mend) {
1714b10d6bcaSMike Rapoport msize = mend - mstart;
1715b2a815a5SHari Bathini
1716b2a815a5SHari Bathini if ((mstart + msize) < base)
1717b2a815a5SHari Bathini continue;
1718b2a815a5SHari Bathini
1719b2a815a5SHari Bathini if (mstart < base) {
1720b2a815a5SHari Bathini msize -= (base - mstart);
1721b2a815a5SHari Bathini mstart = base;
1722b2a815a5SHari Bathini }
1723b2a815a5SHari Bathini
1724b2a815a5SHari Bathini pr_info("Reserving %lluMB of memory at %#016llx for preserving crash data",
1725b2a815a5SHari Bathini (msize >> 20), mstart);
1726b2a815a5SHari Bathini memblock_reserve(mstart, msize);
1727b2a815a5SHari Bathini }
1728b2a815a5SHari Bathini }
1729bec53196SHari Bathini
arch_reserved_kernel_pages(void)1730bec53196SHari Bathini unsigned long __init arch_reserved_kernel_pages(void)
1731bec53196SHari Bathini {
1732bec53196SHari Bathini return memblock_reserved_size() / PAGE_SIZE;
1733bec53196SHari Bathini }
1734