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