]>
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 | ||
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/mmu.h> | |
25 | #include <asm/sections.h> /* _end */ | |
26 | #include <asm/prom.h> | |
2249ca9d | 27 | #include <asm/smp.h> |
fce0d574 S |
28 | |
29 | #define HASH_GROUP_SIZE 0x80 /* size of each hash group, asm/mmu.h */ | |
30 | ||
3d1229d6 | 31 | int default_machine_kexec_prepare(struct kimage *image) |
fce0d574 S |
32 | { |
33 | int i; | |
34 | unsigned long begin, end; /* limits of segment */ | |
35 | unsigned long low, high; /* limits of blocked memory range */ | |
36 | struct device_node *node; | |
37 | unsigned long *basep; | |
38 | unsigned int *sizep; | |
39 | ||
40 | if (!ppc_md.hpte_clear_all) | |
41 | return -ENOENT; | |
42 | ||
43 | /* | |
44 | * Since we use the kernel fault handlers and paging code to | |
45 | * handle the virtual mode, we must make sure no destination | |
46 | * overlaps kernel static data or bss. | |
47 | */ | |
72414d3f | 48 | for (i = 0; i < image->nr_segments; i++) |
fce0d574 S |
49 | if (image->segment[i].mem < __pa(_end)) |
50 | return -ETXTBSY; | |
51 | ||
52 | /* | |
53 | * For non-LPAR, we absolutely can not overwrite the mmu hash | |
54 | * table, since we are still using the bolted entries in it to | |
55 | * do the copy. Check that here. | |
56 | * | |
57 | * It is safe if the end is below the start of the blocked | |
58 | * region (end <= low), or if the beginning is after the | |
59 | * end of the blocked region (begin >= high). Use the | |
60 | * boolean identity !(a || b) === (!a && !b). | |
61 | */ | |
62 | if (htab_address) { | |
63 | low = __pa(htab_address); | |
64 | high = low + (htab_hash_mask + 1) * HASH_GROUP_SIZE; | |
65 | ||
72414d3f | 66 | for (i = 0; i < image->nr_segments; i++) { |
fce0d574 S |
67 | begin = image->segment[i].mem; |
68 | end = begin + image->segment[i].memsz; | |
69 | ||
70 | if ((begin < high) && (end > low)) | |
71 | return -ETXTBSY; | |
72 | } | |
73 | } | |
74 | ||
75 | /* We also should not overwrite the tce tables */ | |
76 | for (node = of_find_node_by_type(NULL, "pci"); node != NULL; | |
77 | node = of_find_node_by_type(node, "pci")) { | |
78 | basep = (unsigned long *)get_property(node, "linux,tce-base", | |
79 | NULL); | |
80 | sizep = (unsigned int *)get_property(node, "linux,tce-size", | |
81 | NULL); | |
82 | if (basep == NULL || sizep == NULL) | |
83 | continue; | |
84 | ||
85 | low = *basep; | |
86 | high = low + (*sizep); | |
87 | ||
72414d3f | 88 | for (i = 0; i < image->nr_segments; i++) { |
fce0d574 S |
89 | begin = image->segment[i].mem; |
90 | end = begin + image->segment[i].memsz; | |
91 | ||
92 | if ((begin < high) && (end > low)) | |
93 | return -ETXTBSY; | |
94 | } | |
95 | } | |
96 | ||
97 | return 0; | |
98 | } | |
99 | ||
fce0d574 S |
100 | #define IND_FLAGS (IND_DESTINATION | IND_INDIRECTION | IND_DONE | IND_SOURCE) |
101 | ||
102 | static void copy_segments(unsigned long ind) | |
103 | { | |
104 | unsigned long entry; | |
105 | unsigned long *ptr; | |
106 | void *dest; | |
107 | void *addr; | |
108 | ||
109 | /* | |
110 | * We rely on kexec_load to create a lists that properly | |
111 | * initializes these pointers before they are used. | |
112 | * We will still crash if the list is wrong, but at least | |
113 | * the compiler will be quiet. | |
114 | */ | |
115 | ptr = NULL; | |
116 | dest = NULL; | |
117 | ||
118 | for (entry = ind; !(entry & IND_DONE); entry = *ptr++) { | |
119 | addr = __va(entry & PAGE_MASK); | |
120 | ||
121 | switch (entry & IND_FLAGS) { | |
122 | case IND_DESTINATION: | |
123 | dest = addr; | |
124 | break; | |
125 | case IND_INDIRECTION: | |
126 | ptr = addr; | |
127 | break; | |
128 | case IND_SOURCE: | |
129 | copy_page(dest, addr); | |
130 | dest += PAGE_SIZE; | |
131 | } | |
132 | } | |
133 | } | |
134 | ||
135 | void kexec_copy_flush(struct kimage *image) | |
136 | { | |
137 | long i, nr_segments = image->nr_segments; | |
138 | struct kexec_segment ranges[KEXEC_SEGMENT_MAX]; | |
139 | ||
140 | /* save the ranges on the stack to efficiently flush the icache */ | |
141 | memcpy(ranges, image->segment, sizeof(ranges)); | |
142 | ||
143 | /* | |
144 | * After this call we may not use anything allocated in dynamic | |
145 | * memory, including *image. | |
146 | * | |
147 | * Only globals and the stack are allowed. | |
148 | */ | |
149 | copy_segments(image->head); | |
150 | ||
151 | /* | |
152 | * we need to clear the icache for all dest pages sometime, | |
153 | * including ones that were in place on the original copy | |
154 | */ | |
155 | for (i = 0; i < nr_segments; i++) | |
b5666f70 ME |
156 | flush_icache_range((unsigned long)__va(ranges[i].mem), |
157 | (unsigned long)__va(ranges[i].mem + ranges[i].memsz)); | |
fce0d574 S |
158 | } |
159 | ||
160 | #ifdef CONFIG_SMP | |
161 | ||
162 | /* FIXME: we should schedule this function to be called on all cpus based | |
163 | * on calling the interrupts, but we would like to call it off irq level | |
164 | * so that the interrupt controller is clean. | |
165 | */ | |
166 | void kexec_smp_down(void *arg) | |
167 | { | |
c5e24354 ME |
168 | if (ppc_md.kexec_cpu_down) |
169 | ppc_md.kexec_cpu_down(0, 1); | |
fce0d574 S |
170 | |
171 | local_irq_disable(); | |
172 | kexec_smp_wait(); | |
173 | /* NOTREACHED */ | |
174 | } | |
175 | ||
176 | static void kexec_prepare_cpus(void) | |
177 | { | |
178 | int my_cpu, i, notified=-1; | |
179 | ||
180 | smp_call_function(kexec_smp_down, NULL, 0, /* wait */0); | |
181 | my_cpu = get_cpu(); | |
182 | ||
183 | /* check the others cpus are now down (via paca hw cpu id == -1) */ | |
184 | for (i=0; i < NR_CPUS; i++) { | |
185 | if (i == my_cpu) | |
186 | continue; | |
187 | ||
188 | while (paca[i].hw_cpu_id != -1) { | |
b3ca8093 | 189 | barrier(); |
fce0d574 S |
190 | if (!cpu_possible(i)) { |
191 | printk("kexec: cpu %d hw_cpu_id %d is not" | |
192 | " possible, ignoring\n", | |
193 | i, paca[i].hw_cpu_id); | |
194 | break; | |
195 | } | |
196 | if (!cpu_online(i)) { | |
197 | /* Fixme: this can be spinning in | |
198 | * pSeries_secondary_wait with a paca | |
199 | * waiting for it to go online. | |
200 | */ | |
201 | printk("kexec: cpu %d hw_cpu_id %d is not" | |
202 | " online, ignoring\n", | |
203 | i, paca[i].hw_cpu_id); | |
204 | break; | |
205 | } | |
206 | if (i != notified) { | |
207 | printk( "kexec: waiting for cpu %d (physical" | |
208 | " %d) to go down\n", | |
209 | i, paca[i].hw_cpu_id); | |
210 | notified = i; | |
211 | } | |
212 | } | |
213 | } | |
214 | ||
215 | /* after we tell the others to go down */ | |
c5e24354 ME |
216 | if (ppc_md.kexec_cpu_down) |
217 | ppc_md.kexec_cpu_down(0, 0); | |
fce0d574 S |
218 | |
219 | put_cpu(); | |
220 | ||
221 | local_irq_disable(); | |
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 | */ | |
257 | union thread_union kexec_stack | |
258 | __attribute__((__section__(".data.init_task"))) = { }; | |
259 | ||
260 | /* Our assembly helper, in kexec_stub.S */ | |
261 | extern NORET_TYPE void kexec_sequence(void *newstack, unsigned long start, | |
72414d3f MS |
262 | void *image, void *control, |
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 S |
267 | { |
268 | ||
269 | /* prepare control code if any */ | |
270 | ||
271 | /* shutdown other cpus into our wait loop and quiesce interrupts */ | |
272 | kexec_prepare_cpus(); | |
273 | ||
274 | /* switch to a staticly allocated stack. Based on irq stack code. | |
275 | * XXX: the task struct will likely be invalid once we do the copy! | |
276 | */ | |
277 | kexec_stack.thread_info.task = current_thread_info()->task; | |
278 | kexec_stack.thread_info.flags = 0; | |
279 | ||
280 | /* Some things are best done in assembly. Finding globals with | |
281 | * a toc is easier in C, so pass in what we can. | |
282 | */ | |
283 | kexec_sequence(&kexec_stack, image->start, image, | |
284 | page_address(image->control_code_page), | |
285 | ppc_md.hpte_clear_all); | |
286 | /* NOTREACHED */ | |
287 | } | |
593e537b ME |
288 | |
289 | /* Values we need to export to the second kernel via the device tree. */ | |
290 | static unsigned long htab_base, htab_size, kernel_end; | |
291 | ||
292 | static struct property htab_base_prop = { | |
293 | .name = "linux,htab-base", | |
294 | .length = sizeof(unsigned long), | |
295 | .value = (unsigned char *)&htab_base, | |
296 | }; | |
297 | ||
298 | static struct property htab_size_prop = { | |
299 | .name = "linux,htab-size", | |
300 | .length = sizeof(unsigned long), | |
301 | .value = (unsigned char *)&htab_size, | |
302 | }; | |
303 | ||
304 | static struct property kernel_end_prop = { | |
305 | .name = "linux,kernel-end", | |
306 | .length = sizeof(unsigned long), | |
307 | .value = (unsigned char *)&kernel_end, | |
308 | }; | |
309 | ||
310 | static void __init export_htab_values(void) | |
311 | { | |
312 | struct device_node *node; | |
313 | ||
314 | node = of_find_node_by_path("/chosen"); | |
315 | if (!node) | |
316 | return; | |
317 | ||
318 | kernel_end = __pa(_end); | |
319 | prom_add_property(node, &kernel_end_prop); | |
320 | ||
321 | /* On machines with no htab htab_address is NULL */ | |
322 | if (NULL == htab_address) | |
323 | goto out; | |
324 | ||
325 | htab_base = __pa(htab_address); | |
326 | prom_add_property(node, &htab_base_prop); | |
327 | ||
328 | htab_size = 1UL << ppc64_pft_size; | |
329 | prom_add_property(node, &htab_size_prop); | |
330 | ||
331 | out: | |
332 | of_node_put(node); | |
333 | } | |
334 | ||
335 | void __init kexec_setup(void) | |
336 | { | |
337 | export_htab_values(); | |
338 | } |