1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Firmware-Assisted Dump support on POWER platform (OPAL).
4  *
5  * Copyright 2019, Hari Bathini, IBM Corporation.
6  */
7 
8 #define pr_fmt(fmt) "opal fadump: " fmt
9 
10 #include <linux/string.h>
11 #include <linux/seq_file.h>
12 #include <linux/of.h>
13 #include <linux/of_fdt.h>
14 #include <linux/libfdt.h>
15 #include <linux/mm.h>
16 #include <linux/crash_dump.h>
17 
18 #include <asm/page.h>
19 #include <asm/opal.h>
20 #include <asm/fadump-internal.h>
21 
22 #include "opal-fadump.h"
23 
24 
25 #ifdef CONFIG_PRESERVE_FA_DUMP
26 /*
27  * When dump is active but PRESERVE_FA_DUMP is enabled on the kernel,
28  * ensure crash data is preserved in hope that the subsequent memory
29  * preserving kernel boot is going to process this crash data.
30  */
31 void __init opal_fadump_dt_scan(struct fw_dump *fadump_conf, u64 node)
32 {
33 	const struct opal_fadump_mem_struct *opal_fdm_active;
34 	const __be32 *prop;
35 	unsigned long dn;
36 	u64 addr = 0;
37 	s64 ret;
38 
39 	dn = of_get_flat_dt_subnode_by_name(node, "dump");
40 	if (dn == -FDT_ERR_NOTFOUND)
41 		return;
42 
43 	/*
44 	 * Check if dump has been initiated on last reboot.
45 	 */
46 	prop = of_get_flat_dt_prop(dn, "mpipl-boot", NULL);
47 	if (!prop)
48 		return;
49 
50 	ret = opal_mpipl_query_tag(OPAL_MPIPL_TAG_KERNEL, &addr);
51 	if ((ret != OPAL_SUCCESS) || !addr) {
52 		pr_debug("Could not get Kernel metadata (%lld)\n", ret);
53 		return;
54 	}
55 
56 	/*
57 	 * Preserve memory only if kernel memory regions are registered
58 	 * with f/w for MPIPL.
59 	 */
60 	addr = be64_to_cpu(addr);
61 	pr_debug("Kernel metadata addr: %llx\n", addr);
62 	opal_fdm_active = (void *)addr;
63 	if (opal_fdm_active->registered_regions == 0)
64 		return;
65 
66 	ret = opal_mpipl_query_tag(OPAL_MPIPL_TAG_BOOT_MEM, &addr);
67 	if ((ret != OPAL_SUCCESS) || !addr) {
68 		pr_err("Failed to get boot memory tag (%lld)\n", ret);
69 		return;
70 	}
71 
72 	/*
73 	 * Memory below this address can be used for booting a
74 	 * capture kernel or petitboot kernel. Preserve everything
75 	 * above this address for processing crashdump.
76 	 */
77 	fadump_conf->boot_mem_top = be64_to_cpu(addr);
78 	pr_debug("Preserve everything above %llx\n", fadump_conf->boot_mem_top);
79 
80 	pr_info("Firmware-assisted dump is active.\n");
81 	fadump_conf->dump_active = 1;
82 }
83 
84 #else /* CONFIG_PRESERVE_FA_DUMP */
85 static const struct opal_fadump_mem_struct *opal_fdm_active;
86 static const struct opal_mpipl_fadump *opal_cpu_metadata;
87 static struct opal_fadump_mem_struct *opal_fdm;
88 
89 #ifdef CONFIG_OPAL_CORE
90 extern bool kernel_initiated;
91 #endif
92 
93 static int opal_fadump_unregister(struct fw_dump *fadump_conf);
94 
95 static void opal_fadump_update_config(struct fw_dump *fadump_conf,
96 				      const struct opal_fadump_mem_struct *fdm)
97 {
98 	pr_debug("Boot memory regions count: %d\n", fdm->region_cnt);
99 
100 	/*
101 	 * The destination address of the first boot memory region is the
102 	 * destination address of boot memory regions.
103 	 */
104 	fadump_conf->boot_mem_dest_addr = fdm->rgn[0].dest;
105 	pr_debug("Destination address of boot memory regions: %#016llx\n",
106 		 fadump_conf->boot_mem_dest_addr);
107 
108 	fadump_conf->fadumphdr_addr = fdm->fadumphdr_addr;
109 }
110 
111 /*
112  * This function is called in the capture kernel to get configuration details
113  * from metadata setup by the first kernel.
114  */
115 static void opal_fadump_get_config(struct fw_dump *fadump_conf,
116 				   const struct opal_fadump_mem_struct *fdm)
117 {
118 	int i;
119 
120 	if (!fadump_conf->dump_active)
121 		return;
122 
123 	fadump_conf->boot_memory_size = 0;
124 
125 	pr_debug("Boot memory regions:\n");
126 	for (i = 0; i < fdm->region_cnt; i++) {
127 		pr_debug("\t%d. base: 0x%llx, size: 0x%llx\n",
128 			 (i + 1), fdm->rgn[i].src, fdm->rgn[i].size);
129 
130 		fadump_conf->boot_memory_size += fdm->rgn[i].size;
131 	}
132 
133 	/*
134 	 * Start address of reserve dump area (permanent reservation) for
135 	 * re-registering FADump after dump capture.
136 	 */
137 	fadump_conf->reserve_dump_area_start = fdm->rgn[0].dest;
138 
139 	/*
140 	 * Rarely, but it can so happen that system crashes before all
141 	 * boot memory regions are registered for MPIPL. In such
142 	 * cases, warn that the vmcore may not be accurate and proceed
143 	 * anyway as that is the best bet considering free pages, cache
144 	 * pages, user pages, etc are usually filtered out.
145 	 *
146 	 * Hope the memory that could not be preserved only has pages
147 	 * that are usually filtered out while saving the vmcore.
148 	 */
149 	if (fdm->region_cnt > fdm->registered_regions) {
150 		pr_warn("Not all memory regions were saved!!!\n");
151 		pr_warn("  Unsaved memory regions:\n");
152 		i = fdm->registered_regions;
153 		while (i < fdm->region_cnt) {
154 			pr_warn("\t[%03d] base: 0x%llx, size: 0x%llx\n",
155 				i, fdm->rgn[i].src, fdm->rgn[i].size);
156 			i++;
157 		}
158 
159 		pr_warn("If the unsaved regions only contain pages that are filtered out (eg. free/user pages), the vmcore should still be usable.\n");
160 		pr_warn("WARNING: If the unsaved regions contain kernel pages, the vmcore will be corrupted.\n");
161 	}
162 
163 	opal_fadump_update_config(fadump_conf, fdm);
164 }
165 
166 /* Initialize kernel metadata */
167 static void opal_fadump_init_metadata(struct opal_fadump_mem_struct *fdm)
168 {
169 	fdm->version = OPAL_FADUMP_VERSION;
170 	fdm->region_cnt = 0;
171 	fdm->registered_regions = 0;
172 	fdm->fadumphdr_addr = 0;
173 }
174 
175 static u64 opal_fadump_init_mem_struct(struct fw_dump *fadump_conf)
176 {
177 	int max_copy_size, cur_size, size;
178 	u64 src_addr, dest_addr;
179 
180 	opal_fdm = __va(fadump_conf->kernel_metadata);
181 	opal_fadump_init_metadata(opal_fdm);
182 
183 	/*
184 	 * Firmware supports 32-bit field for size. Align it to PAGE_SIZE
185 	 * and request firmware to copy multiple kernel boot memory regions.
186 	 */
187 	max_copy_size = _ALIGN_DOWN(U32_MAX, PAGE_SIZE);
188 
189 	/* Boot memory regions */
190 	src_addr = 0;
191 	dest_addr = fadump_conf->reserve_dump_area_start;
192 	size = fadump_conf->boot_memory_size;
193 	while (size) {
194 		cur_size = size > max_copy_size ? max_copy_size : size;
195 
196 		opal_fdm->rgn[opal_fdm->region_cnt].src  = src_addr;
197 		opal_fdm->rgn[opal_fdm->region_cnt].dest = dest_addr;
198 		opal_fdm->rgn[opal_fdm->region_cnt].size = cur_size;
199 
200 		opal_fdm->region_cnt++;
201 		dest_addr	+= cur_size;
202 		src_addr	+= cur_size;
203 		size		-= cur_size;
204 	}
205 
206 	/*
207 	 * Kernel metadata is passed to f/w and retrieved in capture kerenl.
208 	 * So, use it to save fadump header address instead of calculating it.
209 	 */
210 	opal_fdm->fadumphdr_addr = (opal_fdm->rgn[0].dest +
211 				    fadump_conf->boot_memory_size);
212 
213 	opal_fadump_update_config(fadump_conf, opal_fdm);
214 
215 	return dest_addr;
216 }
217 
218 static u64 opal_fadump_get_metadata_size(void)
219 {
220 	return PAGE_ALIGN(sizeof(struct opal_fadump_mem_struct));
221 }
222 
223 static int opal_fadump_setup_metadata(struct fw_dump *fadump_conf)
224 {
225 	int err = 0;
226 	s64 ret;
227 
228 	/*
229 	 * Use the last page(s) in FADump memory reservation for
230 	 * kernel metadata.
231 	 */
232 	fadump_conf->kernel_metadata = (fadump_conf->reserve_dump_area_start +
233 					fadump_conf->reserve_dump_area_size -
234 					opal_fadump_get_metadata_size());
235 	pr_info("Kernel metadata addr: %llx\n", fadump_conf->kernel_metadata);
236 
237 	/* Initialize kernel metadata before registering the address with f/w */
238 	opal_fdm = __va(fadump_conf->kernel_metadata);
239 	opal_fadump_init_metadata(opal_fdm);
240 
241 	/*
242 	 * Register metadata address with f/w. Can be retrieved in
243 	 * the capture kernel.
244 	 */
245 	ret = opal_mpipl_register_tag(OPAL_MPIPL_TAG_KERNEL,
246 				      fadump_conf->kernel_metadata);
247 	if (ret != OPAL_SUCCESS) {
248 		pr_err("Failed to set kernel metadata tag!\n");
249 		err = -EPERM;
250 	}
251 
252 	/*
253 	 * Register boot memory top address with f/w. Should be retrieved
254 	 * by a kernel that intends to preserve crash'ed kernel's memory.
255 	 */
256 	ret = opal_mpipl_register_tag(OPAL_MPIPL_TAG_BOOT_MEM,
257 				      fadump_conf->boot_memory_size);
258 	if (ret != OPAL_SUCCESS) {
259 		pr_err("Failed to set boot memory tag!\n");
260 		err = -EPERM;
261 	}
262 
263 	return err;
264 }
265 
266 static u64 opal_fadump_get_bootmem_min(void)
267 {
268 	return OPAL_FADUMP_MIN_BOOT_MEM;
269 }
270 
271 static int opal_fadump_register(struct fw_dump *fadump_conf)
272 {
273 	s64 rc = OPAL_PARAMETER;
274 	int i, err = -EIO;
275 
276 	for (i = 0; i < opal_fdm->region_cnt; i++) {
277 		rc = opal_mpipl_update(OPAL_MPIPL_ADD_RANGE,
278 				       opal_fdm->rgn[i].src,
279 				       opal_fdm->rgn[i].dest,
280 				       opal_fdm->rgn[i].size);
281 		if (rc != OPAL_SUCCESS)
282 			break;
283 
284 		opal_fdm->registered_regions++;
285 	}
286 
287 	switch (rc) {
288 	case OPAL_SUCCESS:
289 		pr_info("Registration is successful!\n");
290 		fadump_conf->dump_registered = 1;
291 		err = 0;
292 		break;
293 	case OPAL_RESOURCE:
294 		/* If MAX regions limit in f/w is hit, warn and proceed. */
295 		pr_warn("%d regions could not be registered for MPIPL as MAX limit is reached!\n",
296 			(opal_fdm->region_cnt - opal_fdm->registered_regions));
297 		fadump_conf->dump_registered = 1;
298 		err = 0;
299 		break;
300 	case OPAL_PARAMETER:
301 		pr_err("Failed to register. Parameter Error(%lld).\n", rc);
302 		break;
303 	case OPAL_HARDWARE:
304 		pr_err("Support not available.\n");
305 		fadump_conf->fadump_supported = 0;
306 		fadump_conf->fadump_enabled = 0;
307 		break;
308 	default:
309 		pr_err("Failed to register. Unknown Error(%lld).\n", rc);
310 		break;
311 	}
312 
313 	/*
314 	 * If some regions were registered before OPAL_MPIPL_ADD_RANGE
315 	 * OPAL call failed, unregister all regions.
316 	 */
317 	if ((err < 0) && (opal_fdm->registered_regions > 0))
318 		opal_fadump_unregister(fadump_conf);
319 
320 	return err;
321 }
322 
323 static int opal_fadump_unregister(struct fw_dump *fadump_conf)
324 {
325 	s64 rc;
326 
327 	rc = opal_mpipl_update(OPAL_MPIPL_REMOVE_ALL, 0, 0, 0);
328 	if (rc) {
329 		pr_err("Failed to un-register - unexpected Error(%lld).\n", rc);
330 		return -EIO;
331 	}
332 
333 	opal_fdm->registered_regions = 0;
334 	fadump_conf->dump_registered = 0;
335 	return 0;
336 }
337 
338 static int opal_fadump_invalidate(struct fw_dump *fadump_conf)
339 {
340 	s64 rc;
341 
342 	rc = opal_mpipl_update(OPAL_MPIPL_FREE_PRESERVED_MEMORY, 0, 0, 0);
343 	if (rc) {
344 		pr_err("Failed to invalidate - unexpected Error(%lld).\n", rc);
345 		return -EIO;
346 	}
347 
348 	fadump_conf->dump_active = 0;
349 	opal_fdm_active = NULL;
350 	return 0;
351 }
352 
353 static void opal_fadump_cleanup(struct fw_dump *fadump_conf)
354 {
355 	s64 ret;
356 
357 	ret = opal_mpipl_register_tag(OPAL_MPIPL_TAG_KERNEL, 0);
358 	if (ret != OPAL_SUCCESS)
359 		pr_warn("Could not reset (%llu) kernel metadata tag!\n", ret);
360 }
361 
362 /*
363  * Verify if CPU state data is available. If available, do a bit of sanity
364  * checking before processing this data.
365  */
366 static bool __init is_opal_fadump_cpu_data_valid(struct fw_dump *fadump_conf)
367 {
368 	if (!opal_cpu_metadata)
369 		return false;
370 
371 	fadump_conf->cpu_state_data_version =
372 		be32_to_cpu(opal_cpu_metadata->cpu_data_version);
373 	fadump_conf->cpu_state_entry_size =
374 		be32_to_cpu(opal_cpu_metadata->cpu_data_size);
375 	fadump_conf->cpu_state_dest_vaddr =
376 		(u64)__va(be64_to_cpu(opal_cpu_metadata->region[0].dest));
377 	fadump_conf->cpu_state_data_size =
378 		be64_to_cpu(opal_cpu_metadata->region[0].size);
379 
380 	if (fadump_conf->cpu_state_data_version != HDAT_FADUMP_CPU_DATA_VER) {
381 		pr_warn("Supported CPU state data version: %u, found: %d!\n",
382 			HDAT_FADUMP_CPU_DATA_VER,
383 			fadump_conf->cpu_state_data_version);
384 		pr_warn("WARNING: F/W using newer CPU state data format!!\n");
385 	}
386 
387 	if ((fadump_conf->cpu_state_dest_vaddr == 0) ||
388 	    (fadump_conf->cpu_state_entry_size == 0) ||
389 	    (fadump_conf->cpu_state_entry_size >
390 	     fadump_conf->cpu_state_data_size)) {
391 		pr_err("CPU state data is invalid. Ignoring!\n");
392 		return false;
393 	}
394 
395 	return true;
396 }
397 
398 /*
399  * Convert CPU state data saved at the time of crash into ELF notes.
400  *
401  * While the crashing CPU's register data is saved by the kernel, CPU state
402  * data for all CPUs is saved by f/w. In CPU state data provided by f/w,
403  * each register entry is of 16 bytes, a numerical identifier along with
404  * a GPR/SPR flag in the first 8 bytes and the register value in the next
405  * 8 bytes. For more details refer to F/W documentation. If this data is
406  * missing or in unsupported format, append crashing CPU's register data
407  * saved by the kernel in the PT_NOTE, to have something to work with in
408  * the vmcore file.
409  */
410 static int __init
411 opal_fadump_build_cpu_notes(struct fw_dump *fadump_conf,
412 			    struct fadump_crash_info_header *fdh)
413 {
414 	u32 thread_pir, size_per_thread, regs_offset, regs_cnt, reg_esize;
415 	struct hdat_fadump_thread_hdr *thdr;
416 	bool is_cpu_data_valid = false;
417 	u32 num_cpus = 1, *note_buf;
418 	struct pt_regs regs;
419 	char *bufp;
420 	int rc, i;
421 
422 	if (is_opal_fadump_cpu_data_valid(fadump_conf)) {
423 		size_per_thread = fadump_conf->cpu_state_entry_size;
424 		num_cpus = (fadump_conf->cpu_state_data_size / size_per_thread);
425 		bufp = __va(fadump_conf->cpu_state_dest_vaddr);
426 		is_cpu_data_valid = true;
427 	}
428 
429 	rc = fadump_setup_cpu_notes_buf(num_cpus);
430 	if (rc != 0)
431 		return rc;
432 
433 	note_buf = (u32 *)fadump_conf->cpu_notes_buf_vaddr;
434 	if (!is_cpu_data_valid)
435 		goto out;
436 
437 	/*
438 	 * Offset for register entries, entry size and registers count is
439 	 * duplicated in every thread header in keeping with HDAT format.
440 	 * Use these values from the first thread header.
441 	 */
442 	thdr = (struct hdat_fadump_thread_hdr *)bufp;
443 	regs_offset = (offsetof(struct hdat_fadump_thread_hdr, offset) +
444 		       be32_to_cpu(thdr->offset));
445 	reg_esize = be32_to_cpu(thdr->esize);
446 	regs_cnt  = be32_to_cpu(thdr->ecnt);
447 
448 	pr_debug("--------CPU State Data------------\n");
449 	pr_debug("NumCpus     : %u\n", num_cpus);
450 	pr_debug("\tOffset: %u, Entry size: %u, Cnt: %u\n",
451 		 regs_offset, reg_esize, regs_cnt);
452 
453 	for (i = 0; i < num_cpus; i++, bufp += size_per_thread) {
454 		thdr = (struct hdat_fadump_thread_hdr *)bufp;
455 
456 		thread_pir = be32_to_cpu(thdr->pir);
457 		pr_debug("[%04d] PIR: 0x%x, core state: 0x%02x\n",
458 			 i, thread_pir, thdr->core_state);
459 
460 		/*
461 		 * If this is kernel initiated crash, crashing_cpu would be set
462 		 * appropriately and register data of the crashing CPU saved by
463 		 * crashing kernel. Add this saved register data of crashing CPU
464 		 * to elf notes and populate the pt_regs for the remaining CPUs
465 		 * from register state data provided by firmware.
466 		 */
467 		if (fdh->crashing_cpu == thread_pir) {
468 			note_buf = fadump_regs_to_elf_notes(note_buf,
469 							    &fdh->regs);
470 			pr_debug("Crashing CPU PIR: 0x%x - R1 : 0x%lx, NIP : 0x%lx\n",
471 				 fdh->crashing_cpu, fdh->regs.gpr[1],
472 				 fdh->regs.nip);
473 			continue;
474 		}
475 
476 		/*
477 		 * Register state data of MAX cores is provided by firmware,
478 		 * but some of this cores may not be active. So, while
479 		 * processing register state data, check core state and
480 		 * skip threads that belong to inactive cores.
481 		 */
482 		if (thdr->core_state == HDAT_FADUMP_CORE_INACTIVE)
483 			continue;
484 
485 		opal_fadump_read_regs((bufp + regs_offset), regs_cnt,
486 				      reg_esize, true, &regs);
487 		note_buf = fadump_regs_to_elf_notes(note_buf, &regs);
488 		pr_debug("CPU PIR: 0x%x - R1 : 0x%lx, NIP : 0x%lx\n",
489 			 thread_pir, regs.gpr[1], regs.nip);
490 	}
491 
492 out:
493 	/*
494 	 * CPU state data is invalid/unsupported. Try appending crashing CPU's
495 	 * register data, if it is saved by the kernel.
496 	 */
497 	if (fadump_conf->cpu_notes_buf_vaddr == (u64)note_buf) {
498 		if (fdh->crashing_cpu == FADUMP_CPU_UNKNOWN) {
499 			fadump_free_cpu_notes_buf();
500 			return -ENODEV;
501 		}
502 
503 		pr_warn("WARNING: appending only crashing CPU's register data\n");
504 		note_buf = fadump_regs_to_elf_notes(note_buf, &(fdh->regs));
505 	}
506 
507 	final_note(note_buf);
508 
509 	pr_debug("Updating elfcore header (%llx) with cpu notes\n",
510 		 fdh->elfcorehdr_addr);
511 	fadump_update_elfcore_header(__va(fdh->elfcorehdr_addr));
512 	return 0;
513 }
514 
515 static int __init opal_fadump_process(struct fw_dump *fadump_conf)
516 {
517 	struct fadump_crash_info_header *fdh;
518 	int rc = -EINVAL;
519 
520 	if (!opal_fdm_active || !fadump_conf->fadumphdr_addr)
521 		return rc;
522 
523 	/* Validate the fadump crash info header */
524 	fdh = __va(fadump_conf->fadumphdr_addr);
525 	if (fdh->magic_number != FADUMP_CRASH_INFO_MAGIC) {
526 		pr_err("Crash info header is not valid.\n");
527 		return rc;
528 	}
529 
530 #ifdef CONFIG_OPAL_CORE
531 	/*
532 	 * If this is a kernel initiated crash, crashing_cpu would be set
533 	 * appropriately and register data of the crashing CPU saved by
534 	 * crashing kernel. Add this saved register data of crashing CPU
535 	 * to elf notes and populate the pt_regs for the remaining CPUs
536 	 * from register state data provided by firmware.
537 	 */
538 	if (fdh->crashing_cpu != FADUMP_CPU_UNKNOWN)
539 		kernel_initiated = true;
540 #endif
541 
542 	rc = opal_fadump_build_cpu_notes(fadump_conf, fdh);
543 	if (rc)
544 		return rc;
545 
546 	/*
547 	 * We are done validating dump info and elfcore header is now ready
548 	 * to be exported. set elfcorehdr_addr so that vmcore module will
549 	 * export the elfcore header through '/proc/vmcore'.
550 	 */
551 	elfcorehdr_addr = fdh->elfcorehdr_addr;
552 
553 	return rc;
554 }
555 
556 static void opal_fadump_region_show(struct fw_dump *fadump_conf,
557 				    struct seq_file *m)
558 {
559 	const struct opal_fadump_mem_struct *fdm_ptr;
560 	u64 dumped_bytes = 0;
561 	int i;
562 
563 	if (fadump_conf->dump_active)
564 		fdm_ptr = opal_fdm_active;
565 	else
566 		fdm_ptr = opal_fdm;
567 
568 	for (i = 0; i < fdm_ptr->region_cnt; i++) {
569 		/*
570 		 * Only regions that are registered for MPIPL
571 		 * would have dump data.
572 		 */
573 		if ((fadump_conf->dump_active) &&
574 		    (i < fdm_ptr->registered_regions))
575 			dumped_bytes = fdm_ptr->rgn[i].size;
576 
577 		seq_printf(m, "DUMP: Src: %#016llx, Dest: %#016llx, ",
578 			   fdm_ptr->rgn[i].src, fdm_ptr->rgn[i].dest);
579 		seq_printf(m, "Size: %#llx, Dumped: %#llx bytes\n",
580 			   fdm_ptr->rgn[i].size, dumped_bytes);
581 	}
582 
583 	/* Dump is active. Show reserved area start address. */
584 	if (fadump_conf->dump_active) {
585 		seq_printf(m, "\nMemory above %#016lx is reserved for saving crash dump\n",
586 			   fadump_conf->reserve_dump_area_start);
587 	}
588 }
589 
590 static void opal_fadump_trigger(struct fadump_crash_info_header *fdh,
591 				const char *msg)
592 {
593 	int rc;
594 
595 	/*
596 	 * Unlike on pSeries platform, logical CPU number is not provided
597 	 * with architected register state data. So, store the crashing
598 	 * CPU's PIR instead to plug the appropriate register data for
599 	 * crashing CPU in the vmcore file.
600 	 */
601 	fdh->crashing_cpu = (u32)mfspr(SPRN_PIR);
602 
603 	rc = opal_cec_reboot2(OPAL_REBOOT_MPIPL, msg);
604 	if (rc == OPAL_UNSUPPORTED) {
605 		pr_emerg("Reboot type %d not supported.\n",
606 			 OPAL_REBOOT_MPIPL);
607 	} else if (rc == OPAL_HARDWARE)
608 		pr_emerg("No backend support for MPIPL!\n");
609 }
610 
611 static struct fadump_ops opal_fadump_ops = {
612 	.fadump_init_mem_struct		= opal_fadump_init_mem_struct,
613 	.fadump_get_metadata_size	= opal_fadump_get_metadata_size,
614 	.fadump_setup_metadata		= opal_fadump_setup_metadata,
615 	.fadump_get_bootmem_min		= opal_fadump_get_bootmem_min,
616 	.fadump_register		= opal_fadump_register,
617 	.fadump_unregister		= opal_fadump_unregister,
618 	.fadump_invalidate		= opal_fadump_invalidate,
619 	.fadump_cleanup			= opal_fadump_cleanup,
620 	.fadump_process			= opal_fadump_process,
621 	.fadump_region_show		= opal_fadump_region_show,
622 	.fadump_trigger			= opal_fadump_trigger,
623 };
624 
625 void __init opal_fadump_dt_scan(struct fw_dump *fadump_conf, u64 node)
626 {
627 	const __be32 *prop;
628 	unsigned long dn;
629 	u64 addr = 0;
630 	int i, len;
631 	s64 ret;
632 
633 	/*
634 	 * Check if Firmware-Assisted Dump is supported. if yes, check
635 	 * if dump has been initiated on last reboot.
636 	 */
637 	dn = of_get_flat_dt_subnode_by_name(node, "dump");
638 	if (dn == -FDT_ERR_NOTFOUND) {
639 		pr_debug("FADump support is missing!\n");
640 		return;
641 	}
642 
643 	if (!of_flat_dt_is_compatible(dn, "ibm,opal-dump")) {
644 		pr_err("Support missing for this f/w version!\n");
645 		return;
646 	}
647 
648 	prop = of_get_flat_dt_prop(dn, "fw-load-area", &len);
649 	if (prop) {
650 		/*
651 		 * Each f/w load area is an (address,size) pair,
652 		 * 2 cells each, totalling 4 cells per range.
653 		 */
654 		for (i = 0; i < len / (sizeof(*prop) * 4); i++) {
655 			u64 base, end;
656 
657 			base = of_read_number(prop + (i * 4) + 0, 2);
658 			end = base;
659 			end += of_read_number(prop + (i * 4) + 2, 2);
660 			if (end > OPAL_FADUMP_MIN_BOOT_MEM) {
661 				pr_err("F/W load area: 0x%llx-0x%llx\n",
662 				       base, end);
663 				pr_err("F/W version not supported!\n");
664 				return;
665 			}
666 		}
667 	}
668 
669 	fadump_conf->ops		= &opal_fadump_ops;
670 	fadump_conf->fadump_supported	= 1;
671 
672 	/*
673 	 * Check if dump has been initiated on last reboot.
674 	 */
675 	prop = of_get_flat_dt_prop(dn, "mpipl-boot", NULL);
676 	if (!prop)
677 		return;
678 
679 	ret = opal_mpipl_query_tag(OPAL_MPIPL_TAG_KERNEL, &addr);
680 	if ((ret != OPAL_SUCCESS) || !addr) {
681 		pr_err("Failed to get Kernel metadata (%lld)\n", ret);
682 		return;
683 	}
684 
685 	addr = be64_to_cpu(addr);
686 	pr_debug("Kernel metadata addr: %llx\n", addr);
687 
688 	opal_fdm_active = __va(addr);
689 	if (opal_fdm_active->version != OPAL_FADUMP_VERSION) {
690 		pr_warn("Supported kernel metadata version: %u, found: %d!\n",
691 			OPAL_FADUMP_VERSION, opal_fdm_active->version);
692 		pr_warn("WARNING: Kernel metadata format mismatch identified! Core file maybe corrupted..\n");
693 	}
694 
695 	/* Kernel regions not registered with f/w for MPIPL */
696 	if (opal_fdm_active->registered_regions == 0) {
697 		opal_fdm_active = NULL;
698 		return;
699 	}
700 
701 	ret = opal_mpipl_query_tag(OPAL_MPIPL_TAG_CPU, &addr);
702 	if (addr) {
703 		addr = be64_to_cpu(addr);
704 		pr_debug("CPU metadata addr: %llx\n", addr);
705 		opal_cpu_metadata = __va(addr);
706 	}
707 
708 	pr_info("Firmware-assisted dump is active.\n");
709 	fadump_conf->dump_active = 1;
710 	opal_fadump_get_config(fadump_conf, opal_fdm_active);
711 }
712 #endif /* !CONFIG_PRESERVE_FA_DUMP */
713