thermal_core.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * thermal_core.h
  4. *
  5. * Copyright (C) 2012 Intel Corp
  6. * Author: Durgadoss R <durgadoss.r@intel.com>
  7. */
  8. #ifndef __THERMAL_CORE_H__
  9. #define __THERMAL_CORE_H__
  10. #include <linux/cleanup.h>
  11. #include <linux/device.h>
  12. #include <linux/thermal.h>
  13. #include "thermal_netlink.h"
  14. #include "thermal_thresholds.h"
  15. #include "thermal_debugfs.h"
  16. struct thermal_attr {
  17. struct device_attribute attr;
  18. char name[THERMAL_NAME_LENGTH];
  19. };
  20. struct thermal_trip_attrs {
  21. struct thermal_attr type;
  22. struct thermal_attr temp;
  23. struct thermal_attr hyst;
  24. };
  25. struct thermal_trip_desc {
  26. struct thermal_trip trip;
  27. struct thermal_trip_attrs trip_attrs;
  28. struct list_head list_node;
  29. struct list_head thermal_instances;
  30. int threshold;
  31. };
  32. /**
  33. * struct thermal_governor - structure that holds thermal governor information
  34. * @name: name of the governor
  35. * @bind_to_tz: callback called when binding to a thermal zone. If it
  36. * returns 0, the governor is bound to the thermal zone,
  37. * otherwise it fails.
  38. * @unbind_from_tz: callback called when a governor is unbound from a
  39. * thermal zone.
  40. * @trip_crossed: called for trip points that have just been crossed
  41. * @manage: called on thermal zone temperature updates
  42. * @update_tz: callback called when thermal zone internals have changed, e.g.
  43. * thermal cooling instance was added/removed
  44. * @governor_list: node in thermal_governor_list (in thermal_core.c)
  45. */
  46. struct thermal_governor {
  47. const char *name;
  48. int (*bind_to_tz)(struct thermal_zone_device *tz);
  49. void (*unbind_from_tz)(struct thermal_zone_device *tz);
  50. void (*trip_crossed)(struct thermal_zone_device *tz,
  51. const struct thermal_trip *trip,
  52. bool upward);
  53. void (*manage)(struct thermal_zone_device *tz);
  54. void (*update_tz)(struct thermal_zone_device *tz,
  55. enum thermal_notify_event reason);
  56. struct list_head governor_list;
  57. };
  58. #define TZ_STATE_FLAG_SUSPENDED BIT(0)
  59. #define TZ_STATE_FLAG_RESUMING BIT(1)
  60. #define TZ_STATE_FLAG_INIT BIT(2)
  61. #define TZ_STATE_FLAG_EXIT BIT(3)
  62. #define TZ_STATE_READY 0
  63. /**
  64. * struct thermal_zone_device - structure for a thermal zone
  65. * @id: unique id number for each thermal zone
  66. * @type: the thermal zone device type
  67. * @device: &struct device for this thermal zone
  68. * @removal: removal completion
  69. * @resume: resume completion
  70. * @trips_attribute_group: trip point sysfs attributes
  71. * @trips_high: trips above the current zone temperature
  72. * @trips_reached: trips below or at the current zone temperature
  73. * @trips_invalid: trips with invalid temperature
  74. * @mode: current mode of this thermal zone
  75. * @devdata: private pointer for device private data
  76. * @num_trips: number of trip points the thermal zone supports
  77. * @passive_delay_jiffies: number of jiffies to wait between polls when
  78. * performing passive cooling.
  79. * @polling_delay_jiffies: number of jiffies to wait between polls when
  80. * checking whether trip points have been crossed (0 for
  81. * interrupt driven systems)
  82. * @recheck_delay_jiffies: delay after a failed attempt to determine the zone
  83. * temperature before trying again
  84. * @temperature: current temperature. This is only for core code,
  85. * drivers should use thermal_zone_get_temp() to get the
  86. * current temperature
  87. * @last_temperature: previous temperature read
  88. * @emul_temperature: emulated temperature when using CONFIG_THERMAL_EMULATION
  89. * @passive: 1 if you've crossed a passive trip point, 0 otherwise.
  90. * @prev_low_trip: the low current temperature if you've crossed a passive
  91. * trip point.
  92. * @prev_high_trip: the above current temperature if you've crossed a
  93. * passive trip point.
  94. * @ops: operations this &thermal_zone_device supports
  95. * @tzp: thermal zone parameters
  96. * @governor: pointer to the governor for this thermal zone
  97. * @governor_data: private pointer for governor data
  98. * @ida: &struct ida to generate unique id for this zone's cooling
  99. * devices
  100. * @lock: lock to protect thermal_instances list
  101. * @node: node in thermal_tz_list (in thermal_core.c)
  102. * @poll_queue: delayed work for polling
  103. * @notify_event: Last notification event
  104. * @state: current state of the thermal zone
  105. * @debugfs: this thermal zone device's thermal zone debug info
  106. * @user_thresholds: list of userspace thresholds for temp. limit notifications
  107. * @trips: array of struct thermal_trip objects
  108. */
  109. struct thermal_zone_device {
  110. int id;
  111. char type[THERMAL_NAME_LENGTH];
  112. struct device device;
  113. struct completion removal;
  114. struct completion resume;
  115. struct attribute_group trips_attribute_group;
  116. struct list_head trips_high;
  117. struct list_head trips_reached;
  118. struct list_head trips_invalid;
  119. enum thermal_device_mode mode;
  120. void *devdata;
  121. int num_trips;
  122. unsigned long passive_delay_jiffies;
  123. unsigned long polling_delay_jiffies;
  124. unsigned long recheck_delay_jiffies;
  125. int temperature;
  126. int last_temperature;
  127. int emul_temperature;
  128. int passive;
  129. int prev_low_trip;
  130. int prev_high_trip;
  131. struct thermal_zone_device_ops ops;
  132. struct thermal_zone_params *tzp;
  133. struct thermal_governor *governor;
  134. void *governor_data;
  135. struct ida ida;
  136. struct mutex lock;
  137. struct list_head node;
  138. struct delayed_work poll_queue;
  139. enum thermal_notify_event notify_event;
  140. u8 state;
  141. #ifdef CONFIG_THERMAL_DEBUGFS
  142. struct thermal_debugfs *debugfs;
  143. #endif
  144. struct list_head user_thresholds;
  145. struct thermal_trip_desc trips[] __counted_by(num_trips);
  146. };
  147. DEFINE_GUARD(thermal_zone, struct thermal_zone_device *, mutex_lock(&_T->lock),
  148. mutex_unlock(&_T->lock))
  149. DEFINE_GUARD(thermal_zone_reverse, struct thermal_zone_device *,
  150. mutex_unlock(&_T->lock), mutex_lock(&_T->lock))
  151. /* Initial thermal zone temperature. */
  152. #define THERMAL_TEMP_INIT INT_MIN
  153. /*
  154. * Default and maximum delay after a failed thermal zone temperature check
  155. * before attempting to check it again (in jiffies).
  156. */
  157. #define THERMAL_RECHECK_DELAY msecs_to_jiffies(250)
  158. #define THERMAL_MAX_RECHECK_DELAY (120 * HZ)
  159. /* Default Thermal Governor */
  160. #if defined(CONFIG_THERMAL_DEFAULT_GOV_STEP_WISE)
  161. #define DEFAULT_THERMAL_GOVERNOR "step_wise"
  162. #elif defined(CONFIG_THERMAL_DEFAULT_GOV_FAIR_SHARE)
  163. #define DEFAULT_THERMAL_GOVERNOR "fair_share"
  164. #elif defined(CONFIG_THERMAL_DEFAULT_GOV_USER_SPACE)
  165. #define DEFAULT_THERMAL_GOVERNOR "user_space"
  166. #elif defined(CONFIG_THERMAL_DEFAULT_GOV_POWER_ALLOCATOR)
  167. #define DEFAULT_THERMAL_GOVERNOR "power_allocator"
  168. #elif defined(CONFIG_THERMAL_DEFAULT_GOV_BANG_BANG)
  169. #define DEFAULT_THERMAL_GOVERNOR "bang_bang"
  170. #endif
  171. /* Initial state of a cooling device during binding */
  172. #define THERMAL_NO_TARGET -1UL
  173. /* Init section thermal table */
  174. extern struct thermal_governor *__governor_thermal_table[];
  175. extern struct thermal_governor *__governor_thermal_table_end[];
  176. #define THERMAL_TABLE_ENTRY(table, name) \
  177. static typeof(name) *__thermal_table_entry_##name \
  178. __used __section("__" #table "_thermal_table") = &name
  179. #define THERMAL_GOVERNOR_DECLARE(name) THERMAL_TABLE_ENTRY(governor, name)
  180. #define for_each_governor_table(__governor) \
  181. for (__governor = __governor_thermal_table; \
  182. __governor < __governor_thermal_table_end; \
  183. __governor++)
  184. int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
  185. void *);
  186. int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
  187. void *), void *);
  188. int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
  189. void *thermal_governor);
  190. struct thermal_zone_device *thermal_zone_get_by_id(int id);
  191. DEFINE_CLASS(thermal_zone_get_by_id, struct thermal_zone_device *,
  192. if (_T) put_device(&_T->device), thermal_zone_get_by_id(id), int id)
  193. static inline bool cdev_is_power_actor(struct thermal_cooling_device *cdev)
  194. {
  195. return cdev->ops->get_requested_power && cdev->ops->state2power &&
  196. cdev->ops->power2state;
  197. }
  198. void thermal_cdev_update(struct thermal_cooling_device *);
  199. void thermal_cdev_update_nocheck(struct thermal_cooling_device *cdev);
  200. void __thermal_cdev_update(struct thermal_cooling_device *cdev);
  201. int get_tz_trend(struct thermal_zone_device *tz, const struct thermal_trip *trip);
  202. /*
  203. * This structure is used to describe the behavior of
  204. * a certain cooling device on a certain trip point
  205. * in a certain thermal zone
  206. */
  207. struct thermal_instance {
  208. int id;
  209. char name[THERMAL_NAME_LENGTH];
  210. struct thermal_cooling_device *cdev;
  211. const struct thermal_trip *trip;
  212. bool initialized;
  213. unsigned long upper; /* Highest cooling state for this trip point */
  214. unsigned long lower; /* Lowest cooling state for this trip point */
  215. unsigned long target; /* expected cooling state */
  216. char attr_name[THERMAL_NAME_LENGTH];
  217. struct device_attribute attr;
  218. char weight_attr_name[THERMAL_NAME_LENGTH];
  219. struct device_attribute weight_attr;
  220. struct list_head trip_node; /* node in trip->thermal_instances */
  221. struct list_head cdev_node; /* node in cdev->thermal_instances */
  222. unsigned int weight; /* The weight of the cooling device */
  223. bool upper_no_limit;
  224. };
  225. #define to_thermal_zone(_dev) \
  226. container_of(_dev, struct thermal_zone_device, device)
  227. #define to_cooling_device(_dev) \
  228. container_of(_dev, struct thermal_cooling_device, device)
  229. int thermal_register_governor(struct thermal_governor *);
  230. void thermal_unregister_governor(struct thermal_governor *);
  231. int thermal_zone_device_set_policy(struct thermal_zone_device *, char *);
  232. int thermal_build_list_of_policies(char *buf);
  233. void __thermal_zone_device_update(struct thermal_zone_device *tz,
  234. enum thermal_notify_event event);
  235. void thermal_zone_device_critical_reboot(struct thermal_zone_device *tz);
  236. void thermal_zone_device_critical_shutdown(struct thermal_zone_device *tz);
  237. void thermal_governor_update_tz(struct thermal_zone_device *tz,
  238. enum thermal_notify_event reason);
  239. /* Helpers */
  240. #define for_each_trip_desc(__tz, __td) \
  241. for (__td = __tz->trips; __td - __tz->trips < __tz->num_trips; __td++)
  242. #define trip_to_trip_desc(__trip) \
  243. container_of(__trip, struct thermal_trip_desc, trip)
  244. const char *thermal_trip_type_name(enum thermal_trip_type trip_type);
  245. void thermal_zone_set_trips(struct thermal_zone_device *tz, int low, int high);
  246. int thermal_zone_trip_id(const struct thermal_zone_device *tz,
  247. const struct thermal_trip *trip);
  248. int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp);
  249. void thermal_zone_set_trip_hyst(struct thermal_zone_device *tz,
  250. struct thermal_trip *trip, int hyst);
  251. /* sysfs I/F */
  252. int thermal_zone_create_device_groups(struct thermal_zone_device *tz);
  253. void thermal_zone_destroy_device_groups(struct thermal_zone_device *);
  254. void thermal_cooling_device_setup_sysfs(struct thermal_cooling_device *);
  255. void thermal_cooling_device_destroy_sysfs(struct thermal_cooling_device *cdev);
  256. void thermal_cooling_device_stats_reinit(struct thermal_cooling_device *cdev);
  257. /* used only at binding time */
  258. ssize_t trip_point_show(struct device *, struct device_attribute *, char *);
  259. ssize_t weight_show(struct device *, struct device_attribute *, char *);
  260. ssize_t weight_store(struct device *, struct device_attribute *, const char *,
  261. size_t);
  262. #ifdef CONFIG_THERMAL_STATISTICS
  263. void thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev,
  264. unsigned long new_state);
  265. #else
  266. static inline void
  267. thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev,
  268. unsigned long new_state) {}
  269. #endif /* CONFIG_THERMAL_STATISTICS */
  270. #endif /* __THERMAL_CORE_H__ */