xref: /openbmc/linux/arch/powerpc/kernel/fadump.c (revision cbd3d5ba46b68c033986a6087209930f001cbcca)
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>
27eb39c880SMahesh Salgaonkar 
287644d581SMichael Ellerman #include <asm/debugfs.h>
29eb39c880SMahesh Salgaonkar #include <asm/page.h>
30eb39c880SMahesh Salgaonkar #include <asm/prom.h>
31eb39c880SMahesh Salgaonkar #include <asm/fadump.h>
32ca986d7fSHari Bathini #include <asm/fadump-internal.h>
33cad3c834SStephen Rothwell #include <asm/setup.h>
34*cbd3d5baSMichael Ellerman #include <asm/interrupt.h>
35eb39c880SMahesh Salgaonkar 
36ba608c4fSSourabh Jain /*
37ba608c4fSSourabh Jain  * The CPU who acquired the lock to trigger the fadump crash should
38ba608c4fSSourabh Jain  * wait for other CPUs to enter.
39ba608c4fSSourabh Jain  *
40ba608c4fSSourabh Jain  * The timeout is in milliseconds.
41ba608c4fSSourabh Jain  */
42ba608c4fSSourabh Jain #define CRASH_TIMEOUT		500
43ba608c4fSSourabh Jain 
44eb39c880SMahesh Salgaonkar static struct fw_dump fw_dump;
453ccc00a7SMahesh Salgaonkar 
46b2a815a5SHari Bathini static void __init fadump_reserve_crash_area(u64 base);
47b2a815a5SHari Bathini 
48d418b19fSSourabh Jain struct kobject *fadump_kobj;
49d418b19fSSourabh Jain 
50bec53196SHari Bathini #ifndef CONFIG_PRESERVE_FA_DUMP
515f987caeSMichael Ellerman 
525f987caeSMichael Ellerman static atomic_t cpus_in_fadump;
533ccc00a7SMahesh Salgaonkar static DEFINE_MUTEX(fadump_mutex);
545f987caeSMichael Ellerman 
5502c04e37SHari Bathini struct fadump_mrange_info crash_mrange_info = { "crash", NULL, 0, 0, 0, false };
5602c04e37SHari Bathini 
5702c04e37SHari Bathini #define RESERVED_RNGS_SZ	16384 /* 16K - 128 entries */
5802c04e37SHari Bathini #define RESERVED_RNGS_CNT	(RESERVED_RNGS_SZ / \
5902c04e37SHari Bathini 				 sizeof(struct fadump_memory_range))
6002c04e37SHari Bathini static struct fadump_memory_range rngs[RESERVED_RNGS_CNT];
6102c04e37SHari Bathini struct fadump_mrange_info reserved_mrange_info = { "reserved", rngs,
6202c04e37SHari Bathini 						   RESERVED_RNGS_SZ, 0,
6302c04e37SHari Bathini 						   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.
77a4e92ce8SMahesh Salgaonkar  * The reamining portion of fadump reserved memory will be not given
78a4e92ce8SMahesh Salgaonkar  * to CMA and pages for thoes 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  */
83a4e92ce8SMahesh Salgaonkar int __init fadump_cma_init(void)
84a4e92ce8SMahesh Salgaonkar {
85a4e92ce8SMahesh Salgaonkar 	unsigned long long base, size;
86a4e92ce8SMahesh Salgaonkar 	int rc;
87a4e92ce8SMahesh Salgaonkar 
88a4e92ce8SMahesh Salgaonkar 	if (!fw_dump.fadump_enabled)
89a4e92ce8SMahesh Salgaonkar 		return 0;
90a4e92ce8SMahesh Salgaonkar 
91a4e92ce8SMahesh Salgaonkar 	/*
92a4e92ce8SMahesh Salgaonkar 	 * Do not use CMA if user has provided fadump=nocma kernel parameter.
93a4e92ce8SMahesh Salgaonkar 	 * Return 1 to continue with fadump old behaviour.
94a4e92ce8SMahesh Salgaonkar 	 */
95a4e92ce8SMahesh Salgaonkar 	if (fw_dump.nocma)
96a4e92ce8SMahesh Salgaonkar 		return 1;
97a4e92ce8SMahesh Salgaonkar 
98a4e92ce8SMahesh Salgaonkar 	base = fw_dump.reserve_dump_area_start;
99a4e92ce8SMahesh Salgaonkar 	size = fw_dump.boot_memory_size;
100a4e92ce8SMahesh Salgaonkar 
101a4e92ce8SMahesh Salgaonkar 	if (!size)
102a4e92ce8SMahesh Salgaonkar 		return 0;
103a4e92ce8SMahesh Salgaonkar 
104a4e92ce8SMahesh Salgaonkar 	rc = cma_init_reserved_mem(base, size, 0, "fadump_cma", &fadump_cma);
105a4e92ce8SMahesh Salgaonkar 	if (rc) {
106a4e92ce8SMahesh Salgaonkar 		pr_err("Failed to init cma area for firmware-assisted dump,%d\n", rc);
107a4e92ce8SMahesh Salgaonkar 		/*
108a4e92ce8SMahesh Salgaonkar 		 * Though the CMA init has failed we still have memory
109a4e92ce8SMahesh Salgaonkar 		 * reservation with us. The reserved memory will be
110a4e92ce8SMahesh Salgaonkar 		 * blocked from production system usage.  Hence return 1,
111a4e92ce8SMahesh Salgaonkar 		 * so that we can continue with fadump.
112a4e92ce8SMahesh Salgaonkar 		 */
113a4e92ce8SMahesh Salgaonkar 		return 1;
114a4e92ce8SMahesh Salgaonkar 	}
115a4e92ce8SMahesh Salgaonkar 
116a4e92ce8SMahesh Salgaonkar 	/*
117a4e92ce8SMahesh Salgaonkar 	 * So we now have successfully initialized cma area for fadump.
118a4e92ce8SMahesh Salgaonkar 	 */
119a4e92ce8SMahesh Salgaonkar 	pr_info("Initialized 0x%lx bytes cma area at %ldMB from 0x%lx "
120a4e92ce8SMahesh Salgaonkar 		"bytes of memory reserved for firmware-assisted dump\n",
121a4e92ce8SMahesh Salgaonkar 		cma_get_size(fadump_cma),
122a4e92ce8SMahesh Salgaonkar 		(unsigned long)cma_get_base(fadump_cma) >> 20,
123a4e92ce8SMahesh Salgaonkar 		fw_dump.reserve_dump_area_size);
124a4e92ce8SMahesh Salgaonkar 	return 1;
125a4e92ce8SMahesh Salgaonkar }
126a4e92ce8SMahesh Salgaonkar #else
127a4e92ce8SMahesh Salgaonkar static int __init fadump_cma_init(void) { return 1; }
128a4e92ce8SMahesh Salgaonkar #endif /* CONFIG_CMA */
129a4e92ce8SMahesh Salgaonkar 
130eb39c880SMahesh Salgaonkar /* Scan the Firmware Assisted dump configuration details. */
131f3512011SHari Bathini int __init early_init_dt_scan_fw_dump(unsigned long node, const char *uname,
132f3512011SHari Bathini 				      int depth, void *data)
133eb39c880SMahesh Salgaonkar {
13402c04e37SHari Bathini 	if (depth == 0) {
13502c04e37SHari Bathini 		early_init_dt_scan_reserved_ranges(node);
13602c04e37SHari Bathini 		return 0;
13702c04e37SHari Bathini 	}
13802c04e37SHari Bathini 
13941df5928SHari Bathini 	if (depth != 1)
140eb39c880SMahesh Salgaonkar 		return 0;
141eb39c880SMahesh Salgaonkar 
14241df5928SHari Bathini 	if (strcmp(uname, "rtas") == 0) {
143d3833a70SHari Bathini 		rtas_fadump_dt_scan(&fw_dump, node);
144eb39c880SMahesh Salgaonkar 		return 1;
145eb39c880SMahesh Salgaonkar 	}
146eb39c880SMahesh Salgaonkar 
14741df5928SHari Bathini 	if (strcmp(uname, "ibm,opal") == 0) {
14841df5928SHari Bathini 		opal_fadump_dt_scan(&fw_dump, node);
14941df5928SHari Bathini 		return 1;
15041df5928SHari Bathini 	}
15141df5928SHari Bathini 
15241df5928SHari Bathini 	return 0;
15341df5928SHari Bathini }
15441df5928SHari Bathini 
155eae0dfccSHari Bathini /*
156eae0dfccSHari Bathini  * If fadump is registered, check if the memory provided
1570db6896fSMahesh Salgaonkar  * falls within boot memory area and reserved memory area.
158eae0dfccSHari Bathini  */
159becd91d9SHari Bathini int is_fadump_memory_area(u64 addr, unsigned long size)
160eae0dfccSHari Bathini {
161becd91d9SHari Bathini 	u64 d_start, d_end;
1620db6896fSMahesh Salgaonkar 
163eae0dfccSHari Bathini 	if (!fw_dump.dump_registered)
164eae0dfccSHari Bathini 		return 0;
165eae0dfccSHari Bathini 
166becd91d9SHari Bathini 	if (!size)
167becd91d9SHari Bathini 		return 0;
168becd91d9SHari Bathini 
169becd91d9SHari Bathini 	d_start = fw_dump.reserve_dump_area_start;
170becd91d9SHari Bathini 	d_end = d_start + fw_dump.reserve_dump_area_size;
1710db6896fSMahesh Salgaonkar 	if (((addr + size) > d_start) && (addr <= d_end))
1720db6896fSMahesh Salgaonkar 		return 1;
1730db6896fSMahesh Salgaonkar 
1747dee93a9SHari Bathini 	return (addr <= fw_dump.boot_mem_top);
175eae0dfccSHari Bathini }
176eae0dfccSHari Bathini 
1776fcd6baaSNicholas Piggin int should_fadump_crash(void)
1786fcd6baaSNicholas Piggin {
1796fcd6baaSNicholas Piggin 	if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr)
1806fcd6baaSNicholas Piggin 		return 0;
1816fcd6baaSNicholas Piggin 	return 1;
1826fcd6baaSNicholas Piggin }
1836fcd6baaSNicholas Piggin 
1843ccc00a7SMahesh Salgaonkar int is_fadump_active(void)
1853ccc00a7SMahesh Salgaonkar {
1863ccc00a7SMahesh Salgaonkar 	return fw_dump.dump_active;
1873ccc00a7SMahesh Salgaonkar }
1883ccc00a7SMahesh Salgaonkar 
189a5a05b91SHari Bathini /*
190961cf26aSHari Bathini  * Returns true, if there are no holes in memory area between d_start to d_end,
191961cf26aSHari Bathini  * false otherwise.
192a5a05b91SHari Bathini  */
193961cf26aSHari Bathini static bool is_fadump_mem_area_contiguous(u64 d_start, u64 d_end)
194a5a05b91SHari Bathini {
195b10d6bcaSMike Rapoport 	phys_addr_t reg_start, reg_end;
196961cf26aSHari Bathini 	bool ret = false;
197b10d6bcaSMike Rapoport 	u64 i, start, end;
198a5a05b91SHari Bathini 
199b10d6bcaSMike Rapoport 	for_each_mem_range(i, &reg_start, &reg_end) {
200b10d6bcaSMike Rapoport 		start = max_t(u64, d_start, reg_start);
201b10d6bcaSMike Rapoport 		end = min_t(u64, d_end, reg_end);
202961cf26aSHari Bathini 		if (d_start < end) {
203961cf26aSHari Bathini 			/* Memory hole from d_start to start */
204961cf26aSHari Bathini 			if (start > d_start)
205a5a05b91SHari Bathini 				break;
206a5a05b91SHari Bathini 
207961cf26aSHari Bathini 			if (end == d_end) {
208961cf26aSHari Bathini 				ret = true;
209a5a05b91SHari Bathini 				break;
210a5a05b91SHari Bathini 			}
211a5a05b91SHari Bathini 
212961cf26aSHari Bathini 			d_start = end + 1;
213a5a05b91SHari Bathini 		}
214a5a05b91SHari Bathini 	}
215a5a05b91SHari Bathini 
216a5a05b91SHari Bathini 	return ret;
217a5a05b91SHari Bathini }
218a5a05b91SHari Bathini 
219f86593beSMahesh Salgaonkar /*
220961cf26aSHari Bathini  * Returns true, if there are no holes in boot memory area,
221961cf26aSHari Bathini  * false otherwise.
222961cf26aSHari Bathini  */
2237f0ad11dSHari Bathini bool is_fadump_boot_mem_contiguous(void)
224961cf26aSHari Bathini {
2257dee93a9SHari Bathini 	unsigned long d_start, d_end;
2267dee93a9SHari Bathini 	bool ret = false;
2277dee93a9SHari Bathini 	int i;
2287dee93a9SHari Bathini 
2297dee93a9SHari Bathini 	for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) {
2307dee93a9SHari Bathini 		d_start = fw_dump.boot_mem_addr[i];
2317dee93a9SHari Bathini 		d_end   = d_start + fw_dump.boot_mem_sz[i];
2327dee93a9SHari Bathini 
2337dee93a9SHari Bathini 		ret = is_fadump_mem_area_contiguous(d_start, d_end);
2347dee93a9SHari Bathini 		if (!ret)
2357dee93a9SHari Bathini 			break;
2367dee93a9SHari Bathini 	}
2377dee93a9SHari Bathini 
2387dee93a9SHari Bathini 	return ret;
239961cf26aSHari Bathini }
240961cf26aSHari Bathini 
241961cf26aSHari Bathini /*
242f86593beSMahesh Salgaonkar  * Returns true, if there are no holes in reserved memory area,
243f86593beSMahesh Salgaonkar  * false otherwise.
244f86593beSMahesh Salgaonkar  */
2457f0ad11dSHari Bathini bool is_fadump_reserved_mem_contiguous(void)
246f86593beSMahesh Salgaonkar {
247961cf26aSHari Bathini 	u64 d_start, d_end;
248f86593beSMahesh Salgaonkar 
249961cf26aSHari Bathini 	d_start	= fw_dump.reserve_dump_area_start;
250961cf26aSHari Bathini 	d_end	= d_start + fw_dump.reserve_dump_area_size;
251961cf26aSHari Bathini 	return is_fadump_mem_area_contiguous(d_start, d_end);
252f86593beSMahesh Salgaonkar }
253f86593beSMahesh Salgaonkar 
2543ccc00a7SMahesh Salgaonkar /* Print firmware assisted dump configurations for debugging purpose. */
2553ccc00a7SMahesh Salgaonkar static void fadump_show_config(void)
2563ccc00a7SMahesh Salgaonkar {
2577dee93a9SHari Bathini 	int i;
2587dee93a9SHari Bathini 
2593ccc00a7SMahesh Salgaonkar 	pr_debug("Support for firmware-assisted dump (fadump): %s\n",
2603ccc00a7SMahesh Salgaonkar 			(fw_dump.fadump_supported ? "present" : "no support"));
2613ccc00a7SMahesh Salgaonkar 
2623ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_supported)
2633ccc00a7SMahesh Salgaonkar 		return;
2643ccc00a7SMahesh Salgaonkar 
2653ccc00a7SMahesh Salgaonkar 	pr_debug("Fadump enabled    : %s\n",
2663ccc00a7SMahesh Salgaonkar 				(fw_dump.fadump_enabled ? "yes" : "no"));
2673ccc00a7SMahesh Salgaonkar 	pr_debug("Dump Active       : %s\n",
2683ccc00a7SMahesh Salgaonkar 				(fw_dump.dump_active ? "yes" : "no"));
2693ccc00a7SMahesh Salgaonkar 	pr_debug("Dump section sizes:\n");
2703ccc00a7SMahesh Salgaonkar 	pr_debug("    CPU state data size: %lx\n", fw_dump.cpu_state_data_size);
2713ccc00a7SMahesh Salgaonkar 	pr_debug("    HPTE region size   : %lx\n", fw_dump.hpte_region_size);
2723ccc00a7SMahesh Salgaonkar 	pr_debug("    Boot memory size   : %lx\n", fw_dump.boot_memory_size);
2737dee93a9SHari Bathini 	pr_debug("    Boot memory top    : %llx\n", fw_dump.boot_mem_top);
2747dee93a9SHari Bathini 	pr_debug("Boot memory regions cnt: %llx\n", fw_dump.boot_mem_regs_cnt);
2757dee93a9SHari Bathini 	for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) {
2767dee93a9SHari Bathini 		pr_debug("[%03d] base = %llx, size = %llx\n", i,
2777dee93a9SHari Bathini 			 fw_dump.boot_mem_addr[i], fw_dump.boot_mem_sz[i]);
2787dee93a9SHari Bathini 	}
2793ccc00a7SMahesh Salgaonkar }
2803ccc00a7SMahesh Salgaonkar 
281eb39c880SMahesh Salgaonkar /**
282eb39c880SMahesh Salgaonkar  * fadump_calculate_reserve_size(): reserve variable boot area 5% of System RAM
283eb39c880SMahesh Salgaonkar  *
284eb39c880SMahesh Salgaonkar  * Function to find the largest memory size we need to reserve during early
285eb39c880SMahesh Salgaonkar  * boot process. This will be the size of the memory that is required for a
286eb39c880SMahesh Salgaonkar  * kernel to boot successfully.
287eb39c880SMahesh Salgaonkar  *
288eb39c880SMahesh Salgaonkar  * This function has been taken from phyp-assisted dump feature implementation.
289eb39c880SMahesh Salgaonkar  *
290eb39c880SMahesh Salgaonkar  * returns larger of 256MB or 5% rounded down to multiples of 256MB.
291eb39c880SMahesh Salgaonkar  *
292eb39c880SMahesh Salgaonkar  * TODO: Come up with better approach to find out more accurate memory size
293eb39c880SMahesh Salgaonkar  * that is required for a kernel to boot successfully.
294eb39c880SMahesh Salgaonkar  *
295eb39c880SMahesh Salgaonkar  */
296fbced154SNathan Chancellor static __init u64 fadump_calculate_reserve_size(void)
297eb39c880SMahesh Salgaonkar {
2987b1b3b48SHari Bathini 	u64 base, size, bootmem_min;
29911550dc0SHari Bathini 	int ret;
300eb39c880SMahesh Salgaonkar 
30181d9eca5SHari Bathini 	if (fw_dump.reserve_bootvar)
30281d9eca5SHari Bathini 		pr_warn("'fadump_reserve_mem=' parameter is deprecated in favor of 'crashkernel=' parameter.\n");
30381d9eca5SHari Bathini 
304eb39c880SMahesh Salgaonkar 	/*
30511550dc0SHari Bathini 	 * Check if the size is specified through crashkernel= cmdline
306e7467dc6SHari Bathini 	 * option. If yes, then use that but ignore base as fadump reserves
307e7467dc6SHari Bathini 	 * memory at a predefined offset.
308eb39c880SMahesh Salgaonkar 	 */
30911550dc0SHari Bathini 	ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
31011550dc0SHari Bathini 				&size, &base);
31111550dc0SHari Bathini 	if (ret == 0 && size > 0) {
31248a316e3SHari Bathini 		unsigned long max_size;
31348a316e3SHari Bathini 
31481d9eca5SHari Bathini 		if (fw_dump.reserve_bootvar)
31581d9eca5SHari Bathini 			pr_info("Using 'crashkernel=' parameter for memory reservation.\n");
31681d9eca5SHari Bathini 
31711550dc0SHari Bathini 		fw_dump.reserve_bootvar = (unsigned long)size;
31848a316e3SHari Bathini 
31948a316e3SHari Bathini 		/*
32048a316e3SHari Bathini 		 * Adjust if the boot memory size specified is above
32148a316e3SHari Bathini 		 * the upper limit.
32248a316e3SHari Bathini 		 */
32348a316e3SHari Bathini 		max_size = memblock_phys_mem_size() / MAX_BOOT_MEM_RATIO;
32448a316e3SHari Bathini 		if (fw_dump.reserve_bootvar > max_size) {
32548a316e3SHari Bathini 			fw_dump.reserve_bootvar = max_size;
32648a316e3SHari Bathini 			pr_info("Adjusted boot memory size to %luMB\n",
32748a316e3SHari Bathini 				(fw_dump.reserve_bootvar >> 20));
32848a316e3SHari Bathini 		}
32948a316e3SHari Bathini 
330eb39c880SMahesh Salgaonkar 		return fw_dump.reserve_bootvar;
33181d9eca5SHari Bathini 	} else if (fw_dump.reserve_bootvar) {
33281d9eca5SHari Bathini 		/*
33381d9eca5SHari Bathini 		 * 'fadump_reserve_mem=' is being used to reserve memory
33481d9eca5SHari Bathini 		 * for firmware-assisted dump.
33581d9eca5SHari Bathini 		 */
33681d9eca5SHari Bathini 		return fw_dump.reserve_bootvar;
33711550dc0SHari Bathini 	}
338eb39c880SMahesh Salgaonkar 
339eb39c880SMahesh Salgaonkar 	/* divide by 20 to get 5% of value */
34048a316e3SHari Bathini 	size = memblock_phys_mem_size() / 20;
341eb39c880SMahesh Salgaonkar 
342eb39c880SMahesh Salgaonkar 	/* round it down in multiples of 256 */
343eb39c880SMahesh Salgaonkar 	size = size & ~0x0FFFFFFFUL;
344eb39c880SMahesh Salgaonkar 
345eb39c880SMahesh Salgaonkar 	/* Truncate to memory_limit. We don't want to over reserve the memory.*/
346eb39c880SMahesh Salgaonkar 	if (memory_limit && size > memory_limit)
347eb39c880SMahesh Salgaonkar 		size = memory_limit;
348eb39c880SMahesh Salgaonkar 
3497b1b3b48SHari Bathini 	bootmem_min = fw_dump.ops->fadump_get_bootmem_min();
3507b1b3b48SHari Bathini 	return (size > bootmem_min ? size : bootmem_min);
351eb39c880SMahesh Salgaonkar }
352eb39c880SMahesh Salgaonkar 
353eb39c880SMahesh Salgaonkar /*
354eb39c880SMahesh Salgaonkar  * Calculate the total memory size required to be reserved for
355eb39c880SMahesh Salgaonkar  * firmware-assisted dump registration.
356eb39c880SMahesh Salgaonkar  */
357eb39c880SMahesh Salgaonkar static unsigned long get_fadump_area_size(void)
358eb39c880SMahesh Salgaonkar {
359eb39c880SMahesh Salgaonkar 	unsigned long size = 0;
360eb39c880SMahesh Salgaonkar 
361eb39c880SMahesh Salgaonkar 	size += fw_dump.cpu_state_data_size;
362eb39c880SMahesh Salgaonkar 	size += fw_dump.hpte_region_size;
363eb39c880SMahesh Salgaonkar 	size += fw_dump.boot_memory_size;
3642df173d9SMahesh Salgaonkar 	size += sizeof(struct fadump_crash_info_header);
3652df173d9SMahesh Salgaonkar 	size += sizeof(struct elfhdr); /* ELF core header.*/
366ebaeb5aeSMahesh Salgaonkar 	size += sizeof(struct elf_phdr); /* place holder for cpu notes */
3672df173d9SMahesh Salgaonkar 	/* Program headers for crash memory regions. */
3682df173d9SMahesh Salgaonkar 	size += sizeof(struct elf_phdr) * (memblock_num_regions(memory) + 2);
369eb39c880SMahesh Salgaonkar 
370eb39c880SMahesh Salgaonkar 	size = PAGE_ALIGN(size);
371742a265aSHari Bathini 
372742a265aSHari Bathini 	/* This is to hold kernel metadata on platforms that support it */
373742a265aSHari Bathini 	size += (fw_dump.ops->fadump_get_metadata_size ?
374742a265aSHari Bathini 		 fw_dump.ops->fadump_get_metadata_size() : 0);
375eb39c880SMahesh Salgaonkar 	return size;
376eb39c880SMahesh Salgaonkar }
377eb39c880SMahesh Salgaonkar 
3787dee93a9SHari Bathini static int __init add_boot_mem_region(unsigned long rstart,
3797dee93a9SHari Bathini 				      unsigned long rsize)
3807dee93a9SHari Bathini {
3817dee93a9SHari Bathini 	int i = fw_dump.boot_mem_regs_cnt++;
3827dee93a9SHari Bathini 
3837dee93a9SHari Bathini 	if (fw_dump.boot_mem_regs_cnt > FADUMP_MAX_MEM_REGS) {
3847dee93a9SHari Bathini 		fw_dump.boot_mem_regs_cnt = FADUMP_MAX_MEM_REGS;
3857dee93a9SHari Bathini 		return 0;
3867dee93a9SHari Bathini 	}
3877dee93a9SHari Bathini 
3887dee93a9SHari Bathini 	pr_debug("Added boot memory range[%d] [%#016lx-%#016lx)\n",
3897dee93a9SHari Bathini 		 i, rstart, (rstart + rsize));
3907dee93a9SHari Bathini 	fw_dump.boot_mem_addr[i] = rstart;
3917dee93a9SHari Bathini 	fw_dump.boot_mem_sz[i] = rsize;
3927dee93a9SHari Bathini 	return 1;
3937dee93a9SHari Bathini }
3947dee93a9SHari Bathini 
3957dee93a9SHari Bathini /*
3967dee93a9SHari Bathini  * Firmware usually has a hard limit on the data it can copy per region.
3977dee93a9SHari Bathini  * Honour that by splitting a memory range into multiple regions.
3987dee93a9SHari Bathini  */
3997dee93a9SHari Bathini static int __init add_boot_mem_regions(unsigned long mstart,
4007dee93a9SHari Bathini 				       unsigned long msize)
4017dee93a9SHari Bathini {
4027dee93a9SHari Bathini 	unsigned long rstart, rsize, max_size;
4037dee93a9SHari Bathini 	int ret = 1;
4047dee93a9SHari Bathini 
4057dee93a9SHari Bathini 	rstart = mstart;
4067dee93a9SHari Bathini 	max_size = fw_dump.max_copy_size ? fw_dump.max_copy_size : msize;
4077dee93a9SHari Bathini 	while (msize) {
4087dee93a9SHari Bathini 		if (msize > max_size)
4097dee93a9SHari Bathini 			rsize = max_size;
4107dee93a9SHari Bathini 		else
4117dee93a9SHari Bathini 			rsize = msize;
4127dee93a9SHari Bathini 
4137dee93a9SHari Bathini 		ret = add_boot_mem_region(rstart, rsize);
4147dee93a9SHari Bathini 		if (!ret)
4157dee93a9SHari Bathini 			break;
4167dee93a9SHari Bathini 
4177dee93a9SHari Bathini 		msize -= rsize;
4187dee93a9SHari Bathini 		rstart += rsize;
4197dee93a9SHari Bathini 	}
4207dee93a9SHari Bathini 
4217dee93a9SHari Bathini 	return ret;
4227dee93a9SHari Bathini }
4237dee93a9SHari Bathini 
4247dee93a9SHari Bathini static int __init fadump_get_boot_mem_regions(void)
4257dee93a9SHari Bathini {
426b10d6bcaSMike Rapoport 	unsigned long size, cur_size, hole_size, last_end;
4277dee93a9SHari Bathini 	unsigned long mem_size = fw_dump.boot_memory_size;
428b10d6bcaSMike Rapoport 	phys_addr_t reg_start, reg_end;
4297dee93a9SHari Bathini 	int ret = 1;
430b10d6bcaSMike Rapoport 	u64 i;
4317dee93a9SHari Bathini 
4327dee93a9SHari Bathini 	fw_dump.boot_mem_regs_cnt = 0;
4337dee93a9SHari Bathini 
4347dee93a9SHari Bathini 	last_end = 0;
4357dee93a9SHari Bathini 	hole_size = 0;
4367dee93a9SHari Bathini 	cur_size = 0;
437b10d6bcaSMike Rapoport 	for_each_mem_range(i, &reg_start, &reg_end) {
438b10d6bcaSMike Rapoport 		size = reg_end - reg_start;
439b10d6bcaSMike Rapoport 		hole_size += (reg_start - last_end);
4407dee93a9SHari Bathini 
4417dee93a9SHari Bathini 		if ((cur_size + size) >= mem_size) {
4427dee93a9SHari Bathini 			size = (mem_size - cur_size);
443b10d6bcaSMike Rapoport 			ret = add_boot_mem_regions(reg_start, size);
4447dee93a9SHari Bathini 			break;
4457dee93a9SHari Bathini 		}
4467dee93a9SHari Bathini 
4477dee93a9SHari Bathini 		mem_size -= size;
4487dee93a9SHari Bathini 		cur_size += size;
449b10d6bcaSMike Rapoport 		ret = add_boot_mem_regions(reg_start, size);
4507dee93a9SHari Bathini 		if (!ret)
4517dee93a9SHari Bathini 			break;
4527dee93a9SHari Bathini 
453b10d6bcaSMike Rapoport 		last_end = reg_end;
4547dee93a9SHari Bathini 	}
4557dee93a9SHari Bathini 	fw_dump.boot_mem_top = PAGE_ALIGN(fw_dump.boot_memory_size + hole_size);
4567dee93a9SHari Bathini 
4577dee93a9SHari Bathini 	return ret;
4587dee93a9SHari Bathini }
4597dee93a9SHari Bathini 
460140777a3SHari Bathini /*
461140777a3SHari Bathini  * Returns true, if the given range overlaps with reserved memory ranges
462140777a3SHari Bathini  * starting at idx. Also, updates idx to index of overlapping memory range
463140777a3SHari Bathini  * with the given memory range.
464140777a3SHari Bathini  * False, otherwise.
465140777a3SHari Bathini  */
466140777a3SHari Bathini static bool overlaps_reserved_ranges(u64 base, u64 end, int *idx)
467140777a3SHari Bathini {
468140777a3SHari Bathini 	bool ret = false;
469140777a3SHari Bathini 	int i;
470140777a3SHari Bathini 
471140777a3SHari Bathini 	for (i = *idx; i < reserved_mrange_info.mem_range_cnt; i++) {
472140777a3SHari Bathini 		u64 rbase = reserved_mrange_info.mem_ranges[i].base;
473140777a3SHari Bathini 		u64 rend = rbase + reserved_mrange_info.mem_ranges[i].size;
474140777a3SHari Bathini 
475140777a3SHari Bathini 		if (end <= rbase)
476140777a3SHari Bathini 			break;
477140777a3SHari Bathini 
478140777a3SHari Bathini 		if ((end > rbase) &&  (base < rend)) {
479140777a3SHari Bathini 			*idx = i;
480140777a3SHari Bathini 			ret = true;
481140777a3SHari Bathini 			break;
482140777a3SHari Bathini 		}
483140777a3SHari Bathini 	}
484140777a3SHari Bathini 
485140777a3SHari Bathini 	return ret;
486140777a3SHari Bathini }
487140777a3SHari Bathini 
488140777a3SHari Bathini /*
489140777a3SHari Bathini  * Locate a suitable memory area to reserve memory for FADump. While at it,
490140777a3SHari Bathini  * lookup reserved-ranges & avoid overlap with them, as they are used by F/W.
491140777a3SHari Bathini  */
492140777a3SHari Bathini static u64 __init fadump_locate_reserve_mem(u64 base, u64 size)
493140777a3SHari Bathini {
494140777a3SHari Bathini 	struct fadump_memory_range *mrngs;
495140777a3SHari Bathini 	phys_addr_t mstart, mend;
496140777a3SHari Bathini 	int idx = 0;
497140777a3SHari Bathini 	u64 i, ret = 0;
498140777a3SHari Bathini 
499140777a3SHari Bathini 	mrngs = reserved_mrange_info.mem_ranges;
500140777a3SHari Bathini 	for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE,
501140777a3SHari Bathini 				&mstart, &mend, NULL) {
502140777a3SHari Bathini 		pr_debug("%llu) mstart: %llx, mend: %llx, base: %llx\n",
503140777a3SHari Bathini 			 i, mstart, mend, base);
504140777a3SHari Bathini 
505140777a3SHari Bathini 		if (mstart > base)
506140777a3SHari Bathini 			base = PAGE_ALIGN(mstart);
507140777a3SHari Bathini 
508140777a3SHari Bathini 		while ((mend > base) && ((mend - base) >= size)) {
509140777a3SHari Bathini 			if (!overlaps_reserved_ranges(base, base+size, &idx)) {
510140777a3SHari Bathini 				ret = base;
511140777a3SHari Bathini 				goto out;
512140777a3SHari Bathini 			}
513140777a3SHari Bathini 
514140777a3SHari Bathini 			base = mrngs[idx].base + mrngs[idx].size;
515140777a3SHari Bathini 			base = PAGE_ALIGN(base);
516140777a3SHari Bathini 		}
517140777a3SHari Bathini 	}
518140777a3SHari Bathini 
519140777a3SHari Bathini out:
520140777a3SHari Bathini 	return ret;
521140777a3SHari Bathini }
522140777a3SHari Bathini 
523eb39c880SMahesh Salgaonkar int __init fadump_reserve_mem(void)
524eb39c880SMahesh Salgaonkar {
525140777a3SHari Bathini 	u64 base, size, mem_boundary, bootmem_min;
5266abec12cSHari Bathini 	int ret = 1;
527eb39c880SMahesh Salgaonkar 
528eb39c880SMahesh Salgaonkar 	if (!fw_dump.fadump_enabled)
529eb39c880SMahesh Salgaonkar 		return 0;
530eb39c880SMahesh Salgaonkar 
531eb39c880SMahesh Salgaonkar 	if (!fw_dump.fadump_supported) {
5326abec12cSHari Bathini 		pr_info("Firmware-Assisted Dump is not supported on this hardware\n");
5336abec12cSHari Bathini 		goto error_out;
534eb39c880SMahesh Salgaonkar 	}
535742a265aSHari Bathini 
5363ccc00a7SMahesh Salgaonkar 	/*
5373ccc00a7SMahesh Salgaonkar 	 * Initialize boot memory size
5383ccc00a7SMahesh Salgaonkar 	 * If dump is active then we have already calculated the size during
5393ccc00a7SMahesh Salgaonkar 	 * first kernel.
5403ccc00a7SMahesh Salgaonkar 	 */
541f3512011SHari Bathini 	if (!fw_dump.dump_active) {
5426abec12cSHari Bathini 		fw_dump.boot_memory_size =
5436abec12cSHari Bathini 			PAGE_ALIGN(fadump_calculate_reserve_size());
544a4e92ce8SMahesh Salgaonkar #ifdef CONFIG_CMA
545579ca1a2SHari Bathini 		if (!fw_dump.nocma) {
546a4e92ce8SMahesh Salgaonkar 			fw_dump.boot_memory_size =
547140777a3SHari Bathini 				ALIGN(fw_dump.boot_memory_size,
548140777a3SHari Bathini 				      FADUMP_CMA_ALIGNMENT);
549579ca1a2SHari Bathini 		}
550a4e92ce8SMahesh Salgaonkar #endif
5517b1b3b48SHari Bathini 
5527b1b3b48SHari Bathini 		bootmem_min = fw_dump.ops->fadump_get_bootmem_min();
5537b1b3b48SHari Bathini 		if (fw_dump.boot_memory_size < bootmem_min) {
5547b1b3b48SHari Bathini 			pr_err("Can't enable fadump with boot memory size (0x%lx) less than 0x%llx\n",
5557b1b3b48SHari Bathini 			       fw_dump.boot_memory_size, bootmem_min);
5567b1b3b48SHari Bathini 			goto error_out;
5577b1b3b48SHari Bathini 		}
5587dee93a9SHari Bathini 
5597dee93a9SHari Bathini 		if (!fadump_get_boot_mem_regions()) {
5607dee93a9SHari Bathini 			pr_err("Too many holes in boot memory area to enable fadump\n");
5617dee93a9SHari Bathini 			goto error_out;
5627dee93a9SHari Bathini 		}
563a4e92ce8SMahesh Salgaonkar 	}
564eb39c880SMahesh Salgaonkar 
565eb39c880SMahesh Salgaonkar 	/*
566eb39c880SMahesh Salgaonkar 	 * Calculate the memory boundary.
567eb39c880SMahesh Salgaonkar 	 * If memory_limit is less than actual memory boundary then reserve
568eb39c880SMahesh Salgaonkar 	 * the memory for fadump beyond the memory_limit and adjust the
569eb39c880SMahesh Salgaonkar 	 * memory_limit accordingly, so that the running kernel can run with
570eb39c880SMahesh Salgaonkar 	 * specified memory_limit.
571eb39c880SMahesh Salgaonkar 	 */
572eb39c880SMahesh Salgaonkar 	if (memory_limit && memory_limit < memblock_end_of_DRAM()) {
573eb39c880SMahesh Salgaonkar 		size = get_fadump_area_size();
574eb39c880SMahesh Salgaonkar 		if ((memory_limit + size) < memblock_end_of_DRAM())
575eb39c880SMahesh Salgaonkar 			memory_limit += size;
576eb39c880SMahesh Salgaonkar 		else
577eb39c880SMahesh Salgaonkar 			memory_limit = memblock_end_of_DRAM();
578eb39c880SMahesh Salgaonkar 		printk(KERN_INFO "Adjusted memory_limit for firmware-assisted"
579a84fcd46SSuzuki Poulose 				" dump, now %#016llx\n", memory_limit);
580eb39c880SMahesh Salgaonkar 	}
581eb39c880SMahesh Salgaonkar 	if (memory_limit)
5826abec12cSHari Bathini 		mem_boundary = memory_limit;
583eb39c880SMahesh Salgaonkar 	else
5846abec12cSHari Bathini 		mem_boundary = memblock_end_of_DRAM();
585eb39c880SMahesh Salgaonkar 
5867dee93a9SHari Bathini 	base = fw_dump.boot_mem_top;
5878255da95SHari Bathini 	size = get_fadump_area_size();
5888255da95SHari Bathini 	fw_dump.reserve_dump_area_size = size;
589eb39c880SMahesh Salgaonkar 	if (fw_dump.dump_active) {
590b71a693dSMahesh Salgaonkar 		pr_info("Firmware-assisted dump is active.\n");
591b71a693dSMahesh Salgaonkar 
59285975387SHari Bathini #ifdef CONFIG_HUGETLB_PAGE
59385975387SHari Bathini 		/*
59485975387SHari Bathini 		 * FADump capture kernel doesn't care much about hugepages.
59585975387SHari Bathini 		 * In fact, handling hugepages in capture kernel is asking for
59685975387SHari Bathini 		 * trouble. So, disable HugeTLB support when fadump is active.
59785975387SHari Bathini 		 */
59885975387SHari Bathini 		hugetlb_disabled = true;
59985975387SHari Bathini #endif
600eb39c880SMahesh Salgaonkar 		/*
601eb39c880SMahesh Salgaonkar 		 * If last boot has crashed then reserve all the memory
602b2a815a5SHari Bathini 		 * above boot memory size so that we don't touch it until
603eb39c880SMahesh Salgaonkar 		 * dump is written to disk by userspace tool. This memory
604b2a815a5SHari Bathini 		 * can be released for general use by invalidating fadump.
605eb39c880SMahesh Salgaonkar 		 */
606b2a815a5SHari Bathini 		fadump_reserve_crash_area(base);
6072df173d9SMahesh Salgaonkar 
608f3512011SHari Bathini 		pr_debug("fadumphdr_addr = %#016lx\n", fw_dump.fadumphdr_addr);
609f3512011SHari Bathini 		pr_debug("Reserve dump area start address: 0x%lx\n",
610f3512011SHari Bathini 			 fw_dump.reserve_dump_area_start);
6118255da95SHari Bathini 	} else {
6128255da95SHari Bathini 		/*
613f6e6bedbSHari Bathini 		 * Reserve memory at an offset closer to bottom of the RAM to
614579ca1a2SHari Bathini 		 * minimize the impact of memory hot-remove operation.
615f6e6bedbSHari Bathini 		 */
616140777a3SHari Bathini 		base = fadump_locate_reserve_mem(base, size);
617f6e6bedbSHari Bathini 
6189a2921e5SHari Bathini 		if (!base || (base + size > mem_boundary)) {
619742a265aSHari Bathini 			pr_err("Failed to find memory chunk for reservation!\n");
620742a265aSHari Bathini 			goto error_out;
621742a265aSHari Bathini 		}
622742a265aSHari Bathini 		fw_dump.reserve_dump_area_start = base;
623742a265aSHari Bathini 
624742a265aSHari Bathini 		/*
625742a265aSHari Bathini 		 * Calculate the kernel metadata address and register it with
626742a265aSHari Bathini 		 * f/w if the platform supports.
627742a265aSHari Bathini 		 */
628742a265aSHari Bathini 		if (fw_dump.ops->fadump_setup_metadata &&
629742a265aSHari Bathini 		    (fw_dump.ops->fadump_setup_metadata(&fw_dump) < 0))
630742a265aSHari Bathini 			goto error_out;
631742a265aSHari Bathini 
632742a265aSHari Bathini 		if (memblock_reserve(base, size)) {
6336abec12cSHari Bathini 			pr_err("Failed to reserve memory!\n");
6346abec12cSHari Bathini 			goto error_out;
6356abec12cSHari Bathini 		}
6366abec12cSHari Bathini 
6376abec12cSHari Bathini 		pr_info("Reserved %lldMB of memory at %#016llx (System RAM: %lldMB)\n",
6386abec12cSHari Bathini 			(size >> 20), base, (memblock_phys_mem_size() >> 20));
639f6e6bedbSHari Bathini 
6406abec12cSHari Bathini 		ret = fadump_cma_init();
641a4e92ce8SMahesh Salgaonkar 	}
6426abec12cSHari Bathini 
6436abec12cSHari Bathini 	return ret;
6446abec12cSHari Bathini error_out:
6456abec12cSHari Bathini 	fw_dump.fadump_enabled = 0;
6466abec12cSHari Bathini 	return 0;
647eb39c880SMahesh Salgaonkar }
648eb39c880SMahesh Salgaonkar 
649eb39c880SMahesh Salgaonkar /* Look for fadump= cmdline option. */
650eb39c880SMahesh Salgaonkar static int __init early_fadump_param(char *p)
651eb39c880SMahesh Salgaonkar {
652eb39c880SMahesh Salgaonkar 	if (!p)
653eb39c880SMahesh Salgaonkar 		return 1;
654eb39c880SMahesh Salgaonkar 
655eb39c880SMahesh Salgaonkar 	if (strncmp(p, "on", 2) == 0)
656eb39c880SMahesh Salgaonkar 		fw_dump.fadump_enabled = 1;
657eb39c880SMahesh Salgaonkar 	else if (strncmp(p, "off", 3) == 0)
658eb39c880SMahesh Salgaonkar 		fw_dump.fadump_enabled = 0;
659a4e92ce8SMahesh Salgaonkar 	else if (strncmp(p, "nocma", 5) == 0) {
660a4e92ce8SMahesh Salgaonkar 		fw_dump.fadump_enabled = 1;
661a4e92ce8SMahesh Salgaonkar 		fw_dump.nocma = 1;
662a4e92ce8SMahesh Salgaonkar 	}
663eb39c880SMahesh Salgaonkar 
664eb39c880SMahesh Salgaonkar 	return 0;
665eb39c880SMahesh Salgaonkar }
666eb39c880SMahesh Salgaonkar early_param("fadump", early_fadump_param);
667eb39c880SMahesh Salgaonkar 
66881d9eca5SHari Bathini /*
66981d9eca5SHari Bathini  * Look for fadump_reserve_mem= cmdline option
67081d9eca5SHari Bathini  * TODO: Remove references to 'fadump_reserve_mem=' parameter,
67181d9eca5SHari Bathini  *       the sooner 'crashkernel=' parameter is accustomed to.
67281d9eca5SHari Bathini  */
67381d9eca5SHari Bathini static int __init early_fadump_reserve_mem(char *p)
67481d9eca5SHari Bathini {
67581d9eca5SHari Bathini 	if (p)
67681d9eca5SHari Bathini 		fw_dump.reserve_bootvar = memparse(p, &p);
67781d9eca5SHari Bathini 	return 0;
67881d9eca5SHari Bathini }
67981d9eca5SHari Bathini early_param("fadump_reserve_mem", early_fadump_reserve_mem);
68081d9eca5SHari Bathini 
681ebaeb5aeSMahesh Salgaonkar void crash_fadump(struct pt_regs *regs, const char *str)
682ebaeb5aeSMahesh Salgaonkar {
683ba608c4fSSourabh Jain 	unsigned int msecs;
684ebaeb5aeSMahesh Salgaonkar 	struct fadump_crash_info_header *fdh = NULL;
685f2a5e8f0SMahesh Salgaonkar 	int old_cpu, this_cpu;
686ba608c4fSSourabh Jain 	/* Do not include first CPU */
687ba608c4fSSourabh Jain 	unsigned int ncpus = num_online_cpus() - 1;
688ebaeb5aeSMahesh Salgaonkar 
6896fcd6baaSNicholas Piggin 	if (!should_fadump_crash())
690ebaeb5aeSMahesh Salgaonkar 		return;
691ebaeb5aeSMahesh Salgaonkar 
692f2a5e8f0SMahesh Salgaonkar 	/*
693f2a5e8f0SMahesh Salgaonkar 	 * old_cpu == -1 means this is the first CPU which has come here,
694f2a5e8f0SMahesh Salgaonkar 	 * go ahead and trigger fadump.
695f2a5e8f0SMahesh Salgaonkar 	 *
696f2a5e8f0SMahesh Salgaonkar 	 * old_cpu != -1 means some other CPU has already on it's way
697f2a5e8f0SMahesh Salgaonkar 	 * to trigger fadump, just keep looping here.
698f2a5e8f0SMahesh Salgaonkar 	 */
699f2a5e8f0SMahesh Salgaonkar 	this_cpu = smp_processor_id();
700f2a5e8f0SMahesh Salgaonkar 	old_cpu = cmpxchg(&crashing_cpu, -1, this_cpu);
701f2a5e8f0SMahesh Salgaonkar 
702f2a5e8f0SMahesh Salgaonkar 	if (old_cpu != -1) {
703ba608c4fSSourabh Jain 		atomic_inc(&cpus_in_fadump);
704ba608c4fSSourabh Jain 
705f2a5e8f0SMahesh Salgaonkar 		/*
706f2a5e8f0SMahesh Salgaonkar 		 * We can't loop here indefinitely. Wait as long as fadump
707f2a5e8f0SMahesh Salgaonkar 		 * is in force. If we race with fadump un-registration this
708f2a5e8f0SMahesh Salgaonkar 		 * loop will break and then we go down to normal panic path
709f2a5e8f0SMahesh Salgaonkar 		 * and reboot. If fadump is in force the first crashing
710f2a5e8f0SMahesh Salgaonkar 		 * cpu will definitely trigger fadump.
711f2a5e8f0SMahesh Salgaonkar 		 */
712f2a5e8f0SMahesh Salgaonkar 		while (fw_dump.dump_registered)
713f2a5e8f0SMahesh Salgaonkar 			cpu_relax();
714f2a5e8f0SMahesh Salgaonkar 		return;
715f2a5e8f0SMahesh Salgaonkar 	}
716f2a5e8f0SMahesh Salgaonkar 
717ebaeb5aeSMahesh Salgaonkar 	fdh = __va(fw_dump.fadumphdr_addr);
718ebaeb5aeSMahesh Salgaonkar 	fdh->crashing_cpu = crashing_cpu;
719ebaeb5aeSMahesh Salgaonkar 	crash_save_vmcoreinfo();
720ebaeb5aeSMahesh Salgaonkar 
721ebaeb5aeSMahesh Salgaonkar 	if (regs)
722ebaeb5aeSMahesh Salgaonkar 		fdh->regs = *regs;
723ebaeb5aeSMahesh Salgaonkar 	else
724ebaeb5aeSMahesh Salgaonkar 		ppc_save_regs(&fdh->regs);
725ebaeb5aeSMahesh Salgaonkar 
726a0512164SRasmus Villemoes 	fdh->online_mask = *cpu_online_mask;
727ebaeb5aeSMahesh Salgaonkar 
728ba608c4fSSourabh Jain 	/*
729ba608c4fSSourabh Jain 	 * If we came in via system reset, wait a while for the secondary
730ba608c4fSSourabh Jain 	 * CPUs to enter.
731ba608c4fSSourabh Jain 	 */
7327153d4bfSXiongwei Song 	if (TRAP(&(fdh->regs)) == INTERRUPT_SYSTEM_RESET) {
733ba608c4fSSourabh Jain 		msecs = CRASH_TIMEOUT;
734ba608c4fSSourabh Jain 		while ((atomic_read(&cpus_in_fadump) < ncpus) && (--msecs > 0))
735ba608c4fSSourabh Jain 			mdelay(1);
736ba608c4fSSourabh Jain 	}
737ba608c4fSSourabh Jain 
73841a65d16SHari Bathini 	fw_dump.ops->fadump_trigger(fdh, str);
739ebaeb5aeSMahesh Salgaonkar }
740ebaeb5aeSMahesh Salgaonkar 
7417f0ad11dSHari Bathini u32 *fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs)
742ebaeb5aeSMahesh Salgaonkar {
743ebaeb5aeSMahesh Salgaonkar 	struct elf_prstatus prstatus;
744ebaeb5aeSMahesh Salgaonkar 
745ebaeb5aeSMahesh Salgaonkar 	memset(&prstatus, 0, sizeof(prstatus));
746ebaeb5aeSMahesh Salgaonkar 	/*
747ebaeb5aeSMahesh Salgaonkar 	 * FIXME: How do i get PID? Do I really need it?
748ebaeb5aeSMahesh Salgaonkar 	 * prstatus.pr_pid = ????
749ebaeb5aeSMahesh Salgaonkar 	 */
750ebaeb5aeSMahesh Salgaonkar 	elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
75122bd0177SHari Bathini 	buf = append_elf_note(buf, CRASH_CORE_NOTE_NAME, NT_PRSTATUS,
752ebaeb5aeSMahesh Salgaonkar 			      &prstatus, sizeof(prstatus));
753ebaeb5aeSMahesh Salgaonkar 	return buf;
754ebaeb5aeSMahesh Salgaonkar }
755ebaeb5aeSMahesh Salgaonkar 
7567f0ad11dSHari Bathini void fadump_update_elfcore_header(char *bufp)
757ebaeb5aeSMahesh Salgaonkar {
758ebaeb5aeSMahesh Salgaonkar 	struct elf_phdr *phdr;
759ebaeb5aeSMahesh Salgaonkar 
760ebaeb5aeSMahesh Salgaonkar 	bufp += sizeof(struct elfhdr);
761ebaeb5aeSMahesh Salgaonkar 
762ebaeb5aeSMahesh Salgaonkar 	/* First note is a place holder for cpu notes info. */
763ebaeb5aeSMahesh Salgaonkar 	phdr = (struct elf_phdr *)bufp;
764ebaeb5aeSMahesh Salgaonkar 
765ebaeb5aeSMahesh Salgaonkar 	if (phdr->p_type == PT_NOTE) {
766961cf26aSHari Bathini 		phdr->p_paddr	= __pa(fw_dump.cpu_notes_buf_vaddr);
767ebaeb5aeSMahesh Salgaonkar 		phdr->p_offset	= phdr->p_paddr;
768ebaeb5aeSMahesh Salgaonkar 		phdr->p_filesz	= fw_dump.cpu_notes_buf_size;
769ebaeb5aeSMahesh Salgaonkar 		phdr->p_memsz = fw_dump.cpu_notes_buf_size;
770ebaeb5aeSMahesh Salgaonkar 	}
771ebaeb5aeSMahesh Salgaonkar 	return;
772ebaeb5aeSMahesh Salgaonkar }
773ebaeb5aeSMahesh Salgaonkar 
774961cf26aSHari Bathini static void *fadump_alloc_buffer(unsigned long size)
775ebaeb5aeSMahesh Salgaonkar {
77672aa6517SHari Bathini 	unsigned long count, i;
777ebaeb5aeSMahesh Salgaonkar 	struct page *page;
77872aa6517SHari Bathini 	void *vaddr;
779ebaeb5aeSMahesh Salgaonkar 
78072aa6517SHari Bathini 	vaddr = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
781ebaeb5aeSMahesh Salgaonkar 	if (!vaddr)
782ebaeb5aeSMahesh Salgaonkar 		return NULL;
783ebaeb5aeSMahesh Salgaonkar 
78472aa6517SHari Bathini 	count = PAGE_ALIGN(size) / PAGE_SIZE;
785ebaeb5aeSMahesh Salgaonkar 	page = virt_to_page(vaddr);
786ebaeb5aeSMahesh Salgaonkar 	for (i = 0; i < count; i++)
78772aa6517SHari Bathini 		mark_page_reserved(page + i);
788ebaeb5aeSMahesh Salgaonkar 	return vaddr;
789ebaeb5aeSMahesh Salgaonkar }
790ebaeb5aeSMahesh Salgaonkar 
791961cf26aSHari Bathini static void fadump_free_buffer(unsigned long vaddr, unsigned long size)
792ebaeb5aeSMahesh Salgaonkar {
79372aa6517SHari Bathini 	free_reserved_area((void *)vaddr, (void *)(vaddr + size), -1, NULL);
794ebaeb5aeSMahesh Salgaonkar }
795ebaeb5aeSMahesh Salgaonkar 
7967f0ad11dSHari Bathini s32 fadump_setup_cpu_notes_buf(u32 num_cpus)
797961cf26aSHari Bathini {
798961cf26aSHari Bathini 	/* Allocate buffer to hold cpu crash notes. */
799961cf26aSHari Bathini 	fw_dump.cpu_notes_buf_size = num_cpus * sizeof(note_buf_t);
800961cf26aSHari Bathini 	fw_dump.cpu_notes_buf_size = PAGE_ALIGN(fw_dump.cpu_notes_buf_size);
801961cf26aSHari Bathini 	fw_dump.cpu_notes_buf_vaddr =
802961cf26aSHari Bathini 		(unsigned long)fadump_alloc_buffer(fw_dump.cpu_notes_buf_size);
803961cf26aSHari Bathini 	if (!fw_dump.cpu_notes_buf_vaddr) {
804961cf26aSHari Bathini 		pr_err("Failed to allocate %ld bytes for CPU notes buffer\n",
805961cf26aSHari Bathini 		       fw_dump.cpu_notes_buf_size);
806961cf26aSHari Bathini 		return -ENOMEM;
807961cf26aSHari Bathini 	}
808961cf26aSHari Bathini 
809961cf26aSHari Bathini 	pr_debug("Allocated buffer for cpu notes of size %ld at 0x%lx\n",
810961cf26aSHari Bathini 		 fw_dump.cpu_notes_buf_size,
811961cf26aSHari Bathini 		 fw_dump.cpu_notes_buf_vaddr);
812961cf26aSHari Bathini 	return 0;
813961cf26aSHari Bathini }
814961cf26aSHari Bathini 
8157f0ad11dSHari Bathini void fadump_free_cpu_notes_buf(void)
816961cf26aSHari Bathini {
817961cf26aSHari Bathini 	if (!fw_dump.cpu_notes_buf_vaddr)
818961cf26aSHari Bathini 		return;
819961cf26aSHari Bathini 
820961cf26aSHari Bathini 	fadump_free_buffer(fw_dump.cpu_notes_buf_vaddr,
821961cf26aSHari Bathini 			   fw_dump.cpu_notes_buf_size);
822961cf26aSHari Bathini 	fw_dump.cpu_notes_buf_vaddr = 0;
823961cf26aSHari Bathini 	fw_dump.cpu_notes_buf_size = 0;
824961cf26aSHari Bathini }
825961cf26aSHari Bathini 
826e4fc48fbSHari Bathini static void fadump_free_mem_ranges(struct fadump_mrange_info *mrange_info)
8271bd6a1c4SHari Bathini {
82802c04e37SHari Bathini 	if (mrange_info->is_static) {
82902c04e37SHari Bathini 		mrange_info->mem_range_cnt = 0;
83002c04e37SHari Bathini 		return;
83102c04e37SHari Bathini 	}
83202c04e37SHari Bathini 
833e4fc48fbSHari Bathini 	kfree(mrange_info->mem_ranges);
83402c04e37SHari Bathini 	memset((void *)((u64)mrange_info + RNG_NAME_SZ), 0,
83502c04e37SHari Bathini 	       (sizeof(struct fadump_mrange_info) - RNG_NAME_SZ));
8361bd6a1c4SHari Bathini }
8371bd6a1c4SHari Bathini 
8381bd6a1c4SHari Bathini /*
839e4fc48fbSHari Bathini  * Allocate or reallocate mem_ranges array in incremental units
8401bd6a1c4SHari Bathini  * of PAGE_SIZE.
8411bd6a1c4SHari Bathini  */
842e4fc48fbSHari Bathini static int fadump_alloc_mem_ranges(struct fadump_mrange_info *mrange_info)
8431bd6a1c4SHari Bathini {
844e4fc48fbSHari Bathini 	struct fadump_memory_range *new_array;
8451bd6a1c4SHari Bathini 	u64 new_size;
8461bd6a1c4SHari Bathini 
847e4fc48fbSHari Bathini 	new_size = mrange_info->mem_ranges_sz + PAGE_SIZE;
848e4fc48fbSHari Bathini 	pr_debug("Allocating %llu bytes of memory for %s memory ranges\n",
849e4fc48fbSHari Bathini 		 new_size, mrange_info->name);
8501bd6a1c4SHari Bathini 
851e4fc48fbSHari Bathini 	new_array = krealloc(mrange_info->mem_ranges, new_size, GFP_KERNEL);
8521bd6a1c4SHari Bathini 	if (new_array == NULL) {
853e4fc48fbSHari Bathini 		pr_err("Insufficient memory for setting up %s memory ranges\n",
854e4fc48fbSHari Bathini 		       mrange_info->name);
855e4fc48fbSHari Bathini 		fadump_free_mem_ranges(mrange_info);
8561bd6a1c4SHari Bathini 		return -ENOMEM;
8571bd6a1c4SHari Bathini 	}
8581bd6a1c4SHari Bathini 
859e4fc48fbSHari Bathini 	mrange_info->mem_ranges = new_array;
860e4fc48fbSHari Bathini 	mrange_info->mem_ranges_sz = new_size;
861e4fc48fbSHari Bathini 	mrange_info->max_mem_ranges = (new_size /
862e4fc48fbSHari Bathini 				       sizeof(struct fadump_memory_range));
8631bd6a1c4SHari Bathini 	return 0;
8641bd6a1c4SHari Bathini }
8651bd6a1c4SHari Bathini 
866e4fc48fbSHari Bathini static inline int fadump_add_mem_range(struct fadump_mrange_info *mrange_info,
867e4fc48fbSHari Bathini 				       u64 base, u64 end)
8682df173d9SMahesh Salgaonkar {
869e4fc48fbSHari Bathini 	struct fadump_memory_range *mem_ranges = mrange_info->mem_ranges;
870ced1bf52SHari Bathini 	bool is_adjacent = false;
871e4fc48fbSHari Bathini 	u64 start, size;
872ced1bf52SHari Bathini 
8732df173d9SMahesh Salgaonkar 	if (base == end)
8741bd6a1c4SHari Bathini 		return 0;
8751bd6a1c4SHari Bathini 
876ced1bf52SHari Bathini 	/*
877ced1bf52SHari Bathini 	 * Fold adjacent memory ranges to bring down the memory ranges/
878ced1bf52SHari Bathini 	 * PT_LOAD segments count.
879ced1bf52SHari Bathini 	 */
880e4fc48fbSHari Bathini 	if (mrange_info->mem_range_cnt) {
881e4fc48fbSHari Bathini 		start = mem_ranges[mrange_info->mem_range_cnt - 1].base;
882e4fc48fbSHari Bathini 		size  = mem_ranges[mrange_info->mem_range_cnt - 1].size;
883ced1bf52SHari Bathini 
884ced1bf52SHari Bathini 		if ((start + size) == base)
885ced1bf52SHari Bathini 			is_adjacent = true;
886ced1bf52SHari Bathini 	}
887ced1bf52SHari Bathini 	if (!is_adjacent) {
888ced1bf52SHari Bathini 		/* resize the array on reaching the limit */
889e4fc48fbSHari Bathini 		if (mrange_info->mem_range_cnt == mrange_info->max_mem_ranges) {
8901bd6a1c4SHari Bathini 			int ret;
8911bd6a1c4SHari Bathini 
89202c04e37SHari Bathini 			if (mrange_info->is_static) {
89302c04e37SHari Bathini 				pr_err("Reached array size limit for %s memory ranges\n",
89402c04e37SHari Bathini 				       mrange_info->name);
89502c04e37SHari Bathini 				return -ENOSPC;
89602c04e37SHari Bathini 			}
89702c04e37SHari Bathini 
898e4fc48fbSHari Bathini 			ret = fadump_alloc_mem_ranges(mrange_info);
8991bd6a1c4SHari Bathini 			if (ret)
9001bd6a1c4SHari Bathini 				return ret;
901e4fc48fbSHari Bathini 
902e4fc48fbSHari Bathini 			/* Update to the new resized array */
903e4fc48fbSHari Bathini 			mem_ranges = mrange_info->mem_ranges;
9041bd6a1c4SHari Bathini 		}
9052df173d9SMahesh Salgaonkar 
906ced1bf52SHari Bathini 		start = base;
907e4fc48fbSHari Bathini 		mem_ranges[mrange_info->mem_range_cnt].base = start;
908e4fc48fbSHari Bathini 		mrange_info->mem_range_cnt++;
909ced1bf52SHari Bathini 	}
910ced1bf52SHari Bathini 
911e4fc48fbSHari Bathini 	mem_ranges[mrange_info->mem_range_cnt - 1].size = (end - start);
912e4fc48fbSHari Bathini 	pr_debug("%s_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n",
913e4fc48fbSHari Bathini 		 mrange_info->name, (mrange_info->mem_range_cnt - 1),
914e4fc48fbSHari Bathini 		 start, end - 1, (end - start));
9151bd6a1c4SHari Bathini 	return 0;
9162df173d9SMahesh Salgaonkar }
9172df173d9SMahesh Salgaonkar 
918e4fc48fbSHari Bathini static int fadump_exclude_reserved_area(u64 start, u64 end)
9192df173d9SMahesh Salgaonkar {
920e4fc48fbSHari Bathini 	u64 ra_start, ra_end;
9211bd6a1c4SHari Bathini 	int ret = 0;
9222df173d9SMahesh Salgaonkar 
9232df173d9SMahesh Salgaonkar 	ra_start = fw_dump.reserve_dump_area_start;
9242df173d9SMahesh Salgaonkar 	ra_end = ra_start + fw_dump.reserve_dump_area_size;
9252df173d9SMahesh Salgaonkar 
9262df173d9SMahesh Salgaonkar 	if ((ra_start < end) && (ra_end > start)) {
9272df173d9SMahesh Salgaonkar 		if ((start < ra_start) && (end > ra_end)) {
928e4fc48fbSHari Bathini 			ret = fadump_add_mem_range(&crash_mrange_info,
929e4fc48fbSHari Bathini 						   start, ra_start);
9301bd6a1c4SHari Bathini 			if (ret)
9311bd6a1c4SHari Bathini 				return ret;
9321bd6a1c4SHari Bathini 
933e4fc48fbSHari Bathini 			ret = fadump_add_mem_range(&crash_mrange_info,
934e4fc48fbSHari Bathini 						   ra_end, end);
9352df173d9SMahesh Salgaonkar 		} else if (start < ra_start) {
936e4fc48fbSHari Bathini 			ret = fadump_add_mem_range(&crash_mrange_info,
937e4fc48fbSHari Bathini 						   start, ra_start);
9382df173d9SMahesh Salgaonkar 		} else if (ra_end < end) {
939e4fc48fbSHari Bathini 			ret = fadump_add_mem_range(&crash_mrange_info,
940e4fc48fbSHari Bathini 						   ra_end, end);
9412df173d9SMahesh Salgaonkar 		}
9422df173d9SMahesh Salgaonkar 	} else
943e4fc48fbSHari Bathini 		ret = fadump_add_mem_range(&crash_mrange_info, start, end);
9441bd6a1c4SHari Bathini 
9451bd6a1c4SHari Bathini 	return ret;
9462df173d9SMahesh Salgaonkar }
9472df173d9SMahesh Salgaonkar 
9482df173d9SMahesh Salgaonkar static int fadump_init_elfcore_header(char *bufp)
9492df173d9SMahesh Salgaonkar {
9502df173d9SMahesh Salgaonkar 	struct elfhdr *elf;
9512df173d9SMahesh Salgaonkar 
9522df173d9SMahesh Salgaonkar 	elf = (struct elfhdr *) bufp;
9532df173d9SMahesh Salgaonkar 	bufp += sizeof(struct elfhdr);
9542df173d9SMahesh Salgaonkar 	memcpy(elf->e_ident, ELFMAG, SELFMAG);
9552df173d9SMahesh Salgaonkar 	elf->e_ident[EI_CLASS] = ELF_CLASS;
9562df173d9SMahesh Salgaonkar 	elf->e_ident[EI_DATA] = ELF_DATA;
9572df173d9SMahesh Salgaonkar 	elf->e_ident[EI_VERSION] = EV_CURRENT;
9582df173d9SMahesh Salgaonkar 	elf->e_ident[EI_OSABI] = ELF_OSABI;
9592df173d9SMahesh Salgaonkar 	memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
9602df173d9SMahesh Salgaonkar 	elf->e_type = ET_CORE;
9612df173d9SMahesh Salgaonkar 	elf->e_machine = ELF_ARCH;
9622df173d9SMahesh Salgaonkar 	elf->e_version = EV_CURRENT;
9632df173d9SMahesh Salgaonkar 	elf->e_entry = 0;
9642df173d9SMahesh Salgaonkar 	elf->e_phoff = sizeof(struct elfhdr);
9652df173d9SMahesh Salgaonkar 	elf->e_shoff = 0;
966d8bced27SDaniel Axtens #if defined(_CALL_ELF)
967d8bced27SDaniel Axtens 	elf->e_flags = _CALL_ELF;
968d8bced27SDaniel Axtens #else
969d8bced27SDaniel Axtens 	elf->e_flags = 0;
970d8bced27SDaniel Axtens #endif
9712df173d9SMahesh Salgaonkar 	elf->e_ehsize = sizeof(struct elfhdr);
9722df173d9SMahesh Salgaonkar 	elf->e_phentsize = sizeof(struct elf_phdr);
9732df173d9SMahesh Salgaonkar 	elf->e_phnum = 0;
9742df173d9SMahesh Salgaonkar 	elf->e_shentsize = 0;
9752df173d9SMahesh Salgaonkar 	elf->e_shnum = 0;
9762df173d9SMahesh Salgaonkar 	elf->e_shstrndx = 0;
9772df173d9SMahesh Salgaonkar 
9782df173d9SMahesh Salgaonkar 	return 0;
9792df173d9SMahesh Salgaonkar }
9802df173d9SMahesh Salgaonkar 
9812df173d9SMahesh Salgaonkar /*
9822df173d9SMahesh Salgaonkar  * Traverse through memblock structure and setup crash memory ranges. These
9832df173d9SMahesh Salgaonkar  * ranges will be used create PT_LOAD program headers in elfcore header.
9842df173d9SMahesh Salgaonkar  */
9851bd6a1c4SHari Bathini static int fadump_setup_crash_memory_ranges(void)
9862df173d9SMahesh Salgaonkar {
987b10d6bcaSMike Rapoport 	u64 i, start, end;
988b10d6bcaSMike Rapoport 	int ret;
9892df173d9SMahesh Salgaonkar 
9902df173d9SMahesh Salgaonkar 	pr_debug("Setup crash memory ranges.\n");
991e4fc48fbSHari Bathini 	crash_mrange_info.mem_range_cnt = 0;
992ced1bf52SHari Bathini 
9932df173d9SMahesh Salgaonkar 	/*
9947dee93a9SHari Bathini 	 * Boot memory region(s) registered with firmware are moved to
9957dee93a9SHari Bathini 	 * different location at the time of crash. Create separate program
9967dee93a9SHari Bathini 	 * header(s) for this memory chunk(s) with the correct offset.
9972df173d9SMahesh Salgaonkar 	 */
9987dee93a9SHari Bathini 	for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) {
9997dee93a9SHari Bathini 		start = fw_dump.boot_mem_addr[i];
10007dee93a9SHari Bathini 		end = start + fw_dump.boot_mem_sz[i];
10017dee93a9SHari Bathini 		ret = fadump_add_mem_range(&crash_mrange_info, start, end);
10021bd6a1c4SHari Bathini 		if (ret)
10031bd6a1c4SHari Bathini 			return ret;
10047dee93a9SHari Bathini 	}
10052df173d9SMahesh Salgaonkar 
1006b10d6bcaSMike Rapoport 	for_each_mem_range(i, &start, &end) {
1007a77af552SHari Bathini 		/*
10087dee93a9SHari Bathini 		 * skip the memory chunk that is already added
10097dee93a9SHari Bathini 		 * (0 through boot_memory_top).
1010a77af552SHari Bathini 		 */
10117dee93a9SHari Bathini 		if (start < fw_dump.boot_mem_top) {
10127dee93a9SHari Bathini 			if (end > fw_dump.boot_mem_top)
10137dee93a9SHari Bathini 				start = fw_dump.boot_mem_top;
1014a77af552SHari Bathini 			else
1015a77af552SHari Bathini 				continue;
1016a77af552SHari Bathini 		}
10172df173d9SMahesh Salgaonkar 
10182df173d9SMahesh Salgaonkar 		/* add this range excluding the reserved dump area. */
10191bd6a1c4SHari Bathini 		ret = fadump_exclude_reserved_area(start, end);
10201bd6a1c4SHari Bathini 		if (ret)
10211bd6a1c4SHari Bathini 			return ret;
10222df173d9SMahesh Salgaonkar 	}
10231bd6a1c4SHari Bathini 
10241bd6a1c4SHari Bathini 	return 0;
10252df173d9SMahesh Salgaonkar }
10262df173d9SMahesh Salgaonkar 
1027d34c5f26SMahesh Salgaonkar /*
1028d34c5f26SMahesh Salgaonkar  * If the given physical address falls within the boot memory region then
1029d34c5f26SMahesh Salgaonkar  * return the relocated address that points to the dump region reserved
1030d34c5f26SMahesh Salgaonkar  * for saving initial boot memory contents.
1031d34c5f26SMahesh Salgaonkar  */
1032d34c5f26SMahesh Salgaonkar static inline unsigned long fadump_relocate(unsigned long paddr)
1033d34c5f26SMahesh Salgaonkar {
10347dee93a9SHari Bathini 	unsigned long raddr, rstart, rend, rlast, hole_size;
10357dee93a9SHari Bathini 	int i;
10367dee93a9SHari Bathini 
10377dee93a9SHari Bathini 	hole_size = 0;
10387dee93a9SHari Bathini 	rlast = 0;
10397dee93a9SHari Bathini 	raddr = paddr;
10407dee93a9SHari Bathini 	for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) {
10417dee93a9SHari Bathini 		rstart = fw_dump.boot_mem_addr[i];
10427dee93a9SHari Bathini 		rend = rstart + fw_dump.boot_mem_sz[i];
10437dee93a9SHari Bathini 		hole_size += (rstart - rlast);
10447dee93a9SHari Bathini 
10457dee93a9SHari Bathini 		if (paddr >= rstart && paddr < rend) {
10467dee93a9SHari Bathini 			raddr += fw_dump.boot_mem_dest_addr - hole_size;
10477dee93a9SHari Bathini 			break;
10487dee93a9SHari Bathini 		}
10497dee93a9SHari Bathini 
10507dee93a9SHari Bathini 		rlast = rend;
10517dee93a9SHari Bathini 	}
10527dee93a9SHari Bathini 
10537dee93a9SHari Bathini 	pr_debug("vmcoreinfo: paddr = 0x%lx, raddr = 0x%lx\n", paddr, raddr);
10547dee93a9SHari Bathini 	return raddr;
1055d34c5f26SMahesh Salgaonkar }
1056d34c5f26SMahesh Salgaonkar 
10572df173d9SMahesh Salgaonkar static int fadump_create_elfcore_headers(char *bufp)
10582df173d9SMahesh Salgaonkar {
10597dee93a9SHari Bathini 	unsigned long long raddr, offset;
10602df173d9SMahesh Salgaonkar 	struct elf_phdr *phdr;
10617dee93a9SHari Bathini 	struct elfhdr *elf;
10627dee93a9SHari Bathini 	int i, j;
10632df173d9SMahesh Salgaonkar 
10642df173d9SMahesh Salgaonkar 	fadump_init_elfcore_header(bufp);
10652df173d9SMahesh Salgaonkar 	elf = (struct elfhdr *)bufp;
10662df173d9SMahesh Salgaonkar 	bufp += sizeof(struct elfhdr);
10672df173d9SMahesh Salgaonkar 
1068ebaeb5aeSMahesh Salgaonkar 	/*
1069ebaeb5aeSMahesh Salgaonkar 	 * setup ELF PT_NOTE, place holder for cpu notes info. The notes info
1070ebaeb5aeSMahesh Salgaonkar 	 * will be populated during second kernel boot after crash. Hence
1071ebaeb5aeSMahesh Salgaonkar 	 * this PT_NOTE will always be the first elf note.
1072ebaeb5aeSMahesh Salgaonkar 	 *
1073ebaeb5aeSMahesh Salgaonkar 	 * NOTE: Any new ELF note addition should be placed after this note.
1074ebaeb5aeSMahesh Salgaonkar 	 */
1075ebaeb5aeSMahesh Salgaonkar 	phdr = (struct elf_phdr *)bufp;
1076ebaeb5aeSMahesh Salgaonkar 	bufp += sizeof(struct elf_phdr);
1077ebaeb5aeSMahesh Salgaonkar 	phdr->p_type = PT_NOTE;
1078ebaeb5aeSMahesh Salgaonkar 	phdr->p_flags = 0;
1079ebaeb5aeSMahesh Salgaonkar 	phdr->p_vaddr = 0;
1080ebaeb5aeSMahesh Salgaonkar 	phdr->p_align = 0;
1081ebaeb5aeSMahesh Salgaonkar 
1082ebaeb5aeSMahesh Salgaonkar 	phdr->p_offset = 0;
1083ebaeb5aeSMahesh Salgaonkar 	phdr->p_paddr = 0;
1084ebaeb5aeSMahesh Salgaonkar 	phdr->p_filesz = 0;
1085ebaeb5aeSMahesh Salgaonkar 	phdr->p_memsz = 0;
1086ebaeb5aeSMahesh Salgaonkar 
1087ebaeb5aeSMahesh Salgaonkar 	(elf->e_phnum)++;
1088ebaeb5aeSMahesh Salgaonkar 
1089d34c5f26SMahesh Salgaonkar 	/* setup ELF PT_NOTE for vmcoreinfo */
1090d34c5f26SMahesh Salgaonkar 	phdr = (struct elf_phdr *)bufp;
1091d34c5f26SMahesh Salgaonkar 	bufp += sizeof(struct elf_phdr);
1092d34c5f26SMahesh Salgaonkar 	phdr->p_type	= PT_NOTE;
1093d34c5f26SMahesh Salgaonkar 	phdr->p_flags	= 0;
1094d34c5f26SMahesh Salgaonkar 	phdr->p_vaddr	= 0;
1095d34c5f26SMahesh Salgaonkar 	phdr->p_align	= 0;
1096d34c5f26SMahesh Salgaonkar 
1097d34c5f26SMahesh Salgaonkar 	phdr->p_paddr	= fadump_relocate(paddr_vmcoreinfo_note());
1098d34c5f26SMahesh Salgaonkar 	phdr->p_offset	= phdr->p_paddr;
10995203f499SXunlei Pang 	phdr->p_memsz	= phdr->p_filesz = VMCOREINFO_NOTE_SIZE;
1100d34c5f26SMahesh Salgaonkar 
1101d34c5f26SMahesh Salgaonkar 	/* Increment number of program headers. */
1102d34c5f26SMahesh Salgaonkar 	(elf->e_phnum)++;
1103d34c5f26SMahesh Salgaonkar 
11042df173d9SMahesh Salgaonkar 	/* setup PT_LOAD sections. */
11057dee93a9SHari Bathini 	j = 0;
11067dee93a9SHari Bathini 	offset = 0;
11077dee93a9SHari Bathini 	raddr = fw_dump.boot_mem_addr[0];
1108e4fc48fbSHari Bathini 	for (i = 0; i < crash_mrange_info.mem_range_cnt; i++) {
1109e4fc48fbSHari Bathini 		u64 mbase, msize;
11102df173d9SMahesh Salgaonkar 
1111e4fc48fbSHari Bathini 		mbase = crash_mrange_info.mem_ranges[i].base;
1112e4fc48fbSHari Bathini 		msize = crash_mrange_info.mem_ranges[i].size;
11132df173d9SMahesh Salgaonkar 		if (!msize)
11142df173d9SMahesh Salgaonkar 			continue;
11152df173d9SMahesh Salgaonkar 
11162df173d9SMahesh Salgaonkar 		phdr = (struct elf_phdr *)bufp;
11172df173d9SMahesh Salgaonkar 		bufp += sizeof(struct elf_phdr);
11182df173d9SMahesh Salgaonkar 		phdr->p_type	= PT_LOAD;
11192df173d9SMahesh Salgaonkar 		phdr->p_flags	= PF_R|PF_W|PF_X;
11202df173d9SMahesh Salgaonkar 		phdr->p_offset	= mbase;
11212df173d9SMahesh Salgaonkar 
11227dee93a9SHari Bathini 		if (mbase == raddr) {
11232df173d9SMahesh Salgaonkar 			/*
1124becd91d9SHari Bathini 			 * The entire real memory region will be moved by
1125becd91d9SHari Bathini 			 * firmware to the specified destination_address.
1126becd91d9SHari Bathini 			 * Hence set the correct offset.
11272df173d9SMahesh Salgaonkar 			 */
11287dee93a9SHari Bathini 			phdr->p_offset = fw_dump.boot_mem_dest_addr + offset;
11297dee93a9SHari Bathini 			if (j < (fw_dump.boot_mem_regs_cnt - 1)) {
11307dee93a9SHari Bathini 				offset += fw_dump.boot_mem_sz[j];
11317dee93a9SHari Bathini 				raddr = fw_dump.boot_mem_addr[++j];
11327dee93a9SHari Bathini 			}
11332df173d9SMahesh Salgaonkar 		}
11342df173d9SMahesh Salgaonkar 
11352df173d9SMahesh Salgaonkar 		phdr->p_paddr = mbase;
11362df173d9SMahesh Salgaonkar 		phdr->p_vaddr = (unsigned long)__va(mbase);
11372df173d9SMahesh Salgaonkar 		phdr->p_filesz = msize;
11382df173d9SMahesh Salgaonkar 		phdr->p_memsz = msize;
11392df173d9SMahesh Salgaonkar 		phdr->p_align = 0;
11402df173d9SMahesh Salgaonkar 
11412df173d9SMahesh Salgaonkar 		/* Increment number of program headers. */
11422df173d9SMahesh Salgaonkar 		(elf->e_phnum)++;
11432df173d9SMahesh Salgaonkar 	}
11442df173d9SMahesh Salgaonkar 	return 0;
11452df173d9SMahesh Salgaonkar }
11462df173d9SMahesh Salgaonkar 
11472df173d9SMahesh Salgaonkar static unsigned long init_fadump_header(unsigned long addr)
11482df173d9SMahesh Salgaonkar {
11492df173d9SMahesh Salgaonkar 	struct fadump_crash_info_header *fdh;
11502df173d9SMahesh Salgaonkar 
11512df173d9SMahesh Salgaonkar 	if (!addr)
11522df173d9SMahesh Salgaonkar 		return 0;
11532df173d9SMahesh Salgaonkar 
11542df173d9SMahesh Salgaonkar 	fdh = __va(addr);
11552df173d9SMahesh Salgaonkar 	addr += sizeof(struct fadump_crash_info_header);
11562df173d9SMahesh Salgaonkar 
11572df173d9SMahesh Salgaonkar 	memset(fdh, 0, sizeof(struct fadump_crash_info_header));
11582df173d9SMahesh Salgaonkar 	fdh->magic_number = FADUMP_CRASH_INFO_MAGIC;
11592df173d9SMahesh Salgaonkar 	fdh->elfcorehdr_addr = addr;
1160ebaeb5aeSMahesh Salgaonkar 	/* We will set the crashing cpu id in crash_fadump() during crash. */
11610226e552SHari Bathini 	fdh->crashing_cpu = FADUMP_CPU_UNKNOWN;
11622df173d9SMahesh Salgaonkar 
11632df173d9SMahesh Salgaonkar 	return addr;
11642df173d9SMahesh Salgaonkar }
11652df173d9SMahesh Salgaonkar 
116698b8cd7fSMichal Suchanek static int register_fadump(void)
11673ccc00a7SMahesh Salgaonkar {
11682df173d9SMahesh Salgaonkar 	unsigned long addr;
11692df173d9SMahesh Salgaonkar 	void *vaddr;
11701bd6a1c4SHari Bathini 	int ret;
11712df173d9SMahesh Salgaonkar 
11723ccc00a7SMahesh Salgaonkar 	/*
11733ccc00a7SMahesh Salgaonkar 	 * If no memory is reserved then we can not register for firmware-
11743ccc00a7SMahesh Salgaonkar 	 * assisted dump.
11753ccc00a7SMahesh Salgaonkar 	 */
11763ccc00a7SMahesh Salgaonkar 	if (!fw_dump.reserve_dump_area_size)
117798b8cd7fSMichal Suchanek 		return -ENODEV;
11783ccc00a7SMahesh Salgaonkar 
11791bd6a1c4SHari Bathini 	ret = fadump_setup_crash_memory_ranges();
11801bd6a1c4SHari Bathini 	if (ret)
11811bd6a1c4SHari Bathini 		return ret;
11822df173d9SMahesh Salgaonkar 
118341a65d16SHari Bathini 	addr = fw_dump.fadumphdr_addr;
118441a65d16SHari Bathini 
11852df173d9SMahesh Salgaonkar 	/* Initialize fadump crash info header. */
11862df173d9SMahesh Salgaonkar 	addr = init_fadump_header(addr);
11872df173d9SMahesh Salgaonkar 	vaddr = __va(addr);
11882df173d9SMahesh Salgaonkar 
11892df173d9SMahesh Salgaonkar 	pr_debug("Creating ELF core headers at %#016lx\n", addr);
11902df173d9SMahesh Salgaonkar 	fadump_create_elfcore_headers(vaddr);
11912df173d9SMahesh Salgaonkar 
11923ccc00a7SMahesh Salgaonkar 	/* register the future kernel dump with firmware. */
119341a65d16SHari Bathini 	pr_debug("Registering for firmware-assisted kernel dump...\n");
119441a65d16SHari Bathini 	return fw_dump.ops->fadump_register(&fw_dump);
11953ccc00a7SMahesh Salgaonkar }
11963ccc00a7SMahesh Salgaonkar 
1197b500afffSMahesh Salgaonkar void fadump_cleanup(void)
1198b500afffSMahesh Salgaonkar {
11992790d01dSHari Bathini 	if (!fw_dump.fadump_supported)
12002790d01dSHari Bathini 		return;
12012790d01dSHari Bathini 
1202b500afffSMahesh Salgaonkar 	/* Invalidate the registration only if dump is active. */
1203b500afffSMahesh Salgaonkar 	if (fw_dump.dump_active) {
1204f3512011SHari Bathini 		pr_debug("Invalidating firmware-assisted dump registration\n");
1205f3512011SHari Bathini 		fw_dump.ops->fadump_invalidate(&fw_dump);
1206722cde76SMahesh Salgaonkar 	} else if (fw_dump.dump_registered) {
1207722cde76SMahesh Salgaonkar 		/* Un-register Firmware-assisted dump if it was registered. */
120841a65d16SHari Bathini 		fw_dump.ops->fadump_unregister(&fw_dump);
1209e4fc48fbSHari Bathini 		fadump_free_mem_ranges(&crash_mrange_info);
1210b500afffSMahesh Salgaonkar 	}
12112790d01dSHari Bathini 
12122790d01dSHari Bathini 	if (fw_dump.ops->fadump_cleanup)
12132790d01dSHari Bathini 		fw_dump.ops->fadump_cleanup(&fw_dump);
1214b500afffSMahesh Salgaonkar }
1215b500afffSMahesh Salgaonkar 
121668fa6478SHari Bathini static void fadump_free_reserved_memory(unsigned long start_pfn,
121768fa6478SHari Bathini 					unsigned long end_pfn)
121868fa6478SHari Bathini {
121968fa6478SHari Bathini 	unsigned long pfn;
122068fa6478SHari Bathini 	unsigned long time_limit = jiffies + HZ;
122168fa6478SHari Bathini 
122268fa6478SHari Bathini 	pr_info("freeing reserved memory (0x%llx - 0x%llx)\n",
122368fa6478SHari Bathini 		PFN_PHYS(start_pfn), PFN_PHYS(end_pfn));
122468fa6478SHari Bathini 
122568fa6478SHari Bathini 	for (pfn = start_pfn; pfn < end_pfn; pfn++) {
122668fa6478SHari Bathini 		free_reserved_page(pfn_to_page(pfn));
122768fa6478SHari Bathini 
122868fa6478SHari Bathini 		if (time_after(jiffies, time_limit)) {
122968fa6478SHari Bathini 			cond_resched();
123068fa6478SHari Bathini 			time_limit = jiffies + HZ;
123168fa6478SHari Bathini 		}
123268fa6478SHari Bathini 	}
123368fa6478SHari Bathini }
123468fa6478SHari Bathini 
123568fa6478SHari Bathini /*
123668fa6478SHari Bathini  * Skip memory holes and free memory that was actually reserved.
123768fa6478SHari Bathini  */
1238dda9dbfeSHari Bathini static void fadump_release_reserved_area(u64 start, u64 end)
123968fa6478SHari Bathini {
1240b10d6bcaSMike Rapoport 	unsigned long reg_spfn, reg_epfn;
1241b10d6bcaSMike Rapoport 	u64 tstart, tend, spfn, epfn;
1242b10d6bcaSMike Rapoport 	int i;
124368fa6478SHari Bathini 
1244dda9dbfeSHari Bathini 	spfn = PHYS_PFN(start);
1245dda9dbfeSHari Bathini 	epfn = PHYS_PFN(end);
1246c9118e6cSMike Rapoport 
1247c9118e6cSMike Rapoport 	for_each_mem_pfn_range(i, MAX_NUMNODES, &reg_spfn, &reg_epfn, NULL) {
1248c9118e6cSMike Rapoport 		tstart = max_t(u64, spfn, reg_spfn);
1249c9118e6cSMike Rapoport 		tend   = min_t(u64, epfn, reg_epfn);
1250c9118e6cSMike Rapoport 
125168fa6478SHari Bathini 		if (tstart < tend) {
125268fa6478SHari Bathini 			fadump_free_reserved_memory(tstart, tend);
125368fa6478SHari Bathini 
1254dda9dbfeSHari Bathini 			if (tend == epfn)
125568fa6478SHari Bathini 				break;
125668fa6478SHari Bathini 
1257dda9dbfeSHari Bathini 			spfn = tend;
125868fa6478SHari Bathini 		}
125968fa6478SHari Bathini 	}
126068fa6478SHari Bathini }
126168fa6478SHari Bathini 
1262b500afffSMahesh Salgaonkar /*
1263dda9dbfeSHari Bathini  * Sort the mem ranges in-place and merge adjacent ranges
1264dda9dbfeSHari Bathini  * to minimize the memory ranges count.
1265b500afffSMahesh Salgaonkar  */
1266dda9dbfeSHari Bathini static void sort_and_merge_mem_ranges(struct fadump_mrange_info *mrange_info)
1267b500afffSMahesh Salgaonkar {
1268dda9dbfeSHari Bathini 	struct fadump_memory_range *mem_ranges;
1269dda9dbfeSHari Bathini 	struct fadump_memory_range tmp_range;
1270dda9dbfeSHari Bathini 	u64 base, size;
1271dda9dbfeSHari Bathini 	int i, j, idx;
1272dda9dbfeSHari Bathini 
1273dda9dbfeSHari Bathini 	if (!reserved_mrange_info.mem_range_cnt)
1274dda9dbfeSHari Bathini 		return;
1275dda9dbfeSHari Bathini 
1276dda9dbfeSHari Bathini 	/* Sort the memory ranges */
1277dda9dbfeSHari Bathini 	mem_ranges = mrange_info->mem_ranges;
1278dda9dbfeSHari Bathini 	for (i = 0; i < mrange_info->mem_range_cnt; i++) {
1279dda9dbfeSHari Bathini 		idx = i;
1280dda9dbfeSHari Bathini 		for (j = (i + 1); j < mrange_info->mem_range_cnt; j++) {
1281dda9dbfeSHari Bathini 			if (mem_ranges[idx].base > mem_ranges[j].base)
1282dda9dbfeSHari Bathini 				idx = j;
1283dda9dbfeSHari Bathini 		}
1284dda9dbfeSHari Bathini 		if (idx != i) {
1285dda9dbfeSHari Bathini 			tmp_range = mem_ranges[idx];
1286dda9dbfeSHari Bathini 			mem_ranges[idx] = mem_ranges[i];
1287dda9dbfeSHari Bathini 			mem_ranges[i] = tmp_range;
1288dda9dbfeSHari Bathini 		}
1289dda9dbfeSHari Bathini 	}
1290dda9dbfeSHari Bathini 
1291dda9dbfeSHari Bathini 	/* Merge adjacent reserved ranges */
1292dda9dbfeSHari Bathini 	idx = 0;
1293dda9dbfeSHari Bathini 	for (i = 1; i < mrange_info->mem_range_cnt; i++) {
1294dda9dbfeSHari Bathini 		base = mem_ranges[i-1].base;
1295dda9dbfeSHari Bathini 		size = mem_ranges[i-1].size;
1296dda9dbfeSHari Bathini 		if (mem_ranges[i].base == (base + size))
1297dda9dbfeSHari Bathini 			mem_ranges[idx].size += mem_ranges[i].size;
1298dda9dbfeSHari Bathini 		else {
1299dda9dbfeSHari Bathini 			idx++;
1300dda9dbfeSHari Bathini 			if (i == idx)
1301dda9dbfeSHari Bathini 				continue;
1302dda9dbfeSHari Bathini 
1303dda9dbfeSHari Bathini 			mem_ranges[idx] = mem_ranges[i];
1304dda9dbfeSHari Bathini 		}
1305dda9dbfeSHari Bathini 	}
1306dda9dbfeSHari Bathini 	mrange_info->mem_range_cnt = idx + 1;
1307dda9dbfeSHari Bathini }
1308dda9dbfeSHari Bathini 
1309dda9dbfeSHari Bathini /*
1310dda9dbfeSHari Bathini  * Scan reserved-ranges to consider them while reserving/releasing
1311dda9dbfeSHari Bathini  * memory for FADump.
1312dda9dbfeSHari Bathini  */
131302c04e37SHari Bathini static void __init early_init_dt_scan_reserved_ranges(unsigned long node)
1314dda9dbfeSHari Bathini {
1315dda9dbfeSHari Bathini 	const __be32 *prop;
1316dda9dbfeSHari Bathini 	int len, ret = -1;
1317dda9dbfeSHari Bathini 	unsigned long i;
1318dda9dbfeSHari Bathini 
131902c04e37SHari Bathini 	/* reserved-ranges already scanned */
132002c04e37SHari Bathini 	if (reserved_mrange_info.mem_range_cnt != 0)
132102c04e37SHari Bathini 		return;
1322dda9dbfeSHari Bathini 
132302c04e37SHari Bathini 	prop = of_get_flat_dt_prop(node, "reserved-ranges", &len);
1324dda9dbfeSHari Bathini 	if (!prop)
132502c04e37SHari Bathini 		return;
1326dda9dbfeSHari Bathini 
1327dda9dbfeSHari Bathini 	/*
1328dda9dbfeSHari Bathini 	 * Each reserved range is an (address,size) pair, 2 cells each,
1329dda9dbfeSHari Bathini 	 * totalling 4 cells per range.
1330dda9dbfeSHari Bathini 	 */
1331dda9dbfeSHari Bathini 	for (i = 0; i < len / (sizeof(*prop) * 4); i++) {
1332dda9dbfeSHari Bathini 		u64 base, size;
1333dda9dbfeSHari Bathini 
1334dda9dbfeSHari Bathini 		base = of_read_number(prop + (i * 4) + 0, 2);
1335dda9dbfeSHari Bathini 		size = of_read_number(prop + (i * 4) + 2, 2);
1336dda9dbfeSHari Bathini 
1337dda9dbfeSHari Bathini 		if (size) {
1338dda9dbfeSHari Bathini 			ret = fadump_add_mem_range(&reserved_mrange_info,
1339dda9dbfeSHari Bathini 						   base, base + size);
1340dda9dbfeSHari Bathini 			if (ret < 0) {
1341dda9dbfeSHari Bathini 				pr_warn("some reserved ranges are ignored!\n");
1342dda9dbfeSHari Bathini 				break;
1343dda9dbfeSHari Bathini 			}
1344dda9dbfeSHari Bathini 		}
1345dda9dbfeSHari Bathini 	}
1346dda9dbfeSHari Bathini 
134702c04e37SHari Bathini 	/* Compact reserved ranges */
134802c04e37SHari Bathini 	sort_and_merge_mem_ranges(&reserved_mrange_info);
1349dda9dbfeSHari Bathini }
1350dda9dbfeSHari Bathini 
1351dda9dbfeSHari Bathini /*
1352dda9dbfeSHari Bathini  * Release the memory that was reserved during early boot to preserve the
1353dda9dbfeSHari Bathini  * crash'ed kernel's memory contents except reserved dump area (permanent
1354dda9dbfeSHari Bathini  * reservation) and reserved ranges used by F/W. The released memory will
1355dda9dbfeSHari Bathini  * be available for general use.
1356dda9dbfeSHari Bathini  */
1357dda9dbfeSHari Bathini static void fadump_release_memory(u64 begin, u64 end)
1358dda9dbfeSHari Bathini {
1359dda9dbfeSHari Bathini 	u64 ra_start, ra_end, tstart;
1360dda9dbfeSHari Bathini 	int i, ret;
1361dda9dbfeSHari Bathini 
1362b500afffSMahesh Salgaonkar 	ra_start = fw_dump.reserve_dump_area_start;
1363b500afffSMahesh Salgaonkar 	ra_end = ra_start + fw_dump.reserve_dump_area_size;
1364b500afffSMahesh Salgaonkar 
1365b500afffSMahesh Salgaonkar 	/*
136602c04e37SHari Bathini 	 * If reserved ranges array limit is hit, overwrite the last reserved
136702c04e37SHari Bathini 	 * memory range with reserved dump area to ensure it is excluded from
136802c04e37SHari Bathini 	 * the memory being released (reused for next FADump registration).
1369dda9dbfeSHari Bathini 	 */
137002c04e37SHari Bathini 	if (reserved_mrange_info.mem_range_cnt ==
137102c04e37SHari Bathini 	    reserved_mrange_info.max_mem_ranges)
137202c04e37SHari Bathini 		reserved_mrange_info.mem_range_cnt--;
1373dda9dbfeSHari Bathini 
137402c04e37SHari Bathini 	ret = fadump_add_mem_range(&reserved_mrange_info, ra_start, ra_end);
137502c04e37SHari Bathini 	if (ret != 0)
1376dda9dbfeSHari Bathini 		return;
1377dda9dbfeSHari Bathini 
1378dda9dbfeSHari Bathini 	/* Get the reserved ranges list in order first. */
1379dda9dbfeSHari Bathini 	sort_and_merge_mem_ranges(&reserved_mrange_info);
1380dda9dbfeSHari Bathini 
1381dda9dbfeSHari Bathini 	/* Exclude reserved ranges and release remaining memory */
1382dda9dbfeSHari Bathini 	tstart = begin;
1383dda9dbfeSHari Bathini 	for (i = 0; i < reserved_mrange_info.mem_range_cnt; i++) {
1384dda9dbfeSHari Bathini 		ra_start = reserved_mrange_info.mem_ranges[i].base;
1385dda9dbfeSHari Bathini 		ra_end = ra_start + reserved_mrange_info.mem_ranges[i].size;
1386dda9dbfeSHari Bathini 
1387dda9dbfeSHari Bathini 		if (tstart >= ra_end)
1388dda9dbfeSHari Bathini 			continue;
1389dda9dbfeSHari Bathini 
1390dda9dbfeSHari Bathini 		if (tstart < ra_start)
1391dda9dbfeSHari Bathini 			fadump_release_reserved_area(tstart, ra_start);
1392dda9dbfeSHari Bathini 		tstart = ra_end;
1393dda9dbfeSHari Bathini 	}
1394dda9dbfeSHari Bathini 
1395dda9dbfeSHari Bathini 	if (tstart < end)
1396dda9dbfeSHari Bathini 		fadump_release_reserved_area(tstart, end);
1397b500afffSMahesh Salgaonkar }
1398b500afffSMahesh Salgaonkar 
1399b500afffSMahesh Salgaonkar static void fadump_invalidate_release_mem(void)
1400b500afffSMahesh Salgaonkar {
1401b500afffSMahesh Salgaonkar 	mutex_lock(&fadump_mutex);
1402b500afffSMahesh Salgaonkar 	if (!fw_dump.dump_active) {
1403b500afffSMahesh Salgaonkar 		mutex_unlock(&fadump_mutex);
1404b500afffSMahesh Salgaonkar 		return;
1405b500afffSMahesh Salgaonkar 	}
1406b500afffSMahesh Salgaonkar 
1407b500afffSMahesh Salgaonkar 	fadump_cleanup();
1408b500afffSMahesh Salgaonkar 	mutex_unlock(&fadump_mutex);
1409b500afffSMahesh Salgaonkar 
14107dee93a9SHari Bathini 	fadump_release_memory(fw_dump.boot_mem_top, memblock_end_of_DRAM());
1411961cf26aSHari Bathini 	fadump_free_cpu_notes_buf();
1412961cf26aSHari Bathini 
1413a4e2e2caSHari Bathini 	/*
1414a4e2e2caSHari Bathini 	 * Setup kernel metadata and initialize the kernel dump
1415a4e2e2caSHari Bathini 	 * memory structure for FADump re-registration.
1416a4e2e2caSHari Bathini 	 */
1417a4e2e2caSHari Bathini 	if (fw_dump.ops->fadump_setup_metadata &&
1418a4e2e2caSHari Bathini 	    (fw_dump.ops->fadump_setup_metadata(&fw_dump) < 0))
1419a4e2e2caSHari Bathini 		pr_warn("Failed to setup kernel metadata!\n");
142041a65d16SHari Bathini 	fw_dump.ops->fadump_init_mem_struct(&fw_dump);
1421b500afffSMahesh Salgaonkar }
1422b500afffSMahesh Salgaonkar 
1423d418b19fSSourabh Jain static ssize_t release_mem_store(struct kobject *kobj,
1424b500afffSMahesh Salgaonkar 				 struct kobj_attribute *attr,
1425b500afffSMahesh Salgaonkar 				 const char *buf, size_t count)
1426b500afffSMahesh Salgaonkar {
1427dcdc4679SMichal Suchanek 	int input = -1;
1428dcdc4679SMichal Suchanek 
1429b500afffSMahesh Salgaonkar 	if (!fw_dump.dump_active)
1430b500afffSMahesh Salgaonkar 		return -EPERM;
1431b500afffSMahesh Salgaonkar 
1432dcdc4679SMichal Suchanek 	if (kstrtoint(buf, 0, &input))
1433dcdc4679SMichal Suchanek 		return -EINVAL;
1434dcdc4679SMichal Suchanek 
1435dcdc4679SMichal Suchanek 	if (input == 1) {
1436b500afffSMahesh Salgaonkar 		/*
1437b500afffSMahesh Salgaonkar 		 * Take away the '/proc/vmcore'. We are releasing the dump
1438b500afffSMahesh Salgaonkar 		 * memory, hence it will not be valid anymore.
1439b500afffSMahesh Salgaonkar 		 */
14402685f826SMichael Ellerman #ifdef CONFIG_PROC_VMCORE
1441b500afffSMahesh Salgaonkar 		vmcore_cleanup();
14422685f826SMichael Ellerman #endif
1443b500afffSMahesh Salgaonkar 		fadump_invalidate_release_mem();
1444b500afffSMahesh Salgaonkar 
1445b500afffSMahesh Salgaonkar 	} else
1446b500afffSMahesh Salgaonkar 		return -EINVAL;
1447b500afffSMahesh Salgaonkar 	return count;
1448b500afffSMahesh Salgaonkar }
1449b500afffSMahesh Salgaonkar 
1450d418b19fSSourabh Jain /* Release the reserved memory and disable the FADump */
1451d418b19fSSourabh Jain static void unregister_fadump(void)
1452d418b19fSSourabh Jain {
1453d418b19fSSourabh Jain 	fadump_cleanup();
1454d418b19fSSourabh Jain 	fadump_release_memory(fw_dump.reserve_dump_area_start,
1455d418b19fSSourabh Jain 			      fw_dump.reserve_dump_area_size);
1456d418b19fSSourabh Jain 	fw_dump.fadump_enabled = 0;
1457d418b19fSSourabh Jain 	kobject_put(fadump_kobj);
1458d418b19fSSourabh Jain }
1459d418b19fSSourabh Jain 
1460d418b19fSSourabh Jain static ssize_t enabled_show(struct kobject *kobj,
14613ccc00a7SMahesh Salgaonkar 			    struct kobj_attribute *attr,
14623ccc00a7SMahesh Salgaonkar 			    char *buf)
14633ccc00a7SMahesh Salgaonkar {
14643ccc00a7SMahesh Salgaonkar 	return sprintf(buf, "%d\n", fw_dump.fadump_enabled);
14653ccc00a7SMahesh Salgaonkar }
14663ccc00a7SMahesh Salgaonkar 
1467d8e73458SSourabh Jain static ssize_t mem_reserved_show(struct kobject *kobj,
1468d8e73458SSourabh Jain 				 struct kobj_attribute *attr,
1469d8e73458SSourabh Jain 				 char *buf)
1470d8e73458SSourabh Jain {
1471d8e73458SSourabh Jain 	return sprintf(buf, "%ld\n", fw_dump.reserve_dump_area_size);
1472d8e73458SSourabh Jain }
1473d8e73458SSourabh Jain 
1474d418b19fSSourabh Jain static ssize_t registered_show(struct kobject *kobj,
14753ccc00a7SMahesh Salgaonkar 			       struct kobj_attribute *attr,
14763ccc00a7SMahesh Salgaonkar 			       char *buf)
14773ccc00a7SMahesh Salgaonkar {
14783ccc00a7SMahesh Salgaonkar 	return sprintf(buf, "%d\n", fw_dump.dump_registered);
14793ccc00a7SMahesh Salgaonkar }
14803ccc00a7SMahesh Salgaonkar 
1481d418b19fSSourabh Jain static ssize_t registered_store(struct kobject *kobj,
14823ccc00a7SMahesh Salgaonkar 				struct kobj_attribute *attr,
14833ccc00a7SMahesh Salgaonkar 				const char *buf, size_t count)
14843ccc00a7SMahesh Salgaonkar {
14853ccc00a7SMahesh Salgaonkar 	int ret = 0;
1486dcdc4679SMichal Suchanek 	int input = -1;
14873ccc00a7SMahesh Salgaonkar 
1488f3512011SHari Bathini 	if (!fw_dump.fadump_enabled || fw_dump.dump_active)
14893ccc00a7SMahesh Salgaonkar 		return -EPERM;
14903ccc00a7SMahesh Salgaonkar 
1491dcdc4679SMichal Suchanek 	if (kstrtoint(buf, 0, &input))
1492dcdc4679SMichal Suchanek 		return -EINVAL;
1493dcdc4679SMichal Suchanek 
14943ccc00a7SMahesh Salgaonkar 	mutex_lock(&fadump_mutex);
14953ccc00a7SMahesh Salgaonkar 
1496dcdc4679SMichal Suchanek 	switch (input) {
1497dcdc4679SMichal Suchanek 	case 0:
14983ccc00a7SMahesh Salgaonkar 		if (fw_dump.dump_registered == 0) {
14993ccc00a7SMahesh Salgaonkar 			goto unlock_out;
15003ccc00a7SMahesh Salgaonkar 		}
1501f3512011SHari Bathini 
15023ccc00a7SMahesh Salgaonkar 		/* Un-register Firmware-assisted dump */
150341a65d16SHari Bathini 		pr_debug("Un-register firmware-assisted dump\n");
150441a65d16SHari Bathini 		fw_dump.ops->fadump_unregister(&fw_dump);
15053ccc00a7SMahesh Salgaonkar 		break;
1506dcdc4679SMichal Suchanek 	case 1:
15073ccc00a7SMahesh Salgaonkar 		if (fw_dump.dump_registered == 1) {
15080823c68bSHari Bathini 			/* Un-register Firmware-assisted dump */
150941a65d16SHari Bathini 			fw_dump.ops->fadump_unregister(&fw_dump);
15103ccc00a7SMahesh Salgaonkar 		}
15113ccc00a7SMahesh Salgaonkar 		/* Register Firmware-assisted dump */
151298b8cd7fSMichal Suchanek 		ret = register_fadump();
15133ccc00a7SMahesh Salgaonkar 		break;
15143ccc00a7SMahesh Salgaonkar 	default:
15153ccc00a7SMahesh Salgaonkar 		ret = -EINVAL;
15163ccc00a7SMahesh Salgaonkar 		break;
15173ccc00a7SMahesh Salgaonkar 	}
15183ccc00a7SMahesh Salgaonkar 
15193ccc00a7SMahesh Salgaonkar unlock_out:
15203ccc00a7SMahesh Salgaonkar 	mutex_unlock(&fadump_mutex);
15213ccc00a7SMahesh Salgaonkar 	return ret < 0 ? ret : count;
15223ccc00a7SMahesh Salgaonkar }
15233ccc00a7SMahesh Salgaonkar 
15243ccc00a7SMahesh Salgaonkar static int fadump_region_show(struct seq_file *m, void *private)
15253ccc00a7SMahesh Salgaonkar {
15263ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_enabled)
15273ccc00a7SMahesh Salgaonkar 		return 0;
15283ccc00a7SMahesh Salgaonkar 
1529b500afffSMahesh Salgaonkar 	mutex_lock(&fadump_mutex);
153041a65d16SHari Bathini 	fw_dump.ops->fadump_region_show(&fw_dump, m);
1531b500afffSMahesh Salgaonkar 	mutex_unlock(&fadump_mutex);
15323ccc00a7SMahesh Salgaonkar 	return 0;
15333ccc00a7SMahesh Salgaonkar }
15343ccc00a7SMahesh Salgaonkar 
1535d418b19fSSourabh Jain static struct kobj_attribute release_attr = __ATTR_WO(release_mem);
1536d418b19fSSourabh Jain static struct kobj_attribute enable_attr = __ATTR_RO(enabled);
1537d418b19fSSourabh Jain static struct kobj_attribute register_attr = __ATTR_RW(registered);
1538d8e73458SSourabh Jain static struct kobj_attribute mem_reserved_attr = __ATTR_RO(mem_reserved);
1539d418b19fSSourabh Jain 
1540d418b19fSSourabh Jain static struct attribute *fadump_attrs[] = {
1541d418b19fSSourabh Jain 	&enable_attr.attr,
1542d418b19fSSourabh Jain 	&register_attr.attr,
1543d8e73458SSourabh Jain 	&mem_reserved_attr.attr,
1544d418b19fSSourabh Jain 	NULL,
1545d418b19fSSourabh Jain };
1546d418b19fSSourabh Jain 
1547d418b19fSSourabh Jain ATTRIBUTE_GROUPS(fadump);
15483ccc00a7SMahesh Salgaonkar 
1549f6cee260SYangtao Li DEFINE_SHOW_ATTRIBUTE(fadump_region);
15503ccc00a7SMahesh Salgaonkar 
15513ccc00a7SMahesh Salgaonkar static void fadump_init_files(void)
15523ccc00a7SMahesh Salgaonkar {
15533ccc00a7SMahesh Salgaonkar 	int rc = 0;
15543ccc00a7SMahesh Salgaonkar 
1555d418b19fSSourabh Jain 	fadump_kobj = kobject_create_and_add("fadump", kernel_kobj);
1556d418b19fSSourabh Jain 	if (!fadump_kobj) {
1557d418b19fSSourabh Jain 		pr_err("failed to create fadump kobject\n");
1558d418b19fSSourabh Jain 		return;
1559d418b19fSSourabh Jain 	}
1560860286cfSGreg Kroah-Hartman 
1561860286cfSGreg Kroah-Hartman 	debugfs_create_file("fadump_region", 0444, powerpc_debugfs_root, NULL,
15623ccc00a7SMahesh Salgaonkar 			    &fadump_region_fops);
1563b500afffSMahesh Salgaonkar 
1564b500afffSMahesh Salgaonkar 	if (fw_dump.dump_active) {
1565d418b19fSSourabh Jain 		rc = sysfs_create_file(fadump_kobj, &release_attr.attr);
1566b500afffSMahesh Salgaonkar 		if (rc)
1567d418b19fSSourabh Jain 			pr_err("unable to create release_mem sysfs file (%d)\n",
1568d418b19fSSourabh Jain 			       rc);
1569d418b19fSSourabh Jain 	}
1570d418b19fSSourabh Jain 
1571d418b19fSSourabh Jain 	rc = sysfs_create_groups(fadump_kobj, fadump_groups);
1572d418b19fSSourabh Jain 	if (rc) {
1573d418b19fSSourabh Jain 		pr_err("sysfs group creation failed (%d), unregistering FADump",
1574d418b19fSSourabh Jain 		       rc);
1575d418b19fSSourabh Jain 		unregister_fadump();
1576d418b19fSSourabh Jain 		return;
1577d418b19fSSourabh Jain 	}
1578d418b19fSSourabh Jain 
1579d418b19fSSourabh Jain 	/*
1580d418b19fSSourabh Jain 	 * The FADump sysfs are moved from kernel_kobj to fadump_kobj need to
1581d418b19fSSourabh Jain 	 * create symlink at old location to maintain backward compatibility.
1582d418b19fSSourabh Jain 	 *
1583d418b19fSSourabh Jain 	 *      - fadump_enabled -> fadump/enabled
1584d418b19fSSourabh Jain 	 *      - fadump_registered -> fadump/registered
1585d418b19fSSourabh Jain 	 *      - fadump_release_mem -> fadump/release_mem
1586d418b19fSSourabh Jain 	 */
1587d418b19fSSourabh Jain 	rc = compat_only_sysfs_link_entry_to_kobj(kernel_kobj, fadump_kobj,
1588d418b19fSSourabh Jain 						  "enabled", "fadump_enabled");
1589d418b19fSSourabh Jain 	if (rc) {
1590d418b19fSSourabh Jain 		pr_err("unable to create fadump_enabled symlink (%d)", rc);
1591d418b19fSSourabh Jain 		return;
1592d418b19fSSourabh Jain 	}
1593d418b19fSSourabh Jain 
1594d418b19fSSourabh Jain 	rc = compat_only_sysfs_link_entry_to_kobj(kernel_kobj, fadump_kobj,
1595d418b19fSSourabh Jain 						  "registered",
1596d418b19fSSourabh Jain 						  "fadump_registered");
1597d418b19fSSourabh Jain 	if (rc) {
1598d418b19fSSourabh Jain 		pr_err("unable to create fadump_registered symlink (%d)", rc);
1599d418b19fSSourabh Jain 		sysfs_remove_link(kernel_kobj, "fadump_enabled");
1600d418b19fSSourabh Jain 		return;
1601d418b19fSSourabh Jain 	}
1602d418b19fSSourabh Jain 
1603d418b19fSSourabh Jain 	if (fw_dump.dump_active) {
1604d418b19fSSourabh Jain 		rc = compat_only_sysfs_link_entry_to_kobj(kernel_kobj,
1605d418b19fSSourabh Jain 							  fadump_kobj,
1606d418b19fSSourabh Jain 							  "release_mem",
1607d418b19fSSourabh Jain 							  "fadump_release_mem");
1608d418b19fSSourabh Jain 		if (rc)
1609d418b19fSSourabh Jain 			pr_err("unable to create fadump_release_mem symlink (%d)",
1610d418b19fSSourabh Jain 			       rc);
1611b500afffSMahesh Salgaonkar 	}
16123ccc00a7SMahesh Salgaonkar 	return;
16133ccc00a7SMahesh Salgaonkar }
16143ccc00a7SMahesh Salgaonkar 
16153ccc00a7SMahesh Salgaonkar /*
16163ccc00a7SMahesh Salgaonkar  * Prepare for firmware-assisted dump.
16173ccc00a7SMahesh Salgaonkar  */
16183ccc00a7SMahesh Salgaonkar int __init setup_fadump(void)
16193ccc00a7SMahesh Salgaonkar {
1620565f9bc0SMichal Suchanek 	if (!fw_dump.fadump_supported)
16213ccc00a7SMahesh Salgaonkar 		return 0;
16223ccc00a7SMahesh Salgaonkar 
1623565f9bc0SMichal Suchanek 	fadump_init_files();
16243ccc00a7SMahesh Salgaonkar 	fadump_show_config();
1625565f9bc0SMichal Suchanek 
1626565f9bc0SMichal Suchanek 	if (!fw_dump.fadump_enabled)
1627565f9bc0SMichal Suchanek 		return 1;
1628565f9bc0SMichal Suchanek 
16292df173d9SMahesh Salgaonkar 	/*
16302df173d9SMahesh Salgaonkar 	 * If dump data is available then see if it is valid and prepare for
16312df173d9SMahesh Salgaonkar 	 * saving it to the disk.
16322df173d9SMahesh Salgaonkar 	 */
1633b500afffSMahesh Salgaonkar 	if (fw_dump.dump_active) {
1634b500afffSMahesh Salgaonkar 		/*
1635b500afffSMahesh Salgaonkar 		 * if dump process fails then invalidate the registration
1636b500afffSMahesh Salgaonkar 		 * and release memory before proceeding for re-registration.
1637b500afffSMahesh Salgaonkar 		 */
1638f3512011SHari Bathini 		if (fw_dump.ops->fadump_process(&fw_dump) < 0)
1639b500afffSMahesh Salgaonkar 			fadump_invalidate_release_mem();
1640b500afffSMahesh Salgaonkar 	}
16413ccc00a7SMahesh Salgaonkar 	/* Initialize the kernel dump memory structure for FAD registration. */
16422df173d9SMahesh Salgaonkar 	else if (fw_dump.reserve_dump_area_size)
164341a65d16SHari Bathini 		fw_dump.ops->fadump_init_mem_struct(&fw_dump);
1644f3512011SHari Bathini 
16453ccc00a7SMahesh Salgaonkar 	return 1;
16463ccc00a7SMahesh Salgaonkar }
16473ccc00a7SMahesh Salgaonkar subsys_initcall(setup_fadump);
1648bec53196SHari Bathini #else /* !CONFIG_PRESERVE_FA_DUMP */
1649bec53196SHari Bathini 
1650bec53196SHari Bathini /* Scan the Firmware Assisted dump configuration details. */
1651bec53196SHari Bathini int __init early_init_dt_scan_fw_dump(unsigned long node, const char *uname,
1652bec53196SHari Bathini 				      int depth, void *data)
1653bec53196SHari Bathini {
1654bec53196SHari Bathini 	if ((depth != 1) || (strcmp(uname, "ibm,opal") != 0))
1655bec53196SHari Bathini 		return 0;
1656bec53196SHari Bathini 
1657bec53196SHari Bathini 	opal_fadump_dt_scan(&fw_dump, node);
1658bec53196SHari Bathini 	return 1;
1659bec53196SHari Bathini }
1660bec53196SHari Bathini 
1661bec53196SHari Bathini /*
1662bec53196SHari Bathini  * When dump is active but PRESERVE_FA_DUMP is enabled on the kernel,
1663bec53196SHari Bathini  * preserve crash data. The subsequent memory preserving kernel boot
1664bec53196SHari Bathini  * is likely to process this crash data.
1665bec53196SHari Bathini  */
1666bec53196SHari Bathini int __init fadump_reserve_mem(void)
1667bec53196SHari Bathini {
1668bec53196SHari Bathini 	if (fw_dump.dump_active) {
1669bec53196SHari Bathini 		/*
1670bec53196SHari Bathini 		 * If last boot has crashed then reserve all the memory
1671bec53196SHari Bathini 		 * above boot memory to preserve crash data.
1672bec53196SHari Bathini 		 */
1673bec53196SHari Bathini 		pr_info("Preserving crash data for processing in next boot.\n");
1674bec53196SHari Bathini 		fadump_reserve_crash_area(fw_dump.boot_mem_top);
1675bec53196SHari Bathini 	} else
1676bec53196SHari Bathini 		pr_debug("FADump-aware kernel..\n");
1677bec53196SHari Bathini 
1678bec53196SHari Bathini 	return 1;
1679bec53196SHari Bathini }
1680bec53196SHari Bathini #endif /* CONFIG_PRESERVE_FA_DUMP */
1681b2a815a5SHari Bathini 
1682b2a815a5SHari Bathini /* Preserve everything above the base address */
1683b2a815a5SHari Bathini static void __init fadump_reserve_crash_area(u64 base)
1684b2a815a5SHari Bathini {
1685b10d6bcaSMike Rapoport 	u64 i, mstart, mend, msize;
1686b2a815a5SHari Bathini 
1687b10d6bcaSMike Rapoport 	for_each_mem_range(i, &mstart, &mend) {
1688b10d6bcaSMike Rapoport 		msize  = mend - mstart;
1689b2a815a5SHari Bathini 
1690b2a815a5SHari Bathini 		if ((mstart + msize) < base)
1691b2a815a5SHari Bathini 			continue;
1692b2a815a5SHari Bathini 
1693b2a815a5SHari Bathini 		if (mstart < base) {
1694b2a815a5SHari Bathini 			msize -= (base - mstart);
1695b2a815a5SHari Bathini 			mstart = base;
1696b2a815a5SHari Bathini 		}
1697b2a815a5SHari Bathini 
1698b2a815a5SHari Bathini 		pr_info("Reserving %lluMB of memory at %#016llx for preserving crash data",
1699b2a815a5SHari Bathini 			(msize >> 20), mstart);
1700b2a815a5SHari Bathini 		memblock_reserve(mstart, msize);
1701b2a815a5SHari Bathini 	}
1702b2a815a5SHari Bathini }
1703bec53196SHari Bathini 
1704bec53196SHari Bathini unsigned long __init arch_reserved_kernel_pages(void)
1705bec53196SHari Bathini {
1706bec53196SHari Bathini 	return memblock_reserved_size() / PAGE_SIZE;
1707bec53196SHari Bathini }
1708