Merge tag 'staging-3.18-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[linux-2.6-block.git] / drivers / thermal / cpu_cooling.c
CommitLineData
02361418
ADK
1/*
2 * linux/drivers/thermal/cpu_cooling.c
3 *
4 * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
5 * Copyright (C) 2012 Amit Daniel <amit.kachhap@linaro.org>
6 *
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.
11 *
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.
16 *
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.
20 *
21 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22 */
02361418
ADK
23#include <linux/module.h>
24#include <linux/thermal.h>
02361418
ADK
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>
30
31/**
3b3c0748 32 * struct cpufreq_cooling_device - data for cooling device with cpufreq
02361418
ADK
33 * @id: unique integer value corresponding to each cpufreq_cooling_device
34 * registered.
3b3c0748
EV
35 * @cool_dev: thermal_cooling_device pointer to keep track of the
36 * registered cooling device.
02361418
ADK
37 * @cpufreq_state: integer value representing the current state of cpufreq
38 * cooling devices.
39 * @cpufreq_val: integer value representing the absolute value of the clipped
40 * frequency.
41 * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
02361418
ADK
42 *
43 * This structure is required for keeping information of each
67d0b2a8 44 * cpufreq_cooling_device registered. In order to prevent corruption of this a
02361418
ADK
45 * mutex lock cooling_cpufreq_lock is used.
46 */
47struct cpufreq_cooling_device {
48 int id;
49 struct thermal_cooling_device *cool_dev;
50 unsigned int cpufreq_state;
51 unsigned int cpufreq_val;
52 struct cpumask allowed_cpus;
2dcd851f 53 struct list_head node;
02361418 54};
02361418 55static DEFINE_IDR(cpufreq_idr);
160b7d80 56static DEFINE_MUTEX(cooling_cpufreq_lock);
02361418 57
160b7d80 58static unsigned int cpufreq_dev_count;
02361418 59
2dcd851f 60static LIST_HEAD(cpufreq_dev_list);
02361418
ADK
61
62/**
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.
79491e53
EV
66 *
67 * This function will populate @id with an unique
68 * id, using the idr API.
69 *
70 * Return: 0 on success, an error code on failure.
02361418
ADK
71 */
72static int get_idr(struct idr *idr, int *id)
73{
6deb69fa 74 int ret;
02361418
ADK
75
76 mutex_lock(&cooling_cpufreq_lock);
6deb69fa 77 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
02361418 78 mutex_unlock(&cooling_cpufreq_lock);
6deb69fa
TH
79 if (unlikely(ret < 0))
80 return ret;
81 *id = ret;
79491e53 82
02361418
ADK
83 return 0;
84}
85
86/**
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.
90 */
91static void release_idr(struct idr *idr, int id)
92{
93 mutex_lock(&cooling_cpufreq_lock);
94 idr_remove(idr, id);
95 mutex_unlock(&cooling_cpufreq_lock);
96}
97
98/* Below code defines functions to be used for cpufreq as cooling device */
99
100/**
82b9ee40 101 * is_cpufreq_valid - function to check frequency transitioning capability.
02361418 102 * @cpu: cpu for which check is needed.
82b9ee40
EV
103 *
104 * This function will check the current state of the system if
105 * it is capable of changing the frequency for a given @cpu.
106 *
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.
02361418
ADK
109 */
110static int is_cpufreq_valid(int cpu)
111{
112 struct cpufreq_policy policy;
79491e53 113
02361418
ADK
114 return !cpufreq_get_policy(&policy, cpu);
115}
116
fc35b35c
ZR
117enum cpufreq_cooling_property {
118 GET_LEVEL,
119 GET_FREQ,
120 GET_MAXL,
121};
122
2d6f28fe
EV
123/**
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)
129 *
130 * This is the common function to
fc35b35c
ZR
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.
2d6f28fe
EV
139 *
140 * Return: 0 on success, -EINVAL when invalid parameters are passed.
141 */
fc35b35c 142static int get_property(unsigned int cpu, unsigned long input,
d8892a01
EV
143 unsigned int *output,
144 enum cpufreq_cooling_property property)
02361418 145{
3c84ef3a 146 int i;
4469b997 147 unsigned long max_level = 0, level = 0;
fc35b35c
ZR
148 unsigned int freq = CPUFREQ_ENTRY_INVALID;
149 int descend = -1;
3c84ef3a 150 struct cpufreq_frequency_table *pos, *table =
02361418 151 cpufreq_frequency_get_table(cpu);
79d26401 152
fc35b35c
ZR
153 if (!output)
154 return -EINVAL;
155
02361418 156 if (!table)
fc35b35c 157 return -EINVAL;
02361418 158
3c84ef3a 159 cpufreq_for_each_valid_entry(pos, table) {
fc35b35c 160 /* ignore duplicate entry */
3c84ef3a 161 if (freq == pos->frequency)
fc35b35c
ZR
162 continue;
163
164 /* get the frequency order */
24c7a381 165 if (freq != CPUFREQ_ENTRY_INVALID && descend == -1)
3c84ef3a 166 descend = freq > pos->frequency;
02361418 167
3c84ef3a 168 freq = pos->frequency;
fc35b35c 169 max_level++;
02361418 170 }
a116776f
ZR
171
172 /* No valid cpu frequency entry */
173 if (max_level == 0)
174 return -EINVAL;
175
1c9573a4
EV
176 /* max_level is an index, not a counter */
177 max_level--;
02361418 178
fc35b35c
ZR
179 /* get max level */
180 if (property == GET_MAXL) {
181 *output = (unsigned int)max_level;
182 return 0;
183 }
02361418 184
fc35b35c 185 if (property == GET_FREQ)
1c9573a4 186 level = descend ? input : (max_level - input);
fc35b35c 187
3c84ef3a
SK
188 i = 0;
189 cpufreq_for_each_valid_entry(pos, table) {
fc35b35c 190 /* ignore duplicate entry */
3c84ef3a 191 if (freq == pos->frequency)
fc35b35c
ZR
192 continue;
193
194 /* now we have a valid frequency entry */
3c84ef3a 195 freq = pos->frequency;
fc35b35c
ZR
196
197 if (property == GET_LEVEL && (unsigned int)input == freq) {
198 /* get level by frequency */
3c84ef3a 199 *output = descend ? i : (max_level - i);
fc35b35c
ZR
200 return 0;
201 }
3c84ef3a 202 if (property == GET_FREQ && level == i) {
fc35b35c
ZR
203 /* get frequency by level */
204 *output = freq;
205 return 0;
206 }
3c84ef3a 207 i++;
02361418 208 }
79491e53 209
fc35b35c
ZR
210 return -EINVAL;
211}
212
44952d33
EV
213/**
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
217 *
218 * This function will match the cooling level corresponding to the
219 * requested @freq and return it.
220 *
221 * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID
222 * otherwise.
223 */
57df8106
ZR
224unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq)
225{
226 unsigned int val;
227
228 if (get_property(cpu, (unsigned long)freq, &val, GET_LEVEL))
229 return THERMAL_CSTATE_INVALID;
79491e53 230
57df8106
ZR
231 return (unsigned long)val;
232}
243dbd9c 233EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level);
57df8106 234
fc35b35c
ZR
235/**
236 * get_cpu_frequency - get the absolute value of frequency from level.
237 * @cpu: cpu for which frequency is fetched.
41518c41
EV
238 * @level: cooling level
239 *
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.
fc35b35c 243 * e.g level=0 --> 1st MAX FREQ, level=1 ---> 2nd MAX FREQ, .... etc
41518c41
EV
244 *
245 * Return: 0 on error, the corresponding frequency otherwise.
fc35b35c
ZR
246 */
247static unsigned int get_cpu_frequency(unsigned int cpu, unsigned long level)
248{
249 int ret = 0;
250 unsigned int freq;
251
252 ret = get_property(cpu, level, &freq, GET_FREQ);
253 if (ret)
254 return 0;
79491e53 255
fc35b35c 256 return freq;
02361418
ADK
257}
258
259/**
260 * cpufreq_apply_cooling - function to apply frequency clipping.
261 * @cpufreq_device: cpufreq_cooling_device pointer containing frequency
262 * clipping data.
263 * @cooling_state: value of the cooling state.
4b33deb5
EV
264 *
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.
267 *
268 * Return: 0 on success, an error code otherwise (-EINVAL in case wrong
269 * cooling state).
02361418
ADK
270 */
271static int cpufreq_apply_cooling(struct cpufreq_cooling_device *cpufreq_device,
5fda7f68 272 unsigned long cooling_state)
02361418
ADK
273{
274 unsigned int cpuid, clip_freq;
bde00663
LNM
275 struct cpumask *mask = &cpufreq_device->allowed_cpus;
276 unsigned int cpu = cpumask_any(mask);
02361418
ADK
277
278
279 /* Check if the old cooling action is same as new cooling action */
280 if (cpufreq_device->cpufreq_state == cooling_state)
281 return 0;
282
283 clip_freq = get_cpu_frequency(cpu, cooling_state);
284 if (!clip_freq)
285 return -EINVAL;
286
287 cpufreq_device->cpufreq_state = cooling_state;
288 cpufreq_device->cpufreq_val = clip_freq;
02361418 289
bde00663 290 for_each_cpu(cpuid, mask) {
02361418
ADK
291 if (is_cpufreq_valid(cpuid))
292 cpufreq_update_policy(cpuid);
293 }
294
02361418
ADK
295 return 0;
296}
297
298/**
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
bab30554 303 *
9746b6e7 304 * Callback to hijack the notification on cpufreq policy transition.
bab30554
EV
305 * Every time there is a change in policy, we will intercept and
306 * update the cpufreq policy with thermal constraints.
307 *
308 * Return: 0 (success)
02361418
ADK
309 */
310static int cpufreq_thermal_notifier(struct notifier_block *nb,
5fda7f68 311 unsigned long event, void *data)
02361418
ADK
312{
313 struct cpufreq_policy *policy = data;
314 unsigned long max_freq = 0;
2dcd851f 315 struct cpufreq_cooling_device *cpufreq_dev;
02361418 316
2dcd851f 317 if (event != CPUFREQ_ADJUST)
02361418
ADK
318 return 0;
319
2dcd851f
YSB
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))
324 continue;
325
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);
02361418 330
2dcd851f 331 max_freq = cpufreq_dev->cpufreq_val;
02361418 332
2dcd851f
YSB
333 if (policy->max != max_freq)
334 cpufreq_verify_within_limits(policy, 0, max_freq);
335 }
336 mutex_unlock(&cooling_cpufreq_lock);
02361418
ADK
337
338 return 0;
339}
340
1b9e3526 341/* cpufreq cooling device callback functions are defined below */
02361418
ADK
342
343/**
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.
62c00421
EV
347 *
348 * Callback for the thermal cooling device to return the cpufreq
349 * max cooling state.
350 *
351 * Return: 0 on success, an error code otherwise.
02361418
ADK
352 */
353static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
354 unsigned long *state)
355{
160b7d80 356 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
bde00663 357 struct cpumask *mask = &cpufreq_device->allowed_cpus;
02361418 358 unsigned int cpu;
4f89038f 359 unsigned int count = 0;
fc35b35c 360 int ret;
02361418 361
bde00663 362 cpu = cpumask_any(mask);
02361418 363
4f89038f 364 ret = get_property(cpu, 0, &count, GET_MAXL);
9c51b05a 365
fc35b35c
ZR
366 if (count > 0)
367 *state = count;
02361418 368
fc35b35c 369 return ret;
02361418
ADK
370}
371
372/**
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.
3672552d
EV
376 *
377 * Callback for the thermal cooling device to return the cpufreq
378 * current cooling state.
379 *
380 * Return: 0 on success, an error code otherwise.
02361418
ADK
381 */
382static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
383 unsigned long *state)
384{
160b7d80 385 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
02361418 386
160b7d80 387 *state = cpufreq_device->cpufreq_state;
79491e53 388
160b7d80 389 return 0;
02361418
ADK
390}
391
392/**
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.
56e05fdb
EV
396 *
397 * Callback for the thermal cooling device to change the cpufreq
398 * current cooling state.
399 *
400 * Return: 0 on success, an error code otherwise.
02361418
ADK
401 */
402static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
403 unsigned long state)
404{
160b7d80 405 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
02361418 406
160b7d80 407 return cpufreq_apply_cooling(cpufreq_device, state);
02361418
ADK
408}
409
410/* Bind cpufreq callbacks to thermal cooling device ops */
411static 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,
415};
416
417/* Notifier for cpufreq policy change */
418static struct notifier_block thermal_cpufreq_notifier_block = {
419 .notifier_call = cpufreq_thermal_notifier,
420};
421
422/**
39d99cff
EV
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
02361418 425 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
12cb08ba
EV
426 *
427 * This interface function registers the cpufreq cooling device with the name
428 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
39d99cff
EV
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.
12cb08ba
EV
431 *
432 * Return: a valid struct thermal_cooling_device pointer on success,
433 * on failure, it returns a corresponding ERR_PTR().
02361418 434 */
39d99cff
EV
435static struct thermal_cooling_device *
436__cpufreq_cooling_register(struct device_node *np,
437 const struct cpumask *clip_cpus)
02361418
ADK
438{
439 struct thermal_cooling_device *cool_dev;
440 struct cpufreq_cooling_device *cpufreq_dev = NULL;
160b7d80 441 unsigned int min = 0, max = 0;
02361418 442 char dev_name[THERMAL_NAME_LENGTH];
a4b6fec9 443 int ret = 0, i;
02361418
ADK
444 struct cpufreq_policy policy;
445
1b9e3526 446 /* Verify that all the clip cpus have same freq_min, freq_max limit */
02361418 447 for_each_cpu(i, clip_cpus) {
1b9e3526 448 /* continue if cpufreq policy not found and not return error */
02361418
ADK
449 if (!cpufreq_get_policy(&policy, i))
450 continue;
451 if (min == 0 && max == 0) {
452 min = policy.cpuinfo.min_freq;
453 max = policy.cpuinfo.max_freq;
454 } else {
455 if (min != policy.cpuinfo.min_freq ||
5fda7f68 456 max != policy.cpuinfo.max_freq)
02361418 457 return ERR_PTR(-EINVAL);
6b6519df 458 }
02361418
ADK
459 }
460 cpufreq_dev = kzalloc(sizeof(struct cpufreq_cooling_device),
5fda7f68 461 GFP_KERNEL);
02361418
ADK
462 if (!cpufreq_dev)
463 return ERR_PTR(-ENOMEM);
464
465 cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
466
02361418
ADK
467 ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
468 if (ret) {
469 kfree(cpufreq_dev);
470 return ERR_PTR(-EINVAL);
471 }
472
99871a71
EV
473 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
474 cpufreq_dev->id);
02361418 475
39d99cff
EV
476 cool_dev = thermal_of_cooling_device_register(np, dev_name, cpufreq_dev,
477 &cpufreq_cooling_ops);
73b9bcd7 478 if (IS_ERR(cool_dev)) {
02361418
ADK
479 release_idr(&cpufreq_idr, cpufreq_dev->id);
480 kfree(cpufreq_dev);
73b9bcd7 481 return cool_dev;
02361418 482 }
02361418
ADK
483 cpufreq_dev->cool_dev = cool_dev;
484 cpufreq_dev->cpufreq_state = 0;
485 mutex_lock(&cooling_cpufreq_lock);
02361418
ADK
486
487 /* Register the notifier for first cpufreq cooling device */
488 if (cpufreq_dev_count == 0)
489 cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
5fda7f68 490 CPUFREQ_POLICY_NOTIFIER);
160b7d80 491 cpufreq_dev_count++;
2dcd851f 492 list_add(&cpufreq_dev->node, &cpufreq_dev_list);
02361418
ADK
493
494 mutex_unlock(&cooling_cpufreq_lock);
79491e53 495
02361418
ADK
496 return cool_dev;
497}
39d99cff
EV
498
499/**
500 * cpufreq_cooling_register - function to create cpufreq cooling device.
501 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
502 *
503 * This interface function registers the cpufreq cooling device with the name
504 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
505 * cooling devices.
506 *
507 * Return: a valid struct thermal_cooling_device pointer on success,
508 * on failure, it returns a corresponding ERR_PTR().
509 */
510struct thermal_cooling_device *
511cpufreq_cooling_register(const struct cpumask *clip_cpus)
512{
513 return __cpufreq_cooling_register(NULL, clip_cpus);
514}
243dbd9c 515EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
02361418 516
39d99cff
EV
517/**
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.
521 *
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.
526 *
527 * Return: a valid struct thermal_cooling_device pointer on success,
528 * on failure, it returns a corresponding ERR_PTR().
529 */
530struct thermal_cooling_device *
531of_cpufreq_cooling_register(struct device_node *np,
532 const struct cpumask *clip_cpus)
533{
534 if (!np)
535 return ERR_PTR(-EINVAL);
536
537 return __cpufreq_cooling_register(np, clip_cpus);
538}
539EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
540
02361418
ADK
541/**
542 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
543 * @cdev: thermal cooling device pointer.
135266b4
EV
544 *
545 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
02361418
ADK
546 */
547void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
548{
50e66c7e 549 struct cpufreq_cooling_device *cpufreq_dev;
02361418 550
50e66c7e
EV
551 if (!cdev)
552 return;
553
554 cpufreq_dev = cdev->devdata;
02361418 555 mutex_lock(&cooling_cpufreq_lock);
2dcd851f 556 list_del(&cpufreq_dev->node);
160b7d80 557 cpufreq_dev_count--;
02361418
ADK
558
559 /* Unregister the notifier for the last cpufreq cooling device */
2d9bf71c 560 if (cpufreq_dev_count == 0)
02361418 561 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
5fda7f68 562 CPUFREQ_POLICY_NOTIFIER);
02361418 563 mutex_unlock(&cooling_cpufreq_lock);
160b7d80 564
02361418
ADK
565 thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
566 release_idr(&cpufreq_idr, cpufreq_dev->id);
02361418
ADK
567 kfree(cpufreq_dev);
568}
243dbd9c 569EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);