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 (power_actor_get_min_power(cdev, &min_power))
102 sustainable_power += min_power;
105 return sustainable_power;
109 * estimate_pid_constants() - Estimate the constants for the PID controller
110 * @tz: thermal zone for which to estimate the constants
111 * @sustainable_power: sustainable power for the thermal zone
112 * @trip_switch_on: trip point number for the switch on temperature
113 * @control_temp: target temperature for the power allocator governor
114 * @force: whether to force the update of the constants
116 * This function is used to update the estimation of the PID
117 * controller constants in struct thermal_zone_parameters.
118 * Sustainable power is provided in case it was estimated. The
119 * estimated sustainable_power should not be stored in the
120 * thermal_zone_parameters so it has to be passed explicitly to this
123 * If @force is not set, the values in the thermal zone's parameters
124 * are preserved if they are not zero. If @force is set, the values
125 * in thermal zone's parameters are overwritten.
127 static void estimate_pid_constants(struct thermal_zone_device *tz,
128 u32 sustainable_power, int trip_switch_on,
129 int control_temp, bool force)
133 u32 temperature_threshold;
135 ret = tz->ops->get_trip_temp(tz, trip_switch_on, &switch_on_temp);
139 temperature_threshold = control_temp - switch_on_temp;
141 * estimate_pid_constants() tries to find appropriate default
142 * values for thermal zones that don't provide them. If a
143 * system integrator has configured a thermal zone with two
144 * passive trip points at the same temperature, that person
145 * hasn't put any effort to set up the thermal zone properly
148 if (!temperature_threshold)
151 if (!tz->tzp->k_po || force)
152 tz->tzp->k_po = int_to_frac(sustainable_power) /
153 temperature_threshold;
155 if (!tz->tzp->k_pu || force)
156 tz->tzp->k_pu = int_to_frac(2 * sustainable_power) /
157 temperature_threshold;
159 if (!tz->tzp->k_i || force)
160 tz->tzp->k_i = int_to_frac(10) / 1000;
162 * The default for k_d and integral_cutoff is 0, so we can
163 * leave them as they are.
168 * pid_controller() - PID controller
169 * @tz: thermal zone we are operating in
170 * @control_temp: the target temperature in millicelsius
171 * @max_allocatable_power: maximum allocatable power for this thermal zone
173 * This PID controller increases the available power budget so that the
174 * temperature of the thermal zone gets as close as possible to
175 * @control_temp and limits the power if it exceeds it. k_po is the
176 * proportional term when we are overshooting, k_pu is the
177 * proportional term when we are undershooting. integral_cutoff is a
178 * threshold below which we stop accumulating the error. The
179 * accumulated error is only valid if the requested power will make
180 * the system warmer. If the system is mostly idle, there's no point
181 * in accumulating positive error.
183 * Return: The power budget for the next period.
185 static u32 pid_controller(struct thermal_zone_device *tz,
187 u32 max_allocatable_power)
189 s64 p, i, d, power_range;
190 s32 err, max_power_frac;
191 u32 sustainable_power;
192 struct power_allocator_params *params = tz->governor_data;
194 max_power_frac = int_to_frac(max_allocatable_power);
196 if (tz->tzp->sustainable_power) {
197 sustainable_power = tz->tzp->sustainable_power;
199 sustainable_power = estimate_sustainable_power(tz);
200 estimate_pid_constants(tz, sustainable_power,
201 params->trip_switch_on, control_temp,
205 err = control_temp - tz->temperature;
206 err = int_to_frac(err);
208 /* Calculate the proportional term */
209 p = mul_frac(err < 0 ? tz->tzp->k_po : tz->tzp->k_pu, err);
212 * Calculate the integral term
214 * if the error is less than cut off allow integration (but
215 * the integral is limited to max power)
217 i = mul_frac(tz->tzp->k_i, params->err_integral);
219 if (err < int_to_frac(tz->tzp->integral_cutoff)) {
220 s64 i_next = i + mul_frac(tz->tzp->k_i, err);
222 if (abs(i_next) < max_power_frac) {
224 params->err_integral += err;
229 * Calculate the derivative term
231 * We do err - prev_err, so with a positive k_d, a decreasing
232 * error (i.e. driving closer to the line) results in less
233 * power being applied, slowing down the controller)
235 d = mul_frac(tz->tzp->k_d, err - params->prev_err);
236 d = div_frac(d, tz->passive_delay);
237 params->prev_err = err;
239 power_range = p + i + d;
241 /* feed-forward the known sustainable dissipatable power */
242 power_range = sustainable_power + frac_to_int(power_range);
244 power_range = clamp(power_range, (s64)0, (s64)max_allocatable_power);
246 trace_thermal_power_allocator_pid(tz, frac_to_int(err),
247 frac_to_int(params->err_integral),
248 frac_to_int(p), frac_to_int(i),
249 frac_to_int(d), power_range);
255 * divvy_up_power() - divvy the allocated power between the actors
256 * @req_power: each actor's requested power
257 * @max_power: each actor's maximum available power
258 * @num_actors: size of the @req_power, @max_power and @granted_power's array
259 * @total_req_power: sum of @req_power
260 * @power_range: total allocated power
261 * @granted_power: output array: each actor's granted power
262 * @extra_actor_power: an appropriately sized array to be used in the
263 * function as temporary storage of the extra power given
266 * This function divides the total allocated power (@power_range)
267 * fairly between the actors. It first tries to give each actor a
268 * share of the @power_range according to how much power it requested
269 * compared to the rest of the actors. For example, if only one actor
270 * requests power, then it receives all the @power_range. If
271 * three actors each requests 1mW, each receives a third of the
274 * If any actor received more than their maximum power, then that
275 * surplus is re-divvied among the actors based on how far they are
276 * from their respective maximums.
278 * Granted power for each actor is written to @granted_power, which
279 * should've been allocated by the calling function.
281 static void divvy_up_power(u32 *req_power, u32 *max_power, int num_actors,
282 u32 total_req_power, u32 power_range,
283 u32 *granted_power, u32 *extra_actor_power)
285 u32 extra_power, capped_extra_power;
289 * Prevent division by 0 if none of the actors request power.
291 if (!total_req_power)
294 capped_extra_power = 0;
296 for (i = 0; i < num_actors; i++) {
297 u64 req_range = (u64)req_power[i] * power_range;
299 granted_power[i] = DIV_ROUND_CLOSEST_ULL(req_range,
302 if (granted_power[i] > max_power[i]) {
303 extra_power += granted_power[i] - max_power[i];
304 granted_power[i] = max_power[i];
307 extra_actor_power[i] = max_power[i] - granted_power[i];
308 capped_extra_power += extra_actor_power[i];
315 * Re-divvy the reclaimed extra among actors based on
316 * how far they are from the max
318 extra_power = min(extra_power, capped_extra_power);
319 if (capped_extra_power > 0)
320 for (i = 0; i < num_actors; i++)
321 granted_power[i] += (extra_actor_power[i] *
322 extra_power) / capped_extra_power;
325 static int allocate_power(struct thermal_zone_device *tz,
328 struct thermal_instance *instance;
329 struct power_allocator_params *params = tz->governor_data;
330 u32 *req_power, *max_power, *granted_power, *extra_actor_power;
331 u32 *weighted_req_power;
332 u32 total_req_power, max_allocatable_power, total_weighted_req_power;
333 u32 total_granted_power, power_range;
334 int i, num_actors, total_weight, ret = 0;
335 int trip_max_desired_temperature = params->trip_max_desired_temperature;
337 mutex_lock(&tz->lock);
341 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
342 if ((instance->trip == trip_max_desired_temperature) &&
343 cdev_is_power_actor(instance->cdev)) {
345 total_weight += instance->weight;
355 * We need to allocate five arrays of the same size:
356 * req_power, max_power, granted_power, extra_actor_power and
357 * weighted_req_power. They are going to be needed until this
358 * function returns. Allocate them all in one go to simplify
359 * the allocation and deallocation logic.
361 BUILD_BUG_ON(sizeof(*req_power) != sizeof(*max_power));
362 BUILD_BUG_ON(sizeof(*req_power) != sizeof(*granted_power));
363 BUILD_BUG_ON(sizeof(*req_power) != sizeof(*extra_actor_power));
364 BUILD_BUG_ON(sizeof(*req_power) != sizeof(*weighted_req_power));
365 req_power = kcalloc(num_actors * 5, sizeof(*req_power), GFP_KERNEL);
371 max_power = &req_power[num_actors];
372 granted_power = &req_power[2 * num_actors];
373 extra_actor_power = &req_power[3 * num_actors];
374 weighted_req_power = &req_power[4 * num_actors];
377 total_weighted_req_power = 0;
379 max_allocatable_power = 0;
381 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
383 struct thermal_cooling_device *cdev = instance->cdev;
385 if (instance->trip != trip_max_desired_temperature)
388 if (!cdev_is_power_actor(cdev))
391 if (cdev->ops->get_requested_power(cdev, &req_power[i]))
395 weight = 1 << FRAC_BITS;
397 weight = instance->weight;
399 weighted_req_power[i] = frac_to_int(weight * req_power[i]);
401 if (power_actor_get_max_power(cdev, &max_power[i]))
404 total_req_power += req_power[i];
405 max_allocatable_power += max_power[i];
406 total_weighted_req_power += weighted_req_power[i];
411 power_range = pid_controller(tz, control_temp, max_allocatable_power);
413 divvy_up_power(weighted_req_power, max_power, num_actors,
414 total_weighted_req_power, power_range, granted_power,
417 total_granted_power = 0;
419 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
420 if (instance->trip != trip_max_desired_temperature)
423 if (!cdev_is_power_actor(instance->cdev))
426 power_actor_set_power(instance->cdev, instance,
428 total_granted_power += granted_power[i];
433 trace_thermal_power_allocator(tz, req_power, total_req_power,
434 granted_power, total_granted_power,
435 num_actors, power_range,
436 max_allocatable_power, tz->temperature,
437 control_temp - tz->temperature);
441 mutex_unlock(&tz->lock);
447 * get_governor_trips() - get the number of the two trip points that are key for this governor
448 * @tz: thermal zone to operate on
449 * @params: pointer to private data for this governor
451 * The power allocator governor works optimally with two trips points:
452 * a "switch on" trip point and a "maximum desired temperature". These
453 * are defined as the first and last passive trip points.
455 * If there is only one trip point, then that's considered to be the
456 * "maximum desired temperature" trip point and the governor is always
457 * on. If there are no passive or active trip points, then the
458 * governor won't do anything. In fact, its throttle function
459 * won't be called at all.
461 static void get_governor_trips(struct thermal_zone_device *tz,
462 struct power_allocator_params *params)
464 int i, last_active, last_passive;
465 bool found_first_passive;
467 found_first_passive = false;
468 last_active = INVALID_TRIP;
469 last_passive = INVALID_TRIP;
471 for (i = 0; i < tz->trips; i++) {
472 enum thermal_trip_type type;
475 ret = tz->ops->get_trip_type(tz, i, &type);
477 dev_warn(&tz->device,
478 "Failed to get trip point %d type: %d\n", i,
483 if (type == THERMAL_TRIP_PASSIVE) {
484 if (!found_first_passive) {
485 params->trip_switch_on = i;
486 found_first_passive = true;
490 } else if (type == THERMAL_TRIP_ACTIVE) {
497 if (last_passive != INVALID_TRIP) {
498 params->trip_max_desired_temperature = last_passive;
499 } else if (found_first_passive) {
500 params->trip_max_desired_temperature = params->trip_switch_on;
501 params->trip_switch_on = INVALID_TRIP;
503 params->trip_switch_on = INVALID_TRIP;
504 params->trip_max_desired_temperature = last_active;
508 static void reset_pid_controller(struct power_allocator_params *params)
510 params->err_integral = 0;
511 params->prev_err = 0;
514 static void allow_maximum_power(struct thermal_zone_device *tz)
516 struct thermal_instance *instance;
517 struct power_allocator_params *params = tz->governor_data;
519 mutex_lock(&tz->lock);
520 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
521 if ((instance->trip != params->trip_max_desired_temperature) ||
522 (!cdev_is_power_actor(instance->cdev)))
525 instance->target = 0;
526 mutex_lock(&instance->cdev->lock);
527 instance->cdev->updated = false;
528 mutex_unlock(&instance->cdev->lock);
529 thermal_cdev_update(instance->cdev);
531 mutex_unlock(&tz->lock);
535 * power_allocator_bind() - bind the power_allocator governor to a thermal zone
536 * @tz: thermal zone to bind it to
538 * Initialize the PID controller parameters and bind it to the thermal
541 * Return: 0 on success, or -ENOMEM if we ran out of memory.
543 static int power_allocator_bind(struct thermal_zone_device *tz)
546 struct power_allocator_params *params;
549 params = kzalloc(sizeof(*params), GFP_KERNEL);
554 tz->tzp = kzalloc(sizeof(*tz->tzp), GFP_KERNEL);
560 params->allocated_tzp = true;
563 if (!tz->tzp->sustainable_power)
564 dev_warn(&tz->device, "power_allocator: sustainable_power will be estimated\n");
566 get_governor_trips(tz, params);
569 ret = tz->ops->get_trip_temp(tz,
570 params->trip_max_desired_temperature,
573 estimate_pid_constants(tz, tz->tzp->sustainable_power,
574 params->trip_switch_on,
575 control_temp, false);
578 reset_pid_controller(params);
580 tz->governor_data = params;
590 static void power_allocator_unbind(struct thermal_zone_device *tz)
592 struct power_allocator_params *params = tz->governor_data;
594 dev_dbg(&tz->device, "Unbinding from thermal zone %d\n", tz->id);
596 if (params->allocated_tzp) {
601 kfree(tz->governor_data);
602 tz->governor_data = NULL;
605 static int power_allocator_throttle(struct thermal_zone_device *tz, int trip)
608 int switch_on_temp, control_temp;
609 struct power_allocator_params *params = tz->governor_data;
612 * We get called for every trip point but we only need to do
613 * our calculations once
615 if (trip != params->trip_max_desired_temperature)
618 ret = tz->ops->get_trip_temp(tz, params->trip_switch_on,
620 if (!ret && (tz->temperature < switch_on_temp)) {
622 reset_pid_controller(params);
623 allow_maximum_power(tz);
629 ret = tz->ops->get_trip_temp(tz, params->trip_max_desired_temperature,
632 dev_warn(&tz->device,
633 "Failed to get the maximum desired temperature: %d\n",
638 return allocate_power(tz, control_temp);
641 static struct thermal_governor thermal_gov_power_allocator = {
642 .name = "power_allocator",
643 .bind_to_tz = power_allocator_bind,
644 .unbind_from_tz = power_allocator_unbind,
645 .throttle = power_allocator_throttle,
647 THERMAL_GOVERNOR_DECLARE(thermal_gov_power_allocator);