• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/module.h>
24 #include <linux/thermal.h>
25 #include <linux/cpufreq.h>
26 #include <linux/err.h>
27 #include <linux/slab.h>
28 #include <linux/cpu.h>
29 #include <linux/cpu_cooling.h>
30 
31 /**
32  * struct cpufreq_cooling_device - data for cooling device with cpufreq
33  * @id: unique integer value corresponding to each cpufreq_cooling_device
34  *	registered.
35  * @cool_dev: thermal_cooling_device pointer to keep track of the
36  *	registered cooling device.
37  * @cpufreq_state: integer value representing the current state of cpufreq
38  *	cooling	devices.
39  * @cpufreq_val: integer value representing the absolute value of the clipped
40  *	frequency.
41  * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
42  *
43  * This structure is required for keeping information of each
44  * cpufreq_cooling_device registered. In order to prevent corruption of this a
45  * mutex lock cooling_cpufreq_lock is used.
46  */
47 struct cpufreq_cooling_device {
48 	int id;
49 	struct thermal_cooling_device *cool_dev;
50 	unsigned int cpufreq_state;
51 	unsigned int cpufreq_val;
52 	struct cpumask allowed_cpus;
53 	struct list_head node;
54 };
55 static DEFINE_IDR(cpufreq_idr);
56 static DEFINE_MUTEX(cooling_cpufreq_lock);
57 
58 static unsigned int cpufreq_dev_count;
59 
60 static LIST_HEAD(cpufreq_dev_list);
61 
62 /**
63  * get_idr - function to get a unique id.
64  * @idr: struct idr * handle used to create a id.
65  * @id: int * value generated by this function.
66  *
67  * This function will populate @id with an unique
68  * id, using the idr API.
69  *
70  * Return: 0 on success, an error code on failure.
71  */
get_idr(struct idr * idr,int * id)72 static int get_idr(struct idr *idr, int *id)
73 {
74 	int ret;
75 
76 	mutex_lock(&cooling_cpufreq_lock);
77 	ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
78 	mutex_unlock(&cooling_cpufreq_lock);
79 	if (unlikely(ret < 0))
80 		return ret;
81 	*id = ret;
82 
83 	return 0;
84 }
85 
86 /**
87  * release_idr - function to free the unique id.
88  * @idr: struct idr * handle used for creating the id.
89  * @id: int value representing the unique id.
90  */
release_idr(struct idr * idr,int id)91 static void release_idr(struct idr *idr, int id)
92 {
93 	mutex_lock(&cooling_cpufreq_lock);
94 	idr_remove(idr, id);
95 	mutex_unlock(&cooling_cpufreq_lock);
96 }
97 
98 /* Below code defines functions to be used for cpufreq as cooling device */
99 
100 /**
101  * is_cpufreq_valid - function to check frequency transitioning capability.
102  * @cpu: cpu for which check is needed.
103  *
104  * This function will check the current state of the system if
105  * it is capable of changing the frequency for a given @cpu.
106  *
107  * Return: 0 if the system is not currently capable of changing
108  * the frequency of given cpu. !0 in case the frequency is changeable.
109  */
is_cpufreq_valid(int cpu)110 static int is_cpufreq_valid(int cpu)
111 {
112 	struct cpufreq_policy policy;
113 
114 	return !cpufreq_get_policy(&policy, cpu);
115 }
116 
117 enum cpufreq_cooling_property {
118 	GET_LEVEL,
119 	GET_FREQ,
120 	GET_MAXL,
121 };
122 
123 /**
124  * get_property - fetch a property of interest for a give cpu.
125  * @cpu: cpu for which the property is required
126  * @input: query parameter
127  * @output: query return
128  * @property: type of query (frequency, level, max level)
129  *
130  * This is the common function to
131  * 1. get maximum cpu cooling states
132  * 2. translate frequency to cooling state
133  * 3. translate cooling state to frequency
134  * Note that the code may be not in good shape
135  * but it is written in this way in order to:
136  * a) reduce duplicate code as most of the code can be shared.
137  * b) make sure the logic is consistent when translating between
138  *    cooling states and frequencies.
139  *
140  * Return: 0 on success, -EINVAL when invalid parameters are passed.
141  */
get_property(unsigned int cpu,unsigned long input,unsigned int * output,enum cpufreq_cooling_property property)142 static int get_property(unsigned int cpu, unsigned long input,
143 			unsigned int *output,
144 			enum cpufreq_cooling_property property)
145 {
146 	int i;
147 	unsigned long max_level = 0, level = 0;
148 	unsigned int freq = CPUFREQ_ENTRY_INVALID;
149 	int descend = -1;
150 	struct cpufreq_frequency_table *pos, *table =
151 					cpufreq_frequency_get_table(cpu);
152 
153 	if (!output)
154 		return -EINVAL;
155 
156 	if (!table)
157 		return -EINVAL;
158 
159 	cpufreq_for_each_valid_entry(pos, table) {
160 		/* ignore duplicate entry */
161 		if (freq == pos->frequency)
162 			continue;
163 
164 		/* get the frequency order */
165 		if (freq != CPUFREQ_ENTRY_INVALID && descend == -1)
166 			descend = freq > pos->frequency;
167 
168 		freq = pos->frequency;
169 		max_level++;
170 	}
171 
172 	/* No valid cpu frequency entry */
173 	if (max_level == 0)
174 		return -EINVAL;
175 
176 	/* max_level is an index, not a counter */
177 	max_level--;
178 
179 	/* get max level */
180 	if (property == GET_MAXL) {
181 		*output = (unsigned int)max_level;
182 		return 0;
183 	}
184 
185 	if (property == GET_FREQ)
186 		level = descend ? input : (max_level - input);
187 
188 	i = 0;
189 	cpufreq_for_each_valid_entry(pos, table) {
190 		/* ignore duplicate entry */
191 		if (freq == pos->frequency)
192 			continue;
193 
194 		/* now we have a valid frequency entry */
195 		freq = pos->frequency;
196 
197 		if (property == GET_LEVEL && (unsigned int)input == freq) {
198 			/* get level by frequency */
199 			*output = descend ? i : (max_level - i);
200 			return 0;
201 		}
202 		if (property == GET_FREQ && level == i) {
203 			/* get frequency by level */
204 			*output = freq;
205 			return 0;
206 		}
207 		i++;
208 	}
209 
210 	return -EINVAL;
211 }
212 
213 /**
214  * cpufreq_cooling_get_level - for a give cpu, return the cooling level.
215  * @cpu: cpu for which the level is required
216  * @freq: the frequency of interest
217  *
218  * This function will match the cooling level corresponding to the
219  * requested @freq and return it.
220  *
221  * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID
222  * otherwise.
223  */
cpufreq_cooling_get_level(unsigned int cpu,unsigned int freq)224 unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq)
225 {
226 	unsigned int val;
227 
228 	if (get_property(cpu, (unsigned long)freq, &val, GET_LEVEL))
229 		return THERMAL_CSTATE_INVALID;
230 
231 	return (unsigned long)val;
232 }
233 EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level);
234 
235 /**
236  * get_cpu_frequency - get the absolute value of frequency from level.
237  * @cpu: cpu for which frequency is fetched.
238  * @level: cooling level
239  *
240  * This function matches cooling level with frequency. Based on a cooling level
241  * of frequency, equals cooling state of cpu cooling device, it will return
242  * the corresponding frequency.
243  *	e.g level=0 --> 1st MAX FREQ, level=1 ---> 2nd MAX FREQ, .... etc
244  *
245  * Return: 0 on error, the corresponding frequency otherwise.
246  */
get_cpu_frequency(unsigned int cpu,unsigned long level)247 static unsigned int get_cpu_frequency(unsigned int cpu, unsigned long level)
248 {
249 	int ret = 0;
250 	unsigned int freq;
251 
252 	ret = get_property(cpu, level, &freq, GET_FREQ);
253 	if (ret)
254 		return 0;
255 
256 	return freq;
257 }
258 
259 /**
260  * cpufreq_apply_cooling - function to apply frequency clipping.
261  * @cpufreq_device: cpufreq_cooling_device pointer containing frequency
262  *	clipping data.
263  * @cooling_state: value of the cooling state.
264  *
265  * Function used to make sure the cpufreq layer is aware of current thermal
266  * limits. The limits are applied by updating the cpufreq policy.
267  *
268  * Return: 0 on success, an error code otherwise (-EINVAL in case wrong
269  * cooling state).
270  */
cpufreq_apply_cooling(struct cpufreq_cooling_device * cpufreq_device,unsigned long cooling_state)271 static int cpufreq_apply_cooling(struct cpufreq_cooling_device *cpufreq_device,
272 				 unsigned long cooling_state)
273 {
274 	unsigned int cpuid, clip_freq;
275 	struct cpumask *mask = &cpufreq_device->allowed_cpus;
276 	unsigned int cpu = cpumask_any(mask);
277 
278 
279 	/* Check if the old cooling action is same as new cooling action */
280 	if (cpufreq_device->cpufreq_state == cooling_state)
281 		return 0;
282 
283 	clip_freq = get_cpu_frequency(cpu, cooling_state);
284 	if (!clip_freq)
285 		return -EINVAL;
286 
287 	cpufreq_device->cpufreq_state = cooling_state;
288 	cpufreq_device->cpufreq_val = clip_freq;
289 
290 	for_each_cpu(cpuid, mask) {
291 		if (is_cpufreq_valid(cpuid))
292 			cpufreq_update_policy(cpuid);
293 	}
294 
295 	return 0;
296 }
297 
298 /**
299  * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
300  * @nb:	struct notifier_block * with callback info.
301  * @event: value showing cpufreq event for which this function invoked.
302  * @data: callback-specific data
303  *
304  * Callback to hijack the notification on cpufreq policy transition.
305  * Every time there is a change in policy, we will intercept and
306  * update the cpufreq policy with thermal constraints.
307  *
308  * Return: 0 (success)
309  */
cpufreq_thermal_notifier(struct notifier_block * nb,unsigned long event,void * data)310 static int cpufreq_thermal_notifier(struct notifier_block *nb,
311 				    unsigned long event, void *data)
312 {
313 	struct cpufreq_policy *policy = data;
314 	unsigned long max_freq = 0;
315 	struct cpufreq_cooling_device *cpufreq_dev;
316 
317 	if (event != CPUFREQ_ADJUST)
318 		return 0;
319 
320 	mutex_lock(&cooling_cpufreq_lock);
321 	list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
322 		if (!cpumask_test_cpu(policy->cpu,
323 					&cpufreq_dev->allowed_cpus))
324 			continue;
325 
326 		if (!cpufreq_dev->cpufreq_val)
327 			cpufreq_dev->cpufreq_val = get_cpu_frequency(
328 					cpumask_any(&cpufreq_dev->allowed_cpus),
329 					cpufreq_dev->cpufreq_state);
330 
331 		max_freq = cpufreq_dev->cpufreq_val;
332 
333 		if (policy->max != max_freq)
334 			cpufreq_verify_within_limits(policy, 0, max_freq);
335 	}
336 	mutex_unlock(&cooling_cpufreq_lock);
337 
338 	return 0;
339 }
340 
341 /* cpufreq cooling device callback functions are defined below */
342 
343 /**
344  * cpufreq_get_max_state - callback function to get the max cooling state.
345  * @cdev: thermal cooling device pointer.
346  * @state: fill this variable with the max cooling state.
347  *
348  * Callback for the thermal cooling device to return the cpufreq
349  * max cooling state.
350  *
351  * Return: 0 on success, an error code otherwise.
352  */
cpufreq_get_max_state(struct thermal_cooling_device * cdev,unsigned long * state)353 static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
354 				 unsigned long *state)
355 {
356 	struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
357 	struct cpumask *mask = &cpufreq_device->allowed_cpus;
358 	unsigned int cpu;
359 	unsigned int count = 0;
360 	int ret;
361 
362 	cpu = cpumask_any(mask);
363 
364 	ret = get_property(cpu, 0, &count, GET_MAXL);
365 
366 	if (count > 0)
367 		*state = count;
368 
369 	return ret;
370 }
371 
372 /**
373  * cpufreq_get_cur_state - callback function to get the current cooling state.
374  * @cdev: thermal cooling device pointer.
375  * @state: fill this variable with the current cooling state.
376  *
377  * Callback for the thermal cooling device to return the cpufreq
378  * current cooling state.
379  *
380  * Return: 0 on success, an error code otherwise.
381  */
cpufreq_get_cur_state(struct thermal_cooling_device * cdev,unsigned long * state)382 static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
383 				 unsigned long *state)
384 {
385 	struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
386 
387 	*state = cpufreq_device->cpufreq_state;
388 
389 	return 0;
390 }
391 
392 /**
393  * cpufreq_set_cur_state - callback function to set the current cooling state.
394  * @cdev: thermal cooling device pointer.
395  * @state: set this variable to the current cooling state.
396  *
397  * Callback for the thermal cooling device to change the cpufreq
398  * current cooling state.
399  *
400  * Return: 0 on success, an error code otherwise.
401  */
cpufreq_set_cur_state(struct thermal_cooling_device * cdev,unsigned long state)402 static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
403 				 unsigned long state)
404 {
405 	struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
406 
407 	return cpufreq_apply_cooling(cpufreq_device, state);
408 }
409 
410 /* Bind cpufreq callbacks to thermal cooling device ops */
411 static struct thermal_cooling_device_ops const cpufreq_cooling_ops = {
412 	.get_max_state = cpufreq_get_max_state,
413 	.get_cur_state = cpufreq_get_cur_state,
414 	.set_cur_state = cpufreq_set_cur_state,
415 };
416 
417 /* Notifier for cpufreq policy change */
418 static struct notifier_block thermal_cpufreq_notifier_block = {
419 	.notifier_call = cpufreq_thermal_notifier,
420 };
421 
422 /**
423  * __cpufreq_cooling_register - helper function to create cpufreq cooling device
424  * @np: a valid struct device_node to the cooling device device tree node
425  * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
426  *
427  * This interface function registers the cpufreq cooling device with the name
428  * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
429  * cooling devices. It also gives the opportunity to link the cooling device
430  * with a device tree node, in order to bind it via the thermal DT code.
431  *
432  * Return: a valid struct thermal_cooling_device pointer on success,
433  * on failure, it returns a corresponding ERR_PTR().
434  */
435 static struct thermal_cooling_device *
__cpufreq_cooling_register(struct device_node * np,const struct cpumask * clip_cpus)436 __cpufreq_cooling_register(struct device_node *np,
437 			   const struct cpumask *clip_cpus)
438 {
439 	struct thermal_cooling_device *cool_dev;
440 	struct cpufreq_cooling_device *cpufreq_dev = NULL;
441 	unsigned int min = 0, max = 0;
442 	char dev_name[THERMAL_NAME_LENGTH];
443 	int ret = 0, i;
444 	struct cpufreq_policy policy;
445 
446 	/* Verify that all the clip cpus have same freq_min, freq_max limit */
447 	for_each_cpu(i, clip_cpus) {
448 		/* continue if cpufreq policy not found and not return error */
449 		if (!cpufreq_get_policy(&policy, i))
450 			continue;
451 		if (min == 0 && max == 0) {
452 			min = policy.cpuinfo.min_freq;
453 			max = policy.cpuinfo.max_freq;
454 		} else {
455 			if (min != policy.cpuinfo.min_freq ||
456 			    max != policy.cpuinfo.max_freq)
457 				return ERR_PTR(-EINVAL);
458 		}
459 	}
460 	cpufreq_dev = kzalloc(sizeof(struct cpufreq_cooling_device),
461 			      GFP_KERNEL);
462 	if (!cpufreq_dev)
463 		return ERR_PTR(-ENOMEM);
464 
465 	cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
466 
467 	ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
468 	if (ret) {
469 		kfree(cpufreq_dev);
470 		return ERR_PTR(-EINVAL);
471 	}
472 
473 	snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
474 		 cpufreq_dev->id);
475 
476 	cool_dev = thermal_of_cooling_device_register(np, dev_name, cpufreq_dev,
477 						      &cpufreq_cooling_ops);
478 	if (IS_ERR(cool_dev)) {
479 		release_idr(&cpufreq_idr, cpufreq_dev->id);
480 		kfree(cpufreq_dev);
481 		return cool_dev;
482 	}
483 	cpufreq_dev->cool_dev = cool_dev;
484 	cpufreq_dev->cpufreq_state = 0;
485 	mutex_lock(&cooling_cpufreq_lock);
486 
487 	/* Register the notifier for first cpufreq cooling device */
488 	if (cpufreq_dev_count == 0)
489 		cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
490 					  CPUFREQ_POLICY_NOTIFIER);
491 	cpufreq_dev_count++;
492 	list_add(&cpufreq_dev->node, &cpufreq_dev_list);
493 
494 	mutex_unlock(&cooling_cpufreq_lock);
495 
496 	return cool_dev;
497 }
498 
499 /**
500  * cpufreq_cooling_register - function to create cpufreq cooling device.
501  * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
502  *
503  * This interface function registers the cpufreq cooling device with the name
504  * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
505  * cooling devices.
506  *
507  * Return: a valid struct thermal_cooling_device pointer on success,
508  * on failure, it returns a corresponding ERR_PTR().
509  */
510 struct thermal_cooling_device *
cpufreq_cooling_register(const struct cpumask * clip_cpus)511 cpufreq_cooling_register(const struct cpumask *clip_cpus)
512 {
513 	return __cpufreq_cooling_register(NULL, clip_cpus);
514 }
515 EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
516 
517 /**
518  * of_cpufreq_cooling_register - function to create cpufreq cooling device.
519  * @np: a valid struct device_node to the cooling device device tree node
520  * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
521  *
522  * This interface function registers the cpufreq cooling device with the name
523  * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
524  * cooling devices. Using this API, the cpufreq cooling device will be
525  * linked to the device tree node provided.
526  *
527  * Return: a valid struct thermal_cooling_device pointer on success,
528  * on failure, it returns a corresponding ERR_PTR().
529  */
530 struct thermal_cooling_device *
of_cpufreq_cooling_register(struct device_node * np,const struct cpumask * clip_cpus)531 of_cpufreq_cooling_register(struct device_node *np,
532 			    const struct cpumask *clip_cpus)
533 {
534 	if (!np)
535 		return ERR_PTR(-EINVAL);
536 
537 	return __cpufreq_cooling_register(np, clip_cpus);
538 }
539 EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
540 
541 /**
542  * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
543  * @cdev: thermal cooling device pointer.
544  *
545  * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
546  */
cpufreq_cooling_unregister(struct thermal_cooling_device * cdev)547 void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
548 {
549 	struct cpufreq_cooling_device *cpufreq_dev;
550 
551 	if (!cdev)
552 		return;
553 
554 	cpufreq_dev = cdev->devdata;
555 	mutex_lock(&cooling_cpufreq_lock);
556 	list_del(&cpufreq_dev->node);
557 	cpufreq_dev_count--;
558 
559 	/* Unregister the notifier for the last cpufreq cooling device */
560 	if (cpufreq_dev_count == 0)
561 		cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
562 					    CPUFREQ_POLICY_NOTIFIER);
563 	mutex_unlock(&cooling_cpufreq_lock);
564 
565 	thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
566 	release_idr(&cpufreq_idr, cpufreq_dev->id);
567 	kfree(cpufreq_dev);
568 }
569 EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);
570