Commit 9396b2a6 authored by Rafael J. Wysocki's avatar Rafael J. Wysocki
Browse files

Merge branch 'thermal-core'

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()
  ...
parents 00211025 042a3d80
Loading
Loading
Loading
Loading
+38 −59
Original line number Diff line number Diff line
@@ -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:
@@ -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);
+46 −35
Original line number Diff line number Diff line
@@ -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);
	}
}

	return 0;
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;

		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,
	.manage	= fair_share_manage,
};
THERMAL_GOVERNOR_DECLARE(thermal_gov_fair_share);
+14 −25
Original line number Diff line number Diff line
@@ -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.
@@ -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;
@@ -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;
@@ -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);
@@ -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;
}

/**
@@ -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;
@@ -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);
@@ -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);
+32 −37
Original line number Diff line number Diff line
@@ -62,81 +62,76 @@ static unsigned long get_target_state(struct thermal_instance *instance,
}

static void thermal_zone_trip_update(struct thermal_zone_device *tz,
				     const struct thermal_trip *trip)
				     const struct thermal_trip *trip,
				     int trip_threshold)
{
	enum thermal_trend trend = get_tz_trend(tz, trip);
	int trip_id = thermal_zone_trip_id(tz, trip);
	enum thermal_trend trend;
	struct thermal_instance *instance;
	bool throttle = false;
	int old_target;

	trend = get_tz_trend(tz, trip);

	if (tz->temperature >= trip->temperature) {
	if (tz->temperature >= trip_threshold) {
		throttle = true;
		trace_thermal_zone_trip(tz, trip_id, trip->type);
	}

	dev_dbg(&tz->device, "Trip%d[type=%d,temp=%d]:trend=%d,throttle=%d\n",
		trip_id, trip->type, trip->temperature, trend, throttle);
		trip_id, trip->type, trip_threshold, trend, throttle);

	list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
		int old_target;

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

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

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

		if (instance->initialized && old_target == instance->target)
			continue;

		if (trip->type == THERMAL_TRIP_PASSIVE) {
			/* If needed, update the status of passive polling. */
			if (old_target == THERMAL_NO_TARGET &&
			    instance->target != THERMAL_NO_TARGET)
				tz->passive++;
			else if (old_target != THERMAL_NO_TARGET &&
				 instance->target == THERMAL_NO_TARGET)
				tz->passive--;
		}

		instance->initialized = true;

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

/**
 * step_wise_throttle - throttles devices associated with the given zone
 * @tz: thermal_zone_device
 * @trip: trip point
 *
 * Throttling Logic: This uses the trend of the thermal zone to throttle.
 * If the thermal zone is 'heating up' this throttles all the cooling
 * devices associated with the zone and its particular trip point, by one
 * step. If the zone is 'cooling down' it brings back the performance of
 * the devices by one step.
 */
static int step_wise_throttle(struct thermal_zone_device *tz,
			      const struct thermal_trip *trip)
static void step_wise_manage(struct thermal_zone_device *tz)
{
	const struct thermal_trip_desc *td;
	struct thermal_instance *instance;

	lockdep_assert_held(&tz->lock);

	thermal_zone_trip_update(tz, trip);
	/*
	 * Throttling Logic: Use the trend of the thermal zone to throttle.
	 * If the thermal zone is 'heating up', throttle all of the cooling
	 * devices associated with each trip point by one step. If the zone
	 * is 'cooling down', it brings back the performance of the devices
	 * by one step.
	 */
	for_each_trip_desc(tz, td) {
		const struct thermal_trip *trip = &td->trip;

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

		thermal_zone_trip_update(tz, trip, td->threshold);
	}

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

	return 0;
}

static struct thermal_governor thermal_gov_step_wise = {
	.name	= "step_wise",
	.throttle	= step_wise_throttle,
	.manage	= step_wise_manage,
};
THERMAL_GOVERNOR_DECLARE(thermal_gov_step_wise);
+5 −5
Original line number Diff line number Diff line
@@ -26,11 +26,13 @@ static int user_space_bind(struct thermal_zone_device *tz)
 * notify_user_space - Notifies user space about thermal events
 * @tz: thermal_zone_device
 * @trip: trip point
 * @crossed_up: whether or not the trip has been crossed on the way up
 *
 * This function notifies the user space through UEvents.
 */
static int notify_user_space(struct thermal_zone_device *tz,
			     const struct thermal_trip *trip)
static void notify_user_space(struct thermal_zone_device *tz,
			      const struct thermal_trip *trip,
			      bool crossed_up)
{
	char *thermal_prop[5];
	int i;
@@ -46,13 +48,11 @@ static int notify_user_space(struct thermal_zone_device *tz,
	kobject_uevent_env(&tz->device.kobj, KOBJ_CHANGE, thermal_prop);
	for (i = 0; i < 4; ++i)
		kfree(thermal_prop[i]);

	return 0;
}

static struct thermal_governor thermal_gov_user_space = {
	.name		= "user_space",
	.throttle	= notify_user_space,
	.trip_crossed	= notify_user_space,
	.bind_to_tz	= user_space_bind,
};
THERMAL_GOVERNOR_DECLARE(thermal_gov_user_space);
Loading