blob: 03f2708cd954014c0e1049183f5677f5a29f775b [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>
31#include <asm/rtas.h>
32#include <asm/fadump.h>
Hari Bathinica986d72019-09-11 20:16:21 +053033#include <asm/fadump-internal.h>
Stephen Rothwellcad3c832012-03-30 14:01:07 +000034#include <asm/setup.h>
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +000035
Hari Bathini0226e552019-09-11 20:18:14 +053036#include "../platforms/pseries/rtas-fadump.h"
37
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +000038static struct fw_dump fw_dump;
Hari Bathini0226e552019-09-11 20:18:14 +053039static struct rtas_fadump_mem_struct fdm;
40static const struct rtas_fadump_mem_struct *fdm_active;
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +000041
42static DEFINE_MUTEX(fadump_mutex);
Hari Bathini1bd6a1c2018-08-07 02:12:45 +053043struct fad_crash_memory_ranges *crash_memory_ranges;
44int crash_memory_ranges_size;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +000045int crash_mem_ranges;
Hari Bathini1bd6a1c2018-08-07 02:12:45 +053046int max_crash_mem_ranges;
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +000047
Mahesh Salgaonkara4e92ce2018-08-20 13:47:17 +053048#ifdef CONFIG_CMA
Hari Bathini0226e552019-09-11 20:18:14 +053049static struct cma *fadump_cma;
50
Mahesh Salgaonkara4e92ce2018-08-20 13:47:17 +053051/*
52 * fadump_cma_init() - Initialize CMA area from a fadump reserved memory
53 *
54 * This function initializes CMA area from fadump reserved memory.
55 * The total size of fadump reserved memory covers for boot memory size
56 * + cpu data size + hpte size and metadata.
57 * Initialize only the area equivalent to boot memory size for CMA use.
58 * The reamining portion of fadump reserved memory will be not given
59 * to CMA and pages for thoes will stay reserved. boot memory size is
60 * aligned per CMA requirement to satisy cma_init_reserved_mem() call.
61 * But for some reason even if it fails we still have the memory reservation
62 * with us and we can still continue doing fadump.
63 */
64int __init fadump_cma_init(void)
65{
66 unsigned long long base, size;
67 int rc;
68
69 if (!fw_dump.fadump_enabled)
70 return 0;
71
72 /*
73 * Do not use CMA if user has provided fadump=nocma kernel parameter.
74 * Return 1 to continue with fadump old behaviour.
75 */
76 if (fw_dump.nocma)
77 return 1;
78
79 base = fw_dump.reserve_dump_area_start;
80 size = fw_dump.boot_memory_size;
81
82 if (!size)
83 return 0;
84
85 rc = cma_init_reserved_mem(base, size, 0, "fadump_cma", &fadump_cma);
86 if (rc) {
87 pr_err("Failed to init cma area for firmware-assisted dump,%d\n", rc);
88 /*
89 * Though the CMA init has failed we still have memory
90 * reservation with us. The reserved memory will be
91 * blocked from production system usage. Hence return 1,
92 * so that we can continue with fadump.
93 */
94 return 1;
95 }
96
97 /*
98 * So we now have successfully initialized cma area for fadump.
99 */
100 pr_info("Initialized 0x%lx bytes cma area at %ldMB from 0x%lx "
101 "bytes of memory reserved for firmware-assisted dump\n",
102 cma_get_size(fadump_cma),
103 (unsigned long)cma_get_base(fadump_cma) >> 20,
104 fw_dump.reserve_dump_area_size);
105 return 1;
106}
107#else
108static int __init fadump_cma_init(void) { return 1; }
109#endif /* CONFIG_CMA */
110
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000111/* Scan the Firmware Assisted dump configuration details. */
112int __init early_init_dt_scan_fw_dump(unsigned long node,
113 const char *uname, int depth, void *data)
114{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500115 const __be32 *sections;
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000116 int i, num_sections;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500117 int size;
Hari Bathini408cddd2014-10-01 12:32:30 +0530118 const __be32 *token;
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000119
120 if (depth != 1 || strcmp(uname, "rtas") != 0)
121 return 0;
122
123 /*
124 * Check if Firmware Assisted dump is supported. if yes, check
125 * if dump has been initiated on last reboot.
126 */
127 token = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump", NULL);
128 if (!token)
Gavin Shana7d043172014-04-24 18:00:31 +1000129 return 1;
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000130
131 fw_dump.fadump_supported = 1;
Hari Bathini408cddd2014-10-01 12:32:30 +0530132 fw_dump.ibm_configure_kernel_dump = be32_to_cpu(*token);
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000133
134 /*
135 * The 'ibm,kernel-dump' rtas node is present only if there is
136 * dump data waiting for us.
137 */
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000138 fdm_active = of_get_flat_dt_prop(node, "ibm,kernel-dump", NULL);
139 if (fdm_active)
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000140 fw_dump.dump_active = 1;
141
142 /* Get the sizes required to store dump data for the firmware provided
143 * dump sections.
144 * For each dump section type supported, a 32bit cell which defines
145 * the ID of a supported section followed by two 32 bit cells which
146 * gives teh size of the section in bytes.
147 */
148 sections = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump-sizes",
149 &size);
150
151 if (!sections)
Gavin Shana7d043172014-04-24 18:00:31 +1000152 return 1;
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000153
154 num_sections = size / (3 * sizeof(u32));
155
156 for (i = 0; i < num_sections; i++, sections += 3) {
157 u32 type = (u32)of_read_number(sections, 1);
158
159 switch (type) {
Hari Bathini0226e552019-09-11 20:18:14 +0530160 case RTAS_FADUMP_CPU_STATE_DATA:
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000161 fw_dump.cpu_state_data_size =
162 of_read_ulong(&sections[1], 2);
163 break;
Hari Bathini0226e552019-09-11 20:18:14 +0530164 case RTAS_FADUMP_HPTE_REGION:
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000165 fw_dump.hpte_region_size =
166 of_read_ulong(&sections[1], 2);
167 break;
168 }
169 }
Gavin Shana7d043172014-04-24 18:00:31 +1000170
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000171 return 1;
172}
173
Hari Bathinieae0dfc2017-06-01 22:51:26 +0530174/*
175 * If fadump is registered, check if the memory provided
Mahesh Salgaonkar0db68962018-08-20 13:47:32 +0530176 * falls within boot memory area and reserved memory area.
Hari Bathinieae0dfc2017-06-01 22:51:26 +0530177 */
Mahesh Salgaonkar0db68962018-08-20 13:47:32 +0530178int is_fadump_memory_area(u64 addr, ulong size)
Hari Bathinieae0dfc2017-06-01 22:51:26 +0530179{
Mahesh Salgaonkar0db68962018-08-20 13:47:32 +0530180 u64 d_start = fw_dump.reserve_dump_area_start;
181 u64 d_end = d_start + fw_dump.reserve_dump_area_size;
182
Hari Bathinieae0dfc2017-06-01 22:51:26 +0530183 if (!fw_dump.dump_registered)
184 return 0;
185
Mahesh Salgaonkar0db68962018-08-20 13:47:32 +0530186 if (((addr + size) > d_start) && (addr <= d_end))
187 return 1;
188
Hari Bathinieae0dfc2017-06-01 22:51:26 +0530189 return (addr + size) > RMA_START && addr <= fw_dump.boot_memory_size;
190}
191
Nicholas Piggin6fcd6ba2017-07-19 16:59:11 +1000192int should_fadump_crash(void)
193{
194 if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr)
195 return 0;
196 return 1;
197}
198
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000199int is_fadump_active(void)
200{
201 return fw_dump.dump_active;
202}
203
Hari Bathinia5a05b92017-06-01 22:52:10 +0530204/*
Hari Bathini961cf262019-09-11 20:16:36 +0530205 * Returns true, if there are no holes in memory area between d_start to d_end,
206 * false otherwise.
Hari Bathinia5a05b92017-06-01 22:52:10 +0530207 */
Hari Bathini961cf262019-09-11 20:16:36 +0530208static bool is_fadump_mem_area_contiguous(u64 d_start, u64 d_end)
Hari Bathinia5a05b92017-06-01 22:52:10 +0530209{
210 struct memblock_region *reg;
Hari Bathini961cf262019-09-11 20:16:36 +0530211 bool ret = false;
212 u64 start, end;
Hari Bathinia5a05b92017-06-01 22:52:10 +0530213
214 for_each_memblock(memory, reg) {
Hari Bathini961cf262019-09-11 20:16:36 +0530215 start = max_t(u64, d_start, reg->base);
216 end = min_t(u64, d_end, (reg->base + reg->size));
217 if (d_start < end) {
218 /* Memory hole from d_start to start */
219 if (start > d_start)
Hari Bathinia5a05b92017-06-01 22:52:10 +0530220 break;
221
Hari Bathini961cf262019-09-11 20:16:36 +0530222 if (end == d_end) {
223 ret = true;
Hari Bathinia5a05b92017-06-01 22:52:10 +0530224 break;
225 }
226
Hari Bathini961cf262019-09-11 20:16:36 +0530227 d_start = end + 1;
Hari Bathinia5a05b92017-06-01 22:52:10 +0530228 }
229 }
230
231 return ret;
232}
233
Mahesh Salgaonkarf86593b2018-08-20 13:47:24 +0530234/*
Hari Bathini961cf262019-09-11 20:16:36 +0530235 * Returns true, if there are no holes in boot memory area,
236 * false otherwise.
237 */
Hari Bathini7f0ad112019-09-11 20:16:52 +0530238bool is_fadump_boot_mem_contiguous(void)
Hari Bathini961cf262019-09-11 20:16:36 +0530239{
240 return is_fadump_mem_area_contiguous(0, fw_dump.boot_memory_size);
241}
242
243/*
Mahesh Salgaonkarf86593b2018-08-20 13:47:24 +0530244 * Returns true, if there are no holes in reserved memory area,
245 * false otherwise.
246 */
Hari Bathini7f0ad112019-09-11 20:16:52 +0530247bool is_fadump_reserved_mem_contiguous(void)
Mahesh Salgaonkarf86593b2018-08-20 13:47:24 +0530248{
Hari Bathini961cf262019-09-11 20:16:36 +0530249 u64 d_start, d_end;
Mahesh Salgaonkarf86593b2018-08-20 13:47:24 +0530250
Hari Bathini961cf262019-09-11 20:16:36 +0530251 d_start = fw_dump.reserve_dump_area_start;
252 d_end = d_start + fw_dump.reserve_dump_area_size;
253 return is_fadump_mem_area_contiguous(d_start, d_end);
Mahesh Salgaonkarf86593b2018-08-20 13:47:24 +0530254}
255
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000256/* Print firmware assisted dump configurations for debugging purpose. */
257static void fadump_show_config(void)
258{
259 pr_debug("Support for firmware-assisted dump (fadump): %s\n",
260 (fw_dump.fadump_supported ? "present" : "no support"));
261
262 if (!fw_dump.fadump_supported)
263 return;
264
265 pr_debug("Fadump enabled : %s\n",
266 (fw_dump.fadump_enabled ? "yes" : "no"));
267 pr_debug("Dump Active : %s\n",
268 (fw_dump.dump_active ? "yes" : "no"));
269 pr_debug("Dump section sizes:\n");
270 pr_debug(" CPU state data size: %lx\n", fw_dump.cpu_state_data_size);
271 pr_debug(" HPTE region size : %lx\n", fw_dump.hpte_region_size);
272 pr_debug("Boot memory size : %lx\n", fw_dump.boot_memory_size);
273}
274
Hari Bathini0226e552019-09-11 20:18:14 +0530275static unsigned long init_fadump_mem_struct(struct rtas_fadump_mem_struct *fdm,
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000276 unsigned long addr)
277{
278 if (!fdm)
279 return 0;
280
Hari Bathini0226e552019-09-11 20:18:14 +0530281 memset(fdm, 0, sizeof(struct rtas_fadump_mem_struct));
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000282 addr = addr & PAGE_MASK;
283
Hari Bathini408cddd2014-10-01 12:32:30 +0530284 fdm->header.dump_format_version = cpu_to_be32(0x00000001);
285 fdm->header.dump_num_sections = cpu_to_be16(3);
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000286 fdm->header.dump_status_flag = 0;
287 fdm->header.offset_first_dump_section =
Hari Bathini0226e552019-09-11 20:18:14 +0530288 cpu_to_be32((u32)offsetof(struct rtas_fadump_mem_struct, cpu_state_data));
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000289
290 /*
291 * Fields for disk dump option.
292 * We are not using disk dump option, hence set these fields to 0.
293 */
294 fdm->header.dd_block_size = 0;
295 fdm->header.dd_block_offset = 0;
296 fdm->header.dd_num_blocks = 0;
297 fdm->header.dd_offset_disk_path = 0;
298
299 /* set 0 to disable an automatic dump-reboot. */
300 fdm->header.max_time_auto = 0;
301
302 /* Kernel dump sections */
303 /* cpu state data section. */
Hari Bathini0226e552019-09-11 20:18:14 +0530304 fdm->cpu_state_data.request_flag = cpu_to_be32(RTAS_FADUMP_REQUEST_FLAG);
305 fdm->cpu_state_data.source_data_type = cpu_to_be16(RTAS_FADUMP_CPU_STATE_DATA);
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000306 fdm->cpu_state_data.source_address = 0;
Hari Bathini408cddd2014-10-01 12:32:30 +0530307 fdm->cpu_state_data.source_len = cpu_to_be64(fw_dump.cpu_state_data_size);
308 fdm->cpu_state_data.destination_address = cpu_to_be64(addr);
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000309 addr += fw_dump.cpu_state_data_size;
310
311 /* hpte region section */
Hari Bathini0226e552019-09-11 20:18:14 +0530312 fdm->hpte_region.request_flag = cpu_to_be32(RTAS_FADUMP_REQUEST_FLAG);
313 fdm->hpte_region.source_data_type = cpu_to_be16(RTAS_FADUMP_HPTE_REGION);
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000314 fdm->hpte_region.source_address = 0;
Hari Bathini408cddd2014-10-01 12:32:30 +0530315 fdm->hpte_region.source_len = cpu_to_be64(fw_dump.hpte_region_size);
316 fdm->hpte_region.destination_address = cpu_to_be64(addr);
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000317 addr += fw_dump.hpte_region_size;
318
319 /* RMA region section */
Hari Bathini0226e552019-09-11 20:18:14 +0530320 fdm->rmr_region.request_flag = cpu_to_be32(RTAS_FADUMP_REQUEST_FLAG);
321 fdm->rmr_region.source_data_type = cpu_to_be16(RTAS_FADUMP_REAL_MODE_REGION);
Hari Bathini408cddd2014-10-01 12:32:30 +0530322 fdm->rmr_region.source_address = cpu_to_be64(RMA_START);
323 fdm->rmr_region.source_len = cpu_to_be64(fw_dump.boot_memory_size);
324 fdm->rmr_region.destination_address = cpu_to_be64(addr);
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000325 addr += fw_dump.boot_memory_size;
326
327 return addr;
328}
329
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000330/**
331 * fadump_calculate_reserve_size(): reserve variable boot area 5% of System RAM
332 *
333 * Function to find the largest memory size we need to reserve during early
334 * boot process. This will be the size of the memory that is required for a
335 * kernel to boot successfully.
336 *
337 * This function has been taken from phyp-assisted dump feature implementation.
338 *
339 * returns larger of 256MB or 5% rounded down to multiples of 256MB.
340 *
341 * TODO: Come up with better approach to find out more accurate memory size
342 * that is required for a kernel to boot successfully.
343 *
344 */
345static inline unsigned long fadump_calculate_reserve_size(void)
346{
Hari Bathini11550dc2017-05-08 15:56:28 -0700347 int ret;
348 unsigned long long base, size;
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000349
Hari Bathini81d9eca2017-05-22 15:04:23 +0530350 if (fw_dump.reserve_bootvar)
351 pr_warn("'fadump_reserve_mem=' parameter is deprecated in favor of 'crashkernel=' parameter.\n");
352
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000353 /*
Hari Bathini11550dc2017-05-08 15:56:28 -0700354 * Check if the size is specified through crashkernel= cmdline
Hari Bathinie7467dc2017-05-22 15:04:47 +0530355 * option. If yes, then use that but ignore base as fadump reserves
356 * memory at a predefined offset.
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000357 */
Hari Bathini11550dc2017-05-08 15:56:28 -0700358 ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
359 &size, &base);
360 if (ret == 0 && size > 0) {
Hari Bathini48a316e2017-06-02 13:00:27 +0530361 unsigned long max_size;
362
Hari Bathini81d9eca2017-05-22 15:04:23 +0530363 if (fw_dump.reserve_bootvar)
364 pr_info("Using 'crashkernel=' parameter for memory reservation.\n");
365
Hari Bathini11550dc2017-05-08 15:56:28 -0700366 fw_dump.reserve_bootvar = (unsigned long)size;
Hari Bathini48a316e2017-06-02 13:00:27 +0530367
368 /*
369 * Adjust if the boot memory size specified is above
370 * the upper limit.
371 */
372 max_size = memblock_phys_mem_size() / MAX_BOOT_MEM_RATIO;
373 if (fw_dump.reserve_bootvar > max_size) {
374 fw_dump.reserve_bootvar = max_size;
375 pr_info("Adjusted boot memory size to %luMB\n",
376 (fw_dump.reserve_bootvar >> 20));
377 }
378
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000379 return fw_dump.reserve_bootvar;
Hari Bathini81d9eca2017-05-22 15:04:23 +0530380 } else if (fw_dump.reserve_bootvar) {
381 /*
382 * 'fadump_reserve_mem=' is being used to reserve memory
383 * for firmware-assisted dump.
384 */
385 return fw_dump.reserve_bootvar;
Hari Bathini11550dc2017-05-08 15:56:28 -0700386 }
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000387
388 /* divide by 20 to get 5% of value */
Hari Bathini48a316e2017-06-02 13:00:27 +0530389 size = memblock_phys_mem_size() / 20;
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000390
391 /* round it down in multiples of 256 */
392 size = size & ~0x0FFFFFFFUL;
393
394 /* Truncate to memory_limit. We don't want to over reserve the memory.*/
395 if (memory_limit && size > memory_limit)
396 size = memory_limit;
397
398 return (size > MIN_BOOT_MEM ? size : MIN_BOOT_MEM);
399}
400
401/*
402 * Calculate the total memory size required to be reserved for
403 * firmware-assisted dump registration.
404 */
405static unsigned long get_fadump_area_size(void)
406{
407 unsigned long size = 0;
408
409 size += fw_dump.cpu_state_data_size;
410 size += fw_dump.hpte_region_size;
411 size += fw_dump.boot_memory_size;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000412 size += sizeof(struct fadump_crash_info_header);
413 size += sizeof(struct elfhdr); /* ELF core header.*/
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000414 size += sizeof(struct elf_phdr); /* place holder for cpu notes */
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000415 /* Program headers for crash memory regions. */
416 size += sizeof(struct elf_phdr) * (memblock_num_regions(memory) + 2);
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000417
418 size = PAGE_ALIGN(size);
419 return size;
420}
421
Mahesh Salgaonkarb71a6932018-04-10 19:11:16 +0530422static void __init fadump_reserve_crash_area(unsigned long base,
423 unsigned long size)
424{
425 struct memblock_region *reg;
426 unsigned long mstart, mend, msize;
427
428 for_each_memblock(memory, reg) {
429 mstart = max_t(unsigned long, base, reg->base);
430 mend = reg->base + reg->size;
431 mend = min(base + size, mend);
432
433 if (mstart < mend) {
434 msize = mend - mstart;
435 memblock_reserve(mstart, msize);
436 pr_info("Reserved %ldMB of memory at %#016lx for saving crash dump\n",
437 (msize >> 20), mstart);
438 }
439 }
440}
441
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000442int __init fadump_reserve_mem(void)
443{
444 unsigned long base, size, memory_boundary;
445
446 if (!fw_dump.fadump_enabled)
447 return 0;
448
449 if (!fw_dump.fadump_supported) {
450 printk(KERN_INFO "Firmware-assisted dump is not supported on"
451 " this hardware\n");
452 fw_dump.fadump_enabled = 0;
453 return 0;
454 }
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000455 /*
456 * Initialize boot memory size
457 * If dump is active then we have already calculated the size during
458 * first kernel.
459 */
460 if (fdm_active)
Hari Bathini408cddd2014-10-01 12:32:30 +0530461 fw_dump.boot_memory_size = be64_to_cpu(fdm_active->rmr_region.source_len);
Mahesh Salgaonkara4e92ce2018-08-20 13:47:17 +0530462 else {
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000463 fw_dump.boot_memory_size = fadump_calculate_reserve_size();
Mahesh Salgaonkara4e92ce2018-08-20 13:47:17 +0530464#ifdef CONFIG_CMA
465 if (!fw_dump.nocma)
466 fw_dump.boot_memory_size =
467 ALIGN(fw_dump.boot_memory_size,
468 FADUMP_CMA_ALIGNMENT);
469#endif
470 }
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000471
472 /*
473 * Calculate the memory boundary.
474 * If memory_limit is less than actual memory boundary then reserve
475 * the memory for fadump beyond the memory_limit and adjust the
476 * memory_limit accordingly, so that the running kernel can run with
477 * specified memory_limit.
478 */
479 if (memory_limit && memory_limit < memblock_end_of_DRAM()) {
480 size = get_fadump_area_size();
481 if ((memory_limit + size) < memblock_end_of_DRAM())
482 memory_limit += size;
483 else
484 memory_limit = memblock_end_of_DRAM();
485 printk(KERN_INFO "Adjusted memory_limit for firmware-assisted"
Suzuki Poulosea84fcd42012-08-21 01:42:33 +0000486 " dump, now %#016llx\n", memory_limit);
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000487 }
488 if (memory_limit)
489 memory_boundary = memory_limit;
490 else
491 memory_boundary = memblock_end_of_DRAM();
492
493 if (fw_dump.dump_active) {
Mahesh Salgaonkarb71a6932018-04-10 19:11:16 +0530494 pr_info("Firmware-assisted dump is active.\n");
495
Hari Bathini85975382018-04-10 19:11:31 +0530496#ifdef CONFIG_HUGETLB_PAGE
497 /*
498 * FADump capture kernel doesn't care much about hugepages.
499 * In fact, handling hugepages in capture kernel is asking for
500 * trouble. So, disable HugeTLB support when fadump is active.
501 */
502 hugetlb_disabled = true;
503#endif
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000504 /*
505 * If last boot has crashed then reserve all the memory
506 * above boot_memory_size so that we don't touch it until
507 * dump is written to disk by userspace tool. This memory
508 * will be released for general use once the dump is saved.
509 */
510 base = fw_dump.boot_memory_size;
511 size = memory_boundary - base;
Mahesh Salgaonkarb71a6932018-04-10 19:11:16 +0530512 fadump_reserve_crash_area(base, size);
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000513
514 fw_dump.fadumphdr_addr =
Hari Bathini408cddd2014-10-01 12:32:30 +0530515 be64_to_cpu(fdm_active->rmr_region.destination_address) +
516 be64_to_cpu(fdm_active->rmr_region.source_len);
Mahesh Salgaonkara4e92ce2018-08-20 13:47:17 +0530517 pr_debug("fadumphdr_addr = %pa\n", &fw_dump.fadumphdr_addr);
518 fw_dump.reserve_dump_area_start = base;
519 fw_dump.reserve_dump_area_size = size;
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000520 } else {
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000521 size = get_fadump_area_size();
Hari Bathinif6e6bed2017-03-17 02:35:26 +0530522
523 /*
524 * Reserve memory at an offset closer to bottom of the RAM to
525 * minimize the impact of memory hot-remove operation. We can't
526 * use memblock_find_in_range() here since it doesn't allocate
527 * from bottom to top.
528 */
529 for (base = fw_dump.boot_memory_size;
530 base <= (memory_boundary - size);
531 base += size) {
532 if (memblock_is_region_memory(base, size) &&
533 !memblock_is_region_reserved(base, size))
534 break;
535 }
536 if ((base > (memory_boundary - size)) ||
537 memblock_reserve(base, size)) {
538 pr_err("Failed to reserve memory\n");
539 return 0;
540 }
541
542 pr_info("Reserved %ldMB of memory at %ldMB for firmware-"
543 "assisted dump (System RAM: %ldMB)\n",
544 (unsigned long)(size >> 20),
545 (unsigned long)(base >> 20),
546 (unsigned long)(memblock_phys_mem_size() >> 20));
Hari Bathinif6e6bed2017-03-17 02:35:26 +0530547
Mahesh Salgaonkara4e92ce2018-08-20 13:47:17 +0530548 fw_dump.reserve_dump_area_start = base;
549 fw_dump.reserve_dump_area_size = size;
550 return fadump_cma_init();
551 }
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000552 return 1;
553}
554
Srikar Dronamraju1e76609c2016-10-07 16:59:21 -0700555unsigned long __init arch_reserved_kernel_pages(void)
556{
557 return memblock_reserved_size() / PAGE_SIZE;
558}
559
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000560/* Look for fadump= cmdline option. */
561static int __init early_fadump_param(char *p)
562{
563 if (!p)
564 return 1;
565
566 if (strncmp(p, "on", 2) == 0)
567 fw_dump.fadump_enabled = 1;
568 else if (strncmp(p, "off", 3) == 0)
569 fw_dump.fadump_enabled = 0;
Mahesh Salgaonkara4e92ce2018-08-20 13:47:17 +0530570 else if (strncmp(p, "nocma", 5) == 0) {
571 fw_dump.fadump_enabled = 1;
572 fw_dump.nocma = 1;
573 }
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000574
575 return 0;
576}
577early_param("fadump", early_fadump_param);
578
Hari Bathini81d9eca2017-05-22 15:04:23 +0530579/*
580 * Look for fadump_reserve_mem= cmdline option
581 * TODO: Remove references to 'fadump_reserve_mem=' parameter,
582 * the sooner 'crashkernel=' parameter is accustomed to.
583 */
584static int __init early_fadump_reserve_mem(char *p)
585{
586 if (p)
587 fw_dump.reserve_bootvar = memparse(p, &p);
588 return 0;
589}
590early_param("fadump_reserve_mem", early_fadump_reserve_mem);
591
Hari Bathini0226e552019-09-11 20:18:14 +0530592static int register_fw_dump(struct rtas_fadump_mem_struct *fdm)
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000593{
Michal Suchanek98b8cd72017-05-27 17:46:15 +0200594 int rc, err;
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000595 unsigned int wait_time;
596
597 pr_debug("Registering for firmware-assisted kernel dump...\n");
598
599 /* TODO: Add upper time limit for the delay */
600 do {
601 rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
602 FADUMP_REGISTER, fdm,
Hari Bathini0226e552019-09-11 20:18:14 +0530603 sizeof(struct rtas_fadump_mem_struct));
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000604
605 wait_time = rtas_busy_delay_time(rc);
606 if (wait_time)
607 mdelay(wait_time);
608
609 } while (wait_time);
610
Michal Suchanek98b8cd72017-05-27 17:46:15 +0200611 err = -EIO;
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000612 switch (rc) {
Michal Suchanek98b8cd72017-05-27 17:46:15 +0200613 default:
614 pr_err("Failed to register. Unknown Error(%d).\n", rc);
615 break;
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000616 case -1:
617 printk(KERN_ERR "Failed to register firmware-assisted kernel"
618 " dump. Hardware Error(%d).\n", rc);
619 break;
620 case -3:
Hari Bathini7f0ad112019-09-11 20:16:52 +0530621 if (!is_fadump_boot_mem_contiguous())
Mahesh Salgaonkarf86593b2018-08-20 13:47:24 +0530622 pr_err("Can't have holes in boot memory area while registering fadump\n");
Hari Bathini7f0ad112019-09-11 20:16:52 +0530623 else if (!is_fadump_reserved_mem_contiguous())
Mahesh Salgaonkarf86593b2018-08-20 13:47:24 +0530624 pr_err("Can't have holes in reserved memory area while"
625 " registering fadump\n");
Hari Bathinia5a05b92017-06-01 22:52:10 +0530626
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000627 printk(KERN_ERR "Failed to register firmware-assisted kernel"
628 " dump. Parameter Error(%d).\n", rc);
Michal Suchanek98b8cd72017-05-27 17:46:15 +0200629 err = -EINVAL;
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000630 break;
631 case -9:
632 printk(KERN_ERR "firmware-assisted kernel dump is already "
633 " registered.");
634 fw_dump.dump_registered = 1;
Michal Suchanek98b8cd72017-05-27 17:46:15 +0200635 err = -EEXIST;
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000636 break;
637 case 0:
638 printk(KERN_INFO "firmware-assisted kernel dump registration"
639 " is successful\n");
640 fw_dump.dump_registered = 1;
Michal Suchanek98b8cd72017-05-27 17:46:15 +0200641 err = 0;
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000642 break;
643 }
Michal Suchanek98b8cd72017-05-27 17:46:15 +0200644 return err;
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000645}
646
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000647void crash_fadump(struct pt_regs *regs, const char *str)
648{
649 struct fadump_crash_info_header *fdh = NULL;
Mahesh Salgaonkarf2a5e8f2016-10-24 23:51:51 +0530650 int old_cpu, this_cpu;
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000651
Nicholas Piggin6fcd6ba2017-07-19 16:59:11 +1000652 if (!should_fadump_crash())
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000653 return;
654
Mahesh Salgaonkarf2a5e8f2016-10-24 23:51:51 +0530655 /*
656 * old_cpu == -1 means this is the first CPU which has come here,
657 * go ahead and trigger fadump.
658 *
659 * old_cpu != -1 means some other CPU has already on it's way
660 * to trigger fadump, just keep looping here.
661 */
662 this_cpu = smp_processor_id();
663 old_cpu = cmpxchg(&crashing_cpu, -1, this_cpu);
664
665 if (old_cpu != -1) {
666 /*
667 * We can't loop here indefinitely. Wait as long as fadump
668 * is in force. If we race with fadump un-registration this
669 * loop will break and then we go down to normal panic path
670 * and reboot. If fadump is in force the first crashing
671 * cpu will definitely trigger fadump.
672 */
673 while (fw_dump.dump_registered)
674 cpu_relax();
675 return;
676 }
677
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000678 fdh = __va(fw_dump.fadumphdr_addr);
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000679 fdh->crashing_cpu = crashing_cpu;
680 crash_save_vmcoreinfo();
681
682 if (regs)
683 fdh->regs = *regs;
684 else
685 ppc_save_regs(&fdh->regs);
686
Rasmus Villemoesa0512162016-01-20 15:00:13 -0800687 fdh->online_mask = *cpu_online_mask;
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000688
689 /* Call ibm,os-term rtas call to trigger firmware assisted dump */
690 rtas_os_term((char *)str);
691}
692
693#define GPR_MASK 0xffffff0000000000
694static inline int fadump_gpr_index(u64 id)
695{
696 int i = -1;
697 char str[3];
698
Hari Bathini0226e552019-09-11 20:18:14 +0530699 if ((id & GPR_MASK) == fadump_str_to_u64("GPR")) {
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000700 /* get the digits at the end */
701 id &= ~GPR_MASK;
702 id >>= 24;
703 str[2] = '\0';
704 str[1] = id & 0xff;
705 str[0] = (id >> 8) & 0xff;
706 sscanf(str, "%d", &i);
707 if (i > 31)
708 i = -1;
709 }
710 return i;
711}
712
713static inline void fadump_set_regval(struct pt_regs *regs, u64 reg_id,
714 u64 reg_val)
715{
716 int i;
717
718 i = fadump_gpr_index(reg_id);
719 if (i >= 0)
720 regs->gpr[i] = (unsigned long)reg_val;
Hari Bathini0226e552019-09-11 20:18:14 +0530721 else if (reg_id == fadump_str_to_u64("NIA"))
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000722 regs->nip = (unsigned long)reg_val;
Hari Bathini0226e552019-09-11 20:18:14 +0530723 else if (reg_id == fadump_str_to_u64("MSR"))
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000724 regs->msr = (unsigned long)reg_val;
Hari Bathini0226e552019-09-11 20:18:14 +0530725 else if (reg_id == fadump_str_to_u64("CTR"))
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000726 regs->ctr = (unsigned long)reg_val;
Hari Bathini0226e552019-09-11 20:18:14 +0530727 else if (reg_id == fadump_str_to_u64("LR"))
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000728 regs->link = (unsigned long)reg_val;
Hari Bathini0226e552019-09-11 20:18:14 +0530729 else if (reg_id == fadump_str_to_u64("XER"))
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000730 regs->xer = (unsigned long)reg_val;
Hari Bathini0226e552019-09-11 20:18:14 +0530731 else if (reg_id == fadump_str_to_u64("CR"))
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000732 regs->ccr = (unsigned long)reg_val;
Hari Bathini0226e552019-09-11 20:18:14 +0530733 else if (reg_id == fadump_str_to_u64("DAR"))
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000734 regs->dar = (unsigned long)reg_val;
Hari Bathini0226e552019-09-11 20:18:14 +0530735 else if (reg_id == fadump_str_to_u64("DSISR"))
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000736 regs->dsisr = (unsigned long)reg_val;
737}
738
Hari Bathini0226e552019-09-11 20:18:14 +0530739static struct rtas_fadump_reg_entry*
740fadump_read_registers(struct rtas_fadump_reg_entry *reg_entry, struct pt_regs *regs)
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000741{
742 memset(regs, 0, sizeof(struct pt_regs));
743
Hari Bathini0226e552019-09-11 20:18:14 +0530744 while (be64_to_cpu(reg_entry->reg_id) != fadump_str_to_u64("CPUEND")) {
Hari Bathini408cddd2014-10-01 12:32:30 +0530745 fadump_set_regval(regs, be64_to_cpu(reg_entry->reg_id),
746 be64_to_cpu(reg_entry->reg_value));
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000747 reg_entry++;
748 }
749 reg_entry++;
750 return reg_entry;
751}
752
Hari Bathini7f0ad112019-09-11 20:16:52 +0530753u32 *fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs)
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000754{
755 struct elf_prstatus prstatus;
756
757 memset(&prstatus, 0, sizeof(prstatus));
758 /*
759 * FIXME: How do i get PID? Do I really need it?
760 * prstatus.pr_pid = ????
761 */
762 elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
Hari Bathini22bd0172017-05-08 15:56:24 -0700763 buf = append_elf_note(buf, CRASH_CORE_NOTE_NAME, NT_PRSTATUS,
764 &prstatus, sizeof(prstatus));
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000765 return buf;
766}
767
Hari Bathini7f0ad112019-09-11 20:16:52 +0530768void fadump_update_elfcore_header(char *bufp)
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000769{
770 struct elfhdr *elf;
771 struct elf_phdr *phdr;
772
773 elf = (struct elfhdr *)bufp;
774 bufp += sizeof(struct elfhdr);
775
776 /* First note is a place holder for cpu notes info. */
777 phdr = (struct elf_phdr *)bufp;
778
779 if (phdr->p_type == PT_NOTE) {
Hari Bathini961cf262019-09-11 20:16:36 +0530780 phdr->p_paddr = __pa(fw_dump.cpu_notes_buf_vaddr);
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000781 phdr->p_offset = phdr->p_paddr;
782 phdr->p_filesz = fw_dump.cpu_notes_buf_size;
783 phdr->p_memsz = fw_dump.cpu_notes_buf_size;
784 }
785 return;
786}
787
Hari Bathini961cf262019-09-11 20:16:36 +0530788static void *fadump_alloc_buffer(unsigned long size)
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000789{
Hari Bathini72aa6512019-09-11 20:17:56 +0530790 unsigned long count, i;
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000791 struct page *page;
Hari Bathini72aa6512019-09-11 20:17:56 +0530792 void *vaddr;
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000793
Hari Bathini72aa6512019-09-11 20:17:56 +0530794 vaddr = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000795 if (!vaddr)
796 return NULL;
797
Hari Bathini72aa6512019-09-11 20:17:56 +0530798 count = PAGE_ALIGN(size) / PAGE_SIZE;
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000799 page = virt_to_page(vaddr);
800 for (i = 0; i < count; i++)
Hari Bathini72aa6512019-09-11 20:17:56 +0530801 mark_page_reserved(page + i);
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000802 return vaddr;
803}
804
Hari Bathini961cf262019-09-11 20:16:36 +0530805static void fadump_free_buffer(unsigned long vaddr, unsigned long size)
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000806{
Hari Bathini72aa6512019-09-11 20:17:56 +0530807 free_reserved_area((void *)vaddr, (void *)(vaddr + size), -1, NULL);
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000808}
809
Hari Bathini7f0ad112019-09-11 20:16:52 +0530810s32 fadump_setup_cpu_notes_buf(u32 num_cpus)
Hari Bathini961cf262019-09-11 20:16:36 +0530811{
812 /* Allocate buffer to hold cpu crash notes. */
813 fw_dump.cpu_notes_buf_size = num_cpus * sizeof(note_buf_t);
814 fw_dump.cpu_notes_buf_size = PAGE_ALIGN(fw_dump.cpu_notes_buf_size);
815 fw_dump.cpu_notes_buf_vaddr =
816 (unsigned long)fadump_alloc_buffer(fw_dump.cpu_notes_buf_size);
817 if (!fw_dump.cpu_notes_buf_vaddr) {
818 pr_err("Failed to allocate %ld bytes for CPU notes buffer\n",
819 fw_dump.cpu_notes_buf_size);
820 return -ENOMEM;
821 }
822
823 pr_debug("Allocated buffer for cpu notes of size %ld at 0x%lx\n",
824 fw_dump.cpu_notes_buf_size,
825 fw_dump.cpu_notes_buf_vaddr);
826 return 0;
827}
828
Hari Bathini7f0ad112019-09-11 20:16:52 +0530829void fadump_free_cpu_notes_buf(void)
Hari Bathini961cf262019-09-11 20:16:36 +0530830{
831 if (!fw_dump.cpu_notes_buf_vaddr)
832 return;
833
834 fadump_free_buffer(fw_dump.cpu_notes_buf_vaddr,
835 fw_dump.cpu_notes_buf_size);
836 fw_dump.cpu_notes_buf_vaddr = 0;
837 fw_dump.cpu_notes_buf_size = 0;
838}
839
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000840/*
841 * Read CPU state dump data and convert it into ELF notes.
842 * The CPU dump starts with magic number "REGSAVE". NumCpusOffset should be
843 * used to access the data to allow for additional fields to be added without
844 * affecting compatibility. Each list of registers for a CPU starts with
845 * "CPUSTRT" and ends with "CPUEND". Each register entry is of 16 bytes,
846 * 8 Byte ASCII identifier and 8 Byte register value. The register entry
847 * with identifier "CPUSTRT" and "CPUEND" contains 4 byte cpu id as part
848 * of register value. For more details refer to PAPR document.
849 *
850 * Only for the crashing cpu we ignore the CPU dump data and get exact
851 * state from fadump crash info structure populated by first kernel at the
852 * time of crash.
853 */
Hari Bathini0226e552019-09-11 20:18:14 +0530854static int __init fadump_build_cpu_notes(const struct rtas_fadump_mem_struct *fdm)
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000855{
Hari Bathini0226e552019-09-11 20:18:14 +0530856 struct rtas_fadump_reg_save_area_header *reg_header;
857 struct rtas_fadump_reg_entry *reg_entry;
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000858 struct fadump_crash_info_header *fdh = NULL;
859 void *vaddr;
860 unsigned long addr;
861 u32 num_cpus, *note_buf;
862 struct pt_regs regs;
863 int i, rc = 0, cpu = 0;
864
865 if (!fdm->cpu_state_data.bytes_dumped)
866 return -EINVAL;
867
Hari Bathini408cddd2014-10-01 12:32:30 +0530868 addr = be64_to_cpu(fdm->cpu_state_data.destination_address);
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000869 vaddr = __va(addr);
870
871 reg_header = vaddr;
Hari Bathini0226e552019-09-11 20:18:14 +0530872 if (be64_to_cpu(reg_header->magic_number) !=
873 fadump_str_to_u64("REGSAVE")) {
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000874 printk(KERN_ERR "Unable to read register save area.\n");
875 return -ENOENT;
876 }
877 pr_debug("--------CPU State Data------------\n");
Hari Bathini408cddd2014-10-01 12:32:30 +0530878 pr_debug("Magic Number: %llx\n", be64_to_cpu(reg_header->magic_number));
879 pr_debug("NumCpuOffset: %x\n", be32_to_cpu(reg_header->num_cpu_offset));
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000880
Hari Bathini408cddd2014-10-01 12:32:30 +0530881 vaddr += be32_to_cpu(reg_header->num_cpu_offset);
882 num_cpus = be32_to_cpu(*((__be32 *)(vaddr)));
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000883 pr_debug("NumCpus : %u\n", num_cpus);
884 vaddr += sizeof(u32);
Hari Bathini0226e552019-09-11 20:18:14 +0530885 reg_entry = (struct rtas_fadump_reg_entry *)vaddr;
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000886
Hari Bathini961cf262019-09-11 20:16:36 +0530887 rc = fadump_setup_cpu_notes_buf(num_cpus);
888 if (rc != 0)
889 return rc;
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000890
Hari Bathini961cf262019-09-11 20:16:36 +0530891 note_buf = (u32 *)fw_dump.cpu_notes_buf_vaddr;
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000892
893 if (fw_dump.fadumphdr_addr)
894 fdh = __va(fw_dump.fadumphdr_addr);
895
896 for (i = 0; i < num_cpus; i++) {
Hari Bathini0226e552019-09-11 20:18:14 +0530897 if (be64_to_cpu(reg_entry->reg_id) != fadump_str_to_u64("CPUSTRT")) {
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000898 printk(KERN_ERR "Unable to read CPU state data\n");
899 rc = -ENOENT;
900 goto error_out;
901 }
902 /* Lower 4 bytes of reg_value contains logical cpu id */
Hari Bathini0226e552019-09-11 20:18:14 +0530903 cpu = be64_to_cpu(reg_entry->reg_value) & RTAS_FADUMP_CPU_ID_MASK;
Rasmus Villemoesa0512162016-01-20 15:00:13 -0800904 if (fdh && !cpumask_test_cpu(cpu, &fdh->online_mask)) {
Hari Bathini0226e552019-09-11 20:18:14 +0530905 RTAS_FADUMP_SKIP_TO_NEXT_CPU(reg_entry);
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000906 continue;
907 }
908 pr_debug("Reading register data for cpu %d...\n", cpu);
909 if (fdh && fdh->crashing_cpu == cpu) {
910 regs = fdh->regs;
911 note_buf = fadump_regs_to_elf_notes(note_buf, &regs);
Hari Bathini0226e552019-09-11 20:18:14 +0530912 RTAS_FADUMP_SKIP_TO_NEXT_CPU(reg_entry);
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000913 } else {
914 reg_entry++;
915 reg_entry = fadump_read_registers(reg_entry, &regs);
916 note_buf = fadump_regs_to_elf_notes(note_buf, &regs);
917 }
918 }
Hari Bathini22bd0172017-05-08 15:56:24 -0700919 final_note(note_buf);
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000920
Rickard Strandqvistb717d982014-05-23 00:03:16 +0200921 if (fdh) {
922 pr_debug("Updating elfcore header (%llx) with cpu notes\n",
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000923 fdh->elfcorehdr_addr);
Rickard Strandqvistb717d982014-05-23 00:03:16 +0200924 fadump_update_elfcore_header((char *)__va(fdh->elfcorehdr_addr));
925 }
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000926 return 0;
927
928error_out:
Hari Bathini961cf262019-09-11 20:16:36 +0530929 fadump_free_cpu_notes_buf();
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000930 return rc;
931
932}
933
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000934/*
935 * Validate and process the dump data stored by firmware before exporting
936 * it through '/proc/vmcore'.
937 */
Hari Bathini0226e552019-09-11 20:18:14 +0530938static int __init process_fadump(const struct rtas_fadump_mem_struct *fdm_active)
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000939{
940 struct fadump_crash_info_header *fdh;
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000941 int rc = 0;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000942
943 if (!fdm_active || !fw_dump.fadumphdr_addr)
944 return -EINVAL;
945
946 /* Check if the dump data is valid. */
Hari Bathini0226e552019-09-11 20:18:14 +0530947 if ((be16_to_cpu(fdm_active->header.dump_status_flag) == RTAS_FADUMP_ERROR_FLAG) ||
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000948 (fdm_active->cpu_state_data.error_flags != 0) ||
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000949 (fdm_active->rmr_region.error_flags != 0)) {
950 printk(KERN_ERR "Dump taken by platform is not valid\n");
951 return -EINVAL;
952 }
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000953 if ((fdm_active->rmr_region.bytes_dumped !=
954 fdm_active->rmr_region.source_len) ||
955 !fdm_active->cpu_state_data.bytes_dumped) {
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000956 printk(KERN_ERR "Dump taken by platform is incomplete\n");
957 return -EINVAL;
958 }
959
960 /* Validate the fadump crash info header */
961 fdh = __va(fw_dump.fadumphdr_addr);
962 if (fdh->magic_number != FADUMP_CRASH_INFO_MAGIC) {
963 printk(KERN_ERR "Crash info header is not valid.\n");
964 return -EINVAL;
965 }
966
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000967 rc = fadump_build_cpu_notes(fdm_active);
968 if (rc)
969 return rc;
970
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000971 /*
972 * We are done validating dump info and elfcore header is now ready
973 * to be exported. set elfcorehdr_addr so that vmcore module will
974 * export the elfcore header through '/proc/vmcore'.
975 */
976 elfcorehdr_addr = fdh->elfcorehdr_addr;
977
978 return 0;
979}
980
Hari Bathini1bd6a1c2018-08-07 02:12:45 +0530981static void free_crash_memory_ranges(void)
982{
983 kfree(crash_memory_ranges);
984 crash_memory_ranges = NULL;
985 crash_memory_ranges_size = 0;
986 max_crash_mem_ranges = 0;
987}
988
989/*
990 * Allocate or reallocate crash memory ranges array in incremental units
991 * of PAGE_SIZE.
992 */
993static int allocate_crash_memory_ranges(void)
994{
995 struct fad_crash_memory_ranges *new_array;
996 u64 new_size;
997
998 new_size = crash_memory_ranges_size + PAGE_SIZE;
999 pr_debug("Allocating %llu bytes of memory for crash memory ranges\n",
1000 new_size);
1001
1002 new_array = krealloc(crash_memory_ranges, new_size, GFP_KERNEL);
1003 if (new_array == NULL) {
1004 pr_err("Insufficient memory for setting up crash memory ranges\n");
1005 free_crash_memory_ranges();
1006 return -ENOMEM;
1007 }
1008
1009 crash_memory_ranges = new_array;
1010 crash_memory_ranges_size = new_size;
1011 max_crash_mem_ranges = (new_size /
1012 sizeof(struct fad_crash_memory_ranges));
1013 return 0;
1014}
1015
1016static inline int fadump_add_crash_memory(unsigned long long base,
1017 unsigned long long end)
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001018{
Hari Bathiniced1bf52018-08-07 02:12:54 +05301019 u64 start, size;
1020 bool is_adjacent = false;
1021
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001022 if (base == end)
Hari Bathini1bd6a1c2018-08-07 02:12:45 +05301023 return 0;
1024
Hari Bathiniced1bf52018-08-07 02:12:54 +05301025 /*
1026 * Fold adjacent memory ranges to bring down the memory ranges/
1027 * PT_LOAD segments count.
1028 */
1029 if (crash_mem_ranges) {
1030 start = crash_memory_ranges[crash_mem_ranges - 1].base;
1031 size = crash_memory_ranges[crash_mem_ranges - 1].size;
Hari Bathini1bd6a1c2018-08-07 02:12:45 +05301032
Hari Bathiniced1bf52018-08-07 02:12:54 +05301033 if ((start + size) == base)
1034 is_adjacent = true;
1035 }
1036 if (!is_adjacent) {
1037 /* resize the array on reaching the limit */
1038 if (crash_mem_ranges == max_crash_mem_ranges) {
1039 int ret;
1040
1041 ret = allocate_crash_memory_ranges();
1042 if (ret)
1043 return ret;
1044 }
1045
1046 start = base;
1047 crash_memory_ranges[crash_mem_ranges].base = start;
1048 crash_mem_ranges++;
Hari Bathini1bd6a1c2018-08-07 02:12:45 +05301049 }
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001050
Hari Bathiniced1bf52018-08-07 02:12:54 +05301051 crash_memory_ranges[crash_mem_ranges - 1].size = (end - start);
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001052 pr_debug("crash_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n",
Hari Bathiniced1bf52018-08-07 02:12:54 +05301053 (crash_mem_ranges - 1), start, end - 1, (end - start));
Hari Bathini1bd6a1c2018-08-07 02:12:45 +05301054 return 0;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001055}
1056
Hari Bathini1bd6a1c2018-08-07 02:12:45 +05301057static int fadump_exclude_reserved_area(unsigned long long start,
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001058 unsigned long long end)
1059{
1060 unsigned long long ra_start, ra_end;
Hari Bathini1bd6a1c2018-08-07 02:12:45 +05301061 int ret = 0;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001062
1063 ra_start = fw_dump.reserve_dump_area_start;
1064 ra_end = ra_start + fw_dump.reserve_dump_area_size;
1065
1066 if ((ra_start < end) && (ra_end > start)) {
1067 if ((start < ra_start) && (end > ra_end)) {
Hari Bathini1bd6a1c2018-08-07 02:12:45 +05301068 ret = fadump_add_crash_memory(start, ra_start);
1069 if (ret)
1070 return ret;
1071
1072 ret = fadump_add_crash_memory(ra_end, end);
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001073 } else if (start < ra_start) {
Hari Bathini1bd6a1c2018-08-07 02:12:45 +05301074 ret = fadump_add_crash_memory(start, ra_start);
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001075 } else if (ra_end < end) {
Hari Bathini1bd6a1c2018-08-07 02:12:45 +05301076 ret = fadump_add_crash_memory(ra_end, end);
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001077 }
1078 } else
Hari Bathini1bd6a1c2018-08-07 02:12:45 +05301079 ret = fadump_add_crash_memory(start, end);
1080
1081 return ret;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001082}
1083
1084static int fadump_init_elfcore_header(char *bufp)
1085{
1086 struct elfhdr *elf;
1087
1088 elf = (struct elfhdr *) bufp;
1089 bufp += sizeof(struct elfhdr);
1090 memcpy(elf->e_ident, ELFMAG, SELFMAG);
1091 elf->e_ident[EI_CLASS] = ELF_CLASS;
1092 elf->e_ident[EI_DATA] = ELF_DATA;
1093 elf->e_ident[EI_VERSION] = EV_CURRENT;
1094 elf->e_ident[EI_OSABI] = ELF_OSABI;
1095 memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
1096 elf->e_type = ET_CORE;
1097 elf->e_machine = ELF_ARCH;
1098 elf->e_version = EV_CURRENT;
1099 elf->e_entry = 0;
1100 elf->e_phoff = sizeof(struct elfhdr);
1101 elf->e_shoff = 0;
Daniel Axtensd8bced22016-09-06 15:32:42 +10001102#if defined(_CALL_ELF)
1103 elf->e_flags = _CALL_ELF;
1104#else
1105 elf->e_flags = 0;
1106#endif
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001107 elf->e_ehsize = sizeof(struct elfhdr);
1108 elf->e_phentsize = sizeof(struct elf_phdr);
1109 elf->e_phnum = 0;
1110 elf->e_shentsize = 0;
1111 elf->e_shnum = 0;
1112 elf->e_shstrndx = 0;
1113
1114 return 0;
1115}
1116
1117/*
1118 * Traverse through memblock structure and setup crash memory ranges. These
1119 * ranges will be used create PT_LOAD program headers in elfcore header.
1120 */
Hari Bathini1bd6a1c2018-08-07 02:12:45 +05301121static int fadump_setup_crash_memory_ranges(void)
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001122{
1123 struct memblock_region *reg;
1124 unsigned long long start, end;
Hari Bathini1bd6a1c2018-08-07 02:12:45 +05301125 int ret;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001126
1127 pr_debug("Setup crash memory ranges.\n");
1128 crash_mem_ranges = 0;
Hari Bathiniced1bf52018-08-07 02:12:54 +05301129
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001130 /*
1131 * add the first memory chunk (RMA_START through boot_memory_size) as
1132 * a separate memory chunk. The reason is, at the time crash firmware
1133 * will move the content of this memory chunk to different location
1134 * specified during fadump registration. We need to create a separate
1135 * program header for this chunk with the correct offset.
1136 */
Hari Bathini1bd6a1c2018-08-07 02:12:45 +05301137 ret = fadump_add_crash_memory(RMA_START, fw_dump.boot_memory_size);
1138 if (ret)
1139 return ret;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001140
1141 for_each_memblock(memory, reg) {
1142 start = (unsigned long long)reg->base;
1143 end = start + (unsigned long long)reg->size;
Hari Bathinia77af5522017-06-01 22:50:38 +05301144
1145 /*
1146 * skip the first memory chunk that is already added (RMA_START
1147 * through boot_memory_size). This logic needs a relook if and
1148 * when RMA_START changes to a non-zero value.
1149 */
1150 BUILD_BUG_ON(RMA_START != 0);
1151 if (start < fw_dump.boot_memory_size) {
1152 if (end > fw_dump.boot_memory_size)
1153 start = fw_dump.boot_memory_size;
1154 else
1155 continue;
1156 }
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001157
1158 /* add this range excluding the reserved dump area. */
Hari Bathini1bd6a1c2018-08-07 02:12:45 +05301159 ret = fadump_exclude_reserved_area(start, end);
1160 if (ret)
1161 return ret;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001162 }
Hari Bathini1bd6a1c2018-08-07 02:12:45 +05301163
1164 return 0;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001165}
1166
Mahesh Salgaonkard34c5f22012-02-16 01:14:53 +00001167/*
1168 * If the given physical address falls within the boot memory region then
1169 * return the relocated address that points to the dump region reserved
1170 * for saving initial boot memory contents.
1171 */
1172static inline unsigned long fadump_relocate(unsigned long paddr)
1173{
1174 if (paddr > RMA_START && paddr < fw_dump.boot_memory_size)
Hari Bathini408cddd2014-10-01 12:32:30 +05301175 return be64_to_cpu(fdm.rmr_region.destination_address) + paddr;
Mahesh Salgaonkard34c5f22012-02-16 01:14:53 +00001176 else
1177 return paddr;
1178}
1179
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001180static int fadump_create_elfcore_headers(char *bufp)
1181{
1182 struct elfhdr *elf;
1183 struct elf_phdr *phdr;
1184 int i;
1185
1186 fadump_init_elfcore_header(bufp);
1187 elf = (struct elfhdr *)bufp;
1188 bufp += sizeof(struct elfhdr);
1189
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +00001190 /*
1191 * setup ELF PT_NOTE, place holder for cpu notes info. The notes info
1192 * will be populated during second kernel boot after crash. Hence
1193 * this PT_NOTE will always be the first elf note.
1194 *
1195 * NOTE: Any new ELF note addition should be placed after this note.
1196 */
1197 phdr = (struct elf_phdr *)bufp;
1198 bufp += sizeof(struct elf_phdr);
1199 phdr->p_type = PT_NOTE;
1200 phdr->p_flags = 0;
1201 phdr->p_vaddr = 0;
1202 phdr->p_align = 0;
1203
1204 phdr->p_offset = 0;
1205 phdr->p_paddr = 0;
1206 phdr->p_filesz = 0;
1207 phdr->p_memsz = 0;
1208
1209 (elf->e_phnum)++;
1210
Mahesh Salgaonkard34c5f22012-02-16 01:14:53 +00001211 /* setup ELF PT_NOTE for vmcoreinfo */
1212 phdr = (struct elf_phdr *)bufp;
1213 bufp += sizeof(struct elf_phdr);
1214 phdr->p_type = PT_NOTE;
1215 phdr->p_flags = 0;
1216 phdr->p_vaddr = 0;
1217 phdr->p_align = 0;
1218
1219 phdr->p_paddr = fadump_relocate(paddr_vmcoreinfo_note());
1220 phdr->p_offset = phdr->p_paddr;
Xunlei Pang5203f492017-07-12 14:33:17 -07001221 phdr->p_memsz = phdr->p_filesz = VMCOREINFO_NOTE_SIZE;
Mahesh Salgaonkard34c5f22012-02-16 01:14:53 +00001222
1223 /* Increment number of program headers. */
1224 (elf->e_phnum)++;
1225
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001226 /* setup PT_LOAD sections. */
1227
1228 for (i = 0; i < crash_mem_ranges; i++) {
1229 unsigned long long mbase, msize;
1230 mbase = crash_memory_ranges[i].base;
1231 msize = crash_memory_ranges[i].size;
1232
1233 if (!msize)
1234 continue;
1235
1236 phdr = (struct elf_phdr *)bufp;
1237 bufp += sizeof(struct elf_phdr);
1238 phdr->p_type = PT_LOAD;
1239 phdr->p_flags = PF_R|PF_W|PF_X;
1240 phdr->p_offset = mbase;
1241
1242 if (mbase == RMA_START) {
1243 /*
1244 * The entire RMA region will be moved by firmware
1245 * to the specified destination_address. Hence set
1246 * the correct offset.
1247 */
Hari Bathini408cddd2014-10-01 12:32:30 +05301248 phdr->p_offset = be64_to_cpu(fdm.rmr_region.destination_address);
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001249 }
1250
1251 phdr->p_paddr = mbase;
1252 phdr->p_vaddr = (unsigned long)__va(mbase);
1253 phdr->p_filesz = msize;
1254 phdr->p_memsz = msize;
1255 phdr->p_align = 0;
1256
1257 /* Increment number of program headers. */
1258 (elf->e_phnum)++;
1259 }
1260 return 0;
1261}
1262
1263static unsigned long init_fadump_header(unsigned long addr)
1264{
1265 struct fadump_crash_info_header *fdh;
1266
1267 if (!addr)
1268 return 0;
1269
1270 fw_dump.fadumphdr_addr = addr;
1271 fdh = __va(addr);
1272 addr += sizeof(struct fadump_crash_info_header);
1273
1274 memset(fdh, 0, sizeof(struct fadump_crash_info_header));
1275 fdh->magic_number = FADUMP_CRASH_INFO_MAGIC;
1276 fdh->elfcorehdr_addr = addr;
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +00001277 /* We will set the crashing cpu id in crash_fadump() during crash. */
Hari Bathini0226e552019-09-11 20:18:14 +05301278 fdh->crashing_cpu = FADUMP_CPU_UNKNOWN;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001279
1280 return addr;
1281}
1282
Michal Suchanek98b8cd72017-05-27 17:46:15 +02001283static int register_fadump(void)
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001284{
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001285 unsigned long addr;
1286 void *vaddr;
Hari Bathini1bd6a1c2018-08-07 02:12:45 +05301287 int ret;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001288
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001289 /*
1290 * If no memory is reserved then we can not register for firmware-
1291 * assisted dump.
1292 */
1293 if (!fw_dump.reserve_dump_area_size)
Michal Suchanek98b8cd72017-05-27 17:46:15 +02001294 return -ENODEV;
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001295
Hari Bathini1bd6a1c2018-08-07 02:12:45 +05301296 ret = fadump_setup_crash_memory_ranges();
1297 if (ret)
1298 return ret;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001299
Hari Bathini408cddd2014-10-01 12:32:30 +05301300 addr = be64_to_cpu(fdm.rmr_region.destination_address) + be64_to_cpu(fdm.rmr_region.source_len);
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001301 /* Initialize fadump crash info header. */
1302 addr = init_fadump_header(addr);
1303 vaddr = __va(addr);
1304
1305 pr_debug("Creating ELF core headers at %#016lx\n", addr);
1306 fadump_create_elfcore_headers(vaddr);
1307
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001308 /* register the future kernel dump with firmware. */
Michal Suchanek98b8cd72017-05-27 17:46:15 +02001309 return register_fw_dump(&fdm);
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001310}
1311
Hari Bathini0226e552019-09-11 20:18:14 +05301312static int fadump_unregister_dump(struct rtas_fadump_mem_struct *fdm)
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001313{
1314 int rc = 0;
1315 unsigned int wait_time;
1316
1317 pr_debug("Un-register firmware-assisted dump\n");
1318
1319 /* TODO: Add upper time limit for the delay */
1320 do {
1321 rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
1322 FADUMP_UNREGISTER, fdm,
Hari Bathini0226e552019-09-11 20:18:14 +05301323 sizeof(struct rtas_fadump_mem_struct));
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001324
1325 wait_time = rtas_busy_delay_time(rc);
1326 if (wait_time)
1327 mdelay(wait_time);
1328 } while (wait_time);
1329
1330 if (rc) {
1331 printk(KERN_ERR "Failed to un-register firmware-assisted dump."
1332 " unexpected error(%d).\n", rc);
1333 return rc;
1334 }
1335 fw_dump.dump_registered = 0;
1336 return 0;
1337}
1338
Hari Bathini0226e552019-09-11 20:18:14 +05301339static int fadump_invalidate_dump(const struct rtas_fadump_mem_struct *fdm)
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001340{
1341 int rc = 0;
1342 unsigned int wait_time;
1343
1344 pr_debug("Invalidating firmware-assisted dump registration\n");
1345
1346 /* TODO: Add upper time limit for the delay */
1347 do {
1348 rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
1349 FADUMP_INVALIDATE, fdm,
Hari Bathini0226e552019-09-11 20:18:14 +05301350 sizeof(struct rtas_fadump_mem_struct));
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001351
1352 wait_time = rtas_busy_delay_time(rc);
1353 if (wait_time)
1354 mdelay(wait_time);
1355 } while (wait_time);
1356
1357 if (rc) {
Colin Ian King4a037492016-06-27 12:07:41 +01001358 pr_err("Failed to invalidate firmware-assisted dump registration. Unexpected error (%d).\n", rc);
Michael Ellermanb5b1cfc2016-07-05 23:45:56 +10001359 return rc;
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001360 }
1361 fw_dump.dump_active = 0;
1362 fdm_active = NULL;
1363 return 0;
1364}
1365
1366void fadump_cleanup(void)
1367{
1368 /* Invalidate the registration only if dump is active. */
1369 if (fw_dump.dump_active) {
Mahesh Salgaonkara4e92ce2018-08-20 13:47:17 +05301370 /* pass the same memory dump structure provided by platform */
1371 fadump_invalidate_dump(fdm_active);
Mahesh Salgaonkar722cde72018-04-27 11:53:18 +05301372 } else if (fw_dump.dump_registered) {
1373 /* Un-register Firmware-assisted dump if it was registered. */
1374 fadump_unregister_dump(&fdm);
Hari Bathini1bd6a1c2018-08-07 02:12:45 +05301375 free_crash_memory_ranges();
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001376 }
1377}
1378
Hari Bathini68fa6472017-06-02 01:10:10 +05301379static void fadump_free_reserved_memory(unsigned long start_pfn,
1380 unsigned long end_pfn)
1381{
1382 unsigned long pfn;
1383 unsigned long time_limit = jiffies + HZ;
1384
1385 pr_info("freeing reserved memory (0x%llx - 0x%llx)\n",
1386 PFN_PHYS(start_pfn), PFN_PHYS(end_pfn));
1387
1388 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
1389 free_reserved_page(pfn_to_page(pfn));
1390
1391 if (time_after(jiffies, time_limit)) {
1392 cond_resched();
1393 time_limit = jiffies + HZ;
1394 }
1395 }
1396}
1397
1398/*
1399 * Skip memory holes and free memory that was actually reserved.
1400 */
1401static void fadump_release_reserved_area(unsigned long start, unsigned long end)
1402{
1403 struct memblock_region *reg;
1404 unsigned long tstart, tend;
1405 unsigned long start_pfn = PHYS_PFN(start);
1406 unsigned long end_pfn = PHYS_PFN(end);
1407
1408 for_each_memblock(memory, reg) {
1409 tstart = max(start_pfn, memblock_region_memory_base_pfn(reg));
1410 tend = min(end_pfn, memblock_region_memory_end_pfn(reg));
1411 if (tstart < tend) {
1412 fadump_free_reserved_memory(tstart, tend);
1413
1414 if (tend == end_pfn)
1415 break;
1416
1417 start_pfn = tend + 1;
1418 }
1419 }
1420}
1421
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001422/*
1423 * Release the memory that was reserved in early boot to preserve the memory
1424 * contents. The released memory will be available for general use.
1425 */
1426static void fadump_release_memory(unsigned long begin, unsigned long end)
1427{
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001428 unsigned long ra_start, ra_end;
1429
1430 ra_start = fw_dump.reserve_dump_area_start;
1431 ra_end = ra_start + fw_dump.reserve_dump_area_size;
1432
Hari Bathini68fa6472017-06-02 01:10:10 +05301433 /*
1434 * exclude the dump reserve area. Will reuse it for next
1435 * fadump registration.
1436 */
1437 if (begin < ra_end && end > ra_start) {
1438 if (begin < ra_start)
1439 fadump_release_reserved_area(begin, ra_start);
1440 if (end > ra_end)
1441 fadump_release_reserved_area(ra_end, end);
1442 } else
1443 fadump_release_reserved_area(begin, end);
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001444}
1445
1446static void fadump_invalidate_release_mem(void)
1447{
1448 unsigned long reserved_area_start, reserved_area_end;
1449 unsigned long destination_address;
1450
1451 mutex_lock(&fadump_mutex);
1452 if (!fw_dump.dump_active) {
1453 mutex_unlock(&fadump_mutex);
1454 return;
1455 }
1456
Hari Bathini408cddd2014-10-01 12:32:30 +05301457 destination_address = be64_to_cpu(fdm_active->cpu_state_data.destination_address);
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001458 fadump_cleanup();
1459 mutex_unlock(&fadump_mutex);
1460
1461 /*
1462 * Save the current reserved memory bounds we will require them
1463 * later for releasing the memory for general use.
1464 */
1465 reserved_area_start = fw_dump.reserve_dump_area_start;
1466 reserved_area_end = reserved_area_start +
1467 fw_dump.reserve_dump_area_size;
1468 /*
1469 * Setup reserve_dump_area_start and its size so that we can
1470 * reuse this reserved memory for Re-registration.
1471 */
1472 fw_dump.reserve_dump_area_start = destination_address;
1473 fw_dump.reserve_dump_area_size = get_fadump_area_size();
1474
1475 fadump_release_memory(reserved_area_start, reserved_area_end);
Hari Bathini961cf262019-09-11 20:16:36 +05301476 fadump_free_cpu_notes_buf();
1477
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001478 /* Initialize the kernel dump memory structure for FAD registration. */
1479 init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
1480}
1481
1482static ssize_t fadump_release_memory_store(struct kobject *kobj,
1483 struct kobj_attribute *attr,
1484 const char *buf, size_t count)
1485{
Michal Suchanekdcdc4672017-06-26 16:06:01 +02001486 int input = -1;
1487
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001488 if (!fw_dump.dump_active)
1489 return -EPERM;
1490
Michal Suchanekdcdc4672017-06-26 16:06:01 +02001491 if (kstrtoint(buf, 0, &input))
1492 return -EINVAL;
1493
1494 if (input == 1) {
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001495 /*
1496 * Take away the '/proc/vmcore'. We are releasing the dump
1497 * memory, hence it will not be valid anymore.
1498 */
Michael Ellerman2685f822016-09-30 10:51:46 +10001499#ifdef CONFIG_PROC_VMCORE
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001500 vmcore_cleanup();
Michael Ellerman2685f822016-09-30 10:51:46 +10001501#endif
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001502 fadump_invalidate_release_mem();
1503
1504 } else
1505 return -EINVAL;
1506 return count;
1507}
1508
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001509static ssize_t fadump_enabled_show(struct kobject *kobj,
1510 struct kobj_attribute *attr,
1511 char *buf)
1512{
1513 return sprintf(buf, "%d\n", fw_dump.fadump_enabled);
1514}
1515
1516static ssize_t fadump_register_show(struct kobject *kobj,
1517 struct kobj_attribute *attr,
1518 char *buf)
1519{
1520 return sprintf(buf, "%d\n", fw_dump.dump_registered);
1521}
1522
1523static ssize_t fadump_register_store(struct kobject *kobj,
1524 struct kobj_attribute *attr,
1525 const char *buf, size_t count)
1526{
1527 int ret = 0;
Michal Suchanekdcdc4672017-06-26 16:06:01 +02001528 int input = -1;
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001529
1530 if (!fw_dump.fadump_enabled || fdm_active)
1531 return -EPERM;
1532
Michal Suchanekdcdc4672017-06-26 16:06:01 +02001533 if (kstrtoint(buf, 0, &input))
1534 return -EINVAL;
1535
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001536 mutex_lock(&fadump_mutex);
1537
Michal Suchanekdcdc4672017-06-26 16:06:01 +02001538 switch (input) {
1539 case 0:
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001540 if (fw_dump.dump_registered == 0) {
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001541 goto unlock_out;
1542 }
1543 /* Un-register Firmware-assisted dump */
1544 fadump_unregister_dump(&fdm);
1545 break;
Michal Suchanekdcdc4672017-06-26 16:06:01 +02001546 case 1:
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001547 if (fw_dump.dump_registered == 1) {
Hari Bathini0823c682018-09-14 19:36:02 +05301548 /* Un-register Firmware-assisted dump */
1549 fadump_unregister_dump(&fdm);
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001550 }
1551 /* Register Firmware-assisted dump */
Michal Suchanek98b8cd72017-05-27 17:46:15 +02001552 ret = register_fadump();
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001553 break;
1554 default:
1555 ret = -EINVAL;
1556 break;
1557 }
1558
1559unlock_out:
1560 mutex_unlock(&fadump_mutex);
1561 return ret < 0 ? ret : count;
1562}
1563
1564static int fadump_region_show(struct seq_file *m, void *private)
1565{
Hari Bathini0226e552019-09-11 20:18:14 +05301566 const struct rtas_fadump_mem_struct *fdm_ptr;
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001567
1568 if (!fw_dump.fadump_enabled)
1569 return 0;
1570
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001571 mutex_lock(&fadump_mutex);
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001572 if (fdm_active)
1573 fdm_ptr = fdm_active;
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001574 else {
1575 mutex_unlock(&fadump_mutex);
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001576 fdm_ptr = &fdm;
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001577 }
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001578
1579 seq_printf(m,
1580 "CPU : [%#016llx-%#016llx] %#llx bytes, "
1581 "Dumped: %#llx\n",
Hari Bathini408cddd2014-10-01 12:32:30 +05301582 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address),
1583 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) +
1584 be64_to_cpu(fdm_ptr->cpu_state_data.source_len) - 1,
1585 be64_to_cpu(fdm_ptr->cpu_state_data.source_len),
1586 be64_to_cpu(fdm_ptr->cpu_state_data.bytes_dumped));
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001587 seq_printf(m,
1588 "HPTE: [%#016llx-%#016llx] %#llx bytes, "
1589 "Dumped: %#llx\n",
Hari Bathini408cddd2014-10-01 12:32:30 +05301590 be64_to_cpu(fdm_ptr->hpte_region.destination_address),
1591 be64_to_cpu(fdm_ptr->hpte_region.destination_address) +
1592 be64_to_cpu(fdm_ptr->hpte_region.source_len) - 1,
1593 be64_to_cpu(fdm_ptr->hpte_region.source_len),
1594 be64_to_cpu(fdm_ptr->hpte_region.bytes_dumped));
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001595 seq_printf(m,
1596 "DUMP: [%#016llx-%#016llx] %#llx bytes, "
1597 "Dumped: %#llx\n",
Hari Bathini408cddd2014-10-01 12:32:30 +05301598 be64_to_cpu(fdm_ptr->rmr_region.destination_address),
1599 be64_to_cpu(fdm_ptr->rmr_region.destination_address) +
1600 be64_to_cpu(fdm_ptr->rmr_region.source_len) - 1,
1601 be64_to_cpu(fdm_ptr->rmr_region.source_len),
1602 be64_to_cpu(fdm_ptr->rmr_region.bytes_dumped));
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001603
1604 if (!fdm_active ||
1605 (fw_dump.reserve_dump_area_start ==
Hari Bathini408cddd2014-10-01 12:32:30 +05301606 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address)))
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001607 goto out;
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001608
1609 /* Dump is active. Show reserved memory region. */
1610 seq_printf(m,
1611 " : [%#016llx-%#016llx] %#llx bytes, "
1612 "Dumped: %#llx\n",
1613 (unsigned long long)fw_dump.reserve_dump_area_start,
Hari Bathini408cddd2014-10-01 12:32:30 +05301614 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) - 1,
1615 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) -
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001616 fw_dump.reserve_dump_area_start,
Hari Bathini408cddd2014-10-01 12:32:30 +05301617 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) -
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001618 fw_dump.reserve_dump_area_start);
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001619out:
1620 if (fdm_active)
1621 mutex_unlock(&fadump_mutex);
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001622 return 0;
1623}
1624
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001625static struct kobj_attribute fadump_release_attr = __ATTR(fadump_release_mem,
1626 0200, NULL,
1627 fadump_release_memory_store);
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001628static struct kobj_attribute fadump_attr = __ATTR(fadump_enabled,
1629 0444, fadump_enabled_show,
1630 NULL);
1631static struct kobj_attribute fadump_register_attr = __ATTR(fadump_registered,
1632 0644, fadump_register_show,
1633 fadump_register_store);
1634
Yangtao Lif6cee262018-11-05 10:01:19 -05001635DEFINE_SHOW_ATTRIBUTE(fadump_region);
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001636
1637static void fadump_init_files(void)
1638{
1639 struct dentry *debugfs_file;
1640 int rc = 0;
1641
1642 rc = sysfs_create_file(kernel_kobj, &fadump_attr.attr);
1643 if (rc)
1644 printk(KERN_ERR "fadump: unable to create sysfs file"
1645 " fadump_enabled (%d)\n", rc);
1646
1647 rc = sysfs_create_file(kernel_kobj, &fadump_register_attr.attr);
1648 if (rc)
1649 printk(KERN_ERR "fadump: unable to create sysfs file"
1650 " fadump_registered (%d)\n", rc);
1651
1652 debugfs_file = debugfs_create_file("fadump_region", 0444,
1653 powerpc_debugfs_root, NULL,
1654 &fadump_region_fops);
1655 if (!debugfs_file)
1656 printk(KERN_ERR "fadump: unable to create debugfs file"
1657 " fadump_region\n");
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001658
1659 if (fw_dump.dump_active) {
1660 rc = sysfs_create_file(kernel_kobj, &fadump_release_attr.attr);
1661 if (rc)
1662 printk(KERN_ERR "fadump: unable to create sysfs file"
1663 " fadump_release_mem (%d)\n", rc);
1664 }
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001665 return;
1666}
1667
1668/*
1669 * Prepare for firmware-assisted dump.
1670 */
1671int __init setup_fadump(void)
1672{
1673 if (!fw_dump.fadump_enabled)
1674 return 0;
1675
1676 if (!fw_dump.fadump_supported) {
1677 printk(KERN_ERR "Firmware-assisted dump is not supported on"
1678 " this hardware\n");
1679 return 0;
1680 }
1681
1682 fadump_show_config();
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001683 /*
1684 * If dump data is available then see if it is valid and prepare for
1685 * saving it to the disk.
1686 */
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001687 if (fw_dump.dump_active) {
1688 /*
1689 * if dump process fails then invalidate the registration
1690 * and release memory before proceeding for re-registration.
1691 */
1692 if (process_fadump(fdm_active) < 0)
1693 fadump_invalidate_release_mem();
1694 }
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001695 /* Initialize the kernel dump memory structure for FAD registration. */
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001696 else if (fw_dump.reserve_dump_area_size)
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001697 init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
1698 fadump_init_files();
1699
1700 return 1;
1701}
1702subsys_initcall(setup_fadump);