2 * Copyright © 2012-2014 Intel Corporation
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:
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
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
24 * Eugeni Dodonov <eugeni.dodonov@intel.com>
25 * Daniel Vetter <daniel.vetter@ffwll.ch>
29 #include <linux/pm_runtime.h>
31 #include <drm/drm_print.h>
34 #include "i915_trace.h"
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.
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.
53 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_RUNTIME_PM)
55 #include <linux/sort.h>
59 static noinline depot_stack_handle_t __save_depot_stack(void)
61 unsigned long entries[STACKDEPTH];
64 n = stack_trace_save(entries, ARRAY_SIZE(entries), 1);
65 return stack_depot_save(entries, n, GFP_NOWAIT | __GFP_NOWARN);
68 static void __print_depot_stack(depot_stack_handle_t stack,
69 char *buf, int sz, int indent)
71 unsigned long *entries;
72 unsigned int nr_entries;
74 nr_entries = stack_depot_fetch(stack, &entries);
75 stack_trace_snprint(buf, sz, entries, nr_entries, indent);
78 static void init_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm)
80 spin_lock_init(&rpm->debug.lock);
83 static noinline depot_stack_handle_t
84 track_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm)
86 depot_stack_handle_t stack, *stacks;
92 stack = __save_depot_stack();
96 spin_lock_irqsave(&rpm->debug.lock, flags);
98 if (!rpm->debug.count)
99 rpm->debug.last_acquire = stack;
101 stacks = krealloc(rpm->debug.owners,
102 (rpm->debug.count + 1) * sizeof(*stacks),
103 GFP_NOWAIT | __GFP_NOWARN);
105 stacks[rpm->debug.count++] = stack;
106 rpm->debug.owners = stacks;
111 spin_unlock_irqrestore(&rpm->debug.lock, flags);
116 static void untrack_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm,
117 depot_stack_handle_t stack)
119 struct drm_i915_private *i915 = container_of(rpm,
120 struct drm_i915_private,
122 unsigned long flags, n;
125 if (unlikely(stack == -1))
128 spin_lock_irqsave(&rpm->debug.lock, flags);
129 for (n = rpm->debug.count; n--; ) {
130 if (rpm->debug.owners[n] == stack) {
131 memmove(rpm->debug.owners + n,
132 rpm->debug.owners + n + 1,
133 (--rpm->debug.count - n) * sizeof(stack));
138 spin_unlock_irqrestore(&rpm->debug.lock, flags);
140 if (drm_WARN(&i915->drm, !found,
141 "Unmatched wakeref (tracking %lu), count %u\n",
142 rpm->debug.count, atomic_read(&rpm->wakeref_count))) {
145 buf = kmalloc(PAGE_SIZE, GFP_NOWAIT | __GFP_NOWARN);
149 __print_depot_stack(stack, buf, PAGE_SIZE, 2);
150 DRM_DEBUG_DRIVER("wakeref %x from\n%s", stack, buf);
152 stack = READ_ONCE(rpm->debug.last_release);
154 __print_depot_stack(stack, buf, PAGE_SIZE, 2);
155 DRM_DEBUG_DRIVER("wakeref last released at\n%s", buf);
162 static int cmphandle(const void *_a, const void *_b)
164 const depot_stack_handle_t * const a = _a, * const b = _b;
175 __print_intel_runtime_pm_wakeref(struct drm_printer *p,
176 const struct intel_runtime_pm_debug *dbg)
181 buf = kmalloc(PAGE_SIZE, GFP_NOWAIT | __GFP_NOWARN);
185 if (dbg->last_acquire) {
186 __print_depot_stack(dbg->last_acquire, buf, PAGE_SIZE, 2);
187 drm_printf(p, "Wakeref last acquired:\n%s", buf);
190 if (dbg->last_release) {
191 __print_depot_stack(dbg->last_release, buf, PAGE_SIZE, 2);
192 drm_printf(p, "Wakeref last released:\n%s", buf);
195 drm_printf(p, "Wakeref count: %lu\n", dbg->count);
197 sort(dbg->owners, dbg->count, sizeof(*dbg->owners), cmphandle, NULL);
199 for (i = 0; i < dbg->count; i++) {
200 depot_stack_handle_t stack = dbg->owners[i];
204 while (i + 1 < dbg->count && dbg->owners[i + 1] == stack)
206 __print_depot_stack(stack, buf, PAGE_SIZE, 2);
207 drm_printf(p, "Wakeref x%lu taken at:\n%s", rep, buf);
214 __untrack_all_wakerefs(struct intel_runtime_pm_debug *debug,
215 struct intel_runtime_pm_debug *saved)
219 debug->owners = NULL;
221 debug->last_release = __save_depot_stack();
225 dump_and_free_wakeref_tracking(struct intel_runtime_pm_debug *debug)
228 struct drm_printer p = drm_debug_printer("i915");
230 __print_intel_runtime_pm_wakeref(&p, debug);
233 kfree(debug->owners);
237 __intel_wakeref_dec_and_check_tracking(struct intel_runtime_pm *rpm)
239 struct intel_runtime_pm_debug dbg = {};
242 if (!atomic_dec_and_lock_irqsave(&rpm->wakeref_count,
247 __untrack_all_wakerefs(&rpm->debug, &dbg);
248 spin_unlock_irqrestore(&rpm->debug.lock, flags);
250 dump_and_free_wakeref_tracking(&dbg);
254 untrack_all_intel_runtime_pm_wakerefs(struct intel_runtime_pm *rpm)
256 struct intel_runtime_pm_debug dbg = {};
259 spin_lock_irqsave(&rpm->debug.lock, flags);
260 __untrack_all_wakerefs(&rpm->debug, &dbg);
261 spin_unlock_irqrestore(&rpm->debug.lock, flags);
263 dump_and_free_wakeref_tracking(&dbg);
266 void print_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm,
267 struct drm_printer *p)
269 struct intel_runtime_pm_debug dbg = {};
272 unsigned long alloc = dbg.count;
273 depot_stack_handle_t *s;
275 spin_lock_irq(&rpm->debug.lock);
276 dbg.count = rpm->debug.count;
277 if (dbg.count <= alloc) {
280 dbg.count * sizeof(*s));
282 dbg.last_acquire = rpm->debug.last_acquire;
283 dbg.last_release = rpm->debug.last_release;
284 spin_unlock_irq(&rpm->debug.lock);
285 if (dbg.count <= alloc)
288 s = krealloc(dbg.owners,
289 dbg.count * sizeof(*s),
290 GFP_NOWAIT | __GFP_NOWARN);
297 __print_intel_runtime_pm_wakeref(p, &dbg);
305 static void init_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm)
309 static depot_stack_handle_t
310 track_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm)
315 static void untrack_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm,
316 intel_wakeref_t wref)
321 __intel_wakeref_dec_and_check_tracking(struct intel_runtime_pm *rpm)
323 atomic_dec(&rpm->wakeref_count);
327 untrack_all_intel_runtime_pm_wakerefs(struct intel_runtime_pm *rpm)
334 intel_runtime_pm_acquire(struct intel_runtime_pm *rpm, bool wakelock)
337 atomic_add(1 + INTEL_RPM_WAKELOCK_BIAS, &rpm->wakeref_count);
338 assert_rpm_wakelock_held(rpm);
340 atomic_inc(&rpm->wakeref_count);
341 assert_rpm_raw_wakeref_held(rpm);
346 intel_runtime_pm_release(struct intel_runtime_pm *rpm, int wakelock)
349 assert_rpm_wakelock_held(rpm);
350 atomic_sub(INTEL_RPM_WAKELOCK_BIAS, &rpm->wakeref_count);
352 assert_rpm_raw_wakeref_held(rpm);
355 __intel_wakeref_dec_and_check_tracking(rpm);
358 static intel_wakeref_t __intel_runtime_pm_get(struct intel_runtime_pm *rpm,
361 struct drm_i915_private *i915 = container_of(rpm,
362 struct drm_i915_private,
366 ret = pm_runtime_get_sync(rpm->kdev);
367 drm_WARN_ONCE(&i915->drm, ret < 0,
368 "pm_runtime_get_sync() failed: %d\n", ret);
370 intel_runtime_pm_acquire(rpm, wakelock);
372 return track_intel_runtime_pm_wakeref(rpm);
376 * intel_runtime_pm_get_raw - grab a raw runtime pm reference
377 * @rpm: the intel_runtime_pm structure
379 * This is the unlocked version of intel_display_power_is_enabled() and should
380 * only be used from error capture and recovery code where deadlocks are
382 * This function grabs a device-level runtime pm reference (mostly used for
383 * asynchronous PM management from display code) and ensures that it is powered
384 * up. Raw references are not considered during wakelock assert checks.
386 * Any runtime pm reference obtained by this function must have a symmetric
387 * call to intel_runtime_pm_put_raw() to release the reference again.
389 * Returns: the wakeref cookie to pass to intel_runtime_pm_put_raw(), evaluates
390 * as True if the wakeref was acquired, or False otherwise.
392 intel_wakeref_t intel_runtime_pm_get_raw(struct intel_runtime_pm *rpm)
394 return __intel_runtime_pm_get(rpm, false);
398 * intel_runtime_pm_get - grab a runtime pm reference
399 * @rpm: the intel_runtime_pm structure
401 * This function grabs a device-level runtime pm reference (mostly used for GEM
402 * code to ensure the GTT or GT is on) and ensures that it is powered up.
404 * Any runtime pm reference obtained by this function must have a symmetric
405 * call to intel_runtime_pm_put() to release the reference again.
407 * Returns: the wakeref cookie to pass to intel_runtime_pm_put()
409 intel_wakeref_t intel_runtime_pm_get(struct intel_runtime_pm *rpm)
411 return __intel_runtime_pm_get(rpm, true);
415 * __intel_runtime_pm_get_if_active - grab a runtime pm reference if device is active
416 * @rpm: the intel_runtime_pm structure
417 * @ignore_usecount: get a ref even if dev->power.usage_count is 0
419 * This function grabs a device-level runtime pm reference if the device is
420 * already active and ensures that it is powered up. It is illegal to try
421 * and access the HW should intel_runtime_pm_get_if_active() report failure.
423 * If @ignore_usecount is true, a reference will be acquired even if there is no
424 * user requiring the device to be powered up (dev->power.usage_count == 0).
425 * If the function returns false in this case then it's guaranteed that the
426 * device's runtime suspend hook has been called already or that it will be
427 * called (and hence it's also guaranteed that the device's runtime resume
428 * hook will be called eventually).
430 * Any runtime pm reference obtained by this function must have a symmetric
431 * call to intel_runtime_pm_put() to release the reference again.
433 * Returns: the wakeref cookie to pass to intel_runtime_pm_put(), evaluates
434 * as True if the wakeref was acquired, or False otherwise.
436 static intel_wakeref_t __intel_runtime_pm_get_if_active(struct intel_runtime_pm *rpm,
437 bool ignore_usecount)
439 if (IS_ENABLED(CONFIG_PM)) {
441 * In cases runtime PM is disabled by the RPM core and we get
442 * an -EINVAL return value we are not supposed to call this
443 * function, since the power state is undefined. This applies
444 * atm to the late/early system suspend/resume handlers.
446 if (pm_runtime_get_if_active(rpm->kdev, ignore_usecount) <= 0)
450 intel_runtime_pm_acquire(rpm, true);
452 return track_intel_runtime_pm_wakeref(rpm);
455 intel_wakeref_t intel_runtime_pm_get_if_in_use(struct intel_runtime_pm *rpm)
457 return __intel_runtime_pm_get_if_active(rpm, false);
460 intel_wakeref_t intel_runtime_pm_get_if_active(struct intel_runtime_pm *rpm)
462 return __intel_runtime_pm_get_if_active(rpm, true);
466 * intel_runtime_pm_get_noresume - grab a runtime pm reference
467 * @rpm: the intel_runtime_pm structure
469 * This function grabs a device-level runtime pm reference (mostly used for GEM
470 * code to ensure the GTT or GT is on).
472 * It will _not_ power up the device but instead only check that it's powered
473 * on. Therefore it is only valid to call this functions from contexts where
474 * the device is known to be powered up and where trying to power it up would
475 * result in hilarity and deadlocks. That pretty much means only the system
476 * suspend/resume code where this is used to grab runtime pm references for
477 * delayed setup down in work items.
479 * Any runtime pm reference obtained by this function must have a symmetric
480 * call to intel_runtime_pm_put() to release the reference again.
482 * Returns: the wakeref cookie to pass to intel_runtime_pm_put()
484 intel_wakeref_t intel_runtime_pm_get_noresume(struct intel_runtime_pm *rpm)
486 assert_rpm_wakelock_held(rpm);
487 pm_runtime_get_noresume(rpm->kdev);
489 intel_runtime_pm_acquire(rpm, true);
491 return track_intel_runtime_pm_wakeref(rpm);
494 static void __intel_runtime_pm_put(struct intel_runtime_pm *rpm,
495 intel_wakeref_t wref,
498 struct device *kdev = rpm->kdev;
500 untrack_intel_runtime_pm_wakeref(rpm, wref);
502 intel_runtime_pm_release(rpm, wakelock);
504 pm_runtime_mark_last_busy(kdev);
505 pm_runtime_put_autosuspend(kdev);
509 * intel_runtime_pm_put_raw - release a raw runtime pm reference
510 * @rpm: the intel_runtime_pm structure
511 * @wref: wakeref acquired for the reference that is being released
513 * This function drops the device-level runtime pm reference obtained by
514 * intel_runtime_pm_get_raw() and might power down the corresponding
515 * hardware block right away if this is the last reference.
518 intel_runtime_pm_put_raw(struct intel_runtime_pm *rpm, intel_wakeref_t wref)
520 __intel_runtime_pm_put(rpm, wref, false);
524 * intel_runtime_pm_put_unchecked - release an unchecked runtime pm reference
525 * @rpm: the intel_runtime_pm structure
527 * This function drops the device-level runtime pm reference obtained by
528 * intel_runtime_pm_get() and might power down the corresponding
529 * hardware block right away if this is the last reference.
531 * This function exists only for historical reasons and should be avoided in
532 * new code, as the correctness of its use cannot be checked. Always use
533 * intel_runtime_pm_put() instead.
535 void intel_runtime_pm_put_unchecked(struct intel_runtime_pm *rpm)
537 __intel_runtime_pm_put(rpm, -1, true);
540 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_RUNTIME_PM)
542 * intel_runtime_pm_put - release a runtime pm reference
543 * @rpm: the intel_runtime_pm structure
544 * @wref: wakeref acquired for the reference that is being released
546 * This function drops the device-level runtime pm reference obtained by
547 * intel_runtime_pm_get() and might power down the corresponding
548 * hardware block right away if this is the last reference.
550 void intel_runtime_pm_put(struct intel_runtime_pm *rpm, intel_wakeref_t wref)
552 __intel_runtime_pm_put(rpm, wref, true);
557 * intel_runtime_pm_enable - enable runtime pm
558 * @rpm: the intel_runtime_pm structure
560 * This function enables runtime pm at the end of the driver load sequence.
562 * Note that this function does currently not enable runtime pm for the
563 * subordinate display power domains. That is done by
564 * intel_power_domains_enable().
566 void intel_runtime_pm_enable(struct intel_runtime_pm *rpm)
568 struct drm_i915_private *i915 = container_of(rpm,
569 struct drm_i915_private,
571 struct device *kdev = rpm->kdev;
574 * Disable the system suspend direct complete optimization, which can
575 * leave the device suspended skipping the driver's suspend handlers
576 * if the device was already runtime suspended. This is needed due to
577 * the difference in our runtime and system suspend sequence and
578 * becaue the HDA driver may require us to enable the audio power
579 * domain during system suspend.
581 dev_pm_set_driver_flags(kdev, DPM_FLAG_NO_DIRECT_COMPLETE);
583 pm_runtime_set_autosuspend_delay(kdev, 10000); /* 10s */
584 pm_runtime_mark_last_busy(kdev);
587 * Take a permanent reference to disable the RPM functionality and drop
588 * it only when unloading the driver. Use the low level get/put helpers,
589 * so the driver's own RPM reference tracking asserts also work on
590 * platforms without RPM support.
592 if (!rpm->available) {
595 pm_runtime_dont_use_autosuspend(kdev);
596 ret = pm_runtime_get_sync(kdev);
597 drm_WARN(&i915->drm, ret < 0,
598 "pm_runtime_get_sync() failed: %d\n", ret);
600 pm_runtime_use_autosuspend(kdev);
604 * The core calls the driver load handler with an RPM reference held.
605 * We drop that here and will reacquire it during unloading in
606 * intel_power_domains_fini().
608 pm_runtime_put_autosuspend(kdev);
611 void intel_runtime_pm_disable(struct intel_runtime_pm *rpm)
613 struct drm_i915_private *i915 = container_of(rpm,
614 struct drm_i915_private,
616 struct device *kdev = rpm->kdev;
618 /* Transfer rpm ownership back to core */
619 drm_WARN(&i915->drm, pm_runtime_get_sync(kdev) < 0,
620 "Failed to pass rpm ownership back to core\n");
622 pm_runtime_dont_use_autosuspend(kdev);
625 pm_runtime_put(kdev);
628 void intel_runtime_pm_driver_release(struct intel_runtime_pm *rpm)
630 struct drm_i915_private *i915 = container_of(rpm,
631 struct drm_i915_private,
633 int count = atomic_read(&rpm->wakeref_count);
635 drm_WARN(&i915->drm, count,
636 "i915 raw-wakerefs=%d wakelocks=%d on cleanup\n",
637 intel_rpm_raw_wakeref_count(count),
638 intel_rpm_wakelock_count(count));
640 untrack_all_intel_runtime_pm_wakerefs(rpm);
643 void intel_runtime_pm_init_early(struct intel_runtime_pm *rpm)
645 struct drm_i915_private *i915 =
646 container_of(rpm, struct drm_i915_private, runtime_pm);
647 struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
648 struct device *kdev = &pdev->dev;
651 rpm->available = HAS_RUNTIME_PM(i915);
653 init_intel_runtime_pm_wakeref(rpm);