Merge branch 'perf-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[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/kexec.h>
14 #include <linux/smp.h>
15 #include <linux/thread_info.h>
16 #include <linux/init_task.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/mmu.h>
25 #include <asm/sections.h>       /* _end */
26 #include <asm/prom.h>
27 #include <asm/smp.h>
28
29 int default_machine_kexec_prepare(struct kimage *image)
30 {
31         int i;
32         unsigned long begin, end;       /* limits of segment */
33         unsigned long low, high;        /* limits of blocked memory range */
34         struct device_node *node;
35         const unsigned long *basep;
36         const unsigned int *sizep;
37
38         if (!ppc_md.hpte_clear_all)
39                 return -ENOENT;
40
41         /*
42          * Since we use the kernel fault handlers and paging code to
43          * handle the virtual mode, we must make sure no destination
44          * overlaps kernel static data or bss.
45          */
46         for (i = 0; i < image->nr_segments; i++)
47                 if (image->segment[i].mem < __pa(_end))
48                         return -ETXTBSY;
49
50         /*
51          * For non-LPAR, we absolutely can not overwrite the mmu hash
52          * table, since we are still using the bolted entries in it to
53          * do the copy.  Check that here.
54          *
55          * It is safe if the end is below the start of the blocked
56          * region (end <= low), or if the beginning is after the
57          * end of the blocked region (begin >= high).  Use the
58          * boolean identity !(a || b)  === (!a && !b).
59          */
60         if (htab_address) {
61                 low = __pa(htab_address);
62                 high = low + htab_size_bytes;
63
64                 for (i = 0; i < image->nr_segments; i++) {
65                         begin = image->segment[i].mem;
66                         end = begin + image->segment[i].memsz;
67
68                         if ((begin < high) && (end > low))
69                                 return -ETXTBSY;
70                 }
71         }
72
73         /* We also should not overwrite the tce tables */
74         for (node = of_find_node_by_type(NULL, "pci"); node != NULL;
75                         node = of_find_node_by_type(node, "pci")) {
76                 basep = of_get_property(node, "linux,tce-base", NULL);
77                 sizep = of_get_property(node, "linux,tce-size", NULL);
78                 if (basep == NULL || sizep == NULL)
79                         continue;
80
81                 low = *basep;
82                 high = low + (*sizep);
83
84                 for (i = 0; i < image->nr_segments; i++) {
85                         begin = image->segment[i].mem;
86                         end = begin + image->segment[i].memsz;
87
88                         if ((begin < high) && (end > low))
89                                 return -ETXTBSY;
90                 }
91         }
92
93         return 0;
94 }
95
96 #define IND_FLAGS (IND_DESTINATION | IND_INDIRECTION | IND_DONE | IND_SOURCE)
97
98 static void copy_segments(unsigned long ind)
99 {
100         unsigned long entry;
101         unsigned long *ptr;
102         void *dest;
103         void *addr;
104
105         /*
106          * We rely on kexec_load to create a lists that properly
107          * initializes these pointers before they are used.
108          * We will still crash if the list is wrong, but at least
109          * the compiler will be quiet.
110          */
111         ptr = NULL;
112         dest = NULL;
113
114         for (entry = ind; !(entry & IND_DONE); entry = *ptr++) {
115                 addr = __va(entry & PAGE_MASK);
116
117                 switch (entry & IND_FLAGS) {
118                 case IND_DESTINATION:
119                         dest = addr;
120                         break;
121                 case IND_INDIRECTION:
122                         ptr = addr;
123                         break;
124                 case IND_SOURCE:
125                         copy_page(dest, addr);
126                         dest += PAGE_SIZE;
127                 }
128         }
129 }
130
131 void kexec_copy_flush(struct kimage *image)
132 {
133         long i, nr_segments = image->nr_segments;
134         struct  kexec_segment ranges[KEXEC_SEGMENT_MAX];
135
136         /* save the ranges on the stack to efficiently flush the icache */
137         memcpy(ranges, image->segment, sizeof(ranges));
138
139         /*
140          * After this call we may not use anything allocated in dynamic
141          * memory, including *image.
142          *
143          * Only globals and the stack are allowed.
144          */
145         copy_segments(image->head);
146
147         /*
148          * we need to clear the icache for all dest pages sometime,
149          * including ones that were in place on the original copy
150          */
151         for (i = 0; i < nr_segments; i++)
152                 flush_icache_range((unsigned long)__va(ranges[i].mem),
153                         (unsigned long)__va(ranges[i].mem + ranges[i].memsz));
154 }
155
156 #ifdef CONFIG_SMP
157
158 /* FIXME: we should schedule this function to be called on all cpus based
159  * on calling the interrupts, but we would like to call it off irq level
160  * so that the interrupt controller is clean.
161  */
162 static void kexec_smp_down(void *arg)
163 {
164         if (ppc_md.kexec_cpu_down)
165                 ppc_md.kexec_cpu_down(0, 1);
166
167         local_irq_disable();
168         kexec_smp_wait();
169         /* NOTREACHED */
170 }
171
172 static void kexec_prepare_cpus(void)
173 {
174         int my_cpu, i, notified=-1;
175
176         smp_call_function(kexec_smp_down, NULL, /* wait */0);
177         my_cpu = get_cpu();
178
179         /* check the others cpus are now down (via paca hw cpu id == -1) */
180         for (i=0; i < NR_CPUS; i++) {
181                 if (i == my_cpu)
182                         continue;
183
184                 while (paca[i].hw_cpu_id != -1) {
185                         barrier();
186                         if (!cpu_possible(i)) {
187                                 printk("kexec: cpu %d hw_cpu_id %d is not"
188                                                 " possible, ignoring\n",
189                                                 i, paca[i].hw_cpu_id);
190                                 break;
191                         }
192                         if (!cpu_online(i)) {
193                                 /* Fixme: this can be spinning in
194                                  * pSeries_secondary_wait with a paca
195                                  * waiting for it to go online.
196                                  */
197                                 printk("kexec: cpu %d hw_cpu_id %d is not"
198                                                 " online, ignoring\n",
199                                                 i, paca[i].hw_cpu_id);
200                                 break;
201                         }
202                         if (i != notified) {
203                                 printk( "kexec: waiting for cpu %d (physical"
204                                                 " %d) to go down\n",
205                                                 i, paca[i].hw_cpu_id);
206                                 notified = i;
207                         }
208                 }
209         }
210
211         /* after we tell the others to go down */
212         if (ppc_md.kexec_cpu_down)
213                 ppc_md.kexec_cpu_down(0, 0);
214
215         put_cpu();
216
217         local_irq_disable();
218 }
219
220 #else /* ! SMP */
221
222 static void kexec_prepare_cpus(void)
223 {
224         /*
225          * move the secondarys to us so that we can copy
226          * the new kernel 0-0x100 safely
227          *
228          * do this if kexec in setup.c ?
229          *
230          * We need to release the cpus if we are ever going from an
231          * UP to an SMP kernel.
232          */
233         smp_release_cpus();
234         if (ppc_md.kexec_cpu_down)
235                 ppc_md.kexec_cpu_down(0, 0);
236         local_irq_disable();
237 }
238
239 #endif /* SMP */
240
241 /*
242  * kexec thread structure and stack.
243  *
244  * We need to make sure that this is 16384-byte aligned due to the
245  * way process stacks are handled.  It also must be statically allocated
246  * or allocated as part of the kimage, because everything else may be
247  * overwritten when we copy the kexec image.  We piggyback on the
248  * "init_task" linker section here to statically allocate a stack.
249  *
250  * We could use a smaller stack if we don't care about anything using
251  * current, but that audit has not been performed.
252  */
253 static union thread_union kexec_stack __init_task_data =
254         { };
255
256 /* Our assembly helper, in kexec_stub.S */
257 extern NORET_TYPE void kexec_sequence(void *newstack, unsigned long start,
258                                         void *image, void *control,
259                                         void (*clear_all)(void)) ATTRIB_NORET;
260
261 /* too late to fail here */
262 void default_machine_kexec(struct kimage *image)
263 {
264         /* prepare control code if any */
265
266         /*
267         * If the kexec boot is the normal one, need to shutdown other cpus
268         * into our wait loop and quiesce interrupts.
269         * Otherwise, in the case of crashed mode (crashing_cpu >= 0),
270         * stopping other CPUs and collecting their pt_regs is done before
271         * using debugger IPI.
272         */
273
274         if (crashing_cpu == -1)
275                 kexec_prepare_cpus();
276
277         /* switch to a staticly allocated stack.  Based on irq stack code.
278          * XXX: the task struct will likely be invalid once we do the copy!
279          */
280         kexec_stack.thread_info.task = current_thread_info()->task;
281         kexec_stack.thread_info.flags = 0;
282
283         /* Some things are best done in assembly.  Finding globals with
284          * a toc is easier in C, so pass in what we can.
285          */
286         kexec_sequence(&kexec_stack, image->start, image,
287                         page_address(image->control_code_page),
288                         ppc_md.hpte_clear_all);
289         /* NOTREACHED */
290 }
291
292 /* Values we need to export to the second kernel via the device tree. */
293 static unsigned long htab_base;
294
295 static struct property htab_base_prop = {
296         .name = "linux,htab-base",
297         .length = sizeof(unsigned long),
298         .value = &htab_base,
299 };
300
301 static struct property htab_size_prop = {
302         .name = "linux,htab-size",
303         .length = sizeof(unsigned long),
304         .value = &htab_size_bytes,
305 };
306
307 static int __init export_htab_values(void)
308 {
309         struct device_node *node;
310         struct property *prop;
311
312         /* On machines with no htab htab_address is NULL */
313         if (!htab_address)
314                 return -ENODEV;
315
316         node = of_find_node_by_path("/chosen");
317         if (!node)
318                 return -ENODEV;
319
320         /* remove any stale propertys so ours can be found */
321         prop = of_find_property(node, htab_base_prop.name, NULL);
322         if (prop)
323                 prom_remove_property(node, prop);
324         prop = of_find_property(node, htab_size_prop.name, NULL);
325         if (prop)
326                 prom_remove_property(node, prop);
327
328         htab_base = __pa(htab_address);
329         prom_add_property(node, &htab_base_prop);
330         prom_add_property(node, &htab_size_prop);
331
332         of_node_put(node);
333         return 0;
334 }
335 late_initcall(export_htab_values);