xref: /openbmc/linux/arch/powerpc/kernel/fadump.c (revision 5203f4995d9a87952a83c2ce7866adbbe8f97bb5)
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);
502df173d9SMahesh Salgaonkar struct fad_crash_memory_ranges crash_memory_ranges[INIT_CRASHMEM_RANGES];
512df173d9SMahesh Salgaonkar int crash_mem_ranges;
52eb39c880SMahesh Salgaonkar 
53eb39c880SMahesh Salgaonkar /* Scan the Firmware Assisted dump configuration details. */
54eb39c880SMahesh Salgaonkar int __init early_init_dt_scan_fw_dump(unsigned long node,
55eb39c880SMahesh Salgaonkar 			const char *uname, int depth, void *data)
56eb39c880SMahesh Salgaonkar {
579d0c4dfeSRob Herring 	const __be32 *sections;
58eb39c880SMahesh Salgaonkar 	int i, num_sections;
599d0c4dfeSRob Herring 	int size;
60408cddd9SHari Bathini 	const __be32 *token;
61eb39c880SMahesh Salgaonkar 
62eb39c880SMahesh Salgaonkar 	if (depth != 1 || strcmp(uname, "rtas") != 0)
63eb39c880SMahesh Salgaonkar 		return 0;
64eb39c880SMahesh Salgaonkar 
65eb39c880SMahesh Salgaonkar 	/*
66eb39c880SMahesh Salgaonkar 	 * Check if Firmware Assisted dump is supported. if yes, check
67eb39c880SMahesh Salgaonkar 	 * if dump has been initiated on last reboot.
68eb39c880SMahesh Salgaonkar 	 */
69eb39c880SMahesh Salgaonkar 	token = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump", NULL);
70eb39c880SMahesh Salgaonkar 	if (!token)
71a7d04317SGavin Shan 		return 1;
72eb39c880SMahesh Salgaonkar 
73eb39c880SMahesh Salgaonkar 	fw_dump.fadump_supported = 1;
74408cddd9SHari Bathini 	fw_dump.ibm_configure_kernel_dump = be32_to_cpu(*token);
75eb39c880SMahesh Salgaonkar 
76eb39c880SMahesh Salgaonkar 	/*
77eb39c880SMahesh Salgaonkar 	 * The 'ibm,kernel-dump' rtas node is present only if there is
78eb39c880SMahesh Salgaonkar 	 * dump data waiting for us.
79eb39c880SMahesh Salgaonkar 	 */
803ccc00a7SMahesh Salgaonkar 	fdm_active = of_get_flat_dt_prop(node, "ibm,kernel-dump", NULL);
813ccc00a7SMahesh Salgaonkar 	if (fdm_active)
82eb39c880SMahesh Salgaonkar 		fw_dump.dump_active = 1;
83eb39c880SMahesh Salgaonkar 
84eb39c880SMahesh Salgaonkar 	/* Get the sizes required to store dump data for the firmware provided
85eb39c880SMahesh Salgaonkar 	 * dump sections.
86eb39c880SMahesh Salgaonkar 	 * For each dump section type supported, a 32bit cell which defines
87eb39c880SMahesh Salgaonkar 	 * the ID of a supported section followed by two 32 bit cells which
88eb39c880SMahesh Salgaonkar 	 * gives teh size of the section in bytes.
89eb39c880SMahesh Salgaonkar 	 */
90eb39c880SMahesh Salgaonkar 	sections = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump-sizes",
91eb39c880SMahesh Salgaonkar 					&size);
92eb39c880SMahesh Salgaonkar 
93eb39c880SMahesh Salgaonkar 	if (!sections)
94a7d04317SGavin Shan 		return 1;
95eb39c880SMahesh Salgaonkar 
96eb39c880SMahesh Salgaonkar 	num_sections = size / (3 * sizeof(u32));
97eb39c880SMahesh Salgaonkar 
98eb39c880SMahesh Salgaonkar 	for (i = 0; i < num_sections; i++, sections += 3) {
99eb39c880SMahesh Salgaonkar 		u32 type = (u32)of_read_number(sections, 1);
100eb39c880SMahesh Salgaonkar 
101eb39c880SMahesh Salgaonkar 		switch (type) {
102eb39c880SMahesh Salgaonkar 		case FADUMP_CPU_STATE_DATA:
103eb39c880SMahesh Salgaonkar 			fw_dump.cpu_state_data_size =
104eb39c880SMahesh Salgaonkar 					of_read_ulong(&sections[1], 2);
105eb39c880SMahesh Salgaonkar 			break;
106eb39c880SMahesh Salgaonkar 		case FADUMP_HPTE_REGION:
107eb39c880SMahesh Salgaonkar 			fw_dump.hpte_region_size =
108eb39c880SMahesh Salgaonkar 					of_read_ulong(&sections[1], 2);
109eb39c880SMahesh Salgaonkar 			break;
110eb39c880SMahesh Salgaonkar 		}
111eb39c880SMahesh Salgaonkar 	}
112a7d04317SGavin Shan 
113eb39c880SMahesh Salgaonkar 	return 1;
114eb39c880SMahesh Salgaonkar }
115eb39c880SMahesh Salgaonkar 
116eae0dfccSHari Bathini /*
117eae0dfccSHari Bathini  * If fadump is registered, check if the memory provided
118eae0dfccSHari Bathini  * falls within boot memory area.
119eae0dfccSHari Bathini  */
120eae0dfccSHari Bathini int is_fadump_boot_memory_area(u64 addr, ulong size)
121eae0dfccSHari Bathini {
122eae0dfccSHari Bathini 	if (!fw_dump.dump_registered)
123eae0dfccSHari Bathini 		return 0;
124eae0dfccSHari Bathini 
125eae0dfccSHari Bathini 	return (addr + size) > RMA_START && addr <= fw_dump.boot_memory_size;
126eae0dfccSHari Bathini }
127eae0dfccSHari Bathini 
1283ccc00a7SMahesh Salgaonkar int is_fadump_active(void)
1293ccc00a7SMahesh Salgaonkar {
1303ccc00a7SMahesh Salgaonkar 	return fw_dump.dump_active;
1313ccc00a7SMahesh Salgaonkar }
1323ccc00a7SMahesh Salgaonkar 
133a5a05b91SHari Bathini /*
134a5a05b91SHari Bathini  * Returns 1, if there are no holes in boot memory area,
135a5a05b91SHari Bathini  * 0 otherwise.
136a5a05b91SHari Bathini  */
137a5a05b91SHari Bathini static int is_boot_memory_area_contiguous(void)
138a5a05b91SHari Bathini {
139a5a05b91SHari Bathini 	struct memblock_region *reg;
140a5a05b91SHari Bathini 	unsigned long tstart, tend;
141a5a05b91SHari Bathini 	unsigned long start_pfn = PHYS_PFN(RMA_START);
142a5a05b91SHari Bathini 	unsigned long end_pfn = PHYS_PFN(RMA_START + fw_dump.boot_memory_size);
143a5a05b91SHari Bathini 	unsigned int ret = 0;
144a5a05b91SHari Bathini 
145a5a05b91SHari Bathini 	for_each_memblock(memory, reg) {
146a5a05b91SHari Bathini 		tstart = max(start_pfn, memblock_region_memory_base_pfn(reg));
147a5a05b91SHari Bathini 		tend = min(end_pfn, memblock_region_memory_end_pfn(reg));
148a5a05b91SHari Bathini 		if (tstart < tend) {
149a5a05b91SHari Bathini 			/* Memory hole from start_pfn to tstart */
150a5a05b91SHari Bathini 			if (tstart > start_pfn)
151a5a05b91SHari Bathini 				break;
152a5a05b91SHari Bathini 
153a5a05b91SHari Bathini 			if (tend == end_pfn) {
154a5a05b91SHari Bathini 				ret = 1;
155a5a05b91SHari Bathini 				break;
156a5a05b91SHari Bathini 			}
157a5a05b91SHari Bathini 
158a5a05b91SHari Bathini 			start_pfn = tend + 1;
159a5a05b91SHari Bathini 		}
160a5a05b91SHari Bathini 	}
161a5a05b91SHari Bathini 
162a5a05b91SHari Bathini 	return ret;
163a5a05b91SHari Bathini }
164a5a05b91SHari Bathini 
1653ccc00a7SMahesh Salgaonkar /* Print firmware assisted dump configurations for debugging purpose. */
1663ccc00a7SMahesh Salgaonkar static void fadump_show_config(void)
1673ccc00a7SMahesh Salgaonkar {
1683ccc00a7SMahesh Salgaonkar 	pr_debug("Support for firmware-assisted dump (fadump): %s\n",
1693ccc00a7SMahesh Salgaonkar 			(fw_dump.fadump_supported ? "present" : "no support"));
1703ccc00a7SMahesh Salgaonkar 
1713ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_supported)
1723ccc00a7SMahesh Salgaonkar 		return;
1733ccc00a7SMahesh Salgaonkar 
1743ccc00a7SMahesh Salgaonkar 	pr_debug("Fadump enabled    : %s\n",
1753ccc00a7SMahesh Salgaonkar 				(fw_dump.fadump_enabled ? "yes" : "no"));
1763ccc00a7SMahesh Salgaonkar 	pr_debug("Dump Active       : %s\n",
1773ccc00a7SMahesh Salgaonkar 				(fw_dump.dump_active ? "yes" : "no"));
1783ccc00a7SMahesh Salgaonkar 	pr_debug("Dump section sizes:\n");
1793ccc00a7SMahesh Salgaonkar 	pr_debug("    CPU state data size: %lx\n", fw_dump.cpu_state_data_size);
1803ccc00a7SMahesh Salgaonkar 	pr_debug("    HPTE region size   : %lx\n", fw_dump.hpte_region_size);
1813ccc00a7SMahesh Salgaonkar 	pr_debug("Boot memory size  : %lx\n", fw_dump.boot_memory_size);
1823ccc00a7SMahesh Salgaonkar }
1833ccc00a7SMahesh Salgaonkar 
1843ccc00a7SMahesh Salgaonkar static unsigned long init_fadump_mem_struct(struct fadump_mem_struct *fdm,
1853ccc00a7SMahesh Salgaonkar 				unsigned long addr)
1863ccc00a7SMahesh Salgaonkar {
1873ccc00a7SMahesh Salgaonkar 	if (!fdm)
1883ccc00a7SMahesh Salgaonkar 		return 0;
1893ccc00a7SMahesh Salgaonkar 
1903ccc00a7SMahesh Salgaonkar 	memset(fdm, 0, sizeof(struct fadump_mem_struct));
1913ccc00a7SMahesh Salgaonkar 	addr = addr & PAGE_MASK;
1923ccc00a7SMahesh Salgaonkar 
193408cddd9SHari Bathini 	fdm->header.dump_format_version = cpu_to_be32(0x00000001);
194408cddd9SHari Bathini 	fdm->header.dump_num_sections = cpu_to_be16(3);
1953ccc00a7SMahesh Salgaonkar 	fdm->header.dump_status_flag = 0;
1963ccc00a7SMahesh Salgaonkar 	fdm->header.offset_first_dump_section =
197408cddd9SHari Bathini 		cpu_to_be32((u32)offsetof(struct fadump_mem_struct, cpu_state_data));
1983ccc00a7SMahesh Salgaonkar 
1993ccc00a7SMahesh Salgaonkar 	/*
2003ccc00a7SMahesh Salgaonkar 	 * Fields for disk dump option.
2013ccc00a7SMahesh Salgaonkar 	 * We are not using disk dump option, hence set these fields to 0.
2023ccc00a7SMahesh Salgaonkar 	 */
2033ccc00a7SMahesh Salgaonkar 	fdm->header.dd_block_size = 0;
2043ccc00a7SMahesh Salgaonkar 	fdm->header.dd_block_offset = 0;
2053ccc00a7SMahesh Salgaonkar 	fdm->header.dd_num_blocks = 0;
2063ccc00a7SMahesh Salgaonkar 	fdm->header.dd_offset_disk_path = 0;
2073ccc00a7SMahesh Salgaonkar 
2083ccc00a7SMahesh Salgaonkar 	/* set 0 to disable an automatic dump-reboot. */
2093ccc00a7SMahesh Salgaonkar 	fdm->header.max_time_auto = 0;
2103ccc00a7SMahesh Salgaonkar 
2113ccc00a7SMahesh Salgaonkar 	/* Kernel dump sections */
2123ccc00a7SMahesh Salgaonkar 	/* cpu state data section. */
213408cddd9SHari Bathini 	fdm->cpu_state_data.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG);
214408cddd9SHari Bathini 	fdm->cpu_state_data.source_data_type = cpu_to_be16(FADUMP_CPU_STATE_DATA);
2153ccc00a7SMahesh Salgaonkar 	fdm->cpu_state_data.source_address = 0;
216408cddd9SHari Bathini 	fdm->cpu_state_data.source_len = cpu_to_be64(fw_dump.cpu_state_data_size);
217408cddd9SHari Bathini 	fdm->cpu_state_data.destination_address = cpu_to_be64(addr);
2183ccc00a7SMahesh Salgaonkar 	addr += fw_dump.cpu_state_data_size;
2193ccc00a7SMahesh Salgaonkar 
2203ccc00a7SMahesh Salgaonkar 	/* hpte region section */
221408cddd9SHari Bathini 	fdm->hpte_region.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG);
222408cddd9SHari Bathini 	fdm->hpte_region.source_data_type = cpu_to_be16(FADUMP_HPTE_REGION);
2233ccc00a7SMahesh Salgaonkar 	fdm->hpte_region.source_address = 0;
224408cddd9SHari Bathini 	fdm->hpte_region.source_len = cpu_to_be64(fw_dump.hpte_region_size);
225408cddd9SHari Bathini 	fdm->hpte_region.destination_address = cpu_to_be64(addr);
2263ccc00a7SMahesh Salgaonkar 	addr += fw_dump.hpte_region_size;
2273ccc00a7SMahesh Salgaonkar 
2283ccc00a7SMahesh Salgaonkar 	/* RMA region section */
229408cddd9SHari Bathini 	fdm->rmr_region.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG);
230408cddd9SHari Bathini 	fdm->rmr_region.source_data_type = cpu_to_be16(FADUMP_REAL_MODE_REGION);
231408cddd9SHari Bathini 	fdm->rmr_region.source_address = cpu_to_be64(RMA_START);
232408cddd9SHari Bathini 	fdm->rmr_region.source_len = cpu_to_be64(fw_dump.boot_memory_size);
233408cddd9SHari Bathini 	fdm->rmr_region.destination_address = cpu_to_be64(addr);
2343ccc00a7SMahesh Salgaonkar 	addr += fw_dump.boot_memory_size;
2353ccc00a7SMahesh Salgaonkar 
2363ccc00a7SMahesh Salgaonkar 	return addr;
2373ccc00a7SMahesh Salgaonkar }
2383ccc00a7SMahesh Salgaonkar 
239eb39c880SMahesh Salgaonkar /**
240eb39c880SMahesh Salgaonkar  * fadump_calculate_reserve_size(): reserve variable boot area 5% of System RAM
241eb39c880SMahesh Salgaonkar  *
242eb39c880SMahesh Salgaonkar  * Function to find the largest memory size we need to reserve during early
243eb39c880SMahesh Salgaonkar  * boot process. This will be the size of the memory that is required for a
244eb39c880SMahesh Salgaonkar  * kernel to boot successfully.
245eb39c880SMahesh Salgaonkar  *
246eb39c880SMahesh Salgaonkar  * This function has been taken from phyp-assisted dump feature implementation.
247eb39c880SMahesh Salgaonkar  *
248eb39c880SMahesh Salgaonkar  * returns larger of 256MB or 5% rounded down to multiples of 256MB.
249eb39c880SMahesh Salgaonkar  *
250eb39c880SMahesh Salgaonkar  * TODO: Come up with better approach to find out more accurate memory size
251eb39c880SMahesh Salgaonkar  * that is required for a kernel to boot successfully.
252eb39c880SMahesh Salgaonkar  *
253eb39c880SMahesh Salgaonkar  */
254eb39c880SMahesh Salgaonkar static inline unsigned long fadump_calculate_reserve_size(void)
255eb39c880SMahesh Salgaonkar {
25611550dc0SHari Bathini 	int ret;
25711550dc0SHari Bathini 	unsigned long long base, size;
258eb39c880SMahesh Salgaonkar 
25981d9eca5SHari Bathini 	if (fw_dump.reserve_bootvar)
26081d9eca5SHari Bathini 		pr_warn("'fadump_reserve_mem=' parameter is deprecated in favor of 'crashkernel=' parameter.\n");
26181d9eca5SHari Bathini 
262eb39c880SMahesh Salgaonkar 	/*
26311550dc0SHari Bathini 	 * Check if the size is specified through crashkernel= cmdline
264e7467dc6SHari Bathini 	 * option. If yes, then use that but ignore base as fadump reserves
265e7467dc6SHari Bathini 	 * memory at a predefined offset.
266eb39c880SMahesh Salgaonkar 	 */
26711550dc0SHari Bathini 	ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
26811550dc0SHari Bathini 				&size, &base);
26911550dc0SHari Bathini 	if (ret == 0 && size > 0) {
27048a316e3SHari Bathini 		unsigned long max_size;
27148a316e3SHari Bathini 
27281d9eca5SHari Bathini 		if (fw_dump.reserve_bootvar)
27381d9eca5SHari Bathini 			pr_info("Using 'crashkernel=' parameter for memory reservation.\n");
27481d9eca5SHari Bathini 
27511550dc0SHari Bathini 		fw_dump.reserve_bootvar = (unsigned long)size;
27648a316e3SHari Bathini 
27748a316e3SHari Bathini 		/*
27848a316e3SHari Bathini 		 * Adjust if the boot memory size specified is above
27948a316e3SHari Bathini 		 * the upper limit.
28048a316e3SHari Bathini 		 */
28148a316e3SHari Bathini 		max_size = memblock_phys_mem_size() / MAX_BOOT_MEM_RATIO;
28248a316e3SHari Bathini 		if (fw_dump.reserve_bootvar > max_size) {
28348a316e3SHari Bathini 			fw_dump.reserve_bootvar = max_size;
28448a316e3SHari Bathini 			pr_info("Adjusted boot memory size to %luMB\n",
28548a316e3SHari Bathini 				(fw_dump.reserve_bootvar >> 20));
28648a316e3SHari Bathini 		}
28748a316e3SHari Bathini 
288eb39c880SMahesh Salgaonkar 		return fw_dump.reserve_bootvar;
28981d9eca5SHari Bathini 	} else if (fw_dump.reserve_bootvar) {
29081d9eca5SHari Bathini 		/*
29181d9eca5SHari Bathini 		 * 'fadump_reserve_mem=' is being used to reserve memory
29281d9eca5SHari Bathini 		 * for firmware-assisted dump.
29381d9eca5SHari Bathini 		 */
29481d9eca5SHari Bathini 		return fw_dump.reserve_bootvar;
29511550dc0SHari Bathini 	}
296eb39c880SMahesh Salgaonkar 
297eb39c880SMahesh Salgaonkar 	/* divide by 20 to get 5% of value */
29848a316e3SHari Bathini 	size = memblock_phys_mem_size() / 20;
299eb39c880SMahesh Salgaonkar 
300eb39c880SMahesh Salgaonkar 	/* round it down in multiples of 256 */
301eb39c880SMahesh Salgaonkar 	size = size & ~0x0FFFFFFFUL;
302eb39c880SMahesh Salgaonkar 
303eb39c880SMahesh Salgaonkar 	/* Truncate to memory_limit. We don't want to over reserve the memory.*/
304eb39c880SMahesh Salgaonkar 	if (memory_limit && size > memory_limit)
305eb39c880SMahesh Salgaonkar 		size = memory_limit;
306eb39c880SMahesh Salgaonkar 
307eb39c880SMahesh Salgaonkar 	return (size > MIN_BOOT_MEM ? size : MIN_BOOT_MEM);
308eb39c880SMahesh Salgaonkar }
309eb39c880SMahesh Salgaonkar 
310eb39c880SMahesh Salgaonkar /*
311eb39c880SMahesh Salgaonkar  * Calculate the total memory size required to be reserved for
312eb39c880SMahesh Salgaonkar  * firmware-assisted dump registration.
313eb39c880SMahesh Salgaonkar  */
314eb39c880SMahesh Salgaonkar static unsigned long get_fadump_area_size(void)
315eb39c880SMahesh Salgaonkar {
316eb39c880SMahesh Salgaonkar 	unsigned long size = 0;
317eb39c880SMahesh Salgaonkar 
318eb39c880SMahesh Salgaonkar 	size += fw_dump.cpu_state_data_size;
319eb39c880SMahesh Salgaonkar 	size += fw_dump.hpte_region_size;
320eb39c880SMahesh Salgaonkar 	size += fw_dump.boot_memory_size;
3212df173d9SMahesh Salgaonkar 	size += sizeof(struct fadump_crash_info_header);
3222df173d9SMahesh Salgaonkar 	size += sizeof(struct elfhdr); /* ELF core header.*/
323ebaeb5aeSMahesh Salgaonkar 	size += sizeof(struct elf_phdr); /* place holder for cpu notes */
3242df173d9SMahesh Salgaonkar 	/* Program headers for crash memory regions. */
3252df173d9SMahesh Salgaonkar 	size += sizeof(struct elf_phdr) * (memblock_num_regions(memory) + 2);
326eb39c880SMahesh Salgaonkar 
327eb39c880SMahesh Salgaonkar 	size = PAGE_ALIGN(size);
328eb39c880SMahesh Salgaonkar 	return size;
329eb39c880SMahesh Salgaonkar }
330eb39c880SMahesh Salgaonkar 
331eb39c880SMahesh Salgaonkar int __init fadump_reserve_mem(void)
332eb39c880SMahesh Salgaonkar {
333eb39c880SMahesh Salgaonkar 	unsigned long base, size, memory_boundary;
334eb39c880SMahesh Salgaonkar 
335eb39c880SMahesh Salgaonkar 	if (!fw_dump.fadump_enabled)
336eb39c880SMahesh Salgaonkar 		return 0;
337eb39c880SMahesh Salgaonkar 
338eb39c880SMahesh Salgaonkar 	if (!fw_dump.fadump_supported) {
339eb39c880SMahesh Salgaonkar 		printk(KERN_INFO "Firmware-assisted dump is not supported on"
340eb39c880SMahesh Salgaonkar 				" this hardware\n");
341eb39c880SMahesh Salgaonkar 		fw_dump.fadump_enabled = 0;
342eb39c880SMahesh Salgaonkar 		return 0;
343eb39c880SMahesh Salgaonkar 	}
3443ccc00a7SMahesh Salgaonkar 	/*
3453ccc00a7SMahesh Salgaonkar 	 * Initialize boot memory size
3463ccc00a7SMahesh Salgaonkar 	 * If dump is active then we have already calculated the size during
3473ccc00a7SMahesh Salgaonkar 	 * first kernel.
3483ccc00a7SMahesh Salgaonkar 	 */
3493ccc00a7SMahesh Salgaonkar 	if (fdm_active)
350408cddd9SHari Bathini 		fw_dump.boot_memory_size = be64_to_cpu(fdm_active->rmr_region.source_len);
3513ccc00a7SMahesh Salgaonkar 	else
352eb39c880SMahesh Salgaonkar 		fw_dump.boot_memory_size = fadump_calculate_reserve_size();
353eb39c880SMahesh Salgaonkar 
354eb39c880SMahesh Salgaonkar 	/*
355eb39c880SMahesh Salgaonkar 	 * Calculate the memory boundary.
356eb39c880SMahesh Salgaonkar 	 * If memory_limit is less than actual memory boundary then reserve
357eb39c880SMahesh Salgaonkar 	 * the memory for fadump beyond the memory_limit and adjust the
358eb39c880SMahesh Salgaonkar 	 * memory_limit accordingly, so that the running kernel can run with
359eb39c880SMahesh Salgaonkar 	 * specified memory_limit.
360eb39c880SMahesh Salgaonkar 	 */
361eb39c880SMahesh Salgaonkar 	if (memory_limit && memory_limit < memblock_end_of_DRAM()) {
362eb39c880SMahesh Salgaonkar 		size = get_fadump_area_size();
363eb39c880SMahesh Salgaonkar 		if ((memory_limit + size) < memblock_end_of_DRAM())
364eb39c880SMahesh Salgaonkar 			memory_limit += size;
365eb39c880SMahesh Salgaonkar 		else
366eb39c880SMahesh Salgaonkar 			memory_limit = memblock_end_of_DRAM();
367eb39c880SMahesh Salgaonkar 		printk(KERN_INFO "Adjusted memory_limit for firmware-assisted"
368a84fcd46SSuzuki Poulose 				" dump, now %#016llx\n", memory_limit);
369eb39c880SMahesh Salgaonkar 	}
370eb39c880SMahesh Salgaonkar 	if (memory_limit)
371eb39c880SMahesh Salgaonkar 		memory_boundary = memory_limit;
372eb39c880SMahesh Salgaonkar 	else
373eb39c880SMahesh Salgaonkar 		memory_boundary = memblock_end_of_DRAM();
374eb39c880SMahesh Salgaonkar 
375eb39c880SMahesh Salgaonkar 	if (fw_dump.dump_active) {
376eb39c880SMahesh Salgaonkar 		printk(KERN_INFO "Firmware-assisted dump is active.\n");
377eb39c880SMahesh Salgaonkar 		/*
378eb39c880SMahesh Salgaonkar 		 * If last boot has crashed then reserve all the memory
379eb39c880SMahesh Salgaonkar 		 * above boot_memory_size so that we don't touch it until
380eb39c880SMahesh Salgaonkar 		 * dump is written to disk by userspace tool. This memory
381eb39c880SMahesh Salgaonkar 		 * will be released for general use once the dump is saved.
382eb39c880SMahesh Salgaonkar 		 */
383eb39c880SMahesh Salgaonkar 		base = fw_dump.boot_memory_size;
384eb39c880SMahesh Salgaonkar 		size = memory_boundary - base;
385eb39c880SMahesh Salgaonkar 		memblock_reserve(base, size);
386eb39c880SMahesh Salgaonkar 		printk(KERN_INFO "Reserved %ldMB of memory at %ldMB "
387eb39c880SMahesh Salgaonkar 				"for saving crash dump\n",
388eb39c880SMahesh Salgaonkar 				(unsigned long)(size >> 20),
389eb39c880SMahesh Salgaonkar 				(unsigned long)(base >> 20));
3902df173d9SMahesh Salgaonkar 
3912df173d9SMahesh Salgaonkar 		fw_dump.fadumphdr_addr =
392408cddd9SHari Bathini 				be64_to_cpu(fdm_active->rmr_region.destination_address) +
393408cddd9SHari Bathini 				be64_to_cpu(fdm_active->rmr_region.source_len);
3942df173d9SMahesh Salgaonkar 		pr_debug("fadumphdr_addr = %p\n",
3952df173d9SMahesh Salgaonkar 				(void *) fw_dump.fadumphdr_addr);
396eb39c880SMahesh Salgaonkar 	} else {
397eb39c880SMahesh Salgaonkar 		size = get_fadump_area_size();
398f6e6bedbSHari Bathini 
399f6e6bedbSHari Bathini 		/*
400f6e6bedbSHari Bathini 		 * Reserve memory at an offset closer to bottom of the RAM to
401f6e6bedbSHari Bathini 		 * minimize the impact of memory hot-remove operation. We can't
402f6e6bedbSHari Bathini 		 * use memblock_find_in_range() here since it doesn't allocate
403f6e6bedbSHari Bathini 		 * from bottom to top.
404f6e6bedbSHari Bathini 		 */
405f6e6bedbSHari Bathini 		for (base = fw_dump.boot_memory_size;
406f6e6bedbSHari Bathini 		     base <= (memory_boundary - size);
407f6e6bedbSHari Bathini 		     base += size) {
408f6e6bedbSHari Bathini 			if (memblock_is_region_memory(base, size) &&
409f6e6bedbSHari Bathini 			    !memblock_is_region_reserved(base, size))
410f6e6bedbSHari Bathini 				break;
411eb39c880SMahesh Salgaonkar 		}
412f6e6bedbSHari Bathini 		if ((base > (memory_boundary - size)) ||
413f6e6bedbSHari Bathini 		    memblock_reserve(base, size)) {
414f6e6bedbSHari Bathini 			pr_err("Failed to reserve memory\n");
415f6e6bedbSHari Bathini 			return 0;
416f6e6bedbSHari Bathini 		}
417f6e6bedbSHari Bathini 
418f6e6bedbSHari Bathini 		pr_info("Reserved %ldMB of memory at %ldMB for firmware-"
419f6e6bedbSHari Bathini 			"assisted dump (System RAM: %ldMB)\n",
420f6e6bedbSHari Bathini 			(unsigned long)(size >> 20),
421f6e6bedbSHari Bathini 			(unsigned long)(base >> 20),
422f6e6bedbSHari Bathini 			(unsigned long)(memblock_phys_mem_size() >> 20));
423f6e6bedbSHari Bathini 	}
424f6e6bedbSHari Bathini 
425eb39c880SMahesh Salgaonkar 	fw_dump.reserve_dump_area_start = base;
426eb39c880SMahesh Salgaonkar 	fw_dump.reserve_dump_area_size = size;
427eb39c880SMahesh Salgaonkar 	return 1;
428eb39c880SMahesh Salgaonkar }
429eb39c880SMahesh Salgaonkar 
4301e76609cSSrikar Dronamraju unsigned long __init arch_reserved_kernel_pages(void)
4311e76609cSSrikar Dronamraju {
4321e76609cSSrikar Dronamraju 	return memblock_reserved_size() / PAGE_SIZE;
4331e76609cSSrikar Dronamraju }
4341e76609cSSrikar Dronamraju 
435eb39c880SMahesh Salgaonkar /* Look for fadump= cmdline option. */
436eb39c880SMahesh Salgaonkar static int __init early_fadump_param(char *p)
437eb39c880SMahesh Salgaonkar {
438eb39c880SMahesh Salgaonkar 	if (!p)
439eb39c880SMahesh Salgaonkar 		return 1;
440eb39c880SMahesh Salgaonkar 
441eb39c880SMahesh Salgaonkar 	if (strncmp(p, "on", 2) == 0)
442eb39c880SMahesh Salgaonkar 		fw_dump.fadump_enabled = 1;
443eb39c880SMahesh Salgaonkar 	else if (strncmp(p, "off", 3) == 0)
444eb39c880SMahesh Salgaonkar 		fw_dump.fadump_enabled = 0;
445eb39c880SMahesh Salgaonkar 
446eb39c880SMahesh Salgaonkar 	return 0;
447eb39c880SMahesh Salgaonkar }
448eb39c880SMahesh Salgaonkar early_param("fadump", early_fadump_param);
449eb39c880SMahesh Salgaonkar 
45081d9eca5SHari Bathini /*
45181d9eca5SHari Bathini  * Look for fadump_reserve_mem= cmdline option
45281d9eca5SHari Bathini  * TODO: Remove references to 'fadump_reserve_mem=' parameter,
45381d9eca5SHari Bathini  *       the sooner 'crashkernel=' parameter is accustomed to.
45481d9eca5SHari Bathini  */
45581d9eca5SHari Bathini static int __init early_fadump_reserve_mem(char *p)
45681d9eca5SHari Bathini {
45781d9eca5SHari Bathini 	if (p)
45881d9eca5SHari Bathini 		fw_dump.reserve_bootvar = memparse(p, &p);
45981d9eca5SHari Bathini 	return 0;
46081d9eca5SHari Bathini }
46181d9eca5SHari Bathini early_param("fadump_reserve_mem", early_fadump_reserve_mem);
46281d9eca5SHari Bathini 
46398b8cd7fSMichal Suchanek static int register_fw_dump(struct fadump_mem_struct *fdm)
4643ccc00a7SMahesh Salgaonkar {
46598b8cd7fSMichal Suchanek 	int rc, err;
4663ccc00a7SMahesh Salgaonkar 	unsigned int wait_time;
4673ccc00a7SMahesh Salgaonkar 
4683ccc00a7SMahesh Salgaonkar 	pr_debug("Registering for firmware-assisted kernel dump...\n");
4693ccc00a7SMahesh Salgaonkar 
4703ccc00a7SMahesh Salgaonkar 	/* TODO: Add upper time limit for the delay */
4713ccc00a7SMahesh Salgaonkar 	do {
4723ccc00a7SMahesh Salgaonkar 		rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
4733ccc00a7SMahesh Salgaonkar 			FADUMP_REGISTER, fdm,
4743ccc00a7SMahesh Salgaonkar 			sizeof(struct fadump_mem_struct));
4753ccc00a7SMahesh Salgaonkar 
4763ccc00a7SMahesh Salgaonkar 		wait_time = rtas_busy_delay_time(rc);
4773ccc00a7SMahesh Salgaonkar 		if (wait_time)
4783ccc00a7SMahesh Salgaonkar 			mdelay(wait_time);
4793ccc00a7SMahesh Salgaonkar 
4803ccc00a7SMahesh Salgaonkar 	} while (wait_time);
4813ccc00a7SMahesh Salgaonkar 
48298b8cd7fSMichal Suchanek 	err = -EIO;
4833ccc00a7SMahesh Salgaonkar 	switch (rc) {
48498b8cd7fSMichal Suchanek 	default:
48598b8cd7fSMichal Suchanek 		pr_err("Failed to register. Unknown Error(%d).\n", rc);
48698b8cd7fSMichal Suchanek 		break;
4873ccc00a7SMahesh Salgaonkar 	case -1:
4883ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "Failed to register firmware-assisted kernel"
4893ccc00a7SMahesh Salgaonkar 			" dump. Hardware Error(%d).\n", rc);
4903ccc00a7SMahesh Salgaonkar 		break;
4913ccc00a7SMahesh Salgaonkar 	case -3:
492a5a05b91SHari Bathini 		if (!is_boot_memory_area_contiguous())
493a5a05b91SHari Bathini 			pr_err("Can't have holes in boot memory area while "
494a5a05b91SHari Bathini 			       "registering fadump\n");
495a5a05b91SHari Bathini 
4963ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "Failed to register firmware-assisted kernel"
4973ccc00a7SMahesh Salgaonkar 			" dump. Parameter Error(%d).\n", rc);
49898b8cd7fSMichal Suchanek 		err = -EINVAL;
4993ccc00a7SMahesh Salgaonkar 		break;
5003ccc00a7SMahesh Salgaonkar 	case -9:
5013ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "firmware-assisted kernel dump is already "
5023ccc00a7SMahesh Salgaonkar 			" registered.");
5033ccc00a7SMahesh Salgaonkar 		fw_dump.dump_registered = 1;
50498b8cd7fSMichal Suchanek 		err = -EEXIST;
5053ccc00a7SMahesh Salgaonkar 		break;
5063ccc00a7SMahesh Salgaonkar 	case 0:
5073ccc00a7SMahesh Salgaonkar 		printk(KERN_INFO "firmware-assisted kernel dump registration"
5083ccc00a7SMahesh Salgaonkar 			" is successful\n");
5093ccc00a7SMahesh Salgaonkar 		fw_dump.dump_registered = 1;
51098b8cd7fSMichal Suchanek 		err = 0;
5113ccc00a7SMahesh Salgaonkar 		break;
5123ccc00a7SMahesh Salgaonkar 	}
51398b8cd7fSMichal Suchanek 	return err;
5143ccc00a7SMahesh Salgaonkar }
5153ccc00a7SMahesh Salgaonkar 
516ebaeb5aeSMahesh Salgaonkar void crash_fadump(struct pt_regs *regs, const char *str)
517ebaeb5aeSMahesh Salgaonkar {
518ebaeb5aeSMahesh Salgaonkar 	struct fadump_crash_info_header *fdh = NULL;
519f2a5e8f0SMahesh Salgaonkar 	int old_cpu, this_cpu;
520ebaeb5aeSMahesh Salgaonkar 
521ebaeb5aeSMahesh Salgaonkar 	if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr)
522ebaeb5aeSMahesh Salgaonkar 		return;
523ebaeb5aeSMahesh Salgaonkar 
524f2a5e8f0SMahesh Salgaonkar 	/*
525f2a5e8f0SMahesh Salgaonkar 	 * old_cpu == -1 means this is the first CPU which has come here,
526f2a5e8f0SMahesh Salgaonkar 	 * go ahead and trigger fadump.
527f2a5e8f0SMahesh Salgaonkar 	 *
528f2a5e8f0SMahesh Salgaonkar 	 * old_cpu != -1 means some other CPU has already on it's way
529f2a5e8f0SMahesh Salgaonkar 	 * to trigger fadump, just keep looping here.
530f2a5e8f0SMahesh Salgaonkar 	 */
531f2a5e8f0SMahesh Salgaonkar 	this_cpu = smp_processor_id();
532f2a5e8f0SMahesh Salgaonkar 	old_cpu = cmpxchg(&crashing_cpu, -1, this_cpu);
533f2a5e8f0SMahesh Salgaonkar 
534f2a5e8f0SMahesh Salgaonkar 	if (old_cpu != -1) {
535f2a5e8f0SMahesh Salgaonkar 		/*
536f2a5e8f0SMahesh Salgaonkar 		 * We can't loop here indefinitely. Wait as long as fadump
537f2a5e8f0SMahesh Salgaonkar 		 * is in force. If we race with fadump un-registration this
538f2a5e8f0SMahesh Salgaonkar 		 * loop will break and then we go down to normal panic path
539f2a5e8f0SMahesh Salgaonkar 		 * and reboot. If fadump is in force the first crashing
540f2a5e8f0SMahesh Salgaonkar 		 * cpu will definitely trigger fadump.
541f2a5e8f0SMahesh Salgaonkar 		 */
542f2a5e8f0SMahesh Salgaonkar 		while (fw_dump.dump_registered)
543f2a5e8f0SMahesh Salgaonkar 			cpu_relax();
544f2a5e8f0SMahesh Salgaonkar 		return;
545f2a5e8f0SMahesh Salgaonkar 	}
546f2a5e8f0SMahesh Salgaonkar 
547ebaeb5aeSMahesh Salgaonkar 	fdh = __va(fw_dump.fadumphdr_addr);
548ebaeb5aeSMahesh Salgaonkar 	fdh->crashing_cpu = crashing_cpu;
549ebaeb5aeSMahesh Salgaonkar 	crash_save_vmcoreinfo();
550ebaeb5aeSMahesh Salgaonkar 
551ebaeb5aeSMahesh Salgaonkar 	if (regs)
552ebaeb5aeSMahesh Salgaonkar 		fdh->regs = *regs;
553ebaeb5aeSMahesh Salgaonkar 	else
554ebaeb5aeSMahesh Salgaonkar 		ppc_save_regs(&fdh->regs);
555ebaeb5aeSMahesh Salgaonkar 
556a0512164SRasmus Villemoes 	fdh->online_mask = *cpu_online_mask;
557ebaeb5aeSMahesh Salgaonkar 
558ebaeb5aeSMahesh Salgaonkar 	/* Call ibm,os-term rtas call to trigger firmware assisted dump */
559ebaeb5aeSMahesh Salgaonkar 	rtas_os_term((char *)str);
560ebaeb5aeSMahesh Salgaonkar }
561ebaeb5aeSMahesh Salgaonkar 
562ebaeb5aeSMahesh Salgaonkar #define GPR_MASK	0xffffff0000000000
563ebaeb5aeSMahesh Salgaonkar static inline int fadump_gpr_index(u64 id)
564ebaeb5aeSMahesh Salgaonkar {
565ebaeb5aeSMahesh Salgaonkar 	int i = -1;
566ebaeb5aeSMahesh Salgaonkar 	char str[3];
567ebaeb5aeSMahesh Salgaonkar 
568ebaeb5aeSMahesh Salgaonkar 	if ((id & GPR_MASK) == REG_ID("GPR")) {
569ebaeb5aeSMahesh Salgaonkar 		/* get the digits at the end */
570ebaeb5aeSMahesh Salgaonkar 		id &= ~GPR_MASK;
571ebaeb5aeSMahesh Salgaonkar 		id >>= 24;
572ebaeb5aeSMahesh Salgaonkar 		str[2] = '\0';
573ebaeb5aeSMahesh Salgaonkar 		str[1] = id & 0xff;
574ebaeb5aeSMahesh Salgaonkar 		str[0] = (id >> 8) & 0xff;
575ebaeb5aeSMahesh Salgaonkar 		sscanf(str, "%d", &i);
576ebaeb5aeSMahesh Salgaonkar 		if (i > 31)
577ebaeb5aeSMahesh Salgaonkar 			i = -1;
578ebaeb5aeSMahesh Salgaonkar 	}
579ebaeb5aeSMahesh Salgaonkar 	return i;
580ebaeb5aeSMahesh Salgaonkar }
581ebaeb5aeSMahesh Salgaonkar 
582ebaeb5aeSMahesh Salgaonkar static inline void fadump_set_regval(struct pt_regs *regs, u64 reg_id,
583ebaeb5aeSMahesh Salgaonkar 								u64 reg_val)
584ebaeb5aeSMahesh Salgaonkar {
585ebaeb5aeSMahesh Salgaonkar 	int i;
586ebaeb5aeSMahesh Salgaonkar 
587ebaeb5aeSMahesh Salgaonkar 	i = fadump_gpr_index(reg_id);
588ebaeb5aeSMahesh Salgaonkar 	if (i >= 0)
589ebaeb5aeSMahesh Salgaonkar 		regs->gpr[i] = (unsigned long)reg_val;
590ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("NIA"))
591ebaeb5aeSMahesh Salgaonkar 		regs->nip = (unsigned long)reg_val;
592ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("MSR"))
593ebaeb5aeSMahesh Salgaonkar 		regs->msr = (unsigned long)reg_val;
594ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("CTR"))
595ebaeb5aeSMahesh Salgaonkar 		regs->ctr = (unsigned long)reg_val;
596ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("LR"))
597ebaeb5aeSMahesh Salgaonkar 		regs->link = (unsigned long)reg_val;
598ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("XER"))
599ebaeb5aeSMahesh Salgaonkar 		regs->xer = (unsigned long)reg_val;
600ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("CR"))
601ebaeb5aeSMahesh Salgaonkar 		regs->ccr = (unsigned long)reg_val;
602ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("DAR"))
603ebaeb5aeSMahesh Salgaonkar 		regs->dar = (unsigned long)reg_val;
604ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("DSISR"))
605ebaeb5aeSMahesh Salgaonkar 		regs->dsisr = (unsigned long)reg_val;
606ebaeb5aeSMahesh Salgaonkar }
607ebaeb5aeSMahesh Salgaonkar 
608ebaeb5aeSMahesh Salgaonkar static struct fadump_reg_entry*
609ebaeb5aeSMahesh Salgaonkar fadump_read_registers(struct fadump_reg_entry *reg_entry, struct pt_regs *regs)
610ebaeb5aeSMahesh Salgaonkar {
611ebaeb5aeSMahesh Salgaonkar 	memset(regs, 0, sizeof(struct pt_regs));
612ebaeb5aeSMahesh Salgaonkar 
613408cddd9SHari Bathini 	while (be64_to_cpu(reg_entry->reg_id) != REG_ID("CPUEND")) {
614408cddd9SHari Bathini 		fadump_set_regval(regs, be64_to_cpu(reg_entry->reg_id),
615408cddd9SHari Bathini 					be64_to_cpu(reg_entry->reg_value));
616ebaeb5aeSMahesh Salgaonkar 		reg_entry++;
617ebaeb5aeSMahesh Salgaonkar 	}
618ebaeb5aeSMahesh Salgaonkar 	reg_entry++;
619ebaeb5aeSMahesh Salgaonkar 	return reg_entry;
620ebaeb5aeSMahesh Salgaonkar }
621ebaeb5aeSMahesh Salgaonkar 
622ebaeb5aeSMahesh Salgaonkar static u32 *fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs)
623ebaeb5aeSMahesh Salgaonkar {
624ebaeb5aeSMahesh Salgaonkar 	struct elf_prstatus prstatus;
625ebaeb5aeSMahesh Salgaonkar 
626ebaeb5aeSMahesh Salgaonkar 	memset(&prstatus, 0, sizeof(prstatus));
627ebaeb5aeSMahesh Salgaonkar 	/*
628ebaeb5aeSMahesh Salgaonkar 	 * FIXME: How do i get PID? Do I really need it?
629ebaeb5aeSMahesh Salgaonkar 	 * prstatus.pr_pid = ????
630ebaeb5aeSMahesh Salgaonkar 	 */
631ebaeb5aeSMahesh Salgaonkar 	elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
63222bd0177SHari Bathini 	buf = append_elf_note(buf, CRASH_CORE_NOTE_NAME, NT_PRSTATUS,
633ebaeb5aeSMahesh Salgaonkar 			      &prstatus, sizeof(prstatus));
634ebaeb5aeSMahesh Salgaonkar 	return buf;
635ebaeb5aeSMahesh Salgaonkar }
636ebaeb5aeSMahesh Salgaonkar 
637ebaeb5aeSMahesh Salgaonkar static void fadump_update_elfcore_header(char *bufp)
638ebaeb5aeSMahesh Salgaonkar {
639ebaeb5aeSMahesh Salgaonkar 	struct elfhdr *elf;
640ebaeb5aeSMahesh Salgaonkar 	struct elf_phdr *phdr;
641ebaeb5aeSMahesh Salgaonkar 
642ebaeb5aeSMahesh Salgaonkar 	elf = (struct elfhdr *)bufp;
643ebaeb5aeSMahesh Salgaonkar 	bufp += sizeof(struct elfhdr);
644ebaeb5aeSMahesh Salgaonkar 
645ebaeb5aeSMahesh Salgaonkar 	/* First note is a place holder for cpu notes info. */
646ebaeb5aeSMahesh Salgaonkar 	phdr = (struct elf_phdr *)bufp;
647ebaeb5aeSMahesh Salgaonkar 
648ebaeb5aeSMahesh Salgaonkar 	if (phdr->p_type == PT_NOTE) {
649ebaeb5aeSMahesh Salgaonkar 		phdr->p_paddr = fw_dump.cpu_notes_buf;
650ebaeb5aeSMahesh Salgaonkar 		phdr->p_offset	= phdr->p_paddr;
651ebaeb5aeSMahesh Salgaonkar 		phdr->p_filesz	= fw_dump.cpu_notes_buf_size;
652ebaeb5aeSMahesh Salgaonkar 		phdr->p_memsz = fw_dump.cpu_notes_buf_size;
653ebaeb5aeSMahesh Salgaonkar 	}
654ebaeb5aeSMahesh Salgaonkar 	return;
655ebaeb5aeSMahesh Salgaonkar }
656ebaeb5aeSMahesh Salgaonkar 
657ebaeb5aeSMahesh Salgaonkar static void *fadump_cpu_notes_buf_alloc(unsigned long size)
658ebaeb5aeSMahesh Salgaonkar {
659ebaeb5aeSMahesh Salgaonkar 	void *vaddr;
660ebaeb5aeSMahesh Salgaonkar 	struct page *page;
661ebaeb5aeSMahesh Salgaonkar 	unsigned long order, count, i;
662ebaeb5aeSMahesh Salgaonkar 
663ebaeb5aeSMahesh Salgaonkar 	order = get_order(size);
664ebaeb5aeSMahesh Salgaonkar 	vaddr = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
665ebaeb5aeSMahesh Salgaonkar 	if (!vaddr)
666ebaeb5aeSMahesh Salgaonkar 		return NULL;
667ebaeb5aeSMahesh Salgaonkar 
668ebaeb5aeSMahesh Salgaonkar 	count = 1 << order;
669ebaeb5aeSMahesh Salgaonkar 	page = virt_to_page(vaddr);
670ebaeb5aeSMahesh Salgaonkar 	for (i = 0; i < count; i++)
671ebaeb5aeSMahesh Salgaonkar 		SetPageReserved(page + i);
672ebaeb5aeSMahesh Salgaonkar 	return vaddr;
673ebaeb5aeSMahesh Salgaonkar }
674ebaeb5aeSMahesh Salgaonkar 
675ebaeb5aeSMahesh Salgaonkar static void fadump_cpu_notes_buf_free(unsigned long vaddr, unsigned long size)
676ebaeb5aeSMahesh Salgaonkar {
677ebaeb5aeSMahesh Salgaonkar 	struct page *page;
678ebaeb5aeSMahesh Salgaonkar 	unsigned long order, count, i;
679ebaeb5aeSMahesh Salgaonkar 
680ebaeb5aeSMahesh Salgaonkar 	order = get_order(size);
681ebaeb5aeSMahesh Salgaonkar 	count = 1 << order;
682ebaeb5aeSMahesh Salgaonkar 	page = virt_to_page(vaddr);
683ebaeb5aeSMahesh Salgaonkar 	for (i = 0; i < count; i++)
684ebaeb5aeSMahesh Salgaonkar 		ClearPageReserved(page + i);
685ebaeb5aeSMahesh Salgaonkar 	__free_pages(page, order);
686ebaeb5aeSMahesh Salgaonkar }
687ebaeb5aeSMahesh Salgaonkar 
688ebaeb5aeSMahesh Salgaonkar /*
689ebaeb5aeSMahesh Salgaonkar  * Read CPU state dump data and convert it into ELF notes.
690ebaeb5aeSMahesh Salgaonkar  * The CPU dump starts with magic number "REGSAVE". NumCpusOffset should be
691ebaeb5aeSMahesh Salgaonkar  * used to access the data to allow for additional fields to be added without
692ebaeb5aeSMahesh Salgaonkar  * affecting compatibility. Each list of registers for a CPU starts with
693ebaeb5aeSMahesh Salgaonkar  * "CPUSTRT" and ends with "CPUEND". Each register entry is of 16 bytes,
694ebaeb5aeSMahesh Salgaonkar  * 8 Byte ASCII identifier and 8 Byte register value. The register entry
695ebaeb5aeSMahesh Salgaonkar  * with identifier "CPUSTRT" and "CPUEND" contains 4 byte cpu id as part
696ebaeb5aeSMahesh Salgaonkar  * of register value. For more details refer to PAPR document.
697ebaeb5aeSMahesh Salgaonkar  *
698ebaeb5aeSMahesh Salgaonkar  * Only for the crashing cpu we ignore the CPU dump data and get exact
699ebaeb5aeSMahesh Salgaonkar  * state from fadump crash info structure populated by first kernel at the
700ebaeb5aeSMahesh Salgaonkar  * time of crash.
701ebaeb5aeSMahesh Salgaonkar  */
702ebaeb5aeSMahesh Salgaonkar static int __init fadump_build_cpu_notes(const struct fadump_mem_struct *fdm)
703ebaeb5aeSMahesh Salgaonkar {
704ebaeb5aeSMahesh Salgaonkar 	struct fadump_reg_save_area_header *reg_header;
705ebaeb5aeSMahesh Salgaonkar 	struct fadump_reg_entry *reg_entry;
706ebaeb5aeSMahesh Salgaonkar 	struct fadump_crash_info_header *fdh = NULL;
707ebaeb5aeSMahesh Salgaonkar 	void *vaddr;
708ebaeb5aeSMahesh Salgaonkar 	unsigned long addr;
709ebaeb5aeSMahesh Salgaonkar 	u32 num_cpus, *note_buf;
710ebaeb5aeSMahesh Salgaonkar 	struct pt_regs regs;
711ebaeb5aeSMahesh Salgaonkar 	int i, rc = 0, cpu = 0;
712ebaeb5aeSMahesh Salgaonkar 
713ebaeb5aeSMahesh Salgaonkar 	if (!fdm->cpu_state_data.bytes_dumped)
714ebaeb5aeSMahesh Salgaonkar 		return -EINVAL;
715ebaeb5aeSMahesh Salgaonkar 
716408cddd9SHari Bathini 	addr = be64_to_cpu(fdm->cpu_state_data.destination_address);
717ebaeb5aeSMahesh Salgaonkar 	vaddr = __va(addr);
718ebaeb5aeSMahesh Salgaonkar 
719ebaeb5aeSMahesh Salgaonkar 	reg_header = vaddr;
720408cddd9SHari Bathini 	if (be64_to_cpu(reg_header->magic_number) != REGSAVE_AREA_MAGIC) {
721ebaeb5aeSMahesh Salgaonkar 		printk(KERN_ERR "Unable to read register save area.\n");
722ebaeb5aeSMahesh Salgaonkar 		return -ENOENT;
723ebaeb5aeSMahesh Salgaonkar 	}
724ebaeb5aeSMahesh Salgaonkar 	pr_debug("--------CPU State Data------------\n");
725408cddd9SHari Bathini 	pr_debug("Magic Number: %llx\n", be64_to_cpu(reg_header->magic_number));
726408cddd9SHari Bathini 	pr_debug("NumCpuOffset: %x\n", be32_to_cpu(reg_header->num_cpu_offset));
727ebaeb5aeSMahesh Salgaonkar 
728408cddd9SHari Bathini 	vaddr += be32_to_cpu(reg_header->num_cpu_offset);
729408cddd9SHari Bathini 	num_cpus = be32_to_cpu(*((__be32 *)(vaddr)));
730ebaeb5aeSMahesh Salgaonkar 	pr_debug("NumCpus     : %u\n", num_cpus);
731ebaeb5aeSMahesh Salgaonkar 	vaddr += sizeof(u32);
732ebaeb5aeSMahesh Salgaonkar 	reg_entry = (struct fadump_reg_entry *)vaddr;
733ebaeb5aeSMahesh Salgaonkar 
734ebaeb5aeSMahesh Salgaonkar 	/* Allocate buffer to hold cpu crash notes. */
735ebaeb5aeSMahesh Salgaonkar 	fw_dump.cpu_notes_buf_size = num_cpus * sizeof(note_buf_t);
736ebaeb5aeSMahesh Salgaonkar 	fw_dump.cpu_notes_buf_size = PAGE_ALIGN(fw_dump.cpu_notes_buf_size);
737ebaeb5aeSMahesh Salgaonkar 	note_buf = fadump_cpu_notes_buf_alloc(fw_dump.cpu_notes_buf_size);
738ebaeb5aeSMahesh Salgaonkar 	if (!note_buf) {
739ebaeb5aeSMahesh Salgaonkar 		printk(KERN_ERR "Failed to allocate 0x%lx bytes for "
740ebaeb5aeSMahesh Salgaonkar 			"cpu notes buffer\n", fw_dump.cpu_notes_buf_size);
741ebaeb5aeSMahesh Salgaonkar 		return -ENOMEM;
742ebaeb5aeSMahesh Salgaonkar 	}
743ebaeb5aeSMahesh Salgaonkar 	fw_dump.cpu_notes_buf = __pa(note_buf);
744ebaeb5aeSMahesh Salgaonkar 
745ebaeb5aeSMahesh Salgaonkar 	pr_debug("Allocated buffer for cpu notes of size %ld at %p\n",
746ebaeb5aeSMahesh Salgaonkar 			(num_cpus * sizeof(note_buf_t)), note_buf);
747ebaeb5aeSMahesh Salgaonkar 
748ebaeb5aeSMahesh Salgaonkar 	if (fw_dump.fadumphdr_addr)
749ebaeb5aeSMahesh Salgaonkar 		fdh = __va(fw_dump.fadumphdr_addr);
750ebaeb5aeSMahesh Salgaonkar 
751ebaeb5aeSMahesh Salgaonkar 	for (i = 0; i < num_cpus; i++) {
752408cddd9SHari Bathini 		if (be64_to_cpu(reg_entry->reg_id) != REG_ID("CPUSTRT")) {
753ebaeb5aeSMahesh Salgaonkar 			printk(KERN_ERR "Unable to read CPU state data\n");
754ebaeb5aeSMahesh Salgaonkar 			rc = -ENOENT;
755ebaeb5aeSMahesh Salgaonkar 			goto error_out;
756ebaeb5aeSMahesh Salgaonkar 		}
757ebaeb5aeSMahesh Salgaonkar 		/* Lower 4 bytes of reg_value contains logical cpu id */
758408cddd9SHari Bathini 		cpu = be64_to_cpu(reg_entry->reg_value) & FADUMP_CPU_ID_MASK;
759a0512164SRasmus Villemoes 		if (fdh && !cpumask_test_cpu(cpu, &fdh->online_mask)) {
760ebaeb5aeSMahesh Salgaonkar 			SKIP_TO_NEXT_CPU(reg_entry);
761ebaeb5aeSMahesh Salgaonkar 			continue;
762ebaeb5aeSMahesh Salgaonkar 		}
763ebaeb5aeSMahesh Salgaonkar 		pr_debug("Reading register data for cpu %d...\n", cpu);
764ebaeb5aeSMahesh Salgaonkar 		if (fdh && fdh->crashing_cpu == cpu) {
765ebaeb5aeSMahesh Salgaonkar 			regs = fdh->regs;
766ebaeb5aeSMahesh Salgaonkar 			note_buf = fadump_regs_to_elf_notes(note_buf, &regs);
767ebaeb5aeSMahesh Salgaonkar 			SKIP_TO_NEXT_CPU(reg_entry);
768ebaeb5aeSMahesh Salgaonkar 		} else {
769ebaeb5aeSMahesh Salgaonkar 			reg_entry++;
770ebaeb5aeSMahesh Salgaonkar 			reg_entry = fadump_read_registers(reg_entry, &regs);
771ebaeb5aeSMahesh Salgaonkar 			note_buf = fadump_regs_to_elf_notes(note_buf, &regs);
772ebaeb5aeSMahesh Salgaonkar 		}
773ebaeb5aeSMahesh Salgaonkar 	}
77422bd0177SHari Bathini 	final_note(note_buf);
775ebaeb5aeSMahesh Salgaonkar 
776b717d985SRickard Strandqvist 	if (fdh) {
777ebaeb5aeSMahesh Salgaonkar 		pr_debug("Updating elfcore header (%llx) with cpu notes\n",
778ebaeb5aeSMahesh Salgaonkar 							fdh->elfcorehdr_addr);
779ebaeb5aeSMahesh Salgaonkar 		fadump_update_elfcore_header((char *)__va(fdh->elfcorehdr_addr));
780b717d985SRickard Strandqvist 	}
781ebaeb5aeSMahesh Salgaonkar 	return 0;
782ebaeb5aeSMahesh Salgaonkar 
783ebaeb5aeSMahesh Salgaonkar error_out:
784ebaeb5aeSMahesh Salgaonkar 	fadump_cpu_notes_buf_free((unsigned long)__va(fw_dump.cpu_notes_buf),
785ebaeb5aeSMahesh Salgaonkar 					fw_dump.cpu_notes_buf_size);
786ebaeb5aeSMahesh Salgaonkar 	fw_dump.cpu_notes_buf = 0;
787ebaeb5aeSMahesh Salgaonkar 	fw_dump.cpu_notes_buf_size = 0;
788ebaeb5aeSMahesh Salgaonkar 	return rc;
789ebaeb5aeSMahesh Salgaonkar 
790ebaeb5aeSMahesh Salgaonkar }
791ebaeb5aeSMahesh Salgaonkar 
7922df173d9SMahesh Salgaonkar /*
7932df173d9SMahesh Salgaonkar  * Validate and process the dump data stored by firmware before exporting
7942df173d9SMahesh Salgaonkar  * it through '/proc/vmcore'.
7952df173d9SMahesh Salgaonkar  */
7962df173d9SMahesh Salgaonkar static int __init process_fadump(const struct fadump_mem_struct *fdm_active)
7972df173d9SMahesh Salgaonkar {
7982df173d9SMahesh Salgaonkar 	struct fadump_crash_info_header *fdh;
799ebaeb5aeSMahesh Salgaonkar 	int rc = 0;
8002df173d9SMahesh Salgaonkar 
8012df173d9SMahesh Salgaonkar 	if (!fdm_active || !fw_dump.fadumphdr_addr)
8022df173d9SMahesh Salgaonkar 		return -EINVAL;
8032df173d9SMahesh Salgaonkar 
8042df173d9SMahesh Salgaonkar 	/* Check if the dump data is valid. */
805408cddd9SHari Bathini 	if ((be16_to_cpu(fdm_active->header.dump_status_flag) == FADUMP_ERROR_FLAG) ||
806ebaeb5aeSMahesh Salgaonkar 			(fdm_active->cpu_state_data.error_flags != 0) ||
8072df173d9SMahesh Salgaonkar 			(fdm_active->rmr_region.error_flags != 0)) {
8082df173d9SMahesh Salgaonkar 		printk(KERN_ERR "Dump taken by platform is not valid\n");
8092df173d9SMahesh Salgaonkar 		return -EINVAL;
8102df173d9SMahesh Salgaonkar 	}
811ebaeb5aeSMahesh Salgaonkar 	if ((fdm_active->rmr_region.bytes_dumped !=
812ebaeb5aeSMahesh Salgaonkar 			fdm_active->rmr_region.source_len) ||
813ebaeb5aeSMahesh Salgaonkar 			!fdm_active->cpu_state_data.bytes_dumped) {
8142df173d9SMahesh Salgaonkar 		printk(KERN_ERR "Dump taken by platform is incomplete\n");
8152df173d9SMahesh Salgaonkar 		return -EINVAL;
8162df173d9SMahesh Salgaonkar 	}
8172df173d9SMahesh Salgaonkar 
8182df173d9SMahesh Salgaonkar 	/* Validate the fadump crash info header */
8192df173d9SMahesh Salgaonkar 	fdh = __va(fw_dump.fadumphdr_addr);
8202df173d9SMahesh Salgaonkar 	if (fdh->magic_number != FADUMP_CRASH_INFO_MAGIC) {
8212df173d9SMahesh Salgaonkar 		printk(KERN_ERR "Crash info header is not valid.\n");
8222df173d9SMahesh Salgaonkar 		return -EINVAL;
8232df173d9SMahesh Salgaonkar 	}
8242df173d9SMahesh Salgaonkar 
825ebaeb5aeSMahesh Salgaonkar 	rc = fadump_build_cpu_notes(fdm_active);
826ebaeb5aeSMahesh Salgaonkar 	if (rc)
827ebaeb5aeSMahesh Salgaonkar 		return rc;
828ebaeb5aeSMahesh Salgaonkar 
8292df173d9SMahesh Salgaonkar 	/*
8302df173d9SMahesh Salgaonkar 	 * We are done validating dump info and elfcore header is now ready
8312df173d9SMahesh Salgaonkar 	 * to be exported. set elfcorehdr_addr so that vmcore module will
8322df173d9SMahesh Salgaonkar 	 * export the elfcore header through '/proc/vmcore'.
8332df173d9SMahesh Salgaonkar 	 */
8342df173d9SMahesh Salgaonkar 	elfcorehdr_addr = fdh->elfcorehdr_addr;
8352df173d9SMahesh Salgaonkar 
8362df173d9SMahesh Salgaonkar 	return 0;
8372df173d9SMahesh Salgaonkar }
8382df173d9SMahesh Salgaonkar 
8392df173d9SMahesh Salgaonkar static inline void fadump_add_crash_memory(unsigned long long base,
8402df173d9SMahesh Salgaonkar 					unsigned long long end)
8412df173d9SMahesh Salgaonkar {
8422df173d9SMahesh Salgaonkar 	if (base == end)
8432df173d9SMahesh Salgaonkar 		return;
8442df173d9SMahesh Salgaonkar 
8452df173d9SMahesh Salgaonkar 	pr_debug("crash_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n",
8462df173d9SMahesh Salgaonkar 		crash_mem_ranges, base, end - 1, (end - base));
8472df173d9SMahesh Salgaonkar 	crash_memory_ranges[crash_mem_ranges].base = base;
8482df173d9SMahesh Salgaonkar 	crash_memory_ranges[crash_mem_ranges].size = end - base;
8492df173d9SMahesh Salgaonkar 	crash_mem_ranges++;
8502df173d9SMahesh Salgaonkar }
8512df173d9SMahesh Salgaonkar 
8522df173d9SMahesh Salgaonkar static void fadump_exclude_reserved_area(unsigned long long start,
8532df173d9SMahesh Salgaonkar 					unsigned long long end)
8542df173d9SMahesh Salgaonkar {
8552df173d9SMahesh Salgaonkar 	unsigned long long ra_start, ra_end;
8562df173d9SMahesh Salgaonkar 
8572df173d9SMahesh Salgaonkar 	ra_start = fw_dump.reserve_dump_area_start;
8582df173d9SMahesh Salgaonkar 	ra_end = ra_start + fw_dump.reserve_dump_area_size;
8592df173d9SMahesh Salgaonkar 
8602df173d9SMahesh Salgaonkar 	if ((ra_start < end) && (ra_end > start)) {
8612df173d9SMahesh Salgaonkar 		if ((start < ra_start) && (end > ra_end)) {
8622df173d9SMahesh Salgaonkar 			fadump_add_crash_memory(start, ra_start);
8632df173d9SMahesh Salgaonkar 			fadump_add_crash_memory(ra_end, end);
8642df173d9SMahesh Salgaonkar 		} else if (start < ra_start) {
8652df173d9SMahesh Salgaonkar 			fadump_add_crash_memory(start, ra_start);
8662df173d9SMahesh Salgaonkar 		} else if (ra_end < end) {
8672df173d9SMahesh Salgaonkar 			fadump_add_crash_memory(ra_end, end);
8682df173d9SMahesh Salgaonkar 		}
8692df173d9SMahesh Salgaonkar 	} else
8702df173d9SMahesh Salgaonkar 		fadump_add_crash_memory(start, end);
8712df173d9SMahesh Salgaonkar }
8722df173d9SMahesh Salgaonkar 
8732df173d9SMahesh Salgaonkar static int fadump_init_elfcore_header(char *bufp)
8742df173d9SMahesh Salgaonkar {
8752df173d9SMahesh Salgaonkar 	struct elfhdr *elf;
8762df173d9SMahesh Salgaonkar 
8772df173d9SMahesh Salgaonkar 	elf = (struct elfhdr *) bufp;
8782df173d9SMahesh Salgaonkar 	bufp += sizeof(struct elfhdr);
8792df173d9SMahesh Salgaonkar 	memcpy(elf->e_ident, ELFMAG, SELFMAG);
8802df173d9SMahesh Salgaonkar 	elf->e_ident[EI_CLASS] = ELF_CLASS;
8812df173d9SMahesh Salgaonkar 	elf->e_ident[EI_DATA] = ELF_DATA;
8822df173d9SMahesh Salgaonkar 	elf->e_ident[EI_VERSION] = EV_CURRENT;
8832df173d9SMahesh Salgaonkar 	elf->e_ident[EI_OSABI] = ELF_OSABI;
8842df173d9SMahesh Salgaonkar 	memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
8852df173d9SMahesh Salgaonkar 	elf->e_type = ET_CORE;
8862df173d9SMahesh Salgaonkar 	elf->e_machine = ELF_ARCH;
8872df173d9SMahesh Salgaonkar 	elf->e_version = EV_CURRENT;
8882df173d9SMahesh Salgaonkar 	elf->e_entry = 0;
8892df173d9SMahesh Salgaonkar 	elf->e_phoff = sizeof(struct elfhdr);
8902df173d9SMahesh Salgaonkar 	elf->e_shoff = 0;
891d8bced27SDaniel Axtens #if defined(_CALL_ELF)
892d8bced27SDaniel Axtens 	elf->e_flags = _CALL_ELF;
893d8bced27SDaniel Axtens #else
894d8bced27SDaniel Axtens 	elf->e_flags = 0;
895d8bced27SDaniel Axtens #endif
8962df173d9SMahesh Salgaonkar 	elf->e_ehsize = sizeof(struct elfhdr);
8972df173d9SMahesh Salgaonkar 	elf->e_phentsize = sizeof(struct elf_phdr);
8982df173d9SMahesh Salgaonkar 	elf->e_phnum = 0;
8992df173d9SMahesh Salgaonkar 	elf->e_shentsize = 0;
9002df173d9SMahesh Salgaonkar 	elf->e_shnum = 0;
9012df173d9SMahesh Salgaonkar 	elf->e_shstrndx = 0;
9022df173d9SMahesh Salgaonkar 
9032df173d9SMahesh Salgaonkar 	return 0;
9042df173d9SMahesh Salgaonkar }
9052df173d9SMahesh Salgaonkar 
9062df173d9SMahesh Salgaonkar /*
9072df173d9SMahesh Salgaonkar  * Traverse through memblock structure and setup crash memory ranges. These
9082df173d9SMahesh Salgaonkar  * ranges will be used create PT_LOAD program headers in elfcore header.
9092df173d9SMahesh Salgaonkar  */
9102df173d9SMahesh Salgaonkar static void fadump_setup_crash_memory_ranges(void)
9112df173d9SMahesh Salgaonkar {
9122df173d9SMahesh Salgaonkar 	struct memblock_region *reg;
9132df173d9SMahesh Salgaonkar 	unsigned long long start, end;
9142df173d9SMahesh Salgaonkar 
9152df173d9SMahesh Salgaonkar 	pr_debug("Setup crash memory ranges.\n");
9162df173d9SMahesh Salgaonkar 	crash_mem_ranges = 0;
9172df173d9SMahesh Salgaonkar 	/*
9182df173d9SMahesh Salgaonkar 	 * add the first memory chunk (RMA_START through boot_memory_size) as
9192df173d9SMahesh Salgaonkar 	 * a separate memory chunk. The reason is, at the time crash firmware
9202df173d9SMahesh Salgaonkar 	 * will move the content of this memory chunk to different location
9212df173d9SMahesh Salgaonkar 	 * specified during fadump registration. We need to create a separate
9222df173d9SMahesh Salgaonkar 	 * program header for this chunk with the correct offset.
9232df173d9SMahesh Salgaonkar 	 */
9242df173d9SMahesh Salgaonkar 	fadump_add_crash_memory(RMA_START, fw_dump.boot_memory_size);
9252df173d9SMahesh Salgaonkar 
9262df173d9SMahesh Salgaonkar 	for_each_memblock(memory, reg) {
9272df173d9SMahesh Salgaonkar 		start = (unsigned long long)reg->base;
9282df173d9SMahesh Salgaonkar 		end = start + (unsigned long long)reg->size;
929a77af552SHari Bathini 
930a77af552SHari Bathini 		/*
931a77af552SHari Bathini 		 * skip the first memory chunk that is already added (RMA_START
932a77af552SHari Bathini 		 * through boot_memory_size). This logic needs a relook if and
933a77af552SHari Bathini 		 * when RMA_START changes to a non-zero value.
934a77af552SHari Bathini 		 */
935a77af552SHari Bathini 		BUILD_BUG_ON(RMA_START != 0);
936a77af552SHari Bathini 		if (start < fw_dump.boot_memory_size) {
937a77af552SHari Bathini 			if (end > fw_dump.boot_memory_size)
9382df173d9SMahesh Salgaonkar 				start = fw_dump.boot_memory_size;
939a77af552SHari Bathini 			else
940a77af552SHari Bathini 				continue;
941a77af552SHari Bathini 		}
9422df173d9SMahesh Salgaonkar 
9432df173d9SMahesh Salgaonkar 		/* add this range excluding the reserved dump area. */
9442df173d9SMahesh Salgaonkar 		fadump_exclude_reserved_area(start, end);
9452df173d9SMahesh Salgaonkar 	}
9462df173d9SMahesh Salgaonkar }
9472df173d9SMahesh Salgaonkar 
948d34c5f26SMahesh Salgaonkar /*
949d34c5f26SMahesh Salgaonkar  * If the given physical address falls within the boot memory region then
950d34c5f26SMahesh Salgaonkar  * return the relocated address that points to the dump region reserved
951d34c5f26SMahesh Salgaonkar  * for saving initial boot memory contents.
952d34c5f26SMahesh Salgaonkar  */
953d34c5f26SMahesh Salgaonkar static inline unsigned long fadump_relocate(unsigned long paddr)
954d34c5f26SMahesh Salgaonkar {
955d34c5f26SMahesh Salgaonkar 	if (paddr > RMA_START && paddr < fw_dump.boot_memory_size)
956408cddd9SHari Bathini 		return be64_to_cpu(fdm.rmr_region.destination_address) + paddr;
957d34c5f26SMahesh Salgaonkar 	else
958d34c5f26SMahesh Salgaonkar 		return paddr;
959d34c5f26SMahesh Salgaonkar }
960d34c5f26SMahesh Salgaonkar 
9612df173d9SMahesh Salgaonkar static int fadump_create_elfcore_headers(char *bufp)
9622df173d9SMahesh Salgaonkar {
9632df173d9SMahesh Salgaonkar 	struct elfhdr *elf;
9642df173d9SMahesh Salgaonkar 	struct elf_phdr *phdr;
9652df173d9SMahesh Salgaonkar 	int i;
9662df173d9SMahesh Salgaonkar 
9672df173d9SMahesh Salgaonkar 	fadump_init_elfcore_header(bufp);
9682df173d9SMahesh Salgaonkar 	elf = (struct elfhdr *)bufp;
9692df173d9SMahesh Salgaonkar 	bufp += sizeof(struct elfhdr);
9702df173d9SMahesh Salgaonkar 
971ebaeb5aeSMahesh Salgaonkar 	/*
972ebaeb5aeSMahesh Salgaonkar 	 * setup ELF PT_NOTE, place holder for cpu notes info. The notes info
973ebaeb5aeSMahesh Salgaonkar 	 * will be populated during second kernel boot after crash. Hence
974ebaeb5aeSMahesh Salgaonkar 	 * this PT_NOTE will always be the first elf note.
975ebaeb5aeSMahesh Salgaonkar 	 *
976ebaeb5aeSMahesh Salgaonkar 	 * NOTE: Any new ELF note addition should be placed after this note.
977ebaeb5aeSMahesh Salgaonkar 	 */
978ebaeb5aeSMahesh Salgaonkar 	phdr = (struct elf_phdr *)bufp;
979ebaeb5aeSMahesh Salgaonkar 	bufp += sizeof(struct elf_phdr);
980ebaeb5aeSMahesh Salgaonkar 	phdr->p_type = PT_NOTE;
981ebaeb5aeSMahesh Salgaonkar 	phdr->p_flags = 0;
982ebaeb5aeSMahesh Salgaonkar 	phdr->p_vaddr = 0;
983ebaeb5aeSMahesh Salgaonkar 	phdr->p_align = 0;
984ebaeb5aeSMahesh Salgaonkar 
985ebaeb5aeSMahesh Salgaonkar 	phdr->p_offset = 0;
986ebaeb5aeSMahesh Salgaonkar 	phdr->p_paddr = 0;
987ebaeb5aeSMahesh Salgaonkar 	phdr->p_filesz = 0;
988ebaeb5aeSMahesh Salgaonkar 	phdr->p_memsz = 0;
989ebaeb5aeSMahesh Salgaonkar 
990ebaeb5aeSMahesh Salgaonkar 	(elf->e_phnum)++;
991ebaeb5aeSMahesh Salgaonkar 
992d34c5f26SMahesh Salgaonkar 	/* setup ELF PT_NOTE for vmcoreinfo */
993d34c5f26SMahesh Salgaonkar 	phdr = (struct elf_phdr *)bufp;
994d34c5f26SMahesh Salgaonkar 	bufp += sizeof(struct elf_phdr);
995d34c5f26SMahesh Salgaonkar 	phdr->p_type	= PT_NOTE;
996d34c5f26SMahesh Salgaonkar 	phdr->p_flags	= 0;
997d34c5f26SMahesh Salgaonkar 	phdr->p_vaddr	= 0;
998d34c5f26SMahesh Salgaonkar 	phdr->p_align	= 0;
999d34c5f26SMahesh Salgaonkar 
1000d34c5f26SMahesh Salgaonkar 	phdr->p_paddr	= fadump_relocate(paddr_vmcoreinfo_note());
1001d34c5f26SMahesh Salgaonkar 	phdr->p_offset	= phdr->p_paddr;
1002*5203f499SXunlei Pang 	phdr->p_memsz	= phdr->p_filesz = VMCOREINFO_NOTE_SIZE;
1003d34c5f26SMahesh Salgaonkar 
1004d34c5f26SMahesh Salgaonkar 	/* Increment number of program headers. */
1005d34c5f26SMahesh Salgaonkar 	(elf->e_phnum)++;
1006d34c5f26SMahesh Salgaonkar 
10072df173d9SMahesh Salgaonkar 	/* setup PT_LOAD sections. */
10082df173d9SMahesh Salgaonkar 
10092df173d9SMahesh Salgaonkar 	for (i = 0; i < crash_mem_ranges; i++) {
10102df173d9SMahesh Salgaonkar 		unsigned long long mbase, msize;
10112df173d9SMahesh Salgaonkar 		mbase = crash_memory_ranges[i].base;
10122df173d9SMahesh Salgaonkar 		msize = crash_memory_ranges[i].size;
10132df173d9SMahesh Salgaonkar 
10142df173d9SMahesh Salgaonkar 		if (!msize)
10152df173d9SMahesh Salgaonkar 			continue;
10162df173d9SMahesh Salgaonkar 
10172df173d9SMahesh Salgaonkar 		phdr = (struct elf_phdr *)bufp;
10182df173d9SMahesh Salgaonkar 		bufp += sizeof(struct elf_phdr);
10192df173d9SMahesh Salgaonkar 		phdr->p_type	= PT_LOAD;
10202df173d9SMahesh Salgaonkar 		phdr->p_flags	= PF_R|PF_W|PF_X;
10212df173d9SMahesh Salgaonkar 		phdr->p_offset	= mbase;
10222df173d9SMahesh Salgaonkar 
10232df173d9SMahesh Salgaonkar 		if (mbase == RMA_START) {
10242df173d9SMahesh Salgaonkar 			/*
10252df173d9SMahesh Salgaonkar 			 * The entire RMA region will be moved by firmware
10262df173d9SMahesh Salgaonkar 			 * to the specified destination_address. Hence set
10272df173d9SMahesh Salgaonkar 			 * the correct offset.
10282df173d9SMahesh Salgaonkar 			 */
1029408cddd9SHari Bathini 			phdr->p_offset = be64_to_cpu(fdm.rmr_region.destination_address);
10302df173d9SMahesh Salgaonkar 		}
10312df173d9SMahesh Salgaonkar 
10322df173d9SMahesh Salgaonkar 		phdr->p_paddr = mbase;
10332df173d9SMahesh Salgaonkar 		phdr->p_vaddr = (unsigned long)__va(mbase);
10342df173d9SMahesh Salgaonkar 		phdr->p_filesz = msize;
10352df173d9SMahesh Salgaonkar 		phdr->p_memsz = msize;
10362df173d9SMahesh Salgaonkar 		phdr->p_align = 0;
10372df173d9SMahesh Salgaonkar 
10382df173d9SMahesh Salgaonkar 		/* Increment number of program headers. */
10392df173d9SMahesh Salgaonkar 		(elf->e_phnum)++;
10402df173d9SMahesh Salgaonkar 	}
10412df173d9SMahesh Salgaonkar 	return 0;
10422df173d9SMahesh Salgaonkar }
10432df173d9SMahesh Salgaonkar 
10442df173d9SMahesh Salgaonkar static unsigned long init_fadump_header(unsigned long addr)
10452df173d9SMahesh Salgaonkar {
10462df173d9SMahesh Salgaonkar 	struct fadump_crash_info_header *fdh;
10472df173d9SMahesh Salgaonkar 
10482df173d9SMahesh Salgaonkar 	if (!addr)
10492df173d9SMahesh Salgaonkar 		return 0;
10502df173d9SMahesh Salgaonkar 
10512df173d9SMahesh Salgaonkar 	fw_dump.fadumphdr_addr = addr;
10522df173d9SMahesh Salgaonkar 	fdh = __va(addr);
10532df173d9SMahesh Salgaonkar 	addr += sizeof(struct fadump_crash_info_header);
10542df173d9SMahesh Salgaonkar 
10552df173d9SMahesh Salgaonkar 	memset(fdh, 0, sizeof(struct fadump_crash_info_header));
10562df173d9SMahesh Salgaonkar 	fdh->magic_number = FADUMP_CRASH_INFO_MAGIC;
10572df173d9SMahesh Salgaonkar 	fdh->elfcorehdr_addr = addr;
1058ebaeb5aeSMahesh Salgaonkar 	/* We will set the crashing cpu id in crash_fadump() during crash. */
1059ebaeb5aeSMahesh Salgaonkar 	fdh->crashing_cpu = CPU_UNKNOWN;
10602df173d9SMahesh Salgaonkar 
10612df173d9SMahesh Salgaonkar 	return addr;
10622df173d9SMahesh Salgaonkar }
10632df173d9SMahesh Salgaonkar 
106498b8cd7fSMichal Suchanek static int register_fadump(void)
10653ccc00a7SMahesh Salgaonkar {
10662df173d9SMahesh Salgaonkar 	unsigned long addr;
10672df173d9SMahesh Salgaonkar 	void *vaddr;
10682df173d9SMahesh Salgaonkar 
10693ccc00a7SMahesh Salgaonkar 	/*
10703ccc00a7SMahesh Salgaonkar 	 * If no memory is reserved then we can not register for firmware-
10713ccc00a7SMahesh Salgaonkar 	 * assisted dump.
10723ccc00a7SMahesh Salgaonkar 	 */
10733ccc00a7SMahesh Salgaonkar 	if (!fw_dump.reserve_dump_area_size)
107498b8cd7fSMichal Suchanek 		return -ENODEV;
10753ccc00a7SMahesh Salgaonkar 
10762df173d9SMahesh Salgaonkar 	fadump_setup_crash_memory_ranges();
10772df173d9SMahesh Salgaonkar 
1078408cddd9SHari Bathini 	addr = be64_to_cpu(fdm.rmr_region.destination_address) + be64_to_cpu(fdm.rmr_region.source_len);
10792df173d9SMahesh Salgaonkar 	/* Initialize fadump crash info header. */
10802df173d9SMahesh Salgaonkar 	addr = init_fadump_header(addr);
10812df173d9SMahesh Salgaonkar 	vaddr = __va(addr);
10822df173d9SMahesh Salgaonkar 
10832df173d9SMahesh Salgaonkar 	pr_debug("Creating ELF core headers at %#016lx\n", addr);
10842df173d9SMahesh Salgaonkar 	fadump_create_elfcore_headers(vaddr);
10852df173d9SMahesh Salgaonkar 
10863ccc00a7SMahesh Salgaonkar 	/* register the future kernel dump with firmware. */
108798b8cd7fSMichal Suchanek 	return register_fw_dump(&fdm);
10883ccc00a7SMahesh Salgaonkar }
10893ccc00a7SMahesh Salgaonkar 
10903ccc00a7SMahesh Salgaonkar static int fadump_unregister_dump(struct fadump_mem_struct *fdm)
10913ccc00a7SMahesh Salgaonkar {
10923ccc00a7SMahesh Salgaonkar 	int rc = 0;
10933ccc00a7SMahesh Salgaonkar 	unsigned int wait_time;
10943ccc00a7SMahesh Salgaonkar 
10953ccc00a7SMahesh Salgaonkar 	pr_debug("Un-register firmware-assisted dump\n");
10963ccc00a7SMahesh Salgaonkar 
10973ccc00a7SMahesh Salgaonkar 	/* TODO: Add upper time limit for the delay */
10983ccc00a7SMahesh Salgaonkar 	do {
10993ccc00a7SMahesh Salgaonkar 		rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
11003ccc00a7SMahesh Salgaonkar 			FADUMP_UNREGISTER, fdm,
11013ccc00a7SMahesh Salgaonkar 			sizeof(struct fadump_mem_struct));
11023ccc00a7SMahesh Salgaonkar 
11033ccc00a7SMahesh Salgaonkar 		wait_time = rtas_busy_delay_time(rc);
11043ccc00a7SMahesh Salgaonkar 		if (wait_time)
11053ccc00a7SMahesh Salgaonkar 			mdelay(wait_time);
11063ccc00a7SMahesh Salgaonkar 	} while (wait_time);
11073ccc00a7SMahesh Salgaonkar 
11083ccc00a7SMahesh Salgaonkar 	if (rc) {
11093ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "Failed to un-register firmware-assisted dump."
11103ccc00a7SMahesh Salgaonkar 			" unexpected error(%d).\n", rc);
11113ccc00a7SMahesh Salgaonkar 		return rc;
11123ccc00a7SMahesh Salgaonkar 	}
11133ccc00a7SMahesh Salgaonkar 	fw_dump.dump_registered = 0;
11143ccc00a7SMahesh Salgaonkar 	return 0;
11153ccc00a7SMahesh Salgaonkar }
11163ccc00a7SMahesh Salgaonkar 
1117b500afffSMahesh Salgaonkar static int fadump_invalidate_dump(struct fadump_mem_struct *fdm)
1118b500afffSMahesh Salgaonkar {
1119b500afffSMahesh Salgaonkar 	int rc = 0;
1120b500afffSMahesh Salgaonkar 	unsigned int wait_time;
1121b500afffSMahesh Salgaonkar 
1122b500afffSMahesh Salgaonkar 	pr_debug("Invalidating firmware-assisted dump registration\n");
1123b500afffSMahesh Salgaonkar 
1124b500afffSMahesh Salgaonkar 	/* TODO: Add upper time limit for the delay */
1125b500afffSMahesh Salgaonkar 	do {
1126b500afffSMahesh Salgaonkar 		rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
1127b500afffSMahesh Salgaonkar 			FADUMP_INVALIDATE, fdm,
1128b500afffSMahesh Salgaonkar 			sizeof(struct fadump_mem_struct));
1129b500afffSMahesh Salgaonkar 
1130b500afffSMahesh Salgaonkar 		wait_time = rtas_busy_delay_time(rc);
1131b500afffSMahesh Salgaonkar 		if (wait_time)
1132b500afffSMahesh Salgaonkar 			mdelay(wait_time);
1133b500afffSMahesh Salgaonkar 	} while (wait_time);
1134b500afffSMahesh Salgaonkar 
1135b500afffSMahesh Salgaonkar 	if (rc) {
11364a03749fSColin Ian King 		pr_err("Failed to invalidate firmware-assisted dump registration. Unexpected error (%d).\n", rc);
1137b5b1cfc5SMichael Ellerman 		return rc;
1138b500afffSMahesh Salgaonkar 	}
1139b500afffSMahesh Salgaonkar 	fw_dump.dump_active = 0;
1140b500afffSMahesh Salgaonkar 	fdm_active = NULL;
1141b500afffSMahesh Salgaonkar 	return 0;
1142b500afffSMahesh Salgaonkar }
1143b500afffSMahesh Salgaonkar 
1144b500afffSMahesh Salgaonkar void fadump_cleanup(void)
1145b500afffSMahesh Salgaonkar {
1146b500afffSMahesh Salgaonkar 	/* Invalidate the registration only if dump is active. */
1147b500afffSMahesh Salgaonkar 	if (fw_dump.dump_active) {
1148b500afffSMahesh Salgaonkar 		init_fadump_mem_struct(&fdm,
1149408cddd9SHari Bathini 			be64_to_cpu(fdm_active->cpu_state_data.destination_address));
1150b500afffSMahesh Salgaonkar 		fadump_invalidate_dump(&fdm);
1151b500afffSMahesh Salgaonkar 	}
1152b500afffSMahesh Salgaonkar }
1153b500afffSMahesh Salgaonkar 
115468fa6478SHari Bathini static void fadump_free_reserved_memory(unsigned long start_pfn,
115568fa6478SHari Bathini 					unsigned long end_pfn)
115668fa6478SHari Bathini {
115768fa6478SHari Bathini 	unsigned long pfn;
115868fa6478SHari Bathini 	unsigned long time_limit = jiffies + HZ;
115968fa6478SHari Bathini 
116068fa6478SHari Bathini 	pr_info("freeing reserved memory (0x%llx - 0x%llx)\n",
116168fa6478SHari Bathini 		PFN_PHYS(start_pfn), PFN_PHYS(end_pfn));
116268fa6478SHari Bathini 
116368fa6478SHari Bathini 	for (pfn = start_pfn; pfn < end_pfn; pfn++) {
116468fa6478SHari Bathini 		free_reserved_page(pfn_to_page(pfn));
116568fa6478SHari Bathini 
116668fa6478SHari Bathini 		if (time_after(jiffies, time_limit)) {
116768fa6478SHari Bathini 			cond_resched();
116868fa6478SHari Bathini 			time_limit = jiffies + HZ;
116968fa6478SHari Bathini 		}
117068fa6478SHari Bathini 	}
117168fa6478SHari Bathini }
117268fa6478SHari Bathini 
117368fa6478SHari Bathini /*
117468fa6478SHari Bathini  * Skip memory holes and free memory that was actually reserved.
117568fa6478SHari Bathini  */
117668fa6478SHari Bathini static void fadump_release_reserved_area(unsigned long start, unsigned long end)
117768fa6478SHari Bathini {
117868fa6478SHari Bathini 	struct memblock_region *reg;
117968fa6478SHari Bathini 	unsigned long tstart, tend;
118068fa6478SHari Bathini 	unsigned long start_pfn = PHYS_PFN(start);
118168fa6478SHari Bathini 	unsigned long end_pfn = PHYS_PFN(end);
118268fa6478SHari Bathini 
118368fa6478SHari Bathini 	for_each_memblock(memory, reg) {
118468fa6478SHari Bathini 		tstart = max(start_pfn, memblock_region_memory_base_pfn(reg));
118568fa6478SHari Bathini 		tend = min(end_pfn, memblock_region_memory_end_pfn(reg));
118668fa6478SHari Bathini 		if (tstart < tend) {
118768fa6478SHari Bathini 			fadump_free_reserved_memory(tstart, tend);
118868fa6478SHari Bathini 
118968fa6478SHari Bathini 			if (tend == end_pfn)
119068fa6478SHari Bathini 				break;
119168fa6478SHari Bathini 
119268fa6478SHari Bathini 			start_pfn = tend + 1;
119368fa6478SHari Bathini 		}
119468fa6478SHari Bathini 	}
119568fa6478SHari Bathini }
119668fa6478SHari Bathini 
1197b500afffSMahesh Salgaonkar /*
1198b500afffSMahesh Salgaonkar  * Release the memory that was reserved in early boot to preserve the memory
1199b500afffSMahesh Salgaonkar  * contents. The released memory will be available for general use.
1200b500afffSMahesh Salgaonkar  */
1201b500afffSMahesh Salgaonkar static void fadump_release_memory(unsigned long begin, unsigned long end)
1202b500afffSMahesh Salgaonkar {
1203b500afffSMahesh Salgaonkar 	unsigned long ra_start, ra_end;
1204b500afffSMahesh Salgaonkar 
1205b500afffSMahesh Salgaonkar 	ra_start = fw_dump.reserve_dump_area_start;
1206b500afffSMahesh Salgaonkar 	ra_end = ra_start + fw_dump.reserve_dump_area_size;
1207b500afffSMahesh Salgaonkar 
1208b500afffSMahesh Salgaonkar 	/*
1209b500afffSMahesh Salgaonkar 	 * exclude the dump reserve area. Will reuse it for next
1210b500afffSMahesh Salgaonkar 	 * fadump registration.
1211b500afffSMahesh Salgaonkar 	 */
121268fa6478SHari Bathini 	if (begin < ra_end && end > ra_start) {
121368fa6478SHari Bathini 		if (begin < ra_start)
121468fa6478SHari Bathini 			fadump_release_reserved_area(begin, ra_start);
121568fa6478SHari Bathini 		if (end > ra_end)
121668fa6478SHari Bathini 			fadump_release_reserved_area(ra_end, end);
121768fa6478SHari Bathini 	} else
121868fa6478SHari Bathini 		fadump_release_reserved_area(begin, end);
1219b500afffSMahesh Salgaonkar }
1220b500afffSMahesh Salgaonkar 
1221b500afffSMahesh Salgaonkar static void fadump_invalidate_release_mem(void)
1222b500afffSMahesh Salgaonkar {
1223b500afffSMahesh Salgaonkar 	unsigned long reserved_area_start, reserved_area_end;
1224b500afffSMahesh Salgaonkar 	unsigned long destination_address;
1225b500afffSMahesh Salgaonkar 
1226b500afffSMahesh Salgaonkar 	mutex_lock(&fadump_mutex);
1227b500afffSMahesh Salgaonkar 	if (!fw_dump.dump_active) {
1228b500afffSMahesh Salgaonkar 		mutex_unlock(&fadump_mutex);
1229b500afffSMahesh Salgaonkar 		return;
1230b500afffSMahesh Salgaonkar 	}
1231b500afffSMahesh Salgaonkar 
1232408cddd9SHari Bathini 	destination_address = be64_to_cpu(fdm_active->cpu_state_data.destination_address);
1233b500afffSMahesh Salgaonkar 	fadump_cleanup();
1234b500afffSMahesh Salgaonkar 	mutex_unlock(&fadump_mutex);
1235b500afffSMahesh Salgaonkar 
1236b500afffSMahesh Salgaonkar 	/*
1237b500afffSMahesh Salgaonkar 	 * Save the current reserved memory bounds we will require them
1238b500afffSMahesh Salgaonkar 	 * later for releasing the memory for general use.
1239b500afffSMahesh Salgaonkar 	 */
1240b500afffSMahesh Salgaonkar 	reserved_area_start = fw_dump.reserve_dump_area_start;
1241b500afffSMahesh Salgaonkar 	reserved_area_end = reserved_area_start +
1242b500afffSMahesh Salgaonkar 			fw_dump.reserve_dump_area_size;
1243b500afffSMahesh Salgaonkar 	/*
1244b500afffSMahesh Salgaonkar 	 * Setup reserve_dump_area_start and its size so that we can
1245b500afffSMahesh Salgaonkar 	 * reuse this reserved memory for Re-registration.
1246b500afffSMahesh Salgaonkar 	 */
1247b500afffSMahesh Salgaonkar 	fw_dump.reserve_dump_area_start = destination_address;
1248b500afffSMahesh Salgaonkar 	fw_dump.reserve_dump_area_size = get_fadump_area_size();
1249b500afffSMahesh Salgaonkar 
1250b500afffSMahesh Salgaonkar 	fadump_release_memory(reserved_area_start, reserved_area_end);
1251b500afffSMahesh Salgaonkar 	if (fw_dump.cpu_notes_buf) {
1252b500afffSMahesh Salgaonkar 		fadump_cpu_notes_buf_free(
1253b500afffSMahesh Salgaonkar 				(unsigned long)__va(fw_dump.cpu_notes_buf),
1254b500afffSMahesh Salgaonkar 				fw_dump.cpu_notes_buf_size);
1255b500afffSMahesh Salgaonkar 		fw_dump.cpu_notes_buf = 0;
1256b500afffSMahesh Salgaonkar 		fw_dump.cpu_notes_buf_size = 0;
1257b500afffSMahesh Salgaonkar 	}
1258b500afffSMahesh Salgaonkar 	/* Initialize the kernel dump memory structure for FAD registration. */
1259b500afffSMahesh Salgaonkar 	init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
1260b500afffSMahesh Salgaonkar }
1261b500afffSMahesh Salgaonkar 
1262b500afffSMahesh Salgaonkar static ssize_t fadump_release_memory_store(struct kobject *kobj,
1263b500afffSMahesh Salgaonkar 					struct kobj_attribute *attr,
1264b500afffSMahesh Salgaonkar 					const char *buf, size_t count)
1265b500afffSMahesh Salgaonkar {
1266b500afffSMahesh Salgaonkar 	if (!fw_dump.dump_active)
1267b500afffSMahesh Salgaonkar 		return -EPERM;
1268b500afffSMahesh Salgaonkar 
1269b500afffSMahesh Salgaonkar 	if (buf[0] == '1') {
1270b500afffSMahesh Salgaonkar 		/*
1271b500afffSMahesh Salgaonkar 		 * Take away the '/proc/vmcore'. We are releasing the dump
1272b500afffSMahesh Salgaonkar 		 * memory, hence it will not be valid anymore.
1273b500afffSMahesh Salgaonkar 		 */
12742685f826SMichael Ellerman #ifdef CONFIG_PROC_VMCORE
1275b500afffSMahesh Salgaonkar 		vmcore_cleanup();
12762685f826SMichael Ellerman #endif
1277b500afffSMahesh Salgaonkar 		fadump_invalidate_release_mem();
1278b500afffSMahesh Salgaonkar 
1279b500afffSMahesh Salgaonkar 	} else
1280b500afffSMahesh Salgaonkar 		return -EINVAL;
1281b500afffSMahesh Salgaonkar 	return count;
1282b500afffSMahesh Salgaonkar }
1283b500afffSMahesh Salgaonkar 
12843ccc00a7SMahesh Salgaonkar static ssize_t fadump_enabled_show(struct kobject *kobj,
12853ccc00a7SMahesh Salgaonkar 					struct kobj_attribute *attr,
12863ccc00a7SMahesh Salgaonkar 					char *buf)
12873ccc00a7SMahesh Salgaonkar {
12883ccc00a7SMahesh Salgaonkar 	return sprintf(buf, "%d\n", fw_dump.fadump_enabled);
12893ccc00a7SMahesh Salgaonkar }
12903ccc00a7SMahesh Salgaonkar 
12913ccc00a7SMahesh Salgaonkar static ssize_t fadump_register_show(struct kobject *kobj,
12923ccc00a7SMahesh Salgaonkar 					struct kobj_attribute *attr,
12933ccc00a7SMahesh Salgaonkar 					char *buf)
12943ccc00a7SMahesh Salgaonkar {
12953ccc00a7SMahesh Salgaonkar 	return sprintf(buf, "%d\n", fw_dump.dump_registered);
12963ccc00a7SMahesh Salgaonkar }
12973ccc00a7SMahesh Salgaonkar 
12983ccc00a7SMahesh Salgaonkar static ssize_t fadump_register_store(struct kobject *kobj,
12993ccc00a7SMahesh Salgaonkar 					struct kobj_attribute *attr,
13003ccc00a7SMahesh Salgaonkar 					const char *buf, size_t count)
13013ccc00a7SMahesh Salgaonkar {
13023ccc00a7SMahesh Salgaonkar 	int ret = 0;
13033ccc00a7SMahesh Salgaonkar 
13043ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_enabled || fdm_active)
13053ccc00a7SMahesh Salgaonkar 		return -EPERM;
13063ccc00a7SMahesh Salgaonkar 
13073ccc00a7SMahesh Salgaonkar 	mutex_lock(&fadump_mutex);
13083ccc00a7SMahesh Salgaonkar 
13093ccc00a7SMahesh Salgaonkar 	switch (buf[0]) {
13103ccc00a7SMahesh Salgaonkar 	case '0':
13113ccc00a7SMahesh Salgaonkar 		if (fw_dump.dump_registered == 0) {
13123ccc00a7SMahesh Salgaonkar 			goto unlock_out;
13133ccc00a7SMahesh Salgaonkar 		}
13143ccc00a7SMahesh Salgaonkar 		/* Un-register Firmware-assisted dump */
13153ccc00a7SMahesh Salgaonkar 		fadump_unregister_dump(&fdm);
13163ccc00a7SMahesh Salgaonkar 		break;
13173ccc00a7SMahesh Salgaonkar 	case '1':
13183ccc00a7SMahesh Salgaonkar 		if (fw_dump.dump_registered == 1) {
131998b8cd7fSMichal Suchanek 			ret = -EEXIST;
13203ccc00a7SMahesh Salgaonkar 			goto unlock_out;
13213ccc00a7SMahesh Salgaonkar 		}
13223ccc00a7SMahesh Salgaonkar 		/* Register Firmware-assisted dump */
132398b8cd7fSMichal Suchanek 		ret = register_fadump();
13243ccc00a7SMahesh Salgaonkar 		break;
13253ccc00a7SMahesh Salgaonkar 	default:
13263ccc00a7SMahesh Salgaonkar 		ret = -EINVAL;
13273ccc00a7SMahesh Salgaonkar 		break;
13283ccc00a7SMahesh Salgaonkar 	}
13293ccc00a7SMahesh Salgaonkar 
13303ccc00a7SMahesh Salgaonkar unlock_out:
13313ccc00a7SMahesh Salgaonkar 	mutex_unlock(&fadump_mutex);
13323ccc00a7SMahesh Salgaonkar 	return ret < 0 ? ret : count;
13333ccc00a7SMahesh Salgaonkar }
13343ccc00a7SMahesh Salgaonkar 
13353ccc00a7SMahesh Salgaonkar static int fadump_region_show(struct seq_file *m, void *private)
13363ccc00a7SMahesh Salgaonkar {
13373ccc00a7SMahesh Salgaonkar 	const struct fadump_mem_struct *fdm_ptr;
13383ccc00a7SMahesh Salgaonkar 
13393ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_enabled)
13403ccc00a7SMahesh Salgaonkar 		return 0;
13413ccc00a7SMahesh Salgaonkar 
1342b500afffSMahesh Salgaonkar 	mutex_lock(&fadump_mutex);
13433ccc00a7SMahesh Salgaonkar 	if (fdm_active)
13443ccc00a7SMahesh Salgaonkar 		fdm_ptr = fdm_active;
1345b500afffSMahesh Salgaonkar 	else {
1346b500afffSMahesh Salgaonkar 		mutex_unlock(&fadump_mutex);
13473ccc00a7SMahesh Salgaonkar 		fdm_ptr = &fdm;
1348b500afffSMahesh Salgaonkar 	}
13493ccc00a7SMahesh Salgaonkar 
13503ccc00a7SMahesh Salgaonkar 	seq_printf(m,
13513ccc00a7SMahesh Salgaonkar 			"CPU : [%#016llx-%#016llx] %#llx bytes, "
13523ccc00a7SMahesh Salgaonkar 			"Dumped: %#llx\n",
1353408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->cpu_state_data.destination_address),
1354408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) +
1355408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->cpu_state_data.source_len) - 1,
1356408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->cpu_state_data.source_len),
1357408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->cpu_state_data.bytes_dumped));
13583ccc00a7SMahesh Salgaonkar 	seq_printf(m,
13593ccc00a7SMahesh Salgaonkar 			"HPTE: [%#016llx-%#016llx] %#llx bytes, "
13603ccc00a7SMahesh Salgaonkar 			"Dumped: %#llx\n",
1361408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->hpte_region.destination_address),
1362408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->hpte_region.destination_address) +
1363408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->hpte_region.source_len) - 1,
1364408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->hpte_region.source_len),
1365408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->hpte_region.bytes_dumped));
13663ccc00a7SMahesh Salgaonkar 	seq_printf(m,
13673ccc00a7SMahesh Salgaonkar 			"DUMP: [%#016llx-%#016llx] %#llx bytes, "
13683ccc00a7SMahesh Salgaonkar 			"Dumped: %#llx\n",
1369408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->rmr_region.destination_address),
1370408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->rmr_region.destination_address) +
1371408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->rmr_region.source_len) - 1,
1372408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->rmr_region.source_len),
1373408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->rmr_region.bytes_dumped));
13743ccc00a7SMahesh Salgaonkar 
13753ccc00a7SMahesh Salgaonkar 	if (!fdm_active ||
13763ccc00a7SMahesh Salgaonkar 		(fw_dump.reserve_dump_area_start ==
1377408cddd9SHari Bathini 		be64_to_cpu(fdm_ptr->cpu_state_data.destination_address)))
1378b500afffSMahesh Salgaonkar 		goto out;
13793ccc00a7SMahesh Salgaonkar 
13803ccc00a7SMahesh Salgaonkar 	/* Dump is active. Show reserved memory region. */
13813ccc00a7SMahesh Salgaonkar 	seq_printf(m,
13823ccc00a7SMahesh Salgaonkar 			"    : [%#016llx-%#016llx] %#llx bytes, "
13833ccc00a7SMahesh Salgaonkar 			"Dumped: %#llx\n",
13843ccc00a7SMahesh Salgaonkar 			(unsigned long long)fw_dump.reserve_dump_area_start,
1385408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) - 1,
1386408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) -
13873ccc00a7SMahesh Salgaonkar 			fw_dump.reserve_dump_area_start,
1388408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) -
13893ccc00a7SMahesh Salgaonkar 			fw_dump.reserve_dump_area_start);
1390b500afffSMahesh Salgaonkar out:
1391b500afffSMahesh Salgaonkar 	if (fdm_active)
1392b500afffSMahesh Salgaonkar 		mutex_unlock(&fadump_mutex);
13933ccc00a7SMahesh Salgaonkar 	return 0;
13943ccc00a7SMahesh Salgaonkar }
13953ccc00a7SMahesh Salgaonkar 
1396b500afffSMahesh Salgaonkar static struct kobj_attribute fadump_release_attr = __ATTR(fadump_release_mem,
1397b500afffSMahesh Salgaonkar 						0200, NULL,
1398b500afffSMahesh Salgaonkar 						fadump_release_memory_store);
13993ccc00a7SMahesh Salgaonkar static struct kobj_attribute fadump_attr = __ATTR(fadump_enabled,
14003ccc00a7SMahesh Salgaonkar 						0444, fadump_enabled_show,
14013ccc00a7SMahesh Salgaonkar 						NULL);
14023ccc00a7SMahesh Salgaonkar static struct kobj_attribute fadump_register_attr = __ATTR(fadump_registered,
14033ccc00a7SMahesh Salgaonkar 						0644, fadump_register_show,
14043ccc00a7SMahesh Salgaonkar 						fadump_register_store);
14053ccc00a7SMahesh Salgaonkar 
14063ccc00a7SMahesh Salgaonkar static int fadump_region_open(struct inode *inode, struct file *file)
14073ccc00a7SMahesh Salgaonkar {
14083ccc00a7SMahesh Salgaonkar 	return single_open(file, fadump_region_show, inode->i_private);
14093ccc00a7SMahesh Salgaonkar }
14103ccc00a7SMahesh Salgaonkar 
14113ccc00a7SMahesh Salgaonkar static const struct file_operations fadump_region_fops = {
14123ccc00a7SMahesh Salgaonkar 	.open    = fadump_region_open,
14133ccc00a7SMahesh Salgaonkar 	.read    = seq_read,
14143ccc00a7SMahesh Salgaonkar 	.llseek  = seq_lseek,
14153ccc00a7SMahesh Salgaonkar 	.release = single_release,
14163ccc00a7SMahesh Salgaonkar };
14173ccc00a7SMahesh Salgaonkar 
14183ccc00a7SMahesh Salgaonkar static void fadump_init_files(void)
14193ccc00a7SMahesh Salgaonkar {
14203ccc00a7SMahesh Salgaonkar 	struct dentry *debugfs_file;
14213ccc00a7SMahesh Salgaonkar 	int rc = 0;
14223ccc00a7SMahesh Salgaonkar 
14233ccc00a7SMahesh Salgaonkar 	rc = sysfs_create_file(kernel_kobj, &fadump_attr.attr);
14243ccc00a7SMahesh Salgaonkar 	if (rc)
14253ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "fadump: unable to create sysfs file"
14263ccc00a7SMahesh Salgaonkar 			" fadump_enabled (%d)\n", rc);
14273ccc00a7SMahesh Salgaonkar 
14283ccc00a7SMahesh Salgaonkar 	rc = sysfs_create_file(kernel_kobj, &fadump_register_attr.attr);
14293ccc00a7SMahesh Salgaonkar 	if (rc)
14303ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "fadump: unable to create sysfs file"
14313ccc00a7SMahesh Salgaonkar 			" fadump_registered (%d)\n", rc);
14323ccc00a7SMahesh Salgaonkar 
14333ccc00a7SMahesh Salgaonkar 	debugfs_file = debugfs_create_file("fadump_region", 0444,
14343ccc00a7SMahesh Salgaonkar 					powerpc_debugfs_root, NULL,
14353ccc00a7SMahesh Salgaonkar 					&fadump_region_fops);
14363ccc00a7SMahesh Salgaonkar 	if (!debugfs_file)
14373ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "fadump: unable to create debugfs file"
14383ccc00a7SMahesh Salgaonkar 				" fadump_region\n");
1439b500afffSMahesh Salgaonkar 
1440b500afffSMahesh Salgaonkar 	if (fw_dump.dump_active) {
1441b500afffSMahesh Salgaonkar 		rc = sysfs_create_file(kernel_kobj, &fadump_release_attr.attr);
1442b500afffSMahesh Salgaonkar 		if (rc)
1443b500afffSMahesh Salgaonkar 			printk(KERN_ERR "fadump: unable to create sysfs file"
1444b500afffSMahesh Salgaonkar 				" fadump_release_mem (%d)\n", rc);
1445b500afffSMahesh Salgaonkar 	}
14463ccc00a7SMahesh Salgaonkar 	return;
14473ccc00a7SMahesh Salgaonkar }
14483ccc00a7SMahesh Salgaonkar 
14493ccc00a7SMahesh Salgaonkar /*
14503ccc00a7SMahesh Salgaonkar  * Prepare for firmware-assisted dump.
14513ccc00a7SMahesh Salgaonkar  */
14523ccc00a7SMahesh Salgaonkar int __init setup_fadump(void)
14533ccc00a7SMahesh Salgaonkar {
14543ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_enabled)
14553ccc00a7SMahesh Salgaonkar 		return 0;
14563ccc00a7SMahesh Salgaonkar 
14573ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_supported) {
14583ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "Firmware-assisted dump is not supported on"
14593ccc00a7SMahesh Salgaonkar 			" this hardware\n");
14603ccc00a7SMahesh Salgaonkar 		return 0;
14613ccc00a7SMahesh Salgaonkar 	}
14623ccc00a7SMahesh Salgaonkar 
14633ccc00a7SMahesh Salgaonkar 	fadump_show_config();
14642df173d9SMahesh Salgaonkar 	/*
14652df173d9SMahesh Salgaonkar 	 * If dump data is available then see if it is valid and prepare for
14662df173d9SMahesh Salgaonkar 	 * saving it to the disk.
14672df173d9SMahesh Salgaonkar 	 */
1468b500afffSMahesh Salgaonkar 	if (fw_dump.dump_active) {
1469b500afffSMahesh Salgaonkar 		/*
1470b500afffSMahesh Salgaonkar 		 * if dump process fails then invalidate the registration
1471b500afffSMahesh Salgaonkar 		 * and release memory before proceeding for re-registration.
1472b500afffSMahesh Salgaonkar 		 */
1473b500afffSMahesh Salgaonkar 		if (process_fadump(fdm_active) < 0)
1474b500afffSMahesh Salgaonkar 			fadump_invalidate_release_mem();
1475b500afffSMahesh Salgaonkar 	}
14763ccc00a7SMahesh Salgaonkar 	/* Initialize the kernel dump memory structure for FAD registration. */
14772df173d9SMahesh Salgaonkar 	else if (fw_dump.reserve_dump_area_size)
14783ccc00a7SMahesh Salgaonkar 		init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
14793ccc00a7SMahesh Salgaonkar 	fadump_init_files();
14803ccc00a7SMahesh Salgaonkar 
14813ccc00a7SMahesh Salgaonkar 	return 1;
14823ccc00a7SMahesh Salgaonkar }
14833ccc00a7SMahesh Salgaonkar subsys_initcall(setup_fadump);
1484