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