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/kernel.h>
21 #include <linux/module.h>
22 #include <linux/sched.h>
23 #include <linux/smp.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26 #include <linux/init.h>
27 #include <linux/spinlock.h>
28 #include <linux/cache.h>
29 #include <linux/err.h>
30 #include <linux/sysdev.h>
31 #include <linux/cpu.h>
32 #include <linux/notifier.h>
33 #include <linux/topology.h>
35 #include <asm/ptrace.h>
36 #include <asm/atomic.h>
39 #include <asm/pgtable.h>
43 #include <asm/machdep.h>
44 #include <asm/cputhreads.h>
45 #include <asm/cputable.h>
46 #include <asm/system.h>
48 #include <asm/vdso_datapage.h>
55 #define DBG(fmt...) udbg_printf(fmt)
61 /* Store all idle threads, this can be reused instead of creating
62 * a new thread. Also avoids complicated thread destroy functionality
65 #ifdef CONFIG_HOTPLUG_CPU
67 * Needed only for CONFIG_HOTPLUG_CPU because __cpuinitdata is
68 * removed after init for !CONFIG_HOTPLUG_CPU.
70 static DEFINE_PER_CPU(struct task_struct
*, idle_thread_array
);
71 #define get_idle_for_cpu(x) (per_cpu(idle_thread_array, x))
72 #define set_idle_for_cpu(x, p) (per_cpu(idle_thread_array, x) = (p))
74 static struct task_struct
*idle_thread_array
[NR_CPUS
] __cpuinitdata
;
75 #define get_idle_for_cpu(x) (idle_thread_array[(x)])
76 #define set_idle_for_cpu(x, p) (idle_thread_array[(x)] = (p))
79 struct thread_info
*secondary_ti
;
81 DEFINE_PER_CPU(cpumask_var_t
, cpu_sibling_map
);
82 DEFINE_PER_CPU(cpumask_var_t
, cpu_core_map
);
84 EXPORT_PER_CPU_SYMBOL(cpu_sibling_map
);
85 EXPORT_PER_CPU_SYMBOL(cpu_core_map
);
87 /* SMP operations for this machine */
88 struct smp_ops_t
*smp_ops
;
90 /* Can't be static due to PowerMac hackery */
91 volatile unsigned int cpu_callin_map
[NR_CPUS
];
93 int smt_enabled_at_boot
= 1;
95 static void (*crash_ipi_function_ptr
)(struct pt_regs
*) = NULL
;
98 void __devinit
smp_generic_kick_cpu(int nr
)
100 BUG_ON(nr
< 0 || nr
>= NR_CPUS
);
103 * The processor is currently spinning, waiting for the
104 * cpu_start field to become non-zero After we set cpu_start,
105 * the processor will continue on to secondary_start
107 paca
[nr
].cpu_start
= 1;
112 void smp_message_recv(int msg
)
115 case PPC_MSG_CALL_FUNCTION
:
116 generic_smp_call_function_interrupt();
118 case PPC_MSG_RESCHEDULE
:
119 /* we notice need_resched on exit */
121 case PPC_MSG_CALL_FUNC_SINGLE
:
122 generic_smp_call_function_single_interrupt();
124 case PPC_MSG_DEBUGGER_BREAK
:
125 if (crash_ipi_function_ptr
) {
126 crash_ipi_function_ptr(get_irq_regs());
129 #ifdef CONFIG_DEBUGGER
130 debugger_ipi(get_irq_regs());
132 #endif /* CONFIG_DEBUGGER */
135 printk("SMP %d: smp_message_recv(): unknown msg %d\n",
136 smp_processor_id(), msg
);
141 static irqreturn_t
call_function_action(int irq
, void *data
)
143 generic_smp_call_function_interrupt();
147 static irqreturn_t
reschedule_action(int irq
, void *data
)
149 /* we just need the return path side effect of checking need_resched */
153 static irqreturn_t
call_function_single_action(int irq
, void *data
)
155 generic_smp_call_function_single_interrupt();
159 static irqreturn_t
debug_ipi_action(int irq
, void *data
)
161 smp_message_recv(PPC_MSG_DEBUGGER_BREAK
);
165 static irq_handler_t smp_ipi_action
[] = {
166 [PPC_MSG_CALL_FUNCTION
] = call_function_action
,
167 [PPC_MSG_RESCHEDULE
] = reschedule_action
,
168 [PPC_MSG_CALL_FUNC_SINGLE
] = call_function_single_action
,
169 [PPC_MSG_DEBUGGER_BREAK
] = debug_ipi_action
,
172 const char *smp_ipi_name
[] = {
173 [PPC_MSG_CALL_FUNCTION
] = "ipi call function",
174 [PPC_MSG_RESCHEDULE
] = "ipi reschedule",
175 [PPC_MSG_CALL_FUNC_SINGLE
] = "ipi call function single",
176 [PPC_MSG_DEBUGGER_BREAK
] = "ipi debugger",
179 /* optional function to request ipi, for controllers with >= 4 ipis */
180 int smp_request_message_ipi(int virq
, int msg
)
184 if (msg
< 0 || msg
> PPC_MSG_DEBUGGER_BREAK
) {
187 #if !defined(CONFIG_DEBUGGER) && !defined(CONFIG_KEXEC)
188 if (msg
== PPC_MSG_DEBUGGER_BREAK
) {
192 err
= request_irq(virq
, smp_ipi_action
[msg
], IRQF_DISABLED
|IRQF_PERCPU
,
193 smp_ipi_name
[msg
], 0);
194 WARN(err
< 0, "unable to request_irq %d for %s (rc %d)\n",
195 virq
, smp_ipi_name
[msg
], err
);
200 void smp_send_reschedule(int cpu
)
203 smp_ops
->message_pass(cpu
, PPC_MSG_RESCHEDULE
);
206 void arch_send_call_function_single_ipi(int cpu
)
208 smp_ops
->message_pass(cpu
, PPC_MSG_CALL_FUNC_SINGLE
);
211 void arch_send_call_function_ipi_mask(const struct cpumask
*mask
)
215 for_each_cpu(cpu
, mask
)
216 smp_ops
->message_pass(cpu
, PPC_MSG_CALL_FUNCTION
);
219 #ifdef CONFIG_DEBUGGER
220 void smp_send_debugger_break(int cpu
)
223 smp_ops
->message_pass(cpu
, PPC_MSG_DEBUGGER_BREAK
);
228 void crash_send_ipi(void (*crash_ipi_callback
)(struct pt_regs
*))
230 crash_ipi_function_ptr
= crash_ipi_callback
;
231 if (crash_ipi_callback
&& smp_ops
) {
233 smp_ops
->message_pass(MSG_ALL_BUT_SELF
, PPC_MSG_DEBUGGER_BREAK
);
238 static void stop_this_cpu(void *dummy
)
240 /* Remove this CPU */
241 set_cpu_online(smp_processor_id(), false);
248 void smp_send_stop(void)
250 smp_call_function(stop_this_cpu
, NULL
, 0);
253 struct thread_info
*current_set
[NR_CPUS
];
255 static void __devinit
smp_store_cpu_info(int id
)
257 per_cpu(cpu_pvr
, id
) = mfspr(SPRN_PVR
);
260 void __init
smp_prepare_cpus(unsigned int max_cpus
)
264 DBG("smp_prepare_cpus\n");
267 * setup_cpu may need to be called on the boot cpu. We havent
268 * spun any cpus up but lets be paranoid.
270 BUG_ON(boot_cpuid
!= smp_processor_id());
273 smp_store_cpu_info(boot_cpuid
);
274 cpu_callin_map
[boot_cpuid
] = 1;
276 for_each_possible_cpu(cpu
) {
277 zalloc_cpumask_var_node(&per_cpu(cpu_sibling_map
, cpu
),
278 GFP_KERNEL
, cpu_to_node(cpu
));
279 zalloc_cpumask_var_node(&per_cpu(cpu_core_map
, cpu
),
280 GFP_KERNEL
, cpu_to_node(cpu
));
283 cpumask_set_cpu(boot_cpuid
, cpu_sibling_mask(boot_cpuid
));
284 cpumask_set_cpu(boot_cpuid
, cpu_core_mask(boot_cpuid
));
288 max_cpus
= smp_ops
->probe();
295 void __devinit
smp_prepare_boot_cpu(void)
297 BUG_ON(smp_processor_id() != boot_cpuid
);
299 paca
[boot_cpuid
].__current
= current
;
301 current_set
[boot_cpuid
] = task_thread_info(current
);
304 #ifdef CONFIG_HOTPLUG_CPU
305 /* State of each CPU during hotplug phases */
306 static DEFINE_PER_CPU(int, cpu_state
) = { 0 };
308 int generic_cpu_disable(void)
310 unsigned int cpu
= smp_processor_id();
312 if (cpu
== boot_cpuid
)
315 set_cpu_online(cpu
, false);
317 vdso_data
->processorCount
--;
323 void generic_cpu_die(unsigned int cpu
)
327 for (i
= 0; i
< 100; i
++) {
329 if (per_cpu(cpu_state
, cpu
) == CPU_DEAD
)
333 printk(KERN_ERR
"CPU%d didn't die...\n", cpu
);
336 void generic_mach_cpu_die(void)
342 cpu
= smp_processor_id();
343 printk(KERN_DEBUG
"CPU%d offline\n", cpu
);
344 __get_cpu_var(cpu_state
) = CPU_DEAD
;
346 while (__get_cpu_var(cpu_state
) != CPU_UP_PREPARE
)
350 void generic_set_cpu_dead(unsigned int cpu
)
352 per_cpu(cpu_state
, cpu
) = CPU_DEAD
;
357 struct work_struct work
;
358 struct task_struct
*idle
;
359 struct completion done
;
363 static void __cpuinit
do_fork_idle(struct work_struct
*work
)
365 struct create_idle
*c_idle
=
366 container_of(work
, struct create_idle
, work
);
368 c_idle
->idle
= fork_idle(c_idle
->cpu
);
369 complete(&c_idle
->done
);
372 static int __cpuinit
create_idle(unsigned int cpu
)
374 struct thread_info
*ti
;
375 struct create_idle c_idle
= {
377 .done
= COMPLETION_INITIALIZER_ONSTACK(c_idle
.done
),
379 INIT_WORK_ONSTACK(&c_idle
.work
, do_fork_idle
);
381 c_idle
.idle
= get_idle_for_cpu(cpu
);
383 /* We can't use kernel_thread since we must avoid to
384 * reschedule the child. We use a workqueue because
385 * we want to fork from a kernel thread, not whatever
386 * userspace process happens to be trying to online us.
389 schedule_work(&c_idle
.work
);
390 wait_for_completion(&c_idle
.done
);
392 init_idle(c_idle
.idle
, cpu
);
393 if (IS_ERR(c_idle
.idle
)) {
394 pr_err("Failed fork for CPU %u: %li", cpu
, PTR_ERR(c_idle
.idle
));
395 return PTR_ERR(c_idle
.idle
);
397 ti
= task_thread_info(c_idle
.idle
);
400 paca
[cpu
].__current
= c_idle
.idle
;
401 paca
[cpu
].kstack
= (unsigned long)ti
+ THREAD_SIZE
- STACK_FRAME_OVERHEAD
;
404 current_set
[cpu
] = ti
;
409 int __cpuinit
__cpu_up(unsigned int cpu
)
413 secondary_ti
= current_set
[cpu
];
415 if (smp_ops
== NULL
||
416 (smp_ops
->cpu_bootable
&& !smp_ops
->cpu_bootable(cpu
)))
419 /* Make sure we have an idle thread */
420 rc
= create_idle(cpu
);
424 /* Make sure callin-map entry is 0 (can be leftover a CPU
427 cpu_callin_map
[cpu
] = 0;
429 /* The information for processor bringup must
430 * be written out to main store before we release
436 DBG("smp: kicking cpu %d\n", cpu
);
437 smp_ops
->kick_cpu(cpu
);
440 * wait to see if the cpu made a callin (is actually up).
441 * use this value that I found through experimentation.
444 if (system_state
< SYSTEM_RUNNING
)
445 for (c
= 50000; c
&& !cpu_callin_map
[cpu
]; c
--)
447 #ifdef CONFIG_HOTPLUG_CPU
450 * CPUs can take much longer to come up in the
451 * hotplug case. Wait five seconds.
453 for (c
= 5000; c
&& !cpu_callin_map
[cpu
]; c
--)
457 if (!cpu_callin_map
[cpu
]) {
458 printk(KERN_ERR
"Processor %u is stuck.\n", cpu
);
462 DBG("Processor %u found.\n", cpu
);
464 if (smp_ops
->give_timebase
)
465 smp_ops
->give_timebase();
467 /* Wait until cpu puts itself in the online map */
468 while (!cpu_online(cpu
))
474 /* Return the value of the reg property corresponding to the given
477 int cpu_to_core_id(int cpu
)
479 struct device_node
*np
;
483 np
= of_get_cpu_node(cpu
, NULL
);
487 reg
= of_get_property(np
, "reg", NULL
);
497 /* Helper routines for cpu to core mapping */
498 int cpu_core_index_of_thread(int cpu
)
500 return cpu
>> threads_shift
;
502 EXPORT_SYMBOL_GPL(cpu_core_index_of_thread
);
504 int cpu_first_thread_of_core(int core
)
506 return core
<< threads_shift
;
508 EXPORT_SYMBOL_GPL(cpu_first_thread_of_core
);
510 /* Must be called when no change can occur to cpu_present_map,
511 * i.e. during cpu online or offline.
513 static struct device_node
*cpu_to_l2cache(int cpu
)
515 struct device_node
*np
;
516 struct device_node
*cache
;
518 if (!cpu_present(cpu
))
521 np
= of_get_cpu_node(cpu
, NULL
);
525 cache
= of_find_next_cache_node(np
);
532 /* Activate a secondary processor. */
533 void __devinit
start_secondary(void *unused
)
535 unsigned int cpu
= smp_processor_id();
536 struct device_node
*l2_cache
;
539 atomic_inc(&init_mm
.mm_count
);
540 current
->active_mm
= &init_mm
;
542 smp_store_cpu_info(cpu
);
543 set_dec(tb_ticks_per_jiffy
);
545 cpu_callin_map
[cpu
] = 1;
547 if (smp_ops
->setup_cpu
)
548 smp_ops
->setup_cpu(cpu
);
549 if (smp_ops
->take_timebase
)
550 smp_ops
->take_timebase();
552 secondary_cpu_time_init();
555 if (system_state
== SYSTEM_RUNNING
)
556 vdso_data
->processorCount
++;
559 notify_cpu_starting(cpu
);
560 set_cpu_online(cpu
, true);
561 /* Update sibling maps */
562 base
= cpu_first_thread_sibling(cpu
);
563 for (i
= 0; i
< threads_per_core
; i
++) {
564 if (cpu_is_offline(base
+ i
))
566 cpumask_set_cpu(cpu
, cpu_sibling_mask(base
+ i
));
567 cpumask_set_cpu(base
+ i
, cpu_sibling_mask(cpu
));
569 /* cpu_core_map should be a superset of
570 * cpu_sibling_map even if we don't have cache
571 * information, so update the former here, too.
573 cpumask_set_cpu(cpu
, cpu_core_mask(base
+ i
));
574 cpumask_set_cpu(base
+ i
, cpu_core_mask(cpu
));
576 l2_cache
= cpu_to_l2cache(cpu
);
577 for_each_online_cpu(i
) {
578 struct device_node
*np
= cpu_to_l2cache(i
);
581 if (np
== l2_cache
) {
582 cpumask_set_cpu(cpu
, cpu_core_mask(i
));
583 cpumask_set_cpu(i
, cpu_core_mask(cpu
));
587 of_node_put(l2_cache
);
597 int setup_profiling_timer(unsigned int multiplier
)
602 void __init
smp_cpus_done(unsigned int max_cpus
)
604 cpumask_var_t old_mask
;
606 /* We want the setup_cpu() here to be called from CPU 0, but our
607 * init thread may have been "borrowed" by another CPU in the meantime
608 * se we pin us down to CPU 0 for a short while
610 alloc_cpumask_var(&old_mask
, GFP_NOWAIT
);
611 cpumask_copy(old_mask
, ¤t
->cpus_allowed
);
612 set_cpus_allowed_ptr(current
, cpumask_of(boot_cpuid
));
614 if (smp_ops
&& smp_ops
->setup_cpu
)
615 smp_ops
->setup_cpu(boot_cpuid
);
617 set_cpus_allowed_ptr(current
, old_mask
);
619 free_cpumask_var(old_mask
);
621 if (smp_ops
&& smp_ops
->bringup_done
)
622 smp_ops
->bringup_done();
624 dump_numa_cpu_topology();
628 int arch_sd_sibling_asym_packing(void)
630 if (cpu_has_feature(CPU_FTR_ASYM_SMT
)) {
631 printk_once(KERN_INFO
"Enabling Asymmetric SMT scheduling\n");
632 return SD_ASYM_PACKING
;
637 #ifdef CONFIG_HOTPLUG_CPU
638 int __cpu_disable(void)
640 struct device_node
*l2_cache
;
641 int cpu
= smp_processor_id();
645 if (!smp_ops
->cpu_disable
)
648 err
= smp_ops
->cpu_disable();
652 /* Update sibling maps */
653 base
= cpu_first_thread_sibling(cpu
);
654 for (i
= 0; i
< threads_per_core
; i
++) {
655 cpumask_clear_cpu(cpu
, cpu_sibling_mask(base
+ i
));
656 cpumask_clear_cpu(base
+ i
, cpu_sibling_mask(cpu
));
657 cpumask_clear_cpu(cpu
, cpu_core_mask(base
+ i
));
658 cpumask_clear_cpu(base
+ i
, cpu_core_mask(cpu
));
661 l2_cache
= cpu_to_l2cache(cpu
);
662 for_each_present_cpu(i
) {
663 struct device_node
*np
= cpu_to_l2cache(i
);
666 if (np
== l2_cache
) {
667 cpumask_clear_cpu(cpu
, cpu_core_mask(i
));
668 cpumask_clear_cpu(i
, cpu_core_mask(cpu
));
672 of_node_put(l2_cache
);
678 void __cpu_die(unsigned int cpu
)
680 if (smp_ops
->cpu_die
)
681 smp_ops
->cpu_die(cpu
);
684 static DEFINE_MUTEX(powerpc_cpu_hotplug_driver_mutex
);
686 void cpu_hotplug_driver_lock()
688 mutex_lock(&powerpc_cpu_hotplug_driver_mutex
);
691 void cpu_hotplug_driver_unlock()
693 mutex_unlock(&powerpc_cpu_hotplug_driver_mutex
);
701 /* If we return, we re-enter start_secondary */
702 start_secondary_resume();