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