18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * smp.h: PowerPC-specific SMP code.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Original was a copy of sparc smp.h.  Now heavily modified
68c2ecf20Sopenharmony_ci * for PPC.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
98c2ecf20Sopenharmony_ci * Copyright (C) 1996-2001 Cort Dougan <cort@fsmlabs.com>
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#ifndef _ASM_POWERPC_SMP_H
138c2ecf20Sopenharmony_ci#define _ASM_POWERPC_SMP_H
148c2ecf20Sopenharmony_ci#ifdef __KERNEL__
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <linux/threads.h>
178c2ecf20Sopenharmony_ci#include <linux/cpumask.h>
188c2ecf20Sopenharmony_ci#include <linux/kernel.h>
198c2ecf20Sopenharmony_ci#include <linux/irqreturn.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#ifndef __ASSEMBLY__
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC64
248c2ecf20Sopenharmony_ci#include <asm/paca.h>
258c2ecf20Sopenharmony_ci#endif
268c2ecf20Sopenharmony_ci#include <asm/percpu.h>
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ciextern int boot_cpuid;
298c2ecf20Sopenharmony_ciextern int spinning_secondaries;
308c2ecf20Sopenharmony_ciextern u32 *cpu_to_phys_id;
318c2ecf20Sopenharmony_ciextern bool coregroup_enabled;
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ciextern int cpu_to_chip_id(int cpu);
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistruct smp_ops_t {
388c2ecf20Sopenharmony_ci	void  (*message_pass)(int cpu, int msg);
398c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC_SMP_MUXED_IPI
408c2ecf20Sopenharmony_ci	void  (*cause_ipi)(int cpu);
418c2ecf20Sopenharmony_ci#endif
428c2ecf20Sopenharmony_ci	int   (*cause_nmi_ipi)(int cpu);
438c2ecf20Sopenharmony_ci	void  (*probe)(void);
448c2ecf20Sopenharmony_ci	int   (*kick_cpu)(int nr);
458c2ecf20Sopenharmony_ci	int   (*prepare_cpu)(int nr);
468c2ecf20Sopenharmony_ci	void  (*setup_cpu)(int nr);
478c2ecf20Sopenharmony_ci	void  (*bringup_done)(void);
488c2ecf20Sopenharmony_ci	void  (*take_timebase)(void);
498c2ecf20Sopenharmony_ci	void  (*give_timebase)(void);
508c2ecf20Sopenharmony_ci	int   (*cpu_disable)(void);
518c2ecf20Sopenharmony_ci	void  (*cpu_die)(unsigned int nr);
528c2ecf20Sopenharmony_ci	int   (*cpu_bootable)(unsigned int nr);
538c2ecf20Sopenharmony_ci#ifdef CONFIG_HOTPLUG_CPU
548c2ecf20Sopenharmony_ci	void  (*cpu_offline_self)(void);
558c2ecf20Sopenharmony_ci#endif
568c2ecf20Sopenharmony_ci};
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ciextern int smp_send_nmi_ipi(int cpu, void (*fn)(struct pt_regs *), u64 delay_us);
598c2ecf20Sopenharmony_ciextern int smp_send_safe_nmi_ipi(int cpu, void (*fn)(struct pt_regs *), u64 delay_us);
608c2ecf20Sopenharmony_ciextern void smp_send_debugger_break(void);
618c2ecf20Sopenharmony_ciextern void start_secondary_resume(void);
628c2ecf20Sopenharmony_ciextern void smp_generic_give_timebase(void);
638c2ecf20Sopenharmony_ciextern void smp_generic_take_timebase(void);
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ciDECLARE_PER_CPU(unsigned int, cpu_pvr);
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci#ifdef CONFIG_HOTPLUG_CPU
688c2ecf20Sopenharmony_ciint generic_cpu_disable(void);
698c2ecf20Sopenharmony_civoid generic_cpu_die(unsigned int cpu);
708c2ecf20Sopenharmony_civoid generic_set_cpu_dead(unsigned int cpu);
718c2ecf20Sopenharmony_civoid generic_set_cpu_up(unsigned int cpu);
728c2ecf20Sopenharmony_ciint generic_check_cpu_restart(unsigned int cpu);
738c2ecf20Sopenharmony_ciint is_cpu_dead(unsigned int cpu);
748c2ecf20Sopenharmony_ci#else
758c2ecf20Sopenharmony_ci#define generic_set_cpu_up(i)	do { } while (0)
768c2ecf20Sopenharmony_ci#endif
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC64
798c2ecf20Sopenharmony_ci#define raw_smp_processor_id()	(local_paca->paca_index)
808c2ecf20Sopenharmony_ci#define hard_smp_processor_id() (get_paca()->hw_cpu_id)
818c2ecf20Sopenharmony_ci#else
828c2ecf20Sopenharmony_ci/* 32-bit */
838c2ecf20Sopenharmony_ciextern int smp_hw_index[];
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci/*
868c2ecf20Sopenharmony_ci * This is particularly ugly: it appears we can't actually get the definition
878c2ecf20Sopenharmony_ci * of task_struct here, but we need access to the CPU this task is running on.
888c2ecf20Sopenharmony_ci * Instead of using task_struct we're using _TASK_CPU which is extracted from
898c2ecf20Sopenharmony_ci * asm-offsets.h by kbuild to get the current processor ID.
908c2ecf20Sopenharmony_ci *
918c2ecf20Sopenharmony_ci * This also needs to be safeguarded when building asm-offsets.s because at
928c2ecf20Sopenharmony_ci * that time _TASK_CPU is not defined yet. It could have been guarded by
938c2ecf20Sopenharmony_ci * _TASK_CPU itself, but we want the build to fail if _TASK_CPU is missing
948c2ecf20Sopenharmony_ci * when building something else than asm-offsets.s
958c2ecf20Sopenharmony_ci */
968c2ecf20Sopenharmony_ci#ifdef GENERATING_ASM_OFFSETS
978c2ecf20Sopenharmony_ci#define raw_smp_processor_id()		(0)
988c2ecf20Sopenharmony_ci#else
998c2ecf20Sopenharmony_ci#define raw_smp_processor_id()		(*(unsigned int *)((void *)current + _TASK_CPU))
1008c2ecf20Sopenharmony_ci#endif
1018c2ecf20Sopenharmony_ci#define hard_smp_processor_id() 	(smp_hw_index[smp_processor_id()])
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_cistatic inline int get_hard_smp_processor_id(int cpu)
1048c2ecf20Sopenharmony_ci{
1058c2ecf20Sopenharmony_ci	return smp_hw_index[cpu];
1068c2ecf20Sopenharmony_ci}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic inline void set_hard_smp_processor_id(int cpu, int phys)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	smp_hw_index[cpu] = phys;
1118c2ecf20Sopenharmony_ci}
1128c2ecf20Sopenharmony_ci#endif
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ciDECLARE_PER_CPU(cpumask_var_t, cpu_sibling_map);
1158c2ecf20Sopenharmony_ciDECLARE_PER_CPU(cpumask_var_t, cpu_l2_cache_map);
1168c2ecf20Sopenharmony_ciDECLARE_PER_CPU(cpumask_var_t, cpu_core_map);
1178c2ecf20Sopenharmony_ciDECLARE_PER_CPU(cpumask_var_t, cpu_smallcore_map);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistatic inline struct cpumask *cpu_sibling_mask(int cpu)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	return per_cpu(cpu_sibling_map, cpu);
1228c2ecf20Sopenharmony_ci}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_cistatic inline struct cpumask *cpu_core_mask(int cpu)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci	return per_cpu(cpu_core_map, cpu);
1278c2ecf20Sopenharmony_ci}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_cistatic inline struct cpumask *cpu_l2_cache_mask(int cpu)
1308c2ecf20Sopenharmony_ci{
1318c2ecf20Sopenharmony_ci	return per_cpu(cpu_l2_cache_map, cpu);
1328c2ecf20Sopenharmony_ci}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_cistatic inline struct cpumask *cpu_smallcore_mask(int cpu)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	return per_cpu(cpu_smallcore_map, cpu);
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ciextern int cpu_to_core_id(int cpu);
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ciextern bool has_big_cores;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci#define cpu_smt_mask cpu_smt_mask
1448c2ecf20Sopenharmony_ci#ifdef CONFIG_SCHED_SMT
1458c2ecf20Sopenharmony_cistatic inline const struct cpumask *cpu_smt_mask(int cpu)
1468c2ecf20Sopenharmony_ci{
1478c2ecf20Sopenharmony_ci	if (has_big_cores)
1488c2ecf20Sopenharmony_ci		return per_cpu(cpu_smallcore_map, cpu);
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	return per_cpu(cpu_sibling_map, cpu);
1518c2ecf20Sopenharmony_ci}
1528c2ecf20Sopenharmony_ci#endif /* CONFIG_SCHED_SMT */
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci/* Since OpenPIC has only 4 IPIs, we use slightly different message numbers.
1558c2ecf20Sopenharmony_ci *
1568c2ecf20Sopenharmony_ci * Make sure this matches openpic_request_IPIs in open_pic.c, or what shows up
1578c2ecf20Sopenharmony_ci * in /proc/interrupts will be wrong!!! --Troy */
1588c2ecf20Sopenharmony_ci#define PPC_MSG_CALL_FUNCTION	0
1598c2ecf20Sopenharmony_ci#define PPC_MSG_RESCHEDULE	1
1608c2ecf20Sopenharmony_ci#define PPC_MSG_TICK_BROADCAST	2
1618c2ecf20Sopenharmony_ci#define PPC_MSG_NMI_IPI		3
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci/* This is only used by the powernv kernel */
1648c2ecf20Sopenharmony_ci#define PPC_MSG_RM_HOST_ACTION	4
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci#define NMI_IPI_ALL_OTHERS		-2
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci#ifdef CONFIG_NMI_IPI
1698c2ecf20Sopenharmony_ciextern int smp_handle_nmi_ipi(struct pt_regs *regs);
1708c2ecf20Sopenharmony_ci#else
1718c2ecf20Sopenharmony_cistatic inline int smp_handle_nmi_ipi(struct pt_regs *regs) { return 0; }
1728c2ecf20Sopenharmony_ci#endif
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci/* for irq controllers that have dedicated ipis per message (4) */
1758c2ecf20Sopenharmony_ciextern int smp_request_message_ipi(int virq, int message);
1768c2ecf20Sopenharmony_ciextern const char *smp_ipi_name[];
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci/* for irq controllers with only a single ipi */
1798c2ecf20Sopenharmony_ciextern void smp_muxed_ipi_message_pass(int cpu, int msg);
1808c2ecf20Sopenharmony_ciextern void smp_muxed_ipi_set_message(int cpu, int msg);
1818c2ecf20Sopenharmony_ciextern irqreturn_t smp_ipi_demux(void);
1828c2ecf20Sopenharmony_ciextern irqreturn_t smp_ipi_demux_relaxed(void);
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_civoid smp_init_pSeries(void);
1858c2ecf20Sopenharmony_civoid smp_init_cell(void);
1868c2ecf20Sopenharmony_civoid smp_setup_cpu_maps(void);
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ciextern int __cpu_disable(void);
1898c2ecf20Sopenharmony_ciextern void __cpu_die(unsigned int cpu);
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci#else
1928c2ecf20Sopenharmony_ci/* for UP */
1938c2ecf20Sopenharmony_ci#define hard_smp_processor_id()		get_hard_smp_processor_id(0)
1948c2ecf20Sopenharmony_ci#define smp_setup_cpu_maps()
1958c2ecf20Sopenharmony_cistatic inline void inhibit_secondary_onlining(void) {}
1968c2ecf20Sopenharmony_cistatic inline void uninhibit_secondary_onlining(void) {}
1978c2ecf20Sopenharmony_cistatic inline const struct cpumask *cpu_sibling_mask(int cpu)
1988c2ecf20Sopenharmony_ci{
1998c2ecf20Sopenharmony_ci	return cpumask_of(cpu);
2008c2ecf20Sopenharmony_ci}
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_cistatic inline const struct cpumask *cpu_smallcore_mask(int cpu)
2038c2ecf20Sopenharmony_ci{
2048c2ecf20Sopenharmony_ci	return cpumask_of(cpu);
2058c2ecf20Sopenharmony_ci}
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci#endif /* CONFIG_SMP */
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC64
2108c2ecf20Sopenharmony_cistatic inline int get_hard_smp_processor_id(int cpu)
2118c2ecf20Sopenharmony_ci{
2128c2ecf20Sopenharmony_ci	return paca_ptrs[cpu]->hw_cpu_id;
2138c2ecf20Sopenharmony_ci}
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_cistatic inline void set_hard_smp_processor_id(int cpu, int phys)
2168c2ecf20Sopenharmony_ci{
2178c2ecf20Sopenharmony_ci	paca_ptrs[cpu]->hw_cpu_id = phys;
2188c2ecf20Sopenharmony_ci}
2198c2ecf20Sopenharmony_ci#else
2208c2ecf20Sopenharmony_ci/* 32-bit */
2218c2ecf20Sopenharmony_ci#ifndef CONFIG_SMP
2228c2ecf20Sopenharmony_ciextern int boot_cpuid_phys;
2238c2ecf20Sopenharmony_cistatic inline int get_hard_smp_processor_id(int cpu)
2248c2ecf20Sopenharmony_ci{
2258c2ecf20Sopenharmony_ci	return boot_cpuid_phys;
2268c2ecf20Sopenharmony_ci}
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_cistatic inline void set_hard_smp_processor_id(int cpu, int phys)
2298c2ecf20Sopenharmony_ci{
2308c2ecf20Sopenharmony_ci	boot_cpuid_phys = phys;
2318c2ecf20Sopenharmony_ci}
2328c2ecf20Sopenharmony_ci#endif /* !CONFIG_SMP */
2338c2ecf20Sopenharmony_ci#endif /* !CONFIG_PPC64 */
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci#if defined(CONFIG_PPC64) && (defined(CONFIG_SMP) || defined(CONFIG_KEXEC_CORE))
2368c2ecf20Sopenharmony_ciextern void smp_release_cpus(void);
2378c2ecf20Sopenharmony_ci#else
2388c2ecf20Sopenharmony_cistatic inline void smp_release_cpus(void) { };
2398c2ecf20Sopenharmony_ci#endif
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ciextern int smt_enabled_at_boot;
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ciextern void smp_mpic_probe(void);
2448c2ecf20Sopenharmony_ciextern void smp_mpic_setup_cpu(int cpu);
2458c2ecf20Sopenharmony_ciextern int smp_generic_kick_cpu(int nr);
2468c2ecf20Sopenharmony_ciextern int smp_generic_cpu_bootable(unsigned int nr);
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ciextern void smp_generic_give_timebase(void);
2508c2ecf20Sopenharmony_ciextern void smp_generic_take_timebase(void);
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ciextern struct smp_ops_t *smp_ops;
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ciextern void arch_send_call_function_single_ipi(int cpu);
2558c2ecf20Sopenharmony_ciextern void arch_send_call_function_ipi_mask(const struct cpumask *mask);
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci/* Definitions relative to the secondary CPU spin loop
2588c2ecf20Sopenharmony_ci * and entry point. Not all of them exist on both 32 and
2598c2ecf20Sopenharmony_ci * 64-bit but defining them all here doesn't harm
2608c2ecf20Sopenharmony_ci */
2618c2ecf20Sopenharmony_ciextern void generic_secondary_smp_init(void);
2628c2ecf20Sopenharmony_ciextern unsigned long __secondary_hold_spinloop;
2638c2ecf20Sopenharmony_ciextern unsigned long __secondary_hold_acknowledge;
2648c2ecf20Sopenharmony_ciextern char __secondary_hold;
2658c2ecf20Sopenharmony_ciextern unsigned int booting_thread_hwid;
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ciextern void __early_start(void);
2688c2ecf20Sopenharmony_ci#endif /* __ASSEMBLY__ */
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci#endif /* __KERNEL__ */
2718c2ecf20Sopenharmony_ci#endif /* _ASM_POWERPC_SMP_H) */
272