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/export.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/device.h>
31 #include <linux/cpu.h>
32 #include <linux/notifier.h>
33 #include <linux/topology.h>
34 #include <linux/profile.h>
36 #include <asm/ptrace.h>
37 #include <linux/atomic.h>
39 #include <asm/hw_irq.h>
40 #include <asm/kvm_ppc.h>
42 #include <asm/pgtable.h>
46 #include <asm/machdep.h>
47 #include <asm/cputhreads.h>
48 #include <asm/cputable.h>
50 #include <asm/vdso_datapage.h>
55 #include <asm/debug.h>
56 #include <asm/kexec.h>
57 #include <asm/asm-prototypes.h>
58 #include <asm/cpu_has_feature.h>
62 #define DBG(fmt...) udbg_printf(fmt)
67 #ifdef CONFIG_HOTPLUG_CPU
68 /* State of each CPU during hotplug phases */
69 static DEFINE_PER_CPU(int, cpu_state
) = { 0 };
72 struct thread_info
*secondary_ti
;
74 DEFINE_PER_CPU(cpumask_var_t
, cpu_sibling_map
);
75 DEFINE_PER_CPU(cpumask_var_t
, cpu_core_map
);
77 EXPORT_PER_CPU_SYMBOL(cpu_sibling_map
);
78 EXPORT_PER_CPU_SYMBOL(cpu_core_map
);
80 /* SMP operations for this machine */
81 struct smp_ops_t
*smp_ops
;
83 /* Can't be static due to PowerMac hackery */
84 volatile unsigned int cpu_callin_map
[NR_CPUS
];
86 int smt_enabled_at_boot
= 1;
88 static void (*crash_ipi_function_ptr
)(struct pt_regs
*) = NULL
;
91 * Returns 1 if the specified cpu should be brought up during boot.
92 * Used to inhibit booting threads if they've been disabled or
93 * limited on the command line
95 int smp_generic_cpu_bootable(unsigned int nr
)
97 /* Special case - we inhibit secondary thread startup
98 * during boot if the user requests it.
100 if (system_state
== SYSTEM_BOOTING
&& cpu_has_feature(CPU_FTR_SMT
)) {
101 if (!smt_enabled_at_boot
&& cpu_thread_in_core(nr
) != 0)
103 if (smt_enabled_at_boot
104 && cpu_thread_in_core(nr
) >= smt_enabled_at_boot
)
113 int smp_generic_kick_cpu(int nr
)
115 BUG_ON(nr
< 0 || nr
>= NR_CPUS
);
118 * The processor is currently spinning, waiting for the
119 * cpu_start field to become non-zero After we set cpu_start,
120 * the processor will continue on to secondary_start
122 if (!paca
[nr
].cpu_start
) {
123 paca
[nr
].cpu_start
= 1;
128 #ifdef CONFIG_HOTPLUG_CPU
130 * Ok it's not there, so it might be soft-unplugged, let's
131 * try to bring it back
133 generic_set_cpu_up(nr
);
135 smp_send_reschedule(nr
);
136 #endif /* CONFIG_HOTPLUG_CPU */
140 #endif /* CONFIG_PPC64 */
142 static irqreturn_t
call_function_action(int irq
, void *data
)
144 generic_smp_call_function_interrupt();
148 static irqreturn_t
reschedule_action(int irq
, void *data
)
154 static irqreturn_t
tick_broadcast_ipi_action(int irq
, void *data
)
156 tick_broadcast_ipi_handler();
160 static irqreturn_t
debug_ipi_action(int irq
, void *data
)
162 if (crash_ipi_function_ptr
) {
163 crash_ipi_function_ptr(get_irq_regs());
167 #ifdef CONFIG_DEBUGGER
168 debugger_ipi(get_irq_regs());
169 #endif /* CONFIG_DEBUGGER */
174 static irq_handler_t smp_ipi_action
[] = {
175 [PPC_MSG_CALL_FUNCTION
] = call_function_action
,
176 [PPC_MSG_RESCHEDULE
] = reschedule_action
,
177 [PPC_MSG_TICK_BROADCAST
] = tick_broadcast_ipi_action
,
178 [PPC_MSG_DEBUGGER_BREAK
] = debug_ipi_action
,
181 const char *smp_ipi_name
[] = {
182 [PPC_MSG_CALL_FUNCTION
] = "ipi call function",
183 [PPC_MSG_RESCHEDULE
] = "ipi reschedule",
184 [PPC_MSG_TICK_BROADCAST
] = "ipi tick-broadcast",
185 [PPC_MSG_DEBUGGER_BREAK
] = "ipi debugger",
188 /* optional function to request ipi, for controllers with >= 4 ipis */
189 int smp_request_message_ipi(int virq
, int msg
)
193 if (msg
< 0 || msg
> PPC_MSG_DEBUGGER_BREAK
) {
196 #if !defined(CONFIG_DEBUGGER) && !defined(CONFIG_KEXEC)
197 if (msg
== PPC_MSG_DEBUGGER_BREAK
) {
201 err
= request_irq(virq
, smp_ipi_action
[msg
],
202 IRQF_PERCPU
| IRQF_NO_THREAD
| IRQF_NO_SUSPEND
,
203 smp_ipi_name
[msg
], NULL
);
204 WARN(err
< 0, "unable to request_irq %d for %s (rc %d)\n",
205 virq
, smp_ipi_name
[msg
], err
);
210 #ifdef CONFIG_PPC_SMP_MUXED_IPI
211 struct cpu_messages
{
212 long messages
; /* current messages */
213 unsigned long data
; /* data for cause ipi */
215 static DEFINE_PER_CPU_SHARED_ALIGNED(struct cpu_messages
, ipi_message
);
217 void smp_muxed_ipi_set_data(int cpu
, unsigned long data
)
219 struct cpu_messages
*info
= &per_cpu(ipi_message
, cpu
);
224 void smp_muxed_ipi_set_message(int cpu
, int msg
)
226 struct cpu_messages
*info
= &per_cpu(ipi_message
, cpu
);
227 char *message
= (char *)&info
->messages
;
230 * Order previous accesses before accesses in the IPI handler.
236 void smp_muxed_ipi_message_pass(int cpu
, int msg
)
238 struct cpu_messages
*info
= &per_cpu(ipi_message
, cpu
);
240 smp_muxed_ipi_set_message(cpu
, msg
);
242 * cause_ipi functions are required to include a full barrier
243 * before doing whatever causes the IPI.
245 smp_ops
->cause_ipi(cpu
, info
->data
);
248 #ifdef __BIG_ENDIAN__
249 #define IPI_MESSAGE(A) (1uL << ((BITS_PER_LONG - 8) - 8 * (A)))
251 #define IPI_MESSAGE(A) (1uL << (8 * (A)))
254 irqreturn_t
smp_ipi_demux(void)
256 struct cpu_messages
*info
= this_cpu_ptr(&ipi_message
);
259 mb(); /* order any irq clear */
262 all
= xchg(&info
->messages
, 0);
263 #if defined(CONFIG_KVM_XICS) && defined(CONFIG_KVM_BOOK3S_HV_POSSIBLE)
265 * Must check for PPC_MSG_RM_HOST_ACTION messages
266 * before PPC_MSG_CALL_FUNCTION messages because when
267 * a VM is destroyed, we call kick_all_cpus_sync()
268 * to ensure that any pending PPC_MSG_RM_HOST_ACTION
269 * messages have completed before we free any VCPUs.
271 if (all
& IPI_MESSAGE(PPC_MSG_RM_HOST_ACTION
))
272 kvmppc_xics_ipi_action();
274 if (all
& IPI_MESSAGE(PPC_MSG_CALL_FUNCTION
))
275 generic_smp_call_function_interrupt();
276 if (all
& IPI_MESSAGE(PPC_MSG_RESCHEDULE
))
278 if (all
& IPI_MESSAGE(PPC_MSG_TICK_BROADCAST
))
279 tick_broadcast_ipi_handler();
280 if (all
& IPI_MESSAGE(PPC_MSG_DEBUGGER_BREAK
))
281 debug_ipi_action(0, NULL
);
282 } while (info
->messages
);
286 #endif /* CONFIG_PPC_SMP_MUXED_IPI */
288 static inline void do_message_pass(int cpu
, int msg
)
290 if (smp_ops
->message_pass
)
291 smp_ops
->message_pass(cpu
, msg
);
292 #ifdef CONFIG_PPC_SMP_MUXED_IPI
294 smp_muxed_ipi_message_pass(cpu
, msg
);
298 void smp_send_reschedule(int cpu
)
301 do_message_pass(cpu
, PPC_MSG_RESCHEDULE
);
303 EXPORT_SYMBOL_GPL(smp_send_reschedule
);
305 void arch_send_call_function_single_ipi(int cpu
)
307 do_message_pass(cpu
, PPC_MSG_CALL_FUNCTION
);
310 void arch_send_call_function_ipi_mask(const struct cpumask
*mask
)
314 for_each_cpu(cpu
, mask
)
315 do_message_pass(cpu
, PPC_MSG_CALL_FUNCTION
);
318 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
319 void tick_broadcast(const struct cpumask
*mask
)
323 for_each_cpu(cpu
, mask
)
324 do_message_pass(cpu
, PPC_MSG_TICK_BROADCAST
);
328 #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
329 void smp_send_debugger_break(void)
332 int me
= raw_smp_processor_id();
334 if (unlikely(!smp_ops
))
337 for_each_online_cpu(cpu
)
339 do_message_pass(cpu
, PPC_MSG_DEBUGGER_BREAK
);
344 void crash_send_ipi(void (*crash_ipi_callback
)(struct pt_regs
*))
346 crash_ipi_function_ptr
= crash_ipi_callback
;
347 if (crash_ipi_callback
) {
349 smp_send_debugger_break();
354 static void stop_this_cpu(void *dummy
)
356 /* Remove this CPU */
357 set_cpu_online(smp_processor_id(), false);
364 void smp_send_stop(void)
366 smp_call_function(stop_this_cpu
, NULL
, 0);
369 struct thread_info
*current_set
[NR_CPUS
];
371 static void smp_store_cpu_info(int id
)
373 per_cpu(cpu_pvr
, id
) = mfspr(SPRN_PVR
);
374 #ifdef CONFIG_PPC_FSL_BOOK3E
375 per_cpu(next_tlbcam_idx
, id
)
376 = (mfspr(SPRN_TLB1CFG
) & TLBnCFG_N_ENTRY
) - 1;
380 void __init
smp_prepare_cpus(unsigned int max_cpus
)
384 DBG("smp_prepare_cpus\n");
387 * setup_cpu may need to be called on the boot cpu. We havent
388 * spun any cpus up but lets be paranoid.
390 BUG_ON(boot_cpuid
!= smp_processor_id());
393 smp_store_cpu_info(boot_cpuid
);
394 cpu_callin_map
[boot_cpuid
] = 1;
396 for_each_possible_cpu(cpu
) {
397 zalloc_cpumask_var_node(&per_cpu(cpu_sibling_map
, cpu
),
398 GFP_KERNEL
, cpu_to_node(cpu
));
399 zalloc_cpumask_var_node(&per_cpu(cpu_core_map
, cpu
),
400 GFP_KERNEL
, cpu_to_node(cpu
));
402 * numa_node_id() works after this.
404 if (cpu_present(cpu
)) {
405 set_cpu_numa_node(cpu
, numa_cpu_lookup_table
[cpu
]);
406 set_cpu_numa_mem(cpu
,
407 local_memory_node(numa_cpu_lookup_table
[cpu
]));
411 cpumask_set_cpu(boot_cpuid
, cpu_sibling_mask(boot_cpuid
));
412 cpumask_set_cpu(boot_cpuid
, cpu_core_mask(boot_cpuid
));
414 if (smp_ops
&& smp_ops
->probe
)
418 void smp_prepare_boot_cpu(void)
420 BUG_ON(smp_processor_id() != boot_cpuid
);
422 paca
[boot_cpuid
].__current
= current
;
424 set_numa_node(numa_cpu_lookup_table
[boot_cpuid
]);
425 current_set
[boot_cpuid
] = task_thread_info(current
);
428 #ifdef CONFIG_HOTPLUG_CPU
430 int generic_cpu_disable(void)
432 unsigned int cpu
= smp_processor_id();
434 if (cpu
== boot_cpuid
)
437 set_cpu_online(cpu
, false);
439 vdso_data
->processorCount
--;
445 void generic_cpu_die(unsigned int cpu
)
449 for (i
= 0; i
< 100; i
++) {
451 if (is_cpu_dead(cpu
))
455 printk(KERN_ERR
"CPU%d didn't die...\n", cpu
);
458 void generic_set_cpu_dead(unsigned int cpu
)
460 per_cpu(cpu_state
, cpu
) = CPU_DEAD
;
464 * The cpu_state should be set to CPU_UP_PREPARE in kick_cpu(), otherwise
465 * the cpu_state is always CPU_DEAD after calling generic_set_cpu_dead(),
466 * which makes the delay in generic_cpu_die() not happen.
468 void generic_set_cpu_up(unsigned int cpu
)
470 per_cpu(cpu_state
, cpu
) = CPU_UP_PREPARE
;
473 int generic_check_cpu_restart(unsigned int cpu
)
475 return per_cpu(cpu_state
, cpu
) == CPU_UP_PREPARE
;
478 int is_cpu_dead(unsigned int cpu
)
480 return per_cpu(cpu_state
, cpu
) == CPU_DEAD
;
483 static bool secondaries_inhibited(void)
485 return kvm_hv_mode_active();
488 #else /* HOTPLUG_CPU */
490 #define secondaries_inhibited() 0
494 static void cpu_idle_thread_init(unsigned int cpu
, struct task_struct
*idle
)
496 struct thread_info
*ti
= task_thread_info(idle
);
499 paca
[cpu
].__current
= idle
;
500 paca
[cpu
].kstack
= (unsigned long)ti
+ THREAD_SIZE
- STACK_FRAME_OVERHEAD
;
503 secondary_ti
= current_set
[cpu
] = ti
;
506 int __cpu_up(unsigned int cpu
, struct task_struct
*tidle
)
511 * Don't allow secondary threads to come online if inhibited
513 if (threads_per_core
> 1 && secondaries_inhibited() &&
514 cpu_thread_in_subcore(cpu
))
517 if (smp_ops
== NULL
||
518 (smp_ops
->cpu_bootable
&& !smp_ops
->cpu_bootable(cpu
)))
521 cpu_idle_thread_init(cpu
, tidle
);
523 /* Make sure callin-map entry is 0 (can be leftover a CPU
526 cpu_callin_map
[cpu
] = 0;
528 /* The information for processor bringup must
529 * be written out to main store before we release
535 DBG("smp: kicking cpu %d\n", cpu
);
536 rc
= smp_ops
->kick_cpu(cpu
);
538 pr_err("smp: failed starting cpu %d (rc %d)\n", cpu
, rc
);
543 * wait to see if the cpu made a callin (is actually up).
544 * use this value that I found through experimentation.
547 if (system_state
< SYSTEM_RUNNING
)
548 for (c
= 50000; c
&& !cpu_callin_map
[cpu
]; c
--)
550 #ifdef CONFIG_HOTPLUG_CPU
553 * CPUs can take much longer to come up in the
554 * hotplug case. Wait five seconds.
556 for (c
= 5000; c
&& !cpu_callin_map
[cpu
]; c
--)
560 if (!cpu_callin_map
[cpu
]) {
561 printk(KERN_ERR
"Processor %u is stuck.\n", cpu
);
565 DBG("Processor %u found.\n", cpu
);
567 if (smp_ops
->give_timebase
)
568 smp_ops
->give_timebase();
570 /* Wait until cpu puts itself in the online & active maps */
571 while (!cpu_online(cpu
))
577 /* Return the value of the reg property corresponding to the given
580 int cpu_to_core_id(int cpu
)
582 struct device_node
*np
;
586 np
= of_get_cpu_node(cpu
, NULL
);
590 reg
= of_get_property(np
, "reg", NULL
);
594 id
= be32_to_cpup(reg
);
599 EXPORT_SYMBOL_GPL(cpu_to_core_id
);
601 /* Helper routines for cpu to core mapping */
602 int cpu_core_index_of_thread(int cpu
)
604 return cpu
>> threads_shift
;
606 EXPORT_SYMBOL_GPL(cpu_core_index_of_thread
);
608 int cpu_first_thread_of_core(int core
)
610 return core
<< threads_shift
;
612 EXPORT_SYMBOL_GPL(cpu_first_thread_of_core
);
614 static void traverse_siblings_chip_id(int cpu
, bool add
, int chipid
)
616 const struct cpumask
*mask
;
617 struct device_node
*np
;
621 mask
= add
? cpu_online_mask
: cpu_present_mask
;
622 for_each_cpu(i
, mask
) {
623 np
= of_get_cpu_node(i
, NULL
);
626 prop
= of_get_property(np
, "ibm,chip-id", &plen
);
627 if (prop
&& plen
== sizeof(int) &&
628 of_read_number(prop
, 1) == chipid
) {
630 cpumask_set_cpu(cpu
, cpu_core_mask(i
));
631 cpumask_set_cpu(i
, cpu_core_mask(cpu
));
633 cpumask_clear_cpu(cpu
, cpu_core_mask(i
));
634 cpumask_clear_cpu(i
, cpu_core_mask(cpu
));
641 /* Must be called when no change can occur to cpu_present_mask,
642 * i.e. during cpu online or offline.
644 static struct device_node
*cpu_to_l2cache(int cpu
)
646 struct device_node
*np
;
647 struct device_node
*cache
;
649 if (!cpu_present(cpu
))
652 np
= of_get_cpu_node(cpu
, NULL
);
656 cache
= of_find_next_cache_node(np
);
663 static void traverse_core_siblings(int cpu
, bool add
)
665 struct device_node
*l2_cache
, *np
;
666 const struct cpumask
*mask
;
670 /* First see if we have ibm,chip-id properties in cpu nodes */
671 np
= of_get_cpu_node(cpu
, NULL
);
674 prop
= of_get_property(np
, "ibm,chip-id", &plen
);
675 if (prop
&& plen
== sizeof(int))
676 chip
= of_read_number(prop
, 1);
679 traverse_siblings_chip_id(cpu
, add
, chip
);
684 l2_cache
= cpu_to_l2cache(cpu
);
685 mask
= add
? cpu_online_mask
: cpu_present_mask
;
686 for_each_cpu(i
, mask
) {
687 np
= cpu_to_l2cache(i
);
690 if (np
== l2_cache
) {
692 cpumask_set_cpu(cpu
, cpu_core_mask(i
));
693 cpumask_set_cpu(i
, cpu_core_mask(cpu
));
695 cpumask_clear_cpu(cpu
, cpu_core_mask(i
));
696 cpumask_clear_cpu(i
, cpu_core_mask(cpu
));
701 of_node_put(l2_cache
);
704 /* Activate a secondary processor. */
705 void start_secondary(void *unused
)
707 unsigned int cpu
= smp_processor_id();
710 atomic_inc(&init_mm
.mm_count
);
711 current
->active_mm
= &init_mm
;
713 smp_store_cpu_info(cpu
);
714 set_dec(tb_ticks_per_jiffy
);
716 cpu_callin_map
[cpu
] = 1;
718 if (smp_ops
->setup_cpu
)
719 smp_ops
->setup_cpu(cpu
);
720 if (smp_ops
->take_timebase
)
721 smp_ops
->take_timebase();
723 secondary_cpu_time_init();
726 if (system_state
== SYSTEM_RUNNING
)
727 vdso_data
->processorCount
++;
731 /* Update sibling maps */
732 base
= cpu_first_thread_sibling(cpu
);
733 for (i
= 0; i
< threads_per_core
; i
++) {
734 if (cpu_is_offline(base
+ i
) && (cpu
!= base
+ i
))
736 cpumask_set_cpu(cpu
, cpu_sibling_mask(base
+ i
));
737 cpumask_set_cpu(base
+ i
, cpu_sibling_mask(cpu
));
739 /* cpu_core_map should be a superset of
740 * cpu_sibling_map even if we don't have cache
741 * information, so update the former here, too.
743 cpumask_set_cpu(cpu
, cpu_core_mask(base
+ i
));
744 cpumask_set_cpu(base
+ i
, cpu_core_mask(cpu
));
746 traverse_core_siblings(cpu
, true);
748 set_numa_node(numa_cpu_lookup_table
[cpu
]);
749 set_numa_mem(local_memory_node(numa_cpu_lookup_table
[cpu
]));
752 notify_cpu_starting(cpu
);
753 set_cpu_online(cpu
, true);
757 cpu_startup_entry(CPUHP_AP_ONLINE_IDLE
);
762 int setup_profiling_timer(unsigned int multiplier
)
767 #ifdef CONFIG_SCHED_SMT
768 /* cpumask of CPUs with asymetric SMT dependancy */
769 static int powerpc_smt_flags(void)
771 int flags
= SD_SHARE_CPUCAPACITY
| SD_SHARE_PKG_RESOURCES
;
773 if (cpu_has_feature(CPU_FTR_ASYM_SMT
)) {
774 printk_once(KERN_INFO
"Enabling Asymmetric SMT scheduling\n");
775 flags
|= SD_ASYM_PACKING
;
781 static struct sched_domain_topology_level powerpc_topology
[] = {
782 #ifdef CONFIG_SCHED_SMT
783 { cpu_smt_mask
, powerpc_smt_flags
, SD_INIT_NAME(SMT
) },
785 { cpu_cpu_mask
, SD_INIT_NAME(DIE
) },
789 void __init
smp_cpus_done(unsigned int max_cpus
)
791 cpumask_var_t old_mask
;
793 /* We want the setup_cpu() here to be called from CPU 0, but our
794 * init thread may have been "borrowed" by another CPU in the meantime
795 * se we pin us down to CPU 0 for a short while
797 alloc_cpumask_var(&old_mask
, GFP_NOWAIT
);
798 cpumask_copy(old_mask
, tsk_cpus_allowed(current
));
799 set_cpus_allowed_ptr(current
, cpumask_of(boot_cpuid
));
801 if (smp_ops
&& smp_ops
->setup_cpu
)
802 smp_ops
->setup_cpu(boot_cpuid
);
804 set_cpus_allowed_ptr(current
, old_mask
);
806 free_cpumask_var(old_mask
);
808 if (smp_ops
&& smp_ops
->bringup_done
)
809 smp_ops
->bringup_done();
811 dump_numa_cpu_topology();
813 set_sched_topology(powerpc_topology
);
817 #ifdef CONFIG_HOTPLUG_CPU
818 int __cpu_disable(void)
820 int cpu
= smp_processor_id();
824 if (!smp_ops
->cpu_disable
)
827 err
= smp_ops
->cpu_disable();
831 /* Update sibling maps */
832 base
= cpu_first_thread_sibling(cpu
);
833 for (i
= 0; i
< threads_per_core
&& base
+ i
< nr_cpu_ids
; i
++) {
834 cpumask_clear_cpu(cpu
, cpu_sibling_mask(base
+ i
));
835 cpumask_clear_cpu(base
+ i
, cpu_sibling_mask(cpu
));
836 cpumask_clear_cpu(cpu
, cpu_core_mask(base
+ i
));
837 cpumask_clear_cpu(base
+ i
, cpu_core_mask(cpu
));
839 traverse_core_siblings(cpu
, false);
844 void __cpu_die(unsigned int cpu
)
846 if (smp_ops
->cpu_die
)
847 smp_ops
->cpu_die(cpu
);
855 /* If we return, we re-enter start_secondary */
856 start_secondary_resume();