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