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