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