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> |
Joe Perches | d200c92 | 2009-09-20 18:14:13 -0400 | [diff] [blame] | 16 | #include <linux/init_task.h> |
R Sharada | fce0d57 | 2005-06-25 14:58:10 -0700 | [diff] [blame] | 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> |
Paul Mackerras | 2249ca9 | 2005-11-07 13:18:13 +1100 | [diff] [blame] | 27 | #include <asm/smp.h> |
R Sharada | fce0d57 | 2005-06-25 14:58:10 -0700 | [diff] [blame] | 28 | |
Michael Ellerman | 3d1229d | 2005-11-14 23:35:00 +1100 | [diff] [blame] | 29 | int default_machine_kexec_prepare(struct kimage *image) |
R Sharada | fce0d57 | 2005-06-25 14:58:10 -0700 | [diff] [blame] | 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; |
Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 35 | const unsigned long *basep; |
| 36 | const unsigned int *sizep; |
R Sharada | fce0d57 | 2005-06-25 14:58:10 -0700 | [diff] [blame] | 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 | */ |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 46 | for (i = 0; i < image->nr_segments; i++) |
R Sharada | fce0d57 | 2005-06-25 14:58:10 -0700 | [diff] [blame] | 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); |
Michael Ellerman | 337a712 | 2006-02-21 17:22:55 +1100 | [diff] [blame] | 62 | high = low + htab_size_bytes; |
R Sharada | fce0d57 | 2005-06-25 14:58:10 -0700 | [diff] [blame] | 63 | |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 64 | for (i = 0; i < image->nr_segments; i++) { |
R Sharada | fce0d57 | 2005-06-25 14:58:10 -0700 | [diff] [blame] | 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")) { |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 76 | basep = of_get_property(node, "linux,tce-base", NULL); |
| 77 | sizep = of_get_property(node, "linux,tce-size", NULL); |
R Sharada | fce0d57 | 2005-06-25 14:58:10 -0700 | [diff] [blame] | 78 | if (basep == NULL || sizep == NULL) |
| 79 | continue; |
| 80 | |
| 81 | low = *basep; |
| 82 | high = low + (*sizep); |
| 83 | |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 84 | for (i = 0; i < image->nr_segments; i++) { |
R Sharada | fce0d57 | 2005-06-25 14:58:10 -0700 | [diff] [blame] | 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 | |
R Sharada | fce0d57 | 2005-06-25 14:58:10 -0700 | [diff] [blame] | 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++) |
Michael Ellerman | b5666f7 | 2005-12-05 10:24:33 -0600 | [diff] [blame] | 152 | flush_icache_range((unsigned long)__va(ranges[i].mem), |
| 153 | (unsigned long)__va(ranges[i].mem + ranges[i].memsz)); |
R Sharada | fce0d57 | 2005-06-25 14:58:10 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | #ifdef CONFIG_SMP |
| 157 | |
Michael Neuling | 1fc711f | 2010-05-13 19:40:11 +0000 | [diff] [blame] | 158 | static int kexec_all_irq_disabled = 0; |
| 159 | |
Michael Ellerman | 1c21a29 | 2008-05-08 14:27:19 +1000 | [diff] [blame] | 160 | static void kexec_smp_down(void *arg) |
R Sharada | fce0d57 | 2005-06-25 14:58:10 -0700 | [diff] [blame] | 161 | { |
Michael Neuling | 1fc711f | 2010-05-13 19:40:11 +0000 | [diff] [blame] | 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 | */ |
Michael Ellerman | c5e2435 | 2005-11-12 00:06:05 +1100 | [diff] [blame] | 172 | if (ppc_md.kexec_cpu_down) |
| 173 | ppc_md.kexec_cpu_down(0, 1); |
R Sharada | fce0d57 | 2005-06-25 14:58:10 -0700 | [diff] [blame] | 174 | |
R Sharada | fce0d57 | 2005-06-25 14:58:10 -0700 | [diff] [blame] | 175 | kexec_smp_wait(); |
| 176 | /* NOTREACHED */ |
| 177 | } |
| 178 | |
Michael Neuling | 1fc711f | 2010-05-13 19:40:11 +0000 | [diff] [blame] | 179 | static void kexec_prepare_cpus_wait(int wait_state) |
R Sharada | fce0d57 | 2005-06-25 14:58:10 -0700 | [diff] [blame] | 180 | { |
| 181 | int my_cpu, i, notified=-1; |
| 182 | |
R Sharada | fce0d57 | 2005-06-25 14:58:10 -0700 | [diff] [blame] | 183 | my_cpu = get_cpu(); |
Michael Neuling | 1fc711f | 2010-05-13 19:40:11 +0000 | [diff] [blame] | 184 | /* Make sure each CPU has atleast made it to the state we need */ |
R Sharada | fce0d57 | 2005-06-25 14:58:10 -0700 | [diff] [blame] | 185 | for (i=0; i < NR_CPUS; i++) { |
| 186 | if (i == my_cpu) |
| 187 | continue; |
| 188 | |
Michael Neuling | 1fc711f | 2010-05-13 19:40:11 +0000 | [diff] [blame] | 189 | while (paca[i].kexec_state < wait_state) { |
Anton Blanchard | b3ca809 | 2005-09-27 21:45:38 -0700 | [diff] [blame] | 190 | barrier(); |
R Sharada | fce0d57 | 2005-06-25 14:58:10 -0700 | [diff] [blame] | 191 | if (!cpu_possible(i)) { |
| 192 | printk("kexec: cpu %d hw_cpu_id %d is not" |
| 193 | " possible, ignoring\n", |
| 194 | i, paca[i].hw_cpu_id); |
| 195 | break; |
| 196 | } |
| 197 | if (!cpu_online(i)) { |
| 198 | /* Fixme: this can be spinning in |
| 199 | * pSeries_secondary_wait with a paca |
| 200 | * waiting for it to go online. |
| 201 | */ |
| 202 | printk("kexec: cpu %d hw_cpu_id %d is not" |
| 203 | " online, ignoring\n", |
| 204 | i, paca[i].hw_cpu_id); |
| 205 | break; |
| 206 | } |
| 207 | if (i != notified) { |
| 208 | printk( "kexec: waiting for cpu %d (physical" |
Michael Neuling | 1fc711f | 2010-05-13 19:40:11 +0000 | [diff] [blame] | 209 | " %d) to enter %i state\n", |
| 210 | i, paca[i].hw_cpu_id, wait_state); |
R Sharada | fce0d57 | 2005-06-25 14:58:10 -0700 | [diff] [blame] | 211 | notified = i; |
| 212 | } |
| 213 | } |
| 214 | } |
Michael Neuling | 1fc711f | 2010-05-13 19:40:11 +0000 | [diff] [blame] | 215 | mb(); |
| 216 | } |
| 217 | |
| 218 | static void kexec_prepare_cpus(void) |
| 219 | { |
| 220 | |
| 221 | smp_call_function(kexec_smp_down, NULL, /* wait */0); |
| 222 | local_irq_disable(); |
| 223 | mb(); /* make sure IRQs are disabled before we say they are */ |
| 224 | get_paca()->kexec_state = KEXEC_STATE_IRQS_OFF; |
| 225 | |
| 226 | kexec_prepare_cpus_wait(KEXEC_STATE_IRQS_OFF); |
| 227 | /* we are sure every CPU has IRQs off at this point */ |
| 228 | kexec_all_irq_disabled = 1; |
R Sharada | fce0d57 | 2005-06-25 14:58:10 -0700 | [diff] [blame] | 229 | |
| 230 | /* after we tell the others to go down */ |
Michael Ellerman | c5e2435 | 2005-11-12 00:06:05 +1100 | [diff] [blame] | 231 | if (ppc_md.kexec_cpu_down) |
| 232 | ppc_md.kexec_cpu_down(0, 0); |
R Sharada | fce0d57 | 2005-06-25 14:58:10 -0700 | [diff] [blame] | 233 | |
Michael Neuling | 1fc711f | 2010-05-13 19:40:11 +0000 | [diff] [blame] | 234 | /* Before removing MMU mapings make sure all CPUs have entered real mode */ |
| 235 | kexec_prepare_cpus_wait(KEXEC_STATE_REAL_MODE); |
R Sharada | fce0d57 | 2005-06-25 14:58:10 -0700 | [diff] [blame] | 236 | |
Michael Neuling | 1fc711f | 2010-05-13 19:40:11 +0000 | [diff] [blame] | 237 | put_cpu(); |
R Sharada | fce0d57 | 2005-06-25 14:58:10 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | #else /* ! SMP */ |
| 241 | |
| 242 | static void kexec_prepare_cpus(void) |
| 243 | { |
| 244 | /* |
| 245 | * move the secondarys to us so that we can copy |
| 246 | * the new kernel 0-0x100 safely |
| 247 | * |
| 248 | * do this if kexec in setup.c ? |
Olof Johansson | 75eedfe | 2005-08-04 12:53:29 -0700 | [diff] [blame] | 249 | * |
| 250 | * We need to release the cpus if we are ever going from an |
| 251 | * UP to an SMP kernel. |
R Sharada | fce0d57 | 2005-06-25 14:58:10 -0700 | [diff] [blame] | 252 | */ |
Olof Johansson | 75eedfe | 2005-08-04 12:53:29 -0700 | [diff] [blame] | 253 | smp_release_cpus(); |
Michael Ellerman | c5e2435 | 2005-11-12 00:06:05 +1100 | [diff] [blame] | 254 | if (ppc_md.kexec_cpu_down) |
| 255 | ppc_md.kexec_cpu_down(0, 0); |
R Sharada | fce0d57 | 2005-06-25 14:58:10 -0700 | [diff] [blame] | 256 | local_irq_disable(); |
| 257 | } |
| 258 | |
| 259 | #endif /* SMP */ |
| 260 | |
| 261 | /* |
| 262 | * kexec thread structure and stack. |
| 263 | * |
| 264 | * We need to make sure that this is 16384-byte aligned due to the |
| 265 | * way process stacks are handled. It also must be statically allocated |
| 266 | * or allocated as part of the kimage, because everything else may be |
| 267 | * overwritten when we copy the kexec image. We piggyback on the |
| 268 | * "init_task" linker section here to statically allocate a stack. |
| 269 | * |
| 270 | * We could use a smaller stack if we don't care about anything using |
| 271 | * current, but that audit has not been performed. |
| 272 | */ |
Joe Perches | d200c92 | 2009-09-20 18:14:13 -0400 | [diff] [blame] | 273 | static union thread_union kexec_stack __init_task_data = |
| 274 | { }; |
R Sharada | fce0d57 | 2005-06-25 14:58:10 -0700 | [diff] [blame] | 275 | |
| 276 | /* Our assembly helper, in kexec_stub.S */ |
| 277 | extern NORET_TYPE void kexec_sequence(void *newstack, unsigned long start, |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 278 | void *image, void *control, |
Milton Miller | 1767c8f | 2008-10-22 10:39:18 +0000 | [diff] [blame] | 279 | void (*clear_all)(void)) ATTRIB_NORET; |
R Sharada | fce0d57 | 2005-06-25 14:58:10 -0700 | [diff] [blame] | 280 | |
| 281 | /* too late to fail here */ |
Michael Ellerman | 3d1229d | 2005-11-14 23:35:00 +1100 | [diff] [blame] | 282 | void default_machine_kexec(struct kimage *image) |
R Sharada | fce0d57 | 2005-06-25 14:58:10 -0700 | [diff] [blame] | 283 | { |
R Sharada | fce0d57 | 2005-06-25 14:58:10 -0700 | [diff] [blame] | 284 | /* prepare control code if any */ |
| 285 | |
Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 286 | /* |
| 287 | * If the kexec boot is the normal one, need to shutdown other cpus |
| 288 | * into our wait loop and quiesce interrupts. |
| 289 | * Otherwise, in the case of crashed mode (crashing_cpu >= 0), |
| 290 | * stopping other CPUs and collecting their pt_regs is done before |
| 291 | * using debugger IPI. |
| 292 | */ |
| 293 | |
Mohan Kumar M | 54622f1 | 2008-10-21 17:38:10 +0000 | [diff] [blame] | 294 | if (crashing_cpu == -1) |
| 295 | kexec_prepare_cpus(); |
R Sharada | fce0d57 | 2005-06-25 14:58:10 -0700 | [diff] [blame] | 296 | |
| 297 | /* switch to a staticly allocated stack. Based on irq stack code. |
| 298 | * XXX: the task struct will likely be invalid once we do the copy! |
| 299 | */ |
| 300 | kexec_stack.thread_info.task = current_thread_info()->task; |
| 301 | kexec_stack.thread_info.flags = 0; |
| 302 | |
| 303 | /* Some things are best done in assembly. Finding globals with |
| 304 | * a toc is easier in C, so pass in what we can. |
| 305 | */ |
| 306 | kexec_sequence(&kexec_stack, image->start, image, |
| 307 | page_address(image->control_code_page), |
Milton Miller | 1767c8f | 2008-10-22 10:39:18 +0000 | [diff] [blame] | 308 | ppc_md.hpte_clear_all); |
R Sharada | fce0d57 | 2005-06-25 14:58:10 -0700 | [diff] [blame] | 309 | /* NOTREACHED */ |
| 310 | } |
Michael Ellerman | 593e537 | 2005-11-12 00:06:06 +1100 | [diff] [blame] | 311 | |
| 312 | /* Values we need to export to the second kernel via the device tree. */ |
Dale Farnsworth | 2e8e4f5 | 2008-12-16 06:22:59 +0000 | [diff] [blame] | 313 | static unsigned long htab_base; |
Michael Ellerman | 593e537 | 2005-11-12 00:06:06 +1100 | [diff] [blame] | 314 | |
| 315 | static struct property htab_base_prop = { |
| 316 | .name = "linux,htab-base", |
| 317 | .length = sizeof(unsigned long), |
Stephen Rothwell | 1a38147 | 2007-04-03 10:58:52 +1000 | [diff] [blame] | 318 | .value = &htab_base, |
Michael Ellerman | 593e537 | 2005-11-12 00:06:06 +1100 | [diff] [blame] | 319 | }; |
| 320 | |
| 321 | static struct property htab_size_prop = { |
| 322 | .name = "linux,htab-size", |
| 323 | .length = sizeof(unsigned long), |
Stephen Rothwell | 1a38147 | 2007-04-03 10:58:52 +1000 | [diff] [blame] | 324 | .value = &htab_size_bytes, |
Michael Ellerman | 593e537 | 2005-11-12 00:06:06 +1100 | [diff] [blame] | 325 | }; |
| 326 | |
Dale Farnsworth | 6f29c32 | 2008-12-17 10:09:06 +0000 | [diff] [blame] | 327 | static int __init export_htab_values(void) |
Michael Ellerman | 593e537 | 2005-11-12 00:06:06 +1100 | [diff] [blame] | 328 | { |
| 329 | struct device_node *node; |
Milton Miller | ed7b214 | 2008-10-20 15:37:03 +0000 | [diff] [blame] | 330 | struct property *prop; |
Michael Ellerman | 593e537 | 2005-11-12 00:06:06 +1100 | [diff] [blame] | 331 | |
Dale Farnsworth | 2e8e4f5 | 2008-12-16 06:22:59 +0000 | [diff] [blame] | 332 | /* On machines with no htab htab_address is NULL */ |
| 333 | if (!htab_address) |
Dale Farnsworth | 6f29c32 | 2008-12-17 10:09:06 +0000 | [diff] [blame] | 334 | return -ENODEV; |
Dale Farnsworth | 2e8e4f5 | 2008-12-16 06:22:59 +0000 | [diff] [blame] | 335 | |
Michael Ellerman | 593e537 | 2005-11-12 00:06:06 +1100 | [diff] [blame] | 336 | node = of_find_node_by_path("/chosen"); |
| 337 | if (!node) |
Dale Farnsworth | 6f29c32 | 2008-12-17 10:09:06 +0000 | [diff] [blame] | 338 | return -ENODEV; |
Michael Ellerman | 593e537 | 2005-11-12 00:06:06 +1100 | [diff] [blame] | 339 | |
Milton Miller | ed7b214 | 2008-10-20 15:37:03 +0000 | [diff] [blame] | 340 | /* remove any stale propertys so ours can be found */ |
Milton Miller | ed7b214 | 2008-10-20 15:37:03 +0000 | [diff] [blame] | 341 | prop = of_find_property(node, htab_base_prop.name, NULL); |
| 342 | if (prop) |
| 343 | prom_remove_property(node, prop); |
| 344 | prop = of_find_property(node, htab_size_prop.name, NULL); |
| 345 | if (prop) |
| 346 | prom_remove_property(node, prop); |
| 347 | |
Michael Ellerman | 593e537 | 2005-11-12 00:06:06 +1100 | [diff] [blame] | 348 | htab_base = __pa(htab_address); |
| 349 | prom_add_property(node, &htab_base_prop); |
Michael Ellerman | 593e537 | 2005-11-12 00:06:06 +1100 | [diff] [blame] | 350 | prom_add_property(node, &htab_size_prop); |
| 351 | |
Michael Ellerman | 593e537 | 2005-11-12 00:06:06 +1100 | [diff] [blame] | 352 | of_node_put(node); |
Michael Ellerman | aa98c50 | 2006-06-23 18:17:32 +1000 | [diff] [blame] | 353 | return 0; |
Michael Ellerman | 35dd543 | 2006-05-18 11:16:11 +1000 | [diff] [blame] | 354 | } |
Dale Farnsworth | 6f29c32 | 2008-12-17 10:09:06 +0000 | [diff] [blame] | 355 | late_initcall(export_htab_values); |