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 init_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm)
70 spin_lock_init(&rpm->debug.lock);
73 static noinline depot_stack_handle_t
74 track_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm)
76 depot_stack_handle_t stack, *stacks;
82 stack = __save_depot_stack();
86 spin_lock_irqsave(&rpm->debug.lock, flags);
88 if (!rpm->debug.count)
89 rpm->debug.last_acquire = stack;
91 stacks = krealloc(rpm->debug.owners,
92 (rpm->debug.count + 1) * sizeof(*stacks),
93 GFP_NOWAIT | __GFP_NOWARN);
95 stacks[rpm->debug.count++] = stack;
96 rpm->debug.owners = stacks;
101 spin_unlock_irqrestore(&rpm->debug.lock, flags);
106 static void untrack_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm,
107 depot_stack_handle_t stack)
109 struct drm_i915_private *i915 = container_of(rpm,
110 struct drm_i915_private,
112 unsigned long flags, n;
115 if (unlikely(stack == -1))
118 spin_lock_irqsave(&rpm->debug.lock, flags);
119 for (n = rpm->debug.count; n--; ) {
120 if (rpm->debug.owners[n] == stack) {
121 memmove(rpm->debug.owners + n,
122 rpm->debug.owners + n + 1,
123 (--rpm->debug.count - n) * sizeof(stack));
128 spin_unlock_irqrestore(&rpm->debug.lock, flags);
130 if (drm_WARN(&i915->drm, !found,
131 "Unmatched wakeref (tracking %lu), count %u\n",
132 rpm->debug.count, atomic_read(&rpm->wakeref_count))) {
135 buf = kmalloc(PAGE_SIZE, GFP_NOWAIT | __GFP_NOWARN);
139 stack_depot_snprint(stack, buf, PAGE_SIZE, 2);
140 DRM_DEBUG_DRIVER("wakeref %x from\n%s", stack, buf);
142 stack = READ_ONCE(rpm->debug.last_release);
144 stack_depot_snprint(stack, buf, PAGE_SIZE, 2);
145 DRM_DEBUG_DRIVER("wakeref last released at\n%s", buf);
152 static int cmphandle(const void *_a, const void *_b)
154 const depot_stack_handle_t * const a = _a, * const b = _b;
165 __print_intel_runtime_pm_wakeref(struct drm_printer *p,
166 const struct intel_runtime_pm_debug *dbg)
171 buf = kmalloc(PAGE_SIZE, GFP_NOWAIT | __GFP_NOWARN);
175 if (dbg->last_acquire) {
176 stack_depot_snprint(dbg->last_acquire, buf, PAGE_SIZE, 2);
177 drm_printf(p, "Wakeref last acquired:\n%s", buf);
180 if (dbg->last_release) {
181 stack_depot_snprint(dbg->last_release, buf, PAGE_SIZE, 2);
182 drm_printf(p, "Wakeref last released:\n%s", buf);
185 drm_printf(p, "Wakeref count: %lu\n", dbg->count);
187 sort(dbg->owners, dbg->count, sizeof(*dbg->owners), cmphandle, NULL);
189 for (i = 0; i < dbg->count; i++) {
190 depot_stack_handle_t stack = dbg->owners[i];
194 while (i + 1 < dbg->count && dbg->owners[i + 1] == stack)
196 stack_depot_snprint(stack, buf, PAGE_SIZE, 2);
197 drm_printf(p, "Wakeref x%lu taken at:\n%s", rep, buf);
204 __untrack_all_wakerefs(struct intel_runtime_pm_debug *debug,
205 struct intel_runtime_pm_debug *saved)
209 debug->owners = NULL;
211 debug->last_release = __save_depot_stack();
215 dump_and_free_wakeref_tracking(struct intel_runtime_pm_debug *debug)
218 struct drm_printer p = drm_debug_printer("i915");
220 __print_intel_runtime_pm_wakeref(&p, debug);
223 kfree(debug->owners);
227 __intel_wakeref_dec_and_check_tracking(struct intel_runtime_pm *rpm)
229 struct intel_runtime_pm_debug dbg = {};
232 if (!atomic_dec_and_lock_irqsave(&rpm->wakeref_count,
237 __untrack_all_wakerefs(&rpm->debug, &dbg);
238 spin_unlock_irqrestore(&rpm->debug.lock, flags);
240 dump_and_free_wakeref_tracking(&dbg);
244 untrack_all_intel_runtime_pm_wakerefs(struct intel_runtime_pm *rpm)
246 struct intel_runtime_pm_debug dbg = {};
249 spin_lock_irqsave(&rpm->debug.lock, flags);
250 __untrack_all_wakerefs(&rpm->debug, &dbg);
251 spin_unlock_irqrestore(&rpm->debug.lock, flags);
253 dump_and_free_wakeref_tracking(&dbg);
256 void print_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm,
257 struct drm_printer *p)
259 struct intel_runtime_pm_debug dbg = {};
262 unsigned long alloc = dbg.count;
263 depot_stack_handle_t *s;
265 spin_lock_irq(&rpm->debug.lock);
266 dbg.count = rpm->debug.count;
267 if (dbg.count <= alloc) {
270 dbg.count * sizeof(*s));
272 dbg.last_acquire = rpm->debug.last_acquire;
273 dbg.last_release = rpm->debug.last_release;
274 spin_unlock_irq(&rpm->debug.lock);
275 if (dbg.count <= alloc)
278 s = krealloc(dbg.owners,
279 dbg.count * sizeof(*s),
280 GFP_NOWAIT | __GFP_NOWARN);
287 __print_intel_runtime_pm_wakeref(p, &dbg);
295 static void init_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm)
299 static depot_stack_handle_t
300 track_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm)
305 static void untrack_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm,
306 intel_wakeref_t wref)
311 __intel_wakeref_dec_and_check_tracking(struct intel_runtime_pm *rpm)
313 atomic_dec(&rpm->wakeref_count);
317 untrack_all_intel_runtime_pm_wakerefs(struct intel_runtime_pm *rpm)
324 intel_runtime_pm_acquire(struct intel_runtime_pm *rpm, bool wakelock)
327 atomic_add(1 + INTEL_RPM_WAKELOCK_BIAS, &rpm->wakeref_count);
328 assert_rpm_wakelock_held(rpm);
330 atomic_inc(&rpm->wakeref_count);
331 assert_rpm_raw_wakeref_held(rpm);
336 intel_runtime_pm_release(struct intel_runtime_pm *rpm, int wakelock)
339 assert_rpm_wakelock_held(rpm);
340 atomic_sub(INTEL_RPM_WAKELOCK_BIAS, &rpm->wakeref_count);
342 assert_rpm_raw_wakeref_held(rpm);
345 __intel_wakeref_dec_and_check_tracking(rpm);
348 static intel_wakeref_t __intel_runtime_pm_get(struct intel_runtime_pm *rpm,
351 struct drm_i915_private *i915 = container_of(rpm,
352 struct drm_i915_private,
356 ret = pm_runtime_get_sync(rpm->kdev);
357 drm_WARN_ONCE(&i915->drm, ret < 0,
358 "pm_runtime_get_sync() failed: %d\n", ret);
360 intel_runtime_pm_acquire(rpm, wakelock);
362 return track_intel_runtime_pm_wakeref(rpm);
366 * intel_runtime_pm_get_raw - grab a raw runtime pm reference
367 * @rpm: the intel_runtime_pm structure
369 * This is the unlocked version of intel_display_power_is_enabled() and should
370 * only be used from error capture and recovery code where deadlocks are
372 * This function grabs a device-level runtime pm reference (mostly used for
373 * asynchronous PM management from display code) and ensures that it is powered
374 * up. Raw references are not considered during wakelock assert checks.
376 * Any runtime pm reference obtained by this function must have a symmetric
377 * call to intel_runtime_pm_put_raw() to release the reference again.
379 * Returns: the wakeref cookie to pass to intel_runtime_pm_put_raw(), evaluates
380 * as True if the wakeref was acquired, or False otherwise.
382 intel_wakeref_t intel_runtime_pm_get_raw(struct intel_runtime_pm *rpm)
384 return __intel_runtime_pm_get(rpm, false);
388 * intel_runtime_pm_get - grab a runtime pm reference
389 * @rpm: the intel_runtime_pm structure
391 * This function grabs a device-level runtime pm reference (mostly used for GEM
392 * code to ensure the GTT or GT is on) and ensures that it is powered up.
394 * Any runtime pm reference obtained by this function must have a symmetric
395 * call to intel_runtime_pm_put() to release the reference again.
397 * Returns: the wakeref cookie to pass to intel_runtime_pm_put()
399 intel_wakeref_t intel_runtime_pm_get(struct intel_runtime_pm *rpm)
401 return __intel_runtime_pm_get(rpm, true);
405 * __intel_runtime_pm_get_if_active - grab a runtime pm reference if device is active
406 * @rpm: the intel_runtime_pm structure
407 * @ignore_usecount: get a ref even if dev->power.usage_count is 0
409 * This function grabs a device-level runtime pm reference if the device is
410 * already active and ensures that it is powered up. It is illegal to try
411 * and access the HW should intel_runtime_pm_get_if_active() report failure.
413 * If @ignore_usecount is true, a reference will be acquired even if there is no
414 * user requiring the device to be powered up (dev->power.usage_count == 0).
415 * If the function returns false in this case then it's guaranteed that the
416 * device's runtime suspend hook has been called already or that it will be
417 * called (and hence it's also guaranteed that the device's runtime resume
418 * hook will be called eventually).
420 * Any runtime pm reference obtained by this function must have a symmetric
421 * call to intel_runtime_pm_put() to release the reference again.
423 * Returns: the wakeref cookie to pass to intel_runtime_pm_put(), evaluates
424 * as True if the wakeref was acquired, or False otherwise.
426 static intel_wakeref_t __intel_runtime_pm_get_if_active(struct intel_runtime_pm *rpm,
427 bool ignore_usecount)
429 if (IS_ENABLED(CONFIG_PM)) {
431 * In cases runtime PM is disabled by the RPM core and we get
432 * an -EINVAL return value we are not supposed to call this
433 * function, since the power state is undefined. This applies
434 * atm to the late/early system suspend/resume handlers.
436 if (pm_runtime_get_if_active(rpm->kdev, ignore_usecount) <= 0)
440 intel_runtime_pm_acquire(rpm, true);
442 return track_intel_runtime_pm_wakeref(rpm);
445 intel_wakeref_t intel_runtime_pm_get_if_in_use(struct intel_runtime_pm *rpm)
447 return __intel_runtime_pm_get_if_active(rpm, false);
450 intel_wakeref_t intel_runtime_pm_get_if_active(struct intel_runtime_pm *rpm)
452 return __intel_runtime_pm_get_if_active(rpm, true);
456 * intel_runtime_pm_get_noresume - grab a runtime pm reference
457 * @rpm: the intel_runtime_pm structure
459 * This function grabs a device-level runtime pm reference (mostly used for GEM
460 * code to ensure the GTT or GT is on).
462 * It will _not_ power up the device but instead only check that it's powered
463 * on. Therefore it is only valid to call this functions from contexts where
464 * the device is known to be powered up and where trying to power it up would
465 * result in hilarity and deadlocks. That pretty much means only the system
466 * suspend/resume code where this is used to grab runtime pm references for
467 * delayed setup down in work items.
469 * Any runtime pm reference obtained by this function must have a symmetric
470 * call to intel_runtime_pm_put() to release the reference again.
472 * Returns: the wakeref cookie to pass to intel_runtime_pm_put()
474 intel_wakeref_t intel_runtime_pm_get_noresume(struct intel_runtime_pm *rpm)
476 assert_rpm_wakelock_held(rpm);
477 pm_runtime_get_noresume(rpm->kdev);
479 intel_runtime_pm_acquire(rpm, true);
481 return track_intel_runtime_pm_wakeref(rpm);
484 static void __intel_runtime_pm_put(struct intel_runtime_pm *rpm,
485 intel_wakeref_t wref,
488 struct device *kdev = rpm->kdev;
490 untrack_intel_runtime_pm_wakeref(rpm, wref);
492 intel_runtime_pm_release(rpm, wakelock);
494 pm_runtime_mark_last_busy(kdev);
495 pm_runtime_put_autosuspend(kdev);
499 * intel_runtime_pm_put_raw - release a raw runtime pm reference
500 * @rpm: the intel_runtime_pm structure
501 * @wref: wakeref acquired for the reference that is being released
503 * This function drops the device-level runtime pm reference obtained by
504 * intel_runtime_pm_get_raw() and might power down the corresponding
505 * hardware block right away if this is the last reference.
508 intel_runtime_pm_put_raw(struct intel_runtime_pm *rpm, intel_wakeref_t wref)
510 __intel_runtime_pm_put(rpm, wref, false);
514 * intel_runtime_pm_put_unchecked - release an unchecked runtime pm reference
515 * @rpm: the intel_runtime_pm structure
517 * This function drops the device-level runtime pm reference obtained by
518 * intel_runtime_pm_get() and might power down the corresponding
519 * hardware block right away if this is the last reference.
521 * This function exists only for historical reasons and should be avoided in
522 * new code, as the correctness of its use cannot be checked. Always use
523 * intel_runtime_pm_put() instead.
525 void intel_runtime_pm_put_unchecked(struct intel_runtime_pm *rpm)
527 __intel_runtime_pm_put(rpm, -1, true);
530 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_RUNTIME_PM)
532 * intel_runtime_pm_put - release a runtime pm reference
533 * @rpm: the intel_runtime_pm structure
534 * @wref: wakeref acquired for the reference that is being released
536 * This function drops the device-level runtime pm reference obtained by
537 * intel_runtime_pm_get() and might power down the corresponding
538 * hardware block right away if this is the last reference.
540 void intel_runtime_pm_put(struct intel_runtime_pm *rpm, intel_wakeref_t wref)
542 __intel_runtime_pm_put(rpm, wref, true);
547 * intel_runtime_pm_enable - enable runtime pm
548 * @rpm: the intel_runtime_pm structure
550 * This function enables runtime pm at the end of the driver load sequence.
552 * Note that this function does currently not enable runtime pm for the
553 * subordinate display power domains. That is done by
554 * intel_power_domains_enable().
556 void intel_runtime_pm_enable(struct intel_runtime_pm *rpm)
558 struct drm_i915_private *i915 = container_of(rpm,
559 struct drm_i915_private,
561 struct device *kdev = rpm->kdev;
564 * Disable the system suspend direct complete optimization, which can
565 * leave the device suspended skipping the driver's suspend handlers
566 * if the device was already runtime suspended. This is needed due to
567 * the difference in our runtime and system suspend sequence and
568 * becaue the HDA driver may require us to enable the audio power
569 * domain during system suspend.
571 dev_pm_set_driver_flags(kdev, DPM_FLAG_NO_DIRECT_COMPLETE);
573 pm_runtime_set_autosuspend_delay(kdev, 10000); /* 10s */
574 pm_runtime_mark_last_busy(kdev);
577 * Take a permanent reference to disable the RPM functionality and drop
578 * it only when unloading the driver. Use the low level get/put helpers,
579 * so the driver's own RPM reference tracking asserts also work on
580 * platforms without RPM support.
582 if (!rpm->available) {
585 pm_runtime_dont_use_autosuspend(kdev);
586 ret = pm_runtime_get_sync(kdev);
587 drm_WARN(&i915->drm, ret < 0,
588 "pm_runtime_get_sync() failed: %d\n", ret);
590 pm_runtime_use_autosuspend(kdev);
594 * The core calls the driver load handler with an RPM reference held.
595 * We drop that here and will reacquire it during unloading in
596 * intel_power_domains_fini().
598 pm_runtime_put_autosuspend(kdev);
601 void intel_runtime_pm_disable(struct intel_runtime_pm *rpm)
603 struct drm_i915_private *i915 = container_of(rpm,
604 struct drm_i915_private,
606 struct device *kdev = rpm->kdev;
608 /* Transfer rpm ownership back to core */
609 drm_WARN(&i915->drm, pm_runtime_get_sync(kdev) < 0,
610 "Failed to pass rpm ownership back to core\n");
612 pm_runtime_dont_use_autosuspend(kdev);
615 pm_runtime_put(kdev);
618 void intel_runtime_pm_driver_release(struct intel_runtime_pm *rpm)
620 struct drm_i915_private *i915 = container_of(rpm,
621 struct drm_i915_private,
623 int count = atomic_read(&rpm->wakeref_count);
625 drm_WARN(&i915->drm, count,
626 "i915 raw-wakerefs=%d wakelocks=%d on cleanup\n",
627 intel_rpm_raw_wakeref_count(count),
628 intel_rpm_wakelock_count(count));
630 untrack_all_intel_runtime_pm_wakerefs(rpm);
633 void intel_runtime_pm_init_early(struct intel_runtime_pm *rpm)
635 struct drm_i915_private *i915 =
636 container_of(rpm, struct drm_i915_private, runtime_pm);
637 struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
638 struct device *kdev = &pdev->dev;
641 rpm->available = HAS_RUNTIME_PM(i915);
643 init_intel_runtime_pm_wakeref(rpm);