2018-05-07 11:52:29 -06:00
/* SPDX-License-Identifier: GPL-2.0 */
2012-09-18 11:04:53 +05:30
/*
* thermal_core . h
*
* Copyright ( C ) 2012 Intel Corp
* Author : Durgadoss R < durgadoss . r @ intel . com >
*/
# ifndef __THERMAL_CORE_H__
# define __THERMAL_CORE_H__
# include <linux/device.h>
# include <linux/thermal.h>
2020-07-07 11:01:57 +02:00
# include "thermal_netlink.h"
2024-01-09 10:41:11 +01:00
# include "thermal_debugfs.h"
2020-07-07 11:01:57 +02:00
2024-04-02 20:57:57 +02:00
struct thermal_trip_desc {
struct thermal_trip trip ;
2024-04-02 21:03:36 +02:00
struct list_head notify_list_node ;
int notify_temp ;
2024-04-02 20:57:57 +02:00
int threshold ;
} ;
2024-04-04 21:27:07 +02:00
/**
* struct thermal_governor - structure that holds thermal governor information
* @ name : name of the governor
* @ bind_to_tz : callback called when binding to a thermal zone . If it
* returns 0 , the governor is bound to the thermal zone ,
* otherwise it fails .
* @ unbind_from_tz : callback called when a governor is unbound from a
* thermal zone .
2024-04-10 18:10:53 +02:00
* @ trip_crossed : called for trip points that have just been crossed
2024-04-04 21:27:07 +02:00
* @ throttle : callback called for every trip point even if temperature is
* below the trip point temperature
* @ update_tz : callback called when thermal zone internals have changed , e . g .
* thermal cooling instance was added / removed
* @ governor_list : node in thermal_governor_list ( in thermal_core . c )
*/
struct thermal_governor {
const char * name ;
int ( * bind_to_tz ) ( struct thermal_zone_device * tz ) ;
void ( * unbind_from_tz ) ( struct thermal_zone_device * tz ) ;
2024-04-10 18:10:53 +02:00
void ( * trip_crossed ) ( struct thermal_zone_device * tz ,
const struct thermal_trip * trip ,
bool crossed_up ) ;
2024-04-04 21:27:07 +02:00
int ( * throttle ) ( struct thermal_zone_device * tz ,
const struct thermal_trip * trip ) ;
void ( * update_tz ) ( struct thermal_zone_device * tz ,
enum thermal_notify_event reason ) ;
struct list_head governor_list ;
} ;
2024-04-02 20:57:57 +02:00
/**
* struct thermal_zone_device - structure for a thermal zone
* @ id : unique id number for each thermal zone
* @ type : the thermal zone device type
* @ device : & struct device for this thermal zone
* @ removal : removal completion
* @ trip_temp_attrs : attributes for trip points for sysfs : trip temperature
* @ trip_type_attrs : attributes for trip points for sysfs : trip type
* @ trip_hyst_attrs : attributes for trip points for sysfs : trip hysteresis
* @ mode : current mode of this thermal zone
* @ devdata : private pointer for device private data
* @ num_trips : number of trip points the thermal zone supports
* @ passive_delay_jiffies : number of jiffies to wait between polls when
* performing passive cooling .
* @ polling_delay_jiffies : number of jiffies to wait between polls when
* checking whether trip points have been crossed ( 0 for
* interrupt driven systems )
* @ temperature : current temperature . This is only for core code ,
* drivers should use thermal_zone_get_temp ( ) to get the
* current temperature
* @ last_temperature : previous temperature read
* @ emul_temperature : emulated temperature when using CONFIG_THERMAL_EMULATION
* @ passive : 1 if you ' ve crossed a passive trip point , 0 otherwise .
* @ prev_low_trip : the low current temperature if you ' ve crossed a passive
trip point .
* @ prev_high_trip : the above current temperature if you ' ve crossed a
passive trip point .
* @ need_update : if equals 1 , thermal_zone_device_update needs to be invoked .
* @ ops : operations this & thermal_zone_device supports
* @ tzp : thermal zone parameters
* @ governor : pointer to the governor for this thermal zone
* @ governor_data : private pointer for governor data
* @ thermal_instances : list of & struct thermal_instance of this thermal zone
* @ ida : & struct ida to generate unique id for this zone ' s cooling
* devices
* @ lock : lock to protect thermal_instances list
* @ node : node in thermal_tz_list ( in thermal_core . c )
* @ poll_queue : delayed work for polling
* @ notify_event : Last notification event
* @ suspended : thermal zone suspend indicator
* @ trips : array of struct thermal_trip objects
*/
struct thermal_zone_device {
int id ;
char type [ THERMAL_NAME_LENGTH ] ;
struct device device ;
struct completion removal ;
struct attribute_group trips_attribute_group ;
struct thermal_attr * trip_temp_attrs ;
struct thermal_attr * trip_type_attrs ;
struct thermal_attr * trip_hyst_attrs ;
enum thermal_device_mode mode ;
void * devdata ;
int num_trips ;
unsigned long passive_delay_jiffies ;
unsigned long polling_delay_jiffies ;
int temperature ;
int last_temperature ;
int emul_temperature ;
int passive ;
int prev_low_trip ;
int prev_high_trip ;
atomic_t need_update ;
struct thermal_zone_device_ops ops ;
struct thermal_zone_params * tzp ;
struct thermal_governor * governor ;
void * governor_data ;
struct list_head thermal_instances ;
struct ida ida ;
struct mutex lock ;
struct list_head node ;
struct delayed_work poll_queue ;
enum thermal_notify_event notify_event ;
bool suspended ;
# ifdef CONFIG_THERMAL_DEBUGFS
struct thermal_debugfs * debugfs ;
# endif
struct thermal_trip_desc trips [ ] __counted_by ( num_trips ) ;
} ;
2020-04-02 16:27:39 +02:00
/* Default Thermal Governor */
# if defined(CONFIG_THERMAL_DEFAULT_GOV_STEP_WISE)
# define DEFAULT_THERMAL_GOVERNOR "step_wise"
# elif defined(CONFIG_THERMAL_DEFAULT_GOV_FAIR_SHARE)
# define DEFAULT_THERMAL_GOVERNOR "fair_share"
# elif defined(CONFIG_THERMAL_DEFAULT_GOV_USER_SPACE)
# define DEFAULT_THERMAL_GOVERNOR "user_space"
# elif defined(CONFIG_THERMAL_DEFAULT_GOV_POWER_ALLOCATOR)
# define DEFAULT_THERMAL_GOVERNOR "power_allocator"
2023-06-09 14:44:08 +02:00
# elif defined(CONFIG_THERMAL_DEFAULT_GOV_BANG_BANG)
# define DEFAULT_THERMAL_GOVERNOR "bang_bang"
2020-04-02 16:27:39 +02:00
# endif
2012-09-18 11:04:53 +05:30
/* Initial state of a cooling device during binding */
# define THERMAL_NO_TARGET -1UL
2019-06-12 22:13:24 +02:00
/* Init section thermal table */
extern struct thermal_governor * __governor_thermal_table [ ] ;
extern struct thermal_governor * __governor_thermal_table_end [ ] ;
# define THERMAL_TABLE_ENTRY(table, name) \
static typeof ( name ) * __thermal_table_entry_ # # name \
2020-10-21 19:36:07 -07:00
__used __section ( " __ " # table " _thermal_table " ) = & name
2019-06-12 22:13:24 +02:00
# define THERMAL_GOVERNOR_DECLARE(name) THERMAL_TABLE_ENTRY(governor, name)
# define for_each_governor_table(__governor) \
for ( __governor = __governor_thermal_table ; \
__governor < __governor_thermal_table_end ; \
__governor + + )
2020-07-06 12:55:35 +02:00
int for_each_thermal_zone ( int ( * cb ) ( struct thermal_zone_device * , void * ) ,
void * ) ;
int for_each_thermal_cooling_device ( int ( * cb ) ( struct thermal_cooling_device * ,
void * ) , void * ) ;
int for_each_thermal_governor ( int ( * cb ) ( struct thermal_governor * , void * ) ,
void * thermal_governor ) ;
2020-07-06 12:55:36 +02:00
struct thermal_zone_device * thermal_zone_get_by_id ( int id ) ;
2020-04-02 16:27:40 +02:00
struct thermal_attr {
struct device_attribute attr ;
char name [ THERMAL_NAME_LENGTH ] ;
} ;
2020-04-02 16:27:41 +02:00
static inline bool cdev_is_power_actor ( struct thermal_cooling_device * cdev )
{
return cdev - > ops - > get_requested_power & & cdev - > ops - > state2power & &
cdev - > ops - > power2state ;
}
2021-01-18 18:38:24 +01:00
void thermal_cdev_update ( struct thermal_cooling_device * ) ;
2021-04-22 12:43:06 +01:00
void __thermal_cdev_update ( struct thermal_cooling_device * cdev ) ;
2021-01-18 18:38:24 +01:00
2023-10-12 20:34:50 +02:00
int get_tz_trend ( struct thermal_zone_device * tz , const struct thermal_trip * trip ) ;
2020-04-02 16:27:43 +02:00
2020-04-02 16:27:44 +02:00
struct thermal_instance *
get_thermal_instance ( struct thermal_zone_device * tz ,
struct thermal_cooling_device * cdev ,
int trip ) ;
2012-09-18 11:04:53 +05:30
/*
* This structure is used to describe the behavior of
* a certain cooling device on a certain trip point
* in a certain thermal zone
*/
struct thermal_instance {
int id ;
char name [ THERMAL_NAME_LENGTH ] ;
struct thermal_zone_device * tz ;
struct thermal_cooling_device * cdev ;
2023-09-21 19:52:44 +02:00
const struct thermal_trip * trip ;
2015-10-30 16:31:47 +08:00
bool initialized ;
2012-09-18 11:04:53 +05:30
unsigned long upper ; /* Highest cooling state for this trip point */
unsigned long lower ; /* Lowest cooling state for this trip point */
unsigned long target ; /* expected cooling state */
char attr_name [ THERMAL_NAME_LENGTH ] ;
struct device_attribute attr ;
2015-02-18 16:04:24 +00:00
char weight_attr_name [ THERMAL_NAME_LENGTH ] ;
struct device_attribute weight_attr ;
2012-09-18 11:04:53 +05:30
struct list_head tz_node ; /* node in tz->thermal_instances */
struct list_head cdev_node ; /* node in cdev->thermal_instances */
2015-02-18 16:04:21 +00:00
unsigned int weight ; /* The weight of the cooling device */
2023-03-17 18:01:26 +01:00
bool upper_no_limit ;
2012-09-18 11:04:53 +05:30
} ;
2016-11-07 21:08:57 -08:00
# define to_thermal_zone(_dev) \
container_of ( _dev , struct thermal_zone_device , device )
2016-11-07 21:09:01 -08:00
# define to_cooling_device(_dev) \
container_of ( _dev , struct thermal_cooling_device , device )
2013-03-26 16:38:29 +08:00
int thermal_register_governor ( struct thermal_governor * ) ;
void thermal_unregister_governor ( struct thermal_governor * ) ;
2016-11-07 21:08:55 -08:00
int thermal_zone_device_set_policy ( struct thermal_zone_device * , char * ) ;
2016-11-07 21:08:56 -08:00
int thermal_build_list_of_policies ( char * buf ) ;
2022-11-10 07:24:58 -08:00
void __thermal_zone_device_update ( struct thermal_zone_device * tz ,
enum thermal_notify_event event ) ;
2023-11-29 09:43:29 -03:00
void thermal_zone_device_critical_reboot ( struct thermal_zone_device * tz ) ;
2023-12-20 23:17:45 +00:00
void thermal_governor_update_tz ( struct thermal_zone_device * tz ,
enum thermal_notify_event reason ) ;
2013-03-26 16:38:29 +08:00
2020-03-31 18:54:48 +02:00
/* Helpers */
2024-04-02 20:56:43 +02:00
# define for_each_trip_desc(__tz, __td) \
for ( __td = __tz - > trips ; __td - __tz - > trips < __tz - > num_trips ; __td + + )
# define trip_to_trip_desc(__trip) \
container_of ( __trip , struct thermal_trip_desc , trip )
2023-10-12 20:26:46 +02:00
2022-08-05 17:38:34 +02:00
void __thermal_zone_set_trips ( struct thermal_zone_device * tz ) ;
2023-12-15 20:53:52 +01:00
int thermal_zone_trip_id ( const struct thermal_zone_device * tz ,
2023-09-21 19:52:44 +02:00
const struct thermal_trip * trip ) ;
2023-12-05 13:24:08 +01:00
void thermal_zone_trip_updated ( struct thermal_zone_device * tz ,
const struct thermal_trip * trip ) ;
2022-08-05 17:38:34 +02:00
int __thermal_zone_get_temp ( struct thermal_zone_device * tz , int * temp ) ;
2020-03-31 18:54:48 +02:00
2016-11-07 21:09:00 -08:00
/* sysfs I/F */
2024-02-22 18:30:49 +01:00
int thermal_zone_create_device_groups ( struct thermal_zone_device * tz ) ;
2017-08-08 16:39:52 +02:00
void thermal_zone_destroy_device_groups ( struct thermal_zone_device * ) ;
2016-11-07 21:09:02 -08:00
void thermal_cooling_device_setup_sysfs ( struct thermal_cooling_device * ) ;
2018-04-02 16:26:25 +05:30
void thermal_cooling_device_destroy_sysfs ( struct thermal_cooling_device * cdev ) ;
2023-03-17 18:01:26 +01:00
void thermal_cooling_device_stats_reinit ( struct thermal_cooling_device * cdev ) ;
2016-11-07 21:09:02 -08:00
/* used only at binding time */
2018-04-03 15:19:03 +05:30
ssize_t trip_point_show ( struct device * , struct device_attribute * , char * ) ;
ssize_t weight_show ( struct device * , struct device_attribute * , char * ) ;
ssize_t weight_store ( struct device * , struct device_attribute * , const char * ,
size_t ) ;
2016-11-07 21:09:00 -08:00
2018-04-02 16:26:25 +05:30
# ifdef CONFIG_THERMAL_STATISTICS
void thermal_cooling_device_stats_update ( struct thermal_cooling_device * cdev ,
unsigned long new_state ) ;
# else
static inline void
thermal_cooling_device_stats_update ( struct thermal_cooling_device * cdev ,
unsigned long new_state ) { }
# endif /* CONFIG_THERMAL_STATISTICS */
2013-07-03 15:35:39 -04:00
/* device tree support */
2020-07-03 12:43:54 +02:00
int thermal_zone_device_is_enabled ( struct thermal_zone_device * tz ) ;
2012-09-18 11:04:53 +05:30
# endif /* __THERMAL_CORE_H__ */