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> |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 31 | #include <asm/fadump.h> |
Hari Bathini | ca986d7 | 2019-09-11 20:16:21 +0530 | [diff] [blame] | 32 | #include <asm/fadump-internal.h> |
Stephen Rothwell | cad3c83 | 2012-03-30 14:01:07 +0000 | [diff] [blame] | 33 | #include <asm/setup.h> |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 34 | |
| 35 | static struct fw_dump fw_dump; |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 36 | |
| 37 | static DEFINE_MUTEX(fadump_mutex); |
Hari Bathini | e4fc48f | 2019-09-11 20:25:05 +0530 | [diff] [blame] | 38 | struct fadump_mrange_info crash_mrange_info = { "crash", NULL, 0, 0, 0 }; |
Hari Bathini | dda9dbf | 2019-09-11 20:25:36 +0530 | [diff] [blame^] | 39 | struct fadump_mrange_info reserved_mrange_info = { "reserved", NULL, 0, 0, 0 }; |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 40 | |
Mahesh Salgaonkar | a4e92ce | 2018-08-20 13:47:17 +0530 | [diff] [blame] | 41 | #ifdef CONFIG_CMA |
Hari Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame] | 42 | static struct cma *fadump_cma; |
| 43 | |
Mahesh Salgaonkar | a4e92ce | 2018-08-20 13:47:17 +0530 | [diff] [blame] | 44 | /* |
| 45 | * fadump_cma_init() - Initialize CMA area from a fadump reserved memory |
| 46 | * |
| 47 | * This function initializes CMA area from fadump reserved memory. |
| 48 | * The total size of fadump reserved memory covers for boot memory size |
| 49 | * + cpu data size + hpte size and metadata. |
| 50 | * Initialize only the area equivalent to boot memory size for CMA use. |
| 51 | * The reamining portion of fadump reserved memory will be not given |
| 52 | * to CMA and pages for thoes will stay reserved. boot memory size is |
| 53 | * aligned per CMA requirement to satisy cma_init_reserved_mem() call. |
| 54 | * But for some reason even if it fails we still have the memory reservation |
| 55 | * with us and we can still continue doing fadump. |
| 56 | */ |
| 57 | int __init fadump_cma_init(void) |
| 58 | { |
| 59 | unsigned long long base, size; |
| 60 | int rc; |
| 61 | |
| 62 | if (!fw_dump.fadump_enabled) |
| 63 | return 0; |
| 64 | |
| 65 | /* |
| 66 | * Do not use CMA if user has provided fadump=nocma kernel parameter. |
| 67 | * Return 1 to continue with fadump old behaviour. |
| 68 | */ |
| 69 | if (fw_dump.nocma) |
| 70 | return 1; |
| 71 | |
| 72 | base = fw_dump.reserve_dump_area_start; |
| 73 | size = fw_dump.boot_memory_size; |
| 74 | |
| 75 | if (!size) |
| 76 | return 0; |
| 77 | |
| 78 | rc = cma_init_reserved_mem(base, size, 0, "fadump_cma", &fadump_cma); |
| 79 | if (rc) { |
| 80 | pr_err("Failed to init cma area for firmware-assisted dump,%d\n", rc); |
| 81 | /* |
| 82 | * Though the CMA init has failed we still have memory |
| 83 | * reservation with us. The reserved memory will be |
| 84 | * blocked from production system usage. Hence return 1, |
| 85 | * so that we can continue with fadump. |
| 86 | */ |
| 87 | return 1; |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * So we now have successfully initialized cma area for fadump. |
| 92 | */ |
| 93 | pr_info("Initialized 0x%lx bytes cma area at %ldMB from 0x%lx " |
| 94 | "bytes of memory reserved for firmware-assisted dump\n", |
| 95 | cma_get_size(fadump_cma), |
| 96 | (unsigned long)cma_get_base(fadump_cma) >> 20, |
| 97 | fw_dump.reserve_dump_area_size); |
| 98 | return 1; |
| 99 | } |
| 100 | #else |
| 101 | static int __init fadump_cma_init(void) { return 1; } |
| 102 | #endif /* CONFIG_CMA */ |
| 103 | |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 104 | /* Scan the Firmware Assisted dump configuration details. */ |
Hari Bathini | f351201 | 2019-09-11 20:19:44 +0530 | [diff] [blame] | 105 | int __init early_init_dt_scan_fw_dump(unsigned long node, const char *uname, |
| 106 | int depth, void *data) |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 107 | { |
Hari Bathini | 41df592 | 2019-09-11 20:20:26 +0530 | [diff] [blame] | 108 | if (depth != 1) |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 109 | return 0; |
| 110 | |
Hari Bathini | 41df592 | 2019-09-11 20:20:26 +0530 | [diff] [blame] | 111 | if (strcmp(uname, "rtas") == 0) { |
| 112 | rtas_fadump_dt_scan(&fw_dump, node); |
| 113 | return 1; |
| 114 | } |
| 115 | |
| 116 | if (strcmp(uname, "ibm,opal") == 0) { |
| 117 | opal_fadump_dt_scan(&fw_dump, node); |
| 118 | return 1; |
| 119 | } |
| 120 | |
| 121 | return 0; |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Hari Bathini | eae0dfc | 2017-06-01 22:51:26 +0530 | [diff] [blame] | 124 | /* |
| 125 | * If fadump is registered, check if the memory provided |
Mahesh Salgaonkar | 0db6896 | 2018-08-20 13:47:32 +0530 | [diff] [blame] | 126 | * falls within boot memory area and reserved memory area. |
Hari Bathini | eae0dfc | 2017-06-01 22:51:26 +0530 | [diff] [blame] | 127 | */ |
Mahesh Salgaonkar | 0db6896 | 2018-08-20 13:47:32 +0530 | [diff] [blame] | 128 | int is_fadump_memory_area(u64 addr, ulong size) |
Hari Bathini | eae0dfc | 2017-06-01 22:51:26 +0530 | [diff] [blame] | 129 | { |
Mahesh Salgaonkar | 0db6896 | 2018-08-20 13:47:32 +0530 | [diff] [blame] | 130 | u64 d_start = fw_dump.reserve_dump_area_start; |
| 131 | u64 d_end = d_start + fw_dump.reserve_dump_area_size; |
| 132 | |
Hari Bathini | eae0dfc | 2017-06-01 22:51:26 +0530 | [diff] [blame] | 133 | if (!fw_dump.dump_registered) |
| 134 | return 0; |
| 135 | |
Mahesh Salgaonkar | 0db6896 | 2018-08-20 13:47:32 +0530 | [diff] [blame] | 136 | if (((addr + size) > d_start) && (addr <= d_end)) |
| 137 | return 1; |
| 138 | |
Hari Bathini | eae0dfc | 2017-06-01 22:51:26 +0530 | [diff] [blame] | 139 | return (addr + size) > RMA_START && addr <= fw_dump.boot_memory_size; |
| 140 | } |
| 141 | |
Nicholas Piggin | 6fcd6ba | 2017-07-19 16:59:11 +1000 | [diff] [blame] | 142 | int should_fadump_crash(void) |
| 143 | { |
| 144 | if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr) |
| 145 | return 0; |
| 146 | return 1; |
| 147 | } |
| 148 | |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 149 | int is_fadump_active(void) |
| 150 | { |
| 151 | return fw_dump.dump_active; |
| 152 | } |
| 153 | |
Hari Bathini | a5a05b9 | 2017-06-01 22:52:10 +0530 | [diff] [blame] | 154 | /* |
Hari Bathini | 961cf26 | 2019-09-11 20:16:36 +0530 | [diff] [blame] | 155 | * Returns true, if there are no holes in memory area between d_start to d_end, |
| 156 | * false otherwise. |
Hari Bathini | a5a05b9 | 2017-06-01 22:52:10 +0530 | [diff] [blame] | 157 | */ |
Hari Bathini | 961cf26 | 2019-09-11 20:16:36 +0530 | [diff] [blame] | 158 | 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] | 159 | { |
| 160 | struct memblock_region *reg; |
Hari Bathini | 961cf26 | 2019-09-11 20:16:36 +0530 | [diff] [blame] | 161 | bool ret = false; |
| 162 | u64 start, end; |
Hari Bathini | a5a05b9 | 2017-06-01 22:52:10 +0530 | [diff] [blame] | 163 | |
| 164 | for_each_memblock(memory, reg) { |
Hari Bathini | 961cf26 | 2019-09-11 20:16:36 +0530 | [diff] [blame] | 165 | start = max_t(u64, d_start, reg->base); |
| 166 | end = min_t(u64, d_end, (reg->base + reg->size)); |
| 167 | if (d_start < end) { |
| 168 | /* Memory hole from d_start to start */ |
| 169 | if (start > d_start) |
Hari Bathini | a5a05b9 | 2017-06-01 22:52:10 +0530 | [diff] [blame] | 170 | break; |
| 171 | |
Hari Bathini | 961cf26 | 2019-09-11 20:16:36 +0530 | [diff] [blame] | 172 | if (end == d_end) { |
| 173 | ret = true; |
Hari Bathini | a5a05b9 | 2017-06-01 22:52:10 +0530 | [diff] [blame] | 174 | break; |
| 175 | } |
| 176 | |
Hari Bathini | 961cf26 | 2019-09-11 20:16:36 +0530 | [diff] [blame] | 177 | d_start = end + 1; |
Hari Bathini | a5a05b9 | 2017-06-01 22:52:10 +0530 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
| 181 | return ret; |
| 182 | } |
| 183 | |
Mahesh Salgaonkar | f86593b | 2018-08-20 13:47:24 +0530 | [diff] [blame] | 184 | /* |
Hari Bathini | 961cf26 | 2019-09-11 20:16:36 +0530 | [diff] [blame] | 185 | * Returns true, if there are no holes in boot memory area, |
| 186 | * false otherwise. |
| 187 | */ |
Hari Bathini | 7f0ad11 | 2019-09-11 20:16:52 +0530 | [diff] [blame] | 188 | bool is_fadump_boot_mem_contiguous(void) |
Hari Bathini | 961cf26 | 2019-09-11 20:16:36 +0530 | [diff] [blame] | 189 | { |
| 190 | return is_fadump_mem_area_contiguous(0, fw_dump.boot_memory_size); |
| 191 | } |
| 192 | |
| 193 | /* |
Mahesh Salgaonkar | f86593b | 2018-08-20 13:47:24 +0530 | [diff] [blame] | 194 | * Returns true, if there are no holes in reserved memory area, |
| 195 | * false otherwise. |
| 196 | */ |
Hari Bathini | 7f0ad11 | 2019-09-11 20:16:52 +0530 | [diff] [blame] | 197 | bool is_fadump_reserved_mem_contiguous(void) |
Mahesh Salgaonkar | f86593b | 2018-08-20 13:47:24 +0530 | [diff] [blame] | 198 | { |
Hari Bathini | 961cf26 | 2019-09-11 20:16:36 +0530 | [diff] [blame] | 199 | u64 d_start, d_end; |
Mahesh Salgaonkar | f86593b | 2018-08-20 13:47:24 +0530 | [diff] [blame] | 200 | |
Hari Bathini | 961cf26 | 2019-09-11 20:16:36 +0530 | [diff] [blame] | 201 | d_start = fw_dump.reserve_dump_area_start; |
| 202 | d_end = d_start + fw_dump.reserve_dump_area_size; |
| 203 | return is_fadump_mem_area_contiguous(d_start, d_end); |
Mahesh Salgaonkar | f86593b | 2018-08-20 13:47:24 +0530 | [diff] [blame] | 204 | } |
| 205 | |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 206 | /* Print firmware assisted dump configurations for debugging purpose. */ |
| 207 | static void fadump_show_config(void) |
| 208 | { |
| 209 | pr_debug("Support for firmware-assisted dump (fadump): %s\n", |
| 210 | (fw_dump.fadump_supported ? "present" : "no support")); |
| 211 | |
| 212 | if (!fw_dump.fadump_supported) |
| 213 | return; |
| 214 | |
| 215 | pr_debug("Fadump enabled : %s\n", |
| 216 | (fw_dump.fadump_enabled ? "yes" : "no")); |
| 217 | pr_debug("Dump Active : %s\n", |
| 218 | (fw_dump.dump_active ? "yes" : "no")); |
| 219 | pr_debug("Dump section sizes:\n"); |
| 220 | pr_debug(" CPU state data size: %lx\n", fw_dump.cpu_state_data_size); |
| 221 | pr_debug(" HPTE region size : %lx\n", fw_dump.hpte_region_size); |
| 222 | pr_debug("Boot memory size : %lx\n", fw_dump.boot_memory_size); |
| 223 | } |
| 224 | |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 225 | /** |
| 226 | * fadump_calculate_reserve_size(): reserve variable boot area 5% of System RAM |
| 227 | * |
| 228 | * Function to find the largest memory size we need to reserve during early |
| 229 | * boot process. This will be the size of the memory that is required for a |
| 230 | * kernel to boot successfully. |
| 231 | * |
| 232 | * This function has been taken from phyp-assisted dump feature implementation. |
| 233 | * |
| 234 | * returns larger of 256MB or 5% rounded down to multiples of 256MB. |
| 235 | * |
| 236 | * TODO: Come up with better approach to find out more accurate memory size |
| 237 | * that is required for a kernel to boot successfully. |
| 238 | * |
| 239 | */ |
| 240 | static inline unsigned long fadump_calculate_reserve_size(void) |
| 241 | { |
Hari Bathini | 11550dc | 2017-05-08 15:56:28 -0700 | [diff] [blame] | 242 | int ret; |
| 243 | unsigned long long base, size; |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 244 | |
Hari Bathini | 81d9eca | 2017-05-22 15:04:23 +0530 | [diff] [blame] | 245 | if (fw_dump.reserve_bootvar) |
| 246 | pr_warn("'fadump_reserve_mem=' parameter is deprecated in favor of 'crashkernel=' parameter.\n"); |
| 247 | |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 248 | /* |
Hari Bathini | 11550dc | 2017-05-08 15:56:28 -0700 | [diff] [blame] | 249 | * Check if the size is specified through crashkernel= cmdline |
Hari Bathini | e7467dc | 2017-05-22 15:04:47 +0530 | [diff] [blame] | 250 | * option. If yes, then use that but ignore base as fadump reserves |
| 251 | * memory at a predefined offset. |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 252 | */ |
Hari Bathini | 11550dc | 2017-05-08 15:56:28 -0700 | [diff] [blame] | 253 | ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(), |
| 254 | &size, &base); |
| 255 | if (ret == 0 && size > 0) { |
Hari Bathini | 48a316e | 2017-06-02 13:00:27 +0530 | [diff] [blame] | 256 | unsigned long max_size; |
| 257 | |
Hari Bathini | 81d9eca | 2017-05-22 15:04:23 +0530 | [diff] [blame] | 258 | if (fw_dump.reserve_bootvar) |
| 259 | pr_info("Using 'crashkernel=' parameter for memory reservation.\n"); |
| 260 | |
Hari Bathini | 11550dc | 2017-05-08 15:56:28 -0700 | [diff] [blame] | 261 | fw_dump.reserve_bootvar = (unsigned long)size; |
Hari Bathini | 48a316e | 2017-06-02 13:00:27 +0530 | [diff] [blame] | 262 | |
| 263 | /* |
| 264 | * Adjust if the boot memory size specified is above |
| 265 | * the upper limit. |
| 266 | */ |
| 267 | max_size = memblock_phys_mem_size() / MAX_BOOT_MEM_RATIO; |
| 268 | if (fw_dump.reserve_bootvar > max_size) { |
| 269 | fw_dump.reserve_bootvar = max_size; |
| 270 | pr_info("Adjusted boot memory size to %luMB\n", |
| 271 | (fw_dump.reserve_bootvar >> 20)); |
| 272 | } |
| 273 | |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 274 | return fw_dump.reserve_bootvar; |
Hari Bathini | 81d9eca | 2017-05-22 15:04:23 +0530 | [diff] [blame] | 275 | } else if (fw_dump.reserve_bootvar) { |
| 276 | /* |
| 277 | * 'fadump_reserve_mem=' is being used to reserve memory |
| 278 | * for firmware-assisted dump. |
| 279 | */ |
| 280 | return fw_dump.reserve_bootvar; |
Hari Bathini | 11550dc | 2017-05-08 15:56:28 -0700 | [diff] [blame] | 281 | } |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 282 | |
| 283 | /* divide by 20 to get 5% of value */ |
Hari Bathini | 48a316e | 2017-06-02 13:00:27 +0530 | [diff] [blame] | 284 | size = memblock_phys_mem_size() / 20; |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 285 | |
| 286 | /* round it down in multiples of 256 */ |
| 287 | size = size & ~0x0FFFFFFFUL; |
| 288 | |
| 289 | /* Truncate to memory_limit. We don't want to over reserve the memory.*/ |
| 290 | if (memory_limit && size > memory_limit) |
| 291 | size = memory_limit; |
| 292 | |
| 293 | return (size > MIN_BOOT_MEM ? size : MIN_BOOT_MEM); |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | * Calculate the total memory size required to be reserved for |
| 298 | * firmware-assisted dump registration. |
| 299 | */ |
| 300 | static unsigned long get_fadump_area_size(void) |
| 301 | { |
| 302 | unsigned long size = 0; |
| 303 | |
| 304 | size += fw_dump.cpu_state_data_size; |
| 305 | size += fw_dump.hpte_region_size; |
| 306 | size += fw_dump.boot_memory_size; |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 307 | size += sizeof(struct fadump_crash_info_header); |
| 308 | size += sizeof(struct elfhdr); /* ELF core header.*/ |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 309 | size += sizeof(struct elf_phdr); /* place holder for cpu notes */ |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 310 | /* Program headers for crash memory regions. */ |
| 311 | size += sizeof(struct elf_phdr) * (memblock_num_regions(memory) + 2); |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 312 | |
| 313 | size = PAGE_ALIGN(size); |
Hari Bathini | 742a265 | 2019-09-11 20:20:57 +0530 | [diff] [blame] | 314 | |
| 315 | /* This is to hold kernel metadata on platforms that support it */ |
| 316 | size += (fw_dump.ops->fadump_get_metadata_size ? |
| 317 | fw_dump.ops->fadump_get_metadata_size() : 0); |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 318 | return size; |
| 319 | } |
| 320 | |
Mahesh Salgaonkar | b71a693 | 2018-04-10 19:11:16 +0530 | [diff] [blame] | 321 | static void __init fadump_reserve_crash_area(unsigned long base, |
| 322 | unsigned long size) |
| 323 | { |
| 324 | struct memblock_region *reg; |
| 325 | unsigned long mstart, mend, msize; |
| 326 | |
| 327 | for_each_memblock(memory, reg) { |
| 328 | mstart = max_t(unsigned long, base, reg->base); |
| 329 | mend = reg->base + reg->size; |
| 330 | mend = min(base + size, mend); |
| 331 | |
| 332 | if (mstart < mend) { |
| 333 | msize = mend - mstart; |
| 334 | memblock_reserve(mstart, msize); |
| 335 | pr_info("Reserved %ldMB of memory at %#016lx for saving crash dump\n", |
| 336 | (msize >> 20), mstart); |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 341 | int __init fadump_reserve_mem(void) |
| 342 | { |
Hari Bathini | 579ca1a | 2019-09-11 20:24:28 +0530 | [diff] [blame] | 343 | bool is_memblock_bottom_up = memblock_bottom_up(); |
| 344 | u64 base, size, mem_boundary, align = PAGE_SIZE; |
Hari Bathini | 6abec12 | 2019-09-11 20:20:41 +0530 | [diff] [blame] | 345 | int ret = 1; |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 346 | |
| 347 | if (!fw_dump.fadump_enabled) |
| 348 | return 0; |
| 349 | |
| 350 | if (!fw_dump.fadump_supported) { |
Hari Bathini | 6abec12 | 2019-09-11 20:20:41 +0530 | [diff] [blame] | 351 | pr_info("Firmware-Assisted Dump is not supported on this hardware\n"); |
| 352 | goto error_out; |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 353 | } |
Hari Bathini | 742a265 | 2019-09-11 20:20:57 +0530 | [diff] [blame] | 354 | |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 355 | /* |
| 356 | * Initialize boot memory size |
| 357 | * If dump is active then we have already calculated the size during |
| 358 | * first kernel. |
| 359 | */ |
Hari Bathini | f351201 | 2019-09-11 20:19:44 +0530 | [diff] [blame] | 360 | if (!fw_dump.dump_active) { |
Hari Bathini | 6abec12 | 2019-09-11 20:20:41 +0530 | [diff] [blame] | 361 | fw_dump.boot_memory_size = |
| 362 | PAGE_ALIGN(fadump_calculate_reserve_size()); |
Mahesh Salgaonkar | a4e92ce | 2018-08-20 13:47:17 +0530 | [diff] [blame] | 363 | #ifdef CONFIG_CMA |
Hari Bathini | 579ca1a | 2019-09-11 20:24:28 +0530 | [diff] [blame] | 364 | if (!fw_dump.nocma) { |
| 365 | align = FADUMP_CMA_ALIGNMENT; |
Mahesh Salgaonkar | a4e92ce | 2018-08-20 13:47:17 +0530 | [diff] [blame] | 366 | fw_dump.boot_memory_size = |
Hari Bathini | 579ca1a | 2019-09-11 20:24:28 +0530 | [diff] [blame] | 367 | ALIGN(fw_dump.boot_memory_size, align); |
| 368 | } |
Mahesh Salgaonkar | a4e92ce | 2018-08-20 13:47:17 +0530 | [diff] [blame] | 369 | #endif |
| 370 | } |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 371 | |
| 372 | /* |
| 373 | * Calculate the memory boundary. |
| 374 | * If memory_limit is less than actual memory boundary then reserve |
| 375 | * the memory for fadump beyond the memory_limit and adjust the |
| 376 | * memory_limit accordingly, so that the running kernel can run with |
| 377 | * specified memory_limit. |
| 378 | */ |
| 379 | if (memory_limit && memory_limit < memblock_end_of_DRAM()) { |
| 380 | size = get_fadump_area_size(); |
| 381 | if ((memory_limit + size) < memblock_end_of_DRAM()) |
| 382 | memory_limit += size; |
| 383 | else |
| 384 | memory_limit = memblock_end_of_DRAM(); |
| 385 | printk(KERN_INFO "Adjusted memory_limit for firmware-assisted" |
Suzuki Poulose | a84fcd4 | 2012-08-21 01:42:33 +0000 | [diff] [blame] | 386 | " dump, now %#016llx\n", memory_limit); |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 387 | } |
| 388 | if (memory_limit) |
Hari Bathini | 6abec12 | 2019-09-11 20:20:41 +0530 | [diff] [blame] | 389 | mem_boundary = memory_limit; |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 390 | else |
Hari Bathini | 6abec12 | 2019-09-11 20:20:41 +0530 | [diff] [blame] | 391 | mem_boundary = memblock_end_of_DRAM(); |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 392 | |
Hari Bathini | 6abec12 | 2019-09-11 20:20:41 +0530 | [diff] [blame] | 393 | base = fw_dump.boot_memory_size; |
Hari Bathini | 8255da9 | 2019-09-11 20:19:28 +0530 | [diff] [blame] | 394 | size = get_fadump_area_size(); |
| 395 | fw_dump.reserve_dump_area_size = size; |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 396 | if (fw_dump.dump_active) { |
Mahesh Salgaonkar | b71a693 | 2018-04-10 19:11:16 +0530 | [diff] [blame] | 397 | pr_info("Firmware-assisted dump is active.\n"); |
| 398 | |
Hari Bathini | 8597538 | 2018-04-10 19:11:31 +0530 | [diff] [blame] | 399 | #ifdef CONFIG_HUGETLB_PAGE |
| 400 | /* |
| 401 | * FADump capture kernel doesn't care much about hugepages. |
| 402 | * In fact, handling hugepages in capture kernel is asking for |
| 403 | * trouble. So, disable HugeTLB support when fadump is active. |
| 404 | */ |
| 405 | hugetlb_disabled = true; |
| 406 | #endif |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 407 | /* |
| 408 | * If last boot has crashed then reserve all the memory |
| 409 | * above boot_memory_size so that we don't touch it until |
| 410 | * dump is written to disk by userspace tool. This memory |
| 411 | * will be released for general use once the dump is saved. |
| 412 | */ |
Hari Bathini | 6abec12 | 2019-09-11 20:20:41 +0530 | [diff] [blame] | 413 | size = mem_boundary - base; |
Mahesh Salgaonkar | b71a693 | 2018-04-10 19:11:16 +0530 | [diff] [blame] | 414 | fadump_reserve_crash_area(base, size); |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 415 | |
Hari Bathini | f351201 | 2019-09-11 20:19:44 +0530 | [diff] [blame] | 416 | pr_debug("fadumphdr_addr = %#016lx\n", fw_dump.fadumphdr_addr); |
| 417 | pr_debug("Reserve dump area start address: 0x%lx\n", |
| 418 | fw_dump.reserve_dump_area_start); |
Hari Bathini | 8255da9 | 2019-09-11 20:19:28 +0530 | [diff] [blame] | 419 | } else { |
| 420 | /* |
Hari Bathini | f6e6bed | 2017-03-17 02:35:26 +0530 | [diff] [blame] | 421 | * Reserve memory at an offset closer to bottom of the RAM to |
Hari Bathini | 579ca1a | 2019-09-11 20:24:28 +0530 | [diff] [blame] | 422 | * minimize the impact of memory hot-remove operation. |
Hari Bathini | f6e6bed | 2017-03-17 02:35:26 +0530 | [diff] [blame] | 423 | */ |
Hari Bathini | 579ca1a | 2019-09-11 20:24:28 +0530 | [diff] [blame] | 424 | memblock_set_bottom_up(true); |
| 425 | base = memblock_find_in_range(base, mem_boundary, size, align); |
Hari Bathini | 6abec12 | 2019-09-11 20:20:41 +0530 | [diff] [blame] | 426 | |
Hari Bathini | 579ca1a | 2019-09-11 20:24:28 +0530 | [diff] [blame] | 427 | /* Restore the previous allocation mode */ |
| 428 | memblock_set_bottom_up(is_memblock_bottom_up); |
Hari Bathini | f6e6bed | 2017-03-17 02:35:26 +0530 | [diff] [blame] | 429 | |
Hari Bathini | 579ca1a | 2019-09-11 20:24:28 +0530 | [diff] [blame] | 430 | if (!base) { |
Hari Bathini | 742a265 | 2019-09-11 20:20:57 +0530 | [diff] [blame] | 431 | pr_err("Failed to find memory chunk for reservation!\n"); |
| 432 | goto error_out; |
| 433 | } |
| 434 | fw_dump.reserve_dump_area_start = base; |
| 435 | |
| 436 | /* |
| 437 | * Calculate the kernel metadata address and register it with |
| 438 | * f/w if the platform supports. |
| 439 | */ |
| 440 | if (fw_dump.ops->fadump_setup_metadata && |
| 441 | (fw_dump.ops->fadump_setup_metadata(&fw_dump) < 0)) |
| 442 | goto error_out; |
| 443 | |
| 444 | if (memblock_reserve(base, size)) { |
Hari Bathini | 6abec12 | 2019-09-11 20:20:41 +0530 | [diff] [blame] | 445 | pr_err("Failed to reserve memory!\n"); |
| 446 | goto error_out; |
| 447 | } |
| 448 | |
| 449 | pr_info("Reserved %lldMB of memory at %#016llx (System RAM: %lldMB)\n", |
| 450 | (size >> 20), base, (memblock_phys_mem_size() >> 20)); |
Hari Bathini | f6e6bed | 2017-03-17 02:35:26 +0530 | [diff] [blame] | 451 | |
Hari Bathini | 6abec12 | 2019-09-11 20:20:41 +0530 | [diff] [blame] | 452 | ret = fadump_cma_init(); |
Mahesh Salgaonkar | a4e92ce | 2018-08-20 13:47:17 +0530 | [diff] [blame] | 453 | } |
Hari Bathini | 6abec12 | 2019-09-11 20:20:41 +0530 | [diff] [blame] | 454 | |
| 455 | return ret; |
| 456 | error_out: |
| 457 | fw_dump.fadump_enabled = 0; |
| 458 | return 0; |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Srikar Dronamraju | 1e76609c | 2016-10-07 16:59:21 -0700 | [diff] [blame] | 461 | unsigned long __init arch_reserved_kernel_pages(void) |
| 462 | { |
| 463 | return memblock_reserved_size() / PAGE_SIZE; |
| 464 | } |
| 465 | |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 466 | /* Look for fadump= cmdline option. */ |
| 467 | static int __init early_fadump_param(char *p) |
| 468 | { |
| 469 | if (!p) |
| 470 | return 1; |
| 471 | |
| 472 | if (strncmp(p, "on", 2) == 0) |
| 473 | fw_dump.fadump_enabled = 1; |
| 474 | else if (strncmp(p, "off", 3) == 0) |
| 475 | fw_dump.fadump_enabled = 0; |
Mahesh Salgaonkar | a4e92ce | 2018-08-20 13:47:17 +0530 | [diff] [blame] | 476 | else if (strncmp(p, "nocma", 5) == 0) { |
| 477 | fw_dump.fadump_enabled = 1; |
| 478 | fw_dump.nocma = 1; |
| 479 | } |
Mahesh Salgaonkar | eb39c88 | 2012-02-16 01:14:22 +0000 | [diff] [blame] | 480 | |
| 481 | return 0; |
| 482 | } |
| 483 | early_param("fadump", early_fadump_param); |
| 484 | |
Hari Bathini | 81d9eca | 2017-05-22 15:04:23 +0530 | [diff] [blame] | 485 | /* |
| 486 | * Look for fadump_reserve_mem= cmdline option |
| 487 | * TODO: Remove references to 'fadump_reserve_mem=' parameter, |
| 488 | * the sooner 'crashkernel=' parameter is accustomed to. |
| 489 | */ |
| 490 | static int __init early_fadump_reserve_mem(char *p) |
| 491 | { |
| 492 | if (p) |
| 493 | fw_dump.reserve_bootvar = memparse(p, &p); |
| 494 | return 0; |
| 495 | } |
| 496 | early_param("fadump_reserve_mem", early_fadump_reserve_mem); |
| 497 | |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 498 | void crash_fadump(struct pt_regs *regs, const char *str) |
| 499 | { |
| 500 | struct fadump_crash_info_header *fdh = NULL; |
Mahesh Salgaonkar | f2a5e8f | 2016-10-24 23:51:51 +0530 | [diff] [blame] | 501 | int old_cpu, this_cpu; |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 502 | |
Nicholas Piggin | 6fcd6ba | 2017-07-19 16:59:11 +1000 | [diff] [blame] | 503 | if (!should_fadump_crash()) |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 504 | return; |
| 505 | |
Mahesh Salgaonkar | f2a5e8f | 2016-10-24 23:51:51 +0530 | [diff] [blame] | 506 | /* |
| 507 | * old_cpu == -1 means this is the first CPU which has come here, |
| 508 | * go ahead and trigger fadump. |
| 509 | * |
| 510 | * old_cpu != -1 means some other CPU has already on it's way |
| 511 | * to trigger fadump, just keep looping here. |
| 512 | */ |
| 513 | this_cpu = smp_processor_id(); |
| 514 | old_cpu = cmpxchg(&crashing_cpu, -1, this_cpu); |
| 515 | |
| 516 | if (old_cpu != -1) { |
| 517 | /* |
| 518 | * We can't loop here indefinitely. Wait as long as fadump |
| 519 | * is in force. If we race with fadump un-registration this |
| 520 | * loop will break and then we go down to normal panic path |
| 521 | * and reboot. If fadump is in force the first crashing |
| 522 | * cpu will definitely trigger fadump. |
| 523 | */ |
| 524 | while (fw_dump.dump_registered) |
| 525 | cpu_relax(); |
| 526 | return; |
| 527 | } |
| 528 | |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 529 | fdh = __va(fw_dump.fadumphdr_addr); |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 530 | fdh->crashing_cpu = crashing_cpu; |
| 531 | crash_save_vmcoreinfo(); |
| 532 | |
| 533 | if (regs) |
| 534 | fdh->regs = *regs; |
| 535 | else |
| 536 | ppc_save_regs(&fdh->regs); |
| 537 | |
Rasmus Villemoes | a051216 | 2016-01-20 15:00:13 -0800 | [diff] [blame] | 538 | fdh->online_mask = *cpu_online_mask; |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 539 | |
Hari Bathini | 41a65d1 | 2019-09-11 20:18:57 +0530 | [diff] [blame] | 540 | fw_dump.ops->fadump_trigger(fdh, str); |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Hari Bathini | 7f0ad11 | 2019-09-11 20:16:52 +0530 | [diff] [blame] | 543 | u32 *fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs) |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 544 | { |
| 545 | struct elf_prstatus prstatus; |
| 546 | |
| 547 | memset(&prstatus, 0, sizeof(prstatus)); |
| 548 | /* |
| 549 | * FIXME: How do i get PID? Do I really need it? |
| 550 | * prstatus.pr_pid = ???? |
| 551 | */ |
| 552 | elf_core_copy_kernel_regs(&prstatus.pr_reg, regs); |
Hari Bathini | 22bd017 | 2017-05-08 15:56:24 -0700 | [diff] [blame] | 553 | buf = append_elf_note(buf, CRASH_CORE_NOTE_NAME, NT_PRSTATUS, |
| 554 | &prstatus, sizeof(prstatus)); |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 555 | return buf; |
| 556 | } |
| 557 | |
Hari Bathini | 7f0ad11 | 2019-09-11 20:16:52 +0530 | [diff] [blame] | 558 | void fadump_update_elfcore_header(char *bufp) |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 559 | { |
| 560 | struct elfhdr *elf; |
| 561 | struct elf_phdr *phdr; |
| 562 | |
| 563 | elf = (struct elfhdr *)bufp; |
| 564 | bufp += sizeof(struct elfhdr); |
| 565 | |
| 566 | /* First note is a place holder for cpu notes info. */ |
| 567 | phdr = (struct elf_phdr *)bufp; |
| 568 | |
| 569 | if (phdr->p_type == PT_NOTE) { |
Hari Bathini | 961cf26 | 2019-09-11 20:16:36 +0530 | [diff] [blame] | 570 | phdr->p_paddr = __pa(fw_dump.cpu_notes_buf_vaddr); |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 571 | phdr->p_offset = phdr->p_paddr; |
| 572 | phdr->p_filesz = fw_dump.cpu_notes_buf_size; |
| 573 | phdr->p_memsz = fw_dump.cpu_notes_buf_size; |
| 574 | } |
| 575 | return; |
| 576 | } |
| 577 | |
Hari Bathini | 961cf26 | 2019-09-11 20:16:36 +0530 | [diff] [blame] | 578 | static void *fadump_alloc_buffer(unsigned long size) |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 579 | { |
Hari Bathini | 72aa651 | 2019-09-11 20:17:56 +0530 | [diff] [blame] | 580 | unsigned long count, i; |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 581 | struct page *page; |
Hari Bathini | 72aa651 | 2019-09-11 20:17:56 +0530 | [diff] [blame] | 582 | void *vaddr; |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 583 | |
Hari Bathini | 72aa651 | 2019-09-11 20:17:56 +0530 | [diff] [blame] | 584 | vaddr = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO); |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 585 | if (!vaddr) |
| 586 | return NULL; |
| 587 | |
Hari Bathini | 72aa651 | 2019-09-11 20:17:56 +0530 | [diff] [blame] | 588 | count = PAGE_ALIGN(size) / PAGE_SIZE; |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 589 | page = virt_to_page(vaddr); |
| 590 | for (i = 0; i < count; i++) |
Hari Bathini | 72aa651 | 2019-09-11 20:17:56 +0530 | [diff] [blame] | 591 | mark_page_reserved(page + i); |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 592 | return vaddr; |
| 593 | } |
| 594 | |
Hari Bathini | 961cf26 | 2019-09-11 20:16:36 +0530 | [diff] [blame] | 595 | static void fadump_free_buffer(unsigned long vaddr, unsigned long size) |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 596 | { |
Hari Bathini | 72aa651 | 2019-09-11 20:17:56 +0530 | [diff] [blame] | 597 | free_reserved_area((void *)vaddr, (void *)(vaddr + size), -1, NULL); |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Hari Bathini | 7f0ad11 | 2019-09-11 20:16:52 +0530 | [diff] [blame] | 600 | s32 fadump_setup_cpu_notes_buf(u32 num_cpus) |
Hari Bathini | 961cf26 | 2019-09-11 20:16:36 +0530 | [diff] [blame] | 601 | { |
| 602 | /* Allocate buffer to hold cpu crash notes. */ |
| 603 | fw_dump.cpu_notes_buf_size = num_cpus * sizeof(note_buf_t); |
| 604 | fw_dump.cpu_notes_buf_size = PAGE_ALIGN(fw_dump.cpu_notes_buf_size); |
| 605 | fw_dump.cpu_notes_buf_vaddr = |
| 606 | (unsigned long)fadump_alloc_buffer(fw_dump.cpu_notes_buf_size); |
| 607 | if (!fw_dump.cpu_notes_buf_vaddr) { |
| 608 | pr_err("Failed to allocate %ld bytes for CPU notes buffer\n", |
| 609 | fw_dump.cpu_notes_buf_size); |
| 610 | return -ENOMEM; |
| 611 | } |
| 612 | |
| 613 | pr_debug("Allocated buffer for cpu notes of size %ld at 0x%lx\n", |
| 614 | fw_dump.cpu_notes_buf_size, |
| 615 | fw_dump.cpu_notes_buf_vaddr); |
| 616 | return 0; |
| 617 | } |
| 618 | |
Hari Bathini | 7f0ad11 | 2019-09-11 20:16:52 +0530 | [diff] [blame] | 619 | void fadump_free_cpu_notes_buf(void) |
Hari Bathini | 961cf26 | 2019-09-11 20:16:36 +0530 | [diff] [blame] | 620 | { |
| 621 | if (!fw_dump.cpu_notes_buf_vaddr) |
| 622 | return; |
| 623 | |
| 624 | fadump_free_buffer(fw_dump.cpu_notes_buf_vaddr, |
| 625 | fw_dump.cpu_notes_buf_size); |
| 626 | fw_dump.cpu_notes_buf_vaddr = 0; |
| 627 | fw_dump.cpu_notes_buf_size = 0; |
| 628 | } |
| 629 | |
Hari Bathini | e4fc48f | 2019-09-11 20:25:05 +0530 | [diff] [blame] | 630 | static void fadump_free_mem_ranges(struct fadump_mrange_info *mrange_info) |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 631 | { |
Hari Bathini | e4fc48f | 2019-09-11 20:25:05 +0530 | [diff] [blame] | 632 | kfree(mrange_info->mem_ranges); |
| 633 | mrange_info->mem_ranges = NULL; |
| 634 | mrange_info->mem_ranges_sz = 0; |
| 635 | mrange_info->max_mem_ranges = 0; |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | /* |
Hari Bathini | e4fc48f | 2019-09-11 20:25:05 +0530 | [diff] [blame] | 639 | * Allocate or reallocate mem_ranges array in incremental units |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 640 | * of PAGE_SIZE. |
| 641 | */ |
Hari Bathini | e4fc48f | 2019-09-11 20:25:05 +0530 | [diff] [blame] | 642 | static int fadump_alloc_mem_ranges(struct fadump_mrange_info *mrange_info) |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 643 | { |
Hari Bathini | e4fc48f | 2019-09-11 20:25:05 +0530 | [diff] [blame] | 644 | struct fadump_memory_range *new_array; |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 645 | u64 new_size; |
| 646 | |
Hari Bathini | e4fc48f | 2019-09-11 20:25:05 +0530 | [diff] [blame] | 647 | new_size = mrange_info->mem_ranges_sz + PAGE_SIZE; |
| 648 | pr_debug("Allocating %llu bytes of memory for %s memory ranges\n", |
| 649 | new_size, mrange_info->name); |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 650 | |
Hari Bathini | e4fc48f | 2019-09-11 20:25:05 +0530 | [diff] [blame] | 651 | new_array = krealloc(mrange_info->mem_ranges, new_size, GFP_KERNEL); |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 652 | if (new_array == NULL) { |
Hari Bathini | e4fc48f | 2019-09-11 20:25:05 +0530 | [diff] [blame] | 653 | pr_err("Insufficient memory for setting up %s memory ranges\n", |
| 654 | mrange_info->name); |
| 655 | fadump_free_mem_ranges(mrange_info); |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 656 | return -ENOMEM; |
| 657 | } |
| 658 | |
Hari Bathini | e4fc48f | 2019-09-11 20:25:05 +0530 | [diff] [blame] | 659 | mrange_info->mem_ranges = new_array; |
| 660 | mrange_info->mem_ranges_sz = new_size; |
| 661 | mrange_info->max_mem_ranges = (new_size / |
| 662 | sizeof(struct fadump_memory_range)); |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 663 | return 0; |
| 664 | } |
| 665 | |
Hari Bathini | e4fc48f | 2019-09-11 20:25:05 +0530 | [diff] [blame] | 666 | static inline int fadump_add_mem_range(struct fadump_mrange_info *mrange_info, |
| 667 | u64 base, u64 end) |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 668 | { |
Hari Bathini | e4fc48f | 2019-09-11 20:25:05 +0530 | [diff] [blame] | 669 | struct fadump_memory_range *mem_ranges = mrange_info->mem_ranges; |
Hari Bathini | ced1bf5 | 2018-08-07 02:12:54 +0530 | [diff] [blame] | 670 | bool is_adjacent = false; |
Hari Bathini | e4fc48f | 2019-09-11 20:25:05 +0530 | [diff] [blame] | 671 | u64 start, size; |
Hari Bathini | ced1bf5 | 2018-08-07 02:12:54 +0530 | [diff] [blame] | 672 | |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 673 | if (base == end) |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 674 | return 0; |
| 675 | |
Hari Bathini | ced1bf5 | 2018-08-07 02:12:54 +0530 | [diff] [blame] | 676 | /* |
| 677 | * Fold adjacent memory ranges to bring down the memory ranges/ |
| 678 | * PT_LOAD segments count. |
| 679 | */ |
Hari Bathini | e4fc48f | 2019-09-11 20:25:05 +0530 | [diff] [blame] | 680 | if (mrange_info->mem_range_cnt) { |
| 681 | start = mem_ranges[mrange_info->mem_range_cnt - 1].base; |
| 682 | size = mem_ranges[mrange_info->mem_range_cnt - 1].size; |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 683 | |
Hari Bathini | ced1bf5 | 2018-08-07 02:12:54 +0530 | [diff] [blame] | 684 | if ((start + size) == base) |
| 685 | is_adjacent = true; |
| 686 | } |
| 687 | if (!is_adjacent) { |
| 688 | /* resize the array on reaching the limit */ |
Hari Bathini | e4fc48f | 2019-09-11 20:25:05 +0530 | [diff] [blame] | 689 | if (mrange_info->mem_range_cnt == mrange_info->max_mem_ranges) { |
Hari Bathini | ced1bf5 | 2018-08-07 02:12:54 +0530 | [diff] [blame] | 690 | int ret; |
| 691 | |
Hari Bathini | e4fc48f | 2019-09-11 20:25:05 +0530 | [diff] [blame] | 692 | ret = fadump_alloc_mem_ranges(mrange_info); |
Hari Bathini | ced1bf5 | 2018-08-07 02:12:54 +0530 | [diff] [blame] | 693 | if (ret) |
| 694 | return ret; |
Hari Bathini | e4fc48f | 2019-09-11 20:25:05 +0530 | [diff] [blame] | 695 | |
| 696 | /* Update to the new resized array */ |
| 697 | mem_ranges = mrange_info->mem_ranges; |
Hari Bathini | ced1bf5 | 2018-08-07 02:12:54 +0530 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | start = base; |
Hari Bathini | e4fc48f | 2019-09-11 20:25:05 +0530 | [diff] [blame] | 701 | mem_ranges[mrange_info->mem_range_cnt].base = start; |
| 702 | mrange_info->mem_range_cnt++; |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 703 | } |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 704 | |
Hari Bathini | e4fc48f | 2019-09-11 20:25:05 +0530 | [diff] [blame] | 705 | mem_ranges[mrange_info->mem_range_cnt - 1].size = (end - start); |
| 706 | pr_debug("%s_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n", |
| 707 | mrange_info->name, (mrange_info->mem_range_cnt - 1), |
| 708 | start, end - 1, (end - start)); |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 709 | return 0; |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 710 | } |
| 711 | |
Hari Bathini | e4fc48f | 2019-09-11 20:25:05 +0530 | [diff] [blame] | 712 | static int fadump_exclude_reserved_area(u64 start, u64 end) |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 713 | { |
Hari Bathini | e4fc48f | 2019-09-11 20:25:05 +0530 | [diff] [blame] | 714 | u64 ra_start, ra_end; |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 715 | int ret = 0; |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 716 | |
| 717 | ra_start = fw_dump.reserve_dump_area_start; |
| 718 | ra_end = ra_start + fw_dump.reserve_dump_area_size; |
| 719 | |
| 720 | if ((ra_start < end) && (ra_end > start)) { |
| 721 | if ((start < ra_start) && (end > ra_end)) { |
Hari Bathini | e4fc48f | 2019-09-11 20:25:05 +0530 | [diff] [blame] | 722 | ret = fadump_add_mem_range(&crash_mrange_info, |
| 723 | start, ra_start); |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 724 | if (ret) |
| 725 | return ret; |
| 726 | |
Hari Bathini | e4fc48f | 2019-09-11 20:25:05 +0530 | [diff] [blame] | 727 | ret = fadump_add_mem_range(&crash_mrange_info, |
| 728 | ra_end, end); |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 729 | } else if (start < ra_start) { |
Hari Bathini | e4fc48f | 2019-09-11 20:25:05 +0530 | [diff] [blame] | 730 | ret = fadump_add_mem_range(&crash_mrange_info, |
| 731 | start, ra_start); |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 732 | } else if (ra_end < end) { |
Hari Bathini | e4fc48f | 2019-09-11 20:25:05 +0530 | [diff] [blame] | 733 | ret = fadump_add_mem_range(&crash_mrange_info, |
| 734 | ra_end, end); |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 735 | } |
| 736 | } else |
Hari Bathini | e4fc48f | 2019-09-11 20:25:05 +0530 | [diff] [blame] | 737 | ret = fadump_add_mem_range(&crash_mrange_info, start, end); |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 738 | |
| 739 | return ret; |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | static int fadump_init_elfcore_header(char *bufp) |
| 743 | { |
| 744 | struct elfhdr *elf; |
| 745 | |
| 746 | elf = (struct elfhdr *) bufp; |
| 747 | bufp += sizeof(struct elfhdr); |
| 748 | memcpy(elf->e_ident, ELFMAG, SELFMAG); |
| 749 | elf->e_ident[EI_CLASS] = ELF_CLASS; |
| 750 | elf->e_ident[EI_DATA] = ELF_DATA; |
| 751 | elf->e_ident[EI_VERSION] = EV_CURRENT; |
| 752 | elf->e_ident[EI_OSABI] = ELF_OSABI; |
| 753 | memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD); |
| 754 | elf->e_type = ET_CORE; |
| 755 | elf->e_machine = ELF_ARCH; |
| 756 | elf->e_version = EV_CURRENT; |
| 757 | elf->e_entry = 0; |
| 758 | elf->e_phoff = sizeof(struct elfhdr); |
| 759 | elf->e_shoff = 0; |
Daniel Axtens | d8bced2 | 2016-09-06 15:32:42 +1000 | [diff] [blame] | 760 | #if defined(_CALL_ELF) |
| 761 | elf->e_flags = _CALL_ELF; |
| 762 | #else |
| 763 | elf->e_flags = 0; |
| 764 | #endif |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 765 | elf->e_ehsize = sizeof(struct elfhdr); |
| 766 | elf->e_phentsize = sizeof(struct elf_phdr); |
| 767 | elf->e_phnum = 0; |
| 768 | elf->e_shentsize = 0; |
| 769 | elf->e_shnum = 0; |
| 770 | elf->e_shstrndx = 0; |
| 771 | |
| 772 | return 0; |
| 773 | } |
| 774 | |
| 775 | /* |
| 776 | * Traverse through memblock structure and setup crash memory ranges. These |
| 777 | * ranges will be used create PT_LOAD program headers in elfcore header. |
| 778 | */ |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 779 | static int fadump_setup_crash_memory_ranges(void) |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 780 | { |
| 781 | struct memblock_region *reg; |
Hari Bathini | e4fc48f | 2019-09-11 20:25:05 +0530 | [diff] [blame] | 782 | u64 start, end; |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 783 | int ret; |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 784 | |
| 785 | pr_debug("Setup crash memory ranges.\n"); |
Hari Bathini | e4fc48f | 2019-09-11 20:25:05 +0530 | [diff] [blame] | 786 | crash_mrange_info.mem_range_cnt = 0; |
Hari Bathini | ced1bf5 | 2018-08-07 02:12:54 +0530 | [diff] [blame] | 787 | |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 788 | /* |
| 789 | * add the first memory chunk (RMA_START through boot_memory_size) as |
| 790 | * a separate memory chunk. The reason is, at the time crash firmware |
| 791 | * will move the content of this memory chunk to different location |
| 792 | * specified during fadump registration. We need to create a separate |
| 793 | * program header for this chunk with the correct offset. |
| 794 | */ |
Hari Bathini | e4fc48f | 2019-09-11 20:25:05 +0530 | [diff] [blame] | 795 | ret = fadump_add_mem_range(&crash_mrange_info, |
| 796 | RMA_START, fw_dump.boot_memory_size); |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 797 | if (ret) |
| 798 | return ret; |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 799 | |
| 800 | for_each_memblock(memory, reg) { |
Hari Bathini | e4fc48f | 2019-09-11 20:25:05 +0530 | [diff] [blame] | 801 | start = (u64)reg->base; |
| 802 | end = start + (u64)reg->size; |
Hari Bathini | a77af552 | 2017-06-01 22:50:38 +0530 | [diff] [blame] | 803 | |
| 804 | /* |
| 805 | * skip the first memory chunk that is already added (RMA_START |
| 806 | * through boot_memory_size). This logic needs a relook if and |
| 807 | * when RMA_START changes to a non-zero value. |
| 808 | */ |
| 809 | BUILD_BUG_ON(RMA_START != 0); |
| 810 | if (start < fw_dump.boot_memory_size) { |
| 811 | if (end > fw_dump.boot_memory_size) |
| 812 | start = fw_dump.boot_memory_size; |
| 813 | else |
| 814 | continue; |
| 815 | } |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 816 | |
| 817 | /* add this range excluding the reserved dump area. */ |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 818 | ret = fadump_exclude_reserved_area(start, end); |
| 819 | if (ret) |
| 820 | return ret; |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 821 | } |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 822 | |
| 823 | return 0; |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 824 | } |
| 825 | |
Mahesh Salgaonkar | d34c5f2 | 2012-02-16 01:14:53 +0000 | [diff] [blame] | 826 | /* |
| 827 | * If the given physical address falls within the boot memory region then |
| 828 | * return the relocated address that points to the dump region reserved |
| 829 | * for saving initial boot memory contents. |
| 830 | */ |
| 831 | static inline unsigned long fadump_relocate(unsigned long paddr) |
| 832 | { |
| 833 | if (paddr > RMA_START && paddr < fw_dump.boot_memory_size) |
Hari Bathini | 41a65d1 | 2019-09-11 20:18:57 +0530 | [diff] [blame] | 834 | return fw_dump.boot_mem_dest_addr + paddr; |
Mahesh Salgaonkar | d34c5f2 | 2012-02-16 01:14:53 +0000 | [diff] [blame] | 835 | else |
| 836 | return paddr; |
| 837 | } |
| 838 | |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 839 | static int fadump_create_elfcore_headers(char *bufp) |
| 840 | { |
| 841 | struct elfhdr *elf; |
| 842 | struct elf_phdr *phdr; |
| 843 | int i; |
| 844 | |
| 845 | fadump_init_elfcore_header(bufp); |
| 846 | elf = (struct elfhdr *)bufp; |
| 847 | bufp += sizeof(struct elfhdr); |
| 848 | |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 849 | /* |
| 850 | * setup ELF PT_NOTE, place holder for cpu notes info. The notes info |
| 851 | * will be populated during second kernel boot after crash. Hence |
| 852 | * this PT_NOTE will always be the first elf note. |
| 853 | * |
| 854 | * NOTE: Any new ELF note addition should be placed after this note. |
| 855 | */ |
| 856 | phdr = (struct elf_phdr *)bufp; |
| 857 | bufp += sizeof(struct elf_phdr); |
| 858 | phdr->p_type = PT_NOTE; |
| 859 | phdr->p_flags = 0; |
| 860 | phdr->p_vaddr = 0; |
| 861 | phdr->p_align = 0; |
| 862 | |
| 863 | phdr->p_offset = 0; |
| 864 | phdr->p_paddr = 0; |
| 865 | phdr->p_filesz = 0; |
| 866 | phdr->p_memsz = 0; |
| 867 | |
| 868 | (elf->e_phnum)++; |
| 869 | |
Mahesh Salgaonkar | d34c5f2 | 2012-02-16 01:14:53 +0000 | [diff] [blame] | 870 | /* setup ELF PT_NOTE for vmcoreinfo */ |
| 871 | phdr = (struct elf_phdr *)bufp; |
| 872 | bufp += sizeof(struct elf_phdr); |
| 873 | phdr->p_type = PT_NOTE; |
| 874 | phdr->p_flags = 0; |
| 875 | phdr->p_vaddr = 0; |
| 876 | phdr->p_align = 0; |
| 877 | |
| 878 | phdr->p_paddr = fadump_relocate(paddr_vmcoreinfo_note()); |
| 879 | phdr->p_offset = phdr->p_paddr; |
Xunlei Pang | 5203f49 | 2017-07-12 14:33:17 -0700 | [diff] [blame] | 880 | phdr->p_memsz = phdr->p_filesz = VMCOREINFO_NOTE_SIZE; |
Mahesh Salgaonkar | d34c5f2 | 2012-02-16 01:14:53 +0000 | [diff] [blame] | 881 | |
| 882 | /* Increment number of program headers. */ |
| 883 | (elf->e_phnum)++; |
| 884 | |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 885 | /* setup PT_LOAD sections. */ |
| 886 | |
Hari Bathini | e4fc48f | 2019-09-11 20:25:05 +0530 | [diff] [blame] | 887 | for (i = 0; i < crash_mrange_info.mem_range_cnt; i++) { |
| 888 | u64 mbase, msize; |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 889 | |
Hari Bathini | e4fc48f | 2019-09-11 20:25:05 +0530 | [diff] [blame] | 890 | mbase = crash_mrange_info.mem_ranges[i].base; |
| 891 | msize = crash_mrange_info.mem_ranges[i].size; |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 892 | if (!msize) |
| 893 | continue; |
| 894 | |
| 895 | phdr = (struct elf_phdr *)bufp; |
| 896 | bufp += sizeof(struct elf_phdr); |
| 897 | phdr->p_type = PT_LOAD; |
| 898 | phdr->p_flags = PF_R|PF_W|PF_X; |
| 899 | phdr->p_offset = mbase; |
| 900 | |
| 901 | if (mbase == RMA_START) { |
| 902 | /* |
| 903 | * The entire RMA region will be moved by firmware |
| 904 | * to the specified destination_address. Hence set |
| 905 | * the correct offset. |
| 906 | */ |
Hari Bathini | 41a65d1 | 2019-09-11 20:18:57 +0530 | [diff] [blame] | 907 | phdr->p_offset = fw_dump.boot_mem_dest_addr; |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | phdr->p_paddr = mbase; |
| 911 | phdr->p_vaddr = (unsigned long)__va(mbase); |
| 912 | phdr->p_filesz = msize; |
| 913 | phdr->p_memsz = msize; |
| 914 | phdr->p_align = 0; |
| 915 | |
| 916 | /* Increment number of program headers. */ |
| 917 | (elf->e_phnum)++; |
| 918 | } |
| 919 | return 0; |
| 920 | } |
| 921 | |
| 922 | static unsigned long init_fadump_header(unsigned long addr) |
| 923 | { |
| 924 | struct fadump_crash_info_header *fdh; |
| 925 | |
| 926 | if (!addr) |
| 927 | return 0; |
| 928 | |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 929 | fdh = __va(addr); |
| 930 | addr += sizeof(struct fadump_crash_info_header); |
| 931 | |
| 932 | memset(fdh, 0, sizeof(struct fadump_crash_info_header)); |
| 933 | fdh->magic_number = FADUMP_CRASH_INFO_MAGIC; |
| 934 | fdh->elfcorehdr_addr = addr; |
Mahesh Salgaonkar | ebaeb5a | 2012-02-16 01:14:45 +0000 | [diff] [blame] | 935 | /* We will set the crashing cpu id in crash_fadump() during crash. */ |
Hari Bathini | 0226e55 | 2019-09-11 20:18:14 +0530 | [diff] [blame] | 936 | fdh->crashing_cpu = FADUMP_CPU_UNKNOWN; |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 937 | |
| 938 | return addr; |
| 939 | } |
| 940 | |
Michal Suchanek | 98b8cd7 | 2017-05-27 17:46:15 +0200 | [diff] [blame] | 941 | static int register_fadump(void) |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 942 | { |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 943 | unsigned long addr; |
| 944 | void *vaddr; |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 945 | int ret; |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 946 | |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 947 | /* |
| 948 | * If no memory is reserved then we can not register for firmware- |
| 949 | * assisted dump. |
| 950 | */ |
| 951 | if (!fw_dump.reserve_dump_area_size) |
Michal Suchanek | 98b8cd7 | 2017-05-27 17:46:15 +0200 | [diff] [blame] | 952 | return -ENODEV; |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 953 | |
Hari Bathini | 1bd6a1c | 2018-08-07 02:12:45 +0530 | [diff] [blame] | 954 | ret = fadump_setup_crash_memory_ranges(); |
| 955 | if (ret) |
| 956 | return ret; |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 957 | |
Hari Bathini | 41a65d1 | 2019-09-11 20:18:57 +0530 | [diff] [blame] | 958 | addr = fw_dump.fadumphdr_addr; |
| 959 | |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 960 | /* Initialize fadump crash info header. */ |
| 961 | addr = init_fadump_header(addr); |
| 962 | vaddr = __va(addr); |
| 963 | |
| 964 | pr_debug("Creating ELF core headers at %#016lx\n", addr); |
| 965 | fadump_create_elfcore_headers(vaddr); |
| 966 | |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 967 | /* register the future kernel dump with firmware. */ |
Hari Bathini | 41a65d1 | 2019-09-11 20:18:57 +0530 | [diff] [blame] | 968 | pr_debug("Registering for firmware-assisted kernel dump...\n"); |
| 969 | return fw_dump.ops->fadump_register(&fw_dump); |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 970 | } |
| 971 | |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 972 | void fadump_cleanup(void) |
| 973 | { |
Hari Bathini | 2790d01 | 2019-09-11 20:21:16 +0530 | [diff] [blame] | 974 | if (!fw_dump.fadump_supported) |
| 975 | return; |
| 976 | |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 977 | /* Invalidate the registration only if dump is active. */ |
| 978 | if (fw_dump.dump_active) { |
Hari Bathini | f351201 | 2019-09-11 20:19:44 +0530 | [diff] [blame] | 979 | pr_debug("Invalidating firmware-assisted dump registration\n"); |
| 980 | fw_dump.ops->fadump_invalidate(&fw_dump); |
Mahesh Salgaonkar | 722cde7 | 2018-04-27 11:53:18 +0530 | [diff] [blame] | 981 | } else if (fw_dump.dump_registered) { |
| 982 | /* Un-register Firmware-assisted dump if it was registered. */ |
Hari Bathini | 41a65d1 | 2019-09-11 20:18:57 +0530 | [diff] [blame] | 983 | fw_dump.ops->fadump_unregister(&fw_dump); |
Hari Bathini | e4fc48f | 2019-09-11 20:25:05 +0530 | [diff] [blame] | 984 | fadump_free_mem_ranges(&crash_mrange_info); |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 985 | } |
Hari Bathini | 2790d01 | 2019-09-11 20:21:16 +0530 | [diff] [blame] | 986 | |
| 987 | if (fw_dump.ops->fadump_cleanup) |
| 988 | fw_dump.ops->fadump_cleanup(&fw_dump); |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 989 | } |
| 990 | |
Hari Bathini | 68fa647 | 2017-06-02 01:10:10 +0530 | [diff] [blame] | 991 | static void fadump_free_reserved_memory(unsigned long start_pfn, |
| 992 | unsigned long end_pfn) |
| 993 | { |
| 994 | unsigned long pfn; |
| 995 | unsigned long time_limit = jiffies + HZ; |
| 996 | |
| 997 | pr_info("freeing reserved memory (0x%llx - 0x%llx)\n", |
| 998 | PFN_PHYS(start_pfn), PFN_PHYS(end_pfn)); |
| 999 | |
| 1000 | for (pfn = start_pfn; pfn < end_pfn; pfn++) { |
| 1001 | free_reserved_page(pfn_to_page(pfn)); |
| 1002 | |
| 1003 | if (time_after(jiffies, time_limit)) { |
| 1004 | cond_resched(); |
| 1005 | time_limit = jiffies + HZ; |
| 1006 | } |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | /* |
| 1011 | * Skip memory holes and free memory that was actually reserved. |
| 1012 | */ |
Hari Bathini | dda9dbf | 2019-09-11 20:25:36 +0530 | [diff] [blame^] | 1013 | static void fadump_release_reserved_area(u64 start, u64 end) |
Hari Bathini | 68fa647 | 2017-06-02 01:10:10 +0530 | [diff] [blame] | 1014 | { |
Hari Bathini | dda9dbf | 2019-09-11 20:25:36 +0530 | [diff] [blame^] | 1015 | u64 tstart, tend, spfn, epfn; |
Hari Bathini | 68fa647 | 2017-06-02 01:10:10 +0530 | [diff] [blame] | 1016 | struct memblock_region *reg; |
Hari Bathini | 68fa647 | 2017-06-02 01:10:10 +0530 | [diff] [blame] | 1017 | |
Hari Bathini | dda9dbf | 2019-09-11 20:25:36 +0530 | [diff] [blame^] | 1018 | spfn = PHYS_PFN(start); |
| 1019 | epfn = PHYS_PFN(end); |
Hari Bathini | 68fa647 | 2017-06-02 01:10:10 +0530 | [diff] [blame] | 1020 | for_each_memblock(memory, reg) { |
Hari Bathini | dda9dbf | 2019-09-11 20:25:36 +0530 | [diff] [blame^] | 1021 | tstart = max_t(u64, spfn, memblock_region_memory_base_pfn(reg)); |
| 1022 | tend = min_t(u64, epfn, memblock_region_memory_end_pfn(reg)); |
Hari Bathini | 68fa647 | 2017-06-02 01:10:10 +0530 | [diff] [blame] | 1023 | if (tstart < tend) { |
| 1024 | fadump_free_reserved_memory(tstart, tend); |
| 1025 | |
Hari Bathini | dda9dbf | 2019-09-11 20:25:36 +0530 | [diff] [blame^] | 1026 | if (tend == epfn) |
Hari Bathini | 68fa647 | 2017-06-02 01:10:10 +0530 | [diff] [blame] | 1027 | break; |
| 1028 | |
Hari Bathini | dda9dbf | 2019-09-11 20:25:36 +0530 | [diff] [blame^] | 1029 | spfn = tend; |
Hari Bathini | 68fa647 | 2017-06-02 01:10:10 +0530 | [diff] [blame] | 1030 | } |
| 1031 | } |
| 1032 | } |
| 1033 | |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1034 | /* |
Hari Bathini | dda9dbf | 2019-09-11 20:25:36 +0530 | [diff] [blame^] | 1035 | * Sort the mem ranges in-place and merge adjacent ranges |
| 1036 | * to minimize the memory ranges count. |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1037 | */ |
Hari Bathini | dda9dbf | 2019-09-11 20:25:36 +0530 | [diff] [blame^] | 1038 | static void sort_and_merge_mem_ranges(struct fadump_mrange_info *mrange_info) |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1039 | { |
Hari Bathini | dda9dbf | 2019-09-11 20:25:36 +0530 | [diff] [blame^] | 1040 | struct fadump_memory_range *mem_ranges; |
| 1041 | struct fadump_memory_range tmp_range; |
| 1042 | u64 base, size; |
| 1043 | int i, j, idx; |
| 1044 | |
| 1045 | if (!reserved_mrange_info.mem_range_cnt) |
| 1046 | return; |
| 1047 | |
| 1048 | /* Sort the memory ranges */ |
| 1049 | mem_ranges = mrange_info->mem_ranges; |
| 1050 | for (i = 0; i < mrange_info->mem_range_cnt; i++) { |
| 1051 | idx = i; |
| 1052 | for (j = (i + 1); j < mrange_info->mem_range_cnt; j++) { |
| 1053 | if (mem_ranges[idx].base > mem_ranges[j].base) |
| 1054 | idx = j; |
| 1055 | } |
| 1056 | if (idx != i) { |
| 1057 | tmp_range = mem_ranges[idx]; |
| 1058 | mem_ranges[idx] = mem_ranges[i]; |
| 1059 | mem_ranges[i] = tmp_range; |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | /* Merge adjacent reserved ranges */ |
| 1064 | idx = 0; |
| 1065 | for (i = 1; i < mrange_info->mem_range_cnt; i++) { |
| 1066 | base = mem_ranges[i-1].base; |
| 1067 | size = mem_ranges[i-1].size; |
| 1068 | if (mem_ranges[i].base == (base + size)) |
| 1069 | mem_ranges[idx].size += mem_ranges[i].size; |
| 1070 | else { |
| 1071 | idx++; |
| 1072 | if (i == idx) |
| 1073 | continue; |
| 1074 | |
| 1075 | mem_ranges[idx] = mem_ranges[i]; |
| 1076 | } |
| 1077 | } |
| 1078 | mrange_info->mem_range_cnt = idx + 1; |
| 1079 | } |
| 1080 | |
| 1081 | /* |
| 1082 | * Scan reserved-ranges to consider them while reserving/releasing |
| 1083 | * memory for FADump. |
| 1084 | */ |
| 1085 | static inline int fadump_scan_reserved_mem_ranges(void) |
| 1086 | { |
| 1087 | struct device_node *root; |
| 1088 | const __be32 *prop; |
| 1089 | int len, ret = -1; |
| 1090 | unsigned long i; |
| 1091 | |
| 1092 | root = of_find_node_by_path("/"); |
| 1093 | if (!root) |
| 1094 | return ret; |
| 1095 | |
| 1096 | prop = of_get_property(root, "reserved-ranges", &len); |
| 1097 | if (!prop) |
| 1098 | return ret; |
| 1099 | |
| 1100 | /* |
| 1101 | * Each reserved range is an (address,size) pair, 2 cells each, |
| 1102 | * totalling 4 cells per range. |
| 1103 | */ |
| 1104 | for (i = 0; i < len / (sizeof(*prop) * 4); i++) { |
| 1105 | u64 base, size; |
| 1106 | |
| 1107 | base = of_read_number(prop + (i * 4) + 0, 2); |
| 1108 | size = of_read_number(prop + (i * 4) + 2, 2); |
| 1109 | |
| 1110 | if (size) { |
| 1111 | ret = fadump_add_mem_range(&reserved_mrange_info, |
| 1112 | base, base + size); |
| 1113 | if (ret < 0) { |
| 1114 | pr_warn("some reserved ranges are ignored!\n"); |
| 1115 | break; |
| 1116 | } |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | return ret; |
| 1121 | } |
| 1122 | |
| 1123 | /* |
| 1124 | * Release the memory that was reserved during early boot to preserve the |
| 1125 | * crash'ed kernel's memory contents except reserved dump area (permanent |
| 1126 | * reservation) and reserved ranges used by F/W. The released memory will |
| 1127 | * be available for general use. |
| 1128 | */ |
| 1129 | static void fadump_release_memory(u64 begin, u64 end) |
| 1130 | { |
| 1131 | u64 ra_start, ra_end, tstart; |
| 1132 | int i, ret; |
| 1133 | |
| 1134 | fadump_scan_reserved_mem_ranges(); |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1135 | |
| 1136 | ra_start = fw_dump.reserve_dump_area_start; |
| 1137 | ra_end = ra_start + fw_dump.reserve_dump_area_size; |
| 1138 | |
Hari Bathini | 68fa647 | 2017-06-02 01:10:10 +0530 | [diff] [blame] | 1139 | /* |
Hari Bathini | dda9dbf | 2019-09-11 20:25:36 +0530 | [diff] [blame^] | 1140 | * Add reserved dump area to reserved ranges list |
| 1141 | * and exclude all these ranges while releasing memory. |
Hari Bathini | 68fa647 | 2017-06-02 01:10:10 +0530 | [diff] [blame] | 1142 | */ |
Hari Bathini | dda9dbf | 2019-09-11 20:25:36 +0530 | [diff] [blame^] | 1143 | ret = fadump_add_mem_range(&reserved_mrange_info, ra_start, ra_end); |
| 1144 | if (ret != 0) { |
| 1145 | /* |
| 1146 | * Not enough memory to setup reserved ranges but the system is |
| 1147 | * running shortage of memory. So, release all the memory except |
| 1148 | * Reserved dump area (reused for next fadump registration). |
| 1149 | */ |
| 1150 | if (begin < ra_end && end > ra_start) { |
| 1151 | if (begin < ra_start) |
| 1152 | fadump_release_reserved_area(begin, ra_start); |
| 1153 | if (end > ra_end) |
| 1154 | fadump_release_reserved_area(ra_end, end); |
| 1155 | } else |
| 1156 | fadump_release_reserved_area(begin, end); |
| 1157 | |
| 1158 | return; |
| 1159 | } |
| 1160 | |
| 1161 | /* Get the reserved ranges list in order first. */ |
| 1162 | sort_and_merge_mem_ranges(&reserved_mrange_info); |
| 1163 | |
| 1164 | /* Exclude reserved ranges and release remaining memory */ |
| 1165 | tstart = begin; |
| 1166 | for (i = 0; i < reserved_mrange_info.mem_range_cnt; i++) { |
| 1167 | ra_start = reserved_mrange_info.mem_ranges[i].base; |
| 1168 | ra_end = ra_start + reserved_mrange_info.mem_ranges[i].size; |
| 1169 | |
| 1170 | if (tstart >= ra_end) |
| 1171 | continue; |
| 1172 | |
| 1173 | if (tstart < ra_start) |
| 1174 | fadump_release_reserved_area(tstart, ra_start); |
| 1175 | tstart = ra_end; |
| 1176 | } |
| 1177 | |
| 1178 | if (tstart < end) |
| 1179 | fadump_release_reserved_area(tstart, end); |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | static void fadump_invalidate_release_mem(void) |
| 1183 | { |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1184 | mutex_lock(&fadump_mutex); |
| 1185 | if (!fw_dump.dump_active) { |
| 1186 | mutex_unlock(&fadump_mutex); |
| 1187 | return; |
| 1188 | } |
| 1189 | |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1190 | fadump_cleanup(); |
| 1191 | mutex_unlock(&fadump_mutex); |
| 1192 | |
Hari Bathini | 8255da9 | 2019-09-11 20:19:28 +0530 | [diff] [blame] | 1193 | fadump_release_memory(fw_dump.boot_memory_size, memblock_end_of_DRAM()); |
Hari Bathini | 961cf26 | 2019-09-11 20:16:36 +0530 | [diff] [blame] | 1194 | fadump_free_cpu_notes_buf(); |
| 1195 | |
Hari Bathini | a4e2e2c | 2019-09-11 20:23:28 +0530 | [diff] [blame] | 1196 | /* |
| 1197 | * Setup kernel metadata and initialize the kernel dump |
| 1198 | * memory structure for FADump re-registration. |
| 1199 | */ |
| 1200 | if (fw_dump.ops->fadump_setup_metadata && |
| 1201 | (fw_dump.ops->fadump_setup_metadata(&fw_dump) < 0)) |
| 1202 | pr_warn("Failed to setup kernel metadata!\n"); |
Hari Bathini | 41a65d1 | 2019-09-11 20:18:57 +0530 | [diff] [blame] | 1203 | fw_dump.ops->fadump_init_mem_struct(&fw_dump); |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | static ssize_t fadump_release_memory_store(struct kobject *kobj, |
| 1207 | struct kobj_attribute *attr, |
| 1208 | const char *buf, size_t count) |
| 1209 | { |
Michal Suchanek | dcdc467 | 2017-06-26 16:06:01 +0200 | [diff] [blame] | 1210 | int input = -1; |
| 1211 | |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1212 | if (!fw_dump.dump_active) |
| 1213 | return -EPERM; |
| 1214 | |
Michal Suchanek | dcdc467 | 2017-06-26 16:06:01 +0200 | [diff] [blame] | 1215 | if (kstrtoint(buf, 0, &input)) |
| 1216 | return -EINVAL; |
| 1217 | |
| 1218 | if (input == 1) { |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1219 | /* |
| 1220 | * Take away the '/proc/vmcore'. We are releasing the dump |
| 1221 | * memory, hence it will not be valid anymore. |
| 1222 | */ |
Michael Ellerman | 2685f82 | 2016-09-30 10:51:46 +1000 | [diff] [blame] | 1223 | #ifdef CONFIG_PROC_VMCORE |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1224 | vmcore_cleanup(); |
Michael Ellerman | 2685f82 | 2016-09-30 10:51:46 +1000 | [diff] [blame] | 1225 | #endif |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1226 | fadump_invalidate_release_mem(); |
| 1227 | |
| 1228 | } else |
| 1229 | return -EINVAL; |
| 1230 | return count; |
| 1231 | } |
| 1232 | |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1233 | static ssize_t fadump_enabled_show(struct kobject *kobj, |
| 1234 | struct kobj_attribute *attr, |
| 1235 | char *buf) |
| 1236 | { |
| 1237 | return sprintf(buf, "%d\n", fw_dump.fadump_enabled); |
| 1238 | } |
| 1239 | |
| 1240 | static ssize_t fadump_register_show(struct kobject *kobj, |
| 1241 | struct kobj_attribute *attr, |
| 1242 | char *buf) |
| 1243 | { |
| 1244 | return sprintf(buf, "%d\n", fw_dump.dump_registered); |
| 1245 | } |
| 1246 | |
| 1247 | static ssize_t fadump_register_store(struct kobject *kobj, |
| 1248 | struct kobj_attribute *attr, |
| 1249 | const char *buf, size_t count) |
| 1250 | { |
| 1251 | int ret = 0; |
Michal Suchanek | dcdc467 | 2017-06-26 16:06:01 +0200 | [diff] [blame] | 1252 | int input = -1; |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1253 | |
Hari Bathini | f351201 | 2019-09-11 20:19:44 +0530 | [diff] [blame] | 1254 | if (!fw_dump.fadump_enabled || fw_dump.dump_active) |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1255 | return -EPERM; |
| 1256 | |
Michal Suchanek | dcdc467 | 2017-06-26 16:06:01 +0200 | [diff] [blame] | 1257 | if (kstrtoint(buf, 0, &input)) |
| 1258 | return -EINVAL; |
| 1259 | |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1260 | mutex_lock(&fadump_mutex); |
| 1261 | |
Michal Suchanek | dcdc467 | 2017-06-26 16:06:01 +0200 | [diff] [blame] | 1262 | switch (input) { |
| 1263 | case 0: |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1264 | if (fw_dump.dump_registered == 0) { |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1265 | goto unlock_out; |
| 1266 | } |
Hari Bathini | f351201 | 2019-09-11 20:19:44 +0530 | [diff] [blame] | 1267 | |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1268 | /* Un-register Firmware-assisted dump */ |
Hari Bathini | 41a65d1 | 2019-09-11 20:18:57 +0530 | [diff] [blame] | 1269 | pr_debug("Un-register firmware-assisted dump\n"); |
| 1270 | fw_dump.ops->fadump_unregister(&fw_dump); |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1271 | break; |
Michal Suchanek | dcdc467 | 2017-06-26 16:06:01 +0200 | [diff] [blame] | 1272 | case 1: |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1273 | if (fw_dump.dump_registered == 1) { |
Hari Bathini | 0823c68 | 2018-09-14 19:36:02 +0530 | [diff] [blame] | 1274 | /* Un-register Firmware-assisted dump */ |
Hari Bathini | 41a65d1 | 2019-09-11 20:18:57 +0530 | [diff] [blame] | 1275 | fw_dump.ops->fadump_unregister(&fw_dump); |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1276 | } |
| 1277 | /* Register Firmware-assisted dump */ |
Michal Suchanek | 98b8cd7 | 2017-05-27 17:46:15 +0200 | [diff] [blame] | 1278 | ret = register_fadump(); |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1279 | break; |
| 1280 | default: |
| 1281 | ret = -EINVAL; |
| 1282 | break; |
| 1283 | } |
| 1284 | |
| 1285 | unlock_out: |
| 1286 | mutex_unlock(&fadump_mutex); |
| 1287 | return ret < 0 ? ret : count; |
| 1288 | } |
| 1289 | |
| 1290 | static int fadump_region_show(struct seq_file *m, void *private) |
| 1291 | { |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1292 | if (!fw_dump.fadump_enabled) |
| 1293 | return 0; |
| 1294 | |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1295 | mutex_lock(&fadump_mutex); |
Hari Bathini | f351201 | 2019-09-11 20:19:44 +0530 | [diff] [blame] | 1296 | fw_dump.ops->fadump_region_show(&fw_dump, m); |
| 1297 | mutex_unlock(&fadump_mutex); |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1298 | return 0; |
| 1299 | } |
| 1300 | |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1301 | static struct kobj_attribute fadump_release_attr = __ATTR(fadump_release_mem, |
| 1302 | 0200, NULL, |
| 1303 | fadump_release_memory_store); |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1304 | static struct kobj_attribute fadump_attr = __ATTR(fadump_enabled, |
| 1305 | 0444, fadump_enabled_show, |
| 1306 | NULL); |
| 1307 | static struct kobj_attribute fadump_register_attr = __ATTR(fadump_registered, |
| 1308 | 0644, fadump_register_show, |
| 1309 | fadump_register_store); |
| 1310 | |
Yangtao Li | f6cee26 | 2018-11-05 10:01:19 -0500 | [diff] [blame] | 1311 | DEFINE_SHOW_ATTRIBUTE(fadump_region); |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1312 | |
| 1313 | static void fadump_init_files(void) |
| 1314 | { |
| 1315 | struct dentry *debugfs_file; |
| 1316 | int rc = 0; |
| 1317 | |
| 1318 | rc = sysfs_create_file(kernel_kobj, &fadump_attr.attr); |
| 1319 | if (rc) |
| 1320 | printk(KERN_ERR "fadump: unable to create sysfs file" |
| 1321 | " fadump_enabled (%d)\n", rc); |
| 1322 | |
| 1323 | rc = sysfs_create_file(kernel_kobj, &fadump_register_attr.attr); |
| 1324 | if (rc) |
| 1325 | printk(KERN_ERR "fadump: unable to create sysfs file" |
| 1326 | " fadump_registered (%d)\n", rc); |
| 1327 | |
| 1328 | debugfs_file = debugfs_create_file("fadump_region", 0444, |
| 1329 | powerpc_debugfs_root, NULL, |
| 1330 | &fadump_region_fops); |
| 1331 | if (!debugfs_file) |
| 1332 | printk(KERN_ERR "fadump: unable to create debugfs file" |
| 1333 | " fadump_region\n"); |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1334 | |
| 1335 | if (fw_dump.dump_active) { |
| 1336 | rc = sysfs_create_file(kernel_kobj, &fadump_release_attr.attr); |
| 1337 | if (rc) |
| 1338 | printk(KERN_ERR "fadump: unable to create sysfs file" |
| 1339 | " fadump_release_mem (%d)\n", rc); |
| 1340 | } |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1341 | return; |
| 1342 | } |
| 1343 | |
| 1344 | /* |
| 1345 | * Prepare for firmware-assisted dump. |
| 1346 | */ |
| 1347 | int __init setup_fadump(void) |
| 1348 | { |
| 1349 | if (!fw_dump.fadump_enabled) |
| 1350 | return 0; |
| 1351 | |
| 1352 | if (!fw_dump.fadump_supported) { |
| 1353 | printk(KERN_ERR "Firmware-assisted dump is not supported on" |
| 1354 | " this hardware\n"); |
| 1355 | return 0; |
| 1356 | } |
| 1357 | |
| 1358 | fadump_show_config(); |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 1359 | /* |
| 1360 | * If dump data is available then see if it is valid and prepare for |
| 1361 | * saving it to the disk. |
| 1362 | */ |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1363 | if (fw_dump.dump_active) { |
| 1364 | /* |
| 1365 | * if dump process fails then invalidate the registration |
| 1366 | * and release memory before proceeding for re-registration. |
| 1367 | */ |
Hari Bathini | f351201 | 2019-09-11 20:19:44 +0530 | [diff] [blame] | 1368 | if (fw_dump.ops->fadump_process(&fw_dump) < 0) |
Mahesh Salgaonkar | b500aff | 2012-02-16 01:15:08 +0000 | [diff] [blame] | 1369 | fadump_invalidate_release_mem(); |
| 1370 | } |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1371 | /* Initialize the kernel dump memory structure for FAD registration. */ |
Mahesh Salgaonkar | 2df173d | 2012-02-16 01:14:37 +0000 | [diff] [blame] | 1372 | else if (fw_dump.reserve_dump_area_size) |
Hari Bathini | 41a65d1 | 2019-09-11 20:18:57 +0530 | [diff] [blame] | 1373 | fw_dump.ops->fadump_init_mem_struct(&fw_dump); |
Hari Bathini | f351201 | 2019-09-11 20:19:44 +0530 | [diff] [blame] | 1374 | |
Mahesh Salgaonkar | 3ccc00a | 2012-02-20 02:15:03 +0000 | [diff] [blame] | 1375 | fadump_init_files(); |
| 1376 | |
| 1377 | return 1; |
| 1378 | } |
| 1379 | subsys_initcall(setup_fadump); |