xref: /openbmc/linux/arch/powerpc/kernel/fadump.c (revision a84fcd46)
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/debugfs.h>
343ccc00a7SMahesh Salgaonkar #include <linux/seq_file.h>
352df173d9SMahesh Salgaonkar #include <linux/crash_dump.h>
36b500afffSMahesh Salgaonkar #include <linux/kobject.h>
37b500afffSMahesh Salgaonkar #include <linux/sysfs.h>
38eb39c880SMahesh Salgaonkar 
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/debug.h>
44cad3c834SStephen Rothwell #include <asm/setup.h>
45eb39c880SMahesh Salgaonkar 
46eb39c880SMahesh Salgaonkar static struct fw_dump fw_dump;
473ccc00a7SMahesh Salgaonkar static struct fadump_mem_struct fdm;
483ccc00a7SMahesh Salgaonkar static const struct fadump_mem_struct *fdm_active;
493ccc00a7SMahesh Salgaonkar 
503ccc00a7SMahesh Salgaonkar static DEFINE_MUTEX(fadump_mutex);
512df173d9SMahesh Salgaonkar struct fad_crash_memory_ranges crash_memory_ranges[INIT_CRASHMEM_RANGES];
522df173d9SMahesh Salgaonkar int crash_mem_ranges;
53eb39c880SMahesh Salgaonkar 
54eb39c880SMahesh Salgaonkar /* Scan the Firmware Assisted dump configuration details. */
55eb39c880SMahesh Salgaonkar int __init early_init_dt_scan_fw_dump(unsigned long node,
56eb39c880SMahesh Salgaonkar 			const char *uname, int depth, void *data)
57eb39c880SMahesh Salgaonkar {
58eb39c880SMahesh Salgaonkar 	__be32 *sections;
59eb39c880SMahesh Salgaonkar 	int i, num_sections;
60eb39c880SMahesh Salgaonkar 	unsigned long size;
61eb39c880SMahesh Salgaonkar 	const int *token;
62eb39c880SMahesh Salgaonkar 
63eb39c880SMahesh Salgaonkar 	if (depth != 1 || strcmp(uname, "rtas") != 0)
64eb39c880SMahesh Salgaonkar 		return 0;
65eb39c880SMahesh Salgaonkar 
66eb39c880SMahesh Salgaonkar 	/*
67eb39c880SMahesh Salgaonkar 	 * Check if Firmware Assisted dump is supported. if yes, check
68eb39c880SMahesh Salgaonkar 	 * if dump has been initiated on last reboot.
69eb39c880SMahesh Salgaonkar 	 */
70eb39c880SMahesh Salgaonkar 	token = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump", NULL);
71eb39c880SMahesh Salgaonkar 	if (!token)
72eb39c880SMahesh Salgaonkar 		return 0;
73eb39c880SMahesh Salgaonkar 
74eb39c880SMahesh Salgaonkar 	fw_dump.fadump_supported = 1;
75eb39c880SMahesh Salgaonkar 	fw_dump.ibm_configure_kernel_dump = *token;
76eb39c880SMahesh Salgaonkar 
77eb39c880SMahesh Salgaonkar 	/*
78eb39c880SMahesh Salgaonkar 	 * The 'ibm,kernel-dump' rtas node is present only if there is
79eb39c880SMahesh Salgaonkar 	 * dump data waiting for us.
80eb39c880SMahesh Salgaonkar 	 */
813ccc00a7SMahesh Salgaonkar 	fdm_active = of_get_flat_dt_prop(node, "ibm,kernel-dump", NULL);
823ccc00a7SMahesh Salgaonkar 	if (fdm_active)
83eb39c880SMahesh Salgaonkar 		fw_dump.dump_active = 1;
84eb39c880SMahesh Salgaonkar 
85eb39c880SMahesh Salgaonkar 	/* Get the sizes required to store dump data for the firmware provided
86eb39c880SMahesh Salgaonkar 	 * dump sections.
87eb39c880SMahesh Salgaonkar 	 * For each dump section type supported, a 32bit cell which defines
88eb39c880SMahesh Salgaonkar 	 * the ID of a supported section followed by two 32 bit cells which
89eb39c880SMahesh Salgaonkar 	 * gives teh size of the section in bytes.
90eb39c880SMahesh Salgaonkar 	 */
91eb39c880SMahesh Salgaonkar 	sections = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump-sizes",
92eb39c880SMahesh Salgaonkar 					&size);
93eb39c880SMahesh Salgaonkar 
94eb39c880SMahesh Salgaonkar 	if (!sections)
95eb39c880SMahesh Salgaonkar 		return 0;
96eb39c880SMahesh Salgaonkar 
97eb39c880SMahesh Salgaonkar 	num_sections = size / (3 * sizeof(u32));
98eb39c880SMahesh Salgaonkar 
99eb39c880SMahesh Salgaonkar 	for (i = 0; i < num_sections; i++, sections += 3) {
100eb39c880SMahesh Salgaonkar 		u32 type = (u32)of_read_number(sections, 1);
101eb39c880SMahesh Salgaonkar 
102eb39c880SMahesh Salgaonkar 		switch (type) {
103eb39c880SMahesh Salgaonkar 		case FADUMP_CPU_STATE_DATA:
104eb39c880SMahesh Salgaonkar 			fw_dump.cpu_state_data_size =
105eb39c880SMahesh Salgaonkar 					of_read_ulong(&sections[1], 2);
106eb39c880SMahesh Salgaonkar 			break;
107eb39c880SMahesh Salgaonkar 		case FADUMP_HPTE_REGION:
108eb39c880SMahesh Salgaonkar 			fw_dump.hpte_region_size =
109eb39c880SMahesh Salgaonkar 					of_read_ulong(&sections[1], 2);
110eb39c880SMahesh Salgaonkar 			break;
111eb39c880SMahesh Salgaonkar 		}
112eb39c880SMahesh Salgaonkar 	}
113eb39c880SMahesh Salgaonkar 	return 1;
114eb39c880SMahesh Salgaonkar }
115eb39c880SMahesh Salgaonkar 
1163ccc00a7SMahesh Salgaonkar int is_fadump_active(void)
1173ccc00a7SMahesh Salgaonkar {
1183ccc00a7SMahesh Salgaonkar 	return fw_dump.dump_active;
1193ccc00a7SMahesh Salgaonkar }
1203ccc00a7SMahesh Salgaonkar 
1213ccc00a7SMahesh Salgaonkar /* Print firmware assisted dump configurations for debugging purpose. */
1223ccc00a7SMahesh Salgaonkar static void fadump_show_config(void)
1233ccc00a7SMahesh Salgaonkar {
1243ccc00a7SMahesh Salgaonkar 	pr_debug("Support for firmware-assisted dump (fadump): %s\n",
1253ccc00a7SMahesh Salgaonkar 			(fw_dump.fadump_supported ? "present" : "no support"));
1263ccc00a7SMahesh Salgaonkar 
1273ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_supported)
1283ccc00a7SMahesh Salgaonkar 		return;
1293ccc00a7SMahesh Salgaonkar 
1303ccc00a7SMahesh Salgaonkar 	pr_debug("Fadump enabled    : %s\n",
1313ccc00a7SMahesh Salgaonkar 				(fw_dump.fadump_enabled ? "yes" : "no"));
1323ccc00a7SMahesh Salgaonkar 	pr_debug("Dump Active       : %s\n",
1333ccc00a7SMahesh Salgaonkar 				(fw_dump.dump_active ? "yes" : "no"));
1343ccc00a7SMahesh Salgaonkar 	pr_debug("Dump section sizes:\n");
1353ccc00a7SMahesh Salgaonkar 	pr_debug("    CPU state data size: %lx\n", fw_dump.cpu_state_data_size);
1363ccc00a7SMahesh Salgaonkar 	pr_debug("    HPTE region size   : %lx\n", fw_dump.hpte_region_size);
1373ccc00a7SMahesh Salgaonkar 	pr_debug("Boot memory size  : %lx\n", fw_dump.boot_memory_size);
1383ccc00a7SMahesh Salgaonkar }
1393ccc00a7SMahesh Salgaonkar 
1403ccc00a7SMahesh Salgaonkar static unsigned long init_fadump_mem_struct(struct fadump_mem_struct *fdm,
1413ccc00a7SMahesh Salgaonkar 				unsigned long addr)
1423ccc00a7SMahesh Salgaonkar {
1433ccc00a7SMahesh Salgaonkar 	if (!fdm)
1443ccc00a7SMahesh Salgaonkar 		return 0;
1453ccc00a7SMahesh Salgaonkar 
1463ccc00a7SMahesh Salgaonkar 	memset(fdm, 0, sizeof(struct fadump_mem_struct));
1473ccc00a7SMahesh Salgaonkar 	addr = addr & PAGE_MASK;
1483ccc00a7SMahesh Salgaonkar 
1493ccc00a7SMahesh Salgaonkar 	fdm->header.dump_format_version = 0x00000001;
1503ccc00a7SMahesh Salgaonkar 	fdm->header.dump_num_sections = 3;
1513ccc00a7SMahesh Salgaonkar 	fdm->header.dump_status_flag = 0;
1523ccc00a7SMahesh Salgaonkar 	fdm->header.offset_first_dump_section =
1533ccc00a7SMahesh Salgaonkar 		(u32)offsetof(struct fadump_mem_struct, cpu_state_data);
1543ccc00a7SMahesh Salgaonkar 
1553ccc00a7SMahesh Salgaonkar 	/*
1563ccc00a7SMahesh Salgaonkar 	 * Fields for disk dump option.
1573ccc00a7SMahesh Salgaonkar 	 * We are not using disk dump option, hence set these fields to 0.
1583ccc00a7SMahesh Salgaonkar 	 */
1593ccc00a7SMahesh Salgaonkar 	fdm->header.dd_block_size = 0;
1603ccc00a7SMahesh Salgaonkar 	fdm->header.dd_block_offset = 0;
1613ccc00a7SMahesh Salgaonkar 	fdm->header.dd_num_blocks = 0;
1623ccc00a7SMahesh Salgaonkar 	fdm->header.dd_offset_disk_path = 0;
1633ccc00a7SMahesh Salgaonkar 
1643ccc00a7SMahesh Salgaonkar 	/* set 0 to disable an automatic dump-reboot. */
1653ccc00a7SMahesh Salgaonkar 	fdm->header.max_time_auto = 0;
1663ccc00a7SMahesh Salgaonkar 
1673ccc00a7SMahesh Salgaonkar 	/* Kernel dump sections */
1683ccc00a7SMahesh Salgaonkar 	/* cpu state data section. */
1693ccc00a7SMahesh Salgaonkar 	fdm->cpu_state_data.request_flag = FADUMP_REQUEST_FLAG;
1703ccc00a7SMahesh Salgaonkar 	fdm->cpu_state_data.source_data_type = FADUMP_CPU_STATE_DATA;
1713ccc00a7SMahesh Salgaonkar 	fdm->cpu_state_data.source_address = 0;
1723ccc00a7SMahesh Salgaonkar 	fdm->cpu_state_data.source_len = fw_dump.cpu_state_data_size;
1733ccc00a7SMahesh Salgaonkar 	fdm->cpu_state_data.destination_address = addr;
1743ccc00a7SMahesh Salgaonkar 	addr += fw_dump.cpu_state_data_size;
1753ccc00a7SMahesh Salgaonkar 
1763ccc00a7SMahesh Salgaonkar 	/* hpte region section */
1773ccc00a7SMahesh Salgaonkar 	fdm->hpte_region.request_flag = FADUMP_REQUEST_FLAG;
1783ccc00a7SMahesh Salgaonkar 	fdm->hpte_region.source_data_type = FADUMP_HPTE_REGION;
1793ccc00a7SMahesh Salgaonkar 	fdm->hpte_region.source_address = 0;
1803ccc00a7SMahesh Salgaonkar 	fdm->hpte_region.source_len = fw_dump.hpte_region_size;
1813ccc00a7SMahesh Salgaonkar 	fdm->hpte_region.destination_address = addr;
1823ccc00a7SMahesh Salgaonkar 	addr += fw_dump.hpte_region_size;
1833ccc00a7SMahesh Salgaonkar 
1843ccc00a7SMahesh Salgaonkar 	/* RMA region section */
1853ccc00a7SMahesh Salgaonkar 	fdm->rmr_region.request_flag = FADUMP_REQUEST_FLAG;
1863ccc00a7SMahesh Salgaonkar 	fdm->rmr_region.source_data_type = FADUMP_REAL_MODE_REGION;
1873ccc00a7SMahesh Salgaonkar 	fdm->rmr_region.source_address = RMA_START;
1883ccc00a7SMahesh Salgaonkar 	fdm->rmr_region.source_len = fw_dump.boot_memory_size;
1893ccc00a7SMahesh Salgaonkar 	fdm->rmr_region.destination_address = addr;
1903ccc00a7SMahesh Salgaonkar 	addr += fw_dump.boot_memory_size;
1913ccc00a7SMahesh Salgaonkar 
1923ccc00a7SMahesh Salgaonkar 	return addr;
1933ccc00a7SMahesh Salgaonkar }
1943ccc00a7SMahesh Salgaonkar 
195eb39c880SMahesh Salgaonkar /**
196eb39c880SMahesh Salgaonkar  * fadump_calculate_reserve_size(): reserve variable boot area 5% of System RAM
197eb39c880SMahesh Salgaonkar  *
198eb39c880SMahesh Salgaonkar  * Function to find the largest memory size we need to reserve during early
199eb39c880SMahesh Salgaonkar  * boot process. This will be the size of the memory that is required for a
200eb39c880SMahesh Salgaonkar  * kernel to boot successfully.
201eb39c880SMahesh Salgaonkar  *
202eb39c880SMahesh Salgaonkar  * This function has been taken from phyp-assisted dump feature implementation.
203eb39c880SMahesh Salgaonkar  *
204eb39c880SMahesh Salgaonkar  * returns larger of 256MB or 5% rounded down to multiples of 256MB.
205eb39c880SMahesh Salgaonkar  *
206eb39c880SMahesh Salgaonkar  * TODO: Come up with better approach to find out more accurate memory size
207eb39c880SMahesh Salgaonkar  * that is required for a kernel to boot successfully.
208eb39c880SMahesh Salgaonkar  *
209eb39c880SMahesh Salgaonkar  */
210eb39c880SMahesh Salgaonkar static inline unsigned long fadump_calculate_reserve_size(void)
211eb39c880SMahesh Salgaonkar {
212eb39c880SMahesh Salgaonkar 	unsigned long size;
213eb39c880SMahesh Salgaonkar 
214eb39c880SMahesh Salgaonkar 	/*
215eb39c880SMahesh Salgaonkar 	 * Check if the size is specified through fadump_reserve_mem= cmdline
216eb39c880SMahesh Salgaonkar 	 * option. If yes, then use that.
217eb39c880SMahesh Salgaonkar 	 */
218eb39c880SMahesh Salgaonkar 	if (fw_dump.reserve_bootvar)
219eb39c880SMahesh Salgaonkar 		return fw_dump.reserve_bootvar;
220eb39c880SMahesh Salgaonkar 
221eb39c880SMahesh Salgaonkar 	/* divide by 20 to get 5% of value */
222eb39c880SMahesh Salgaonkar 	size = memblock_end_of_DRAM() / 20;
223eb39c880SMahesh Salgaonkar 
224eb39c880SMahesh Salgaonkar 	/* round it down in multiples of 256 */
225eb39c880SMahesh Salgaonkar 	size = size & ~0x0FFFFFFFUL;
226eb39c880SMahesh Salgaonkar 
227eb39c880SMahesh Salgaonkar 	/* Truncate to memory_limit. We don't want to over reserve the memory.*/
228eb39c880SMahesh Salgaonkar 	if (memory_limit && size > memory_limit)
229eb39c880SMahesh Salgaonkar 		size = memory_limit;
230eb39c880SMahesh Salgaonkar 
231eb39c880SMahesh Salgaonkar 	return (size > MIN_BOOT_MEM ? size : MIN_BOOT_MEM);
232eb39c880SMahesh Salgaonkar }
233eb39c880SMahesh Salgaonkar 
234eb39c880SMahesh Salgaonkar /*
235eb39c880SMahesh Salgaonkar  * Calculate the total memory size required to be reserved for
236eb39c880SMahesh Salgaonkar  * firmware-assisted dump registration.
237eb39c880SMahesh Salgaonkar  */
238eb39c880SMahesh Salgaonkar static unsigned long get_fadump_area_size(void)
239eb39c880SMahesh Salgaonkar {
240eb39c880SMahesh Salgaonkar 	unsigned long size = 0;
241eb39c880SMahesh Salgaonkar 
242eb39c880SMahesh Salgaonkar 	size += fw_dump.cpu_state_data_size;
243eb39c880SMahesh Salgaonkar 	size += fw_dump.hpte_region_size;
244eb39c880SMahesh Salgaonkar 	size += fw_dump.boot_memory_size;
2452df173d9SMahesh Salgaonkar 	size += sizeof(struct fadump_crash_info_header);
2462df173d9SMahesh Salgaonkar 	size += sizeof(struct elfhdr); /* ELF core header.*/
247ebaeb5aeSMahesh Salgaonkar 	size += sizeof(struct elf_phdr); /* place holder for cpu notes */
2482df173d9SMahesh Salgaonkar 	/* Program headers for crash memory regions. */
2492df173d9SMahesh Salgaonkar 	size += sizeof(struct elf_phdr) * (memblock_num_regions(memory) + 2);
250eb39c880SMahesh Salgaonkar 
251eb39c880SMahesh Salgaonkar 	size = PAGE_ALIGN(size);
252eb39c880SMahesh Salgaonkar 	return size;
253eb39c880SMahesh Salgaonkar }
254eb39c880SMahesh Salgaonkar 
255eb39c880SMahesh Salgaonkar int __init fadump_reserve_mem(void)
256eb39c880SMahesh Salgaonkar {
257eb39c880SMahesh Salgaonkar 	unsigned long base, size, memory_boundary;
258eb39c880SMahesh Salgaonkar 
259eb39c880SMahesh Salgaonkar 	if (!fw_dump.fadump_enabled)
260eb39c880SMahesh Salgaonkar 		return 0;
261eb39c880SMahesh Salgaonkar 
262eb39c880SMahesh Salgaonkar 	if (!fw_dump.fadump_supported) {
263eb39c880SMahesh Salgaonkar 		printk(KERN_INFO "Firmware-assisted dump is not supported on"
264eb39c880SMahesh Salgaonkar 				" this hardware\n");
265eb39c880SMahesh Salgaonkar 		fw_dump.fadump_enabled = 0;
266eb39c880SMahesh Salgaonkar 		return 0;
267eb39c880SMahesh Salgaonkar 	}
2683ccc00a7SMahesh Salgaonkar 	/*
2693ccc00a7SMahesh Salgaonkar 	 * Initialize boot memory size
2703ccc00a7SMahesh Salgaonkar 	 * If dump is active then we have already calculated the size during
2713ccc00a7SMahesh Salgaonkar 	 * first kernel.
2723ccc00a7SMahesh Salgaonkar 	 */
2733ccc00a7SMahesh Salgaonkar 	if (fdm_active)
2743ccc00a7SMahesh Salgaonkar 		fw_dump.boot_memory_size = fdm_active->rmr_region.source_len;
2753ccc00a7SMahesh Salgaonkar 	else
276eb39c880SMahesh Salgaonkar 		fw_dump.boot_memory_size = fadump_calculate_reserve_size();
277eb39c880SMahesh Salgaonkar 
278eb39c880SMahesh Salgaonkar 	/*
279eb39c880SMahesh Salgaonkar 	 * Calculate the memory boundary.
280eb39c880SMahesh Salgaonkar 	 * If memory_limit is less than actual memory boundary then reserve
281eb39c880SMahesh Salgaonkar 	 * the memory for fadump beyond the memory_limit and adjust the
282eb39c880SMahesh Salgaonkar 	 * memory_limit accordingly, so that the running kernel can run with
283eb39c880SMahesh Salgaonkar 	 * specified memory_limit.
284eb39c880SMahesh Salgaonkar 	 */
285eb39c880SMahesh Salgaonkar 	if (memory_limit && memory_limit < memblock_end_of_DRAM()) {
286eb39c880SMahesh Salgaonkar 		size = get_fadump_area_size();
287eb39c880SMahesh Salgaonkar 		if ((memory_limit + size) < memblock_end_of_DRAM())
288eb39c880SMahesh Salgaonkar 			memory_limit += size;
289eb39c880SMahesh Salgaonkar 		else
290eb39c880SMahesh Salgaonkar 			memory_limit = memblock_end_of_DRAM();
291eb39c880SMahesh Salgaonkar 		printk(KERN_INFO "Adjusted memory_limit for firmware-assisted"
292a84fcd46SSuzuki Poulose 				" dump, now %#016llx\n", memory_limit);
293eb39c880SMahesh Salgaonkar 	}
294eb39c880SMahesh Salgaonkar 	if (memory_limit)
295eb39c880SMahesh Salgaonkar 		memory_boundary = memory_limit;
296eb39c880SMahesh Salgaonkar 	else
297eb39c880SMahesh Salgaonkar 		memory_boundary = memblock_end_of_DRAM();
298eb39c880SMahesh Salgaonkar 
299eb39c880SMahesh Salgaonkar 	if (fw_dump.dump_active) {
300eb39c880SMahesh Salgaonkar 		printk(KERN_INFO "Firmware-assisted dump is active.\n");
301eb39c880SMahesh Salgaonkar 		/*
302eb39c880SMahesh Salgaonkar 		 * If last boot has crashed then reserve all the memory
303eb39c880SMahesh Salgaonkar 		 * above boot_memory_size so that we don't touch it until
304eb39c880SMahesh Salgaonkar 		 * dump is written to disk by userspace tool. This memory
305eb39c880SMahesh Salgaonkar 		 * will be released for general use once the dump is saved.
306eb39c880SMahesh Salgaonkar 		 */
307eb39c880SMahesh Salgaonkar 		base = fw_dump.boot_memory_size;
308eb39c880SMahesh Salgaonkar 		size = memory_boundary - base;
309eb39c880SMahesh Salgaonkar 		memblock_reserve(base, size);
310eb39c880SMahesh Salgaonkar 		printk(KERN_INFO "Reserved %ldMB of memory at %ldMB "
311eb39c880SMahesh Salgaonkar 				"for saving crash dump\n",
312eb39c880SMahesh Salgaonkar 				(unsigned long)(size >> 20),
313eb39c880SMahesh Salgaonkar 				(unsigned long)(base >> 20));
3142df173d9SMahesh Salgaonkar 
3152df173d9SMahesh Salgaonkar 		fw_dump.fadumphdr_addr =
3162df173d9SMahesh Salgaonkar 				fdm_active->rmr_region.destination_address +
3172df173d9SMahesh Salgaonkar 				fdm_active->rmr_region.source_len;
3182df173d9SMahesh Salgaonkar 		pr_debug("fadumphdr_addr = %p\n",
3192df173d9SMahesh Salgaonkar 				(void *) fw_dump.fadumphdr_addr);
320eb39c880SMahesh Salgaonkar 	} else {
321eb39c880SMahesh Salgaonkar 		/* Reserve the memory at the top of memory. */
322eb39c880SMahesh Salgaonkar 		size = get_fadump_area_size();
323eb39c880SMahesh Salgaonkar 		base = memory_boundary - size;
324eb39c880SMahesh Salgaonkar 		memblock_reserve(base, size);
325eb39c880SMahesh Salgaonkar 		printk(KERN_INFO "Reserved %ldMB of memory at %ldMB "
326eb39c880SMahesh Salgaonkar 				"for firmware-assisted dump\n",
327eb39c880SMahesh Salgaonkar 				(unsigned long)(size >> 20),
328eb39c880SMahesh Salgaonkar 				(unsigned long)(base >> 20));
329eb39c880SMahesh Salgaonkar 	}
330eb39c880SMahesh Salgaonkar 	fw_dump.reserve_dump_area_start = base;
331eb39c880SMahesh Salgaonkar 	fw_dump.reserve_dump_area_size = size;
332eb39c880SMahesh Salgaonkar 	return 1;
333eb39c880SMahesh Salgaonkar }
334eb39c880SMahesh Salgaonkar 
335eb39c880SMahesh Salgaonkar /* Look for fadump= cmdline option. */
336eb39c880SMahesh Salgaonkar static int __init early_fadump_param(char *p)
337eb39c880SMahesh Salgaonkar {
338eb39c880SMahesh Salgaonkar 	if (!p)
339eb39c880SMahesh Salgaonkar 		return 1;
340eb39c880SMahesh Salgaonkar 
341eb39c880SMahesh Salgaonkar 	if (strncmp(p, "on", 2) == 0)
342eb39c880SMahesh Salgaonkar 		fw_dump.fadump_enabled = 1;
343eb39c880SMahesh Salgaonkar 	else if (strncmp(p, "off", 3) == 0)
344eb39c880SMahesh Salgaonkar 		fw_dump.fadump_enabled = 0;
345eb39c880SMahesh Salgaonkar 
346eb39c880SMahesh Salgaonkar 	return 0;
347eb39c880SMahesh Salgaonkar }
348eb39c880SMahesh Salgaonkar early_param("fadump", early_fadump_param);
349eb39c880SMahesh Salgaonkar 
350eb39c880SMahesh Salgaonkar /* Look for fadump_reserve_mem= cmdline option */
351eb39c880SMahesh Salgaonkar static int __init early_fadump_reserve_mem(char *p)
352eb39c880SMahesh Salgaonkar {
353eb39c880SMahesh Salgaonkar 	if (p)
354eb39c880SMahesh Salgaonkar 		fw_dump.reserve_bootvar = memparse(p, &p);
355eb39c880SMahesh Salgaonkar 	return 0;
356eb39c880SMahesh Salgaonkar }
357eb39c880SMahesh Salgaonkar early_param("fadump_reserve_mem", early_fadump_reserve_mem);
3583ccc00a7SMahesh Salgaonkar 
3593ccc00a7SMahesh Salgaonkar static void register_fw_dump(struct fadump_mem_struct *fdm)
3603ccc00a7SMahesh Salgaonkar {
3613ccc00a7SMahesh Salgaonkar 	int rc;
3623ccc00a7SMahesh Salgaonkar 	unsigned int wait_time;
3633ccc00a7SMahesh Salgaonkar 
3643ccc00a7SMahesh Salgaonkar 	pr_debug("Registering for firmware-assisted kernel dump...\n");
3653ccc00a7SMahesh Salgaonkar 
3663ccc00a7SMahesh Salgaonkar 	/* TODO: Add upper time limit for the delay */
3673ccc00a7SMahesh Salgaonkar 	do {
3683ccc00a7SMahesh Salgaonkar 		rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
3693ccc00a7SMahesh Salgaonkar 			FADUMP_REGISTER, fdm,
3703ccc00a7SMahesh Salgaonkar 			sizeof(struct fadump_mem_struct));
3713ccc00a7SMahesh Salgaonkar 
3723ccc00a7SMahesh Salgaonkar 		wait_time = rtas_busy_delay_time(rc);
3733ccc00a7SMahesh Salgaonkar 		if (wait_time)
3743ccc00a7SMahesh Salgaonkar 			mdelay(wait_time);
3753ccc00a7SMahesh Salgaonkar 
3763ccc00a7SMahesh Salgaonkar 	} while (wait_time);
3773ccc00a7SMahesh Salgaonkar 
3783ccc00a7SMahesh Salgaonkar 	switch (rc) {
3793ccc00a7SMahesh Salgaonkar 	case -1:
3803ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "Failed to register firmware-assisted kernel"
3813ccc00a7SMahesh Salgaonkar 			" dump. Hardware Error(%d).\n", rc);
3823ccc00a7SMahesh Salgaonkar 		break;
3833ccc00a7SMahesh Salgaonkar 	case -3:
3843ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "Failed to register firmware-assisted kernel"
3853ccc00a7SMahesh Salgaonkar 			" dump. Parameter Error(%d).\n", rc);
3863ccc00a7SMahesh Salgaonkar 		break;
3873ccc00a7SMahesh Salgaonkar 	case -9:
3883ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "firmware-assisted kernel dump is already "
3893ccc00a7SMahesh Salgaonkar 			" registered.");
3903ccc00a7SMahesh Salgaonkar 		fw_dump.dump_registered = 1;
3913ccc00a7SMahesh Salgaonkar 		break;
3923ccc00a7SMahesh Salgaonkar 	case 0:
3933ccc00a7SMahesh Salgaonkar 		printk(KERN_INFO "firmware-assisted kernel dump registration"
3943ccc00a7SMahesh Salgaonkar 			" is successful\n");
3953ccc00a7SMahesh Salgaonkar 		fw_dump.dump_registered = 1;
3963ccc00a7SMahesh Salgaonkar 		break;
3973ccc00a7SMahesh Salgaonkar 	}
3983ccc00a7SMahesh Salgaonkar }
3993ccc00a7SMahesh Salgaonkar 
400ebaeb5aeSMahesh Salgaonkar void crash_fadump(struct pt_regs *regs, const char *str)
401ebaeb5aeSMahesh Salgaonkar {
402ebaeb5aeSMahesh Salgaonkar 	struct fadump_crash_info_header *fdh = NULL;
403ebaeb5aeSMahesh Salgaonkar 
404ebaeb5aeSMahesh Salgaonkar 	if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr)
405ebaeb5aeSMahesh Salgaonkar 		return;
406ebaeb5aeSMahesh Salgaonkar 
407ebaeb5aeSMahesh Salgaonkar 	fdh = __va(fw_dump.fadumphdr_addr);
408ebaeb5aeSMahesh Salgaonkar 	crashing_cpu = smp_processor_id();
409ebaeb5aeSMahesh Salgaonkar 	fdh->crashing_cpu = crashing_cpu;
410ebaeb5aeSMahesh Salgaonkar 	crash_save_vmcoreinfo();
411ebaeb5aeSMahesh Salgaonkar 
412ebaeb5aeSMahesh Salgaonkar 	if (regs)
413ebaeb5aeSMahesh Salgaonkar 		fdh->regs = *regs;
414ebaeb5aeSMahesh Salgaonkar 	else
415ebaeb5aeSMahesh Salgaonkar 		ppc_save_regs(&fdh->regs);
416ebaeb5aeSMahesh Salgaonkar 
417ebaeb5aeSMahesh Salgaonkar 	fdh->cpu_online_mask = *cpu_online_mask;
418ebaeb5aeSMahesh Salgaonkar 
419ebaeb5aeSMahesh Salgaonkar 	/* Call ibm,os-term rtas call to trigger firmware assisted dump */
420ebaeb5aeSMahesh Salgaonkar 	rtas_os_term((char *)str);
421ebaeb5aeSMahesh Salgaonkar }
422ebaeb5aeSMahesh Salgaonkar 
423ebaeb5aeSMahesh Salgaonkar #define GPR_MASK	0xffffff0000000000
424ebaeb5aeSMahesh Salgaonkar static inline int fadump_gpr_index(u64 id)
425ebaeb5aeSMahesh Salgaonkar {
426ebaeb5aeSMahesh Salgaonkar 	int i = -1;
427ebaeb5aeSMahesh Salgaonkar 	char str[3];
428ebaeb5aeSMahesh Salgaonkar 
429ebaeb5aeSMahesh Salgaonkar 	if ((id & GPR_MASK) == REG_ID("GPR")) {
430ebaeb5aeSMahesh Salgaonkar 		/* get the digits at the end */
431ebaeb5aeSMahesh Salgaonkar 		id &= ~GPR_MASK;
432ebaeb5aeSMahesh Salgaonkar 		id >>= 24;
433ebaeb5aeSMahesh Salgaonkar 		str[2] = '\0';
434ebaeb5aeSMahesh Salgaonkar 		str[1] = id & 0xff;
435ebaeb5aeSMahesh Salgaonkar 		str[0] = (id >> 8) & 0xff;
436ebaeb5aeSMahesh Salgaonkar 		sscanf(str, "%d", &i);
437ebaeb5aeSMahesh Salgaonkar 		if (i > 31)
438ebaeb5aeSMahesh Salgaonkar 			i = -1;
439ebaeb5aeSMahesh Salgaonkar 	}
440ebaeb5aeSMahesh Salgaonkar 	return i;
441ebaeb5aeSMahesh Salgaonkar }
442ebaeb5aeSMahesh Salgaonkar 
443ebaeb5aeSMahesh Salgaonkar static inline void fadump_set_regval(struct pt_regs *regs, u64 reg_id,
444ebaeb5aeSMahesh Salgaonkar 								u64 reg_val)
445ebaeb5aeSMahesh Salgaonkar {
446ebaeb5aeSMahesh Salgaonkar 	int i;
447ebaeb5aeSMahesh Salgaonkar 
448ebaeb5aeSMahesh Salgaonkar 	i = fadump_gpr_index(reg_id);
449ebaeb5aeSMahesh Salgaonkar 	if (i >= 0)
450ebaeb5aeSMahesh Salgaonkar 		regs->gpr[i] = (unsigned long)reg_val;
451ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("NIA"))
452ebaeb5aeSMahesh Salgaonkar 		regs->nip = (unsigned long)reg_val;
453ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("MSR"))
454ebaeb5aeSMahesh Salgaonkar 		regs->msr = (unsigned long)reg_val;
455ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("CTR"))
456ebaeb5aeSMahesh Salgaonkar 		regs->ctr = (unsigned long)reg_val;
457ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("LR"))
458ebaeb5aeSMahesh Salgaonkar 		regs->link = (unsigned long)reg_val;
459ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("XER"))
460ebaeb5aeSMahesh Salgaonkar 		regs->xer = (unsigned long)reg_val;
461ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("CR"))
462ebaeb5aeSMahesh Salgaonkar 		regs->ccr = (unsigned long)reg_val;
463ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("DAR"))
464ebaeb5aeSMahesh Salgaonkar 		regs->dar = (unsigned long)reg_val;
465ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("DSISR"))
466ebaeb5aeSMahesh Salgaonkar 		regs->dsisr = (unsigned long)reg_val;
467ebaeb5aeSMahesh Salgaonkar }
468ebaeb5aeSMahesh Salgaonkar 
469ebaeb5aeSMahesh Salgaonkar static struct fadump_reg_entry*
470ebaeb5aeSMahesh Salgaonkar fadump_read_registers(struct fadump_reg_entry *reg_entry, struct pt_regs *regs)
471ebaeb5aeSMahesh Salgaonkar {
472ebaeb5aeSMahesh Salgaonkar 	memset(regs, 0, sizeof(struct pt_regs));
473ebaeb5aeSMahesh Salgaonkar 
474ebaeb5aeSMahesh Salgaonkar 	while (reg_entry->reg_id != REG_ID("CPUEND")) {
475ebaeb5aeSMahesh Salgaonkar 		fadump_set_regval(regs, reg_entry->reg_id,
476ebaeb5aeSMahesh Salgaonkar 					reg_entry->reg_value);
477ebaeb5aeSMahesh Salgaonkar 		reg_entry++;
478ebaeb5aeSMahesh Salgaonkar 	}
479ebaeb5aeSMahesh Salgaonkar 	reg_entry++;
480ebaeb5aeSMahesh Salgaonkar 	return reg_entry;
481ebaeb5aeSMahesh Salgaonkar }
482ebaeb5aeSMahesh Salgaonkar 
483ebaeb5aeSMahesh Salgaonkar static u32 *fadump_append_elf_note(u32 *buf, char *name, unsigned type,
484ebaeb5aeSMahesh Salgaonkar 						void *data, size_t data_len)
485ebaeb5aeSMahesh Salgaonkar {
486ebaeb5aeSMahesh Salgaonkar 	struct elf_note note;
487ebaeb5aeSMahesh Salgaonkar 
488ebaeb5aeSMahesh Salgaonkar 	note.n_namesz = strlen(name) + 1;
489ebaeb5aeSMahesh Salgaonkar 	note.n_descsz = data_len;
490ebaeb5aeSMahesh Salgaonkar 	note.n_type   = type;
491ebaeb5aeSMahesh Salgaonkar 	memcpy(buf, &note, sizeof(note));
492ebaeb5aeSMahesh Salgaonkar 	buf += (sizeof(note) + 3)/4;
493ebaeb5aeSMahesh Salgaonkar 	memcpy(buf, name, note.n_namesz);
494ebaeb5aeSMahesh Salgaonkar 	buf += (note.n_namesz + 3)/4;
495ebaeb5aeSMahesh Salgaonkar 	memcpy(buf, data, note.n_descsz);
496ebaeb5aeSMahesh Salgaonkar 	buf += (note.n_descsz + 3)/4;
497ebaeb5aeSMahesh Salgaonkar 
498ebaeb5aeSMahesh Salgaonkar 	return buf;
499ebaeb5aeSMahesh Salgaonkar }
500ebaeb5aeSMahesh Salgaonkar 
501ebaeb5aeSMahesh Salgaonkar static void fadump_final_note(u32 *buf)
502ebaeb5aeSMahesh Salgaonkar {
503ebaeb5aeSMahesh Salgaonkar 	struct elf_note note;
504ebaeb5aeSMahesh Salgaonkar 
505ebaeb5aeSMahesh Salgaonkar 	note.n_namesz = 0;
506ebaeb5aeSMahesh Salgaonkar 	note.n_descsz = 0;
507ebaeb5aeSMahesh Salgaonkar 	note.n_type   = 0;
508ebaeb5aeSMahesh Salgaonkar 	memcpy(buf, &note, sizeof(note));
509ebaeb5aeSMahesh Salgaonkar }
510ebaeb5aeSMahesh Salgaonkar 
511ebaeb5aeSMahesh Salgaonkar static u32 *fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs)
512ebaeb5aeSMahesh Salgaonkar {
513ebaeb5aeSMahesh Salgaonkar 	struct elf_prstatus prstatus;
514ebaeb5aeSMahesh Salgaonkar 
515ebaeb5aeSMahesh Salgaonkar 	memset(&prstatus, 0, sizeof(prstatus));
516ebaeb5aeSMahesh Salgaonkar 	/*
517ebaeb5aeSMahesh Salgaonkar 	 * FIXME: How do i get PID? Do I really need it?
518ebaeb5aeSMahesh Salgaonkar 	 * prstatus.pr_pid = ????
519ebaeb5aeSMahesh Salgaonkar 	 */
520ebaeb5aeSMahesh Salgaonkar 	elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
521ebaeb5aeSMahesh Salgaonkar 	buf = fadump_append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS,
522ebaeb5aeSMahesh Salgaonkar 				&prstatus, sizeof(prstatus));
523ebaeb5aeSMahesh Salgaonkar 	return buf;
524ebaeb5aeSMahesh Salgaonkar }
525ebaeb5aeSMahesh Salgaonkar 
526ebaeb5aeSMahesh Salgaonkar static void fadump_update_elfcore_header(char *bufp)
527ebaeb5aeSMahesh Salgaonkar {
528ebaeb5aeSMahesh Salgaonkar 	struct elfhdr *elf;
529ebaeb5aeSMahesh Salgaonkar 	struct elf_phdr *phdr;
530ebaeb5aeSMahesh Salgaonkar 
531ebaeb5aeSMahesh Salgaonkar 	elf = (struct elfhdr *)bufp;
532ebaeb5aeSMahesh Salgaonkar 	bufp += sizeof(struct elfhdr);
533ebaeb5aeSMahesh Salgaonkar 
534ebaeb5aeSMahesh Salgaonkar 	/* First note is a place holder for cpu notes info. */
535ebaeb5aeSMahesh Salgaonkar 	phdr = (struct elf_phdr *)bufp;
536ebaeb5aeSMahesh Salgaonkar 
537ebaeb5aeSMahesh Salgaonkar 	if (phdr->p_type == PT_NOTE) {
538ebaeb5aeSMahesh Salgaonkar 		phdr->p_paddr = fw_dump.cpu_notes_buf;
539ebaeb5aeSMahesh Salgaonkar 		phdr->p_offset	= phdr->p_paddr;
540ebaeb5aeSMahesh Salgaonkar 		phdr->p_filesz	= fw_dump.cpu_notes_buf_size;
541ebaeb5aeSMahesh Salgaonkar 		phdr->p_memsz = fw_dump.cpu_notes_buf_size;
542ebaeb5aeSMahesh Salgaonkar 	}
543ebaeb5aeSMahesh Salgaonkar 	return;
544ebaeb5aeSMahesh Salgaonkar }
545ebaeb5aeSMahesh Salgaonkar 
546ebaeb5aeSMahesh Salgaonkar static void *fadump_cpu_notes_buf_alloc(unsigned long size)
547ebaeb5aeSMahesh Salgaonkar {
548ebaeb5aeSMahesh Salgaonkar 	void *vaddr;
549ebaeb5aeSMahesh Salgaonkar 	struct page *page;
550ebaeb5aeSMahesh Salgaonkar 	unsigned long order, count, i;
551ebaeb5aeSMahesh Salgaonkar 
552ebaeb5aeSMahesh Salgaonkar 	order = get_order(size);
553ebaeb5aeSMahesh Salgaonkar 	vaddr = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
554ebaeb5aeSMahesh Salgaonkar 	if (!vaddr)
555ebaeb5aeSMahesh Salgaonkar 		return NULL;
556ebaeb5aeSMahesh Salgaonkar 
557ebaeb5aeSMahesh Salgaonkar 	count = 1 << order;
558ebaeb5aeSMahesh Salgaonkar 	page = virt_to_page(vaddr);
559ebaeb5aeSMahesh Salgaonkar 	for (i = 0; i < count; i++)
560ebaeb5aeSMahesh Salgaonkar 		SetPageReserved(page + i);
561ebaeb5aeSMahesh Salgaonkar 	return vaddr;
562ebaeb5aeSMahesh Salgaonkar }
563ebaeb5aeSMahesh Salgaonkar 
564ebaeb5aeSMahesh Salgaonkar static void fadump_cpu_notes_buf_free(unsigned long vaddr, unsigned long size)
565ebaeb5aeSMahesh Salgaonkar {
566ebaeb5aeSMahesh Salgaonkar 	struct page *page;
567ebaeb5aeSMahesh Salgaonkar 	unsigned long order, count, i;
568ebaeb5aeSMahesh Salgaonkar 
569ebaeb5aeSMahesh Salgaonkar 	order = get_order(size);
570ebaeb5aeSMahesh Salgaonkar 	count = 1 << order;
571ebaeb5aeSMahesh Salgaonkar 	page = virt_to_page(vaddr);
572ebaeb5aeSMahesh Salgaonkar 	for (i = 0; i < count; i++)
573ebaeb5aeSMahesh Salgaonkar 		ClearPageReserved(page + i);
574ebaeb5aeSMahesh Salgaonkar 	__free_pages(page, order);
575ebaeb5aeSMahesh Salgaonkar }
576ebaeb5aeSMahesh Salgaonkar 
577ebaeb5aeSMahesh Salgaonkar /*
578ebaeb5aeSMahesh Salgaonkar  * Read CPU state dump data and convert it into ELF notes.
579ebaeb5aeSMahesh Salgaonkar  * The CPU dump starts with magic number "REGSAVE". NumCpusOffset should be
580ebaeb5aeSMahesh Salgaonkar  * used to access the data to allow for additional fields to be added without
581ebaeb5aeSMahesh Salgaonkar  * affecting compatibility. Each list of registers for a CPU starts with
582ebaeb5aeSMahesh Salgaonkar  * "CPUSTRT" and ends with "CPUEND". Each register entry is of 16 bytes,
583ebaeb5aeSMahesh Salgaonkar  * 8 Byte ASCII identifier and 8 Byte register value. The register entry
584ebaeb5aeSMahesh Salgaonkar  * with identifier "CPUSTRT" and "CPUEND" contains 4 byte cpu id as part
585ebaeb5aeSMahesh Salgaonkar  * of register value. For more details refer to PAPR document.
586ebaeb5aeSMahesh Salgaonkar  *
587ebaeb5aeSMahesh Salgaonkar  * Only for the crashing cpu we ignore the CPU dump data and get exact
588ebaeb5aeSMahesh Salgaonkar  * state from fadump crash info structure populated by first kernel at the
589ebaeb5aeSMahesh Salgaonkar  * time of crash.
590ebaeb5aeSMahesh Salgaonkar  */
591ebaeb5aeSMahesh Salgaonkar static int __init fadump_build_cpu_notes(const struct fadump_mem_struct *fdm)
592ebaeb5aeSMahesh Salgaonkar {
593ebaeb5aeSMahesh Salgaonkar 	struct fadump_reg_save_area_header *reg_header;
594ebaeb5aeSMahesh Salgaonkar 	struct fadump_reg_entry *reg_entry;
595ebaeb5aeSMahesh Salgaonkar 	struct fadump_crash_info_header *fdh = NULL;
596ebaeb5aeSMahesh Salgaonkar 	void *vaddr;
597ebaeb5aeSMahesh Salgaonkar 	unsigned long addr;
598ebaeb5aeSMahesh Salgaonkar 	u32 num_cpus, *note_buf;
599ebaeb5aeSMahesh Salgaonkar 	struct pt_regs regs;
600ebaeb5aeSMahesh Salgaonkar 	int i, rc = 0, cpu = 0;
601ebaeb5aeSMahesh Salgaonkar 
602ebaeb5aeSMahesh Salgaonkar 	if (!fdm->cpu_state_data.bytes_dumped)
603ebaeb5aeSMahesh Salgaonkar 		return -EINVAL;
604ebaeb5aeSMahesh Salgaonkar 
605ebaeb5aeSMahesh Salgaonkar 	addr = fdm->cpu_state_data.destination_address;
606ebaeb5aeSMahesh Salgaonkar 	vaddr = __va(addr);
607ebaeb5aeSMahesh Salgaonkar 
608ebaeb5aeSMahesh Salgaonkar 	reg_header = vaddr;
609ebaeb5aeSMahesh Salgaonkar 	if (reg_header->magic_number != REGSAVE_AREA_MAGIC) {
610ebaeb5aeSMahesh Salgaonkar 		printk(KERN_ERR "Unable to read register save area.\n");
611ebaeb5aeSMahesh Salgaonkar 		return -ENOENT;
612ebaeb5aeSMahesh Salgaonkar 	}
613ebaeb5aeSMahesh Salgaonkar 	pr_debug("--------CPU State Data------------\n");
614ebaeb5aeSMahesh Salgaonkar 	pr_debug("Magic Number: %llx\n", reg_header->magic_number);
615ebaeb5aeSMahesh Salgaonkar 	pr_debug("NumCpuOffset: %x\n", reg_header->num_cpu_offset);
616ebaeb5aeSMahesh Salgaonkar 
617ebaeb5aeSMahesh Salgaonkar 	vaddr += reg_header->num_cpu_offset;
618ebaeb5aeSMahesh Salgaonkar 	num_cpus = *((u32 *)(vaddr));
619ebaeb5aeSMahesh Salgaonkar 	pr_debug("NumCpus     : %u\n", num_cpus);
620ebaeb5aeSMahesh Salgaonkar 	vaddr += sizeof(u32);
621ebaeb5aeSMahesh Salgaonkar 	reg_entry = (struct fadump_reg_entry *)vaddr;
622ebaeb5aeSMahesh Salgaonkar 
623ebaeb5aeSMahesh Salgaonkar 	/* Allocate buffer to hold cpu crash notes. */
624ebaeb5aeSMahesh Salgaonkar 	fw_dump.cpu_notes_buf_size = num_cpus * sizeof(note_buf_t);
625ebaeb5aeSMahesh Salgaonkar 	fw_dump.cpu_notes_buf_size = PAGE_ALIGN(fw_dump.cpu_notes_buf_size);
626ebaeb5aeSMahesh Salgaonkar 	note_buf = fadump_cpu_notes_buf_alloc(fw_dump.cpu_notes_buf_size);
627ebaeb5aeSMahesh Salgaonkar 	if (!note_buf) {
628ebaeb5aeSMahesh Salgaonkar 		printk(KERN_ERR "Failed to allocate 0x%lx bytes for "
629ebaeb5aeSMahesh Salgaonkar 			"cpu notes buffer\n", fw_dump.cpu_notes_buf_size);
630ebaeb5aeSMahesh Salgaonkar 		return -ENOMEM;
631ebaeb5aeSMahesh Salgaonkar 	}
632ebaeb5aeSMahesh Salgaonkar 	fw_dump.cpu_notes_buf = __pa(note_buf);
633ebaeb5aeSMahesh Salgaonkar 
634ebaeb5aeSMahesh Salgaonkar 	pr_debug("Allocated buffer for cpu notes of size %ld at %p\n",
635ebaeb5aeSMahesh Salgaonkar 			(num_cpus * sizeof(note_buf_t)), note_buf);
636ebaeb5aeSMahesh Salgaonkar 
637ebaeb5aeSMahesh Salgaonkar 	if (fw_dump.fadumphdr_addr)
638ebaeb5aeSMahesh Salgaonkar 		fdh = __va(fw_dump.fadumphdr_addr);
639ebaeb5aeSMahesh Salgaonkar 
640ebaeb5aeSMahesh Salgaonkar 	for (i = 0; i < num_cpus; i++) {
641ebaeb5aeSMahesh Salgaonkar 		if (reg_entry->reg_id != REG_ID("CPUSTRT")) {
642ebaeb5aeSMahesh Salgaonkar 			printk(KERN_ERR "Unable to read CPU state data\n");
643ebaeb5aeSMahesh Salgaonkar 			rc = -ENOENT;
644ebaeb5aeSMahesh Salgaonkar 			goto error_out;
645ebaeb5aeSMahesh Salgaonkar 		}
646ebaeb5aeSMahesh Salgaonkar 		/* Lower 4 bytes of reg_value contains logical cpu id */
647ebaeb5aeSMahesh Salgaonkar 		cpu = reg_entry->reg_value & FADUMP_CPU_ID_MASK;
648ebaeb5aeSMahesh Salgaonkar 		if (!cpumask_test_cpu(cpu, &fdh->cpu_online_mask)) {
649ebaeb5aeSMahesh Salgaonkar 			SKIP_TO_NEXT_CPU(reg_entry);
650ebaeb5aeSMahesh Salgaonkar 			continue;
651ebaeb5aeSMahesh Salgaonkar 		}
652ebaeb5aeSMahesh Salgaonkar 		pr_debug("Reading register data for cpu %d...\n", cpu);
653ebaeb5aeSMahesh Salgaonkar 		if (fdh && fdh->crashing_cpu == cpu) {
654ebaeb5aeSMahesh Salgaonkar 			regs = fdh->regs;
655ebaeb5aeSMahesh Salgaonkar 			note_buf = fadump_regs_to_elf_notes(note_buf, &regs);
656ebaeb5aeSMahesh Salgaonkar 			SKIP_TO_NEXT_CPU(reg_entry);
657ebaeb5aeSMahesh Salgaonkar 		} else {
658ebaeb5aeSMahesh Salgaonkar 			reg_entry++;
659ebaeb5aeSMahesh Salgaonkar 			reg_entry = fadump_read_registers(reg_entry, &regs);
660ebaeb5aeSMahesh Salgaonkar 			note_buf = fadump_regs_to_elf_notes(note_buf, &regs);
661ebaeb5aeSMahesh Salgaonkar 		}
662ebaeb5aeSMahesh Salgaonkar 	}
663ebaeb5aeSMahesh Salgaonkar 	fadump_final_note(note_buf);
664ebaeb5aeSMahesh Salgaonkar 
665ebaeb5aeSMahesh Salgaonkar 	pr_debug("Updating elfcore header (%llx) with cpu notes\n",
666ebaeb5aeSMahesh Salgaonkar 							fdh->elfcorehdr_addr);
667ebaeb5aeSMahesh Salgaonkar 	fadump_update_elfcore_header((char *)__va(fdh->elfcorehdr_addr));
668ebaeb5aeSMahesh Salgaonkar 	return 0;
669ebaeb5aeSMahesh Salgaonkar 
670ebaeb5aeSMahesh Salgaonkar error_out:
671ebaeb5aeSMahesh Salgaonkar 	fadump_cpu_notes_buf_free((unsigned long)__va(fw_dump.cpu_notes_buf),
672ebaeb5aeSMahesh Salgaonkar 					fw_dump.cpu_notes_buf_size);
673ebaeb5aeSMahesh Salgaonkar 	fw_dump.cpu_notes_buf = 0;
674ebaeb5aeSMahesh Salgaonkar 	fw_dump.cpu_notes_buf_size = 0;
675ebaeb5aeSMahesh Salgaonkar 	return rc;
676ebaeb5aeSMahesh Salgaonkar 
677ebaeb5aeSMahesh Salgaonkar }
678ebaeb5aeSMahesh Salgaonkar 
6792df173d9SMahesh Salgaonkar /*
6802df173d9SMahesh Salgaonkar  * Validate and process the dump data stored by firmware before exporting
6812df173d9SMahesh Salgaonkar  * it through '/proc/vmcore'.
6822df173d9SMahesh Salgaonkar  */
6832df173d9SMahesh Salgaonkar static int __init process_fadump(const struct fadump_mem_struct *fdm_active)
6842df173d9SMahesh Salgaonkar {
6852df173d9SMahesh Salgaonkar 	struct fadump_crash_info_header *fdh;
686ebaeb5aeSMahesh Salgaonkar 	int rc = 0;
6872df173d9SMahesh Salgaonkar 
6882df173d9SMahesh Salgaonkar 	if (!fdm_active || !fw_dump.fadumphdr_addr)
6892df173d9SMahesh Salgaonkar 		return -EINVAL;
6902df173d9SMahesh Salgaonkar 
6912df173d9SMahesh Salgaonkar 	/* Check if the dump data is valid. */
6922df173d9SMahesh Salgaonkar 	if ((fdm_active->header.dump_status_flag == FADUMP_ERROR_FLAG) ||
693ebaeb5aeSMahesh Salgaonkar 			(fdm_active->cpu_state_data.error_flags != 0) ||
6942df173d9SMahesh Salgaonkar 			(fdm_active->rmr_region.error_flags != 0)) {
6952df173d9SMahesh Salgaonkar 		printk(KERN_ERR "Dump taken by platform is not valid\n");
6962df173d9SMahesh Salgaonkar 		return -EINVAL;
6972df173d9SMahesh Salgaonkar 	}
698ebaeb5aeSMahesh Salgaonkar 	if ((fdm_active->rmr_region.bytes_dumped !=
699ebaeb5aeSMahesh Salgaonkar 			fdm_active->rmr_region.source_len) ||
700ebaeb5aeSMahesh Salgaonkar 			!fdm_active->cpu_state_data.bytes_dumped) {
7012df173d9SMahesh Salgaonkar 		printk(KERN_ERR "Dump taken by platform is incomplete\n");
7022df173d9SMahesh Salgaonkar 		return -EINVAL;
7032df173d9SMahesh Salgaonkar 	}
7042df173d9SMahesh Salgaonkar 
7052df173d9SMahesh Salgaonkar 	/* Validate the fadump crash info header */
7062df173d9SMahesh Salgaonkar 	fdh = __va(fw_dump.fadumphdr_addr);
7072df173d9SMahesh Salgaonkar 	if (fdh->magic_number != FADUMP_CRASH_INFO_MAGIC) {
7082df173d9SMahesh Salgaonkar 		printk(KERN_ERR "Crash info header is not valid.\n");
7092df173d9SMahesh Salgaonkar 		return -EINVAL;
7102df173d9SMahesh Salgaonkar 	}
7112df173d9SMahesh Salgaonkar 
712ebaeb5aeSMahesh Salgaonkar 	rc = fadump_build_cpu_notes(fdm_active);
713ebaeb5aeSMahesh Salgaonkar 	if (rc)
714ebaeb5aeSMahesh Salgaonkar 		return rc;
715ebaeb5aeSMahesh Salgaonkar 
7162df173d9SMahesh Salgaonkar 	/*
7172df173d9SMahesh Salgaonkar 	 * We are done validating dump info and elfcore header is now ready
7182df173d9SMahesh Salgaonkar 	 * to be exported. set elfcorehdr_addr so that vmcore module will
7192df173d9SMahesh Salgaonkar 	 * export the elfcore header through '/proc/vmcore'.
7202df173d9SMahesh Salgaonkar 	 */
7212df173d9SMahesh Salgaonkar 	elfcorehdr_addr = fdh->elfcorehdr_addr;
7222df173d9SMahesh Salgaonkar 
7232df173d9SMahesh Salgaonkar 	return 0;
7242df173d9SMahesh Salgaonkar }
7252df173d9SMahesh Salgaonkar 
7262df173d9SMahesh Salgaonkar static inline void fadump_add_crash_memory(unsigned long long base,
7272df173d9SMahesh Salgaonkar 					unsigned long long end)
7282df173d9SMahesh Salgaonkar {
7292df173d9SMahesh Salgaonkar 	if (base == end)
7302df173d9SMahesh Salgaonkar 		return;
7312df173d9SMahesh Salgaonkar 
7322df173d9SMahesh Salgaonkar 	pr_debug("crash_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n",
7332df173d9SMahesh Salgaonkar 		crash_mem_ranges, base, end - 1, (end - base));
7342df173d9SMahesh Salgaonkar 	crash_memory_ranges[crash_mem_ranges].base = base;
7352df173d9SMahesh Salgaonkar 	crash_memory_ranges[crash_mem_ranges].size = end - base;
7362df173d9SMahesh Salgaonkar 	crash_mem_ranges++;
7372df173d9SMahesh Salgaonkar }
7382df173d9SMahesh Salgaonkar 
7392df173d9SMahesh Salgaonkar static void fadump_exclude_reserved_area(unsigned long long start,
7402df173d9SMahesh Salgaonkar 					unsigned long long end)
7412df173d9SMahesh Salgaonkar {
7422df173d9SMahesh Salgaonkar 	unsigned long long ra_start, ra_end;
7432df173d9SMahesh Salgaonkar 
7442df173d9SMahesh Salgaonkar 	ra_start = fw_dump.reserve_dump_area_start;
7452df173d9SMahesh Salgaonkar 	ra_end = ra_start + fw_dump.reserve_dump_area_size;
7462df173d9SMahesh Salgaonkar 
7472df173d9SMahesh Salgaonkar 	if ((ra_start < end) && (ra_end > start)) {
7482df173d9SMahesh Salgaonkar 		if ((start < ra_start) && (end > ra_end)) {
7492df173d9SMahesh Salgaonkar 			fadump_add_crash_memory(start, ra_start);
7502df173d9SMahesh Salgaonkar 			fadump_add_crash_memory(ra_end, end);
7512df173d9SMahesh Salgaonkar 		} else if (start < ra_start) {
7522df173d9SMahesh Salgaonkar 			fadump_add_crash_memory(start, ra_start);
7532df173d9SMahesh Salgaonkar 		} else if (ra_end < end) {
7542df173d9SMahesh Salgaonkar 			fadump_add_crash_memory(ra_end, end);
7552df173d9SMahesh Salgaonkar 		}
7562df173d9SMahesh Salgaonkar 	} else
7572df173d9SMahesh Salgaonkar 		fadump_add_crash_memory(start, end);
7582df173d9SMahesh Salgaonkar }
7592df173d9SMahesh Salgaonkar 
7602df173d9SMahesh Salgaonkar static int fadump_init_elfcore_header(char *bufp)
7612df173d9SMahesh Salgaonkar {
7622df173d9SMahesh Salgaonkar 	struct elfhdr *elf;
7632df173d9SMahesh Salgaonkar 
7642df173d9SMahesh Salgaonkar 	elf = (struct elfhdr *) bufp;
7652df173d9SMahesh Salgaonkar 	bufp += sizeof(struct elfhdr);
7662df173d9SMahesh Salgaonkar 	memcpy(elf->e_ident, ELFMAG, SELFMAG);
7672df173d9SMahesh Salgaonkar 	elf->e_ident[EI_CLASS] = ELF_CLASS;
7682df173d9SMahesh Salgaonkar 	elf->e_ident[EI_DATA] = ELF_DATA;
7692df173d9SMahesh Salgaonkar 	elf->e_ident[EI_VERSION] = EV_CURRENT;
7702df173d9SMahesh Salgaonkar 	elf->e_ident[EI_OSABI] = ELF_OSABI;
7712df173d9SMahesh Salgaonkar 	memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
7722df173d9SMahesh Salgaonkar 	elf->e_type = ET_CORE;
7732df173d9SMahesh Salgaonkar 	elf->e_machine = ELF_ARCH;
7742df173d9SMahesh Salgaonkar 	elf->e_version = EV_CURRENT;
7752df173d9SMahesh Salgaonkar 	elf->e_entry = 0;
7762df173d9SMahesh Salgaonkar 	elf->e_phoff = sizeof(struct elfhdr);
7772df173d9SMahesh Salgaonkar 	elf->e_shoff = 0;
7782df173d9SMahesh Salgaonkar 	elf->e_flags = ELF_CORE_EFLAGS;
7792df173d9SMahesh Salgaonkar 	elf->e_ehsize = sizeof(struct elfhdr);
7802df173d9SMahesh Salgaonkar 	elf->e_phentsize = sizeof(struct elf_phdr);
7812df173d9SMahesh Salgaonkar 	elf->e_phnum = 0;
7822df173d9SMahesh Salgaonkar 	elf->e_shentsize = 0;
7832df173d9SMahesh Salgaonkar 	elf->e_shnum = 0;
7842df173d9SMahesh Salgaonkar 	elf->e_shstrndx = 0;
7852df173d9SMahesh Salgaonkar 
7862df173d9SMahesh Salgaonkar 	return 0;
7872df173d9SMahesh Salgaonkar }
7882df173d9SMahesh Salgaonkar 
7892df173d9SMahesh Salgaonkar /*
7902df173d9SMahesh Salgaonkar  * Traverse through memblock structure and setup crash memory ranges. These
7912df173d9SMahesh Salgaonkar  * ranges will be used create PT_LOAD program headers in elfcore header.
7922df173d9SMahesh Salgaonkar  */
7932df173d9SMahesh Salgaonkar static void fadump_setup_crash_memory_ranges(void)
7942df173d9SMahesh Salgaonkar {
7952df173d9SMahesh Salgaonkar 	struct memblock_region *reg;
7962df173d9SMahesh Salgaonkar 	unsigned long long start, end;
7972df173d9SMahesh Salgaonkar 
7982df173d9SMahesh Salgaonkar 	pr_debug("Setup crash memory ranges.\n");
7992df173d9SMahesh Salgaonkar 	crash_mem_ranges = 0;
8002df173d9SMahesh Salgaonkar 	/*
8012df173d9SMahesh Salgaonkar 	 * add the first memory chunk (RMA_START through boot_memory_size) as
8022df173d9SMahesh Salgaonkar 	 * a separate memory chunk. The reason is, at the time crash firmware
8032df173d9SMahesh Salgaonkar 	 * will move the content of this memory chunk to different location
8042df173d9SMahesh Salgaonkar 	 * specified during fadump registration. We need to create a separate
8052df173d9SMahesh Salgaonkar 	 * program header for this chunk with the correct offset.
8062df173d9SMahesh Salgaonkar 	 */
8072df173d9SMahesh Salgaonkar 	fadump_add_crash_memory(RMA_START, fw_dump.boot_memory_size);
8082df173d9SMahesh Salgaonkar 
8092df173d9SMahesh Salgaonkar 	for_each_memblock(memory, reg) {
8102df173d9SMahesh Salgaonkar 		start = (unsigned long long)reg->base;
8112df173d9SMahesh Salgaonkar 		end = start + (unsigned long long)reg->size;
8122df173d9SMahesh Salgaonkar 		if (start == RMA_START && end >= fw_dump.boot_memory_size)
8132df173d9SMahesh Salgaonkar 			start = fw_dump.boot_memory_size;
8142df173d9SMahesh Salgaonkar 
8152df173d9SMahesh Salgaonkar 		/* add this range excluding the reserved dump area. */
8162df173d9SMahesh Salgaonkar 		fadump_exclude_reserved_area(start, end);
8172df173d9SMahesh Salgaonkar 	}
8182df173d9SMahesh Salgaonkar }
8192df173d9SMahesh Salgaonkar 
820d34c5f26SMahesh Salgaonkar /*
821d34c5f26SMahesh Salgaonkar  * If the given physical address falls within the boot memory region then
822d34c5f26SMahesh Salgaonkar  * return the relocated address that points to the dump region reserved
823d34c5f26SMahesh Salgaonkar  * for saving initial boot memory contents.
824d34c5f26SMahesh Salgaonkar  */
825d34c5f26SMahesh Salgaonkar static inline unsigned long fadump_relocate(unsigned long paddr)
826d34c5f26SMahesh Salgaonkar {
827d34c5f26SMahesh Salgaonkar 	if (paddr > RMA_START && paddr < fw_dump.boot_memory_size)
828d34c5f26SMahesh Salgaonkar 		return fdm.rmr_region.destination_address + paddr;
829d34c5f26SMahesh Salgaonkar 	else
830d34c5f26SMahesh Salgaonkar 		return paddr;
831d34c5f26SMahesh Salgaonkar }
832d34c5f26SMahesh Salgaonkar 
8332df173d9SMahesh Salgaonkar static int fadump_create_elfcore_headers(char *bufp)
8342df173d9SMahesh Salgaonkar {
8352df173d9SMahesh Salgaonkar 	struct elfhdr *elf;
8362df173d9SMahesh Salgaonkar 	struct elf_phdr *phdr;
8372df173d9SMahesh Salgaonkar 	int i;
8382df173d9SMahesh Salgaonkar 
8392df173d9SMahesh Salgaonkar 	fadump_init_elfcore_header(bufp);
8402df173d9SMahesh Salgaonkar 	elf = (struct elfhdr *)bufp;
8412df173d9SMahesh Salgaonkar 	bufp += sizeof(struct elfhdr);
8422df173d9SMahesh Salgaonkar 
843ebaeb5aeSMahesh Salgaonkar 	/*
844ebaeb5aeSMahesh Salgaonkar 	 * setup ELF PT_NOTE, place holder for cpu notes info. The notes info
845ebaeb5aeSMahesh Salgaonkar 	 * will be populated during second kernel boot after crash. Hence
846ebaeb5aeSMahesh Salgaonkar 	 * this PT_NOTE will always be the first elf note.
847ebaeb5aeSMahesh Salgaonkar 	 *
848ebaeb5aeSMahesh Salgaonkar 	 * NOTE: Any new ELF note addition should be placed after this note.
849ebaeb5aeSMahesh Salgaonkar 	 */
850ebaeb5aeSMahesh Salgaonkar 	phdr = (struct elf_phdr *)bufp;
851ebaeb5aeSMahesh Salgaonkar 	bufp += sizeof(struct elf_phdr);
852ebaeb5aeSMahesh Salgaonkar 	phdr->p_type = PT_NOTE;
853ebaeb5aeSMahesh Salgaonkar 	phdr->p_flags = 0;
854ebaeb5aeSMahesh Salgaonkar 	phdr->p_vaddr = 0;
855ebaeb5aeSMahesh Salgaonkar 	phdr->p_align = 0;
856ebaeb5aeSMahesh Salgaonkar 
857ebaeb5aeSMahesh Salgaonkar 	phdr->p_offset = 0;
858ebaeb5aeSMahesh Salgaonkar 	phdr->p_paddr = 0;
859ebaeb5aeSMahesh Salgaonkar 	phdr->p_filesz = 0;
860ebaeb5aeSMahesh Salgaonkar 	phdr->p_memsz = 0;
861ebaeb5aeSMahesh Salgaonkar 
862ebaeb5aeSMahesh Salgaonkar 	(elf->e_phnum)++;
863ebaeb5aeSMahesh Salgaonkar 
864d34c5f26SMahesh Salgaonkar 	/* setup ELF PT_NOTE for vmcoreinfo */
865d34c5f26SMahesh Salgaonkar 	phdr = (struct elf_phdr *)bufp;
866d34c5f26SMahesh Salgaonkar 	bufp += sizeof(struct elf_phdr);
867d34c5f26SMahesh Salgaonkar 	phdr->p_type	= PT_NOTE;
868d34c5f26SMahesh Salgaonkar 	phdr->p_flags	= 0;
869d34c5f26SMahesh Salgaonkar 	phdr->p_vaddr	= 0;
870d34c5f26SMahesh Salgaonkar 	phdr->p_align	= 0;
871d34c5f26SMahesh Salgaonkar 
872d34c5f26SMahesh Salgaonkar 	phdr->p_paddr	= fadump_relocate(paddr_vmcoreinfo_note());
873d34c5f26SMahesh Salgaonkar 	phdr->p_offset	= phdr->p_paddr;
874d34c5f26SMahesh Salgaonkar 	phdr->p_memsz	= vmcoreinfo_max_size;
875d34c5f26SMahesh Salgaonkar 	phdr->p_filesz	= vmcoreinfo_max_size;
876d34c5f26SMahesh Salgaonkar 
877d34c5f26SMahesh Salgaonkar 	/* Increment number of program headers. */
878d34c5f26SMahesh Salgaonkar 	(elf->e_phnum)++;
879d34c5f26SMahesh Salgaonkar 
8802df173d9SMahesh Salgaonkar 	/* setup PT_LOAD sections. */
8812df173d9SMahesh Salgaonkar 
8822df173d9SMahesh Salgaonkar 	for (i = 0; i < crash_mem_ranges; i++) {
8832df173d9SMahesh Salgaonkar 		unsigned long long mbase, msize;
8842df173d9SMahesh Salgaonkar 		mbase = crash_memory_ranges[i].base;
8852df173d9SMahesh Salgaonkar 		msize = crash_memory_ranges[i].size;
8862df173d9SMahesh Salgaonkar 
8872df173d9SMahesh Salgaonkar 		if (!msize)
8882df173d9SMahesh Salgaonkar 			continue;
8892df173d9SMahesh Salgaonkar 
8902df173d9SMahesh Salgaonkar 		phdr = (struct elf_phdr *)bufp;
8912df173d9SMahesh Salgaonkar 		bufp += sizeof(struct elf_phdr);
8922df173d9SMahesh Salgaonkar 		phdr->p_type	= PT_LOAD;
8932df173d9SMahesh Salgaonkar 		phdr->p_flags	= PF_R|PF_W|PF_X;
8942df173d9SMahesh Salgaonkar 		phdr->p_offset	= mbase;
8952df173d9SMahesh Salgaonkar 
8962df173d9SMahesh Salgaonkar 		if (mbase == RMA_START) {
8972df173d9SMahesh Salgaonkar 			/*
8982df173d9SMahesh Salgaonkar 			 * The entire RMA region will be moved by firmware
8992df173d9SMahesh Salgaonkar 			 * to the specified destination_address. Hence set
9002df173d9SMahesh Salgaonkar 			 * the correct offset.
9012df173d9SMahesh Salgaonkar 			 */
9022df173d9SMahesh Salgaonkar 			phdr->p_offset = fdm.rmr_region.destination_address;
9032df173d9SMahesh Salgaonkar 		}
9042df173d9SMahesh Salgaonkar 
9052df173d9SMahesh Salgaonkar 		phdr->p_paddr = mbase;
9062df173d9SMahesh Salgaonkar 		phdr->p_vaddr = (unsigned long)__va(mbase);
9072df173d9SMahesh Salgaonkar 		phdr->p_filesz = msize;
9082df173d9SMahesh Salgaonkar 		phdr->p_memsz = msize;
9092df173d9SMahesh Salgaonkar 		phdr->p_align = 0;
9102df173d9SMahesh Salgaonkar 
9112df173d9SMahesh Salgaonkar 		/* Increment number of program headers. */
9122df173d9SMahesh Salgaonkar 		(elf->e_phnum)++;
9132df173d9SMahesh Salgaonkar 	}
9142df173d9SMahesh Salgaonkar 	return 0;
9152df173d9SMahesh Salgaonkar }
9162df173d9SMahesh Salgaonkar 
9172df173d9SMahesh Salgaonkar static unsigned long init_fadump_header(unsigned long addr)
9182df173d9SMahesh Salgaonkar {
9192df173d9SMahesh Salgaonkar 	struct fadump_crash_info_header *fdh;
9202df173d9SMahesh Salgaonkar 
9212df173d9SMahesh Salgaonkar 	if (!addr)
9222df173d9SMahesh Salgaonkar 		return 0;
9232df173d9SMahesh Salgaonkar 
9242df173d9SMahesh Salgaonkar 	fw_dump.fadumphdr_addr = addr;
9252df173d9SMahesh Salgaonkar 	fdh = __va(addr);
9262df173d9SMahesh Salgaonkar 	addr += sizeof(struct fadump_crash_info_header);
9272df173d9SMahesh Salgaonkar 
9282df173d9SMahesh Salgaonkar 	memset(fdh, 0, sizeof(struct fadump_crash_info_header));
9292df173d9SMahesh Salgaonkar 	fdh->magic_number = FADUMP_CRASH_INFO_MAGIC;
9302df173d9SMahesh Salgaonkar 	fdh->elfcorehdr_addr = addr;
931ebaeb5aeSMahesh Salgaonkar 	/* We will set the crashing cpu id in crash_fadump() during crash. */
932ebaeb5aeSMahesh Salgaonkar 	fdh->crashing_cpu = CPU_UNKNOWN;
9332df173d9SMahesh Salgaonkar 
9342df173d9SMahesh Salgaonkar 	return addr;
9352df173d9SMahesh Salgaonkar }
9362df173d9SMahesh Salgaonkar 
9373ccc00a7SMahesh Salgaonkar static void register_fadump(void)
9383ccc00a7SMahesh Salgaonkar {
9392df173d9SMahesh Salgaonkar 	unsigned long addr;
9402df173d9SMahesh Salgaonkar 	void *vaddr;
9412df173d9SMahesh Salgaonkar 
9423ccc00a7SMahesh Salgaonkar 	/*
9433ccc00a7SMahesh Salgaonkar 	 * If no memory is reserved then we can not register for firmware-
9443ccc00a7SMahesh Salgaonkar 	 * assisted dump.
9453ccc00a7SMahesh Salgaonkar 	 */
9463ccc00a7SMahesh Salgaonkar 	if (!fw_dump.reserve_dump_area_size)
9473ccc00a7SMahesh Salgaonkar 		return;
9483ccc00a7SMahesh Salgaonkar 
9492df173d9SMahesh Salgaonkar 	fadump_setup_crash_memory_ranges();
9502df173d9SMahesh Salgaonkar 
9512df173d9SMahesh Salgaonkar 	addr = fdm.rmr_region.destination_address + fdm.rmr_region.source_len;
9522df173d9SMahesh Salgaonkar 	/* Initialize fadump crash info header. */
9532df173d9SMahesh Salgaonkar 	addr = init_fadump_header(addr);
9542df173d9SMahesh Salgaonkar 	vaddr = __va(addr);
9552df173d9SMahesh Salgaonkar 
9562df173d9SMahesh Salgaonkar 	pr_debug("Creating ELF core headers at %#016lx\n", addr);
9572df173d9SMahesh Salgaonkar 	fadump_create_elfcore_headers(vaddr);
9582df173d9SMahesh Salgaonkar 
9593ccc00a7SMahesh Salgaonkar 	/* register the future kernel dump with firmware. */
9603ccc00a7SMahesh Salgaonkar 	register_fw_dump(&fdm);
9613ccc00a7SMahesh Salgaonkar }
9623ccc00a7SMahesh Salgaonkar 
9633ccc00a7SMahesh Salgaonkar static int fadump_unregister_dump(struct fadump_mem_struct *fdm)
9643ccc00a7SMahesh Salgaonkar {
9653ccc00a7SMahesh Salgaonkar 	int rc = 0;
9663ccc00a7SMahesh Salgaonkar 	unsigned int wait_time;
9673ccc00a7SMahesh Salgaonkar 
9683ccc00a7SMahesh Salgaonkar 	pr_debug("Un-register firmware-assisted dump\n");
9693ccc00a7SMahesh Salgaonkar 
9703ccc00a7SMahesh Salgaonkar 	/* TODO: Add upper time limit for the delay */
9713ccc00a7SMahesh Salgaonkar 	do {
9723ccc00a7SMahesh Salgaonkar 		rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
9733ccc00a7SMahesh Salgaonkar 			FADUMP_UNREGISTER, fdm,
9743ccc00a7SMahesh Salgaonkar 			sizeof(struct fadump_mem_struct));
9753ccc00a7SMahesh Salgaonkar 
9763ccc00a7SMahesh Salgaonkar 		wait_time = rtas_busy_delay_time(rc);
9773ccc00a7SMahesh Salgaonkar 		if (wait_time)
9783ccc00a7SMahesh Salgaonkar 			mdelay(wait_time);
9793ccc00a7SMahesh Salgaonkar 	} while (wait_time);
9803ccc00a7SMahesh Salgaonkar 
9813ccc00a7SMahesh Salgaonkar 	if (rc) {
9823ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "Failed to un-register firmware-assisted dump."
9833ccc00a7SMahesh Salgaonkar 			" unexpected error(%d).\n", rc);
9843ccc00a7SMahesh Salgaonkar 		return rc;
9853ccc00a7SMahesh Salgaonkar 	}
9863ccc00a7SMahesh Salgaonkar 	fw_dump.dump_registered = 0;
9873ccc00a7SMahesh Salgaonkar 	return 0;
9883ccc00a7SMahesh Salgaonkar }
9893ccc00a7SMahesh Salgaonkar 
990b500afffSMahesh Salgaonkar static int fadump_invalidate_dump(struct fadump_mem_struct *fdm)
991b500afffSMahesh Salgaonkar {
992b500afffSMahesh Salgaonkar 	int rc = 0;
993b500afffSMahesh Salgaonkar 	unsigned int wait_time;
994b500afffSMahesh Salgaonkar 
995b500afffSMahesh Salgaonkar 	pr_debug("Invalidating firmware-assisted dump registration\n");
996b500afffSMahesh Salgaonkar 
997b500afffSMahesh Salgaonkar 	/* TODO: Add upper time limit for the delay */
998b500afffSMahesh Salgaonkar 	do {
999b500afffSMahesh Salgaonkar 		rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
1000b500afffSMahesh Salgaonkar 			FADUMP_INVALIDATE, fdm,
1001b500afffSMahesh Salgaonkar 			sizeof(struct fadump_mem_struct));
1002b500afffSMahesh Salgaonkar 
1003b500afffSMahesh Salgaonkar 		wait_time = rtas_busy_delay_time(rc);
1004b500afffSMahesh Salgaonkar 		if (wait_time)
1005b500afffSMahesh Salgaonkar 			mdelay(wait_time);
1006b500afffSMahesh Salgaonkar 	} while (wait_time);
1007b500afffSMahesh Salgaonkar 
1008b500afffSMahesh Salgaonkar 	if (rc) {
1009b500afffSMahesh Salgaonkar 		printk(KERN_ERR "Failed to invalidate firmware-assisted dump "
1010b500afffSMahesh Salgaonkar 			"rgistration. unexpected error(%d).\n", rc);
1011b500afffSMahesh Salgaonkar 		return rc;
1012b500afffSMahesh Salgaonkar 	}
1013b500afffSMahesh Salgaonkar 	fw_dump.dump_active = 0;
1014b500afffSMahesh Salgaonkar 	fdm_active = NULL;
1015b500afffSMahesh Salgaonkar 	return 0;
1016b500afffSMahesh Salgaonkar }
1017b500afffSMahesh Salgaonkar 
1018b500afffSMahesh Salgaonkar void fadump_cleanup(void)
1019b500afffSMahesh Salgaonkar {
1020b500afffSMahesh Salgaonkar 	/* Invalidate the registration only if dump is active. */
1021b500afffSMahesh Salgaonkar 	if (fw_dump.dump_active) {
1022b500afffSMahesh Salgaonkar 		init_fadump_mem_struct(&fdm,
1023b500afffSMahesh Salgaonkar 			fdm_active->cpu_state_data.destination_address);
1024b500afffSMahesh Salgaonkar 		fadump_invalidate_dump(&fdm);
1025b500afffSMahesh Salgaonkar 	}
1026b500afffSMahesh Salgaonkar }
1027b500afffSMahesh Salgaonkar 
1028b500afffSMahesh Salgaonkar /*
1029b500afffSMahesh Salgaonkar  * Release the memory that was reserved in early boot to preserve the memory
1030b500afffSMahesh Salgaonkar  * contents. The released memory will be available for general use.
1031b500afffSMahesh Salgaonkar  */
1032b500afffSMahesh Salgaonkar static void fadump_release_memory(unsigned long begin, unsigned long end)
1033b500afffSMahesh Salgaonkar {
1034b500afffSMahesh Salgaonkar 	unsigned long addr;
1035b500afffSMahesh Salgaonkar 	unsigned long ra_start, ra_end;
1036b500afffSMahesh Salgaonkar 
1037b500afffSMahesh Salgaonkar 	ra_start = fw_dump.reserve_dump_area_start;
1038b500afffSMahesh Salgaonkar 	ra_end = ra_start + fw_dump.reserve_dump_area_size;
1039b500afffSMahesh Salgaonkar 
1040b500afffSMahesh Salgaonkar 	for (addr = begin; addr < end; addr += PAGE_SIZE) {
1041b500afffSMahesh Salgaonkar 		/*
1042b500afffSMahesh Salgaonkar 		 * exclude the dump reserve area. Will reuse it for next
1043b500afffSMahesh Salgaonkar 		 * fadump registration.
1044b500afffSMahesh Salgaonkar 		 */
1045b500afffSMahesh Salgaonkar 		if (addr <= ra_end && ((addr + PAGE_SIZE) > ra_start))
1046b500afffSMahesh Salgaonkar 			continue;
1047b500afffSMahesh Salgaonkar 
1048b500afffSMahesh Salgaonkar 		ClearPageReserved(pfn_to_page(addr >> PAGE_SHIFT));
1049b500afffSMahesh Salgaonkar 		init_page_count(pfn_to_page(addr >> PAGE_SHIFT));
1050b500afffSMahesh Salgaonkar 		free_page((unsigned long)__va(addr));
1051b500afffSMahesh Salgaonkar 		totalram_pages++;
1052b500afffSMahesh Salgaonkar 	}
1053b500afffSMahesh Salgaonkar }
1054b500afffSMahesh Salgaonkar 
1055b500afffSMahesh Salgaonkar static void fadump_invalidate_release_mem(void)
1056b500afffSMahesh Salgaonkar {
1057b500afffSMahesh Salgaonkar 	unsigned long reserved_area_start, reserved_area_end;
1058b500afffSMahesh Salgaonkar 	unsigned long destination_address;
1059b500afffSMahesh Salgaonkar 
1060b500afffSMahesh Salgaonkar 	mutex_lock(&fadump_mutex);
1061b500afffSMahesh Salgaonkar 	if (!fw_dump.dump_active) {
1062b500afffSMahesh Salgaonkar 		mutex_unlock(&fadump_mutex);
1063b500afffSMahesh Salgaonkar 		return;
1064b500afffSMahesh Salgaonkar 	}
1065b500afffSMahesh Salgaonkar 
1066b500afffSMahesh Salgaonkar 	destination_address = fdm_active->cpu_state_data.destination_address;
1067b500afffSMahesh Salgaonkar 	fadump_cleanup();
1068b500afffSMahesh Salgaonkar 	mutex_unlock(&fadump_mutex);
1069b500afffSMahesh Salgaonkar 
1070b500afffSMahesh Salgaonkar 	/*
1071b500afffSMahesh Salgaonkar 	 * Save the current reserved memory bounds we will require them
1072b500afffSMahesh Salgaonkar 	 * later for releasing the memory for general use.
1073b500afffSMahesh Salgaonkar 	 */
1074b500afffSMahesh Salgaonkar 	reserved_area_start = fw_dump.reserve_dump_area_start;
1075b500afffSMahesh Salgaonkar 	reserved_area_end = reserved_area_start +
1076b500afffSMahesh Salgaonkar 			fw_dump.reserve_dump_area_size;
1077b500afffSMahesh Salgaonkar 	/*
1078b500afffSMahesh Salgaonkar 	 * Setup reserve_dump_area_start and its size so that we can
1079b500afffSMahesh Salgaonkar 	 * reuse this reserved memory for Re-registration.
1080b500afffSMahesh Salgaonkar 	 */
1081b500afffSMahesh Salgaonkar 	fw_dump.reserve_dump_area_start = destination_address;
1082b500afffSMahesh Salgaonkar 	fw_dump.reserve_dump_area_size = get_fadump_area_size();
1083b500afffSMahesh Salgaonkar 
1084b500afffSMahesh Salgaonkar 	fadump_release_memory(reserved_area_start, reserved_area_end);
1085b500afffSMahesh Salgaonkar 	if (fw_dump.cpu_notes_buf) {
1086b500afffSMahesh Salgaonkar 		fadump_cpu_notes_buf_free(
1087b500afffSMahesh Salgaonkar 				(unsigned long)__va(fw_dump.cpu_notes_buf),
1088b500afffSMahesh Salgaonkar 				fw_dump.cpu_notes_buf_size);
1089b500afffSMahesh Salgaonkar 		fw_dump.cpu_notes_buf = 0;
1090b500afffSMahesh Salgaonkar 		fw_dump.cpu_notes_buf_size = 0;
1091b500afffSMahesh Salgaonkar 	}
1092b500afffSMahesh Salgaonkar 	/* Initialize the kernel dump memory structure for FAD registration. */
1093b500afffSMahesh Salgaonkar 	init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
1094b500afffSMahesh Salgaonkar }
1095b500afffSMahesh Salgaonkar 
1096b500afffSMahesh Salgaonkar static ssize_t fadump_release_memory_store(struct kobject *kobj,
1097b500afffSMahesh Salgaonkar 					struct kobj_attribute *attr,
1098b500afffSMahesh Salgaonkar 					const char *buf, size_t count)
1099b500afffSMahesh Salgaonkar {
1100b500afffSMahesh Salgaonkar 	if (!fw_dump.dump_active)
1101b500afffSMahesh Salgaonkar 		return -EPERM;
1102b500afffSMahesh Salgaonkar 
1103b500afffSMahesh Salgaonkar 	if (buf[0] == '1') {
1104b500afffSMahesh Salgaonkar 		/*
1105b500afffSMahesh Salgaonkar 		 * Take away the '/proc/vmcore'. We are releasing the dump
1106b500afffSMahesh Salgaonkar 		 * memory, hence it will not be valid anymore.
1107b500afffSMahesh Salgaonkar 		 */
1108b500afffSMahesh Salgaonkar 		vmcore_cleanup();
1109b500afffSMahesh Salgaonkar 		fadump_invalidate_release_mem();
1110b500afffSMahesh Salgaonkar 
1111b500afffSMahesh Salgaonkar 	} else
1112b500afffSMahesh Salgaonkar 		return -EINVAL;
1113b500afffSMahesh Salgaonkar 	return count;
1114b500afffSMahesh Salgaonkar }
1115b500afffSMahesh Salgaonkar 
11163ccc00a7SMahesh Salgaonkar static ssize_t fadump_enabled_show(struct kobject *kobj,
11173ccc00a7SMahesh Salgaonkar 					struct kobj_attribute *attr,
11183ccc00a7SMahesh Salgaonkar 					char *buf)
11193ccc00a7SMahesh Salgaonkar {
11203ccc00a7SMahesh Salgaonkar 	return sprintf(buf, "%d\n", fw_dump.fadump_enabled);
11213ccc00a7SMahesh Salgaonkar }
11223ccc00a7SMahesh Salgaonkar 
11233ccc00a7SMahesh Salgaonkar static ssize_t fadump_register_show(struct kobject *kobj,
11243ccc00a7SMahesh Salgaonkar 					struct kobj_attribute *attr,
11253ccc00a7SMahesh Salgaonkar 					char *buf)
11263ccc00a7SMahesh Salgaonkar {
11273ccc00a7SMahesh Salgaonkar 	return sprintf(buf, "%d\n", fw_dump.dump_registered);
11283ccc00a7SMahesh Salgaonkar }
11293ccc00a7SMahesh Salgaonkar 
11303ccc00a7SMahesh Salgaonkar static ssize_t fadump_register_store(struct kobject *kobj,
11313ccc00a7SMahesh Salgaonkar 					struct kobj_attribute *attr,
11323ccc00a7SMahesh Salgaonkar 					const char *buf, size_t count)
11333ccc00a7SMahesh Salgaonkar {
11343ccc00a7SMahesh Salgaonkar 	int ret = 0;
11353ccc00a7SMahesh Salgaonkar 
11363ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_enabled || fdm_active)
11373ccc00a7SMahesh Salgaonkar 		return -EPERM;
11383ccc00a7SMahesh Salgaonkar 
11393ccc00a7SMahesh Salgaonkar 	mutex_lock(&fadump_mutex);
11403ccc00a7SMahesh Salgaonkar 
11413ccc00a7SMahesh Salgaonkar 	switch (buf[0]) {
11423ccc00a7SMahesh Salgaonkar 	case '0':
11433ccc00a7SMahesh Salgaonkar 		if (fw_dump.dump_registered == 0) {
11443ccc00a7SMahesh Salgaonkar 			ret = -EINVAL;
11453ccc00a7SMahesh Salgaonkar 			goto unlock_out;
11463ccc00a7SMahesh Salgaonkar 		}
11473ccc00a7SMahesh Salgaonkar 		/* Un-register Firmware-assisted dump */
11483ccc00a7SMahesh Salgaonkar 		fadump_unregister_dump(&fdm);
11493ccc00a7SMahesh Salgaonkar 		break;
11503ccc00a7SMahesh Salgaonkar 	case '1':
11513ccc00a7SMahesh Salgaonkar 		if (fw_dump.dump_registered == 1) {
11523ccc00a7SMahesh Salgaonkar 			ret = -EINVAL;
11533ccc00a7SMahesh Salgaonkar 			goto unlock_out;
11543ccc00a7SMahesh Salgaonkar 		}
11553ccc00a7SMahesh Salgaonkar 		/* Register Firmware-assisted dump */
11563ccc00a7SMahesh Salgaonkar 		register_fadump();
11573ccc00a7SMahesh Salgaonkar 		break;
11583ccc00a7SMahesh Salgaonkar 	default:
11593ccc00a7SMahesh Salgaonkar 		ret = -EINVAL;
11603ccc00a7SMahesh Salgaonkar 		break;
11613ccc00a7SMahesh Salgaonkar 	}
11623ccc00a7SMahesh Salgaonkar 
11633ccc00a7SMahesh Salgaonkar unlock_out:
11643ccc00a7SMahesh Salgaonkar 	mutex_unlock(&fadump_mutex);
11653ccc00a7SMahesh Salgaonkar 	return ret < 0 ? ret : count;
11663ccc00a7SMahesh Salgaonkar }
11673ccc00a7SMahesh Salgaonkar 
11683ccc00a7SMahesh Salgaonkar static int fadump_region_show(struct seq_file *m, void *private)
11693ccc00a7SMahesh Salgaonkar {
11703ccc00a7SMahesh Salgaonkar 	const struct fadump_mem_struct *fdm_ptr;
11713ccc00a7SMahesh Salgaonkar 
11723ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_enabled)
11733ccc00a7SMahesh Salgaonkar 		return 0;
11743ccc00a7SMahesh Salgaonkar 
1175b500afffSMahesh Salgaonkar 	mutex_lock(&fadump_mutex);
11763ccc00a7SMahesh Salgaonkar 	if (fdm_active)
11773ccc00a7SMahesh Salgaonkar 		fdm_ptr = fdm_active;
1178b500afffSMahesh Salgaonkar 	else {
1179b500afffSMahesh Salgaonkar 		mutex_unlock(&fadump_mutex);
11803ccc00a7SMahesh Salgaonkar 		fdm_ptr = &fdm;
1181b500afffSMahesh Salgaonkar 	}
11823ccc00a7SMahesh Salgaonkar 
11833ccc00a7SMahesh Salgaonkar 	seq_printf(m,
11843ccc00a7SMahesh Salgaonkar 			"CPU : [%#016llx-%#016llx] %#llx bytes, "
11853ccc00a7SMahesh Salgaonkar 			"Dumped: %#llx\n",
11863ccc00a7SMahesh Salgaonkar 			fdm_ptr->cpu_state_data.destination_address,
11873ccc00a7SMahesh Salgaonkar 			fdm_ptr->cpu_state_data.destination_address +
11883ccc00a7SMahesh Salgaonkar 			fdm_ptr->cpu_state_data.source_len - 1,
11893ccc00a7SMahesh Salgaonkar 			fdm_ptr->cpu_state_data.source_len,
11903ccc00a7SMahesh Salgaonkar 			fdm_ptr->cpu_state_data.bytes_dumped);
11913ccc00a7SMahesh Salgaonkar 	seq_printf(m,
11923ccc00a7SMahesh Salgaonkar 			"HPTE: [%#016llx-%#016llx] %#llx bytes, "
11933ccc00a7SMahesh Salgaonkar 			"Dumped: %#llx\n",
11943ccc00a7SMahesh Salgaonkar 			fdm_ptr->hpte_region.destination_address,
11953ccc00a7SMahesh Salgaonkar 			fdm_ptr->hpte_region.destination_address +
11963ccc00a7SMahesh Salgaonkar 			fdm_ptr->hpte_region.source_len - 1,
11973ccc00a7SMahesh Salgaonkar 			fdm_ptr->hpte_region.source_len,
11983ccc00a7SMahesh Salgaonkar 			fdm_ptr->hpte_region.bytes_dumped);
11993ccc00a7SMahesh Salgaonkar 	seq_printf(m,
12003ccc00a7SMahesh Salgaonkar 			"DUMP: [%#016llx-%#016llx] %#llx bytes, "
12013ccc00a7SMahesh Salgaonkar 			"Dumped: %#llx\n",
12023ccc00a7SMahesh Salgaonkar 			fdm_ptr->rmr_region.destination_address,
12033ccc00a7SMahesh Salgaonkar 			fdm_ptr->rmr_region.destination_address +
12043ccc00a7SMahesh Salgaonkar 			fdm_ptr->rmr_region.source_len - 1,
12053ccc00a7SMahesh Salgaonkar 			fdm_ptr->rmr_region.source_len,
12063ccc00a7SMahesh Salgaonkar 			fdm_ptr->rmr_region.bytes_dumped);
12073ccc00a7SMahesh Salgaonkar 
12083ccc00a7SMahesh Salgaonkar 	if (!fdm_active ||
12093ccc00a7SMahesh Salgaonkar 		(fw_dump.reserve_dump_area_start ==
12103ccc00a7SMahesh Salgaonkar 		fdm_ptr->cpu_state_data.destination_address))
1211b500afffSMahesh Salgaonkar 		goto out;
12123ccc00a7SMahesh Salgaonkar 
12133ccc00a7SMahesh Salgaonkar 	/* Dump is active. Show reserved memory region. */
12143ccc00a7SMahesh Salgaonkar 	seq_printf(m,
12153ccc00a7SMahesh Salgaonkar 			"    : [%#016llx-%#016llx] %#llx bytes, "
12163ccc00a7SMahesh Salgaonkar 			"Dumped: %#llx\n",
12173ccc00a7SMahesh Salgaonkar 			(unsigned long long)fw_dump.reserve_dump_area_start,
12183ccc00a7SMahesh Salgaonkar 			fdm_ptr->cpu_state_data.destination_address - 1,
12193ccc00a7SMahesh Salgaonkar 			fdm_ptr->cpu_state_data.destination_address -
12203ccc00a7SMahesh Salgaonkar 			fw_dump.reserve_dump_area_start,
12213ccc00a7SMahesh Salgaonkar 			fdm_ptr->cpu_state_data.destination_address -
12223ccc00a7SMahesh Salgaonkar 			fw_dump.reserve_dump_area_start);
1223b500afffSMahesh Salgaonkar out:
1224b500afffSMahesh Salgaonkar 	if (fdm_active)
1225b500afffSMahesh Salgaonkar 		mutex_unlock(&fadump_mutex);
12263ccc00a7SMahesh Salgaonkar 	return 0;
12273ccc00a7SMahesh Salgaonkar }
12283ccc00a7SMahesh Salgaonkar 
1229b500afffSMahesh Salgaonkar static struct kobj_attribute fadump_release_attr = __ATTR(fadump_release_mem,
1230b500afffSMahesh Salgaonkar 						0200, NULL,
1231b500afffSMahesh Salgaonkar 						fadump_release_memory_store);
12323ccc00a7SMahesh Salgaonkar static struct kobj_attribute fadump_attr = __ATTR(fadump_enabled,
12333ccc00a7SMahesh Salgaonkar 						0444, fadump_enabled_show,
12343ccc00a7SMahesh Salgaonkar 						NULL);
12353ccc00a7SMahesh Salgaonkar static struct kobj_attribute fadump_register_attr = __ATTR(fadump_registered,
12363ccc00a7SMahesh Salgaonkar 						0644, fadump_register_show,
12373ccc00a7SMahesh Salgaonkar 						fadump_register_store);
12383ccc00a7SMahesh Salgaonkar 
12393ccc00a7SMahesh Salgaonkar static int fadump_region_open(struct inode *inode, struct file *file)
12403ccc00a7SMahesh Salgaonkar {
12413ccc00a7SMahesh Salgaonkar 	return single_open(file, fadump_region_show, inode->i_private);
12423ccc00a7SMahesh Salgaonkar }
12433ccc00a7SMahesh Salgaonkar 
12443ccc00a7SMahesh Salgaonkar static const struct file_operations fadump_region_fops = {
12453ccc00a7SMahesh Salgaonkar 	.open    = fadump_region_open,
12463ccc00a7SMahesh Salgaonkar 	.read    = seq_read,
12473ccc00a7SMahesh Salgaonkar 	.llseek  = seq_lseek,
12483ccc00a7SMahesh Salgaonkar 	.release = single_release,
12493ccc00a7SMahesh Salgaonkar };
12503ccc00a7SMahesh Salgaonkar 
12513ccc00a7SMahesh Salgaonkar static void fadump_init_files(void)
12523ccc00a7SMahesh Salgaonkar {
12533ccc00a7SMahesh Salgaonkar 	struct dentry *debugfs_file;
12543ccc00a7SMahesh Salgaonkar 	int rc = 0;
12553ccc00a7SMahesh Salgaonkar 
12563ccc00a7SMahesh Salgaonkar 	rc = sysfs_create_file(kernel_kobj, &fadump_attr.attr);
12573ccc00a7SMahesh Salgaonkar 	if (rc)
12583ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "fadump: unable to create sysfs file"
12593ccc00a7SMahesh Salgaonkar 			" fadump_enabled (%d)\n", rc);
12603ccc00a7SMahesh Salgaonkar 
12613ccc00a7SMahesh Salgaonkar 	rc = sysfs_create_file(kernel_kobj, &fadump_register_attr.attr);
12623ccc00a7SMahesh Salgaonkar 	if (rc)
12633ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "fadump: unable to create sysfs file"
12643ccc00a7SMahesh Salgaonkar 			" fadump_registered (%d)\n", rc);
12653ccc00a7SMahesh Salgaonkar 
12663ccc00a7SMahesh Salgaonkar 	debugfs_file = debugfs_create_file("fadump_region", 0444,
12673ccc00a7SMahesh Salgaonkar 					powerpc_debugfs_root, NULL,
12683ccc00a7SMahesh Salgaonkar 					&fadump_region_fops);
12693ccc00a7SMahesh Salgaonkar 	if (!debugfs_file)
12703ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "fadump: unable to create debugfs file"
12713ccc00a7SMahesh Salgaonkar 				" fadump_region\n");
1272b500afffSMahesh Salgaonkar 
1273b500afffSMahesh Salgaonkar 	if (fw_dump.dump_active) {
1274b500afffSMahesh Salgaonkar 		rc = sysfs_create_file(kernel_kobj, &fadump_release_attr.attr);
1275b500afffSMahesh Salgaonkar 		if (rc)
1276b500afffSMahesh Salgaonkar 			printk(KERN_ERR "fadump: unable to create sysfs file"
1277b500afffSMahesh Salgaonkar 				" fadump_release_mem (%d)\n", rc);
1278b500afffSMahesh Salgaonkar 	}
12793ccc00a7SMahesh Salgaonkar 	return;
12803ccc00a7SMahesh Salgaonkar }
12813ccc00a7SMahesh Salgaonkar 
12823ccc00a7SMahesh Salgaonkar /*
12833ccc00a7SMahesh Salgaonkar  * Prepare for firmware-assisted dump.
12843ccc00a7SMahesh Salgaonkar  */
12853ccc00a7SMahesh Salgaonkar int __init setup_fadump(void)
12863ccc00a7SMahesh Salgaonkar {
12873ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_enabled)
12883ccc00a7SMahesh Salgaonkar 		return 0;
12893ccc00a7SMahesh Salgaonkar 
12903ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_supported) {
12913ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "Firmware-assisted dump is not supported on"
12923ccc00a7SMahesh Salgaonkar 			" this hardware\n");
12933ccc00a7SMahesh Salgaonkar 		return 0;
12943ccc00a7SMahesh Salgaonkar 	}
12953ccc00a7SMahesh Salgaonkar 
12963ccc00a7SMahesh Salgaonkar 	fadump_show_config();
12972df173d9SMahesh Salgaonkar 	/*
12982df173d9SMahesh Salgaonkar 	 * If dump data is available then see if it is valid and prepare for
12992df173d9SMahesh Salgaonkar 	 * saving it to the disk.
13002df173d9SMahesh Salgaonkar 	 */
1301b500afffSMahesh Salgaonkar 	if (fw_dump.dump_active) {
1302b500afffSMahesh Salgaonkar 		/*
1303b500afffSMahesh Salgaonkar 		 * if dump process fails then invalidate the registration
1304b500afffSMahesh Salgaonkar 		 * and release memory before proceeding for re-registration.
1305b500afffSMahesh Salgaonkar 		 */
1306b500afffSMahesh Salgaonkar 		if (process_fadump(fdm_active) < 0)
1307b500afffSMahesh Salgaonkar 			fadump_invalidate_release_mem();
1308b500afffSMahesh Salgaonkar 	}
13093ccc00a7SMahesh Salgaonkar 	/* Initialize the kernel dump memory structure for FAD registration. */
13102df173d9SMahesh Salgaonkar 	else if (fw_dump.reserve_dump_area_size)
13113ccc00a7SMahesh Salgaonkar 		init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
13123ccc00a7SMahesh Salgaonkar 	fadump_init_files();
13133ccc00a7SMahesh Salgaonkar 
13143ccc00a7SMahesh Salgaonkar 	return 1;
13153ccc00a7SMahesh Salgaonkar }
13163ccc00a7SMahesh Salgaonkar subsys_initcall(setup_fadump);
1317