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 20 struct thermal_attr { 21 struct device_attribute attr; 22 char name[THERMAL_NAME_LENGTH]; 23 }; 24 25 struct thermal_trip_attrs { 26 struct thermal_attr type; 27 struct thermal_attr temp; 28 struct thermal_attr hyst; 29 }; 30 31 struct 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 */ 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 #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 */ 116 struct 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 155 DEFINE_GUARD(thermal_zone, struct thermal_zone_device *, mutex_lock(&_T->lock), 156 mutex_unlock(&_T->lock)) 157 158 DEFINE_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 */ 188 extern struct thermal_governor *__governor_thermal_table[]; 189 extern 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 202 int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *), 203 void *); 204 205 int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *, 206 void *), void *); 207 208 int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *), 209 void *thermal_governor); 210 211 struct thermal_zone_device *thermal_zone_get_by_id(int id); 212 213 DEFINE_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 216 static 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 222 void thermal_cdev_update(struct thermal_cooling_device *); 223 void thermal_cdev_update_nocheck(struct thermal_cooling_device *cdev); 224 void __thermal_cdev_update(struct thermal_cooling_device *cdev); 225 226 int 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 */ 233 struct 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 258 int thermal_register_governor(struct thermal_governor *); 259 void thermal_unregister_governor(struct thermal_governor *); 260 int thermal_zone_device_set_policy(struct thermal_zone_device *, char *); 261 int thermal_build_list_of_policies(char *buf); 262 void __thermal_zone_device_update(struct thermal_zone_device *tz, 263 enum thermal_notify_event event); 264 void thermal_zone_device_critical_reboot(struct thermal_zone_device *tz); 265 void 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 275 const char *thermal_trip_type_name(enum thermal_trip_type trip_type); 276 277 void thermal_zone_set_trips(struct thermal_zone_device *tz, int low, int high); 278 int thermal_zone_trip_id(const struct thermal_zone_device *tz, 279 const struct thermal_trip *trip); 280 int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp); 281 void thermal_zone_set_trip_hyst(struct thermal_zone_device *tz, 282 struct thermal_trip *trip, int hyst); 283 284 /* sysfs I/F */ 285 int thermal_zone_create_device_groups(struct thermal_zone_device *tz); 286 void thermal_zone_destroy_device_groups(struct thermal_zone_device *); 287 void thermal_cooling_device_setup_sysfs(struct thermal_cooling_device *); 288 void thermal_cooling_device_destroy_sysfs(struct thermal_cooling_device *cdev); 289 void thermal_cooling_device_stats_reinit(struct thermal_cooling_device *cdev); 290 /* used only at binding time */ 291 ssize_t trip_point_show(struct device *, struct device_attribute *, char *); 292 ssize_t weight_show(struct device *, struct device_attribute *, char *); 293 ssize_t weight_store(struct device *, struct device_attribute *, const char *, 294 size_t); 295 296 #ifdef CONFIG_THERMAL_STATISTICS 297 void thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev, 298 unsigned long new_state); 299 #else 300 static inline void 301 thermal_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__ */ 306