at v5.2-rc2 803 lines 24 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/slab.h> 20#include <linux/cpu.h> 21#include <linux/cpu_cooling.h> 22 23#include <trace/events/thermal.h> 24 25/* 26 * Cooling state <-> CPUFreq frequency 27 * 28 * Cooling states are translated to frequencies throughout this driver and this 29 * is the relation between them. 30 * 31 * Highest cooling state corresponds to lowest possible frequency. 32 * 33 * i.e. 34 * level 0 --> 1st Max Freq 35 * level 1 --> 2nd Max Freq 36 * ... 37 */ 38 39/** 40 * struct freq_table - frequency table along with power entries 41 * @frequency: frequency in KHz 42 * @power: power in mW 43 * 44 * This structure is built when the cooling device registers and helps 45 * in translating frequency to power and vice versa. 46 */ 47struct freq_table { 48 u32 frequency; 49 u32 power; 50}; 51 52/** 53 * struct time_in_idle - Idle time stats 54 * @time: previous reading of the absolute time that this cpu was idle 55 * @timestamp: wall time of the last invocation of get_cpu_idle_time_us() 56 */ 57struct time_in_idle { 58 u64 time; 59 u64 timestamp; 60}; 61 62/** 63 * struct cpufreq_cooling_device - data for cooling device with cpufreq 64 * @id: unique integer value corresponding to each cpufreq_cooling_device 65 * registered. 66 * @last_load: load measured by the latest call to cpufreq_get_requested_power() 67 * @cpufreq_state: integer value representing the current state of cpufreq 68 * cooling devices. 69 * @clipped_freq: integer value representing the absolute value of the clipped 70 * frequency. 71 * @max_level: maximum cooling level. One less than total number of valid 72 * cpufreq frequencies. 73 * @freq_table: Freq table in descending order of frequencies 74 * @cdev: thermal_cooling_device pointer to keep track of the 75 * registered cooling device. 76 * @policy: cpufreq policy. 77 * @node: list_head to link all cpufreq_cooling_device together. 78 * @idle_time: idle time stats 79 * 80 * This structure is required for keeping information of each registered 81 * cpufreq_cooling_device. 82 */ 83struct cpufreq_cooling_device { 84 int id; 85 u32 last_load; 86 unsigned int cpufreq_state; 87 unsigned int clipped_freq; 88 unsigned int max_level; 89 struct freq_table *freq_table; /* In descending order */ 90 struct cpufreq_policy *policy; 91 struct list_head node; 92 struct time_in_idle *idle_time; 93}; 94 95static DEFINE_IDA(cpufreq_ida); 96static DEFINE_MUTEX(cooling_list_lock); 97static LIST_HEAD(cpufreq_cdev_list); 98 99/* Below code defines functions to be used for cpufreq as cooling device */ 100 101/** 102 * get_level: Find the level for a particular frequency 103 * @cpufreq_cdev: cpufreq_cdev for which the property is required 104 * @freq: Frequency 105 * 106 * Return: level corresponding to the frequency. 107 */ 108static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_cdev, 109 unsigned int freq) 110{ 111 struct freq_table *freq_table = cpufreq_cdev->freq_table; 112 unsigned long level; 113 114 for (level = 1; level <= cpufreq_cdev->max_level; level++) 115 if (freq > freq_table[level].frequency) 116 break; 117 118 return level - 1; 119} 120 121/** 122 * cpufreq_thermal_notifier - notifier callback for cpufreq policy change. 123 * @nb: struct notifier_block * with callback info. 124 * @event: value showing cpufreq event for which this function invoked. 125 * @data: callback-specific data 126 * 127 * Callback to hijack the notification on cpufreq policy transition. 128 * Every time there is a change in policy, we will intercept and 129 * update the cpufreq policy with thermal constraints. 130 * 131 * Return: 0 (success) 132 */ 133static int cpufreq_thermal_notifier(struct notifier_block *nb, 134 unsigned long event, void *data) 135{ 136 struct cpufreq_policy *policy = data; 137 unsigned long clipped_freq; 138 struct cpufreq_cooling_device *cpufreq_cdev; 139 140 if (event != CPUFREQ_ADJUST) 141 return NOTIFY_DONE; 142 143 mutex_lock(&cooling_list_lock); 144 list_for_each_entry(cpufreq_cdev, &cpufreq_cdev_list, node) { 145 /* 146 * A new copy of the policy is sent to the notifier and can't 147 * compare that directly. 148 */ 149 if (policy->cpu != cpufreq_cdev->policy->cpu) 150 continue; 151 152 /* 153 * policy->max is the maximum allowed frequency defined by user 154 * and clipped_freq is the maximum that thermal constraints 155 * allow. 156 * 157 * If clipped_freq is lower than policy->max, then we need to 158 * readjust policy->max. 159 * 160 * But, if clipped_freq is greater than policy->max, we don't 161 * need to do anything. 162 */ 163 clipped_freq = cpufreq_cdev->clipped_freq; 164 165 if (policy->max > clipped_freq) 166 cpufreq_verify_within_limits(policy, 0, clipped_freq); 167 break; 168 } 169 mutex_unlock(&cooling_list_lock); 170 171 return NOTIFY_OK; 172} 173 174/** 175 * update_freq_table() - Update the freq table with power numbers 176 * @cpufreq_cdev: the cpufreq cooling device in which to update the table 177 * @capacitance: dynamic power coefficient for these cpus 178 * 179 * Update the freq table with power numbers. This table will be used in 180 * cpu_power_to_freq() and cpu_freq_to_power() to convert between power and 181 * frequency efficiently. Power is stored in mW, frequency in KHz. The 182 * resulting table is in descending order. 183 * 184 * Return: 0 on success, -EINVAL if there are no OPPs for any CPUs, 185 * or -ENOMEM if we run out of memory. 186 */ 187static int update_freq_table(struct cpufreq_cooling_device *cpufreq_cdev, 188 u32 capacitance) 189{ 190 struct freq_table *freq_table = cpufreq_cdev->freq_table; 191 struct dev_pm_opp *opp; 192 struct device *dev = NULL; 193 int num_opps = 0, cpu = cpufreq_cdev->policy->cpu, i; 194 195 dev = get_cpu_device(cpu); 196 if (unlikely(!dev)) { 197 pr_warn("No cpu device for cpu %d\n", cpu); 198 return -ENODEV; 199 } 200 201 num_opps = dev_pm_opp_get_opp_count(dev); 202 if (num_opps < 0) 203 return num_opps; 204 205 /* 206 * The cpufreq table is also built from the OPP table and so the count 207 * should match. 208 */ 209 if (num_opps != cpufreq_cdev->max_level + 1) { 210 dev_warn(dev, "Number of OPPs not matching with max_levels\n"); 211 return -EINVAL; 212 } 213 214 for (i = 0; i <= cpufreq_cdev->max_level; i++) { 215 unsigned long freq = freq_table[i].frequency * 1000; 216 u32 freq_mhz = freq_table[i].frequency / 1000; 217 u64 power; 218 u32 voltage_mv; 219 220 /* 221 * Find ceil frequency as 'freq' may be slightly lower than OPP 222 * freq due to truncation while converting to kHz. 223 */ 224 opp = dev_pm_opp_find_freq_ceil(dev, &freq); 225 if (IS_ERR(opp)) { 226 dev_err(dev, "failed to get opp for %lu frequency\n", 227 freq); 228 return -EINVAL; 229 } 230 231 voltage_mv = dev_pm_opp_get_voltage(opp) / 1000; 232 dev_pm_opp_put(opp); 233 234 /* 235 * Do the multiplication with MHz and millivolt so as 236 * to not overflow. 237 */ 238 power = (u64)capacitance * freq_mhz * voltage_mv * voltage_mv; 239 do_div(power, 1000000000); 240 241 /* power is stored in mW */ 242 freq_table[i].power = power; 243 } 244 245 return 0; 246} 247 248static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_cdev, 249 u32 freq) 250{ 251 int i; 252 struct freq_table *freq_table = cpufreq_cdev->freq_table; 253 254 for (i = 1; i <= cpufreq_cdev->max_level; i++) 255 if (freq > freq_table[i].frequency) 256 break; 257 258 return freq_table[i - 1].power; 259} 260 261static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_cdev, 262 u32 power) 263{ 264 int i; 265 struct freq_table *freq_table = cpufreq_cdev->freq_table; 266 267 for (i = 1; i <= cpufreq_cdev->max_level; i++) 268 if (power > freq_table[i].power) 269 break; 270 271 return freq_table[i - 1].frequency; 272} 273 274/** 275 * get_load() - get load for a cpu since last updated 276 * @cpufreq_cdev: &struct cpufreq_cooling_device for this cpu 277 * @cpu: cpu number 278 * @cpu_idx: index of the cpu in time_in_idle* 279 * 280 * Return: The average load of cpu @cpu in percentage since this 281 * function was last called. 282 */ 283static u32 get_load(struct cpufreq_cooling_device *cpufreq_cdev, int cpu, 284 int cpu_idx) 285{ 286 u32 load; 287 u64 now, now_idle, delta_time, delta_idle; 288 struct time_in_idle *idle_time = &cpufreq_cdev->idle_time[cpu_idx]; 289 290 now_idle = get_cpu_idle_time(cpu, &now, 0); 291 delta_idle = now_idle - idle_time->time; 292 delta_time = now - idle_time->timestamp; 293 294 if (delta_time <= delta_idle) 295 load = 0; 296 else 297 load = div64_u64(100 * (delta_time - delta_idle), delta_time); 298 299 idle_time->time = now_idle; 300 idle_time->timestamp = now; 301 302 return load; 303} 304 305/** 306 * get_dynamic_power() - calculate the dynamic power 307 * @cpufreq_cdev: &cpufreq_cooling_device for this cdev 308 * @freq: current frequency 309 * 310 * Return: the dynamic power consumed by the cpus described by 311 * @cpufreq_cdev. 312 */ 313static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_cdev, 314 unsigned long freq) 315{ 316 u32 raw_cpu_power; 317 318 raw_cpu_power = cpu_freq_to_power(cpufreq_cdev, freq); 319 return (raw_cpu_power * cpufreq_cdev->last_load) / 100; 320} 321 322/* cpufreq cooling device callback functions are defined below */ 323 324/** 325 * cpufreq_get_max_state - callback function to get the max cooling state. 326 * @cdev: thermal cooling device pointer. 327 * @state: fill this variable with the max cooling state. 328 * 329 * Callback for the thermal cooling device to return the cpufreq 330 * max cooling state. 331 * 332 * Return: 0 on success, an error code otherwise. 333 */ 334static int cpufreq_get_max_state(struct thermal_cooling_device *cdev, 335 unsigned long *state) 336{ 337 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; 338 339 *state = cpufreq_cdev->max_level; 340 return 0; 341} 342 343/** 344 * cpufreq_get_cur_state - callback function to get the current cooling state. 345 * @cdev: thermal cooling device pointer. 346 * @state: fill this variable with the current cooling state. 347 * 348 * Callback for the thermal cooling device to return the cpufreq 349 * current cooling state. 350 * 351 * Return: 0 on success, an error code otherwise. 352 */ 353static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev, 354 unsigned long *state) 355{ 356 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; 357 358 *state = cpufreq_cdev->cpufreq_state; 359 360 return 0; 361} 362 363/** 364 * cpufreq_set_cur_state - callback function to set the current cooling state. 365 * @cdev: thermal cooling device pointer. 366 * @state: set this variable to the current cooling state. 367 * 368 * Callback for the thermal cooling device to change the cpufreq 369 * current cooling state. 370 * 371 * Return: 0 on success, an error code otherwise. 372 */ 373static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev, 374 unsigned long state) 375{ 376 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; 377 unsigned int clip_freq; 378 379 /* Request state should be less than max_level */ 380 if (WARN_ON(state > cpufreq_cdev->max_level)) 381 return -EINVAL; 382 383 /* Check if the old cooling action is same as new cooling action */ 384 if (cpufreq_cdev->cpufreq_state == state) 385 return 0; 386 387 clip_freq = cpufreq_cdev->freq_table[state].frequency; 388 cpufreq_cdev->cpufreq_state = state; 389 cpufreq_cdev->clipped_freq = clip_freq; 390 391 cpufreq_update_policy(cpufreq_cdev->policy->cpu); 392 393 return 0; 394} 395 396/** 397 * cpufreq_get_requested_power() - get the current power 398 * @cdev: &thermal_cooling_device pointer 399 * @tz: a valid thermal zone device pointer 400 * @power: pointer in which to store the resulting power 401 * 402 * Calculate the current power consumption of the cpus in milliwatts 403 * and store it in @power. This function should actually calculate 404 * the requested power, but it's hard to get the frequency that 405 * cpufreq would have assigned if there were no thermal limits. 406 * Instead, we calculate the current power on the assumption that the 407 * immediate future will look like the immediate past. 408 * 409 * We use the current frequency and the average load since this 410 * function was last called. In reality, there could have been 411 * multiple opps since this function was last called and that affects 412 * the load calculation. While it's not perfectly accurate, this 413 * simplification is good enough and works. REVISIT this, as more 414 * complex code may be needed if experiments show that it's not 415 * accurate enough. 416 * 417 * Return: 0 on success, -E* if getting the static power failed. 418 */ 419static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev, 420 struct thermal_zone_device *tz, 421 u32 *power) 422{ 423 unsigned long freq; 424 int i = 0, cpu; 425 u32 total_load = 0; 426 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; 427 struct cpufreq_policy *policy = cpufreq_cdev->policy; 428 u32 *load_cpu = NULL; 429 430 freq = cpufreq_quick_get(policy->cpu); 431 432 if (trace_thermal_power_cpu_get_power_enabled()) { 433 u32 ncpus = cpumask_weight(policy->related_cpus); 434 435 load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL); 436 } 437 438 for_each_cpu(cpu, policy->related_cpus) { 439 u32 load; 440 441 if (cpu_online(cpu)) 442 load = get_load(cpufreq_cdev, cpu, i); 443 else 444 load = 0; 445 446 total_load += load; 447 if (load_cpu) 448 load_cpu[i] = load; 449 450 i++; 451 } 452 453 cpufreq_cdev->last_load = total_load; 454 455 *power = get_dynamic_power(cpufreq_cdev, freq); 456 457 if (load_cpu) { 458 trace_thermal_power_cpu_get_power(policy->related_cpus, freq, 459 load_cpu, i, *power); 460 461 kfree(load_cpu); 462 } 463 464 return 0; 465} 466 467/** 468 * cpufreq_state2power() - convert a cpu cdev state to power consumed 469 * @cdev: &thermal_cooling_device pointer 470 * @tz: a valid thermal zone device pointer 471 * @state: cooling device state to be converted 472 * @power: pointer in which to store the resulting power 473 * 474 * Convert cooling device state @state into power consumption in 475 * milliwatts assuming 100% load. Store the calculated power in 476 * @power. 477 * 478 * Return: 0 on success, -EINVAL if the cooling device state could not 479 * be converted into a frequency or other -E* if there was an error 480 * when calculating the static power. 481 */ 482static int cpufreq_state2power(struct thermal_cooling_device *cdev, 483 struct thermal_zone_device *tz, 484 unsigned long state, u32 *power) 485{ 486 unsigned int freq, num_cpus; 487 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; 488 489 /* Request state should be less than max_level */ 490 if (WARN_ON(state > cpufreq_cdev->max_level)) 491 return -EINVAL; 492 493 num_cpus = cpumask_weight(cpufreq_cdev->policy->cpus); 494 495 freq = cpufreq_cdev->freq_table[state].frequency; 496 *power = cpu_freq_to_power(cpufreq_cdev, freq) * num_cpus; 497 498 return 0; 499} 500 501/** 502 * cpufreq_power2state() - convert power to a cooling device state 503 * @cdev: &thermal_cooling_device pointer 504 * @tz: a valid thermal zone device pointer 505 * @power: power in milliwatts to be converted 506 * @state: pointer in which to store the resulting state 507 * 508 * Calculate a cooling device state for the cpus described by @cdev 509 * that would allow them to consume at most @power mW and store it in 510 * @state. Note that this calculation depends on external factors 511 * such as the cpu load or the current static power. Calling this 512 * function with the same power as input can yield different cooling 513 * device states depending on those external factors. 514 * 515 * Return: 0 on success, -ENODEV if no cpus are online or -EINVAL if 516 * the calculated frequency could not be converted to a valid state. 517 * The latter should not happen unless the frequencies available to 518 * cpufreq have changed since the initialization of the cpu cooling 519 * device. 520 */ 521static int cpufreq_power2state(struct thermal_cooling_device *cdev, 522 struct thermal_zone_device *tz, u32 power, 523 unsigned long *state) 524{ 525 unsigned int target_freq; 526 u32 last_load, normalised_power; 527 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; 528 struct cpufreq_policy *policy = cpufreq_cdev->policy; 529 530 last_load = cpufreq_cdev->last_load ?: 1; 531 normalised_power = (power * 100) / last_load; 532 target_freq = cpu_power_to_freq(cpufreq_cdev, normalised_power); 533 534 *state = get_level(cpufreq_cdev, target_freq); 535 trace_thermal_power_cpu_limit(policy->related_cpus, target_freq, *state, 536 power); 537 return 0; 538} 539 540/* Bind cpufreq callbacks to thermal cooling device ops */ 541 542static struct thermal_cooling_device_ops cpufreq_cooling_ops = { 543 .get_max_state = cpufreq_get_max_state, 544 .get_cur_state = cpufreq_get_cur_state, 545 .set_cur_state = cpufreq_set_cur_state, 546}; 547 548static struct thermal_cooling_device_ops cpufreq_power_cooling_ops = { 549 .get_max_state = cpufreq_get_max_state, 550 .get_cur_state = cpufreq_get_cur_state, 551 .set_cur_state = cpufreq_set_cur_state, 552 .get_requested_power = cpufreq_get_requested_power, 553 .state2power = cpufreq_state2power, 554 .power2state = cpufreq_power2state, 555}; 556 557/* Notifier for cpufreq policy change */ 558static struct notifier_block thermal_cpufreq_notifier_block = { 559 .notifier_call = cpufreq_thermal_notifier, 560}; 561 562static unsigned int find_next_max(struct cpufreq_frequency_table *table, 563 unsigned int prev_max) 564{ 565 struct cpufreq_frequency_table *pos; 566 unsigned int max = 0; 567 568 cpufreq_for_each_valid_entry(pos, table) { 569 if (pos->frequency > max && pos->frequency < prev_max) 570 max = pos->frequency; 571 } 572 573 return max; 574} 575 576/** 577 * __cpufreq_cooling_register - helper function to create cpufreq cooling device 578 * @np: a valid struct device_node to the cooling device device tree node 579 * @policy: cpufreq policy 580 * Normally this should be same as cpufreq policy->related_cpus. 581 * @capacitance: dynamic power coefficient for these cpus 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. It also gives the opportunity to link the cooling device 586 * with a device tree node, in order to bind it via the thermal DT code. 587 * 588 * Return: a valid struct thermal_cooling_device pointer on success, 589 * on failure, it returns a corresponding ERR_PTR(). 590 */ 591static struct thermal_cooling_device * 592__cpufreq_cooling_register(struct device_node *np, 593 struct cpufreq_policy *policy, u32 capacitance) 594{ 595 struct thermal_cooling_device *cdev; 596 struct cpufreq_cooling_device *cpufreq_cdev; 597 char dev_name[THERMAL_NAME_LENGTH]; 598 unsigned int freq, i, num_cpus; 599 int ret; 600 struct thermal_cooling_device_ops *cooling_ops; 601 bool first; 602 603 if (IS_ERR_OR_NULL(policy)) { 604 pr_err("%s: cpufreq policy isn't valid: %p\n", __func__, policy); 605 return ERR_PTR(-EINVAL); 606 } 607 608 i = cpufreq_table_count_valid_entries(policy); 609 if (!i) { 610 pr_debug("%s: CPUFreq table not found or has no valid entries\n", 611 __func__); 612 return ERR_PTR(-ENODEV); 613 } 614 615 cpufreq_cdev = kzalloc(sizeof(*cpufreq_cdev), GFP_KERNEL); 616 if (!cpufreq_cdev) 617 return ERR_PTR(-ENOMEM); 618 619 cpufreq_cdev->policy = policy; 620 num_cpus = cpumask_weight(policy->related_cpus); 621 cpufreq_cdev->idle_time = kcalloc(num_cpus, 622 sizeof(*cpufreq_cdev->idle_time), 623 GFP_KERNEL); 624 if (!cpufreq_cdev->idle_time) { 625 cdev = ERR_PTR(-ENOMEM); 626 goto free_cdev; 627 } 628 629 /* max_level is an index, not a counter */ 630 cpufreq_cdev->max_level = i - 1; 631 632 cpufreq_cdev->freq_table = kmalloc_array(i, 633 sizeof(*cpufreq_cdev->freq_table), 634 GFP_KERNEL); 635 if (!cpufreq_cdev->freq_table) { 636 cdev = ERR_PTR(-ENOMEM); 637 goto free_idle_time; 638 } 639 640 ret = ida_simple_get(&cpufreq_ida, 0, 0, GFP_KERNEL); 641 if (ret < 0) { 642 cdev = ERR_PTR(ret); 643 goto free_table; 644 } 645 cpufreq_cdev->id = ret; 646 647 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d", 648 cpufreq_cdev->id); 649 650 /* Fill freq-table in descending order of frequencies */ 651 for (i = 0, freq = -1; i <= cpufreq_cdev->max_level; i++) { 652 freq = find_next_max(policy->freq_table, freq); 653 cpufreq_cdev->freq_table[i].frequency = freq; 654 655 /* Warn for duplicate entries */ 656 if (!freq) 657 pr_warn("%s: table has duplicate entries\n", __func__); 658 else 659 pr_debug("%s: freq:%u KHz\n", __func__, freq); 660 } 661 662 if (capacitance) { 663 ret = update_freq_table(cpufreq_cdev, capacitance); 664 if (ret) { 665 cdev = ERR_PTR(ret); 666 goto remove_ida; 667 } 668 669 cooling_ops = &cpufreq_power_cooling_ops; 670 } else { 671 cooling_ops = &cpufreq_cooling_ops; 672 } 673 674 cdev = thermal_of_cooling_device_register(np, dev_name, cpufreq_cdev, 675 cooling_ops); 676 if (IS_ERR(cdev)) 677 goto remove_ida; 678 679 cpufreq_cdev->clipped_freq = cpufreq_cdev->freq_table[0].frequency; 680 681 mutex_lock(&cooling_list_lock); 682 /* Register the notifier for first cpufreq cooling device */ 683 first = list_empty(&cpufreq_cdev_list); 684 list_add(&cpufreq_cdev->node, &cpufreq_cdev_list); 685 mutex_unlock(&cooling_list_lock); 686 687 if (first) 688 cpufreq_register_notifier(&thermal_cpufreq_notifier_block, 689 CPUFREQ_POLICY_NOTIFIER); 690 691 return cdev; 692 693remove_ida: 694 ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id); 695free_table: 696 kfree(cpufreq_cdev->freq_table); 697free_idle_time: 698 kfree(cpufreq_cdev->idle_time); 699free_cdev: 700 kfree(cpufreq_cdev); 701 return cdev; 702} 703 704/** 705 * cpufreq_cooling_register - function to create cpufreq cooling device. 706 * @policy: cpufreq policy 707 * 708 * This interface function registers the cpufreq cooling device with the name 709 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq 710 * cooling devices. 711 * 712 * Return: a valid struct thermal_cooling_device pointer on success, 713 * on failure, it returns a corresponding ERR_PTR(). 714 */ 715struct thermal_cooling_device * 716cpufreq_cooling_register(struct cpufreq_policy *policy) 717{ 718 return __cpufreq_cooling_register(NULL, policy, 0); 719} 720EXPORT_SYMBOL_GPL(cpufreq_cooling_register); 721 722/** 723 * of_cpufreq_cooling_register - function to create cpufreq cooling device. 724 * @policy: cpufreq policy 725 * 726 * This interface function registers the cpufreq cooling device with the name 727 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq 728 * cooling devices. Using this API, the cpufreq cooling device will be 729 * linked to the device tree node provided. 730 * 731 * Using this function, the cooling device will implement the power 732 * extensions by using a simple cpu power model. The cpus must have 733 * registered their OPPs using the OPP library. 734 * 735 * It also takes into account, if property present in policy CPU node, the 736 * static power consumed by the cpu. 737 * 738 * Return: a valid struct thermal_cooling_device pointer on success, 739 * and NULL on failure. 740 */ 741struct thermal_cooling_device * 742of_cpufreq_cooling_register(struct cpufreq_policy *policy) 743{ 744 struct device_node *np = of_get_cpu_node(policy->cpu, NULL); 745 struct thermal_cooling_device *cdev = NULL; 746 u32 capacitance = 0; 747 748 if (!np) { 749 pr_err("cpu_cooling: OF node not available for cpu%d\n", 750 policy->cpu); 751 return NULL; 752 } 753 754 if (of_find_property(np, "#cooling-cells", NULL)) { 755 of_property_read_u32(np, "dynamic-power-coefficient", 756 &capacitance); 757 758 cdev = __cpufreq_cooling_register(np, policy, capacitance); 759 if (IS_ERR(cdev)) { 760 pr_err("cpu_cooling: cpu%d failed to register as cooling device: %ld\n", 761 policy->cpu, PTR_ERR(cdev)); 762 cdev = NULL; 763 } 764 } 765 766 of_node_put(np); 767 return cdev; 768} 769EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register); 770 771/** 772 * cpufreq_cooling_unregister - function to remove cpufreq cooling device. 773 * @cdev: thermal cooling device pointer. 774 * 775 * This interface function unregisters the "thermal-cpufreq-%x" cooling device. 776 */ 777void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev) 778{ 779 struct cpufreq_cooling_device *cpufreq_cdev; 780 bool last; 781 782 if (!cdev) 783 return; 784 785 cpufreq_cdev = cdev->devdata; 786 787 mutex_lock(&cooling_list_lock); 788 list_del(&cpufreq_cdev->node); 789 /* Unregister the notifier for the last cpufreq cooling device */ 790 last = list_empty(&cpufreq_cdev_list); 791 mutex_unlock(&cooling_list_lock); 792 793 if (last) 794 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block, 795 CPUFREQ_POLICY_NOTIFIER); 796 797 thermal_cooling_device_unregister(cdev); 798 ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id); 799 kfree(cpufreq_cdev->idle_time); 800 kfree(cpufreq_cdev->freq_table); 801 kfree(cpufreq_cdev); 802} 803EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);