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