18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * SPDX-License-Identifier: MIT
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright © 2019 Intel Corporation
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#ifndef INTEL_WAKEREF_H
88c2ecf20Sopenharmony_ci#define INTEL_WAKEREF_H
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/atomic.h>
118c2ecf20Sopenharmony_ci#include <linux/bitfield.h>
128c2ecf20Sopenharmony_ci#include <linux/bits.h>
138c2ecf20Sopenharmony_ci#include <linux/lockdep.h>
148c2ecf20Sopenharmony_ci#include <linux/mutex.h>
158c2ecf20Sopenharmony_ci#include <linux/refcount.h>
168c2ecf20Sopenharmony_ci#include <linux/stackdepot.h>
178c2ecf20Sopenharmony_ci#include <linux/timer.h>
188c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_DRM_I915_DEBUG)
218c2ecf20Sopenharmony_ci#define INTEL_WAKEREF_BUG_ON(expr) BUG_ON(expr)
228c2ecf20Sopenharmony_ci#else
238c2ecf20Sopenharmony_ci#define INTEL_WAKEREF_BUG_ON(expr) BUILD_BUG_ON_INVALID(expr)
248c2ecf20Sopenharmony_ci#endif
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistruct intel_runtime_pm;
278c2ecf20Sopenharmony_cistruct intel_wakeref;
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_citypedef depot_stack_handle_t intel_wakeref_t;
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistruct intel_wakeref_ops {
328c2ecf20Sopenharmony_ci	int (*get)(struct intel_wakeref *wf);
338c2ecf20Sopenharmony_ci	int (*put)(struct intel_wakeref *wf);
348c2ecf20Sopenharmony_ci};
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_cistruct intel_wakeref {
378c2ecf20Sopenharmony_ci	atomic_t count;
388c2ecf20Sopenharmony_ci	struct mutex mutex;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	intel_wakeref_t wakeref;
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	struct intel_runtime_pm *rpm;
438c2ecf20Sopenharmony_ci	const struct intel_wakeref_ops *ops;
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	struct delayed_work work;
468c2ecf20Sopenharmony_ci};
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistruct intel_wakeref_lockclass {
498c2ecf20Sopenharmony_ci	struct lock_class_key mutex;
508c2ecf20Sopenharmony_ci	struct lock_class_key work;
518c2ecf20Sopenharmony_ci};
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_civoid __intel_wakeref_init(struct intel_wakeref *wf,
548c2ecf20Sopenharmony_ci			  struct intel_runtime_pm *rpm,
558c2ecf20Sopenharmony_ci			  const struct intel_wakeref_ops *ops,
568c2ecf20Sopenharmony_ci			  struct intel_wakeref_lockclass *key);
578c2ecf20Sopenharmony_ci#define intel_wakeref_init(wf, rpm, ops) do {				\
588c2ecf20Sopenharmony_ci	static struct intel_wakeref_lockclass __key;			\
598c2ecf20Sopenharmony_ci									\
608c2ecf20Sopenharmony_ci	__intel_wakeref_init((wf), (rpm), (ops), &__key);		\
618c2ecf20Sopenharmony_ci} while (0)
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ciint __intel_wakeref_get_first(struct intel_wakeref *wf);
648c2ecf20Sopenharmony_civoid __intel_wakeref_put_last(struct intel_wakeref *wf, unsigned long flags);
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci/**
678c2ecf20Sopenharmony_ci * intel_wakeref_get: Acquire the wakeref
688c2ecf20Sopenharmony_ci * @wf: the wakeref
698c2ecf20Sopenharmony_ci *
708c2ecf20Sopenharmony_ci * Acquire a hold on the wakeref. The first user to do so, will acquire
718c2ecf20Sopenharmony_ci * the runtime pm wakeref and then call the @fn underneath the wakeref
728c2ecf20Sopenharmony_ci * mutex.
738c2ecf20Sopenharmony_ci *
748c2ecf20Sopenharmony_ci * Note that @fn is allowed to fail, in which case the runtime-pm wakeref
758c2ecf20Sopenharmony_ci * will be released and the acquisition unwound, and an error reported.
768c2ecf20Sopenharmony_ci *
778c2ecf20Sopenharmony_ci * Returns: 0 if the wakeref was acquired successfully, or a negative error
788c2ecf20Sopenharmony_ci * code otherwise.
798c2ecf20Sopenharmony_ci */
808c2ecf20Sopenharmony_cistatic inline int
818c2ecf20Sopenharmony_ciintel_wakeref_get(struct intel_wakeref *wf)
828c2ecf20Sopenharmony_ci{
838c2ecf20Sopenharmony_ci	might_sleep();
848c2ecf20Sopenharmony_ci	if (unlikely(!atomic_inc_not_zero(&wf->count)))
858c2ecf20Sopenharmony_ci		return __intel_wakeref_get_first(wf);
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	return 0;
888c2ecf20Sopenharmony_ci}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci/**
918c2ecf20Sopenharmony_ci * __intel_wakeref_get: Acquire the wakeref, again
928c2ecf20Sopenharmony_ci * @wf: the wakeref
938c2ecf20Sopenharmony_ci *
948c2ecf20Sopenharmony_ci * Increment the wakeref counter, only valid if it is already held by
958c2ecf20Sopenharmony_ci * the caller.
968c2ecf20Sopenharmony_ci *
978c2ecf20Sopenharmony_ci * See intel_wakeref_get().
988c2ecf20Sopenharmony_ci */
998c2ecf20Sopenharmony_cistatic inline void
1008c2ecf20Sopenharmony_ci__intel_wakeref_get(struct intel_wakeref *wf)
1018c2ecf20Sopenharmony_ci{
1028c2ecf20Sopenharmony_ci	INTEL_WAKEREF_BUG_ON(atomic_read(&wf->count) <= 0);
1038c2ecf20Sopenharmony_ci	atomic_inc(&wf->count);
1048c2ecf20Sopenharmony_ci}
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci/**
1078c2ecf20Sopenharmony_ci * intel_wakeref_get_if_in_use: Acquire the wakeref
1088c2ecf20Sopenharmony_ci * @wf: the wakeref
1098c2ecf20Sopenharmony_ci *
1108c2ecf20Sopenharmony_ci * Acquire a hold on the wakeref, but only if the wakeref is already
1118c2ecf20Sopenharmony_ci * active.
1128c2ecf20Sopenharmony_ci *
1138c2ecf20Sopenharmony_ci * Returns: true if the wakeref was acquired, false otherwise.
1148c2ecf20Sopenharmony_ci */
1158c2ecf20Sopenharmony_cistatic inline bool
1168c2ecf20Sopenharmony_ciintel_wakeref_get_if_active(struct intel_wakeref *wf)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	return atomic_inc_not_zero(&wf->count);
1198c2ecf20Sopenharmony_ci}
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_cienum {
1228c2ecf20Sopenharmony_ci	INTEL_WAKEREF_PUT_ASYNC_BIT = 0,
1238c2ecf20Sopenharmony_ci	__INTEL_WAKEREF_PUT_LAST_BIT__
1248c2ecf20Sopenharmony_ci};
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci/**
1278c2ecf20Sopenharmony_ci * intel_wakeref_put_flags: Release the wakeref
1288c2ecf20Sopenharmony_ci * @wf: the wakeref
1298c2ecf20Sopenharmony_ci * @flags: control flags
1308c2ecf20Sopenharmony_ci *
1318c2ecf20Sopenharmony_ci * Release our hold on the wakeref. When there are no more users,
1328c2ecf20Sopenharmony_ci * the runtime pm wakeref will be released after the @fn callback is called
1338c2ecf20Sopenharmony_ci * underneath the wakeref mutex.
1348c2ecf20Sopenharmony_ci *
1358c2ecf20Sopenharmony_ci * Note that @fn is allowed to fail, in which case the runtime-pm wakeref
1368c2ecf20Sopenharmony_ci * is retained and an error reported.
1378c2ecf20Sopenharmony_ci *
1388c2ecf20Sopenharmony_ci * Returns: 0 if the wakeref was released successfully, or a negative error
1398c2ecf20Sopenharmony_ci * code otherwise.
1408c2ecf20Sopenharmony_ci */
1418c2ecf20Sopenharmony_cistatic inline void
1428c2ecf20Sopenharmony_ci__intel_wakeref_put(struct intel_wakeref *wf, unsigned long flags)
1438c2ecf20Sopenharmony_ci#define INTEL_WAKEREF_PUT_ASYNC BIT(INTEL_WAKEREF_PUT_ASYNC_BIT)
1448c2ecf20Sopenharmony_ci#define INTEL_WAKEREF_PUT_DELAY \
1458c2ecf20Sopenharmony_ci	GENMASK(BITS_PER_LONG - 1, __INTEL_WAKEREF_PUT_LAST_BIT__)
1468c2ecf20Sopenharmony_ci{
1478c2ecf20Sopenharmony_ci	INTEL_WAKEREF_BUG_ON(atomic_read(&wf->count) <= 0);
1488c2ecf20Sopenharmony_ci	if (unlikely(!atomic_add_unless(&wf->count, -1, 1)))
1498c2ecf20Sopenharmony_ci		__intel_wakeref_put_last(wf, flags);
1508c2ecf20Sopenharmony_ci}
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_cistatic inline void
1538c2ecf20Sopenharmony_ciintel_wakeref_put(struct intel_wakeref *wf)
1548c2ecf20Sopenharmony_ci{
1558c2ecf20Sopenharmony_ci	might_sleep();
1568c2ecf20Sopenharmony_ci	__intel_wakeref_put(wf, 0);
1578c2ecf20Sopenharmony_ci}
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_cistatic inline void
1608c2ecf20Sopenharmony_ciintel_wakeref_put_async(struct intel_wakeref *wf)
1618c2ecf20Sopenharmony_ci{
1628c2ecf20Sopenharmony_ci	__intel_wakeref_put(wf, INTEL_WAKEREF_PUT_ASYNC);
1638c2ecf20Sopenharmony_ci}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_cistatic inline void
1668c2ecf20Sopenharmony_ciintel_wakeref_put_delay(struct intel_wakeref *wf, unsigned long delay)
1678c2ecf20Sopenharmony_ci{
1688c2ecf20Sopenharmony_ci	__intel_wakeref_put(wf,
1698c2ecf20Sopenharmony_ci			    INTEL_WAKEREF_PUT_ASYNC |
1708c2ecf20Sopenharmony_ci			    FIELD_PREP(INTEL_WAKEREF_PUT_DELAY, delay));
1718c2ecf20Sopenharmony_ci}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci/**
1748c2ecf20Sopenharmony_ci * intel_wakeref_lock: Lock the wakeref (mutex)
1758c2ecf20Sopenharmony_ci * @wf: the wakeref
1768c2ecf20Sopenharmony_ci *
1778c2ecf20Sopenharmony_ci * Locks the wakeref to prevent it being acquired or released. New users
1788c2ecf20Sopenharmony_ci * can still adjust the counter, but the wakeref itself (and callback)
1798c2ecf20Sopenharmony_ci * cannot be acquired or released.
1808c2ecf20Sopenharmony_ci */
1818c2ecf20Sopenharmony_cistatic inline void
1828c2ecf20Sopenharmony_ciintel_wakeref_lock(struct intel_wakeref *wf)
1838c2ecf20Sopenharmony_ci	__acquires(wf->mutex)
1848c2ecf20Sopenharmony_ci{
1858c2ecf20Sopenharmony_ci	mutex_lock(&wf->mutex);
1868c2ecf20Sopenharmony_ci}
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci/**
1898c2ecf20Sopenharmony_ci * intel_wakeref_unlock: Unlock the wakeref
1908c2ecf20Sopenharmony_ci * @wf: the wakeref
1918c2ecf20Sopenharmony_ci *
1928c2ecf20Sopenharmony_ci * Releases a previously acquired intel_wakeref_lock().
1938c2ecf20Sopenharmony_ci */
1948c2ecf20Sopenharmony_cistatic inline void
1958c2ecf20Sopenharmony_ciintel_wakeref_unlock(struct intel_wakeref *wf)
1968c2ecf20Sopenharmony_ci	__releases(wf->mutex)
1978c2ecf20Sopenharmony_ci{
1988c2ecf20Sopenharmony_ci	mutex_unlock(&wf->mutex);
1998c2ecf20Sopenharmony_ci}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci/**
2028c2ecf20Sopenharmony_ci * intel_wakeref_unlock_wait: Wait until the active callback is complete
2038c2ecf20Sopenharmony_ci * @wf: the wakeref
2048c2ecf20Sopenharmony_ci *
2058c2ecf20Sopenharmony_ci * Waits for the active callback (under the @wf->mutex or another CPU) is
2068c2ecf20Sopenharmony_ci * complete.
2078c2ecf20Sopenharmony_ci */
2088c2ecf20Sopenharmony_cistatic inline void
2098c2ecf20Sopenharmony_ciintel_wakeref_unlock_wait(struct intel_wakeref *wf)
2108c2ecf20Sopenharmony_ci{
2118c2ecf20Sopenharmony_ci	mutex_lock(&wf->mutex);
2128c2ecf20Sopenharmony_ci	mutex_unlock(&wf->mutex);
2138c2ecf20Sopenharmony_ci	flush_delayed_work(&wf->work);
2148c2ecf20Sopenharmony_ci}
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci/**
2178c2ecf20Sopenharmony_ci * intel_wakeref_is_active: Query whether the wakeref is currently held
2188c2ecf20Sopenharmony_ci * @wf: the wakeref
2198c2ecf20Sopenharmony_ci *
2208c2ecf20Sopenharmony_ci * Returns: true if the wakeref is currently held.
2218c2ecf20Sopenharmony_ci */
2228c2ecf20Sopenharmony_cistatic inline bool
2238c2ecf20Sopenharmony_ciintel_wakeref_is_active(const struct intel_wakeref *wf)
2248c2ecf20Sopenharmony_ci{
2258c2ecf20Sopenharmony_ci	return READ_ONCE(wf->wakeref);
2268c2ecf20Sopenharmony_ci}
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci/**
2298c2ecf20Sopenharmony_ci * __intel_wakeref_defer_park: Defer the current park callback
2308c2ecf20Sopenharmony_ci * @wf: the wakeref
2318c2ecf20Sopenharmony_ci */
2328c2ecf20Sopenharmony_cistatic inline void
2338c2ecf20Sopenharmony_ci__intel_wakeref_defer_park(struct intel_wakeref *wf)
2348c2ecf20Sopenharmony_ci{
2358c2ecf20Sopenharmony_ci	lockdep_assert_held(&wf->mutex);
2368c2ecf20Sopenharmony_ci	INTEL_WAKEREF_BUG_ON(atomic_read(&wf->count));
2378c2ecf20Sopenharmony_ci	atomic_set_release(&wf->count, 1);
2388c2ecf20Sopenharmony_ci}
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci/**
2418c2ecf20Sopenharmony_ci * intel_wakeref_wait_for_idle: Wait until the wakeref is idle
2428c2ecf20Sopenharmony_ci * @wf: the wakeref
2438c2ecf20Sopenharmony_ci *
2448c2ecf20Sopenharmony_ci * Wait for the earlier asynchronous release of the wakeref. Note
2458c2ecf20Sopenharmony_ci * this will wait for any third party as well, so make sure you only wait
2468c2ecf20Sopenharmony_ci * when you have control over the wakeref and trust no one else is acquiring
2478c2ecf20Sopenharmony_ci * it.
2488c2ecf20Sopenharmony_ci *
2498c2ecf20Sopenharmony_ci * Return: 0 on success, error code if killed.
2508c2ecf20Sopenharmony_ci */
2518c2ecf20Sopenharmony_ciint intel_wakeref_wait_for_idle(struct intel_wakeref *wf);
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_cistruct intel_wakeref_auto {
2548c2ecf20Sopenharmony_ci	struct intel_runtime_pm *rpm;
2558c2ecf20Sopenharmony_ci	struct timer_list timer;
2568c2ecf20Sopenharmony_ci	intel_wakeref_t wakeref;
2578c2ecf20Sopenharmony_ci	spinlock_t lock;
2588c2ecf20Sopenharmony_ci	refcount_t count;
2598c2ecf20Sopenharmony_ci};
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci/**
2628c2ecf20Sopenharmony_ci * intel_wakeref_auto: Delay the runtime-pm autosuspend
2638c2ecf20Sopenharmony_ci * @wf: the wakeref
2648c2ecf20Sopenharmony_ci * @timeout: relative timeout in jiffies
2658c2ecf20Sopenharmony_ci *
2668c2ecf20Sopenharmony_ci * The runtime-pm core uses a suspend delay after the last wakeref
2678c2ecf20Sopenharmony_ci * is released before triggering runtime suspend of the device. That
2688c2ecf20Sopenharmony_ci * delay is configurable via sysfs with little regard to the device
2698c2ecf20Sopenharmony_ci * characteristics. Instead, we want to tune the autosuspend based on our
2708c2ecf20Sopenharmony_ci * HW knowledge. intel_wakeref_auto() delays the sleep by the supplied
2718c2ecf20Sopenharmony_ci * timeout.
2728c2ecf20Sopenharmony_ci *
2738c2ecf20Sopenharmony_ci * Pass @timeout = 0 to cancel a previous autosuspend by executing the
2748c2ecf20Sopenharmony_ci * suspend immediately.
2758c2ecf20Sopenharmony_ci */
2768c2ecf20Sopenharmony_civoid intel_wakeref_auto(struct intel_wakeref_auto *wf, unsigned long timeout);
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_civoid intel_wakeref_auto_init(struct intel_wakeref_auto *wf,
2798c2ecf20Sopenharmony_ci			     struct intel_runtime_pm *rpm);
2808c2ecf20Sopenharmony_civoid intel_wakeref_auto_fini(struct intel_wakeref_auto *wf);
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci#endif /* INTEL_WAKEREF_H */
283