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