10015d9a2SAmit Kucheria /*
20015d9a2SAmit Kucheria  * A power allocator to manage temperature
30015d9a2SAmit Kucheria  *
40015d9a2SAmit Kucheria  * Copyright (C) 2014 ARM Ltd.
50015d9a2SAmit Kucheria  *
60015d9a2SAmit Kucheria  * This program is free software; you can redistribute it and/or modify
70015d9a2SAmit Kucheria  * it under the terms of the GNU General Public License version 2 as
80015d9a2SAmit Kucheria  * published by the Free Software Foundation.
90015d9a2SAmit Kucheria  *
100015d9a2SAmit Kucheria  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
110015d9a2SAmit Kucheria  * kind, whether express or implied; without even the implied warranty
120015d9a2SAmit Kucheria  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
130015d9a2SAmit Kucheria  * GNU General Public License for more details.
140015d9a2SAmit Kucheria  */
150015d9a2SAmit Kucheria 
160015d9a2SAmit Kucheria #define pr_fmt(fmt) "Power allocator: " fmt
170015d9a2SAmit Kucheria 
180015d9a2SAmit Kucheria #include <linux/rculist.h>
190015d9a2SAmit Kucheria #include <linux/slab.h>
200015d9a2SAmit Kucheria #include <linux/thermal.h>
210015d9a2SAmit Kucheria 
220015d9a2SAmit Kucheria #define CREATE_TRACE_POINTS
230015d9a2SAmit Kucheria #include <trace/events/thermal_power_allocator.h>
240015d9a2SAmit Kucheria 
250015d9a2SAmit Kucheria #include "thermal_core.h"
260015d9a2SAmit Kucheria 
270015d9a2SAmit Kucheria #define INVALID_TRIP -1
280015d9a2SAmit Kucheria 
290015d9a2SAmit Kucheria #define FRAC_BITS 10
300015d9a2SAmit Kucheria #define int_to_frac(x) ((x) << FRAC_BITS)
310015d9a2SAmit Kucheria #define frac_to_int(x) ((x) >> FRAC_BITS)
320015d9a2SAmit Kucheria 
330015d9a2SAmit Kucheria /**
340015d9a2SAmit Kucheria  * mul_frac() - multiply two fixed-point numbers
350015d9a2SAmit Kucheria  * @x:	first multiplicand
360015d9a2SAmit Kucheria  * @y:	second multiplicand
370015d9a2SAmit Kucheria  *
380015d9a2SAmit Kucheria  * Return: the result of multiplying two fixed-point numbers.  The
390015d9a2SAmit Kucheria  * result is also a fixed-point number.
400015d9a2SAmit Kucheria  */
410015d9a2SAmit Kucheria static inline s64 mul_frac(s64 x, s64 y)
420015d9a2SAmit Kucheria {
430015d9a2SAmit Kucheria 	return (x * y) >> FRAC_BITS;
440015d9a2SAmit Kucheria }
450015d9a2SAmit Kucheria 
460015d9a2SAmit Kucheria /**
470015d9a2SAmit Kucheria  * div_frac() - divide two fixed-point numbers
480015d9a2SAmit Kucheria  * @x:	the dividend
490015d9a2SAmit Kucheria  * @y:	the divisor
500015d9a2SAmit Kucheria  *
510015d9a2SAmit Kucheria  * Return: the result of dividing two fixed-point numbers.  The
520015d9a2SAmit Kucheria  * result is also a fixed-point number.
530015d9a2SAmit Kucheria  */
540015d9a2SAmit Kucheria static inline s64 div_frac(s64 x, s64 y)
550015d9a2SAmit Kucheria {
560015d9a2SAmit Kucheria 	return div_s64(x << FRAC_BITS, y);
570015d9a2SAmit Kucheria }
580015d9a2SAmit Kucheria 
590015d9a2SAmit Kucheria /**
600015d9a2SAmit Kucheria  * struct power_allocator_params - parameters for the power allocator governor
610015d9a2SAmit Kucheria  * @allocated_tzp:	whether we have allocated tzp for this thermal zone and
620015d9a2SAmit Kucheria  *			it needs to be freed on unbind
630015d9a2SAmit Kucheria  * @err_integral:	accumulated error in the PID controller.
640015d9a2SAmit Kucheria  * @prev_err:	error in the previous iteration of the PID controller.
650015d9a2SAmit Kucheria  *		Used to calculate the derivative term.
660015d9a2SAmit Kucheria  * @trip_switch_on:	first passive trip point of the thermal zone.  The
670015d9a2SAmit Kucheria  *			governor switches on when this trip point is crossed.
680015d9a2SAmit Kucheria  *			If the thermal zone only has one passive trip point,
690015d9a2SAmit Kucheria  *			@trip_switch_on should be INVALID_TRIP.
700015d9a2SAmit Kucheria  * @trip_max_desired_temperature:	last passive trip point of the thermal
710015d9a2SAmit Kucheria  *					zone.  The temperature we are
720015d9a2SAmit Kucheria  *					controlling for.
730015d9a2SAmit Kucheria  */
740015d9a2SAmit Kucheria struct power_allocator_params {
750015d9a2SAmit Kucheria 	bool allocated_tzp;
760015d9a2SAmit Kucheria 	s64 err_integral;
770015d9a2SAmit Kucheria 	s32 prev_err;
780015d9a2SAmit Kucheria 	int trip_switch_on;
790015d9a2SAmit Kucheria 	int trip_max_desired_temperature;
800015d9a2SAmit Kucheria };
810015d9a2SAmit Kucheria 
820015d9a2SAmit Kucheria /**
830015d9a2SAmit Kucheria  * estimate_sustainable_power() - Estimate the sustainable power of a thermal zone
840015d9a2SAmit Kucheria  * @tz: thermal zone we are operating in
850015d9a2SAmit Kucheria  *
860015d9a2SAmit Kucheria  * For thermal zones that don't provide a sustainable_power in their
870015d9a2SAmit Kucheria  * thermal_zone_params, estimate one.  Calculate it using the minimum
880015d9a2SAmit Kucheria  * power of all the cooling devices as that gives a valid value that
890015d9a2SAmit Kucheria  * can give some degree of functionality.  For optimal performance of
900015d9a2SAmit Kucheria  * this governor, provide a sustainable_power in the thermal zone's
910015d9a2SAmit Kucheria  * thermal_zone_params.
920015d9a2SAmit Kucheria  */
930015d9a2SAmit Kucheria static u32 estimate_sustainable_power(struct thermal_zone_device *tz)
940015d9a2SAmit Kucheria {
950015d9a2SAmit Kucheria 	u32 sustainable_power = 0;
960015d9a2SAmit Kucheria 	struct thermal_instance *instance;
970015d9a2SAmit Kucheria 	struct power_allocator_params *params = tz->governor_data;
980015d9a2SAmit Kucheria 
990015d9a2SAmit Kucheria 	list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
1000015d9a2SAmit Kucheria 		struct thermal_cooling_device *cdev = instance->cdev;
1010015d9a2SAmit Kucheria 		u32 min_power;
1020015d9a2SAmit Kucheria 
1030015d9a2SAmit Kucheria 		if (instance->trip != params->trip_max_desired_temperature)
1040015d9a2SAmit Kucheria 			continue;
1050015d9a2SAmit Kucheria 
1060015d9a2SAmit Kucheria 		if (power_actor_get_min_power(cdev, tz, &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  * @force:	whether to force the update of the constants
1220015d9a2SAmit Kucheria  *
1230015d9a2SAmit Kucheria  * This function is used to update the estimation of the PID
1240015d9a2SAmit Kucheria  * controller constants in struct thermal_zone_parameters.
1250015d9a2SAmit Kucheria  * Sustainable power is provided in case it was estimated.  The
1260015d9a2SAmit Kucheria  * estimated sustainable_power should not be stored in the
1270015d9a2SAmit Kucheria  * thermal_zone_parameters so it has to be passed explicitly to this
1280015d9a2SAmit Kucheria  * function.
1290015d9a2SAmit Kucheria  *
1300015d9a2SAmit Kucheria  * If @force is not set, the values in the thermal zone's parameters
1310015d9a2SAmit Kucheria  * are preserved if they are not zero.  If @force is set, the values
1320015d9a2SAmit Kucheria  * in thermal zone's parameters are overwritten.
1330015d9a2SAmit Kucheria  */
1340015d9a2SAmit Kucheria static void estimate_pid_constants(struct thermal_zone_device *tz,
1350015d9a2SAmit Kucheria 				   u32 sustainable_power, int trip_switch_on,
1360015d9a2SAmit Kucheria 				   int control_temp, bool force)
1370015d9a2SAmit Kucheria {
1380015d9a2SAmit Kucheria 	int ret;
1390015d9a2SAmit Kucheria 	int switch_on_temp;
1400015d9a2SAmit Kucheria 	u32 temperature_threshold;
1410015d9a2SAmit Kucheria 
1420015d9a2SAmit Kucheria 	ret = tz->ops->get_trip_temp(tz, trip_switch_on, &switch_on_temp);
1430015d9a2SAmit Kucheria 	if (ret)
1440015d9a2SAmit Kucheria 		switch_on_temp = 0;
1450015d9a2SAmit Kucheria 
1460015d9a2SAmit Kucheria 	temperature_threshold = control_temp - switch_on_temp;
1470015d9a2SAmit Kucheria 	/*
1480015d9a2SAmit Kucheria 	 * estimate_pid_constants() tries to find appropriate default
1490015d9a2SAmit Kucheria 	 * values for thermal zones that don't provide them. If a
1500015d9a2SAmit Kucheria 	 * system integrator has configured a thermal zone with two
1510015d9a2SAmit Kucheria 	 * passive trip points at the same temperature, that person
1520015d9a2SAmit Kucheria 	 * hasn't put any effort to set up the thermal zone properly
1530015d9a2SAmit Kucheria 	 * so just give up.
1540015d9a2SAmit Kucheria 	 */
1550015d9a2SAmit Kucheria 	if (!temperature_threshold)
1560015d9a2SAmit Kucheria 		return;
1570015d9a2SAmit Kucheria 
1580015d9a2SAmit Kucheria 	if (!tz->tzp->k_po || force)
1590015d9a2SAmit Kucheria 		tz->tzp->k_po = int_to_frac(sustainable_power) /
1600015d9a2SAmit Kucheria 			temperature_threshold;
1610015d9a2SAmit Kucheria 
1620015d9a2SAmit Kucheria 	if (!tz->tzp->k_pu || force)
1630015d9a2SAmit Kucheria 		tz->tzp->k_pu = int_to_frac(2 * sustainable_power) /
1640015d9a2SAmit Kucheria 			temperature_threshold;
1650015d9a2SAmit Kucheria 
1660015d9a2SAmit Kucheria 	if (!tz->tzp->k_i || force)
1670015d9a2SAmit Kucheria 		tz->tzp->k_i = int_to_frac(10) / 1000;
1680015d9a2SAmit Kucheria 	/*
1690015d9a2SAmit Kucheria 	 * The default for k_d and integral_cutoff is 0, so we can
1700015d9a2SAmit Kucheria 	 * leave them as they are.
1710015d9a2SAmit Kucheria 	 */
1720015d9a2SAmit Kucheria }
1730015d9a2SAmit Kucheria 
1740015d9a2SAmit Kucheria /**
1750015d9a2SAmit Kucheria  * pid_controller() - PID controller
1760015d9a2SAmit Kucheria  * @tz:	thermal zone we are operating in
1770015d9a2SAmit Kucheria  * @control_temp:	the target temperature in millicelsius
1780015d9a2SAmit Kucheria  * @max_allocatable_power:	maximum allocatable power for this thermal zone
1790015d9a2SAmit Kucheria  *
1800015d9a2SAmit Kucheria  * This PID controller increases the available power budget so that the
1810015d9a2SAmit Kucheria  * temperature of the thermal zone gets as close as possible to
1820015d9a2SAmit Kucheria  * @control_temp and limits the power if it exceeds it.  k_po is the
1830015d9a2SAmit Kucheria  * proportional term when we are overshooting, k_pu is the
1840015d9a2SAmit Kucheria  * proportional term when we are undershooting.  integral_cutoff is a
1850015d9a2SAmit Kucheria  * threshold below which we stop accumulating the error.  The
1860015d9a2SAmit Kucheria  * accumulated error is only valid if the requested power will make
1870015d9a2SAmit Kucheria  * the system warmer.  If the system is mostly idle, there's no point
1880015d9a2SAmit Kucheria  * in accumulating positive error.
1890015d9a2SAmit Kucheria  *
1900015d9a2SAmit Kucheria  * Return: The power budget for the next period.
1910015d9a2SAmit Kucheria  */
1920015d9a2SAmit Kucheria static u32 pid_controller(struct thermal_zone_device *tz,
1930015d9a2SAmit Kucheria 			  int control_temp,
1940015d9a2SAmit Kucheria 			  u32 max_allocatable_power)
1950015d9a2SAmit Kucheria {
1960015d9a2SAmit Kucheria 	s64 p, i, d, power_range;
1970015d9a2SAmit Kucheria 	s32 err, max_power_frac;
1980015d9a2SAmit Kucheria 	u32 sustainable_power;
1990015d9a2SAmit Kucheria 	struct power_allocator_params *params = tz->governor_data;
2000015d9a2SAmit Kucheria 
2010015d9a2SAmit Kucheria 	max_power_frac = int_to_frac(max_allocatable_power);
2020015d9a2SAmit Kucheria 
2030015d9a2SAmit Kucheria 	if (tz->tzp->sustainable_power) {
2040015d9a2SAmit Kucheria 		sustainable_power = tz->tzp->sustainable_power;
2050015d9a2SAmit Kucheria 	} else {
2060015d9a2SAmit Kucheria 		sustainable_power = estimate_sustainable_power(tz);
2070015d9a2SAmit Kucheria 		estimate_pid_constants(tz, sustainable_power,
2080015d9a2SAmit Kucheria 				       params->trip_switch_on, control_temp,
2090015d9a2SAmit Kucheria 				       true);
2100015d9a2SAmit Kucheria 	}
2110015d9a2SAmit Kucheria 
2120015d9a2SAmit Kucheria 	err = control_temp - tz->temperature;
2130015d9a2SAmit Kucheria 	err = int_to_frac(err);
2140015d9a2SAmit Kucheria 
2150015d9a2SAmit Kucheria 	/* Calculate the proportional term */
2160015d9a2SAmit Kucheria 	p = mul_frac(err < 0 ? tz->tzp->k_po : tz->tzp->k_pu, err);
2170015d9a2SAmit Kucheria 
2180015d9a2SAmit Kucheria 	/*
2190015d9a2SAmit Kucheria 	 * Calculate the integral term
2200015d9a2SAmit Kucheria 	 *
2210015d9a2SAmit Kucheria 	 * if the error is less than cut off allow integration (but
2220015d9a2SAmit Kucheria 	 * the integral is limited to max power)
2230015d9a2SAmit Kucheria 	 */
2240015d9a2SAmit Kucheria 	i = mul_frac(tz->tzp->k_i, params->err_integral);
2250015d9a2SAmit Kucheria 
2260015d9a2SAmit Kucheria 	if (err < int_to_frac(tz->tzp->integral_cutoff)) {
2270015d9a2SAmit Kucheria 		s64 i_next = i + mul_frac(tz->tzp->k_i, err);
2280015d9a2SAmit Kucheria 
2290015d9a2SAmit Kucheria 		if (abs(i_next) < max_power_frac) {
2300015d9a2SAmit Kucheria 			i = i_next;
2310015d9a2SAmit Kucheria 			params->err_integral += err;
2320015d9a2SAmit Kucheria 		}
2330015d9a2SAmit Kucheria 	}
2340015d9a2SAmit Kucheria 
2350015d9a2SAmit Kucheria 	/*
2360015d9a2SAmit Kucheria 	 * Calculate the derivative term
2370015d9a2SAmit Kucheria 	 *
2380015d9a2SAmit Kucheria 	 * We do err - prev_err, so with a positive k_d, a decreasing
2390015d9a2SAmit Kucheria 	 * error (i.e. driving closer to the line) results in less
2400015d9a2SAmit Kucheria 	 * power being applied, slowing down the controller)
2410015d9a2SAmit Kucheria 	 */
2420015d9a2SAmit Kucheria 	d = mul_frac(tz->tzp->k_d, err - params->prev_err);
2430015d9a2SAmit Kucheria 	d = div_frac(d, tz->passive_delay);
2440015d9a2SAmit Kucheria 	params->prev_err = err;
2450015d9a2SAmit Kucheria 
2460015d9a2SAmit Kucheria 	power_range = p + i + d;
2470015d9a2SAmit Kucheria 
2480015d9a2SAmit Kucheria 	/* feed-forward the known sustainable dissipatable power */
2490015d9a2SAmit Kucheria 	power_range = sustainable_power + frac_to_int(power_range);
2500015d9a2SAmit Kucheria 
2510015d9a2SAmit Kucheria 	power_range = clamp(power_range, (s64)0, (s64)max_allocatable_power);
2520015d9a2SAmit Kucheria 
2530015d9a2SAmit Kucheria 	trace_thermal_power_allocator_pid(tz, frac_to_int(err),
2540015d9a2SAmit Kucheria 					  frac_to_int(params->err_integral),
2550015d9a2SAmit Kucheria 					  frac_to_int(p), frac_to_int(i),
2560015d9a2SAmit Kucheria 					  frac_to_int(d), power_range);
2570015d9a2SAmit Kucheria 
2580015d9a2SAmit Kucheria 	return power_range;
2590015d9a2SAmit Kucheria }
2600015d9a2SAmit Kucheria 
2610015d9a2SAmit Kucheria /**
2620015d9a2SAmit Kucheria  * divvy_up_power() - divvy the allocated power between the actors
2630015d9a2SAmit Kucheria  * @req_power:	each actor's requested power
2640015d9a2SAmit Kucheria  * @max_power:	each actor's maximum available power
2650015d9a2SAmit Kucheria  * @num_actors:	size of the @req_power, @max_power and @granted_power's array
2660015d9a2SAmit Kucheria  * @total_req_power: sum of @req_power
2670015d9a2SAmit Kucheria  * @power_range:	total allocated power
2680015d9a2SAmit Kucheria  * @granted_power:	output array: each actor's granted power
2690015d9a2SAmit Kucheria  * @extra_actor_power:	an appropriately sized array to be used in the
2700015d9a2SAmit Kucheria  *			function as temporary storage of the extra power given
2710015d9a2SAmit Kucheria  *			to the actors
2720015d9a2SAmit Kucheria  *
2730015d9a2SAmit Kucheria  * This function divides the total allocated power (@power_range)
2740015d9a2SAmit Kucheria  * fairly between the actors.  It first tries to give each actor a
2750015d9a2SAmit Kucheria  * share of the @power_range according to how much power it requested
2760015d9a2SAmit Kucheria  * compared to the rest of the actors.  For example, if only one actor
2770015d9a2SAmit Kucheria  * requests power, then it receives all the @power_range.  If
2780015d9a2SAmit Kucheria  * three actors each requests 1mW, each receives a third of the
2790015d9a2SAmit Kucheria  * @power_range.
2800015d9a2SAmit Kucheria  *
2810015d9a2SAmit Kucheria  * If any actor received more than their maximum power, then that
2820015d9a2SAmit Kucheria  * surplus is re-divvied among the actors based on how far they are
2830015d9a2SAmit Kucheria  * from their respective maximums.
2840015d9a2SAmit Kucheria  *
2850015d9a2SAmit Kucheria  * Granted power for each actor is written to @granted_power, which
2860015d9a2SAmit Kucheria  * should've been allocated by the calling function.
2870015d9a2SAmit Kucheria  */
2880015d9a2SAmit Kucheria static void divvy_up_power(u32 *req_power, u32 *max_power, int num_actors,
2890015d9a2SAmit Kucheria 			   u32 total_req_power, u32 power_range,
2900015d9a2SAmit Kucheria 			   u32 *granted_power, u32 *extra_actor_power)
2910015d9a2SAmit Kucheria {
2920015d9a2SAmit Kucheria 	u32 extra_power, capped_extra_power;
2930015d9a2SAmit Kucheria 	int i;
2940015d9a2SAmit Kucheria 
2950015d9a2SAmit Kucheria 	/*
2960015d9a2SAmit Kucheria 	 * Prevent division by 0 if none of the actors request power.
2970015d9a2SAmit Kucheria 	 */
2980015d9a2SAmit Kucheria 	if (!total_req_power)
2990015d9a2SAmit Kucheria 		total_req_power = 1;
3000015d9a2SAmit Kucheria 
3010015d9a2SAmit Kucheria 	capped_extra_power = 0;
3020015d9a2SAmit Kucheria 	extra_power = 0;
3030015d9a2SAmit Kucheria 	for (i = 0; i < num_actors; i++) {
3040015d9a2SAmit Kucheria 		u64 req_range = (u64)req_power[i] * power_range;
3050015d9a2SAmit Kucheria 
3060015d9a2SAmit Kucheria 		granted_power[i] = DIV_ROUND_CLOSEST_ULL(req_range,
3070015d9a2SAmit Kucheria 							 total_req_power);
3080015d9a2SAmit Kucheria 
3090015d9a2SAmit Kucheria 		if (granted_power[i] > max_power[i]) {
3100015d9a2SAmit Kucheria 			extra_power += granted_power[i] - max_power[i];
3110015d9a2SAmit Kucheria 			granted_power[i] = max_power[i];
3120015d9a2SAmit Kucheria 		}
3130015d9a2SAmit Kucheria 
3140015d9a2SAmit Kucheria 		extra_actor_power[i] = max_power[i] - granted_power[i];
3150015d9a2SAmit Kucheria 		capped_extra_power += extra_actor_power[i];
3160015d9a2SAmit Kucheria 	}
3170015d9a2SAmit Kucheria 
3180015d9a2SAmit Kucheria 	if (!extra_power)
3190015d9a2SAmit Kucheria 		return;
3200015d9a2SAmit Kucheria 
3210015d9a2SAmit Kucheria 	/*
3220015d9a2SAmit Kucheria 	 * Re-divvy the reclaimed extra among actors based on
3230015d9a2SAmit Kucheria 	 * how far they are from the max
3240015d9a2SAmit Kucheria 	 */
3250015d9a2SAmit Kucheria 	extra_power = min(extra_power, capped_extra_power);
3260015d9a2SAmit Kucheria 	if (capped_extra_power > 0)
3270015d9a2SAmit Kucheria 		for (i = 0; i < num_actors; i++)
3280015d9a2SAmit Kucheria 			granted_power[i] += (extra_actor_power[i] *
3290015d9a2SAmit Kucheria 					extra_power) / capped_extra_power;
3300015d9a2SAmit Kucheria }
3310015d9a2SAmit Kucheria 
3320015d9a2SAmit Kucheria static int allocate_power(struct thermal_zone_device *tz,
3330015d9a2SAmit Kucheria 			  int control_temp)
3340015d9a2SAmit Kucheria {
3350015d9a2SAmit Kucheria 	struct thermal_instance *instance;
3360015d9a2SAmit Kucheria 	struct power_allocator_params *params = tz->governor_data;
3370015d9a2SAmit Kucheria 	u32 *req_power, *max_power, *granted_power, *extra_actor_power;
3380015d9a2SAmit Kucheria 	u32 *weighted_req_power;
3390015d9a2SAmit Kucheria 	u32 total_req_power, max_allocatable_power, total_weighted_req_power;
3400015d9a2SAmit Kucheria 	u32 total_granted_power, power_range;
3410015d9a2SAmit Kucheria 	int i, num_actors, total_weight, ret = 0;
3420015d9a2SAmit Kucheria 	int trip_max_desired_temperature = params->trip_max_desired_temperature;
3430015d9a2SAmit Kucheria 
3440015d9a2SAmit Kucheria 	mutex_lock(&tz->lock);
3450015d9a2SAmit Kucheria 
3460015d9a2SAmit Kucheria 	num_actors = 0;
3470015d9a2SAmit Kucheria 	total_weight = 0;
3480015d9a2SAmit Kucheria 	list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
3490015d9a2SAmit Kucheria 		if ((instance->trip == trip_max_desired_temperature) &&
3500015d9a2SAmit Kucheria 		    cdev_is_power_actor(instance->cdev)) {
3510015d9a2SAmit Kucheria 			num_actors++;
3520015d9a2SAmit Kucheria 			total_weight += instance->weight;
3530015d9a2SAmit Kucheria 		}
3540015d9a2SAmit Kucheria 	}
3550015d9a2SAmit Kucheria 
3560015d9a2SAmit Kucheria 	if (!num_actors) {
3570015d9a2SAmit Kucheria 		ret = -ENODEV;
3580015d9a2SAmit Kucheria 		goto unlock;
3590015d9a2SAmit Kucheria 	}
3600015d9a2SAmit Kucheria 
3610015d9a2SAmit Kucheria 	/*
3620015d9a2SAmit Kucheria 	 * We need to allocate five arrays of the same size:
3630015d9a2SAmit Kucheria 	 * req_power, max_power, granted_power, extra_actor_power and
3640015d9a2SAmit Kucheria 	 * weighted_req_power.  They are going to be needed until this
3650015d9a2SAmit Kucheria 	 * function returns.  Allocate them all in one go to simplify
3660015d9a2SAmit Kucheria 	 * the allocation and deallocation logic.
3670015d9a2SAmit Kucheria 	 */
3680015d9a2SAmit Kucheria 	BUILD_BUG_ON(sizeof(*req_power) != sizeof(*max_power));
3690015d9a2SAmit Kucheria 	BUILD_BUG_ON(sizeof(*req_power) != sizeof(*granted_power));
3700015d9a2SAmit Kucheria 	BUILD_BUG_ON(sizeof(*req_power) != sizeof(*extra_actor_power));
3710015d9a2SAmit Kucheria 	BUILD_BUG_ON(sizeof(*req_power) != sizeof(*weighted_req_power));
3720015d9a2SAmit Kucheria 	req_power = kcalloc(num_actors * 5, sizeof(*req_power), GFP_KERNEL);
3730015d9a2SAmit Kucheria 	if (!req_power) {
3740015d9a2SAmit Kucheria 		ret = -ENOMEM;
3750015d9a2SAmit Kucheria 		goto unlock;
3760015d9a2SAmit Kucheria 	}
3770015d9a2SAmit Kucheria 
3780015d9a2SAmit Kucheria 	max_power = &req_power[num_actors];
3790015d9a2SAmit Kucheria 	granted_power = &req_power[2 * num_actors];
3800015d9a2SAmit Kucheria 	extra_actor_power = &req_power[3 * num_actors];
3810015d9a2SAmit Kucheria 	weighted_req_power = &req_power[4 * num_actors];
3820015d9a2SAmit Kucheria 
3830015d9a2SAmit Kucheria 	i = 0;
3840015d9a2SAmit Kucheria 	total_weighted_req_power = 0;
3850015d9a2SAmit Kucheria 	total_req_power = 0;
3860015d9a2SAmit Kucheria 	max_allocatable_power = 0;
3870015d9a2SAmit Kucheria 
3880015d9a2SAmit Kucheria 	list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
3890015d9a2SAmit Kucheria 		int weight;
3900015d9a2SAmit Kucheria 		struct thermal_cooling_device *cdev = instance->cdev;
3910015d9a2SAmit Kucheria 
3920015d9a2SAmit Kucheria 		if (instance->trip != trip_max_desired_temperature)
3930015d9a2SAmit Kucheria 			continue;
3940015d9a2SAmit Kucheria 
3950015d9a2SAmit Kucheria 		if (!cdev_is_power_actor(cdev))
3960015d9a2SAmit Kucheria 			continue;
3970015d9a2SAmit Kucheria 
3980015d9a2SAmit Kucheria 		if (cdev->ops->get_requested_power(cdev, tz, &req_power[i]))
3990015d9a2SAmit Kucheria 			continue;
4000015d9a2SAmit Kucheria 
4010015d9a2SAmit Kucheria 		if (!total_weight)
4020015d9a2SAmit Kucheria 			weight = 1 << FRAC_BITS;
4030015d9a2SAmit Kucheria 		else
4040015d9a2SAmit Kucheria 			weight = instance->weight;
4050015d9a2SAmit Kucheria 
4060015d9a2SAmit Kucheria 		weighted_req_power[i] = frac_to_int(weight * req_power[i]);
4070015d9a2SAmit Kucheria 
4080015d9a2SAmit Kucheria 		if (power_actor_get_max_power(cdev, tz, &max_power[i]))
4090015d9a2SAmit Kucheria 			continue;
4100015d9a2SAmit Kucheria 
4110015d9a2SAmit Kucheria 		total_req_power += req_power[i];
4120015d9a2SAmit Kucheria 		max_allocatable_power += max_power[i];
4130015d9a2SAmit Kucheria 		total_weighted_req_power += weighted_req_power[i];
4140015d9a2SAmit Kucheria 
4150015d9a2SAmit Kucheria 		i++;
4160015d9a2SAmit Kucheria 	}
4170015d9a2SAmit Kucheria 
4180015d9a2SAmit Kucheria 	power_range = pid_controller(tz, control_temp, max_allocatable_power);
4190015d9a2SAmit Kucheria 
4200015d9a2SAmit Kucheria 	divvy_up_power(weighted_req_power, max_power, num_actors,
4210015d9a2SAmit Kucheria 		       total_weighted_req_power, power_range, granted_power,
4220015d9a2SAmit Kucheria 		       extra_actor_power);
4230015d9a2SAmit Kucheria 
4240015d9a2SAmit Kucheria 	total_granted_power = 0;
4250015d9a2SAmit Kucheria 	i = 0;
4260015d9a2SAmit Kucheria 	list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
4270015d9a2SAmit Kucheria 		if (instance->trip != trip_max_desired_temperature)
4280015d9a2SAmit Kucheria 			continue;
4290015d9a2SAmit Kucheria 
4300015d9a2SAmit Kucheria 		if (!cdev_is_power_actor(instance->cdev))
4310015d9a2SAmit Kucheria 			continue;
4320015d9a2SAmit Kucheria 
4330015d9a2SAmit Kucheria 		power_actor_set_power(instance->cdev, instance,
4340015d9a2SAmit Kucheria 				      granted_power[i]);
4350015d9a2SAmit Kucheria 		total_granted_power += granted_power[i];
4360015d9a2SAmit Kucheria 
4370015d9a2SAmit Kucheria 		i++;
4380015d9a2SAmit Kucheria 	}
4390015d9a2SAmit Kucheria 
4400015d9a2SAmit Kucheria 	trace_thermal_power_allocator(tz, req_power, total_req_power,
4410015d9a2SAmit Kucheria 				      granted_power, total_granted_power,
4420015d9a2SAmit Kucheria 				      num_actors, power_range,
4430015d9a2SAmit Kucheria 				      max_allocatable_power, tz->temperature,
4440015d9a2SAmit Kucheria 				      control_temp - tz->temperature);
4450015d9a2SAmit Kucheria 
4460015d9a2SAmit Kucheria 	kfree(req_power);
4470015d9a2SAmit Kucheria unlock:
4480015d9a2SAmit Kucheria 	mutex_unlock(&tz->lock);
4490015d9a2SAmit Kucheria 
4500015d9a2SAmit Kucheria 	return ret;
4510015d9a2SAmit Kucheria }
4520015d9a2SAmit Kucheria 
4530015d9a2SAmit Kucheria /**
4540015d9a2SAmit Kucheria  * get_governor_trips() - get the number of the two trip points that are key for this governor
4550015d9a2SAmit Kucheria  * @tz:	thermal zone to operate on
4560015d9a2SAmit Kucheria  * @params:	pointer to private data for this governor
4570015d9a2SAmit Kucheria  *
4580015d9a2SAmit Kucheria  * The power allocator governor works optimally with two trips points:
4590015d9a2SAmit Kucheria  * a "switch on" trip point and a "maximum desired temperature".  These
4600015d9a2SAmit Kucheria  * are defined as the first and last passive trip points.
4610015d9a2SAmit Kucheria  *
4620015d9a2SAmit Kucheria  * If there is only one trip point, then that's considered to be the
4630015d9a2SAmit Kucheria  * "maximum desired temperature" trip point and the governor is always
4640015d9a2SAmit Kucheria  * on.  If there are no passive or active trip points, then the
4650015d9a2SAmit Kucheria  * governor won't do anything.  In fact, its throttle function
4660015d9a2SAmit Kucheria  * won't be called at all.
4670015d9a2SAmit Kucheria  */
4680015d9a2SAmit Kucheria static void get_governor_trips(struct thermal_zone_device *tz,
4690015d9a2SAmit Kucheria 			       struct power_allocator_params *params)
4700015d9a2SAmit Kucheria {
4710015d9a2SAmit Kucheria 	int i, last_active, last_passive;
4720015d9a2SAmit Kucheria 	bool found_first_passive;
4730015d9a2SAmit Kucheria 
4740015d9a2SAmit Kucheria 	found_first_passive = false;
4750015d9a2SAmit Kucheria 	last_active = INVALID_TRIP;
4760015d9a2SAmit Kucheria 	last_passive = INVALID_TRIP;
4770015d9a2SAmit Kucheria 
4780015d9a2SAmit Kucheria 	for (i = 0; i < tz->trips; i++) {
4790015d9a2SAmit Kucheria 		enum thermal_trip_type type;
4800015d9a2SAmit Kucheria 		int ret;
4810015d9a2SAmit Kucheria 
4820015d9a2SAmit Kucheria 		ret = tz->ops->get_trip_type(tz, i, &type);
4830015d9a2SAmit Kucheria 		if (ret) {
4840015d9a2SAmit Kucheria 			dev_warn(&tz->device,
4850015d9a2SAmit Kucheria 				 "Failed to get trip point %d type: %d\n", i,
4860015d9a2SAmit Kucheria 				 ret);
4870015d9a2SAmit Kucheria 			continue;
4880015d9a2SAmit Kucheria 		}
4890015d9a2SAmit Kucheria 
4900015d9a2SAmit Kucheria 		if (type == THERMAL_TRIP_PASSIVE) {
4910015d9a2SAmit Kucheria 			if (!found_first_passive) {
4920015d9a2SAmit Kucheria 				params->trip_switch_on = i;
4930015d9a2SAmit Kucheria 				found_first_passive = true;
4940015d9a2SAmit Kucheria 			} else  {
4950015d9a2SAmit Kucheria 				last_passive = i;
4960015d9a2SAmit Kucheria 			}
4970015d9a2SAmit Kucheria 		} else if (type == THERMAL_TRIP_ACTIVE) {
4980015d9a2SAmit Kucheria 			last_active = i;
4990015d9a2SAmit Kucheria 		} else {
5000015d9a2SAmit Kucheria 			break;
5010015d9a2SAmit Kucheria 		}
5020015d9a2SAmit Kucheria 	}
5030015d9a2SAmit Kucheria 
5040015d9a2SAmit Kucheria 	if (last_passive != INVALID_TRIP) {
5050015d9a2SAmit Kucheria 		params->trip_max_desired_temperature = last_passive;
5060015d9a2SAmit Kucheria 	} else if (found_first_passive) {
5070015d9a2SAmit Kucheria 		params->trip_max_desired_temperature = params->trip_switch_on;
5080015d9a2SAmit Kucheria 		params->trip_switch_on = INVALID_TRIP;
5090015d9a2SAmit Kucheria 	} else {
5100015d9a2SAmit Kucheria 		params->trip_switch_on = INVALID_TRIP;
5110015d9a2SAmit Kucheria 		params->trip_max_desired_temperature = last_active;
5120015d9a2SAmit Kucheria 	}
5130015d9a2SAmit Kucheria }
5140015d9a2SAmit Kucheria 
5150015d9a2SAmit Kucheria static void reset_pid_controller(struct power_allocator_params *params)
5160015d9a2SAmit Kucheria {
5170015d9a2SAmit Kucheria 	params->err_integral = 0;
5180015d9a2SAmit Kucheria 	params->prev_err = 0;
5190015d9a2SAmit Kucheria }
5200015d9a2SAmit Kucheria 
5210015d9a2SAmit Kucheria static void allow_maximum_power(struct thermal_zone_device *tz)
5220015d9a2SAmit Kucheria {
5230015d9a2SAmit Kucheria 	struct thermal_instance *instance;
5240015d9a2SAmit Kucheria 	struct power_allocator_params *params = tz->governor_data;
5250015d9a2SAmit Kucheria 
5260015d9a2SAmit Kucheria 	mutex_lock(&tz->lock);
5270015d9a2SAmit Kucheria 	list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
5280015d9a2SAmit Kucheria 		if ((instance->trip != params->trip_max_desired_temperature) ||
5290015d9a2SAmit Kucheria 		    (!cdev_is_power_actor(instance->cdev)))
5300015d9a2SAmit Kucheria 			continue;
5310015d9a2SAmit Kucheria 
5320015d9a2SAmit Kucheria 		instance->target = 0;
5330015d9a2SAmit Kucheria 		mutex_lock(&instance->cdev->lock);
5340015d9a2SAmit Kucheria 		instance->cdev->updated = false;
5350015d9a2SAmit Kucheria 		mutex_unlock(&instance->cdev->lock);
5360015d9a2SAmit Kucheria 		thermal_cdev_update(instance->cdev);
5370015d9a2SAmit Kucheria 	}
5380015d9a2SAmit Kucheria 	mutex_unlock(&tz->lock);
5390015d9a2SAmit Kucheria }
5400015d9a2SAmit Kucheria 
5410015d9a2SAmit Kucheria /**
5420015d9a2SAmit Kucheria  * power_allocator_bind() - bind the power_allocator governor to a thermal zone
5430015d9a2SAmit Kucheria  * @tz:	thermal zone to bind it to
5440015d9a2SAmit Kucheria  *
5450015d9a2SAmit Kucheria  * Initialize the PID controller parameters and bind it to the thermal
5460015d9a2SAmit Kucheria  * zone.
5470015d9a2SAmit Kucheria  *
5480015d9a2SAmit Kucheria  * Return: 0 on success, or -ENOMEM if we ran out of memory.
5490015d9a2SAmit Kucheria  */
5500015d9a2SAmit Kucheria static int power_allocator_bind(struct thermal_zone_device *tz)
5510015d9a2SAmit Kucheria {
5520015d9a2SAmit Kucheria 	int ret;
5530015d9a2SAmit Kucheria 	struct power_allocator_params *params;
5540015d9a2SAmit Kucheria 	int control_temp;
5550015d9a2SAmit Kucheria 
5560015d9a2SAmit Kucheria 	params = kzalloc(sizeof(*params), GFP_KERNEL);
5570015d9a2SAmit Kucheria 	if (!params)
5580015d9a2SAmit Kucheria 		return -ENOMEM;
5590015d9a2SAmit Kucheria 
5600015d9a2SAmit Kucheria 	if (!tz->tzp) {
5610015d9a2SAmit Kucheria 		tz->tzp = kzalloc(sizeof(*tz->tzp), GFP_KERNEL);
5620015d9a2SAmit Kucheria 		if (!tz->tzp) {
5630015d9a2SAmit Kucheria 			ret = -ENOMEM;
5640015d9a2SAmit Kucheria 			goto free_params;
5650015d9a2SAmit Kucheria 		}
5660015d9a2SAmit Kucheria 
5670015d9a2SAmit Kucheria 		params->allocated_tzp = true;
5680015d9a2SAmit Kucheria 	}
5690015d9a2SAmit Kucheria 
5700015d9a2SAmit Kucheria 	if (!tz->tzp->sustainable_power)
5710015d9a2SAmit Kucheria 		dev_warn(&tz->device, "power_allocator: sustainable_power will be estimated\n");
5720015d9a2SAmit Kucheria 
5730015d9a2SAmit Kucheria 	get_governor_trips(tz, params);
5740015d9a2SAmit Kucheria 
5750015d9a2SAmit Kucheria 	if (tz->trips > 0) {
5760015d9a2SAmit Kucheria 		ret = tz->ops->get_trip_temp(tz,
5770015d9a2SAmit Kucheria 					params->trip_max_desired_temperature,
5780015d9a2SAmit Kucheria 					&control_temp);
5790015d9a2SAmit Kucheria 		if (!ret)
5800015d9a2SAmit Kucheria 			estimate_pid_constants(tz, tz->tzp->sustainable_power,
5810015d9a2SAmit Kucheria 					       params->trip_switch_on,
5820015d9a2SAmit Kucheria 					       control_temp, false);
5830015d9a2SAmit Kucheria 	}
5840015d9a2SAmit Kucheria 
5850015d9a2SAmit Kucheria 	reset_pid_controller(params);
5860015d9a2SAmit Kucheria 
5870015d9a2SAmit Kucheria 	tz->governor_data = params;
5880015d9a2SAmit Kucheria 
5890015d9a2SAmit Kucheria 	return 0;
5900015d9a2SAmit Kucheria 
5910015d9a2SAmit Kucheria free_params:
5920015d9a2SAmit Kucheria 	kfree(params);
5930015d9a2SAmit Kucheria 
5940015d9a2SAmit Kucheria 	return ret;
5950015d9a2SAmit Kucheria }
5960015d9a2SAmit Kucheria 
5970015d9a2SAmit Kucheria static void power_allocator_unbind(struct thermal_zone_device *tz)
5980015d9a2SAmit Kucheria {
5990015d9a2SAmit Kucheria 	struct power_allocator_params *params = tz->governor_data;
6000015d9a2SAmit Kucheria 
6010015d9a2SAmit Kucheria 	dev_dbg(&tz->device, "Unbinding from thermal zone %d\n", tz->id);
6020015d9a2SAmit Kucheria 
6030015d9a2SAmit Kucheria 	if (params->allocated_tzp) {
6040015d9a2SAmit Kucheria 		kfree(tz->tzp);
6050015d9a2SAmit Kucheria 		tz->tzp = NULL;
6060015d9a2SAmit Kucheria 	}
6070015d9a2SAmit Kucheria 
6080015d9a2SAmit Kucheria 	kfree(tz->governor_data);
6090015d9a2SAmit Kucheria 	tz->governor_data = NULL;
6100015d9a2SAmit Kucheria }
6110015d9a2SAmit Kucheria 
6120015d9a2SAmit Kucheria static int power_allocator_throttle(struct thermal_zone_device *tz, int trip)
6130015d9a2SAmit Kucheria {
6140015d9a2SAmit Kucheria 	int ret;
6150015d9a2SAmit Kucheria 	int switch_on_temp, control_temp;
6160015d9a2SAmit Kucheria 	struct power_allocator_params *params = tz->governor_data;
6170015d9a2SAmit Kucheria 
6180015d9a2SAmit Kucheria 	/*
6190015d9a2SAmit Kucheria 	 * We get called for every trip point but we only need to do
6200015d9a2SAmit Kucheria 	 * our calculations once
6210015d9a2SAmit Kucheria 	 */
6220015d9a2SAmit Kucheria 	if (trip != params->trip_max_desired_temperature)
6230015d9a2SAmit Kucheria 		return 0;
6240015d9a2SAmit Kucheria 
6250015d9a2SAmit Kucheria 	ret = tz->ops->get_trip_temp(tz, params->trip_switch_on,
6260015d9a2SAmit Kucheria 				     &switch_on_temp);
6270015d9a2SAmit Kucheria 	if (!ret && (tz->temperature < switch_on_temp)) {
6280015d9a2SAmit Kucheria 		tz->passive = 0;
6290015d9a2SAmit Kucheria 		reset_pid_controller(params);
6300015d9a2SAmit Kucheria 		allow_maximum_power(tz);
6310015d9a2SAmit Kucheria 		return 0;
6320015d9a2SAmit Kucheria 	}
6330015d9a2SAmit Kucheria 
6340015d9a2SAmit Kucheria 	tz->passive = 1;
6350015d9a2SAmit Kucheria 
6360015d9a2SAmit Kucheria 	ret = tz->ops->get_trip_temp(tz, params->trip_max_desired_temperature,
6370015d9a2SAmit Kucheria 				&control_temp);
6380015d9a2SAmit Kucheria 	if (ret) {
6390015d9a2SAmit Kucheria 		dev_warn(&tz->device,
6400015d9a2SAmit Kucheria 			 "Failed to get the maximum desired temperature: %d\n",
6410015d9a2SAmit Kucheria 			 ret);
6420015d9a2SAmit Kucheria 		return ret;
6430015d9a2SAmit Kucheria 	}
6440015d9a2SAmit Kucheria 
6450015d9a2SAmit Kucheria 	return allocate_power(tz, control_temp);
6460015d9a2SAmit Kucheria }
6470015d9a2SAmit Kucheria 
6480015d9a2SAmit Kucheria static struct thermal_governor thermal_gov_power_allocator = {
6490015d9a2SAmit Kucheria 	.name		= "power_allocator",
6500015d9a2SAmit Kucheria 	.bind_to_tz	= power_allocator_bind,
6510015d9a2SAmit Kucheria 	.unbind_from_tz	= power_allocator_unbind,
6520015d9a2SAmit Kucheria 	.throttle	= power_allocator_throttle,
6530015d9a2SAmit Kucheria };
6540015d9a2SAmit Kucheria THERMAL_GOVERNOR_DECLARE(thermal_gov_power_allocator);
655