Lukasz Luba | 0de967f | 2020-07-30 17:51:17 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 2 | /* |
| 3 | * devfreq_cooling: Thermal cooling device implementation for devices using |
| 4 | * devfreq |
| 5 | * |
| 6 | * Copyright (C) 2014-2015 ARM Limited |
| 7 | * |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 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> |
Matthew Wilcox | 2f96c03 | 2016-12-21 09:47:06 -0800 | [diff] [blame] | 16 | #include <linux/idr.h> |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 17 | #include <linux/slab.h> |
| 18 | #include <linux/pm_opp.h> |
Matthias Kaehlcke | 04fa9c8 | 2020-03-18 11:45:46 +0000 | [diff] [blame] | 19 | #include <linux/pm_qos.h> |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 20 | #include <linux/thermal.h> |
| 21 | |
Javi Merino | 9876b1a | 2015-09-10 18:09:31 +0100 | [diff] [blame] | 22 | #include <trace/events/thermal.h> |
| 23 | |
Matthias Kaehlcke | 04fa9c8 | 2020-03-18 11:45:46 +0000 | [diff] [blame] | 24 | #define HZ_PER_KHZ 1000 |
| 25 | #define SCALE_ERROR_MITIGATION 100 |
Lukasz Luba | 2be83da | 2017-05-04 12:34:32 +0100 | [diff] [blame] | 26 | |
Matthew Wilcox | 2f96c03 | 2016-12-21 09:47:06 -0800 | [diff] [blame] | 27 | static DEFINE_IDA(devfreq_ida); |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 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. |
Lukasz Luba | 2be83da | 2017-05-04 12:34:32 +0100 | [diff] [blame] | 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'. |
Amit Kucheria | 1b5cb95 | 2019-11-20 21:15:13 +0530 | [diff] [blame] | 50 | * @capped_state: index to cooling state with in dynamic power budget |
Matthias Kaehlcke | 04fa9c8 | 2020-03-18 11:45:46 +0000 | [diff] [blame] | 51 | * @req_max_freq: PM QoS request for limiting the maximum frequency |
| 52 | * of the devfreq device. |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 53 | */ |
| 54 | struct 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; |
Lukasz Luba | 2be83da | 2017-05-04 12:34:32 +0100 | [diff] [blame] | 63 | u32 res_util; |
| 64 | int capped_state; |
Matthias Kaehlcke | 04fa9c8 | 2020-03-18 11:45:46 +0000 | [diff] [blame] | 65 | struct dev_pm_qos_request req_max_freq; |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 66 | }; |
| 67 | |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 68 | static 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 | |
| 78 | static 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 | |
| 88 | static 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; |
Matthias Kaehlcke | 04fa9c8 | 2020-03-18 11:45:46 +0000 | [diff] [blame] | 94 | unsigned long freq; |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 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 | |
Matthias Kaehlcke | 04fa9c8 | 2020-03-18 11:45:46 +0000 | [diff] [blame] | 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)); |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 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 | */ |
| 122 | static unsigned long |
| 123 | freq_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 | |
Lukasz Luba | e34cab4 | 2017-05-04 12:34:31 +0100 | [diff] [blame] | 135 | static unsigned long get_voltage(struct devfreq *df, unsigned long freq) |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 136 | { |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 137 | struct device *dev = df->dev.parent; |
| 138 | unsigned long voltage; |
| 139 | struct dev_pm_opp *opp; |
| 140 | |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 141 | opp = dev_pm_opp_find_freq_exact(dev, freq, true); |
Viresh Kumar | a4e49c9b | 2017-02-07 09:40:01 +0530 | [diff] [blame] | 142 | if (PTR_ERR(opp) == -ERANGE) |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 143 | opp = dev_pm_opp_find_freq_exact(dev, freq, false); |
| 144 | |
Viresh Kumar | afd1f4e0 | 2017-02-07 09:40:03 +0530 | [diff] [blame] | 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 | |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 151 | voltage = dev_pm_opp_get_voltage(opp) / 1000; /* mV */ |
Viresh Kumar | 8a31d9d9 | 2017-01-23 10:11:47 +0530 | [diff] [blame] | 152 | dev_pm_opp_put(opp); |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 153 | |
| 154 | if (voltage == 0) { |
Viresh Kumar | 8327b83 | 2017-02-07 09:40:02 +0530 | [diff] [blame] | 155 | dev_err_ratelimited(dev, |
Viresh Kumar | afd1f4e0 | 2017-02-07 09:40:03 +0530 | [diff] [blame] | 156 | "Failed to get voltage for frequency %lu\n", |
| 157 | freq); |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 158 | } |
| 159 | |
Lukasz Luba | e34cab4 | 2017-05-04 12:34:31 +0100 | [diff] [blame] | 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 | */ |
| 173 | static unsigned long |
| 174 | get_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 | |
Javi Merino | 3aa5374 | 2016-09-15 15:44:23 +0100 | [diff] [blame] | 187 | return dfc->power_ops->get_static_power(df, voltage); |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 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 | */ |
| 202 | static unsigned long |
| 203 | get_dynamic_power(struct devfreq_cooling_device *dfc, unsigned long freq, |
| 204 | unsigned long voltage) |
| 205 | { |
Javi Merino | 61c8e8a | 2015-11-02 19:03:04 +0000 | [diff] [blame] | 206 | u64 power; |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 207 | u32 freq_mhz; |
| 208 | struct devfreq_cooling_power *dfc_power = dfc->power_ops; |
| 209 | |
| 210 | if (dfc_power->get_dynamic_power) |
Javi Merino | 3aa5374 | 2016-09-15 15:44:23 +0100 | [diff] [blame] | 211 | return dfc_power->get_dynamic_power(dfc->devfreq, freq, |
| 212 | voltage); |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 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 | |
Lukasz Luba | 2be83da | 2017-05-04 12:34:32 +0100 | [diff] [blame] | 221 | |
| 222 | static 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 | |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 231 | static int devfreq_cooling_get_requested_power(struct thermal_cooling_device *cdev, |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 232 | u32 *power) |
| 233 | { |
| 234 | struct devfreq_cooling_device *dfc = cdev->devdata; |
| 235 | struct devfreq *df = dfc->devfreq; |
| 236 | struct devfreq_dev_status *status = &df->last_status; |
| 237 | unsigned long state; |
| 238 | unsigned long freq = status->current_frequency; |
Lukasz Luba | 2be83da | 2017-05-04 12:34:32 +0100 | [diff] [blame] | 239 | unsigned long voltage; |
| 240 | u32 dyn_power = 0; |
| 241 | u32 static_power = 0; |
| 242 | int res; |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 243 | |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 244 | state = freq_get_state(dfc, freq); |
Lukasz Luba | 2be83da | 2017-05-04 12:34:32 +0100 | [diff] [blame] | 245 | if (state == THERMAL_CSTATE_INVALID) { |
| 246 | res = -EAGAIN; |
| 247 | goto fail; |
| 248 | } |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 249 | |
Lukasz Luba | 2be83da | 2017-05-04 12:34:32 +0100 | [diff] [blame] | 250 | if (dfc->power_ops->get_real_power) { |
| 251 | voltage = get_voltage(df, freq); |
| 252 | if (voltage == 0) { |
| 253 | res = -EINVAL; |
| 254 | goto fail; |
| 255 | } |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 256 | |
Lukasz Luba | 2be83da | 2017-05-04 12:34:32 +0100 | [diff] [blame] | 257 | res = dfc->power_ops->get_real_power(df, power, freq, voltage); |
| 258 | if (!res) { |
| 259 | state = dfc->capped_state; |
| 260 | dfc->res_util = dfc->power_table[state]; |
| 261 | dfc->res_util *= SCALE_ERROR_MITIGATION; |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 262 | |
Lukasz Luba | 2be83da | 2017-05-04 12:34:32 +0100 | [diff] [blame] | 263 | if (*power > 1) |
| 264 | dfc->res_util /= *power; |
| 265 | } else { |
| 266 | goto fail; |
| 267 | } |
| 268 | } else { |
| 269 | dyn_power = dfc->power_table[state]; |
| 270 | |
| 271 | /* Scale dynamic power for utilization */ |
| 272 | dyn_power *= status->busy_time; |
| 273 | dyn_power /= status->total_time; |
| 274 | /* Get static power */ |
| 275 | static_power = get_static_power(dfc, freq); |
| 276 | |
| 277 | *power = dyn_power + static_power; |
| 278 | } |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 279 | |
Javi Merino | 9876b1a | 2015-09-10 18:09:31 +0100 | [diff] [blame] | 280 | trace_thermal_power_devfreq_get_power(cdev, status, freq, dyn_power, |
Lukasz Luba | 771ffa1 | 2017-05-04 12:34:33 +0100 | [diff] [blame] | 281 | static_power, *power); |
Javi Merino | 9876b1a | 2015-09-10 18:09:31 +0100 | [diff] [blame] | 282 | |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 283 | return 0; |
Lukasz Luba | 2be83da | 2017-05-04 12:34:32 +0100 | [diff] [blame] | 284 | fail: |
| 285 | /* It is safe to set max in this case */ |
| 286 | dfc->res_util = SCALE_ERROR_MITIGATION; |
| 287 | return res; |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | static int devfreq_cooling_state2power(struct thermal_cooling_device *cdev, |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 291 | unsigned long state, |
| 292 | u32 *power) |
| 293 | { |
| 294 | struct devfreq_cooling_device *dfc = cdev->devdata; |
| 295 | unsigned long freq; |
| 296 | u32 static_power; |
| 297 | |
Shawn Lin | e3da1cb | 2016-08-22 16:08:06 +0800 | [diff] [blame] | 298 | if (state >= dfc->freq_table_size) |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 299 | return -EINVAL; |
| 300 | |
| 301 | freq = dfc->freq_table[state]; |
| 302 | static_power = get_static_power(dfc, freq); |
| 303 | |
| 304 | *power = dfc->power_table[state] + static_power; |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | static int devfreq_cooling_power2state(struct thermal_cooling_device *cdev, |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 309 | u32 power, unsigned long *state) |
| 310 | { |
| 311 | struct devfreq_cooling_device *dfc = cdev->devdata; |
| 312 | struct devfreq *df = dfc->devfreq; |
| 313 | struct devfreq_dev_status *status = &df->last_status; |
| 314 | unsigned long freq = status->current_frequency; |
| 315 | unsigned long busy_time; |
| 316 | s32 dyn_power; |
| 317 | u32 static_power; |
Lukasz Luba | 2be83da | 2017-05-04 12:34:32 +0100 | [diff] [blame] | 318 | s32 est_power; |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 319 | int i; |
| 320 | |
Lukasz Luba | 2be83da | 2017-05-04 12:34:32 +0100 | [diff] [blame] | 321 | if (dfc->power_ops->get_real_power) { |
| 322 | /* Scale for resource utilization */ |
| 323 | est_power = power * dfc->res_util; |
| 324 | est_power /= SCALE_ERROR_MITIGATION; |
| 325 | } else { |
| 326 | static_power = get_static_power(dfc, freq); |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 327 | |
Lukasz Luba | 2be83da | 2017-05-04 12:34:32 +0100 | [diff] [blame] | 328 | dyn_power = power - static_power; |
| 329 | dyn_power = dyn_power > 0 ? dyn_power : 0; |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 330 | |
Lukasz Luba | 2be83da | 2017-05-04 12:34:32 +0100 | [diff] [blame] | 331 | /* Scale dynamic power for utilization */ |
| 332 | busy_time = status->busy_time ?: 1; |
| 333 | est_power = (dyn_power * status->total_time) / busy_time; |
| 334 | } |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 335 | |
| 336 | /* |
| 337 | * Find the first cooling state that is within the power |
| 338 | * budget for dynamic power. |
| 339 | */ |
| 340 | for (i = 0; i < dfc->freq_table_size - 1; i++) |
Lukasz Luba | 2be83da | 2017-05-04 12:34:32 +0100 | [diff] [blame] | 341 | if (est_power >= dfc->power_table[i]) |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 342 | break; |
| 343 | |
| 344 | *state = i; |
Lukasz Luba | 2be83da | 2017-05-04 12:34:32 +0100 | [diff] [blame] | 345 | dfc->capped_state = i; |
Javi Merino | 9876b1a | 2015-09-10 18:09:31 +0100 | [diff] [blame] | 346 | trace_thermal_power_devfreq_limit(cdev, freq, *state, power); |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | static struct thermal_cooling_device_ops devfreq_cooling_ops = { |
| 351 | .get_max_state = devfreq_cooling_get_max_state, |
| 352 | .get_cur_state = devfreq_cooling_get_cur_state, |
| 353 | .set_cur_state = devfreq_cooling_set_cur_state, |
| 354 | }; |
| 355 | |
| 356 | /** |
| 357 | * devfreq_cooling_gen_tables() - Generate power and freq tables. |
| 358 | * @dfc: Pointer to devfreq cooling device. |
| 359 | * |
| 360 | * Generate power and frequency tables: the power table hold the |
| 361 | * device's maximum power usage at each cooling state (OPP). The |
| 362 | * static and dynamic power using the appropriate voltage and |
| 363 | * frequency for the state, is acquired from the struct |
| 364 | * devfreq_cooling_power, and summed to make the maximum power draw. |
| 365 | * |
| 366 | * The frequency table holds the frequencies in descending order. |
| 367 | * That way its indexed by cooling device state. |
| 368 | * |
| 369 | * The tables are malloced, and pointers put in dfc. They must be |
| 370 | * freed when unregistering the devfreq cooling device. |
| 371 | * |
| 372 | * Return: 0 on success, negative error code on failure. |
| 373 | */ |
| 374 | static int devfreq_cooling_gen_tables(struct devfreq_cooling_device *dfc) |
| 375 | { |
| 376 | struct devfreq *df = dfc->devfreq; |
| 377 | struct device *dev = df->dev.parent; |
| 378 | int ret, num_opps; |
| 379 | unsigned long freq; |
| 380 | u32 *power_table = NULL; |
| 381 | u32 *freq_table; |
| 382 | int i; |
| 383 | |
| 384 | num_opps = dev_pm_opp_get_opp_count(dev); |
| 385 | |
| 386 | if (dfc->power_ops) { |
| 387 | power_table = kcalloc(num_opps, sizeof(*power_table), |
| 388 | GFP_KERNEL); |
| 389 | if (!power_table) |
Dan Carpenter | ce5ee16 | 2015-11-04 16:36:20 +0300 | [diff] [blame] | 390 | return -ENOMEM; |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | freq_table = kcalloc(num_opps, sizeof(*freq_table), |
| 394 | GFP_KERNEL); |
| 395 | if (!freq_table) { |
| 396 | ret = -ENOMEM; |
| 397 | goto free_power_table; |
| 398 | } |
| 399 | |
| 400 | for (i = 0, freq = ULONG_MAX; i < num_opps; i++, freq--) { |
Lukasz Luba | 2be83da | 2017-05-04 12:34:32 +0100 | [diff] [blame] | 401 | unsigned long power, voltage; |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 402 | struct dev_pm_opp *opp; |
| 403 | |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 404 | opp = dev_pm_opp_find_freq_floor(dev, &freq); |
| 405 | if (IS_ERR(opp)) { |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 406 | ret = PTR_ERR(opp); |
| 407 | goto free_tables; |
| 408 | } |
| 409 | |
| 410 | voltage = dev_pm_opp_get_voltage(opp) / 1000; /* mV */ |
Viresh Kumar | 8a31d9d9 | 2017-01-23 10:11:47 +0530 | [diff] [blame] | 411 | dev_pm_opp_put(opp); |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 412 | |
| 413 | if (dfc->power_ops) { |
Lukasz Luba | 2be83da | 2017-05-04 12:34:32 +0100 | [diff] [blame] | 414 | if (dfc->power_ops->get_real_power) |
| 415 | power = get_total_power(dfc, freq, voltage); |
| 416 | else |
| 417 | power = get_dynamic_power(dfc, freq, voltage); |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 418 | |
Lukasz Luba | 2be83da | 2017-05-04 12:34:32 +0100 | [diff] [blame] | 419 | dev_dbg(dev, "Power table: %lu MHz @ %lu mV: %lu = %lu mW\n", |
| 420 | freq / 1000000, voltage, power, power); |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 421 | |
Lukasz Luba | 2be83da | 2017-05-04 12:34:32 +0100 | [diff] [blame] | 422 | power_table[i] = power; |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | freq_table[i] = freq; |
| 426 | } |
| 427 | |
| 428 | if (dfc->power_ops) |
| 429 | dfc->power_table = power_table; |
| 430 | |
| 431 | dfc->freq_table = freq_table; |
| 432 | dfc->freq_table_size = num_opps; |
| 433 | |
| 434 | return 0; |
| 435 | |
| 436 | free_tables: |
| 437 | kfree(freq_table); |
| 438 | free_power_table: |
| 439 | kfree(power_table); |
| 440 | |
| 441 | return ret; |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * of_devfreq_cooling_register_power() - Register devfreq cooling device, |
| 446 | * with OF and power information. |
| 447 | * @np: Pointer to OF device_node. |
| 448 | * @df: Pointer to devfreq device. |
| 449 | * @dfc_power: Pointer to devfreq_cooling_power. |
| 450 | * |
| 451 | * Register a devfreq cooling device. The available OPPs must be |
| 452 | * registered on the device. |
| 453 | * |
| 454 | * If @dfc_power is provided, the cooling device is registered with the |
| 455 | * power extensions. For the power extensions to work correctly, |
| 456 | * devfreq should use the simple_ondemand governor, other governors |
| 457 | * are not currently supported. |
| 458 | */ |
Javi Merino | 3c99c2c | 2015-11-02 19:03:03 +0000 | [diff] [blame] | 459 | struct thermal_cooling_device * |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 460 | of_devfreq_cooling_register_power(struct device_node *np, struct devfreq *df, |
| 461 | struct devfreq_cooling_power *dfc_power) |
| 462 | { |
| 463 | struct thermal_cooling_device *cdev; |
| 464 | struct devfreq_cooling_device *dfc; |
| 465 | char dev_name[THERMAL_NAME_LENGTH]; |
| 466 | int err; |
| 467 | |
| 468 | dfc = kzalloc(sizeof(*dfc), GFP_KERNEL); |
| 469 | if (!dfc) |
| 470 | return ERR_PTR(-ENOMEM); |
| 471 | |
| 472 | dfc->devfreq = df; |
| 473 | |
| 474 | if (dfc_power) { |
| 475 | dfc->power_ops = dfc_power; |
| 476 | |
| 477 | devfreq_cooling_ops.get_requested_power = |
| 478 | devfreq_cooling_get_requested_power; |
| 479 | devfreq_cooling_ops.state2power = devfreq_cooling_state2power; |
| 480 | devfreq_cooling_ops.power2state = devfreq_cooling_power2state; |
| 481 | } |
| 482 | |
| 483 | err = devfreq_cooling_gen_tables(dfc); |
| 484 | if (err) |
| 485 | goto free_dfc; |
| 486 | |
Matthias Kaehlcke | 04fa9c8 | 2020-03-18 11:45:46 +0000 | [diff] [blame] | 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); |
Matthew Wilcox | 2f96c03 | 2016-12-21 09:47:06 -0800 | [diff] [blame] | 490 | if (err < 0) |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 491 | goto free_tables; |
Matthias Kaehlcke | 04fa9c8 | 2020-03-18 11:45:46 +0000 | [diff] [blame] | 492 | |
| 493 | err = ida_simple_get(&devfreq_ida, 0, 0, GFP_KERNEL); |
| 494 | if (err < 0) |
| 495 | goto remove_qos_req; |
Matthew Wilcox | 2f96c03 | 2016-12-21 09:47:06 -0800 | [diff] [blame] | 496 | dfc->id = err; |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 497 | |
| 498 | snprintf(dev_name, sizeof(dev_name), "thermal-devfreq-%d", dfc->id); |
| 499 | |
| 500 | cdev = thermal_of_cooling_device_register(np, dev_name, dfc, |
| 501 | &devfreq_cooling_ops); |
| 502 | if (IS_ERR(cdev)) { |
| 503 | err = PTR_ERR(cdev); |
| 504 | dev_err(df->dev.parent, |
| 505 | "Failed to register devfreq cooling device (%d)\n", |
| 506 | err); |
Matthew Wilcox | 2f96c03 | 2016-12-21 09:47:06 -0800 | [diff] [blame] | 507 | goto release_ida; |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | dfc->cdev = cdev; |
| 511 | |
Javi Merino | 3c99c2c | 2015-11-02 19:03:03 +0000 | [diff] [blame] | 512 | return cdev; |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 513 | |
Matthew Wilcox | 2f96c03 | 2016-12-21 09:47:06 -0800 | [diff] [blame] | 514 | release_ida: |
| 515 | ida_simple_remove(&devfreq_ida, dfc->id); |
Matthias Kaehlcke | 04fa9c8 | 2020-03-18 11:45:46 +0000 | [diff] [blame] | 516 | |
| 517 | remove_qos_req: |
| 518 | dev_pm_qos_remove_request(&dfc->req_max_freq); |
| 519 | |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 520 | free_tables: |
| 521 | kfree(dfc->power_table); |
| 522 | kfree(dfc->freq_table); |
| 523 | free_dfc: |
| 524 | kfree(dfc); |
| 525 | |
| 526 | return ERR_PTR(err); |
| 527 | } |
| 528 | EXPORT_SYMBOL_GPL(of_devfreq_cooling_register_power); |
| 529 | |
| 530 | /** |
| 531 | * of_devfreq_cooling_register() - Register devfreq cooling device, |
| 532 | * with OF information. |
| 533 | * @np: Pointer to OF device_node. |
| 534 | * @df: Pointer to devfreq device. |
| 535 | */ |
Javi Merino | 3c99c2c | 2015-11-02 19:03:03 +0000 | [diff] [blame] | 536 | struct thermal_cooling_device * |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 537 | of_devfreq_cooling_register(struct device_node *np, struct devfreq *df) |
| 538 | { |
| 539 | return of_devfreq_cooling_register_power(np, df, NULL); |
| 540 | } |
| 541 | EXPORT_SYMBOL_GPL(of_devfreq_cooling_register); |
| 542 | |
| 543 | /** |
| 544 | * devfreq_cooling_register() - Register devfreq cooling device. |
| 545 | * @df: Pointer to devfreq device. |
| 546 | */ |
Javi Merino | 3c99c2c | 2015-11-02 19:03:03 +0000 | [diff] [blame] | 547 | struct thermal_cooling_device *devfreq_cooling_register(struct devfreq *df) |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 548 | { |
| 549 | return of_devfreq_cooling_register(NULL, df); |
| 550 | } |
| 551 | EXPORT_SYMBOL_GPL(devfreq_cooling_register); |
| 552 | |
| 553 | /** |
| 554 | * devfreq_cooling_unregister() - Unregister devfreq cooling device. |
Amit Kucheria | 1b5cb95 | 2019-11-20 21:15:13 +0530 | [diff] [blame] | 555 | * @cdev: Pointer to devfreq cooling device to unregister. |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 556 | */ |
Javi Merino | 3c99c2c | 2015-11-02 19:03:03 +0000 | [diff] [blame] | 557 | void devfreq_cooling_unregister(struct thermal_cooling_device *cdev) |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 558 | { |
Javi Merino | 3c99c2c | 2015-11-02 19:03:03 +0000 | [diff] [blame] | 559 | struct devfreq_cooling_device *dfc; |
| 560 | |
| 561 | if (!cdev) |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 562 | return; |
| 563 | |
Javi Merino | 3c99c2c | 2015-11-02 19:03:03 +0000 | [diff] [blame] | 564 | dfc = cdev->devdata; |
| 565 | |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 566 | thermal_cooling_device_unregister(dfc->cdev); |
Matthew Wilcox | 2f96c03 | 2016-12-21 09:47:06 -0800 | [diff] [blame] | 567 | ida_simple_remove(&devfreq_ida, dfc->id); |
Matthias Kaehlcke | 04fa9c8 | 2020-03-18 11:45:46 +0000 | [diff] [blame] | 568 | dev_pm_qos_remove_request(&dfc->req_max_freq); |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 569 | kfree(dfc->power_table); |
| 570 | kfree(dfc->freq_table); |
| 571 | |
| 572 | kfree(dfc); |
| 573 | } |
| 574 | EXPORT_SYMBOL_GPL(devfreq_cooling_unregister); |