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