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