xref: /openbmc/linux/include/linux/cpu.h (revision b181f7029bd71238ac2754ce7052dffd69432085)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * include/linux/cpu.h - generic cpu definition
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * This is mainly for topological representation. We define the
61da177e4SLinus Torvalds  * basic 'struct cpu' here, which can be embedded in per-arch
71da177e4SLinus Torvalds  * definitions of processors.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Basic handling of the devices is done in drivers/base/cpu.c
101da177e4SLinus Torvalds  *
11611a75e1SRobert P. J. Day  * CPUs are exported via sysfs in the devices/system/cpu
121da177e4SLinus Torvalds  * directory.
131da177e4SLinus Torvalds  */
141da177e4SLinus Torvalds #ifndef _LINUX_CPU_H_
151da177e4SLinus Torvalds #define _LINUX_CPU_H_
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include <linux/node.h>
181da177e4SLinus Torvalds #include <linux/compiler.h>
191da177e4SLinus Torvalds #include <linux/cpumask.h>
20cff7d378SThomas Gleixner #include <linux/cpuhotplug.h>
213f916919SMichael Ellerman #include <linux/cpu_smt.h>
221da177e4SLinus Torvalds 
23313162d0SPaul Gortmaker struct device;
24d1cb9d1aSDavid Miller struct device_node;
253d52943bSSudeep Holla struct attribute_group;
26313162d0SPaul Gortmaker 
271da177e4SLinus Torvalds struct cpu {
281da177e4SLinus Torvalds 	int node_id;		/* The node which contains the CPU */
2972486f1fSSiddha, Suresh B 	int hotpluggable;	/* creates sysfs control file if hotpluggable */
308a25a2fdSKay Sievers 	struct device dev;
311da177e4SLinus Torvalds };
321da177e4SLinus Torvalds 
33cff7d378SThomas Gleixner extern void boot_cpu_init(void);
34b5b1404dSLinus Torvalds extern void boot_cpu_hotplug_init(void);
351777e463SIngo Molnar extern void cpu_init(void);
361777e463SIngo Molnar extern void trap_init(void);
37cff7d378SThomas Gleixner 
3876b67ed9SKAMEZAWA Hiroyuki extern int register_cpu(struct cpu *cpu, int num);
398a25a2fdSKay Sievers extern struct device *get_cpu_device(unsigned cpu);
402987557fSJosh Triplett extern bool cpu_is_hotpluggable(unsigned cpu);
41183912d3SSudeep KarkadaNagesha extern bool arch_match_cpu_phys_id(int cpu, u64 phys_id);
42d1cb9d1aSDavid Miller extern bool arch_find_n_match_cpu_physical_id(struct device_node *cpun,
43d1cb9d1aSDavid Miller 					      int cpu, unsigned int *thread);
440344c6c5SChristian Krafft 
458a25a2fdSKay Sievers extern int cpu_add_dev_attr(struct device_attribute *attr);
468a25a2fdSKay Sievers extern void cpu_remove_dev_attr(struct device_attribute *attr);
470344c6c5SChristian Krafft 
488a25a2fdSKay Sievers extern int cpu_add_dev_attr_group(struct attribute_group *attrs);
498a25a2fdSKay Sievers extern void cpu_remove_dev_attr_group(struct attribute_group *attrs);
500344c6c5SChristian Krafft 
5187590ce6SThomas Gleixner extern ssize_t cpu_show_meltdown(struct device *dev,
5287590ce6SThomas Gleixner 				 struct device_attribute *attr, char *buf);
5387590ce6SThomas Gleixner extern ssize_t cpu_show_spectre_v1(struct device *dev,
5487590ce6SThomas Gleixner 				   struct device_attribute *attr, char *buf);
5587590ce6SThomas Gleixner extern ssize_t cpu_show_spectre_v2(struct device *dev,
5687590ce6SThomas Gleixner 				   struct device_attribute *attr, char *buf);
57c456442cSKonrad Rzeszutek Wilk extern ssize_t cpu_show_spec_store_bypass(struct device *dev,
58c456442cSKonrad Rzeszutek Wilk 					  struct device_attribute *attr, char *buf);
5917dbca11SAndi Kleen extern ssize_t cpu_show_l1tf(struct device *dev,
6017dbca11SAndi Kleen 			     struct device_attribute *attr, char *buf);
618a4b06d3SThomas Gleixner extern ssize_t cpu_show_mds(struct device *dev,
628a4b06d3SThomas Gleixner 			    struct device_attribute *attr, char *buf);
636608b45aSPawan Gupta extern ssize_t cpu_show_tsx_async_abort(struct device *dev,
646608b45aSPawan Gupta 					struct device_attribute *attr,
656608b45aSPawan Gupta 					char *buf);
66db4d30fbSVineela Tummalapalli extern ssize_t cpu_show_itlb_multihit(struct device *dev,
67db4d30fbSVineela Tummalapalli 				      struct device_attribute *attr, char *buf);
682accfa69SGuenter Roeck extern ssize_t cpu_show_srbds(struct device *dev, struct device_attribute *attr, char *buf);
698d50cdf8SPawan Gupta extern ssize_t cpu_show_mmio_stale_data(struct device *dev,
708d50cdf8SPawan Gupta 					struct device_attribute *attr,
718d50cdf8SPawan Gupta 					char *buf);
726b80b59bSAlexandre Chartre extern ssize_t cpu_show_retbleed(struct device *dev,
736b80b59bSAlexandre Chartre 				 struct device_attribute *attr, char *buf);
74fb3bd914SBorislav Petkov (AMD) extern ssize_t cpu_show_spec_rstack_overflow(struct device *dev,
75fb3bd914SBorislav Petkov (AMD) 					     struct device_attribute *attr, char *buf);
76a57c27c7SArnd Bergmann extern ssize_t cpu_show_gds(struct device *dev,
77a57c27c7SArnd Bergmann 			    struct device_attribute *attr, char *buf);
7877018fb9SPawan Gupta extern ssize_t cpu_show_reg_file_data_sampling(struct device *dev,
7977018fb9SPawan Gupta 					       struct device_attribute *attr, char *buf);
8087590ce6SThomas Gleixner 
818db14860SNicolas Iooss extern __printf(4, 5)
828db14860SNicolas Iooss struct device *cpu_device_create(struct device *parent, void *drvdata,
833d52943bSSudeep Holla 				 const struct attribute_group **groups,
843d52943bSSudeep Holla 				 const char *fmt, ...);
85c4dd854fSRussell King (Oracle) extern int arch_register_cpu(int cpu);
86c4dd854fSRussell King (Oracle) extern void arch_unregister_cpu(int cpu);
871da177e4SLinus Torvalds #ifdef CONFIG_HOTPLUG_CPU
8876b67ed9SKAMEZAWA Hiroyuki extern void unregister_cpu(struct cpu *cpu);
8912633e80SNathan Fontenot extern ssize_t arch_cpu_probe(const char *, size_t);
9012633e80SNathan Fontenot extern ssize_t arch_cpu_release(const char *, size_t);
911da177e4SLinus Torvalds #endif
921da177e4SLinus Torvalds 
93f4c09f87SThomas Gleixner /*
94f4c09f87SThomas Gleixner  * These states are not related to the core CPU hotplug mechanism. They are
95f4c09f87SThomas Gleixner  * used by various (sub)architectures to track internal state
9680f1ff97SAmerigo Wang  */
97f4c09f87SThomas Gleixner #define CPU_ONLINE		0x0002 /* CPU is up */
98f4c09f87SThomas Gleixner #define CPU_UP_PREPARE		0x0003 /* CPU coming up */
99f4c09f87SThomas Gleixner #define CPU_DEAD		0x0007 /* CPU dead */
100f4c09f87SThomas Gleixner #define CPU_DEAD_FROZEN		0x0008 /* CPU timed out on unplug */
101f4c09f87SThomas Gleixner #define CPU_POST_DEAD		0x0009 /* CPU successfully unplugged */
102f4c09f87SThomas Gleixner #define CPU_BROKEN		0x000B /* CPU did not die properly */
10380f1ff97SAmerigo Wang 
1041da177e4SLinus Torvalds #ifdef CONFIG_SMP
105090e77c3SThomas Gleixner extern bool cpuhp_tasks_frozen;
10693ef1429SQais Yousef int add_cpu(unsigned int cpu);
10733c3736eSQais Yousef int cpu_device_up(struct device *dev);
108e545a614SManfred Spraul void notify_cpu_starting(unsigned int cpu);
1093da1c84cSOleg Nesterov extern void cpu_maps_update_begin(void);
1103da1c84cSOleg Nesterov extern void cpu_maps_update_done(void);
111d720f986SQais Yousef int bringup_hibernate_cpu(unsigned int sleep_cpu);
112b99a2659SQais Yousef void bringup_nonboot_cpus(unsigned int setup_max_cpus);
113d0d23b54SIngo Molnar 
1143da1c84cSOleg Nesterov #else	/* CONFIG_SMP */
115090e77c3SThomas Gleixner #define cpuhp_tasks_frozen	0
1161da177e4SLinus Torvalds 
cpu_maps_update_begin(void)1173da1c84cSOleg Nesterov static inline void cpu_maps_update_begin(void)
1183da1c84cSOleg Nesterov {
1193da1c84cSOleg Nesterov }
1203da1c84cSOleg Nesterov 
cpu_maps_update_done(void)1213da1c84cSOleg Nesterov static inline void cpu_maps_update_done(void)
1223da1c84cSOleg Nesterov {
1233da1c84cSOleg Nesterov }
1243da1c84cSOleg Nesterov 
add_cpu(unsigned int cpu)12551f24030SShuo Liu static inline int add_cpu(unsigned int cpu) { return 0;}
12651f24030SShuo Liu 
1271da177e4SLinus Torvalds #endif /* CONFIG_SMP */
1288a25a2fdSKay Sievers extern struct bus_type cpu_subsys;
1291da177e4SLinus Torvalds 
13043759fe5SFrederic Weisbecker extern int lockdep_is_cpus_held(void);
13143759fe5SFrederic Weisbecker 
1321da177e4SLinus Torvalds #ifdef CONFIG_HOTPLUG_CPU
1338f553c49SThomas Gleixner extern void cpus_write_lock(void);
1348f553c49SThomas Gleixner extern void cpus_write_unlock(void);
1358f553c49SThomas Gleixner extern void cpus_read_lock(void);
1368f553c49SThomas Gleixner extern void cpus_read_unlock(void);
1376f4ceee9SWaiman Long extern int  cpus_read_trylock(void);
138fc8dffd3SThomas Gleixner extern void lockdep_assert_cpus_held(void);
13916e53dbfSSrivatsa S. Bhat extern void cpu_hotplug_disable(void);
14016e53dbfSSrivatsa S. Bhat extern void cpu_hotplug_enable(void);
141cb79295eSAnton Vorontsov void clear_tasks_mm_cpumask(int cpu);
14293ef1429SQais Yousef int remove_cpu(unsigned int cpu);
14333c3736eSQais Yousef int cpu_device_down(struct device *dev);
1440441a559SQais Yousef extern void smp_shutdown_nonboot_cpus(unsigned int primary_cpu);
145f7dff2b1SGautham R Shenoy 
146f7dff2b1SGautham R Shenoy #else /* CONFIG_HOTPLUG_CPU */
147f7dff2b1SGautham R Shenoy 
cpus_write_lock(void)1488f553c49SThomas Gleixner static inline void cpus_write_lock(void) { }
cpus_write_unlock(void)1498f553c49SThomas Gleixner static inline void cpus_write_unlock(void) { }
cpus_read_lock(void)1508f553c49SThomas Gleixner static inline void cpus_read_lock(void) { }
cpus_read_unlock(void)1518f553c49SThomas Gleixner static inline void cpus_read_unlock(void) { }
cpus_read_trylock(void)1526f4ceee9SWaiman Long static inline int  cpus_read_trylock(void) { return true; }
lockdep_assert_cpus_held(void)153ade3f680SThomas Gleixner static inline void lockdep_assert_cpus_held(void) { }
cpu_hotplug_disable(void)1548f553c49SThomas Gleixner static inline void cpu_hotplug_disable(void) { }
cpu_hotplug_enable(void)1558f553c49SThomas Gleixner static inline void cpu_hotplug_enable(void) { }
remove_cpu(unsigned int cpu)15651f24030SShuo Liu static inline int remove_cpu(unsigned int cpu) { return -EPERM; }
smp_shutdown_nonboot_cpus(unsigned int primary_cpu)1570441a559SQais Yousef static inline void smp_shutdown_nonboot_cpus(unsigned int primary_cpu) { }
1588f553c49SThomas Gleixner #endif	/* !CONFIG_HOTPLUG_CPU */
1598f553c49SThomas Gleixner 
160c4c2f7e6SPeter Zijlstra DEFINE_LOCK_GUARD_0(cpus_read_lock, cpus_read_lock(), cpus_read_unlock())
161c4c2f7e6SPeter Zijlstra 
162f3de4be9SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP_SMP
163fb7fb84aSQais Yousef extern int freeze_secondary_cpus(int primary);
16456555855SQais Yousef extern void thaw_secondary_cpus(void);
1652f1a6fbbSNicholas Piggin 
suspend_disable_secondary_cpus(void)1662f1a6fbbSNicholas Piggin static inline int suspend_disable_secondary_cpus(void)
1672f1a6fbbSNicholas Piggin {
1689ca12ac0SNicholas Piggin 	int cpu = 0;
1699ca12ac0SNicholas Piggin 
1709ca12ac0SNicholas Piggin 	if (IS_ENABLED(CONFIG_PM_SLEEP_SMP_NONZERO_CPU))
1719ca12ac0SNicholas Piggin 		cpu = -1;
1729ca12ac0SNicholas Piggin 
1739ca12ac0SNicholas Piggin 	return freeze_secondary_cpus(cpu);
1742f1a6fbbSNicholas Piggin }
suspend_enable_secondary_cpus(void)1752f1a6fbbSNicholas Piggin static inline void suspend_enable_secondary_cpus(void)
1762f1a6fbbSNicholas Piggin {
17756555855SQais Yousef 	return thaw_secondary_cpus();
1782f1a6fbbSNicholas Piggin }
1792f1a6fbbSNicholas Piggin 
180f3de4be9SRafael J. Wysocki #else /* !CONFIG_PM_SLEEP_SMP */
18156555855SQais Yousef static inline void thaw_secondary_cpus(void) {}
1822f1a6fbbSNicholas Piggin static inline int suspend_disable_secondary_cpus(void) { return 0; }
1832f1a6fbbSNicholas Piggin static inline void suspend_enable_secondary_cpus(void) { }
184f3de4be9SRafael J. Wysocki #endif /* !CONFIG_PM_SLEEP_SMP */
185e3920fb4SRafael J. Wysocki 
186d4e5268aSPeter Zijlstra void __noreturn cpu_startup_entry(enum cpuhp_state state);
187a1a04ec3SThomas Gleixner 
188d1669912SThomas Gleixner void cpu_idle_poll_ctrl(bool enable);
189d1669912SThomas Gleixner 
1906727ad9eSChris Metcalf bool cpu_in_idle(unsigned long pc);
1916727ad9eSChris Metcalf 
192d1669912SThomas Gleixner void arch_cpu_idle(void);
193d1669912SThomas Gleixner void arch_cpu_idle_prepare(void);
194d1669912SThomas Gleixner void arch_cpu_idle_enter(void);
195d1669912SThomas Gleixner void arch_cpu_idle_exit(void);
196071c44e4SJosh Poimboeuf void __noreturn arch_cpu_idle_dead(void);
197d1669912SThomas Gleixner 
1987725acaaSThomas Gleixner #ifdef CONFIG_ARCH_HAS_CPU_FINALIZE_INIT
1997725acaaSThomas Gleixner void arch_cpu_finalize_init(void);
2007725acaaSThomas Gleixner #else
arch_cpu_finalize_init(void)2017725acaaSThomas Gleixner static inline void arch_cpu_finalize_init(void) { }
2027725acaaSThomas Gleixner #endif
2037725acaaSThomas Gleixner 
204c55b51a0SDaniel Lezcano void play_idle_precise(u64 duration_ns, u64 latency_ns);
205c55b51a0SDaniel Lezcano 
play_idle(unsigned long duration_us)206c55b51a0SDaniel Lezcano static inline void play_idle(unsigned long duration_us)
207c55b51a0SDaniel Lezcano {
208c55b51a0SDaniel Lezcano 	play_idle_precise(duration_us * NSEC_PER_USEC, U64_MAX);
209c55b51a0SDaniel Lezcano }
210c1de45caSPeter Zijlstra 
2118038dad7SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU
212e69aab13SThomas Gleixner void cpuhp_report_idle_dead(void);
213e69aab13SThomas Gleixner #else
cpuhp_report_idle_dead(void)214e69aab13SThomas Gleixner static inline void cpuhp_report_idle_dead(void) { }
2158038dad7SPaul E. McKenney #endif /* #ifdef CONFIG_HOTPLUG_CPU */
2168038dad7SPaul E. McKenney 
217*976b74faSSean Christopherson #ifdef CONFIG_CPU_MITIGATIONS
218731dc9dfSTyler Hicks extern bool cpu_mitigations_off(void);
219731dc9dfSTyler Hicks extern bool cpu_mitigations_auto_nosmt(void);
220*976b74faSSean Christopherson #else
cpu_mitigations_off(void)221*976b74faSSean Christopherson static inline bool cpu_mitigations_off(void)
222*976b74faSSean Christopherson {
223*976b74faSSean Christopherson 	return true;
224*976b74faSSean Christopherson }
cpu_mitigations_auto_nosmt(void)225*976b74faSSean Christopherson static inline bool cpu_mitigations_auto_nosmt(void)
226*976b74faSSean Christopherson {
227*976b74faSSean Christopherson 	return false;
228*976b74faSSean Christopherson }
229*976b74faSSean Christopherson #endif
23098af8452SJosh Poimboeuf 
2311da177e4SLinus Torvalds #endif /* _LINUX_CPU_H_ */
232