]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/thermal/cpu_cooling.c
thermal: cpu_cooling: Merge cpufreq_apply_cooling() into cpufreq_set_cur_state()
[karo-tx-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  * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
56  *
57  * This structure is required for keeping information of each registered
58  * cpufreq_cooling_device.
59  */
60 struct cpufreq_cooling_device {
61         int id;
62         struct thermal_cooling_device *cool_dev;
63         unsigned int cpufreq_state;
64         unsigned int cpufreq_val;
65         struct cpumask allowed_cpus;
66         struct list_head node;
67 };
68 static DEFINE_IDR(cpufreq_idr);
69 static DEFINE_MUTEX(cooling_cpufreq_lock);
70
71 static unsigned int cpufreq_dev_count;
72
73 static LIST_HEAD(cpufreq_dev_list);
74
75 /**
76  * get_idr - function to get a unique id.
77  * @idr: struct idr * handle used to create a id.
78  * @id: int * value generated by this function.
79  *
80  * This function will populate @id with an unique
81  * id, using the idr API.
82  *
83  * Return: 0 on success, an error code on failure.
84  */
85 static int get_idr(struct idr *idr, int *id)
86 {
87         int ret;
88
89         mutex_lock(&cooling_cpufreq_lock);
90         ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
91         mutex_unlock(&cooling_cpufreq_lock);
92         if (unlikely(ret < 0))
93                 return ret;
94         *id = ret;
95
96         return 0;
97 }
98
99 /**
100  * release_idr - function to free the unique id.
101  * @idr: struct idr * handle used for creating the id.
102  * @id: int value representing the unique id.
103  */
104 static void release_idr(struct idr *idr, int id)
105 {
106         mutex_lock(&cooling_cpufreq_lock);
107         idr_remove(idr, id);
108         mutex_unlock(&cooling_cpufreq_lock);
109 }
110
111 /* Below code defines functions to be used for cpufreq as cooling device */
112
113 enum cpufreq_cooling_property {
114         GET_LEVEL,
115         GET_FREQ,
116         GET_MAXL,
117 };
118
119 /**
120  * get_property - fetch a property of interest for a given cpu.
121  * @cpu: cpu for which the property is required
122  * @input: query parameter
123  * @output: query return
124  * @property: type of query (frequency, level, max level)
125  *
126  * This is the common function to
127  * 1. get maximum cpu cooling states
128  * 2. translate frequency to cooling state
129  * 3. 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(unsigned int cpu, unsigned long input,
140                         unsigned int *output,
141                         enum cpufreq_cooling_property property)
142 {
143         int i;
144         unsigned long max_level = 0, level = 0;
145         unsigned int freq = CPUFREQ_ENTRY_INVALID;
146         int descend = -1;
147         struct cpufreq_frequency_table *pos, *table =
148                                         cpufreq_frequency_get_table(cpu);
149
150         if (!output)
151                 return -EINVAL;
152
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                 max_level++;
167         }
168
169         /* No valid cpu frequency entry */
170         if (max_level == 0)
171                 return -EINVAL;
172
173         /* max_level is an index, not a counter */
174         max_level--;
175
176         /* get max level */
177         if (property == GET_MAXL) {
178                 *output = (unsigned int)max_level;
179                 return 0;
180         }
181
182         if (property == GET_FREQ)
183                 level = descend ? input : (max_level - input);
184
185         i = 0;
186         cpufreq_for_each_valid_entry(pos, table) {
187                 /* ignore duplicate entry */
188                 if (freq == pos->frequency)
189                         continue;
190
191                 /* now we have a valid frequency entry */
192                 freq = pos->frequency;
193
194                 if (property == GET_LEVEL && (unsigned int)input == freq) {
195                         /* get level by frequency */
196                         *output = descend ? i : (max_level - i);
197                         return 0;
198                 }
199                 if (property == GET_FREQ && level == i) {
200                         /* get frequency by level */
201                         *output = freq;
202                         return 0;
203                 }
204                 i++;
205         }
206
207         return -EINVAL;
208 }
209
210 /**
211  * cpufreq_cooling_get_level - for a given cpu, return the cooling level.
212  * @cpu: cpu for which the level is required
213  * @freq: the frequency of interest
214  *
215  * This function will match the cooling level corresponding to the
216  * requested @freq and return it.
217  *
218  * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID
219  * otherwise.
220  */
221 unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq)
222 {
223         unsigned int val;
224
225         if (get_property(cpu, (unsigned long)freq, &val, GET_LEVEL))
226                 return THERMAL_CSTATE_INVALID;
227
228         return (unsigned long)val;
229 }
230 EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level);
231
232 /**
233  * get_cpu_frequency - get the absolute value of frequency from level.
234  * @cpu: cpu for which frequency is fetched.
235  * @level: cooling level
236  *
237  * This function matches cooling level with frequency. Based on a cooling level
238  * of frequency, equals cooling state of cpu cooling device, it will return
239  * the corresponding frequency.
240  *      e.g level=0 --> 1st MAX FREQ, level=1 ---> 2nd MAX FREQ, .... etc
241  *
242  * Return: 0 on error, the corresponding frequency otherwise.
243  */
244 static unsigned int get_cpu_frequency(unsigned int cpu, unsigned long level)
245 {
246         int ret = 0;
247         unsigned int freq;
248
249         ret = get_property(cpu, level, &freq, GET_FREQ);
250         if (ret)
251                 return 0;
252
253         return freq;
254 }
255
256 /**
257  * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
258  * @nb: struct notifier_block * with callback info.
259  * @event: value showing cpufreq event for which this function invoked.
260  * @data: callback-specific data
261  *
262  * Callback to hijack the notification on cpufreq policy transition.
263  * Every time there is a change in policy, we will intercept and
264  * update the cpufreq policy with thermal constraints.
265  *
266  * Return: 0 (success)
267  */
268 static int cpufreq_thermal_notifier(struct notifier_block *nb,
269                                     unsigned long event, void *data)
270 {
271         struct cpufreq_policy *policy = data;
272         unsigned long max_freq = 0;
273         struct cpufreq_cooling_device *cpufreq_dev;
274
275         if (event != CPUFREQ_ADJUST)
276                 return 0;
277
278         mutex_lock(&cooling_cpufreq_lock);
279         list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
280                 if (!cpumask_test_cpu(policy->cpu,
281                                         &cpufreq_dev->allowed_cpus))
282                         continue;
283
284                 max_freq = cpufreq_dev->cpufreq_val;
285
286                 if (policy->max != max_freq)
287                         cpufreq_verify_within_limits(policy, 0, max_freq);
288         }
289         mutex_unlock(&cooling_cpufreq_lock);
290
291         return 0;
292 }
293
294 /* cpufreq cooling device callback functions are defined below */
295
296 /**
297  * cpufreq_get_max_state - callback function to get the max cooling state.
298  * @cdev: thermal cooling device pointer.
299  * @state: fill this variable with the max cooling state.
300  *
301  * Callback for the thermal cooling device to return the cpufreq
302  * max cooling state.
303  *
304  * Return: 0 on success, an error code otherwise.
305  */
306 static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
307                                  unsigned long *state)
308 {
309         struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
310         struct cpumask *mask = &cpufreq_device->allowed_cpus;
311         unsigned int cpu;
312         unsigned int count = 0;
313         int ret;
314
315         cpu = cpumask_any(mask);
316
317         ret = get_property(cpu, 0, &count, GET_MAXL);
318
319         if (count > 0)
320                 *state = count;
321
322         return ret;
323 }
324
325 /**
326  * cpufreq_get_cur_state - callback function to get the current cooling state.
327  * @cdev: thermal cooling device pointer.
328  * @state: fill this variable with the current cooling state.
329  *
330  * Callback for the thermal cooling device to return the cpufreq
331  * current cooling state.
332  *
333  * Return: 0 on success, an error code otherwise.
334  */
335 static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
336                                  unsigned long *state)
337 {
338         struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
339
340         *state = cpufreq_device->cpufreq_state;
341
342         return 0;
343 }
344
345 /**
346  * cpufreq_set_cur_state - callback function to set the current cooling state.
347  * @cdev: thermal cooling device pointer.
348  * @state: set this variable to the current cooling state.
349  *
350  * Callback for the thermal cooling device to change the cpufreq
351  * current cooling state.
352  *
353  * Return: 0 on success, an error code otherwise.
354  */
355 static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
356                                  unsigned long state)
357 {
358         struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
359         unsigned int cpu = cpumask_any(&cpufreq_device->allowed_cpus);
360         unsigned int clip_freq;
361
362         /* Check if the old cooling action is same as new cooling action */
363         if (cpufreq_device->cpufreq_state == state)
364                 return 0;
365
366         clip_freq = get_cpu_frequency(cpu, state);
367         if (!clip_freq)
368                 return -EINVAL;
369
370         cpufreq_device->cpufreq_state = state;
371         cpufreq_device->cpufreq_val = clip_freq;
372
373         cpufreq_update_policy(cpu);
374
375         return 0;
376 }
377
378 /* Bind cpufreq callbacks to thermal cooling device ops */
379 static struct thermal_cooling_device_ops const cpufreq_cooling_ops = {
380         .get_max_state = cpufreq_get_max_state,
381         .get_cur_state = cpufreq_get_cur_state,
382         .set_cur_state = cpufreq_set_cur_state,
383 };
384
385 /* Notifier for cpufreq policy change */
386 static struct notifier_block thermal_cpufreq_notifier_block = {
387         .notifier_call = cpufreq_thermal_notifier,
388 };
389
390 /**
391  * __cpufreq_cooling_register - helper function to create cpufreq cooling device
392  * @np: a valid struct device_node to the cooling device device tree node
393  * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
394  * Normally this should be same as cpufreq policy->related_cpus.
395  *
396  * This interface function registers the cpufreq cooling device with the name
397  * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
398  * cooling devices. It also gives the opportunity to link the cooling device
399  * with a device tree node, in order to bind it via the thermal DT code.
400  *
401  * Return: a valid struct thermal_cooling_device pointer on success,
402  * on failure, it returns a corresponding ERR_PTR().
403  */
404 static struct thermal_cooling_device *
405 __cpufreq_cooling_register(struct device_node *np,
406                            const struct cpumask *clip_cpus)
407 {
408         struct thermal_cooling_device *cool_dev;
409         struct cpufreq_cooling_device *cpufreq_dev;
410         char dev_name[THERMAL_NAME_LENGTH];
411         int ret;
412
413         if (!cpufreq_frequency_get_table(cpumask_first(clip_cpus))) {
414                 pr_debug("%s: CPUFreq table not found\n", __func__);
415                 return ERR_PTR(-EPROBE_DEFER);
416         }
417
418         cpufreq_dev = kzalloc(sizeof(*cpufreq_dev), GFP_KERNEL);
419         if (!cpufreq_dev)
420                 return ERR_PTR(-ENOMEM);
421
422         cpufreq_dev->cpufreq_val = get_cpu_frequency(cpumask_any(clip_cpus), 0);
423         if (!cpufreq_dev->cpufreq_val) {
424                 pr_err("%s: Failed to get frequency", __func__);
425                 cool_dev = ERR_PTR(-EINVAL);
426                 goto free_cdev;
427         }
428
429         cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
430
431         ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
432         if (ret) {
433                 cool_dev = ERR_PTR(ret);
434                 goto free_cdev;
435         }
436
437         snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
438                  cpufreq_dev->id);
439
440         cool_dev = thermal_of_cooling_device_register(np, dev_name, cpufreq_dev,
441                                                       &cpufreq_cooling_ops);
442         if (IS_ERR(cool_dev))
443                 goto remove_idr;
444
445         cpufreq_dev->cool_dev = cool_dev;
446
447         mutex_lock(&cooling_cpufreq_lock);
448
449         /* Register the notifier for first cpufreq cooling device */
450         if (cpufreq_dev_count == 0)
451                 cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
452                                           CPUFREQ_POLICY_NOTIFIER);
453         cpufreq_dev_count++;
454         list_add(&cpufreq_dev->node, &cpufreq_dev_list);
455
456         mutex_unlock(&cooling_cpufreq_lock);
457
458         return cool_dev;
459
460 remove_idr:
461         release_idr(&cpufreq_idr, cpufreq_dev->id);
462 free_cdev:
463         kfree(cpufreq_dev);
464
465         return cool_dev;
466 }
467
468 /**
469  * cpufreq_cooling_register - function to create cpufreq cooling device.
470  * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
471  *
472  * This interface function registers the cpufreq cooling device with the name
473  * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
474  * cooling devices.
475  *
476  * Return: a valid struct thermal_cooling_device pointer on success,
477  * on failure, it returns a corresponding ERR_PTR().
478  */
479 struct thermal_cooling_device *
480 cpufreq_cooling_register(const struct cpumask *clip_cpus)
481 {
482         return __cpufreq_cooling_register(NULL, clip_cpus);
483 }
484 EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
485
486 /**
487  * of_cpufreq_cooling_register - function to create cpufreq cooling device.
488  * @np: a valid struct device_node to the cooling device device tree node
489  * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
490  *
491  * This interface function registers the cpufreq cooling device with the name
492  * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
493  * cooling devices. Using this API, the cpufreq cooling device will be
494  * linked to the device tree node provided.
495  *
496  * Return: a valid struct thermal_cooling_device pointer on success,
497  * on failure, it returns a corresponding ERR_PTR().
498  */
499 struct thermal_cooling_device *
500 of_cpufreq_cooling_register(struct device_node *np,
501                             const struct cpumask *clip_cpus)
502 {
503         if (!np)
504                 return ERR_PTR(-EINVAL);
505
506         return __cpufreq_cooling_register(np, clip_cpus);
507 }
508 EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
509
510 /**
511  * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
512  * @cdev: thermal cooling device pointer.
513  *
514  * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
515  */
516 void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
517 {
518         struct cpufreq_cooling_device *cpufreq_dev;
519
520         if (!cdev)
521                 return;
522
523         cpufreq_dev = cdev->devdata;
524         mutex_lock(&cooling_cpufreq_lock);
525         list_del(&cpufreq_dev->node);
526         cpufreq_dev_count--;
527
528         /* Unregister the notifier for the last cpufreq cooling device */
529         if (cpufreq_dev_count == 0)
530                 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
531                                             CPUFREQ_POLICY_NOTIFIER);
532         mutex_unlock(&cooling_cpufreq_lock);
533
534         thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
535         release_idr(&cpufreq_idr, cpufreq_dev->id);
536         kfree(cpufreq_dev);
537 }
538 EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);