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