mm: don't include asm/pgtable.h if linux/mm.h is already included
[platform/kernel/linux-starfive.git] / arch / sh / kernel / machine_kexec.c
1 // SPDX-License-Identifier: GPL-2.0
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
8  */
9 #include <linux/mm.h>
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/pgalloc.h>
18 #include <asm/mmu_context.h>
19 #include <asm/io.h>
20 #include <asm/cacheflush.h>
21 #include <asm/sh_bios.h>
22 #include <asm/reboot.h>
23
24 typedef void (*relocate_new_kernel_t)(unsigned long indirection_page,
25                                       unsigned long reboot_code_buffer,
26                                       unsigned long start_address);
27
28 extern const unsigned char relocate_new_kernel[];
29 extern const unsigned int relocate_new_kernel_size;
30 extern void *vbr_base;
31
32 void native_machine_crash_shutdown(struct pt_regs *regs)
33 {
34         /* Nothing to do for UP, but definitely broken for SMP.. */
35 }
36
37 /*
38  * Do what every setup is needed on image and the
39  * reboot code buffer to allow us to avoid allocations
40  * later.
41  */
42 int machine_kexec_prepare(struct kimage *image)
43 {
44         return 0;
45 }
46
47 void machine_kexec_cleanup(struct kimage *image)
48 {
49 }
50
51 static void kexec_info(struct kimage *image)
52 {
53         int i;
54         printk("kexec information\n");
55         for (i = 0; i < image->nr_segments; i++) {
56                 printk("  segment[%d]: 0x%08x - 0x%08x (0x%08x)\n",
57                        i,
58                        (unsigned int)image->segment[i].mem,
59                        (unsigned int)image->segment[i].mem +
60                                      image->segment[i].memsz,
61                        (unsigned int)image->segment[i].memsz);
62         }
63         printk("  start     : 0x%08x\n\n", (unsigned int)image->start);
64 }
65
66 /*
67  * Do not allocate memory (or fail in any way) in machine_kexec().
68  * We are past the point of no return, committed to rebooting now.
69  */
70 void machine_kexec(struct kimage *image)
71 {
72         unsigned long page_list;
73         unsigned long reboot_code_buffer;
74         relocate_new_kernel_t rnk;
75         unsigned long entry;
76         unsigned long *ptr;
77         int save_ftrace_enabled;
78
79         /*
80          * Nicked from the mips version of machine_kexec():
81          * The generic kexec code builds a page list with physical
82          * addresses. Use phys_to_virt() to convert them to virtual.
83          */
84         for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE);
85              ptr = (entry & IND_INDIRECTION) ?
86                phys_to_virt(entry & PAGE_MASK) : ptr + 1) {
87                 if (*ptr & IND_SOURCE || *ptr & IND_INDIRECTION ||
88                     *ptr & IND_DESTINATION)
89                         *ptr = (unsigned long) phys_to_virt(*ptr);
90         }
91
92 #ifdef CONFIG_KEXEC_JUMP
93         if (image->preserve_context)
94                 save_processor_state();
95 #endif
96
97         save_ftrace_enabled = __ftrace_enabled_save();
98
99         /* Interrupts aren't acceptable while we reboot */
100         local_irq_disable();
101
102         page_list = image->head;
103
104         /* we need both effective and real address here */
105         reboot_code_buffer =
106                         (unsigned long)page_address(image->control_code_page);
107
108         /* copy our kernel relocation code to the control code page */
109         memcpy((void *)reboot_code_buffer, relocate_new_kernel,
110                                                 relocate_new_kernel_size);
111
112         kexec_info(image);
113         flush_cache_all();
114
115         sh_bios_vbr_reload();
116
117         /* now call it */
118         rnk = (relocate_new_kernel_t) reboot_code_buffer;
119         (*rnk)(page_list, reboot_code_buffer,
120                (unsigned long)phys_to_virt(image->start));
121
122 #ifdef CONFIG_KEXEC_JUMP
123         asm volatile("ldc %0, vbr" : : "r" (&vbr_base) : "memory");
124
125         if (image->preserve_context)
126                 restore_processor_state();
127
128         /* Convert page list back to physical addresses, what a mess. */
129         for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE);
130              ptr = (*ptr & IND_INDIRECTION) ?
131                phys_to_virt(*ptr & PAGE_MASK) : ptr + 1) {
132                 if (*ptr & IND_SOURCE || *ptr & IND_INDIRECTION ||
133                     *ptr & IND_DESTINATION)
134                         *ptr = virt_to_phys(*ptr);
135         }
136 #endif
137
138         __ftrace_enabled_restore(save_ftrace_enabled);
139 }
140
141 void arch_crash_save_vmcoreinfo(void)
142 {
143 #ifdef CONFIG_NUMA
144         VMCOREINFO_SYMBOL(node_data);
145         VMCOREINFO_LENGTH(node_data, MAX_NUMNODES);
146 #endif
147 #ifdef CONFIG_X2TLB
148         VMCOREINFO_CONFIG(X2TLB);
149 #endif
150 }
151
152 void __init reserve_crashkernel(void)
153 {
154         unsigned long long crash_size, crash_base;
155         int ret;
156
157         ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
158                         &crash_size, &crash_base);
159         if (ret == 0 && crash_size > 0) {
160                 crashk_res.start = crash_base;
161                 crashk_res.end = crash_base + crash_size - 1;
162         }
163
164         if (crashk_res.end == crashk_res.start)
165                 goto disable;
166
167         crash_size = PAGE_ALIGN(resource_size(&crashk_res));
168         if (!crashk_res.start) {
169                 unsigned long max = memblock_end_of_DRAM() - memory_limit;
170                 crashk_res.start = memblock_phys_alloc_range(crash_size,
171                                                              PAGE_SIZE, 0, max);
172                 if (!crashk_res.start) {
173                         pr_err("crashkernel allocation failed\n");
174                         goto disable;
175                 }
176         } else {
177                 ret = memblock_reserve(crashk_res.start, crash_size);
178                 if (unlikely(ret < 0)) {
179                         pr_err("crashkernel reservation failed - "
180                                "memory is in use\n");
181                         goto disable;
182                 }
183         }
184
185         crashk_res.end = crashk_res.start + crash_size - 1;
186
187         /*
188          * Crash kernel trumps memory limit
189          */
190         if ((memblock_end_of_DRAM() - memory_limit) <= crashk_res.end) {
191                 memory_limit = 0;
192                 pr_info("Disabled memory limit for crashkernel\n");
193         }
194
195         pr_info("Reserving %ldMB of memory at 0x%08lx "
196                 "for crashkernel (System RAM: %ldMB)\n",
197                 (unsigned long)(crash_size >> 20),
198                 (unsigned long)(crashk_res.start),
199                 (unsigned long)(memblock_phys_mem_size() >> 20));
200
201         return;
202
203 disable:
204         crashk_res.start = crashk_res.end = 0;
205 }