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