Linux Audio

Check our new training course

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