10de967f2SLukasz Luba // SPDX-License-Identifier: GPL-2.0
20015d9a2SAmit Kucheria /*
30015d9a2SAmit Kucheria  * A power allocator to manage temperature
40015d9a2SAmit Kucheria  *
50015d9a2SAmit Kucheria  * Copyright (C) 2014 ARM Ltd.
60015d9a2SAmit Kucheria  *
70015d9a2SAmit Kucheria  */
80015d9a2SAmit Kucheria 
90015d9a2SAmit Kucheria #define pr_fmt(fmt) "Power allocator: " fmt
100015d9a2SAmit Kucheria 
110015d9a2SAmit Kucheria #include <linux/slab.h>
120015d9a2SAmit Kucheria #include <linux/thermal.h>
130015d9a2SAmit Kucheria 
140015d9a2SAmit Kucheria #define CREATE_TRACE_POINTS
1532a7a021SDaniel Lezcano #include "thermal_trace_ipa.h"
160015d9a2SAmit Kucheria 
170015d9a2SAmit Kucheria #include "thermal_core.h"
180015d9a2SAmit Kucheria 
190015d9a2SAmit Kucheria #define INVALID_TRIP -1
200015d9a2SAmit Kucheria 
210015d9a2SAmit Kucheria #define FRAC_BITS 10
220015d9a2SAmit Kucheria #define int_to_frac(x) ((x) << FRAC_BITS)
230015d9a2SAmit Kucheria #define frac_to_int(x) ((x) >> FRAC_BITS)
240015d9a2SAmit Kucheria 
250015d9a2SAmit Kucheria /**
260015d9a2SAmit Kucheria  * mul_frac() - multiply two fixed-point numbers
270015d9a2SAmit Kucheria  * @x:	first multiplicand
280015d9a2SAmit Kucheria  * @y:	second multiplicand
290015d9a2SAmit Kucheria  *
300015d9a2SAmit Kucheria  * Return: the result of multiplying two fixed-point numbers.  The
310015d9a2SAmit Kucheria  * result is also a fixed-point number.
320015d9a2SAmit Kucheria  */
mul_frac(s64 x,s64 y)330015d9a2SAmit Kucheria static inline s64 mul_frac(s64 x, s64 y)
340015d9a2SAmit Kucheria {
350015d9a2SAmit Kucheria 	return (x * y) >> FRAC_BITS;
360015d9a2SAmit Kucheria }
370015d9a2SAmit Kucheria 
380015d9a2SAmit Kucheria /**
390015d9a2SAmit Kucheria  * div_frac() - divide two fixed-point numbers
400015d9a2SAmit Kucheria  * @x:	the dividend
410015d9a2SAmit Kucheria  * @y:	the divisor
420015d9a2SAmit Kucheria  *
430015d9a2SAmit Kucheria  * Return: the result of dividing two fixed-point numbers.  The
440015d9a2SAmit Kucheria  * result is also a fixed-point number.
450015d9a2SAmit Kucheria  */
div_frac(s64 x,s64 y)460015d9a2SAmit Kucheria static inline s64 div_frac(s64 x, s64 y)
470015d9a2SAmit Kucheria {
480015d9a2SAmit Kucheria 	return div_s64(x << FRAC_BITS, y);
490015d9a2SAmit Kucheria }
500015d9a2SAmit Kucheria 
510015d9a2SAmit Kucheria /**
520015d9a2SAmit Kucheria  * struct power_allocator_params - parameters for the power allocator governor
530015d9a2SAmit Kucheria  * @allocated_tzp:	whether we have allocated tzp for this thermal zone and
540015d9a2SAmit Kucheria  *			it needs to be freed on unbind
550015d9a2SAmit Kucheria  * @err_integral:	accumulated error in the PID controller.
560015d9a2SAmit Kucheria  * @prev_err:	error in the previous iteration of the PID controller.
570015d9a2SAmit Kucheria  *		Used to calculate the derivative term.
580015d9a2SAmit Kucheria  * @trip_switch_on:	first passive trip point of the thermal zone.  The
590015d9a2SAmit Kucheria  *			governor switches on when this trip point is crossed.
600015d9a2SAmit Kucheria  *			If the thermal zone only has one passive trip point,
610015d9a2SAmit Kucheria  *			@trip_switch_on should be INVALID_TRIP.
620015d9a2SAmit Kucheria  * @trip_max_desired_temperature:	last passive trip point of the thermal
630015d9a2SAmit Kucheria  *					zone.  The temperature we are
640015d9a2SAmit Kucheria  *					controlling for.
65eda1ecfaSLukasz Luba  * @sustainable_power:	Sustainable power (heat) that this thermal zone can
66eda1ecfaSLukasz Luba  *			dissipate
670015d9a2SAmit Kucheria  */
680015d9a2SAmit Kucheria struct power_allocator_params {
690015d9a2SAmit Kucheria 	bool allocated_tzp;
700015d9a2SAmit Kucheria 	s64 err_integral;
710015d9a2SAmit Kucheria 	s32 prev_err;
720015d9a2SAmit Kucheria 	int trip_switch_on;
730015d9a2SAmit Kucheria 	int trip_max_desired_temperature;
74eda1ecfaSLukasz Luba 	u32 sustainable_power;
750015d9a2SAmit Kucheria };
760015d9a2SAmit Kucheria 
770015d9a2SAmit Kucheria /**
780015d9a2SAmit Kucheria  * estimate_sustainable_power() - Estimate the sustainable power of a thermal zone
790015d9a2SAmit Kucheria  * @tz: thermal zone we are operating in
800015d9a2SAmit Kucheria  *
810015d9a2SAmit Kucheria  * For thermal zones that don't provide a sustainable_power in their
820015d9a2SAmit Kucheria  * thermal_zone_params, estimate one.  Calculate it using the minimum
830015d9a2SAmit Kucheria  * power of all the cooling devices as that gives a valid value that
840015d9a2SAmit Kucheria  * can give some degree of functionality.  For optimal performance of
850015d9a2SAmit Kucheria  * this governor, provide a sustainable_power in the thermal zone's
860015d9a2SAmit Kucheria  * thermal_zone_params.
870015d9a2SAmit Kucheria  */
estimate_sustainable_power(struct thermal_zone_device * tz)880015d9a2SAmit Kucheria static u32 estimate_sustainable_power(struct thermal_zone_device *tz)
890015d9a2SAmit Kucheria {
900015d9a2SAmit Kucheria 	u32 sustainable_power = 0;
910015d9a2SAmit Kucheria 	struct thermal_instance *instance;
920015d9a2SAmit Kucheria 	struct power_allocator_params *params = tz->governor_data;
9377451ef5SRafael J. Wysocki 	const struct thermal_trip *trip_max_desired_temperature =
9477451ef5SRafael J. Wysocki 			&tz->trips[params->trip_max_desired_temperature];
950015d9a2SAmit Kucheria 
960015d9a2SAmit Kucheria 	list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
970015d9a2SAmit Kucheria 		struct thermal_cooling_device *cdev = instance->cdev;
980015d9a2SAmit Kucheria 		u32 min_power;
990015d9a2SAmit Kucheria 
10077451ef5SRafael J. Wysocki 		if (instance->trip != trip_max_desired_temperature)
1010015d9a2SAmit Kucheria 			continue;
1020015d9a2SAmit Kucheria 
1038132df3aSLukasz Luba 		if (!cdev_is_power_actor(cdev))
1048132df3aSLukasz Luba 			continue;
1058132df3aSLukasz Luba 
1068132df3aSLukasz Luba 		if (cdev->ops->state2power(cdev, instance->upper, &min_power))
1070015d9a2SAmit Kucheria 			continue;
1080015d9a2SAmit Kucheria 
1090015d9a2SAmit Kucheria 		sustainable_power += min_power;
1100015d9a2SAmit Kucheria 	}
1110015d9a2SAmit Kucheria 
1120015d9a2SAmit Kucheria 	return sustainable_power;
1130015d9a2SAmit Kucheria }
1140015d9a2SAmit Kucheria 
1150015d9a2SAmit Kucheria /**
1160015d9a2SAmit Kucheria  * estimate_pid_constants() - Estimate the constants for the PID controller
1170015d9a2SAmit Kucheria  * @tz:		thermal zone for which to estimate the constants
1180015d9a2SAmit Kucheria  * @sustainable_power:	sustainable power for the thermal zone
1190015d9a2SAmit Kucheria  * @trip_switch_on:	trip point number for the switch on temperature
1200015d9a2SAmit Kucheria  * @control_temp:	target temperature for the power allocator governor
1210015d9a2SAmit Kucheria  *
1220015d9a2SAmit Kucheria  * This function is used to update the estimation of the PID
1230015d9a2SAmit Kucheria  * controller constants in struct thermal_zone_parameters.
1240015d9a2SAmit Kucheria  */
estimate_pid_constants(struct thermal_zone_device * tz,u32 sustainable_power,int trip_switch_on,int control_temp)1250015d9a2SAmit Kucheria static void estimate_pid_constants(struct thermal_zone_device *tz,
1260015d9a2SAmit Kucheria 				   u32 sustainable_power, int trip_switch_on,
12790a99654SLukasz Luba 				   int control_temp)
1280015d9a2SAmit Kucheria {
1297f725a23SDaniel Lezcano 	struct thermal_trip trip;
1307f725a23SDaniel Lezcano 	u32 temperature_threshold = control_temp;
1310015d9a2SAmit Kucheria 	int ret;
132e34a7233SLukasz Luba 	s32 k_i;
1330015d9a2SAmit Kucheria 
1347f725a23SDaniel Lezcano 	ret = __thermal_zone_get_trip(tz, trip_switch_on, &trip);
1357f725a23SDaniel Lezcano 	if (!ret)
1367f725a23SDaniel Lezcano 		temperature_threshold -= trip.temperature;
1370015d9a2SAmit Kucheria 
1380015d9a2SAmit Kucheria 	/*
1390015d9a2SAmit Kucheria 	 * estimate_pid_constants() tries to find appropriate default
1400015d9a2SAmit Kucheria 	 * values for thermal zones that don't provide them. If a
1410015d9a2SAmit Kucheria 	 * system integrator has configured a thermal zone with two
1420015d9a2SAmit Kucheria 	 * passive trip points at the same temperature, that person
1430015d9a2SAmit Kucheria 	 * hasn't put any effort to set up the thermal zone properly
1440015d9a2SAmit Kucheria 	 * so just give up.
1450015d9a2SAmit Kucheria 	 */
1460015d9a2SAmit Kucheria 	if (!temperature_threshold)
1470015d9a2SAmit Kucheria 		return;
1480015d9a2SAmit Kucheria 
1490015d9a2SAmit Kucheria 	tz->tzp->k_po = int_to_frac(sustainable_power) /
1500015d9a2SAmit Kucheria 		temperature_threshold;
1510015d9a2SAmit Kucheria 
1520015d9a2SAmit Kucheria 	tz->tzp->k_pu = int_to_frac(2 * sustainable_power) /
1530015d9a2SAmit Kucheria 		temperature_threshold;
1540015d9a2SAmit Kucheria 
155e34a7233SLukasz Luba 	k_i = tz->tzp->k_pu / 10;
156e34a7233SLukasz Luba 	tz->tzp->k_i = k_i > 0 ? k_i : 1;
157e34a7233SLukasz Luba 
1580015d9a2SAmit Kucheria 	/*
1590015d9a2SAmit Kucheria 	 * The default for k_d and integral_cutoff is 0, so we can
1600015d9a2SAmit Kucheria 	 * leave them as they are.
1610015d9a2SAmit Kucheria 	 */
1620015d9a2SAmit Kucheria }
1630015d9a2SAmit Kucheria 
1640015d9a2SAmit Kucheria /**
165eda1ecfaSLukasz Luba  * get_sustainable_power() - Get the right sustainable power
166eda1ecfaSLukasz Luba  * @tz:		thermal zone for which to estimate the constants
167eda1ecfaSLukasz Luba  * @params:	parameters for the power allocator governor
168eda1ecfaSLukasz Luba  * @control_temp:	target temperature for the power allocator governor
169eda1ecfaSLukasz Luba  *
170eda1ecfaSLukasz Luba  * This function is used for getting the proper sustainable power value based
171eda1ecfaSLukasz Luba  * on variables which might be updated by the user sysfs interface. If that
172eda1ecfaSLukasz Luba  * happen the new value is going to be estimated and updated. It is also used
173eda1ecfaSLukasz Luba  * after thermal zone binding, where the initial values where set to 0.
174eda1ecfaSLukasz Luba  */
get_sustainable_power(struct thermal_zone_device * tz,struct power_allocator_params * params,int control_temp)175eda1ecfaSLukasz Luba static u32 get_sustainable_power(struct thermal_zone_device *tz,
176eda1ecfaSLukasz Luba 				 struct power_allocator_params *params,
177eda1ecfaSLukasz Luba 				 int control_temp)
178eda1ecfaSLukasz Luba {
179eda1ecfaSLukasz Luba 	u32 sustainable_power;
180eda1ecfaSLukasz Luba 
181eda1ecfaSLukasz Luba 	if (!tz->tzp->sustainable_power)
182eda1ecfaSLukasz Luba 		sustainable_power = estimate_sustainable_power(tz);
183eda1ecfaSLukasz Luba 	else
184eda1ecfaSLukasz Luba 		sustainable_power = tz->tzp->sustainable_power;
185eda1ecfaSLukasz Luba 
186eda1ecfaSLukasz Luba 	/* Check if it's init value 0 or there was update via sysfs */
187eda1ecfaSLukasz Luba 	if (sustainable_power != params->sustainable_power) {
188eda1ecfaSLukasz Luba 		estimate_pid_constants(tz, sustainable_power,
18990a99654SLukasz Luba 				       params->trip_switch_on, control_temp);
190eda1ecfaSLukasz Luba 
191eda1ecfaSLukasz Luba 		/* Do the estimation only once and make available in sysfs */
192eda1ecfaSLukasz Luba 		tz->tzp->sustainable_power = sustainable_power;
193eda1ecfaSLukasz Luba 		params->sustainable_power = sustainable_power;
194eda1ecfaSLukasz Luba 	}
195eda1ecfaSLukasz Luba 
196eda1ecfaSLukasz Luba 	return sustainable_power;
197eda1ecfaSLukasz Luba }
198eda1ecfaSLukasz Luba 
199eda1ecfaSLukasz Luba /**
2000015d9a2SAmit Kucheria  * pid_controller() - PID controller
2010015d9a2SAmit Kucheria  * @tz:	thermal zone we are operating in
2020015d9a2SAmit Kucheria  * @control_temp:	the target temperature in millicelsius
2030015d9a2SAmit Kucheria  * @max_allocatable_power:	maximum allocatable power for this thermal zone
2040015d9a2SAmit Kucheria  *
2050015d9a2SAmit Kucheria  * This PID controller increases the available power budget so that the
2060015d9a2SAmit Kucheria  * temperature of the thermal zone gets as close as possible to
2070015d9a2SAmit Kucheria  * @control_temp and limits the power if it exceeds it.  k_po is the
2080015d9a2SAmit Kucheria  * proportional term when we are overshooting, k_pu is the
2090015d9a2SAmit Kucheria  * proportional term when we are undershooting.  integral_cutoff is a
2100015d9a2SAmit Kucheria  * threshold below which we stop accumulating the error.  The
2110015d9a2SAmit Kucheria  * accumulated error is only valid if the requested power will make
2120015d9a2SAmit Kucheria  * the system warmer.  If the system is mostly idle, there's no point
2130015d9a2SAmit Kucheria  * in accumulating positive error.
2140015d9a2SAmit Kucheria  *
2150015d9a2SAmit Kucheria  * Return: The power budget for the next period.
2160015d9a2SAmit Kucheria  */
pid_controller(struct thermal_zone_device * tz,int control_temp,u32 max_allocatable_power)2170015d9a2SAmit Kucheria static u32 pid_controller(struct thermal_zone_device *tz,
2180015d9a2SAmit Kucheria 			  int control_temp,
2190015d9a2SAmit Kucheria 			  u32 max_allocatable_power)
2200015d9a2SAmit Kucheria {
2210015d9a2SAmit Kucheria 	s64 p, i, d, power_range;
2220015d9a2SAmit Kucheria 	s32 err, max_power_frac;
2230015d9a2SAmit Kucheria 	u32 sustainable_power;
2240015d9a2SAmit Kucheria 	struct power_allocator_params *params = tz->governor_data;
2250015d9a2SAmit Kucheria 
2260015d9a2SAmit Kucheria 	max_power_frac = int_to_frac(max_allocatable_power);
2270015d9a2SAmit Kucheria 
228eda1ecfaSLukasz Luba 	sustainable_power = get_sustainable_power(tz, params, control_temp);
2290015d9a2SAmit Kucheria 
2300015d9a2SAmit Kucheria 	err = control_temp - tz->temperature;
2310015d9a2SAmit Kucheria 	err = int_to_frac(err);
2320015d9a2SAmit Kucheria 
2330015d9a2SAmit Kucheria 	/* Calculate the proportional term */
2340015d9a2SAmit Kucheria 	p = mul_frac(err < 0 ? tz->tzp->k_po : tz->tzp->k_pu, err);
2350015d9a2SAmit Kucheria 
2360015d9a2SAmit Kucheria 	/*
2370015d9a2SAmit Kucheria 	 * Calculate the integral term
2380015d9a2SAmit Kucheria 	 *
2390015d9a2SAmit Kucheria 	 * if the error is less than cut off allow integration (but
2400015d9a2SAmit Kucheria 	 * the integral is limited to max power)
2410015d9a2SAmit Kucheria 	 */
2420015d9a2SAmit Kucheria 	i = mul_frac(tz->tzp->k_i, params->err_integral);
2430015d9a2SAmit Kucheria 
2440015d9a2SAmit Kucheria 	if (err < int_to_frac(tz->tzp->integral_cutoff)) {
2450015d9a2SAmit Kucheria 		s64 i_next = i + mul_frac(tz->tzp->k_i, err);
2460015d9a2SAmit Kucheria 
2470015d9a2SAmit Kucheria 		if (abs(i_next) < max_power_frac) {
2480015d9a2SAmit Kucheria 			i = i_next;
2490015d9a2SAmit Kucheria 			params->err_integral += err;
2500015d9a2SAmit Kucheria 		}
2510015d9a2SAmit Kucheria 	}
2520015d9a2SAmit Kucheria 
2530015d9a2SAmit Kucheria 	/*
2540015d9a2SAmit Kucheria 	 * Calculate the derivative term
2550015d9a2SAmit Kucheria 	 *
2560015d9a2SAmit Kucheria 	 * We do err - prev_err, so with a positive k_d, a decreasing
2570015d9a2SAmit Kucheria 	 * error (i.e. driving closer to the line) results in less
2580015d9a2SAmit Kucheria 	 * power being applied, slowing down the controller)
2590015d9a2SAmit Kucheria 	 */
2600015d9a2SAmit Kucheria 	d = mul_frac(tz->tzp->k_d, err - params->prev_err);
261b39d2dd5SDaniel Lezcano 	d = div_frac(d, jiffies_to_msecs(tz->passive_delay_jiffies));
2620015d9a2SAmit Kucheria 	params->prev_err = err;
2630015d9a2SAmit Kucheria 
2640015d9a2SAmit Kucheria 	power_range = p + i + d;
2650015d9a2SAmit Kucheria 
2660015d9a2SAmit Kucheria 	/* feed-forward the known sustainable dissipatable power */
2670015d9a2SAmit Kucheria 	power_range = sustainable_power + frac_to_int(power_range);
2680015d9a2SAmit Kucheria 
2690015d9a2SAmit Kucheria 	power_range = clamp(power_range, (s64)0, (s64)max_allocatable_power);
2700015d9a2SAmit Kucheria 
2710015d9a2SAmit Kucheria 	trace_thermal_power_allocator_pid(tz, frac_to_int(err),
2720015d9a2SAmit Kucheria 					  frac_to_int(params->err_integral),
2730015d9a2SAmit Kucheria 					  frac_to_int(p), frac_to_int(i),
2740015d9a2SAmit Kucheria 					  frac_to_int(d), power_range);
2750015d9a2SAmit Kucheria 
2760015d9a2SAmit Kucheria 	return power_range;
2770015d9a2SAmit Kucheria }
2780015d9a2SAmit Kucheria 
2790015d9a2SAmit Kucheria /**
280345a8af7SLukasz Luba  * power_actor_set_power() - limit the maximum power a cooling device consumes
281345a8af7SLukasz Luba  * @cdev:	pointer to &thermal_cooling_device
282345a8af7SLukasz Luba  * @instance:	thermal instance to update
283345a8af7SLukasz Luba  * @power:	the power in milliwatts
284345a8af7SLukasz Luba  *
285345a8af7SLukasz Luba  * Set the cooling device to consume at most @power milliwatts. The limit is
286345a8af7SLukasz Luba  * expected to be a cap at the maximum power consumption.
287345a8af7SLukasz Luba  *
288345a8af7SLukasz Luba  * Return: 0 on success, -EINVAL if the cooling device does not
289345a8af7SLukasz Luba  * implement the power actor API or -E* for other failures.
290345a8af7SLukasz Luba  */
291345a8af7SLukasz Luba static int
power_actor_set_power(struct thermal_cooling_device * cdev,struct thermal_instance * instance,u32 power)292345a8af7SLukasz Luba power_actor_set_power(struct thermal_cooling_device *cdev,
293345a8af7SLukasz Luba 		      struct thermal_instance *instance, u32 power)
294345a8af7SLukasz Luba {
295345a8af7SLukasz Luba 	unsigned long state;
296345a8af7SLukasz Luba 	int ret;
297345a8af7SLukasz Luba 
298345a8af7SLukasz Luba 	ret = cdev->ops->power2state(cdev, power, &state);
299345a8af7SLukasz Luba 	if (ret)
300345a8af7SLukasz Luba 		return ret;
301345a8af7SLukasz Luba 
302345a8af7SLukasz Luba 	instance->target = clamp_val(state, instance->lower, instance->upper);
303345a8af7SLukasz Luba 	mutex_lock(&cdev->lock);
304ab39c885SLukasz Luba 	__thermal_cdev_update(cdev);
305345a8af7SLukasz Luba 	mutex_unlock(&cdev->lock);
306345a8af7SLukasz Luba 
307345a8af7SLukasz Luba 	return 0;
308345a8af7SLukasz Luba }
309345a8af7SLukasz Luba 
310345a8af7SLukasz Luba /**
3110015d9a2SAmit Kucheria  * divvy_up_power() - divvy the allocated power between the actors
3120015d9a2SAmit Kucheria  * @req_power:	each actor's requested power
3130015d9a2SAmit Kucheria  * @max_power:	each actor's maximum available power
3140015d9a2SAmit Kucheria  * @num_actors:	size of the @req_power, @max_power and @granted_power's array
3150015d9a2SAmit Kucheria  * @total_req_power: sum of @req_power
3160015d9a2SAmit Kucheria  * @power_range:	total allocated power
3170015d9a2SAmit Kucheria  * @granted_power:	output array: each actor's granted power
3180015d9a2SAmit Kucheria  * @extra_actor_power:	an appropriately sized array to be used in the
3190015d9a2SAmit Kucheria  *			function as temporary storage of the extra power given
3200015d9a2SAmit Kucheria  *			to the actors
3210015d9a2SAmit Kucheria  *
3220015d9a2SAmit Kucheria  * This function divides the total allocated power (@power_range)
3230015d9a2SAmit Kucheria  * fairly between the actors.  It first tries to give each actor a
3240015d9a2SAmit Kucheria  * share of the @power_range according to how much power it requested
3250015d9a2SAmit Kucheria  * compared to the rest of the actors.  For example, if only one actor
3260015d9a2SAmit Kucheria  * requests power, then it receives all the @power_range.  If
3270015d9a2SAmit Kucheria  * three actors each requests 1mW, each receives a third of the
3280015d9a2SAmit Kucheria  * @power_range.
3290015d9a2SAmit Kucheria  *
3300015d9a2SAmit Kucheria  * If any actor received more than their maximum power, then that
3310015d9a2SAmit Kucheria  * surplus is re-divvied among the actors based on how far they are
3320015d9a2SAmit Kucheria  * from their respective maximums.
3330015d9a2SAmit Kucheria  *
3340015d9a2SAmit Kucheria  * Granted power for each actor is written to @granted_power, which
3350015d9a2SAmit Kucheria  * should've been allocated by the calling function.
3360015d9a2SAmit Kucheria  */
divvy_up_power(u32 * req_power,u32 * max_power,int num_actors,u32 total_req_power,u32 power_range,u32 * granted_power,u32 * extra_actor_power)3370015d9a2SAmit Kucheria static void divvy_up_power(u32 *req_power, u32 *max_power, int num_actors,
3380015d9a2SAmit Kucheria 			   u32 total_req_power, u32 power_range,
3390015d9a2SAmit Kucheria 			   u32 *granted_power, u32 *extra_actor_power)
3400015d9a2SAmit Kucheria {
3410015d9a2SAmit Kucheria 	u32 extra_power, capped_extra_power;
3420015d9a2SAmit Kucheria 	int i;
3430015d9a2SAmit Kucheria 
3440015d9a2SAmit Kucheria 	/*
3450015d9a2SAmit Kucheria 	 * Prevent division by 0 if none of the actors request power.
3460015d9a2SAmit Kucheria 	 */
3470015d9a2SAmit Kucheria 	if (!total_req_power)
3480015d9a2SAmit Kucheria 		total_req_power = 1;
3490015d9a2SAmit Kucheria 
3500015d9a2SAmit Kucheria 	capped_extra_power = 0;
3510015d9a2SAmit Kucheria 	extra_power = 0;
3520015d9a2SAmit Kucheria 	for (i = 0; i < num_actors; i++) {
3530015d9a2SAmit Kucheria 		u64 req_range = (u64)req_power[i] * power_range;
3540015d9a2SAmit Kucheria 
3550015d9a2SAmit Kucheria 		granted_power[i] = DIV_ROUND_CLOSEST_ULL(req_range,
3560015d9a2SAmit Kucheria 							 total_req_power);
3570015d9a2SAmit Kucheria 
3580015d9a2SAmit Kucheria 		if (granted_power[i] > max_power[i]) {
3590015d9a2SAmit Kucheria 			extra_power += granted_power[i] - max_power[i];
3600015d9a2SAmit Kucheria 			granted_power[i] = max_power[i];
3610015d9a2SAmit Kucheria 		}
3620015d9a2SAmit Kucheria 
3630015d9a2SAmit Kucheria 		extra_actor_power[i] = max_power[i] - granted_power[i];
3640015d9a2SAmit Kucheria 		capped_extra_power += extra_actor_power[i];
3650015d9a2SAmit Kucheria 	}
3660015d9a2SAmit Kucheria 
3670015d9a2SAmit Kucheria 	if (!extra_power)
3680015d9a2SAmit Kucheria 		return;
3690015d9a2SAmit Kucheria 
3700015d9a2SAmit Kucheria 	/*
3710015d9a2SAmit Kucheria 	 * Re-divvy the reclaimed extra among actors based on
3720015d9a2SAmit Kucheria 	 * how far they are from the max
3730015d9a2SAmit Kucheria 	 */
3740015d9a2SAmit Kucheria 	extra_power = min(extra_power, capped_extra_power);
3750015d9a2SAmit Kucheria 	if (capped_extra_power > 0)
3766e3e14c9Sjeson.gao 		for (i = 0; i < num_actors; i++) {
3776e3e14c9Sjeson.gao 			u64 extra_range = (u64)extra_actor_power[i] * extra_power;
3786e3e14c9Sjeson.gao 			granted_power[i] += DIV_ROUND_CLOSEST_ULL(extra_range,
3796e3e14c9Sjeson.gao 							 capped_extra_power);
3806e3e14c9Sjeson.gao 		}
3810015d9a2SAmit Kucheria }
3820015d9a2SAmit Kucheria 
allocate_power(struct thermal_zone_device * tz,int control_temp)3830015d9a2SAmit Kucheria static int allocate_power(struct thermal_zone_device *tz,
3840015d9a2SAmit Kucheria 			  int control_temp)
3850015d9a2SAmit Kucheria {
3860015d9a2SAmit Kucheria 	struct thermal_instance *instance;
3870015d9a2SAmit Kucheria 	struct power_allocator_params *params = tz->governor_data;
38877451ef5SRafael J. Wysocki 	const struct thermal_trip *trip_max_desired_temperature =
38977451ef5SRafael J. Wysocki 			&tz->trips[params->trip_max_desired_temperature];
3900015d9a2SAmit Kucheria 	u32 *req_power, *max_power, *granted_power, *extra_actor_power;
3910015d9a2SAmit Kucheria 	u32 *weighted_req_power;
3920015d9a2SAmit Kucheria 	u32 total_req_power, max_allocatable_power, total_weighted_req_power;
3930015d9a2SAmit Kucheria 	u32 total_granted_power, power_range;
3940015d9a2SAmit Kucheria 	int i, num_actors, total_weight, ret = 0;
3950015d9a2SAmit Kucheria 
3960015d9a2SAmit Kucheria 	num_actors = 0;
3970015d9a2SAmit Kucheria 	total_weight = 0;
3980015d9a2SAmit Kucheria 	list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
3990015d9a2SAmit Kucheria 		if ((instance->trip == trip_max_desired_temperature) &&
4000015d9a2SAmit Kucheria 		    cdev_is_power_actor(instance->cdev)) {
4010015d9a2SAmit Kucheria 			num_actors++;
4020015d9a2SAmit Kucheria 			total_weight += instance->weight;
4030015d9a2SAmit Kucheria 		}
4040015d9a2SAmit Kucheria 	}
4050015d9a2SAmit Kucheria 
40663561fe3SDaniel Lezcano 	if (!num_actors)
40763561fe3SDaniel Lezcano 		return -ENODEV;
4080015d9a2SAmit Kucheria 
4090015d9a2SAmit Kucheria 	/*
4100015d9a2SAmit Kucheria 	 * We need to allocate five arrays of the same size:
4110015d9a2SAmit Kucheria 	 * req_power, max_power, granted_power, extra_actor_power and
4120015d9a2SAmit Kucheria 	 * weighted_req_power.  They are going to be needed until this
4130015d9a2SAmit Kucheria 	 * function returns.  Allocate them all in one go to simplify
4140015d9a2SAmit Kucheria 	 * the allocation and deallocation logic.
4150015d9a2SAmit Kucheria 	 */
4160015d9a2SAmit Kucheria 	BUILD_BUG_ON(sizeof(*req_power) != sizeof(*max_power));
4170015d9a2SAmit Kucheria 	BUILD_BUG_ON(sizeof(*req_power) != sizeof(*granted_power));
4180015d9a2SAmit Kucheria 	BUILD_BUG_ON(sizeof(*req_power) != sizeof(*extra_actor_power));
4190015d9a2SAmit Kucheria 	BUILD_BUG_ON(sizeof(*req_power) != sizeof(*weighted_req_power));
4200015d9a2SAmit Kucheria 	req_power = kcalloc(num_actors * 5, sizeof(*req_power), GFP_KERNEL);
42163561fe3SDaniel Lezcano 	if (!req_power)
42263561fe3SDaniel Lezcano 		return -ENOMEM;
4230015d9a2SAmit Kucheria 
4240015d9a2SAmit Kucheria 	max_power = &req_power[num_actors];
4250015d9a2SAmit Kucheria 	granted_power = &req_power[2 * num_actors];
4260015d9a2SAmit Kucheria 	extra_actor_power = &req_power[3 * num_actors];
4270015d9a2SAmit Kucheria 	weighted_req_power = &req_power[4 * num_actors];
4280015d9a2SAmit Kucheria 
4290015d9a2SAmit Kucheria 	i = 0;
4300015d9a2SAmit Kucheria 	total_weighted_req_power = 0;
4310015d9a2SAmit Kucheria 	total_req_power = 0;
4320015d9a2SAmit Kucheria 	max_allocatable_power = 0;
4330015d9a2SAmit Kucheria 
4340015d9a2SAmit Kucheria 	list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
4350015d9a2SAmit Kucheria 		int weight;
4360015d9a2SAmit Kucheria 		struct thermal_cooling_device *cdev = instance->cdev;
4370015d9a2SAmit Kucheria 
4380015d9a2SAmit Kucheria 		if (instance->trip != trip_max_desired_temperature)
4390015d9a2SAmit Kucheria 			continue;
4400015d9a2SAmit Kucheria 
4410015d9a2SAmit Kucheria 		if (!cdev_is_power_actor(cdev))
4420015d9a2SAmit Kucheria 			continue;
4430015d9a2SAmit Kucheria 
444ecd1d2a3Szhuguangqing 		if (cdev->ops->get_requested_power(cdev, &req_power[i]))
4450015d9a2SAmit Kucheria 			continue;
4460015d9a2SAmit Kucheria 
4470015d9a2SAmit Kucheria 		if (!total_weight)
4480015d9a2SAmit Kucheria 			weight = 1 << FRAC_BITS;
4490015d9a2SAmit Kucheria 		else
4500015d9a2SAmit Kucheria 			weight = instance->weight;
4510015d9a2SAmit Kucheria 
4520015d9a2SAmit Kucheria 		weighted_req_power[i] = frac_to_int(weight * req_power[i]);
4530015d9a2SAmit Kucheria 
4548132df3aSLukasz Luba 		if (cdev->ops->state2power(cdev, instance->lower,
4558132df3aSLukasz Luba 					   &max_power[i]))
4560015d9a2SAmit Kucheria 			continue;
4570015d9a2SAmit Kucheria 
4580015d9a2SAmit Kucheria 		total_req_power += req_power[i];
4590015d9a2SAmit Kucheria 		max_allocatable_power += max_power[i];
4600015d9a2SAmit Kucheria 		total_weighted_req_power += weighted_req_power[i];
4610015d9a2SAmit Kucheria 
4620015d9a2SAmit Kucheria 		i++;
4630015d9a2SAmit Kucheria 	}
4640015d9a2SAmit Kucheria 
4650015d9a2SAmit Kucheria 	power_range = pid_controller(tz, control_temp, max_allocatable_power);
4660015d9a2SAmit Kucheria 
4670015d9a2SAmit Kucheria 	divvy_up_power(weighted_req_power, max_power, num_actors,
4680015d9a2SAmit Kucheria 		       total_weighted_req_power, power_range, granted_power,
4690015d9a2SAmit Kucheria 		       extra_actor_power);
4700015d9a2SAmit Kucheria 
4710015d9a2SAmit Kucheria 	total_granted_power = 0;
4720015d9a2SAmit Kucheria 	i = 0;
4730015d9a2SAmit Kucheria 	list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
4740015d9a2SAmit Kucheria 		if (instance->trip != trip_max_desired_temperature)
4750015d9a2SAmit Kucheria 			continue;
4760015d9a2SAmit Kucheria 
4770015d9a2SAmit Kucheria 		if (!cdev_is_power_actor(instance->cdev))
4780015d9a2SAmit Kucheria 			continue;
4790015d9a2SAmit Kucheria 
4800015d9a2SAmit Kucheria 		power_actor_set_power(instance->cdev, instance,
4810015d9a2SAmit Kucheria 				      granted_power[i]);
4820015d9a2SAmit Kucheria 		total_granted_power += granted_power[i];
4830015d9a2SAmit Kucheria 
4840015d9a2SAmit Kucheria 		i++;
4850015d9a2SAmit Kucheria 	}
4860015d9a2SAmit Kucheria 
4870015d9a2SAmit Kucheria 	trace_thermal_power_allocator(tz, req_power, total_req_power,
4880015d9a2SAmit Kucheria 				      granted_power, total_granted_power,
4890015d9a2SAmit Kucheria 				      num_actors, power_range,
4900015d9a2SAmit Kucheria 				      max_allocatable_power, tz->temperature,
4910015d9a2SAmit Kucheria 				      control_temp - tz->temperature);
4920015d9a2SAmit Kucheria 
4930015d9a2SAmit Kucheria 	kfree(req_power);
4940015d9a2SAmit Kucheria 
4950015d9a2SAmit Kucheria 	return ret;
4960015d9a2SAmit Kucheria }
4970015d9a2SAmit Kucheria 
4980015d9a2SAmit Kucheria /**
4990015d9a2SAmit Kucheria  * get_governor_trips() - get the number of the two trip points that are key for this governor
5000015d9a2SAmit Kucheria  * @tz:	thermal zone to operate on
5010015d9a2SAmit Kucheria  * @params:	pointer to private data for this governor
5020015d9a2SAmit Kucheria  *
5030015d9a2SAmit Kucheria  * The power allocator governor works optimally with two trips points:
5040015d9a2SAmit Kucheria  * a "switch on" trip point and a "maximum desired temperature".  These
5050015d9a2SAmit Kucheria  * are defined as the first and last passive trip points.
5060015d9a2SAmit Kucheria  *
5070015d9a2SAmit Kucheria  * If there is only one trip point, then that's considered to be the
5080015d9a2SAmit Kucheria  * "maximum desired temperature" trip point and the governor is always
5090015d9a2SAmit Kucheria  * on.  If there are no passive or active trip points, then the
5100015d9a2SAmit Kucheria  * governor won't do anything.  In fact, its throttle function
5110015d9a2SAmit Kucheria  * won't be called at all.
5120015d9a2SAmit Kucheria  */
get_governor_trips(struct thermal_zone_device * tz,struct power_allocator_params * params)5130015d9a2SAmit Kucheria static void get_governor_trips(struct thermal_zone_device *tz,
5140015d9a2SAmit Kucheria 			       struct power_allocator_params *params)
5150015d9a2SAmit Kucheria {
5160015d9a2SAmit Kucheria 	int i, last_active, last_passive;
5170015d9a2SAmit Kucheria 	bool found_first_passive;
5180015d9a2SAmit Kucheria 
5190015d9a2SAmit Kucheria 	found_first_passive = false;
5200015d9a2SAmit Kucheria 	last_active = INVALID_TRIP;
5210015d9a2SAmit Kucheria 	last_passive = INVALID_TRIP;
5220015d9a2SAmit Kucheria 
523e5bfcd30SDaniel Lezcano 	for (i = 0; i < tz->num_trips; i++) {
5247f725a23SDaniel Lezcano 		struct thermal_trip trip;
5250015d9a2SAmit Kucheria 		int ret;
5260015d9a2SAmit Kucheria 
5277f725a23SDaniel Lezcano 		ret = __thermal_zone_get_trip(tz, i, &trip);
5280015d9a2SAmit Kucheria 		if (ret) {
5290015d9a2SAmit Kucheria 			dev_warn(&tz->device,
5300015d9a2SAmit Kucheria 				 "Failed to get trip point %d type: %d\n", i,
5310015d9a2SAmit Kucheria 				 ret);
5320015d9a2SAmit Kucheria 			continue;
5330015d9a2SAmit Kucheria 		}
5340015d9a2SAmit Kucheria 
5357f725a23SDaniel Lezcano 		if (trip.type == THERMAL_TRIP_PASSIVE) {
5360015d9a2SAmit Kucheria 			if (!found_first_passive) {
5370015d9a2SAmit Kucheria 				params->trip_switch_on = i;
5380015d9a2SAmit Kucheria 				found_first_passive = true;
5390015d9a2SAmit Kucheria 			} else  {
5400015d9a2SAmit Kucheria 				last_passive = i;
5410015d9a2SAmit Kucheria 			}
5427f725a23SDaniel Lezcano 		} else if (trip.type == THERMAL_TRIP_ACTIVE) {
5430015d9a2SAmit Kucheria 			last_active = i;
5440015d9a2SAmit Kucheria 		} else {
5450015d9a2SAmit Kucheria 			break;
5460015d9a2SAmit Kucheria 		}
5470015d9a2SAmit Kucheria 	}
5480015d9a2SAmit Kucheria 
5490015d9a2SAmit Kucheria 	if (last_passive != INVALID_TRIP) {
5500015d9a2SAmit Kucheria 		params->trip_max_desired_temperature = last_passive;
5510015d9a2SAmit Kucheria 	} else if (found_first_passive) {
5520015d9a2SAmit Kucheria 		params->trip_max_desired_temperature = params->trip_switch_on;
5530015d9a2SAmit Kucheria 		params->trip_switch_on = INVALID_TRIP;
5540015d9a2SAmit Kucheria 	} else {
5550015d9a2SAmit Kucheria 		params->trip_switch_on = INVALID_TRIP;
5560015d9a2SAmit Kucheria 		params->trip_max_desired_temperature = last_active;
5570015d9a2SAmit Kucheria 	}
5580015d9a2SAmit Kucheria }
5590015d9a2SAmit Kucheria 
reset_pid_controller(struct power_allocator_params * params)5600015d9a2SAmit Kucheria static void reset_pid_controller(struct power_allocator_params *params)
5610015d9a2SAmit Kucheria {
5620015d9a2SAmit Kucheria 	params->err_integral = 0;
5630015d9a2SAmit Kucheria 	params->prev_err = 0;
5640015d9a2SAmit Kucheria }
5650015d9a2SAmit Kucheria 
allow_maximum_power(struct thermal_zone_device * tz,bool update)5660952177fSLukasz Luba static void allow_maximum_power(struct thermal_zone_device *tz, bool update)
5670015d9a2SAmit Kucheria {
5680015d9a2SAmit Kucheria 	struct thermal_instance *instance;
5690015d9a2SAmit Kucheria 	struct power_allocator_params *params = tz->governor_data;
57077451ef5SRafael J. Wysocki 	const struct thermal_trip *trip_max_desired_temperature =
57177451ef5SRafael J. Wysocki 			&tz->trips[params->trip_max_desired_temperature];
572d3b60ed9SLukasz Luba 	u32 req_power;
5730015d9a2SAmit Kucheria 
5740015d9a2SAmit Kucheria 	list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
575d3b60ed9SLukasz Luba 		struct thermal_cooling_device *cdev = instance->cdev;
576d3b60ed9SLukasz Luba 
57777451ef5SRafael J. Wysocki 		if ((instance->trip != trip_max_desired_temperature) ||
5780015d9a2SAmit Kucheria 		    (!cdev_is_power_actor(instance->cdev)))
5790015d9a2SAmit Kucheria 			continue;
5800015d9a2SAmit Kucheria 
5810015d9a2SAmit Kucheria 		instance->target = 0;
5820015d9a2SAmit Kucheria 		mutex_lock(&instance->cdev->lock);
583d3b60ed9SLukasz Luba 		/*
584d3b60ed9SLukasz Luba 		 * Call for updating the cooling devices local stats and avoid
585d3b60ed9SLukasz Luba 		 * periods of dozen of seconds when those have not been
586d3b60ed9SLukasz Luba 		 * maintained.
587d3b60ed9SLukasz Luba 		 */
588d3b60ed9SLukasz Luba 		cdev->ops->get_requested_power(cdev, &req_power);
589d3b60ed9SLukasz Luba 
5900952177fSLukasz Luba 		if (update)
5910952177fSLukasz Luba 			__thermal_cdev_update(instance->cdev);
5920952177fSLukasz Luba 
5930015d9a2SAmit Kucheria 		mutex_unlock(&instance->cdev->lock);
5940015d9a2SAmit Kucheria 	}
5950015d9a2SAmit Kucheria }
5960015d9a2SAmit Kucheria 
5970015d9a2SAmit Kucheria /**
5987a583405SLukasz Luba  * check_power_actors() - Check all cooling devices and warn when they are
5997a583405SLukasz Luba  *			not power actors
6007a583405SLukasz Luba  * @tz:		thermal zone to operate on
6017a583405SLukasz Luba  *
6027a583405SLukasz Luba  * Check all cooling devices in the @tz and warn every time they are missing
6037a583405SLukasz Luba  * power actor API. The warning should help to investigate the issue, which
6047a583405SLukasz Luba  * could be e.g. lack of Energy Model for a given device.
6057a583405SLukasz Luba  *
6067a583405SLukasz Luba  * Return: 0 on success, -EINVAL if any cooling device does not implement
6077a583405SLukasz Luba  * the power actor API.
6087a583405SLukasz Luba  */
check_power_actors(struct thermal_zone_device * tz)6097a583405SLukasz Luba static int check_power_actors(struct thermal_zone_device *tz)
6107a583405SLukasz Luba {
6117a583405SLukasz Luba 	struct thermal_instance *instance;
6127a583405SLukasz Luba 	int ret = 0;
6137a583405SLukasz Luba 
6147a583405SLukasz Luba 	list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
6157a583405SLukasz Luba 		if (!cdev_is_power_actor(instance->cdev)) {
6167a583405SLukasz Luba 			dev_warn(&tz->device, "power_allocator: %s is not a power actor\n",
6177a583405SLukasz Luba 				 instance->cdev->type);
6187a583405SLukasz Luba 			ret = -EINVAL;
6197a583405SLukasz Luba 		}
6207a583405SLukasz Luba 	}
6217a583405SLukasz Luba 
6227a583405SLukasz Luba 	return ret;
6237a583405SLukasz Luba }
6247a583405SLukasz Luba 
6257a583405SLukasz Luba /**
6260015d9a2SAmit Kucheria  * power_allocator_bind() - bind the power_allocator governor to a thermal zone
6270015d9a2SAmit Kucheria  * @tz:	thermal zone to bind it to
6280015d9a2SAmit Kucheria  *
6290015d9a2SAmit Kucheria  * Initialize the PID controller parameters and bind it to the thermal
6300015d9a2SAmit Kucheria  * zone.
6310015d9a2SAmit Kucheria  *
6327a583405SLukasz Luba  * Return: 0 on success, or -ENOMEM if we ran out of memory, or -EINVAL
6337a583405SLukasz Luba  * when there are unsupported cooling devices in the @tz.
6340015d9a2SAmit Kucheria  */
power_allocator_bind(struct thermal_zone_device * tz)6350015d9a2SAmit Kucheria static int power_allocator_bind(struct thermal_zone_device *tz)
6360015d9a2SAmit Kucheria {
6370015d9a2SAmit Kucheria 	int ret;
6380015d9a2SAmit Kucheria 	struct power_allocator_params *params;
6397f725a23SDaniel Lezcano 	struct thermal_trip trip;
6400015d9a2SAmit Kucheria 
6417a583405SLukasz Luba 	ret = check_power_actors(tz);
6427a583405SLukasz Luba 	if (ret)
6437a583405SLukasz Luba 		return ret;
6447a583405SLukasz Luba 
6450015d9a2SAmit Kucheria 	params = kzalloc(sizeof(*params), GFP_KERNEL);
6460015d9a2SAmit Kucheria 	if (!params)
6470015d9a2SAmit Kucheria 		return -ENOMEM;
6480015d9a2SAmit Kucheria 
6490015d9a2SAmit Kucheria 	if (!tz->tzp) {
6500015d9a2SAmit Kucheria 		tz->tzp = kzalloc(sizeof(*tz->tzp), GFP_KERNEL);
6510015d9a2SAmit Kucheria 		if (!tz->tzp) {
6520015d9a2SAmit Kucheria 			ret = -ENOMEM;
6530015d9a2SAmit Kucheria 			goto free_params;
6540015d9a2SAmit Kucheria 		}
6550015d9a2SAmit Kucheria 
6560015d9a2SAmit Kucheria 		params->allocated_tzp = true;
6570015d9a2SAmit Kucheria 	}
6580015d9a2SAmit Kucheria 
6590015d9a2SAmit Kucheria 	if (!tz->tzp->sustainable_power)
6600015d9a2SAmit Kucheria 		dev_warn(&tz->device, "power_allocator: sustainable_power will be estimated\n");
6610015d9a2SAmit Kucheria 
6620015d9a2SAmit Kucheria 	get_governor_trips(tz, params);
6630015d9a2SAmit Kucheria 
664e5bfcd30SDaniel Lezcano 	if (tz->num_trips > 0) {
6657f725a23SDaniel Lezcano 		ret = __thermal_zone_get_trip(tz, params->trip_max_desired_temperature,
6667f725a23SDaniel Lezcano 					      &trip);
6670015d9a2SAmit Kucheria 		if (!ret)
6680015d9a2SAmit Kucheria 			estimate_pid_constants(tz, tz->tzp->sustainable_power,
6690015d9a2SAmit Kucheria 					       params->trip_switch_on,
6707f725a23SDaniel Lezcano 					       trip.temperature);
6710015d9a2SAmit Kucheria 	}
6720015d9a2SAmit Kucheria 
6730015d9a2SAmit Kucheria 	reset_pid_controller(params);
6740015d9a2SAmit Kucheria 
6750015d9a2SAmit Kucheria 	tz->governor_data = params;
6760015d9a2SAmit Kucheria 
6770015d9a2SAmit Kucheria 	return 0;
6780015d9a2SAmit Kucheria 
6790015d9a2SAmit Kucheria free_params:
6800015d9a2SAmit Kucheria 	kfree(params);
6810015d9a2SAmit Kucheria 
6820015d9a2SAmit Kucheria 	return ret;
6830015d9a2SAmit Kucheria }
6840015d9a2SAmit Kucheria 
power_allocator_unbind(struct thermal_zone_device * tz)6850015d9a2SAmit Kucheria static void power_allocator_unbind(struct thermal_zone_device *tz)
6860015d9a2SAmit Kucheria {
6870015d9a2SAmit Kucheria 	struct power_allocator_params *params = tz->governor_data;
6880015d9a2SAmit Kucheria 
6890015d9a2SAmit Kucheria 	dev_dbg(&tz->device, "Unbinding from thermal zone %d\n", tz->id);
6900015d9a2SAmit Kucheria 
6910015d9a2SAmit Kucheria 	if (params->allocated_tzp) {
6920015d9a2SAmit Kucheria 		kfree(tz->tzp);
6930015d9a2SAmit Kucheria 		tz->tzp = NULL;
6940015d9a2SAmit Kucheria 	}
6950015d9a2SAmit Kucheria 
6960015d9a2SAmit Kucheria 	kfree(tz->governor_data);
6970015d9a2SAmit Kucheria 	tz->governor_data = NULL;
6980015d9a2SAmit Kucheria }
6990015d9a2SAmit Kucheria 
power_allocator_throttle(struct thermal_zone_device * tz,int trip_id)7007f725a23SDaniel Lezcano static int power_allocator_throttle(struct thermal_zone_device *tz, int trip_id)
7010015d9a2SAmit Kucheria {
7020015d9a2SAmit Kucheria 	struct power_allocator_params *params = tz->governor_data;
7037f725a23SDaniel Lezcano 	struct thermal_trip trip;
7047f725a23SDaniel Lezcano 	int ret;
7050952177fSLukasz Luba 	bool update;
7060015d9a2SAmit Kucheria 
707670a5e35SDaniel Lezcano 	lockdep_assert_held(&tz->lock);
70863561fe3SDaniel Lezcano 
7090015d9a2SAmit Kucheria 	/*
7100015d9a2SAmit Kucheria 	 * We get called for every trip point but we only need to do
7110015d9a2SAmit Kucheria 	 * our calculations once
7120015d9a2SAmit Kucheria 	 */
7137f725a23SDaniel Lezcano 	if (trip_id != params->trip_max_desired_temperature)
714670a5e35SDaniel Lezcano 		return 0;
7150015d9a2SAmit Kucheria 
7167f725a23SDaniel Lezcano 	ret = __thermal_zone_get_trip(tz, params->trip_switch_on, &trip);
7177f725a23SDaniel Lezcano 	if (!ret && (tz->temperature < trip.temperature)) {
718*d10ff0b3SDi Shen 		update = tz->passive;
7190015d9a2SAmit Kucheria 		tz->passive = 0;
7200015d9a2SAmit Kucheria 		reset_pid_controller(params);
7210952177fSLukasz Luba 		allow_maximum_power(tz, update);
722670a5e35SDaniel Lezcano 		return 0;
7230015d9a2SAmit Kucheria 	}
7240015d9a2SAmit Kucheria 
7250015d9a2SAmit Kucheria 	tz->passive = 1;
7260015d9a2SAmit Kucheria 
7277f725a23SDaniel Lezcano 	ret = __thermal_zone_get_trip(tz, params->trip_max_desired_temperature, &trip);
7280015d9a2SAmit Kucheria 	if (ret) {
7297f725a23SDaniel Lezcano 		dev_warn(&tz->device, "Failed to get the maximum desired temperature: %d\n",
7300015d9a2SAmit Kucheria 			 ret);
731670a5e35SDaniel Lezcano 		return ret;
7320015d9a2SAmit Kucheria 	}
7330015d9a2SAmit Kucheria 
7347f725a23SDaniel Lezcano 	return allocate_power(tz, trip.temperature);
7350015d9a2SAmit Kucheria }
7360015d9a2SAmit Kucheria 
7370015d9a2SAmit Kucheria static struct thermal_governor thermal_gov_power_allocator = {
7380015d9a2SAmit Kucheria 	.name		= "power_allocator",
7390015d9a2SAmit Kucheria 	.bind_to_tz	= power_allocator_bind,
7400015d9a2SAmit Kucheria 	.unbind_from_tz	= power_allocator_unbind,
7410015d9a2SAmit Kucheria 	.throttle	= power_allocator_throttle,
7420015d9a2SAmit Kucheria };
7430015d9a2SAmit Kucheria THERMAL_GOVERNOR_DECLARE(thermal_gov_power_allocator);
744