xref: /linux/drivers/thermal/thermal_core.h (revision d7f39aee79f04eeaa42085728423501b33ac5be5)
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 /* Default Thermal Governor */
137 #if defined(CONFIG_THERMAL_DEFAULT_GOV_STEP_WISE)
138 #define DEFAULT_THERMAL_GOVERNOR       "step_wise"
139 #elif defined(CONFIG_THERMAL_DEFAULT_GOV_FAIR_SHARE)
140 #define DEFAULT_THERMAL_GOVERNOR       "fair_share"
141 #elif defined(CONFIG_THERMAL_DEFAULT_GOV_USER_SPACE)
142 #define DEFAULT_THERMAL_GOVERNOR       "user_space"
143 #elif defined(CONFIG_THERMAL_DEFAULT_GOV_POWER_ALLOCATOR)
144 #define DEFAULT_THERMAL_GOVERNOR       "power_allocator"
145 #elif defined(CONFIG_THERMAL_DEFAULT_GOV_BANG_BANG)
146 #define DEFAULT_THERMAL_GOVERNOR       "bang_bang"
147 #endif
148 
149 /* Initial state of a cooling device during binding */
150 #define THERMAL_NO_TARGET -1UL
151 
152 /* Init section thermal table */
153 extern struct thermal_governor *__governor_thermal_table[];
154 extern struct thermal_governor *__governor_thermal_table_end[];
155 
156 #define THERMAL_TABLE_ENTRY(table, name)			\
157 	static typeof(name) *__thermal_table_entry_##name	\
158 	__used __section("__" #table "_thermal_table") = &name
159 
160 #define THERMAL_GOVERNOR_DECLARE(name)	THERMAL_TABLE_ENTRY(governor, name)
161 
162 #define for_each_governor_table(__governor)		\
163 	for (__governor = __governor_thermal_table;	\
164 	     __governor < __governor_thermal_table_end;	\
165 	     __governor++)
166 
167 int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
168 			  void *);
169 
170 int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
171 					      void *), void *);
172 
173 int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
174 			      void *thermal_governor);
175 
176 struct thermal_zone_device *thermal_zone_get_by_id(int id);
177 
178 struct thermal_attr {
179 	struct device_attribute attr;
180 	char name[THERMAL_NAME_LENGTH];
181 };
182 
183 static inline bool cdev_is_power_actor(struct thermal_cooling_device *cdev)
184 {
185 	return cdev->ops->get_requested_power && cdev->ops->state2power &&
186 		cdev->ops->power2state;
187 }
188 
189 void thermal_cdev_update(struct thermal_cooling_device *);
190 void __thermal_cdev_update(struct thermal_cooling_device *cdev);
191 
192 int get_tz_trend(struct thermal_zone_device *tz, const struct thermal_trip *trip);
193 
194 struct thermal_instance *
195 get_thermal_instance(struct thermal_zone_device *tz,
196 		     struct thermal_cooling_device *cdev,
197 		     int trip);
198 
199 /*
200  * This structure is used to describe the behavior of
201  * a certain cooling device on a certain trip point
202  * in a certain thermal zone
203  */
204 struct thermal_instance {
205 	int id;
206 	char name[THERMAL_NAME_LENGTH];
207 	struct thermal_zone_device *tz;
208 	struct thermal_cooling_device *cdev;
209 	const struct thermal_trip *trip;
210 	bool initialized;
211 	unsigned long upper;	/* Highest cooling state for this trip point */
212 	unsigned long lower;	/* Lowest cooling state for this trip point */
213 	unsigned long target;	/* expected cooling state */
214 	char attr_name[THERMAL_NAME_LENGTH];
215 	struct device_attribute attr;
216 	char weight_attr_name[THERMAL_NAME_LENGTH];
217 	struct device_attribute weight_attr;
218 	struct list_head tz_node; /* node in tz->thermal_instances */
219 	struct list_head cdev_node; /* node in cdev->thermal_instances */
220 	unsigned int weight; /* The weight of the cooling device */
221 	bool upper_no_limit;
222 };
223 
224 #define to_thermal_zone(_dev) \
225 	container_of(_dev, struct thermal_zone_device, device)
226 
227 #define to_cooling_device(_dev)	\
228 	container_of(_dev, struct thermal_cooling_device, device)
229 
230 int thermal_register_governor(struct thermal_governor *);
231 void thermal_unregister_governor(struct thermal_governor *);
232 int thermal_zone_device_set_policy(struct thermal_zone_device *, char *);
233 int thermal_build_list_of_policies(char *buf);
234 void __thermal_zone_device_update(struct thermal_zone_device *tz,
235 				  enum thermal_notify_event event);
236 void thermal_zone_device_critical_reboot(struct thermal_zone_device *tz);
237 void thermal_governor_update_tz(struct thermal_zone_device *tz,
238 				enum thermal_notify_event reason);
239 
240 /* Helpers */
241 #define for_each_trip_desc(__tz, __td)	\
242 	for (__td = __tz->trips; __td - __tz->trips < __tz->num_trips; __td++)
243 
244 #define trip_to_trip_desc(__trip)	\
245 	container_of(__trip, struct thermal_trip_desc, trip)
246 
247 void __thermal_zone_set_trips(struct thermal_zone_device *tz);
248 int thermal_zone_trip_id(const struct thermal_zone_device *tz,
249 			 const struct thermal_trip *trip);
250 void thermal_zone_trip_updated(struct thermal_zone_device *tz,
251 			       const struct thermal_trip *trip);
252 int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp);
253 void thermal_zone_trip_down(struct thermal_zone_device *tz,
254 			    const struct thermal_trip *trip);
255 
256 /* sysfs I/F */
257 int thermal_zone_create_device_groups(struct thermal_zone_device *tz);
258 void thermal_zone_destroy_device_groups(struct thermal_zone_device *);
259 void thermal_cooling_device_setup_sysfs(struct thermal_cooling_device *);
260 void thermal_cooling_device_destroy_sysfs(struct thermal_cooling_device *cdev);
261 void thermal_cooling_device_stats_reinit(struct thermal_cooling_device *cdev);
262 /* used only at binding time */
263 ssize_t trip_point_show(struct device *, struct device_attribute *, char *);
264 ssize_t weight_show(struct device *, struct device_attribute *, char *);
265 ssize_t weight_store(struct device *, struct device_attribute *, const char *,
266 		     size_t);
267 
268 #ifdef CONFIG_THERMAL_STATISTICS
269 void thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev,
270 					 unsigned long new_state);
271 #else
272 static inline void
273 thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev,
274 				    unsigned long new_state) {}
275 #endif /* CONFIG_THERMAL_STATISTICS */
276 
277 /* device tree support */
278 int thermal_zone_device_is_enabled(struct thermal_zone_device *tz);
279 
280 #endif /* __THERMAL_CORE_H__ */
281