Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Smp support for ppc. |
| 3 | * |
| 4 | * Written by Cort Dougan (cort@cs.nmt.edu) borrowing a great |
| 5 | * deal of code from the sparc and intel versions. |
| 6 | * |
| 7 | * Copyright (C) 1999 Cort Dougan <cort@cs.nmt.edu> |
| 8 | * |
| 9 | */ |
| 10 | |
| 11 | #include <linux/config.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/sched.h> |
| 15 | #include <linux/smp.h> |
| 16 | #include <linux/smp_lock.h> |
| 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/kernel_stat.h> |
| 19 | #include <linux/delay.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/spinlock.h> |
| 22 | #include <linux/cache.h> |
| 23 | |
| 24 | #include <asm/ptrace.h> |
| 25 | #include <asm/atomic.h> |
| 26 | #include <asm/irq.h> |
| 27 | #include <asm/page.h> |
| 28 | #include <asm/pgtable.h> |
| 29 | #include <asm/io.h> |
| 30 | #include <asm/prom.h> |
| 31 | #include <asm/smp.h> |
| 32 | #include <asm/residual.h> |
| 33 | #include <asm/time.h> |
| 34 | #include <asm/thread_info.h> |
| 35 | #include <asm/tlbflush.h> |
| 36 | #include <asm/xmon.h> |
| 37 | |
| 38 | volatile int smp_commenced; |
| 39 | int smp_tb_synchronized; |
| 40 | struct cpuinfo_PPC cpu_data[NR_CPUS]; |
| 41 | struct klock_info_struct klock_info = { KLOCK_CLEAR, 0 }; |
| 42 | atomic_t ipi_recv; |
| 43 | atomic_t ipi_sent; |
| 44 | cpumask_t cpu_online_map; |
| 45 | cpumask_t cpu_possible_map; |
| 46 | int smp_hw_index[NR_CPUS]; |
| 47 | struct thread_info *secondary_ti; |
| 48 | |
| 49 | EXPORT_SYMBOL(cpu_online_map); |
| 50 | EXPORT_SYMBOL(cpu_possible_map); |
| 51 | |
| 52 | /* SMP operations for this machine */ |
| 53 | static struct smp_ops_t *smp_ops; |
| 54 | |
| 55 | /* all cpu mappings are 1-1 -- Cort */ |
| 56 | volatile unsigned long cpu_callin_map[NR_CPUS]; |
| 57 | |
| 58 | int start_secondary(void *); |
| 59 | void smp_call_function_interrupt(void); |
| 60 | static int __smp_call_function(void (*func) (void *info), void *info, |
| 61 | int wait, int target); |
| 62 | |
| 63 | /* Low level assembly function used to backup CPU 0 state */ |
| 64 | extern void __save_cpu_setup(void); |
| 65 | |
| 66 | /* Since OpenPIC has only 4 IPIs, we use slightly different message numbers. |
| 67 | * |
| 68 | * Make sure this matches openpic_request_IPIs in open_pic.c, or what shows up |
| 69 | * in /proc/interrupts will be wrong!!! --Troy */ |
| 70 | #define PPC_MSG_CALL_FUNCTION 0 |
| 71 | #define PPC_MSG_RESCHEDULE 1 |
| 72 | #define PPC_MSG_INVALIDATE_TLB 2 |
| 73 | #define PPC_MSG_XMON_BREAK 3 |
| 74 | |
| 75 | static inline void |
| 76 | smp_message_pass(int target, int msg, unsigned long data, int wait) |
| 77 | { |
| 78 | if (smp_ops){ |
| 79 | atomic_inc(&ipi_sent); |
| 80 | smp_ops->message_pass(target,msg,data,wait); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | * Common functions |
| 86 | */ |
| 87 | void smp_message_recv(int msg, struct pt_regs *regs) |
| 88 | { |
| 89 | atomic_inc(&ipi_recv); |
| 90 | |
| 91 | switch( msg ) { |
| 92 | case PPC_MSG_CALL_FUNCTION: |
| 93 | smp_call_function_interrupt(); |
| 94 | break; |
| 95 | case PPC_MSG_RESCHEDULE: |
| 96 | set_need_resched(); |
| 97 | break; |
| 98 | case PPC_MSG_INVALIDATE_TLB: |
| 99 | _tlbia(); |
| 100 | break; |
| 101 | #ifdef CONFIG_XMON |
| 102 | case PPC_MSG_XMON_BREAK: |
| 103 | xmon(regs); |
| 104 | break; |
| 105 | #endif /* CONFIG_XMON */ |
| 106 | default: |
| 107 | printk("SMP %d: smp_message_recv(): unknown msg %d\n", |
| 108 | smp_processor_id(), msg); |
| 109 | break; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | * 750's don't broadcast tlb invalidates so |
| 115 | * we have to emulate that behavior. |
| 116 | * -- Cort |
| 117 | */ |
| 118 | void smp_send_tlb_invalidate(int cpu) |
| 119 | { |
| 120 | if ( PVR_VER(mfspr(SPRN_PVR)) == 8 ) |
| 121 | smp_message_pass(MSG_ALL_BUT_SELF, PPC_MSG_INVALIDATE_TLB, 0, 0); |
| 122 | } |
| 123 | |
| 124 | void smp_send_reschedule(int cpu) |
| 125 | { |
| 126 | /* |
| 127 | * This is only used if `cpu' is running an idle task, |
| 128 | * so it will reschedule itself anyway... |
| 129 | * |
| 130 | * This isn't the case anymore since the other CPU could be |
| 131 | * sleeping and won't reschedule until the next interrupt (such |
| 132 | * as the timer). |
| 133 | * -- Cort |
| 134 | */ |
| 135 | /* This is only used if `cpu' is running an idle task, |
| 136 | so it will reschedule itself anyway... */ |
| 137 | smp_message_pass(cpu, PPC_MSG_RESCHEDULE, 0, 0); |
| 138 | } |
| 139 | |
| 140 | #ifdef CONFIG_XMON |
| 141 | void smp_send_xmon_break(int cpu) |
| 142 | { |
| 143 | smp_message_pass(cpu, PPC_MSG_XMON_BREAK, 0, 0); |
| 144 | } |
| 145 | #endif /* CONFIG_XMON */ |
| 146 | |
| 147 | static void stop_this_cpu(void *dummy) |
| 148 | { |
| 149 | local_irq_disable(); |
| 150 | while (1) |
| 151 | ; |
| 152 | } |
| 153 | |
| 154 | void smp_send_stop(void) |
| 155 | { |
| 156 | smp_call_function(stop_this_cpu, NULL, 1, 0); |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * Structure and data for smp_call_function(). This is designed to minimise |
| 161 | * static memory requirements. It also looks cleaner. |
| 162 | * Stolen from the i386 version. |
| 163 | */ |
| 164 | static DEFINE_SPINLOCK(call_lock); |
| 165 | |
| 166 | static struct call_data_struct { |
| 167 | void (*func) (void *info); |
| 168 | void *info; |
| 169 | atomic_t started; |
| 170 | atomic_t finished; |
| 171 | int wait; |
| 172 | } *call_data; |
| 173 | |
| 174 | /* |
| 175 | * this function sends a 'generic call function' IPI to all other CPUs |
| 176 | * in the system. |
| 177 | */ |
| 178 | |
| 179 | int smp_call_function(void (*func) (void *info), void *info, int nonatomic, |
| 180 | int wait) |
| 181 | /* |
| 182 | * [SUMMARY] Run a function on all other CPUs. |
| 183 | * <func> The function to run. This must be fast and non-blocking. |
| 184 | * <info> An arbitrary pointer to pass to the function. |
| 185 | * <nonatomic> currently unused. |
| 186 | * <wait> If true, wait (atomically) until function has completed on other CPUs. |
| 187 | * [RETURNS] 0 on success, else a negative status code. Does not return until |
| 188 | * remote CPUs are nearly ready to execute <<func>> or are or have executed. |
| 189 | * |
| 190 | * You must not call this function with disabled interrupts or from a |
| 191 | * hardware interrupt handler or from a bottom half handler. |
| 192 | */ |
| 193 | { |
| 194 | /* FIXME: get cpu lock with hotplug cpus, or change this to |
| 195 | bitmask. --RR */ |
| 196 | if (num_online_cpus() <= 1) |
| 197 | return 0; |
| 198 | /* Can deadlock when called with interrupts disabled */ |
| 199 | WARN_ON(irqs_disabled()); |
| 200 | return __smp_call_function(func, info, wait, MSG_ALL_BUT_SELF); |
| 201 | } |
| 202 | |
| 203 | static int __smp_call_function(void (*func) (void *info), void *info, |
| 204 | int wait, int target) |
| 205 | { |
| 206 | struct call_data_struct data; |
| 207 | int ret = -1; |
| 208 | int timeout; |
| 209 | int ncpus = 1; |
| 210 | |
| 211 | if (target == MSG_ALL_BUT_SELF) |
| 212 | ncpus = num_online_cpus() - 1; |
| 213 | else if (target == MSG_ALL) |
| 214 | ncpus = num_online_cpus(); |
| 215 | |
| 216 | data.func = func; |
| 217 | data.info = info; |
| 218 | atomic_set(&data.started, 0); |
| 219 | data.wait = wait; |
| 220 | if (wait) |
| 221 | atomic_set(&data.finished, 0); |
| 222 | |
| 223 | spin_lock(&call_lock); |
| 224 | call_data = &data; |
| 225 | /* Send a message to all other CPUs and wait for them to respond */ |
| 226 | smp_message_pass(target, PPC_MSG_CALL_FUNCTION, 0, 0); |
| 227 | |
| 228 | /* Wait for response */ |
| 229 | timeout = 1000000; |
| 230 | while (atomic_read(&data.started) != ncpus) { |
| 231 | if (--timeout == 0) { |
| 232 | printk("smp_call_function on cpu %d: other cpus not responding (%d)\n", |
| 233 | smp_processor_id(), atomic_read(&data.started)); |
| 234 | goto out; |
| 235 | } |
| 236 | barrier(); |
| 237 | udelay(1); |
| 238 | } |
| 239 | |
| 240 | if (wait) { |
| 241 | timeout = 1000000; |
| 242 | while (atomic_read(&data.finished) != ncpus) { |
| 243 | if (--timeout == 0) { |
| 244 | printk("smp_call_function on cpu %d: other cpus not finishing (%d/%d)\n", |
| 245 | smp_processor_id(), atomic_read(&data.finished), atomic_read(&data.started)); |
| 246 | goto out; |
| 247 | } |
| 248 | barrier(); |
| 249 | udelay(1); |
| 250 | } |
| 251 | } |
| 252 | ret = 0; |
| 253 | |
| 254 | out: |
| 255 | spin_unlock(&call_lock); |
| 256 | return ret; |
| 257 | } |
| 258 | |
| 259 | void smp_call_function_interrupt(void) |
| 260 | { |
| 261 | void (*func) (void *info) = call_data->func; |
| 262 | void *info = call_data->info; |
| 263 | int wait = call_data->wait; |
| 264 | |
| 265 | /* |
| 266 | * Notify initiating CPU that I've grabbed the data and am |
| 267 | * about to execute the function |
| 268 | */ |
| 269 | atomic_inc(&call_data->started); |
| 270 | /* |
| 271 | * At this point the info structure may be out of scope unless wait==1 |
| 272 | */ |
| 273 | (*func)(info); |
| 274 | if (wait) |
| 275 | atomic_inc(&call_data->finished); |
| 276 | } |
| 277 | |
| 278 | static void __devinit smp_store_cpu_info(int id) |
| 279 | { |
| 280 | struct cpuinfo_PPC *c = &cpu_data[id]; |
| 281 | |
| 282 | /* assume bogomips are same for everything */ |
| 283 | c->loops_per_jiffy = loops_per_jiffy; |
| 284 | c->pvr = mfspr(SPRN_PVR); |
| 285 | } |
| 286 | |
| 287 | void __init smp_prepare_cpus(unsigned int max_cpus) |
| 288 | { |
| 289 | int num_cpus, i; |
| 290 | |
| 291 | /* Fixup boot cpu */ |
| 292 | smp_store_cpu_info(smp_processor_id()); |
| 293 | cpu_callin_map[smp_processor_id()] = 1; |
| 294 | |
| 295 | smp_ops = ppc_md.smp_ops; |
| 296 | if (smp_ops == NULL) { |
| 297 | printk("SMP not supported on this machine.\n"); |
| 298 | return; |
| 299 | } |
| 300 | |
| 301 | /* Probe platform for CPUs: always linear. */ |
| 302 | num_cpus = smp_ops->probe(); |
| 303 | for (i = 0; i < num_cpus; ++i) |
| 304 | cpu_set(i, cpu_possible_map); |
| 305 | |
| 306 | /* Backup CPU 0 state */ |
| 307 | __save_cpu_setup(); |
| 308 | |
| 309 | if (smp_ops->space_timers) |
| 310 | smp_ops->space_timers(num_cpus); |
| 311 | } |
| 312 | |
| 313 | void __devinit smp_prepare_boot_cpu(void) |
| 314 | { |
| 315 | cpu_set(smp_processor_id(), cpu_online_map); |
| 316 | cpu_set(smp_processor_id(), cpu_possible_map); |
| 317 | } |
| 318 | |
| 319 | int __init setup_profiling_timer(unsigned int multiplier) |
| 320 | { |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | /* Processor coming up starts here */ |
| 325 | int __devinit start_secondary(void *unused) |
| 326 | { |
| 327 | int cpu; |
| 328 | |
| 329 | atomic_inc(&init_mm.mm_count); |
| 330 | current->active_mm = &init_mm; |
| 331 | |
| 332 | cpu = smp_processor_id(); |
| 333 | smp_store_cpu_info(cpu); |
| 334 | set_dec(tb_ticks_per_jiffy); |
| 335 | cpu_callin_map[cpu] = 1; |
| 336 | |
| 337 | printk("CPU %i done callin...\n", cpu); |
| 338 | smp_ops->setup_cpu(cpu); |
| 339 | printk("CPU %i done setup...\n", cpu); |
| 340 | local_irq_enable(); |
| 341 | smp_ops->take_timebase(); |
| 342 | printk("CPU %i done timebase take...\n", cpu); |
| 343 | |
| 344 | cpu_idle(); |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | int __cpu_up(unsigned int cpu) |
| 349 | { |
| 350 | struct task_struct *p; |
| 351 | char buf[32]; |
| 352 | int c; |
| 353 | |
| 354 | /* create a process for the processor */ |
| 355 | /* only regs.msr is actually used, and 0 is OK for it */ |
| 356 | p = fork_idle(cpu); |
| 357 | if (IS_ERR(p)) |
| 358 | panic("failed fork for CPU %u: %li", cpu, PTR_ERR(p)); |
| 359 | secondary_ti = p->thread_info; |
| 360 | p->thread_info->cpu = cpu; |
| 361 | |
| 362 | /* |
| 363 | * There was a cache flush loop here to flush the cache |
| 364 | * to memory for the first 8MB of RAM. The cache flush |
| 365 | * has been pushed into the kick_cpu function for those |
| 366 | * platforms that need it. |
| 367 | */ |
| 368 | |
| 369 | /* wake up cpu */ |
| 370 | smp_ops->kick_cpu(cpu); |
| 371 | |
| 372 | /* |
| 373 | * wait to see if the cpu made a callin (is actually up). |
| 374 | * use this value that I found through experimentation. |
| 375 | * -- Cort |
| 376 | */ |
| 377 | for (c = 1000; c && !cpu_callin_map[cpu]; c--) |
| 378 | udelay(100); |
| 379 | |
| 380 | if (!cpu_callin_map[cpu]) { |
| 381 | sprintf(buf, "didn't find cpu %u", cpu); |
| 382 | if (ppc_md.progress) ppc_md.progress(buf, 0x360+cpu); |
| 383 | printk("Processor %u is stuck.\n", cpu); |
| 384 | return -ENOENT; |
| 385 | } |
| 386 | |
| 387 | sprintf(buf, "found cpu %u", cpu); |
| 388 | if (ppc_md.progress) ppc_md.progress(buf, 0x350+cpu); |
| 389 | printk("Processor %d found.\n", cpu); |
| 390 | |
| 391 | smp_ops->give_timebase(); |
| 392 | cpu_set(cpu, cpu_online_map); |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | void smp_cpus_done(unsigned int max_cpus) |
| 397 | { |
| 398 | smp_ops->setup_cpu(0); |
| 399 | } |