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