xref: /openbmc/linux/arch/powerpc/kernel/fadump.c (revision 7153d4bf0b373428d0393c001019da4d0483fddb)
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>
34eb39c880SMahesh Salgaonkar 
35ba608c4fSSourabh Jain /*
36ba608c4fSSourabh Jain  * The CPU who acquired the lock to trigger the fadump crash should
37ba608c4fSSourabh Jain  * wait for other CPUs to enter.
38ba608c4fSSourabh Jain  *
39ba608c4fSSourabh Jain  * The timeout is in milliseconds.
40ba608c4fSSourabh Jain  */
41ba608c4fSSourabh Jain #define CRASH_TIMEOUT		500
42ba608c4fSSourabh Jain 
43eb39c880SMahesh Salgaonkar static struct fw_dump fw_dump;
443ccc00a7SMahesh Salgaonkar 
45b2a815a5SHari Bathini static void __init fadump_reserve_crash_area(u64 base);
46b2a815a5SHari Bathini 
47d418b19fSSourabh Jain struct kobject *fadump_kobj;
48d418b19fSSourabh Jain 
49bec53196SHari Bathini #ifndef CONFIG_PRESERVE_FA_DUMP
505f987caeSMichael Ellerman 
515f987caeSMichael Ellerman static atomic_t cpus_in_fadump;
523ccc00a7SMahesh Salgaonkar static DEFINE_MUTEX(fadump_mutex);
535f987caeSMichael Ellerman 
5402c04e37SHari Bathini struct fadump_mrange_info crash_mrange_info = { "crash", NULL, 0, 0, 0, false };
5502c04e37SHari Bathini 
5602c04e37SHari Bathini #define RESERVED_RNGS_SZ	16384 /* 16K - 128 entries */
5702c04e37SHari Bathini #define RESERVED_RNGS_CNT	(RESERVED_RNGS_SZ / \
5802c04e37SHari Bathini 				 sizeof(struct fadump_memory_range))
5902c04e37SHari Bathini static struct fadump_memory_range rngs[RESERVED_RNGS_CNT];
6002c04e37SHari Bathini struct fadump_mrange_info reserved_mrange_info = { "reserved", rngs,
6102c04e37SHari Bathini 						   RESERVED_RNGS_SZ, 0,
6202c04e37SHari Bathini 						   RESERVED_RNGS_CNT, true };
6302c04e37SHari Bathini 
6402c04e37SHari Bathini static void __init early_init_dt_scan_reserved_ranges(unsigned long node);
65eb39c880SMahesh Salgaonkar 
66a4e92ce8SMahesh Salgaonkar #ifdef CONFIG_CMA
670226e552SHari Bathini static struct cma *fadump_cma;
680226e552SHari Bathini 
69a4e92ce8SMahesh Salgaonkar /*
70a4e92ce8SMahesh Salgaonkar  * fadump_cma_init() - Initialize CMA area from a fadump reserved memory
71a4e92ce8SMahesh Salgaonkar  *
72a4e92ce8SMahesh Salgaonkar  * This function initializes CMA area from fadump reserved memory.
73a4e92ce8SMahesh Salgaonkar  * The total size of fadump reserved memory covers for boot memory size
74a4e92ce8SMahesh Salgaonkar  * + cpu data size + hpte size and metadata.
75a4e92ce8SMahesh Salgaonkar  * Initialize only the area equivalent to boot memory size for CMA use.
76a4e92ce8SMahesh Salgaonkar  * The reamining portion of fadump reserved memory will be not given
77a4e92ce8SMahesh Salgaonkar  * to CMA and pages for thoes will stay reserved. boot memory size is
78a4e92ce8SMahesh Salgaonkar  * aligned per CMA requirement to satisy cma_init_reserved_mem() call.
79a4e92ce8SMahesh Salgaonkar  * But for some reason even if it fails we still have the memory reservation
80a4e92ce8SMahesh Salgaonkar  * with us and we can still continue doing fadump.
81a4e92ce8SMahesh Salgaonkar  */
82a4e92ce8SMahesh Salgaonkar int __init fadump_cma_init(void)
83a4e92ce8SMahesh Salgaonkar {
84a4e92ce8SMahesh Salgaonkar 	unsigned long long base, size;
85a4e92ce8SMahesh Salgaonkar 	int rc;
86a4e92ce8SMahesh Salgaonkar 
87a4e92ce8SMahesh Salgaonkar 	if (!fw_dump.fadump_enabled)
88a4e92ce8SMahesh Salgaonkar 		return 0;
89a4e92ce8SMahesh Salgaonkar 
90a4e92ce8SMahesh Salgaonkar 	/*
91a4e92ce8SMahesh Salgaonkar 	 * Do not use CMA if user has provided fadump=nocma kernel parameter.
92a4e92ce8SMahesh Salgaonkar 	 * Return 1 to continue with fadump old behaviour.
93a4e92ce8SMahesh Salgaonkar 	 */
94a4e92ce8SMahesh Salgaonkar 	if (fw_dump.nocma)
95a4e92ce8SMahesh Salgaonkar 		return 1;
96a4e92ce8SMahesh Salgaonkar 
97a4e92ce8SMahesh Salgaonkar 	base = fw_dump.reserve_dump_area_start;
98a4e92ce8SMahesh Salgaonkar 	size = fw_dump.boot_memory_size;
99a4e92ce8SMahesh Salgaonkar 
100a4e92ce8SMahesh Salgaonkar 	if (!size)
101a4e92ce8SMahesh Salgaonkar 		return 0;
102a4e92ce8SMahesh Salgaonkar 
103a4e92ce8SMahesh Salgaonkar 	rc = cma_init_reserved_mem(base, size, 0, "fadump_cma", &fadump_cma);
104a4e92ce8SMahesh Salgaonkar 	if (rc) {
105a4e92ce8SMahesh Salgaonkar 		pr_err("Failed to init cma area for firmware-assisted dump,%d\n", rc);
106a4e92ce8SMahesh Salgaonkar 		/*
107a4e92ce8SMahesh Salgaonkar 		 * Though the CMA init has failed we still have memory
108a4e92ce8SMahesh Salgaonkar 		 * reservation with us. The reserved memory will be
109a4e92ce8SMahesh Salgaonkar 		 * blocked from production system usage.  Hence return 1,
110a4e92ce8SMahesh Salgaonkar 		 * so that we can continue with fadump.
111a4e92ce8SMahesh Salgaonkar 		 */
112a4e92ce8SMahesh Salgaonkar 		return 1;
113a4e92ce8SMahesh Salgaonkar 	}
114a4e92ce8SMahesh Salgaonkar 
115a4e92ce8SMahesh Salgaonkar 	/*
116a4e92ce8SMahesh Salgaonkar 	 * So we now have successfully initialized cma area for fadump.
117a4e92ce8SMahesh Salgaonkar 	 */
118a4e92ce8SMahesh Salgaonkar 	pr_info("Initialized 0x%lx bytes cma area at %ldMB from 0x%lx "
119a4e92ce8SMahesh Salgaonkar 		"bytes of memory reserved for firmware-assisted dump\n",
120a4e92ce8SMahesh Salgaonkar 		cma_get_size(fadump_cma),
121a4e92ce8SMahesh Salgaonkar 		(unsigned long)cma_get_base(fadump_cma) >> 20,
122a4e92ce8SMahesh Salgaonkar 		fw_dump.reserve_dump_area_size);
123a4e92ce8SMahesh Salgaonkar 	return 1;
124a4e92ce8SMahesh Salgaonkar }
125a4e92ce8SMahesh Salgaonkar #else
126a4e92ce8SMahesh Salgaonkar static int __init fadump_cma_init(void) { return 1; }
127a4e92ce8SMahesh Salgaonkar #endif /* CONFIG_CMA */
128a4e92ce8SMahesh Salgaonkar 
129eb39c880SMahesh Salgaonkar /* Scan the Firmware Assisted dump configuration details. */
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  */
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 
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 
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  */
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, &reg_start, &reg_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  */
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  */
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. */
2543ccc00a7SMahesh Salgaonkar static void 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  */
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  */
356eb39c880SMahesh Salgaonkar static unsigned long 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;
362eb39c880SMahesh Salgaonkar 	size += fw_dump.boot_memory_size;
3632df173d9SMahesh Salgaonkar 	size += sizeof(struct fadump_crash_info_header);
3642df173d9SMahesh Salgaonkar 	size += sizeof(struct elfhdr); /* ELF core header.*/
365ebaeb5aeSMahesh Salgaonkar 	size += sizeof(struct elf_phdr); /* place holder for cpu notes */
3662df173d9SMahesh Salgaonkar 	/* Program headers for crash memory regions. */
3672df173d9SMahesh Salgaonkar 	size += sizeof(struct elf_phdr) * (memblock_num_regions(memory) + 2);
368eb39c880SMahesh Salgaonkar 
369eb39c880SMahesh Salgaonkar 	size = PAGE_ALIGN(size);
370742a265aSHari Bathini 
371742a265aSHari Bathini 	/* This is to hold kernel metadata on platforms that support it */
372742a265aSHari Bathini 	size += (fw_dump.ops->fadump_get_metadata_size ?
373742a265aSHari Bathini 		 fw_dump.ops->fadump_get_metadata_size() : 0);
374eb39c880SMahesh Salgaonkar 	return size;
375eb39c880SMahesh Salgaonkar }
376eb39c880SMahesh Salgaonkar 
3777dee93a9SHari Bathini static int __init add_boot_mem_region(unsigned long rstart,
3787dee93a9SHari Bathini 				      unsigned long rsize)
3797dee93a9SHari Bathini {
3807dee93a9SHari Bathini 	int i = fw_dump.boot_mem_regs_cnt++;
3817dee93a9SHari Bathini 
3827dee93a9SHari Bathini 	if (fw_dump.boot_mem_regs_cnt > FADUMP_MAX_MEM_REGS) {
3837dee93a9SHari Bathini 		fw_dump.boot_mem_regs_cnt = FADUMP_MAX_MEM_REGS;
3847dee93a9SHari Bathini 		return 0;
3857dee93a9SHari Bathini 	}
3867dee93a9SHari Bathini 
3877dee93a9SHari Bathini 	pr_debug("Added boot memory range[%d] [%#016lx-%#016lx)\n",
3887dee93a9SHari Bathini 		 i, rstart, (rstart + rsize));
3897dee93a9SHari Bathini 	fw_dump.boot_mem_addr[i] = rstart;
3907dee93a9SHari Bathini 	fw_dump.boot_mem_sz[i] = rsize;
3917dee93a9SHari Bathini 	return 1;
3927dee93a9SHari Bathini }
3937dee93a9SHari Bathini 
3947dee93a9SHari Bathini /*
3957dee93a9SHari Bathini  * Firmware usually has a hard limit on the data it can copy per region.
3967dee93a9SHari Bathini  * Honour that by splitting a memory range into multiple regions.
3977dee93a9SHari Bathini  */
3987dee93a9SHari Bathini static int __init add_boot_mem_regions(unsigned long mstart,
3997dee93a9SHari Bathini 				       unsigned long msize)
4007dee93a9SHari Bathini {
4017dee93a9SHari Bathini 	unsigned long rstart, rsize, max_size;
4027dee93a9SHari Bathini 	int ret = 1;
4037dee93a9SHari Bathini 
4047dee93a9SHari Bathini 	rstart = mstart;
4057dee93a9SHari Bathini 	max_size = fw_dump.max_copy_size ? fw_dump.max_copy_size : msize;
4067dee93a9SHari Bathini 	while (msize) {
4077dee93a9SHari Bathini 		if (msize > max_size)
4087dee93a9SHari Bathini 			rsize = max_size;
4097dee93a9SHari Bathini 		else
4107dee93a9SHari Bathini 			rsize = msize;
4117dee93a9SHari Bathini 
4127dee93a9SHari Bathini 		ret = add_boot_mem_region(rstart, rsize);
4137dee93a9SHari Bathini 		if (!ret)
4147dee93a9SHari Bathini 			break;
4157dee93a9SHari Bathini 
4167dee93a9SHari Bathini 		msize -= rsize;
4177dee93a9SHari Bathini 		rstart += rsize;
4187dee93a9SHari Bathini 	}
4197dee93a9SHari Bathini 
4207dee93a9SHari Bathini 	return ret;
4217dee93a9SHari Bathini }
4227dee93a9SHari Bathini 
4237dee93a9SHari Bathini static int __init fadump_get_boot_mem_regions(void)
4247dee93a9SHari Bathini {
425b10d6bcaSMike Rapoport 	unsigned long size, cur_size, hole_size, last_end;
4267dee93a9SHari Bathini 	unsigned long mem_size = fw_dump.boot_memory_size;
427b10d6bcaSMike Rapoport 	phys_addr_t reg_start, reg_end;
4287dee93a9SHari Bathini 	int ret = 1;
429b10d6bcaSMike Rapoport 	u64 i;
4307dee93a9SHari Bathini 
4317dee93a9SHari Bathini 	fw_dump.boot_mem_regs_cnt = 0;
4327dee93a9SHari Bathini 
4337dee93a9SHari Bathini 	last_end = 0;
4347dee93a9SHari Bathini 	hole_size = 0;
4357dee93a9SHari Bathini 	cur_size = 0;
436b10d6bcaSMike Rapoport 	for_each_mem_range(i, &reg_start, &reg_end) {
437b10d6bcaSMike Rapoport 		size = reg_end - reg_start;
438b10d6bcaSMike Rapoport 		hole_size += (reg_start - last_end);
4397dee93a9SHari Bathini 
4407dee93a9SHari Bathini 		if ((cur_size + size) >= mem_size) {
4417dee93a9SHari Bathini 			size = (mem_size - cur_size);
442b10d6bcaSMike Rapoport 			ret = add_boot_mem_regions(reg_start, size);
4437dee93a9SHari Bathini 			break;
4447dee93a9SHari Bathini 		}
4457dee93a9SHari Bathini 
4467dee93a9SHari Bathini 		mem_size -= size;
4477dee93a9SHari Bathini 		cur_size += size;
448b10d6bcaSMike Rapoport 		ret = add_boot_mem_regions(reg_start, size);
4497dee93a9SHari Bathini 		if (!ret)
4507dee93a9SHari Bathini 			break;
4517dee93a9SHari Bathini 
452b10d6bcaSMike Rapoport 		last_end = reg_end;
4537dee93a9SHari Bathini 	}
4547dee93a9SHari Bathini 	fw_dump.boot_mem_top = PAGE_ALIGN(fw_dump.boot_memory_size + hole_size);
4557dee93a9SHari Bathini 
4567dee93a9SHari Bathini 	return ret;
4577dee93a9SHari Bathini }
4587dee93a9SHari Bathini 
459140777a3SHari Bathini /*
460140777a3SHari Bathini  * Returns true, if the given range overlaps with reserved memory ranges
461140777a3SHari Bathini  * starting at idx. Also, updates idx to index of overlapping memory range
462140777a3SHari Bathini  * with the given memory range.
463140777a3SHari Bathini  * False, otherwise.
464140777a3SHari Bathini  */
465140777a3SHari Bathini static bool overlaps_reserved_ranges(u64 base, u64 end, int *idx)
466140777a3SHari Bathini {
467140777a3SHari Bathini 	bool ret = false;
468140777a3SHari Bathini 	int i;
469140777a3SHari Bathini 
470140777a3SHari Bathini 	for (i = *idx; i < reserved_mrange_info.mem_range_cnt; i++) {
471140777a3SHari Bathini 		u64 rbase = reserved_mrange_info.mem_ranges[i].base;
472140777a3SHari Bathini 		u64 rend = rbase + reserved_mrange_info.mem_ranges[i].size;
473140777a3SHari Bathini 
474140777a3SHari Bathini 		if (end <= rbase)
475140777a3SHari Bathini 			break;
476140777a3SHari Bathini 
477140777a3SHari Bathini 		if ((end > rbase) &&  (base < rend)) {
478140777a3SHari Bathini 			*idx = i;
479140777a3SHari Bathini 			ret = true;
480140777a3SHari Bathini 			break;
481140777a3SHari Bathini 		}
482140777a3SHari Bathini 	}
483140777a3SHari Bathini 
484140777a3SHari Bathini 	return ret;
485140777a3SHari Bathini }
486140777a3SHari Bathini 
487140777a3SHari Bathini /*
488140777a3SHari Bathini  * Locate a suitable memory area to reserve memory for FADump. While at it,
489140777a3SHari Bathini  * lookup reserved-ranges & avoid overlap with them, as they are used by F/W.
490140777a3SHari Bathini  */
491140777a3SHari Bathini static u64 __init fadump_locate_reserve_mem(u64 base, u64 size)
492140777a3SHari Bathini {
493140777a3SHari Bathini 	struct fadump_memory_range *mrngs;
494140777a3SHari Bathini 	phys_addr_t mstart, mend;
495140777a3SHari Bathini 	int idx = 0;
496140777a3SHari Bathini 	u64 i, ret = 0;
497140777a3SHari Bathini 
498140777a3SHari Bathini 	mrngs = reserved_mrange_info.mem_ranges;
499140777a3SHari Bathini 	for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE,
500140777a3SHari Bathini 				&mstart, &mend, NULL) {
501140777a3SHari Bathini 		pr_debug("%llu) mstart: %llx, mend: %llx, base: %llx\n",
502140777a3SHari Bathini 			 i, mstart, mend, base);
503140777a3SHari Bathini 
504140777a3SHari Bathini 		if (mstart > base)
505140777a3SHari Bathini 			base = PAGE_ALIGN(mstart);
506140777a3SHari Bathini 
507140777a3SHari Bathini 		while ((mend > base) && ((mend - base) >= size)) {
508140777a3SHari Bathini 			if (!overlaps_reserved_ranges(base, base+size, &idx)) {
509140777a3SHari Bathini 				ret = base;
510140777a3SHari Bathini 				goto out;
511140777a3SHari Bathini 			}
512140777a3SHari Bathini 
513140777a3SHari Bathini 			base = mrngs[idx].base + mrngs[idx].size;
514140777a3SHari Bathini 			base = PAGE_ALIGN(base);
515140777a3SHari Bathini 		}
516140777a3SHari Bathini 	}
517140777a3SHari Bathini 
518140777a3SHari Bathini out:
519140777a3SHari Bathini 	return ret;
520140777a3SHari Bathini }
521140777a3SHari Bathini 
522eb39c880SMahesh Salgaonkar int __init fadump_reserve_mem(void)
523eb39c880SMahesh Salgaonkar {
524140777a3SHari Bathini 	u64 base, size, mem_boundary, bootmem_min;
5256abec12cSHari Bathini 	int ret = 1;
526eb39c880SMahesh Salgaonkar 
527eb39c880SMahesh Salgaonkar 	if (!fw_dump.fadump_enabled)
528eb39c880SMahesh Salgaonkar 		return 0;
529eb39c880SMahesh Salgaonkar 
530eb39c880SMahesh Salgaonkar 	if (!fw_dump.fadump_supported) {
5316abec12cSHari Bathini 		pr_info("Firmware-Assisted Dump is not supported on this hardware\n");
5326abec12cSHari Bathini 		goto error_out;
533eb39c880SMahesh Salgaonkar 	}
534742a265aSHari Bathini 
5353ccc00a7SMahesh Salgaonkar 	/*
5363ccc00a7SMahesh Salgaonkar 	 * Initialize boot memory size
5373ccc00a7SMahesh Salgaonkar 	 * If dump is active then we have already calculated the size during
5383ccc00a7SMahesh Salgaonkar 	 * first kernel.
5393ccc00a7SMahesh Salgaonkar 	 */
540f3512011SHari Bathini 	if (!fw_dump.dump_active) {
5416abec12cSHari Bathini 		fw_dump.boot_memory_size =
5426abec12cSHari Bathini 			PAGE_ALIGN(fadump_calculate_reserve_size());
543a4e92ce8SMahesh Salgaonkar #ifdef CONFIG_CMA
544579ca1a2SHari Bathini 		if (!fw_dump.nocma) {
545a4e92ce8SMahesh Salgaonkar 			fw_dump.boot_memory_size =
546140777a3SHari Bathini 				ALIGN(fw_dump.boot_memory_size,
547140777a3SHari Bathini 				      FADUMP_CMA_ALIGNMENT);
548579ca1a2SHari Bathini 		}
549a4e92ce8SMahesh Salgaonkar #endif
5507b1b3b48SHari Bathini 
5517b1b3b48SHari Bathini 		bootmem_min = fw_dump.ops->fadump_get_bootmem_min();
5527b1b3b48SHari Bathini 		if (fw_dump.boot_memory_size < bootmem_min) {
5537b1b3b48SHari Bathini 			pr_err("Can't enable fadump with boot memory size (0x%lx) less than 0x%llx\n",
5547b1b3b48SHari Bathini 			       fw_dump.boot_memory_size, bootmem_min);
5557b1b3b48SHari Bathini 			goto error_out;
5567b1b3b48SHari Bathini 		}
5577dee93a9SHari Bathini 
5587dee93a9SHari Bathini 		if (!fadump_get_boot_mem_regions()) {
5597dee93a9SHari Bathini 			pr_err("Too many holes in boot memory area to enable fadump\n");
5607dee93a9SHari Bathini 			goto error_out;
5617dee93a9SHari Bathini 		}
562a4e92ce8SMahesh Salgaonkar 	}
563eb39c880SMahesh Salgaonkar 
564eb39c880SMahesh Salgaonkar 	/*
565eb39c880SMahesh Salgaonkar 	 * Calculate the memory boundary.
566eb39c880SMahesh Salgaonkar 	 * If memory_limit is less than actual memory boundary then reserve
567eb39c880SMahesh Salgaonkar 	 * the memory for fadump beyond the memory_limit and adjust the
568eb39c880SMahesh Salgaonkar 	 * memory_limit accordingly, so that the running kernel can run with
569eb39c880SMahesh Salgaonkar 	 * specified memory_limit.
570eb39c880SMahesh Salgaonkar 	 */
571eb39c880SMahesh Salgaonkar 	if (memory_limit && memory_limit < memblock_end_of_DRAM()) {
572eb39c880SMahesh Salgaonkar 		size = get_fadump_area_size();
573eb39c880SMahesh Salgaonkar 		if ((memory_limit + size) < memblock_end_of_DRAM())
574eb39c880SMahesh Salgaonkar 			memory_limit += size;
575eb39c880SMahesh Salgaonkar 		else
576eb39c880SMahesh Salgaonkar 			memory_limit = memblock_end_of_DRAM();
577eb39c880SMahesh Salgaonkar 		printk(KERN_INFO "Adjusted memory_limit for firmware-assisted"
578a84fcd46SSuzuki Poulose 				" dump, now %#016llx\n", memory_limit);
579eb39c880SMahesh Salgaonkar 	}
580eb39c880SMahesh Salgaonkar 	if (memory_limit)
5816abec12cSHari Bathini 		mem_boundary = memory_limit;
582eb39c880SMahesh Salgaonkar 	else
5836abec12cSHari Bathini 		mem_boundary = memblock_end_of_DRAM();
584eb39c880SMahesh Salgaonkar 
5857dee93a9SHari Bathini 	base = fw_dump.boot_mem_top;
5868255da95SHari Bathini 	size = get_fadump_area_size();
5878255da95SHari Bathini 	fw_dump.reserve_dump_area_size = size;
588eb39c880SMahesh Salgaonkar 	if (fw_dump.dump_active) {
589b71a693dSMahesh Salgaonkar 		pr_info("Firmware-assisted dump is active.\n");
590b71a693dSMahesh Salgaonkar 
59185975387SHari Bathini #ifdef CONFIG_HUGETLB_PAGE
59285975387SHari Bathini 		/*
59385975387SHari Bathini 		 * FADump capture kernel doesn't care much about hugepages.
59485975387SHari Bathini 		 * In fact, handling hugepages in capture kernel is asking for
59585975387SHari Bathini 		 * trouble. So, disable HugeTLB support when fadump is active.
59685975387SHari Bathini 		 */
59785975387SHari Bathini 		hugetlb_disabled = true;
59885975387SHari Bathini #endif
599eb39c880SMahesh Salgaonkar 		/*
600eb39c880SMahesh Salgaonkar 		 * If last boot has crashed then reserve all the memory
601b2a815a5SHari Bathini 		 * above boot memory size so that we don't touch it until
602eb39c880SMahesh Salgaonkar 		 * dump is written to disk by userspace tool. This memory
603b2a815a5SHari Bathini 		 * can be released for general use by invalidating fadump.
604eb39c880SMahesh Salgaonkar 		 */
605b2a815a5SHari Bathini 		fadump_reserve_crash_area(base);
6062df173d9SMahesh Salgaonkar 
607f3512011SHari Bathini 		pr_debug("fadumphdr_addr = %#016lx\n", fw_dump.fadumphdr_addr);
608f3512011SHari Bathini 		pr_debug("Reserve dump area start address: 0x%lx\n",
609f3512011SHari Bathini 			 fw_dump.reserve_dump_area_start);
6108255da95SHari Bathini 	} else {
6118255da95SHari Bathini 		/*
612f6e6bedbSHari Bathini 		 * Reserve memory at an offset closer to bottom of the RAM to
613579ca1a2SHari Bathini 		 * minimize the impact of memory hot-remove operation.
614f6e6bedbSHari Bathini 		 */
615140777a3SHari Bathini 		base = fadump_locate_reserve_mem(base, size);
616f6e6bedbSHari Bathini 
6179a2921e5SHari Bathini 		if (!base || (base + size > mem_boundary)) {
618742a265aSHari Bathini 			pr_err("Failed to find memory chunk for reservation!\n");
619742a265aSHari Bathini 			goto error_out;
620742a265aSHari Bathini 		}
621742a265aSHari Bathini 		fw_dump.reserve_dump_area_start = base;
622742a265aSHari Bathini 
623742a265aSHari Bathini 		/*
624742a265aSHari Bathini 		 * Calculate the kernel metadata address and register it with
625742a265aSHari Bathini 		 * f/w if the platform supports.
626742a265aSHari Bathini 		 */
627742a265aSHari Bathini 		if (fw_dump.ops->fadump_setup_metadata &&
628742a265aSHari Bathini 		    (fw_dump.ops->fadump_setup_metadata(&fw_dump) < 0))
629742a265aSHari Bathini 			goto error_out;
630742a265aSHari Bathini 
631742a265aSHari Bathini 		if (memblock_reserve(base, size)) {
6326abec12cSHari Bathini 			pr_err("Failed to reserve memory!\n");
6336abec12cSHari Bathini 			goto error_out;
6346abec12cSHari Bathini 		}
6356abec12cSHari Bathini 
6366abec12cSHari Bathini 		pr_info("Reserved %lldMB of memory at %#016llx (System RAM: %lldMB)\n",
6376abec12cSHari Bathini 			(size >> 20), base, (memblock_phys_mem_size() >> 20));
638f6e6bedbSHari Bathini 
6396abec12cSHari Bathini 		ret = fadump_cma_init();
640a4e92ce8SMahesh Salgaonkar 	}
6416abec12cSHari Bathini 
6426abec12cSHari Bathini 	return ret;
6436abec12cSHari Bathini error_out:
6446abec12cSHari Bathini 	fw_dump.fadump_enabled = 0;
6456abec12cSHari Bathini 	return 0;
646eb39c880SMahesh Salgaonkar }
647eb39c880SMahesh Salgaonkar 
648eb39c880SMahesh Salgaonkar /* Look for fadump= cmdline option. */
649eb39c880SMahesh Salgaonkar static int __init early_fadump_param(char *p)
650eb39c880SMahesh Salgaonkar {
651eb39c880SMahesh Salgaonkar 	if (!p)
652eb39c880SMahesh Salgaonkar 		return 1;
653eb39c880SMahesh Salgaonkar 
654eb39c880SMahesh Salgaonkar 	if (strncmp(p, "on", 2) == 0)
655eb39c880SMahesh Salgaonkar 		fw_dump.fadump_enabled = 1;
656eb39c880SMahesh Salgaonkar 	else if (strncmp(p, "off", 3) == 0)
657eb39c880SMahesh Salgaonkar 		fw_dump.fadump_enabled = 0;
658a4e92ce8SMahesh Salgaonkar 	else if (strncmp(p, "nocma", 5) == 0) {
659a4e92ce8SMahesh Salgaonkar 		fw_dump.fadump_enabled = 1;
660a4e92ce8SMahesh Salgaonkar 		fw_dump.nocma = 1;
661a4e92ce8SMahesh Salgaonkar 	}
662eb39c880SMahesh Salgaonkar 
663eb39c880SMahesh Salgaonkar 	return 0;
664eb39c880SMahesh Salgaonkar }
665eb39c880SMahesh Salgaonkar early_param("fadump", early_fadump_param);
666eb39c880SMahesh Salgaonkar 
66781d9eca5SHari Bathini /*
66881d9eca5SHari Bathini  * Look for fadump_reserve_mem= cmdline option
66981d9eca5SHari Bathini  * TODO: Remove references to 'fadump_reserve_mem=' parameter,
67081d9eca5SHari Bathini  *       the sooner 'crashkernel=' parameter is accustomed to.
67181d9eca5SHari Bathini  */
67281d9eca5SHari Bathini static int __init early_fadump_reserve_mem(char *p)
67381d9eca5SHari Bathini {
67481d9eca5SHari Bathini 	if (p)
67581d9eca5SHari Bathini 		fw_dump.reserve_bootvar = memparse(p, &p);
67681d9eca5SHari Bathini 	return 0;
67781d9eca5SHari Bathini }
67881d9eca5SHari Bathini early_param("fadump_reserve_mem", early_fadump_reserve_mem);
67981d9eca5SHari Bathini 
680ebaeb5aeSMahesh Salgaonkar void crash_fadump(struct pt_regs *regs, const char *str)
681ebaeb5aeSMahesh Salgaonkar {
682ba608c4fSSourabh Jain 	unsigned int msecs;
683ebaeb5aeSMahesh Salgaonkar 	struct fadump_crash_info_header *fdh = NULL;
684f2a5e8f0SMahesh Salgaonkar 	int old_cpu, this_cpu;
685ba608c4fSSourabh Jain 	/* Do not include first CPU */
686ba608c4fSSourabh Jain 	unsigned int ncpus = num_online_cpus() - 1;
687ebaeb5aeSMahesh Salgaonkar 
6886fcd6baaSNicholas Piggin 	if (!should_fadump_crash())
689ebaeb5aeSMahesh Salgaonkar 		return;
690ebaeb5aeSMahesh Salgaonkar 
691f2a5e8f0SMahesh Salgaonkar 	/*
692f2a5e8f0SMahesh Salgaonkar 	 * old_cpu == -1 means this is the first CPU which has come here,
693f2a5e8f0SMahesh Salgaonkar 	 * go ahead and trigger fadump.
694f2a5e8f0SMahesh Salgaonkar 	 *
695f2a5e8f0SMahesh Salgaonkar 	 * old_cpu != -1 means some other CPU has already on it's way
696f2a5e8f0SMahesh Salgaonkar 	 * to trigger fadump, just keep looping here.
697f2a5e8f0SMahesh Salgaonkar 	 */
698f2a5e8f0SMahesh Salgaonkar 	this_cpu = smp_processor_id();
699f2a5e8f0SMahesh Salgaonkar 	old_cpu = cmpxchg(&crashing_cpu, -1, this_cpu);
700f2a5e8f0SMahesh Salgaonkar 
701f2a5e8f0SMahesh Salgaonkar 	if (old_cpu != -1) {
702ba608c4fSSourabh Jain 		atomic_inc(&cpus_in_fadump);
703ba608c4fSSourabh Jain 
704f2a5e8f0SMahesh Salgaonkar 		/*
705f2a5e8f0SMahesh Salgaonkar 		 * We can't loop here indefinitely. Wait as long as fadump
706f2a5e8f0SMahesh Salgaonkar 		 * is in force. If we race with fadump un-registration this
707f2a5e8f0SMahesh Salgaonkar 		 * loop will break and then we go down to normal panic path
708f2a5e8f0SMahesh Salgaonkar 		 * and reboot. If fadump is in force the first crashing
709f2a5e8f0SMahesh Salgaonkar 		 * cpu will definitely trigger fadump.
710f2a5e8f0SMahesh Salgaonkar 		 */
711f2a5e8f0SMahesh Salgaonkar 		while (fw_dump.dump_registered)
712f2a5e8f0SMahesh Salgaonkar 			cpu_relax();
713f2a5e8f0SMahesh Salgaonkar 		return;
714f2a5e8f0SMahesh Salgaonkar 	}
715f2a5e8f0SMahesh Salgaonkar 
716ebaeb5aeSMahesh Salgaonkar 	fdh = __va(fw_dump.fadumphdr_addr);
717ebaeb5aeSMahesh Salgaonkar 	fdh->crashing_cpu = crashing_cpu;
718ebaeb5aeSMahesh Salgaonkar 	crash_save_vmcoreinfo();
719ebaeb5aeSMahesh Salgaonkar 
720ebaeb5aeSMahesh Salgaonkar 	if (regs)
721ebaeb5aeSMahesh Salgaonkar 		fdh->regs = *regs;
722ebaeb5aeSMahesh Salgaonkar 	else
723ebaeb5aeSMahesh Salgaonkar 		ppc_save_regs(&fdh->regs);
724ebaeb5aeSMahesh Salgaonkar 
725a0512164SRasmus Villemoes 	fdh->online_mask = *cpu_online_mask;
726ebaeb5aeSMahesh Salgaonkar 
727ba608c4fSSourabh Jain 	/*
728ba608c4fSSourabh Jain 	 * If we came in via system reset, wait a while for the secondary
729ba608c4fSSourabh Jain 	 * CPUs to enter.
730ba608c4fSSourabh Jain 	 */
731*7153d4bfSXiongwei Song 	if (TRAP(&(fdh->regs)) == INTERRUPT_SYSTEM_RESET) {
732ba608c4fSSourabh Jain 		msecs = CRASH_TIMEOUT;
733ba608c4fSSourabh Jain 		while ((atomic_read(&cpus_in_fadump) < ncpus) && (--msecs > 0))
734ba608c4fSSourabh Jain 			mdelay(1);
735ba608c4fSSourabh Jain 	}
736ba608c4fSSourabh Jain 
73741a65d16SHari Bathini 	fw_dump.ops->fadump_trigger(fdh, str);
738ebaeb5aeSMahesh Salgaonkar }
739ebaeb5aeSMahesh Salgaonkar 
7407f0ad11dSHari Bathini u32 *fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs)
741ebaeb5aeSMahesh Salgaonkar {
742ebaeb5aeSMahesh Salgaonkar 	struct elf_prstatus prstatus;
743ebaeb5aeSMahesh Salgaonkar 
744ebaeb5aeSMahesh Salgaonkar 	memset(&prstatus, 0, sizeof(prstatus));
745ebaeb5aeSMahesh Salgaonkar 	/*
746ebaeb5aeSMahesh Salgaonkar 	 * FIXME: How do i get PID? Do I really need it?
747ebaeb5aeSMahesh Salgaonkar 	 * prstatus.pr_pid = ????
748ebaeb5aeSMahesh Salgaonkar 	 */
749ebaeb5aeSMahesh Salgaonkar 	elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
75022bd0177SHari Bathini 	buf = append_elf_note(buf, CRASH_CORE_NOTE_NAME, NT_PRSTATUS,
751ebaeb5aeSMahesh Salgaonkar 			      &prstatus, sizeof(prstatus));
752ebaeb5aeSMahesh Salgaonkar 	return buf;
753ebaeb5aeSMahesh Salgaonkar }
754ebaeb5aeSMahesh Salgaonkar 
7557f0ad11dSHari Bathini void fadump_update_elfcore_header(char *bufp)
756ebaeb5aeSMahesh Salgaonkar {
757ebaeb5aeSMahesh Salgaonkar 	struct elf_phdr *phdr;
758ebaeb5aeSMahesh Salgaonkar 
759ebaeb5aeSMahesh Salgaonkar 	bufp += sizeof(struct elfhdr);
760ebaeb5aeSMahesh Salgaonkar 
761ebaeb5aeSMahesh Salgaonkar 	/* First note is a place holder for cpu notes info. */
762ebaeb5aeSMahesh Salgaonkar 	phdr = (struct elf_phdr *)bufp;
763ebaeb5aeSMahesh Salgaonkar 
764ebaeb5aeSMahesh Salgaonkar 	if (phdr->p_type == PT_NOTE) {
765961cf26aSHari Bathini 		phdr->p_paddr	= __pa(fw_dump.cpu_notes_buf_vaddr);
766ebaeb5aeSMahesh Salgaonkar 		phdr->p_offset	= phdr->p_paddr;
767ebaeb5aeSMahesh Salgaonkar 		phdr->p_filesz	= fw_dump.cpu_notes_buf_size;
768ebaeb5aeSMahesh Salgaonkar 		phdr->p_memsz = fw_dump.cpu_notes_buf_size;
769ebaeb5aeSMahesh Salgaonkar 	}
770ebaeb5aeSMahesh Salgaonkar 	return;
771ebaeb5aeSMahesh Salgaonkar }
772ebaeb5aeSMahesh Salgaonkar 
773961cf26aSHari Bathini static void *fadump_alloc_buffer(unsigned long size)
774ebaeb5aeSMahesh Salgaonkar {
77572aa6517SHari Bathini 	unsigned long count, i;
776ebaeb5aeSMahesh Salgaonkar 	struct page *page;
77772aa6517SHari Bathini 	void *vaddr;
778ebaeb5aeSMahesh Salgaonkar 
77972aa6517SHari Bathini 	vaddr = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
780ebaeb5aeSMahesh Salgaonkar 	if (!vaddr)
781ebaeb5aeSMahesh Salgaonkar 		return NULL;
782ebaeb5aeSMahesh Salgaonkar 
78372aa6517SHari Bathini 	count = PAGE_ALIGN(size) / PAGE_SIZE;
784ebaeb5aeSMahesh Salgaonkar 	page = virt_to_page(vaddr);
785ebaeb5aeSMahesh Salgaonkar 	for (i = 0; i < count; i++)
78672aa6517SHari Bathini 		mark_page_reserved(page + i);
787ebaeb5aeSMahesh Salgaonkar 	return vaddr;
788ebaeb5aeSMahesh Salgaonkar }
789ebaeb5aeSMahesh Salgaonkar 
790961cf26aSHari Bathini static void fadump_free_buffer(unsigned long vaddr, unsigned long size)
791ebaeb5aeSMahesh Salgaonkar {
79272aa6517SHari Bathini 	free_reserved_area((void *)vaddr, (void *)(vaddr + size), -1, NULL);
793ebaeb5aeSMahesh Salgaonkar }
794ebaeb5aeSMahesh Salgaonkar 
7957f0ad11dSHari Bathini s32 fadump_setup_cpu_notes_buf(u32 num_cpus)
796961cf26aSHari Bathini {
797961cf26aSHari Bathini 	/* Allocate buffer to hold cpu crash notes. */
798961cf26aSHari Bathini 	fw_dump.cpu_notes_buf_size = num_cpus * sizeof(note_buf_t);
799961cf26aSHari Bathini 	fw_dump.cpu_notes_buf_size = PAGE_ALIGN(fw_dump.cpu_notes_buf_size);
800961cf26aSHari Bathini 	fw_dump.cpu_notes_buf_vaddr =
801961cf26aSHari Bathini 		(unsigned long)fadump_alloc_buffer(fw_dump.cpu_notes_buf_size);
802961cf26aSHari Bathini 	if (!fw_dump.cpu_notes_buf_vaddr) {
803961cf26aSHari Bathini 		pr_err("Failed to allocate %ld bytes for CPU notes buffer\n",
804961cf26aSHari Bathini 		       fw_dump.cpu_notes_buf_size);
805961cf26aSHari Bathini 		return -ENOMEM;
806961cf26aSHari Bathini 	}
807961cf26aSHari Bathini 
808961cf26aSHari Bathini 	pr_debug("Allocated buffer for cpu notes of size %ld at 0x%lx\n",
809961cf26aSHari Bathini 		 fw_dump.cpu_notes_buf_size,
810961cf26aSHari Bathini 		 fw_dump.cpu_notes_buf_vaddr);
811961cf26aSHari Bathini 	return 0;
812961cf26aSHari Bathini }
813961cf26aSHari Bathini 
8147f0ad11dSHari Bathini void fadump_free_cpu_notes_buf(void)
815961cf26aSHari Bathini {
816961cf26aSHari Bathini 	if (!fw_dump.cpu_notes_buf_vaddr)
817961cf26aSHari Bathini 		return;
818961cf26aSHari Bathini 
819961cf26aSHari Bathini 	fadump_free_buffer(fw_dump.cpu_notes_buf_vaddr,
820961cf26aSHari Bathini 			   fw_dump.cpu_notes_buf_size);
821961cf26aSHari Bathini 	fw_dump.cpu_notes_buf_vaddr = 0;
822961cf26aSHari Bathini 	fw_dump.cpu_notes_buf_size = 0;
823961cf26aSHari Bathini }
824961cf26aSHari Bathini 
825e4fc48fbSHari Bathini static void fadump_free_mem_ranges(struct fadump_mrange_info *mrange_info)
8261bd6a1c4SHari Bathini {
82702c04e37SHari Bathini 	if (mrange_info->is_static) {
82802c04e37SHari Bathini 		mrange_info->mem_range_cnt = 0;
82902c04e37SHari Bathini 		return;
83002c04e37SHari Bathini 	}
83102c04e37SHari Bathini 
832e4fc48fbSHari Bathini 	kfree(mrange_info->mem_ranges);
83302c04e37SHari Bathini 	memset((void *)((u64)mrange_info + RNG_NAME_SZ), 0,
83402c04e37SHari Bathini 	       (sizeof(struct fadump_mrange_info) - RNG_NAME_SZ));
8351bd6a1c4SHari Bathini }
8361bd6a1c4SHari Bathini 
8371bd6a1c4SHari Bathini /*
838e4fc48fbSHari Bathini  * Allocate or reallocate mem_ranges array in incremental units
8391bd6a1c4SHari Bathini  * of PAGE_SIZE.
8401bd6a1c4SHari Bathini  */
841e4fc48fbSHari Bathini static int fadump_alloc_mem_ranges(struct fadump_mrange_info *mrange_info)
8421bd6a1c4SHari Bathini {
843e4fc48fbSHari Bathini 	struct fadump_memory_range *new_array;
8441bd6a1c4SHari Bathini 	u64 new_size;
8451bd6a1c4SHari Bathini 
846e4fc48fbSHari Bathini 	new_size = mrange_info->mem_ranges_sz + PAGE_SIZE;
847e4fc48fbSHari Bathini 	pr_debug("Allocating %llu bytes of memory for %s memory ranges\n",
848e4fc48fbSHari Bathini 		 new_size, mrange_info->name);
8491bd6a1c4SHari Bathini 
850e4fc48fbSHari Bathini 	new_array = krealloc(mrange_info->mem_ranges, new_size, GFP_KERNEL);
8511bd6a1c4SHari Bathini 	if (new_array == NULL) {
852e4fc48fbSHari Bathini 		pr_err("Insufficient memory for setting up %s memory ranges\n",
853e4fc48fbSHari Bathini 		       mrange_info->name);
854e4fc48fbSHari Bathini 		fadump_free_mem_ranges(mrange_info);
8551bd6a1c4SHari Bathini 		return -ENOMEM;
8561bd6a1c4SHari Bathini 	}
8571bd6a1c4SHari Bathini 
858e4fc48fbSHari Bathini 	mrange_info->mem_ranges = new_array;
859e4fc48fbSHari Bathini 	mrange_info->mem_ranges_sz = new_size;
860e4fc48fbSHari Bathini 	mrange_info->max_mem_ranges = (new_size /
861e4fc48fbSHari Bathini 				       sizeof(struct fadump_memory_range));
8621bd6a1c4SHari Bathini 	return 0;
8631bd6a1c4SHari Bathini }
8641bd6a1c4SHari Bathini 
865e4fc48fbSHari Bathini static inline int fadump_add_mem_range(struct fadump_mrange_info *mrange_info,
866e4fc48fbSHari Bathini 				       u64 base, u64 end)
8672df173d9SMahesh Salgaonkar {
868e4fc48fbSHari Bathini 	struct fadump_memory_range *mem_ranges = mrange_info->mem_ranges;
869ced1bf52SHari Bathini 	bool is_adjacent = false;
870e4fc48fbSHari Bathini 	u64 start, size;
871ced1bf52SHari Bathini 
8722df173d9SMahesh Salgaonkar 	if (base == end)
8731bd6a1c4SHari Bathini 		return 0;
8741bd6a1c4SHari Bathini 
875ced1bf52SHari Bathini 	/*
876ced1bf52SHari Bathini 	 * Fold adjacent memory ranges to bring down the memory ranges/
877ced1bf52SHari Bathini 	 * PT_LOAD segments count.
878ced1bf52SHari Bathini 	 */
879e4fc48fbSHari Bathini 	if (mrange_info->mem_range_cnt) {
880e4fc48fbSHari Bathini 		start = mem_ranges[mrange_info->mem_range_cnt - 1].base;
881e4fc48fbSHari Bathini 		size  = mem_ranges[mrange_info->mem_range_cnt - 1].size;
882ced1bf52SHari Bathini 
883ced1bf52SHari Bathini 		if ((start + size) == base)
884ced1bf52SHari Bathini 			is_adjacent = true;
885ced1bf52SHari Bathini 	}
886ced1bf52SHari Bathini 	if (!is_adjacent) {
887ced1bf52SHari Bathini 		/* resize the array on reaching the limit */
888e4fc48fbSHari Bathini 		if (mrange_info->mem_range_cnt == mrange_info->max_mem_ranges) {
8891bd6a1c4SHari Bathini 			int ret;
8901bd6a1c4SHari Bathini 
89102c04e37SHari Bathini 			if (mrange_info->is_static) {
89202c04e37SHari Bathini 				pr_err("Reached array size limit for %s memory ranges\n",
89302c04e37SHari Bathini 				       mrange_info->name);
89402c04e37SHari Bathini 				return -ENOSPC;
89502c04e37SHari Bathini 			}
89602c04e37SHari Bathini 
897e4fc48fbSHari Bathini 			ret = fadump_alloc_mem_ranges(mrange_info);
8981bd6a1c4SHari Bathini 			if (ret)
8991bd6a1c4SHari Bathini 				return ret;
900e4fc48fbSHari Bathini 
901e4fc48fbSHari Bathini 			/* Update to the new resized array */
902e4fc48fbSHari Bathini 			mem_ranges = mrange_info->mem_ranges;
9031bd6a1c4SHari Bathini 		}
9042df173d9SMahesh Salgaonkar 
905ced1bf52SHari Bathini 		start = base;
906e4fc48fbSHari Bathini 		mem_ranges[mrange_info->mem_range_cnt].base = start;
907e4fc48fbSHari Bathini 		mrange_info->mem_range_cnt++;
908ced1bf52SHari Bathini 	}
909ced1bf52SHari Bathini 
910e4fc48fbSHari Bathini 	mem_ranges[mrange_info->mem_range_cnt - 1].size = (end - start);
911e4fc48fbSHari Bathini 	pr_debug("%s_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n",
912e4fc48fbSHari Bathini 		 mrange_info->name, (mrange_info->mem_range_cnt - 1),
913e4fc48fbSHari Bathini 		 start, end - 1, (end - start));
9141bd6a1c4SHari Bathini 	return 0;
9152df173d9SMahesh Salgaonkar }
9162df173d9SMahesh Salgaonkar 
917e4fc48fbSHari Bathini static int fadump_exclude_reserved_area(u64 start, u64 end)
9182df173d9SMahesh Salgaonkar {
919e4fc48fbSHari Bathini 	u64 ra_start, ra_end;
9201bd6a1c4SHari Bathini 	int ret = 0;
9212df173d9SMahesh Salgaonkar 
9222df173d9SMahesh Salgaonkar 	ra_start = fw_dump.reserve_dump_area_start;
9232df173d9SMahesh Salgaonkar 	ra_end = ra_start + fw_dump.reserve_dump_area_size;
9242df173d9SMahesh Salgaonkar 
9252df173d9SMahesh Salgaonkar 	if ((ra_start < end) && (ra_end > start)) {
9262df173d9SMahesh Salgaonkar 		if ((start < ra_start) && (end > ra_end)) {
927e4fc48fbSHari Bathini 			ret = fadump_add_mem_range(&crash_mrange_info,
928e4fc48fbSHari Bathini 						   start, ra_start);
9291bd6a1c4SHari Bathini 			if (ret)
9301bd6a1c4SHari Bathini 				return ret;
9311bd6a1c4SHari Bathini 
932e4fc48fbSHari Bathini 			ret = fadump_add_mem_range(&crash_mrange_info,
933e4fc48fbSHari Bathini 						   ra_end, end);
9342df173d9SMahesh Salgaonkar 		} else if (start < ra_start) {
935e4fc48fbSHari Bathini 			ret = fadump_add_mem_range(&crash_mrange_info,
936e4fc48fbSHari Bathini 						   start, ra_start);
9372df173d9SMahesh Salgaonkar 		} else if (ra_end < end) {
938e4fc48fbSHari Bathini 			ret = fadump_add_mem_range(&crash_mrange_info,
939e4fc48fbSHari Bathini 						   ra_end, end);
9402df173d9SMahesh Salgaonkar 		}
9412df173d9SMahesh Salgaonkar 	} else
942e4fc48fbSHari Bathini 		ret = fadump_add_mem_range(&crash_mrange_info, start, end);
9431bd6a1c4SHari Bathini 
9441bd6a1c4SHari Bathini 	return ret;
9452df173d9SMahesh Salgaonkar }
9462df173d9SMahesh Salgaonkar 
9472df173d9SMahesh Salgaonkar static int fadump_init_elfcore_header(char *bufp)
9482df173d9SMahesh Salgaonkar {
9492df173d9SMahesh Salgaonkar 	struct elfhdr *elf;
9502df173d9SMahesh Salgaonkar 
9512df173d9SMahesh Salgaonkar 	elf = (struct elfhdr *) bufp;
9522df173d9SMahesh Salgaonkar 	bufp += sizeof(struct elfhdr);
9532df173d9SMahesh Salgaonkar 	memcpy(elf->e_ident, ELFMAG, SELFMAG);
9542df173d9SMahesh Salgaonkar 	elf->e_ident[EI_CLASS] = ELF_CLASS;
9552df173d9SMahesh Salgaonkar 	elf->e_ident[EI_DATA] = ELF_DATA;
9562df173d9SMahesh Salgaonkar 	elf->e_ident[EI_VERSION] = EV_CURRENT;
9572df173d9SMahesh Salgaonkar 	elf->e_ident[EI_OSABI] = ELF_OSABI;
9582df173d9SMahesh Salgaonkar 	memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
9592df173d9SMahesh Salgaonkar 	elf->e_type = ET_CORE;
9602df173d9SMahesh Salgaonkar 	elf->e_machine = ELF_ARCH;
9612df173d9SMahesh Salgaonkar 	elf->e_version = EV_CURRENT;
9622df173d9SMahesh Salgaonkar 	elf->e_entry = 0;
9632df173d9SMahesh Salgaonkar 	elf->e_phoff = sizeof(struct elfhdr);
9642df173d9SMahesh Salgaonkar 	elf->e_shoff = 0;
965d8bced27SDaniel Axtens #if defined(_CALL_ELF)
966d8bced27SDaniel Axtens 	elf->e_flags = _CALL_ELF;
967d8bced27SDaniel Axtens #else
968d8bced27SDaniel Axtens 	elf->e_flags = 0;
969d8bced27SDaniel Axtens #endif
9702df173d9SMahesh Salgaonkar 	elf->e_ehsize = sizeof(struct elfhdr);
9712df173d9SMahesh Salgaonkar 	elf->e_phentsize = sizeof(struct elf_phdr);
9722df173d9SMahesh Salgaonkar 	elf->e_phnum = 0;
9732df173d9SMahesh Salgaonkar 	elf->e_shentsize = 0;
9742df173d9SMahesh Salgaonkar 	elf->e_shnum = 0;
9752df173d9SMahesh Salgaonkar 	elf->e_shstrndx = 0;
9762df173d9SMahesh Salgaonkar 
9772df173d9SMahesh Salgaonkar 	return 0;
9782df173d9SMahesh Salgaonkar }
9792df173d9SMahesh Salgaonkar 
9802df173d9SMahesh Salgaonkar /*
9812df173d9SMahesh Salgaonkar  * Traverse through memblock structure and setup crash memory ranges. These
9822df173d9SMahesh Salgaonkar  * ranges will be used create PT_LOAD program headers in elfcore header.
9832df173d9SMahesh Salgaonkar  */
9841bd6a1c4SHari Bathini static int fadump_setup_crash_memory_ranges(void)
9852df173d9SMahesh Salgaonkar {
986b10d6bcaSMike Rapoport 	u64 i, start, end;
987b10d6bcaSMike Rapoport 	int ret;
9882df173d9SMahesh Salgaonkar 
9892df173d9SMahesh Salgaonkar 	pr_debug("Setup crash memory ranges.\n");
990e4fc48fbSHari Bathini 	crash_mrange_info.mem_range_cnt = 0;
991ced1bf52SHari Bathini 
9922df173d9SMahesh Salgaonkar 	/*
9937dee93a9SHari Bathini 	 * Boot memory region(s) registered with firmware are moved to
9947dee93a9SHari Bathini 	 * different location at the time of crash. Create separate program
9957dee93a9SHari Bathini 	 * header(s) for this memory chunk(s) with the correct offset.
9962df173d9SMahesh Salgaonkar 	 */
9977dee93a9SHari Bathini 	for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) {
9987dee93a9SHari Bathini 		start = fw_dump.boot_mem_addr[i];
9997dee93a9SHari Bathini 		end = start + fw_dump.boot_mem_sz[i];
10007dee93a9SHari Bathini 		ret = fadump_add_mem_range(&crash_mrange_info, start, end);
10011bd6a1c4SHari Bathini 		if (ret)
10021bd6a1c4SHari Bathini 			return ret;
10037dee93a9SHari Bathini 	}
10042df173d9SMahesh Salgaonkar 
1005b10d6bcaSMike Rapoport 	for_each_mem_range(i, &start, &end) {
1006a77af552SHari Bathini 		/*
10077dee93a9SHari Bathini 		 * skip the memory chunk that is already added
10087dee93a9SHari Bathini 		 * (0 through boot_memory_top).
1009a77af552SHari Bathini 		 */
10107dee93a9SHari Bathini 		if (start < fw_dump.boot_mem_top) {
10117dee93a9SHari Bathini 			if (end > fw_dump.boot_mem_top)
10127dee93a9SHari Bathini 				start = fw_dump.boot_mem_top;
1013a77af552SHari Bathini 			else
1014a77af552SHari Bathini 				continue;
1015a77af552SHari Bathini 		}
10162df173d9SMahesh Salgaonkar 
10172df173d9SMahesh Salgaonkar 		/* add this range excluding the reserved dump area. */
10181bd6a1c4SHari Bathini 		ret = fadump_exclude_reserved_area(start, end);
10191bd6a1c4SHari Bathini 		if (ret)
10201bd6a1c4SHari Bathini 			return ret;
10212df173d9SMahesh Salgaonkar 	}
10221bd6a1c4SHari Bathini 
10231bd6a1c4SHari Bathini 	return 0;
10242df173d9SMahesh Salgaonkar }
10252df173d9SMahesh Salgaonkar 
1026d34c5f26SMahesh Salgaonkar /*
1027d34c5f26SMahesh Salgaonkar  * If the given physical address falls within the boot memory region then
1028d34c5f26SMahesh Salgaonkar  * return the relocated address that points to the dump region reserved
1029d34c5f26SMahesh Salgaonkar  * for saving initial boot memory contents.
1030d34c5f26SMahesh Salgaonkar  */
1031d34c5f26SMahesh Salgaonkar static inline unsigned long fadump_relocate(unsigned long paddr)
1032d34c5f26SMahesh Salgaonkar {
10337dee93a9SHari Bathini 	unsigned long raddr, rstart, rend, rlast, hole_size;
10347dee93a9SHari Bathini 	int i;
10357dee93a9SHari Bathini 
10367dee93a9SHari Bathini 	hole_size = 0;
10377dee93a9SHari Bathini 	rlast = 0;
10387dee93a9SHari Bathini 	raddr = paddr;
10397dee93a9SHari Bathini 	for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) {
10407dee93a9SHari Bathini 		rstart = fw_dump.boot_mem_addr[i];
10417dee93a9SHari Bathini 		rend = rstart + fw_dump.boot_mem_sz[i];
10427dee93a9SHari Bathini 		hole_size += (rstart - rlast);
10437dee93a9SHari Bathini 
10447dee93a9SHari Bathini 		if (paddr >= rstart && paddr < rend) {
10457dee93a9SHari Bathini 			raddr += fw_dump.boot_mem_dest_addr - hole_size;
10467dee93a9SHari Bathini 			break;
10477dee93a9SHari Bathini 		}
10487dee93a9SHari Bathini 
10497dee93a9SHari Bathini 		rlast = rend;
10507dee93a9SHari Bathini 	}
10517dee93a9SHari Bathini 
10527dee93a9SHari Bathini 	pr_debug("vmcoreinfo: paddr = 0x%lx, raddr = 0x%lx\n", paddr, raddr);
10537dee93a9SHari Bathini 	return raddr;
1054d34c5f26SMahesh Salgaonkar }
1055d34c5f26SMahesh Salgaonkar 
10562df173d9SMahesh Salgaonkar static int fadump_create_elfcore_headers(char *bufp)
10572df173d9SMahesh Salgaonkar {
10587dee93a9SHari Bathini 	unsigned long long raddr, offset;
10592df173d9SMahesh Salgaonkar 	struct elf_phdr *phdr;
10607dee93a9SHari Bathini 	struct elfhdr *elf;
10617dee93a9SHari Bathini 	int i, j;
10622df173d9SMahesh Salgaonkar 
10632df173d9SMahesh Salgaonkar 	fadump_init_elfcore_header(bufp);
10642df173d9SMahesh Salgaonkar 	elf = (struct elfhdr *)bufp;
10652df173d9SMahesh Salgaonkar 	bufp += sizeof(struct elfhdr);
10662df173d9SMahesh Salgaonkar 
1067ebaeb5aeSMahesh Salgaonkar 	/*
1068ebaeb5aeSMahesh Salgaonkar 	 * setup ELF PT_NOTE, place holder for cpu notes info. The notes info
1069ebaeb5aeSMahesh Salgaonkar 	 * will be populated during second kernel boot after crash. Hence
1070ebaeb5aeSMahesh Salgaonkar 	 * this PT_NOTE will always be the first elf note.
1071ebaeb5aeSMahesh Salgaonkar 	 *
1072ebaeb5aeSMahesh Salgaonkar 	 * NOTE: Any new ELF note addition should be placed after this note.
1073ebaeb5aeSMahesh Salgaonkar 	 */
1074ebaeb5aeSMahesh Salgaonkar 	phdr = (struct elf_phdr *)bufp;
1075ebaeb5aeSMahesh Salgaonkar 	bufp += sizeof(struct elf_phdr);
1076ebaeb5aeSMahesh Salgaonkar 	phdr->p_type = PT_NOTE;
1077ebaeb5aeSMahesh Salgaonkar 	phdr->p_flags = 0;
1078ebaeb5aeSMahesh Salgaonkar 	phdr->p_vaddr = 0;
1079ebaeb5aeSMahesh Salgaonkar 	phdr->p_align = 0;
1080ebaeb5aeSMahesh Salgaonkar 
1081ebaeb5aeSMahesh Salgaonkar 	phdr->p_offset = 0;
1082ebaeb5aeSMahesh Salgaonkar 	phdr->p_paddr = 0;
1083ebaeb5aeSMahesh Salgaonkar 	phdr->p_filesz = 0;
1084ebaeb5aeSMahesh Salgaonkar 	phdr->p_memsz = 0;
1085ebaeb5aeSMahesh Salgaonkar 
1086ebaeb5aeSMahesh Salgaonkar 	(elf->e_phnum)++;
1087ebaeb5aeSMahesh Salgaonkar 
1088d34c5f26SMahesh Salgaonkar 	/* setup ELF PT_NOTE for vmcoreinfo */
1089d34c5f26SMahesh Salgaonkar 	phdr = (struct elf_phdr *)bufp;
1090d34c5f26SMahesh Salgaonkar 	bufp += sizeof(struct elf_phdr);
1091d34c5f26SMahesh Salgaonkar 	phdr->p_type	= PT_NOTE;
1092d34c5f26SMahesh Salgaonkar 	phdr->p_flags	= 0;
1093d34c5f26SMahesh Salgaonkar 	phdr->p_vaddr	= 0;
1094d34c5f26SMahesh Salgaonkar 	phdr->p_align	= 0;
1095d34c5f26SMahesh Salgaonkar 
1096d34c5f26SMahesh Salgaonkar 	phdr->p_paddr	= fadump_relocate(paddr_vmcoreinfo_note());
1097d34c5f26SMahesh Salgaonkar 	phdr->p_offset	= phdr->p_paddr;
10985203f499SXunlei Pang 	phdr->p_memsz	= phdr->p_filesz = VMCOREINFO_NOTE_SIZE;
1099d34c5f26SMahesh Salgaonkar 
1100d34c5f26SMahesh Salgaonkar 	/* Increment number of program headers. */
1101d34c5f26SMahesh Salgaonkar 	(elf->e_phnum)++;
1102d34c5f26SMahesh Salgaonkar 
11032df173d9SMahesh Salgaonkar 	/* setup PT_LOAD sections. */
11047dee93a9SHari Bathini 	j = 0;
11057dee93a9SHari Bathini 	offset = 0;
11067dee93a9SHari Bathini 	raddr = fw_dump.boot_mem_addr[0];
1107e4fc48fbSHari Bathini 	for (i = 0; i < crash_mrange_info.mem_range_cnt; i++) {
1108e4fc48fbSHari Bathini 		u64 mbase, msize;
11092df173d9SMahesh Salgaonkar 
1110e4fc48fbSHari Bathini 		mbase = crash_mrange_info.mem_ranges[i].base;
1111e4fc48fbSHari Bathini 		msize = crash_mrange_info.mem_ranges[i].size;
11122df173d9SMahesh Salgaonkar 		if (!msize)
11132df173d9SMahesh Salgaonkar 			continue;
11142df173d9SMahesh Salgaonkar 
11152df173d9SMahesh Salgaonkar 		phdr = (struct elf_phdr *)bufp;
11162df173d9SMahesh Salgaonkar 		bufp += sizeof(struct elf_phdr);
11172df173d9SMahesh Salgaonkar 		phdr->p_type	= PT_LOAD;
11182df173d9SMahesh Salgaonkar 		phdr->p_flags	= PF_R|PF_W|PF_X;
11192df173d9SMahesh Salgaonkar 		phdr->p_offset	= mbase;
11202df173d9SMahesh Salgaonkar 
11217dee93a9SHari Bathini 		if (mbase == raddr) {
11222df173d9SMahesh Salgaonkar 			/*
1123becd91d9SHari Bathini 			 * The entire real memory region will be moved by
1124becd91d9SHari Bathini 			 * firmware to the specified destination_address.
1125becd91d9SHari Bathini 			 * Hence set the correct offset.
11262df173d9SMahesh Salgaonkar 			 */
11277dee93a9SHari Bathini 			phdr->p_offset = fw_dump.boot_mem_dest_addr + offset;
11287dee93a9SHari Bathini 			if (j < (fw_dump.boot_mem_regs_cnt - 1)) {
11297dee93a9SHari Bathini 				offset += fw_dump.boot_mem_sz[j];
11307dee93a9SHari Bathini 				raddr = fw_dump.boot_mem_addr[++j];
11317dee93a9SHari Bathini 			}
11322df173d9SMahesh Salgaonkar 		}
11332df173d9SMahesh Salgaonkar 
11342df173d9SMahesh Salgaonkar 		phdr->p_paddr = mbase;
11352df173d9SMahesh Salgaonkar 		phdr->p_vaddr = (unsigned long)__va(mbase);
11362df173d9SMahesh Salgaonkar 		phdr->p_filesz = msize;
11372df173d9SMahesh Salgaonkar 		phdr->p_memsz = msize;
11382df173d9SMahesh Salgaonkar 		phdr->p_align = 0;
11392df173d9SMahesh Salgaonkar 
11402df173d9SMahesh Salgaonkar 		/* Increment number of program headers. */
11412df173d9SMahesh Salgaonkar 		(elf->e_phnum)++;
11422df173d9SMahesh Salgaonkar 	}
11432df173d9SMahesh Salgaonkar 	return 0;
11442df173d9SMahesh Salgaonkar }
11452df173d9SMahesh Salgaonkar 
11462df173d9SMahesh Salgaonkar static unsigned long init_fadump_header(unsigned long addr)
11472df173d9SMahesh Salgaonkar {
11482df173d9SMahesh Salgaonkar 	struct fadump_crash_info_header *fdh;
11492df173d9SMahesh Salgaonkar 
11502df173d9SMahesh Salgaonkar 	if (!addr)
11512df173d9SMahesh Salgaonkar 		return 0;
11522df173d9SMahesh Salgaonkar 
11532df173d9SMahesh Salgaonkar 	fdh = __va(addr);
11542df173d9SMahesh Salgaonkar 	addr += sizeof(struct fadump_crash_info_header);
11552df173d9SMahesh Salgaonkar 
11562df173d9SMahesh Salgaonkar 	memset(fdh, 0, sizeof(struct fadump_crash_info_header));
11572df173d9SMahesh Salgaonkar 	fdh->magic_number = FADUMP_CRASH_INFO_MAGIC;
11582df173d9SMahesh Salgaonkar 	fdh->elfcorehdr_addr = addr;
1159ebaeb5aeSMahesh Salgaonkar 	/* We will set the crashing cpu id in crash_fadump() during crash. */
11600226e552SHari Bathini 	fdh->crashing_cpu = FADUMP_CPU_UNKNOWN;
11612df173d9SMahesh Salgaonkar 
11622df173d9SMahesh Salgaonkar 	return addr;
11632df173d9SMahesh Salgaonkar }
11642df173d9SMahesh Salgaonkar 
116598b8cd7fSMichal Suchanek static int register_fadump(void)
11663ccc00a7SMahesh Salgaonkar {
11672df173d9SMahesh Salgaonkar 	unsigned long addr;
11682df173d9SMahesh Salgaonkar 	void *vaddr;
11691bd6a1c4SHari Bathini 	int ret;
11702df173d9SMahesh Salgaonkar 
11713ccc00a7SMahesh Salgaonkar 	/*
11723ccc00a7SMahesh Salgaonkar 	 * If no memory is reserved then we can not register for firmware-
11733ccc00a7SMahesh Salgaonkar 	 * assisted dump.
11743ccc00a7SMahesh Salgaonkar 	 */
11753ccc00a7SMahesh Salgaonkar 	if (!fw_dump.reserve_dump_area_size)
117698b8cd7fSMichal Suchanek 		return -ENODEV;
11773ccc00a7SMahesh Salgaonkar 
11781bd6a1c4SHari Bathini 	ret = fadump_setup_crash_memory_ranges();
11791bd6a1c4SHari Bathini 	if (ret)
11801bd6a1c4SHari Bathini 		return ret;
11812df173d9SMahesh Salgaonkar 
118241a65d16SHari Bathini 	addr = fw_dump.fadumphdr_addr;
118341a65d16SHari Bathini 
11842df173d9SMahesh Salgaonkar 	/* Initialize fadump crash info header. */
11852df173d9SMahesh Salgaonkar 	addr = init_fadump_header(addr);
11862df173d9SMahesh Salgaonkar 	vaddr = __va(addr);
11872df173d9SMahesh Salgaonkar 
11882df173d9SMahesh Salgaonkar 	pr_debug("Creating ELF core headers at %#016lx\n", addr);
11892df173d9SMahesh Salgaonkar 	fadump_create_elfcore_headers(vaddr);
11902df173d9SMahesh Salgaonkar 
11913ccc00a7SMahesh Salgaonkar 	/* register the future kernel dump with firmware. */
119241a65d16SHari Bathini 	pr_debug("Registering for firmware-assisted kernel dump...\n");
119341a65d16SHari Bathini 	return fw_dump.ops->fadump_register(&fw_dump);
11943ccc00a7SMahesh Salgaonkar }
11953ccc00a7SMahesh Salgaonkar 
1196b500afffSMahesh Salgaonkar void fadump_cleanup(void)
1197b500afffSMahesh Salgaonkar {
11982790d01dSHari Bathini 	if (!fw_dump.fadump_supported)
11992790d01dSHari Bathini 		return;
12002790d01dSHari Bathini 
1201b500afffSMahesh Salgaonkar 	/* Invalidate the registration only if dump is active. */
1202b500afffSMahesh Salgaonkar 	if (fw_dump.dump_active) {
1203f3512011SHari Bathini 		pr_debug("Invalidating firmware-assisted dump registration\n");
1204f3512011SHari Bathini 		fw_dump.ops->fadump_invalidate(&fw_dump);
1205722cde76SMahesh Salgaonkar 	} else if (fw_dump.dump_registered) {
1206722cde76SMahesh Salgaonkar 		/* Un-register Firmware-assisted dump if it was registered. */
120741a65d16SHari Bathini 		fw_dump.ops->fadump_unregister(&fw_dump);
1208e4fc48fbSHari Bathini 		fadump_free_mem_ranges(&crash_mrange_info);
1209b500afffSMahesh Salgaonkar 	}
12102790d01dSHari Bathini 
12112790d01dSHari Bathini 	if (fw_dump.ops->fadump_cleanup)
12122790d01dSHari Bathini 		fw_dump.ops->fadump_cleanup(&fw_dump);
1213b500afffSMahesh Salgaonkar }
1214b500afffSMahesh Salgaonkar 
121568fa6478SHari Bathini static void fadump_free_reserved_memory(unsigned long start_pfn,
121668fa6478SHari Bathini 					unsigned long end_pfn)
121768fa6478SHari Bathini {
121868fa6478SHari Bathini 	unsigned long pfn;
121968fa6478SHari Bathini 	unsigned long time_limit = jiffies + HZ;
122068fa6478SHari Bathini 
122168fa6478SHari Bathini 	pr_info("freeing reserved memory (0x%llx - 0x%llx)\n",
122268fa6478SHari Bathini 		PFN_PHYS(start_pfn), PFN_PHYS(end_pfn));
122368fa6478SHari Bathini 
122468fa6478SHari Bathini 	for (pfn = start_pfn; pfn < end_pfn; pfn++) {
122568fa6478SHari Bathini 		free_reserved_page(pfn_to_page(pfn));
122668fa6478SHari Bathini 
122768fa6478SHari Bathini 		if (time_after(jiffies, time_limit)) {
122868fa6478SHari Bathini 			cond_resched();
122968fa6478SHari Bathini 			time_limit = jiffies + HZ;
123068fa6478SHari Bathini 		}
123168fa6478SHari Bathini 	}
123268fa6478SHari Bathini }
123368fa6478SHari Bathini 
123468fa6478SHari Bathini /*
123568fa6478SHari Bathini  * Skip memory holes and free memory that was actually reserved.
123668fa6478SHari Bathini  */
1237dda9dbfeSHari Bathini static void fadump_release_reserved_area(u64 start, u64 end)
123868fa6478SHari Bathini {
1239b10d6bcaSMike Rapoport 	unsigned long reg_spfn, reg_epfn;
1240b10d6bcaSMike Rapoport 	u64 tstart, tend, spfn, epfn;
1241b10d6bcaSMike Rapoport 	int i;
124268fa6478SHari Bathini 
1243dda9dbfeSHari Bathini 	spfn = PHYS_PFN(start);
1244dda9dbfeSHari Bathini 	epfn = PHYS_PFN(end);
1245c9118e6cSMike Rapoport 
1246c9118e6cSMike Rapoport 	for_each_mem_pfn_range(i, MAX_NUMNODES, &reg_spfn, &reg_epfn, NULL) {
1247c9118e6cSMike Rapoport 		tstart = max_t(u64, spfn, reg_spfn);
1248c9118e6cSMike Rapoport 		tend   = min_t(u64, epfn, reg_epfn);
1249c9118e6cSMike Rapoport 
125068fa6478SHari Bathini 		if (tstart < tend) {
125168fa6478SHari Bathini 			fadump_free_reserved_memory(tstart, tend);
125268fa6478SHari Bathini 
1253dda9dbfeSHari Bathini 			if (tend == epfn)
125468fa6478SHari Bathini 				break;
125568fa6478SHari Bathini 
1256dda9dbfeSHari Bathini 			spfn = tend;
125768fa6478SHari Bathini 		}
125868fa6478SHari Bathini 	}
125968fa6478SHari Bathini }
126068fa6478SHari Bathini 
1261b500afffSMahesh Salgaonkar /*
1262dda9dbfeSHari Bathini  * Sort the mem ranges in-place and merge adjacent ranges
1263dda9dbfeSHari Bathini  * to minimize the memory ranges count.
1264b500afffSMahesh Salgaonkar  */
1265dda9dbfeSHari Bathini static void sort_and_merge_mem_ranges(struct fadump_mrange_info *mrange_info)
1266b500afffSMahesh Salgaonkar {
1267dda9dbfeSHari Bathini 	struct fadump_memory_range *mem_ranges;
1268dda9dbfeSHari Bathini 	struct fadump_memory_range tmp_range;
1269dda9dbfeSHari Bathini 	u64 base, size;
1270dda9dbfeSHari Bathini 	int i, j, idx;
1271dda9dbfeSHari Bathini 
1272dda9dbfeSHari Bathini 	if (!reserved_mrange_info.mem_range_cnt)
1273dda9dbfeSHari Bathini 		return;
1274dda9dbfeSHari Bathini 
1275dda9dbfeSHari Bathini 	/* Sort the memory ranges */
1276dda9dbfeSHari Bathini 	mem_ranges = mrange_info->mem_ranges;
1277dda9dbfeSHari Bathini 	for (i = 0; i < mrange_info->mem_range_cnt; i++) {
1278dda9dbfeSHari Bathini 		idx = i;
1279dda9dbfeSHari Bathini 		for (j = (i + 1); j < mrange_info->mem_range_cnt; j++) {
1280dda9dbfeSHari Bathini 			if (mem_ranges[idx].base > mem_ranges[j].base)
1281dda9dbfeSHari Bathini 				idx = j;
1282dda9dbfeSHari Bathini 		}
1283dda9dbfeSHari Bathini 		if (idx != i) {
1284dda9dbfeSHari Bathini 			tmp_range = mem_ranges[idx];
1285dda9dbfeSHari Bathini 			mem_ranges[idx] = mem_ranges[i];
1286dda9dbfeSHari Bathini 			mem_ranges[i] = tmp_range;
1287dda9dbfeSHari Bathini 		}
1288dda9dbfeSHari Bathini 	}
1289dda9dbfeSHari Bathini 
1290dda9dbfeSHari Bathini 	/* Merge adjacent reserved ranges */
1291dda9dbfeSHari Bathini 	idx = 0;
1292dda9dbfeSHari Bathini 	for (i = 1; i < mrange_info->mem_range_cnt; i++) {
1293dda9dbfeSHari Bathini 		base = mem_ranges[i-1].base;
1294dda9dbfeSHari Bathini 		size = mem_ranges[i-1].size;
1295dda9dbfeSHari Bathini 		if (mem_ranges[i].base == (base + size))
1296dda9dbfeSHari Bathini 			mem_ranges[idx].size += mem_ranges[i].size;
1297dda9dbfeSHari Bathini 		else {
1298dda9dbfeSHari Bathini 			idx++;
1299dda9dbfeSHari Bathini 			if (i == idx)
1300dda9dbfeSHari Bathini 				continue;
1301dda9dbfeSHari Bathini 
1302dda9dbfeSHari Bathini 			mem_ranges[idx] = mem_ranges[i];
1303dda9dbfeSHari Bathini 		}
1304dda9dbfeSHari Bathini 	}
1305dda9dbfeSHari Bathini 	mrange_info->mem_range_cnt = idx + 1;
1306dda9dbfeSHari Bathini }
1307dda9dbfeSHari Bathini 
1308dda9dbfeSHari Bathini /*
1309dda9dbfeSHari Bathini  * Scan reserved-ranges to consider them while reserving/releasing
1310dda9dbfeSHari Bathini  * memory for FADump.
1311dda9dbfeSHari Bathini  */
131202c04e37SHari Bathini static void __init early_init_dt_scan_reserved_ranges(unsigned long node)
1313dda9dbfeSHari Bathini {
1314dda9dbfeSHari Bathini 	const __be32 *prop;
1315dda9dbfeSHari Bathini 	int len, ret = -1;
1316dda9dbfeSHari Bathini 	unsigned long i;
1317dda9dbfeSHari Bathini 
131802c04e37SHari Bathini 	/* reserved-ranges already scanned */
131902c04e37SHari Bathini 	if (reserved_mrange_info.mem_range_cnt != 0)
132002c04e37SHari Bathini 		return;
1321dda9dbfeSHari Bathini 
132202c04e37SHari Bathini 	prop = of_get_flat_dt_prop(node, "reserved-ranges", &len);
1323dda9dbfeSHari Bathini 	if (!prop)
132402c04e37SHari Bathini 		return;
1325dda9dbfeSHari Bathini 
1326dda9dbfeSHari Bathini 	/*
1327dda9dbfeSHari Bathini 	 * Each reserved range is an (address,size) pair, 2 cells each,
1328dda9dbfeSHari Bathini 	 * totalling 4 cells per range.
1329dda9dbfeSHari Bathini 	 */
1330dda9dbfeSHari Bathini 	for (i = 0; i < len / (sizeof(*prop) * 4); i++) {
1331dda9dbfeSHari Bathini 		u64 base, size;
1332dda9dbfeSHari Bathini 
1333dda9dbfeSHari Bathini 		base = of_read_number(prop + (i * 4) + 0, 2);
1334dda9dbfeSHari Bathini 		size = of_read_number(prop + (i * 4) + 2, 2);
1335dda9dbfeSHari Bathini 
1336dda9dbfeSHari Bathini 		if (size) {
1337dda9dbfeSHari Bathini 			ret = fadump_add_mem_range(&reserved_mrange_info,
1338dda9dbfeSHari Bathini 						   base, base + size);
1339dda9dbfeSHari Bathini 			if (ret < 0) {
1340dda9dbfeSHari Bathini 				pr_warn("some reserved ranges are ignored!\n");
1341dda9dbfeSHari Bathini 				break;
1342dda9dbfeSHari Bathini 			}
1343dda9dbfeSHari Bathini 		}
1344dda9dbfeSHari Bathini 	}
1345dda9dbfeSHari Bathini 
134602c04e37SHari Bathini 	/* Compact reserved ranges */
134702c04e37SHari Bathini 	sort_and_merge_mem_ranges(&reserved_mrange_info);
1348dda9dbfeSHari Bathini }
1349dda9dbfeSHari Bathini 
1350dda9dbfeSHari Bathini /*
1351dda9dbfeSHari Bathini  * Release the memory that was reserved during early boot to preserve the
1352dda9dbfeSHari Bathini  * crash'ed kernel's memory contents except reserved dump area (permanent
1353dda9dbfeSHari Bathini  * reservation) and reserved ranges used by F/W. The released memory will
1354dda9dbfeSHari Bathini  * be available for general use.
1355dda9dbfeSHari Bathini  */
1356dda9dbfeSHari Bathini static void fadump_release_memory(u64 begin, u64 end)
1357dda9dbfeSHari Bathini {
1358dda9dbfeSHari Bathini 	u64 ra_start, ra_end, tstart;
1359dda9dbfeSHari Bathini 	int i, ret;
1360dda9dbfeSHari Bathini 
1361b500afffSMahesh Salgaonkar 	ra_start = fw_dump.reserve_dump_area_start;
1362b500afffSMahesh Salgaonkar 	ra_end = ra_start + fw_dump.reserve_dump_area_size;
1363b500afffSMahesh Salgaonkar 
1364b500afffSMahesh Salgaonkar 	/*
136502c04e37SHari Bathini 	 * If reserved ranges array limit is hit, overwrite the last reserved
136602c04e37SHari Bathini 	 * memory range with reserved dump area to ensure it is excluded from
136702c04e37SHari Bathini 	 * the memory being released (reused for next FADump registration).
1368dda9dbfeSHari Bathini 	 */
136902c04e37SHari Bathini 	if (reserved_mrange_info.mem_range_cnt ==
137002c04e37SHari Bathini 	    reserved_mrange_info.max_mem_ranges)
137102c04e37SHari Bathini 		reserved_mrange_info.mem_range_cnt--;
1372dda9dbfeSHari Bathini 
137302c04e37SHari Bathini 	ret = fadump_add_mem_range(&reserved_mrange_info, ra_start, ra_end);
137402c04e37SHari Bathini 	if (ret != 0)
1375dda9dbfeSHari Bathini 		return;
1376dda9dbfeSHari Bathini 
1377dda9dbfeSHari Bathini 	/* Get the reserved ranges list in order first. */
1378dda9dbfeSHari Bathini 	sort_and_merge_mem_ranges(&reserved_mrange_info);
1379dda9dbfeSHari Bathini 
1380dda9dbfeSHari Bathini 	/* Exclude reserved ranges and release remaining memory */
1381dda9dbfeSHari Bathini 	tstart = begin;
1382dda9dbfeSHari Bathini 	for (i = 0; i < reserved_mrange_info.mem_range_cnt; i++) {
1383dda9dbfeSHari Bathini 		ra_start = reserved_mrange_info.mem_ranges[i].base;
1384dda9dbfeSHari Bathini 		ra_end = ra_start + reserved_mrange_info.mem_ranges[i].size;
1385dda9dbfeSHari Bathini 
1386dda9dbfeSHari Bathini 		if (tstart >= ra_end)
1387dda9dbfeSHari Bathini 			continue;
1388dda9dbfeSHari Bathini 
1389dda9dbfeSHari Bathini 		if (tstart < ra_start)
1390dda9dbfeSHari Bathini 			fadump_release_reserved_area(tstart, ra_start);
1391dda9dbfeSHari Bathini 		tstart = ra_end;
1392dda9dbfeSHari Bathini 	}
1393dda9dbfeSHari Bathini 
1394dda9dbfeSHari Bathini 	if (tstart < end)
1395dda9dbfeSHari Bathini 		fadump_release_reserved_area(tstart, end);
1396b500afffSMahesh Salgaonkar }
1397b500afffSMahesh Salgaonkar 
1398b500afffSMahesh Salgaonkar static void fadump_invalidate_release_mem(void)
1399b500afffSMahesh Salgaonkar {
1400b500afffSMahesh Salgaonkar 	mutex_lock(&fadump_mutex);
1401b500afffSMahesh Salgaonkar 	if (!fw_dump.dump_active) {
1402b500afffSMahesh Salgaonkar 		mutex_unlock(&fadump_mutex);
1403b500afffSMahesh Salgaonkar 		return;
1404b500afffSMahesh Salgaonkar 	}
1405b500afffSMahesh Salgaonkar 
1406b500afffSMahesh Salgaonkar 	fadump_cleanup();
1407b500afffSMahesh Salgaonkar 	mutex_unlock(&fadump_mutex);
1408b500afffSMahesh Salgaonkar 
14097dee93a9SHari Bathini 	fadump_release_memory(fw_dump.boot_mem_top, memblock_end_of_DRAM());
1410961cf26aSHari Bathini 	fadump_free_cpu_notes_buf();
1411961cf26aSHari Bathini 
1412a4e2e2caSHari Bathini 	/*
1413a4e2e2caSHari Bathini 	 * Setup kernel metadata and initialize the kernel dump
1414a4e2e2caSHari Bathini 	 * memory structure for FADump re-registration.
1415a4e2e2caSHari Bathini 	 */
1416a4e2e2caSHari Bathini 	if (fw_dump.ops->fadump_setup_metadata &&
1417a4e2e2caSHari Bathini 	    (fw_dump.ops->fadump_setup_metadata(&fw_dump) < 0))
1418a4e2e2caSHari Bathini 		pr_warn("Failed to setup kernel metadata!\n");
141941a65d16SHari Bathini 	fw_dump.ops->fadump_init_mem_struct(&fw_dump);
1420b500afffSMahesh Salgaonkar }
1421b500afffSMahesh Salgaonkar 
1422d418b19fSSourabh Jain static ssize_t release_mem_store(struct kobject *kobj,
1423b500afffSMahesh Salgaonkar 				 struct kobj_attribute *attr,
1424b500afffSMahesh Salgaonkar 				 const char *buf, size_t count)
1425b500afffSMahesh Salgaonkar {
1426dcdc4679SMichal Suchanek 	int input = -1;
1427dcdc4679SMichal Suchanek 
1428b500afffSMahesh Salgaonkar 	if (!fw_dump.dump_active)
1429b500afffSMahesh Salgaonkar 		return -EPERM;
1430b500afffSMahesh Salgaonkar 
1431dcdc4679SMichal Suchanek 	if (kstrtoint(buf, 0, &input))
1432dcdc4679SMichal Suchanek 		return -EINVAL;
1433dcdc4679SMichal Suchanek 
1434dcdc4679SMichal Suchanek 	if (input == 1) {
1435b500afffSMahesh Salgaonkar 		/*
1436b500afffSMahesh Salgaonkar 		 * Take away the '/proc/vmcore'. We are releasing the dump
1437b500afffSMahesh Salgaonkar 		 * memory, hence it will not be valid anymore.
1438b500afffSMahesh Salgaonkar 		 */
14392685f826SMichael Ellerman #ifdef CONFIG_PROC_VMCORE
1440b500afffSMahesh Salgaonkar 		vmcore_cleanup();
14412685f826SMichael Ellerman #endif
1442b500afffSMahesh Salgaonkar 		fadump_invalidate_release_mem();
1443b500afffSMahesh Salgaonkar 
1444b500afffSMahesh Salgaonkar 	} else
1445b500afffSMahesh Salgaonkar 		return -EINVAL;
1446b500afffSMahesh Salgaonkar 	return count;
1447b500afffSMahesh Salgaonkar }
1448b500afffSMahesh Salgaonkar 
1449d418b19fSSourabh Jain /* Release the reserved memory and disable the FADump */
1450d418b19fSSourabh Jain static void unregister_fadump(void)
1451d418b19fSSourabh Jain {
1452d418b19fSSourabh Jain 	fadump_cleanup();
1453d418b19fSSourabh Jain 	fadump_release_memory(fw_dump.reserve_dump_area_start,
1454d418b19fSSourabh Jain 			      fw_dump.reserve_dump_area_size);
1455d418b19fSSourabh Jain 	fw_dump.fadump_enabled = 0;
1456d418b19fSSourabh Jain 	kobject_put(fadump_kobj);
1457d418b19fSSourabh Jain }
1458d418b19fSSourabh Jain 
1459d418b19fSSourabh Jain static ssize_t enabled_show(struct kobject *kobj,
14603ccc00a7SMahesh Salgaonkar 			    struct kobj_attribute *attr,
14613ccc00a7SMahesh Salgaonkar 			    char *buf)
14623ccc00a7SMahesh Salgaonkar {
14633ccc00a7SMahesh Salgaonkar 	return sprintf(buf, "%d\n", fw_dump.fadump_enabled);
14643ccc00a7SMahesh Salgaonkar }
14653ccc00a7SMahesh Salgaonkar 
1466d8e73458SSourabh Jain static ssize_t mem_reserved_show(struct kobject *kobj,
1467d8e73458SSourabh Jain 				 struct kobj_attribute *attr,
1468d8e73458SSourabh Jain 				 char *buf)
1469d8e73458SSourabh Jain {
1470d8e73458SSourabh Jain 	return sprintf(buf, "%ld\n", fw_dump.reserve_dump_area_size);
1471d8e73458SSourabh Jain }
1472d8e73458SSourabh Jain 
1473d418b19fSSourabh Jain static ssize_t registered_show(struct kobject *kobj,
14743ccc00a7SMahesh Salgaonkar 			       struct kobj_attribute *attr,
14753ccc00a7SMahesh Salgaonkar 			       char *buf)
14763ccc00a7SMahesh Salgaonkar {
14773ccc00a7SMahesh Salgaonkar 	return sprintf(buf, "%d\n", fw_dump.dump_registered);
14783ccc00a7SMahesh Salgaonkar }
14793ccc00a7SMahesh Salgaonkar 
1480d418b19fSSourabh Jain static ssize_t registered_store(struct kobject *kobj,
14813ccc00a7SMahesh Salgaonkar 				struct kobj_attribute *attr,
14823ccc00a7SMahesh Salgaonkar 				const char *buf, size_t count)
14833ccc00a7SMahesh Salgaonkar {
14843ccc00a7SMahesh Salgaonkar 	int ret = 0;
1485dcdc4679SMichal Suchanek 	int input = -1;
14863ccc00a7SMahesh Salgaonkar 
1487f3512011SHari Bathini 	if (!fw_dump.fadump_enabled || fw_dump.dump_active)
14883ccc00a7SMahesh Salgaonkar 		return -EPERM;
14893ccc00a7SMahesh Salgaonkar 
1490dcdc4679SMichal Suchanek 	if (kstrtoint(buf, 0, &input))
1491dcdc4679SMichal Suchanek 		return -EINVAL;
1492dcdc4679SMichal Suchanek 
14933ccc00a7SMahesh Salgaonkar 	mutex_lock(&fadump_mutex);
14943ccc00a7SMahesh Salgaonkar 
1495dcdc4679SMichal Suchanek 	switch (input) {
1496dcdc4679SMichal Suchanek 	case 0:
14973ccc00a7SMahesh Salgaonkar 		if (fw_dump.dump_registered == 0) {
14983ccc00a7SMahesh Salgaonkar 			goto unlock_out;
14993ccc00a7SMahesh Salgaonkar 		}
1500f3512011SHari Bathini 
15013ccc00a7SMahesh Salgaonkar 		/* Un-register Firmware-assisted dump */
150241a65d16SHari Bathini 		pr_debug("Un-register firmware-assisted dump\n");
150341a65d16SHari Bathini 		fw_dump.ops->fadump_unregister(&fw_dump);
15043ccc00a7SMahesh Salgaonkar 		break;
1505dcdc4679SMichal Suchanek 	case 1:
15063ccc00a7SMahesh Salgaonkar 		if (fw_dump.dump_registered == 1) {
15070823c68bSHari Bathini 			/* Un-register Firmware-assisted dump */
150841a65d16SHari Bathini 			fw_dump.ops->fadump_unregister(&fw_dump);
15093ccc00a7SMahesh Salgaonkar 		}
15103ccc00a7SMahesh Salgaonkar 		/* Register Firmware-assisted dump */
151198b8cd7fSMichal Suchanek 		ret = register_fadump();
15123ccc00a7SMahesh Salgaonkar 		break;
15133ccc00a7SMahesh Salgaonkar 	default:
15143ccc00a7SMahesh Salgaonkar 		ret = -EINVAL;
15153ccc00a7SMahesh Salgaonkar 		break;
15163ccc00a7SMahesh Salgaonkar 	}
15173ccc00a7SMahesh Salgaonkar 
15183ccc00a7SMahesh Salgaonkar unlock_out:
15193ccc00a7SMahesh Salgaonkar 	mutex_unlock(&fadump_mutex);
15203ccc00a7SMahesh Salgaonkar 	return ret < 0 ? ret : count;
15213ccc00a7SMahesh Salgaonkar }
15223ccc00a7SMahesh Salgaonkar 
15233ccc00a7SMahesh Salgaonkar static int fadump_region_show(struct seq_file *m, void *private)
15243ccc00a7SMahesh Salgaonkar {
15253ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_enabled)
15263ccc00a7SMahesh Salgaonkar 		return 0;
15273ccc00a7SMahesh Salgaonkar 
1528b500afffSMahesh Salgaonkar 	mutex_lock(&fadump_mutex);
152941a65d16SHari Bathini 	fw_dump.ops->fadump_region_show(&fw_dump, m);
1530b500afffSMahesh Salgaonkar 	mutex_unlock(&fadump_mutex);
15313ccc00a7SMahesh Salgaonkar 	return 0;
15323ccc00a7SMahesh Salgaonkar }
15333ccc00a7SMahesh Salgaonkar 
1534d418b19fSSourabh Jain static struct kobj_attribute release_attr = __ATTR_WO(release_mem);
1535d418b19fSSourabh Jain static struct kobj_attribute enable_attr = __ATTR_RO(enabled);
1536d418b19fSSourabh Jain static struct kobj_attribute register_attr = __ATTR_RW(registered);
1537d8e73458SSourabh Jain static struct kobj_attribute mem_reserved_attr = __ATTR_RO(mem_reserved);
1538d418b19fSSourabh Jain 
1539d418b19fSSourabh Jain static struct attribute *fadump_attrs[] = {
1540d418b19fSSourabh Jain 	&enable_attr.attr,
1541d418b19fSSourabh Jain 	&register_attr.attr,
1542d8e73458SSourabh Jain 	&mem_reserved_attr.attr,
1543d418b19fSSourabh Jain 	NULL,
1544d418b19fSSourabh Jain };
1545d418b19fSSourabh Jain 
1546d418b19fSSourabh Jain ATTRIBUTE_GROUPS(fadump);
15473ccc00a7SMahesh Salgaonkar 
1548f6cee260SYangtao Li DEFINE_SHOW_ATTRIBUTE(fadump_region);
15493ccc00a7SMahesh Salgaonkar 
15503ccc00a7SMahesh Salgaonkar static void fadump_init_files(void)
15513ccc00a7SMahesh Salgaonkar {
15523ccc00a7SMahesh Salgaonkar 	int rc = 0;
15533ccc00a7SMahesh Salgaonkar 
1554d418b19fSSourabh Jain 	fadump_kobj = kobject_create_and_add("fadump", kernel_kobj);
1555d418b19fSSourabh Jain 	if (!fadump_kobj) {
1556d418b19fSSourabh Jain 		pr_err("failed to create fadump kobject\n");
1557d418b19fSSourabh Jain 		return;
1558d418b19fSSourabh Jain 	}
1559860286cfSGreg Kroah-Hartman 
1560860286cfSGreg Kroah-Hartman 	debugfs_create_file("fadump_region", 0444, powerpc_debugfs_root, NULL,
15613ccc00a7SMahesh Salgaonkar 			    &fadump_region_fops);
1562b500afffSMahesh Salgaonkar 
1563b500afffSMahesh Salgaonkar 	if (fw_dump.dump_active) {
1564d418b19fSSourabh Jain 		rc = sysfs_create_file(fadump_kobj, &release_attr.attr);
1565b500afffSMahesh Salgaonkar 		if (rc)
1566d418b19fSSourabh Jain 			pr_err("unable to create release_mem sysfs file (%d)\n",
1567d418b19fSSourabh Jain 			       rc);
1568d418b19fSSourabh Jain 	}
1569d418b19fSSourabh Jain 
1570d418b19fSSourabh Jain 	rc = sysfs_create_groups(fadump_kobj, fadump_groups);
1571d418b19fSSourabh Jain 	if (rc) {
1572d418b19fSSourabh Jain 		pr_err("sysfs group creation failed (%d), unregistering FADump",
1573d418b19fSSourabh Jain 		       rc);
1574d418b19fSSourabh Jain 		unregister_fadump();
1575d418b19fSSourabh Jain 		return;
1576d418b19fSSourabh Jain 	}
1577d418b19fSSourabh Jain 
1578d418b19fSSourabh Jain 	/*
1579d418b19fSSourabh Jain 	 * The FADump sysfs are moved from kernel_kobj to fadump_kobj need to
1580d418b19fSSourabh Jain 	 * create symlink at old location to maintain backward compatibility.
1581d418b19fSSourabh Jain 	 *
1582d418b19fSSourabh Jain 	 *      - fadump_enabled -> fadump/enabled
1583d418b19fSSourabh Jain 	 *      - fadump_registered -> fadump/registered
1584d418b19fSSourabh Jain 	 *      - fadump_release_mem -> fadump/release_mem
1585d418b19fSSourabh Jain 	 */
1586d418b19fSSourabh Jain 	rc = compat_only_sysfs_link_entry_to_kobj(kernel_kobj, fadump_kobj,
1587d418b19fSSourabh Jain 						  "enabled", "fadump_enabled");
1588d418b19fSSourabh Jain 	if (rc) {
1589d418b19fSSourabh Jain 		pr_err("unable to create fadump_enabled symlink (%d)", rc);
1590d418b19fSSourabh Jain 		return;
1591d418b19fSSourabh Jain 	}
1592d418b19fSSourabh Jain 
1593d418b19fSSourabh Jain 	rc = compat_only_sysfs_link_entry_to_kobj(kernel_kobj, fadump_kobj,
1594d418b19fSSourabh Jain 						  "registered",
1595d418b19fSSourabh Jain 						  "fadump_registered");
1596d418b19fSSourabh Jain 	if (rc) {
1597d418b19fSSourabh Jain 		pr_err("unable to create fadump_registered symlink (%d)", rc);
1598d418b19fSSourabh Jain 		sysfs_remove_link(kernel_kobj, "fadump_enabled");
1599d418b19fSSourabh Jain 		return;
1600d418b19fSSourabh Jain 	}
1601d418b19fSSourabh Jain 
1602d418b19fSSourabh Jain 	if (fw_dump.dump_active) {
1603d418b19fSSourabh Jain 		rc = compat_only_sysfs_link_entry_to_kobj(kernel_kobj,
1604d418b19fSSourabh Jain 							  fadump_kobj,
1605d418b19fSSourabh Jain 							  "release_mem",
1606d418b19fSSourabh Jain 							  "fadump_release_mem");
1607d418b19fSSourabh Jain 		if (rc)
1608d418b19fSSourabh Jain 			pr_err("unable to create fadump_release_mem symlink (%d)",
1609d418b19fSSourabh Jain 			       rc);
1610b500afffSMahesh Salgaonkar 	}
16113ccc00a7SMahesh Salgaonkar 	return;
16123ccc00a7SMahesh Salgaonkar }
16133ccc00a7SMahesh Salgaonkar 
16143ccc00a7SMahesh Salgaonkar /*
16153ccc00a7SMahesh Salgaonkar  * Prepare for firmware-assisted dump.
16163ccc00a7SMahesh Salgaonkar  */
16173ccc00a7SMahesh Salgaonkar int __init setup_fadump(void)
16183ccc00a7SMahesh Salgaonkar {
1619565f9bc0SMichal Suchanek 	if (!fw_dump.fadump_supported)
16203ccc00a7SMahesh Salgaonkar 		return 0;
16213ccc00a7SMahesh Salgaonkar 
1622565f9bc0SMichal Suchanek 	fadump_init_files();
16233ccc00a7SMahesh Salgaonkar 	fadump_show_config();
1624565f9bc0SMichal Suchanek 
1625565f9bc0SMichal Suchanek 	if (!fw_dump.fadump_enabled)
1626565f9bc0SMichal Suchanek 		return 1;
1627565f9bc0SMichal Suchanek 
16282df173d9SMahesh Salgaonkar 	/*
16292df173d9SMahesh Salgaonkar 	 * If dump data is available then see if it is valid and prepare for
16302df173d9SMahesh Salgaonkar 	 * saving it to the disk.
16312df173d9SMahesh Salgaonkar 	 */
1632b500afffSMahesh Salgaonkar 	if (fw_dump.dump_active) {
1633b500afffSMahesh Salgaonkar 		/*
1634b500afffSMahesh Salgaonkar 		 * if dump process fails then invalidate the registration
1635b500afffSMahesh Salgaonkar 		 * and release memory before proceeding for re-registration.
1636b500afffSMahesh Salgaonkar 		 */
1637f3512011SHari Bathini 		if (fw_dump.ops->fadump_process(&fw_dump) < 0)
1638b500afffSMahesh Salgaonkar 			fadump_invalidate_release_mem();
1639b500afffSMahesh Salgaonkar 	}
16403ccc00a7SMahesh Salgaonkar 	/* Initialize the kernel dump memory structure for FAD registration. */
16412df173d9SMahesh Salgaonkar 	else if (fw_dump.reserve_dump_area_size)
164241a65d16SHari Bathini 		fw_dump.ops->fadump_init_mem_struct(&fw_dump);
1643f3512011SHari Bathini 
16443ccc00a7SMahesh Salgaonkar 	return 1;
16453ccc00a7SMahesh Salgaonkar }
16463ccc00a7SMahesh Salgaonkar subsys_initcall(setup_fadump);
1647bec53196SHari Bathini #else /* !CONFIG_PRESERVE_FA_DUMP */
1648bec53196SHari Bathini 
1649bec53196SHari Bathini /* Scan the Firmware Assisted dump configuration details. */
1650bec53196SHari Bathini int __init early_init_dt_scan_fw_dump(unsigned long node, const char *uname,
1651bec53196SHari Bathini 				      int depth, void *data)
1652bec53196SHari Bathini {
1653bec53196SHari Bathini 	if ((depth != 1) || (strcmp(uname, "ibm,opal") != 0))
1654bec53196SHari Bathini 		return 0;
1655bec53196SHari Bathini 
1656bec53196SHari Bathini 	opal_fadump_dt_scan(&fw_dump, node);
1657bec53196SHari Bathini 	return 1;
1658bec53196SHari Bathini }
1659bec53196SHari Bathini 
1660bec53196SHari Bathini /*
1661bec53196SHari Bathini  * When dump is active but PRESERVE_FA_DUMP is enabled on the kernel,
1662bec53196SHari Bathini  * preserve crash data. The subsequent memory preserving kernel boot
1663bec53196SHari Bathini  * is likely to process this crash data.
1664bec53196SHari Bathini  */
1665bec53196SHari Bathini int __init fadump_reserve_mem(void)
1666bec53196SHari Bathini {
1667bec53196SHari Bathini 	if (fw_dump.dump_active) {
1668bec53196SHari Bathini 		/*
1669bec53196SHari Bathini 		 * If last boot has crashed then reserve all the memory
1670bec53196SHari Bathini 		 * above boot memory to preserve crash data.
1671bec53196SHari Bathini 		 */
1672bec53196SHari Bathini 		pr_info("Preserving crash data for processing in next boot.\n");
1673bec53196SHari Bathini 		fadump_reserve_crash_area(fw_dump.boot_mem_top);
1674bec53196SHari Bathini 	} else
1675bec53196SHari Bathini 		pr_debug("FADump-aware kernel..\n");
1676bec53196SHari Bathini 
1677bec53196SHari Bathini 	return 1;
1678bec53196SHari Bathini }
1679bec53196SHari Bathini #endif /* CONFIG_PRESERVE_FA_DUMP */
1680b2a815a5SHari Bathini 
1681b2a815a5SHari Bathini /* Preserve everything above the base address */
1682b2a815a5SHari Bathini static void __init fadump_reserve_crash_area(u64 base)
1683b2a815a5SHari Bathini {
1684b10d6bcaSMike Rapoport 	u64 i, mstart, mend, msize;
1685b2a815a5SHari Bathini 
1686b10d6bcaSMike Rapoport 	for_each_mem_range(i, &mstart, &mend) {
1687b10d6bcaSMike Rapoport 		msize  = mend - mstart;
1688b2a815a5SHari Bathini 
1689b2a815a5SHari Bathini 		if ((mstart + msize) < base)
1690b2a815a5SHari Bathini 			continue;
1691b2a815a5SHari Bathini 
1692b2a815a5SHari Bathini 		if (mstart < base) {
1693b2a815a5SHari Bathini 			msize -= (base - mstart);
1694b2a815a5SHari Bathini 			mstart = base;
1695b2a815a5SHari Bathini 		}
1696b2a815a5SHari Bathini 
1697b2a815a5SHari Bathini 		pr_info("Reserving %lluMB of memory at %#016llx for preserving crash data",
1698b2a815a5SHari Bathini 			(msize >> 20), mstart);
1699b2a815a5SHari Bathini 		memblock_reserve(mstart, msize);
1700b2a815a5SHari Bathini 	}
1701b2a815a5SHari Bathini }
1702bec53196SHari Bathini 
1703bec53196SHari Bathini unsigned long __init arch_reserved_kernel_pages(void)
1704bec53196SHari Bathini {
1705bec53196SHari Bathini 	return memblock_reserved_size() / PAGE_SIZE;
1706bec53196SHari Bathini }
1707