Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
at v5.5-rc4 669 lines 20 kB view raw
1// SPDX-License-Identifier: GPL-2.0 2/* 3 * linux/drivers/thermal/cpu_cooling.c 4 * 5 * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com) 6 * 7 * Copyright (C) 2012-2018 Linaro Limited. 8 * 9 * Authors: Amit Daniel <amit.kachhap@linaro.org> 10 * Viresh Kumar <viresh.kumar@linaro.org> 11 * 12 */ 13#include <linux/module.h> 14#include <linux/thermal.h> 15#include <linux/cpufreq.h> 16#include <linux/err.h> 17#include <linux/idr.h> 18#include <linux/pm_opp.h> 19#include <linux/pm_qos.h> 20#include <linux/slab.h> 21#include <linux/cpu.h> 22#include <linux/cpu_cooling.h> 23#include <linux/energy_model.h> 24 25#include <trace/events/thermal.h> 26 27/* 28 * Cooling state <-> CPUFreq frequency 29 * 30 * Cooling states are translated to frequencies throughout this driver and this 31 * is the relation between them. 32 * 33 * Highest cooling state corresponds to lowest possible frequency. 34 * 35 * i.e. 36 * level 0 --> 1st Max Freq 37 * level 1 --> 2nd Max Freq 38 * ... 39 */ 40 41/** 42 * struct time_in_idle - Idle time stats 43 * @time: previous reading of the absolute time that this cpu was idle 44 * @timestamp: wall time of the last invocation of get_cpu_idle_time_us() 45 */ 46struct time_in_idle { 47 u64 time; 48 u64 timestamp; 49}; 50 51/** 52 * struct cpufreq_cooling_device - data for cooling device with cpufreq 53 * @id: unique integer value corresponding to each cpufreq_cooling_device 54 * registered. 55 * @last_load: load measured by the latest call to cpufreq_get_requested_power() 56 * @cpufreq_state: integer value representing the current state of cpufreq 57 * cooling devices. 58 * @max_level: maximum cooling level. One less than total number of valid 59 * cpufreq frequencies. 60 * @em: Reference on the Energy Model of the device 61 * @cdev: thermal_cooling_device pointer to keep track of the 62 * registered cooling device. 63 * @policy: cpufreq policy. 64 * @node: list_head to link all cpufreq_cooling_device together. 65 * @idle_time: idle time stats 66 * 67 * This structure is required for keeping information of each registered 68 * cpufreq_cooling_device. 69 */ 70struct cpufreq_cooling_device { 71 int id; 72 u32 last_load; 73 unsigned int cpufreq_state; 74 unsigned int max_level; 75 struct em_perf_domain *em; 76 struct cpufreq_policy *policy; 77 struct list_head node; 78 struct time_in_idle *idle_time; 79 struct freq_qos_request qos_req; 80}; 81 82static DEFINE_IDA(cpufreq_ida); 83static DEFINE_MUTEX(cooling_list_lock); 84static LIST_HEAD(cpufreq_cdev_list); 85 86#ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR 87/** 88 * get_level: Find the level for a particular frequency 89 * @cpufreq_cdev: cpufreq_cdev for which the property is required 90 * @freq: Frequency 91 * 92 * Return: level corresponding to the frequency. 93 */ 94static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_cdev, 95 unsigned int freq) 96{ 97 int i; 98 99 for (i = cpufreq_cdev->max_level - 1; i >= 0; i--) { 100 if (freq > cpufreq_cdev->em->table[i].frequency) 101 break; 102 } 103 104 return cpufreq_cdev->max_level - i - 1; 105} 106 107static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_cdev, 108 u32 freq) 109{ 110 int i; 111 112 for (i = cpufreq_cdev->max_level - 1; i >= 0; i--) { 113 if (freq > cpufreq_cdev->em->table[i].frequency) 114 break; 115 } 116 117 return cpufreq_cdev->em->table[i + 1].power; 118} 119 120static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_cdev, 121 u32 power) 122{ 123 int i; 124 125 for (i = cpufreq_cdev->max_level - 1; i >= 0; i--) { 126 if (power > cpufreq_cdev->em->table[i].power) 127 break; 128 } 129 130 return cpufreq_cdev->em->table[i + 1].frequency; 131} 132 133/** 134 * get_load() - get load for a cpu since last updated 135 * @cpufreq_cdev: &struct cpufreq_cooling_device for this cpu 136 * @cpu: cpu number 137 * @cpu_idx: index of the cpu in time_in_idle* 138 * 139 * Return: The average load of cpu @cpu in percentage since this 140 * function was last called. 141 */ 142static u32 get_load(struct cpufreq_cooling_device *cpufreq_cdev, int cpu, 143 int cpu_idx) 144{ 145 u32 load; 146 u64 now, now_idle, delta_time, delta_idle; 147 struct time_in_idle *idle_time = &cpufreq_cdev->idle_time[cpu_idx]; 148 149 now_idle = get_cpu_idle_time(cpu, &now, 0); 150 delta_idle = now_idle - idle_time->time; 151 delta_time = now - idle_time->timestamp; 152 153 if (delta_time <= delta_idle) 154 load = 0; 155 else 156 load = div64_u64(100 * (delta_time - delta_idle), delta_time); 157 158 idle_time->time = now_idle; 159 idle_time->timestamp = now; 160 161 return load; 162} 163 164/** 165 * get_dynamic_power() - calculate the dynamic power 166 * @cpufreq_cdev: &cpufreq_cooling_device for this cdev 167 * @freq: current frequency 168 * 169 * Return: the dynamic power consumed by the cpus described by 170 * @cpufreq_cdev. 171 */ 172static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_cdev, 173 unsigned long freq) 174{ 175 u32 raw_cpu_power; 176 177 raw_cpu_power = cpu_freq_to_power(cpufreq_cdev, freq); 178 return (raw_cpu_power * cpufreq_cdev->last_load) / 100; 179} 180 181/** 182 * cpufreq_get_requested_power() - get the current power 183 * @cdev: &thermal_cooling_device pointer 184 * @tz: a valid thermal zone device pointer 185 * @power: pointer in which to store the resulting power 186 * 187 * Calculate the current power consumption of the cpus in milliwatts 188 * and store it in @power. This function should actually calculate 189 * the requested power, but it's hard to get the frequency that 190 * cpufreq would have assigned if there were no thermal limits. 191 * Instead, we calculate the current power on the assumption that the 192 * immediate future will look like the immediate past. 193 * 194 * We use the current frequency and the average load since this 195 * function was last called. In reality, there could have been 196 * multiple opps since this function was last called and that affects 197 * the load calculation. While it's not perfectly accurate, this 198 * simplification is good enough and works. REVISIT this, as more 199 * complex code may be needed if experiments show that it's not 200 * accurate enough. 201 * 202 * Return: 0 on success, -E* if getting the static power failed. 203 */ 204static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev, 205 struct thermal_zone_device *tz, 206 u32 *power) 207{ 208 unsigned long freq; 209 int i = 0, cpu; 210 u32 total_load = 0; 211 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; 212 struct cpufreq_policy *policy = cpufreq_cdev->policy; 213 u32 *load_cpu = NULL; 214 215 freq = cpufreq_quick_get(policy->cpu); 216 217 if (trace_thermal_power_cpu_get_power_enabled()) { 218 u32 ncpus = cpumask_weight(policy->related_cpus); 219 220 load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL); 221 } 222 223 for_each_cpu(cpu, policy->related_cpus) { 224 u32 load; 225 226 if (cpu_online(cpu)) 227 load = get_load(cpufreq_cdev, cpu, i); 228 else 229 load = 0; 230 231 total_load += load; 232 if (load_cpu) 233 load_cpu[i] = load; 234 235 i++; 236 } 237 238 cpufreq_cdev->last_load = total_load; 239 240 *power = get_dynamic_power(cpufreq_cdev, freq); 241 242 if (load_cpu) { 243 trace_thermal_power_cpu_get_power(policy->related_cpus, freq, 244 load_cpu, i, *power); 245 246 kfree(load_cpu); 247 } 248 249 return 0; 250} 251 252/** 253 * cpufreq_state2power() - convert a cpu cdev state to power consumed 254 * @cdev: &thermal_cooling_device pointer 255 * @tz: a valid thermal zone device pointer 256 * @state: cooling device state to be converted 257 * @power: pointer in which to store the resulting power 258 * 259 * Convert cooling device state @state into power consumption in 260 * milliwatts assuming 100% load. Store the calculated power in 261 * @power. 262 * 263 * Return: 0 on success, -EINVAL if the cooling device state could not 264 * be converted into a frequency or other -E* if there was an error 265 * when calculating the static power. 266 */ 267static int cpufreq_state2power(struct thermal_cooling_device *cdev, 268 struct thermal_zone_device *tz, 269 unsigned long state, u32 *power) 270{ 271 unsigned int freq, num_cpus, idx; 272 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; 273 274 /* Request state should be less than max_level */ 275 if (WARN_ON(state > cpufreq_cdev->max_level)) 276 return -EINVAL; 277 278 num_cpus = cpumask_weight(cpufreq_cdev->policy->cpus); 279 280 idx = cpufreq_cdev->max_level - state; 281 freq = cpufreq_cdev->em->table[idx].frequency; 282 *power = cpu_freq_to_power(cpufreq_cdev, freq) * num_cpus; 283 284 return 0; 285} 286 287/** 288 * cpufreq_power2state() - convert power to a cooling device state 289 * @cdev: &thermal_cooling_device pointer 290 * @tz: a valid thermal zone device pointer 291 * @power: power in milliwatts to be converted 292 * @state: pointer in which to store the resulting state 293 * 294 * Calculate a cooling device state for the cpus described by @cdev 295 * that would allow them to consume at most @power mW and store it in 296 * @state. Note that this calculation depends on external factors 297 * such as the cpu load or the current static power. Calling this 298 * function with the same power as input can yield different cooling 299 * device states depending on those external factors. 300 * 301 * Return: 0 on success, -ENODEV if no cpus are online or -EINVAL if 302 * the calculated frequency could not be converted to a valid state. 303 * The latter should not happen unless the frequencies available to 304 * cpufreq have changed since the initialization of the cpu cooling 305 * device. 306 */ 307static int cpufreq_power2state(struct thermal_cooling_device *cdev, 308 struct thermal_zone_device *tz, u32 power, 309 unsigned long *state) 310{ 311 unsigned int target_freq; 312 u32 last_load, normalised_power; 313 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; 314 struct cpufreq_policy *policy = cpufreq_cdev->policy; 315 316 last_load = cpufreq_cdev->last_load ?: 1; 317 normalised_power = (power * 100) / last_load; 318 target_freq = cpu_power_to_freq(cpufreq_cdev, normalised_power); 319 320 *state = get_level(cpufreq_cdev, target_freq); 321 trace_thermal_power_cpu_limit(policy->related_cpus, target_freq, *state, 322 power); 323 return 0; 324} 325 326static inline bool em_is_sane(struct cpufreq_cooling_device *cpufreq_cdev, 327 struct em_perf_domain *em) { 328 struct cpufreq_policy *policy; 329 unsigned int nr_levels; 330 331 if (!em) 332 return false; 333 334 policy = cpufreq_cdev->policy; 335 if (!cpumask_equal(policy->related_cpus, to_cpumask(em->cpus))) { 336 pr_err("The span of pd %*pbl is misaligned with cpufreq policy %*pbl\n", 337 cpumask_pr_args(to_cpumask(em->cpus)), 338 cpumask_pr_args(policy->related_cpus)); 339 return false; 340 } 341 342 nr_levels = cpufreq_cdev->max_level + 1; 343 if (em->nr_cap_states != nr_levels) { 344 pr_err("The number of cap states in pd %*pbl (%u) doesn't match the number of cooling levels (%u)\n", 345 cpumask_pr_args(to_cpumask(em->cpus)), 346 em->nr_cap_states, nr_levels); 347 return false; 348 } 349 350 return true; 351} 352#endif /* CONFIG_THERMAL_GOV_POWER_ALLOCATOR */ 353 354static unsigned int get_state_freq(struct cpufreq_cooling_device *cpufreq_cdev, 355 unsigned long state) 356{ 357 struct cpufreq_policy *policy; 358 unsigned long idx; 359 360#ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR 361 /* Use the Energy Model table if available */ 362 if (cpufreq_cdev->em) { 363 idx = cpufreq_cdev->max_level - state; 364 return cpufreq_cdev->em->table[idx].frequency; 365 } 366#endif 367 368 /* Otherwise, fallback on the CPUFreq table */ 369 policy = cpufreq_cdev->policy; 370 if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING) 371 idx = cpufreq_cdev->max_level - state; 372 else 373 idx = state; 374 375 return policy->freq_table[idx].frequency; 376} 377 378/* cpufreq cooling device callback functions are defined below */ 379 380/** 381 * cpufreq_get_max_state - callback function to get the max cooling state. 382 * @cdev: thermal cooling device pointer. 383 * @state: fill this variable with the max cooling state. 384 * 385 * Callback for the thermal cooling device to return the cpufreq 386 * max cooling state. 387 * 388 * Return: 0 on success, an error code otherwise. 389 */ 390static int cpufreq_get_max_state(struct thermal_cooling_device *cdev, 391 unsigned long *state) 392{ 393 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; 394 395 *state = cpufreq_cdev->max_level; 396 return 0; 397} 398 399/** 400 * cpufreq_get_cur_state - callback function to get the current cooling state. 401 * @cdev: thermal cooling device pointer. 402 * @state: fill this variable with the current cooling state. 403 * 404 * Callback for the thermal cooling device to return the cpufreq 405 * current cooling state. 406 * 407 * Return: 0 on success, an error code otherwise. 408 */ 409static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev, 410 unsigned long *state) 411{ 412 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; 413 414 *state = cpufreq_cdev->cpufreq_state; 415 416 return 0; 417} 418 419/** 420 * cpufreq_set_cur_state - callback function to set the current cooling state. 421 * @cdev: thermal cooling device pointer. 422 * @state: set this variable to the current cooling state. 423 * 424 * Callback for the thermal cooling device to change the cpufreq 425 * current cooling state. 426 * 427 * Return: 0 on success, an error code otherwise. 428 */ 429static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev, 430 unsigned long state) 431{ 432 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; 433 434 /* Request state should be less than max_level */ 435 if (WARN_ON(state > cpufreq_cdev->max_level)) 436 return -EINVAL; 437 438 /* Check if the old cooling action is same as new cooling action */ 439 if (cpufreq_cdev->cpufreq_state == state) 440 return 0; 441 442 cpufreq_cdev->cpufreq_state = state; 443 444 return freq_qos_update_request(&cpufreq_cdev->qos_req, 445 get_state_freq(cpufreq_cdev, state)); 446} 447 448/* Bind cpufreq callbacks to thermal cooling device ops */ 449 450static struct thermal_cooling_device_ops cpufreq_cooling_ops = { 451 .get_max_state = cpufreq_get_max_state, 452 .get_cur_state = cpufreq_get_cur_state, 453 .set_cur_state = cpufreq_set_cur_state, 454}; 455 456/** 457 * __cpufreq_cooling_register - helper function to create cpufreq cooling device 458 * @np: a valid struct device_node to the cooling device device tree node 459 * @policy: cpufreq policy 460 * Normally this should be same as cpufreq policy->related_cpus. 461 * @em: Energy Model of the cpufreq policy 462 * 463 * This interface function registers the cpufreq cooling device with the name 464 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq 465 * cooling devices. It also gives the opportunity to link the cooling device 466 * with a device tree node, in order to bind it via the thermal DT code. 467 * 468 * Return: a valid struct thermal_cooling_device pointer on success, 469 * on failure, it returns a corresponding ERR_PTR(). 470 */ 471static struct thermal_cooling_device * 472__cpufreq_cooling_register(struct device_node *np, 473 struct cpufreq_policy *policy, 474 struct em_perf_domain *em) 475{ 476 struct thermal_cooling_device *cdev; 477 struct cpufreq_cooling_device *cpufreq_cdev; 478 char dev_name[THERMAL_NAME_LENGTH]; 479 unsigned int i, num_cpus; 480 struct device *dev; 481 int ret; 482 struct thermal_cooling_device_ops *cooling_ops; 483 484 dev = get_cpu_device(policy->cpu); 485 if (unlikely(!dev)) { 486 pr_warn("No cpu device for cpu %d\n", policy->cpu); 487 return ERR_PTR(-ENODEV); 488 } 489 490 491 if (IS_ERR_OR_NULL(policy)) { 492 pr_err("%s: cpufreq policy isn't valid: %p\n", __func__, policy); 493 return ERR_PTR(-EINVAL); 494 } 495 496 i = cpufreq_table_count_valid_entries(policy); 497 if (!i) { 498 pr_debug("%s: CPUFreq table not found or has no valid entries\n", 499 __func__); 500 return ERR_PTR(-ENODEV); 501 } 502 503 cpufreq_cdev = kzalloc(sizeof(*cpufreq_cdev), GFP_KERNEL); 504 if (!cpufreq_cdev) 505 return ERR_PTR(-ENOMEM); 506 507 cpufreq_cdev->policy = policy; 508 num_cpus = cpumask_weight(policy->related_cpus); 509 cpufreq_cdev->idle_time = kcalloc(num_cpus, 510 sizeof(*cpufreq_cdev->idle_time), 511 GFP_KERNEL); 512 if (!cpufreq_cdev->idle_time) { 513 cdev = ERR_PTR(-ENOMEM); 514 goto free_cdev; 515 } 516 517 /* max_level is an index, not a counter */ 518 cpufreq_cdev->max_level = i - 1; 519 520 ret = ida_simple_get(&cpufreq_ida, 0, 0, GFP_KERNEL); 521 if (ret < 0) { 522 cdev = ERR_PTR(ret); 523 goto free_idle_time; 524 } 525 cpufreq_cdev->id = ret; 526 527 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d", 528 cpufreq_cdev->id); 529 530 cooling_ops = &cpufreq_cooling_ops; 531 532#ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR 533 if (em_is_sane(cpufreq_cdev, em)) { 534 cpufreq_cdev->em = em; 535 cooling_ops->get_requested_power = cpufreq_get_requested_power; 536 cooling_ops->state2power = cpufreq_state2power; 537 cooling_ops->power2state = cpufreq_power2state; 538 } else 539#endif 540 if (policy->freq_table_sorted == CPUFREQ_TABLE_UNSORTED) { 541 pr_err("%s: unsorted frequency tables are not supported\n", 542 __func__); 543 cdev = ERR_PTR(-EINVAL); 544 goto remove_ida; 545 } 546 547 ret = freq_qos_add_request(&policy->constraints, 548 &cpufreq_cdev->qos_req, FREQ_QOS_MAX, 549 get_state_freq(cpufreq_cdev, 0)); 550 if (ret < 0) { 551 pr_err("%s: Failed to add freq constraint (%d)\n", __func__, 552 ret); 553 cdev = ERR_PTR(ret); 554 goto remove_ida; 555 } 556 557 cdev = thermal_of_cooling_device_register(np, dev_name, cpufreq_cdev, 558 cooling_ops); 559 if (IS_ERR(cdev)) 560 goto remove_qos_req; 561 562 mutex_lock(&cooling_list_lock); 563 list_add(&cpufreq_cdev->node, &cpufreq_cdev_list); 564 mutex_unlock(&cooling_list_lock); 565 566 return cdev; 567 568remove_qos_req: 569 freq_qos_remove_request(&cpufreq_cdev->qos_req); 570remove_ida: 571 ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id); 572free_idle_time: 573 kfree(cpufreq_cdev->idle_time); 574free_cdev: 575 kfree(cpufreq_cdev); 576 return cdev; 577} 578 579/** 580 * cpufreq_cooling_register - function to create cpufreq cooling device. 581 * @policy: cpufreq policy 582 * 583 * This interface function registers the cpufreq cooling device with the name 584 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq 585 * cooling devices. 586 * 587 * Return: a valid struct thermal_cooling_device pointer on success, 588 * on failure, it returns a corresponding ERR_PTR(). 589 */ 590struct thermal_cooling_device * 591cpufreq_cooling_register(struct cpufreq_policy *policy) 592{ 593 return __cpufreq_cooling_register(NULL, policy, NULL); 594} 595EXPORT_SYMBOL_GPL(cpufreq_cooling_register); 596 597/** 598 * of_cpufreq_cooling_register - function to create cpufreq cooling device. 599 * @policy: cpufreq policy 600 * 601 * This interface function registers the cpufreq cooling device with the name 602 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq 603 * cooling devices. Using this API, the cpufreq cooling device will be 604 * linked to the device tree node provided. 605 * 606 * Using this function, the cooling device will implement the power 607 * extensions by using a simple cpu power model. The cpus must have 608 * registered their OPPs using the OPP library. 609 * 610 * It also takes into account, if property present in policy CPU node, the 611 * static power consumed by the cpu. 612 * 613 * Return: a valid struct thermal_cooling_device pointer on success, 614 * and NULL on failure. 615 */ 616struct thermal_cooling_device * 617of_cpufreq_cooling_register(struct cpufreq_policy *policy) 618{ 619 struct device_node *np = of_get_cpu_node(policy->cpu, NULL); 620 struct thermal_cooling_device *cdev = NULL; 621 622 if (!np) { 623 pr_err("cpu_cooling: OF node not available for cpu%d\n", 624 policy->cpu); 625 return NULL; 626 } 627 628 if (of_find_property(np, "#cooling-cells", NULL)) { 629 struct em_perf_domain *em = em_cpu_get(policy->cpu); 630 631 cdev = __cpufreq_cooling_register(np, policy, em); 632 if (IS_ERR(cdev)) { 633 pr_err("cpu_cooling: cpu%d failed to register as cooling device: %ld\n", 634 policy->cpu, PTR_ERR(cdev)); 635 cdev = NULL; 636 } 637 } 638 639 of_node_put(np); 640 return cdev; 641} 642EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register); 643 644/** 645 * cpufreq_cooling_unregister - function to remove cpufreq cooling device. 646 * @cdev: thermal cooling device pointer. 647 * 648 * This interface function unregisters the "thermal-cpufreq-%x" cooling device. 649 */ 650void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev) 651{ 652 struct cpufreq_cooling_device *cpufreq_cdev; 653 654 if (!cdev) 655 return; 656 657 cpufreq_cdev = cdev->devdata; 658 659 mutex_lock(&cooling_list_lock); 660 list_del(&cpufreq_cdev->node); 661 mutex_unlock(&cooling_list_lock); 662 663 thermal_cooling_device_unregister(cdev); 664 freq_qos_remove_request(&cpufreq_cdev->qos_req); 665 ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id); 666 kfree(cpufreq_cdev->idle_time); 667 kfree(cpufreq_cdev); 668} 669EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);