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