xref: /openbmc/linux/arch/powerpc/kernel/fadump.c (revision ebaeb5ae24379b5b635dc1d1fa6df904bc95b4d9)
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>
36eb39c880SMahesh Salgaonkar 
37eb39c880SMahesh Salgaonkar #include <asm/page.h>
38eb39c880SMahesh Salgaonkar #include <asm/prom.h>
39eb39c880SMahesh Salgaonkar #include <asm/rtas.h>
40eb39c880SMahesh Salgaonkar #include <asm/fadump.h>
41eb39c880SMahesh Salgaonkar 
42eb39c880SMahesh Salgaonkar static struct fw_dump fw_dump;
433ccc00a7SMahesh Salgaonkar static struct fadump_mem_struct fdm;
443ccc00a7SMahesh Salgaonkar static const struct fadump_mem_struct *fdm_active;
453ccc00a7SMahesh Salgaonkar 
463ccc00a7SMahesh Salgaonkar static DEFINE_MUTEX(fadump_mutex);
472df173d9SMahesh Salgaonkar struct fad_crash_memory_ranges crash_memory_ranges[INIT_CRASHMEM_RANGES];
482df173d9SMahesh Salgaonkar int crash_mem_ranges;
49eb39c880SMahesh Salgaonkar 
50eb39c880SMahesh Salgaonkar /* Scan the Firmware Assisted dump configuration details. */
51eb39c880SMahesh Salgaonkar int __init early_init_dt_scan_fw_dump(unsigned long node,
52eb39c880SMahesh Salgaonkar 			const char *uname, int depth, void *data)
53eb39c880SMahesh Salgaonkar {
54eb39c880SMahesh Salgaonkar 	__be32 *sections;
55eb39c880SMahesh Salgaonkar 	int i, num_sections;
56eb39c880SMahesh Salgaonkar 	unsigned long size;
57eb39c880SMahesh Salgaonkar 	const int *token;
58eb39c880SMahesh Salgaonkar 
59eb39c880SMahesh Salgaonkar 	if (depth != 1 || strcmp(uname, "rtas") != 0)
60eb39c880SMahesh Salgaonkar 		return 0;
61eb39c880SMahesh Salgaonkar 
62eb39c880SMahesh Salgaonkar 	/*
63eb39c880SMahesh Salgaonkar 	 * Check if Firmware Assisted dump is supported. if yes, check
64eb39c880SMahesh Salgaonkar 	 * if dump has been initiated on last reboot.
65eb39c880SMahesh Salgaonkar 	 */
66eb39c880SMahesh Salgaonkar 	token = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump", NULL);
67eb39c880SMahesh Salgaonkar 	if (!token)
68eb39c880SMahesh Salgaonkar 		return 0;
69eb39c880SMahesh Salgaonkar 
70eb39c880SMahesh Salgaonkar 	fw_dump.fadump_supported = 1;
71eb39c880SMahesh Salgaonkar 	fw_dump.ibm_configure_kernel_dump = *token;
72eb39c880SMahesh Salgaonkar 
73eb39c880SMahesh Salgaonkar 	/*
74eb39c880SMahesh Salgaonkar 	 * The 'ibm,kernel-dump' rtas node is present only if there is
75eb39c880SMahesh Salgaonkar 	 * dump data waiting for us.
76eb39c880SMahesh Salgaonkar 	 */
773ccc00a7SMahesh Salgaonkar 	fdm_active = of_get_flat_dt_prop(node, "ibm,kernel-dump", NULL);
783ccc00a7SMahesh Salgaonkar 	if (fdm_active)
79eb39c880SMahesh Salgaonkar 		fw_dump.dump_active = 1;
80eb39c880SMahesh Salgaonkar 
81eb39c880SMahesh Salgaonkar 	/* Get the sizes required to store dump data for the firmware provided
82eb39c880SMahesh Salgaonkar 	 * dump sections.
83eb39c880SMahesh Salgaonkar 	 * For each dump section type supported, a 32bit cell which defines
84eb39c880SMahesh Salgaonkar 	 * the ID of a supported section followed by two 32 bit cells which
85eb39c880SMahesh Salgaonkar 	 * gives teh size of the section in bytes.
86eb39c880SMahesh Salgaonkar 	 */
87eb39c880SMahesh Salgaonkar 	sections = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump-sizes",
88eb39c880SMahesh Salgaonkar 					&size);
89eb39c880SMahesh Salgaonkar 
90eb39c880SMahesh Salgaonkar 	if (!sections)
91eb39c880SMahesh Salgaonkar 		return 0;
92eb39c880SMahesh Salgaonkar 
93eb39c880SMahesh Salgaonkar 	num_sections = size / (3 * sizeof(u32));
94eb39c880SMahesh Salgaonkar 
95eb39c880SMahesh Salgaonkar 	for (i = 0; i < num_sections; i++, sections += 3) {
96eb39c880SMahesh Salgaonkar 		u32 type = (u32)of_read_number(sections, 1);
97eb39c880SMahesh Salgaonkar 
98eb39c880SMahesh Salgaonkar 		switch (type) {
99eb39c880SMahesh Salgaonkar 		case FADUMP_CPU_STATE_DATA:
100eb39c880SMahesh Salgaonkar 			fw_dump.cpu_state_data_size =
101eb39c880SMahesh Salgaonkar 					of_read_ulong(&sections[1], 2);
102eb39c880SMahesh Salgaonkar 			break;
103eb39c880SMahesh Salgaonkar 		case FADUMP_HPTE_REGION:
104eb39c880SMahesh Salgaonkar 			fw_dump.hpte_region_size =
105eb39c880SMahesh Salgaonkar 					of_read_ulong(&sections[1], 2);
106eb39c880SMahesh Salgaonkar 			break;
107eb39c880SMahesh Salgaonkar 		}
108eb39c880SMahesh Salgaonkar 	}
109eb39c880SMahesh Salgaonkar 	return 1;
110eb39c880SMahesh Salgaonkar }
111eb39c880SMahesh Salgaonkar 
1123ccc00a7SMahesh Salgaonkar int is_fadump_active(void)
1133ccc00a7SMahesh Salgaonkar {
1143ccc00a7SMahesh Salgaonkar 	return fw_dump.dump_active;
1153ccc00a7SMahesh Salgaonkar }
1163ccc00a7SMahesh Salgaonkar 
1173ccc00a7SMahesh Salgaonkar /* Print firmware assisted dump configurations for debugging purpose. */
1183ccc00a7SMahesh Salgaonkar static void fadump_show_config(void)
1193ccc00a7SMahesh Salgaonkar {
1203ccc00a7SMahesh Salgaonkar 	pr_debug("Support for firmware-assisted dump (fadump): %s\n",
1213ccc00a7SMahesh Salgaonkar 			(fw_dump.fadump_supported ? "present" : "no support"));
1223ccc00a7SMahesh Salgaonkar 
1233ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_supported)
1243ccc00a7SMahesh Salgaonkar 		return;
1253ccc00a7SMahesh Salgaonkar 
1263ccc00a7SMahesh Salgaonkar 	pr_debug("Fadump enabled    : %s\n",
1273ccc00a7SMahesh Salgaonkar 				(fw_dump.fadump_enabled ? "yes" : "no"));
1283ccc00a7SMahesh Salgaonkar 	pr_debug("Dump Active       : %s\n",
1293ccc00a7SMahesh Salgaonkar 				(fw_dump.dump_active ? "yes" : "no"));
1303ccc00a7SMahesh Salgaonkar 	pr_debug("Dump section sizes:\n");
1313ccc00a7SMahesh Salgaonkar 	pr_debug("    CPU state data size: %lx\n", fw_dump.cpu_state_data_size);
1323ccc00a7SMahesh Salgaonkar 	pr_debug("    HPTE region size   : %lx\n", fw_dump.hpte_region_size);
1333ccc00a7SMahesh Salgaonkar 	pr_debug("Boot memory size  : %lx\n", fw_dump.boot_memory_size);
1343ccc00a7SMahesh Salgaonkar }
1353ccc00a7SMahesh Salgaonkar 
1363ccc00a7SMahesh Salgaonkar static unsigned long init_fadump_mem_struct(struct fadump_mem_struct *fdm,
1373ccc00a7SMahesh Salgaonkar 				unsigned long addr)
1383ccc00a7SMahesh Salgaonkar {
1393ccc00a7SMahesh Salgaonkar 	if (!fdm)
1403ccc00a7SMahesh Salgaonkar 		return 0;
1413ccc00a7SMahesh Salgaonkar 
1423ccc00a7SMahesh Salgaonkar 	memset(fdm, 0, sizeof(struct fadump_mem_struct));
1433ccc00a7SMahesh Salgaonkar 	addr = addr & PAGE_MASK;
1443ccc00a7SMahesh Salgaonkar 
1453ccc00a7SMahesh Salgaonkar 	fdm->header.dump_format_version = 0x00000001;
1463ccc00a7SMahesh Salgaonkar 	fdm->header.dump_num_sections = 3;
1473ccc00a7SMahesh Salgaonkar 	fdm->header.dump_status_flag = 0;
1483ccc00a7SMahesh Salgaonkar 	fdm->header.offset_first_dump_section =
1493ccc00a7SMahesh Salgaonkar 		(u32)offsetof(struct fadump_mem_struct, cpu_state_data);
1503ccc00a7SMahesh Salgaonkar 
1513ccc00a7SMahesh Salgaonkar 	/*
1523ccc00a7SMahesh Salgaonkar 	 * Fields for disk dump option.
1533ccc00a7SMahesh Salgaonkar 	 * We are not using disk dump option, hence set these fields to 0.
1543ccc00a7SMahesh Salgaonkar 	 */
1553ccc00a7SMahesh Salgaonkar 	fdm->header.dd_block_size = 0;
1563ccc00a7SMahesh Salgaonkar 	fdm->header.dd_block_offset = 0;
1573ccc00a7SMahesh Salgaonkar 	fdm->header.dd_num_blocks = 0;
1583ccc00a7SMahesh Salgaonkar 	fdm->header.dd_offset_disk_path = 0;
1593ccc00a7SMahesh Salgaonkar 
1603ccc00a7SMahesh Salgaonkar 	/* set 0 to disable an automatic dump-reboot. */
1613ccc00a7SMahesh Salgaonkar 	fdm->header.max_time_auto = 0;
1623ccc00a7SMahesh Salgaonkar 
1633ccc00a7SMahesh Salgaonkar 	/* Kernel dump sections */
1643ccc00a7SMahesh Salgaonkar 	/* cpu state data section. */
1653ccc00a7SMahesh Salgaonkar 	fdm->cpu_state_data.request_flag = FADUMP_REQUEST_FLAG;
1663ccc00a7SMahesh Salgaonkar 	fdm->cpu_state_data.source_data_type = FADUMP_CPU_STATE_DATA;
1673ccc00a7SMahesh Salgaonkar 	fdm->cpu_state_data.source_address = 0;
1683ccc00a7SMahesh Salgaonkar 	fdm->cpu_state_data.source_len = fw_dump.cpu_state_data_size;
1693ccc00a7SMahesh Salgaonkar 	fdm->cpu_state_data.destination_address = addr;
1703ccc00a7SMahesh Salgaonkar 	addr += fw_dump.cpu_state_data_size;
1713ccc00a7SMahesh Salgaonkar 
1723ccc00a7SMahesh Salgaonkar 	/* hpte region section */
1733ccc00a7SMahesh Salgaonkar 	fdm->hpte_region.request_flag = FADUMP_REQUEST_FLAG;
1743ccc00a7SMahesh Salgaonkar 	fdm->hpte_region.source_data_type = FADUMP_HPTE_REGION;
1753ccc00a7SMahesh Salgaonkar 	fdm->hpte_region.source_address = 0;
1763ccc00a7SMahesh Salgaonkar 	fdm->hpte_region.source_len = fw_dump.hpte_region_size;
1773ccc00a7SMahesh Salgaonkar 	fdm->hpte_region.destination_address = addr;
1783ccc00a7SMahesh Salgaonkar 	addr += fw_dump.hpte_region_size;
1793ccc00a7SMahesh Salgaonkar 
1803ccc00a7SMahesh Salgaonkar 	/* RMA region section */
1813ccc00a7SMahesh Salgaonkar 	fdm->rmr_region.request_flag = FADUMP_REQUEST_FLAG;
1823ccc00a7SMahesh Salgaonkar 	fdm->rmr_region.source_data_type = FADUMP_REAL_MODE_REGION;
1833ccc00a7SMahesh Salgaonkar 	fdm->rmr_region.source_address = RMA_START;
1843ccc00a7SMahesh Salgaonkar 	fdm->rmr_region.source_len = fw_dump.boot_memory_size;
1853ccc00a7SMahesh Salgaonkar 	fdm->rmr_region.destination_address = addr;
1863ccc00a7SMahesh Salgaonkar 	addr += fw_dump.boot_memory_size;
1873ccc00a7SMahesh Salgaonkar 
1883ccc00a7SMahesh Salgaonkar 	return addr;
1893ccc00a7SMahesh Salgaonkar }
1903ccc00a7SMahesh Salgaonkar 
191eb39c880SMahesh Salgaonkar /**
192eb39c880SMahesh Salgaonkar  * fadump_calculate_reserve_size(): reserve variable boot area 5% of System RAM
193eb39c880SMahesh Salgaonkar  *
194eb39c880SMahesh Salgaonkar  * Function to find the largest memory size we need to reserve during early
195eb39c880SMahesh Salgaonkar  * boot process. This will be the size of the memory that is required for a
196eb39c880SMahesh Salgaonkar  * kernel to boot successfully.
197eb39c880SMahesh Salgaonkar  *
198eb39c880SMahesh Salgaonkar  * This function has been taken from phyp-assisted dump feature implementation.
199eb39c880SMahesh Salgaonkar  *
200eb39c880SMahesh Salgaonkar  * returns larger of 256MB or 5% rounded down to multiples of 256MB.
201eb39c880SMahesh Salgaonkar  *
202eb39c880SMahesh Salgaonkar  * TODO: Come up with better approach to find out more accurate memory size
203eb39c880SMahesh Salgaonkar  * that is required for a kernel to boot successfully.
204eb39c880SMahesh Salgaonkar  *
205eb39c880SMahesh Salgaonkar  */
206eb39c880SMahesh Salgaonkar static inline unsigned long fadump_calculate_reserve_size(void)
207eb39c880SMahesh Salgaonkar {
208eb39c880SMahesh Salgaonkar 	unsigned long size;
209eb39c880SMahesh Salgaonkar 
210eb39c880SMahesh Salgaonkar 	/*
211eb39c880SMahesh Salgaonkar 	 * Check if the size is specified through fadump_reserve_mem= cmdline
212eb39c880SMahesh Salgaonkar 	 * option. If yes, then use that.
213eb39c880SMahesh Salgaonkar 	 */
214eb39c880SMahesh Salgaonkar 	if (fw_dump.reserve_bootvar)
215eb39c880SMahesh Salgaonkar 		return fw_dump.reserve_bootvar;
216eb39c880SMahesh Salgaonkar 
217eb39c880SMahesh Salgaonkar 	/* divide by 20 to get 5% of value */
218eb39c880SMahesh Salgaonkar 	size = memblock_end_of_DRAM() / 20;
219eb39c880SMahesh Salgaonkar 
220eb39c880SMahesh Salgaonkar 	/* round it down in multiples of 256 */
221eb39c880SMahesh Salgaonkar 	size = size & ~0x0FFFFFFFUL;
222eb39c880SMahesh Salgaonkar 
223eb39c880SMahesh Salgaonkar 	/* Truncate to memory_limit. We don't want to over reserve the memory.*/
224eb39c880SMahesh Salgaonkar 	if (memory_limit && size > memory_limit)
225eb39c880SMahesh Salgaonkar 		size = memory_limit;
226eb39c880SMahesh Salgaonkar 
227eb39c880SMahesh Salgaonkar 	return (size > MIN_BOOT_MEM ? size : MIN_BOOT_MEM);
228eb39c880SMahesh Salgaonkar }
229eb39c880SMahesh Salgaonkar 
230eb39c880SMahesh Salgaonkar /*
231eb39c880SMahesh Salgaonkar  * Calculate the total memory size required to be reserved for
232eb39c880SMahesh Salgaonkar  * firmware-assisted dump registration.
233eb39c880SMahesh Salgaonkar  */
234eb39c880SMahesh Salgaonkar static unsigned long get_fadump_area_size(void)
235eb39c880SMahesh Salgaonkar {
236eb39c880SMahesh Salgaonkar 	unsigned long size = 0;
237eb39c880SMahesh Salgaonkar 
238eb39c880SMahesh Salgaonkar 	size += fw_dump.cpu_state_data_size;
239eb39c880SMahesh Salgaonkar 	size += fw_dump.hpte_region_size;
240eb39c880SMahesh Salgaonkar 	size += fw_dump.boot_memory_size;
2412df173d9SMahesh Salgaonkar 	size += sizeof(struct fadump_crash_info_header);
2422df173d9SMahesh Salgaonkar 	size += sizeof(struct elfhdr); /* ELF core header.*/
243*ebaeb5aeSMahesh Salgaonkar 	size += sizeof(struct elf_phdr); /* place holder for cpu notes */
2442df173d9SMahesh Salgaonkar 	/* Program headers for crash memory regions. */
2452df173d9SMahesh Salgaonkar 	size += sizeof(struct elf_phdr) * (memblock_num_regions(memory) + 2);
246eb39c880SMahesh Salgaonkar 
247eb39c880SMahesh Salgaonkar 	size = PAGE_ALIGN(size);
248eb39c880SMahesh Salgaonkar 	return size;
249eb39c880SMahesh Salgaonkar }
250eb39c880SMahesh Salgaonkar 
251eb39c880SMahesh Salgaonkar int __init fadump_reserve_mem(void)
252eb39c880SMahesh Salgaonkar {
253eb39c880SMahesh Salgaonkar 	unsigned long base, size, memory_boundary;
254eb39c880SMahesh Salgaonkar 
255eb39c880SMahesh Salgaonkar 	if (!fw_dump.fadump_enabled)
256eb39c880SMahesh Salgaonkar 		return 0;
257eb39c880SMahesh Salgaonkar 
258eb39c880SMahesh Salgaonkar 	if (!fw_dump.fadump_supported) {
259eb39c880SMahesh Salgaonkar 		printk(KERN_INFO "Firmware-assisted dump is not supported on"
260eb39c880SMahesh Salgaonkar 				" this hardware\n");
261eb39c880SMahesh Salgaonkar 		fw_dump.fadump_enabled = 0;
262eb39c880SMahesh Salgaonkar 		return 0;
263eb39c880SMahesh Salgaonkar 	}
2643ccc00a7SMahesh Salgaonkar 	/*
2653ccc00a7SMahesh Salgaonkar 	 * Initialize boot memory size
2663ccc00a7SMahesh Salgaonkar 	 * If dump is active then we have already calculated the size during
2673ccc00a7SMahesh Salgaonkar 	 * first kernel.
2683ccc00a7SMahesh Salgaonkar 	 */
2693ccc00a7SMahesh Salgaonkar 	if (fdm_active)
2703ccc00a7SMahesh Salgaonkar 		fw_dump.boot_memory_size = fdm_active->rmr_region.source_len;
2713ccc00a7SMahesh Salgaonkar 	else
272eb39c880SMahesh Salgaonkar 		fw_dump.boot_memory_size = fadump_calculate_reserve_size();
273eb39c880SMahesh Salgaonkar 
274eb39c880SMahesh Salgaonkar 	/*
275eb39c880SMahesh Salgaonkar 	 * Calculate the memory boundary.
276eb39c880SMahesh Salgaonkar 	 * If memory_limit is less than actual memory boundary then reserve
277eb39c880SMahesh Salgaonkar 	 * the memory for fadump beyond the memory_limit and adjust the
278eb39c880SMahesh Salgaonkar 	 * memory_limit accordingly, so that the running kernel can run with
279eb39c880SMahesh Salgaonkar 	 * specified memory_limit.
280eb39c880SMahesh Salgaonkar 	 */
281eb39c880SMahesh Salgaonkar 	if (memory_limit && memory_limit < memblock_end_of_DRAM()) {
282eb39c880SMahesh Salgaonkar 		size = get_fadump_area_size();
283eb39c880SMahesh Salgaonkar 		if ((memory_limit + size) < memblock_end_of_DRAM())
284eb39c880SMahesh Salgaonkar 			memory_limit += size;
285eb39c880SMahesh Salgaonkar 		else
286eb39c880SMahesh Salgaonkar 			memory_limit = memblock_end_of_DRAM();
287eb39c880SMahesh Salgaonkar 		printk(KERN_INFO "Adjusted memory_limit for firmware-assisted"
288eb39c880SMahesh Salgaonkar 				" dump, now %#016llx\n",
289eb39c880SMahesh Salgaonkar 				(unsigned long long)memory_limit);
290eb39c880SMahesh Salgaonkar 	}
291eb39c880SMahesh Salgaonkar 	if (memory_limit)
292eb39c880SMahesh Salgaonkar 		memory_boundary = memory_limit;
293eb39c880SMahesh Salgaonkar 	else
294eb39c880SMahesh Salgaonkar 		memory_boundary = memblock_end_of_DRAM();
295eb39c880SMahesh Salgaonkar 
296eb39c880SMahesh Salgaonkar 	if (fw_dump.dump_active) {
297eb39c880SMahesh Salgaonkar 		printk(KERN_INFO "Firmware-assisted dump is active.\n");
298eb39c880SMahesh Salgaonkar 		/*
299eb39c880SMahesh Salgaonkar 		 * If last boot has crashed then reserve all the memory
300eb39c880SMahesh Salgaonkar 		 * above boot_memory_size so that we don't touch it until
301eb39c880SMahesh Salgaonkar 		 * dump is written to disk by userspace tool. This memory
302eb39c880SMahesh Salgaonkar 		 * will be released for general use once the dump is saved.
303eb39c880SMahesh Salgaonkar 		 */
304eb39c880SMahesh Salgaonkar 		base = fw_dump.boot_memory_size;
305eb39c880SMahesh Salgaonkar 		size = memory_boundary - base;
306eb39c880SMahesh Salgaonkar 		memblock_reserve(base, size);
307eb39c880SMahesh Salgaonkar 		printk(KERN_INFO "Reserved %ldMB of memory at %ldMB "
308eb39c880SMahesh Salgaonkar 				"for saving crash dump\n",
309eb39c880SMahesh Salgaonkar 				(unsigned long)(size >> 20),
310eb39c880SMahesh Salgaonkar 				(unsigned long)(base >> 20));
3112df173d9SMahesh Salgaonkar 
3122df173d9SMahesh Salgaonkar 		fw_dump.fadumphdr_addr =
3132df173d9SMahesh Salgaonkar 				fdm_active->rmr_region.destination_address +
3142df173d9SMahesh Salgaonkar 				fdm_active->rmr_region.source_len;
3152df173d9SMahesh Salgaonkar 		pr_debug("fadumphdr_addr = %p\n",
3162df173d9SMahesh Salgaonkar 				(void *) fw_dump.fadumphdr_addr);
317eb39c880SMahesh Salgaonkar 	} else {
318eb39c880SMahesh Salgaonkar 		/* Reserve the memory at the top of memory. */
319eb39c880SMahesh Salgaonkar 		size = get_fadump_area_size();
320eb39c880SMahesh Salgaonkar 		base = memory_boundary - size;
321eb39c880SMahesh Salgaonkar 		memblock_reserve(base, size);
322eb39c880SMahesh Salgaonkar 		printk(KERN_INFO "Reserved %ldMB of memory at %ldMB "
323eb39c880SMahesh Salgaonkar 				"for firmware-assisted dump\n",
324eb39c880SMahesh Salgaonkar 				(unsigned long)(size >> 20),
325eb39c880SMahesh Salgaonkar 				(unsigned long)(base >> 20));
326eb39c880SMahesh Salgaonkar 	}
327eb39c880SMahesh Salgaonkar 	fw_dump.reserve_dump_area_start = base;
328eb39c880SMahesh Salgaonkar 	fw_dump.reserve_dump_area_size = size;
329eb39c880SMahesh Salgaonkar 	return 1;
330eb39c880SMahesh Salgaonkar }
331eb39c880SMahesh Salgaonkar 
332eb39c880SMahesh Salgaonkar /* Look for fadump= cmdline option. */
333eb39c880SMahesh Salgaonkar static int __init early_fadump_param(char *p)
334eb39c880SMahesh Salgaonkar {
335eb39c880SMahesh Salgaonkar 	if (!p)
336eb39c880SMahesh Salgaonkar 		return 1;
337eb39c880SMahesh Salgaonkar 
338eb39c880SMahesh Salgaonkar 	if (strncmp(p, "on", 2) == 0)
339eb39c880SMahesh Salgaonkar 		fw_dump.fadump_enabled = 1;
340eb39c880SMahesh Salgaonkar 	else if (strncmp(p, "off", 3) == 0)
341eb39c880SMahesh Salgaonkar 		fw_dump.fadump_enabled = 0;
342eb39c880SMahesh Salgaonkar 
343eb39c880SMahesh Salgaonkar 	return 0;
344eb39c880SMahesh Salgaonkar }
345eb39c880SMahesh Salgaonkar early_param("fadump", early_fadump_param);
346eb39c880SMahesh Salgaonkar 
347eb39c880SMahesh Salgaonkar /* Look for fadump_reserve_mem= cmdline option */
348eb39c880SMahesh Salgaonkar static int __init early_fadump_reserve_mem(char *p)
349eb39c880SMahesh Salgaonkar {
350eb39c880SMahesh Salgaonkar 	if (p)
351eb39c880SMahesh Salgaonkar 		fw_dump.reserve_bootvar = memparse(p, &p);
352eb39c880SMahesh Salgaonkar 	return 0;
353eb39c880SMahesh Salgaonkar }
354eb39c880SMahesh Salgaonkar early_param("fadump_reserve_mem", early_fadump_reserve_mem);
3553ccc00a7SMahesh Salgaonkar 
3563ccc00a7SMahesh Salgaonkar static void register_fw_dump(struct fadump_mem_struct *fdm)
3573ccc00a7SMahesh Salgaonkar {
3583ccc00a7SMahesh Salgaonkar 	int rc;
3593ccc00a7SMahesh Salgaonkar 	unsigned int wait_time;
3603ccc00a7SMahesh Salgaonkar 
3613ccc00a7SMahesh Salgaonkar 	pr_debug("Registering for firmware-assisted kernel dump...\n");
3623ccc00a7SMahesh Salgaonkar 
3633ccc00a7SMahesh Salgaonkar 	/* TODO: Add upper time limit for the delay */
3643ccc00a7SMahesh Salgaonkar 	do {
3653ccc00a7SMahesh Salgaonkar 		rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
3663ccc00a7SMahesh Salgaonkar 			FADUMP_REGISTER, fdm,
3673ccc00a7SMahesh Salgaonkar 			sizeof(struct fadump_mem_struct));
3683ccc00a7SMahesh Salgaonkar 
3693ccc00a7SMahesh Salgaonkar 		wait_time = rtas_busy_delay_time(rc);
3703ccc00a7SMahesh Salgaonkar 		if (wait_time)
3713ccc00a7SMahesh Salgaonkar 			mdelay(wait_time);
3723ccc00a7SMahesh Salgaonkar 
3733ccc00a7SMahesh Salgaonkar 	} while (wait_time);
3743ccc00a7SMahesh Salgaonkar 
3753ccc00a7SMahesh Salgaonkar 	switch (rc) {
3763ccc00a7SMahesh Salgaonkar 	case -1:
3773ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "Failed to register firmware-assisted kernel"
3783ccc00a7SMahesh Salgaonkar 			" dump. Hardware Error(%d).\n", rc);
3793ccc00a7SMahesh Salgaonkar 		break;
3803ccc00a7SMahesh Salgaonkar 	case -3:
3813ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "Failed to register firmware-assisted kernel"
3823ccc00a7SMahesh Salgaonkar 			" dump. Parameter Error(%d).\n", rc);
3833ccc00a7SMahesh Salgaonkar 		break;
3843ccc00a7SMahesh Salgaonkar 	case -9:
3853ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "firmware-assisted kernel dump is already "
3863ccc00a7SMahesh Salgaonkar 			" registered.");
3873ccc00a7SMahesh Salgaonkar 		fw_dump.dump_registered = 1;
3883ccc00a7SMahesh Salgaonkar 		break;
3893ccc00a7SMahesh Salgaonkar 	case 0:
3903ccc00a7SMahesh Salgaonkar 		printk(KERN_INFO "firmware-assisted kernel dump registration"
3913ccc00a7SMahesh Salgaonkar 			" is successful\n");
3923ccc00a7SMahesh Salgaonkar 		fw_dump.dump_registered = 1;
3933ccc00a7SMahesh Salgaonkar 		break;
3943ccc00a7SMahesh Salgaonkar 	}
3953ccc00a7SMahesh Salgaonkar }
3963ccc00a7SMahesh Salgaonkar 
397*ebaeb5aeSMahesh Salgaonkar void crash_fadump(struct pt_regs *regs, const char *str)
398*ebaeb5aeSMahesh Salgaonkar {
399*ebaeb5aeSMahesh Salgaonkar 	struct fadump_crash_info_header *fdh = NULL;
400*ebaeb5aeSMahesh Salgaonkar 
401*ebaeb5aeSMahesh Salgaonkar 	if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr)
402*ebaeb5aeSMahesh Salgaonkar 		return;
403*ebaeb5aeSMahesh Salgaonkar 
404*ebaeb5aeSMahesh Salgaonkar 	fdh = __va(fw_dump.fadumphdr_addr);
405*ebaeb5aeSMahesh Salgaonkar 	crashing_cpu = smp_processor_id();
406*ebaeb5aeSMahesh Salgaonkar 	fdh->crashing_cpu = crashing_cpu;
407*ebaeb5aeSMahesh Salgaonkar 	crash_save_vmcoreinfo();
408*ebaeb5aeSMahesh Salgaonkar 
409*ebaeb5aeSMahesh Salgaonkar 	if (regs)
410*ebaeb5aeSMahesh Salgaonkar 		fdh->regs = *regs;
411*ebaeb5aeSMahesh Salgaonkar 	else
412*ebaeb5aeSMahesh Salgaonkar 		ppc_save_regs(&fdh->regs);
413*ebaeb5aeSMahesh Salgaonkar 
414*ebaeb5aeSMahesh Salgaonkar 	fdh->cpu_online_mask = *cpu_online_mask;
415*ebaeb5aeSMahesh Salgaonkar 
416*ebaeb5aeSMahesh Salgaonkar 	/* Call ibm,os-term rtas call to trigger firmware assisted dump */
417*ebaeb5aeSMahesh Salgaonkar 	rtas_os_term((char *)str);
418*ebaeb5aeSMahesh Salgaonkar }
419*ebaeb5aeSMahesh Salgaonkar 
420*ebaeb5aeSMahesh Salgaonkar #define GPR_MASK	0xffffff0000000000
421*ebaeb5aeSMahesh Salgaonkar static inline int fadump_gpr_index(u64 id)
422*ebaeb5aeSMahesh Salgaonkar {
423*ebaeb5aeSMahesh Salgaonkar 	int i = -1;
424*ebaeb5aeSMahesh Salgaonkar 	char str[3];
425*ebaeb5aeSMahesh Salgaonkar 
426*ebaeb5aeSMahesh Salgaonkar 	if ((id & GPR_MASK) == REG_ID("GPR")) {
427*ebaeb5aeSMahesh Salgaonkar 		/* get the digits at the end */
428*ebaeb5aeSMahesh Salgaonkar 		id &= ~GPR_MASK;
429*ebaeb5aeSMahesh Salgaonkar 		id >>= 24;
430*ebaeb5aeSMahesh Salgaonkar 		str[2] = '\0';
431*ebaeb5aeSMahesh Salgaonkar 		str[1] = id & 0xff;
432*ebaeb5aeSMahesh Salgaonkar 		str[0] = (id >> 8) & 0xff;
433*ebaeb5aeSMahesh Salgaonkar 		sscanf(str, "%d", &i);
434*ebaeb5aeSMahesh Salgaonkar 		if (i > 31)
435*ebaeb5aeSMahesh Salgaonkar 			i = -1;
436*ebaeb5aeSMahesh Salgaonkar 	}
437*ebaeb5aeSMahesh Salgaonkar 	return i;
438*ebaeb5aeSMahesh Salgaonkar }
439*ebaeb5aeSMahesh Salgaonkar 
440*ebaeb5aeSMahesh Salgaonkar static inline void fadump_set_regval(struct pt_regs *regs, u64 reg_id,
441*ebaeb5aeSMahesh Salgaonkar 								u64 reg_val)
442*ebaeb5aeSMahesh Salgaonkar {
443*ebaeb5aeSMahesh Salgaonkar 	int i;
444*ebaeb5aeSMahesh Salgaonkar 
445*ebaeb5aeSMahesh Salgaonkar 	i = fadump_gpr_index(reg_id);
446*ebaeb5aeSMahesh Salgaonkar 	if (i >= 0)
447*ebaeb5aeSMahesh Salgaonkar 		regs->gpr[i] = (unsigned long)reg_val;
448*ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("NIA"))
449*ebaeb5aeSMahesh Salgaonkar 		regs->nip = (unsigned long)reg_val;
450*ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("MSR"))
451*ebaeb5aeSMahesh Salgaonkar 		regs->msr = (unsigned long)reg_val;
452*ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("CTR"))
453*ebaeb5aeSMahesh Salgaonkar 		regs->ctr = (unsigned long)reg_val;
454*ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("LR"))
455*ebaeb5aeSMahesh Salgaonkar 		regs->link = (unsigned long)reg_val;
456*ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("XER"))
457*ebaeb5aeSMahesh Salgaonkar 		regs->xer = (unsigned long)reg_val;
458*ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("CR"))
459*ebaeb5aeSMahesh Salgaonkar 		regs->ccr = (unsigned long)reg_val;
460*ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("DAR"))
461*ebaeb5aeSMahesh Salgaonkar 		regs->dar = (unsigned long)reg_val;
462*ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("DSISR"))
463*ebaeb5aeSMahesh Salgaonkar 		regs->dsisr = (unsigned long)reg_val;
464*ebaeb5aeSMahesh Salgaonkar }
465*ebaeb5aeSMahesh Salgaonkar 
466*ebaeb5aeSMahesh Salgaonkar static struct fadump_reg_entry*
467*ebaeb5aeSMahesh Salgaonkar fadump_read_registers(struct fadump_reg_entry *reg_entry, struct pt_regs *regs)
468*ebaeb5aeSMahesh Salgaonkar {
469*ebaeb5aeSMahesh Salgaonkar 	memset(regs, 0, sizeof(struct pt_regs));
470*ebaeb5aeSMahesh Salgaonkar 
471*ebaeb5aeSMahesh Salgaonkar 	while (reg_entry->reg_id != REG_ID("CPUEND")) {
472*ebaeb5aeSMahesh Salgaonkar 		fadump_set_regval(regs, reg_entry->reg_id,
473*ebaeb5aeSMahesh Salgaonkar 					reg_entry->reg_value);
474*ebaeb5aeSMahesh Salgaonkar 		reg_entry++;
475*ebaeb5aeSMahesh Salgaonkar 	}
476*ebaeb5aeSMahesh Salgaonkar 	reg_entry++;
477*ebaeb5aeSMahesh Salgaonkar 	return reg_entry;
478*ebaeb5aeSMahesh Salgaonkar }
479*ebaeb5aeSMahesh Salgaonkar 
480*ebaeb5aeSMahesh Salgaonkar static u32 *fadump_append_elf_note(u32 *buf, char *name, unsigned type,
481*ebaeb5aeSMahesh Salgaonkar 						void *data, size_t data_len)
482*ebaeb5aeSMahesh Salgaonkar {
483*ebaeb5aeSMahesh Salgaonkar 	struct elf_note note;
484*ebaeb5aeSMahesh Salgaonkar 
485*ebaeb5aeSMahesh Salgaonkar 	note.n_namesz = strlen(name) + 1;
486*ebaeb5aeSMahesh Salgaonkar 	note.n_descsz = data_len;
487*ebaeb5aeSMahesh Salgaonkar 	note.n_type   = type;
488*ebaeb5aeSMahesh Salgaonkar 	memcpy(buf, &note, sizeof(note));
489*ebaeb5aeSMahesh Salgaonkar 	buf += (sizeof(note) + 3)/4;
490*ebaeb5aeSMahesh Salgaonkar 	memcpy(buf, name, note.n_namesz);
491*ebaeb5aeSMahesh Salgaonkar 	buf += (note.n_namesz + 3)/4;
492*ebaeb5aeSMahesh Salgaonkar 	memcpy(buf, data, note.n_descsz);
493*ebaeb5aeSMahesh Salgaonkar 	buf += (note.n_descsz + 3)/4;
494*ebaeb5aeSMahesh Salgaonkar 
495*ebaeb5aeSMahesh Salgaonkar 	return buf;
496*ebaeb5aeSMahesh Salgaonkar }
497*ebaeb5aeSMahesh Salgaonkar 
498*ebaeb5aeSMahesh Salgaonkar static void fadump_final_note(u32 *buf)
499*ebaeb5aeSMahesh Salgaonkar {
500*ebaeb5aeSMahesh Salgaonkar 	struct elf_note note;
501*ebaeb5aeSMahesh Salgaonkar 
502*ebaeb5aeSMahesh Salgaonkar 	note.n_namesz = 0;
503*ebaeb5aeSMahesh Salgaonkar 	note.n_descsz = 0;
504*ebaeb5aeSMahesh Salgaonkar 	note.n_type   = 0;
505*ebaeb5aeSMahesh Salgaonkar 	memcpy(buf, &note, sizeof(note));
506*ebaeb5aeSMahesh Salgaonkar }
507*ebaeb5aeSMahesh Salgaonkar 
508*ebaeb5aeSMahesh Salgaonkar static u32 *fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs)
509*ebaeb5aeSMahesh Salgaonkar {
510*ebaeb5aeSMahesh Salgaonkar 	struct elf_prstatus prstatus;
511*ebaeb5aeSMahesh Salgaonkar 
512*ebaeb5aeSMahesh Salgaonkar 	memset(&prstatus, 0, sizeof(prstatus));
513*ebaeb5aeSMahesh Salgaonkar 	/*
514*ebaeb5aeSMahesh Salgaonkar 	 * FIXME: How do i get PID? Do I really need it?
515*ebaeb5aeSMahesh Salgaonkar 	 * prstatus.pr_pid = ????
516*ebaeb5aeSMahesh Salgaonkar 	 */
517*ebaeb5aeSMahesh Salgaonkar 	elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
518*ebaeb5aeSMahesh Salgaonkar 	buf = fadump_append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS,
519*ebaeb5aeSMahesh Salgaonkar 				&prstatus, sizeof(prstatus));
520*ebaeb5aeSMahesh Salgaonkar 	return buf;
521*ebaeb5aeSMahesh Salgaonkar }
522*ebaeb5aeSMahesh Salgaonkar 
523*ebaeb5aeSMahesh Salgaonkar static void fadump_update_elfcore_header(char *bufp)
524*ebaeb5aeSMahesh Salgaonkar {
525*ebaeb5aeSMahesh Salgaonkar 	struct elfhdr *elf;
526*ebaeb5aeSMahesh Salgaonkar 	struct elf_phdr *phdr;
527*ebaeb5aeSMahesh Salgaonkar 
528*ebaeb5aeSMahesh Salgaonkar 	elf = (struct elfhdr *)bufp;
529*ebaeb5aeSMahesh Salgaonkar 	bufp += sizeof(struct elfhdr);
530*ebaeb5aeSMahesh Salgaonkar 
531*ebaeb5aeSMahesh Salgaonkar 	/* First note is a place holder for cpu notes info. */
532*ebaeb5aeSMahesh Salgaonkar 	phdr = (struct elf_phdr *)bufp;
533*ebaeb5aeSMahesh Salgaonkar 
534*ebaeb5aeSMahesh Salgaonkar 	if (phdr->p_type == PT_NOTE) {
535*ebaeb5aeSMahesh Salgaonkar 		phdr->p_paddr = fw_dump.cpu_notes_buf;
536*ebaeb5aeSMahesh Salgaonkar 		phdr->p_offset	= phdr->p_paddr;
537*ebaeb5aeSMahesh Salgaonkar 		phdr->p_filesz	= fw_dump.cpu_notes_buf_size;
538*ebaeb5aeSMahesh Salgaonkar 		phdr->p_memsz = fw_dump.cpu_notes_buf_size;
539*ebaeb5aeSMahesh Salgaonkar 	}
540*ebaeb5aeSMahesh Salgaonkar 	return;
541*ebaeb5aeSMahesh Salgaonkar }
542*ebaeb5aeSMahesh Salgaonkar 
543*ebaeb5aeSMahesh Salgaonkar static void *fadump_cpu_notes_buf_alloc(unsigned long size)
544*ebaeb5aeSMahesh Salgaonkar {
545*ebaeb5aeSMahesh Salgaonkar 	void *vaddr;
546*ebaeb5aeSMahesh Salgaonkar 	struct page *page;
547*ebaeb5aeSMahesh Salgaonkar 	unsigned long order, count, i;
548*ebaeb5aeSMahesh Salgaonkar 
549*ebaeb5aeSMahesh Salgaonkar 	order = get_order(size);
550*ebaeb5aeSMahesh Salgaonkar 	vaddr = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
551*ebaeb5aeSMahesh Salgaonkar 	if (!vaddr)
552*ebaeb5aeSMahesh Salgaonkar 		return NULL;
553*ebaeb5aeSMahesh Salgaonkar 
554*ebaeb5aeSMahesh Salgaonkar 	count = 1 << order;
555*ebaeb5aeSMahesh Salgaonkar 	page = virt_to_page(vaddr);
556*ebaeb5aeSMahesh Salgaonkar 	for (i = 0; i < count; i++)
557*ebaeb5aeSMahesh Salgaonkar 		SetPageReserved(page + i);
558*ebaeb5aeSMahesh Salgaonkar 	return vaddr;
559*ebaeb5aeSMahesh Salgaonkar }
560*ebaeb5aeSMahesh Salgaonkar 
561*ebaeb5aeSMahesh Salgaonkar static void fadump_cpu_notes_buf_free(unsigned long vaddr, unsigned long size)
562*ebaeb5aeSMahesh Salgaonkar {
563*ebaeb5aeSMahesh Salgaonkar 	struct page *page;
564*ebaeb5aeSMahesh Salgaonkar 	unsigned long order, count, i;
565*ebaeb5aeSMahesh Salgaonkar 
566*ebaeb5aeSMahesh Salgaonkar 	order = get_order(size);
567*ebaeb5aeSMahesh Salgaonkar 	count = 1 << order;
568*ebaeb5aeSMahesh Salgaonkar 	page = virt_to_page(vaddr);
569*ebaeb5aeSMahesh Salgaonkar 	for (i = 0; i < count; i++)
570*ebaeb5aeSMahesh Salgaonkar 		ClearPageReserved(page + i);
571*ebaeb5aeSMahesh Salgaonkar 	__free_pages(page, order);
572*ebaeb5aeSMahesh Salgaonkar }
573*ebaeb5aeSMahesh Salgaonkar 
574*ebaeb5aeSMahesh Salgaonkar /*
575*ebaeb5aeSMahesh Salgaonkar  * Read CPU state dump data and convert it into ELF notes.
576*ebaeb5aeSMahesh Salgaonkar  * The CPU dump starts with magic number "REGSAVE". NumCpusOffset should be
577*ebaeb5aeSMahesh Salgaonkar  * used to access the data to allow for additional fields to be added without
578*ebaeb5aeSMahesh Salgaonkar  * affecting compatibility. Each list of registers for a CPU starts with
579*ebaeb5aeSMahesh Salgaonkar  * "CPUSTRT" and ends with "CPUEND". Each register entry is of 16 bytes,
580*ebaeb5aeSMahesh Salgaonkar  * 8 Byte ASCII identifier and 8 Byte register value. The register entry
581*ebaeb5aeSMahesh Salgaonkar  * with identifier "CPUSTRT" and "CPUEND" contains 4 byte cpu id as part
582*ebaeb5aeSMahesh Salgaonkar  * of register value. For more details refer to PAPR document.
583*ebaeb5aeSMahesh Salgaonkar  *
584*ebaeb5aeSMahesh Salgaonkar  * Only for the crashing cpu we ignore the CPU dump data and get exact
585*ebaeb5aeSMahesh Salgaonkar  * state from fadump crash info structure populated by first kernel at the
586*ebaeb5aeSMahesh Salgaonkar  * time of crash.
587*ebaeb5aeSMahesh Salgaonkar  */
588*ebaeb5aeSMahesh Salgaonkar static int __init fadump_build_cpu_notes(const struct fadump_mem_struct *fdm)
589*ebaeb5aeSMahesh Salgaonkar {
590*ebaeb5aeSMahesh Salgaonkar 	struct fadump_reg_save_area_header *reg_header;
591*ebaeb5aeSMahesh Salgaonkar 	struct fadump_reg_entry *reg_entry;
592*ebaeb5aeSMahesh Salgaonkar 	struct fadump_crash_info_header *fdh = NULL;
593*ebaeb5aeSMahesh Salgaonkar 	void *vaddr;
594*ebaeb5aeSMahesh Salgaonkar 	unsigned long addr;
595*ebaeb5aeSMahesh Salgaonkar 	u32 num_cpus, *note_buf;
596*ebaeb5aeSMahesh Salgaonkar 	struct pt_regs regs;
597*ebaeb5aeSMahesh Salgaonkar 	int i, rc = 0, cpu = 0;
598*ebaeb5aeSMahesh Salgaonkar 
599*ebaeb5aeSMahesh Salgaonkar 	if (!fdm->cpu_state_data.bytes_dumped)
600*ebaeb5aeSMahesh Salgaonkar 		return -EINVAL;
601*ebaeb5aeSMahesh Salgaonkar 
602*ebaeb5aeSMahesh Salgaonkar 	addr = fdm->cpu_state_data.destination_address;
603*ebaeb5aeSMahesh Salgaonkar 	vaddr = __va(addr);
604*ebaeb5aeSMahesh Salgaonkar 
605*ebaeb5aeSMahesh Salgaonkar 	reg_header = vaddr;
606*ebaeb5aeSMahesh Salgaonkar 	if (reg_header->magic_number != REGSAVE_AREA_MAGIC) {
607*ebaeb5aeSMahesh Salgaonkar 		printk(KERN_ERR "Unable to read register save area.\n");
608*ebaeb5aeSMahesh Salgaonkar 		return -ENOENT;
609*ebaeb5aeSMahesh Salgaonkar 	}
610*ebaeb5aeSMahesh Salgaonkar 	pr_debug("--------CPU State Data------------\n");
611*ebaeb5aeSMahesh Salgaonkar 	pr_debug("Magic Number: %llx\n", reg_header->magic_number);
612*ebaeb5aeSMahesh Salgaonkar 	pr_debug("NumCpuOffset: %x\n", reg_header->num_cpu_offset);
613*ebaeb5aeSMahesh Salgaonkar 
614*ebaeb5aeSMahesh Salgaonkar 	vaddr += reg_header->num_cpu_offset;
615*ebaeb5aeSMahesh Salgaonkar 	num_cpus = *((u32 *)(vaddr));
616*ebaeb5aeSMahesh Salgaonkar 	pr_debug("NumCpus     : %u\n", num_cpus);
617*ebaeb5aeSMahesh Salgaonkar 	vaddr += sizeof(u32);
618*ebaeb5aeSMahesh Salgaonkar 	reg_entry = (struct fadump_reg_entry *)vaddr;
619*ebaeb5aeSMahesh Salgaonkar 
620*ebaeb5aeSMahesh Salgaonkar 	/* Allocate buffer to hold cpu crash notes. */
621*ebaeb5aeSMahesh Salgaonkar 	fw_dump.cpu_notes_buf_size = num_cpus * sizeof(note_buf_t);
622*ebaeb5aeSMahesh Salgaonkar 	fw_dump.cpu_notes_buf_size = PAGE_ALIGN(fw_dump.cpu_notes_buf_size);
623*ebaeb5aeSMahesh Salgaonkar 	note_buf = fadump_cpu_notes_buf_alloc(fw_dump.cpu_notes_buf_size);
624*ebaeb5aeSMahesh Salgaonkar 	if (!note_buf) {
625*ebaeb5aeSMahesh Salgaonkar 		printk(KERN_ERR "Failed to allocate 0x%lx bytes for "
626*ebaeb5aeSMahesh Salgaonkar 			"cpu notes buffer\n", fw_dump.cpu_notes_buf_size);
627*ebaeb5aeSMahesh Salgaonkar 		return -ENOMEM;
628*ebaeb5aeSMahesh Salgaonkar 	}
629*ebaeb5aeSMahesh Salgaonkar 	fw_dump.cpu_notes_buf = __pa(note_buf);
630*ebaeb5aeSMahesh Salgaonkar 
631*ebaeb5aeSMahesh Salgaonkar 	pr_debug("Allocated buffer for cpu notes of size %ld at %p\n",
632*ebaeb5aeSMahesh Salgaonkar 			(num_cpus * sizeof(note_buf_t)), note_buf);
633*ebaeb5aeSMahesh Salgaonkar 
634*ebaeb5aeSMahesh Salgaonkar 	if (fw_dump.fadumphdr_addr)
635*ebaeb5aeSMahesh Salgaonkar 		fdh = __va(fw_dump.fadumphdr_addr);
636*ebaeb5aeSMahesh Salgaonkar 
637*ebaeb5aeSMahesh Salgaonkar 	for (i = 0; i < num_cpus; i++) {
638*ebaeb5aeSMahesh Salgaonkar 		if (reg_entry->reg_id != REG_ID("CPUSTRT")) {
639*ebaeb5aeSMahesh Salgaonkar 			printk(KERN_ERR "Unable to read CPU state data\n");
640*ebaeb5aeSMahesh Salgaonkar 			rc = -ENOENT;
641*ebaeb5aeSMahesh Salgaonkar 			goto error_out;
642*ebaeb5aeSMahesh Salgaonkar 		}
643*ebaeb5aeSMahesh Salgaonkar 		/* Lower 4 bytes of reg_value contains logical cpu id */
644*ebaeb5aeSMahesh Salgaonkar 		cpu = reg_entry->reg_value & FADUMP_CPU_ID_MASK;
645*ebaeb5aeSMahesh Salgaonkar 		if (!cpumask_test_cpu(cpu, &fdh->cpu_online_mask)) {
646*ebaeb5aeSMahesh Salgaonkar 			SKIP_TO_NEXT_CPU(reg_entry);
647*ebaeb5aeSMahesh Salgaonkar 			continue;
648*ebaeb5aeSMahesh Salgaonkar 		}
649*ebaeb5aeSMahesh Salgaonkar 		pr_debug("Reading register data for cpu %d...\n", cpu);
650*ebaeb5aeSMahesh Salgaonkar 		if (fdh && fdh->crashing_cpu == cpu) {
651*ebaeb5aeSMahesh Salgaonkar 			regs = fdh->regs;
652*ebaeb5aeSMahesh Salgaonkar 			note_buf = fadump_regs_to_elf_notes(note_buf, &regs);
653*ebaeb5aeSMahesh Salgaonkar 			SKIP_TO_NEXT_CPU(reg_entry);
654*ebaeb5aeSMahesh Salgaonkar 		} else {
655*ebaeb5aeSMahesh Salgaonkar 			reg_entry++;
656*ebaeb5aeSMahesh Salgaonkar 			reg_entry = fadump_read_registers(reg_entry, &regs);
657*ebaeb5aeSMahesh Salgaonkar 			note_buf = fadump_regs_to_elf_notes(note_buf, &regs);
658*ebaeb5aeSMahesh Salgaonkar 		}
659*ebaeb5aeSMahesh Salgaonkar 	}
660*ebaeb5aeSMahesh Salgaonkar 	fadump_final_note(note_buf);
661*ebaeb5aeSMahesh Salgaonkar 
662*ebaeb5aeSMahesh Salgaonkar 	pr_debug("Updating elfcore header (%llx) with cpu notes\n",
663*ebaeb5aeSMahesh Salgaonkar 							fdh->elfcorehdr_addr);
664*ebaeb5aeSMahesh Salgaonkar 	fadump_update_elfcore_header((char *)__va(fdh->elfcorehdr_addr));
665*ebaeb5aeSMahesh Salgaonkar 	return 0;
666*ebaeb5aeSMahesh Salgaonkar 
667*ebaeb5aeSMahesh Salgaonkar error_out:
668*ebaeb5aeSMahesh Salgaonkar 	fadump_cpu_notes_buf_free((unsigned long)__va(fw_dump.cpu_notes_buf),
669*ebaeb5aeSMahesh Salgaonkar 					fw_dump.cpu_notes_buf_size);
670*ebaeb5aeSMahesh Salgaonkar 	fw_dump.cpu_notes_buf = 0;
671*ebaeb5aeSMahesh Salgaonkar 	fw_dump.cpu_notes_buf_size = 0;
672*ebaeb5aeSMahesh Salgaonkar 	return rc;
673*ebaeb5aeSMahesh Salgaonkar 
674*ebaeb5aeSMahesh Salgaonkar }
675*ebaeb5aeSMahesh Salgaonkar 
6762df173d9SMahesh Salgaonkar /*
6772df173d9SMahesh Salgaonkar  * Validate and process the dump data stored by firmware before exporting
6782df173d9SMahesh Salgaonkar  * it through '/proc/vmcore'.
6792df173d9SMahesh Salgaonkar  */
6802df173d9SMahesh Salgaonkar static int __init process_fadump(const struct fadump_mem_struct *fdm_active)
6812df173d9SMahesh Salgaonkar {
6822df173d9SMahesh Salgaonkar 	struct fadump_crash_info_header *fdh;
683*ebaeb5aeSMahesh Salgaonkar 	int rc = 0;
6842df173d9SMahesh Salgaonkar 
6852df173d9SMahesh Salgaonkar 	if (!fdm_active || !fw_dump.fadumphdr_addr)
6862df173d9SMahesh Salgaonkar 		return -EINVAL;
6872df173d9SMahesh Salgaonkar 
6882df173d9SMahesh Salgaonkar 	/* Check if the dump data is valid. */
6892df173d9SMahesh Salgaonkar 	if ((fdm_active->header.dump_status_flag == FADUMP_ERROR_FLAG) ||
690*ebaeb5aeSMahesh Salgaonkar 			(fdm_active->cpu_state_data.error_flags != 0) ||
6912df173d9SMahesh Salgaonkar 			(fdm_active->rmr_region.error_flags != 0)) {
6922df173d9SMahesh Salgaonkar 		printk(KERN_ERR "Dump taken by platform is not valid\n");
6932df173d9SMahesh Salgaonkar 		return -EINVAL;
6942df173d9SMahesh Salgaonkar 	}
695*ebaeb5aeSMahesh Salgaonkar 	if ((fdm_active->rmr_region.bytes_dumped !=
696*ebaeb5aeSMahesh Salgaonkar 			fdm_active->rmr_region.source_len) ||
697*ebaeb5aeSMahesh Salgaonkar 			!fdm_active->cpu_state_data.bytes_dumped) {
6982df173d9SMahesh Salgaonkar 		printk(KERN_ERR "Dump taken by platform is incomplete\n");
6992df173d9SMahesh Salgaonkar 		return -EINVAL;
7002df173d9SMahesh Salgaonkar 	}
7012df173d9SMahesh Salgaonkar 
7022df173d9SMahesh Salgaonkar 	/* Validate the fadump crash info header */
7032df173d9SMahesh Salgaonkar 	fdh = __va(fw_dump.fadumphdr_addr);
7042df173d9SMahesh Salgaonkar 	if (fdh->magic_number != FADUMP_CRASH_INFO_MAGIC) {
7052df173d9SMahesh Salgaonkar 		printk(KERN_ERR "Crash info header is not valid.\n");
7062df173d9SMahesh Salgaonkar 		return -EINVAL;
7072df173d9SMahesh Salgaonkar 	}
7082df173d9SMahesh Salgaonkar 
709*ebaeb5aeSMahesh Salgaonkar 	rc = fadump_build_cpu_notes(fdm_active);
710*ebaeb5aeSMahesh Salgaonkar 	if (rc)
711*ebaeb5aeSMahesh Salgaonkar 		return rc;
712*ebaeb5aeSMahesh Salgaonkar 
7132df173d9SMahesh Salgaonkar 	/*
7142df173d9SMahesh Salgaonkar 	 * We are done validating dump info and elfcore header is now ready
7152df173d9SMahesh Salgaonkar 	 * to be exported. set elfcorehdr_addr so that vmcore module will
7162df173d9SMahesh Salgaonkar 	 * export the elfcore header through '/proc/vmcore'.
7172df173d9SMahesh Salgaonkar 	 */
7182df173d9SMahesh Salgaonkar 	elfcorehdr_addr = fdh->elfcorehdr_addr;
7192df173d9SMahesh Salgaonkar 
7202df173d9SMahesh Salgaonkar 	return 0;
7212df173d9SMahesh Salgaonkar }
7222df173d9SMahesh Salgaonkar 
7232df173d9SMahesh Salgaonkar static inline void fadump_add_crash_memory(unsigned long long base,
7242df173d9SMahesh Salgaonkar 					unsigned long long end)
7252df173d9SMahesh Salgaonkar {
7262df173d9SMahesh Salgaonkar 	if (base == end)
7272df173d9SMahesh Salgaonkar 		return;
7282df173d9SMahesh Salgaonkar 
7292df173d9SMahesh Salgaonkar 	pr_debug("crash_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n",
7302df173d9SMahesh Salgaonkar 		crash_mem_ranges, base, end - 1, (end - base));
7312df173d9SMahesh Salgaonkar 	crash_memory_ranges[crash_mem_ranges].base = base;
7322df173d9SMahesh Salgaonkar 	crash_memory_ranges[crash_mem_ranges].size = end - base;
7332df173d9SMahesh Salgaonkar 	crash_mem_ranges++;
7342df173d9SMahesh Salgaonkar }
7352df173d9SMahesh Salgaonkar 
7362df173d9SMahesh Salgaonkar static void fadump_exclude_reserved_area(unsigned long long start,
7372df173d9SMahesh Salgaonkar 					unsigned long long end)
7382df173d9SMahesh Salgaonkar {
7392df173d9SMahesh Salgaonkar 	unsigned long long ra_start, ra_end;
7402df173d9SMahesh Salgaonkar 
7412df173d9SMahesh Salgaonkar 	ra_start = fw_dump.reserve_dump_area_start;
7422df173d9SMahesh Salgaonkar 	ra_end = ra_start + fw_dump.reserve_dump_area_size;
7432df173d9SMahesh Salgaonkar 
7442df173d9SMahesh Salgaonkar 	if ((ra_start < end) && (ra_end > start)) {
7452df173d9SMahesh Salgaonkar 		if ((start < ra_start) && (end > ra_end)) {
7462df173d9SMahesh Salgaonkar 			fadump_add_crash_memory(start, ra_start);
7472df173d9SMahesh Salgaonkar 			fadump_add_crash_memory(ra_end, end);
7482df173d9SMahesh Salgaonkar 		} else if (start < ra_start) {
7492df173d9SMahesh Salgaonkar 			fadump_add_crash_memory(start, ra_start);
7502df173d9SMahesh Salgaonkar 		} else if (ra_end < end) {
7512df173d9SMahesh Salgaonkar 			fadump_add_crash_memory(ra_end, end);
7522df173d9SMahesh Salgaonkar 		}
7532df173d9SMahesh Salgaonkar 	} else
7542df173d9SMahesh Salgaonkar 		fadump_add_crash_memory(start, end);
7552df173d9SMahesh Salgaonkar }
7562df173d9SMahesh Salgaonkar 
7572df173d9SMahesh Salgaonkar static int fadump_init_elfcore_header(char *bufp)
7582df173d9SMahesh Salgaonkar {
7592df173d9SMahesh Salgaonkar 	struct elfhdr *elf;
7602df173d9SMahesh Salgaonkar 
7612df173d9SMahesh Salgaonkar 	elf = (struct elfhdr *) bufp;
7622df173d9SMahesh Salgaonkar 	bufp += sizeof(struct elfhdr);
7632df173d9SMahesh Salgaonkar 	memcpy(elf->e_ident, ELFMAG, SELFMAG);
7642df173d9SMahesh Salgaonkar 	elf->e_ident[EI_CLASS] = ELF_CLASS;
7652df173d9SMahesh Salgaonkar 	elf->e_ident[EI_DATA] = ELF_DATA;
7662df173d9SMahesh Salgaonkar 	elf->e_ident[EI_VERSION] = EV_CURRENT;
7672df173d9SMahesh Salgaonkar 	elf->e_ident[EI_OSABI] = ELF_OSABI;
7682df173d9SMahesh Salgaonkar 	memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
7692df173d9SMahesh Salgaonkar 	elf->e_type = ET_CORE;
7702df173d9SMahesh Salgaonkar 	elf->e_machine = ELF_ARCH;
7712df173d9SMahesh Salgaonkar 	elf->e_version = EV_CURRENT;
7722df173d9SMahesh Salgaonkar 	elf->e_entry = 0;
7732df173d9SMahesh Salgaonkar 	elf->e_phoff = sizeof(struct elfhdr);
7742df173d9SMahesh Salgaonkar 	elf->e_shoff = 0;
7752df173d9SMahesh Salgaonkar 	elf->e_flags = ELF_CORE_EFLAGS;
7762df173d9SMahesh Salgaonkar 	elf->e_ehsize = sizeof(struct elfhdr);
7772df173d9SMahesh Salgaonkar 	elf->e_phentsize = sizeof(struct elf_phdr);
7782df173d9SMahesh Salgaonkar 	elf->e_phnum = 0;
7792df173d9SMahesh Salgaonkar 	elf->e_shentsize = 0;
7802df173d9SMahesh Salgaonkar 	elf->e_shnum = 0;
7812df173d9SMahesh Salgaonkar 	elf->e_shstrndx = 0;
7822df173d9SMahesh Salgaonkar 
7832df173d9SMahesh Salgaonkar 	return 0;
7842df173d9SMahesh Salgaonkar }
7852df173d9SMahesh Salgaonkar 
7862df173d9SMahesh Salgaonkar /*
7872df173d9SMahesh Salgaonkar  * Traverse through memblock structure and setup crash memory ranges. These
7882df173d9SMahesh Salgaonkar  * ranges will be used create PT_LOAD program headers in elfcore header.
7892df173d9SMahesh Salgaonkar  */
7902df173d9SMahesh Salgaonkar static void fadump_setup_crash_memory_ranges(void)
7912df173d9SMahesh Salgaonkar {
7922df173d9SMahesh Salgaonkar 	struct memblock_region *reg;
7932df173d9SMahesh Salgaonkar 	unsigned long long start, end;
7942df173d9SMahesh Salgaonkar 
7952df173d9SMahesh Salgaonkar 	pr_debug("Setup crash memory ranges.\n");
7962df173d9SMahesh Salgaonkar 	crash_mem_ranges = 0;
7972df173d9SMahesh Salgaonkar 	/*
7982df173d9SMahesh Salgaonkar 	 * add the first memory chunk (RMA_START through boot_memory_size) as
7992df173d9SMahesh Salgaonkar 	 * a separate memory chunk. The reason is, at the time crash firmware
8002df173d9SMahesh Salgaonkar 	 * will move the content of this memory chunk to different location
8012df173d9SMahesh Salgaonkar 	 * specified during fadump registration. We need to create a separate
8022df173d9SMahesh Salgaonkar 	 * program header for this chunk with the correct offset.
8032df173d9SMahesh Salgaonkar 	 */
8042df173d9SMahesh Salgaonkar 	fadump_add_crash_memory(RMA_START, fw_dump.boot_memory_size);
8052df173d9SMahesh Salgaonkar 
8062df173d9SMahesh Salgaonkar 	for_each_memblock(memory, reg) {
8072df173d9SMahesh Salgaonkar 		start = (unsigned long long)reg->base;
8082df173d9SMahesh Salgaonkar 		end = start + (unsigned long long)reg->size;
8092df173d9SMahesh Salgaonkar 		if (start == RMA_START && end >= fw_dump.boot_memory_size)
8102df173d9SMahesh Salgaonkar 			start = fw_dump.boot_memory_size;
8112df173d9SMahesh Salgaonkar 
8122df173d9SMahesh Salgaonkar 		/* add this range excluding the reserved dump area. */
8132df173d9SMahesh Salgaonkar 		fadump_exclude_reserved_area(start, end);
8142df173d9SMahesh Salgaonkar 	}
8152df173d9SMahesh Salgaonkar }
8162df173d9SMahesh Salgaonkar 
8172df173d9SMahesh Salgaonkar static int fadump_create_elfcore_headers(char *bufp)
8182df173d9SMahesh Salgaonkar {
8192df173d9SMahesh Salgaonkar 	struct elfhdr *elf;
8202df173d9SMahesh Salgaonkar 	struct elf_phdr *phdr;
8212df173d9SMahesh Salgaonkar 	int i;
8222df173d9SMahesh Salgaonkar 
8232df173d9SMahesh Salgaonkar 	fadump_init_elfcore_header(bufp);
8242df173d9SMahesh Salgaonkar 	elf = (struct elfhdr *)bufp;
8252df173d9SMahesh Salgaonkar 	bufp += sizeof(struct elfhdr);
8262df173d9SMahesh Salgaonkar 
827*ebaeb5aeSMahesh Salgaonkar 	/*
828*ebaeb5aeSMahesh Salgaonkar 	 * setup ELF PT_NOTE, place holder for cpu notes info. The notes info
829*ebaeb5aeSMahesh Salgaonkar 	 * will be populated during second kernel boot after crash. Hence
830*ebaeb5aeSMahesh Salgaonkar 	 * this PT_NOTE will always be the first elf note.
831*ebaeb5aeSMahesh Salgaonkar 	 *
832*ebaeb5aeSMahesh Salgaonkar 	 * NOTE: Any new ELF note addition should be placed after this note.
833*ebaeb5aeSMahesh Salgaonkar 	 */
834*ebaeb5aeSMahesh Salgaonkar 	phdr = (struct elf_phdr *)bufp;
835*ebaeb5aeSMahesh Salgaonkar 	bufp += sizeof(struct elf_phdr);
836*ebaeb5aeSMahesh Salgaonkar 	phdr->p_type = PT_NOTE;
837*ebaeb5aeSMahesh Salgaonkar 	phdr->p_flags = 0;
838*ebaeb5aeSMahesh Salgaonkar 	phdr->p_vaddr = 0;
839*ebaeb5aeSMahesh Salgaonkar 	phdr->p_align = 0;
840*ebaeb5aeSMahesh Salgaonkar 
841*ebaeb5aeSMahesh Salgaonkar 	phdr->p_offset = 0;
842*ebaeb5aeSMahesh Salgaonkar 	phdr->p_paddr = 0;
843*ebaeb5aeSMahesh Salgaonkar 	phdr->p_filesz = 0;
844*ebaeb5aeSMahesh Salgaonkar 	phdr->p_memsz = 0;
845*ebaeb5aeSMahesh Salgaonkar 
846*ebaeb5aeSMahesh Salgaonkar 	(elf->e_phnum)++;
847*ebaeb5aeSMahesh Salgaonkar 
8482df173d9SMahesh Salgaonkar 	/* setup PT_LOAD sections. */
8492df173d9SMahesh Salgaonkar 
8502df173d9SMahesh Salgaonkar 	for (i = 0; i < crash_mem_ranges; i++) {
8512df173d9SMahesh Salgaonkar 		unsigned long long mbase, msize;
8522df173d9SMahesh Salgaonkar 		mbase = crash_memory_ranges[i].base;
8532df173d9SMahesh Salgaonkar 		msize = crash_memory_ranges[i].size;
8542df173d9SMahesh Salgaonkar 
8552df173d9SMahesh Salgaonkar 		if (!msize)
8562df173d9SMahesh Salgaonkar 			continue;
8572df173d9SMahesh Salgaonkar 
8582df173d9SMahesh Salgaonkar 		phdr = (struct elf_phdr *)bufp;
8592df173d9SMahesh Salgaonkar 		bufp += sizeof(struct elf_phdr);
8602df173d9SMahesh Salgaonkar 		phdr->p_type	= PT_LOAD;
8612df173d9SMahesh Salgaonkar 		phdr->p_flags	= PF_R|PF_W|PF_X;
8622df173d9SMahesh Salgaonkar 		phdr->p_offset	= mbase;
8632df173d9SMahesh Salgaonkar 
8642df173d9SMahesh Salgaonkar 		if (mbase == RMA_START) {
8652df173d9SMahesh Salgaonkar 			/*
8662df173d9SMahesh Salgaonkar 			 * The entire RMA region will be moved by firmware
8672df173d9SMahesh Salgaonkar 			 * to the specified destination_address. Hence set
8682df173d9SMahesh Salgaonkar 			 * the correct offset.
8692df173d9SMahesh Salgaonkar 			 */
8702df173d9SMahesh Salgaonkar 			phdr->p_offset = fdm.rmr_region.destination_address;
8712df173d9SMahesh Salgaonkar 		}
8722df173d9SMahesh Salgaonkar 
8732df173d9SMahesh Salgaonkar 		phdr->p_paddr = mbase;
8742df173d9SMahesh Salgaonkar 		phdr->p_vaddr = (unsigned long)__va(mbase);
8752df173d9SMahesh Salgaonkar 		phdr->p_filesz = msize;
8762df173d9SMahesh Salgaonkar 		phdr->p_memsz = msize;
8772df173d9SMahesh Salgaonkar 		phdr->p_align = 0;
8782df173d9SMahesh Salgaonkar 
8792df173d9SMahesh Salgaonkar 		/* Increment number of program headers. */
8802df173d9SMahesh Salgaonkar 		(elf->e_phnum)++;
8812df173d9SMahesh Salgaonkar 	}
8822df173d9SMahesh Salgaonkar 	return 0;
8832df173d9SMahesh Salgaonkar }
8842df173d9SMahesh Salgaonkar 
8852df173d9SMahesh Salgaonkar static unsigned long init_fadump_header(unsigned long addr)
8862df173d9SMahesh Salgaonkar {
8872df173d9SMahesh Salgaonkar 	struct fadump_crash_info_header *fdh;
8882df173d9SMahesh Salgaonkar 
8892df173d9SMahesh Salgaonkar 	if (!addr)
8902df173d9SMahesh Salgaonkar 		return 0;
8912df173d9SMahesh Salgaonkar 
8922df173d9SMahesh Salgaonkar 	fw_dump.fadumphdr_addr = addr;
8932df173d9SMahesh Salgaonkar 	fdh = __va(addr);
8942df173d9SMahesh Salgaonkar 	addr += sizeof(struct fadump_crash_info_header);
8952df173d9SMahesh Salgaonkar 
8962df173d9SMahesh Salgaonkar 	memset(fdh, 0, sizeof(struct fadump_crash_info_header));
8972df173d9SMahesh Salgaonkar 	fdh->magic_number = FADUMP_CRASH_INFO_MAGIC;
8982df173d9SMahesh Salgaonkar 	fdh->elfcorehdr_addr = addr;
899*ebaeb5aeSMahesh Salgaonkar 	/* We will set the crashing cpu id in crash_fadump() during crash. */
900*ebaeb5aeSMahesh Salgaonkar 	fdh->crashing_cpu = CPU_UNKNOWN;
9012df173d9SMahesh Salgaonkar 
9022df173d9SMahesh Salgaonkar 	return addr;
9032df173d9SMahesh Salgaonkar }
9042df173d9SMahesh Salgaonkar 
9053ccc00a7SMahesh Salgaonkar static void register_fadump(void)
9063ccc00a7SMahesh Salgaonkar {
9072df173d9SMahesh Salgaonkar 	unsigned long addr;
9082df173d9SMahesh Salgaonkar 	void *vaddr;
9092df173d9SMahesh Salgaonkar 
9103ccc00a7SMahesh Salgaonkar 	/*
9113ccc00a7SMahesh Salgaonkar 	 * If no memory is reserved then we can not register for firmware-
9123ccc00a7SMahesh Salgaonkar 	 * assisted dump.
9133ccc00a7SMahesh Salgaonkar 	 */
9143ccc00a7SMahesh Salgaonkar 	if (!fw_dump.reserve_dump_area_size)
9153ccc00a7SMahesh Salgaonkar 		return;
9163ccc00a7SMahesh Salgaonkar 
9172df173d9SMahesh Salgaonkar 	fadump_setup_crash_memory_ranges();
9182df173d9SMahesh Salgaonkar 
9192df173d9SMahesh Salgaonkar 	addr = fdm.rmr_region.destination_address + fdm.rmr_region.source_len;
9202df173d9SMahesh Salgaonkar 	/* Initialize fadump crash info header. */
9212df173d9SMahesh Salgaonkar 	addr = init_fadump_header(addr);
9222df173d9SMahesh Salgaonkar 	vaddr = __va(addr);
9232df173d9SMahesh Salgaonkar 
9242df173d9SMahesh Salgaonkar 	pr_debug("Creating ELF core headers at %#016lx\n", addr);
9252df173d9SMahesh Salgaonkar 	fadump_create_elfcore_headers(vaddr);
9262df173d9SMahesh Salgaonkar 
9273ccc00a7SMahesh Salgaonkar 	/* register the future kernel dump with firmware. */
9283ccc00a7SMahesh Salgaonkar 	register_fw_dump(&fdm);
9293ccc00a7SMahesh Salgaonkar }
9303ccc00a7SMahesh Salgaonkar 
9313ccc00a7SMahesh Salgaonkar static int fadump_unregister_dump(struct fadump_mem_struct *fdm)
9323ccc00a7SMahesh Salgaonkar {
9333ccc00a7SMahesh Salgaonkar 	int rc = 0;
9343ccc00a7SMahesh Salgaonkar 	unsigned int wait_time;
9353ccc00a7SMahesh Salgaonkar 
9363ccc00a7SMahesh Salgaonkar 	pr_debug("Un-register firmware-assisted dump\n");
9373ccc00a7SMahesh Salgaonkar 
9383ccc00a7SMahesh Salgaonkar 	/* TODO: Add upper time limit for the delay */
9393ccc00a7SMahesh Salgaonkar 	do {
9403ccc00a7SMahesh Salgaonkar 		rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
9413ccc00a7SMahesh Salgaonkar 			FADUMP_UNREGISTER, fdm,
9423ccc00a7SMahesh Salgaonkar 			sizeof(struct fadump_mem_struct));
9433ccc00a7SMahesh Salgaonkar 
9443ccc00a7SMahesh Salgaonkar 		wait_time = rtas_busy_delay_time(rc);
9453ccc00a7SMahesh Salgaonkar 		if (wait_time)
9463ccc00a7SMahesh Salgaonkar 			mdelay(wait_time);
9473ccc00a7SMahesh Salgaonkar 	} while (wait_time);
9483ccc00a7SMahesh Salgaonkar 
9493ccc00a7SMahesh Salgaonkar 	if (rc) {
9503ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "Failed to un-register firmware-assisted dump."
9513ccc00a7SMahesh Salgaonkar 			" unexpected error(%d).\n", rc);
9523ccc00a7SMahesh Salgaonkar 		return rc;
9533ccc00a7SMahesh Salgaonkar 	}
9543ccc00a7SMahesh Salgaonkar 	fw_dump.dump_registered = 0;
9553ccc00a7SMahesh Salgaonkar 	return 0;
9563ccc00a7SMahesh Salgaonkar }
9573ccc00a7SMahesh Salgaonkar 
9583ccc00a7SMahesh Salgaonkar static ssize_t fadump_enabled_show(struct kobject *kobj,
9593ccc00a7SMahesh Salgaonkar 					struct kobj_attribute *attr,
9603ccc00a7SMahesh Salgaonkar 					char *buf)
9613ccc00a7SMahesh Salgaonkar {
9623ccc00a7SMahesh Salgaonkar 	return sprintf(buf, "%d\n", fw_dump.fadump_enabled);
9633ccc00a7SMahesh Salgaonkar }
9643ccc00a7SMahesh Salgaonkar 
9653ccc00a7SMahesh Salgaonkar static ssize_t fadump_register_show(struct kobject *kobj,
9663ccc00a7SMahesh Salgaonkar 					struct kobj_attribute *attr,
9673ccc00a7SMahesh Salgaonkar 					char *buf)
9683ccc00a7SMahesh Salgaonkar {
9693ccc00a7SMahesh Salgaonkar 	return sprintf(buf, "%d\n", fw_dump.dump_registered);
9703ccc00a7SMahesh Salgaonkar }
9713ccc00a7SMahesh Salgaonkar 
9723ccc00a7SMahesh Salgaonkar static ssize_t fadump_register_store(struct kobject *kobj,
9733ccc00a7SMahesh Salgaonkar 					struct kobj_attribute *attr,
9743ccc00a7SMahesh Salgaonkar 					const char *buf, size_t count)
9753ccc00a7SMahesh Salgaonkar {
9763ccc00a7SMahesh Salgaonkar 	int ret = 0;
9773ccc00a7SMahesh Salgaonkar 
9783ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_enabled || fdm_active)
9793ccc00a7SMahesh Salgaonkar 		return -EPERM;
9803ccc00a7SMahesh Salgaonkar 
9813ccc00a7SMahesh Salgaonkar 	mutex_lock(&fadump_mutex);
9823ccc00a7SMahesh Salgaonkar 
9833ccc00a7SMahesh Salgaonkar 	switch (buf[0]) {
9843ccc00a7SMahesh Salgaonkar 	case '0':
9853ccc00a7SMahesh Salgaonkar 		if (fw_dump.dump_registered == 0) {
9863ccc00a7SMahesh Salgaonkar 			ret = -EINVAL;
9873ccc00a7SMahesh Salgaonkar 			goto unlock_out;
9883ccc00a7SMahesh Salgaonkar 		}
9893ccc00a7SMahesh Salgaonkar 		/* Un-register Firmware-assisted dump */
9903ccc00a7SMahesh Salgaonkar 		fadump_unregister_dump(&fdm);
9913ccc00a7SMahesh Salgaonkar 		break;
9923ccc00a7SMahesh Salgaonkar 	case '1':
9933ccc00a7SMahesh Salgaonkar 		if (fw_dump.dump_registered == 1) {
9943ccc00a7SMahesh Salgaonkar 			ret = -EINVAL;
9953ccc00a7SMahesh Salgaonkar 			goto unlock_out;
9963ccc00a7SMahesh Salgaonkar 		}
9973ccc00a7SMahesh Salgaonkar 		/* Register Firmware-assisted dump */
9983ccc00a7SMahesh Salgaonkar 		register_fadump();
9993ccc00a7SMahesh Salgaonkar 		break;
10003ccc00a7SMahesh Salgaonkar 	default:
10013ccc00a7SMahesh Salgaonkar 		ret = -EINVAL;
10023ccc00a7SMahesh Salgaonkar 		break;
10033ccc00a7SMahesh Salgaonkar 	}
10043ccc00a7SMahesh Salgaonkar 
10053ccc00a7SMahesh Salgaonkar unlock_out:
10063ccc00a7SMahesh Salgaonkar 	mutex_unlock(&fadump_mutex);
10073ccc00a7SMahesh Salgaonkar 	return ret < 0 ? ret : count;
10083ccc00a7SMahesh Salgaonkar }
10093ccc00a7SMahesh Salgaonkar 
10103ccc00a7SMahesh Salgaonkar static int fadump_region_show(struct seq_file *m, void *private)
10113ccc00a7SMahesh Salgaonkar {
10123ccc00a7SMahesh Salgaonkar 	const struct fadump_mem_struct *fdm_ptr;
10133ccc00a7SMahesh Salgaonkar 
10143ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_enabled)
10153ccc00a7SMahesh Salgaonkar 		return 0;
10163ccc00a7SMahesh Salgaonkar 
10173ccc00a7SMahesh Salgaonkar 	if (fdm_active)
10183ccc00a7SMahesh Salgaonkar 		fdm_ptr = fdm_active;
10193ccc00a7SMahesh Salgaonkar 	else
10203ccc00a7SMahesh Salgaonkar 		fdm_ptr = &fdm;
10213ccc00a7SMahesh Salgaonkar 
10223ccc00a7SMahesh Salgaonkar 	seq_printf(m,
10233ccc00a7SMahesh Salgaonkar 			"CPU : [%#016llx-%#016llx] %#llx bytes, "
10243ccc00a7SMahesh Salgaonkar 			"Dumped: %#llx\n",
10253ccc00a7SMahesh Salgaonkar 			fdm_ptr->cpu_state_data.destination_address,
10263ccc00a7SMahesh Salgaonkar 			fdm_ptr->cpu_state_data.destination_address +
10273ccc00a7SMahesh Salgaonkar 			fdm_ptr->cpu_state_data.source_len - 1,
10283ccc00a7SMahesh Salgaonkar 			fdm_ptr->cpu_state_data.source_len,
10293ccc00a7SMahesh Salgaonkar 			fdm_ptr->cpu_state_data.bytes_dumped);
10303ccc00a7SMahesh Salgaonkar 	seq_printf(m,
10313ccc00a7SMahesh Salgaonkar 			"HPTE: [%#016llx-%#016llx] %#llx bytes, "
10323ccc00a7SMahesh Salgaonkar 			"Dumped: %#llx\n",
10333ccc00a7SMahesh Salgaonkar 			fdm_ptr->hpte_region.destination_address,
10343ccc00a7SMahesh Salgaonkar 			fdm_ptr->hpte_region.destination_address +
10353ccc00a7SMahesh Salgaonkar 			fdm_ptr->hpte_region.source_len - 1,
10363ccc00a7SMahesh Salgaonkar 			fdm_ptr->hpte_region.source_len,
10373ccc00a7SMahesh Salgaonkar 			fdm_ptr->hpte_region.bytes_dumped);
10383ccc00a7SMahesh Salgaonkar 	seq_printf(m,
10393ccc00a7SMahesh Salgaonkar 			"DUMP: [%#016llx-%#016llx] %#llx bytes, "
10403ccc00a7SMahesh Salgaonkar 			"Dumped: %#llx\n",
10413ccc00a7SMahesh Salgaonkar 			fdm_ptr->rmr_region.destination_address,
10423ccc00a7SMahesh Salgaonkar 			fdm_ptr->rmr_region.destination_address +
10433ccc00a7SMahesh Salgaonkar 			fdm_ptr->rmr_region.source_len - 1,
10443ccc00a7SMahesh Salgaonkar 			fdm_ptr->rmr_region.source_len,
10453ccc00a7SMahesh Salgaonkar 			fdm_ptr->rmr_region.bytes_dumped);
10463ccc00a7SMahesh Salgaonkar 
10473ccc00a7SMahesh Salgaonkar 	if (!fdm_active ||
10483ccc00a7SMahesh Salgaonkar 		(fw_dump.reserve_dump_area_start ==
10493ccc00a7SMahesh Salgaonkar 		fdm_ptr->cpu_state_data.destination_address))
10503ccc00a7SMahesh Salgaonkar 		return 0;
10513ccc00a7SMahesh Salgaonkar 
10523ccc00a7SMahesh Salgaonkar 	/* Dump is active. Show reserved memory region. */
10533ccc00a7SMahesh Salgaonkar 	seq_printf(m,
10543ccc00a7SMahesh Salgaonkar 			"    : [%#016llx-%#016llx] %#llx bytes, "
10553ccc00a7SMahesh Salgaonkar 			"Dumped: %#llx\n",
10563ccc00a7SMahesh Salgaonkar 			(unsigned long long)fw_dump.reserve_dump_area_start,
10573ccc00a7SMahesh Salgaonkar 			fdm_ptr->cpu_state_data.destination_address - 1,
10583ccc00a7SMahesh Salgaonkar 			fdm_ptr->cpu_state_data.destination_address -
10593ccc00a7SMahesh Salgaonkar 			fw_dump.reserve_dump_area_start,
10603ccc00a7SMahesh Salgaonkar 			fdm_ptr->cpu_state_data.destination_address -
10613ccc00a7SMahesh Salgaonkar 			fw_dump.reserve_dump_area_start);
10623ccc00a7SMahesh Salgaonkar 	return 0;
10633ccc00a7SMahesh Salgaonkar }
10643ccc00a7SMahesh Salgaonkar 
10653ccc00a7SMahesh Salgaonkar static struct kobj_attribute fadump_attr = __ATTR(fadump_enabled,
10663ccc00a7SMahesh Salgaonkar 						0444, fadump_enabled_show,
10673ccc00a7SMahesh Salgaonkar 						NULL);
10683ccc00a7SMahesh Salgaonkar static struct kobj_attribute fadump_register_attr = __ATTR(fadump_registered,
10693ccc00a7SMahesh Salgaonkar 						0644, fadump_register_show,
10703ccc00a7SMahesh Salgaonkar 						fadump_register_store);
10713ccc00a7SMahesh Salgaonkar 
10723ccc00a7SMahesh Salgaonkar static int fadump_region_open(struct inode *inode, struct file *file)
10733ccc00a7SMahesh Salgaonkar {
10743ccc00a7SMahesh Salgaonkar 	return single_open(file, fadump_region_show, inode->i_private);
10753ccc00a7SMahesh Salgaonkar }
10763ccc00a7SMahesh Salgaonkar 
10773ccc00a7SMahesh Salgaonkar static const struct file_operations fadump_region_fops = {
10783ccc00a7SMahesh Salgaonkar 	.open    = fadump_region_open,
10793ccc00a7SMahesh Salgaonkar 	.read    = seq_read,
10803ccc00a7SMahesh Salgaonkar 	.llseek  = seq_lseek,
10813ccc00a7SMahesh Salgaonkar 	.release = single_release,
10823ccc00a7SMahesh Salgaonkar };
10833ccc00a7SMahesh Salgaonkar 
10843ccc00a7SMahesh Salgaonkar static void fadump_init_files(void)
10853ccc00a7SMahesh Salgaonkar {
10863ccc00a7SMahesh Salgaonkar 	struct dentry *debugfs_file;
10873ccc00a7SMahesh Salgaonkar 	int rc = 0;
10883ccc00a7SMahesh Salgaonkar 
10893ccc00a7SMahesh Salgaonkar 	rc = sysfs_create_file(kernel_kobj, &fadump_attr.attr);
10903ccc00a7SMahesh Salgaonkar 	if (rc)
10913ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "fadump: unable to create sysfs file"
10923ccc00a7SMahesh Salgaonkar 			" fadump_enabled (%d)\n", rc);
10933ccc00a7SMahesh Salgaonkar 
10943ccc00a7SMahesh Salgaonkar 	rc = sysfs_create_file(kernel_kobj, &fadump_register_attr.attr);
10953ccc00a7SMahesh Salgaonkar 	if (rc)
10963ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "fadump: unable to create sysfs file"
10973ccc00a7SMahesh Salgaonkar 			" fadump_registered (%d)\n", rc);
10983ccc00a7SMahesh Salgaonkar 
10993ccc00a7SMahesh Salgaonkar 	debugfs_file = debugfs_create_file("fadump_region", 0444,
11003ccc00a7SMahesh Salgaonkar 					powerpc_debugfs_root, NULL,
11013ccc00a7SMahesh Salgaonkar 					&fadump_region_fops);
11023ccc00a7SMahesh Salgaonkar 	if (!debugfs_file)
11033ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "fadump: unable to create debugfs file"
11043ccc00a7SMahesh Salgaonkar 				" fadump_region\n");
11053ccc00a7SMahesh Salgaonkar 	return;
11063ccc00a7SMahesh Salgaonkar }
11073ccc00a7SMahesh Salgaonkar 
11083ccc00a7SMahesh Salgaonkar /*
11093ccc00a7SMahesh Salgaonkar  * Prepare for firmware-assisted dump.
11103ccc00a7SMahesh Salgaonkar  */
11113ccc00a7SMahesh Salgaonkar int __init setup_fadump(void)
11123ccc00a7SMahesh Salgaonkar {
11133ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_enabled)
11143ccc00a7SMahesh Salgaonkar 		return 0;
11153ccc00a7SMahesh Salgaonkar 
11163ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_supported) {
11173ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "Firmware-assisted dump is not supported on"
11183ccc00a7SMahesh Salgaonkar 			" this hardware\n");
11193ccc00a7SMahesh Salgaonkar 		return 0;
11203ccc00a7SMahesh Salgaonkar 	}
11213ccc00a7SMahesh Salgaonkar 
11223ccc00a7SMahesh Salgaonkar 	fadump_show_config();
11232df173d9SMahesh Salgaonkar 	/*
11242df173d9SMahesh Salgaonkar 	 * If dump data is available then see if it is valid and prepare for
11252df173d9SMahesh Salgaonkar 	 * saving it to the disk.
11262df173d9SMahesh Salgaonkar 	 */
11272df173d9SMahesh Salgaonkar 	if (fw_dump.dump_active)
11282df173d9SMahesh Salgaonkar 		process_fadump(fdm_active);
11293ccc00a7SMahesh Salgaonkar 	/* Initialize the kernel dump memory structure for FAD registration. */
11302df173d9SMahesh Salgaonkar 	else if (fw_dump.reserve_dump_area_size)
11313ccc00a7SMahesh Salgaonkar 		init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
11323ccc00a7SMahesh Salgaonkar 	fadump_init_files();
11333ccc00a7SMahesh Salgaonkar 
11343ccc00a7SMahesh Salgaonkar 	return 1;
11353ccc00a7SMahesh Salgaonkar }
11363ccc00a7SMahesh Salgaonkar subsys_initcall(setup_fadump);
1137