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