2 * SPDX-License-Identifier: MIT
4 * Copyright © 2019 Intel Corporation
7 #ifndef INTEL_WAKEREF_H
8 #define INTEL_WAKEREF_H
10 #include <linux/atomic.h>
11 #include <linux/bits.h>
12 #include <linux/mutex.h>
13 #include <linux/refcount.h>
14 #include <linux/stackdepot.h>
15 #include <linux/timer.h>
16 #include <linux/workqueue.h>
18 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG)
19 #define INTEL_WAKEREF_BUG_ON(expr) BUG_ON(expr)
21 #define INTEL_WAKEREF_BUG_ON(expr) BUILD_BUG_ON_INVALID(expr)
24 struct intel_runtime_pm;
27 typedef depot_stack_handle_t intel_wakeref_t;
29 struct intel_wakeref_ops {
30 int (*get)(struct intel_wakeref *wf);
31 int (*put)(struct intel_wakeref *wf);
34 #define INTEL_WAKEREF_PUT_ASYNC BIT(0)
37 struct intel_wakeref {
41 intel_wakeref_t wakeref;
43 struct intel_runtime_pm *rpm;
44 const struct intel_wakeref_ops *ops;
46 struct work_struct work;
49 void __intel_wakeref_init(struct intel_wakeref *wf,
50 struct intel_runtime_pm *rpm,
51 const struct intel_wakeref_ops *ops,
52 struct lock_class_key *key);
53 #define intel_wakeref_init(wf, rpm, ops) do { \
54 static struct lock_class_key __key; \
56 __intel_wakeref_init((wf), (rpm), (ops), &__key); \
59 int __intel_wakeref_get_first(struct intel_wakeref *wf);
60 void __intel_wakeref_put_last(struct intel_wakeref *wf);
63 * intel_wakeref_get: Acquire the wakeref
64 * @i915: the drm_i915_private device
66 * @fn: callback for acquired the wakeref, called only on first acquire.
68 * Acquire a hold on the wakeref. The first user to do so, will acquire
69 * the runtime pm wakeref and then call the @fn underneath the wakeref
72 * Note that @fn is allowed to fail, in which case the runtime-pm wakeref
73 * will be released and the acquisition unwound, and an error reported.
75 * Returns: 0 if the wakeref was acquired successfully, or a negative error
79 intel_wakeref_get(struct intel_wakeref *wf)
81 if (unlikely(!atomic_inc_not_zero(&wf->count)))
82 return __intel_wakeref_get_first(wf);
88 * intel_wakeref_get_if_in_use: Acquire the wakeref
91 * Acquire a hold on the wakeref, but only if the wakeref is already
94 * Returns: true if the wakeref was acquired, false otherwise.
97 intel_wakeref_get_if_active(struct intel_wakeref *wf)
99 return atomic_inc_not_zero(&wf->count);
103 * intel_wakeref_put: Release the wakeref
104 * @i915: the drm_i915_private device
106 * @fn: callback for releasing the wakeref, called only on final release.
108 * Release our hold on the wakeref. When there are no more users,
109 * the runtime pm wakeref will be released after the @fn callback is called
110 * underneath the wakeref mutex.
112 * Note that @fn is allowed to fail, in which case the runtime-pm wakeref
113 * is retained and an error reported.
115 * Returns: 0 if the wakeref was released successfully, or a negative error
119 intel_wakeref_put(struct intel_wakeref *wf)
121 INTEL_WAKEREF_BUG_ON(atomic_read(&wf->count) <= 0);
122 if (unlikely(!atomic_add_unless(&wf->count, -1, 1)))
123 __intel_wakeref_put_last(wf);
127 * intel_wakeref_lock: Lock the wakeref (mutex)
130 * Locks the wakeref to prevent it being acquired or released. New users
131 * can still adjust the counter, but the wakeref itself (and callback)
132 * cannot be acquired or released.
135 intel_wakeref_lock(struct intel_wakeref *wf)
136 __acquires(wf->mutex)
138 mutex_lock(&wf->mutex);
142 * intel_wakeref_unlock: Unlock the wakeref
145 * Releases a previously acquired intel_wakeref_lock().
148 intel_wakeref_unlock(struct intel_wakeref *wf)
149 __releases(wf->mutex)
151 mutex_unlock(&wf->mutex);
155 * intel_wakeref_is_active: Query whether the wakeref is currently held
158 * Returns: true if the wakeref is currently held.
161 intel_wakeref_is_active(const struct intel_wakeref *wf)
163 return READ_ONCE(wf->wakeref);
167 * __intel_wakeref_defer_park: Defer the current park callback
171 __intel_wakeref_defer_park(struct intel_wakeref *wf)
173 INTEL_WAKEREF_BUG_ON(atomic_read(&wf->count));
174 atomic_set_release(&wf->count, 1);
178 * intel_wakeref_wait_for_idle: Wait until the wakeref is idle
181 * Wait for the earlier asynchronous release of the wakeref. Note
182 * this will wait for any third party as well, so make sure you only wait
183 * when you have control over the wakeref and trust no one else is acquiring
186 * Return: 0 on success, error code if killed.
188 int intel_wakeref_wait_for_idle(struct intel_wakeref *wf);
190 struct intel_wakeref_auto {
191 struct intel_runtime_pm *rpm;
192 struct timer_list timer;
193 intel_wakeref_t wakeref;
199 * intel_wakeref_auto: Delay the runtime-pm autosuspend
201 * @timeout: relative timeout in jiffies
203 * The runtime-pm core uses a suspend delay after the last wakeref
204 * is released before triggering runtime suspend of the device. That
205 * delay is configurable via sysfs with little regard to the device
206 * characteristics. Instead, we want to tune the autosuspend based on our
207 * HW knowledge. intel_wakeref_auto() delays the sleep by the supplied
210 * Pass @timeout = 0 to cancel a previous autosuspend by executing the
211 * suspend immediately.
213 void intel_wakeref_auto(struct intel_wakeref_auto *wf, unsigned long timeout);
215 void intel_wakeref_auto_init(struct intel_wakeref_auto *wf,
216 struct intel_runtime_pm *rpm);
217 void intel_wakeref_auto_fini(struct intel_wakeref_auto *wf);
219 #endif /* INTEL_WAKEREF_H */