2 * PPC64 code to handle Linux booting another kernel.
4 * Copyright (C) 2004-2005, IBM Corp.
6 * Created by: Milton D Miller II
8 * This source code is licensed under the GNU General Public License,
9 * Version 2. See the file COPYING for more details.
13 #include <linux/cpumask.h>
14 #include <linux/kexec.h>
15 #include <linux/smp.h>
16 #include <linux/thread_info.h>
17 #include <linux/errno.h>
20 #include <asm/current.h>
21 #include <asm/machdep.h>
22 #include <asm/cacheflush.h>
26 #include <asm/sections.h> /* _end */
30 int default_machine_kexec_prepare(struct kimage *image)
33 unsigned long begin, end; /* limits of segment */
34 unsigned long low, high; /* limits of blocked memory range */
35 struct device_node *node;
39 if (!ppc_md.hpte_clear_all)
43 * Since we use the kernel fault handlers and paging code to
44 * handle the virtual mode, we must make sure no destination
45 * overlaps kernel static data or bss.
47 for (i = 0; i < image->nr_segments; i++)
48 if (image->segment[i].mem < __pa(_end))
52 * For non-LPAR, we absolutely can not overwrite the mmu hash
53 * table, since we are still using the bolted entries in it to
54 * do the copy. Check that here.
56 * It is safe if the end is below the start of the blocked
57 * region (end <= low), or if the beginning is after the
58 * end of the blocked region (begin >= high). Use the
59 * boolean identity !(a || b) === (!a && !b).
62 low = __pa(htab_address);
63 high = low + htab_size_bytes;
65 for (i = 0; i < image->nr_segments; i++) {
66 begin = image->segment[i].mem;
67 end = begin + image->segment[i].memsz;
69 if ((begin < high) && (end > low))
74 /* We also should not overwrite the tce tables */
75 for (node = of_find_node_by_type(NULL, "pci"); node != NULL;
76 node = of_find_node_by_type(node, "pci")) {
77 basep = (unsigned long *)get_property(node, "linux,tce-base",
79 sizep = (unsigned int *)get_property(node, "linux,tce-size",
81 if (basep == NULL || sizep == NULL)
85 high = low + (*sizep);
87 for (i = 0; i < image->nr_segments; i++) {
88 begin = image->segment[i].mem;
89 end = begin + image->segment[i].memsz;
91 if ((begin < high) && (end > low))
99 #define IND_FLAGS (IND_DESTINATION | IND_INDIRECTION | IND_DONE | IND_SOURCE)
101 static void copy_segments(unsigned long ind)
109 * We rely on kexec_load to create a lists that properly
110 * initializes these pointers before they are used.
111 * We will still crash if the list is wrong, but at least
112 * the compiler will be quiet.
117 for (entry = ind; !(entry & IND_DONE); entry = *ptr++) {
118 addr = __va(entry & PAGE_MASK);
120 switch (entry & IND_FLAGS) {
121 case IND_DESTINATION:
124 case IND_INDIRECTION:
128 copy_page(dest, addr);
134 void kexec_copy_flush(struct kimage *image)
136 long i, nr_segments = image->nr_segments;
137 struct kexec_segment ranges[KEXEC_SEGMENT_MAX];
139 /* save the ranges on the stack to efficiently flush the icache */
140 memcpy(ranges, image->segment, sizeof(ranges));
143 * After this call we may not use anything allocated in dynamic
144 * memory, including *image.
146 * Only globals and the stack are allowed.
148 copy_segments(image->head);
151 * we need to clear the icache for all dest pages sometime,
152 * including ones that were in place on the original copy
154 for (i = 0; i < nr_segments; i++)
155 flush_icache_range((unsigned long)__va(ranges[i].mem),
156 (unsigned long)__va(ranges[i].mem + ranges[i].memsz));
161 /* FIXME: we should schedule this function to be called on all cpus based
162 * on calling the interrupts, but we would like to call it off irq level
163 * so that the interrupt controller is clean.
165 void kexec_smp_down(void *arg)
167 if (ppc_md.kexec_cpu_down)
168 ppc_md.kexec_cpu_down(0, 1);
175 static void kexec_prepare_cpus(void)
177 int my_cpu, i, notified=-1;
179 smp_call_function(kexec_smp_down, NULL, 0, /* wait */0);
182 /* check the others cpus are now down (via paca hw cpu id == -1) */
183 for (i=0; i < NR_CPUS; i++) {
187 while (paca[i].hw_cpu_id != -1) {
189 if (!cpu_possible(i)) {
190 printk("kexec: cpu %d hw_cpu_id %d is not"
191 " possible, ignoring\n",
192 i, paca[i].hw_cpu_id);
195 if (!cpu_online(i)) {
196 /* Fixme: this can be spinning in
197 * pSeries_secondary_wait with a paca
198 * waiting for it to go online.
200 printk("kexec: cpu %d hw_cpu_id %d is not"
201 " online, ignoring\n",
202 i, paca[i].hw_cpu_id);
206 printk( "kexec: waiting for cpu %d (physical"
208 i, paca[i].hw_cpu_id);
214 /* after we tell the others to go down */
215 if (ppc_md.kexec_cpu_down)
216 ppc_md.kexec_cpu_down(0, 0);
225 static void kexec_prepare_cpus(void)
228 * move the secondarys to us so that we can copy
229 * the new kernel 0-0x100 safely
231 * do this if kexec in setup.c ?
233 * We need to release the cpus if we are ever going from an
234 * UP to an SMP kernel.
237 if (ppc_md.kexec_cpu_down)
238 ppc_md.kexec_cpu_down(0, 0);
245 * kexec thread structure and stack.
247 * We need to make sure that this is 16384-byte aligned due to the
248 * way process stacks are handled. It also must be statically allocated
249 * or allocated as part of the kimage, because everything else may be
250 * overwritten when we copy the kexec image. We piggyback on the
251 * "init_task" linker section here to statically allocate a stack.
253 * We could use a smaller stack if we don't care about anything using
254 * current, but that audit has not been performed.
256 union thread_union kexec_stack
257 __attribute__((__section__(".data.init_task"))) = { };
259 /* Our assembly helper, in kexec_stub.S */
260 extern NORET_TYPE void kexec_sequence(void *newstack, unsigned long start,
261 void *image, void *control,
262 void (*clear_all)(void)) ATTRIB_NORET;
264 /* too late to fail here */
265 void default_machine_kexec(struct kimage *image)
267 /* prepare control code if any */
270 * If the kexec boot is the normal one, need to shutdown other cpus
271 * into our wait loop and quiesce interrupts.
272 * Otherwise, in the case of crashed mode (crashing_cpu >= 0),
273 * stopping other CPUs and collecting their pt_regs is done before
274 * using debugger IPI.
277 if (crashing_cpu == -1)
278 kexec_prepare_cpus();
280 /* switch to a staticly allocated stack. Based on irq stack code.
281 * XXX: the task struct will likely be invalid once we do the copy!
283 kexec_stack.thread_info.task = current_thread_info()->task;
284 kexec_stack.thread_info.flags = 0;
286 /* Some things are best done in assembly. Finding globals with
287 * a toc is easier in C, so pass in what we can.
289 kexec_sequence(&kexec_stack, image->start, image,
290 page_address(image->control_code_page),
291 ppc_md.hpte_clear_all);
295 /* Values we need to export to the second kernel via the device tree. */
296 static unsigned long htab_base, kernel_end;
298 static struct property htab_base_prop = {
299 .name = "linux,htab-base",
300 .length = sizeof(unsigned long),
301 .value = (unsigned char *)&htab_base,
304 static struct property htab_size_prop = {
305 .name = "linux,htab-size",
306 .length = sizeof(unsigned long),
307 .value = (unsigned char *)&htab_size_bytes,
310 static struct property kernel_end_prop = {
311 .name = "linux,kernel-end",
312 .length = sizeof(unsigned long),
313 .value = (unsigned char *)&kernel_end,
316 static void __init export_htab_values(void)
318 struct device_node *node;
320 node = of_find_node_by_path("/chosen");
324 kernel_end = __pa(_end);
325 prom_add_property(node, &kernel_end_prop);
327 /* On machines with no htab htab_address is NULL */
328 if (NULL == htab_address)
331 htab_base = __pa(htab_address);
332 prom_add_property(node, &htab_base_prop);
333 prom_add_property(node, &htab_size_prop);
339 static struct property crashk_base_prop = {
340 .name = "linux,crashkernel-base",
341 .length = sizeof(unsigned long),
342 .value = (unsigned char *)&crashk_res.start,
345 static unsigned long crashk_size;
347 static struct property crashk_size_prop = {
348 .name = "linux,crashkernel-size",
349 .length = sizeof(unsigned long),
350 .value = (unsigned char *)&crashk_size,
353 static void __init export_crashk_values(void)
355 struct device_node *node;
356 struct property *prop;
358 node = of_find_node_by_path("/chosen");
362 /* There might be existing crash kernel properties, but we can't
363 * be sure what's in them, so remove them. */
364 prop = of_find_property(node, "linux,crashkernel-base", NULL);
366 prom_remove_property(node, prop);
368 prop = of_find_property(node, "linux,crashkernel-size", NULL);
370 prom_remove_property(node, prop);
372 if (crashk_res.start != 0) {
373 prom_add_property(node, &crashk_base_prop);
374 crashk_size = crashk_res.end - crashk_res.start + 1;
375 prom_add_property(node, &crashk_size_prop);
381 static int __init kexec_setup(void)
383 export_htab_values();
384 export_crashk_values();
387 __initcall(kexec_setup);
389 static int __init early_parse_crashk(char *p)
396 size = memparse(p, &p);
399 crashk_res.start = memparse(p + 1, &p);
401 crashk_res.start = KDUMP_KERNELBASE;
403 crashk_res.end = crashk_res.start + size - 1;
407 early_param("crashkernel", early_parse_crashk);
409 void __init reserve_crashkernel(void)
413 if (crashk_res.start == 0)
416 /* We might have got these values via the command line or the
417 * device tree, either way sanitise them now. */
419 size = crashk_res.end - crashk_res.start + 1;
421 if (crashk_res.start != KDUMP_KERNELBASE)
422 printk("Crash kernel location must be 0x%x\n",
425 crashk_res.start = KDUMP_KERNELBASE;
426 size = PAGE_ALIGN(size);
427 crashk_res.end = crashk_res.start + size - 1;
429 /* Crash kernel trumps memory limit */
430 if (memory_limit && memory_limit <= crashk_res.end) {
431 memory_limit = crashk_res.end + 1;
432 printk("Adjusted memory limit for crashkernel, now 0x%lx\n",
436 lmb_reserve(crashk_res.start, size);
439 int overlaps_crashkernel(unsigned long start, unsigned long size)
441 return (start + size) > crashk_res.start && start <= crashk_res.end;