blob: 63aac8b5f23326245c96dac57f9bd2a953f06e92 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +00002/*
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 *
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +000010 * 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>
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +000019#include <linux/delay.h>
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +000020#include <linux/seq_file.h>
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +000021#include <linux/crash_dump.h>
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +000022#include <linux/kobject.h>
23#include <linux/sysfs.h>
Hari Bathinia5818312018-08-18 02:10:56 +053024#include <linux/slab.h>
Mahesh Salgaonkara4e92ce2018-08-20 13:47:17 +053025#include <linux/cma.h>
Christophe Leroy45d0ba52019-04-26 05:59:47 +000026#include <linux/hugetlb.h>
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +000027
Michael Ellerman7644d582017-02-10 12:04:56 +110028#include <asm/debugfs.h>
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +000029#include <asm/page.h>
30#include <asm/prom.h>
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +000031#include <asm/fadump.h>
Hari Bathinica986d72019-09-11 20:16:21 +053032#include <asm/fadump-internal.h>
Stephen Rothwellcad3c832012-03-30 14:01:07 +000033#include <asm/setup.h>
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +000034
35static struct fw_dump fw_dump;
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +000036
Hari Bathinib2a815a2019-09-11 20:25:49 +053037static void __init fadump_reserve_crash_area(u64 base);
38
Sourabh Jaind418b192019-12-11 21:39:07 +053039struct kobject *fadump_kobj;
40
Hari Bathinibec53192019-09-11 20:26:03 +053041#ifndef CONFIG_PRESERVE_FA_DUMP
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +000042static DEFINE_MUTEX(fadump_mutex);
Hari Bathini02c04e32020-04-20 14:26:09 +053043struct fadump_mrange_info crash_mrange_info = { "crash", NULL, 0, 0, 0, false };
44
45#define RESERVED_RNGS_SZ 16384 /* 16K - 128 entries */
46#define RESERVED_RNGS_CNT (RESERVED_RNGS_SZ / \
47 sizeof(struct fadump_memory_range))
48static struct fadump_memory_range rngs[RESERVED_RNGS_CNT];
49struct fadump_mrange_info reserved_mrange_info = { "reserved", rngs,
50 RESERVED_RNGS_SZ, 0,
51 RESERVED_RNGS_CNT, true };
52
53static void __init early_init_dt_scan_reserved_ranges(unsigned long node);
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +000054
Mahesh Salgaonkara4e92ce2018-08-20 13:47:17 +053055#ifdef CONFIG_CMA
Hari Bathini0226e552019-09-11 20:18:14 +053056static struct cma *fadump_cma;
57
Mahesh Salgaonkara4e92ce2018-08-20 13:47:17 +053058/*
59 * fadump_cma_init() - Initialize CMA area from a fadump reserved memory
60 *
61 * This function initializes CMA area from fadump reserved memory.
62 * The total size of fadump reserved memory covers for boot memory size
63 * + cpu data size + hpte size and metadata.
64 * Initialize only the area equivalent to boot memory size for CMA use.
65 * The reamining portion of fadump reserved memory will be not given
66 * to CMA and pages for thoes will stay reserved. boot memory size is
67 * aligned per CMA requirement to satisy cma_init_reserved_mem() call.
68 * But for some reason even if it fails we still have the memory reservation
69 * with us and we can still continue doing fadump.
70 */
71int __init fadump_cma_init(void)
72{
73 unsigned long long base, size;
74 int rc;
75
76 if (!fw_dump.fadump_enabled)
77 return 0;
78
79 /*
80 * Do not use CMA if user has provided fadump=nocma kernel parameter.
81 * Return 1 to continue with fadump old behaviour.
82 */
83 if (fw_dump.nocma)
84 return 1;
85
86 base = fw_dump.reserve_dump_area_start;
87 size = fw_dump.boot_memory_size;
88
89 if (!size)
90 return 0;
91
92 rc = cma_init_reserved_mem(base, size, 0, "fadump_cma", &fadump_cma);
93 if (rc) {
94 pr_err("Failed to init cma area for firmware-assisted dump,%d\n", rc);
95 /*
96 * Though the CMA init has failed we still have memory
97 * reservation with us. The reserved memory will be
98 * blocked from production system usage. Hence return 1,
99 * so that we can continue with fadump.
100 */
101 return 1;
102 }
103
104 /*
105 * So we now have successfully initialized cma area for fadump.
106 */
107 pr_info("Initialized 0x%lx bytes cma area at %ldMB from 0x%lx "
108 "bytes of memory reserved for firmware-assisted dump\n",
109 cma_get_size(fadump_cma),
110 (unsigned long)cma_get_base(fadump_cma) >> 20,
111 fw_dump.reserve_dump_area_size);
112 return 1;
113}
114#else
115static int __init fadump_cma_init(void) { return 1; }
116#endif /* CONFIG_CMA */
117
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000118/* Scan the Firmware Assisted dump configuration details. */
Hari Bathinif3512012019-09-11 20:19:44 +0530119int __init early_init_dt_scan_fw_dump(unsigned long node, const char *uname,
120 int depth, void *data)
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000121{
Hari Bathini02c04e32020-04-20 14:26:09 +0530122 if (depth == 0) {
123 early_init_dt_scan_reserved_ranges(node);
124 return 0;
125 }
126
Hari Bathini41df5922019-09-11 20:20:26 +0530127 if (depth != 1)
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000128 return 0;
129
Hari Bathini41df5922019-09-11 20:20:26 +0530130 if (strcmp(uname, "rtas") == 0) {
131 rtas_fadump_dt_scan(&fw_dump, node);
132 return 1;
133 }
134
135 if (strcmp(uname, "ibm,opal") == 0) {
136 opal_fadump_dt_scan(&fw_dump, node);
137 return 1;
138 }
139
140 return 0;
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000141}
142
Hari Bathinieae0dfc2017-06-01 22:51:26 +0530143/*
144 * If fadump is registered, check if the memory provided
Mahesh Salgaonkar0db68962018-08-20 13:47:32 +0530145 * falls within boot memory area and reserved memory area.
Hari Bathinieae0dfc2017-06-01 22:51:26 +0530146 */
Hari Bathinibecd91d2019-09-11 20:27:26 +0530147int is_fadump_memory_area(u64 addr, unsigned long size)
Hari Bathinieae0dfc2017-06-01 22:51:26 +0530148{
Hari Bathinibecd91d2019-09-11 20:27:26 +0530149 u64 d_start, d_end;
Mahesh Salgaonkar0db68962018-08-20 13:47:32 +0530150
Hari Bathinieae0dfc2017-06-01 22:51:26 +0530151 if (!fw_dump.dump_registered)
152 return 0;
153
Hari Bathinibecd91d2019-09-11 20:27:26 +0530154 if (!size)
155 return 0;
156
157 d_start = fw_dump.reserve_dump_area_start;
158 d_end = d_start + fw_dump.reserve_dump_area_size;
Mahesh Salgaonkar0db68962018-08-20 13:47:32 +0530159 if (((addr + size) > d_start) && (addr <= d_end))
160 return 1;
161
Hari Bathini7dee93a2019-09-11 20:27:39 +0530162 return (addr <= fw_dump.boot_mem_top);
Hari Bathinieae0dfc2017-06-01 22:51:26 +0530163}
164
Nicholas Piggin6fcd6ba2017-07-19 16:59:11 +1000165int should_fadump_crash(void)
166{
167 if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr)
168 return 0;
169 return 1;
170}
171
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000172int is_fadump_active(void)
173{
174 return fw_dump.dump_active;
175}
176
Hari Bathinia5a05b92017-06-01 22:52:10 +0530177/*
Hari Bathini961cf262019-09-11 20:16:36 +0530178 * Returns true, if there are no holes in memory area between d_start to d_end,
179 * false otherwise.
Hari Bathinia5a05b92017-06-01 22:52:10 +0530180 */
Hari Bathini961cf262019-09-11 20:16:36 +0530181static bool is_fadump_mem_area_contiguous(u64 d_start, u64 d_end)
Hari Bathinia5a05b92017-06-01 22:52:10 +0530182{
183 struct memblock_region *reg;
Hari Bathini961cf262019-09-11 20:16:36 +0530184 bool ret = false;
185 u64 start, end;
Hari Bathinia5a05b92017-06-01 22:52:10 +0530186
187 for_each_memblock(memory, reg) {
Hari Bathini961cf262019-09-11 20:16:36 +0530188 start = max_t(u64, d_start, reg->base);
189 end = min_t(u64, d_end, (reg->base + reg->size));
190 if (d_start < end) {
191 /* Memory hole from d_start to start */
192 if (start > d_start)
Hari Bathinia5a05b92017-06-01 22:52:10 +0530193 break;
194
Hari Bathini961cf262019-09-11 20:16:36 +0530195 if (end == d_end) {
196 ret = true;
Hari Bathinia5a05b92017-06-01 22:52:10 +0530197 break;
198 }
199
Hari Bathini961cf262019-09-11 20:16:36 +0530200 d_start = end + 1;
Hari Bathinia5a05b92017-06-01 22:52:10 +0530201 }
202 }
203
204 return ret;
205}
206
Mahesh Salgaonkarf86593b2018-08-20 13:47:24 +0530207/*
Hari Bathini961cf262019-09-11 20:16:36 +0530208 * Returns true, if there are no holes in boot memory area,
209 * false otherwise.
210 */
Hari Bathini7f0ad112019-09-11 20:16:52 +0530211bool is_fadump_boot_mem_contiguous(void)
Hari Bathini961cf262019-09-11 20:16:36 +0530212{
Hari Bathini7dee93a2019-09-11 20:27:39 +0530213 unsigned long d_start, d_end;
214 bool ret = false;
215 int i;
216
217 for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) {
218 d_start = fw_dump.boot_mem_addr[i];
219 d_end = d_start + fw_dump.boot_mem_sz[i];
220
221 ret = is_fadump_mem_area_contiguous(d_start, d_end);
222 if (!ret)
223 break;
224 }
225
226 return ret;
Hari Bathini961cf262019-09-11 20:16:36 +0530227}
228
229/*
Mahesh Salgaonkarf86593b2018-08-20 13:47:24 +0530230 * Returns true, if there are no holes in reserved memory area,
231 * false otherwise.
232 */
Hari Bathini7f0ad112019-09-11 20:16:52 +0530233bool is_fadump_reserved_mem_contiguous(void)
Mahesh Salgaonkarf86593b2018-08-20 13:47:24 +0530234{
Hari Bathini961cf262019-09-11 20:16:36 +0530235 u64 d_start, d_end;
Mahesh Salgaonkarf86593b2018-08-20 13:47:24 +0530236
Hari Bathini961cf262019-09-11 20:16:36 +0530237 d_start = fw_dump.reserve_dump_area_start;
238 d_end = d_start + fw_dump.reserve_dump_area_size;
239 return is_fadump_mem_area_contiguous(d_start, d_end);
Mahesh Salgaonkarf86593b2018-08-20 13:47:24 +0530240}
241
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000242/* Print firmware assisted dump configurations for debugging purpose. */
243static void fadump_show_config(void)
244{
Hari Bathini7dee93a2019-09-11 20:27:39 +0530245 int i;
246
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000247 pr_debug("Support for firmware-assisted dump (fadump): %s\n",
248 (fw_dump.fadump_supported ? "present" : "no support"));
249
250 if (!fw_dump.fadump_supported)
251 return;
252
253 pr_debug("Fadump enabled : %s\n",
254 (fw_dump.fadump_enabled ? "yes" : "no"));
255 pr_debug("Dump Active : %s\n",
256 (fw_dump.dump_active ? "yes" : "no"));
257 pr_debug("Dump section sizes:\n");
258 pr_debug(" CPU state data size: %lx\n", fw_dump.cpu_state_data_size);
259 pr_debug(" HPTE region size : %lx\n", fw_dump.hpte_region_size);
Hari Bathini7dee93a2019-09-11 20:27:39 +0530260 pr_debug(" Boot memory size : %lx\n", fw_dump.boot_memory_size);
261 pr_debug(" Boot memory top : %llx\n", fw_dump.boot_mem_top);
262 pr_debug("Boot memory regions cnt: %llx\n", fw_dump.boot_mem_regs_cnt);
263 for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) {
264 pr_debug("[%03d] base = %llx, size = %llx\n", i,
265 fw_dump.boot_mem_addr[i], fw_dump.boot_mem_sz[i]);
266 }
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000267}
268
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000269/**
270 * fadump_calculate_reserve_size(): reserve variable boot area 5% of System RAM
271 *
272 * Function to find the largest memory size we need to reserve during early
273 * boot process. This will be the size of the memory that is required for a
274 * kernel to boot successfully.
275 *
276 * This function has been taken from phyp-assisted dump feature implementation.
277 *
278 * returns larger of 256MB or 5% rounded down to multiples of 256MB.
279 *
280 * TODO: Come up with better approach to find out more accurate memory size
281 * that is required for a kernel to boot successfully.
282 *
283 */
Hari Bathini7b1b3b42019-09-11 20:26:59 +0530284static inline u64 fadump_calculate_reserve_size(void)
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000285{
Hari Bathini7b1b3b42019-09-11 20:26:59 +0530286 u64 base, size, bootmem_min;
Hari Bathini11550dc2017-05-08 15:56:28 -0700287 int ret;
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000288
Hari Bathini81d9eca2017-05-22 15:04:23 +0530289 if (fw_dump.reserve_bootvar)
290 pr_warn("'fadump_reserve_mem=' parameter is deprecated in favor of 'crashkernel=' parameter.\n");
291
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000292 /*
Hari Bathini11550dc2017-05-08 15:56:28 -0700293 * Check if the size is specified through crashkernel= cmdline
Hari Bathinie7467dc2017-05-22 15:04:47 +0530294 * option. If yes, then use that but ignore base as fadump reserves
295 * memory at a predefined offset.
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000296 */
Hari Bathini11550dc2017-05-08 15:56:28 -0700297 ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
298 &size, &base);
299 if (ret == 0 && size > 0) {
Hari Bathini48a316e2017-06-02 13:00:27 +0530300 unsigned long max_size;
301
Hari Bathini81d9eca2017-05-22 15:04:23 +0530302 if (fw_dump.reserve_bootvar)
303 pr_info("Using 'crashkernel=' parameter for memory reservation.\n");
304
Hari Bathini11550dc2017-05-08 15:56:28 -0700305 fw_dump.reserve_bootvar = (unsigned long)size;
Hari Bathini48a316e2017-06-02 13:00:27 +0530306
307 /*
308 * Adjust if the boot memory size specified is above
309 * the upper limit.
310 */
311 max_size = memblock_phys_mem_size() / MAX_BOOT_MEM_RATIO;
312 if (fw_dump.reserve_bootvar > max_size) {
313 fw_dump.reserve_bootvar = max_size;
314 pr_info("Adjusted boot memory size to %luMB\n",
315 (fw_dump.reserve_bootvar >> 20));
316 }
317
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000318 return fw_dump.reserve_bootvar;
Hari Bathini81d9eca2017-05-22 15:04:23 +0530319 } else if (fw_dump.reserve_bootvar) {
320 /*
321 * 'fadump_reserve_mem=' is being used to reserve memory
322 * for firmware-assisted dump.
323 */
324 return fw_dump.reserve_bootvar;
Hari Bathini11550dc2017-05-08 15:56:28 -0700325 }
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000326
327 /* divide by 20 to get 5% of value */
Hari Bathini48a316e2017-06-02 13:00:27 +0530328 size = memblock_phys_mem_size() / 20;
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000329
330 /* round it down in multiples of 256 */
331 size = size & ~0x0FFFFFFFUL;
332
333 /* Truncate to memory_limit. We don't want to over reserve the memory.*/
334 if (memory_limit && size > memory_limit)
335 size = memory_limit;
336
Hari Bathini7b1b3b42019-09-11 20:26:59 +0530337 bootmem_min = fw_dump.ops->fadump_get_bootmem_min();
338 return (size > bootmem_min ? size : bootmem_min);
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000339}
340
341/*
342 * Calculate the total memory size required to be reserved for
343 * firmware-assisted dump registration.
344 */
345static unsigned long get_fadump_area_size(void)
346{
347 unsigned long size = 0;
348
349 size += fw_dump.cpu_state_data_size;
350 size += fw_dump.hpte_region_size;
351 size += fw_dump.boot_memory_size;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000352 size += sizeof(struct fadump_crash_info_header);
353 size += sizeof(struct elfhdr); /* ELF core header.*/
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000354 size += sizeof(struct elf_phdr); /* place holder for cpu notes */
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000355 /* Program headers for crash memory regions. */
356 size += sizeof(struct elf_phdr) * (memblock_num_regions(memory) + 2);
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000357
358 size = PAGE_ALIGN(size);
Hari Bathini742a2652019-09-11 20:20:57 +0530359
360 /* This is to hold kernel metadata on platforms that support it */
361 size += (fw_dump.ops->fadump_get_metadata_size ?
362 fw_dump.ops->fadump_get_metadata_size() : 0);
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000363 return size;
364}
365
Hari Bathini7dee93a2019-09-11 20:27:39 +0530366static int __init add_boot_mem_region(unsigned long rstart,
367 unsigned long rsize)
368{
369 int i = fw_dump.boot_mem_regs_cnt++;
370
371 if (fw_dump.boot_mem_regs_cnt > FADUMP_MAX_MEM_REGS) {
372 fw_dump.boot_mem_regs_cnt = FADUMP_MAX_MEM_REGS;
373 return 0;
374 }
375
376 pr_debug("Added boot memory range[%d] [%#016lx-%#016lx)\n",
377 i, rstart, (rstart + rsize));
378 fw_dump.boot_mem_addr[i] = rstart;
379 fw_dump.boot_mem_sz[i] = rsize;
380 return 1;
381}
382
383/*
384 * Firmware usually has a hard limit on the data it can copy per region.
385 * Honour that by splitting a memory range into multiple regions.
386 */
387static int __init add_boot_mem_regions(unsigned long mstart,
388 unsigned long msize)
389{
390 unsigned long rstart, rsize, max_size;
391 int ret = 1;
392
393 rstart = mstart;
394 max_size = fw_dump.max_copy_size ? fw_dump.max_copy_size : msize;
395 while (msize) {
396 if (msize > max_size)
397 rsize = max_size;
398 else
399 rsize = msize;
400
401 ret = add_boot_mem_region(rstart, rsize);
402 if (!ret)
403 break;
404
405 msize -= rsize;
406 rstart += rsize;
407 }
408
409 return ret;
410}
411
412static int __init fadump_get_boot_mem_regions(void)
413{
414 unsigned long base, size, cur_size, hole_size, last_end;
415 unsigned long mem_size = fw_dump.boot_memory_size;
416 struct memblock_region *reg;
417 int ret = 1;
418
419 fw_dump.boot_mem_regs_cnt = 0;
420
421 last_end = 0;
422 hole_size = 0;
423 cur_size = 0;
424 for_each_memblock(memory, reg) {
425 base = reg->base;
426 size = reg->size;
427 hole_size += (base - last_end);
428
429 if ((cur_size + size) >= mem_size) {
430 size = (mem_size - cur_size);
431 ret = add_boot_mem_regions(base, size);
432 break;
433 }
434
435 mem_size -= size;
436 cur_size += size;
437 ret = add_boot_mem_regions(base, size);
438 if (!ret)
439 break;
440
441 last_end = base + size;
442 }
443 fw_dump.boot_mem_top = PAGE_ALIGN(fw_dump.boot_memory_size + hole_size);
444
445 return ret;
446}
447
Hari Bathini140777a2020-04-20 14:26:22 +0530448/*
449 * Returns true, if the given range overlaps with reserved memory ranges
450 * starting at idx. Also, updates idx to index of overlapping memory range
451 * with the given memory range.
452 * False, otherwise.
453 */
454static bool overlaps_reserved_ranges(u64 base, u64 end, int *idx)
455{
456 bool ret = false;
457 int i;
458
459 for (i = *idx; i < reserved_mrange_info.mem_range_cnt; i++) {
460 u64 rbase = reserved_mrange_info.mem_ranges[i].base;
461 u64 rend = rbase + reserved_mrange_info.mem_ranges[i].size;
462
463 if (end <= rbase)
464 break;
465
466 if ((end > rbase) && (base < rend)) {
467 *idx = i;
468 ret = true;
469 break;
470 }
471 }
472
473 return ret;
474}
475
476/*
477 * Locate a suitable memory area to reserve memory for FADump. While at it,
478 * lookup reserved-ranges & avoid overlap with them, as they are used by F/W.
479 */
480static u64 __init fadump_locate_reserve_mem(u64 base, u64 size)
481{
482 struct fadump_memory_range *mrngs;
483 phys_addr_t mstart, mend;
484 int idx = 0;
485 u64 i, ret = 0;
486
487 mrngs = reserved_mrange_info.mem_ranges;
488 for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE,
489 &mstart, &mend, NULL) {
490 pr_debug("%llu) mstart: %llx, mend: %llx, base: %llx\n",
491 i, mstart, mend, base);
492
493 if (mstart > base)
494 base = PAGE_ALIGN(mstart);
495
496 while ((mend > base) && ((mend - base) >= size)) {
497 if (!overlaps_reserved_ranges(base, base+size, &idx)) {
498 ret = base;
499 goto out;
500 }
501
502 base = mrngs[idx].base + mrngs[idx].size;
503 base = PAGE_ALIGN(base);
504 }
505 }
506
507out:
508 return ret;
509}
510
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000511int __init fadump_reserve_mem(void)
512{
Hari Bathini140777a2020-04-20 14:26:22 +0530513 u64 base, size, mem_boundary, bootmem_min;
Hari Bathini6abec122019-09-11 20:20:41 +0530514 int ret = 1;
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000515
516 if (!fw_dump.fadump_enabled)
517 return 0;
518
519 if (!fw_dump.fadump_supported) {
Hari Bathini6abec122019-09-11 20:20:41 +0530520 pr_info("Firmware-Assisted Dump is not supported on this hardware\n");
521 goto error_out;
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000522 }
Hari Bathini742a2652019-09-11 20:20:57 +0530523
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000524 /*
525 * Initialize boot memory size
526 * If dump is active then we have already calculated the size during
527 * first kernel.
528 */
Hari Bathinif3512012019-09-11 20:19:44 +0530529 if (!fw_dump.dump_active) {
Hari Bathini6abec122019-09-11 20:20:41 +0530530 fw_dump.boot_memory_size =
531 PAGE_ALIGN(fadump_calculate_reserve_size());
Mahesh Salgaonkara4e92ce2018-08-20 13:47:17 +0530532#ifdef CONFIG_CMA
Hari Bathini579ca1a2019-09-11 20:24:28 +0530533 if (!fw_dump.nocma) {
Mahesh Salgaonkara4e92ce2018-08-20 13:47:17 +0530534 fw_dump.boot_memory_size =
Hari Bathini140777a2020-04-20 14:26:22 +0530535 ALIGN(fw_dump.boot_memory_size,
536 FADUMP_CMA_ALIGNMENT);
Hari Bathini579ca1a2019-09-11 20:24:28 +0530537 }
Mahesh Salgaonkara4e92ce2018-08-20 13:47:17 +0530538#endif
Hari Bathini7b1b3b42019-09-11 20:26:59 +0530539
540 bootmem_min = fw_dump.ops->fadump_get_bootmem_min();
541 if (fw_dump.boot_memory_size < bootmem_min) {
542 pr_err("Can't enable fadump with boot memory size (0x%lx) less than 0x%llx\n",
543 fw_dump.boot_memory_size, bootmem_min);
544 goto error_out;
545 }
Hari Bathini7dee93a2019-09-11 20:27:39 +0530546
547 if (!fadump_get_boot_mem_regions()) {
548 pr_err("Too many holes in boot memory area to enable fadump\n");
549 goto error_out;
550 }
Mahesh Salgaonkara4e92ce2018-08-20 13:47:17 +0530551 }
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000552
553 /*
554 * Calculate the memory boundary.
555 * If memory_limit is less than actual memory boundary then reserve
556 * the memory for fadump beyond the memory_limit and adjust the
557 * memory_limit accordingly, so that the running kernel can run with
558 * specified memory_limit.
559 */
560 if (memory_limit && memory_limit < memblock_end_of_DRAM()) {
561 size = get_fadump_area_size();
562 if ((memory_limit + size) < memblock_end_of_DRAM())
563 memory_limit += size;
564 else
565 memory_limit = memblock_end_of_DRAM();
566 printk(KERN_INFO "Adjusted memory_limit for firmware-assisted"
Suzuki Poulosea84fcd42012-08-21 01:42:33 +0000567 " dump, now %#016llx\n", memory_limit);
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000568 }
569 if (memory_limit)
Hari Bathini6abec122019-09-11 20:20:41 +0530570 mem_boundary = memory_limit;
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000571 else
Hari Bathini6abec122019-09-11 20:20:41 +0530572 mem_boundary = memblock_end_of_DRAM();
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000573
Hari Bathini7dee93a2019-09-11 20:27:39 +0530574 base = fw_dump.boot_mem_top;
Hari Bathini8255da92019-09-11 20:19:28 +0530575 size = get_fadump_area_size();
576 fw_dump.reserve_dump_area_size = size;
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000577 if (fw_dump.dump_active) {
Mahesh Salgaonkarb71a6932018-04-10 19:11:16 +0530578 pr_info("Firmware-assisted dump is active.\n");
579
Hari Bathini85975382018-04-10 19:11:31 +0530580#ifdef CONFIG_HUGETLB_PAGE
581 /*
582 * FADump capture kernel doesn't care much about hugepages.
583 * In fact, handling hugepages in capture kernel is asking for
584 * trouble. So, disable HugeTLB support when fadump is active.
585 */
586 hugetlb_disabled = true;
587#endif
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000588 /*
589 * If last boot has crashed then reserve all the memory
Hari Bathinib2a815a2019-09-11 20:25:49 +0530590 * above boot memory size so that we don't touch it until
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000591 * dump is written to disk by userspace tool. This memory
Hari Bathinib2a815a2019-09-11 20:25:49 +0530592 * can be released for general use by invalidating fadump.
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000593 */
Hari Bathinib2a815a2019-09-11 20:25:49 +0530594 fadump_reserve_crash_area(base);
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000595
Hari Bathinif3512012019-09-11 20:19:44 +0530596 pr_debug("fadumphdr_addr = %#016lx\n", fw_dump.fadumphdr_addr);
597 pr_debug("Reserve dump area start address: 0x%lx\n",
598 fw_dump.reserve_dump_area_start);
Hari Bathini8255da92019-09-11 20:19:28 +0530599 } else {
600 /*
Hari Bathinif6e6bed2017-03-17 02:35:26 +0530601 * Reserve memory at an offset closer to bottom of the RAM to
Hari Bathini579ca1a2019-09-11 20:24:28 +0530602 * minimize the impact of memory hot-remove operation.
Hari Bathinif6e6bed2017-03-17 02:35:26 +0530603 */
Hari Bathini140777a2020-04-20 14:26:22 +0530604 base = fadump_locate_reserve_mem(base, size);
Hari Bathinif6e6bed2017-03-17 02:35:26 +0530605
Hari Bathini579ca1a2019-09-11 20:24:28 +0530606 if (!base) {
Hari Bathini742a2652019-09-11 20:20:57 +0530607 pr_err("Failed to find memory chunk for reservation!\n");
608 goto error_out;
609 }
610 fw_dump.reserve_dump_area_start = base;
611
612 /*
613 * Calculate the kernel metadata address and register it with
614 * f/w if the platform supports.
615 */
616 if (fw_dump.ops->fadump_setup_metadata &&
617 (fw_dump.ops->fadump_setup_metadata(&fw_dump) < 0))
618 goto error_out;
619
620 if (memblock_reserve(base, size)) {
Hari Bathini6abec122019-09-11 20:20:41 +0530621 pr_err("Failed to reserve memory!\n");
622 goto error_out;
623 }
624
625 pr_info("Reserved %lldMB of memory at %#016llx (System RAM: %lldMB)\n",
626 (size >> 20), base, (memblock_phys_mem_size() >> 20));
Hari Bathinif6e6bed2017-03-17 02:35:26 +0530627
Hari Bathini6abec122019-09-11 20:20:41 +0530628 ret = fadump_cma_init();
Mahesh Salgaonkara4e92ce2018-08-20 13:47:17 +0530629 }
Hari Bathini6abec122019-09-11 20:20:41 +0530630
631 return ret;
632error_out:
633 fw_dump.fadump_enabled = 0;
634 return 0;
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000635}
636
637/* Look for fadump= cmdline option. */
638static int __init early_fadump_param(char *p)
639{
640 if (!p)
641 return 1;
642
643 if (strncmp(p, "on", 2) == 0)
644 fw_dump.fadump_enabled = 1;
645 else if (strncmp(p, "off", 3) == 0)
646 fw_dump.fadump_enabled = 0;
Mahesh Salgaonkara4e92ce2018-08-20 13:47:17 +0530647 else if (strncmp(p, "nocma", 5) == 0) {
648 fw_dump.fadump_enabled = 1;
649 fw_dump.nocma = 1;
650 }
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000651
652 return 0;
653}
654early_param("fadump", early_fadump_param);
655
Hari Bathini81d9eca2017-05-22 15:04:23 +0530656/*
657 * Look for fadump_reserve_mem= cmdline option
658 * TODO: Remove references to 'fadump_reserve_mem=' parameter,
659 * the sooner 'crashkernel=' parameter is accustomed to.
660 */
661static int __init early_fadump_reserve_mem(char *p)
662{
663 if (p)
664 fw_dump.reserve_bootvar = memparse(p, &p);
665 return 0;
666}
667early_param("fadump_reserve_mem", early_fadump_reserve_mem);
668
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000669void crash_fadump(struct pt_regs *regs, const char *str)
670{
671 struct fadump_crash_info_header *fdh = NULL;
Mahesh Salgaonkarf2a5e8f2016-10-24 23:51:51 +0530672 int old_cpu, this_cpu;
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000673
Nicholas Piggin6fcd6ba2017-07-19 16:59:11 +1000674 if (!should_fadump_crash())
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000675 return;
676
Mahesh Salgaonkarf2a5e8f2016-10-24 23:51:51 +0530677 /*
678 * old_cpu == -1 means this is the first CPU which has come here,
679 * go ahead and trigger fadump.
680 *
681 * old_cpu != -1 means some other CPU has already on it's way
682 * to trigger fadump, just keep looping here.
683 */
684 this_cpu = smp_processor_id();
685 old_cpu = cmpxchg(&crashing_cpu, -1, this_cpu);
686
687 if (old_cpu != -1) {
688 /*
689 * We can't loop here indefinitely. Wait as long as fadump
690 * is in force. If we race with fadump un-registration this
691 * loop will break and then we go down to normal panic path
692 * and reboot. If fadump is in force the first crashing
693 * cpu will definitely trigger fadump.
694 */
695 while (fw_dump.dump_registered)
696 cpu_relax();
697 return;
698 }
699
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000700 fdh = __va(fw_dump.fadumphdr_addr);
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000701 fdh->crashing_cpu = crashing_cpu;
702 crash_save_vmcoreinfo();
703
704 if (regs)
705 fdh->regs = *regs;
706 else
707 ppc_save_regs(&fdh->regs);
708
Rasmus Villemoesa0512162016-01-20 15:00:13 -0800709 fdh->online_mask = *cpu_online_mask;
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000710
Hari Bathini41a65d12019-09-11 20:18:57 +0530711 fw_dump.ops->fadump_trigger(fdh, str);
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000712}
713
Hari Bathini7f0ad112019-09-11 20:16:52 +0530714u32 *fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs)
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000715{
716 struct elf_prstatus prstatus;
717
718 memset(&prstatus, 0, sizeof(prstatus));
719 /*
720 * FIXME: How do i get PID? Do I really need it?
721 * prstatus.pr_pid = ????
722 */
723 elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
Hari Bathini22bd0172017-05-08 15:56:24 -0700724 buf = append_elf_note(buf, CRASH_CORE_NOTE_NAME, NT_PRSTATUS,
725 &prstatus, sizeof(prstatus));
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000726 return buf;
727}
728
Hari Bathini7f0ad112019-09-11 20:16:52 +0530729void fadump_update_elfcore_header(char *bufp)
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000730{
731 struct elfhdr *elf;
732 struct elf_phdr *phdr;
733
734 elf = (struct elfhdr *)bufp;
735 bufp += sizeof(struct elfhdr);
736
737 /* First note is a place holder for cpu notes info. */
738 phdr = (struct elf_phdr *)bufp;
739
740 if (phdr->p_type == PT_NOTE) {
Hari Bathini961cf262019-09-11 20:16:36 +0530741 phdr->p_paddr = __pa(fw_dump.cpu_notes_buf_vaddr);
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000742 phdr->p_offset = phdr->p_paddr;
743 phdr->p_filesz = fw_dump.cpu_notes_buf_size;
744 phdr->p_memsz = fw_dump.cpu_notes_buf_size;
745 }
746 return;
747}
748
Hari Bathini961cf262019-09-11 20:16:36 +0530749static void *fadump_alloc_buffer(unsigned long size)
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000750{
Hari Bathini72aa6512019-09-11 20:17:56 +0530751 unsigned long count, i;
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000752 struct page *page;
Hari Bathini72aa6512019-09-11 20:17:56 +0530753 void *vaddr;
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000754
Hari Bathini72aa6512019-09-11 20:17:56 +0530755 vaddr = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000756 if (!vaddr)
757 return NULL;
758
Hari Bathini72aa6512019-09-11 20:17:56 +0530759 count = PAGE_ALIGN(size) / PAGE_SIZE;
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000760 page = virt_to_page(vaddr);
761 for (i = 0; i < count; i++)
Hari Bathini72aa6512019-09-11 20:17:56 +0530762 mark_page_reserved(page + i);
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000763 return vaddr;
764}
765
Hari Bathini961cf262019-09-11 20:16:36 +0530766static void fadump_free_buffer(unsigned long vaddr, unsigned long size)
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000767{
Hari Bathini72aa6512019-09-11 20:17:56 +0530768 free_reserved_area((void *)vaddr, (void *)(vaddr + size), -1, NULL);
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000769}
770
Hari Bathini7f0ad112019-09-11 20:16:52 +0530771s32 fadump_setup_cpu_notes_buf(u32 num_cpus)
Hari Bathini961cf262019-09-11 20:16:36 +0530772{
773 /* Allocate buffer to hold cpu crash notes. */
774 fw_dump.cpu_notes_buf_size = num_cpus * sizeof(note_buf_t);
775 fw_dump.cpu_notes_buf_size = PAGE_ALIGN(fw_dump.cpu_notes_buf_size);
776 fw_dump.cpu_notes_buf_vaddr =
777 (unsigned long)fadump_alloc_buffer(fw_dump.cpu_notes_buf_size);
778 if (!fw_dump.cpu_notes_buf_vaddr) {
779 pr_err("Failed to allocate %ld bytes for CPU notes buffer\n",
780 fw_dump.cpu_notes_buf_size);
781 return -ENOMEM;
782 }
783
784 pr_debug("Allocated buffer for cpu notes of size %ld at 0x%lx\n",
785 fw_dump.cpu_notes_buf_size,
786 fw_dump.cpu_notes_buf_vaddr);
787 return 0;
788}
789
Hari Bathini7f0ad112019-09-11 20:16:52 +0530790void fadump_free_cpu_notes_buf(void)
Hari Bathini961cf262019-09-11 20:16:36 +0530791{
792 if (!fw_dump.cpu_notes_buf_vaddr)
793 return;
794
795 fadump_free_buffer(fw_dump.cpu_notes_buf_vaddr,
796 fw_dump.cpu_notes_buf_size);
797 fw_dump.cpu_notes_buf_vaddr = 0;
798 fw_dump.cpu_notes_buf_size = 0;
799}
800
Hari Bathinie4fc48f2019-09-11 20:25:05 +0530801static void fadump_free_mem_ranges(struct fadump_mrange_info *mrange_info)
Hari Bathini1bd6a1c2018-08-07 02:12:45 +0530802{
Hari Bathini02c04e32020-04-20 14:26:09 +0530803 if (mrange_info->is_static) {
804 mrange_info->mem_range_cnt = 0;
805 return;
806 }
807
Hari Bathinie4fc48f2019-09-11 20:25:05 +0530808 kfree(mrange_info->mem_ranges);
Hari Bathini02c04e32020-04-20 14:26:09 +0530809 memset((void *)((u64)mrange_info + RNG_NAME_SZ), 0,
810 (sizeof(struct fadump_mrange_info) - RNG_NAME_SZ));
Hari Bathini1bd6a1c2018-08-07 02:12:45 +0530811}
812
813/*
Hari Bathinie4fc48f2019-09-11 20:25:05 +0530814 * Allocate or reallocate mem_ranges array in incremental units
Hari Bathini1bd6a1c2018-08-07 02:12:45 +0530815 * of PAGE_SIZE.
816 */
Hari Bathinie4fc48f2019-09-11 20:25:05 +0530817static int fadump_alloc_mem_ranges(struct fadump_mrange_info *mrange_info)
Hari Bathini1bd6a1c2018-08-07 02:12:45 +0530818{
Hari Bathinie4fc48f2019-09-11 20:25:05 +0530819 struct fadump_memory_range *new_array;
Hari Bathini1bd6a1c2018-08-07 02:12:45 +0530820 u64 new_size;
821
Hari Bathinie4fc48f2019-09-11 20:25:05 +0530822 new_size = mrange_info->mem_ranges_sz + PAGE_SIZE;
823 pr_debug("Allocating %llu bytes of memory for %s memory ranges\n",
824 new_size, mrange_info->name);
Hari Bathini1bd6a1c2018-08-07 02:12:45 +0530825
Hari Bathinie4fc48f2019-09-11 20:25:05 +0530826 new_array = krealloc(mrange_info->mem_ranges, new_size, GFP_KERNEL);
Hari Bathini1bd6a1c2018-08-07 02:12:45 +0530827 if (new_array == NULL) {
Hari Bathinie4fc48f2019-09-11 20:25:05 +0530828 pr_err("Insufficient memory for setting up %s memory ranges\n",
829 mrange_info->name);
830 fadump_free_mem_ranges(mrange_info);
Hari Bathini1bd6a1c2018-08-07 02:12:45 +0530831 return -ENOMEM;
832 }
833
Hari Bathinie4fc48f2019-09-11 20:25:05 +0530834 mrange_info->mem_ranges = new_array;
835 mrange_info->mem_ranges_sz = new_size;
836 mrange_info->max_mem_ranges = (new_size /
837 sizeof(struct fadump_memory_range));
Hari Bathini1bd6a1c2018-08-07 02:12:45 +0530838 return 0;
839}
840
Hari Bathinie4fc48f2019-09-11 20:25:05 +0530841static inline int fadump_add_mem_range(struct fadump_mrange_info *mrange_info,
842 u64 base, u64 end)
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000843{
Hari Bathinie4fc48f2019-09-11 20:25:05 +0530844 struct fadump_memory_range *mem_ranges = mrange_info->mem_ranges;
Hari Bathiniced1bf52018-08-07 02:12:54 +0530845 bool is_adjacent = false;
Hari Bathinie4fc48f2019-09-11 20:25:05 +0530846 u64 start, size;
Hari Bathiniced1bf52018-08-07 02:12:54 +0530847
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000848 if (base == end)
Hari Bathini1bd6a1c2018-08-07 02:12:45 +0530849 return 0;
850
Hari Bathiniced1bf52018-08-07 02:12:54 +0530851 /*
852 * Fold adjacent memory ranges to bring down the memory ranges/
853 * PT_LOAD segments count.
854 */
Hari Bathinie4fc48f2019-09-11 20:25:05 +0530855 if (mrange_info->mem_range_cnt) {
856 start = mem_ranges[mrange_info->mem_range_cnt - 1].base;
857 size = mem_ranges[mrange_info->mem_range_cnt - 1].size;
Hari Bathini1bd6a1c2018-08-07 02:12:45 +0530858
Hari Bathiniced1bf52018-08-07 02:12:54 +0530859 if ((start + size) == base)
860 is_adjacent = true;
861 }
862 if (!is_adjacent) {
863 /* resize the array on reaching the limit */
Hari Bathinie4fc48f2019-09-11 20:25:05 +0530864 if (mrange_info->mem_range_cnt == mrange_info->max_mem_ranges) {
Hari Bathiniced1bf52018-08-07 02:12:54 +0530865 int ret;
866
Hari Bathini02c04e32020-04-20 14:26:09 +0530867 if (mrange_info->is_static) {
868 pr_err("Reached array size limit for %s memory ranges\n",
869 mrange_info->name);
870 return -ENOSPC;
871 }
872
Hari Bathinie4fc48f2019-09-11 20:25:05 +0530873 ret = fadump_alloc_mem_ranges(mrange_info);
Hari Bathiniced1bf52018-08-07 02:12:54 +0530874 if (ret)
875 return ret;
Hari Bathinie4fc48f2019-09-11 20:25:05 +0530876
877 /* Update to the new resized array */
878 mem_ranges = mrange_info->mem_ranges;
Hari Bathiniced1bf52018-08-07 02:12:54 +0530879 }
880
881 start = base;
Hari Bathinie4fc48f2019-09-11 20:25:05 +0530882 mem_ranges[mrange_info->mem_range_cnt].base = start;
883 mrange_info->mem_range_cnt++;
Hari Bathini1bd6a1c2018-08-07 02:12:45 +0530884 }
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000885
Hari Bathinie4fc48f2019-09-11 20:25:05 +0530886 mem_ranges[mrange_info->mem_range_cnt - 1].size = (end - start);
887 pr_debug("%s_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n",
888 mrange_info->name, (mrange_info->mem_range_cnt - 1),
889 start, end - 1, (end - start));
Hari Bathini1bd6a1c2018-08-07 02:12:45 +0530890 return 0;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000891}
892
Hari Bathinie4fc48f2019-09-11 20:25:05 +0530893static int fadump_exclude_reserved_area(u64 start, u64 end)
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000894{
Hari Bathinie4fc48f2019-09-11 20:25:05 +0530895 u64 ra_start, ra_end;
Hari Bathini1bd6a1c2018-08-07 02:12:45 +0530896 int ret = 0;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000897
898 ra_start = fw_dump.reserve_dump_area_start;
899 ra_end = ra_start + fw_dump.reserve_dump_area_size;
900
901 if ((ra_start < end) && (ra_end > start)) {
902 if ((start < ra_start) && (end > ra_end)) {
Hari Bathinie4fc48f2019-09-11 20:25:05 +0530903 ret = fadump_add_mem_range(&crash_mrange_info,
904 start, ra_start);
Hari Bathini1bd6a1c2018-08-07 02:12:45 +0530905 if (ret)
906 return ret;
907
Hari Bathinie4fc48f2019-09-11 20:25:05 +0530908 ret = fadump_add_mem_range(&crash_mrange_info,
909 ra_end, end);
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000910 } else if (start < ra_start) {
Hari Bathinie4fc48f2019-09-11 20:25:05 +0530911 ret = fadump_add_mem_range(&crash_mrange_info,
912 start, ra_start);
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000913 } else if (ra_end < end) {
Hari Bathinie4fc48f2019-09-11 20:25:05 +0530914 ret = fadump_add_mem_range(&crash_mrange_info,
915 ra_end, end);
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000916 }
917 } else
Hari Bathinie4fc48f2019-09-11 20:25:05 +0530918 ret = fadump_add_mem_range(&crash_mrange_info, start, end);
Hari Bathini1bd6a1c2018-08-07 02:12:45 +0530919
920 return ret;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000921}
922
923static int fadump_init_elfcore_header(char *bufp)
924{
925 struct elfhdr *elf;
926
927 elf = (struct elfhdr *) bufp;
928 bufp += sizeof(struct elfhdr);
929 memcpy(elf->e_ident, ELFMAG, SELFMAG);
930 elf->e_ident[EI_CLASS] = ELF_CLASS;
931 elf->e_ident[EI_DATA] = ELF_DATA;
932 elf->e_ident[EI_VERSION] = EV_CURRENT;
933 elf->e_ident[EI_OSABI] = ELF_OSABI;
934 memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
935 elf->e_type = ET_CORE;
936 elf->e_machine = ELF_ARCH;
937 elf->e_version = EV_CURRENT;
938 elf->e_entry = 0;
939 elf->e_phoff = sizeof(struct elfhdr);
940 elf->e_shoff = 0;
Daniel Axtensd8bced22016-09-06 15:32:42 +1000941#if defined(_CALL_ELF)
942 elf->e_flags = _CALL_ELF;
943#else
944 elf->e_flags = 0;
945#endif
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000946 elf->e_ehsize = sizeof(struct elfhdr);
947 elf->e_phentsize = sizeof(struct elf_phdr);
948 elf->e_phnum = 0;
949 elf->e_shentsize = 0;
950 elf->e_shnum = 0;
951 elf->e_shstrndx = 0;
952
953 return 0;
954}
955
956/*
957 * Traverse through memblock structure and setup crash memory ranges. These
958 * ranges will be used create PT_LOAD program headers in elfcore header.
959 */
Hari Bathini1bd6a1c2018-08-07 02:12:45 +0530960static int fadump_setup_crash_memory_ranges(void)
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000961{
962 struct memblock_region *reg;
Hari Bathinie4fc48f2019-09-11 20:25:05 +0530963 u64 start, end;
Hari Bathini7dee93a2019-09-11 20:27:39 +0530964 int i, ret;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000965
966 pr_debug("Setup crash memory ranges.\n");
Hari Bathinie4fc48f2019-09-11 20:25:05 +0530967 crash_mrange_info.mem_range_cnt = 0;
Hari Bathiniced1bf52018-08-07 02:12:54 +0530968
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000969 /*
Hari Bathini7dee93a2019-09-11 20:27:39 +0530970 * Boot memory region(s) registered with firmware are moved to
971 * different location at the time of crash. Create separate program
972 * header(s) for this memory chunk(s) with the correct offset.
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000973 */
Hari Bathini7dee93a2019-09-11 20:27:39 +0530974 for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) {
975 start = fw_dump.boot_mem_addr[i];
976 end = start + fw_dump.boot_mem_sz[i];
977 ret = fadump_add_mem_range(&crash_mrange_info, start, end);
978 if (ret)
979 return ret;
980 }
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000981
982 for_each_memblock(memory, reg) {
Hari Bathinie4fc48f2019-09-11 20:25:05 +0530983 start = (u64)reg->base;
984 end = start + (u64)reg->size;
Hari Bathinia77af5522017-06-01 22:50:38 +0530985
986 /*
Hari Bathini7dee93a2019-09-11 20:27:39 +0530987 * skip the memory chunk that is already added
988 * (0 through boot_memory_top).
Hari Bathinia77af5522017-06-01 22:50:38 +0530989 */
Hari Bathini7dee93a2019-09-11 20:27:39 +0530990 if (start < fw_dump.boot_mem_top) {
991 if (end > fw_dump.boot_mem_top)
992 start = fw_dump.boot_mem_top;
Hari Bathinia77af5522017-06-01 22:50:38 +0530993 else
994 continue;
995 }
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000996
997 /* add this range excluding the reserved dump area. */
Hari Bathini1bd6a1c2018-08-07 02:12:45 +0530998 ret = fadump_exclude_reserved_area(start, end);
999 if (ret)
1000 return ret;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001001 }
Hari Bathini1bd6a1c2018-08-07 02:12:45 +05301002
1003 return 0;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001004}
1005
Mahesh Salgaonkard34c5f22012-02-16 01:14:53 +00001006/*
1007 * If the given physical address falls within the boot memory region then
1008 * return the relocated address that points to the dump region reserved
1009 * for saving initial boot memory contents.
1010 */
1011static inline unsigned long fadump_relocate(unsigned long paddr)
1012{
Hari Bathini7dee93a2019-09-11 20:27:39 +05301013 unsigned long raddr, rstart, rend, rlast, hole_size;
1014 int i;
1015
1016 hole_size = 0;
1017 rlast = 0;
1018 raddr = paddr;
1019 for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) {
1020 rstart = fw_dump.boot_mem_addr[i];
1021 rend = rstart + fw_dump.boot_mem_sz[i];
1022 hole_size += (rstart - rlast);
1023
1024 if (paddr >= rstart && paddr < rend) {
1025 raddr += fw_dump.boot_mem_dest_addr - hole_size;
1026 break;
1027 }
1028
1029 rlast = rend;
1030 }
1031
1032 pr_debug("vmcoreinfo: paddr = 0x%lx, raddr = 0x%lx\n", paddr, raddr);
1033 return raddr;
Mahesh Salgaonkard34c5f22012-02-16 01:14:53 +00001034}
1035
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001036static int fadump_create_elfcore_headers(char *bufp)
1037{
Hari Bathini7dee93a2019-09-11 20:27:39 +05301038 unsigned long long raddr, offset;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001039 struct elf_phdr *phdr;
Hari Bathini7dee93a2019-09-11 20:27:39 +05301040 struct elfhdr *elf;
1041 int i, j;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001042
1043 fadump_init_elfcore_header(bufp);
1044 elf = (struct elfhdr *)bufp;
1045 bufp += sizeof(struct elfhdr);
1046
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +00001047 /*
1048 * setup ELF PT_NOTE, place holder for cpu notes info. The notes info
1049 * will be populated during second kernel boot after crash. Hence
1050 * this PT_NOTE will always be the first elf note.
1051 *
1052 * NOTE: Any new ELF note addition should be placed after this note.
1053 */
1054 phdr = (struct elf_phdr *)bufp;
1055 bufp += sizeof(struct elf_phdr);
1056 phdr->p_type = PT_NOTE;
1057 phdr->p_flags = 0;
1058 phdr->p_vaddr = 0;
1059 phdr->p_align = 0;
1060
1061 phdr->p_offset = 0;
1062 phdr->p_paddr = 0;
1063 phdr->p_filesz = 0;
1064 phdr->p_memsz = 0;
1065
1066 (elf->e_phnum)++;
1067
Mahesh Salgaonkard34c5f22012-02-16 01:14:53 +00001068 /* setup ELF PT_NOTE for vmcoreinfo */
1069 phdr = (struct elf_phdr *)bufp;
1070 bufp += sizeof(struct elf_phdr);
1071 phdr->p_type = PT_NOTE;
1072 phdr->p_flags = 0;
1073 phdr->p_vaddr = 0;
1074 phdr->p_align = 0;
1075
1076 phdr->p_paddr = fadump_relocate(paddr_vmcoreinfo_note());
1077 phdr->p_offset = phdr->p_paddr;
Xunlei Pang5203f492017-07-12 14:33:17 -07001078 phdr->p_memsz = phdr->p_filesz = VMCOREINFO_NOTE_SIZE;
Mahesh Salgaonkard34c5f22012-02-16 01:14:53 +00001079
1080 /* Increment number of program headers. */
1081 (elf->e_phnum)++;
1082
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001083 /* setup PT_LOAD sections. */
Hari Bathini7dee93a2019-09-11 20:27:39 +05301084 j = 0;
1085 offset = 0;
1086 raddr = fw_dump.boot_mem_addr[0];
Hari Bathinie4fc48f2019-09-11 20:25:05 +05301087 for (i = 0; i < crash_mrange_info.mem_range_cnt; i++) {
1088 u64 mbase, msize;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001089
Hari Bathinie4fc48f2019-09-11 20:25:05 +05301090 mbase = crash_mrange_info.mem_ranges[i].base;
1091 msize = crash_mrange_info.mem_ranges[i].size;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001092 if (!msize)
1093 continue;
1094
1095 phdr = (struct elf_phdr *)bufp;
1096 bufp += sizeof(struct elf_phdr);
1097 phdr->p_type = PT_LOAD;
1098 phdr->p_flags = PF_R|PF_W|PF_X;
1099 phdr->p_offset = mbase;
1100
Hari Bathini7dee93a2019-09-11 20:27:39 +05301101 if (mbase == raddr) {
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001102 /*
Hari Bathinibecd91d2019-09-11 20:27:26 +05301103 * The entire real memory region will be moved by
1104 * firmware to the specified destination_address.
1105 * Hence set the correct offset.
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001106 */
Hari Bathini7dee93a2019-09-11 20:27:39 +05301107 phdr->p_offset = fw_dump.boot_mem_dest_addr + offset;
1108 if (j < (fw_dump.boot_mem_regs_cnt - 1)) {
1109 offset += fw_dump.boot_mem_sz[j];
1110 raddr = fw_dump.boot_mem_addr[++j];
1111 }
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001112 }
1113
1114 phdr->p_paddr = mbase;
1115 phdr->p_vaddr = (unsigned long)__va(mbase);
1116 phdr->p_filesz = msize;
1117 phdr->p_memsz = msize;
1118 phdr->p_align = 0;
1119
1120 /* Increment number of program headers. */
1121 (elf->e_phnum)++;
1122 }
1123 return 0;
1124}
1125
1126static unsigned long init_fadump_header(unsigned long addr)
1127{
1128 struct fadump_crash_info_header *fdh;
1129
1130 if (!addr)
1131 return 0;
1132
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001133 fdh = __va(addr);
1134 addr += sizeof(struct fadump_crash_info_header);
1135
1136 memset(fdh, 0, sizeof(struct fadump_crash_info_header));
1137 fdh->magic_number = FADUMP_CRASH_INFO_MAGIC;
1138 fdh->elfcorehdr_addr = addr;
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +00001139 /* We will set the crashing cpu id in crash_fadump() during crash. */
Hari Bathini0226e552019-09-11 20:18:14 +05301140 fdh->crashing_cpu = FADUMP_CPU_UNKNOWN;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001141
1142 return addr;
1143}
1144
Michal Suchanek98b8cd72017-05-27 17:46:15 +02001145static int register_fadump(void)
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001146{
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001147 unsigned long addr;
1148 void *vaddr;
Hari Bathini1bd6a1c2018-08-07 02:12:45 +05301149 int ret;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001150
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001151 /*
1152 * If no memory is reserved then we can not register for firmware-
1153 * assisted dump.
1154 */
1155 if (!fw_dump.reserve_dump_area_size)
Michal Suchanek98b8cd72017-05-27 17:46:15 +02001156 return -ENODEV;
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001157
Hari Bathini1bd6a1c2018-08-07 02:12:45 +05301158 ret = fadump_setup_crash_memory_ranges();
1159 if (ret)
1160 return ret;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001161
Hari Bathini41a65d12019-09-11 20:18:57 +05301162 addr = fw_dump.fadumphdr_addr;
1163
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001164 /* Initialize fadump crash info header. */
1165 addr = init_fadump_header(addr);
1166 vaddr = __va(addr);
1167
1168 pr_debug("Creating ELF core headers at %#016lx\n", addr);
1169 fadump_create_elfcore_headers(vaddr);
1170
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001171 /* register the future kernel dump with firmware. */
Hari Bathini41a65d12019-09-11 20:18:57 +05301172 pr_debug("Registering for firmware-assisted kernel dump...\n");
1173 return fw_dump.ops->fadump_register(&fw_dump);
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001174}
1175
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001176void fadump_cleanup(void)
1177{
Hari Bathini2790d012019-09-11 20:21:16 +05301178 if (!fw_dump.fadump_supported)
1179 return;
1180
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001181 /* Invalidate the registration only if dump is active. */
1182 if (fw_dump.dump_active) {
Hari Bathinif3512012019-09-11 20:19:44 +05301183 pr_debug("Invalidating firmware-assisted dump registration\n");
1184 fw_dump.ops->fadump_invalidate(&fw_dump);
Mahesh Salgaonkar722cde72018-04-27 11:53:18 +05301185 } else if (fw_dump.dump_registered) {
1186 /* Un-register Firmware-assisted dump if it was registered. */
Hari Bathini41a65d12019-09-11 20:18:57 +05301187 fw_dump.ops->fadump_unregister(&fw_dump);
Hari Bathinie4fc48f2019-09-11 20:25:05 +05301188 fadump_free_mem_ranges(&crash_mrange_info);
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001189 }
Hari Bathini2790d012019-09-11 20:21:16 +05301190
1191 if (fw_dump.ops->fadump_cleanup)
1192 fw_dump.ops->fadump_cleanup(&fw_dump);
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001193}
1194
Hari Bathini68fa6472017-06-02 01:10:10 +05301195static void fadump_free_reserved_memory(unsigned long start_pfn,
1196 unsigned long end_pfn)
1197{
1198 unsigned long pfn;
1199 unsigned long time_limit = jiffies + HZ;
1200
1201 pr_info("freeing reserved memory (0x%llx - 0x%llx)\n",
1202 PFN_PHYS(start_pfn), PFN_PHYS(end_pfn));
1203
1204 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
1205 free_reserved_page(pfn_to_page(pfn));
1206
1207 if (time_after(jiffies, time_limit)) {
1208 cond_resched();
1209 time_limit = jiffies + HZ;
1210 }
1211 }
1212}
1213
1214/*
1215 * Skip memory holes and free memory that was actually reserved.
1216 */
Hari Bathinidda9dbf2019-09-11 20:25:36 +05301217static void fadump_release_reserved_area(u64 start, u64 end)
Hari Bathini68fa6472017-06-02 01:10:10 +05301218{
Hari Bathinidda9dbf2019-09-11 20:25:36 +05301219 u64 tstart, tend, spfn, epfn;
Hari Bathini68fa6472017-06-02 01:10:10 +05301220 struct memblock_region *reg;
Hari Bathini68fa6472017-06-02 01:10:10 +05301221
Hari Bathinidda9dbf2019-09-11 20:25:36 +05301222 spfn = PHYS_PFN(start);
1223 epfn = PHYS_PFN(end);
Hari Bathini68fa6472017-06-02 01:10:10 +05301224 for_each_memblock(memory, reg) {
Hari Bathinidda9dbf2019-09-11 20:25:36 +05301225 tstart = max_t(u64, spfn, memblock_region_memory_base_pfn(reg));
1226 tend = min_t(u64, epfn, memblock_region_memory_end_pfn(reg));
Hari Bathini68fa6472017-06-02 01:10:10 +05301227 if (tstart < tend) {
1228 fadump_free_reserved_memory(tstart, tend);
1229
Hari Bathinidda9dbf2019-09-11 20:25:36 +05301230 if (tend == epfn)
Hari Bathini68fa6472017-06-02 01:10:10 +05301231 break;
1232
Hari Bathinidda9dbf2019-09-11 20:25:36 +05301233 spfn = tend;
Hari Bathini68fa6472017-06-02 01:10:10 +05301234 }
1235 }
1236}
1237
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001238/*
Hari Bathinidda9dbf2019-09-11 20:25:36 +05301239 * Sort the mem ranges in-place and merge adjacent ranges
1240 * to minimize the memory ranges count.
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001241 */
Hari Bathinidda9dbf2019-09-11 20:25:36 +05301242static void sort_and_merge_mem_ranges(struct fadump_mrange_info *mrange_info)
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001243{
Hari Bathinidda9dbf2019-09-11 20:25:36 +05301244 struct fadump_memory_range *mem_ranges;
1245 struct fadump_memory_range tmp_range;
1246 u64 base, size;
1247 int i, j, idx;
1248
1249 if (!reserved_mrange_info.mem_range_cnt)
1250 return;
1251
1252 /* Sort the memory ranges */
1253 mem_ranges = mrange_info->mem_ranges;
1254 for (i = 0; i < mrange_info->mem_range_cnt; i++) {
1255 idx = i;
1256 for (j = (i + 1); j < mrange_info->mem_range_cnt; j++) {
1257 if (mem_ranges[idx].base > mem_ranges[j].base)
1258 idx = j;
1259 }
1260 if (idx != i) {
1261 tmp_range = mem_ranges[idx];
1262 mem_ranges[idx] = mem_ranges[i];
1263 mem_ranges[i] = tmp_range;
1264 }
1265 }
1266
1267 /* Merge adjacent reserved ranges */
1268 idx = 0;
1269 for (i = 1; i < mrange_info->mem_range_cnt; i++) {
1270 base = mem_ranges[i-1].base;
1271 size = mem_ranges[i-1].size;
1272 if (mem_ranges[i].base == (base + size))
1273 mem_ranges[idx].size += mem_ranges[i].size;
1274 else {
1275 idx++;
1276 if (i == idx)
1277 continue;
1278
1279 mem_ranges[idx] = mem_ranges[i];
1280 }
1281 }
1282 mrange_info->mem_range_cnt = idx + 1;
1283}
1284
1285/*
1286 * Scan reserved-ranges to consider them while reserving/releasing
1287 * memory for FADump.
1288 */
Hari Bathini02c04e32020-04-20 14:26:09 +05301289static void __init early_init_dt_scan_reserved_ranges(unsigned long node)
Hari Bathinidda9dbf2019-09-11 20:25:36 +05301290{
Hari Bathinidda9dbf2019-09-11 20:25:36 +05301291 const __be32 *prop;
1292 int len, ret = -1;
1293 unsigned long i;
1294
Hari Bathini02c04e32020-04-20 14:26:09 +05301295 /* reserved-ranges already scanned */
1296 if (reserved_mrange_info.mem_range_cnt != 0)
1297 return;
Hari Bathinidda9dbf2019-09-11 20:25:36 +05301298
Hari Bathini02c04e32020-04-20 14:26:09 +05301299 prop = of_get_flat_dt_prop(node, "reserved-ranges", &len);
Hari Bathinidda9dbf2019-09-11 20:25:36 +05301300 if (!prop)
Hari Bathini02c04e32020-04-20 14:26:09 +05301301 return;
Hari Bathinidda9dbf2019-09-11 20:25:36 +05301302
1303 /*
1304 * Each reserved range is an (address,size) pair, 2 cells each,
1305 * totalling 4 cells per range.
1306 */
1307 for (i = 0; i < len / (sizeof(*prop) * 4); i++) {
1308 u64 base, size;
1309
1310 base = of_read_number(prop + (i * 4) + 0, 2);
1311 size = of_read_number(prop + (i * 4) + 2, 2);
1312
1313 if (size) {
1314 ret = fadump_add_mem_range(&reserved_mrange_info,
1315 base, base + size);
1316 if (ret < 0) {
1317 pr_warn("some reserved ranges are ignored!\n");
1318 break;
1319 }
1320 }
1321 }
1322
Hari Bathini02c04e32020-04-20 14:26:09 +05301323 /* Compact reserved ranges */
1324 sort_and_merge_mem_ranges(&reserved_mrange_info);
Hari Bathinidda9dbf2019-09-11 20:25:36 +05301325}
1326
1327/*
1328 * Release the memory that was reserved during early boot to preserve the
1329 * crash'ed kernel's memory contents except reserved dump area (permanent
1330 * reservation) and reserved ranges used by F/W. The released memory will
1331 * be available for general use.
1332 */
1333static void fadump_release_memory(u64 begin, u64 end)
1334{
1335 u64 ra_start, ra_end, tstart;
1336 int i, ret;
1337
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001338 ra_start = fw_dump.reserve_dump_area_start;
1339 ra_end = ra_start + fw_dump.reserve_dump_area_size;
1340
Hari Bathini68fa6472017-06-02 01:10:10 +05301341 /*
Hari Bathini02c04e32020-04-20 14:26:09 +05301342 * If reserved ranges array limit is hit, overwrite the last reserved
1343 * memory range with reserved dump area to ensure it is excluded from
1344 * the memory being released (reused for next FADump registration).
Hari Bathini68fa6472017-06-02 01:10:10 +05301345 */
Hari Bathini02c04e32020-04-20 14:26:09 +05301346 if (reserved_mrange_info.mem_range_cnt ==
1347 reserved_mrange_info.max_mem_ranges)
1348 reserved_mrange_info.mem_range_cnt--;
Hari Bathinidda9dbf2019-09-11 20:25:36 +05301349
Hari Bathini02c04e32020-04-20 14:26:09 +05301350 ret = fadump_add_mem_range(&reserved_mrange_info, ra_start, ra_end);
1351 if (ret != 0)
Hari Bathinidda9dbf2019-09-11 20:25:36 +05301352 return;
Hari Bathinidda9dbf2019-09-11 20:25:36 +05301353
1354 /* Get the reserved ranges list in order first. */
1355 sort_and_merge_mem_ranges(&reserved_mrange_info);
1356
1357 /* Exclude reserved ranges and release remaining memory */
1358 tstart = begin;
1359 for (i = 0; i < reserved_mrange_info.mem_range_cnt; i++) {
1360 ra_start = reserved_mrange_info.mem_ranges[i].base;
1361 ra_end = ra_start + reserved_mrange_info.mem_ranges[i].size;
1362
1363 if (tstart >= ra_end)
1364 continue;
1365
1366 if (tstart < ra_start)
1367 fadump_release_reserved_area(tstart, ra_start);
1368 tstart = ra_end;
1369 }
1370
1371 if (tstart < end)
1372 fadump_release_reserved_area(tstart, end);
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001373}
1374
1375static void fadump_invalidate_release_mem(void)
1376{
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001377 mutex_lock(&fadump_mutex);
1378 if (!fw_dump.dump_active) {
1379 mutex_unlock(&fadump_mutex);
1380 return;
1381 }
1382
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001383 fadump_cleanup();
1384 mutex_unlock(&fadump_mutex);
1385
Hari Bathini7dee93a2019-09-11 20:27:39 +05301386 fadump_release_memory(fw_dump.boot_mem_top, memblock_end_of_DRAM());
Hari Bathini961cf262019-09-11 20:16:36 +05301387 fadump_free_cpu_notes_buf();
1388
Hari Bathinia4e2e2c2019-09-11 20:23:28 +05301389 /*
1390 * Setup kernel metadata and initialize the kernel dump
1391 * memory structure for FADump re-registration.
1392 */
1393 if (fw_dump.ops->fadump_setup_metadata &&
1394 (fw_dump.ops->fadump_setup_metadata(&fw_dump) < 0))
1395 pr_warn("Failed to setup kernel metadata!\n");
Hari Bathini41a65d12019-09-11 20:18:57 +05301396 fw_dump.ops->fadump_init_mem_struct(&fw_dump);
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001397}
1398
Sourabh Jaind418b192019-12-11 21:39:07 +05301399static ssize_t release_mem_store(struct kobject *kobj,
1400 struct kobj_attribute *attr,
1401 const char *buf, size_t count)
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001402{
Michal Suchanekdcdc4672017-06-26 16:06:01 +02001403 int input = -1;
1404
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001405 if (!fw_dump.dump_active)
1406 return -EPERM;
1407
Michal Suchanekdcdc4672017-06-26 16:06:01 +02001408 if (kstrtoint(buf, 0, &input))
1409 return -EINVAL;
1410
1411 if (input == 1) {
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001412 /*
1413 * Take away the '/proc/vmcore'. We are releasing the dump
1414 * memory, hence it will not be valid anymore.
1415 */
Michael Ellerman2685f822016-09-30 10:51:46 +10001416#ifdef CONFIG_PROC_VMCORE
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001417 vmcore_cleanup();
Michael Ellerman2685f822016-09-30 10:51:46 +10001418#endif
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001419 fadump_invalidate_release_mem();
1420
1421 } else
1422 return -EINVAL;
1423 return count;
1424}
1425
Sourabh Jaind418b192019-12-11 21:39:07 +05301426/* Release the reserved memory and disable the FADump */
1427static void unregister_fadump(void)
1428{
1429 fadump_cleanup();
1430 fadump_release_memory(fw_dump.reserve_dump_area_start,
1431 fw_dump.reserve_dump_area_size);
1432 fw_dump.fadump_enabled = 0;
1433 kobject_put(fadump_kobj);
1434}
1435
1436static ssize_t enabled_show(struct kobject *kobj,
1437 struct kobj_attribute *attr,
1438 char *buf)
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001439{
1440 return sprintf(buf, "%d\n", fw_dump.fadump_enabled);
1441}
1442
Sourabh Jaind8e73452019-12-11 21:39:10 +05301443static ssize_t mem_reserved_show(struct kobject *kobj,
1444 struct kobj_attribute *attr,
1445 char *buf)
1446{
1447 return sprintf(buf, "%ld\n", fw_dump.reserve_dump_area_size);
1448}
1449
Sourabh Jaind418b192019-12-11 21:39:07 +05301450static ssize_t registered_show(struct kobject *kobj,
1451 struct kobj_attribute *attr,
1452 char *buf)
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001453{
1454 return sprintf(buf, "%d\n", fw_dump.dump_registered);
1455}
1456
Sourabh Jaind418b192019-12-11 21:39:07 +05301457static ssize_t registered_store(struct kobject *kobj,
1458 struct kobj_attribute *attr,
1459 const char *buf, size_t count)
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001460{
1461 int ret = 0;
Michal Suchanekdcdc4672017-06-26 16:06:01 +02001462 int input = -1;
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001463
Hari Bathinif3512012019-09-11 20:19:44 +05301464 if (!fw_dump.fadump_enabled || fw_dump.dump_active)
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001465 return -EPERM;
1466
Michal Suchanekdcdc4672017-06-26 16:06:01 +02001467 if (kstrtoint(buf, 0, &input))
1468 return -EINVAL;
1469
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001470 mutex_lock(&fadump_mutex);
1471
Michal Suchanekdcdc4672017-06-26 16:06:01 +02001472 switch (input) {
1473 case 0:
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001474 if (fw_dump.dump_registered == 0) {
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001475 goto unlock_out;
1476 }
Hari Bathinif3512012019-09-11 20:19:44 +05301477
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001478 /* Un-register Firmware-assisted dump */
Hari Bathini41a65d12019-09-11 20:18:57 +05301479 pr_debug("Un-register firmware-assisted dump\n");
1480 fw_dump.ops->fadump_unregister(&fw_dump);
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001481 break;
Michal Suchanekdcdc4672017-06-26 16:06:01 +02001482 case 1:
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001483 if (fw_dump.dump_registered == 1) {
Hari Bathini0823c682018-09-14 19:36:02 +05301484 /* Un-register Firmware-assisted dump */
Hari Bathini41a65d12019-09-11 20:18:57 +05301485 fw_dump.ops->fadump_unregister(&fw_dump);
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001486 }
1487 /* Register Firmware-assisted dump */
Michal Suchanek98b8cd72017-05-27 17:46:15 +02001488 ret = register_fadump();
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001489 break;
1490 default:
1491 ret = -EINVAL;
1492 break;
1493 }
1494
1495unlock_out:
1496 mutex_unlock(&fadump_mutex);
1497 return ret < 0 ? ret : count;
1498}
1499
1500static int fadump_region_show(struct seq_file *m, void *private)
1501{
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001502 if (!fw_dump.fadump_enabled)
1503 return 0;
1504
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001505 mutex_lock(&fadump_mutex);
Hari Bathinif3512012019-09-11 20:19:44 +05301506 fw_dump.ops->fadump_region_show(&fw_dump, m);
1507 mutex_unlock(&fadump_mutex);
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001508 return 0;
1509}
1510
Sourabh Jaind418b192019-12-11 21:39:07 +05301511static struct kobj_attribute release_attr = __ATTR_WO(release_mem);
1512static struct kobj_attribute enable_attr = __ATTR_RO(enabled);
1513static struct kobj_attribute register_attr = __ATTR_RW(registered);
Sourabh Jaind8e73452019-12-11 21:39:10 +05301514static struct kobj_attribute mem_reserved_attr = __ATTR_RO(mem_reserved);
Sourabh Jaind418b192019-12-11 21:39:07 +05301515
1516static struct attribute *fadump_attrs[] = {
1517 &enable_attr.attr,
1518 &register_attr.attr,
Sourabh Jaind8e73452019-12-11 21:39:10 +05301519 &mem_reserved_attr.attr,
Sourabh Jaind418b192019-12-11 21:39:07 +05301520 NULL,
1521};
1522
1523ATTRIBUTE_GROUPS(fadump);
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001524
Yangtao Lif6cee262018-11-05 10:01:19 -05001525DEFINE_SHOW_ATTRIBUTE(fadump_region);
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001526
1527static void fadump_init_files(void)
1528{
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001529 int rc = 0;
1530
Sourabh Jaind418b192019-12-11 21:39:07 +05301531 fadump_kobj = kobject_create_and_add("fadump", kernel_kobj);
1532 if (!fadump_kobj) {
1533 pr_err("failed to create fadump kobject\n");
1534 return;
1535 }
Greg Kroah-Hartman860286c2020-02-09 11:58:56 +01001536
1537 debugfs_create_file("fadump_region", 0444, powerpc_debugfs_root, NULL,
1538 &fadump_region_fops);
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001539
1540 if (fw_dump.dump_active) {
Sourabh Jaind418b192019-12-11 21:39:07 +05301541 rc = sysfs_create_file(fadump_kobj, &release_attr.attr);
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001542 if (rc)
Sourabh Jaind418b192019-12-11 21:39:07 +05301543 pr_err("unable to create release_mem sysfs file (%d)\n",
1544 rc);
1545 }
1546
1547 rc = sysfs_create_groups(fadump_kobj, fadump_groups);
1548 if (rc) {
1549 pr_err("sysfs group creation failed (%d), unregistering FADump",
1550 rc);
1551 unregister_fadump();
1552 return;
1553 }
1554
1555 /*
1556 * The FADump sysfs are moved from kernel_kobj to fadump_kobj need to
1557 * create symlink at old location to maintain backward compatibility.
1558 *
1559 * - fadump_enabled -> fadump/enabled
1560 * - fadump_registered -> fadump/registered
1561 * - fadump_release_mem -> fadump/release_mem
1562 */
1563 rc = compat_only_sysfs_link_entry_to_kobj(kernel_kobj, fadump_kobj,
1564 "enabled", "fadump_enabled");
1565 if (rc) {
1566 pr_err("unable to create fadump_enabled symlink (%d)", rc);
1567 return;
1568 }
1569
1570 rc = compat_only_sysfs_link_entry_to_kobj(kernel_kobj, fadump_kobj,
1571 "registered",
1572 "fadump_registered");
1573 if (rc) {
1574 pr_err("unable to create fadump_registered symlink (%d)", rc);
1575 sysfs_remove_link(kernel_kobj, "fadump_enabled");
1576 return;
1577 }
1578
1579 if (fw_dump.dump_active) {
1580 rc = compat_only_sysfs_link_entry_to_kobj(kernel_kobj,
1581 fadump_kobj,
1582 "release_mem",
1583 "fadump_release_mem");
1584 if (rc)
1585 pr_err("unable to create fadump_release_mem symlink (%d)",
1586 rc);
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001587 }
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001588 return;
1589}
1590
1591/*
1592 * Prepare for firmware-assisted dump.
1593 */
1594int __init setup_fadump(void)
1595{
Michal Suchanek565f9bc2019-11-07 17:47:57 +01001596 if (!fw_dump.fadump_supported)
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001597 return 0;
1598
Michal Suchanek565f9bc2019-11-07 17:47:57 +01001599 fadump_init_files();
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001600 fadump_show_config();
Michal Suchanek565f9bc2019-11-07 17:47:57 +01001601
1602 if (!fw_dump.fadump_enabled)
1603 return 1;
1604
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001605 /*
1606 * If dump data is available then see if it is valid and prepare for
1607 * saving it to the disk.
1608 */
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001609 if (fw_dump.dump_active) {
1610 /*
1611 * if dump process fails then invalidate the registration
1612 * and release memory before proceeding for re-registration.
1613 */
Hari Bathinif3512012019-09-11 20:19:44 +05301614 if (fw_dump.ops->fadump_process(&fw_dump) < 0)
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001615 fadump_invalidate_release_mem();
1616 }
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001617 /* Initialize the kernel dump memory structure for FAD registration. */
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001618 else if (fw_dump.reserve_dump_area_size)
Hari Bathini41a65d12019-09-11 20:18:57 +05301619 fw_dump.ops->fadump_init_mem_struct(&fw_dump);
Hari Bathinif3512012019-09-11 20:19:44 +05301620
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001621 return 1;
1622}
1623subsys_initcall(setup_fadump);
Hari Bathinibec53192019-09-11 20:26:03 +05301624#else /* !CONFIG_PRESERVE_FA_DUMP */
1625
1626/* Scan the Firmware Assisted dump configuration details. */
1627int __init early_init_dt_scan_fw_dump(unsigned long node, const char *uname,
1628 int depth, void *data)
1629{
1630 if ((depth != 1) || (strcmp(uname, "ibm,opal") != 0))
1631 return 0;
1632
1633 opal_fadump_dt_scan(&fw_dump, node);
1634 return 1;
1635}
1636
1637/*
1638 * When dump is active but PRESERVE_FA_DUMP is enabled on the kernel,
1639 * preserve crash data. The subsequent memory preserving kernel boot
1640 * is likely to process this crash data.
1641 */
1642int __init fadump_reserve_mem(void)
1643{
1644 if (fw_dump.dump_active) {
1645 /*
1646 * If last boot has crashed then reserve all the memory
1647 * above boot memory to preserve crash data.
1648 */
1649 pr_info("Preserving crash data for processing in next boot.\n");
1650 fadump_reserve_crash_area(fw_dump.boot_mem_top);
1651 } else
1652 pr_debug("FADump-aware kernel..\n");
1653
1654 return 1;
1655}
1656#endif /* CONFIG_PRESERVE_FA_DUMP */
Hari Bathinib2a815a2019-09-11 20:25:49 +05301657
1658/* Preserve everything above the base address */
1659static void __init fadump_reserve_crash_area(u64 base)
1660{
1661 struct memblock_region *reg;
1662 u64 mstart, msize;
1663
1664 for_each_memblock(memory, reg) {
1665 mstart = reg->base;
1666 msize = reg->size;
1667
1668 if ((mstart + msize) < base)
1669 continue;
1670
1671 if (mstart < base) {
1672 msize -= (base - mstart);
1673 mstart = base;
1674 }
1675
1676 pr_info("Reserving %lluMB of memory at %#016llx for preserving crash data",
1677 (msize >> 20), mstart);
1678 memblock_reserve(mstart, msize);
1679 }
1680}
Hari Bathinibec53192019-09-11 20:26:03 +05301681
1682unsigned long __init arch_reserved_kernel_pages(void)
1683{
1684 return memblock_reserved_size() / PAGE_SIZE;
1685}