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