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