1 /* 2 * Copyright © 2012-2014 Intel Corporation 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 * IN THE SOFTWARE. 22 * 23 * Authors: 24 * Eugeni Dodonov <eugeni.dodonov@intel.com> 25 * Daniel Vetter <daniel.vetter@ffwll.ch> 26 * 27 */ 28 29 #include <linux/pm_runtime.h> 30 31 #include <drm/drm_print.h> 32 33 #include "i915_drv.h" 34 #include "i915_trace.h" 35 36 /** 37 * DOC: runtime pm 38 * 39 * The i915 driver supports dynamic enabling and disabling of entire hardware 40 * blocks at runtime. This is especially important on the display side where 41 * software is supposed to control many power gates manually on recent hardware, 42 * since on the GT side a lot of the power management is done by the hardware. 43 * But even there some manual control at the device level is required. 44 * 45 * Since i915 supports a diverse set of platforms with a unified codebase and 46 * hardware engineers just love to shuffle functionality around between power 47 * domains there's a sizeable amount of indirection required. This file provides 48 * generic functions to the driver for grabbing and releasing references for 49 * abstract power domains. It then maps those to the actual power wells 50 * present for a given platform. 51 */ 52 53 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_RUNTIME_PM) 54 55 #include <linux/sort.h> 56 57 #define STACKDEPTH 8 58 59 static noinline depot_stack_handle_t __save_depot_stack(void) 60 { 61 unsigned long entries[STACKDEPTH]; 62 unsigned int n; 63 64 n = stack_trace_save(entries, ARRAY_SIZE(entries), 1); 65 return stack_depot_save(entries, n, GFP_NOWAIT | __GFP_NOWARN); 66 } 67 68 static void init_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm) 69 { 70 spin_lock_init(&rpm->debug.lock); 71 stack_depot_init(); 72 } 73 74 static noinline depot_stack_handle_t 75 track_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm) 76 { 77 depot_stack_handle_t stack, *stacks; 78 unsigned long flags; 79 80 stack = __save_depot_stack(); 81 if (!stack) 82 return -1; 83 84 spin_lock_irqsave(&rpm->debug.lock, flags); 85 86 if (!rpm->debug.count) 87 rpm->debug.last_acquire = stack; 88 89 stacks = krealloc(rpm->debug.owners, 90 (rpm->debug.count + 1) * sizeof(*stacks), 91 GFP_NOWAIT | __GFP_NOWARN); 92 if (stacks) { 93 stacks[rpm->debug.count++] = stack; 94 rpm->debug.owners = stacks; 95 } else { 96 stack = -1; 97 } 98 99 spin_unlock_irqrestore(&rpm->debug.lock, flags); 100 101 return stack; 102 } 103 104 static void untrack_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm, 105 depot_stack_handle_t stack) 106 { 107 struct drm_i915_private *i915 = container_of(rpm, 108 struct drm_i915_private, 109 runtime_pm); 110 unsigned long flags, n; 111 bool found = false; 112 113 if (unlikely(stack == -1)) 114 return; 115 116 spin_lock_irqsave(&rpm->debug.lock, flags); 117 for (n = rpm->debug.count; n--; ) { 118 if (rpm->debug.owners[n] == stack) { 119 memmove(rpm->debug.owners + n, 120 rpm->debug.owners + n + 1, 121 (--rpm->debug.count - n) * sizeof(stack)); 122 found = true; 123 break; 124 } 125 } 126 spin_unlock_irqrestore(&rpm->debug.lock, flags); 127 128 if (drm_WARN(&i915->drm, !found, 129 "Unmatched wakeref (tracking %lu), count %u\n", 130 rpm->debug.count, atomic_read(&rpm->wakeref_count))) { 131 char *buf; 132 133 buf = kmalloc(PAGE_SIZE, GFP_NOWAIT | __GFP_NOWARN); 134 if (!buf) 135 return; 136 137 stack_depot_snprint(stack, buf, PAGE_SIZE, 2); 138 DRM_DEBUG_DRIVER("wakeref %x from\n%s", stack, buf); 139 140 stack = READ_ONCE(rpm->debug.last_release); 141 if (stack) { 142 stack_depot_snprint(stack, buf, PAGE_SIZE, 2); 143 DRM_DEBUG_DRIVER("wakeref last released at\n%s", buf); 144 } 145 146 kfree(buf); 147 } 148 } 149 150 static int cmphandle(const void *_a, const void *_b) 151 { 152 const depot_stack_handle_t * const a = _a, * const b = _b; 153 154 if (*a < *b) 155 return -1; 156 else if (*a > *b) 157 return 1; 158 else 159 return 0; 160 } 161 162 static void 163 __print_intel_runtime_pm_wakeref(struct drm_printer *p, 164 const struct intel_runtime_pm_debug *dbg) 165 { 166 unsigned long i; 167 char *buf; 168 169 buf = kmalloc(PAGE_SIZE, GFP_NOWAIT | __GFP_NOWARN); 170 if (!buf) 171 return; 172 173 if (dbg->last_acquire) { 174 stack_depot_snprint(dbg->last_acquire, buf, PAGE_SIZE, 2); 175 drm_printf(p, "Wakeref last acquired:\n%s", buf); 176 } 177 178 if (dbg->last_release) { 179 stack_depot_snprint(dbg->last_release, buf, PAGE_SIZE, 2); 180 drm_printf(p, "Wakeref last released:\n%s", buf); 181 } 182 183 drm_printf(p, "Wakeref count: %lu\n", dbg->count); 184 185 sort(dbg->owners, dbg->count, sizeof(*dbg->owners), cmphandle, NULL); 186 187 for (i = 0; i < dbg->count; i++) { 188 depot_stack_handle_t stack = dbg->owners[i]; 189 unsigned long rep; 190 191 rep = 1; 192 while (i + 1 < dbg->count && dbg->owners[i + 1] == stack) 193 rep++, i++; 194 stack_depot_snprint(stack, buf, PAGE_SIZE, 2); 195 drm_printf(p, "Wakeref x%lu taken at:\n%s", rep, buf); 196 } 197 198 kfree(buf); 199 } 200 201 static noinline void 202 __untrack_all_wakerefs(struct intel_runtime_pm_debug *debug, 203 struct intel_runtime_pm_debug *saved) 204 { 205 *saved = *debug; 206 207 debug->owners = NULL; 208 debug->count = 0; 209 debug->last_release = __save_depot_stack(); 210 } 211 212 static void 213 dump_and_free_wakeref_tracking(struct intel_runtime_pm_debug *debug) 214 { 215 if (debug->count) { 216 struct drm_printer p = drm_debug_printer("i915"); 217 218 __print_intel_runtime_pm_wakeref(&p, debug); 219 } 220 221 kfree(debug->owners); 222 } 223 224 static noinline void 225 __intel_wakeref_dec_and_check_tracking(struct intel_runtime_pm *rpm) 226 { 227 struct intel_runtime_pm_debug dbg = {}; 228 unsigned long flags; 229 230 if (!atomic_dec_and_lock_irqsave(&rpm->wakeref_count, 231 &rpm->debug.lock, 232 flags)) 233 return; 234 235 __untrack_all_wakerefs(&rpm->debug, &dbg); 236 spin_unlock_irqrestore(&rpm->debug.lock, flags); 237 238 dump_and_free_wakeref_tracking(&dbg); 239 } 240 241 static noinline void 242 untrack_all_intel_runtime_pm_wakerefs(struct intel_runtime_pm *rpm) 243 { 244 struct intel_runtime_pm_debug dbg = {}; 245 unsigned long flags; 246 247 spin_lock_irqsave(&rpm->debug.lock, flags); 248 __untrack_all_wakerefs(&rpm->debug, &dbg); 249 spin_unlock_irqrestore(&rpm->debug.lock, flags); 250 251 dump_and_free_wakeref_tracking(&dbg); 252 } 253 254 void print_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm, 255 struct drm_printer *p) 256 { 257 struct intel_runtime_pm_debug dbg = {}; 258 259 do { 260 unsigned long alloc = dbg.count; 261 depot_stack_handle_t *s; 262 263 spin_lock_irq(&rpm->debug.lock); 264 dbg.count = rpm->debug.count; 265 if (dbg.count <= alloc) { 266 memcpy(dbg.owners, 267 rpm->debug.owners, 268 dbg.count * sizeof(*s)); 269 } 270 dbg.last_acquire = rpm->debug.last_acquire; 271 dbg.last_release = rpm->debug.last_release; 272 spin_unlock_irq(&rpm->debug.lock); 273 if (dbg.count <= alloc) 274 break; 275 276 s = krealloc(dbg.owners, 277 dbg.count * sizeof(*s), 278 GFP_NOWAIT | __GFP_NOWARN); 279 if (!s) 280 goto out; 281 282 dbg.owners = s; 283 } while (1); 284 285 __print_intel_runtime_pm_wakeref(p, &dbg); 286 287 out: 288 kfree(dbg.owners); 289 } 290 291 #else 292 293 static void init_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm) 294 { 295 } 296 297 static depot_stack_handle_t 298 track_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm) 299 { 300 return -1; 301 } 302 303 static void untrack_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm, 304 intel_wakeref_t wref) 305 { 306 } 307 308 static void 309 __intel_wakeref_dec_and_check_tracking(struct intel_runtime_pm *rpm) 310 { 311 atomic_dec(&rpm->wakeref_count); 312 } 313 314 static void 315 untrack_all_intel_runtime_pm_wakerefs(struct intel_runtime_pm *rpm) 316 { 317 } 318 319 #endif 320 321 static void 322 intel_runtime_pm_acquire(struct intel_runtime_pm *rpm, bool wakelock) 323 { 324 if (wakelock) { 325 atomic_add(1 + INTEL_RPM_WAKELOCK_BIAS, &rpm->wakeref_count); 326 assert_rpm_wakelock_held(rpm); 327 } else { 328 atomic_inc(&rpm->wakeref_count); 329 assert_rpm_raw_wakeref_held(rpm); 330 } 331 } 332 333 static void 334 intel_runtime_pm_release(struct intel_runtime_pm *rpm, int wakelock) 335 { 336 if (wakelock) { 337 assert_rpm_wakelock_held(rpm); 338 atomic_sub(INTEL_RPM_WAKELOCK_BIAS, &rpm->wakeref_count); 339 } else { 340 assert_rpm_raw_wakeref_held(rpm); 341 } 342 343 __intel_wakeref_dec_and_check_tracking(rpm); 344 } 345 346 static intel_wakeref_t __intel_runtime_pm_get(struct intel_runtime_pm *rpm, 347 bool wakelock) 348 { 349 struct drm_i915_private *i915 = container_of(rpm, 350 struct drm_i915_private, 351 runtime_pm); 352 int ret; 353 354 ret = pm_runtime_get_sync(rpm->kdev); 355 drm_WARN_ONCE(&i915->drm, ret < 0, 356 "pm_runtime_get_sync() failed: %d\n", ret); 357 358 intel_runtime_pm_acquire(rpm, wakelock); 359 360 return track_intel_runtime_pm_wakeref(rpm); 361 } 362 363 /** 364 * intel_runtime_pm_get_raw - grab a raw runtime pm reference 365 * @rpm: the intel_runtime_pm structure 366 * 367 * This is the unlocked version of intel_display_power_is_enabled() and should 368 * only be used from error capture and recovery code where deadlocks are 369 * possible. 370 * This function grabs a device-level runtime pm reference (mostly used for 371 * asynchronous PM management from display code) and ensures that it is powered 372 * up. Raw references are not considered during wakelock assert checks. 373 * 374 * Any runtime pm reference obtained by this function must have a symmetric 375 * call to intel_runtime_pm_put_raw() to release the reference again. 376 * 377 * Returns: the wakeref cookie to pass to intel_runtime_pm_put_raw(), evaluates 378 * as True if the wakeref was acquired, or False otherwise. 379 */ 380 intel_wakeref_t intel_runtime_pm_get_raw(struct intel_runtime_pm *rpm) 381 { 382 return __intel_runtime_pm_get(rpm, false); 383 } 384 385 /** 386 * intel_runtime_pm_get - grab a runtime pm reference 387 * @rpm: the intel_runtime_pm structure 388 * 389 * This function grabs a device-level runtime pm reference (mostly used for GEM 390 * code to ensure the GTT or GT is on) and ensures that it is powered up. 391 * 392 * Any runtime pm reference obtained by this function must have a symmetric 393 * call to intel_runtime_pm_put() to release the reference again. 394 * 395 * Returns: the wakeref cookie to pass to intel_runtime_pm_put() 396 */ 397 intel_wakeref_t intel_runtime_pm_get(struct intel_runtime_pm *rpm) 398 { 399 return __intel_runtime_pm_get(rpm, true); 400 } 401 402 /** 403 * __intel_runtime_pm_get_if_active - grab a runtime pm reference if device is active 404 * @rpm: the intel_runtime_pm structure 405 * @ignore_usecount: get a ref even if dev->power.usage_count is 0 406 * 407 * This function grabs a device-level runtime pm reference if the device is 408 * already active and ensures that it is powered up. It is illegal to try 409 * and access the HW should intel_runtime_pm_get_if_active() report failure. 410 * 411 * If @ignore_usecount is true, a reference will be acquired even if there is no 412 * user requiring the device to be powered up (dev->power.usage_count == 0). 413 * If the function returns false in this case then it's guaranteed that the 414 * device's runtime suspend hook has been called already or that it will be 415 * called (and hence it's also guaranteed that the device's runtime resume 416 * hook will be called eventually). 417 * 418 * Any runtime pm reference obtained by this function must have a symmetric 419 * call to intel_runtime_pm_put() to release the reference again. 420 * 421 * Returns: the wakeref cookie to pass to intel_runtime_pm_put(), evaluates 422 * as True if the wakeref was acquired, or False otherwise. 423 */ 424 static intel_wakeref_t __intel_runtime_pm_get_if_active(struct intel_runtime_pm *rpm, 425 bool ignore_usecount) 426 { 427 if (IS_ENABLED(CONFIG_PM)) { 428 /* 429 * In cases runtime PM is disabled by the RPM core and we get 430 * an -EINVAL return value we are not supposed to call this 431 * function, since the power state is undefined. This applies 432 * atm to the late/early system suspend/resume handlers. 433 */ 434 if (pm_runtime_get_if_active(rpm->kdev, ignore_usecount) <= 0) 435 return 0; 436 } 437 438 intel_runtime_pm_acquire(rpm, true); 439 440 return track_intel_runtime_pm_wakeref(rpm); 441 } 442 443 intel_wakeref_t intel_runtime_pm_get_if_in_use(struct intel_runtime_pm *rpm) 444 { 445 return __intel_runtime_pm_get_if_active(rpm, false); 446 } 447 448 intel_wakeref_t intel_runtime_pm_get_if_active(struct intel_runtime_pm *rpm) 449 { 450 return __intel_runtime_pm_get_if_active(rpm, true); 451 } 452 453 /** 454 * intel_runtime_pm_get_noresume - grab a runtime pm reference 455 * @rpm: the intel_runtime_pm structure 456 * 457 * This function grabs a device-level runtime pm reference (mostly used for GEM 458 * code to ensure the GTT or GT is on). 459 * 460 * It will _not_ power up the device but instead only check that it's powered 461 * on. Therefore it is only valid to call this functions from contexts where 462 * the device is known to be powered up and where trying to power it up would 463 * result in hilarity and deadlocks. That pretty much means only the system 464 * suspend/resume code where this is used to grab runtime pm references for 465 * delayed setup down in work items. 466 * 467 * Any runtime pm reference obtained by this function must have a symmetric 468 * call to intel_runtime_pm_put() to release the reference again. 469 * 470 * Returns: the wakeref cookie to pass to intel_runtime_pm_put() 471 */ 472 intel_wakeref_t intel_runtime_pm_get_noresume(struct intel_runtime_pm *rpm) 473 { 474 assert_rpm_wakelock_held(rpm); 475 pm_runtime_get_noresume(rpm->kdev); 476 477 intel_runtime_pm_acquire(rpm, true); 478 479 return track_intel_runtime_pm_wakeref(rpm); 480 } 481 482 static void __intel_runtime_pm_put(struct intel_runtime_pm *rpm, 483 intel_wakeref_t wref, 484 bool wakelock) 485 { 486 struct device *kdev = rpm->kdev; 487 488 untrack_intel_runtime_pm_wakeref(rpm, wref); 489 490 intel_runtime_pm_release(rpm, wakelock); 491 492 pm_runtime_mark_last_busy(kdev); 493 pm_runtime_put_autosuspend(kdev); 494 } 495 496 /** 497 * intel_runtime_pm_put_raw - release a raw runtime pm reference 498 * @rpm: the intel_runtime_pm structure 499 * @wref: wakeref acquired for the reference that is being released 500 * 501 * This function drops the device-level runtime pm reference obtained by 502 * intel_runtime_pm_get_raw() and might power down the corresponding 503 * hardware block right away if this is the last reference. 504 */ 505 void 506 intel_runtime_pm_put_raw(struct intel_runtime_pm *rpm, intel_wakeref_t wref) 507 { 508 __intel_runtime_pm_put(rpm, wref, false); 509 } 510 511 /** 512 * intel_runtime_pm_put_unchecked - release an unchecked runtime pm reference 513 * @rpm: the intel_runtime_pm structure 514 * 515 * This function drops the device-level runtime pm reference obtained by 516 * intel_runtime_pm_get() and might power down the corresponding 517 * hardware block right away if this is the last reference. 518 * 519 * This function exists only for historical reasons and should be avoided in 520 * new code, as the correctness of its use cannot be checked. Always use 521 * intel_runtime_pm_put() instead. 522 */ 523 void intel_runtime_pm_put_unchecked(struct intel_runtime_pm *rpm) 524 { 525 __intel_runtime_pm_put(rpm, -1, true); 526 } 527 528 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_RUNTIME_PM) 529 /** 530 * intel_runtime_pm_put - release a runtime pm reference 531 * @rpm: the intel_runtime_pm structure 532 * @wref: wakeref acquired for the reference that is being released 533 * 534 * This function drops the device-level runtime pm reference obtained by 535 * intel_runtime_pm_get() and might power down the corresponding 536 * hardware block right away if this is the last reference. 537 */ 538 void intel_runtime_pm_put(struct intel_runtime_pm *rpm, intel_wakeref_t wref) 539 { 540 __intel_runtime_pm_put(rpm, wref, true); 541 } 542 #endif 543 544 /** 545 * intel_runtime_pm_enable - enable runtime pm 546 * @rpm: the intel_runtime_pm structure 547 * 548 * This function enables runtime pm at the end of the driver load sequence. 549 * 550 * Note that this function does currently not enable runtime pm for the 551 * subordinate display power domains. That is done by 552 * intel_power_domains_enable(). 553 */ 554 void intel_runtime_pm_enable(struct intel_runtime_pm *rpm) 555 { 556 struct drm_i915_private *i915 = container_of(rpm, 557 struct drm_i915_private, 558 runtime_pm); 559 struct device *kdev = rpm->kdev; 560 561 /* 562 * Disable the system suspend direct complete optimization, which can 563 * leave the device suspended skipping the driver's suspend handlers 564 * if the device was already runtime suspended. This is needed due to 565 * the difference in our runtime and system suspend sequence and 566 * becaue the HDA driver may require us to enable the audio power 567 * domain during system suspend. 568 */ 569 dev_pm_set_driver_flags(kdev, DPM_FLAG_NO_DIRECT_COMPLETE); 570 571 pm_runtime_set_autosuspend_delay(kdev, 10000); /* 10s */ 572 pm_runtime_mark_last_busy(kdev); 573 574 /* 575 * Take a permanent reference to disable the RPM functionality and drop 576 * it only when unloading the driver. Use the low level get/put helpers, 577 * so the driver's own RPM reference tracking asserts also work on 578 * platforms without RPM support. 579 */ 580 if (!rpm->available) { 581 int ret; 582 583 pm_runtime_dont_use_autosuspend(kdev); 584 ret = pm_runtime_get_sync(kdev); 585 drm_WARN(&i915->drm, ret < 0, 586 "pm_runtime_get_sync() failed: %d\n", ret); 587 } else { 588 pm_runtime_use_autosuspend(kdev); 589 } 590 591 /* Enable by default */ 592 pm_runtime_allow(kdev); 593 594 /* 595 * The core calls the driver load handler with an RPM reference held. 596 * We drop that here and will reacquire it during unloading in 597 * intel_power_domains_fini(). 598 */ 599 pm_runtime_put_autosuspend(kdev); 600 } 601 602 void intel_runtime_pm_disable(struct intel_runtime_pm *rpm) 603 { 604 struct drm_i915_private *i915 = container_of(rpm, 605 struct drm_i915_private, 606 runtime_pm); 607 struct device *kdev = rpm->kdev; 608 609 /* Transfer rpm ownership back to core */ 610 drm_WARN(&i915->drm, pm_runtime_get_sync(kdev) < 0, 611 "Failed to pass rpm ownership back to core\n"); 612 613 pm_runtime_dont_use_autosuspend(kdev); 614 615 if (!rpm->available) 616 pm_runtime_put(kdev); 617 } 618 619 void intel_runtime_pm_driver_release(struct intel_runtime_pm *rpm) 620 { 621 struct drm_i915_private *i915 = container_of(rpm, 622 struct drm_i915_private, 623 runtime_pm); 624 int count = atomic_read(&rpm->wakeref_count); 625 626 drm_WARN(&i915->drm, count, 627 "i915 raw-wakerefs=%d wakelocks=%d on cleanup\n", 628 intel_rpm_raw_wakeref_count(count), 629 intel_rpm_wakelock_count(count)); 630 631 untrack_all_intel_runtime_pm_wakerefs(rpm); 632 } 633 634 void intel_runtime_pm_init_early(struct intel_runtime_pm *rpm) 635 { 636 struct drm_i915_private *i915 = 637 container_of(rpm, struct drm_i915_private, runtime_pm); 638 struct pci_dev *pdev = to_pci_dev(i915->drm.dev); 639 struct device *kdev = &pdev->dev; 640 641 rpm->kdev = kdev; 642 rpm->available = HAS_RUNTIME_PM(i915); 643 644 init_intel_runtime_pm_wakeref(rpm); 645 } 646