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/bitfield.h>
12 #include <linux/bits.h>
13 #include <linux/lockdep.h>
14 #include <linux/mutex.h>
15 #include <linux/refcount.h>
16 #include <linux/stackdepot.h>
17 #include <linux/timer.h>
18 #include <linux/workqueue.h>
20 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG)
21 #define INTEL_WAKEREF_BUG_ON(expr) BUG_ON(expr)
23 #define INTEL_WAKEREF_BUG_ON(expr) BUILD_BUG_ON_INVALID(expr)
26 struct intel_runtime_pm;
29 typedef depot_stack_handle_t intel_wakeref_t;
31 struct intel_wakeref_ops {
32 int (*get)(struct intel_wakeref *wf);
33 int (*put)(struct intel_wakeref *wf);
36 struct intel_wakeref {
40 intel_wakeref_t wakeref;
42 struct drm_i915_private *i915;
43 const struct intel_wakeref_ops *ops;
45 struct delayed_work work;
48 struct intel_wakeref_lockclass {
49 struct lock_class_key mutex;
50 struct lock_class_key work;
53 void __intel_wakeref_init(struct intel_wakeref *wf,
54 struct drm_i915_private *i915,
55 const struct intel_wakeref_ops *ops,
56 struct intel_wakeref_lockclass *key);
57 #define intel_wakeref_init(wf, i915, ops) do { \
58 static struct intel_wakeref_lockclass __key; \
60 __intel_wakeref_init((wf), (i915), (ops), &__key); \
63 int __intel_wakeref_get_first(struct intel_wakeref *wf);
64 void __intel_wakeref_put_last(struct intel_wakeref *wf, unsigned long flags);
67 * intel_wakeref_get: Acquire the wakeref
70 * Acquire a hold on the wakeref. The first user to do so, will acquire
71 * the runtime pm wakeref and then call the intel_wakeref_ops->get()
72 * underneath the wakeref mutex.
74 * Note that intel_wakeref_ops->get() is allowed to fail, in which case
75 * the runtime-pm wakeref will be released and the acquisition unwound,
76 * and an error reported.
78 * Returns: 0 if the wakeref was acquired successfully, or a negative error
82 intel_wakeref_get(struct intel_wakeref *wf)
85 if (unlikely(!atomic_inc_not_zero(&wf->count)))
86 return __intel_wakeref_get_first(wf);
92 * __intel_wakeref_get: Acquire the wakeref, again
95 * Increment the wakeref counter, only valid if it is already held by
98 * See intel_wakeref_get().
101 __intel_wakeref_get(struct intel_wakeref *wf)
103 INTEL_WAKEREF_BUG_ON(atomic_read(&wf->count) <= 0);
104 atomic_inc(&wf->count);
108 * intel_wakeref_get_if_active: Acquire the wakeref
111 * Acquire a hold on the wakeref, but only if the wakeref is already
114 * Returns: true if the wakeref was acquired, false otherwise.
117 intel_wakeref_get_if_active(struct intel_wakeref *wf)
119 return atomic_inc_not_zero(&wf->count);
123 INTEL_WAKEREF_PUT_ASYNC_BIT = 0,
124 __INTEL_WAKEREF_PUT_LAST_BIT__
128 intel_wakeref_might_get(struct intel_wakeref *wf)
130 might_lock(&wf->mutex);
134 * __intel_wakeref_put: Release the wakeref
136 * @flags: control flags
138 * Release our hold on the wakeref. When there are no more users,
139 * the runtime pm wakeref will be released after the intel_wakeref_ops->put()
140 * callback is called underneath the wakeref mutex.
142 * Note that intel_wakeref_ops->put() is allowed to fail, in which case the
143 * runtime-pm wakeref is retained.
147 __intel_wakeref_put(struct intel_wakeref *wf, unsigned long flags)
148 #define INTEL_WAKEREF_PUT_ASYNC BIT(INTEL_WAKEREF_PUT_ASYNC_BIT)
149 #define INTEL_WAKEREF_PUT_DELAY \
150 GENMASK(BITS_PER_LONG - 1, __INTEL_WAKEREF_PUT_LAST_BIT__)
152 INTEL_WAKEREF_BUG_ON(atomic_read(&wf->count) <= 0);
153 if (unlikely(!atomic_add_unless(&wf->count, -1, 1)))
154 __intel_wakeref_put_last(wf, flags);
158 intel_wakeref_put(struct intel_wakeref *wf)
161 __intel_wakeref_put(wf, 0);
165 intel_wakeref_put_async(struct intel_wakeref *wf)
167 __intel_wakeref_put(wf, INTEL_WAKEREF_PUT_ASYNC);
171 intel_wakeref_put_delay(struct intel_wakeref *wf, unsigned long delay)
173 __intel_wakeref_put(wf,
174 INTEL_WAKEREF_PUT_ASYNC |
175 FIELD_PREP(INTEL_WAKEREF_PUT_DELAY, delay));
179 intel_wakeref_might_put(struct intel_wakeref *wf)
181 might_lock(&wf->mutex);
185 * intel_wakeref_lock: Lock the wakeref (mutex)
188 * Locks the wakeref to prevent it being acquired or released. New users
189 * can still adjust the counter, but the wakeref itself (and callback)
190 * cannot be acquired or released.
193 intel_wakeref_lock(struct intel_wakeref *wf)
194 __acquires(wf->mutex)
196 mutex_lock(&wf->mutex);
200 * intel_wakeref_unlock: Unlock the wakeref
203 * Releases a previously acquired intel_wakeref_lock().
206 intel_wakeref_unlock(struct intel_wakeref *wf)
207 __releases(wf->mutex)
209 mutex_unlock(&wf->mutex);
213 * intel_wakeref_unlock_wait: Wait until the active callback is complete
216 * Waits for the active callback (under the @wf->mutex or another CPU) is
220 intel_wakeref_unlock_wait(struct intel_wakeref *wf)
222 mutex_lock(&wf->mutex);
223 mutex_unlock(&wf->mutex);
224 flush_delayed_work(&wf->work);
228 * intel_wakeref_is_active: Query whether the wakeref is currently held
231 * Returns: true if the wakeref is currently held.
234 intel_wakeref_is_active(const struct intel_wakeref *wf)
236 return READ_ONCE(wf->wakeref);
240 * __intel_wakeref_defer_park: Defer the current park callback
244 __intel_wakeref_defer_park(struct intel_wakeref *wf)
246 lockdep_assert_held(&wf->mutex);
247 INTEL_WAKEREF_BUG_ON(atomic_read(&wf->count));
248 atomic_set_release(&wf->count, 1);
252 * intel_wakeref_wait_for_idle: Wait until the wakeref is idle
255 * Wait for the earlier asynchronous release of the wakeref. Note
256 * this will wait for any third party as well, so make sure you only wait
257 * when you have control over the wakeref and trust no one else is acquiring
260 * Return: 0 on success, error code if killed.
262 int intel_wakeref_wait_for_idle(struct intel_wakeref *wf);
264 struct intel_wakeref_auto {
265 struct drm_i915_private *i915;
266 struct timer_list timer;
267 intel_wakeref_t wakeref;
273 * intel_wakeref_auto: Delay the runtime-pm autosuspend
275 * @timeout: relative timeout in jiffies
277 * The runtime-pm core uses a suspend delay after the last wakeref
278 * is released before triggering runtime suspend of the device. That
279 * delay is configurable via sysfs with little regard to the device
280 * characteristics. Instead, we want to tune the autosuspend based on our
281 * HW knowledge. intel_wakeref_auto() delays the sleep by the supplied
284 * Pass @timeout = 0 to cancel a previous autosuspend by executing the
285 * suspend immediately.
287 void intel_wakeref_auto(struct intel_wakeref_auto *wf, unsigned long timeout);
289 void intel_wakeref_auto_init(struct intel_wakeref_auto *wf,
290 struct drm_i915_private *i915);
291 void intel_wakeref_auto_fini(struct intel_wakeref_auto *wf);
293 #endif /* INTEL_WAKEREF_H */