blob: 8dc44cbb3e09fe253546347e376c0834957efc1e [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
111/**
112 * get_cpu_frequency - get the absolute value of frequency from level.
113 * @cpu: cpu for which frequency is fetched.
Zhang Rui475f41c2013-02-06 09:34:32 +0800114 * @level: level of frequency, equals cooling state of cpu cooling device
115 * e.g level=0 --> 1st MAX FREQ, level=1 ---> 2nd MAX FREQ, .... etc
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530116 */
117static unsigned int get_cpu_frequency(unsigned int cpu, unsigned long level)
118{
119 int ret = 0, i = 0;
120 unsigned long level_index;
121 bool descend = false;
122 struct cpufreq_frequency_table *table =
123 cpufreq_frequency_get_table(cpu);
124 if (!table)
125 return ret;
126
127 while (table[i].frequency != CPUFREQ_TABLE_END) {
128 if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
129 continue;
130
131 /*check if table in ascending or descending order*/
132 if ((table[i + 1].frequency != CPUFREQ_TABLE_END) &&
133 (table[i + 1].frequency < table[i].frequency)
134 && !descend) {
135 descend = true;
136 }
137
138 /*return if level matched and table in descending order*/
139 if (descend && i == level)
140 return table[i].frequency;
141 i++;
142 }
143 i--;
144
145 if (level > i || descend)
146 return ret;
147 level_index = i - level;
148
149 /*Scan the table in reverse order and match the level*/
150 while (i >= 0) {
151 if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
152 continue;
153 /*return if level matched*/
154 if (i == level_index)
155 return table[i].frequency;
156 i--;
157 }
158 return ret;
159}
160
161/**
162 * cpufreq_apply_cooling - function to apply frequency clipping.
163 * @cpufreq_device: cpufreq_cooling_device pointer containing frequency
164 * clipping data.
165 * @cooling_state: value of the cooling state.
166 */
167static int cpufreq_apply_cooling(struct cpufreq_cooling_device *cpufreq_device,
168 unsigned long cooling_state)
169{
170 unsigned int cpuid, clip_freq;
171 struct cpumask *maskPtr = &cpufreq_device->allowed_cpus;
172 unsigned int cpu = cpumask_any(maskPtr);
173
174
175 /* Check if the old cooling action is same as new cooling action */
176 if (cpufreq_device->cpufreq_state == cooling_state)
177 return 0;
178
179 clip_freq = get_cpu_frequency(cpu, cooling_state);
180 if (!clip_freq)
181 return -EINVAL;
182
183 cpufreq_device->cpufreq_state = cooling_state;
184 cpufreq_device->cpufreq_val = clip_freq;
185 notify_device = cpufreq_device;
186
187 for_each_cpu(cpuid, maskPtr) {
188 if (is_cpufreq_valid(cpuid))
189 cpufreq_update_policy(cpuid);
190 }
191
192 notify_device = NOTIFY_INVALID;
193
194 return 0;
195}
196
197/**
198 * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
199 * @nb: struct notifier_block * with callback info.
200 * @event: value showing cpufreq event for which this function invoked.
201 * @data: callback-specific data
202 */
203static int cpufreq_thermal_notifier(struct notifier_block *nb,
204 unsigned long event, void *data)
205{
206 struct cpufreq_policy *policy = data;
207 unsigned long max_freq = 0;
208
209 if (event != CPUFREQ_ADJUST || notify_device == NOTIFY_INVALID)
210 return 0;
211
212 if (cpumask_test_cpu(policy->cpu, &notify_device->allowed_cpus))
213 max_freq = notify_device->cpufreq_val;
214
215 /* Never exceed user_policy.max*/
216 if (max_freq > policy->user_policy.max)
217 max_freq = policy->user_policy.max;
218
219 if (policy->max != max_freq)
220 cpufreq_verify_within_limits(policy, 0, max_freq);
221
222 return 0;
223}
224
225/*
226 * cpufreq cooling device callback functions are defined below
227 */
228
229/**
230 * cpufreq_get_max_state - callback function to get the max cooling state.
231 * @cdev: thermal cooling device pointer.
232 * @state: fill this variable with the max cooling state.
233 */
234static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
235 unsigned long *state)
236{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100237 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
238 struct cpumask *maskPtr = &cpufreq_device->allowed_cpus;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530239 unsigned int cpu;
240 struct cpufreq_frequency_table *table;
hongbo.zhang9c51b052012-10-30 17:48:58 +0100241 unsigned long count = 0;
hongbo.zhang160b7d82012-10-30 17:48:59 +0100242 int i = 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530243
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530244 cpu = cpumask_any(maskPtr);
245 table = cpufreq_frequency_get_table(cpu);
246 if (!table) {
247 *state = 0;
hongbo.zhang160b7d82012-10-30 17:48:59 +0100248 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530249 }
250
hongbo.zhang9c51b052012-10-30 17:48:58 +0100251 for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530252 if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
253 continue;
hongbo.zhang9c51b052012-10-30 17:48:58 +0100254 count++;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530255 }
hongbo.zhang9c51b052012-10-30 17:48:58 +0100256
257 if (count > 0) {
258 *state = --count;
hongbo.zhang160b7d82012-10-30 17:48:59 +0100259 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530260 }
261
hongbo.zhang160b7d82012-10-30 17:48:59 +0100262 return -EINVAL;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530263}
264
265/**
266 * cpufreq_get_cur_state - callback function to get the current cooling state.
267 * @cdev: thermal cooling device pointer.
268 * @state: fill this variable with the current cooling state.
269 */
270static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
271 unsigned long *state)
272{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100273 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530274
hongbo.zhang160b7d82012-10-30 17:48:59 +0100275 *state = cpufreq_device->cpufreq_state;
276 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530277}
278
279/**
280 * cpufreq_set_cur_state - callback function to set the current cooling state.
281 * @cdev: thermal cooling device pointer.
282 * @state: set this variable to the current cooling state.
283 */
284static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
285 unsigned long state)
286{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100287 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530288
hongbo.zhang160b7d82012-10-30 17:48:59 +0100289 return cpufreq_apply_cooling(cpufreq_device, state);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530290}
291
292/* Bind cpufreq callbacks to thermal cooling device ops */
293static struct thermal_cooling_device_ops const cpufreq_cooling_ops = {
294 .get_max_state = cpufreq_get_max_state,
295 .get_cur_state = cpufreq_get_cur_state,
296 .set_cur_state = cpufreq_set_cur_state,
297};
298
299/* Notifier for cpufreq policy change */
300static struct notifier_block thermal_cpufreq_notifier_block = {
301 .notifier_call = cpufreq_thermal_notifier,
302};
303
304/**
305 * cpufreq_cooling_register - function to create cpufreq cooling device.
306 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
307 */
308struct thermal_cooling_device *cpufreq_cooling_register(
Eduardo Valentin3778ff52012-11-12 15:58:50 +0000309 const struct cpumask *clip_cpus)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530310{
311 struct thermal_cooling_device *cool_dev;
312 struct cpufreq_cooling_device *cpufreq_dev = NULL;
hongbo.zhang160b7d82012-10-30 17:48:59 +0100313 unsigned int min = 0, max = 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530314 char dev_name[THERMAL_NAME_LENGTH];
Jonghwa Leea4b6fec2012-09-26 09:43:31 +0900315 int ret = 0, i;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530316 struct cpufreq_policy policy;
317
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530318 /*Verify that all the clip cpus have same freq_min, freq_max limit*/
319 for_each_cpu(i, clip_cpus) {
320 /*continue if cpufreq policy not found and not return error*/
321 if (!cpufreq_get_policy(&policy, i))
322 continue;
323 if (min == 0 && max == 0) {
324 min = policy.cpuinfo.min_freq;
325 max = policy.cpuinfo.max_freq;
326 } else {
327 if (min != policy.cpuinfo.min_freq ||
328 max != policy.cpuinfo.max_freq)
329 return ERR_PTR(-EINVAL);
hongbo.zhang6b6519d2012-10-30 17:48:57 +0100330 }
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530331 }
332 cpufreq_dev = kzalloc(sizeof(struct cpufreq_cooling_device),
333 GFP_KERNEL);
334 if (!cpufreq_dev)
335 return ERR_PTR(-ENOMEM);
336
337 cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
338
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530339 ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
340 if (ret) {
341 kfree(cpufreq_dev);
342 return ERR_PTR(-EINVAL);
343 }
344
345 sprintf(dev_name, "thermal-cpufreq-%d", cpufreq_dev->id);
346
347 cool_dev = thermal_cooling_device_register(dev_name, cpufreq_dev,
348 &cpufreq_cooling_ops);
349 if (!cool_dev) {
350 release_idr(&cpufreq_idr, cpufreq_dev->id);
351 kfree(cpufreq_dev);
352 return ERR_PTR(-EINVAL);
353 }
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530354 cpufreq_dev->cool_dev = cool_dev;
355 cpufreq_dev->cpufreq_state = 0;
356 mutex_lock(&cooling_cpufreq_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530357
358 /* Register the notifier for first cpufreq cooling device */
359 if (cpufreq_dev_count == 0)
360 cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
361 CPUFREQ_POLICY_NOTIFIER);
hongbo.zhang160b7d82012-10-30 17:48:59 +0100362 cpufreq_dev_count++;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530363
364 mutex_unlock(&cooling_cpufreq_lock);
365 return cool_dev;
366}
367EXPORT_SYMBOL(cpufreq_cooling_register);
368
369/**
370 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
371 * @cdev: thermal cooling device pointer.
372 */
373void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
374{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100375 struct cpufreq_cooling_device *cpufreq_dev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530376
377 mutex_lock(&cooling_cpufreq_lock);
hongbo.zhang160b7d82012-10-30 17:48:59 +0100378 cpufreq_dev_count--;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530379
380 /* Unregister the notifier for the last cpufreq cooling device */
hongbo.zhang160b7d82012-10-30 17:48:59 +0100381 if (cpufreq_dev_count == 0) {
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530382 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
383 CPUFREQ_POLICY_NOTIFIER);
384 }
385 mutex_unlock(&cooling_cpufreq_lock);
hongbo.zhang160b7d82012-10-30 17:48:59 +0100386
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530387 thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
388 release_idr(&cpufreq_idr, cpufreq_dev->id);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530389 kfree(cpufreq_dev);
390}
391EXPORT_SYMBOL(cpufreq_cooling_unregister);