Commit | Line | Data |
---|---|---|
fce0d574 | 1 | /* |
3d1229d6 | 2 | * PPC64 code to handle Linux booting another kernel. |
fce0d574 S |
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 | ||
fce0d574 S |
13 | #include <linux/kexec.h> |
14 | #include <linux/smp.h> | |
15 | #include <linux/thread_info.h> | |
d200c922 | 16 | #include <linux/init_task.h> |
fce0d574 S |
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> | |
2249ca9d | 27 | #include <asm/smp.h> |
5aae8a53 | 28 | #include <asm/hw_breakpoint.h> |
fce0d574 | 29 | |
3d1229d6 | 30 | int default_machine_kexec_prepare(struct kimage *image) |
fce0d574 S |
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; | |
a7f67bdf JK |
36 | const unsigned long *basep; |
37 | const unsigned int *sizep; | |
fce0d574 S |
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 | */ | |
72414d3f | 47 | for (i = 0; i < image->nr_segments; i++) |
fce0d574 S |
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); | |
337a7128 | 63 | high = low + htab_size_bytes; |
fce0d574 | 64 | |
72414d3f | 65 | for (i = 0; i < image->nr_segments; i++) { |
fce0d574 S |
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")) { | |
e2eb6392 SR |
77 | basep = of_get_property(node, "linux,tce-base", NULL); |
78 | sizep = of_get_property(node, "linux,tce-size", NULL); | |
fce0d574 S |
79 | if (basep == NULL || sizep == NULL) |
80 | continue; | |
81 | ||
82 | low = *basep; | |
83 | high = low + (*sizep); | |
84 | ||
72414d3f | 85 | for (i = 0; i < image->nr_segments; i++) { |
fce0d574 S |
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 | ||
fce0d574 S |
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++) | |
b5666f70 ME |
153 | flush_icache_range((unsigned long)__va(ranges[i].mem), |
154 | (unsigned long)__va(ranges[i].mem + ranges[i].memsz)); | |
fce0d574 S |
155 | } |
156 | ||
157 | #ifdef CONFIG_SMP | |
158 | ||
1fc711f7 MN |
159 | static int kexec_all_irq_disabled = 0; |
160 | ||
1c21a293 | 161 | static void kexec_smp_down(void *arg) |
fce0d574 | 162 | { |
1fc711f7 MN |
163 | local_irq_disable(); |
164 | mb(); /* make sure our irqs are disabled before we say they are */ | |
165 | get_paca()->kexec_state = KEXEC_STATE_IRQS_OFF; | |
166 | while(kexec_all_irq_disabled == 0) | |
167 | cpu_relax(); | |
168 | mb(); /* make sure all irqs are disabled before this */ | |
5aae8a53 | 169 | hw_breakpoint_disable(); |
1fc711f7 MN |
170 | /* |
171 | * Now every CPU has IRQs off, we can clear out any pending | |
172 | * IPIs and be sure that no more will come in after this. | |
173 | */ | |
c5e24354 ME |
174 | if (ppc_md.kexec_cpu_down) |
175 | ppc_md.kexec_cpu_down(0, 1); | |
fce0d574 | 176 | |
fce0d574 S |
177 | kexec_smp_wait(); |
178 | /* NOTREACHED */ | |
179 | } | |
180 | ||
1fc711f7 | 181 | static void kexec_prepare_cpus_wait(int wait_state) |
fce0d574 S |
182 | { |
183 | int my_cpu, i, notified=-1; | |
184 | ||
5aae8a53 | 185 | hw_breakpoint_disable(); |
fce0d574 | 186 | my_cpu = get_cpu(); |
1fc711f7 | 187 | /* Make sure each CPU has atleast made it to the state we need */ |
b636f137 | 188 | for_each_online_cpu(i) { |
fce0d574 S |
189 | if (i == my_cpu) |
190 | continue; | |
191 | ||
1fc711f7 | 192 | while (paca[i].kexec_state < wait_state) { |
b3ca8093 | 193 | barrier(); |
fce0d574 S |
194 | if (i != notified) { |
195 | printk( "kexec: waiting for cpu %d (physical" | |
1fc711f7 MN |
196 | " %d) to enter %i state\n", |
197 | i, paca[i].hw_cpu_id, wait_state); | |
fce0d574 S |
198 | notified = i; |
199 | } | |
200 | } | |
201 | } | |
1fc711f7 MN |
202 | mb(); |
203 | } | |
204 | ||
205 | static void kexec_prepare_cpus(void) | |
206 | { | |
207 | ||
208 | smp_call_function(kexec_smp_down, NULL, /* wait */0); | |
209 | local_irq_disable(); | |
210 | mb(); /* make sure IRQs are disabled before we say they are */ | |
211 | get_paca()->kexec_state = KEXEC_STATE_IRQS_OFF; | |
212 | ||
213 | kexec_prepare_cpus_wait(KEXEC_STATE_IRQS_OFF); | |
214 | /* we are sure every CPU has IRQs off at this point */ | |
215 | kexec_all_irq_disabled = 1; | |
fce0d574 S |
216 | |
217 | /* after we tell the others to go down */ | |
c5e24354 ME |
218 | if (ppc_md.kexec_cpu_down) |
219 | ppc_md.kexec_cpu_down(0, 0); | |
fce0d574 | 220 | |
1fc711f7 MN |
221 | /* Before removing MMU mapings make sure all CPUs have entered real mode */ |
222 | kexec_prepare_cpus_wait(KEXEC_STATE_REAL_MODE); | |
fce0d574 | 223 | |
1fc711f7 | 224 | put_cpu(); |
fce0d574 S |
225 | } |
226 | ||
227 | #else /* ! SMP */ | |
228 | ||
229 | static void kexec_prepare_cpus(void) | |
230 | { | |
231 | /* | |
232 | * move the secondarys to us so that we can copy | |
233 | * the new kernel 0-0x100 safely | |
234 | * | |
235 | * do this if kexec in setup.c ? | |
75eedfed OJ |
236 | * |
237 | * We need to release the cpus if we are ever going from an | |
238 | * UP to an SMP kernel. | |
fce0d574 | 239 | */ |
75eedfed | 240 | smp_release_cpus(); |
c5e24354 ME |
241 | if (ppc_md.kexec_cpu_down) |
242 | ppc_md.kexec_cpu_down(0, 0); | |
fce0d574 S |
243 | local_irq_disable(); |
244 | } | |
245 | ||
246 | #endif /* SMP */ | |
247 | ||
248 | /* | |
249 | * kexec thread structure and stack. | |
250 | * | |
251 | * We need to make sure that this is 16384-byte aligned due to the | |
252 | * way process stacks are handled. It also must be statically allocated | |
253 | * or allocated as part of the kimage, because everything else may be | |
254 | * overwritten when we copy the kexec image. We piggyback on the | |
255 | * "init_task" linker section here to statically allocate a stack. | |
256 | * | |
257 | * We could use a smaller stack if we don't care about anything using | |
258 | * current, but that audit has not been performed. | |
259 | */ | |
d200c922 JP |
260 | static union thread_union kexec_stack __init_task_data = |
261 | { }; | |
fce0d574 | 262 | |
fc53b420 ME |
263 | /* |
264 | * For similar reasons to the stack above, the kexecing CPU needs to be on a | |
265 | * static PACA; we switch to kexec_paca. | |
266 | */ | |
267 | struct paca_struct kexec_paca; | |
268 | ||
fce0d574 S |
269 | /* Our assembly helper, in kexec_stub.S */ |
270 | extern NORET_TYPE void kexec_sequence(void *newstack, unsigned long start, | |
72414d3f | 271 | void *image, void *control, |
1767c8f3 | 272 | void (*clear_all)(void)) ATTRIB_NORET; |
fce0d574 S |
273 | |
274 | /* too late to fail here */ | |
3d1229d6 | 275 | void default_machine_kexec(struct kimage *image) |
fce0d574 | 276 | { |
fce0d574 S |
277 | /* prepare control code if any */ |
278 | ||
cc532915 ME |
279 | /* |
280 | * If the kexec boot is the normal one, need to shutdown other cpus | |
281 | * into our wait loop and quiesce interrupts. | |
282 | * Otherwise, in the case of crashed mode (crashing_cpu >= 0), | |
283 | * stopping other CPUs and collecting their pt_regs is done before | |
284 | * using debugger IPI. | |
285 | */ | |
286 | ||
54622f10 MK |
287 | if (crashing_cpu == -1) |
288 | kexec_prepare_cpus(); | |
fce0d574 S |
289 | |
290 | /* switch to a staticly allocated stack. Based on irq stack code. | |
291 | * XXX: the task struct will likely be invalid once we do the copy! | |
292 | */ | |
293 | kexec_stack.thread_info.task = current_thread_info()->task; | |
294 | kexec_stack.thread_info.flags = 0; | |
295 | ||
fc53b420 ME |
296 | /* We need a static PACA, too; copy this CPU's PACA over and switch to |
297 | * it. Also poison per_cpu_offset to catch anyone using non-static | |
298 | * data. | |
299 | */ | |
300 | memcpy(&kexec_paca, get_paca(), sizeof(struct paca_struct)); | |
301 | kexec_paca.data_offset = 0xedeaddeadeeeeeeeUL; | |
302 | paca = (struct paca_struct *)RELOC_HIDE(&kexec_paca, 0) - | |
303 | kexec_paca.paca_index; | |
304 | setup_paca(&kexec_paca); | |
305 | ||
306 | /* XXX: If anyone does 'dynamic lppacas' this will also need to be | |
307 | * switched to a static version! | |
308 | */ | |
309 | ||
fce0d574 S |
310 | /* Some things are best done in assembly. Finding globals with |
311 | * a toc is easier in C, so pass in what we can. | |
312 | */ | |
313 | kexec_sequence(&kexec_stack, image->start, image, | |
314 | page_address(image->control_code_page), | |
1767c8f3 | 315 | ppc_md.hpte_clear_all); |
fce0d574 S |
316 | /* NOTREACHED */ |
317 | } | |
593e537b ME |
318 | |
319 | /* Values we need to export to the second kernel via the device tree. */ | |
2e8e4f5b | 320 | static unsigned long htab_base; |
593e537b ME |
321 | |
322 | static struct property htab_base_prop = { | |
323 | .name = "linux,htab-base", | |
324 | .length = sizeof(unsigned long), | |
1a38147e | 325 | .value = &htab_base, |
593e537b ME |
326 | }; |
327 | ||
328 | static struct property htab_size_prop = { | |
329 | .name = "linux,htab-size", | |
330 | .length = sizeof(unsigned long), | |
1a38147e | 331 | .value = &htab_size_bytes, |
593e537b ME |
332 | }; |
333 | ||
6f29c329 | 334 | static int __init export_htab_values(void) |
593e537b ME |
335 | { |
336 | struct device_node *node; | |
ed7b2144 | 337 | struct property *prop; |
593e537b | 338 | |
2e8e4f5b DF |
339 | /* On machines with no htab htab_address is NULL */ |
340 | if (!htab_address) | |
6f29c329 | 341 | return -ENODEV; |
2e8e4f5b | 342 | |
593e537b ME |
343 | node = of_find_node_by_path("/chosen"); |
344 | if (!node) | |
6f29c329 | 345 | return -ENODEV; |
593e537b | 346 | |
ed7b2144 | 347 | /* remove any stale propertys so ours can be found */ |
ed7b2144 MM |
348 | prop = of_find_property(node, htab_base_prop.name, NULL); |
349 | if (prop) | |
350 | prom_remove_property(node, prop); | |
351 | prop = of_find_property(node, htab_size_prop.name, NULL); | |
352 | if (prop) | |
353 | prom_remove_property(node, prop); | |
354 | ||
593e537b ME |
355 | htab_base = __pa(htab_address); |
356 | prom_add_property(node, &htab_base_prop); | |
593e537b ME |
357 | prom_add_property(node, &htab_size_prop); |
358 | ||
593e537b | 359 | of_node_put(node); |
aa98c50d | 360 | return 0; |
35dd5432 | 361 | } |
6f29c329 | 362 | late_initcall(export_htab_values); |