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/slab.h>
18 #include <linux/pm_opp.h>
19 #include <linux/pm_qos.h>
20 #include <linux/thermal.h>
21 #include <linux/units.h>
23 #include <trace/events/thermal.h>
25 #define SCALE_ERROR_MITIGATION 100
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
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
50 struct 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;
57 struct devfreq_cooling_power *power_ops;
60 struct dev_pm_qos_request req_max_freq;
61 struct em_perf_domain *em_pd;
64 static int devfreq_cooling_get_max_state(struct thermal_cooling_device *cdev,
67 struct devfreq_cooling_device *dfc = cdev->devdata;
69 *state = dfc->max_state;
74 static int devfreq_cooling_get_cur_state(struct thermal_cooling_device *cdev,
77 struct devfreq_cooling_device *dfc = cdev->devdata;
79 *state = dfc->cooling_state;
84 static int devfreq_cooling_set_cur_state(struct thermal_cooling_device *cdev,
87 struct devfreq_cooling_device *dfc = cdev->devdata;
88 struct devfreq *df = dfc->devfreq;
89 struct device *dev = df->dev.parent;
93 if (state == dfc->cooling_state)
96 dev_dbg(dev, "Setting cooling state %lu\n", state);
98 if (state > dfc->max_state)
102 perf_idx = dfc->max_state - state;
103 freq = dfc->em_pd->table[perf_idx].frequency * 1000;
105 freq = dfc->freq_table[state];
108 dev_pm_qos_update_request(&dfc->req_max_freq,
109 DIV_ROUND_UP(freq, HZ_PER_KHZ));
111 dfc->cooling_state = state;
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
121 * Return: the performance index associated with the @freq, or
122 * -EINVAL if it wasn't found.
124 static int get_perf_idx(struct em_perf_domain *em_pd, unsigned long freq)
128 for (i = 0; i < em_pd->nr_perf_states; i++) {
129 if (em_pd->table[i].frequency == freq)
136 static unsigned long get_voltage(struct devfreq *df, unsigned long freq)
138 struct device *dev = df->dev.parent;
139 unsigned long voltage;
140 struct dev_pm_opp *opp;
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);
147 dev_err_ratelimited(dev, "Failed to find OPP for frequency %lu: %ld\n",
152 voltage = dev_pm_opp_get_voltage(opp) / 1000; /* mV */
156 dev_err_ratelimited(dev,
157 "Failed to get voltage for frequency %lu\n",
164 static void _normalize_load(struct devfreq_dev_status *status)
166 if (status->total_time > 0xfffff) {
167 status->total_time >>= 10;
168 status->busy_time >>= 10;
171 status->busy_time <<= 10;
172 status->busy_time /= status->total_time ? : 1;
174 status->busy_time = status->busy_time ? : 1;
175 status->total_time = 1024;
178 static int devfreq_cooling_get_requested_power(struct thermal_cooling_device *cdev,
181 struct devfreq_cooling_device *dfc = cdev->devdata;
182 struct devfreq *df = dfc->devfreq;
183 struct devfreq_dev_status status;
186 unsigned long voltage;
189 mutex_lock(&df->lock);
190 status = df->last_status;
191 mutex_unlock(&df->lock);
193 freq = status.current_frequency;
195 if (dfc->power_ops && dfc->power_ops->get_real_power) {
196 voltage = get_voltage(df, freq);
202 res = dfc->power_ops->get_real_power(df, power, freq, voltage);
204 state = dfc->capped_state;
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;
210 dfc->res_util *= SCALE_ERROR_MITIGATION;
213 dfc->res_util /= *power;
218 /* Energy Model frequencies are in kHz */
219 perf_idx = get_perf_idx(dfc->em_pd, freq / 1000);
225 _normalize_load(&status);
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;
235 trace_thermal_power_devfreq_get_power(cdev, &status, freq, *power);
239 /* It is safe to set max in this case */
240 dfc->res_util = SCALE_ERROR_MITIGATION;
244 static int devfreq_cooling_state2power(struct thermal_cooling_device *cdev,
245 unsigned long state, u32 *power)
247 struct devfreq_cooling_device *dfc = cdev->devdata;
250 if (state > dfc->max_state)
253 perf_idx = dfc->max_state - state;
254 *power = dfc->em_pd->table[perf_idx].power;
255 *power /= MICROWATT_PER_MILLIWATT;
260 static int devfreq_cooling_power2state(struct thermal_cooling_device *cdev,
261 u32 power, unsigned long *state)
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;
270 mutex_lock(&df->lock);
271 status = df->last_status;
272 mutex_unlock(&df->lock);
274 freq = status.current_frequency;
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;
281 /* Scale dynamic power for utilization */
282 _normalize_load(&status);
283 est_power = power << 10;
284 est_power /= status.busy_time;
288 * Find the first cooling state that is within the power
289 * budget. The EM power table is sorted ascending.
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)
299 *state = dfc->max_state - i;
300 dfc->capped_state = *state;
302 trace_thermal_power_devfreq_limit(cdev, freq, *state, power);
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 struct em_perf_domain *em;
369 struct thermal_cooling_device_ops *ops;
374 dfc = kzalloc(sizeof(*dfc), GFP_KERNEL);
376 return ERR_PTR(-ENOMEM);
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;
386 if (em && !em_is_artificial(em)) {
388 ops->get_requested_power =
389 devfreq_cooling_get_requested_power;
390 ops->state2power = devfreq_cooling_state2power;
391 ops->power2state = devfreq_cooling_power2state;
393 dfc->power_ops = dfc_power;
395 num_opps = em_pd_nr_perf_states(dfc->em_pd);
397 /* Backward compatibility for drivers which do not use IPA */
398 dev_dbg(dev, "missing proper EM for cooling device\n");
400 num_opps = dev_pm_opp_get_opp_count(dev);
402 err = devfreq_cooling_gen_tables(dfc, num_opps);
412 /* max_state is an index, not a counter */
413 dfc->max_state = num_opps - 1;
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);
422 name = kasprintf(GFP_KERNEL, "devfreq-%s", dev_name(dev));
426 cdev = thermal_of_cooling_device_register(np, name, dfc, ops);
432 "Failed to register devfreq cooling device (%d)\n",
442 dev_pm_qos_remove_request(&dfc->req_max_freq);
444 kfree(dfc->freq_table);
450 EXPORT_SYMBOL_GPL(of_devfreq_cooling_register_power);
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.
458 struct thermal_cooling_device *
459 of_devfreq_cooling_register(struct device_node *np, struct devfreq *df)
461 return of_devfreq_cooling_register_power(np, df, NULL);
463 EXPORT_SYMBOL_GPL(of_devfreq_cooling_register);
466 * devfreq_cooling_register() - Register devfreq cooling device.
467 * @df: Pointer to devfreq device.
469 struct thermal_cooling_device *devfreq_cooling_register(struct devfreq *df)
471 return of_devfreq_cooling_register(NULL, df);
473 EXPORT_SYMBOL_GPL(devfreq_cooling_register);
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.
481 * Register a devfreq cooling device and automatically register EM. The
482 * available OPPs must be registered for the device.
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
491 struct thermal_cooling_device *
492 devfreq_cooling_em_register(struct devfreq *df,
493 struct devfreq_cooling_power *dfc_power)
495 struct thermal_cooling_device *cdev;
499 if (IS_ERR_OR_NULL(df))
500 return ERR_PTR(-EINVAL);
502 dev = df->dev.parent;
504 ret = dev_pm_opp_of_register_em(dev, NULL);
506 dev_dbg(dev, "Unable to register EM for devfreq cooling device (%d)\n",
509 cdev = of_devfreq_cooling_register_power(dev->of_node, df, dfc_power);
511 if (IS_ERR_OR_NULL(cdev))
512 em_dev_unregister_perf_domain(dev);
516 EXPORT_SYMBOL_GPL(devfreq_cooling_em_register);
519 * devfreq_cooling_unregister() - Unregister devfreq cooling device.
520 * @cdev: Pointer to devfreq cooling device to unregister.
522 * Unregisters devfreq cooling device and related Energy Model if it was
525 void devfreq_cooling_unregister(struct thermal_cooling_device *cdev)
527 struct devfreq_cooling_device *dfc;
530 if (IS_ERR_OR_NULL(cdev))
534 dev = dfc->devfreq->dev.parent;
536 thermal_cooling_device_unregister(dfc->cdev);
537 dev_pm_qos_remove_request(&dfc->req_max_freq);
539 em_dev_unregister_perf_domain(dev);
541 kfree(dfc->freq_table);
544 EXPORT_SYMBOL_GPL(devfreq_cooling_unregister);