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