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