xref: /openbmc/linux/arch/powerpc/kernel/fadump.c (revision dfc53baa)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Firmware Assisted dump: A robust mechanism to get reliable kernel crash
4  * dump with assistance from firmware. This approach does not use kexec,
5  * instead firmware assists in booting the kdump kernel while preserving
6  * memory contents. The most of the code implementation has been adapted
7  * from phyp assisted dump implementation written by Linas Vepstas and
8  * Manish Ahuja
9  *
10  * Copyright 2011 IBM Corporation
11  * Author: Mahesh Salgaonkar <mahesh@linux.vnet.ibm.com>
12  */
13 
14 #undef DEBUG
15 #define pr_fmt(fmt) "fadump: " fmt
16 
17 #include <linux/string.h>
18 #include <linux/memblock.h>
19 #include <linux/delay.h>
20 #include <linux/seq_file.h>
21 #include <linux/crash_dump.h>
22 #include <linux/kobject.h>
23 #include <linux/sysfs.h>
24 #include <linux/slab.h>
25 #include <linux/cma.h>
26 #include <linux/hugetlb.h>
27 
28 #include <asm/debugfs.h>
29 #include <asm/page.h>
30 #include <asm/prom.h>
31 #include <asm/fadump.h>
32 #include <asm/fadump-internal.h>
33 #include <asm/setup.h>
34 
35 /*
36  * The CPU who acquired the lock to trigger the fadump crash should
37  * wait for other CPUs to enter.
38  *
39  * The timeout is in milliseconds.
40  */
41 #define CRASH_TIMEOUT		500
42 
43 static struct fw_dump fw_dump;
44 
45 static void __init fadump_reserve_crash_area(u64 base);
46 
47 struct kobject *fadump_kobj;
48 
49 #ifndef CONFIG_PRESERVE_FA_DUMP
50 
51 static atomic_t cpus_in_fadump;
52 static DEFINE_MUTEX(fadump_mutex);
53 
54 struct fadump_mrange_info crash_mrange_info = { "crash", NULL, 0, 0, 0, false };
55 
56 #define RESERVED_RNGS_SZ	16384 /* 16K - 128 entries */
57 #define RESERVED_RNGS_CNT	(RESERVED_RNGS_SZ / \
58 				 sizeof(struct fadump_memory_range))
59 static struct fadump_memory_range rngs[RESERVED_RNGS_CNT];
60 struct fadump_mrange_info reserved_mrange_info = { "reserved", rngs,
61 						   RESERVED_RNGS_SZ, 0,
62 						   RESERVED_RNGS_CNT, true };
63 
64 static void __init early_init_dt_scan_reserved_ranges(unsigned long node);
65 
66 #ifdef CONFIG_CMA
67 static struct cma *fadump_cma;
68 
69 /*
70  * fadump_cma_init() - Initialize CMA area from a fadump reserved memory
71  *
72  * This function initializes CMA area from fadump reserved memory.
73  * The total size of fadump reserved memory covers for boot memory size
74  * + cpu data size + hpte size and metadata.
75  * Initialize only the area equivalent to boot memory size for CMA use.
76  * The reamining portion of fadump reserved memory will be not given
77  * to CMA and pages for thoes will stay reserved. boot memory size is
78  * aligned per CMA requirement to satisy cma_init_reserved_mem() call.
79  * But for some reason even if it fails we still have the memory reservation
80  * with us and we can still continue doing fadump.
81  */
82 int __init fadump_cma_init(void)
83 {
84 	unsigned long long base, size;
85 	int rc;
86 
87 	if (!fw_dump.fadump_enabled)
88 		return 0;
89 
90 	/*
91 	 * Do not use CMA if user has provided fadump=nocma kernel parameter.
92 	 * Return 1 to continue with fadump old behaviour.
93 	 */
94 	if (fw_dump.nocma)
95 		return 1;
96 
97 	base = fw_dump.reserve_dump_area_start;
98 	size = fw_dump.boot_memory_size;
99 
100 	if (!size)
101 		return 0;
102 
103 	rc = cma_init_reserved_mem(base, size, 0, "fadump_cma", &fadump_cma);
104 	if (rc) {
105 		pr_err("Failed to init cma area for firmware-assisted dump,%d\n", rc);
106 		/*
107 		 * Though the CMA init has failed we still have memory
108 		 * reservation with us. The reserved memory will be
109 		 * blocked from production system usage.  Hence return 1,
110 		 * so that we can continue with fadump.
111 		 */
112 		return 1;
113 	}
114 
115 	/*
116 	 * So we now have successfully initialized cma area for fadump.
117 	 */
118 	pr_info("Initialized 0x%lx bytes cma area at %ldMB from 0x%lx "
119 		"bytes of memory reserved for firmware-assisted dump\n",
120 		cma_get_size(fadump_cma),
121 		(unsigned long)cma_get_base(fadump_cma) >> 20,
122 		fw_dump.reserve_dump_area_size);
123 	return 1;
124 }
125 #else
126 static int __init fadump_cma_init(void) { return 1; }
127 #endif /* CONFIG_CMA */
128 
129 /* Scan the Firmware Assisted dump configuration details. */
130 int __init early_init_dt_scan_fw_dump(unsigned long node, const char *uname,
131 				      int depth, void *data)
132 {
133 	if (depth == 0) {
134 		early_init_dt_scan_reserved_ranges(node);
135 		return 0;
136 	}
137 
138 	if (depth != 1)
139 		return 0;
140 
141 	if (strcmp(uname, "rtas") == 0) {
142 		rtas_fadump_dt_scan(&fw_dump, node);
143 		return 1;
144 	}
145 
146 	if (strcmp(uname, "ibm,opal") == 0) {
147 		opal_fadump_dt_scan(&fw_dump, node);
148 		return 1;
149 	}
150 
151 	return 0;
152 }
153 
154 /*
155  * If fadump is registered, check if the memory provided
156  * falls within boot memory area and reserved memory area.
157  */
158 int is_fadump_memory_area(u64 addr, unsigned long size)
159 {
160 	u64 d_start, d_end;
161 
162 	if (!fw_dump.dump_registered)
163 		return 0;
164 
165 	if (!size)
166 		return 0;
167 
168 	d_start = fw_dump.reserve_dump_area_start;
169 	d_end = d_start + fw_dump.reserve_dump_area_size;
170 	if (((addr + size) > d_start) && (addr <= d_end))
171 		return 1;
172 
173 	return (addr <= fw_dump.boot_mem_top);
174 }
175 
176 int should_fadump_crash(void)
177 {
178 	if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr)
179 		return 0;
180 	return 1;
181 }
182 
183 int is_fadump_active(void)
184 {
185 	return fw_dump.dump_active;
186 }
187 
188 /*
189  * Returns true, if there are no holes in memory area between d_start to d_end,
190  * false otherwise.
191  */
192 static bool is_fadump_mem_area_contiguous(u64 d_start, u64 d_end)
193 {
194 	struct memblock_region *reg;
195 	bool ret = false;
196 	u64 start, end;
197 
198 	for_each_memblock(memory, reg) {
199 		start = max_t(u64, d_start, reg->base);
200 		end = min_t(u64, d_end, (reg->base + reg->size));
201 		if (d_start < end) {
202 			/* Memory hole from d_start to start */
203 			if (start > d_start)
204 				break;
205 
206 			if (end == d_end) {
207 				ret = true;
208 				break;
209 			}
210 
211 			d_start = end + 1;
212 		}
213 	}
214 
215 	return ret;
216 }
217 
218 /*
219  * Returns true, if there are no holes in boot memory area,
220  * false otherwise.
221  */
222 bool is_fadump_boot_mem_contiguous(void)
223 {
224 	unsigned long d_start, d_end;
225 	bool ret = false;
226 	int i;
227 
228 	for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) {
229 		d_start = fw_dump.boot_mem_addr[i];
230 		d_end   = d_start + fw_dump.boot_mem_sz[i];
231 
232 		ret = is_fadump_mem_area_contiguous(d_start, d_end);
233 		if (!ret)
234 			break;
235 	}
236 
237 	return ret;
238 }
239 
240 /*
241  * Returns true, if there are no holes in reserved memory area,
242  * false otherwise.
243  */
244 bool is_fadump_reserved_mem_contiguous(void)
245 {
246 	u64 d_start, d_end;
247 
248 	d_start	= fw_dump.reserve_dump_area_start;
249 	d_end	= d_start + fw_dump.reserve_dump_area_size;
250 	return is_fadump_mem_area_contiguous(d_start, d_end);
251 }
252 
253 /* Print firmware assisted dump configurations for debugging purpose. */
254 static void fadump_show_config(void)
255 {
256 	int i;
257 
258 	pr_debug("Support for firmware-assisted dump (fadump): %s\n",
259 			(fw_dump.fadump_supported ? "present" : "no support"));
260 
261 	if (!fw_dump.fadump_supported)
262 		return;
263 
264 	pr_debug("Fadump enabled    : %s\n",
265 				(fw_dump.fadump_enabled ? "yes" : "no"));
266 	pr_debug("Dump Active       : %s\n",
267 				(fw_dump.dump_active ? "yes" : "no"));
268 	pr_debug("Dump section sizes:\n");
269 	pr_debug("    CPU state data size: %lx\n", fw_dump.cpu_state_data_size);
270 	pr_debug("    HPTE region size   : %lx\n", fw_dump.hpte_region_size);
271 	pr_debug("    Boot memory size   : %lx\n", fw_dump.boot_memory_size);
272 	pr_debug("    Boot memory top    : %llx\n", fw_dump.boot_mem_top);
273 	pr_debug("Boot memory regions cnt: %llx\n", fw_dump.boot_mem_regs_cnt);
274 	for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) {
275 		pr_debug("[%03d] base = %llx, size = %llx\n", i,
276 			 fw_dump.boot_mem_addr[i], fw_dump.boot_mem_sz[i]);
277 	}
278 }
279 
280 /**
281  * fadump_calculate_reserve_size(): reserve variable boot area 5% of System RAM
282  *
283  * Function to find the largest memory size we need to reserve during early
284  * boot process. This will be the size of the memory that is required for a
285  * kernel to boot successfully.
286  *
287  * This function has been taken from phyp-assisted dump feature implementation.
288  *
289  * returns larger of 256MB or 5% rounded down to multiples of 256MB.
290  *
291  * TODO: Come up with better approach to find out more accurate memory size
292  * that is required for a kernel to boot successfully.
293  *
294  */
295 static inline u64 fadump_calculate_reserve_size(void)
296 {
297 	u64 base, size, bootmem_min;
298 	int ret;
299 
300 	if (fw_dump.reserve_bootvar)
301 		pr_warn("'fadump_reserve_mem=' parameter is deprecated in favor of 'crashkernel=' parameter.\n");
302 
303 	/*
304 	 * Check if the size is specified through crashkernel= cmdline
305 	 * option. If yes, then use that but ignore base as fadump reserves
306 	 * memory at a predefined offset.
307 	 */
308 	ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
309 				&size, &base);
310 	if (ret == 0 && size > 0) {
311 		unsigned long max_size;
312 
313 		if (fw_dump.reserve_bootvar)
314 			pr_info("Using 'crashkernel=' parameter for memory reservation.\n");
315 
316 		fw_dump.reserve_bootvar = (unsigned long)size;
317 
318 		/*
319 		 * Adjust if the boot memory size specified is above
320 		 * the upper limit.
321 		 */
322 		max_size = memblock_phys_mem_size() / MAX_BOOT_MEM_RATIO;
323 		if (fw_dump.reserve_bootvar > max_size) {
324 			fw_dump.reserve_bootvar = max_size;
325 			pr_info("Adjusted boot memory size to %luMB\n",
326 				(fw_dump.reserve_bootvar >> 20));
327 		}
328 
329 		return fw_dump.reserve_bootvar;
330 	} else if (fw_dump.reserve_bootvar) {
331 		/*
332 		 * 'fadump_reserve_mem=' is being used to reserve memory
333 		 * for firmware-assisted dump.
334 		 */
335 		return fw_dump.reserve_bootvar;
336 	}
337 
338 	/* divide by 20 to get 5% of value */
339 	size = memblock_phys_mem_size() / 20;
340 
341 	/* round it down in multiples of 256 */
342 	size = size & ~0x0FFFFFFFUL;
343 
344 	/* Truncate to memory_limit. We don't want to over reserve the memory.*/
345 	if (memory_limit && size > memory_limit)
346 		size = memory_limit;
347 
348 	bootmem_min = fw_dump.ops->fadump_get_bootmem_min();
349 	return (size > bootmem_min ? size : bootmem_min);
350 }
351 
352 /*
353  * Calculate the total memory size required to be reserved for
354  * firmware-assisted dump registration.
355  */
356 static unsigned long get_fadump_area_size(void)
357 {
358 	unsigned long size = 0;
359 
360 	size += fw_dump.cpu_state_data_size;
361 	size += fw_dump.hpte_region_size;
362 	size += fw_dump.boot_memory_size;
363 	size += sizeof(struct fadump_crash_info_header);
364 	size += sizeof(struct elfhdr); /* ELF core header.*/
365 	size += sizeof(struct elf_phdr); /* place holder for cpu notes */
366 	/* Program headers for crash memory regions. */
367 	size += sizeof(struct elf_phdr) * (memblock_num_regions(memory) + 2);
368 
369 	size = PAGE_ALIGN(size);
370 
371 	/* This is to hold kernel metadata on platforms that support it */
372 	size += (fw_dump.ops->fadump_get_metadata_size ?
373 		 fw_dump.ops->fadump_get_metadata_size() : 0);
374 	return size;
375 }
376 
377 static int __init add_boot_mem_region(unsigned long rstart,
378 				      unsigned long rsize)
379 {
380 	int i = fw_dump.boot_mem_regs_cnt++;
381 
382 	if (fw_dump.boot_mem_regs_cnt > FADUMP_MAX_MEM_REGS) {
383 		fw_dump.boot_mem_regs_cnt = FADUMP_MAX_MEM_REGS;
384 		return 0;
385 	}
386 
387 	pr_debug("Added boot memory range[%d] [%#016lx-%#016lx)\n",
388 		 i, rstart, (rstart + rsize));
389 	fw_dump.boot_mem_addr[i] = rstart;
390 	fw_dump.boot_mem_sz[i] = rsize;
391 	return 1;
392 }
393 
394 /*
395  * Firmware usually has a hard limit on the data it can copy per region.
396  * Honour that by splitting a memory range into multiple regions.
397  */
398 static int __init add_boot_mem_regions(unsigned long mstart,
399 				       unsigned long msize)
400 {
401 	unsigned long rstart, rsize, max_size;
402 	int ret = 1;
403 
404 	rstart = mstart;
405 	max_size = fw_dump.max_copy_size ? fw_dump.max_copy_size : msize;
406 	while (msize) {
407 		if (msize > max_size)
408 			rsize = max_size;
409 		else
410 			rsize = msize;
411 
412 		ret = add_boot_mem_region(rstart, rsize);
413 		if (!ret)
414 			break;
415 
416 		msize -= rsize;
417 		rstart += rsize;
418 	}
419 
420 	return ret;
421 }
422 
423 static int __init fadump_get_boot_mem_regions(void)
424 {
425 	unsigned long base, size, cur_size, hole_size, last_end;
426 	unsigned long mem_size = fw_dump.boot_memory_size;
427 	struct memblock_region *reg;
428 	int ret = 1;
429 
430 	fw_dump.boot_mem_regs_cnt = 0;
431 
432 	last_end = 0;
433 	hole_size = 0;
434 	cur_size = 0;
435 	for_each_memblock(memory, reg) {
436 		base = reg->base;
437 		size = reg->size;
438 		hole_size += (base - last_end);
439 
440 		if ((cur_size + size) >= mem_size) {
441 			size = (mem_size - cur_size);
442 			ret = add_boot_mem_regions(base, size);
443 			break;
444 		}
445 
446 		mem_size -= size;
447 		cur_size += size;
448 		ret = add_boot_mem_regions(base, size);
449 		if (!ret)
450 			break;
451 
452 		last_end = base + size;
453 	}
454 	fw_dump.boot_mem_top = PAGE_ALIGN(fw_dump.boot_memory_size + hole_size);
455 
456 	return ret;
457 }
458 
459 /*
460  * Returns true, if the given range overlaps with reserved memory ranges
461  * starting at idx. Also, updates idx to index of overlapping memory range
462  * with the given memory range.
463  * False, otherwise.
464  */
465 static bool overlaps_reserved_ranges(u64 base, u64 end, int *idx)
466 {
467 	bool ret = false;
468 	int i;
469 
470 	for (i = *idx; i < reserved_mrange_info.mem_range_cnt; i++) {
471 		u64 rbase = reserved_mrange_info.mem_ranges[i].base;
472 		u64 rend = rbase + reserved_mrange_info.mem_ranges[i].size;
473 
474 		if (end <= rbase)
475 			break;
476 
477 		if ((end > rbase) &&  (base < rend)) {
478 			*idx = i;
479 			ret = true;
480 			break;
481 		}
482 	}
483 
484 	return ret;
485 }
486 
487 /*
488  * Locate a suitable memory area to reserve memory for FADump. While at it,
489  * lookup reserved-ranges & avoid overlap with them, as they are used by F/W.
490  */
491 static u64 __init fadump_locate_reserve_mem(u64 base, u64 size)
492 {
493 	struct fadump_memory_range *mrngs;
494 	phys_addr_t mstart, mend;
495 	int idx = 0;
496 	u64 i, ret = 0;
497 
498 	mrngs = reserved_mrange_info.mem_ranges;
499 	for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE,
500 				&mstart, &mend, NULL) {
501 		pr_debug("%llu) mstart: %llx, mend: %llx, base: %llx\n",
502 			 i, mstart, mend, base);
503 
504 		if (mstart > base)
505 			base = PAGE_ALIGN(mstart);
506 
507 		while ((mend > base) && ((mend - base) >= size)) {
508 			if (!overlaps_reserved_ranges(base, base+size, &idx)) {
509 				ret = base;
510 				goto out;
511 			}
512 
513 			base = mrngs[idx].base + mrngs[idx].size;
514 			base = PAGE_ALIGN(base);
515 		}
516 	}
517 
518 out:
519 	return ret;
520 }
521 
522 int __init fadump_reserve_mem(void)
523 {
524 	u64 base, size, mem_boundary, bootmem_min;
525 	int ret = 1;
526 
527 	if (!fw_dump.fadump_enabled)
528 		return 0;
529 
530 	if (!fw_dump.fadump_supported) {
531 		pr_info("Firmware-Assisted Dump is not supported on this hardware\n");
532 		goto error_out;
533 	}
534 
535 	/*
536 	 * Initialize boot memory size
537 	 * If dump is active then we have already calculated the size during
538 	 * first kernel.
539 	 */
540 	if (!fw_dump.dump_active) {
541 		fw_dump.boot_memory_size =
542 			PAGE_ALIGN(fadump_calculate_reserve_size());
543 #ifdef CONFIG_CMA
544 		if (!fw_dump.nocma) {
545 			fw_dump.boot_memory_size =
546 				ALIGN(fw_dump.boot_memory_size,
547 				      FADUMP_CMA_ALIGNMENT);
548 		}
549 #endif
550 
551 		bootmem_min = fw_dump.ops->fadump_get_bootmem_min();
552 		if (fw_dump.boot_memory_size < bootmem_min) {
553 			pr_err("Can't enable fadump with boot memory size (0x%lx) less than 0x%llx\n",
554 			       fw_dump.boot_memory_size, bootmem_min);
555 			goto error_out;
556 		}
557 
558 		if (!fadump_get_boot_mem_regions()) {
559 			pr_err("Too many holes in boot memory area to enable fadump\n");
560 			goto error_out;
561 		}
562 	}
563 
564 	/*
565 	 * Calculate the memory boundary.
566 	 * If memory_limit is less than actual memory boundary then reserve
567 	 * the memory for fadump beyond the memory_limit and adjust the
568 	 * memory_limit accordingly, so that the running kernel can run with
569 	 * specified memory_limit.
570 	 */
571 	if (memory_limit && memory_limit < memblock_end_of_DRAM()) {
572 		size = get_fadump_area_size();
573 		if ((memory_limit + size) < memblock_end_of_DRAM())
574 			memory_limit += size;
575 		else
576 			memory_limit = memblock_end_of_DRAM();
577 		printk(KERN_INFO "Adjusted memory_limit for firmware-assisted"
578 				" dump, now %#016llx\n", memory_limit);
579 	}
580 	if (memory_limit)
581 		mem_boundary = memory_limit;
582 	else
583 		mem_boundary = memblock_end_of_DRAM();
584 
585 	base = fw_dump.boot_mem_top;
586 	size = get_fadump_area_size();
587 	fw_dump.reserve_dump_area_size = size;
588 	if (fw_dump.dump_active) {
589 		pr_info("Firmware-assisted dump is active.\n");
590 
591 #ifdef CONFIG_HUGETLB_PAGE
592 		/*
593 		 * FADump capture kernel doesn't care much about hugepages.
594 		 * In fact, handling hugepages in capture kernel is asking for
595 		 * trouble. So, disable HugeTLB support when fadump is active.
596 		 */
597 		hugetlb_disabled = true;
598 #endif
599 		/*
600 		 * If last boot has crashed then reserve all the memory
601 		 * above boot memory size so that we don't touch it until
602 		 * dump is written to disk by userspace tool. This memory
603 		 * can be released for general use by invalidating fadump.
604 		 */
605 		fadump_reserve_crash_area(base);
606 
607 		pr_debug("fadumphdr_addr = %#016lx\n", fw_dump.fadumphdr_addr);
608 		pr_debug("Reserve dump area start address: 0x%lx\n",
609 			 fw_dump.reserve_dump_area_start);
610 	} else {
611 		/*
612 		 * Reserve memory at an offset closer to bottom of the RAM to
613 		 * minimize the impact of memory hot-remove operation.
614 		 */
615 		base = fadump_locate_reserve_mem(base, size);
616 
617 		if (!base || (base + size > mem_boundary)) {
618 			pr_err("Failed to find memory chunk for reservation!\n");
619 			goto error_out;
620 		}
621 		fw_dump.reserve_dump_area_start = base;
622 
623 		/*
624 		 * Calculate the kernel metadata address and register it with
625 		 * f/w if the platform supports.
626 		 */
627 		if (fw_dump.ops->fadump_setup_metadata &&
628 		    (fw_dump.ops->fadump_setup_metadata(&fw_dump) < 0))
629 			goto error_out;
630 
631 		if (memblock_reserve(base, size)) {
632 			pr_err("Failed to reserve memory!\n");
633 			goto error_out;
634 		}
635 
636 		pr_info("Reserved %lldMB of memory at %#016llx (System RAM: %lldMB)\n",
637 			(size >> 20), base, (memblock_phys_mem_size() >> 20));
638 
639 		ret = fadump_cma_init();
640 	}
641 
642 	return ret;
643 error_out:
644 	fw_dump.fadump_enabled = 0;
645 	return 0;
646 }
647 
648 /* Look for fadump= cmdline option. */
649 static int __init early_fadump_param(char *p)
650 {
651 	if (!p)
652 		return 1;
653 
654 	if (strncmp(p, "on", 2) == 0)
655 		fw_dump.fadump_enabled = 1;
656 	else if (strncmp(p, "off", 3) == 0)
657 		fw_dump.fadump_enabled = 0;
658 	else if (strncmp(p, "nocma", 5) == 0) {
659 		fw_dump.fadump_enabled = 1;
660 		fw_dump.nocma = 1;
661 	}
662 
663 	return 0;
664 }
665 early_param("fadump", early_fadump_param);
666 
667 /*
668  * Look for fadump_reserve_mem= cmdline option
669  * TODO: Remove references to 'fadump_reserve_mem=' parameter,
670  *       the sooner 'crashkernel=' parameter is accustomed to.
671  */
672 static int __init early_fadump_reserve_mem(char *p)
673 {
674 	if (p)
675 		fw_dump.reserve_bootvar = memparse(p, &p);
676 	return 0;
677 }
678 early_param("fadump_reserve_mem", early_fadump_reserve_mem);
679 
680 void crash_fadump(struct pt_regs *regs, const char *str)
681 {
682 	unsigned int msecs;
683 	struct fadump_crash_info_header *fdh = NULL;
684 	int old_cpu, this_cpu;
685 	/* Do not include first CPU */
686 	unsigned int ncpus = num_online_cpus() - 1;
687 
688 	if (!should_fadump_crash())
689 		return;
690 
691 	/*
692 	 * old_cpu == -1 means this is the first CPU which has come here,
693 	 * go ahead and trigger fadump.
694 	 *
695 	 * old_cpu != -1 means some other CPU has already on it's way
696 	 * to trigger fadump, just keep looping here.
697 	 */
698 	this_cpu = smp_processor_id();
699 	old_cpu = cmpxchg(&crashing_cpu, -1, this_cpu);
700 
701 	if (old_cpu != -1) {
702 		atomic_inc(&cpus_in_fadump);
703 
704 		/*
705 		 * We can't loop here indefinitely. Wait as long as fadump
706 		 * is in force. If we race with fadump un-registration this
707 		 * loop will break and then we go down to normal panic path
708 		 * and reboot. If fadump is in force the first crashing
709 		 * cpu will definitely trigger fadump.
710 		 */
711 		while (fw_dump.dump_registered)
712 			cpu_relax();
713 		return;
714 	}
715 
716 	fdh = __va(fw_dump.fadumphdr_addr);
717 	fdh->crashing_cpu = crashing_cpu;
718 	crash_save_vmcoreinfo();
719 
720 	if (regs)
721 		fdh->regs = *regs;
722 	else
723 		ppc_save_regs(&fdh->regs);
724 
725 	fdh->online_mask = *cpu_online_mask;
726 
727 	/*
728 	 * If we came in via system reset, wait a while for the secondary
729 	 * CPUs to enter.
730 	 */
731 	if (TRAP(&(fdh->regs)) == 0x100) {
732 		msecs = CRASH_TIMEOUT;
733 		while ((atomic_read(&cpus_in_fadump) < ncpus) && (--msecs > 0))
734 			mdelay(1);
735 	}
736 
737 	fw_dump.ops->fadump_trigger(fdh, str);
738 }
739 
740 u32 *fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs)
741 {
742 	struct elf_prstatus prstatus;
743 
744 	memset(&prstatus, 0, sizeof(prstatus));
745 	/*
746 	 * FIXME: How do i get PID? Do I really need it?
747 	 * prstatus.pr_pid = ????
748 	 */
749 	elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
750 	buf = append_elf_note(buf, CRASH_CORE_NOTE_NAME, NT_PRSTATUS,
751 			      &prstatus, sizeof(prstatus));
752 	return buf;
753 }
754 
755 void fadump_update_elfcore_header(char *bufp)
756 {
757 	struct elfhdr *elf;
758 	struct elf_phdr *phdr;
759 
760 	elf = (struct elfhdr *)bufp;
761 	bufp += sizeof(struct elfhdr);
762 
763 	/* First note is a place holder for cpu notes info. */
764 	phdr = (struct elf_phdr *)bufp;
765 
766 	if (phdr->p_type == PT_NOTE) {
767 		phdr->p_paddr	= __pa(fw_dump.cpu_notes_buf_vaddr);
768 		phdr->p_offset	= phdr->p_paddr;
769 		phdr->p_filesz	= fw_dump.cpu_notes_buf_size;
770 		phdr->p_memsz = fw_dump.cpu_notes_buf_size;
771 	}
772 	return;
773 }
774 
775 static void *fadump_alloc_buffer(unsigned long size)
776 {
777 	unsigned long count, i;
778 	struct page *page;
779 	void *vaddr;
780 
781 	vaddr = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
782 	if (!vaddr)
783 		return NULL;
784 
785 	count = PAGE_ALIGN(size) / PAGE_SIZE;
786 	page = virt_to_page(vaddr);
787 	for (i = 0; i < count; i++)
788 		mark_page_reserved(page + i);
789 	return vaddr;
790 }
791 
792 static void fadump_free_buffer(unsigned long vaddr, unsigned long size)
793 {
794 	free_reserved_area((void *)vaddr, (void *)(vaddr + size), -1, NULL);
795 }
796 
797 s32 fadump_setup_cpu_notes_buf(u32 num_cpus)
798 {
799 	/* Allocate buffer to hold cpu crash notes. */
800 	fw_dump.cpu_notes_buf_size = num_cpus * sizeof(note_buf_t);
801 	fw_dump.cpu_notes_buf_size = PAGE_ALIGN(fw_dump.cpu_notes_buf_size);
802 	fw_dump.cpu_notes_buf_vaddr =
803 		(unsigned long)fadump_alloc_buffer(fw_dump.cpu_notes_buf_size);
804 	if (!fw_dump.cpu_notes_buf_vaddr) {
805 		pr_err("Failed to allocate %ld bytes for CPU notes buffer\n",
806 		       fw_dump.cpu_notes_buf_size);
807 		return -ENOMEM;
808 	}
809 
810 	pr_debug("Allocated buffer for cpu notes of size %ld at 0x%lx\n",
811 		 fw_dump.cpu_notes_buf_size,
812 		 fw_dump.cpu_notes_buf_vaddr);
813 	return 0;
814 }
815 
816 void fadump_free_cpu_notes_buf(void)
817 {
818 	if (!fw_dump.cpu_notes_buf_vaddr)
819 		return;
820 
821 	fadump_free_buffer(fw_dump.cpu_notes_buf_vaddr,
822 			   fw_dump.cpu_notes_buf_size);
823 	fw_dump.cpu_notes_buf_vaddr = 0;
824 	fw_dump.cpu_notes_buf_size = 0;
825 }
826 
827 static void fadump_free_mem_ranges(struct fadump_mrange_info *mrange_info)
828 {
829 	if (mrange_info->is_static) {
830 		mrange_info->mem_range_cnt = 0;
831 		return;
832 	}
833 
834 	kfree(mrange_info->mem_ranges);
835 	memset((void *)((u64)mrange_info + RNG_NAME_SZ), 0,
836 	       (sizeof(struct fadump_mrange_info) - RNG_NAME_SZ));
837 }
838 
839 /*
840  * Allocate or reallocate mem_ranges array in incremental units
841  * of PAGE_SIZE.
842  */
843 static int fadump_alloc_mem_ranges(struct fadump_mrange_info *mrange_info)
844 {
845 	struct fadump_memory_range *new_array;
846 	u64 new_size;
847 
848 	new_size = mrange_info->mem_ranges_sz + PAGE_SIZE;
849 	pr_debug("Allocating %llu bytes of memory for %s memory ranges\n",
850 		 new_size, mrange_info->name);
851 
852 	new_array = krealloc(mrange_info->mem_ranges, new_size, GFP_KERNEL);
853 	if (new_array == NULL) {
854 		pr_err("Insufficient memory for setting up %s memory ranges\n",
855 		       mrange_info->name);
856 		fadump_free_mem_ranges(mrange_info);
857 		return -ENOMEM;
858 	}
859 
860 	mrange_info->mem_ranges = new_array;
861 	mrange_info->mem_ranges_sz = new_size;
862 	mrange_info->max_mem_ranges = (new_size /
863 				       sizeof(struct fadump_memory_range));
864 	return 0;
865 }
866 
867 static inline int fadump_add_mem_range(struct fadump_mrange_info *mrange_info,
868 				       u64 base, u64 end)
869 {
870 	struct fadump_memory_range *mem_ranges = mrange_info->mem_ranges;
871 	bool is_adjacent = false;
872 	u64 start, size;
873 
874 	if (base == end)
875 		return 0;
876 
877 	/*
878 	 * Fold adjacent memory ranges to bring down the memory ranges/
879 	 * PT_LOAD segments count.
880 	 */
881 	if (mrange_info->mem_range_cnt) {
882 		start = mem_ranges[mrange_info->mem_range_cnt - 1].base;
883 		size  = mem_ranges[mrange_info->mem_range_cnt - 1].size;
884 
885 		if ((start + size) == base)
886 			is_adjacent = true;
887 	}
888 	if (!is_adjacent) {
889 		/* resize the array on reaching the limit */
890 		if (mrange_info->mem_range_cnt == mrange_info->max_mem_ranges) {
891 			int ret;
892 
893 			if (mrange_info->is_static) {
894 				pr_err("Reached array size limit for %s memory ranges\n",
895 				       mrange_info->name);
896 				return -ENOSPC;
897 			}
898 
899 			ret = fadump_alloc_mem_ranges(mrange_info);
900 			if (ret)
901 				return ret;
902 
903 			/* Update to the new resized array */
904 			mem_ranges = mrange_info->mem_ranges;
905 		}
906 
907 		start = base;
908 		mem_ranges[mrange_info->mem_range_cnt].base = start;
909 		mrange_info->mem_range_cnt++;
910 	}
911 
912 	mem_ranges[mrange_info->mem_range_cnt - 1].size = (end - start);
913 	pr_debug("%s_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n",
914 		 mrange_info->name, (mrange_info->mem_range_cnt - 1),
915 		 start, end - 1, (end - start));
916 	return 0;
917 }
918 
919 static int fadump_exclude_reserved_area(u64 start, u64 end)
920 {
921 	u64 ra_start, ra_end;
922 	int ret = 0;
923 
924 	ra_start = fw_dump.reserve_dump_area_start;
925 	ra_end = ra_start + fw_dump.reserve_dump_area_size;
926 
927 	if ((ra_start < end) && (ra_end > start)) {
928 		if ((start < ra_start) && (end > ra_end)) {
929 			ret = fadump_add_mem_range(&crash_mrange_info,
930 						   start, ra_start);
931 			if (ret)
932 				return ret;
933 
934 			ret = fadump_add_mem_range(&crash_mrange_info,
935 						   ra_end, end);
936 		} else if (start < ra_start) {
937 			ret = fadump_add_mem_range(&crash_mrange_info,
938 						   start, ra_start);
939 		} else if (ra_end < end) {
940 			ret = fadump_add_mem_range(&crash_mrange_info,
941 						   ra_end, end);
942 		}
943 	} else
944 		ret = fadump_add_mem_range(&crash_mrange_info, start, end);
945 
946 	return ret;
947 }
948 
949 static int fadump_init_elfcore_header(char *bufp)
950 {
951 	struct elfhdr *elf;
952 
953 	elf = (struct elfhdr *) bufp;
954 	bufp += sizeof(struct elfhdr);
955 	memcpy(elf->e_ident, ELFMAG, SELFMAG);
956 	elf->e_ident[EI_CLASS] = ELF_CLASS;
957 	elf->e_ident[EI_DATA] = ELF_DATA;
958 	elf->e_ident[EI_VERSION] = EV_CURRENT;
959 	elf->e_ident[EI_OSABI] = ELF_OSABI;
960 	memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
961 	elf->e_type = ET_CORE;
962 	elf->e_machine = ELF_ARCH;
963 	elf->e_version = EV_CURRENT;
964 	elf->e_entry = 0;
965 	elf->e_phoff = sizeof(struct elfhdr);
966 	elf->e_shoff = 0;
967 #if defined(_CALL_ELF)
968 	elf->e_flags = _CALL_ELF;
969 #else
970 	elf->e_flags = 0;
971 #endif
972 	elf->e_ehsize = sizeof(struct elfhdr);
973 	elf->e_phentsize = sizeof(struct elf_phdr);
974 	elf->e_phnum = 0;
975 	elf->e_shentsize = 0;
976 	elf->e_shnum = 0;
977 	elf->e_shstrndx = 0;
978 
979 	return 0;
980 }
981 
982 /*
983  * Traverse through memblock structure and setup crash memory ranges. These
984  * ranges will be used create PT_LOAD program headers in elfcore header.
985  */
986 static int fadump_setup_crash_memory_ranges(void)
987 {
988 	struct memblock_region *reg;
989 	u64 start, end;
990 	int i, ret;
991 
992 	pr_debug("Setup crash memory ranges.\n");
993 	crash_mrange_info.mem_range_cnt = 0;
994 
995 	/*
996 	 * Boot memory region(s) registered with firmware are moved to
997 	 * different location at the time of crash. Create separate program
998 	 * header(s) for this memory chunk(s) with the correct offset.
999 	 */
1000 	for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) {
1001 		start = fw_dump.boot_mem_addr[i];
1002 		end = start + fw_dump.boot_mem_sz[i];
1003 		ret = fadump_add_mem_range(&crash_mrange_info, start, end);
1004 		if (ret)
1005 			return ret;
1006 	}
1007 
1008 	for_each_memblock(memory, reg) {
1009 		start = (u64)reg->base;
1010 		end = start + (u64)reg->size;
1011 
1012 		/*
1013 		 * skip the memory chunk that is already added
1014 		 * (0 through boot_memory_top).
1015 		 */
1016 		if (start < fw_dump.boot_mem_top) {
1017 			if (end > fw_dump.boot_mem_top)
1018 				start = fw_dump.boot_mem_top;
1019 			else
1020 				continue;
1021 		}
1022 
1023 		/* add this range excluding the reserved dump area. */
1024 		ret = fadump_exclude_reserved_area(start, end);
1025 		if (ret)
1026 			return ret;
1027 	}
1028 
1029 	return 0;
1030 }
1031 
1032 /*
1033  * If the given physical address falls within the boot memory region then
1034  * return the relocated address that points to the dump region reserved
1035  * for saving initial boot memory contents.
1036  */
1037 static inline unsigned long fadump_relocate(unsigned long paddr)
1038 {
1039 	unsigned long raddr, rstart, rend, rlast, hole_size;
1040 	int i;
1041 
1042 	hole_size = 0;
1043 	rlast = 0;
1044 	raddr = paddr;
1045 	for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) {
1046 		rstart = fw_dump.boot_mem_addr[i];
1047 		rend = rstart + fw_dump.boot_mem_sz[i];
1048 		hole_size += (rstart - rlast);
1049 
1050 		if (paddr >= rstart && paddr < rend) {
1051 			raddr += fw_dump.boot_mem_dest_addr - hole_size;
1052 			break;
1053 		}
1054 
1055 		rlast = rend;
1056 	}
1057 
1058 	pr_debug("vmcoreinfo: paddr = 0x%lx, raddr = 0x%lx\n", paddr, raddr);
1059 	return raddr;
1060 }
1061 
1062 static int fadump_create_elfcore_headers(char *bufp)
1063 {
1064 	unsigned long long raddr, offset;
1065 	struct elf_phdr *phdr;
1066 	struct elfhdr *elf;
1067 	int i, j;
1068 
1069 	fadump_init_elfcore_header(bufp);
1070 	elf = (struct elfhdr *)bufp;
1071 	bufp += sizeof(struct elfhdr);
1072 
1073 	/*
1074 	 * setup ELF PT_NOTE, place holder for cpu notes info. The notes info
1075 	 * will be populated during second kernel boot after crash. Hence
1076 	 * this PT_NOTE will always be the first elf note.
1077 	 *
1078 	 * NOTE: Any new ELF note addition should be placed after this note.
1079 	 */
1080 	phdr = (struct elf_phdr *)bufp;
1081 	bufp += sizeof(struct elf_phdr);
1082 	phdr->p_type = PT_NOTE;
1083 	phdr->p_flags = 0;
1084 	phdr->p_vaddr = 0;
1085 	phdr->p_align = 0;
1086 
1087 	phdr->p_offset = 0;
1088 	phdr->p_paddr = 0;
1089 	phdr->p_filesz = 0;
1090 	phdr->p_memsz = 0;
1091 
1092 	(elf->e_phnum)++;
1093 
1094 	/* setup ELF PT_NOTE for vmcoreinfo */
1095 	phdr = (struct elf_phdr *)bufp;
1096 	bufp += sizeof(struct elf_phdr);
1097 	phdr->p_type	= PT_NOTE;
1098 	phdr->p_flags	= 0;
1099 	phdr->p_vaddr	= 0;
1100 	phdr->p_align	= 0;
1101 
1102 	phdr->p_paddr	= fadump_relocate(paddr_vmcoreinfo_note());
1103 	phdr->p_offset	= phdr->p_paddr;
1104 	phdr->p_memsz	= phdr->p_filesz = VMCOREINFO_NOTE_SIZE;
1105 
1106 	/* Increment number of program headers. */
1107 	(elf->e_phnum)++;
1108 
1109 	/* setup PT_LOAD sections. */
1110 	j = 0;
1111 	offset = 0;
1112 	raddr = fw_dump.boot_mem_addr[0];
1113 	for (i = 0; i < crash_mrange_info.mem_range_cnt; i++) {
1114 		u64 mbase, msize;
1115 
1116 		mbase = crash_mrange_info.mem_ranges[i].base;
1117 		msize = crash_mrange_info.mem_ranges[i].size;
1118 		if (!msize)
1119 			continue;
1120 
1121 		phdr = (struct elf_phdr *)bufp;
1122 		bufp += sizeof(struct elf_phdr);
1123 		phdr->p_type	= PT_LOAD;
1124 		phdr->p_flags	= PF_R|PF_W|PF_X;
1125 		phdr->p_offset	= mbase;
1126 
1127 		if (mbase == raddr) {
1128 			/*
1129 			 * The entire real memory region will be moved by
1130 			 * firmware to the specified destination_address.
1131 			 * Hence set the correct offset.
1132 			 */
1133 			phdr->p_offset = fw_dump.boot_mem_dest_addr + offset;
1134 			if (j < (fw_dump.boot_mem_regs_cnt - 1)) {
1135 				offset += fw_dump.boot_mem_sz[j];
1136 				raddr = fw_dump.boot_mem_addr[++j];
1137 			}
1138 		}
1139 
1140 		phdr->p_paddr = mbase;
1141 		phdr->p_vaddr = (unsigned long)__va(mbase);
1142 		phdr->p_filesz = msize;
1143 		phdr->p_memsz = msize;
1144 		phdr->p_align = 0;
1145 
1146 		/* Increment number of program headers. */
1147 		(elf->e_phnum)++;
1148 	}
1149 	return 0;
1150 }
1151 
1152 static unsigned long init_fadump_header(unsigned long addr)
1153 {
1154 	struct fadump_crash_info_header *fdh;
1155 
1156 	if (!addr)
1157 		return 0;
1158 
1159 	fdh = __va(addr);
1160 	addr += sizeof(struct fadump_crash_info_header);
1161 
1162 	memset(fdh, 0, sizeof(struct fadump_crash_info_header));
1163 	fdh->magic_number = FADUMP_CRASH_INFO_MAGIC;
1164 	fdh->elfcorehdr_addr = addr;
1165 	/* We will set the crashing cpu id in crash_fadump() during crash. */
1166 	fdh->crashing_cpu = FADUMP_CPU_UNKNOWN;
1167 
1168 	return addr;
1169 }
1170 
1171 static int register_fadump(void)
1172 {
1173 	unsigned long addr;
1174 	void *vaddr;
1175 	int ret;
1176 
1177 	/*
1178 	 * If no memory is reserved then we can not register for firmware-
1179 	 * assisted dump.
1180 	 */
1181 	if (!fw_dump.reserve_dump_area_size)
1182 		return -ENODEV;
1183 
1184 	ret = fadump_setup_crash_memory_ranges();
1185 	if (ret)
1186 		return ret;
1187 
1188 	addr = fw_dump.fadumphdr_addr;
1189 
1190 	/* Initialize fadump crash info header. */
1191 	addr = init_fadump_header(addr);
1192 	vaddr = __va(addr);
1193 
1194 	pr_debug("Creating ELF core headers at %#016lx\n", addr);
1195 	fadump_create_elfcore_headers(vaddr);
1196 
1197 	/* register the future kernel dump with firmware. */
1198 	pr_debug("Registering for firmware-assisted kernel dump...\n");
1199 	return fw_dump.ops->fadump_register(&fw_dump);
1200 }
1201 
1202 void fadump_cleanup(void)
1203 {
1204 	if (!fw_dump.fadump_supported)
1205 		return;
1206 
1207 	/* Invalidate the registration only if dump is active. */
1208 	if (fw_dump.dump_active) {
1209 		pr_debug("Invalidating firmware-assisted dump registration\n");
1210 		fw_dump.ops->fadump_invalidate(&fw_dump);
1211 	} else if (fw_dump.dump_registered) {
1212 		/* Un-register Firmware-assisted dump if it was registered. */
1213 		fw_dump.ops->fadump_unregister(&fw_dump);
1214 		fadump_free_mem_ranges(&crash_mrange_info);
1215 	}
1216 
1217 	if (fw_dump.ops->fadump_cleanup)
1218 		fw_dump.ops->fadump_cleanup(&fw_dump);
1219 }
1220 
1221 static void fadump_free_reserved_memory(unsigned long start_pfn,
1222 					unsigned long end_pfn)
1223 {
1224 	unsigned long pfn;
1225 	unsigned long time_limit = jiffies + HZ;
1226 
1227 	pr_info("freeing reserved memory (0x%llx - 0x%llx)\n",
1228 		PFN_PHYS(start_pfn), PFN_PHYS(end_pfn));
1229 
1230 	for (pfn = start_pfn; pfn < end_pfn; pfn++) {
1231 		free_reserved_page(pfn_to_page(pfn));
1232 
1233 		if (time_after(jiffies, time_limit)) {
1234 			cond_resched();
1235 			time_limit = jiffies + HZ;
1236 		}
1237 	}
1238 }
1239 
1240 /*
1241  * Skip memory holes and free memory that was actually reserved.
1242  */
1243 static void fadump_release_reserved_area(u64 start, u64 end)
1244 {
1245 	u64 tstart, tend, spfn, epfn;
1246 	struct memblock_region *reg;
1247 
1248 	spfn = PHYS_PFN(start);
1249 	epfn = PHYS_PFN(end);
1250 	for_each_memblock(memory, reg) {
1251 		tstart = max_t(u64, spfn, memblock_region_memory_base_pfn(reg));
1252 		tend   = min_t(u64, epfn, memblock_region_memory_end_pfn(reg));
1253 		if (tstart < tend) {
1254 			fadump_free_reserved_memory(tstart, tend);
1255 
1256 			if (tend == epfn)
1257 				break;
1258 
1259 			spfn = tend;
1260 		}
1261 	}
1262 }
1263 
1264 /*
1265  * Sort the mem ranges in-place and merge adjacent ranges
1266  * to minimize the memory ranges count.
1267  */
1268 static void sort_and_merge_mem_ranges(struct fadump_mrange_info *mrange_info)
1269 {
1270 	struct fadump_memory_range *mem_ranges;
1271 	struct fadump_memory_range tmp_range;
1272 	u64 base, size;
1273 	int i, j, idx;
1274 
1275 	if (!reserved_mrange_info.mem_range_cnt)
1276 		return;
1277 
1278 	/* Sort the memory ranges */
1279 	mem_ranges = mrange_info->mem_ranges;
1280 	for (i = 0; i < mrange_info->mem_range_cnt; i++) {
1281 		idx = i;
1282 		for (j = (i + 1); j < mrange_info->mem_range_cnt; j++) {
1283 			if (mem_ranges[idx].base > mem_ranges[j].base)
1284 				idx = j;
1285 		}
1286 		if (idx != i) {
1287 			tmp_range = mem_ranges[idx];
1288 			mem_ranges[idx] = mem_ranges[i];
1289 			mem_ranges[i] = tmp_range;
1290 		}
1291 	}
1292 
1293 	/* Merge adjacent reserved ranges */
1294 	idx = 0;
1295 	for (i = 1; i < mrange_info->mem_range_cnt; i++) {
1296 		base = mem_ranges[i-1].base;
1297 		size = mem_ranges[i-1].size;
1298 		if (mem_ranges[i].base == (base + size))
1299 			mem_ranges[idx].size += mem_ranges[i].size;
1300 		else {
1301 			idx++;
1302 			if (i == idx)
1303 				continue;
1304 
1305 			mem_ranges[idx] = mem_ranges[i];
1306 		}
1307 	}
1308 	mrange_info->mem_range_cnt = idx + 1;
1309 }
1310 
1311 /*
1312  * Scan reserved-ranges to consider them while reserving/releasing
1313  * memory for FADump.
1314  */
1315 static void __init early_init_dt_scan_reserved_ranges(unsigned long node)
1316 {
1317 	const __be32 *prop;
1318 	int len, ret = -1;
1319 	unsigned long i;
1320 
1321 	/* reserved-ranges already scanned */
1322 	if (reserved_mrange_info.mem_range_cnt != 0)
1323 		return;
1324 
1325 	prop = of_get_flat_dt_prop(node, "reserved-ranges", &len);
1326 	if (!prop)
1327 		return;
1328 
1329 	/*
1330 	 * Each reserved range is an (address,size) pair, 2 cells each,
1331 	 * totalling 4 cells per range.
1332 	 */
1333 	for (i = 0; i < len / (sizeof(*prop) * 4); i++) {
1334 		u64 base, size;
1335 
1336 		base = of_read_number(prop + (i * 4) + 0, 2);
1337 		size = of_read_number(prop + (i * 4) + 2, 2);
1338 
1339 		if (size) {
1340 			ret = fadump_add_mem_range(&reserved_mrange_info,
1341 						   base, base + size);
1342 			if (ret < 0) {
1343 				pr_warn("some reserved ranges are ignored!\n");
1344 				break;
1345 			}
1346 		}
1347 	}
1348 
1349 	/* Compact reserved ranges */
1350 	sort_and_merge_mem_ranges(&reserved_mrange_info);
1351 }
1352 
1353 /*
1354  * Release the memory that was reserved during early boot to preserve the
1355  * crash'ed kernel's memory contents except reserved dump area (permanent
1356  * reservation) and reserved ranges used by F/W. The released memory will
1357  * be available for general use.
1358  */
1359 static void fadump_release_memory(u64 begin, u64 end)
1360 {
1361 	u64 ra_start, ra_end, tstart;
1362 	int i, ret;
1363 
1364 	ra_start = fw_dump.reserve_dump_area_start;
1365 	ra_end = ra_start + fw_dump.reserve_dump_area_size;
1366 
1367 	/*
1368 	 * If reserved ranges array limit is hit, overwrite the last reserved
1369 	 * memory range with reserved dump area to ensure it is excluded from
1370 	 * the memory being released (reused for next FADump registration).
1371 	 */
1372 	if (reserved_mrange_info.mem_range_cnt ==
1373 	    reserved_mrange_info.max_mem_ranges)
1374 		reserved_mrange_info.mem_range_cnt--;
1375 
1376 	ret = fadump_add_mem_range(&reserved_mrange_info, ra_start, ra_end);
1377 	if (ret != 0)
1378 		return;
1379 
1380 	/* Get the reserved ranges list in order first. */
1381 	sort_and_merge_mem_ranges(&reserved_mrange_info);
1382 
1383 	/* Exclude reserved ranges and release remaining memory */
1384 	tstart = begin;
1385 	for (i = 0; i < reserved_mrange_info.mem_range_cnt; i++) {
1386 		ra_start = reserved_mrange_info.mem_ranges[i].base;
1387 		ra_end = ra_start + reserved_mrange_info.mem_ranges[i].size;
1388 
1389 		if (tstart >= ra_end)
1390 			continue;
1391 
1392 		if (tstart < ra_start)
1393 			fadump_release_reserved_area(tstart, ra_start);
1394 		tstart = ra_end;
1395 	}
1396 
1397 	if (tstart < end)
1398 		fadump_release_reserved_area(tstart, end);
1399 }
1400 
1401 static void fadump_invalidate_release_mem(void)
1402 {
1403 	mutex_lock(&fadump_mutex);
1404 	if (!fw_dump.dump_active) {
1405 		mutex_unlock(&fadump_mutex);
1406 		return;
1407 	}
1408 
1409 	fadump_cleanup();
1410 	mutex_unlock(&fadump_mutex);
1411 
1412 	fadump_release_memory(fw_dump.boot_mem_top, memblock_end_of_DRAM());
1413 	fadump_free_cpu_notes_buf();
1414 
1415 	/*
1416 	 * Setup kernel metadata and initialize the kernel dump
1417 	 * memory structure for FADump re-registration.
1418 	 */
1419 	if (fw_dump.ops->fadump_setup_metadata &&
1420 	    (fw_dump.ops->fadump_setup_metadata(&fw_dump) < 0))
1421 		pr_warn("Failed to setup kernel metadata!\n");
1422 	fw_dump.ops->fadump_init_mem_struct(&fw_dump);
1423 }
1424 
1425 static ssize_t release_mem_store(struct kobject *kobj,
1426 				 struct kobj_attribute *attr,
1427 				 const char *buf, size_t count)
1428 {
1429 	int input = -1;
1430 
1431 	if (!fw_dump.dump_active)
1432 		return -EPERM;
1433 
1434 	if (kstrtoint(buf, 0, &input))
1435 		return -EINVAL;
1436 
1437 	if (input == 1) {
1438 		/*
1439 		 * Take away the '/proc/vmcore'. We are releasing the dump
1440 		 * memory, hence it will not be valid anymore.
1441 		 */
1442 #ifdef CONFIG_PROC_VMCORE
1443 		vmcore_cleanup();
1444 #endif
1445 		fadump_invalidate_release_mem();
1446 
1447 	} else
1448 		return -EINVAL;
1449 	return count;
1450 }
1451 
1452 /* Release the reserved memory and disable the FADump */
1453 static void unregister_fadump(void)
1454 {
1455 	fadump_cleanup();
1456 	fadump_release_memory(fw_dump.reserve_dump_area_start,
1457 			      fw_dump.reserve_dump_area_size);
1458 	fw_dump.fadump_enabled = 0;
1459 	kobject_put(fadump_kobj);
1460 }
1461 
1462 static ssize_t enabled_show(struct kobject *kobj,
1463 			    struct kobj_attribute *attr,
1464 			    char *buf)
1465 {
1466 	return sprintf(buf, "%d\n", fw_dump.fadump_enabled);
1467 }
1468 
1469 static ssize_t mem_reserved_show(struct kobject *kobj,
1470 				 struct kobj_attribute *attr,
1471 				 char *buf)
1472 {
1473 	return sprintf(buf, "%ld\n", fw_dump.reserve_dump_area_size);
1474 }
1475 
1476 static ssize_t registered_show(struct kobject *kobj,
1477 			       struct kobj_attribute *attr,
1478 			       char *buf)
1479 {
1480 	return sprintf(buf, "%d\n", fw_dump.dump_registered);
1481 }
1482 
1483 static ssize_t registered_store(struct kobject *kobj,
1484 				struct kobj_attribute *attr,
1485 				const char *buf, size_t count)
1486 {
1487 	int ret = 0;
1488 	int input = -1;
1489 
1490 	if (!fw_dump.fadump_enabled || fw_dump.dump_active)
1491 		return -EPERM;
1492 
1493 	if (kstrtoint(buf, 0, &input))
1494 		return -EINVAL;
1495 
1496 	mutex_lock(&fadump_mutex);
1497 
1498 	switch (input) {
1499 	case 0:
1500 		if (fw_dump.dump_registered == 0) {
1501 			goto unlock_out;
1502 		}
1503 
1504 		/* Un-register Firmware-assisted dump */
1505 		pr_debug("Un-register firmware-assisted dump\n");
1506 		fw_dump.ops->fadump_unregister(&fw_dump);
1507 		break;
1508 	case 1:
1509 		if (fw_dump.dump_registered == 1) {
1510 			/* Un-register Firmware-assisted dump */
1511 			fw_dump.ops->fadump_unregister(&fw_dump);
1512 		}
1513 		/* Register Firmware-assisted dump */
1514 		ret = register_fadump();
1515 		break;
1516 	default:
1517 		ret = -EINVAL;
1518 		break;
1519 	}
1520 
1521 unlock_out:
1522 	mutex_unlock(&fadump_mutex);
1523 	return ret < 0 ? ret : count;
1524 }
1525 
1526 static int fadump_region_show(struct seq_file *m, void *private)
1527 {
1528 	if (!fw_dump.fadump_enabled)
1529 		return 0;
1530 
1531 	mutex_lock(&fadump_mutex);
1532 	fw_dump.ops->fadump_region_show(&fw_dump, m);
1533 	mutex_unlock(&fadump_mutex);
1534 	return 0;
1535 }
1536 
1537 static struct kobj_attribute release_attr = __ATTR_WO(release_mem);
1538 static struct kobj_attribute enable_attr = __ATTR_RO(enabled);
1539 static struct kobj_attribute register_attr = __ATTR_RW(registered);
1540 static struct kobj_attribute mem_reserved_attr = __ATTR_RO(mem_reserved);
1541 
1542 static struct attribute *fadump_attrs[] = {
1543 	&enable_attr.attr,
1544 	&register_attr.attr,
1545 	&mem_reserved_attr.attr,
1546 	NULL,
1547 };
1548 
1549 ATTRIBUTE_GROUPS(fadump);
1550 
1551 DEFINE_SHOW_ATTRIBUTE(fadump_region);
1552 
1553 static void fadump_init_files(void)
1554 {
1555 	int rc = 0;
1556 
1557 	fadump_kobj = kobject_create_and_add("fadump", kernel_kobj);
1558 	if (!fadump_kobj) {
1559 		pr_err("failed to create fadump kobject\n");
1560 		return;
1561 	}
1562 
1563 	debugfs_create_file("fadump_region", 0444, powerpc_debugfs_root, NULL,
1564 			    &fadump_region_fops);
1565 
1566 	if (fw_dump.dump_active) {
1567 		rc = sysfs_create_file(fadump_kobj, &release_attr.attr);
1568 		if (rc)
1569 			pr_err("unable to create release_mem sysfs file (%d)\n",
1570 			       rc);
1571 	}
1572 
1573 	rc = sysfs_create_groups(fadump_kobj, fadump_groups);
1574 	if (rc) {
1575 		pr_err("sysfs group creation failed (%d), unregistering FADump",
1576 		       rc);
1577 		unregister_fadump();
1578 		return;
1579 	}
1580 
1581 	/*
1582 	 * The FADump sysfs are moved from kernel_kobj to fadump_kobj need to
1583 	 * create symlink at old location to maintain backward compatibility.
1584 	 *
1585 	 *      - fadump_enabled -> fadump/enabled
1586 	 *      - fadump_registered -> fadump/registered
1587 	 *      - fadump_release_mem -> fadump/release_mem
1588 	 */
1589 	rc = compat_only_sysfs_link_entry_to_kobj(kernel_kobj, fadump_kobj,
1590 						  "enabled", "fadump_enabled");
1591 	if (rc) {
1592 		pr_err("unable to create fadump_enabled symlink (%d)", rc);
1593 		return;
1594 	}
1595 
1596 	rc = compat_only_sysfs_link_entry_to_kobj(kernel_kobj, fadump_kobj,
1597 						  "registered",
1598 						  "fadump_registered");
1599 	if (rc) {
1600 		pr_err("unable to create fadump_registered symlink (%d)", rc);
1601 		sysfs_remove_link(kernel_kobj, "fadump_enabled");
1602 		return;
1603 	}
1604 
1605 	if (fw_dump.dump_active) {
1606 		rc = compat_only_sysfs_link_entry_to_kobj(kernel_kobj,
1607 							  fadump_kobj,
1608 							  "release_mem",
1609 							  "fadump_release_mem");
1610 		if (rc)
1611 			pr_err("unable to create fadump_release_mem symlink (%d)",
1612 			       rc);
1613 	}
1614 	return;
1615 }
1616 
1617 /*
1618  * Prepare for firmware-assisted dump.
1619  */
1620 int __init setup_fadump(void)
1621 {
1622 	if (!fw_dump.fadump_supported)
1623 		return 0;
1624 
1625 	fadump_init_files();
1626 	fadump_show_config();
1627 
1628 	if (!fw_dump.fadump_enabled)
1629 		return 1;
1630 
1631 	/*
1632 	 * If dump data is available then see if it is valid and prepare for
1633 	 * saving it to the disk.
1634 	 */
1635 	if (fw_dump.dump_active) {
1636 		/*
1637 		 * if dump process fails then invalidate the registration
1638 		 * and release memory before proceeding for re-registration.
1639 		 */
1640 		if (fw_dump.ops->fadump_process(&fw_dump) < 0)
1641 			fadump_invalidate_release_mem();
1642 	}
1643 	/* Initialize the kernel dump memory structure for FAD registration. */
1644 	else if (fw_dump.reserve_dump_area_size)
1645 		fw_dump.ops->fadump_init_mem_struct(&fw_dump);
1646 
1647 	return 1;
1648 }
1649 subsys_initcall(setup_fadump);
1650 #else /* !CONFIG_PRESERVE_FA_DUMP */
1651 
1652 /* Scan the Firmware Assisted dump configuration details. */
1653 int __init early_init_dt_scan_fw_dump(unsigned long node, const char *uname,
1654 				      int depth, void *data)
1655 {
1656 	if ((depth != 1) || (strcmp(uname, "ibm,opal") != 0))
1657 		return 0;
1658 
1659 	opal_fadump_dt_scan(&fw_dump, node);
1660 	return 1;
1661 }
1662 
1663 /*
1664  * When dump is active but PRESERVE_FA_DUMP is enabled on the kernel,
1665  * preserve crash data. The subsequent memory preserving kernel boot
1666  * is likely to process this crash data.
1667  */
1668 int __init fadump_reserve_mem(void)
1669 {
1670 	if (fw_dump.dump_active) {
1671 		/*
1672 		 * If last boot has crashed then reserve all the memory
1673 		 * above boot memory to preserve crash data.
1674 		 */
1675 		pr_info("Preserving crash data for processing in next boot.\n");
1676 		fadump_reserve_crash_area(fw_dump.boot_mem_top);
1677 	} else
1678 		pr_debug("FADump-aware kernel..\n");
1679 
1680 	return 1;
1681 }
1682 #endif /* CONFIG_PRESERVE_FA_DUMP */
1683 
1684 /* Preserve everything above the base address */
1685 static void __init fadump_reserve_crash_area(u64 base)
1686 {
1687 	struct memblock_region *reg;
1688 	u64 mstart, msize;
1689 
1690 	for_each_memblock(memory, reg) {
1691 		mstart = reg->base;
1692 		msize  = reg->size;
1693 
1694 		if ((mstart + msize) < base)
1695 			continue;
1696 
1697 		if (mstart < base) {
1698 			msize -= (base - mstart);
1699 			mstart = base;
1700 		}
1701 
1702 		pr_info("Reserving %lluMB of memory at %#016llx for preserving crash data",
1703 			(msize >> 20), mstart);
1704 		memblock_reserve(mstart, msize);
1705 	}
1706 }
1707 
1708 unsigned long __init arch_reserved_kernel_pages(void)
1709 {
1710 	return memblock_reserved_size() / PAGE_SIZE;
1711 }
1712