thermal_core.h 10 KB

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