#include <linux/pm_runtime.h>
#include <drm/drm_print.h>
#include <drm/intel/display_parent_interface.h>
#include "i915_drv.h"
#include "i915_trace.h"
static struct drm_i915_private *rpm_to_i915(struct intel_runtime_pm *rpm)
{
return container_of(rpm, struct drm_i915_private, runtime_pm);
}
#if IS_ENABLED(CONFIG_DRM_I915_DEBUG_RUNTIME_PM)
static void init_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm)
{
if (!rpm->debug.class)
ref_tracker_dir_init(&rpm->debug, INTEL_REFTRACK_DEAD_COUNT,
"intel_runtime_pm");
}
static intel_wakeref_t
track_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm)
{
if (!rpm->available || rpm->no_wakeref_tracking)
return INTEL_WAKEREF_DEF;
return intel_ref_tracker_alloc(&rpm->debug);
}
static void untrack_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm,
intel_wakeref_t wakeref)
{
if (!rpm->available || rpm->no_wakeref_tracking)
return;
intel_ref_tracker_free(&rpm->debug, wakeref);
}
static void untrack_all_intel_runtime_pm_wakerefs(struct intel_runtime_pm *rpm)
{
ref_tracker_dir_exit(&rpm->debug);
}
static noinline void
__intel_wakeref_dec_and_check_tracking(struct intel_runtime_pm *rpm)
{
unsigned long flags;
if (!atomic_dec_and_lock_irqsave(&rpm->wakeref_count,
&rpm->debug.lock,
flags))
return;
ref_tracker_dir_print_locked(&rpm->debug, INTEL_REFTRACK_PRINT_LIMIT);
spin_unlock_irqrestore(&rpm->debug.lock, flags);
}
void print_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm,
struct drm_printer *p)
{
intel_ref_tracker_show(&rpm->debug, p);
}
#else
static void init_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm)
{
}
static intel_wakeref_t
track_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm)
{
return INTEL_WAKEREF_DEF;
}
static void untrack_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm,
intel_wakeref_t wakeref)
{
}
static void
__intel_wakeref_dec_and_check_tracking(struct intel_runtime_pm *rpm)
{
atomic_dec(&rpm->wakeref_count);
}
static void
untrack_all_intel_runtime_pm_wakerefs(struct intel_runtime_pm *rpm)
{
}
#endif
static void
intel_runtime_pm_acquire(struct intel_runtime_pm *rpm, bool wakelock)
{
if (wakelock) {
atomic_add(1 + INTEL_RPM_WAKELOCK_BIAS, &rpm->wakeref_count);
assert_rpm_wakelock_held(rpm);
} else {
atomic_inc(&rpm->wakeref_count);
assert_rpm_raw_wakeref_held(rpm);
}
}
static void
intel_runtime_pm_release(struct intel_runtime_pm *rpm, int wakelock)
{
if (wakelock) {
assert_rpm_wakelock_held(rpm);
atomic_sub(INTEL_RPM_WAKELOCK_BIAS, &rpm->wakeref_count);
} else {
assert_rpm_raw_wakeref_held(rpm);
}
__intel_wakeref_dec_and_check_tracking(rpm);
}
static intel_wakeref_t __intel_runtime_pm_get(struct intel_runtime_pm *rpm,
bool wakelock)
{
struct drm_i915_private *i915 = rpm_to_i915(rpm);
int ret;
ret = pm_runtime_get_sync(rpm->kdev);
drm_WARN_ONCE(&i915->drm, ret < 0,
"pm_runtime_get_sync() failed: %d\n", ret);
intel_runtime_pm_acquire(rpm, wakelock);
return track_intel_runtime_pm_wakeref(rpm);
}
static struct intel_runtime_pm *drm_to_rpm(const struct drm_device *drm)
{
struct drm_i915_private *i915 = to_i915(drm);
return &i915->runtime_pm;
}
static struct ref_tracker *i915_display_rpm_get(const struct drm_device *drm)
{
return intel_runtime_pm_get(drm_to_rpm(drm));
}
static struct ref_tracker *i915_display_rpm_get_raw(const struct drm_device *drm)
{
return intel_runtime_pm_get_raw(drm_to_rpm(drm));
}
static struct ref_tracker *i915_display_rpm_get_if_in_use(const struct drm_device *drm)
{
return intel_runtime_pm_get_if_in_use(drm_to_rpm(drm));
}
static struct ref_tracker *i915_display_rpm_get_noresume(const struct drm_device *drm)
{
return intel_runtime_pm_get_noresume(drm_to_rpm(drm));
}
static void i915_display_rpm_put(const struct drm_device *drm, struct ref_tracker *wakeref)
{
intel_runtime_pm_put(drm_to_rpm(drm), wakeref);
}
static void i915_display_rpm_put_raw(const struct drm_device *drm, struct ref_tracker *wakeref)
{
intel_runtime_pm_put_raw(drm_to_rpm(drm), wakeref);
}
static void i915_display_rpm_put_unchecked(const struct drm_device *drm)
{
intel_runtime_pm_put_unchecked(drm_to_rpm(drm));
}
static bool i915_display_rpm_suspended(const struct drm_device *drm)
{
return intel_runtime_pm_suspended(drm_to_rpm(drm));
}
static void i915_display_rpm_assert_held(const struct drm_device *drm)
{
assert_rpm_wakelock_held(drm_to_rpm(drm));
}
static void i915_display_rpm_assert_block(const struct drm_device *drm)
{
disable_rpm_wakeref_asserts(drm_to_rpm(drm));
}
static void i915_display_rpm_assert_unblock(const struct drm_device *drm)
{
enable_rpm_wakeref_asserts(drm_to_rpm(drm));
}
const struct intel_display_rpm_interface i915_display_rpm_interface = {
.get = i915_display_rpm_get,
.get_raw = i915_display_rpm_get_raw,
.get_if_in_use = i915_display_rpm_get_if_in_use,
.get_noresume = i915_display_rpm_get_noresume,
.put = i915_display_rpm_put,
.put_raw = i915_display_rpm_put_raw,
.put_unchecked = i915_display_rpm_put_unchecked,
.suspended = i915_display_rpm_suspended,
.assert_held = i915_display_rpm_assert_held,
.assert_block = i915_display_rpm_assert_block,
.assert_unblock = i915_display_rpm_assert_unblock
};
intel_wakeref_t intel_runtime_pm_get_raw(struct intel_runtime_pm *rpm)
{
return __intel_runtime_pm_get(rpm, false);
}
intel_wakeref_t intel_runtime_pm_get(struct intel_runtime_pm *rpm)
{
return __intel_runtime_pm_get(rpm, true);
}
static intel_wakeref_t __intel_runtime_pm_get_if_active(struct intel_runtime_pm *rpm,
bool ignore_usecount)
{
if (IS_ENABLED(CONFIG_PM)) {
if ((ignore_usecount &&
pm_runtime_get_if_active(rpm->kdev) <= 0) ||
(!ignore_usecount &&
pm_runtime_get_if_in_use(rpm->kdev) <= 0))
return NULL;
}
intel_runtime_pm_acquire(rpm, true);
return track_intel_runtime_pm_wakeref(rpm);
}
intel_wakeref_t intel_runtime_pm_get_if_in_use(struct intel_runtime_pm *rpm)
{
return __intel_runtime_pm_get_if_active(rpm, false);
}
intel_wakeref_t intel_runtime_pm_get_if_active(struct intel_runtime_pm *rpm)
{
return __intel_runtime_pm_get_if_active(rpm, true);
}
intel_wakeref_t intel_runtime_pm_get_noresume(struct intel_runtime_pm *rpm)
{
assert_rpm_raw_wakeref_held(rpm);
pm_runtime_get_noresume(rpm->kdev);
intel_runtime_pm_acquire(rpm, true);
return track_intel_runtime_pm_wakeref(rpm);
}
static void __intel_runtime_pm_put(struct intel_runtime_pm *rpm,
intel_wakeref_t wref,
bool wakelock)
{
struct device *kdev = rpm->kdev;
untrack_intel_runtime_pm_wakeref(rpm, wref);
intel_runtime_pm_release(rpm, wakelock);
pm_runtime_mark_last_busy(kdev);
pm_runtime_put_autosuspend(kdev);
}
void
intel_runtime_pm_put_raw(struct intel_runtime_pm *rpm, intel_wakeref_t wref)
{
__intel_runtime_pm_put(rpm, wref, false);
}
void intel_runtime_pm_put_unchecked(struct intel_runtime_pm *rpm)
{
__intel_runtime_pm_put(rpm, INTEL_WAKEREF_DEF, true);
}
#if IS_ENABLED(CONFIG_DRM_I915_DEBUG_RUNTIME_PM)
void intel_runtime_pm_put(struct intel_runtime_pm *rpm, intel_wakeref_t wref)
{
__intel_runtime_pm_put(rpm, wref, true);
}
#endif
void intel_runtime_pm_enable(struct intel_runtime_pm *rpm)
{
struct drm_i915_private *i915 = rpm_to_i915(rpm);
struct device *kdev = rpm->kdev;
dev_pm_set_driver_flags(kdev, DPM_FLAG_NO_DIRECT_COMPLETE);
pm_runtime_set_autosuspend_delay(kdev, 10000);
pm_runtime_mark_last_busy(kdev);
if (!rpm->available) {
int ret;
pm_runtime_dont_use_autosuspend(kdev);
ret = pm_runtime_get_sync(kdev);
drm_WARN(&i915->drm, ret < 0,
"pm_runtime_get_sync() failed: %d\n", ret);
} else {
pm_runtime_use_autosuspend(kdev);
}
if (!IS_DGFX(i915))
pm_runtime_allow(kdev);
pm_runtime_put_autosuspend(kdev);
}
void intel_runtime_pm_disable(struct intel_runtime_pm *rpm)
{
struct drm_i915_private *i915 = rpm_to_i915(rpm);
struct device *kdev = rpm->kdev;
drm_WARN(&i915->drm, pm_runtime_get_sync(kdev) < 0,
"Failed to pass rpm ownership back to core\n");
pm_runtime_dont_use_autosuspend(kdev);
if (!rpm->available)
pm_runtime_put(kdev);
}
void intel_runtime_pm_driver_release(struct intel_runtime_pm *rpm)
{
struct drm_i915_private *i915 = rpm_to_i915(rpm);
int count = atomic_read(&rpm->wakeref_count);
intel_wakeref_auto_fini(&rpm->userfault_wakeref);
drm_WARN(&i915->drm, count,
"i915 raw-wakerefs=%d wakelocks=%d on cleanup\n",
intel_rpm_raw_wakeref_count(count),
intel_rpm_wakelock_count(count));
}
void intel_runtime_pm_driver_last_release(struct intel_runtime_pm *rpm)
{
intel_runtime_pm_driver_release(rpm);
untrack_all_intel_runtime_pm_wakerefs(rpm);
}
void intel_runtime_pm_init_early(struct intel_runtime_pm *rpm)
{
struct drm_i915_private *i915 = rpm_to_i915(rpm);
struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
struct device *kdev = &pdev->dev;
rpm->kdev = kdev;
rpm->available = HAS_RUNTIME_PM(i915);
atomic_set(&rpm->wakeref_count, 0);
init_intel_runtime_pm_wakeref(rpm);
INIT_LIST_HEAD(&rpm->lmem_userfault_list);
spin_lock_init(&rpm->lmem_userfault_lock);
intel_wakeref_auto_init(&rpm->userfault_wakeref, i915);
}