]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/thermal/cpu_cooling.c
thermal: cpu_cooling: Store frequencies in descending order
[mirror_ubuntu-artful-kernel.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
07d888d8
VK
31/*
32 * Cooling state <-> CPUFreq frequency
33 *
34 * Cooling states are translated to frequencies throughout this driver and this
35 * is the relation between them.
36 *
37 * Highest cooling state corresponds to lowest possible frequency.
38 *
39 * i.e.
40 * level 0 --> 1st Max Freq
41 * level 1 --> 2nd Max Freq
42 * ...
43 */
44
02361418 45/**
3b3c0748 46 * struct cpufreq_cooling_device - data for cooling device with cpufreq
02361418
ADK
47 * @id: unique integer value corresponding to each cpufreq_cooling_device
48 * registered.
3b3c0748
EV
49 * @cool_dev: thermal_cooling_device pointer to keep track of the
50 * registered cooling device.
02361418
ADK
51 * @cpufreq_state: integer value representing the current state of cpufreq
52 * cooling devices.
53 * @cpufreq_val: integer value representing the absolute value of the clipped
54 * frequency.
dcc6c7fd
VK
55 * @max_level: maximum cooling level. One less than total number of valid
56 * cpufreq frequencies.
02361418 57 * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
02361418 58 *
beca6053
VK
59 * This structure is required for keeping information of each registered
60 * cpufreq_cooling_device.
02361418
ADK
61 */
62struct cpufreq_cooling_device {
63 int id;
64 struct thermal_cooling_device *cool_dev;
65 unsigned int cpufreq_state;
66 unsigned int cpufreq_val;
dcc6c7fd 67 unsigned int max_level;
f6859014 68 unsigned int *freq_table; /* In descending order */
02361418 69 struct cpumask allowed_cpus;
2dcd851f 70 struct list_head node;
02361418 71};
02361418 72static DEFINE_IDR(cpufreq_idr);
160b7d80 73static DEFINE_MUTEX(cooling_cpufreq_lock);
02361418 74
2dcd851f 75static LIST_HEAD(cpufreq_dev_list);
02361418
ADK
76
77/**
78 * get_idr - function to get a unique id.
79 * @idr: struct idr * handle used to create a id.
80 * @id: int * value generated by this function.
79491e53
EV
81 *
82 * This function will populate @id with an unique
83 * id, using the idr API.
84 *
85 * Return: 0 on success, an error code on failure.
02361418
ADK
86 */
87static int get_idr(struct idr *idr, int *id)
88{
6deb69fa 89 int ret;
02361418
ADK
90
91 mutex_lock(&cooling_cpufreq_lock);
6deb69fa 92 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
02361418 93 mutex_unlock(&cooling_cpufreq_lock);
6deb69fa
TH
94 if (unlikely(ret < 0))
95 return ret;
96 *id = ret;
79491e53 97
02361418
ADK
98 return 0;
99}
100
101/**
102 * release_idr - function to free the unique id.
103 * @idr: struct idr * handle used for creating the id.
104 * @id: int value representing the unique id.
105 */
106static void release_idr(struct idr *idr, int id)
107{
108 mutex_lock(&cooling_cpufreq_lock);
109 idr_remove(idr, id);
110 mutex_unlock(&cooling_cpufreq_lock);
111}
112
113/* Below code defines functions to be used for cpufreq as cooling device */
114
fc35b35c
ZR
115enum cpufreq_cooling_property {
116 GET_LEVEL,
117 GET_FREQ,
fc35b35c
ZR
118};
119
2d6f28fe 120/**
728c03c9 121 * get_property - fetch a property of interest for a given cpu.
b9f8b416 122 * @cpufreq_dev: cpufreq_dev for which the property is required
2d6f28fe
EV
123 * @input: query parameter
124 * @output: query return
97afa4aa 125 * @property: type of query (frequency, level)
2d6f28fe
EV
126 *
127 * This is the common function to
97afa4aa
VK
128 * 1. translate frequency to cooling state
129 * 2. translate cooling state to frequency
728c03c9 130 *
fc35b35c
ZR
131 * Note that the code may be not in good shape
132 * but it is written in this way in order to:
133 * a) reduce duplicate code as most of the code can be shared.
134 * b) make sure the logic is consistent when translating between
135 * cooling states and frequencies.
2d6f28fe
EV
136 *
137 * Return: 0 on success, -EINVAL when invalid parameters are passed.
138 */
b9f8b416
VK
139static int get_property(struct cpufreq_cooling_device *cpufreq_dev,
140 unsigned long input, unsigned int *output,
d8892a01 141 enum cpufreq_cooling_property property)
02361418 142{
3c84ef3a 143 int i;
b9f8b416 144 unsigned long level = 0;
fc35b35c
ZR
145 unsigned int freq = CPUFREQ_ENTRY_INVALID;
146 int descend = -1;
b9f8b416 147 struct cpufreq_frequency_table *pos, *table;
79d26401 148
fc35b35c
ZR
149 if (!output)
150 return -EINVAL;
151
b9f8b416 152 table = cpufreq_frequency_get_table(cpumask_first(&cpufreq_dev->allowed_cpus));
02361418 153 if (!table)
fc35b35c 154 return -EINVAL;
02361418 155
3c84ef3a 156 cpufreq_for_each_valid_entry(pos, table) {
fc35b35c 157 /* ignore duplicate entry */
3c84ef3a 158 if (freq == pos->frequency)
fc35b35c
ZR
159 continue;
160
161 /* get the frequency order */
24c7a381 162 if (freq != CPUFREQ_ENTRY_INVALID && descend == -1)
3c84ef3a 163 descend = freq > pos->frequency;
02361418 164
3c84ef3a 165 freq = pos->frequency;
02361418 166 }
a116776f 167
fc35b35c 168 if (property == GET_FREQ)
b9f8b416 169 level = descend ? input : (cpufreq_dev->max_level - input);
fc35b35c 170
3c84ef3a
SK
171 i = 0;
172 cpufreq_for_each_valid_entry(pos, table) {
fc35b35c 173 /* ignore duplicate entry */
3c84ef3a 174 if (freq == pos->frequency)
fc35b35c
ZR
175 continue;
176
177 /* now we have a valid frequency entry */
3c84ef3a 178 freq = pos->frequency;
fc35b35c
ZR
179
180 if (property == GET_LEVEL && (unsigned int)input == freq) {
181 /* get level by frequency */
b9f8b416 182 *output = descend ? i : (cpufreq_dev->max_level - i);
fc35b35c
ZR
183 return 0;
184 }
3c84ef3a 185 if (property == GET_FREQ && level == i) {
fc35b35c
ZR
186 /* get frequency by level */
187 *output = freq;
188 return 0;
189 }
3c84ef3a 190 i++;
02361418 191 }
79491e53 192
fc35b35c
ZR
193 return -EINVAL;
194}
195
44952d33 196/**
728c03c9 197 * cpufreq_cooling_get_level - for a given cpu, return the cooling level.
44952d33
EV
198 * @cpu: cpu for which the level is required
199 * @freq: the frequency of interest
200 *
201 * This function will match the cooling level corresponding to the
202 * requested @freq and return it.
203 *
204 * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID
205 * otherwise.
206 */
57df8106
ZR
207unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq)
208{
b9f8b416 209 struct cpufreq_cooling_device *cpufreq_dev;
57df8106 210
b9f8b416
VK
211 mutex_lock(&cooling_cpufreq_lock);
212 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
213 if (cpumask_test_cpu(cpu, &cpufreq_dev->allowed_cpus)) {
214 unsigned int val;
215
216 mutex_unlock(&cooling_cpufreq_lock);
217 if (get_property(cpufreq_dev, (unsigned long)freq, &val,
218 GET_LEVEL))
219 return THERMAL_CSTATE_INVALID;
220
221 return (unsigned long)val;
222 }
223 }
224 mutex_unlock(&cooling_cpufreq_lock);
79491e53 225
b9f8b416
VK
226 pr_err("%s: cpu:%d not part of any cooling device\n", __func__, cpu);
227 return THERMAL_CSTATE_INVALID;
57df8106 228}
243dbd9c 229EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level);
57df8106 230
02361418
ADK
231/**
232 * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
233 * @nb: struct notifier_block * with callback info.
234 * @event: value showing cpufreq event for which this function invoked.
235 * @data: callback-specific data
bab30554 236 *
9746b6e7 237 * Callback to hijack the notification on cpufreq policy transition.
bab30554
EV
238 * Every time there is a change in policy, we will intercept and
239 * update the cpufreq policy with thermal constraints.
240 *
241 * Return: 0 (success)
02361418
ADK
242 */
243static int cpufreq_thermal_notifier(struct notifier_block *nb,
5fda7f68 244 unsigned long event, void *data)
02361418
ADK
245{
246 struct cpufreq_policy *policy = data;
247 unsigned long max_freq = 0;
2dcd851f 248 struct cpufreq_cooling_device *cpufreq_dev;
02361418 249
2dcd851f 250 if (event != CPUFREQ_ADJUST)
02361418
ADK
251 return 0;
252
2dcd851f
YSB
253 mutex_lock(&cooling_cpufreq_lock);
254 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
255 if (!cpumask_test_cpu(policy->cpu,
256 &cpufreq_dev->allowed_cpus))
257 continue;
258
2dcd851f 259 max_freq = cpufreq_dev->cpufreq_val;
02361418 260
2dcd851f
YSB
261 if (policy->max != max_freq)
262 cpufreq_verify_within_limits(policy, 0, max_freq);
263 }
264 mutex_unlock(&cooling_cpufreq_lock);
02361418
ADK
265
266 return 0;
267}
268
1b9e3526 269/* cpufreq cooling device callback functions are defined below */
02361418
ADK
270
271/**
272 * cpufreq_get_max_state - callback function to get the max cooling state.
273 * @cdev: thermal cooling device pointer.
274 * @state: fill this variable with the max cooling state.
62c00421
EV
275 *
276 * Callback for the thermal cooling device to return the cpufreq
277 * max cooling state.
278 *
279 * Return: 0 on success, an error code otherwise.
02361418
ADK
280 */
281static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
282 unsigned long *state)
283{
160b7d80 284 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
02361418 285
dcc6c7fd
VK
286 *state = cpufreq_device->max_level;
287 return 0;
02361418
ADK
288}
289
290/**
291 * cpufreq_get_cur_state - callback function to get the current cooling state.
292 * @cdev: thermal cooling device pointer.
293 * @state: fill this variable with the current cooling state.
3672552d
EV
294 *
295 * Callback for the thermal cooling device to return the cpufreq
296 * current cooling state.
297 *
298 * Return: 0 on success, an error code otherwise.
02361418
ADK
299 */
300static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
301 unsigned long *state)
302{
160b7d80 303 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
02361418 304
160b7d80 305 *state = cpufreq_device->cpufreq_state;
79491e53 306
160b7d80 307 return 0;
02361418
ADK
308}
309
310/**
311 * cpufreq_set_cur_state - callback function to set the current cooling state.
312 * @cdev: thermal cooling device pointer.
313 * @state: set this variable to the current cooling state.
56e05fdb
EV
314 *
315 * Callback for the thermal cooling device to change the cpufreq
316 * current cooling state.
317 *
318 * Return: 0 on success, an error code otherwise.
02361418
ADK
319 */
320static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
321 unsigned long state)
322{
160b7d80 323 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
5194fe46
VK
324 unsigned int cpu = cpumask_any(&cpufreq_device->allowed_cpus);
325 unsigned int clip_freq;
521a2e58 326 int ret;
5194fe46
VK
327
328 /* Check if the old cooling action is same as new cooling action */
329 if (cpufreq_device->cpufreq_state == state)
330 return 0;
02361418 331
b9f8b416 332 ret = get_property(cpufreq_device, state, &clip_freq, GET_FREQ);
521a2e58
VK
333 if (ret)
334 return ret;
5194fe46
VK
335
336 cpufreq_device->cpufreq_state = state;
337 cpufreq_device->cpufreq_val = clip_freq;
338
339 cpufreq_update_policy(cpu);
340
341 return 0;
02361418
ADK
342}
343
344/* Bind cpufreq callbacks to thermal cooling device ops */
345static struct thermal_cooling_device_ops const cpufreq_cooling_ops = {
346 .get_max_state = cpufreq_get_max_state,
347 .get_cur_state = cpufreq_get_cur_state,
348 .set_cur_state = cpufreq_set_cur_state,
349};
350
351/* Notifier for cpufreq policy change */
352static struct notifier_block thermal_cpufreq_notifier_block = {
353 .notifier_call = cpufreq_thermal_notifier,
354};
355
f6859014
VK
356static unsigned int find_next_max(struct cpufreq_frequency_table *table,
357 unsigned int prev_max)
358{
359 struct cpufreq_frequency_table *pos;
360 unsigned int max = 0;
361
362 cpufreq_for_each_valid_entry(pos, table) {
363 if (pos->frequency > max && pos->frequency < prev_max)
364 max = pos->frequency;
365 }
366
367 return max;
368}
369
02361418 370/**
39d99cff
EV
371 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
372 * @np: a valid struct device_node to the cooling device device tree node
02361418 373 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
405fb825 374 * Normally this should be same as cpufreq policy->related_cpus.
12cb08ba
EV
375 *
376 * This interface function registers the cpufreq cooling device with the name
377 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
39d99cff
EV
378 * cooling devices. It also gives the opportunity to link the cooling device
379 * with a device tree node, in order to bind it via the thermal DT code.
12cb08ba
EV
380 *
381 * Return: a valid struct thermal_cooling_device pointer on success,
382 * on failure, it returns a corresponding ERR_PTR().
02361418 383 */
39d99cff
EV
384static struct thermal_cooling_device *
385__cpufreq_cooling_register(struct device_node *np,
386 const struct cpumask *clip_cpus)
02361418
ADK
387{
388 struct thermal_cooling_device *cool_dev;
5d3bdb89 389 struct cpufreq_cooling_device *cpufreq_dev;
02361418 390 char dev_name[THERMAL_NAME_LENGTH];
dcc6c7fd 391 struct cpufreq_frequency_table *pos, *table;
f6859014 392 unsigned int freq, i;
405fb825 393 int ret;
02361418 394
dcc6c7fd
VK
395 table = cpufreq_frequency_get_table(cpumask_first(clip_cpus));
396 if (!table) {
0f1be51c
EV
397 pr_debug("%s: CPUFreq table not found\n", __func__);
398 return ERR_PTR(-EPROBE_DEFER);
399 }
400
98d522f0 401 cpufreq_dev = kzalloc(sizeof(*cpufreq_dev), GFP_KERNEL);
02361418
ADK
402 if (!cpufreq_dev)
403 return ERR_PTR(-ENOMEM);
404
b9f8b416 405 ret = get_property(cpufreq_dev, 0, &cpufreq_dev->cpufreq_val, GET_FREQ);
521a2e58
VK
406 if (ret) {
407 pr_err("%s: Failed to get frequency: %d", __func__, ret);
408 cool_dev = ERR_PTR(ret);
7adb635b
VK
409 goto free_cdev;
410 }
411
dcc6c7fd
VK
412 /* Find max levels */
413 cpufreq_for_each_valid_entry(pos, table)
414 cpufreq_dev->max_level++;
415
f6859014
VK
416 cpufreq_dev->freq_table = kmalloc(sizeof(*cpufreq_dev->freq_table) *
417 cpufreq_dev->max_level, GFP_KERNEL);
418 if (!cpufreq_dev->freq_table) {
419 return ERR_PTR(-ENOMEM);
420 cool_dev = ERR_PTR(-ENOMEM);
421 goto free_cdev;
422 }
423
dcc6c7fd
VK
424 /* max_level is an index, not a counter */
425 cpufreq_dev->max_level--;
426
02361418
ADK
427 cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
428
02361418
ADK
429 ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
430 if (ret) {
730abe06 431 cool_dev = ERR_PTR(ret);
f6859014 432 goto free_table;
02361418
ADK
433 }
434
99871a71
EV
435 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
436 cpufreq_dev->id);
02361418 437
39d99cff
EV
438 cool_dev = thermal_of_cooling_device_register(np, dev_name, cpufreq_dev,
439 &cpufreq_cooling_ops);
730abe06
VK
440 if (IS_ERR(cool_dev))
441 goto remove_idr;
442
f6859014
VK
443 /* Fill freq-table in descending order of frequencies */
444 for (i = 0, freq = -1; i <= cpufreq_dev->max_level; i++) {
445 freq = find_next_max(table, freq);
446 cpufreq_dev->freq_table[i] = freq;
447
448 /* Warn for duplicate entries */
449 if (!freq)
450 pr_warn("%s: table has duplicate entries\n", __func__);
451 else
452 pr_debug("%s: freq:%u KHz\n", __func__, freq);
453 }
454
02361418 455 cpufreq_dev->cool_dev = cool_dev;
92e615ec 456
02361418 457 mutex_lock(&cooling_cpufreq_lock);
02361418
ADK
458
459 /* Register the notifier for first cpufreq cooling device */
2479bb64 460 if (list_empty(&cpufreq_dev_list))
02361418 461 cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
5fda7f68 462 CPUFREQ_POLICY_NOTIFIER);
2dcd851f 463 list_add(&cpufreq_dev->node, &cpufreq_dev_list);
02361418
ADK
464
465 mutex_unlock(&cooling_cpufreq_lock);
79491e53 466
730abe06
VK
467 return cool_dev;
468
469remove_idr:
470 release_idr(&cpufreq_idr, cpufreq_dev->id);
f6859014
VK
471free_table:
472 kfree(cpufreq_dev->freq_table);
730abe06
VK
473free_cdev:
474 kfree(cpufreq_dev);
475
02361418
ADK
476 return cool_dev;
477}
39d99cff
EV
478
479/**
480 * cpufreq_cooling_register - function to create cpufreq cooling device.
481 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
482 *
483 * This interface function registers the cpufreq cooling device with the name
484 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
485 * cooling devices.
486 *
487 * Return: a valid struct thermal_cooling_device pointer on success,
488 * on failure, it returns a corresponding ERR_PTR().
489 */
490struct thermal_cooling_device *
491cpufreq_cooling_register(const struct cpumask *clip_cpus)
492{
493 return __cpufreq_cooling_register(NULL, clip_cpus);
494}
243dbd9c 495EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
02361418 496
39d99cff
EV
497/**
498 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
499 * @np: a valid struct device_node to the cooling device device tree node
500 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
501 *
502 * This interface function registers the cpufreq cooling device with the name
503 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
504 * cooling devices. Using this API, the cpufreq cooling device will be
505 * linked to the device tree node provided.
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 *
511of_cpufreq_cooling_register(struct device_node *np,
512 const struct cpumask *clip_cpus)
513{
514 if (!np)
515 return ERR_PTR(-EINVAL);
516
517 return __cpufreq_cooling_register(np, clip_cpus);
518}
519EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
520
02361418
ADK
521/**
522 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
523 * @cdev: thermal cooling device pointer.
135266b4
EV
524 *
525 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
02361418
ADK
526 */
527void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
528{
50e66c7e 529 struct cpufreq_cooling_device *cpufreq_dev;
02361418 530
50e66c7e
EV
531 if (!cdev)
532 return;
533
534 cpufreq_dev = cdev->devdata;
02361418 535 mutex_lock(&cooling_cpufreq_lock);
2dcd851f 536 list_del(&cpufreq_dev->node);
02361418
ADK
537
538 /* Unregister the notifier for the last cpufreq cooling device */
2479bb64 539 if (list_empty(&cpufreq_dev_list))
02361418 540 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
5fda7f68 541 CPUFREQ_POLICY_NOTIFIER);
02361418 542 mutex_unlock(&cooling_cpufreq_lock);
160b7d80 543
02361418
ADK
544 thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
545 release_idr(&cpufreq_idr, cpufreq_dev->id);
f6859014 546 kfree(cpufreq_dev->freq_table);
02361418
ADK
547 kfree(cpufreq_dev);
548}
243dbd9c 549EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);