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.
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>
39 #include <asm/pgtable.h>
44 #include <asm/machdep.h>
45 #include <asm/cputable.h>
46 #include <asm/system.h>
47 #include <asm/abs_addr.h>
52 #define DBG(fmt...) udbg_printf(fmt)
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;
73 void smp_mpic_message_pass(int target
, int msg
)
75 /* make sure we're sending something that translates to an IPI */
77 printk("SMP %d: smp_message_pass: unknown msg %d\n",
78 smp_processor_id(), msg
);
84 mpic_send_ipi(msg
, 0xffffffff);
86 case MSG_ALL_BUT_SELF
:
87 mpic_send_ipi(msg
, 0xffffffff & ~(1 << smp_processor_id()));
90 mpic_send_ipi(msg
, 1 << target
);
95 int __init
smp_mpic_probe(void)
99 DBG("smp_mpic_probe()...\n");
101 nr_cpus
= cpus_weight(cpu_possible_map
);
103 DBG("nr_cpus: %d\n", 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;
129 #endif /* CONFIG_MPIC */
131 static void __init
smp_space_timers(unsigned int max_cpus
)
134 unsigned long offset
= tb_ticks_per_jiffy
/ max_cpus
;
135 unsigned long previous_tb
= paca
[boot_cpuid
].next_jiffy_update_tb
;
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
)
149 case PPC_MSG_CALL_FUNCTION
:
150 smp_call_function_interrupt();
152 case PPC_MSG_RESCHEDULE
:
153 /* XXX Do we have to do this? */
157 case PPC_MSG_MIGRATE_TASK
:
161 #ifdef CONFIG_DEBUGGER
162 case PPC_MSG_DEBUGGER_BREAK
:
167 printk("SMP %d: smp_message_recv(): unknown msg %d\n",
168 smp_processor_id(), msg
);
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
);
185 static void stop_this_cpu(void *dummy
)
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
);
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
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
,
233 struct call_data_struct data
;
235 unsigned long timeout
;
237 /* Can deadlock when called with interrupts disabled */
238 WARN_ON(irqs_disabled());
242 atomic_set(&data
.started
, 0);
245 atomic_set(&data
.finished
, 0);
247 spin_lock(&call_lock
);
248 /* Must grab online cpu count with preempt disabled, otherwise
250 cpus
= num_online_cpus() - 1;
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
) {
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
));
275 timeout
= SMP_CALL_TIMEOUT
;
276 while (atomic_read(&data
.finished
) != cpus
) {
278 if (--timeout
== 0) {
279 printk("smp_call_function on cpu %d: other "
280 "cpus not finishing (%d/%d)\n",
282 atomic_read(&data
.finished
),
283 atomic_read(&data
.started
));
295 spin_unlock(&call_lock
);
299 EXPORT_SYMBOL(smp_call_function
);
301 void smp_call_function_interrupt(void)
303 void (*func
) (void *info
);
307 /* call_data will be NULL if the sender timed out while
308 * waiting on us to receive the call.
313 func
= call_data
->func
;
314 info
= call_data
->info
;
315 wait
= call_data
->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
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 */
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
)
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());
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
;
386 max_cpus
= smp_ops
->probe();
388 smp_space_timers(max_cpus
);
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
)
416 systemcfg
->processorCount
--;
417 cpu_clear(cpu
, cpu_online_map
);
418 fixup_irqs(cpu_online_map
);
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
)
429 /* get the target out of it's holding state */
430 per_cpu(cpu_state
, cpu
) = CPU_UP_PREPARE
;
433 while (!cpu_online(cpu
))
436 fixup_irqs(cpu_online_map
);
437 /* counter the irq disable in fixup_irqs */
442 void generic_cpu_die(unsigned int cpu
)
446 for (i
= 0; i
< 100; i
++) {
448 if (per_cpu(cpu_state
, cpu
) == CPU_DEAD
)
452 printk(KERN_ERR
"CPU%d didn't die...\n", cpu
);
455 void generic_mach_cpu_die(void)
460 cpu
= smp_processor_id();
461 printk(KERN_DEBUG
"CPU%d offline\n", cpu
);
462 __get_cpu_var(cpu_state
) = CPU_DEAD
;
464 while (__get_cpu_var(cpu_state
) != CPU_UP_PREPARE
)
468 cpu_set(cpu
, cpu_online_map
);
473 static int __devinit
cpu_enable(unsigned int cpu
)
475 if (smp_ops
->cpu_enable
)
476 return smp_ops
->cpu_enable(cpu
);
481 int __devinit
__cpu_up(unsigned int cpu
)
485 if (!cpu_enable(cpu
))
488 if (smp_ops
->cpu_bootable
&& !smp_ops
->cpu_bootable(cpu
))
491 paca
[cpu
].default_decr
= tb_ticks_per_jiffy
;
493 /* Make sure callin-map entry is 0 (can be leftover a CPU
496 cpu_callin_map
[cpu
] = 0;
498 /* The information for processor bringup must
499 * be written out to main store before we release
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.
513 if (system_state
< SYSTEM_RUNNING
)
514 for (c
= 5000; c
&& !cpu_callin_map
[cpu
]; c
--)
516 #ifdef CONFIG_HOTPLUG_CPU
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
--) {
527 if (!cpu_callin_map
[cpu
]) {
528 printk("Processor %u is stuck.\n", cpu
);
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
))
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
);
571 int setup_profiling_timer(unsigned int multiplier
)
576 void __init
smp_cpus_done(unsigned int max_cpus
)
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();
601 void __cpu_die(unsigned int cpu
)
603 if (smp_ops
->cpu_die
)
604 smp_ops
->cpu_die(cpu
);