1 // SPDX-License-Identifier: GPL-2.0
3 * A power allocator to manage temperature
5 * Copyright (C) 2014 ARM Ltd.
9 #define pr_fmt(fmt) "Power allocator: " fmt
11 #include <linux/rculist.h>
12 #include <linux/slab.h>
13 #include <linux/thermal.h>
15 #define CREATE_TRACE_POINTS
16 #include <trace/events/thermal_power_allocator.h>
18 #include "thermal_core.h"
20 #define INVALID_TRIP -1
23 #define int_to_frac(x) ((x) << FRAC_BITS)
24 #define frac_to_int(x) ((x) >> FRAC_BITS)
27 * mul_frac() - multiply two fixed-point numbers
28 * @x: first multiplicand
29 * @y: second multiplicand
31 * Return: the result of multiplying two fixed-point numbers. The
32 * result is also a fixed-point number.
34 static inline s64 mul_frac(s64 x, s64 y)
36 return (x * y) >> FRAC_BITS;
40 * div_frac() - divide two fixed-point numbers
44 * Return: the result of dividing two fixed-point numbers. The
45 * result is also a fixed-point number.
47 static inline s64 div_frac(s64 x, s64 y)
49 return div_s64(x << FRAC_BITS, y);
53 * struct power_allocator_params - parameters for the power allocator governor
54 * @allocated_tzp: whether we have allocated tzp for this thermal zone and
55 * it needs to be freed on unbind
56 * @err_integral: accumulated error in the PID controller.
57 * @prev_err: error in the previous iteration of the PID controller.
58 * Used to calculate the derivative term.
59 * @trip_switch_on: first passive trip point of the thermal zone. The
60 * governor switches on when this trip point is crossed.
61 * If the thermal zone only has one passive trip point,
62 * @trip_switch_on should be INVALID_TRIP.
63 * @trip_max_desired_temperature: last passive trip point of the thermal
64 * zone. The temperature we are
67 struct power_allocator_params {
72 int trip_max_desired_temperature;
76 * estimate_sustainable_power() - Estimate the sustainable power of a thermal zone
77 * @tz: thermal zone we are operating in
79 * For thermal zones that don't provide a sustainable_power in their
80 * thermal_zone_params, estimate one. Calculate it using the minimum
81 * power of all the cooling devices as that gives a valid value that
82 * can give some degree of functionality. For optimal performance of
83 * this governor, provide a sustainable_power in the thermal zone's
84 * thermal_zone_params.
86 static u32 estimate_sustainable_power(struct thermal_zone_device *tz)
88 u32 sustainable_power = 0;
89 struct thermal_instance *instance;
90 struct power_allocator_params *params = tz->governor_data;
92 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
93 struct thermal_cooling_device *cdev = instance->cdev;
96 if (instance->trip != params->trip_max_desired_temperature)
99 if (!cdev_is_power_actor(cdev))
102 if (cdev->ops->state2power(cdev, instance->upper, &min_power))
105 sustainable_power += min_power;
108 return sustainable_power;
112 * estimate_pid_constants() - Estimate the constants for the PID controller
113 * @tz: thermal zone for which to estimate the constants
114 * @sustainable_power: sustainable power for the thermal zone
115 * @trip_switch_on: trip point number for the switch on temperature
116 * @control_temp: target temperature for the power allocator governor
117 * @force: whether to force the update of the constants
119 * This function is used to update the estimation of the PID
120 * controller constants in struct thermal_zone_parameters.
121 * Sustainable power is provided in case it was estimated. The
122 * estimated sustainable_power should not be stored in the
123 * thermal_zone_parameters so it has to be passed explicitly to this
126 * If @force is not set, the values in the thermal zone's parameters
127 * are preserved if they are not zero. If @force is set, the values
128 * in thermal zone's parameters are overwritten.
130 static void estimate_pid_constants(struct thermal_zone_device *tz,
131 u32 sustainable_power, int trip_switch_on,
132 int control_temp, bool force)
136 u32 temperature_threshold;
138 ret = tz->ops->get_trip_temp(tz, trip_switch_on, &switch_on_temp);
142 temperature_threshold = control_temp - switch_on_temp;
144 * estimate_pid_constants() tries to find appropriate default
145 * values for thermal zones that don't provide them. If a
146 * system integrator has configured a thermal zone with two
147 * passive trip points at the same temperature, that person
148 * hasn't put any effort to set up the thermal zone properly
151 if (!temperature_threshold)
154 if (!tz->tzp->k_po || force)
155 tz->tzp->k_po = int_to_frac(sustainable_power) /
156 temperature_threshold;
158 if (!tz->tzp->k_pu || force)
159 tz->tzp->k_pu = int_to_frac(2 * sustainable_power) /
160 temperature_threshold;
162 if (!tz->tzp->k_i || force)
163 tz->tzp->k_i = int_to_frac(10) / 1000;
165 * The default for k_d and integral_cutoff is 0, so we can
166 * leave them as they are.
171 * pid_controller() - PID controller
172 * @tz: thermal zone we are operating in
173 * @control_temp: the target temperature in millicelsius
174 * @max_allocatable_power: maximum allocatable power for this thermal zone
176 * This PID controller increases the available power budget so that the
177 * temperature of the thermal zone gets as close as possible to
178 * @control_temp and limits the power if it exceeds it. k_po is the
179 * proportional term when we are overshooting, k_pu is the
180 * proportional term when we are undershooting. integral_cutoff is a
181 * threshold below which we stop accumulating the error. The
182 * accumulated error is only valid if the requested power will make
183 * the system warmer. If the system is mostly idle, there's no point
184 * in accumulating positive error.
186 * Return: The power budget for the next period.
188 static u32 pid_controller(struct thermal_zone_device *tz,
190 u32 max_allocatable_power)
192 s64 p, i, d, power_range;
193 s32 err, max_power_frac;
194 u32 sustainable_power;
195 struct power_allocator_params *params = tz->governor_data;
197 max_power_frac = int_to_frac(max_allocatable_power);
199 if (tz->tzp->sustainable_power) {
200 sustainable_power = tz->tzp->sustainable_power;
202 sustainable_power = estimate_sustainable_power(tz);
203 estimate_pid_constants(tz, sustainable_power,
204 params->trip_switch_on, control_temp,
208 err = control_temp - tz->temperature;
209 err = int_to_frac(err);
211 /* Calculate the proportional term */
212 p = mul_frac(err < 0 ? tz->tzp->k_po : tz->tzp->k_pu, err);
215 * Calculate the integral term
217 * if the error is less than cut off allow integration (but
218 * the integral is limited to max power)
220 i = mul_frac(tz->tzp->k_i, params->err_integral);
222 if (err < int_to_frac(tz->tzp->integral_cutoff)) {
223 s64 i_next = i + mul_frac(tz->tzp->k_i, err);
225 if (abs(i_next) < max_power_frac) {
227 params->err_integral += err;
232 * Calculate the derivative term
234 * We do err - prev_err, so with a positive k_d, a decreasing
235 * error (i.e. driving closer to the line) results in less
236 * power being applied, slowing down the controller)
238 d = mul_frac(tz->tzp->k_d, err - params->prev_err);
239 d = div_frac(d, tz->passive_delay);
240 params->prev_err = err;
242 power_range = p + i + d;
244 /* feed-forward the known sustainable dissipatable power */
245 power_range = sustainable_power + frac_to_int(power_range);
247 power_range = clamp(power_range, (s64)0, (s64)max_allocatable_power);
249 trace_thermal_power_allocator_pid(tz, frac_to_int(err),
250 frac_to_int(params->err_integral),
251 frac_to_int(p), frac_to_int(i),
252 frac_to_int(d), power_range);
258 * divvy_up_power() - divvy the allocated power between the actors
259 * @req_power: each actor's requested power
260 * @max_power: each actor's maximum available power
261 * @num_actors: size of the @req_power, @max_power and @granted_power's array
262 * @total_req_power: sum of @req_power
263 * @power_range: total allocated power
264 * @granted_power: output array: each actor's granted power
265 * @extra_actor_power: an appropriately sized array to be used in the
266 * function as temporary storage of the extra power given
269 * This function divides the total allocated power (@power_range)
270 * fairly between the actors. It first tries to give each actor a
271 * share of the @power_range according to how much power it requested
272 * compared to the rest of the actors. For example, if only one actor
273 * requests power, then it receives all the @power_range. If
274 * three actors each requests 1mW, each receives a third of the
277 * If any actor received more than their maximum power, then that
278 * surplus is re-divvied among the actors based on how far they are
279 * from their respective maximums.
281 * Granted power for each actor is written to @granted_power, which
282 * should've been allocated by the calling function.
284 static void divvy_up_power(u32 *req_power, u32 *max_power, int num_actors,
285 u32 total_req_power, u32 power_range,
286 u32 *granted_power, u32 *extra_actor_power)
288 u32 extra_power, capped_extra_power;
292 * Prevent division by 0 if none of the actors request power.
294 if (!total_req_power)
297 capped_extra_power = 0;
299 for (i = 0; i < num_actors; i++) {
300 u64 req_range = (u64)req_power[i] * power_range;
302 granted_power[i] = DIV_ROUND_CLOSEST_ULL(req_range,
305 if (granted_power[i] > max_power[i]) {
306 extra_power += granted_power[i] - max_power[i];
307 granted_power[i] = max_power[i];
310 extra_actor_power[i] = max_power[i] - granted_power[i];
311 capped_extra_power += extra_actor_power[i];
318 * Re-divvy the reclaimed extra among actors based on
319 * how far they are from the max
321 extra_power = min(extra_power, capped_extra_power);
322 if (capped_extra_power > 0)
323 for (i = 0; i < num_actors; i++)
324 granted_power[i] += (extra_actor_power[i] *
325 extra_power) / capped_extra_power;
328 static int allocate_power(struct thermal_zone_device *tz,
331 struct thermal_instance *instance;
332 struct power_allocator_params *params = tz->governor_data;
333 u32 *req_power, *max_power, *granted_power, *extra_actor_power;
334 u32 *weighted_req_power;
335 u32 total_req_power, max_allocatable_power, total_weighted_req_power;
336 u32 total_granted_power, power_range;
337 int i, num_actors, total_weight, ret = 0;
338 int trip_max_desired_temperature = params->trip_max_desired_temperature;
340 mutex_lock(&tz->lock);
344 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
345 if ((instance->trip == trip_max_desired_temperature) &&
346 cdev_is_power_actor(instance->cdev)) {
348 total_weight += instance->weight;
358 * We need to allocate five arrays of the same size:
359 * req_power, max_power, granted_power, extra_actor_power and
360 * weighted_req_power. They are going to be needed until this
361 * function returns. Allocate them all in one go to simplify
362 * the allocation and deallocation logic.
364 BUILD_BUG_ON(sizeof(*req_power) != sizeof(*max_power));
365 BUILD_BUG_ON(sizeof(*req_power) != sizeof(*granted_power));
366 BUILD_BUG_ON(sizeof(*req_power) != sizeof(*extra_actor_power));
367 BUILD_BUG_ON(sizeof(*req_power) != sizeof(*weighted_req_power));
368 req_power = kcalloc(num_actors * 5, sizeof(*req_power), GFP_KERNEL);
374 max_power = &req_power[num_actors];
375 granted_power = &req_power[2 * num_actors];
376 extra_actor_power = &req_power[3 * num_actors];
377 weighted_req_power = &req_power[4 * num_actors];
380 total_weighted_req_power = 0;
382 max_allocatable_power = 0;
384 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
386 struct thermal_cooling_device *cdev = instance->cdev;
388 if (instance->trip != trip_max_desired_temperature)
391 if (!cdev_is_power_actor(cdev))
394 if (cdev->ops->get_requested_power(cdev, &req_power[i]))
398 weight = 1 << FRAC_BITS;
400 weight = instance->weight;
402 weighted_req_power[i] = frac_to_int(weight * req_power[i]);
404 if (cdev->ops->state2power(cdev, instance->lower,
408 total_req_power += req_power[i];
409 max_allocatable_power += max_power[i];
410 total_weighted_req_power += weighted_req_power[i];
415 power_range = pid_controller(tz, control_temp, max_allocatable_power);
417 divvy_up_power(weighted_req_power, max_power, num_actors,
418 total_weighted_req_power, power_range, granted_power,
421 total_granted_power = 0;
423 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
424 if (instance->trip != trip_max_desired_temperature)
427 if (!cdev_is_power_actor(instance->cdev))
430 power_actor_set_power(instance->cdev, instance,
432 total_granted_power += granted_power[i];
437 trace_thermal_power_allocator(tz, req_power, total_req_power,
438 granted_power, total_granted_power,
439 num_actors, power_range,
440 max_allocatable_power, tz->temperature,
441 control_temp - tz->temperature);
445 mutex_unlock(&tz->lock);
451 * get_governor_trips() - get the number of the two trip points that are key for this governor
452 * @tz: thermal zone to operate on
453 * @params: pointer to private data for this governor
455 * The power allocator governor works optimally with two trips points:
456 * a "switch on" trip point and a "maximum desired temperature". These
457 * are defined as the first and last passive trip points.
459 * If there is only one trip point, then that's considered to be the
460 * "maximum desired temperature" trip point and the governor is always
461 * on. If there are no passive or active trip points, then the
462 * governor won't do anything. In fact, its throttle function
463 * won't be called at all.
465 static void get_governor_trips(struct thermal_zone_device *tz,
466 struct power_allocator_params *params)
468 int i, last_active, last_passive;
469 bool found_first_passive;
471 found_first_passive = false;
472 last_active = INVALID_TRIP;
473 last_passive = INVALID_TRIP;
475 for (i = 0; i < tz->trips; i++) {
476 enum thermal_trip_type type;
479 ret = tz->ops->get_trip_type(tz, i, &type);
481 dev_warn(&tz->device,
482 "Failed to get trip point %d type: %d\n", i,
487 if (type == THERMAL_TRIP_PASSIVE) {
488 if (!found_first_passive) {
489 params->trip_switch_on = i;
490 found_first_passive = true;
494 } else if (type == THERMAL_TRIP_ACTIVE) {
501 if (last_passive != INVALID_TRIP) {
502 params->trip_max_desired_temperature = last_passive;
503 } else if (found_first_passive) {
504 params->trip_max_desired_temperature = params->trip_switch_on;
505 params->trip_switch_on = INVALID_TRIP;
507 params->trip_switch_on = INVALID_TRIP;
508 params->trip_max_desired_temperature = last_active;
512 static void reset_pid_controller(struct power_allocator_params *params)
514 params->err_integral = 0;
515 params->prev_err = 0;
518 static void allow_maximum_power(struct thermal_zone_device *tz)
520 struct thermal_instance *instance;
521 struct power_allocator_params *params = tz->governor_data;
523 mutex_lock(&tz->lock);
524 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
525 if ((instance->trip != params->trip_max_desired_temperature) ||
526 (!cdev_is_power_actor(instance->cdev)))
529 instance->target = 0;
530 mutex_lock(&instance->cdev->lock);
531 instance->cdev->updated = false;
532 mutex_unlock(&instance->cdev->lock);
533 thermal_cdev_update(instance->cdev);
535 mutex_unlock(&tz->lock);
539 * power_allocator_bind() - bind the power_allocator governor to a thermal zone
540 * @tz: thermal zone to bind it to
542 * Initialize the PID controller parameters and bind it to the thermal
545 * Return: 0 on success, or -ENOMEM if we ran out of memory.
547 static int power_allocator_bind(struct thermal_zone_device *tz)
550 struct power_allocator_params *params;
553 params = kzalloc(sizeof(*params), GFP_KERNEL);
558 tz->tzp = kzalloc(sizeof(*tz->tzp), GFP_KERNEL);
564 params->allocated_tzp = true;
567 if (!tz->tzp->sustainable_power)
568 dev_warn(&tz->device, "power_allocator: sustainable_power will be estimated\n");
570 get_governor_trips(tz, params);
573 ret = tz->ops->get_trip_temp(tz,
574 params->trip_max_desired_temperature,
577 estimate_pid_constants(tz, tz->tzp->sustainable_power,
578 params->trip_switch_on,
579 control_temp, false);
582 reset_pid_controller(params);
584 tz->governor_data = params;
594 static void power_allocator_unbind(struct thermal_zone_device *tz)
596 struct power_allocator_params *params = tz->governor_data;
598 dev_dbg(&tz->device, "Unbinding from thermal zone %d\n", tz->id);
600 if (params->allocated_tzp) {
605 kfree(tz->governor_data);
606 tz->governor_data = NULL;
609 static int power_allocator_throttle(struct thermal_zone_device *tz, int trip)
612 int switch_on_temp, control_temp;
613 struct power_allocator_params *params = tz->governor_data;
616 * We get called for every trip point but we only need to do
617 * our calculations once
619 if (trip != params->trip_max_desired_temperature)
622 ret = tz->ops->get_trip_temp(tz, params->trip_switch_on,
624 if (!ret && (tz->temperature < switch_on_temp)) {
626 reset_pid_controller(params);
627 allow_maximum_power(tz);
633 ret = tz->ops->get_trip_temp(tz, params->trip_max_desired_temperature,
636 dev_warn(&tz->device,
637 "Failed to get the maximum desired temperature: %d\n",
642 return allocate_power(tz, control_temp);
645 static struct thermal_governor thermal_gov_power_allocator = {
646 .name = "power_allocator",
647 .bind_to_tz = power_allocator_bind,
648 .unbind_from_tz = power_allocator_unbind,
649 .throttle = power_allocator_throttle,
651 THERMAL_GOVERNOR_DECLARE(thermal_gov_power_allocator);