thermal: cpu_cooling: improve documentation for get_cpu_frequency
[cascardo/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  * struct cpufreq_cooling_device - data for cooling device with cpufreq
33  * @id: unique integer value corresponding to each cpufreq_cooling_device
34  *      registered.
35  * @cool_dev: thermal_cooling_device pointer to keep track of the
36  *      registered cooling device.
37  * @cpufreq_state: integer value representing the current state of cpufreq
38  *      cooling devices.
39  * @cpufreq_val: integer value representing the absolute value of the clipped
40  *      frequency.
41  * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
42  * @node: list_head to link all cpufreq_cooling_device together.
43  *
44  * This structure is required for keeping information of each
45  * cpufreq_cooling_device registered as a list whose head is represented by
46  * cooling_cpufreq_list. In order to prevent corruption of this list a
47  * mutex lock cooling_cpufreq_lock is used.
48  */
49 struct cpufreq_cooling_device {
50         int id;
51         struct thermal_cooling_device *cool_dev;
52         unsigned int cpufreq_state;
53         unsigned int cpufreq_val;
54         struct cpumask allowed_cpus;
55         struct list_head node;
56 };
57 static LIST_HEAD(cooling_cpufreq_list);
58 static DEFINE_IDR(cpufreq_idr);
59 static DEFINE_MUTEX(cooling_cpufreq_lock);
60
61 static unsigned int cpufreq_dev_count;
62
63 /* notify_table passes value to the CPUFREQ_ADJUST callback function. */
64 #define NOTIFY_INVALID NULL
65 static struct cpufreq_cooling_device *notify_device;
66
67 /**
68  * get_idr - function to get a unique id.
69  * @idr: struct idr * handle used to create a id.
70  * @id: int * value generated by this function.
71  */
72 static int get_idr(struct idr *idr, int *id)
73 {
74         int ret;
75
76         mutex_lock(&cooling_cpufreq_lock);
77         ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
78         mutex_unlock(&cooling_cpufreq_lock);
79         if (unlikely(ret < 0))
80                 return ret;
81         *id = ret;
82         return 0;
83 }
84
85 /**
86  * release_idr - function to free the unique id.
87  * @idr: struct idr * handle used for creating the id.
88  * @id: int value representing the unique id.
89  */
90 static void release_idr(struct idr *idr, int id)
91 {
92         mutex_lock(&cooling_cpufreq_lock);
93         idr_remove(idr, id);
94         mutex_unlock(&cooling_cpufreq_lock);
95 }
96
97 /* Below code defines functions to be used for cpufreq as cooling device */
98
99 /**
100  * is_cpufreq_valid - function to check frequency transitioning capability.
101  * @cpu: cpu for which check is needed.
102  *
103  * This function will check the current state of the system if
104  * it is capable of changing the frequency for a given @cpu.
105  *
106  * Return: 0 if the system is not currently capable of changing
107  * the frequency of given cpu. !0 in case the frequency is changeable.
108  */
109 static int is_cpufreq_valid(int cpu)
110 {
111         struct cpufreq_policy policy;
112         return !cpufreq_get_policy(&policy, cpu);
113 }
114
115 enum cpufreq_cooling_property {
116         GET_LEVEL,
117         GET_FREQ,
118         GET_MAXL,
119 };
120
121 /**
122  * get_property - fetch a property of interest for a give cpu.
123  * @cpu: cpu for which the property is required
124  * @input: query parameter
125  * @output: query return
126  * @property: type of query (frequency, level, max level)
127  *
128  * This is the common function to
129  * 1. get maximum cpu cooling states
130  * 2. translate frequency to cooling state
131  * 3. translate cooling state to frequency
132  * Note that the code may be not in good shape
133  * but it is written in this way in order to:
134  * a) reduce duplicate code as most of the code can be shared.
135  * b) make sure the logic is consistent when translating between
136  *    cooling states and frequencies.
137  *
138  * Return: 0 on success, -EINVAL when invalid parameters are passed.
139  */
140 static int get_property(unsigned int cpu, unsigned long input,
141         unsigned int* output, enum cpufreq_cooling_property property)
142 {
143         int i, j;
144         unsigned long max_level = 0, level = 0;
145         unsigned int freq = CPUFREQ_ENTRY_INVALID;
146         int descend = -1;
147         struct cpufreq_frequency_table *table =
148                                         cpufreq_frequency_get_table(cpu);
149         
150         if (!output)
151                 return -EINVAL;
152
153         if (!table)
154                 return -EINVAL;
155
156         
157         for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
158                 /* ignore invalid entries */
159                 if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
160                         continue;
161
162                 /* ignore duplicate entry */
163                 if (freq == table[i].frequency)
164                         continue;
165
166                 /* get the frequency order */
167                 if (freq != CPUFREQ_ENTRY_INVALID && descend != -1)
168                         descend = !!(freq > table[i].frequency);
169
170                 freq = table[i].frequency;
171                 max_level++;
172         }
173
174         /* get max level */
175         if (property == GET_MAXL) {
176                 *output = (unsigned int)max_level;
177                 return 0;
178         }
179
180         if (property == GET_FREQ)
181                 level = descend ? input : (max_level - input -1);
182
183
184         for (i = 0, j = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
185                 /* ignore invalid entry */
186                 if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
187                         continue;
188
189                 /* ignore duplicate entry */
190                 if (freq == table[i].frequency)
191                         continue;
192
193                 /* now we have a valid frequency entry */
194                 freq = table[i].frequency;
195
196                 if (property == GET_LEVEL && (unsigned int)input == freq) {
197                         /* get level by frequency */
198                         *output = descend ? j : (max_level - j - 1);
199                         return 0;
200                 }
201                 if (property == GET_FREQ && level == j) {
202                         /* get frequency by level */
203                         *output = freq;
204                         return 0;
205                 }
206                 j++;
207         }
208         return -EINVAL;
209 }
210
211 /**
212  * cpufreq_cooling_get_level - for a give cpu, return the cooling level.
213  * @cpu: cpu for which the level is required
214  * @freq: the frequency of interest
215  *
216  * This function will match the cooling level corresponding to the
217  * requested @freq and return it.
218  *
219  * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID
220  * otherwise.
221  */
222 unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq)
223 {
224         unsigned int val;
225
226         if (get_property(cpu, (unsigned long)freq, &val, GET_LEVEL))
227                 return THERMAL_CSTATE_INVALID;
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         return freq;
253 }
254
255 /**
256  * cpufreq_apply_cooling - function to apply frequency clipping.
257  * @cpufreq_device: cpufreq_cooling_device pointer containing frequency
258  *      clipping data.
259  * @cooling_state: value of the cooling state.
260  */
261 static int cpufreq_apply_cooling(struct cpufreq_cooling_device *cpufreq_device,
262                                 unsigned long cooling_state)
263 {
264         unsigned int cpuid, clip_freq;
265         struct cpumask *mask = &cpufreq_device->allowed_cpus;
266         unsigned int cpu = cpumask_any(mask);
267
268
269         /* Check if the old cooling action is same as new cooling action */
270         if (cpufreq_device->cpufreq_state == cooling_state)
271                 return 0;
272
273         clip_freq = get_cpu_frequency(cpu, cooling_state);
274         if (!clip_freq)
275                 return -EINVAL;
276
277         cpufreq_device->cpufreq_state = cooling_state;
278         cpufreq_device->cpufreq_val = clip_freq;
279         notify_device = cpufreq_device;
280
281         for_each_cpu(cpuid, mask) {
282                 if (is_cpufreq_valid(cpuid))
283                         cpufreq_update_policy(cpuid);
284         }
285
286         notify_device = NOTIFY_INVALID;
287
288         return 0;
289 }
290
291 /**
292  * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
293  * @nb: struct notifier_block * with callback info.
294  * @event: value showing cpufreq event for which this function invoked.
295  * @data: callback-specific data
296  */
297 static int cpufreq_thermal_notifier(struct notifier_block *nb,
298                                         unsigned long event, void *data)
299 {
300         struct cpufreq_policy *policy = data;
301         unsigned long max_freq = 0;
302
303         if (event != CPUFREQ_ADJUST || notify_device == NOTIFY_INVALID)
304                 return 0;
305
306         if (cpumask_test_cpu(policy->cpu, &notify_device->allowed_cpus))
307                 max_freq = notify_device->cpufreq_val;
308
309         /* Never exceed user_policy.max*/
310         if (max_freq > policy->user_policy.max)
311                 max_freq = policy->user_policy.max;
312
313         if (policy->max != max_freq)
314                 cpufreq_verify_within_limits(policy, 0, max_freq);
315
316         return 0;
317 }
318
319 /*
320  * cpufreq cooling device callback functions are defined below
321  */
322
323 /**
324  * cpufreq_get_max_state - callback function to get the max cooling state.
325  * @cdev: thermal cooling device pointer.
326  * @state: fill this variable with the max cooling state.
327  */
328 static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
329                                  unsigned long *state)
330 {
331         struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
332         struct cpumask *mask = &cpufreq_device->allowed_cpus;
333         unsigned int cpu;
334         unsigned int count = 0;
335         int ret;
336
337         cpu = cpumask_any(mask);
338
339         ret = get_property(cpu, 0, &count, GET_MAXL);
340
341         if (count > 0)
342                 *state = count;
343
344         return ret;
345 }
346
347 /**
348  * cpufreq_get_cur_state - callback function to get the current cooling state.
349  * @cdev: thermal cooling device pointer.
350  * @state: fill this variable with the current cooling state.
351  */
352 static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
353                                  unsigned long *state)
354 {
355         struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
356
357         *state = cpufreq_device->cpufreq_state;
358         return 0;
359 }
360
361 /**
362  * cpufreq_set_cur_state - callback function to set the current cooling state.
363  * @cdev: thermal cooling device pointer.
364  * @state: set this variable to the current cooling state.
365  */
366 static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
367                                  unsigned long state)
368 {
369         struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
370
371         return cpufreq_apply_cooling(cpufreq_device, state);
372 }
373
374 /* Bind cpufreq callbacks to thermal cooling device ops */
375 static struct thermal_cooling_device_ops const cpufreq_cooling_ops = {
376         .get_max_state = cpufreq_get_max_state,
377         .get_cur_state = cpufreq_get_cur_state,
378         .set_cur_state = cpufreq_set_cur_state,
379 };
380
381 /* Notifier for cpufreq policy change */
382 static struct notifier_block thermal_cpufreq_notifier_block = {
383         .notifier_call = cpufreq_thermal_notifier,
384 };
385
386 /**
387  * cpufreq_cooling_register - function to create cpufreq cooling device.
388  * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
389  */
390 struct thermal_cooling_device *cpufreq_cooling_register(
391         const struct cpumask *clip_cpus)
392 {
393         struct thermal_cooling_device *cool_dev;
394         struct cpufreq_cooling_device *cpufreq_dev = NULL;
395         unsigned int min = 0, max = 0;
396         char dev_name[THERMAL_NAME_LENGTH];
397         int ret = 0, i;
398         struct cpufreq_policy policy;
399
400         /*Verify that all the clip cpus have same freq_min, freq_max limit*/
401         for_each_cpu(i, clip_cpus) {
402                 /*continue if cpufreq policy not found and not return error*/
403                 if (!cpufreq_get_policy(&policy, i))
404                         continue;
405                 if (min == 0 && max == 0) {
406                         min = policy.cpuinfo.min_freq;
407                         max = policy.cpuinfo.max_freq;
408                 } else {
409                         if (min != policy.cpuinfo.min_freq ||
410                                 max != policy.cpuinfo.max_freq)
411                                 return ERR_PTR(-EINVAL);
412                 }
413         }
414         cpufreq_dev = kzalloc(sizeof(struct cpufreq_cooling_device),
415                         GFP_KERNEL);
416         if (!cpufreq_dev)
417                 return ERR_PTR(-ENOMEM);
418
419         cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
420
421         ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
422         if (ret) {
423                 kfree(cpufreq_dev);
424                 return ERR_PTR(-EINVAL);
425         }
426
427         sprintf(dev_name, "thermal-cpufreq-%d", cpufreq_dev->id);
428
429         cool_dev = thermal_cooling_device_register(dev_name, cpufreq_dev,
430                                                 &cpufreq_cooling_ops);
431         if (!cool_dev) {
432                 release_idr(&cpufreq_idr, cpufreq_dev->id);
433                 kfree(cpufreq_dev);
434                 return ERR_PTR(-EINVAL);
435         }
436         cpufreq_dev->cool_dev = cool_dev;
437         cpufreq_dev->cpufreq_state = 0;
438         mutex_lock(&cooling_cpufreq_lock);
439
440         /* Register the notifier for first cpufreq cooling device */
441         if (cpufreq_dev_count == 0)
442                 cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
443                                                 CPUFREQ_POLICY_NOTIFIER);
444         cpufreq_dev_count++;
445
446         mutex_unlock(&cooling_cpufreq_lock);
447         return cool_dev;
448 }
449 EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
450
451 /**
452  * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
453  * @cdev: thermal cooling device pointer.
454  */
455 void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
456 {
457         struct cpufreq_cooling_device *cpufreq_dev = cdev->devdata;
458
459         mutex_lock(&cooling_cpufreq_lock);
460         cpufreq_dev_count--;
461
462         /* Unregister the notifier for the last cpufreq cooling device */
463         if (cpufreq_dev_count == 0) {
464                 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
465                                         CPUFREQ_POLICY_NOTIFIER);
466         }
467         mutex_unlock(&cooling_cpufreq_lock);
468
469         thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
470         release_idr(&cpufreq_idr, cpufreq_dev->id);
471         kfree(cpufreq_dev);
472 }
473 EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);