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 #include "thermal_core.h"
25 #define int_to_frac(x) ((x) << FRAC_BITS)
26 #define frac_to_int(x) ((x) >> FRAC_BITS)
29 * mul_frac() - multiply two fixed-point numbers
30 * @x: first multiplicand
31 * @y: second multiplicand
33 * Return: the result of multiplying two fixed-point numbers. The
34 * result is also a fixed-point number.
36 static inline s64
mul_frac(s64 x
, s64 y
)
38 return (x
* y
) >> FRAC_BITS
;
42 * div_frac() - divide two fixed-point numbers
46 * Return: the result of dividing two fixed-point numbers. The
47 * result is also a fixed-point number.
49 static inline s64
div_frac(s64 x
, s64 y
)
51 return div_s64(x
<< FRAC_BITS
, y
);
55 * struct power_allocator_params - parameters for the power allocator governor
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 * @trip_max_desired_temperature: last passive trip point of the thermal
62 * zone. The temperature we are
65 struct power_allocator_params
{
69 int trip_max_desired_temperature
;
73 * pid_controller() - PID controller
74 * @tz: thermal zone we are operating in
75 * @current_temp: the current temperature in millicelsius
76 * @control_temp: the target temperature in millicelsius
77 * @max_allocatable_power: maximum allocatable power for this thermal zone
79 * This PID controller increases the available power budget so that the
80 * temperature of the thermal zone gets as close as possible to
81 * @control_temp and limits the power if it exceeds it. k_po is the
82 * proportional term when we are overshooting, k_pu is the
83 * proportional term when we are undershooting. integral_cutoff is a
84 * threshold below which we stop accumulating the error. The
85 * accumulated error is only valid if the requested power will make
86 * the system warmer. If the system is mostly idle, there's no point
87 * in accumulating positive error.
89 * Return: The power budget for the next period.
91 static u32
pid_controller(struct thermal_zone_device
*tz
,
92 unsigned long current_temp
,
93 unsigned long control_temp
,
94 u32 max_allocatable_power
)
96 s64 p
, i
, d
, power_range
;
97 s32 err
, max_power_frac
;
98 struct power_allocator_params
*params
= tz
->governor_data
;
100 max_power_frac
= int_to_frac(max_allocatable_power
);
102 err
= ((s32
)control_temp
- (s32
)current_temp
);
103 err
= int_to_frac(err
);
105 /* Calculate the proportional term */
106 p
= mul_frac(err
< 0 ? tz
->tzp
->k_po
: tz
->tzp
->k_pu
, err
);
109 * Calculate the integral term
111 * if the error is less than cut off allow integration (but
112 * the integral is limited to max power)
114 i
= mul_frac(tz
->tzp
->k_i
, params
->err_integral
);
116 if (err
< int_to_frac(tz
->tzp
->integral_cutoff
)) {
117 s64 i_next
= i
+ mul_frac(tz
->tzp
->k_i
, err
);
119 if (abs64(i_next
) < max_power_frac
) {
121 params
->err_integral
+= err
;
126 * Calculate the derivative term
128 * We do err - prev_err, so with a positive k_d, a decreasing
129 * error (i.e. driving closer to the line) results in less
130 * power being applied, slowing down the controller)
132 d
= mul_frac(tz
->tzp
->k_d
, err
- params
->prev_err
);
133 d
= div_frac(d
, tz
->passive_delay
);
134 params
->prev_err
= err
;
136 power_range
= p
+ i
+ d
;
138 /* feed-forward the known sustainable dissipatable power */
139 power_range
= tz
->tzp
->sustainable_power
+ frac_to_int(power_range
);
141 return clamp(power_range
, (s64
)0, (s64
)max_allocatable_power
);
145 * divvy_up_power() - divvy the allocated power between the actors
146 * @req_power: each actor's requested power
147 * @max_power: each actor's maximum available power
148 * @num_actors: size of the @req_power, @max_power and @granted_power's array
149 * @total_req_power: sum of @req_power
150 * @power_range: total allocated power
151 * @granted_power: output array: each actor's granted power
152 * @extra_actor_power: an appropriately sized array to be used in the
153 * function as temporary storage of the extra power given
156 * This function divides the total allocated power (@power_range)
157 * fairly between the actors. It first tries to give each actor a
158 * share of the @power_range according to how much power it requested
159 * compared to the rest of the actors. For example, if only one actor
160 * requests power, then it receives all the @power_range. If
161 * three actors each requests 1mW, each receives a third of the
164 * If any actor received more than their maximum power, then that
165 * surplus is re-divvied among the actors based on how far they are
166 * from their respective maximums.
168 * Granted power for each actor is written to @granted_power, which
169 * should've been allocated by the calling function.
171 static void divvy_up_power(u32
*req_power
, u32
*max_power
, int num_actors
,
172 u32 total_req_power
, u32 power_range
,
173 u32
*granted_power
, u32
*extra_actor_power
)
175 u32 extra_power
, capped_extra_power
;
179 * Prevent division by 0 if none of the actors request power.
181 if (!total_req_power
)
184 capped_extra_power
= 0;
186 for (i
= 0; i
< num_actors
; i
++) {
187 u64 req_range
= req_power
[i
] * power_range
;
189 granted_power
[i
] = div_u64(req_range
, total_req_power
);
191 if (granted_power
[i
] > max_power
[i
]) {
192 extra_power
+= granted_power
[i
] - max_power
[i
];
193 granted_power
[i
] = max_power
[i
];
196 extra_actor_power
[i
] = max_power
[i
] - granted_power
[i
];
197 capped_extra_power
+= extra_actor_power
[i
];
204 * Re-divvy the reclaimed extra among actors based on
205 * how far they are from the max
207 extra_power
= min(extra_power
, capped_extra_power
);
208 if (capped_extra_power
> 0)
209 for (i
= 0; i
< num_actors
; i
++)
210 granted_power
[i
] += (extra_actor_power
[i
] *
211 extra_power
) / capped_extra_power
;
214 static int allocate_power(struct thermal_zone_device
*tz
,
215 unsigned long current_temp
,
216 unsigned long control_temp
)
218 struct thermal_instance
*instance
;
219 struct power_allocator_params
*params
= tz
->governor_data
;
220 u32
*req_power
, *max_power
, *granted_power
, *extra_actor_power
;
221 u32 total_req_power
, max_allocatable_power
;
223 int i
, num_actors
, total_weight
, ret
= 0;
224 int trip_max_desired_temperature
= params
->trip_max_desired_temperature
;
226 mutex_lock(&tz
->lock
);
230 list_for_each_entry(instance
, &tz
->thermal_instances
, tz_node
) {
231 if ((instance
->trip
== trip_max_desired_temperature
) &&
232 cdev_is_power_actor(instance
->cdev
)) {
234 total_weight
+= instance
->weight
;
239 * We need to allocate three arrays of the same size:
240 * req_power, max_power and granted_power. They are going to
241 * be needed until this function returns. Allocate them all
242 * in one go to simplify the allocation and deallocation
245 BUILD_BUG_ON(sizeof(*req_power
) != sizeof(*max_power
));
246 BUILD_BUG_ON(sizeof(*req_power
) != sizeof(*granted_power
));
247 BUILD_BUG_ON(sizeof(*req_power
) != sizeof(*extra_actor_power
));
248 req_power
= devm_kcalloc(&tz
->device
, num_actors
* 4,
249 sizeof(*req_power
), GFP_KERNEL
);
255 max_power
= &req_power
[num_actors
];
256 granted_power
= &req_power
[2 * num_actors
];
257 extra_actor_power
= &req_power
[3 * num_actors
];
261 max_allocatable_power
= 0;
263 list_for_each_entry(instance
, &tz
->thermal_instances
, tz_node
) {
265 struct thermal_cooling_device
*cdev
= instance
->cdev
;
267 if (instance
->trip
!= trip_max_desired_temperature
)
270 if (!cdev_is_power_actor(cdev
))
273 if (cdev
->ops
->get_requested_power(cdev
, tz
, &req_power
[i
]))
277 weight
= 1 << FRAC_BITS
;
279 weight
= instance
->weight
;
281 req_power
[i
] = frac_to_int(weight
* req_power
[i
]);
283 if (power_actor_get_max_power(cdev
, tz
, &max_power
[i
]))
286 total_req_power
+= req_power
[i
];
287 max_allocatable_power
+= max_power
[i
];
292 power_range
= pid_controller(tz
, current_temp
, control_temp
,
293 max_allocatable_power
);
295 divvy_up_power(req_power
, max_power
, num_actors
, total_req_power
,
296 power_range
, granted_power
, extra_actor_power
);
299 list_for_each_entry(instance
, &tz
->thermal_instances
, tz_node
) {
300 if (instance
->trip
!= trip_max_desired_temperature
)
303 if (!cdev_is_power_actor(instance
->cdev
))
306 power_actor_set_power(instance
->cdev
, instance
,
312 devm_kfree(&tz
->device
, req_power
);
314 mutex_unlock(&tz
->lock
);
319 static int get_governor_trips(struct thermal_zone_device
*tz
,
320 struct power_allocator_params
*params
)
322 int i
, ret
, last_passive
;
323 bool found_first_passive
;
325 found_first_passive
= false;
329 for (i
= 0; i
< tz
->trips
; i
++) {
330 enum thermal_trip_type type
;
332 ret
= tz
->ops
->get_trip_type(tz
, i
, &type
);
336 if (!found_first_passive
) {
337 if (type
== THERMAL_TRIP_PASSIVE
) {
338 params
->trip_switch_on
= i
;
339 found_first_passive
= true;
341 } else if (type
== THERMAL_TRIP_PASSIVE
) {
348 if (last_passive
!= -1) {
349 params
->trip_max_desired_temperature
= last_passive
;
358 static void reset_pid_controller(struct power_allocator_params
*params
)
360 params
->err_integral
= 0;
361 params
->prev_err
= 0;
364 static void allow_maximum_power(struct thermal_zone_device
*tz
)
366 struct thermal_instance
*instance
;
367 struct power_allocator_params
*params
= tz
->governor_data
;
369 list_for_each_entry(instance
, &tz
->thermal_instances
, tz_node
) {
370 if ((instance
->trip
!= params
->trip_max_desired_temperature
) ||
371 (!cdev_is_power_actor(instance
->cdev
)))
374 instance
->target
= 0;
375 instance
->cdev
->updated
= false;
376 thermal_cdev_update(instance
->cdev
);
381 * power_allocator_bind() - bind the power_allocator governor to a thermal zone
382 * @tz: thermal zone to bind it to
384 * Check that the thermal zone is valid for this governor, that is, it
385 * has two thermal trips. If so, initialize the PID controller
386 * parameters and bind it to the thermal zone.
388 * Return: 0 on success, -EINVAL if the trips were invalid or -ENOMEM
389 * if we ran out of memory.
391 static int power_allocator_bind(struct thermal_zone_device
*tz
)
394 struct power_allocator_params
*params
;
395 unsigned long switch_on_temp
, control_temp
;
396 u32 temperature_threshold
;
398 if (!tz
->tzp
|| !tz
->tzp
->sustainable_power
) {
400 "power_allocator: missing sustainable_power\n");
404 params
= devm_kzalloc(&tz
->device
, sizeof(*params
), GFP_KERNEL
);
408 ret
= get_governor_trips(tz
, params
);
411 "thermal zone %s has wrong trip setup for power allocator\n",
416 ret
= tz
->ops
->get_trip_temp(tz
, params
->trip_switch_on
,
421 ret
= tz
->ops
->get_trip_temp(tz
, params
->trip_max_desired_temperature
,
426 temperature_threshold
= control_temp
- switch_on_temp
;
428 tz
->tzp
->k_po
= tz
->tzp
->k_po
?:
429 int_to_frac(tz
->tzp
->sustainable_power
) / temperature_threshold
;
430 tz
->tzp
->k_pu
= tz
->tzp
->k_pu
?:
431 int_to_frac(2 * tz
->tzp
->sustainable_power
) /
432 temperature_threshold
;
433 tz
->tzp
->k_i
= tz
->tzp
->k_i
?: int_to_frac(10) / 1000;
435 * The default for k_d and integral_cutoff is 0, so we can
436 * leave them as they are.
439 reset_pid_controller(params
);
441 tz
->governor_data
= params
;
446 devm_kfree(&tz
->device
, params
);
450 static void power_allocator_unbind(struct thermal_zone_device
*tz
)
452 dev_dbg(&tz
->device
, "Unbinding from thermal zone %d\n", tz
->id
);
453 devm_kfree(&tz
->device
, tz
->governor_data
);
454 tz
->governor_data
= NULL
;
457 static int power_allocator_throttle(struct thermal_zone_device
*tz
, int trip
)
460 unsigned long switch_on_temp
, control_temp
, current_temp
;
461 struct power_allocator_params
*params
= tz
->governor_data
;
464 * We get called for every trip point but we only need to do
465 * our calculations once
467 if (trip
!= params
->trip_max_desired_temperature
)
470 ret
= thermal_zone_get_temp(tz
, ¤t_temp
);
472 dev_warn(&tz
->device
, "Failed to get temperature: %d\n", ret
);
476 ret
= tz
->ops
->get_trip_temp(tz
, params
->trip_switch_on
,
479 dev_warn(&tz
->device
,
480 "Failed to get switch on temperature: %d\n", ret
);
484 if (current_temp
< switch_on_temp
) {
486 reset_pid_controller(params
);
487 allow_maximum_power(tz
);
493 ret
= tz
->ops
->get_trip_temp(tz
, params
->trip_max_desired_temperature
,
496 dev_warn(&tz
->device
,
497 "Failed to get the maximum desired temperature: %d\n",
502 return allocate_power(tz
, current_temp
, control_temp
);
505 static struct thermal_governor thermal_gov_power_allocator
= {
506 .name
= "power_allocator",
507 .bind_to_tz
= power_allocator_bind
,
508 .unbind_from_tz
= power_allocator_unbind
,
509 .throttle
= power_allocator_throttle
,
512 int thermal_gov_power_allocator_register(void)
514 return thermal_register_governor(&thermal_gov_power_allocator
);
517 void thermal_gov_power_allocator_unregister(void)
519 thermal_unregister_governor(&thermal_gov_power_allocator
);