2 * linux/drivers/thermal/cpu_cooling.c
4 * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
5 * Copyright (C) 2012 Amit Daniel <amit.kachhap@linaro.org>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23 #include <linux/module.h>
24 #include <linux/thermal.h>
25 #include <linux/cpufreq.h>
26 #include <linux/err.h>
27 #include <linux/slab.h>
28 #include <linux/cpu.h>
29 #include <linux/cpu_cooling.h>
32 * Cooling state <-> CPUFreq frequency
34 * Cooling states are translated to frequencies throughout this driver and this
35 * is the relation between them.
37 * Highest cooling state corresponds to lowest possible frequency.
40 * level 0 --> 1st Max Freq
41 * level 1 --> 2nd Max Freq
46 * struct cpufreq_cooling_device - data for cooling device with cpufreq
47 * @id: unique integer value corresponding to each cpufreq_cooling_device
49 * @cool_dev: thermal_cooling_device pointer to keep track of the
50 * registered cooling device.
51 * @cpufreq_state: integer value representing the current state of cpufreq
53 * @cpufreq_val: integer value representing the absolute value of the clipped
55 * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
57 * This structure is required for keeping information of each registered
58 * cpufreq_cooling_device.
60 struct cpufreq_cooling_device
{
62 struct thermal_cooling_device
*cool_dev
;
63 unsigned int cpufreq_state
;
64 unsigned int cpufreq_val
;
65 struct cpumask allowed_cpus
;
66 struct list_head node
;
68 static DEFINE_IDR(cpufreq_idr
);
69 static DEFINE_MUTEX(cooling_cpufreq_lock
);
71 static unsigned int cpufreq_dev_count
;
73 static LIST_HEAD(cpufreq_dev_list
);
76 * get_idr - function to get a unique id.
77 * @idr: struct idr * handle used to create a id.
78 * @id: int * value generated by this function.
80 * This function will populate @id with an unique
81 * id, using the idr API.
83 * Return: 0 on success, an error code on failure.
85 static int get_idr(struct idr
*idr
, int *id
)
89 mutex_lock(&cooling_cpufreq_lock
);
90 ret
= idr_alloc(idr
, NULL
, 0, 0, GFP_KERNEL
);
91 mutex_unlock(&cooling_cpufreq_lock
);
92 if (unlikely(ret
< 0))
100 * release_idr - function to free the unique id.
101 * @idr: struct idr * handle used for creating the id.
102 * @id: int value representing the unique id.
104 static void release_idr(struct idr
*idr
, int id
)
106 mutex_lock(&cooling_cpufreq_lock
);
108 mutex_unlock(&cooling_cpufreq_lock
);
111 /* Below code defines functions to be used for cpufreq as cooling device */
114 * is_cpufreq_valid - function to check frequency transitioning capability.
115 * @cpu: cpu for which check is needed.
117 * This function will check the current state of the system if
118 * it is capable of changing the frequency for a given @cpu.
120 * Return: 0 if the system is not currently capable of changing
121 * the frequency of given cpu. !0 in case the frequency is changeable.
123 static int is_cpufreq_valid(int cpu
)
125 struct cpufreq_policy policy
;
127 return !cpufreq_get_policy(&policy
, cpu
);
130 enum cpufreq_cooling_property
{
137 * get_property - fetch a property of interest for a given cpu.
138 * @cpu: cpu for which the property is required
139 * @input: query parameter
140 * @output: query return
141 * @property: type of query (frequency, level, max level)
143 * This is the common function to
144 * 1. get maximum cpu cooling states
145 * 2. translate frequency to cooling state
146 * 3. translate cooling state to frequency
148 * Note that the code may be not in good shape
149 * but it is written in this way in order to:
150 * a) reduce duplicate code as most of the code can be shared.
151 * b) make sure the logic is consistent when translating between
152 * cooling states and frequencies.
154 * Return: 0 on success, -EINVAL when invalid parameters are passed.
156 static int get_property(unsigned int cpu
, unsigned long input
,
157 unsigned int *output
,
158 enum cpufreq_cooling_property property
)
161 unsigned long max_level
= 0, level
= 0;
162 unsigned int freq
= CPUFREQ_ENTRY_INVALID
;
164 struct cpufreq_frequency_table
*pos
, *table
=
165 cpufreq_frequency_get_table(cpu
);
173 cpufreq_for_each_valid_entry(pos
, table
) {
174 /* ignore duplicate entry */
175 if (freq
== pos
->frequency
)
178 /* get the frequency order */
179 if (freq
!= CPUFREQ_ENTRY_INVALID
&& descend
== -1)
180 descend
= freq
> pos
->frequency
;
182 freq
= pos
->frequency
;
186 /* No valid cpu frequency entry */
190 /* max_level is an index, not a counter */
194 if (property
== GET_MAXL
) {
195 *output
= (unsigned int)max_level
;
199 if (property
== GET_FREQ
)
200 level
= descend
? input
: (max_level
- input
);
203 cpufreq_for_each_valid_entry(pos
, table
) {
204 /* ignore duplicate entry */
205 if (freq
== pos
->frequency
)
208 /* now we have a valid frequency entry */
209 freq
= pos
->frequency
;
211 if (property
== GET_LEVEL
&& (unsigned int)input
== freq
) {
212 /* get level by frequency */
213 *output
= descend
? i
: (max_level
- i
);
216 if (property
== GET_FREQ
&& level
== i
) {
217 /* get frequency by level */
228 * cpufreq_cooling_get_level - for a given cpu, return the cooling level.
229 * @cpu: cpu for which the level is required
230 * @freq: the frequency of interest
232 * This function will match the cooling level corresponding to the
233 * requested @freq and return it.
235 * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID
238 unsigned long cpufreq_cooling_get_level(unsigned int cpu
, unsigned int freq
)
242 if (get_property(cpu
, (unsigned long)freq
, &val
, GET_LEVEL
))
243 return THERMAL_CSTATE_INVALID
;
245 return (unsigned long)val
;
247 EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level
);
250 * get_cpu_frequency - get the absolute value of frequency from level.
251 * @cpu: cpu for which frequency is fetched.
252 * @level: cooling level
254 * This function matches cooling level with frequency. Based on a cooling level
255 * of frequency, equals cooling state of cpu cooling device, it will return
256 * the corresponding frequency.
257 * e.g level=0 --> 1st MAX FREQ, level=1 ---> 2nd MAX FREQ, .... etc
259 * Return: 0 on error, the corresponding frequency otherwise.
261 static unsigned int get_cpu_frequency(unsigned int cpu
, unsigned long level
)
266 ret
= get_property(cpu
, level
, &freq
, GET_FREQ
);
274 * cpufreq_apply_cooling - function to apply frequency clipping.
275 * @cpufreq_device: cpufreq_cooling_device pointer containing frequency
277 * @cooling_state: value of the cooling state.
279 * Function used to make sure the cpufreq layer is aware of current thermal
280 * limits. The limits are applied by updating the cpufreq policy.
282 * Return: 0 on success, an error code otherwise (-EINVAL in case wrong
285 static int cpufreq_apply_cooling(struct cpufreq_cooling_device
*cpufreq_device
,
286 unsigned long cooling_state
)
288 unsigned int cpuid
, clip_freq
;
289 struct cpumask
*mask
= &cpufreq_device
->allowed_cpus
;
290 unsigned int cpu
= cpumask_any(mask
);
293 /* Check if the old cooling action is same as new cooling action */
294 if (cpufreq_device
->cpufreq_state
== cooling_state
)
297 clip_freq
= get_cpu_frequency(cpu
, cooling_state
);
301 cpufreq_device
->cpufreq_state
= cooling_state
;
302 cpufreq_device
->cpufreq_val
= clip_freq
;
304 for_each_cpu(cpuid
, mask
) {
305 if (is_cpufreq_valid(cpuid
))
306 cpufreq_update_policy(cpuid
);
313 * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
314 * @nb: struct notifier_block * with callback info.
315 * @event: value showing cpufreq event for which this function invoked.
316 * @data: callback-specific data
318 * Callback to hijack the notification on cpufreq policy transition.
319 * Every time there is a change in policy, we will intercept and
320 * update the cpufreq policy with thermal constraints.
322 * Return: 0 (success)
324 static int cpufreq_thermal_notifier(struct notifier_block
*nb
,
325 unsigned long event
, void *data
)
327 struct cpufreq_policy
*policy
= data
;
328 unsigned long max_freq
= 0;
329 struct cpufreq_cooling_device
*cpufreq_dev
;
331 if (event
!= CPUFREQ_ADJUST
)
334 mutex_lock(&cooling_cpufreq_lock
);
335 list_for_each_entry(cpufreq_dev
, &cpufreq_dev_list
, node
) {
336 if (!cpumask_test_cpu(policy
->cpu
,
337 &cpufreq_dev
->allowed_cpus
))
340 if (!cpufreq_dev
->cpufreq_val
)
341 cpufreq_dev
->cpufreq_val
= get_cpu_frequency(
342 cpumask_any(&cpufreq_dev
->allowed_cpus
),
343 cpufreq_dev
->cpufreq_state
);
345 max_freq
= cpufreq_dev
->cpufreq_val
;
347 if (policy
->max
!= max_freq
)
348 cpufreq_verify_within_limits(policy
, 0, max_freq
);
350 mutex_unlock(&cooling_cpufreq_lock
);
355 /* cpufreq cooling device callback functions are defined below */
358 * cpufreq_get_max_state - callback function to get the max cooling state.
359 * @cdev: thermal cooling device pointer.
360 * @state: fill this variable with the max cooling state.
362 * Callback for the thermal cooling device to return the cpufreq
365 * Return: 0 on success, an error code otherwise.
367 static int cpufreq_get_max_state(struct thermal_cooling_device
*cdev
,
368 unsigned long *state
)
370 struct cpufreq_cooling_device
*cpufreq_device
= cdev
->devdata
;
371 struct cpumask
*mask
= &cpufreq_device
->allowed_cpus
;
373 unsigned int count
= 0;
376 cpu
= cpumask_any(mask
);
378 ret
= get_property(cpu
, 0, &count
, GET_MAXL
);
387 * cpufreq_get_cur_state - callback function to get the current cooling state.
388 * @cdev: thermal cooling device pointer.
389 * @state: fill this variable with the current cooling state.
391 * Callback for the thermal cooling device to return the cpufreq
392 * current cooling state.
394 * Return: 0 on success, an error code otherwise.
396 static int cpufreq_get_cur_state(struct thermal_cooling_device
*cdev
,
397 unsigned long *state
)
399 struct cpufreq_cooling_device
*cpufreq_device
= cdev
->devdata
;
401 *state
= cpufreq_device
->cpufreq_state
;
407 * cpufreq_set_cur_state - callback function to set the current cooling state.
408 * @cdev: thermal cooling device pointer.
409 * @state: set this variable to the current cooling state.
411 * Callback for the thermal cooling device to change the cpufreq
412 * current cooling state.
414 * Return: 0 on success, an error code otherwise.
416 static int cpufreq_set_cur_state(struct thermal_cooling_device
*cdev
,
419 struct cpufreq_cooling_device
*cpufreq_device
= cdev
->devdata
;
421 return cpufreq_apply_cooling(cpufreq_device
, state
);
424 /* Bind cpufreq callbacks to thermal cooling device ops */
425 static struct thermal_cooling_device_ops
const cpufreq_cooling_ops
= {
426 .get_max_state
= cpufreq_get_max_state
,
427 .get_cur_state
= cpufreq_get_cur_state
,
428 .set_cur_state
= cpufreq_set_cur_state
,
431 /* Notifier for cpufreq policy change */
432 static struct notifier_block thermal_cpufreq_notifier_block
= {
433 .notifier_call
= cpufreq_thermal_notifier
,
437 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
438 * @np: a valid struct device_node to the cooling device device tree node
439 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
441 * This interface function registers the cpufreq cooling device with the name
442 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
443 * cooling devices. It also gives the opportunity to link the cooling device
444 * with a device tree node, in order to bind it via the thermal DT code.
446 * Return: a valid struct thermal_cooling_device pointer on success,
447 * on failure, it returns a corresponding ERR_PTR().
449 static struct thermal_cooling_device
*
450 __cpufreq_cooling_register(struct device_node
*np
,
451 const struct cpumask
*clip_cpus
)
453 struct thermal_cooling_device
*cool_dev
;
454 struct cpufreq_cooling_device
*cpufreq_dev
;
455 unsigned int min
= 0, max
= 0;
456 char dev_name
[THERMAL_NAME_LENGTH
];
458 struct cpufreq_policy policy
;
460 if (!cpufreq_frequency_get_table(cpumask_first(clip_cpus
))) {
461 pr_debug("%s: CPUFreq table not found\n", __func__
);
462 return ERR_PTR(-EPROBE_DEFER
);
465 /* Verify that all the clip cpus have same freq_min, freq_max limit */
466 for_each_cpu(i
, clip_cpus
) {
467 /* continue if cpufreq policy not found and not return error */
468 if (!cpufreq_get_policy(&policy
, i
))
470 if (min
== 0 && max
== 0) {
471 min
= policy
.cpuinfo
.min_freq
;
472 max
= policy
.cpuinfo
.max_freq
;
474 if (min
!= policy
.cpuinfo
.min_freq
||
475 max
!= policy
.cpuinfo
.max_freq
)
476 return ERR_PTR(-EINVAL
);
479 cpufreq_dev
= kzalloc(sizeof(*cpufreq_dev
), GFP_KERNEL
);
481 return ERR_PTR(-ENOMEM
);
483 cpumask_copy(&cpufreq_dev
->allowed_cpus
, clip_cpus
);
485 ret
= get_idr(&cpufreq_idr
, &cpufreq_dev
->id
);
488 return ERR_PTR(-EINVAL
);
491 snprintf(dev_name
, sizeof(dev_name
), "thermal-cpufreq-%d",
494 cool_dev
= thermal_of_cooling_device_register(np
, dev_name
, cpufreq_dev
,
495 &cpufreq_cooling_ops
);
496 if (IS_ERR(cool_dev
)) {
497 release_idr(&cpufreq_idr
, cpufreq_dev
->id
);
501 cpufreq_dev
->cool_dev
= cool_dev
;
503 mutex_lock(&cooling_cpufreq_lock
);
505 /* Register the notifier for first cpufreq cooling device */
506 if (cpufreq_dev_count
== 0)
507 cpufreq_register_notifier(&thermal_cpufreq_notifier_block
,
508 CPUFREQ_POLICY_NOTIFIER
);
510 list_add(&cpufreq_dev
->node
, &cpufreq_dev_list
);
512 mutex_unlock(&cooling_cpufreq_lock
);
518 * cpufreq_cooling_register - function to create cpufreq cooling device.
519 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
521 * This interface function registers the cpufreq cooling device with the name
522 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
525 * Return: a valid struct thermal_cooling_device pointer on success,
526 * on failure, it returns a corresponding ERR_PTR().
528 struct thermal_cooling_device
*
529 cpufreq_cooling_register(const struct cpumask
*clip_cpus
)
531 return __cpufreq_cooling_register(NULL
, clip_cpus
);
533 EXPORT_SYMBOL_GPL(cpufreq_cooling_register
);
536 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
537 * @np: a valid struct device_node to the cooling device device tree node
538 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
540 * This interface function registers the cpufreq cooling device with the name
541 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
542 * cooling devices. Using this API, the cpufreq cooling device will be
543 * linked to the device tree node provided.
545 * Return: a valid struct thermal_cooling_device pointer on success,
546 * on failure, it returns a corresponding ERR_PTR().
548 struct thermal_cooling_device
*
549 of_cpufreq_cooling_register(struct device_node
*np
,
550 const struct cpumask
*clip_cpus
)
553 return ERR_PTR(-EINVAL
);
555 return __cpufreq_cooling_register(np
, clip_cpus
);
557 EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register
);
560 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
561 * @cdev: thermal cooling device pointer.
563 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
565 void cpufreq_cooling_unregister(struct thermal_cooling_device
*cdev
)
567 struct cpufreq_cooling_device
*cpufreq_dev
;
572 cpufreq_dev
= cdev
->devdata
;
573 mutex_lock(&cooling_cpufreq_lock
);
574 list_del(&cpufreq_dev
->node
);
577 /* Unregister the notifier for the last cpufreq cooling device */
578 if (cpufreq_dev_count
== 0)
579 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block
,
580 CPUFREQ_POLICY_NOTIFIER
);
581 mutex_unlock(&cooling_cpufreq_lock
);
583 thermal_cooling_device_unregister(cpufreq_dev
->cool_dev
);
584 release_idr(&cpufreq_idr
, cpufreq_dev
->id
);
587 EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister
);