1 /* 2 * devfreq_cooling: Thermal cooling device implementation for devices using 3 * devfreq 4 * 5 * 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 * 16 * TODO: 17 * - If OPPs are added or removed after devfreq cooling has 18 * registered, the devfreq cooling won't react to it. 19 */ 20 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> 28 29 #include <trace/events/thermal.h> 30 31 static DEFINE_IDA(devfreq_ida); 32 33 /** 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 42 * the power is in mW. 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 47 * @power_table. 48 */ 49 struct devfreq_cooling_device { 50 int id; 51 struct thermal_cooling_device *cdev; 52 struct devfreq *devfreq; 53 unsigned long cooling_state; 54 u32 *power_table; 55 u32 *freq_table; 56 size_t freq_table_size; 57 struct devfreq_cooling_power *power_ops; 58 }; 59 60 /** 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 64 * 65 * Go through the OPPs of the device, enabling all OPPs until 66 * @cdev_state and disabling those frequencies above it. 67 */ 68 static int partition_enable_opps(struct devfreq_cooling_device *dfc, 69 unsigned long cdev_state) 70 { 71 int i; 72 struct device *dev = dfc->devfreq->dev.parent; 73 74 for (i = 0; i < dfc->freq_table_size; i++) { 75 struct dev_pm_opp *opp; 76 int ret = 0; 77 unsigned int freq = dfc->freq_table[i]; 78 bool want_enable = i >= cdev_state ? true : false; 79 80 opp = dev_pm_opp_find_freq_exact(dev, freq, !want_enable); 81 82 if (PTR_ERR(opp) == -ERANGE) 83 continue; 84 else if (IS_ERR(opp)) 85 return PTR_ERR(opp); 86 87 dev_pm_opp_put(opp); 88 89 if (want_enable) 90 ret = dev_pm_opp_enable(dev, freq); 91 else 92 ret = dev_pm_opp_disable(dev, freq); 93 94 if (ret) 95 return ret; 96 } 97 98 return 0; 99 } 100 101 static int devfreq_cooling_get_max_state(struct thermal_cooling_device *cdev, 102 unsigned long *state) 103 { 104 struct devfreq_cooling_device *dfc = cdev->devdata; 105 106 *state = dfc->freq_table_size - 1; 107 108 return 0; 109 } 110 111 static int devfreq_cooling_get_cur_state(struct thermal_cooling_device *cdev, 112 unsigned long *state) 113 { 114 struct devfreq_cooling_device *dfc = cdev->devdata; 115 116 *state = dfc->cooling_state; 117 118 return 0; 119 } 120 121 static int devfreq_cooling_set_cur_state(struct thermal_cooling_device *cdev, 122 unsigned long state) 123 { 124 struct devfreq_cooling_device *dfc = cdev->devdata; 125 struct devfreq *df = dfc->devfreq; 126 struct device *dev = df->dev.parent; 127 int ret; 128 129 if (state == dfc->cooling_state) 130 return 0; 131 132 dev_dbg(dev, "Setting cooling state %lu\n", state); 133 134 if (state >= dfc->freq_table_size) 135 return -EINVAL; 136 137 ret = partition_enable_opps(dfc, state); 138 if (ret) 139 return ret; 140 141 dfc->cooling_state = state; 142 143 return 0; 144 } 145 146 /** 147 * freq_get_state() - get the cooling state corresponding to a frequency 148 * @dfc: Pointer to devfreq cooling device 149 * @freq: frequency in Hz 150 * 151 * Return: the cooling state associated with the @freq, or 152 * THERMAL_CSTATE_INVALID if it wasn't found. 153 */ 154 static unsigned long 155 freq_get_state(struct devfreq_cooling_device *dfc, unsigned long freq) 156 { 157 int i; 158 159 for (i = 0; i < dfc->freq_table_size; i++) { 160 if (dfc->freq_table[i] == freq) 161 return i; 162 } 163 164 return THERMAL_CSTATE_INVALID; 165 } 166 167 /** 168 * get_static_power() - calculate the static power 169 * @dfc: Pointer to devfreq cooling device 170 * @freq: Frequency in Hz 171 * 172 * Calculate the static power in milliwatts using the supplied 173 * get_static_power(). The current voltage is calculated using the 174 * OPP library. If no get_static_power() was supplied, assume the 175 * static power is negligible. 176 */ 177 static unsigned long 178 get_static_power(struct devfreq_cooling_device *dfc, unsigned long freq) 179 { 180 struct devfreq *df = dfc->devfreq; 181 struct device *dev = df->dev.parent; 182 unsigned long voltage; 183 struct dev_pm_opp *opp; 184 185 if (!dfc->power_ops->get_static_power) 186 return 0; 187 188 opp = dev_pm_opp_find_freq_exact(dev, freq, true); 189 if (PTR_ERR(opp) == -ERANGE) 190 opp = dev_pm_opp_find_freq_exact(dev, freq, false); 191 192 if (IS_ERR(opp)) { 193 dev_err_ratelimited(dev, "Failed to find OPP for frequency %lu: %ld\n", 194 freq, PTR_ERR(opp)); 195 return 0; 196 } 197 198 voltage = dev_pm_opp_get_voltage(opp) / 1000; /* mV */ 199 dev_pm_opp_put(opp); 200 201 if (voltage == 0) { 202 dev_err_ratelimited(dev, 203 "Failed to get voltage for frequency %lu\n", 204 freq); 205 return 0; 206 } 207 208 return dfc->power_ops->get_static_power(df, voltage); 209 } 210 211 /** 212 * get_dynamic_power - calculate the dynamic power 213 * @dfc: Pointer to devfreq cooling device 214 * @freq: Frequency in Hz 215 * @voltage: Voltage in millivolts 216 * 217 * Calculate the dynamic power in milliwatts consumed by the device at 218 * frequency @freq and voltage @voltage. If the get_dynamic_power() 219 * was supplied as part of the devfreq_cooling_power struct, then that 220 * function is used. Otherwise, a simple power model (Pdyn = Coeff * 221 * Voltage^2 * Frequency) is used. 222 */ 223 static unsigned long 224 get_dynamic_power(struct devfreq_cooling_device *dfc, unsigned long freq, 225 unsigned long voltage) 226 { 227 u64 power; 228 u32 freq_mhz; 229 struct devfreq_cooling_power *dfc_power = dfc->power_ops; 230 231 if (dfc_power->get_dynamic_power) 232 return dfc_power->get_dynamic_power(dfc->devfreq, freq, 233 voltage); 234 235 freq_mhz = freq / 1000000; 236 power = (u64)dfc_power->dyn_power_coeff * freq_mhz * voltage * voltage; 237 do_div(power, 1000000000); 238 239 return power; 240 } 241 242 static int devfreq_cooling_get_requested_power(struct thermal_cooling_device *cdev, 243 struct thermal_zone_device *tz, 244 u32 *power) 245 { 246 struct devfreq_cooling_device *dfc = cdev->devdata; 247 struct devfreq *df = dfc->devfreq; 248 struct devfreq_dev_status *status = &df->last_status; 249 unsigned long state; 250 unsigned long freq = status->current_frequency; 251 u32 dyn_power, static_power; 252 253 /* Get dynamic power for state */ 254 state = freq_get_state(dfc, freq); 255 if (state == THERMAL_CSTATE_INVALID) 256 return -EAGAIN; 257 258 dyn_power = dfc->power_table[state]; 259 260 /* Scale dynamic power for utilization */ 261 dyn_power = (dyn_power * status->busy_time) / status->total_time; 262 263 /* Get static power */ 264 static_power = get_static_power(dfc, freq); 265 266 trace_thermal_power_devfreq_get_power(cdev, status, freq, dyn_power, 267 static_power); 268 269 *power = dyn_power + static_power; 270 271 return 0; 272 } 273 274 static int devfreq_cooling_state2power(struct thermal_cooling_device *cdev, 275 struct thermal_zone_device *tz, 276 unsigned long state, 277 u32 *power) 278 { 279 struct devfreq_cooling_device *dfc = cdev->devdata; 280 unsigned long freq; 281 u32 static_power; 282 283 if (state >= dfc->freq_table_size) 284 return -EINVAL; 285 286 freq = dfc->freq_table[state]; 287 static_power = get_static_power(dfc, freq); 288 289 *power = dfc->power_table[state] + static_power; 290 return 0; 291 } 292 293 static int devfreq_cooling_power2state(struct thermal_cooling_device *cdev, 294 struct thermal_zone_device *tz, 295 u32 power, unsigned long *state) 296 { 297 struct devfreq_cooling_device *dfc = cdev->devdata; 298 struct devfreq *df = dfc->devfreq; 299 struct devfreq_dev_status *status = &df->last_status; 300 unsigned long freq = status->current_frequency; 301 unsigned long busy_time; 302 s32 dyn_power; 303 u32 static_power; 304 int i; 305 306 static_power = get_static_power(dfc, freq); 307 308 dyn_power = power - static_power; 309 dyn_power = dyn_power > 0 ? dyn_power : 0; 310 311 /* Scale dynamic power for utilization */ 312 busy_time = status->busy_time ?: 1; 313 dyn_power = (dyn_power * status->total_time) / busy_time; 314 315 /* 316 * Find the first cooling state that is within the power 317 * budget for dynamic power. 318 */ 319 for (i = 0; i < dfc->freq_table_size - 1; i++) 320 if (dyn_power >= dfc->power_table[i]) 321 break; 322 323 *state = i; 324 trace_thermal_power_devfreq_limit(cdev, freq, *state, power); 325 return 0; 326 } 327 328 static struct thermal_cooling_device_ops devfreq_cooling_ops = { 329 .get_max_state = devfreq_cooling_get_max_state, 330 .get_cur_state = devfreq_cooling_get_cur_state, 331 .set_cur_state = devfreq_cooling_set_cur_state, 332 }; 333 334 /** 335 * devfreq_cooling_gen_tables() - Generate power and freq tables. 336 * @dfc: Pointer to devfreq cooling device. 337 * 338 * Generate power and frequency tables: the power table hold the 339 * device's maximum power usage at each cooling state (OPP). The 340 * static and dynamic power using the appropriate voltage and 341 * frequency for the state, is acquired from the struct 342 * devfreq_cooling_power, and summed to make the maximum power draw. 343 * 344 * The frequency table holds the frequencies in descending order. 345 * That way its indexed by cooling device state. 346 * 347 * The tables are malloced, and pointers put in dfc. They must be 348 * freed when unregistering the devfreq cooling device. 349 * 350 * Return: 0 on success, negative error code on failure. 351 */ 352 static int devfreq_cooling_gen_tables(struct devfreq_cooling_device *dfc) 353 { 354 struct devfreq *df = dfc->devfreq; 355 struct device *dev = df->dev.parent; 356 int ret, num_opps; 357 unsigned long freq; 358 u32 *power_table = NULL; 359 u32 *freq_table; 360 int i; 361 362 num_opps = dev_pm_opp_get_opp_count(dev); 363 364 if (dfc->power_ops) { 365 power_table = kcalloc(num_opps, sizeof(*power_table), 366 GFP_KERNEL); 367 if (!power_table) 368 return -ENOMEM; 369 } 370 371 freq_table = kcalloc(num_opps, sizeof(*freq_table), 372 GFP_KERNEL); 373 if (!freq_table) { 374 ret = -ENOMEM; 375 goto free_power_table; 376 } 377 378 for (i = 0, freq = ULONG_MAX; i < num_opps; i++, freq--) { 379 unsigned long power_dyn, voltage; 380 struct dev_pm_opp *opp; 381 382 opp = dev_pm_opp_find_freq_floor(dev, &freq); 383 if (IS_ERR(opp)) { 384 ret = PTR_ERR(opp); 385 goto free_tables; 386 } 387 388 voltage = dev_pm_opp_get_voltage(opp) / 1000; /* mV */ 389 dev_pm_opp_put(opp); 390 391 if (dfc->power_ops) { 392 power_dyn = get_dynamic_power(dfc, freq, voltage); 393 394 dev_dbg(dev, "Dynamic power table: %lu MHz @ %lu mV: %lu = %lu mW\n", 395 freq / 1000000, voltage, power_dyn, power_dyn); 396 397 power_table[i] = power_dyn; 398 } 399 400 freq_table[i] = freq; 401 } 402 403 if (dfc->power_ops) 404 dfc->power_table = power_table; 405 406 dfc->freq_table = freq_table; 407 dfc->freq_table_size = num_opps; 408 409 return 0; 410 411 free_tables: 412 kfree(freq_table); 413 free_power_table: 414 kfree(power_table); 415 416 return ret; 417 } 418 419 /** 420 * of_devfreq_cooling_register_power() - Register devfreq cooling device, 421 * with OF and power information. 422 * @np: Pointer to OF device_node. 423 * @df: Pointer to devfreq device. 424 * @dfc_power: Pointer to devfreq_cooling_power. 425 * 426 * Register a devfreq cooling device. The available OPPs must be 427 * registered on the device. 428 * 429 * If @dfc_power is provided, the cooling device is registered with the 430 * power extensions. For the power extensions to work correctly, 431 * devfreq should use the simple_ondemand governor, other governors 432 * are not currently supported. 433 */ 434 struct thermal_cooling_device * 435 of_devfreq_cooling_register_power(struct device_node *np, struct devfreq *df, 436 struct devfreq_cooling_power *dfc_power) 437 { 438 struct thermal_cooling_device *cdev; 439 struct devfreq_cooling_device *dfc; 440 char dev_name[THERMAL_NAME_LENGTH]; 441 int err; 442 443 dfc = kzalloc(sizeof(*dfc), GFP_KERNEL); 444 if (!dfc) 445 return ERR_PTR(-ENOMEM); 446 447 dfc->devfreq = df; 448 449 if (dfc_power) { 450 dfc->power_ops = dfc_power; 451 452 devfreq_cooling_ops.get_requested_power = 453 devfreq_cooling_get_requested_power; 454 devfreq_cooling_ops.state2power = devfreq_cooling_state2power; 455 devfreq_cooling_ops.power2state = devfreq_cooling_power2state; 456 } 457 458 err = devfreq_cooling_gen_tables(dfc); 459 if (err) 460 goto free_dfc; 461 462 err = ida_simple_get(&devfreq_ida, 0, 0, GFP_KERNEL); 463 if (err < 0) 464 goto free_tables; 465 dfc->id = err; 466 467 snprintf(dev_name, sizeof(dev_name), "thermal-devfreq-%d", dfc->id); 468 469 cdev = thermal_of_cooling_device_register(np, dev_name, dfc, 470 &devfreq_cooling_ops); 471 if (IS_ERR(cdev)) { 472 err = PTR_ERR(cdev); 473 dev_err(df->dev.parent, 474 "Failed to register devfreq cooling device (%d)\n", 475 err); 476 goto release_ida; 477 } 478 479 dfc->cdev = cdev; 480 481 return cdev; 482 483 release_ida: 484 ida_simple_remove(&devfreq_ida, dfc->id); 485 free_tables: 486 kfree(dfc->power_table); 487 kfree(dfc->freq_table); 488 free_dfc: 489 kfree(dfc); 490 491 return ERR_PTR(err); 492 } 493 EXPORT_SYMBOL_GPL(of_devfreq_cooling_register_power); 494 495 /** 496 * of_devfreq_cooling_register() - Register devfreq cooling device, 497 * with OF information. 498 * @np: Pointer to OF device_node. 499 * @df: Pointer to devfreq device. 500 */ 501 struct thermal_cooling_device * 502 of_devfreq_cooling_register(struct device_node *np, struct devfreq *df) 503 { 504 return of_devfreq_cooling_register_power(np, df, NULL); 505 } 506 EXPORT_SYMBOL_GPL(of_devfreq_cooling_register); 507 508 /** 509 * devfreq_cooling_register() - Register devfreq cooling device. 510 * @df: Pointer to devfreq device. 511 */ 512 struct thermal_cooling_device *devfreq_cooling_register(struct devfreq *df) 513 { 514 return of_devfreq_cooling_register(NULL, df); 515 } 516 EXPORT_SYMBOL_GPL(devfreq_cooling_register); 517 518 /** 519 * devfreq_cooling_unregister() - Unregister devfreq cooling device. 520 * @dfc: Pointer to devfreq cooling device to unregister. 521 */ 522 void devfreq_cooling_unregister(struct thermal_cooling_device *cdev) 523 { 524 struct devfreq_cooling_device *dfc; 525 526 if (!cdev) 527 return; 528 529 dfc = cdev->devdata; 530 531 thermal_cooling_device_unregister(dfc->cdev); 532 ida_simple_remove(&devfreq_ida, dfc->id); 533 kfree(dfc->power_table); 534 kfree(dfc->freq_table); 535 536 kfree(dfc); 537 } 538 EXPORT_SYMBOL_GPL(devfreq_cooling_unregister); 539