smp.h 7.6 KB

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