xref: /openbmc/linux/arch/powerpc/kernel/fadump.c (revision d8bced27be25537bde3714cbdb34ccece81f6a0d)
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 {
589d0c4dfeSRob Herring 	const __be32 *sections;
59eb39c880SMahesh Salgaonkar 	int i, num_sections;
609d0c4dfeSRob Herring 	int size;
61408cddd9SHari Bathini 	const __be32 *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)
72a7d04317SGavin Shan 		return 1;
73eb39c880SMahesh Salgaonkar 
74eb39c880SMahesh Salgaonkar 	fw_dump.fadump_supported = 1;
75408cddd9SHari Bathini 	fw_dump.ibm_configure_kernel_dump = be32_to_cpu(*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)
95a7d04317SGavin Shan 		return 1;
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 	}
113a7d04317SGavin Shan 
114eb39c880SMahesh Salgaonkar 	return 1;
115eb39c880SMahesh Salgaonkar }
116eb39c880SMahesh Salgaonkar 
1173ccc00a7SMahesh Salgaonkar int is_fadump_active(void)
1183ccc00a7SMahesh Salgaonkar {
1193ccc00a7SMahesh Salgaonkar 	return fw_dump.dump_active;
1203ccc00a7SMahesh Salgaonkar }
1213ccc00a7SMahesh Salgaonkar 
1223ccc00a7SMahesh Salgaonkar /* Print firmware assisted dump configurations for debugging purpose. */
1233ccc00a7SMahesh Salgaonkar static void fadump_show_config(void)
1243ccc00a7SMahesh Salgaonkar {
1253ccc00a7SMahesh Salgaonkar 	pr_debug("Support for firmware-assisted dump (fadump): %s\n",
1263ccc00a7SMahesh Salgaonkar 			(fw_dump.fadump_supported ? "present" : "no support"));
1273ccc00a7SMahesh Salgaonkar 
1283ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_supported)
1293ccc00a7SMahesh Salgaonkar 		return;
1303ccc00a7SMahesh Salgaonkar 
1313ccc00a7SMahesh Salgaonkar 	pr_debug("Fadump enabled    : %s\n",
1323ccc00a7SMahesh Salgaonkar 				(fw_dump.fadump_enabled ? "yes" : "no"));
1333ccc00a7SMahesh Salgaonkar 	pr_debug("Dump Active       : %s\n",
1343ccc00a7SMahesh Salgaonkar 				(fw_dump.dump_active ? "yes" : "no"));
1353ccc00a7SMahesh Salgaonkar 	pr_debug("Dump section sizes:\n");
1363ccc00a7SMahesh Salgaonkar 	pr_debug("    CPU state data size: %lx\n", fw_dump.cpu_state_data_size);
1373ccc00a7SMahesh Salgaonkar 	pr_debug("    HPTE region size   : %lx\n", fw_dump.hpte_region_size);
1383ccc00a7SMahesh Salgaonkar 	pr_debug("Boot memory size  : %lx\n", fw_dump.boot_memory_size);
1393ccc00a7SMahesh Salgaonkar }
1403ccc00a7SMahesh Salgaonkar 
1413ccc00a7SMahesh Salgaonkar static unsigned long init_fadump_mem_struct(struct fadump_mem_struct *fdm,
1423ccc00a7SMahesh Salgaonkar 				unsigned long addr)
1433ccc00a7SMahesh Salgaonkar {
1443ccc00a7SMahesh Salgaonkar 	if (!fdm)
1453ccc00a7SMahesh Salgaonkar 		return 0;
1463ccc00a7SMahesh Salgaonkar 
1473ccc00a7SMahesh Salgaonkar 	memset(fdm, 0, sizeof(struct fadump_mem_struct));
1483ccc00a7SMahesh Salgaonkar 	addr = addr & PAGE_MASK;
1493ccc00a7SMahesh Salgaonkar 
150408cddd9SHari Bathini 	fdm->header.dump_format_version = cpu_to_be32(0x00000001);
151408cddd9SHari Bathini 	fdm->header.dump_num_sections = cpu_to_be16(3);
1523ccc00a7SMahesh Salgaonkar 	fdm->header.dump_status_flag = 0;
1533ccc00a7SMahesh Salgaonkar 	fdm->header.offset_first_dump_section =
154408cddd9SHari Bathini 		cpu_to_be32((u32)offsetof(struct fadump_mem_struct, cpu_state_data));
1553ccc00a7SMahesh Salgaonkar 
1563ccc00a7SMahesh Salgaonkar 	/*
1573ccc00a7SMahesh Salgaonkar 	 * Fields for disk dump option.
1583ccc00a7SMahesh Salgaonkar 	 * We are not using disk dump option, hence set these fields to 0.
1593ccc00a7SMahesh Salgaonkar 	 */
1603ccc00a7SMahesh Salgaonkar 	fdm->header.dd_block_size = 0;
1613ccc00a7SMahesh Salgaonkar 	fdm->header.dd_block_offset = 0;
1623ccc00a7SMahesh Salgaonkar 	fdm->header.dd_num_blocks = 0;
1633ccc00a7SMahesh Salgaonkar 	fdm->header.dd_offset_disk_path = 0;
1643ccc00a7SMahesh Salgaonkar 
1653ccc00a7SMahesh Salgaonkar 	/* set 0 to disable an automatic dump-reboot. */
1663ccc00a7SMahesh Salgaonkar 	fdm->header.max_time_auto = 0;
1673ccc00a7SMahesh Salgaonkar 
1683ccc00a7SMahesh Salgaonkar 	/* Kernel dump sections */
1693ccc00a7SMahesh Salgaonkar 	/* cpu state data section. */
170408cddd9SHari Bathini 	fdm->cpu_state_data.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG);
171408cddd9SHari Bathini 	fdm->cpu_state_data.source_data_type = cpu_to_be16(FADUMP_CPU_STATE_DATA);
1723ccc00a7SMahesh Salgaonkar 	fdm->cpu_state_data.source_address = 0;
173408cddd9SHari Bathini 	fdm->cpu_state_data.source_len = cpu_to_be64(fw_dump.cpu_state_data_size);
174408cddd9SHari Bathini 	fdm->cpu_state_data.destination_address = cpu_to_be64(addr);
1753ccc00a7SMahesh Salgaonkar 	addr += fw_dump.cpu_state_data_size;
1763ccc00a7SMahesh Salgaonkar 
1773ccc00a7SMahesh Salgaonkar 	/* hpte region section */
178408cddd9SHari Bathini 	fdm->hpte_region.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG);
179408cddd9SHari Bathini 	fdm->hpte_region.source_data_type = cpu_to_be16(FADUMP_HPTE_REGION);
1803ccc00a7SMahesh Salgaonkar 	fdm->hpte_region.source_address = 0;
181408cddd9SHari Bathini 	fdm->hpte_region.source_len = cpu_to_be64(fw_dump.hpte_region_size);
182408cddd9SHari Bathini 	fdm->hpte_region.destination_address = cpu_to_be64(addr);
1833ccc00a7SMahesh Salgaonkar 	addr += fw_dump.hpte_region_size;
1843ccc00a7SMahesh Salgaonkar 
1853ccc00a7SMahesh Salgaonkar 	/* RMA region section */
186408cddd9SHari Bathini 	fdm->rmr_region.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG);
187408cddd9SHari Bathini 	fdm->rmr_region.source_data_type = cpu_to_be16(FADUMP_REAL_MODE_REGION);
188408cddd9SHari Bathini 	fdm->rmr_region.source_address = cpu_to_be64(RMA_START);
189408cddd9SHari Bathini 	fdm->rmr_region.source_len = cpu_to_be64(fw_dump.boot_memory_size);
190408cddd9SHari Bathini 	fdm->rmr_region.destination_address = cpu_to_be64(addr);
1913ccc00a7SMahesh Salgaonkar 	addr += fw_dump.boot_memory_size;
1923ccc00a7SMahesh Salgaonkar 
1933ccc00a7SMahesh Salgaonkar 	return addr;
1943ccc00a7SMahesh Salgaonkar }
1953ccc00a7SMahesh Salgaonkar 
196eb39c880SMahesh Salgaonkar /**
197eb39c880SMahesh Salgaonkar  * fadump_calculate_reserve_size(): reserve variable boot area 5% of System RAM
198eb39c880SMahesh Salgaonkar  *
199eb39c880SMahesh Salgaonkar  * Function to find the largest memory size we need to reserve during early
200eb39c880SMahesh Salgaonkar  * boot process. This will be the size of the memory that is required for a
201eb39c880SMahesh Salgaonkar  * kernel to boot successfully.
202eb39c880SMahesh Salgaonkar  *
203eb39c880SMahesh Salgaonkar  * This function has been taken from phyp-assisted dump feature implementation.
204eb39c880SMahesh Salgaonkar  *
205eb39c880SMahesh Salgaonkar  * returns larger of 256MB or 5% rounded down to multiples of 256MB.
206eb39c880SMahesh Salgaonkar  *
207eb39c880SMahesh Salgaonkar  * TODO: Come up with better approach to find out more accurate memory size
208eb39c880SMahesh Salgaonkar  * that is required for a kernel to boot successfully.
209eb39c880SMahesh Salgaonkar  *
210eb39c880SMahesh Salgaonkar  */
211eb39c880SMahesh Salgaonkar static inline unsigned long fadump_calculate_reserve_size(void)
212eb39c880SMahesh Salgaonkar {
213eb39c880SMahesh Salgaonkar 	unsigned long size;
214eb39c880SMahesh Salgaonkar 
215eb39c880SMahesh Salgaonkar 	/*
216eb39c880SMahesh Salgaonkar 	 * Check if the size is specified through fadump_reserve_mem= cmdline
217eb39c880SMahesh Salgaonkar 	 * option. If yes, then use that.
218eb39c880SMahesh Salgaonkar 	 */
219eb39c880SMahesh Salgaonkar 	if (fw_dump.reserve_bootvar)
220eb39c880SMahesh Salgaonkar 		return fw_dump.reserve_bootvar;
221eb39c880SMahesh Salgaonkar 
222eb39c880SMahesh Salgaonkar 	/* divide by 20 to get 5% of value */
223eb39c880SMahesh Salgaonkar 	size = memblock_end_of_DRAM() / 20;
224eb39c880SMahesh Salgaonkar 
225eb39c880SMahesh Salgaonkar 	/* round it down in multiples of 256 */
226eb39c880SMahesh Salgaonkar 	size = size & ~0x0FFFFFFFUL;
227eb39c880SMahesh Salgaonkar 
228eb39c880SMahesh Salgaonkar 	/* Truncate to memory_limit. We don't want to over reserve the memory.*/
229eb39c880SMahesh Salgaonkar 	if (memory_limit && size > memory_limit)
230eb39c880SMahesh Salgaonkar 		size = memory_limit;
231eb39c880SMahesh Salgaonkar 
232eb39c880SMahesh Salgaonkar 	return (size > MIN_BOOT_MEM ? size : MIN_BOOT_MEM);
233eb39c880SMahesh Salgaonkar }
234eb39c880SMahesh Salgaonkar 
235eb39c880SMahesh Salgaonkar /*
236eb39c880SMahesh Salgaonkar  * Calculate the total memory size required to be reserved for
237eb39c880SMahesh Salgaonkar  * firmware-assisted dump registration.
238eb39c880SMahesh Salgaonkar  */
239eb39c880SMahesh Salgaonkar static unsigned long get_fadump_area_size(void)
240eb39c880SMahesh Salgaonkar {
241eb39c880SMahesh Salgaonkar 	unsigned long size = 0;
242eb39c880SMahesh Salgaonkar 
243eb39c880SMahesh Salgaonkar 	size += fw_dump.cpu_state_data_size;
244eb39c880SMahesh Salgaonkar 	size += fw_dump.hpte_region_size;
245eb39c880SMahesh Salgaonkar 	size += fw_dump.boot_memory_size;
2462df173d9SMahesh Salgaonkar 	size += sizeof(struct fadump_crash_info_header);
2472df173d9SMahesh Salgaonkar 	size += sizeof(struct elfhdr); /* ELF core header.*/
248ebaeb5aeSMahesh Salgaonkar 	size += sizeof(struct elf_phdr); /* place holder for cpu notes */
2492df173d9SMahesh Salgaonkar 	/* Program headers for crash memory regions. */
2502df173d9SMahesh Salgaonkar 	size += sizeof(struct elf_phdr) * (memblock_num_regions(memory) + 2);
251eb39c880SMahesh Salgaonkar 
252eb39c880SMahesh Salgaonkar 	size = PAGE_ALIGN(size);
253eb39c880SMahesh Salgaonkar 	return size;
254eb39c880SMahesh Salgaonkar }
255eb39c880SMahesh Salgaonkar 
256eb39c880SMahesh Salgaonkar int __init fadump_reserve_mem(void)
257eb39c880SMahesh Salgaonkar {
258eb39c880SMahesh Salgaonkar 	unsigned long base, size, memory_boundary;
259eb39c880SMahesh Salgaonkar 
260eb39c880SMahesh Salgaonkar 	if (!fw_dump.fadump_enabled)
261eb39c880SMahesh Salgaonkar 		return 0;
262eb39c880SMahesh Salgaonkar 
263eb39c880SMahesh Salgaonkar 	if (!fw_dump.fadump_supported) {
264eb39c880SMahesh Salgaonkar 		printk(KERN_INFO "Firmware-assisted dump is not supported on"
265eb39c880SMahesh Salgaonkar 				" this hardware\n");
266eb39c880SMahesh Salgaonkar 		fw_dump.fadump_enabled = 0;
267eb39c880SMahesh Salgaonkar 		return 0;
268eb39c880SMahesh Salgaonkar 	}
2693ccc00a7SMahesh Salgaonkar 	/*
2703ccc00a7SMahesh Salgaonkar 	 * Initialize boot memory size
2713ccc00a7SMahesh Salgaonkar 	 * If dump is active then we have already calculated the size during
2723ccc00a7SMahesh Salgaonkar 	 * first kernel.
2733ccc00a7SMahesh Salgaonkar 	 */
2743ccc00a7SMahesh Salgaonkar 	if (fdm_active)
275408cddd9SHari Bathini 		fw_dump.boot_memory_size = be64_to_cpu(fdm_active->rmr_region.source_len);
2763ccc00a7SMahesh Salgaonkar 	else
277eb39c880SMahesh Salgaonkar 		fw_dump.boot_memory_size = fadump_calculate_reserve_size();
278eb39c880SMahesh Salgaonkar 
279eb39c880SMahesh Salgaonkar 	/*
280eb39c880SMahesh Salgaonkar 	 * Calculate the memory boundary.
281eb39c880SMahesh Salgaonkar 	 * If memory_limit is less than actual memory boundary then reserve
282eb39c880SMahesh Salgaonkar 	 * the memory for fadump beyond the memory_limit and adjust the
283eb39c880SMahesh Salgaonkar 	 * memory_limit accordingly, so that the running kernel can run with
284eb39c880SMahesh Salgaonkar 	 * specified memory_limit.
285eb39c880SMahesh Salgaonkar 	 */
286eb39c880SMahesh Salgaonkar 	if (memory_limit && memory_limit < memblock_end_of_DRAM()) {
287eb39c880SMahesh Salgaonkar 		size = get_fadump_area_size();
288eb39c880SMahesh Salgaonkar 		if ((memory_limit + size) < memblock_end_of_DRAM())
289eb39c880SMahesh Salgaonkar 			memory_limit += size;
290eb39c880SMahesh Salgaonkar 		else
291eb39c880SMahesh Salgaonkar 			memory_limit = memblock_end_of_DRAM();
292eb39c880SMahesh Salgaonkar 		printk(KERN_INFO "Adjusted memory_limit for firmware-assisted"
293a84fcd46SSuzuki Poulose 				" dump, now %#016llx\n", memory_limit);
294eb39c880SMahesh Salgaonkar 	}
295eb39c880SMahesh Salgaonkar 	if (memory_limit)
296eb39c880SMahesh Salgaonkar 		memory_boundary = memory_limit;
297eb39c880SMahesh Salgaonkar 	else
298eb39c880SMahesh Salgaonkar 		memory_boundary = memblock_end_of_DRAM();
299eb39c880SMahesh Salgaonkar 
300eb39c880SMahesh Salgaonkar 	if (fw_dump.dump_active) {
301eb39c880SMahesh Salgaonkar 		printk(KERN_INFO "Firmware-assisted dump is active.\n");
302eb39c880SMahesh Salgaonkar 		/*
303eb39c880SMahesh Salgaonkar 		 * If last boot has crashed then reserve all the memory
304eb39c880SMahesh Salgaonkar 		 * above boot_memory_size so that we don't touch it until
305eb39c880SMahesh Salgaonkar 		 * dump is written to disk by userspace tool. This memory
306eb39c880SMahesh Salgaonkar 		 * will be released for general use once the dump is saved.
307eb39c880SMahesh Salgaonkar 		 */
308eb39c880SMahesh Salgaonkar 		base = fw_dump.boot_memory_size;
309eb39c880SMahesh Salgaonkar 		size = memory_boundary - base;
310eb39c880SMahesh Salgaonkar 		memblock_reserve(base, size);
311eb39c880SMahesh Salgaonkar 		printk(KERN_INFO "Reserved %ldMB of memory at %ldMB "
312eb39c880SMahesh Salgaonkar 				"for saving crash dump\n",
313eb39c880SMahesh Salgaonkar 				(unsigned long)(size >> 20),
314eb39c880SMahesh Salgaonkar 				(unsigned long)(base >> 20));
3152df173d9SMahesh Salgaonkar 
3162df173d9SMahesh Salgaonkar 		fw_dump.fadumphdr_addr =
317408cddd9SHari Bathini 				be64_to_cpu(fdm_active->rmr_region.destination_address) +
318408cddd9SHari Bathini 				be64_to_cpu(fdm_active->rmr_region.source_len);
3192df173d9SMahesh Salgaonkar 		pr_debug("fadumphdr_addr = %p\n",
3202df173d9SMahesh Salgaonkar 				(void *) fw_dump.fadumphdr_addr);
321eb39c880SMahesh Salgaonkar 	} else {
322eb39c880SMahesh Salgaonkar 		/* Reserve the memory at the top of memory. */
323eb39c880SMahesh Salgaonkar 		size = get_fadump_area_size();
324eb39c880SMahesh Salgaonkar 		base = memory_boundary - size;
325eb39c880SMahesh Salgaonkar 		memblock_reserve(base, size);
326eb39c880SMahesh Salgaonkar 		printk(KERN_INFO "Reserved %ldMB of memory at %ldMB "
327eb39c880SMahesh Salgaonkar 				"for firmware-assisted dump\n",
328eb39c880SMahesh Salgaonkar 				(unsigned long)(size >> 20),
329eb39c880SMahesh Salgaonkar 				(unsigned long)(base >> 20));
330eb39c880SMahesh Salgaonkar 	}
331eb39c880SMahesh Salgaonkar 	fw_dump.reserve_dump_area_start = base;
332eb39c880SMahesh Salgaonkar 	fw_dump.reserve_dump_area_size = size;
333eb39c880SMahesh Salgaonkar 	return 1;
334eb39c880SMahesh Salgaonkar }
335eb39c880SMahesh Salgaonkar 
336eb39c880SMahesh Salgaonkar /* Look for fadump= cmdline option. */
337eb39c880SMahesh Salgaonkar static int __init early_fadump_param(char *p)
338eb39c880SMahesh Salgaonkar {
339eb39c880SMahesh Salgaonkar 	if (!p)
340eb39c880SMahesh Salgaonkar 		return 1;
341eb39c880SMahesh Salgaonkar 
342eb39c880SMahesh Salgaonkar 	if (strncmp(p, "on", 2) == 0)
343eb39c880SMahesh Salgaonkar 		fw_dump.fadump_enabled = 1;
344eb39c880SMahesh Salgaonkar 	else if (strncmp(p, "off", 3) == 0)
345eb39c880SMahesh Salgaonkar 		fw_dump.fadump_enabled = 0;
346eb39c880SMahesh Salgaonkar 
347eb39c880SMahesh Salgaonkar 	return 0;
348eb39c880SMahesh Salgaonkar }
349eb39c880SMahesh Salgaonkar early_param("fadump", early_fadump_param);
350eb39c880SMahesh Salgaonkar 
351eb39c880SMahesh Salgaonkar /* Look for fadump_reserve_mem= cmdline option */
352eb39c880SMahesh Salgaonkar static int __init early_fadump_reserve_mem(char *p)
353eb39c880SMahesh Salgaonkar {
354eb39c880SMahesh Salgaonkar 	if (p)
355eb39c880SMahesh Salgaonkar 		fw_dump.reserve_bootvar = memparse(p, &p);
356eb39c880SMahesh Salgaonkar 	return 0;
357eb39c880SMahesh Salgaonkar }
358eb39c880SMahesh Salgaonkar early_param("fadump_reserve_mem", early_fadump_reserve_mem);
3593ccc00a7SMahesh Salgaonkar 
3603ccc00a7SMahesh Salgaonkar static void register_fw_dump(struct fadump_mem_struct *fdm)
3613ccc00a7SMahesh Salgaonkar {
3623ccc00a7SMahesh Salgaonkar 	int rc;
3633ccc00a7SMahesh Salgaonkar 	unsigned int wait_time;
3643ccc00a7SMahesh Salgaonkar 
3653ccc00a7SMahesh Salgaonkar 	pr_debug("Registering for firmware-assisted kernel dump...\n");
3663ccc00a7SMahesh Salgaonkar 
3673ccc00a7SMahesh Salgaonkar 	/* TODO: Add upper time limit for the delay */
3683ccc00a7SMahesh Salgaonkar 	do {
3693ccc00a7SMahesh Salgaonkar 		rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
3703ccc00a7SMahesh Salgaonkar 			FADUMP_REGISTER, fdm,
3713ccc00a7SMahesh Salgaonkar 			sizeof(struct fadump_mem_struct));
3723ccc00a7SMahesh Salgaonkar 
3733ccc00a7SMahesh Salgaonkar 		wait_time = rtas_busy_delay_time(rc);
3743ccc00a7SMahesh Salgaonkar 		if (wait_time)
3753ccc00a7SMahesh Salgaonkar 			mdelay(wait_time);
3763ccc00a7SMahesh Salgaonkar 
3773ccc00a7SMahesh Salgaonkar 	} while (wait_time);
3783ccc00a7SMahesh Salgaonkar 
3793ccc00a7SMahesh Salgaonkar 	switch (rc) {
3803ccc00a7SMahesh Salgaonkar 	case -1:
3813ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "Failed to register firmware-assisted kernel"
3823ccc00a7SMahesh Salgaonkar 			" dump. Hardware Error(%d).\n", rc);
3833ccc00a7SMahesh Salgaonkar 		break;
3843ccc00a7SMahesh Salgaonkar 	case -3:
3853ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "Failed to register firmware-assisted kernel"
3863ccc00a7SMahesh Salgaonkar 			" dump. Parameter Error(%d).\n", rc);
3873ccc00a7SMahesh Salgaonkar 		break;
3883ccc00a7SMahesh Salgaonkar 	case -9:
3893ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "firmware-assisted kernel dump is already "
3903ccc00a7SMahesh Salgaonkar 			" registered.");
3913ccc00a7SMahesh Salgaonkar 		fw_dump.dump_registered = 1;
3923ccc00a7SMahesh Salgaonkar 		break;
3933ccc00a7SMahesh Salgaonkar 	case 0:
3943ccc00a7SMahesh Salgaonkar 		printk(KERN_INFO "firmware-assisted kernel dump registration"
3953ccc00a7SMahesh Salgaonkar 			" is successful\n");
3963ccc00a7SMahesh Salgaonkar 		fw_dump.dump_registered = 1;
3973ccc00a7SMahesh Salgaonkar 		break;
3983ccc00a7SMahesh Salgaonkar 	}
3993ccc00a7SMahesh Salgaonkar }
4003ccc00a7SMahesh Salgaonkar 
401ebaeb5aeSMahesh Salgaonkar void crash_fadump(struct pt_regs *regs, const char *str)
402ebaeb5aeSMahesh Salgaonkar {
403ebaeb5aeSMahesh Salgaonkar 	struct fadump_crash_info_header *fdh = NULL;
404ebaeb5aeSMahesh Salgaonkar 
405ebaeb5aeSMahesh Salgaonkar 	if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr)
406ebaeb5aeSMahesh Salgaonkar 		return;
407ebaeb5aeSMahesh Salgaonkar 
408ebaeb5aeSMahesh Salgaonkar 	fdh = __va(fw_dump.fadumphdr_addr);
409ebaeb5aeSMahesh Salgaonkar 	crashing_cpu = smp_processor_id();
410ebaeb5aeSMahesh Salgaonkar 	fdh->crashing_cpu = crashing_cpu;
411ebaeb5aeSMahesh Salgaonkar 	crash_save_vmcoreinfo();
412ebaeb5aeSMahesh Salgaonkar 
413ebaeb5aeSMahesh Salgaonkar 	if (regs)
414ebaeb5aeSMahesh Salgaonkar 		fdh->regs = *regs;
415ebaeb5aeSMahesh Salgaonkar 	else
416ebaeb5aeSMahesh Salgaonkar 		ppc_save_regs(&fdh->regs);
417ebaeb5aeSMahesh Salgaonkar 
418a0512164SRasmus Villemoes 	fdh->online_mask = *cpu_online_mask;
419ebaeb5aeSMahesh Salgaonkar 
420ebaeb5aeSMahesh Salgaonkar 	/* Call ibm,os-term rtas call to trigger firmware assisted dump */
421ebaeb5aeSMahesh Salgaonkar 	rtas_os_term((char *)str);
422ebaeb5aeSMahesh Salgaonkar }
423ebaeb5aeSMahesh Salgaonkar 
424ebaeb5aeSMahesh Salgaonkar #define GPR_MASK	0xffffff0000000000
425ebaeb5aeSMahesh Salgaonkar static inline int fadump_gpr_index(u64 id)
426ebaeb5aeSMahesh Salgaonkar {
427ebaeb5aeSMahesh Salgaonkar 	int i = -1;
428ebaeb5aeSMahesh Salgaonkar 	char str[3];
429ebaeb5aeSMahesh Salgaonkar 
430ebaeb5aeSMahesh Salgaonkar 	if ((id & GPR_MASK) == REG_ID("GPR")) {
431ebaeb5aeSMahesh Salgaonkar 		/* get the digits at the end */
432ebaeb5aeSMahesh Salgaonkar 		id &= ~GPR_MASK;
433ebaeb5aeSMahesh Salgaonkar 		id >>= 24;
434ebaeb5aeSMahesh Salgaonkar 		str[2] = '\0';
435ebaeb5aeSMahesh Salgaonkar 		str[1] = id & 0xff;
436ebaeb5aeSMahesh Salgaonkar 		str[0] = (id >> 8) & 0xff;
437ebaeb5aeSMahesh Salgaonkar 		sscanf(str, "%d", &i);
438ebaeb5aeSMahesh Salgaonkar 		if (i > 31)
439ebaeb5aeSMahesh Salgaonkar 			i = -1;
440ebaeb5aeSMahesh Salgaonkar 	}
441ebaeb5aeSMahesh Salgaonkar 	return i;
442ebaeb5aeSMahesh Salgaonkar }
443ebaeb5aeSMahesh Salgaonkar 
444ebaeb5aeSMahesh Salgaonkar static inline void fadump_set_regval(struct pt_regs *regs, u64 reg_id,
445ebaeb5aeSMahesh Salgaonkar 								u64 reg_val)
446ebaeb5aeSMahesh Salgaonkar {
447ebaeb5aeSMahesh Salgaonkar 	int i;
448ebaeb5aeSMahesh Salgaonkar 
449ebaeb5aeSMahesh Salgaonkar 	i = fadump_gpr_index(reg_id);
450ebaeb5aeSMahesh Salgaonkar 	if (i >= 0)
451ebaeb5aeSMahesh Salgaonkar 		regs->gpr[i] = (unsigned long)reg_val;
452ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("NIA"))
453ebaeb5aeSMahesh Salgaonkar 		regs->nip = (unsigned long)reg_val;
454ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("MSR"))
455ebaeb5aeSMahesh Salgaonkar 		regs->msr = (unsigned long)reg_val;
456ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("CTR"))
457ebaeb5aeSMahesh Salgaonkar 		regs->ctr = (unsigned long)reg_val;
458ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("LR"))
459ebaeb5aeSMahesh Salgaonkar 		regs->link = (unsigned long)reg_val;
460ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("XER"))
461ebaeb5aeSMahesh Salgaonkar 		regs->xer = (unsigned long)reg_val;
462ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("CR"))
463ebaeb5aeSMahesh Salgaonkar 		regs->ccr = (unsigned long)reg_val;
464ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("DAR"))
465ebaeb5aeSMahesh Salgaonkar 		regs->dar = (unsigned long)reg_val;
466ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("DSISR"))
467ebaeb5aeSMahesh Salgaonkar 		regs->dsisr = (unsigned long)reg_val;
468ebaeb5aeSMahesh Salgaonkar }
469ebaeb5aeSMahesh Salgaonkar 
470ebaeb5aeSMahesh Salgaonkar static struct fadump_reg_entry*
471ebaeb5aeSMahesh Salgaonkar fadump_read_registers(struct fadump_reg_entry *reg_entry, struct pt_regs *regs)
472ebaeb5aeSMahesh Salgaonkar {
473ebaeb5aeSMahesh Salgaonkar 	memset(regs, 0, sizeof(struct pt_regs));
474ebaeb5aeSMahesh Salgaonkar 
475408cddd9SHari Bathini 	while (be64_to_cpu(reg_entry->reg_id) != REG_ID("CPUEND")) {
476408cddd9SHari Bathini 		fadump_set_regval(regs, be64_to_cpu(reg_entry->reg_id),
477408cddd9SHari Bathini 					be64_to_cpu(reg_entry->reg_value));
478ebaeb5aeSMahesh Salgaonkar 		reg_entry++;
479ebaeb5aeSMahesh Salgaonkar 	}
480ebaeb5aeSMahesh Salgaonkar 	reg_entry++;
481ebaeb5aeSMahesh Salgaonkar 	return reg_entry;
482ebaeb5aeSMahesh Salgaonkar }
483ebaeb5aeSMahesh Salgaonkar 
484ebaeb5aeSMahesh Salgaonkar static u32 *fadump_append_elf_note(u32 *buf, char *name, unsigned type,
485ebaeb5aeSMahesh Salgaonkar 						void *data, size_t data_len)
486ebaeb5aeSMahesh Salgaonkar {
487ebaeb5aeSMahesh Salgaonkar 	struct elf_note note;
488ebaeb5aeSMahesh Salgaonkar 
489ebaeb5aeSMahesh Salgaonkar 	note.n_namesz = strlen(name) + 1;
490ebaeb5aeSMahesh Salgaonkar 	note.n_descsz = data_len;
491ebaeb5aeSMahesh Salgaonkar 	note.n_type   = type;
492ebaeb5aeSMahesh Salgaonkar 	memcpy(buf, &note, sizeof(note));
493ebaeb5aeSMahesh Salgaonkar 	buf += (sizeof(note) + 3)/4;
494ebaeb5aeSMahesh Salgaonkar 	memcpy(buf, name, note.n_namesz);
495ebaeb5aeSMahesh Salgaonkar 	buf += (note.n_namesz + 3)/4;
496ebaeb5aeSMahesh Salgaonkar 	memcpy(buf, data, note.n_descsz);
497ebaeb5aeSMahesh Salgaonkar 	buf += (note.n_descsz + 3)/4;
498ebaeb5aeSMahesh Salgaonkar 
499ebaeb5aeSMahesh Salgaonkar 	return buf;
500ebaeb5aeSMahesh Salgaonkar }
501ebaeb5aeSMahesh Salgaonkar 
502ebaeb5aeSMahesh Salgaonkar static void fadump_final_note(u32 *buf)
503ebaeb5aeSMahesh Salgaonkar {
504ebaeb5aeSMahesh Salgaonkar 	struct elf_note note;
505ebaeb5aeSMahesh Salgaonkar 
506ebaeb5aeSMahesh Salgaonkar 	note.n_namesz = 0;
507ebaeb5aeSMahesh Salgaonkar 	note.n_descsz = 0;
508ebaeb5aeSMahesh Salgaonkar 	note.n_type   = 0;
509ebaeb5aeSMahesh Salgaonkar 	memcpy(buf, &note, sizeof(note));
510ebaeb5aeSMahesh Salgaonkar }
511ebaeb5aeSMahesh Salgaonkar 
512ebaeb5aeSMahesh Salgaonkar static u32 *fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs)
513ebaeb5aeSMahesh Salgaonkar {
514ebaeb5aeSMahesh Salgaonkar 	struct elf_prstatus prstatus;
515ebaeb5aeSMahesh Salgaonkar 
516ebaeb5aeSMahesh Salgaonkar 	memset(&prstatus, 0, sizeof(prstatus));
517ebaeb5aeSMahesh Salgaonkar 	/*
518ebaeb5aeSMahesh Salgaonkar 	 * FIXME: How do i get PID? Do I really need it?
519ebaeb5aeSMahesh Salgaonkar 	 * prstatus.pr_pid = ????
520ebaeb5aeSMahesh Salgaonkar 	 */
521ebaeb5aeSMahesh Salgaonkar 	elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
522ebaeb5aeSMahesh Salgaonkar 	buf = fadump_append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS,
523ebaeb5aeSMahesh Salgaonkar 				&prstatus, sizeof(prstatus));
524ebaeb5aeSMahesh Salgaonkar 	return buf;
525ebaeb5aeSMahesh Salgaonkar }
526ebaeb5aeSMahesh Salgaonkar 
527ebaeb5aeSMahesh Salgaonkar static void fadump_update_elfcore_header(char *bufp)
528ebaeb5aeSMahesh Salgaonkar {
529ebaeb5aeSMahesh Salgaonkar 	struct elfhdr *elf;
530ebaeb5aeSMahesh Salgaonkar 	struct elf_phdr *phdr;
531ebaeb5aeSMahesh Salgaonkar 
532ebaeb5aeSMahesh Salgaonkar 	elf = (struct elfhdr *)bufp;
533ebaeb5aeSMahesh Salgaonkar 	bufp += sizeof(struct elfhdr);
534ebaeb5aeSMahesh Salgaonkar 
535ebaeb5aeSMahesh Salgaonkar 	/* First note is a place holder for cpu notes info. */
536ebaeb5aeSMahesh Salgaonkar 	phdr = (struct elf_phdr *)bufp;
537ebaeb5aeSMahesh Salgaonkar 
538ebaeb5aeSMahesh Salgaonkar 	if (phdr->p_type == PT_NOTE) {
539ebaeb5aeSMahesh Salgaonkar 		phdr->p_paddr = fw_dump.cpu_notes_buf;
540ebaeb5aeSMahesh Salgaonkar 		phdr->p_offset	= phdr->p_paddr;
541ebaeb5aeSMahesh Salgaonkar 		phdr->p_filesz	= fw_dump.cpu_notes_buf_size;
542ebaeb5aeSMahesh Salgaonkar 		phdr->p_memsz = fw_dump.cpu_notes_buf_size;
543ebaeb5aeSMahesh Salgaonkar 	}
544ebaeb5aeSMahesh Salgaonkar 	return;
545ebaeb5aeSMahesh Salgaonkar }
546ebaeb5aeSMahesh Salgaonkar 
547ebaeb5aeSMahesh Salgaonkar static void *fadump_cpu_notes_buf_alloc(unsigned long size)
548ebaeb5aeSMahesh Salgaonkar {
549ebaeb5aeSMahesh Salgaonkar 	void *vaddr;
550ebaeb5aeSMahesh Salgaonkar 	struct page *page;
551ebaeb5aeSMahesh Salgaonkar 	unsigned long order, count, i;
552ebaeb5aeSMahesh Salgaonkar 
553ebaeb5aeSMahesh Salgaonkar 	order = get_order(size);
554ebaeb5aeSMahesh Salgaonkar 	vaddr = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
555ebaeb5aeSMahesh Salgaonkar 	if (!vaddr)
556ebaeb5aeSMahesh Salgaonkar 		return NULL;
557ebaeb5aeSMahesh Salgaonkar 
558ebaeb5aeSMahesh Salgaonkar 	count = 1 << order;
559ebaeb5aeSMahesh Salgaonkar 	page = virt_to_page(vaddr);
560ebaeb5aeSMahesh Salgaonkar 	for (i = 0; i < count; i++)
561ebaeb5aeSMahesh Salgaonkar 		SetPageReserved(page + i);
562ebaeb5aeSMahesh Salgaonkar 	return vaddr;
563ebaeb5aeSMahesh Salgaonkar }
564ebaeb5aeSMahesh Salgaonkar 
565ebaeb5aeSMahesh Salgaonkar static void fadump_cpu_notes_buf_free(unsigned long vaddr, unsigned long size)
566ebaeb5aeSMahesh Salgaonkar {
567ebaeb5aeSMahesh Salgaonkar 	struct page *page;
568ebaeb5aeSMahesh Salgaonkar 	unsigned long order, count, i;
569ebaeb5aeSMahesh Salgaonkar 
570ebaeb5aeSMahesh Salgaonkar 	order = get_order(size);
571ebaeb5aeSMahesh Salgaonkar 	count = 1 << order;
572ebaeb5aeSMahesh Salgaonkar 	page = virt_to_page(vaddr);
573ebaeb5aeSMahesh Salgaonkar 	for (i = 0; i < count; i++)
574ebaeb5aeSMahesh Salgaonkar 		ClearPageReserved(page + i);
575ebaeb5aeSMahesh Salgaonkar 	__free_pages(page, order);
576ebaeb5aeSMahesh Salgaonkar }
577ebaeb5aeSMahesh Salgaonkar 
578ebaeb5aeSMahesh Salgaonkar /*
579ebaeb5aeSMahesh Salgaonkar  * Read CPU state dump data and convert it into ELF notes.
580ebaeb5aeSMahesh Salgaonkar  * The CPU dump starts with magic number "REGSAVE". NumCpusOffset should be
581ebaeb5aeSMahesh Salgaonkar  * used to access the data to allow for additional fields to be added without
582ebaeb5aeSMahesh Salgaonkar  * affecting compatibility. Each list of registers for a CPU starts with
583ebaeb5aeSMahesh Salgaonkar  * "CPUSTRT" and ends with "CPUEND". Each register entry is of 16 bytes,
584ebaeb5aeSMahesh Salgaonkar  * 8 Byte ASCII identifier and 8 Byte register value. The register entry
585ebaeb5aeSMahesh Salgaonkar  * with identifier "CPUSTRT" and "CPUEND" contains 4 byte cpu id as part
586ebaeb5aeSMahesh Salgaonkar  * of register value. For more details refer to PAPR document.
587ebaeb5aeSMahesh Salgaonkar  *
588ebaeb5aeSMahesh Salgaonkar  * Only for the crashing cpu we ignore the CPU dump data and get exact
589ebaeb5aeSMahesh Salgaonkar  * state from fadump crash info structure populated by first kernel at the
590ebaeb5aeSMahesh Salgaonkar  * time of crash.
591ebaeb5aeSMahesh Salgaonkar  */
592ebaeb5aeSMahesh Salgaonkar static int __init fadump_build_cpu_notes(const struct fadump_mem_struct *fdm)
593ebaeb5aeSMahesh Salgaonkar {
594ebaeb5aeSMahesh Salgaonkar 	struct fadump_reg_save_area_header *reg_header;
595ebaeb5aeSMahesh Salgaonkar 	struct fadump_reg_entry *reg_entry;
596ebaeb5aeSMahesh Salgaonkar 	struct fadump_crash_info_header *fdh = NULL;
597ebaeb5aeSMahesh Salgaonkar 	void *vaddr;
598ebaeb5aeSMahesh Salgaonkar 	unsigned long addr;
599ebaeb5aeSMahesh Salgaonkar 	u32 num_cpus, *note_buf;
600ebaeb5aeSMahesh Salgaonkar 	struct pt_regs regs;
601ebaeb5aeSMahesh Salgaonkar 	int i, rc = 0, cpu = 0;
602ebaeb5aeSMahesh Salgaonkar 
603ebaeb5aeSMahesh Salgaonkar 	if (!fdm->cpu_state_data.bytes_dumped)
604ebaeb5aeSMahesh Salgaonkar 		return -EINVAL;
605ebaeb5aeSMahesh Salgaonkar 
606408cddd9SHari Bathini 	addr = be64_to_cpu(fdm->cpu_state_data.destination_address);
607ebaeb5aeSMahesh Salgaonkar 	vaddr = __va(addr);
608ebaeb5aeSMahesh Salgaonkar 
609ebaeb5aeSMahesh Salgaonkar 	reg_header = vaddr;
610408cddd9SHari Bathini 	if (be64_to_cpu(reg_header->magic_number) != REGSAVE_AREA_MAGIC) {
611ebaeb5aeSMahesh Salgaonkar 		printk(KERN_ERR "Unable to read register save area.\n");
612ebaeb5aeSMahesh Salgaonkar 		return -ENOENT;
613ebaeb5aeSMahesh Salgaonkar 	}
614ebaeb5aeSMahesh Salgaonkar 	pr_debug("--------CPU State Data------------\n");
615408cddd9SHari Bathini 	pr_debug("Magic Number: %llx\n", be64_to_cpu(reg_header->magic_number));
616408cddd9SHari Bathini 	pr_debug("NumCpuOffset: %x\n", be32_to_cpu(reg_header->num_cpu_offset));
617ebaeb5aeSMahesh Salgaonkar 
618408cddd9SHari Bathini 	vaddr += be32_to_cpu(reg_header->num_cpu_offset);
619408cddd9SHari Bathini 	num_cpus = be32_to_cpu(*((__be32 *)(vaddr)));
620ebaeb5aeSMahesh Salgaonkar 	pr_debug("NumCpus     : %u\n", num_cpus);
621ebaeb5aeSMahesh Salgaonkar 	vaddr += sizeof(u32);
622ebaeb5aeSMahesh Salgaonkar 	reg_entry = (struct fadump_reg_entry *)vaddr;
623ebaeb5aeSMahesh Salgaonkar 
624ebaeb5aeSMahesh Salgaonkar 	/* Allocate buffer to hold cpu crash notes. */
625ebaeb5aeSMahesh Salgaonkar 	fw_dump.cpu_notes_buf_size = num_cpus * sizeof(note_buf_t);
626ebaeb5aeSMahesh Salgaonkar 	fw_dump.cpu_notes_buf_size = PAGE_ALIGN(fw_dump.cpu_notes_buf_size);
627ebaeb5aeSMahesh Salgaonkar 	note_buf = fadump_cpu_notes_buf_alloc(fw_dump.cpu_notes_buf_size);
628ebaeb5aeSMahesh Salgaonkar 	if (!note_buf) {
629ebaeb5aeSMahesh Salgaonkar 		printk(KERN_ERR "Failed to allocate 0x%lx bytes for "
630ebaeb5aeSMahesh Salgaonkar 			"cpu notes buffer\n", fw_dump.cpu_notes_buf_size);
631ebaeb5aeSMahesh Salgaonkar 		return -ENOMEM;
632ebaeb5aeSMahesh Salgaonkar 	}
633ebaeb5aeSMahesh Salgaonkar 	fw_dump.cpu_notes_buf = __pa(note_buf);
634ebaeb5aeSMahesh Salgaonkar 
635ebaeb5aeSMahesh Salgaonkar 	pr_debug("Allocated buffer for cpu notes of size %ld at %p\n",
636ebaeb5aeSMahesh Salgaonkar 			(num_cpus * sizeof(note_buf_t)), note_buf);
637ebaeb5aeSMahesh Salgaonkar 
638ebaeb5aeSMahesh Salgaonkar 	if (fw_dump.fadumphdr_addr)
639ebaeb5aeSMahesh Salgaonkar 		fdh = __va(fw_dump.fadumphdr_addr);
640ebaeb5aeSMahesh Salgaonkar 
641ebaeb5aeSMahesh Salgaonkar 	for (i = 0; i < num_cpus; i++) {
642408cddd9SHari Bathini 		if (be64_to_cpu(reg_entry->reg_id) != REG_ID("CPUSTRT")) {
643ebaeb5aeSMahesh Salgaonkar 			printk(KERN_ERR "Unable to read CPU state data\n");
644ebaeb5aeSMahesh Salgaonkar 			rc = -ENOENT;
645ebaeb5aeSMahesh Salgaonkar 			goto error_out;
646ebaeb5aeSMahesh Salgaonkar 		}
647ebaeb5aeSMahesh Salgaonkar 		/* Lower 4 bytes of reg_value contains logical cpu id */
648408cddd9SHari Bathini 		cpu = be64_to_cpu(reg_entry->reg_value) & FADUMP_CPU_ID_MASK;
649a0512164SRasmus Villemoes 		if (fdh && !cpumask_test_cpu(cpu, &fdh->online_mask)) {
650ebaeb5aeSMahesh Salgaonkar 			SKIP_TO_NEXT_CPU(reg_entry);
651ebaeb5aeSMahesh Salgaonkar 			continue;
652ebaeb5aeSMahesh Salgaonkar 		}
653ebaeb5aeSMahesh Salgaonkar 		pr_debug("Reading register data for cpu %d...\n", cpu);
654ebaeb5aeSMahesh Salgaonkar 		if (fdh && fdh->crashing_cpu == cpu) {
655ebaeb5aeSMahesh Salgaonkar 			regs = fdh->regs;
656ebaeb5aeSMahesh Salgaonkar 			note_buf = fadump_regs_to_elf_notes(note_buf, &regs);
657ebaeb5aeSMahesh Salgaonkar 			SKIP_TO_NEXT_CPU(reg_entry);
658ebaeb5aeSMahesh Salgaonkar 		} else {
659ebaeb5aeSMahesh Salgaonkar 			reg_entry++;
660ebaeb5aeSMahesh Salgaonkar 			reg_entry = fadump_read_registers(reg_entry, &regs);
661ebaeb5aeSMahesh Salgaonkar 			note_buf = fadump_regs_to_elf_notes(note_buf, &regs);
662ebaeb5aeSMahesh Salgaonkar 		}
663ebaeb5aeSMahesh Salgaonkar 	}
664ebaeb5aeSMahesh Salgaonkar 	fadump_final_note(note_buf);
665ebaeb5aeSMahesh Salgaonkar 
666b717d985SRickard Strandqvist 	if (fdh) {
667ebaeb5aeSMahesh Salgaonkar 		pr_debug("Updating elfcore header (%llx) with cpu notes\n",
668ebaeb5aeSMahesh Salgaonkar 							fdh->elfcorehdr_addr);
669ebaeb5aeSMahesh Salgaonkar 		fadump_update_elfcore_header((char *)__va(fdh->elfcorehdr_addr));
670b717d985SRickard Strandqvist 	}
671ebaeb5aeSMahesh Salgaonkar 	return 0;
672ebaeb5aeSMahesh Salgaonkar 
673ebaeb5aeSMahesh Salgaonkar error_out:
674ebaeb5aeSMahesh Salgaonkar 	fadump_cpu_notes_buf_free((unsigned long)__va(fw_dump.cpu_notes_buf),
675ebaeb5aeSMahesh Salgaonkar 					fw_dump.cpu_notes_buf_size);
676ebaeb5aeSMahesh Salgaonkar 	fw_dump.cpu_notes_buf = 0;
677ebaeb5aeSMahesh Salgaonkar 	fw_dump.cpu_notes_buf_size = 0;
678ebaeb5aeSMahesh Salgaonkar 	return rc;
679ebaeb5aeSMahesh Salgaonkar 
680ebaeb5aeSMahesh Salgaonkar }
681ebaeb5aeSMahesh Salgaonkar 
6822df173d9SMahesh Salgaonkar /*
6832df173d9SMahesh Salgaonkar  * Validate and process the dump data stored by firmware before exporting
6842df173d9SMahesh Salgaonkar  * it through '/proc/vmcore'.
6852df173d9SMahesh Salgaonkar  */
6862df173d9SMahesh Salgaonkar static int __init process_fadump(const struct fadump_mem_struct *fdm_active)
6872df173d9SMahesh Salgaonkar {
6882df173d9SMahesh Salgaonkar 	struct fadump_crash_info_header *fdh;
689ebaeb5aeSMahesh Salgaonkar 	int rc = 0;
6902df173d9SMahesh Salgaonkar 
6912df173d9SMahesh Salgaonkar 	if (!fdm_active || !fw_dump.fadumphdr_addr)
6922df173d9SMahesh Salgaonkar 		return -EINVAL;
6932df173d9SMahesh Salgaonkar 
6942df173d9SMahesh Salgaonkar 	/* Check if the dump data is valid. */
695408cddd9SHari Bathini 	if ((be16_to_cpu(fdm_active->header.dump_status_flag) == FADUMP_ERROR_FLAG) ||
696ebaeb5aeSMahesh Salgaonkar 			(fdm_active->cpu_state_data.error_flags != 0) ||
6972df173d9SMahesh Salgaonkar 			(fdm_active->rmr_region.error_flags != 0)) {
6982df173d9SMahesh Salgaonkar 		printk(KERN_ERR "Dump taken by platform is not valid\n");
6992df173d9SMahesh Salgaonkar 		return -EINVAL;
7002df173d9SMahesh Salgaonkar 	}
701ebaeb5aeSMahesh Salgaonkar 	if ((fdm_active->rmr_region.bytes_dumped !=
702ebaeb5aeSMahesh Salgaonkar 			fdm_active->rmr_region.source_len) ||
703ebaeb5aeSMahesh Salgaonkar 			!fdm_active->cpu_state_data.bytes_dumped) {
7042df173d9SMahesh Salgaonkar 		printk(KERN_ERR "Dump taken by platform is incomplete\n");
7052df173d9SMahesh Salgaonkar 		return -EINVAL;
7062df173d9SMahesh Salgaonkar 	}
7072df173d9SMahesh Salgaonkar 
7082df173d9SMahesh Salgaonkar 	/* Validate the fadump crash info header */
7092df173d9SMahesh Salgaonkar 	fdh = __va(fw_dump.fadumphdr_addr);
7102df173d9SMahesh Salgaonkar 	if (fdh->magic_number != FADUMP_CRASH_INFO_MAGIC) {
7112df173d9SMahesh Salgaonkar 		printk(KERN_ERR "Crash info header is not valid.\n");
7122df173d9SMahesh Salgaonkar 		return -EINVAL;
7132df173d9SMahesh Salgaonkar 	}
7142df173d9SMahesh Salgaonkar 
715ebaeb5aeSMahesh Salgaonkar 	rc = fadump_build_cpu_notes(fdm_active);
716ebaeb5aeSMahesh Salgaonkar 	if (rc)
717ebaeb5aeSMahesh Salgaonkar 		return rc;
718ebaeb5aeSMahesh Salgaonkar 
7192df173d9SMahesh Salgaonkar 	/*
7202df173d9SMahesh Salgaonkar 	 * We are done validating dump info and elfcore header is now ready
7212df173d9SMahesh Salgaonkar 	 * to be exported. set elfcorehdr_addr so that vmcore module will
7222df173d9SMahesh Salgaonkar 	 * export the elfcore header through '/proc/vmcore'.
7232df173d9SMahesh Salgaonkar 	 */
7242df173d9SMahesh Salgaonkar 	elfcorehdr_addr = fdh->elfcorehdr_addr;
7252df173d9SMahesh Salgaonkar 
7262df173d9SMahesh Salgaonkar 	return 0;
7272df173d9SMahesh Salgaonkar }
7282df173d9SMahesh Salgaonkar 
7292df173d9SMahesh Salgaonkar static inline void fadump_add_crash_memory(unsigned long long base,
7302df173d9SMahesh Salgaonkar 					unsigned long long end)
7312df173d9SMahesh Salgaonkar {
7322df173d9SMahesh Salgaonkar 	if (base == end)
7332df173d9SMahesh Salgaonkar 		return;
7342df173d9SMahesh Salgaonkar 
7352df173d9SMahesh Salgaonkar 	pr_debug("crash_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n",
7362df173d9SMahesh Salgaonkar 		crash_mem_ranges, base, end - 1, (end - base));
7372df173d9SMahesh Salgaonkar 	crash_memory_ranges[crash_mem_ranges].base = base;
7382df173d9SMahesh Salgaonkar 	crash_memory_ranges[crash_mem_ranges].size = end - base;
7392df173d9SMahesh Salgaonkar 	crash_mem_ranges++;
7402df173d9SMahesh Salgaonkar }
7412df173d9SMahesh Salgaonkar 
7422df173d9SMahesh Salgaonkar static void fadump_exclude_reserved_area(unsigned long long start,
7432df173d9SMahesh Salgaonkar 					unsigned long long end)
7442df173d9SMahesh Salgaonkar {
7452df173d9SMahesh Salgaonkar 	unsigned long long ra_start, ra_end;
7462df173d9SMahesh Salgaonkar 
7472df173d9SMahesh Salgaonkar 	ra_start = fw_dump.reserve_dump_area_start;
7482df173d9SMahesh Salgaonkar 	ra_end = ra_start + fw_dump.reserve_dump_area_size;
7492df173d9SMahesh Salgaonkar 
7502df173d9SMahesh Salgaonkar 	if ((ra_start < end) && (ra_end > start)) {
7512df173d9SMahesh Salgaonkar 		if ((start < ra_start) && (end > ra_end)) {
7522df173d9SMahesh Salgaonkar 			fadump_add_crash_memory(start, ra_start);
7532df173d9SMahesh Salgaonkar 			fadump_add_crash_memory(ra_end, end);
7542df173d9SMahesh Salgaonkar 		} else if (start < ra_start) {
7552df173d9SMahesh Salgaonkar 			fadump_add_crash_memory(start, ra_start);
7562df173d9SMahesh Salgaonkar 		} else if (ra_end < end) {
7572df173d9SMahesh Salgaonkar 			fadump_add_crash_memory(ra_end, end);
7582df173d9SMahesh Salgaonkar 		}
7592df173d9SMahesh Salgaonkar 	} else
7602df173d9SMahesh Salgaonkar 		fadump_add_crash_memory(start, end);
7612df173d9SMahesh Salgaonkar }
7622df173d9SMahesh Salgaonkar 
7632df173d9SMahesh Salgaonkar static int fadump_init_elfcore_header(char *bufp)
7642df173d9SMahesh Salgaonkar {
7652df173d9SMahesh Salgaonkar 	struct elfhdr *elf;
7662df173d9SMahesh Salgaonkar 
7672df173d9SMahesh Salgaonkar 	elf = (struct elfhdr *) bufp;
7682df173d9SMahesh Salgaonkar 	bufp += sizeof(struct elfhdr);
7692df173d9SMahesh Salgaonkar 	memcpy(elf->e_ident, ELFMAG, SELFMAG);
7702df173d9SMahesh Salgaonkar 	elf->e_ident[EI_CLASS] = ELF_CLASS;
7712df173d9SMahesh Salgaonkar 	elf->e_ident[EI_DATA] = ELF_DATA;
7722df173d9SMahesh Salgaonkar 	elf->e_ident[EI_VERSION] = EV_CURRENT;
7732df173d9SMahesh Salgaonkar 	elf->e_ident[EI_OSABI] = ELF_OSABI;
7742df173d9SMahesh Salgaonkar 	memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
7752df173d9SMahesh Salgaonkar 	elf->e_type = ET_CORE;
7762df173d9SMahesh Salgaonkar 	elf->e_machine = ELF_ARCH;
7772df173d9SMahesh Salgaonkar 	elf->e_version = EV_CURRENT;
7782df173d9SMahesh Salgaonkar 	elf->e_entry = 0;
7792df173d9SMahesh Salgaonkar 	elf->e_phoff = sizeof(struct elfhdr);
7802df173d9SMahesh Salgaonkar 	elf->e_shoff = 0;
781*d8bced27SDaniel Axtens #if defined(_CALL_ELF)
782*d8bced27SDaniel Axtens 	elf->e_flags = _CALL_ELF;
783*d8bced27SDaniel Axtens #else
784*d8bced27SDaniel Axtens 	elf->e_flags = 0;
785*d8bced27SDaniel Axtens #endif
7862df173d9SMahesh Salgaonkar 	elf->e_ehsize = sizeof(struct elfhdr);
7872df173d9SMahesh Salgaonkar 	elf->e_phentsize = sizeof(struct elf_phdr);
7882df173d9SMahesh Salgaonkar 	elf->e_phnum = 0;
7892df173d9SMahesh Salgaonkar 	elf->e_shentsize = 0;
7902df173d9SMahesh Salgaonkar 	elf->e_shnum = 0;
7912df173d9SMahesh Salgaonkar 	elf->e_shstrndx = 0;
7922df173d9SMahesh Salgaonkar 
7932df173d9SMahesh Salgaonkar 	return 0;
7942df173d9SMahesh Salgaonkar }
7952df173d9SMahesh Salgaonkar 
7962df173d9SMahesh Salgaonkar /*
7972df173d9SMahesh Salgaonkar  * Traverse through memblock structure and setup crash memory ranges. These
7982df173d9SMahesh Salgaonkar  * ranges will be used create PT_LOAD program headers in elfcore header.
7992df173d9SMahesh Salgaonkar  */
8002df173d9SMahesh Salgaonkar static void fadump_setup_crash_memory_ranges(void)
8012df173d9SMahesh Salgaonkar {
8022df173d9SMahesh Salgaonkar 	struct memblock_region *reg;
8032df173d9SMahesh Salgaonkar 	unsigned long long start, end;
8042df173d9SMahesh Salgaonkar 
8052df173d9SMahesh Salgaonkar 	pr_debug("Setup crash memory ranges.\n");
8062df173d9SMahesh Salgaonkar 	crash_mem_ranges = 0;
8072df173d9SMahesh Salgaonkar 	/*
8082df173d9SMahesh Salgaonkar 	 * add the first memory chunk (RMA_START through boot_memory_size) as
8092df173d9SMahesh Salgaonkar 	 * a separate memory chunk. The reason is, at the time crash firmware
8102df173d9SMahesh Salgaonkar 	 * will move the content of this memory chunk to different location
8112df173d9SMahesh Salgaonkar 	 * specified during fadump registration. We need to create a separate
8122df173d9SMahesh Salgaonkar 	 * program header for this chunk with the correct offset.
8132df173d9SMahesh Salgaonkar 	 */
8142df173d9SMahesh Salgaonkar 	fadump_add_crash_memory(RMA_START, fw_dump.boot_memory_size);
8152df173d9SMahesh Salgaonkar 
8162df173d9SMahesh Salgaonkar 	for_each_memblock(memory, reg) {
8172df173d9SMahesh Salgaonkar 		start = (unsigned long long)reg->base;
8182df173d9SMahesh Salgaonkar 		end = start + (unsigned long long)reg->size;
8192df173d9SMahesh Salgaonkar 		if (start == RMA_START && end >= fw_dump.boot_memory_size)
8202df173d9SMahesh Salgaonkar 			start = fw_dump.boot_memory_size;
8212df173d9SMahesh Salgaonkar 
8222df173d9SMahesh Salgaonkar 		/* add this range excluding the reserved dump area. */
8232df173d9SMahesh Salgaonkar 		fadump_exclude_reserved_area(start, end);
8242df173d9SMahesh Salgaonkar 	}
8252df173d9SMahesh Salgaonkar }
8262df173d9SMahesh Salgaonkar 
827d34c5f26SMahesh Salgaonkar /*
828d34c5f26SMahesh Salgaonkar  * If the given physical address falls within the boot memory region then
829d34c5f26SMahesh Salgaonkar  * return the relocated address that points to the dump region reserved
830d34c5f26SMahesh Salgaonkar  * for saving initial boot memory contents.
831d34c5f26SMahesh Salgaonkar  */
832d34c5f26SMahesh Salgaonkar static inline unsigned long fadump_relocate(unsigned long paddr)
833d34c5f26SMahesh Salgaonkar {
834d34c5f26SMahesh Salgaonkar 	if (paddr > RMA_START && paddr < fw_dump.boot_memory_size)
835408cddd9SHari Bathini 		return be64_to_cpu(fdm.rmr_region.destination_address) + paddr;
836d34c5f26SMahesh Salgaonkar 	else
837d34c5f26SMahesh Salgaonkar 		return paddr;
838d34c5f26SMahesh Salgaonkar }
839d34c5f26SMahesh Salgaonkar 
8402df173d9SMahesh Salgaonkar static int fadump_create_elfcore_headers(char *bufp)
8412df173d9SMahesh Salgaonkar {
8422df173d9SMahesh Salgaonkar 	struct elfhdr *elf;
8432df173d9SMahesh Salgaonkar 	struct elf_phdr *phdr;
8442df173d9SMahesh Salgaonkar 	int i;
8452df173d9SMahesh Salgaonkar 
8462df173d9SMahesh Salgaonkar 	fadump_init_elfcore_header(bufp);
8472df173d9SMahesh Salgaonkar 	elf = (struct elfhdr *)bufp;
8482df173d9SMahesh Salgaonkar 	bufp += sizeof(struct elfhdr);
8492df173d9SMahesh Salgaonkar 
850ebaeb5aeSMahesh Salgaonkar 	/*
851ebaeb5aeSMahesh Salgaonkar 	 * setup ELF PT_NOTE, place holder for cpu notes info. The notes info
852ebaeb5aeSMahesh Salgaonkar 	 * will be populated during second kernel boot after crash. Hence
853ebaeb5aeSMahesh Salgaonkar 	 * this PT_NOTE will always be the first elf note.
854ebaeb5aeSMahesh Salgaonkar 	 *
855ebaeb5aeSMahesh Salgaonkar 	 * NOTE: Any new ELF note addition should be placed after this note.
856ebaeb5aeSMahesh Salgaonkar 	 */
857ebaeb5aeSMahesh Salgaonkar 	phdr = (struct elf_phdr *)bufp;
858ebaeb5aeSMahesh Salgaonkar 	bufp += sizeof(struct elf_phdr);
859ebaeb5aeSMahesh Salgaonkar 	phdr->p_type = PT_NOTE;
860ebaeb5aeSMahesh Salgaonkar 	phdr->p_flags = 0;
861ebaeb5aeSMahesh Salgaonkar 	phdr->p_vaddr = 0;
862ebaeb5aeSMahesh Salgaonkar 	phdr->p_align = 0;
863ebaeb5aeSMahesh Salgaonkar 
864ebaeb5aeSMahesh Salgaonkar 	phdr->p_offset = 0;
865ebaeb5aeSMahesh Salgaonkar 	phdr->p_paddr = 0;
866ebaeb5aeSMahesh Salgaonkar 	phdr->p_filesz = 0;
867ebaeb5aeSMahesh Salgaonkar 	phdr->p_memsz = 0;
868ebaeb5aeSMahesh Salgaonkar 
869ebaeb5aeSMahesh Salgaonkar 	(elf->e_phnum)++;
870ebaeb5aeSMahesh Salgaonkar 
871d34c5f26SMahesh Salgaonkar 	/* setup ELF PT_NOTE for vmcoreinfo */
872d34c5f26SMahesh Salgaonkar 	phdr = (struct elf_phdr *)bufp;
873d34c5f26SMahesh Salgaonkar 	bufp += sizeof(struct elf_phdr);
874d34c5f26SMahesh Salgaonkar 	phdr->p_type	= PT_NOTE;
875d34c5f26SMahesh Salgaonkar 	phdr->p_flags	= 0;
876d34c5f26SMahesh Salgaonkar 	phdr->p_vaddr	= 0;
877d34c5f26SMahesh Salgaonkar 	phdr->p_align	= 0;
878d34c5f26SMahesh Salgaonkar 
879d34c5f26SMahesh Salgaonkar 	phdr->p_paddr	= fadump_relocate(paddr_vmcoreinfo_note());
880d34c5f26SMahesh Salgaonkar 	phdr->p_offset	= phdr->p_paddr;
881d34c5f26SMahesh Salgaonkar 	phdr->p_memsz	= vmcoreinfo_max_size;
882d34c5f26SMahesh Salgaonkar 	phdr->p_filesz	= vmcoreinfo_max_size;
883d34c5f26SMahesh Salgaonkar 
884d34c5f26SMahesh Salgaonkar 	/* Increment number of program headers. */
885d34c5f26SMahesh Salgaonkar 	(elf->e_phnum)++;
886d34c5f26SMahesh Salgaonkar 
8872df173d9SMahesh Salgaonkar 	/* setup PT_LOAD sections. */
8882df173d9SMahesh Salgaonkar 
8892df173d9SMahesh Salgaonkar 	for (i = 0; i < crash_mem_ranges; i++) {
8902df173d9SMahesh Salgaonkar 		unsigned long long mbase, msize;
8912df173d9SMahesh Salgaonkar 		mbase = crash_memory_ranges[i].base;
8922df173d9SMahesh Salgaonkar 		msize = crash_memory_ranges[i].size;
8932df173d9SMahesh Salgaonkar 
8942df173d9SMahesh Salgaonkar 		if (!msize)
8952df173d9SMahesh Salgaonkar 			continue;
8962df173d9SMahesh Salgaonkar 
8972df173d9SMahesh Salgaonkar 		phdr = (struct elf_phdr *)bufp;
8982df173d9SMahesh Salgaonkar 		bufp += sizeof(struct elf_phdr);
8992df173d9SMahesh Salgaonkar 		phdr->p_type	= PT_LOAD;
9002df173d9SMahesh Salgaonkar 		phdr->p_flags	= PF_R|PF_W|PF_X;
9012df173d9SMahesh Salgaonkar 		phdr->p_offset	= mbase;
9022df173d9SMahesh Salgaonkar 
9032df173d9SMahesh Salgaonkar 		if (mbase == RMA_START) {
9042df173d9SMahesh Salgaonkar 			/*
9052df173d9SMahesh Salgaonkar 			 * The entire RMA region will be moved by firmware
9062df173d9SMahesh Salgaonkar 			 * to the specified destination_address. Hence set
9072df173d9SMahesh Salgaonkar 			 * the correct offset.
9082df173d9SMahesh Salgaonkar 			 */
909408cddd9SHari Bathini 			phdr->p_offset = be64_to_cpu(fdm.rmr_region.destination_address);
9102df173d9SMahesh Salgaonkar 		}
9112df173d9SMahesh Salgaonkar 
9122df173d9SMahesh Salgaonkar 		phdr->p_paddr = mbase;
9132df173d9SMahesh Salgaonkar 		phdr->p_vaddr = (unsigned long)__va(mbase);
9142df173d9SMahesh Salgaonkar 		phdr->p_filesz = msize;
9152df173d9SMahesh Salgaonkar 		phdr->p_memsz = msize;
9162df173d9SMahesh Salgaonkar 		phdr->p_align = 0;
9172df173d9SMahesh Salgaonkar 
9182df173d9SMahesh Salgaonkar 		/* Increment number of program headers. */
9192df173d9SMahesh Salgaonkar 		(elf->e_phnum)++;
9202df173d9SMahesh Salgaonkar 	}
9212df173d9SMahesh Salgaonkar 	return 0;
9222df173d9SMahesh Salgaonkar }
9232df173d9SMahesh Salgaonkar 
9242df173d9SMahesh Salgaonkar static unsigned long init_fadump_header(unsigned long addr)
9252df173d9SMahesh Salgaonkar {
9262df173d9SMahesh Salgaonkar 	struct fadump_crash_info_header *fdh;
9272df173d9SMahesh Salgaonkar 
9282df173d9SMahesh Salgaonkar 	if (!addr)
9292df173d9SMahesh Salgaonkar 		return 0;
9302df173d9SMahesh Salgaonkar 
9312df173d9SMahesh Salgaonkar 	fw_dump.fadumphdr_addr = addr;
9322df173d9SMahesh Salgaonkar 	fdh = __va(addr);
9332df173d9SMahesh Salgaonkar 	addr += sizeof(struct fadump_crash_info_header);
9342df173d9SMahesh Salgaonkar 
9352df173d9SMahesh Salgaonkar 	memset(fdh, 0, sizeof(struct fadump_crash_info_header));
9362df173d9SMahesh Salgaonkar 	fdh->magic_number = FADUMP_CRASH_INFO_MAGIC;
9372df173d9SMahesh Salgaonkar 	fdh->elfcorehdr_addr = addr;
938ebaeb5aeSMahesh Salgaonkar 	/* We will set the crashing cpu id in crash_fadump() during crash. */
939ebaeb5aeSMahesh Salgaonkar 	fdh->crashing_cpu = CPU_UNKNOWN;
9402df173d9SMahesh Salgaonkar 
9412df173d9SMahesh Salgaonkar 	return addr;
9422df173d9SMahesh Salgaonkar }
9432df173d9SMahesh Salgaonkar 
9443ccc00a7SMahesh Salgaonkar static void register_fadump(void)
9453ccc00a7SMahesh Salgaonkar {
9462df173d9SMahesh Salgaonkar 	unsigned long addr;
9472df173d9SMahesh Salgaonkar 	void *vaddr;
9482df173d9SMahesh Salgaonkar 
9493ccc00a7SMahesh Salgaonkar 	/*
9503ccc00a7SMahesh Salgaonkar 	 * If no memory is reserved then we can not register for firmware-
9513ccc00a7SMahesh Salgaonkar 	 * assisted dump.
9523ccc00a7SMahesh Salgaonkar 	 */
9533ccc00a7SMahesh Salgaonkar 	if (!fw_dump.reserve_dump_area_size)
9543ccc00a7SMahesh Salgaonkar 		return;
9553ccc00a7SMahesh Salgaonkar 
9562df173d9SMahesh Salgaonkar 	fadump_setup_crash_memory_ranges();
9572df173d9SMahesh Salgaonkar 
958408cddd9SHari Bathini 	addr = be64_to_cpu(fdm.rmr_region.destination_address) + be64_to_cpu(fdm.rmr_region.source_len);
9592df173d9SMahesh Salgaonkar 	/* Initialize fadump crash info header. */
9602df173d9SMahesh Salgaonkar 	addr = init_fadump_header(addr);
9612df173d9SMahesh Salgaonkar 	vaddr = __va(addr);
9622df173d9SMahesh Salgaonkar 
9632df173d9SMahesh Salgaonkar 	pr_debug("Creating ELF core headers at %#016lx\n", addr);
9642df173d9SMahesh Salgaonkar 	fadump_create_elfcore_headers(vaddr);
9652df173d9SMahesh Salgaonkar 
9663ccc00a7SMahesh Salgaonkar 	/* register the future kernel dump with firmware. */
9673ccc00a7SMahesh Salgaonkar 	register_fw_dump(&fdm);
9683ccc00a7SMahesh Salgaonkar }
9693ccc00a7SMahesh Salgaonkar 
9703ccc00a7SMahesh Salgaonkar static int fadump_unregister_dump(struct fadump_mem_struct *fdm)
9713ccc00a7SMahesh Salgaonkar {
9723ccc00a7SMahesh Salgaonkar 	int rc = 0;
9733ccc00a7SMahesh Salgaonkar 	unsigned int wait_time;
9743ccc00a7SMahesh Salgaonkar 
9753ccc00a7SMahesh Salgaonkar 	pr_debug("Un-register firmware-assisted dump\n");
9763ccc00a7SMahesh Salgaonkar 
9773ccc00a7SMahesh Salgaonkar 	/* TODO: Add upper time limit for the delay */
9783ccc00a7SMahesh Salgaonkar 	do {
9793ccc00a7SMahesh Salgaonkar 		rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
9803ccc00a7SMahesh Salgaonkar 			FADUMP_UNREGISTER, fdm,
9813ccc00a7SMahesh Salgaonkar 			sizeof(struct fadump_mem_struct));
9823ccc00a7SMahesh Salgaonkar 
9833ccc00a7SMahesh Salgaonkar 		wait_time = rtas_busy_delay_time(rc);
9843ccc00a7SMahesh Salgaonkar 		if (wait_time)
9853ccc00a7SMahesh Salgaonkar 			mdelay(wait_time);
9863ccc00a7SMahesh Salgaonkar 	} while (wait_time);
9873ccc00a7SMahesh Salgaonkar 
9883ccc00a7SMahesh Salgaonkar 	if (rc) {
9893ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "Failed to un-register firmware-assisted dump."
9903ccc00a7SMahesh Salgaonkar 			" unexpected error(%d).\n", rc);
9913ccc00a7SMahesh Salgaonkar 		return rc;
9923ccc00a7SMahesh Salgaonkar 	}
9933ccc00a7SMahesh Salgaonkar 	fw_dump.dump_registered = 0;
9943ccc00a7SMahesh Salgaonkar 	return 0;
9953ccc00a7SMahesh Salgaonkar }
9963ccc00a7SMahesh Salgaonkar 
997b500afffSMahesh Salgaonkar static int fadump_invalidate_dump(struct fadump_mem_struct *fdm)
998b500afffSMahesh Salgaonkar {
999b500afffSMahesh Salgaonkar 	int rc = 0;
1000b500afffSMahesh Salgaonkar 	unsigned int wait_time;
1001b500afffSMahesh Salgaonkar 
1002b500afffSMahesh Salgaonkar 	pr_debug("Invalidating firmware-assisted dump registration\n");
1003b500afffSMahesh Salgaonkar 
1004b500afffSMahesh Salgaonkar 	/* TODO: Add upper time limit for the delay */
1005b500afffSMahesh Salgaonkar 	do {
1006b500afffSMahesh Salgaonkar 		rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
1007b500afffSMahesh Salgaonkar 			FADUMP_INVALIDATE, fdm,
1008b500afffSMahesh Salgaonkar 			sizeof(struct fadump_mem_struct));
1009b500afffSMahesh Salgaonkar 
1010b500afffSMahesh Salgaonkar 		wait_time = rtas_busy_delay_time(rc);
1011b500afffSMahesh Salgaonkar 		if (wait_time)
1012b500afffSMahesh Salgaonkar 			mdelay(wait_time);
1013b500afffSMahesh Salgaonkar 	} while (wait_time);
1014b500afffSMahesh Salgaonkar 
1015b500afffSMahesh Salgaonkar 	if (rc) {
10164a03749fSColin Ian King 		pr_err("Failed to invalidate firmware-assisted dump registration. Unexpected error (%d).\n", rc);
1017b5b1cfc5SMichael Ellerman 		return rc;
1018b500afffSMahesh Salgaonkar 	}
1019b500afffSMahesh Salgaonkar 	fw_dump.dump_active = 0;
1020b500afffSMahesh Salgaonkar 	fdm_active = NULL;
1021b500afffSMahesh Salgaonkar 	return 0;
1022b500afffSMahesh Salgaonkar }
1023b500afffSMahesh Salgaonkar 
1024b500afffSMahesh Salgaonkar void fadump_cleanup(void)
1025b500afffSMahesh Salgaonkar {
1026b500afffSMahesh Salgaonkar 	/* Invalidate the registration only if dump is active. */
1027b500afffSMahesh Salgaonkar 	if (fw_dump.dump_active) {
1028b500afffSMahesh Salgaonkar 		init_fadump_mem_struct(&fdm,
1029408cddd9SHari Bathini 			be64_to_cpu(fdm_active->cpu_state_data.destination_address));
1030b500afffSMahesh Salgaonkar 		fadump_invalidate_dump(&fdm);
1031b500afffSMahesh Salgaonkar 	}
1032b500afffSMahesh Salgaonkar }
1033b500afffSMahesh Salgaonkar 
1034b500afffSMahesh Salgaonkar /*
1035b500afffSMahesh Salgaonkar  * Release the memory that was reserved in early boot to preserve the memory
1036b500afffSMahesh Salgaonkar  * contents. The released memory will be available for general use.
1037b500afffSMahesh Salgaonkar  */
1038b500afffSMahesh Salgaonkar static void fadump_release_memory(unsigned long begin, unsigned long end)
1039b500afffSMahesh Salgaonkar {
1040b500afffSMahesh Salgaonkar 	unsigned long addr;
1041b500afffSMahesh Salgaonkar 	unsigned long ra_start, ra_end;
1042b500afffSMahesh Salgaonkar 
1043b500afffSMahesh Salgaonkar 	ra_start = fw_dump.reserve_dump_area_start;
1044b500afffSMahesh Salgaonkar 	ra_end = ra_start + fw_dump.reserve_dump_area_size;
1045b500afffSMahesh Salgaonkar 
1046b500afffSMahesh Salgaonkar 	for (addr = begin; addr < end; addr += PAGE_SIZE) {
1047b500afffSMahesh Salgaonkar 		/*
1048b500afffSMahesh Salgaonkar 		 * exclude the dump reserve area. Will reuse it for next
1049b500afffSMahesh Salgaonkar 		 * fadump registration.
1050b500afffSMahesh Salgaonkar 		 */
1051b500afffSMahesh Salgaonkar 		if (addr <= ra_end && ((addr + PAGE_SIZE) > ra_start))
1052b500afffSMahesh Salgaonkar 			continue;
1053b500afffSMahesh Salgaonkar 
10545d585e5cSJiang Liu 		free_reserved_page(pfn_to_page(addr >> PAGE_SHIFT));
1055b500afffSMahesh Salgaonkar 	}
1056b500afffSMahesh Salgaonkar }
1057b500afffSMahesh Salgaonkar 
1058b500afffSMahesh Salgaonkar static void fadump_invalidate_release_mem(void)
1059b500afffSMahesh Salgaonkar {
1060b500afffSMahesh Salgaonkar 	unsigned long reserved_area_start, reserved_area_end;
1061b500afffSMahesh Salgaonkar 	unsigned long destination_address;
1062b500afffSMahesh Salgaonkar 
1063b500afffSMahesh Salgaonkar 	mutex_lock(&fadump_mutex);
1064b500afffSMahesh Salgaonkar 	if (!fw_dump.dump_active) {
1065b500afffSMahesh Salgaonkar 		mutex_unlock(&fadump_mutex);
1066b500afffSMahesh Salgaonkar 		return;
1067b500afffSMahesh Salgaonkar 	}
1068b500afffSMahesh Salgaonkar 
1069408cddd9SHari Bathini 	destination_address = be64_to_cpu(fdm_active->cpu_state_data.destination_address);
1070b500afffSMahesh Salgaonkar 	fadump_cleanup();
1071b500afffSMahesh Salgaonkar 	mutex_unlock(&fadump_mutex);
1072b500afffSMahesh Salgaonkar 
1073b500afffSMahesh Salgaonkar 	/*
1074b500afffSMahesh Salgaonkar 	 * Save the current reserved memory bounds we will require them
1075b500afffSMahesh Salgaonkar 	 * later for releasing the memory for general use.
1076b500afffSMahesh Salgaonkar 	 */
1077b500afffSMahesh Salgaonkar 	reserved_area_start = fw_dump.reserve_dump_area_start;
1078b500afffSMahesh Salgaonkar 	reserved_area_end = reserved_area_start +
1079b500afffSMahesh Salgaonkar 			fw_dump.reserve_dump_area_size;
1080b500afffSMahesh Salgaonkar 	/*
1081b500afffSMahesh Salgaonkar 	 * Setup reserve_dump_area_start and its size so that we can
1082b500afffSMahesh Salgaonkar 	 * reuse this reserved memory for Re-registration.
1083b500afffSMahesh Salgaonkar 	 */
1084b500afffSMahesh Salgaonkar 	fw_dump.reserve_dump_area_start = destination_address;
1085b500afffSMahesh Salgaonkar 	fw_dump.reserve_dump_area_size = get_fadump_area_size();
1086b500afffSMahesh Salgaonkar 
1087b500afffSMahesh Salgaonkar 	fadump_release_memory(reserved_area_start, reserved_area_end);
1088b500afffSMahesh Salgaonkar 	if (fw_dump.cpu_notes_buf) {
1089b500afffSMahesh Salgaonkar 		fadump_cpu_notes_buf_free(
1090b500afffSMahesh Salgaonkar 				(unsigned long)__va(fw_dump.cpu_notes_buf),
1091b500afffSMahesh Salgaonkar 				fw_dump.cpu_notes_buf_size);
1092b500afffSMahesh Salgaonkar 		fw_dump.cpu_notes_buf = 0;
1093b500afffSMahesh Salgaonkar 		fw_dump.cpu_notes_buf_size = 0;
1094b500afffSMahesh Salgaonkar 	}
1095b500afffSMahesh Salgaonkar 	/* Initialize the kernel dump memory structure for FAD registration. */
1096b500afffSMahesh Salgaonkar 	init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
1097b500afffSMahesh Salgaonkar }
1098b500afffSMahesh Salgaonkar 
1099b500afffSMahesh Salgaonkar static ssize_t fadump_release_memory_store(struct kobject *kobj,
1100b500afffSMahesh Salgaonkar 					struct kobj_attribute *attr,
1101b500afffSMahesh Salgaonkar 					const char *buf, size_t count)
1102b500afffSMahesh Salgaonkar {
1103b500afffSMahesh Salgaonkar 	if (!fw_dump.dump_active)
1104b500afffSMahesh Salgaonkar 		return -EPERM;
1105b500afffSMahesh Salgaonkar 
1106b500afffSMahesh Salgaonkar 	if (buf[0] == '1') {
1107b500afffSMahesh Salgaonkar 		/*
1108b500afffSMahesh Salgaonkar 		 * Take away the '/proc/vmcore'. We are releasing the dump
1109b500afffSMahesh Salgaonkar 		 * memory, hence it will not be valid anymore.
1110b500afffSMahesh Salgaonkar 		 */
1111b500afffSMahesh Salgaonkar 		vmcore_cleanup();
1112b500afffSMahesh Salgaonkar 		fadump_invalidate_release_mem();
1113b500afffSMahesh Salgaonkar 
1114b500afffSMahesh Salgaonkar 	} else
1115b500afffSMahesh Salgaonkar 		return -EINVAL;
1116b500afffSMahesh Salgaonkar 	return count;
1117b500afffSMahesh Salgaonkar }
1118b500afffSMahesh Salgaonkar 
11193ccc00a7SMahesh Salgaonkar static ssize_t fadump_enabled_show(struct kobject *kobj,
11203ccc00a7SMahesh Salgaonkar 					struct kobj_attribute *attr,
11213ccc00a7SMahesh Salgaonkar 					char *buf)
11223ccc00a7SMahesh Salgaonkar {
11233ccc00a7SMahesh Salgaonkar 	return sprintf(buf, "%d\n", fw_dump.fadump_enabled);
11243ccc00a7SMahesh Salgaonkar }
11253ccc00a7SMahesh Salgaonkar 
11263ccc00a7SMahesh Salgaonkar static ssize_t fadump_register_show(struct kobject *kobj,
11273ccc00a7SMahesh Salgaonkar 					struct kobj_attribute *attr,
11283ccc00a7SMahesh Salgaonkar 					char *buf)
11293ccc00a7SMahesh Salgaonkar {
11303ccc00a7SMahesh Salgaonkar 	return sprintf(buf, "%d\n", fw_dump.dump_registered);
11313ccc00a7SMahesh Salgaonkar }
11323ccc00a7SMahesh Salgaonkar 
11333ccc00a7SMahesh Salgaonkar static ssize_t fadump_register_store(struct kobject *kobj,
11343ccc00a7SMahesh Salgaonkar 					struct kobj_attribute *attr,
11353ccc00a7SMahesh Salgaonkar 					const char *buf, size_t count)
11363ccc00a7SMahesh Salgaonkar {
11373ccc00a7SMahesh Salgaonkar 	int ret = 0;
11383ccc00a7SMahesh Salgaonkar 
11393ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_enabled || fdm_active)
11403ccc00a7SMahesh Salgaonkar 		return -EPERM;
11413ccc00a7SMahesh Salgaonkar 
11423ccc00a7SMahesh Salgaonkar 	mutex_lock(&fadump_mutex);
11433ccc00a7SMahesh Salgaonkar 
11443ccc00a7SMahesh Salgaonkar 	switch (buf[0]) {
11453ccc00a7SMahesh Salgaonkar 	case '0':
11463ccc00a7SMahesh Salgaonkar 		if (fw_dump.dump_registered == 0) {
11473ccc00a7SMahesh Salgaonkar 			ret = -EINVAL;
11483ccc00a7SMahesh Salgaonkar 			goto unlock_out;
11493ccc00a7SMahesh Salgaonkar 		}
11503ccc00a7SMahesh Salgaonkar 		/* Un-register Firmware-assisted dump */
11513ccc00a7SMahesh Salgaonkar 		fadump_unregister_dump(&fdm);
11523ccc00a7SMahesh Salgaonkar 		break;
11533ccc00a7SMahesh Salgaonkar 	case '1':
11543ccc00a7SMahesh Salgaonkar 		if (fw_dump.dump_registered == 1) {
11553ccc00a7SMahesh Salgaonkar 			ret = -EINVAL;
11563ccc00a7SMahesh Salgaonkar 			goto unlock_out;
11573ccc00a7SMahesh Salgaonkar 		}
11583ccc00a7SMahesh Salgaonkar 		/* Register Firmware-assisted dump */
11593ccc00a7SMahesh Salgaonkar 		register_fadump();
11603ccc00a7SMahesh Salgaonkar 		break;
11613ccc00a7SMahesh Salgaonkar 	default:
11623ccc00a7SMahesh Salgaonkar 		ret = -EINVAL;
11633ccc00a7SMahesh Salgaonkar 		break;
11643ccc00a7SMahesh Salgaonkar 	}
11653ccc00a7SMahesh Salgaonkar 
11663ccc00a7SMahesh Salgaonkar unlock_out:
11673ccc00a7SMahesh Salgaonkar 	mutex_unlock(&fadump_mutex);
11683ccc00a7SMahesh Salgaonkar 	return ret < 0 ? ret : count;
11693ccc00a7SMahesh Salgaonkar }
11703ccc00a7SMahesh Salgaonkar 
11713ccc00a7SMahesh Salgaonkar static int fadump_region_show(struct seq_file *m, void *private)
11723ccc00a7SMahesh Salgaonkar {
11733ccc00a7SMahesh Salgaonkar 	const struct fadump_mem_struct *fdm_ptr;
11743ccc00a7SMahesh Salgaonkar 
11753ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_enabled)
11763ccc00a7SMahesh Salgaonkar 		return 0;
11773ccc00a7SMahesh Salgaonkar 
1178b500afffSMahesh Salgaonkar 	mutex_lock(&fadump_mutex);
11793ccc00a7SMahesh Salgaonkar 	if (fdm_active)
11803ccc00a7SMahesh Salgaonkar 		fdm_ptr = fdm_active;
1181b500afffSMahesh Salgaonkar 	else {
1182b500afffSMahesh Salgaonkar 		mutex_unlock(&fadump_mutex);
11833ccc00a7SMahesh Salgaonkar 		fdm_ptr = &fdm;
1184b500afffSMahesh Salgaonkar 	}
11853ccc00a7SMahesh Salgaonkar 
11863ccc00a7SMahesh Salgaonkar 	seq_printf(m,
11873ccc00a7SMahesh Salgaonkar 			"CPU : [%#016llx-%#016llx] %#llx bytes, "
11883ccc00a7SMahesh Salgaonkar 			"Dumped: %#llx\n",
1189408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->cpu_state_data.destination_address),
1190408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) +
1191408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->cpu_state_data.source_len) - 1,
1192408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->cpu_state_data.source_len),
1193408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->cpu_state_data.bytes_dumped));
11943ccc00a7SMahesh Salgaonkar 	seq_printf(m,
11953ccc00a7SMahesh Salgaonkar 			"HPTE: [%#016llx-%#016llx] %#llx bytes, "
11963ccc00a7SMahesh Salgaonkar 			"Dumped: %#llx\n",
1197408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->hpte_region.destination_address),
1198408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->hpte_region.destination_address) +
1199408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->hpte_region.source_len) - 1,
1200408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->hpte_region.source_len),
1201408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->hpte_region.bytes_dumped));
12023ccc00a7SMahesh Salgaonkar 	seq_printf(m,
12033ccc00a7SMahesh Salgaonkar 			"DUMP: [%#016llx-%#016llx] %#llx bytes, "
12043ccc00a7SMahesh Salgaonkar 			"Dumped: %#llx\n",
1205408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->rmr_region.destination_address),
1206408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->rmr_region.destination_address) +
1207408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->rmr_region.source_len) - 1,
1208408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->rmr_region.source_len),
1209408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->rmr_region.bytes_dumped));
12103ccc00a7SMahesh Salgaonkar 
12113ccc00a7SMahesh Salgaonkar 	if (!fdm_active ||
12123ccc00a7SMahesh Salgaonkar 		(fw_dump.reserve_dump_area_start ==
1213408cddd9SHari Bathini 		be64_to_cpu(fdm_ptr->cpu_state_data.destination_address)))
1214b500afffSMahesh Salgaonkar 		goto out;
12153ccc00a7SMahesh Salgaonkar 
12163ccc00a7SMahesh Salgaonkar 	/* Dump is active. Show reserved memory region. */
12173ccc00a7SMahesh Salgaonkar 	seq_printf(m,
12183ccc00a7SMahesh Salgaonkar 			"    : [%#016llx-%#016llx] %#llx bytes, "
12193ccc00a7SMahesh Salgaonkar 			"Dumped: %#llx\n",
12203ccc00a7SMahesh Salgaonkar 			(unsigned long long)fw_dump.reserve_dump_area_start,
1221408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) - 1,
1222408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) -
12233ccc00a7SMahesh Salgaonkar 			fw_dump.reserve_dump_area_start,
1224408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) -
12253ccc00a7SMahesh Salgaonkar 			fw_dump.reserve_dump_area_start);
1226b500afffSMahesh Salgaonkar out:
1227b500afffSMahesh Salgaonkar 	if (fdm_active)
1228b500afffSMahesh Salgaonkar 		mutex_unlock(&fadump_mutex);
12293ccc00a7SMahesh Salgaonkar 	return 0;
12303ccc00a7SMahesh Salgaonkar }
12313ccc00a7SMahesh Salgaonkar 
1232b500afffSMahesh Salgaonkar static struct kobj_attribute fadump_release_attr = __ATTR(fadump_release_mem,
1233b500afffSMahesh Salgaonkar 						0200, NULL,
1234b500afffSMahesh Salgaonkar 						fadump_release_memory_store);
12353ccc00a7SMahesh Salgaonkar static struct kobj_attribute fadump_attr = __ATTR(fadump_enabled,
12363ccc00a7SMahesh Salgaonkar 						0444, fadump_enabled_show,
12373ccc00a7SMahesh Salgaonkar 						NULL);
12383ccc00a7SMahesh Salgaonkar static struct kobj_attribute fadump_register_attr = __ATTR(fadump_registered,
12393ccc00a7SMahesh Salgaonkar 						0644, fadump_register_show,
12403ccc00a7SMahesh Salgaonkar 						fadump_register_store);
12413ccc00a7SMahesh Salgaonkar 
12423ccc00a7SMahesh Salgaonkar static int fadump_region_open(struct inode *inode, struct file *file)
12433ccc00a7SMahesh Salgaonkar {
12443ccc00a7SMahesh Salgaonkar 	return single_open(file, fadump_region_show, inode->i_private);
12453ccc00a7SMahesh Salgaonkar }
12463ccc00a7SMahesh Salgaonkar 
12473ccc00a7SMahesh Salgaonkar static const struct file_operations fadump_region_fops = {
12483ccc00a7SMahesh Salgaonkar 	.open    = fadump_region_open,
12493ccc00a7SMahesh Salgaonkar 	.read    = seq_read,
12503ccc00a7SMahesh Salgaonkar 	.llseek  = seq_lseek,
12513ccc00a7SMahesh Salgaonkar 	.release = single_release,
12523ccc00a7SMahesh Salgaonkar };
12533ccc00a7SMahesh Salgaonkar 
12543ccc00a7SMahesh Salgaonkar static void fadump_init_files(void)
12553ccc00a7SMahesh Salgaonkar {
12563ccc00a7SMahesh Salgaonkar 	struct dentry *debugfs_file;
12573ccc00a7SMahesh Salgaonkar 	int rc = 0;
12583ccc00a7SMahesh Salgaonkar 
12593ccc00a7SMahesh Salgaonkar 	rc = sysfs_create_file(kernel_kobj, &fadump_attr.attr);
12603ccc00a7SMahesh Salgaonkar 	if (rc)
12613ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "fadump: unable to create sysfs file"
12623ccc00a7SMahesh Salgaonkar 			" fadump_enabled (%d)\n", rc);
12633ccc00a7SMahesh Salgaonkar 
12643ccc00a7SMahesh Salgaonkar 	rc = sysfs_create_file(kernel_kobj, &fadump_register_attr.attr);
12653ccc00a7SMahesh Salgaonkar 	if (rc)
12663ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "fadump: unable to create sysfs file"
12673ccc00a7SMahesh Salgaonkar 			" fadump_registered (%d)\n", rc);
12683ccc00a7SMahesh Salgaonkar 
12693ccc00a7SMahesh Salgaonkar 	debugfs_file = debugfs_create_file("fadump_region", 0444,
12703ccc00a7SMahesh Salgaonkar 					powerpc_debugfs_root, NULL,
12713ccc00a7SMahesh Salgaonkar 					&fadump_region_fops);
12723ccc00a7SMahesh Salgaonkar 	if (!debugfs_file)
12733ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "fadump: unable to create debugfs file"
12743ccc00a7SMahesh Salgaonkar 				" fadump_region\n");
1275b500afffSMahesh Salgaonkar 
1276b500afffSMahesh Salgaonkar 	if (fw_dump.dump_active) {
1277b500afffSMahesh Salgaonkar 		rc = sysfs_create_file(kernel_kobj, &fadump_release_attr.attr);
1278b500afffSMahesh Salgaonkar 		if (rc)
1279b500afffSMahesh Salgaonkar 			printk(KERN_ERR "fadump: unable to create sysfs file"
1280b500afffSMahesh Salgaonkar 				" fadump_release_mem (%d)\n", rc);
1281b500afffSMahesh Salgaonkar 	}
12823ccc00a7SMahesh Salgaonkar 	return;
12833ccc00a7SMahesh Salgaonkar }
12843ccc00a7SMahesh Salgaonkar 
12853ccc00a7SMahesh Salgaonkar /*
12863ccc00a7SMahesh Salgaonkar  * Prepare for firmware-assisted dump.
12873ccc00a7SMahesh Salgaonkar  */
12883ccc00a7SMahesh Salgaonkar int __init setup_fadump(void)
12893ccc00a7SMahesh Salgaonkar {
12903ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_enabled)
12913ccc00a7SMahesh Salgaonkar 		return 0;
12923ccc00a7SMahesh Salgaonkar 
12933ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_supported) {
12943ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "Firmware-assisted dump is not supported on"
12953ccc00a7SMahesh Salgaonkar 			" this hardware\n");
12963ccc00a7SMahesh Salgaonkar 		return 0;
12973ccc00a7SMahesh Salgaonkar 	}
12983ccc00a7SMahesh Salgaonkar 
12993ccc00a7SMahesh Salgaonkar 	fadump_show_config();
13002df173d9SMahesh Salgaonkar 	/*
13012df173d9SMahesh Salgaonkar 	 * If dump data is available then see if it is valid and prepare for
13022df173d9SMahesh Salgaonkar 	 * saving it to the disk.
13032df173d9SMahesh Salgaonkar 	 */
1304b500afffSMahesh Salgaonkar 	if (fw_dump.dump_active) {
1305b500afffSMahesh Salgaonkar 		/*
1306b500afffSMahesh Salgaonkar 		 * if dump process fails then invalidate the registration
1307b500afffSMahesh Salgaonkar 		 * and release memory before proceeding for re-registration.
1308b500afffSMahesh Salgaonkar 		 */
1309b500afffSMahesh Salgaonkar 		if (process_fadump(fdm_active) < 0)
1310b500afffSMahesh Salgaonkar 			fadump_invalidate_release_mem();
1311b500afffSMahesh Salgaonkar 	}
13123ccc00a7SMahesh Salgaonkar 	/* Initialize the kernel dump memory structure for FAD registration. */
13132df173d9SMahesh Salgaonkar 	else if (fw_dump.reserve_dump_area_size)
13143ccc00a7SMahesh Salgaonkar 		init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
13153ccc00a7SMahesh Salgaonkar 	fadump_init_files();
13163ccc00a7SMahesh Salgaonkar 
13173ccc00a7SMahesh Salgaonkar 	return 1;
13183ccc00a7SMahesh Salgaonkar }
13193ccc00a7SMahesh Salgaonkar subsys_initcall(setup_fadump);
1320