Javi Merino | 6b775e8 | 2015-03-02 17:17:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * A power allocator to manage temperature |
| 3 | * |
| 4 | * Copyright (C) 2014 ARM Ltd. |
| 5 | * |
| 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. |
| 9 | * |
| 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. |
| 14 | */ |
| 15 | |
| 16 | #define pr_fmt(fmt) "Power allocator: " fmt |
| 17 | |
| 18 | #include <linux/rculist.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/thermal.h> |
| 21 | |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame^] | 22 | #define CREATE_TRACE_POINTS |
| 23 | #include <trace/events/thermal_power_allocator.h> |
| 24 | |
Javi Merino | 6b775e8 | 2015-03-02 17:17:19 +0000 | [diff] [blame] | 25 | #include "thermal_core.h" |
| 26 | |
| 27 | #define FRAC_BITS 10 |
| 28 | #define int_to_frac(x) ((x) << FRAC_BITS) |
| 29 | #define frac_to_int(x) ((x) >> FRAC_BITS) |
| 30 | |
| 31 | /** |
| 32 | * mul_frac() - multiply two fixed-point numbers |
| 33 | * @x: first multiplicand |
| 34 | * @y: second multiplicand |
| 35 | * |
| 36 | * Return: the result of multiplying two fixed-point numbers. The |
| 37 | * result is also a fixed-point number. |
| 38 | */ |
| 39 | static inline s64 mul_frac(s64 x, s64 y) |
| 40 | { |
| 41 | return (x * y) >> FRAC_BITS; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * div_frac() - divide two fixed-point numbers |
| 46 | * @x: the dividend |
| 47 | * @y: the divisor |
| 48 | * |
| 49 | * Return: the result of dividing two fixed-point numbers. The |
| 50 | * result is also a fixed-point number. |
| 51 | */ |
| 52 | static inline s64 div_frac(s64 x, s64 y) |
| 53 | { |
| 54 | return div_s64(x << FRAC_BITS, y); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * struct power_allocator_params - parameters for the power allocator governor |
| 59 | * @err_integral: accumulated error in the PID controller. |
| 60 | * @prev_err: error in the previous iteration of the PID controller. |
| 61 | * Used to calculate the derivative term. |
| 62 | * @trip_switch_on: first passive trip point of the thermal zone. The |
| 63 | * governor switches on when this trip point is crossed. |
| 64 | * @trip_max_desired_temperature: last passive trip point of the thermal |
| 65 | * zone. The temperature we are |
| 66 | * controlling for. |
| 67 | */ |
| 68 | struct power_allocator_params { |
| 69 | s64 err_integral; |
| 70 | s32 prev_err; |
| 71 | int trip_switch_on; |
| 72 | int trip_max_desired_temperature; |
| 73 | }; |
| 74 | |
| 75 | /** |
| 76 | * pid_controller() - PID controller |
| 77 | * @tz: thermal zone we are operating in |
| 78 | * @current_temp: the current temperature in millicelsius |
| 79 | * @control_temp: the target temperature in millicelsius |
| 80 | * @max_allocatable_power: maximum allocatable power for this thermal zone |
| 81 | * |
| 82 | * This PID controller increases the available power budget so that the |
| 83 | * temperature of the thermal zone gets as close as possible to |
| 84 | * @control_temp and limits the power if it exceeds it. k_po is the |
| 85 | * proportional term when we are overshooting, k_pu is the |
| 86 | * proportional term when we are undershooting. integral_cutoff is a |
| 87 | * threshold below which we stop accumulating the error. The |
| 88 | * accumulated error is only valid if the requested power will make |
| 89 | * the system warmer. If the system is mostly idle, there's no point |
| 90 | * in accumulating positive error. |
| 91 | * |
| 92 | * Return: The power budget for the next period. |
| 93 | */ |
| 94 | static u32 pid_controller(struct thermal_zone_device *tz, |
| 95 | unsigned long current_temp, |
| 96 | unsigned long control_temp, |
| 97 | u32 max_allocatable_power) |
| 98 | { |
| 99 | s64 p, i, d, power_range; |
| 100 | s32 err, max_power_frac; |
| 101 | struct power_allocator_params *params = tz->governor_data; |
| 102 | |
| 103 | max_power_frac = int_to_frac(max_allocatable_power); |
| 104 | |
| 105 | err = ((s32)control_temp - (s32)current_temp); |
| 106 | err = int_to_frac(err); |
| 107 | |
| 108 | /* Calculate the proportional term */ |
| 109 | p = mul_frac(err < 0 ? tz->tzp->k_po : tz->tzp->k_pu, err); |
| 110 | |
| 111 | /* |
| 112 | * Calculate the integral term |
| 113 | * |
| 114 | * if the error is less than cut off allow integration (but |
| 115 | * the integral is limited to max power) |
| 116 | */ |
| 117 | i = mul_frac(tz->tzp->k_i, params->err_integral); |
| 118 | |
| 119 | if (err < int_to_frac(tz->tzp->integral_cutoff)) { |
| 120 | s64 i_next = i + mul_frac(tz->tzp->k_i, err); |
| 121 | |
| 122 | if (abs64(i_next) < max_power_frac) { |
| 123 | i = i_next; |
| 124 | params->err_integral += err; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * Calculate the derivative term |
| 130 | * |
| 131 | * We do err - prev_err, so with a positive k_d, a decreasing |
| 132 | * error (i.e. driving closer to the line) results in less |
| 133 | * power being applied, slowing down the controller) |
| 134 | */ |
| 135 | d = mul_frac(tz->tzp->k_d, err - params->prev_err); |
| 136 | d = div_frac(d, tz->passive_delay); |
| 137 | params->prev_err = err; |
| 138 | |
| 139 | power_range = p + i + d; |
| 140 | |
| 141 | /* feed-forward the known sustainable dissipatable power */ |
| 142 | power_range = tz->tzp->sustainable_power + frac_to_int(power_range); |
| 143 | |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame^] | 144 | power_range = clamp(power_range, (s64)0, (s64)max_allocatable_power); |
| 145 | |
| 146 | trace_thermal_power_allocator_pid(tz, frac_to_int(err), |
| 147 | frac_to_int(params->err_integral), |
| 148 | frac_to_int(p), frac_to_int(i), |
| 149 | frac_to_int(d), power_range); |
| 150 | |
| 151 | return power_range; |
Javi Merino | 6b775e8 | 2015-03-02 17:17:19 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /** |
| 155 | * divvy_up_power() - divvy the allocated power between the actors |
| 156 | * @req_power: each actor's requested power |
| 157 | * @max_power: each actor's maximum available power |
| 158 | * @num_actors: size of the @req_power, @max_power and @granted_power's array |
| 159 | * @total_req_power: sum of @req_power |
| 160 | * @power_range: total allocated power |
| 161 | * @granted_power: output array: each actor's granted power |
| 162 | * @extra_actor_power: an appropriately sized array to be used in the |
| 163 | * function as temporary storage of the extra power given |
| 164 | * to the actors |
| 165 | * |
| 166 | * This function divides the total allocated power (@power_range) |
| 167 | * fairly between the actors. It first tries to give each actor a |
| 168 | * share of the @power_range according to how much power it requested |
| 169 | * compared to the rest of the actors. For example, if only one actor |
| 170 | * requests power, then it receives all the @power_range. If |
| 171 | * three actors each requests 1mW, each receives a third of the |
| 172 | * @power_range. |
| 173 | * |
| 174 | * If any actor received more than their maximum power, then that |
| 175 | * surplus is re-divvied among the actors based on how far they are |
| 176 | * from their respective maximums. |
| 177 | * |
| 178 | * Granted power for each actor is written to @granted_power, which |
| 179 | * should've been allocated by the calling function. |
| 180 | */ |
| 181 | static void divvy_up_power(u32 *req_power, u32 *max_power, int num_actors, |
| 182 | u32 total_req_power, u32 power_range, |
| 183 | u32 *granted_power, u32 *extra_actor_power) |
| 184 | { |
| 185 | u32 extra_power, capped_extra_power; |
| 186 | int i; |
| 187 | |
| 188 | /* |
| 189 | * Prevent division by 0 if none of the actors request power. |
| 190 | */ |
| 191 | if (!total_req_power) |
| 192 | total_req_power = 1; |
| 193 | |
| 194 | capped_extra_power = 0; |
| 195 | extra_power = 0; |
| 196 | for (i = 0; i < num_actors; i++) { |
| 197 | u64 req_range = req_power[i] * power_range; |
| 198 | |
| 199 | granted_power[i] = div_u64(req_range, total_req_power); |
| 200 | |
| 201 | if (granted_power[i] > max_power[i]) { |
| 202 | extra_power += granted_power[i] - max_power[i]; |
| 203 | granted_power[i] = max_power[i]; |
| 204 | } |
| 205 | |
| 206 | extra_actor_power[i] = max_power[i] - granted_power[i]; |
| 207 | capped_extra_power += extra_actor_power[i]; |
| 208 | } |
| 209 | |
| 210 | if (!extra_power) |
| 211 | return; |
| 212 | |
| 213 | /* |
| 214 | * Re-divvy the reclaimed extra among actors based on |
| 215 | * how far they are from the max |
| 216 | */ |
| 217 | extra_power = min(extra_power, capped_extra_power); |
| 218 | if (capped_extra_power > 0) |
| 219 | for (i = 0; i < num_actors; i++) |
| 220 | granted_power[i] += (extra_actor_power[i] * |
| 221 | extra_power) / capped_extra_power; |
| 222 | } |
| 223 | |
| 224 | static int allocate_power(struct thermal_zone_device *tz, |
| 225 | unsigned long current_temp, |
| 226 | unsigned long control_temp) |
| 227 | { |
| 228 | struct thermal_instance *instance; |
| 229 | struct power_allocator_params *params = tz->governor_data; |
| 230 | u32 *req_power, *max_power, *granted_power, *extra_actor_power; |
| 231 | u32 total_req_power, max_allocatable_power; |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame^] | 232 | u32 total_granted_power, power_range; |
Javi Merino | 6b775e8 | 2015-03-02 17:17:19 +0000 | [diff] [blame] | 233 | int i, num_actors, total_weight, ret = 0; |
| 234 | int trip_max_desired_temperature = params->trip_max_desired_temperature; |
| 235 | |
| 236 | mutex_lock(&tz->lock); |
| 237 | |
| 238 | num_actors = 0; |
| 239 | total_weight = 0; |
| 240 | list_for_each_entry(instance, &tz->thermal_instances, tz_node) { |
| 241 | if ((instance->trip == trip_max_desired_temperature) && |
| 242 | cdev_is_power_actor(instance->cdev)) { |
| 243 | num_actors++; |
| 244 | total_weight += instance->weight; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | * We need to allocate three arrays of the same size: |
| 250 | * req_power, max_power and granted_power. They are going to |
| 251 | * be needed until this function returns. Allocate them all |
| 252 | * in one go to simplify the allocation and deallocation |
| 253 | * logic. |
| 254 | */ |
| 255 | BUILD_BUG_ON(sizeof(*req_power) != sizeof(*max_power)); |
| 256 | BUILD_BUG_ON(sizeof(*req_power) != sizeof(*granted_power)); |
| 257 | BUILD_BUG_ON(sizeof(*req_power) != sizeof(*extra_actor_power)); |
| 258 | req_power = devm_kcalloc(&tz->device, num_actors * 4, |
| 259 | sizeof(*req_power), GFP_KERNEL); |
| 260 | if (!req_power) { |
| 261 | ret = -ENOMEM; |
| 262 | goto unlock; |
| 263 | } |
| 264 | |
| 265 | max_power = &req_power[num_actors]; |
| 266 | granted_power = &req_power[2 * num_actors]; |
| 267 | extra_actor_power = &req_power[3 * num_actors]; |
| 268 | |
| 269 | i = 0; |
| 270 | total_req_power = 0; |
| 271 | max_allocatable_power = 0; |
| 272 | |
| 273 | list_for_each_entry(instance, &tz->thermal_instances, tz_node) { |
| 274 | int weight; |
| 275 | struct thermal_cooling_device *cdev = instance->cdev; |
| 276 | |
| 277 | if (instance->trip != trip_max_desired_temperature) |
| 278 | continue; |
| 279 | |
| 280 | if (!cdev_is_power_actor(cdev)) |
| 281 | continue; |
| 282 | |
| 283 | if (cdev->ops->get_requested_power(cdev, tz, &req_power[i])) |
| 284 | continue; |
| 285 | |
| 286 | if (!total_weight) |
| 287 | weight = 1 << FRAC_BITS; |
| 288 | else |
| 289 | weight = instance->weight; |
| 290 | |
| 291 | req_power[i] = frac_to_int(weight * req_power[i]); |
| 292 | |
| 293 | if (power_actor_get_max_power(cdev, tz, &max_power[i])) |
| 294 | continue; |
| 295 | |
| 296 | total_req_power += req_power[i]; |
| 297 | max_allocatable_power += max_power[i]; |
| 298 | |
| 299 | i++; |
| 300 | } |
| 301 | |
| 302 | power_range = pid_controller(tz, current_temp, control_temp, |
| 303 | max_allocatable_power); |
| 304 | |
| 305 | divvy_up_power(req_power, max_power, num_actors, total_req_power, |
| 306 | power_range, granted_power, extra_actor_power); |
| 307 | |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame^] | 308 | total_granted_power = 0; |
Javi Merino | 6b775e8 | 2015-03-02 17:17:19 +0000 | [diff] [blame] | 309 | i = 0; |
| 310 | list_for_each_entry(instance, &tz->thermal_instances, tz_node) { |
| 311 | if (instance->trip != trip_max_desired_temperature) |
| 312 | continue; |
| 313 | |
| 314 | if (!cdev_is_power_actor(instance->cdev)) |
| 315 | continue; |
| 316 | |
| 317 | power_actor_set_power(instance->cdev, instance, |
| 318 | granted_power[i]); |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame^] | 319 | total_granted_power += granted_power[i]; |
Javi Merino | 6b775e8 | 2015-03-02 17:17:19 +0000 | [diff] [blame] | 320 | |
| 321 | i++; |
| 322 | } |
| 323 | |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame^] | 324 | trace_thermal_power_allocator(tz, req_power, total_req_power, |
| 325 | granted_power, total_granted_power, |
| 326 | num_actors, power_range, |
| 327 | max_allocatable_power, current_temp, |
| 328 | (s32)control_temp - (s32)current_temp); |
| 329 | |
Javi Merino | 6b775e8 | 2015-03-02 17:17:19 +0000 | [diff] [blame] | 330 | devm_kfree(&tz->device, req_power); |
| 331 | unlock: |
| 332 | mutex_unlock(&tz->lock); |
| 333 | |
| 334 | return ret; |
| 335 | } |
| 336 | |
| 337 | static int get_governor_trips(struct thermal_zone_device *tz, |
| 338 | struct power_allocator_params *params) |
| 339 | { |
| 340 | int i, ret, last_passive; |
| 341 | bool found_first_passive; |
| 342 | |
| 343 | found_first_passive = false; |
| 344 | last_passive = -1; |
| 345 | ret = -EINVAL; |
| 346 | |
| 347 | for (i = 0; i < tz->trips; i++) { |
| 348 | enum thermal_trip_type type; |
| 349 | |
| 350 | ret = tz->ops->get_trip_type(tz, i, &type); |
| 351 | if (ret) |
| 352 | return ret; |
| 353 | |
| 354 | if (!found_first_passive) { |
| 355 | if (type == THERMAL_TRIP_PASSIVE) { |
| 356 | params->trip_switch_on = i; |
| 357 | found_first_passive = true; |
| 358 | } |
| 359 | } else if (type == THERMAL_TRIP_PASSIVE) { |
| 360 | last_passive = i; |
| 361 | } else { |
| 362 | break; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | if (last_passive != -1) { |
| 367 | params->trip_max_desired_temperature = last_passive; |
| 368 | ret = 0; |
| 369 | } else { |
| 370 | ret = -EINVAL; |
| 371 | } |
| 372 | |
| 373 | return ret; |
| 374 | } |
| 375 | |
| 376 | static void reset_pid_controller(struct power_allocator_params *params) |
| 377 | { |
| 378 | params->err_integral = 0; |
| 379 | params->prev_err = 0; |
| 380 | } |
| 381 | |
| 382 | static void allow_maximum_power(struct thermal_zone_device *tz) |
| 383 | { |
| 384 | struct thermal_instance *instance; |
| 385 | struct power_allocator_params *params = tz->governor_data; |
| 386 | |
| 387 | list_for_each_entry(instance, &tz->thermal_instances, tz_node) { |
| 388 | if ((instance->trip != params->trip_max_desired_temperature) || |
| 389 | (!cdev_is_power_actor(instance->cdev))) |
| 390 | continue; |
| 391 | |
| 392 | instance->target = 0; |
| 393 | instance->cdev->updated = false; |
| 394 | thermal_cdev_update(instance->cdev); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * power_allocator_bind() - bind the power_allocator governor to a thermal zone |
| 400 | * @tz: thermal zone to bind it to |
| 401 | * |
| 402 | * Check that the thermal zone is valid for this governor, that is, it |
| 403 | * has two thermal trips. If so, initialize the PID controller |
| 404 | * parameters and bind it to the thermal zone. |
| 405 | * |
| 406 | * Return: 0 on success, -EINVAL if the trips were invalid or -ENOMEM |
| 407 | * if we ran out of memory. |
| 408 | */ |
| 409 | static int power_allocator_bind(struct thermal_zone_device *tz) |
| 410 | { |
| 411 | int ret; |
| 412 | struct power_allocator_params *params; |
| 413 | unsigned long switch_on_temp, control_temp; |
| 414 | u32 temperature_threshold; |
| 415 | |
| 416 | if (!tz->tzp || !tz->tzp->sustainable_power) { |
| 417 | dev_err(&tz->device, |
| 418 | "power_allocator: missing sustainable_power\n"); |
| 419 | return -EINVAL; |
| 420 | } |
| 421 | |
| 422 | params = devm_kzalloc(&tz->device, sizeof(*params), GFP_KERNEL); |
| 423 | if (!params) |
| 424 | return -ENOMEM; |
| 425 | |
| 426 | ret = get_governor_trips(tz, params); |
| 427 | if (ret) { |
| 428 | dev_err(&tz->device, |
| 429 | "thermal zone %s has wrong trip setup for power allocator\n", |
| 430 | tz->type); |
| 431 | goto free; |
| 432 | } |
| 433 | |
| 434 | ret = tz->ops->get_trip_temp(tz, params->trip_switch_on, |
| 435 | &switch_on_temp); |
| 436 | if (ret) |
| 437 | goto free; |
| 438 | |
| 439 | ret = tz->ops->get_trip_temp(tz, params->trip_max_desired_temperature, |
| 440 | &control_temp); |
| 441 | if (ret) |
| 442 | goto free; |
| 443 | |
| 444 | temperature_threshold = control_temp - switch_on_temp; |
| 445 | |
| 446 | tz->tzp->k_po = tz->tzp->k_po ?: |
| 447 | int_to_frac(tz->tzp->sustainable_power) / temperature_threshold; |
| 448 | tz->tzp->k_pu = tz->tzp->k_pu ?: |
| 449 | int_to_frac(2 * tz->tzp->sustainable_power) / |
| 450 | temperature_threshold; |
| 451 | tz->tzp->k_i = tz->tzp->k_i ?: int_to_frac(10) / 1000; |
| 452 | /* |
| 453 | * The default for k_d and integral_cutoff is 0, so we can |
| 454 | * leave them as they are. |
| 455 | */ |
| 456 | |
| 457 | reset_pid_controller(params); |
| 458 | |
| 459 | tz->governor_data = params; |
| 460 | |
| 461 | return 0; |
| 462 | |
| 463 | free: |
| 464 | devm_kfree(&tz->device, params); |
| 465 | return ret; |
| 466 | } |
| 467 | |
| 468 | static void power_allocator_unbind(struct thermal_zone_device *tz) |
| 469 | { |
| 470 | dev_dbg(&tz->device, "Unbinding from thermal zone %d\n", tz->id); |
| 471 | devm_kfree(&tz->device, tz->governor_data); |
| 472 | tz->governor_data = NULL; |
| 473 | } |
| 474 | |
| 475 | static int power_allocator_throttle(struct thermal_zone_device *tz, int trip) |
| 476 | { |
| 477 | int ret; |
| 478 | unsigned long switch_on_temp, control_temp, current_temp; |
| 479 | struct power_allocator_params *params = tz->governor_data; |
| 480 | |
| 481 | /* |
| 482 | * We get called for every trip point but we only need to do |
| 483 | * our calculations once |
| 484 | */ |
| 485 | if (trip != params->trip_max_desired_temperature) |
| 486 | return 0; |
| 487 | |
| 488 | ret = thermal_zone_get_temp(tz, ¤t_temp); |
| 489 | if (ret) { |
| 490 | dev_warn(&tz->device, "Failed to get temperature: %d\n", ret); |
| 491 | return ret; |
| 492 | } |
| 493 | |
| 494 | ret = tz->ops->get_trip_temp(tz, params->trip_switch_on, |
| 495 | &switch_on_temp); |
| 496 | if (ret) { |
| 497 | dev_warn(&tz->device, |
| 498 | "Failed to get switch on temperature: %d\n", ret); |
| 499 | return ret; |
| 500 | } |
| 501 | |
| 502 | if (current_temp < switch_on_temp) { |
| 503 | tz->passive = 0; |
| 504 | reset_pid_controller(params); |
| 505 | allow_maximum_power(tz); |
| 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | tz->passive = 1; |
| 510 | |
| 511 | ret = tz->ops->get_trip_temp(tz, params->trip_max_desired_temperature, |
| 512 | &control_temp); |
| 513 | if (ret) { |
| 514 | dev_warn(&tz->device, |
| 515 | "Failed to get the maximum desired temperature: %d\n", |
| 516 | ret); |
| 517 | return ret; |
| 518 | } |
| 519 | |
| 520 | return allocate_power(tz, current_temp, control_temp); |
| 521 | } |
| 522 | |
| 523 | static struct thermal_governor thermal_gov_power_allocator = { |
| 524 | .name = "power_allocator", |
| 525 | .bind_to_tz = power_allocator_bind, |
| 526 | .unbind_from_tz = power_allocator_unbind, |
| 527 | .throttle = power_allocator_throttle, |
| 528 | }; |
| 529 | |
| 530 | int thermal_gov_power_allocator_register(void) |
| 531 | { |
| 532 | return thermal_register_governor(&thermal_gov_power_allocator); |
| 533 | } |
| 534 | |
| 535 | void thermal_gov_power_allocator_unregister(void) |
| 536 | { |
| 537 | thermal_unregister_governor(&thermal_gov_power_allocator); |
| 538 | } |