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