smp.h 6.4 KB

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