1 // SPDX-License-Identifier: GPL-2.0
3 * devfreq_cooling: Thermal cooling device implementation for devices using
6 * Copyright (C) 2014-2015 ARM Limited
9 * - If OPPs are added or removed after devfreq cooling has
10 * registered, the devfreq cooling won't react to it.
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/idr.h>
18 #include <linux/slab.h>
19 #include <linux/pm_opp.h>
20 #include <linux/pm_qos.h>
21 #include <linux/thermal.h>
23 #include <trace/events/thermal.h>
25 #define HZ_PER_KHZ 1000
26 #define SCALE_ERROR_MITIGATION 100
28 static DEFINE_IDA(devfreq_ida);
31 * struct devfreq_cooling_device - Devfreq cooling device
32 * @id: unique integer value corresponding to each
33 * devfreq_cooling_device registered.
34 * @cdev: Pointer to associated thermal cooling device.
35 * @devfreq: Pointer to associated devfreq device.
36 * @cooling_state: Current cooling state.
37 * @freq_table: Pointer to a table with the frequencies sorted in descending
38 * order. You can index the table by cooling device state
39 * @max_state: It is the last index, that is, one less than the number of the
41 * @power_ops: Pointer to devfreq_cooling_power, a more precised model.
42 * @res_util: Resource utilization scaling factor for the power.
43 * It is multiplied by 100 to minimize the error. It is used
44 * for estimation of the power budget instead of using
45 * 'utilization' (which is 'busy_time' / 'total_time').
46 * The 'res_util' range is from 100 to power * 100 for the
47 * corresponding 'state'.
48 * @capped_state: index to cooling state with in dynamic power budget
49 * @req_max_freq: PM QoS request for limiting the maximum frequency
50 * of the devfreq device.
52 struct devfreq_cooling_device {
54 struct thermal_cooling_device *cdev;
55 struct devfreq *devfreq;
56 unsigned long cooling_state;
59 struct devfreq_cooling_power *power_ops;
62 struct dev_pm_qos_request req_max_freq;
65 static int devfreq_cooling_get_max_state(struct thermal_cooling_device *cdev,
68 struct devfreq_cooling_device *dfc = cdev->devdata;
70 *state = dfc->max_state;
75 static int devfreq_cooling_get_cur_state(struct thermal_cooling_device *cdev,
78 struct devfreq_cooling_device *dfc = cdev->devdata;
80 *state = dfc->cooling_state;
85 static int devfreq_cooling_set_cur_state(struct thermal_cooling_device *cdev,
88 struct devfreq_cooling_device *dfc = cdev->devdata;
89 struct devfreq *df = dfc->devfreq;
90 struct device *dev = df->dev.parent;
94 if (state == dfc->cooling_state)
97 dev_dbg(dev, "Setting cooling state %lu\n", state);
99 if (state > dfc->max_state)
103 perf_idx = dfc->max_state - state;
104 freq = dev->em_pd->table[perf_idx].frequency * 1000;
106 freq = dfc->freq_table[state];
109 dev_pm_qos_update_request(&dfc->req_max_freq,
110 DIV_ROUND_UP(freq, HZ_PER_KHZ));
112 dfc->cooling_state = state;
118 * get_perf_idx() - get the performance index corresponding to a frequency
119 * @em_pd: Pointer to device's Energy Model
120 * @freq: frequency in kHz
122 * Return: the performance index associated with the @freq, or
123 * -EINVAL if it wasn't found.
125 static int get_perf_idx(struct em_perf_domain *em_pd, unsigned long freq)
129 for (i = 0; i < em_pd->nr_perf_states; i++) {
130 if (em_pd->table[i].frequency == freq)
137 static unsigned long get_voltage(struct devfreq *df, unsigned long freq)
139 struct device *dev = df->dev.parent;
140 unsigned long voltage;
141 struct dev_pm_opp *opp;
143 opp = dev_pm_opp_find_freq_exact(dev, freq, true);
144 if (PTR_ERR(opp) == -ERANGE)
145 opp = dev_pm_opp_find_freq_exact(dev, freq, false);
148 dev_err_ratelimited(dev, "Failed to find OPP for frequency %lu: %ld\n",
153 voltage = dev_pm_opp_get_voltage(opp) / 1000; /* mV */
157 dev_err_ratelimited(dev,
158 "Failed to get voltage for frequency %lu\n",
165 static void _normalize_load(struct devfreq_dev_status *status)
167 if (status->total_time > 0xfffff) {
168 status->total_time >>= 10;
169 status->busy_time >>= 10;
172 status->busy_time <<= 10;
173 status->busy_time /= status->total_time ? : 1;
175 status->busy_time = status->busy_time ? : 1;
176 status->total_time = 1024;
179 static int devfreq_cooling_get_requested_power(struct thermal_cooling_device *cdev,
182 struct devfreq_cooling_device *dfc = cdev->devdata;
183 struct devfreq *df = dfc->devfreq;
184 struct device *dev = df->dev.parent;
185 struct devfreq_dev_status status;
188 unsigned long voltage;
191 mutex_lock(&df->lock);
192 status = df->last_status;
193 mutex_unlock(&df->lock);
195 freq = status.current_frequency;
197 if (dfc->power_ops && dfc->power_ops->get_real_power) {
198 voltage = get_voltage(df, freq);
204 res = dfc->power_ops->get_real_power(df, power, freq, voltage);
206 state = dfc->capped_state;
207 dfc->res_util = dev->em_pd->table[state].power;
208 dfc->res_util *= SCALE_ERROR_MITIGATION;
211 dfc->res_util /= *power;
216 /* Energy Model frequencies are in kHz */
217 perf_idx = get_perf_idx(dev->em_pd, freq / 1000);
223 _normalize_load(&status);
225 /* Scale power for utilization */
226 *power = dev->em_pd->table[perf_idx].power;
227 *power *= status.busy_time;
231 trace_thermal_power_devfreq_get_power(cdev, &status, freq, *power);
235 /* It is safe to set max in this case */
236 dfc->res_util = SCALE_ERROR_MITIGATION;
240 static int devfreq_cooling_state2power(struct thermal_cooling_device *cdev,
241 unsigned long state, u32 *power)
243 struct devfreq_cooling_device *dfc = cdev->devdata;
244 struct devfreq *df = dfc->devfreq;
245 struct device *dev = df->dev.parent;
248 if (state > dfc->max_state)
251 perf_idx = dfc->max_state - state;
252 *power = dev->em_pd->table[perf_idx].power;
257 static int devfreq_cooling_power2state(struct thermal_cooling_device *cdev,
258 u32 power, unsigned long *state)
260 struct devfreq_cooling_device *dfc = cdev->devdata;
261 struct devfreq *df = dfc->devfreq;
262 struct device *dev = df->dev.parent;
263 struct devfreq_dev_status status;
268 mutex_lock(&df->lock);
269 status = df->last_status;
270 mutex_unlock(&df->lock);
272 freq = status.current_frequency;
274 if (dfc->power_ops && dfc->power_ops->get_real_power) {
275 /* Scale for resource utilization */
276 est_power = power * dfc->res_util;
277 est_power /= SCALE_ERROR_MITIGATION;
279 /* Scale dynamic power for utilization */
280 _normalize_load(&status);
281 est_power = power << 10;
282 est_power /= status.busy_time;
286 * Find the first cooling state that is within the power
287 * budget. The EM power table is sorted ascending.
289 for (i = dfc->max_state; i > 0; i--)
290 if (est_power >= dev->em_pd->table[i].power)
293 *state = dfc->max_state - i;
294 dfc->capped_state = *state;
296 trace_thermal_power_devfreq_limit(cdev, freq, *state, power);
300 static struct thermal_cooling_device_ops devfreq_cooling_ops = {
301 .get_max_state = devfreq_cooling_get_max_state,
302 .get_cur_state = devfreq_cooling_get_cur_state,
303 .set_cur_state = devfreq_cooling_set_cur_state,
307 * devfreq_cooling_gen_tables() - Generate frequency table.
308 * @dfc: Pointer to devfreq cooling device.
309 * @num_opps: Number of OPPs
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.
315 * Return: 0 on success, negative error code on failure.
317 static int devfreq_cooling_gen_tables(struct devfreq_cooling_device *dfc,
320 struct devfreq *df = dfc->devfreq;
321 struct device *dev = df->dev.parent;
325 dfc->freq_table = kcalloc(num_opps, sizeof(*dfc->freq_table),
327 if (!dfc->freq_table)
330 for (i = 0, freq = ULONG_MAX; i < num_opps; i++, freq--) {
331 struct dev_pm_opp *opp;
333 opp = dev_pm_opp_find_freq_floor(dev, &freq);
335 kfree(dfc->freq_table);
340 dfc->freq_table[i] = freq;
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.
353 * Register a devfreq cooling device. The available OPPs must be
354 * registered on the device.
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.
361 struct thermal_cooling_device *
362 of_devfreq_cooling_register_power(struct device_node *np, struct devfreq *df,
363 struct devfreq_cooling_power *dfc_power)
365 struct thermal_cooling_device *cdev;
366 struct device *dev = df->dev.parent;
367 struct devfreq_cooling_device *dfc;
368 char dev_name[THERMAL_NAME_LENGTH];
371 dfc = kzalloc(sizeof(*dfc), GFP_KERNEL);
373 return ERR_PTR(-ENOMEM);
378 devfreq_cooling_ops.get_requested_power =
379 devfreq_cooling_get_requested_power;
380 devfreq_cooling_ops.state2power = devfreq_cooling_state2power;
381 devfreq_cooling_ops.power2state = devfreq_cooling_power2state;
383 dfc->power_ops = dfc_power;
385 num_opps = em_pd_nr_perf_states(dev->em_pd);
387 /* Backward compatibility for drivers which do not use IPA */
388 dev_dbg(dev, "missing EM for cooling device\n");
390 num_opps = dev_pm_opp_get_opp_count(dev);
392 err = devfreq_cooling_gen_tables(dfc, num_opps);
402 /* max_state is an index, not a counter */
403 dfc->max_state = num_opps - 1;
405 err = dev_pm_qos_add_request(dev, &dfc->req_max_freq,
406 DEV_PM_QOS_MAX_FREQUENCY,
407 PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE);
411 err = ida_simple_get(&devfreq_ida, 0, 0, GFP_KERNEL);
417 snprintf(dev_name, sizeof(dev_name), "thermal-devfreq-%d", dfc->id);
419 cdev = thermal_of_cooling_device_register(np, dev_name, dfc,
420 &devfreq_cooling_ops);
424 "Failed to register devfreq cooling device (%d)\n",
434 ida_simple_remove(&devfreq_ida, dfc->id);
436 dev_pm_qos_remove_request(&dfc->req_max_freq);
438 kfree(dfc->freq_table);
444 EXPORT_SYMBOL_GPL(of_devfreq_cooling_register_power);
447 * of_devfreq_cooling_register() - Register devfreq cooling device,
448 * with OF information.
449 * @np: Pointer to OF device_node.
450 * @df: Pointer to devfreq device.
452 struct thermal_cooling_device *
453 of_devfreq_cooling_register(struct device_node *np, struct devfreq *df)
455 return of_devfreq_cooling_register_power(np, df, NULL);
457 EXPORT_SYMBOL_GPL(of_devfreq_cooling_register);
460 * devfreq_cooling_register() - Register devfreq cooling device.
461 * @df: Pointer to devfreq device.
463 struct thermal_cooling_device *devfreq_cooling_register(struct devfreq *df)
465 return of_devfreq_cooling_register(NULL, df);
467 EXPORT_SYMBOL_GPL(devfreq_cooling_register);
470 * devfreq_cooling_em_register_power() - Register devfreq cooling device with
471 * power information and automatically register Energy Model (EM)
472 * @df: Pointer to devfreq device.
473 * @dfc_power: Pointer to devfreq_cooling_power.
475 * Register a devfreq cooling device and automatically register EM. The
476 * available OPPs must be registered for the device.
478 * If @dfc_power is provided, the cooling device is registered with the
479 * power extensions. It is using the simple Energy Model which requires
480 * "dynamic-power-coefficient" a devicetree property. To not break drivers
481 * which miss that DT property, the function won't bail out when the EM
482 * registration failed. The cooling device will be registered if everything
485 struct thermal_cooling_device *
486 devfreq_cooling_em_register(struct devfreq *df,
487 struct devfreq_cooling_power *dfc_power)
489 struct thermal_cooling_device *cdev;
493 if (IS_ERR_OR_NULL(df))
494 return ERR_PTR(-EINVAL);
496 dev = df->dev.parent;
498 ret = dev_pm_opp_of_register_em(dev, NULL);
500 dev_dbg(dev, "Unable to register EM for devfreq cooling device (%d)\n",
503 cdev = of_devfreq_cooling_register_power(dev->of_node, df, dfc_power);
505 if (IS_ERR_OR_NULL(cdev))
506 em_dev_unregister_perf_domain(dev);
510 EXPORT_SYMBOL_GPL(devfreq_cooling_em_register);
513 * devfreq_cooling_unregister() - Unregister devfreq cooling device.
514 * @cdev: Pointer to devfreq cooling device to unregister.
516 * Unregisters devfreq cooling device and related Energy Model if it was
519 void devfreq_cooling_unregister(struct thermal_cooling_device *cdev)
521 struct devfreq_cooling_device *dfc;
524 if (IS_ERR_OR_NULL(cdev))
528 dev = dfc->devfreq->dev.parent;
530 thermal_cooling_device_unregister(dfc->cdev);
531 ida_simple_remove(&devfreq_ida, dfc->id);
532 dev_pm_qos_remove_request(&dfc->req_max_freq);
534 em_dev_unregister_perf_domain(dev);
536 kfree(dfc->freq_table);
539 EXPORT_SYMBOL_GPL(devfreq_cooling_unregister);