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 | { | |
76 | int err; | |
77 | again: | |
78 | if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0)) | |
79 | return -ENOMEM; | |
80 | ||
81 | mutex_lock(&cooling_cpufreq_lock); | |
82 | err = idr_get_new(idr, NULL, id); | |
83 | mutex_unlock(&cooling_cpufreq_lock); | |
84 | ||
85 | if (unlikely(err == -EAGAIN)) | |
86 | goto again; | |
87 | else if (unlikely(err)) | |
88 | return err; | |
89 | ||
29b19e25 | 90 | *id = *id & MAX_IDR_MASK; |
02361418 ADK |
91 | return 0; |
92 | } | |
93 | ||
94 | /** | |
95 | * release_idr - function to free the unique id. | |
96 | * @idr: struct idr * handle used for creating the id. | |
97 | * @id: int value representing the unique id. | |
98 | */ | |
99 | static void release_idr(struct idr *idr, int id) | |
100 | { | |
101 | mutex_lock(&cooling_cpufreq_lock); | |
102 | idr_remove(idr, id); | |
103 | mutex_unlock(&cooling_cpufreq_lock); | |
104 | } | |
105 | ||
106 | /* Below code defines functions to be used for cpufreq as cooling device */ | |
107 | ||
108 | /** | |
109 | * is_cpufreq_valid - function to check if a cpu has frequency transition policy. | |
110 | * @cpu: cpu for which check is needed. | |
111 | */ | |
112 | static int is_cpufreq_valid(int cpu) | |
113 | { | |
114 | struct cpufreq_policy policy; | |
115 | return !cpufreq_get_policy(&policy, cpu); | |
116 | } | |
117 | ||
118 | /** | |
119 | * get_cpu_frequency - get the absolute value of frequency from level. | |
120 | * @cpu: cpu for which frequency is fetched. | |
121 | * @level: level of frequency of the CPU | |
122 | * e.g level=1 --> 1st MAX FREQ, LEVEL=2 ---> 2nd MAX FREQ, .... etc | |
123 | */ | |
124 | static unsigned int get_cpu_frequency(unsigned int cpu, unsigned long level) | |
125 | { | |
126 | int ret = 0, i = 0; | |
127 | unsigned long level_index; | |
128 | bool descend = false; | |
129 | struct cpufreq_frequency_table *table = | |
130 | cpufreq_frequency_get_table(cpu); | |
131 | if (!table) | |
132 | return ret; | |
133 | ||
134 | while (table[i].frequency != CPUFREQ_TABLE_END) { | |
135 | if (table[i].frequency == CPUFREQ_ENTRY_INVALID) | |
136 | continue; | |
137 | ||
138 | /*check if table in ascending or descending order*/ | |
139 | if ((table[i + 1].frequency != CPUFREQ_TABLE_END) && | |
140 | (table[i + 1].frequency < table[i].frequency) | |
141 | && !descend) { | |
142 | descend = true; | |
143 | } | |
144 | ||
145 | /*return if level matched and table in descending order*/ | |
146 | if (descend && i == level) | |
147 | return table[i].frequency; | |
148 | i++; | |
149 | } | |
150 | i--; | |
151 | ||
152 | if (level > i || descend) | |
153 | return ret; | |
154 | level_index = i - level; | |
155 | ||
156 | /*Scan the table in reverse order and match the level*/ | |
157 | while (i >= 0) { | |
158 | if (table[i].frequency == CPUFREQ_ENTRY_INVALID) | |
159 | continue; | |
160 | /*return if level matched*/ | |
161 | if (i == level_index) | |
162 | return table[i].frequency; | |
163 | i--; | |
164 | } | |
165 | return ret; | |
166 | } | |
167 | ||
168 | /** | |
169 | * cpufreq_apply_cooling - function to apply frequency clipping. | |
170 | * @cpufreq_device: cpufreq_cooling_device pointer containing frequency | |
171 | * clipping data. | |
172 | * @cooling_state: value of the cooling state. | |
173 | */ | |
174 | static int cpufreq_apply_cooling(struct cpufreq_cooling_device *cpufreq_device, | |
175 | unsigned long cooling_state) | |
176 | { | |
177 | unsigned int cpuid, clip_freq; | |
178 | struct cpumask *maskPtr = &cpufreq_device->allowed_cpus; | |
179 | unsigned int cpu = cpumask_any(maskPtr); | |
180 | ||
181 | ||
182 | /* Check if the old cooling action is same as new cooling action */ | |
183 | if (cpufreq_device->cpufreq_state == cooling_state) | |
184 | return 0; | |
185 | ||
186 | clip_freq = get_cpu_frequency(cpu, cooling_state); | |
187 | if (!clip_freq) | |
188 | return -EINVAL; | |
189 | ||
190 | cpufreq_device->cpufreq_state = cooling_state; | |
191 | cpufreq_device->cpufreq_val = clip_freq; | |
192 | notify_device = cpufreq_device; | |
193 | ||
194 | for_each_cpu(cpuid, maskPtr) { | |
195 | if (is_cpufreq_valid(cpuid)) | |
196 | cpufreq_update_policy(cpuid); | |
197 | } | |
198 | ||
199 | notify_device = NOTIFY_INVALID; | |
200 | ||
201 | return 0; | |
202 | } | |
203 | ||
204 | /** | |
205 | * cpufreq_thermal_notifier - notifier callback for cpufreq policy change. | |
206 | * @nb: struct notifier_block * with callback info. | |
207 | * @event: value showing cpufreq event for which this function invoked. | |
208 | * @data: callback-specific data | |
209 | */ | |
210 | static int cpufreq_thermal_notifier(struct notifier_block *nb, | |
211 | unsigned long event, void *data) | |
212 | { | |
213 | struct cpufreq_policy *policy = data; | |
214 | unsigned long max_freq = 0; | |
215 | ||
216 | if (event != CPUFREQ_ADJUST || notify_device == NOTIFY_INVALID) | |
217 | return 0; | |
218 | ||
219 | if (cpumask_test_cpu(policy->cpu, ¬ify_device->allowed_cpus)) | |
220 | max_freq = notify_device->cpufreq_val; | |
221 | ||
222 | /* Never exceed user_policy.max*/ | |
223 | if (max_freq > policy->user_policy.max) | |
224 | max_freq = policy->user_policy.max; | |
225 | ||
226 | if (policy->max != max_freq) | |
227 | cpufreq_verify_within_limits(policy, 0, max_freq); | |
228 | ||
229 | return 0; | |
230 | } | |
231 | ||
232 | /* | |
233 | * cpufreq cooling device callback functions are defined below | |
234 | */ | |
235 | ||
236 | /** | |
237 | * cpufreq_get_max_state - callback function to get the max cooling state. | |
238 | * @cdev: thermal cooling device pointer. | |
239 | * @state: fill this variable with the max cooling state. | |
240 | */ | |
241 | static int cpufreq_get_max_state(struct thermal_cooling_device *cdev, | |
242 | unsigned long *state) | |
243 | { | |
160b7d80 | 244 | struct cpufreq_cooling_device *cpufreq_device = cdev->devdata; |
245 | struct cpumask *maskPtr = &cpufreq_device->allowed_cpus; | |
02361418 ADK |
246 | unsigned int cpu; |
247 | struct cpufreq_frequency_table *table; | |
9c51b05a | 248 | unsigned long count = 0; |
160b7d80 | 249 | int i = 0; |
02361418 | 250 | |
02361418 ADK |
251 | cpu = cpumask_any(maskPtr); |
252 | table = cpufreq_frequency_get_table(cpu); | |
253 | if (!table) { | |
254 | *state = 0; | |
160b7d80 | 255 | return 0; |
02361418 ADK |
256 | } |
257 | ||
9c51b05a | 258 | for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) { |
02361418 ADK |
259 | if (table[i].frequency == CPUFREQ_ENTRY_INVALID) |
260 | continue; | |
9c51b05a | 261 | count++; |
02361418 | 262 | } |
9c51b05a | 263 | |
264 | if (count > 0) { | |
265 | *state = --count; | |
160b7d80 | 266 | return 0; |
02361418 ADK |
267 | } |
268 | ||
160b7d80 | 269 | return -EINVAL; |
02361418 ADK |
270 | } |
271 | ||
272 | /** | |
273 | * cpufreq_get_cur_state - callback function to get the current cooling state. | |
274 | * @cdev: thermal cooling device pointer. | |
275 | * @state: fill this variable with the current cooling state. | |
276 | */ | |
277 | static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev, | |
278 | unsigned long *state) | |
279 | { | |
160b7d80 | 280 | struct cpufreq_cooling_device *cpufreq_device = cdev->devdata; |
02361418 | 281 | |
160b7d80 | 282 | *state = cpufreq_device->cpufreq_state; |
283 | return 0; | |
02361418 ADK |
284 | } |
285 | ||
286 | /** | |
287 | * cpufreq_set_cur_state - callback function to set the current cooling state. | |
288 | * @cdev: thermal cooling device pointer. | |
289 | * @state: set this variable to the current cooling state. | |
290 | */ | |
291 | static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev, | |
292 | unsigned long state) | |
293 | { | |
160b7d80 | 294 | struct cpufreq_cooling_device *cpufreq_device = cdev->devdata; |
02361418 | 295 | |
160b7d80 | 296 | return cpufreq_apply_cooling(cpufreq_device, state); |
02361418 ADK |
297 | } |
298 | ||
299 | /* Bind cpufreq callbacks to thermal cooling device ops */ | |
300 | static struct thermal_cooling_device_ops const cpufreq_cooling_ops = { | |
301 | .get_max_state = cpufreq_get_max_state, | |
302 | .get_cur_state = cpufreq_get_cur_state, | |
303 | .set_cur_state = cpufreq_set_cur_state, | |
304 | }; | |
305 | ||
306 | /* Notifier for cpufreq policy change */ | |
307 | static struct notifier_block thermal_cpufreq_notifier_block = { | |
308 | .notifier_call = cpufreq_thermal_notifier, | |
309 | }; | |
310 | ||
311 | /** | |
312 | * cpufreq_cooling_register - function to create cpufreq cooling device. | |
313 | * @clip_cpus: cpumask of cpus where the frequency constraints will happen. | |
314 | */ | |
315 | struct thermal_cooling_device *cpufreq_cooling_register( | |
3778ff5c | 316 | const struct cpumask *clip_cpus) |
02361418 ADK |
317 | { |
318 | struct thermal_cooling_device *cool_dev; | |
319 | struct cpufreq_cooling_device *cpufreq_dev = NULL; | |
160b7d80 | 320 | unsigned int min = 0, max = 0; |
02361418 | 321 | char dev_name[THERMAL_NAME_LENGTH]; |
a4b6fec9 | 322 | int ret = 0, i; |
02361418 ADK |
323 | struct cpufreq_policy policy; |
324 | ||
02361418 ADK |
325 | /*Verify that all the clip cpus have same freq_min, freq_max limit*/ |
326 | for_each_cpu(i, clip_cpus) { | |
327 | /*continue if cpufreq policy not found and not return error*/ | |
328 | if (!cpufreq_get_policy(&policy, i)) | |
329 | continue; | |
330 | if (min == 0 && max == 0) { | |
331 | min = policy.cpuinfo.min_freq; | |
332 | max = policy.cpuinfo.max_freq; | |
333 | } else { | |
334 | if (min != policy.cpuinfo.min_freq || | |
335 | max != policy.cpuinfo.max_freq) | |
336 | return ERR_PTR(-EINVAL); | |
6b6519df | 337 | } |
02361418 ADK |
338 | } |
339 | cpufreq_dev = kzalloc(sizeof(struct cpufreq_cooling_device), | |
340 | GFP_KERNEL); | |
341 | if (!cpufreq_dev) | |
342 | return ERR_PTR(-ENOMEM); | |
343 | ||
344 | cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus); | |
345 | ||
02361418 ADK |
346 | ret = get_idr(&cpufreq_idr, &cpufreq_dev->id); |
347 | if (ret) { | |
348 | kfree(cpufreq_dev); | |
349 | return ERR_PTR(-EINVAL); | |
350 | } | |
351 | ||
352 | sprintf(dev_name, "thermal-cpufreq-%d", cpufreq_dev->id); | |
353 | ||
354 | cool_dev = thermal_cooling_device_register(dev_name, cpufreq_dev, | |
355 | &cpufreq_cooling_ops); | |
356 | if (!cool_dev) { | |
357 | release_idr(&cpufreq_idr, cpufreq_dev->id); | |
358 | kfree(cpufreq_dev); | |
359 | return ERR_PTR(-EINVAL); | |
360 | } | |
02361418 ADK |
361 | cpufreq_dev->cool_dev = cool_dev; |
362 | cpufreq_dev->cpufreq_state = 0; | |
363 | mutex_lock(&cooling_cpufreq_lock); | |
02361418 ADK |
364 | |
365 | /* Register the notifier for first cpufreq cooling device */ | |
366 | if (cpufreq_dev_count == 0) | |
367 | cpufreq_register_notifier(&thermal_cpufreq_notifier_block, | |
368 | CPUFREQ_POLICY_NOTIFIER); | |
160b7d80 | 369 | cpufreq_dev_count++; |
02361418 ADK |
370 | |
371 | mutex_unlock(&cooling_cpufreq_lock); | |
372 | return cool_dev; | |
373 | } | |
374 | EXPORT_SYMBOL(cpufreq_cooling_register); | |
375 | ||
376 | /** | |
377 | * cpufreq_cooling_unregister - function to remove cpufreq cooling device. | |
378 | * @cdev: thermal cooling device pointer. | |
379 | */ | |
380 | void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev) | |
381 | { | |
160b7d80 | 382 | struct cpufreq_cooling_device *cpufreq_dev = cdev->devdata; |
02361418 ADK |
383 | |
384 | mutex_lock(&cooling_cpufreq_lock); | |
160b7d80 | 385 | cpufreq_dev_count--; |
02361418 ADK |
386 | |
387 | /* Unregister the notifier for the last cpufreq cooling device */ | |
160b7d80 | 388 | if (cpufreq_dev_count == 0) { |
02361418 ADK |
389 | cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block, |
390 | CPUFREQ_POLICY_NOTIFIER); | |
391 | } | |
392 | mutex_unlock(&cooling_cpufreq_lock); | |
160b7d80 | 393 | |
02361418 ADK |
394 | thermal_cooling_device_unregister(cpufreq_dev->cool_dev); |
395 | release_idr(&cpufreq_idr, cpufreq_dev->id); | |
02361418 ADK |
396 | kfree(cpufreq_dev); |
397 | } | |
398 | EXPORT_SYMBOL(cpufreq_cooling_unregister); |