1
2
3
4
5
6
7#include <linux/kexec.h>
8#include <asm/kexec.h>
9#include <linux/smp.h>
10#include <asm/cacheflush.h>
11#include <asm/barrier.h>
12#include <asm/page.h>
13#include <linux/libfdt.h>
14#include <asm/set_memory.h>
15#include <linux/compiler.h>
16#include <linux/cpu.h>
17#include <linux/reboot.h>
18
19
20
21
22static void
23kexec_image_info(const struct kimage *image)
24{
25 unsigned long i;
26
27 pr_debug("Kexec image info:\n");
28 pr_debug("\ttype: %d\n", image->type);
29 pr_debug("\tstart: %lx\n", image->start);
30 pr_debug("\thead: %lx\n", image->head);
31 pr_debug("\tnr_segments: %lu\n", image->nr_segments);
32
33 for (i = 0; i < image->nr_segments; i++) {
34 pr_debug("\t segment[%lu]: %016lx - %016lx", i,
35 image->segment[i].mem,
36 image->segment[i].mem + image->segment[i].memsz);
37 pr_debug("\t\t0x%lx bytes, %lu pages\n",
38 (unsigned long) image->segment[i].memsz,
39 (unsigned long) image->segment[i].memsz / PAGE_SIZE);
40 }
41}
42
43
44
45
46
47
48
49
50
51
52int
53machine_kexec_prepare(struct kimage *image)
54{
55 struct kimage_arch *internal = &image->arch;
56 struct fdt_header fdt = {0};
57 void *control_code_buffer = NULL;
58 unsigned int control_code_buffer_sz = 0;
59 int i = 0;
60
61 kexec_image_info(image);
62
63
64 for (i = 0; i < image->nr_segments; i++) {
65 if (image->segment[i].memsz <= sizeof(fdt))
66 continue;
67
68 if (copy_from_user(&fdt, image->segment[i].buf, sizeof(fdt)))
69 continue;
70
71 if (fdt_check_header(&fdt))
72 continue;
73
74 internal->fdt_addr = (unsigned long) image->segment[i].mem;
75 break;
76 }
77
78 if (!internal->fdt_addr) {
79 pr_err("Device tree not included in the provided image\n");
80 return -EINVAL;
81 }
82
83
84 if (image->type != KEXEC_TYPE_CRASH) {
85 control_code_buffer = page_address(image->control_code_page);
86 control_code_buffer_sz = page_size(image->control_code_page);
87
88 if (unlikely(riscv_kexec_relocate_size > control_code_buffer_sz)) {
89 pr_err("Relocation code doesn't fit within a control page\n");
90 return -EINVAL;
91 }
92
93 memcpy(control_code_buffer, riscv_kexec_relocate,
94 riscv_kexec_relocate_size);
95
96
97 set_memory_x((unsigned long) control_code_buffer, 1);
98 }
99
100 return 0;
101}
102
103
104
105
106
107
108
109
110
111
112
113void
114machine_kexec_cleanup(struct kimage *image)
115{
116}
117
118
119
120
121
122
123
124
125
126void machine_shutdown(void)
127{
128
129
130
131
132 local_irq_disable();
133
134#if defined(CONFIG_HOTPLUG_CPU)
135 smp_shutdown_nonboot_cpus(smp_processor_id());
136#endif
137}
138
139
140
141
142
143
144
145
146
147void
148machine_crash_shutdown(struct pt_regs *regs)
149{
150 crash_save_cpu(regs, smp_processor_id());
151 machine_shutdown();
152 pr_info("Starting crashdump kernel...\n");
153}
154
155
156
157
158
159
160
161
162
163
164
165
166void __noreturn
167machine_kexec(struct kimage *image)
168{
169 struct kimage_arch *internal = &image->arch;
170 unsigned long jump_addr = (unsigned long) image->start;
171 unsigned long first_ind_entry = (unsigned long) &image->head;
172 unsigned long this_hart_id = raw_smp_processor_id();
173 unsigned long fdt_addr = internal->fdt_addr;
174 void *control_code_buffer = page_address(image->control_code_page);
175 riscv_kexec_method kexec_method = NULL;
176
177 if (image->type != KEXEC_TYPE_CRASH)
178 kexec_method = control_code_buffer;
179 else
180 kexec_method = (riscv_kexec_method) &riscv_kexec_norelocate;
181
182 pr_notice("Will call new kernel at %08lx from hart id %lx\n",
183 jump_addr, this_hart_id);
184 pr_notice("FDT image at %08lx\n", fdt_addr);
185
186
187 local_flush_icache_all();
188
189
190 pr_notice("Bye...\n");
191 kexec_method(first_ind_entry, jump_addr, fdt_addr,
192 this_hart_id, kernel_map.va_pa_offset);
193 unreachable();
194}
195