.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
---|
1 | 2 | /* |
---|
2 | 3 | * devfreq_cooling: Thermal cooling device implementation for devices using |
---|
3 | 4 | * devfreq |
---|
4 | 5 | * |
---|
5 | 6 | * 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 | 7 | * |
---|
16 | 8 | * TODO: |
---|
17 | 9 | * - If OPPs are added or removed after devfreq cooling has |
---|
.. | .. |
---|
24 | 16 | #include <linux/idr.h> |
---|
25 | 17 | #include <linux/slab.h> |
---|
26 | 18 | #include <linux/pm_opp.h> |
---|
| 19 | +#include <linux/pm_qos.h> |
---|
27 | 20 | #include <linux/thermal.h> |
---|
28 | 21 | |
---|
29 | 22 | #include <trace/events/thermal.h> |
---|
30 | 23 | |
---|
31 | | -#define SCALE_ERROR_MITIGATION 100 |
---|
| 24 | +#define HZ_PER_KHZ 1000 |
---|
| 25 | +#define SCALE_ERROR_MITIGATION 100 |
---|
32 | 26 | |
---|
33 | 27 | static DEFINE_IDA(devfreq_ida); |
---|
34 | 28 | |
---|
.. | .. |
---|
53 | 47 | * 'utilization' (which is 'busy_time / 'total_time'). |
---|
54 | 48 | * The 'res_util' range is from 100 to (power_table[state] * 100) |
---|
55 | 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. |
---|
56 | 53 | */ |
---|
57 | 54 | struct devfreq_cooling_device { |
---|
58 | 55 | int id; |
---|
.. | .. |
---|
65 | 62 | struct devfreq_cooling_power *power_ops; |
---|
66 | 63 | u32 res_util; |
---|
67 | 64 | int capped_state; |
---|
| 65 | + struct dev_pm_qos_request req_max_freq; |
---|
68 | 66 | }; |
---|
69 | | - |
---|
70 | | -/** |
---|
71 | | - * partition_enable_opps() - disable all opps above a given state |
---|
72 | | - * @dfc: Pointer to devfreq we are operating on |
---|
73 | | - * @cdev_state: cooling device state we're setting |
---|
74 | | - * |
---|
75 | | - * Go through the OPPs of the device, enabling all OPPs until |
---|
76 | | - * @cdev_state and disabling those frequencies above it. |
---|
77 | | - */ |
---|
78 | | -static int partition_enable_opps(struct devfreq_cooling_device *dfc, |
---|
79 | | - unsigned long cdev_state) |
---|
80 | | -{ |
---|
81 | | - int i; |
---|
82 | | - struct device *dev = dfc->devfreq->dev.parent; |
---|
83 | | - |
---|
84 | | - for (i = 0; i < dfc->freq_table_size; i++) { |
---|
85 | | - struct dev_pm_opp *opp; |
---|
86 | | - int ret = 0; |
---|
87 | | - unsigned int freq = dfc->freq_table[i]; |
---|
88 | | - bool want_enable = i >= cdev_state ? true : false; |
---|
89 | | - |
---|
90 | | - opp = dev_pm_opp_find_freq_exact(dev, freq, !want_enable); |
---|
91 | | - |
---|
92 | | - if (PTR_ERR(opp) == -ERANGE) |
---|
93 | | - continue; |
---|
94 | | - else if (IS_ERR(opp)) |
---|
95 | | - return PTR_ERR(opp); |
---|
96 | | - |
---|
97 | | - dev_pm_opp_put(opp); |
---|
98 | | - |
---|
99 | | - if (want_enable) |
---|
100 | | - ret = dev_pm_opp_enable(dev, freq); |
---|
101 | | - else |
---|
102 | | - ret = dev_pm_opp_disable(dev, freq); |
---|
103 | | - |
---|
104 | | - if (ret) |
---|
105 | | - return ret; |
---|
106 | | - } |
---|
107 | | - |
---|
108 | | - return 0; |
---|
109 | | -} |
---|
110 | 67 | |
---|
111 | 68 | static int devfreq_cooling_get_max_state(struct thermal_cooling_device *cdev, |
---|
112 | 69 | unsigned long *state) |
---|
.. | .. |
---|
134 | 91 | struct devfreq_cooling_device *dfc = cdev->devdata; |
---|
135 | 92 | struct devfreq *df = dfc->devfreq; |
---|
136 | 93 | struct device *dev = df->dev.parent; |
---|
137 | | - int ret; |
---|
| 94 | + unsigned long freq; |
---|
138 | 95 | |
---|
139 | 96 | if (state == dfc->cooling_state) |
---|
140 | 97 | return 0; |
---|
.. | .. |
---|
144 | 101 | if (state >= dfc->freq_table_size) |
---|
145 | 102 | return -EINVAL; |
---|
146 | 103 | |
---|
147 | | - ret = partition_enable_opps(dfc, state); |
---|
148 | | - if (ret) |
---|
149 | | - return ret; |
---|
| 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)); |
---|
150 | 108 | |
---|
151 | 109 | dfc->cooling_state = state; |
---|
152 | 110 | |
---|
.. | .. |
---|
271 | 229 | |
---|
272 | 230 | |
---|
273 | 231 | static int devfreq_cooling_get_requested_power(struct thermal_cooling_device *cdev, |
---|
274 | | - struct thermal_zone_device *tz, |
---|
275 | 232 | u32 *power) |
---|
276 | 233 | { |
---|
277 | 234 | struct devfreq_cooling_device *dfc = cdev->devdata; |
---|
.. | .. |
---|
283 | 240 | u32 dyn_power = 0; |
---|
284 | 241 | u32 static_power = 0; |
---|
285 | 242 | int res; |
---|
286 | | - |
---|
287 | | - if (status->update) { |
---|
288 | | - if (df->profile->get_dev_status(df->dev.parent, |
---|
289 | | - &df->last_status)) { |
---|
290 | | - status->busy_time = 1; |
---|
291 | | - status->total_time = 1; |
---|
292 | | - } |
---|
293 | | - } |
---|
294 | 243 | |
---|
295 | 244 | state = freq_get_state(dfc, freq); |
---|
296 | 245 | if (state == THERMAL_CSTATE_INVALID) { |
---|
.. | .. |
---|
339 | 288 | } |
---|
340 | 289 | |
---|
341 | 290 | static int devfreq_cooling_state2power(struct thermal_cooling_device *cdev, |
---|
342 | | - struct thermal_zone_device *tz, |
---|
343 | 291 | unsigned long state, |
---|
344 | 292 | u32 *power) |
---|
345 | 293 | { |
---|
.. | .. |
---|
358 | 306 | } |
---|
359 | 307 | |
---|
360 | 308 | static int devfreq_cooling_power2state(struct thermal_cooling_device *cdev, |
---|
361 | | - struct thermal_zone_device *tz, |
---|
362 | 309 | u32 power, unsigned long *state) |
---|
363 | 310 | { |
---|
364 | 311 | struct devfreq_cooling_device *dfc = cdev->devdata; |
---|
.. | .. |
---|
537 | 484 | if (err) |
---|
538 | 485 | goto free_dfc; |
---|
539 | 486 | |
---|
540 | | - err = ida_simple_get(&devfreq_ida, 0, 0, GFP_KERNEL); |
---|
| 487 | + err = dev_pm_qos_add_request(df->dev.parent, &dfc->req_max_freq, |
---|
| 488 | + DEV_PM_QOS_MAX_FREQUENCY, |
---|
| 489 | + PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE); |
---|
541 | 490 | if (err < 0) |
---|
542 | 491 | goto free_tables; |
---|
| 492 | + |
---|
| 493 | + err = ida_simple_get(&devfreq_ida, 0, 0, GFP_KERNEL); |
---|
| 494 | + if (err < 0) |
---|
| 495 | + goto remove_qos_req; |
---|
543 | 496 | dfc->id = err; |
---|
544 | 497 | |
---|
545 | 498 | snprintf(dev_name, sizeof(dev_name), "thermal-devfreq-%d", dfc->id); |
---|
.. | .. |
---|
560 | 513 | |
---|
561 | 514 | release_ida: |
---|
562 | 515 | ida_simple_remove(&devfreq_ida, dfc->id); |
---|
| 516 | + |
---|
| 517 | +remove_qos_req: |
---|
| 518 | + dev_pm_qos_remove_request(&dfc->req_max_freq); |
---|
| 519 | + |
---|
563 | 520 | free_tables: |
---|
564 | 521 | kfree(dfc->power_table); |
---|
565 | 522 | kfree(dfc->freq_table); |
---|
.. | .. |
---|
595 | 552 | |
---|
596 | 553 | /** |
---|
597 | 554 | * devfreq_cooling_unregister() - Unregister devfreq cooling device. |
---|
598 | | - * @dfc: Pointer to devfreq cooling device to unregister. |
---|
| 555 | + * @cdev: Pointer to devfreq cooling device to unregister. |
---|
599 | 556 | */ |
---|
600 | 557 | void devfreq_cooling_unregister(struct thermal_cooling_device *cdev) |
---|
601 | 558 | { |
---|
.. | .. |
---|
608 | 565 | |
---|
609 | 566 | thermal_cooling_device_unregister(dfc->cdev); |
---|
610 | 567 | ida_simple_remove(&devfreq_ida, dfc->id); |
---|
| 568 | + dev_pm_qos_remove_request(&dfc->req_max_freq); |
---|
611 | 569 | kfree(dfc->power_table); |
---|
612 | 570 | kfree(dfc->freq_table); |
---|
613 | 571 | |
---|