2 * A power allocator to manage temperature
4 * Copyright (C) 2014 ARM Ltd.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
11 * kind, whether express or implied; without even the implied warranty
12 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #define pr_fmt(fmt) "Power allocator: " fmt
18 #include <linux/rculist.h>
19 #include <linux/slab.h>
20 #include <linux/thermal.h>
22 #define CREATE_TRACE_POINTS
23 #include <trace/events/thermal_power_allocator.h>
25 #include "thermal_core.h"
27 #define INVALID_TRIP -1
30 #define int_to_frac(x) ((x) << FRAC_BITS)
31 #define frac_to_int(x) ((x) >> FRAC_BITS)
34 * mul_frac() - multiply two fixed-point numbers
35 * @x: first multiplicand
36 * @y: second multiplicand
38 * Return: the result of multiplying two fixed-point numbers. The
39 * result is also a fixed-point number.
41 static inline s64
mul_frac(s64 x
, s64 y
)
43 return (x
* y
) >> FRAC_BITS
;
47 * div_frac() - divide two fixed-point numbers
51 * Return: the result of dividing two fixed-point numbers. The
52 * result is also a fixed-point number.
54 static inline s64
div_frac(s64 x
, s64 y
)
56 return div_s64(x
<< FRAC_BITS
, y
);
60 * struct power_allocator_params - parameters for the power allocator governor
61 * @allocated_tzp: whether we have allocated tzp for this thermal zone and
62 * it needs to be freed on unbind
63 * @err_integral: accumulated error in the PID controller.
64 * @prev_err: error in the previous iteration of the PID controller.
65 * Used to calculate the derivative term.
66 * @trip_switch_on: first passive trip point of the thermal zone. The
67 * governor switches on when this trip point is crossed.
68 * If the thermal zone only has one passive trip point,
69 * @trip_switch_on should be INVALID_TRIP.
70 * @trip_max_desired_temperature: last passive trip point of the thermal
71 * zone. The temperature we are
74 struct power_allocator_params
{
79 int trip_max_desired_temperature
;
83 * estimate_sustainable_power() - Estimate the sustainable power of a thermal zone
84 * @tz: thermal zone we are operating in
86 * For thermal zones that don't provide a sustainable_power in their
87 * thermal_zone_params, estimate one. Calculate it using the minimum
88 * power of all the cooling devices as that gives a valid value that
89 * can give some degree of functionality. For optimal performance of
90 * this governor, provide a sustainable_power in the thermal zone's
91 * thermal_zone_params.
93 static u32
estimate_sustainable_power(struct thermal_zone_device
*tz
)
95 u32 sustainable_power
= 0;
96 struct thermal_instance
*instance
;
97 struct power_allocator_params
*params
= tz
->governor_data
;
99 list_for_each_entry(instance
, &tz
->thermal_instances
, tz_node
) {
100 struct thermal_cooling_device
*cdev
= instance
->cdev
;
103 if (instance
->trip
!= params
->trip_max_desired_temperature
)
106 if (power_actor_get_min_power(cdev
, tz
, &min_power
))
109 sustainable_power
+= min_power
;
112 return sustainable_power
;
116 * estimate_pid_constants() - Estimate the constants for the PID controller
117 * @tz: thermal zone for which to estimate the constants
118 * @sustainable_power: sustainable power for the thermal zone
119 * @trip_switch_on: trip point number for the switch on temperature
120 * @control_temp: target temperature for the power allocator governor
121 * @force: whether to force the update of the constants
123 * This function is used to update the estimation of the PID
124 * controller constants in struct thermal_zone_parameters.
125 * Sustainable power is provided in case it was estimated. The
126 * estimated sustainable_power should not be stored in the
127 * thermal_zone_parameters so it has to be passed explicitly to this
130 * If @force is not set, the values in the thermal zone's parameters
131 * are preserved if they are not zero. If @force is set, the values
132 * in thermal zone's parameters are overwritten.
134 static void estimate_pid_constants(struct thermal_zone_device
*tz
,
135 u32 sustainable_power
, int trip_switch_on
,
136 int control_temp
, bool force
)
140 u32 temperature_threshold
;
142 ret
= tz
->ops
->get_trip_temp(tz
, trip_switch_on
, &switch_on_temp
);
146 temperature_threshold
= control_temp
- switch_on_temp
;
148 * estimate_pid_constants() tries to find appropriate default
149 * values for thermal zones that don't provide them. If a
150 * system integrator has configured a thermal zone with two
151 * passive trip points at the same temperature, that person
152 * hasn't put any effort to set up the thermal zone properly
155 if (!temperature_threshold
)
158 if (!tz
->tzp
->k_po
|| force
)
159 tz
->tzp
->k_po
= int_to_frac(sustainable_power
) /
160 temperature_threshold
;
162 if (!tz
->tzp
->k_pu
|| force
)
163 tz
->tzp
->k_pu
= int_to_frac(2 * sustainable_power
) /
164 temperature_threshold
;
166 if (!tz
->tzp
->k_i
|| force
)
167 tz
->tzp
->k_i
= int_to_frac(10) / 1000;
169 * The default for k_d and integral_cutoff is 0, so we can
170 * leave them as they are.
175 * pid_controller() - PID controller
176 * @tz: thermal zone we are operating in
177 * @current_temp: the current temperature in millicelsius
178 * @control_temp: the target temperature in millicelsius
179 * @max_allocatable_power: maximum allocatable power for this thermal zone
181 * This PID controller increases the available power budget so that the
182 * temperature of the thermal zone gets as close as possible to
183 * @control_temp and limits the power if it exceeds it. k_po is the
184 * proportional term when we are overshooting, k_pu is the
185 * proportional term when we are undershooting. integral_cutoff is a
186 * threshold below which we stop accumulating the error. The
187 * accumulated error is only valid if the requested power will make
188 * the system warmer. If the system is mostly idle, there's no point
189 * in accumulating positive error.
191 * Return: The power budget for the next period.
193 static u32
pid_controller(struct thermal_zone_device
*tz
,
196 u32 max_allocatable_power
)
198 s64 p
, i
, d
, power_range
;
199 s32 err
, max_power_frac
;
200 u32 sustainable_power
;
201 struct power_allocator_params
*params
= tz
->governor_data
;
203 max_power_frac
= int_to_frac(max_allocatable_power
);
205 if (tz
->tzp
->sustainable_power
) {
206 sustainable_power
= tz
->tzp
->sustainable_power
;
208 sustainable_power
= estimate_sustainable_power(tz
);
209 estimate_pid_constants(tz
, sustainable_power
,
210 params
->trip_switch_on
, control_temp
,
214 err
= control_temp
- current_temp
;
215 err
= int_to_frac(err
);
217 /* Calculate the proportional term */
218 p
= mul_frac(err
< 0 ? tz
->tzp
->k_po
: tz
->tzp
->k_pu
, err
);
221 * Calculate the integral term
223 * if the error is less than cut off allow integration (but
224 * the integral is limited to max power)
226 i
= mul_frac(tz
->tzp
->k_i
, params
->err_integral
);
228 if (err
< int_to_frac(tz
->tzp
->integral_cutoff
)) {
229 s64 i_next
= i
+ mul_frac(tz
->tzp
->k_i
, err
);
231 if (abs(i_next
) < max_power_frac
) {
233 params
->err_integral
+= err
;
238 * Calculate the derivative term
240 * We do err - prev_err, so with a positive k_d, a decreasing
241 * error (i.e. driving closer to the line) results in less
242 * power being applied, slowing down the controller)
244 d
= mul_frac(tz
->tzp
->k_d
, err
- params
->prev_err
);
245 d
= div_frac(d
, tz
->passive_delay
);
246 params
->prev_err
= err
;
248 power_range
= p
+ i
+ d
;
250 /* feed-forward the known sustainable dissipatable power */
251 power_range
= sustainable_power
+ frac_to_int(power_range
);
253 power_range
= clamp(power_range
, (s64
)0, (s64
)max_allocatable_power
);
255 trace_thermal_power_allocator_pid(tz
, frac_to_int(err
),
256 frac_to_int(params
->err_integral
),
257 frac_to_int(p
), frac_to_int(i
),
258 frac_to_int(d
), power_range
);
264 * divvy_up_power() - divvy the allocated power between the actors
265 * @req_power: each actor's requested power
266 * @max_power: each actor's maximum available power
267 * @num_actors: size of the @req_power, @max_power and @granted_power's array
268 * @total_req_power: sum of @req_power
269 * @power_range: total allocated power
270 * @granted_power: output array: each actor's granted power
271 * @extra_actor_power: an appropriately sized array to be used in the
272 * function as temporary storage of the extra power given
275 * This function divides the total allocated power (@power_range)
276 * fairly between the actors. It first tries to give each actor a
277 * share of the @power_range according to how much power it requested
278 * compared to the rest of the actors. For example, if only one actor
279 * requests power, then it receives all the @power_range. If
280 * three actors each requests 1mW, each receives a third of the
283 * If any actor received more than their maximum power, then that
284 * surplus is re-divvied among the actors based on how far they are
285 * from their respective maximums.
287 * Granted power for each actor is written to @granted_power, which
288 * should've been allocated by the calling function.
290 static void divvy_up_power(u32
*req_power
, u32
*max_power
, int num_actors
,
291 u32 total_req_power
, u32 power_range
,
292 u32
*granted_power
, u32
*extra_actor_power
)
294 u32 extra_power
, capped_extra_power
;
298 * Prevent division by 0 if none of the actors request power.
300 if (!total_req_power
)
303 capped_extra_power
= 0;
305 for (i
= 0; i
< num_actors
; i
++) {
306 u64 req_range
= req_power
[i
] * power_range
;
308 granted_power
[i
] = DIV_ROUND_CLOSEST_ULL(req_range
,
311 if (granted_power
[i
] > max_power
[i
]) {
312 extra_power
+= granted_power
[i
] - max_power
[i
];
313 granted_power
[i
] = max_power
[i
];
316 extra_actor_power
[i
] = max_power
[i
] - granted_power
[i
];
317 capped_extra_power
+= extra_actor_power
[i
];
324 * Re-divvy the reclaimed extra among actors based on
325 * how far they are from the max
327 extra_power
= min(extra_power
, capped_extra_power
);
328 if (capped_extra_power
> 0)
329 for (i
= 0; i
< num_actors
; i
++)
330 granted_power
[i
] += (extra_actor_power
[i
] *
331 extra_power
) / capped_extra_power
;
334 static int allocate_power(struct thermal_zone_device
*tz
,
338 struct thermal_instance
*instance
;
339 struct power_allocator_params
*params
= tz
->governor_data
;
340 u32
*req_power
, *max_power
, *granted_power
, *extra_actor_power
;
341 u32
*weighted_req_power
;
342 u32 total_req_power
, max_allocatable_power
, total_weighted_req_power
;
343 u32 total_granted_power
, power_range
;
344 int i
, num_actors
, total_weight
, ret
= 0;
345 int trip_max_desired_temperature
= params
->trip_max_desired_temperature
;
347 mutex_lock(&tz
->lock
);
351 list_for_each_entry(instance
, &tz
->thermal_instances
, tz_node
) {
352 if ((instance
->trip
== trip_max_desired_temperature
) &&
353 cdev_is_power_actor(instance
->cdev
)) {
355 total_weight
+= instance
->weight
;
365 * We need to allocate five arrays of the same size:
366 * req_power, max_power, granted_power, extra_actor_power and
367 * weighted_req_power. They are going to be needed until this
368 * function returns. Allocate them all in one go to simplify
369 * the allocation and deallocation logic.
371 BUILD_BUG_ON(sizeof(*req_power
) != sizeof(*max_power
));
372 BUILD_BUG_ON(sizeof(*req_power
) != sizeof(*granted_power
));
373 BUILD_BUG_ON(sizeof(*req_power
) != sizeof(*extra_actor_power
));
374 BUILD_BUG_ON(sizeof(*req_power
) != sizeof(*weighted_req_power
));
375 req_power
= kcalloc(num_actors
* 5, sizeof(*req_power
), GFP_KERNEL
);
381 max_power
= &req_power
[num_actors
];
382 granted_power
= &req_power
[2 * num_actors
];
383 extra_actor_power
= &req_power
[3 * num_actors
];
384 weighted_req_power
= &req_power
[4 * num_actors
];
387 total_weighted_req_power
= 0;
389 max_allocatable_power
= 0;
391 list_for_each_entry(instance
, &tz
->thermal_instances
, tz_node
) {
393 struct thermal_cooling_device
*cdev
= instance
->cdev
;
395 if (instance
->trip
!= trip_max_desired_temperature
)
398 if (!cdev_is_power_actor(cdev
))
401 if (cdev
->ops
->get_requested_power(cdev
, tz
, &req_power
[i
]))
405 weight
= 1 << FRAC_BITS
;
407 weight
= instance
->weight
;
409 weighted_req_power
[i
] = frac_to_int(weight
* req_power
[i
]);
411 if (power_actor_get_max_power(cdev
, tz
, &max_power
[i
]))
414 total_req_power
+= req_power
[i
];
415 max_allocatable_power
+= max_power
[i
];
416 total_weighted_req_power
+= weighted_req_power
[i
];
421 power_range
= pid_controller(tz
, current_temp
, control_temp
,
422 max_allocatable_power
);
424 divvy_up_power(weighted_req_power
, max_power
, num_actors
,
425 total_weighted_req_power
, power_range
, granted_power
,
428 total_granted_power
= 0;
430 list_for_each_entry(instance
, &tz
->thermal_instances
, tz_node
) {
431 if (instance
->trip
!= trip_max_desired_temperature
)
434 if (!cdev_is_power_actor(instance
->cdev
))
437 power_actor_set_power(instance
->cdev
, instance
,
439 total_granted_power
+= granted_power
[i
];
444 trace_thermal_power_allocator(tz
, req_power
, total_req_power
,
445 granted_power
, total_granted_power
,
446 num_actors
, power_range
,
447 max_allocatable_power
, current_temp
,
448 control_temp
- current_temp
);
452 mutex_unlock(&tz
->lock
);
458 * get_governor_trips() - get the number of the two trip points that are key for this governor
459 * @tz: thermal zone to operate on
460 * @params: pointer to private data for this governor
462 * The power allocator governor works optimally with two trips points:
463 * a "switch on" trip point and a "maximum desired temperature". These
464 * are defined as the first and last passive trip points.
466 * If there is only one trip point, then that's considered to be the
467 * "maximum desired temperature" trip point and the governor is always
468 * on. If there are no passive or active trip points, then the
469 * governor won't do anything. In fact, its throttle function
470 * won't be called at all.
472 static void get_governor_trips(struct thermal_zone_device
*tz
,
473 struct power_allocator_params
*params
)
475 int i
, last_active
, last_passive
;
476 bool found_first_passive
;
478 found_first_passive
= false;
479 last_active
= INVALID_TRIP
;
480 last_passive
= INVALID_TRIP
;
482 for (i
= 0; i
< tz
->trips
; i
++) {
483 enum thermal_trip_type type
;
486 ret
= tz
->ops
->get_trip_type(tz
, i
, &type
);
488 dev_warn(&tz
->device
,
489 "Failed to get trip point %d type: %d\n", i
,
494 if (type
== THERMAL_TRIP_PASSIVE
) {
495 if (!found_first_passive
) {
496 params
->trip_switch_on
= i
;
497 found_first_passive
= true;
501 } else if (type
== THERMAL_TRIP_ACTIVE
) {
508 if (last_passive
!= INVALID_TRIP
) {
509 params
->trip_max_desired_temperature
= last_passive
;
510 } else if (found_first_passive
) {
511 params
->trip_max_desired_temperature
= params
->trip_switch_on
;
512 params
->trip_switch_on
= INVALID_TRIP
;
514 params
->trip_switch_on
= INVALID_TRIP
;
515 params
->trip_max_desired_temperature
= last_active
;
519 static void reset_pid_controller(struct power_allocator_params
*params
)
521 params
->err_integral
= 0;
522 params
->prev_err
= 0;
525 static void allow_maximum_power(struct thermal_zone_device
*tz
)
527 struct thermal_instance
*instance
;
528 struct power_allocator_params
*params
= tz
->governor_data
;
530 list_for_each_entry(instance
, &tz
->thermal_instances
, tz_node
) {
531 if ((instance
->trip
!= params
->trip_max_desired_temperature
) ||
532 (!cdev_is_power_actor(instance
->cdev
)))
535 instance
->target
= 0;
536 instance
->cdev
->updated
= false;
537 thermal_cdev_update(instance
->cdev
);
542 * power_allocator_bind() - bind the power_allocator governor to a thermal zone
543 * @tz: thermal zone to bind it to
545 * Initialize the PID controller parameters and bind it to the thermal
548 * Return: 0 on success, or -ENOMEM if we ran out of memory.
550 static int power_allocator_bind(struct thermal_zone_device
*tz
)
553 struct power_allocator_params
*params
;
556 params
= kzalloc(sizeof(*params
), GFP_KERNEL
);
561 tz
->tzp
= kzalloc(sizeof(*tz
->tzp
), GFP_KERNEL
);
567 params
->allocated_tzp
= true;
570 if (!tz
->tzp
->sustainable_power
)
571 dev_warn(&tz
->device
, "power_allocator: sustainable_power will be estimated\n");
573 get_governor_trips(tz
, params
);
576 ret
= tz
->ops
->get_trip_temp(tz
,
577 params
->trip_max_desired_temperature
,
580 estimate_pid_constants(tz
, tz
->tzp
->sustainable_power
,
581 params
->trip_switch_on
,
582 control_temp
, false);
585 reset_pid_controller(params
);
587 tz
->governor_data
= params
;
597 static void power_allocator_unbind(struct thermal_zone_device
*tz
)
599 struct power_allocator_params
*params
= tz
->governor_data
;
601 dev_dbg(&tz
->device
, "Unbinding from thermal zone %d\n", tz
->id
);
603 if (params
->allocated_tzp
) {
608 kfree(tz
->governor_data
);
609 tz
->governor_data
= NULL
;
612 static int power_allocator_throttle(struct thermal_zone_device
*tz
, int trip
)
615 int switch_on_temp
, control_temp
, current_temp
;
616 struct power_allocator_params
*params
= tz
->governor_data
;
619 * We get called for every trip point but we only need to do
620 * our calculations once
622 if (trip
!= params
->trip_max_desired_temperature
)
625 ret
= thermal_zone_get_temp(tz
, ¤t_temp
);
627 dev_warn(&tz
->device
, "Failed to get temperature: %d\n", ret
);
631 ret
= tz
->ops
->get_trip_temp(tz
, params
->trip_switch_on
,
633 if (!ret
&& (current_temp
< switch_on_temp
)) {
635 reset_pid_controller(params
);
636 allow_maximum_power(tz
);
642 ret
= tz
->ops
->get_trip_temp(tz
, params
->trip_max_desired_temperature
,
645 dev_warn(&tz
->device
,
646 "Failed to get the maximum desired temperature: %d\n",
651 return allocate_power(tz
, current_temp
, control_temp
);
654 static struct thermal_governor thermal_gov_power_allocator
= {
655 .name
= "power_allocator",
656 .bind_to_tz
= power_allocator_bind
,
657 .unbind_from_tz
= power_allocator_unbind
,
658 .throttle
= power_allocator_throttle
,
661 int thermal_gov_power_allocator_register(void)
663 return thermal_register_governor(&thermal_gov_power_allocator
);
666 void thermal_gov_power_allocator_unregister(void)
668 thermal_unregister_governor(&thermal_gov_power_allocator
);