• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_SMP_H
3 #define __LINUX_SMP_H
4 
5 /*
6  *	Generic SMP support
7  *		Alan Cox. <alan@redhat.com>
8  */
9 
10 #include <linux/errno.h>
11 #include <linux/types.h>
12 #include <linux/list.h>
13 #include <linux/cpumask.h>
14 #include <linux/init.h>
15 #include <linux/smp_types.h>
16 
17 typedef void (*smp_call_func_t)(void *info);
18 typedef bool (*smp_cond_func_t)(int cpu, void *info);
19 
20 /*
21  * structure shares (partial) layout with struct irq_work
22  */
23 struct __call_single_data {
24 	union {
25 		struct __call_single_node node;
26 		struct {
27 			struct llist_node llist;
28 			unsigned int flags;
29 #ifdef CONFIG_64BIT
30 			u16 src, dst;
31 #endif
32 		};
33 	};
34 	smp_call_func_t func;
35 	void *info;
36 };
37 
38 /* Use __aligned() to avoid to use 2 cache lines for 1 csd */
39 typedef struct __call_single_data call_single_data_t
40 	__aligned(sizeof(struct __call_single_data));
41 
42 /*
43  * Enqueue a llist_node on the call_single_queue; be very careful, read
44  * flush_smp_call_function_queue() in detail.
45  */
46 extern void __smp_call_single_queue(int cpu, struct llist_node *node);
47 
48 /* total number of cpus in this system (may exceed NR_CPUS) */
49 extern unsigned int total_cpus;
50 
51 int smp_call_function_single(int cpuid, smp_call_func_t func, void *info,
52 			     int wait);
53 
54 /*
55  * Call a function on all processors
56  */
57 void on_each_cpu(smp_call_func_t func, void *info, int wait);
58 
59 /*
60  * Call a function on processors specified by mask, which might include
61  * the local one.
62  */
63 void on_each_cpu_mask(const struct cpumask *mask, smp_call_func_t func,
64 		void *info, bool wait);
65 
66 /*
67  * Call a function on each processor for which the supplied function
68  * cond_func returns a positive value. This may include the local
69  * processor.
70  */
71 void on_each_cpu_cond(smp_cond_func_t cond_func, smp_call_func_t func,
72 		      void *info, bool wait);
73 
74 void on_each_cpu_cond_mask(smp_cond_func_t cond_func, smp_call_func_t func,
75 			   void *info, bool wait, const struct cpumask *mask);
76 
77 int smp_call_function_single_async(int cpu, struct __call_single_data *csd);
78 
79 #ifdef CONFIG_SMP
80 
81 #include <linux/preempt.h>
82 #include <linux/kernel.h>
83 #include <linux/compiler.h>
84 #include <linux/thread_info.h>
85 #include <asm/smp.h>
86 
87 /*
88  * main cross-CPU interfaces, handles INIT, TLB flush, STOP, etc.
89  * (defined in asm header):
90  */
91 
92 /*
93  * stops all CPUs but the current one:
94  */
95 extern void smp_send_stop(void);
96 
97 /*
98  * sends a 'reschedule' event to another CPU:
99  */
100 extern void smp_send_reschedule(int cpu);
101 
102 
103 /*
104  * Prepare machine for booting other CPUs.
105  */
106 extern void smp_prepare_cpus(unsigned int max_cpus);
107 
108 /*
109  * Bring a CPU up
110  */
111 extern int __cpu_up(unsigned int cpunum, struct task_struct *tidle);
112 
113 /*
114  * Final polishing of CPUs
115  */
116 extern void smp_cpus_done(unsigned int max_cpus);
117 
118 /*
119  * Call a function on all other processors
120  */
121 void smp_call_function(smp_call_func_t func, void *info, int wait);
122 void smp_call_function_many(const struct cpumask *mask,
123 			    smp_call_func_t func, void *info, bool wait);
124 
125 int smp_call_function_any(const struct cpumask *mask,
126 			  smp_call_func_t func, void *info, int wait);
127 
128 void kick_all_cpus_sync(void);
129 void wake_up_all_idle_cpus(void);
130 void wake_up_all_online_idle_cpus(void);
131 
132 /*
133  * Generic and arch helpers
134  */
135 void __init call_function_init(void);
136 void generic_smp_call_function_single_interrupt(void);
137 #define generic_smp_call_function_interrupt \
138 	generic_smp_call_function_single_interrupt
139 
140 /*
141  * Mark the boot cpu "online" so that it can call console drivers in
142  * printk() and can access its per-cpu storage.
143  */
144 void smp_prepare_boot_cpu(void);
145 
146 extern unsigned int setup_max_cpus;
147 extern void __init setup_nr_cpu_ids(void);
148 extern void __init smp_init(void);
149 
150 extern int __boot_cpu_id;
151 
get_boot_cpu_id(void)152 static inline int get_boot_cpu_id(void)
153 {
154 	return __boot_cpu_id;
155 }
156 
157 #else /* !SMP */
158 
smp_send_stop(void)159 static inline void smp_send_stop(void) { }
160 
161 /*
162  *	These macros fold the SMP functionality into a single CPU system
163  */
164 #define raw_smp_processor_id()			0
up_smp_call_function(smp_call_func_t func,void * info)165 static inline void up_smp_call_function(smp_call_func_t func, void *info)
166 {
167 }
168 #define smp_call_function(func, info, wait) \
169 			(up_smp_call_function(func, info))
170 
smp_send_reschedule(int cpu)171 static inline void smp_send_reschedule(int cpu) { }
172 #define smp_prepare_boot_cpu()			do {} while (0)
173 #define smp_call_function_many(mask, func, info, wait) \
174 			(up_smp_call_function(func, info))
call_function_init(void)175 static inline void call_function_init(void) { }
176 
177 static inline int
smp_call_function_any(const struct cpumask * mask,smp_call_func_t func,void * info,int wait)178 smp_call_function_any(const struct cpumask *mask, smp_call_func_t func,
179 		      void *info, int wait)
180 {
181 	return smp_call_function_single(0, func, info, wait);
182 }
183 
kick_all_cpus_sync(void)184 static inline void kick_all_cpus_sync(void) {  }
wake_up_all_idle_cpus(void)185 static inline void wake_up_all_idle_cpus(void) {  }
wake_up_all_online_idle_cpus(void)186 static inline void wake_up_all_online_idle_cpus(void) {  }
187 
188 #ifdef CONFIG_UP_LATE_INIT
189 extern void __init up_late_init(void);
smp_init(void)190 static inline void smp_init(void) { up_late_init(); }
191 #else
smp_init(void)192 static inline void smp_init(void) { }
193 #endif
194 
get_boot_cpu_id(void)195 static inline int get_boot_cpu_id(void)
196 {
197 	return 0;
198 }
199 
200 #endif /* !SMP */
201 
202 /**
203  * raw_processor_id() - get the current (unstable) CPU id
204  *
205  * For then you know what you are doing and need an unstable
206  * CPU id.
207  */
208 
209 /**
210  * smp_processor_id() - get the current (stable) CPU id
211  *
212  * This is the normal accessor to the CPU id and should be used
213  * whenever possible.
214  *
215  * The CPU id is stable when:
216  *
217  *  - IRQs are disabled;
218  *  - preemption is disabled;
219  *  - the task is CPU affine.
220  *
221  * When CONFIG_DEBUG_PREEMPT; we verify these assumption and WARN
222  * when smp_processor_id() is used when the CPU id is not stable.
223  */
224 
225 /*
226  * Allow the architecture to differentiate between a stable and unstable read.
227  * For example, x86 uses an IRQ-safe asm-volatile read for the unstable but a
228  * regular asm read for the stable.
229  */
230 #ifndef __smp_processor_id
231 #define __smp_processor_id(x) raw_smp_processor_id(x)
232 #endif
233 
234 #ifdef CONFIG_DEBUG_PREEMPT
235   extern unsigned int debug_smp_processor_id(void);
236 # define smp_processor_id() debug_smp_processor_id()
237 #else
238 # define smp_processor_id() __smp_processor_id()
239 #endif
240 
241 #define get_cpu()		({ preempt_disable(); __smp_processor_id(); })
242 #define put_cpu()		preempt_enable()
243 
244 /*
245  * Callback to arch code if there's nosmp or maxcpus=0 on the
246  * boot command line:
247  */
248 extern void arch_disable_smp_support(void);
249 
250 extern void arch_thaw_secondary_cpus_begin(void);
251 extern void arch_thaw_secondary_cpus_end(void);
252 
253 void smp_setup_processor_id(void);
254 
255 int smp_call_on_cpu(unsigned int cpu, int (*func)(void *), void *par,
256 		    bool phys);
257 
258 /* SMP core functions */
259 int smpcfd_prepare_cpu(unsigned int cpu);
260 int smpcfd_dead_cpu(unsigned int cpu);
261 int smpcfd_dying_cpu(unsigned int cpu);
262 
263 #endif /* __LINUX_SMP_H */
264