1 /*
2  * devfreq_cooling: Thermal cooling device implementation for devices using
3  *                  devfreq
4  *
5  * Copyright (C) 2014-2015 ARM Limited
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
12  * kind, whether express or implied; without even the implied warranty
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * TODO:
17  *    - If OPPs are added or removed after devfreq cooling has
18  *      registered, the devfreq cooling won't react to it.
19  */
20 
21 #include <linux/devfreq.h>
22 #include <linux/devfreq_cooling.h>
23 #include <linux/export.h>
24 #include <linux/slab.h>
25 #include <linux/pm_opp.h>
26 #include <linux/thermal.h>
27 
28 #include <trace/events/thermal.h>
29 
30 static DEFINE_MUTEX(devfreq_lock);
31 static DEFINE_IDR(devfreq_idr);
32 
33 /**
34  * struct devfreq_cooling_device - Devfreq cooling device
35  * @id:		unique integer value corresponding to each
36  *		devfreq_cooling_device registered.
37  * @cdev:	Pointer to associated thermal cooling device.
38  * @devfreq:	Pointer to associated devfreq device.
39  * @cooling_state:	Current cooling state.
40  * @power_table:	Pointer to table with maximum power draw for each
41  *			cooling state. State is the index into the table, and
42  *			the power is in mW.
43  * @freq_table:	Pointer to a table with the frequencies sorted in descending
44  *		order.  You can index the table by cooling device state
45  * @freq_table_size:	Size of the @freq_table and @power_table
46  * @power_ops:	Pointer to devfreq_cooling_power, used to generate the
47  *		@power_table.
48  */
49 struct devfreq_cooling_device {
50 	int id;
51 	struct thermal_cooling_device *cdev;
52 	struct devfreq *devfreq;
53 	unsigned long cooling_state;
54 	u32 *power_table;
55 	u32 *freq_table;
56 	size_t freq_table_size;
57 	struct devfreq_cooling_power *power_ops;
58 };
59 
60 /**
61  * get_idr - function to get a unique id.
62  * @idr: struct idr * handle used to create a id.
63  * @id: int * value generated by this function.
64  *
65  * This function will populate @id with an unique
66  * id, using the idr API.
67  *
68  * Return: 0 on success, an error code on failure.
69  */
70 static int get_idr(struct idr *idr, int *id)
71 {
72 	int ret;
73 
74 	mutex_lock(&devfreq_lock);
75 	ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
76 	mutex_unlock(&devfreq_lock);
77 	if (unlikely(ret < 0))
78 		return ret;
79 	*id = ret;
80 
81 	return 0;
82 }
83 
84 /**
85  * release_idr - function to free the unique id.
86  * @idr: struct idr * handle used for creating the id.
87  * @id: int value representing the unique id.
88  */
89 static void release_idr(struct idr *idr, int id)
90 {
91 	mutex_lock(&devfreq_lock);
92 	idr_remove(idr, id);
93 	mutex_unlock(&devfreq_lock);
94 }
95 
96 /**
97  * partition_enable_opps() - disable all opps above a given state
98  * @dfc:	Pointer to devfreq we are operating on
99  * @cdev_state:	cooling device state we're setting
100  *
101  * Go through the OPPs of the device, enabling all OPPs until
102  * @cdev_state and disabling those frequencies above it.
103  */
104 static int partition_enable_opps(struct devfreq_cooling_device *dfc,
105 				 unsigned long cdev_state)
106 {
107 	int i;
108 	struct device *dev = dfc->devfreq->dev.parent;
109 
110 	for (i = 0; i < dfc->freq_table_size; i++) {
111 		struct dev_pm_opp *opp;
112 		int ret = 0;
113 		unsigned int freq = dfc->freq_table[i];
114 		bool want_enable = i >= cdev_state ? true : false;
115 
116 		opp = dev_pm_opp_find_freq_exact(dev, freq, !want_enable);
117 
118 		if (PTR_ERR(opp) == -ERANGE)
119 			continue;
120 		else if (IS_ERR(opp))
121 			return PTR_ERR(opp);
122 
123 		dev_pm_opp_put(opp);
124 
125 		if (want_enable)
126 			ret = dev_pm_opp_enable(dev, freq);
127 		else
128 			ret = dev_pm_opp_disable(dev, freq);
129 
130 		if (ret)
131 			return ret;
132 	}
133 
134 	return 0;
135 }
136 
137 static int devfreq_cooling_get_max_state(struct thermal_cooling_device *cdev,
138 					 unsigned long *state)
139 {
140 	struct devfreq_cooling_device *dfc = cdev->devdata;
141 
142 	*state = dfc->freq_table_size - 1;
143 
144 	return 0;
145 }
146 
147 static int devfreq_cooling_get_cur_state(struct thermal_cooling_device *cdev,
148 					 unsigned long *state)
149 {
150 	struct devfreq_cooling_device *dfc = cdev->devdata;
151 
152 	*state = dfc->cooling_state;
153 
154 	return 0;
155 }
156 
157 static int devfreq_cooling_set_cur_state(struct thermal_cooling_device *cdev,
158 					 unsigned long state)
159 {
160 	struct devfreq_cooling_device *dfc = cdev->devdata;
161 	struct devfreq *df = dfc->devfreq;
162 	struct device *dev = df->dev.parent;
163 	int ret;
164 
165 	if (state == dfc->cooling_state)
166 		return 0;
167 
168 	dev_dbg(dev, "Setting cooling state %lu\n", state);
169 
170 	if (state >= dfc->freq_table_size)
171 		return -EINVAL;
172 
173 	ret = partition_enable_opps(dfc, state);
174 	if (ret)
175 		return ret;
176 
177 	dfc->cooling_state = state;
178 
179 	return 0;
180 }
181 
182 /**
183  * freq_get_state() - get the cooling state corresponding to a frequency
184  * @dfc:	Pointer to devfreq cooling device
185  * @freq:	frequency in Hz
186  *
187  * Return: the cooling state associated with the @freq, or
188  * THERMAL_CSTATE_INVALID if it wasn't found.
189  */
190 static unsigned long
191 freq_get_state(struct devfreq_cooling_device *dfc, unsigned long freq)
192 {
193 	int i;
194 
195 	for (i = 0; i < dfc->freq_table_size; i++) {
196 		if (dfc->freq_table[i] == freq)
197 			return i;
198 	}
199 
200 	return THERMAL_CSTATE_INVALID;
201 }
202 
203 /**
204  * get_static_power() - calculate the static power
205  * @dfc:	Pointer to devfreq cooling device
206  * @freq:	Frequency in Hz
207  *
208  * Calculate the static power in milliwatts using the supplied
209  * get_static_power().  The current voltage is calculated using the
210  * OPP library.  If no get_static_power() was supplied, assume the
211  * static power is negligible.
212  */
213 static unsigned long
214 get_static_power(struct devfreq_cooling_device *dfc, unsigned long freq)
215 {
216 	struct devfreq *df = dfc->devfreq;
217 	struct device *dev = df->dev.parent;
218 	unsigned long voltage;
219 	struct dev_pm_opp *opp;
220 
221 	if (!dfc->power_ops->get_static_power)
222 		return 0;
223 
224 	opp = dev_pm_opp_find_freq_exact(dev, freq, true);
225 	if (IS_ERR(opp) && (PTR_ERR(opp) == -ERANGE))
226 		opp = dev_pm_opp_find_freq_exact(dev, freq, false);
227 
228 	voltage = dev_pm_opp_get_voltage(opp) / 1000; /* mV */
229 	dev_pm_opp_put(opp);
230 
231 	if (voltage == 0) {
232 		dev_warn_ratelimited(dev,
233 				     "Failed to get voltage for frequency %lu: %ld\n",
234 				     freq, IS_ERR(opp) ? PTR_ERR(opp) : 0);
235 		return 0;
236 	}
237 
238 	return dfc->power_ops->get_static_power(df, voltage);
239 }
240 
241 /**
242  * get_dynamic_power - calculate the dynamic power
243  * @dfc:	Pointer to devfreq cooling device
244  * @freq:	Frequency in Hz
245  * @voltage:	Voltage in millivolts
246  *
247  * Calculate the dynamic power in milliwatts consumed by the device at
248  * frequency @freq and voltage @voltage.  If the get_dynamic_power()
249  * was supplied as part of the devfreq_cooling_power struct, then that
250  * function is used.  Otherwise, a simple power model (Pdyn = Coeff *
251  * Voltage^2 * Frequency) is used.
252  */
253 static unsigned long
254 get_dynamic_power(struct devfreq_cooling_device *dfc, unsigned long freq,
255 		  unsigned long voltage)
256 {
257 	u64 power;
258 	u32 freq_mhz;
259 	struct devfreq_cooling_power *dfc_power = dfc->power_ops;
260 
261 	if (dfc_power->get_dynamic_power)
262 		return dfc_power->get_dynamic_power(dfc->devfreq, freq,
263 						    voltage);
264 
265 	freq_mhz = freq / 1000000;
266 	power = (u64)dfc_power->dyn_power_coeff * freq_mhz * voltage * voltage;
267 	do_div(power, 1000000000);
268 
269 	return power;
270 }
271 
272 static int devfreq_cooling_get_requested_power(struct thermal_cooling_device *cdev,
273 					       struct thermal_zone_device *tz,
274 					       u32 *power)
275 {
276 	struct devfreq_cooling_device *dfc = cdev->devdata;
277 	struct devfreq *df = dfc->devfreq;
278 	struct devfreq_dev_status *status = &df->last_status;
279 	unsigned long state;
280 	unsigned long freq = status->current_frequency;
281 	u32 dyn_power, static_power;
282 
283 	/* Get dynamic power for state */
284 	state = freq_get_state(dfc, freq);
285 	if (state == THERMAL_CSTATE_INVALID)
286 		return -EAGAIN;
287 
288 	dyn_power = dfc->power_table[state];
289 
290 	/* Scale dynamic power for utilization */
291 	dyn_power = (dyn_power * status->busy_time) / status->total_time;
292 
293 	/* Get static power */
294 	static_power = get_static_power(dfc, freq);
295 
296 	trace_thermal_power_devfreq_get_power(cdev, status, freq, dyn_power,
297 					      static_power);
298 
299 	*power = dyn_power + static_power;
300 
301 	return 0;
302 }
303 
304 static int devfreq_cooling_state2power(struct thermal_cooling_device *cdev,
305 				       struct thermal_zone_device *tz,
306 				       unsigned long state,
307 				       u32 *power)
308 {
309 	struct devfreq_cooling_device *dfc = cdev->devdata;
310 	unsigned long freq;
311 	u32 static_power;
312 
313 	if (state >= dfc->freq_table_size)
314 		return -EINVAL;
315 
316 	freq = dfc->freq_table[state];
317 	static_power = get_static_power(dfc, freq);
318 
319 	*power = dfc->power_table[state] + static_power;
320 	return 0;
321 }
322 
323 static int devfreq_cooling_power2state(struct thermal_cooling_device *cdev,
324 				       struct thermal_zone_device *tz,
325 				       u32 power, unsigned long *state)
326 {
327 	struct devfreq_cooling_device *dfc = cdev->devdata;
328 	struct devfreq *df = dfc->devfreq;
329 	struct devfreq_dev_status *status = &df->last_status;
330 	unsigned long freq = status->current_frequency;
331 	unsigned long busy_time;
332 	s32 dyn_power;
333 	u32 static_power;
334 	int i;
335 
336 	static_power = get_static_power(dfc, freq);
337 
338 	dyn_power = power - static_power;
339 	dyn_power = dyn_power > 0 ? dyn_power : 0;
340 
341 	/* Scale dynamic power for utilization */
342 	busy_time = status->busy_time ?: 1;
343 	dyn_power = (dyn_power * status->total_time) / busy_time;
344 
345 	/*
346 	 * Find the first cooling state that is within the power
347 	 * budget for dynamic power.
348 	 */
349 	for (i = 0; i < dfc->freq_table_size - 1; i++)
350 		if (dyn_power >= dfc->power_table[i])
351 			break;
352 
353 	*state = i;
354 	trace_thermal_power_devfreq_limit(cdev, freq, *state, power);
355 	return 0;
356 }
357 
358 static struct thermal_cooling_device_ops devfreq_cooling_ops = {
359 	.get_max_state = devfreq_cooling_get_max_state,
360 	.get_cur_state = devfreq_cooling_get_cur_state,
361 	.set_cur_state = devfreq_cooling_set_cur_state,
362 };
363 
364 /**
365  * devfreq_cooling_gen_tables() - Generate power and freq tables.
366  * @dfc: Pointer to devfreq cooling device.
367  *
368  * Generate power and frequency tables: the power table hold the
369  * device's maximum power usage at each cooling state (OPP).  The
370  * static and dynamic power using the appropriate voltage and
371  * frequency for the state, is acquired from the struct
372  * devfreq_cooling_power, and summed to make the maximum power draw.
373  *
374  * The frequency table holds the frequencies in descending order.
375  * That way its indexed by cooling device state.
376  *
377  * The tables are malloced, and pointers put in dfc.  They must be
378  * freed when unregistering the devfreq cooling device.
379  *
380  * Return: 0 on success, negative error code on failure.
381  */
382 static int devfreq_cooling_gen_tables(struct devfreq_cooling_device *dfc)
383 {
384 	struct devfreq *df = dfc->devfreq;
385 	struct device *dev = df->dev.parent;
386 	int ret, num_opps;
387 	unsigned long freq;
388 	u32 *power_table = NULL;
389 	u32 *freq_table;
390 	int i;
391 
392 	num_opps = dev_pm_opp_get_opp_count(dev);
393 
394 	if (dfc->power_ops) {
395 		power_table = kcalloc(num_opps, sizeof(*power_table),
396 				      GFP_KERNEL);
397 		if (!power_table)
398 			return -ENOMEM;
399 	}
400 
401 	freq_table = kcalloc(num_opps, sizeof(*freq_table),
402 			     GFP_KERNEL);
403 	if (!freq_table) {
404 		ret = -ENOMEM;
405 		goto free_power_table;
406 	}
407 
408 	for (i = 0, freq = ULONG_MAX; i < num_opps; i++, freq--) {
409 		unsigned long power_dyn, voltage;
410 		struct dev_pm_opp *opp;
411 
412 		opp = dev_pm_opp_find_freq_floor(dev, &freq);
413 		if (IS_ERR(opp)) {
414 			ret = PTR_ERR(opp);
415 			goto free_tables;
416 		}
417 
418 		voltage = dev_pm_opp_get_voltage(opp) / 1000; /* mV */
419 		dev_pm_opp_put(opp);
420 
421 		if (dfc->power_ops) {
422 			power_dyn = get_dynamic_power(dfc, freq, voltage);
423 
424 			dev_dbg(dev, "Dynamic power table: %lu MHz @ %lu mV: %lu = %lu mW\n",
425 				freq / 1000000, voltage, power_dyn, power_dyn);
426 
427 			power_table[i] = power_dyn;
428 		}
429 
430 		freq_table[i] = freq;
431 	}
432 
433 	if (dfc->power_ops)
434 		dfc->power_table = power_table;
435 
436 	dfc->freq_table = freq_table;
437 	dfc->freq_table_size = num_opps;
438 
439 	return 0;
440 
441 free_tables:
442 	kfree(freq_table);
443 free_power_table:
444 	kfree(power_table);
445 
446 	return ret;
447 }
448 
449 /**
450  * of_devfreq_cooling_register_power() - Register devfreq cooling device,
451  *                                      with OF and power information.
452  * @np:	Pointer to OF device_node.
453  * @df:	Pointer to devfreq device.
454  * @dfc_power:	Pointer to devfreq_cooling_power.
455  *
456  * Register a devfreq cooling device.  The available OPPs must be
457  * registered on the device.
458  *
459  * If @dfc_power is provided, the cooling device is registered with the
460  * power extensions.  For the power extensions to work correctly,
461  * devfreq should use the simple_ondemand governor, other governors
462  * are not currently supported.
463  */
464 struct thermal_cooling_device *
465 of_devfreq_cooling_register_power(struct device_node *np, struct devfreq *df,
466 				  struct devfreq_cooling_power *dfc_power)
467 {
468 	struct thermal_cooling_device *cdev;
469 	struct devfreq_cooling_device *dfc;
470 	char dev_name[THERMAL_NAME_LENGTH];
471 	int err;
472 
473 	dfc = kzalloc(sizeof(*dfc), GFP_KERNEL);
474 	if (!dfc)
475 		return ERR_PTR(-ENOMEM);
476 
477 	dfc->devfreq = df;
478 
479 	if (dfc_power) {
480 		dfc->power_ops = dfc_power;
481 
482 		devfreq_cooling_ops.get_requested_power =
483 			devfreq_cooling_get_requested_power;
484 		devfreq_cooling_ops.state2power = devfreq_cooling_state2power;
485 		devfreq_cooling_ops.power2state = devfreq_cooling_power2state;
486 	}
487 
488 	err = devfreq_cooling_gen_tables(dfc);
489 	if (err)
490 		goto free_dfc;
491 
492 	err = get_idr(&devfreq_idr, &dfc->id);
493 	if (err)
494 		goto free_tables;
495 
496 	snprintf(dev_name, sizeof(dev_name), "thermal-devfreq-%d", dfc->id);
497 
498 	cdev = thermal_of_cooling_device_register(np, dev_name, dfc,
499 						  &devfreq_cooling_ops);
500 	if (IS_ERR(cdev)) {
501 		err = PTR_ERR(cdev);
502 		dev_err(df->dev.parent,
503 			"Failed to register devfreq cooling device (%d)\n",
504 			err);
505 		goto release_idr;
506 	}
507 
508 	dfc->cdev = cdev;
509 
510 	return cdev;
511 
512 release_idr:
513 	release_idr(&devfreq_idr, dfc->id);
514 free_tables:
515 	kfree(dfc->power_table);
516 	kfree(dfc->freq_table);
517 free_dfc:
518 	kfree(dfc);
519 
520 	return ERR_PTR(err);
521 }
522 EXPORT_SYMBOL_GPL(of_devfreq_cooling_register_power);
523 
524 /**
525  * of_devfreq_cooling_register() - Register devfreq cooling device,
526  *                                with OF information.
527  * @np: Pointer to OF device_node.
528  * @df: Pointer to devfreq device.
529  */
530 struct thermal_cooling_device *
531 of_devfreq_cooling_register(struct device_node *np, struct devfreq *df)
532 {
533 	return of_devfreq_cooling_register_power(np, df, NULL);
534 }
535 EXPORT_SYMBOL_GPL(of_devfreq_cooling_register);
536 
537 /**
538  * devfreq_cooling_register() - Register devfreq cooling device.
539  * @df: Pointer to devfreq device.
540  */
541 struct thermal_cooling_device *devfreq_cooling_register(struct devfreq *df)
542 {
543 	return of_devfreq_cooling_register(NULL, df);
544 }
545 EXPORT_SYMBOL_GPL(devfreq_cooling_register);
546 
547 /**
548  * devfreq_cooling_unregister() - Unregister devfreq cooling device.
549  * @dfc: Pointer to devfreq cooling device to unregister.
550  */
551 void devfreq_cooling_unregister(struct thermal_cooling_device *cdev)
552 {
553 	struct devfreq_cooling_device *dfc;
554 
555 	if (!cdev)
556 		return;
557 
558 	dfc = cdev->devdata;
559 
560 	thermal_cooling_device_unregister(dfc->cdev);
561 	release_idr(&devfreq_idr, dfc->id);
562 	kfree(dfc->power_table);
563 	kfree(dfc->freq_table);
564 
565 	kfree(dfc);
566 }
567 EXPORT_SYMBOL_GPL(devfreq_cooling_unregister);
568