thermal: cpu_cooling: Use cpufreq_dev->freq_table for finding level/freq
[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;
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
2d6f28fe 115/**
4843c4a1 116 * get_level: Find the level for a particular frequency
b9f8b416 117 * @cpufreq_dev: cpufreq_dev for which the property is required
4843c4a1 118 * @freq: Frequency
728c03c9 119 *
4843c4a1 120 * Return: level on success, THERMAL_CSTATE_INVALID on error.
2d6f28fe 121 */
4843c4a1
VK
122static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_dev,
123 unsigned int freq)
02361418 124{
4843c4a1 125 unsigned long level;
fc35b35c 126
4843c4a1
VK
127 for (level = 0; level <= cpufreq_dev->max_level; level++) {
128 if (freq == cpufreq_dev->freq_table[level])
129 return level;
fc35b35c 130
4843c4a1
VK
131 if (freq > cpufreq_dev->freq_table[level])
132 break;
02361418 133 }
79491e53 134
4843c4a1 135 return THERMAL_CSTATE_INVALID;
fc35b35c
ZR
136}
137
44952d33 138/**
728c03c9 139 * cpufreq_cooling_get_level - for a given cpu, return the cooling level.
44952d33
EV
140 * @cpu: cpu for which the level is required
141 * @freq: the frequency of interest
142 *
143 * This function will match the cooling level corresponding to the
144 * requested @freq and return it.
145 *
146 * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID
147 * otherwise.
148 */
57df8106
ZR
149unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq)
150{
b9f8b416 151 struct cpufreq_cooling_device *cpufreq_dev;
57df8106 152
b9f8b416
VK
153 mutex_lock(&cooling_cpufreq_lock);
154 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
155 if (cpumask_test_cpu(cpu, &cpufreq_dev->allowed_cpus)) {
b9f8b416 156 mutex_unlock(&cooling_cpufreq_lock);
4843c4a1 157 return get_level(cpufreq_dev, freq);
b9f8b416
VK
158 }
159 }
160 mutex_unlock(&cooling_cpufreq_lock);
79491e53 161
b9f8b416
VK
162 pr_err("%s: cpu:%d not part of any cooling device\n", __func__, cpu);
163 return THERMAL_CSTATE_INVALID;
57df8106 164}
243dbd9c 165EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level);
57df8106 166
02361418
ADK
167/**
168 * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
169 * @nb: struct notifier_block * with callback info.
170 * @event: value showing cpufreq event for which this function invoked.
171 * @data: callback-specific data
bab30554 172 *
9746b6e7 173 * Callback to hijack the notification on cpufreq policy transition.
bab30554
EV
174 * Every time there is a change in policy, we will intercept and
175 * update the cpufreq policy with thermal constraints.
176 *
177 * Return: 0 (success)
02361418
ADK
178 */
179static int cpufreq_thermal_notifier(struct notifier_block *nb,
5fda7f68 180 unsigned long event, void *data)
02361418
ADK
181{
182 struct cpufreq_policy *policy = data;
183 unsigned long max_freq = 0;
2dcd851f 184 struct cpufreq_cooling_device *cpufreq_dev;
02361418 185
2dcd851f 186 if (event != CPUFREQ_ADJUST)
02361418
ADK
187 return 0;
188
2dcd851f
YSB
189 mutex_lock(&cooling_cpufreq_lock);
190 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
191 if (!cpumask_test_cpu(policy->cpu,
192 &cpufreq_dev->allowed_cpus))
193 continue;
194
2dcd851f 195 max_freq = cpufreq_dev->cpufreq_val;
02361418 196
2dcd851f
YSB
197 if (policy->max != max_freq)
198 cpufreq_verify_within_limits(policy, 0, max_freq);
199 }
200 mutex_unlock(&cooling_cpufreq_lock);
02361418
ADK
201
202 return 0;
203}
204
1b9e3526 205/* cpufreq cooling device callback functions are defined below */
02361418
ADK
206
207/**
208 * cpufreq_get_max_state - callback function to get the max cooling state.
209 * @cdev: thermal cooling device pointer.
210 * @state: fill this variable with the max cooling state.
62c00421
EV
211 *
212 * Callback for the thermal cooling device to return the cpufreq
213 * max cooling state.
214 *
215 * Return: 0 on success, an error code otherwise.
02361418
ADK
216 */
217static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
218 unsigned long *state)
219{
160b7d80 220 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
02361418 221
dcc6c7fd
VK
222 *state = cpufreq_device->max_level;
223 return 0;
02361418
ADK
224}
225
226/**
227 * cpufreq_get_cur_state - callback function to get the current cooling state.
228 * @cdev: thermal cooling device pointer.
229 * @state: fill this variable with the current cooling state.
3672552d
EV
230 *
231 * Callback for the thermal cooling device to return the cpufreq
232 * current cooling state.
233 *
234 * Return: 0 on success, an error code otherwise.
02361418
ADK
235 */
236static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
237 unsigned long *state)
238{
160b7d80 239 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
02361418 240
160b7d80 241 *state = cpufreq_device->cpufreq_state;
79491e53 242
160b7d80 243 return 0;
02361418
ADK
244}
245
246/**
247 * cpufreq_set_cur_state - callback function to set the current cooling state.
248 * @cdev: thermal cooling device pointer.
249 * @state: set this variable to the current cooling state.
56e05fdb
EV
250 *
251 * Callback for the thermal cooling device to change the cpufreq
252 * current cooling state.
253 *
254 * Return: 0 on success, an error code otherwise.
02361418
ADK
255 */
256static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
257 unsigned long state)
258{
160b7d80 259 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
5194fe46
VK
260 unsigned int cpu = cpumask_any(&cpufreq_device->allowed_cpus);
261 unsigned int clip_freq;
4843c4a1
VK
262
263 /* Request state should be less than max_level */
264 if (WARN_ON(state > cpufreq_device->max_level))
265 return -EINVAL;
5194fe46
VK
266
267 /* Check if the old cooling action is same as new cooling action */
268 if (cpufreq_device->cpufreq_state == state)
269 return 0;
02361418 270
4843c4a1 271 clip_freq = cpufreq_device->freq_table[state];
5194fe46
VK
272 cpufreq_device->cpufreq_state = state;
273 cpufreq_device->cpufreq_val = clip_freq;
274
275 cpufreq_update_policy(cpu);
276
277 return 0;
02361418
ADK
278}
279
280/* Bind cpufreq callbacks to thermal cooling device ops */
281static struct thermal_cooling_device_ops const cpufreq_cooling_ops = {
282 .get_max_state = cpufreq_get_max_state,
283 .get_cur_state = cpufreq_get_cur_state,
284 .set_cur_state = cpufreq_set_cur_state,
285};
286
287/* Notifier for cpufreq policy change */
288static struct notifier_block thermal_cpufreq_notifier_block = {
289 .notifier_call = cpufreq_thermal_notifier,
290};
291
f6859014
VK
292static unsigned int find_next_max(struct cpufreq_frequency_table *table,
293 unsigned int prev_max)
294{
295 struct cpufreq_frequency_table *pos;
296 unsigned int max = 0;
297
298 cpufreq_for_each_valid_entry(pos, table) {
299 if (pos->frequency > max && pos->frequency < prev_max)
300 max = pos->frequency;
301 }
302
303 return max;
304}
305
02361418 306/**
39d99cff
EV
307 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
308 * @np: a valid struct device_node to the cooling device device tree node
02361418 309 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
405fb825 310 * Normally this should be same as cpufreq policy->related_cpus.
12cb08ba
EV
311 *
312 * This interface function registers the cpufreq cooling device with the name
313 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
39d99cff
EV
314 * cooling devices. It also gives the opportunity to link the cooling device
315 * with a device tree node, in order to bind it via the thermal DT code.
12cb08ba
EV
316 *
317 * Return: a valid struct thermal_cooling_device pointer on success,
318 * on failure, it returns a corresponding ERR_PTR().
02361418 319 */
39d99cff
EV
320static struct thermal_cooling_device *
321__cpufreq_cooling_register(struct device_node *np,
322 const struct cpumask *clip_cpus)
02361418
ADK
323{
324 struct thermal_cooling_device *cool_dev;
5d3bdb89 325 struct cpufreq_cooling_device *cpufreq_dev;
02361418 326 char dev_name[THERMAL_NAME_LENGTH];
dcc6c7fd 327 struct cpufreq_frequency_table *pos, *table;
f6859014 328 unsigned int freq, i;
405fb825 329 int ret;
02361418 330
dcc6c7fd
VK
331 table = cpufreq_frequency_get_table(cpumask_first(clip_cpus));
332 if (!table) {
0f1be51c
EV
333 pr_debug("%s: CPUFreq table not found\n", __func__);
334 return ERR_PTR(-EPROBE_DEFER);
335 }
336
98d522f0 337 cpufreq_dev = kzalloc(sizeof(*cpufreq_dev), GFP_KERNEL);
02361418
ADK
338 if (!cpufreq_dev)
339 return ERR_PTR(-ENOMEM);
340
dcc6c7fd
VK
341 /* Find max levels */
342 cpufreq_for_each_valid_entry(pos, table)
343 cpufreq_dev->max_level++;
344
f6859014
VK
345 cpufreq_dev->freq_table = kmalloc(sizeof(*cpufreq_dev->freq_table) *
346 cpufreq_dev->max_level, GFP_KERNEL);
347 if (!cpufreq_dev->freq_table) {
348 return ERR_PTR(-ENOMEM);
349 cool_dev = ERR_PTR(-ENOMEM);
350 goto free_cdev;
351 }
352
dcc6c7fd
VK
353 /* max_level is an index, not a counter */
354 cpufreq_dev->max_level--;
355
02361418
ADK
356 cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
357
02361418
ADK
358 ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
359 if (ret) {
730abe06 360 cool_dev = ERR_PTR(ret);
f6859014 361 goto free_table;
02361418
ADK
362 }
363
99871a71
EV
364 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
365 cpufreq_dev->id);
02361418 366
39d99cff
EV
367 cool_dev = thermal_of_cooling_device_register(np, dev_name, cpufreq_dev,
368 &cpufreq_cooling_ops);
730abe06
VK
369 if (IS_ERR(cool_dev))
370 goto remove_idr;
371
f6859014
VK
372 /* Fill freq-table in descending order of frequencies */
373 for (i = 0, freq = -1; i <= cpufreq_dev->max_level; i++) {
374 freq = find_next_max(table, freq);
375 cpufreq_dev->freq_table[i] = freq;
376
377 /* Warn for duplicate entries */
378 if (!freq)
379 pr_warn("%s: table has duplicate entries\n", __func__);
380 else
381 pr_debug("%s: freq:%u KHz\n", __func__, freq);
382 }
383
4843c4a1 384 cpufreq_dev->cpufreq_val = cpufreq_dev->freq_table[0];
02361418 385 cpufreq_dev->cool_dev = cool_dev;
92e615ec 386
02361418 387 mutex_lock(&cooling_cpufreq_lock);
02361418
ADK
388
389 /* Register the notifier for first cpufreq cooling device */
2479bb64 390 if (list_empty(&cpufreq_dev_list))
02361418 391 cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
5fda7f68 392 CPUFREQ_POLICY_NOTIFIER);
2dcd851f 393 list_add(&cpufreq_dev->node, &cpufreq_dev_list);
02361418
ADK
394
395 mutex_unlock(&cooling_cpufreq_lock);
79491e53 396
730abe06
VK
397 return cool_dev;
398
399remove_idr:
400 release_idr(&cpufreq_idr, cpufreq_dev->id);
f6859014
VK
401free_table:
402 kfree(cpufreq_dev->freq_table);
730abe06
VK
403free_cdev:
404 kfree(cpufreq_dev);
405
02361418
ADK
406 return cool_dev;
407}
39d99cff
EV
408
409/**
410 * cpufreq_cooling_register - function to create cpufreq cooling device.
411 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
412 *
413 * This interface function registers the cpufreq cooling device with the name
414 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
415 * cooling devices.
416 *
417 * Return: a valid struct thermal_cooling_device pointer on success,
418 * on failure, it returns a corresponding ERR_PTR().
419 */
420struct thermal_cooling_device *
421cpufreq_cooling_register(const struct cpumask *clip_cpus)
422{
423 return __cpufreq_cooling_register(NULL, clip_cpus);
424}
243dbd9c 425EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
02361418 426
39d99cff
EV
427/**
428 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
429 * @np: a valid struct device_node to the cooling device device tree node
430 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
431 *
432 * This interface function registers the cpufreq cooling device with the name
433 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
434 * cooling devices. Using this API, the cpufreq cooling device will be
435 * linked to the device tree node provided.
436 *
437 * Return: a valid struct thermal_cooling_device pointer on success,
438 * on failure, it returns a corresponding ERR_PTR().
439 */
440struct thermal_cooling_device *
441of_cpufreq_cooling_register(struct device_node *np,
442 const struct cpumask *clip_cpus)
443{
444 if (!np)
445 return ERR_PTR(-EINVAL);
446
447 return __cpufreq_cooling_register(np, clip_cpus);
448}
449EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
450
02361418
ADK
451/**
452 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
453 * @cdev: thermal cooling device pointer.
135266b4
EV
454 *
455 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
02361418
ADK
456 */
457void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
458{
50e66c7e 459 struct cpufreq_cooling_device *cpufreq_dev;
02361418 460
50e66c7e
EV
461 if (!cdev)
462 return;
463
464 cpufreq_dev = cdev->devdata;
02361418 465 mutex_lock(&cooling_cpufreq_lock);
2dcd851f 466 list_del(&cpufreq_dev->node);
02361418
ADK
467
468 /* Unregister the notifier for the last cpufreq cooling device */
2479bb64 469 if (list_empty(&cpufreq_dev_list))
02361418 470 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
5fda7f68 471 CPUFREQ_POLICY_NOTIFIER);
02361418 472 mutex_unlock(&cooling_cpufreq_lock);
160b7d80 473
02361418
ADK
474 thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
475 release_idr(&cpufreq_idr, cpufreq_dev->id);
f6859014 476 kfree(cpufreq_dev->freq_table);
02361418
ADK
477 kfree(cpufreq_dev);
478}
243dbd9c 479EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);
This page took 0.147441 seconds and 5 git commands to generate.