blob: 9e208d300647d1d9c27c01f314580dc67e129012 [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
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530199/**
200 * get_cpu_frequency - get the absolute value of frequency from level.
201 * @cpu: cpu for which frequency is fetched.
Zhang Rui475f41c2013-02-06 09:34:32 +0800202 * @level: level of frequency, equals cooling state of cpu cooling device
203 * e.g level=0 --> 1st MAX FREQ, level=1 ---> 2nd MAX FREQ, .... etc
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530204 */
205static unsigned int get_cpu_frequency(unsigned int cpu, unsigned long level)
206{
Zhang Ruifc35b352013-02-08 13:09:32 +0800207 int ret = 0;
208 unsigned int freq;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530209
Zhang Ruifc35b352013-02-08 13:09:32 +0800210 ret = get_property(cpu, level, &freq, GET_FREQ);
211 if (ret)
212 return 0;
213 return freq;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530214}
215
216/**
217 * cpufreq_apply_cooling - function to apply frequency clipping.
218 * @cpufreq_device: cpufreq_cooling_device pointer containing frequency
219 * clipping data.
220 * @cooling_state: value of the cooling state.
221 */
222static int cpufreq_apply_cooling(struct cpufreq_cooling_device *cpufreq_device,
223 unsigned long cooling_state)
224{
225 unsigned int cpuid, clip_freq;
226 struct cpumask *maskPtr = &cpufreq_device->allowed_cpus;
227 unsigned int cpu = cpumask_any(maskPtr);
228
229
230 /* Check if the old cooling action is same as new cooling action */
231 if (cpufreq_device->cpufreq_state == cooling_state)
232 return 0;
233
234 clip_freq = get_cpu_frequency(cpu, cooling_state);
235 if (!clip_freq)
236 return -EINVAL;
237
238 cpufreq_device->cpufreq_state = cooling_state;
239 cpufreq_device->cpufreq_val = clip_freq;
240 notify_device = cpufreq_device;
241
242 for_each_cpu(cpuid, maskPtr) {
243 if (is_cpufreq_valid(cpuid))
244 cpufreq_update_policy(cpuid);
245 }
246
247 notify_device = NOTIFY_INVALID;
248
249 return 0;
250}
251
252/**
253 * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
254 * @nb: struct notifier_block * with callback info.
255 * @event: value showing cpufreq event for which this function invoked.
256 * @data: callback-specific data
257 */
258static int cpufreq_thermal_notifier(struct notifier_block *nb,
259 unsigned long event, void *data)
260{
261 struct cpufreq_policy *policy = data;
262 unsigned long max_freq = 0;
263
264 if (event != CPUFREQ_ADJUST || notify_device == NOTIFY_INVALID)
265 return 0;
266
267 if (cpumask_test_cpu(policy->cpu, &notify_device->allowed_cpus))
268 max_freq = notify_device->cpufreq_val;
269
270 /* Never exceed user_policy.max*/
271 if (max_freq > policy->user_policy.max)
272 max_freq = policy->user_policy.max;
273
274 if (policy->max != max_freq)
275 cpufreq_verify_within_limits(policy, 0, max_freq);
276
277 return 0;
278}
279
280/*
281 * cpufreq cooling device callback functions are defined below
282 */
283
284/**
285 * cpufreq_get_max_state - callback function to get the max cooling state.
286 * @cdev: thermal cooling device pointer.
287 * @state: fill this variable with the max cooling state.
288 */
289static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
290 unsigned long *state)
291{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100292 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
293 struct cpumask *maskPtr = &cpufreq_device->allowed_cpus;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530294 unsigned int cpu;
hongbo.zhang9c51b052012-10-30 17:48:58 +0100295 unsigned long count = 0;
Zhang Ruifc35b352013-02-08 13:09:32 +0800296 int ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530297
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530298 cpu = cpumask_any(maskPtr);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530299
Zhang Ruifc35b352013-02-08 13:09:32 +0800300 ret = get_property(cpu, 0, (unsigned int *)&count, GET_MAXL);
hongbo.zhang9c51b052012-10-30 17:48:58 +0100301
Zhang Ruifc35b352013-02-08 13:09:32 +0800302 if (count > 0)
303 *state = count;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530304
Zhang Ruifc35b352013-02-08 13:09:32 +0800305 return ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530306}
307
308/**
309 * cpufreq_get_cur_state - callback function to get the current cooling state.
310 * @cdev: thermal cooling device pointer.
311 * @state: fill this variable with the current cooling state.
312 */
313static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
314 unsigned long *state)
315{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100316 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530317
hongbo.zhang160b7d82012-10-30 17:48:59 +0100318 *state = cpufreq_device->cpufreq_state;
319 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530320}
321
322/**
323 * cpufreq_set_cur_state - callback function to set the current cooling state.
324 * @cdev: thermal cooling device pointer.
325 * @state: set this variable to the current cooling state.
326 */
327static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
328 unsigned long state)
329{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100330 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530331
hongbo.zhang160b7d82012-10-30 17:48:59 +0100332 return cpufreq_apply_cooling(cpufreq_device, state);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530333}
334
335/* Bind cpufreq callbacks to thermal cooling device ops */
336static struct thermal_cooling_device_ops const cpufreq_cooling_ops = {
337 .get_max_state = cpufreq_get_max_state,
338 .get_cur_state = cpufreq_get_cur_state,
339 .set_cur_state = cpufreq_set_cur_state,
340};
341
342/* Notifier for cpufreq policy change */
343static struct notifier_block thermal_cpufreq_notifier_block = {
344 .notifier_call = cpufreq_thermal_notifier,
345};
346
347/**
348 * cpufreq_cooling_register - function to create cpufreq cooling device.
349 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
350 */
351struct thermal_cooling_device *cpufreq_cooling_register(
Eduardo Valentin3778ff52012-11-12 15:58:50 +0000352 const struct cpumask *clip_cpus)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530353{
354 struct thermal_cooling_device *cool_dev;
355 struct cpufreq_cooling_device *cpufreq_dev = NULL;
hongbo.zhang160b7d82012-10-30 17:48:59 +0100356 unsigned int min = 0, max = 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530357 char dev_name[THERMAL_NAME_LENGTH];
Jonghwa Leea4b6fec2012-09-26 09:43:31 +0900358 int ret = 0, i;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530359 struct cpufreq_policy policy;
360
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530361 /*Verify that all the clip cpus have same freq_min, freq_max limit*/
362 for_each_cpu(i, clip_cpus) {
363 /*continue if cpufreq policy not found and not return error*/
364 if (!cpufreq_get_policy(&policy, i))
365 continue;
366 if (min == 0 && max == 0) {
367 min = policy.cpuinfo.min_freq;
368 max = policy.cpuinfo.max_freq;
369 } else {
370 if (min != policy.cpuinfo.min_freq ||
371 max != policy.cpuinfo.max_freq)
372 return ERR_PTR(-EINVAL);
hongbo.zhang6b6519d2012-10-30 17:48:57 +0100373 }
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530374 }
375 cpufreq_dev = kzalloc(sizeof(struct cpufreq_cooling_device),
376 GFP_KERNEL);
377 if (!cpufreq_dev)
378 return ERR_PTR(-ENOMEM);
379
380 cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
381
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530382 ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
383 if (ret) {
384 kfree(cpufreq_dev);
385 return ERR_PTR(-EINVAL);
386 }
387
388 sprintf(dev_name, "thermal-cpufreq-%d", cpufreq_dev->id);
389
390 cool_dev = thermal_cooling_device_register(dev_name, cpufreq_dev,
391 &cpufreq_cooling_ops);
392 if (!cool_dev) {
393 release_idr(&cpufreq_idr, cpufreq_dev->id);
394 kfree(cpufreq_dev);
395 return ERR_PTR(-EINVAL);
396 }
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530397 cpufreq_dev->cool_dev = cool_dev;
398 cpufreq_dev->cpufreq_state = 0;
399 mutex_lock(&cooling_cpufreq_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530400
401 /* Register the notifier for first cpufreq cooling device */
402 if (cpufreq_dev_count == 0)
403 cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
404 CPUFREQ_POLICY_NOTIFIER);
hongbo.zhang160b7d82012-10-30 17:48:59 +0100405 cpufreq_dev_count++;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530406
407 mutex_unlock(&cooling_cpufreq_lock);
408 return cool_dev;
409}
410EXPORT_SYMBOL(cpufreq_cooling_register);
411
412/**
413 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
414 * @cdev: thermal cooling device pointer.
415 */
416void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
417{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100418 struct cpufreq_cooling_device *cpufreq_dev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530419
420 mutex_lock(&cooling_cpufreq_lock);
hongbo.zhang160b7d82012-10-30 17:48:59 +0100421 cpufreq_dev_count--;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530422
423 /* Unregister the notifier for the last cpufreq cooling device */
hongbo.zhang160b7d82012-10-30 17:48:59 +0100424 if (cpufreq_dev_count == 0) {
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530425 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
426 CPUFREQ_POLICY_NOTIFIER);
427 }
428 mutex_unlock(&cooling_cpufreq_lock);
hongbo.zhang160b7d82012-10-30 17:48:59 +0100429
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530430 thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
431 release_idr(&cpufreq_idr, cpufreq_dev->id);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530432 kfree(cpufreq_dev);
433}
434EXPORT_SYMBOL(cpufreq_cooling_unregister);