Merge ../linus
[pandora-kernel.git] / arch / powerpc / kernel / machine_kexec_64.c
1 /*
2  * PPC64 code to handle Linux booting another kernel.
3  *
4  * Copyright (C) 2004-2005, IBM Corp.
5  *
6  * Created by: Milton D Miller II
7  *
8  * This source code is licensed under the GNU General Public License,
9  * Version 2.  See the file COPYING for more details.
10  */
11
12
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>
18
19 #include <asm/page.h>
20 #include <asm/current.h>
21 #include <asm/machdep.h>
22 #include <asm/cacheflush.h>
23 #include <asm/paca.h>
24 #include <asm/lmb.h>
25 #include <asm/mmu.h>
26 #include <asm/sections.h>       /* _end */
27 #include <asm/prom.h>
28 #include <asm/smp.h>
29
30 int default_machine_kexec_prepare(struct kimage *image)
31 {
32         int i;
33         unsigned long begin, end;       /* limits of segment */
34         unsigned long low, high;        /* limits of blocked memory range */
35         struct device_node *node;
36         unsigned long *basep;
37         unsigned int *sizep;
38
39         if (!ppc_md.hpte_clear_all)
40                 return -ENOENT;
41
42         /*
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.
46          */
47         for (i = 0; i < image->nr_segments; i++)
48                 if (image->segment[i].mem < __pa(_end))
49                         return -ETXTBSY;
50
51         /*
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.
55          *
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).
60          */
61         if (htab_address) {
62                 low = __pa(htab_address);
63                 high = low + htab_size_bytes;
64
65                 for (i = 0; i < image->nr_segments; i++) {
66                         begin = image->segment[i].mem;
67                         end = begin + image->segment[i].memsz;
68
69                         if ((begin < high) && (end > low))
70                                 return -ETXTBSY;
71                 }
72         }
73
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",
78                                                         NULL);
79                 sizep = (unsigned int *)get_property(node, "linux,tce-size",
80                                                         NULL);
81                 if (basep == NULL || sizep == NULL)
82                         continue;
83
84                 low = *basep;
85                 high = low + (*sizep);
86
87                 for (i = 0; i < image->nr_segments; i++) {
88                         begin = image->segment[i].mem;
89                         end = begin + image->segment[i].memsz;
90
91                         if ((begin < high) && (end > low))
92                                 return -ETXTBSY;
93                 }
94         }
95
96         return 0;
97 }
98
99 #define IND_FLAGS (IND_DESTINATION | IND_INDIRECTION | IND_DONE | IND_SOURCE)
100
101 static void copy_segments(unsigned long ind)
102 {
103         unsigned long entry;
104         unsigned long *ptr;
105         void *dest;
106         void *addr;
107
108         /*
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.
113          */
114         ptr = NULL;
115         dest = NULL;
116
117         for (entry = ind; !(entry & IND_DONE); entry = *ptr++) {
118                 addr = __va(entry & PAGE_MASK);
119
120                 switch (entry & IND_FLAGS) {
121                 case IND_DESTINATION:
122                         dest = addr;
123                         break;
124                 case IND_INDIRECTION:
125                         ptr = addr;
126                         break;
127                 case IND_SOURCE:
128                         copy_page(dest, addr);
129                         dest += PAGE_SIZE;
130                 }
131         }
132 }
133
134 void kexec_copy_flush(struct kimage *image)
135 {
136         long i, nr_segments = image->nr_segments;
137         struct  kexec_segment ranges[KEXEC_SEGMENT_MAX];
138
139         /* save the ranges on the stack to efficiently flush the icache */
140         memcpy(ranges, image->segment, sizeof(ranges));
141
142         /*
143          * After this call we may not use anything allocated in dynamic
144          * memory, including *image.
145          *
146          * Only globals and the stack are allowed.
147          */
148         copy_segments(image->head);
149
150         /*
151          * we need to clear the icache for all dest pages sometime,
152          * including ones that were in place on the original copy
153          */
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));
157 }
158
159 #ifdef CONFIG_SMP
160
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.
164  */
165 void kexec_smp_down(void *arg)
166 {
167         if (ppc_md.kexec_cpu_down)
168                 ppc_md.kexec_cpu_down(0, 1);
169
170         local_irq_disable();
171         kexec_smp_wait();
172         /* NOTREACHED */
173 }
174
175 static void kexec_prepare_cpus(void)
176 {
177         int my_cpu, i, notified=-1;
178
179         smp_call_function(kexec_smp_down, NULL, 0, /* wait */0);
180         my_cpu = get_cpu();
181
182         /* check the others cpus are now down (via paca hw cpu id == -1) */
183         for (i=0; i < NR_CPUS; i++) {
184                 if (i == my_cpu)
185                         continue;
186
187                 while (paca[i].hw_cpu_id != -1) {
188                         barrier();
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);
193                                 break;
194                         }
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.
199                                  */
200                                 printk("kexec: cpu %d hw_cpu_id %d is not"
201                                                 " online, ignoring\n",
202                                                 i, paca[i].hw_cpu_id);
203                                 break;
204                         }
205                         if (i != notified) {
206                                 printk( "kexec: waiting for cpu %d (physical"
207                                                 " %d) to go down\n",
208                                                 i, paca[i].hw_cpu_id);
209                                 notified = i;
210                         }
211                 }
212         }
213
214         /* after we tell the others to go down */
215         if (ppc_md.kexec_cpu_down)
216                 ppc_md.kexec_cpu_down(0, 0);
217
218         put_cpu();
219
220         local_irq_disable();
221 }
222
223 #else /* ! SMP */
224
225 static void kexec_prepare_cpus(void)
226 {
227         /*
228          * move the secondarys to us so that we can copy
229          * the new kernel 0-0x100 safely
230          *
231          * do this if kexec in setup.c ?
232          *
233          * We need to release the cpus if we are ever going from an
234          * UP to an SMP kernel.
235          */
236         smp_release_cpus();
237         if (ppc_md.kexec_cpu_down)
238                 ppc_md.kexec_cpu_down(0, 0);
239         local_irq_disable();
240 }
241
242 #endif /* SMP */
243
244 /*
245  * kexec thread structure and stack.
246  *
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.
252  *
253  * We could use a smaller stack if we don't care about anything using
254  * current, but that audit has not been performed.
255  */
256 union thread_union kexec_stack
257         __attribute__((__section__(".data.init_task"))) = { };
258
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;
263
264 /* too late to fail here */
265 void default_machine_kexec(struct kimage *image)
266 {
267         /* prepare control code if any */
268
269         /*
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.
275         */
276
277        if (crashing_cpu == -1)
278                kexec_prepare_cpus();
279
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!
282          */
283         kexec_stack.thread_info.task = current_thread_info()->task;
284         kexec_stack.thread_info.flags = 0;
285
286         /* Some things are best done in assembly.  Finding globals with
287          * a toc is easier in C, so pass in what we can.
288          */
289         kexec_sequence(&kexec_stack, image->start, image,
290                         page_address(image->control_code_page),
291                         ppc_md.hpte_clear_all);
292         /* NOTREACHED */
293 }
294
295 /* Values we need to export to the second kernel via the device tree. */
296 static unsigned long htab_base, kernel_end;
297
298 static struct property htab_base_prop = {
299         .name = "linux,htab-base",
300         .length = sizeof(unsigned long),
301         .value = (unsigned char *)&htab_base,
302 };
303
304 static struct property htab_size_prop = {
305         .name = "linux,htab-size",
306         .length = sizeof(unsigned long),
307         .value = (unsigned char *)&htab_size_bytes,
308 };
309
310 static struct property kernel_end_prop = {
311         .name = "linux,kernel-end",
312         .length = sizeof(unsigned long),
313         .value = (unsigned char *)&kernel_end,
314 };
315
316 static void __init export_htab_values(void)
317 {
318         struct device_node *node;
319
320         node = of_find_node_by_path("/chosen");
321         if (!node)
322                 return;
323
324         kernel_end = __pa(_end);
325         prom_add_property(node, &kernel_end_prop);
326
327         /* On machines with no htab htab_address is NULL */
328         if (NULL == htab_address)
329                 goto out;
330
331         htab_base = __pa(htab_address);
332         prom_add_property(node, &htab_base_prop);
333         prom_add_property(node, &htab_size_prop);
334
335  out:
336         of_node_put(node);
337 }
338
339 static struct property crashk_base_prop = {
340         .name = "linux,crashkernel-base",
341         .length = sizeof(unsigned long),
342         .value = (unsigned char *)&crashk_res.start,
343 };
344
345 static unsigned long crashk_size;
346
347 static struct property crashk_size_prop = {
348         .name = "linux,crashkernel-size",
349         .length = sizeof(unsigned long),
350         .value = (unsigned char *)&crashk_size,
351 };
352
353 static void __init export_crashk_values(void)
354 {
355         struct device_node *node;
356         struct property *prop;
357
358         node = of_find_node_by_path("/chosen");
359         if (!node)
360                 return;
361
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);
365         if (prop)
366                 prom_remove_property(node, prop);
367
368         prop = of_find_property(node, "linux,crashkernel-size", NULL);
369         if (prop)
370                 prom_remove_property(node, prop);
371
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);
376         }
377
378         of_node_put(node);
379 }
380
381 static int __init kexec_setup(void)
382 {
383         export_htab_values();
384         export_crashk_values();
385         return 0;
386 }
387 __initcall(kexec_setup);
388
389 static int __init early_parse_crashk(char *p)
390 {
391         unsigned long size;
392
393         if (!p)
394                 return 1;
395
396         size = memparse(p, &p);
397
398         if (*p == '@')
399                 crashk_res.start = memparse(p + 1, &p);
400         else
401                 crashk_res.start = KDUMP_KERNELBASE;
402
403         crashk_res.end = crashk_res.start + size - 1;
404
405         return 0;
406 }
407 early_param("crashkernel", early_parse_crashk);
408
409 void __init reserve_crashkernel(void)
410 {
411         unsigned long size;
412
413         if (crashk_res.start == 0)
414                 return;
415
416         /* We might have got these values via the command line or the
417          * device tree, either way sanitise them now. */
418
419         size = crashk_res.end - crashk_res.start + 1;
420
421         if (crashk_res.start != KDUMP_KERNELBASE)
422                 printk("Crash kernel location must be 0x%x\n",
423                                 KDUMP_KERNELBASE);
424
425         crashk_res.start = KDUMP_KERNELBASE;
426         size = PAGE_ALIGN(size);
427         crashk_res.end = crashk_res.start + size - 1;
428
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",
433                                 memory_limit);
434         }
435
436         lmb_reserve(crashk_res.start, size);
437 }
438
439 int overlaps_crashkernel(unsigned long start, unsigned long size)
440 {
441         return (start + size) > crashk_res.start && start <= crashk_res.end;
442 }