xref: /linux/drivers/thermal/thermal_core.h (revision c2a96b7f187fb6a455836d4a6e113947ff11de97)
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  * @temperature:	current temperature.  This is only for core code,
71  *			drivers should use thermal_zone_get_temp() to get the
72  *			current temperature
73  * @last_temperature:	previous temperature read
74  * @emul_temperature:	emulated temperature when using CONFIG_THERMAL_EMULATION
75  * @passive:		1 if you've crossed a passive trip point, 0 otherwise.
76  * @prev_low_trip:	the low current temperature if you've crossed a passive
77 			trip point.
78  * @prev_high_trip:	the above current temperature if you've crossed a
79 			passive trip point.
80  * @need_update:	if equals 1, thermal_zone_device_update needs to be invoked.
81  * @ops:	operations this &thermal_zone_device supports
82  * @tzp:	thermal zone parameters
83  * @governor:	pointer to the governor for this thermal zone
84  * @governor_data:	private pointer for governor data
85  * @thermal_instances:	list of &struct thermal_instance of this thermal zone
86  * @ida:	&struct ida to generate unique id for this zone's cooling
87  *		devices
88  * @lock:	lock to protect thermal_instances list
89  * @node:	node in thermal_tz_list (in thermal_core.c)
90  * @poll_queue:	delayed work for polling
91  * @notify_event: Last notification event
92  * @suspended: thermal zone suspend indicator
93  * @resuming:	indicates whether or not thermal zone resume is in progress
94  * @trips:	array of struct thermal_trip objects
95  */
96 struct thermal_zone_device {
97 	int id;
98 	char type[THERMAL_NAME_LENGTH];
99 	struct device device;
100 	struct completion removal;
101 	struct completion resume;
102 	struct attribute_group trips_attribute_group;
103 	struct thermal_attr *trip_temp_attrs;
104 	struct thermal_attr *trip_type_attrs;
105 	struct thermal_attr *trip_hyst_attrs;
106 	enum thermal_device_mode mode;
107 	void *devdata;
108 	int num_trips;
109 	unsigned long passive_delay_jiffies;
110 	unsigned long polling_delay_jiffies;
111 	int temperature;
112 	int last_temperature;
113 	int emul_temperature;
114 	int passive;
115 	int prev_low_trip;
116 	int prev_high_trip;
117 	atomic_t need_update;
118 	struct thermal_zone_device_ops ops;
119 	struct thermal_zone_params *tzp;
120 	struct thermal_governor *governor;
121 	void *governor_data;
122 	struct list_head thermal_instances;
123 	struct ida ida;
124 	struct mutex lock;
125 	struct list_head node;
126 	struct delayed_work poll_queue;
127 	enum thermal_notify_event notify_event;
128 	bool suspended;
129 	bool resuming;
130 #ifdef CONFIG_THERMAL_DEBUGFS
131 	struct thermal_debugfs *debugfs;
132 #endif
133 	struct thermal_trip_desc trips[] __counted_by(num_trips);
134 };
135 
136 /* Initial thermal zone temperature. */
137 #define THERMAL_TEMP_INIT	INT_MIN
138 
139 /*
140  * Default delay after a failing thermal zone temperature check before
141  * attempting to check it again.
142  */
143 #define THERMAL_RECHECK_DELAY_MS	250
144 
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 
158 /* Initial state of a cooling device during binding */
159 #define THERMAL_NO_TARGET -1UL
160 
161 /* Init section thermal table */
162 extern struct thermal_governor *__governor_thermal_table[];
163 extern struct thermal_governor *__governor_thermal_table_end[];
164 
165 #define THERMAL_TABLE_ENTRY(table, name)			\
166 	static typeof(name) *__thermal_table_entry_##name	\
167 	__used __section("__" #table "_thermal_table") = &name
168 
169 #define THERMAL_GOVERNOR_DECLARE(name)	THERMAL_TABLE_ENTRY(governor, name)
170 
171 #define for_each_governor_table(__governor)		\
172 	for (__governor = __governor_thermal_table;	\
173 	     __governor < __governor_thermal_table_end;	\
174 	     __governor++)
175 
176 int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
177 			  void *);
178 
179 int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
180 					      void *), void *);
181 
182 int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
183 			      void *thermal_governor);
184 
185 struct thermal_zone_device *thermal_zone_get_by_id(int id);
186 
187 struct thermal_attr {
188 	struct device_attribute attr;
189 	char name[THERMAL_NAME_LENGTH];
190 };
191 
192 static inline bool cdev_is_power_actor(struct thermal_cooling_device *cdev)
193 {
194 	return cdev->ops->get_requested_power && cdev->ops->state2power &&
195 		cdev->ops->power2state;
196 }
197 
198 void thermal_cdev_update(struct thermal_cooling_device *);
199 void __thermal_cdev_update(struct thermal_cooling_device *cdev);
200 
201 int get_tz_trend(struct thermal_zone_device *tz, const struct thermal_trip *trip);
202 
203 struct thermal_instance *
204 get_thermal_instance(struct thermal_zone_device *tz,
205 		     struct thermal_cooling_device *cdev,
206 		     int trip);
207 
208 /*
209  * This structure is used to describe the behavior of
210  * a certain cooling device on a certain trip point
211  * in a certain thermal zone
212  */
213 struct thermal_instance {
214 	int id;
215 	char name[THERMAL_NAME_LENGTH];
216 	struct thermal_zone_device *tz;
217 	struct thermal_cooling_device *cdev;
218 	const struct thermal_trip *trip;
219 	bool initialized;
220 	unsigned long upper;	/* Highest cooling state for this trip point */
221 	unsigned long lower;	/* Lowest cooling state for this trip point */
222 	unsigned long target;	/* expected cooling state */
223 	char attr_name[THERMAL_NAME_LENGTH];
224 	struct device_attribute attr;
225 	char weight_attr_name[THERMAL_NAME_LENGTH];
226 	struct device_attribute weight_attr;
227 	struct list_head tz_node; /* node in tz->thermal_instances */
228 	struct list_head cdev_node; /* node in cdev->thermal_instances */
229 	unsigned int weight; /* The weight of the cooling device */
230 	bool upper_no_limit;
231 };
232 
233 #define to_thermal_zone(_dev) \
234 	container_of(_dev, struct thermal_zone_device, device)
235 
236 #define to_cooling_device(_dev)	\
237 	container_of(_dev, struct thermal_cooling_device, device)
238 
239 int thermal_register_governor(struct thermal_governor *);
240 void thermal_unregister_governor(struct thermal_governor *);
241 int thermal_zone_device_set_policy(struct thermal_zone_device *, char *);
242 int thermal_build_list_of_policies(char *buf);
243 void __thermal_zone_device_update(struct thermal_zone_device *tz,
244 				  enum thermal_notify_event event);
245 void thermal_zone_device_critical_reboot(struct thermal_zone_device *tz);
246 void thermal_governor_update_tz(struct thermal_zone_device *tz,
247 				enum thermal_notify_event reason);
248 
249 /* Helpers */
250 #define for_each_trip_desc(__tz, __td)	\
251 	for (__td = __tz->trips; __td - __tz->trips < __tz->num_trips; __td++)
252 
253 #define trip_to_trip_desc(__trip)	\
254 	container_of(__trip, struct thermal_trip_desc, trip)
255 
256 const char *thermal_trip_type_name(enum thermal_trip_type trip_type);
257 
258 void thermal_zone_set_trips(struct thermal_zone_device *tz);
259 int thermal_zone_trip_id(const struct thermal_zone_device *tz,
260 			 const struct thermal_trip *trip);
261 void thermal_zone_trip_updated(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 
267 /* sysfs I/F */
268 int thermal_zone_create_device_groups(struct thermal_zone_device *tz);
269 void thermal_zone_destroy_device_groups(struct thermal_zone_device *);
270 void thermal_cooling_device_setup_sysfs(struct thermal_cooling_device *);
271 void thermal_cooling_device_destroy_sysfs(struct thermal_cooling_device *cdev);
272 void thermal_cooling_device_stats_reinit(struct thermal_cooling_device *cdev);
273 /* used only at binding time */
274 ssize_t trip_point_show(struct device *, struct device_attribute *, char *);
275 ssize_t weight_show(struct device *, struct device_attribute *, char *);
276 ssize_t weight_store(struct device *, struct device_attribute *, const char *,
277 		     size_t);
278 
279 #ifdef CONFIG_THERMAL_STATISTICS
280 void thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev,
281 					 unsigned long new_state);
282 #else
283 static inline void
284 thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev,
285 				    unsigned long new_state) {}
286 #endif /* CONFIG_THERMAL_STATISTICS */
287 
288 /* device tree support */
289 int thermal_zone_device_is_enabled(struct thermal_zone_device *tz);
290 
291 #endif /* __THERMAL_CORE_H__ */
292