thermal: cpu_cooling: Store frequencies in descending order
[deliverable/linux.git] / drivers / thermal / cpu_cooling.c
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 */
23 #include <linux/module.h>
24 #include <linux/thermal.h>
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 /*
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
45 /**
46 * struct cpufreq_cooling_device - data for cooling device with cpufreq
47 * @id: unique integer value corresponding to each cpufreq_cooling_device
48 * registered.
49 * @cool_dev: thermal_cooling_device pointer to keep track of the
50 * registered cooling device.
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.
55 * @max_level: maximum cooling level. One less than total number of valid
56 * cpufreq frequencies.
57 * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
58 *
59 * This structure is required for keeping information of each registered
60 * cpufreq_cooling_device.
61 */
62 struct cpufreq_cooling_device {
63 int id;
64 struct thermal_cooling_device *cool_dev;
65 unsigned int cpufreq_state;
66 unsigned int cpufreq_val;
67 unsigned int max_level;
68 unsigned int *freq_table; /* In descending order */
69 struct cpumask allowed_cpus;
70 struct list_head node;
71 };
72 static DEFINE_IDR(cpufreq_idr);
73 static DEFINE_MUTEX(cooling_cpufreq_lock);
74
75 static LIST_HEAD(cpufreq_dev_list);
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.
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.
86 */
87 static int get_idr(struct idr *idr, int *id)
88 {
89 int ret;
90
91 mutex_lock(&cooling_cpufreq_lock);
92 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
93 mutex_unlock(&cooling_cpufreq_lock);
94 if (unlikely(ret < 0))
95 return ret;
96 *id = ret;
97
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 */
106 static 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
115 enum cpufreq_cooling_property {
116 GET_LEVEL,
117 GET_FREQ,
118 };
119
120 /**
121 * get_property - fetch a property of interest for a given cpu.
122 * @cpufreq_dev: cpufreq_dev for which the property is required
123 * @input: query parameter
124 * @output: query return
125 * @property: type of query (frequency, level)
126 *
127 * This is the common function to
128 * 1. translate frequency to cooling state
129 * 2. translate cooling state to frequency
130 *
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.
136 *
137 * Return: 0 on success, -EINVAL when invalid parameters are passed.
138 */
139 static int get_property(struct cpufreq_cooling_device *cpufreq_dev,
140 unsigned long input, unsigned int *output,
141 enum cpufreq_cooling_property property)
142 {
143 int i;
144 unsigned long level = 0;
145 unsigned int freq = CPUFREQ_ENTRY_INVALID;
146 int descend = -1;
147 struct cpufreq_frequency_table *pos, *table;
148
149 if (!output)
150 return -EINVAL;
151
152 table = cpufreq_frequency_get_table(cpumask_first(&cpufreq_dev->allowed_cpus));
153 if (!table)
154 return -EINVAL;
155
156 cpufreq_for_each_valid_entry(pos, table) {
157 /* ignore duplicate entry */
158 if (freq == pos->frequency)
159 continue;
160
161 /* get the frequency order */
162 if (freq != CPUFREQ_ENTRY_INVALID && descend == -1)
163 descend = freq > pos->frequency;
164
165 freq = pos->frequency;
166 }
167
168 if (property == GET_FREQ)
169 level = descend ? input : (cpufreq_dev->max_level - input);
170
171 i = 0;
172 cpufreq_for_each_valid_entry(pos, table) {
173 /* ignore duplicate entry */
174 if (freq == pos->frequency)
175 continue;
176
177 /* now we have a valid frequency entry */
178 freq = pos->frequency;
179
180 if (property == GET_LEVEL && (unsigned int)input == freq) {
181 /* get level by frequency */
182 *output = descend ? i : (cpufreq_dev->max_level - i);
183 return 0;
184 }
185 if (property == GET_FREQ && level == i) {
186 /* get frequency by level */
187 *output = freq;
188 return 0;
189 }
190 i++;
191 }
192
193 return -EINVAL;
194 }
195
196 /**
197 * cpufreq_cooling_get_level - for a given cpu, return the cooling level.
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 */
207 unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq)
208 {
209 struct cpufreq_cooling_device *cpufreq_dev;
210
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);
225
226 pr_err("%s: cpu:%d not part of any cooling device\n", __func__, cpu);
227 return THERMAL_CSTATE_INVALID;
228 }
229 EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level);
230
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
236 *
237 * Callback to hijack the notification on cpufreq policy transition.
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)
242 */
243 static int cpufreq_thermal_notifier(struct notifier_block *nb,
244 unsigned long event, void *data)
245 {
246 struct cpufreq_policy *policy = data;
247 unsigned long max_freq = 0;
248 struct cpufreq_cooling_device *cpufreq_dev;
249
250 if (event != CPUFREQ_ADJUST)
251 return 0;
252
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
259 max_freq = cpufreq_dev->cpufreq_val;
260
261 if (policy->max != max_freq)
262 cpufreq_verify_within_limits(policy, 0, max_freq);
263 }
264 mutex_unlock(&cooling_cpufreq_lock);
265
266 return 0;
267 }
268
269 /* cpufreq cooling device callback functions are defined below */
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.
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.
280 */
281 static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
282 unsigned long *state)
283 {
284 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
285
286 *state = cpufreq_device->max_level;
287 return 0;
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.
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.
299 */
300 static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
301 unsigned long *state)
302 {
303 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
304
305 *state = cpufreq_device->cpufreq_state;
306
307 return 0;
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.
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.
319 */
320 static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
321 unsigned long state)
322 {
323 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
324 unsigned int cpu = cpumask_any(&cpufreq_device->allowed_cpus);
325 unsigned int clip_freq;
326 int ret;
327
328 /* Check if the old cooling action is same as new cooling action */
329 if (cpufreq_device->cpufreq_state == state)
330 return 0;
331
332 ret = get_property(cpufreq_device, state, &clip_freq, GET_FREQ);
333 if (ret)
334 return ret;
335
336 cpufreq_device->cpufreq_state = state;
337 cpufreq_device->cpufreq_val = clip_freq;
338
339 cpufreq_update_policy(cpu);
340
341 return 0;
342 }
343
344 /* Bind cpufreq callbacks to thermal cooling device ops */
345 static 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 */
352 static struct notifier_block thermal_cpufreq_notifier_block = {
353 .notifier_call = cpufreq_thermal_notifier,
354 };
355
356 static 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
370 /**
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
373 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
374 * Normally this should be same as cpufreq policy->related_cpus.
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
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.
380 *
381 * Return: a valid struct thermal_cooling_device pointer on success,
382 * on failure, it returns a corresponding ERR_PTR().
383 */
384 static struct thermal_cooling_device *
385 __cpufreq_cooling_register(struct device_node *np,
386 const struct cpumask *clip_cpus)
387 {
388 struct thermal_cooling_device *cool_dev;
389 struct cpufreq_cooling_device *cpufreq_dev;
390 char dev_name[THERMAL_NAME_LENGTH];
391 struct cpufreq_frequency_table *pos, *table;
392 unsigned int freq, i;
393 int ret;
394
395 table = cpufreq_frequency_get_table(cpumask_first(clip_cpus));
396 if (!table) {
397 pr_debug("%s: CPUFreq table not found\n", __func__);
398 return ERR_PTR(-EPROBE_DEFER);
399 }
400
401 cpufreq_dev = kzalloc(sizeof(*cpufreq_dev), GFP_KERNEL);
402 if (!cpufreq_dev)
403 return ERR_PTR(-ENOMEM);
404
405 ret = get_property(cpufreq_dev, 0, &cpufreq_dev->cpufreq_val, GET_FREQ);
406 if (ret) {
407 pr_err("%s: Failed to get frequency: %d", __func__, ret);
408 cool_dev = ERR_PTR(ret);
409 goto free_cdev;
410 }
411
412 /* Find max levels */
413 cpufreq_for_each_valid_entry(pos, table)
414 cpufreq_dev->max_level++;
415
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
424 /* max_level is an index, not a counter */
425 cpufreq_dev->max_level--;
426
427 cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
428
429 ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
430 if (ret) {
431 cool_dev = ERR_PTR(ret);
432 goto free_table;
433 }
434
435 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
436 cpufreq_dev->id);
437
438 cool_dev = thermal_of_cooling_device_register(np, dev_name, cpufreq_dev,
439 &cpufreq_cooling_ops);
440 if (IS_ERR(cool_dev))
441 goto remove_idr;
442
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
455 cpufreq_dev->cool_dev = cool_dev;
456
457 mutex_lock(&cooling_cpufreq_lock);
458
459 /* Register the notifier for first cpufreq cooling device */
460 if (list_empty(&cpufreq_dev_list))
461 cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
462 CPUFREQ_POLICY_NOTIFIER);
463 list_add(&cpufreq_dev->node, &cpufreq_dev_list);
464
465 mutex_unlock(&cooling_cpufreq_lock);
466
467 return cool_dev;
468
469 remove_idr:
470 release_idr(&cpufreq_idr, cpufreq_dev->id);
471 free_table:
472 kfree(cpufreq_dev->freq_table);
473 free_cdev:
474 kfree(cpufreq_dev);
475
476 return cool_dev;
477 }
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 */
490 struct thermal_cooling_device *
491 cpufreq_cooling_register(const struct cpumask *clip_cpus)
492 {
493 return __cpufreq_cooling_register(NULL, clip_cpus);
494 }
495 EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
496
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 */
510 struct thermal_cooling_device *
511 of_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 }
519 EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
520
521 /**
522 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
523 * @cdev: thermal cooling device pointer.
524 *
525 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
526 */
527 void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
528 {
529 struct cpufreq_cooling_device *cpufreq_dev;
530
531 if (!cdev)
532 return;
533
534 cpufreq_dev = cdev->devdata;
535 mutex_lock(&cooling_cpufreq_lock);
536 list_del(&cpufreq_dev->node);
537
538 /* Unregister the notifier for the last cpufreq cooling device */
539 if (list_empty(&cpufreq_dev_list))
540 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
541 CPUFREQ_POLICY_NOTIFIER);
542 mutex_unlock(&cooling_cpufreq_lock);
543
544 thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
545 release_idr(&cpufreq_idr, cpufreq_dev->id);
546 kfree(cpufreq_dev->freq_table);
547 kfree(cpufreq_dev);
548 }
549 EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);
This page took 0.044358 seconds and 5 git commands to generate.