2 * devfreq_cooling: Thermal cooling device implementation for devices using
5 * Copyright (C) 2014-2015 ARM Limited
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.
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.
17 * - If OPPs are added or removed after devfreq cooling has
18 * registered, the devfreq cooling won't react to it.
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>
29 #include <trace/events/thermal.h>
31 static DEFINE_IDA(devfreq_ida);
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
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
49 struct devfreq_cooling_device {
51 struct thermal_cooling_device *cdev;
52 struct devfreq *devfreq;
53 unsigned long cooling_state;
56 size_t freq_table_size;
57 struct devfreq_cooling_power *power_ops;
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
65 * Go through the OPPs of the device, enabling all OPPs until
66 * @cdev_state and disabling those frequencies above it.
68 static int partition_enable_opps(struct devfreq_cooling_device *dfc,
69 unsigned long cdev_state)
72 struct device *dev = dfc->devfreq->dev.parent;
74 for (i = 0; i < dfc->freq_table_size; i++) {
75 struct dev_pm_opp *opp;
77 unsigned int freq = dfc->freq_table[i];
78 bool want_enable = i >= cdev_state ? true : false;
80 opp = dev_pm_opp_find_freq_exact(dev, freq, !want_enable);
82 if (PTR_ERR(opp) == -ERANGE)
90 ret = dev_pm_opp_enable(dev, freq);
92 ret = dev_pm_opp_disable(dev, freq);
101 static int devfreq_cooling_get_max_state(struct thermal_cooling_device *cdev,
102 unsigned long *state)
104 struct devfreq_cooling_device *dfc = cdev->devdata;
106 *state = dfc->freq_table_size - 1;
111 static int devfreq_cooling_get_cur_state(struct thermal_cooling_device *cdev,
112 unsigned long *state)
114 struct devfreq_cooling_device *dfc = cdev->devdata;
116 *state = dfc->cooling_state;
121 static int devfreq_cooling_set_cur_state(struct thermal_cooling_device *cdev,
124 struct devfreq_cooling_device *dfc = cdev->devdata;
125 struct devfreq *df = dfc->devfreq;
126 struct device *dev = df->dev.parent;
129 if (state == dfc->cooling_state)
132 dev_dbg(dev, "Setting cooling state %lu\n", state);
134 if (state >= dfc->freq_table_size)
137 ret = partition_enable_opps(dfc, state);
141 dfc->cooling_state = state;
147 * freq_get_state() - get the cooling state corresponding to a frequency
148 * @dfc: Pointer to devfreq cooling device
149 * @freq: frequency in Hz
151 * Return: the cooling state associated with the @freq, or
152 * THERMAL_CSTATE_INVALID if it wasn't found.
155 freq_get_state(struct devfreq_cooling_device *dfc, unsigned long freq)
159 for (i = 0; i < dfc->freq_table_size; i++) {
160 if (dfc->freq_table[i] == freq)
164 return THERMAL_CSTATE_INVALID;
167 static unsigned long get_voltage(struct devfreq *df, unsigned long freq)
169 struct device *dev = df->dev.parent;
170 unsigned long voltage;
171 struct dev_pm_opp *opp;
173 opp = dev_pm_opp_find_freq_exact(dev, freq, true);
174 if (PTR_ERR(opp) == -ERANGE)
175 opp = dev_pm_opp_find_freq_exact(dev, freq, false);
178 dev_err_ratelimited(dev, "Failed to find OPP for frequency %lu: %ld\n",
183 voltage = dev_pm_opp_get_voltage(opp) / 1000; /* mV */
187 dev_err_ratelimited(dev,
188 "Failed to get voltage for frequency %lu\n",
196 * get_static_power() - calculate the static power
197 * @dfc: Pointer to devfreq cooling device
198 * @freq: Frequency in Hz
200 * Calculate the static power in milliwatts using the supplied
201 * get_static_power(). The current voltage is calculated using the
202 * OPP library. If no get_static_power() was supplied, assume the
203 * static power is negligible.
206 get_static_power(struct devfreq_cooling_device *dfc, unsigned long freq)
208 struct devfreq *df = dfc->devfreq;
209 unsigned long voltage;
211 if (!dfc->power_ops->get_static_power)
214 voltage = get_voltage(df, freq);
219 return dfc->power_ops->get_static_power(df, voltage);
223 * get_dynamic_power - calculate the dynamic power
224 * @dfc: Pointer to devfreq cooling device
225 * @freq: Frequency in Hz
226 * @voltage: Voltage in millivolts
228 * Calculate the dynamic power in milliwatts consumed by the device at
229 * frequency @freq and voltage @voltage. If the get_dynamic_power()
230 * was supplied as part of the devfreq_cooling_power struct, then that
231 * function is used. Otherwise, a simple power model (Pdyn = Coeff *
232 * Voltage^2 * Frequency) is used.
235 get_dynamic_power(struct devfreq_cooling_device *dfc, unsigned long freq,
236 unsigned long voltage)
240 struct devfreq_cooling_power *dfc_power = dfc->power_ops;
242 if (dfc_power->get_dynamic_power)
243 return dfc_power->get_dynamic_power(dfc->devfreq, freq,
246 freq_mhz = freq / 1000000;
247 power = (u64)dfc_power->dyn_power_coeff * freq_mhz * voltage * voltage;
248 do_div(power, 1000000000);
253 static int devfreq_cooling_get_requested_power(struct thermal_cooling_device *cdev,
254 struct thermal_zone_device *tz,
257 struct devfreq_cooling_device *dfc = cdev->devdata;
258 struct devfreq *df = dfc->devfreq;
259 struct devfreq_dev_status *status = &df->last_status;
261 unsigned long freq = status->current_frequency;
262 u32 dyn_power, static_power;
264 /* Get dynamic power for state */
265 state = freq_get_state(dfc, freq);
266 if (state == THERMAL_CSTATE_INVALID)
269 dyn_power = dfc->power_table[state];
271 /* Scale dynamic power for utilization */
272 dyn_power = (dyn_power * status->busy_time) / status->total_time;
274 /* Get static power */
275 static_power = get_static_power(dfc, freq);
277 trace_thermal_power_devfreq_get_power(cdev, status, freq, dyn_power,
280 *power = dyn_power + static_power;
285 static int devfreq_cooling_state2power(struct thermal_cooling_device *cdev,
286 struct thermal_zone_device *tz,
290 struct devfreq_cooling_device *dfc = cdev->devdata;
294 if (state >= dfc->freq_table_size)
297 freq = dfc->freq_table[state];
298 static_power = get_static_power(dfc, freq);
300 *power = dfc->power_table[state] + static_power;
304 static int devfreq_cooling_power2state(struct thermal_cooling_device *cdev,
305 struct thermal_zone_device *tz,
306 u32 power, unsigned long *state)
308 struct devfreq_cooling_device *dfc = cdev->devdata;
309 struct devfreq *df = dfc->devfreq;
310 struct devfreq_dev_status *status = &df->last_status;
311 unsigned long freq = status->current_frequency;
312 unsigned long busy_time;
317 static_power = get_static_power(dfc, freq);
319 dyn_power = power - static_power;
320 dyn_power = dyn_power > 0 ? dyn_power : 0;
322 /* Scale dynamic power for utilization */
323 busy_time = status->busy_time ?: 1;
324 dyn_power = (dyn_power * status->total_time) / busy_time;
327 * Find the first cooling state that is within the power
328 * budget for dynamic power.
330 for (i = 0; i < dfc->freq_table_size - 1; i++)
331 if (dyn_power >= dfc->power_table[i])
335 trace_thermal_power_devfreq_limit(cdev, freq, *state, power);
339 static struct thermal_cooling_device_ops devfreq_cooling_ops = {
340 .get_max_state = devfreq_cooling_get_max_state,
341 .get_cur_state = devfreq_cooling_get_cur_state,
342 .set_cur_state = devfreq_cooling_set_cur_state,
346 * devfreq_cooling_gen_tables() - Generate power and freq tables.
347 * @dfc: Pointer to devfreq cooling device.
349 * Generate power and frequency tables: the power table hold the
350 * device's maximum power usage at each cooling state (OPP). The
351 * static and dynamic power using the appropriate voltage and
352 * frequency for the state, is acquired from the struct
353 * devfreq_cooling_power, and summed to make the maximum power draw.
355 * The frequency table holds the frequencies in descending order.
356 * That way its indexed by cooling device state.
358 * The tables are malloced, and pointers put in dfc. They must be
359 * freed when unregistering the devfreq cooling device.
361 * Return: 0 on success, negative error code on failure.
363 static int devfreq_cooling_gen_tables(struct devfreq_cooling_device *dfc)
365 struct devfreq *df = dfc->devfreq;
366 struct device *dev = df->dev.parent;
369 u32 *power_table = NULL;
373 num_opps = dev_pm_opp_get_opp_count(dev);
375 if (dfc->power_ops) {
376 power_table = kcalloc(num_opps, sizeof(*power_table),
382 freq_table = kcalloc(num_opps, sizeof(*freq_table),
386 goto free_power_table;
389 for (i = 0, freq = ULONG_MAX; i < num_opps; i++, freq--) {
390 unsigned long power_dyn, voltage;
391 struct dev_pm_opp *opp;
393 opp = dev_pm_opp_find_freq_floor(dev, &freq);
399 voltage = dev_pm_opp_get_voltage(opp) / 1000; /* mV */
402 if (dfc->power_ops) {
403 power_dyn = get_dynamic_power(dfc, freq, voltage);
405 dev_dbg(dev, "Dynamic power table: %lu MHz @ %lu mV: %lu = %lu mW\n",
406 freq / 1000000, voltage, power_dyn, power_dyn);
408 power_table[i] = power_dyn;
411 freq_table[i] = freq;
415 dfc->power_table = power_table;
417 dfc->freq_table = freq_table;
418 dfc->freq_table_size = num_opps;
431 * of_devfreq_cooling_register_power() - Register devfreq cooling device,
432 * with OF and power information.
433 * @np: Pointer to OF device_node.
434 * @df: Pointer to devfreq device.
435 * @dfc_power: Pointer to devfreq_cooling_power.
437 * Register a devfreq cooling device. The available OPPs must be
438 * registered on the device.
440 * If @dfc_power is provided, the cooling device is registered with the
441 * power extensions. For the power extensions to work correctly,
442 * devfreq should use the simple_ondemand governor, other governors
443 * are not currently supported.
445 struct thermal_cooling_device *
446 of_devfreq_cooling_register_power(struct device_node *np, struct devfreq *df,
447 struct devfreq_cooling_power *dfc_power)
449 struct thermal_cooling_device *cdev;
450 struct devfreq_cooling_device *dfc;
451 char dev_name[THERMAL_NAME_LENGTH];
454 dfc = kzalloc(sizeof(*dfc), GFP_KERNEL);
456 return ERR_PTR(-ENOMEM);
461 dfc->power_ops = dfc_power;
463 devfreq_cooling_ops.get_requested_power =
464 devfreq_cooling_get_requested_power;
465 devfreq_cooling_ops.state2power = devfreq_cooling_state2power;
466 devfreq_cooling_ops.power2state = devfreq_cooling_power2state;
469 err = devfreq_cooling_gen_tables(dfc);
473 err = ida_simple_get(&devfreq_ida, 0, 0, GFP_KERNEL);
478 snprintf(dev_name, sizeof(dev_name), "thermal-devfreq-%d", dfc->id);
480 cdev = thermal_of_cooling_device_register(np, dev_name, dfc,
481 &devfreq_cooling_ops);
484 dev_err(df->dev.parent,
485 "Failed to register devfreq cooling device (%d)\n",
495 ida_simple_remove(&devfreq_ida, dfc->id);
497 kfree(dfc->power_table);
498 kfree(dfc->freq_table);
504 EXPORT_SYMBOL_GPL(of_devfreq_cooling_register_power);
507 * of_devfreq_cooling_register() - Register devfreq cooling device,
508 * with OF information.
509 * @np: Pointer to OF device_node.
510 * @df: Pointer to devfreq device.
512 struct thermal_cooling_device *
513 of_devfreq_cooling_register(struct device_node *np, struct devfreq *df)
515 return of_devfreq_cooling_register_power(np, df, NULL);
517 EXPORT_SYMBOL_GPL(of_devfreq_cooling_register);
520 * devfreq_cooling_register() - Register devfreq cooling device.
521 * @df: Pointer to devfreq device.
523 struct thermal_cooling_device *devfreq_cooling_register(struct devfreq *df)
525 return of_devfreq_cooling_register(NULL, df);
527 EXPORT_SYMBOL_GPL(devfreq_cooling_register);
530 * devfreq_cooling_unregister() - Unregister devfreq cooling device.
531 * @dfc: Pointer to devfreq cooling device to unregister.
533 void devfreq_cooling_unregister(struct thermal_cooling_device *cdev)
535 struct devfreq_cooling_device *dfc;
542 thermal_cooling_device_unregister(dfc->cdev);
543 ida_simple_remove(&devfreq_ida, dfc->id);
544 kfree(dfc->power_table);
545 kfree(dfc->freq_table);
549 EXPORT_SYMBOL_GPL(devfreq_cooling_unregister);