| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/thermal/cpu_cooling.c |
| 3 | * |
| 4 | * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com) |
| 5 | * Copyright (C) 2012 Amit Daniel <amit.kachhap@linaro.org> |
| 6 | * |
| 7 | * Copyright (C) 2014 Viresh Kumar <viresh.kumar@linaro.org> |
| 8 | * |
| 9 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; version 2 of the License. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but |
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 22 | * |
| 23 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 24 | */ |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/thermal.h> |
| 27 | #include <linux/cpufreq.h> |
| 28 | #include <linux/err.h> |
| 29 | #include <linux/idr.h> |
| 30 | #include <linux/pm_opp.h> |
| 31 | #include <linux/slab.h> |
| 32 | #include <linux/cpu.h> |
| 33 | #include <linux/cpu_cooling.h> |
| 34 | #include <linux/energy_model.h> |
| 35 | |
| 36 | #include <trace/events/thermal.h> |
| 37 | |
| 38 | /* |
| 39 | * Cooling state <-> CPUFreq frequency |
| 40 | * |
| 41 | * Cooling states are translated to frequencies throughout this driver and this |
| 42 | * is the relation between them. |
| 43 | * |
| 44 | * Highest cooling state corresponds to lowest possible frequency. |
| 45 | * |
| 46 | * i.e. |
| 47 | * level 0 --> 1st Max Freq |
| 48 | * level 1 --> 2nd Max Freq |
| 49 | * ... |
| 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 | */ |
| 57 | struct 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 | * @em: Reference on the Energy Model of the device |
| 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 | */ |
| 83 | struct 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 em_perf_domain *em; |
| 90 | struct thermal_cooling_device *cdev; |
| 91 | struct cpufreq_policy *policy; |
| 92 | struct list_head node; |
| 93 | struct time_in_idle *idle_time; |
| 94 | }; |
| 95 | |
| 96 | static DEFINE_IDA(cpufreq_ida); |
| 97 | static DEFINE_MUTEX(cooling_list_lock); |
| 98 | static LIST_HEAD(cpufreq_cdev_list); |
| 99 | |
| 100 | /* Below code defines functions to be used for cpufreq as cooling device */ |
| 101 | |
| 102 | /** |
| 103 | * cpufreq_thermal_notifier - notifier callback for cpufreq policy change. |
| 104 | * @nb: struct notifier_block * with callback info. |
| 105 | * @event: value showing cpufreq event for which this function invoked. |
| 106 | * @data: callback-specific data |
| 107 | * |
| 108 | * Callback to hijack the notification on cpufreq policy transition. |
| 109 | * Every time there is a change in policy, we will intercept and |
| 110 | * update the cpufreq policy with thermal constraints. |
| 111 | * |
| 112 | * Return: 0 (success) |
| 113 | */ |
| 114 | static int cpufreq_thermal_notifier(struct notifier_block *nb, |
| 115 | unsigned long event, void *data) |
| 116 | { |
| 117 | struct cpufreq_policy *policy = data; |
| 118 | unsigned long clipped_freq; |
| 119 | struct cpufreq_cooling_device *cpufreq_cdev; |
| 120 | |
| 121 | if (event != CPUFREQ_ADJUST) |
| 122 | return NOTIFY_DONE; |
| 123 | |
| 124 | mutex_lock(&cooling_list_lock); |
| 125 | list_for_each_entry(cpufreq_cdev, &cpufreq_cdev_list, node) { |
| 126 | /* |
| 127 | * A new copy of the policy is sent to the notifier and can't |
| 128 | * compare that directly. |
| 129 | */ |
| 130 | if (policy->cpu != cpufreq_cdev->policy->cpu) |
| 131 | continue; |
| 132 | |
| 133 | /* |
| 134 | * policy->max is the maximum allowed frequency defined by user |
| 135 | * and clipped_freq is the maximum that thermal constraints |
| 136 | * allow. |
| 137 | * |
| 138 | * If clipped_freq is lower than policy->max, then we need to |
| 139 | * readjust policy->max. |
| 140 | * |
| 141 | * But, if clipped_freq is greater than policy->max, we don't |
| 142 | * need to do anything. |
| 143 | */ |
| 144 | clipped_freq = cpufreq_cdev->clipped_freq; |
| 145 | |
| 146 | if (policy->max > clipped_freq) |
| 147 | cpufreq_verify_within_limits(policy, 0, clipped_freq); |
| 148 | break; |
| 149 | } |
| 150 | mutex_unlock(&cooling_list_lock); |
| 151 | |
| 152 | return NOTIFY_OK; |
| 153 | } |
| 154 | |
| 155 | #ifdef CONFIG_ENERGY_MODEL |
| 156 | /** |
| 157 | * get_level: Find the level for a particular frequency |
| 158 | * @cpufreq_cdev: cpufreq_cdev for which the property is required |
| 159 | * @freq: Frequency |
| 160 | * |
| 161 | * Return: level corresponding to the frequency. |
| 162 | */ |
| 163 | static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_cdev, |
| 164 | unsigned int freq) |
| 165 | { |
| 166 | int i; |
| 167 | |
| 168 | for (i = cpufreq_cdev->max_level - 1; i >= 0; i--) { |
| 169 | if (freq > cpufreq_cdev->em->table[i].frequency) |
| 170 | break; |
| 171 | } |
| 172 | |
| 173 | return cpufreq_cdev->max_level - i - 1; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_cdev, |
| 178 | u32 freq) |
| 179 | { |
| 180 | int i; |
| 181 | |
| 182 | for (i = cpufreq_cdev->max_level - 1; i >= 0; i--) { |
| 183 | if (freq > cpufreq_cdev->em->table[i].frequency) |
| 184 | break; |
| 185 | } |
| 186 | |
| 187 | return cpufreq_cdev->em->table[i + 1].power; |
| 188 | } |
| 189 | |
| 190 | static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_cdev, |
| 191 | u32 power) |
| 192 | { |
| 193 | int i; |
| 194 | |
| 195 | for (i = cpufreq_cdev->max_level - 1; i >= 0; i--) { |
| 196 | if (power > cpufreq_cdev->em->table[i].power) |
| 197 | break; |
| 198 | } |
| 199 | |
| 200 | return cpufreq_cdev->em->table[i + 1].frequency; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * get_load() - get load for a cpu since last updated |
| 205 | * @cpufreq_cdev: &struct cpufreq_cooling_device for this cpu |
| 206 | * @cpu: cpu number |
| 207 | * @cpu_idx: index of the cpu in time_in_idle* |
| 208 | * |
| 209 | * Return: The average load of cpu @cpu in percentage since this |
| 210 | * function was last called. |
| 211 | */ |
| 212 | static u32 get_load(struct cpufreq_cooling_device *cpufreq_cdev, int cpu, |
| 213 | int cpu_idx) |
| 214 | { |
| 215 | u32 load; |
| 216 | u64 now, now_idle, delta_time, delta_idle; |
| 217 | struct time_in_idle *idle_time = &cpufreq_cdev->idle_time[cpu_idx]; |
| 218 | |
| 219 | now_idle = get_cpu_idle_time(cpu, &now, 0); |
| 220 | delta_idle = now_idle - idle_time->time; |
| 221 | delta_time = now - idle_time->timestamp; |
| 222 | |
| 223 | if (delta_time <= delta_idle) |
| 224 | load = 0; |
| 225 | else |
| 226 | load = div64_u64(100 * (delta_time - delta_idle), delta_time); |
| 227 | |
| 228 | idle_time->time = now_idle; |
| 229 | idle_time->timestamp = now; |
| 230 | |
| 231 | return load; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * get_dynamic_power() - calculate the dynamic power |
| 236 | * @cpufreq_cdev: &cpufreq_cooling_device for this cdev |
| 237 | * @freq: current frequency |
| 238 | * |
| 239 | * Return: the dynamic power consumed by the cpus described by |
| 240 | * @cpufreq_cdev. |
| 241 | */ |
| 242 | static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_cdev, |
| 243 | unsigned long freq) |
| 244 | { |
| 245 | u32 raw_cpu_power; |
| 246 | |
| 247 | raw_cpu_power = cpu_freq_to_power(cpufreq_cdev, freq); |
| 248 | return (raw_cpu_power * cpufreq_cdev->last_load) / 100; |
| 249 | } |
| 250 | #endif |
| 251 | |
| 252 | /* cpufreq cooling device callback functions are defined below */ |
| 253 | |
| 254 | /** |
| 255 | * cpufreq_get_max_state - callback function to get the max cooling state. |
| 256 | * @cdev: thermal cooling device pointer. |
| 257 | * @state: fill this variable with the max cooling state. |
| 258 | * |
| 259 | * Callback for the thermal cooling device to return the cpufreq |
| 260 | * max cooling state. |
| 261 | * |
| 262 | * Return: 0 on success, an error code otherwise. |
| 263 | */ |
| 264 | static int cpufreq_get_max_state(struct thermal_cooling_device *cdev, |
| 265 | unsigned long *state) |
| 266 | { |
| 267 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
| 268 | |
| 269 | *state = cpufreq_cdev->max_level; |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * cpufreq_get_cur_state - callback function to get the current cooling state. |
| 275 | * @cdev: thermal cooling device pointer. |
| 276 | * @state: fill this variable with the current cooling state. |
| 277 | * |
| 278 | * Callback for the thermal cooling device to return the cpufreq |
| 279 | * current cooling state. |
| 280 | * |
| 281 | * Return: 0 on success, an error code otherwise. |
| 282 | */ |
| 283 | static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev, |
| 284 | unsigned long *state) |
| 285 | { |
| 286 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
| 287 | |
| 288 | *state = cpufreq_cdev->cpufreq_state; |
| 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | static unsigned int get_state_freq(struct cpufreq_cooling_device *cpufreq_cdev, |
| 294 | unsigned long state) |
| 295 | { |
| 296 | struct cpufreq_policy *policy; |
| 297 | unsigned long idx; |
| 298 | |
| 299 | #ifdef CONFIG_ENERGY_MODEL |
| 300 | /* Use the Energy Model table if available */ |
| 301 | if (cpufreq_cdev->em) { |
| 302 | idx = cpufreq_cdev->max_level - state; |
| 303 | return cpufreq_cdev->em->table[idx].frequency; |
| 304 | } |
| 305 | #endif |
| 306 | |
| 307 | /* Otherwise, fallback on the CPUFreq table */ |
| 308 | policy = cpufreq_cdev->policy; |
| 309 | if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING) |
| 310 | idx = cpufreq_cdev->max_level - state; |
| 311 | else |
| 312 | idx = state; |
| 313 | |
| 314 | return policy->freq_table[idx].frequency; |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * cpufreq_set_cur_state - callback function to set the current cooling state. |
| 319 | * @cdev: thermal cooling device pointer. |
| 320 | * @state: set this variable to the current cooling state. |
| 321 | * |
| 322 | * Callback for the thermal cooling device to change the cpufreq |
| 323 | * current cooling state. |
| 324 | * |
| 325 | * Return: 0 on success, an error code otherwise. |
| 326 | */ |
| 327 | static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev, |
| 328 | unsigned long state) |
| 329 | { |
| 330 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
| 331 | unsigned int clip_freq; |
| 332 | |
| 333 | /* Request state should be less than max_level */ |
| 334 | if (WARN_ON(state > cpufreq_cdev->max_level)) |
| 335 | return -EINVAL; |
| 336 | |
| 337 | /* Check if the old cooling action is same as new cooling action */ |
| 338 | if (cpufreq_cdev->cpufreq_state == state) |
| 339 | return 0; |
| 340 | |
| 341 | clip_freq = get_state_freq(cpufreq_cdev, state); |
| 342 | cpufreq_cdev->cpufreq_state = state; |
| 343 | cpufreq_cdev->clipped_freq = clip_freq; |
| 344 | |
| 345 | cpufreq_update_policy(cpufreq_cdev->policy->cpu); |
| 346 | |
| 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | #ifdef CONFIG_ENERGY_MODEL |
| 351 | /** |
| 352 | * cpufreq_get_requested_power() - get the current power |
| 353 | * @cdev: &thermal_cooling_device pointer |
| 354 | * @tz: a valid thermal zone device pointer |
| 355 | * @power: pointer in which to store the resulting power |
| 356 | * |
| 357 | * Calculate the current power consumption of the cpus in milliwatts |
| 358 | * and store it in @power. This function should actually calculate |
| 359 | * the requested power, but it's hard to get the frequency that |
| 360 | * cpufreq would have assigned if there were no thermal limits. |
| 361 | * Instead, we calculate the current power on the assumption that the |
| 362 | * immediate future will look like the immediate past. |
| 363 | * |
| 364 | * We use the current frequency and the average load since this |
| 365 | * function was last called. In reality, there could have been |
| 366 | * multiple opps since this function was last called and that affects |
| 367 | * the load calculation. While it's not perfectly accurate, this |
| 368 | * simplification is good enough and works. REVISIT this, as more |
| 369 | * complex code may be needed if experiments show that it's not |
| 370 | * accurate enough. |
| 371 | * |
| 372 | * Return: 0 on success, -E* if getting the static power failed. |
| 373 | */ |
| 374 | static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev, |
| 375 | struct thermal_zone_device *tz, |
| 376 | u32 *power) |
| 377 | { |
| 378 | unsigned long freq; |
| 379 | int i = 0, cpu; |
| 380 | u32 total_load = 0; |
| 381 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
| 382 | struct cpufreq_policy *policy = cpufreq_cdev->policy; |
| 383 | u32 *load_cpu = NULL; |
| 384 | |
| 385 | freq = cpufreq_quick_get(policy->cpu); |
| 386 | |
| 387 | if (trace_thermal_power_cpu_get_power_enabled()) { |
| 388 | u32 ncpus = cpumask_weight(policy->related_cpus); |
| 389 | |
| 390 | load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL); |
| 391 | } |
| 392 | |
| 393 | for_each_cpu(cpu, policy->related_cpus) { |
| 394 | u32 load; |
| 395 | |
| 396 | if (cpu_online(cpu)) |
| 397 | load = get_load(cpufreq_cdev, cpu, i); |
| 398 | else |
| 399 | load = 0; |
| 400 | |
| 401 | total_load += load; |
| 402 | if (trace_thermal_power_cpu_limit_enabled() && load_cpu) |
| 403 | load_cpu[i] = load; |
| 404 | |
| 405 | i++; |
| 406 | } |
| 407 | |
| 408 | cpufreq_cdev->last_load = total_load; |
| 409 | |
| 410 | *power = get_dynamic_power(cpufreq_cdev, freq); |
| 411 | |
| 412 | if (load_cpu) { |
| 413 | trace_thermal_power_cpu_get_power(policy->related_cpus, freq, |
| 414 | load_cpu, i, *power); |
| 415 | |
| 416 | kfree(load_cpu); |
| 417 | } |
| 418 | |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * cpufreq_state2power() - convert a cpu cdev state to power consumed |
| 424 | * @cdev: &thermal_cooling_device pointer |
| 425 | * @tz: a valid thermal zone device pointer |
| 426 | * @state: cooling device state to be converted |
| 427 | * @power: pointer in which to store the resulting power |
| 428 | * |
| 429 | * Convert cooling device state @state into power consumption in |
| 430 | * milliwatts assuming 100% load. Store the calculated power in |
| 431 | * @power. |
| 432 | * |
| 433 | * Return: 0 on success, -EINVAL if the cooling device state could not |
| 434 | * be converted into a frequency or other -E* if there was an error |
| 435 | * when calculating the static power. |
| 436 | */ |
| 437 | static int cpufreq_state2power(struct thermal_cooling_device *cdev, |
| 438 | struct thermal_zone_device *tz, |
| 439 | unsigned long state, u32 *power) |
| 440 | { |
| 441 | unsigned int freq, num_cpus, idx; |
| 442 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
| 443 | |
| 444 | /* Request state should be less than max_level */ |
| 445 | if (WARN_ON(state > cpufreq_cdev->max_level)) |
| 446 | return -EINVAL; |
| 447 | |
| 448 | num_cpus = cpumask_weight(cpufreq_cdev->policy->cpus); |
| 449 | |
| 450 | idx = cpufreq_cdev->max_level - state; |
| 451 | freq = cpufreq_cdev->em->table[idx].frequency; |
| 452 | *power = cpu_freq_to_power(cpufreq_cdev, freq) * num_cpus; |
| 453 | |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * cpufreq_power2state() - convert power to a cooling device state |
| 459 | * @cdev: &thermal_cooling_device pointer |
| 460 | * @tz: a valid thermal zone device pointer |
| 461 | * @power: power in milliwatts to be converted |
| 462 | * @state: pointer in which to store the resulting state |
| 463 | * |
| 464 | * Calculate a cooling device state for the cpus described by @cdev |
| 465 | * that would allow them to consume at most @power mW and store it in |
| 466 | * @state. Note that this calculation depends on external factors |
| 467 | * such as the cpu load or the current static power. Calling this |
| 468 | * function with the same power as input can yield different cooling |
| 469 | * device states depending on those external factors. |
| 470 | * |
| 471 | * Return: 0 on success, -ENODEV if no cpus are online or -EINVAL if |
| 472 | * the calculated frequency could not be converted to a valid state. |
| 473 | * The latter should not happen unless the frequencies available to |
| 474 | * cpufreq have changed since the initialization of the cpu cooling |
| 475 | * device. |
| 476 | */ |
| 477 | static int cpufreq_power2state(struct thermal_cooling_device *cdev, |
| 478 | struct thermal_zone_device *tz, u32 power, |
| 479 | unsigned long *state) |
| 480 | { |
| 481 | unsigned int cur_freq, target_freq; |
| 482 | u32 last_load, normalised_power; |
| 483 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
| 484 | struct cpufreq_policy *policy = cpufreq_cdev->policy; |
| 485 | |
| 486 | cur_freq = cpufreq_quick_get(policy->cpu); |
| 487 | power = power > 0 ? power : 0; |
| 488 | last_load = cpufreq_cdev->last_load ?: 1; |
| 489 | normalised_power = (power * 100) / last_load; |
| 490 | target_freq = cpu_power_to_freq(cpufreq_cdev, normalised_power); |
| 491 | |
| 492 | *state = get_level(cpufreq_cdev, target_freq); |
| 493 | trace_thermal_power_cpu_limit(policy->related_cpus, target_freq, *state, |
| 494 | power); |
| 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | static struct thermal_cooling_device_ops cpufreq_power_cooling_ops = { |
| 499 | .get_max_state = cpufreq_get_max_state, |
| 500 | .get_cur_state = cpufreq_get_cur_state, |
| 501 | .set_cur_state = cpufreq_set_cur_state, |
| 502 | .get_requested_power = cpufreq_get_requested_power, |
| 503 | .state2power = cpufreq_state2power, |
| 504 | .power2state = cpufreq_power2state, |
| 505 | }; |
| 506 | #endif |
| 507 | |
| 508 | /* Bind cpufreq callbacks to thermal cooling device ops */ |
| 509 | |
| 510 | static struct thermal_cooling_device_ops cpufreq_cooling_ops = { |
| 511 | .get_max_state = cpufreq_get_max_state, |
| 512 | .get_cur_state = cpufreq_get_cur_state, |
| 513 | .set_cur_state = cpufreq_set_cur_state, |
| 514 | }; |
| 515 | |
| 516 | /* Notifier for cpufreq policy change */ |
| 517 | static struct notifier_block thermal_cpufreq_notifier_block = { |
| 518 | .notifier_call = cpufreq_thermal_notifier, |
| 519 | }; |
| 520 | |
| 521 | /** |
| 522 | * __cpufreq_cooling_register - helper function to create cpufreq cooling device |
| 523 | * @np: a valid struct device_node to the cooling device device tree node |
| 524 | * @policy: cpufreq policy |
| 525 | * Normally this should be same as cpufreq policy->related_cpus. |
| 526 | * @try_model: true if a power model should be used |
| 527 | * |
| 528 | * This interface function registers the cpufreq cooling device with the name |
| 529 | * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq |
| 530 | * cooling devices. It also gives the opportunity to link the cooling device |
| 531 | * with a device tree node, in order to bind it via the thermal DT code. |
| 532 | * |
| 533 | * Return: a valid struct thermal_cooling_device pointer on success, |
| 534 | * on failure, it returns a corresponding ERR_PTR(). |
| 535 | */ |
| 536 | static struct thermal_cooling_device * |
| 537 | __cpufreq_cooling_register(struct device_node *np, |
| 538 | struct cpufreq_policy *policy, bool try_model) |
| 539 | { |
| 540 | struct thermal_cooling_device *cdev; |
| 541 | struct cpufreq_cooling_device *cpufreq_cdev; |
| 542 | char dev_name[THERMAL_NAME_LENGTH]; |
| 543 | unsigned int i, num_cpus; |
| 544 | int ret; |
| 545 | struct thermal_cooling_device_ops *cooling_ops; |
| 546 | bool first; |
| 547 | |
| 548 | if (IS_ERR_OR_NULL(policy)) { |
| 549 | pr_err("%s: cpufreq policy isn't valid: %p\n", __func__, policy); |
| 550 | return ERR_PTR(-EINVAL); |
| 551 | } |
| 552 | |
| 553 | i = cpufreq_table_count_valid_entries(policy); |
| 554 | if (!i) { |
| 555 | pr_debug("%s: CPUFreq table not found or has no valid entries\n", |
| 556 | __func__); |
| 557 | return ERR_PTR(-ENODEV); |
| 558 | } |
| 559 | |
| 560 | cpufreq_cdev = kzalloc(sizeof(*cpufreq_cdev), GFP_KERNEL); |
| 561 | if (!cpufreq_cdev) |
| 562 | return ERR_PTR(-ENOMEM); |
| 563 | |
| 564 | cpufreq_cdev->policy = policy; |
| 565 | num_cpus = cpumask_weight(policy->related_cpus); |
| 566 | cpufreq_cdev->idle_time = kcalloc(num_cpus, |
| 567 | sizeof(*cpufreq_cdev->idle_time), |
| 568 | GFP_KERNEL); |
| 569 | if (!cpufreq_cdev->idle_time) { |
| 570 | cdev = ERR_PTR(-ENOMEM); |
| 571 | goto free_cdev; |
| 572 | } |
| 573 | |
| 574 | /* max_level is an index, not a counter */ |
| 575 | cpufreq_cdev->max_level = i - 1; |
| 576 | |
| 577 | #ifdef CONFIG_ENERGY_MODEL |
| 578 | if (try_model) { |
| 579 | struct em_perf_domain *em = em_cpu_get(policy->cpu); |
| 580 | |
| 581 | if (!em || !cpumask_equal(policy->related_cpus, |
| 582 | to_cpumask(em->cpus))) { |
| 583 | cdev = ERR_PTR(-EINVAL); |
| 584 | goto free_idle_time; |
| 585 | } |
| 586 | cpufreq_cdev->em = em; |
| 587 | cooling_ops = &cpufreq_power_cooling_ops; |
| 588 | } else |
| 589 | #endif |
| 590 | cooling_ops = &cpufreq_cooling_ops; |
| 591 | |
| 592 | ret = ida_simple_get(&cpufreq_ida, 0, 0, GFP_KERNEL); |
| 593 | if (ret < 0) { |
| 594 | cdev = ERR_PTR(ret); |
| 595 | goto free_idle_time; |
| 596 | } |
| 597 | cpufreq_cdev->id = ret; |
| 598 | |
| 599 | snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d", |
| 600 | cpufreq_cdev->id); |
| 601 | |
| 602 | cdev = thermal_of_cooling_device_register(np, dev_name, cpufreq_cdev, |
| 603 | cooling_ops); |
| 604 | if (IS_ERR(cdev)) |
| 605 | goto remove_ida; |
| 606 | |
| 607 | cpufreq_cdev->clipped_freq = get_state_freq(cpufreq_cdev, 0); |
| 608 | cpufreq_cdev->cdev = cdev; |
| 609 | |
| 610 | mutex_lock(&cooling_list_lock); |
| 611 | /* Register the notifier for first cpufreq cooling device */ |
| 612 | first = list_empty(&cpufreq_cdev_list); |
| 613 | list_add(&cpufreq_cdev->node, &cpufreq_cdev_list); |
| 614 | mutex_unlock(&cooling_list_lock); |
| 615 | |
| 616 | if (first) |
| 617 | cpufreq_register_notifier(&thermal_cpufreq_notifier_block, |
| 618 | CPUFREQ_POLICY_NOTIFIER); |
| 619 | |
| 620 | return cdev; |
| 621 | |
| 622 | remove_ida: |
| 623 | ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id); |
| 624 | free_idle_time: |
| 625 | kfree(cpufreq_cdev->idle_time); |
| 626 | free_cdev: |
| 627 | kfree(cpufreq_cdev); |
| 628 | return cdev; |
| 629 | } |
| 630 | |
| 631 | /** |
| 632 | * cpufreq_cooling_register - function to create cpufreq cooling device. |
| 633 | * @policy: cpufreq policy |
| 634 | * |
| 635 | * This interface function registers the cpufreq cooling device with the name |
| 636 | * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq |
| 637 | * cooling devices. |
| 638 | * |
| 639 | * Return: a valid struct thermal_cooling_device pointer on success, |
| 640 | * on failure, it returns a corresponding ERR_PTR(). |
| 641 | */ |
| 642 | struct thermal_cooling_device * |
| 643 | cpufreq_cooling_register(struct cpufreq_policy *policy) |
| 644 | { |
| 645 | return __cpufreq_cooling_register(NULL, policy, false); |
| 646 | } |
| 647 | EXPORT_SYMBOL_GPL(cpufreq_cooling_register); |
| 648 | |
| 649 | /** |
| 650 | * of_cpufreq_cooling_register - function to create cpufreq cooling device. |
| 651 | * @policy: cpufreq policy |
| 652 | * |
| 653 | * This interface function registers the cpufreq cooling device with the name |
| 654 | * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq |
| 655 | * cooling devices. Using this API, the cpufreq cooling device will be |
| 656 | * linked to the device tree node provided. |
| 657 | * |
| 658 | * Using this function, the cooling device will implement the power |
| 659 | * extensions by using a simple cpu power model. The cpus must have |
| 660 | * registered their OPPs using the OPP library. |
| 661 | * |
| 662 | * It also takes into account, if property present in policy CPU node, the |
| 663 | * static power consumed by the cpu. |
| 664 | * |
| 665 | * Return: a valid struct thermal_cooling_device pointer on success, |
| 666 | * and NULL on failure. |
| 667 | */ |
| 668 | struct thermal_cooling_device * |
| 669 | of_cpufreq_cooling_register(struct cpufreq_policy *policy) |
| 670 | { |
| 671 | struct device_node *np = of_get_cpu_node(policy->cpu, NULL); |
| 672 | struct thermal_cooling_device *cdev = NULL; |
| 673 | |
| 674 | if (!np) { |
| 675 | pr_err("cpu_cooling: OF node not available for cpu%d\n", |
| 676 | policy->cpu); |
| 677 | return NULL; |
| 678 | } |
| 679 | |
| 680 | if (of_find_property(np, "#cooling-cells", NULL)) { |
| 681 | cdev = __cpufreq_cooling_register(np, policy, true); |
| 682 | if (IS_ERR(cdev)) { |
| 683 | pr_err("cpu_cooling: cpu%d is not running as cooling device: %ld\n", |
| 684 | policy->cpu, PTR_ERR(cdev)); |
| 685 | cdev = NULL; |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | of_node_put(np); |
| 690 | return cdev; |
| 691 | } |
| 692 | EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register); |
| 693 | |
| 694 | /** |
| 695 | * cpufreq_cooling_unregister - function to remove cpufreq cooling device. |
| 696 | * @cdev: thermal cooling device pointer. |
| 697 | * |
| 698 | * This interface function unregisters the "thermal-cpufreq-%x" cooling device. |
| 699 | */ |
| 700 | void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev) |
| 701 | { |
| 702 | struct cpufreq_cooling_device *cpufreq_cdev; |
| 703 | bool last; |
| 704 | |
| 705 | if (!cdev) |
| 706 | return; |
| 707 | |
| 708 | cpufreq_cdev = cdev->devdata; |
| 709 | |
| 710 | mutex_lock(&cooling_list_lock); |
| 711 | list_del(&cpufreq_cdev->node); |
| 712 | /* Unregister the notifier for the last cpufreq cooling device */ |
| 713 | last = list_empty(&cpufreq_cdev_list); |
| 714 | mutex_unlock(&cooling_list_lock); |
| 715 | |
| 716 | if (last) |
| 717 | cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block, |
| 718 | CPUFREQ_POLICY_NOTIFIER); |
| 719 | |
| 720 | thermal_cooling_device_unregister(cpufreq_cdev->cdev); |
| 721 | ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id); |
| 722 | kfree(cpufreq_cdev->idle_time); |
| 723 | kfree(cpufreq_cdev); |
| 724 | } |
| 725 | EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister); |