xref: /openbmc/linux/arch/powerpc/kernel/fadump.c (revision 20776319)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2eb39c880SMahesh Salgaonkar /*
3eb39c880SMahesh Salgaonkar  * Firmware Assisted dump: A robust mechanism to get reliable kernel crash
4eb39c880SMahesh Salgaonkar  * dump with assistance from firmware. This approach does not use kexec,
5eb39c880SMahesh Salgaonkar  * instead firmware assists in booting the kdump kernel while preserving
6eb39c880SMahesh Salgaonkar  * memory contents. The most of the code implementation has been adapted
7eb39c880SMahesh Salgaonkar  * from phyp assisted dump implementation written by Linas Vepstas and
8eb39c880SMahesh Salgaonkar  * Manish Ahuja
9eb39c880SMahesh Salgaonkar  *
10eb39c880SMahesh Salgaonkar  * Copyright 2011 IBM Corporation
11eb39c880SMahesh Salgaonkar  * Author: Mahesh Salgaonkar <mahesh@linux.vnet.ibm.com>
12eb39c880SMahesh Salgaonkar  */
13eb39c880SMahesh Salgaonkar 
14eb39c880SMahesh Salgaonkar #undef DEBUG
15eb39c880SMahesh Salgaonkar #define pr_fmt(fmt) "fadump: " fmt
16eb39c880SMahesh Salgaonkar 
17eb39c880SMahesh Salgaonkar #include <linux/string.h>
18eb39c880SMahesh Salgaonkar #include <linux/memblock.h>
193ccc00a7SMahesh Salgaonkar #include <linux/delay.h>
203ccc00a7SMahesh Salgaonkar #include <linux/seq_file.h>
212df173d9SMahesh Salgaonkar #include <linux/crash_dump.h>
22b500afffSMahesh Salgaonkar #include <linux/kobject.h>
23b500afffSMahesh Salgaonkar #include <linux/sysfs.h>
24a5818313SHari Bathini #include <linux/slab.h>
25a4e92ce8SMahesh Salgaonkar #include <linux/cma.h>
2645d0ba52SChristophe Leroy #include <linux/hugetlb.h>
27dbf77fedSAneesh Kumar K.V #include <linux/debugfs.h>
28eb39c880SMahesh Salgaonkar 
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>
34cbd3d5baSMichael Ellerman #include <asm/interrupt.h>
35eb39c880SMahesh Salgaonkar 
36ba608c4fSSourabh Jain /*
37ba608c4fSSourabh Jain  * The CPU who acquired the lock to trigger the fadump crash should
38ba608c4fSSourabh Jain  * wait for other CPUs to enter.
39ba608c4fSSourabh Jain  *
40ba608c4fSSourabh Jain  * The timeout is in milliseconds.
41ba608c4fSSourabh Jain  */
42ba608c4fSSourabh Jain #define CRASH_TIMEOUT		500
43ba608c4fSSourabh Jain 
44eb39c880SMahesh Salgaonkar static struct fw_dump fw_dump;
453ccc00a7SMahesh Salgaonkar 
46b2a815a5SHari Bathini static void __init fadump_reserve_crash_area(u64 base);
47b2a815a5SHari Bathini 
48bec53196SHari Bathini #ifndef CONFIG_PRESERVE_FA_DUMP
495f987caeSMichael Ellerman 
502e341f56SMichael Ellerman static struct kobject *fadump_kobj;
512e341f56SMichael Ellerman 
525f987caeSMichael Ellerman static atomic_t cpus_in_fadump;
533ccc00a7SMahesh Salgaonkar static DEFINE_MUTEX(fadump_mutex);
545f987caeSMichael Ellerman 
552e341f56SMichael Ellerman static struct fadump_mrange_info crash_mrange_info = { "crash", NULL, 0, 0, 0, false };
5602c04e37SHari Bathini 
5702c04e37SHari Bathini #define RESERVED_RNGS_SZ	16384 /* 16K - 128 entries */
5802c04e37SHari Bathini #define RESERVED_RNGS_CNT	(RESERVED_RNGS_SZ / \
5902c04e37SHari Bathini 				 sizeof(struct fadump_memory_range))
6002c04e37SHari Bathini static struct fadump_memory_range rngs[RESERVED_RNGS_CNT];
612e341f56SMichael Ellerman static struct fadump_mrange_info
622e341f56SMichael Ellerman reserved_mrange_info = { "reserved", rngs, RESERVED_RNGS_SZ, 0, 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.
76887f56a0SRandy Dunlap  * The remaining portion of fadump reserved memory will be not given
77887f56a0SRandy Dunlap  * to CMA and pages for those 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  */
822e341f56SMichael Ellerman static 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 	/*
116ee97347fSHari Bathini 	 *  If CMA activation fails, keep the pages reserved, instead of
117ee97347fSHari Bathini 	 *  exposing them to buddy allocator. Same as 'fadump=nocma' case.
118ee97347fSHari Bathini 	 */
119ee97347fSHari Bathini 	cma_reserve_pages_on_error(fadump_cma);
120ee97347fSHari Bathini 
121ee97347fSHari Bathini 	/*
122a4e92ce8SMahesh Salgaonkar 	 * So we now have successfully initialized cma area for fadump.
123a4e92ce8SMahesh Salgaonkar 	 */
124a4e92ce8SMahesh Salgaonkar 	pr_info("Initialized 0x%lx bytes cma area at %ldMB from 0x%lx "
125a4e92ce8SMahesh Salgaonkar 		"bytes of memory reserved for firmware-assisted dump\n",
126a4e92ce8SMahesh Salgaonkar 		cma_get_size(fadump_cma),
127a4e92ce8SMahesh Salgaonkar 		(unsigned long)cma_get_base(fadump_cma) >> 20,
128a4e92ce8SMahesh Salgaonkar 		fw_dump.reserve_dump_area_size);
129a4e92ce8SMahesh Salgaonkar 	return 1;
130a4e92ce8SMahesh Salgaonkar }
131a4e92ce8SMahesh Salgaonkar #else
132a4e92ce8SMahesh Salgaonkar static int __init fadump_cma_init(void) { return 1; }
133a4e92ce8SMahesh Salgaonkar #endif /* CONFIG_CMA */
134a4e92ce8SMahesh Salgaonkar 
135eb39c880SMahesh Salgaonkar /* Scan the Firmware Assisted dump configuration details. */
136f3512011SHari Bathini int __init early_init_dt_scan_fw_dump(unsigned long node, const char *uname,
137f3512011SHari Bathini 				      int depth, void *data)
138eb39c880SMahesh Salgaonkar {
13902c04e37SHari Bathini 	if (depth == 0) {
14002c04e37SHari Bathini 		early_init_dt_scan_reserved_ranges(node);
14102c04e37SHari Bathini 		return 0;
14202c04e37SHari Bathini 	}
14302c04e37SHari Bathini 
14441df5928SHari Bathini 	if (depth != 1)
145eb39c880SMahesh Salgaonkar 		return 0;
146eb39c880SMahesh Salgaonkar 
14741df5928SHari Bathini 	if (strcmp(uname, "rtas") == 0) {
148d3833a70SHari Bathini 		rtas_fadump_dt_scan(&fw_dump, node);
149eb39c880SMahesh Salgaonkar 		return 1;
150eb39c880SMahesh Salgaonkar 	}
151eb39c880SMahesh Salgaonkar 
15241df5928SHari Bathini 	if (strcmp(uname, "ibm,opal") == 0) {
15341df5928SHari Bathini 		opal_fadump_dt_scan(&fw_dump, node);
15441df5928SHari Bathini 		return 1;
15541df5928SHari Bathini 	}
15641df5928SHari Bathini 
15741df5928SHari Bathini 	return 0;
15841df5928SHari Bathini }
15941df5928SHari Bathini 
160eae0dfccSHari Bathini /*
161eae0dfccSHari Bathini  * If fadump is registered, check if the memory provided
1620db6896fSMahesh Salgaonkar  * falls within boot memory area and reserved memory area.
163eae0dfccSHari Bathini  */
164becd91d9SHari Bathini int is_fadump_memory_area(u64 addr, unsigned long size)
165eae0dfccSHari Bathini {
166becd91d9SHari Bathini 	u64 d_start, d_end;
1670db6896fSMahesh Salgaonkar 
168eae0dfccSHari Bathini 	if (!fw_dump.dump_registered)
169eae0dfccSHari Bathini 		return 0;
170eae0dfccSHari Bathini 
171becd91d9SHari Bathini 	if (!size)
172becd91d9SHari Bathini 		return 0;
173becd91d9SHari Bathini 
174becd91d9SHari Bathini 	d_start = fw_dump.reserve_dump_area_start;
175becd91d9SHari Bathini 	d_end = d_start + fw_dump.reserve_dump_area_size;
1760db6896fSMahesh Salgaonkar 	if (((addr + size) > d_start) && (addr <= d_end))
1770db6896fSMahesh Salgaonkar 		return 1;
1780db6896fSMahesh Salgaonkar 
1797dee93a9SHari Bathini 	return (addr <= fw_dump.boot_mem_top);
180eae0dfccSHari Bathini }
181eae0dfccSHari Bathini 
1826fcd6baaSNicholas Piggin int should_fadump_crash(void)
1836fcd6baaSNicholas Piggin {
1846fcd6baaSNicholas Piggin 	if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr)
1856fcd6baaSNicholas Piggin 		return 0;
1866fcd6baaSNicholas Piggin 	return 1;
1876fcd6baaSNicholas Piggin }
1886fcd6baaSNicholas Piggin 
1893ccc00a7SMahesh Salgaonkar int is_fadump_active(void)
1903ccc00a7SMahesh Salgaonkar {
1913ccc00a7SMahesh Salgaonkar 	return fw_dump.dump_active;
1923ccc00a7SMahesh Salgaonkar }
1933ccc00a7SMahesh Salgaonkar 
194a5a05b91SHari Bathini /*
195961cf26aSHari Bathini  * Returns true, if there are no holes in memory area between d_start to d_end,
196961cf26aSHari Bathini  * false otherwise.
197a5a05b91SHari Bathini  */
198961cf26aSHari Bathini static bool is_fadump_mem_area_contiguous(u64 d_start, u64 d_end)
199a5a05b91SHari Bathini {
200b10d6bcaSMike Rapoport 	phys_addr_t reg_start, reg_end;
201961cf26aSHari Bathini 	bool ret = false;
202b10d6bcaSMike Rapoport 	u64 i, start, end;
203a5a05b91SHari Bathini 
204b10d6bcaSMike Rapoport 	for_each_mem_range(i, &reg_start, &reg_end) {
205b10d6bcaSMike Rapoport 		start = max_t(u64, d_start, reg_start);
206b10d6bcaSMike Rapoport 		end = min_t(u64, d_end, reg_end);
207961cf26aSHari Bathini 		if (d_start < end) {
208961cf26aSHari Bathini 			/* Memory hole from d_start to start */
209961cf26aSHari Bathini 			if (start > d_start)
210a5a05b91SHari Bathini 				break;
211a5a05b91SHari Bathini 
212961cf26aSHari Bathini 			if (end == d_end) {
213961cf26aSHari Bathini 				ret = true;
214a5a05b91SHari Bathini 				break;
215a5a05b91SHari Bathini 			}
216a5a05b91SHari Bathini 
217961cf26aSHari Bathini 			d_start = end + 1;
218a5a05b91SHari Bathini 		}
219a5a05b91SHari Bathini 	}
220a5a05b91SHari Bathini 
221a5a05b91SHari Bathini 	return ret;
222a5a05b91SHari Bathini }
223a5a05b91SHari Bathini 
224f86593beSMahesh Salgaonkar /*
225961cf26aSHari Bathini  * Returns true, if there are no holes in boot memory area,
226961cf26aSHari Bathini  * false otherwise.
227961cf26aSHari Bathini  */
2287f0ad11dSHari Bathini bool is_fadump_boot_mem_contiguous(void)
229961cf26aSHari Bathini {
2307dee93a9SHari Bathini 	unsigned long d_start, d_end;
2317dee93a9SHari Bathini 	bool ret = false;
2327dee93a9SHari Bathini 	int i;
2337dee93a9SHari Bathini 
2347dee93a9SHari Bathini 	for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) {
2357dee93a9SHari Bathini 		d_start = fw_dump.boot_mem_addr[i];
2367dee93a9SHari Bathini 		d_end   = d_start + fw_dump.boot_mem_sz[i];
2377dee93a9SHari Bathini 
2387dee93a9SHari Bathini 		ret = is_fadump_mem_area_contiguous(d_start, d_end);
2397dee93a9SHari Bathini 		if (!ret)
2407dee93a9SHari Bathini 			break;
2417dee93a9SHari Bathini 	}
2427dee93a9SHari Bathini 
2437dee93a9SHari Bathini 	return ret;
244961cf26aSHari Bathini }
245961cf26aSHari Bathini 
246961cf26aSHari Bathini /*
247f86593beSMahesh Salgaonkar  * Returns true, if there are no holes in reserved memory area,
248f86593beSMahesh Salgaonkar  * false otherwise.
249f86593beSMahesh Salgaonkar  */
2507f0ad11dSHari Bathini bool is_fadump_reserved_mem_contiguous(void)
251f86593beSMahesh Salgaonkar {
252961cf26aSHari Bathini 	u64 d_start, d_end;
253f86593beSMahesh Salgaonkar 
254961cf26aSHari Bathini 	d_start	= fw_dump.reserve_dump_area_start;
255961cf26aSHari Bathini 	d_end	= d_start + fw_dump.reserve_dump_area_size;
256961cf26aSHari Bathini 	return is_fadump_mem_area_contiguous(d_start, d_end);
257f86593beSMahesh Salgaonkar }
258f86593beSMahesh Salgaonkar 
2593ccc00a7SMahesh Salgaonkar /* Print firmware assisted dump configurations for debugging purpose. */
260d276960dSNick Child static void __init fadump_show_config(void)
2613ccc00a7SMahesh Salgaonkar {
2627dee93a9SHari Bathini 	int i;
2637dee93a9SHari Bathini 
2643ccc00a7SMahesh Salgaonkar 	pr_debug("Support for firmware-assisted dump (fadump): %s\n",
2653ccc00a7SMahesh Salgaonkar 			(fw_dump.fadump_supported ? "present" : "no support"));
2663ccc00a7SMahesh Salgaonkar 
2673ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_supported)
2683ccc00a7SMahesh Salgaonkar 		return;
2693ccc00a7SMahesh Salgaonkar 
2703ccc00a7SMahesh Salgaonkar 	pr_debug("Fadump enabled    : %s\n",
2713ccc00a7SMahesh Salgaonkar 				(fw_dump.fadump_enabled ? "yes" : "no"));
2723ccc00a7SMahesh Salgaonkar 	pr_debug("Dump Active       : %s\n",
2733ccc00a7SMahesh Salgaonkar 				(fw_dump.dump_active ? "yes" : "no"));
2743ccc00a7SMahesh Salgaonkar 	pr_debug("Dump section sizes:\n");
2753ccc00a7SMahesh Salgaonkar 	pr_debug("    CPU state data size: %lx\n", fw_dump.cpu_state_data_size);
2763ccc00a7SMahesh Salgaonkar 	pr_debug("    HPTE region size   : %lx\n", fw_dump.hpte_region_size);
2773ccc00a7SMahesh Salgaonkar 	pr_debug("    Boot memory size   : %lx\n", fw_dump.boot_memory_size);
2787dee93a9SHari Bathini 	pr_debug("    Boot memory top    : %llx\n", fw_dump.boot_mem_top);
2797dee93a9SHari Bathini 	pr_debug("Boot memory regions cnt: %llx\n", fw_dump.boot_mem_regs_cnt);
2807dee93a9SHari Bathini 	for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) {
2817dee93a9SHari Bathini 		pr_debug("[%03d] base = %llx, size = %llx\n", i,
2827dee93a9SHari Bathini 			 fw_dump.boot_mem_addr[i], fw_dump.boot_mem_sz[i]);
2837dee93a9SHari Bathini 	}
2843ccc00a7SMahesh Salgaonkar }
2853ccc00a7SMahesh Salgaonkar 
286eb39c880SMahesh Salgaonkar /**
287eb39c880SMahesh Salgaonkar  * fadump_calculate_reserve_size(): reserve variable boot area 5% of System RAM
288eb39c880SMahesh Salgaonkar  *
289eb39c880SMahesh Salgaonkar  * Function to find the largest memory size we need to reserve during early
290eb39c880SMahesh Salgaonkar  * boot process. This will be the size of the memory that is required for a
291eb39c880SMahesh Salgaonkar  * kernel to boot successfully.
292eb39c880SMahesh Salgaonkar  *
293eb39c880SMahesh Salgaonkar  * This function has been taken from phyp-assisted dump feature implementation.
294eb39c880SMahesh Salgaonkar  *
295eb39c880SMahesh Salgaonkar  * returns larger of 256MB or 5% rounded down to multiples of 256MB.
296eb39c880SMahesh Salgaonkar  *
297eb39c880SMahesh Salgaonkar  * TODO: Come up with better approach to find out more accurate memory size
298eb39c880SMahesh Salgaonkar  * that is required for a kernel to boot successfully.
299eb39c880SMahesh Salgaonkar  *
300eb39c880SMahesh Salgaonkar  */
301fbced154SNathan Chancellor static __init u64 fadump_calculate_reserve_size(void)
302eb39c880SMahesh Salgaonkar {
3037b1b3b48SHari Bathini 	u64 base, size, bootmem_min;
30411550dc0SHari Bathini 	int ret;
305eb39c880SMahesh Salgaonkar 
30681d9eca5SHari Bathini 	if (fw_dump.reserve_bootvar)
30781d9eca5SHari Bathini 		pr_warn("'fadump_reserve_mem=' parameter is deprecated in favor of 'crashkernel=' parameter.\n");
30881d9eca5SHari Bathini 
309eb39c880SMahesh Salgaonkar 	/*
31011550dc0SHari Bathini 	 * Check if the size is specified through crashkernel= cmdline
311e7467dc6SHari Bathini 	 * option. If yes, then use that but ignore base as fadump reserves
312e7467dc6SHari Bathini 	 * memory at a predefined offset.
313eb39c880SMahesh Salgaonkar 	 */
31411550dc0SHari Bathini 	ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
31511550dc0SHari Bathini 				&size, &base);
31611550dc0SHari Bathini 	if (ret == 0 && size > 0) {
31748a316e3SHari Bathini 		unsigned long max_size;
31848a316e3SHari Bathini 
31981d9eca5SHari Bathini 		if (fw_dump.reserve_bootvar)
32081d9eca5SHari Bathini 			pr_info("Using 'crashkernel=' parameter for memory reservation.\n");
32181d9eca5SHari Bathini 
32211550dc0SHari Bathini 		fw_dump.reserve_bootvar = (unsigned long)size;
32348a316e3SHari Bathini 
32448a316e3SHari Bathini 		/*
32548a316e3SHari Bathini 		 * Adjust if the boot memory size specified is above
32648a316e3SHari Bathini 		 * the upper limit.
32748a316e3SHari Bathini 		 */
32848a316e3SHari Bathini 		max_size = memblock_phys_mem_size() / MAX_BOOT_MEM_RATIO;
32948a316e3SHari Bathini 		if (fw_dump.reserve_bootvar > max_size) {
33048a316e3SHari Bathini 			fw_dump.reserve_bootvar = max_size;
33148a316e3SHari Bathini 			pr_info("Adjusted boot memory size to %luMB\n",
33248a316e3SHari Bathini 				(fw_dump.reserve_bootvar >> 20));
33348a316e3SHari Bathini 		}
33448a316e3SHari Bathini 
335eb39c880SMahesh Salgaonkar 		return fw_dump.reserve_bootvar;
33681d9eca5SHari Bathini 	} else if (fw_dump.reserve_bootvar) {
33781d9eca5SHari Bathini 		/*
33881d9eca5SHari Bathini 		 * 'fadump_reserve_mem=' is being used to reserve memory
33981d9eca5SHari Bathini 		 * for firmware-assisted dump.
34081d9eca5SHari Bathini 		 */
34181d9eca5SHari Bathini 		return fw_dump.reserve_bootvar;
34211550dc0SHari Bathini 	}
343eb39c880SMahesh Salgaonkar 
344eb39c880SMahesh Salgaonkar 	/* divide by 20 to get 5% of value */
34548a316e3SHari Bathini 	size = memblock_phys_mem_size() / 20;
346eb39c880SMahesh Salgaonkar 
347eb39c880SMahesh Salgaonkar 	/* round it down in multiples of 256 */
348eb39c880SMahesh Salgaonkar 	size = size & ~0x0FFFFFFFUL;
349eb39c880SMahesh Salgaonkar 
350eb39c880SMahesh Salgaonkar 	/* Truncate to memory_limit. We don't want to over reserve the memory.*/
351eb39c880SMahesh Salgaonkar 	if (memory_limit && size > memory_limit)
352eb39c880SMahesh Salgaonkar 		size = memory_limit;
353eb39c880SMahesh Salgaonkar 
3547b1b3b48SHari Bathini 	bootmem_min = fw_dump.ops->fadump_get_bootmem_min();
3557b1b3b48SHari Bathini 	return (size > bootmem_min ? size : bootmem_min);
356eb39c880SMahesh Salgaonkar }
357eb39c880SMahesh Salgaonkar 
358eb39c880SMahesh Salgaonkar /*
359eb39c880SMahesh Salgaonkar  * Calculate the total memory size required to be reserved for
360eb39c880SMahesh Salgaonkar  * firmware-assisted dump registration.
361eb39c880SMahesh Salgaonkar  */
362d276960dSNick Child static unsigned long __init get_fadump_area_size(void)
363eb39c880SMahesh Salgaonkar {
364eb39c880SMahesh Salgaonkar 	unsigned long size = 0;
365eb39c880SMahesh Salgaonkar 
366eb39c880SMahesh Salgaonkar 	size += fw_dump.cpu_state_data_size;
367eb39c880SMahesh Salgaonkar 	size += fw_dump.hpte_region_size;
3689cf3b3a3SHari Bathini 	/*
3699cf3b3a3SHari Bathini 	 * Account for pagesize alignment of boot memory area destination address.
3709cf3b3a3SHari Bathini 	 * This faciliates in mmap reading of first kernel's memory.
3719cf3b3a3SHari Bathini 	 */
3729cf3b3a3SHari Bathini 	size = PAGE_ALIGN(size);
373eb39c880SMahesh Salgaonkar 	size += fw_dump.boot_memory_size;
3742df173d9SMahesh Salgaonkar 	size += sizeof(struct fadump_crash_info_header);
3752df173d9SMahesh Salgaonkar 	size += sizeof(struct elfhdr); /* ELF core header.*/
376ebaeb5aeSMahesh Salgaonkar 	size += sizeof(struct elf_phdr); /* place holder for cpu notes */
3772df173d9SMahesh Salgaonkar 	/* Program headers for crash memory regions. */
3782df173d9SMahesh Salgaonkar 	size += sizeof(struct elf_phdr) * (memblock_num_regions(memory) + 2);
379eb39c880SMahesh Salgaonkar 
380eb39c880SMahesh Salgaonkar 	size = PAGE_ALIGN(size);
381742a265aSHari Bathini 
382742a265aSHari Bathini 	/* This is to hold kernel metadata on platforms that support it */
383742a265aSHari Bathini 	size += (fw_dump.ops->fadump_get_metadata_size ?
384742a265aSHari Bathini 		 fw_dump.ops->fadump_get_metadata_size() : 0);
385eb39c880SMahesh Salgaonkar 	return size;
386eb39c880SMahesh Salgaonkar }
387eb39c880SMahesh Salgaonkar 
3887dee93a9SHari Bathini static int __init add_boot_mem_region(unsigned long rstart,
3897dee93a9SHari Bathini 				      unsigned long rsize)
3907dee93a9SHari Bathini {
3917dee93a9SHari Bathini 	int i = fw_dump.boot_mem_regs_cnt++;
3927dee93a9SHari Bathini 
3937dee93a9SHari Bathini 	if (fw_dump.boot_mem_regs_cnt > FADUMP_MAX_MEM_REGS) {
3947dee93a9SHari Bathini 		fw_dump.boot_mem_regs_cnt = FADUMP_MAX_MEM_REGS;
3957dee93a9SHari Bathini 		return 0;
3967dee93a9SHari Bathini 	}
3977dee93a9SHari Bathini 
3987dee93a9SHari Bathini 	pr_debug("Added boot memory range[%d] [%#016lx-%#016lx)\n",
3997dee93a9SHari Bathini 		 i, rstart, (rstart + rsize));
4007dee93a9SHari Bathini 	fw_dump.boot_mem_addr[i] = rstart;
4017dee93a9SHari Bathini 	fw_dump.boot_mem_sz[i] = rsize;
4027dee93a9SHari Bathini 	return 1;
4037dee93a9SHari Bathini }
4047dee93a9SHari Bathini 
4057dee93a9SHari Bathini /*
4067dee93a9SHari Bathini  * Firmware usually has a hard limit on the data it can copy per region.
4077dee93a9SHari Bathini  * Honour that by splitting a memory range into multiple regions.
4087dee93a9SHari Bathini  */
4097dee93a9SHari Bathini static int __init add_boot_mem_regions(unsigned long mstart,
4107dee93a9SHari Bathini 				       unsigned long msize)
4117dee93a9SHari Bathini {
4127dee93a9SHari Bathini 	unsigned long rstart, rsize, max_size;
4137dee93a9SHari Bathini 	int ret = 1;
4147dee93a9SHari Bathini 
4157dee93a9SHari Bathini 	rstart = mstart;
4167dee93a9SHari Bathini 	max_size = fw_dump.max_copy_size ? fw_dump.max_copy_size : msize;
4177dee93a9SHari Bathini 	while (msize) {
4187dee93a9SHari Bathini 		if (msize > max_size)
4197dee93a9SHari Bathini 			rsize = max_size;
4207dee93a9SHari Bathini 		else
4217dee93a9SHari Bathini 			rsize = msize;
4227dee93a9SHari Bathini 
4237dee93a9SHari Bathini 		ret = add_boot_mem_region(rstart, rsize);
4247dee93a9SHari Bathini 		if (!ret)
4257dee93a9SHari Bathini 			break;
4267dee93a9SHari Bathini 
4277dee93a9SHari Bathini 		msize -= rsize;
4287dee93a9SHari Bathini 		rstart += rsize;
4297dee93a9SHari Bathini 	}
4307dee93a9SHari Bathini 
4317dee93a9SHari Bathini 	return ret;
4327dee93a9SHari Bathini }
4337dee93a9SHari Bathini 
4347dee93a9SHari Bathini static int __init fadump_get_boot_mem_regions(void)
4357dee93a9SHari Bathini {
436b10d6bcaSMike Rapoport 	unsigned long size, cur_size, hole_size, last_end;
4377dee93a9SHari Bathini 	unsigned long mem_size = fw_dump.boot_memory_size;
438b10d6bcaSMike Rapoport 	phys_addr_t reg_start, reg_end;
4397dee93a9SHari Bathini 	int ret = 1;
440b10d6bcaSMike Rapoport 	u64 i;
4417dee93a9SHari Bathini 
4427dee93a9SHari Bathini 	fw_dump.boot_mem_regs_cnt = 0;
4437dee93a9SHari Bathini 
4447dee93a9SHari Bathini 	last_end = 0;
4457dee93a9SHari Bathini 	hole_size = 0;
4467dee93a9SHari Bathini 	cur_size = 0;
447b10d6bcaSMike Rapoport 	for_each_mem_range(i, &reg_start, &reg_end) {
448b10d6bcaSMike Rapoport 		size = reg_end - reg_start;
449b10d6bcaSMike Rapoport 		hole_size += (reg_start - last_end);
4507dee93a9SHari Bathini 
4517dee93a9SHari Bathini 		if ((cur_size + size) >= mem_size) {
4527dee93a9SHari Bathini 			size = (mem_size - cur_size);
453b10d6bcaSMike Rapoport 			ret = add_boot_mem_regions(reg_start, size);
4547dee93a9SHari Bathini 			break;
4557dee93a9SHari Bathini 		}
4567dee93a9SHari Bathini 
4577dee93a9SHari Bathini 		mem_size -= size;
4587dee93a9SHari Bathini 		cur_size += size;
459b10d6bcaSMike Rapoport 		ret = add_boot_mem_regions(reg_start, size);
4607dee93a9SHari Bathini 		if (!ret)
4617dee93a9SHari Bathini 			break;
4627dee93a9SHari Bathini 
463b10d6bcaSMike Rapoport 		last_end = reg_end;
4647dee93a9SHari Bathini 	}
4657dee93a9SHari Bathini 	fw_dump.boot_mem_top = PAGE_ALIGN(fw_dump.boot_memory_size + hole_size);
4667dee93a9SHari Bathini 
4677dee93a9SHari Bathini 	return ret;
4687dee93a9SHari Bathini }
4697dee93a9SHari Bathini 
470140777a3SHari Bathini /*
471140777a3SHari Bathini  * Returns true, if the given range overlaps with reserved memory ranges
472140777a3SHari Bathini  * starting at idx. Also, updates idx to index of overlapping memory range
473140777a3SHari Bathini  * with the given memory range.
474140777a3SHari Bathini  * False, otherwise.
475140777a3SHari Bathini  */
476d276960dSNick Child static bool __init overlaps_reserved_ranges(u64 base, u64 end, int *idx)
477140777a3SHari Bathini {
478140777a3SHari Bathini 	bool ret = false;
479140777a3SHari Bathini 	int i;
480140777a3SHari Bathini 
481140777a3SHari Bathini 	for (i = *idx; i < reserved_mrange_info.mem_range_cnt; i++) {
482140777a3SHari Bathini 		u64 rbase = reserved_mrange_info.mem_ranges[i].base;
483140777a3SHari Bathini 		u64 rend = rbase + reserved_mrange_info.mem_ranges[i].size;
484140777a3SHari Bathini 
485140777a3SHari Bathini 		if (end <= rbase)
486140777a3SHari Bathini 			break;
487140777a3SHari Bathini 
488140777a3SHari Bathini 		if ((end > rbase) &&  (base < rend)) {
489140777a3SHari Bathini 			*idx = i;
490140777a3SHari Bathini 			ret = true;
491140777a3SHari Bathini 			break;
492140777a3SHari Bathini 		}
493140777a3SHari Bathini 	}
494140777a3SHari Bathini 
495140777a3SHari Bathini 	return ret;
496140777a3SHari Bathini }
497140777a3SHari Bathini 
498140777a3SHari Bathini /*
499140777a3SHari Bathini  * Locate a suitable memory area to reserve memory for FADump. While at it,
500140777a3SHari Bathini  * lookup reserved-ranges & avoid overlap with them, as they are used by F/W.
501140777a3SHari Bathini  */
502140777a3SHari Bathini static u64 __init fadump_locate_reserve_mem(u64 base, u64 size)
503140777a3SHari Bathini {
504140777a3SHari Bathini 	struct fadump_memory_range *mrngs;
505140777a3SHari Bathini 	phys_addr_t mstart, mend;
506140777a3SHari Bathini 	int idx = 0;
507140777a3SHari Bathini 	u64 i, ret = 0;
508140777a3SHari Bathini 
509140777a3SHari Bathini 	mrngs = reserved_mrange_info.mem_ranges;
510140777a3SHari Bathini 	for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE,
511140777a3SHari Bathini 				&mstart, &mend, NULL) {
512140777a3SHari Bathini 		pr_debug("%llu) mstart: %llx, mend: %llx, base: %llx\n",
513140777a3SHari Bathini 			 i, mstart, mend, base);
514140777a3SHari Bathini 
515140777a3SHari Bathini 		if (mstart > base)
516140777a3SHari Bathini 			base = PAGE_ALIGN(mstart);
517140777a3SHari Bathini 
518140777a3SHari Bathini 		while ((mend > base) && ((mend - base) >= size)) {
519140777a3SHari Bathini 			if (!overlaps_reserved_ranges(base, base+size, &idx)) {
520140777a3SHari Bathini 				ret = base;
521140777a3SHari Bathini 				goto out;
522140777a3SHari Bathini 			}
523140777a3SHari Bathini 
524140777a3SHari Bathini 			base = mrngs[idx].base + mrngs[idx].size;
525140777a3SHari Bathini 			base = PAGE_ALIGN(base);
526140777a3SHari Bathini 		}
527140777a3SHari Bathini 	}
528140777a3SHari Bathini 
529140777a3SHari Bathini out:
530140777a3SHari Bathini 	return ret;
531140777a3SHari Bathini }
532140777a3SHari Bathini 
533eb39c880SMahesh Salgaonkar int __init fadump_reserve_mem(void)
534eb39c880SMahesh Salgaonkar {
535140777a3SHari Bathini 	u64 base, size, mem_boundary, bootmem_min;
5366abec12cSHari Bathini 	int ret = 1;
537eb39c880SMahesh Salgaonkar 
538eb39c880SMahesh Salgaonkar 	if (!fw_dump.fadump_enabled)
539eb39c880SMahesh Salgaonkar 		return 0;
540eb39c880SMahesh Salgaonkar 
541eb39c880SMahesh Salgaonkar 	if (!fw_dump.fadump_supported) {
5426abec12cSHari Bathini 		pr_info("Firmware-Assisted Dump is not supported on this hardware\n");
5436abec12cSHari Bathini 		goto error_out;
544eb39c880SMahesh Salgaonkar 	}
545742a265aSHari Bathini 
5463ccc00a7SMahesh Salgaonkar 	/*
5473ccc00a7SMahesh Salgaonkar 	 * Initialize boot memory size
5483ccc00a7SMahesh Salgaonkar 	 * If dump is active then we have already calculated the size during
5493ccc00a7SMahesh Salgaonkar 	 * first kernel.
5503ccc00a7SMahesh Salgaonkar 	 */
551f3512011SHari Bathini 	if (!fw_dump.dump_active) {
5526abec12cSHari Bathini 		fw_dump.boot_memory_size =
5536abec12cSHari Bathini 			PAGE_ALIGN(fadump_calculate_reserve_size());
554a4e92ce8SMahesh Salgaonkar #ifdef CONFIG_CMA
555579ca1a2SHari Bathini 		if (!fw_dump.nocma) {
556a4e92ce8SMahesh Salgaonkar 			fw_dump.boot_memory_size =
557140777a3SHari Bathini 				ALIGN(fw_dump.boot_memory_size,
558e16faf26SDavid Hildenbrand 				      CMA_MIN_ALIGNMENT_BYTES);
559579ca1a2SHari Bathini 		}
560a4e92ce8SMahesh Salgaonkar #endif
5617b1b3b48SHari Bathini 
5627b1b3b48SHari Bathini 		bootmem_min = fw_dump.ops->fadump_get_bootmem_min();
5637b1b3b48SHari Bathini 		if (fw_dump.boot_memory_size < bootmem_min) {
5647b1b3b48SHari Bathini 			pr_err("Can't enable fadump with boot memory size (0x%lx) less than 0x%llx\n",
5657b1b3b48SHari Bathini 			       fw_dump.boot_memory_size, bootmem_min);
5667b1b3b48SHari Bathini 			goto error_out;
5677b1b3b48SHari Bathini 		}
5687dee93a9SHari Bathini 
5697dee93a9SHari Bathini 		if (!fadump_get_boot_mem_regions()) {
5707dee93a9SHari Bathini 			pr_err("Too many holes in boot memory area to enable fadump\n");
5717dee93a9SHari Bathini 			goto error_out;
5727dee93a9SHari Bathini 		}
573a4e92ce8SMahesh Salgaonkar 	}
574eb39c880SMahesh Salgaonkar 
575eb39c880SMahesh Salgaonkar 	/*
576eb39c880SMahesh Salgaonkar 	 * Calculate the memory boundary.
577eb39c880SMahesh Salgaonkar 	 * If memory_limit is less than actual memory boundary then reserve
578eb39c880SMahesh Salgaonkar 	 * the memory for fadump beyond the memory_limit and adjust the
579eb39c880SMahesh Salgaonkar 	 * memory_limit accordingly, so that the running kernel can run with
580eb39c880SMahesh Salgaonkar 	 * specified memory_limit.
581eb39c880SMahesh Salgaonkar 	 */
582eb39c880SMahesh Salgaonkar 	if (memory_limit && memory_limit < memblock_end_of_DRAM()) {
583eb39c880SMahesh Salgaonkar 		size = get_fadump_area_size();
584eb39c880SMahesh Salgaonkar 		if ((memory_limit + size) < memblock_end_of_DRAM())
585eb39c880SMahesh Salgaonkar 			memory_limit += size;
586eb39c880SMahesh Salgaonkar 		else
587eb39c880SMahesh Salgaonkar 			memory_limit = memblock_end_of_DRAM();
588eb39c880SMahesh Salgaonkar 		printk(KERN_INFO "Adjusted memory_limit for firmware-assisted"
589a84fcd46SSuzuki Poulose 				" dump, now %#016llx\n", memory_limit);
590eb39c880SMahesh Salgaonkar 	}
591eb39c880SMahesh Salgaonkar 	if (memory_limit)
5926abec12cSHari Bathini 		mem_boundary = memory_limit;
593eb39c880SMahesh Salgaonkar 	else
5946abec12cSHari Bathini 		mem_boundary = memblock_end_of_DRAM();
595eb39c880SMahesh Salgaonkar 
5967dee93a9SHari Bathini 	base = fw_dump.boot_mem_top;
5978255da95SHari Bathini 	size = get_fadump_area_size();
5988255da95SHari Bathini 	fw_dump.reserve_dump_area_size = size;
599eb39c880SMahesh Salgaonkar 	if (fw_dump.dump_active) {
600b71a693dSMahesh Salgaonkar 		pr_info("Firmware-assisted dump is active.\n");
601b71a693dSMahesh Salgaonkar 
60285975387SHari Bathini #ifdef CONFIG_HUGETLB_PAGE
60385975387SHari Bathini 		/*
60485975387SHari Bathini 		 * FADump capture kernel doesn't care much about hugepages.
60585975387SHari Bathini 		 * In fact, handling hugepages in capture kernel is asking for
60685975387SHari Bathini 		 * trouble. So, disable HugeTLB support when fadump is active.
60785975387SHari Bathini 		 */
60885975387SHari Bathini 		hugetlb_disabled = true;
60985975387SHari Bathini #endif
610eb39c880SMahesh Salgaonkar 		/*
611eb39c880SMahesh Salgaonkar 		 * If last boot has crashed then reserve all the memory
612b2a815a5SHari Bathini 		 * above boot memory size so that we don't touch it until
613eb39c880SMahesh Salgaonkar 		 * dump is written to disk by userspace tool. This memory
614b2a815a5SHari Bathini 		 * can be released for general use by invalidating fadump.
615eb39c880SMahesh Salgaonkar 		 */
616b2a815a5SHari Bathini 		fadump_reserve_crash_area(base);
6172df173d9SMahesh Salgaonkar 
618f3512011SHari Bathini 		pr_debug("fadumphdr_addr = %#016lx\n", fw_dump.fadumphdr_addr);
619f3512011SHari Bathini 		pr_debug("Reserve dump area start address: 0x%lx\n",
620f3512011SHari Bathini 			 fw_dump.reserve_dump_area_start);
6218255da95SHari Bathini 	} else {
6228255da95SHari Bathini 		/*
623f6e6bedbSHari Bathini 		 * Reserve memory at an offset closer to bottom of the RAM to
624579ca1a2SHari Bathini 		 * minimize the impact of memory hot-remove operation.
625f6e6bedbSHari Bathini 		 */
626140777a3SHari Bathini 		base = fadump_locate_reserve_mem(base, size);
627f6e6bedbSHari Bathini 
6289a2921e5SHari Bathini 		if (!base || (base + size > mem_boundary)) {
629742a265aSHari Bathini 			pr_err("Failed to find memory chunk for reservation!\n");
630742a265aSHari Bathini 			goto error_out;
631742a265aSHari Bathini 		}
632742a265aSHari Bathini 		fw_dump.reserve_dump_area_start = base;
633742a265aSHari Bathini 
634742a265aSHari Bathini 		/*
635742a265aSHari Bathini 		 * Calculate the kernel metadata address and register it with
636742a265aSHari Bathini 		 * f/w if the platform supports.
637742a265aSHari Bathini 		 */
638742a265aSHari Bathini 		if (fw_dump.ops->fadump_setup_metadata &&
639742a265aSHari Bathini 		    (fw_dump.ops->fadump_setup_metadata(&fw_dump) < 0))
640742a265aSHari Bathini 			goto error_out;
641742a265aSHari Bathini 
642742a265aSHari Bathini 		if (memblock_reserve(base, size)) {
6436abec12cSHari Bathini 			pr_err("Failed to reserve memory!\n");
6446abec12cSHari Bathini 			goto error_out;
6456abec12cSHari Bathini 		}
6466abec12cSHari Bathini 
6476abec12cSHari Bathini 		pr_info("Reserved %lldMB of memory at %#016llx (System RAM: %lldMB)\n",
6486abec12cSHari Bathini 			(size >> 20), base, (memblock_phys_mem_size() >> 20));
649f6e6bedbSHari Bathini 
6506abec12cSHari Bathini 		ret = fadump_cma_init();
651a4e92ce8SMahesh Salgaonkar 	}
6526abec12cSHari Bathini 
6536abec12cSHari Bathini 	return ret;
6546abec12cSHari Bathini error_out:
6556abec12cSHari Bathini 	fw_dump.fadump_enabled = 0;
6566abec12cSHari Bathini 	return 0;
657eb39c880SMahesh Salgaonkar }
658eb39c880SMahesh Salgaonkar 
659eb39c880SMahesh Salgaonkar /* Look for fadump= cmdline option. */
660eb39c880SMahesh Salgaonkar static int __init early_fadump_param(char *p)
661eb39c880SMahesh Salgaonkar {
662eb39c880SMahesh Salgaonkar 	if (!p)
663eb39c880SMahesh Salgaonkar 		return 1;
664eb39c880SMahesh Salgaonkar 
665eb39c880SMahesh Salgaonkar 	if (strncmp(p, "on", 2) == 0)
666eb39c880SMahesh Salgaonkar 		fw_dump.fadump_enabled = 1;
667eb39c880SMahesh Salgaonkar 	else if (strncmp(p, "off", 3) == 0)
668eb39c880SMahesh Salgaonkar 		fw_dump.fadump_enabled = 0;
669a4e92ce8SMahesh Salgaonkar 	else if (strncmp(p, "nocma", 5) == 0) {
670a4e92ce8SMahesh Salgaonkar 		fw_dump.fadump_enabled = 1;
671a4e92ce8SMahesh Salgaonkar 		fw_dump.nocma = 1;
672a4e92ce8SMahesh Salgaonkar 	}
673eb39c880SMahesh Salgaonkar 
674eb39c880SMahesh Salgaonkar 	return 0;
675eb39c880SMahesh Salgaonkar }
676eb39c880SMahesh Salgaonkar early_param("fadump", early_fadump_param);
677eb39c880SMahesh Salgaonkar 
67881d9eca5SHari Bathini /*
67981d9eca5SHari Bathini  * Look for fadump_reserve_mem= cmdline option
68081d9eca5SHari Bathini  * TODO: Remove references to 'fadump_reserve_mem=' parameter,
68181d9eca5SHari Bathini  *       the sooner 'crashkernel=' parameter is accustomed to.
68281d9eca5SHari Bathini  */
68381d9eca5SHari Bathini static int __init early_fadump_reserve_mem(char *p)
68481d9eca5SHari Bathini {
68581d9eca5SHari Bathini 	if (p)
68681d9eca5SHari Bathini 		fw_dump.reserve_bootvar = memparse(p, &p);
68781d9eca5SHari Bathini 	return 0;
68881d9eca5SHari Bathini }
68981d9eca5SHari Bathini early_param("fadump_reserve_mem", early_fadump_reserve_mem);
69081d9eca5SHari Bathini 
691ebaeb5aeSMahesh Salgaonkar void crash_fadump(struct pt_regs *regs, const char *str)
692ebaeb5aeSMahesh Salgaonkar {
693ba608c4fSSourabh Jain 	unsigned int msecs;
694ebaeb5aeSMahesh Salgaonkar 	struct fadump_crash_info_header *fdh = NULL;
695f2a5e8f0SMahesh Salgaonkar 	int old_cpu, this_cpu;
696ba608c4fSSourabh Jain 	/* Do not include first CPU */
697ba608c4fSSourabh Jain 	unsigned int ncpus = num_online_cpus() - 1;
698ebaeb5aeSMahesh Salgaonkar 
6996fcd6baaSNicholas Piggin 	if (!should_fadump_crash())
700ebaeb5aeSMahesh Salgaonkar 		return;
701ebaeb5aeSMahesh Salgaonkar 
702f2a5e8f0SMahesh Salgaonkar 	/*
703f2a5e8f0SMahesh Salgaonkar 	 * old_cpu == -1 means this is the first CPU which has come here,
704f2a5e8f0SMahesh Salgaonkar 	 * go ahead and trigger fadump.
705f2a5e8f0SMahesh Salgaonkar 	 *
706f2a5e8f0SMahesh Salgaonkar 	 * old_cpu != -1 means some other CPU has already on it's way
707f2a5e8f0SMahesh Salgaonkar 	 * to trigger fadump, just keep looping here.
708f2a5e8f0SMahesh Salgaonkar 	 */
709f2a5e8f0SMahesh Salgaonkar 	this_cpu = smp_processor_id();
710f2a5e8f0SMahesh Salgaonkar 	old_cpu = cmpxchg(&crashing_cpu, -1, this_cpu);
711f2a5e8f0SMahesh Salgaonkar 
712f2a5e8f0SMahesh Salgaonkar 	if (old_cpu != -1) {
713ba608c4fSSourabh Jain 		atomic_inc(&cpus_in_fadump);
714ba608c4fSSourabh Jain 
715f2a5e8f0SMahesh Salgaonkar 		/*
716f2a5e8f0SMahesh Salgaonkar 		 * We can't loop here indefinitely. Wait as long as fadump
717f2a5e8f0SMahesh Salgaonkar 		 * is in force. If we race with fadump un-registration this
718f2a5e8f0SMahesh Salgaonkar 		 * loop will break and then we go down to normal panic path
719f2a5e8f0SMahesh Salgaonkar 		 * and reboot. If fadump is in force the first crashing
720f2a5e8f0SMahesh Salgaonkar 		 * cpu will definitely trigger fadump.
721f2a5e8f0SMahesh Salgaonkar 		 */
722f2a5e8f0SMahesh Salgaonkar 		while (fw_dump.dump_registered)
723f2a5e8f0SMahesh Salgaonkar 			cpu_relax();
724f2a5e8f0SMahesh Salgaonkar 		return;
725f2a5e8f0SMahesh Salgaonkar 	}
726f2a5e8f0SMahesh Salgaonkar 
727ebaeb5aeSMahesh Salgaonkar 	fdh = __va(fw_dump.fadumphdr_addr);
728ebaeb5aeSMahesh Salgaonkar 	fdh->crashing_cpu = crashing_cpu;
729ebaeb5aeSMahesh Salgaonkar 	crash_save_vmcoreinfo();
730ebaeb5aeSMahesh Salgaonkar 
731ebaeb5aeSMahesh Salgaonkar 	if (regs)
732ebaeb5aeSMahesh Salgaonkar 		fdh->regs = *regs;
733ebaeb5aeSMahesh Salgaonkar 	else
734ebaeb5aeSMahesh Salgaonkar 		ppc_save_regs(&fdh->regs);
735ebaeb5aeSMahesh Salgaonkar 
7366584cec0SHari Bathini 	fdh->cpu_mask = *cpu_online_mask;
737ebaeb5aeSMahesh Salgaonkar 
738ba608c4fSSourabh Jain 	/*
739ba608c4fSSourabh Jain 	 * If we came in via system reset, wait a while for the secondary
740ba608c4fSSourabh Jain 	 * CPUs to enter.
741ba608c4fSSourabh Jain 	 */
7427153d4bfSXiongwei Song 	if (TRAP(&(fdh->regs)) == INTERRUPT_SYSTEM_RESET) {
743ba608c4fSSourabh Jain 		msecs = CRASH_TIMEOUT;
744ba608c4fSSourabh Jain 		while ((atomic_read(&cpus_in_fadump) < ncpus) && (--msecs > 0))
745ba608c4fSSourabh Jain 			mdelay(1);
746ba608c4fSSourabh Jain 	}
747ba608c4fSSourabh Jain 
74841a65d16SHari Bathini 	fw_dump.ops->fadump_trigger(fdh, str);
749ebaeb5aeSMahesh Salgaonkar }
750ebaeb5aeSMahesh Salgaonkar 
751d276960dSNick Child u32 *__init fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs)
752ebaeb5aeSMahesh Salgaonkar {
753ebaeb5aeSMahesh Salgaonkar 	struct elf_prstatus prstatus;
754ebaeb5aeSMahesh Salgaonkar 
755ebaeb5aeSMahesh Salgaonkar 	memset(&prstatus, 0, sizeof(prstatus));
756ebaeb5aeSMahesh Salgaonkar 	/*
757ebaeb5aeSMahesh Salgaonkar 	 * FIXME: How do i get PID? Do I really need it?
758ebaeb5aeSMahesh Salgaonkar 	 * prstatus.pr_pid = ????
759ebaeb5aeSMahesh Salgaonkar 	 */
760ebaeb5aeSMahesh Salgaonkar 	elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
76122bd0177SHari Bathini 	buf = append_elf_note(buf, CRASH_CORE_NOTE_NAME, NT_PRSTATUS,
762ebaeb5aeSMahesh Salgaonkar 			      &prstatus, sizeof(prstatus));
763ebaeb5aeSMahesh Salgaonkar 	return buf;
764ebaeb5aeSMahesh Salgaonkar }
765ebaeb5aeSMahesh Salgaonkar 
766d276960dSNick Child void __init fadump_update_elfcore_header(char *bufp)
767ebaeb5aeSMahesh Salgaonkar {
768ebaeb5aeSMahesh Salgaonkar 	struct elf_phdr *phdr;
769ebaeb5aeSMahesh Salgaonkar 
770ebaeb5aeSMahesh Salgaonkar 	bufp += sizeof(struct elfhdr);
771ebaeb5aeSMahesh Salgaonkar 
772ebaeb5aeSMahesh Salgaonkar 	/* First note is a place holder for cpu notes info. */
773ebaeb5aeSMahesh Salgaonkar 	phdr = (struct elf_phdr *)bufp;
774ebaeb5aeSMahesh Salgaonkar 
775ebaeb5aeSMahesh Salgaonkar 	if (phdr->p_type == PT_NOTE) {
776961cf26aSHari Bathini 		phdr->p_paddr	= __pa(fw_dump.cpu_notes_buf_vaddr);
777ebaeb5aeSMahesh Salgaonkar 		phdr->p_offset	= phdr->p_paddr;
778ebaeb5aeSMahesh Salgaonkar 		phdr->p_filesz	= fw_dump.cpu_notes_buf_size;
779ebaeb5aeSMahesh Salgaonkar 		phdr->p_memsz = fw_dump.cpu_notes_buf_size;
780ebaeb5aeSMahesh Salgaonkar 	}
781ebaeb5aeSMahesh Salgaonkar 	return;
782ebaeb5aeSMahesh Salgaonkar }
783ebaeb5aeSMahesh Salgaonkar 
784d276960dSNick Child static void *__init fadump_alloc_buffer(unsigned long size)
785ebaeb5aeSMahesh Salgaonkar {
78672aa6517SHari Bathini 	unsigned long count, i;
787ebaeb5aeSMahesh Salgaonkar 	struct page *page;
78872aa6517SHari Bathini 	void *vaddr;
789ebaeb5aeSMahesh Salgaonkar 
79072aa6517SHari Bathini 	vaddr = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
791ebaeb5aeSMahesh Salgaonkar 	if (!vaddr)
792ebaeb5aeSMahesh Salgaonkar 		return NULL;
793ebaeb5aeSMahesh Salgaonkar 
79472aa6517SHari Bathini 	count = PAGE_ALIGN(size) / PAGE_SIZE;
795ebaeb5aeSMahesh Salgaonkar 	page = virt_to_page(vaddr);
796ebaeb5aeSMahesh Salgaonkar 	for (i = 0; i < count; i++)
79772aa6517SHari Bathini 		mark_page_reserved(page + i);
798ebaeb5aeSMahesh Salgaonkar 	return vaddr;
799ebaeb5aeSMahesh Salgaonkar }
800ebaeb5aeSMahesh Salgaonkar 
801961cf26aSHari Bathini static void fadump_free_buffer(unsigned long vaddr, unsigned long size)
802ebaeb5aeSMahesh Salgaonkar {
80372aa6517SHari Bathini 	free_reserved_area((void *)vaddr, (void *)(vaddr + size), -1, NULL);
804ebaeb5aeSMahesh Salgaonkar }
805ebaeb5aeSMahesh Salgaonkar 
806d276960dSNick Child s32 __init fadump_setup_cpu_notes_buf(u32 num_cpus)
807961cf26aSHari Bathini {
808961cf26aSHari Bathini 	/* Allocate buffer to hold cpu crash notes. */
809961cf26aSHari Bathini 	fw_dump.cpu_notes_buf_size = num_cpus * sizeof(note_buf_t);
810961cf26aSHari Bathini 	fw_dump.cpu_notes_buf_size = PAGE_ALIGN(fw_dump.cpu_notes_buf_size);
811961cf26aSHari Bathini 	fw_dump.cpu_notes_buf_vaddr =
812961cf26aSHari Bathini 		(unsigned long)fadump_alloc_buffer(fw_dump.cpu_notes_buf_size);
813961cf26aSHari Bathini 	if (!fw_dump.cpu_notes_buf_vaddr) {
814961cf26aSHari Bathini 		pr_err("Failed to allocate %ld bytes for CPU notes buffer\n",
815961cf26aSHari Bathini 		       fw_dump.cpu_notes_buf_size);
816961cf26aSHari Bathini 		return -ENOMEM;
817961cf26aSHari Bathini 	}
818961cf26aSHari Bathini 
819961cf26aSHari Bathini 	pr_debug("Allocated buffer for cpu notes of size %ld at 0x%lx\n",
820961cf26aSHari Bathini 		 fw_dump.cpu_notes_buf_size,
821961cf26aSHari Bathini 		 fw_dump.cpu_notes_buf_vaddr);
822961cf26aSHari Bathini 	return 0;
823961cf26aSHari Bathini }
824961cf26aSHari Bathini 
8257f0ad11dSHari Bathini void fadump_free_cpu_notes_buf(void)
826961cf26aSHari Bathini {
827961cf26aSHari Bathini 	if (!fw_dump.cpu_notes_buf_vaddr)
828961cf26aSHari Bathini 		return;
829961cf26aSHari Bathini 
830961cf26aSHari Bathini 	fadump_free_buffer(fw_dump.cpu_notes_buf_vaddr,
831961cf26aSHari Bathini 			   fw_dump.cpu_notes_buf_size);
832961cf26aSHari Bathini 	fw_dump.cpu_notes_buf_vaddr = 0;
833961cf26aSHari Bathini 	fw_dump.cpu_notes_buf_size = 0;
834961cf26aSHari Bathini }
835961cf26aSHari Bathini 
836e4fc48fbSHari Bathini static void fadump_free_mem_ranges(struct fadump_mrange_info *mrange_info)
8371bd6a1c4SHari Bathini {
83802c04e37SHari Bathini 	if (mrange_info->is_static) {
83902c04e37SHari Bathini 		mrange_info->mem_range_cnt = 0;
84002c04e37SHari Bathini 		return;
84102c04e37SHari Bathini 	}
84202c04e37SHari Bathini 
843e4fc48fbSHari Bathini 	kfree(mrange_info->mem_ranges);
84402c04e37SHari Bathini 	memset((void *)((u64)mrange_info + RNG_NAME_SZ), 0,
84502c04e37SHari Bathini 	       (sizeof(struct fadump_mrange_info) - RNG_NAME_SZ));
8461bd6a1c4SHari Bathini }
8471bd6a1c4SHari Bathini 
8481bd6a1c4SHari Bathini /*
849e4fc48fbSHari Bathini  * Allocate or reallocate mem_ranges array in incremental units
8501bd6a1c4SHari Bathini  * of PAGE_SIZE.
8511bd6a1c4SHari Bathini  */
852e4fc48fbSHari Bathini static int fadump_alloc_mem_ranges(struct fadump_mrange_info *mrange_info)
8531bd6a1c4SHari Bathini {
854e4fc48fbSHari Bathini 	struct fadump_memory_range *new_array;
8551bd6a1c4SHari Bathini 	u64 new_size;
8561bd6a1c4SHari Bathini 
857e4fc48fbSHari Bathini 	new_size = mrange_info->mem_ranges_sz + PAGE_SIZE;
858e4fc48fbSHari Bathini 	pr_debug("Allocating %llu bytes of memory for %s memory ranges\n",
859e4fc48fbSHari Bathini 		 new_size, mrange_info->name);
8601bd6a1c4SHari Bathini 
861e4fc48fbSHari Bathini 	new_array = krealloc(mrange_info->mem_ranges, new_size, GFP_KERNEL);
8621bd6a1c4SHari Bathini 	if (new_array == NULL) {
863e4fc48fbSHari Bathini 		pr_err("Insufficient memory for setting up %s memory ranges\n",
864e4fc48fbSHari Bathini 		       mrange_info->name);
865e4fc48fbSHari Bathini 		fadump_free_mem_ranges(mrange_info);
8661bd6a1c4SHari Bathini 		return -ENOMEM;
8671bd6a1c4SHari Bathini 	}
8681bd6a1c4SHari Bathini 
869e4fc48fbSHari Bathini 	mrange_info->mem_ranges = new_array;
870e4fc48fbSHari Bathini 	mrange_info->mem_ranges_sz = new_size;
871e4fc48fbSHari Bathini 	mrange_info->max_mem_ranges = (new_size /
872e4fc48fbSHari Bathini 				       sizeof(struct fadump_memory_range));
8731bd6a1c4SHari Bathini 	return 0;
8741bd6a1c4SHari Bathini }
875e4fc48fbSHari Bathini static inline int fadump_add_mem_range(struct fadump_mrange_info *mrange_info,
876e4fc48fbSHari Bathini 				       u64 base, u64 end)
8772df173d9SMahesh Salgaonkar {
878e4fc48fbSHari Bathini 	struct fadump_memory_range *mem_ranges = mrange_info->mem_ranges;
879ced1bf52SHari Bathini 	bool is_adjacent = false;
880e4fc48fbSHari Bathini 	u64 start, size;
881ced1bf52SHari Bathini 
8822df173d9SMahesh Salgaonkar 	if (base == end)
8831bd6a1c4SHari Bathini 		return 0;
8841bd6a1c4SHari Bathini 
885ced1bf52SHari Bathini 	/*
886ced1bf52SHari Bathini 	 * Fold adjacent memory ranges to bring down the memory ranges/
887ced1bf52SHari Bathini 	 * PT_LOAD segments count.
888ced1bf52SHari Bathini 	 */
889e4fc48fbSHari Bathini 	if (mrange_info->mem_range_cnt) {
890e4fc48fbSHari Bathini 		start = mem_ranges[mrange_info->mem_range_cnt - 1].base;
891e4fc48fbSHari Bathini 		size  = mem_ranges[mrange_info->mem_range_cnt - 1].size;
892ced1bf52SHari Bathini 
89315eb77f8SHari Bathini 		/*
89415eb77f8SHari Bathini 		 * Boot memory area needs separate PT_LOAD segment(s) as it
89515eb77f8SHari Bathini 		 * is moved to a different location at the time of crash.
89615eb77f8SHari Bathini 		 * So, fold only if the region is not boot memory area.
89715eb77f8SHari Bathini 		 */
89815eb77f8SHari Bathini 		if ((start + size) == base && start >= fw_dump.boot_mem_top)
899ced1bf52SHari Bathini 			is_adjacent = true;
900ced1bf52SHari Bathini 	}
901ced1bf52SHari Bathini 	if (!is_adjacent) {
902ced1bf52SHari Bathini 		/* resize the array on reaching the limit */
903e4fc48fbSHari Bathini 		if (mrange_info->mem_range_cnt == mrange_info->max_mem_ranges) {
9041bd6a1c4SHari Bathini 			int ret;
9051bd6a1c4SHari Bathini 
90602c04e37SHari Bathini 			if (mrange_info->is_static) {
90702c04e37SHari Bathini 				pr_err("Reached array size limit for %s memory ranges\n",
90802c04e37SHari Bathini 				       mrange_info->name);
90902c04e37SHari Bathini 				return -ENOSPC;
91002c04e37SHari Bathini 			}
91102c04e37SHari Bathini 
912e4fc48fbSHari Bathini 			ret = fadump_alloc_mem_ranges(mrange_info);
9131bd6a1c4SHari Bathini 			if (ret)
9141bd6a1c4SHari Bathini 				return ret;
915e4fc48fbSHari Bathini 
916e4fc48fbSHari Bathini 			/* Update to the new resized array */
917e4fc48fbSHari Bathini 			mem_ranges = mrange_info->mem_ranges;
9181bd6a1c4SHari Bathini 		}
9192df173d9SMahesh Salgaonkar 
920ced1bf52SHari Bathini 		start = base;
921e4fc48fbSHari Bathini 		mem_ranges[mrange_info->mem_range_cnt].base = start;
922e4fc48fbSHari Bathini 		mrange_info->mem_range_cnt++;
923ced1bf52SHari Bathini 	}
924ced1bf52SHari Bathini 
925e4fc48fbSHari Bathini 	mem_ranges[mrange_info->mem_range_cnt - 1].size = (end - start);
926e4fc48fbSHari Bathini 	pr_debug("%s_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n",
927e4fc48fbSHari Bathini 		 mrange_info->name, (mrange_info->mem_range_cnt - 1),
928e4fc48fbSHari Bathini 		 start, end - 1, (end - start));
9291bd6a1c4SHari Bathini 	return 0;
9302df173d9SMahesh Salgaonkar }
9312df173d9SMahesh Salgaonkar 
932e4fc48fbSHari Bathini static int fadump_exclude_reserved_area(u64 start, u64 end)
9332df173d9SMahesh Salgaonkar {
934e4fc48fbSHari Bathini 	u64 ra_start, ra_end;
9351bd6a1c4SHari Bathini 	int ret = 0;
9362df173d9SMahesh Salgaonkar 
9372df173d9SMahesh Salgaonkar 	ra_start = fw_dump.reserve_dump_area_start;
9382df173d9SMahesh Salgaonkar 	ra_end = ra_start + fw_dump.reserve_dump_area_size;
9392df173d9SMahesh Salgaonkar 
9402df173d9SMahesh Salgaonkar 	if ((ra_start < end) && (ra_end > start)) {
9412df173d9SMahesh Salgaonkar 		if ((start < ra_start) && (end > ra_end)) {
942e4fc48fbSHari Bathini 			ret = fadump_add_mem_range(&crash_mrange_info,
943e4fc48fbSHari Bathini 						   start, ra_start);
9441bd6a1c4SHari Bathini 			if (ret)
9451bd6a1c4SHari Bathini 				return ret;
9461bd6a1c4SHari Bathini 
947e4fc48fbSHari Bathini 			ret = fadump_add_mem_range(&crash_mrange_info,
948e4fc48fbSHari Bathini 						   ra_end, end);
9492df173d9SMahesh Salgaonkar 		} else if (start < ra_start) {
950e4fc48fbSHari Bathini 			ret = fadump_add_mem_range(&crash_mrange_info,
951e4fc48fbSHari Bathini 						   start, ra_start);
9522df173d9SMahesh Salgaonkar 		} else if (ra_end < end) {
953e4fc48fbSHari Bathini 			ret = fadump_add_mem_range(&crash_mrange_info,
954e4fc48fbSHari Bathini 						   ra_end, end);
9552df173d9SMahesh Salgaonkar 		}
9562df173d9SMahesh Salgaonkar 	} else
957e4fc48fbSHari Bathini 		ret = fadump_add_mem_range(&crash_mrange_info, start, end);
9581bd6a1c4SHari Bathini 
9591bd6a1c4SHari Bathini 	return ret;
9602df173d9SMahesh Salgaonkar }
9612df173d9SMahesh Salgaonkar 
9622df173d9SMahesh Salgaonkar static int fadump_init_elfcore_header(char *bufp)
9632df173d9SMahesh Salgaonkar {
9642df173d9SMahesh Salgaonkar 	struct elfhdr *elf;
9652df173d9SMahesh Salgaonkar 
9662df173d9SMahesh Salgaonkar 	elf = (struct elfhdr *) bufp;
9672df173d9SMahesh Salgaonkar 	bufp += sizeof(struct elfhdr);
9682df173d9SMahesh Salgaonkar 	memcpy(elf->e_ident, ELFMAG, SELFMAG);
9692df173d9SMahesh Salgaonkar 	elf->e_ident[EI_CLASS] = ELF_CLASS;
9702df173d9SMahesh Salgaonkar 	elf->e_ident[EI_DATA] = ELF_DATA;
9712df173d9SMahesh Salgaonkar 	elf->e_ident[EI_VERSION] = EV_CURRENT;
9722df173d9SMahesh Salgaonkar 	elf->e_ident[EI_OSABI] = ELF_OSABI;
9732df173d9SMahesh Salgaonkar 	memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
9742df173d9SMahesh Salgaonkar 	elf->e_type = ET_CORE;
9752df173d9SMahesh Salgaonkar 	elf->e_machine = ELF_ARCH;
9762df173d9SMahesh Salgaonkar 	elf->e_version = EV_CURRENT;
9772df173d9SMahesh Salgaonkar 	elf->e_entry = 0;
9782df173d9SMahesh Salgaonkar 	elf->e_phoff = sizeof(struct elfhdr);
9792df173d9SMahesh Salgaonkar 	elf->e_shoff = 0;
980d8bced27SDaniel Axtens #if defined(_CALL_ELF)
981d8bced27SDaniel Axtens 	elf->e_flags = _CALL_ELF;
982d8bced27SDaniel Axtens #else
983d8bced27SDaniel Axtens 	elf->e_flags = 0;
984d8bced27SDaniel Axtens #endif
9852df173d9SMahesh Salgaonkar 	elf->e_ehsize = sizeof(struct elfhdr);
9862df173d9SMahesh Salgaonkar 	elf->e_phentsize = sizeof(struct elf_phdr);
9872df173d9SMahesh Salgaonkar 	elf->e_phnum = 0;
9882df173d9SMahesh Salgaonkar 	elf->e_shentsize = 0;
9892df173d9SMahesh Salgaonkar 	elf->e_shnum = 0;
9902df173d9SMahesh Salgaonkar 	elf->e_shstrndx = 0;
9912df173d9SMahesh Salgaonkar 
9922df173d9SMahesh Salgaonkar 	return 0;
9932df173d9SMahesh Salgaonkar }
9942df173d9SMahesh Salgaonkar 
9952df173d9SMahesh Salgaonkar /*
9962df173d9SMahesh Salgaonkar  * Traverse through memblock structure and setup crash memory ranges. These
9972df173d9SMahesh Salgaonkar  * ranges will be used create PT_LOAD program headers in elfcore header.
9982df173d9SMahesh Salgaonkar  */
9991bd6a1c4SHari Bathini static int fadump_setup_crash_memory_ranges(void)
10002df173d9SMahesh Salgaonkar {
1001b10d6bcaSMike Rapoport 	u64 i, start, end;
1002b10d6bcaSMike Rapoport 	int ret;
10032df173d9SMahesh Salgaonkar 
10042df173d9SMahesh Salgaonkar 	pr_debug("Setup crash memory ranges.\n");
1005e4fc48fbSHari Bathini 	crash_mrange_info.mem_range_cnt = 0;
1006ced1bf52SHari Bathini 
10072df173d9SMahesh Salgaonkar 	/*
10087dee93a9SHari Bathini 	 * Boot memory region(s) registered with firmware are moved to
10097dee93a9SHari Bathini 	 * different location at the time of crash. Create separate program
10107dee93a9SHari Bathini 	 * header(s) for this memory chunk(s) with the correct offset.
10112df173d9SMahesh Salgaonkar 	 */
10127dee93a9SHari Bathini 	for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) {
10137dee93a9SHari Bathini 		start = fw_dump.boot_mem_addr[i];
10147dee93a9SHari Bathini 		end = start + fw_dump.boot_mem_sz[i];
10157dee93a9SHari Bathini 		ret = fadump_add_mem_range(&crash_mrange_info, start, end);
10161bd6a1c4SHari Bathini 		if (ret)
10171bd6a1c4SHari Bathini 			return ret;
10187dee93a9SHari Bathini 	}
10192df173d9SMahesh Salgaonkar 
1020b10d6bcaSMike Rapoport 	for_each_mem_range(i, &start, &end) {
1021a77af552SHari Bathini 		/*
10227dee93a9SHari Bathini 		 * skip the memory chunk that is already added
10237dee93a9SHari Bathini 		 * (0 through boot_memory_top).
1024a77af552SHari Bathini 		 */
10257dee93a9SHari Bathini 		if (start < fw_dump.boot_mem_top) {
10267dee93a9SHari Bathini 			if (end > fw_dump.boot_mem_top)
10277dee93a9SHari Bathini 				start = fw_dump.boot_mem_top;
1028a77af552SHari Bathini 			else
1029a77af552SHari Bathini 				continue;
1030a77af552SHari Bathini 		}
10312df173d9SMahesh Salgaonkar 
10322df173d9SMahesh Salgaonkar 		/* add this range excluding the reserved dump area. */
10331bd6a1c4SHari Bathini 		ret = fadump_exclude_reserved_area(start, end);
10341bd6a1c4SHari Bathini 		if (ret)
10351bd6a1c4SHari Bathini 			return ret;
10362df173d9SMahesh Salgaonkar 	}
10371bd6a1c4SHari Bathini 
10381bd6a1c4SHari Bathini 	return 0;
10392df173d9SMahesh Salgaonkar }
10402df173d9SMahesh Salgaonkar 
1041d34c5f26SMahesh Salgaonkar /*
1042d34c5f26SMahesh Salgaonkar  * If the given physical address falls within the boot memory region then
1043d34c5f26SMahesh Salgaonkar  * return the relocated address that points to the dump region reserved
1044d34c5f26SMahesh Salgaonkar  * for saving initial boot memory contents.
1045d34c5f26SMahesh Salgaonkar  */
1046d34c5f26SMahesh Salgaonkar static inline unsigned long fadump_relocate(unsigned long paddr)
1047d34c5f26SMahesh Salgaonkar {
10487dee93a9SHari Bathini 	unsigned long raddr, rstart, rend, rlast, hole_size;
10497dee93a9SHari Bathini 	int i;
10507dee93a9SHari Bathini 
10517dee93a9SHari Bathini 	hole_size = 0;
10527dee93a9SHari Bathini 	rlast = 0;
10537dee93a9SHari Bathini 	raddr = paddr;
10547dee93a9SHari Bathini 	for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) {
10557dee93a9SHari Bathini 		rstart = fw_dump.boot_mem_addr[i];
10567dee93a9SHari Bathini 		rend = rstart + fw_dump.boot_mem_sz[i];
10577dee93a9SHari Bathini 		hole_size += (rstart - rlast);
10587dee93a9SHari Bathini 
10597dee93a9SHari Bathini 		if (paddr >= rstart && paddr < rend) {
10607dee93a9SHari Bathini 			raddr += fw_dump.boot_mem_dest_addr - hole_size;
10617dee93a9SHari Bathini 			break;
10627dee93a9SHari Bathini 		}
10637dee93a9SHari Bathini 
10647dee93a9SHari Bathini 		rlast = rend;
10657dee93a9SHari Bathini 	}
10667dee93a9SHari Bathini 
10677dee93a9SHari Bathini 	pr_debug("vmcoreinfo: paddr = 0x%lx, raddr = 0x%lx\n", paddr, raddr);
10687dee93a9SHari Bathini 	return raddr;
1069d34c5f26SMahesh Salgaonkar }
1070d34c5f26SMahesh Salgaonkar 
10712df173d9SMahesh Salgaonkar static int fadump_create_elfcore_headers(char *bufp)
10722df173d9SMahesh Salgaonkar {
10737dee93a9SHari Bathini 	unsigned long long raddr, offset;
10742df173d9SMahesh Salgaonkar 	struct elf_phdr *phdr;
10757dee93a9SHari Bathini 	struct elfhdr *elf;
10767dee93a9SHari Bathini 	int i, j;
10772df173d9SMahesh Salgaonkar 
10782df173d9SMahesh Salgaonkar 	fadump_init_elfcore_header(bufp);
10792df173d9SMahesh Salgaonkar 	elf = (struct elfhdr *)bufp;
10802df173d9SMahesh Salgaonkar 	bufp += sizeof(struct elfhdr);
10812df173d9SMahesh Salgaonkar 
1082ebaeb5aeSMahesh Salgaonkar 	/*
1083ebaeb5aeSMahesh Salgaonkar 	 * setup ELF PT_NOTE, place holder for cpu notes info. The notes info
1084ebaeb5aeSMahesh Salgaonkar 	 * will be populated during second kernel boot after crash. Hence
1085ebaeb5aeSMahesh Salgaonkar 	 * this PT_NOTE will always be the first elf note.
1086ebaeb5aeSMahesh Salgaonkar 	 *
1087ebaeb5aeSMahesh Salgaonkar 	 * NOTE: Any new ELF note addition should be placed after this note.
1088ebaeb5aeSMahesh Salgaonkar 	 */
1089ebaeb5aeSMahesh Salgaonkar 	phdr = (struct elf_phdr *)bufp;
1090ebaeb5aeSMahesh Salgaonkar 	bufp += sizeof(struct elf_phdr);
1091ebaeb5aeSMahesh Salgaonkar 	phdr->p_type = PT_NOTE;
1092ebaeb5aeSMahesh Salgaonkar 	phdr->p_flags = 0;
1093ebaeb5aeSMahesh Salgaonkar 	phdr->p_vaddr = 0;
1094ebaeb5aeSMahesh Salgaonkar 	phdr->p_align = 0;
1095ebaeb5aeSMahesh Salgaonkar 
1096ebaeb5aeSMahesh Salgaonkar 	phdr->p_offset = 0;
1097ebaeb5aeSMahesh Salgaonkar 	phdr->p_paddr = 0;
1098ebaeb5aeSMahesh Salgaonkar 	phdr->p_filesz = 0;
1099ebaeb5aeSMahesh Salgaonkar 	phdr->p_memsz = 0;
1100ebaeb5aeSMahesh Salgaonkar 
1101ebaeb5aeSMahesh Salgaonkar 	(elf->e_phnum)++;
1102ebaeb5aeSMahesh Salgaonkar 
1103d34c5f26SMahesh Salgaonkar 	/* setup ELF PT_NOTE for vmcoreinfo */
1104d34c5f26SMahesh Salgaonkar 	phdr = (struct elf_phdr *)bufp;
1105d34c5f26SMahesh Salgaonkar 	bufp += sizeof(struct elf_phdr);
1106d34c5f26SMahesh Salgaonkar 	phdr->p_type	= PT_NOTE;
1107d34c5f26SMahesh Salgaonkar 	phdr->p_flags	= 0;
1108d34c5f26SMahesh Salgaonkar 	phdr->p_vaddr	= 0;
1109d34c5f26SMahesh Salgaonkar 	phdr->p_align	= 0;
1110d34c5f26SMahesh Salgaonkar 
1111d34c5f26SMahesh Salgaonkar 	phdr->p_paddr	= fadump_relocate(paddr_vmcoreinfo_note());
1112d34c5f26SMahesh Salgaonkar 	phdr->p_offset	= phdr->p_paddr;
11135203f499SXunlei Pang 	phdr->p_memsz	= phdr->p_filesz = VMCOREINFO_NOTE_SIZE;
1114d34c5f26SMahesh Salgaonkar 
1115d34c5f26SMahesh Salgaonkar 	/* Increment number of program headers. */
1116d34c5f26SMahesh Salgaonkar 	(elf->e_phnum)++;
1117d34c5f26SMahesh Salgaonkar 
11182df173d9SMahesh Salgaonkar 	/* setup PT_LOAD sections. */
11197dee93a9SHari Bathini 	j = 0;
11207dee93a9SHari Bathini 	offset = 0;
11217dee93a9SHari Bathini 	raddr = fw_dump.boot_mem_addr[0];
1122e4fc48fbSHari Bathini 	for (i = 0; i < crash_mrange_info.mem_range_cnt; i++) {
1123e4fc48fbSHari Bathini 		u64 mbase, msize;
11242df173d9SMahesh Salgaonkar 
1125e4fc48fbSHari Bathini 		mbase = crash_mrange_info.mem_ranges[i].base;
1126e4fc48fbSHari Bathini 		msize = crash_mrange_info.mem_ranges[i].size;
11272df173d9SMahesh Salgaonkar 		if (!msize)
11282df173d9SMahesh Salgaonkar 			continue;
11292df173d9SMahesh Salgaonkar 
11302df173d9SMahesh Salgaonkar 		phdr = (struct elf_phdr *)bufp;
11312df173d9SMahesh Salgaonkar 		bufp += sizeof(struct elf_phdr);
11322df173d9SMahesh Salgaonkar 		phdr->p_type	= PT_LOAD;
11332df173d9SMahesh Salgaonkar 		phdr->p_flags	= PF_R|PF_W|PF_X;
11342df173d9SMahesh Salgaonkar 		phdr->p_offset	= mbase;
11352df173d9SMahesh Salgaonkar 
11367dee93a9SHari Bathini 		if (mbase == raddr) {
11372df173d9SMahesh Salgaonkar 			/*
1138becd91d9SHari Bathini 			 * The entire real memory region will be moved by
1139becd91d9SHari Bathini 			 * firmware to the specified destination_address.
1140becd91d9SHari Bathini 			 * Hence set the correct offset.
11412df173d9SMahesh Salgaonkar 			 */
11427dee93a9SHari Bathini 			phdr->p_offset = fw_dump.boot_mem_dest_addr + offset;
11437dee93a9SHari Bathini 			if (j < (fw_dump.boot_mem_regs_cnt - 1)) {
11447dee93a9SHari Bathini 				offset += fw_dump.boot_mem_sz[j];
11457dee93a9SHari Bathini 				raddr = fw_dump.boot_mem_addr[++j];
11467dee93a9SHari Bathini 			}
11472df173d9SMahesh Salgaonkar 		}
11482df173d9SMahesh Salgaonkar 
11492df173d9SMahesh Salgaonkar 		phdr->p_paddr = mbase;
11502df173d9SMahesh Salgaonkar 		phdr->p_vaddr = (unsigned long)__va(mbase);
11512df173d9SMahesh Salgaonkar 		phdr->p_filesz = msize;
11522df173d9SMahesh Salgaonkar 		phdr->p_memsz = msize;
11532df173d9SMahesh Salgaonkar 		phdr->p_align = 0;
11542df173d9SMahesh Salgaonkar 
11552df173d9SMahesh Salgaonkar 		/* Increment number of program headers. */
11562df173d9SMahesh Salgaonkar 		(elf->e_phnum)++;
11572df173d9SMahesh Salgaonkar 	}
11582df173d9SMahesh Salgaonkar 	return 0;
11592df173d9SMahesh Salgaonkar }
11602df173d9SMahesh Salgaonkar 
11612df173d9SMahesh Salgaonkar static unsigned long init_fadump_header(unsigned long addr)
11622df173d9SMahesh Salgaonkar {
11632df173d9SMahesh Salgaonkar 	struct fadump_crash_info_header *fdh;
11642df173d9SMahesh Salgaonkar 
11652df173d9SMahesh Salgaonkar 	if (!addr)
11662df173d9SMahesh Salgaonkar 		return 0;
11672df173d9SMahesh Salgaonkar 
11682df173d9SMahesh Salgaonkar 	fdh = __va(addr);
11692df173d9SMahesh Salgaonkar 	addr += sizeof(struct fadump_crash_info_header);
11702df173d9SMahesh Salgaonkar 
11712df173d9SMahesh Salgaonkar 	memset(fdh, 0, sizeof(struct fadump_crash_info_header));
11722df173d9SMahesh Salgaonkar 	fdh->magic_number = FADUMP_CRASH_INFO_MAGIC;
11732df173d9SMahesh Salgaonkar 	fdh->elfcorehdr_addr = addr;
1174ebaeb5aeSMahesh Salgaonkar 	/* We will set the crashing cpu id in crash_fadump() during crash. */
11750226e552SHari Bathini 	fdh->crashing_cpu = FADUMP_CPU_UNKNOWN;
11766584cec0SHari Bathini 	/*
11776584cec0SHari Bathini 	 * When LPAR is terminated by PYHP, ensure all possible CPUs'
11786584cec0SHari Bathini 	 * register data is processed while exporting the vmcore.
11796584cec0SHari Bathini 	 */
11806584cec0SHari Bathini 	fdh->cpu_mask = *cpu_possible_mask;
11812df173d9SMahesh Salgaonkar 
11822df173d9SMahesh Salgaonkar 	return addr;
11832df173d9SMahesh Salgaonkar }
11842df173d9SMahesh Salgaonkar 
118598b8cd7fSMichal Suchanek static int register_fadump(void)
11863ccc00a7SMahesh Salgaonkar {
11872df173d9SMahesh Salgaonkar 	unsigned long addr;
11882df173d9SMahesh Salgaonkar 	void *vaddr;
11891bd6a1c4SHari Bathini 	int ret;
11902df173d9SMahesh Salgaonkar 
11913ccc00a7SMahesh Salgaonkar 	/*
11923ccc00a7SMahesh Salgaonkar 	 * If no memory is reserved then we can not register for firmware-
11933ccc00a7SMahesh Salgaonkar 	 * assisted dump.
11943ccc00a7SMahesh Salgaonkar 	 */
11953ccc00a7SMahesh Salgaonkar 	if (!fw_dump.reserve_dump_area_size)
119698b8cd7fSMichal Suchanek 		return -ENODEV;
11973ccc00a7SMahesh Salgaonkar 
11981bd6a1c4SHari Bathini 	ret = fadump_setup_crash_memory_ranges();
11991bd6a1c4SHari Bathini 	if (ret)
12001bd6a1c4SHari Bathini 		return ret;
12012df173d9SMahesh Salgaonkar 
120241a65d16SHari Bathini 	addr = fw_dump.fadumphdr_addr;
120341a65d16SHari Bathini 
12042df173d9SMahesh Salgaonkar 	/* Initialize fadump crash info header. */
12052df173d9SMahesh Salgaonkar 	addr = init_fadump_header(addr);
12062df173d9SMahesh Salgaonkar 	vaddr = __va(addr);
12072df173d9SMahesh Salgaonkar 
12082df173d9SMahesh Salgaonkar 	pr_debug("Creating ELF core headers at %#016lx\n", addr);
12092df173d9SMahesh Salgaonkar 	fadump_create_elfcore_headers(vaddr);
12102df173d9SMahesh Salgaonkar 
12113ccc00a7SMahesh Salgaonkar 	/* register the future kernel dump with firmware. */
121241a65d16SHari Bathini 	pr_debug("Registering for firmware-assisted kernel dump...\n");
121341a65d16SHari Bathini 	return fw_dump.ops->fadump_register(&fw_dump);
12143ccc00a7SMahesh Salgaonkar }
12153ccc00a7SMahesh Salgaonkar 
1216b500afffSMahesh Salgaonkar void fadump_cleanup(void)
1217b500afffSMahesh Salgaonkar {
12182790d01dSHari Bathini 	if (!fw_dump.fadump_supported)
12192790d01dSHari Bathini 		return;
12202790d01dSHari Bathini 
1221b500afffSMahesh Salgaonkar 	/* Invalidate the registration only if dump is active. */
1222b500afffSMahesh Salgaonkar 	if (fw_dump.dump_active) {
1223f3512011SHari Bathini 		pr_debug("Invalidating firmware-assisted dump registration\n");
1224f3512011SHari Bathini 		fw_dump.ops->fadump_invalidate(&fw_dump);
1225722cde76SMahesh Salgaonkar 	} else if (fw_dump.dump_registered) {
1226722cde76SMahesh Salgaonkar 		/* Un-register Firmware-assisted dump if it was registered. */
122741a65d16SHari Bathini 		fw_dump.ops->fadump_unregister(&fw_dump);
1228e4fc48fbSHari Bathini 		fadump_free_mem_ranges(&crash_mrange_info);
1229b500afffSMahesh Salgaonkar 	}
12302790d01dSHari Bathini 
12312790d01dSHari Bathini 	if (fw_dump.ops->fadump_cleanup)
12322790d01dSHari Bathini 		fw_dump.ops->fadump_cleanup(&fw_dump);
1233b500afffSMahesh Salgaonkar }
1234b500afffSMahesh Salgaonkar 
123568fa6478SHari Bathini static void fadump_free_reserved_memory(unsigned long start_pfn,
123668fa6478SHari Bathini 					unsigned long end_pfn)
123768fa6478SHari Bathini {
123868fa6478SHari Bathini 	unsigned long pfn;
123968fa6478SHari Bathini 	unsigned long time_limit = jiffies + HZ;
124068fa6478SHari Bathini 
124168fa6478SHari Bathini 	pr_info("freeing reserved memory (0x%llx - 0x%llx)\n",
124268fa6478SHari Bathini 		PFN_PHYS(start_pfn), PFN_PHYS(end_pfn));
124368fa6478SHari Bathini 
124468fa6478SHari Bathini 	for (pfn = start_pfn; pfn < end_pfn; pfn++) {
124568fa6478SHari Bathini 		free_reserved_page(pfn_to_page(pfn));
124668fa6478SHari Bathini 
124768fa6478SHari Bathini 		if (time_after(jiffies, time_limit)) {
124868fa6478SHari Bathini 			cond_resched();
124968fa6478SHari Bathini 			time_limit = jiffies + HZ;
125068fa6478SHari Bathini 		}
125168fa6478SHari Bathini 	}
125268fa6478SHari Bathini }
125368fa6478SHari Bathini 
125468fa6478SHari Bathini /*
125568fa6478SHari Bathini  * Skip memory holes and free memory that was actually reserved.
125668fa6478SHari Bathini  */
1257dda9dbfeSHari Bathini static void fadump_release_reserved_area(u64 start, u64 end)
125868fa6478SHari Bathini {
1259b10d6bcaSMike Rapoport 	unsigned long reg_spfn, reg_epfn;
1260b10d6bcaSMike Rapoport 	u64 tstart, tend, spfn, epfn;
1261b10d6bcaSMike Rapoport 	int i;
126268fa6478SHari Bathini 
1263dda9dbfeSHari Bathini 	spfn = PHYS_PFN(start);
1264dda9dbfeSHari Bathini 	epfn = PHYS_PFN(end);
1265c9118e6cSMike Rapoport 
1266c9118e6cSMike Rapoport 	for_each_mem_pfn_range(i, MAX_NUMNODES, &reg_spfn, &reg_epfn, NULL) {
1267c9118e6cSMike Rapoport 		tstart = max_t(u64, spfn, reg_spfn);
1268c9118e6cSMike Rapoport 		tend   = min_t(u64, epfn, reg_epfn);
1269c9118e6cSMike Rapoport 
127068fa6478SHari Bathini 		if (tstart < tend) {
127168fa6478SHari Bathini 			fadump_free_reserved_memory(tstart, tend);
127268fa6478SHari Bathini 
1273dda9dbfeSHari Bathini 			if (tend == epfn)
127468fa6478SHari Bathini 				break;
127568fa6478SHari Bathini 
1276dda9dbfeSHari Bathini 			spfn = tend;
127768fa6478SHari Bathini 		}
127868fa6478SHari Bathini 	}
127968fa6478SHari Bathini }
128068fa6478SHari Bathini 
1281b500afffSMahesh Salgaonkar /*
1282dda9dbfeSHari Bathini  * Sort the mem ranges in-place and merge adjacent ranges
1283dda9dbfeSHari Bathini  * to minimize the memory ranges count.
1284b500afffSMahesh Salgaonkar  */
1285dda9dbfeSHari Bathini static void sort_and_merge_mem_ranges(struct fadump_mrange_info *mrange_info)
1286b500afffSMahesh Salgaonkar {
1287dda9dbfeSHari Bathini 	struct fadump_memory_range *mem_ranges;
1288dda9dbfeSHari Bathini 	u64 base, size;
1289dda9dbfeSHari Bathini 	int i, j, idx;
1290dda9dbfeSHari Bathini 
1291dda9dbfeSHari Bathini 	if (!reserved_mrange_info.mem_range_cnt)
1292dda9dbfeSHari Bathini 		return;
1293dda9dbfeSHari Bathini 
1294dda9dbfeSHari Bathini 	/* Sort the memory ranges */
1295dda9dbfeSHari Bathini 	mem_ranges = mrange_info->mem_ranges;
1296dda9dbfeSHari Bathini 	for (i = 0; i < mrange_info->mem_range_cnt; i++) {
1297dda9dbfeSHari Bathini 		idx = i;
1298dda9dbfeSHari Bathini 		for (j = (i + 1); j < mrange_info->mem_range_cnt; j++) {
1299dda9dbfeSHari Bathini 			if (mem_ranges[idx].base > mem_ranges[j].base)
1300dda9dbfeSHari Bathini 				idx = j;
1301dda9dbfeSHari Bathini 		}
1302*20776319SJiapeng Chong 		if (idx != i)
1303*20776319SJiapeng Chong 			swap(mem_ranges[idx], mem_ranges[i]);
1304dda9dbfeSHari Bathini 	}
1305dda9dbfeSHari Bathini 
1306dda9dbfeSHari Bathini 	/* Merge adjacent reserved ranges */
1307dda9dbfeSHari Bathini 	idx = 0;
1308dda9dbfeSHari Bathini 	for (i = 1; i < mrange_info->mem_range_cnt; i++) {
1309dda9dbfeSHari Bathini 		base = mem_ranges[i-1].base;
1310dda9dbfeSHari Bathini 		size = mem_ranges[i-1].size;
1311dda9dbfeSHari Bathini 		if (mem_ranges[i].base == (base + size))
1312dda9dbfeSHari Bathini 			mem_ranges[idx].size += mem_ranges[i].size;
1313dda9dbfeSHari Bathini 		else {
1314dda9dbfeSHari Bathini 			idx++;
1315dda9dbfeSHari Bathini 			if (i == idx)
1316dda9dbfeSHari Bathini 				continue;
1317dda9dbfeSHari Bathini 
1318dda9dbfeSHari Bathini 			mem_ranges[idx] = mem_ranges[i];
1319dda9dbfeSHari Bathini 		}
1320dda9dbfeSHari Bathini 	}
1321dda9dbfeSHari Bathini 	mrange_info->mem_range_cnt = idx + 1;
1322dda9dbfeSHari Bathini }
1323dda9dbfeSHari Bathini 
1324dda9dbfeSHari Bathini /*
1325dda9dbfeSHari Bathini  * Scan reserved-ranges to consider them while reserving/releasing
1326dda9dbfeSHari Bathini  * memory for FADump.
1327dda9dbfeSHari Bathini  */
132802c04e37SHari Bathini static void __init early_init_dt_scan_reserved_ranges(unsigned long node)
1329dda9dbfeSHari Bathini {
1330dda9dbfeSHari Bathini 	const __be32 *prop;
1331dda9dbfeSHari Bathini 	int len, ret = -1;
1332dda9dbfeSHari Bathini 	unsigned long i;
1333dda9dbfeSHari Bathini 
133402c04e37SHari Bathini 	/* reserved-ranges already scanned */
133502c04e37SHari Bathini 	if (reserved_mrange_info.mem_range_cnt != 0)
133602c04e37SHari Bathini 		return;
1337dda9dbfeSHari Bathini 
133802c04e37SHari Bathini 	prop = of_get_flat_dt_prop(node, "reserved-ranges", &len);
1339dda9dbfeSHari Bathini 	if (!prop)
134002c04e37SHari Bathini 		return;
1341dda9dbfeSHari Bathini 
1342dda9dbfeSHari Bathini 	/*
1343dda9dbfeSHari Bathini 	 * Each reserved range is an (address,size) pair, 2 cells each,
1344dda9dbfeSHari Bathini 	 * totalling 4 cells per range.
1345dda9dbfeSHari Bathini 	 */
1346dda9dbfeSHari Bathini 	for (i = 0; i < len / (sizeof(*prop) * 4); i++) {
1347dda9dbfeSHari Bathini 		u64 base, size;
1348dda9dbfeSHari Bathini 
1349dda9dbfeSHari Bathini 		base = of_read_number(prop + (i * 4) + 0, 2);
1350dda9dbfeSHari Bathini 		size = of_read_number(prop + (i * 4) + 2, 2);
1351dda9dbfeSHari Bathini 
1352dda9dbfeSHari Bathini 		if (size) {
1353dda9dbfeSHari Bathini 			ret = fadump_add_mem_range(&reserved_mrange_info,
1354dda9dbfeSHari Bathini 						   base, base + size);
1355dda9dbfeSHari Bathini 			if (ret < 0) {
1356dda9dbfeSHari Bathini 				pr_warn("some reserved ranges are ignored!\n");
1357dda9dbfeSHari Bathini 				break;
1358dda9dbfeSHari Bathini 			}
1359dda9dbfeSHari Bathini 		}
1360dda9dbfeSHari Bathini 	}
1361dda9dbfeSHari Bathini 
136202c04e37SHari Bathini 	/* Compact reserved ranges */
136302c04e37SHari Bathini 	sort_and_merge_mem_ranges(&reserved_mrange_info);
1364dda9dbfeSHari Bathini }
1365dda9dbfeSHari Bathini 
1366dda9dbfeSHari Bathini /*
1367dda9dbfeSHari Bathini  * Release the memory that was reserved during early boot to preserve the
1368dda9dbfeSHari Bathini  * crash'ed kernel's memory contents except reserved dump area (permanent
1369dda9dbfeSHari Bathini  * reservation) and reserved ranges used by F/W. The released memory will
1370dda9dbfeSHari Bathini  * be available for general use.
1371dda9dbfeSHari Bathini  */
1372dda9dbfeSHari Bathini static void fadump_release_memory(u64 begin, u64 end)
1373dda9dbfeSHari Bathini {
1374dda9dbfeSHari Bathini 	u64 ra_start, ra_end, tstart;
1375dda9dbfeSHari Bathini 	int i, ret;
1376dda9dbfeSHari Bathini 
1377b500afffSMahesh Salgaonkar 	ra_start = fw_dump.reserve_dump_area_start;
1378b500afffSMahesh Salgaonkar 	ra_end = ra_start + fw_dump.reserve_dump_area_size;
1379b500afffSMahesh Salgaonkar 
1380b500afffSMahesh Salgaonkar 	/*
138102c04e37SHari Bathini 	 * If reserved ranges array limit is hit, overwrite the last reserved
138202c04e37SHari Bathini 	 * memory range with reserved dump area to ensure it is excluded from
138302c04e37SHari Bathini 	 * the memory being released (reused for next FADump registration).
1384dda9dbfeSHari Bathini 	 */
138502c04e37SHari Bathini 	if (reserved_mrange_info.mem_range_cnt ==
138602c04e37SHari Bathini 	    reserved_mrange_info.max_mem_ranges)
138702c04e37SHari Bathini 		reserved_mrange_info.mem_range_cnt--;
1388dda9dbfeSHari Bathini 
138902c04e37SHari Bathini 	ret = fadump_add_mem_range(&reserved_mrange_info, ra_start, ra_end);
139002c04e37SHari Bathini 	if (ret != 0)
1391dda9dbfeSHari Bathini 		return;
1392dda9dbfeSHari Bathini 
1393dda9dbfeSHari Bathini 	/* Get the reserved ranges list in order first. */
1394dda9dbfeSHari Bathini 	sort_and_merge_mem_ranges(&reserved_mrange_info);
1395dda9dbfeSHari Bathini 
1396dda9dbfeSHari Bathini 	/* Exclude reserved ranges and release remaining memory */
1397dda9dbfeSHari Bathini 	tstart = begin;
1398dda9dbfeSHari Bathini 	for (i = 0; i < reserved_mrange_info.mem_range_cnt; i++) {
1399dda9dbfeSHari Bathini 		ra_start = reserved_mrange_info.mem_ranges[i].base;
1400dda9dbfeSHari Bathini 		ra_end = ra_start + reserved_mrange_info.mem_ranges[i].size;
1401dda9dbfeSHari Bathini 
1402dda9dbfeSHari Bathini 		if (tstart >= ra_end)
1403dda9dbfeSHari Bathini 			continue;
1404dda9dbfeSHari Bathini 
1405dda9dbfeSHari Bathini 		if (tstart < ra_start)
1406dda9dbfeSHari Bathini 			fadump_release_reserved_area(tstart, ra_start);
1407dda9dbfeSHari Bathini 		tstart = ra_end;
1408dda9dbfeSHari Bathini 	}
1409dda9dbfeSHari Bathini 
1410dda9dbfeSHari Bathini 	if (tstart < end)
1411dda9dbfeSHari Bathini 		fadump_release_reserved_area(tstart, end);
1412b500afffSMahesh Salgaonkar }
1413b500afffSMahesh Salgaonkar 
1414b500afffSMahesh Salgaonkar static void fadump_invalidate_release_mem(void)
1415b500afffSMahesh Salgaonkar {
1416b500afffSMahesh Salgaonkar 	mutex_lock(&fadump_mutex);
1417b500afffSMahesh Salgaonkar 	if (!fw_dump.dump_active) {
1418b500afffSMahesh Salgaonkar 		mutex_unlock(&fadump_mutex);
1419b500afffSMahesh Salgaonkar 		return;
1420b500afffSMahesh Salgaonkar 	}
1421b500afffSMahesh Salgaonkar 
1422b500afffSMahesh Salgaonkar 	fadump_cleanup();
1423b500afffSMahesh Salgaonkar 	mutex_unlock(&fadump_mutex);
1424b500afffSMahesh Salgaonkar 
14257dee93a9SHari Bathini 	fadump_release_memory(fw_dump.boot_mem_top, memblock_end_of_DRAM());
1426961cf26aSHari Bathini 	fadump_free_cpu_notes_buf();
1427961cf26aSHari Bathini 
1428a4e2e2caSHari Bathini 	/*
1429a4e2e2caSHari Bathini 	 * Setup kernel metadata and initialize the kernel dump
1430a4e2e2caSHari Bathini 	 * memory structure for FADump re-registration.
1431a4e2e2caSHari Bathini 	 */
1432a4e2e2caSHari Bathini 	if (fw_dump.ops->fadump_setup_metadata &&
1433a4e2e2caSHari Bathini 	    (fw_dump.ops->fadump_setup_metadata(&fw_dump) < 0))
1434a4e2e2caSHari Bathini 		pr_warn("Failed to setup kernel metadata!\n");
143541a65d16SHari Bathini 	fw_dump.ops->fadump_init_mem_struct(&fw_dump);
1436b500afffSMahesh Salgaonkar }
1437b500afffSMahesh Salgaonkar 
1438d418b19fSSourabh Jain static ssize_t release_mem_store(struct kobject *kobj,
1439b500afffSMahesh Salgaonkar 				 struct kobj_attribute *attr,
1440b500afffSMahesh Salgaonkar 				 const char *buf, size_t count)
1441b500afffSMahesh Salgaonkar {
1442dcdc4679SMichal Suchanek 	int input = -1;
1443dcdc4679SMichal Suchanek 
1444b500afffSMahesh Salgaonkar 	if (!fw_dump.dump_active)
1445b500afffSMahesh Salgaonkar 		return -EPERM;
1446b500afffSMahesh Salgaonkar 
1447dcdc4679SMichal Suchanek 	if (kstrtoint(buf, 0, &input))
1448dcdc4679SMichal Suchanek 		return -EINVAL;
1449dcdc4679SMichal Suchanek 
1450dcdc4679SMichal Suchanek 	if (input == 1) {
1451b500afffSMahesh Salgaonkar 		/*
1452b500afffSMahesh Salgaonkar 		 * Take away the '/proc/vmcore'. We are releasing the dump
1453b500afffSMahesh Salgaonkar 		 * memory, hence it will not be valid anymore.
1454b500afffSMahesh Salgaonkar 		 */
14552685f826SMichael Ellerman #ifdef CONFIG_PROC_VMCORE
1456b500afffSMahesh Salgaonkar 		vmcore_cleanup();
14572685f826SMichael Ellerman #endif
1458b500afffSMahesh Salgaonkar 		fadump_invalidate_release_mem();
1459b500afffSMahesh Salgaonkar 
1460b500afffSMahesh Salgaonkar 	} else
1461b500afffSMahesh Salgaonkar 		return -EINVAL;
1462b500afffSMahesh Salgaonkar 	return count;
1463b500afffSMahesh Salgaonkar }
1464b500afffSMahesh Salgaonkar 
1465d418b19fSSourabh Jain /* Release the reserved memory and disable the FADump */
1466d276960dSNick Child static void __init unregister_fadump(void)
1467d418b19fSSourabh Jain {
1468d418b19fSSourabh Jain 	fadump_cleanup();
1469d418b19fSSourabh Jain 	fadump_release_memory(fw_dump.reserve_dump_area_start,
1470d418b19fSSourabh Jain 			      fw_dump.reserve_dump_area_size);
1471d418b19fSSourabh Jain 	fw_dump.fadump_enabled = 0;
1472d418b19fSSourabh Jain 	kobject_put(fadump_kobj);
1473d418b19fSSourabh Jain }
1474d418b19fSSourabh Jain 
1475d418b19fSSourabh Jain static ssize_t enabled_show(struct kobject *kobj,
14763ccc00a7SMahesh Salgaonkar 			    struct kobj_attribute *attr,
14773ccc00a7SMahesh Salgaonkar 			    char *buf)
14783ccc00a7SMahesh Salgaonkar {
14793ccc00a7SMahesh Salgaonkar 	return sprintf(buf, "%d\n", fw_dump.fadump_enabled);
14803ccc00a7SMahesh Salgaonkar }
14813ccc00a7SMahesh Salgaonkar 
1482d8e73458SSourabh Jain static ssize_t mem_reserved_show(struct kobject *kobj,
1483d8e73458SSourabh Jain 				 struct kobj_attribute *attr,
1484d8e73458SSourabh Jain 				 char *buf)
1485d8e73458SSourabh Jain {
1486d8e73458SSourabh Jain 	return sprintf(buf, "%ld\n", fw_dump.reserve_dump_area_size);
1487d8e73458SSourabh Jain }
1488d8e73458SSourabh Jain 
1489d418b19fSSourabh Jain static ssize_t registered_show(struct kobject *kobj,
14903ccc00a7SMahesh Salgaonkar 			       struct kobj_attribute *attr,
14913ccc00a7SMahesh Salgaonkar 			       char *buf)
14923ccc00a7SMahesh Salgaonkar {
14933ccc00a7SMahesh Salgaonkar 	return sprintf(buf, "%d\n", fw_dump.dump_registered);
14943ccc00a7SMahesh Salgaonkar }
14953ccc00a7SMahesh Salgaonkar 
1496d418b19fSSourabh Jain static ssize_t registered_store(struct kobject *kobj,
14973ccc00a7SMahesh Salgaonkar 				struct kobj_attribute *attr,
14983ccc00a7SMahesh Salgaonkar 				const char *buf, size_t count)
14993ccc00a7SMahesh Salgaonkar {
15003ccc00a7SMahesh Salgaonkar 	int ret = 0;
1501dcdc4679SMichal Suchanek 	int input = -1;
15023ccc00a7SMahesh Salgaonkar 
1503f3512011SHari Bathini 	if (!fw_dump.fadump_enabled || fw_dump.dump_active)
15043ccc00a7SMahesh Salgaonkar 		return -EPERM;
15053ccc00a7SMahesh Salgaonkar 
1506dcdc4679SMichal Suchanek 	if (kstrtoint(buf, 0, &input))
1507dcdc4679SMichal Suchanek 		return -EINVAL;
1508dcdc4679SMichal Suchanek 
15093ccc00a7SMahesh Salgaonkar 	mutex_lock(&fadump_mutex);
15103ccc00a7SMahesh Salgaonkar 
1511dcdc4679SMichal Suchanek 	switch (input) {
1512dcdc4679SMichal Suchanek 	case 0:
15133ccc00a7SMahesh Salgaonkar 		if (fw_dump.dump_registered == 0) {
15143ccc00a7SMahesh Salgaonkar 			goto unlock_out;
15153ccc00a7SMahesh Salgaonkar 		}
1516f3512011SHari Bathini 
15173ccc00a7SMahesh Salgaonkar 		/* Un-register Firmware-assisted dump */
151841a65d16SHari Bathini 		pr_debug("Un-register firmware-assisted dump\n");
151941a65d16SHari Bathini 		fw_dump.ops->fadump_unregister(&fw_dump);
15203ccc00a7SMahesh Salgaonkar 		break;
1521dcdc4679SMichal Suchanek 	case 1:
15223ccc00a7SMahesh Salgaonkar 		if (fw_dump.dump_registered == 1) {
15230823c68bSHari Bathini 			/* Un-register Firmware-assisted dump */
152441a65d16SHari Bathini 			fw_dump.ops->fadump_unregister(&fw_dump);
15253ccc00a7SMahesh Salgaonkar 		}
15263ccc00a7SMahesh Salgaonkar 		/* Register Firmware-assisted dump */
152798b8cd7fSMichal Suchanek 		ret = register_fadump();
15283ccc00a7SMahesh Salgaonkar 		break;
15293ccc00a7SMahesh Salgaonkar 	default:
15303ccc00a7SMahesh Salgaonkar 		ret = -EINVAL;
15313ccc00a7SMahesh Salgaonkar 		break;
15323ccc00a7SMahesh Salgaonkar 	}
15333ccc00a7SMahesh Salgaonkar 
15343ccc00a7SMahesh Salgaonkar unlock_out:
15353ccc00a7SMahesh Salgaonkar 	mutex_unlock(&fadump_mutex);
15363ccc00a7SMahesh Salgaonkar 	return ret < 0 ? ret : count;
15373ccc00a7SMahesh Salgaonkar }
15383ccc00a7SMahesh Salgaonkar 
15393ccc00a7SMahesh Salgaonkar static int fadump_region_show(struct seq_file *m, void *private)
15403ccc00a7SMahesh Salgaonkar {
15413ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_enabled)
15423ccc00a7SMahesh Salgaonkar 		return 0;
15433ccc00a7SMahesh Salgaonkar 
1544b500afffSMahesh Salgaonkar 	mutex_lock(&fadump_mutex);
154541a65d16SHari Bathini 	fw_dump.ops->fadump_region_show(&fw_dump, m);
1546b500afffSMahesh Salgaonkar 	mutex_unlock(&fadump_mutex);
15473ccc00a7SMahesh Salgaonkar 	return 0;
15483ccc00a7SMahesh Salgaonkar }
15493ccc00a7SMahesh Salgaonkar 
1550d418b19fSSourabh Jain static struct kobj_attribute release_attr = __ATTR_WO(release_mem);
1551d418b19fSSourabh Jain static struct kobj_attribute enable_attr = __ATTR_RO(enabled);
1552d418b19fSSourabh Jain static struct kobj_attribute register_attr = __ATTR_RW(registered);
1553d8e73458SSourabh Jain static struct kobj_attribute mem_reserved_attr = __ATTR_RO(mem_reserved);
1554d418b19fSSourabh Jain 
1555d418b19fSSourabh Jain static struct attribute *fadump_attrs[] = {
1556d418b19fSSourabh Jain 	&enable_attr.attr,
1557d418b19fSSourabh Jain 	&register_attr.attr,
1558d8e73458SSourabh Jain 	&mem_reserved_attr.attr,
1559d418b19fSSourabh Jain 	NULL,
1560d418b19fSSourabh Jain };
1561d418b19fSSourabh Jain 
1562d418b19fSSourabh Jain ATTRIBUTE_GROUPS(fadump);
15633ccc00a7SMahesh Salgaonkar 
1564f6cee260SYangtao Li DEFINE_SHOW_ATTRIBUTE(fadump_region);
15653ccc00a7SMahesh Salgaonkar 
1566d276960dSNick Child static void __init fadump_init_files(void)
15673ccc00a7SMahesh Salgaonkar {
15683ccc00a7SMahesh Salgaonkar 	int rc = 0;
15693ccc00a7SMahesh Salgaonkar 
1570d418b19fSSourabh Jain 	fadump_kobj = kobject_create_and_add("fadump", kernel_kobj);
1571d418b19fSSourabh Jain 	if (!fadump_kobj) {
1572d418b19fSSourabh Jain 		pr_err("failed to create fadump kobject\n");
1573d418b19fSSourabh Jain 		return;
1574d418b19fSSourabh Jain 	}
1575860286cfSGreg Kroah-Hartman 
1576dbf77fedSAneesh Kumar K.V 	debugfs_create_file("fadump_region", 0444, arch_debugfs_dir, NULL,
15773ccc00a7SMahesh Salgaonkar 			    &fadump_region_fops);
1578b500afffSMahesh Salgaonkar 
1579b500afffSMahesh Salgaonkar 	if (fw_dump.dump_active) {
1580d418b19fSSourabh Jain 		rc = sysfs_create_file(fadump_kobj, &release_attr.attr);
1581b500afffSMahesh Salgaonkar 		if (rc)
1582d418b19fSSourabh Jain 			pr_err("unable to create release_mem sysfs file (%d)\n",
1583d418b19fSSourabh Jain 			       rc);
1584d418b19fSSourabh Jain 	}
1585d418b19fSSourabh Jain 
1586d418b19fSSourabh Jain 	rc = sysfs_create_groups(fadump_kobj, fadump_groups);
1587d418b19fSSourabh Jain 	if (rc) {
1588d418b19fSSourabh Jain 		pr_err("sysfs group creation failed (%d), unregistering FADump",
1589d418b19fSSourabh Jain 		       rc);
1590d418b19fSSourabh Jain 		unregister_fadump();
1591d418b19fSSourabh Jain 		return;
1592d418b19fSSourabh Jain 	}
1593d418b19fSSourabh Jain 
1594d418b19fSSourabh Jain 	/*
1595d418b19fSSourabh Jain 	 * The FADump sysfs are moved from kernel_kobj to fadump_kobj need to
1596d418b19fSSourabh Jain 	 * create symlink at old location to maintain backward compatibility.
1597d418b19fSSourabh Jain 	 *
1598d418b19fSSourabh Jain 	 *      - fadump_enabled -> fadump/enabled
1599d418b19fSSourabh Jain 	 *      - fadump_registered -> fadump/registered
1600d418b19fSSourabh Jain 	 *      - fadump_release_mem -> fadump/release_mem
1601d418b19fSSourabh Jain 	 */
1602d418b19fSSourabh Jain 	rc = compat_only_sysfs_link_entry_to_kobj(kernel_kobj, fadump_kobj,
1603d418b19fSSourabh Jain 						  "enabled", "fadump_enabled");
1604d418b19fSSourabh Jain 	if (rc) {
1605d418b19fSSourabh Jain 		pr_err("unable to create fadump_enabled symlink (%d)", rc);
1606d418b19fSSourabh Jain 		return;
1607d418b19fSSourabh Jain 	}
1608d418b19fSSourabh Jain 
1609d418b19fSSourabh Jain 	rc = compat_only_sysfs_link_entry_to_kobj(kernel_kobj, fadump_kobj,
1610d418b19fSSourabh Jain 						  "registered",
1611d418b19fSSourabh Jain 						  "fadump_registered");
1612d418b19fSSourabh Jain 	if (rc) {
1613d418b19fSSourabh Jain 		pr_err("unable to create fadump_registered symlink (%d)", rc);
1614d418b19fSSourabh Jain 		sysfs_remove_link(kernel_kobj, "fadump_enabled");
1615d418b19fSSourabh Jain 		return;
1616d418b19fSSourabh Jain 	}
1617d418b19fSSourabh Jain 
1618d418b19fSSourabh Jain 	if (fw_dump.dump_active) {
1619d418b19fSSourabh Jain 		rc = compat_only_sysfs_link_entry_to_kobj(kernel_kobj,
1620d418b19fSSourabh Jain 							  fadump_kobj,
1621d418b19fSSourabh Jain 							  "release_mem",
1622d418b19fSSourabh Jain 							  "fadump_release_mem");
1623d418b19fSSourabh Jain 		if (rc)
1624d418b19fSSourabh Jain 			pr_err("unable to create fadump_release_mem symlink (%d)",
1625d418b19fSSourabh Jain 			       rc);
1626b500afffSMahesh Salgaonkar 	}
16273ccc00a7SMahesh Salgaonkar 	return;
16283ccc00a7SMahesh Salgaonkar }
16293ccc00a7SMahesh Salgaonkar 
16303ccc00a7SMahesh Salgaonkar /*
16313ccc00a7SMahesh Salgaonkar  * Prepare for firmware-assisted dump.
16323ccc00a7SMahesh Salgaonkar  */
16333ccc00a7SMahesh Salgaonkar int __init setup_fadump(void)
16343ccc00a7SMahesh Salgaonkar {
1635565f9bc0SMichal Suchanek 	if (!fw_dump.fadump_supported)
16363ccc00a7SMahesh Salgaonkar 		return 0;
16373ccc00a7SMahesh Salgaonkar 
1638565f9bc0SMichal Suchanek 	fadump_init_files();
16393ccc00a7SMahesh Salgaonkar 	fadump_show_config();
1640565f9bc0SMichal Suchanek 
1641565f9bc0SMichal Suchanek 	if (!fw_dump.fadump_enabled)
1642565f9bc0SMichal Suchanek 		return 1;
1643565f9bc0SMichal Suchanek 
16442df173d9SMahesh Salgaonkar 	/*
16452df173d9SMahesh Salgaonkar 	 * If dump data is available then see if it is valid and prepare for
16462df173d9SMahesh Salgaonkar 	 * saving it to the disk.
16472df173d9SMahesh Salgaonkar 	 */
1648b500afffSMahesh Salgaonkar 	if (fw_dump.dump_active) {
1649b500afffSMahesh Salgaonkar 		/*
1650b500afffSMahesh Salgaonkar 		 * if dump process fails then invalidate the registration
1651b500afffSMahesh Salgaonkar 		 * and release memory before proceeding for re-registration.
1652b500afffSMahesh Salgaonkar 		 */
1653f3512011SHari Bathini 		if (fw_dump.ops->fadump_process(&fw_dump) < 0)
1654b500afffSMahesh Salgaonkar 			fadump_invalidate_release_mem();
1655b500afffSMahesh Salgaonkar 	}
1656607451ceSHari Bathini 	/* Initialize the kernel dump memory structure and register with f/w */
1657607451ceSHari Bathini 	else if (fw_dump.reserve_dump_area_size) {
165841a65d16SHari Bathini 		fw_dump.ops->fadump_init_mem_struct(&fw_dump);
1659607451ceSHari Bathini 		register_fadump();
1660607451ceSHari Bathini 	}
1661f3512011SHari Bathini 
166206e629c2SHari Bathini 	/*
166306e629c2SHari Bathini 	 * In case of panic, fadump is triggered via ppc_panic_event()
166406e629c2SHari Bathini 	 * panic notifier. Setting crash_kexec_post_notifiers to 'true'
166506e629c2SHari Bathini 	 * lets panic() function take crash friendly path before panic
166606e629c2SHari Bathini 	 * notifiers are invoked.
166706e629c2SHari Bathini 	 */
166806e629c2SHari Bathini 	crash_kexec_post_notifiers = true;
166906e629c2SHari Bathini 
16703ccc00a7SMahesh Salgaonkar 	return 1;
16713ccc00a7SMahesh Salgaonkar }
1672607451ceSHari Bathini /*
1673607451ceSHari Bathini  * Use subsys_initcall_sync() here because there is dependency with
1674607451ceSHari Bathini  * crash_save_vmcoreinfo_init(), which mush run first to ensure vmcoreinfo initialization
1675607451ceSHari Bathini  * is done before regisering with f/w.
1676607451ceSHari Bathini  */
1677607451ceSHari Bathini subsys_initcall_sync(setup_fadump);
1678bec53196SHari Bathini #else /* !CONFIG_PRESERVE_FA_DUMP */
1679bec53196SHari Bathini 
1680bec53196SHari Bathini /* Scan the Firmware Assisted dump configuration details. */
1681bec53196SHari Bathini int __init early_init_dt_scan_fw_dump(unsigned long node, const char *uname,
1682bec53196SHari Bathini 				      int depth, void *data)
1683bec53196SHari Bathini {
1684bec53196SHari Bathini 	if ((depth != 1) || (strcmp(uname, "ibm,opal") != 0))
1685bec53196SHari Bathini 		return 0;
1686bec53196SHari Bathini 
1687bec53196SHari Bathini 	opal_fadump_dt_scan(&fw_dump, node);
1688bec53196SHari Bathini 	return 1;
1689bec53196SHari Bathini }
1690bec53196SHari Bathini 
1691bec53196SHari Bathini /*
1692bec53196SHari Bathini  * When dump is active but PRESERVE_FA_DUMP is enabled on the kernel,
1693bec53196SHari Bathini  * preserve crash data. The subsequent memory preserving kernel boot
1694bec53196SHari Bathini  * is likely to process this crash data.
1695bec53196SHari Bathini  */
1696bec53196SHari Bathini int __init fadump_reserve_mem(void)
1697bec53196SHari Bathini {
1698bec53196SHari Bathini 	if (fw_dump.dump_active) {
1699bec53196SHari Bathini 		/*
1700bec53196SHari Bathini 		 * If last boot has crashed then reserve all the memory
1701bec53196SHari Bathini 		 * above boot memory to preserve crash data.
1702bec53196SHari Bathini 		 */
1703bec53196SHari Bathini 		pr_info("Preserving crash data for processing in next boot.\n");
1704bec53196SHari Bathini 		fadump_reserve_crash_area(fw_dump.boot_mem_top);
1705bec53196SHari Bathini 	} else
1706bec53196SHari Bathini 		pr_debug("FADump-aware kernel..\n");
1707bec53196SHari Bathini 
1708bec53196SHari Bathini 	return 1;
1709bec53196SHari Bathini }
1710bec53196SHari Bathini #endif /* CONFIG_PRESERVE_FA_DUMP */
1711b2a815a5SHari Bathini 
1712b2a815a5SHari Bathini /* Preserve everything above the base address */
1713b2a815a5SHari Bathini static void __init fadump_reserve_crash_area(u64 base)
1714b2a815a5SHari Bathini {
1715b10d6bcaSMike Rapoport 	u64 i, mstart, mend, msize;
1716b2a815a5SHari Bathini 
1717b10d6bcaSMike Rapoport 	for_each_mem_range(i, &mstart, &mend) {
1718b10d6bcaSMike Rapoport 		msize  = mend - mstart;
1719b2a815a5SHari Bathini 
1720b2a815a5SHari Bathini 		if ((mstart + msize) < base)
1721b2a815a5SHari Bathini 			continue;
1722b2a815a5SHari Bathini 
1723b2a815a5SHari Bathini 		if (mstart < base) {
1724b2a815a5SHari Bathini 			msize -= (base - mstart);
1725b2a815a5SHari Bathini 			mstart = base;
1726b2a815a5SHari Bathini 		}
1727b2a815a5SHari Bathini 
1728b2a815a5SHari Bathini 		pr_info("Reserving %lluMB of memory at %#016llx for preserving crash data",
1729b2a815a5SHari Bathini 			(msize >> 20), mstart);
1730b2a815a5SHari Bathini 		memblock_reserve(mstart, msize);
1731b2a815a5SHari Bathini 	}
1732b2a815a5SHari Bathini }
1733bec53196SHari Bathini 
1734bec53196SHari Bathini unsigned long __init arch_reserved_kernel_pages(void)
1735bec53196SHari Bathini {
1736bec53196SHari Bathini 	return memblock_reserved_size() / PAGE_SIZE;
1737bec53196SHari Bathini }
1738