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 * struct cpufreq_cooling_device - data for cooling device with cpufreq
33 * @id: unique integer value corresponding to each cpufreq_cooling_device
35 * @cool_dev: thermal_cooling_device pointer to keep track of the
36 * registered cooling device.
37 * @cpufreq_state: integer value representing the current state of cpufreq
39 * @cpufreq_val: integer value representing the absolute value of the clipped
41 * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
43 * This structure is required for keeping information of each
44 * cpufreq_cooling_device registered. In order to prevent corruption of this a
45 * mutex lock cooling_cpufreq_lock is used.
47 struct cpufreq_cooling_device
{
49 struct thermal_cooling_device
*cool_dev
;
50 unsigned int cpufreq_state
;
51 unsigned int cpufreq_val
;
52 struct cpumask allowed_cpus
;
53 struct list_head node
;
55 static DEFINE_IDR(cpufreq_idr
);
56 static DEFINE_MUTEX(cooling_cpufreq_lock
);
58 static unsigned int cpufreq_dev_count
;
60 static LIST_HEAD(cpufreq_dev_list
);
63 * get_idr - function to get a unique id.
64 * @idr: struct idr * handle used to create a id.
65 * @id: int * value generated by this function.
67 * This function will populate @id with an unique
68 * id, using the idr API.
70 * Return: 0 on success, an error code on failure.
72 static int get_idr(struct idr
*idr
, int *id
)
76 mutex_lock(&cooling_cpufreq_lock
);
77 ret
= idr_alloc(idr
, NULL
, 0, 0, GFP_KERNEL
);
78 mutex_unlock(&cooling_cpufreq_lock
);
79 if (unlikely(ret
< 0))
87 * release_idr - function to free the unique id.
88 * @idr: struct idr * handle used for creating the id.
89 * @id: int value representing the unique id.
91 static void release_idr(struct idr
*idr
, int id
)
93 mutex_lock(&cooling_cpufreq_lock
);
95 mutex_unlock(&cooling_cpufreq_lock
);
98 /* Below code defines functions to be used for cpufreq as cooling device */
101 * is_cpufreq_valid - function to check frequency transitioning capability.
102 * @cpu: cpu for which check is needed.
104 * This function will check the current state of the system if
105 * it is capable of changing the frequency for a given @cpu.
107 * Return: 0 if the system is not currently capable of changing
108 * the frequency of given cpu. !0 in case the frequency is changeable.
110 static int is_cpufreq_valid(int cpu
)
112 struct cpufreq_policy policy
;
114 return !cpufreq_get_policy(&policy
, cpu
);
117 enum cpufreq_cooling_property
{
124 * get_property - fetch a property of interest for a give cpu.
125 * @cpu: cpu for which the property is required
126 * @input: query parameter
127 * @output: query return
128 * @property: type of query (frequency, level, max level)
130 * This is the common function to
131 * 1. get maximum cpu cooling states
132 * 2. translate frequency to cooling state
133 * 3. translate cooling state to frequency
134 * Note that the code may be not in good shape
135 * but it is written in this way in order to:
136 * a) reduce duplicate code as most of the code can be shared.
137 * b) make sure the logic is consistent when translating between
138 * cooling states and frequencies.
140 * Return: 0 on success, -EINVAL when invalid parameters are passed.
142 static int get_property(unsigned int cpu
, unsigned long input
,
143 unsigned int *output
,
144 enum cpufreq_cooling_property property
)
147 unsigned long max_level
= 0, level
= 0;
148 unsigned int freq
= CPUFREQ_ENTRY_INVALID
;
150 struct cpufreq_frequency_table
*pos
, *table
=
151 cpufreq_frequency_get_table(cpu
);
159 cpufreq_for_each_valid_entry(pos
, table
) {
160 /* ignore duplicate entry */
161 if (freq
== pos
->frequency
)
164 /* get the frequency order */
165 if (freq
!= CPUFREQ_ENTRY_INVALID
&& descend
== -1)
166 descend
= freq
> pos
->frequency
;
168 freq
= pos
->frequency
;
172 /* No valid cpu frequency entry */
176 /* max_level is an index, not a counter */
180 if (property
== GET_MAXL
) {
181 *output
= (unsigned int)max_level
;
185 if (property
== GET_FREQ
)
186 level
= descend
? input
: (max_level
- input
);
189 cpufreq_for_each_valid_entry(pos
, table
) {
190 /* ignore duplicate entry */
191 if (freq
== pos
->frequency
)
194 /* now we have a valid frequency entry */
195 freq
= pos
->frequency
;
197 if (property
== GET_LEVEL
&& (unsigned int)input
== freq
) {
198 /* get level by frequency */
199 *output
= descend
? i
: (max_level
- i
);
202 if (property
== GET_FREQ
&& level
== i
) {
203 /* get frequency by level */
214 * cpufreq_cooling_get_level - for a give cpu, return the cooling level.
215 * @cpu: cpu for which the level is required
216 * @freq: the frequency of interest
218 * This function will match the cooling level corresponding to the
219 * requested @freq and return it.
221 * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID
224 unsigned long cpufreq_cooling_get_level(unsigned int cpu
, unsigned int freq
)
228 if (get_property(cpu
, (unsigned long)freq
, &val
, GET_LEVEL
))
229 return THERMAL_CSTATE_INVALID
;
231 return (unsigned long)val
;
233 EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level
);
236 * get_cpu_frequency - get the absolute value of frequency from level.
237 * @cpu: cpu for which frequency is fetched.
238 * @level: cooling level
240 * This function matches cooling level with frequency. Based on a cooling level
241 * of frequency, equals cooling state of cpu cooling device, it will return
242 * the corresponding frequency.
243 * e.g level=0 --> 1st MAX FREQ, level=1 ---> 2nd MAX FREQ, .... etc
245 * Return: 0 on error, the corresponding frequency otherwise.
247 static unsigned int get_cpu_frequency(unsigned int cpu
, unsigned long level
)
252 ret
= get_property(cpu
, level
, &freq
, GET_FREQ
);
260 * cpufreq_apply_cooling - function to apply frequency clipping.
261 * @cpufreq_device: cpufreq_cooling_device pointer containing frequency
263 * @cooling_state: value of the cooling state.
265 * Function used to make sure the cpufreq layer is aware of current thermal
266 * limits. The limits are applied by updating the cpufreq policy.
268 * Return: 0 on success, an error code otherwise (-EINVAL in case wrong
271 static int cpufreq_apply_cooling(struct cpufreq_cooling_device
*cpufreq_device
,
272 unsigned long cooling_state
)
274 unsigned int cpuid
, clip_freq
;
275 struct cpumask
*mask
= &cpufreq_device
->allowed_cpus
;
276 unsigned int cpu
= cpumask_any(mask
);
279 /* Check if the old cooling action is same as new cooling action */
280 if (cpufreq_device
->cpufreq_state
== cooling_state
)
283 clip_freq
= get_cpu_frequency(cpu
, cooling_state
);
287 cpufreq_device
->cpufreq_state
= cooling_state
;
288 cpufreq_device
->cpufreq_val
= clip_freq
;
290 for_each_cpu(cpuid
, mask
) {
291 if (is_cpufreq_valid(cpuid
))
292 cpufreq_update_policy(cpuid
);
299 * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
300 * @nb: struct notifier_block * with callback info.
301 * @event: value showing cpufreq event for which this function invoked.
302 * @data: callback-specific data
304 * Callback to hijack the notification on cpufreq policy transition.
305 * Every time there is a change in policy, we will intercept and
306 * update the cpufreq policy with thermal constraints.
308 * Return: 0 (success)
310 static int cpufreq_thermal_notifier(struct notifier_block
*nb
,
311 unsigned long event
, void *data
)
313 struct cpufreq_policy
*policy
= data
;
314 unsigned long max_freq
= 0;
315 struct cpufreq_cooling_device
*cpufreq_dev
;
317 if (event
!= CPUFREQ_ADJUST
)
320 mutex_lock(&cooling_cpufreq_lock
);
321 list_for_each_entry(cpufreq_dev
, &cpufreq_dev_list
, node
) {
322 if (!cpumask_test_cpu(policy
->cpu
,
323 &cpufreq_dev
->allowed_cpus
))
326 if (!cpufreq_dev
->cpufreq_val
)
327 cpufreq_dev
->cpufreq_val
= get_cpu_frequency(
328 cpumask_any(&cpufreq_dev
->allowed_cpus
),
329 cpufreq_dev
->cpufreq_state
);
331 max_freq
= cpufreq_dev
->cpufreq_val
;
333 if (policy
->max
!= max_freq
)
334 cpufreq_verify_within_limits(policy
, 0, max_freq
);
336 mutex_unlock(&cooling_cpufreq_lock
);
341 /* cpufreq cooling device callback functions are defined below */
344 * cpufreq_get_max_state - callback function to get the max cooling state.
345 * @cdev: thermal cooling device pointer.
346 * @state: fill this variable with the max cooling state.
348 * Callback for the thermal cooling device to return the cpufreq
351 * Return: 0 on success, an error code otherwise.
353 static int cpufreq_get_max_state(struct thermal_cooling_device
*cdev
,
354 unsigned long *state
)
356 struct cpufreq_cooling_device
*cpufreq_device
= cdev
->devdata
;
357 struct cpumask
*mask
= &cpufreq_device
->allowed_cpus
;
359 unsigned int count
= 0;
362 cpu
= cpumask_any(mask
);
364 ret
= get_property(cpu
, 0, &count
, GET_MAXL
);
373 * cpufreq_get_cur_state - callback function to get the current cooling state.
374 * @cdev: thermal cooling device pointer.
375 * @state: fill this variable with the current cooling state.
377 * Callback for the thermal cooling device to return the cpufreq
378 * current cooling state.
380 * Return: 0 on success, an error code otherwise.
382 static int cpufreq_get_cur_state(struct thermal_cooling_device
*cdev
,
383 unsigned long *state
)
385 struct cpufreq_cooling_device
*cpufreq_device
= cdev
->devdata
;
387 *state
= cpufreq_device
->cpufreq_state
;
393 * cpufreq_set_cur_state - callback function to set the current cooling state.
394 * @cdev: thermal cooling device pointer.
395 * @state: set this variable to the current cooling state.
397 * Callback for the thermal cooling device to change the cpufreq
398 * current cooling state.
400 * Return: 0 on success, an error code otherwise.
402 static int cpufreq_set_cur_state(struct thermal_cooling_device
*cdev
,
405 struct cpufreq_cooling_device
*cpufreq_device
= cdev
->devdata
;
407 return cpufreq_apply_cooling(cpufreq_device
, state
);
410 /* Bind cpufreq callbacks to thermal cooling device ops */
411 static struct thermal_cooling_device_ops
const cpufreq_cooling_ops
= {
412 .get_max_state
= cpufreq_get_max_state
,
413 .get_cur_state
= cpufreq_get_cur_state
,
414 .set_cur_state
= cpufreq_set_cur_state
,
417 /* Notifier for cpufreq policy change */
418 static struct notifier_block thermal_cpufreq_notifier_block
= {
419 .notifier_call
= cpufreq_thermal_notifier
,
423 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
424 * @np: a valid struct device_node to the cooling device device tree node
425 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
427 * This interface function registers the cpufreq cooling device with the name
428 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
429 * cooling devices. It also gives the opportunity to link the cooling device
430 * with a device tree node, in order to bind it via the thermal DT code.
432 * Return: a valid struct thermal_cooling_device pointer on success,
433 * on failure, it returns a corresponding ERR_PTR().
435 static struct thermal_cooling_device
*
436 __cpufreq_cooling_register(struct device_node
*np
,
437 const struct cpumask
*clip_cpus
)
439 struct thermal_cooling_device
*cool_dev
;
440 struct cpufreq_cooling_device
*cpufreq_dev
= NULL
;
441 unsigned int min
= 0, max
= 0;
442 char dev_name
[THERMAL_NAME_LENGTH
];
444 struct cpufreq_policy policy
;
446 /* Verify that all the clip cpus have same freq_min, freq_max limit */
447 for_each_cpu(i
, clip_cpus
) {
448 /* continue if cpufreq policy not found and not return error */
449 if (!cpufreq_get_policy(&policy
, i
))
451 if (min
== 0 && max
== 0) {
452 min
= policy
.cpuinfo
.min_freq
;
453 max
= policy
.cpuinfo
.max_freq
;
455 if (min
!= policy
.cpuinfo
.min_freq
||
456 max
!= policy
.cpuinfo
.max_freq
)
457 return ERR_PTR(-EINVAL
);
460 cpufreq_dev
= kzalloc(sizeof(struct cpufreq_cooling_device
),
463 return ERR_PTR(-ENOMEM
);
465 cpumask_copy(&cpufreq_dev
->allowed_cpus
, clip_cpus
);
467 ret
= get_idr(&cpufreq_idr
, &cpufreq_dev
->id
);
470 return ERR_PTR(-EINVAL
);
473 snprintf(dev_name
, sizeof(dev_name
), "thermal-cpufreq-%d",
476 cool_dev
= thermal_of_cooling_device_register(np
, dev_name
, cpufreq_dev
,
477 &cpufreq_cooling_ops
);
478 if (IS_ERR(cool_dev
)) {
479 release_idr(&cpufreq_idr
, cpufreq_dev
->id
);
483 cpufreq_dev
->cool_dev
= cool_dev
;
484 cpufreq_dev
->cpufreq_state
= 0;
485 mutex_lock(&cooling_cpufreq_lock
);
487 /* Register the notifier for first cpufreq cooling device */
488 if (cpufreq_dev_count
== 0)
489 cpufreq_register_notifier(&thermal_cpufreq_notifier_block
,
490 CPUFREQ_POLICY_NOTIFIER
);
492 list_add(&cpufreq_dev
->node
, &cpufreq_dev_list
);
494 mutex_unlock(&cooling_cpufreq_lock
);
500 * cpufreq_cooling_register - function to create cpufreq cooling device.
501 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
503 * This interface function registers the cpufreq cooling device with the name
504 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
507 * Return: a valid struct thermal_cooling_device pointer on success,
508 * on failure, it returns a corresponding ERR_PTR().
510 struct thermal_cooling_device
*
511 cpufreq_cooling_register(const struct cpumask
*clip_cpus
)
513 return __cpufreq_cooling_register(NULL
, clip_cpus
);
515 EXPORT_SYMBOL_GPL(cpufreq_cooling_register
);
518 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
519 * @np: a valid struct device_node to the cooling device device tree node
520 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
522 * This interface function registers the cpufreq cooling device with the name
523 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
524 * cooling devices. Using this API, the cpufreq cooling device will be
525 * linked to the device tree node provided.
527 * Return: a valid struct thermal_cooling_device pointer on success,
528 * on failure, it returns a corresponding ERR_PTR().
530 struct thermal_cooling_device
*
531 of_cpufreq_cooling_register(struct device_node
*np
,
532 const struct cpumask
*clip_cpus
)
535 return ERR_PTR(-EINVAL
);
537 return __cpufreq_cooling_register(np
, clip_cpus
);
539 EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register
);
542 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
543 * @cdev: thermal cooling device pointer.
545 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
547 void cpufreq_cooling_unregister(struct thermal_cooling_device
*cdev
)
549 struct cpufreq_cooling_device
*cpufreq_dev
;
554 cpufreq_dev
= cdev
->devdata
;
555 mutex_lock(&cooling_cpufreq_lock
);
556 list_del(&cpufreq_dev
->node
);
559 /* Unregister the notifier for the last cpufreq cooling device */
560 if (cpufreq_dev_count
== 0)
561 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block
,
562 CPUFREQ_POLICY_NOTIFIER
);
563 mutex_unlock(&cooling_cpufreq_lock
);
565 thermal_cooling_device_unregister(cpufreq_dev
->cool_dev
);
566 release_idr(&cpufreq_idr
, cpufreq_dev
->id
);
569 EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister
);