291 lines · c
1/* SPDX-License-Identifier: GPL-2.0 */2/*3 * thermal_core.h4 *5 * Copyright (C) 2012 Intel Corp6 * 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 18struct thermal_attr {19 struct device_attribute attr;20 char name[THERMAL_NAME_LENGTH];21};22 23struct thermal_trip_attrs {24 struct thermal_attr type;25 struct thermal_attr temp;26 struct thermal_attr hyst;27};28 29struct thermal_trip_desc {30 struct thermal_trip trip;31 struct thermal_trip_attrs trip_attrs;32 struct list_head notify_list_node;33 int notify_temp;34 int threshold;35};36 37/**38 * struct thermal_governor - structure that holds thermal governor information39 * @name: name of the governor40 * @bind_to_tz: callback called when binding to a thermal zone. If it41 * returns 0, the governor is bound to the thermal zone,42 * otherwise it fails.43 * @unbind_from_tz: callback called when a governor is unbound from a44 * thermal zone.45 * @trip_crossed: called for trip points that have just been crossed46 * @manage: called on thermal zone temperature updates47 * @update_tz: callback called when thermal zone internals have changed, e.g.48 * thermal cooling instance was added/removed49 * @governor_list: node in thermal_governor_list (in thermal_core.c)50 */51struct thermal_governor {52 const char *name;53 int (*bind_to_tz)(struct thermal_zone_device *tz);54 void (*unbind_from_tz)(struct thermal_zone_device *tz);55 void (*trip_crossed)(struct thermal_zone_device *tz,56 const struct thermal_trip *trip,57 bool crossed_up);58 void (*manage)(struct thermal_zone_device *tz);59 void (*update_tz)(struct thermal_zone_device *tz,60 enum thermal_notify_event reason);61 struct list_head governor_list;62};63 64/**65 * struct thermal_zone_device - structure for a thermal zone66 * @id: unique id number for each thermal zone67 * @type: the thermal zone device type68 * @device: &struct device for this thermal zone69 * @removal: removal completion70 * @resume: resume completion71 * @mode: current mode of this thermal zone72 * @devdata: private pointer for device private data73 * @num_trips: number of trip points the thermal zone supports74 * @passive_delay_jiffies: number of jiffies to wait between polls when75 * performing passive cooling.76 * @polling_delay_jiffies: number of jiffies to wait between polls when77 * checking whether trip points have been crossed (0 for78 * interrupt driven systems)79 * @recheck_delay_jiffies: delay after a failed attempt to determine the zone80 * temperature before trying again81 * @temperature: current temperature. This is only for core code,82 * drivers should use thermal_zone_get_temp() to get the83 * current temperature84 * @last_temperature: previous temperature read85 * @emul_temperature: emulated temperature when using CONFIG_THERMAL_EMULATION86 * @passive: 1 if you've crossed a passive trip point, 0 otherwise.87 * @prev_low_trip: the low current temperature if you've crossed a passive88 trip point.89 * @prev_high_trip: the above current temperature if you've crossed a90 passive trip point.91 * @need_update: if equals 1, thermal_zone_device_update needs to be invoked.92 * @ops: operations this &thermal_zone_device supports93 * @tzp: thermal zone parameters94 * @governor: pointer to the governor for this thermal zone95 * @governor_data: private pointer for governor data96 * @thermal_instances: list of &struct thermal_instance of this thermal zone97 * @ida: &struct ida to generate unique id for this zone's cooling98 * devices99 * @lock: lock to protect thermal_instances list100 * @node: node in thermal_tz_list (in thermal_core.c)101 * @poll_queue: delayed work for polling102 * @notify_event: Last notification event103 * @suspended: thermal zone suspend indicator104 * @resuming: indicates whether or not thermal zone resume is in progress105 * @trips: array of struct thermal_trip objects106 */107struct thermal_zone_device {108 int id;109 char type[THERMAL_NAME_LENGTH];110 struct device device;111 struct completion removal;112 struct completion resume;113 struct attribute_group trips_attribute_group;114 enum thermal_device_mode mode;115 void *devdata;116 int num_trips;117 unsigned long passive_delay_jiffies;118 unsigned long polling_delay_jiffies;119 unsigned long recheck_delay_jiffies;120 int temperature;121 int last_temperature;122 int emul_temperature;123 int passive;124 int prev_low_trip;125 int prev_high_trip;126 atomic_t need_update;127 struct thermal_zone_device_ops ops;128 struct thermal_zone_params *tzp;129 struct thermal_governor *governor;130 void *governor_data;131 struct list_head thermal_instances;132 struct ida ida;133 struct mutex lock;134 struct list_head node;135 struct delayed_work poll_queue;136 enum thermal_notify_event notify_event;137 bool suspended;138 bool resuming;139#ifdef CONFIG_THERMAL_DEBUGFS140 struct thermal_debugfs *debugfs;141#endif142 struct thermal_trip_desc trips[] __counted_by(num_trips);143};144 145/* Initial thermal zone temperature. */146#define THERMAL_TEMP_INIT INT_MIN147 148/*149 * Default and maximum delay after a failed thermal zone temperature check150 * before attempting to check it again (in jiffies).151 */152#define THERMAL_RECHECK_DELAY msecs_to_jiffies(250)153#define THERMAL_MAX_RECHECK_DELAY (120 * HZ)154 155/* Default Thermal Governor */156#if defined(CONFIG_THERMAL_DEFAULT_GOV_STEP_WISE)157#define DEFAULT_THERMAL_GOVERNOR "step_wise"158#elif defined(CONFIG_THERMAL_DEFAULT_GOV_FAIR_SHARE)159#define DEFAULT_THERMAL_GOVERNOR "fair_share"160#elif defined(CONFIG_THERMAL_DEFAULT_GOV_USER_SPACE)161#define DEFAULT_THERMAL_GOVERNOR "user_space"162#elif defined(CONFIG_THERMAL_DEFAULT_GOV_POWER_ALLOCATOR)163#define DEFAULT_THERMAL_GOVERNOR "power_allocator"164#elif defined(CONFIG_THERMAL_DEFAULT_GOV_BANG_BANG)165#define DEFAULT_THERMAL_GOVERNOR "bang_bang"166#endif167 168/* Initial state of a cooling device during binding */169#define THERMAL_NO_TARGET -1UL170 171/* Init section thermal table */172extern struct thermal_governor *__governor_thermal_table[];173extern struct thermal_governor *__governor_thermal_table_end[];174 175#define THERMAL_TABLE_ENTRY(table, name) \176 static typeof(name) *__thermal_table_entry_##name \177 __used __section("__" #table "_thermal_table") = &name178 179#define THERMAL_GOVERNOR_DECLARE(name) THERMAL_TABLE_ENTRY(governor, name)180 181#define for_each_governor_table(__governor) \182 for (__governor = __governor_thermal_table; \183 __governor < __governor_thermal_table_end; \184 __governor++)185 186int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),187 void *);188 189int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,190 void *), void *);191 192int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),193 void *thermal_governor);194 195struct thermal_zone_device *thermal_zone_get_by_id(int id);196 197DEFINE_CLASS(thermal_zone_get_by_id, struct thermal_zone_device *,198 if (_T) put_device(&_T->device), thermal_zone_get_by_id(id), int id)199 200static inline bool cdev_is_power_actor(struct thermal_cooling_device *cdev)201{202 return cdev->ops->get_requested_power && cdev->ops->state2power &&203 cdev->ops->power2state;204}205 206void thermal_cdev_update(struct thermal_cooling_device *);207void __thermal_cdev_update(struct thermal_cooling_device *cdev);208 209int get_tz_trend(struct thermal_zone_device *tz, const struct thermal_trip *trip);210 211/*212 * This structure is used to describe the behavior of213 * a certain cooling device on a certain trip point214 * in a certain thermal zone215 */216struct thermal_instance {217 int id;218 char name[THERMAL_NAME_LENGTH];219 struct thermal_cooling_device *cdev;220 const struct thermal_trip *trip;221 bool initialized;222 unsigned long upper; /* Highest cooling state for this trip point */223 unsigned long lower; /* Lowest cooling state for this trip point */224 unsigned long target; /* expected cooling state */225 char attr_name[THERMAL_NAME_LENGTH];226 struct device_attribute attr;227 char weight_attr_name[THERMAL_NAME_LENGTH];228 struct device_attribute weight_attr;229 struct list_head tz_node; /* node in tz->thermal_instances */230 struct list_head cdev_node; /* node in cdev->thermal_instances */231 unsigned int weight; /* The weight of the cooling device */232 bool upper_no_limit;233};234 235#define to_thermal_zone(_dev) \236 container_of(_dev, struct thermal_zone_device, device)237 238#define to_cooling_device(_dev) \239 container_of(_dev, struct thermal_cooling_device, device)240 241int thermal_register_governor(struct thermal_governor *);242void thermal_unregister_governor(struct thermal_governor *);243int thermal_zone_device_set_policy(struct thermal_zone_device *, char *);244int thermal_build_list_of_policies(char *buf);245void __thermal_zone_device_update(struct thermal_zone_device *tz,246 enum thermal_notify_event event);247void thermal_zone_device_critical_reboot(struct thermal_zone_device *tz);248void thermal_governor_update_tz(struct thermal_zone_device *tz,249 enum thermal_notify_event reason);250 251/* Helpers */252#define for_each_trip_desc(__tz, __td) \253 for (__td = __tz->trips; __td - __tz->trips < __tz->num_trips; __td++)254 255#define trip_to_trip_desc(__trip) \256 container_of(__trip, struct thermal_trip_desc, trip)257 258const char *thermal_trip_type_name(enum thermal_trip_type trip_type);259 260void thermal_zone_set_trips(struct thermal_zone_device *tz, int low, int high);261int thermal_zone_trip_id(const struct thermal_zone_device *tz,262 const struct thermal_trip *trip);263int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp);264void thermal_zone_trip_down(struct thermal_zone_device *tz,265 const struct thermal_trip *trip);266void thermal_zone_set_trip_hyst(struct thermal_zone_device *tz,267 struct thermal_trip *trip, int hyst);268 269/* sysfs I/F */270int thermal_zone_create_device_groups(struct thermal_zone_device *tz);271void thermal_zone_destroy_device_groups(struct thermal_zone_device *);272void thermal_cooling_device_setup_sysfs(struct thermal_cooling_device *);273void thermal_cooling_device_destroy_sysfs(struct thermal_cooling_device *cdev);274void thermal_cooling_device_stats_reinit(struct thermal_cooling_device *cdev);275/* used only at binding time */276ssize_t trip_point_show(struct device *, struct device_attribute *, char *);277ssize_t weight_show(struct device *, struct device_attribute *, char *);278ssize_t weight_store(struct device *, struct device_attribute *, const char *,279 size_t);280 281#ifdef CONFIG_THERMAL_STATISTICS282void thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev,283 unsigned long new_state);284#else285static inline void286thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev,287 unsigned long new_state) {}288#endif /* CONFIG_THERMAL_STATISTICS */289 290#endif /* __THERMAL_CORE_H__ */291