xref: /openbmc/linux/arch/powerpc/kernel/fadump.c (revision d236d361)
1 /*
2  * Firmware Assisted dump: A robust mechanism to get reliable kernel crash
3  * dump with assistance from firmware. This approach does not use kexec,
4  * instead firmware assists in booting the kdump kernel while preserving
5  * memory contents. The most of the code implementation has been adapted
6  * from phyp assisted dump implementation written by Linas Vepstas and
7  * Manish Ahuja
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * Copyright 2011 IBM Corporation
24  * Author: Mahesh Salgaonkar <mahesh@linux.vnet.ibm.com>
25  */
26 
27 #undef DEBUG
28 #define pr_fmt(fmt) "fadump: " fmt
29 
30 #include <linux/string.h>
31 #include <linux/memblock.h>
32 #include <linux/delay.h>
33 #include <linux/seq_file.h>
34 #include <linux/crash_dump.h>
35 #include <linux/kobject.h>
36 #include <linux/sysfs.h>
37 
38 #include <asm/debugfs.h>
39 #include <asm/page.h>
40 #include <asm/prom.h>
41 #include <asm/rtas.h>
42 #include <asm/fadump.h>
43 #include <asm/setup.h>
44 
45 static struct fw_dump fw_dump;
46 static struct fadump_mem_struct fdm;
47 static const struct fadump_mem_struct *fdm_active;
48 
49 static DEFINE_MUTEX(fadump_mutex);
50 struct fad_crash_memory_ranges crash_memory_ranges[INIT_CRASHMEM_RANGES];
51 int crash_mem_ranges;
52 
53 /* Scan the Firmware Assisted dump configuration details. */
54 int __init early_init_dt_scan_fw_dump(unsigned long node,
55 			const char *uname, int depth, void *data)
56 {
57 	const __be32 *sections;
58 	int i, num_sections;
59 	int size;
60 	const __be32 *token;
61 
62 	if (depth != 1 || strcmp(uname, "rtas") != 0)
63 		return 0;
64 
65 	/*
66 	 * Check if Firmware Assisted dump is supported. if yes, check
67 	 * if dump has been initiated on last reboot.
68 	 */
69 	token = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump", NULL);
70 	if (!token)
71 		return 1;
72 
73 	fw_dump.fadump_supported = 1;
74 	fw_dump.ibm_configure_kernel_dump = be32_to_cpu(*token);
75 
76 	/*
77 	 * The 'ibm,kernel-dump' rtas node is present only if there is
78 	 * dump data waiting for us.
79 	 */
80 	fdm_active = of_get_flat_dt_prop(node, "ibm,kernel-dump", NULL);
81 	if (fdm_active)
82 		fw_dump.dump_active = 1;
83 
84 	/* Get the sizes required to store dump data for the firmware provided
85 	 * dump sections.
86 	 * For each dump section type supported, a 32bit cell which defines
87 	 * the ID of a supported section followed by two 32 bit cells which
88 	 * gives teh size of the section in bytes.
89 	 */
90 	sections = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump-sizes",
91 					&size);
92 
93 	if (!sections)
94 		return 1;
95 
96 	num_sections = size / (3 * sizeof(u32));
97 
98 	for (i = 0; i < num_sections; i++, sections += 3) {
99 		u32 type = (u32)of_read_number(sections, 1);
100 
101 		switch (type) {
102 		case FADUMP_CPU_STATE_DATA:
103 			fw_dump.cpu_state_data_size =
104 					of_read_ulong(&sections[1], 2);
105 			break;
106 		case FADUMP_HPTE_REGION:
107 			fw_dump.hpte_region_size =
108 					of_read_ulong(&sections[1], 2);
109 			break;
110 		}
111 	}
112 
113 	return 1;
114 }
115 
116 int is_fadump_active(void)
117 {
118 	return fw_dump.dump_active;
119 }
120 
121 /* Print firmware assisted dump configurations for debugging purpose. */
122 static void fadump_show_config(void)
123 {
124 	pr_debug("Support for firmware-assisted dump (fadump): %s\n",
125 			(fw_dump.fadump_supported ? "present" : "no support"));
126 
127 	if (!fw_dump.fadump_supported)
128 		return;
129 
130 	pr_debug("Fadump enabled    : %s\n",
131 				(fw_dump.fadump_enabled ? "yes" : "no"));
132 	pr_debug("Dump Active       : %s\n",
133 				(fw_dump.dump_active ? "yes" : "no"));
134 	pr_debug("Dump section sizes:\n");
135 	pr_debug("    CPU state data size: %lx\n", fw_dump.cpu_state_data_size);
136 	pr_debug("    HPTE region size   : %lx\n", fw_dump.hpte_region_size);
137 	pr_debug("Boot memory size  : %lx\n", fw_dump.boot_memory_size);
138 }
139 
140 static unsigned long init_fadump_mem_struct(struct fadump_mem_struct *fdm,
141 				unsigned long addr)
142 {
143 	if (!fdm)
144 		return 0;
145 
146 	memset(fdm, 0, sizeof(struct fadump_mem_struct));
147 	addr = addr & PAGE_MASK;
148 
149 	fdm->header.dump_format_version = cpu_to_be32(0x00000001);
150 	fdm->header.dump_num_sections = cpu_to_be16(3);
151 	fdm->header.dump_status_flag = 0;
152 	fdm->header.offset_first_dump_section =
153 		cpu_to_be32((u32)offsetof(struct fadump_mem_struct, cpu_state_data));
154 
155 	/*
156 	 * Fields for disk dump option.
157 	 * We are not using disk dump option, hence set these fields to 0.
158 	 */
159 	fdm->header.dd_block_size = 0;
160 	fdm->header.dd_block_offset = 0;
161 	fdm->header.dd_num_blocks = 0;
162 	fdm->header.dd_offset_disk_path = 0;
163 
164 	/* set 0 to disable an automatic dump-reboot. */
165 	fdm->header.max_time_auto = 0;
166 
167 	/* Kernel dump sections */
168 	/* cpu state data section. */
169 	fdm->cpu_state_data.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG);
170 	fdm->cpu_state_data.source_data_type = cpu_to_be16(FADUMP_CPU_STATE_DATA);
171 	fdm->cpu_state_data.source_address = 0;
172 	fdm->cpu_state_data.source_len = cpu_to_be64(fw_dump.cpu_state_data_size);
173 	fdm->cpu_state_data.destination_address = cpu_to_be64(addr);
174 	addr += fw_dump.cpu_state_data_size;
175 
176 	/* hpte region section */
177 	fdm->hpte_region.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG);
178 	fdm->hpte_region.source_data_type = cpu_to_be16(FADUMP_HPTE_REGION);
179 	fdm->hpte_region.source_address = 0;
180 	fdm->hpte_region.source_len = cpu_to_be64(fw_dump.hpte_region_size);
181 	fdm->hpte_region.destination_address = cpu_to_be64(addr);
182 	addr += fw_dump.hpte_region_size;
183 
184 	/* RMA region section */
185 	fdm->rmr_region.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG);
186 	fdm->rmr_region.source_data_type = cpu_to_be16(FADUMP_REAL_MODE_REGION);
187 	fdm->rmr_region.source_address = cpu_to_be64(RMA_START);
188 	fdm->rmr_region.source_len = cpu_to_be64(fw_dump.boot_memory_size);
189 	fdm->rmr_region.destination_address = cpu_to_be64(addr);
190 	addr += fw_dump.boot_memory_size;
191 
192 	return addr;
193 }
194 
195 /**
196  * fadump_calculate_reserve_size(): reserve variable boot area 5% of System RAM
197  *
198  * Function to find the largest memory size we need to reserve during early
199  * boot process. This will be the size of the memory that is required for a
200  * kernel to boot successfully.
201  *
202  * This function has been taken from phyp-assisted dump feature implementation.
203  *
204  * returns larger of 256MB or 5% rounded down to multiples of 256MB.
205  *
206  * TODO: Come up with better approach to find out more accurate memory size
207  * that is required for a kernel to boot successfully.
208  *
209  */
210 static inline unsigned long fadump_calculate_reserve_size(void)
211 {
212 	int ret;
213 	unsigned long long base, size;
214 
215 	/*
216 	 * Check if the size is specified through crashkernel= cmdline
217 	 * option. If yes, then use that but ignore base as fadump
218 	 * reserves memory at end of RAM.
219 	 */
220 	ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
221 				&size, &base);
222 	if (ret == 0 && size > 0) {
223 		fw_dump.reserve_bootvar = (unsigned long)size;
224 		return fw_dump.reserve_bootvar;
225 	}
226 
227 	/* divide by 20 to get 5% of value */
228 	size = memblock_end_of_DRAM() / 20;
229 
230 	/* round it down in multiples of 256 */
231 	size = size & ~0x0FFFFFFFUL;
232 
233 	/* Truncate to memory_limit. We don't want to over reserve the memory.*/
234 	if (memory_limit && size > memory_limit)
235 		size = memory_limit;
236 
237 	return (size > MIN_BOOT_MEM ? size : MIN_BOOT_MEM);
238 }
239 
240 /*
241  * Calculate the total memory size required to be reserved for
242  * firmware-assisted dump registration.
243  */
244 static unsigned long get_fadump_area_size(void)
245 {
246 	unsigned long size = 0;
247 
248 	size += fw_dump.cpu_state_data_size;
249 	size += fw_dump.hpte_region_size;
250 	size += fw_dump.boot_memory_size;
251 	size += sizeof(struct fadump_crash_info_header);
252 	size += sizeof(struct elfhdr); /* ELF core header.*/
253 	size += sizeof(struct elf_phdr); /* place holder for cpu notes */
254 	/* Program headers for crash memory regions. */
255 	size += sizeof(struct elf_phdr) * (memblock_num_regions(memory) + 2);
256 
257 	size = PAGE_ALIGN(size);
258 	return size;
259 }
260 
261 int __init fadump_reserve_mem(void)
262 {
263 	unsigned long base, size, memory_boundary;
264 
265 	if (!fw_dump.fadump_enabled)
266 		return 0;
267 
268 	if (!fw_dump.fadump_supported) {
269 		printk(KERN_INFO "Firmware-assisted dump is not supported on"
270 				" this hardware\n");
271 		fw_dump.fadump_enabled = 0;
272 		return 0;
273 	}
274 	/*
275 	 * Initialize boot memory size
276 	 * If dump is active then we have already calculated the size during
277 	 * first kernel.
278 	 */
279 	if (fdm_active)
280 		fw_dump.boot_memory_size = be64_to_cpu(fdm_active->rmr_region.source_len);
281 	else
282 		fw_dump.boot_memory_size = fadump_calculate_reserve_size();
283 
284 	/*
285 	 * Calculate the memory boundary.
286 	 * If memory_limit is less than actual memory boundary then reserve
287 	 * the memory for fadump beyond the memory_limit and adjust the
288 	 * memory_limit accordingly, so that the running kernel can run with
289 	 * specified memory_limit.
290 	 */
291 	if (memory_limit && memory_limit < memblock_end_of_DRAM()) {
292 		size = get_fadump_area_size();
293 		if ((memory_limit + size) < memblock_end_of_DRAM())
294 			memory_limit += size;
295 		else
296 			memory_limit = memblock_end_of_DRAM();
297 		printk(KERN_INFO "Adjusted memory_limit for firmware-assisted"
298 				" dump, now %#016llx\n", memory_limit);
299 	}
300 	if (memory_limit)
301 		memory_boundary = memory_limit;
302 	else
303 		memory_boundary = memblock_end_of_DRAM();
304 
305 	if (fw_dump.dump_active) {
306 		printk(KERN_INFO "Firmware-assisted dump is active.\n");
307 		/*
308 		 * If last boot has crashed then reserve all the memory
309 		 * above boot_memory_size so that we don't touch it until
310 		 * dump is written to disk by userspace tool. This memory
311 		 * will be released for general use once the dump is saved.
312 		 */
313 		base = fw_dump.boot_memory_size;
314 		size = memory_boundary - base;
315 		memblock_reserve(base, size);
316 		printk(KERN_INFO "Reserved %ldMB of memory at %ldMB "
317 				"for saving crash dump\n",
318 				(unsigned long)(size >> 20),
319 				(unsigned long)(base >> 20));
320 
321 		fw_dump.fadumphdr_addr =
322 				be64_to_cpu(fdm_active->rmr_region.destination_address) +
323 				be64_to_cpu(fdm_active->rmr_region.source_len);
324 		pr_debug("fadumphdr_addr = %p\n",
325 				(void *) fw_dump.fadumphdr_addr);
326 	} else {
327 		size = get_fadump_area_size();
328 
329 		/*
330 		 * Reserve memory at an offset closer to bottom of the RAM to
331 		 * minimize the impact of memory hot-remove operation. We can't
332 		 * use memblock_find_in_range() here since it doesn't allocate
333 		 * from bottom to top.
334 		 */
335 		for (base = fw_dump.boot_memory_size;
336 		     base <= (memory_boundary - size);
337 		     base += size) {
338 			if (memblock_is_region_memory(base, size) &&
339 			    !memblock_is_region_reserved(base, size))
340 				break;
341 		}
342 		if ((base > (memory_boundary - size)) ||
343 		    memblock_reserve(base, size)) {
344 			pr_err("Failed to reserve memory\n");
345 			return 0;
346 		}
347 
348 		pr_info("Reserved %ldMB of memory at %ldMB for firmware-"
349 			"assisted dump (System RAM: %ldMB)\n",
350 			(unsigned long)(size >> 20),
351 			(unsigned long)(base >> 20),
352 			(unsigned long)(memblock_phys_mem_size() >> 20));
353 	}
354 
355 	fw_dump.reserve_dump_area_start = base;
356 	fw_dump.reserve_dump_area_size = size;
357 	return 1;
358 }
359 
360 unsigned long __init arch_reserved_kernel_pages(void)
361 {
362 	return memblock_reserved_size() / PAGE_SIZE;
363 }
364 
365 /* Look for fadump= cmdline option. */
366 static int __init early_fadump_param(char *p)
367 {
368 	if (!p)
369 		return 1;
370 
371 	if (strncmp(p, "on", 2) == 0)
372 		fw_dump.fadump_enabled = 1;
373 	else if (strncmp(p, "off", 3) == 0)
374 		fw_dump.fadump_enabled = 0;
375 
376 	return 0;
377 }
378 early_param("fadump", early_fadump_param);
379 
380 static void register_fw_dump(struct fadump_mem_struct *fdm)
381 {
382 	int rc;
383 	unsigned int wait_time;
384 
385 	pr_debug("Registering for firmware-assisted kernel dump...\n");
386 
387 	/* TODO: Add upper time limit for the delay */
388 	do {
389 		rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
390 			FADUMP_REGISTER, fdm,
391 			sizeof(struct fadump_mem_struct));
392 
393 		wait_time = rtas_busy_delay_time(rc);
394 		if (wait_time)
395 			mdelay(wait_time);
396 
397 	} while (wait_time);
398 
399 	switch (rc) {
400 	case -1:
401 		printk(KERN_ERR "Failed to register firmware-assisted kernel"
402 			" dump. Hardware Error(%d).\n", rc);
403 		break;
404 	case -3:
405 		printk(KERN_ERR "Failed to register firmware-assisted kernel"
406 			" dump. Parameter Error(%d).\n", rc);
407 		break;
408 	case -9:
409 		printk(KERN_ERR "firmware-assisted kernel dump is already "
410 			" registered.");
411 		fw_dump.dump_registered = 1;
412 		break;
413 	case 0:
414 		printk(KERN_INFO "firmware-assisted kernel dump registration"
415 			" is successful\n");
416 		fw_dump.dump_registered = 1;
417 		break;
418 	}
419 }
420 
421 void crash_fadump(struct pt_regs *regs, const char *str)
422 {
423 	struct fadump_crash_info_header *fdh = NULL;
424 	int old_cpu, this_cpu;
425 
426 	if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr)
427 		return;
428 
429 	/*
430 	 * old_cpu == -1 means this is the first CPU which has come here,
431 	 * go ahead and trigger fadump.
432 	 *
433 	 * old_cpu != -1 means some other CPU has already on it's way
434 	 * to trigger fadump, just keep looping here.
435 	 */
436 	this_cpu = smp_processor_id();
437 	old_cpu = cmpxchg(&crashing_cpu, -1, this_cpu);
438 
439 	if (old_cpu != -1) {
440 		/*
441 		 * We can't loop here indefinitely. Wait as long as fadump
442 		 * is in force. If we race with fadump un-registration this
443 		 * loop will break and then we go down to normal panic path
444 		 * and reboot. If fadump is in force the first crashing
445 		 * cpu will definitely trigger fadump.
446 		 */
447 		while (fw_dump.dump_registered)
448 			cpu_relax();
449 		return;
450 	}
451 
452 	fdh = __va(fw_dump.fadumphdr_addr);
453 	fdh->crashing_cpu = crashing_cpu;
454 	crash_save_vmcoreinfo();
455 
456 	if (regs)
457 		fdh->regs = *regs;
458 	else
459 		ppc_save_regs(&fdh->regs);
460 
461 	fdh->online_mask = *cpu_online_mask;
462 
463 	/* Call ibm,os-term rtas call to trigger firmware assisted dump */
464 	rtas_os_term((char *)str);
465 }
466 
467 #define GPR_MASK	0xffffff0000000000
468 static inline int fadump_gpr_index(u64 id)
469 {
470 	int i = -1;
471 	char str[3];
472 
473 	if ((id & GPR_MASK) == REG_ID("GPR")) {
474 		/* get the digits at the end */
475 		id &= ~GPR_MASK;
476 		id >>= 24;
477 		str[2] = '\0';
478 		str[1] = id & 0xff;
479 		str[0] = (id >> 8) & 0xff;
480 		sscanf(str, "%d", &i);
481 		if (i > 31)
482 			i = -1;
483 	}
484 	return i;
485 }
486 
487 static inline void fadump_set_regval(struct pt_regs *regs, u64 reg_id,
488 								u64 reg_val)
489 {
490 	int i;
491 
492 	i = fadump_gpr_index(reg_id);
493 	if (i >= 0)
494 		regs->gpr[i] = (unsigned long)reg_val;
495 	else if (reg_id == REG_ID("NIA"))
496 		regs->nip = (unsigned long)reg_val;
497 	else if (reg_id == REG_ID("MSR"))
498 		regs->msr = (unsigned long)reg_val;
499 	else if (reg_id == REG_ID("CTR"))
500 		regs->ctr = (unsigned long)reg_val;
501 	else if (reg_id == REG_ID("LR"))
502 		regs->link = (unsigned long)reg_val;
503 	else if (reg_id == REG_ID("XER"))
504 		regs->xer = (unsigned long)reg_val;
505 	else if (reg_id == REG_ID("CR"))
506 		regs->ccr = (unsigned long)reg_val;
507 	else if (reg_id == REG_ID("DAR"))
508 		regs->dar = (unsigned long)reg_val;
509 	else if (reg_id == REG_ID("DSISR"))
510 		regs->dsisr = (unsigned long)reg_val;
511 }
512 
513 static struct fadump_reg_entry*
514 fadump_read_registers(struct fadump_reg_entry *reg_entry, struct pt_regs *regs)
515 {
516 	memset(regs, 0, sizeof(struct pt_regs));
517 
518 	while (be64_to_cpu(reg_entry->reg_id) != REG_ID("CPUEND")) {
519 		fadump_set_regval(regs, be64_to_cpu(reg_entry->reg_id),
520 					be64_to_cpu(reg_entry->reg_value));
521 		reg_entry++;
522 	}
523 	reg_entry++;
524 	return reg_entry;
525 }
526 
527 static u32 *fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs)
528 {
529 	struct elf_prstatus prstatus;
530 
531 	memset(&prstatus, 0, sizeof(prstatus));
532 	/*
533 	 * FIXME: How do i get PID? Do I really need it?
534 	 * prstatus.pr_pid = ????
535 	 */
536 	elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
537 	buf = append_elf_note(buf, CRASH_CORE_NOTE_NAME, NT_PRSTATUS,
538 			      &prstatus, sizeof(prstatus));
539 	return buf;
540 }
541 
542 static void fadump_update_elfcore_header(char *bufp)
543 {
544 	struct elfhdr *elf;
545 	struct elf_phdr *phdr;
546 
547 	elf = (struct elfhdr *)bufp;
548 	bufp += sizeof(struct elfhdr);
549 
550 	/* First note is a place holder for cpu notes info. */
551 	phdr = (struct elf_phdr *)bufp;
552 
553 	if (phdr->p_type == PT_NOTE) {
554 		phdr->p_paddr = fw_dump.cpu_notes_buf;
555 		phdr->p_offset	= phdr->p_paddr;
556 		phdr->p_filesz	= fw_dump.cpu_notes_buf_size;
557 		phdr->p_memsz = fw_dump.cpu_notes_buf_size;
558 	}
559 	return;
560 }
561 
562 static void *fadump_cpu_notes_buf_alloc(unsigned long size)
563 {
564 	void *vaddr;
565 	struct page *page;
566 	unsigned long order, count, i;
567 
568 	order = get_order(size);
569 	vaddr = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
570 	if (!vaddr)
571 		return NULL;
572 
573 	count = 1 << order;
574 	page = virt_to_page(vaddr);
575 	for (i = 0; i < count; i++)
576 		SetPageReserved(page + i);
577 	return vaddr;
578 }
579 
580 static void fadump_cpu_notes_buf_free(unsigned long vaddr, unsigned long size)
581 {
582 	struct page *page;
583 	unsigned long order, count, i;
584 
585 	order = get_order(size);
586 	count = 1 << order;
587 	page = virt_to_page(vaddr);
588 	for (i = 0; i < count; i++)
589 		ClearPageReserved(page + i);
590 	__free_pages(page, order);
591 }
592 
593 /*
594  * Read CPU state dump data and convert it into ELF notes.
595  * The CPU dump starts with magic number "REGSAVE". NumCpusOffset should be
596  * used to access the data to allow for additional fields to be added without
597  * affecting compatibility. Each list of registers for a CPU starts with
598  * "CPUSTRT" and ends with "CPUEND". Each register entry is of 16 bytes,
599  * 8 Byte ASCII identifier and 8 Byte register value. The register entry
600  * with identifier "CPUSTRT" and "CPUEND" contains 4 byte cpu id as part
601  * of register value. For more details refer to PAPR document.
602  *
603  * Only for the crashing cpu we ignore the CPU dump data and get exact
604  * state from fadump crash info structure populated by first kernel at the
605  * time of crash.
606  */
607 static int __init fadump_build_cpu_notes(const struct fadump_mem_struct *fdm)
608 {
609 	struct fadump_reg_save_area_header *reg_header;
610 	struct fadump_reg_entry *reg_entry;
611 	struct fadump_crash_info_header *fdh = NULL;
612 	void *vaddr;
613 	unsigned long addr;
614 	u32 num_cpus, *note_buf;
615 	struct pt_regs regs;
616 	int i, rc = 0, cpu = 0;
617 
618 	if (!fdm->cpu_state_data.bytes_dumped)
619 		return -EINVAL;
620 
621 	addr = be64_to_cpu(fdm->cpu_state_data.destination_address);
622 	vaddr = __va(addr);
623 
624 	reg_header = vaddr;
625 	if (be64_to_cpu(reg_header->magic_number) != REGSAVE_AREA_MAGIC) {
626 		printk(KERN_ERR "Unable to read register save area.\n");
627 		return -ENOENT;
628 	}
629 	pr_debug("--------CPU State Data------------\n");
630 	pr_debug("Magic Number: %llx\n", be64_to_cpu(reg_header->magic_number));
631 	pr_debug("NumCpuOffset: %x\n", be32_to_cpu(reg_header->num_cpu_offset));
632 
633 	vaddr += be32_to_cpu(reg_header->num_cpu_offset);
634 	num_cpus = be32_to_cpu(*((__be32 *)(vaddr)));
635 	pr_debug("NumCpus     : %u\n", num_cpus);
636 	vaddr += sizeof(u32);
637 	reg_entry = (struct fadump_reg_entry *)vaddr;
638 
639 	/* Allocate buffer to hold cpu crash notes. */
640 	fw_dump.cpu_notes_buf_size = num_cpus * sizeof(note_buf_t);
641 	fw_dump.cpu_notes_buf_size = PAGE_ALIGN(fw_dump.cpu_notes_buf_size);
642 	note_buf = fadump_cpu_notes_buf_alloc(fw_dump.cpu_notes_buf_size);
643 	if (!note_buf) {
644 		printk(KERN_ERR "Failed to allocate 0x%lx bytes for "
645 			"cpu notes buffer\n", fw_dump.cpu_notes_buf_size);
646 		return -ENOMEM;
647 	}
648 	fw_dump.cpu_notes_buf = __pa(note_buf);
649 
650 	pr_debug("Allocated buffer for cpu notes of size %ld at %p\n",
651 			(num_cpus * sizeof(note_buf_t)), note_buf);
652 
653 	if (fw_dump.fadumphdr_addr)
654 		fdh = __va(fw_dump.fadumphdr_addr);
655 
656 	for (i = 0; i < num_cpus; i++) {
657 		if (be64_to_cpu(reg_entry->reg_id) != REG_ID("CPUSTRT")) {
658 			printk(KERN_ERR "Unable to read CPU state data\n");
659 			rc = -ENOENT;
660 			goto error_out;
661 		}
662 		/* Lower 4 bytes of reg_value contains logical cpu id */
663 		cpu = be64_to_cpu(reg_entry->reg_value) & FADUMP_CPU_ID_MASK;
664 		if (fdh && !cpumask_test_cpu(cpu, &fdh->online_mask)) {
665 			SKIP_TO_NEXT_CPU(reg_entry);
666 			continue;
667 		}
668 		pr_debug("Reading register data for cpu %d...\n", cpu);
669 		if (fdh && fdh->crashing_cpu == cpu) {
670 			regs = fdh->regs;
671 			note_buf = fadump_regs_to_elf_notes(note_buf, &regs);
672 			SKIP_TO_NEXT_CPU(reg_entry);
673 		} else {
674 			reg_entry++;
675 			reg_entry = fadump_read_registers(reg_entry, &regs);
676 			note_buf = fadump_regs_to_elf_notes(note_buf, &regs);
677 		}
678 	}
679 	final_note(note_buf);
680 
681 	if (fdh) {
682 		pr_debug("Updating elfcore header (%llx) with cpu notes\n",
683 							fdh->elfcorehdr_addr);
684 		fadump_update_elfcore_header((char *)__va(fdh->elfcorehdr_addr));
685 	}
686 	return 0;
687 
688 error_out:
689 	fadump_cpu_notes_buf_free((unsigned long)__va(fw_dump.cpu_notes_buf),
690 					fw_dump.cpu_notes_buf_size);
691 	fw_dump.cpu_notes_buf = 0;
692 	fw_dump.cpu_notes_buf_size = 0;
693 	return rc;
694 
695 }
696 
697 /*
698  * Validate and process the dump data stored by firmware before exporting
699  * it through '/proc/vmcore'.
700  */
701 static int __init process_fadump(const struct fadump_mem_struct *fdm_active)
702 {
703 	struct fadump_crash_info_header *fdh;
704 	int rc = 0;
705 
706 	if (!fdm_active || !fw_dump.fadumphdr_addr)
707 		return -EINVAL;
708 
709 	/* Check if the dump data is valid. */
710 	if ((be16_to_cpu(fdm_active->header.dump_status_flag) == FADUMP_ERROR_FLAG) ||
711 			(fdm_active->cpu_state_data.error_flags != 0) ||
712 			(fdm_active->rmr_region.error_flags != 0)) {
713 		printk(KERN_ERR "Dump taken by platform is not valid\n");
714 		return -EINVAL;
715 	}
716 	if ((fdm_active->rmr_region.bytes_dumped !=
717 			fdm_active->rmr_region.source_len) ||
718 			!fdm_active->cpu_state_data.bytes_dumped) {
719 		printk(KERN_ERR "Dump taken by platform is incomplete\n");
720 		return -EINVAL;
721 	}
722 
723 	/* Validate the fadump crash info header */
724 	fdh = __va(fw_dump.fadumphdr_addr);
725 	if (fdh->magic_number != FADUMP_CRASH_INFO_MAGIC) {
726 		printk(KERN_ERR "Crash info header is not valid.\n");
727 		return -EINVAL;
728 	}
729 
730 	rc = fadump_build_cpu_notes(fdm_active);
731 	if (rc)
732 		return rc;
733 
734 	/*
735 	 * We are done validating dump info and elfcore header is now ready
736 	 * to be exported. set elfcorehdr_addr so that vmcore module will
737 	 * export the elfcore header through '/proc/vmcore'.
738 	 */
739 	elfcorehdr_addr = fdh->elfcorehdr_addr;
740 
741 	return 0;
742 }
743 
744 static inline void fadump_add_crash_memory(unsigned long long base,
745 					unsigned long long end)
746 {
747 	if (base == end)
748 		return;
749 
750 	pr_debug("crash_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n",
751 		crash_mem_ranges, base, end - 1, (end - base));
752 	crash_memory_ranges[crash_mem_ranges].base = base;
753 	crash_memory_ranges[crash_mem_ranges].size = end - base;
754 	crash_mem_ranges++;
755 }
756 
757 static void fadump_exclude_reserved_area(unsigned long long start,
758 					unsigned long long end)
759 {
760 	unsigned long long ra_start, ra_end;
761 
762 	ra_start = fw_dump.reserve_dump_area_start;
763 	ra_end = ra_start + fw_dump.reserve_dump_area_size;
764 
765 	if ((ra_start < end) && (ra_end > start)) {
766 		if ((start < ra_start) && (end > ra_end)) {
767 			fadump_add_crash_memory(start, ra_start);
768 			fadump_add_crash_memory(ra_end, end);
769 		} else if (start < ra_start) {
770 			fadump_add_crash_memory(start, ra_start);
771 		} else if (ra_end < end) {
772 			fadump_add_crash_memory(ra_end, end);
773 		}
774 	} else
775 		fadump_add_crash_memory(start, end);
776 }
777 
778 static int fadump_init_elfcore_header(char *bufp)
779 {
780 	struct elfhdr *elf;
781 
782 	elf = (struct elfhdr *) bufp;
783 	bufp += sizeof(struct elfhdr);
784 	memcpy(elf->e_ident, ELFMAG, SELFMAG);
785 	elf->e_ident[EI_CLASS] = ELF_CLASS;
786 	elf->e_ident[EI_DATA] = ELF_DATA;
787 	elf->e_ident[EI_VERSION] = EV_CURRENT;
788 	elf->e_ident[EI_OSABI] = ELF_OSABI;
789 	memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
790 	elf->e_type = ET_CORE;
791 	elf->e_machine = ELF_ARCH;
792 	elf->e_version = EV_CURRENT;
793 	elf->e_entry = 0;
794 	elf->e_phoff = sizeof(struct elfhdr);
795 	elf->e_shoff = 0;
796 #if defined(_CALL_ELF)
797 	elf->e_flags = _CALL_ELF;
798 #else
799 	elf->e_flags = 0;
800 #endif
801 	elf->e_ehsize = sizeof(struct elfhdr);
802 	elf->e_phentsize = sizeof(struct elf_phdr);
803 	elf->e_phnum = 0;
804 	elf->e_shentsize = 0;
805 	elf->e_shnum = 0;
806 	elf->e_shstrndx = 0;
807 
808 	return 0;
809 }
810 
811 /*
812  * Traverse through memblock structure and setup crash memory ranges. These
813  * ranges will be used create PT_LOAD program headers in elfcore header.
814  */
815 static void fadump_setup_crash_memory_ranges(void)
816 {
817 	struct memblock_region *reg;
818 	unsigned long long start, end;
819 
820 	pr_debug("Setup crash memory ranges.\n");
821 	crash_mem_ranges = 0;
822 	/*
823 	 * add the first memory chunk (RMA_START through boot_memory_size) as
824 	 * a separate memory chunk. The reason is, at the time crash firmware
825 	 * will move the content of this memory chunk to different location
826 	 * specified during fadump registration. We need to create a separate
827 	 * program header for this chunk with the correct offset.
828 	 */
829 	fadump_add_crash_memory(RMA_START, fw_dump.boot_memory_size);
830 
831 	for_each_memblock(memory, reg) {
832 		start = (unsigned long long)reg->base;
833 		end = start + (unsigned long long)reg->size;
834 		if (start == RMA_START && end >= fw_dump.boot_memory_size)
835 			start = fw_dump.boot_memory_size;
836 
837 		/* add this range excluding the reserved dump area. */
838 		fadump_exclude_reserved_area(start, end);
839 	}
840 }
841 
842 /*
843  * If the given physical address falls within the boot memory region then
844  * return the relocated address that points to the dump region reserved
845  * for saving initial boot memory contents.
846  */
847 static inline unsigned long fadump_relocate(unsigned long paddr)
848 {
849 	if (paddr > RMA_START && paddr < fw_dump.boot_memory_size)
850 		return be64_to_cpu(fdm.rmr_region.destination_address) + paddr;
851 	else
852 		return paddr;
853 }
854 
855 static int fadump_create_elfcore_headers(char *bufp)
856 {
857 	struct elfhdr *elf;
858 	struct elf_phdr *phdr;
859 	int i;
860 
861 	fadump_init_elfcore_header(bufp);
862 	elf = (struct elfhdr *)bufp;
863 	bufp += sizeof(struct elfhdr);
864 
865 	/*
866 	 * setup ELF PT_NOTE, place holder for cpu notes info. The notes info
867 	 * will be populated during second kernel boot after crash. Hence
868 	 * this PT_NOTE will always be the first elf note.
869 	 *
870 	 * NOTE: Any new ELF note addition should be placed after this note.
871 	 */
872 	phdr = (struct elf_phdr *)bufp;
873 	bufp += sizeof(struct elf_phdr);
874 	phdr->p_type = PT_NOTE;
875 	phdr->p_flags = 0;
876 	phdr->p_vaddr = 0;
877 	phdr->p_align = 0;
878 
879 	phdr->p_offset = 0;
880 	phdr->p_paddr = 0;
881 	phdr->p_filesz = 0;
882 	phdr->p_memsz = 0;
883 
884 	(elf->e_phnum)++;
885 
886 	/* setup ELF PT_NOTE for vmcoreinfo */
887 	phdr = (struct elf_phdr *)bufp;
888 	bufp += sizeof(struct elf_phdr);
889 	phdr->p_type	= PT_NOTE;
890 	phdr->p_flags	= 0;
891 	phdr->p_vaddr	= 0;
892 	phdr->p_align	= 0;
893 
894 	phdr->p_paddr	= fadump_relocate(paddr_vmcoreinfo_note());
895 	phdr->p_offset	= phdr->p_paddr;
896 	phdr->p_memsz	= vmcoreinfo_max_size;
897 	phdr->p_filesz	= vmcoreinfo_max_size;
898 
899 	/* Increment number of program headers. */
900 	(elf->e_phnum)++;
901 
902 	/* setup PT_LOAD sections. */
903 
904 	for (i = 0; i < crash_mem_ranges; i++) {
905 		unsigned long long mbase, msize;
906 		mbase = crash_memory_ranges[i].base;
907 		msize = crash_memory_ranges[i].size;
908 
909 		if (!msize)
910 			continue;
911 
912 		phdr = (struct elf_phdr *)bufp;
913 		bufp += sizeof(struct elf_phdr);
914 		phdr->p_type	= PT_LOAD;
915 		phdr->p_flags	= PF_R|PF_W|PF_X;
916 		phdr->p_offset	= mbase;
917 
918 		if (mbase == RMA_START) {
919 			/*
920 			 * The entire RMA region will be moved by firmware
921 			 * to the specified destination_address. Hence set
922 			 * the correct offset.
923 			 */
924 			phdr->p_offset = be64_to_cpu(fdm.rmr_region.destination_address);
925 		}
926 
927 		phdr->p_paddr = mbase;
928 		phdr->p_vaddr = (unsigned long)__va(mbase);
929 		phdr->p_filesz = msize;
930 		phdr->p_memsz = msize;
931 		phdr->p_align = 0;
932 
933 		/* Increment number of program headers. */
934 		(elf->e_phnum)++;
935 	}
936 	return 0;
937 }
938 
939 static unsigned long init_fadump_header(unsigned long addr)
940 {
941 	struct fadump_crash_info_header *fdh;
942 
943 	if (!addr)
944 		return 0;
945 
946 	fw_dump.fadumphdr_addr = addr;
947 	fdh = __va(addr);
948 	addr += sizeof(struct fadump_crash_info_header);
949 
950 	memset(fdh, 0, sizeof(struct fadump_crash_info_header));
951 	fdh->magic_number = FADUMP_CRASH_INFO_MAGIC;
952 	fdh->elfcorehdr_addr = addr;
953 	/* We will set the crashing cpu id in crash_fadump() during crash. */
954 	fdh->crashing_cpu = CPU_UNKNOWN;
955 
956 	return addr;
957 }
958 
959 static void register_fadump(void)
960 {
961 	unsigned long addr;
962 	void *vaddr;
963 
964 	/*
965 	 * If no memory is reserved then we can not register for firmware-
966 	 * assisted dump.
967 	 */
968 	if (!fw_dump.reserve_dump_area_size)
969 		return;
970 
971 	fadump_setup_crash_memory_ranges();
972 
973 	addr = be64_to_cpu(fdm.rmr_region.destination_address) + be64_to_cpu(fdm.rmr_region.source_len);
974 	/* Initialize fadump crash info header. */
975 	addr = init_fadump_header(addr);
976 	vaddr = __va(addr);
977 
978 	pr_debug("Creating ELF core headers at %#016lx\n", addr);
979 	fadump_create_elfcore_headers(vaddr);
980 
981 	/* register the future kernel dump with firmware. */
982 	register_fw_dump(&fdm);
983 }
984 
985 static int fadump_unregister_dump(struct fadump_mem_struct *fdm)
986 {
987 	int rc = 0;
988 	unsigned int wait_time;
989 
990 	pr_debug("Un-register firmware-assisted dump\n");
991 
992 	/* TODO: Add upper time limit for the delay */
993 	do {
994 		rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
995 			FADUMP_UNREGISTER, fdm,
996 			sizeof(struct fadump_mem_struct));
997 
998 		wait_time = rtas_busy_delay_time(rc);
999 		if (wait_time)
1000 			mdelay(wait_time);
1001 	} while (wait_time);
1002 
1003 	if (rc) {
1004 		printk(KERN_ERR "Failed to un-register firmware-assisted dump."
1005 			" unexpected error(%d).\n", rc);
1006 		return rc;
1007 	}
1008 	fw_dump.dump_registered = 0;
1009 	return 0;
1010 }
1011 
1012 static int fadump_invalidate_dump(struct fadump_mem_struct *fdm)
1013 {
1014 	int rc = 0;
1015 	unsigned int wait_time;
1016 
1017 	pr_debug("Invalidating firmware-assisted dump registration\n");
1018 
1019 	/* TODO: Add upper time limit for the delay */
1020 	do {
1021 		rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
1022 			FADUMP_INVALIDATE, fdm,
1023 			sizeof(struct fadump_mem_struct));
1024 
1025 		wait_time = rtas_busy_delay_time(rc);
1026 		if (wait_time)
1027 			mdelay(wait_time);
1028 	} while (wait_time);
1029 
1030 	if (rc) {
1031 		pr_err("Failed to invalidate firmware-assisted dump registration. Unexpected error (%d).\n", rc);
1032 		return rc;
1033 	}
1034 	fw_dump.dump_active = 0;
1035 	fdm_active = NULL;
1036 	return 0;
1037 }
1038 
1039 void fadump_cleanup(void)
1040 {
1041 	/* Invalidate the registration only if dump is active. */
1042 	if (fw_dump.dump_active) {
1043 		init_fadump_mem_struct(&fdm,
1044 			be64_to_cpu(fdm_active->cpu_state_data.destination_address));
1045 		fadump_invalidate_dump(&fdm);
1046 	}
1047 }
1048 
1049 /*
1050  * Release the memory that was reserved in early boot to preserve the memory
1051  * contents. The released memory will be available for general use.
1052  */
1053 static void fadump_release_memory(unsigned long begin, unsigned long end)
1054 {
1055 	unsigned long addr;
1056 	unsigned long ra_start, ra_end;
1057 
1058 	ra_start = fw_dump.reserve_dump_area_start;
1059 	ra_end = ra_start + fw_dump.reserve_dump_area_size;
1060 
1061 	for (addr = begin; addr < end; addr += PAGE_SIZE) {
1062 		/*
1063 		 * exclude the dump reserve area. Will reuse it for next
1064 		 * fadump registration.
1065 		 */
1066 		if (addr <= ra_end && ((addr + PAGE_SIZE) > ra_start))
1067 			continue;
1068 
1069 		free_reserved_page(pfn_to_page(addr >> PAGE_SHIFT));
1070 	}
1071 }
1072 
1073 static void fadump_invalidate_release_mem(void)
1074 {
1075 	unsigned long reserved_area_start, reserved_area_end;
1076 	unsigned long destination_address;
1077 
1078 	mutex_lock(&fadump_mutex);
1079 	if (!fw_dump.dump_active) {
1080 		mutex_unlock(&fadump_mutex);
1081 		return;
1082 	}
1083 
1084 	destination_address = be64_to_cpu(fdm_active->cpu_state_data.destination_address);
1085 	fadump_cleanup();
1086 	mutex_unlock(&fadump_mutex);
1087 
1088 	/*
1089 	 * Save the current reserved memory bounds we will require them
1090 	 * later for releasing the memory for general use.
1091 	 */
1092 	reserved_area_start = fw_dump.reserve_dump_area_start;
1093 	reserved_area_end = reserved_area_start +
1094 			fw_dump.reserve_dump_area_size;
1095 	/*
1096 	 * Setup reserve_dump_area_start and its size so that we can
1097 	 * reuse this reserved memory for Re-registration.
1098 	 */
1099 	fw_dump.reserve_dump_area_start = destination_address;
1100 	fw_dump.reserve_dump_area_size = get_fadump_area_size();
1101 
1102 	fadump_release_memory(reserved_area_start, reserved_area_end);
1103 	if (fw_dump.cpu_notes_buf) {
1104 		fadump_cpu_notes_buf_free(
1105 				(unsigned long)__va(fw_dump.cpu_notes_buf),
1106 				fw_dump.cpu_notes_buf_size);
1107 		fw_dump.cpu_notes_buf = 0;
1108 		fw_dump.cpu_notes_buf_size = 0;
1109 	}
1110 	/* Initialize the kernel dump memory structure for FAD registration. */
1111 	init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
1112 }
1113 
1114 static ssize_t fadump_release_memory_store(struct kobject *kobj,
1115 					struct kobj_attribute *attr,
1116 					const char *buf, size_t count)
1117 {
1118 	if (!fw_dump.dump_active)
1119 		return -EPERM;
1120 
1121 	if (buf[0] == '1') {
1122 		/*
1123 		 * Take away the '/proc/vmcore'. We are releasing the dump
1124 		 * memory, hence it will not be valid anymore.
1125 		 */
1126 #ifdef CONFIG_PROC_VMCORE
1127 		vmcore_cleanup();
1128 #endif
1129 		fadump_invalidate_release_mem();
1130 
1131 	} else
1132 		return -EINVAL;
1133 	return count;
1134 }
1135 
1136 static ssize_t fadump_enabled_show(struct kobject *kobj,
1137 					struct kobj_attribute *attr,
1138 					char *buf)
1139 {
1140 	return sprintf(buf, "%d\n", fw_dump.fadump_enabled);
1141 }
1142 
1143 static ssize_t fadump_register_show(struct kobject *kobj,
1144 					struct kobj_attribute *attr,
1145 					char *buf)
1146 {
1147 	return sprintf(buf, "%d\n", fw_dump.dump_registered);
1148 }
1149 
1150 static ssize_t fadump_register_store(struct kobject *kobj,
1151 					struct kobj_attribute *attr,
1152 					const char *buf, size_t count)
1153 {
1154 	int ret = 0;
1155 
1156 	if (!fw_dump.fadump_enabled || fdm_active)
1157 		return -EPERM;
1158 
1159 	mutex_lock(&fadump_mutex);
1160 
1161 	switch (buf[0]) {
1162 	case '0':
1163 		if (fw_dump.dump_registered == 0) {
1164 			ret = -EINVAL;
1165 			goto unlock_out;
1166 		}
1167 		/* Un-register Firmware-assisted dump */
1168 		fadump_unregister_dump(&fdm);
1169 		break;
1170 	case '1':
1171 		if (fw_dump.dump_registered == 1) {
1172 			ret = -EINVAL;
1173 			goto unlock_out;
1174 		}
1175 		/* Register Firmware-assisted dump */
1176 		register_fadump();
1177 		break;
1178 	default:
1179 		ret = -EINVAL;
1180 		break;
1181 	}
1182 
1183 unlock_out:
1184 	mutex_unlock(&fadump_mutex);
1185 	return ret < 0 ? ret : count;
1186 }
1187 
1188 static int fadump_region_show(struct seq_file *m, void *private)
1189 {
1190 	const struct fadump_mem_struct *fdm_ptr;
1191 
1192 	if (!fw_dump.fadump_enabled)
1193 		return 0;
1194 
1195 	mutex_lock(&fadump_mutex);
1196 	if (fdm_active)
1197 		fdm_ptr = fdm_active;
1198 	else {
1199 		mutex_unlock(&fadump_mutex);
1200 		fdm_ptr = &fdm;
1201 	}
1202 
1203 	seq_printf(m,
1204 			"CPU : [%#016llx-%#016llx] %#llx bytes, "
1205 			"Dumped: %#llx\n",
1206 			be64_to_cpu(fdm_ptr->cpu_state_data.destination_address),
1207 			be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) +
1208 			be64_to_cpu(fdm_ptr->cpu_state_data.source_len) - 1,
1209 			be64_to_cpu(fdm_ptr->cpu_state_data.source_len),
1210 			be64_to_cpu(fdm_ptr->cpu_state_data.bytes_dumped));
1211 	seq_printf(m,
1212 			"HPTE: [%#016llx-%#016llx] %#llx bytes, "
1213 			"Dumped: %#llx\n",
1214 			be64_to_cpu(fdm_ptr->hpte_region.destination_address),
1215 			be64_to_cpu(fdm_ptr->hpte_region.destination_address) +
1216 			be64_to_cpu(fdm_ptr->hpte_region.source_len) - 1,
1217 			be64_to_cpu(fdm_ptr->hpte_region.source_len),
1218 			be64_to_cpu(fdm_ptr->hpte_region.bytes_dumped));
1219 	seq_printf(m,
1220 			"DUMP: [%#016llx-%#016llx] %#llx bytes, "
1221 			"Dumped: %#llx\n",
1222 			be64_to_cpu(fdm_ptr->rmr_region.destination_address),
1223 			be64_to_cpu(fdm_ptr->rmr_region.destination_address) +
1224 			be64_to_cpu(fdm_ptr->rmr_region.source_len) - 1,
1225 			be64_to_cpu(fdm_ptr->rmr_region.source_len),
1226 			be64_to_cpu(fdm_ptr->rmr_region.bytes_dumped));
1227 
1228 	if (!fdm_active ||
1229 		(fw_dump.reserve_dump_area_start ==
1230 		be64_to_cpu(fdm_ptr->cpu_state_data.destination_address)))
1231 		goto out;
1232 
1233 	/* Dump is active. Show reserved memory region. */
1234 	seq_printf(m,
1235 			"    : [%#016llx-%#016llx] %#llx bytes, "
1236 			"Dumped: %#llx\n",
1237 			(unsigned long long)fw_dump.reserve_dump_area_start,
1238 			be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) - 1,
1239 			be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) -
1240 			fw_dump.reserve_dump_area_start,
1241 			be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) -
1242 			fw_dump.reserve_dump_area_start);
1243 out:
1244 	if (fdm_active)
1245 		mutex_unlock(&fadump_mutex);
1246 	return 0;
1247 }
1248 
1249 static struct kobj_attribute fadump_release_attr = __ATTR(fadump_release_mem,
1250 						0200, NULL,
1251 						fadump_release_memory_store);
1252 static struct kobj_attribute fadump_attr = __ATTR(fadump_enabled,
1253 						0444, fadump_enabled_show,
1254 						NULL);
1255 static struct kobj_attribute fadump_register_attr = __ATTR(fadump_registered,
1256 						0644, fadump_register_show,
1257 						fadump_register_store);
1258 
1259 static int fadump_region_open(struct inode *inode, struct file *file)
1260 {
1261 	return single_open(file, fadump_region_show, inode->i_private);
1262 }
1263 
1264 static const struct file_operations fadump_region_fops = {
1265 	.open    = fadump_region_open,
1266 	.read    = seq_read,
1267 	.llseek  = seq_lseek,
1268 	.release = single_release,
1269 };
1270 
1271 static void fadump_init_files(void)
1272 {
1273 	struct dentry *debugfs_file;
1274 	int rc = 0;
1275 
1276 	rc = sysfs_create_file(kernel_kobj, &fadump_attr.attr);
1277 	if (rc)
1278 		printk(KERN_ERR "fadump: unable to create sysfs file"
1279 			" fadump_enabled (%d)\n", rc);
1280 
1281 	rc = sysfs_create_file(kernel_kobj, &fadump_register_attr.attr);
1282 	if (rc)
1283 		printk(KERN_ERR "fadump: unable to create sysfs file"
1284 			" fadump_registered (%d)\n", rc);
1285 
1286 	debugfs_file = debugfs_create_file("fadump_region", 0444,
1287 					powerpc_debugfs_root, NULL,
1288 					&fadump_region_fops);
1289 	if (!debugfs_file)
1290 		printk(KERN_ERR "fadump: unable to create debugfs file"
1291 				" fadump_region\n");
1292 
1293 	if (fw_dump.dump_active) {
1294 		rc = sysfs_create_file(kernel_kobj, &fadump_release_attr.attr);
1295 		if (rc)
1296 			printk(KERN_ERR "fadump: unable to create sysfs file"
1297 				" fadump_release_mem (%d)\n", rc);
1298 	}
1299 	return;
1300 }
1301 
1302 /*
1303  * Prepare for firmware-assisted dump.
1304  */
1305 int __init setup_fadump(void)
1306 {
1307 	if (!fw_dump.fadump_enabled)
1308 		return 0;
1309 
1310 	if (!fw_dump.fadump_supported) {
1311 		printk(KERN_ERR "Firmware-assisted dump is not supported on"
1312 			" this hardware\n");
1313 		return 0;
1314 	}
1315 
1316 	fadump_show_config();
1317 	/*
1318 	 * If dump data is available then see if it is valid and prepare for
1319 	 * saving it to the disk.
1320 	 */
1321 	if (fw_dump.dump_active) {
1322 		/*
1323 		 * if dump process fails then invalidate the registration
1324 		 * and release memory before proceeding for re-registration.
1325 		 */
1326 		if (process_fadump(fdm_active) < 0)
1327 			fadump_invalidate_release_mem();
1328 	}
1329 	/* Initialize the kernel dump memory structure for FAD registration. */
1330 	else if (fw_dump.reserve_dump_area_size)
1331 		init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
1332 	fadump_init_files();
1333 
1334 	return 1;
1335 }
1336 subsys_initcall(setup_fadump);
1337