blob: 02925043575a8a2f768770d722ad151bc1944ecf [file] [log] [blame]
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +00001/*
2 * Firmware Assisted dump: A robust mechanism to get reliable kernel crash
3 * dump with assistance from firmware. This approach does not use kexec,
4 * instead firmware assists in booting the kdump kernel while preserving
5 * memory contents. The most of the code implementation has been adapted
6 * from phyp assisted dump implementation written by Linas Vepstas and
7 * Manish Ahuja
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 *
23 * Copyright 2011 IBM Corporation
24 * Author: Mahesh Salgaonkar <mahesh@linux.vnet.ibm.com>
25 */
26
27#undef DEBUG
28#define pr_fmt(fmt) "fadump: " fmt
29
30#include <linux/string.h>
31#include <linux/memblock.h>
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +000032#include <linux/delay.h>
33#include <linux/debugfs.h>
34#include <linux/seq_file.h>
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +000035#include <linux/crash_dump.h>
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +000036#include <linux/kobject.h>
37#include <linux/sysfs.h>
Hari Bathini1ae31742018-08-07 02:12:45 +053038#include <linux/slab.h>
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +000039
40#include <asm/page.h>
41#include <asm/prom.h>
42#include <asm/rtas.h>
43#include <asm/fadump.h>
Stephen Rothwellcad3c832012-03-30 14:01:07 +000044#include <asm/debug.h>
45#include <asm/setup.h>
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +000046
47static struct fw_dump fw_dump;
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +000048static struct fadump_mem_struct fdm;
49static const struct fadump_mem_struct *fdm_active;
50
51static DEFINE_MUTEX(fadump_mutex);
Hari Bathini1ae31742018-08-07 02:12:45 +053052struct fad_crash_memory_ranges *crash_memory_ranges;
53int crash_memory_ranges_size;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +000054int crash_mem_ranges;
Hari Bathini1ae31742018-08-07 02:12:45 +053055int max_crash_mem_ranges;
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +000056
57/* Scan the Firmware Assisted dump configuration details. */
58int __init early_init_dt_scan_fw_dump(unsigned long node,
59 const char *uname, int depth, void *data)
60{
Rob Herring9d0c4df2014-04-01 23:49:03 -050061 const __be32 *sections;
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +000062 int i, num_sections;
Rob Herring9d0c4df2014-04-01 23:49:03 -050063 int size;
Hari Bathini408cddd2014-10-01 12:32:30 +053064 const __be32 *token;
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +000065
66 if (depth != 1 || strcmp(uname, "rtas") != 0)
67 return 0;
68
69 /*
70 * Check if Firmware Assisted dump is supported. if yes, check
71 * if dump has been initiated on last reboot.
72 */
73 token = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump", NULL);
74 if (!token)
Gavin Shana7d04312014-04-24 18:00:31 +100075 return 1;
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +000076
77 fw_dump.fadump_supported = 1;
Hari Bathini408cddd2014-10-01 12:32:30 +053078 fw_dump.ibm_configure_kernel_dump = be32_to_cpu(*token);
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +000079
80 /*
81 * The 'ibm,kernel-dump' rtas node is present only if there is
82 * dump data waiting for us.
83 */
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +000084 fdm_active = of_get_flat_dt_prop(node, "ibm,kernel-dump", NULL);
85 if (fdm_active)
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +000086 fw_dump.dump_active = 1;
87
88 /* Get the sizes required to store dump data for the firmware provided
89 * dump sections.
90 * For each dump section type supported, a 32bit cell which defines
91 * the ID of a supported section followed by two 32 bit cells which
92 * gives teh size of the section in bytes.
93 */
94 sections = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump-sizes",
95 &size);
96
97 if (!sections)
Gavin Shana7d04312014-04-24 18:00:31 +100098 return 1;
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +000099
100 num_sections = size / (3 * sizeof(u32));
101
102 for (i = 0; i < num_sections; i++, sections += 3) {
103 u32 type = (u32)of_read_number(sections, 1);
104
105 switch (type) {
106 case FADUMP_CPU_STATE_DATA:
107 fw_dump.cpu_state_data_size =
108 of_read_ulong(&sections[1], 2);
109 break;
110 case FADUMP_HPTE_REGION:
111 fw_dump.hpte_region_size =
112 of_read_ulong(&sections[1], 2);
113 break;
114 }
115 }
Gavin Shana7d04312014-04-24 18:00:31 +1000116
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000117 return 1;
118}
119
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000120int is_fadump_active(void)
121{
122 return fw_dump.dump_active;
123}
124
125/* Print firmware assisted dump configurations for debugging purpose. */
126static void fadump_show_config(void)
127{
128 pr_debug("Support for firmware-assisted dump (fadump): %s\n",
129 (fw_dump.fadump_supported ? "present" : "no support"));
130
131 if (!fw_dump.fadump_supported)
132 return;
133
134 pr_debug("Fadump enabled : %s\n",
135 (fw_dump.fadump_enabled ? "yes" : "no"));
136 pr_debug("Dump Active : %s\n",
137 (fw_dump.dump_active ? "yes" : "no"));
138 pr_debug("Dump section sizes:\n");
139 pr_debug(" CPU state data size: %lx\n", fw_dump.cpu_state_data_size);
140 pr_debug(" HPTE region size : %lx\n", fw_dump.hpte_region_size);
141 pr_debug("Boot memory size : %lx\n", fw_dump.boot_memory_size);
142}
143
144static unsigned long init_fadump_mem_struct(struct fadump_mem_struct *fdm,
145 unsigned long addr)
146{
147 if (!fdm)
148 return 0;
149
150 memset(fdm, 0, sizeof(struct fadump_mem_struct));
151 addr = addr & PAGE_MASK;
152
Hari Bathini408cddd2014-10-01 12:32:30 +0530153 fdm->header.dump_format_version = cpu_to_be32(0x00000001);
154 fdm->header.dump_num_sections = cpu_to_be16(3);
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000155 fdm->header.dump_status_flag = 0;
156 fdm->header.offset_first_dump_section =
Hari Bathini408cddd2014-10-01 12:32:30 +0530157 cpu_to_be32((u32)offsetof(struct fadump_mem_struct, cpu_state_data));
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000158
159 /*
160 * Fields for disk dump option.
161 * We are not using disk dump option, hence set these fields to 0.
162 */
163 fdm->header.dd_block_size = 0;
164 fdm->header.dd_block_offset = 0;
165 fdm->header.dd_num_blocks = 0;
166 fdm->header.dd_offset_disk_path = 0;
167
168 /* set 0 to disable an automatic dump-reboot. */
169 fdm->header.max_time_auto = 0;
170
171 /* Kernel dump sections */
172 /* cpu state data section. */
Hari Bathini408cddd2014-10-01 12:32:30 +0530173 fdm->cpu_state_data.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG);
174 fdm->cpu_state_data.source_data_type = cpu_to_be16(FADUMP_CPU_STATE_DATA);
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000175 fdm->cpu_state_data.source_address = 0;
Hari Bathini408cddd2014-10-01 12:32:30 +0530176 fdm->cpu_state_data.source_len = cpu_to_be64(fw_dump.cpu_state_data_size);
177 fdm->cpu_state_data.destination_address = cpu_to_be64(addr);
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000178 addr += fw_dump.cpu_state_data_size;
179
180 /* hpte region section */
Hari Bathini408cddd2014-10-01 12:32:30 +0530181 fdm->hpte_region.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG);
182 fdm->hpte_region.source_data_type = cpu_to_be16(FADUMP_HPTE_REGION);
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000183 fdm->hpte_region.source_address = 0;
Hari Bathini408cddd2014-10-01 12:32:30 +0530184 fdm->hpte_region.source_len = cpu_to_be64(fw_dump.hpte_region_size);
185 fdm->hpte_region.destination_address = cpu_to_be64(addr);
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000186 addr += fw_dump.hpte_region_size;
187
188 /* RMA region section */
Hari Bathini408cddd2014-10-01 12:32:30 +0530189 fdm->rmr_region.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG);
190 fdm->rmr_region.source_data_type = cpu_to_be16(FADUMP_REAL_MODE_REGION);
191 fdm->rmr_region.source_address = cpu_to_be64(RMA_START);
192 fdm->rmr_region.source_len = cpu_to_be64(fw_dump.boot_memory_size);
193 fdm->rmr_region.destination_address = cpu_to_be64(addr);
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000194 addr += fw_dump.boot_memory_size;
195
196 return addr;
197}
198
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000199/**
200 * fadump_calculate_reserve_size(): reserve variable boot area 5% of System RAM
201 *
202 * Function to find the largest memory size we need to reserve during early
203 * boot process. This will be the size of the memory that is required for a
204 * kernel to boot successfully.
205 *
206 * This function has been taken from phyp-assisted dump feature implementation.
207 *
208 * returns larger of 256MB or 5% rounded down to multiples of 256MB.
209 *
210 * TODO: Come up with better approach to find out more accurate memory size
211 * that is required for a kernel to boot successfully.
212 *
213 */
214static inline unsigned long fadump_calculate_reserve_size(void)
215{
216 unsigned long size;
217
218 /*
219 * Check if the size is specified through fadump_reserve_mem= cmdline
220 * option. If yes, then use that.
221 */
222 if (fw_dump.reserve_bootvar)
223 return fw_dump.reserve_bootvar;
224
225 /* divide by 20 to get 5% of value */
226 size = memblock_end_of_DRAM() / 20;
227
228 /* round it down in multiples of 256 */
229 size = size & ~0x0FFFFFFFUL;
230
231 /* Truncate to memory_limit. We don't want to over reserve the memory.*/
232 if (memory_limit && size > memory_limit)
233 size = memory_limit;
234
235 return (size > MIN_BOOT_MEM ? size : MIN_BOOT_MEM);
236}
237
238/*
239 * Calculate the total memory size required to be reserved for
240 * firmware-assisted dump registration.
241 */
242static unsigned long get_fadump_area_size(void)
243{
244 unsigned long size = 0;
245
246 size += fw_dump.cpu_state_data_size;
247 size += fw_dump.hpte_region_size;
248 size += fw_dump.boot_memory_size;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000249 size += sizeof(struct fadump_crash_info_header);
250 size += sizeof(struct elfhdr); /* ELF core header.*/
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000251 size += sizeof(struct elf_phdr); /* place holder for cpu notes */
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000252 /* Program headers for crash memory regions. */
253 size += sizeof(struct elf_phdr) * (memblock_num_regions(memory) + 2);
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000254
255 size = PAGE_ALIGN(size);
256 return size;
257}
258
259int __init fadump_reserve_mem(void)
260{
261 unsigned long base, size, memory_boundary;
262
263 if (!fw_dump.fadump_enabled)
264 return 0;
265
266 if (!fw_dump.fadump_supported) {
267 printk(KERN_INFO "Firmware-assisted dump is not supported on"
268 " this hardware\n");
269 fw_dump.fadump_enabled = 0;
270 return 0;
271 }
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000272 /*
273 * Initialize boot memory size
274 * If dump is active then we have already calculated the size during
275 * first kernel.
276 */
277 if (fdm_active)
Hari Bathini408cddd2014-10-01 12:32:30 +0530278 fw_dump.boot_memory_size = be64_to_cpu(fdm_active->rmr_region.source_len);
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000279 else
280 fw_dump.boot_memory_size = fadump_calculate_reserve_size();
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000281
282 /*
283 * Calculate the memory boundary.
284 * If memory_limit is less than actual memory boundary then reserve
285 * the memory for fadump beyond the memory_limit and adjust the
286 * memory_limit accordingly, so that the running kernel can run with
287 * specified memory_limit.
288 */
289 if (memory_limit && memory_limit < memblock_end_of_DRAM()) {
290 size = get_fadump_area_size();
291 if ((memory_limit + size) < memblock_end_of_DRAM())
292 memory_limit += size;
293 else
294 memory_limit = memblock_end_of_DRAM();
295 printk(KERN_INFO "Adjusted memory_limit for firmware-assisted"
Suzuki Poulosea84fcd42012-08-21 01:42:33 +0000296 " dump, now %#016llx\n", memory_limit);
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000297 }
298 if (memory_limit)
299 memory_boundary = memory_limit;
300 else
301 memory_boundary = memblock_end_of_DRAM();
302
303 if (fw_dump.dump_active) {
304 printk(KERN_INFO "Firmware-assisted dump is active.\n");
305 /*
306 * If last boot has crashed then reserve all the memory
307 * above boot_memory_size so that we don't touch it until
308 * dump is written to disk by userspace tool. This memory
309 * will be released for general use once the dump is saved.
310 */
311 base = fw_dump.boot_memory_size;
312 size = memory_boundary - base;
313 memblock_reserve(base, size);
314 printk(KERN_INFO "Reserved %ldMB of memory at %ldMB "
315 "for saving crash dump\n",
316 (unsigned long)(size >> 20),
317 (unsigned long)(base >> 20));
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000318
319 fw_dump.fadumphdr_addr =
Hari Bathini408cddd2014-10-01 12:32:30 +0530320 be64_to_cpu(fdm_active->rmr_region.destination_address) +
321 be64_to_cpu(fdm_active->rmr_region.source_len);
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000322 pr_debug("fadumphdr_addr = %p\n",
323 (void *) fw_dump.fadumphdr_addr);
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000324 } else {
325 /* Reserve the memory at the top of memory. */
326 size = get_fadump_area_size();
327 base = memory_boundary - size;
328 memblock_reserve(base, size);
329 printk(KERN_INFO "Reserved %ldMB of memory at %ldMB "
330 "for firmware-assisted dump\n",
331 (unsigned long)(size >> 20),
332 (unsigned long)(base >> 20));
333 }
334 fw_dump.reserve_dump_area_start = base;
335 fw_dump.reserve_dump_area_size = size;
336 return 1;
337}
338
Srikar Dronamraju1e766092016-10-07 16:59:21 -0700339unsigned long __init arch_reserved_kernel_pages(void)
340{
341 return memblock_reserved_size() / PAGE_SIZE;
342}
343
Mahesh Salgaonkareb39c882012-02-16 01:14:22 +0000344/* Look for fadump= cmdline option. */
345static int __init early_fadump_param(char *p)
346{
347 if (!p)
348 return 1;
349
350 if (strncmp(p, "on", 2) == 0)
351 fw_dump.fadump_enabled = 1;
352 else if (strncmp(p, "off", 3) == 0)
353 fw_dump.fadump_enabled = 0;
354
355 return 0;
356}
357early_param("fadump", early_fadump_param);
358
359/* Look for fadump_reserve_mem= cmdline option */
360static int __init early_fadump_reserve_mem(char *p)
361{
362 if (p)
363 fw_dump.reserve_bootvar = memparse(p, &p);
364 return 0;
365}
366early_param("fadump_reserve_mem", early_fadump_reserve_mem);
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000367
Michal Suchanekd3d39d22017-05-27 17:46:15 +0200368static int register_fw_dump(struct fadump_mem_struct *fdm)
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000369{
Michal Suchanekd3d39d22017-05-27 17:46:15 +0200370 int rc, err;
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000371 unsigned int wait_time;
372
373 pr_debug("Registering for firmware-assisted kernel dump...\n");
374
375 /* TODO: Add upper time limit for the delay */
376 do {
377 rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
378 FADUMP_REGISTER, fdm,
379 sizeof(struct fadump_mem_struct));
380
381 wait_time = rtas_busy_delay_time(rc);
382 if (wait_time)
383 mdelay(wait_time);
384
385 } while (wait_time);
386
Michal Suchanekd3d39d22017-05-27 17:46:15 +0200387 err = -EIO;
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000388 switch (rc) {
Michal Suchanekd3d39d22017-05-27 17:46:15 +0200389 default:
390 pr_err("Failed to register. Unknown Error(%d).\n", rc);
391 break;
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000392 case -1:
393 printk(KERN_ERR "Failed to register firmware-assisted kernel"
394 " dump. Hardware Error(%d).\n", rc);
395 break;
396 case -3:
397 printk(KERN_ERR "Failed to register firmware-assisted kernel"
398 " dump. Parameter Error(%d).\n", rc);
Michal Suchanekd3d39d22017-05-27 17:46:15 +0200399 err = -EINVAL;
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000400 break;
401 case -9:
402 printk(KERN_ERR "firmware-assisted kernel dump is already "
403 " registered.");
404 fw_dump.dump_registered = 1;
Michal Suchanekd3d39d22017-05-27 17:46:15 +0200405 err = -EEXIST;
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000406 break;
407 case 0:
408 printk(KERN_INFO "firmware-assisted kernel dump registration"
409 " is successful\n");
410 fw_dump.dump_registered = 1;
Michal Suchanekd3d39d22017-05-27 17:46:15 +0200411 err = 0;
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000412 break;
413 }
Michal Suchanekd3d39d22017-05-27 17:46:15 +0200414 return err;
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000415}
416
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000417void crash_fadump(struct pt_regs *regs, const char *str)
418{
419 struct fadump_crash_info_header *fdh = NULL;
420
421 if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr)
422 return;
423
424 fdh = __va(fw_dump.fadumphdr_addr);
425 crashing_cpu = smp_processor_id();
426 fdh->crashing_cpu = crashing_cpu;
427 crash_save_vmcoreinfo();
428
429 if (regs)
430 fdh->regs = *regs;
431 else
432 ppc_save_regs(&fdh->regs);
433
Rasmus Villemoesa0512162016-01-20 15:00:13 -0800434 fdh->online_mask = *cpu_online_mask;
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000435
436 /* Call ibm,os-term rtas call to trigger firmware assisted dump */
437 rtas_os_term((char *)str);
438}
439
440#define GPR_MASK 0xffffff0000000000
441static inline int fadump_gpr_index(u64 id)
442{
443 int i = -1;
444 char str[3];
445
446 if ((id & GPR_MASK) == REG_ID("GPR")) {
447 /* get the digits at the end */
448 id &= ~GPR_MASK;
449 id >>= 24;
450 str[2] = '\0';
451 str[1] = id & 0xff;
452 str[0] = (id >> 8) & 0xff;
453 sscanf(str, "%d", &i);
454 if (i > 31)
455 i = -1;
456 }
457 return i;
458}
459
460static inline void fadump_set_regval(struct pt_regs *regs, u64 reg_id,
461 u64 reg_val)
462{
463 int i;
464
465 i = fadump_gpr_index(reg_id);
466 if (i >= 0)
467 regs->gpr[i] = (unsigned long)reg_val;
468 else if (reg_id == REG_ID("NIA"))
469 regs->nip = (unsigned long)reg_val;
470 else if (reg_id == REG_ID("MSR"))
471 regs->msr = (unsigned long)reg_val;
472 else if (reg_id == REG_ID("CTR"))
473 regs->ctr = (unsigned long)reg_val;
474 else if (reg_id == REG_ID("LR"))
475 regs->link = (unsigned long)reg_val;
476 else if (reg_id == REG_ID("XER"))
477 regs->xer = (unsigned long)reg_val;
478 else if (reg_id == REG_ID("CR"))
479 regs->ccr = (unsigned long)reg_val;
480 else if (reg_id == REG_ID("DAR"))
481 regs->dar = (unsigned long)reg_val;
482 else if (reg_id == REG_ID("DSISR"))
483 regs->dsisr = (unsigned long)reg_val;
484}
485
486static struct fadump_reg_entry*
487fadump_read_registers(struct fadump_reg_entry *reg_entry, struct pt_regs *regs)
488{
489 memset(regs, 0, sizeof(struct pt_regs));
490
Hari Bathini408cddd2014-10-01 12:32:30 +0530491 while (be64_to_cpu(reg_entry->reg_id) != REG_ID("CPUEND")) {
492 fadump_set_regval(regs, be64_to_cpu(reg_entry->reg_id),
493 be64_to_cpu(reg_entry->reg_value));
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000494 reg_entry++;
495 }
496 reg_entry++;
497 return reg_entry;
498}
499
500static u32 *fadump_append_elf_note(u32 *buf, char *name, unsigned type,
501 void *data, size_t data_len)
502{
503 struct elf_note note;
504
505 note.n_namesz = strlen(name) + 1;
506 note.n_descsz = data_len;
507 note.n_type = type;
508 memcpy(buf, &note, sizeof(note));
509 buf += (sizeof(note) + 3)/4;
510 memcpy(buf, name, note.n_namesz);
511 buf += (note.n_namesz + 3)/4;
512 memcpy(buf, data, note.n_descsz);
513 buf += (note.n_descsz + 3)/4;
514
515 return buf;
516}
517
518static void fadump_final_note(u32 *buf)
519{
520 struct elf_note note;
521
522 note.n_namesz = 0;
523 note.n_descsz = 0;
524 note.n_type = 0;
525 memcpy(buf, &note, sizeof(note));
526}
527
528static u32 *fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs)
529{
530 struct elf_prstatus prstatus;
531
532 memset(&prstatus, 0, sizeof(prstatus));
533 /*
534 * FIXME: How do i get PID? Do I really need it?
535 * prstatus.pr_pid = ????
536 */
537 elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
538 buf = fadump_append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS,
539 &prstatus, sizeof(prstatus));
540 return buf;
541}
542
543static void fadump_update_elfcore_header(char *bufp)
544{
545 struct elfhdr *elf;
546 struct elf_phdr *phdr;
547
548 elf = (struct elfhdr *)bufp;
549 bufp += sizeof(struct elfhdr);
550
551 /* First note is a place holder for cpu notes info. */
552 phdr = (struct elf_phdr *)bufp;
553
554 if (phdr->p_type == PT_NOTE) {
555 phdr->p_paddr = fw_dump.cpu_notes_buf;
556 phdr->p_offset = phdr->p_paddr;
557 phdr->p_filesz = fw_dump.cpu_notes_buf_size;
558 phdr->p_memsz = fw_dump.cpu_notes_buf_size;
559 }
560 return;
561}
562
563static void *fadump_cpu_notes_buf_alloc(unsigned long size)
564{
565 void *vaddr;
566 struct page *page;
567 unsigned long order, count, i;
568
569 order = get_order(size);
570 vaddr = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
571 if (!vaddr)
572 return NULL;
573
574 count = 1 << order;
575 page = virt_to_page(vaddr);
576 for (i = 0; i < count; i++)
577 SetPageReserved(page + i);
578 return vaddr;
579}
580
581static void fadump_cpu_notes_buf_free(unsigned long vaddr, unsigned long size)
582{
583 struct page *page;
584 unsigned long order, count, i;
585
586 order = get_order(size);
587 count = 1 << order;
588 page = virt_to_page(vaddr);
589 for (i = 0; i < count; i++)
590 ClearPageReserved(page + i);
591 __free_pages(page, order);
592}
593
594/*
595 * Read CPU state dump data and convert it into ELF notes.
596 * The CPU dump starts with magic number "REGSAVE". NumCpusOffset should be
597 * used to access the data to allow for additional fields to be added without
598 * affecting compatibility. Each list of registers for a CPU starts with
599 * "CPUSTRT" and ends with "CPUEND". Each register entry is of 16 bytes,
600 * 8 Byte ASCII identifier and 8 Byte register value. The register entry
601 * with identifier "CPUSTRT" and "CPUEND" contains 4 byte cpu id as part
602 * of register value. For more details refer to PAPR document.
603 *
604 * Only for the crashing cpu we ignore the CPU dump data and get exact
605 * state from fadump crash info structure populated by first kernel at the
606 * time of crash.
607 */
608static int __init fadump_build_cpu_notes(const struct fadump_mem_struct *fdm)
609{
610 struct fadump_reg_save_area_header *reg_header;
611 struct fadump_reg_entry *reg_entry;
612 struct fadump_crash_info_header *fdh = NULL;
613 void *vaddr;
614 unsigned long addr;
615 u32 num_cpus, *note_buf;
616 struct pt_regs regs;
617 int i, rc = 0, cpu = 0;
618
619 if (!fdm->cpu_state_data.bytes_dumped)
620 return -EINVAL;
621
Hari Bathini408cddd2014-10-01 12:32:30 +0530622 addr = be64_to_cpu(fdm->cpu_state_data.destination_address);
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000623 vaddr = __va(addr);
624
625 reg_header = vaddr;
Hari Bathini408cddd2014-10-01 12:32:30 +0530626 if (be64_to_cpu(reg_header->magic_number) != REGSAVE_AREA_MAGIC) {
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000627 printk(KERN_ERR "Unable to read register save area.\n");
628 return -ENOENT;
629 }
630 pr_debug("--------CPU State Data------------\n");
Hari Bathini408cddd2014-10-01 12:32:30 +0530631 pr_debug("Magic Number: %llx\n", be64_to_cpu(reg_header->magic_number));
632 pr_debug("NumCpuOffset: %x\n", be32_to_cpu(reg_header->num_cpu_offset));
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000633
Hari Bathini408cddd2014-10-01 12:32:30 +0530634 vaddr += be32_to_cpu(reg_header->num_cpu_offset);
635 num_cpus = be32_to_cpu(*((__be32 *)(vaddr)));
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000636 pr_debug("NumCpus : %u\n", num_cpus);
637 vaddr += sizeof(u32);
638 reg_entry = (struct fadump_reg_entry *)vaddr;
639
640 /* Allocate buffer to hold cpu crash notes. */
641 fw_dump.cpu_notes_buf_size = num_cpus * sizeof(note_buf_t);
642 fw_dump.cpu_notes_buf_size = PAGE_ALIGN(fw_dump.cpu_notes_buf_size);
643 note_buf = fadump_cpu_notes_buf_alloc(fw_dump.cpu_notes_buf_size);
644 if (!note_buf) {
645 printk(KERN_ERR "Failed to allocate 0x%lx bytes for "
646 "cpu notes buffer\n", fw_dump.cpu_notes_buf_size);
647 return -ENOMEM;
648 }
649 fw_dump.cpu_notes_buf = __pa(note_buf);
650
651 pr_debug("Allocated buffer for cpu notes of size %ld at %p\n",
652 (num_cpus * sizeof(note_buf_t)), note_buf);
653
654 if (fw_dump.fadumphdr_addr)
655 fdh = __va(fw_dump.fadumphdr_addr);
656
657 for (i = 0; i < num_cpus; i++) {
Hari Bathini408cddd2014-10-01 12:32:30 +0530658 if (be64_to_cpu(reg_entry->reg_id) != REG_ID("CPUSTRT")) {
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000659 printk(KERN_ERR "Unable to read CPU state data\n");
660 rc = -ENOENT;
661 goto error_out;
662 }
663 /* Lower 4 bytes of reg_value contains logical cpu id */
Hari Bathini408cddd2014-10-01 12:32:30 +0530664 cpu = be64_to_cpu(reg_entry->reg_value) & FADUMP_CPU_ID_MASK;
Rasmus Villemoesa0512162016-01-20 15:00:13 -0800665 if (fdh && !cpumask_test_cpu(cpu, &fdh->online_mask)) {
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000666 SKIP_TO_NEXT_CPU(reg_entry);
667 continue;
668 }
669 pr_debug("Reading register data for cpu %d...\n", cpu);
670 if (fdh && fdh->crashing_cpu == cpu) {
671 regs = fdh->regs;
672 note_buf = fadump_regs_to_elf_notes(note_buf, &regs);
673 SKIP_TO_NEXT_CPU(reg_entry);
674 } else {
675 reg_entry++;
676 reg_entry = fadump_read_registers(reg_entry, &regs);
677 note_buf = fadump_regs_to_elf_notes(note_buf, &regs);
678 }
679 }
680 fadump_final_note(note_buf);
681
Rickard Strandqvistb717d982014-05-23 00:03:16 +0200682 if (fdh) {
683 pr_debug("Updating elfcore header (%llx) with cpu notes\n",
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000684 fdh->elfcorehdr_addr);
Rickard Strandqvistb717d982014-05-23 00:03:16 +0200685 fadump_update_elfcore_header((char *)__va(fdh->elfcorehdr_addr));
686 }
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000687 return 0;
688
689error_out:
690 fadump_cpu_notes_buf_free((unsigned long)__va(fw_dump.cpu_notes_buf),
691 fw_dump.cpu_notes_buf_size);
692 fw_dump.cpu_notes_buf = 0;
693 fw_dump.cpu_notes_buf_size = 0;
694 return rc;
695
696}
697
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000698/*
699 * Validate and process the dump data stored by firmware before exporting
700 * it through '/proc/vmcore'.
701 */
702static int __init process_fadump(const struct fadump_mem_struct *fdm_active)
703{
704 struct fadump_crash_info_header *fdh;
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000705 int rc = 0;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000706
707 if (!fdm_active || !fw_dump.fadumphdr_addr)
708 return -EINVAL;
709
710 /* Check if the dump data is valid. */
Hari Bathini408cddd2014-10-01 12:32:30 +0530711 if ((be16_to_cpu(fdm_active->header.dump_status_flag) == FADUMP_ERROR_FLAG) ||
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000712 (fdm_active->cpu_state_data.error_flags != 0) ||
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000713 (fdm_active->rmr_region.error_flags != 0)) {
714 printk(KERN_ERR "Dump taken by platform is not valid\n");
715 return -EINVAL;
716 }
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000717 if ((fdm_active->rmr_region.bytes_dumped !=
718 fdm_active->rmr_region.source_len) ||
719 !fdm_active->cpu_state_data.bytes_dumped) {
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000720 printk(KERN_ERR "Dump taken by platform is incomplete\n");
721 return -EINVAL;
722 }
723
724 /* Validate the fadump crash info header */
725 fdh = __va(fw_dump.fadumphdr_addr);
726 if (fdh->magic_number != FADUMP_CRASH_INFO_MAGIC) {
727 printk(KERN_ERR "Crash info header is not valid.\n");
728 return -EINVAL;
729 }
730
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000731 rc = fadump_build_cpu_notes(fdm_active);
732 if (rc)
733 return rc;
734
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000735 /*
736 * We are done validating dump info and elfcore header is now ready
737 * to be exported. set elfcorehdr_addr so that vmcore module will
738 * export the elfcore header through '/proc/vmcore'.
739 */
740 elfcorehdr_addr = fdh->elfcorehdr_addr;
741
742 return 0;
743}
744
Hari Bathini1ae31742018-08-07 02:12:45 +0530745static void free_crash_memory_ranges(void)
746{
747 kfree(crash_memory_ranges);
748 crash_memory_ranges = NULL;
749 crash_memory_ranges_size = 0;
750 max_crash_mem_ranges = 0;
751}
752
753/*
754 * Allocate or reallocate crash memory ranges array in incremental units
755 * of PAGE_SIZE.
756 */
757static int allocate_crash_memory_ranges(void)
758{
759 struct fad_crash_memory_ranges *new_array;
760 u64 new_size;
761
762 new_size = crash_memory_ranges_size + PAGE_SIZE;
763 pr_debug("Allocating %llu bytes of memory for crash memory ranges\n",
764 new_size);
765
766 new_array = krealloc(crash_memory_ranges, new_size, GFP_KERNEL);
767 if (new_array == NULL) {
768 pr_err("Insufficient memory for setting up crash memory ranges\n");
769 free_crash_memory_ranges();
770 return -ENOMEM;
771 }
772
773 crash_memory_ranges = new_array;
774 crash_memory_ranges_size = new_size;
775 max_crash_mem_ranges = (new_size /
776 sizeof(struct fad_crash_memory_ranges));
777 return 0;
778}
779
780static inline int fadump_add_crash_memory(unsigned long long base,
781 unsigned long long end)
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000782{
783 if (base == end)
Hari Bathini1ae31742018-08-07 02:12:45 +0530784 return 0;
785
786 if (crash_mem_ranges == max_crash_mem_ranges) {
787 int ret;
788
789 ret = allocate_crash_memory_ranges();
790 if (ret)
791 return ret;
792 }
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000793
794 pr_debug("crash_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n",
795 crash_mem_ranges, base, end - 1, (end - base));
796 crash_memory_ranges[crash_mem_ranges].base = base;
797 crash_memory_ranges[crash_mem_ranges].size = end - base;
798 crash_mem_ranges++;
Hari Bathini1ae31742018-08-07 02:12:45 +0530799 return 0;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000800}
801
Hari Bathini1ae31742018-08-07 02:12:45 +0530802static int fadump_exclude_reserved_area(unsigned long long start,
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000803 unsigned long long end)
804{
805 unsigned long long ra_start, ra_end;
Hari Bathini1ae31742018-08-07 02:12:45 +0530806 int ret = 0;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000807
808 ra_start = fw_dump.reserve_dump_area_start;
809 ra_end = ra_start + fw_dump.reserve_dump_area_size;
810
811 if ((ra_start < end) && (ra_end > start)) {
812 if ((start < ra_start) && (end > ra_end)) {
Hari Bathini1ae31742018-08-07 02:12:45 +0530813 ret = fadump_add_crash_memory(start, ra_start);
814 if (ret)
815 return ret;
816
817 ret = fadump_add_crash_memory(ra_end, end);
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000818 } else if (start < ra_start) {
Hari Bathini1ae31742018-08-07 02:12:45 +0530819 ret = fadump_add_crash_memory(start, ra_start);
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000820 } else if (ra_end < end) {
Hari Bathini1ae31742018-08-07 02:12:45 +0530821 ret = fadump_add_crash_memory(ra_end, end);
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000822 }
823 } else
Hari Bathini1ae31742018-08-07 02:12:45 +0530824 ret = fadump_add_crash_memory(start, end);
825
826 return ret;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000827}
828
829static int fadump_init_elfcore_header(char *bufp)
830{
831 struct elfhdr *elf;
832
833 elf = (struct elfhdr *) bufp;
834 bufp += sizeof(struct elfhdr);
835 memcpy(elf->e_ident, ELFMAG, SELFMAG);
836 elf->e_ident[EI_CLASS] = ELF_CLASS;
837 elf->e_ident[EI_DATA] = ELF_DATA;
838 elf->e_ident[EI_VERSION] = EV_CURRENT;
839 elf->e_ident[EI_OSABI] = ELF_OSABI;
840 memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
841 elf->e_type = ET_CORE;
842 elf->e_machine = ELF_ARCH;
843 elf->e_version = EV_CURRENT;
844 elf->e_entry = 0;
845 elf->e_phoff = sizeof(struct elfhdr);
846 elf->e_shoff = 0;
Daniel Axtensd8bced22016-09-06 15:32:42 +1000847#if defined(_CALL_ELF)
848 elf->e_flags = _CALL_ELF;
849#else
850 elf->e_flags = 0;
851#endif
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000852 elf->e_ehsize = sizeof(struct elfhdr);
853 elf->e_phentsize = sizeof(struct elf_phdr);
854 elf->e_phnum = 0;
855 elf->e_shentsize = 0;
856 elf->e_shnum = 0;
857 elf->e_shstrndx = 0;
858
859 return 0;
860}
861
862/*
863 * Traverse through memblock structure and setup crash memory ranges. These
864 * ranges will be used create PT_LOAD program headers in elfcore header.
865 */
Hari Bathini1ae31742018-08-07 02:12:45 +0530866static int fadump_setup_crash_memory_ranges(void)
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000867{
868 struct memblock_region *reg;
869 unsigned long long start, end;
Hari Bathini1ae31742018-08-07 02:12:45 +0530870 int ret;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000871
872 pr_debug("Setup crash memory ranges.\n");
873 crash_mem_ranges = 0;
874 /*
875 * add the first memory chunk (RMA_START through boot_memory_size) as
876 * a separate memory chunk. The reason is, at the time crash firmware
877 * will move the content of this memory chunk to different location
878 * specified during fadump registration. We need to create a separate
879 * program header for this chunk with the correct offset.
880 */
Hari Bathini1ae31742018-08-07 02:12:45 +0530881 ret = fadump_add_crash_memory(RMA_START, fw_dump.boot_memory_size);
882 if (ret)
883 return ret;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000884
885 for_each_memblock(memory, reg) {
886 start = (unsigned long long)reg->base;
887 end = start + (unsigned long long)reg->size;
888 if (start == RMA_START && end >= fw_dump.boot_memory_size)
889 start = fw_dump.boot_memory_size;
890
891 /* add this range excluding the reserved dump area. */
Hari Bathini1ae31742018-08-07 02:12:45 +0530892 ret = fadump_exclude_reserved_area(start, end);
893 if (ret)
894 return ret;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000895 }
Hari Bathini1ae31742018-08-07 02:12:45 +0530896
897 return 0;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000898}
899
Mahesh Salgaonkard34c5f22012-02-16 01:14:53 +0000900/*
901 * If the given physical address falls within the boot memory region then
902 * return the relocated address that points to the dump region reserved
903 * for saving initial boot memory contents.
904 */
905static inline unsigned long fadump_relocate(unsigned long paddr)
906{
907 if (paddr > RMA_START && paddr < fw_dump.boot_memory_size)
Hari Bathini408cddd2014-10-01 12:32:30 +0530908 return be64_to_cpu(fdm.rmr_region.destination_address) + paddr;
Mahesh Salgaonkard34c5f22012-02-16 01:14:53 +0000909 else
910 return paddr;
911}
912
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000913static int fadump_create_elfcore_headers(char *bufp)
914{
915 struct elfhdr *elf;
916 struct elf_phdr *phdr;
917 int i;
918
919 fadump_init_elfcore_header(bufp);
920 elf = (struct elfhdr *)bufp;
921 bufp += sizeof(struct elfhdr);
922
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000923 /*
924 * setup ELF PT_NOTE, place holder for cpu notes info. The notes info
925 * will be populated during second kernel boot after crash. Hence
926 * this PT_NOTE will always be the first elf note.
927 *
928 * NOTE: Any new ELF note addition should be placed after this note.
929 */
930 phdr = (struct elf_phdr *)bufp;
931 bufp += sizeof(struct elf_phdr);
932 phdr->p_type = PT_NOTE;
933 phdr->p_flags = 0;
934 phdr->p_vaddr = 0;
935 phdr->p_align = 0;
936
937 phdr->p_offset = 0;
938 phdr->p_paddr = 0;
939 phdr->p_filesz = 0;
940 phdr->p_memsz = 0;
941
942 (elf->e_phnum)++;
943
Mahesh Salgaonkard34c5f22012-02-16 01:14:53 +0000944 /* setup ELF PT_NOTE for vmcoreinfo */
945 phdr = (struct elf_phdr *)bufp;
946 bufp += sizeof(struct elf_phdr);
947 phdr->p_type = PT_NOTE;
948 phdr->p_flags = 0;
949 phdr->p_vaddr = 0;
950 phdr->p_align = 0;
951
952 phdr->p_paddr = fadump_relocate(paddr_vmcoreinfo_note());
953 phdr->p_offset = phdr->p_paddr;
954 phdr->p_memsz = vmcoreinfo_max_size;
955 phdr->p_filesz = vmcoreinfo_max_size;
956
957 /* Increment number of program headers. */
958 (elf->e_phnum)++;
959
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000960 /* setup PT_LOAD sections. */
961
962 for (i = 0; i < crash_mem_ranges; i++) {
963 unsigned long long mbase, msize;
964 mbase = crash_memory_ranges[i].base;
965 msize = crash_memory_ranges[i].size;
966
967 if (!msize)
968 continue;
969
970 phdr = (struct elf_phdr *)bufp;
971 bufp += sizeof(struct elf_phdr);
972 phdr->p_type = PT_LOAD;
973 phdr->p_flags = PF_R|PF_W|PF_X;
974 phdr->p_offset = mbase;
975
976 if (mbase == RMA_START) {
977 /*
978 * The entire RMA region will be moved by firmware
979 * to the specified destination_address. Hence set
980 * the correct offset.
981 */
Hari Bathini408cddd2014-10-01 12:32:30 +0530982 phdr->p_offset = be64_to_cpu(fdm.rmr_region.destination_address);
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +0000983 }
984
985 phdr->p_paddr = mbase;
986 phdr->p_vaddr = (unsigned long)__va(mbase);
987 phdr->p_filesz = msize;
988 phdr->p_memsz = msize;
989 phdr->p_align = 0;
990
991 /* Increment number of program headers. */
992 (elf->e_phnum)++;
993 }
994 return 0;
995}
996
997static unsigned long init_fadump_header(unsigned long addr)
998{
999 struct fadump_crash_info_header *fdh;
1000
1001 if (!addr)
1002 return 0;
1003
1004 fw_dump.fadumphdr_addr = addr;
1005 fdh = __va(addr);
1006 addr += sizeof(struct fadump_crash_info_header);
1007
1008 memset(fdh, 0, sizeof(struct fadump_crash_info_header));
1009 fdh->magic_number = FADUMP_CRASH_INFO_MAGIC;
1010 fdh->elfcorehdr_addr = addr;
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +00001011 /* We will set the crashing cpu id in crash_fadump() during crash. */
1012 fdh->crashing_cpu = CPU_UNKNOWN;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001013
1014 return addr;
1015}
1016
Michal Suchanekd3d39d22017-05-27 17:46:15 +02001017static int register_fadump(void)
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001018{
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001019 unsigned long addr;
1020 void *vaddr;
Hari Bathini1ae31742018-08-07 02:12:45 +05301021 int ret;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001022
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001023 /*
1024 * If no memory is reserved then we can not register for firmware-
1025 * assisted dump.
1026 */
1027 if (!fw_dump.reserve_dump_area_size)
Michal Suchanekd3d39d22017-05-27 17:46:15 +02001028 return -ENODEV;
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001029
Hari Bathini1ae31742018-08-07 02:12:45 +05301030 ret = fadump_setup_crash_memory_ranges();
1031 if (ret)
1032 return ret;
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001033
Hari Bathini408cddd2014-10-01 12:32:30 +05301034 addr = be64_to_cpu(fdm.rmr_region.destination_address) + be64_to_cpu(fdm.rmr_region.source_len);
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001035 /* Initialize fadump crash info header. */
1036 addr = init_fadump_header(addr);
1037 vaddr = __va(addr);
1038
1039 pr_debug("Creating ELF core headers at %#016lx\n", addr);
1040 fadump_create_elfcore_headers(vaddr);
1041
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001042 /* register the future kernel dump with firmware. */
Michal Suchanekd3d39d22017-05-27 17:46:15 +02001043 return register_fw_dump(&fdm);
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001044}
1045
1046static int fadump_unregister_dump(struct fadump_mem_struct *fdm)
1047{
1048 int rc = 0;
1049 unsigned int wait_time;
1050
1051 pr_debug("Un-register firmware-assisted dump\n");
1052
1053 /* TODO: Add upper time limit for the delay */
1054 do {
1055 rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
1056 FADUMP_UNREGISTER, fdm,
1057 sizeof(struct fadump_mem_struct));
1058
1059 wait_time = rtas_busy_delay_time(rc);
1060 if (wait_time)
1061 mdelay(wait_time);
1062 } while (wait_time);
1063
1064 if (rc) {
1065 printk(KERN_ERR "Failed to un-register firmware-assisted dump."
1066 " unexpected error(%d).\n", rc);
1067 return rc;
1068 }
1069 fw_dump.dump_registered = 0;
1070 return 0;
1071}
1072
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001073static int fadump_invalidate_dump(struct fadump_mem_struct *fdm)
1074{
1075 int rc = 0;
1076 unsigned int wait_time;
1077
1078 pr_debug("Invalidating firmware-assisted dump registration\n");
1079
1080 /* TODO: Add upper time limit for the delay */
1081 do {
1082 rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
1083 FADUMP_INVALIDATE, fdm,
1084 sizeof(struct fadump_mem_struct));
1085
1086 wait_time = rtas_busy_delay_time(rc);
1087 if (wait_time)
1088 mdelay(wait_time);
1089 } while (wait_time);
1090
1091 if (rc) {
Colin Ian King4a037492016-06-27 12:07:41 +01001092 pr_err("Failed to invalidate firmware-assisted dump registration. Unexpected error (%d).\n", rc);
Michael Ellermanb5b1cfc2016-07-05 23:45:56 +10001093 return rc;
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001094 }
1095 fw_dump.dump_active = 0;
1096 fdm_active = NULL;
1097 return 0;
1098}
1099
1100void fadump_cleanup(void)
1101{
1102 /* Invalidate the registration only if dump is active. */
1103 if (fw_dump.dump_active) {
1104 init_fadump_mem_struct(&fdm,
Hari Bathini408cddd2014-10-01 12:32:30 +05301105 be64_to_cpu(fdm_active->cpu_state_data.destination_address));
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001106 fadump_invalidate_dump(&fdm);
Mahesh Salgaonkar81d6e7152018-04-27 11:53:18 +05301107 } else if (fw_dump.dump_registered) {
1108 /* Un-register Firmware-assisted dump if it was registered. */
1109 fadump_unregister_dump(&fdm);
Hari Bathini1ae31742018-08-07 02:12:45 +05301110 free_crash_memory_ranges();
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001111 }
1112}
1113
1114/*
1115 * Release the memory that was reserved in early boot to preserve the memory
1116 * contents. The released memory will be available for general use.
1117 */
1118static void fadump_release_memory(unsigned long begin, unsigned long end)
1119{
1120 unsigned long addr;
1121 unsigned long ra_start, ra_end;
1122
1123 ra_start = fw_dump.reserve_dump_area_start;
1124 ra_end = ra_start + fw_dump.reserve_dump_area_size;
1125
1126 for (addr = begin; addr < end; addr += PAGE_SIZE) {
1127 /*
1128 * exclude the dump reserve area. Will reuse it for next
1129 * fadump registration.
1130 */
1131 if (addr <= ra_end && ((addr + PAGE_SIZE) > ra_start))
1132 continue;
1133
Jiang Liu5d585e52013-04-29 15:06:47 -07001134 free_reserved_page(pfn_to_page(addr >> PAGE_SHIFT));
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001135 }
1136}
1137
1138static void fadump_invalidate_release_mem(void)
1139{
1140 unsigned long reserved_area_start, reserved_area_end;
1141 unsigned long destination_address;
1142
1143 mutex_lock(&fadump_mutex);
1144 if (!fw_dump.dump_active) {
1145 mutex_unlock(&fadump_mutex);
1146 return;
1147 }
1148
Hari Bathini408cddd2014-10-01 12:32:30 +05301149 destination_address = be64_to_cpu(fdm_active->cpu_state_data.destination_address);
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001150 fadump_cleanup();
1151 mutex_unlock(&fadump_mutex);
1152
1153 /*
1154 * Save the current reserved memory bounds we will require them
1155 * later for releasing the memory for general use.
1156 */
1157 reserved_area_start = fw_dump.reserve_dump_area_start;
1158 reserved_area_end = reserved_area_start +
1159 fw_dump.reserve_dump_area_size;
1160 /*
1161 * Setup reserve_dump_area_start and its size so that we can
1162 * reuse this reserved memory for Re-registration.
1163 */
1164 fw_dump.reserve_dump_area_start = destination_address;
1165 fw_dump.reserve_dump_area_size = get_fadump_area_size();
1166
1167 fadump_release_memory(reserved_area_start, reserved_area_end);
1168 if (fw_dump.cpu_notes_buf) {
1169 fadump_cpu_notes_buf_free(
1170 (unsigned long)__va(fw_dump.cpu_notes_buf),
1171 fw_dump.cpu_notes_buf_size);
1172 fw_dump.cpu_notes_buf = 0;
1173 fw_dump.cpu_notes_buf_size = 0;
1174 }
1175 /* Initialize the kernel dump memory structure for FAD registration. */
1176 init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
1177}
1178
1179static ssize_t fadump_release_memory_store(struct kobject *kobj,
1180 struct kobj_attribute *attr,
1181 const char *buf, size_t count)
1182{
1183 if (!fw_dump.dump_active)
1184 return -EPERM;
1185
1186 if (buf[0] == '1') {
1187 /*
1188 * Take away the '/proc/vmcore'. We are releasing the dump
1189 * memory, hence it will not be valid anymore.
1190 */
Michael Ellerman2685f822016-09-30 10:51:46 +10001191#ifdef CONFIG_PROC_VMCORE
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001192 vmcore_cleanup();
Michael Ellerman2685f822016-09-30 10:51:46 +10001193#endif
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001194 fadump_invalidate_release_mem();
1195
1196 } else
1197 return -EINVAL;
1198 return count;
1199}
1200
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001201static ssize_t fadump_enabled_show(struct kobject *kobj,
1202 struct kobj_attribute *attr,
1203 char *buf)
1204{
1205 return sprintf(buf, "%d\n", fw_dump.fadump_enabled);
1206}
1207
1208static ssize_t fadump_register_show(struct kobject *kobj,
1209 struct kobj_attribute *attr,
1210 char *buf)
1211{
1212 return sprintf(buf, "%d\n", fw_dump.dump_registered);
1213}
1214
1215static ssize_t fadump_register_store(struct kobject *kobj,
1216 struct kobj_attribute *attr,
1217 const char *buf, size_t count)
1218{
1219 int ret = 0;
1220
1221 if (!fw_dump.fadump_enabled || fdm_active)
1222 return -EPERM;
1223
1224 mutex_lock(&fadump_mutex);
1225
1226 switch (buf[0]) {
1227 case '0':
1228 if (fw_dump.dump_registered == 0) {
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001229 goto unlock_out;
1230 }
1231 /* Un-register Firmware-assisted dump */
1232 fadump_unregister_dump(&fdm);
1233 break;
1234 case '1':
1235 if (fw_dump.dump_registered == 1) {
Michal Suchanekd3d39d22017-05-27 17:46:15 +02001236 ret = -EEXIST;
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001237 goto unlock_out;
1238 }
1239 /* Register Firmware-assisted dump */
Michal Suchanekd3d39d22017-05-27 17:46:15 +02001240 ret = register_fadump();
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001241 break;
1242 default:
1243 ret = -EINVAL;
1244 break;
1245 }
1246
1247unlock_out:
1248 mutex_unlock(&fadump_mutex);
1249 return ret < 0 ? ret : count;
1250}
1251
1252static int fadump_region_show(struct seq_file *m, void *private)
1253{
1254 const struct fadump_mem_struct *fdm_ptr;
1255
1256 if (!fw_dump.fadump_enabled)
1257 return 0;
1258
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001259 mutex_lock(&fadump_mutex);
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001260 if (fdm_active)
1261 fdm_ptr = fdm_active;
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001262 else {
1263 mutex_unlock(&fadump_mutex);
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001264 fdm_ptr = &fdm;
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001265 }
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001266
1267 seq_printf(m,
1268 "CPU : [%#016llx-%#016llx] %#llx bytes, "
1269 "Dumped: %#llx\n",
Hari Bathini408cddd2014-10-01 12:32:30 +05301270 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address),
1271 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) +
1272 be64_to_cpu(fdm_ptr->cpu_state_data.source_len) - 1,
1273 be64_to_cpu(fdm_ptr->cpu_state_data.source_len),
1274 be64_to_cpu(fdm_ptr->cpu_state_data.bytes_dumped));
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001275 seq_printf(m,
1276 "HPTE: [%#016llx-%#016llx] %#llx bytes, "
1277 "Dumped: %#llx\n",
Hari Bathini408cddd2014-10-01 12:32:30 +05301278 be64_to_cpu(fdm_ptr->hpte_region.destination_address),
1279 be64_to_cpu(fdm_ptr->hpte_region.destination_address) +
1280 be64_to_cpu(fdm_ptr->hpte_region.source_len) - 1,
1281 be64_to_cpu(fdm_ptr->hpte_region.source_len),
1282 be64_to_cpu(fdm_ptr->hpte_region.bytes_dumped));
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001283 seq_printf(m,
1284 "DUMP: [%#016llx-%#016llx] %#llx bytes, "
1285 "Dumped: %#llx\n",
Hari Bathini408cddd2014-10-01 12:32:30 +05301286 be64_to_cpu(fdm_ptr->rmr_region.destination_address),
1287 be64_to_cpu(fdm_ptr->rmr_region.destination_address) +
1288 be64_to_cpu(fdm_ptr->rmr_region.source_len) - 1,
1289 be64_to_cpu(fdm_ptr->rmr_region.source_len),
1290 be64_to_cpu(fdm_ptr->rmr_region.bytes_dumped));
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001291
1292 if (!fdm_active ||
1293 (fw_dump.reserve_dump_area_start ==
Hari Bathini408cddd2014-10-01 12:32:30 +05301294 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address)))
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001295 goto out;
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001296
1297 /* Dump is active. Show reserved memory region. */
1298 seq_printf(m,
1299 " : [%#016llx-%#016llx] %#llx bytes, "
1300 "Dumped: %#llx\n",
1301 (unsigned long long)fw_dump.reserve_dump_area_start,
Hari Bathini408cddd2014-10-01 12:32:30 +05301302 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) - 1,
1303 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) -
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001304 fw_dump.reserve_dump_area_start,
Hari Bathini408cddd2014-10-01 12:32:30 +05301305 be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) -
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001306 fw_dump.reserve_dump_area_start);
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001307out:
1308 if (fdm_active)
1309 mutex_unlock(&fadump_mutex);
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001310 return 0;
1311}
1312
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001313static struct kobj_attribute fadump_release_attr = __ATTR(fadump_release_mem,
1314 0200, NULL,
1315 fadump_release_memory_store);
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001316static struct kobj_attribute fadump_attr = __ATTR(fadump_enabled,
1317 0444, fadump_enabled_show,
1318 NULL);
1319static struct kobj_attribute fadump_register_attr = __ATTR(fadump_registered,
1320 0644, fadump_register_show,
1321 fadump_register_store);
1322
1323static int fadump_region_open(struct inode *inode, struct file *file)
1324{
1325 return single_open(file, fadump_region_show, inode->i_private);
1326}
1327
1328static const struct file_operations fadump_region_fops = {
1329 .open = fadump_region_open,
1330 .read = seq_read,
1331 .llseek = seq_lseek,
1332 .release = single_release,
1333};
1334
1335static void fadump_init_files(void)
1336{
1337 struct dentry *debugfs_file;
1338 int rc = 0;
1339
1340 rc = sysfs_create_file(kernel_kobj, &fadump_attr.attr);
1341 if (rc)
1342 printk(KERN_ERR "fadump: unable to create sysfs file"
1343 " fadump_enabled (%d)\n", rc);
1344
1345 rc = sysfs_create_file(kernel_kobj, &fadump_register_attr.attr);
1346 if (rc)
1347 printk(KERN_ERR "fadump: unable to create sysfs file"
1348 " fadump_registered (%d)\n", rc);
1349
1350 debugfs_file = debugfs_create_file("fadump_region", 0444,
1351 powerpc_debugfs_root, NULL,
1352 &fadump_region_fops);
1353 if (!debugfs_file)
1354 printk(KERN_ERR "fadump: unable to create debugfs file"
1355 " fadump_region\n");
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001356
1357 if (fw_dump.dump_active) {
1358 rc = sysfs_create_file(kernel_kobj, &fadump_release_attr.attr);
1359 if (rc)
1360 printk(KERN_ERR "fadump: unable to create sysfs file"
1361 " fadump_release_mem (%d)\n", rc);
1362 }
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001363 return;
1364}
1365
1366/*
1367 * Prepare for firmware-assisted dump.
1368 */
1369int __init setup_fadump(void)
1370{
1371 if (!fw_dump.fadump_enabled)
1372 return 0;
1373
1374 if (!fw_dump.fadump_supported) {
1375 printk(KERN_ERR "Firmware-assisted dump is not supported on"
1376 " this hardware\n");
1377 return 0;
1378 }
1379
1380 fadump_show_config();
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001381 /*
1382 * If dump data is available then see if it is valid and prepare for
1383 * saving it to the disk.
1384 */
Mahesh Salgaonkarb500aff2012-02-16 01:15:08 +00001385 if (fw_dump.dump_active) {
1386 /*
1387 * if dump process fails then invalidate the registration
1388 * and release memory before proceeding for re-registration.
1389 */
1390 if (process_fadump(fdm_active) < 0)
1391 fadump_invalidate_release_mem();
1392 }
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001393 /* Initialize the kernel dump memory structure for FAD registration. */
Mahesh Salgaonkar2df173d2012-02-16 01:14:37 +00001394 else if (fw_dump.reserve_dump_area_size)
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +00001395 init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
1396 fadump_init_files();
1397
1398 return 1;
1399}
1400subsys_initcall(setup_fadump);