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 void smp_mpic_message_pass(int target, int msg)
74 /* make sure we're sending something that translates to an IPI */
76 printk("SMP %d: smp_message_pass: unknown msg %d\n",
77 smp_processor_id(), msg);
83 mpic_send_ipi(msg, 0xffffffff);
85 case MSG_ALL_BUT_SELF:
86 mpic_send_ipi(msg, 0xffffffff & ~(1 << smp_processor_id()));
89 mpic_send_ipi(msg, 1 << target);
94 int __init smp_mpic_probe(void)
98 DBG("smp_mpic_probe()...\n");
100 nr_cpus = cpus_weight(cpu_possible_map);
102 DBG("nr_cpus: %d\n", nr_cpus);
110 void __devinit smp_mpic_setup_cpu(int cpu)
112 mpic_setup_this_cpu();
115 void __devinit smp_generic_kick_cpu(int nr)
117 BUG_ON(nr < 0 || nr >= NR_CPUS);
120 * The processor is currently spinning, waiting for the
121 * cpu_start field to become non-zero After we set cpu_start,
122 * the processor will continue on to secondary_start
124 paca[nr].cpu_start = 1;
128 #endif /* CONFIG_MPIC */
130 static void __init smp_space_timers(unsigned int max_cpus)
133 unsigned long offset = tb_ticks_per_jiffy / max_cpus;
134 unsigned long previous_tb = paca[boot_cpuid].next_jiffy_update_tb;
137 if (i != boot_cpuid) {
138 paca[i].next_jiffy_update_tb =
139 previous_tb + offset;
140 previous_tb = paca[i].next_jiffy_update_tb;
145 void smp_message_recv(int msg, struct pt_regs *regs)
148 case PPC_MSG_CALL_FUNCTION:
149 smp_call_function_interrupt();
151 case PPC_MSG_RESCHEDULE:
152 /* XXX Do we have to do this? */
156 case PPC_MSG_MIGRATE_TASK:
160 #ifdef CONFIG_DEBUGGER
161 case PPC_MSG_DEBUGGER_BREAK:
166 printk("SMP %d: smp_message_recv(): unknown msg %d\n",
167 smp_processor_id(), msg);
172 void smp_send_reschedule(int cpu)
174 smp_ops->message_pass(cpu, PPC_MSG_RESCHEDULE);
177 #ifdef CONFIG_DEBUGGER
178 void smp_send_debugger_break(int cpu)
180 smp_ops->message_pass(cpu, PPC_MSG_DEBUGGER_BREAK);
184 static void stop_this_cpu(void *dummy)
191 void smp_send_stop(void)
193 smp_call_function(stop_this_cpu, NULL, 1, 0);
197 * Structure and data for smp_call_function(). This is designed to minimise
198 * static memory requirements. It also looks cleaner.
199 * Stolen from the i386 version.
201 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(call_lock);
203 static struct call_data_struct {
204 void (*func) (void *info);
211 /* delay of at least 8 seconds on 1GHz cpu */
212 #define SMP_CALL_TIMEOUT (1UL << (30 + 3))
215 * This function sends a 'generic call function' IPI to all other CPUs
218 * [SUMMARY] Run a function on all other CPUs.
219 * <func> The function to run. This must be fast and non-blocking.
220 * <info> An arbitrary pointer to pass to the function.
221 * <nonatomic> currently unused.
222 * <wait> If true, wait (atomically) until function has completed on other CPUs.
223 * [RETURNS] 0 on success, else a negative status code. Does not return until
224 * remote CPUs are nearly ready to execute <<func>> or are or have executed.
226 * You must not call this function with disabled interrupts or from a
227 * hardware interrupt handler or from a bottom half handler.
229 int smp_call_function (void (*func) (void *info), void *info, int nonatomic,
232 struct call_data_struct data;
234 unsigned long timeout;
236 /* Can deadlock when called with interrupts disabled */
237 WARN_ON(irqs_disabled());
241 atomic_set(&data.started, 0);
244 atomic_set(&data.finished, 0);
246 spin_lock(&call_lock);
247 /* Must grab online cpu count with preempt disabled, otherwise
249 cpus = num_online_cpus() - 1;
257 /* Send a message to all other CPUs and wait for them to respond */
258 smp_ops->message_pass(MSG_ALL_BUT_SELF, PPC_MSG_CALL_FUNCTION);
260 /* Wait for response */
261 timeout = SMP_CALL_TIMEOUT;
262 while (atomic_read(&data.started) != cpus) {
264 if (--timeout == 0) {
265 printk("smp_call_function on cpu %d: other cpus not "
266 "responding (%d)\n", smp_processor_id(),
267 atomic_read(&data.started));
274 timeout = SMP_CALL_TIMEOUT;
275 while (atomic_read(&data.finished) != cpus) {
277 if (--timeout == 0) {
278 printk("smp_call_function on cpu %d: other "
279 "cpus not finishing (%d/%d)\n",
281 atomic_read(&data.finished),
282 atomic_read(&data.started));
294 spin_unlock(&call_lock);
298 EXPORT_SYMBOL(smp_call_function);
300 void smp_call_function_interrupt(void)
302 void (*func) (void *info);
306 /* call_data will be NULL if the sender timed out while
307 * waiting on us to receive the call.
312 func = call_data->func;
313 info = call_data->info;
314 wait = call_data->wait;
317 smp_mb__before_atomic_inc();
320 * Notify initiating CPU that I've grabbed the data and am
321 * about to execute the function
323 atomic_inc(&call_data->started);
325 * At this point the info structure may be out of scope unless wait==1
329 smp_mb__before_atomic_inc();
330 atomic_inc(&call_data->finished);
334 extern struct gettimeofday_struct do_gtod;
336 struct thread_info *current_set[NR_CPUS];
338 DECLARE_PER_CPU(unsigned int, pvr);
340 static void __devinit smp_store_cpu_info(int id)
342 per_cpu(pvr, id) = mfspr(SPRN_PVR);
345 static void __init smp_create_idle(unsigned int cpu)
347 struct task_struct *p;
349 /* create a process for the processor */
352 panic("failed fork for CPU %u: %li", cpu, PTR_ERR(p));
353 paca[cpu].__current = p;
354 current_set[cpu] = p->thread_info;
357 void __init smp_prepare_cpus(unsigned int max_cpus)
361 DBG("smp_prepare_cpus\n");
364 * setup_cpu may need to be called on the boot cpu. We havent
365 * spun any cpus up but lets be paranoid.
367 BUG_ON(boot_cpuid != smp_processor_id());
370 smp_store_cpu_info(boot_cpuid);
371 cpu_callin_map[boot_cpuid] = 1;
373 #ifndef CONFIG_PPC_ISERIES
374 paca[boot_cpuid].next_jiffy_update_tb = tb_last_stamp = get_tb();
377 * Should update do_gtod.stamp_xsec.
378 * For now we leave it which means the time can be some
379 * number of msecs off until someone does a settimeofday()
381 do_gtod.varp->tb_orig_stamp = tb_last_stamp;
382 systemcfg->tb_orig_stamp = tb_last_stamp;
385 max_cpus = smp_ops->probe();
387 smp_space_timers(max_cpus);
390 if (cpu != boot_cpuid)
391 smp_create_idle(cpu);
394 void __devinit smp_prepare_boot_cpu(void)
396 BUG_ON(smp_processor_id() != boot_cpuid);
398 cpu_set(boot_cpuid, cpu_online_map);
400 paca[boot_cpuid].__current = current;
401 current_set[boot_cpuid] = current->thread_info;
404 #ifdef CONFIG_HOTPLUG_CPU
405 /* State of each CPU during hotplug phases */
406 DEFINE_PER_CPU(int, cpu_state) = { 0 };
408 int generic_cpu_disable(void)
410 unsigned int cpu = smp_processor_id();
412 if (cpu == boot_cpuid)
415 systemcfg->processorCount--;
416 cpu_clear(cpu, cpu_online_map);
417 fixup_irqs(cpu_online_map);
421 int generic_cpu_enable(unsigned int cpu)
423 /* Do the normal bootup if we haven't
424 * already bootstrapped. */
425 if (system_state != SYSTEM_RUNNING)
428 /* get the target out of it's holding state */
429 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
432 while (!cpu_online(cpu))
435 fixup_irqs(cpu_online_map);
436 /* counter the irq disable in fixup_irqs */
441 void generic_cpu_die(unsigned int cpu)
445 for (i = 0; i < 100; i++) {
447 if (per_cpu(cpu_state, cpu) == CPU_DEAD)
451 printk(KERN_ERR "CPU%d didn't die...\n", cpu);
454 void generic_mach_cpu_die(void)
459 cpu = smp_processor_id();
460 printk(KERN_DEBUG "CPU%d offline\n", cpu);
461 __get_cpu_var(cpu_state) = CPU_DEAD;
463 while (__get_cpu_var(cpu_state) != CPU_UP_PREPARE)
467 cpu_set(cpu, cpu_online_map);
472 static int __devinit cpu_enable(unsigned int cpu)
474 if (smp_ops->cpu_enable)
475 return smp_ops->cpu_enable(cpu);
480 int __devinit __cpu_up(unsigned int cpu)
484 if (!cpu_enable(cpu))
487 if (smp_ops->cpu_bootable && !smp_ops->cpu_bootable(cpu))
490 paca[cpu].default_decr = tb_ticks_per_jiffy;
492 /* Make sure callin-map entry is 0 (can be leftover a CPU
495 cpu_callin_map[cpu] = 0;
497 /* The information for processor bringup must
498 * be written out to main store before we release
504 DBG("smp: kicking cpu %d\n", cpu);
505 smp_ops->kick_cpu(cpu);
508 * wait to see if the cpu made a callin (is actually up).
509 * use this value that I found through experimentation.
512 if (system_state < SYSTEM_RUNNING)
513 for (c = 5000; c && !cpu_callin_map[cpu]; c--)
515 #ifdef CONFIG_HOTPLUG_CPU
518 * CPUs can take much longer to come up in the
519 * hotplug case. Wait five seconds.
521 for (c = 25; c && !cpu_callin_map[cpu]; c--) {
526 if (!cpu_callin_map[cpu]) {
527 printk("Processor %u is stuck.\n", cpu);
531 printk("Processor %u found.\n", cpu);
533 if (smp_ops->give_timebase)
534 smp_ops->give_timebase();
536 /* Wait until cpu puts itself in the online map */
537 while (!cpu_online(cpu))
544 /* Activate a secondary processor. */
545 int __devinit start_secondary(void *unused)
547 unsigned int cpu = smp_processor_id();
549 atomic_inc(&init_mm.mm_count);
550 current->active_mm = &init_mm;
552 smp_store_cpu_info(cpu);
553 set_dec(paca[cpu].default_decr);
554 cpu_callin_map[cpu] = 1;
556 smp_ops->setup_cpu(cpu);
557 if (smp_ops->take_timebase)
558 smp_ops->take_timebase();
560 spin_lock(&call_lock);
561 cpu_set(cpu, cpu_online_map);
562 spin_unlock(&call_lock);
570 int setup_profiling_timer(unsigned int multiplier)
575 void __init smp_cpus_done(unsigned int max_cpus)
579 /* We want the setup_cpu() here to be called from CPU 0, but our
580 * init thread may have been "borrowed" by another CPU in the meantime
581 * se we pin us down to CPU 0 for a short while
583 old_mask = current->cpus_allowed;
584 set_cpus_allowed(current, cpumask_of_cpu(boot_cpuid));
586 smp_ops->setup_cpu(boot_cpuid);
588 set_cpus_allowed(current, old_mask);
591 #ifdef CONFIG_HOTPLUG_CPU
592 int __cpu_disable(void)
594 if (smp_ops->cpu_disable)
595 return smp_ops->cpu_disable();
600 void __cpu_die(unsigned int cpu)
602 if (smp_ops->cpu_die)
603 smp_ops->cpu_die(cpu);