11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2eb39c880SMahesh Salgaonkar /* 3eb39c880SMahesh Salgaonkar * Firmware Assisted dump: A robust mechanism to get reliable kernel crash 4eb39c880SMahesh Salgaonkar * dump with assistance from firmware. This approach does not use kexec, 5eb39c880SMahesh Salgaonkar * instead firmware assists in booting the kdump kernel while preserving 6eb39c880SMahesh Salgaonkar * memory contents. The most of the code implementation has been adapted 7eb39c880SMahesh Salgaonkar * from phyp assisted dump implementation written by Linas Vepstas and 8eb39c880SMahesh Salgaonkar * Manish Ahuja 9eb39c880SMahesh Salgaonkar * 10eb39c880SMahesh Salgaonkar * Copyright 2011 IBM Corporation 11eb39c880SMahesh Salgaonkar * Author: Mahesh Salgaonkar <mahesh@linux.vnet.ibm.com> 12eb39c880SMahesh Salgaonkar */ 13eb39c880SMahesh Salgaonkar 14eb39c880SMahesh Salgaonkar #undef DEBUG 15eb39c880SMahesh Salgaonkar #define pr_fmt(fmt) "fadump: " fmt 16eb39c880SMahesh Salgaonkar 17eb39c880SMahesh Salgaonkar #include <linux/string.h> 18eb39c880SMahesh Salgaonkar #include <linux/memblock.h> 193ccc00a7SMahesh Salgaonkar #include <linux/delay.h> 203ccc00a7SMahesh Salgaonkar #include <linux/seq_file.h> 212df173d9SMahesh Salgaonkar #include <linux/crash_dump.h> 22b500afffSMahesh Salgaonkar #include <linux/kobject.h> 23b500afffSMahesh Salgaonkar #include <linux/sysfs.h> 24a5818313SHari Bathini #include <linux/slab.h> 25a4e92ce8SMahesh Salgaonkar #include <linux/cma.h> 2645d0ba52SChristophe Leroy #include <linux/hugetlb.h> 27eb39c880SMahesh Salgaonkar 287644d581SMichael Ellerman #include <asm/debugfs.h> 29eb39c880SMahesh Salgaonkar #include <asm/page.h> 30eb39c880SMahesh Salgaonkar #include <asm/prom.h> 31eb39c880SMahesh Salgaonkar #include <asm/fadump.h> 32ca986d7fSHari Bathini #include <asm/fadump-internal.h> 33cad3c834SStephen Rothwell #include <asm/setup.h> 34eb39c880SMahesh Salgaonkar 35eb39c880SMahesh Salgaonkar static struct fw_dump fw_dump; 363ccc00a7SMahesh Salgaonkar 373ccc00a7SMahesh Salgaonkar static DEFINE_MUTEX(fadump_mutex); 381bd6a1c4SHari Bathini struct fad_crash_memory_ranges *crash_memory_ranges; 391bd6a1c4SHari Bathini int crash_memory_ranges_size; 402df173d9SMahesh Salgaonkar int crash_mem_ranges; 411bd6a1c4SHari Bathini int max_crash_mem_ranges; 42eb39c880SMahesh Salgaonkar 43a4e92ce8SMahesh Salgaonkar #ifdef CONFIG_CMA 440226e552SHari Bathini static struct cma *fadump_cma; 450226e552SHari Bathini 46a4e92ce8SMahesh Salgaonkar /* 47a4e92ce8SMahesh Salgaonkar * fadump_cma_init() - Initialize CMA area from a fadump reserved memory 48a4e92ce8SMahesh Salgaonkar * 49a4e92ce8SMahesh Salgaonkar * This function initializes CMA area from fadump reserved memory. 50a4e92ce8SMahesh Salgaonkar * The total size of fadump reserved memory covers for boot memory size 51a4e92ce8SMahesh Salgaonkar * + cpu data size + hpte size and metadata. 52a4e92ce8SMahesh Salgaonkar * Initialize only the area equivalent to boot memory size for CMA use. 53a4e92ce8SMahesh Salgaonkar * The reamining portion of fadump reserved memory will be not given 54a4e92ce8SMahesh Salgaonkar * to CMA and pages for thoes will stay reserved. boot memory size is 55a4e92ce8SMahesh Salgaonkar * aligned per CMA requirement to satisy cma_init_reserved_mem() call. 56a4e92ce8SMahesh Salgaonkar * But for some reason even if it fails we still have the memory reservation 57a4e92ce8SMahesh Salgaonkar * with us and we can still continue doing fadump. 58a4e92ce8SMahesh Salgaonkar */ 59a4e92ce8SMahesh Salgaonkar int __init fadump_cma_init(void) 60a4e92ce8SMahesh Salgaonkar { 61a4e92ce8SMahesh Salgaonkar unsigned long long base, size; 62a4e92ce8SMahesh Salgaonkar int rc; 63a4e92ce8SMahesh Salgaonkar 64a4e92ce8SMahesh Salgaonkar if (!fw_dump.fadump_enabled) 65a4e92ce8SMahesh Salgaonkar return 0; 66a4e92ce8SMahesh Salgaonkar 67a4e92ce8SMahesh Salgaonkar /* 68a4e92ce8SMahesh Salgaonkar * Do not use CMA if user has provided fadump=nocma kernel parameter. 69a4e92ce8SMahesh Salgaonkar * Return 1 to continue with fadump old behaviour. 70a4e92ce8SMahesh Salgaonkar */ 71a4e92ce8SMahesh Salgaonkar if (fw_dump.nocma) 72a4e92ce8SMahesh Salgaonkar return 1; 73a4e92ce8SMahesh Salgaonkar 74a4e92ce8SMahesh Salgaonkar base = fw_dump.reserve_dump_area_start; 75a4e92ce8SMahesh Salgaonkar size = fw_dump.boot_memory_size; 76a4e92ce8SMahesh Salgaonkar 77a4e92ce8SMahesh Salgaonkar if (!size) 78a4e92ce8SMahesh Salgaonkar return 0; 79a4e92ce8SMahesh Salgaonkar 80a4e92ce8SMahesh Salgaonkar rc = cma_init_reserved_mem(base, size, 0, "fadump_cma", &fadump_cma); 81a4e92ce8SMahesh Salgaonkar if (rc) { 82a4e92ce8SMahesh Salgaonkar pr_err("Failed to init cma area for firmware-assisted dump,%d\n", rc); 83a4e92ce8SMahesh Salgaonkar /* 84a4e92ce8SMahesh Salgaonkar * Though the CMA init has failed we still have memory 85a4e92ce8SMahesh Salgaonkar * reservation with us. The reserved memory will be 86a4e92ce8SMahesh Salgaonkar * blocked from production system usage. Hence return 1, 87a4e92ce8SMahesh Salgaonkar * so that we can continue with fadump. 88a4e92ce8SMahesh Salgaonkar */ 89a4e92ce8SMahesh Salgaonkar return 1; 90a4e92ce8SMahesh Salgaonkar } 91a4e92ce8SMahesh Salgaonkar 92a4e92ce8SMahesh Salgaonkar /* 93a4e92ce8SMahesh Salgaonkar * So we now have successfully initialized cma area for fadump. 94a4e92ce8SMahesh Salgaonkar */ 95a4e92ce8SMahesh Salgaonkar pr_info("Initialized 0x%lx bytes cma area at %ldMB from 0x%lx " 96a4e92ce8SMahesh Salgaonkar "bytes of memory reserved for firmware-assisted dump\n", 97a4e92ce8SMahesh Salgaonkar cma_get_size(fadump_cma), 98a4e92ce8SMahesh Salgaonkar (unsigned long)cma_get_base(fadump_cma) >> 20, 99a4e92ce8SMahesh Salgaonkar fw_dump.reserve_dump_area_size); 100a4e92ce8SMahesh Salgaonkar return 1; 101a4e92ce8SMahesh Salgaonkar } 102a4e92ce8SMahesh Salgaonkar #else 103a4e92ce8SMahesh Salgaonkar static int __init fadump_cma_init(void) { return 1; } 104a4e92ce8SMahesh Salgaonkar #endif /* CONFIG_CMA */ 105a4e92ce8SMahesh Salgaonkar 106eb39c880SMahesh Salgaonkar /* Scan the Firmware Assisted dump configuration details. */ 107f3512011SHari Bathini int __init early_init_dt_scan_fw_dump(unsigned long node, const char *uname, 108f3512011SHari Bathini int depth, void *data) 109eb39c880SMahesh Salgaonkar { 110*41df5928SHari Bathini if (depth != 1) 111eb39c880SMahesh Salgaonkar return 0; 112eb39c880SMahesh Salgaonkar 113*41df5928SHari Bathini if (strcmp(uname, "rtas") == 0) { 114d3833a70SHari Bathini rtas_fadump_dt_scan(&fw_dump, node); 115eb39c880SMahesh Salgaonkar return 1; 116eb39c880SMahesh Salgaonkar } 117eb39c880SMahesh Salgaonkar 118*41df5928SHari Bathini if (strcmp(uname, "ibm,opal") == 0) { 119*41df5928SHari Bathini opal_fadump_dt_scan(&fw_dump, node); 120*41df5928SHari Bathini return 1; 121*41df5928SHari Bathini } 122*41df5928SHari Bathini 123*41df5928SHari Bathini return 0; 124*41df5928SHari Bathini } 125*41df5928SHari Bathini 126eae0dfccSHari Bathini /* 127eae0dfccSHari Bathini * If fadump is registered, check if the memory provided 1280db6896fSMahesh Salgaonkar * falls within boot memory area and reserved memory area. 129eae0dfccSHari Bathini */ 1300db6896fSMahesh Salgaonkar int is_fadump_memory_area(u64 addr, ulong size) 131eae0dfccSHari Bathini { 1320db6896fSMahesh Salgaonkar u64 d_start = fw_dump.reserve_dump_area_start; 1330db6896fSMahesh Salgaonkar u64 d_end = d_start + fw_dump.reserve_dump_area_size; 1340db6896fSMahesh Salgaonkar 135eae0dfccSHari Bathini if (!fw_dump.dump_registered) 136eae0dfccSHari Bathini return 0; 137eae0dfccSHari Bathini 1380db6896fSMahesh Salgaonkar if (((addr + size) > d_start) && (addr <= d_end)) 1390db6896fSMahesh Salgaonkar return 1; 1400db6896fSMahesh Salgaonkar 141eae0dfccSHari Bathini return (addr + size) > RMA_START && addr <= fw_dump.boot_memory_size; 142eae0dfccSHari Bathini } 143eae0dfccSHari Bathini 1446fcd6baaSNicholas Piggin int should_fadump_crash(void) 1456fcd6baaSNicholas Piggin { 1466fcd6baaSNicholas Piggin if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr) 1476fcd6baaSNicholas Piggin return 0; 1486fcd6baaSNicholas Piggin return 1; 1496fcd6baaSNicholas Piggin } 1506fcd6baaSNicholas Piggin 1513ccc00a7SMahesh Salgaonkar int is_fadump_active(void) 1523ccc00a7SMahesh Salgaonkar { 1533ccc00a7SMahesh Salgaonkar return fw_dump.dump_active; 1543ccc00a7SMahesh Salgaonkar } 1553ccc00a7SMahesh Salgaonkar 156a5a05b91SHari Bathini /* 157961cf26aSHari Bathini * Returns true, if there are no holes in memory area between d_start to d_end, 158961cf26aSHari Bathini * false otherwise. 159a5a05b91SHari Bathini */ 160961cf26aSHari Bathini static bool is_fadump_mem_area_contiguous(u64 d_start, u64 d_end) 161a5a05b91SHari Bathini { 162a5a05b91SHari Bathini struct memblock_region *reg; 163961cf26aSHari Bathini bool ret = false; 164961cf26aSHari Bathini u64 start, end; 165a5a05b91SHari Bathini 166a5a05b91SHari Bathini for_each_memblock(memory, reg) { 167961cf26aSHari Bathini start = max_t(u64, d_start, reg->base); 168961cf26aSHari Bathini end = min_t(u64, d_end, (reg->base + reg->size)); 169961cf26aSHari Bathini if (d_start < end) { 170961cf26aSHari Bathini /* Memory hole from d_start to start */ 171961cf26aSHari Bathini if (start > d_start) 172a5a05b91SHari Bathini break; 173a5a05b91SHari Bathini 174961cf26aSHari Bathini if (end == d_end) { 175961cf26aSHari Bathini ret = true; 176a5a05b91SHari Bathini break; 177a5a05b91SHari Bathini } 178a5a05b91SHari Bathini 179961cf26aSHari Bathini d_start = end + 1; 180a5a05b91SHari Bathini } 181a5a05b91SHari Bathini } 182a5a05b91SHari Bathini 183a5a05b91SHari Bathini return ret; 184a5a05b91SHari Bathini } 185a5a05b91SHari Bathini 186f86593beSMahesh Salgaonkar /* 187961cf26aSHari Bathini * Returns true, if there are no holes in boot memory area, 188961cf26aSHari Bathini * false otherwise. 189961cf26aSHari Bathini */ 1907f0ad11dSHari Bathini bool is_fadump_boot_mem_contiguous(void) 191961cf26aSHari Bathini { 192961cf26aSHari Bathini return is_fadump_mem_area_contiguous(0, fw_dump.boot_memory_size); 193961cf26aSHari Bathini } 194961cf26aSHari Bathini 195961cf26aSHari Bathini /* 196f86593beSMahesh Salgaonkar * Returns true, if there are no holes in reserved memory area, 197f86593beSMahesh Salgaonkar * false otherwise. 198f86593beSMahesh Salgaonkar */ 1997f0ad11dSHari Bathini bool is_fadump_reserved_mem_contiguous(void) 200f86593beSMahesh Salgaonkar { 201961cf26aSHari Bathini u64 d_start, d_end; 202f86593beSMahesh Salgaonkar 203961cf26aSHari Bathini d_start = fw_dump.reserve_dump_area_start; 204961cf26aSHari Bathini d_end = d_start + fw_dump.reserve_dump_area_size; 205961cf26aSHari Bathini return is_fadump_mem_area_contiguous(d_start, d_end); 206f86593beSMahesh Salgaonkar } 207f86593beSMahesh Salgaonkar 2083ccc00a7SMahesh Salgaonkar /* Print firmware assisted dump configurations for debugging purpose. */ 2093ccc00a7SMahesh Salgaonkar static void fadump_show_config(void) 2103ccc00a7SMahesh Salgaonkar { 2113ccc00a7SMahesh Salgaonkar pr_debug("Support for firmware-assisted dump (fadump): %s\n", 2123ccc00a7SMahesh Salgaonkar (fw_dump.fadump_supported ? "present" : "no support")); 2133ccc00a7SMahesh Salgaonkar 2143ccc00a7SMahesh Salgaonkar if (!fw_dump.fadump_supported) 2153ccc00a7SMahesh Salgaonkar return; 2163ccc00a7SMahesh Salgaonkar 2173ccc00a7SMahesh Salgaonkar pr_debug("Fadump enabled : %s\n", 2183ccc00a7SMahesh Salgaonkar (fw_dump.fadump_enabled ? "yes" : "no")); 2193ccc00a7SMahesh Salgaonkar pr_debug("Dump Active : %s\n", 2203ccc00a7SMahesh Salgaonkar (fw_dump.dump_active ? "yes" : "no")); 2213ccc00a7SMahesh Salgaonkar pr_debug("Dump section sizes:\n"); 2223ccc00a7SMahesh Salgaonkar pr_debug(" CPU state data size: %lx\n", fw_dump.cpu_state_data_size); 2233ccc00a7SMahesh Salgaonkar pr_debug(" HPTE region size : %lx\n", fw_dump.hpte_region_size); 2243ccc00a7SMahesh Salgaonkar pr_debug("Boot memory size : %lx\n", fw_dump.boot_memory_size); 2253ccc00a7SMahesh Salgaonkar } 2263ccc00a7SMahesh Salgaonkar 227eb39c880SMahesh Salgaonkar /** 228eb39c880SMahesh Salgaonkar * fadump_calculate_reserve_size(): reserve variable boot area 5% of System RAM 229eb39c880SMahesh Salgaonkar * 230eb39c880SMahesh Salgaonkar * Function to find the largest memory size we need to reserve during early 231eb39c880SMahesh Salgaonkar * boot process. This will be the size of the memory that is required for a 232eb39c880SMahesh Salgaonkar * kernel to boot successfully. 233eb39c880SMahesh Salgaonkar * 234eb39c880SMahesh Salgaonkar * This function has been taken from phyp-assisted dump feature implementation. 235eb39c880SMahesh Salgaonkar * 236eb39c880SMahesh Salgaonkar * returns larger of 256MB or 5% rounded down to multiples of 256MB. 237eb39c880SMahesh Salgaonkar * 238eb39c880SMahesh Salgaonkar * TODO: Come up with better approach to find out more accurate memory size 239eb39c880SMahesh Salgaonkar * that is required for a kernel to boot successfully. 240eb39c880SMahesh Salgaonkar * 241eb39c880SMahesh Salgaonkar */ 242eb39c880SMahesh Salgaonkar static inline unsigned long fadump_calculate_reserve_size(void) 243eb39c880SMahesh Salgaonkar { 24411550dc0SHari Bathini int ret; 24511550dc0SHari Bathini unsigned long long base, size; 246eb39c880SMahesh Salgaonkar 24781d9eca5SHari Bathini if (fw_dump.reserve_bootvar) 24881d9eca5SHari Bathini pr_warn("'fadump_reserve_mem=' parameter is deprecated in favor of 'crashkernel=' parameter.\n"); 24981d9eca5SHari Bathini 250eb39c880SMahesh Salgaonkar /* 25111550dc0SHari Bathini * Check if the size is specified through crashkernel= cmdline 252e7467dc6SHari Bathini * option. If yes, then use that but ignore base as fadump reserves 253e7467dc6SHari Bathini * memory at a predefined offset. 254eb39c880SMahesh Salgaonkar */ 25511550dc0SHari Bathini ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(), 25611550dc0SHari Bathini &size, &base); 25711550dc0SHari Bathini if (ret == 0 && size > 0) { 25848a316e3SHari Bathini unsigned long max_size; 25948a316e3SHari Bathini 26081d9eca5SHari Bathini if (fw_dump.reserve_bootvar) 26181d9eca5SHari Bathini pr_info("Using 'crashkernel=' parameter for memory reservation.\n"); 26281d9eca5SHari Bathini 26311550dc0SHari Bathini fw_dump.reserve_bootvar = (unsigned long)size; 26448a316e3SHari Bathini 26548a316e3SHari Bathini /* 26648a316e3SHari Bathini * Adjust if the boot memory size specified is above 26748a316e3SHari Bathini * the upper limit. 26848a316e3SHari Bathini */ 26948a316e3SHari Bathini max_size = memblock_phys_mem_size() / MAX_BOOT_MEM_RATIO; 27048a316e3SHari Bathini if (fw_dump.reserve_bootvar > max_size) { 27148a316e3SHari Bathini fw_dump.reserve_bootvar = max_size; 27248a316e3SHari Bathini pr_info("Adjusted boot memory size to %luMB\n", 27348a316e3SHari Bathini (fw_dump.reserve_bootvar >> 20)); 27448a316e3SHari Bathini } 27548a316e3SHari Bathini 276eb39c880SMahesh Salgaonkar return fw_dump.reserve_bootvar; 27781d9eca5SHari Bathini } else if (fw_dump.reserve_bootvar) { 27881d9eca5SHari Bathini /* 27981d9eca5SHari Bathini * 'fadump_reserve_mem=' is being used to reserve memory 28081d9eca5SHari Bathini * for firmware-assisted dump. 28181d9eca5SHari Bathini */ 28281d9eca5SHari Bathini return fw_dump.reserve_bootvar; 28311550dc0SHari Bathini } 284eb39c880SMahesh Salgaonkar 285eb39c880SMahesh Salgaonkar /* divide by 20 to get 5% of value */ 28648a316e3SHari Bathini size = memblock_phys_mem_size() / 20; 287eb39c880SMahesh Salgaonkar 288eb39c880SMahesh Salgaonkar /* round it down in multiples of 256 */ 289eb39c880SMahesh Salgaonkar size = size & ~0x0FFFFFFFUL; 290eb39c880SMahesh Salgaonkar 291eb39c880SMahesh Salgaonkar /* Truncate to memory_limit. We don't want to over reserve the memory.*/ 292eb39c880SMahesh Salgaonkar if (memory_limit && size > memory_limit) 293eb39c880SMahesh Salgaonkar size = memory_limit; 294eb39c880SMahesh Salgaonkar 295eb39c880SMahesh Salgaonkar return (size > MIN_BOOT_MEM ? size : MIN_BOOT_MEM); 296eb39c880SMahesh Salgaonkar } 297eb39c880SMahesh Salgaonkar 298eb39c880SMahesh Salgaonkar /* 299eb39c880SMahesh Salgaonkar * Calculate the total memory size required to be reserved for 300eb39c880SMahesh Salgaonkar * firmware-assisted dump registration. 301eb39c880SMahesh Salgaonkar */ 302eb39c880SMahesh Salgaonkar static unsigned long get_fadump_area_size(void) 303eb39c880SMahesh Salgaonkar { 304eb39c880SMahesh Salgaonkar unsigned long size = 0; 305eb39c880SMahesh Salgaonkar 306eb39c880SMahesh Salgaonkar size += fw_dump.cpu_state_data_size; 307eb39c880SMahesh Salgaonkar size += fw_dump.hpte_region_size; 308eb39c880SMahesh Salgaonkar size += fw_dump.boot_memory_size; 3092df173d9SMahesh Salgaonkar size += sizeof(struct fadump_crash_info_header); 3102df173d9SMahesh Salgaonkar size += sizeof(struct elfhdr); /* ELF core header.*/ 311ebaeb5aeSMahesh Salgaonkar size += sizeof(struct elf_phdr); /* place holder for cpu notes */ 3122df173d9SMahesh Salgaonkar /* Program headers for crash memory regions. */ 3132df173d9SMahesh Salgaonkar size += sizeof(struct elf_phdr) * (memblock_num_regions(memory) + 2); 314eb39c880SMahesh Salgaonkar 315eb39c880SMahesh Salgaonkar size = PAGE_ALIGN(size); 316eb39c880SMahesh Salgaonkar return size; 317eb39c880SMahesh Salgaonkar } 318eb39c880SMahesh Salgaonkar 319b71a693dSMahesh Salgaonkar static void __init fadump_reserve_crash_area(unsigned long base, 320b71a693dSMahesh Salgaonkar unsigned long size) 321b71a693dSMahesh Salgaonkar { 322b71a693dSMahesh Salgaonkar struct memblock_region *reg; 323b71a693dSMahesh Salgaonkar unsigned long mstart, mend, msize; 324b71a693dSMahesh Salgaonkar 325b71a693dSMahesh Salgaonkar for_each_memblock(memory, reg) { 326b71a693dSMahesh Salgaonkar mstart = max_t(unsigned long, base, reg->base); 327b71a693dSMahesh Salgaonkar mend = reg->base + reg->size; 328b71a693dSMahesh Salgaonkar mend = min(base + size, mend); 329b71a693dSMahesh Salgaonkar 330b71a693dSMahesh Salgaonkar if (mstart < mend) { 331b71a693dSMahesh Salgaonkar msize = mend - mstart; 332b71a693dSMahesh Salgaonkar memblock_reserve(mstart, msize); 333b71a693dSMahesh Salgaonkar pr_info("Reserved %ldMB of memory at %#016lx for saving crash dump\n", 334b71a693dSMahesh Salgaonkar (msize >> 20), mstart); 335b71a693dSMahesh Salgaonkar } 336b71a693dSMahesh Salgaonkar } 337b71a693dSMahesh Salgaonkar } 338b71a693dSMahesh Salgaonkar 339eb39c880SMahesh Salgaonkar int __init fadump_reserve_mem(void) 340eb39c880SMahesh Salgaonkar { 341eb39c880SMahesh Salgaonkar unsigned long base, size, memory_boundary; 342eb39c880SMahesh Salgaonkar 343eb39c880SMahesh Salgaonkar if (!fw_dump.fadump_enabled) 344eb39c880SMahesh Salgaonkar return 0; 345eb39c880SMahesh Salgaonkar 346eb39c880SMahesh Salgaonkar if (!fw_dump.fadump_supported) { 347eb39c880SMahesh Salgaonkar printk(KERN_INFO "Firmware-assisted dump is not supported on" 348eb39c880SMahesh Salgaonkar " this hardware\n"); 349eb39c880SMahesh Salgaonkar fw_dump.fadump_enabled = 0; 350eb39c880SMahesh Salgaonkar return 0; 351eb39c880SMahesh Salgaonkar } 3523ccc00a7SMahesh Salgaonkar /* 3533ccc00a7SMahesh Salgaonkar * Initialize boot memory size 3543ccc00a7SMahesh Salgaonkar * If dump is active then we have already calculated the size during 3553ccc00a7SMahesh Salgaonkar * first kernel. 3563ccc00a7SMahesh Salgaonkar */ 357f3512011SHari Bathini if (!fw_dump.dump_active) { 358eb39c880SMahesh Salgaonkar fw_dump.boot_memory_size = fadump_calculate_reserve_size(); 359a4e92ce8SMahesh Salgaonkar #ifdef CONFIG_CMA 360a4e92ce8SMahesh Salgaonkar if (!fw_dump.nocma) 361a4e92ce8SMahesh Salgaonkar fw_dump.boot_memory_size = 362a4e92ce8SMahesh Salgaonkar ALIGN(fw_dump.boot_memory_size, 363a4e92ce8SMahesh Salgaonkar FADUMP_CMA_ALIGNMENT); 364a4e92ce8SMahesh Salgaonkar #endif 365a4e92ce8SMahesh Salgaonkar } 366eb39c880SMahesh Salgaonkar 367eb39c880SMahesh Salgaonkar /* 368eb39c880SMahesh Salgaonkar * Calculate the memory boundary. 369eb39c880SMahesh Salgaonkar * If memory_limit is less than actual memory boundary then reserve 370eb39c880SMahesh Salgaonkar * the memory for fadump beyond the memory_limit and adjust the 371eb39c880SMahesh Salgaonkar * memory_limit accordingly, so that the running kernel can run with 372eb39c880SMahesh Salgaonkar * specified memory_limit. 373eb39c880SMahesh Salgaonkar */ 374eb39c880SMahesh Salgaonkar if (memory_limit && memory_limit < memblock_end_of_DRAM()) { 375eb39c880SMahesh Salgaonkar size = get_fadump_area_size(); 376eb39c880SMahesh Salgaonkar if ((memory_limit + size) < memblock_end_of_DRAM()) 377eb39c880SMahesh Salgaonkar memory_limit += size; 378eb39c880SMahesh Salgaonkar else 379eb39c880SMahesh Salgaonkar memory_limit = memblock_end_of_DRAM(); 380eb39c880SMahesh Salgaonkar printk(KERN_INFO "Adjusted memory_limit for firmware-assisted" 381a84fcd46SSuzuki Poulose " dump, now %#016llx\n", memory_limit); 382eb39c880SMahesh Salgaonkar } 383eb39c880SMahesh Salgaonkar if (memory_limit) 384eb39c880SMahesh Salgaonkar memory_boundary = memory_limit; 385eb39c880SMahesh Salgaonkar else 386eb39c880SMahesh Salgaonkar memory_boundary = memblock_end_of_DRAM(); 387eb39c880SMahesh Salgaonkar 3888255da95SHari Bathini size = get_fadump_area_size(); 3898255da95SHari Bathini fw_dump.reserve_dump_area_size = size; 390eb39c880SMahesh Salgaonkar if (fw_dump.dump_active) { 391b71a693dSMahesh Salgaonkar pr_info("Firmware-assisted dump is active.\n"); 392b71a693dSMahesh Salgaonkar 39385975387SHari Bathini #ifdef CONFIG_HUGETLB_PAGE 39485975387SHari Bathini /* 39585975387SHari Bathini * FADump capture kernel doesn't care much about hugepages. 39685975387SHari Bathini * In fact, handling hugepages in capture kernel is asking for 39785975387SHari Bathini * trouble. So, disable HugeTLB support when fadump is active. 39885975387SHari Bathini */ 39985975387SHari Bathini hugetlb_disabled = true; 40085975387SHari Bathini #endif 401eb39c880SMahesh Salgaonkar /* 402eb39c880SMahesh Salgaonkar * If last boot has crashed then reserve all the memory 403eb39c880SMahesh Salgaonkar * above boot_memory_size so that we don't touch it until 404eb39c880SMahesh Salgaonkar * dump is written to disk by userspace tool. This memory 405eb39c880SMahesh Salgaonkar * will be released for general use once the dump is saved. 406eb39c880SMahesh Salgaonkar */ 407eb39c880SMahesh Salgaonkar base = fw_dump.boot_memory_size; 408eb39c880SMahesh Salgaonkar size = memory_boundary - base; 409b71a693dSMahesh Salgaonkar fadump_reserve_crash_area(base, size); 4102df173d9SMahesh Salgaonkar 411f3512011SHari Bathini pr_debug("fadumphdr_addr = %#016lx\n", fw_dump.fadumphdr_addr); 412f3512011SHari Bathini pr_debug("Reserve dump area start address: 0x%lx\n", 413f3512011SHari Bathini fw_dump.reserve_dump_area_start); 4148255da95SHari Bathini } else { 4158255da95SHari Bathini /* 416f6e6bedbSHari Bathini * Reserve memory at an offset closer to bottom of the RAM to 417f6e6bedbSHari Bathini * minimize the impact of memory hot-remove operation. We can't 418f6e6bedbSHari Bathini * use memblock_find_in_range() here since it doesn't allocate 419f6e6bedbSHari Bathini * from bottom to top. 420f6e6bedbSHari Bathini */ 421f6e6bedbSHari Bathini for (base = fw_dump.boot_memory_size; 422f6e6bedbSHari Bathini base <= (memory_boundary - size); 423f6e6bedbSHari Bathini base += size) { 424f6e6bedbSHari Bathini if (memblock_is_region_memory(base, size) && 425f6e6bedbSHari Bathini !memblock_is_region_reserved(base, size)) 426f6e6bedbSHari Bathini break; 427eb39c880SMahesh Salgaonkar } 428f6e6bedbSHari Bathini if ((base > (memory_boundary - size)) || 429f6e6bedbSHari Bathini memblock_reserve(base, size)) { 430f6e6bedbSHari Bathini pr_err("Failed to reserve memory\n"); 431f6e6bedbSHari Bathini return 0; 432f6e6bedbSHari Bathini } 433f6e6bedbSHari Bathini 434f6e6bedbSHari Bathini pr_info("Reserved %ldMB of memory at %ldMB for firmware-" 435f6e6bedbSHari Bathini "assisted dump (System RAM: %ldMB)\n", 436f6e6bedbSHari Bathini (unsigned long)(size >> 20), 437f6e6bedbSHari Bathini (unsigned long)(base >> 20), 438f6e6bedbSHari Bathini (unsigned long)(memblock_phys_mem_size() >> 20)); 439f6e6bedbSHari Bathini 440eb39c880SMahesh Salgaonkar fw_dump.reserve_dump_area_start = base; 441a4e92ce8SMahesh Salgaonkar return fadump_cma_init(); 442a4e92ce8SMahesh Salgaonkar } 443eb39c880SMahesh Salgaonkar return 1; 444eb39c880SMahesh Salgaonkar } 445eb39c880SMahesh Salgaonkar 4461e76609cSSrikar Dronamraju unsigned long __init arch_reserved_kernel_pages(void) 4471e76609cSSrikar Dronamraju { 4481e76609cSSrikar Dronamraju return memblock_reserved_size() / PAGE_SIZE; 4491e76609cSSrikar Dronamraju } 4501e76609cSSrikar Dronamraju 451eb39c880SMahesh Salgaonkar /* Look for fadump= cmdline option. */ 452eb39c880SMahesh Salgaonkar static int __init early_fadump_param(char *p) 453eb39c880SMahesh Salgaonkar { 454eb39c880SMahesh Salgaonkar if (!p) 455eb39c880SMahesh Salgaonkar return 1; 456eb39c880SMahesh Salgaonkar 457eb39c880SMahesh Salgaonkar if (strncmp(p, "on", 2) == 0) 458eb39c880SMahesh Salgaonkar fw_dump.fadump_enabled = 1; 459eb39c880SMahesh Salgaonkar else if (strncmp(p, "off", 3) == 0) 460eb39c880SMahesh Salgaonkar fw_dump.fadump_enabled = 0; 461a4e92ce8SMahesh Salgaonkar else if (strncmp(p, "nocma", 5) == 0) { 462a4e92ce8SMahesh Salgaonkar fw_dump.fadump_enabled = 1; 463a4e92ce8SMahesh Salgaonkar fw_dump.nocma = 1; 464a4e92ce8SMahesh Salgaonkar } 465eb39c880SMahesh Salgaonkar 466eb39c880SMahesh Salgaonkar return 0; 467eb39c880SMahesh Salgaonkar } 468eb39c880SMahesh Salgaonkar early_param("fadump", early_fadump_param); 469eb39c880SMahesh Salgaonkar 47081d9eca5SHari Bathini /* 47181d9eca5SHari Bathini * Look for fadump_reserve_mem= cmdline option 47281d9eca5SHari Bathini * TODO: Remove references to 'fadump_reserve_mem=' parameter, 47381d9eca5SHari Bathini * the sooner 'crashkernel=' parameter is accustomed to. 47481d9eca5SHari Bathini */ 47581d9eca5SHari Bathini static int __init early_fadump_reserve_mem(char *p) 47681d9eca5SHari Bathini { 47781d9eca5SHari Bathini if (p) 47881d9eca5SHari Bathini fw_dump.reserve_bootvar = memparse(p, &p); 47981d9eca5SHari Bathini return 0; 48081d9eca5SHari Bathini } 48181d9eca5SHari Bathini early_param("fadump_reserve_mem", early_fadump_reserve_mem); 48281d9eca5SHari Bathini 483ebaeb5aeSMahesh Salgaonkar void crash_fadump(struct pt_regs *regs, const char *str) 484ebaeb5aeSMahesh Salgaonkar { 485ebaeb5aeSMahesh Salgaonkar struct fadump_crash_info_header *fdh = NULL; 486f2a5e8f0SMahesh Salgaonkar int old_cpu, this_cpu; 487ebaeb5aeSMahesh Salgaonkar 4886fcd6baaSNicholas Piggin if (!should_fadump_crash()) 489ebaeb5aeSMahesh Salgaonkar return; 490ebaeb5aeSMahesh Salgaonkar 491f2a5e8f0SMahesh Salgaonkar /* 492f2a5e8f0SMahesh Salgaonkar * old_cpu == -1 means this is the first CPU which has come here, 493f2a5e8f0SMahesh Salgaonkar * go ahead and trigger fadump. 494f2a5e8f0SMahesh Salgaonkar * 495f2a5e8f0SMahesh Salgaonkar * old_cpu != -1 means some other CPU has already on it's way 496f2a5e8f0SMahesh Salgaonkar * to trigger fadump, just keep looping here. 497f2a5e8f0SMahesh Salgaonkar */ 498f2a5e8f0SMahesh Salgaonkar this_cpu = smp_processor_id(); 499f2a5e8f0SMahesh Salgaonkar old_cpu = cmpxchg(&crashing_cpu, -1, this_cpu); 500f2a5e8f0SMahesh Salgaonkar 501f2a5e8f0SMahesh Salgaonkar if (old_cpu != -1) { 502f2a5e8f0SMahesh Salgaonkar /* 503f2a5e8f0SMahesh Salgaonkar * We can't loop here indefinitely. Wait as long as fadump 504f2a5e8f0SMahesh Salgaonkar * is in force. If we race with fadump un-registration this 505f2a5e8f0SMahesh Salgaonkar * loop will break and then we go down to normal panic path 506f2a5e8f0SMahesh Salgaonkar * and reboot. If fadump is in force the first crashing 507f2a5e8f0SMahesh Salgaonkar * cpu will definitely trigger fadump. 508f2a5e8f0SMahesh Salgaonkar */ 509f2a5e8f0SMahesh Salgaonkar while (fw_dump.dump_registered) 510f2a5e8f0SMahesh Salgaonkar cpu_relax(); 511f2a5e8f0SMahesh Salgaonkar return; 512f2a5e8f0SMahesh Salgaonkar } 513f2a5e8f0SMahesh Salgaonkar 514ebaeb5aeSMahesh Salgaonkar fdh = __va(fw_dump.fadumphdr_addr); 515ebaeb5aeSMahesh Salgaonkar fdh->crashing_cpu = crashing_cpu; 516ebaeb5aeSMahesh Salgaonkar crash_save_vmcoreinfo(); 517ebaeb5aeSMahesh Salgaonkar 518ebaeb5aeSMahesh Salgaonkar if (regs) 519ebaeb5aeSMahesh Salgaonkar fdh->regs = *regs; 520ebaeb5aeSMahesh Salgaonkar else 521ebaeb5aeSMahesh Salgaonkar ppc_save_regs(&fdh->regs); 522ebaeb5aeSMahesh Salgaonkar 523a0512164SRasmus Villemoes fdh->online_mask = *cpu_online_mask; 524ebaeb5aeSMahesh Salgaonkar 52541a65d16SHari Bathini fw_dump.ops->fadump_trigger(fdh, str); 526ebaeb5aeSMahesh Salgaonkar } 527ebaeb5aeSMahesh Salgaonkar 5287f0ad11dSHari Bathini u32 *fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs) 529ebaeb5aeSMahesh Salgaonkar { 530ebaeb5aeSMahesh Salgaonkar struct elf_prstatus prstatus; 531ebaeb5aeSMahesh Salgaonkar 532ebaeb5aeSMahesh Salgaonkar memset(&prstatus, 0, sizeof(prstatus)); 533ebaeb5aeSMahesh Salgaonkar /* 534ebaeb5aeSMahesh Salgaonkar * FIXME: How do i get PID? Do I really need it? 535ebaeb5aeSMahesh Salgaonkar * prstatus.pr_pid = ???? 536ebaeb5aeSMahesh Salgaonkar */ 537ebaeb5aeSMahesh Salgaonkar elf_core_copy_kernel_regs(&prstatus.pr_reg, regs); 53822bd0177SHari Bathini buf = append_elf_note(buf, CRASH_CORE_NOTE_NAME, NT_PRSTATUS, 539ebaeb5aeSMahesh Salgaonkar &prstatus, sizeof(prstatus)); 540ebaeb5aeSMahesh Salgaonkar return buf; 541ebaeb5aeSMahesh Salgaonkar } 542ebaeb5aeSMahesh Salgaonkar 5437f0ad11dSHari Bathini void fadump_update_elfcore_header(char *bufp) 544ebaeb5aeSMahesh Salgaonkar { 545ebaeb5aeSMahesh Salgaonkar struct elfhdr *elf; 546ebaeb5aeSMahesh Salgaonkar struct elf_phdr *phdr; 547ebaeb5aeSMahesh Salgaonkar 548ebaeb5aeSMahesh Salgaonkar elf = (struct elfhdr *)bufp; 549ebaeb5aeSMahesh Salgaonkar bufp += sizeof(struct elfhdr); 550ebaeb5aeSMahesh Salgaonkar 551ebaeb5aeSMahesh Salgaonkar /* First note is a place holder for cpu notes info. */ 552ebaeb5aeSMahesh Salgaonkar phdr = (struct elf_phdr *)bufp; 553ebaeb5aeSMahesh Salgaonkar 554ebaeb5aeSMahesh Salgaonkar if (phdr->p_type == PT_NOTE) { 555961cf26aSHari Bathini phdr->p_paddr = __pa(fw_dump.cpu_notes_buf_vaddr); 556ebaeb5aeSMahesh Salgaonkar phdr->p_offset = phdr->p_paddr; 557ebaeb5aeSMahesh Salgaonkar phdr->p_filesz = fw_dump.cpu_notes_buf_size; 558ebaeb5aeSMahesh Salgaonkar phdr->p_memsz = fw_dump.cpu_notes_buf_size; 559ebaeb5aeSMahesh Salgaonkar } 560ebaeb5aeSMahesh Salgaonkar return; 561ebaeb5aeSMahesh Salgaonkar } 562ebaeb5aeSMahesh Salgaonkar 563961cf26aSHari Bathini static void *fadump_alloc_buffer(unsigned long size) 564ebaeb5aeSMahesh Salgaonkar { 56572aa6517SHari Bathini unsigned long count, i; 566ebaeb5aeSMahesh Salgaonkar struct page *page; 56772aa6517SHari Bathini void *vaddr; 568ebaeb5aeSMahesh Salgaonkar 56972aa6517SHari Bathini vaddr = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO); 570ebaeb5aeSMahesh Salgaonkar if (!vaddr) 571ebaeb5aeSMahesh Salgaonkar return NULL; 572ebaeb5aeSMahesh Salgaonkar 57372aa6517SHari Bathini count = PAGE_ALIGN(size) / PAGE_SIZE; 574ebaeb5aeSMahesh Salgaonkar page = virt_to_page(vaddr); 575ebaeb5aeSMahesh Salgaonkar for (i = 0; i < count; i++) 57672aa6517SHari Bathini mark_page_reserved(page + i); 577ebaeb5aeSMahesh Salgaonkar return vaddr; 578ebaeb5aeSMahesh Salgaonkar } 579ebaeb5aeSMahesh Salgaonkar 580961cf26aSHari Bathini static void fadump_free_buffer(unsigned long vaddr, unsigned long size) 581ebaeb5aeSMahesh Salgaonkar { 58272aa6517SHari Bathini free_reserved_area((void *)vaddr, (void *)(vaddr + size), -1, NULL); 583ebaeb5aeSMahesh Salgaonkar } 584ebaeb5aeSMahesh Salgaonkar 5857f0ad11dSHari Bathini s32 fadump_setup_cpu_notes_buf(u32 num_cpus) 586961cf26aSHari Bathini { 587961cf26aSHari Bathini /* Allocate buffer to hold cpu crash notes. */ 588961cf26aSHari Bathini fw_dump.cpu_notes_buf_size = num_cpus * sizeof(note_buf_t); 589961cf26aSHari Bathini fw_dump.cpu_notes_buf_size = PAGE_ALIGN(fw_dump.cpu_notes_buf_size); 590961cf26aSHari Bathini fw_dump.cpu_notes_buf_vaddr = 591961cf26aSHari Bathini (unsigned long)fadump_alloc_buffer(fw_dump.cpu_notes_buf_size); 592961cf26aSHari Bathini if (!fw_dump.cpu_notes_buf_vaddr) { 593961cf26aSHari Bathini pr_err("Failed to allocate %ld bytes for CPU notes buffer\n", 594961cf26aSHari Bathini fw_dump.cpu_notes_buf_size); 595961cf26aSHari Bathini return -ENOMEM; 596961cf26aSHari Bathini } 597961cf26aSHari Bathini 598961cf26aSHari Bathini pr_debug("Allocated buffer for cpu notes of size %ld at 0x%lx\n", 599961cf26aSHari Bathini fw_dump.cpu_notes_buf_size, 600961cf26aSHari Bathini fw_dump.cpu_notes_buf_vaddr); 601961cf26aSHari Bathini return 0; 602961cf26aSHari Bathini } 603961cf26aSHari Bathini 6047f0ad11dSHari Bathini void fadump_free_cpu_notes_buf(void) 605961cf26aSHari Bathini { 606961cf26aSHari Bathini if (!fw_dump.cpu_notes_buf_vaddr) 607961cf26aSHari Bathini return; 608961cf26aSHari Bathini 609961cf26aSHari Bathini fadump_free_buffer(fw_dump.cpu_notes_buf_vaddr, 610961cf26aSHari Bathini fw_dump.cpu_notes_buf_size); 611961cf26aSHari Bathini fw_dump.cpu_notes_buf_vaddr = 0; 612961cf26aSHari Bathini fw_dump.cpu_notes_buf_size = 0; 613961cf26aSHari Bathini } 614961cf26aSHari Bathini 6151bd6a1c4SHari Bathini static void free_crash_memory_ranges(void) 6161bd6a1c4SHari Bathini { 6171bd6a1c4SHari Bathini kfree(crash_memory_ranges); 6181bd6a1c4SHari Bathini crash_memory_ranges = NULL; 6191bd6a1c4SHari Bathini crash_memory_ranges_size = 0; 6201bd6a1c4SHari Bathini max_crash_mem_ranges = 0; 6211bd6a1c4SHari Bathini } 6221bd6a1c4SHari Bathini 6231bd6a1c4SHari Bathini /* 6241bd6a1c4SHari Bathini * Allocate or reallocate crash memory ranges array in incremental units 6251bd6a1c4SHari Bathini * of PAGE_SIZE. 6261bd6a1c4SHari Bathini */ 6271bd6a1c4SHari Bathini static int allocate_crash_memory_ranges(void) 6281bd6a1c4SHari Bathini { 6291bd6a1c4SHari Bathini struct fad_crash_memory_ranges *new_array; 6301bd6a1c4SHari Bathini u64 new_size; 6311bd6a1c4SHari Bathini 6321bd6a1c4SHari Bathini new_size = crash_memory_ranges_size + PAGE_SIZE; 6331bd6a1c4SHari Bathini pr_debug("Allocating %llu bytes of memory for crash memory ranges\n", 6341bd6a1c4SHari Bathini new_size); 6351bd6a1c4SHari Bathini 6361bd6a1c4SHari Bathini new_array = krealloc(crash_memory_ranges, new_size, GFP_KERNEL); 6371bd6a1c4SHari Bathini if (new_array == NULL) { 6381bd6a1c4SHari Bathini pr_err("Insufficient memory for setting up crash memory ranges\n"); 6391bd6a1c4SHari Bathini free_crash_memory_ranges(); 6401bd6a1c4SHari Bathini return -ENOMEM; 6411bd6a1c4SHari Bathini } 6421bd6a1c4SHari Bathini 6431bd6a1c4SHari Bathini crash_memory_ranges = new_array; 6441bd6a1c4SHari Bathini crash_memory_ranges_size = new_size; 6451bd6a1c4SHari Bathini max_crash_mem_ranges = (new_size / 6461bd6a1c4SHari Bathini sizeof(struct fad_crash_memory_ranges)); 6471bd6a1c4SHari Bathini return 0; 6481bd6a1c4SHari Bathini } 6491bd6a1c4SHari Bathini 6501bd6a1c4SHari Bathini static inline int fadump_add_crash_memory(unsigned long long base, 6512df173d9SMahesh Salgaonkar unsigned long long end) 6522df173d9SMahesh Salgaonkar { 653ced1bf52SHari Bathini u64 start, size; 654ced1bf52SHari Bathini bool is_adjacent = false; 655ced1bf52SHari Bathini 6562df173d9SMahesh Salgaonkar if (base == end) 6571bd6a1c4SHari Bathini return 0; 6581bd6a1c4SHari Bathini 659ced1bf52SHari Bathini /* 660ced1bf52SHari Bathini * Fold adjacent memory ranges to bring down the memory ranges/ 661ced1bf52SHari Bathini * PT_LOAD segments count. 662ced1bf52SHari Bathini */ 663ced1bf52SHari Bathini if (crash_mem_ranges) { 664ced1bf52SHari Bathini start = crash_memory_ranges[crash_mem_ranges - 1].base; 665ced1bf52SHari Bathini size = crash_memory_ranges[crash_mem_ranges - 1].size; 666ced1bf52SHari Bathini 667ced1bf52SHari Bathini if ((start + size) == base) 668ced1bf52SHari Bathini is_adjacent = true; 669ced1bf52SHari Bathini } 670ced1bf52SHari Bathini if (!is_adjacent) { 671ced1bf52SHari Bathini /* resize the array on reaching the limit */ 6721bd6a1c4SHari Bathini if (crash_mem_ranges == max_crash_mem_ranges) { 6731bd6a1c4SHari Bathini int ret; 6741bd6a1c4SHari Bathini 6751bd6a1c4SHari Bathini ret = allocate_crash_memory_ranges(); 6761bd6a1c4SHari Bathini if (ret) 6771bd6a1c4SHari Bathini return ret; 6781bd6a1c4SHari Bathini } 6792df173d9SMahesh Salgaonkar 680ced1bf52SHari Bathini start = base; 681ced1bf52SHari Bathini crash_memory_ranges[crash_mem_ranges].base = start; 6822df173d9SMahesh Salgaonkar crash_mem_ranges++; 683ced1bf52SHari Bathini } 684ced1bf52SHari Bathini 685ced1bf52SHari Bathini crash_memory_ranges[crash_mem_ranges - 1].size = (end - start); 686ced1bf52SHari Bathini pr_debug("crash_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n", 687ced1bf52SHari Bathini (crash_mem_ranges - 1), start, end - 1, (end - start)); 6881bd6a1c4SHari Bathini return 0; 6892df173d9SMahesh Salgaonkar } 6902df173d9SMahesh Salgaonkar 6911bd6a1c4SHari Bathini static int fadump_exclude_reserved_area(unsigned long long start, 6922df173d9SMahesh Salgaonkar unsigned long long end) 6932df173d9SMahesh Salgaonkar { 6942df173d9SMahesh Salgaonkar unsigned long long ra_start, ra_end; 6951bd6a1c4SHari Bathini int ret = 0; 6962df173d9SMahesh Salgaonkar 6972df173d9SMahesh Salgaonkar ra_start = fw_dump.reserve_dump_area_start; 6982df173d9SMahesh Salgaonkar ra_end = ra_start + fw_dump.reserve_dump_area_size; 6992df173d9SMahesh Salgaonkar 7002df173d9SMahesh Salgaonkar if ((ra_start < end) && (ra_end > start)) { 7012df173d9SMahesh Salgaonkar if ((start < ra_start) && (end > ra_end)) { 7021bd6a1c4SHari Bathini ret = fadump_add_crash_memory(start, ra_start); 7031bd6a1c4SHari Bathini if (ret) 7041bd6a1c4SHari Bathini return ret; 7051bd6a1c4SHari Bathini 7061bd6a1c4SHari Bathini ret = fadump_add_crash_memory(ra_end, end); 7072df173d9SMahesh Salgaonkar } else if (start < ra_start) { 7081bd6a1c4SHari Bathini ret = fadump_add_crash_memory(start, ra_start); 7092df173d9SMahesh Salgaonkar } else if (ra_end < end) { 7101bd6a1c4SHari Bathini ret = fadump_add_crash_memory(ra_end, end); 7112df173d9SMahesh Salgaonkar } 7122df173d9SMahesh Salgaonkar } else 7131bd6a1c4SHari Bathini ret = fadump_add_crash_memory(start, end); 7141bd6a1c4SHari Bathini 7151bd6a1c4SHari Bathini return ret; 7162df173d9SMahesh Salgaonkar } 7172df173d9SMahesh Salgaonkar 7182df173d9SMahesh Salgaonkar static int fadump_init_elfcore_header(char *bufp) 7192df173d9SMahesh Salgaonkar { 7202df173d9SMahesh Salgaonkar struct elfhdr *elf; 7212df173d9SMahesh Salgaonkar 7222df173d9SMahesh Salgaonkar elf = (struct elfhdr *) bufp; 7232df173d9SMahesh Salgaonkar bufp += sizeof(struct elfhdr); 7242df173d9SMahesh Salgaonkar memcpy(elf->e_ident, ELFMAG, SELFMAG); 7252df173d9SMahesh Salgaonkar elf->e_ident[EI_CLASS] = ELF_CLASS; 7262df173d9SMahesh Salgaonkar elf->e_ident[EI_DATA] = ELF_DATA; 7272df173d9SMahesh Salgaonkar elf->e_ident[EI_VERSION] = EV_CURRENT; 7282df173d9SMahesh Salgaonkar elf->e_ident[EI_OSABI] = ELF_OSABI; 7292df173d9SMahesh Salgaonkar memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD); 7302df173d9SMahesh Salgaonkar elf->e_type = ET_CORE; 7312df173d9SMahesh Salgaonkar elf->e_machine = ELF_ARCH; 7322df173d9SMahesh Salgaonkar elf->e_version = EV_CURRENT; 7332df173d9SMahesh Salgaonkar elf->e_entry = 0; 7342df173d9SMahesh Salgaonkar elf->e_phoff = sizeof(struct elfhdr); 7352df173d9SMahesh Salgaonkar elf->e_shoff = 0; 736d8bced27SDaniel Axtens #if defined(_CALL_ELF) 737d8bced27SDaniel Axtens elf->e_flags = _CALL_ELF; 738d8bced27SDaniel Axtens #else 739d8bced27SDaniel Axtens elf->e_flags = 0; 740d8bced27SDaniel Axtens #endif 7412df173d9SMahesh Salgaonkar elf->e_ehsize = sizeof(struct elfhdr); 7422df173d9SMahesh Salgaonkar elf->e_phentsize = sizeof(struct elf_phdr); 7432df173d9SMahesh Salgaonkar elf->e_phnum = 0; 7442df173d9SMahesh Salgaonkar elf->e_shentsize = 0; 7452df173d9SMahesh Salgaonkar elf->e_shnum = 0; 7462df173d9SMahesh Salgaonkar elf->e_shstrndx = 0; 7472df173d9SMahesh Salgaonkar 7482df173d9SMahesh Salgaonkar return 0; 7492df173d9SMahesh Salgaonkar } 7502df173d9SMahesh Salgaonkar 7512df173d9SMahesh Salgaonkar /* 7522df173d9SMahesh Salgaonkar * Traverse through memblock structure and setup crash memory ranges. These 7532df173d9SMahesh Salgaonkar * ranges will be used create PT_LOAD program headers in elfcore header. 7542df173d9SMahesh Salgaonkar */ 7551bd6a1c4SHari Bathini static int fadump_setup_crash_memory_ranges(void) 7562df173d9SMahesh Salgaonkar { 7572df173d9SMahesh Salgaonkar struct memblock_region *reg; 7582df173d9SMahesh Salgaonkar unsigned long long start, end; 7591bd6a1c4SHari Bathini int ret; 7602df173d9SMahesh Salgaonkar 7612df173d9SMahesh Salgaonkar pr_debug("Setup crash memory ranges.\n"); 7622df173d9SMahesh Salgaonkar crash_mem_ranges = 0; 763ced1bf52SHari Bathini 7642df173d9SMahesh Salgaonkar /* 7652df173d9SMahesh Salgaonkar * add the first memory chunk (RMA_START through boot_memory_size) as 7662df173d9SMahesh Salgaonkar * a separate memory chunk. The reason is, at the time crash firmware 7672df173d9SMahesh Salgaonkar * will move the content of this memory chunk to different location 7682df173d9SMahesh Salgaonkar * specified during fadump registration. We need to create a separate 7692df173d9SMahesh Salgaonkar * program header for this chunk with the correct offset. 7702df173d9SMahesh Salgaonkar */ 7711bd6a1c4SHari Bathini ret = fadump_add_crash_memory(RMA_START, fw_dump.boot_memory_size); 7721bd6a1c4SHari Bathini if (ret) 7731bd6a1c4SHari Bathini return ret; 7742df173d9SMahesh Salgaonkar 7752df173d9SMahesh Salgaonkar for_each_memblock(memory, reg) { 7762df173d9SMahesh Salgaonkar start = (unsigned long long)reg->base; 7772df173d9SMahesh Salgaonkar end = start + (unsigned long long)reg->size; 778a77af552SHari Bathini 779a77af552SHari Bathini /* 780a77af552SHari Bathini * skip the first memory chunk that is already added (RMA_START 781a77af552SHari Bathini * through boot_memory_size). This logic needs a relook if and 782a77af552SHari Bathini * when RMA_START changes to a non-zero value. 783a77af552SHari Bathini */ 784a77af552SHari Bathini BUILD_BUG_ON(RMA_START != 0); 785a77af552SHari Bathini if (start < fw_dump.boot_memory_size) { 786a77af552SHari Bathini if (end > fw_dump.boot_memory_size) 7872df173d9SMahesh Salgaonkar start = fw_dump.boot_memory_size; 788a77af552SHari Bathini else 789a77af552SHari Bathini continue; 790a77af552SHari Bathini } 7912df173d9SMahesh Salgaonkar 7922df173d9SMahesh Salgaonkar /* add this range excluding the reserved dump area. */ 7931bd6a1c4SHari Bathini ret = fadump_exclude_reserved_area(start, end); 7941bd6a1c4SHari Bathini if (ret) 7951bd6a1c4SHari Bathini return ret; 7962df173d9SMahesh Salgaonkar } 7971bd6a1c4SHari Bathini 7981bd6a1c4SHari Bathini return 0; 7992df173d9SMahesh Salgaonkar } 8002df173d9SMahesh Salgaonkar 801d34c5f26SMahesh Salgaonkar /* 802d34c5f26SMahesh Salgaonkar * If the given physical address falls within the boot memory region then 803d34c5f26SMahesh Salgaonkar * return the relocated address that points to the dump region reserved 804d34c5f26SMahesh Salgaonkar * for saving initial boot memory contents. 805d34c5f26SMahesh Salgaonkar */ 806d34c5f26SMahesh Salgaonkar static inline unsigned long fadump_relocate(unsigned long paddr) 807d34c5f26SMahesh Salgaonkar { 808d34c5f26SMahesh Salgaonkar if (paddr > RMA_START && paddr < fw_dump.boot_memory_size) 80941a65d16SHari Bathini return fw_dump.boot_mem_dest_addr + paddr; 810d34c5f26SMahesh Salgaonkar else 811d34c5f26SMahesh Salgaonkar return paddr; 812d34c5f26SMahesh Salgaonkar } 813d34c5f26SMahesh Salgaonkar 8142df173d9SMahesh Salgaonkar static int fadump_create_elfcore_headers(char *bufp) 8152df173d9SMahesh Salgaonkar { 8162df173d9SMahesh Salgaonkar struct elfhdr *elf; 8172df173d9SMahesh Salgaonkar struct elf_phdr *phdr; 8182df173d9SMahesh Salgaonkar int i; 8192df173d9SMahesh Salgaonkar 8202df173d9SMahesh Salgaonkar fadump_init_elfcore_header(bufp); 8212df173d9SMahesh Salgaonkar elf = (struct elfhdr *)bufp; 8222df173d9SMahesh Salgaonkar bufp += sizeof(struct elfhdr); 8232df173d9SMahesh Salgaonkar 824ebaeb5aeSMahesh Salgaonkar /* 825ebaeb5aeSMahesh Salgaonkar * setup ELF PT_NOTE, place holder for cpu notes info. The notes info 826ebaeb5aeSMahesh Salgaonkar * will be populated during second kernel boot after crash. Hence 827ebaeb5aeSMahesh Salgaonkar * this PT_NOTE will always be the first elf note. 828ebaeb5aeSMahesh Salgaonkar * 829ebaeb5aeSMahesh Salgaonkar * NOTE: Any new ELF note addition should be placed after this note. 830ebaeb5aeSMahesh Salgaonkar */ 831ebaeb5aeSMahesh Salgaonkar phdr = (struct elf_phdr *)bufp; 832ebaeb5aeSMahesh Salgaonkar bufp += sizeof(struct elf_phdr); 833ebaeb5aeSMahesh Salgaonkar phdr->p_type = PT_NOTE; 834ebaeb5aeSMahesh Salgaonkar phdr->p_flags = 0; 835ebaeb5aeSMahesh Salgaonkar phdr->p_vaddr = 0; 836ebaeb5aeSMahesh Salgaonkar phdr->p_align = 0; 837ebaeb5aeSMahesh Salgaonkar 838ebaeb5aeSMahesh Salgaonkar phdr->p_offset = 0; 839ebaeb5aeSMahesh Salgaonkar phdr->p_paddr = 0; 840ebaeb5aeSMahesh Salgaonkar phdr->p_filesz = 0; 841ebaeb5aeSMahesh Salgaonkar phdr->p_memsz = 0; 842ebaeb5aeSMahesh Salgaonkar 843ebaeb5aeSMahesh Salgaonkar (elf->e_phnum)++; 844ebaeb5aeSMahesh Salgaonkar 845d34c5f26SMahesh Salgaonkar /* setup ELF PT_NOTE for vmcoreinfo */ 846d34c5f26SMahesh Salgaonkar phdr = (struct elf_phdr *)bufp; 847d34c5f26SMahesh Salgaonkar bufp += sizeof(struct elf_phdr); 848d34c5f26SMahesh Salgaonkar phdr->p_type = PT_NOTE; 849d34c5f26SMahesh Salgaonkar phdr->p_flags = 0; 850d34c5f26SMahesh Salgaonkar phdr->p_vaddr = 0; 851d34c5f26SMahesh Salgaonkar phdr->p_align = 0; 852d34c5f26SMahesh Salgaonkar 853d34c5f26SMahesh Salgaonkar phdr->p_paddr = fadump_relocate(paddr_vmcoreinfo_note()); 854d34c5f26SMahesh Salgaonkar phdr->p_offset = phdr->p_paddr; 8555203f499SXunlei Pang phdr->p_memsz = phdr->p_filesz = VMCOREINFO_NOTE_SIZE; 856d34c5f26SMahesh Salgaonkar 857d34c5f26SMahesh Salgaonkar /* Increment number of program headers. */ 858d34c5f26SMahesh Salgaonkar (elf->e_phnum)++; 859d34c5f26SMahesh Salgaonkar 8602df173d9SMahesh Salgaonkar /* setup PT_LOAD sections. */ 8612df173d9SMahesh Salgaonkar 8622df173d9SMahesh Salgaonkar for (i = 0; i < crash_mem_ranges; i++) { 8632df173d9SMahesh Salgaonkar unsigned long long mbase, msize; 8642df173d9SMahesh Salgaonkar mbase = crash_memory_ranges[i].base; 8652df173d9SMahesh Salgaonkar msize = crash_memory_ranges[i].size; 8662df173d9SMahesh Salgaonkar 8672df173d9SMahesh Salgaonkar if (!msize) 8682df173d9SMahesh Salgaonkar continue; 8692df173d9SMahesh Salgaonkar 8702df173d9SMahesh Salgaonkar phdr = (struct elf_phdr *)bufp; 8712df173d9SMahesh Salgaonkar bufp += sizeof(struct elf_phdr); 8722df173d9SMahesh Salgaonkar phdr->p_type = PT_LOAD; 8732df173d9SMahesh Salgaonkar phdr->p_flags = PF_R|PF_W|PF_X; 8742df173d9SMahesh Salgaonkar phdr->p_offset = mbase; 8752df173d9SMahesh Salgaonkar 8762df173d9SMahesh Salgaonkar if (mbase == RMA_START) { 8772df173d9SMahesh Salgaonkar /* 8782df173d9SMahesh Salgaonkar * The entire RMA region will be moved by firmware 8792df173d9SMahesh Salgaonkar * to the specified destination_address. Hence set 8802df173d9SMahesh Salgaonkar * the correct offset. 8812df173d9SMahesh Salgaonkar */ 88241a65d16SHari Bathini phdr->p_offset = fw_dump.boot_mem_dest_addr; 8832df173d9SMahesh Salgaonkar } 8842df173d9SMahesh Salgaonkar 8852df173d9SMahesh Salgaonkar phdr->p_paddr = mbase; 8862df173d9SMahesh Salgaonkar phdr->p_vaddr = (unsigned long)__va(mbase); 8872df173d9SMahesh Salgaonkar phdr->p_filesz = msize; 8882df173d9SMahesh Salgaonkar phdr->p_memsz = msize; 8892df173d9SMahesh Salgaonkar phdr->p_align = 0; 8902df173d9SMahesh Salgaonkar 8912df173d9SMahesh Salgaonkar /* Increment number of program headers. */ 8922df173d9SMahesh Salgaonkar (elf->e_phnum)++; 8932df173d9SMahesh Salgaonkar } 8942df173d9SMahesh Salgaonkar return 0; 8952df173d9SMahesh Salgaonkar } 8962df173d9SMahesh Salgaonkar 8972df173d9SMahesh Salgaonkar static unsigned long init_fadump_header(unsigned long addr) 8982df173d9SMahesh Salgaonkar { 8992df173d9SMahesh Salgaonkar struct fadump_crash_info_header *fdh; 9002df173d9SMahesh Salgaonkar 9012df173d9SMahesh Salgaonkar if (!addr) 9022df173d9SMahesh Salgaonkar return 0; 9032df173d9SMahesh Salgaonkar 9042df173d9SMahesh Salgaonkar fdh = __va(addr); 9052df173d9SMahesh Salgaonkar addr += sizeof(struct fadump_crash_info_header); 9062df173d9SMahesh Salgaonkar 9072df173d9SMahesh Salgaonkar memset(fdh, 0, sizeof(struct fadump_crash_info_header)); 9082df173d9SMahesh Salgaonkar fdh->magic_number = FADUMP_CRASH_INFO_MAGIC; 9092df173d9SMahesh Salgaonkar fdh->elfcorehdr_addr = addr; 910ebaeb5aeSMahesh Salgaonkar /* We will set the crashing cpu id in crash_fadump() during crash. */ 9110226e552SHari Bathini fdh->crashing_cpu = FADUMP_CPU_UNKNOWN; 9122df173d9SMahesh Salgaonkar 9132df173d9SMahesh Salgaonkar return addr; 9142df173d9SMahesh Salgaonkar } 9152df173d9SMahesh Salgaonkar 91698b8cd7fSMichal Suchanek static int register_fadump(void) 9173ccc00a7SMahesh Salgaonkar { 9182df173d9SMahesh Salgaonkar unsigned long addr; 9192df173d9SMahesh Salgaonkar void *vaddr; 9201bd6a1c4SHari Bathini int ret; 9212df173d9SMahesh Salgaonkar 9223ccc00a7SMahesh Salgaonkar /* 9233ccc00a7SMahesh Salgaonkar * If no memory is reserved then we can not register for firmware- 9243ccc00a7SMahesh Salgaonkar * assisted dump. 9253ccc00a7SMahesh Salgaonkar */ 9263ccc00a7SMahesh Salgaonkar if (!fw_dump.reserve_dump_area_size) 92798b8cd7fSMichal Suchanek return -ENODEV; 9283ccc00a7SMahesh Salgaonkar 9291bd6a1c4SHari Bathini ret = fadump_setup_crash_memory_ranges(); 9301bd6a1c4SHari Bathini if (ret) 9311bd6a1c4SHari Bathini return ret; 9322df173d9SMahesh Salgaonkar 93341a65d16SHari Bathini addr = fw_dump.fadumphdr_addr; 93441a65d16SHari Bathini 9352df173d9SMahesh Salgaonkar /* Initialize fadump crash info header. */ 9362df173d9SMahesh Salgaonkar addr = init_fadump_header(addr); 9372df173d9SMahesh Salgaonkar vaddr = __va(addr); 9382df173d9SMahesh Salgaonkar 9392df173d9SMahesh Salgaonkar pr_debug("Creating ELF core headers at %#016lx\n", addr); 9402df173d9SMahesh Salgaonkar fadump_create_elfcore_headers(vaddr); 9412df173d9SMahesh Salgaonkar 9423ccc00a7SMahesh Salgaonkar /* register the future kernel dump with firmware. */ 94341a65d16SHari Bathini pr_debug("Registering for firmware-assisted kernel dump...\n"); 94441a65d16SHari Bathini return fw_dump.ops->fadump_register(&fw_dump); 9453ccc00a7SMahesh Salgaonkar } 9463ccc00a7SMahesh Salgaonkar 947b500afffSMahesh Salgaonkar void fadump_cleanup(void) 948b500afffSMahesh Salgaonkar { 949b500afffSMahesh Salgaonkar /* Invalidate the registration only if dump is active. */ 950b500afffSMahesh Salgaonkar if (fw_dump.dump_active) { 951f3512011SHari Bathini pr_debug("Invalidating firmware-assisted dump registration\n"); 952f3512011SHari Bathini fw_dump.ops->fadump_invalidate(&fw_dump); 953722cde76SMahesh Salgaonkar } else if (fw_dump.dump_registered) { 954722cde76SMahesh Salgaonkar /* Un-register Firmware-assisted dump if it was registered. */ 95541a65d16SHari Bathini fw_dump.ops->fadump_unregister(&fw_dump); 9561bd6a1c4SHari Bathini free_crash_memory_ranges(); 957b500afffSMahesh Salgaonkar } 958b500afffSMahesh Salgaonkar } 959b500afffSMahesh Salgaonkar 96068fa6478SHari Bathini static void fadump_free_reserved_memory(unsigned long start_pfn, 96168fa6478SHari Bathini unsigned long end_pfn) 96268fa6478SHari Bathini { 96368fa6478SHari Bathini unsigned long pfn; 96468fa6478SHari Bathini unsigned long time_limit = jiffies + HZ; 96568fa6478SHari Bathini 96668fa6478SHari Bathini pr_info("freeing reserved memory (0x%llx - 0x%llx)\n", 96768fa6478SHari Bathini PFN_PHYS(start_pfn), PFN_PHYS(end_pfn)); 96868fa6478SHari Bathini 96968fa6478SHari Bathini for (pfn = start_pfn; pfn < end_pfn; pfn++) { 97068fa6478SHari Bathini free_reserved_page(pfn_to_page(pfn)); 97168fa6478SHari Bathini 97268fa6478SHari Bathini if (time_after(jiffies, time_limit)) { 97368fa6478SHari Bathini cond_resched(); 97468fa6478SHari Bathini time_limit = jiffies + HZ; 97568fa6478SHari Bathini } 97668fa6478SHari Bathini } 97768fa6478SHari Bathini } 97868fa6478SHari Bathini 97968fa6478SHari Bathini /* 98068fa6478SHari Bathini * Skip memory holes and free memory that was actually reserved. 98168fa6478SHari Bathini */ 98268fa6478SHari Bathini static void fadump_release_reserved_area(unsigned long start, unsigned long end) 98368fa6478SHari Bathini { 98468fa6478SHari Bathini struct memblock_region *reg; 98568fa6478SHari Bathini unsigned long tstart, tend; 98668fa6478SHari Bathini unsigned long start_pfn = PHYS_PFN(start); 98768fa6478SHari Bathini unsigned long end_pfn = PHYS_PFN(end); 98868fa6478SHari Bathini 98968fa6478SHari Bathini for_each_memblock(memory, reg) { 99068fa6478SHari Bathini tstart = max(start_pfn, memblock_region_memory_base_pfn(reg)); 99168fa6478SHari Bathini tend = min(end_pfn, memblock_region_memory_end_pfn(reg)); 99268fa6478SHari Bathini if (tstart < tend) { 99368fa6478SHari Bathini fadump_free_reserved_memory(tstart, tend); 99468fa6478SHari Bathini 99568fa6478SHari Bathini if (tend == end_pfn) 99668fa6478SHari Bathini break; 99768fa6478SHari Bathini 99868fa6478SHari Bathini start_pfn = tend + 1; 99968fa6478SHari Bathini } 100068fa6478SHari Bathini } 100168fa6478SHari Bathini } 100268fa6478SHari Bathini 1003b500afffSMahesh Salgaonkar /* 1004b500afffSMahesh Salgaonkar * Release the memory that was reserved in early boot to preserve the memory 1005b500afffSMahesh Salgaonkar * contents. The released memory will be available for general use. 1006b500afffSMahesh Salgaonkar */ 1007b500afffSMahesh Salgaonkar static void fadump_release_memory(unsigned long begin, unsigned long end) 1008b500afffSMahesh Salgaonkar { 1009b500afffSMahesh Salgaonkar unsigned long ra_start, ra_end; 1010b500afffSMahesh Salgaonkar 1011b500afffSMahesh Salgaonkar ra_start = fw_dump.reserve_dump_area_start; 1012b500afffSMahesh Salgaonkar ra_end = ra_start + fw_dump.reserve_dump_area_size; 1013b500afffSMahesh Salgaonkar 1014b500afffSMahesh Salgaonkar /* 1015b500afffSMahesh Salgaonkar * exclude the dump reserve area. Will reuse it for next 1016b500afffSMahesh Salgaonkar * fadump registration. 1017b500afffSMahesh Salgaonkar */ 101868fa6478SHari Bathini if (begin < ra_end && end > ra_start) { 101968fa6478SHari Bathini if (begin < ra_start) 102068fa6478SHari Bathini fadump_release_reserved_area(begin, ra_start); 102168fa6478SHari Bathini if (end > ra_end) 102268fa6478SHari Bathini fadump_release_reserved_area(ra_end, end); 102368fa6478SHari Bathini } else 102468fa6478SHari Bathini fadump_release_reserved_area(begin, end); 1025b500afffSMahesh Salgaonkar } 1026b500afffSMahesh Salgaonkar 1027b500afffSMahesh Salgaonkar static void fadump_invalidate_release_mem(void) 1028b500afffSMahesh Salgaonkar { 1029b500afffSMahesh Salgaonkar mutex_lock(&fadump_mutex); 1030b500afffSMahesh Salgaonkar if (!fw_dump.dump_active) { 1031b500afffSMahesh Salgaonkar mutex_unlock(&fadump_mutex); 1032b500afffSMahesh Salgaonkar return; 1033b500afffSMahesh Salgaonkar } 1034b500afffSMahesh Salgaonkar 1035b500afffSMahesh Salgaonkar fadump_cleanup(); 1036b500afffSMahesh Salgaonkar mutex_unlock(&fadump_mutex); 1037b500afffSMahesh Salgaonkar 10388255da95SHari Bathini fadump_release_memory(fw_dump.boot_memory_size, memblock_end_of_DRAM()); 1039961cf26aSHari Bathini fadump_free_cpu_notes_buf(); 1040961cf26aSHari Bathini 1041b500afffSMahesh Salgaonkar /* Initialize the kernel dump memory structure for FAD registration. */ 104241a65d16SHari Bathini fw_dump.ops->fadump_init_mem_struct(&fw_dump); 1043b500afffSMahesh Salgaonkar } 1044b500afffSMahesh Salgaonkar 1045b500afffSMahesh Salgaonkar static ssize_t fadump_release_memory_store(struct kobject *kobj, 1046b500afffSMahesh Salgaonkar struct kobj_attribute *attr, 1047b500afffSMahesh Salgaonkar const char *buf, size_t count) 1048b500afffSMahesh Salgaonkar { 1049dcdc4679SMichal Suchanek int input = -1; 1050dcdc4679SMichal Suchanek 1051b500afffSMahesh Salgaonkar if (!fw_dump.dump_active) 1052b500afffSMahesh Salgaonkar return -EPERM; 1053b500afffSMahesh Salgaonkar 1054dcdc4679SMichal Suchanek if (kstrtoint(buf, 0, &input)) 1055dcdc4679SMichal Suchanek return -EINVAL; 1056dcdc4679SMichal Suchanek 1057dcdc4679SMichal Suchanek if (input == 1) { 1058b500afffSMahesh Salgaonkar /* 1059b500afffSMahesh Salgaonkar * Take away the '/proc/vmcore'. We are releasing the dump 1060b500afffSMahesh Salgaonkar * memory, hence it will not be valid anymore. 1061b500afffSMahesh Salgaonkar */ 10622685f826SMichael Ellerman #ifdef CONFIG_PROC_VMCORE 1063b500afffSMahesh Salgaonkar vmcore_cleanup(); 10642685f826SMichael Ellerman #endif 1065b500afffSMahesh Salgaonkar fadump_invalidate_release_mem(); 1066b500afffSMahesh Salgaonkar 1067b500afffSMahesh Salgaonkar } else 1068b500afffSMahesh Salgaonkar return -EINVAL; 1069b500afffSMahesh Salgaonkar return count; 1070b500afffSMahesh Salgaonkar } 1071b500afffSMahesh Salgaonkar 10723ccc00a7SMahesh Salgaonkar static ssize_t fadump_enabled_show(struct kobject *kobj, 10733ccc00a7SMahesh Salgaonkar struct kobj_attribute *attr, 10743ccc00a7SMahesh Salgaonkar char *buf) 10753ccc00a7SMahesh Salgaonkar { 10763ccc00a7SMahesh Salgaonkar return sprintf(buf, "%d\n", fw_dump.fadump_enabled); 10773ccc00a7SMahesh Salgaonkar } 10783ccc00a7SMahesh Salgaonkar 10793ccc00a7SMahesh Salgaonkar static ssize_t fadump_register_show(struct kobject *kobj, 10803ccc00a7SMahesh Salgaonkar struct kobj_attribute *attr, 10813ccc00a7SMahesh Salgaonkar char *buf) 10823ccc00a7SMahesh Salgaonkar { 10833ccc00a7SMahesh Salgaonkar return sprintf(buf, "%d\n", fw_dump.dump_registered); 10843ccc00a7SMahesh Salgaonkar } 10853ccc00a7SMahesh Salgaonkar 10863ccc00a7SMahesh Salgaonkar static ssize_t fadump_register_store(struct kobject *kobj, 10873ccc00a7SMahesh Salgaonkar struct kobj_attribute *attr, 10883ccc00a7SMahesh Salgaonkar const char *buf, size_t count) 10893ccc00a7SMahesh Salgaonkar { 10903ccc00a7SMahesh Salgaonkar int ret = 0; 1091dcdc4679SMichal Suchanek int input = -1; 10923ccc00a7SMahesh Salgaonkar 1093f3512011SHari Bathini if (!fw_dump.fadump_enabled || fw_dump.dump_active) 10943ccc00a7SMahesh Salgaonkar return -EPERM; 10953ccc00a7SMahesh Salgaonkar 1096dcdc4679SMichal Suchanek if (kstrtoint(buf, 0, &input)) 1097dcdc4679SMichal Suchanek return -EINVAL; 1098dcdc4679SMichal Suchanek 10993ccc00a7SMahesh Salgaonkar mutex_lock(&fadump_mutex); 11003ccc00a7SMahesh Salgaonkar 1101dcdc4679SMichal Suchanek switch (input) { 1102dcdc4679SMichal Suchanek case 0: 11033ccc00a7SMahesh Salgaonkar if (fw_dump.dump_registered == 0) { 11043ccc00a7SMahesh Salgaonkar goto unlock_out; 11053ccc00a7SMahesh Salgaonkar } 1106f3512011SHari Bathini 11073ccc00a7SMahesh Salgaonkar /* Un-register Firmware-assisted dump */ 110841a65d16SHari Bathini pr_debug("Un-register firmware-assisted dump\n"); 110941a65d16SHari Bathini fw_dump.ops->fadump_unregister(&fw_dump); 11103ccc00a7SMahesh Salgaonkar break; 1111dcdc4679SMichal Suchanek case 1: 11123ccc00a7SMahesh Salgaonkar if (fw_dump.dump_registered == 1) { 11130823c68bSHari Bathini /* Un-register Firmware-assisted dump */ 111441a65d16SHari Bathini fw_dump.ops->fadump_unregister(&fw_dump); 11153ccc00a7SMahesh Salgaonkar } 11163ccc00a7SMahesh Salgaonkar /* Register Firmware-assisted dump */ 111798b8cd7fSMichal Suchanek ret = register_fadump(); 11183ccc00a7SMahesh Salgaonkar break; 11193ccc00a7SMahesh Salgaonkar default: 11203ccc00a7SMahesh Salgaonkar ret = -EINVAL; 11213ccc00a7SMahesh Salgaonkar break; 11223ccc00a7SMahesh Salgaonkar } 11233ccc00a7SMahesh Salgaonkar 11243ccc00a7SMahesh Salgaonkar unlock_out: 11253ccc00a7SMahesh Salgaonkar mutex_unlock(&fadump_mutex); 11263ccc00a7SMahesh Salgaonkar return ret < 0 ? ret : count; 11273ccc00a7SMahesh Salgaonkar } 11283ccc00a7SMahesh Salgaonkar 11293ccc00a7SMahesh Salgaonkar static int fadump_region_show(struct seq_file *m, void *private) 11303ccc00a7SMahesh Salgaonkar { 11313ccc00a7SMahesh Salgaonkar if (!fw_dump.fadump_enabled) 11323ccc00a7SMahesh Salgaonkar return 0; 11333ccc00a7SMahesh Salgaonkar 1134b500afffSMahesh Salgaonkar mutex_lock(&fadump_mutex); 113541a65d16SHari Bathini fw_dump.ops->fadump_region_show(&fw_dump, m); 1136b500afffSMahesh Salgaonkar mutex_unlock(&fadump_mutex); 11373ccc00a7SMahesh Salgaonkar return 0; 11383ccc00a7SMahesh Salgaonkar } 11393ccc00a7SMahesh Salgaonkar 1140b500afffSMahesh Salgaonkar static struct kobj_attribute fadump_release_attr = __ATTR(fadump_release_mem, 1141b500afffSMahesh Salgaonkar 0200, NULL, 1142b500afffSMahesh Salgaonkar fadump_release_memory_store); 11433ccc00a7SMahesh Salgaonkar static struct kobj_attribute fadump_attr = __ATTR(fadump_enabled, 11443ccc00a7SMahesh Salgaonkar 0444, fadump_enabled_show, 11453ccc00a7SMahesh Salgaonkar NULL); 11463ccc00a7SMahesh Salgaonkar static struct kobj_attribute fadump_register_attr = __ATTR(fadump_registered, 11473ccc00a7SMahesh Salgaonkar 0644, fadump_register_show, 11483ccc00a7SMahesh Salgaonkar fadump_register_store); 11493ccc00a7SMahesh Salgaonkar 1150f6cee260SYangtao Li DEFINE_SHOW_ATTRIBUTE(fadump_region); 11513ccc00a7SMahesh Salgaonkar 11523ccc00a7SMahesh Salgaonkar static void fadump_init_files(void) 11533ccc00a7SMahesh Salgaonkar { 11543ccc00a7SMahesh Salgaonkar struct dentry *debugfs_file; 11553ccc00a7SMahesh Salgaonkar int rc = 0; 11563ccc00a7SMahesh Salgaonkar 11573ccc00a7SMahesh Salgaonkar rc = sysfs_create_file(kernel_kobj, &fadump_attr.attr); 11583ccc00a7SMahesh Salgaonkar if (rc) 11593ccc00a7SMahesh Salgaonkar printk(KERN_ERR "fadump: unable to create sysfs file" 11603ccc00a7SMahesh Salgaonkar " fadump_enabled (%d)\n", rc); 11613ccc00a7SMahesh Salgaonkar 11623ccc00a7SMahesh Salgaonkar rc = sysfs_create_file(kernel_kobj, &fadump_register_attr.attr); 11633ccc00a7SMahesh Salgaonkar if (rc) 11643ccc00a7SMahesh Salgaonkar printk(KERN_ERR "fadump: unable to create sysfs file" 11653ccc00a7SMahesh Salgaonkar " fadump_registered (%d)\n", rc); 11663ccc00a7SMahesh Salgaonkar 11673ccc00a7SMahesh Salgaonkar debugfs_file = debugfs_create_file("fadump_region", 0444, 11683ccc00a7SMahesh Salgaonkar powerpc_debugfs_root, NULL, 11693ccc00a7SMahesh Salgaonkar &fadump_region_fops); 11703ccc00a7SMahesh Salgaonkar if (!debugfs_file) 11713ccc00a7SMahesh Salgaonkar printk(KERN_ERR "fadump: unable to create debugfs file" 11723ccc00a7SMahesh Salgaonkar " fadump_region\n"); 1173b500afffSMahesh Salgaonkar 1174b500afffSMahesh Salgaonkar if (fw_dump.dump_active) { 1175b500afffSMahesh Salgaonkar rc = sysfs_create_file(kernel_kobj, &fadump_release_attr.attr); 1176b500afffSMahesh Salgaonkar if (rc) 1177b500afffSMahesh Salgaonkar printk(KERN_ERR "fadump: unable to create sysfs file" 1178b500afffSMahesh Salgaonkar " fadump_release_mem (%d)\n", rc); 1179b500afffSMahesh Salgaonkar } 11803ccc00a7SMahesh Salgaonkar return; 11813ccc00a7SMahesh Salgaonkar } 11823ccc00a7SMahesh Salgaonkar 11833ccc00a7SMahesh Salgaonkar /* 11843ccc00a7SMahesh Salgaonkar * Prepare for firmware-assisted dump. 11853ccc00a7SMahesh Salgaonkar */ 11863ccc00a7SMahesh Salgaonkar int __init setup_fadump(void) 11873ccc00a7SMahesh Salgaonkar { 11883ccc00a7SMahesh Salgaonkar if (!fw_dump.fadump_enabled) 11893ccc00a7SMahesh Salgaonkar return 0; 11903ccc00a7SMahesh Salgaonkar 11913ccc00a7SMahesh Salgaonkar if (!fw_dump.fadump_supported) { 11923ccc00a7SMahesh Salgaonkar printk(KERN_ERR "Firmware-assisted dump is not supported on" 11933ccc00a7SMahesh Salgaonkar " this hardware\n"); 11943ccc00a7SMahesh Salgaonkar return 0; 11953ccc00a7SMahesh Salgaonkar } 11963ccc00a7SMahesh Salgaonkar 11973ccc00a7SMahesh Salgaonkar fadump_show_config(); 11982df173d9SMahesh Salgaonkar /* 11992df173d9SMahesh Salgaonkar * If dump data is available then see if it is valid and prepare for 12002df173d9SMahesh Salgaonkar * saving it to the disk. 12012df173d9SMahesh Salgaonkar */ 1202b500afffSMahesh Salgaonkar if (fw_dump.dump_active) { 1203b500afffSMahesh Salgaonkar /* 1204b500afffSMahesh Salgaonkar * if dump process fails then invalidate the registration 1205b500afffSMahesh Salgaonkar * and release memory before proceeding for re-registration. 1206b500afffSMahesh Salgaonkar */ 1207f3512011SHari Bathini if (fw_dump.ops->fadump_process(&fw_dump) < 0) 1208b500afffSMahesh Salgaonkar fadump_invalidate_release_mem(); 1209b500afffSMahesh Salgaonkar } 12103ccc00a7SMahesh Salgaonkar /* Initialize the kernel dump memory structure for FAD registration. */ 12112df173d9SMahesh Salgaonkar else if (fw_dump.reserve_dump_area_size) 121241a65d16SHari Bathini fw_dump.ops->fadump_init_mem_struct(&fw_dump); 1213f3512011SHari Bathini 12143ccc00a7SMahesh Salgaonkar fadump_init_files(); 12153ccc00a7SMahesh Salgaonkar 12163ccc00a7SMahesh Salgaonkar return 1; 12173ccc00a7SMahesh Salgaonkar } 12183ccc00a7SMahesh Salgaonkar subsys_initcall(setup_fadump); 1219