Linux Audio

Check our new training course

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