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> 27dbf77fedSAneesh Kumar K.V #include <linux/debugfs.h> 28eb39c880SMahesh Salgaonkar 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> 34cbd3d5baSMichael Ellerman #include <asm/interrupt.h> 35eb39c880SMahesh Salgaonkar 36ba608c4fSSourabh Jain /* 37ba608c4fSSourabh Jain * The CPU who acquired the lock to trigger the fadump crash should 38ba608c4fSSourabh Jain * wait for other CPUs to enter. 39ba608c4fSSourabh Jain * 40ba608c4fSSourabh Jain * The timeout is in milliseconds. 41ba608c4fSSourabh Jain */ 42ba608c4fSSourabh Jain #define CRASH_TIMEOUT 500 43ba608c4fSSourabh Jain 44eb39c880SMahesh Salgaonkar static struct fw_dump fw_dump; 453ccc00a7SMahesh Salgaonkar 46b2a815a5SHari Bathini static void __init fadump_reserve_crash_area(u64 base); 47b2a815a5SHari Bathini 48bec53196SHari Bathini #ifndef CONFIG_PRESERVE_FA_DUMP 495f987caeSMichael Ellerman 502e341f56SMichael Ellerman static struct kobject *fadump_kobj; 512e341f56SMichael Ellerman 525f987caeSMichael Ellerman static atomic_t cpus_in_fadump; 533ccc00a7SMahesh Salgaonkar static DEFINE_MUTEX(fadump_mutex); 545f987caeSMichael Ellerman 552e341f56SMichael Ellerman static struct fadump_mrange_info crash_mrange_info = { "crash", NULL, 0, 0, 0, false }; 5602c04e37SHari Bathini 5702c04e37SHari Bathini #define RESERVED_RNGS_SZ 16384 /* 16K - 128 entries */ 5802c04e37SHari Bathini #define RESERVED_RNGS_CNT (RESERVED_RNGS_SZ / \ 5902c04e37SHari Bathini sizeof(struct fadump_memory_range)) 6002c04e37SHari Bathini static struct fadump_memory_range rngs[RESERVED_RNGS_CNT]; 612e341f56SMichael Ellerman static struct fadump_mrange_info 622e341f56SMichael Ellerman reserved_mrange_info = { "reserved", rngs, RESERVED_RNGS_SZ, 0, RESERVED_RNGS_CNT, true }; 6302c04e37SHari Bathini 6402c04e37SHari Bathini static void __init early_init_dt_scan_reserved_ranges(unsigned long node); 65eb39c880SMahesh Salgaonkar 66a4e92ce8SMahesh Salgaonkar #ifdef CONFIG_CMA 670226e552SHari Bathini static struct cma *fadump_cma; 680226e552SHari Bathini 69a4e92ce8SMahesh Salgaonkar /* 70a4e92ce8SMahesh Salgaonkar * fadump_cma_init() - Initialize CMA area from a fadump reserved memory 71a4e92ce8SMahesh Salgaonkar * 72a4e92ce8SMahesh Salgaonkar * This function initializes CMA area from fadump reserved memory. 73a4e92ce8SMahesh Salgaonkar * The total size of fadump reserved memory covers for boot memory size 74a4e92ce8SMahesh Salgaonkar * + cpu data size + hpte size and metadata. 75a4e92ce8SMahesh Salgaonkar * Initialize only the area equivalent to boot memory size for CMA use. 76a4e92ce8SMahesh Salgaonkar * The reamining portion of fadump reserved memory will be not given 77a4e92ce8SMahesh Salgaonkar * to CMA and pages for thoes will stay reserved. boot memory size is 78a4e92ce8SMahesh Salgaonkar * aligned per CMA requirement to satisy cma_init_reserved_mem() call. 79a4e92ce8SMahesh Salgaonkar * But for some reason even if it fails we still have the memory reservation 80a4e92ce8SMahesh Salgaonkar * with us and we can still continue doing fadump. 81a4e92ce8SMahesh Salgaonkar */ 822e341f56SMichael Ellerman static int __init fadump_cma_init(void) 83a4e92ce8SMahesh Salgaonkar { 84a4e92ce8SMahesh Salgaonkar unsigned long long base, size; 85a4e92ce8SMahesh Salgaonkar int rc; 86a4e92ce8SMahesh Salgaonkar 87a4e92ce8SMahesh Salgaonkar if (!fw_dump.fadump_enabled) 88a4e92ce8SMahesh Salgaonkar return 0; 89a4e92ce8SMahesh Salgaonkar 90a4e92ce8SMahesh Salgaonkar /* 91a4e92ce8SMahesh Salgaonkar * Do not use CMA if user has provided fadump=nocma kernel parameter. 92a4e92ce8SMahesh Salgaonkar * Return 1 to continue with fadump old behaviour. 93a4e92ce8SMahesh Salgaonkar */ 94a4e92ce8SMahesh Salgaonkar if (fw_dump.nocma) 95a4e92ce8SMahesh Salgaonkar return 1; 96a4e92ce8SMahesh Salgaonkar 97a4e92ce8SMahesh Salgaonkar base = fw_dump.reserve_dump_area_start; 98a4e92ce8SMahesh Salgaonkar size = fw_dump.boot_memory_size; 99a4e92ce8SMahesh Salgaonkar 100a4e92ce8SMahesh Salgaonkar if (!size) 101a4e92ce8SMahesh Salgaonkar return 0; 102a4e92ce8SMahesh Salgaonkar 103a4e92ce8SMahesh Salgaonkar rc = cma_init_reserved_mem(base, size, 0, "fadump_cma", &fadump_cma); 104a4e92ce8SMahesh Salgaonkar if (rc) { 105a4e92ce8SMahesh Salgaonkar pr_err("Failed to init cma area for firmware-assisted dump,%d\n", rc); 106a4e92ce8SMahesh Salgaonkar /* 107a4e92ce8SMahesh Salgaonkar * Though the CMA init has failed we still have memory 108a4e92ce8SMahesh Salgaonkar * reservation with us. The reserved memory will be 109a4e92ce8SMahesh Salgaonkar * blocked from production system usage. Hence return 1, 110a4e92ce8SMahesh Salgaonkar * so that we can continue with fadump. 111a4e92ce8SMahesh Salgaonkar */ 112a4e92ce8SMahesh Salgaonkar return 1; 113a4e92ce8SMahesh Salgaonkar } 114a4e92ce8SMahesh Salgaonkar 115a4e92ce8SMahesh Salgaonkar /* 116*ee97347fSHari Bathini * If CMA activation fails, keep the pages reserved, instead of 117*ee97347fSHari Bathini * exposing them to buddy allocator. Same as 'fadump=nocma' case. 118*ee97347fSHari Bathini */ 119*ee97347fSHari Bathini cma_reserve_pages_on_error(fadump_cma); 120*ee97347fSHari Bathini 121*ee97347fSHari Bathini /* 122a4e92ce8SMahesh Salgaonkar * So we now have successfully initialized cma area for fadump. 123a4e92ce8SMahesh Salgaonkar */ 124a4e92ce8SMahesh Salgaonkar pr_info("Initialized 0x%lx bytes cma area at %ldMB from 0x%lx " 125a4e92ce8SMahesh Salgaonkar "bytes of memory reserved for firmware-assisted dump\n", 126a4e92ce8SMahesh Salgaonkar cma_get_size(fadump_cma), 127a4e92ce8SMahesh Salgaonkar (unsigned long)cma_get_base(fadump_cma) >> 20, 128a4e92ce8SMahesh Salgaonkar fw_dump.reserve_dump_area_size); 129a4e92ce8SMahesh Salgaonkar return 1; 130a4e92ce8SMahesh Salgaonkar } 131a4e92ce8SMahesh Salgaonkar #else 132a4e92ce8SMahesh Salgaonkar static int __init fadump_cma_init(void) { return 1; } 133a4e92ce8SMahesh Salgaonkar #endif /* CONFIG_CMA */ 134a4e92ce8SMahesh Salgaonkar 135eb39c880SMahesh Salgaonkar /* Scan the Firmware Assisted dump configuration details. */ 136f3512011SHari Bathini int __init early_init_dt_scan_fw_dump(unsigned long node, const char *uname, 137f3512011SHari Bathini int depth, void *data) 138eb39c880SMahesh Salgaonkar { 13902c04e37SHari Bathini if (depth == 0) { 14002c04e37SHari Bathini early_init_dt_scan_reserved_ranges(node); 14102c04e37SHari Bathini return 0; 14202c04e37SHari Bathini } 14302c04e37SHari Bathini 14441df5928SHari Bathini if (depth != 1) 145eb39c880SMahesh Salgaonkar return 0; 146eb39c880SMahesh Salgaonkar 14741df5928SHari Bathini if (strcmp(uname, "rtas") == 0) { 148d3833a70SHari Bathini rtas_fadump_dt_scan(&fw_dump, node); 149eb39c880SMahesh Salgaonkar return 1; 150eb39c880SMahesh Salgaonkar } 151eb39c880SMahesh Salgaonkar 15241df5928SHari Bathini if (strcmp(uname, "ibm,opal") == 0) { 15341df5928SHari Bathini opal_fadump_dt_scan(&fw_dump, node); 15441df5928SHari Bathini return 1; 15541df5928SHari Bathini } 15641df5928SHari Bathini 15741df5928SHari Bathini return 0; 15841df5928SHari Bathini } 15941df5928SHari Bathini 160eae0dfccSHari Bathini /* 161eae0dfccSHari Bathini * If fadump is registered, check if the memory provided 1620db6896fSMahesh Salgaonkar * falls within boot memory area and reserved memory area. 163eae0dfccSHari Bathini */ 164becd91d9SHari Bathini int is_fadump_memory_area(u64 addr, unsigned long size) 165eae0dfccSHari Bathini { 166becd91d9SHari Bathini u64 d_start, d_end; 1670db6896fSMahesh Salgaonkar 168eae0dfccSHari Bathini if (!fw_dump.dump_registered) 169eae0dfccSHari Bathini return 0; 170eae0dfccSHari Bathini 171becd91d9SHari Bathini if (!size) 172becd91d9SHari Bathini return 0; 173becd91d9SHari Bathini 174becd91d9SHari Bathini d_start = fw_dump.reserve_dump_area_start; 175becd91d9SHari Bathini d_end = d_start + fw_dump.reserve_dump_area_size; 1760db6896fSMahesh Salgaonkar if (((addr + size) > d_start) && (addr <= d_end)) 1770db6896fSMahesh Salgaonkar return 1; 1780db6896fSMahesh Salgaonkar 1797dee93a9SHari Bathini return (addr <= fw_dump.boot_mem_top); 180eae0dfccSHari Bathini } 181eae0dfccSHari Bathini 1826fcd6baaSNicholas Piggin int should_fadump_crash(void) 1836fcd6baaSNicholas Piggin { 1846fcd6baaSNicholas Piggin if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr) 1856fcd6baaSNicholas Piggin return 0; 1866fcd6baaSNicholas Piggin return 1; 1876fcd6baaSNicholas Piggin } 1886fcd6baaSNicholas Piggin 1893ccc00a7SMahesh Salgaonkar int is_fadump_active(void) 1903ccc00a7SMahesh Salgaonkar { 1913ccc00a7SMahesh Salgaonkar return fw_dump.dump_active; 1923ccc00a7SMahesh Salgaonkar } 1933ccc00a7SMahesh Salgaonkar 194a5a05b91SHari Bathini /* 195961cf26aSHari Bathini * Returns true, if there are no holes in memory area between d_start to d_end, 196961cf26aSHari Bathini * false otherwise. 197a5a05b91SHari Bathini */ 198961cf26aSHari Bathini static bool is_fadump_mem_area_contiguous(u64 d_start, u64 d_end) 199a5a05b91SHari Bathini { 200b10d6bcaSMike Rapoport phys_addr_t reg_start, reg_end; 201961cf26aSHari Bathini bool ret = false; 202b10d6bcaSMike Rapoport u64 i, start, end; 203a5a05b91SHari Bathini 204b10d6bcaSMike Rapoport for_each_mem_range(i, ®_start, ®_end) { 205b10d6bcaSMike Rapoport start = max_t(u64, d_start, reg_start); 206b10d6bcaSMike Rapoport end = min_t(u64, d_end, reg_end); 207961cf26aSHari Bathini if (d_start < end) { 208961cf26aSHari Bathini /* Memory hole from d_start to start */ 209961cf26aSHari Bathini if (start > d_start) 210a5a05b91SHari Bathini break; 211a5a05b91SHari Bathini 212961cf26aSHari Bathini if (end == d_end) { 213961cf26aSHari Bathini ret = true; 214a5a05b91SHari Bathini break; 215a5a05b91SHari Bathini } 216a5a05b91SHari Bathini 217961cf26aSHari Bathini d_start = end + 1; 218a5a05b91SHari Bathini } 219a5a05b91SHari Bathini } 220a5a05b91SHari Bathini 221a5a05b91SHari Bathini return ret; 222a5a05b91SHari Bathini } 223a5a05b91SHari Bathini 224f86593beSMahesh Salgaonkar /* 225961cf26aSHari Bathini * Returns true, if there are no holes in boot memory area, 226961cf26aSHari Bathini * false otherwise. 227961cf26aSHari Bathini */ 2287f0ad11dSHari Bathini bool is_fadump_boot_mem_contiguous(void) 229961cf26aSHari Bathini { 2307dee93a9SHari Bathini unsigned long d_start, d_end; 2317dee93a9SHari Bathini bool ret = false; 2327dee93a9SHari Bathini int i; 2337dee93a9SHari Bathini 2347dee93a9SHari Bathini for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) { 2357dee93a9SHari Bathini d_start = fw_dump.boot_mem_addr[i]; 2367dee93a9SHari Bathini d_end = d_start + fw_dump.boot_mem_sz[i]; 2377dee93a9SHari Bathini 2387dee93a9SHari Bathini ret = is_fadump_mem_area_contiguous(d_start, d_end); 2397dee93a9SHari Bathini if (!ret) 2407dee93a9SHari Bathini break; 2417dee93a9SHari Bathini } 2427dee93a9SHari Bathini 2437dee93a9SHari Bathini return ret; 244961cf26aSHari Bathini } 245961cf26aSHari Bathini 246961cf26aSHari Bathini /* 247f86593beSMahesh Salgaonkar * Returns true, if there are no holes in reserved memory area, 248f86593beSMahesh Salgaonkar * false otherwise. 249f86593beSMahesh Salgaonkar */ 2507f0ad11dSHari Bathini bool is_fadump_reserved_mem_contiguous(void) 251f86593beSMahesh Salgaonkar { 252961cf26aSHari Bathini u64 d_start, d_end; 253f86593beSMahesh Salgaonkar 254961cf26aSHari Bathini d_start = fw_dump.reserve_dump_area_start; 255961cf26aSHari Bathini d_end = d_start + fw_dump.reserve_dump_area_size; 256961cf26aSHari Bathini return is_fadump_mem_area_contiguous(d_start, d_end); 257f86593beSMahesh Salgaonkar } 258f86593beSMahesh Salgaonkar 2593ccc00a7SMahesh Salgaonkar /* Print firmware assisted dump configurations for debugging purpose. */ 260d276960dSNick Child static void __init fadump_show_config(void) 2613ccc00a7SMahesh Salgaonkar { 2627dee93a9SHari Bathini int i; 2637dee93a9SHari Bathini 2643ccc00a7SMahesh Salgaonkar pr_debug("Support for firmware-assisted dump (fadump): %s\n", 2653ccc00a7SMahesh Salgaonkar (fw_dump.fadump_supported ? "present" : "no support")); 2663ccc00a7SMahesh Salgaonkar 2673ccc00a7SMahesh Salgaonkar if (!fw_dump.fadump_supported) 2683ccc00a7SMahesh Salgaonkar return; 2693ccc00a7SMahesh Salgaonkar 2703ccc00a7SMahesh Salgaonkar pr_debug("Fadump enabled : %s\n", 2713ccc00a7SMahesh Salgaonkar (fw_dump.fadump_enabled ? "yes" : "no")); 2723ccc00a7SMahesh Salgaonkar pr_debug("Dump Active : %s\n", 2733ccc00a7SMahesh Salgaonkar (fw_dump.dump_active ? "yes" : "no")); 2743ccc00a7SMahesh Salgaonkar pr_debug("Dump section sizes:\n"); 2753ccc00a7SMahesh Salgaonkar pr_debug(" CPU state data size: %lx\n", fw_dump.cpu_state_data_size); 2763ccc00a7SMahesh Salgaonkar pr_debug(" HPTE region size : %lx\n", fw_dump.hpte_region_size); 2773ccc00a7SMahesh Salgaonkar pr_debug(" Boot memory size : %lx\n", fw_dump.boot_memory_size); 2787dee93a9SHari Bathini pr_debug(" Boot memory top : %llx\n", fw_dump.boot_mem_top); 2797dee93a9SHari Bathini pr_debug("Boot memory regions cnt: %llx\n", fw_dump.boot_mem_regs_cnt); 2807dee93a9SHari Bathini for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) { 2817dee93a9SHari Bathini pr_debug("[%03d] base = %llx, size = %llx\n", i, 2827dee93a9SHari Bathini fw_dump.boot_mem_addr[i], fw_dump.boot_mem_sz[i]); 2837dee93a9SHari Bathini } 2843ccc00a7SMahesh Salgaonkar } 2853ccc00a7SMahesh Salgaonkar 286eb39c880SMahesh Salgaonkar /** 287eb39c880SMahesh Salgaonkar * fadump_calculate_reserve_size(): reserve variable boot area 5% of System RAM 288eb39c880SMahesh Salgaonkar * 289eb39c880SMahesh Salgaonkar * Function to find the largest memory size we need to reserve during early 290eb39c880SMahesh Salgaonkar * boot process. This will be the size of the memory that is required for a 291eb39c880SMahesh Salgaonkar * kernel to boot successfully. 292eb39c880SMahesh Salgaonkar * 293eb39c880SMahesh Salgaonkar * This function has been taken from phyp-assisted dump feature implementation. 294eb39c880SMahesh Salgaonkar * 295eb39c880SMahesh Salgaonkar * returns larger of 256MB or 5% rounded down to multiples of 256MB. 296eb39c880SMahesh Salgaonkar * 297eb39c880SMahesh Salgaonkar * TODO: Come up with better approach to find out more accurate memory size 298eb39c880SMahesh Salgaonkar * that is required for a kernel to boot successfully. 299eb39c880SMahesh Salgaonkar * 300eb39c880SMahesh Salgaonkar */ 301fbced154SNathan Chancellor static __init u64 fadump_calculate_reserve_size(void) 302eb39c880SMahesh Salgaonkar { 3037b1b3b48SHari Bathini u64 base, size, bootmem_min; 30411550dc0SHari Bathini int ret; 305eb39c880SMahesh Salgaonkar 30681d9eca5SHari Bathini if (fw_dump.reserve_bootvar) 30781d9eca5SHari Bathini pr_warn("'fadump_reserve_mem=' parameter is deprecated in favor of 'crashkernel=' parameter.\n"); 30881d9eca5SHari Bathini 309eb39c880SMahesh Salgaonkar /* 31011550dc0SHari Bathini * Check if the size is specified through crashkernel= cmdline 311e7467dc6SHari Bathini * option. If yes, then use that but ignore base as fadump reserves 312e7467dc6SHari Bathini * memory at a predefined offset. 313eb39c880SMahesh Salgaonkar */ 31411550dc0SHari Bathini ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(), 31511550dc0SHari Bathini &size, &base); 31611550dc0SHari Bathini if (ret == 0 && size > 0) { 31748a316e3SHari Bathini unsigned long max_size; 31848a316e3SHari Bathini 31981d9eca5SHari Bathini if (fw_dump.reserve_bootvar) 32081d9eca5SHari Bathini pr_info("Using 'crashkernel=' parameter for memory reservation.\n"); 32181d9eca5SHari Bathini 32211550dc0SHari Bathini fw_dump.reserve_bootvar = (unsigned long)size; 32348a316e3SHari Bathini 32448a316e3SHari Bathini /* 32548a316e3SHari Bathini * Adjust if the boot memory size specified is above 32648a316e3SHari Bathini * the upper limit. 32748a316e3SHari Bathini */ 32848a316e3SHari Bathini max_size = memblock_phys_mem_size() / MAX_BOOT_MEM_RATIO; 32948a316e3SHari Bathini if (fw_dump.reserve_bootvar > max_size) { 33048a316e3SHari Bathini fw_dump.reserve_bootvar = max_size; 33148a316e3SHari Bathini pr_info("Adjusted boot memory size to %luMB\n", 33248a316e3SHari Bathini (fw_dump.reserve_bootvar >> 20)); 33348a316e3SHari Bathini } 33448a316e3SHari Bathini 335eb39c880SMahesh Salgaonkar return fw_dump.reserve_bootvar; 33681d9eca5SHari Bathini } else if (fw_dump.reserve_bootvar) { 33781d9eca5SHari Bathini /* 33881d9eca5SHari Bathini * 'fadump_reserve_mem=' is being used to reserve memory 33981d9eca5SHari Bathini * for firmware-assisted dump. 34081d9eca5SHari Bathini */ 34181d9eca5SHari Bathini return fw_dump.reserve_bootvar; 34211550dc0SHari Bathini } 343eb39c880SMahesh Salgaonkar 344eb39c880SMahesh Salgaonkar /* divide by 20 to get 5% of value */ 34548a316e3SHari Bathini size = memblock_phys_mem_size() / 20; 346eb39c880SMahesh Salgaonkar 347eb39c880SMahesh Salgaonkar /* round it down in multiples of 256 */ 348eb39c880SMahesh Salgaonkar size = size & ~0x0FFFFFFFUL; 349eb39c880SMahesh Salgaonkar 350eb39c880SMahesh Salgaonkar /* Truncate to memory_limit. We don't want to over reserve the memory.*/ 351eb39c880SMahesh Salgaonkar if (memory_limit && size > memory_limit) 352eb39c880SMahesh Salgaonkar size = memory_limit; 353eb39c880SMahesh Salgaonkar 3547b1b3b48SHari Bathini bootmem_min = fw_dump.ops->fadump_get_bootmem_min(); 3557b1b3b48SHari Bathini return (size > bootmem_min ? size : bootmem_min); 356eb39c880SMahesh Salgaonkar } 357eb39c880SMahesh Salgaonkar 358eb39c880SMahesh Salgaonkar /* 359eb39c880SMahesh Salgaonkar * Calculate the total memory size required to be reserved for 360eb39c880SMahesh Salgaonkar * firmware-assisted dump registration. 361eb39c880SMahesh Salgaonkar */ 362d276960dSNick Child static unsigned long __init get_fadump_area_size(void) 363eb39c880SMahesh Salgaonkar { 364eb39c880SMahesh Salgaonkar unsigned long size = 0; 365eb39c880SMahesh Salgaonkar 366eb39c880SMahesh Salgaonkar size += fw_dump.cpu_state_data_size; 367eb39c880SMahesh Salgaonkar size += fw_dump.hpte_region_size; 368eb39c880SMahesh Salgaonkar size += fw_dump.boot_memory_size; 3692df173d9SMahesh Salgaonkar size += sizeof(struct fadump_crash_info_header); 3702df173d9SMahesh Salgaonkar size += sizeof(struct elfhdr); /* ELF core header.*/ 371ebaeb5aeSMahesh Salgaonkar size += sizeof(struct elf_phdr); /* place holder for cpu notes */ 3722df173d9SMahesh Salgaonkar /* Program headers for crash memory regions. */ 3732df173d9SMahesh Salgaonkar size += sizeof(struct elf_phdr) * (memblock_num_regions(memory) + 2); 374eb39c880SMahesh Salgaonkar 375eb39c880SMahesh Salgaonkar size = PAGE_ALIGN(size); 376742a265aSHari Bathini 377742a265aSHari Bathini /* This is to hold kernel metadata on platforms that support it */ 378742a265aSHari Bathini size += (fw_dump.ops->fadump_get_metadata_size ? 379742a265aSHari Bathini fw_dump.ops->fadump_get_metadata_size() : 0); 380eb39c880SMahesh Salgaonkar return size; 381eb39c880SMahesh Salgaonkar } 382eb39c880SMahesh Salgaonkar 3837dee93a9SHari Bathini static int __init add_boot_mem_region(unsigned long rstart, 3847dee93a9SHari Bathini unsigned long rsize) 3857dee93a9SHari Bathini { 3867dee93a9SHari Bathini int i = fw_dump.boot_mem_regs_cnt++; 3877dee93a9SHari Bathini 3887dee93a9SHari Bathini if (fw_dump.boot_mem_regs_cnt > FADUMP_MAX_MEM_REGS) { 3897dee93a9SHari Bathini fw_dump.boot_mem_regs_cnt = FADUMP_MAX_MEM_REGS; 3907dee93a9SHari Bathini return 0; 3917dee93a9SHari Bathini } 3927dee93a9SHari Bathini 3937dee93a9SHari Bathini pr_debug("Added boot memory range[%d] [%#016lx-%#016lx)\n", 3947dee93a9SHari Bathini i, rstart, (rstart + rsize)); 3957dee93a9SHari Bathini fw_dump.boot_mem_addr[i] = rstart; 3967dee93a9SHari Bathini fw_dump.boot_mem_sz[i] = rsize; 3977dee93a9SHari Bathini return 1; 3987dee93a9SHari Bathini } 3997dee93a9SHari Bathini 4007dee93a9SHari Bathini /* 4017dee93a9SHari Bathini * Firmware usually has a hard limit on the data it can copy per region. 4027dee93a9SHari Bathini * Honour that by splitting a memory range into multiple regions. 4037dee93a9SHari Bathini */ 4047dee93a9SHari Bathini static int __init add_boot_mem_regions(unsigned long mstart, 4057dee93a9SHari Bathini unsigned long msize) 4067dee93a9SHari Bathini { 4077dee93a9SHari Bathini unsigned long rstart, rsize, max_size; 4087dee93a9SHari Bathini int ret = 1; 4097dee93a9SHari Bathini 4107dee93a9SHari Bathini rstart = mstart; 4117dee93a9SHari Bathini max_size = fw_dump.max_copy_size ? fw_dump.max_copy_size : msize; 4127dee93a9SHari Bathini while (msize) { 4137dee93a9SHari Bathini if (msize > max_size) 4147dee93a9SHari Bathini rsize = max_size; 4157dee93a9SHari Bathini else 4167dee93a9SHari Bathini rsize = msize; 4177dee93a9SHari Bathini 4187dee93a9SHari Bathini ret = add_boot_mem_region(rstart, rsize); 4197dee93a9SHari Bathini if (!ret) 4207dee93a9SHari Bathini break; 4217dee93a9SHari Bathini 4227dee93a9SHari Bathini msize -= rsize; 4237dee93a9SHari Bathini rstart += rsize; 4247dee93a9SHari Bathini } 4257dee93a9SHari Bathini 4267dee93a9SHari Bathini return ret; 4277dee93a9SHari Bathini } 4287dee93a9SHari Bathini 4297dee93a9SHari Bathini static int __init fadump_get_boot_mem_regions(void) 4307dee93a9SHari Bathini { 431b10d6bcaSMike Rapoport unsigned long size, cur_size, hole_size, last_end; 4327dee93a9SHari Bathini unsigned long mem_size = fw_dump.boot_memory_size; 433b10d6bcaSMike Rapoport phys_addr_t reg_start, reg_end; 4347dee93a9SHari Bathini int ret = 1; 435b10d6bcaSMike Rapoport u64 i; 4367dee93a9SHari Bathini 4377dee93a9SHari Bathini fw_dump.boot_mem_regs_cnt = 0; 4387dee93a9SHari Bathini 4397dee93a9SHari Bathini last_end = 0; 4407dee93a9SHari Bathini hole_size = 0; 4417dee93a9SHari Bathini cur_size = 0; 442b10d6bcaSMike Rapoport for_each_mem_range(i, ®_start, ®_end) { 443b10d6bcaSMike Rapoport size = reg_end - reg_start; 444b10d6bcaSMike Rapoport hole_size += (reg_start - last_end); 4457dee93a9SHari Bathini 4467dee93a9SHari Bathini if ((cur_size + size) >= mem_size) { 4477dee93a9SHari Bathini size = (mem_size - cur_size); 448b10d6bcaSMike Rapoport ret = add_boot_mem_regions(reg_start, size); 4497dee93a9SHari Bathini break; 4507dee93a9SHari Bathini } 4517dee93a9SHari Bathini 4527dee93a9SHari Bathini mem_size -= size; 4537dee93a9SHari Bathini cur_size += size; 454b10d6bcaSMike Rapoport ret = add_boot_mem_regions(reg_start, size); 4557dee93a9SHari Bathini if (!ret) 4567dee93a9SHari Bathini break; 4577dee93a9SHari Bathini 458b10d6bcaSMike Rapoport last_end = reg_end; 4597dee93a9SHari Bathini } 4607dee93a9SHari Bathini fw_dump.boot_mem_top = PAGE_ALIGN(fw_dump.boot_memory_size + hole_size); 4617dee93a9SHari Bathini 4627dee93a9SHari Bathini return ret; 4637dee93a9SHari Bathini } 4647dee93a9SHari Bathini 465140777a3SHari Bathini /* 466140777a3SHari Bathini * Returns true, if the given range overlaps with reserved memory ranges 467140777a3SHari Bathini * starting at idx. Also, updates idx to index of overlapping memory range 468140777a3SHari Bathini * with the given memory range. 469140777a3SHari Bathini * False, otherwise. 470140777a3SHari Bathini */ 471d276960dSNick Child static bool __init overlaps_reserved_ranges(u64 base, u64 end, int *idx) 472140777a3SHari Bathini { 473140777a3SHari Bathini bool ret = false; 474140777a3SHari Bathini int i; 475140777a3SHari Bathini 476140777a3SHari Bathini for (i = *idx; i < reserved_mrange_info.mem_range_cnt; i++) { 477140777a3SHari Bathini u64 rbase = reserved_mrange_info.mem_ranges[i].base; 478140777a3SHari Bathini u64 rend = rbase + reserved_mrange_info.mem_ranges[i].size; 479140777a3SHari Bathini 480140777a3SHari Bathini if (end <= rbase) 481140777a3SHari Bathini break; 482140777a3SHari Bathini 483140777a3SHari Bathini if ((end > rbase) && (base < rend)) { 484140777a3SHari Bathini *idx = i; 485140777a3SHari Bathini ret = true; 486140777a3SHari Bathini break; 487140777a3SHari Bathini } 488140777a3SHari Bathini } 489140777a3SHari Bathini 490140777a3SHari Bathini return ret; 491140777a3SHari Bathini } 492140777a3SHari Bathini 493140777a3SHari Bathini /* 494140777a3SHari Bathini * Locate a suitable memory area to reserve memory for FADump. While at it, 495140777a3SHari Bathini * lookup reserved-ranges & avoid overlap with them, as they are used by F/W. 496140777a3SHari Bathini */ 497140777a3SHari Bathini static u64 __init fadump_locate_reserve_mem(u64 base, u64 size) 498140777a3SHari Bathini { 499140777a3SHari Bathini struct fadump_memory_range *mrngs; 500140777a3SHari Bathini phys_addr_t mstart, mend; 501140777a3SHari Bathini int idx = 0; 502140777a3SHari Bathini u64 i, ret = 0; 503140777a3SHari Bathini 504140777a3SHari Bathini mrngs = reserved_mrange_info.mem_ranges; 505140777a3SHari Bathini for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, 506140777a3SHari Bathini &mstart, &mend, NULL) { 507140777a3SHari Bathini pr_debug("%llu) mstart: %llx, mend: %llx, base: %llx\n", 508140777a3SHari Bathini i, mstart, mend, base); 509140777a3SHari Bathini 510140777a3SHari Bathini if (mstart > base) 511140777a3SHari Bathini base = PAGE_ALIGN(mstart); 512140777a3SHari Bathini 513140777a3SHari Bathini while ((mend > base) && ((mend - base) >= size)) { 514140777a3SHari Bathini if (!overlaps_reserved_ranges(base, base+size, &idx)) { 515140777a3SHari Bathini ret = base; 516140777a3SHari Bathini goto out; 517140777a3SHari Bathini } 518140777a3SHari Bathini 519140777a3SHari Bathini base = mrngs[idx].base + mrngs[idx].size; 520140777a3SHari Bathini base = PAGE_ALIGN(base); 521140777a3SHari Bathini } 522140777a3SHari Bathini } 523140777a3SHari Bathini 524140777a3SHari Bathini out: 525140777a3SHari Bathini return ret; 526140777a3SHari Bathini } 527140777a3SHari Bathini 528eb39c880SMahesh Salgaonkar int __init fadump_reserve_mem(void) 529eb39c880SMahesh Salgaonkar { 530140777a3SHari Bathini u64 base, size, mem_boundary, bootmem_min; 5316abec12cSHari Bathini int ret = 1; 532eb39c880SMahesh Salgaonkar 533eb39c880SMahesh Salgaonkar if (!fw_dump.fadump_enabled) 534eb39c880SMahesh Salgaonkar return 0; 535eb39c880SMahesh Salgaonkar 536eb39c880SMahesh Salgaonkar if (!fw_dump.fadump_supported) { 5376abec12cSHari Bathini pr_info("Firmware-Assisted Dump is not supported on this hardware\n"); 5386abec12cSHari Bathini goto error_out; 539eb39c880SMahesh Salgaonkar } 540742a265aSHari Bathini 5413ccc00a7SMahesh Salgaonkar /* 5423ccc00a7SMahesh Salgaonkar * Initialize boot memory size 5433ccc00a7SMahesh Salgaonkar * If dump is active then we have already calculated the size during 5443ccc00a7SMahesh Salgaonkar * first kernel. 5453ccc00a7SMahesh Salgaonkar */ 546f3512011SHari Bathini if (!fw_dump.dump_active) { 5476abec12cSHari Bathini fw_dump.boot_memory_size = 5486abec12cSHari Bathini PAGE_ALIGN(fadump_calculate_reserve_size()); 549a4e92ce8SMahesh Salgaonkar #ifdef CONFIG_CMA 550579ca1a2SHari Bathini if (!fw_dump.nocma) { 551a4e92ce8SMahesh Salgaonkar fw_dump.boot_memory_size = 552140777a3SHari Bathini ALIGN(fw_dump.boot_memory_size, 553e16faf26SDavid Hildenbrand CMA_MIN_ALIGNMENT_BYTES); 554579ca1a2SHari Bathini } 555a4e92ce8SMahesh Salgaonkar #endif 5567b1b3b48SHari Bathini 5577b1b3b48SHari Bathini bootmem_min = fw_dump.ops->fadump_get_bootmem_min(); 5587b1b3b48SHari Bathini if (fw_dump.boot_memory_size < bootmem_min) { 5597b1b3b48SHari Bathini pr_err("Can't enable fadump with boot memory size (0x%lx) less than 0x%llx\n", 5607b1b3b48SHari Bathini fw_dump.boot_memory_size, bootmem_min); 5617b1b3b48SHari Bathini goto error_out; 5627b1b3b48SHari Bathini } 5637dee93a9SHari Bathini 5647dee93a9SHari Bathini if (!fadump_get_boot_mem_regions()) { 5657dee93a9SHari Bathini pr_err("Too many holes in boot memory area to enable fadump\n"); 5667dee93a9SHari Bathini goto error_out; 5677dee93a9SHari Bathini } 568a4e92ce8SMahesh Salgaonkar } 569eb39c880SMahesh Salgaonkar 570eb39c880SMahesh Salgaonkar /* 571eb39c880SMahesh Salgaonkar * Calculate the memory boundary. 572eb39c880SMahesh Salgaonkar * If memory_limit is less than actual memory boundary then reserve 573eb39c880SMahesh Salgaonkar * the memory for fadump beyond the memory_limit and adjust the 574eb39c880SMahesh Salgaonkar * memory_limit accordingly, so that the running kernel can run with 575eb39c880SMahesh Salgaonkar * specified memory_limit. 576eb39c880SMahesh Salgaonkar */ 577eb39c880SMahesh Salgaonkar if (memory_limit && memory_limit < memblock_end_of_DRAM()) { 578eb39c880SMahesh Salgaonkar size = get_fadump_area_size(); 579eb39c880SMahesh Salgaonkar if ((memory_limit + size) < memblock_end_of_DRAM()) 580eb39c880SMahesh Salgaonkar memory_limit += size; 581eb39c880SMahesh Salgaonkar else 582eb39c880SMahesh Salgaonkar memory_limit = memblock_end_of_DRAM(); 583eb39c880SMahesh Salgaonkar printk(KERN_INFO "Adjusted memory_limit for firmware-assisted" 584a84fcd46SSuzuki Poulose " dump, now %#016llx\n", memory_limit); 585eb39c880SMahesh Salgaonkar } 586eb39c880SMahesh Salgaonkar if (memory_limit) 5876abec12cSHari Bathini mem_boundary = memory_limit; 588eb39c880SMahesh Salgaonkar else 5896abec12cSHari Bathini mem_boundary = memblock_end_of_DRAM(); 590eb39c880SMahesh Salgaonkar 5917dee93a9SHari Bathini base = fw_dump.boot_mem_top; 5928255da95SHari Bathini size = get_fadump_area_size(); 5938255da95SHari Bathini fw_dump.reserve_dump_area_size = size; 594eb39c880SMahesh Salgaonkar if (fw_dump.dump_active) { 595b71a693dSMahesh Salgaonkar pr_info("Firmware-assisted dump is active.\n"); 596b71a693dSMahesh Salgaonkar 59785975387SHari Bathini #ifdef CONFIG_HUGETLB_PAGE 59885975387SHari Bathini /* 59985975387SHari Bathini * FADump capture kernel doesn't care much about hugepages. 60085975387SHari Bathini * In fact, handling hugepages in capture kernel is asking for 60185975387SHari Bathini * trouble. So, disable HugeTLB support when fadump is active. 60285975387SHari Bathini */ 60385975387SHari Bathini hugetlb_disabled = true; 60485975387SHari Bathini #endif 605eb39c880SMahesh Salgaonkar /* 606eb39c880SMahesh Salgaonkar * If last boot has crashed then reserve all the memory 607b2a815a5SHari Bathini * above boot memory size so that we don't touch it until 608eb39c880SMahesh Salgaonkar * dump is written to disk by userspace tool. This memory 609b2a815a5SHari Bathini * can be released for general use by invalidating fadump. 610eb39c880SMahesh Salgaonkar */ 611b2a815a5SHari Bathini fadump_reserve_crash_area(base); 6122df173d9SMahesh Salgaonkar 613f3512011SHari Bathini pr_debug("fadumphdr_addr = %#016lx\n", fw_dump.fadumphdr_addr); 614f3512011SHari Bathini pr_debug("Reserve dump area start address: 0x%lx\n", 615f3512011SHari Bathini fw_dump.reserve_dump_area_start); 6168255da95SHari Bathini } else { 6178255da95SHari Bathini /* 618f6e6bedbSHari Bathini * Reserve memory at an offset closer to bottom of the RAM to 619579ca1a2SHari Bathini * minimize the impact of memory hot-remove operation. 620f6e6bedbSHari Bathini */ 621140777a3SHari Bathini base = fadump_locate_reserve_mem(base, size); 622f6e6bedbSHari Bathini 6239a2921e5SHari Bathini if (!base || (base + size > mem_boundary)) { 624742a265aSHari Bathini pr_err("Failed to find memory chunk for reservation!\n"); 625742a265aSHari Bathini goto error_out; 626742a265aSHari Bathini } 627742a265aSHari Bathini fw_dump.reserve_dump_area_start = base; 628742a265aSHari Bathini 629742a265aSHari Bathini /* 630742a265aSHari Bathini * Calculate the kernel metadata address and register it with 631742a265aSHari Bathini * f/w if the platform supports. 632742a265aSHari Bathini */ 633742a265aSHari Bathini if (fw_dump.ops->fadump_setup_metadata && 634742a265aSHari Bathini (fw_dump.ops->fadump_setup_metadata(&fw_dump) < 0)) 635742a265aSHari Bathini goto error_out; 636742a265aSHari Bathini 637742a265aSHari Bathini if (memblock_reserve(base, size)) { 6386abec12cSHari Bathini pr_err("Failed to reserve memory!\n"); 6396abec12cSHari Bathini goto error_out; 6406abec12cSHari Bathini } 6416abec12cSHari Bathini 6426abec12cSHari Bathini pr_info("Reserved %lldMB of memory at %#016llx (System RAM: %lldMB)\n", 6436abec12cSHari Bathini (size >> 20), base, (memblock_phys_mem_size() >> 20)); 644f6e6bedbSHari Bathini 6456abec12cSHari Bathini ret = fadump_cma_init(); 646a4e92ce8SMahesh Salgaonkar } 6476abec12cSHari Bathini 6486abec12cSHari Bathini return ret; 6496abec12cSHari Bathini error_out: 6506abec12cSHari Bathini fw_dump.fadump_enabled = 0; 6516abec12cSHari Bathini return 0; 652eb39c880SMahesh Salgaonkar } 653eb39c880SMahesh Salgaonkar 654eb39c880SMahesh Salgaonkar /* Look for fadump= cmdline option. */ 655eb39c880SMahesh Salgaonkar static int __init early_fadump_param(char *p) 656eb39c880SMahesh Salgaonkar { 657eb39c880SMahesh Salgaonkar if (!p) 658eb39c880SMahesh Salgaonkar return 1; 659eb39c880SMahesh Salgaonkar 660eb39c880SMahesh Salgaonkar if (strncmp(p, "on", 2) == 0) 661eb39c880SMahesh Salgaonkar fw_dump.fadump_enabled = 1; 662eb39c880SMahesh Salgaonkar else if (strncmp(p, "off", 3) == 0) 663eb39c880SMahesh Salgaonkar fw_dump.fadump_enabled = 0; 664a4e92ce8SMahesh Salgaonkar else if (strncmp(p, "nocma", 5) == 0) { 665a4e92ce8SMahesh Salgaonkar fw_dump.fadump_enabled = 1; 666a4e92ce8SMahesh Salgaonkar fw_dump.nocma = 1; 667a4e92ce8SMahesh Salgaonkar } 668eb39c880SMahesh Salgaonkar 669eb39c880SMahesh Salgaonkar return 0; 670eb39c880SMahesh Salgaonkar } 671eb39c880SMahesh Salgaonkar early_param("fadump", early_fadump_param); 672eb39c880SMahesh Salgaonkar 67381d9eca5SHari Bathini /* 67481d9eca5SHari Bathini * Look for fadump_reserve_mem= cmdline option 67581d9eca5SHari Bathini * TODO: Remove references to 'fadump_reserve_mem=' parameter, 67681d9eca5SHari Bathini * the sooner 'crashkernel=' parameter is accustomed to. 67781d9eca5SHari Bathini */ 67881d9eca5SHari Bathini static int __init early_fadump_reserve_mem(char *p) 67981d9eca5SHari Bathini { 68081d9eca5SHari Bathini if (p) 68181d9eca5SHari Bathini fw_dump.reserve_bootvar = memparse(p, &p); 68281d9eca5SHari Bathini return 0; 68381d9eca5SHari Bathini } 68481d9eca5SHari Bathini early_param("fadump_reserve_mem", early_fadump_reserve_mem); 68581d9eca5SHari Bathini 686ebaeb5aeSMahesh Salgaonkar void crash_fadump(struct pt_regs *regs, const char *str) 687ebaeb5aeSMahesh Salgaonkar { 688ba608c4fSSourabh Jain unsigned int msecs; 689ebaeb5aeSMahesh Salgaonkar struct fadump_crash_info_header *fdh = NULL; 690f2a5e8f0SMahesh Salgaonkar int old_cpu, this_cpu; 691ba608c4fSSourabh Jain /* Do not include first CPU */ 692ba608c4fSSourabh Jain unsigned int ncpus = num_online_cpus() - 1; 693ebaeb5aeSMahesh Salgaonkar 6946fcd6baaSNicholas Piggin if (!should_fadump_crash()) 695ebaeb5aeSMahesh Salgaonkar return; 696ebaeb5aeSMahesh Salgaonkar 697f2a5e8f0SMahesh Salgaonkar /* 698f2a5e8f0SMahesh Salgaonkar * old_cpu == -1 means this is the first CPU which has come here, 699f2a5e8f0SMahesh Salgaonkar * go ahead and trigger fadump. 700f2a5e8f0SMahesh Salgaonkar * 701f2a5e8f0SMahesh Salgaonkar * old_cpu != -1 means some other CPU has already on it's way 702f2a5e8f0SMahesh Salgaonkar * to trigger fadump, just keep looping here. 703f2a5e8f0SMahesh Salgaonkar */ 704f2a5e8f0SMahesh Salgaonkar this_cpu = smp_processor_id(); 705f2a5e8f0SMahesh Salgaonkar old_cpu = cmpxchg(&crashing_cpu, -1, this_cpu); 706f2a5e8f0SMahesh Salgaonkar 707f2a5e8f0SMahesh Salgaonkar if (old_cpu != -1) { 708ba608c4fSSourabh Jain atomic_inc(&cpus_in_fadump); 709ba608c4fSSourabh Jain 710f2a5e8f0SMahesh Salgaonkar /* 711f2a5e8f0SMahesh Salgaonkar * We can't loop here indefinitely. Wait as long as fadump 712f2a5e8f0SMahesh Salgaonkar * is in force. If we race with fadump un-registration this 713f2a5e8f0SMahesh Salgaonkar * loop will break and then we go down to normal panic path 714f2a5e8f0SMahesh Salgaonkar * and reboot. If fadump is in force the first crashing 715f2a5e8f0SMahesh Salgaonkar * cpu will definitely trigger fadump. 716f2a5e8f0SMahesh Salgaonkar */ 717f2a5e8f0SMahesh Salgaonkar while (fw_dump.dump_registered) 718f2a5e8f0SMahesh Salgaonkar cpu_relax(); 719f2a5e8f0SMahesh Salgaonkar return; 720f2a5e8f0SMahesh Salgaonkar } 721f2a5e8f0SMahesh Salgaonkar 722ebaeb5aeSMahesh Salgaonkar fdh = __va(fw_dump.fadumphdr_addr); 723ebaeb5aeSMahesh Salgaonkar fdh->crashing_cpu = crashing_cpu; 724ebaeb5aeSMahesh Salgaonkar crash_save_vmcoreinfo(); 725ebaeb5aeSMahesh Salgaonkar 726ebaeb5aeSMahesh Salgaonkar if (regs) 727ebaeb5aeSMahesh Salgaonkar fdh->regs = *regs; 728ebaeb5aeSMahesh Salgaonkar else 729ebaeb5aeSMahesh Salgaonkar ppc_save_regs(&fdh->regs); 730ebaeb5aeSMahesh Salgaonkar 731a0512164SRasmus Villemoes fdh->online_mask = *cpu_online_mask; 732ebaeb5aeSMahesh Salgaonkar 733ba608c4fSSourabh Jain /* 734ba608c4fSSourabh Jain * If we came in via system reset, wait a while for the secondary 735ba608c4fSSourabh Jain * CPUs to enter. 736ba608c4fSSourabh Jain */ 7377153d4bfSXiongwei Song if (TRAP(&(fdh->regs)) == INTERRUPT_SYSTEM_RESET) { 738ba608c4fSSourabh Jain msecs = CRASH_TIMEOUT; 739ba608c4fSSourabh Jain while ((atomic_read(&cpus_in_fadump) < ncpus) && (--msecs > 0)) 740ba608c4fSSourabh Jain mdelay(1); 741ba608c4fSSourabh Jain } 742ba608c4fSSourabh Jain 74341a65d16SHari Bathini fw_dump.ops->fadump_trigger(fdh, str); 744ebaeb5aeSMahesh Salgaonkar } 745ebaeb5aeSMahesh Salgaonkar 746d276960dSNick Child u32 *__init fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs) 747ebaeb5aeSMahesh Salgaonkar { 748ebaeb5aeSMahesh Salgaonkar struct elf_prstatus prstatus; 749ebaeb5aeSMahesh Salgaonkar 750ebaeb5aeSMahesh Salgaonkar memset(&prstatus, 0, sizeof(prstatus)); 751ebaeb5aeSMahesh Salgaonkar /* 752ebaeb5aeSMahesh Salgaonkar * FIXME: How do i get PID? Do I really need it? 753ebaeb5aeSMahesh Salgaonkar * prstatus.pr_pid = ???? 754ebaeb5aeSMahesh Salgaonkar */ 755ebaeb5aeSMahesh Salgaonkar elf_core_copy_kernel_regs(&prstatus.pr_reg, regs); 75622bd0177SHari Bathini buf = append_elf_note(buf, CRASH_CORE_NOTE_NAME, NT_PRSTATUS, 757ebaeb5aeSMahesh Salgaonkar &prstatus, sizeof(prstatus)); 758ebaeb5aeSMahesh Salgaonkar return buf; 759ebaeb5aeSMahesh Salgaonkar } 760ebaeb5aeSMahesh Salgaonkar 761d276960dSNick Child void __init fadump_update_elfcore_header(char *bufp) 762ebaeb5aeSMahesh Salgaonkar { 763ebaeb5aeSMahesh Salgaonkar struct elf_phdr *phdr; 764ebaeb5aeSMahesh Salgaonkar 765ebaeb5aeSMahesh Salgaonkar bufp += sizeof(struct elfhdr); 766ebaeb5aeSMahesh Salgaonkar 767ebaeb5aeSMahesh Salgaonkar /* First note is a place holder for cpu notes info. */ 768ebaeb5aeSMahesh Salgaonkar phdr = (struct elf_phdr *)bufp; 769ebaeb5aeSMahesh Salgaonkar 770ebaeb5aeSMahesh Salgaonkar if (phdr->p_type == PT_NOTE) { 771961cf26aSHari Bathini phdr->p_paddr = __pa(fw_dump.cpu_notes_buf_vaddr); 772ebaeb5aeSMahesh Salgaonkar phdr->p_offset = phdr->p_paddr; 773ebaeb5aeSMahesh Salgaonkar phdr->p_filesz = fw_dump.cpu_notes_buf_size; 774ebaeb5aeSMahesh Salgaonkar phdr->p_memsz = fw_dump.cpu_notes_buf_size; 775ebaeb5aeSMahesh Salgaonkar } 776ebaeb5aeSMahesh Salgaonkar return; 777ebaeb5aeSMahesh Salgaonkar } 778ebaeb5aeSMahesh Salgaonkar 779d276960dSNick Child static void *__init fadump_alloc_buffer(unsigned long size) 780ebaeb5aeSMahesh Salgaonkar { 78172aa6517SHari Bathini unsigned long count, i; 782ebaeb5aeSMahesh Salgaonkar struct page *page; 78372aa6517SHari Bathini void *vaddr; 784ebaeb5aeSMahesh Salgaonkar 78572aa6517SHari Bathini vaddr = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO); 786ebaeb5aeSMahesh Salgaonkar if (!vaddr) 787ebaeb5aeSMahesh Salgaonkar return NULL; 788ebaeb5aeSMahesh Salgaonkar 78972aa6517SHari Bathini count = PAGE_ALIGN(size) / PAGE_SIZE; 790ebaeb5aeSMahesh Salgaonkar page = virt_to_page(vaddr); 791ebaeb5aeSMahesh Salgaonkar for (i = 0; i < count; i++) 79272aa6517SHari Bathini mark_page_reserved(page + i); 793ebaeb5aeSMahesh Salgaonkar return vaddr; 794ebaeb5aeSMahesh Salgaonkar } 795ebaeb5aeSMahesh Salgaonkar 796961cf26aSHari Bathini static void fadump_free_buffer(unsigned long vaddr, unsigned long size) 797ebaeb5aeSMahesh Salgaonkar { 79872aa6517SHari Bathini free_reserved_area((void *)vaddr, (void *)(vaddr + size), -1, NULL); 799ebaeb5aeSMahesh Salgaonkar } 800ebaeb5aeSMahesh Salgaonkar 801d276960dSNick Child s32 __init fadump_setup_cpu_notes_buf(u32 num_cpus) 802961cf26aSHari Bathini { 803961cf26aSHari Bathini /* Allocate buffer to hold cpu crash notes. */ 804961cf26aSHari Bathini fw_dump.cpu_notes_buf_size = num_cpus * sizeof(note_buf_t); 805961cf26aSHari Bathini fw_dump.cpu_notes_buf_size = PAGE_ALIGN(fw_dump.cpu_notes_buf_size); 806961cf26aSHari Bathini fw_dump.cpu_notes_buf_vaddr = 807961cf26aSHari Bathini (unsigned long)fadump_alloc_buffer(fw_dump.cpu_notes_buf_size); 808961cf26aSHari Bathini if (!fw_dump.cpu_notes_buf_vaddr) { 809961cf26aSHari Bathini pr_err("Failed to allocate %ld bytes for CPU notes buffer\n", 810961cf26aSHari Bathini fw_dump.cpu_notes_buf_size); 811961cf26aSHari Bathini return -ENOMEM; 812961cf26aSHari Bathini } 813961cf26aSHari Bathini 814961cf26aSHari Bathini pr_debug("Allocated buffer for cpu notes of size %ld at 0x%lx\n", 815961cf26aSHari Bathini fw_dump.cpu_notes_buf_size, 816961cf26aSHari Bathini fw_dump.cpu_notes_buf_vaddr); 817961cf26aSHari Bathini return 0; 818961cf26aSHari Bathini } 819961cf26aSHari Bathini 8207f0ad11dSHari Bathini void fadump_free_cpu_notes_buf(void) 821961cf26aSHari Bathini { 822961cf26aSHari Bathini if (!fw_dump.cpu_notes_buf_vaddr) 823961cf26aSHari Bathini return; 824961cf26aSHari Bathini 825961cf26aSHari Bathini fadump_free_buffer(fw_dump.cpu_notes_buf_vaddr, 826961cf26aSHari Bathini fw_dump.cpu_notes_buf_size); 827961cf26aSHari Bathini fw_dump.cpu_notes_buf_vaddr = 0; 828961cf26aSHari Bathini fw_dump.cpu_notes_buf_size = 0; 829961cf26aSHari Bathini } 830961cf26aSHari Bathini 831e4fc48fbSHari Bathini static void fadump_free_mem_ranges(struct fadump_mrange_info *mrange_info) 8321bd6a1c4SHari Bathini { 83302c04e37SHari Bathini if (mrange_info->is_static) { 83402c04e37SHari Bathini mrange_info->mem_range_cnt = 0; 83502c04e37SHari Bathini return; 83602c04e37SHari Bathini } 83702c04e37SHari Bathini 838e4fc48fbSHari Bathini kfree(mrange_info->mem_ranges); 83902c04e37SHari Bathini memset((void *)((u64)mrange_info + RNG_NAME_SZ), 0, 84002c04e37SHari Bathini (sizeof(struct fadump_mrange_info) - RNG_NAME_SZ)); 8411bd6a1c4SHari Bathini } 8421bd6a1c4SHari Bathini 8431bd6a1c4SHari Bathini /* 844e4fc48fbSHari Bathini * Allocate or reallocate mem_ranges array in incremental units 8451bd6a1c4SHari Bathini * of PAGE_SIZE. 8461bd6a1c4SHari Bathini */ 847e4fc48fbSHari Bathini static int fadump_alloc_mem_ranges(struct fadump_mrange_info *mrange_info) 8481bd6a1c4SHari Bathini { 849e4fc48fbSHari Bathini struct fadump_memory_range *new_array; 8501bd6a1c4SHari Bathini u64 new_size; 8511bd6a1c4SHari Bathini 852e4fc48fbSHari Bathini new_size = mrange_info->mem_ranges_sz + PAGE_SIZE; 853e4fc48fbSHari Bathini pr_debug("Allocating %llu bytes of memory for %s memory ranges\n", 854e4fc48fbSHari Bathini new_size, mrange_info->name); 8551bd6a1c4SHari Bathini 856e4fc48fbSHari Bathini new_array = krealloc(mrange_info->mem_ranges, new_size, GFP_KERNEL); 8571bd6a1c4SHari Bathini if (new_array == NULL) { 858e4fc48fbSHari Bathini pr_err("Insufficient memory for setting up %s memory ranges\n", 859e4fc48fbSHari Bathini mrange_info->name); 860e4fc48fbSHari Bathini fadump_free_mem_ranges(mrange_info); 8611bd6a1c4SHari Bathini return -ENOMEM; 8621bd6a1c4SHari Bathini } 8631bd6a1c4SHari Bathini 864e4fc48fbSHari Bathini mrange_info->mem_ranges = new_array; 865e4fc48fbSHari Bathini mrange_info->mem_ranges_sz = new_size; 866e4fc48fbSHari Bathini mrange_info->max_mem_ranges = (new_size / 867e4fc48fbSHari Bathini sizeof(struct fadump_memory_range)); 8681bd6a1c4SHari Bathini return 0; 8691bd6a1c4SHari Bathini } 8701bd6a1c4SHari Bathini 871e4fc48fbSHari Bathini static inline int fadump_add_mem_range(struct fadump_mrange_info *mrange_info, 872e4fc48fbSHari Bathini u64 base, u64 end) 8732df173d9SMahesh Salgaonkar { 874e4fc48fbSHari Bathini struct fadump_memory_range *mem_ranges = mrange_info->mem_ranges; 875ced1bf52SHari Bathini bool is_adjacent = false; 876e4fc48fbSHari Bathini u64 start, size; 877ced1bf52SHari Bathini 8782df173d9SMahesh Salgaonkar if (base == end) 8791bd6a1c4SHari Bathini return 0; 8801bd6a1c4SHari Bathini 881ced1bf52SHari Bathini /* 882ced1bf52SHari Bathini * Fold adjacent memory ranges to bring down the memory ranges/ 883ced1bf52SHari Bathini * PT_LOAD segments count. 884ced1bf52SHari Bathini */ 885e4fc48fbSHari Bathini if (mrange_info->mem_range_cnt) { 886e4fc48fbSHari Bathini start = mem_ranges[mrange_info->mem_range_cnt - 1].base; 887e4fc48fbSHari Bathini size = mem_ranges[mrange_info->mem_range_cnt - 1].size; 888ced1bf52SHari Bathini 889ced1bf52SHari Bathini if ((start + size) == base) 890ced1bf52SHari Bathini is_adjacent = true; 891ced1bf52SHari Bathini } 892ced1bf52SHari Bathini if (!is_adjacent) { 893ced1bf52SHari Bathini /* resize the array on reaching the limit */ 894e4fc48fbSHari Bathini if (mrange_info->mem_range_cnt == mrange_info->max_mem_ranges) { 8951bd6a1c4SHari Bathini int ret; 8961bd6a1c4SHari Bathini 89702c04e37SHari Bathini if (mrange_info->is_static) { 89802c04e37SHari Bathini pr_err("Reached array size limit for %s memory ranges\n", 89902c04e37SHari Bathini mrange_info->name); 90002c04e37SHari Bathini return -ENOSPC; 90102c04e37SHari Bathini } 90202c04e37SHari Bathini 903e4fc48fbSHari Bathini ret = fadump_alloc_mem_ranges(mrange_info); 9041bd6a1c4SHari Bathini if (ret) 9051bd6a1c4SHari Bathini return ret; 906e4fc48fbSHari Bathini 907e4fc48fbSHari Bathini /* Update to the new resized array */ 908e4fc48fbSHari Bathini mem_ranges = mrange_info->mem_ranges; 9091bd6a1c4SHari Bathini } 9102df173d9SMahesh Salgaonkar 911ced1bf52SHari Bathini start = base; 912e4fc48fbSHari Bathini mem_ranges[mrange_info->mem_range_cnt].base = start; 913e4fc48fbSHari Bathini mrange_info->mem_range_cnt++; 914ced1bf52SHari Bathini } 915ced1bf52SHari Bathini 916e4fc48fbSHari Bathini mem_ranges[mrange_info->mem_range_cnt - 1].size = (end - start); 917e4fc48fbSHari Bathini pr_debug("%s_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n", 918e4fc48fbSHari Bathini mrange_info->name, (mrange_info->mem_range_cnt - 1), 919e4fc48fbSHari Bathini start, end - 1, (end - start)); 9201bd6a1c4SHari Bathini return 0; 9212df173d9SMahesh Salgaonkar } 9222df173d9SMahesh Salgaonkar 923e4fc48fbSHari Bathini static int fadump_exclude_reserved_area(u64 start, u64 end) 9242df173d9SMahesh Salgaonkar { 925e4fc48fbSHari Bathini u64 ra_start, ra_end; 9261bd6a1c4SHari Bathini int ret = 0; 9272df173d9SMahesh Salgaonkar 9282df173d9SMahesh Salgaonkar ra_start = fw_dump.reserve_dump_area_start; 9292df173d9SMahesh Salgaonkar ra_end = ra_start + fw_dump.reserve_dump_area_size; 9302df173d9SMahesh Salgaonkar 9312df173d9SMahesh Salgaonkar if ((ra_start < end) && (ra_end > start)) { 9322df173d9SMahesh Salgaonkar if ((start < ra_start) && (end > ra_end)) { 933e4fc48fbSHari Bathini ret = fadump_add_mem_range(&crash_mrange_info, 934e4fc48fbSHari Bathini start, ra_start); 9351bd6a1c4SHari Bathini if (ret) 9361bd6a1c4SHari Bathini return ret; 9371bd6a1c4SHari Bathini 938e4fc48fbSHari Bathini ret = fadump_add_mem_range(&crash_mrange_info, 939e4fc48fbSHari Bathini ra_end, end); 9402df173d9SMahesh Salgaonkar } else if (start < ra_start) { 941e4fc48fbSHari Bathini ret = fadump_add_mem_range(&crash_mrange_info, 942e4fc48fbSHari Bathini start, ra_start); 9432df173d9SMahesh Salgaonkar } else if (ra_end < end) { 944e4fc48fbSHari Bathini ret = fadump_add_mem_range(&crash_mrange_info, 945e4fc48fbSHari Bathini ra_end, end); 9462df173d9SMahesh Salgaonkar } 9472df173d9SMahesh Salgaonkar } else 948e4fc48fbSHari Bathini ret = fadump_add_mem_range(&crash_mrange_info, start, end); 9491bd6a1c4SHari Bathini 9501bd6a1c4SHari Bathini return ret; 9512df173d9SMahesh Salgaonkar } 9522df173d9SMahesh Salgaonkar 9532df173d9SMahesh Salgaonkar static int fadump_init_elfcore_header(char *bufp) 9542df173d9SMahesh Salgaonkar { 9552df173d9SMahesh Salgaonkar struct elfhdr *elf; 9562df173d9SMahesh Salgaonkar 9572df173d9SMahesh Salgaonkar elf = (struct elfhdr *) bufp; 9582df173d9SMahesh Salgaonkar bufp += sizeof(struct elfhdr); 9592df173d9SMahesh Salgaonkar memcpy(elf->e_ident, ELFMAG, SELFMAG); 9602df173d9SMahesh Salgaonkar elf->e_ident[EI_CLASS] = ELF_CLASS; 9612df173d9SMahesh Salgaonkar elf->e_ident[EI_DATA] = ELF_DATA; 9622df173d9SMahesh Salgaonkar elf->e_ident[EI_VERSION] = EV_CURRENT; 9632df173d9SMahesh Salgaonkar elf->e_ident[EI_OSABI] = ELF_OSABI; 9642df173d9SMahesh Salgaonkar memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD); 9652df173d9SMahesh Salgaonkar elf->e_type = ET_CORE; 9662df173d9SMahesh Salgaonkar elf->e_machine = ELF_ARCH; 9672df173d9SMahesh Salgaonkar elf->e_version = EV_CURRENT; 9682df173d9SMahesh Salgaonkar elf->e_entry = 0; 9692df173d9SMahesh Salgaonkar elf->e_phoff = sizeof(struct elfhdr); 9702df173d9SMahesh Salgaonkar elf->e_shoff = 0; 971d8bced27SDaniel Axtens #if defined(_CALL_ELF) 972d8bced27SDaniel Axtens elf->e_flags = _CALL_ELF; 973d8bced27SDaniel Axtens #else 974d8bced27SDaniel Axtens elf->e_flags = 0; 975d8bced27SDaniel Axtens #endif 9762df173d9SMahesh Salgaonkar elf->e_ehsize = sizeof(struct elfhdr); 9772df173d9SMahesh Salgaonkar elf->e_phentsize = sizeof(struct elf_phdr); 9782df173d9SMahesh Salgaonkar elf->e_phnum = 0; 9792df173d9SMahesh Salgaonkar elf->e_shentsize = 0; 9802df173d9SMahesh Salgaonkar elf->e_shnum = 0; 9812df173d9SMahesh Salgaonkar elf->e_shstrndx = 0; 9822df173d9SMahesh Salgaonkar 9832df173d9SMahesh Salgaonkar return 0; 9842df173d9SMahesh Salgaonkar } 9852df173d9SMahesh Salgaonkar 9862df173d9SMahesh Salgaonkar /* 9872df173d9SMahesh Salgaonkar * Traverse through memblock structure and setup crash memory ranges. These 9882df173d9SMahesh Salgaonkar * ranges will be used create PT_LOAD program headers in elfcore header. 9892df173d9SMahesh Salgaonkar */ 9901bd6a1c4SHari Bathini static int fadump_setup_crash_memory_ranges(void) 9912df173d9SMahesh Salgaonkar { 992b10d6bcaSMike Rapoport u64 i, start, end; 993b10d6bcaSMike Rapoport int ret; 9942df173d9SMahesh Salgaonkar 9952df173d9SMahesh Salgaonkar pr_debug("Setup crash memory ranges.\n"); 996e4fc48fbSHari Bathini crash_mrange_info.mem_range_cnt = 0; 997ced1bf52SHari Bathini 9982df173d9SMahesh Salgaonkar /* 9997dee93a9SHari Bathini * Boot memory region(s) registered with firmware are moved to 10007dee93a9SHari Bathini * different location at the time of crash. Create separate program 10017dee93a9SHari Bathini * header(s) for this memory chunk(s) with the correct offset. 10022df173d9SMahesh Salgaonkar */ 10037dee93a9SHari Bathini for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) { 10047dee93a9SHari Bathini start = fw_dump.boot_mem_addr[i]; 10057dee93a9SHari Bathini end = start + fw_dump.boot_mem_sz[i]; 10067dee93a9SHari Bathini ret = fadump_add_mem_range(&crash_mrange_info, start, end); 10071bd6a1c4SHari Bathini if (ret) 10081bd6a1c4SHari Bathini return ret; 10097dee93a9SHari Bathini } 10102df173d9SMahesh Salgaonkar 1011b10d6bcaSMike Rapoport for_each_mem_range(i, &start, &end) { 1012a77af552SHari Bathini /* 10137dee93a9SHari Bathini * skip the memory chunk that is already added 10147dee93a9SHari Bathini * (0 through boot_memory_top). 1015a77af552SHari Bathini */ 10167dee93a9SHari Bathini if (start < fw_dump.boot_mem_top) { 10177dee93a9SHari Bathini if (end > fw_dump.boot_mem_top) 10187dee93a9SHari Bathini start = fw_dump.boot_mem_top; 1019a77af552SHari Bathini else 1020a77af552SHari Bathini continue; 1021a77af552SHari Bathini } 10222df173d9SMahesh Salgaonkar 10232df173d9SMahesh Salgaonkar /* add this range excluding the reserved dump area. */ 10241bd6a1c4SHari Bathini ret = fadump_exclude_reserved_area(start, end); 10251bd6a1c4SHari Bathini if (ret) 10261bd6a1c4SHari Bathini return ret; 10272df173d9SMahesh Salgaonkar } 10281bd6a1c4SHari Bathini 10291bd6a1c4SHari Bathini return 0; 10302df173d9SMahesh Salgaonkar } 10312df173d9SMahesh Salgaonkar 1032d34c5f26SMahesh Salgaonkar /* 1033d34c5f26SMahesh Salgaonkar * If the given physical address falls within the boot memory region then 1034d34c5f26SMahesh Salgaonkar * return the relocated address that points to the dump region reserved 1035d34c5f26SMahesh Salgaonkar * for saving initial boot memory contents. 1036d34c5f26SMahesh Salgaonkar */ 1037d34c5f26SMahesh Salgaonkar static inline unsigned long fadump_relocate(unsigned long paddr) 1038d34c5f26SMahesh Salgaonkar { 10397dee93a9SHari Bathini unsigned long raddr, rstart, rend, rlast, hole_size; 10407dee93a9SHari Bathini int i; 10417dee93a9SHari Bathini 10427dee93a9SHari Bathini hole_size = 0; 10437dee93a9SHari Bathini rlast = 0; 10447dee93a9SHari Bathini raddr = paddr; 10457dee93a9SHari Bathini for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) { 10467dee93a9SHari Bathini rstart = fw_dump.boot_mem_addr[i]; 10477dee93a9SHari Bathini rend = rstart + fw_dump.boot_mem_sz[i]; 10487dee93a9SHari Bathini hole_size += (rstart - rlast); 10497dee93a9SHari Bathini 10507dee93a9SHari Bathini if (paddr >= rstart && paddr < rend) { 10517dee93a9SHari Bathini raddr += fw_dump.boot_mem_dest_addr - hole_size; 10527dee93a9SHari Bathini break; 10537dee93a9SHari Bathini } 10547dee93a9SHari Bathini 10557dee93a9SHari Bathini rlast = rend; 10567dee93a9SHari Bathini } 10577dee93a9SHari Bathini 10587dee93a9SHari Bathini pr_debug("vmcoreinfo: paddr = 0x%lx, raddr = 0x%lx\n", paddr, raddr); 10597dee93a9SHari Bathini return raddr; 1060d34c5f26SMahesh Salgaonkar } 1061d34c5f26SMahesh Salgaonkar 10622df173d9SMahesh Salgaonkar static int fadump_create_elfcore_headers(char *bufp) 10632df173d9SMahesh Salgaonkar { 10647dee93a9SHari Bathini unsigned long long raddr, offset; 10652df173d9SMahesh Salgaonkar struct elf_phdr *phdr; 10667dee93a9SHari Bathini struct elfhdr *elf; 10677dee93a9SHari Bathini int i, j; 10682df173d9SMahesh Salgaonkar 10692df173d9SMahesh Salgaonkar fadump_init_elfcore_header(bufp); 10702df173d9SMahesh Salgaonkar elf = (struct elfhdr *)bufp; 10712df173d9SMahesh Salgaonkar bufp += sizeof(struct elfhdr); 10722df173d9SMahesh Salgaonkar 1073ebaeb5aeSMahesh Salgaonkar /* 1074ebaeb5aeSMahesh Salgaonkar * setup ELF PT_NOTE, place holder for cpu notes info. The notes info 1075ebaeb5aeSMahesh Salgaonkar * will be populated during second kernel boot after crash. Hence 1076ebaeb5aeSMahesh Salgaonkar * this PT_NOTE will always be the first elf note. 1077ebaeb5aeSMahesh Salgaonkar * 1078ebaeb5aeSMahesh Salgaonkar * NOTE: Any new ELF note addition should be placed after this note. 1079ebaeb5aeSMahesh Salgaonkar */ 1080ebaeb5aeSMahesh Salgaonkar phdr = (struct elf_phdr *)bufp; 1081ebaeb5aeSMahesh Salgaonkar bufp += sizeof(struct elf_phdr); 1082ebaeb5aeSMahesh Salgaonkar phdr->p_type = PT_NOTE; 1083ebaeb5aeSMahesh Salgaonkar phdr->p_flags = 0; 1084ebaeb5aeSMahesh Salgaonkar phdr->p_vaddr = 0; 1085ebaeb5aeSMahesh Salgaonkar phdr->p_align = 0; 1086ebaeb5aeSMahesh Salgaonkar 1087ebaeb5aeSMahesh Salgaonkar phdr->p_offset = 0; 1088ebaeb5aeSMahesh Salgaonkar phdr->p_paddr = 0; 1089ebaeb5aeSMahesh Salgaonkar phdr->p_filesz = 0; 1090ebaeb5aeSMahesh Salgaonkar phdr->p_memsz = 0; 1091ebaeb5aeSMahesh Salgaonkar 1092ebaeb5aeSMahesh Salgaonkar (elf->e_phnum)++; 1093ebaeb5aeSMahesh Salgaonkar 1094d34c5f26SMahesh Salgaonkar /* setup ELF PT_NOTE for vmcoreinfo */ 1095d34c5f26SMahesh Salgaonkar phdr = (struct elf_phdr *)bufp; 1096d34c5f26SMahesh Salgaonkar bufp += sizeof(struct elf_phdr); 1097d34c5f26SMahesh Salgaonkar phdr->p_type = PT_NOTE; 1098d34c5f26SMahesh Salgaonkar phdr->p_flags = 0; 1099d34c5f26SMahesh Salgaonkar phdr->p_vaddr = 0; 1100d34c5f26SMahesh Salgaonkar phdr->p_align = 0; 1101d34c5f26SMahesh Salgaonkar 1102d34c5f26SMahesh Salgaonkar phdr->p_paddr = fadump_relocate(paddr_vmcoreinfo_note()); 1103d34c5f26SMahesh Salgaonkar phdr->p_offset = phdr->p_paddr; 11045203f499SXunlei Pang phdr->p_memsz = phdr->p_filesz = VMCOREINFO_NOTE_SIZE; 1105d34c5f26SMahesh Salgaonkar 1106d34c5f26SMahesh Salgaonkar /* Increment number of program headers. */ 1107d34c5f26SMahesh Salgaonkar (elf->e_phnum)++; 1108d34c5f26SMahesh Salgaonkar 11092df173d9SMahesh Salgaonkar /* setup PT_LOAD sections. */ 11107dee93a9SHari Bathini j = 0; 11117dee93a9SHari Bathini offset = 0; 11127dee93a9SHari Bathini raddr = fw_dump.boot_mem_addr[0]; 1113e4fc48fbSHari Bathini for (i = 0; i < crash_mrange_info.mem_range_cnt; i++) { 1114e4fc48fbSHari Bathini u64 mbase, msize; 11152df173d9SMahesh Salgaonkar 1116e4fc48fbSHari Bathini mbase = crash_mrange_info.mem_ranges[i].base; 1117e4fc48fbSHari Bathini msize = crash_mrange_info.mem_ranges[i].size; 11182df173d9SMahesh Salgaonkar if (!msize) 11192df173d9SMahesh Salgaonkar continue; 11202df173d9SMahesh Salgaonkar 11212df173d9SMahesh Salgaonkar phdr = (struct elf_phdr *)bufp; 11222df173d9SMahesh Salgaonkar bufp += sizeof(struct elf_phdr); 11232df173d9SMahesh Salgaonkar phdr->p_type = PT_LOAD; 11242df173d9SMahesh Salgaonkar phdr->p_flags = PF_R|PF_W|PF_X; 11252df173d9SMahesh Salgaonkar phdr->p_offset = mbase; 11262df173d9SMahesh Salgaonkar 11277dee93a9SHari Bathini if (mbase == raddr) { 11282df173d9SMahesh Salgaonkar /* 1129becd91d9SHari Bathini * The entire real memory region will be moved by 1130becd91d9SHari Bathini * firmware to the specified destination_address. 1131becd91d9SHari Bathini * Hence set the correct offset. 11322df173d9SMahesh Salgaonkar */ 11337dee93a9SHari Bathini phdr->p_offset = fw_dump.boot_mem_dest_addr + offset; 11347dee93a9SHari Bathini if (j < (fw_dump.boot_mem_regs_cnt - 1)) { 11357dee93a9SHari Bathini offset += fw_dump.boot_mem_sz[j]; 11367dee93a9SHari Bathini raddr = fw_dump.boot_mem_addr[++j]; 11377dee93a9SHari Bathini } 11382df173d9SMahesh Salgaonkar } 11392df173d9SMahesh Salgaonkar 11402df173d9SMahesh Salgaonkar phdr->p_paddr = mbase; 11412df173d9SMahesh Salgaonkar phdr->p_vaddr = (unsigned long)__va(mbase); 11422df173d9SMahesh Salgaonkar phdr->p_filesz = msize; 11432df173d9SMahesh Salgaonkar phdr->p_memsz = msize; 11442df173d9SMahesh Salgaonkar phdr->p_align = 0; 11452df173d9SMahesh Salgaonkar 11462df173d9SMahesh Salgaonkar /* Increment number of program headers. */ 11472df173d9SMahesh Salgaonkar (elf->e_phnum)++; 11482df173d9SMahesh Salgaonkar } 11492df173d9SMahesh Salgaonkar return 0; 11502df173d9SMahesh Salgaonkar } 11512df173d9SMahesh Salgaonkar 11522df173d9SMahesh Salgaonkar static unsigned long init_fadump_header(unsigned long addr) 11532df173d9SMahesh Salgaonkar { 11542df173d9SMahesh Salgaonkar struct fadump_crash_info_header *fdh; 11552df173d9SMahesh Salgaonkar 11562df173d9SMahesh Salgaonkar if (!addr) 11572df173d9SMahesh Salgaonkar return 0; 11582df173d9SMahesh Salgaonkar 11592df173d9SMahesh Salgaonkar fdh = __va(addr); 11602df173d9SMahesh Salgaonkar addr += sizeof(struct fadump_crash_info_header); 11612df173d9SMahesh Salgaonkar 11622df173d9SMahesh Salgaonkar memset(fdh, 0, sizeof(struct fadump_crash_info_header)); 11632df173d9SMahesh Salgaonkar fdh->magic_number = FADUMP_CRASH_INFO_MAGIC; 11642df173d9SMahesh Salgaonkar fdh->elfcorehdr_addr = addr; 1165ebaeb5aeSMahesh Salgaonkar /* We will set the crashing cpu id in crash_fadump() during crash. */ 11660226e552SHari Bathini fdh->crashing_cpu = FADUMP_CPU_UNKNOWN; 11672df173d9SMahesh Salgaonkar 11682df173d9SMahesh Salgaonkar return addr; 11692df173d9SMahesh Salgaonkar } 11702df173d9SMahesh Salgaonkar 117198b8cd7fSMichal Suchanek static int register_fadump(void) 11723ccc00a7SMahesh Salgaonkar { 11732df173d9SMahesh Salgaonkar unsigned long addr; 11742df173d9SMahesh Salgaonkar void *vaddr; 11751bd6a1c4SHari Bathini int ret; 11762df173d9SMahesh Salgaonkar 11773ccc00a7SMahesh Salgaonkar /* 11783ccc00a7SMahesh Salgaonkar * If no memory is reserved then we can not register for firmware- 11793ccc00a7SMahesh Salgaonkar * assisted dump. 11803ccc00a7SMahesh Salgaonkar */ 11813ccc00a7SMahesh Salgaonkar if (!fw_dump.reserve_dump_area_size) 118298b8cd7fSMichal Suchanek return -ENODEV; 11833ccc00a7SMahesh Salgaonkar 11841bd6a1c4SHari Bathini ret = fadump_setup_crash_memory_ranges(); 11851bd6a1c4SHari Bathini if (ret) 11861bd6a1c4SHari Bathini return ret; 11872df173d9SMahesh Salgaonkar 118841a65d16SHari Bathini addr = fw_dump.fadumphdr_addr; 118941a65d16SHari Bathini 11902df173d9SMahesh Salgaonkar /* Initialize fadump crash info header. */ 11912df173d9SMahesh Salgaonkar addr = init_fadump_header(addr); 11922df173d9SMahesh Salgaonkar vaddr = __va(addr); 11932df173d9SMahesh Salgaonkar 11942df173d9SMahesh Salgaonkar pr_debug("Creating ELF core headers at %#016lx\n", addr); 11952df173d9SMahesh Salgaonkar fadump_create_elfcore_headers(vaddr); 11962df173d9SMahesh Salgaonkar 11973ccc00a7SMahesh Salgaonkar /* register the future kernel dump with firmware. */ 119841a65d16SHari Bathini pr_debug("Registering for firmware-assisted kernel dump...\n"); 119941a65d16SHari Bathini return fw_dump.ops->fadump_register(&fw_dump); 12003ccc00a7SMahesh Salgaonkar } 12013ccc00a7SMahesh Salgaonkar 1202b500afffSMahesh Salgaonkar void fadump_cleanup(void) 1203b500afffSMahesh Salgaonkar { 12042790d01dSHari Bathini if (!fw_dump.fadump_supported) 12052790d01dSHari Bathini return; 12062790d01dSHari Bathini 1207b500afffSMahesh Salgaonkar /* Invalidate the registration only if dump is active. */ 1208b500afffSMahesh Salgaonkar if (fw_dump.dump_active) { 1209f3512011SHari Bathini pr_debug("Invalidating firmware-assisted dump registration\n"); 1210f3512011SHari Bathini fw_dump.ops->fadump_invalidate(&fw_dump); 1211722cde76SMahesh Salgaonkar } else if (fw_dump.dump_registered) { 1212722cde76SMahesh Salgaonkar /* Un-register Firmware-assisted dump if it was registered. */ 121341a65d16SHari Bathini fw_dump.ops->fadump_unregister(&fw_dump); 1214e4fc48fbSHari Bathini fadump_free_mem_ranges(&crash_mrange_info); 1215b500afffSMahesh Salgaonkar } 12162790d01dSHari Bathini 12172790d01dSHari Bathini if (fw_dump.ops->fadump_cleanup) 12182790d01dSHari Bathini fw_dump.ops->fadump_cleanup(&fw_dump); 1219b500afffSMahesh Salgaonkar } 1220b500afffSMahesh Salgaonkar 122168fa6478SHari Bathini static void fadump_free_reserved_memory(unsigned long start_pfn, 122268fa6478SHari Bathini unsigned long end_pfn) 122368fa6478SHari Bathini { 122468fa6478SHari Bathini unsigned long pfn; 122568fa6478SHari Bathini unsigned long time_limit = jiffies + HZ; 122668fa6478SHari Bathini 122768fa6478SHari Bathini pr_info("freeing reserved memory (0x%llx - 0x%llx)\n", 122868fa6478SHari Bathini PFN_PHYS(start_pfn), PFN_PHYS(end_pfn)); 122968fa6478SHari Bathini 123068fa6478SHari Bathini for (pfn = start_pfn; pfn < end_pfn; pfn++) { 123168fa6478SHari Bathini free_reserved_page(pfn_to_page(pfn)); 123268fa6478SHari Bathini 123368fa6478SHari Bathini if (time_after(jiffies, time_limit)) { 123468fa6478SHari Bathini cond_resched(); 123568fa6478SHari Bathini time_limit = jiffies + HZ; 123668fa6478SHari Bathini } 123768fa6478SHari Bathini } 123868fa6478SHari Bathini } 123968fa6478SHari Bathini 124068fa6478SHari Bathini /* 124168fa6478SHari Bathini * Skip memory holes and free memory that was actually reserved. 124268fa6478SHari Bathini */ 1243dda9dbfeSHari Bathini static void fadump_release_reserved_area(u64 start, u64 end) 124468fa6478SHari Bathini { 1245b10d6bcaSMike Rapoport unsigned long reg_spfn, reg_epfn; 1246b10d6bcaSMike Rapoport u64 tstart, tend, spfn, epfn; 1247b10d6bcaSMike Rapoport int i; 124868fa6478SHari Bathini 1249dda9dbfeSHari Bathini spfn = PHYS_PFN(start); 1250dda9dbfeSHari Bathini epfn = PHYS_PFN(end); 1251c9118e6cSMike Rapoport 1252c9118e6cSMike Rapoport for_each_mem_pfn_range(i, MAX_NUMNODES, ®_spfn, ®_epfn, NULL) { 1253c9118e6cSMike Rapoport tstart = max_t(u64, spfn, reg_spfn); 1254c9118e6cSMike Rapoport tend = min_t(u64, epfn, reg_epfn); 1255c9118e6cSMike Rapoport 125668fa6478SHari Bathini if (tstart < tend) { 125768fa6478SHari Bathini fadump_free_reserved_memory(tstart, tend); 125868fa6478SHari Bathini 1259dda9dbfeSHari Bathini if (tend == epfn) 126068fa6478SHari Bathini break; 126168fa6478SHari Bathini 1262dda9dbfeSHari Bathini spfn = tend; 126368fa6478SHari Bathini } 126468fa6478SHari Bathini } 126568fa6478SHari Bathini } 126668fa6478SHari Bathini 1267b500afffSMahesh Salgaonkar /* 1268dda9dbfeSHari Bathini * Sort the mem ranges in-place and merge adjacent ranges 1269dda9dbfeSHari Bathini * to minimize the memory ranges count. 1270b500afffSMahesh Salgaonkar */ 1271dda9dbfeSHari Bathini static void sort_and_merge_mem_ranges(struct fadump_mrange_info *mrange_info) 1272b500afffSMahesh Salgaonkar { 1273dda9dbfeSHari Bathini struct fadump_memory_range *mem_ranges; 1274dda9dbfeSHari Bathini struct fadump_memory_range tmp_range; 1275dda9dbfeSHari Bathini u64 base, size; 1276dda9dbfeSHari Bathini int i, j, idx; 1277dda9dbfeSHari Bathini 1278dda9dbfeSHari Bathini if (!reserved_mrange_info.mem_range_cnt) 1279dda9dbfeSHari Bathini return; 1280dda9dbfeSHari Bathini 1281dda9dbfeSHari Bathini /* Sort the memory ranges */ 1282dda9dbfeSHari Bathini mem_ranges = mrange_info->mem_ranges; 1283dda9dbfeSHari Bathini for (i = 0; i < mrange_info->mem_range_cnt; i++) { 1284dda9dbfeSHari Bathini idx = i; 1285dda9dbfeSHari Bathini for (j = (i + 1); j < mrange_info->mem_range_cnt; j++) { 1286dda9dbfeSHari Bathini if (mem_ranges[idx].base > mem_ranges[j].base) 1287dda9dbfeSHari Bathini idx = j; 1288dda9dbfeSHari Bathini } 1289dda9dbfeSHari Bathini if (idx != i) { 1290dda9dbfeSHari Bathini tmp_range = mem_ranges[idx]; 1291dda9dbfeSHari Bathini mem_ranges[idx] = mem_ranges[i]; 1292dda9dbfeSHari Bathini mem_ranges[i] = tmp_range; 1293dda9dbfeSHari Bathini } 1294dda9dbfeSHari Bathini } 1295dda9dbfeSHari Bathini 1296dda9dbfeSHari Bathini /* Merge adjacent reserved ranges */ 1297dda9dbfeSHari Bathini idx = 0; 1298dda9dbfeSHari Bathini for (i = 1; i < mrange_info->mem_range_cnt; i++) { 1299dda9dbfeSHari Bathini base = mem_ranges[i-1].base; 1300dda9dbfeSHari Bathini size = mem_ranges[i-1].size; 1301dda9dbfeSHari Bathini if (mem_ranges[i].base == (base + size)) 1302dda9dbfeSHari Bathini mem_ranges[idx].size += mem_ranges[i].size; 1303dda9dbfeSHari Bathini else { 1304dda9dbfeSHari Bathini idx++; 1305dda9dbfeSHari Bathini if (i == idx) 1306dda9dbfeSHari Bathini continue; 1307dda9dbfeSHari Bathini 1308dda9dbfeSHari Bathini mem_ranges[idx] = mem_ranges[i]; 1309dda9dbfeSHari Bathini } 1310dda9dbfeSHari Bathini } 1311dda9dbfeSHari Bathini mrange_info->mem_range_cnt = idx + 1; 1312dda9dbfeSHari Bathini } 1313dda9dbfeSHari Bathini 1314dda9dbfeSHari Bathini /* 1315dda9dbfeSHari Bathini * Scan reserved-ranges to consider them while reserving/releasing 1316dda9dbfeSHari Bathini * memory for FADump. 1317dda9dbfeSHari Bathini */ 131802c04e37SHari Bathini static void __init early_init_dt_scan_reserved_ranges(unsigned long node) 1319dda9dbfeSHari Bathini { 1320dda9dbfeSHari Bathini const __be32 *prop; 1321dda9dbfeSHari Bathini int len, ret = -1; 1322dda9dbfeSHari Bathini unsigned long i; 1323dda9dbfeSHari Bathini 132402c04e37SHari Bathini /* reserved-ranges already scanned */ 132502c04e37SHari Bathini if (reserved_mrange_info.mem_range_cnt != 0) 132602c04e37SHari Bathini return; 1327dda9dbfeSHari Bathini 132802c04e37SHari Bathini prop = of_get_flat_dt_prop(node, "reserved-ranges", &len); 1329dda9dbfeSHari Bathini if (!prop) 133002c04e37SHari Bathini return; 1331dda9dbfeSHari Bathini 1332dda9dbfeSHari Bathini /* 1333dda9dbfeSHari Bathini * Each reserved range is an (address,size) pair, 2 cells each, 1334dda9dbfeSHari Bathini * totalling 4 cells per range. 1335dda9dbfeSHari Bathini */ 1336dda9dbfeSHari Bathini for (i = 0; i < len / (sizeof(*prop) * 4); i++) { 1337dda9dbfeSHari Bathini u64 base, size; 1338dda9dbfeSHari Bathini 1339dda9dbfeSHari Bathini base = of_read_number(prop + (i * 4) + 0, 2); 1340dda9dbfeSHari Bathini size = of_read_number(prop + (i * 4) + 2, 2); 1341dda9dbfeSHari Bathini 1342dda9dbfeSHari Bathini if (size) { 1343dda9dbfeSHari Bathini ret = fadump_add_mem_range(&reserved_mrange_info, 1344dda9dbfeSHari Bathini base, base + size); 1345dda9dbfeSHari Bathini if (ret < 0) { 1346dda9dbfeSHari Bathini pr_warn("some reserved ranges are ignored!\n"); 1347dda9dbfeSHari Bathini break; 1348dda9dbfeSHari Bathini } 1349dda9dbfeSHari Bathini } 1350dda9dbfeSHari Bathini } 1351dda9dbfeSHari Bathini 135202c04e37SHari Bathini /* Compact reserved ranges */ 135302c04e37SHari Bathini sort_and_merge_mem_ranges(&reserved_mrange_info); 1354dda9dbfeSHari Bathini } 1355dda9dbfeSHari Bathini 1356dda9dbfeSHari Bathini /* 1357dda9dbfeSHari Bathini * Release the memory that was reserved during early boot to preserve the 1358dda9dbfeSHari Bathini * crash'ed kernel's memory contents except reserved dump area (permanent 1359dda9dbfeSHari Bathini * reservation) and reserved ranges used by F/W. The released memory will 1360dda9dbfeSHari Bathini * be available for general use. 1361dda9dbfeSHari Bathini */ 1362dda9dbfeSHari Bathini static void fadump_release_memory(u64 begin, u64 end) 1363dda9dbfeSHari Bathini { 1364dda9dbfeSHari Bathini u64 ra_start, ra_end, tstart; 1365dda9dbfeSHari Bathini int i, ret; 1366dda9dbfeSHari Bathini 1367b500afffSMahesh Salgaonkar ra_start = fw_dump.reserve_dump_area_start; 1368b500afffSMahesh Salgaonkar ra_end = ra_start + fw_dump.reserve_dump_area_size; 1369b500afffSMahesh Salgaonkar 1370b500afffSMahesh Salgaonkar /* 137102c04e37SHari Bathini * If reserved ranges array limit is hit, overwrite the last reserved 137202c04e37SHari Bathini * memory range with reserved dump area to ensure it is excluded from 137302c04e37SHari Bathini * the memory being released (reused for next FADump registration). 1374dda9dbfeSHari Bathini */ 137502c04e37SHari Bathini if (reserved_mrange_info.mem_range_cnt == 137602c04e37SHari Bathini reserved_mrange_info.max_mem_ranges) 137702c04e37SHari Bathini reserved_mrange_info.mem_range_cnt--; 1378dda9dbfeSHari Bathini 137902c04e37SHari Bathini ret = fadump_add_mem_range(&reserved_mrange_info, ra_start, ra_end); 138002c04e37SHari Bathini if (ret != 0) 1381dda9dbfeSHari Bathini return; 1382dda9dbfeSHari Bathini 1383dda9dbfeSHari Bathini /* Get the reserved ranges list in order first. */ 1384dda9dbfeSHari Bathini sort_and_merge_mem_ranges(&reserved_mrange_info); 1385dda9dbfeSHari Bathini 1386dda9dbfeSHari Bathini /* Exclude reserved ranges and release remaining memory */ 1387dda9dbfeSHari Bathini tstart = begin; 1388dda9dbfeSHari Bathini for (i = 0; i < reserved_mrange_info.mem_range_cnt; i++) { 1389dda9dbfeSHari Bathini ra_start = reserved_mrange_info.mem_ranges[i].base; 1390dda9dbfeSHari Bathini ra_end = ra_start + reserved_mrange_info.mem_ranges[i].size; 1391dda9dbfeSHari Bathini 1392dda9dbfeSHari Bathini if (tstart >= ra_end) 1393dda9dbfeSHari Bathini continue; 1394dda9dbfeSHari Bathini 1395dda9dbfeSHari Bathini if (tstart < ra_start) 1396dda9dbfeSHari Bathini fadump_release_reserved_area(tstart, ra_start); 1397dda9dbfeSHari Bathini tstart = ra_end; 1398dda9dbfeSHari Bathini } 1399dda9dbfeSHari Bathini 1400dda9dbfeSHari Bathini if (tstart < end) 1401dda9dbfeSHari Bathini fadump_release_reserved_area(tstart, end); 1402b500afffSMahesh Salgaonkar } 1403b500afffSMahesh Salgaonkar 1404b500afffSMahesh Salgaonkar static void fadump_invalidate_release_mem(void) 1405b500afffSMahesh Salgaonkar { 1406b500afffSMahesh Salgaonkar mutex_lock(&fadump_mutex); 1407b500afffSMahesh Salgaonkar if (!fw_dump.dump_active) { 1408b500afffSMahesh Salgaonkar mutex_unlock(&fadump_mutex); 1409b500afffSMahesh Salgaonkar return; 1410b500afffSMahesh Salgaonkar } 1411b500afffSMahesh Salgaonkar 1412b500afffSMahesh Salgaonkar fadump_cleanup(); 1413b500afffSMahesh Salgaonkar mutex_unlock(&fadump_mutex); 1414b500afffSMahesh Salgaonkar 14157dee93a9SHari Bathini fadump_release_memory(fw_dump.boot_mem_top, memblock_end_of_DRAM()); 1416961cf26aSHari Bathini fadump_free_cpu_notes_buf(); 1417961cf26aSHari Bathini 1418a4e2e2caSHari Bathini /* 1419a4e2e2caSHari Bathini * Setup kernel metadata and initialize the kernel dump 1420a4e2e2caSHari Bathini * memory structure for FADump re-registration. 1421a4e2e2caSHari Bathini */ 1422a4e2e2caSHari Bathini if (fw_dump.ops->fadump_setup_metadata && 1423a4e2e2caSHari Bathini (fw_dump.ops->fadump_setup_metadata(&fw_dump) < 0)) 1424a4e2e2caSHari Bathini pr_warn("Failed to setup kernel metadata!\n"); 142541a65d16SHari Bathini fw_dump.ops->fadump_init_mem_struct(&fw_dump); 1426b500afffSMahesh Salgaonkar } 1427b500afffSMahesh Salgaonkar 1428d418b19fSSourabh Jain static ssize_t release_mem_store(struct kobject *kobj, 1429b500afffSMahesh Salgaonkar struct kobj_attribute *attr, 1430b500afffSMahesh Salgaonkar const char *buf, size_t count) 1431b500afffSMahesh Salgaonkar { 1432dcdc4679SMichal Suchanek int input = -1; 1433dcdc4679SMichal Suchanek 1434b500afffSMahesh Salgaonkar if (!fw_dump.dump_active) 1435b500afffSMahesh Salgaonkar return -EPERM; 1436b500afffSMahesh Salgaonkar 1437dcdc4679SMichal Suchanek if (kstrtoint(buf, 0, &input)) 1438dcdc4679SMichal Suchanek return -EINVAL; 1439dcdc4679SMichal Suchanek 1440dcdc4679SMichal Suchanek if (input == 1) { 1441b500afffSMahesh Salgaonkar /* 1442b500afffSMahesh Salgaonkar * Take away the '/proc/vmcore'. We are releasing the dump 1443b500afffSMahesh Salgaonkar * memory, hence it will not be valid anymore. 1444b500afffSMahesh Salgaonkar */ 14452685f826SMichael Ellerman #ifdef CONFIG_PROC_VMCORE 1446b500afffSMahesh Salgaonkar vmcore_cleanup(); 14472685f826SMichael Ellerman #endif 1448b500afffSMahesh Salgaonkar fadump_invalidate_release_mem(); 1449b500afffSMahesh Salgaonkar 1450b500afffSMahesh Salgaonkar } else 1451b500afffSMahesh Salgaonkar return -EINVAL; 1452b500afffSMahesh Salgaonkar return count; 1453b500afffSMahesh Salgaonkar } 1454b500afffSMahesh Salgaonkar 1455d418b19fSSourabh Jain /* Release the reserved memory and disable the FADump */ 1456d276960dSNick Child static void __init unregister_fadump(void) 1457d418b19fSSourabh Jain { 1458d418b19fSSourabh Jain fadump_cleanup(); 1459d418b19fSSourabh Jain fadump_release_memory(fw_dump.reserve_dump_area_start, 1460d418b19fSSourabh Jain fw_dump.reserve_dump_area_size); 1461d418b19fSSourabh Jain fw_dump.fadump_enabled = 0; 1462d418b19fSSourabh Jain kobject_put(fadump_kobj); 1463d418b19fSSourabh Jain } 1464d418b19fSSourabh Jain 1465d418b19fSSourabh Jain static ssize_t enabled_show(struct kobject *kobj, 14663ccc00a7SMahesh Salgaonkar struct kobj_attribute *attr, 14673ccc00a7SMahesh Salgaonkar char *buf) 14683ccc00a7SMahesh Salgaonkar { 14693ccc00a7SMahesh Salgaonkar return sprintf(buf, "%d\n", fw_dump.fadump_enabled); 14703ccc00a7SMahesh Salgaonkar } 14713ccc00a7SMahesh Salgaonkar 1472d8e73458SSourabh Jain static ssize_t mem_reserved_show(struct kobject *kobj, 1473d8e73458SSourabh Jain struct kobj_attribute *attr, 1474d8e73458SSourabh Jain char *buf) 1475d8e73458SSourabh Jain { 1476d8e73458SSourabh Jain return sprintf(buf, "%ld\n", fw_dump.reserve_dump_area_size); 1477d8e73458SSourabh Jain } 1478d8e73458SSourabh Jain 1479d418b19fSSourabh Jain static ssize_t registered_show(struct kobject *kobj, 14803ccc00a7SMahesh Salgaonkar struct kobj_attribute *attr, 14813ccc00a7SMahesh Salgaonkar char *buf) 14823ccc00a7SMahesh Salgaonkar { 14833ccc00a7SMahesh Salgaonkar return sprintf(buf, "%d\n", fw_dump.dump_registered); 14843ccc00a7SMahesh Salgaonkar } 14853ccc00a7SMahesh Salgaonkar 1486d418b19fSSourabh Jain static ssize_t registered_store(struct kobject *kobj, 14873ccc00a7SMahesh Salgaonkar struct kobj_attribute *attr, 14883ccc00a7SMahesh Salgaonkar const char *buf, size_t count) 14893ccc00a7SMahesh Salgaonkar { 14903ccc00a7SMahesh Salgaonkar int ret = 0; 1491dcdc4679SMichal Suchanek int input = -1; 14923ccc00a7SMahesh Salgaonkar 1493f3512011SHari Bathini if (!fw_dump.fadump_enabled || fw_dump.dump_active) 14943ccc00a7SMahesh Salgaonkar return -EPERM; 14953ccc00a7SMahesh Salgaonkar 1496dcdc4679SMichal Suchanek if (kstrtoint(buf, 0, &input)) 1497dcdc4679SMichal Suchanek return -EINVAL; 1498dcdc4679SMichal Suchanek 14993ccc00a7SMahesh Salgaonkar mutex_lock(&fadump_mutex); 15003ccc00a7SMahesh Salgaonkar 1501dcdc4679SMichal Suchanek switch (input) { 1502dcdc4679SMichal Suchanek case 0: 15033ccc00a7SMahesh Salgaonkar if (fw_dump.dump_registered == 0) { 15043ccc00a7SMahesh Salgaonkar goto unlock_out; 15053ccc00a7SMahesh Salgaonkar } 1506f3512011SHari Bathini 15073ccc00a7SMahesh Salgaonkar /* Un-register Firmware-assisted dump */ 150841a65d16SHari Bathini pr_debug("Un-register firmware-assisted dump\n"); 150941a65d16SHari Bathini fw_dump.ops->fadump_unregister(&fw_dump); 15103ccc00a7SMahesh Salgaonkar break; 1511dcdc4679SMichal Suchanek case 1: 15123ccc00a7SMahesh Salgaonkar if (fw_dump.dump_registered == 1) { 15130823c68bSHari Bathini /* Un-register Firmware-assisted dump */ 151441a65d16SHari Bathini fw_dump.ops->fadump_unregister(&fw_dump); 15153ccc00a7SMahesh Salgaonkar } 15163ccc00a7SMahesh Salgaonkar /* Register Firmware-assisted dump */ 151798b8cd7fSMichal Suchanek ret = register_fadump(); 15183ccc00a7SMahesh Salgaonkar break; 15193ccc00a7SMahesh Salgaonkar default: 15203ccc00a7SMahesh Salgaonkar ret = -EINVAL; 15213ccc00a7SMahesh Salgaonkar break; 15223ccc00a7SMahesh Salgaonkar } 15233ccc00a7SMahesh Salgaonkar 15243ccc00a7SMahesh Salgaonkar unlock_out: 15253ccc00a7SMahesh Salgaonkar mutex_unlock(&fadump_mutex); 15263ccc00a7SMahesh Salgaonkar return ret < 0 ? ret : count; 15273ccc00a7SMahesh Salgaonkar } 15283ccc00a7SMahesh Salgaonkar 15293ccc00a7SMahesh Salgaonkar static int fadump_region_show(struct seq_file *m, void *private) 15303ccc00a7SMahesh Salgaonkar { 15313ccc00a7SMahesh Salgaonkar if (!fw_dump.fadump_enabled) 15323ccc00a7SMahesh Salgaonkar return 0; 15333ccc00a7SMahesh Salgaonkar 1534b500afffSMahesh Salgaonkar mutex_lock(&fadump_mutex); 153541a65d16SHari Bathini fw_dump.ops->fadump_region_show(&fw_dump, m); 1536b500afffSMahesh Salgaonkar mutex_unlock(&fadump_mutex); 15373ccc00a7SMahesh Salgaonkar return 0; 15383ccc00a7SMahesh Salgaonkar } 15393ccc00a7SMahesh Salgaonkar 1540d418b19fSSourabh Jain static struct kobj_attribute release_attr = __ATTR_WO(release_mem); 1541d418b19fSSourabh Jain static struct kobj_attribute enable_attr = __ATTR_RO(enabled); 1542d418b19fSSourabh Jain static struct kobj_attribute register_attr = __ATTR_RW(registered); 1543d8e73458SSourabh Jain static struct kobj_attribute mem_reserved_attr = __ATTR_RO(mem_reserved); 1544d418b19fSSourabh Jain 1545d418b19fSSourabh Jain static struct attribute *fadump_attrs[] = { 1546d418b19fSSourabh Jain &enable_attr.attr, 1547d418b19fSSourabh Jain ®ister_attr.attr, 1548d8e73458SSourabh Jain &mem_reserved_attr.attr, 1549d418b19fSSourabh Jain NULL, 1550d418b19fSSourabh Jain }; 1551d418b19fSSourabh Jain 1552d418b19fSSourabh Jain ATTRIBUTE_GROUPS(fadump); 15533ccc00a7SMahesh Salgaonkar 1554f6cee260SYangtao Li DEFINE_SHOW_ATTRIBUTE(fadump_region); 15553ccc00a7SMahesh Salgaonkar 1556d276960dSNick Child static void __init fadump_init_files(void) 15573ccc00a7SMahesh Salgaonkar { 15583ccc00a7SMahesh Salgaonkar int rc = 0; 15593ccc00a7SMahesh Salgaonkar 1560d418b19fSSourabh Jain fadump_kobj = kobject_create_and_add("fadump", kernel_kobj); 1561d418b19fSSourabh Jain if (!fadump_kobj) { 1562d418b19fSSourabh Jain pr_err("failed to create fadump kobject\n"); 1563d418b19fSSourabh Jain return; 1564d418b19fSSourabh Jain } 1565860286cfSGreg Kroah-Hartman 1566dbf77fedSAneesh Kumar K.V debugfs_create_file("fadump_region", 0444, arch_debugfs_dir, NULL, 15673ccc00a7SMahesh Salgaonkar &fadump_region_fops); 1568b500afffSMahesh Salgaonkar 1569b500afffSMahesh Salgaonkar if (fw_dump.dump_active) { 1570d418b19fSSourabh Jain rc = sysfs_create_file(fadump_kobj, &release_attr.attr); 1571b500afffSMahesh Salgaonkar if (rc) 1572d418b19fSSourabh Jain pr_err("unable to create release_mem sysfs file (%d)\n", 1573d418b19fSSourabh Jain rc); 1574d418b19fSSourabh Jain } 1575d418b19fSSourabh Jain 1576d418b19fSSourabh Jain rc = sysfs_create_groups(fadump_kobj, fadump_groups); 1577d418b19fSSourabh Jain if (rc) { 1578d418b19fSSourabh Jain pr_err("sysfs group creation failed (%d), unregistering FADump", 1579d418b19fSSourabh Jain rc); 1580d418b19fSSourabh Jain unregister_fadump(); 1581d418b19fSSourabh Jain return; 1582d418b19fSSourabh Jain } 1583d418b19fSSourabh Jain 1584d418b19fSSourabh Jain /* 1585d418b19fSSourabh Jain * The FADump sysfs are moved from kernel_kobj to fadump_kobj need to 1586d418b19fSSourabh Jain * create symlink at old location to maintain backward compatibility. 1587d418b19fSSourabh Jain * 1588d418b19fSSourabh Jain * - fadump_enabled -> fadump/enabled 1589d418b19fSSourabh Jain * - fadump_registered -> fadump/registered 1590d418b19fSSourabh Jain * - fadump_release_mem -> fadump/release_mem 1591d418b19fSSourabh Jain */ 1592d418b19fSSourabh Jain rc = compat_only_sysfs_link_entry_to_kobj(kernel_kobj, fadump_kobj, 1593d418b19fSSourabh Jain "enabled", "fadump_enabled"); 1594d418b19fSSourabh Jain if (rc) { 1595d418b19fSSourabh Jain pr_err("unable to create fadump_enabled symlink (%d)", rc); 1596d418b19fSSourabh Jain return; 1597d418b19fSSourabh Jain } 1598d418b19fSSourabh Jain 1599d418b19fSSourabh Jain rc = compat_only_sysfs_link_entry_to_kobj(kernel_kobj, fadump_kobj, 1600d418b19fSSourabh Jain "registered", 1601d418b19fSSourabh Jain "fadump_registered"); 1602d418b19fSSourabh Jain if (rc) { 1603d418b19fSSourabh Jain pr_err("unable to create fadump_registered symlink (%d)", rc); 1604d418b19fSSourabh Jain sysfs_remove_link(kernel_kobj, "fadump_enabled"); 1605d418b19fSSourabh Jain return; 1606d418b19fSSourabh Jain } 1607d418b19fSSourabh Jain 1608d418b19fSSourabh Jain if (fw_dump.dump_active) { 1609d418b19fSSourabh Jain rc = compat_only_sysfs_link_entry_to_kobj(kernel_kobj, 1610d418b19fSSourabh Jain fadump_kobj, 1611d418b19fSSourabh Jain "release_mem", 1612d418b19fSSourabh Jain "fadump_release_mem"); 1613d418b19fSSourabh Jain if (rc) 1614d418b19fSSourabh Jain pr_err("unable to create fadump_release_mem symlink (%d)", 1615d418b19fSSourabh Jain rc); 1616b500afffSMahesh Salgaonkar } 16173ccc00a7SMahesh Salgaonkar return; 16183ccc00a7SMahesh Salgaonkar } 16193ccc00a7SMahesh Salgaonkar 16203ccc00a7SMahesh Salgaonkar /* 16213ccc00a7SMahesh Salgaonkar * Prepare for firmware-assisted dump. 16223ccc00a7SMahesh Salgaonkar */ 16233ccc00a7SMahesh Salgaonkar int __init setup_fadump(void) 16243ccc00a7SMahesh Salgaonkar { 1625565f9bc0SMichal Suchanek if (!fw_dump.fadump_supported) 16263ccc00a7SMahesh Salgaonkar return 0; 16273ccc00a7SMahesh Salgaonkar 1628565f9bc0SMichal Suchanek fadump_init_files(); 16293ccc00a7SMahesh Salgaonkar fadump_show_config(); 1630565f9bc0SMichal Suchanek 1631565f9bc0SMichal Suchanek if (!fw_dump.fadump_enabled) 1632565f9bc0SMichal Suchanek return 1; 1633565f9bc0SMichal Suchanek 16342df173d9SMahesh Salgaonkar /* 16352df173d9SMahesh Salgaonkar * If dump data is available then see if it is valid and prepare for 16362df173d9SMahesh Salgaonkar * saving it to the disk. 16372df173d9SMahesh Salgaonkar */ 1638b500afffSMahesh Salgaonkar if (fw_dump.dump_active) { 1639b500afffSMahesh Salgaonkar /* 1640b500afffSMahesh Salgaonkar * if dump process fails then invalidate the registration 1641b500afffSMahesh Salgaonkar * and release memory before proceeding for re-registration. 1642b500afffSMahesh Salgaonkar */ 1643f3512011SHari Bathini if (fw_dump.ops->fadump_process(&fw_dump) < 0) 1644b500afffSMahesh Salgaonkar fadump_invalidate_release_mem(); 1645b500afffSMahesh Salgaonkar } 16463ccc00a7SMahesh Salgaonkar /* Initialize the kernel dump memory structure for FAD registration. */ 16472df173d9SMahesh Salgaonkar else if (fw_dump.reserve_dump_area_size) 164841a65d16SHari Bathini fw_dump.ops->fadump_init_mem_struct(&fw_dump); 1649f3512011SHari Bathini 165006e629c2SHari Bathini /* 165106e629c2SHari Bathini * In case of panic, fadump is triggered via ppc_panic_event() 165206e629c2SHari Bathini * panic notifier. Setting crash_kexec_post_notifiers to 'true' 165306e629c2SHari Bathini * lets panic() function take crash friendly path before panic 165406e629c2SHari Bathini * notifiers are invoked. 165506e629c2SHari Bathini */ 165606e629c2SHari Bathini crash_kexec_post_notifiers = true; 165706e629c2SHari Bathini 16583ccc00a7SMahesh Salgaonkar return 1; 16593ccc00a7SMahesh Salgaonkar } 16603ccc00a7SMahesh Salgaonkar subsys_initcall(setup_fadump); 1661bec53196SHari Bathini #else /* !CONFIG_PRESERVE_FA_DUMP */ 1662bec53196SHari Bathini 1663bec53196SHari Bathini /* Scan the Firmware Assisted dump configuration details. */ 1664bec53196SHari Bathini int __init early_init_dt_scan_fw_dump(unsigned long node, const char *uname, 1665bec53196SHari Bathini int depth, void *data) 1666bec53196SHari Bathini { 1667bec53196SHari Bathini if ((depth != 1) || (strcmp(uname, "ibm,opal") != 0)) 1668bec53196SHari Bathini return 0; 1669bec53196SHari Bathini 1670bec53196SHari Bathini opal_fadump_dt_scan(&fw_dump, node); 1671bec53196SHari Bathini return 1; 1672bec53196SHari Bathini } 1673bec53196SHari Bathini 1674bec53196SHari Bathini /* 1675bec53196SHari Bathini * When dump is active but PRESERVE_FA_DUMP is enabled on the kernel, 1676bec53196SHari Bathini * preserve crash data. The subsequent memory preserving kernel boot 1677bec53196SHari Bathini * is likely to process this crash data. 1678bec53196SHari Bathini */ 1679bec53196SHari Bathini int __init fadump_reserve_mem(void) 1680bec53196SHari Bathini { 1681bec53196SHari Bathini if (fw_dump.dump_active) { 1682bec53196SHari Bathini /* 1683bec53196SHari Bathini * If last boot has crashed then reserve all the memory 1684bec53196SHari Bathini * above boot memory to preserve crash data. 1685bec53196SHari Bathini */ 1686bec53196SHari Bathini pr_info("Preserving crash data for processing in next boot.\n"); 1687bec53196SHari Bathini fadump_reserve_crash_area(fw_dump.boot_mem_top); 1688bec53196SHari Bathini } else 1689bec53196SHari Bathini pr_debug("FADump-aware kernel..\n"); 1690bec53196SHari Bathini 1691bec53196SHari Bathini return 1; 1692bec53196SHari Bathini } 1693bec53196SHari Bathini #endif /* CONFIG_PRESERVE_FA_DUMP */ 1694b2a815a5SHari Bathini 1695b2a815a5SHari Bathini /* Preserve everything above the base address */ 1696b2a815a5SHari Bathini static void __init fadump_reserve_crash_area(u64 base) 1697b2a815a5SHari Bathini { 1698b10d6bcaSMike Rapoport u64 i, mstart, mend, msize; 1699b2a815a5SHari Bathini 1700b10d6bcaSMike Rapoport for_each_mem_range(i, &mstart, &mend) { 1701b10d6bcaSMike Rapoport msize = mend - mstart; 1702b2a815a5SHari Bathini 1703b2a815a5SHari Bathini if ((mstart + msize) < base) 1704b2a815a5SHari Bathini continue; 1705b2a815a5SHari Bathini 1706b2a815a5SHari Bathini if (mstart < base) { 1707b2a815a5SHari Bathini msize -= (base - mstart); 1708b2a815a5SHari Bathini mstart = base; 1709b2a815a5SHari Bathini } 1710b2a815a5SHari Bathini 1711b2a815a5SHari Bathini pr_info("Reserving %lluMB of memory at %#016llx for preserving crash data", 1712b2a815a5SHari Bathini (msize >> 20), mstart); 1713b2a815a5SHari Bathini memblock_reserve(mstart, msize); 1714b2a815a5SHari Bathini } 1715b2a815a5SHari Bathini } 1716bec53196SHari Bathini 1717bec53196SHari Bathini unsigned long __init arch_reserved_kernel_pages(void) 1718bec53196SHari Bathini { 1719bec53196SHari Bathini return memblock_reserved_size() / PAGE_SIZE; 1720bec53196SHari Bathini } 1721