[PATCH] USB: ftdi_sio: new microHAM and Evolution Robotics devices
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / ppc64 / kernel / smp.c
blob793b562da653699e19ce2c0e31df9d918d26feed
1 /*
2 * SMP support for ppc.
4 * Written by Cort Dougan (cort@cs.nmt.edu) borrowing a great
5 * deal of code from the sparc and intel versions.
7 * Copyright (C) 1999 Cort Dougan <cort@cs.nmt.edu>
9 * PowerPC-64 Support added by Dave Engebretsen, Peter Bergner, and
10 * Mike Corrigan {engebret|bergner|mikec}@us.ibm.com
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
18 #undef DEBUG
20 #include <linux/config.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/sched.h>
24 #include <linux/smp.h>
25 #include <linux/interrupt.h>
26 #include <linux/delay.h>
27 #include <linux/init.h>
28 #include <linux/spinlock.h>
29 #include <linux/cache.h>
30 #include <linux/err.h>
31 #include <linux/sysdev.h>
32 #include <linux/cpu.h>
33 #include <linux/notifier.h>
35 #include <asm/ptrace.h>
36 #include <asm/atomic.h>
37 #include <asm/irq.h>
38 #include <asm/page.h>
39 #include <asm/pgtable.h>
40 #include <asm/prom.h>
41 #include <asm/smp.h>
42 #include <asm/paca.h>
43 #include <asm/time.h>
44 #include <asm/machdep.h>
45 #include <asm/cputable.h>
46 #include <asm/system.h>
47 #include <asm/abs_addr.h>
49 #include "mpic.h"
51 #ifdef DEBUG
52 #define DBG(fmt...) udbg_printf(fmt)
53 #else
54 #define DBG(fmt...)
55 #endif
57 cpumask_t cpu_possible_map = CPU_MASK_NONE;
58 cpumask_t cpu_online_map = CPU_MASK_NONE;
59 cpumask_t cpu_sibling_map[NR_CPUS] = { [0 ... NR_CPUS-1] = CPU_MASK_NONE };
61 EXPORT_SYMBOL(cpu_online_map);
62 EXPORT_SYMBOL(cpu_possible_map);
64 struct smp_ops_t *smp_ops;
66 static volatile unsigned int cpu_callin_map[NR_CPUS];
68 void smp_call_function_interrupt(void);
70 int smt_enabled_at_boot = 1;
72 #ifdef CONFIG_MPIC
73 void smp_mpic_message_pass(int target, int msg)
75 /* make sure we're sending something that translates to an IPI */
76 if ( msg > 0x3 ){
77 printk("SMP %d: smp_message_pass: unknown msg %d\n",
78 smp_processor_id(), msg);
79 return;
81 switch ( target )
83 case MSG_ALL:
84 mpic_send_ipi(msg, 0xffffffff);
85 break;
86 case MSG_ALL_BUT_SELF:
87 mpic_send_ipi(msg, 0xffffffff & ~(1 << smp_processor_id()));
88 break;
89 default:
90 mpic_send_ipi(msg, 1 << target);
91 break;
95 int __init smp_mpic_probe(void)
97 int nr_cpus;
99 DBG("smp_mpic_probe()...\n");
101 nr_cpus = cpus_weight(cpu_possible_map);
103 DBG("nr_cpus: %d\n", nr_cpus);
105 if (nr_cpus > 1)
106 mpic_request_ipis();
108 return nr_cpus;
111 void __devinit smp_mpic_setup_cpu(int cpu)
113 mpic_setup_this_cpu();
116 void __devinit smp_generic_kick_cpu(int nr)
118 BUG_ON(nr < 0 || nr >= NR_CPUS);
121 * The processor is currently spinning, waiting for the
122 * cpu_start field to become non-zero After we set cpu_start,
123 * the processor will continue on to secondary_start
125 paca[nr].cpu_start = 1;
126 smp_mb();
129 #endif /* CONFIG_MPIC */
131 static void __init smp_space_timers(unsigned int max_cpus)
133 int i;
134 unsigned long offset = tb_ticks_per_jiffy / max_cpus;
135 unsigned long previous_tb = paca[boot_cpuid].next_jiffy_update_tb;
137 for_each_cpu(i) {
138 if (i != boot_cpuid) {
139 paca[i].next_jiffy_update_tb =
140 previous_tb + offset;
141 previous_tb = paca[i].next_jiffy_update_tb;
146 void smp_message_recv(int msg, struct pt_regs *regs)
148 switch(msg) {
149 case PPC_MSG_CALL_FUNCTION:
150 smp_call_function_interrupt();
151 break;
152 case PPC_MSG_RESCHEDULE:
153 /* XXX Do we have to do this? */
154 set_need_resched();
155 break;
156 #if 0
157 case PPC_MSG_MIGRATE_TASK:
158 /* spare */
159 break;
160 #endif
161 #ifdef CONFIG_DEBUGGER
162 case PPC_MSG_DEBUGGER_BREAK:
163 debugger_ipi(regs);
164 break;
165 #endif
166 default:
167 printk("SMP %d: smp_message_recv(): unknown msg %d\n",
168 smp_processor_id(), msg);
169 break;
173 void smp_send_reschedule(int cpu)
175 smp_ops->message_pass(cpu, PPC_MSG_RESCHEDULE);
178 #ifdef CONFIG_DEBUGGER
179 void smp_send_debugger_break(int cpu)
181 smp_ops->message_pass(cpu, PPC_MSG_DEBUGGER_BREAK);
183 #endif
185 static void stop_this_cpu(void *dummy)
187 local_irq_disable();
188 while (1)
192 void smp_send_stop(void)
194 smp_call_function(stop_this_cpu, NULL, 1, 0);
198 * Structure and data for smp_call_function(). This is designed to minimise
199 * static memory requirements. It also looks cleaner.
200 * Stolen from the i386 version.
202 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(call_lock);
204 static struct call_data_struct {
205 void (*func) (void *info);
206 void *info;
207 atomic_t started;
208 atomic_t finished;
209 int wait;
210 } *call_data;
212 /* delay of at least 8 seconds on 1GHz cpu */
213 #define SMP_CALL_TIMEOUT (1UL << (30 + 3))
216 * This function sends a 'generic call function' IPI to all other CPUs
217 * in the system.
219 * [SUMMARY] Run a function on all other CPUs.
220 * <func> The function to run. This must be fast and non-blocking.
221 * <info> An arbitrary pointer to pass to the function.
222 * <nonatomic> currently unused.
223 * <wait> If true, wait (atomically) until function has completed on other CPUs.
224 * [RETURNS] 0 on success, else a negative status code. Does not return until
225 * remote CPUs are nearly ready to execute <<func>> or are or have executed.
227 * You must not call this function with disabled interrupts or from a
228 * hardware interrupt handler or from a bottom half handler.
230 int smp_call_function (void (*func) (void *info), void *info, int nonatomic,
231 int wait)
233 struct call_data_struct data;
234 int ret = -1, cpus;
235 unsigned long timeout;
237 /* Can deadlock when called with interrupts disabled */
238 WARN_ON(irqs_disabled());
240 data.func = func;
241 data.info = info;
242 atomic_set(&data.started, 0);
243 data.wait = wait;
244 if (wait)
245 atomic_set(&data.finished, 0);
247 spin_lock(&call_lock);
248 /* Must grab online cpu count with preempt disabled, otherwise
249 * it can change. */
250 cpus = num_online_cpus() - 1;
251 if (!cpus) {
252 ret = 0;
253 goto out;
256 call_data = &data;
257 smp_wmb();
258 /* Send a message to all other CPUs and wait for them to respond */
259 smp_ops->message_pass(MSG_ALL_BUT_SELF, PPC_MSG_CALL_FUNCTION);
261 /* Wait for response */
262 timeout = SMP_CALL_TIMEOUT;
263 while (atomic_read(&data.started) != cpus) {
264 HMT_low();
265 if (--timeout == 0) {
266 printk("smp_call_function on cpu %d: other cpus not "
267 "responding (%d)\n", smp_processor_id(),
268 atomic_read(&data.started));
269 debugger(NULL);
270 goto out;
274 if (wait) {
275 timeout = SMP_CALL_TIMEOUT;
276 while (atomic_read(&data.finished) != cpus) {
277 HMT_low();
278 if (--timeout == 0) {
279 printk("smp_call_function on cpu %d: other "
280 "cpus not finishing (%d/%d)\n",
281 smp_processor_id(),
282 atomic_read(&data.finished),
283 atomic_read(&data.started));
284 debugger(NULL);
285 goto out;
290 ret = 0;
292 out:
293 call_data = NULL;
294 HMT_medium();
295 spin_unlock(&call_lock);
296 return ret;
299 EXPORT_SYMBOL(smp_call_function);
301 void smp_call_function_interrupt(void)
303 void (*func) (void *info);
304 void *info;
305 int wait;
307 /* call_data will be NULL if the sender timed out while
308 * waiting on us to receive the call.
310 if (!call_data)
311 return;
313 func = call_data->func;
314 info = call_data->info;
315 wait = call_data->wait;
317 if (!wait)
318 smp_mb__before_atomic_inc();
321 * Notify initiating CPU that I've grabbed the data and am
322 * about to execute the function
324 atomic_inc(&call_data->started);
326 * At this point the info structure may be out of scope unless wait==1
328 (*func)(info);
329 if (wait) {
330 smp_mb__before_atomic_inc();
331 atomic_inc(&call_data->finished);
335 extern struct gettimeofday_struct do_gtod;
337 struct thread_info *current_set[NR_CPUS];
339 DECLARE_PER_CPU(unsigned int, pvr);
341 static void __devinit smp_store_cpu_info(int id)
343 per_cpu(pvr, id) = mfspr(SPRN_PVR);
346 static void __init smp_create_idle(unsigned int cpu)
348 struct task_struct *p;
350 /* create a process for the processor */
351 p = fork_idle(cpu);
352 if (IS_ERR(p))
353 panic("failed fork for CPU %u: %li", cpu, PTR_ERR(p));
354 paca[cpu].__current = p;
355 current_set[cpu] = p->thread_info;
358 void __init smp_prepare_cpus(unsigned int max_cpus)
360 unsigned int cpu;
362 DBG("smp_prepare_cpus\n");
365 * setup_cpu may need to be called on the boot cpu. We havent
366 * spun any cpus up but lets be paranoid.
368 BUG_ON(boot_cpuid != smp_processor_id());
370 /* Fixup boot cpu */
371 smp_store_cpu_info(boot_cpuid);
372 cpu_callin_map[boot_cpuid] = 1;
374 #ifndef CONFIG_PPC_ISERIES
375 paca[boot_cpuid].next_jiffy_update_tb = tb_last_stamp = get_tb();
378 * Should update do_gtod.stamp_xsec.
379 * For now we leave it which means the time can be some
380 * number of msecs off until someone does a settimeofday()
382 do_gtod.varp->tb_orig_stamp = tb_last_stamp;
383 systemcfg->tb_orig_stamp = tb_last_stamp;
384 #endif
386 max_cpus = smp_ops->probe();
388 smp_space_timers(max_cpus);
390 for_each_cpu(cpu)
391 if (cpu != boot_cpuid)
392 smp_create_idle(cpu);
395 void __devinit smp_prepare_boot_cpu(void)
397 BUG_ON(smp_processor_id() != boot_cpuid);
399 cpu_set(boot_cpuid, cpu_online_map);
401 paca[boot_cpuid].__current = current;
402 current_set[boot_cpuid] = current->thread_info;
405 #ifdef CONFIG_HOTPLUG_CPU
406 /* State of each CPU during hotplug phases */
407 DEFINE_PER_CPU(int, cpu_state) = { 0 };
409 int generic_cpu_disable(void)
411 unsigned int cpu = smp_processor_id();
413 if (cpu == boot_cpuid)
414 return -EBUSY;
416 systemcfg->processorCount--;
417 cpu_clear(cpu, cpu_online_map);
418 fixup_irqs(cpu_online_map);
419 return 0;
422 int generic_cpu_enable(unsigned int cpu)
424 /* Do the normal bootup if we haven't
425 * already bootstrapped. */
426 if (system_state != SYSTEM_RUNNING)
427 return -ENOSYS;
429 /* get the target out of it's holding state */
430 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
431 smp_wmb();
433 while (!cpu_online(cpu))
434 cpu_relax();
436 fixup_irqs(cpu_online_map);
437 /* counter the irq disable in fixup_irqs */
438 local_irq_enable();
439 return 0;
442 void generic_cpu_die(unsigned int cpu)
444 int i;
446 for (i = 0; i < 100; i++) {
447 smp_rmb();
448 if (per_cpu(cpu_state, cpu) == CPU_DEAD)
449 return;
450 msleep(100);
452 printk(KERN_ERR "CPU%d didn't die...\n", cpu);
455 void generic_mach_cpu_die(void)
457 unsigned int cpu;
459 local_irq_disable();
460 cpu = smp_processor_id();
461 printk(KERN_DEBUG "CPU%d offline\n", cpu);
462 __get_cpu_var(cpu_state) = CPU_DEAD;
463 smp_wmb();
464 while (__get_cpu_var(cpu_state) != CPU_UP_PREPARE)
465 cpu_relax();
467 flush_tlb_pending();
468 cpu_set(cpu, cpu_online_map);
469 local_irq_enable();
471 #endif
473 static int __devinit cpu_enable(unsigned int cpu)
475 if (smp_ops->cpu_enable)
476 return smp_ops->cpu_enable(cpu);
478 return -ENOSYS;
481 int __devinit __cpu_up(unsigned int cpu)
483 int c;
485 if (!cpu_enable(cpu))
486 return 0;
488 if (smp_ops->cpu_bootable && !smp_ops->cpu_bootable(cpu))
489 return -EINVAL;
491 paca[cpu].default_decr = tb_ticks_per_jiffy;
493 /* Make sure callin-map entry is 0 (can be leftover a CPU
494 * hotplug
496 cpu_callin_map[cpu] = 0;
498 /* The information for processor bringup must
499 * be written out to main store before we release
500 * the processor.
502 smp_mb();
504 /* wake up cpus */
505 DBG("smp: kicking cpu %d\n", cpu);
506 smp_ops->kick_cpu(cpu);
509 * wait to see if the cpu made a callin (is actually up).
510 * use this value that I found through experimentation.
511 * -- Cort
513 if (system_state < SYSTEM_RUNNING)
514 for (c = 5000; c && !cpu_callin_map[cpu]; c--)
515 udelay(100);
516 #ifdef CONFIG_HOTPLUG_CPU
517 else
519 * CPUs can take much longer to come up in the
520 * hotplug case. Wait five seconds.
522 for (c = 25; c && !cpu_callin_map[cpu]; c--) {
523 msleep(200);
525 #endif
527 if (!cpu_callin_map[cpu]) {
528 printk("Processor %u is stuck.\n", cpu);
529 return -ENOENT;
532 printk("Processor %u found.\n", cpu);
534 if (smp_ops->give_timebase)
535 smp_ops->give_timebase();
537 /* Wait until cpu puts itself in the online map */
538 while (!cpu_online(cpu))
539 cpu_relax();
541 return 0;
545 /* Activate a secondary processor. */
546 int __devinit start_secondary(void *unused)
548 unsigned int cpu = smp_processor_id();
550 atomic_inc(&init_mm.mm_count);
551 current->active_mm = &init_mm;
553 smp_store_cpu_info(cpu);
554 set_dec(paca[cpu].default_decr);
555 cpu_callin_map[cpu] = 1;
557 smp_ops->setup_cpu(cpu);
558 if (smp_ops->take_timebase)
559 smp_ops->take_timebase();
561 spin_lock(&call_lock);
562 cpu_set(cpu, cpu_online_map);
563 spin_unlock(&call_lock);
565 local_irq_enable();
567 cpu_idle();
568 return 0;
571 int setup_profiling_timer(unsigned int multiplier)
573 return 0;
576 void __init smp_cpus_done(unsigned int max_cpus)
578 cpumask_t old_mask;
580 /* We want the setup_cpu() here to be called from CPU 0, but our
581 * init thread may have been "borrowed" by another CPU in the meantime
582 * se we pin us down to CPU 0 for a short while
584 old_mask = current->cpus_allowed;
585 set_cpus_allowed(current, cpumask_of_cpu(boot_cpuid));
587 smp_ops->setup_cpu(boot_cpuid);
589 set_cpus_allowed(current, old_mask);
592 #ifdef CONFIG_HOTPLUG_CPU
593 int __cpu_disable(void)
595 if (smp_ops->cpu_disable)
596 return smp_ops->cpu_disable();
598 return -ENOSYS;
601 void __cpu_die(unsigned int cpu)
603 if (smp_ops->cpu_die)
604 smp_ops->cpu_die(cpu);
606 #endif