xref: /openbmc/linux/arch/powerpc/kernel/fadump.c (revision 02c04e374e176ae3a3f64a682f80702f8d2fb65d)
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 
35eb39c880SMahesh Salgaonkar static struct fw_dump fw_dump;
363ccc00a7SMahesh Salgaonkar 
37b2a815a5SHari Bathini static void __init fadump_reserve_crash_area(u64 base);
38b2a815a5SHari Bathini 
39d418b19fSSourabh Jain struct kobject *fadump_kobj;
40d418b19fSSourabh Jain 
41bec53196SHari Bathini #ifndef CONFIG_PRESERVE_FA_DUMP
423ccc00a7SMahesh Salgaonkar static DEFINE_MUTEX(fadump_mutex);
43*02c04e37SHari Bathini struct fadump_mrange_info crash_mrange_info = { "crash", NULL, 0, 0, 0, false };
44*02c04e37SHari Bathini 
45*02c04e37SHari Bathini #define RESERVED_RNGS_SZ	16384 /* 16K - 128 entries */
46*02c04e37SHari Bathini #define RESERVED_RNGS_CNT	(RESERVED_RNGS_SZ / \
47*02c04e37SHari Bathini 				 sizeof(struct fadump_memory_range))
48*02c04e37SHari Bathini static struct fadump_memory_range rngs[RESERVED_RNGS_CNT];
49*02c04e37SHari Bathini struct fadump_mrange_info reserved_mrange_info = { "reserved", rngs,
50*02c04e37SHari Bathini 						   RESERVED_RNGS_SZ, 0,
51*02c04e37SHari Bathini 						   RESERVED_RNGS_CNT, true };
52*02c04e37SHari Bathini 
53*02c04e37SHari Bathini static void __init early_init_dt_scan_reserved_ranges(unsigned long node);
54eb39c880SMahesh Salgaonkar 
55a4e92ce8SMahesh Salgaonkar #ifdef CONFIG_CMA
560226e552SHari Bathini static struct cma *fadump_cma;
570226e552SHari Bathini 
58a4e92ce8SMahesh Salgaonkar /*
59a4e92ce8SMahesh Salgaonkar  * fadump_cma_init() - Initialize CMA area from a fadump reserved memory
60a4e92ce8SMahesh Salgaonkar  *
61a4e92ce8SMahesh Salgaonkar  * This function initializes CMA area from fadump reserved memory.
62a4e92ce8SMahesh Salgaonkar  * The total size of fadump reserved memory covers for boot memory size
63a4e92ce8SMahesh Salgaonkar  * + cpu data size + hpte size and metadata.
64a4e92ce8SMahesh Salgaonkar  * Initialize only the area equivalent to boot memory size for CMA use.
65a4e92ce8SMahesh Salgaonkar  * The reamining portion of fadump reserved memory will be not given
66a4e92ce8SMahesh Salgaonkar  * to CMA and pages for thoes will stay reserved. boot memory size is
67a4e92ce8SMahesh Salgaonkar  * aligned per CMA requirement to satisy cma_init_reserved_mem() call.
68a4e92ce8SMahesh Salgaonkar  * But for some reason even if it fails we still have the memory reservation
69a4e92ce8SMahesh Salgaonkar  * with us and we can still continue doing fadump.
70a4e92ce8SMahesh Salgaonkar  */
71a4e92ce8SMahesh Salgaonkar int __init fadump_cma_init(void)
72a4e92ce8SMahesh Salgaonkar {
73a4e92ce8SMahesh Salgaonkar 	unsigned long long base, size;
74a4e92ce8SMahesh Salgaonkar 	int rc;
75a4e92ce8SMahesh Salgaonkar 
76a4e92ce8SMahesh Salgaonkar 	if (!fw_dump.fadump_enabled)
77a4e92ce8SMahesh Salgaonkar 		return 0;
78a4e92ce8SMahesh Salgaonkar 
79a4e92ce8SMahesh Salgaonkar 	/*
80a4e92ce8SMahesh Salgaonkar 	 * Do not use CMA if user has provided fadump=nocma kernel parameter.
81a4e92ce8SMahesh Salgaonkar 	 * Return 1 to continue with fadump old behaviour.
82a4e92ce8SMahesh Salgaonkar 	 */
83a4e92ce8SMahesh Salgaonkar 	if (fw_dump.nocma)
84a4e92ce8SMahesh Salgaonkar 		return 1;
85a4e92ce8SMahesh Salgaonkar 
86a4e92ce8SMahesh Salgaonkar 	base = fw_dump.reserve_dump_area_start;
87a4e92ce8SMahesh Salgaonkar 	size = fw_dump.boot_memory_size;
88a4e92ce8SMahesh Salgaonkar 
89a4e92ce8SMahesh Salgaonkar 	if (!size)
90a4e92ce8SMahesh Salgaonkar 		return 0;
91a4e92ce8SMahesh Salgaonkar 
92a4e92ce8SMahesh Salgaonkar 	rc = cma_init_reserved_mem(base, size, 0, "fadump_cma", &fadump_cma);
93a4e92ce8SMahesh Salgaonkar 	if (rc) {
94a4e92ce8SMahesh Salgaonkar 		pr_err("Failed to init cma area for firmware-assisted dump,%d\n", rc);
95a4e92ce8SMahesh Salgaonkar 		/*
96a4e92ce8SMahesh Salgaonkar 		 * Though the CMA init has failed we still have memory
97a4e92ce8SMahesh Salgaonkar 		 * reservation with us. The reserved memory will be
98a4e92ce8SMahesh Salgaonkar 		 * blocked from production system usage.  Hence return 1,
99a4e92ce8SMahesh Salgaonkar 		 * so that we can continue with fadump.
100a4e92ce8SMahesh Salgaonkar 		 */
101a4e92ce8SMahesh Salgaonkar 		return 1;
102a4e92ce8SMahesh Salgaonkar 	}
103a4e92ce8SMahesh Salgaonkar 
104a4e92ce8SMahesh Salgaonkar 	/*
105a4e92ce8SMahesh Salgaonkar 	 * So we now have successfully initialized cma area for fadump.
106a4e92ce8SMahesh Salgaonkar 	 */
107a4e92ce8SMahesh Salgaonkar 	pr_info("Initialized 0x%lx bytes cma area at %ldMB from 0x%lx "
108a4e92ce8SMahesh Salgaonkar 		"bytes of memory reserved for firmware-assisted dump\n",
109a4e92ce8SMahesh Salgaonkar 		cma_get_size(fadump_cma),
110a4e92ce8SMahesh Salgaonkar 		(unsigned long)cma_get_base(fadump_cma) >> 20,
111a4e92ce8SMahesh Salgaonkar 		fw_dump.reserve_dump_area_size);
112a4e92ce8SMahesh Salgaonkar 	return 1;
113a4e92ce8SMahesh Salgaonkar }
114a4e92ce8SMahesh Salgaonkar #else
115a4e92ce8SMahesh Salgaonkar static int __init fadump_cma_init(void) { return 1; }
116a4e92ce8SMahesh Salgaonkar #endif /* CONFIG_CMA */
117a4e92ce8SMahesh Salgaonkar 
118eb39c880SMahesh Salgaonkar /* Scan the Firmware Assisted dump configuration details. */
119f3512011SHari Bathini int __init early_init_dt_scan_fw_dump(unsigned long node, const char *uname,
120f3512011SHari Bathini 				      int depth, void *data)
121eb39c880SMahesh Salgaonkar {
122*02c04e37SHari Bathini 	if (depth == 0) {
123*02c04e37SHari Bathini 		early_init_dt_scan_reserved_ranges(node);
124*02c04e37SHari Bathini 		return 0;
125*02c04e37SHari Bathini 	}
126*02c04e37SHari Bathini 
12741df5928SHari Bathini 	if (depth != 1)
128eb39c880SMahesh Salgaonkar 		return 0;
129eb39c880SMahesh Salgaonkar 
13041df5928SHari Bathini 	if (strcmp(uname, "rtas") == 0) {
131d3833a70SHari Bathini 		rtas_fadump_dt_scan(&fw_dump, node);
132eb39c880SMahesh Salgaonkar 		return 1;
133eb39c880SMahesh Salgaonkar 	}
134eb39c880SMahesh Salgaonkar 
13541df5928SHari Bathini 	if (strcmp(uname, "ibm,opal") == 0) {
13641df5928SHari Bathini 		opal_fadump_dt_scan(&fw_dump, node);
13741df5928SHari Bathini 		return 1;
13841df5928SHari Bathini 	}
13941df5928SHari Bathini 
14041df5928SHari Bathini 	return 0;
14141df5928SHari Bathini }
14241df5928SHari Bathini 
143eae0dfccSHari Bathini /*
144eae0dfccSHari Bathini  * If fadump is registered, check if the memory provided
1450db6896fSMahesh Salgaonkar  * falls within boot memory area and reserved memory area.
146eae0dfccSHari Bathini  */
147becd91d9SHari Bathini int is_fadump_memory_area(u64 addr, unsigned long size)
148eae0dfccSHari Bathini {
149becd91d9SHari Bathini 	u64 d_start, d_end;
1500db6896fSMahesh Salgaonkar 
151eae0dfccSHari Bathini 	if (!fw_dump.dump_registered)
152eae0dfccSHari Bathini 		return 0;
153eae0dfccSHari Bathini 
154becd91d9SHari Bathini 	if (!size)
155becd91d9SHari Bathini 		return 0;
156becd91d9SHari Bathini 
157becd91d9SHari Bathini 	d_start = fw_dump.reserve_dump_area_start;
158becd91d9SHari Bathini 	d_end = d_start + fw_dump.reserve_dump_area_size;
1590db6896fSMahesh Salgaonkar 	if (((addr + size) > d_start) && (addr <= d_end))
1600db6896fSMahesh Salgaonkar 		return 1;
1610db6896fSMahesh Salgaonkar 
1627dee93a9SHari Bathini 	return (addr <= fw_dump.boot_mem_top);
163eae0dfccSHari Bathini }
164eae0dfccSHari Bathini 
1656fcd6baaSNicholas Piggin int should_fadump_crash(void)
1666fcd6baaSNicholas Piggin {
1676fcd6baaSNicholas Piggin 	if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr)
1686fcd6baaSNicholas Piggin 		return 0;
1696fcd6baaSNicholas Piggin 	return 1;
1706fcd6baaSNicholas Piggin }
1716fcd6baaSNicholas Piggin 
1723ccc00a7SMahesh Salgaonkar int is_fadump_active(void)
1733ccc00a7SMahesh Salgaonkar {
1743ccc00a7SMahesh Salgaonkar 	return fw_dump.dump_active;
1753ccc00a7SMahesh Salgaonkar }
1763ccc00a7SMahesh Salgaonkar 
177a5a05b91SHari Bathini /*
178961cf26aSHari Bathini  * Returns true, if there are no holes in memory area between d_start to d_end,
179961cf26aSHari Bathini  * false otherwise.
180a5a05b91SHari Bathini  */
181961cf26aSHari Bathini static bool is_fadump_mem_area_contiguous(u64 d_start, u64 d_end)
182a5a05b91SHari Bathini {
183a5a05b91SHari Bathini 	struct memblock_region *reg;
184961cf26aSHari Bathini 	bool ret = false;
185961cf26aSHari Bathini 	u64 start, end;
186a5a05b91SHari Bathini 
187a5a05b91SHari Bathini 	for_each_memblock(memory, reg) {
188961cf26aSHari Bathini 		start = max_t(u64, d_start, reg->base);
189961cf26aSHari Bathini 		end = min_t(u64, d_end, (reg->base + reg->size));
190961cf26aSHari Bathini 		if (d_start < end) {
191961cf26aSHari Bathini 			/* Memory hole from d_start to start */
192961cf26aSHari Bathini 			if (start > d_start)
193a5a05b91SHari Bathini 				break;
194a5a05b91SHari Bathini 
195961cf26aSHari Bathini 			if (end == d_end) {
196961cf26aSHari Bathini 				ret = true;
197a5a05b91SHari Bathini 				break;
198a5a05b91SHari Bathini 			}
199a5a05b91SHari Bathini 
200961cf26aSHari Bathini 			d_start = end + 1;
201a5a05b91SHari Bathini 		}
202a5a05b91SHari Bathini 	}
203a5a05b91SHari Bathini 
204a5a05b91SHari Bathini 	return ret;
205a5a05b91SHari Bathini }
206a5a05b91SHari Bathini 
207f86593beSMahesh Salgaonkar /*
208961cf26aSHari Bathini  * Returns true, if there are no holes in boot memory area,
209961cf26aSHari Bathini  * false otherwise.
210961cf26aSHari Bathini  */
2117f0ad11dSHari Bathini bool is_fadump_boot_mem_contiguous(void)
212961cf26aSHari Bathini {
2137dee93a9SHari Bathini 	unsigned long d_start, d_end;
2147dee93a9SHari Bathini 	bool ret = false;
2157dee93a9SHari Bathini 	int i;
2167dee93a9SHari Bathini 
2177dee93a9SHari Bathini 	for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) {
2187dee93a9SHari Bathini 		d_start = fw_dump.boot_mem_addr[i];
2197dee93a9SHari Bathini 		d_end   = d_start + fw_dump.boot_mem_sz[i];
2207dee93a9SHari Bathini 
2217dee93a9SHari Bathini 		ret = is_fadump_mem_area_contiguous(d_start, d_end);
2227dee93a9SHari Bathini 		if (!ret)
2237dee93a9SHari Bathini 			break;
2247dee93a9SHari Bathini 	}
2257dee93a9SHari Bathini 
2267dee93a9SHari Bathini 	return ret;
227961cf26aSHari Bathini }
228961cf26aSHari Bathini 
229961cf26aSHari Bathini /*
230f86593beSMahesh Salgaonkar  * Returns true, if there are no holes in reserved memory area,
231f86593beSMahesh Salgaonkar  * false otherwise.
232f86593beSMahesh Salgaonkar  */
2337f0ad11dSHari Bathini bool is_fadump_reserved_mem_contiguous(void)
234f86593beSMahesh Salgaonkar {
235961cf26aSHari Bathini 	u64 d_start, d_end;
236f86593beSMahesh Salgaonkar 
237961cf26aSHari Bathini 	d_start	= fw_dump.reserve_dump_area_start;
238961cf26aSHari Bathini 	d_end	= d_start + fw_dump.reserve_dump_area_size;
239961cf26aSHari Bathini 	return is_fadump_mem_area_contiguous(d_start, d_end);
240f86593beSMahesh Salgaonkar }
241f86593beSMahesh Salgaonkar 
2423ccc00a7SMahesh Salgaonkar /* Print firmware assisted dump configurations for debugging purpose. */
2433ccc00a7SMahesh Salgaonkar static void fadump_show_config(void)
2443ccc00a7SMahesh Salgaonkar {
2457dee93a9SHari Bathini 	int i;
2467dee93a9SHari Bathini 
2473ccc00a7SMahesh Salgaonkar 	pr_debug("Support for firmware-assisted dump (fadump): %s\n",
2483ccc00a7SMahesh Salgaonkar 			(fw_dump.fadump_supported ? "present" : "no support"));
2493ccc00a7SMahesh Salgaonkar 
2503ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_supported)
2513ccc00a7SMahesh Salgaonkar 		return;
2523ccc00a7SMahesh Salgaonkar 
2533ccc00a7SMahesh Salgaonkar 	pr_debug("Fadump enabled    : %s\n",
2543ccc00a7SMahesh Salgaonkar 				(fw_dump.fadump_enabled ? "yes" : "no"));
2553ccc00a7SMahesh Salgaonkar 	pr_debug("Dump Active       : %s\n",
2563ccc00a7SMahesh Salgaonkar 				(fw_dump.dump_active ? "yes" : "no"));
2573ccc00a7SMahesh Salgaonkar 	pr_debug("Dump section sizes:\n");
2583ccc00a7SMahesh Salgaonkar 	pr_debug("    CPU state data size: %lx\n", fw_dump.cpu_state_data_size);
2593ccc00a7SMahesh Salgaonkar 	pr_debug("    HPTE region size   : %lx\n", fw_dump.hpte_region_size);
2603ccc00a7SMahesh Salgaonkar 	pr_debug("    Boot memory size   : %lx\n", fw_dump.boot_memory_size);
2617dee93a9SHari Bathini 	pr_debug("    Boot memory top    : %llx\n", fw_dump.boot_mem_top);
2627dee93a9SHari Bathini 	pr_debug("Boot memory regions cnt: %llx\n", fw_dump.boot_mem_regs_cnt);
2637dee93a9SHari Bathini 	for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) {
2647dee93a9SHari Bathini 		pr_debug("[%03d] base = %llx, size = %llx\n", i,
2657dee93a9SHari Bathini 			 fw_dump.boot_mem_addr[i], fw_dump.boot_mem_sz[i]);
2667dee93a9SHari Bathini 	}
2673ccc00a7SMahesh Salgaonkar }
2683ccc00a7SMahesh Salgaonkar 
269eb39c880SMahesh Salgaonkar /**
270eb39c880SMahesh Salgaonkar  * fadump_calculate_reserve_size(): reserve variable boot area 5% of System RAM
271eb39c880SMahesh Salgaonkar  *
272eb39c880SMahesh Salgaonkar  * Function to find the largest memory size we need to reserve during early
273eb39c880SMahesh Salgaonkar  * boot process. This will be the size of the memory that is required for a
274eb39c880SMahesh Salgaonkar  * kernel to boot successfully.
275eb39c880SMahesh Salgaonkar  *
276eb39c880SMahesh Salgaonkar  * This function has been taken from phyp-assisted dump feature implementation.
277eb39c880SMahesh Salgaonkar  *
278eb39c880SMahesh Salgaonkar  * returns larger of 256MB or 5% rounded down to multiples of 256MB.
279eb39c880SMahesh Salgaonkar  *
280eb39c880SMahesh Salgaonkar  * TODO: Come up with better approach to find out more accurate memory size
281eb39c880SMahesh Salgaonkar  * that is required for a kernel to boot successfully.
282eb39c880SMahesh Salgaonkar  *
283eb39c880SMahesh Salgaonkar  */
2847b1b3b48SHari Bathini static inline u64 fadump_calculate_reserve_size(void)
285eb39c880SMahesh Salgaonkar {
2867b1b3b48SHari Bathini 	u64 base, size, bootmem_min;
28711550dc0SHari Bathini 	int ret;
288eb39c880SMahesh Salgaonkar 
28981d9eca5SHari Bathini 	if (fw_dump.reserve_bootvar)
29081d9eca5SHari Bathini 		pr_warn("'fadump_reserve_mem=' parameter is deprecated in favor of 'crashkernel=' parameter.\n");
29181d9eca5SHari Bathini 
292eb39c880SMahesh Salgaonkar 	/*
29311550dc0SHari Bathini 	 * Check if the size is specified through crashkernel= cmdline
294e7467dc6SHari Bathini 	 * option. If yes, then use that but ignore base as fadump reserves
295e7467dc6SHari Bathini 	 * memory at a predefined offset.
296eb39c880SMahesh Salgaonkar 	 */
29711550dc0SHari Bathini 	ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
29811550dc0SHari Bathini 				&size, &base);
29911550dc0SHari Bathini 	if (ret == 0 && size > 0) {
30048a316e3SHari Bathini 		unsigned long max_size;
30148a316e3SHari Bathini 
30281d9eca5SHari Bathini 		if (fw_dump.reserve_bootvar)
30381d9eca5SHari Bathini 			pr_info("Using 'crashkernel=' parameter for memory reservation.\n");
30481d9eca5SHari Bathini 
30511550dc0SHari Bathini 		fw_dump.reserve_bootvar = (unsigned long)size;
30648a316e3SHari Bathini 
30748a316e3SHari Bathini 		/*
30848a316e3SHari Bathini 		 * Adjust if the boot memory size specified is above
30948a316e3SHari Bathini 		 * the upper limit.
31048a316e3SHari Bathini 		 */
31148a316e3SHari Bathini 		max_size = memblock_phys_mem_size() / MAX_BOOT_MEM_RATIO;
31248a316e3SHari Bathini 		if (fw_dump.reserve_bootvar > max_size) {
31348a316e3SHari Bathini 			fw_dump.reserve_bootvar = max_size;
31448a316e3SHari Bathini 			pr_info("Adjusted boot memory size to %luMB\n",
31548a316e3SHari Bathini 				(fw_dump.reserve_bootvar >> 20));
31648a316e3SHari Bathini 		}
31748a316e3SHari Bathini 
318eb39c880SMahesh Salgaonkar 		return fw_dump.reserve_bootvar;
31981d9eca5SHari Bathini 	} else if (fw_dump.reserve_bootvar) {
32081d9eca5SHari Bathini 		/*
32181d9eca5SHari Bathini 		 * 'fadump_reserve_mem=' is being used to reserve memory
32281d9eca5SHari Bathini 		 * for firmware-assisted dump.
32381d9eca5SHari Bathini 		 */
32481d9eca5SHari Bathini 		return fw_dump.reserve_bootvar;
32511550dc0SHari Bathini 	}
326eb39c880SMahesh Salgaonkar 
327eb39c880SMahesh Salgaonkar 	/* divide by 20 to get 5% of value */
32848a316e3SHari Bathini 	size = memblock_phys_mem_size() / 20;
329eb39c880SMahesh Salgaonkar 
330eb39c880SMahesh Salgaonkar 	/* round it down in multiples of 256 */
331eb39c880SMahesh Salgaonkar 	size = size & ~0x0FFFFFFFUL;
332eb39c880SMahesh Salgaonkar 
333eb39c880SMahesh Salgaonkar 	/* Truncate to memory_limit. We don't want to over reserve the memory.*/
334eb39c880SMahesh Salgaonkar 	if (memory_limit && size > memory_limit)
335eb39c880SMahesh Salgaonkar 		size = memory_limit;
336eb39c880SMahesh Salgaonkar 
3377b1b3b48SHari Bathini 	bootmem_min = fw_dump.ops->fadump_get_bootmem_min();
3387b1b3b48SHari Bathini 	return (size > bootmem_min ? size : bootmem_min);
339eb39c880SMahesh Salgaonkar }
340eb39c880SMahesh Salgaonkar 
341eb39c880SMahesh Salgaonkar /*
342eb39c880SMahesh Salgaonkar  * Calculate the total memory size required to be reserved for
343eb39c880SMahesh Salgaonkar  * firmware-assisted dump registration.
344eb39c880SMahesh Salgaonkar  */
345eb39c880SMahesh Salgaonkar static unsigned long get_fadump_area_size(void)
346eb39c880SMahesh Salgaonkar {
347eb39c880SMahesh Salgaonkar 	unsigned long size = 0;
348eb39c880SMahesh Salgaonkar 
349eb39c880SMahesh Salgaonkar 	size += fw_dump.cpu_state_data_size;
350eb39c880SMahesh Salgaonkar 	size += fw_dump.hpte_region_size;
351eb39c880SMahesh Salgaonkar 	size += fw_dump.boot_memory_size;
3522df173d9SMahesh Salgaonkar 	size += sizeof(struct fadump_crash_info_header);
3532df173d9SMahesh Salgaonkar 	size += sizeof(struct elfhdr); /* ELF core header.*/
354ebaeb5aeSMahesh Salgaonkar 	size += sizeof(struct elf_phdr); /* place holder for cpu notes */
3552df173d9SMahesh Salgaonkar 	/* Program headers for crash memory regions. */
3562df173d9SMahesh Salgaonkar 	size += sizeof(struct elf_phdr) * (memblock_num_regions(memory) + 2);
357eb39c880SMahesh Salgaonkar 
358eb39c880SMahesh Salgaonkar 	size = PAGE_ALIGN(size);
359742a265aSHari Bathini 
360742a265aSHari Bathini 	/* This is to hold kernel metadata on platforms that support it */
361742a265aSHari Bathini 	size += (fw_dump.ops->fadump_get_metadata_size ?
362742a265aSHari Bathini 		 fw_dump.ops->fadump_get_metadata_size() : 0);
363eb39c880SMahesh Salgaonkar 	return size;
364eb39c880SMahesh Salgaonkar }
365eb39c880SMahesh Salgaonkar 
3667dee93a9SHari Bathini static int __init add_boot_mem_region(unsigned long rstart,
3677dee93a9SHari Bathini 				      unsigned long rsize)
3687dee93a9SHari Bathini {
3697dee93a9SHari Bathini 	int i = fw_dump.boot_mem_regs_cnt++;
3707dee93a9SHari Bathini 
3717dee93a9SHari Bathini 	if (fw_dump.boot_mem_regs_cnt > FADUMP_MAX_MEM_REGS) {
3727dee93a9SHari Bathini 		fw_dump.boot_mem_regs_cnt = FADUMP_MAX_MEM_REGS;
3737dee93a9SHari Bathini 		return 0;
3747dee93a9SHari Bathini 	}
3757dee93a9SHari Bathini 
3767dee93a9SHari Bathini 	pr_debug("Added boot memory range[%d] [%#016lx-%#016lx)\n",
3777dee93a9SHari Bathini 		 i, rstart, (rstart + rsize));
3787dee93a9SHari Bathini 	fw_dump.boot_mem_addr[i] = rstart;
3797dee93a9SHari Bathini 	fw_dump.boot_mem_sz[i] = rsize;
3807dee93a9SHari Bathini 	return 1;
3817dee93a9SHari Bathini }
3827dee93a9SHari Bathini 
3837dee93a9SHari Bathini /*
3847dee93a9SHari Bathini  * Firmware usually has a hard limit on the data it can copy per region.
3857dee93a9SHari Bathini  * Honour that by splitting a memory range into multiple regions.
3867dee93a9SHari Bathini  */
3877dee93a9SHari Bathini static int __init add_boot_mem_regions(unsigned long mstart,
3887dee93a9SHari Bathini 				       unsigned long msize)
3897dee93a9SHari Bathini {
3907dee93a9SHari Bathini 	unsigned long rstart, rsize, max_size;
3917dee93a9SHari Bathini 	int ret = 1;
3927dee93a9SHari Bathini 
3937dee93a9SHari Bathini 	rstart = mstart;
3947dee93a9SHari Bathini 	max_size = fw_dump.max_copy_size ? fw_dump.max_copy_size : msize;
3957dee93a9SHari Bathini 	while (msize) {
3967dee93a9SHari Bathini 		if (msize > max_size)
3977dee93a9SHari Bathini 			rsize = max_size;
3987dee93a9SHari Bathini 		else
3997dee93a9SHari Bathini 			rsize = msize;
4007dee93a9SHari Bathini 
4017dee93a9SHari Bathini 		ret = add_boot_mem_region(rstart, rsize);
4027dee93a9SHari Bathini 		if (!ret)
4037dee93a9SHari Bathini 			break;
4047dee93a9SHari Bathini 
4057dee93a9SHari Bathini 		msize -= rsize;
4067dee93a9SHari Bathini 		rstart += rsize;
4077dee93a9SHari Bathini 	}
4087dee93a9SHari Bathini 
4097dee93a9SHari Bathini 	return ret;
4107dee93a9SHari Bathini }
4117dee93a9SHari Bathini 
4127dee93a9SHari Bathini static int __init fadump_get_boot_mem_regions(void)
4137dee93a9SHari Bathini {
4147dee93a9SHari Bathini 	unsigned long base, size, cur_size, hole_size, last_end;
4157dee93a9SHari Bathini 	unsigned long mem_size = fw_dump.boot_memory_size;
4167dee93a9SHari Bathini 	struct memblock_region *reg;
4177dee93a9SHari Bathini 	int ret = 1;
4187dee93a9SHari Bathini 
4197dee93a9SHari Bathini 	fw_dump.boot_mem_regs_cnt = 0;
4207dee93a9SHari Bathini 
4217dee93a9SHari Bathini 	last_end = 0;
4227dee93a9SHari Bathini 	hole_size = 0;
4237dee93a9SHari Bathini 	cur_size = 0;
4247dee93a9SHari Bathini 	for_each_memblock(memory, reg) {
4257dee93a9SHari Bathini 		base = reg->base;
4267dee93a9SHari Bathini 		size = reg->size;
4277dee93a9SHari Bathini 		hole_size += (base - last_end);
4287dee93a9SHari Bathini 
4297dee93a9SHari Bathini 		if ((cur_size + size) >= mem_size) {
4307dee93a9SHari Bathini 			size = (mem_size - cur_size);
4317dee93a9SHari Bathini 			ret = add_boot_mem_regions(base, size);
4327dee93a9SHari Bathini 			break;
4337dee93a9SHari Bathini 		}
4347dee93a9SHari Bathini 
4357dee93a9SHari Bathini 		mem_size -= size;
4367dee93a9SHari Bathini 		cur_size += size;
4377dee93a9SHari Bathini 		ret = add_boot_mem_regions(base, size);
4387dee93a9SHari Bathini 		if (!ret)
4397dee93a9SHari Bathini 			break;
4407dee93a9SHari Bathini 
4417dee93a9SHari Bathini 		last_end = base + size;
4427dee93a9SHari Bathini 	}
4437dee93a9SHari Bathini 	fw_dump.boot_mem_top = PAGE_ALIGN(fw_dump.boot_memory_size + hole_size);
4447dee93a9SHari Bathini 
4457dee93a9SHari Bathini 	return ret;
4467dee93a9SHari Bathini }
4477dee93a9SHari Bathini 
448eb39c880SMahesh Salgaonkar int __init fadump_reserve_mem(void)
449eb39c880SMahesh Salgaonkar {
4507b1b3b48SHari Bathini 	u64 base, size, mem_boundary, bootmem_min, align = PAGE_SIZE;
451579ca1a2SHari Bathini 	bool is_memblock_bottom_up = memblock_bottom_up();
4526abec12cSHari Bathini 	int ret = 1;
453eb39c880SMahesh Salgaonkar 
454eb39c880SMahesh Salgaonkar 	if (!fw_dump.fadump_enabled)
455eb39c880SMahesh Salgaonkar 		return 0;
456eb39c880SMahesh Salgaonkar 
457eb39c880SMahesh Salgaonkar 	if (!fw_dump.fadump_supported) {
4586abec12cSHari Bathini 		pr_info("Firmware-Assisted Dump is not supported on this hardware\n");
4596abec12cSHari Bathini 		goto error_out;
460eb39c880SMahesh Salgaonkar 	}
461742a265aSHari Bathini 
4623ccc00a7SMahesh Salgaonkar 	/*
4633ccc00a7SMahesh Salgaonkar 	 * Initialize boot memory size
4643ccc00a7SMahesh Salgaonkar 	 * If dump is active then we have already calculated the size during
4653ccc00a7SMahesh Salgaonkar 	 * first kernel.
4663ccc00a7SMahesh Salgaonkar 	 */
467f3512011SHari Bathini 	if (!fw_dump.dump_active) {
4686abec12cSHari Bathini 		fw_dump.boot_memory_size =
4696abec12cSHari Bathini 			PAGE_ALIGN(fadump_calculate_reserve_size());
470a4e92ce8SMahesh Salgaonkar #ifdef CONFIG_CMA
471579ca1a2SHari Bathini 		if (!fw_dump.nocma) {
472579ca1a2SHari Bathini 			align = FADUMP_CMA_ALIGNMENT;
473a4e92ce8SMahesh Salgaonkar 			fw_dump.boot_memory_size =
474579ca1a2SHari Bathini 				ALIGN(fw_dump.boot_memory_size, align);
475579ca1a2SHari Bathini 		}
476a4e92ce8SMahesh Salgaonkar #endif
4777b1b3b48SHari Bathini 
4787b1b3b48SHari Bathini 		bootmem_min = fw_dump.ops->fadump_get_bootmem_min();
4797b1b3b48SHari Bathini 		if (fw_dump.boot_memory_size < bootmem_min) {
4807b1b3b48SHari Bathini 			pr_err("Can't enable fadump with boot memory size (0x%lx) less than 0x%llx\n",
4817b1b3b48SHari Bathini 			       fw_dump.boot_memory_size, bootmem_min);
4827b1b3b48SHari Bathini 			goto error_out;
4837b1b3b48SHari Bathini 		}
4847dee93a9SHari Bathini 
4857dee93a9SHari Bathini 		if (!fadump_get_boot_mem_regions()) {
4867dee93a9SHari Bathini 			pr_err("Too many holes in boot memory area to enable fadump\n");
4877dee93a9SHari Bathini 			goto error_out;
4887dee93a9SHari Bathini 		}
489a4e92ce8SMahesh Salgaonkar 	}
490eb39c880SMahesh Salgaonkar 
491eb39c880SMahesh Salgaonkar 	/*
492eb39c880SMahesh Salgaonkar 	 * Calculate the memory boundary.
493eb39c880SMahesh Salgaonkar 	 * If memory_limit is less than actual memory boundary then reserve
494eb39c880SMahesh Salgaonkar 	 * the memory for fadump beyond the memory_limit and adjust the
495eb39c880SMahesh Salgaonkar 	 * memory_limit accordingly, so that the running kernel can run with
496eb39c880SMahesh Salgaonkar 	 * specified memory_limit.
497eb39c880SMahesh Salgaonkar 	 */
498eb39c880SMahesh Salgaonkar 	if (memory_limit && memory_limit < memblock_end_of_DRAM()) {
499eb39c880SMahesh Salgaonkar 		size = get_fadump_area_size();
500eb39c880SMahesh Salgaonkar 		if ((memory_limit + size) < memblock_end_of_DRAM())
501eb39c880SMahesh Salgaonkar 			memory_limit += size;
502eb39c880SMahesh Salgaonkar 		else
503eb39c880SMahesh Salgaonkar 			memory_limit = memblock_end_of_DRAM();
504eb39c880SMahesh Salgaonkar 		printk(KERN_INFO "Adjusted memory_limit for firmware-assisted"
505a84fcd46SSuzuki Poulose 				" dump, now %#016llx\n", memory_limit);
506eb39c880SMahesh Salgaonkar 	}
507eb39c880SMahesh Salgaonkar 	if (memory_limit)
5086abec12cSHari Bathini 		mem_boundary = memory_limit;
509eb39c880SMahesh Salgaonkar 	else
5106abec12cSHari Bathini 		mem_boundary = memblock_end_of_DRAM();
511eb39c880SMahesh Salgaonkar 
5127dee93a9SHari Bathini 	base = fw_dump.boot_mem_top;
5138255da95SHari Bathini 	size = get_fadump_area_size();
5148255da95SHari Bathini 	fw_dump.reserve_dump_area_size = size;
515eb39c880SMahesh Salgaonkar 	if (fw_dump.dump_active) {
516b71a693dSMahesh Salgaonkar 		pr_info("Firmware-assisted dump is active.\n");
517b71a693dSMahesh Salgaonkar 
51885975387SHari Bathini #ifdef CONFIG_HUGETLB_PAGE
51985975387SHari Bathini 		/*
52085975387SHari Bathini 		 * FADump capture kernel doesn't care much about hugepages.
52185975387SHari Bathini 		 * In fact, handling hugepages in capture kernel is asking for
52285975387SHari Bathini 		 * trouble. So, disable HugeTLB support when fadump is active.
52385975387SHari Bathini 		 */
52485975387SHari Bathini 		hugetlb_disabled = true;
52585975387SHari Bathini #endif
526eb39c880SMahesh Salgaonkar 		/*
527eb39c880SMahesh Salgaonkar 		 * If last boot has crashed then reserve all the memory
528b2a815a5SHari Bathini 		 * above boot memory size so that we don't touch it until
529eb39c880SMahesh Salgaonkar 		 * dump is written to disk by userspace tool. This memory
530b2a815a5SHari Bathini 		 * can be released for general use by invalidating fadump.
531eb39c880SMahesh Salgaonkar 		 */
532b2a815a5SHari Bathini 		fadump_reserve_crash_area(base);
5332df173d9SMahesh Salgaonkar 
534f3512011SHari Bathini 		pr_debug("fadumphdr_addr = %#016lx\n", fw_dump.fadumphdr_addr);
535f3512011SHari Bathini 		pr_debug("Reserve dump area start address: 0x%lx\n",
536f3512011SHari Bathini 			 fw_dump.reserve_dump_area_start);
5378255da95SHari Bathini 	} else {
5388255da95SHari Bathini 		/*
539f6e6bedbSHari Bathini 		 * Reserve memory at an offset closer to bottom of the RAM to
540579ca1a2SHari Bathini 		 * minimize the impact of memory hot-remove operation.
541f6e6bedbSHari Bathini 		 */
542579ca1a2SHari Bathini 		memblock_set_bottom_up(true);
543579ca1a2SHari Bathini 		base = memblock_find_in_range(base, mem_boundary, size, align);
5446abec12cSHari Bathini 
545579ca1a2SHari Bathini 		/* Restore the previous allocation mode */
546579ca1a2SHari Bathini 		memblock_set_bottom_up(is_memblock_bottom_up);
547f6e6bedbSHari Bathini 
548579ca1a2SHari Bathini 		if (!base) {
549742a265aSHari Bathini 			pr_err("Failed to find memory chunk for reservation!\n");
550742a265aSHari Bathini 			goto error_out;
551742a265aSHari Bathini 		}
552742a265aSHari Bathini 		fw_dump.reserve_dump_area_start = base;
553742a265aSHari Bathini 
554742a265aSHari Bathini 		/*
555742a265aSHari Bathini 		 * Calculate the kernel metadata address and register it with
556742a265aSHari Bathini 		 * f/w if the platform supports.
557742a265aSHari Bathini 		 */
558742a265aSHari Bathini 		if (fw_dump.ops->fadump_setup_metadata &&
559742a265aSHari Bathini 		    (fw_dump.ops->fadump_setup_metadata(&fw_dump) < 0))
560742a265aSHari Bathini 			goto error_out;
561742a265aSHari Bathini 
562742a265aSHari Bathini 		if (memblock_reserve(base, size)) {
5636abec12cSHari Bathini 			pr_err("Failed to reserve memory!\n");
5646abec12cSHari Bathini 			goto error_out;
5656abec12cSHari Bathini 		}
5666abec12cSHari Bathini 
5676abec12cSHari Bathini 		pr_info("Reserved %lldMB of memory at %#016llx (System RAM: %lldMB)\n",
5686abec12cSHari Bathini 			(size >> 20), base, (memblock_phys_mem_size() >> 20));
569f6e6bedbSHari Bathini 
5706abec12cSHari Bathini 		ret = fadump_cma_init();
571a4e92ce8SMahesh Salgaonkar 	}
5726abec12cSHari Bathini 
5736abec12cSHari Bathini 	return ret;
5746abec12cSHari Bathini error_out:
5756abec12cSHari Bathini 	fw_dump.fadump_enabled = 0;
5766abec12cSHari Bathini 	return 0;
577eb39c880SMahesh Salgaonkar }
578eb39c880SMahesh Salgaonkar 
579eb39c880SMahesh Salgaonkar /* Look for fadump= cmdline option. */
580eb39c880SMahesh Salgaonkar static int __init early_fadump_param(char *p)
581eb39c880SMahesh Salgaonkar {
582eb39c880SMahesh Salgaonkar 	if (!p)
583eb39c880SMahesh Salgaonkar 		return 1;
584eb39c880SMahesh Salgaonkar 
585eb39c880SMahesh Salgaonkar 	if (strncmp(p, "on", 2) == 0)
586eb39c880SMahesh Salgaonkar 		fw_dump.fadump_enabled = 1;
587eb39c880SMahesh Salgaonkar 	else if (strncmp(p, "off", 3) == 0)
588eb39c880SMahesh Salgaonkar 		fw_dump.fadump_enabled = 0;
589a4e92ce8SMahesh Salgaonkar 	else if (strncmp(p, "nocma", 5) == 0) {
590a4e92ce8SMahesh Salgaonkar 		fw_dump.fadump_enabled = 1;
591a4e92ce8SMahesh Salgaonkar 		fw_dump.nocma = 1;
592a4e92ce8SMahesh Salgaonkar 	}
593eb39c880SMahesh Salgaonkar 
594eb39c880SMahesh Salgaonkar 	return 0;
595eb39c880SMahesh Salgaonkar }
596eb39c880SMahesh Salgaonkar early_param("fadump", early_fadump_param);
597eb39c880SMahesh Salgaonkar 
59881d9eca5SHari Bathini /*
59981d9eca5SHari Bathini  * Look for fadump_reserve_mem= cmdline option
60081d9eca5SHari Bathini  * TODO: Remove references to 'fadump_reserve_mem=' parameter,
60181d9eca5SHari Bathini  *       the sooner 'crashkernel=' parameter is accustomed to.
60281d9eca5SHari Bathini  */
60381d9eca5SHari Bathini static int __init early_fadump_reserve_mem(char *p)
60481d9eca5SHari Bathini {
60581d9eca5SHari Bathini 	if (p)
60681d9eca5SHari Bathini 		fw_dump.reserve_bootvar = memparse(p, &p);
60781d9eca5SHari Bathini 	return 0;
60881d9eca5SHari Bathini }
60981d9eca5SHari Bathini early_param("fadump_reserve_mem", early_fadump_reserve_mem);
61081d9eca5SHari Bathini 
611ebaeb5aeSMahesh Salgaonkar void crash_fadump(struct pt_regs *regs, const char *str)
612ebaeb5aeSMahesh Salgaonkar {
613ebaeb5aeSMahesh Salgaonkar 	struct fadump_crash_info_header *fdh = NULL;
614f2a5e8f0SMahesh Salgaonkar 	int old_cpu, this_cpu;
615ebaeb5aeSMahesh Salgaonkar 
6166fcd6baaSNicholas Piggin 	if (!should_fadump_crash())
617ebaeb5aeSMahesh Salgaonkar 		return;
618ebaeb5aeSMahesh Salgaonkar 
619f2a5e8f0SMahesh Salgaonkar 	/*
620f2a5e8f0SMahesh Salgaonkar 	 * old_cpu == -1 means this is the first CPU which has come here,
621f2a5e8f0SMahesh Salgaonkar 	 * go ahead and trigger fadump.
622f2a5e8f0SMahesh Salgaonkar 	 *
623f2a5e8f0SMahesh Salgaonkar 	 * old_cpu != -1 means some other CPU has already on it's way
624f2a5e8f0SMahesh Salgaonkar 	 * to trigger fadump, just keep looping here.
625f2a5e8f0SMahesh Salgaonkar 	 */
626f2a5e8f0SMahesh Salgaonkar 	this_cpu = smp_processor_id();
627f2a5e8f0SMahesh Salgaonkar 	old_cpu = cmpxchg(&crashing_cpu, -1, this_cpu);
628f2a5e8f0SMahesh Salgaonkar 
629f2a5e8f0SMahesh Salgaonkar 	if (old_cpu != -1) {
630f2a5e8f0SMahesh Salgaonkar 		/*
631f2a5e8f0SMahesh Salgaonkar 		 * We can't loop here indefinitely. Wait as long as fadump
632f2a5e8f0SMahesh Salgaonkar 		 * is in force. If we race with fadump un-registration this
633f2a5e8f0SMahesh Salgaonkar 		 * loop will break and then we go down to normal panic path
634f2a5e8f0SMahesh Salgaonkar 		 * and reboot. If fadump is in force the first crashing
635f2a5e8f0SMahesh Salgaonkar 		 * cpu will definitely trigger fadump.
636f2a5e8f0SMahesh Salgaonkar 		 */
637f2a5e8f0SMahesh Salgaonkar 		while (fw_dump.dump_registered)
638f2a5e8f0SMahesh Salgaonkar 			cpu_relax();
639f2a5e8f0SMahesh Salgaonkar 		return;
640f2a5e8f0SMahesh Salgaonkar 	}
641f2a5e8f0SMahesh Salgaonkar 
642ebaeb5aeSMahesh Salgaonkar 	fdh = __va(fw_dump.fadumphdr_addr);
643ebaeb5aeSMahesh Salgaonkar 	fdh->crashing_cpu = crashing_cpu;
644ebaeb5aeSMahesh Salgaonkar 	crash_save_vmcoreinfo();
645ebaeb5aeSMahesh Salgaonkar 
646ebaeb5aeSMahesh Salgaonkar 	if (regs)
647ebaeb5aeSMahesh Salgaonkar 		fdh->regs = *regs;
648ebaeb5aeSMahesh Salgaonkar 	else
649ebaeb5aeSMahesh Salgaonkar 		ppc_save_regs(&fdh->regs);
650ebaeb5aeSMahesh Salgaonkar 
651a0512164SRasmus Villemoes 	fdh->online_mask = *cpu_online_mask;
652ebaeb5aeSMahesh Salgaonkar 
65341a65d16SHari Bathini 	fw_dump.ops->fadump_trigger(fdh, str);
654ebaeb5aeSMahesh Salgaonkar }
655ebaeb5aeSMahesh Salgaonkar 
6567f0ad11dSHari Bathini u32 *fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs)
657ebaeb5aeSMahesh Salgaonkar {
658ebaeb5aeSMahesh Salgaonkar 	struct elf_prstatus prstatus;
659ebaeb5aeSMahesh Salgaonkar 
660ebaeb5aeSMahesh Salgaonkar 	memset(&prstatus, 0, sizeof(prstatus));
661ebaeb5aeSMahesh Salgaonkar 	/*
662ebaeb5aeSMahesh Salgaonkar 	 * FIXME: How do i get PID? Do I really need it?
663ebaeb5aeSMahesh Salgaonkar 	 * prstatus.pr_pid = ????
664ebaeb5aeSMahesh Salgaonkar 	 */
665ebaeb5aeSMahesh Salgaonkar 	elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
66622bd0177SHari Bathini 	buf = append_elf_note(buf, CRASH_CORE_NOTE_NAME, NT_PRSTATUS,
667ebaeb5aeSMahesh Salgaonkar 			      &prstatus, sizeof(prstatus));
668ebaeb5aeSMahesh Salgaonkar 	return buf;
669ebaeb5aeSMahesh Salgaonkar }
670ebaeb5aeSMahesh Salgaonkar 
6717f0ad11dSHari Bathini void fadump_update_elfcore_header(char *bufp)
672ebaeb5aeSMahesh Salgaonkar {
673ebaeb5aeSMahesh Salgaonkar 	struct elfhdr *elf;
674ebaeb5aeSMahesh Salgaonkar 	struct elf_phdr *phdr;
675ebaeb5aeSMahesh Salgaonkar 
676ebaeb5aeSMahesh Salgaonkar 	elf = (struct elfhdr *)bufp;
677ebaeb5aeSMahesh Salgaonkar 	bufp += sizeof(struct elfhdr);
678ebaeb5aeSMahesh Salgaonkar 
679ebaeb5aeSMahesh Salgaonkar 	/* First note is a place holder for cpu notes info. */
680ebaeb5aeSMahesh Salgaonkar 	phdr = (struct elf_phdr *)bufp;
681ebaeb5aeSMahesh Salgaonkar 
682ebaeb5aeSMahesh Salgaonkar 	if (phdr->p_type == PT_NOTE) {
683961cf26aSHari Bathini 		phdr->p_paddr	= __pa(fw_dump.cpu_notes_buf_vaddr);
684ebaeb5aeSMahesh Salgaonkar 		phdr->p_offset	= phdr->p_paddr;
685ebaeb5aeSMahesh Salgaonkar 		phdr->p_filesz	= fw_dump.cpu_notes_buf_size;
686ebaeb5aeSMahesh Salgaonkar 		phdr->p_memsz = fw_dump.cpu_notes_buf_size;
687ebaeb5aeSMahesh Salgaonkar 	}
688ebaeb5aeSMahesh Salgaonkar 	return;
689ebaeb5aeSMahesh Salgaonkar }
690ebaeb5aeSMahesh Salgaonkar 
691961cf26aSHari Bathini static void *fadump_alloc_buffer(unsigned long size)
692ebaeb5aeSMahesh Salgaonkar {
69372aa6517SHari Bathini 	unsigned long count, i;
694ebaeb5aeSMahesh Salgaonkar 	struct page *page;
69572aa6517SHari Bathini 	void *vaddr;
696ebaeb5aeSMahesh Salgaonkar 
69772aa6517SHari Bathini 	vaddr = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
698ebaeb5aeSMahesh Salgaonkar 	if (!vaddr)
699ebaeb5aeSMahesh Salgaonkar 		return NULL;
700ebaeb5aeSMahesh Salgaonkar 
70172aa6517SHari Bathini 	count = PAGE_ALIGN(size) / PAGE_SIZE;
702ebaeb5aeSMahesh Salgaonkar 	page = virt_to_page(vaddr);
703ebaeb5aeSMahesh Salgaonkar 	for (i = 0; i < count; i++)
70472aa6517SHari Bathini 		mark_page_reserved(page + i);
705ebaeb5aeSMahesh Salgaonkar 	return vaddr;
706ebaeb5aeSMahesh Salgaonkar }
707ebaeb5aeSMahesh Salgaonkar 
708961cf26aSHari Bathini static void fadump_free_buffer(unsigned long vaddr, unsigned long size)
709ebaeb5aeSMahesh Salgaonkar {
71072aa6517SHari Bathini 	free_reserved_area((void *)vaddr, (void *)(vaddr + size), -1, NULL);
711ebaeb5aeSMahesh Salgaonkar }
712ebaeb5aeSMahesh Salgaonkar 
7137f0ad11dSHari Bathini s32 fadump_setup_cpu_notes_buf(u32 num_cpus)
714961cf26aSHari Bathini {
715961cf26aSHari Bathini 	/* Allocate buffer to hold cpu crash notes. */
716961cf26aSHari Bathini 	fw_dump.cpu_notes_buf_size = num_cpus * sizeof(note_buf_t);
717961cf26aSHari Bathini 	fw_dump.cpu_notes_buf_size = PAGE_ALIGN(fw_dump.cpu_notes_buf_size);
718961cf26aSHari Bathini 	fw_dump.cpu_notes_buf_vaddr =
719961cf26aSHari Bathini 		(unsigned long)fadump_alloc_buffer(fw_dump.cpu_notes_buf_size);
720961cf26aSHari Bathini 	if (!fw_dump.cpu_notes_buf_vaddr) {
721961cf26aSHari Bathini 		pr_err("Failed to allocate %ld bytes for CPU notes buffer\n",
722961cf26aSHari Bathini 		       fw_dump.cpu_notes_buf_size);
723961cf26aSHari Bathini 		return -ENOMEM;
724961cf26aSHari Bathini 	}
725961cf26aSHari Bathini 
726961cf26aSHari Bathini 	pr_debug("Allocated buffer for cpu notes of size %ld at 0x%lx\n",
727961cf26aSHari Bathini 		 fw_dump.cpu_notes_buf_size,
728961cf26aSHari Bathini 		 fw_dump.cpu_notes_buf_vaddr);
729961cf26aSHari Bathini 	return 0;
730961cf26aSHari Bathini }
731961cf26aSHari Bathini 
7327f0ad11dSHari Bathini void fadump_free_cpu_notes_buf(void)
733961cf26aSHari Bathini {
734961cf26aSHari Bathini 	if (!fw_dump.cpu_notes_buf_vaddr)
735961cf26aSHari Bathini 		return;
736961cf26aSHari Bathini 
737961cf26aSHari Bathini 	fadump_free_buffer(fw_dump.cpu_notes_buf_vaddr,
738961cf26aSHari Bathini 			   fw_dump.cpu_notes_buf_size);
739961cf26aSHari Bathini 	fw_dump.cpu_notes_buf_vaddr = 0;
740961cf26aSHari Bathini 	fw_dump.cpu_notes_buf_size = 0;
741961cf26aSHari Bathini }
742961cf26aSHari Bathini 
743e4fc48fbSHari Bathini static void fadump_free_mem_ranges(struct fadump_mrange_info *mrange_info)
7441bd6a1c4SHari Bathini {
745*02c04e37SHari Bathini 	if (mrange_info->is_static) {
746*02c04e37SHari Bathini 		mrange_info->mem_range_cnt = 0;
747*02c04e37SHari Bathini 		return;
748*02c04e37SHari Bathini 	}
749*02c04e37SHari Bathini 
750e4fc48fbSHari Bathini 	kfree(mrange_info->mem_ranges);
751*02c04e37SHari Bathini 	memset((void *)((u64)mrange_info + RNG_NAME_SZ), 0,
752*02c04e37SHari Bathini 	       (sizeof(struct fadump_mrange_info) - RNG_NAME_SZ));
7531bd6a1c4SHari Bathini }
7541bd6a1c4SHari Bathini 
7551bd6a1c4SHari Bathini /*
756e4fc48fbSHari Bathini  * Allocate or reallocate mem_ranges array in incremental units
7571bd6a1c4SHari Bathini  * of PAGE_SIZE.
7581bd6a1c4SHari Bathini  */
759e4fc48fbSHari Bathini static int fadump_alloc_mem_ranges(struct fadump_mrange_info *mrange_info)
7601bd6a1c4SHari Bathini {
761e4fc48fbSHari Bathini 	struct fadump_memory_range *new_array;
7621bd6a1c4SHari Bathini 	u64 new_size;
7631bd6a1c4SHari Bathini 
764e4fc48fbSHari Bathini 	new_size = mrange_info->mem_ranges_sz + PAGE_SIZE;
765e4fc48fbSHari Bathini 	pr_debug("Allocating %llu bytes of memory for %s memory ranges\n",
766e4fc48fbSHari Bathini 		 new_size, mrange_info->name);
7671bd6a1c4SHari Bathini 
768e4fc48fbSHari Bathini 	new_array = krealloc(mrange_info->mem_ranges, new_size, GFP_KERNEL);
7691bd6a1c4SHari Bathini 	if (new_array == NULL) {
770e4fc48fbSHari Bathini 		pr_err("Insufficient memory for setting up %s memory ranges\n",
771e4fc48fbSHari Bathini 		       mrange_info->name);
772e4fc48fbSHari Bathini 		fadump_free_mem_ranges(mrange_info);
7731bd6a1c4SHari Bathini 		return -ENOMEM;
7741bd6a1c4SHari Bathini 	}
7751bd6a1c4SHari Bathini 
776e4fc48fbSHari Bathini 	mrange_info->mem_ranges = new_array;
777e4fc48fbSHari Bathini 	mrange_info->mem_ranges_sz = new_size;
778e4fc48fbSHari Bathini 	mrange_info->max_mem_ranges = (new_size /
779e4fc48fbSHari Bathini 				       sizeof(struct fadump_memory_range));
7801bd6a1c4SHari Bathini 	return 0;
7811bd6a1c4SHari Bathini }
7821bd6a1c4SHari Bathini 
783e4fc48fbSHari Bathini static inline int fadump_add_mem_range(struct fadump_mrange_info *mrange_info,
784e4fc48fbSHari Bathini 				       u64 base, u64 end)
7852df173d9SMahesh Salgaonkar {
786e4fc48fbSHari Bathini 	struct fadump_memory_range *mem_ranges = mrange_info->mem_ranges;
787ced1bf52SHari Bathini 	bool is_adjacent = false;
788e4fc48fbSHari Bathini 	u64 start, size;
789ced1bf52SHari Bathini 
7902df173d9SMahesh Salgaonkar 	if (base == end)
7911bd6a1c4SHari Bathini 		return 0;
7921bd6a1c4SHari Bathini 
793ced1bf52SHari Bathini 	/*
794ced1bf52SHari Bathini 	 * Fold adjacent memory ranges to bring down the memory ranges/
795ced1bf52SHari Bathini 	 * PT_LOAD segments count.
796ced1bf52SHari Bathini 	 */
797e4fc48fbSHari Bathini 	if (mrange_info->mem_range_cnt) {
798e4fc48fbSHari Bathini 		start = mem_ranges[mrange_info->mem_range_cnt - 1].base;
799e4fc48fbSHari Bathini 		size  = mem_ranges[mrange_info->mem_range_cnt - 1].size;
800ced1bf52SHari Bathini 
801ced1bf52SHari Bathini 		if ((start + size) == base)
802ced1bf52SHari Bathini 			is_adjacent = true;
803ced1bf52SHari Bathini 	}
804ced1bf52SHari Bathini 	if (!is_adjacent) {
805ced1bf52SHari Bathini 		/* resize the array on reaching the limit */
806e4fc48fbSHari Bathini 		if (mrange_info->mem_range_cnt == mrange_info->max_mem_ranges) {
8071bd6a1c4SHari Bathini 			int ret;
8081bd6a1c4SHari Bathini 
809*02c04e37SHari Bathini 			if (mrange_info->is_static) {
810*02c04e37SHari Bathini 				pr_err("Reached array size limit for %s memory ranges\n",
811*02c04e37SHari Bathini 				       mrange_info->name);
812*02c04e37SHari Bathini 				return -ENOSPC;
813*02c04e37SHari Bathini 			}
814*02c04e37SHari Bathini 
815e4fc48fbSHari Bathini 			ret = fadump_alloc_mem_ranges(mrange_info);
8161bd6a1c4SHari Bathini 			if (ret)
8171bd6a1c4SHari Bathini 				return ret;
818e4fc48fbSHari Bathini 
819e4fc48fbSHari Bathini 			/* Update to the new resized array */
820e4fc48fbSHari Bathini 			mem_ranges = mrange_info->mem_ranges;
8211bd6a1c4SHari Bathini 		}
8222df173d9SMahesh Salgaonkar 
823ced1bf52SHari Bathini 		start = base;
824e4fc48fbSHari Bathini 		mem_ranges[mrange_info->mem_range_cnt].base = start;
825e4fc48fbSHari Bathini 		mrange_info->mem_range_cnt++;
826ced1bf52SHari Bathini 	}
827ced1bf52SHari Bathini 
828e4fc48fbSHari Bathini 	mem_ranges[mrange_info->mem_range_cnt - 1].size = (end - start);
829e4fc48fbSHari Bathini 	pr_debug("%s_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n",
830e4fc48fbSHari Bathini 		 mrange_info->name, (mrange_info->mem_range_cnt - 1),
831e4fc48fbSHari Bathini 		 start, end - 1, (end - start));
8321bd6a1c4SHari Bathini 	return 0;
8332df173d9SMahesh Salgaonkar }
8342df173d9SMahesh Salgaonkar 
835e4fc48fbSHari Bathini static int fadump_exclude_reserved_area(u64 start, u64 end)
8362df173d9SMahesh Salgaonkar {
837e4fc48fbSHari Bathini 	u64 ra_start, ra_end;
8381bd6a1c4SHari Bathini 	int ret = 0;
8392df173d9SMahesh Salgaonkar 
8402df173d9SMahesh Salgaonkar 	ra_start = fw_dump.reserve_dump_area_start;
8412df173d9SMahesh Salgaonkar 	ra_end = ra_start + fw_dump.reserve_dump_area_size;
8422df173d9SMahesh Salgaonkar 
8432df173d9SMahesh Salgaonkar 	if ((ra_start < end) && (ra_end > start)) {
8442df173d9SMahesh Salgaonkar 		if ((start < ra_start) && (end > ra_end)) {
845e4fc48fbSHari Bathini 			ret = fadump_add_mem_range(&crash_mrange_info,
846e4fc48fbSHari Bathini 						   start, ra_start);
8471bd6a1c4SHari Bathini 			if (ret)
8481bd6a1c4SHari Bathini 				return ret;
8491bd6a1c4SHari Bathini 
850e4fc48fbSHari Bathini 			ret = fadump_add_mem_range(&crash_mrange_info,
851e4fc48fbSHari Bathini 						   ra_end, end);
8522df173d9SMahesh Salgaonkar 		} else if (start < ra_start) {
853e4fc48fbSHari Bathini 			ret = fadump_add_mem_range(&crash_mrange_info,
854e4fc48fbSHari Bathini 						   start, ra_start);
8552df173d9SMahesh Salgaonkar 		} else if (ra_end < end) {
856e4fc48fbSHari Bathini 			ret = fadump_add_mem_range(&crash_mrange_info,
857e4fc48fbSHari Bathini 						   ra_end, end);
8582df173d9SMahesh Salgaonkar 		}
8592df173d9SMahesh Salgaonkar 	} else
860e4fc48fbSHari Bathini 		ret = fadump_add_mem_range(&crash_mrange_info, start, end);
8611bd6a1c4SHari Bathini 
8621bd6a1c4SHari Bathini 	return ret;
8632df173d9SMahesh Salgaonkar }
8642df173d9SMahesh Salgaonkar 
8652df173d9SMahesh Salgaonkar static int fadump_init_elfcore_header(char *bufp)
8662df173d9SMahesh Salgaonkar {
8672df173d9SMahesh Salgaonkar 	struct elfhdr *elf;
8682df173d9SMahesh Salgaonkar 
8692df173d9SMahesh Salgaonkar 	elf = (struct elfhdr *) bufp;
8702df173d9SMahesh Salgaonkar 	bufp += sizeof(struct elfhdr);
8712df173d9SMahesh Salgaonkar 	memcpy(elf->e_ident, ELFMAG, SELFMAG);
8722df173d9SMahesh Salgaonkar 	elf->e_ident[EI_CLASS] = ELF_CLASS;
8732df173d9SMahesh Salgaonkar 	elf->e_ident[EI_DATA] = ELF_DATA;
8742df173d9SMahesh Salgaonkar 	elf->e_ident[EI_VERSION] = EV_CURRENT;
8752df173d9SMahesh Salgaonkar 	elf->e_ident[EI_OSABI] = ELF_OSABI;
8762df173d9SMahesh Salgaonkar 	memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
8772df173d9SMahesh Salgaonkar 	elf->e_type = ET_CORE;
8782df173d9SMahesh Salgaonkar 	elf->e_machine = ELF_ARCH;
8792df173d9SMahesh Salgaonkar 	elf->e_version = EV_CURRENT;
8802df173d9SMahesh Salgaonkar 	elf->e_entry = 0;
8812df173d9SMahesh Salgaonkar 	elf->e_phoff = sizeof(struct elfhdr);
8822df173d9SMahesh Salgaonkar 	elf->e_shoff = 0;
883d8bced27SDaniel Axtens #if defined(_CALL_ELF)
884d8bced27SDaniel Axtens 	elf->e_flags = _CALL_ELF;
885d8bced27SDaniel Axtens #else
886d8bced27SDaniel Axtens 	elf->e_flags = 0;
887d8bced27SDaniel Axtens #endif
8882df173d9SMahesh Salgaonkar 	elf->e_ehsize = sizeof(struct elfhdr);
8892df173d9SMahesh Salgaonkar 	elf->e_phentsize = sizeof(struct elf_phdr);
8902df173d9SMahesh Salgaonkar 	elf->e_phnum = 0;
8912df173d9SMahesh Salgaonkar 	elf->e_shentsize = 0;
8922df173d9SMahesh Salgaonkar 	elf->e_shnum = 0;
8932df173d9SMahesh Salgaonkar 	elf->e_shstrndx = 0;
8942df173d9SMahesh Salgaonkar 
8952df173d9SMahesh Salgaonkar 	return 0;
8962df173d9SMahesh Salgaonkar }
8972df173d9SMahesh Salgaonkar 
8982df173d9SMahesh Salgaonkar /*
8992df173d9SMahesh Salgaonkar  * Traverse through memblock structure and setup crash memory ranges. These
9002df173d9SMahesh Salgaonkar  * ranges will be used create PT_LOAD program headers in elfcore header.
9012df173d9SMahesh Salgaonkar  */
9021bd6a1c4SHari Bathini static int fadump_setup_crash_memory_ranges(void)
9032df173d9SMahesh Salgaonkar {
9042df173d9SMahesh Salgaonkar 	struct memblock_region *reg;
905e4fc48fbSHari Bathini 	u64 start, end;
9067dee93a9SHari Bathini 	int i, ret;
9072df173d9SMahesh Salgaonkar 
9082df173d9SMahesh Salgaonkar 	pr_debug("Setup crash memory ranges.\n");
909e4fc48fbSHari Bathini 	crash_mrange_info.mem_range_cnt = 0;
910ced1bf52SHari Bathini 
9112df173d9SMahesh Salgaonkar 	/*
9127dee93a9SHari Bathini 	 * Boot memory region(s) registered with firmware are moved to
9137dee93a9SHari Bathini 	 * different location at the time of crash. Create separate program
9147dee93a9SHari Bathini 	 * header(s) for this memory chunk(s) with the correct offset.
9152df173d9SMahesh Salgaonkar 	 */
9167dee93a9SHari Bathini 	for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) {
9177dee93a9SHari Bathini 		start = fw_dump.boot_mem_addr[i];
9187dee93a9SHari Bathini 		end = start + fw_dump.boot_mem_sz[i];
9197dee93a9SHari Bathini 		ret = fadump_add_mem_range(&crash_mrange_info, start, end);
9201bd6a1c4SHari Bathini 		if (ret)
9211bd6a1c4SHari Bathini 			return ret;
9227dee93a9SHari Bathini 	}
9232df173d9SMahesh Salgaonkar 
9242df173d9SMahesh Salgaonkar 	for_each_memblock(memory, reg) {
925e4fc48fbSHari Bathini 		start = (u64)reg->base;
926e4fc48fbSHari Bathini 		end = start + (u64)reg->size;
927a77af552SHari Bathini 
928a77af552SHari Bathini 		/*
9297dee93a9SHari Bathini 		 * skip the memory chunk that is already added
9307dee93a9SHari Bathini 		 * (0 through boot_memory_top).
931a77af552SHari Bathini 		 */
9327dee93a9SHari Bathini 		if (start < fw_dump.boot_mem_top) {
9337dee93a9SHari Bathini 			if (end > fw_dump.boot_mem_top)
9347dee93a9SHari Bathini 				start = fw_dump.boot_mem_top;
935a77af552SHari Bathini 			else
936a77af552SHari Bathini 				continue;
937a77af552SHari Bathini 		}
9382df173d9SMahesh Salgaonkar 
9392df173d9SMahesh Salgaonkar 		/* add this range excluding the reserved dump area. */
9401bd6a1c4SHari Bathini 		ret = fadump_exclude_reserved_area(start, end);
9411bd6a1c4SHari Bathini 		if (ret)
9421bd6a1c4SHari Bathini 			return ret;
9432df173d9SMahesh Salgaonkar 	}
9441bd6a1c4SHari Bathini 
9451bd6a1c4SHari Bathini 	return 0;
9462df173d9SMahesh Salgaonkar }
9472df173d9SMahesh Salgaonkar 
948d34c5f26SMahesh Salgaonkar /*
949d34c5f26SMahesh Salgaonkar  * If the given physical address falls within the boot memory region then
950d34c5f26SMahesh Salgaonkar  * return the relocated address that points to the dump region reserved
951d34c5f26SMahesh Salgaonkar  * for saving initial boot memory contents.
952d34c5f26SMahesh Salgaonkar  */
953d34c5f26SMahesh Salgaonkar static inline unsigned long fadump_relocate(unsigned long paddr)
954d34c5f26SMahesh Salgaonkar {
9557dee93a9SHari Bathini 	unsigned long raddr, rstart, rend, rlast, hole_size;
9567dee93a9SHari Bathini 	int i;
9577dee93a9SHari Bathini 
9587dee93a9SHari Bathini 	hole_size = 0;
9597dee93a9SHari Bathini 	rlast = 0;
9607dee93a9SHari Bathini 	raddr = paddr;
9617dee93a9SHari Bathini 	for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) {
9627dee93a9SHari Bathini 		rstart = fw_dump.boot_mem_addr[i];
9637dee93a9SHari Bathini 		rend = rstart + fw_dump.boot_mem_sz[i];
9647dee93a9SHari Bathini 		hole_size += (rstart - rlast);
9657dee93a9SHari Bathini 
9667dee93a9SHari Bathini 		if (paddr >= rstart && paddr < rend) {
9677dee93a9SHari Bathini 			raddr += fw_dump.boot_mem_dest_addr - hole_size;
9687dee93a9SHari Bathini 			break;
9697dee93a9SHari Bathini 		}
9707dee93a9SHari Bathini 
9717dee93a9SHari Bathini 		rlast = rend;
9727dee93a9SHari Bathini 	}
9737dee93a9SHari Bathini 
9747dee93a9SHari Bathini 	pr_debug("vmcoreinfo: paddr = 0x%lx, raddr = 0x%lx\n", paddr, raddr);
9757dee93a9SHari Bathini 	return raddr;
976d34c5f26SMahesh Salgaonkar }
977d34c5f26SMahesh Salgaonkar 
9782df173d9SMahesh Salgaonkar static int fadump_create_elfcore_headers(char *bufp)
9792df173d9SMahesh Salgaonkar {
9807dee93a9SHari Bathini 	unsigned long long raddr, offset;
9812df173d9SMahesh Salgaonkar 	struct elf_phdr *phdr;
9827dee93a9SHari Bathini 	struct elfhdr *elf;
9837dee93a9SHari Bathini 	int i, j;
9842df173d9SMahesh Salgaonkar 
9852df173d9SMahesh Salgaonkar 	fadump_init_elfcore_header(bufp);
9862df173d9SMahesh Salgaonkar 	elf = (struct elfhdr *)bufp;
9872df173d9SMahesh Salgaonkar 	bufp += sizeof(struct elfhdr);
9882df173d9SMahesh Salgaonkar 
989ebaeb5aeSMahesh Salgaonkar 	/*
990ebaeb5aeSMahesh Salgaonkar 	 * setup ELF PT_NOTE, place holder for cpu notes info. The notes info
991ebaeb5aeSMahesh Salgaonkar 	 * will be populated during second kernel boot after crash. Hence
992ebaeb5aeSMahesh Salgaonkar 	 * this PT_NOTE will always be the first elf note.
993ebaeb5aeSMahesh Salgaonkar 	 *
994ebaeb5aeSMahesh Salgaonkar 	 * NOTE: Any new ELF note addition should be placed after this note.
995ebaeb5aeSMahesh Salgaonkar 	 */
996ebaeb5aeSMahesh Salgaonkar 	phdr = (struct elf_phdr *)bufp;
997ebaeb5aeSMahesh Salgaonkar 	bufp += sizeof(struct elf_phdr);
998ebaeb5aeSMahesh Salgaonkar 	phdr->p_type = PT_NOTE;
999ebaeb5aeSMahesh Salgaonkar 	phdr->p_flags = 0;
1000ebaeb5aeSMahesh Salgaonkar 	phdr->p_vaddr = 0;
1001ebaeb5aeSMahesh Salgaonkar 	phdr->p_align = 0;
1002ebaeb5aeSMahesh Salgaonkar 
1003ebaeb5aeSMahesh Salgaonkar 	phdr->p_offset = 0;
1004ebaeb5aeSMahesh Salgaonkar 	phdr->p_paddr = 0;
1005ebaeb5aeSMahesh Salgaonkar 	phdr->p_filesz = 0;
1006ebaeb5aeSMahesh Salgaonkar 	phdr->p_memsz = 0;
1007ebaeb5aeSMahesh Salgaonkar 
1008ebaeb5aeSMahesh Salgaonkar 	(elf->e_phnum)++;
1009ebaeb5aeSMahesh Salgaonkar 
1010d34c5f26SMahesh Salgaonkar 	/* setup ELF PT_NOTE for vmcoreinfo */
1011d34c5f26SMahesh Salgaonkar 	phdr = (struct elf_phdr *)bufp;
1012d34c5f26SMahesh Salgaonkar 	bufp += sizeof(struct elf_phdr);
1013d34c5f26SMahesh Salgaonkar 	phdr->p_type	= PT_NOTE;
1014d34c5f26SMahesh Salgaonkar 	phdr->p_flags	= 0;
1015d34c5f26SMahesh Salgaonkar 	phdr->p_vaddr	= 0;
1016d34c5f26SMahesh Salgaonkar 	phdr->p_align	= 0;
1017d34c5f26SMahesh Salgaonkar 
1018d34c5f26SMahesh Salgaonkar 	phdr->p_paddr	= fadump_relocate(paddr_vmcoreinfo_note());
1019d34c5f26SMahesh Salgaonkar 	phdr->p_offset	= phdr->p_paddr;
10205203f499SXunlei Pang 	phdr->p_memsz	= phdr->p_filesz = VMCOREINFO_NOTE_SIZE;
1021d34c5f26SMahesh Salgaonkar 
1022d34c5f26SMahesh Salgaonkar 	/* Increment number of program headers. */
1023d34c5f26SMahesh Salgaonkar 	(elf->e_phnum)++;
1024d34c5f26SMahesh Salgaonkar 
10252df173d9SMahesh Salgaonkar 	/* setup PT_LOAD sections. */
10267dee93a9SHari Bathini 	j = 0;
10277dee93a9SHari Bathini 	offset = 0;
10287dee93a9SHari Bathini 	raddr = fw_dump.boot_mem_addr[0];
1029e4fc48fbSHari Bathini 	for (i = 0; i < crash_mrange_info.mem_range_cnt; i++) {
1030e4fc48fbSHari Bathini 		u64 mbase, msize;
10312df173d9SMahesh Salgaonkar 
1032e4fc48fbSHari Bathini 		mbase = crash_mrange_info.mem_ranges[i].base;
1033e4fc48fbSHari Bathini 		msize = crash_mrange_info.mem_ranges[i].size;
10342df173d9SMahesh Salgaonkar 		if (!msize)
10352df173d9SMahesh Salgaonkar 			continue;
10362df173d9SMahesh Salgaonkar 
10372df173d9SMahesh Salgaonkar 		phdr = (struct elf_phdr *)bufp;
10382df173d9SMahesh Salgaonkar 		bufp += sizeof(struct elf_phdr);
10392df173d9SMahesh Salgaonkar 		phdr->p_type	= PT_LOAD;
10402df173d9SMahesh Salgaonkar 		phdr->p_flags	= PF_R|PF_W|PF_X;
10412df173d9SMahesh Salgaonkar 		phdr->p_offset	= mbase;
10422df173d9SMahesh Salgaonkar 
10437dee93a9SHari Bathini 		if (mbase == raddr) {
10442df173d9SMahesh Salgaonkar 			/*
1045becd91d9SHari Bathini 			 * The entire real memory region will be moved by
1046becd91d9SHari Bathini 			 * firmware to the specified destination_address.
1047becd91d9SHari Bathini 			 * Hence set the correct offset.
10482df173d9SMahesh Salgaonkar 			 */
10497dee93a9SHari Bathini 			phdr->p_offset = fw_dump.boot_mem_dest_addr + offset;
10507dee93a9SHari Bathini 			if (j < (fw_dump.boot_mem_regs_cnt - 1)) {
10517dee93a9SHari Bathini 				offset += fw_dump.boot_mem_sz[j];
10527dee93a9SHari Bathini 				raddr = fw_dump.boot_mem_addr[++j];
10537dee93a9SHari Bathini 			}
10542df173d9SMahesh Salgaonkar 		}
10552df173d9SMahesh Salgaonkar 
10562df173d9SMahesh Salgaonkar 		phdr->p_paddr = mbase;
10572df173d9SMahesh Salgaonkar 		phdr->p_vaddr = (unsigned long)__va(mbase);
10582df173d9SMahesh Salgaonkar 		phdr->p_filesz = msize;
10592df173d9SMahesh Salgaonkar 		phdr->p_memsz = msize;
10602df173d9SMahesh Salgaonkar 		phdr->p_align = 0;
10612df173d9SMahesh Salgaonkar 
10622df173d9SMahesh Salgaonkar 		/* Increment number of program headers. */
10632df173d9SMahesh Salgaonkar 		(elf->e_phnum)++;
10642df173d9SMahesh Salgaonkar 	}
10652df173d9SMahesh Salgaonkar 	return 0;
10662df173d9SMahesh Salgaonkar }
10672df173d9SMahesh Salgaonkar 
10682df173d9SMahesh Salgaonkar static unsigned long init_fadump_header(unsigned long addr)
10692df173d9SMahesh Salgaonkar {
10702df173d9SMahesh Salgaonkar 	struct fadump_crash_info_header *fdh;
10712df173d9SMahesh Salgaonkar 
10722df173d9SMahesh Salgaonkar 	if (!addr)
10732df173d9SMahesh Salgaonkar 		return 0;
10742df173d9SMahesh Salgaonkar 
10752df173d9SMahesh Salgaonkar 	fdh = __va(addr);
10762df173d9SMahesh Salgaonkar 	addr += sizeof(struct fadump_crash_info_header);
10772df173d9SMahesh Salgaonkar 
10782df173d9SMahesh Salgaonkar 	memset(fdh, 0, sizeof(struct fadump_crash_info_header));
10792df173d9SMahesh Salgaonkar 	fdh->magic_number = FADUMP_CRASH_INFO_MAGIC;
10802df173d9SMahesh Salgaonkar 	fdh->elfcorehdr_addr = addr;
1081ebaeb5aeSMahesh Salgaonkar 	/* We will set the crashing cpu id in crash_fadump() during crash. */
10820226e552SHari Bathini 	fdh->crashing_cpu = FADUMP_CPU_UNKNOWN;
10832df173d9SMahesh Salgaonkar 
10842df173d9SMahesh Salgaonkar 	return addr;
10852df173d9SMahesh Salgaonkar }
10862df173d9SMahesh Salgaonkar 
108798b8cd7fSMichal Suchanek static int register_fadump(void)
10883ccc00a7SMahesh Salgaonkar {
10892df173d9SMahesh Salgaonkar 	unsigned long addr;
10902df173d9SMahesh Salgaonkar 	void *vaddr;
10911bd6a1c4SHari Bathini 	int ret;
10922df173d9SMahesh Salgaonkar 
10933ccc00a7SMahesh Salgaonkar 	/*
10943ccc00a7SMahesh Salgaonkar 	 * If no memory is reserved then we can not register for firmware-
10953ccc00a7SMahesh Salgaonkar 	 * assisted dump.
10963ccc00a7SMahesh Salgaonkar 	 */
10973ccc00a7SMahesh Salgaonkar 	if (!fw_dump.reserve_dump_area_size)
109898b8cd7fSMichal Suchanek 		return -ENODEV;
10993ccc00a7SMahesh Salgaonkar 
11001bd6a1c4SHari Bathini 	ret = fadump_setup_crash_memory_ranges();
11011bd6a1c4SHari Bathini 	if (ret)
11021bd6a1c4SHari Bathini 		return ret;
11032df173d9SMahesh Salgaonkar 
110441a65d16SHari Bathini 	addr = fw_dump.fadumphdr_addr;
110541a65d16SHari Bathini 
11062df173d9SMahesh Salgaonkar 	/* Initialize fadump crash info header. */
11072df173d9SMahesh Salgaonkar 	addr = init_fadump_header(addr);
11082df173d9SMahesh Salgaonkar 	vaddr = __va(addr);
11092df173d9SMahesh Salgaonkar 
11102df173d9SMahesh Salgaonkar 	pr_debug("Creating ELF core headers at %#016lx\n", addr);
11112df173d9SMahesh Salgaonkar 	fadump_create_elfcore_headers(vaddr);
11122df173d9SMahesh Salgaonkar 
11133ccc00a7SMahesh Salgaonkar 	/* register the future kernel dump with firmware. */
111441a65d16SHari Bathini 	pr_debug("Registering for firmware-assisted kernel dump...\n");
111541a65d16SHari Bathini 	return fw_dump.ops->fadump_register(&fw_dump);
11163ccc00a7SMahesh Salgaonkar }
11173ccc00a7SMahesh Salgaonkar 
1118b500afffSMahesh Salgaonkar void fadump_cleanup(void)
1119b500afffSMahesh Salgaonkar {
11202790d01dSHari Bathini 	if (!fw_dump.fadump_supported)
11212790d01dSHari Bathini 		return;
11222790d01dSHari Bathini 
1123b500afffSMahesh Salgaonkar 	/* Invalidate the registration only if dump is active. */
1124b500afffSMahesh Salgaonkar 	if (fw_dump.dump_active) {
1125f3512011SHari Bathini 		pr_debug("Invalidating firmware-assisted dump registration\n");
1126f3512011SHari Bathini 		fw_dump.ops->fadump_invalidate(&fw_dump);
1127722cde76SMahesh Salgaonkar 	} else if (fw_dump.dump_registered) {
1128722cde76SMahesh Salgaonkar 		/* Un-register Firmware-assisted dump if it was registered. */
112941a65d16SHari Bathini 		fw_dump.ops->fadump_unregister(&fw_dump);
1130e4fc48fbSHari Bathini 		fadump_free_mem_ranges(&crash_mrange_info);
1131b500afffSMahesh Salgaonkar 	}
11322790d01dSHari Bathini 
11332790d01dSHari Bathini 	if (fw_dump.ops->fadump_cleanup)
11342790d01dSHari Bathini 		fw_dump.ops->fadump_cleanup(&fw_dump);
1135b500afffSMahesh Salgaonkar }
1136b500afffSMahesh Salgaonkar 
113768fa6478SHari Bathini static void fadump_free_reserved_memory(unsigned long start_pfn,
113868fa6478SHari Bathini 					unsigned long end_pfn)
113968fa6478SHari Bathini {
114068fa6478SHari Bathini 	unsigned long pfn;
114168fa6478SHari Bathini 	unsigned long time_limit = jiffies + HZ;
114268fa6478SHari Bathini 
114368fa6478SHari Bathini 	pr_info("freeing reserved memory (0x%llx - 0x%llx)\n",
114468fa6478SHari Bathini 		PFN_PHYS(start_pfn), PFN_PHYS(end_pfn));
114568fa6478SHari Bathini 
114668fa6478SHari Bathini 	for (pfn = start_pfn; pfn < end_pfn; pfn++) {
114768fa6478SHari Bathini 		free_reserved_page(pfn_to_page(pfn));
114868fa6478SHari Bathini 
114968fa6478SHari Bathini 		if (time_after(jiffies, time_limit)) {
115068fa6478SHari Bathini 			cond_resched();
115168fa6478SHari Bathini 			time_limit = jiffies + HZ;
115268fa6478SHari Bathini 		}
115368fa6478SHari Bathini 	}
115468fa6478SHari Bathini }
115568fa6478SHari Bathini 
115668fa6478SHari Bathini /*
115768fa6478SHari Bathini  * Skip memory holes and free memory that was actually reserved.
115868fa6478SHari Bathini  */
1159dda9dbfeSHari Bathini static void fadump_release_reserved_area(u64 start, u64 end)
116068fa6478SHari Bathini {
1161dda9dbfeSHari Bathini 	u64 tstart, tend, spfn, epfn;
116268fa6478SHari Bathini 	struct memblock_region *reg;
116368fa6478SHari Bathini 
1164dda9dbfeSHari Bathini 	spfn = PHYS_PFN(start);
1165dda9dbfeSHari Bathini 	epfn = PHYS_PFN(end);
116668fa6478SHari Bathini 	for_each_memblock(memory, reg) {
1167dda9dbfeSHari Bathini 		tstart = max_t(u64, spfn, memblock_region_memory_base_pfn(reg));
1168dda9dbfeSHari Bathini 		tend   = min_t(u64, epfn, memblock_region_memory_end_pfn(reg));
116968fa6478SHari Bathini 		if (tstart < tend) {
117068fa6478SHari Bathini 			fadump_free_reserved_memory(tstart, tend);
117168fa6478SHari Bathini 
1172dda9dbfeSHari Bathini 			if (tend == epfn)
117368fa6478SHari Bathini 				break;
117468fa6478SHari Bathini 
1175dda9dbfeSHari Bathini 			spfn = tend;
117668fa6478SHari Bathini 		}
117768fa6478SHari Bathini 	}
117868fa6478SHari Bathini }
117968fa6478SHari Bathini 
1180b500afffSMahesh Salgaonkar /*
1181dda9dbfeSHari Bathini  * Sort the mem ranges in-place and merge adjacent ranges
1182dda9dbfeSHari Bathini  * to minimize the memory ranges count.
1183b500afffSMahesh Salgaonkar  */
1184dda9dbfeSHari Bathini static void sort_and_merge_mem_ranges(struct fadump_mrange_info *mrange_info)
1185b500afffSMahesh Salgaonkar {
1186dda9dbfeSHari Bathini 	struct fadump_memory_range *mem_ranges;
1187dda9dbfeSHari Bathini 	struct fadump_memory_range tmp_range;
1188dda9dbfeSHari Bathini 	u64 base, size;
1189dda9dbfeSHari Bathini 	int i, j, idx;
1190dda9dbfeSHari Bathini 
1191dda9dbfeSHari Bathini 	if (!reserved_mrange_info.mem_range_cnt)
1192dda9dbfeSHari Bathini 		return;
1193dda9dbfeSHari Bathini 
1194dda9dbfeSHari Bathini 	/* Sort the memory ranges */
1195dda9dbfeSHari Bathini 	mem_ranges = mrange_info->mem_ranges;
1196dda9dbfeSHari Bathini 	for (i = 0; i < mrange_info->mem_range_cnt; i++) {
1197dda9dbfeSHari Bathini 		idx = i;
1198dda9dbfeSHari Bathini 		for (j = (i + 1); j < mrange_info->mem_range_cnt; j++) {
1199dda9dbfeSHari Bathini 			if (mem_ranges[idx].base > mem_ranges[j].base)
1200dda9dbfeSHari Bathini 				idx = j;
1201dda9dbfeSHari Bathini 		}
1202dda9dbfeSHari Bathini 		if (idx != i) {
1203dda9dbfeSHari Bathini 			tmp_range = mem_ranges[idx];
1204dda9dbfeSHari Bathini 			mem_ranges[idx] = mem_ranges[i];
1205dda9dbfeSHari Bathini 			mem_ranges[i] = tmp_range;
1206dda9dbfeSHari Bathini 		}
1207dda9dbfeSHari Bathini 	}
1208dda9dbfeSHari Bathini 
1209dda9dbfeSHari Bathini 	/* Merge adjacent reserved ranges */
1210dda9dbfeSHari Bathini 	idx = 0;
1211dda9dbfeSHari Bathini 	for (i = 1; i < mrange_info->mem_range_cnt; i++) {
1212dda9dbfeSHari Bathini 		base = mem_ranges[i-1].base;
1213dda9dbfeSHari Bathini 		size = mem_ranges[i-1].size;
1214dda9dbfeSHari Bathini 		if (mem_ranges[i].base == (base + size))
1215dda9dbfeSHari Bathini 			mem_ranges[idx].size += mem_ranges[i].size;
1216dda9dbfeSHari Bathini 		else {
1217dda9dbfeSHari Bathini 			idx++;
1218dda9dbfeSHari Bathini 			if (i == idx)
1219dda9dbfeSHari Bathini 				continue;
1220dda9dbfeSHari Bathini 
1221dda9dbfeSHari Bathini 			mem_ranges[idx] = mem_ranges[i];
1222dda9dbfeSHari Bathini 		}
1223dda9dbfeSHari Bathini 	}
1224dda9dbfeSHari Bathini 	mrange_info->mem_range_cnt = idx + 1;
1225dda9dbfeSHari Bathini }
1226dda9dbfeSHari Bathini 
1227dda9dbfeSHari Bathini /*
1228dda9dbfeSHari Bathini  * Scan reserved-ranges to consider them while reserving/releasing
1229dda9dbfeSHari Bathini  * memory for FADump.
1230dda9dbfeSHari Bathini  */
1231*02c04e37SHari Bathini static void __init early_init_dt_scan_reserved_ranges(unsigned long node)
1232dda9dbfeSHari Bathini {
1233dda9dbfeSHari Bathini 	const __be32 *prop;
1234dda9dbfeSHari Bathini 	int len, ret = -1;
1235dda9dbfeSHari Bathini 	unsigned long i;
1236dda9dbfeSHari Bathini 
1237*02c04e37SHari Bathini 	/* reserved-ranges already scanned */
1238*02c04e37SHari Bathini 	if (reserved_mrange_info.mem_range_cnt != 0)
1239*02c04e37SHari Bathini 		return;
1240dda9dbfeSHari Bathini 
1241*02c04e37SHari Bathini 	prop = of_get_flat_dt_prop(node, "reserved-ranges", &len);
1242dda9dbfeSHari Bathini 	if (!prop)
1243*02c04e37SHari Bathini 		return;
1244dda9dbfeSHari Bathini 
1245dda9dbfeSHari Bathini 	/*
1246dda9dbfeSHari Bathini 	 * Each reserved range is an (address,size) pair, 2 cells each,
1247dda9dbfeSHari Bathini 	 * totalling 4 cells per range.
1248dda9dbfeSHari Bathini 	 */
1249dda9dbfeSHari Bathini 	for (i = 0; i < len / (sizeof(*prop) * 4); i++) {
1250dda9dbfeSHari Bathini 		u64 base, size;
1251dda9dbfeSHari Bathini 
1252dda9dbfeSHari Bathini 		base = of_read_number(prop + (i * 4) + 0, 2);
1253dda9dbfeSHari Bathini 		size = of_read_number(prop + (i * 4) + 2, 2);
1254dda9dbfeSHari Bathini 
1255dda9dbfeSHari Bathini 		if (size) {
1256dda9dbfeSHari Bathini 			ret = fadump_add_mem_range(&reserved_mrange_info,
1257dda9dbfeSHari Bathini 						   base, base + size);
1258dda9dbfeSHari Bathini 			if (ret < 0) {
1259dda9dbfeSHari Bathini 				pr_warn("some reserved ranges are ignored!\n");
1260dda9dbfeSHari Bathini 				break;
1261dda9dbfeSHari Bathini 			}
1262dda9dbfeSHari Bathini 		}
1263dda9dbfeSHari Bathini 	}
1264dda9dbfeSHari Bathini 
1265*02c04e37SHari Bathini 	/* Compact reserved ranges */
1266*02c04e37SHari Bathini 	sort_and_merge_mem_ranges(&reserved_mrange_info);
1267dda9dbfeSHari Bathini }
1268dda9dbfeSHari Bathini 
1269dda9dbfeSHari Bathini /*
1270dda9dbfeSHari Bathini  * Release the memory that was reserved during early boot to preserve the
1271dda9dbfeSHari Bathini  * crash'ed kernel's memory contents except reserved dump area (permanent
1272dda9dbfeSHari Bathini  * reservation) and reserved ranges used by F/W. The released memory will
1273dda9dbfeSHari Bathini  * be available for general use.
1274dda9dbfeSHari Bathini  */
1275dda9dbfeSHari Bathini static void fadump_release_memory(u64 begin, u64 end)
1276dda9dbfeSHari Bathini {
1277dda9dbfeSHari Bathini 	u64 ra_start, ra_end, tstart;
1278dda9dbfeSHari Bathini 	int i, ret;
1279dda9dbfeSHari Bathini 
1280b500afffSMahesh Salgaonkar 	ra_start = fw_dump.reserve_dump_area_start;
1281b500afffSMahesh Salgaonkar 	ra_end = ra_start + fw_dump.reserve_dump_area_size;
1282b500afffSMahesh Salgaonkar 
1283b500afffSMahesh Salgaonkar 	/*
1284*02c04e37SHari Bathini 	 * If reserved ranges array limit is hit, overwrite the last reserved
1285*02c04e37SHari Bathini 	 * memory range with reserved dump area to ensure it is excluded from
1286*02c04e37SHari Bathini 	 * the memory being released (reused for next FADump registration).
1287dda9dbfeSHari Bathini 	 */
1288*02c04e37SHari Bathini 	if (reserved_mrange_info.mem_range_cnt ==
1289*02c04e37SHari Bathini 	    reserved_mrange_info.max_mem_ranges)
1290*02c04e37SHari Bathini 		reserved_mrange_info.mem_range_cnt--;
1291dda9dbfeSHari Bathini 
1292*02c04e37SHari Bathini 	ret = fadump_add_mem_range(&reserved_mrange_info, ra_start, ra_end);
1293*02c04e37SHari Bathini 	if (ret != 0)
1294dda9dbfeSHari Bathini 		return;
1295dda9dbfeSHari Bathini 
1296dda9dbfeSHari Bathini 	/* Get the reserved ranges list in order first. */
1297dda9dbfeSHari Bathini 	sort_and_merge_mem_ranges(&reserved_mrange_info);
1298dda9dbfeSHari Bathini 
1299dda9dbfeSHari Bathini 	/* Exclude reserved ranges and release remaining memory */
1300dda9dbfeSHari Bathini 	tstart = begin;
1301dda9dbfeSHari Bathini 	for (i = 0; i < reserved_mrange_info.mem_range_cnt; i++) {
1302dda9dbfeSHari Bathini 		ra_start = reserved_mrange_info.mem_ranges[i].base;
1303dda9dbfeSHari Bathini 		ra_end = ra_start + reserved_mrange_info.mem_ranges[i].size;
1304dda9dbfeSHari Bathini 
1305dda9dbfeSHari Bathini 		if (tstart >= ra_end)
1306dda9dbfeSHari Bathini 			continue;
1307dda9dbfeSHari Bathini 
1308dda9dbfeSHari Bathini 		if (tstart < ra_start)
1309dda9dbfeSHari Bathini 			fadump_release_reserved_area(tstart, ra_start);
1310dda9dbfeSHari Bathini 		tstart = ra_end;
1311dda9dbfeSHari Bathini 	}
1312dda9dbfeSHari Bathini 
1313dda9dbfeSHari Bathini 	if (tstart < end)
1314dda9dbfeSHari Bathini 		fadump_release_reserved_area(tstart, end);
1315b500afffSMahesh Salgaonkar }
1316b500afffSMahesh Salgaonkar 
1317b500afffSMahesh Salgaonkar static void fadump_invalidate_release_mem(void)
1318b500afffSMahesh Salgaonkar {
1319b500afffSMahesh Salgaonkar 	mutex_lock(&fadump_mutex);
1320b500afffSMahesh Salgaonkar 	if (!fw_dump.dump_active) {
1321b500afffSMahesh Salgaonkar 		mutex_unlock(&fadump_mutex);
1322b500afffSMahesh Salgaonkar 		return;
1323b500afffSMahesh Salgaonkar 	}
1324b500afffSMahesh Salgaonkar 
1325b500afffSMahesh Salgaonkar 	fadump_cleanup();
1326b500afffSMahesh Salgaonkar 	mutex_unlock(&fadump_mutex);
1327b500afffSMahesh Salgaonkar 
13287dee93a9SHari Bathini 	fadump_release_memory(fw_dump.boot_mem_top, memblock_end_of_DRAM());
1329961cf26aSHari Bathini 	fadump_free_cpu_notes_buf();
1330961cf26aSHari Bathini 
1331a4e2e2caSHari Bathini 	/*
1332a4e2e2caSHari Bathini 	 * Setup kernel metadata and initialize the kernel dump
1333a4e2e2caSHari Bathini 	 * memory structure for FADump re-registration.
1334a4e2e2caSHari Bathini 	 */
1335a4e2e2caSHari Bathini 	if (fw_dump.ops->fadump_setup_metadata &&
1336a4e2e2caSHari Bathini 	    (fw_dump.ops->fadump_setup_metadata(&fw_dump) < 0))
1337a4e2e2caSHari Bathini 		pr_warn("Failed to setup kernel metadata!\n");
133841a65d16SHari Bathini 	fw_dump.ops->fadump_init_mem_struct(&fw_dump);
1339b500afffSMahesh Salgaonkar }
1340b500afffSMahesh Salgaonkar 
1341d418b19fSSourabh Jain static ssize_t release_mem_store(struct kobject *kobj,
1342b500afffSMahesh Salgaonkar 				 struct kobj_attribute *attr,
1343b500afffSMahesh Salgaonkar 				 const char *buf, size_t count)
1344b500afffSMahesh Salgaonkar {
1345dcdc4679SMichal Suchanek 	int input = -1;
1346dcdc4679SMichal Suchanek 
1347b500afffSMahesh Salgaonkar 	if (!fw_dump.dump_active)
1348b500afffSMahesh Salgaonkar 		return -EPERM;
1349b500afffSMahesh Salgaonkar 
1350dcdc4679SMichal Suchanek 	if (kstrtoint(buf, 0, &input))
1351dcdc4679SMichal Suchanek 		return -EINVAL;
1352dcdc4679SMichal Suchanek 
1353dcdc4679SMichal Suchanek 	if (input == 1) {
1354b500afffSMahesh Salgaonkar 		/*
1355b500afffSMahesh Salgaonkar 		 * Take away the '/proc/vmcore'. We are releasing the dump
1356b500afffSMahesh Salgaonkar 		 * memory, hence it will not be valid anymore.
1357b500afffSMahesh Salgaonkar 		 */
13582685f826SMichael Ellerman #ifdef CONFIG_PROC_VMCORE
1359b500afffSMahesh Salgaonkar 		vmcore_cleanup();
13602685f826SMichael Ellerman #endif
1361b500afffSMahesh Salgaonkar 		fadump_invalidate_release_mem();
1362b500afffSMahesh Salgaonkar 
1363b500afffSMahesh Salgaonkar 	} else
1364b500afffSMahesh Salgaonkar 		return -EINVAL;
1365b500afffSMahesh Salgaonkar 	return count;
1366b500afffSMahesh Salgaonkar }
1367b500afffSMahesh Salgaonkar 
1368d418b19fSSourabh Jain /* Release the reserved memory and disable the FADump */
1369d418b19fSSourabh Jain static void unregister_fadump(void)
1370d418b19fSSourabh Jain {
1371d418b19fSSourabh Jain 	fadump_cleanup();
1372d418b19fSSourabh Jain 	fadump_release_memory(fw_dump.reserve_dump_area_start,
1373d418b19fSSourabh Jain 			      fw_dump.reserve_dump_area_size);
1374d418b19fSSourabh Jain 	fw_dump.fadump_enabled = 0;
1375d418b19fSSourabh Jain 	kobject_put(fadump_kobj);
1376d418b19fSSourabh Jain }
1377d418b19fSSourabh Jain 
1378d418b19fSSourabh Jain static ssize_t enabled_show(struct kobject *kobj,
13793ccc00a7SMahesh Salgaonkar 			    struct kobj_attribute *attr,
13803ccc00a7SMahesh Salgaonkar 			    char *buf)
13813ccc00a7SMahesh Salgaonkar {
13823ccc00a7SMahesh Salgaonkar 	return sprintf(buf, "%d\n", fw_dump.fadump_enabled);
13833ccc00a7SMahesh Salgaonkar }
13843ccc00a7SMahesh Salgaonkar 
1385d8e73458SSourabh Jain static ssize_t mem_reserved_show(struct kobject *kobj,
1386d8e73458SSourabh Jain 				 struct kobj_attribute *attr,
1387d8e73458SSourabh Jain 				 char *buf)
1388d8e73458SSourabh Jain {
1389d8e73458SSourabh Jain 	return sprintf(buf, "%ld\n", fw_dump.reserve_dump_area_size);
1390d8e73458SSourabh Jain }
1391d8e73458SSourabh Jain 
1392d418b19fSSourabh Jain static ssize_t registered_show(struct kobject *kobj,
13933ccc00a7SMahesh Salgaonkar 			       struct kobj_attribute *attr,
13943ccc00a7SMahesh Salgaonkar 			       char *buf)
13953ccc00a7SMahesh Salgaonkar {
13963ccc00a7SMahesh Salgaonkar 	return sprintf(buf, "%d\n", fw_dump.dump_registered);
13973ccc00a7SMahesh Salgaonkar }
13983ccc00a7SMahesh Salgaonkar 
1399d418b19fSSourabh Jain static ssize_t registered_store(struct kobject *kobj,
14003ccc00a7SMahesh Salgaonkar 				struct kobj_attribute *attr,
14013ccc00a7SMahesh Salgaonkar 				const char *buf, size_t count)
14023ccc00a7SMahesh Salgaonkar {
14033ccc00a7SMahesh Salgaonkar 	int ret = 0;
1404dcdc4679SMichal Suchanek 	int input = -1;
14053ccc00a7SMahesh Salgaonkar 
1406f3512011SHari Bathini 	if (!fw_dump.fadump_enabled || fw_dump.dump_active)
14073ccc00a7SMahesh Salgaonkar 		return -EPERM;
14083ccc00a7SMahesh Salgaonkar 
1409dcdc4679SMichal Suchanek 	if (kstrtoint(buf, 0, &input))
1410dcdc4679SMichal Suchanek 		return -EINVAL;
1411dcdc4679SMichal Suchanek 
14123ccc00a7SMahesh Salgaonkar 	mutex_lock(&fadump_mutex);
14133ccc00a7SMahesh Salgaonkar 
1414dcdc4679SMichal Suchanek 	switch (input) {
1415dcdc4679SMichal Suchanek 	case 0:
14163ccc00a7SMahesh Salgaonkar 		if (fw_dump.dump_registered == 0) {
14173ccc00a7SMahesh Salgaonkar 			goto unlock_out;
14183ccc00a7SMahesh Salgaonkar 		}
1419f3512011SHari Bathini 
14203ccc00a7SMahesh Salgaonkar 		/* Un-register Firmware-assisted dump */
142141a65d16SHari Bathini 		pr_debug("Un-register firmware-assisted dump\n");
142241a65d16SHari Bathini 		fw_dump.ops->fadump_unregister(&fw_dump);
14233ccc00a7SMahesh Salgaonkar 		break;
1424dcdc4679SMichal Suchanek 	case 1:
14253ccc00a7SMahesh Salgaonkar 		if (fw_dump.dump_registered == 1) {
14260823c68bSHari Bathini 			/* Un-register Firmware-assisted dump */
142741a65d16SHari Bathini 			fw_dump.ops->fadump_unregister(&fw_dump);
14283ccc00a7SMahesh Salgaonkar 		}
14293ccc00a7SMahesh Salgaonkar 		/* Register Firmware-assisted dump */
143098b8cd7fSMichal Suchanek 		ret = register_fadump();
14313ccc00a7SMahesh Salgaonkar 		break;
14323ccc00a7SMahesh Salgaonkar 	default:
14333ccc00a7SMahesh Salgaonkar 		ret = -EINVAL;
14343ccc00a7SMahesh Salgaonkar 		break;
14353ccc00a7SMahesh Salgaonkar 	}
14363ccc00a7SMahesh Salgaonkar 
14373ccc00a7SMahesh Salgaonkar unlock_out:
14383ccc00a7SMahesh Salgaonkar 	mutex_unlock(&fadump_mutex);
14393ccc00a7SMahesh Salgaonkar 	return ret < 0 ? ret : count;
14403ccc00a7SMahesh Salgaonkar }
14413ccc00a7SMahesh Salgaonkar 
14423ccc00a7SMahesh Salgaonkar static int fadump_region_show(struct seq_file *m, void *private)
14433ccc00a7SMahesh Salgaonkar {
14443ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_enabled)
14453ccc00a7SMahesh Salgaonkar 		return 0;
14463ccc00a7SMahesh Salgaonkar 
1447b500afffSMahesh Salgaonkar 	mutex_lock(&fadump_mutex);
144841a65d16SHari Bathini 	fw_dump.ops->fadump_region_show(&fw_dump, m);
1449b500afffSMahesh Salgaonkar 	mutex_unlock(&fadump_mutex);
14503ccc00a7SMahesh Salgaonkar 	return 0;
14513ccc00a7SMahesh Salgaonkar }
14523ccc00a7SMahesh Salgaonkar 
1453d418b19fSSourabh Jain static struct kobj_attribute release_attr = __ATTR_WO(release_mem);
1454d418b19fSSourabh Jain static struct kobj_attribute enable_attr = __ATTR_RO(enabled);
1455d418b19fSSourabh Jain static struct kobj_attribute register_attr = __ATTR_RW(registered);
1456d8e73458SSourabh Jain static struct kobj_attribute mem_reserved_attr = __ATTR_RO(mem_reserved);
1457d418b19fSSourabh Jain 
1458d418b19fSSourabh Jain static struct attribute *fadump_attrs[] = {
1459d418b19fSSourabh Jain 	&enable_attr.attr,
1460d418b19fSSourabh Jain 	&register_attr.attr,
1461d8e73458SSourabh Jain 	&mem_reserved_attr.attr,
1462d418b19fSSourabh Jain 	NULL,
1463d418b19fSSourabh Jain };
1464d418b19fSSourabh Jain 
1465d418b19fSSourabh Jain ATTRIBUTE_GROUPS(fadump);
14663ccc00a7SMahesh Salgaonkar 
1467f6cee260SYangtao Li DEFINE_SHOW_ATTRIBUTE(fadump_region);
14683ccc00a7SMahesh Salgaonkar 
14693ccc00a7SMahesh Salgaonkar static void fadump_init_files(void)
14703ccc00a7SMahesh Salgaonkar {
14713ccc00a7SMahesh Salgaonkar 	int rc = 0;
14723ccc00a7SMahesh Salgaonkar 
1473d418b19fSSourabh Jain 	fadump_kobj = kobject_create_and_add("fadump", kernel_kobj);
1474d418b19fSSourabh Jain 	if (!fadump_kobj) {
1475d418b19fSSourabh Jain 		pr_err("failed to create fadump kobject\n");
1476d418b19fSSourabh Jain 		return;
1477d418b19fSSourabh Jain 	}
1478860286cfSGreg Kroah-Hartman 
1479860286cfSGreg Kroah-Hartman 	debugfs_create_file("fadump_region", 0444, powerpc_debugfs_root, NULL,
14803ccc00a7SMahesh Salgaonkar 			    &fadump_region_fops);
1481b500afffSMahesh Salgaonkar 
1482b500afffSMahesh Salgaonkar 	if (fw_dump.dump_active) {
1483d418b19fSSourabh Jain 		rc = sysfs_create_file(fadump_kobj, &release_attr.attr);
1484b500afffSMahesh Salgaonkar 		if (rc)
1485d418b19fSSourabh Jain 			pr_err("unable to create release_mem sysfs file (%d)\n",
1486d418b19fSSourabh Jain 			       rc);
1487d418b19fSSourabh Jain 	}
1488d418b19fSSourabh Jain 
1489d418b19fSSourabh Jain 	rc = sysfs_create_groups(fadump_kobj, fadump_groups);
1490d418b19fSSourabh Jain 	if (rc) {
1491d418b19fSSourabh Jain 		pr_err("sysfs group creation failed (%d), unregistering FADump",
1492d418b19fSSourabh Jain 		       rc);
1493d418b19fSSourabh Jain 		unregister_fadump();
1494d418b19fSSourabh Jain 		return;
1495d418b19fSSourabh Jain 	}
1496d418b19fSSourabh Jain 
1497d418b19fSSourabh Jain 	/*
1498d418b19fSSourabh Jain 	 * The FADump sysfs are moved from kernel_kobj to fadump_kobj need to
1499d418b19fSSourabh Jain 	 * create symlink at old location to maintain backward compatibility.
1500d418b19fSSourabh Jain 	 *
1501d418b19fSSourabh Jain 	 *      - fadump_enabled -> fadump/enabled
1502d418b19fSSourabh Jain 	 *      - fadump_registered -> fadump/registered
1503d418b19fSSourabh Jain 	 *      - fadump_release_mem -> fadump/release_mem
1504d418b19fSSourabh Jain 	 */
1505d418b19fSSourabh Jain 	rc = compat_only_sysfs_link_entry_to_kobj(kernel_kobj, fadump_kobj,
1506d418b19fSSourabh Jain 						  "enabled", "fadump_enabled");
1507d418b19fSSourabh Jain 	if (rc) {
1508d418b19fSSourabh Jain 		pr_err("unable to create fadump_enabled symlink (%d)", rc);
1509d418b19fSSourabh Jain 		return;
1510d418b19fSSourabh Jain 	}
1511d418b19fSSourabh Jain 
1512d418b19fSSourabh Jain 	rc = compat_only_sysfs_link_entry_to_kobj(kernel_kobj, fadump_kobj,
1513d418b19fSSourabh Jain 						  "registered",
1514d418b19fSSourabh Jain 						  "fadump_registered");
1515d418b19fSSourabh Jain 	if (rc) {
1516d418b19fSSourabh Jain 		pr_err("unable to create fadump_registered symlink (%d)", rc);
1517d418b19fSSourabh Jain 		sysfs_remove_link(kernel_kobj, "fadump_enabled");
1518d418b19fSSourabh Jain 		return;
1519d418b19fSSourabh Jain 	}
1520d418b19fSSourabh Jain 
1521d418b19fSSourabh Jain 	if (fw_dump.dump_active) {
1522d418b19fSSourabh Jain 		rc = compat_only_sysfs_link_entry_to_kobj(kernel_kobj,
1523d418b19fSSourabh Jain 							  fadump_kobj,
1524d418b19fSSourabh Jain 							  "release_mem",
1525d418b19fSSourabh Jain 							  "fadump_release_mem");
1526d418b19fSSourabh Jain 		if (rc)
1527d418b19fSSourabh Jain 			pr_err("unable to create fadump_release_mem symlink (%d)",
1528d418b19fSSourabh Jain 			       rc);
1529b500afffSMahesh Salgaonkar 	}
15303ccc00a7SMahesh Salgaonkar 	return;
15313ccc00a7SMahesh Salgaonkar }
15323ccc00a7SMahesh Salgaonkar 
15333ccc00a7SMahesh Salgaonkar /*
15343ccc00a7SMahesh Salgaonkar  * Prepare for firmware-assisted dump.
15353ccc00a7SMahesh Salgaonkar  */
15363ccc00a7SMahesh Salgaonkar int __init setup_fadump(void)
15373ccc00a7SMahesh Salgaonkar {
1538565f9bc0SMichal Suchanek 	if (!fw_dump.fadump_supported)
15393ccc00a7SMahesh Salgaonkar 		return 0;
15403ccc00a7SMahesh Salgaonkar 
1541565f9bc0SMichal Suchanek 	fadump_init_files();
15423ccc00a7SMahesh Salgaonkar 	fadump_show_config();
1543565f9bc0SMichal Suchanek 
1544565f9bc0SMichal Suchanek 	if (!fw_dump.fadump_enabled)
1545565f9bc0SMichal Suchanek 		return 1;
1546565f9bc0SMichal Suchanek 
15472df173d9SMahesh Salgaonkar 	/*
15482df173d9SMahesh Salgaonkar 	 * If dump data is available then see if it is valid and prepare for
15492df173d9SMahesh Salgaonkar 	 * saving it to the disk.
15502df173d9SMahesh Salgaonkar 	 */
1551b500afffSMahesh Salgaonkar 	if (fw_dump.dump_active) {
1552b500afffSMahesh Salgaonkar 		/*
1553b500afffSMahesh Salgaonkar 		 * if dump process fails then invalidate the registration
1554b500afffSMahesh Salgaonkar 		 * and release memory before proceeding for re-registration.
1555b500afffSMahesh Salgaonkar 		 */
1556f3512011SHari Bathini 		if (fw_dump.ops->fadump_process(&fw_dump) < 0)
1557b500afffSMahesh Salgaonkar 			fadump_invalidate_release_mem();
1558b500afffSMahesh Salgaonkar 	}
15593ccc00a7SMahesh Salgaonkar 	/* Initialize the kernel dump memory structure for FAD registration. */
15602df173d9SMahesh Salgaonkar 	else if (fw_dump.reserve_dump_area_size)
156141a65d16SHari Bathini 		fw_dump.ops->fadump_init_mem_struct(&fw_dump);
1562f3512011SHari Bathini 
15633ccc00a7SMahesh Salgaonkar 	return 1;
15643ccc00a7SMahesh Salgaonkar }
15653ccc00a7SMahesh Salgaonkar subsys_initcall(setup_fadump);
1566bec53196SHari Bathini #else /* !CONFIG_PRESERVE_FA_DUMP */
1567bec53196SHari Bathini 
1568bec53196SHari Bathini /* Scan the Firmware Assisted dump configuration details. */
1569bec53196SHari Bathini int __init early_init_dt_scan_fw_dump(unsigned long node, const char *uname,
1570bec53196SHari Bathini 				      int depth, void *data)
1571bec53196SHari Bathini {
1572bec53196SHari Bathini 	if ((depth != 1) || (strcmp(uname, "ibm,opal") != 0))
1573bec53196SHari Bathini 		return 0;
1574bec53196SHari Bathini 
1575bec53196SHari Bathini 	opal_fadump_dt_scan(&fw_dump, node);
1576bec53196SHari Bathini 	return 1;
1577bec53196SHari Bathini }
1578bec53196SHari Bathini 
1579bec53196SHari Bathini /*
1580bec53196SHari Bathini  * When dump is active but PRESERVE_FA_DUMP is enabled on the kernel,
1581bec53196SHari Bathini  * preserve crash data. The subsequent memory preserving kernel boot
1582bec53196SHari Bathini  * is likely to process this crash data.
1583bec53196SHari Bathini  */
1584bec53196SHari Bathini int __init fadump_reserve_mem(void)
1585bec53196SHari Bathini {
1586bec53196SHari Bathini 	if (fw_dump.dump_active) {
1587bec53196SHari Bathini 		/*
1588bec53196SHari Bathini 		 * If last boot has crashed then reserve all the memory
1589bec53196SHari Bathini 		 * above boot memory to preserve crash data.
1590bec53196SHari Bathini 		 */
1591bec53196SHari Bathini 		pr_info("Preserving crash data for processing in next boot.\n");
1592bec53196SHari Bathini 		fadump_reserve_crash_area(fw_dump.boot_mem_top);
1593bec53196SHari Bathini 	} else
1594bec53196SHari Bathini 		pr_debug("FADump-aware kernel..\n");
1595bec53196SHari Bathini 
1596bec53196SHari Bathini 	return 1;
1597bec53196SHari Bathini }
1598bec53196SHari Bathini #endif /* CONFIG_PRESERVE_FA_DUMP */
1599b2a815a5SHari Bathini 
1600b2a815a5SHari Bathini /* Preserve everything above the base address */
1601b2a815a5SHari Bathini static void __init fadump_reserve_crash_area(u64 base)
1602b2a815a5SHari Bathini {
1603b2a815a5SHari Bathini 	struct memblock_region *reg;
1604b2a815a5SHari Bathini 	u64 mstart, msize;
1605b2a815a5SHari Bathini 
1606b2a815a5SHari Bathini 	for_each_memblock(memory, reg) {
1607b2a815a5SHari Bathini 		mstart = reg->base;
1608b2a815a5SHari Bathini 		msize  = reg->size;
1609b2a815a5SHari Bathini 
1610b2a815a5SHari Bathini 		if ((mstart + msize) < base)
1611b2a815a5SHari Bathini 			continue;
1612b2a815a5SHari Bathini 
1613b2a815a5SHari Bathini 		if (mstart < base) {
1614b2a815a5SHari Bathini 			msize -= (base - mstart);
1615b2a815a5SHari Bathini 			mstart = base;
1616b2a815a5SHari Bathini 		}
1617b2a815a5SHari Bathini 
1618b2a815a5SHari Bathini 		pr_info("Reserving %lluMB of memory at %#016llx for preserving crash data",
1619b2a815a5SHari Bathini 			(msize >> 20), mstart);
1620b2a815a5SHari Bathini 		memblock_reserve(mstart, msize);
1621b2a815a5SHari Bathini 	}
1622b2a815a5SHari Bathini }
1623bec53196SHari Bathini 
1624bec53196SHari Bathini unsigned long __init arch_reserved_kernel_pages(void)
1625bec53196SHari Bathini {
1626bec53196SHari Bathini 	return memblock_reserved_size() / PAGE_SIZE;
1627bec53196SHari Bathini }
1628