Kuninori Morimoto | 5933f6d | 2018-12-28 00:32:24 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
kogiidena | 9d44190 | 2006-01-16 22:14:10 -0800 | [diff] [blame] | 2 | /* |
| 3 | * machine_kexec.c - handle transition of Linux booting another kernel |
| 4 | * Copyright (C) 2002-2003 Eric Biederman <ebiederm@xmission.com> |
| 5 | * |
| 6 | * GameCube/ppc32 port Copyright (C) 2004 Albert Herranz |
| 7 | * LANDISK/sh4 supported by kogiidena |
kogiidena | 9d44190 | 2006-01-16 22:14:10 -0800 | [diff] [blame] | 8 | */ |
kogiidena | 9d44190 | 2006-01-16 22:14:10 -0800 | [diff] [blame] | 9 | #include <linux/mm.h> |
| 10 | #include <linux/kexec.h> |
| 11 | #include <linux/delay.h> |
| 12 | #include <linux/reboot.h> |
Paul Mundt | c3b4adf | 2008-08-04 13:42:49 +0900 | [diff] [blame] | 13 | #include <linux/numa.h> |
Paul Mundt | 7e6b6f2 | 2009-03-18 19:07:16 +0900 | [diff] [blame] | 14 | #include <linux/ftrace.h> |
Magnus Damm | b7cf6dd | 2009-03-18 08:51:29 +0000 | [diff] [blame] | 15 | #include <linux/suspend.h> |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 16 | #include <linux/memblock.h> |
kogiidena | 9d44190 | 2006-01-16 22:14:10 -0800 | [diff] [blame] | 17 | #include <asm/mmu_context.h> |
| 18 | #include <asm/io.h> |
| 19 | #include <asm/cacheflush.h> |
Paul Mundt | 191d0d2 | 2010-01-12 14:50:43 +0900 | [diff] [blame] | 20 | #include <asm/sh_bios.h> |
Paul Mundt | fbb82b0 | 2010-01-20 16:42:52 +0900 | [diff] [blame] | 21 | #include <asm/reboot.h> |
kogiidena | 9d44190 | 2006-01-16 22:14:10 -0800 | [diff] [blame] | 22 | |
Magnus Damm | b7cf6dd | 2009-03-18 08:51:29 +0000 | [diff] [blame] | 23 | typedef void (*relocate_new_kernel_t)(unsigned long indirection_page, |
| 24 | unsigned long reboot_code_buffer, |
| 25 | unsigned long start_address); |
kogiidena | 9d44190 | 2006-01-16 22:14:10 -0800 | [diff] [blame] | 26 | |
Tobias Klauser | 2efe55a | 2006-06-26 18:57:34 +0200 | [diff] [blame] | 27 | extern const unsigned char relocate_new_kernel[]; |
| 28 | extern const unsigned int relocate_new_kernel_size; |
Magnus Damm | b7cf6dd | 2009-03-18 08:51:29 +0000 | [diff] [blame] | 29 | extern void *vbr_base; |
kogiidena | 9d44190 | 2006-01-16 22:14:10 -0800 | [diff] [blame] | 30 | |
Paul Mundt | fbb82b0 | 2010-01-20 16:42:52 +0900 | [diff] [blame] | 31 | void native_machine_crash_shutdown(struct pt_regs *regs) |
kogiidena | 9d44190 | 2006-01-16 22:14:10 -0800 | [diff] [blame] | 32 | { |
Paul Mundt | fbb82b0 | 2010-01-20 16:42:52 +0900 | [diff] [blame] | 33 | /* Nothing to do for UP, but definitely broken for SMP.. */ |
kogiidena | 9d44190 | 2006-01-16 22:14:10 -0800 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | /* |
| 37 | * Do what every setup is needed on image and the |
| 38 | * reboot code buffer to allow us to avoid allocations |
| 39 | * later. |
| 40 | */ |
| 41 | int machine_kexec_prepare(struct kimage *image) |
| 42 | { |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | void machine_kexec_cleanup(struct kimage *image) |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | static void kexec_info(struct kimage *image) |
| 51 | { |
| 52 | int i; |
| 53 | printk("kexec information\n"); |
| 54 | for (i = 0; i < image->nr_segments; i++) { |
| 55 | printk(" segment[%d]: 0x%08x - 0x%08x (0x%08x)\n", |
| 56 | i, |
| 57 | (unsigned int)image->segment[i].mem, |
Paul Mundt | 4d5ade5 | 2007-04-27 11:25:57 +0900 | [diff] [blame] | 58 | (unsigned int)image->segment[i].mem + |
| 59 | image->segment[i].memsz, |
kogiidena | 9d44190 | 2006-01-16 22:14:10 -0800 | [diff] [blame] | 60 | (unsigned int)image->segment[i].memsz); |
Paul Mundt | 4d5ade5 | 2007-04-27 11:25:57 +0900 | [diff] [blame] | 61 | } |
kogiidena | 9d44190 | 2006-01-16 22:14:10 -0800 | [diff] [blame] | 62 | printk(" start : 0x%08x\n\n", (unsigned int)image->start); |
| 63 | } |
| 64 | |
kogiidena | 9d44190 | 2006-01-16 22:14:10 -0800 | [diff] [blame] | 65 | /* |
| 66 | * Do not allocate memory (or fail in any way) in machine_kexec(). |
| 67 | * We are past the point of no return, committed to rebooting now. |
| 68 | */ |
Huang Ying | 3ab8352 | 2008-07-25 19:45:07 -0700 | [diff] [blame] | 69 | void machine_kexec(struct kimage *image) |
kogiidena | 9d44190 | 2006-01-16 22:14:10 -0800 | [diff] [blame] | 70 | { |
kogiidena | 9d44190 | 2006-01-16 22:14:10 -0800 | [diff] [blame] | 71 | unsigned long page_list; |
| 72 | unsigned long reboot_code_buffer; |
kogiidena | 9d44190 | 2006-01-16 22:14:10 -0800 | [diff] [blame] | 73 | relocate_new_kernel_t rnk; |
Magnus Damm | e4e063d | 2009-03-18 08:49:45 +0000 | [diff] [blame] | 74 | unsigned long entry; |
| 75 | unsigned long *ptr; |
Paul Mundt | 7e6b6f2 | 2009-03-18 19:07:16 +0900 | [diff] [blame] | 76 | int save_ftrace_enabled; |
Magnus Damm | e4e063d | 2009-03-18 08:49:45 +0000 | [diff] [blame] | 77 | |
| 78 | /* |
| 79 | * Nicked from the mips version of machine_kexec(): |
| 80 | * The generic kexec code builds a page list with physical |
| 81 | * addresses. Use phys_to_virt() to convert them to virtual. |
| 82 | */ |
| 83 | for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE); |
| 84 | ptr = (entry & IND_INDIRECTION) ? |
| 85 | phys_to_virt(entry & PAGE_MASK) : ptr + 1) { |
| 86 | if (*ptr & IND_SOURCE || *ptr & IND_INDIRECTION || |
| 87 | *ptr & IND_DESTINATION) |
| 88 | *ptr = (unsigned long) phys_to_virt(*ptr); |
| 89 | } |
kogiidena | 9d44190 | 2006-01-16 22:14:10 -0800 | [diff] [blame] | 90 | |
Magnus Damm | b7cf6dd | 2009-03-18 08:51:29 +0000 | [diff] [blame] | 91 | #ifdef CONFIG_KEXEC_JUMP |
| 92 | if (image->preserve_context) |
| 93 | save_processor_state(); |
| 94 | #endif |
| 95 | |
Paul Mundt | 7e6b6f2 | 2009-03-18 19:07:16 +0900 | [diff] [blame] | 96 | save_ftrace_enabled = __ftrace_enabled_save(); |
| 97 | |
kogiidena | 9d44190 | 2006-01-16 22:14:10 -0800 | [diff] [blame] | 98 | /* Interrupts aren't acceptable while we reboot */ |
| 99 | local_irq_disable(); |
| 100 | |
| 101 | page_list = image->head; |
| 102 | |
| 103 | /* we need both effective and real address here */ |
| 104 | reboot_code_buffer = |
| 105 | (unsigned long)page_address(image->control_code_page); |
| 106 | |
| 107 | /* copy our kernel relocation code to the control code page */ |
| 108 | memcpy((void *)reboot_code_buffer, relocate_new_kernel, |
| 109 | relocate_new_kernel_size); |
| 110 | |
Paul Mundt | a6bab7b | 2009-03-18 19:06:15 +0900 | [diff] [blame] | 111 | kexec_info(image); |
kogiidena | 9d44190 | 2006-01-16 22:14:10 -0800 | [diff] [blame] | 112 | flush_cache_all(); |
| 113 | |
Paul Mundt | 191d0d2 | 2010-01-12 14:50:43 +0900 | [diff] [blame] | 114 | sh_bios_vbr_reload(); |
Paul Mundt | a6bab7b | 2009-03-18 19:06:15 +0900 | [diff] [blame] | 115 | |
kogiidena | 9d44190 | 2006-01-16 22:14:10 -0800 | [diff] [blame] | 116 | /* now call it */ |
| 117 | rnk = (relocate_new_kernel_t) reboot_code_buffer; |
Magnus Damm | 615e73b | 2009-03-19 10:04:29 +0000 | [diff] [blame] | 118 | (*rnk)(page_list, reboot_code_buffer, |
| 119 | (unsigned long)phys_to_virt(image->start)); |
Magnus Damm | b7cf6dd | 2009-03-18 08:51:29 +0000 | [diff] [blame] | 120 | |
| 121 | #ifdef CONFIG_KEXEC_JUMP |
| 122 | asm volatile("ldc %0, vbr" : : "r" (&vbr_base) : "memory"); |
Paul Mundt | a6bab7b | 2009-03-18 19:06:15 +0900 | [diff] [blame] | 123 | |
Magnus Damm | b7cf6dd | 2009-03-18 08:51:29 +0000 | [diff] [blame] | 124 | if (image->preserve_context) |
| 125 | restore_processor_state(); |
| 126 | |
| 127 | /* Convert page list back to physical addresses, what a mess. */ |
| 128 | for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE); |
| 129 | ptr = (*ptr & IND_INDIRECTION) ? |
| 130 | phys_to_virt(*ptr & PAGE_MASK) : ptr + 1) { |
| 131 | if (*ptr & IND_SOURCE || *ptr & IND_INDIRECTION || |
| 132 | *ptr & IND_DESTINATION) |
| 133 | *ptr = virt_to_phys(*ptr); |
| 134 | } |
| 135 | #endif |
Paul Mundt | 7e6b6f2 | 2009-03-18 19:07:16 +0900 | [diff] [blame] | 136 | |
| 137 | __ftrace_enabled_restore(save_ftrace_enabled); |
kogiidena | 9d44190 | 2006-01-16 22:14:10 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Paul Mundt | c3b4adf | 2008-08-04 13:42:49 +0900 | [diff] [blame] | 140 | void arch_crash_save_vmcoreinfo(void) |
| 141 | { |
| 142 | #ifdef CONFIG_NUMA |
| 143 | VMCOREINFO_SYMBOL(node_data); |
| 144 | VMCOREINFO_LENGTH(node_data, MAX_NUMNODES); |
| 145 | #endif |
Paul Mundt | aa424bb | 2010-05-07 17:14:00 +0900 | [diff] [blame] | 146 | #ifdef CONFIG_X2TLB |
| 147 | VMCOREINFO_CONFIG(X2TLB); |
| 148 | #endif |
Paul Mundt | c3b4adf | 2008-08-04 13:42:49 +0900 | [diff] [blame] | 149 | } |
Paul Mundt | a5ec395 | 2010-05-07 14:54:55 +0900 | [diff] [blame] | 150 | |
| 151 | void __init reserve_crashkernel(void) |
| 152 | { |
| 153 | unsigned long long crash_size, crash_base; |
| 154 | int ret; |
| 155 | |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 156 | ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(), |
Paul Mundt | a5ec395 | 2010-05-07 14:54:55 +0900 | [diff] [blame] | 157 | &crash_size, &crash_base); |
| 158 | if (ret == 0 && crash_size > 0) { |
| 159 | crashk_res.start = crash_base; |
| 160 | crashk_res.end = crash_base + crash_size - 1; |
| 161 | } |
| 162 | |
| 163 | if (crashk_res.end == crashk_res.start) |
| 164 | goto disable; |
| 165 | |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 166 | crash_size = PAGE_ALIGN(resource_size(&crashk_res)); |
Paul Mundt | a5ec395 | 2010-05-07 14:54:55 +0900 | [diff] [blame] | 167 | if (!crashk_res.start) { |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 168 | unsigned long max = memblock_end_of_DRAM() - memory_limit; |
Mike Rapoport | 42b46ae | 2019-03-11 23:29:31 -0700 | [diff] [blame] | 169 | crashk_res.start = memblock_phys_alloc_range(crash_size, |
| 170 | PAGE_SIZE, 0, max); |
Paul Mundt | a5ec395 | 2010-05-07 14:54:55 +0900 | [diff] [blame] | 171 | if (!crashk_res.start) { |
| 172 | pr_err("crashkernel allocation failed\n"); |
| 173 | goto disable; |
| 174 | } |
| 175 | } else { |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 176 | ret = memblock_reserve(crashk_res.start, crash_size); |
Paul Mundt | a5ec395 | 2010-05-07 14:54:55 +0900 | [diff] [blame] | 177 | if (unlikely(ret < 0)) { |
| 178 | pr_err("crashkernel reservation failed - " |
| 179 | "memory is in use\n"); |
| 180 | goto disable; |
| 181 | } |
| 182 | } |
| 183 | |
Paul Mundt | 5e2ff32 | 2010-05-10 20:17:25 +0900 | [diff] [blame] | 184 | crashk_res.end = crashk_res.start + crash_size - 1; |
| 185 | |
| 186 | /* |
| 187 | * Crash kernel trumps memory limit |
| 188 | */ |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 189 | if ((memblock_end_of_DRAM() - memory_limit) <= crashk_res.end) { |
Paul Mundt | 5e2ff32 | 2010-05-10 20:17:25 +0900 | [diff] [blame] | 190 | memory_limit = 0; |
| 191 | pr_info("Disabled memory limit for crashkernel\n"); |
| 192 | } |
| 193 | |
| 194 | pr_info("Reserving %ldMB of memory at 0x%08lx " |
Paul Mundt | a5ec395 | 2010-05-07 14:54:55 +0900 | [diff] [blame] | 195 | "for crashkernel (System RAM: %ldMB)\n", |
| 196 | (unsigned long)(crash_size >> 20), |
Paul Mundt | 5e2ff32 | 2010-05-10 20:17:25 +0900 | [diff] [blame] | 197 | (unsigned long)(crashk_res.start), |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 198 | (unsigned long)(memblock_phys_mem_size() >> 20)); |
Paul Mundt | a5ec395 | 2010-05-07 14:54:55 +0900 | [diff] [blame] | 199 | |
Paul Mundt | a5ec395 | 2010-05-07 14:54:55 +0900 | [diff] [blame] | 200 | return; |
| 201 | |
| 202 | disable: |
| 203 | crashk_res.start = crashk_res.end = 0; |
| 204 | } |