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 72 if (rpm->available) 73 stack_depot_init(); 74 } 75 76 static noinline depot_stack_handle_t 77 track_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm) 78 { 79 depot_stack_handle_t stack, *stacks; 80 unsigned long flags; 81 82 if (!rpm->available) 83 return -1; 84 85 stack = __save_depot_stack(); 86 if (!stack) 87 return -1; 88 89 spin_lock_irqsave(&rpm->debug.lock, flags); 90 91 if (!rpm->debug.count) 92 rpm->debug.last_acquire = stack; 93 94 stacks = krealloc(rpm->debug.owners, 95 (rpm->debug.count + 1) * sizeof(*stacks), 96 GFP_NOWAIT | __GFP_NOWARN); 97 if (stacks) { 98 stacks[rpm->debug.count++] = stack; 99 rpm->debug.owners = stacks; 100 } else { 101 stack = -1; 102 } 103 104 spin_unlock_irqrestore(&rpm->debug.lock, flags); 105 106 return stack; 107 } 108 109 static void untrack_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm, 110 depot_stack_handle_t stack) 111 { 112 struct drm_i915_private *i915 = container_of(rpm, 113 struct drm_i915_private, 114 runtime_pm); 115 unsigned long flags, n; 116 bool found = false; 117 118 if (unlikely(stack == -1)) 119 return; 120 121 spin_lock_irqsave(&rpm->debug.lock, flags); 122 for (n = rpm->debug.count; n--; ) { 123 if (rpm->debug.owners[n] == stack) { 124 memmove(rpm->debug.owners + n, 125 rpm->debug.owners + n + 1, 126 (--rpm->debug.count - n) * sizeof(stack)); 127 found = true; 128 break; 129 } 130 } 131 spin_unlock_irqrestore(&rpm->debug.lock, flags); 132 133 if (drm_WARN(&i915->drm, !found, 134 "Unmatched wakeref (tracking %lu), count %u\n", 135 rpm->debug.count, atomic_read(&rpm->wakeref_count))) { 136 char *buf; 137 138 buf = kmalloc(PAGE_SIZE, GFP_NOWAIT | __GFP_NOWARN); 139 if (!buf) 140 return; 141 142 stack_depot_snprint(stack, buf, PAGE_SIZE, 2); 143 DRM_DEBUG_DRIVER("wakeref %x from\n%s", stack, buf); 144 145 stack = READ_ONCE(rpm->debug.last_release); 146 if (stack) { 147 stack_depot_snprint(stack, buf, PAGE_SIZE, 2); 148 DRM_DEBUG_DRIVER("wakeref last released at\n%s", buf); 149 } 150 151 kfree(buf); 152 } 153 } 154 155 static int cmphandle(const void *_a, const void *_b) 156 { 157 const depot_stack_handle_t * const a = _a, * const b = _b; 158 159 if (*a < *b) 160 return -1; 161 else if (*a > *b) 162 return 1; 163 else 164 return 0; 165 } 166 167 static void 168 __print_intel_runtime_pm_wakeref(struct drm_printer *p, 169 const struct intel_runtime_pm_debug *dbg) 170 { 171 unsigned long i; 172 char *buf; 173 174 buf = kmalloc(PAGE_SIZE, GFP_NOWAIT | __GFP_NOWARN); 175 if (!buf) 176 return; 177 178 if (dbg->last_acquire) { 179 stack_depot_snprint(dbg->last_acquire, buf, PAGE_SIZE, 2); 180 drm_printf(p, "Wakeref last acquired:\n%s", buf); 181 } 182 183 if (dbg->last_release) { 184 stack_depot_snprint(dbg->last_release, buf, PAGE_SIZE, 2); 185 drm_printf(p, "Wakeref last released:\n%s", buf); 186 } 187 188 drm_printf(p, "Wakeref count: %lu\n", dbg->count); 189 190 sort(dbg->owners, dbg->count, sizeof(*dbg->owners), cmphandle, NULL); 191 192 for (i = 0; i < dbg->count; i++) { 193 depot_stack_handle_t stack = dbg->owners[i]; 194 unsigned long rep; 195 196 rep = 1; 197 while (i + 1 < dbg->count && dbg->owners[i + 1] == stack) 198 rep++, i++; 199 stack_depot_snprint(stack, buf, PAGE_SIZE, 2); 200 drm_printf(p, "Wakeref x%lu taken at:\n%s", rep, buf); 201 } 202 203 kfree(buf); 204 } 205 206 static noinline void 207 __untrack_all_wakerefs(struct intel_runtime_pm_debug *debug, 208 struct intel_runtime_pm_debug *saved) 209 { 210 *saved = *debug; 211 212 debug->owners = NULL; 213 debug->count = 0; 214 debug->last_release = __save_depot_stack(); 215 } 216 217 static void 218 dump_and_free_wakeref_tracking(struct intel_runtime_pm_debug *debug) 219 { 220 if (debug->count) { 221 struct drm_printer p = drm_debug_printer("i915"); 222 223 __print_intel_runtime_pm_wakeref(&p, debug); 224 } 225 226 kfree(debug->owners); 227 } 228 229 static noinline void 230 __intel_wakeref_dec_and_check_tracking(struct intel_runtime_pm *rpm) 231 { 232 struct intel_runtime_pm_debug dbg = {}; 233 unsigned long flags; 234 235 if (!atomic_dec_and_lock_irqsave(&rpm->wakeref_count, 236 &rpm->debug.lock, 237 flags)) 238 return; 239 240 __untrack_all_wakerefs(&rpm->debug, &dbg); 241 spin_unlock_irqrestore(&rpm->debug.lock, flags); 242 243 dump_and_free_wakeref_tracking(&dbg); 244 } 245 246 static noinline void 247 untrack_all_intel_runtime_pm_wakerefs(struct intel_runtime_pm *rpm) 248 { 249 struct intel_runtime_pm_debug dbg = {}; 250 unsigned long flags; 251 252 spin_lock_irqsave(&rpm->debug.lock, flags); 253 __untrack_all_wakerefs(&rpm->debug, &dbg); 254 spin_unlock_irqrestore(&rpm->debug.lock, flags); 255 256 dump_and_free_wakeref_tracking(&dbg); 257 } 258 259 void print_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm, 260 struct drm_printer *p) 261 { 262 struct intel_runtime_pm_debug dbg = {}; 263 264 do { 265 unsigned long alloc = dbg.count; 266 depot_stack_handle_t *s; 267 268 spin_lock_irq(&rpm->debug.lock); 269 dbg.count = rpm->debug.count; 270 if (dbg.count <= alloc) { 271 memcpy(dbg.owners, 272 rpm->debug.owners, 273 dbg.count * sizeof(*s)); 274 } 275 dbg.last_acquire = rpm->debug.last_acquire; 276 dbg.last_release = rpm->debug.last_release; 277 spin_unlock_irq(&rpm->debug.lock); 278 if (dbg.count <= alloc) 279 break; 280 281 s = krealloc(dbg.owners, 282 dbg.count * sizeof(*s), 283 GFP_NOWAIT | __GFP_NOWARN); 284 if (!s) 285 goto out; 286 287 dbg.owners = s; 288 } while (1); 289 290 __print_intel_runtime_pm_wakeref(p, &dbg); 291 292 out: 293 kfree(dbg.owners); 294 } 295 296 #else 297 298 static void init_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm) 299 { 300 } 301 302 static depot_stack_handle_t 303 track_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm) 304 { 305 return -1; 306 } 307 308 static void untrack_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm, 309 intel_wakeref_t wref) 310 { 311 } 312 313 static void 314 __intel_wakeref_dec_and_check_tracking(struct intel_runtime_pm *rpm) 315 { 316 atomic_dec(&rpm->wakeref_count); 317 } 318 319 static void 320 untrack_all_intel_runtime_pm_wakerefs(struct intel_runtime_pm *rpm) 321 { 322 } 323 324 #endif 325 326 static void 327 intel_runtime_pm_acquire(struct intel_runtime_pm *rpm, bool wakelock) 328 { 329 if (wakelock) { 330 atomic_add(1 + INTEL_RPM_WAKELOCK_BIAS, &rpm->wakeref_count); 331 assert_rpm_wakelock_held(rpm); 332 } else { 333 atomic_inc(&rpm->wakeref_count); 334 assert_rpm_raw_wakeref_held(rpm); 335 } 336 } 337 338 static void 339 intel_runtime_pm_release(struct intel_runtime_pm *rpm, int wakelock) 340 { 341 if (wakelock) { 342 assert_rpm_wakelock_held(rpm); 343 atomic_sub(INTEL_RPM_WAKELOCK_BIAS, &rpm->wakeref_count); 344 } else { 345 assert_rpm_raw_wakeref_held(rpm); 346 } 347 348 __intel_wakeref_dec_and_check_tracking(rpm); 349 } 350 351 static intel_wakeref_t __intel_runtime_pm_get(struct intel_runtime_pm *rpm, 352 bool wakelock) 353 { 354 struct drm_i915_private *i915 = container_of(rpm, 355 struct drm_i915_private, 356 runtime_pm); 357 int ret; 358 359 ret = pm_runtime_get_sync(rpm->kdev); 360 drm_WARN_ONCE(&i915->drm, ret < 0, 361 "pm_runtime_get_sync() failed: %d\n", ret); 362 363 intel_runtime_pm_acquire(rpm, wakelock); 364 365 return track_intel_runtime_pm_wakeref(rpm); 366 } 367 368 /** 369 * intel_runtime_pm_get_raw - grab a raw runtime pm reference 370 * @rpm: the intel_runtime_pm structure 371 * 372 * This is the unlocked version of intel_display_power_is_enabled() and should 373 * only be used from error capture and recovery code where deadlocks are 374 * possible. 375 * This function grabs a device-level runtime pm reference (mostly used for 376 * asynchronous PM management from display code) and ensures that it is powered 377 * up. Raw references are not considered during wakelock assert checks. 378 * 379 * Any runtime pm reference obtained by this function must have a symmetric 380 * call to intel_runtime_pm_put_raw() to release the reference again. 381 * 382 * Returns: the wakeref cookie to pass to intel_runtime_pm_put_raw(), evaluates 383 * as True if the wakeref was acquired, or False otherwise. 384 */ 385 intel_wakeref_t intel_runtime_pm_get_raw(struct intel_runtime_pm *rpm) 386 { 387 return __intel_runtime_pm_get(rpm, false); 388 } 389 390 /** 391 * intel_runtime_pm_get - grab a runtime pm reference 392 * @rpm: the intel_runtime_pm structure 393 * 394 * This function grabs a device-level runtime pm reference (mostly used for GEM 395 * code to ensure the GTT or GT is on) and ensures that it is powered up. 396 * 397 * Any runtime pm reference obtained by this function must have a symmetric 398 * call to intel_runtime_pm_put() to release the reference again. 399 * 400 * Returns: the wakeref cookie to pass to intel_runtime_pm_put() 401 */ 402 intel_wakeref_t intel_runtime_pm_get(struct intel_runtime_pm *rpm) 403 { 404 return __intel_runtime_pm_get(rpm, true); 405 } 406 407 /** 408 * __intel_runtime_pm_get_if_active - grab a runtime pm reference if device is active 409 * @rpm: the intel_runtime_pm structure 410 * @ignore_usecount: get a ref even if dev->power.usage_count is 0 411 * 412 * This function grabs a device-level runtime pm reference if the device is 413 * already active and ensures that it is powered up. It is illegal to try 414 * and access the HW should intel_runtime_pm_get_if_active() report failure. 415 * 416 * If @ignore_usecount is true, a reference will be acquired even if there is no 417 * user requiring the device to be powered up (dev->power.usage_count == 0). 418 * If the function returns false in this case then it's guaranteed that the 419 * device's runtime suspend hook has been called already or that it will be 420 * called (and hence it's also guaranteed that the device's runtime resume 421 * hook will be called eventually). 422 * 423 * Any runtime pm reference obtained by this function must have a symmetric 424 * call to intel_runtime_pm_put() to release the reference again. 425 * 426 * Returns: the wakeref cookie to pass to intel_runtime_pm_put(), evaluates 427 * as True if the wakeref was acquired, or False otherwise. 428 */ 429 static intel_wakeref_t __intel_runtime_pm_get_if_active(struct intel_runtime_pm *rpm, 430 bool ignore_usecount) 431 { 432 if (IS_ENABLED(CONFIG_PM)) { 433 /* 434 * In cases runtime PM is disabled by the RPM core and we get 435 * an -EINVAL return value we are not supposed to call this 436 * function, since the power state is undefined. This applies 437 * atm to the late/early system suspend/resume handlers. 438 */ 439 if (pm_runtime_get_if_active(rpm->kdev, ignore_usecount) <= 0) 440 return 0; 441 } 442 443 intel_runtime_pm_acquire(rpm, true); 444 445 return track_intel_runtime_pm_wakeref(rpm); 446 } 447 448 intel_wakeref_t intel_runtime_pm_get_if_in_use(struct intel_runtime_pm *rpm) 449 { 450 return __intel_runtime_pm_get_if_active(rpm, false); 451 } 452 453 intel_wakeref_t intel_runtime_pm_get_if_active(struct intel_runtime_pm *rpm) 454 { 455 return __intel_runtime_pm_get_if_active(rpm, true); 456 } 457 458 /** 459 * intel_runtime_pm_get_noresume - grab a runtime pm reference 460 * @rpm: the intel_runtime_pm structure 461 * 462 * This function grabs a device-level runtime pm reference (mostly used for GEM 463 * code to ensure the GTT or GT is on). 464 * 465 * It will _not_ power up the device but instead only check that it's powered 466 * on. Therefore it is only valid to call this functions from contexts where 467 * the device is known to be powered up and where trying to power it up would 468 * result in hilarity and deadlocks. That pretty much means only the system 469 * suspend/resume code where this is used to grab runtime pm references for 470 * delayed setup down in work items. 471 * 472 * Any runtime pm reference obtained by this function must have a symmetric 473 * call to intel_runtime_pm_put() to release the reference again. 474 * 475 * Returns: the wakeref cookie to pass to intel_runtime_pm_put() 476 */ 477 intel_wakeref_t intel_runtime_pm_get_noresume(struct intel_runtime_pm *rpm) 478 { 479 assert_rpm_wakelock_held(rpm); 480 pm_runtime_get_noresume(rpm->kdev); 481 482 intel_runtime_pm_acquire(rpm, true); 483 484 return track_intel_runtime_pm_wakeref(rpm); 485 } 486 487 static void __intel_runtime_pm_put(struct intel_runtime_pm *rpm, 488 intel_wakeref_t wref, 489 bool wakelock) 490 { 491 struct device *kdev = rpm->kdev; 492 493 untrack_intel_runtime_pm_wakeref(rpm, wref); 494 495 intel_runtime_pm_release(rpm, wakelock); 496 497 pm_runtime_mark_last_busy(kdev); 498 pm_runtime_put_autosuspend(kdev); 499 } 500 501 /** 502 * intel_runtime_pm_put_raw - release a raw runtime pm reference 503 * @rpm: the intel_runtime_pm structure 504 * @wref: wakeref acquired for the reference that is being released 505 * 506 * This function drops the device-level runtime pm reference obtained by 507 * intel_runtime_pm_get_raw() and might power down the corresponding 508 * hardware block right away if this is the last reference. 509 */ 510 void 511 intel_runtime_pm_put_raw(struct intel_runtime_pm *rpm, intel_wakeref_t wref) 512 { 513 __intel_runtime_pm_put(rpm, wref, false); 514 } 515 516 /** 517 * intel_runtime_pm_put_unchecked - release an unchecked runtime pm reference 518 * @rpm: the intel_runtime_pm structure 519 * 520 * This function drops the device-level runtime pm reference obtained by 521 * intel_runtime_pm_get() and might power down the corresponding 522 * hardware block right away if this is the last reference. 523 * 524 * This function exists only for historical reasons and should be avoided in 525 * new code, as the correctness of its use cannot be checked. Always use 526 * intel_runtime_pm_put() instead. 527 */ 528 void intel_runtime_pm_put_unchecked(struct intel_runtime_pm *rpm) 529 { 530 __intel_runtime_pm_put(rpm, -1, true); 531 } 532 533 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_RUNTIME_PM) 534 /** 535 * intel_runtime_pm_put - release a runtime pm reference 536 * @rpm: the intel_runtime_pm structure 537 * @wref: wakeref acquired for the reference that is being released 538 * 539 * This function drops the device-level runtime pm reference obtained by 540 * intel_runtime_pm_get() and might power down the corresponding 541 * hardware block right away if this is the last reference. 542 */ 543 void intel_runtime_pm_put(struct intel_runtime_pm *rpm, intel_wakeref_t wref) 544 { 545 __intel_runtime_pm_put(rpm, wref, true); 546 } 547 #endif 548 549 /** 550 * intel_runtime_pm_enable - enable runtime pm 551 * @rpm: the intel_runtime_pm structure 552 * 553 * This function enables runtime pm at the end of the driver load sequence. 554 * 555 * Note that this function does currently not enable runtime pm for the 556 * subordinate display power domains. That is done by 557 * intel_power_domains_enable(). 558 */ 559 void intel_runtime_pm_enable(struct intel_runtime_pm *rpm) 560 { 561 struct drm_i915_private *i915 = container_of(rpm, 562 struct drm_i915_private, 563 runtime_pm); 564 struct device *kdev = rpm->kdev; 565 566 /* 567 * Disable the system suspend direct complete optimization, which can 568 * leave the device suspended skipping the driver's suspend handlers 569 * if the device was already runtime suspended. This is needed due to 570 * the difference in our runtime and system suspend sequence and 571 * becaue the HDA driver may require us to enable the audio power 572 * domain during system suspend. 573 */ 574 dev_pm_set_driver_flags(kdev, DPM_FLAG_NO_DIRECT_COMPLETE); 575 576 pm_runtime_set_autosuspend_delay(kdev, 10000); /* 10s */ 577 pm_runtime_mark_last_busy(kdev); 578 579 /* 580 * Take a permanent reference to disable the RPM functionality and drop 581 * it only when unloading the driver. Use the low level get/put helpers, 582 * so the driver's own RPM reference tracking asserts also work on 583 * platforms without RPM support. 584 */ 585 if (!rpm->available) { 586 int ret; 587 588 pm_runtime_dont_use_autosuspend(kdev); 589 ret = pm_runtime_get_sync(kdev); 590 drm_WARN(&i915->drm, ret < 0, 591 "pm_runtime_get_sync() failed: %d\n", ret); 592 } else { 593 pm_runtime_use_autosuspend(kdev); 594 } 595 596 /* Enable by default */ 597 pm_runtime_allow(kdev); 598 599 /* 600 * The core calls the driver load handler with an RPM reference held. 601 * We drop that here and will reacquire it during unloading in 602 * intel_power_domains_fini(). 603 */ 604 pm_runtime_put_autosuspend(kdev); 605 } 606 607 void intel_runtime_pm_disable(struct intel_runtime_pm *rpm) 608 { 609 struct drm_i915_private *i915 = container_of(rpm, 610 struct drm_i915_private, 611 runtime_pm); 612 struct device *kdev = rpm->kdev; 613 614 /* Transfer rpm ownership back to core */ 615 drm_WARN(&i915->drm, pm_runtime_get_sync(kdev) < 0, 616 "Failed to pass rpm ownership back to core\n"); 617 618 pm_runtime_dont_use_autosuspend(kdev); 619 620 if (!rpm->available) 621 pm_runtime_put(kdev); 622 } 623 624 void intel_runtime_pm_driver_release(struct intel_runtime_pm *rpm) 625 { 626 struct drm_i915_private *i915 = container_of(rpm, 627 struct drm_i915_private, 628 runtime_pm); 629 int count = atomic_read(&rpm->wakeref_count); 630 631 drm_WARN(&i915->drm, count, 632 "i915 raw-wakerefs=%d wakelocks=%d on cleanup\n", 633 intel_rpm_raw_wakeref_count(count), 634 intel_rpm_wakelock_count(count)); 635 636 untrack_all_intel_runtime_pm_wakerefs(rpm); 637 } 638 639 void intel_runtime_pm_init_early(struct intel_runtime_pm *rpm) 640 { 641 struct drm_i915_private *i915 = 642 container_of(rpm, struct drm_i915_private, runtime_pm); 643 struct pci_dev *pdev = to_pci_dev(i915->drm.dev); 644 struct device *kdev = &pdev->dev; 645 646 rpm->kdev = kdev; 647 rpm->available = HAS_RUNTIME_PM(i915); 648 649 init_intel_runtime_pm_wakeref(rpm); 650 } 651