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