cpu.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * include/linux/cpu.h - generic cpu definition
  3. *
  4. * This is mainly for topological representation. We define the
  5. * basic 'struct cpu' here, which can be embedded in per-arch
  6. * definitions of processors.
  7. *
  8. * Basic handling of the devices is done in drivers/base/cpu.c
  9. *
  10. * CPUs are exported via sysfs in the devices/system/cpu
  11. * directory.
  12. */
  13. #ifndef _LINUX_CPU_H_
  14. #define _LINUX_CPU_H_
  15. #include <linux/node.h>
  16. #include <linux/compiler.h>
  17. #include <linux/cpumask.h>
  18. #include <linux/cpuhotplug.h>
  19. struct device;
  20. struct device_node;
  21. struct attribute_group;
  22. struct cpu {
  23. int node_id; /* The node which contains the CPU */
  24. int hotpluggable; /* creates sysfs control file if hotpluggable */
  25. struct device dev;
  26. };
  27. extern void boot_cpu_init(void);
  28. extern void boot_cpu_state_init(void);
  29. extern int register_cpu(struct cpu *cpu, int num);
  30. extern struct device *get_cpu_device(unsigned cpu);
  31. extern bool cpu_is_hotpluggable(unsigned cpu);
  32. extern bool arch_match_cpu_phys_id(int cpu, u64 phys_id);
  33. extern bool arch_find_n_match_cpu_physical_id(struct device_node *cpun,
  34. int cpu, unsigned int *thread);
  35. extern int cpu_add_dev_attr(struct device_attribute *attr);
  36. extern void cpu_remove_dev_attr(struct device_attribute *attr);
  37. extern int cpu_add_dev_attr_group(struct attribute_group *attrs);
  38. extern void cpu_remove_dev_attr_group(struct attribute_group *attrs);
  39. extern ssize_t cpu_show_meltdown(struct device *dev,
  40. struct device_attribute *attr, char *buf);
  41. extern ssize_t cpu_show_spectre_v1(struct device *dev,
  42. struct device_attribute *attr, char *buf);
  43. extern ssize_t cpu_show_spectre_v2(struct device *dev,
  44. struct device_attribute *attr, char *buf);
  45. extern ssize_t cpu_show_spec_store_bypass(struct device *dev,
  46. struct device_attribute *attr, char *buf);
  47. extern __printf(4, 5)
  48. struct device *cpu_device_create(struct device *parent, void *drvdata,
  49. const struct attribute_group **groups,
  50. const char *fmt, ...);
  51. #ifdef CONFIG_HOTPLUG_CPU
  52. extern void unregister_cpu(struct cpu *cpu);
  53. extern ssize_t arch_cpu_probe(const char *, size_t);
  54. extern ssize_t arch_cpu_release(const char *, size_t);
  55. #endif
  56. struct notifier_block;
  57. #define CPU_ONLINE 0x0002 /* CPU (unsigned)v is up */
  58. #define CPU_UP_PREPARE 0x0003 /* CPU (unsigned)v coming up */
  59. #define CPU_UP_CANCELED 0x0004 /* CPU (unsigned)v NOT coming up */
  60. #define CPU_DOWN_PREPARE 0x0005 /* CPU (unsigned)v going down */
  61. #define CPU_DOWN_FAILED 0x0006 /* CPU (unsigned)v NOT going down */
  62. #define CPU_DEAD 0x0007 /* CPU (unsigned)v dead */
  63. #define CPU_POST_DEAD 0x0009 /* CPU (unsigned)v dead, cpu_hotplug
  64. * lock is dropped */
  65. #define CPU_BROKEN 0x000B /* CPU (unsigned)v did not die properly,
  66. * perhaps due to preemption. */
  67. /* Used for CPU hotplug events occurring while tasks are frozen due to a suspend
  68. * operation in progress
  69. */
  70. #define CPU_TASKS_FROZEN 0x0010
  71. #define CPU_ONLINE_FROZEN (CPU_ONLINE | CPU_TASKS_FROZEN)
  72. #define CPU_UP_PREPARE_FROZEN (CPU_UP_PREPARE | CPU_TASKS_FROZEN)
  73. #define CPU_UP_CANCELED_FROZEN (CPU_UP_CANCELED | CPU_TASKS_FROZEN)
  74. #define CPU_DOWN_PREPARE_FROZEN (CPU_DOWN_PREPARE | CPU_TASKS_FROZEN)
  75. #define CPU_DOWN_FAILED_FROZEN (CPU_DOWN_FAILED | CPU_TASKS_FROZEN)
  76. #define CPU_DEAD_FROZEN (CPU_DEAD | CPU_TASKS_FROZEN)
  77. #ifdef CONFIG_SMP
  78. extern bool cpuhp_tasks_frozen;
  79. /* Need to know about CPUs going up/down? */
  80. #if defined(CONFIG_HOTPLUG_CPU) || !defined(MODULE)
  81. #define cpu_notifier(fn, pri) { \
  82. static struct notifier_block fn##_nb = \
  83. { .notifier_call = fn, .priority = pri }; \
  84. register_cpu_notifier(&fn##_nb); \
  85. }
  86. #define __cpu_notifier(fn, pri) { \
  87. static struct notifier_block fn##_nb = \
  88. { .notifier_call = fn, .priority = pri }; \
  89. __register_cpu_notifier(&fn##_nb); \
  90. }
  91. extern int register_cpu_notifier(struct notifier_block *nb);
  92. extern int __register_cpu_notifier(struct notifier_block *nb);
  93. extern void unregister_cpu_notifier(struct notifier_block *nb);
  94. extern void __unregister_cpu_notifier(struct notifier_block *nb);
  95. #else /* #if defined(CONFIG_HOTPLUG_CPU) || !defined(MODULE) */
  96. #define cpu_notifier(fn, pri) do { (void)(fn); } while (0)
  97. #define __cpu_notifier(fn, pri) do { (void)(fn); } while (0)
  98. static inline int register_cpu_notifier(struct notifier_block *nb)
  99. {
  100. return 0;
  101. }
  102. static inline int __register_cpu_notifier(struct notifier_block *nb)
  103. {
  104. return 0;
  105. }
  106. static inline void unregister_cpu_notifier(struct notifier_block *nb)
  107. {
  108. }
  109. static inline void __unregister_cpu_notifier(struct notifier_block *nb)
  110. {
  111. }
  112. #endif
  113. int cpu_up(unsigned int cpu);
  114. void notify_cpu_starting(unsigned int cpu);
  115. extern void cpu_maps_update_begin(void);
  116. extern void cpu_maps_update_done(void);
  117. #define cpu_notifier_register_begin cpu_maps_update_begin
  118. #define cpu_notifier_register_done cpu_maps_update_done
  119. #else /* CONFIG_SMP */
  120. #define cpuhp_tasks_frozen 0
  121. #define cpu_notifier(fn, pri) do { (void)(fn); } while (0)
  122. #define __cpu_notifier(fn, pri) do { (void)(fn); } while (0)
  123. static inline int register_cpu_notifier(struct notifier_block *nb)
  124. {
  125. return 0;
  126. }
  127. static inline int __register_cpu_notifier(struct notifier_block *nb)
  128. {
  129. return 0;
  130. }
  131. static inline void unregister_cpu_notifier(struct notifier_block *nb)
  132. {
  133. }
  134. static inline void __unregister_cpu_notifier(struct notifier_block *nb)
  135. {
  136. }
  137. static inline void cpu_maps_update_begin(void)
  138. {
  139. }
  140. static inline void cpu_maps_update_done(void)
  141. {
  142. }
  143. static inline void cpu_notifier_register_begin(void)
  144. {
  145. }
  146. static inline void cpu_notifier_register_done(void)
  147. {
  148. }
  149. #endif /* CONFIG_SMP */
  150. extern struct bus_type cpu_subsys;
  151. #ifdef CONFIG_HOTPLUG_CPU
  152. /* Stop CPUs going up and down. */
  153. extern void cpu_hotplug_begin(void);
  154. extern void cpu_hotplug_done(void);
  155. extern void get_online_cpus(void);
  156. extern void put_online_cpus(void);
  157. extern void cpu_hotplug_disable(void);
  158. extern void cpu_hotplug_enable(void);
  159. #define hotcpu_notifier(fn, pri) cpu_notifier(fn, pri)
  160. #define __hotcpu_notifier(fn, pri) __cpu_notifier(fn, pri)
  161. #define register_hotcpu_notifier(nb) register_cpu_notifier(nb)
  162. #define __register_hotcpu_notifier(nb) __register_cpu_notifier(nb)
  163. #define unregister_hotcpu_notifier(nb) unregister_cpu_notifier(nb)
  164. #define __unregister_hotcpu_notifier(nb) __unregister_cpu_notifier(nb)
  165. void clear_tasks_mm_cpumask(int cpu);
  166. int cpu_down(unsigned int cpu);
  167. #else /* CONFIG_HOTPLUG_CPU */
  168. static inline void cpu_hotplug_begin(void) {}
  169. static inline void cpu_hotplug_done(void) {}
  170. #define get_online_cpus() do { } while (0)
  171. #define put_online_cpus() do { } while (0)
  172. #define cpu_hotplug_disable() do { } while (0)
  173. #define cpu_hotplug_enable() do { } while (0)
  174. #define hotcpu_notifier(fn, pri) do { (void)(fn); } while (0)
  175. #define __hotcpu_notifier(fn, pri) do { (void)(fn); } while (0)
  176. /* These aren't inline functions due to a GCC bug. */
  177. #define register_hotcpu_notifier(nb) ({ (void)(nb); 0; })
  178. #define __register_hotcpu_notifier(nb) ({ (void)(nb); 0; })
  179. #define unregister_hotcpu_notifier(nb) ({ (void)(nb); })
  180. #define __unregister_hotcpu_notifier(nb) ({ (void)(nb); })
  181. #endif /* CONFIG_HOTPLUG_CPU */
  182. #ifdef CONFIG_PM_SLEEP_SMP
  183. extern int freeze_secondary_cpus(int primary);
  184. static inline int disable_nonboot_cpus(void)
  185. {
  186. return freeze_secondary_cpus(0);
  187. }
  188. extern void enable_nonboot_cpus(void);
  189. #else /* !CONFIG_PM_SLEEP_SMP */
  190. static inline int disable_nonboot_cpus(void) { return 0; }
  191. static inline void enable_nonboot_cpus(void) {}
  192. #endif /* !CONFIG_PM_SLEEP_SMP */
  193. void cpu_startup_entry(enum cpuhp_state state);
  194. void cpu_idle_poll_ctrl(bool enable);
  195. /* Attach to any functions which should be considered cpuidle. */
  196. #define __cpuidle __attribute__((__section__(".cpuidle.text")))
  197. bool cpu_in_idle(unsigned long pc);
  198. void arch_cpu_idle(void);
  199. void arch_cpu_idle_prepare(void);
  200. void arch_cpu_idle_enter(void);
  201. void arch_cpu_idle_exit(void);
  202. void arch_cpu_idle_dead(void);
  203. int cpu_report_state(int cpu);
  204. int cpu_check_up_prepare(int cpu);
  205. void cpu_set_state_online(int cpu);
  206. #ifdef CONFIG_HOTPLUG_CPU
  207. bool cpu_wait_death(unsigned int cpu, int seconds);
  208. bool cpu_report_death(void);
  209. void cpuhp_report_idle_dead(void);
  210. #else
  211. static inline void cpuhp_report_idle_dead(void) { }
  212. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  213. #endif /* _LINUX_CPU_H_ */