cpu.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * include/linux/cpu.h - generic cpu definition
  4. *
  5. * This is mainly for topological representation. We define the
  6. * basic 'struct cpu' here, which can be embedded in per-arch
  7. * definitions of processors.
  8. *
  9. * Basic handling of the devices is done in drivers/base/cpu.c
  10. *
  11. * CPUs are exported via sysfs in the devices/system/cpu
  12. * directory.
  13. */
  14. #ifndef _LINUX_CPU_H_
  15. #define _LINUX_CPU_H_
  16. #include <linux/node.h>
  17. #include <linux/compiler.h>
  18. #include <linux/cpumask.h>
  19. #include <linux/cpuhotplug.h>
  20. struct device;
  21. struct device_node;
  22. struct attribute_group;
  23. struct cpu {
  24. int node_id; /* The node which contains the CPU */
  25. int hotpluggable; /* creates sysfs control file if hotpluggable */
  26. struct device dev;
  27. };
  28. extern void boot_cpu_init(void);
  29. extern void boot_cpu_hotplug_init(void);
  30. extern void cpu_init(void);
  31. extern void trap_init(void);
  32. extern int register_cpu(struct cpu *cpu, int num);
  33. extern struct device *get_cpu_device(unsigned cpu);
  34. extern bool cpu_is_hotpluggable(unsigned cpu);
  35. extern bool arch_match_cpu_phys_id(int cpu, u64 phys_id);
  36. extern bool arch_find_n_match_cpu_physical_id(struct device_node *cpun,
  37. int cpu, unsigned int *thread);
  38. extern int cpu_add_dev_attr(struct device_attribute *attr);
  39. extern void cpu_remove_dev_attr(struct device_attribute *attr);
  40. extern int cpu_add_dev_attr_group(struct attribute_group *attrs);
  41. extern void cpu_remove_dev_attr_group(struct attribute_group *attrs);
  42. extern ssize_t cpu_show_meltdown(struct device *dev,
  43. struct device_attribute *attr, char *buf);
  44. extern ssize_t cpu_show_spectre_v1(struct device *dev,
  45. struct device_attribute *attr, char *buf);
  46. extern ssize_t cpu_show_spectre_v2(struct device *dev,
  47. struct device_attribute *attr, char *buf);
  48. extern ssize_t cpu_show_spec_store_bypass(struct device *dev,
  49. struct device_attribute *attr, char *buf);
  50. extern ssize_t cpu_show_l1tf(struct device *dev,
  51. struct device_attribute *attr, char *buf);
  52. extern ssize_t cpu_show_mds(struct device *dev,
  53. struct device_attribute *attr, char *buf);
  54. extern ssize_t cpu_show_tsx_async_abort(struct device *dev,
  55. struct device_attribute *attr,
  56. char *buf);
  57. extern ssize_t cpu_show_itlb_multihit(struct device *dev,
  58. struct device_attribute *attr, char *buf);
  59. extern ssize_t cpu_show_srbds(struct device *dev, struct device_attribute *attr, char *buf);
  60. extern ssize_t cpu_show_mmio_stale_data(struct device *dev,
  61. struct device_attribute *attr,
  62. char *buf);
  63. extern ssize_t cpu_show_retbleed(struct device *dev,
  64. struct device_attribute *attr, char *buf);
  65. extern ssize_t cpu_show_spec_rstack_overflow(struct device *dev,
  66. struct device_attribute *attr, char *buf);
  67. extern ssize_t cpu_show_gds(struct device *dev,
  68. struct device_attribute *attr, char *buf);
  69. extern __printf(4, 5)
  70. struct device *cpu_device_create(struct device *parent, void *drvdata,
  71. const struct attribute_group **groups,
  72. const char *fmt, ...);
  73. #ifdef CONFIG_HOTPLUG_CPU
  74. extern void unregister_cpu(struct cpu *cpu);
  75. extern ssize_t arch_cpu_probe(const char *, size_t);
  76. extern ssize_t arch_cpu_release(const char *, size_t);
  77. #endif
  78. /*
  79. * These states are not related to the core CPU hotplug mechanism. They are
  80. * used by various (sub)architectures to track internal state
  81. */
  82. #define CPU_ONLINE 0x0002 /* CPU is up */
  83. #define CPU_UP_PREPARE 0x0003 /* CPU coming up */
  84. #define CPU_DEAD 0x0007 /* CPU dead */
  85. #define CPU_DEAD_FROZEN 0x0008 /* CPU timed out on unplug */
  86. #define CPU_POST_DEAD 0x0009 /* CPU successfully unplugged */
  87. #define CPU_BROKEN 0x000B /* CPU did not die properly */
  88. #ifdef CONFIG_SMP
  89. extern bool cpuhp_tasks_frozen;
  90. int add_cpu(unsigned int cpu);
  91. int cpu_device_up(struct device *dev);
  92. void notify_cpu_starting(unsigned int cpu);
  93. extern void cpu_maps_update_begin(void);
  94. extern void cpu_maps_update_done(void);
  95. int bringup_hibernate_cpu(unsigned int sleep_cpu);
  96. void bringup_nonboot_cpus(unsigned int setup_max_cpus);
  97. #else /* CONFIG_SMP */
  98. #define cpuhp_tasks_frozen 0
  99. static inline void cpu_maps_update_begin(void)
  100. {
  101. }
  102. static inline void cpu_maps_update_done(void)
  103. {
  104. }
  105. static inline int add_cpu(unsigned int cpu) { return 0;}
  106. #endif /* CONFIG_SMP */
  107. extern struct bus_type cpu_subsys;
  108. extern int lockdep_is_cpus_held(void);
  109. #ifdef CONFIG_HOTPLUG_CPU
  110. extern void cpus_write_lock(void);
  111. extern void cpus_write_unlock(void);
  112. extern void cpus_read_lock(void);
  113. extern void cpus_read_unlock(void);
  114. extern int cpus_read_trylock(void);
  115. extern void lockdep_assert_cpus_held(void);
  116. extern void cpu_hotplug_disable(void);
  117. extern void cpu_hotplug_enable(void);
  118. void clear_tasks_mm_cpumask(int cpu);
  119. int remove_cpu(unsigned int cpu);
  120. int cpu_device_down(struct device *dev);
  121. extern void smp_shutdown_nonboot_cpus(unsigned int primary_cpu);
  122. #else /* CONFIG_HOTPLUG_CPU */
  123. static inline void cpus_write_lock(void) { }
  124. static inline void cpus_write_unlock(void) { }
  125. static inline void cpus_read_lock(void) { }
  126. static inline void cpus_read_unlock(void) { }
  127. static inline int cpus_read_trylock(void) { return true; }
  128. static inline void lockdep_assert_cpus_held(void) { }
  129. static inline void cpu_hotplug_disable(void) { }
  130. static inline void cpu_hotplug_enable(void) { }
  131. static inline int remove_cpu(unsigned int cpu) { return -EPERM; }
  132. static inline void smp_shutdown_nonboot_cpus(unsigned int primary_cpu) { }
  133. #endif /* !CONFIG_HOTPLUG_CPU */
  134. #ifdef CONFIG_PM_SLEEP_SMP
  135. extern int freeze_secondary_cpus(int primary);
  136. extern void thaw_secondary_cpus(void);
  137. static inline int suspend_disable_secondary_cpus(void)
  138. {
  139. int cpu = 0;
  140. if (IS_ENABLED(CONFIG_PM_SLEEP_SMP_NONZERO_CPU))
  141. cpu = -1;
  142. return freeze_secondary_cpus(cpu);
  143. }
  144. static inline void suspend_enable_secondary_cpus(void)
  145. {
  146. return thaw_secondary_cpus();
  147. }
  148. #else /* !CONFIG_PM_SLEEP_SMP */
  149. static inline void thaw_secondary_cpus(void) {}
  150. static inline int suspend_disable_secondary_cpus(void) { return 0; }
  151. static inline void suspend_enable_secondary_cpus(void) { }
  152. #endif /* !CONFIG_PM_SLEEP_SMP */
  153. void __noreturn cpu_startup_entry(enum cpuhp_state state);
  154. void cpu_idle_poll_ctrl(bool enable);
  155. /* Attach to any functions which should be considered cpuidle. */
  156. #define __cpuidle __section(".cpuidle.text")
  157. bool cpu_in_idle(unsigned long pc);
  158. void arch_cpu_idle(void);
  159. void arch_cpu_idle_prepare(void);
  160. void arch_cpu_idle_enter(void);
  161. void arch_cpu_idle_exit(void);
  162. void arch_cpu_idle_dead(void);
  163. #ifdef CONFIG_ARCH_HAS_CPU_FINALIZE_INIT
  164. void arch_cpu_finalize_init(void);
  165. #else
  166. static inline void arch_cpu_finalize_init(void) { }
  167. #endif
  168. int cpu_report_state(int cpu);
  169. int cpu_check_up_prepare(int cpu);
  170. void cpu_set_state_online(int cpu);
  171. void play_idle_precise(u64 duration_ns, u64 latency_ns);
  172. static inline void play_idle(unsigned long duration_us)
  173. {
  174. play_idle_precise(duration_us * NSEC_PER_USEC, U64_MAX);
  175. }
  176. #ifdef CONFIG_HOTPLUG_CPU
  177. bool cpu_wait_death(unsigned int cpu, int seconds);
  178. bool cpu_report_death(void);
  179. void cpuhp_report_idle_dead(void);
  180. #else
  181. static inline void cpuhp_report_idle_dead(void) { }
  182. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  183. enum cpuhp_smt_control {
  184. CPU_SMT_ENABLED,
  185. CPU_SMT_DISABLED,
  186. CPU_SMT_FORCE_DISABLED,
  187. CPU_SMT_NOT_SUPPORTED,
  188. CPU_SMT_NOT_IMPLEMENTED,
  189. };
  190. #if defined(CONFIG_SMP) && defined(CONFIG_HOTPLUG_SMT)
  191. extern enum cpuhp_smt_control cpu_smt_control;
  192. extern void cpu_smt_disable(bool force);
  193. extern void cpu_smt_check_topology(void);
  194. extern bool cpu_smt_possible(void);
  195. extern int cpuhp_smt_enable(void);
  196. extern int cpuhp_smt_disable(enum cpuhp_smt_control ctrlval);
  197. #else
  198. # define cpu_smt_control (CPU_SMT_NOT_IMPLEMENTED)
  199. static inline void cpu_smt_disable(bool force) { }
  200. static inline void cpu_smt_check_topology(void) { }
  201. static inline bool cpu_smt_possible(void) { return false; }
  202. static inline int cpuhp_smt_enable(void) { return 0; }
  203. static inline int cpuhp_smt_disable(enum cpuhp_smt_control ctrlval) { return 0; }
  204. #endif
  205. extern bool cpu_mitigations_off(void);
  206. extern bool cpu_mitigations_auto_nosmt(void);
  207. #endif /* _LINUX_CPU_H_ */