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 37b2a815a5SHari Bathini static void __init fadump_reserve_crash_area(u64 base); 38b2a815a5SHari Bathini 39bec53196SHari Bathini #ifndef CONFIG_PRESERVE_FA_DUMP 403ccc00a7SMahesh Salgaonkar static DEFINE_MUTEX(fadump_mutex); 41e4fc48fbSHari Bathini struct fadump_mrange_info crash_mrange_info = { "crash", NULL, 0, 0, 0 }; 42dda9dbfeSHari Bathini struct fadump_mrange_info reserved_mrange_info = { "reserved", NULL, 0, 0, 0 }; 43eb39c880SMahesh Salgaonkar 44a4e92ce8SMahesh Salgaonkar #ifdef CONFIG_CMA 450226e552SHari Bathini static struct cma *fadump_cma; 460226e552SHari Bathini 47a4e92ce8SMahesh Salgaonkar /* 48a4e92ce8SMahesh Salgaonkar * fadump_cma_init() - Initialize CMA area from a fadump reserved memory 49a4e92ce8SMahesh Salgaonkar * 50a4e92ce8SMahesh Salgaonkar * This function initializes CMA area from fadump reserved memory. 51a4e92ce8SMahesh Salgaonkar * The total size of fadump reserved memory covers for boot memory size 52a4e92ce8SMahesh Salgaonkar * + cpu data size + hpte size and metadata. 53a4e92ce8SMahesh Salgaonkar * Initialize only the area equivalent to boot memory size for CMA use. 54a4e92ce8SMahesh Salgaonkar * The reamining portion of fadump reserved memory will be not given 55a4e92ce8SMahesh Salgaonkar * to CMA and pages for thoes will stay reserved. boot memory size is 56a4e92ce8SMahesh Salgaonkar * aligned per CMA requirement to satisy cma_init_reserved_mem() call. 57a4e92ce8SMahesh Salgaonkar * But for some reason even if it fails we still have the memory reservation 58a4e92ce8SMahesh Salgaonkar * with us and we can still continue doing fadump. 59a4e92ce8SMahesh Salgaonkar */ 60a4e92ce8SMahesh Salgaonkar int __init fadump_cma_init(void) 61a4e92ce8SMahesh Salgaonkar { 62a4e92ce8SMahesh Salgaonkar unsigned long long base, size; 63a4e92ce8SMahesh Salgaonkar int rc; 64a4e92ce8SMahesh Salgaonkar 65a4e92ce8SMahesh Salgaonkar if (!fw_dump.fadump_enabled) 66a4e92ce8SMahesh Salgaonkar return 0; 67a4e92ce8SMahesh Salgaonkar 68a4e92ce8SMahesh Salgaonkar /* 69a4e92ce8SMahesh Salgaonkar * Do not use CMA if user has provided fadump=nocma kernel parameter. 70a4e92ce8SMahesh Salgaonkar * Return 1 to continue with fadump old behaviour. 71a4e92ce8SMahesh Salgaonkar */ 72a4e92ce8SMahesh Salgaonkar if (fw_dump.nocma) 73a4e92ce8SMahesh Salgaonkar return 1; 74a4e92ce8SMahesh Salgaonkar 75a4e92ce8SMahesh Salgaonkar base = fw_dump.reserve_dump_area_start; 76a4e92ce8SMahesh Salgaonkar size = fw_dump.boot_memory_size; 77a4e92ce8SMahesh Salgaonkar 78a4e92ce8SMahesh Salgaonkar if (!size) 79a4e92ce8SMahesh Salgaonkar return 0; 80a4e92ce8SMahesh Salgaonkar 81a4e92ce8SMahesh Salgaonkar rc = cma_init_reserved_mem(base, size, 0, "fadump_cma", &fadump_cma); 82a4e92ce8SMahesh Salgaonkar if (rc) { 83a4e92ce8SMahesh Salgaonkar pr_err("Failed to init cma area for firmware-assisted dump,%d\n", rc); 84a4e92ce8SMahesh Salgaonkar /* 85a4e92ce8SMahesh Salgaonkar * Though the CMA init has failed we still have memory 86a4e92ce8SMahesh Salgaonkar * reservation with us. The reserved memory will be 87a4e92ce8SMahesh Salgaonkar * blocked from production system usage. Hence return 1, 88a4e92ce8SMahesh Salgaonkar * so that we can continue with fadump. 89a4e92ce8SMahesh Salgaonkar */ 90a4e92ce8SMahesh Salgaonkar return 1; 91a4e92ce8SMahesh Salgaonkar } 92a4e92ce8SMahesh Salgaonkar 93a4e92ce8SMahesh Salgaonkar /* 94a4e92ce8SMahesh Salgaonkar * So we now have successfully initialized cma area for fadump. 95a4e92ce8SMahesh Salgaonkar */ 96a4e92ce8SMahesh Salgaonkar pr_info("Initialized 0x%lx bytes cma area at %ldMB from 0x%lx " 97a4e92ce8SMahesh Salgaonkar "bytes of memory reserved for firmware-assisted dump\n", 98a4e92ce8SMahesh Salgaonkar cma_get_size(fadump_cma), 99a4e92ce8SMahesh Salgaonkar (unsigned long)cma_get_base(fadump_cma) >> 20, 100a4e92ce8SMahesh Salgaonkar fw_dump.reserve_dump_area_size); 101a4e92ce8SMahesh Salgaonkar return 1; 102a4e92ce8SMahesh Salgaonkar } 103a4e92ce8SMahesh Salgaonkar #else 104a4e92ce8SMahesh Salgaonkar static int __init fadump_cma_init(void) { return 1; } 105a4e92ce8SMahesh Salgaonkar #endif /* CONFIG_CMA */ 106a4e92ce8SMahesh Salgaonkar 107eb39c880SMahesh Salgaonkar /* Scan the Firmware Assisted dump configuration details. */ 108f3512011SHari Bathini int __init early_init_dt_scan_fw_dump(unsigned long node, const char *uname, 109f3512011SHari Bathini int depth, void *data) 110eb39c880SMahesh Salgaonkar { 11141df5928SHari Bathini if (depth != 1) 112eb39c880SMahesh Salgaonkar return 0; 113eb39c880SMahesh Salgaonkar 11441df5928SHari Bathini if (strcmp(uname, "rtas") == 0) { 115d3833a70SHari Bathini rtas_fadump_dt_scan(&fw_dump, node); 116eb39c880SMahesh Salgaonkar return 1; 117eb39c880SMahesh Salgaonkar } 118eb39c880SMahesh Salgaonkar 11941df5928SHari Bathini if (strcmp(uname, "ibm,opal") == 0) { 12041df5928SHari Bathini opal_fadump_dt_scan(&fw_dump, node); 12141df5928SHari Bathini return 1; 12241df5928SHari Bathini } 12341df5928SHari Bathini 12441df5928SHari Bathini return 0; 12541df5928SHari Bathini } 12641df5928SHari Bathini 127eae0dfccSHari Bathini /* 128eae0dfccSHari Bathini * If fadump is registered, check if the memory provided 1290db6896fSMahesh Salgaonkar * falls within boot memory area and reserved memory area. 130eae0dfccSHari Bathini */ 1310db6896fSMahesh Salgaonkar int is_fadump_memory_area(u64 addr, ulong size) 132eae0dfccSHari Bathini { 1330db6896fSMahesh Salgaonkar u64 d_start = fw_dump.reserve_dump_area_start; 1340db6896fSMahesh Salgaonkar u64 d_end = d_start + fw_dump.reserve_dump_area_size; 1350db6896fSMahesh Salgaonkar 136eae0dfccSHari Bathini if (!fw_dump.dump_registered) 137eae0dfccSHari Bathini return 0; 138eae0dfccSHari Bathini 1390db6896fSMahesh Salgaonkar if (((addr + size) > d_start) && (addr <= d_end)) 1400db6896fSMahesh Salgaonkar return 1; 1410db6896fSMahesh Salgaonkar 142eae0dfccSHari Bathini return (addr + size) > RMA_START && addr <= fw_dump.boot_memory_size; 143eae0dfccSHari Bathini } 144eae0dfccSHari Bathini 1456fcd6baaSNicholas Piggin int should_fadump_crash(void) 1466fcd6baaSNicholas Piggin { 1476fcd6baaSNicholas Piggin if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr) 1486fcd6baaSNicholas Piggin return 0; 1496fcd6baaSNicholas Piggin return 1; 1506fcd6baaSNicholas Piggin } 1516fcd6baaSNicholas Piggin 1523ccc00a7SMahesh Salgaonkar int is_fadump_active(void) 1533ccc00a7SMahesh Salgaonkar { 1543ccc00a7SMahesh Salgaonkar return fw_dump.dump_active; 1553ccc00a7SMahesh Salgaonkar } 1563ccc00a7SMahesh Salgaonkar 157a5a05b91SHari Bathini /* 158961cf26aSHari Bathini * Returns true, if there are no holes in memory area between d_start to d_end, 159961cf26aSHari Bathini * false otherwise. 160a5a05b91SHari Bathini */ 161961cf26aSHari Bathini static bool is_fadump_mem_area_contiguous(u64 d_start, u64 d_end) 162a5a05b91SHari Bathini { 163a5a05b91SHari Bathini struct memblock_region *reg; 164961cf26aSHari Bathini bool ret = false; 165961cf26aSHari Bathini u64 start, end; 166a5a05b91SHari Bathini 167a5a05b91SHari Bathini for_each_memblock(memory, reg) { 168961cf26aSHari Bathini start = max_t(u64, d_start, reg->base); 169961cf26aSHari Bathini end = min_t(u64, d_end, (reg->base + reg->size)); 170961cf26aSHari Bathini if (d_start < end) { 171961cf26aSHari Bathini /* Memory hole from d_start to start */ 172961cf26aSHari Bathini if (start > d_start) 173a5a05b91SHari Bathini break; 174a5a05b91SHari Bathini 175961cf26aSHari Bathini if (end == d_end) { 176961cf26aSHari Bathini ret = true; 177a5a05b91SHari Bathini break; 178a5a05b91SHari Bathini } 179a5a05b91SHari Bathini 180961cf26aSHari Bathini d_start = end + 1; 181a5a05b91SHari Bathini } 182a5a05b91SHari Bathini } 183a5a05b91SHari Bathini 184a5a05b91SHari Bathini return ret; 185a5a05b91SHari Bathini } 186a5a05b91SHari Bathini 187f86593beSMahesh Salgaonkar /* 188961cf26aSHari Bathini * Returns true, if there are no holes in boot memory area, 189961cf26aSHari Bathini * false otherwise. 190961cf26aSHari Bathini */ 1917f0ad11dSHari Bathini bool is_fadump_boot_mem_contiguous(void) 192961cf26aSHari Bathini { 193961cf26aSHari Bathini return is_fadump_mem_area_contiguous(0, fw_dump.boot_memory_size); 194961cf26aSHari Bathini } 195961cf26aSHari Bathini 196961cf26aSHari Bathini /* 197f86593beSMahesh Salgaonkar * Returns true, if there are no holes in reserved memory area, 198f86593beSMahesh Salgaonkar * false otherwise. 199f86593beSMahesh Salgaonkar */ 2007f0ad11dSHari Bathini bool is_fadump_reserved_mem_contiguous(void) 201f86593beSMahesh Salgaonkar { 202961cf26aSHari Bathini u64 d_start, d_end; 203f86593beSMahesh Salgaonkar 204961cf26aSHari Bathini d_start = fw_dump.reserve_dump_area_start; 205961cf26aSHari Bathini d_end = d_start + fw_dump.reserve_dump_area_size; 206961cf26aSHari Bathini return is_fadump_mem_area_contiguous(d_start, d_end); 207f86593beSMahesh Salgaonkar } 208f86593beSMahesh Salgaonkar 2093ccc00a7SMahesh Salgaonkar /* Print firmware assisted dump configurations for debugging purpose. */ 2103ccc00a7SMahesh Salgaonkar static void fadump_show_config(void) 2113ccc00a7SMahesh Salgaonkar { 2123ccc00a7SMahesh Salgaonkar pr_debug("Support for firmware-assisted dump (fadump): %s\n", 2133ccc00a7SMahesh Salgaonkar (fw_dump.fadump_supported ? "present" : "no support")); 2143ccc00a7SMahesh Salgaonkar 2153ccc00a7SMahesh Salgaonkar if (!fw_dump.fadump_supported) 2163ccc00a7SMahesh Salgaonkar return; 2173ccc00a7SMahesh Salgaonkar 2183ccc00a7SMahesh Salgaonkar pr_debug("Fadump enabled : %s\n", 2193ccc00a7SMahesh Salgaonkar (fw_dump.fadump_enabled ? "yes" : "no")); 2203ccc00a7SMahesh Salgaonkar pr_debug("Dump Active : %s\n", 2213ccc00a7SMahesh Salgaonkar (fw_dump.dump_active ? "yes" : "no")); 2223ccc00a7SMahesh Salgaonkar pr_debug("Dump section sizes:\n"); 2233ccc00a7SMahesh Salgaonkar pr_debug(" CPU state data size: %lx\n", fw_dump.cpu_state_data_size); 2243ccc00a7SMahesh Salgaonkar pr_debug(" HPTE region size : %lx\n", fw_dump.hpte_region_size); 2253ccc00a7SMahesh Salgaonkar pr_debug("Boot memory size : %lx\n", fw_dump.boot_memory_size); 2263ccc00a7SMahesh Salgaonkar } 2273ccc00a7SMahesh Salgaonkar 228eb39c880SMahesh Salgaonkar /** 229eb39c880SMahesh Salgaonkar * fadump_calculate_reserve_size(): reserve variable boot area 5% of System RAM 230eb39c880SMahesh Salgaonkar * 231eb39c880SMahesh Salgaonkar * Function to find the largest memory size we need to reserve during early 232eb39c880SMahesh Salgaonkar * boot process. This will be the size of the memory that is required for a 233eb39c880SMahesh Salgaonkar * kernel to boot successfully. 234eb39c880SMahesh Salgaonkar * 235eb39c880SMahesh Salgaonkar * This function has been taken from phyp-assisted dump feature implementation. 236eb39c880SMahesh Salgaonkar * 237eb39c880SMahesh Salgaonkar * returns larger of 256MB or 5% rounded down to multiples of 256MB. 238eb39c880SMahesh Salgaonkar * 239eb39c880SMahesh Salgaonkar * TODO: Come up with better approach to find out more accurate memory size 240eb39c880SMahesh Salgaonkar * that is required for a kernel to boot successfully. 241eb39c880SMahesh Salgaonkar * 242eb39c880SMahesh Salgaonkar */ 2437b1b3b48SHari Bathini static inline u64 fadump_calculate_reserve_size(void) 244eb39c880SMahesh Salgaonkar { 2457b1b3b48SHari Bathini u64 base, size, bootmem_min; 24611550dc0SHari Bathini int ret; 247eb39c880SMahesh Salgaonkar 24881d9eca5SHari Bathini if (fw_dump.reserve_bootvar) 24981d9eca5SHari Bathini pr_warn("'fadump_reserve_mem=' parameter is deprecated in favor of 'crashkernel=' parameter.\n"); 25081d9eca5SHari Bathini 251eb39c880SMahesh Salgaonkar /* 25211550dc0SHari Bathini * Check if the size is specified through crashkernel= cmdline 253e7467dc6SHari Bathini * option. If yes, then use that but ignore base as fadump reserves 254e7467dc6SHari Bathini * memory at a predefined offset. 255eb39c880SMahesh Salgaonkar */ 25611550dc0SHari Bathini ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(), 25711550dc0SHari Bathini &size, &base); 25811550dc0SHari Bathini if (ret == 0 && size > 0) { 25948a316e3SHari Bathini unsigned long max_size; 26048a316e3SHari Bathini 26181d9eca5SHari Bathini if (fw_dump.reserve_bootvar) 26281d9eca5SHari Bathini pr_info("Using 'crashkernel=' parameter for memory reservation.\n"); 26381d9eca5SHari Bathini 26411550dc0SHari Bathini fw_dump.reserve_bootvar = (unsigned long)size; 26548a316e3SHari Bathini 26648a316e3SHari Bathini /* 26748a316e3SHari Bathini * Adjust if the boot memory size specified is above 26848a316e3SHari Bathini * the upper limit. 26948a316e3SHari Bathini */ 27048a316e3SHari Bathini max_size = memblock_phys_mem_size() / MAX_BOOT_MEM_RATIO; 27148a316e3SHari Bathini if (fw_dump.reserve_bootvar > max_size) { 27248a316e3SHari Bathini fw_dump.reserve_bootvar = max_size; 27348a316e3SHari Bathini pr_info("Adjusted boot memory size to %luMB\n", 27448a316e3SHari Bathini (fw_dump.reserve_bootvar >> 20)); 27548a316e3SHari Bathini } 27648a316e3SHari Bathini 277eb39c880SMahesh Salgaonkar return fw_dump.reserve_bootvar; 27881d9eca5SHari Bathini } else if (fw_dump.reserve_bootvar) { 27981d9eca5SHari Bathini /* 28081d9eca5SHari Bathini * 'fadump_reserve_mem=' is being used to reserve memory 28181d9eca5SHari Bathini * for firmware-assisted dump. 28281d9eca5SHari Bathini */ 28381d9eca5SHari Bathini return fw_dump.reserve_bootvar; 28411550dc0SHari Bathini } 285eb39c880SMahesh Salgaonkar 286eb39c880SMahesh Salgaonkar /* divide by 20 to get 5% of value */ 28748a316e3SHari Bathini size = memblock_phys_mem_size() / 20; 288eb39c880SMahesh Salgaonkar 289eb39c880SMahesh Salgaonkar /* round it down in multiples of 256 */ 290eb39c880SMahesh Salgaonkar size = size & ~0x0FFFFFFFUL; 291eb39c880SMahesh Salgaonkar 292eb39c880SMahesh Salgaonkar /* Truncate to memory_limit. We don't want to over reserve the memory.*/ 293eb39c880SMahesh Salgaonkar if (memory_limit && size > memory_limit) 294eb39c880SMahesh Salgaonkar size = memory_limit; 295eb39c880SMahesh Salgaonkar 2967b1b3b48SHari Bathini bootmem_min = fw_dump.ops->fadump_get_bootmem_min(); 2977b1b3b48SHari Bathini return (size > bootmem_min ? size : bootmem_min); 298eb39c880SMahesh Salgaonkar } 299eb39c880SMahesh Salgaonkar 300eb39c880SMahesh Salgaonkar /* 301eb39c880SMahesh Salgaonkar * Calculate the total memory size required to be reserved for 302eb39c880SMahesh Salgaonkar * firmware-assisted dump registration. 303eb39c880SMahesh Salgaonkar */ 304eb39c880SMahesh Salgaonkar static unsigned long get_fadump_area_size(void) 305eb39c880SMahesh Salgaonkar { 306eb39c880SMahesh Salgaonkar unsigned long size = 0; 307eb39c880SMahesh Salgaonkar 308eb39c880SMahesh Salgaonkar size += fw_dump.cpu_state_data_size; 309eb39c880SMahesh Salgaonkar size += fw_dump.hpte_region_size; 310eb39c880SMahesh Salgaonkar size += fw_dump.boot_memory_size; 3112df173d9SMahesh Salgaonkar size += sizeof(struct fadump_crash_info_header); 3122df173d9SMahesh Salgaonkar size += sizeof(struct elfhdr); /* ELF core header.*/ 313ebaeb5aeSMahesh Salgaonkar size += sizeof(struct elf_phdr); /* place holder for cpu notes */ 3142df173d9SMahesh Salgaonkar /* Program headers for crash memory regions. */ 3152df173d9SMahesh Salgaonkar size += sizeof(struct elf_phdr) * (memblock_num_regions(memory) + 2); 316eb39c880SMahesh Salgaonkar 317eb39c880SMahesh Salgaonkar size = PAGE_ALIGN(size); 318742a265aSHari Bathini 319742a265aSHari Bathini /* This is to hold kernel metadata on platforms that support it */ 320742a265aSHari Bathini size += (fw_dump.ops->fadump_get_metadata_size ? 321742a265aSHari Bathini fw_dump.ops->fadump_get_metadata_size() : 0); 322eb39c880SMahesh Salgaonkar return size; 323eb39c880SMahesh Salgaonkar } 324eb39c880SMahesh Salgaonkar 325eb39c880SMahesh Salgaonkar int __init fadump_reserve_mem(void) 326eb39c880SMahesh Salgaonkar { 3277b1b3b48SHari Bathini u64 base, size, mem_boundary, bootmem_min, align = PAGE_SIZE; 328579ca1a2SHari Bathini bool is_memblock_bottom_up = memblock_bottom_up(); 3296abec12cSHari Bathini int ret = 1; 330eb39c880SMahesh Salgaonkar 331eb39c880SMahesh Salgaonkar if (!fw_dump.fadump_enabled) 332eb39c880SMahesh Salgaonkar return 0; 333eb39c880SMahesh Salgaonkar 334eb39c880SMahesh Salgaonkar if (!fw_dump.fadump_supported) { 3356abec12cSHari Bathini pr_info("Firmware-Assisted Dump is not supported on this hardware\n"); 3366abec12cSHari Bathini goto error_out; 337eb39c880SMahesh Salgaonkar } 338742a265aSHari Bathini 3393ccc00a7SMahesh Salgaonkar /* 3403ccc00a7SMahesh Salgaonkar * Initialize boot memory size 3413ccc00a7SMahesh Salgaonkar * If dump is active then we have already calculated the size during 3423ccc00a7SMahesh Salgaonkar * first kernel. 3433ccc00a7SMahesh Salgaonkar */ 344f3512011SHari Bathini if (!fw_dump.dump_active) { 3456abec12cSHari Bathini fw_dump.boot_memory_size = 3466abec12cSHari Bathini PAGE_ALIGN(fadump_calculate_reserve_size()); 347a4e92ce8SMahesh Salgaonkar #ifdef CONFIG_CMA 348579ca1a2SHari Bathini if (!fw_dump.nocma) { 349579ca1a2SHari Bathini align = FADUMP_CMA_ALIGNMENT; 350a4e92ce8SMahesh Salgaonkar fw_dump.boot_memory_size = 351579ca1a2SHari Bathini ALIGN(fw_dump.boot_memory_size, align); 352579ca1a2SHari Bathini } 353a4e92ce8SMahesh Salgaonkar #endif 3547b1b3b48SHari Bathini 3557b1b3b48SHari Bathini bootmem_min = fw_dump.ops->fadump_get_bootmem_min(); 3567b1b3b48SHari Bathini if (fw_dump.boot_memory_size < bootmem_min) { 3577b1b3b48SHari Bathini pr_err("Can't enable fadump with boot memory size (0x%lx) less than 0x%llx\n", 3587b1b3b48SHari Bathini fw_dump.boot_memory_size, bootmem_min); 3597b1b3b48SHari Bathini goto error_out; 3607b1b3b48SHari Bathini } 361a4e92ce8SMahesh Salgaonkar } 362eb39c880SMahesh Salgaonkar 363eb39c880SMahesh Salgaonkar /* 364eb39c880SMahesh Salgaonkar * Calculate the memory boundary. 365eb39c880SMahesh Salgaonkar * If memory_limit is less than actual memory boundary then reserve 366eb39c880SMahesh Salgaonkar * the memory for fadump beyond the memory_limit and adjust the 367eb39c880SMahesh Salgaonkar * memory_limit accordingly, so that the running kernel can run with 368eb39c880SMahesh Salgaonkar * specified memory_limit. 369eb39c880SMahesh Salgaonkar */ 370eb39c880SMahesh Salgaonkar if (memory_limit && memory_limit < memblock_end_of_DRAM()) { 371eb39c880SMahesh Salgaonkar size = get_fadump_area_size(); 372eb39c880SMahesh Salgaonkar if ((memory_limit + size) < memblock_end_of_DRAM()) 373eb39c880SMahesh Salgaonkar memory_limit += size; 374eb39c880SMahesh Salgaonkar else 375eb39c880SMahesh Salgaonkar memory_limit = memblock_end_of_DRAM(); 376eb39c880SMahesh Salgaonkar printk(KERN_INFO "Adjusted memory_limit for firmware-assisted" 377a84fcd46SSuzuki Poulose " dump, now %#016llx\n", memory_limit); 378eb39c880SMahesh Salgaonkar } 379eb39c880SMahesh Salgaonkar if (memory_limit) 3806abec12cSHari Bathini mem_boundary = memory_limit; 381eb39c880SMahesh Salgaonkar else 3826abec12cSHari Bathini mem_boundary = memblock_end_of_DRAM(); 383eb39c880SMahesh Salgaonkar 3846abec12cSHari Bathini base = fw_dump.boot_memory_size; 3858255da95SHari Bathini size = get_fadump_area_size(); 3868255da95SHari Bathini fw_dump.reserve_dump_area_size = size; 387eb39c880SMahesh Salgaonkar if (fw_dump.dump_active) { 388b71a693dSMahesh Salgaonkar pr_info("Firmware-assisted dump is active.\n"); 389b71a693dSMahesh Salgaonkar 39085975387SHari Bathini #ifdef CONFIG_HUGETLB_PAGE 39185975387SHari Bathini /* 39285975387SHari Bathini * FADump capture kernel doesn't care much about hugepages. 39385975387SHari Bathini * In fact, handling hugepages in capture kernel is asking for 39485975387SHari Bathini * trouble. So, disable HugeTLB support when fadump is active. 39585975387SHari Bathini */ 39685975387SHari Bathini hugetlb_disabled = true; 39785975387SHari Bathini #endif 398eb39c880SMahesh Salgaonkar /* 399eb39c880SMahesh Salgaonkar * If last boot has crashed then reserve all the memory 400b2a815a5SHari Bathini * above boot memory size so that we don't touch it until 401eb39c880SMahesh Salgaonkar * dump is written to disk by userspace tool. This memory 402b2a815a5SHari Bathini * can be released for general use by invalidating fadump. 403eb39c880SMahesh Salgaonkar */ 404b2a815a5SHari Bathini fadump_reserve_crash_area(base); 4052df173d9SMahesh Salgaonkar 406f3512011SHari Bathini pr_debug("fadumphdr_addr = %#016lx\n", fw_dump.fadumphdr_addr); 407f3512011SHari Bathini pr_debug("Reserve dump area start address: 0x%lx\n", 408f3512011SHari Bathini fw_dump.reserve_dump_area_start); 4098255da95SHari Bathini } else { 4108255da95SHari Bathini /* 411f6e6bedbSHari Bathini * Reserve memory at an offset closer to bottom of the RAM to 412579ca1a2SHari Bathini * minimize the impact of memory hot-remove operation. 413f6e6bedbSHari Bathini */ 414579ca1a2SHari Bathini memblock_set_bottom_up(true); 415579ca1a2SHari Bathini base = memblock_find_in_range(base, mem_boundary, size, align); 4166abec12cSHari Bathini 417579ca1a2SHari Bathini /* Restore the previous allocation mode */ 418579ca1a2SHari Bathini memblock_set_bottom_up(is_memblock_bottom_up); 419f6e6bedbSHari Bathini 420579ca1a2SHari Bathini if (!base) { 421742a265aSHari Bathini pr_err("Failed to find memory chunk for reservation!\n"); 422742a265aSHari Bathini goto error_out; 423742a265aSHari Bathini } 424742a265aSHari Bathini fw_dump.reserve_dump_area_start = base; 425742a265aSHari Bathini 426742a265aSHari Bathini /* 427742a265aSHari Bathini * Calculate the kernel metadata address and register it with 428742a265aSHari Bathini * f/w if the platform supports. 429742a265aSHari Bathini */ 430742a265aSHari Bathini if (fw_dump.ops->fadump_setup_metadata && 431742a265aSHari Bathini (fw_dump.ops->fadump_setup_metadata(&fw_dump) < 0)) 432742a265aSHari Bathini goto error_out; 433742a265aSHari Bathini 434742a265aSHari Bathini if (memblock_reserve(base, size)) { 4356abec12cSHari Bathini pr_err("Failed to reserve memory!\n"); 4366abec12cSHari Bathini goto error_out; 4376abec12cSHari Bathini } 4386abec12cSHari Bathini 4396abec12cSHari Bathini pr_info("Reserved %lldMB of memory at %#016llx (System RAM: %lldMB)\n", 4406abec12cSHari Bathini (size >> 20), base, (memblock_phys_mem_size() >> 20)); 441f6e6bedbSHari Bathini 4426abec12cSHari Bathini ret = fadump_cma_init(); 443a4e92ce8SMahesh Salgaonkar } 4446abec12cSHari Bathini 4456abec12cSHari Bathini return ret; 4466abec12cSHari Bathini error_out: 4476abec12cSHari Bathini fw_dump.fadump_enabled = 0; 4486abec12cSHari Bathini return 0; 449eb39c880SMahesh Salgaonkar } 450eb39c880SMahesh Salgaonkar 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 615e4fc48fbSHari Bathini static void fadump_free_mem_ranges(struct fadump_mrange_info *mrange_info) 6161bd6a1c4SHari Bathini { 617e4fc48fbSHari Bathini kfree(mrange_info->mem_ranges); 618e4fc48fbSHari Bathini mrange_info->mem_ranges = NULL; 619e4fc48fbSHari Bathini mrange_info->mem_ranges_sz = 0; 620e4fc48fbSHari Bathini mrange_info->max_mem_ranges = 0; 6211bd6a1c4SHari Bathini } 6221bd6a1c4SHari Bathini 6231bd6a1c4SHari Bathini /* 624e4fc48fbSHari Bathini * Allocate or reallocate mem_ranges array in incremental units 6251bd6a1c4SHari Bathini * of PAGE_SIZE. 6261bd6a1c4SHari Bathini */ 627e4fc48fbSHari Bathini static int fadump_alloc_mem_ranges(struct fadump_mrange_info *mrange_info) 6281bd6a1c4SHari Bathini { 629e4fc48fbSHari Bathini struct fadump_memory_range *new_array; 6301bd6a1c4SHari Bathini u64 new_size; 6311bd6a1c4SHari Bathini 632e4fc48fbSHari Bathini new_size = mrange_info->mem_ranges_sz + PAGE_SIZE; 633e4fc48fbSHari Bathini pr_debug("Allocating %llu bytes of memory for %s memory ranges\n", 634e4fc48fbSHari Bathini new_size, mrange_info->name); 6351bd6a1c4SHari Bathini 636e4fc48fbSHari Bathini new_array = krealloc(mrange_info->mem_ranges, new_size, GFP_KERNEL); 6371bd6a1c4SHari Bathini if (new_array == NULL) { 638e4fc48fbSHari Bathini pr_err("Insufficient memory for setting up %s memory ranges\n", 639e4fc48fbSHari Bathini mrange_info->name); 640e4fc48fbSHari Bathini fadump_free_mem_ranges(mrange_info); 6411bd6a1c4SHari Bathini return -ENOMEM; 6421bd6a1c4SHari Bathini } 6431bd6a1c4SHari Bathini 644e4fc48fbSHari Bathini mrange_info->mem_ranges = new_array; 645e4fc48fbSHari Bathini mrange_info->mem_ranges_sz = new_size; 646e4fc48fbSHari Bathini mrange_info->max_mem_ranges = (new_size / 647e4fc48fbSHari Bathini sizeof(struct fadump_memory_range)); 6481bd6a1c4SHari Bathini return 0; 6491bd6a1c4SHari Bathini } 6501bd6a1c4SHari Bathini 651e4fc48fbSHari Bathini static inline int fadump_add_mem_range(struct fadump_mrange_info *mrange_info, 652e4fc48fbSHari Bathini u64 base, u64 end) 6532df173d9SMahesh Salgaonkar { 654e4fc48fbSHari Bathini struct fadump_memory_range *mem_ranges = mrange_info->mem_ranges; 655ced1bf52SHari Bathini bool is_adjacent = false; 656e4fc48fbSHari Bathini u64 start, size; 657ced1bf52SHari Bathini 6582df173d9SMahesh Salgaonkar if (base == end) 6591bd6a1c4SHari Bathini return 0; 6601bd6a1c4SHari Bathini 661ced1bf52SHari Bathini /* 662ced1bf52SHari Bathini * Fold adjacent memory ranges to bring down the memory ranges/ 663ced1bf52SHari Bathini * PT_LOAD segments count. 664ced1bf52SHari Bathini */ 665e4fc48fbSHari Bathini if (mrange_info->mem_range_cnt) { 666e4fc48fbSHari Bathini start = mem_ranges[mrange_info->mem_range_cnt - 1].base; 667e4fc48fbSHari Bathini size = mem_ranges[mrange_info->mem_range_cnt - 1].size; 668ced1bf52SHari Bathini 669ced1bf52SHari Bathini if ((start + size) == base) 670ced1bf52SHari Bathini is_adjacent = true; 671ced1bf52SHari Bathini } 672ced1bf52SHari Bathini if (!is_adjacent) { 673ced1bf52SHari Bathini /* resize the array on reaching the limit */ 674e4fc48fbSHari Bathini if (mrange_info->mem_range_cnt == mrange_info->max_mem_ranges) { 6751bd6a1c4SHari Bathini int ret; 6761bd6a1c4SHari Bathini 677e4fc48fbSHari Bathini ret = fadump_alloc_mem_ranges(mrange_info); 6781bd6a1c4SHari Bathini if (ret) 6791bd6a1c4SHari Bathini return ret; 680e4fc48fbSHari Bathini 681e4fc48fbSHari Bathini /* Update to the new resized array */ 682e4fc48fbSHari Bathini mem_ranges = mrange_info->mem_ranges; 6831bd6a1c4SHari Bathini } 6842df173d9SMahesh Salgaonkar 685ced1bf52SHari Bathini start = base; 686e4fc48fbSHari Bathini mem_ranges[mrange_info->mem_range_cnt].base = start; 687e4fc48fbSHari Bathini mrange_info->mem_range_cnt++; 688ced1bf52SHari Bathini } 689ced1bf52SHari Bathini 690e4fc48fbSHari Bathini mem_ranges[mrange_info->mem_range_cnt - 1].size = (end - start); 691e4fc48fbSHari Bathini pr_debug("%s_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n", 692e4fc48fbSHari Bathini mrange_info->name, (mrange_info->mem_range_cnt - 1), 693e4fc48fbSHari Bathini start, end - 1, (end - start)); 6941bd6a1c4SHari Bathini return 0; 6952df173d9SMahesh Salgaonkar } 6962df173d9SMahesh Salgaonkar 697e4fc48fbSHari Bathini static int fadump_exclude_reserved_area(u64 start, u64 end) 6982df173d9SMahesh Salgaonkar { 699e4fc48fbSHari Bathini u64 ra_start, ra_end; 7001bd6a1c4SHari Bathini int ret = 0; 7012df173d9SMahesh Salgaonkar 7022df173d9SMahesh Salgaonkar ra_start = fw_dump.reserve_dump_area_start; 7032df173d9SMahesh Salgaonkar ra_end = ra_start + fw_dump.reserve_dump_area_size; 7042df173d9SMahesh Salgaonkar 7052df173d9SMahesh Salgaonkar if ((ra_start < end) && (ra_end > start)) { 7062df173d9SMahesh Salgaonkar if ((start < ra_start) && (end > ra_end)) { 707e4fc48fbSHari Bathini ret = fadump_add_mem_range(&crash_mrange_info, 708e4fc48fbSHari Bathini start, ra_start); 7091bd6a1c4SHari Bathini if (ret) 7101bd6a1c4SHari Bathini return ret; 7111bd6a1c4SHari Bathini 712e4fc48fbSHari Bathini ret = fadump_add_mem_range(&crash_mrange_info, 713e4fc48fbSHari Bathini ra_end, end); 7142df173d9SMahesh Salgaonkar } else if (start < ra_start) { 715e4fc48fbSHari Bathini ret = fadump_add_mem_range(&crash_mrange_info, 716e4fc48fbSHari Bathini start, ra_start); 7172df173d9SMahesh Salgaonkar } else if (ra_end < end) { 718e4fc48fbSHari Bathini ret = fadump_add_mem_range(&crash_mrange_info, 719e4fc48fbSHari Bathini ra_end, end); 7202df173d9SMahesh Salgaonkar } 7212df173d9SMahesh Salgaonkar } else 722e4fc48fbSHari Bathini ret = fadump_add_mem_range(&crash_mrange_info, start, end); 7231bd6a1c4SHari Bathini 7241bd6a1c4SHari Bathini return ret; 7252df173d9SMahesh Salgaonkar } 7262df173d9SMahesh Salgaonkar 7272df173d9SMahesh Salgaonkar static int fadump_init_elfcore_header(char *bufp) 7282df173d9SMahesh Salgaonkar { 7292df173d9SMahesh Salgaonkar struct elfhdr *elf; 7302df173d9SMahesh Salgaonkar 7312df173d9SMahesh Salgaonkar elf = (struct elfhdr *) bufp; 7322df173d9SMahesh Salgaonkar bufp += sizeof(struct elfhdr); 7332df173d9SMahesh Salgaonkar memcpy(elf->e_ident, ELFMAG, SELFMAG); 7342df173d9SMahesh Salgaonkar elf->e_ident[EI_CLASS] = ELF_CLASS; 7352df173d9SMahesh Salgaonkar elf->e_ident[EI_DATA] = ELF_DATA; 7362df173d9SMahesh Salgaonkar elf->e_ident[EI_VERSION] = EV_CURRENT; 7372df173d9SMahesh Salgaonkar elf->e_ident[EI_OSABI] = ELF_OSABI; 7382df173d9SMahesh Salgaonkar memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD); 7392df173d9SMahesh Salgaonkar elf->e_type = ET_CORE; 7402df173d9SMahesh Salgaonkar elf->e_machine = ELF_ARCH; 7412df173d9SMahesh Salgaonkar elf->e_version = EV_CURRENT; 7422df173d9SMahesh Salgaonkar elf->e_entry = 0; 7432df173d9SMahesh Salgaonkar elf->e_phoff = sizeof(struct elfhdr); 7442df173d9SMahesh Salgaonkar elf->e_shoff = 0; 745d8bced27SDaniel Axtens #if defined(_CALL_ELF) 746d8bced27SDaniel Axtens elf->e_flags = _CALL_ELF; 747d8bced27SDaniel Axtens #else 748d8bced27SDaniel Axtens elf->e_flags = 0; 749d8bced27SDaniel Axtens #endif 7502df173d9SMahesh Salgaonkar elf->e_ehsize = sizeof(struct elfhdr); 7512df173d9SMahesh Salgaonkar elf->e_phentsize = sizeof(struct elf_phdr); 7522df173d9SMahesh Salgaonkar elf->e_phnum = 0; 7532df173d9SMahesh Salgaonkar elf->e_shentsize = 0; 7542df173d9SMahesh Salgaonkar elf->e_shnum = 0; 7552df173d9SMahesh Salgaonkar elf->e_shstrndx = 0; 7562df173d9SMahesh Salgaonkar 7572df173d9SMahesh Salgaonkar return 0; 7582df173d9SMahesh Salgaonkar } 7592df173d9SMahesh Salgaonkar 7602df173d9SMahesh Salgaonkar /* 7612df173d9SMahesh Salgaonkar * Traverse through memblock structure and setup crash memory ranges. These 7622df173d9SMahesh Salgaonkar * ranges will be used create PT_LOAD program headers in elfcore header. 7632df173d9SMahesh Salgaonkar */ 7641bd6a1c4SHari Bathini static int fadump_setup_crash_memory_ranges(void) 7652df173d9SMahesh Salgaonkar { 7662df173d9SMahesh Salgaonkar struct memblock_region *reg; 767e4fc48fbSHari Bathini u64 start, end; 7681bd6a1c4SHari Bathini int ret; 7692df173d9SMahesh Salgaonkar 7702df173d9SMahesh Salgaonkar pr_debug("Setup crash memory ranges.\n"); 771e4fc48fbSHari Bathini crash_mrange_info.mem_range_cnt = 0; 772ced1bf52SHari Bathini 7732df173d9SMahesh Salgaonkar /* 7742df173d9SMahesh Salgaonkar * add the first memory chunk (RMA_START through boot_memory_size) as 7752df173d9SMahesh Salgaonkar * a separate memory chunk. The reason is, at the time crash firmware 7762df173d9SMahesh Salgaonkar * will move the content of this memory chunk to different location 7772df173d9SMahesh Salgaonkar * specified during fadump registration. We need to create a separate 7782df173d9SMahesh Salgaonkar * program header for this chunk with the correct offset. 7792df173d9SMahesh Salgaonkar */ 780e4fc48fbSHari Bathini ret = fadump_add_mem_range(&crash_mrange_info, 781e4fc48fbSHari Bathini RMA_START, fw_dump.boot_memory_size); 7821bd6a1c4SHari Bathini if (ret) 7831bd6a1c4SHari Bathini return ret; 7842df173d9SMahesh Salgaonkar 7852df173d9SMahesh Salgaonkar for_each_memblock(memory, reg) { 786e4fc48fbSHari Bathini start = (u64)reg->base; 787e4fc48fbSHari Bathini end = start + (u64)reg->size; 788a77af552SHari Bathini 789a77af552SHari Bathini /* 790a77af552SHari Bathini * skip the first memory chunk that is already added (RMA_START 791a77af552SHari Bathini * through boot_memory_size). This logic needs a relook if and 792a77af552SHari Bathini * when RMA_START changes to a non-zero value. 793a77af552SHari Bathini */ 794a77af552SHari Bathini BUILD_BUG_ON(RMA_START != 0); 795a77af552SHari Bathini if (start < fw_dump.boot_memory_size) { 796a77af552SHari Bathini if (end > fw_dump.boot_memory_size) 7972df173d9SMahesh Salgaonkar start = fw_dump.boot_memory_size; 798a77af552SHari Bathini else 799a77af552SHari Bathini continue; 800a77af552SHari Bathini } 8012df173d9SMahesh Salgaonkar 8022df173d9SMahesh Salgaonkar /* add this range excluding the reserved dump area. */ 8031bd6a1c4SHari Bathini ret = fadump_exclude_reserved_area(start, end); 8041bd6a1c4SHari Bathini if (ret) 8051bd6a1c4SHari Bathini return ret; 8062df173d9SMahesh Salgaonkar } 8071bd6a1c4SHari Bathini 8081bd6a1c4SHari Bathini return 0; 8092df173d9SMahesh Salgaonkar } 8102df173d9SMahesh Salgaonkar 811d34c5f26SMahesh Salgaonkar /* 812d34c5f26SMahesh Salgaonkar * If the given physical address falls within the boot memory region then 813d34c5f26SMahesh Salgaonkar * return the relocated address that points to the dump region reserved 814d34c5f26SMahesh Salgaonkar * for saving initial boot memory contents. 815d34c5f26SMahesh Salgaonkar */ 816d34c5f26SMahesh Salgaonkar static inline unsigned long fadump_relocate(unsigned long paddr) 817d34c5f26SMahesh Salgaonkar { 818d34c5f26SMahesh Salgaonkar if (paddr > RMA_START && paddr < fw_dump.boot_memory_size) 81941a65d16SHari Bathini return fw_dump.boot_mem_dest_addr + paddr; 820d34c5f26SMahesh Salgaonkar else 821d34c5f26SMahesh Salgaonkar return paddr; 822d34c5f26SMahesh Salgaonkar } 823d34c5f26SMahesh Salgaonkar 8242df173d9SMahesh Salgaonkar static int fadump_create_elfcore_headers(char *bufp) 8252df173d9SMahesh Salgaonkar { 8262df173d9SMahesh Salgaonkar struct elfhdr *elf; 8272df173d9SMahesh Salgaonkar struct elf_phdr *phdr; 8282df173d9SMahesh Salgaonkar int i; 8292df173d9SMahesh Salgaonkar 8302df173d9SMahesh Salgaonkar fadump_init_elfcore_header(bufp); 8312df173d9SMahesh Salgaonkar elf = (struct elfhdr *)bufp; 8322df173d9SMahesh Salgaonkar bufp += sizeof(struct elfhdr); 8332df173d9SMahesh Salgaonkar 834ebaeb5aeSMahesh Salgaonkar /* 835ebaeb5aeSMahesh Salgaonkar * setup ELF PT_NOTE, place holder for cpu notes info. The notes info 836ebaeb5aeSMahesh Salgaonkar * will be populated during second kernel boot after crash. Hence 837ebaeb5aeSMahesh Salgaonkar * this PT_NOTE will always be the first elf note. 838ebaeb5aeSMahesh Salgaonkar * 839ebaeb5aeSMahesh Salgaonkar * NOTE: Any new ELF note addition should be placed after this note. 840ebaeb5aeSMahesh Salgaonkar */ 841ebaeb5aeSMahesh Salgaonkar phdr = (struct elf_phdr *)bufp; 842ebaeb5aeSMahesh Salgaonkar bufp += sizeof(struct elf_phdr); 843ebaeb5aeSMahesh Salgaonkar phdr->p_type = PT_NOTE; 844ebaeb5aeSMahesh Salgaonkar phdr->p_flags = 0; 845ebaeb5aeSMahesh Salgaonkar phdr->p_vaddr = 0; 846ebaeb5aeSMahesh Salgaonkar phdr->p_align = 0; 847ebaeb5aeSMahesh Salgaonkar 848ebaeb5aeSMahesh Salgaonkar phdr->p_offset = 0; 849ebaeb5aeSMahesh Salgaonkar phdr->p_paddr = 0; 850ebaeb5aeSMahesh Salgaonkar phdr->p_filesz = 0; 851ebaeb5aeSMahesh Salgaonkar phdr->p_memsz = 0; 852ebaeb5aeSMahesh Salgaonkar 853ebaeb5aeSMahesh Salgaonkar (elf->e_phnum)++; 854ebaeb5aeSMahesh Salgaonkar 855d34c5f26SMahesh Salgaonkar /* setup ELF PT_NOTE for vmcoreinfo */ 856d34c5f26SMahesh Salgaonkar phdr = (struct elf_phdr *)bufp; 857d34c5f26SMahesh Salgaonkar bufp += sizeof(struct elf_phdr); 858d34c5f26SMahesh Salgaonkar phdr->p_type = PT_NOTE; 859d34c5f26SMahesh Salgaonkar phdr->p_flags = 0; 860d34c5f26SMahesh Salgaonkar phdr->p_vaddr = 0; 861d34c5f26SMahesh Salgaonkar phdr->p_align = 0; 862d34c5f26SMahesh Salgaonkar 863d34c5f26SMahesh Salgaonkar phdr->p_paddr = fadump_relocate(paddr_vmcoreinfo_note()); 864d34c5f26SMahesh Salgaonkar phdr->p_offset = phdr->p_paddr; 8655203f499SXunlei Pang phdr->p_memsz = phdr->p_filesz = VMCOREINFO_NOTE_SIZE; 866d34c5f26SMahesh Salgaonkar 867d34c5f26SMahesh Salgaonkar /* Increment number of program headers. */ 868d34c5f26SMahesh Salgaonkar (elf->e_phnum)++; 869d34c5f26SMahesh Salgaonkar 8702df173d9SMahesh Salgaonkar /* setup PT_LOAD sections. */ 8712df173d9SMahesh Salgaonkar 872e4fc48fbSHari Bathini for (i = 0; i < crash_mrange_info.mem_range_cnt; i++) { 873e4fc48fbSHari Bathini u64 mbase, msize; 8742df173d9SMahesh Salgaonkar 875e4fc48fbSHari Bathini mbase = crash_mrange_info.mem_ranges[i].base; 876e4fc48fbSHari Bathini msize = crash_mrange_info.mem_ranges[i].size; 8772df173d9SMahesh Salgaonkar if (!msize) 8782df173d9SMahesh Salgaonkar continue; 8792df173d9SMahesh Salgaonkar 8802df173d9SMahesh Salgaonkar phdr = (struct elf_phdr *)bufp; 8812df173d9SMahesh Salgaonkar bufp += sizeof(struct elf_phdr); 8822df173d9SMahesh Salgaonkar phdr->p_type = PT_LOAD; 8832df173d9SMahesh Salgaonkar phdr->p_flags = PF_R|PF_W|PF_X; 8842df173d9SMahesh Salgaonkar phdr->p_offset = mbase; 8852df173d9SMahesh Salgaonkar 8862df173d9SMahesh Salgaonkar if (mbase == RMA_START) { 8872df173d9SMahesh Salgaonkar /* 8882df173d9SMahesh Salgaonkar * The entire RMA region will be moved by firmware 8892df173d9SMahesh Salgaonkar * to the specified destination_address. Hence set 8902df173d9SMahesh Salgaonkar * the correct offset. 8912df173d9SMahesh Salgaonkar */ 89241a65d16SHari Bathini phdr->p_offset = fw_dump.boot_mem_dest_addr; 8932df173d9SMahesh Salgaonkar } 8942df173d9SMahesh Salgaonkar 8952df173d9SMahesh Salgaonkar phdr->p_paddr = mbase; 8962df173d9SMahesh Salgaonkar phdr->p_vaddr = (unsigned long)__va(mbase); 8972df173d9SMahesh Salgaonkar phdr->p_filesz = msize; 8982df173d9SMahesh Salgaonkar phdr->p_memsz = msize; 8992df173d9SMahesh Salgaonkar phdr->p_align = 0; 9002df173d9SMahesh Salgaonkar 9012df173d9SMahesh Salgaonkar /* Increment number of program headers. */ 9022df173d9SMahesh Salgaonkar (elf->e_phnum)++; 9032df173d9SMahesh Salgaonkar } 9042df173d9SMahesh Salgaonkar return 0; 9052df173d9SMahesh Salgaonkar } 9062df173d9SMahesh Salgaonkar 9072df173d9SMahesh Salgaonkar static unsigned long init_fadump_header(unsigned long addr) 9082df173d9SMahesh Salgaonkar { 9092df173d9SMahesh Salgaonkar struct fadump_crash_info_header *fdh; 9102df173d9SMahesh Salgaonkar 9112df173d9SMahesh Salgaonkar if (!addr) 9122df173d9SMahesh Salgaonkar return 0; 9132df173d9SMahesh Salgaonkar 9142df173d9SMahesh Salgaonkar fdh = __va(addr); 9152df173d9SMahesh Salgaonkar addr += sizeof(struct fadump_crash_info_header); 9162df173d9SMahesh Salgaonkar 9172df173d9SMahesh Salgaonkar memset(fdh, 0, sizeof(struct fadump_crash_info_header)); 9182df173d9SMahesh Salgaonkar fdh->magic_number = FADUMP_CRASH_INFO_MAGIC; 9192df173d9SMahesh Salgaonkar fdh->elfcorehdr_addr = addr; 920ebaeb5aeSMahesh Salgaonkar /* We will set the crashing cpu id in crash_fadump() during crash. */ 9210226e552SHari Bathini fdh->crashing_cpu = FADUMP_CPU_UNKNOWN; 9222df173d9SMahesh Salgaonkar 9232df173d9SMahesh Salgaonkar return addr; 9242df173d9SMahesh Salgaonkar } 9252df173d9SMahesh Salgaonkar 92698b8cd7fSMichal Suchanek static int register_fadump(void) 9273ccc00a7SMahesh Salgaonkar { 9282df173d9SMahesh Salgaonkar unsigned long addr; 9292df173d9SMahesh Salgaonkar void *vaddr; 9301bd6a1c4SHari Bathini int ret; 9312df173d9SMahesh Salgaonkar 9323ccc00a7SMahesh Salgaonkar /* 9333ccc00a7SMahesh Salgaonkar * If no memory is reserved then we can not register for firmware- 9343ccc00a7SMahesh Salgaonkar * assisted dump. 9353ccc00a7SMahesh Salgaonkar */ 9363ccc00a7SMahesh Salgaonkar if (!fw_dump.reserve_dump_area_size) 93798b8cd7fSMichal Suchanek return -ENODEV; 9383ccc00a7SMahesh Salgaonkar 9391bd6a1c4SHari Bathini ret = fadump_setup_crash_memory_ranges(); 9401bd6a1c4SHari Bathini if (ret) 9411bd6a1c4SHari Bathini return ret; 9422df173d9SMahesh Salgaonkar 94341a65d16SHari Bathini addr = fw_dump.fadumphdr_addr; 94441a65d16SHari Bathini 9452df173d9SMahesh Salgaonkar /* Initialize fadump crash info header. */ 9462df173d9SMahesh Salgaonkar addr = init_fadump_header(addr); 9472df173d9SMahesh Salgaonkar vaddr = __va(addr); 9482df173d9SMahesh Salgaonkar 9492df173d9SMahesh Salgaonkar pr_debug("Creating ELF core headers at %#016lx\n", addr); 9502df173d9SMahesh Salgaonkar fadump_create_elfcore_headers(vaddr); 9512df173d9SMahesh Salgaonkar 9523ccc00a7SMahesh Salgaonkar /* register the future kernel dump with firmware. */ 95341a65d16SHari Bathini pr_debug("Registering for firmware-assisted kernel dump...\n"); 95441a65d16SHari Bathini return fw_dump.ops->fadump_register(&fw_dump); 9553ccc00a7SMahesh Salgaonkar } 9563ccc00a7SMahesh Salgaonkar 957b500afffSMahesh Salgaonkar void fadump_cleanup(void) 958b500afffSMahesh Salgaonkar { 9592790d01dSHari Bathini if (!fw_dump.fadump_supported) 9602790d01dSHari Bathini return; 9612790d01dSHari Bathini 962b500afffSMahesh Salgaonkar /* Invalidate the registration only if dump is active. */ 963b500afffSMahesh Salgaonkar if (fw_dump.dump_active) { 964f3512011SHari Bathini pr_debug("Invalidating firmware-assisted dump registration\n"); 965f3512011SHari Bathini fw_dump.ops->fadump_invalidate(&fw_dump); 966722cde76SMahesh Salgaonkar } else if (fw_dump.dump_registered) { 967722cde76SMahesh Salgaonkar /* Un-register Firmware-assisted dump if it was registered. */ 96841a65d16SHari Bathini fw_dump.ops->fadump_unregister(&fw_dump); 969e4fc48fbSHari Bathini fadump_free_mem_ranges(&crash_mrange_info); 970b500afffSMahesh Salgaonkar } 9712790d01dSHari Bathini 9722790d01dSHari Bathini if (fw_dump.ops->fadump_cleanup) 9732790d01dSHari Bathini fw_dump.ops->fadump_cleanup(&fw_dump); 974b500afffSMahesh Salgaonkar } 975b500afffSMahesh Salgaonkar 97668fa6478SHari Bathini static void fadump_free_reserved_memory(unsigned long start_pfn, 97768fa6478SHari Bathini unsigned long end_pfn) 97868fa6478SHari Bathini { 97968fa6478SHari Bathini unsigned long pfn; 98068fa6478SHari Bathini unsigned long time_limit = jiffies + HZ; 98168fa6478SHari Bathini 98268fa6478SHari Bathini pr_info("freeing reserved memory (0x%llx - 0x%llx)\n", 98368fa6478SHari Bathini PFN_PHYS(start_pfn), PFN_PHYS(end_pfn)); 98468fa6478SHari Bathini 98568fa6478SHari Bathini for (pfn = start_pfn; pfn < end_pfn; pfn++) { 98668fa6478SHari Bathini free_reserved_page(pfn_to_page(pfn)); 98768fa6478SHari Bathini 98868fa6478SHari Bathini if (time_after(jiffies, time_limit)) { 98968fa6478SHari Bathini cond_resched(); 99068fa6478SHari Bathini time_limit = jiffies + HZ; 99168fa6478SHari Bathini } 99268fa6478SHari Bathini } 99368fa6478SHari Bathini } 99468fa6478SHari Bathini 99568fa6478SHari Bathini /* 99668fa6478SHari Bathini * Skip memory holes and free memory that was actually reserved. 99768fa6478SHari Bathini */ 998dda9dbfeSHari Bathini static void fadump_release_reserved_area(u64 start, u64 end) 99968fa6478SHari Bathini { 1000dda9dbfeSHari Bathini u64 tstart, tend, spfn, epfn; 100168fa6478SHari Bathini struct memblock_region *reg; 100268fa6478SHari Bathini 1003dda9dbfeSHari Bathini spfn = PHYS_PFN(start); 1004dda9dbfeSHari Bathini epfn = PHYS_PFN(end); 100568fa6478SHari Bathini for_each_memblock(memory, reg) { 1006dda9dbfeSHari Bathini tstart = max_t(u64, spfn, memblock_region_memory_base_pfn(reg)); 1007dda9dbfeSHari Bathini tend = min_t(u64, epfn, memblock_region_memory_end_pfn(reg)); 100868fa6478SHari Bathini if (tstart < tend) { 100968fa6478SHari Bathini fadump_free_reserved_memory(tstart, tend); 101068fa6478SHari Bathini 1011dda9dbfeSHari Bathini if (tend == epfn) 101268fa6478SHari Bathini break; 101368fa6478SHari Bathini 1014dda9dbfeSHari Bathini spfn = tend; 101568fa6478SHari Bathini } 101668fa6478SHari Bathini } 101768fa6478SHari Bathini } 101868fa6478SHari Bathini 1019b500afffSMahesh Salgaonkar /* 1020dda9dbfeSHari Bathini * Sort the mem ranges in-place and merge adjacent ranges 1021dda9dbfeSHari Bathini * to minimize the memory ranges count. 1022b500afffSMahesh Salgaonkar */ 1023dda9dbfeSHari Bathini static void sort_and_merge_mem_ranges(struct fadump_mrange_info *mrange_info) 1024b500afffSMahesh Salgaonkar { 1025dda9dbfeSHari Bathini struct fadump_memory_range *mem_ranges; 1026dda9dbfeSHari Bathini struct fadump_memory_range tmp_range; 1027dda9dbfeSHari Bathini u64 base, size; 1028dda9dbfeSHari Bathini int i, j, idx; 1029dda9dbfeSHari Bathini 1030dda9dbfeSHari Bathini if (!reserved_mrange_info.mem_range_cnt) 1031dda9dbfeSHari Bathini return; 1032dda9dbfeSHari Bathini 1033dda9dbfeSHari Bathini /* Sort the memory ranges */ 1034dda9dbfeSHari Bathini mem_ranges = mrange_info->mem_ranges; 1035dda9dbfeSHari Bathini for (i = 0; i < mrange_info->mem_range_cnt; i++) { 1036dda9dbfeSHari Bathini idx = i; 1037dda9dbfeSHari Bathini for (j = (i + 1); j < mrange_info->mem_range_cnt; j++) { 1038dda9dbfeSHari Bathini if (mem_ranges[idx].base > mem_ranges[j].base) 1039dda9dbfeSHari Bathini idx = j; 1040dda9dbfeSHari Bathini } 1041dda9dbfeSHari Bathini if (idx != i) { 1042dda9dbfeSHari Bathini tmp_range = mem_ranges[idx]; 1043dda9dbfeSHari Bathini mem_ranges[idx] = mem_ranges[i]; 1044dda9dbfeSHari Bathini mem_ranges[i] = tmp_range; 1045dda9dbfeSHari Bathini } 1046dda9dbfeSHari Bathini } 1047dda9dbfeSHari Bathini 1048dda9dbfeSHari Bathini /* Merge adjacent reserved ranges */ 1049dda9dbfeSHari Bathini idx = 0; 1050dda9dbfeSHari Bathini for (i = 1; i < mrange_info->mem_range_cnt; i++) { 1051dda9dbfeSHari Bathini base = mem_ranges[i-1].base; 1052dda9dbfeSHari Bathini size = mem_ranges[i-1].size; 1053dda9dbfeSHari Bathini if (mem_ranges[i].base == (base + size)) 1054dda9dbfeSHari Bathini mem_ranges[idx].size += mem_ranges[i].size; 1055dda9dbfeSHari Bathini else { 1056dda9dbfeSHari Bathini idx++; 1057dda9dbfeSHari Bathini if (i == idx) 1058dda9dbfeSHari Bathini continue; 1059dda9dbfeSHari Bathini 1060dda9dbfeSHari Bathini mem_ranges[idx] = mem_ranges[i]; 1061dda9dbfeSHari Bathini } 1062dda9dbfeSHari Bathini } 1063dda9dbfeSHari Bathini mrange_info->mem_range_cnt = idx + 1; 1064dda9dbfeSHari Bathini } 1065dda9dbfeSHari Bathini 1066dda9dbfeSHari Bathini /* 1067dda9dbfeSHari Bathini * Scan reserved-ranges to consider them while reserving/releasing 1068dda9dbfeSHari Bathini * memory for FADump. 1069dda9dbfeSHari Bathini */ 1070dda9dbfeSHari Bathini static inline int fadump_scan_reserved_mem_ranges(void) 1071dda9dbfeSHari Bathini { 1072dda9dbfeSHari Bathini struct device_node *root; 1073dda9dbfeSHari Bathini const __be32 *prop; 1074dda9dbfeSHari Bathini int len, ret = -1; 1075dda9dbfeSHari Bathini unsigned long i; 1076dda9dbfeSHari Bathini 1077dda9dbfeSHari Bathini root = of_find_node_by_path("/"); 1078dda9dbfeSHari Bathini if (!root) 1079dda9dbfeSHari Bathini return ret; 1080dda9dbfeSHari Bathini 1081dda9dbfeSHari Bathini prop = of_get_property(root, "reserved-ranges", &len); 1082dda9dbfeSHari Bathini if (!prop) 1083dda9dbfeSHari Bathini return ret; 1084dda9dbfeSHari Bathini 1085dda9dbfeSHari Bathini /* 1086dda9dbfeSHari Bathini * Each reserved range is an (address,size) pair, 2 cells each, 1087dda9dbfeSHari Bathini * totalling 4 cells per range. 1088dda9dbfeSHari Bathini */ 1089dda9dbfeSHari Bathini for (i = 0; i < len / (sizeof(*prop) * 4); i++) { 1090dda9dbfeSHari Bathini u64 base, size; 1091dda9dbfeSHari Bathini 1092dda9dbfeSHari Bathini base = of_read_number(prop + (i * 4) + 0, 2); 1093dda9dbfeSHari Bathini size = of_read_number(prop + (i * 4) + 2, 2); 1094dda9dbfeSHari Bathini 1095dda9dbfeSHari Bathini if (size) { 1096dda9dbfeSHari Bathini ret = fadump_add_mem_range(&reserved_mrange_info, 1097dda9dbfeSHari Bathini base, base + size); 1098dda9dbfeSHari Bathini if (ret < 0) { 1099dda9dbfeSHari Bathini pr_warn("some reserved ranges are ignored!\n"); 1100dda9dbfeSHari Bathini break; 1101dda9dbfeSHari Bathini } 1102dda9dbfeSHari Bathini } 1103dda9dbfeSHari Bathini } 1104dda9dbfeSHari Bathini 1105dda9dbfeSHari Bathini return ret; 1106dda9dbfeSHari Bathini } 1107dda9dbfeSHari Bathini 1108dda9dbfeSHari Bathini /* 1109dda9dbfeSHari Bathini * Release the memory that was reserved during early boot to preserve the 1110dda9dbfeSHari Bathini * crash'ed kernel's memory contents except reserved dump area (permanent 1111dda9dbfeSHari Bathini * reservation) and reserved ranges used by F/W. The released memory will 1112dda9dbfeSHari Bathini * be available for general use. 1113dda9dbfeSHari Bathini */ 1114dda9dbfeSHari Bathini static void fadump_release_memory(u64 begin, u64 end) 1115dda9dbfeSHari Bathini { 1116dda9dbfeSHari Bathini u64 ra_start, ra_end, tstart; 1117dda9dbfeSHari Bathini int i, ret; 1118dda9dbfeSHari Bathini 1119dda9dbfeSHari Bathini fadump_scan_reserved_mem_ranges(); 1120b500afffSMahesh Salgaonkar 1121b500afffSMahesh Salgaonkar ra_start = fw_dump.reserve_dump_area_start; 1122b500afffSMahesh Salgaonkar ra_end = ra_start + fw_dump.reserve_dump_area_size; 1123b500afffSMahesh Salgaonkar 1124b500afffSMahesh Salgaonkar /* 1125dda9dbfeSHari Bathini * Add reserved dump area to reserved ranges list 1126dda9dbfeSHari Bathini * and exclude all these ranges while releasing memory. 1127dda9dbfeSHari Bathini */ 1128dda9dbfeSHari Bathini ret = fadump_add_mem_range(&reserved_mrange_info, ra_start, ra_end); 1129dda9dbfeSHari Bathini if (ret != 0) { 1130dda9dbfeSHari Bathini /* 1131dda9dbfeSHari Bathini * Not enough memory to setup reserved ranges but the system is 1132dda9dbfeSHari Bathini * running shortage of memory. So, release all the memory except 1133dda9dbfeSHari Bathini * Reserved dump area (reused for next fadump registration). 1134b500afffSMahesh Salgaonkar */ 113568fa6478SHari Bathini if (begin < ra_end && end > ra_start) { 113668fa6478SHari Bathini if (begin < ra_start) 113768fa6478SHari Bathini fadump_release_reserved_area(begin, ra_start); 113868fa6478SHari Bathini if (end > ra_end) 113968fa6478SHari Bathini fadump_release_reserved_area(ra_end, end); 114068fa6478SHari Bathini } else 114168fa6478SHari Bathini fadump_release_reserved_area(begin, end); 1142dda9dbfeSHari Bathini 1143dda9dbfeSHari Bathini return; 1144dda9dbfeSHari Bathini } 1145dda9dbfeSHari Bathini 1146dda9dbfeSHari Bathini /* Get the reserved ranges list in order first. */ 1147dda9dbfeSHari Bathini sort_and_merge_mem_ranges(&reserved_mrange_info); 1148dda9dbfeSHari Bathini 1149dda9dbfeSHari Bathini /* Exclude reserved ranges and release remaining memory */ 1150dda9dbfeSHari Bathini tstart = begin; 1151dda9dbfeSHari Bathini for (i = 0; i < reserved_mrange_info.mem_range_cnt; i++) { 1152dda9dbfeSHari Bathini ra_start = reserved_mrange_info.mem_ranges[i].base; 1153dda9dbfeSHari Bathini ra_end = ra_start + reserved_mrange_info.mem_ranges[i].size; 1154dda9dbfeSHari Bathini 1155dda9dbfeSHari Bathini if (tstart >= ra_end) 1156dda9dbfeSHari Bathini continue; 1157dda9dbfeSHari Bathini 1158dda9dbfeSHari Bathini if (tstart < ra_start) 1159dda9dbfeSHari Bathini fadump_release_reserved_area(tstart, ra_start); 1160dda9dbfeSHari Bathini tstart = ra_end; 1161dda9dbfeSHari Bathini } 1162dda9dbfeSHari Bathini 1163dda9dbfeSHari Bathini if (tstart < end) 1164dda9dbfeSHari Bathini fadump_release_reserved_area(tstart, end); 1165b500afffSMahesh Salgaonkar } 1166b500afffSMahesh Salgaonkar 1167b500afffSMahesh Salgaonkar static void fadump_invalidate_release_mem(void) 1168b500afffSMahesh Salgaonkar { 1169b500afffSMahesh Salgaonkar mutex_lock(&fadump_mutex); 1170b500afffSMahesh Salgaonkar if (!fw_dump.dump_active) { 1171b500afffSMahesh Salgaonkar mutex_unlock(&fadump_mutex); 1172b500afffSMahesh Salgaonkar return; 1173b500afffSMahesh Salgaonkar } 1174b500afffSMahesh Salgaonkar 1175b500afffSMahesh Salgaonkar fadump_cleanup(); 1176b500afffSMahesh Salgaonkar mutex_unlock(&fadump_mutex); 1177b500afffSMahesh Salgaonkar 11788255da95SHari Bathini fadump_release_memory(fw_dump.boot_memory_size, memblock_end_of_DRAM()); 1179961cf26aSHari Bathini fadump_free_cpu_notes_buf(); 1180961cf26aSHari Bathini 1181a4e2e2caSHari Bathini /* 1182a4e2e2caSHari Bathini * Setup kernel metadata and initialize the kernel dump 1183a4e2e2caSHari Bathini * memory structure for FADump re-registration. 1184a4e2e2caSHari Bathini */ 1185a4e2e2caSHari Bathini if (fw_dump.ops->fadump_setup_metadata && 1186a4e2e2caSHari Bathini (fw_dump.ops->fadump_setup_metadata(&fw_dump) < 0)) 1187a4e2e2caSHari Bathini pr_warn("Failed to setup kernel metadata!\n"); 118841a65d16SHari Bathini fw_dump.ops->fadump_init_mem_struct(&fw_dump); 1189b500afffSMahesh Salgaonkar } 1190b500afffSMahesh Salgaonkar 1191b500afffSMahesh Salgaonkar static ssize_t fadump_release_memory_store(struct kobject *kobj, 1192b500afffSMahesh Salgaonkar struct kobj_attribute *attr, 1193b500afffSMahesh Salgaonkar const char *buf, size_t count) 1194b500afffSMahesh Salgaonkar { 1195dcdc4679SMichal Suchanek int input = -1; 1196dcdc4679SMichal Suchanek 1197b500afffSMahesh Salgaonkar if (!fw_dump.dump_active) 1198b500afffSMahesh Salgaonkar return -EPERM; 1199b500afffSMahesh Salgaonkar 1200dcdc4679SMichal Suchanek if (kstrtoint(buf, 0, &input)) 1201dcdc4679SMichal Suchanek return -EINVAL; 1202dcdc4679SMichal Suchanek 1203dcdc4679SMichal Suchanek if (input == 1) { 1204b500afffSMahesh Salgaonkar /* 1205b500afffSMahesh Salgaonkar * Take away the '/proc/vmcore'. We are releasing the dump 1206b500afffSMahesh Salgaonkar * memory, hence it will not be valid anymore. 1207b500afffSMahesh Salgaonkar */ 12082685f826SMichael Ellerman #ifdef CONFIG_PROC_VMCORE 1209b500afffSMahesh Salgaonkar vmcore_cleanup(); 12102685f826SMichael Ellerman #endif 1211b500afffSMahesh Salgaonkar fadump_invalidate_release_mem(); 1212b500afffSMahesh Salgaonkar 1213b500afffSMahesh Salgaonkar } else 1214b500afffSMahesh Salgaonkar return -EINVAL; 1215b500afffSMahesh Salgaonkar return count; 1216b500afffSMahesh Salgaonkar } 1217b500afffSMahesh Salgaonkar 12183ccc00a7SMahesh Salgaonkar static ssize_t fadump_enabled_show(struct kobject *kobj, 12193ccc00a7SMahesh Salgaonkar struct kobj_attribute *attr, 12203ccc00a7SMahesh Salgaonkar char *buf) 12213ccc00a7SMahesh Salgaonkar { 12223ccc00a7SMahesh Salgaonkar return sprintf(buf, "%d\n", fw_dump.fadump_enabled); 12233ccc00a7SMahesh Salgaonkar } 12243ccc00a7SMahesh Salgaonkar 12253ccc00a7SMahesh Salgaonkar static ssize_t fadump_register_show(struct kobject *kobj, 12263ccc00a7SMahesh Salgaonkar struct kobj_attribute *attr, 12273ccc00a7SMahesh Salgaonkar char *buf) 12283ccc00a7SMahesh Salgaonkar { 12293ccc00a7SMahesh Salgaonkar return sprintf(buf, "%d\n", fw_dump.dump_registered); 12303ccc00a7SMahesh Salgaonkar } 12313ccc00a7SMahesh Salgaonkar 12323ccc00a7SMahesh Salgaonkar static ssize_t fadump_register_store(struct kobject *kobj, 12333ccc00a7SMahesh Salgaonkar struct kobj_attribute *attr, 12343ccc00a7SMahesh Salgaonkar const char *buf, size_t count) 12353ccc00a7SMahesh Salgaonkar { 12363ccc00a7SMahesh Salgaonkar int ret = 0; 1237dcdc4679SMichal Suchanek int input = -1; 12383ccc00a7SMahesh Salgaonkar 1239f3512011SHari Bathini if (!fw_dump.fadump_enabled || fw_dump.dump_active) 12403ccc00a7SMahesh Salgaonkar return -EPERM; 12413ccc00a7SMahesh Salgaonkar 1242dcdc4679SMichal Suchanek if (kstrtoint(buf, 0, &input)) 1243dcdc4679SMichal Suchanek return -EINVAL; 1244dcdc4679SMichal Suchanek 12453ccc00a7SMahesh Salgaonkar mutex_lock(&fadump_mutex); 12463ccc00a7SMahesh Salgaonkar 1247dcdc4679SMichal Suchanek switch (input) { 1248dcdc4679SMichal Suchanek case 0: 12493ccc00a7SMahesh Salgaonkar if (fw_dump.dump_registered == 0) { 12503ccc00a7SMahesh Salgaonkar goto unlock_out; 12513ccc00a7SMahesh Salgaonkar } 1252f3512011SHari Bathini 12533ccc00a7SMahesh Salgaonkar /* Un-register Firmware-assisted dump */ 125441a65d16SHari Bathini pr_debug("Un-register firmware-assisted dump\n"); 125541a65d16SHari Bathini fw_dump.ops->fadump_unregister(&fw_dump); 12563ccc00a7SMahesh Salgaonkar break; 1257dcdc4679SMichal Suchanek case 1: 12583ccc00a7SMahesh Salgaonkar if (fw_dump.dump_registered == 1) { 12590823c68bSHari Bathini /* Un-register Firmware-assisted dump */ 126041a65d16SHari Bathini fw_dump.ops->fadump_unregister(&fw_dump); 12613ccc00a7SMahesh Salgaonkar } 12623ccc00a7SMahesh Salgaonkar /* Register Firmware-assisted dump */ 126398b8cd7fSMichal Suchanek ret = register_fadump(); 12643ccc00a7SMahesh Salgaonkar break; 12653ccc00a7SMahesh Salgaonkar default: 12663ccc00a7SMahesh Salgaonkar ret = -EINVAL; 12673ccc00a7SMahesh Salgaonkar break; 12683ccc00a7SMahesh Salgaonkar } 12693ccc00a7SMahesh Salgaonkar 12703ccc00a7SMahesh Salgaonkar unlock_out: 12713ccc00a7SMahesh Salgaonkar mutex_unlock(&fadump_mutex); 12723ccc00a7SMahesh Salgaonkar return ret < 0 ? ret : count; 12733ccc00a7SMahesh Salgaonkar } 12743ccc00a7SMahesh Salgaonkar 12753ccc00a7SMahesh Salgaonkar static int fadump_region_show(struct seq_file *m, void *private) 12763ccc00a7SMahesh Salgaonkar { 12773ccc00a7SMahesh Salgaonkar if (!fw_dump.fadump_enabled) 12783ccc00a7SMahesh Salgaonkar return 0; 12793ccc00a7SMahesh Salgaonkar 1280b500afffSMahesh Salgaonkar mutex_lock(&fadump_mutex); 128141a65d16SHari Bathini fw_dump.ops->fadump_region_show(&fw_dump, m); 1282b500afffSMahesh Salgaonkar mutex_unlock(&fadump_mutex); 12833ccc00a7SMahesh Salgaonkar return 0; 12843ccc00a7SMahesh Salgaonkar } 12853ccc00a7SMahesh Salgaonkar 1286b500afffSMahesh Salgaonkar static struct kobj_attribute fadump_release_attr = __ATTR(fadump_release_mem, 1287b500afffSMahesh Salgaonkar 0200, NULL, 1288b500afffSMahesh Salgaonkar fadump_release_memory_store); 12893ccc00a7SMahesh Salgaonkar static struct kobj_attribute fadump_attr = __ATTR(fadump_enabled, 12903ccc00a7SMahesh Salgaonkar 0444, fadump_enabled_show, 12913ccc00a7SMahesh Salgaonkar NULL); 12923ccc00a7SMahesh Salgaonkar static struct kobj_attribute fadump_register_attr = __ATTR(fadump_registered, 12933ccc00a7SMahesh Salgaonkar 0644, fadump_register_show, 12943ccc00a7SMahesh Salgaonkar fadump_register_store); 12953ccc00a7SMahesh Salgaonkar 1296f6cee260SYangtao Li DEFINE_SHOW_ATTRIBUTE(fadump_region); 12973ccc00a7SMahesh Salgaonkar 12983ccc00a7SMahesh Salgaonkar static void fadump_init_files(void) 12993ccc00a7SMahesh Salgaonkar { 13003ccc00a7SMahesh Salgaonkar struct dentry *debugfs_file; 13013ccc00a7SMahesh Salgaonkar int rc = 0; 13023ccc00a7SMahesh Salgaonkar 13033ccc00a7SMahesh Salgaonkar rc = sysfs_create_file(kernel_kobj, &fadump_attr.attr); 13043ccc00a7SMahesh Salgaonkar if (rc) 13053ccc00a7SMahesh Salgaonkar printk(KERN_ERR "fadump: unable to create sysfs file" 13063ccc00a7SMahesh Salgaonkar " fadump_enabled (%d)\n", rc); 13073ccc00a7SMahesh Salgaonkar 13083ccc00a7SMahesh Salgaonkar rc = sysfs_create_file(kernel_kobj, &fadump_register_attr.attr); 13093ccc00a7SMahesh Salgaonkar if (rc) 13103ccc00a7SMahesh Salgaonkar printk(KERN_ERR "fadump: unable to create sysfs file" 13113ccc00a7SMahesh Salgaonkar " fadump_registered (%d)\n", rc); 13123ccc00a7SMahesh Salgaonkar 13133ccc00a7SMahesh Salgaonkar debugfs_file = debugfs_create_file("fadump_region", 0444, 13143ccc00a7SMahesh Salgaonkar powerpc_debugfs_root, NULL, 13153ccc00a7SMahesh Salgaonkar &fadump_region_fops); 13163ccc00a7SMahesh Salgaonkar if (!debugfs_file) 13173ccc00a7SMahesh Salgaonkar printk(KERN_ERR "fadump: unable to create debugfs file" 13183ccc00a7SMahesh Salgaonkar " fadump_region\n"); 1319b500afffSMahesh Salgaonkar 1320b500afffSMahesh Salgaonkar if (fw_dump.dump_active) { 1321b500afffSMahesh Salgaonkar rc = sysfs_create_file(kernel_kobj, &fadump_release_attr.attr); 1322b500afffSMahesh Salgaonkar if (rc) 1323b500afffSMahesh Salgaonkar printk(KERN_ERR "fadump: unable to create sysfs file" 1324b500afffSMahesh Salgaonkar " fadump_release_mem (%d)\n", rc); 1325b500afffSMahesh Salgaonkar } 13263ccc00a7SMahesh Salgaonkar return; 13273ccc00a7SMahesh Salgaonkar } 13283ccc00a7SMahesh Salgaonkar 13293ccc00a7SMahesh Salgaonkar /* 13303ccc00a7SMahesh Salgaonkar * Prepare for firmware-assisted dump. 13313ccc00a7SMahesh Salgaonkar */ 13323ccc00a7SMahesh Salgaonkar int __init setup_fadump(void) 13333ccc00a7SMahesh Salgaonkar { 13343ccc00a7SMahesh Salgaonkar if (!fw_dump.fadump_enabled) 13353ccc00a7SMahesh Salgaonkar return 0; 13363ccc00a7SMahesh Salgaonkar 13373ccc00a7SMahesh Salgaonkar if (!fw_dump.fadump_supported) { 13383ccc00a7SMahesh Salgaonkar printk(KERN_ERR "Firmware-assisted dump is not supported on" 13393ccc00a7SMahesh Salgaonkar " this hardware\n"); 13403ccc00a7SMahesh Salgaonkar return 0; 13413ccc00a7SMahesh Salgaonkar } 13423ccc00a7SMahesh Salgaonkar 13433ccc00a7SMahesh Salgaonkar fadump_show_config(); 13442df173d9SMahesh Salgaonkar /* 13452df173d9SMahesh Salgaonkar * If dump data is available then see if it is valid and prepare for 13462df173d9SMahesh Salgaonkar * saving it to the disk. 13472df173d9SMahesh Salgaonkar */ 1348b500afffSMahesh Salgaonkar if (fw_dump.dump_active) { 1349b500afffSMahesh Salgaonkar /* 1350b500afffSMahesh Salgaonkar * if dump process fails then invalidate the registration 1351b500afffSMahesh Salgaonkar * and release memory before proceeding for re-registration. 1352b500afffSMahesh Salgaonkar */ 1353f3512011SHari Bathini if (fw_dump.ops->fadump_process(&fw_dump) < 0) 1354b500afffSMahesh Salgaonkar fadump_invalidate_release_mem(); 1355b500afffSMahesh Salgaonkar } 13563ccc00a7SMahesh Salgaonkar /* Initialize the kernel dump memory structure for FAD registration. */ 13572df173d9SMahesh Salgaonkar else if (fw_dump.reserve_dump_area_size) 135841a65d16SHari Bathini fw_dump.ops->fadump_init_mem_struct(&fw_dump); 1359f3512011SHari Bathini 13603ccc00a7SMahesh Salgaonkar fadump_init_files(); 13613ccc00a7SMahesh Salgaonkar 13623ccc00a7SMahesh Salgaonkar return 1; 13633ccc00a7SMahesh Salgaonkar } 13643ccc00a7SMahesh Salgaonkar subsys_initcall(setup_fadump); 1365bec53196SHari Bathini #else /* !CONFIG_PRESERVE_FA_DUMP */ 1366bec53196SHari Bathini 1367bec53196SHari Bathini /* Scan the Firmware Assisted dump configuration details. */ 1368bec53196SHari Bathini int __init early_init_dt_scan_fw_dump(unsigned long node, const char *uname, 1369bec53196SHari Bathini int depth, void *data) 1370bec53196SHari Bathini { 1371bec53196SHari Bathini if ((depth != 1) || (strcmp(uname, "ibm,opal") != 0)) 1372bec53196SHari Bathini return 0; 1373bec53196SHari Bathini 1374bec53196SHari Bathini opal_fadump_dt_scan(&fw_dump, node); 1375bec53196SHari Bathini return 1; 1376bec53196SHari Bathini } 1377bec53196SHari Bathini 1378bec53196SHari Bathini /* 1379bec53196SHari Bathini * When dump is active but PRESERVE_FA_DUMP is enabled on the kernel, 1380bec53196SHari Bathini * preserve crash data. The subsequent memory preserving kernel boot 1381bec53196SHari Bathini * is likely to process this crash data. 1382bec53196SHari Bathini */ 1383bec53196SHari Bathini int __init fadump_reserve_mem(void) 1384bec53196SHari Bathini { 1385bec53196SHari Bathini if (fw_dump.dump_active) { 1386bec53196SHari Bathini /* 1387bec53196SHari Bathini * If last boot has crashed then reserve all the memory 1388bec53196SHari Bathini * above boot memory to preserve crash data. 1389bec53196SHari Bathini */ 1390bec53196SHari Bathini pr_info("Preserving crash data for processing in next boot.\n"); 1391bec53196SHari Bathini fadump_reserve_crash_area(fw_dump.boot_mem_top); 1392bec53196SHari Bathini } else 1393bec53196SHari Bathini pr_debug("FADump-aware kernel..\n"); 1394bec53196SHari Bathini 1395bec53196SHari Bathini return 1; 1396bec53196SHari Bathini } 1397bec53196SHari Bathini #endif /* CONFIG_PRESERVE_FA_DUMP */ 1398b2a815a5SHari Bathini 1399b2a815a5SHari Bathini /* Preserve everything above the base address */ 1400b2a815a5SHari Bathini static void __init fadump_reserve_crash_area(u64 base) 1401b2a815a5SHari Bathini { 1402b2a815a5SHari Bathini struct memblock_region *reg; 1403b2a815a5SHari Bathini u64 mstart, msize; 1404b2a815a5SHari Bathini 1405b2a815a5SHari Bathini for_each_memblock(memory, reg) { 1406b2a815a5SHari Bathini mstart = reg->base; 1407b2a815a5SHari Bathini msize = reg->size; 1408b2a815a5SHari Bathini 1409b2a815a5SHari Bathini if ((mstart + msize) < base) 1410b2a815a5SHari Bathini continue; 1411b2a815a5SHari Bathini 1412b2a815a5SHari Bathini if (mstart < base) { 1413b2a815a5SHari Bathini msize -= (base - mstart); 1414b2a815a5SHari Bathini mstart = base; 1415b2a815a5SHari Bathini } 1416b2a815a5SHari Bathini 1417b2a815a5SHari Bathini pr_info("Reserving %lluMB of memory at %#016llx for preserving crash data", 1418b2a815a5SHari Bathini (msize >> 20), mstart); 1419b2a815a5SHari Bathini memblock_reserve(mstart, msize); 1420b2a815a5SHari Bathini } 1421b2a815a5SHari Bathini } 1422bec53196SHari Bathini 1423bec53196SHari Bathini unsigned long __init arch_reserved_kernel_pages(void) 1424bec53196SHari Bathini { 1425bec53196SHari Bathini return memblock_reserved_size() / PAGE_SIZE; 1426bec53196SHari Bathini } 1427