Skip to content

Commit

Permalink
Merge branch 'thermal-core'
Browse files Browse the repository at this point in the history
This includes a major rework of thermal governors and part of the
thermal core interacting with them as well as some fixes and cleanups
of the thermal debug code:

 - Redesign the thermal governor interface to allow the governors to
   work in a more straightforward way.

 - Make thermal governors take the current trip point thresholds into
   account in their computations which allows trip hysteresis to be
   observed more accurately.

 - Clean up thermal governors.

 - Make the thermal core manage passive polling for thermal zones and
   remove passive polling management from thermal governors.

 - Improve the handling of cooling device states and thermal mitigation
   episodes in progress in the thermal debug code.

 - Avoid excessive updates of trip point statistics and clean up the
   printing of thermal mitigation episode information.

* thermal-core: (27 commits)
  thermal: core: Move passive polling management to the core
  thermal: core: Do not call handle_thermal_trip() if zone temperature is invalid
  thermal: trip: Add missing empty code line
  thermal/debugfs: Avoid printing zero duration for mitigation events in progress
  thermal/debugfs: Pass cooling device state to thermal_debug_cdev_add()
  thermal/debugfs: Create records for cdev states as they get used
  thermal: core: Introduce thermal_governor_trip_crossed()
  thermal/debugfs: Make tze_seq_show() skip invalid trips and trips with no stats
  thermal/debugfs: Rename thermal_debug_update_temp() to thermal_debug_update_trip_stats()
  thermal/debugfs: Clean up thermal_debug_update_temp()
  thermal/debugfs: Avoid excessive updates of trip point statistics
  thermal: core: Relocate critical and hot trip handling
  thermal: core: Drop the .throttle() governor callback
  thermal: gov_user_space: Use .trip_crossed() instead of .throttle()
  thermal: gov_fair_share: Eliminate unnecessary integer divisions
  thermal: gov_fair_share: Use trip thresholds instead of trip temperatures
  thermal: gov_fair_share: Use .manage() callback instead of .throttle()
  thermal: gov_step_wise: Clean up thermal_zone_trip_update()
  thermal: gov_step_wise: Use trip thresholds instead of trip temperatures
  thermal: gov_step_wise: Use .manage() callback instead of .throttle()
  ...
  • Loading branch information
Rafael J. Wysocki committed May 6, 2024
2 parents 0021102 + 042a3d8 commit 9396b2a
Show file tree
Hide file tree
Showing 10 changed files with 268 additions and 216 deletions.
97 changes: 38 additions & 59 deletions drivers/thermal/gov_bang_bang.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,60 +13,11 @@

#include "thermal_core.h"

static int thermal_zone_trip_update(struct thermal_zone_device *tz,
const struct thermal_trip *trip)
{
int trip_index = thermal_zone_trip_id(tz, trip);
struct thermal_instance *instance;

if (!trip->hysteresis)
dev_info_once(&tz->device,
"Zero hysteresis value for thermal zone %s\n", tz->type);

dev_dbg(&tz->device, "Trip%d[temp=%d]:temp=%d:hyst=%d\n",
trip_index, trip->temperature, tz->temperature,
trip->hysteresis);

list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
if (instance->trip != trip)
continue;

/* in case fan is in initial state, switch the fan off */
if (instance->target == THERMAL_NO_TARGET)
instance->target = 0;

/* in case fan is neither on nor off set the fan to active */
if (instance->target != 0 && instance->target != 1) {
pr_warn("Thermal instance %s controlled by bang-bang has unexpected state: %ld\n",
instance->name, instance->target);
instance->target = 1;
}

/*
* enable fan when temperature exceeds trip_temp and disable
* the fan in case it falls below trip_temp minus hysteresis
*/
if (instance->target == 0 && tz->temperature >= trip->temperature)
instance->target = 1;
else if (instance->target == 1 &&
tz->temperature < trip->temperature - trip->hysteresis)
instance->target = 0;

dev_dbg(&instance->cdev->device, "target=%d\n",
(int)instance->target);

mutex_lock(&instance->cdev->lock);
instance->cdev->updated = false; /* cdev needs update */
mutex_unlock(&instance->cdev->lock);
}

return 0;
}

/**
* bang_bang_control - controls devices associated with the given zone
* @tz: thermal_zone_device
* @trip: the trip point
* @crossed_up: whether or not the trip has been crossed on the way up
*
* Regulation Logic: a two point regulation, deliver cooling state depending
* on the previous state shown in this diagram:
Expand All @@ -90,26 +41,54 @@ static int thermal_zone_trip_update(struct thermal_zone_device *tz,
* (trip_temp - hyst) so that the fan gets turned off again.
*
*/
static int bang_bang_control(struct thermal_zone_device *tz,
const struct thermal_trip *trip)
static void bang_bang_control(struct thermal_zone_device *tz,
const struct thermal_trip *trip,
bool crossed_up)
{
struct thermal_instance *instance;
int ret;

lockdep_assert_held(&tz->lock);

ret = thermal_zone_trip_update(tz, trip);
if (ret)
return ret;
dev_dbg(&tz->device, "Trip%d[temp=%d]:temp=%d:hyst=%d\n",
thermal_zone_trip_id(tz, trip), trip->temperature,
tz->temperature, trip->hysteresis);

list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
if (instance->trip != trip)
continue;

if (instance->target == THERMAL_NO_TARGET)
instance->target = 0;

if (instance->target != 0 && instance->target != 1) {
pr_debug("Unexpected state %ld of thermal instance %s in bang-bang\n",
instance->target, instance->name);

instance->target = 1;
}

/*
* Enable the fan when the trip is crossed on the way up and
* disable it when the trip is crossed on the way down.
*/
if (instance->target == 0 && crossed_up)
instance->target = 1;
else if (instance->target == 1 && !crossed_up)
instance->target = 0;

dev_dbg(&instance->cdev->device, "target=%ld\n", instance->target);

mutex_lock(&instance->cdev->lock);
instance->cdev->updated = false; /* cdev needs update */
mutex_unlock(&instance->cdev->lock);
}

list_for_each_entry(instance, &tz->thermal_instances, tz_node)
thermal_cdev_update(instance->cdev);

return 0;
}

static struct thermal_governor thermal_gov_bang_bang = {
.name = "bang_bang",
.throttle = bang_bang_control,
.trip_crossed = bang_bang_control,
};
THERMAL_GOVERNOR_DECLARE(thermal_gov_bang_bang);
81 changes: 46 additions & 35 deletions drivers/thermal/gov_fair_share.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,100 +17,111 @@

static int get_trip_level(struct thermal_zone_device *tz)
{
const struct thermal_trip *level_trip = NULL;
const struct thermal_trip_desc *level_td = NULL;
const struct thermal_trip_desc *td;
int trip_level = -1;

for_each_trip_desc(tz, td) {
const struct thermal_trip *trip = &td->trip;

if (trip->temperature >= tz->temperature)
if (td->threshold > tz->temperature)
continue;

trip_level++;

if (!level_trip || trip->temperature > level_trip->temperature)
level_trip = trip;
if (!level_td || td->threshold > level_td->threshold)
level_td = td;
}

/* Bail out if the temperature is not greater than any trips. */
if (trip_level < 0)
return 0;

trace_thermal_zone_trip(tz, thermal_zone_trip_id(tz, level_trip),
level_trip->type);
trace_thermal_zone_trip(tz, thermal_zone_trip_id(tz, &level_td->trip),
level_td->trip.type);

return trip_level;
}

static long get_target_state(struct thermal_zone_device *tz,
struct thermal_cooling_device *cdev, int percentage, int level)
{
return (long)(percentage * level * cdev->max_state) / (100 * tz->num_trips);
}

/**
* fair_share_throttle - throttles devices associated with the given zone
* @tz: thermal_zone_device
* @trip: trip point
* @trip_level: number of trips crossed by the zone temperature
*
* Throttling Logic: This uses three parameters to calculate the new
* throttle state of the cooling devices associated with the given zone.
*
* Parameters used for Throttling:
* P1. max_state: Maximum throttle state exposed by the cooling device.
* P2. percentage[i]/100:
* P2. weight[i]/total_weight:
* How 'effective' the 'i'th device is, in cooling the given zone.
* P3. cur_trip_level/max_no_of_trips:
* P3. trip_level/max_no_of_trips:
* This describes the extent to which the devices should be throttled.
* We do not want to throttle too much when we trip a lower temperature,
* whereas the throttling is at full swing if we trip critical levels.
* (Heavily assumes the trip points are in ascending order)
* new_state of cooling device = P3 * P2 * P1
*/
static int fair_share_throttle(struct thermal_zone_device *tz,
const struct thermal_trip *trip)
static void fair_share_throttle(struct thermal_zone_device *tz,
const struct thermal_trip *trip,
int trip_level)
{
struct thermal_instance *instance;
int total_weight = 0;
int total_instance = 0;
int cur_trip_level = get_trip_level(tz);

lockdep_assert_held(&tz->lock);
int nr_instances = 0;

list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
if (instance->trip != trip)
continue;

total_weight += instance->weight;
total_instance++;
nr_instances++;
}

list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
int percentage;
struct thermal_cooling_device *cdev = instance->cdev;
u64 dividend;
u32 divisor;

if (instance->trip != trip)
continue;

if (!total_weight)
percentage = 100 / total_instance;
else
percentage = (instance->weight * 100) / total_weight;

instance->target = get_target_state(tz, cdev, percentage,
cur_trip_level);
dividend = trip_level;
dividend *= cdev->max_state;
divisor = tz->num_trips;
if (total_weight) {
dividend *= instance->weight;
divisor *= total_weight;
} else {
divisor *= nr_instances;
}
instance->target = div_u64(dividend, divisor);

mutex_lock(&cdev->lock);
__thermal_cdev_update(cdev);
mutex_unlock(&cdev->lock);
}
}

static void fair_share_manage(struct thermal_zone_device *tz)
{
int trip_level = get_trip_level(tz);
const struct thermal_trip_desc *td;

lockdep_assert_held(&tz->lock);

for_each_trip_desc(tz, td) {
const struct thermal_trip *trip = &td->trip;

return 0;
if (trip->temperature == THERMAL_TEMP_INVALID ||
trip->type == THERMAL_TRIP_CRITICAL ||
trip->type == THERMAL_TRIP_HOT)
continue;

fair_share_throttle(tz, trip, trip_level);
}
}

static struct thermal_governor thermal_gov_fair_share = {
.name = "fair_share",
.throttle = fair_share_throttle,
.name = "fair_share",
.manage = fair_share_manage,
};
THERMAL_GOVERNOR_DECLARE(thermal_gov_fair_share);
39 changes: 14 additions & 25 deletions drivers/thermal/gov_power_allocator.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ struct power_actor {
* struct power_allocator_params - parameters for the power allocator governor
* @allocated_tzp: whether we have allocated tzp for this thermal zone and
* it needs to be freed on unbind
* @update_cdevs: whether or not update cdevs on the next run
* @err_integral: accumulated error in the PID controller.
* @prev_err: error in the previous iteration of the PID controller.
* Used to calculate the derivative term.
Expand All @@ -84,6 +85,7 @@ struct power_actor {
*/
struct power_allocator_params {
bool allocated_tzp;
bool update_cdevs;
s64 err_integral;
s32 prev_err;
u32 sustainable_power;
Expand Down Expand Up @@ -395,7 +397,7 @@ static void divvy_up_power(struct power_actor *power, int num_actors,
}
}

static int allocate_power(struct thermal_zone_device *tz, int control_temp)
static void allocate_power(struct thermal_zone_device *tz, int control_temp)
{
struct power_allocator_params *params = tz->governor_data;
unsigned int num_actors = params->num_actors;
Expand All @@ -410,7 +412,7 @@ static int allocate_power(struct thermal_zone_device *tz, int control_temp)
int i = 0, ret;

if (!num_actors)
return -ENODEV;
return;

/* Clean all buffers for new power estimations */
memset(power, 0, params->buffer_size);
Expand Down Expand Up @@ -471,8 +473,6 @@ static int allocate_power(struct thermal_zone_device *tz, int control_temp)
num_actors, power_range,
max_allocatable_power, tz->temperature,
control_temp - tz->temperature);

return 0;
}

/**
Expand Down Expand Up @@ -535,7 +535,7 @@ static void reset_pid_controller(struct power_allocator_params *params)
params->prev_err = 0;
}

static void allow_maximum_power(struct thermal_zone_device *tz, bool update)
static void allow_maximum_power(struct thermal_zone_device *tz)
{
struct power_allocator_params *params = tz->governor_data;
struct thermal_cooling_device *cdev;
Expand All @@ -557,7 +557,7 @@ static void allow_maximum_power(struct thermal_zone_device *tz, bool update)
*/
cdev->ops->get_requested_power(cdev, &req_power);

if (update)
if (params->update_cdevs)
__thermal_cdev_update(cdev);

mutex_unlock(&cdev->lock);
Expand Down Expand Up @@ -745,40 +745,29 @@ static void power_allocator_unbind(struct thermal_zone_device *tz)
tz->governor_data = NULL;
}

static int power_allocator_throttle(struct thermal_zone_device *tz,
const struct thermal_trip *trip)
static void power_allocator_manage(struct thermal_zone_device *tz)
{
struct power_allocator_params *params = tz->governor_data;
bool update;
const struct thermal_trip *trip = params->trip_switch_on;

lockdep_assert_held(&tz->lock);

/*
* We get called for every trip point but we only need to do
* our calculations once
*/
if (trip != params->trip_max)
return 0;

trip = params->trip_switch_on;
if (trip && tz->temperature < trip->temperature) {
update = tz->passive;
tz->passive = 0;
reset_pid_controller(params);
allow_maximum_power(tz, update);
return 0;
allow_maximum_power(tz);
params->update_cdevs = false;
return;
}

tz->passive = 1;

return allocate_power(tz, params->trip_max->temperature);
allocate_power(tz, params->trip_max->temperature);
params->update_cdevs = true;
}

static struct thermal_governor thermal_gov_power_allocator = {
.name = "power_allocator",
.bind_to_tz = power_allocator_bind,
.unbind_from_tz = power_allocator_unbind,
.throttle = power_allocator_throttle,
.manage = power_allocator_manage,
.update_tz = power_allocator_update_tz,
};
THERMAL_GOVERNOR_DECLARE(thermal_gov_power_allocator);
Loading

0 comments on commit 9396b2a

Please sign in to comment.