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>
25 #include <asm/sections.h> /* _end */
29 #define HASH_GROUP_SIZE 0x80 /* size of each hash group, asm/mmu.h */
31 int default_machine_kexec_prepare(struct kimage *image)
34 unsigned long begin, end; /* limits of segment */
35 unsigned long low, high; /* limits of blocked memory range */
36 struct device_node *node;
40 if (!ppc_md.hpte_clear_all)
44 * Since we use the kernel fault handlers and paging code to
45 * handle the virtual mode, we must make sure no destination
46 * overlaps kernel static data or bss.
48 for (i = 0; i < image->nr_segments; i++)
49 if (image->segment[i].mem < __pa(_end))
53 * For non-LPAR, we absolutely can not overwrite the mmu hash
54 * table, since we are still using the bolted entries in it to
55 * do the copy. Check that here.
57 * It is safe if the end is below the start of the blocked
58 * region (end <= low), or if the beginning is after the
59 * end of the blocked region (begin >= high). Use the
60 * boolean identity !(a || b) === (!a && !b).
63 low = __pa(htab_address);
64 high = low + (htab_hash_mask + 1) * HASH_GROUP_SIZE;
66 for (i = 0; i < image->nr_segments; i++) {
67 begin = image->segment[i].mem;
68 end = begin + image->segment[i].memsz;
70 if ((begin < high) && (end > low))
75 /* We also should not overwrite the tce tables */
76 for (node = of_find_node_by_type(NULL, "pci"); node != NULL;
77 node = of_find_node_by_type(node, "pci")) {
78 basep = (unsigned long *)get_property(node, "linux,tce-base",
80 sizep = (unsigned int *)get_property(node, "linux,tce-size",
82 if (basep == NULL || sizep == NULL)
86 high = low + (*sizep);
88 for (i = 0; i < image->nr_segments; i++) {
89 begin = image->segment[i].mem;
90 end = begin + image->segment[i].memsz;
92 if ((begin < high) && (end > low))
100 #define IND_FLAGS (IND_DESTINATION | IND_INDIRECTION | IND_DONE | IND_SOURCE)
102 static void copy_segments(unsigned long ind)
110 * We rely on kexec_load to create a lists that properly
111 * initializes these pointers before they are used.
112 * We will still crash if the list is wrong, but at least
113 * the compiler will be quiet.
118 for (entry = ind; !(entry & IND_DONE); entry = *ptr++) {
119 addr = __va(entry & PAGE_MASK);
121 switch (entry & IND_FLAGS) {
122 case IND_DESTINATION:
125 case IND_INDIRECTION:
129 copy_page(dest, addr);
135 void kexec_copy_flush(struct kimage *image)
137 long i, nr_segments = image->nr_segments;
138 struct kexec_segment ranges[KEXEC_SEGMENT_MAX];
140 /* save the ranges on the stack to efficiently flush the icache */
141 memcpy(ranges, image->segment, sizeof(ranges));
144 * After this call we may not use anything allocated in dynamic
145 * memory, including *image.
147 * Only globals and the stack are allowed.
149 copy_segments(image->head);
152 * we need to clear the icache for all dest pages sometime,
153 * including ones that were in place on the original copy
155 for (i = 0; i < nr_segments; i++)
156 flush_icache_range(ranges[i].mem + KERNELBASE,
157 ranges[i].mem + KERNELBASE +
163 /* FIXME: we should schedule this function to be called on all cpus based
164 * on calling the interrupts, but we would like to call it off irq level
165 * so that the interrupt controller is clean.
167 void kexec_smp_down(void *arg)
169 if (ppc_md.kexec_cpu_down)
170 ppc_md.kexec_cpu_down(0, 1);
177 static void kexec_prepare_cpus(void)
179 int my_cpu, i, notified=-1;
181 smp_call_function(kexec_smp_down, NULL, 0, /* wait */0);
184 /* check the others cpus are now down (via paca hw cpu id == -1) */
185 for (i=0; i < NR_CPUS; i++) {
189 while (paca[i].hw_cpu_id != -1) {
191 if (!cpu_possible(i)) {
192 printk("kexec: cpu %d hw_cpu_id %d is not"
193 " possible, ignoring\n",
194 i, paca[i].hw_cpu_id);
197 if (!cpu_online(i)) {
198 /* Fixme: this can be spinning in
199 * pSeries_secondary_wait with a paca
200 * waiting for it to go online.
202 printk("kexec: cpu %d hw_cpu_id %d is not"
203 " online, ignoring\n",
204 i, paca[i].hw_cpu_id);
208 printk( "kexec: waiting for cpu %d (physical"
210 i, paca[i].hw_cpu_id);
216 /* after we tell the others to go down */
217 if (ppc_md.kexec_cpu_down)
218 ppc_md.kexec_cpu_down(0, 0);
227 static void kexec_prepare_cpus(void)
230 * move the secondarys to us so that we can copy
231 * the new kernel 0-0x100 safely
233 * do this if kexec in setup.c ?
235 * We need to release the cpus if we are ever going from an
236 * UP to an SMP kernel.
239 if (ppc_md.kexec_cpu_down)
240 ppc_md.kexec_cpu_down(0, 0);
247 * kexec thread structure and stack.
249 * We need to make sure that this is 16384-byte aligned due to the
250 * way process stacks are handled. It also must be statically allocated
251 * or allocated as part of the kimage, because everything else may be
252 * overwritten when we copy the kexec image. We piggyback on the
253 * "init_task" linker section here to statically allocate a stack.
255 * We could use a smaller stack if we don't care about anything using
256 * current, but that audit has not been performed.
258 union thread_union kexec_stack
259 __attribute__((__section__(".data.init_task"))) = { };
261 /* Our assembly helper, in kexec_stub.S */
262 extern NORET_TYPE void kexec_sequence(void *newstack, unsigned long start,
263 void *image, void *control,
264 void (*clear_all)(void)) ATTRIB_NORET;
266 /* too late to fail here */
267 void default_machine_kexec(struct kimage *image)
270 /* prepare control code if any */
272 /* shutdown other cpus into our wait loop and quiesce interrupts */
273 kexec_prepare_cpus();
275 /* switch to a staticly allocated stack. Based on irq stack code.
276 * XXX: the task struct will likely be invalid once we do the copy!
278 kexec_stack.thread_info.task = current_thread_info()->task;
279 kexec_stack.thread_info.flags = 0;
281 /* Some things are best done in assembly. Finding globals with
282 * a toc is easier in C, so pass in what we can.
284 kexec_sequence(&kexec_stack, image->start, image,
285 page_address(image->control_code_page),
286 ppc_md.hpte_clear_all);
290 /* Values we need to export to the second kernel via the device tree. */
291 static unsigned long htab_base, htab_size, kernel_end;
293 static struct property htab_base_prop = {
294 .name = "linux,htab-base",
295 .length = sizeof(unsigned long),
296 .value = (unsigned char *)&htab_base,
299 static struct property htab_size_prop = {
300 .name = "linux,htab-size",
301 .length = sizeof(unsigned long),
302 .value = (unsigned char *)&htab_size,
305 static struct property kernel_end_prop = {
306 .name = "linux,kernel-end",
307 .length = sizeof(unsigned long),
308 .value = (unsigned char *)&kernel_end,
311 static void __init export_htab_values(void)
313 struct device_node *node;
315 node = of_find_node_by_path("/chosen");
319 kernel_end = __pa(_end);
320 prom_add_property(node, &kernel_end_prop);
322 /* On machines with no htab htab_address is NULL */
323 if (NULL == htab_address)
326 htab_base = __pa(htab_address);
327 prom_add_property(node, &htab_base_prop);
329 htab_size = 1UL << ppc64_pft_size;
330 prom_add_property(node, &htab_size_prop);
336 void __init kexec_setup(void)
338 export_htab_values();