xref: /openbmc/linux/arch/powerpc/kernel/fadump.c (revision ced1bf52f47783135b985d2aacf53fa77fd72e2e)
1eb39c880SMahesh Salgaonkar /*
2eb39c880SMahesh Salgaonkar  * Firmware Assisted dump: A robust mechanism to get reliable kernel crash
3eb39c880SMahesh Salgaonkar  * dump with assistance from firmware. This approach does not use kexec,
4eb39c880SMahesh Salgaonkar  * instead firmware assists in booting the kdump kernel while preserving
5eb39c880SMahesh Salgaonkar  * memory contents. The most of the code implementation has been adapted
6eb39c880SMahesh Salgaonkar  * from phyp assisted dump implementation written by Linas Vepstas and
7eb39c880SMahesh Salgaonkar  * Manish Ahuja
8eb39c880SMahesh Salgaonkar  *
9eb39c880SMahesh Salgaonkar  * This program is free software; you can redistribute it and/or modify
10eb39c880SMahesh Salgaonkar  * it under the terms of the GNU General Public License as published by
11eb39c880SMahesh Salgaonkar  * the Free Software Foundation; either version 2 of the License, or
12eb39c880SMahesh Salgaonkar  * (at your option) any later version.
13eb39c880SMahesh Salgaonkar  *
14eb39c880SMahesh Salgaonkar  * This program is distributed in the hope that it will be useful,
15eb39c880SMahesh Salgaonkar  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16eb39c880SMahesh Salgaonkar  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17eb39c880SMahesh Salgaonkar  * GNU General Public License for more details.
18eb39c880SMahesh Salgaonkar  *
19eb39c880SMahesh Salgaonkar  * You should have received a copy of the GNU General Public License
20eb39c880SMahesh Salgaonkar  * along with this program; if not, write to the Free Software
21eb39c880SMahesh Salgaonkar  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22eb39c880SMahesh Salgaonkar  *
23eb39c880SMahesh Salgaonkar  * Copyright 2011 IBM Corporation
24eb39c880SMahesh Salgaonkar  * Author: Mahesh Salgaonkar <mahesh@linux.vnet.ibm.com>
25eb39c880SMahesh Salgaonkar  */
26eb39c880SMahesh Salgaonkar 
27eb39c880SMahesh Salgaonkar #undef DEBUG
28eb39c880SMahesh Salgaonkar #define pr_fmt(fmt) "fadump: " fmt
29eb39c880SMahesh Salgaonkar 
30eb39c880SMahesh Salgaonkar #include <linux/string.h>
31eb39c880SMahesh Salgaonkar #include <linux/memblock.h>
323ccc00a7SMahesh Salgaonkar #include <linux/delay.h>
333ccc00a7SMahesh Salgaonkar #include <linux/seq_file.h>
342df173d9SMahesh Salgaonkar #include <linux/crash_dump.h>
35b500afffSMahesh Salgaonkar #include <linux/kobject.h>
36b500afffSMahesh Salgaonkar #include <linux/sysfs.h>
37eb39c880SMahesh Salgaonkar 
387644d581SMichael Ellerman #include <asm/debugfs.h>
39eb39c880SMahesh Salgaonkar #include <asm/page.h>
40eb39c880SMahesh Salgaonkar #include <asm/prom.h>
41eb39c880SMahesh Salgaonkar #include <asm/rtas.h>
42eb39c880SMahesh Salgaonkar #include <asm/fadump.h>
43cad3c834SStephen Rothwell #include <asm/setup.h>
44eb39c880SMahesh Salgaonkar 
45eb39c880SMahesh Salgaonkar static struct fw_dump fw_dump;
463ccc00a7SMahesh Salgaonkar static struct fadump_mem_struct fdm;
473ccc00a7SMahesh Salgaonkar static const struct fadump_mem_struct *fdm_active;
483ccc00a7SMahesh Salgaonkar 
493ccc00a7SMahesh Salgaonkar static DEFINE_MUTEX(fadump_mutex);
501bd6a1c4SHari Bathini struct fad_crash_memory_ranges *crash_memory_ranges;
511bd6a1c4SHari Bathini int crash_memory_ranges_size;
522df173d9SMahesh Salgaonkar int crash_mem_ranges;
531bd6a1c4SHari Bathini int max_crash_mem_ranges;
54eb39c880SMahesh Salgaonkar 
55eb39c880SMahesh Salgaonkar /* Scan the Firmware Assisted dump configuration details. */
56eb39c880SMahesh Salgaonkar int __init early_init_dt_scan_fw_dump(unsigned long node,
57eb39c880SMahesh Salgaonkar 			const char *uname, int depth, void *data)
58eb39c880SMahesh Salgaonkar {
599d0c4dfeSRob Herring 	const __be32 *sections;
60eb39c880SMahesh Salgaonkar 	int i, num_sections;
619d0c4dfeSRob Herring 	int size;
62408cddd9SHari Bathini 	const __be32 *token;
63eb39c880SMahesh Salgaonkar 
64eb39c880SMahesh Salgaonkar 	if (depth != 1 || strcmp(uname, "rtas") != 0)
65eb39c880SMahesh Salgaonkar 		return 0;
66eb39c880SMahesh Salgaonkar 
67eb39c880SMahesh Salgaonkar 	/*
68eb39c880SMahesh Salgaonkar 	 * Check if Firmware Assisted dump is supported. if yes, check
69eb39c880SMahesh Salgaonkar 	 * if dump has been initiated on last reboot.
70eb39c880SMahesh Salgaonkar 	 */
71eb39c880SMahesh Salgaonkar 	token = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump", NULL);
72eb39c880SMahesh Salgaonkar 	if (!token)
73a7d04317SGavin Shan 		return 1;
74eb39c880SMahesh Salgaonkar 
75eb39c880SMahesh Salgaonkar 	fw_dump.fadump_supported = 1;
76408cddd9SHari Bathini 	fw_dump.ibm_configure_kernel_dump = be32_to_cpu(*token);
77eb39c880SMahesh Salgaonkar 
78eb39c880SMahesh Salgaonkar 	/*
79eb39c880SMahesh Salgaonkar 	 * The 'ibm,kernel-dump' rtas node is present only if there is
80eb39c880SMahesh Salgaonkar 	 * dump data waiting for us.
81eb39c880SMahesh Salgaonkar 	 */
823ccc00a7SMahesh Salgaonkar 	fdm_active = of_get_flat_dt_prop(node, "ibm,kernel-dump", NULL);
833ccc00a7SMahesh Salgaonkar 	if (fdm_active)
84eb39c880SMahesh Salgaonkar 		fw_dump.dump_active = 1;
85eb39c880SMahesh Salgaonkar 
86eb39c880SMahesh Salgaonkar 	/* Get the sizes required to store dump data for the firmware provided
87eb39c880SMahesh Salgaonkar 	 * dump sections.
88eb39c880SMahesh Salgaonkar 	 * For each dump section type supported, a 32bit cell which defines
89eb39c880SMahesh Salgaonkar 	 * the ID of a supported section followed by two 32 bit cells which
90eb39c880SMahesh Salgaonkar 	 * gives teh size of the section in bytes.
91eb39c880SMahesh Salgaonkar 	 */
92eb39c880SMahesh Salgaonkar 	sections = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump-sizes",
93eb39c880SMahesh Salgaonkar 					&size);
94eb39c880SMahesh Salgaonkar 
95eb39c880SMahesh Salgaonkar 	if (!sections)
96a7d04317SGavin Shan 		return 1;
97eb39c880SMahesh Salgaonkar 
98eb39c880SMahesh Salgaonkar 	num_sections = size / (3 * sizeof(u32));
99eb39c880SMahesh Salgaonkar 
100eb39c880SMahesh Salgaonkar 	for (i = 0; i < num_sections; i++, sections += 3) {
101eb39c880SMahesh Salgaonkar 		u32 type = (u32)of_read_number(sections, 1);
102eb39c880SMahesh Salgaonkar 
103eb39c880SMahesh Salgaonkar 		switch (type) {
104eb39c880SMahesh Salgaonkar 		case FADUMP_CPU_STATE_DATA:
105eb39c880SMahesh Salgaonkar 			fw_dump.cpu_state_data_size =
106eb39c880SMahesh Salgaonkar 					of_read_ulong(&sections[1], 2);
107eb39c880SMahesh Salgaonkar 			break;
108eb39c880SMahesh Salgaonkar 		case FADUMP_HPTE_REGION:
109eb39c880SMahesh Salgaonkar 			fw_dump.hpte_region_size =
110eb39c880SMahesh Salgaonkar 					of_read_ulong(&sections[1], 2);
111eb39c880SMahesh Salgaonkar 			break;
112eb39c880SMahesh Salgaonkar 		}
113eb39c880SMahesh Salgaonkar 	}
114a7d04317SGavin Shan 
115eb39c880SMahesh Salgaonkar 	return 1;
116eb39c880SMahesh Salgaonkar }
117eb39c880SMahesh Salgaonkar 
118eae0dfccSHari Bathini /*
119eae0dfccSHari Bathini  * If fadump is registered, check if the memory provided
120eae0dfccSHari Bathini  * falls within boot memory area.
121eae0dfccSHari Bathini  */
122eae0dfccSHari Bathini int is_fadump_boot_memory_area(u64 addr, ulong size)
123eae0dfccSHari Bathini {
124eae0dfccSHari Bathini 	if (!fw_dump.dump_registered)
125eae0dfccSHari Bathini 		return 0;
126eae0dfccSHari Bathini 
127eae0dfccSHari Bathini 	return (addr + size) > RMA_START && addr <= fw_dump.boot_memory_size;
128eae0dfccSHari Bathini }
129eae0dfccSHari Bathini 
1306fcd6baaSNicholas Piggin int should_fadump_crash(void)
1316fcd6baaSNicholas Piggin {
1326fcd6baaSNicholas Piggin 	if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr)
1336fcd6baaSNicholas Piggin 		return 0;
1346fcd6baaSNicholas Piggin 	return 1;
1356fcd6baaSNicholas Piggin }
1366fcd6baaSNicholas Piggin 
1373ccc00a7SMahesh Salgaonkar int is_fadump_active(void)
1383ccc00a7SMahesh Salgaonkar {
1393ccc00a7SMahesh Salgaonkar 	return fw_dump.dump_active;
1403ccc00a7SMahesh Salgaonkar }
1413ccc00a7SMahesh Salgaonkar 
142a5a05b91SHari Bathini /*
143a5a05b91SHari Bathini  * Returns 1, if there are no holes in boot memory area,
144a5a05b91SHari Bathini  * 0 otherwise.
145a5a05b91SHari Bathini  */
146a5a05b91SHari Bathini static int is_boot_memory_area_contiguous(void)
147a5a05b91SHari Bathini {
148a5a05b91SHari Bathini 	struct memblock_region *reg;
149a5a05b91SHari Bathini 	unsigned long tstart, tend;
150a5a05b91SHari Bathini 	unsigned long start_pfn = PHYS_PFN(RMA_START);
151a5a05b91SHari Bathini 	unsigned long end_pfn = PHYS_PFN(RMA_START + fw_dump.boot_memory_size);
152a5a05b91SHari Bathini 	unsigned int ret = 0;
153a5a05b91SHari Bathini 
154a5a05b91SHari Bathini 	for_each_memblock(memory, reg) {
155a5a05b91SHari Bathini 		tstart = max(start_pfn, memblock_region_memory_base_pfn(reg));
156a5a05b91SHari Bathini 		tend = min(end_pfn, memblock_region_memory_end_pfn(reg));
157a5a05b91SHari Bathini 		if (tstart < tend) {
158a5a05b91SHari Bathini 			/* Memory hole from start_pfn to tstart */
159a5a05b91SHari Bathini 			if (tstart > start_pfn)
160a5a05b91SHari Bathini 				break;
161a5a05b91SHari Bathini 
162a5a05b91SHari Bathini 			if (tend == end_pfn) {
163a5a05b91SHari Bathini 				ret = 1;
164a5a05b91SHari Bathini 				break;
165a5a05b91SHari Bathini 			}
166a5a05b91SHari Bathini 
167a5a05b91SHari Bathini 			start_pfn = tend + 1;
168a5a05b91SHari Bathini 		}
169a5a05b91SHari Bathini 	}
170a5a05b91SHari Bathini 
171a5a05b91SHari Bathini 	return ret;
172a5a05b91SHari Bathini }
173a5a05b91SHari Bathini 
1743ccc00a7SMahesh Salgaonkar /* Print firmware assisted dump configurations for debugging purpose. */
1753ccc00a7SMahesh Salgaonkar static void fadump_show_config(void)
1763ccc00a7SMahesh Salgaonkar {
1773ccc00a7SMahesh Salgaonkar 	pr_debug("Support for firmware-assisted dump (fadump): %s\n",
1783ccc00a7SMahesh Salgaonkar 			(fw_dump.fadump_supported ? "present" : "no support"));
1793ccc00a7SMahesh Salgaonkar 
1803ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_supported)
1813ccc00a7SMahesh Salgaonkar 		return;
1823ccc00a7SMahesh Salgaonkar 
1833ccc00a7SMahesh Salgaonkar 	pr_debug("Fadump enabled    : %s\n",
1843ccc00a7SMahesh Salgaonkar 				(fw_dump.fadump_enabled ? "yes" : "no"));
1853ccc00a7SMahesh Salgaonkar 	pr_debug("Dump Active       : %s\n",
1863ccc00a7SMahesh Salgaonkar 				(fw_dump.dump_active ? "yes" : "no"));
1873ccc00a7SMahesh Salgaonkar 	pr_debug("Dump section sizes:\n");
1883ccc00a7SMahesh Salgaonkar 	pr_debug("    CPU state data size: %lx\n", fw_dump.cpu_state_data_size);
1893ccc00a7SMahesh Salgaonkar 	pr_debug("    HPTE region size   : %lx\n", fw_dump.hpte_region_size);
1903ccc00a7SMahesh Salgaonkar 	pr_debug("Boot memory size  : %lx\n", fw_dump.boot_memory_size);
1913ccc00a7SMahesh Salgaonkar }
1923ccc00a7SMahesh Salgaonkar 
1933ccc00a7SMahesh Salgaonkar static unsigned long init_fadump_mem_struct(struct fadump_mem_struct *fdm,
1943ccc00a7SMahesh Salgaonkar 				unsigned long addr)
1953ccc00a7SMahesh Salgaonkar {
1963ccc00a7SMahesh Salgaonkar 	if (!fdm)
1973ccc00a7SMahesh Salgaonkar 		return 0;
1983ccc00a7SMahesh Salgaonkar 
1993ccc00a7SMahesh Salgaonkar 	memset(fdm, 0, sizeof(struct fadump_mem_struct));
2003ccc00a7SMahesh Salgaonkar 	addr = addr & PAGE_MASK;
2013ccc00a7SMahesh Salgaonkar 
202408cddd9SHari Bathini 	fdm->header.dump_format_version = cpu_to_be32(0x00000001);
203408cddd9SHari Bathini 	fdm->header.dump_num_sections = cpu_to_be16(3);
2043ccc00a7SMahesh Salgaonkar 	fdm->header.dump_status_flag = 0;
2053ccc00a7SMahesh Salgaonkar 	fdm->header.offset_first_dump_section =
206408cddd9SHari Bathini 		cpu_to_be32((u32)offsetof(struct fadump_mem_struct, cpu_state_data));
2073ccc00a7SMahesh Salgaonkar 
2083ccc00a7SMahesh Salgaonkar 	/*
2093ccc00a7SMahesh Salgaonkar 	 * Fields for disk dump option.
2103ccc00a7SMahesh Salgaonkar 	 * We are not using disk dump option, hence set these fields to 0.
2113ccc00a7SMahesh Salgaonkar 	 */
2123ccc00a7SMahesh Salgaonkar 	fdm->header.dd_block_size = 0;
2133ccc00a7SMahesh Salgaonkar 	fdm->header.dd_block_offset = 0;
2143ccc00a7SMahesh Salgaonkar 	fdm->header.dd_num_blocks = 0;
2153ccc00a7SMahesh Salgaonkar 	fdm->header.dd_offset_disk_path = 0;
2163ccc00a7SMahesh Salgaonkar 
2173ccc00a7SMahesh Salgaonkar 	/* set 0 to disable an automatic dump-reboot. */
2183ccc00a7SMahesh Salgaonkar 	fdm->header.max_time_auto = 0;
2193ccc00a7SMahesh Salgaonkar 
2203ccc00a7SMahesh Salgaonkar 	/* Kernel dump sections */
2213ccc00a7SMahesh Salgaonkar 	/* cpu state data section. */
222408cddd9SHari Bathini 	fdm->cpu_state_data.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG);
223408cddd9SHari Bathini 	fdm->cpu_state_data.source_data_type = cpu_to_be16(FADUMP_CPU_STATE_DATA);
2243ccc00a7SMahesh Salgaonkar 	fdm->cpu_state_data.source_address = 0;
225408cddd9SHari Bathini 	fdm->cpu_state_data.source_len = cpu_to_be64(fw_dump.cpu_state_data_size);
226408cddd9SHari Bathini 	fdm->cpu_state_data.destination_address = cpu_to_be64(addr);
2273ccc00a7SMahesh Salgaonkar 	addr += fw_dump.cpu_state_data_size;
2283ccc00a7SMahesh Salgaonkar 
2293ccc00a7SMahesh Salgaonkar 	/* hpte region section */
230408cddd9SHari Bathini 	fdm->hpte_region.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG);
231408cddd9SHari Bathini 	fdm->hpte_region.source_data_type = cpu_to_be16(FADUMP_HPTE_REGION);
2323ccc00a7SMahesh Salgaonkar 	fdm->hpte_region.source_address = 0;
233408cddd9SHari Bathini 	fdm->hpte_region.source_len = cpu_to_be64(fw_dump.hpte_region_size);
234408cddd9SHari Bathini 	fdm->hpte_region.destination_address = cpu_to_be64(addr);
2353ccc00a7SMahesh Salgaonkar 	addr += fw_dump.hpte_region_size;
2363ccc00a7SMahesh Salgaonkar 
2373ccc00a7SMahesh Salgaonkar 	/* RMA region section */
238408cddd9SHari Bathini 	fdm->rmr_region.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG);
239408cddd9SHari Bathini 	fdm->rmr_region.source_data_type = cpu_to_be16(FADUMP_REAL_MODE_REGION);
240408cddd9SHari Bathini 	fdm->rmr_region.source_address = cpu_to_be64(RMA_START);
241408cddd9SHari Bathini 	fdm->rmr_region.source_len = cpu_to_be64(fw_dump.boot_memory_size);
242408cddd9SHari Bathini 	fdm->rmr_region.destination_address = cpu_to_be64(addr);
2433ccc00a7SMahesh Salgaonkar 	addr += fw_dump.boot_memory_size;
2443ccc00a7SMahesh Salgaonkar 
2453ccc00a7SMahesh Salgaonkar 	return addr;
2463ccc00a7SMahesh Salgaonkar }
2473ccc00a7SMahesh Salgaonkar 
248eb39c880SMahesh Salgaonkar /**
249eb39c880SMahesh Salgaonkar  * fadump_calculate_reserve_size(): reserve variable boot area 5% of System RAM
250eb39c880SMahesh Salgaonkar  *
251eb39c880SMahesh Salgaonkar  * Function to find the largest memory size we need to reserve during early
252eb39c880SMahesh Salgaonkar  * boot process. This will be the size of the memory that is required for a
253eb39c880SMahesh Salgaonkar  * kernel to boot successfully.
254eb39c880SMahesh Salgaonkar  *
255eb39c880SMahesh Salgaonkar  * This function has been taken from phyp-assisted dump feature implementation.
256eb39c880SMahesh Salgaonkar  *
257eb39c880SMahesh Salgaonkar  * returns larger of 256MB or 5% rounded down to multiples of 256MB.
258eb39c880SMahesh Salgaonkar  *
259eb39c880SMahesh Salgaonkar  * TODO: Come up with better approach to find out more accurate memory size
260eb39c880SMahesh Salgaonkar  * that is required for a kernel to boot successfully.
261eb39c880SMahesh Salgaonkar  *
262eb39c880SMahesh Salgaonkar  */
263eb39c880SMahesh Salgaonkar static inline unsigned long fadump_calculate_reserve_size(void)
264eb39c880SMahesh Salgaonkar {
26511550dc0SHari Bathini 	int ret;
26611550dc0SHari Bathini 	unsigned long long base, size;
267eb39c880SMahesh Salgaonkar 
26881d9eca5SHari Bathini 	if (fw_dump.reserve_bootvar)
26981d9eca5SHari Bathini 		pr_warn("'fadump_reserve_mem=' parameter is deprecated in favor of 'crashkernel=' parameter.\n");
27081d9eca5SHari Bathini 
271eb39c880SMahesh Salgaonkar 	/*
27211550dc0SHari Bathini 	 * Check if the size is specified through crashkernel= cmdline
273e7467dc6SHari Bathini 	 * option. If yes, then use that but ignore base as fadump reserves
274e7467dc6SHari Bathini 	 * memory at a predefined offset.
275eb39c880SMahesh Salgaonkar 	 */
27611550dc0SHari Bathini 	ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
27711550dc0SHari Bathini 				&size, &base);
27811550dc0SHari Bathini 	if (ret == 0 && size > 0) {
27948a316e3SHari Bathini 		unsigned long max_size;
28048a316e3SHari Bathini 
28181d9eca5SHari Bathini 		if (fw_dump.reserve_bootvar)
28281d9eca5SHari Bathini 			pr_info("Using 'crashkernel=' parameter for memory reservation.\n");
28381d9eca5SHari Bathini 
28411550dc0SHari Bathini 		fw_dump.reserve_bootvar = (unsigned long)size;
28548a316e3SHari Bathini 
28648a316e3SHari Bathini 		/*
28748a316e3SHari Bathini 		 * Adjust if the boot memory size specified is above
28848a316e3SHari Bathini 		 * the upper limit.
28948a316e3SHari Bathini 		 */
29048a316e3SHari Bathini 		max_size = memblock_phys_mem_size() / MAX_BOOT_MEM_RATIO;
29148a316e3SHari Bathini 		if (fw_dump.reserve_bootvar > max_size) {
29248a316e3SHari Bathini 			fw_dump.reserve_bootvar = max_size;
29348a316e3SHari Bathini 			pr_info("Adjusted boot memory size to %luMB\n",
29448a316e3SHari Bathini 				(fw_dump.reserve_bootvar >> 20));
29548a316e3SHari Bathini 		}
29648a316e3SHari Bathini 
297eb39c880SMahesh Salgaonkar 		return fw_dump.reserve_bootvar;
29881d9eca5SHari Bathini 	} else if (fw_dump.reserve_bootvar) {
29981d9eca5SHari Bathini 		/*
30081d9eca5SHari Bathini 		 * 'fadump_reserve_mem=' is being used to reserve memory
30181d9eca5SHari Bathini 		 * for firmware-assisted dump.
30281d9eca5SHari Bathini 		 */
30381d9eca5SHari Bathini 		return fw_dump.reserve_bootvar;
30411550dc0SHari Bathini 	}
305eb39c880SMahesh Salgaonkar 
306eb39c880SMahesh Salgaonkar 	/* divide by 20 to get 5% of value */
30748a316e3SHari Bathini 	size = memblock_phys_mem_size() / 20;
308eb39c880SMahesh Salgaonkar 
309eb39c880SMahesh Salgaonkar 	/* round it down in multiples of 256 */
310eb39c880SMahesh Salgaonkar 	size = size & ~0x0FFFFFFFUL;
311eb39c880SMahesh Salgaonkar 
312eb39c880SMahesh Salgaonkar 	/* Truncate to memory_limit. We don't want to over reserve the memory.*/
313eb39c880SMahesh Salgaonkar 	if (memory_limit && size > memory_limit)
314eb39c880SMahesh Salgaonkar 		size = memory_limit;
315eb39c880SMahesh Salgaonkar 
316eb39c880SMahesh Salgaonkar 	return (size > MIN_BOOT_MEM ? size : MIN_BOOT_MEM);
317eb39c880SMahesh Salgaonkar }
318eb39c880SMahesh Salgaonkar 
319eb39c880SMahesh Salgaonkar /*
320eb39c880SMahesh Salgaonkar  * Calculate the total memory size required to be reserved for
321eb39c880SMahesh Salgaonkar  * firmware-assisted dump registration.
322eb39c880SMahesh Salgaonkar  */
323eb39c880SMahesh Salgaonkar static unsigned long get_fadump_area_size(void)
324eb39c880SMahesh Salgaonkar {
325eb39c880SMahesh Salgaonkar 	unsigned long size = 0;
326eb39c880SMahesh Salgaonkar 
327eb39c880SMahesh Salgaonkar 	size += fw_dump.cpu_state_data_size;
328eb39c880SMahesh Salgaonkar 	size += fw_dump.hpte_region_size;
329eb39c880SMahesh Salgaonkar 	size += fw_dump.boot_memory_size;
3302df173d9SMahesh Salgaonkar 	size += sizeof(struct fadump_crash_info_header);
3312df173d9SMahesh Salgaonkar 	size += sizeof(struct elfhdr); /* ELF core header.*/
332ebaeb5aeSMahesh Salgaonkar 	size += sizeof(struct elf_phdr); /* place holder for cpu notes */
3332df173d9SMahesh Salgaonkar 	/* Program headers for crash memory regions. */
3342df173d9SMahesh Salgaonkar 	size += sizeof(struct elf_phdr) * (memblock_num_regions(memory) + 2);
335eb39c880SMahesh Salgaonkar 
336eb39c880SMahesh Salgaonkar 	size = PAGE_ALIGN(size);
337eb39c880SMahesh Salgaonkar 	return size;
338eb39c880SMahesh Salgaonkar }
339eb39c880SMahesh Salgaonkar 
340b71a693dSMahesh Salgaonkar static void __init fadump_reserve_crash_area(unsigned long base,
341b71a693dSMahesh Salgaonkar 					     unsigned long size)
342b71a693dSMahesh Salgaonkar {
343b71a693dSMahesh Salgaonkar 	struct memblock_region *reg;
344b71a693dSMahesh Salgaonkar 	unsigned long mstart, mend, msize;
345b71a693dSMahesh Salgaonkar 
346b71a693dSMahesh Salgaonkar 	for_each_memblock(memory, reg) {
347b71a693dSMahesh Salgaonkar 		mstart = max_t(unsigned long, base, reg->base);
348b71a693dSMahesh Salgaonkar 		mend = reg->base + reg->size;
349b71a693dSMahesh Salgaonkar 		mend = min(base + size, mend);
350b71a693dSMahesh Salgaonkar 
351b71a693dSMahesh Salgaonkar 		if (mstart < mend) {
352b71a693dSMahesh Salgaonkar 			msize = mend - mstart;
353b71a693dSMahesh Salgaonkar 			memblock_reserve(mstart, msize);
354b71a693dSMahesh Salgaonkar 			pr_info("Reserved %ldMB of memory at %#016lx for saving crash dump\n",
355b71a693dSMahesh Salgaonkar 				(msize >> 20), mstart);
356b71a693dSMahesh Salgaonkar 		}
357b71a693dSMahesh Salgaonkar 	}
358b71a693dSMahesh Salgaonkar }
359b71a693dSMahesh Salgaonkar 
360eb39c880SMahesh Salgaonkar int __init fadump_reserve_mem(void)
361eb39c880SMahesh Salgaonkar {
362eb39c880SMahesh Salgaonkar 	unsigned long base, size, memory_boundary;
363eb39c880SMahesh Salgaonkar 
364eb39c880SMahesh Salgaonkar 	if (!fw_dump.fadump_enabled)
365eb39c880SMahesh Salgaonkar 		return 0;
366eb39c880SMahesh Salgaonkar 
367eb39c880SMahesh Salgaonkar 	if (!fw_dump.fadump_supported) {
368eb39c880SMahesh Salgaonkar 		printk(KERN_INFO "Firmware-assisted dump is not supported on"
369eb39c880SMahesh Salgaonkar 				" this hardware\n");
370eb39c880SMahesh Salgaonkar 		fw_dump.fadump_enabled = 0;
371eb39c880SMahesh Salgaonkar 		return 0;
372eb39c880SMahesh Salgaonkar 	}
3733ccc00a7SMahesh Salgaonkar 	/*
3743ccc00a7SMahesh Salgaonkar 	 * Initialize boot memory size
3753ccc00a7SMahesh Salgaonkar 	 * If dump is active then we have already calculated the size during
3763ccc00a7SMahesh Salgaonkar 	 * first kernel.
3773ccc00a7SMahesh Salgaonkar 	 */
3783ccc00a7SMahesh Salgaonkar 	if (fdm_active)
379408cddd9SHari Bathini 		fw_dump.boot_memory_size = be64_to_cpu(fdm_active->rmr_region.source_len);
3803ccc00a7SMahesh Salgaonkar 	else
381eb39c880SMahesh Salgaonkar 		fw_dump.boot_memory_size = fadump_calculate_reserve_size();
382eb39c880SMahesh Salgaonkar 
383eb39c880SMahesh Salgaonkar 	/*
384eb39c880SMahesh Salgaonkar 	 * Calculate the memory boundary.
385eb39c880SMahesh Salgaonkar 	 * If memory_limit is less than actual memory boundary then reserve
386eb39c880SMahesh Salgaonkar 	 * the memory for fadump beyond the memory_limit and adjust the
387eb39c880SMahesh Salgaonkar 	 * memory_limit accordingly, so that the running kernel can run with
388eb39c880SMahesh Salgaonkar 	 * specified memory_limit.
389eb39c880SMahesh Salgaonkar 	 */
390eb39c880SMahesh Salgaonkar 	if (memory_limit && memory_limit < memblock_end_of_DRAM()) {
391eb39c880SMahesh Salgaonkar 		size = get_fadump_area_size();
392eb39c880SMahesh Salgaonkar 		if ((memory_limit + size) < memblock_end_of_DRAM())
393eb39c880SMahesh Salgaonkar 			memory_limit += size;
394eb39c880SMahesh Salgaonkar 		else
395eb39c880SMahesh Salgaonkar 			memory_limit = memblock_end_of_DRAM();
396eb39c880SMahesh Salgaonkar 		printk(KERN_INFO "Adjusted memory_limit for firmware-assisted"
397a84fcd46SSuzuki Poulose 				" dump, now %#016llx\n", memory_limit);
398eb39c880SMahesh Salgaonkar 	}
399eb39c880SMahesh Salgaonkar 	if (memory_limit)
400eb39c880SMahesh Salgaonkar 		memory_boundary = memory_limit;
401eb39c880SMahesh Salgaonkar 	else
402eb39c880SMahesh Salgaonkar 		memory_boundary = memblock_end_of_DRAM();
403eb39c880SMahesh Salgaonkar 
404eb39c880SMahesh Salgaonkar 	if (fw_dump.dump_active) {
405b71a693dSMahesh Salgaonkar 		pr_info("Firmware-assisted dump is active.\n");
406b71a693dSMahesh Salgaonkar 
40785975387SHari Bathini #ifdef CONFIG_HUGETLB_PAGE
40885975387SHari Bathini 		/*
40985975387SHari Bathini 		 * FADump capture kernel doesn't care much about hugepages.
41085975387SHari Bathini 		 * In fact, handling hugepages in capture kernel is asking for
41185975387SHari Bathini 		 * trouble. So, disable HugeTLB support when fadump is active.
41285975387SHari Bathini 		 */
41385975387SHari Bathini 		hugetlb_disabled = true;
41485975387SHari Bathini #endif
415eb39c880SMahesh Salgaonkar 		/*
416eb39c880SMahesh Salgaonkar 		 * If last boot has crashed then reserve all the memory
417eb39c880SMahesh Salgaonkar 		 * above boot_memory_size so that we don't touch it until
418eb39c880SMahesh Salgaonkar 		 * dump is written to disk by userspace tool. This memory
419eb39c880SMahesh Salgaonkar 		 * will be released for general use once the dump is saved.
420eb39c880SMahesh Salgaonkar 		 */
421eb39c880SMahesh Salgaonkar 		base = fw_dump.boot_memory_size;
422eb39c880SMahesh Salgaonkar 		size = memory_boundary - base;
423b71a693dSMahesh Salgaonkar 		fadump_reserve_crash_area(base, size);
4242df173d9SMahesh Salgaonkar 
4252df173d9SMahesh Salgaonkar 		fw_dump.fadumphdr_addr =
426408cddd9SHari Bathini 				be64_to_cpu(fdm_active->rmr_region.destination_address) +
427408cddd9SHari Bathini 				be64_to_cpu(fdm_active->rmr_region.source_len);
4282df173d9SMahesh Salgaonkar 		pr_debug("fadumphdr_addr = %p\n",
4292df173d9SMahesh Salgaonkar 				(void *) fw_dump.fadumphdr_addr);
430eb39c880SMahesh Salgaonkar 	} else {
431eb39c880SMahesh Salgaonkar 		size = get_fadump_area_size();
432f6e6bedbSHari Bathini 
433f6e6bedbSHari Bathini 		/*
434f6e6bedbSHari Bathini 		 * Reserve memory at an offset closer to bottom of the RAM to
435f6e6bedbSHari Bathini 		 * minimize the impact of memory hot-remove operation. We can't
436f6e6bedbSHari Bathini 		 * use memblock_find_in_range() here since it doesn't allocate
437f6e6bedbSHari Bathini 		 * from bottom to top.
438f6e6bedbSHari Bathini 		 */
439f6e6bedbSHari Bathini 		for (base = fw_dump.boot_memory_size;
440f6e6bedbSHari Bathini 		     base <= (memory_boundary - size);
441f6e6bedbSHari Bathini 		     base += size) {
442f6e6bedbSHari Bathini 			if (memblock_is_region_memory(base, size) &&
443f6e6bedbSHari Bathini 			    !memblock_is_region_reserved(base, size))
444f6e6bedbSHari Bathini 				break;
445eb39c880SMahesh Salgaonkar 		}
446f6e6bedbSHari Bathini 		if ((base > (memory_boundary - size)) ||
447f6e6bedbSHari Bathini 		    memblock_reserve(base, size)) {
448f6e6bedbSHari Bathini 			pr_err("Failed to reserve memory\n");
449f6e6bedbSHari Bathini 			return 0;
450f6e6bedbSHari Bathini 		}
451f6e6bedbSHari Bathini 
452f6e6bedbSHari Bathini 		pr_info("Reserved %ldMB of memory at %ldMB for firmware-"
453f6e6bedbSHari Bathini 			"assisted dump (System RAM: %ldMB)\n",
454f6e6bedbSHari Bathini 			(unsigned long)(size >> 20),
455f6e6bedbSHari Bathini 			(unsigned long)(base >> 20),
456f6e6bedbSHari Bathini 			(unsigned long)(memblock_phys_mem_size() >> 20));
457f6e6bedbSHari Bathini 	}
458f6e6bedbSHari Bathini 
459eb39c880SMahesh Salgaonkar 	fw_dump.reserve_dump_area_start = base;
460eb39c880SMahesh Salgaonkar 	fw_dump.reserve_dump_area_size = size;
461eb39c880SMahesh Salgaonkar 	return 1;
462eb39c880SMahesh Salgaonkar }
463eb39c880SMahesh Salgaonkar 
4641e76609cSSrikar Dronamraju unsigned long __init arch_reserved_kernel_pages(void)
4651e76609cSSrikar Dronamraju {
4661e76609cSSrikar Dronamraju 	return memblock_reserved_size() / PAGE_SIZE;
4671e76609cSSrikar Dronamraju }
4681e76609cSSrikar Dronamraju 
469eb39c880SMahesh Salgaonkar /* Look for fadump= cmdline option. */
470eb39c880SMahesh Salgaonkar static int __init early_fadump_param(char *p)
471eb39c880SMahesh Salgaonkar {
472eb39c880SMahesh Salgaonkar 	if (!p)
473eb39c880SMahesh Salgaonkar 		return 1;
474eb39c880SMahesh Salgaonkar 
475eb39c880SMahesh Salgaonkar 	if (strncmp(p, "on", 2) == 0)
476eb39c880SMahesh Salgaonkar 		fw_dump.fadump_enabled = 1;
477eb39c880SMahesh Salgaonkar 	else if (strncmp(p, "off", 3) == 0)
478eb39c880SMahesh Salgaonkar 		fw_dump.fadump_enabled = 0;
479eb39c880SMahesh Salgaonkar 
480eb39c880SMahesh Salgaonkar 	return 0;
481eb39c880SMahesh Salgaonkar }
482eb39c880SMahesh Salgaonkar early_param("fadump", early_fadump_param);
483eb39c880SMahesh Salgaonkar 
48481d9eca5SHari Bathini /*
48581d9eca5SHari Bathini  * Look for fadump_reserve_mem= cmdline option
48681d9eca5SHari Bathini  * TODO: Remove references to 'fadump_reserve_mem=' parameter,
48781d9eca5SHari Bathini  *       the sooner 'crashkernel=' parameter is accustomed to.
48881d9eca5SHari Bathini  */
48981d9eca5SHari Bathini static int __init early_fadump_reserve_mem(char *p)
49081d9eca5SHari Bathini {
49181d9eca5SHari Bathini 	if (p)
49281d9eca5SHari Bathini 		fw_dump.reserve_bootvar = memparse(p, &p);
49381d9eca5SHari Bathini 	return 0;
49481d9eca5SHari Bathini }
49581d9eca5SHari Bathini early_param("fadump_reserve_mem", early_fadump_reserve_mem);
49681d9eca5SHari Bathini 
49798b8cd7fSMichal Suchanek static int register_fw_dump(struct fadump_mem_struct *fdm)
4983ccc00a7SMahesh Salgaonkar {
49998b8cd7fSMichal Suchanek 	int rc, err;
5003ccc00a7SMahesh Salgaonkar 	unsigned int wait_time;
5013ccc00a7SMahesh Salgaonkar 
5023ccc00a7SMahesh Salgaonkar 	pr_debug("Registering for firmware-assisted kernel dump...\n");
5033ccc00a7SMahesh Salgaonkar 
5043ccc00a7SMahesh Salgaonkar 	/* TODO: Add upper time limit for the delay */
5053ccc00a7SMahesh Salgaonkar 	do {
5063ccc00a7SMahesh Salgaonkar 		rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
5073ccc00a7SMahesh Salgaonkar 			FADUMP_REGISTER, fdm,
5083ccc00a7SMahesh Salgaonkar 			sizeof(struct fadump_mem_struct));
5093ccc00a7SMahesh Salgaonkar 
5103ccc00a7SMahesh Salgaonkar 		wait_time = rtas_busy_delay_time(rc);
5113ccc00a7SMahesh Salgaonkar 		if (wait_time)
5123ccc00a7SMahesh Salgaonkar 			mdelay(wait_time);
5133ccc00a7SMahesh Salgaonkar 
5143ccc00a7SMahesh Salgaonkar 	} while (wait_time);
5153ccc00a7SMahesh Salgaonkar 
51698b8cd7fSMichal Suchanek 	err = -EIO;
5173ccc00a7SMahesh Salgaonkar 	switch (rc) {
51898b8cd7fSMichal Suchanek 	default:
51998b8cd7fSMichal Suchanek 		pr_err("Failed to register. Unknown Error(%d).\n", rc);
52098b8cd7fSMichal Suchanek 		break;
5213ccc00a7SMahesh Salgaonkar 	case -1:
5223ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "Failed to register firmware-assisted kernel"
5233ccc00a7SMahesh Salgaonkar 			" dump. Hardware Error(%d).\n", rc);
5243ccc00a7SMahesh Salgaonkar 		break;
5253ccc00a7SMahesh Salgaonkar 	case -3:
526a5a05b91SHari Bathini 		if (!is_boot_memory_area_contiguous())
527a5a05b91SHari Bathini 			pr_err("Can't have holes in boot memory area while "
528a5a05b91SHari Bathini 			       "registering fadump\n");
529a5a05b91SHari Bathini 
5303ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "Failed to register firmware-assisted kernel"
5313ccc00a7SMahesh Salgaonkar 			" dump. Parameter Error(%d).\n", rc);
53298b8cd7fSMichal Suchanek 		err = -EINVAL;
5333ccc00a7SMahesh Salgaonkar 		break;
5343ccc00a7SMahesh Salgaonkar 	case -9:
5353ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "firmware-assisted kernel dump is already "
5363ccc00a7SMahesh Salgaonkar 			" registered.");
5373ccc00a7SMahesh Salgaonkar 		fw_dump.dump_registered = 1;
53898b8cd7fSMichal Suchanek 		err = -EEXIST;
5393ccc00a7SMahesh Salgaonkar 		break;
5403ccc00a7SMahesh Salgaonkar 	case 0:
5413ccc00a7SMahesh Salgaonkar 		printk(KERN_INFO "firmware-assisted kernel dump registration"
5423ccc00a7SMahesh Salgaonkar 			" is successful\n");
5433ccc00a7SMahesh Salgaonkar 		fw_dump.dump_registered = 1;
54498b8cd7fSMichal Suchanek 		err = 0;
5453ccc00a7SMahesh Salgaonkar 		break;
5463ccc00a7SMahesh Salgaonkar 	}
54798b8cd7fSMichal Suchanek 	return err;
5483ccc00a7SMahesh Salgaonkar }
5493ccc00a7SMahesh Salgaonkar 
550ebaeb5aeSMahesh Salgaonkar void crash_fadump(struct pt_regs *regs, const char *str)
551ebaeb5aeSMahesh Salgaonkar {
552ebaeb5aeSMahesh Salgaonkar 	struct fadump_crash_info_header *fdh = NULL;
553f2a5e8f0SMahesh Salgaonkar 	int old_cpu, this_cpu;
554ebaeb5aeSMahesh Salgaonkar 
5556fcd6baaSNicholas Piggin 	if (!should_fadump_crash())
556ebaeb5aeSMahesh Salgaonkar 		return;
557ebaeb5aeSMahesh Salgaonkar 
558f2a5e8f0SMahesh Salgaonkar 	/*
559f2a5e8f0SMahesh Salgaonkar 	 * old_cpu == -1 means this is the first CPU which has come here,
560f2a5e8f0SMahesh Salgaonkar 	 * go ahead and trigger fadump.
561f2a5e8f0SMahesh Salgaonkar 	 *
562f2a5e8f0SMahesh Salgaonkar 	 * old_cpu != -1 means some other CPU has already on it's way
563f2a5e8f0SMahesh Salgaonkar 	 * to trigger fadump, just keep looping here.
564f2a5e8f0SMahesh Salgaonkar 	 */
565f2a5e8f0SMahesh Salgaonkar 	this_cpu = smp_processor_id();
566f2a5e8f0SMahesh Salgaonkar 	old_cpu = cmpxchg(&crashing_cpu, -1, this_cpu);
567f2a5e8f0SMahesh Salgaonkar 
568f2a5e8f0SMahesh Salgaonkar 	if (old_cpu != -1) {
569f2a5e8f0SMahesh Salgaonkar 		/*
570f2a5e8f0SMahesh Salgaonkar 		 * We can't loop here indefinitely. Wait as long as fadump
571f2a5e8f0SMahesh Salgaonkar 		 * is in force. If we race with fadump un-registration this
572f2a5e8f0SMahesh Salgaonkar 		 * loop will break and then we go down to normal panic path
573f2a5e8f0SMahesh Salgaonkar 		 * and reboot. If fadump is in force the first crashing
574f2a5e8f0SMahesh Salgaonkar 		 * cpu will definitely trigger fadump.
575f2a5e8f0SMahesh Salgaonkar 		 */
576f2a5e8f0SMahesh Salgaonkar 		while (fw_dump.dump_registered)
577f2a5e8f0SMahesh Salgaonkar 			cpu_relax();
578f2a5e8f0SMahesh Salgaonkar 		return;
579f2a5e8f0SMahesh Salgaonkar 	}
580f2a5e8f0SMahesh Salgaonkar 
581ebaeb5aeSMahesh Salgaonkar 	fdh = __va(fw_dump.fadumphdr_addr);
582ebaeb5aeSMahesh Salgaonkar 	fdh->crashing_cpu = crashing_cpu;
583ebaeb5aeSMahesh Salgaonkar 	crash_save_vmcoreinfo();
584ebaeb5aeSMahesh Salgaonkar 
585ebaeb5aeSMahesh Salgaonkar 	if (regs)
586ebaeb5aeSMahesh Salgaonkar 		fdh->regs = *regs;
587ebaeb5aeSMahesh Salgaonkar 	else
588ebaeb5aeSMahesh Salgaonkar 		ppc_save_regs(&fdh->regs);
589ebaeb5aeSMahesh Salgaonkar 
590a0512164SRasmus Villemoes 	fdh->online_mask = *cpu_online_mask;
591ebaeb5aeSMahesh Salgaonkar 
592ebaeb5aeSMahesh Salgaonkar 	/* Call ibm,os-term rtas call to trigger firmware assisted dump */
593ebaeb5aeSMahesh Salgaonkar 	rtas_os_term((char *)str);
594ebaeb5aeSMahesh Salgaonkar }
595ebaeb5aeSMahesh Salgaonkar 
596ebaeb5aeSMahesh Salgaonkar #define GPR_MASK	0xffffff0000000000
597ebaeb5aeSMahesh Salgaonkar static inline int fadump_gpr_index(u64 id)
598ebaeb5aeSMahesh Salgaonkar {
599ebaeb5aeSMahesh Salgaonkar 	int i = -1;
600ebaeb5aeSMahesh Salgaonkar 	char str[3];
601ebaeb5aeSMahesh Salgaonkar 
602ebaeb5aeSMahesh Salgaonkar 	if ((id & GPR_MASK) == REG_ID("GPR")) {
603ebaeb5aeSMahesh Salgaonkar 		/* get the digits at the end */
604ebaeb5aeSMahesh Salgaonkar 		id &= ~GPR_MASK;
605ebaeb5aeSMahesh Salgaonkar 		id >>= 24;
606ebaeb5aeSMahesh Salgaonkar 		str[2] = '\0';
607ebaeb5aeSMahesh Salgaonkar 		str[1] = id & 0xff;
608ebaeb5aeSMahesh Salgaonkar 		str[0] = (id >> 8) & 0xff;
609ebaeb5aeSMahesh Salgaonkar 		sscanf(str, "%d", &i);
610ebaeb5aeSMahesh Salgaonkar 		if (i > 31)
611ebaeb5aeSMahesh Salgaonkar 			i = -1;
612ebaeb5aeSMahesh Salgaonkar 	}
613ebaeb5aeSMahesh Salgaonkar 	return i;
614ebaeb5aeSMahesh Salgaonkar }
615ebaeb5aeSMahesh Salgaonkar 
616ebaeb5aeSMahesh Salgaonkar static inline void fadump_set_regval(struct pt_regs *regs, u64 reg_id,
617ebaeb5aeSMahesh Salgaonkar 								u64 reg_val)
618ebaeb5aeSMahesh Salgaonkar {
619ebaeb5aeSMahesh Salgaonkar 	int i;
620ebaeb5aeSMahesh Salgaonkar 
621ebaeb5aeSMahesh Salgaonkar 	i = fadump_gpr_index(reg_id);
622ebaeb5aeSMahesh Salgaonkar 	if (i >= 0)
623ebaeb5aeSMahesh Salgaonkar 		regs->gpr[i] = (unsigned long)reg_val;
624ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("NIA"))
625ebaeb5aeSMahesh Salgaonkar 		regs->nip = (unsigned long)reg_val;
626ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("MSR"))
627ebaeb5aeSMahesh Salgaonkar 		regs->msr = (unsigned long)reg_val;
628ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("CTR"))
629ebaeb5aeSMahesh Salgaonkar 		regs->ctr = (unsigned long)reg_val;
630ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("LR"))
631ebaeb5aeSMahesh Salgaonkar 		regs->link = (unsigned long)reg_val;
632ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("XER"))
633ebaeb5aeSMahesh Salgaonkar 		regs->xer = (unsigned long)reg_val;
634ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("CR"))
635ebaeb5aeSMahesh Salgaonkar 		regs->ccr = (unsigned long)reg_val;
636ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("DAR"))
637ebaeb5aeSMahesh Salgaonkar 		regs->dar = (unsigned long)reg_val;
638ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("DSISR"))
639ebaeb5aeSMahesh Salgaonkar 		regs->dsisr = (unsigned long)reg_val;
640ebaeb5aeSMahesh Salgaonkar }
641ebaeb5aeSMahesh Salgaonkar 
642ebaeb5aeSMahesh Salgaonkar static struct fadump_reg_entry*
643ebaeb5aeSMahesh Salgaonkar fadump_read_registers(struct fadump_reg_entry *reg_entry, struct pt_regs *regs)
644ebaeb5aeSMahesh Salgaonkar {
645ebaeb5aeSMahesh Salgaonkar 	memset(regs, 0, sizeof(struct pt_regs));
646ebaeb5aeSMahesh Salgaonkar 
647408cddd9SHari Bathini 	while (be64_to_cpu(reg_entry->reg_id) != REG_ID("CPUEND")) {
648408cddd9SHari Bathini 		fadump_set_regval(regs, be64_to_cpu(reg_entry->reg_id),
649408cddd9SHari Bathini 					be64_to_cpu(reg_entry->reg_value));
650ebaeb5aeSMahesh Salgaonkar 		reg_entry++;
651ebaeb5aeSMahesh Salgaonkar 	}
652ebaeb5aeSMahesh Salgaonkar 	reg_entry++;
653ebaeb5aeSMahesh Salgaonkar 	return reg_entry;
654ebaeb5aeSMahesh Salgaonkar }
655ebaeb5aeSMahesh Salgaonkar 
656ebaeb5aeSMahesh Salgaonkar static u32 *fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs)
657ebaeb5aeSMahesh Salgaonkar {
658ebaeb5aeSMahesh Salgaonkar 	struct elf_prstatus prstatus;
659ebaeb5aeSMahesh Salgaonkar 
660ebaeb5aeSMahesh Salgaonkar 	memset(&prstatus, 0, sizeof(prstatus));
661ebaeb5aeSMahesh Salgaonkar 	/*
662ebaeb5aeSMahesh Salgaonkar 	 * FIXME: How do i get PID? Do I really need it?
663ebaeb5aeSMahesh Salgaonkar 	 * prstatus.pr_pid = ????
664ebaeb5aeSMahesh Salgaonkar 	 */
665ebaeb5aeSMahesh Salgaonkar 	elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
66622bd0177SHari Bathini 	buf = append_elf_note(buf, CRASH_CORE_NOTE_NAME, NT_PRSTATUS,
667ebaeb5aeSMahesh Salgaonkar 			      &prstatus, sizeof(prstatus));
668ebaeb5aeSMahesh Salgaonkar 	return buf;
669ebaeb5aeSMahesh Salgaonkar }
670ebaeb5aeSMahesh Salgaonkar 
671ebaeb5aeSMahesh Salgaonkar static void fadump_update_elfcore_header(char *bufp)
672ebaeb5aeSMahesh Salgaonkar {
673ebaeb5aeSMahesh Salgaonkar 	struct elfhdr *elf;
674ebaeb5aeSMahesh Salgaonkar 	struct elf_phdr *phdr;
675ebaeb5aeSMahesh Salgaonkar 
676ebaeb5aeSMahesh Salgaonkar 	elf = (struct elfhdr *)bufp;
677ebaeb5aeSMahesh Salgaonkar 	bufp += sizeof(struct elfhdr);
678ebaeb5aeSMahesh Salgaonkar 
679ebaeb5aeSMahesh Salgaonkar 	/* First note is a place holder for cpu notes info. */
680ebaeb5aeSMahesh Salgaonkar 	phdr = (struct elf_phdr *)bufp;
681ebaeb5aeSMahesh Salgaonkar 
682ebaeb5aeSMahesh Salgaonkar 	if (phdr->p_type == PT_NOTE) {
683ebaeb5aeSMahesh Salgaonkar 		phdr->p_paddr = fw_dump.cpu_notes_buf;
684ebaeb5aeSMahesh Salgaonkar 		phdr->p_offset	= phdr->p_paddr;
685ebaeb5aeSMahesh Salgaonkar 		phdr->p_filesz	= fw_dump.cpu_notes_buf_size;
686ebaeb5aeSMahesh Salgaonkar 		phdr->p_memsz = fw_dump.cpu_notes_buf_size;
687ebaeb5aeSMahesh Salgaonkar 	}
688ebaeb5aeSMahesh Salgaonkar 	return;
689ebaeb5aeSMahesh Salgaonkar }
690ebaeb5aeSMahesh Salgaonkar 
691ebaeb5aeSMahesh Salgaonkar static void *fadump_cpu_notes_buf_alloc(unsigned long size)
692ebaeb5aeSMahesh Salgaonkar {
693ebaeb5aeSMahesh Salgaonkar 	void *vaddr;
694ebaeb5aeSMahesh Salgaonkar 	struct page *page;
695ebaeb5aeSMahesh Salgaonkar 	unsigned long order, count, i;
696ebaeb5aeSMahesh Salgaonkar 
697ebaeb5aeSMahesh Salgaonkar 	order = get_order(size);
698ebaeb5aeSMahesh Salgaonkar 	vaddr = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
699ebaeb5aeSMahesh Salgaonkar 	if (!vaddr)
700ebaeb5aeSMahesh Salgaonkar 		return NULL;
701ebaeb5aeSMahesh Salgaonkar 
702ebaeb5aeSMahesh Salgaonkar 	count = 1 << order;
703ebaeb5aeSMahesh Salgaonkar 	page = virt_to_page(vaddr);
704ebaeb5aeSMahesh Salgaonkar 	for (i = 0; i < count; i++)
705ebaeb5aeSMahesh Salgaonkar 		SetPageReserved(page + i);
706ebaeb5aeSMahesh Salgaonkar 	return vaddr;
707ebaeb5aeSMahesh Salgaonkar }
708ebaeb5aeSMahesh Salgaonkar 
709ebaeb5aeSMahesh Salgaonkar static void fadump_cpu_notes_buf_free(unsigned long vaddr, unsigned long size)
710ebaeb5aeSMahesh Salgaonkar {
711ebaeb5aeSMahesh Salgaonkar 	struct page *page;
712ebaeb5aeSMahesh Salgaonkar 	unsigned long order, count, i;
713ebaeb5aeSMahesh Salgaonkar 
714ebaeb5aeSMahesh Salgaonkar 	order = get_order(size);
715ebaeb5aeSMahesh Salgaonkar 	count = 1 << order;
716ebaeb5aeSMahesh Salgaonkar 	page = virt_to_page(vaddr);
717ebaeb5aeSMahesh Salgaonkar 	for (i = 0; i < count; i++)
718ebaeb5aeSMahesh Salgaonkar 		ClearPageReserved(page + i);
719ebaeb5aeSMahesh Salgaonkar 	__free_pages(page, order);
720ebaeb5aeSMahesh Salgaonkar }
721ebaeb5aeSMahesh Salgaonkar 
722ebaeb5aeSMahesh Salgaonkar /*
723ebaeb5aeSMahesh Salgaonkar  * Read CPU state dump data and convert it into ELF notes.
724ebaeb5aeSMahesh Salgaonkar  * The CPU dump starts with magic number "REGSAVE". NumCpusOffset should be
725ebaeb5aeSMahesh Salgaonkar  * used to access the data to allow for additional fields to be added without
726ebaeb5aeSMahesh Salgaonkar  * affecting compatibility. Each list of registers for a CPU starts with
727ebaeb5aeSMahesh Salgaonkar  * "CPUSTRT" and ends with "CPUEND". Each register entry is of 16 bytes,
728ebaeb5aeSMahesh Salgaonkar  * 8 Byte ASCII identifier and 8 Byte register value. The register entry
729ebaeb5aeSMahesh Salgaonkar  * with identifier "CPUSTRT" and "CPUEND" contains 4 byte cpu id as part
730ebaeb5aeSMahesh Salgaonkar  * of register value. For more details refer to PAPR document.
731ebaeb5aeSMahesh Salgaonkar  *
732ebaeb5aeSMahesh Salgaonkar  * Only for the crashing cpu we ignore the CPU dump data and get exact
733ebaeb5aeSMahesh Salgaonkar  * state from fadump crash info structure populated by first kernel at the
734ebaeb5aeSMahesh Salgaonkar  * time of crash.
735ebaeb5aeSMahesh Salgaonkar  */
736ebaeb5aeSMahesh Salgaonkar static int __init fadump_build_cpu_notes(const struct fadump_mem_struct *fdm)
737ebaeb5aeSMahesh Salgaonkar {
738ebaeb5aeSMahesh Salgaonkar 	struct fadump_reg_save_area_header *reg_header;
739ebaeb5aeSMahesh Salgaonkar 	struct fadump_reg_entry *reg_entry;
740ebaeb5aeSMahesh Salgaonkar 	struct fadump_crash_info_header *fdh = NULL;
741ebaeb5aeSMahesh Salgaonkar 	void *vaddr;
742ebaeb5aeSMahesh Salgaonkar 	unsigned long addr;
743ebaeb5aeSMahesh Salgaonkar 	u32 num_cpus, *note_buf;
744ebaeb5aeSMahesh Salgaonkar 	struct pt_regs regs;
745ebaeb5aeSMahesh Salgaonkar 	int i, rc = 0, cpu = 0;
746ebaeb5aeSMahesh Salgaonkar 
747ebaeb5aeSMahesh Salgaonkar 	if (!fdm->cpu_state_data.bytes_dumped)
748ebaeb5aeSMahesh Salgaonkar 		return -EINVAL;
749ebaeb5aeSMahesh Salgaonkar 
750408cddd9SHari Bathini 	addr = be64_to_cpu(fdm->cpu_state_data.destination_address);
751ebaeb5aeSMahesh Salgaonkar 	vaddr = __va(addr);
752ebaeb5aeSMahesh Salgaonkar 
753ebaeb5aeSMahesh Salgaonkar 	reg_header = vaddr;
754408cddd9SHari Bathini 	if (be64_to_cpu(reg_header->magic_number) != REGSAVE_AREA_MAGIC) {
755ebaeb5aeSMahesh Salgaonkar 		printk(KERN_ERR "Unable to read register save area.\n");
756ebaeb5aeSMahesh Salgaonkar 		return -ENOENT;
757ebaeb5aeSMahesh Salgaonkar 	}
758ebaeb5aeSMahesh Salgaonkar 	pr_debug("--------CPU State Data------------\n");
759408cddd9SHari Bathini 	pr_debug("Magic Number: %llx\n", be64_to_cpu(reg_header->magic_number));
760408cddd9SHari Bathini 	pr_debug("NumCpuOffset: %x\n", be32_to_cpu(reg_header->num_cpu_offset));
761ebaeb5aeSMahesh Salgaonkar 
762408cddd9SHari Bathini 	vaddr += be32_to_cpu(reg_header->num_cpu_offset);
763408cddd9SHari Bathini 	num_cpus = be32_to_cpu(*((__be32 *)(vaddr)));
764ebaeb5aeSMahesh Salgaonkar 	pr_debug("NumCpus     : %u\n", num_cpus);
765ebaeb5aeSMahesh Salgaonkar 	vaddr += sizeof(u32);
766ebaeb5aeSMahesh Salgaonkar 	reg_entry = (struct fadump_reg_entry *)vaddr;
767ebaeb5aeSMahesh Salgaonkar 
768ebaeb5aeSMahesh Salgaonkar 	/* Allocate buffer to hold cpu crash notes. */
769ebaeb5aeSMahesh Salgaonkar 	fw_dump.cpu_notes_buf_size = num_cpus * sizeof(note_buf_t);
770ebaeb5aeSMahesh Salgaonkar 	fw_dump.cpu_notes_buf_size = PAGE_ALIGN(fw_dump.cpu_notes_buf_size);
771ebaeb5aeSMahesh Salgaonkar 	note_buf = fadump_cpu_notes_buf_alloc(fw_dump.cpu_notes_buf_size);
772ebaeb5aeSMahesh Salgaonkar 	if (!note_buf) {
773ebaeb5aeSMahesh Salgaonkar 		printk(KERN_ERR "Failed to allocate 0x%lx bytes for "
774ebaeb5aeSMahesh Salgaonkar 			"cpu notes buffer\n", fw_dump.cpu_notes_buf_size);
775ebaeb5aeSMahesh Salgaonkar 		return -ENOMEM;
776ebaeb5aeSMahesh Salgaonkar 	}
777ebaeb5aeSMahesh Salgaonkar 	fw_dump.cpu_notes_buf = __pa(note_buf);
778ebaeb5aeSMahesh Salgaonkar 
779ebaeb5aeSMahesh Salgaonkar 	pr_debug("Allocated buffer for cpu notes of size %ld at %p\n",
780ebaeb5aeSMahesh Salgaonkar 			(num_cpus * sizeof(note_buf_t)), note_buf);
781ebaeb5aeSMahesh Salgaonkar 
782ebaeb5aeSMahesh Salgaonkar 	if (fw_dump.fadumphdr_addr)
783ebaeb5aeSMahesh Salgaonkar 		fdh = __va(fw_dump.fadumphdr_addr);
784ebaeb5aeSMahesh Salgaonkar 
785ebaeb5aeSMahesh Salgaonkar 	for (i = 0; i < num_cpus; i++) {
786408cddd9SHari Bathini 		if (be64_to_cpu(reg_entry->reg_id) != REG_ID("CPUSTRT")) {
787ebaeb5aeSMahesh Salgaonkar 			printk(KERN_ERR "Unable to read CPU state data\n");
788ebaeb5aeSMahesh Salgaonkar 			rc = -ENOENT;
789ebaeb5aeSMahesh Salgaonkar 			goto error_out;
790ebaeb5aeSMahesh Salgaonkar 		}
791ebaeb5aeSMahesh Salgaonkar 		/* Lower 4 bytes of reg_value contains logical cpu id */
792408cddd9SHari Bathini 		cpu = be64_to_cpu(reg_entry->reg_value) & FADUMP_CPU_ID_MASK;
793a0512164SRasmus Villemoes 		if (fdh && !cpumask_test_cpu(cpu, &fdh->online_mask)) {
794ebaeb5aeSMahesh Salgaonkar 			SKIP_TO_NEXT_CPU(reg_entry);
795ebaeb5aeSMahesh Salgaonkar 			continue;
796ebaeb5aeSMahesh Salgaonkar 		}
797ebaeb5aeSMahesh Salgaonkar 		pr_debug("Reading register data for cpu %d...\n", cpu);
798ebaeb5aeSMahesh Salgaonkar 		if (fdh && fdh->crashing_cpu == cpu) {
799ebaeb5aeSMahesh Salgaonkar 			regs = fdh->regs;
800ebaeb5aeSMahesh Salgaonkar 			note_buf = fadump_regs_to_elf_notes(note_buf, &regs);
801ebaeb5aeSMahesh Salgaonkar 			SKIP_TO_NEXT_CPU(reg_entry);
802ebaeb5aeSMahesh Salgaonkar 		} else {
803ebaeb5aeSMahesh Salgaonkar 			reg_entry++;
804ebaeb5aeSMahesh Salgaonkar 			reg_entry = fadump_read_registers(reg_entry, &regs);
805ebaeb5aeSMahesh Salgaonkar 			note_buf = fadump_regs_to_elf_notes(note_buf, &regs);
806ebaeb5aeSMahesh Salgaonkar 		}
807ebaeb5aeSMahesh Salgaonkar 	}
80822bd0177SHari Bathini 	final_note(note_buf);
809ebaeb5aeSMahesh Salgaonkar 
810b717d985SRickard Strandqvist 	if (fdh) {
811ebaeb5aeSMahesh Salgaonkar 		pr_debug("Updating elfcore header (%llx) with cpu notes\n",
812ebaeb5aeSMahesh Salgaonkar 							fdh->elfcorehdr_addr);
813ebaeb5aeSMahesh Salgaonkar 		fadump_update_elfcore_header((char *)__va(fdh->elfcorehdr_addr));
814b717d985SRickard Strandqvist 	}
815ebaeb5aeSMahesh Salgaonkar 	return 0;
816ebaeb5aeSMahesh Salgaonkar 
817ebaeb5aeSMahesh Salgaonkar error_out:
818ebaeb5aeSMahesh Salgaonkar 	fadump_cpu_notes_buf_free((unsigned long)__va(fw_dump.cpu_notes_buf),
819ebaeb5aeSMahesh Salgaonkar 					fw_dump.cpu_notes_buf_size);
820ebaeb5aeSMahesh Salgaonkar 	fw_dump.cpu_notes_buf = 0;
821ebaeb5aeSMahesh Salgaonkar 	fw_dump.cpu_notes_buf_size = 0;
822ebaeb5aeSMahesh Salgaonkar 	return rc;
823ebaeb5aeSMahesh Salgaonkar 
824ebaeb5aeSMahesh Salgaonkar }
825ebaeb5aeSMahesh Salgaonkar 
8262df173d9SMahesh Salgaonkar /*
8272df173d9SMahesh Salgaonkar  * Validate and process the dump data stored by firmware before exporting
8282df173d9SMahesh Salgaonkar  * it through '/proc/vmcore'.
8292df173d9SMahesh Salgaonkar  */
8302df173d9SMahesh Salgaonkar static int __init process_fadump(const struct fadump_mem_struct *fdm_active)
8312df173d9SMahesh Salgaonkar {
8322df173d9SMahesh Salgaonkar 	struct fadump_crash_info_header *fdh;
833ebaeb5aeSMahesh Salgaonkar 	int rc = 0;
8342df173d9SMahesh Salgaonkar 
8352df173d9SMahesh Salgaonkar 	if (!fdm_active || !fw_dump.fadumphdr_addr)
8362df173d9SMahesh Salgaonkar 		return -EINVAL;
8372df173d9SMahesh Salgaonkar 
8382df173d9SMahesh Salgaonkar 	/* Check if the dump data is valid. */
839408cddd9SHari Bathini 	if ((be16_to_cpu(fdm_active->header.dump_status_flag) == FADUMP_ERROR_FLAG) ||
840ebaeb5aeSMahesh Salgaonkar 			(fdm_active->cpu_state_data.error_flags != 0) ||
8412df173d9SMahesh Salgaonkar 			(fdm_active->rmr_region.error_flags != 0)) {
8422df173d9SMahesh Salgaonkar 		printk(KERN_ERR "Dump taken by platform is not valid\n");
8432df173d9SMahesh Salgaonkar 		return -EINVAL;
8442df173d9SMahesh Salgaonkar 	}
845ebaeb5aeSMahesh Salgaonkar 	if ((fdm_active->rmr_region.bytes_dumped !=
846ebaeb5aeSMahesh Salgaonkar 			fdm_active->rmr_region.source_len) ||
847ebaeb5aeSMahesh Salgaonkar 			!fdm_active->cpu_state_data.bytes_dumped) {
8482df173d9SMahesh Salgaonkar 		printk(KERN_ERR "Dump taken by platform is incomplete\n");
8492df173d9SMahesh Salgaonkar 		return -EINVAL;
8502df173d9SMahesh Salgaonkar 	}
8512df173d9SMahesh Salgaonkar 
8522df173d9SMahesh Salgaonkar 	/* Validate the fadump crash info header */
8532df173d9SMahesh Salgaonkar 	fdh = __va(fw_dump.fadumphdr_addr);
8542df173d9SMahesh Salgaonkar 	if (fdh->magic_number != FADUMP_CRASH_INFO_MAGIC) {
8552df173d9SMahesh Salgaonkar 		printk(KERN_ERR "Crash info header is not valid.\n");
8562df173d9SMahesh Salgaonkar 		return -EINVAL;
8572df173d9SMahesh Salgaonkar 	}
8582df173d9SMahesh Salgaonkar 
859ebaeb5aeSMahesh Salgaonkar 	rc = fadump_build_cpu_notes(fdm_active);
860ebaeb5aeSMahesh Salgaonkar 	if (rc)
861ebaeb5aeSMahesh Salgaonkar 		return rc;
862ebaeb5aeSMahesh Salgaonkar 
8632df173d9SMahesh Salgaonkar 	/*
8642df173d9SMahesh Salgaonkar 	 * We are done validating dump info and elfcore header is now ready
8652df173d9SMahesh Salgaonkar 	 * to be exported. set elfcorehdr_addr so that vmcore module will
8662df173d9SMahesh Salgaonkar 	 * export the elfcore header through '/proc/vmcore'.
8672df173d9SMahesh Salgaonkar 	 */
8682df173d9SMahesh Salgaonkar 	elfcorehdr_addr = fdh->elfcorehdr_addr;
8692df173d9SMahesh Salgaonkar 
8702df173d9SMahesh Salgaonkar 	return 0;
8712df173d9SMahesh Salgaonkar }
8722df173d9SMahesh Salgaonkar 
8731bd6a1c4SHari Bathini static void free_crash_memory_ranges(void)
8741bd6a1c4SHari Bathini {
8751bd6a1c4SHari Bathini 	kfree(crash_memory_ranges);
8761bd6a1c4SHari Bathini 	crash_memory_ranges = NULL;
8771bd6a1c4SHari Bathini 	crash_memory_ranges_size = 0;
8781bd6a1c4SHari Bathini 	max_crash_mem_ranges = 0;
8791bd6a1c4SHari Bathini }
8801bd6a1c4SHari Bathini 
8811bd6a1c4SHari Bathini /*
8821bd6a1c4SHari Bathini  * Allocate or reallocate crash memory ranges array in incremental units
8831bd6a1c4SHari Bathini  * of PAGE_SIZE.
8841bd6a1c4SHari Bathini  */
8851bd6a1c4SHari Bathini static int allocate_crash_memory_ranges(void)
8861bd6a1c4SHari Bathini {
8871bd6a1c4SHari Bathini 	struct fad_crash_memory_ranges *new_array;
8881bd6a1c4SHari Bathini 	u64 new_size;
8891bd6a1c4SHari Bathini 
8901bd6a1c4SHari Bathini 	new_size = crash_memory_ranges_size + PAGE_SIZE;
8911bd6a1c4SHari Bathini 	pr_debug("Allocating %llu bytes of memory for crash memory ranges\n",
8921bd6a1c4SHari Bathini 		 new_size);
8931bd6a1c4SHari Bathini 
8941bd6a1c4SHari Bathini 	new_array = krealloc(crash_memory_ranges, new_size, GFP_KERNEL);
8951bd6a1c4SHari Bathini 	if (new_array == NULL) {
8961bd6a1c4SHari Bathini 		pr_err("Insufficient memory for setting up crash memory ranges\n");
8971bd6a1c4SHari Bathini 		free_crash_memory_ranges();
8981bd6a1c4SHari Bathini 		return -ENOMEM;
8991bd6a1c4SHari Bathini 	}
9001bd6a1c4SHari Bathini 
9011bd6a1c4SHari Bathini 	crash_memory_ranges = new_array;
9021bd6a1c4SHari Bathini 	crash_memory_ranges_size = new_size;
9031bd6a1c4SHari Bathini 	max_crash_mem_ranges = (new_size /
9041bd6a1c4SHari Bathini 				sizeof(struct fad_crash_memory_ranges));
9051bd6a1c4SHari Bathini 	return 0;
9061bd6a1c4SHari Bathini }
9071bd6a1c4SHari Bathini 
9081bd6a1c4SHari Bathini static inline int fadump_add_crash_memory(unsigned long long base,
9092df173d9SMahesh Salgaonkar 					  unsigned long long end)
9102df173d9SMahesh Salgaonkar {
911*ced1bf52SHari Bathini 	u64  start, size;
912*ced1bf52SHari Bathini 	bool is_adjacent = false;
913*ced1bf52SHari Bathini 
9142df173d9SMahesh Salgaonkar 	if (base == end)
9151bd6a1c4SHari Bathini 		return 0;
9161bd6a1c4SHari Bathini 
917*ced1bf52SHari Bathini 	/*
918*ced1bf52SHari Bathini 	 * Fold adjacent memory ranges to bring down the memory ranges/
919*ced1bf52SHari Bathini 	 * PT_LOAD segments count.
920*ced1bf52SHari Bathini 	 */
921*ced1bf52SHari Bathini 	if (crash_mem_ranges) {
922*ced1bf52SHari Bathini 		start = crash_memory_ranges[crash_mem_ranges - 1].base;
923*ced1bf52SHari Bathini 		size = crash_memory_ranges[crash_mem_ranges - 1].size;
924*ced1bf52SHari Bathini 
925*ced1bf52SHari Bathini 		if ((start + size) == base)
926*ced1bf52SHari Bathini 			is_adjacent = true;
927*ced1bf52SHari Bathini 	}
928*ced1bf52SHari Bathini 	if (!is_adjacent) {
929*ced1bf52SHari Bathini 		/* resize the array on reaching the limit */
9301bd6a1c4SHari Bathini 		if (crash_mem_ranges == max_crash_mem_ranges) {
9311bd6a1c4SHari Bathini 			int ret;
9321bd6a1c4SHari Bathini 
9331bd6a1c4SHari Bathini 			ret = allocate_crash_memory_ranges();
9341bd6a1c4SHari Bathini 			if (ret)
9351bd6a1c4SHari Bathini 				return ret;
9361bd6a1c4SHari Bathini 		}
9372df173d9SMahesh Salgaonkar 
938*ced1bf52SHari Bathini 		start = base;
939*ced1bf52SHari Bathini 		crash_memory_ranges[crash_mem_ranges].base = start;
9402df173d9SMahesh Salgaonkar 		crash_mem_ranges++;
941*ced1bf52SHari Bathini 	}
942*ced1bf52SHari Bathini 
943*ced1bf52SHari Bathini 	crash_memory_ranges[crash_mem_ranges - 1].size = (end - start);
944*ced1bf52SHari Bathini 	pr_debug("crash_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n",
945*ced1bf52SHari Bathini 		(crash_mem_ranges - 1), start, end - 1, (end - start));
9461bd6a1c4SHari Bathini 	return 0;
9472df173d9SMahesh Salgaonkar }
9482df173d9SMahesh Salgaonkar 
9491bd6a1c4SHari Bathini static int fadump_exclude_reserved_area(unsigned long long start,
9502df173d9SMahesh Salgaonkar 					unsigned long long end)
9512df173d9SMahesh Salgaonkar {
9522df173d9SMahesh Salgaonkar 	unsigned long long ra_start, ra_end;
9531bd6a1c4SHari Bathini 	int ret = 0;
9542df173d9SMahesh Salgaonkar 
9552df173d9SMahesh Salgaonkar 	ra_start = fw_dump.reserve_dump_area_start;
9562df173d9SMahesh Salgaonkar 	ra_end = ra_start + fw_dump.reserve_dump_area_size;
9572df173d9SMahesh Salgaonkar 
9582df173d9SMahesh Salgaonkar 	if ((ra_start < end) && (ra_end > start)) {
9592df173d9SMahesh Salgaonkar 		if ((start < ra_start) && (end > ra_end)) {
9601bd6a1c4SHari Bathini 			ret = fadump_add_crash_memory(start, ra_start);
9611bd6a1c4SHari Bathini 			if (ret)
9621bd6a1c4SHari Bathini 				return ret;
9631bd6a1c4SHari Bathini 
9641bd6a1c4SHari Bathini 			ret = fadump_add_crash_memory(ra_end, end);
9652df173d9SMahesh Salgaonkar 		} else if (start < ra_start) {
9661bd6a1c4SHari Bathini 			ret = fadump_add_crash_memory(start, ra_start);
9672df173d9SMahesh Salgaonkar 		} else if (ra_end < end) {
9681bd6a1c4SHari Bathini 			ret = fadump_add_crash_memory(ra_end, end);
9692df173d9SMahesh Salgaonkar 		}
9702df173d9SMahesh Salgaonkar 	} else
9711bd6a1c4SHari Bathini 		ret = fadump_add_crash_memory(start, end);
9721bd6a1c4SHari Bathini 
9731bd6a1c4SHari Bathini 	return ret;
9742df173d9SMahesh Salgaonkar }
9752df173d9SMahesh Salgaonkar 
9762df173d9SMahesh Salgaonkar static int fadump_init_elfcore_header(char *bufp)
9772df173d9SMahesh Salgaonkar {
9782df173d9SMahesh Salgaonkar 	struct elfhdr *elf;
9792df173d9SMahesh Salgaonkar 
9802df173d9SMahesh Salgaonkar 	elf = (struct elfhdr *) bufp;
9812df173d9SMahesh Salgaonkar 	bufp += sizeof(struct elfhdr);
9822df173d9SMahesh Salgaonkar 	memcpy(elf->e_ident, ELFMAG, SELFMAG);
9832df173d9SMahesh Salgaonkar 	elf->e_ident[EI_CLASS] = ELF_CLASS;
9842df173d9SMahesh Salgaonkar 	elf->e_ident[EI_DATA] = ELF_DATA;
9852df173d9SMahesh Salgaonkar 	elf->e_ident[EI_VERSION] = EV_CURRENT;
9862df173d9SMahesh Salgaonkar 	elf->e_ident[EI_OSABI] = ELF_OSABI;
9872df173d9SMahesh Salgaonkar 	memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
9882df173d9SMahesh Salgaonkar 	elf->e_type = ET_CORE;
9892df173d9SMahesh Salgaonkar 	elf->e_machine = ELF_ARCH;
9902df173d9SMahesh Salgaonkar 	elf->e_version = EV_CURRENT;
9912df173d9SMahesh Salgaonkar 	elf->e_entry = 0;
9922df173d9SMahesh Salgaonkar 	elf->e_phoff = sizeof(struct elfhdr);
9932df173d9SMahesh Salgaonkar 	elf->e_shoff = 0;
994d8bced27SDaniel Axtens #if defined(_CALL_ELF)
995d8bced27SDaniel Axtens 	elf->e_flags = _CALL_ELF;
996d8bced27SDaniel Axtens #else
997d8bced27SDaniel Axtens 	elf->e_flags = 0;
998d8bced27SDaniel Axtens #endif
9992df173d9SMahesh Salgaonkar 	elf->e_ehsize = sizeof(struct elfhdr);
10002df173d9SMahesh Salgaonkar 	elf->e_phentsize = sizeof(struct elf_phdr);
10012df173d9SMahesh Salgaonkar 	elf->e_phnum = 0;
10022df173d9SMahesh Salgaonkar 	elf->e_shentsize = 0;
10032df173d9SMahesh Salgaonkar 	elf->e_shnum = 0;
10042df173d9SMahesh Salgaonkar 	elf->e_shstrndx = 0;
10052df173d9SMahesh Salgaonkar 
10062df173d9SMahesh Salgaonkar 	return 0;
10072df173d9SMahesh Salgaonkar }
10082df173d9SMahesh Salgaonkar 
10092df173d9SMahesh Salgaonkar /*
10102df173d9SMahesh Salgaonkar  * Traverse through memblock structure and setup crash memory ranges. These
10112df173d9SMahesh Salgaonkar  * ranges will be used create PT_LOAD program headers in elfcore header.
10122df173d9SMahesh Salgaonkar  */
10131bd6a1c4SHari Bathini static int fadump_setup_crash_memory_ranges(void)
10142df173d9SMahesh Salgaonkar {
10152df173d9SMahesh Salgaonkar 	struct memblock_region *reg;
10162df173d9SMahesh Salgaonkar 	unsigned long long start, end;
10171bd6a1c4SHari Bathini 	int ret;
10182df173d9SMahesh Salgaonkar 
10192df173d9SMahesh Salgaonkar 	pr_debug("Setup crash memory ranges.\n");
10202df173d9SMahesh Salgaonkar 	crash_mem_ranges = 0;
1021*ced1bf52SHari Bathini 
1022*ced1bf52SHari Bathini 	/* allocate memory for crash memory ranges for the first time */
1023*ced1bf52SHari Bathini 	if (!max_crash_mem_ranges) {
1024*ced1bf52SHari Bathini 		ret = allocate_crash_memory_ranges();
1025*ced1bf52SHari Bathini 		if (ret)
1026*ced1bf52SHari Bathini 			return ret;
1027*ced1bf52SHari Bathini 	}
1028*ced1bf52SHari Bathini 
10292df173d9SMahesh Salgaonkar 	/*
10302df173d9SMahesh Salgaonkar 	 * add the first memory chunk (RMA_START through boot_memory_size) as
10312df173d9SMahesh Salgaonkar 	 * a separate memory chunk. The reason is, at the time crash firmware
10322df173d9SMahesh Salgaonkar 	 * will move the content of this memory chunk to different location
10332df173d9SMahesh Salgaonkar 	 * specified during fadump registration. We need to create a separate
10342df173d9SMahesh Salgaonkar 	 * program header for this chunk with the correct offset.
10352df173d9SMahesh Salgaonkar 	 */
10361bd6a1c4SHari Bathini 	ret = fadump_add_crash_memory(RMA_START, fw_dump.boot_memory_size);
10371bd6a1c4SHari Bathini 	if (ret)
10381bd6a1c4SHari Bathini 		return ret;
10392df173d9SMahesh Salgaonkar 
10402df173d9SMahesh Salgaonkar 	for_each_memblock(memory, reg) {
10412df173d9SMahesh Salgaonkar 		start = (unsigned long long)reg->base;
10422df173d9SMahesh Salgaonkar 		end = start + (unsigned long long)reg->size;
1043a77af552SHari Bathini 
1044a77af552SHari Bathini 		/*
1045a77af552SHari Bathini 		 * skip the first memory chunk that is already added (RMA_START
1046a77af552SHari Bathini 		 * through boot_memory_size). This logic needs a relook if and
1047a77af552SHari Bathini 		 * when RMA_START changes to a non-zero value.
1048a77af552SHari Bathini 		 */
1049a77af552SHari Bathini 		BUILD_BUG_ON(RMA_START != 0);
1050a77af552SHari Bathini 		if (start < fw_dump.boot_memory_size) {
1051a77af552SHari Bathini 			if (end > fw_dump.boot_memory_size)
10522df173d9SMahesh Salgaonkar 				start = fw_dump.boot_memory_size;
1053a77af552SHari Bathini 			else
1054a77af552SHari Bathini 				continue;
1055a77af552SHari Bathini 		}
10562df173d9SMahesh Salgaonkar 
10572df173d9SMahesh Salgaonkar 		/* add this range excluding the reserved dump area. */
10581bd6a1c4SHari Bathini 		ret = fadump_exclude_reserved_area(start, end);
10591bd6a1c4SHari Bathini 		if (ret)
10601bd6a1c4SHari Bathini 			return ret;
10612df173d9SMahesh Salgaonkar 	}
10621bd6a1c4SHari Bathini 
10631bd6a1c4SHari Bathini 	return 0;
10642df173d9SMahesh Salgaonkar }
10652df173d9SMahesh Salgaonkar 
1066d34c5f26SMahesh Salgaonkar /*
1067d34c5f26SMahesh Salgaonkar  * If the given physical address falls within the boot memory region then
1068d34c5f26SMahesh Salgaonkar  * return the relocated address that points to the dump region reserved
1069d34c5f26SMahesh Salgaonkar  * for saving initial boot memory contents.
1070d34c5f26SMahesh Salgaonkar  */
1071d34c5f26SMahesh Salgaonkar static inline unsigned long fadump_relocate(unsigned long paddr)
1072d34c5f26SMahesh Salgaonkar {
1073d34c5f26SMahesh Salgaonkar 	if (paddr > RMA_START && paddr < fw_dump.boot_memory_size)
1074408cddd9SHari Bathini 		return be64_to_cpu(fdm.rmr_region.destination_address) + paddr;
1075d34c5f26SMahesh Salgaonkar 	else
1076d34c5f26SMahesh Salgaonkar 		return paddr;
1077d34c5f26SMahesh Salgaonkar }
1078d34c5f26SMahesh Salgaonkar 
10792df173d9SMahesh Salgaonkar static int fadump_create_elfcore_headers(char *bufp)
10802df173d9SMahesh Salgaonkar {
10812df173d9SMahesh Salgaonkar 	struct elfhdr *elf;
10822df173d9SMahesh Salgaonkar 	struct elf_phdr *phdr;
10832df173d9SMahesh Salgaonkar 	int i;
10842df173d9SMahesh Salgaonkar 
10852df173d9SMahesh Salgaonkar 	fadump_init_elfcore_header(bufp);
10862df173d9SMahesh Salgaonkar 	elf = (struct elfhdr *)bufp;
10872df173d9SMahesh Salgaonkar 	bufp += sizeof(struct elfhdr);
10882df173d9SMahesh Salgaonkar 
1089ebaeb5aeSMahesh Salgaonkar 	/*
1090ebaeb5aeSMahesh Salgaonkar 	 * setup ELF PT_NOTE, place holder for cpu notes info. The notes info
1091ebaeb5aeSMahesh Salgaonkar 	 * will be populated during second kernel boot after crash. Hence
1092ebaeb5aeSMahesh Salgaonkar 	 * this PT_NOTE will always be the first elf note.
1093ebaeb5aeSMahesh Salgaonkar 	 *
1094ebaeb5aeSMahesh Salgaonkar 	 * NOTE: Any new ELF note addition should be placed after this note.
1095ebaeb5aeSMahesh Salgaonkar 	 */
1096ebaeb5aeSMahesh Salgaonkar 	phdr = (struct elf_phdr *)bufp;
1097ebaeb5aeSMahesh Salgaonkar 	bufp += sizeof(struct elf_phdr);
1098ebaeb5aeSMahesh Salgaonkar 	phdr->p_type = PT_NOTE;
1099ebaeb5aeSMahesh Salgaonkar 	phdr->p_flags = 0;
1100ebaeb5aeSMahesh Salgaonkar 	phdr->p_vaddr = 0;
1101ebaeb5aeSMahesh Salgaonkar 	phdr->p_align = 0;
1102ebaeb5aeSMahesh Salgaonkar 
1103ebaeb5aeSMahesh Salgaonkar 	phdr->p_offset = 0;
1104ebaeb5aeSMahesh Salgaonkar 	phdr->p_paddr = 0;
1105ebaeb5aeSMahesh Salgaonkar 	phdr->p_filesz = 0;
1106ebaeb5aeSMahesh Salgaonkar 	phdr->p_memsz = 0;
1107ebaeb5aeSMahesh Salgaonkar 
1108ebaeb5aeSMahesh Salgaonkar 	(elf->e_phnum)++;
1109ebaeb5aeSMahesh Salgaonkar 
1110d34c5f26SMahesh Salgaonkar 	/* setup ELF PT_NOTE for vmcoreinfo */
1111d34c5f26SMahesh Salgaonkar 	phdr = (struct elf_phdr *)bufp;
1112d34c5f26SMahesh Salgaonkar 	bufp += sizeof(struct elf_phdr);
1113d34c5f26SMahesh Salgaonkar 	phdr->p_type	= PT_NOTE;
1114d34c5f26SMahesh Salgaonkar 	phdr->p_flags	= 0;
1115d34c5f26SMahesh Salgaonkar 	phdr->p_vaddr	= 0;
1116d34c5f26SMahesh Salgaonkar 	phdr->p_align	= 0;
1117d34c5f26SMahesh Salgaonkar 
1118d34c5f26SMahesh Salgaonkar 	phdr->p_paddr	= fadump_relocate(paddr_vmcoreinfo_note());
1119d34c5f26SMahesh Salgaonkar 	phdr->p_offset	= phdr->p_paddr;
11205203f499SXunlei Pang 	phdr->p_memsz	= phdr->p_filesz = VMCOREINFO_NOTE_SIZE;
1121d34c5f26SMahesh Salgaonkar 
1122d34c5f26SMahesh Salgaonkar 	/* Increment number of program headers. */
1123d34c5f26SMahesh Salgaonkar 	(elf->e_phnum)++;
1124d34c5f26SMahesh Salgaonkar 
11252df173d9SMahesh Salgaonkar 	/* setup PT_LOAD sections. */
11262df173d9SMahesh Salgaonkar 
11272df173d9SMahesh Salgaonkar 	for (i = 0; i < crash_mem_ranges; i++) {
11282df173d9SMahesh Salgaonkar 		unsigned long long mbase, msize;
11292df173d9SMahesh Salgaonkar 		mbase = crash_memory_ranges[i].base;
11302df173d9SMahesh Salgaonkar 		msize = crash_memory_ranges[i].size;
11312df173d9SMahesh Salgaonkar 
11322df173d9SMahesh Salgaonkar 		if (!msize)
11332df173d9SMahesh Salgaonkar 			continue;
11342df173d9SMahesh Salgaonkar 
11352df173d9SMahesh Salgaonkar 		phdr = (struct elf_phdr *)bufp;
11362df173d9SMahesh Salgaonkar 		bufp += sizeof(struct elf_phdr);
11372df173d9SMahesh Salgaonkar 		phdr->p_type	= PT_LOAD;
11382df173d9SMahesh Salgaonkar 		phdr->p_flags	= PF_R|PF_W|PF_X;
11392df173d9SMahesh Salgaonkar 		phdr->p_offset	= mbase;
11402df173d9SMahesh Salgaonkar 
11412df173d9SMahesh Salgaonkar 		if (mbase == RMA_START) {
11422df173d9SMahesh Salgaonkar 			/*
11432df173d9SMahesh Salgaonkar 			 * The entire RMA region will be moved by firmware
11442df173d9SMahesh Salgaonkar 			 * to the specified destination_address. Hence set
11452df173d9SMahesh Salgaonkar 			 * the correct offset.
11462df173d9SMahesh Salgaonkar 			 */
1147408cddd9SHari Bathini 			phdr->p_offset = be64_to_cpu(fdm.rmr_region.destination_address);
11482df173d9SMahesh Salgaonkar 		}
11492df173d9SMahesh Salgaonkar 
11502df173d9SMahesh Salgaonkar 		phdr->p_paddr = mbase;
11512df173d9SMahesh Salgaonkar 		phdr->p_vaddr = (unsigned long)__va(mbase);
11522df173d9SMahesh Salgaonkar 		phdr->p_filesz = msize;
11532df173d9SMahesh Salgaonkar 		phdr->p_memsz = msize;
11542df173d9SMahesh Salgaonkar 		phdr->p_align = 0;
11552df173d9SMahesh Salgaonkar 
11562df173d9SMahesh Salgaonkar 		/* Increment number of program headers. */
11572df173d9SMahesh Salgaonkar 		(elf->e_phnum)++;
11582df173d9SMahesh Salgaonkar 	}
11592df173d9SMahesh Salgaonkar 	return 0;
11602df173d9SMahesh Salgaonkar }
11612df173d9SMahesh Salgaonkar 
11622df173d9SMahesh Salgaonkar static unsigned long init_fadump_header(unsigned long addr)
11632df173d9SMahesh Salgaonkar {
11642df173d9SMahesh Salgaonkar 	struct fadump_crash_info_header *fdh;
11652df173d9SMahesh Salgaonkar 
11662df173d9SMahesh Salgaonkar 	if (!addr)
11672df173d9SMahesh Salgaonkar 		return 0;
11682df173d9SMahesh Salgaonkar 
11692df173d9SMahesh Salgaonkar 	fw_dump.fadumphdr_addr = addr;
11702df173d9SMahesh Salgaonkar 	fdh = __va(addr);
11712df173d9SMahesh Salgaonkar 	addr += sizeof(struct fadump_crash_info_header);
11722df173d9SMahesh Salgaonkar 
11732df173d9SMahesh Salgaonkar 	memset(fdh, 0, sizeof(struct fadump_crash_info_header));
11742df173d9SMahesh Salgaonkar 	fdh->magic_number = FADUMP_CRASH_INFO_MAGIC;
11752df173d9SMahesh Salgaonkar 	fdh->elfcorehdr_addr = addr;
1176ebaeb5aeSMahesh Salgaonkar 	/* We will set the crashing cpu id in crash_fadump() during crash. */
1177ebaeb5aeSMahesh Salgaonkar 	fdh->crashing_cpu = CPU_UNKNOWN;
11782df173d9SMahesh Salgaonkar 
11792df173d9SMahesh Salgaonkar 	return addr;
11802df173d9SMahesh Salgaonkar }
11812df173d9SMahesh Salgaonkar 
118298b8cd7fSMichal Suchanek static int register_fadump(void)
11833ccc00a7SMahesh Salgaonkar {
11842df173d9SMahesh Salgaonkar 	unsigned long addr;
11852df173d9SMahesh Salgaonkar 	void *vaddr;
11861bd6a1c4SHari Bathini 	int ret;
11872df173d9SMahesh Salgaonkar 
11883ccc00a7SMahesh Salgaonkar 	/*
11893ccc00a7SMahesh Salgaonkar 	 * If no memory is reserved then we can not register for firmware-
11903ccc00a7SMahesh Salgaonkar 	 * assisted dump.
11913ccc00a7SMahesh Salgaonkar 	 */
11923ccc00a7SMahesh Salgaonkar 	if (!fw_dump.reserve_dump_area_size)
119398b8cd7fSMichal Suchanek 		return -ENODEV;
11943ccc00a7SMahesh Salgaonkar 
11951bd6a1c4SHari Bathini 	ret = fadump_setup_crash_memory_ranges();
11961bd6a1c4SHari Bathini 	if (ret)
11971bd6a1c4SHari Bathini 		return ret;
11982df173d9SMahesh Salgaonkar 
1199408cddd9SHari Bathini 	addr = be64_to_cpu(fdm.rmr_region.destination_address) + be64_to_cpu(fdm.rmr_region.source_len);
12002df173d9SMahesh Salgaonkar 	/* Initialize fadump crash info header. */
12012df173d9SMahesh Salgaonkar 	addr = init_fadump_header(addr);
12022df173d9SMahesh Salgaonkar 	vaddr = __va(addr);
12032df173d9SMahesh Salgaonkar 
12042df173d9SMahesh Salgaonkar 	pr_debug("Creating ELF core headers at %#016lx\n", addr);
12052df173d9SMahesh Salgaonkar 	fadump_create_elfcore_headers(vaddr);
12062df173d9SMahesh Salgaonkar 
12073ccc00a7SMahesh Salgaonkar 	/* register the future kernel dump with firmware. */
120898b8cd7fSMichal Suchanek 	return register_fw_dump(&fdm);
12093ccc00a7SMahesh Salgaonkar }
12103ccc00a7SMahesh Salgaonkar 
12113ccc00a7SMahesh Salgaonkar static int fadump_unregister_dump(struct fadump_mem_struct *fdm)
12123ccc00a7SMahesh Salgaonkar {
12133ccc00a7SMahesh Salgaonkar 	int rc = 0;
12143ccc00a7SMahesh Salgaonkar 	unsigned int wait_time;
12153ccc00a7SMahesh Salgaonkar 
12163ccc00a7SMahesh Salgaonkar 	pr_debug("Un-register firmware-assisted dump\n");
12173ccc00a7SMahesh Salgaonkar 
12183ccc00a7SMahesh Salgaonkar 	/* TODO: Add upper time limit for the delay */
12193ccc00a7SMahesh Salgaonkar 	do {
12203ccc00a7SMahesh Salgaonkar 		rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
12213ccc00a7SMahesh Salgaonkar 			FADUMP_UNREGISTER, fdm,
12223ccc00a7SMahesh Salgaonkar 			sizeof(struct fadump_mem_struct));
12233ccc00a7SMahesh Salgaonkar 
12243ccc00a7SMahesh Salgaonkar 		wait_time = rtas_busy_delay_time(rc);
12253ccc00a7SMahesh Salgaonkar 		if (wait_time)
12263ccc00a7SMahesh Salgaonkar 			mdelay(wait_time);
12273ccc00a7SMahesh Salgaonkar 	} while (wait_time);
12283ccc00a7SMahesh Salgaonkar 
12293ccc00a7SMahesh Salgaonkar 	if (rc) {
12303ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "Failed to un-register firmware-assisted dump."
12313ccc00a7SMahesh Salgaonkar 			" unexpected error(%d).\n", rc);
12323ccc00a7SMahesh Salgaonkar 		return rc;
12333ccc00a7SMahesh Salgaonkar 	}
12343ccc00a7SMahesh Salgaonkar 	fw_dump.dump_registered = 0;
12353ccc00a7SMahesh Salgaonkar 	return 0;
12363ccc00a7SMahesh Salgaonkar }
12373ccc00a7SMahesh Salgaonkar 
1238b500afffSMahesh Salgaonkar static int fadump_invalidate_dump(struct fadump_mem_struct *fdm)
1239b500afffSMahesh Salgaonkar {
1240b500afffSMahesh Salgaonkar 	int rc = 0;
1241b500afffSMahesh Salgaonkar 	unsigned int wait_time;
1242b500afffSMahesh Salgaonkar 
1243b500afffSMahesh Salgaonkar 	pr_debug("Invalidating firmware-assisted dump registration\n");
1244b500afffSMahesh Salgaonkar 
1245b500afffSMahesh Salgaonkar 	/* TODO: Add upper time limit for the delay */
1246b500afffSMahesh Salgaonkar 	do {
1247b500afffSMahesh Salgaonkar 		rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
1248b500afffSMahesh Salgaonkar 			FADUMP_INVALIDATE, fdm,
1249b500afffSMahesh Salgaonkar 			sizeof(struct fadump_mem_struct));
1250b500afffSMahesh Salgaonkar 
1251b500afffSMahesh Salgaonkar 		wait_time = rtas_busy_delay_time(rc);
1252b500afffSMahesh Salgaonkar 		if (wait_time)
1253b500afffSMahesh Salgaonkar 			mdelay(wait_time);
1254b500afffSMahesh Salgaonkar 	} while (wait_time);
1255b500afffSMahesh Salgaonkar 
1256b500afffSMahesh Salgaonkar 	if (rc) {
12574a03749fSColin Ian King 		pr_err("Failed to invalidate firmware-assisted dump registration. Unexpected error (%d).\n", rc);
1258b5b1cfc5SMichael Ellerman 		return rc;
1259b500afffSMahesh Salgaonkar 	}
1260b500afffSMahesh Salgaonkar 	fw_dump.dump_active = 0;
1261b500afffSMahesh Salgaonkar 	fdm_active = NULL;
1262b500afffSMahesh Salgaonkar 	return 0;
1263b500afffSMahesh Salgaonkar }
1264b500afffSMahesh Salgaonkar 
1265b500afffSMahesh Salgaonkar void fadump_cleanup(void)
1266b500afffSMahesh Salgaonkar {
1267b500afffSMahesh Salgaonkar 	/* Invalidate the registration only if dump is active. */
1268b500afffSMahesh Salgaonkar 	if (fw_dump.dump_active) {
1269b500afffSMahesh Salgaonkar 		init_fadump_mem_struct(&fdm,
1270408cddd9SHari Bathini 			be64_to_cpu(fdm_active->cpu_state_data.destination_address));
1271b500afffSMahesh Salgaonkar 		fadump_invalidate_dump(&fdm);
1272722cde76SMahesh Salgaonkar 	} else if (fw_dump.dump_registered) {
1273722cde76SMahesh Salgaonkar 		/* Un-register Firmware-assisted dump if it was registered. */
1274722cde76SMahesh Salgaonkar 		fadump_unregister_dump(&fdm);
12751bd6a1c4SHari Bathini 		free_crash_memory_ranges();
1276b500afffSMahesh Salgaonkar 	}
1277b500afffSMahesh Salgaonkar }
1278b500afffSMahesh Salgaonkar 
127968fa6478SHari Bathini static void fadump_free_reserved_memory(unsigned long start_pfn,
128068fa6478SHari Bathini 					unsigned long end_pfn)
128168fa6478SHari Bathini {
128268fa6478SHari Bathini 	unsigned long pfn;
128368fa6478SHari Bathini 	unsigned long time_limit = jiffies + HZ;
128468fa6478SHari Bathini 
128568fa6478SHari Bathini 	pr_info("freeing reserved memory (0x%llx - 0x%llx)\n",
128668fa6478SHari Bathini 		PFN_PHYS(start_pfn), PFN_PHYS(end_pfn));
128768fa6478SHari Bathini 
128868fa6478SHari Bathini 	for (pfn = start_pfn; pfn < end_pfn; pfn++) {
128968fa6478SHari Bathini 		free_reserved_page(pfn_to_page(pfn));
129068fa6478SHari Bathini 
129168fa6478SHari Bathini 		if (time_after(jiffies, time_limit)) {
129268fa6478SHari Bathini 			cond_resched();
129368fa6478SHari Bathini 			time_limit = jiffies + HZ;
129468fa6478SHari Bathini 		}
129568fa6478SHari Bathini 	}
129668fa6478SHari Bathini }
129768fa6478SHari Bathini 
129868fa6478SHari Bathini /*
129968fa6478SHari Bathini  * Skip memory holes and free memory that was actually reserved.
130068fa6478SHari Bathini  */
130168fa6478SHari Bathini static void fadump_release_reserved_area(unsigned long start, unsigned long end)
130268fa6478SHari Bathini {
130368fa6478SHari Bathini 	struct memblock_region *reg;
130468fa6478SHari Bathini 	unsigned long tstart, tend;
130568fa6478SHari Bathini 	unsigned long start_pfn = PHYS_PFN(start);
130668fa6478SHari Bathini 	unsigned long end_pfn = PHYS_PFN(end);
130768fa6478SHari Bathini 
130868fa6478SHari Bathini 	for_each_memblock(memory, reg) {
130968fa6478SHari Bathini 		tstart = max(start_pfn, memblock_region_memory_base_pfn(reg));
131068fa6478SHari Bathini 		tend = min(end_pfn, memblock_region_memory_end_pfn(reg));
131168fa6478SHari Bathini 		if (tstart < tend) {
131268fa6478SHari Bathini 			fadump_free_reserved_memory(tstart, tend);
131368fa6478SHari Bathini 
131468fa6478SHari Bathini 			if (tend == end_pfn)
131568fa6478SHari Bathini 				break;
131668fa6478SHari Bathini 
131768fa6478SHari Bathini 			start_pfn = tend + 1;
131868fa6478SHari Bathini 		}
131968fa6478SHari Bathini 	}
132068fa6478SHari Bathini }
132168fa6478SHari Bathini 
1322b500afffSMahesh Salgaonkar /*
1323b500afffSMahesh Salgaonkar  * Release the memory that was reserved in early boot to preserve the memory
1324b500afffSMahesh Salgaonkar  * contents. The released memory will be available for general use.
1325b500afffSMahesh Salgaonkar  */
1326b500afffSMahesh Salgaonkar static void fadump_release_memory(unsigned long begin, unsigned long end)
1327b500afffSMahesh Salgaonkar {
1328b500afffSMahesh Salgaonkar 	unsigned long ra_start, ra_end;
1329b500afffSMahesh Salgaonkar 
1330b500afffSMahesh Salgaonkar 	ra_start = fw_dump.reserve_dump_area_start;
1331b500afffSMahesh Salgaonkar 	ra_end = ra_start + fw_dump.reserve_dump_area_size;
1332b500afffSMahesh Salgaonkar 
1333b500afffSMahesh Salgaonkar 	/*
1334b500afffSMahesh Salgaonkar 	 * exclude the dump reserve area. Will reuse it for next
1335b500afffSMahesh Salgaonkar 	 * fadump registration.
1336b500afffSMahesh Salgaonkar 	 */
133768fa6478SHari Bathini 	if (begin < ra_end && end > ra_start) {
133868fa6478SHari Bathini 		if (begin < ra_start)
133968fa6478SHari Bathini 			fadump_release_reserved_area(begin, ra_start);
134068fa6478SHari Bathini 		if (end > ra_end)
134168fa6478SHari Bathini 			fadump_release_reserved_area(ra_end, end);
134268fa6478SHari Bathini 	} else
134368fa6478SHari Bathini 		fadump_release_reserved_area(begin, end);
1344b500afffSMahesh Salgaonkar }
1345b500afffSMahesh Salgaonkar 
1346b500afffSMahesh Salgaonkar static void fadump_invalidate_release_mem(void)
1347b500afffSMahesh Salgaonkar {
1348b500afffSMahesh Salgaonkar 	unsigned long reserved_area_start, reserved_area_end;
1349b500afffSMahesh Salgaonkar 	unsigned long destination_address;
1350b500afffSMahesh Salgaonkar 
1351b500afffSMahesh Salgaonkar 	mutex_lock(&fadump_mutex);
1352b500afffSMahesh Salgaonkar 	if (!fw_dump.dump_active) {
1353b500afffSMahesh Salgaonkar 		mutex_unlock(&fadump_mutex);
1354b500afffSMahesh Salgaonkar 		return;
1355b500afffSMahesh Salgaonkar 	}
1356b500afffSMahesh Salgaonkar 
1357408cddd9SHari Bathini 	destination_address = be64_to_cpu(fdm_active->cpu_state_data.destination_address);
1358b500afffSMahesh Salgaonkar 	fadump_cleanup();
1359b500afffSMahesh Salgaonkar 	mutex_unlock(&fadump_mutex);
1360b500afffSMahesh Salgaonkar 
1361b500afffSMahesh Salgaonkar 	/*
1362b500afffSMahesh Salgaonkar 	 * Save the current reserved memory bounds we will require them
1363b500afffSMahesh Salgaonkar 	 * later for releasing the memory for general use.
1364b500afffSMahesh Salgaonkar 	 */
1365b500afffSMahesh Salgaonkar 	reserved_area_start = fw_dump.reserve_dump_area_start;
1366b500afffSMahesh Salgaonkar 	reserved_area_end = reserved_area_start +
1367b500afffSMahesh Salgaonkar 			fw_dump.reserve_dump_area_size;
1368b500afffSMahesh Salgaonkar 	/*
1369b500afffSMahesh Salgaonkar 	 * Setup reserve_dump_area_start and its size so that we can
1370b500afffSMahesh Salgaonkar 	 * reuse this reserved memory for Re-registration.
1371b500afffSMahesh Salgaonkar 	 */
1372b500afffSMahesh Salgaonkar 	fw_dump.reserve_dump_area_start = destination_address;
1373b500afffSMahesh Salgaonkar 	fw_dump.reserve_dump_area_size = get_fadump_area_size();
1374b500afffSMahesh Salgaonkar 
1375b500afffSMahesh Salgaonkar 	fadump_release_memory(reserved_area_start, reserved_area_end);
1376b500afffSMahesh Salgaonkar 	if (fw_dump.cpu_notes_buf) {
1377b500afffSMahesh Salgaonkar 		fadump_cpu_notes_buf_free(
1378b500afffSMahesh Salgaonkar 				(unsigned long)__va(fw_dump.cpu_notes_buf),
1379b500afffSMahesh Salgaonkar 				fw_dump.cpu_notes_buf_size);
1380b500afffSMahesh Salgaonkar 		fw_dump.cpu_notes_buf = 0;
1381b500afffSMahesh Salgaonkar 		fw_dump.cpu_notes_buf_size = 0;
1382b500afffSMahesh Salgaonkar 	}
1383b500afffSMahesh Salgaonkar 	/* Initialize the kernel dump memory structure for FAD registration. */
1384b500afffSMahesh Salgaonkar 	init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
1385b500afffSMahesh Salgaonkar }
1386b500afffSMahesh Salgaonkar 
1387b500afffSMahesh Salgaonkar static ssize_t fadump_release_memory_store(struct kobject *kobj,
1388b500afffSMahesh Salgaonkar 					struct kobj_attribute *attr,
1389b500afffSMahesh Salgaonkar 					const char *buf, size_t count)
1390b500afffSMahesh Salgaonkar {
1391dcdc4679SMichal Suchanek 	int input = -1;
1392dcdc4679SMichal Suchanek 
1393b500afffSMahesh Salgaonkar 	if (!fw_dump.dump_active)
1394b500afffSMahesh Salgaonkar 		return -EPERM;
1395b500afffSMahesh Salgaonkar 
1396dcdc4679SMichal Suchanek 	if (kstrtoint(buf, 0, &input))
1397dcdc4679SMichal Suchanek 		return -EINVAL;
1398dcdc4679SMichal Suchanek 
1399dcdc4679SMichal Suchanek 	if (input == 1) {
1400b500afffSMahesh Salgaonkar 		/*
1401b500afffSMahesh Salgaonkar 		 * Take away the '/proc/vmcore'. We are releasing the dump
1402b500afffSMahesh Salgaonkar 		 * memory, hence it will not be valid anymore.
1403b500afffSMahesh Salgaonkar 		 */
14042685f826SMichael Ellerman #ifdef CONFIG_PROC_VMCORE
1405b500afffSMahesh Salgaonkar 		vmcore_cleanup();
14062685f826SMichael Ellerman #endif
1407b500afffSMahesh Salgaonkar 		fadump_invalidate_release_mem();
1408b500afffSMahesh Salgaonkar 
1409b500afffSMahesh Salgaonkar 	} else
1410b500afffSMahesh Salgaonkar 		return -EINVAL;
1411b500afffSMahesh Salgaonkar 	return count;
1412b500afffSMahesh Salgaonkar }
1413b500afffSMahesh Salgaonkar 
14143ccc00a7SMahesh Salgaonkar static ssize_t fadump_enabled_show(struct kobject *kobj,
14153ccc00a7SMahesh Salgaonkar 					struct kobj_attribute *attr,
14163ccc00a7SMahesh Salgaonkar 					char *buf)
14173ccc00a7SMahesh Salgaonkar {
14183ccc00a7SMahesh Salgaonkar 	return sprintf(buf, "%d\n", fw_dump.fadump_enabled);
14193ccc00a7SMahesh Salgaonkar }
14203ccc00a7SMahesh Salgaonkar 
14213ccc00a7SMahesh Salgaonkar static ssize_t fadump_register_show(struct kobject *kobj,
14223ccc00a7SMahesh Salgaonkar 					struct kobj_attribute *attr,
14233ccc00a7SMahesh Salgaonkar 					char *buf)
14243ccc00a7SMahesh Salgaonkar {
14253ccc00a7SMahesh Salgaonkar 	return sprintf(buf, "%d\n", fw_dump.dump_registered);
14263ccc00a7SMahesh Salgaonkar }
14273ccc00a7SMahesh Salgaonkar 
14283ccc00a7SMahesh Salgaonkar static ssize_t fadump_register_store(struct kobject *kobj,
14293ccc00a7SMahesh Salgaonkar 					struct kobj_attribute *attr,
14303ccc00a7SMahesh Salgaonkar 					const char *buf, size_t count)
14313ccc00a7SMahesh Salgaonkar {
14323ccc00a7SMahesh Salgaonkar 	int ret = 0;
1433dcdc4679SMichal Suchanek 	int input = -1;
14343ccc00a7SMahesh Salgaonkar 
14353ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_enabled || fdm_active)
14363ccc00a7SMahesh Salgaonkar 		return -EPERM;
14373ccc00a7SMahesh Salgaonkar 
1438dcdc4679SMichal Suchanek 	if (kstrtoint(buf, 0, &input))
1439dcdc4679SMichal Suchanek 		return -EINVAL;
1440dcdc4679SMichal Suchanek 
14413ccc00a7SMahesh Salgaonkar 	mutex_lock(&fadump_mutex);
14423ccc00a7SMahesh Salgaonkar 
1443dcdc4679SMichal Suchanek 	switch (input) {
1444dcdc4679SMichal Suchanek 	case 0:
14453ccc00a7SMahesh Salgaonkar 		if (fw_dump.dump_registered == 0) {
14463ccc00a7SMahesh Salgaonkar 			goto unlock_out;
14473ccc00a7SMahesh Salgaonkar 		}
14483ccc00a7SMahesh Salgaonkar 		/* Un-register Firmware-assisted dump */
14493ccc00a7SMahesh Salgaonkar 		fadump_unregister_dump(&fdm);
14503ccc00a7SMahesh Salgaonkar 		break;
1451dcdc4679SMichal Suchanek 	case 1:
14523ccc00a7SMahesh Salgaonkar 		if (fw_dump.dump_registered == 1) {
145398b8cd7fSMichal Suchanek 			ret = -EEXIST;
14543ccc00a7SMahesh Salgaonkar 			goto unlock_out;
14553ccc00a7SMahesh Salgaonkar 		}
14563ccc00a7SMahesh Salgaonkar 		/* Register Firmware-assisted dump */
145798b8cd7fSMichal Suchanek 		ret = register_fadump();
14583ccc00a7SMahesh Salgaonkar 		break;
14593ccc00a7SMahesh Salgaonkar 	default:
14603ccc00a7SMahesh Salgaonkar 		ret = -EINVAL;
14613ccc00a7SMahesh Salgaonkar 		break;
14623ccc00a7SMahesh Salgaonkar 	}
14633ccc00a7SMahesh Salgaonkar 
14643ccc00a7SMahesh Salgaonkar unlock_out:
14653ccc00a7SMahesh Salgaonkar 	mutex_unlock(&fadump_mutex);
14663ccc00a7SMahesh Salgaonkar 	return ret < 0 ? ret : count;
14673ccc00a7SMahesh Salgaonkar }
14683ccc00a7SMahesh Salgaonkar 
14693ccc00a7SMahesh Salgaonkar static int fadump_region_show(struct seq_file *m, void *private)
14703ccc00a7SMahesh Salgaonkar {
14713ccc00a7SMahesh Salgaonkar 	const struct fadump_mem_struct *fdm_ptr;
14723ccc00a7SMahesh Salgaonkar 
14733ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_enabled)
14743ccc00a7SMahesh Salgaonkar 		return 0;
14753ccc00a7SMahesh Salgaonkar 
1476b500afffSMahesh Salgaonkar 	mutex_lock(&fadump_mutex);
14773ccc00a7SMahesh Salgaonkar 	if (fdm_active)
14783ccc00a7SMahesh Salgaonkar 		fdm_ptr = fdm_active;
1479b500afffSMahesh Salgaonkar 	else {
1480b500afffSMahesh Salgaonkar 		mutex_unlock(&fadump_mutex);
14813ccc00a7SMahesh Salgaonkar 		fdm_ptr = &fdm;
1482b500afffSMahesh Salgaonkar 	}
14833ccc00a7SMahesh Salgaonkar 
14843ccc00a7SMahesh Salgaonkar 	seq_printf(m,
14853ccc00a7SMahesh Salgaonkar 			"CPU : [%#016llx-%#016llx] %#llx bytes, "
14863ccc00a7SMahesh Salgaonkar 			"Dumped: %#llx\n",
1487408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->cpu_state_data.destination_address),
1488408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) +
1489408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->cpu_state_data.source_len) - 1,
1490408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->cpu_state_data.source_len),
1491408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->cpu_state_data.bytes_dumped));
14923ccc00a7SMahesh Salgaonkar 	seq_printf(m,
14933ccc00a7SMahesh Salgaonkar 			"HPTE: [%#016llx-%#016llx] %#llx bytes, "
14943ccc00a7SMahesh Salgaonkar 			"Dumped: %#llx\n",
1495408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->hpte_region.destination_address),
1496408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->hpte_region.destination_address) +
1497408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->hpte_region.source_len) - 1,
1498408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->hpte_region.source_len),
1499408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->hpte_region.bytes_dumped));
15003ccc00a7SMahesh Salgaonkar 	seq_printf(m,
15013ccc00a7SMahesh Salgaonkar 			"DUMP: [%#016llx-%#016llx] %#llx bytes, "
15023ccc00a7SMahesh Salgaonkar 			"Dumped: %#llx\n",
1503408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->rmr_region.destination_address),
1504408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->rmr_region.destination_address) +
1505408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->rmr_region.source_len) - 1,
1506408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->rmr_region.source_len),
1507408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->rmr_region.bytes_dumped));
15083ccc00a7SMahesh Salgaonkar 
15093ccc00a7SMahesh Salgaonkar 	if (!fdm_active ||
15103ccc00a7SMahesh Salgaonkar 		(fw_dump.reserve_dump_area_start ==
1511408cddd9SHari Bathini 		be64_to_cpu(fdm_ptr->cpu_state_data.destination_address)))
1512b500afffSMahesh Salgaonkar 		goto out;
15133ccc00a7SMahesh Salgaonkar 
15143ccc00a7SMahesh Salgaonkar 	/* Dump is active. Show reserved memory region. */
15153ccc00a7SMahesh Salgaonkar 	seq_printf(m,
15163ccc00a7SMahesh Salgaonkar 			"    : [%#016llx-%#016llx] %#llx bytes, "
15173ccc00a7SMahesh Salgaonkar 			"Dumped: %#llx\n",
15183ccc00a7SMahesh Salgaonkar 			(unsigned long long)fw_dump.reserve_dump_area_start,
1519408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) - 1,
1520408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) -
15213ccc00a7SMahesh Salgaonkar 			fw_dump.reserve_dump_area_start,
1522408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) -
15233ccc00a7SMahesh Salgaonkar 			fw_dump.reserve_dump_area_start);
1524b500afffSMahesh Salgaonkar out:
1525b500afffSMahesh Salgaonkar 	if (fdm_active)
1526b500afffSMahesh Salgaonkar 		mutex_unlock(&fadump_mutex);
15273ccc00a7SMahesh Salgaonkar 	return 0;
15283ccc00a7SMahesh Salgaonkar }
15293ccc00a7SMahesh Salgaonkar 
1530b500afffSMahesh Salgaonkar static struct kobj_attribute fadump_release_attr = __ATTR(fadump_release_mem,
1531b500afffSMahesh Salgaonkar 						0200, NULL,
1532b500afffSMahesh Salgaonkar 						fadump_release_memory_store);
15333ccc00a7SMahesh Salgaonkar static struct kobj_attribute fadump_attr = __ATTR(fadump_enabled,
15343ccc00a7SMahesh Salgaonkar 						0444, fadump_enabled_show,
15353ccc00a7SMahesh Salgaonkar 						NULL);
15363ccc00a7SMahesh Salgaonkar static struct kobj_attribute fadump_register_attr = __ATTR(fadump_registered,
15373ccc00a7SMahesh Salgaonkar 						0644, fadump_register_show,
15383ccc00a7SMahesh Salgaonkar 						fadump_register_store);
15393ccc00a7SMahesh Salgaonkar 
15403ccc00a7SMahesh Salgaonkar static int fadump_region_open(struct inode *inode, struct file *file)
15413ccc00a7SMahesh Salgaonkar {
15423ccc00a7SMahesh Salgaonkar 	return single_open(file, fadump_region_show, inode->i_private);
15433ccc00a7SMahesh Salgaonkar }
15443ccc00a7SMahesh Salgaonkar 
15453ccc00a7SMahesh Salgaonkar static const struct file_operations fadump_region_fops = {
15463ccc00a7SMahesh Salgaonkar 	.open    = fadump_region_open,
15473ccc00a7SMahesh Salgaonkar 	.read    = seq_read,
15483ccc00a7SMahesh Salgaonkar 	.llseek  = seq_lseek,
15493ccc00a7SMahesh Salgaonkar 	.release = single_release,
15503ccc00a7SMahesh Salgaonkar };
15513ccc00a7SMahesh Salgaonkar 
15523ccc00a7SMahesh Salgaonkar static void fadump_init_files(void)
15533ccc00a7SMahesh Salgaonkar {
15543ccc00a7SMahesh Salgaonkar 	struct dentry *debugfs_file;
15553ccc00a7SMahesh Salgaonkar 	int rc = 0;
15563ccc00a7SMahesh Salgaonkar 
15573ccc00a7SMahesh Salgaonkar 	rc = sysfs_create_file(kernel_kobj, &fadump_attr.attr);
15583ccc00a7SMahesh Salgaonkar 	if (rc)
15593ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "fadump: unable to create sysfs file"
15603ccc00a7SMahesh Salgaonkar 			" fadump_enabled (%d)\n", rc);
15613ccc00a7SMahesh Salgaonkar 
15623ccc00a7SMahesh Salgaonkar 	rc = sysfs_create_file(kernel_kobj, &fadump_register_attr.attr);
15633ccc00a7SMahesh Salgaonkar 	if (rc)
15643ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "fadump: unable to create sysfs file"
15653ccc00a7SMahesh Salgaonkar 			" fadump_registered (%d)\n", rc);
15663ccc00a7SMahesh Salgaonkar 
15673ccc00a7SMahesh Salgaonkar 	debugfs_file = debugfs_create_file("fadump_region", 0444,
15683ccc00a7SMahesh Salgaonkar 					powerpc_debugfs_root, NULL,
15693ccc00a7SMahesh Salgaonkar 					&fadump_region_fops);
15703ccc00a7SMahesh Salgaonkar 	if (!debugfs_file)
15713ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "fadump: unable to create debugfs file"
15723ccc00a7SMahesh Salgaonkar 				" fadump_region\n");
1573b500afffSMahesh Salgaonkar 
1574b500afffSMahesh Salgaonkar 	if (fw_dump.dump_active) {
1575b500afffSMahesh Salgaonkar 		rc = sysfs_create_file(kernel_kobj, &fadump_release_attr.attr);
1576b500afffSMahesh Salgaonkar 		if (rc)
1577b500afffSMahesh Salgaonkar 			printk(KERN_ERR "fadump: unable to create sysfs file"
1578b500afffSMahesh Salgaonkar 				" fadump_release_mem (%d)\n", rc);
1579b500afffSMahesh Salgaonkar 	}
15803ccc00a7SMahesh Salgaonkar 	return;
15813ccc00a7SMahesh Salgaonkar }
15823ccc00a7SMahesh Salgaonkar 
15833ccc00a7SMahesh Salgaonkar /*
15843ccc00a7SMahesh Salgaonkar  * Prepare for firmware-assisted dump.
15853ccc00a7SMahesh Salgaonkar  */
15863ccc00a7SMahesh Salgaonkar int __init setup_fadump(void)
15873ccc00a7SMahesh Salgaonkar {
15883ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_enabled)
15893ccc00a7SMahesh Salgaonkar 		return 0;
15903ccc00a7SMahesh Salgaonkar 
15913ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_supported) {
15923ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "Firmware-assisted dump is not supported on"
15933ccc00a7SMahesh Salgaonkar 			" this hardware\n");
15943ccc00a7SMahesh Salgaonkar 		return 0;
15953ccc00a7SMahesh Salgaonkar 	}
15963ccc00a7SMahesh Salgaonkar 
15973ccc00a7SMahesh Salgaonkar 	fadump_show_config();
15982df173d9SMahesh Salgaonkar 	/*
15992df173d9SMahesh Salgaonkar 	 * If dump data is available then see if it is valid and prepare for
16002df173d9SMahesh Salgaonkar 	 * saving it to the disk.
16012df173d9SMahesh Salgaonkar 	 */
1602b500afffSMahesh Salgaonkar 	if (fw_dump.dump_active) {
1603b500afffSMahesh Salgaonkar 		/*
1604b500afffSMahesh Salgaonkar 		 * if dump process fails then invalidate the registration
1605b500afffSMahesh Salgaonkar 		 * and release memory before proceeding for re-registration.
1606b500afffSMahesh Salgaonkar 		 */
1607b500afffSMahesh Salgaonkar 		if (process_fadump(fdm_active) < 0)
1608b500afffSMahesh Salgaonkar 			fadump_invalidate_release_mem();
1609b500afffSMahesh Salgaonkar 	}
16103ccc00a7SMahesh Salgaonkar 	/* Initialize the kernel dump memory structure for FAD registration. */
16112df173d9SMahesh Salgaonkar 	else if (fw_dump.reserve_dump_area_size)
16123ccc00a7SMahesh Salgaonkar 		init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
16133ccc00a7SMahesh Salgaonkar 	fadump_init_files();
16143ccc00a7SMahesh Salgaonkar 
16153ccc00a7SMahesh Salgaonkar 	return 1;
16163ccc00a7SMahesh Salgaonkar }
16173ccc00a7SMahesh Salgaonkar subsys_initcall(setup_fadump);
1618