thermal: cpu_cooling: don't iterate over all allowed_cpus to update cpufreq policy
[linux-2.6-block.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 /**
114  * is_cpufreq_valid - function to check frequency transitioning capability.
115  * @cpu: cpu for which check is needed.
116  *
117  * This function will check the current state of the system if
118  * it is capable of changing the frequency for a given @cpu.
119  *
120  * Return: 0 if the system is not currently capable of changing
121  * the frequency of given cpu. !0 in case the frequency is changeable.
122  */
123 static int is_cpufreq_valid(int cpu)
124 {
125         struct cpufreq_policy policy;
126
127         return !cpufreq_get_policy(&policy, cpu);
128 }
129
130 enum cpufreq_cooling_property {
131         GET_LEVEL,
132         GET_FREQ,
133         GET_MAXL,
134 };
135
136 /**
137  * get_property - fetch a property of interest for a given cpu.
138  * @cpu: cpu for which the property is required
139  * @input: query parameter
140  * @output: query return
141  * @property: type of query (frequency, level, max level)
142  *
143  * This is the common function to
144  * 1. get maximum cpu cooling states
145  * 2. translate frequency to cooling state
146  * 3. translate cooling state to frequency
147  *
148  * Note that the code may be not in good shape
149  * but it is written in this way in order to:
150  * a) reduce duplicate code as most of the code can be shared.
151  * b) make sure the logic is consistent when translating between
152  *    cooling states and frequencies.
153  *
154  * Return: 0 on success, -EINVAL when invalid parameters are passed.
155  */
156 static int get_property(unsigned int cpu, unsigned long input,
157                         unsigned int *output,
158                         enum cpufreq_cooling_property property)
159 {
160         int i;
161         unsigned long max_level = 0, level = 0;
162         unsigned int freq = CPUFREQ_ENTRY_INVALID;
163         int descend = -1;
164         struct cpufreq_frequency_table *pos, *table =
165                                         cpufreq_frequency_get_table(cpu);
166
167         if (!output)
168                 return -EINVAL;
169
170         if (!table)
171                 return -EINVAL;
172
173         cpufreq_for_each_valid_entry(pos, table) {
174                 /* ignore duplicate entry */
175                 if (freq == pos->frequency)
176                         continue;
177
178                 /* get the frequency order */
179                 if (freq != CPUFREQ_ENTRY_INVALID && descend == -1)
180                         descend = freq > pos->frequency;
181
182                 freq = pos->frequency;
183                 max_level++;
184         }
185
186         /* No valid cpu frequency entry */
187         if (max_level == 0)
188                 return -EINVAL;
189
190         /* max_level is an index, not a counter */
191         max_level--;
192
193         /* get max level */
194         if (property == GET_MAXL) {
195                 *output = (unsigned int)max_level;
196                 return 0;
197         }
198
199         if (property == GET_FREQ)
200                 level = descend ? input : (max_level - input);
201
202         i = 0;
203         cpufreq_for_each_valid_entry(pos, table) {
204                 /* ignore duplicate entry */
205                 if (freq == pos->frequency)
206                         continue;
207
208                 /* now we have a valid frequency entry */
209                 freq = pos->frequency;
210
211                 if (property == GET_LEVEL && (unsigned int)input == freq) {
212                         /* get level by frequency */
213                         *output = descend ? i : (max_level - i);
214                         return 0;
215                 }
216                 if (property == GET_FREQ && level == i) {
217                         /* get frequency by level */
218                         *output = freq;
219                         return 0;
220                 }
221                 i++;
222         }
223
224         return -EINVAL;
225 }
226
227 /**
228  * cpufreq_cooling_get_level - for a given cpu, return the cooling level.
229  * @cpu: cpu for which the level is required
230  * @freq: the frequency of interest
231  *
232  * This function will match the cooling level corresponding to the
233  * requested @freq and return it.
234  *
235  * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID
236  * otherwise.
237  */
238 unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq)
239 {
240         unsigned int val;
241
242         if (get_property(cpu, (unsigned long)freq, &val, GET_LEVEL))
243                 return THERMAL_CSTATE_INVALID;
244
245         return (unsigned long)val;
246 }
247 EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level);
248
249 /**
250  * get_cpu_frequency - get the absolute value of frequency from level.
251  * @cpu: cpu for which frequency is fetched.
252  * @level: cooling level
253  *
254  * This function matches cooling level with frequency. Based on a cooling level
255  * of frequency, equals cooling state of cpu cooling device, it will return
256  * the corresponding frequency.
257  *      e.g level=0 --> 1st MAX FREQ, level=1 ---> 2nd MAX FREQ, .... etc
258  *
259  * Return: 0 on error, the corresponding frequency otherwise.
260  */
261 static unsigned int get_cpu_frequency(unsigned int cpu, unsigned long level)
262 {
263         int ret = 0;
264         unsigned int freq;
265
266         ret = get_property(cpu, level, &freq, GET_FREQ);
267         if (ret)
268                 return 0;
269
270         return freq;
271 }
272
273 /**
274  * cpufreq_apply_cooling - function to apply frequency clipping.
275  * @cpufreq_device: cpufreq_cooling_device pointer containing frequency
276  *      clipping data.
277  * @cooling_state: value of the cooling state.
278  *
279  * Function used to make sure the cpufreq layer is aware of current thermal
280  * limits. The limits are applied by updating the cpufreq policy.
281  *
282  * Return: 0 on success, an error code otherwise (-EINVAL in case wrong
283  * cooling state).
284  */
285 static int cpufreq_apply_cooling(struct cpufreq_cooling_device *cpufreq_device,
286                                  unsigned long cooling_state)
287 {
288         unsigned int clip_freq;
289         struct cpumask *mask = &cpufreq_device->allowed_cpus;
290         unsigned int cpu = cpumask_any(mask);
291
292         /* Check if the old cooling action is same as new cooling action */
293         if (cpufreq_device->cpufreq_state == cooling_state)
294                 return 0;
295
296         clip_freq = get_cpu_frequency(cpu, cooling_state);
297         if (!clip_freq)
298                 return -EINVAL;
299
300         cpufreq_device->cpufreq_state = cooling_state;
301         cpufreq_device->cpufreq_val = clip_freq;
302
303         if (is_cpufreq_valid(cpu))
304                 cpufreq_update_policy(cpu);
305
306         return 0;
307 }
308
309 /**
310  * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
311  * @nb: struct notifier_block * with callback info.
312  * @event: value showing cpufreq event for which this function invoked.
313  * @data: callback-specific data
314  *
315  * Callback to hijack the notification on cpufreq policy transition.
316  * Every time there is a change in policy, we will intercept and
317  * update the cpufreq policy with thermal constraints.
318  *
319  * Return: 0 (success)
320  */
321 static int cpufreq_thermal_notifier(struct notifier_block *nb,
322                                     unsigned long event, void *data)
323 {
324         struct cpufreq_policy *policy = data;
325         unsigned long max_freq = 0;
326         struct cpufreq_cooling_device *cpufreq_dev;
327
328         if (event != CPUFREQ_ADJUST)
329                 return 0;
330
331         mutex_lock(&cooling_cpufreq_lock);
332         list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
333                 if (!cpumask_test_cpu(policy->cpu,
334                                         &cpufreq_dev->allowed_cpus))
335                         continue;
336
337                 if (!cpufreq_dev->cpufreq_val)
338                         cpufreq_dev->cpufreq_val = get_cpu_frequency(
339                                         cpumask_any(&cpufreq_dev->allowed_cpus),
340                                         cpufreq_dev->cpufreq_state);
341
342                 max_freq = cpufreq_dev->cpufreq_val;
343
344                 if (policy->max != max_freq)
345                         cpufreq_verify_within_limits(policy, 0, max_freq);
346         }
347         mutex_unlock(&cooling_cpufreq_lock);
348
349         return 0;
350 }
351
352 /* cpufreq cooling device callback functions are defined below */
353
354 /**
355  * cpufreq_get_max_state - callback function to get the max cooling state.
356  * @cdev: thermal cooling device pointer.
357  * @state: fill this variable with the max cooling state.
358  *
359  * Callback for the thermal cooling device to return the cpufreq
360  * max cooling state.
361  *
362  * Return: 0 on success, an error code otherwise.
363  */
364 static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
365                                  unsigned long *state)
366 {
367         struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
368         struct cpumask *mask = &cpufreq_device->allowed_cpus;
369         unsigned int cpu;
370         unsigned int count = 0;
371         int ret;
372
373         cpu = cpumask_any(mask);
374
375         ret = get_property(cpu, 0, &count, GET_MAXL);
376
377         if (count > 0)
378                 *state = count;
379
380         return ret;
381 }
382
383 /**
384  * cpufreq_get_cur_state - callback function to get the current cooling state.
385  * @cdev: thermal cooling device pointer.
386  * @state: fill this variable with the current cooling state.
387  *
388  * Callback for the thermal cooling device to return the cpufreq
389  * current cooling state.
390  *
391  * Return: 0 on success, an error code otherwise.
392  */
393 static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
394                                  unsigned long *state)
395 {
396         struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
397
398         *state = cpufreq_device->cpufreq_state;
399
400         return 0;
401 }
402
403 /**
404  * cpufreq_set_cur_state - callback function to set the current cooling state.
405  * @cdev: thermal cooling device pointer.
406  * @state: set this variable to the current cooling state.
407  *
408  * Callback for the thermal cooling device to change the cpufreq
409  * current cooling state.
410  *
411  * Return: 0 on success, an error code otherwise.
412  */
413 static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
414                                  unsigned long state)
415 {
416         struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
417
418         return cpufreq_apply_cooling(cpufreq_device, state);
419 }
420
421 /* Bind cpufreq callbacks to thermal cooling device ops */
422 static struct thermal_cooling_device_ops const cpufreq_cooling_ops = {
423         .get_max_state = cpufreq_get_max_state,
424         .get_cur_state = cpufreq_get_cur_state,
425         .set_cur_state = cpufreq_set_cur_state,
426 };
427
428 /* Notifier for cpufreq policy change */
429 static struct notifier_block thermal_cpufreq_notifier_block = {
430         .notifier_call = cpufreq_thermal_notifier,
431 };
432
433 /**
434  * __cpufreq_cooling_register - helper function to create cpufreq cooling device
435  * @np: a valid struct device_node to the cooling device device tree node
436  * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
437  * Normally this should be same as cpufreq policy->related_cpus.
438  *
439  * This interface function registers the cpufreq cooling device with the name
440  * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
441  * cooling devices. It also gives the opportunity to link the cooling device
442  * with a device tree node, in order to bind it via the thermal DT code.
443  *
444  * Return: a valid struct thermal_cooling_device pointer on success,
445  * on failure, it returns a corresponding ERR_PTR().
446  */
447 static struct thermal_cooling_device *
448 __cpufreq_cooling_register(struct device_node *np,
449                            const struct cpumask *clip_cpus)
450 {
451         struct thermal_cooling_device *cool_dev;
452         struct cpufreq_cooling_device *cpufreq_dev;
453         char dev_name[THERMAL_NAME_LENGTH];
454         int ret;
455
456         if (!cpufreq_frequency_get_table(cpumask_first(clip_cpus))) {
457                 pr_debug("%s: CPUFreq table not found\n", __func__);
458                 return ERR_PTR(-EPROBE_DEFER);
459         }
460
461         cpufreq_dev = kzalloc(sizeof(*cpufreq_dev), GFP_KERNEL);
462         if (!cpufreq_dev)
463                 return ERR_PTR(-ENOMEM);
464
465         cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
466
467         ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
468         if (ret) {
469                 kfree(cpufreq_dev);
470                 return ERR_PTR(ret);
471         }
472
473         snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
474                  cpufreq_dev->id);
475
476         cool_dev = thermal_of_cooling_device_register(np, dev_name, cpufreq_dev,
477                                                       &cpufreq_cooling_ops);
478         if (IS_ERR(cool_dev)) {
479                 release_idr(&cpufreq_idr, cpufreq_dev->id);
480                 kfree(cpufreq_dev);
481                 return cool_dev;
482         }
483         cpufreq_dev->cool_dev = cool_dev;
484
485         mutex_lock(&cooling_cpufreq_lock);
486
487         /* Register the notifier for first cpufreq cooling device */
488         if (cpufreq_dev_count == 0)
489                 cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
490                                           CPUFREQ_POLICY_NOTIFIER);
491         cpufreq_dev_count++;
492         list_add(&cpufreq_dev->node, &cpufreq_dev_list);
493
494         mutex_unlock(&cooling_cpufreq_lock);
495
496         return cool_dev;
497 }
498
499 /**
500  * cpufreq_cooling_register - function to create cpufreq cooling device.
501  * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
502  *
503  * This interface function registers the cpufreq cooling device with the name
504  * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
505  * cooling devices.
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 cpufreq_cooling_register(const struct cpumask *clip_cpus)
512 {
513         return __cpufreq_cooling_register(NULL, clip_cpus);
514 }
515 EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
516
517 /**
518  * of_cpufreq_cooling_register - function to create cpufreq cooling device.
519  * @np: a valid struct device_node to the cooling device device tree node
520  * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
521  *
522  * This interface function registers the cpufreq cooling device with the name
523  * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
524  * cooling devices. Using this API, the cpufreq cooling device will be
525  * linked to the device tree node provided.
526  *
527  * Return: a valid struct thermal_cooling_device pointer on success,
528  * on failure, it returns a corresponding ERR_PTR().
529  */
530 struct thermal_cooling_device *
531 of_cpufreq_cooling_register(struct device_node *np,
532                             const struct cpumask *clip_cpus)
533 {
534         if (!np)
535                 return ERR_PTR(-EINVAL);
536
537         return __cpufreq_cooling_register(np, clip_cpus);
538 }
539 EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
540
541 /**
542  * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
543  * @cdev: thermal cooling device pointer.
544  *
545  * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
546  */
547 void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
548 {
549         struct cpufreq_cooling_device *cpufreq_dev;
550
551         if (!cdev)
552                 return;
553
554         cpufreq_dev = cdev->devdata;
555         mutex_lock(&cooling_cpufreq_lock);
556         list_del(&cpufreq_dev->node);
557         cpufreq_dev_count--;
558
559         /* Unregister the notifier for the last cpufreq cooling device */
560         if (cpufreq_dev_count == 0)
561                 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
562                                             CPUFREQ_POLICY_NOTIFIER);
563         mutex_unlock(&cooling_cpufreq_lock);
564
565         thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
566         release_idr(&cpufreq_idr, cpufreq_dev->id);
567         kfree(cpufreq_dev);
568 }
569 EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);