blob: e03891b03c9b22245403795fb22b1c6ea6b35879 [file] [log] [blame]
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301/*
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/kernel.h>
24#include <linux/module.h>
25#include <linux/thermal.h>
26#include <linux/platform_device.h>
27#include <linux/cpufreq.h>
28#include <linux/err.h>
29#include <linux/slab.h>
30#include <linux/cpu.h>
31#include <linux/cpu_cooling.h>
32
33/**
34 * struct cpufreq_cooling_device
35 * @id: unique integer value corresponding to each cpufreq_cooling_device
36 * registered.
37 * @cool_dev: thermal_cooling_device pointer to keep track of the the
38 * egistered cooling device.
39 * @cpufreq_state: integer value representing the current state of cpufreq
40 * cooling devices.
41 * @cpufreq_val: integer value representing the absolute value of the clipped
42 * frequency.
43 * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
44 * @node: list_head to link all cpufreq_cooling_device together.
45 *
46 * This structure is required for keeping information of each
47 * cpufreq_cooling_device registered as a list whose head is represented by
48 * cooling_cpufreq_list. In order to prevent corruption of this list a
49 * mutex lock cooling_cpufreq_lock is used.
50 */
51struct cpufreq_cooling_device {
52 int id;
53 struct thermal_cooling_device *cool_dev;
54 unsigned int cpufreq_state;
55 unsigned int cpufreq_val;
56 struct cpumask allowed_cpus;
57 struct list_head node;
58};
59static LIST_HEAD(cooling_cpufreq_list);
60static DEFINE_IDR(cpufreq_idr);
hongbo.zhang160b7d82012-10-30 17:48:59 +010061static DEFINE_MUTEX(cooling_cpufreq_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053062
hongbo.zhang160b7d82012-10-30 17:48:59 +010063static unsigned int cpufreq_dev_count;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053064
65/* notify_table passes value to the CPUFREQ_ADJUST callback function. */
66#define NOTIFY_INVALID NULL
Sachin Kamata0f846c2012-11-15 12:19:44 +053067static struct cpufreq_cooling_device *notify_device;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053068
69/**
70 * get_idr - function to get a unique id.
71 * @idr: struct idr * handle used to create a id.
72 * @id: int * value generated by this function.
73 */
74static int get_idr(struct idr *idr, int *id)
75{
Tejun Heo6deb69f2013-02-27 17:04:46 -080076 int ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053077
78 mutex_lock(&cooling_cpufreq_lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -080079 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053080 mutex_unlock(&cooling_cpufreq_lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -080081 if (unlikely(ret < 0))
82 return ret;
83 *id = ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053084 return 0;
85}
86
87/**
88 * release_idr - function to free the unique id.
89 * @idr: struct idr * handle used for creating the id.
90 * @id: int value representing the unique id.
91 */
92static void release_idr(struct idr *idr, int id)
93{
94 mutex_lock(&cooling_cpufreq_lock);
95 idr_remove(idr, id);
96 mutex_unlock(&cooling_cpufreq_lock);
97}
98
99/* Below code defines functions to be used for cpufreq as cooling device */
100
101/**
102 * is_cpufreq_valid - function to check if a cpu has frequency transition policy.
103 * @cpu: cpu for which check is needed.
104 */
105static int is_cpufreq_valid(int cpu)
106{
107 struct cpufreq_policy policy;
108 return !cpufreq_get_policy(&policy, cpu);
109}
110
Zhang Ruifc35b352013-02-08 13:09:32 +0800111enum cpufreq_cooling_property {
112 GET_LEVEL,
113 GET_FREQ,
114 GET_MAXL,
115};
116
117/*
118 * this is the common function to
119 * 1. get maximum cpu cooling states
120 * 2. translate frequency to cooling state
121 * 3. translate cooling state to frequency
122 * Note that the code may be not in good shape
123 * but it is written in this way in order to:
124 * a) reduce duplicate code as most of the code can be shared.
125 * b) make sure the logic is consistent when translating between
126 * cooling states and frequencies.
127*/
128static int get_property(unsigned int cpu, unsigned long input,
129 unsigned int* output, enum cpufreq_cooling_property property)
130{
131 int i, j;
132 unsigned long max_level = 0, level;
133 unsigned int freq = CPUFREQ_ENTRY_INVALID;
134 int descend = -1;
135 struct cpufreq_frequency_table *table =
136 cpufreq_frequency_get_table(cpu);
137
138 if (!output)
139 return -EINVAL;
140
141 if (!table)
142 return -EINVAL;
143
144
145 for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
146 /* ignore invalid entries */
147 if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
148 continue;
149
150 /* ignore duplicate entry */
151 if (freq == table[i].frequency)
152 continue;
153
154 /* get the frequency order */
155 if (freq != CPUFREQ_ENTRY_INVALID && descend != -1)
156 descend = !!(freq > table[i].frequency);
157
158 freq = table[i].frequency;
159 max_level++;
160 }
161
162 /* get max level */
163 if (property == GET_MAXL) {
164 *output = (unsigned int)max_level;
165 return 0;
166 }
167
168 if (property == GET_FREQ)
169 level = descend ? input : (max_level - input -1);
170
171
172 for (i = 0, j = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
173 /* ignore invalid entry */
174 if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
175 continue;
176
177 /* ignore duplicate entry */
178 if (freq == table[i].frequency)
179 continue;
180
181 /* now we have a valid frequency entry */
182 freq = table[i].frequency;
183
184 if (property == GET_LEVEL && (unsigned int)input == freq) {
185 /* get level by frequency */
186 *output = descend ? j : (max_level - j - 1);
187 return 0;
188 }
189 if (property == GET_FREQ && level == j) {
190 /* get frequency by level */
191 *output = freq;
192 return 0;
193 }
194 j++;
195 }
196 return -EINVAL;
197}
198
Zhang Rui57df8102013-02-08 14:52:06 +0800199unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq)
200{
201 unsigned int val;
202
203 if (get_property(cpu, (unsigned long)freq, &val, GET_LEVEL))
204 return THERMAL_CSTATE_INVALID;
205 return (unsigned long)val;
206}
207
208EXPORT_SYMBOL(cpufreq_cooling_get_level);
209
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530210/**
211 * get_cpu_frequency - get the absolute value of frequency from level.
212 * @cpu: cpu for which frequency is fetched.
Zhang Rui475f41c2013-02-06 09:34:32 +0800213 * @level: level of frequency, equals cooling state of cpu cooling device
214 * e.g level=0 --> 1st MAX FREQ, level=1 ---> 2nd MAX FREQ, .... etc
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530215 */
216static unsigned int get_cpu_frequency(unsigned int cpu, unsigned long level)
217{
Zhang Ruifc35b352013-02-08 13:09:32 +0800218 int ret = 0;
219 unsigned int freq;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530220
Zhang Ruifc35b352013-02-08 13:09:32 +0800221 ret = get_property(cpu, level, &freq, GET_FREQ);
222 if (ret)
223 return 0;
224 return freq;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530225}
226
227/**
228 * cpufreq_apply_cooling - function to apply frequency clipping.
229 * @cpufreq_device: cpufreq_cooling_device pointer containing frequency
230 * clipping data.
231 * @cooling_state: value of the cooling state.
232 */
233static int cpufreq_apply_cooling(struct cpufreq_cooling_device *cpufreq_device,
234 unsigned long cooling_state)
235{
236 unsigned int cpuid, clip_freq;
237 struct cpumask *maskPtr = &cpufreq_device->allowed_cpus;
238 unsigned int cpu = cpumask_any(maskPtr);
239
240
241 /* Check if the old cooling action is same as new cooling action */
242 if (cpufreq_device->cpufreq_state == cooling_state)
243 return 0;
244
245 clip_freq = get_cpu_frequency(cpu, cooling_state);
246 if (!clip_freq)
247 return -EINVAL;
248
249 cpufreq_device->cpufreq_state = cooling_state;
250 cpufreq_device->cpufreq_val = clip_freq;
251 notify_device = cpufreq_device;
252
253 for_each_cpu(cpuid, maskPtr) {
254 if (is_cpufreq_valid(cpuid))
255 cpufreq_update_policy(cpuid);
256 }
257
258 notify_device = NOTIFY_INVALID;
259
260 return 0;
261}
262
263/**
264 * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
265 * @nb: struct notifier_block * with callback info.
266 * @event: value showing cpufreq event for which this function invoked.
267 * @data: callback-specific data
268 */
269static int cpufreq_thermal_notifier(struct notifier_block *nb,
270 unsigned long event, void *data)
271{
272 struct cpufreq_policy *policy = data;
273 unsigned long max_freq = 0;
274
275 if (event != CPUFREQ_ADJUST || notify_device == NOTIFY_INVALID)
276 return 0;
277
278 if (cpumask_test_cpu(policy->cpu, &notify_device->allowed_cpus))
279 max_freq = notify_device->cpufreq_val;
280
281 /* Never exceed user_policy.max*/
282 if (max_freq > policy->user_policy.max)
283 max_freq = policy->user_policy.max;
284
285 if (policy->max != max_freq)
286 cpufreq_verify_within_limits(policy, 0, max_freq);
287
288 return 0;
289}
290
291/*
292 * cpufreq cooling device callback functions are defined below
293 */
294
295/**
296 * cpufreq_get_max_state - callback function to get the max cooling state.
297 * @cdev: thermal cooling device pointer.
298 * @state: fill this variable with the max cooling state.
299 */
300static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
301 unsigned long *state)
302{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100303 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
304 struct cpumask *maskPtr = &cpufreq_device->allowed_cpus;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530305 unsigned int cpu;
hongbo.zhang9c51b052012-10-30 17:48:58 +0100306 unsigned long count = 0;
Zhang Ruifc35b352013-02-08 13:09:32 +0800307 int ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530308
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530309 cpu = cpumask_any(maskPtr);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530310
Zhang Ruifc35b352013-02-08 13:09:32 +0800311 ret = get_property(cpu, 0, (unsigned int *)&count, GET_MAXL);
hongbo.zhang9c51b052012-10-30 17:48:58 +0100312
Zhang Ruifc35b352013-02-08 13:09:32 +0800313 if (count > 0)
314 *state = count;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530315
Zhang Ruifc35b352013-02-08 13:09:32 +0800316 return ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530317}
318
319/**
320 * cpufreq_get_cur_state - callback function to get the current cooling state.
321 * @cdev: thermal cooling device pointer.
322 * @state: fill this variable with the current cooling state.
323 */
324static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
325 unsigned long *state)
326{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100327 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530328
hongbo.zhang160b7d82012-10-30 17:48:59 +0100329 *state = cpufreq_device->cpufreq_state;
330 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530331}
332
333/**
334 * cpufreq_set_cur_state - callback function to set the current cooling state.
335 * @cdev: thermal cooling device pointer.
336 * @state: set this variable to the current cooling state.
337 */
338static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
339 unsigned long state)
340{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100341 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530342
hongbo.zhang160b7d82012-10-30 17:48:59 +0100343 return cpufreq_apply_cooling(cpufreq_device, state);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530344}
345
346/* Bind cpufreq callbacks to thermal cooling device ops */
347static struct thermal_cooling_device_ops const cpufreq_cooling_ops = {
348 .get_max_state = cpufreq_get_max_state,
349 .get_cur_state = cpufreq_get_cur_state,
350 .set_cur_state = cpufreq_set_cur_state,
351};
352
353/* Notifier for cpufreq policy change */
354static struct notifier_block thermal_cpufreq_notifier_block = {
355 .notifier_call = cpufreq_thermal_notifier,
356};
357
358/**
359 * cpufreq_cooling_register - function to create cpufreq cooling device.
360 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
361 */
362struct thermal_cooling_device *cpufreq_cooling_register(
Eduardo Valentin3778ff52012-11-12 15:58:50 +0000363 const struct cpumask *clip_cpus)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530364{
365 struct thermal_cooling_device *cool_dev;
366 struct cpufreq_cooling_device *cpufreq_dev = NULL;
hongbo.zhang160b7d82012-10-30 17:48:59 +0100367 unsigned int min = 0, max = 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530368 char dev_name[THERMAL_NAME_LENGTH];
Jonghwa Leea4b6fec2012-09-26 09:43:31 +0900369 int ret = 0, i;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530370 struct cpufreq_policy policy;
371
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530372 /*Verify that all the clip cpus have same freq_min, freq_max limit*/
373 for_each_cpu(i, clip_cpus) {
374 /*continue if cpufreq policy not found and not return error*/
375 if (!cpufreq_get_policy(&policy, i))
376 continue;
377 if (min == 0 && max == 0) {
378 min = policy.cpuinfo.min_freq;
379 max = policy.cpuinfo.max_freq;
380 } else {
381 if (min != policy.cpuinfo.min_freq ||
382 max != policy.cpuinfo.max_freq)
383 return ERR_PTR(-EINVAL);
hongbo.zhang6b6519d2012-10-30 17:48:57 +0100384 }
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530385 }
386 cpufreq_dev = kzalloc(sizeof(struct cpufreq_cooling_device),
387 GFP_KERNEL);
388 if (!cpufreq_dev)
389 return ERR_PTR(-ENOMEM);
390
391 cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
392
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530393 ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
394 if (ret) {
395 kfree(cpufreq_dev);
396 return ERR_PTR(-EINVAL);
397 }
398
399 sprintf(dev_name, "thermal-cpufreq-%d", cpufreq_dev->id);
400
401 cool_dev = thermal_cooling_device_register(dev_name, cpufreq_dev,
402 &cpufreq_cooling_ops);
403 if (!cool_dev) {
404 release_idr(&cpufreq_idr, cpufreq_dev->id);
405 kfree(cpufreq_dev);
406 return ERR_PTR(-EINVAL);
407 }
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530408 cpufreq_dev->cool_dev = cool_dev;
409 cpufreq_dev->cpufreq_state = 0;
410 mutex_lock(&cooling_cpufreq_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530411
412 /* Register the notifier for first cpufreq cooling device */
413 if (cpufreq_dev_count == 0)
414 cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
415 CPUFREQ_POLICY_NOTIFIER);
hongbo.zhang160b7d82012-10-30 17:48:59 +0100416 cpufreq_dev_count++;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530417
418 mutex_unlock(&cooling_cpufreq_lock);
419 return cool_dev;
420}
421EXPORT_SYMBOL(cpufreq_cooling_register);
422
423/**
424 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
425 * @cdev: thermal cooling device pointer.
426 */
427void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
428{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100429 struct cpufreq_cooling_device *cpufreq_dev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530430
431 mutex_lock(&cooling_cpufreq_lock);
hongbo.zhang160b7d82012-10-30 17:48:59 +0100432 cpufreq_dev_count--;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530433
434 /* Unregister the notifier for the last cpufreq cooling device */
hongbo.zhang160b7d82012-10-30 17:48:59 +0100435 if (cpufreq_dev_count == 0) {
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530436 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
437 CPUFREQ_POLICY_NOTIFIER);
438 }
439 mutex_unlock(&cooling_cpufreq_lock);
hongbo.zhang160b7d82012-10-30 17:48:59 +0100440
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530441 thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
442 release_idr(&cpufreq_idr, cpufreq_dev->id);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530443 kfree(cpufreq_dev);
444}
445EXPORT_SYMBOL(cpufreq_cooling_unregister);