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