xref: /openbmc/linux/arch/powerpc/kernel/fadump.c (revision f86593be1e7f5405b980bb4b11640250ac81d7cb)
1eb39c880SMahesh Salgaonkar /*
2eb39c880SMahesh Salgaonkar  * Firmware Assisted dump: A robust mechanism to get reliable kernel crash
3eb39c880SMahesh Salgaonkar  * dump with assistance from firmware. This approach does not use kexec,
4eb39c880SMahesh Salgaonkar  * instead firmware assists in booting the kdump kernel while preserving
5eb39c880SMahesh Salgaonkar  * memory contents. The most of the code implementation has been adapted
6eb39c880SMahesh Salgaonkar  * from phyp assisted dump implementation written by Linas Vepstas and
7eb39c880SMahesh Salgaonkar  * Manish Ahuja
8eb39c880SMahesh Salgaonkar  *
9eb39c880SMahesh Salgaonkar  * This program is free software; you can redistribute it and/or modify
10eb39c880SMahesh Salgaonkar  * it under the terms of the GNU General Public License as published by
11eb39c880SMahesh Salgaonkar  * the Free Software Foundation; either version 2 of the License, or
12eb39c880SMahesh Salgaonkar  * (at your option) any later version.
13eb39c880SMahesh Salgaonkar  *
14eb39c880SMahesh Salgaonkar  * This program is distributed in the hope that it will be useful,
15eb39c880SMahesh Salgaonkar  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16eb39c880SMahesh Salgaonkar  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17eb39c880SMahesh Salgaonkar  * GNU General Public License for more details.
18eb39c880SMahesh Salgaonkar  *
19eb39c880SMahesh Salgaonkar  * You should have received a copy of the GNU General Public License
20eb39c880SMahesh Salgaonkar  * along with this program; if not, write to the Free Software
21eb39c880SMahesh Salgaonkar  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22eb39c880SMahesh Salgaonkar  *
23eb39c880SMahesh Salgaonkar  * Copyright 2011 IBM Corporation
24eb39c880SMahesh Salgaonkar  * Author: Mahesh Salgaonkar <mahesh@linux.vnet.ibm.com>
25eb39c880SMahesh Salgaonkar  */
26eb39c880SMahesh Salgaonkar 
27eb39c880SMahesh Salgaonkar #undef DEBUG
28eb39c880SMahesh Salgaonkar #define pr_fmt(fmt) "fadump: " fmt
29eb39c880SMahesh Salgaonkar 
30eb39c880SMahesh Salgaonkar #include <linux/string.h>
31eb39c880SMahesh Salgaonkar #include <linux/memblock.h>
323ccc00a7SMahesh Salgaonkar #include <linux/delay.h>
333ccc00a7SMahesh Salgaonkar #include <linux/seq_file.h>
342df173d9SMahesh Salgaonkar #include <linux/crash_dump.h>
35b500afffSMahesh Salgaonkar #include <linux/kobject.h>
36b500afffSMahesh Salgaonkar #include <linux/sysfs.h>
37a5818313SHari Bathini #include <linux/slab.h>
38a4e92ce8SMahesh Salgaonkar #include <linux/cma.h>
39eb39c880SMahesh Salgaonkar 
407644d581SMichael Ellerman #include <asm/debugfs.h>
41eb39c880SMahesh Salgaonkar #include <asm/page.h>
42eb39c880SMahesh Salgaonkar #include <asm/prom.h>
43eb39c880SMahesh Salgaonkar #include <asm/rtas.h>
44eb39c880SMahesh Salgaonkar #include <asm/fadump.h>
45cad3c834SStephen Rothwell #include <asm/setup.h>
46eb39c880SMahesh Salgaonkar 
47eb39c880SMahesh Salgaonkar static struct fw_dump fw_dump;
483ccc00a7SMahesh Salgaonkar static struct fadump_mem_struct fdm;
493ccc00a7SMahesh Salgaonkar static const struct fadump_mem_struct *fdm_active;
50a4e92ce8SMahesh Salgaonkar #ifdef CONFIG_CMA
51a4e92ce8SMahesh Salgaonkar static struct cma *fadump_cma;
52a4e92ce8SMahesh Salgaonkar #endif
533ccc00a7SMahesh Salgaonkar 
543ccc00a7SMahesh Salgaonkar static DEFINE_MUTEX(fadump_mutex);
551bd6a1c4SHari Bathini struct fad_crash_memory_ranges *crash_memory_ranges;
561bd6a1c4SHari Bathini int crash_memory_ranges_size;
572df173d9SMahesh Salgaonkar int crash_mem_ranges;
581bd6a1c4SHari Bathini int max_crash_mem_ranges;
59eb39c880SMahesh Salgaonkar 
60a4e92ce8SMahesh Salgaonkar #ifdef CONFIG_CMA
61a4e92ce8SMahesh Salgaonkar /*
62a4e92ce8SMahesh Salgaonkar  * fadump_cma_init() - Initialize CMA area from a fadump reserved memory
63a4e92ce8SMahesh Salgaonkar  *
64a4e92ce8SMahesh Salgaonkar  * This function initializes CMA area from fadump reserved memory.
65a4e92ce8SMahesh Salgaonkar  * The total size of fadump reserved memory covers for boot memory size
66a4e92ce8SMahesh Salgaonkar  * + cpu data size + hpte size and metadata.
67a4e92ce8SMahesh Salgaonkar  * Initialize only the area equivalent to boot memory size for CMA use.
68a4e92ce8SMahesh Salgaonkar  * The reamining portion of fadump reserved memory will be not given
69a4e92ce8SMahesh Salgaonkar  * to CMA and pages for thoes will stay reserved. boot memory size is
70a4e92ce8SMahesh Salgaonkar  * aligned per CMA requirement to satisy cma_init_reserved_mem() call.
71a4e92ce8SMahesh Salgaonkar  * But for some reason even if it fails we still have the memory reservation
72a4e92ce8SMahesh Salgaonkar  * with us and we can still continue doing fadump.
73a4e92ce8SMahesh Salgaonkar  */
74a4e92ce8SMahesh Salgaonkar int __init fadump_cma_init(void)
75a4e92ce8SMahesh Salgaonkar {
76a4e92ce8SMahesh Salgaonkar 	unsigned long long base, size;
77a4e92ce8SMahesh Salgaonkar 	int rc;
78a4e92ce8SMahesh Salgaonkar 
79a4e92ce8SMahesh Salgaonkar 	if (!fw_dump.fadump_enabled)
80a4e92ce8SMahesh Salgaonkar 		return 0;
81a4e92ce8SMahesh Salgaonkar 
82a4e92ce8SMahesh Salgaonkar 	/*
83a4e92ce8SMahesh Salgaonkar 	 * Do not use CMA if user has provided fadump=nocma kernel parameter.
84a4e92ce8SMahesh Salgaonkar 	 * Return 1 to continue with fadump old behaviour.
85a4e92ce8SMahesh Salgaonkar 	 */
86a4e92ce8SMahesh Salgaonkar 	if (fw_dump.nocma)
87a4e92ce8SMahesh Salgaonkar 		return 1;
88a4e92ce8SMahesh Salgaonkar 
89a4e92ce8SMahesh Salgaonkar 	base = fw_dump.reserve_dump_area_start;
90a4e92ce8SMahesh Salgaonkar 	size = fw_dump.boot_memory_size;
91a4e92ce8SMahesh Salgaonkar 
92a4e92ce8SMahesh Salgaonkar 	if (!size)
93a4e92ce8SMahesh Salgaonkar 		return 0;
94a4e92ce8SMahesh Salgaonkar 
95a4e92ce8SMahesh Salgaonkar 	rc = cma_init_reserved_mem(base, size, 0, "fadump_cma", &fadump_cma);
96a4e92ce8SMahesh Salgaonkar 	if (rc) {
97a4e92ce8SMahesh Salgaonkar 		pr_err("Failed to init cma area for firmware-assisted dump,%d\n", rc);
98a4e92ce8SMahesh Salgaonkar 		/*
99a4e92ce8SMahesh Salgaonkar 		 * Though the CMA init has failed we still have memory
100a4e92ce8SMahesh Salgaonkar 		 * reservation with us. The reserved memory will be
101a4e92ce8SMahesh Salgaonkar 		 * blocked from production system usage.  Hence return 1,
102a4e92ce8SMahesh Salgaonkar 		 * so that we can continue with fadump.
103a4e92ce8SMahesh Salgaonkar 		 */
104a4e92ce8SMahesh Salgaonkar 		return 1;
105a4e92ce8SMahesh Salgaonkar 	}
106a4e92ce8SMahesh Salgaonkar 
107a4e92ce8SMahesh Salgaonkar 	/*
108a4e92ce8SMahesh Salgaonkar 	 * So we now have successfully initialized cma area for fadump.
109a4e92ce8SMahesh Salgaonkar 	 */
110a4e92ce8SMahesh Salgaonkar 	pr_info("Initialized 0x%lx bytes cma area at %ldMB from 0x%lx "
111a4e92ce8SMahesh Salgaonkar 		"bytes of memory reserved for firmware-assisted dump\n",
112a4e92ce8SMahesh Salgaonkar 		cma_get_size(fadump_cma),
113a4e92ce8SMahesh Salgaonkar 		(unsigned long)cma_get_base(fadump_cma) >> 20,
114a4e92ce8SMahesh Salgaonkar 		fw_dump.reserve_dump_area_size);
115a4e92ce8SMahesh Salgaonkar 	return 1;
116a4e92ce8SMahesh Salgaonkar }
117a4e92ce8SMahesh Salgaonkar #else
118a4e92ce8SMahesh Salgaonkar static int __init fadump_cma_init(void) { return 1; }
119a4e92ce8SMahesh Salgaonkar #endif /* CONFIG_CMA */
120a4e92ce8SMahesh Salgaonkar 
121eb39c880SMahesh Salgaonkar /* Scan the Firmware Assisted dump configuration details. */
122eb39c880SMahesh Salgaonkar int __init early_init_dt_scan_fw_dump(unsigned long node,
123eb39c880SMahesh Salgaonkar 			const char *uname, int depth, void *data)
124eb39c880SMahesh Salgaonkar {
1259d0c4dfeSRob Herring 	const __be32 *sections;
126eb39c880SMahesh Salgaonkar 	int i, num_sections;
1279d0c4dfeSRob Herring 	int size;
128408cddd9SHari Bathini 	const __be32 *token;
129eb39c880SMahesh Salgaonkar 
130eb39c880SMahesh Salgaonkar 	if (depth != 1 || strcmp(uname, "rtas") != 0)
131eb39c880SMahesh Salgaonkar 		return 0;
132eb39c880SMahesh Salgaonkar 
133eb39c880SMahesh Salgaonkar 	/*
134eb39c880SMahesh Salgaonkar 	 * Check if Firmware Assisted dump is supported. if yes, check
135eb39c880SMahesh Salgaonkar 	 * if dump has been initiated on last reboot.
136eb39c880SMahesh Salgaonkar 	 */
137eb39c880SMahesh Salgaonkar 	token = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump", NULL);
138eb39c880SMahesh Salgaonkar 	if (!token)
139a7d04317SGavin Shan 		return 1;
140eb39c880SMahesh Salgaonkar 
141eb39c880SMahesh Salgaonkar 	fw_dump.fadump_supported = 1;
142408cddd9SHari Bathini 	fw_dump.ibm_configure_kernel_dump = be32_to_cpu(*token);
143eb39c880SMahesh Salgaonkar 
144eb39c880SMahesh Salgaonkar 	/*
145eb39c880SMahesh Salgaonkar 	 * The 'ibm,kernel-dump' rtas node is present only if there is
146eb39c880SMahesh Salgaonkar 	 * dump data waiting for us.
147eb39c880SMahesh Salgaonkar 	 */
1483ccc00a7SMahesh Salgaonkar 	fdm_active = of_get_flat_dt_prop(node, "ibm,kernel-dump", NULL);
1493ccc00a7SMahesh Salgaonkar 	if (fdm_active)
150eb39c880SMahesh Salgaonkar 		fw_dump.dump_active = 1;
151eb39c880SMahesh Salgaonkar 
152eb39c880SMahesh Salgaonkar 	/* Get the sizes required to store dump data for the firmware provided
153eb39c880SMahesh Salgaonkar 	 * dump sections.
154eb39c880SMahesh Salgaonkar 	 * For each dump section type supported, a 32bit cell which defines
155eb39c880SMahesh Salgaonkar 	 * the ID of a supported section followed by two 32 bit cells which
156eb39c880SMahesh Salgaonkar 	 * gives teh size of the section in bytes.
157eb39c880SMahesh Salgaonkar 	 */
158eb39c880SMahesh Salgaonkar 	sections = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump-sizes",
159eb39c880SMahesh Salgaonkar 					&size);
160eb39c880SMahesh Salgaonkar 
161eb39c880SMahesh Salgaonkar 	if (!sections)
162a7d04317SGavin Shan 		return 1;
163eb39c880SMahesh Salgaonkar 
164eb39c880SMahesh Salgaonkar 	num_sections = size / (3 * sizeof(u32));
165eb39c880SMahesh Salgaonkar 
166eb39c880SMahesh Salgaonkar 	for (i = 0; i < num_sections; i++, sections += 3) {
167eb39c880SMahesh Salgaonkar 		u32 type = (u32)of_read_number(sections, 1);
168eb39c880SMahesh Salgaonkar 
169eb39c880SMahesh Salgaonkar 		switch (type) {
170eb39c880SMahesh Salgaonkar 		case FADUMP_CPU_STATE_DATA:
171eb39c880SMahesh Salgaonkar 			fw_dump.cpu_state_data_size =
172eb39c880SMahesh Salgaonkar 					of_read_ulong(&sections[1], 2);
173eb39c880SMahesh Salgaonkar 			break;
174eb39c880SMahesh Salgaonkar 		case FADUMP_HPTE_REGION:
175eb39c880SMahesh Salgaonkar 			fw_dump.hpte_region_size =
176eb39c880SMahesh Salgaonkar 					of_read_ulong(&sections[1], 2);
177eb39c880SMahesh Salgaonkar 			break;
178eb39c880SMahesh Salgaonkar 		}
179eb39c880SMahesh Salgaonkar 	}
180a7d04317SGavin Shan 
181eb39c880SMahesh Salgaonkar 	return 1;
182eb39c880SMahesh Salgaonkar }
183eb39c880SMahesh Salgaonkar 
184eae0dfccSHari Bathini /*
185eae0dfccSHari Bathini  * If fadump is registered, check if the memory provided
186eae0dfccSHari Bathini  * falls within boot memory area.
187eae0dfccSHari Bathini  */
188eae0dfccSHari Bathini int is_fadump_boot_memory_area(u64 addr, ulong size)
189eae0dfccSHari Bathini {
190eae0dfccSHari Bathini 	if (!fw_dump.dump_registered)
191eae0dfccSHari Bathini 		return 0;
192eae0dfccSHari Bathini 
193eae0dfccSHari Bathini 	return (addr + size) > RMA_START && addr <= fw_dump.boot_memory_size;
194eae0dfccSHari Bathini }
195eae0dfccSHari Bathini 
1966fcd6baaSNicholas Piggin int should_fadump_crash(void)
1976fcd6baaSNicholas Piggin {
1986fcd6baaSNicholas Piggin 	if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr)
1996fcd6baaSNicholas Piggin 		return 0;
2006fcd6baaSNicholas Piggin 	return 1;
2016fcd6baaSNicholas Piggin }
2026fcd6baaSNicholas Piggin 
2033ccc00a7SMahesh Salgaonkar int is_fadump_active(void)
2043ccc00a7SMahesh Salgaonkar {
2053ccc00a7SMahesh Salgaonkar 	return fw_dump.dump_active;
2063ccc00a7SMahesh Salgaonkar }
2073ccc00a7SMahesh Salgaonkar 
208a5a05b91SHari Bathini /*
209a5a05b91SHari Bathini  * Returns 1, if there are no holes in boot memory area,
210a5a05b91SHari Bathini  * 0 otherwise.
211a5a05b91SHari Bathini  */
212a5a05b91SHari Bathini static int is_boot_memory_area_contiguous(void)
213a5a05b91SHari Bathini {
214a5a05b91SHari Bathini 	struct memblock_region *reg;
215a5a05b91SHari Bathini 	unsigned long tstart, tend;
216a5a05b91SHari Bathini 	unsigned long start_pfn = PHYS_PFN(RMA_START);
217a5a05b91SHari Bathini 	unsigned long end_pfn = PHYS_PFN(RMA_START + fw_dump.boot_memory_size);
218a5a05b91SHari Bathini 	unsigned int ret = 0;
219a5a05b91SHari Bathini 
220a5a05b91SHari Bathini 	for_each_memblock(memory, reg) {
221a5a05b91SHari Bathini 		tstart = max(start_pfn, memblock_region_memory_base_pfn(reg));
222a5a05b91SHari Bathini 		tend = min(end_pfn, memblock_region_memory_end_pfn(reg));
223a5a05b91SHari Bathini 		if (tstart < tend) {
224a5a05b91SHari Bathini 			/* Memory hole from start_pfn to tstart */
225a5a05b91SHari Bathini 			if (tstart > start_pfn)
226a5a05b91SHari Bathini 				break;
227a5a05b91SHari Bathini 
228a5a05b91SHari Bathini 			if (tend == end_pfn) {
229a5a05b91SHari Bathini 				ret = 1;
230a5a05b91SHari Bathini 				break;
231a5a05b91SHari Bathini 			}
232a5a05b91SHari Bathini 
233a5a05b91SHari Bathini 			start_pfn = tend + 1;
234a5a05b91SHari Bathini 		}
235a5a05b91SHari Bathini 	}
236a5a05b91SHari Bathini 
237a5a05b91SHari Bathini 	return ret;
238a5a05b91SHari Bathini }
239a5a05b91SHari Bathini 
240*f86593beSMahesh Salgaonkar /*
241*f86593beSMahesh Salgaonkar  * Returns true, if there are no holes in reserved memory area,
242*f86593beSMahesh Salgaonkar  * false otherwise.
243*f86593beSMahesh Salgaonkar  */
244*f86593beSMahesh Salgaonkar static bool is_reserved_memory_area_contiguous(void)
245*f86593beSMahesh Salgaonkar {
246*f86593beSMahesh Salgaonkar 	struct memblock_region *reg;
247*f86593beSMahesh Salgaonkar 	unsigned long start, end;
248*f86593beSMahesh Salgaonkar 	unsigned long d_start = fw_dump.reserve_dump_area_start;
249*f86593beSMahesh Salgaonkar 	unsigned long d_end = d_start + fw_dump.reserve_dump_area_size;
250*f86593beSMahesh Salgaonkar 
251*f86593beSMahesh Salgaonkar 	for_each_memblock(memory, reg) {
252*f86593beSMahesh Salgaonkar 		start = max(d_start, (unsigned long)reg->base);
253*f86593beSMahesh Salgaonkar 		end = min(d_end, (unsigned long)(reg->base + reg->size));
254*f86593beSMahesh Salgaonkar 		if (d_start < end) {
255*f86593beSMahesh Salgaonkar 			/* Memory hole from d_start to start */
256*f86593beSMahesh Salgaonkar 			if (start > d_start)
257*f86593beSMahesh Salgaonkar 				break;
258*f86593beSMahesh Salgaonkar 
259*f86593beSMahesh Salgaonkar 			if (end == d_end)
260*f86593beSMahesh Salgaonkar 				return true;
261*f86593beSMahesh Salgaonkar 
262*f86593beSMahesh Salgaonkar 			d_start = end + 1;
263*f86593beSMahesh Salgaonkar 		}
264*f86593beSMahesh Salgaonkar 	}
265*f86593beSMahesh Salgaonkar 
266*f86593beSMahesh Salgaonkar 	return false;
267*f86593beSMahesh Salgaonkar }
268*f86593beSMahesh Salgaonkar 
2693ccc00a7SMahesh Salgaonkar /* Print firmware assisted dump configurations for debugging purpose. */
2703ccc00a7SMahesh Salgaonkar static void fadump_show_config(void)
2713ccc00a7SMahesh Salgaonkar {
2723ccc00a7SMahesh Salgaonkar 	pr_debug("Support for firmware-assisted dump (fadump): %s\n",
2733ccc00a7SMahesh Salgaonkar 			(fw_dump.fadump_supported ? "present" : "no support"));
2743ccc00a7SMahesh Salgaonkar 
2753ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_supported)
2763ccc00a7SMahesh Salgaonkar 		return;
2773ccc00a7SMahesh Salgaonkar 
2783ccc00a7SMahesh Salgaonkar 	pr_debug("Fadump enabled    : %s\n",
2793ccc00a7SMahesh Salgaonkar 				(fw_dump.fadump_enabled ? "yes" : "no"));
2803ccc00a7SMahesh Salgaonkar 	pr_debug("Dump Active       : %s\n",
2813ccc00a7SMahesh Salgaonkar 				(fw_dump.dump_active ? "yes" : "no"));
2823ccc00a7SMahesh Salgaonkar 	pr_debug("Dump section sizes:\n");
2833ccc00a7SMahesh Salgaonkar 	pr_debug("    CPU state data size: %lx\n", fw_dump.cpu_state_data_size);
2843ccc00a7SMahesh Salgaonkar 	pr_debug("    HPTE region size   : %lx\n", fw_dump.hpte_region_size);
2853ccc00a7SMahesh Salgaonkar 	pr_debug("Boot memory size  : %lx\n", fw_dump.boot_memory_size);
2863ccc00a7SMahesh Salgaonkar }
2873ccc00a7SMahesh Salgaonkar 
2883ccc00a7SMahesh Salgaonkar static unsigned long init_fadump_mem_struct(struct fadump_mem_struct *fdm,
2893ccc00a7SMahesh Salgaonkar 				unsigned long addr)
2903ccc00a7SMahesh Salgaonkar {
2913ccc00a7SMahesh Salgaonkar 	if (!fdm)
2923ccc00a7SMahesh Salgaonkar 		return 0;
2933ccc00a7SMahesh Salgaonkar 
2943ccc00a7SMahesh Salgaonkar 	memset(fdm, 0, sizeof(struct fadump_mem_struct));
2953ccc00a7SMahesh Salgaonkar 	addr = addr & PAGE_MASK;
2963ccc00a7SMahesh Salgaonkar 
297408cddd9SHari Bathini 	fdm->header.dump_format_version = cpu_to_be32(0x00000001);
298408cddd9SHari Bathini 	fdm->header.dump_num_sections = cpu_to_be16(3);
2993ccc00a7SMahesh Salgaonkar 	fdm->header.dump_status_flag = 0;
3003ccc00a7SMahesh Salgaonkar 	fdm->header.offset_first_dump_section =
301408cddd9SHari Bathini 		cpu_to_be32((u32)offsetof(struct fadump_mem_struct, cpu_state_data));
3023ccc00a7SMahesh Salgaonkar 
3033ccc00a7SMahesh Salgaonkar 	/*
3043ccc00a7SMahesh Salgaonkar 	 * Fields for disk dump option.
3053ccc00a7SMahesh Salgaonkar 	 * We are not using disk dump option, hence set these fields to 0.
3063ccc00a7SMahesh Salgaonkar 	 */
3073ccc00a7SMahesh Salgaonkar 	fdm->header.dd_block_size = 0;
3083ccc00a7SMahesh Salgaonkar 	fdm->header.dd_block_offset = 0;
3093ccc00a7SMahesh Salgaonkar 	fdm->header.dd_num_blocks = 0;
3103ccc00a7SMahesh Salgaonkar 	fdm->header.dd_offset_disk_path = 0;
3113ccc00a7SMahesh Salgaonkar 
3123ccc00a7SMahesh Salgaonkar 	/* set 0 to disable an automatic dump-reboot. */
3133ccc00a7SMahesh Salgaonkar 	fdm->header.max_time_auto = 0;
3143ccc00a7SMahesh Salgaonkar 
3153ccc00a7SMahesh Salgaonkar 	/* Kernel dump sections */
3163ccc00a7SMahesh Salgaonkar 	/* cpu state data section. */
317408cddd9SHari Bathini 	fdm->cpu_state_data.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG);
318408cddd9SHari Bathini 	fdm->cpu_state_data.source_data_type = cpu_to_be16(FADUMP_CPU_STATE_DATA);
3193ccc00a7SMahesh Salgaonkar 	fdm->cpu_state_data.source_address = 0;
320408cddd9SHari Bathini 	fdm->cpu_state_data.source_len = cpu_to_be64(fw_dump.cpu_state_data_size);
321408cddd9SHari Bathini 	fdm->cpu_state_data.destination_address = cpu_to_be64(addr);
3223ccc00a7SMahesh Salgaonkar 	addr += fw_dump.cpu_state_data_size;
3233ccc00a7SMahesh Salgaonkar 
3243ccc00a7SMahesh Salgaonkar 	/* hpte region section */
325408cddd9SHari Bathini 	fdm->hpte_region.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG);
326408cddd9SHari Bathini 	fdm->hpte_region.source_data_type = cpu_to_be16(FADUMP_HPTE_REGION);
3273ccc00a7SMahesh Salgaonkar 	fdm->hpte_region.source_address = 0;
328408cddd9SHari Bathini 	fdm->hpte_region.source_len = cpu_to_be64(fw_dump.hpte_region_size);
329408cddd9SHari Bathini 	fdm->hpte_region.destination_address = cpu_to_be64(addr);
3303ccc00a7SMahesh Salgaonkar 	addr += fw_dump.hpte_region_size;
3313ccc00a7SMahesh Salgaonkar 
3323ccc00a7SMahesh Salgaonkar 	/* RMA region section */
333408cddd9SHari Bathini 	fdm->rmr_region.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG);
334408cddd9SHari Bathini 	fdm->rmr_region.source_data_type = cpu_to_be16(FADUMP_REAL_MODE_REGION);
335408cddd9SHari Bathini 	fdm->rmr_region.source_address = cpu_to_be64(RMA_START);
336408cddd9SHari Bathini 	fdm->rmr_region.source_len = cpu_to_be64(fw_dump.boot_memory_size);
337408cddd9SHari Bathini 	fdm->rmr_region.destination_address = cpu_to_be64(addr);
3383ccc00a7SMahesh Salgaonkar 	addr += fw_dump.boot_memory_size;
3393ccc00a7SMahesh Salgaonkar 
3403ccc00a7SMahesh Salgaonkar 	return addr;
3413ccc00a7SMahesh Salgaonkar }
3423ccc00a7SMahesh Salgaonkar 
343eb39c880SMahesh Salgaonkar /**
344eb39c880SMahesh Salgaonkar  * fadump_calculate_reserve_size(): reserve variable boot area 5% of System RAM
345eb39c880SMahesh Salgaonkar  *
346eb39c880SMahesh Salgaonkar  * Function to find the largest memory size we need to reserve during early
347eb39c880SMahesh Salgaonkar  * boot process. This will be the size of the memory that is required for a
348eb39c880SMahesh Salgaonkar  * kernel to boot successfully.
349eb39c880SMahesh Salgaonkar  *
350eb39c880SMahesh Salgaonkar  * This function has been taken from phyp-assisted dump feature implementation.
351eb39c880SMahesh Salgaonkar  *
352eb39c880SMahesh Salgaonkar  * returns larger of 256MB or 5% rounded down to multiples of 256MB.
353eb39c880SMahesh Salgaonkar  *
354eb39c880SMahesh Salgaonkar  * TODO: Come up with better approach to find out more accurate memory size
355eb39c880SMahesh Salgaonkar  * that is required for a kernel to boot successfully.
356eb39c880SMahesh Salgaonkar  *
357eb39c880SMahesh Salgaonkar  */
358eb39c880SMahesh Salgaonkar static inline unsigned long fadump_calculate_reserve_size(void)
359eb39c880SMahesh Salgaonkar {
36011550dc0SHari Bathini 	int ret;
36111550dc0SHari Bathini 	unsigned long long base, size;
362eb39c880SMahesh Salgaonkar 
36381d9eca5SHari Bathini 	if (fw_dump.reserve_bootvar)
36481d9eca5SHari Bathini 		pr_warn("'fadump_reserve_mem=' parameter is deprecated in favor of 'crashkernel=' parameter.\n");
36581d9eca5SHari Bathini 
366eb39c880SMahesh Salgaonkar 	/*
36711550dc0SHari Bathini 	 * Check if the size is specified through crashkernel= cmdline
368e7467dc6SHari Bathini 	 * option. If yes, then use that but ignore base as fadump reserves
369e7467dc6SHari Bathini 	 * memory at a predefined offset.
370eb39c880SMahesh Salgaonkar 	 */
37111550dc0SHari Bathini 	ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
37211550dc0SHari Bathini 				&size, &base);
37311550dc0SHari Bathini 	if (ret == 0 && size > 0) {
37448a316e3SHari Bathini 		unsigned long max_size;
37548a316e3SHari Bathini 
37681d9eca5SHari Bathini 		if (fw_dump.reserve_bootvar)
37781d9eca5SHari Bathini 			pr_info("Using 'crashkernel=' parameter for memory reservation.\n");
37881d9eca5SHari Bathini 
37911550dc0SHari Bathini 		fw_dump.reserve_bootvar = (unsigned long)size;
38048a316e3SHari Bathini 
38148a316e3SHari Bathini 		/*
38248a316e3SHari Bathini 		 * Adjust if the boot memory size specified is above
38348a316e3SHari Bathini 		 * the upper limit.
38448a316e3SHari Bathini 		 */
38548a316e3SHari Bathini 		max_size = memblock_phys_mem_size() / MAX_BOOT_MEM_RATIO;
38648a316e3SHari Bathini 		if (fw_dump.reserve_bootvar > max_size) {
38748a316e3SHari Bathini 			fw_dump.reserve_bootvar = max_size;
38848a316e3SHari Bathini 			pr_info("Adjusted boot memory size to %luMB\n",
38948a316e3SHari Bathini 				(fw_dump.reserve_bootvar >> 20));
39048a316e3SHari Bathini 		}
39148a316e3SHari Bathini 
392eb39c880SMahesh Salgaonkar 		return fw_dump.reserve_bootvar;
39381d9eca5SHari Bathini 	} else if (fw_dump.reserve_bootvar) {
39481d9eca5SHari Bathini 		/*
39581d9eca5SHari Bathini 		 * 'fadump_reserve_mem=' is being used to reserve memory
39681d9eca5SHari Bathini 		 * for firmware-assisted dump.
39781d9eca5SHari Bathini 		 */
39881d9eca5SHari Bathini 		return fw_dump.reserve_bootvar;
39911550dc0SHari Bathini 	}
400eb39c880SMahesh Salgaonkar 
401eb39c880SMahesh Salgaonkar 	/* divide by 20 to get 5% of value */
40248a316e3SHari Bathini 	size = memblock_phys_mem_size() / 20;
403eb39c880SMahesh Salgaonkar 
404eb39c880SMahesh Salgaonkar 	/* round it down in multiples of 256 */
405eb39c880SMahesh Salgaonkar 	size = size & ~0x0FFFFFFFUL;
406eb39c880SMahesh Salgaonkar 
407eb39c880SMahesh Salgaonkar 	/* Truncate to memory_limit. We don't want to over reserve the memory.*/
408eb39c880SMahesh Salgaonkar 	if (memory_limit && size > memory_limit)
409eb39c880SMahesh Salgaonkar 		size = memory_limit;
410eb39c880SMahesh Salgaonkar 
411eb39c880SMahesh Salgaonkar 	return (size > MIN_BOOT_MEM ? size : MIN_BOOT_MEM);
412eb39c880SMahesh Salgaonkar }
413eb39c880SMahesh Salgaonkar 
414eb39c880SMahesh Salgaonkar /*
415eb39c880SMahesh Salgaonkar  * Calculate the total memory size required to be reserved for
416eb39c880SMahesh Salgaonkar  * firmware-assisted dump registration.
417eb39c880SMahesh Salgaonkar  */
418eb39c880SMahesh Salgaonkar static unsigned long get_fadump_area_size(void)
419eb39c880SMahesh Salgaonkar {
420eb39c880SMahesh Salgaonkar 	unsigned long size = 0;
421eb39c880SMahesh Salgaonkar 
422eb39c880SMahesh Salgaonkar 	size += fw_dump.cpu_state_data_size;
423eb39c880SMahesh Salgaonkar 	size += fw_dump.hpte_region_size;
424eb39c880SMahesh Salgaonkar 	size += fw_dump.boot_memory_size;
4252df173d9SMahesh Salgaonkar 	size += sizeof(struct fadump_crash_info_header);
4262df173d9SMahesh Salgaonkar 	size += sizeof(struct elfhdr); /* ELF core header.*/
427ebaeb5aeSMahesh Salgaonkar 	size += sizeof(struct elf_phdr); /* place holder for cpu notes */
4282df173d9SMahesh Salgaonkar 	/* Program headers for crash memory regions. */
4292df173d9SMahesh Salgaonkar 	size += sizeof(struct elf_phdr) * (memblock_num_regions(memory) + 2);
430eb39c880SMahesh Salgaonkar 
431eb39c880SMahesh Salgaonkar 	size = PAGE_ALIGN(size);
432eb39c880SMahesh Salgaonkar 	return size;
433eb39c880SMahesh Salgaonkar }
434eb39c880SMahesh Salgaonkar 
435b71a693dSMahesh Salgaonkar static void __init fadump_reserve_crash_area(unsigned long base,
436b71a693dSMahesh Salgaonkar 					     unsigned long size)
437b71a693dSMahesh Salgaonkar {
438b71a693dSMahesh Salgaonkar 	struct memblock_region *reg;
439b71a693dSMahesh Salgaonkar 	unsigned long mstart, mend, msize;
440b71a693dSMahesh Salgaonkar 
441b71a693dSMahesh Salgaonkar 	for_each_memblock(memory, reg) {
442b71a693dSMahesh Salgaonkar 		mstart = max_t(unsigned long, base, reg->base);
443b71a693dSMahesh Salgaonkar 		mend = reg->base + reg->size;
444b71a693dSMahesh Salgaonkar 		mend = min(base + size, mend);
445b71a693dSMahesh Salgaonkar 
446b71a693dSMahesh Salgaonkar 		if (mstart < mend) {
447b71a693dSMahesh Salgaonkar 			msize = mend - mstart;
448b71a693dSMahesh Salgaonkar 			memblock_reserve(mstart, msize);
449b71a693dSMahesh Salgaonkar 			pr_info("Reserved %ldMB of memory at %#016lx for saving crash dump\n",
450b71a693dSMahesh Salgaonkar 				(msize >> 20), mstart);
451b71a693dSMahesh Salgaonkar 		}
452b71a693dSMahesh Salgaonkar 	}
453b71a693dSMahesh Salgaonkar }
454b71a693dSMahesh Salgaonkar 
455eb39c880SMahesh Salgaonkar int __init fadump_reserve_mem(void)
456eb39c880SMahesh Salgaonkar {
457eb39c880SMahesh Salgaonkar 	unsigned long base, size, memory_boundary;
458eb39c880SMahesh Salgaonkar 
459eb39c880SMahesh Salgaonkar 	if (!fw_dump.fadump_enabled)
460eb39c880SMahesh Salgaonkar 		return 0;
461eb39c880SMahesh Salgaonkar 
462eb39c880SMahesh Salgaonkar 	if (!fw_dump.fadump_supported) {
463eb39c880SMahesh Salgaonkar 		printk(KERN_INFO "Firmware-assisted dump is not supported on"
464eb39c880SMahesh Salgaonkar 				" this hardware\n");
465eb39c880SMahesh Salgaonkar 		fw_dump.fadump_enabled = 0;
466eb39c880SMahesh Salgaonkar 		return 0;
467eb39c880SMahesh Salgaonkar 	}
4683ccc00a7SMahesh Salgaonkar 	/*
4693ccc00a7SMahesh Salgaonkar 	 * Initialize boot memory size
4703ccc00a7SMahesh Salgaonkar 	 * If dump is active then we have already calculated the size during
4713ccc00a7SMahesh Salgaonkar 	 * first kernel.
4723ccc00a7SMahesh Salgaonkar 	 */
4733ccc00a7SMahesh Salgaonkar 	if (fdm_active)
474408cddd9SHari Bathini 		fw_dump.boot_memory_size = be64_to_cpu(fdm_active->rmr_region.source_len);
475a4e92ce8SMahesh Salgaonkar 	else {
476eb39c880SMahesh Salgaonkar 		fw_dump.boot_memory_size = fadump_calculate_reserve_size();
477a4e92ce8SMahesh Salgaonkar #ifdef CONFIG_CMA
478a4e92ce8SMahesh Salgaonkar 		if (!fw_dump.nocma)
479a4e92ce8SMahesh Salgaonkar 			fw_dump.boot_memory_size =
480a4e92ce8SMahesh Salgaonkar 				ALIGN(fw_dump.boot_memory_size,
481a4e92ce8SMahesh Salgaonkar 							FADUMP_CMA_ALIGNMENT);
482a4e92ce8SMahesh Salgaonkar #endif
483a4e92ce8SMahesh Salgaonkar 	}
484eb39c880SMahesh Salgaonkar 
485eb39c880SMahesh Salgaonkar 	/*
486eb39c880SMahesh Salgaonkar 	 * Calculate the memory boundary.
487eb39c880SMahesh Salgaonkar 	 * If memory_limit is less than actual memory boundary then reserve
488eb39c880SMahesh Salgaonkar 	 * the memory for fadump beyond the memory_limit and adjust the
489eb39c880SMahesh Salgaonkar 	 * memory_limit accordingly, so that the running kernel can run with
490eb39c880SMahesh Salgaonkar 	 * specified memory_limit.
491eb39c880SMahesh Salgaonkar 	 */
492eb39c880SMahesh Salgaonkar 	if (memory_limit && memory_limit < memblock_end_of_DRAM()) {
493eb39c880SMahesh Salgaonkar 		size = get_fadump_area_size();
494eb39c880SMahesh Salgaonkar 		if ((memory_limit + size) < memblock_end_of_DRAM())
495eb39c880SMahesh Salgaonkar 			memory_limit += size;
496eb39c880SMahesh Salgaonkar 		else
497eb39c880SMahesh Salgaonkar 			memory_limit = memblock_end_of_DRAM();
498eb39c880SMahesh Salgaonkar 		printk(KERN_INFO "Adjusted memory_limit for firmware-assisted"
499a84fcd46SSuzuki Poulose 				" dump, now %#016llx\n", memory_limit);
500eb39c880SMahesh Salgaonkar 	}
501eb39c880SMahesh Salgaonkar 	if (memory_limit)
502eb39c880SMahesh Salgaonkar 		memory_boundary = memory_limit;
503eb39c880SMahesh Salgaonkar 	else
504eb39c880SMahesh Salgaonkar 		memory_boundary = memblock_end_of_DRAM();
505eb39c880SMahesh Salgaonkar 
506eb39c880SMahesh Salgaonkar 	if (fw_dump.dump_active) {
507b71a693dSMahesh Salgaonkar 		pr_info("Firmware-assisted dump is active.\n");
508b71a693dSMahesh Salgaonkar 
50985975387SHari Bathini #ifdef CONFIG_HUGETLB_PAGE
51085975387SHari Bathini 		/*
51185975387SHari Bathini 		 * FADump capture kernel doesn't care much about hugepages.
51285975387SHari Bathini 		 * In fact, handling hugepages in capture kernel is asking for
51385975387SHari Bathini 		 * trouble. So, disable HugeTLB support when fadump is active.
51485975387SHari Bathini 		 */
51585975387SHari Bathini 		hugetlb_disabled = true;
51685975387SHari Bathini #endif
517eb39c880SMahesh Salgaonkar 		/*
518eb39c880SMahesh Salgaonkar 		 * If last boot has crashed then reserve all the memory
519eb39c880SMahesh Salgaonkar 		 * above boot_memory_size so that we don't touch it until
520eb39c880SMahesh Salgaonkar 		 * dump is written to disk by userspace tool. This memory
521eb39c880SMahesh Salgaonkar 		 * will be released for general use once the dump is saved.
522eb39c880SMahesh Salgaonkar 		 */
523eb39c880SMahesh Salgaonkar 		base = fw_dump.boot_memory_size;
524eb39c880SMahesh Salgaonkar 		size = memory_boundary - base;
525b71a693dSMahesh Salgaonkar 		fadump_reserve_crash_area(base, size);
5262df173d9SMahesh Salgaonkar 
5272df173d9SMahesh Salgaonkar 		fw_dump.fadumphdr_addr =
528408cddd9SHari Bathini 				be64_to_cpu(fdm_active->rmr_region.destination_address) +
529408cddd9SHari Bathini 				be64_to_cpu(fdm_active->rmr_region.source_len);
530a4e92ce8SMahesh Salgaonkar 		pr_debug("fadumphdr_addr = %pa\n", &fw_dump.fadumphdr_addr);
531a4e92ce8SMahesh Salgaonkar 		fw_dump.reserve_dump_area_start = base;
532a4e92ce8SMahesh Salgaonkar 		fw_dump.reserve_dump_area_size = size;
533eb39c880SMahesh Salgaonkar 	} else {
534eb39c880SMahesh Salgaonkar 		size = get_fadump_area_size();
535f6e6bedbSHari Bathini 
536f6e6bedbSHari Bathini 		/*
537f6e6bedbSHari Bathini 		 * Reserve memory at an offset closer to bottom of the RAM to
538f6e6bedbSHari Bathini 		 * minimize the impact of memory hot-remove operation. We can't
539f6e6bedbSHari Bathini 		 * use memblock_find_in_range() here since it doesn't allocate
540f6e6bedbSHari Bathini 		 * from bottom to top.
541f6e6bedbSHari Bathini 		 */
542f6e6bedbSHari Bathini 		for (base = fw_dump.boot_memory_size;
543f6e6bedbSHari Bathini 		     base <= (memory_boundary - size);
544f6e6bedbSHari Bathini 		     base += size) {
545f6e6bedbSHari Bathini 			if (memblock_is_region_memory(base, size) &&
546f6e6bedbSHari Bathini 			    !memblock_is_region_reserved(base, size))
547f6e6bedbSHari Bathini 				break;
548eb39c880SMahesh Salgaonkar 		}
549f6e6bedbSHari Bathini 		if ((base > (memory_boundary - size)) ||
550f6e6bedbSHari Bathini 		    memblock_reserve(base, size)) {
551f6e6bedbSHari Bathini 			pr_err("Failed to reserve memory\n");
552f6e6bedbSHari Bathini 			return 0;
553f6e6bedbSHari Bathini 		}
554f6e6bedbSHari Bathini 
555f6e6bedbSHari Bathini 		pr_info("Reserved %ldMB of memory at %ldMB for firmware-"
556f6e6bedbSHari Bathini 			"assisted dump (System RAM: %ldMB)\n",
557f6e6bedbSHari Bathini 			(unsigned long)(size >> 20),
558f6e6bedbSHari Bathini 			(unsigned long)(base >> 20),
559f6e6bedbSHari Bathini 			(unsigned long)(memblock_phys_mem_size() >> 20));
560f6e6bedbSHari Bathini 
561eb39c880SMahesh Salgaonkar 		fw_dump.reserve_dump_area_start = base;
562eb39c880SMahesh Salgaonkar 		fw_dump.reserve_dump_area_size = size;
563a4e92ce8SMahesh Salgaonkar 		return fadump_cma_init();
564a4e92ce8SMahesh Salgaonkar 	}
565eb39c880SMahesh Salgaonkar 	return 1;
566eb39c880SMahesh Salgaonkar }
567eb39c880SMahesh Salgaonkar 
5681e76609cSSrikar Dronamraju unsigned long __init arch_reserved_kernel_pages(void)
5691e76609cSSrikar Dronamraju {
5701e76609cSSrikar Dronamraju 	return memblock_reserved_size() / PAGE_SIZE;
5711e76609cSSrikar Dronamraju }
5721e76609cSSrikar Dronamraju 
573eb39c880SMahesh Salgaonkar /* Look for fadump= cmdline option. */
574eb39c880SMahesh Salgaonkar static int __init early_fadump_param(char *p)
575eb39c880SMahesh Salgaonkar {
576eb39c880SMahesh Salgaonkar 	if (!p)
577eb39c880SMahesh Salgaonkar 		return 1;
578eb39c880SMahesh Salgaonkar 
579eb39c880SMahesh Salgaonkar 	if (strncmp(p, "on", 2) == 0)
580eb39c880SMahesh Salgaonkar 		fw_dump.fadump_enabled = 1;
581eb39c880SMahesh Salgaonkar 	else if (strncmp(p, "off", 3) == 0)
582eb39c880SMahesh Salgaonkar 		fw_dump.fadump_enabled = 0;
583a4e92ce8SMahesh Salgaonkar 	else if (strncmp(p, "nocma", 5) == 0) {
584a4e92ce8SMahesh Salgaonkar 		fw_dump.fadump_enabled = 1;
585a4e92ce8SMahesh Salgaonkar 		fw_dump.nocma = 1;
586a4e92ce8SMahesh Salgaonkar 	}
587eb39c880SMahesh Salgaonkar 
588eb39c880SMahesh Salgaonkar 	return 0;
589eb39c880SMahesh Salgaonkar }
590eb39c880SMahesh Salgaonkar early_param("fadump", early_fadump_param);
591eb39c880SMahesh Salgaonkar 
59281d9eca5SHari Bathini /*
59381d9eca5SHari Bathini  * Look for fadump_reserve_mem= cmdline option
59481d9eca5SHari Bathini  * TODO: Remove references to 'fadump_reserve_mem=' parameter,
59581d9eca5SHari Bathini  *       the sooner 'crashkernel=' parameter is accustomed to.
59681d9eca5SHari Bathini  */
59781d9eca5SHari Bathini static int __init early_fadump_reserve_mem(char *p)
59881d9eca5SHari Bathini {
59981d9eca5SHari Bathini 	if (p)
60081d9eca5SHari Bathini 		fw_dump.reserve_bootvar = memparse(p, &p);
60181d9eca5SHari Bathini 	return 0;
60281d9eca5SHari Bathini }
60381d9eca5SHari Bathini early_param("fadump_reserve_mem", early_fadump_reserve_mem);
60481d9eca5SHari Bathini 
60598b8cd7fSMichal Suchanek static int register_fw_dump(struct fadump_mem_struct *fdm)
6063ccc00a7SMahesh Salgaonkar {
60798b8cd7fSMichal Suchanek 	int rc, err;
6083ccc00a7SMahesh Salgaonkar 	unsigned int wait_time;
6093ccc00a7SMahesh Salgaonkar 
6103ccc00a7SMahesh Salgaonkar 	pr_debug("Registering for firmware-assisted kernel dump...\n");
6113ccc00a7SMahesh Salgaonkar 
6123ccc00a7SMahesh Salgaonkar 	/* TODO: Add upper time limit for the delay */
6133ccc00a7SMahesh Salgaonkar 	do {
6143ccc00a7SMahesh Salgaonkar 		rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
6153ccc00a7SMahesh Salgaonkar 			FADUMP_REGISTER, fdm,
6163ccc00a7SMahesh Salgaonkar 			sizeof(struct fadump_mem_struct));
6173ccc00a7SMahesh Salgaonkar 
6183ccc00a7SMahesh Salgaonkar 		wait_time = rtas_busy_delay_time(rc);
6193ccc00a7SMahesh Salgaonkar 		if (wait_time)
6203ccc00a7SMahesh Salgaonkar 			mdelay(wait_time);
6213ccc00a7SMahesh Salgaonkar 
6223ccc00a7SMahesh Salgaonkar 	} while (wait_time);
6233ccc00a7SMahesh Salgaonkar 
62498b8cd7fSMichal Suchanek 	err = -EIO;
6253ccc00a7SMahesh Salgaonkar 	switch (rc) {
62698b8cd7fSMichal Suchanek 	default:
62798b8cd7fSMichal Suchanek 		pr_err("Failed to register. Unknown Error(%d).\n", rc);
62898b8cd7fSMichal Suchanek 		break;
6293ccc00a7SMahesh Salgaonkar 	case -1:
6303ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "Failed to register firmware-assisted kernel"
6313ccc00a7SMahesh Salgaonkar 			" dump. Hardware Error(%d).\n", rc);
6323ccc00a7SMahesh Salgaonkar 		break;
6333ccc00a7SMahesh Salgaonkar 	case -3:
634a5a05b91SHari Bathini 		if (!is_boot_memory_area_contiguous())
635*f86593beSMahesh Salgaonkar 			pr_err("Can't have holes in boot memory area while registering fadump\n");
636*f86593beSMahesh Salgaonkar 		else if (!is_reserved_memory_area_contiguous())
637*f86593beSMahesh Salgaonkar 			pr_err("Can't have holes in reserved memory area while"
638a5a05b91SHari Bathini 			       " registering fadump\n");
639a5a05b91SHari Bathini 
6403ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "Failed to register firmware-assisted kernel"
6413ccc00a7SMahesh Salgaonkar 			" dump. Parameter Error(%d).\n", rc);
64298b8cd7fSMichal Suchanek 		err = -EINVAL;
6433ccc00a7SMahesh Salgaonkar 		break;
6443ccc00a7SMahesh Salgaonkar 	case -9:
6453ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "firmware-assisted kernel dump is already "
6463ccc00a7SMahesh Salgaonkar 			" registered.");
6473ccc00a7SMahesh Salgaonkar 		fw_dump.dump_registered = 1;
64898b8cd7fSMichal Suchanek 		err = -EEXIST;
6493ccc00a7SMahesh Salgaonkar 		break;
6503ccc00a7SMahesh Salgaonkar 	case 0:
6513ccc00a7SMahesh Salgaonkar 		printk(KERN_INFO "firmware-assisted kernel dump registration"
6523ccc00a7SMahesh Salgaonkar 			" is successful\n");
6533ccc00a7SMahesh Salgaonkar 		fw_dump.dump_registered = 1;
65498b8cd7fSMichal Suchanek 		err = 0;
6553ccc00a7SMahesh Salgaonkar 		break;
6563ccc00a7SMahesh Salgaonkar 	}
65798b8cd7fSMichal Suchanek 	return err;
6583ccc00a7SMahesh Salgaonkar }
6593ccc00a7SMahesh Salgaonkar 
660ebaeb5aeSMahesh Salgaonkar void crash_fadump(struct pt_regs *regs, const char *str)
661ebaeb5aeSMahesh Salgaonkar {
662ebaeb5aeSMahesh Salgaonkar 	struct fadump_crash_info_header *fdh = NULL;
663f2a5e8f0SMahesh Salgaonkar 	int old_cpu, this_cpu;
664ebaeb5aeSMahesh Salgaonkar 
6656fcd6baaSNicholas Piggin 	if (!should_fadump_crash())
666ebaeb5aeSMahesh Salgaonkar 		return;
667ebaeb5aeSMahesh Salgaonkar 
668f2a5e8f0SMahesh Salgaonkar 	/*
669f2a5e8f0SMahesh Salgaonkar 	 * old_cpu == -1 means this is the first CPU which has come here,
670f2a5e8f0SMahesh Salgaonkar 	 * go ahead and trigger fadump.
671f2a5e8f0SMahesh Salgaonkar 	 *
672f2a5e8f0SMahesh Salgaonkar 	 * old_cpu != -1 means some other CPU has already on it's way
673f2a5e8f0SMahesh Salgaonkar 	 * to trigger fadump, just keep looping here.
674f2a5e8f0SMahesh Salgaonkar 	 */
675f2a5e8f0SMahesh Salgaonkar 	this_cpu = smp_processor_id();
676f2a5e8f0SMahesh Salgaonkar 	old_cpu = cmpxchg(&crashing_cpu, -1, this_cpu);
677f2a5e8f0SMahesh Salgaonkar 
678f2a5e8f0SMahesh Salgaonkar 	if (old_cpu != -1) {
679f2a5e8f0SMahesh Salgaonkar 		/*
680f2a5e8f0SMahesh Salgaonkar 		 * We can't loop here indefinitely. Wait as long as fadump
681f2a5e8f0SMahesh Salgaonkar 		 * is in force. If we race with fadump un-registration this
682f2a5e8f0SMahesh Salgaonkar 		 * loop will break and then we go down to normal panic path
683f2a5e8f0SMahesh Salgaonkar 		 * and reboot. If fadump is in force the first crashing
684f2a5e8f0SMahesh Salgaonkar 		 * cpu will definitely trigger fadump.
685f2a5e8f0SMahesh Salgaonkar 		 */
686f2a5e8f0SMahesh Salgaonkar 		while (fw_dump.dump_registered)
687f2a5e8f0SMahesh Salgaonkar 			cpu_relax();
688f2a5e8f0SMahesh Salgaonkar 		return;
689f2a5e8f0SMahesh Salgaonkar 	}
690f2a5e8f0SMahesh Salgaonkar 
691ebaeb5aeSMahesh Salgaonkar 	fdh = __va(fw_dump.fadumphdr_addr);
692ebaeb5aeSMahesh Salgaonkar 	fdh->crashing_cpu = crashing_cpu;
693ebaeb5aeSMahesh Salgaonkar 	crash_save_vmcoreinfo();
694ebaeb5aeSMahesh Salgaonkar 
695ebaeb5aeSMahesh Salgaonkar 	if (regs)
696ebaeb5aeSMahesh Salgaonkar 		fdh->regs = *regs;
697ebaeb5aeSMahesh Salgaonkar 	else
698ebaeb5aeSMahesh Salgaonkar 		ppc_save_regs(&fdh->regs);
699ebaeb5aeSMahesh Salgaonkar 
700a0512164SRasmus Villemoes 	fdh->online_mask = *cpu_online_mask;
701ebaeb5aeSMahesh Salgaonkar 
702ebaeb5aeSMahesh Salgaonkar 	/* Call ibm,os-term rtas call to trigger firmware assisted dump */
703ebaeb5aeSMahesh Salgaonkar 	rtas_os_term((char *)str);
704ebaeb5aeSMahesh Salgaonkar }
705ebaeb5aeSMahesh Salgaonkar 
706ebaeb5aeSMahesh Salgaonkar #define GPR_MASK	0xffffff0000000000
707ebaeb5aeSMahesh Salgaonkar static inline int fadump_gpr_index(u64 id)
708ebaeb5aeSMahesh Salgaonkar {
709ebaeb5aeSMahesh Salgaonkar 	int i = -1;
710ebaeb5aeSMahesh Salgaonkar 	char str[3];
711ebaeb5aeSMahesh Salgaonkar 
712ebaeb5aeSMahesh Salgaonkar 	if ((id & GPR_MASK) == REG_ID("GPR")) {
713ebaeb5aeSMahesh Salgaonkar 		/* get the digits at the end */
714ebaeb5aeSMahesh Salgaonkar 		id &= ~GPR_MASK;
715ebaeb5aeSMahesh Salgaonkar 		id >>= 24;
716ebaeb5aeSMahesh Salgaonkar 		str[2] = '\0';
717ebaeb5aeSMahesh Salgaonkar 		str[1] = id & 0xff;
718ebaeb5aeSMahesh Salgaonkar 		str[0] = (id >> 8) & 0xff;
719ebaeb5aeSMahesh Salgaonkar 		sscanf(str, "%d", &i);
720ebaeb5aeSMahesh Salgaonkar 		if (i > 31)
721ebaeb5aeSMahesh Salgaonkar 			i = -1;
722ebaeb5aeSMahesh Salgaonkar 	}
723ebaeb5aeSMahesh Salgaonkar 	return i;
724ebaeb5aeSMahesh Salgaonkar }
725ebaeb5aeSMahesh Salgaonkar 
726ebaeb5aeSMahesh Salgaonkar static inline void fadump_set_regval(struct pt_regs *regs, u64 reg_id,
727ebaeb5aeSMahesh Salgaonkar 								u64 reg_val)
728ebaeb5aeSMahesh Salgaonkar {
729ebaeb5aeSMahesh Salgaonkar 	int i;
730ebaeb5aeSMahesh Salgaonkar 
731ebaeb5aeSMahesh Salgaonkar 	i = fadump_gpr_index(reg_id);
732ebaeb5aeSMahesh Salgaonkar 	if (i >= 0)
733ebaeb5aeSMahesh Salgaonkar 		regs->gpr[i] = (unsigned long)reg_val;
734ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("NIA"))
735ebaeb5aeSMahesh Salgaonkar 		regs->nip = (unsigned long)reg_val;
736ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("MSR"))
737ebaeb5aeSMahesh Salgaonkar 		regs->msr = (unsigned long)reg_val;
738ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("CTR"))
739ebaeb5aeSMahesh Salgaonkar 		regs->ctr = (unsigned long)reg_val;
740ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("LR"))
741ebaeb5aeSMahesh Salgaonkar 		regs->link = (unsigned long)reg_val;
742ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("XER"))
743ebaeb5aeSMahesh Salgaonkar 		regs->xer = (unsigned long)reg_val;
744ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("CR"))
745ebaeb5aeSMahesh Salgaonkar 		regs->ccr = (unsigned long)reg_val;
746ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("DAR"))
747ebaeb5aeSMahesh Salgaonkar 		regs->dar = (unsigned long)reg_val;
748ebaeb5aeSMahesh Salgaonkar 	else if (reg_id == REG_ID("DSISR"))
749ebaeb5aeSMahesh Salgaonkar 		regs->dsisr = (unsigned long)reg_val;
750ebaeb5aeSMahesh Salgaonkar }
751ebaeb5aeSMahesh Salgaonkar 
752ebaeb5aeSMahesh Salgaonkar static struct fadump_reg_entry*
753ebaeb5aeSMahesh Salgaonkar fadump_read_registers(struct fadump_reg_entry *reg_entry, struct pt_regs *regs)
754ebaeb5aeSMahesh Salgaonkar {
755ebaeb5aeSMahesh Salgaonkar 	memset(regs, 0, sizeof(struct pt_regs));
756ebaeb5aeSMahesh Salgaonkar 
757408cddd9SHari Bathini 	while (be64_to_cpu(reg_entry->reg_id) != REG_ID("CPUEND")) {
758408cddd9SHari Bathini 		fadump_set_regval(regs, be64_to_cpu(reg_entry->reg_id),
759408cddd9SHari Bathini 					be64_to_cpu(reg_entry->reg_value));
760ebaeb5aeSMahesh Salgaonkar 		reg_entry++;
761ebaeb5aeSMahesh Salgaonkar 	}
762ebaeb5aeSMahesh Salgaonkar 	reg_entry++;
763ebaeb5aeSMahesh Salgaonkar 	return reg_entry;
764ebaeb5aeSMahesh Salgaonkar }
765ebaeb5aeSMahesh Salgaonkar 
766ebaeb5aeSMahesh Salgaonkar static u32 *fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs)
767ebaeb5aeSMahesh Salgaonkar {
768ebaeb5aeSMahesh Salgaonkar 	struct elf_prstatus prstatus;
769ebaeb5aeSMahesh Salgaonkar 
770ebaeb5aeSMahesh Salgaonkar 	memset(&prstatus, 0, sizeof(prstatus));
771ebaeb5aeSMahesh Salgaonkar 	/*
772ebaeb5aeSMahesh Salgaonkar 	 * FIXME: How do i get PID? Do I really need it?
773ebaeb5aeSMahesh Salgaonkar 	 * prstatus.pr_pid = ????
774ebaeb5aeSMahesh Salgaonkar 	 */
775ebaeb5aeSMahesh Salgaonkar 	elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
77622bd0177SHari Bathini 	buf = append_elf_note(buf, CRASH_CORE_NOTE_NAME, NT_PRSTATUS,
777ebaeb5aeSMahesh Salgaonkar 			      &prstatus, sizeof(prstatus));
778ebaeb5aeSMahesh Salgaonkar 	return buf;
779ebaeb5aeSMahesh Salgaonkar }
780ebaeb5aeSMahesh Salgaonkar 
781ebaeb5aeSMahesh Salgaonkar static void fadump_update_elfcore_header(char *bufp)
782ebaeb5aeSMahesh Salgaonkar {
783ebaeb5aeSMahesh Salgaonkar 	struct elfhdr *elf;
784ebaeb5aeSMahesh Salgaonkar 	struct elf_phdr *phdr;
785ebaeb5aeSMahesh Salgaonkar 
786ebaeb5aeSMahesh Salgaonkar 	elf = (struct elfhdr *)bufp;
787ebaeb5aeSMahesh Salgaonkar 	bufp += sizeof(struct elfhdr);
788ebaeb5aeSMahesh Salgaonkar 
789ebaeb5aeSMahesh Salgaonkar 	/* First note is a place holder for cpu notes info. */
790ebaeb5aeSMahesh Salgaonkar 	phdr = (struct elf_phdr *)bufp;
791ebaeb5aeSMahesh Salgaonkar 
792ebaeb5aeSMahesh Salgaonkar 	if (phdr->p_type == PT_NOTE) {
793ebaeb5aeSMahesh Salgaonkar 		phdr->p_paddr = fw_dump.cpu_notes_buf;
794ebaeb5aeSMahesh Salgaonkar 		phdr->p_offset	= phdr->p_paddr;
795ebaeb5aeSMahesh Salgaonkar 		phdr->p_filesz	= fw_dump.cpu_notes_buf_size;
796ebaeb5aeSMahesh Salgaonkar 		phdr->p_memsz = fw_dump.cpu_notes_buf_size;
797ebaeb5aeSMahesh Salgaonkar 	}
798ebaeb5aeSMahesh Salgaonkar 	return;
799ebaeb5aeSMahesh Salgaonkar }
800ebaeb5aeSMahesh Salgaonkar 
801ebaeb5aeSMahesh Salgaonkar static void *fadump_cpu_notes_buf_alloc(unsigned long size)
802ebaeb5aeSMahesh Salgaonkar {
803ebaeb5aeSMahesh Salgaonkar 	void *vaddr;
804ebaeb5aeSMahesh Salgaonkar 	struct page *page;
805ebaeb5aeSMahesh Salgaonkar 	unsigned long order, count, i;
806ebaeb5aeSMahesh Salgaonkar 
807ebaeb5aeSMahesh Salgaonkar 	order = get_order(size);
808ebaeb5aeSMahesh Salgaonkar 	vaddr = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
809ebaeb5aeSMahesh Salgaonkar 	if (!vaddr)
810ebaeb5aeSMahesh Salgaonkar 		return NULL;
811ebaeb5aeSMahesh Salgaonkar 
812ebaeb5aeSMahesh Salgaonkar 	count = 1 << order;
813ebaeb5aeSMahesh Salgaonkar 	page = virt_to_page(vaddr);
814ebaeb5aeSMahesh Salgaonkar 	for (i = 0; i < count; i++)
815ebaeb5aeSMahesh Salgaonkar 		SetPageReserved(page + i);
816ebaeb5aeSMahesh Salgaonkar 	return vaddr;
817ebaeb5aeSMahesh Salgaonkar }
818ebaeb5aeSMahesh Salgaonkar 
819ebaeb5aeSMahesh Salgaonkar static void fadump_cpu_notes_buf_free(unsigned long vaddr, unsigned long size)
820ebaeb5aeSMahesh Salgaonkar {
821ebaeb5aeSMahesh Salgaonkar 	struct page *page;
822ebaeb5aeSMahesh Salgaonkar 	unsigned long order, count, i;
823ebaeb5aeSMahesh Salgaonkar 
824ebaeb5aeSMahesh Salgaonkar 	order = get_order(size);
825ebaeb5aeSMahesh Salgaonkar 	count = 1 << order;
826ebaeb5aeSMahesh Salgaonkar 	page = virt_to_page(vaddr);
827ebaeb5aeSMahesh Salgaonkar 	for (i = 0; i < count; i++)
828ebaeb5aeSMahesh Salgaonkar 		ClearPageReserved(page + i);
829ebaeb5aeSMahesh Salgaonkar 	__free_pages(page, order);
830ebaeb5aeSMahesh Salgaonkar }
831ebaeb5aeSMahesh Salgaonkar 
832ebaeb5aeSMahesh Salgaonkar /*
833ebaeb5aeSMahesh Salgaonkar  * Read CPU state dump data and convert it into ELF notes.
834ebaeb5aeSMahesh Salgaonkar  * The CPU dump starts with magic number "REGSAVE". NumCpusOffset should be
835ebaeb5aeSMahesh Salgaonkar  * used to access the data to allow for additional fields to be added without
836ebaeb5aeSMahesh Salgaonkar  * affecting compatibility. Each list of registers for a CPU starts with
837ebaeb5aeSMahesh Salgaonkar  * "CPUSTRT" and ends with "CPUEND". Each register entry is of 16 bytes,
838ebaeb5aeSMahesh Salgaonkar  * 8 Byte ASCII identifier and 8 Byte register value. The register entry
839ebaeb5aeSMahesh Salgaonkar  * with identifier "CPUSTRT" and "CPUEND" contains 4 byte cpu id as part
840ebaeb5aeSMahesh Salgaonkar  * of register value. For more details refer to PAPR document.
841ebaeb5aeSMahesh Salgaonkar  *
842ebaeb5aeSMahesh Salgaonkar  * Only for the crashing cpu we ignore the CPU dump data and get exact
843ebaeb5aeSMahesh Salgaonkar  * state from fadump crash info structure populated by first kernel at the
844ebaeb5aeSMahesh Salgaonkar  * time of crash.
845ebaeb5aeSMahesh Salgaonkar  */
846ebaeb5aeSMahesh Salgaonkar static int __init fadump_build_cpu_notes(const struct fadump_mem_struct *fdm)
847ebaeb5aeSMahesh Salgaonkar {
848ebaeb5aeSMahesh Salgaonkar 	struct fadump_reg_save_area_header *reg_header;
849ebaeb5aeSMahesh Salgaonkar 	struct fadump_reg_entry *reg_entry;
850ebaeb5aeSMahesh Salgaonkar 	struct fadump_crash_info_header *fdh = NULL;
851ebaeb5aeSMahesh Salgaonkar 	void *vaddr;
852ebaeb5aeSMahesh Salgaonkar 	unsigned long addr;
853ebaeb5aeSMahesh Salgaonkar 	u32 num_cpus, *note_buf;
854ebaeb5aeSMahesh Salgaonkar 	struct pt_regs regs;
855ebaeb5aeSMahesh Salgaonkar 	int i, rc = 0, cpu = 0;
856ebaeb5aeSMahesh Salgaonkar 
857ebaeb5aeSMahesh Salgaonkar 	if (!fdm->cpu_state_data.bytes_dumped)
858ebaeb5aeSMahesh Salgaonkar 		return -EINVAL;
859ebaeb5aeSMahesh Salgaonkar 
860408cddd9SHari Bathini 	addr = be64_to_cpu(fdm->cpu_state_data.destination_address);
861ebaeb5aeSMahesh Salgaonkar 	vaddr = __va(addr);
862ebaeb5aeSMahesh Salgaonkar 
863ebaeb5aeSMahesh Salgaonkar 	reg_header = vaddr;
864408cddd9SHari Bathini 	if (be64_to_cpu(reg_header->magic_number) != REGSAVE_AREA_MAGIC) {
865ebaeb5aeSMahesh Salgaonkar 		printk(KERN_ERR "Unable to read register save area.\n");
866ebaeb5aeSMahesh Salgaonkar 		return -ENOENT;
867ebaeb5aeSMahesh Salgaonkar 	}
868ebaeb5aeSMahesh Salgaonkar 	pr_debug("--------CPU State Data------------\n");
869408cddd9SHari Bathini 	pr_debug("Magic Number: %llx\n", be64_to_cpu(reg_header->magic_number));
870408cddd9SHari Bathini 	pr_debug("NumCpuOffset: %x\n", be32_to_cpu(reg_header->num_cpu_offset));
871ebaeb5aeSMahesh Salgaonkar 
872408cddd9SHari Bathini 	vaddr += be32_to_cpu(reg_header->num_cpu_offset);
873408cddd9SHari Bathini 	num_cpus = be32_to_cpu(*((__be32 *)(vaddr)));
874ebaeb5aeSMahesh Salgaonkar 	pr_debug("NumCpus     : %u\n", num_cpus);
875ebaeb5aeSMahesh Salgaonkar 	vaddr += sizeof(u32);
876ebaeb5aeSMahesh Salgaonkar 	reg_entry = (struct fadump_reg_entry *)vaddr;
877ebaeb5aeSMahesh Salgaonkar 
878ebaeb5aeSMahesh Salgaonkar 	/* Allocate buffer to hold cpu crash notes. */
879ebaeb5aeSMahesh Salgaonkar 	fw_dump.cpu_notes_buf_size = num_cpus * sizeof(note_buf_t);
880ebaeb5aeSMahesh Salgaonkar 	fw_dump.cpu_notes_buf_size = PAGE_ALIGN(fw_dump.cpu_notes_buf_size);
881ebaeb5aeSMahesh Salgaonkar 	note_buf = fadump_cpu_notes_buf_alloc(fw_dump.cpu_notes_buf_size);
882ebaeb5aeSMahesh Salgaonkar 	if (!note_buf) {
883ebaeb5aeSMahesh Salgaonkar 		printk(KERN_ERR "Failed to allocate 0x%lx bytes for "
884ebaeb5aeSMahesh Salgaonkar 			"cpu notes buffer\n", fw_dump.cpu_notes_buf_size);
885ebaeb5aeSMahesh Salgaonkar 		return -ENOMEM;
886ebaeb5aeSMahesh Salgaonkar 	}
887ebaeb5aeSMahesh Salgaonkar 	fw_dump.cpu_notes_buf = __pa(note_buf);
888ebaeb5aeSMahesh Salgaonkar 
889ebaeb5aeSMahesh Salgaonkar 	pr_debug("Allocated buffer for cpu notes of size %ld at %p\n",
890ebaeb5aeSMahesh Salgaonkar 			(num_cpus * sizeof(note_buf_t)), note_buf);
891ebaeb5aeSMahesh Salgaonkar 
892ebaeb5aeSMahesh Salgaonkar 	if (fw_dump.fadumphdr_addr)
893ebaeb5aeSMahesh Salgaonkar 		fdh = __va(fw_dump.fadumphdr_addr);
894ebaeb5aeSMahesh Salgaonkar 
895ebaeb5aeSMahesh Salgaonkar 	for (i = 0; i < num_cpus; i++) {
896408cddd9SHari Bathini 		if (be64_to_cpu(reg_entry->reg_id) != REG_ID("CPUSTRT")) {
897ebaeb5aeSMahesh Salgaonkar 			printk(KERN_ERR "Unable to read CPU state data\n");
898ebaeb5aeSMahesh Salgaonkar 			rc = -ENOENT;
899ebaeb5aeSMahesh Salgaonkar 			goto error_out;
900ebaeb5aeSMahesh Salgaonkar 		}
901ebaeb5aeSMahesh Salgaonkar 		/* Lower 4 bytes of reg_value contains logical cpu id */
902408cddd9SHari Bathini 		cpu = be64_to_cpu(reg_entry->reg_value) & FADUMP_CPU_ID_MASK;
903a0512164SRasmus Villemoes 		if (fdh && !cpumask_test_cpu(cpu, &fdh->online_mask)) {
904ebaeb5aeSMahesh Salgaonkar 			SKIP_TO_NEXT_CPU(reg_entry);
905ebaeb5aeSMahesh Salgaonkar 			continue;
906ebaeb5aeSMahesh Salgaonkar 		}
907ebaeb5aeSMahesh Salgaonkar 		pr_debug("Reading register data for cpu %d...\n", cpu);
908ebaeb5aeSMahesh Salgaonkar 		if (fdh && fdh->crashing_cpu == cpu) {
909ebaeb5aeSMahesh Salgaonkar 			regs = fdh->regs;
910ebaeb5aeSMahesh Salgaonkar 			note_buf = fadump_regs_to_elf_notes(note_buf, &regs);
911ebaeb5aeSMahesh Salgaonkar 			SKIP_TO_NEXT_CPU(reg_entry);
912ebaeb5aeSMahesh Salgaonkar 		} else {
913ebaeb5aeSMahesh Salgaonkar 			reg_entry++;
914ebaeb5aeSMahesh Salgaonkar 			reg_entry = fadump_read_registers(reg_entry, &regs);
915ebaeb5aeSMahesh Salgaonkar 			note_buf = fadump_regs_to_elf_notes(note_buf, &regs);
916ebaeb5aeSMahesh Salgaonkar 		}
917ebaeb5aeSMahesh Salgaonkar 	}
91822bd0177SHari Bathini 	final_note(note_buf);
919ebaeb5aeSMahesh Salgaonkar 
920b717d985SRickard Strandqvist 	if (fdh) {
921ebaeb5aeSMahesh Salgaonkar 		pr_debug("Updating elfcore header (%llx) with cpu notes\n",
922ebaeb5aeSMahesh Salgaonkar 							fdh->elfcorehdr_addr);
923ebaeb5aeSMahesh Salgaonkar 		fadump_update_elfcore_header((char *)__va(fdh->elfcorehdr_addr));
924b717d985SRickard Strandqvist 	}
925ebaeb5aeSMahesh Salgaonkar 	return 0;
926ebaeb5aeSMahesh Salgaonkar 
927ebaeb5aeSMahesh Salgaonkar error_out:
928ebaeb5aeSMahesh Salgaonkar 	fadump_cpu_notes_buf_free((unsigned long)__va(fw_dump.cpu_notes_buf),
929ebaeb5aeSMahesh Salgaonkar 					fw_dump.cpu_notes_buf_size);
930ebaeb5aeSMahesh Salgaonkar 	fw_dump.cpu_notes_buf = 0;
931ebaeb5aeSMahesh Salgaonkar 	fw_dump.cpu_notes_buf_size = 0;
932ebaeb5aeSMahesh Salgaonkar 	return rc;
933ebaeb5aeSMahesh Salgaonkar 
934ebaeb5aeSMahesh Salgaonkar }
935ebaeb5aeSMahesh Salgaonkar 
9362df173d9SMahesh Salgaonkar /*
9372df173d9SMahesh Salgaonkar  * Validate and process the dump data stored by firmware before exporting
9382df173d9SMahesh Salgaonkar  * it through '/proc/vmcore'.
9392df173d9SMahesh Salgaonkar  */
9402df173d9SMahesh Salgaonkar static int __init process_fadump(const struct fadump_mem_struct *fdm_active)
9412df173d9SMahesh Salgaonkar {
9422df173d9SMahesh Salgaonkar 	struct fadump_crash_info_header *fdh;
943ebaeb5aeSMahesh Salgaonkar 	int rc = 0;
9442df173d9SMahesh Salgaonkar 
9452df173d9SMahesh Salgaonkar 	if (!fdm_active || !fw_dump.fadumphdr_addr)
9462df173d9SMahesh Salgaonkar 		return -EINVAL;
9472df173d9SMahesh Salgaonkar 
9482df173d9SMahesh Salgaonkar 	/* Check if the dump data is valid. */
949408cddd9SHari Bathini 	if ((be16_to_cpu(fdm_active->header.dump_status_flag) == FADUMP_ERROR_FLAG) ||
950ebaeb5aeSMahesh Salgaonkar 			(fdm_active->cpu_state_data.error_flags != 0) ||
9512df173d9SMahesh Salgaonkar 			(fdm_active->rmr_region.error_flags != 0)) {
9522df173d9SMahesh Salgaonkar 		printk(KERN_ERR "Dump taken by platform is not valid\n");
9532df173d9SMahesh Salgaonkar 		return -EINVAL;
9542df173d9SMahesh Salgaonkar 	}
955ebaeb5aeSMahesh Salgaonkar 	if ((fdm_active->rmr_region.bytes_dumped !=
956ebaeb5aeSMahesh Salgaonkar 			fdm_active->rmr_region.source_len) ||
957ebaeb5aeSMahesh Salgaonkar 			!fdm_active->cpu_state_data.bytes_dumped) {
9582df173d9SMahesh Salgaonkar 		printk(KERN_ERR "Dump taken by platform is incomplete\n");
9592df173d9SMahesh Salgaonkar 		return -EINVAL;
9602df173d9SMahesh Salgaonkar 	}
9612df173d9SMahesh Salgaonkar 
9622df173d9SMahesh Salgaonkar 	/* Validate the fadump crash info header */
9632df173d9SMahesh Salgaonkar 	fdh = __va(fw_dump.fadumphdr_addr);
9642df173d9SMahesh Salgaonkar 	if (fdh->magic_number != FADUMP_CRASH_INFO_MAGIC) {
9652df173d9SMahesh Salgaonkar 		printk(KERN_ERR "Crash info header is not valid.\n");
9662df173d9SMahesh Salgaonkar 		return -EINVAL;
9672df173d9SMahesh Salgaonkar 	}
9682df173d9SMahesh Salgaonkar 
969ebaeb5aeSMahesh Salgaonkar 	rc = fadump_build_cpu_notes(fdm_active);
970ebaeb5aeSMahesh Salgaonkar 	if (rc)
971ebaeb5aeSMahesh Salgaonkar 		return rc;
972ebaeb5aeSMahesh Salgaonkar 
9732df173d9SMahesh Salgaonkar 	/*
9742df173d9SMahesh Salgaonkar 	 * We are done validating dump info and elfcore header is now ready
9752df173d9SMahesh Salgaonkar 	 * to be exported. set elfcorehdr_addr so that vmcore module will
9762df173d9SMahesh Salgaonkar 	 * export the elfcore header through '/proc/vmcore'.
9772df173d9SMahesh Salgaonkar 	 */
9782df173d9SMahesh Salgaonkar 	elfcorehdr_addr = fdh->elfcorehdr_addr;
9792df173d9SMahesh Salgaonkar 
9802df173d9SMahesh Salgaonkar 	return 0;
9812df173d9SMahesh Salgaonkar }
9822df173d9SMahesh Salgaonkar 
9831bd6a1c4SHari Bathini static void free_crash_memory_ranges(void)
9841bd6a1c4SHari Bathini {
9851bd6a1c4SHari Bathini 	kfree(crash_memory_ranges);
9861bd6a1c4SHari Bathini 	crash_memory_ranges = NULL;
9871bd6a1c4SHari Bathini 	crash_memory_ranges_size = 0;
9881bd6a1c4SHari Bathini 	max_crash_mem_ranges = 0;
9891bd6a1c4SHari Bathini }
9901bd6a1c4SHari Bathini 
9911bd6a1c4SHari Bathini /*
9921bd6a1c4SHari Bathini  * Allocate or reallocate crash memory ranges array in incremental units
9931bd6a1c4SHari Bathini  * of PAGE_SIZE.
9941bd6a1c4SHari Bathini  */
9951bd6a1c4SHari Bathini static int allocate_crash_memory_ranges(void)
9961bd6a1c4SHari Bathini {
9971bd6a1c4SHari Bathini 	struct fad_crash_memory_ranges *new_array;
9981bd6a1c4SHari Bathini 	u64 new_size;
9991bd6a1c4SHari Bathini 
10001bd6a1c4SHari Bathini 	new_size = crash_memory_ranges_size + PAGE_SIZE;
10011bd6a1c4SHari Bathini 	pr_debug("Allocating %llu bytes of memory for crash memory ranges\n",
10021bd6a1c4SHari Bathini 		 new_size);
10031bd6a1c4SHari Bathini 
10041bd6a1c4SHari Bathini 	new_array = krealloc(crash_memory_ranges, new_size, GFP_KERNEL);
10051bd6a1c4SHari Bathini 	if (new_array == NULL) {
10061bd6a1c4SHari Bathini 		pr_err("Insufficient memory for setting up crash memory ranges\n");
10071bd6a1c4SHari Bathini 		free_crash_memory_ranges();
10081bd6a1c4SHari Bathini 		return -ENOMEM;
10091bd6a1c4SHari Bathini 	}
10101bd6a1c4SHari Bathini 
10111bd6a1c4SHari Bathini 	crash_memory_ranges = new_array;
10121bd6a1c4SHari Bathini 	crash_memory_ranges_size = new_size;
10131bd6a1c4SHari Bathini 	max_crash_mem_ranges = (new_size /
10141bd6a1c4SHari Bathini 				sizeof(struct fad_crash_memory_ranges));
10151bd6a1c4SHari Bathini 	return 0;
10161bd6a1c4SHari Bathini }
10171bd6a1c4SHari Bathini 
10181bd6a1c4SHari Bathini static inline int fadump_add_crash_memory(unsigned long long base,
10192df173d9SMahesh Salgaonkar 					  unsigned long long end)
10202df173d9SMahesh Salgaonkar {
1021ced1bf52SHari Bathini 	u64  start, size;
1022ced1bf52SHari Bathini 	bool is_adjacent = false;
1023ced1bf52SHari Bathini 
10242df173d9SMahesh Salgaonkar 	if (base == end)
10251bd6a1c4SHari Bathini 		return 0;
10261bd6a1c4SHari Bathini 
1027ced1bf52SHari Bathini 	/*
1028ced1bf52SHari Bathini 	 * Fold adjacent memory ranges to bring down the memory ranges/
1029ced1bf52SHari Bathini 	 * PT_LOAD segments count.
1030ced1bf52SHari Bathini 	 */
1031ced1bf52SHari Bathini 	if (crash_mem_ranges) {
1032ced1bf52SHari Bathini 		start = crash_memory_ranges[crash_mem_ranges - 1].base;
1033ced1bf52SHari Bathini 		size = crash_memory_ranges[crash_mem_ranges - 1].size;
1034ced1bf52SHari Bathini 
1035ced1bf52SHari Bathini 		if ((start + size) == base)
1036ced1bf52SHari Bathini 			is_adjacent = true;
1037ced1bf52SHari Bathini 	}
1038ced1bf52SHari Bathini 	if (!is_adjacent) {
1039ced1bf52SHari Bathini 		/* resize the array on reaching the limit */
10401bd6a1c4SHari Bathini 		if (crash_mem_ranges == max_crash_mem_ranges) {
10411bd6a1c4SHari Bathini 			int ret;
10421bd6a1c4SHari Bathini 
10431bd6a1c4SHari Bathini 			ret = allocate_crash_memory_ranges();
10441bd6a1c4SHari Bathini 			if (ret)
10451bd6a1c4SHari Bathini 				return ret;
10461bd6a1c4SHari Bathini 		}
10472df173d9SMahesh Salgaonkar 
1048ced1bf52SHari Bathini 		start = base;
1049ced1bf52SHari Bathini 		crash_memory_ranges[crash_mem_ranges].base = start;
10502df173d9SMahesh Salgaonkar 		crash_mem_ranges++;
1051ced1bf52SHari Bathini 	}
1052ced1bf52SHari Bathini 
1053ced1bf52SHari Bathini 	crash_memory_ranges[crash_mem_ranges - 1].size = (end - start);
1054ced1bf52SHari Bathini 	pr_debug("crash_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n",
1055ced1bf52SHari Bathini 		(crash_mem_ranges - 1), start, end - 1, (end - start));
10561bd6a1c4SHari Bathini 	return 0;
10572df173d9SMahesh Salgaonkar }
10582df173d9SMahesh Salgaonkar 
10591bd6a1c4SHari Bathini static int fadump_exclude_reserved_area(unsigned long long start,
10602df173d9SMahesh Salgaonkar 					unsigned long long end)
10612df173d9SMahesh Salgaonkar {
10622df173d9SMahesh Salgaonkar 	unsigned long long ra_start, ra_end;
10631bd6a1c4SHari Bathini 	int ret = 0;
10642df173d9SMahesh Salgaonkar 
10652df173d9SMahesh Salgaonkar 	ra_start = fw_dump.reserve_dump_area_start;
10662df173d9SMahesh Salgaonkar 	ra_end = ra_start + fw_dump.reserve_dump_area_size;
10672df173d9SMahesh Salgaonkar 
10682df173d9SMahesh Salgaonkar 	if ((ra_start < end) && (ra_end > start)) {
10692df173d9SMahesh Salgaonkar 		if ((start < ra_start) && (end > ra_end)) {
10701bd6a1c4SHari Bathini 			ret = fadump_add_crash_memory(start, ra_start);
10711bd6a1c4SHari Bathini 			if (ret)
10721bd6a1c4SHari Bathini 				return ret;
10731bd6a1c4SHari Bathini 
10741bd6a1c4SHari Bathini 			ret = fadump_add_crash_memory(ra_end, end);
10752df173d9SMahesh Salgaonkar 		} else if (start < ra_start) {
10761bd6a1c4SHari Bathini 			ret = fadump_add_crash_memory(start, ra_start);
10772df173d9SMahesh Salgaonkar 		} else if (ra_end < end) {
10781bd6a1c4SHari Bathini 			ret = fadump_add_crash_memory(ra_end, end);
10792df173d9SMahesh Salgaonkar 		}
10802df173d9SMahesh Salgaonkar 	} else
10811bd6a1c4SHari Bathini 		ret = fadump_add_crash_memory(start, end);
10821bd6a1c4SHari Bathini 
10831bd6a1c4SHari Bathini 	return ret;
10842df173d9SMahesh Salgaonkar }
10852df173d9SMahesh Salgaonkar 
10862df173d9SMahesh Salgaonkar static int fadump_init_elfcore_header(char *bufp)
10872df173d9SMahesh Salgaonkar {
10882df173d9SMahesh Salgaonkar 	struct elfhdr *elf;
10892df173d9SMahesh Salgaonkar 
10902df173d9SMahesh Salgaonkar 	elf = (struct elfhdr *) bufp;
10912df173d9SMahesh Salgaonkar 	bufp += sizeof(struct elfhdr);
10922df173d9SMahesh Salgaonkar 	memcpy(elf->e_ident, ELFMAG, SELFMAG);
10932df173d9SMahesh Salgaonkar 	elf->e_ident[EI_CLASS] = ELF_CLASS;
10942df173d9SMahesh Salgaonkar 	elf->e_ident[EI_DATA] = ELF_DATA;
10952df173d9SMahesh Salgaonkar 	elf->e_ident[EI_VERSION] = EV_CURRENT;
10962df173d9SMahesh Salgaonkar 	elf->e_ident[EI_OSABI] = ELF_OSABI;
10972df173d9SMahesh Salgaonkar 	memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
10982df173d9SMahesh Salgaonkar 	elf->e_type = ET_CORE;
10992df173d9SMahesh Salgaonkar 	elf->e_machine = ELF_ARCH;
11002df173d9SMahesh Salgaonkar 	elf->e_version = EV_CURRENT;
11012df173d9SMahesh Salgaonkar 	elf->e_entry = 0;
11022df173d9SMahesh Salgaonkar 	elf->e_phoff = sizeof(struct elfhdr);
11032df173d9SMahesh Salgaonkar 	elf->e_shoff = 0;
1104d8bced27SDaniel Axtens #if defined(_CALL_ELF)
1105d8bced27SDaniel Axtens 	elf->e_flags = _CALL_ELF;
1106d8bced27SDaniel Axtens #else
1107d8bced27SDaniel Axtens 	elf->e_flags = 0;
1108d8bced27SDaniel Axtens #endif
11092df173d9SMahesh Salgaonkar 	elf->e_ehsize = sizeof(struct elfhdr);
11102df173d9SMahesh Salgaonkar 	elf->e_phentsize = sizeof(struct elf_phdr);
11112df173d9SMahesh Salgaonkar 	elf->e_phnum = 0;
11122df173d9SMahesh Salgaonkar 	elf->e_shentsize = 0;
11132df173d9SMahesh Salgaonkar 	elf->e_shnum = 0;
11142df173d9SMahesh Salgaonkar 	elf->e_shstrndx = 0;
11152df173d9SMahesh Salgaonkar 
11162df173d9SMahesh Salgaonkar 	return 0;
11172df173d9SMahesh Salgaonkar }
11182df173d9SMahesh Salgaonkar 
11192df173d9SMahesh Salgaonkar /*
11202df173d9SMahesh Salgaonkar  * Traverse through memblock structure and setup crash memory ranges. These
11212df173d9SMahesh Salgaonkar  * ranges will be used create PT_LOAD program headers in elfcore header.
11222df173d9SMahesh Salgaonkar  */
11231bd6a1c4SHari Bathini static int fadump_setup_crash_memory_ranges(void)
11242df173d9SMahesh Salgaonkar {
11252df173d9SMahesh Salgaonkar 	struct memblock_region *reg;
11262df173d9SMahesh Salgaonkar 	unsigned long long start, end;
11271bd6a1c4SHari Bathini 	int ret;
11282df173d9SMahesh Salgaonkar 
11292df173d9SMahesh Salgaonkar 	pr_debug("Setup crash memory ranges.\n");
11302df173d9SMahesh Salgaonkar 	crash_mem_ranges = 0;
1131ced1bf52SHari Bathini 
11322df173d9SMahesh Salgaonkar 	/*
11332df173d9SMahesh Salgaonkar 	 * add the first memory chunk (RMA_START through boot_memory_size) as
11342df173d9SMahesh Salgaonkar 	 * a separate memory chunk. The reason is, at the time crash firmware
11352df173d9SMahesh Salgaonkar 	 * will move the content of this memory chunk to different location
11362df173d9SMahesh Salgaonkar 	 * specified during fadump registration. We need to create a separate
11372df173d9SMahesh Salgaonkar 	 * program header for this chunk with the correct offset.
11382df173d9SMahesh Salgaonkar 	 */
11391bd6a1c4SHari Bathini 	ret = fadump_add_crash_memory(RMA_START, fw_dump.boot_memory_size);
11401bd6a1c4SHari Bathini 	if (ret)
11411bd6a1c4SHari Bathini 		return ret;
11422df173d9SMahesh Salgaonkar 
11432df173d9SMahesh Salgaonkar 	for_each_memblock(memory, reg) {
11442df173d9SMahesh Salgaonkar 		start = (unsigned long long)reg->base;
11452df173d9SMahesh Salgaonkar 		end = start + (unsigned long long)reg->size;
1146a77af552SHari Bathini 
1147a77af552SHari Bathini 		/*
1148a77af552SHari Bathini 		 * skip the first memory chunk that is already added (RMA_START
1149a77af552SHari Bathini 		 * through boot_memory_size). This logic needs a relook if and
1150a77af552SHari Bathini 		 * when RMA_START changes to a non-zero value.
1151a77af552SHari Bathini 		 */
1152a77af552SHari Bathini 		BUILD_BUG_ON(RMA_START != 0);
1153a77af552SHari Bathini 		if (start < fw_dump.boot_memory_size) {
1154a77af552SHari Bathini 			if (end > fw_dump.boot_memory_size)
11552df173d9SMahesh Salgaonkar 				start = fw_dump.boot_memory_size;
1156a77af552SHari Bathini 			else
1157a77af552SHari Bathini 				continue;
1158a77af552SHari Bathini 		}
11592df173d9SMahesh Salgaonkar 
11602df173d9SMahesh Salgaonkar 		/* add this range excluding the reserved dump area. */
11611bd6a1c4SHari Bathini 		ret = fadump_exclude_reserved_area(start, end);
11621bd6a1c4SHari Bathini 		if (ret)
11631bd6a1c4SHari Bathini 			return ret;
11642df173d9SMahesh Salgaonkar 	}
11651bd6a1c4SHari Bathini 
11661bd6a1c4SHari Bathini 	return 0;
11672df173d9SMahesh Salgaonkar }
11682df173d9SMahesh Salgaonkar 
1169d34c5f26SMahesh Salgaonkar /*
1170d34c5f26SMahesh Salgaonkar  * If the given physical address falls within the boot memory region then
1171d34c5f26SMahesh Salgaonkar  * return the relocated address that points to the dump region reserved
1172d34c5f26SMahesh Salgaonkar  * for saving initial boot memory contents.
1173d34c5f26SMahesh Salgaonkar  */
1174d34c5f26SMahesh Salgaonkar static inline unsigned long fadump_relocate(unsigned long paddr)
1175d34c5f26SMahesh Salgaonkar {
1176d34c5f26SMahesh Salgaonkar 	if (paddr > RMA_START && paddr < fw_dump.boot_memory_size)
1177408cddd9SHari Bathini 		return be64_to_cpu(fdm.rmr_region.destination_address) + paddr;
1178d34c5f26SMahesh Salgaonkar 	else
1179d34c5f26SMahesh Salgaonkar 		return paddr;
1180d34c5f26SMahesh Salgaonkar }
1181d34c5f26SMahesh Salgaonkar 
11822df173d9SMahesh Salgaonkar static int fadump_create_elfcore_headers(char *bufp)
11832df173d9SMahesh Salgaonkar {
11842df173d9SMahesh Salgaonkar 	struct elfhdr *elf;
11852df173d9SMahesh Salgaonkar 	struct elf_phdr *phdr;
11862df173d9SMahesh Salgaonkar 	int i;
11872df173d9SMahesh Salgaonkar 
11882df173d9SMahesh Salgaonkar 	fadump_init_elfcore_header(bufp);
11892df173d9SMahesh Salgaonkar 	elf = (struct elfhdr *)bufp;
11902df173d9SMahesh Salgaonkar 	bufp += sizeof(struct elfhdr);
11912df173d9SMahesh Salgaonkar 
1192ebaeb5aeSMahesh Salgaonkar 	/*
1193ebaeb5aeSMahesh Salgaonkar 	 * setup ELF PT_NOTE, place holder for cpu notes info. The notes info
1194ebaeb5aeSMahesh Salgaonkar 	 * will be populated during second kernel boot after crash. Hence
1195ebaeb5aeSMahesh Salgaonkar 	 * this PT_NOTE will always be the first elf note.
1196ebaeb5aeSMahesh Salgaonkar 	 *
1197ebaeb5aeSMahesh Salgaonkar 	 * NOTE: Any new ELF note addition should be placed after this note.
1198ebaeb5aeSMahesh Salgaonkar 	 */
1199ebaeb5aeSMahesh Salgaonkar 	phdr = (struct elf_phdr *)bufp;
1200ebaeb5aeSMahesh Salgaonkar 	bufp += sizeof(struct elf_phdr);
1201ebaeb5aeSMahesh Salgaonkar 	phdr->p_type = PT_NOTE;
1202ebaeb5aeSMahesh Salgaonkar 	phdr->p_flags = 0;
1203ebaeb5aeSMahesh Salgaonkar 	phdr->p_vaddr = 0;
1204ebaeb5aeSMahesh Salgaonkar 	phdr->p_align = 0;
1205ebaeb5aeSMahesh Salgaonkar 
1206ebaeb5aeSMahesh Salgaonkar 	phdr->p_offset = 0;
1207ebaeb5aeSMahesh Salgaonkar 	phdr->p_paddr = 0;
1208ebaeb5aeSMahesh Salgaonkar 	phdr->p_filesz = 0;
1209ebaeb5aeSMahesh Salgaonkar 	phdr->p_memsz = 0;
1210ebaeb5aeSMahesh Salgaonkar 
1211ebaeb5aeSMahesh Salgaonkar 	(elf->e_phnum)++;
1212ebaeb5aeSMahesh Salgaonkar 
1213d34c5f26SMahesh Salgaonkar 	/* setup ELF PT_NOTE for vmcoreinfo */
1214d34c5f26SMahesh Salgaonkar 	phdr = (struct elf_phdr *)bufp;
1215d34c5f26SMahesh Salgaonkar 	bufp += sizeof(struct elf_phdr);
1216d34c5f26SMahesh Salgaonkar 	phdr->p_type	= PT_NOTE;
1217d34c5f26SMahesh Salgaonkar 	phdr->p_flags	= 0;
1218d34c5f26SMahesh Salgaonkar 	phdr->p_vaddr	= 0;
1219d34c5f26SMahesh Salgaonkar 	phdr->p_align	= 0;
1220d34c5f26SMahesh Salgaonkar 
1221d34c5f26SMahesh Salgaonkar 	phdr->p_paddr	= fadump_relocate(paddr_vmcoreinfo_note());
1222d34c5f26SMahesh Salgaonkar 	phdr->p_offset	= phdr->p_paddr;
12235203f499SXunlei Pang 	phdr->p_memsz	= phdr->p_filesz = VMCOREINFO_NOTE_SIZE;
1224d34c5f26SMahesh Salgaonkar 
1225d34c5f26SMahesh Salgaonkar 	/* Increment number of program headers. */
1226d34c5f26SMahesh Salgaonkar 	(elf->e_phnum)++;
1227d34c5f26SMahesh Salgaonkar 
12282df173d9SMahesh Salgaonkar 	/* setup PT_LOAD sections. */
12292df173d9SMahesh Salgaonkar 
12302df173d9SMahesh Salgaonkar 	for (i = 0; i < crash_mem_ranges; i++) {
12312df173d9SMahesh Salgaonkar 		unsigned long long mbase, msize;
12322df173d9SMahesh Salgaonkar 		mbase = crash_memory_ranges[i].base;
12332df173d9SMahesh Salgaonkar 		msize = crash_memory_ranges[i].size;
12342df173d9SMahesh Salgaonkar 
12352df173d9SMahesh Salgaonkar 		if (!msize)
12362df173d9SMahesh Salgaonkar 			continue;
12372df173d9SMahesh Salgaonkar 
12382df173d9SMahesh Salgaonkar 		phdr = (struct elf_phdr *)bufp;
12392df173d9SMahesh Salgaonkar 		bufp += sizeof(struct elf_phdr);
12402df173d9SMahesh Salgaonkar 		phdr->p_type	= PT_LOAD;
12412df173d9SMahesh Salgaonkar 		phdr->p_flags	= PF_R|PF_W|PF_X;
12422df173d9SMahesh Salgaonkar 		phdr->p_offset	= mbase;
12432df173d9SMahesh Salgaonkar 
12442df173d9SMahesh Salgaonkar 		if (mbase == RMA_START) {
12452df173d9SMahesh Salgaonkar 			/*
12462df173d9SMahesh Salgaonkar 			 * The entire RMA region will be moved by firmware
12472df173d9SMahesh Salgaonkar 			 * to the specified destination_address. Hence set
12482df173d9SMahesh Salgaonkar 			 * the correct offset.
12492df173d9SMahesh Salgaonkar 			 */
1250408cddd9SHari Bathini 			phdr->p_offset = be64_to_cpu(fdm.rmr_region.destination_address);
12512df173d9SMahesh Salgaonkar 		}
12522df173d9SMahesh Salgaonkar 
12532df173d9SMahesh Salgaonkar 		phdr->p_paddr = mbase;
12542df173d9SMahesh Salgaonkar 		phdr->p_vaddr = (unsigned long)__va(mbase);
12552df173d9SMahesh Salgaonkar 		phdr->p_filesz = msize;
12562df173d9SMahesh Salgaonkar 		phdr->p_memsz = msize;
12572df173d9SMahesh Salgaonkar 		phdr->p_align = 0;
12582df173d9SMahesh Salgaonkar 
12592df173d9SMahesh Salgaonkar 		/* Increment number of program headers. */
12602df173d9SMahesh Salgaonkar 		(elf->e_phnum)++;
12612df173d9SMahesh Salgaonkar 	}
12622df173d9SMahesh Salgaonkar 	return 0;
12632df173d9SMahesh Salgaonkar }
12642df173d9SMahesh Salgaonkar 
12652df173d9SMahesh Salgaonkar static unsigned long init_fadump_header(unsigned long addr)
12662df173d9SMahesh Salgaonkar {
12672df173d9SMahesh Salgaonkar 	struct fadump_crash_info_header *fdh;
12682df173d9SMahesh Salgaonkar 
12692df173d9SMahesh Salgaonkar 	if (!addr)
12702df173d9SMahesh Salgaonkar 		return 0;
12712df173d9SMahesh Salgaonkar 
12722df173d9SMahesh Salgaonkar 	fw_dump.fadumphdr_addr = addr;
12732df173d9SMahesh Salgaonkar 	fdh = __va(addr);
12742df173d9SMahesh Salgaonkar 	addr += sizeof(struct fadump_crash_info_header);
12752df173d9SMahesh Salgaonkar 
12762df173d9SMahesh Salgaonkar 	memset(fdh, 0, sizeof(struct fadump_crash_info_header));
12772df173d9SMahesh Salgaonkar 	fdh->magic_number = FADUMP_CRASH_INFO_MAGIC;
12782df173d9SMahesh Salgaonkar 	fdh->elfcorehdr_addr = addr;
1279ebaeb5aeSMahesh Salgaonkar 	/* We will set the crashing cpu id in crash_fadump() during crash. */
1280ebaeb5aeSMahesh Salgaonkar 	fdh->crashing_cpu = CPU_UNKNOWN;
12812df173d9SMahesh Salgaonkar 
12822df173d9SMahesh Salgaonkar 	return addr;
12832df173d9SMahesh Salgaonkar }
12842df173d9SMahesh Salgaonkar 
128598b8cd7fSMichal Suchanek static int register_fadump(void)
12863ccc00a7SMahesh Salgaonkar {
12872df173d9SMahesh Salgaonkar 	unsigned long addr;
12882df173d9SMahesh Salgaonkar 	void *vaddr;
12891bd6a1c4SHari Bathini 	int ret;
12902df173d9SMahesh Salgaonkar 
12913ccc00a7SMahesh Salgaonkar 	/*
12923ccc00a7SMahesh Salgaonkar 	 * If no memory is reserved then we can not register for firmware-
12933ccc00a7SMahesh Salgaonkar 	 * assisted dump.
12943ccc00a7SMahesh Salgaonkar 	 */
12953ccc00a7SMahesh Salgaonkar 	if (!fw_dump.reserve_dump_area_size)
129698b8cd7fSMichal Suchanek 		return -ENODEV;
12973ccc00a7SMahesh Salgaonkar 
12981bd6a1c4SHari Bathini 	ret = fadump_setup_crash_memory_ranges();
12991bd6a1c4SHari Bathini 	if (ret)
13001bd6a1c4SHari Bathini 		return ret;
13012df173d9SMahesh Salgaonkar 
1302408cddd9SHari Bathini 	addr = be64_to_cpu(fdm.rmr_region.destination_address) + be64_to_cpu(fdm.rmr_region.source_len);
13032df173d9SMahesh Salgaonkar 	/* Initialize fadump crash info header. */
13042df173d9SMahesh Salgaonkar 	addr = init_fadump_header(addr);
13052df173d9SMahesh Salgaonkar 	vaddr = __va(addr);
13062df173d9SMahesh Salgaonkar 
13072df173d9SMahesh Salgaonkar 	pr_debug("Creating ELF core headers at %#016lx\n", addr);
13082df173d9SMahesh Salgaonkar 	fadump_create_elfcore_headers(vaddr);
13092df173d9SMahesh Salgaonkar 
13103ccc00a7SMahesh Salgaonkar 	/* register the future kernel dump with firmware. */
131198b8cd7fSMichal Suchanek 	return register_fw_dump(&fdm);
13123ccc00a7SMahesh Salgaonkar }
13133ccc00a7SMahesh Salgaonkar 
13143ccc00a7SMahesh Salgaonkar static int fadump_unregister_dump(struct fadump_mem_struct *fdm)
13153ccc00a7SMahesh Salgaonkar {
13163ccc00a7SMahesh Salgaonkar 	int rc = 0;
13173ccc00a7SMahesh Salgaonkar 	unsigned int wait_time;
13183ccc00a7SMahesh Salgaonkar 
13193ccc00a7SMahesh Salgaonkar 	pr_debug("Un-register firmware-assisted dump\n");
13203ccc00a7SMahesh Salgaonkar 
13213ccc00a7SMahesh Salgaonkar 	/* TODO: Add upper time limit for the delay */
13223ccc00a7SMahesh Salgaonkar 	do {
13233ccc00a7SMahesh Salgaonkar 		rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
13243ccc00a7SMahesh Salgaonkar 			FADUMP_UNREGISTER, fdm,
13253ccc00a7SMahesh Salgaonkar 			sizeof(struct fadump_mem_struct));
13263ccc00a7SMahesh Salgaonkar 
13273ccc00a7SMahesh Salgaonkar 		wait_time = rtas_busy_delay_time(rc);
13283ccc00a7SMahesh Salgaonkar 		if (wait_time)
13293ccc00a7SMahesh Salgaonkar 			mdelay(wait_time);
13303ccc00a7SMahesh Salgaonkar 	} while (wait_time);
13313ccc00a7SMahesh Salgaonkar 
13323ccc00a7SMahesh Salgaonkar 	if (rc) {
13333ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "Failed to un-register firmware-assisted dump."
13343ccc00a7SMahesh Salgaonkar 			" unexpected error(%d).\n", rc);
13353ccc00a7SMahesh Salgaonkar 		return rc;
13363ccc00a7SMahesh Salgaonkar 	}
13373ccc00a7SMahesh Salgaonkar 	fw_dump.dump_registered = 0;
13383ccc00a7SMahesh Salgaonkar 	return 0;
13393ccc00a7SMahesh Salgaonkar }
13403ccc00a7SMahesh Salgaonkar 
1341a4e92ce8SMahesh Salgaonkar static int fadump_invalidate_dump(const struct fadump_mem_struct *fdm)
1342b500afffSMahesh Salgaonkar {
1343b500afffSMahesh Salgaonkar 	int rc = 0;
1344b500afffSMahesh Salgaonkar 	unsigned int wait_time;
1345b500afffSMahesh Salgaonkar 
1346b500afffSMahesh Salgaonkar 	pr_debug("Invalidating firmware-assisted dump registration\n");
1347b500afffSMahesh Salgaonkar 
1348b500afffSMahesh Salgaonkar 	/* TODO: Add upper time limit for the delay */
1349b500afffSMahesh Salgaonkar 	do {
1350b500afffSMahesh Salgaonkar 		rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
1351b500afffSMahesh Salgaonkar 			FADUMP_INVALIDATE, fdm,
1352b500afffSMahesh Salgaonkar 			sizeof(struct fadump_mem_struct));
1353b500afffSMahesh Salgaonkar 
1354b500afffSMahesh Salgaonkar 		wait_time = rtas_busy_delay_time(rc);
1355b500afffSMahesh Salgaonkar 		if (wait_time)
1356b500afffSMahesh Salgaonkar 			mdelay(wait_time);
1357b500afffSMahesh Salgaonkar 	} while (wait_time);
1358b500afffSMahesh Salgaonkar 
1359b500afffSMahesh Salgaonkar 	if (rc) {
13604a03749fSColin Ian King 		pr_err("Failed to invalidate firmware-assisted dump registration. Unexpected error (%d).\n", rc);
1361b5b1cfc5SMichael Ellerman 		return rc;
1362b500afffSMahesh Salgaonkar 	}
1363b500afffSMahesh Salgaonkar 	fw_dump.dump_active = 0;
1364b500afffSMahesh Salgaonkar 	fdm_active = NULL;
1365b500afffSMahesh Salgaonkar 	return 0;
1366b500afffSMahesh Salgaonkar }
1367b500afffSMahesh Salgaonkar 
1368b500afffSMahesh Salgaonkar void fadump_cleanup(void)
1369b500afffSMahesh Salgaonkar {
1370b500afffSMahesh Salgaonkar 	/* Invalidate the registration only if dump is active. */
1371b500afffSMahesh Salgaonkar 	if (fw_dump.dump_active) {
1372a4e92ce8SMahesh Salgaonkar 		/* pass the same memory dump structure provided by platform */
1373a4e92ce8SMahesh Salgaonkar 		fadump_invalidate_dump(fdm_active);
1374722cde76SMahesh Salgaonkar 	} else if (fw_dump.dump_registered) {
1375722cde76SMahesh Salgaonkar 		/* Un-register Firmware-assisted dump if it was registered. */
1376722cde76SMahesh Salgaonkar 		fadump_unregister_dump(&fdm);
13771bd6a1c4SHari Bathini 		free_crash_memory_ranges();
1378b500afffSMahesh Salgaonkar 	}
1379b500afffSMahesh Salgaonkar }
1380b500afffSMahesh Salgaonkar 
138168fa6478SHari Bathini static void fadump_free_reserved_memory(unsigned long start_pfn,
138268fa6478SHari Bathini 					unsigned long end_pfn)
138368fa6478SHari Bathini {
138468fa6478SHari Bathini 	unsigned long pfn;
138568fa6478SHari Bathini 	unsigned long time_limit = jiffies + HZ;
138668fa6478SHari Bathini 
138768fa6478SHari Bathini 	pr_info("freeing reserved memory (0x%llx - 0x%llx)\n",
138868fa6478SHari Bathini 		PFN_PHYS(start_pfn), PFN_PHYS(end_pfn));
138968fa6478SHari Bathini 
139068fa6478SHari Bathini 	for (pfn = start_pfn; pfn < end_pfn; pfn++) {
139168fa6478SHari Bathini 		free_reserved_page(pfn_to_page(pfn));
139268fa6478SHari Bathini 
139368fa6478SHari Bathini 		if (time_after(jiffies, time_limit)) {
139468fa6478SHari Bathini 			cond_resched();
139568fa6478SHari Bathini 			time_limit = jiffies + HZ;
139668fa6478SHari Bathini 		}
139768fa6478SHari Bathini 	}
139868fa6478SHari Bathini }
139968fa6478SHari Bathini 
140068fa6478SHari Bathini /*
140168fa6478SHari Bathini  * Skip memory holes and free memory that was actually reserved.
140268fa6478SHari Bathini  */
140368fa6478SHari Bathini static void fadump_release_reserved_area(unsigned long start, unsigned long end)
140468fa6478SHari Bathini {
140568fa6478SHari Bathini 	struct memblock_region *reg;
140668fa6478SHari Bathini 	unsigned long tstart, tend;
140768fa6478SHari Bathini 	unsigned long start_pfn = PHYS_PFN(start);
140868fa6478SHari Bathini 	unsigned long end_pfn = PHYS_PFN(end);
140968fa6478SHari Bathini 
141068fa6478SHari Bathini 	for_each_memblock(memory, reg) {
141168fa6478SHari Bathini 		tstart = max(start_pfn, memblock_region_memory_base_pfn(reg));
141268fa6478SHari Bathini 		tend = min(end_pfn, memblock_region_memory_end_pfn(reg));
141368fa6478SHari Bathini 		if (tstart < tend) {
141468fa6478SHari Bathini 			fadump_free_reserved_memory(tstart, tend);
141568fa6478SHari Bathini 
141668fa6478SHari Bathini 			if (tend == end_pfn)
141768fa6478SHari Bathini 				break;
141868fa6478SHari Bathini 
141968fa6478SHari Bathini 			start_pfn = tend + 1;
142068fa6478SHari Bathini 		}
142168fa6478SHari Bathini 	}
142268fa6478SHari Bathini }
142368fa6478SHari Bathini 
1424b500afffSMahesh Salgaonkar /*
1425b500afffSMahesh Salgaonkar  * Release the memory that was reserved in early boot to preserve the memory
1426b500afffSMahesh Salgaonkar  * contents. The released memory will be available for general use.
1427b500afffSMahesh Salgaonkar  */
1428b500afffSMahesh Salgaonkar static void fadump_release_memory(unsigned long begin, unsigned long end)
1429b500afffSMahesh Salgaonkar {
1430b500afffSMahesh Salgaonkar 	unsigned long ra_start, ra_end;
1431b500afffSMahesh Salgaonkar 
1432b500afffSMahesh Salgaonkar 	ra_start = fw_dump.reserve_dump_area_start;
1433b500afffSMahesh Salgaonkar 	ra_end = ra_start + fw_dump.reserve_dump_area_size;
1434b500afffSMahesh Salgaonkar 
1435b500afffSMahesh Salgaonkar 	/*
1436b500afffSMahesh Salgaonkar 	 * exclude the dump reserve area. Will reuse it for next
1437b500afffSMahesh Salgaonkar 	 * fadump registration.
1438b500afffSMahesh Salgaonkar 	 */
143968fa6478SHari Bathini 	if (begin < ra_end && end > ra_start) {
144068fa6478SHari Bathini 		if (begin < ra_start)
144168fa6478SHari Bathini 			fadump_release_reserved_area(begin, ra_start);
144268fa6478SHari Bathini 		if (end > ra_end)
144368fa6478SHari Bathini 			fadump_release_reserved_area(ra_end, end);
144468fa6478SHari Bathini 	} else
144568fa6478SHari Bathini 		fadump_release_reserved_area(begin, end);
1446b500afffSMahesh Salgaonkar }
1447b500afffSMahesh Salgaonkar 
1448b500afffSMahesh Salgaonkar static void fadump_invalidate_release_mem(void)
1449b500afffSMahesh Salgaonkar {
1450b500afffSMahesh Salgaonkar 	unsigned long reserved_area_start, reserved_area_end;
1451b500afffSMahesh Salgaonkar 	unsigned long destination_address;
1452b500afffSMahesh Salgaonkar 
1453b500afffSMahesh Salgaonkar 	mutex_lock(&fadump_mutex);
1454b500afffSMahesh Salgaonkar 	if (!fw_dump.dump_active) {
1455b500afffSMahesh Salgaonkar 		mutex_unlock(&fadump_mutex);
1456b500afffSMahesh Salgaonkar 		return;
1457b500afffSMahesh Salgaonkar 	}
1458b500afffSMahesh Salgaonkar 
1459408cddd9SHari Bathini 	destination_address = be64_to_cpu(fdm_active->cpu_state_data.destination_address);
1460b500afffSMahesh Salgaonkar 	fadump_cleanup();
1461b500afffSMahesh Salgaonkar 	mutex_unlock(&fadump_mutex);
1462b500afffSMahesh Salgaonkar 
1463b500afffSMahesh Salgaonkar 	/*
1464b500afffSMahesh Salgaonkar 	 * Save the current reserved memory bounds we will require them
1465b500afffSMahesh Salgaonkar 	 * later for releasing the memory for general use.
1466b500afffSMahesh Salgaonkar 	 */
1467b500afffSMahesh Salgaonkar 	reserved_area_start = fw_dump.reserve_dump_area_start;
1468b500afffSMahesh Salgaonkar 	reserved_area_end = reserved_area_start +
1469b500afffSMahesh Salgaonkar 			fw_dump.reserve_dump_area_size;
1470b500afffSMahesh Salgaonkar 	/*
1471b500afffSMahesh Salgaonkar 	 * Setup reserve_dump_area_start and its size so that we can
1472b500afffSMahesh Salgaonkar 	 * reuse this reserved memory for Re-registration.
1473b500afffSMahesh Salgaonkar 	 */
1474b500afffSMahesh Salgaonkar 	fw_dump.reserve_dump_area_start = destination_address;
1475b500afffSMahesh Salgaonkar 	fw_dump.reserve_dump_area_size = get_fadump_area_size();
1476b500afffSMahesh Salgaonkar 
1477b500afffSMahesh Salgaonkar 	fadump_release_memory(reserved_area_start, reserved_area_end);
1478b500afffSMahesh Salgaonkar 	if (fw_dump.cpu_notes_buf) {
1479b500afffSMahesh Salgaonkar 		fadump_cpu_notes_buf_free(
1480b500afffSMahesh Salgaonkar 				(unsigned long)__va(fw_dump.cpu_notes_buf),
1481b500afffSMahesh Salgaonkar 				fw_dump.cpu_notes_buf_size);
1482b500afffSMahesh Salgaonkar 		fw_dump.cpu_notes_buf = 0;
1483b500afffSMahesh Salgaonkar 		fw_dump.cpu_notes_buf_size = 0;
1484b500afffSMahesh Salgaonkar 	}
1485b500afffSMahesh Salgaonkar 	/* Initialize the kernel dump memory structure for FAD registration. */
1486b500afffSMahesh Salgaonkar 	init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
1487b500afffSMahesh Salgaonkar }
1488b500afffSMahesh Salgaonkar 
1489b500afffSMahesh Salgaonkar static ssize_t fadump_release_memory_store(struct kobject *kobj,
1490b500afffSMahesh Salgaonkar 					struct kobj_attribute *attr,
1491b500afffSMahesh Salgaonkar 					const char *buf, size_t count)
1492b500afffSMahesh Salgaonkar {
1493dcdc4679SMichal Suchanek 	int input = -1;
1494dcdc4679SMichal Suchanek 
1495b500afffSMahesh Salgaonkar 	if (!fw_dump.dump_active)
1496b500afffSMahesh Salgaonkar 		return -EPERM;
1497b500afffSMahesh Salgaonkar 
1498dcdc4679SMichal Suchanek 	if (kstrtoint(buf, 0, &input))
1499dcdc4679SMichal Suchanek 		return -EINVAL;
1500dcdc4679SMichal Suchanek 
1501dcdc4679SMichal Suchanek 	if (input == 1) {
1502b500afffSMahesh Salgaonkar 		/*
1503b500afffSMahesh Salgaonkar 		 * Take away the '/proc/vmcore'. We are releasing the dump
1504b500afffSMahesh Salgaonkar 		 * memory, hence it will not be valid anymore.
1505b500afffSMahesh Salgaonkar 		 */
15062685f826SMichael Ellerman #ifdef CONFIG_PROC_VMCORE
1507b500afffSMahesh Salgaonkar 		vmcore_cleanup();
15082685f826SMichael Ellerman #endif
1509b500afffSMahesh Salgaonkar 		fadump_invalidate_release_mem();
1510b500afffSMahesh Salgaonkar 
1511b500afffSMahesh Salgaonkar 	} else
1512b500afffSMahesh Salgaonkar 		return -EINVAL;
1513b500afffSMahesh Salgaonkar 	return count;
1514b500afffSMahesh Salgaonkar }
1515b500afffSMahesh Salgaonkar 
15163ccc00a7SMahesh Salgaonkar static ssize_t fadump_enabled_show(struct kobject *kobj,
15173ccc00a7SMahesh Salgaonkar 					struct kobj_attribute *attr,
15183ccc00a7SMahesh Salgaonkar 					char *buf)
15193ccc00a7SMahesh Salgaonkar {
15203ccc00a7SMahesh Salgaonkar 	return sprintf(buf, "%d\n", fw_dump.fadump_enabled);
15213ccc00a7SMahesh Salgaonkar }
15223ccc00a7SMahesh Salgaonkar 
15233ccc00a7SMahesh Salgaonkar static ssize_t fadump_register_show(struct kobject *kobj,
15243ccc00a7SMahesh Salgaonkar 					struct kobj_attribute *attr,
15253ccc00a7SMahesh Salgaonkar 					char *buf)
15263ccc00a7SMahesh Salgaonkar {
15273ccc00a7SMahesh Salgaonkar 	return sprintf(buf, "%d\n", fw_dump.dump_registered);
15283ccc00a7SMahesh Salgaonkar }
15293ccc00a7SMahesh Salgaonkar 
15303ccc00a7SMahesh Salgaonkar static ssize_t fadump_register_store(struct kobject *kobj,
15313ccc00a7SMahesh Salgaonkar 					struct kobj_attribute *attr,
15323ccc00a7SMahesh Salgaonkar 					const char *buf, size_t count)
15333ccc00a7SMahesh Salgaonkar {
15343ccc00a7SMahesh Salgaonkar 	int ret = 0;
1535dcdc4679SMichal Suchanek 	int input = -1;
15363ccc00a7SMahesh Salgaonkar 
15373ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_enabled || fdm_active)
15383ccc00a7SMahesh Salgaonkar 		return -EPERM;
15393ccc00a7SMahesh Salgaonkar 
1540dcdc4679SMichal Suchanek 	if (kstrtoint(buf, 0, &input))
1541dcdc4679SMichal Suchanek 		return -EINVAL;
1542dcdc4679SMichal Suchanek 
15433ccc00a7SMahesh Salgaonkar 	mutex_lock(&fadump_mutex);
15443ccc00a7SMahesh Salgaonkar 
1545dcdc4679SMichal Suchanek 	switch (input) {
1546dcdc4679SMichal Suchanek 	case 0:
15473ccc00a7SMahesh Salgaonkar 		if (fw_dump.dump_registered == 0) {
15483ccc00a7SMahesh Salgaonkar 			goto unlock_out;
15493ccc00a7SMahesh Salgaonkar 		}
15503ccc00a7SMahesh Salgaonkar 		/* Un-register Firmware-assisted dump */
15513ccc00a7SMahesh Salgaonkar 		fadump_unregister_dump(&fdm);
15523ccc00a7SMahesh Salgaonkar 		break;
1553dcdc4679SMichal Suchanek 	case 1:
15543ccc00a7SMahesh Salgaonkar 		if (fw_dump.dump_registered == 1) {
15550823c68bSHari Bathini 			/* Un-register Firmware-assisted dump */
15560823c68bSHari Bathini 			fadump_unregister_dump(&fdm);
15573ccc00a7SMahesh Salgaonkar 		}
15583ccc00a7SMahesh Salgaonkar 		/* Register Firmware-assisted dump */
155998b8cd7fSMichal Suchanek 		ret = register_fadump();
15603ccc00a7SMahesh Salgaonkar 		break;
15613ccc00a7SMahesh Salgaonkar 	default:
15623ccc00a7SMahesh Salgaonkar 		ret = -EINVAL;
15633ccc00a7SMahesh Salgaonkar 		break;
15643ccc00a7SMahesh Salgaonkar 	}
15653ccc00a7SMahesh Salgaonkar 
15663ccc00a7SMahesh Salgaonkar unlock_out:
15673ccc00a7SMahesh Salgaonkar 	mutex_unlock(&fadump_mutex);
15683ccc00a7SMahesh Salgaonkar 	return ret < 0 ? ret : count;
15693ccc00a7SMahesh Salgaonkar }
15703ccc00a7SMahesh Salgaonkar 
15713ccc00a7SMahesh Salgaonkar static int fadump_region_show(struct seq_file *m, void *private)
15723ccc00a7SMahesh Salgaonkar {
15733ccc00a7SMahesh Salgaonkar 	const struct fadump_mem_struct *fdm_ptr;
15743ccc00a7SMahesh Salgaonkar 
15753ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_enabled)
15763ccc00a7SMahesh Salgaonkar 		return 0;
15773ccc00a7SMahesh Salgaonkar 
1578b500afffSMahesh Salgaonkar 	mutex_lock(&fadump_mutex);
15793ccc00a7SMahesh Salgaonkar 	if (fdm_active)
15803ccc00a7SMahesh Salgaonkar 		fdm_ptr = fdm_active;
1581b500afffSMahesh Salgaonkar 	else {
1582b500afffSMahesh Salgaonkar 		mutex_unlock(&fadump_mutex);
15833ccc00a7SMahesh Salgaonkar 		fdm_ptr = &fdm;
1584b500afffSMahesh Salgaonkar 	}
15853ccc00a7SMahesh Salgaonkar 
15863ccc00a7SMahesh Salgaonkar 	seq_printf(m,
15873ccc00a7SMahesh Salgaonkar 			"CPU : [%#016llx-%#016llx] %#llx bytes, "
15883ccc00a7SMahesh Salgaonkar 			"Dumped: %#llx\n",
1589408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->cpu_state_data.destination_address),
1590408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) +
1591408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->cpu_state_data.source_len) - 1,
1592408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->cpu_state_data.source_len),
1593408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->cpu_state_data.bytes_dumped));
15943ccc00a7SMahesh Salgaonkar 	seq_printf(m,
15953ccc00a7SMahesh Salgaonkar 			"HPTE: [%#016llx-%#016llx] %#llx bytes, "
15963ccc00a7SMahesh Salgaonkar 			"Dumped: %#llx\n",
1597408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->hpte_region.destination_address),
1598408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->hpte_region.destination_address) +
1599408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->hpte_region.source_len) - 1,
1600408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->hpte_region.source_len),
1601408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->hpte_region.bytes_dumped));
16023ccc00a7SMahesh Salgaonkar 	seq_printf(m,
16033ccc00a7SMahesh Salgaonkar 			"DUMP: [%#016llx-%#016llx] %#llx bytes, "
16043ccc00a7SMahesh Salgaonkar 			"Dumped: %#llx\n",
1605408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->rmr_region.destination_address),
1606408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->rmr_region.destination_address) +
1607408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->rmr_region.source_len) - 1,
1608408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->rmr_region.source_len),
1609408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->rmr_region.bytes_dumped));
16103ccc00a7SMahesh Salgaonkar 
16113ccc00a7SMahesh Salgaonkar 	if (!fdm_active ||
16123ccc00a7SMahesh Salgaonkar 		(fw_dump.reserve_dump_area_start ==
1613408cddd9SHari Bathini 		be64_to_cpu(fdm_ptr->cpu_state_data.destination_address)))
1614b500afffSMahesh Salgaonkar 		goto out;
16153ccc00a7SMahesh Salgaonkar 
16163ccc00a7SMahesh Salgaonkar 	/* Dump is active. Show reserved memory region. */
16173ccc00a7SMahesh Salgaonkar 	seq_printf(m,
16183ccc00a7SMahesh Salgaonkar 			"    : [%#016llx-%#016llx] %#llx bytes, "
16193ccc00a7SMahesh Salgaonkar 			"Dumped: %#llx\n",
16203ccc00a7SMahesh Salgaonkar 			(unsigned long long)fw_dump.reserve_dump_area_start,
1621408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) - 1,
1622408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) -
16233ccc00a7SMahesh Salgaonkar 			fw_dump.reserve_dump_area_start,
1624408cddd9SHari Bathini 			be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) -
16253ccc00a7SMahesh Salgaonkar 			fw_dump.reserve_dump_area_start);
1626b500afffSMahesh Salgaonkar out:
1627b500afffSMahesh Salgaonkar 	if (fdm_active)
1628b500afffSMahesh Salgaonkar 		mutex_unlock(&fadump_mutex);
16293ccc00a7SMahesh Salgaonkar 	return 0;
16303ccc00a7SMahesh Salgaonkar }
16313ccc00a7SMahesh Salgaonkar 
1632b500afffSMahesh Salgaonkar static struct kobj_attribute fadump_release_attr = __ATTR(fadump_release_mem,
1633b500afffSMahesh Salgaonkar 						0200, NULL,
1634b500afffSMahesh Salgaonkar 						fadump_release_memory_store);
16353ccc00a7SMahesh Salgaonkar static struct kobj_attribute fadump_attr = __ATTR(fadump_enabled,
16363ccc00a7SMahesh Salgaonkar 						0444, fadump_enabled_show,
16373ccc00a7SMahesh Salgaonkar 						NULL);
16383ccc00a7SMahesh Salgaonkar static struct kobj_attribute fadump_register_attr = __ATTR(fadump_registered,
16393ccc00a7SMahesh Salgaonkar 						0644, fadump_register_show,
16403ccc00a7SMahesh Salgaonkar 						fadump_register_store);
16413ccc00a7SMahesh Salgaonkar 
1642f6cee260SYangtao Li DEFINE_SHOW_ATTRIBUTE(fadump_region);
16433ccc00a7SMahesh Salgaonkar 
16443ccc00a7SMahesh Salgaonkar static void fadump_init_files(void)
16453ccc00a7SMahesh Salgaonkar {
16463ccc00a7SMahesh Salgaonkar 	struct dentry *debugfs_file;
16473ccc00a7SMahesh Salgaonkar 	int rc = 0;
16483ccc00a7SMahesh Salgaonkar 
16493ccc00a7SMahesh Salgaonkar 	rc = sysfs_create_file(kernel_kobj, &fadump_attr.attr);
16503ccc00a7SMahesh Salgaonkar 	if (rc)
16513ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "fadump: unable to create sysfs file"
16523ccc00a7SMahesh Salgaonkar 			" fadump_enabled (%d)\n", rc);
16533ccc00a7SMahesh Salgaonkar 
16543ccc00a7SMahesh Salgaonkar 	rc = sysfs_create_file(kernel_kobj, &fadump_register_attr.attr);
16553ccc00a7SMahesh Salgaonkar 	if (rc)
16563ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "fadump: unable to create sysfs file"
16573ccc00a7SMahesh Salgaonkar 			" fadump_registered (%d)\n", rc);
16583ccc00a7SMahesh Salgaonkar 
16593ccc00a7SMahesh Salgaonkar 	debugfs_file = debugfs_create_file("fadump_region", 0444,
16603ccc00a7SMahesh Salgaonkar 					powerpc_debugfs_root, NULL,
16613ccc00a7SMahesh Salgaonkar 					&fadump_region_fops);
16623ccc00a7SMahesh Salgaonkar 	if (!debugfs_file)
16633ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "fadump: unable to create debugfs file"
16643ccc00a7SMahesh Salgaonkar 				" fadump_region\n");
1665b500afffSMahesh Salgaonkar 
1666b500afffSMahesh Salgaonkar 	if (fw_dump.dump_active) {
1667b500afffSMahesh Salgaonkar 		rc = sysfs_create_file(kernel_kobj, &fadump_release_attr.attr);
1668b500afffSMahesh Salgaonkar 		if (rc)
1669b500afffSMahesh Salgaonkar 			printk(KERN_ERR "fadump: unable to create sysfs file"
1670b500afffSMahesh Salgaonkar 				" fadump_release_mem (%d)\n", rc);
1671b500afffSMahesh Salgaonkar 	}
16723ccc00a7SMahesh Salgaonkar 	return;
16733ccc00a7SMahesh Salgaonkar }
16743ccc00a7SMahesh Salgaonkar 
16753ccc00a7SMahesh Salgaonkar /*
16763ccc00a7SMahesh Salgaonkar  * Prepare for firmware-assisted dump.
16773ccc00a7SMahesh Salgaonkar  */
16783ccc00a7SMahesh Salgaonkar int __init setup_fadump(void)
16793ccc00a7SMahesh Salgaonkar {
16803ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_enabled)
16813ccc00a7SMahesh Salgaonkar 		return 0;
16823ccc00a7SMahesh Salgaonkar 
16833ccc00a7SMahesh Salgaonkar 	if (!fw_dump.fadump_supported) {
16843ccc00a7SMahesh Salgaonkar 		printk(KERN_ERR "Firmware-assisted dump is not supported on"
16853ccc00a7SMahesh Salgaonkar 			" this hardware\n");
16863ccc00a7SMahesh Salgaonkar 		return 0;
16873ccc00a7SMahesh Salgaonkar 	}
16883ccc00a7SMahesh Salgaonkar 
16893ccc00a7SMahesh Salgaonkar 	fadump_show_config();
16902df173d9SMahesh Salgaonkar 	/*
16912df173d9SMahesh Salgaonkar 	 * If dump data is available then see if it is valid and prepare for
16922df173d9SMahesh Salgaonkar 	 * saving it to the disk.
16932df173d9SMahesh Salgaonkar 	 */
1694b500afffSMahesh Salgaonkar 	if (fw_dump.dump_active) {
1695b500afffSMahesh Salgaonkar 		/*
1696b500afffSMahesh Salgaonkar 		 * if dump process fails then invalidate the registration
1697b500afffSMahesh Salgaonkar 		 * and release memory before proceeding for re-registration.
1698b500afffSMahesh Salgaonkar 		 */
1699b500afffSMahesh Salgaonkar 		if (process_fadump(fdm_active) < 0)
1700b500afffSMahesh Salgaonkar 			fadump_invalidate_release_mem();
1701b500afffSMahesh Salgaonkar 	}
17023ccc00a7SMahesh Salgaonkar 	/* Initialize the kernel dump memory structure for FAD registration. */
17032df173d9SMahesh Salgaonkar 	else if (fw_dump.reserve_dump_area_size)
17043ccc00a7SMahesh Salgaonkar 		init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
17053ccc00a7SMahesh Salgaonkar 	fadump_init_files();
17063ccc00a7SMahesh Salgaonkar 
17073ccc00a7SMahesh Salgaonkar 	return 1;
17083ccc00a7SMahesh Salgaonkar }
17093ccc00a7SMahesh Salgaonkar subsys_initcall(setup_fadump);
1710