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