Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Firmware Assisted dump: A robust mechanism to get reliable kernel crash |
| 4 | * dump with assistance from firmware. This approach does not use kexec, |
| 5 | * instead firmware assists in booting the kdump kernel while preserving |
| 6 | * memory contents. The most of the code implementation has been adapted |
| 7 | * from phyp assisted dump implementation written by Linas Vepstas and |
| 8 | * Manish Ahuja |
| 9 | * |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 10 | * Copyright 2011 IBM Corporation |
| 11 | * Author: Mahesh Salgaonkar <mahesh@linux.vnet.ibm.com> |
| 12 | */ |
| 13 | |
| 14 | #undef DEBUG |
| 15 | #define pr_fmt(fmt) "fadump: " fmt |
| 16 | |
| 17 | #include <linux/string.h> |
| 18 | #include <linux/memblock.h> |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 19 | #include <linux/delay.h> |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 20 | #include <linux/seq_file.h> |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 21 | #include <linux/crash_dump.h> |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 22 | #include <linux/kobject.h> |
| 23 | #include <linux/sysfs.h> |
Hari Bathini | a581831 | 2018-08-18 02:10:56 +0530 | [diff] [blame] | 24 | #include <linux/slab.h> |
Mahesh Salgaonkar | a4e92ce | 2018-08-20 13:47:17 +0530 | [diff] [blame] | 25 | #include <linux/cma.h> |
Christophe Leroy | 45d0ba5 | 2019-04-26 05:59:47 +0000 | [diff] [blame] | 26 | #include <linux/hugetlb.h> |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 27 | |
Michael Ellerman | 7644d58 | 2017-02-10 12:04:56 +1100 | [diff] [blame] | 28 | #include <asm/debugfs.h> |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 29 | #include <asm/page.h> |
| 30 | #include <asm/prom.h> |
| 31 | #include <asm/rtas.h> |
| 32 | #include <asm/fadump.h> |
Hari Bathini | ca986d7 | 2019-09-11 20:16:21 +0530 | [diff] [blame] | 33 | #include <asm/fadump-internal.h> |
Stephen Rothwell | cad3c83 | 2012-03-30 14:01:07 +0000 | [diff] [blame] | 34 | #include <asm/setup.h> |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 35 | |
Hari Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 36 | #include "../platforms/pseries/rtas-fadump.h" |
| 37 | |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 38 | static struct fw_dump fw_dump; |
Hari Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 39 | static struct rtas_fadump_mem_struct fdm; |
| 40 | static const struct rtas_fadump_mem_struct *fdm_active; |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 41 | |
| 42 | static DEFINE_MUTEX(fadump_mutex); |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 43 | struct fad_crash_memory_ranges *crash_memory_ranges; |
| 44 | int crash_memory_ranges_size; |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 45 | int crash_mem_ranges; |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 46 | int max_crash_mem_ranges; |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 47 | |
Mahesh Salgaonkar | a4e92ce | 2018-08-20 13:47:17 +0530 | [diff] [blame] | 48 | #ifdef CONFIG_CMA |
Hari Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 49 | static struct cma *fadump_cma; |
| 50 | |
Mahesh Salgaonkar | a4e92ce | 2018-08-20 13:47:17 +0530 | [diff] [blame] | 51 | /* |
| 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 | */ |
| 64 | int __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 |
| 108 | static int __init fadump_cma_init(void) { return 1; } |
| 109 | #endif /* CONFIG_CMA */ |
| 110 | |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 111 | /* Scan the Firmware Assisted dump configuration details. */ |
| 112 | int __init early_init_dt_scan_fw_dump(unsigned long node, |
| 113 | const char *uname, int depth, void *data) |
| 114 | { |
Rob Herring | 9d0c4df | 2014-04-01 23:49:03 -0500 | [diff] [blame] | 115 | const __be32 *sections; |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 116 | int i, num_sections; |
Rob Herring | 9d0c4df | 2014-04-01 23:49:03 -0500 | [diff] [blame] | 117 | int size; |
Hari Bathini | 408cddd | 2014-10-01 12:32:30 +0530 | [diff] [blame] | 118 | const __be32 *token; |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 119 | |
| 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 Shan | a7d04317 | 2014-04-24 18:00:31 +1000 | [diff] [blame] | 129 | return 1; |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 130 | |
| 131 | fw_dump.fadump_supported = 1; |
Hari Bathini | 408cddd | 2014-10-01 12:32:30 +0530 | [diff] [blame] | 132 | fw_dump.ibm_configure_kernel_dump = be32_to_cpu(*token); |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 133 | |
| 134 | /* |
| 135 | * The 'ibm,kernel-dump' rtas node is present only if there is |
| 136 | * dump data waiting for us. |
| 137 | */ |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 138 | fdm_active = of_get_flat_dt_prop(node, "ibm,kernel-dump", NULL); |
| 139 | if (fdm_active) |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 140 | 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 Shan | a7d04317 | 2014-04-24 18:00:31 +1000 | [diff] [blame] | 152 | return 1; |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 153 | |
| 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 Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 160 | case RTAS_FADUMP_CPU_STATE_DATA: |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 161 | fw_dump.cpu_state_data_size = |
| 162 | of_read_ulong(§ions[1], 2); |
| 163 | break; |
Hari Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 164 | case RTAS_FADUMP_HPTE_REGION: |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 165 | fw_dump.hpte_region_size = |
| 166 | of_read_ulong(§ions[1], 2); |
| 167 | break; |
| 168 | } |
| 169 | } |
Gavin Shan | a7d04317 | 2014-04-24 18:00:31 +1000 | [diff] [blame] | 170 | |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 171 | return 1; |
| 172 | } |
| 173 | |
Hari Bathini | eae0dfc | 2017-06-01 22:51:26 +0530 | [diff] [blame] | 174 | /* |
| 175 | * If fadump is registered, check if the memory provided |
Mahesh Salgaonkar | 0db6896 | 2018-08-20 13:47:32 +0530 | [diff] [blame] | 176 | * falls within boot memory area and reserved memory area. |
Hari Bathini | eae0dfc | 2017-06-01 22:51:26 +0530 | [diff] [blame] | 177 | */ |
Mahesh Salgaonkar | 0db6896 | 2018-08-20 13:47:32 +0530 | [diff] [blame] | 178 | int is_fadump_memory_area(u64 addr, ulong size) |
Hari Bathini | eae0dfc | 2017-06-01 22:51:26 +0530 | [diff] [blame] | 179 | { |
Mahesh Salgaonkar | 0db6896 | 2018-08-20 13:47:32 +0530 | [diff] [blame] | 180 | u64 d_start = fw_dump.reserve_dump_area_start; |
| 181 | u64 d_end = d_start + fw_dump.reserve_dump_area_size; |
| 182 | |
Hari Bathini | eae0dfc | 2017-06-01 22:51:26 +0530 | [diff] [blame] | 183 | if (!fw_dump.dump_registered) |
| 184 | return 0; |
| 185 | |
Mahesh Salgaonkar | 0db6896 | 2018-08-20 13:47:32 +0530 | [diff] [blame] | 186 | if (((addr + size) > d_start) && (addr <= d_end)) |
| 187 | return 1; |
| 188 | |
Hari Bathini | eae0dfc | 2017-06-01 22:51:26 +0530 | [diff] [blame] | 189 | return (addr + size) > RMA_START && addr <= fw_dump.boot_memory_size; |
| 190 | } |
| 191 | |
Nicholas Piggin | 6fcd6ba | 2017-07-19 16:59:11 +1000 | [diff] [blame] | 192 | int should_fadump_crash(void) |
| 193 | { |
| 194 | if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr) |
| 195 | return 0; |
| 196 | return 1; |
| 197 | } |
| 198 | |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 199 | int is_fadump_active(void) |
| 200 | { |
| 201 | return fw_dump.dump_active; |
| 202 | } |
| 203 | |
Hari Bathini | a5a05b9 | 2017-06-01 22:52:10 +0530 | [diff] [blame] | 204 | /* |
Hari Bathini | 961cf26 | 2019-09-11 20:16:36 +0530 | [diff] [blame] | 205 | * Returns true, if there are no holes in memory area between d_start to d_end, |
| 206 | * false otherwise. |
Hari Bathini | a5a05b9 | 2017-06-01 22:52:10 +0530 | [diff] [blame] | 207 | */ |
Hari Bathini | 961cf26 | 2019-09-11 20:16:36 +0530 | [diff] [blame] | 208 | static bool is_fadump_mem_area_contiguous(u64 d_start, u64 d_end) |
Hari Bathini | a5a05b9 | 2017-06-01 22:52:10 +0530 | [diff] [blame] | 209 | { |
| 210 | struct memblock_region *reg; |
Hari Bathini | 961cf26 | 2019-09-11 20:16:36 +0530 | [diff] [blame] | 211 | bool ret = false; |
| 212 | u64 start, end; |
Hari Bathini | a5a05b9 | 2017-06-01 22:52:10 +0530 | [diff] [blame] | 213 | |
| 214 | for_each_memblock(memory, reg) { |
Hari Bathini | 961cf26 | 2019-09-11 20:16:36 +0530 | [diff] [blame] | 215 | 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 Bathini | a5a05b9 | 2017-06-01 22:52:10 +0530 | [diff] [blame] | 220 | break; |
| 221 | |
Hari Bathini | 961cf26 | 2019-09-11 20:16:36 +0530 | [diff] [blame] | 222 | if (end == d_end) { |
| 223 | ret = true; |
Hari Bathini | a5a05b9 | 2017-06-01 22:52:10 +0530 | [diff] [blame] | 224 | break; |
| 225 | } |
| 226 | |
Hari Bathini | 961cf26 | 2019-09-11 20:16:36 +0530 | [diff] [blame] | 227 | d_start = end + 1; |
Hari Bathini | a5a05b9 | 2017-06-01 22:52:10 +0530 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
| 231 | return ret; |
| 232 | } |
| 233 | |
Mahesh Salgaonkar | f86593b | 2018-08-20 13:47:24 +0530 | [diff] [blame] | 234 | /* |
Hari Bathini | 961cf26 | 2019-09-11 20:16:36 +0530 | [diff] [blame] | 235 | * Returns true, if there are no holes in boot memory area, |
| 236 | * false otherwise. |
| 237 | */ |
Hari Bathini | 7f0ad11 | 2019-09-11 20:16:52 +0530 | [diff] [blame] | 238 | bool is_fadump_boot_mem_contiguous(void) |
Hari Bathini | 961cf26 | 2019-09-11 20:16:36 +0530 | [diff] [blame] | 239 | { |
| 240 | return is_fadump_mem_area_contiguous(0, fw_dump.boot_memory_size); |
| 241 | } |
| 242 | |
| 243 | /* |
Mahesh Salgaonkar | f86593b | 2018-08-20 13:47:24 +0530 | [diff] [blame] | 244 | * Returns true, if there are no holes in reserved memory area, |
| 245 | * false otherwise. |
| 246 | */ |
Hari Bathini | 7f0ad11 | 2019-09-11 20:16:52 +0530 | [diff] [blame] | 247 | bool is_fadump_reserved_mem_contiguous(void) |
Mahesh Salgaonkar | f86593b | 2018-08-20 13:47:24 +0530 | [diff] [blame] | 248 | { |
Hari Bathini | 961cf26 | 2019-09-11 20:16:36 +0530 | [diff] [blame] | 249 | u64 d_start, d_end; |
Mahesh Salgaonkar | f86593b | 2018-08-20 13:47:24 +0530 | [diff] [blame] | 250 | |
Hari Bathini | 961cf26 | 2019-09-11 20:16:36 +0530 | [diff] [blame] | 251 | 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 Salgaonkar | f86593b | 2018-08-20 13:47:24 +0530 | [diff] [blame] | 254 | } |
| 255 | |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 256 | /* Print firmware assisted dump configurations for debugging purpose. */ |
| 257 | static 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 Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 275 | static unsigned long init_fadump_mem_struct(struct rtas_fadump_mem_struct *fdm, |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 276 | unsigned long addr) |
| 277 | { |
| 278 | if (!fdm) |
| 279 | return 0; |
| 280 | |
Hari Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 281 | memset(fdm, 0, sizeof(struct rtas_fadump_mem_struct)); |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 282 | addr = addr & PAGE_MASK; |
| 283 | |
Hari Bathini | 408cddd | 2014-10-01 12:32:30 +0530 | [diff] [blame] | 284 | fdm->header.dump_format_version = cpu_to_be32(0x00000001); |
| 285 | fdm->header.dump_num_sections = cpu_to_be16(3); |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 286 | fdm->header.dump_status_flag = 0; |
| 287 | fdm->header.offset_first_dump_section = |
Hari Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 288 | cpu_to_be32((u32)offsetof(struct rtas_fadump_mem_struct, cpu_state_data)); |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 289 | |
| 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 Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 304 | 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 Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 306 | fdm->cpu_state_data.source_address = 0; |
Hari Bathini | 408cddd | 2014-10-01 12:32:30 +0530 | [diff] [blame] | 307 | 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 Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 309 | addr += fw_dump.cpu_state_data_size; |
| 310 | |
| 311 | /* hpte region section */ |
Hari Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 312 | 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 Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 314 | fdm->hpte_region.source_address = 0; |
Hari Bathini | 408cddd | 2014-10-01 12:32:30 +0530 | [diff] [blame] | 315 | fdm->hpte_region.source_len = cpu_to_be64(fw_dump.hpte_region_size); |
| 316 | fdm->hpte_region.destination_address = cpu_to_be64(addr); |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 317 | addr += fw_dump.hpte_region_size; |
| 318 | |
| 319 | /* RMA region section */ |
Hari Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 320 | 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 Bathini | 408cddd | 2014-10-01 12:32:30 +0530 | [diff] [blame] | 322 | 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 Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 325 | addr += fw_dump.boot_memory_size; |
| 326 | |
| 327 | return addr; |
| 328 | } |
| 329 | |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 330 | /** |
| 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 | */ |
| 345 | static inline unsigned long fadump_calculate_reserve_size(void) |
| 346 | { |
Hari Bathini | 11550dc | 2017-05-08 15:56:28 -0700 | [diff] [blame] | 347 | int ret; |
| 348 | unsigned long long base, size; |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 349 | |
Hari Bathini | 81d9eca | 2017-05-22 15:04:23 +0530 | [diff] [blame] | 350 | if (fw_dump.reserve_bootvar) |
| 351 | pr_warn("'fadump_reserve_mem=' parameter is deprecated in favor of 'crashkernel=' parameter.\n"); |
| 352 | |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 353 | /* |
Hari Bathini | 11550dc | 2017-05-08 15:56:28 -0700 | [diff] [blame] | 354 | * Check if the size is specified through crashkernel= cmdline |
Hari Bathini | e7467dc | 2017-05-22 15:04:47 +0530 | [diff] [blame] | 355 | * option. If yes, then use that but ignore base as fadump reserves |
| 356 | * memory at a predefined offset. |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 357 | */ |
Hari Bathini | 11550dc | 2017-05-08 15:56:28 -0700 | [diff] [blame] | 358 | ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(), |
| 359 | &size, &base); |
| 360 | if (ret == 0 && size > 0) { |
Hari Bathini | 48a316e | 2017-06-02 13:00:27 +0530 | [diff] [blame] | 361 | unsigned long max_size; |
| 362 | |
Hari Bathini | 81d9eca | 2017-05-22 15:04:23 +0530 | [diff] [blame] | 363 | if (fw_dump.reserve_bootvar) |
| 364 | pr_info("Using 'crashkernel=' parameter for memory reservation.\n"); |
| 365 | |
Hari Bathini | 11550dc | 2017-05-08 15:56:28 -0700 | [diff] [blame] | 366 | fw_dump.reserve_bootvar = (unsigned long)size; |
Hari Bathini | 48a316e | 2017-06-02 13:00:27 +0530 | [diff] [blame] | 367 | |
| 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 Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 379 | return fw_dump.reserve_bootvar; |
Hari Bathini | 81d9eca | 2017-05-22 15:04:23 +0530 | [diff] [blame] | 380 | } 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 Bathini | 11550dc | 2017-05-08 15:56:28 -0700 | [diff] [blame] | 386 | } |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 387 | |
| 388 | /* divide by 20 to get 5% of value */ |
Hari Bathini | 48a316e | 2017-06-02 13:00:27 +0530 | [diff] [blame] | 389 | size = memblock_phys_mem_size() / 20; |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 390 | |
| 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 | */ |
| 405 | static 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 Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 412 | size += sizeof(struct fadump_crash_info_header); |
| 413 | size += sizeof(struct elfhdr); /* ELF core header.*/ |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 414 | size += sizeof(struct elf_phdr); /* place holder for cpu notes */ |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 415 | /* Program headers for crash memory regions. */ |
| 416 | size += sizeof(struct elf_phdr) * (memblock_num_regions(memory) + 2); |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 417 | |
| 418 | size = PAGE_ALIGN(size); |
| 419 | return size; |
| 420 | } |
| 421 | |
Mahesh Salgaonkar | b71a693 | 2018-04-10 19:11:16 +0530 | [diff] [blame] | 422 | static 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 Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 442 | int __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 Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 455 | /* |
| 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 Bathini | 408cddd | 2014-10-01 12:32:30 +0530 | [diff] [blame] | 461 | fw_dump.boot_memory_size = be64_to_cpu(fdm_active->rmr_region.source_len); |
Mahesh Salgaonkar | a4e92ce | 2018-08-20 13:47:17 +0530 | [diff] [blame] | 462 | else { |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 463 | fw_dump.boot_memory_size = fadump_calculate_reserve_size(); |
Mahesh Salgaonkar | a4e92ce | 2018-08-20 13:47:17 +0530 | [diff] [blame] | 464 | #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 Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 471 | |
| 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 Poulose | a84fcd4 | 2012-08-21 01:42:33 +0000 | [diff] [blame] | 486 | " dump, now %#016llx\n", memory_limit); |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 487 | } |
| 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 Salgaonkar | b71a693 | 2018-04-10 19:11:16 +0530 | [diff] [blame] | 494 | pr_info("Firmware-assisted dump is active.\n"); |
| 495 | |
Hari Bathini | 8597538 | 2018-04-10 19:11:31 +0530 | [diff] [blame] | 496 | #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 Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 504 | /* |
| 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 Salgaonkar | b71a693 | 2018-04-10 19:11:16 +0530 | [diff] [blame] | 512 | fadump_reserve_crash_area(base, size); |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 513 | |
| 514 | fw_dump.fadumphdr_addr = |
Hari Bathini | 408cddd | 2014-10-01 12:32:30 +0530 | [diff] [blame] | 515 | be64_to_cpu(fdm_active->rmr_region.destination_address) + |
| 516 | be64_to_cpu(fdm_active->rmr_region.source_len); |
Mahesh Salgaonkar | a4e92ce | 2018-08-20 13:47:17 +0530 | [diff] [blame] | 517 | 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 Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 520 | } else { |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 521 | size = get_fadump_area_size(); |
Hari Bathini | f6e6bed | 2017-03-17 02:35:26 +0530 | [diff] [blame] | 522 | |
| 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 Bathini | f6e6bed | 2017-03-17 02:35:26 +0530 | [diff] [blame] | 547 | |
Mahesh Salgaonkar | a4e92ce | 2018-08-20 13:47:17 +0530 | [diff] [blame] | 548 | fw_dump.reserve_dump_area_start = base; |
| 549 | fw_dump.reserve_dump_area_size = size; |
| 550 | return fadump_cma_init(); |
| 551 | } |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 552 | return 1; |
| 553 | } |
| 554 | |
Srikar Dronamraju | 1e76609c | 2016-10-07 16:59:21 -0700 | [diff] [blame] | 555 | unsigned long __init arch_reserved_kernel_pages(void) |
| 556 | { |
| 557 | return memblock_reserved_size() / PAGE_SIZE; |
| 558 | } |
| 559 | |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 560 | /* Look for fadump= cmdline option. */ |
| 561 | static 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 Salgaonkar | a4e92ce | 2018-08-20 13:47:17 +0530 | [diff] [blame] | 570 | else if (strncmp(p, "nocma", 5) == 0) { |
| 571 | fw_dump.fadump_enabled = 1; |
| 572 | fw_dump.nocma = 1; |
| 573 | } |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 574 | |
| 575 | return 0; |
| 576 | } |
| 577 | early_param("fadump", early_fadump_param); |
| 578 | |
Hari Bathini | 81d9eca | 2017-05-22 15:04:23 +0530 | [diff] [blame] | 579 | /* |
| 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 | */ |
| 584 | static int __init early_fadump_reserve_mem(char *p) |
| 585 | { |
| 586 | if (p) |
| 587 | fw_dump.reserve_bootvar = memparse(p, &p); |
| 588 | return 0; |
| 589 | } |
| 590 | early_param("fadump_reserve_mem", early_fadump_reserve_mem); |
| 591 | |
Hari Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 592 | static int register_fw_dump(struct rtas_fadump_mem_struct *fdm) |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 593 | { |
Michal Suchanek | 98b8cd7 | 2017-05-27 17:46:15 +0200 | [diff] [blame] | 594 | int rc, err; |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 595 | 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 Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 603 | sizeof(struct rtas_fadump_mem_struct)); |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 604 | |
| 605 | wait_time = rtas_busy_delay_time(rc); |
| 606 | if (wait_time) |
| 607 | mdelay(wait_time); |
| 608 | |
| 609 | } while (wait_time); |
| 610 | |
Michal Suchanek | 98b8cd7 | 2017-05-27 17:46:15 +0200 | [diff] [blame] | 611 | err = -EIO; |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 612 | switch (rc) { |
Michal Suchanek | 98b8cd7 | 2017-05-27 17:46:15 +0200 | [diff] [blame] | 613 | default: |
| 614 | pr_err("Failed to register. Unknown Error(%d).\n", rc); |
| 615 | break; |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 616 | 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 Bathini | 7f0ad11 | 2019-09-11 20:16:52 +0530 | [diff] [blame] | 621 | if (!is_fadump_boot_mem_contiguous()) |
Mahesh Salgaonkar | f86593b | 2018-08-20 13:47:24 +0530 | [diff] [blame] | 622 | pr_err("Can't have holes in boot memory area while registering fadump\n"); |
Hari Bathini | 7f0ad11 | 2019-09-11 20:16:52 +0530 | [diff] [blame] | 623 | else if (!is_fadump_reserved_mem_contiguous()) |
Mahesh Salgaonkar | f86593b | 2018-08-20 13:47:24 +0530 | [diff] [blame] | 624 | pr_err("Can't have holes in reserved memory area while" |
| 625 | " registering fadump\n"); |
Hari Bathini | a5a05b9 | 2017-06-01 22:52:10 +0530 | [diff] [blame] | 626 | |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 627 | printk(KERN_ERR "Failed to register firmware-assisted kernel" |
| 628 | " dump. Parameter Error(%d).\n", rc); |
Michal Suchanek | 98b8cd7 | 2017-05-27 17:46:15 +0200 | [diff] [blame] | 629 | err = -EINVAL; |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 630 | break; |
| 631 | case -9: |
| 632 | printk(KERN_ERR "firmware-assisted kernel dump is already " |
| 633 | " registered."); |
| 634 | fw_dump.dump_registered = 1; |
Michal Suchanek | 98b8cd7 | 2017-05-27 17:46:15 +0200 | [diff] [blame] | 635 | err = -EEXIST; |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 636 | break; |
| 637 | case 0: |
| 638 | printk(KERN_INFO "firmware-assisted kernel dump registration" |
| 639 | " is successful\n"); |
| 640 | fw_dump.dump_registered = 1; |
Michal Suchanek | 98b8cd7 | 2017-05-27 17:46:15 +0200 | [diff] [blame] | 641 | err = 0; |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 642 | break; |
| 643 | } |
Michal Suchanek | 98b8cd7 | 2017-05-27 17:46:15 +0200 | [diff] [blame] | 644 | return err; |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 647 | void crash_fadump(struct pt_regs *regs, const char *str) |
| 648 | { |
| 649 | struct fadump_crash_info_header *fdh = NULL; |
Mahesh Salgaonkar | f2a5e8f | 2016-10-24 23:51:51 +0530 | [diff] [blame] | 650 | int old_cpu, this_cpu; |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 651 | |
Nicholas Piggin | 6fcd6ba | 2017-07-19 16:59:11 +1000 | [diff] [blame] | 652 | if (!should_fadump_crash()) |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 653 | return; |
| 654 | |
Mahesh Salgaonkar | f2a5e8f | 2016-10-24 23:51:51 +0530 | [diff] [blame] | 655 | /* |
| 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 Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 678 | fdh = __va(fw_dump.fadumphdr_addr); |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 679 | 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 Villemoes | a051216 | 2016-01-20 15:00:13 -0800 | [diff] [blame] | 687 | fdh->online_mask = *cpu_online_mask; |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 688 | |
| 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 |
| 694 | static inline int fadump_gpr_index(u64 id) |
| 695 | { |
| 696 | int i = -1; |
| 697 | char str[3]; |
| 698 | |
Hari Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 699 | if ((id & GPR_MASK) == fadump_str_to_u64("GPR")) { |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 700 | /* 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 | |
| 713 | static 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 Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 721 | else if (reg_id == fadump_str_to_u64("NIA")) |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 722 | regs->nip = (unsigned long)reg_val; |
Hari Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 723 | else if (reg_id == fadump_str_to_u64("MSR")) |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 724 | regs->msr = (unsigned long)reg_val; |
Hari Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 725 | else if (reg_id == fadump_str_to_u64("CTR")) |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 726 | regs->ctr = (unsigned long)reg_val; |
Hari Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 727 | else if (reg_id == fadump_str_to_u64("LR")) |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 728 | regs->link = (unsigned long)reg_val; |
Hari Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 729 | else if (reg_id == fadump_str_to_u64("XER")) |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 730 | regs->xer = (unsigned long)reg_val; |
Hari Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 731 | else if (reg_id == fadump_str_to_u64("CR")) |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 732 | regs->ccr = (unsigned long)reg_val; |
Hari Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 733 | else if (reg_id == fadump_str_to_u64("DAR")) |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 734 | regs->dar = (unsigned long)reg_val; |
Hari Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 735 | else if (reg_id == fadump_str_to_u64("DSISR")) |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 736 | regs->dsisr = (unsigned long)reg_val; |
| 737 | } |
| 738 | |
Hari Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 739 | static struct rtas_fadump_reg_entry* |
| 740 | fadump_read_registers(struct rtas_fadump_reg_entry *reg_entry, struct pt_regs *regs) |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 741 | { |
| 742 | memset(regs, 0, sizeof(struct pt_regs)); |
| 743 | |
Hari Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 744 | while (be64_to_cpu(reg_entry->reg_id) != fadump_str_to_u64("CPUEND")) { |
Hari Bathini | 408cddd | 2014-10-01 12:32:30 +0530 | [diff] [blame] | 745 | fadump_set_regval(regs, be64_to_cpu(reg_entry->reg_id), |
| 746 | be64_to_cpu(reg_entry->reg_value)); |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 747 | reg_entry++; |
| 748 | } |
| 749 | reg_entry++; |
| 750 | return reg_entry; |
| 751 | } |
| 752 | |
Hari Bathini | 7f0ad11 | 2019-09-11 20:16:52 +0530 | [diff] [blame] | 753 | u32 *fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs) |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 754 | { |
| 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 Bathini | 22bd017 | 2017-05-08 15:56:24 -0700 | [diff] [blame] | 763 | buf = append_elf_note(buf, CRASH_CORE_NOTE_NAME, NT_PRSTATUS, |
| 764 | &prstatus, sizeof(prstatus)); |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 765 | return buf; |
| 766 | } |
| 767 | |
Hari Bathini | 7f0ad11 | 2019-09-11 20:16:52 +0530 | [diff] [blame] | 768 | void fadump_update_elfcore_header(char *bufp) |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 769 | { |
| 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 Bathini | 961cf26 | 2019-09-11 20:16:36 +0530 | [diff] [blame] | 780 | phdr->p_paddr = __pa(fw_dump.cpu_notes_buf_vaddr); |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 781 | 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 Bathini | 961cf26 | 2019-09-11 20:16:36 +0530 | [diff] [blame] | 788 | static void *fadump_alloc_buffer(unsigned long size) |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 789 | { |
Hari Bathini | 72aa651 | 2019-09-11 20:17:56 +0530 | [diff] [blame] | 790 | unsigned long count, i; |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 791 | struct page *page; |
Hari Bathini | 72aa651 | 2019-09-11 20:17:56 +0530 | [diff] [blame] | 792 | void *vaddr; |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 793 | |
Hari Bathini | 72aa651 | 2019-09-11 20:17:56 +0530 | [diff] [blame] | 794 | vaddr = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO); |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 795 | if (!vaddr) |
| 796 | return NULL; |
| 797 | |
Hari Bathini | 72aa651 | 2019-09-11 20:17:56 +0530 | [diff] [blame] | 798 | count = PAGE_ALIGN(size) / PAGE_SIZE; |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 799 | page = virt_to_page(vaddr); |
| 800 | for (i = 0; i < count; i++) |
Hari Bathini | 72aa651 | 2019-09-11 20:17:56 +0530 | [diff] [blame] | 801 | mark_page_reserved(page + i); |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 802 | return vaddr; |
| 803 | } |
| 804 | |
Hari Bathini | 961cf26 | 2019-09-11 20:16:36 +0530 | [diff] [blame] | 805 | static void fadump_free_buffer(unsigned long vaddr, unsigned long size) |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 806 | { |
Hari Bathini | 72aa651 | 2019-09-11 20:17:56 +0530 | [diff] [blame] | 807 | free_reserved_area((void *)vaddr, (void *)(vaddr + size), -1, NULL); |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Hari Bathini | 7f0ad11 | 2019-09-11 20:16:52 +0530 | [diff] [blame] | 810 | s32 fadump_setup_cpu_notes_buf(u32 num_cpus) |
Hari Bathini | 961cf26 | 2019-09-11 20:16:36 +0530 | [diff] [blame] | 811 | { |
| 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 Bathini | 7f0ad11 | 2019-09-11 20:16:52 +0530 | [diff] [blame] | 829 | void fadump_free_cpu_notes_buf(void) |
Hari Bathini | 961cf26 | 2019-09-11 20:16:36 +0530 | [diff] [blame] | 830 | { |
| 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 Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 840 | /* |
| 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 Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 854 | static int __init fadump_build_cpu_notes(const struct rtas_fadump_mem_struct *fdm) |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 855 | { |
Hari Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 856 | struct rtas_fadump_reg_save_area_header *reg_header; |
| 857 | struct rtas_fadump_reg_entry *reg_entry; |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 858 | 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 Bathini | 408cddd | 2014-10-01 12:32:30 +0530 | [diff] [blame] | 868 | addr = be64_to_cpu(fdm->cpu_state_data.destination_address); |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 869 | vaddr = __va(addr); |
| 870 | |
| 871 | reg_header = vaddr; |
Hari Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 872 | if (be64_to_cpu(reg_header->magic_number) != |
| 873 | fadump_str_to_u64("REGSAVE")) { |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 874 | printk(KERN_ERR "Unable to read register save area.\n"); |
| 875 | return -ENOENT; |
| 876 | } |
| 877 | pr_debug("--------CPU State Data------------\n"); |
Hari Bathini | 408cddd | 2014-10-01 12:32:30 +0530 | [diff] [blame] | 878 | 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 Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 880 | |
Hari Bathini | 408cddd | 2014-10-01 12:32:30 +0530 | [diff] [blame] | 881 | vaddr += be32_to_cpu(reg_header->num_cpu_offset); |
| 882 | num_cpus = be32_to_cpu(*((__be32 *)(vaddr))); |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 883 | pr_debug("NumCpus : %u\n", num_cpus); |
| 884 | vaddr += sizeof(u32); |
Hari Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 885 | reg_entry = (struct rtas_fadump_reg_entry *)vaddr; |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 886 | |
Hari Bathini | 961cf26 | 2019-09-11 20:16:36 +0530 | [diff] [blame] | 887 | rc = fadump_setup_cpu_notes_buf(num_cpus); |
| 888 | if (rc != 0) |
| 889 | return rc; |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 890 | |
Hari Bathini | 961cf26 | 2019-09-11 20:16:36 +0530 | [diff] [blame] | 891 | note_buf = (u32 *)fw_dump.cpu_notes_buf_vaddr; |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 892 | |
| 893 | if (fw_dump.fadumphdr_addr) |
| 894 | fdh = __va(fw_dump.fadumphdr_addr); |
| 895 | |
| 896 | for (i = 0; i < num_cpus; i++) { |
Hari Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 897 | if (be64_to_cpu(reg_entry->reg_id) != fadump_str_to_u64("CPUSTRT")) { |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 898 | 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 Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 903 | cpu = be64_to_cpu(reg_entry->reg_value) & RTAS_FADUMP_CPU_ID_MASK; |
Rasmus Villemoes | a051216 | 2016-01-20 15:00:13 -0800 | [diff] [blame] | 904 | if (fdh && !cpumask_test_cpu(cpu, &fdh->online_mask)) { |
Hari Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 905 | RTAS_FADUMP_SKIP_TO_NEXT_CPU(reg_entry); |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 906 | 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, ®s); |
Hari Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 912 | RTAS_FADUMP_SKIP_TO_NEXT_CPU(reg_entry); |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 913 | } else { |
| 914 | reg_entry++; |
| 915 | reg_entry = fadump_read_registers(reg_entry, ®s); |
| 916 | note_buf = fadump_regs_to_elf_notes(note_buf, ®s); |
| 917 | } |
| 918 | } |
Hari Bathini | 22bd017 | 2017-05-08 15:56:24 -0700 | [diff] [blame] | 919 | final_note(note_buf); |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 920 | |
Rickard Strandqvist | b717d98 | 2014-05-23 00:03:16 +0200 | [diff] [blame] | 921 | if (fdh) { |
| 922 | pr_debug("Updating elfcore header (%llx) with cpu notes\n", |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 923 | fdh->elfcorehdr_addr); |
Rickard Strandqvist | b717d98 | 2014-05-23 00:03:16 +0200 | [diff] [blame] | 924 | fadump_update_elfcore_header((char *)__va(fdh->elfcorehdr_addr)); |
| 925 | } |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 926 | return 0; |
| 927 | |
| 928 | error_out: |
Hari Bathini | 961cf26 | 2019-09-11 20:16:36 +0530 | [diff] [blame] | 929 | fadump_free_cpu_notes_buf(); |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 930 | return rc; |
| 931 | |
| 932 | } |
| 933 | |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 934 | /* |
| 935 | * Validate and process the dump data stored by firmware before exporting |
| 936 | * it through '/proc/vmcore'. |
| 937 | */ |
Hari Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 938 | static int __init process_fadump(const struct rtas_fadump_mem_struct *fdm_active) |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 939 | { |
| 940 | struct fadump_crash_info_header *fdh; |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 941 | int rc = 0; |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 942 | |
| 943 | if (!fdm_active || !fw_dump.fadumphdr_addr) |
| 944 | return -EINVAL; |
| 945 | |
| 946 | /* Check if the dump data is valid. */ |
Hari Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 947 | if ((be16_to_cpu(fdm_active->header.dump_status_flag) == RTAS_FADUMP_ERROR_FLAG) || |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 948 | (fdm_active->cpu_state_data.error_flags != 0) || |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 949 | (fdm_active->rmr_region.error_flags != 0)) { |
| 950 | printk(KERN_ERR "Dump taken by platform is not valid\n"); |
| 951 | return -EINVAL; |
| 952 | } |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 953 | if ((fdm_active->rmr_region.bytes_dumped != |
| 954 | fdm_active->rmr_region.source_len) || |
| 955 | !fdm_active->cpu_state_data.bytes_dumped) { |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 956 | 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 Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 967 | rc = fadump_build_cpu_notes(fdm_active); |
| 968 | if (rc) |
| 969 | return rc; |
| 970 | |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 971 | /* |
| 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 Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 981 | static 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 | */ |
| 993 | static 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 | |
| 1016 | static inline int fadump_add_crash_memory(unsigned long long base, |
| 1017 | unsigned long long end) |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 1018 | { |
Hari Bathini | ced1bf5 | 2018-08-07 02:12:54 +0530 | [diff] [blame] | 1019 | u64 start, size; |
| 1020 | bool is_adjacent = false; |
| 1021 | |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 1022 | if (base == end) |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 1023 | return 0; |
| 1024 | |
Hari Bathini | ced1bf5 | 2018-08-07 02:12:54 +0530 | [diff] [blame] | 1025 | /* |
| 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 Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 1032 | |
Hari Bathini | ced1bf5 | 2018-08-07 02:12:54 +0530 | [diff] [blame] | 1033 | 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 Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 1049 | } |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 1050 | |
Hari Bathini | ced1bf5 | 2018-08-07 02:12:54 +0530 | [diff] [blame] | 1051 | crash_memory_ranges[crash_mem_ranges - 1].size = (end - start); |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 1052 | pr_debug("crash_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n", |
Hari Bathini | ced1bf5 | 2018-08-07 02:12:54 +0530 | [diff] [blame] | 1053 | (crash_mem_ranges - 1), start, end - 1, (end - start)); |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 1054 | return 0; |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 1057 | static int fadump_exclude_reserved_area(unsigned long long start, |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 1058 | unsigned long long end) |
| 1059 | { |
| 1060 | unsigned long long ra_start, ra_end; |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 1061 | int ret = 0; |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 1062 | |
| 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 Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 1068 | 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 Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 1073 | } else if (start < ra_start) { |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 1074 | ret = fadump_add_crash_memory(start, ra_start); |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 1075 | } else if (ra_end < end) { |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 1076 | ret = fadump_add_crash_memory(ra_end, end); |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 1077 | } |
| 1078 | } else |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 1079 | ret = fadump_add_crash_memory(start, end); |
| 1080 | |
| 1081 | return ret; |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | static 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 Axtens | d8bced2 | 2016-09-06 15:32:42 +1000 | [diff] [blame] | 1102 | #if defined(_CALL_ELF) |
| 1103 | elf->e_flags = _CALL_ELF; |
| 1104 | #else |
| 1105 | elf->e_flags = 0; |
| 1106 | #endif |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 1107 | 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 Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 1121 | static int fadump_setup_crash_memory_ranges(void) |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 1122 | { |
| 1123 | struct memblock_region *reg; |
| 1124 | unsigned long long start, end; |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 1125 | int ret; |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 1126 | |
| 1127 | pr_debug("Setup crash memory ranges.\n"); |
| 1128 | crash_mem_ranges = 0; |
Hari Bathini | ced1bf5 | 2018-08-07 02:12:54 +0530 | [diff] [blame] | 1129 | |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 1130 | /* |
| 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 Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 1137 | ret = fadump_add_crash_memory(RMA_START, fw_dump.boot_memory_size); |
| 1138 | if (ret) |
| 1139 | return ret; |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 1140 | |
| 1141 | for_each_memblock(memory, reg) { |
| 1142 | start = (unsigned long long)reg->base; |
| 1143 | end = start + (unsigned long long)reg->size; |
Hari Bathini | a77af552 | 2017-06-01 22:50:38 +0530 | [diff] [blame] | 1144 | |
| 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 Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 1157 | |
| 1158 | /* add this range excluding the reserved dump area. */ |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 1159 | ret = fadump_exclude_reserved_area(start, end); |
| 1160 | if (ret) |
| 1161 | return ret; |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 1162 | } |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 1163 | |
| 1164 | return 0; |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
Mahesh Salgaonkar | d34c5f2 | 2012-02-16 01:14:53 +0000 | [diff] [blame] | 1167 | /* |
| 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 | */ |
| 1172 | static inline unsigned long fadump_relocate(unsigned long paddr) |
| 1173 | { |
| 1174 | if (paddr > RMA_START && paddr < fw_dump.boot_memory_size) |
Hari Bathini | 408cddd | 2014-10-01 12:32:30 +0530 | [diff] [blame] | 1175 | return be64_to_cpu(fdm.rmr_region.destination_address) + paddr; |
Mahesh Salgaonkar | d34c5f2 | 2012-02-16 01:14:53 +0000 | [diff] [blame] | 1176 | else |
| 1177 | return paddr; |
| 1178 | } |
| 1179 | |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 1180 | static 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 Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 1190 | /* |
| 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 Salgaonkar | d34c5f2 | 2012-02-16 01:14:53 +0000 | [diff] [blame] | 1211 | /* 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 Pang | 5203f49 | 2017-07-12 14:33:17 -0700 | [diff] [blame] | 1221 | phdr->p_memsz = phdr->p_filesz = VMCOREINFO_NOTE_SIZE; |
Mahesh Salgaonkar | d34c5f2 | 2012-02-16 01:14:53 +0000 | [diff] [blame] | 1222 | |
| 1223 | /* Increment number of program headers. */ |
| 1224 | (elf->e_phnum)++; |
| 1225 | |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 1226 | /* 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 Bathini | 408cddd | 2014-10-01 12:32:30 +0530 | [diff] [blame] | 1248 | phdr->p_offset = be64_to_cpu(fdm.rmr_region.destination_address); |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 1249 | } |
| 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 | |
| 1263 | static 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 Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 1277 | /* We will set the crashing cpu id in crash_fadump() during crash. */ |
Hari Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 1278 | fdh->crashing_cpu = FADUMP_CPU_UNKNOWN; |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 1279 | |
| 1280 | return addr; |
| 1281 | } |
| 1282 | |
Michal Suchanek | 98b8cd7 | 2017-05-27 17:46:15 +0200 | [diff] [blame] | 1283 | static int register_fadump(void) |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1284 | { |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 1285 | unsigned long addr; |
| 1286 | void *vaddr; |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 1287 | int ret; |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 1288 | |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1289 | /* |
| 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 Suchanek | 98b8cd7 | 2017-05-27 17:46:15 +0200 | [diff] [blame] | 1294 | return -ENODEV; |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1295 | |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 1296 | ret = fadump_setup_crash_memory_ranges(); |
| 1297 | if (ret) |
| 1298 | return ret; |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 1299 | |
Hari Bathini | 408cddd | 2014-10-01 12:32:30 +0530 | [diff] [blame] | 1300 | addr = be64_to_cpu(fdm.rmr_region.destination_address) + be64_to_cpu(fdm.rmr_region.source_len); |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 1301 | /* 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 Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1308 | /* register the future kernel dump with firmware. */ |
Michal Suchanek | 98b8cd7 | 2017-05-27 17:46:15 +0200 | [diff] [blame] | 1309 | return register_fw_dump(&fdm); |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
Hari Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 1312 | static int fadump_unregister_dump(struct rtas_fadump_mem_struct *fdm) |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1313 | { |
| 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 Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 1323 | sizeof(struct rtas_fadump_mem_struct)); |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1324 | |
| 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 Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 1339 | static int fadump_invalidate_dump(const struct rtas_fadump_mem_struct *fdm) |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1340 | { |
| 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 Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 1350 | sizeof(struct rtas_fadump_mem_struct)); |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1351 | |
| 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 King | 4a03749 | 2016-06-27 12:07:41 +0100 | [diff] [blame] | 1358 | pr_err("Failed to invalidate firmware-assisted dump registration. Unexpected error (%d).\n", rc); |
Michael Ellerman | b5b1cfc | 2016-07-05 23:45:56 +1000 | [diff] [blame] | 1359 | return rc; |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1360 | } |
| 1361 | fw_dump.dump_active = 0; |
| 1362 | fdm_active = NULL; |
| 1363 | return 0; |
| 1364 | } |
| 1365 | |
| 1366 | void fadump_cleanup(void) |
| 1367 | { |
| 1368 | /* Invalidate the registration only if dump is active. */ |
| 1369 | if (fw_dump.dump_active) { |
Mahesh Salgaonkar | a4e92ce | 2018-08-20 13:47:17 +0530 | [diff] [blame] | 1370 | /* pass the same memory dump structure provided by platform */ |
| 1371 | fadump_invalidate_dump(fdm_active); |
Mahesh Salgaonkar | 722cde7 | 2018-04-27 11:53:18 +0530 | [diff] [blame] | 1372 | } else if (fw_dump.dump_registered) { |
| 1373 | /* Un-register Firmware-assisted dump if it was registered. */ |
| 1374 | fadump_unregister_dump(&fdm); |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 1375 | free_crash_memory_ranges(); |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1376 | } |
| 1377 | } |
| 1378 | |
Hari Bathini | 68fa647 | 2017-06-02 01:10:10 +0530 | [diff] [blame] | 1379 | static 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 | */ |
| 1401 | static 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 Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1422 | /* |
| 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 | */ |
| 1426 | static void fadump_release_memory(unsigned long begin, unsigned long end) |
| 1427 | { |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1428 | 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 Bathini | 68fa647 | 2017-06-02 01:10:10 +0530 | [diff] [blame] | 1433 | /* |
| 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 Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
| 1446 | static 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 Bathini | 408cddd | 2014-10-01 12:32:30 +0530 | [diff] [blame] | 1457 | destination_address = be64_to_cpu(fdm_active->cpu_state_data.destination_address); |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1458 | 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 Bathini | 961cf26 | 2019-09-11 20:16:36 +0530 | [diff] [blame] | 1476 | fadump_free_cpu_notes_buf(); |
| 1477 | |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1478 | /* Initialize the kernel dump memory structure for FAD registration. */ |
| 1479 | init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start); |
| 1480 | } |
| 1481 | |
| 1482 | static ssize_t fadump_release_memory_store(struct kobject *kobj, |
| 1483 | struct kobj_attribute *attr, |
| 1484 | const char *buf, size_t count) |
| 1485 | { |
Michal Suchanek | dcdc467 | 2017-06-26 16:06:01 +0200 | [diff] [blame] | 1486 | int input = -1; |
| 1487 | |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1488 | if (!fw_dump.dump_active) |
| 1489 | return -EPERM; |
| 1490 | |
Michal Suchanek | dcdc467 | 2017-06-26 16:06:01 +0200 | [diff] [blame] | 1491 | if (kstrtoint(buf, 0, &input)) |
| 1492 | return -EINVAL; |
| 1493 | |
| 1494 | if (input == 1) { |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1495 | /* |
| 1496 | * Take away the '/proc/vmcore'. We are releasing the dump |
| 1497 | * memory, hence it will not be valid anymore. |
| 1498 | */ |
Michael Ellerman | 2685f82 | 2016-09-30 10:51:46 +1000 | [diff] [blame] | 1499 | #ifdef CONFIG_PROC_VMCORE |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1500 | vmcore_cleanup(); |
Michael Ellerman | 2685f82 | 2016-09-30 10:51:46 +1000 | [diff] [blame] | 1501 | #endif |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1502 | fadump_invalidate_release_mem(); |
| 1503 | |
| 1504 | } else |
| 1505 | return -EINVAL; |
| 1506 | return count; |
| 1507 | } |
| 1508 | |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1509 | static 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 | |
| 1516 | static 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 | |
| 1523 | static 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 Suchanek | dcdc467 | 2017-06-26 16:06:01 +0200 | [diff] [blame] | 1528 | int input = -1; |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1529 | |
| 1530 | if (!fw_dump.fadump_enabled || fdm_active) |
| 1531 | return -EPERM; |
| 1532 | |
Michal Suchanek | dcdc467 | 2017-06-26 16:06:01 +0200 | [diff] [blame] | 1533 | if (kstrtoint(buf, 0, &input)) |
| 1534 | return -EINVAL; |
| 1535 | |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1536 | mutex_lock(&fadump_mutex); |
| 1537 | |
Michal Suchanek | dcdc467 | 2017-06-26 16:06:01 +0200 | [diff] [blame] | 1538 | switch (input) { |
| 1539 | case 0: |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1540 | if (fw_dump.dump_registered == 0) { |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1541 | goto unlock_out; |
| 1542 | } |
| 1543 | /* Un-register Firmware-assisted dump */ |
| 1544 | fadump_unregister_dump(&fdm); |
| 1545 | break; |
Michal Suchanek | dcdc467 | 2017-06-26 16:06:01 +0200 | [diff] [blame] | 1546 | case 1: |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1547 | if (fw_dump.dump_registered == 1) { |
Hari Bathini | 0823c68 | 2018-09-14 19:36:02 +0530 | [diff] [blame] | 1548 | /* Un-register Firmware-assisted dump */ |
| 1549 | fadump_unregister_dump(&fdm); |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1550 | } |
| 1551 | /* Register Firmware-assisted dump */ |
Michal Suchanek | 98b8cd7 | 2017-05-27 17:46:15 +0200 | [diff] [blame] | 1552 | ret = register_fadump(); |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1553 | break; |
| 1554 | default: |
| 1555 | ret = -EINVAL; |
| 1556 | break; |
| 1557 | } |
| 1558 | |
| 1559 | unlock_out: |
| 1560 | mutex_unlock(&fadump_mutex); |
| 1561 | return ret < 0 ? ret : count; |
| 1562 | } |
| 1563 | |
| 1564 | static int fadump_region_show(struct seq_file *m, void *private) |
| 1565 | { |
Hari Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame^] | 1566 | const struct rtas_fadump_mem_struct *fdm_ptr; |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1567 | |
| 1568 | if (!fw_dump.fadump_enabled) |
| 1569 | return 0; |
| 1570 | |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1571 | mutex_lock(&fadump_mutex); |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1572 | if (fdm_active) |
| 1573 | fdm_ptr = fdm_active; |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1574 | else { |
| 1575 | mutex_unlock(&fadump_mutex); |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1576 | fdm_ptr = &fdm; |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1577 | } |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1578 | |
| 1579 | seq_printf(m, |
| 1580 | "CPU : [%#016llx-%#016llx] %#llx bytes, " |
| 1581 | "Dumped: %#llx\n", |
Hari Bathini | 408cddd | 2014-10-01 12:32:30 +0530 | [diff] [blame] | 1582 | 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 Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1587 | seq_printf(m, |
| 1588 | "HPTE: [%#016llx-%#016llx] %#llx bytes, " |
| 1589 | "Dumped: %#llx\n", |
Hari Bathini | 408cddd | 2014-10-01 12:32:30 +0530 | [diff] [blame] | 1590 | 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 Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1595 | seq_printf(m, |
| 1596 | "DUMP: [%#016llx-%#016llx] %#llx bytes, " |
| 1597 | "Dumped: %#llx\n", |
Hari Bathini | 408cddd | 2014-10-01 12:32:30 +0530 | [diff] [blame] | 1598 | 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 Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1603 | |
| 1604 | if (!fdm_active || |
| 1605 | (fw_dump.reserve_dump_area_start == |
Hari Bathini | 408cddd | 2014-10-01 12:32:30 +0530 | [diff] [blame] | 1606 | be64_to_cpu(fdm_ptr->cpu_state_data.destination_address))) |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1607 | goto out; |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1608 | |
| 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 Bathini | 408cddd | 2014-10-01 12:32:30 +0530 | [diff] [blame] | 1614 | be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) - 1, |
| 1615 | be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) - |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1616 | fw_dump.reserve_dump_area_start, |
Hari Bathini | 408cddd | 2014-10-01 12:32:30 +0530 | [diff] [blame] | 1617 | be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) - |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1618 | fw_dump.reserve_dump_area_start); |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1619 | out: |
| 1620 | if (fdm_active) |
| 1621 | mutex_unlock(&fadump_mutex); |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1622 | return 0; |
| 1623 | } |
| 1624 | |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1625 | static struct kobj_attribute fadump_release_attr = __ATTR(fadump_release_mem, |
| 1626 | 0200, NULL, |
| 1627 | fadump_release_memory_store); |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1628 | static struct kobj_attribute fadump_attr = __ATTR(fadump_enabled, |
| 1629 | 0444, fadump_enabled_show, |
| 1630 | NULL); |
| 1631 | static struct kobj_attribute fadump_register_attr = __ATTR(fadump_registered, |
| 1632 | 0644, fadump_register_show, |
| 1633 | fadump_register_store); |
| 1634 | |
Yangtao Li | f6cee26 | 2018-11-05 10:01:19 -0500 | [diff] [blame] | 1635 | DEFINE_SHOW_ATTRIBUTE(fadump_region); |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1636 | |
| 1637 | static 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 Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1658 | |
| 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 Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1665 | return; |
| 1666 | } |
| 1667 | |
| 1668 | /* |
| 1669 | * Prepare for firmware-assisted dump. |
| 1670 | */ |
| 1671 | int __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 Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 1683 | /* |
| 1684 | * If dump data is available then see if it is valid and prepare for |
| 1685 | * saving it to the disk. |
| 1686 | */ |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1687 | 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 Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1695 | /* Initialize the kernel dump memory structure for FAD registration. */ |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 1696 | else if (fw_dump.reserve_dump_area_size) |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1697 | init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start); |
| 1698 | fadump_init_files(); |
| 1699 | |
| 1700 | return 1; |
| 1701 | } |
| 1702 | subsys_initcall(setup_fadump); |