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