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>
51 #define DBG(fmt...) udbg_printf(fmt)
56 cpumask_t cpu_possible_map = CPU_MASK_NONE;
57 cpumask_t cpu_online_map = CPU_MASK_NONE;
58 cpumask_t cpu_sibling_map[NR_CPUS] = { [0 ... NR_CPUS-1] = CPU_MASK_NONE };
60 EXPORT_SYMBOL(cpu_online_map);
61 EXPORT_SYMBOL(cpu_possible_map);
63 struct smp_ops_t *smp_ops;
65 static volatile unsigned int cpu_callin_map[NR_CPUS];
67 void smp_call_function_interrupt(void);
69 int smt_enabled_at_boot = 1;
72 int __init smp_mpic_probe(void)
76 DBG("smp_mpic_probe()...\n");
78 nr_cpus = cpus_weight(cpu_possible_map);
80 DBG("nr_cpus: %d\n", nr_cpus);
88 void __devinit smp_mpic_setup_cpu(int cpu)
90 mpic_setup_this_cpu();
93 void __devinit smp_generic_kick_cpu(int nr)
95 BUG_ON(nr < 0 || nr >= NR_CPUS);
98 * The processor is currently spinning, waiting for the
99 * cpu_start field to become non-zero After we set cpu_start,
100 * the processor will continue on to secondary_start
102 paca[nr].cpu_start = 1;
106 #endif /* CONFIG_MPIC */
108 static void __init smp_space_timers(unsigned int max_cpus)
111 unsigned long offset = tb_ticks_per_jiffy / max_cpus;
112 unsigned long previous_tb = paca[boot_cpuid].next_jiffy_update_tb;
115 if (i != boot_cpuid) {
116 paca[i].next_jiffy_update_tb =
117 previous_tb + offset;
118 previous_tb = paca[i].next_jiffy_update_tb;
123 void smp_message_recv(int msg, struct pt_regs *regs)
126 case PPC_MSG_CALL_FUNCTION:
127 smp_call_function_interrupt();
129 case PPC_MSG_RESCHEDULE:
130 /* XXX Do we have to do this? */
134 case PPC_MSG_MIGRATE_TASK:
138 #ifdef CONFIG_DEBUGGER
139 case PPC_MSG_DEBUGGER_BREAK:
144 printk("SMP %d: smp_message_recv(): unknown msg %d\n",
145 smp_processor_id(), msg);
150 void smp_send_reschedule(int cpu)
152 smp_ops->message_pass(cpu, PPC_MSG_RESCHEDULE);
155 #ifdef CONFIG_DEBUGGER
156 void smp_send_debugger_break(int cpu)
158 smp_ops->message_pass(cpu, PPC_MSG_DEBUGGER_BREAK);
162 static void stop_this_cpu(void *dummy)
169 void smp_send_stop(void)
171 smp_call_function(stop_this_cpu, NULL, 1, 0);
175 * Structure and data for smp_call_function(). This is designed to minimise
176 * static memory requirements. It also looks cleaner.
177 * Stolen from the i386 version.
179 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(call_lock);
181 static struct call_data_struct {
182 void (*func) (void *info);
189 /* delay of at least 8 seconds on 1GHz cpu */
190 #define SMP_CALL_TIMEOUT (1UL << (30 + 3))
193 * This function sends a 'generic call function' IPI to all other CPUs
196 * [SUMMARY] Run a function on all other CPUs.
197 * <func> The function to run. This must be fast and non-blocking.
198 * <info> An arbitrary pointer to pass to the function.
199 * <nonatomic> currently unused.
200 * <wait> If true, wait (atomically) until function has completed on other CPUs.
201 * [RETURNS] 0 on success, else a negative status code. Does not return until
202 * remote CPUs are nearly ready to execute <<func>> or are or have executed.
204 * You must not call this function with disabled interrupts or from a
205 * hardware interrupt handler or from a bottom half handler.
207 int smp_call_function (void (*func) (void *info), void *info, int nonatomic,
210 struct call_data_struct data;
212 unsigned long timeout;
214 /* Can deadlock when called with interrupts disabled */
215 WARN_ON(irqs_disabled());
219 atomic_set(&data.started, 0);
222 atomic_set(&data.finished, 0);
224 spin_lock(&call_lock);
225 /* Must grab online cpu count with preempt disabled, otherwise
227 cpus = num_online_cpus() - 1;
235 /* Send a message to all other CPUs and wait for them to respond */
236 smp_ops->message_pass(MSG_ALL_BUT_SELF, PPC_MSG_CALL_FUNCTION);
238 /* Wait for response */
239 timeout = SMP_CALL_TIMEOUT;
240 while (atomic_read(&data.started) != cpus) {
242 if (--timeout == 0) {
243 printk("smp_call_function on cpu %d: other cpus not "
244 "responding (%d)\n", smp_processor_id(),
245 atomic_read(&data.started));
252 timeout = SMP_CALL_TIMEOUT;
253 while (atomic_read(&data.finished) != cpus) {
255 if (--timeout == 0) {
256 printk("smp_call_function on cpu %d: other "
257 "cpus not finishing (%d/%d)\n",
259 atomic_read(&data.finished),
260 atomic_read(&data.started));
272 spin_unlock(&call_lock);
276 EXPORT_SYMBOL(smp_call_function);
278 void smp_call_function_interrupt(void)
280 void (*func) (void *info);
284 /* call_data will be NULL if the sender timed out while
285 * waiting on us to receive the call.
290 func = call_data->func;
291 info = call_data->info;
292 wait = call_data->wait;
295 smp_mb__before_atomic_inc();
298 * Notify initiating CPU that I've grabbed the data and am
299 * about to execute the function
301 atomic_inc(&call_data->started);
303 * At this point the info structure may be out of scope unless wait==1
307 smp_mb__before_atomic_inc();
308 atomic_inc(&call_data->finished);
312 extern struct gettimeofday_struct do_gtod;
314 struct thread_info *current_set[NR_CPUS];
316 DECLARE_PER_CPU(unsigned int, pvr);
318 static void __devinit smp_store_cpu_info(int id)
320 per_cpu(pvr, id) = mfspr(SPRN_PVR);
323 static void __init smp_create_idle(unsigned int cpu)
325 struct task_struct *p;
327 /* create a process for the processor */
330 panic("failed fork for CPU %u: %li", cpu, PTR_ERR(p));
331 paca[cpu].__current = p;
332 current_set[cpu] = p->thread_info;
335 void __init smp_prepare_cpus(unsigned int max_cpus)
339 DBG("smp_prepare_cpus\n");
342 * setup_cpu may need to be called on the boot cpu. We havent
343 * spun any cpus up but lets be paranoid.
345 BUG_ON(boot_cpuid != smp_processor_id());
348 smp_store_cpu_info(boot_cpuid);
349 cpu_callin_map[boot_cpuid] = 1;
351 #ifndef CONFIG_PPC_ISERIES
352 paca[boot_cpuid].next_jiffy_update_tb = tb_last_stamp = get_tb();
355 * Should update do_gtod.stamp_xsec.
356 * For now we leave it which means the time can be some
357 * number of msecs off until someone does a settimeofday()
359 do_gtod.varp->tb_orig_stamp = tb_last_stamp;
360 systemcfg->tb_orig_stamp = tb_last_stamp;
363 max_cpus = smp_ops->probe();
365 smp_space_timers(max_cpus);
368 if (cpu != boot_cpuid)
369 smp_create_idle(cpu);
372 void __devinit smp_prepare_boot_cpu(void)
374 BUG_ON(smp_processor_id() != boot_cpuid);
376 cpu_set(boot_cpuid, cpu_online_map);
378 paca[boot_cpuid].__current = current;
379 current_set[boot_cpuid] = current->thread_info;
382 #ifdef CONFIG_HOTPLUG_CPU
383 /* State of each CPU during hotplug phases */
384 DEFINE_PER_CPU(int, cpu_state) = { 0 };
386 int generic_cpu_disable(void)
388 unsigned int cpu = smp_processor_id();
390 if (cpu == boot_cpuid)
393 systemcfg->processorCount--;
394 cpu_clear(cpu, cpu_online_map);
395 fixup_irqs(cpu_online_map);
399 int generic_cpu_enable(unsigned int cpu)
401 /* Do the normal bootup if we haven't
402 * already bootstrapped. */
403 if (system_state != SYSTEM_RUNNING)
406 /* get the target out of it's holding state */
407 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
410 while (!cpu_online(cpu))
413 fixup_irqs(cpu_online_map);
414 /* counter the irq disable in fixup_irqs */
419 void generic_cpu_die(unsigned int cpu)
423 for (i = 0; i < 100; i++) {
425 if (per_cpu(cpu_state, cpu) == CPU_DEAD)
429 printk(KERN_ERR "CPU%d didn't die...\n", cpu);
432 void generic_mach_cpu_die(void)
437 cpu = smp_processor_id();
438 printk(KERN_DEBUG "CPU%d offline\n", cpu);
439 __get_cpu_var(cpu_state) = CPU_DEAD;
441 while (__get_cpu_var(cpu_state) != CPU_UP_PREPARE)
445 cpu_set(cpu, cpu_online_map);
450 static int __devinit cpu_enable(unsigned int cpu)
452 if (smp_ops->cpu_enable)
453 return smp_ops->cpu_enable(cpu);
458 int __devinit __cpu_up(unsigned int cpu)
462 if (!cpu_enable(cpu))
465 if (smp_ops->cpu_bootable && !smp_ops->cpu_bootable(cpu))
468 paca[cpu].default_decr = tb_ticks_per_jiffy;
470 /* Make sure callin-map entry is 0 (can be leftover a CPU
473 cpu_callin_map[cpu] = 0;
475 /* The information for processor bringup must
476 * be written out to main store before we release
482 DBG("smp: kicking cpu %d\n", cpu);
483 smp_ops->kick_cpu(cpu);
486 * wait to see if the cpu made a callin (is actually up).
487 * use this value that I found through experimentation.
490 if (system_state < SYSTEM_RUNNING)
491 for (c = 5000; c && !cpu_callin_map[cpu]; c--)
493 #ifdef CONFIG_HOTPLUG_CPU
496 * CPUs can take much longer to come up in the
497 * hotplug case. Wait five seconds.
499 for (c = 25; c && !cpu_callin_map[cpu]; c--) {
504 if (!cpu_callin_map[cpu]) {
505 printk("Processor %u is stuck.\n", cpu);
509 printk("Processor %u found.\n", cpu);
511 if (smp_ops->give_timebase)
512 smp_ops->give_timebase();
514 /* Wait until cpu puts itself in the online map */
515 while (!cpu_online(cpu))
522 /* Activate a secondary processor. */
523 int __devinit start_secondary(void *unused)
525 unsigned int cpu = smp_processor_id();
527 atomic_inc(&init_mm.mm_count);
528 current->active_mm = &init_mm;
530 smp_store_cpu_info(cpu);
531 set_dec(paca[cpu].default_decr);
532 cpu_callin_map[cpu] = 1;
534 smp_ops->setup_cpu(cpu);
535 if (smp_ops->take_timebase)
536 smp_ops->take_timebase();
538 spin_lock(&call_lock);
539 cpu_set(cpu, cpu_online_map);
540 spin_unlock(&call_lock);
548 int setup_profiling_timer(unsigned int multiplier)
553 void __init smp_cpus_done(unsigned int max_cpus)
557 /* We want the setup_cpu() here to be called from CPU 0, but our
558 * init thread may have been "borrowed" by another CPU in the meantime
559 * se we pin us down to CPU 0 for a short while
561 old_mask = current->cpus_allowed;
562 set_cpus_allowed(current, cpumask_of_cpu(boot_cpuid));
564 smp_ops->setup_cpu(boot_cpuid);
566 set_cpus_allowed(current, old_mask);
569 #ifdef CONFIG_HOTPLUG_CPU
570 int __cpu_disable(void)
572 if (smp_ops->cpu_disable)
573 return smp_ops->cpu_disable();
578 void __cpu_die(unsigned int cpu)
580 if (smp_ops->cpu_die)
581 smp_ops->cpu_die(cpu);