]>
Commit | Line | Data |
---|---|---|
02361418 ADK |
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/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 | */ | |
51 | struct 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 | }; | |
59 | static LIST_HEAD(cooling_cpufreq_list); | |
60 | static DEFINE_IDR(cpufreq_idr); | |
160b7d80 | 61 | static DEFINE_MUTEX(cooling_cpufreq_lock); |
02361418 | 62 | |
160b7d80 | 63 | static unsigned int cpufreq_dev_count; |
02361418 ADK |
64 | |
65 | /* notify_table passes value to the CPUFREQ_ADJUST callback function. */ | |
66 | #define NOTIFY_INVALID NULL | |
a0f846c2 | 67 | static struct cpufreq_cooling_device *notify_device; |
02361418 ADK |
68 | |
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 | */ | |
74 | static int get_idr(struct idr *idr, int *id) | |
75 | { | |
6deb69fa | 76 | int ret; |
02361418 ADK |
77 | |
78 | mutex_lock(&cooling_cpufreq_lock); | |
6deb69fa | 79 | ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL); |
02361418 | 80 | mutex_unlock(&cooling_cpufreq_lock); |
6deb69fa TH |
81 | if (unlikely(ret < 0)) |
82 | return ret; | |
83 | *id = ret; | |
02361418 ADK |
84 | 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 | */ | |
92 | static 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 | */ | |
105 | static int is_cpufreq_valid(int cpu) | |
106 | { | |
107 | struct cpufreq_policy policy; | |
108 | return !cpufreq_get_policy(&policy, cpu); | |
109 | } | |
110 | ||
fc35b35c ZR |
111 | enum 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 | */ | |
128 | static int get_property(unsigned int cpu, unsigned long input, | |
129 | unsigned int* output, enum cpufreq_cooling_property property) | |
02361418 | 130 | { |
fc35b35c ZR |
131 | int i, j; |
132 | unsigned long max_level = 0, level; | |
133 | unsigned int freq = CPUFREQ_ENTRY_INVALID; | |
134 | int descend = -1; | |
02361418 ADK |
135 | struct cpufreq_frequency_table *table = |
136 | cpufreq_frequency_get_table(cpu); | |
fc35b35c ZR |
137 | |
138 | if (!output) | |
139 | return -EINVAL; | |
140 | ||
02361418 | 141 | if (!table) |
fc35b35c | 142 | return -EINVAL; |
02361418 | 143 | |
fc35b35c ZR |
144 | |
145 | for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) { | |
146 | /* ignore invalid entries */ | |
02361418 ADK |
147 | if (table[i].frequency == CPUFREQ_ENTRY_INVALID) |
148 | continue; | |
149 | ||
fc35b35c ZR |
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); | |
02361418 | 157 | |
fc35b35c ZR |
158 | freq = table[i].frequency; |
159 | max_level++; | |
02361418 | 160 | } |
02361418 | 161 | |
fc35b35c ZR |
162 | /* get max level */ |
163 | if (property == GET_MAXL) { | |
164 | *output = (unsigned int)max_level; | |
165 | return 0; | |
166 | } | |
02361418 | 167 | |
fc35b35c ZR |
168 | if (property == GET_FREQ) |
169 | level = descend ? input : (max_level - input -1); | |
170 | ||
02361418 | 171 | |
fc35b35c ZR |
172 | for (i = 0, j = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) { |
173 | /* ignore invalid entry */ | |
02361418 ADK |
174 | if (table[i].frequency == CPUFREQ_ENTRY_INVALID) |
175 | continue; | |
fc35b35c ZR |
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++; | |
02361418 | 195 | } |
fc35b35c ZR |
196 | return -EINVAL; |
197 | } | |
198 | ||
57df8106 ZR |
199 | unsigned 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 | ||
208 | EXPORT_SYMBOL(cpufreq_cooling_get_level); | |
209 | ||
fc35b35c ZR |
210 | /** |
211 | * get_cpu_frequency - get the absolute value of frequency from level. | |
212 | * @cpu: cpu for which frequency is fetched. | |
213 | * @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 | |
215 | */ | |
216 | static unsigned int get_cpu_frequency(unsigned int cpu, unsigned long level) | |
217 | { | |
218 | int ret = 0; | |
219 | unsigned int freq; | |
220 | ||
221 | ret = get_property(cpu, level, &freq, GET_FREQ); | |
222 | if (ret) | |
223 | return 0; | |
224 | return freq; | |
02361418 ADK |
225 | } |
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 | */ | |
233 | static int cpufreq_apply_cooling(struct cpufreq_cooling_device *cpufreq_device, | |
234 | unsigned long cooling_state) | |
235 | { | |
236 | unsigned int cpuid, clip_freq; | |
bde00663 LNM |
237 | struct cpumask *mask = &cpufreq_device->allowed_cpus; |
238 | unsigned int cpu = cpumask_any(mask); | |
02361418 ADK |
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 | ||
bde00663 | 253 | for_each_cpu(cpuid, mask) { |
02361418 ADK |
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 | */ | |
269 | static 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, ¬ify_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 | */ | |
300 | static int cpufreq_get_max_state(struct thermal_cooling_device *cdev, | |
301 | unsigned long *state) | |
302 | { | |
160b7d80 | 303 | struct cpufreq_cooling_device *cpufreq_device = cdev->devdata; |
bde00663 | 304 | struct cpumask *mask = &cpufreq_device->allowed_cpus; |
02361418 | 305 | unsigned int cpu; |
9c51b05a | 306 | unsigned long count = 0; |
fc35b35c | 307 | int ret; |
02361418 | 308 | |
bde00663 | 309 | cpu = cpumask_any(mask); |
02361418 | 310 | |
fc35b35c | 311 | ret = get_property(cpu, 0, (unsigned int *)&count, GET_MAXL); |
9c51b05a | 312 | |
fc35b35c ZR |
313 | if (count > 0) |
314 | *state = count; | |
02361418 | 315 | |
fc35b35c | 316 | return ret; |
02361418 ADK |
317 | } |
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 | */ | |
324 | static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev, | |
325 | unsigned long *state) | |
326 | { | |
160b7d80 | 327 | struct cpufreq_cooling_device *cpufreq_device = cdev->devdata; |
02361418 | 328 | |
160b7d80 | 329 | *state = cpufreq_device->cpufreq_state; |
330 | return 0; | |
02361418 ADK |
331 | } |
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 | */ | |
338 | static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev, | |
339 | unsigned long state) | |
340 | { | |
160b7d80 | 341 | struct cpufreq_cooling_device *cpufreq_device = cdev->devdata; |
02361418 | 342 | |
160b7d80 | 343 | return cpufreq_apply_cooling(cpufreq_device, state); |
02361418 ADK |
344 | } |
345 | ||
346 | /* Bind cpufreq callbacks to thermal cooling device ops */ | |
347 | static 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 */ | |
354 | static 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 | */ | |
362 | struct thermal_cooling_device *cpufreq_cooling_register( | |
3778ff5c | 363 | const struct cpumask *clip_cpus) |
02361418 ADK |
364 | { |
365 | struct thermal_cooling_device *cool_dev; | |
366 | struct cpufreq_cooling_device *cpufreq_dev = NULL; | |
160b7d80 | 367 | unsigned int min = 0, max = 0; |
02361418 | 368 | char dev_name[THERMAL_NAME_LENGTH]; |
a4b6fec9 | 369 | int ret = 0, i; |
02361418 ADK |
370 | struct cpufreq_policy policy; |
371 | ||
02361418 ADK |
372 | /*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); | |
6b6519df | 384 | } |
02361418 ADK |
385 | } |
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 | ||
02361418 ADK |
393 | 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 | } | |
02361418 ADK |
408 | cpufreq_dev->cool_dev = cool_dev; |
409 | cpufreq_dev->cpufreq_state = 0; | |
410 | mutex_lock(&cooling_cpufreq_lock); | |
02361418 ADK |
411 | |
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); | |
160b7d80 | 416 | cpufreq_dev_count++; |
02361418 ADK |
417 | |
418 | mutex_unlock(&cooling_cpufreq_lock); | |
419 | return cool_dev; | |
420 | } | |
421 | EXPORT_SYMBOL(cpufreq_cooling_register); | |
422 | ||
423 | /** | |
424 | * cpufreq_cooling_unregister - function to remove cpufreq cooling device. | |
425 | * @cdev: thermal cooling device pointer. | |
426 | */ | |
427 | void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev) | |
428 | { | |
160b7d80 | 429 | struct cpufreq_cooling_device *cpufreq_dev = cdev->devdata; |
02361418 ADK |
430 | |
431 | mutex_lock(&cooling_cpufreq_lock); | |
160b7d80 | 432 | cpufreq_dev_count--; |
02361418 ADK |
433 | |
434 | /* Unregister the notifier for the last cpufreq cooling device */ | |
160b7d80 | 435 | if (cpufreq_dev_count == 0) { |
02361418 ADK |
436 | cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block, |
437 | CPUFREQ_POLICY_NOTIFIER); | |
438 | } | |
439 | mutex_unlock(&cooling_cpufreq_lock); | |
160b7d80 | 440 | |
02361418 ADK |
441 | thermal_cooling_device_unregister(cpufreq_dev->cool_dev); |
442 | release_idr(&cpufreq_idr, cpufreq_dev->id); | |
02361418 ADK |
443 | kfree(cpufreq_dev); |
444 | } | |
445 | EXPORT_SYMBOL(cpufreq_cooling_unregister); |