1 // SPDX-License-Identifier: GPL-2.0
3 * machine_kexec.c - handle transition of Linux booting another kernel
4 * Copyright (C) 2002-2003 Eric Biederman <ebiederm@xmission.com>
6 * GameCube/ppc32 port Copyright (C) 2004 Albert Herranz
7 * LANDISK/sh4 supported by kogiidena
10 #include <linux/kexec.h>
11 #include <linux/delay.h>
12 #include <linux/reboot.h>
13 #include <linux/numa.h>
14 #include <linux/ftrace.h>
15 #include <linux/suspend.h>
16 #include <linux/memblock.h>
17 #include <asm/mmu_context.h>
19 #include <asm/cacheflush.h>
20 #include <asm/sh_bios.h>
21 #include <asm/reboot.h>
23 typedef void (*relocate_new_kernel_t)(unsigned long indirection_page,
24 unsigned long reboot_code_buffer,
25 unsigned long start_address);
27 extern const unsigned char relocate_new_kernel[];
28 extern const unsigned int relocate_new_kernel_size;
29 extern void *vbr_base;
31 void native_machine_crash_shutdown(struct pt_regs *regs)
33 /* Nothing to do for UP, but definitely broken for SMP.. */
37 * Do what every setup is needed on image and the
38 * reboot code buffer to allow us to avoid allocations
41 int machine_kexec_prepare(struct kimage *image)
46 void machine_kexec_cleanup(struct kimage *image)
50 static void kexec_info(struct kimage *image)
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",
57 (unsigned int)image->segment[i].mem,
58 (unsigned int)image->segment[i].mem +
59 image->segment[i].memsz,
60 (unsigned int)image->segment[i].memsz);
62 printk(" start : 0x%08x\n\n", (unsigned int)image->start);
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.
69 void machine_kexec(struct kimage *image)
71 unsigned long page_list;
72 unsigned long reboot_code_buffer;
73 relocate_new_kernel_t rnk;
76 int save_ftrace_enabled;
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.
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);
91 #ifdef CONFIG_KEXEC_JUMP
92 if (image->preserve_context)
93 save_processor_state();
96 save_ftrace_enabled = __ftrace_enabled_save();
98 /* Interrupts aren't acceptable while we reboot */
101 page_list = image->head;
103 /* we need both effective and real address here */
105 (unsigned long)page_address(image->control_code_page);
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);
114 sh_bios_vbr_reload();
117 rnk = (relocate_new_kernel_t) reboot_code_buffer;
118 (*rnk)(page_list, reboot_code_buffer,
119 (unsigned long)phys_to_virt(image->start));
121 #ifdef CONFIG_KEXEC_JUMP
122 asm volatile("ldc %0, vbr" : : "r" (&vbr_base) : "memory");
124 if (image->preserve_context)
125 restore_processor_state();
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);
137 __ftrace_enabled_restore(save_ftrace_enabled);
140 void arch_crash_save_vmcoreinfo(void)
143 VMCOREINFO_SYMBOL(node_data);
144 VMCOREINFO_LENGTH(node_data, MAX_NUMNODES);
147 VMCOREINFO_CONFIG(X2TLB);
151 void __init reserve_crashkernel(void)
153 unsigned long long crash_size, crash_base;
156 ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
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;
163 if (crashk_res.end == crashk_res.start)
166 crash_size = PAGE_ALIGN(resource_size(&crashk_res));
167 if (!crashk_res.start) {
168 unsigned long max = memblock_end_of_DRAM() - memory_limit;
169 crashk_res.start = memblock_phys_alloc_range(crash_size,
171 if (!crashk_res.start) {
172 pr_err("crashkernel allocation failed\n");
176 ret = memblock_reserve(crashk_res.start, crash_size);
177 if (unlikely(ret < 0)) {
178 pr_err("crashkernel reservation failed - "
179 "memory is in use\n");
184 crashk_res.end = crashk_res.start + crash_size - 1;
187 * Crash kernel trumps memory limit
189 if ((memblock_end_of_DRAM() - memory_limit) <= crashk_res.end) {
191 pr_info("Disabled memory limit for crashkernel\n");
194 pr_info("Reserving %ldMB of memory at 0x%08lx "
195 "for crashkernel (System RAM: %ldMB)\n",
196 (unsigned long)(crash_size >> 20),
197 (unsigned long)(crashk_res.start),
198 (unsigned long)(memblock_phys_mem_size() >> 20));
203 crashk_res.start = crashk_res.end = 0;