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