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