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/kernel.h>
24 #include <linux/module.h>
25 #include <linux/thermal.h>
26 #include <linux/platform_device.h>
27 #include <linux/cpufreq.h>
28 #include <linux/err.h>
29 #include <linux/slab.h>
30 #include <linux/cpu.h>
31 #include <linux/cpu_cooling.h>
34 * struct cpufreq_cooling_device
35 * @id: unique integer value corresponding to each cpufreq_cooling_device
37 * @cool_dev: thermal_cooling_device pointer to keep track of the the
38 * egistered cooling device.
39 * @cpufreq_state: integer value representing the current state of cpufreq
41 * @cpufreq_val: integer value representing the absolute value of the clipped
43 * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
44 * @node: list_head to link all cpufreq_cooling_device together.
46 * This structure is required for keeping information of each
47 * cpufreq_cooling_device registered as a list whose head is represented by
48 * cooling_cpufreq_list. In order to prevent corruption of this list a
49 * mutex lock cooling_cpufreq_lock is used.
51 struct cpufreq_cooling_device
{
53 struct thermal_cooling_device
*cool_dev
;
54 unsigned int cpufreq_state
;
55 unsigned int cpufreq_val
;
56 struct cpumask allowed_cpus
;
57 struct list_head node
;
59 static LIST_HEAD(cooling_cpufreq_list
);
60 static DEFINE_IDR(cpufreq_idr
);
62 static struct mutex cooling_cpufreq_lock
;
64 /* notify_table passes value to the CPUFREQ_ADJUST callback function. */
65 #define NOTIFY_INVALID NULL
66 struct cpufreq_cooling_device
*notify_device
;
69 * get_idr - function to get a unique id.
70 * @idr: struct idr * handle used to create a id.
71 * @id: int * value generated by this function.
73 static int get_idr(struct idr
*idr
, int *id
)
77 if (unlikely(idr_pre_get(idr
, GFP_KERNEL
) == 0))
80 mutex_lock(&cooling_cpufreq_lock
);
81 err
= idr_get_new(idr
, NULL
, id
);
82 mutex_unlock(&cooling_cpufreq_lock
);
84 if (unlikely(err
== -EAGAIN
))
86 else if (unlikely(err
))
89 *id
= *id
& MAX_IDR_MASK
;
94 * release_idr - function to free the unique id.
95 * @idr: struct idr * handle used for creating the id.
96 * @id: int value representing the unique id.
98 static void release_idr(struct idr
*idr
, int id
)
100 mutex_lock(&cooling_cpufreq_lock
);
102 mutex_unlock(&cooling_cpufreq_lock
);
105 /* Below code defines functions to be used for cpufreq as cooling device */
108 * is_cpufreq_valid - function to check if a cpu has frequency transition policy.
109 * @cpu: cpu for which check is needed.
111 static int is_cpufreq_valid(int cpu
)
113 struct cpufreq_policy policy
;
114 return !cpufreq_get_policy(&policy
, cpu
);
118 * get_cpu_frequency - get the absolute value of frequency from level.
119 * @cpu: cpu for which frequency is fetched.
120 * @level: level of frequency of the CPU
121 * e.g level=1 --> 1st MAX FREQ, LEVEL=2 ---> 2nd MAX FREQ, .... etc
123 static unsigned int get_cpu_frequency(unsigned int cpu
, unsigned long level
)
126 unsigned long level_index
;
127 bool descend
= false;
128 struct cpufreq_frequency_table
*table
=
129 cpufreq_frequency_get_table(cpu
);
133 while (table
[i
].frequency
!= CPUFREQ_TABLE_END
) {
134 if (table
[i
].frequency
== CPUFREQ_ENTRY_INVALID
)
137 /*check if table in ascending or descending order*/
138 if ((table
[i
+ 1].frequency
!= CPUFREQ_TABLE_END
) &&
139 (table
[i
+ 1].frequency
< table
[i
].frequency
)
144 /*return if level matched and table in descending order*/
145 if (descend
&& i
== level
)
146 return table
[i
].frequency
;
151 if (level
> i
|| descend
)
153 level_index
= i
- level
;
155 /*Scan the table in reverse order and match the level*/
157 if (table
[i
].frequency
== CPUFREQ_ENTRY_INVALID
)
159 /*return if level matched*/
160 if (i
== level_index
)
161 return table
[i
].frequency
;
168 * cpufreq_apply_cooling - function to apply frequency clipping.
169 * @cpufreq_device: cpufreq_cooling_device pointer containing frequency
171 * @cooling_state: value of the cooling state.
173 static int cpufreq_apply_cooling(struct cpufreq_cooling_device
*cpufreq_device
,
174 unsigned long cooling_state
)
176 unsigned int cpuid
, clip_freq
;
177 struct cpumask
*maskPtr
= &cpufreq_device
->allowed_cpus
;
178 unsigned int cpu
= cpumask_any(maskPtr
);
181 /* Check if the old cooling action is same as new cooling action */
182 if (cpufreq_device
->cpufreq_state
== cooling_state
)
185 clip_freq
= get_cpu_frequency(cpu
, cooling_state
);
189 cpufreq_device
->cpufreq_state
= cooling_state
;
190 cpufreq_device
->cpufreq_val
= clip_freq
;
191 notify_device
= cpufreq_device
;
193 for_each_cpu(cpuid
, maskPtr
) {
194 if (is_cpufreq_valid(cpuid
))
195 cpufreq_update_policy(cpuid
);
198 notify_device
= NOTIFY_INVALID
;
204 * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
205 * @nb: struct notifier_block * with callback info.
206 * @event: value showing cpufreq event for which this function invoked.
207 * @data: callback-specific data
209 static int cpufreq_thermal_notifier(struct notifier_block
*nb
,
210 unsigned long event
, void *data
)
212 struct cpufreq_policy
*policy
= data
;
213 unsigned long max_freq
= 0;
215 if (event
!= CPUFREQ_ADJUST
|| notify_device
== NOTIFY_INVALID
)
218 if (cpumask_test_cpu(policy
->cpu
, ¬ify_device
->allowed_cpus
))
219 max_freq
= notify_device
->cpufreq_val
;
221 /* Never exceed user_policy.max*/
222 if (max_freq
> policy
->user_policy
.max
)
223 max_freq
= policy
->user_policy
.max
;
225 if (policy
->max
!= max_freq
)
226 cpufreq_verify_within_limits(policy
, 0, max_freq
);
232 * cpufreq cooling device callback functions are defined below
236 * cpufreq_get_max_state - callback function to get the max cooling state.
237 * @cdev: thermal cooling device pointer.
238 * @state: fill this variable with the max cooling state.
240 static int cpufreq_get_max_state(struct thermal_cooling_device
*cdev
,
241 unsigned long *state
)
243 int ret
= -EINVAL
, i
= 0;
244 struct cpufreq_cooling_device
*cpufreq_device
;
245 struct cpumask
*maskPtr
;
247 struct cpufreq_frequency_table
*table
;
248 unsigned long count
= 0;
250 mutex_lock(&cooling_cpufreq_lock
);
251 list_for_each_entry(cpufreq_device
, &cooling_cpufreq_list
, node
) {
252 if (cpufreq_device
&& cpufreq_device
->cool_dev
== cdev
)
255 if (cpufreq_device
== NULL
)
256 goto return_get_max_state
;
258 maskPtr
= &cpufreq_device
->allowed_cpus
;
259 cpu
= cpumask_any(maskPtr
);
260 table
= cpufreq_frequency_get_table(cpu
);
264 goto return_get_max_state
;
267 for (i
= 0; (table
[i
].frequency
!= CPUFREQ_TABLE_END
); i
++) {
268 if (table
[i
].frequency
== CPUFREQ_ENTRY_INVALID
)
278 return_get_max_state
:
279 mutex_unlock(&cooling_cpufreq_lock
);
284 * cpufreq_get_cur_state - callback function to get the current cooling state.
285 * @cdev: thermal cooling device pointer.
286 * @state: fill this variable with the current cooling state.
288 static int cpufreq_get_cur_state(struct thermal_cooling_device
*cdev
,
289 unsigned long *state
)
292 struct cpufreq_cooling_device
*cpufreq_device
;
294 mutex_lock(&cooling_cpufreq_lock
);
295 list_for_each_entry(cpufreq_device
, &cooling_cpufreq_list
, node
) {
296 if (cpufreq_device
&& cpufreq_device
->cool_dev
== cdev
) {
297 *state
= cpufreq_device
->cpufreq_state
;
302 mutex_unlock(&cooling_cpufreq_lock
);
308 * cpufreq_set_cur_state - callback function to set the current cooling state.
309 * @cdev: thermal cooling device pointer.
310 * @state: set this variable to the current cooling state.
312 static int cpufreq_set_cur_state(struct thermal_cooling_device
*cdev
,
316 struct cpufreq_cooling_device
*cpufreq_device
;
318 mutex_lock(&cooling_cpufreq_lock
);
319 list_for_each_entry(cpufreq_device
, &cooling_cpufreq_list
, node
) {
320 if (cpufreq_device
&& cpufreq_device
->cool_dev
== cdev
) {
326 ret
= cpufreq_apply_cooling(cpufreq_device
, state
);
328 mutex_unlock(&cooling_cpufreq_lock
);
333 /* Bind cpufreq callbacks to thermal cooling device ops */
334 static struct thermal_cooling_device_ops
const cpufreq_cooling_ops
= {
335 .get_max_state
= cpufreq_get_max_state
,
336 .get_cur_state
= cpufreq_get_cur_state
,
337 .set_cur_state
= cpufreq_set_cur_state
,
340 /* Notifier for cpufreq policy change */
341 static struct notifier_block thermal_cpufreq_notifier_block
= {
342 .notifier_call
= cpufreq_thermal_notifier
,
346 * cpufreq_cooling_register - function to create cpufreq cooling device.
347 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
349 struct thermal_cooling_device
*cpufreq_cooling_register(
350 struct cpumask
*clip_cpus
)
352 struct thermal_cooling_device
*cool_dev
;
353 struct cpufreq_cooling_device
*cpufreq_dev
= NULL
;
354 unsigned int cpufreq_dev_count
= 0, min
= 0, max
= 0;
355 char dev_name
[THERMAL_NAME_LENGTH
];
357 struct cpufreq_policy policy
;
359 list_for_each_entry(cpufreq_dev
, &cooling_cpufreq_list
, node
)
362 /*Verify that all the clip cpus have same freq_min, freq_max limit*/
363 for_each_cpu(i
, clip_cpus
) {
364 /*continue if cpufreq policy not found and not return error*/
365 if (!cpufreq_get_policy(&policy
, i
))
367 if (min
== 0 && max
== 0) {
368 min
= policy
.cpuinfo
.min_freq
;
369 max
= policy
.cpuinfo
.max_freq
;
371 if (min
!= policy
.cpuinfo
.min_freq
||
372 max
!= policy
.cpuinfo
.max_freq
)
373 return ERR_PTR(-EINVAL
);
376 cpufreq_dev
= kzalloc(sizeof(struct cpufreq_cooling_device
),
379 return ERR_PTR(-ENOMEM
);
381 cpumask_copy(&cpufreq_dev
->allowed_cpus
, clip_cpus
);
383 if (cpufreq_dev_count
== 0)
384 mutex_init(&cooling_cpufreq_lock
);
386 ret
= get_idr(&cpufreq_idr
, &cpufreq_dev
->id
);
389 return ERR_PTR(-EINVAL
);
392 sprintf(dev_name
, "thermal-cpufreq-%d", cpufreq_dev
->id
);
394 cool_dev
= thermal_cooling_device_register(dev_name
, cpufreq_dev
,
395 &cpufreq_cooling_ops
);
397 release_idr(&cpufreq_idr
, cpufreq_dev
->id
);
399 return ERR_PTR(-EINVAL
);
401 cpufreq_dev
->cool_dev
= cool_dev
;
402 cpufreq_dev
->cpufreq_state
= 0;
403 mutex_lock(&cooling_cpufreq_lock
);
404 list_add_tail(&cpufreq_dev
->node
, &cooling_cpufreq_list
);
406 /* Register the notifier for first cpufreq cooling device */
407 if (cpufreq_dev_count
== 0)
408 cpufreq_register_notifier(&thermal_cpufreq_notifier_block
,
409 CPUFREQ_POLICY_NOTIFIER
);
411 mutex_unlock(&cooling_cpufreq_lock
);
414 EXPORT_SYMBOL(cpufreq_cooling_register
);
417 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
418 * @cdev: thermal cooling device pointer.
420 void cpufreq_cooling_unregister(struct thermal_cooling_device
*cdev
)
422 struct cpufreq_cooling_device
*cpufreq_dev
= NULL
;
423 unsigned int cpufreq_dev_count
= 0;
425 mutex_lock(&cooling_cpufreq_lock
);
426 list_for_each_entry(cpufreq_dev
, &cooling_cpufreq_list
, node
) {
427 if (cpufreq_dev
&& cpufreq_dev
->cool_dev
== cdev
)
432 if (!cpufreq_dev
|| cpufreq_dev
->cool_dev
!= cdev
) {
433 mutex_unlock(&cooling_cpufreq_lock
);
437 list_del(&cpufreq_dev
->node
);
439 /* Unregister the notifier for the last cpufreq cooling device */
440 if (cpufreq_dev_count
== 1) {
441 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block
,
442 CPUFREQ_POLICY_NOTIFIER
);
444 mutex_unlock(&cooling_cpufreq_lock
);
445 thermal_cooling_device_unregister(cpufreq_dev
->cool_dev
);
446 release_idr(&cpufreq_idr
, cpufreq_dev
->id
);
447 if (cpufreq_dev_count
== 1)
448 mutex_destroy(&cooling_cpufreq_lock
);
451 EXPORT_SYMBOL(cpufreq_cooling_unregister
);