1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright © 2019 Intel Corporation
5 */
6
7 #ifndef INTEL_WAKEREF_H
8 #define INTEL_WAKEREF_H
9
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>
19
20 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG)
21 #define INTEL_WAKEREF_BUG_ON(expr) BUG_ON(expr)
22 #else
23 #define INTEL_WAKEREF_BUG_ON(expr) BUILD_BUG_ON_INVALID(expr)
24 #endif
25
26 struct intel_runtime_pm;
27 struct intel_wakeref;
28
29 typedef depot_stack_handle_t intel_wakeref_t;
30
31 struct intel_wakeref_ops {
32 int (*get)(struct intel_wakeref *wf);
33 int (*put)(struct intel_wakeref *wf);
34 };
35
36 struct intel_wakeref {
37 atomic_t count;
38 struct rwlock mutex;
39
40 intel_wakeref_t wakeref;
41
42 #define drm_i915_private inteldrm_softc
43 struct drm_i915_private *i915;
44 const struct intel_wakeref_ops *ops;
45
46 struct delayed_work work;
47 };
48
49 struct intel_wakeref_lockclass {
50 struct lock_class_key mutex;
51 struct lock_class_key work;
52 };
53
54 void __intel_wakeref_init(struct intel_wakeref *wf,
55 struct drm_i915_private *i915,
56 const struct intel_wakeref_ops *ops,
57 struct intel_wakeref_lockclass *key);
58 #define intel_wakeref_init(wf, i915, ops) do { \
59 static struct intel_wakeref_lockclass __key; \
60 \
61 __intel_wakeref_init((wf), (i915), (ops), &__key); \
62 } while (0)
63
64 int __intel_wakeref_get_first(struct intel_wakeref *wf);
65 void __intel_wakeref_put_last(struct intel_wakeref *wf, unsigned long flags);
66
67 /**
68 * intel_wakeref_get: Acquire the wakeref
69 * @wf: the wakeref
70 *
71 * Acquire a hold on the wakeref. The first user to do so, will acquire
72 * the runtime pm wakeref and then call the intel_wakeref_ops->get()
73 * underneath the wakeref mutex.
74 *
75 * Note that intel_wakeref_ops->get() is allowed to fail, in which case
76 * the runtime-pm wakeref will be released and the acquisition unwound,
77 * and an error reported.
78 *
79 * Returns: 0 if the wakeref was acquired successfully, or a negative error
80 * code otherwise.
81 */
82 static inline int
intel_wakeref_get(struct intel_wakeref * wf)83 intel_wakeref_get(struct intel_wakeref *wf)
84 {
85 might_sleep();
86 if (unlikely(!atomic_inc_not_zero(&wf->count)))
87 return __intel_wakeref_get_first(wf);
88
89 return 0;
90 }
91
92 /**
93 * __intel_wakeref_get: Acquire the wakeref, again
94 * @wf: the wakeref
95 *
96 * Increment the wakeref counter, only valid if it is already held by
97 * the caller.
98 *
99 * See intel_wakeref_get().
100 */
101 static inline void
__intel_wakeref_get(struct intel_wakeref * wf)102 __intel_wakeref_get(struct intel_wakeref *wf)
103 {
104 INTEL_WAKEREF_BUG_ON(atomic_read(&wf->count) <= 0);
105 atomic_inc(&wf->count);
106 }
107
108 /**
109 * intel_wakeref_get_if_active: Acquire the wakeref
110 * @wf: the wakeref
111 *
112 * Acquire a hold on the wakeref, but only if the wakeref is already
113 * active.
114 *
115 * Returns: true if the wakeref was acquired, false otherwise.
116 */
117 static inline bool
intel_wakeref_get_if_active(struct intel_wakeref * wf)118 intel_wakeref_get_if_active(struct intel_wakeref *wf)
119 {
120 return atomic_inc_not_zero(&wf->count);
121 }
122
123 enum {
124 INTEL_WAKEREF_PUT_ASYNC_BIT = 0,
125 __INTEL_WAKEREF_PUT_LAST_BIT__
126 };
127
128 static inline void
intel_wakeref_might_get(struct intel_wakeref * wf)129 intel_wakeref_might_get(struct intel_wakeref *wf)
130 {
131 might_lock(&wf->mutex);
132 }
133
134 /**
135 * __intel_wakeref_put: Release the wakeref
136 * @wf: the wakeref
137 * @flags: control flags
138 *
139 * Release our hold on the wakeref. When there are no more users,
140 * the runtime pm wakeref will be released after the intel_wakeref_ops->put()
141 * callback is called underneath the wakeref mutex.
142 *
143 * Note that intel_wakeref_ops->put() is allowed to fail, in which case the
144 * runtime-pm wakeref is retained.
145 *
146 */
147 static inline void
__intel_wakeref_put(struct intel_wakeref * wf,unsigned long flags)148 __intel_wakeref_put(struct intel_wakeref *wf, unsigned long flags)
149 #define INTEL_WAKEREF_PUT_ASYNC BIT(INTEL_WAKEREF_PUT_ASYNC_BIT)
150 #define INTEL_WAKEREF_PUT_DELAY \
151 GENMASK(BITS_PER_LONG - 1, __INTEL_WAKEREF_PUT_LAST_BIT__)
152 {
153 INTEL_WAKEREF_BUG_ON(atomic_read(&wf->count) <= 0);
154 if (unlikely(!atomic_add_unless(&wf->count, -1, 1)))
155 __intel_wakeref_put_last(wf, flags);
156 }
157
158 static inline void
intel_wakeref_put(struct intel_wakeref * wf)159 intel_wakeref_put(struct intel_wakeref *wf)
160 {
161 might_sleep();
162 __intel_wakeref_put(wf, 0);
163 }
164
165 static inline void
intel_wakeref_put_async(struct intel_wakeref * wf)166 intel_wakeref_put_async(struct intel_wakeref *wf)
167 {
168 __intel_wakeref_put(wf, INTEL_WAKEREF_PUT_ASYNC);
169 }
170
171 static inline void
intel_wakeref_put_delay(struct intel_wakeref * wf,unsigned long delay)172 intel_wakeref_put_delay(struct intel_wakeref *wf, unsigned long delay)
173 {
174 __intel_wakeref_put(wf,
175 INTEL_WAKEREF_PUT_ASYNC |
176 FIELD_PREP(INTEL_WAKEREF_PUT_DELAY, delay));
177 }
178
179 static inline void
intel_wakeref_might_put(struct intel_wakeref * wf)180 intel_wakeref_might_put(struct intel_wakeref *wf)
181 {
182 might_lock(&wf->mutex);
183 }
184
185 /**
186 * intel_wakeref_lock: Lock the wakeref (mutex)
187 * @wf: the wakeref
188 *
189 * Locks the wakeref to prevent it being acquired or released. New users
190 * can still adjust the counter, but the wakeref itself (and callback)
191 * cannot be acquired or released.
192 */
193 static inline void
intel_wakeref_lock(struct intel_wakeref * wf)194 intel_wakeref_lock(struct intel_wakeref *wf)
195 __acquires(wf->mutex)
196 {
197 mutex_lock(&wf->mutex);
198 }
199
200 /**
201 * intel_wakeref_unlock: Unlock the wakeref
202 * @wf: the wakeref
203 *
204 * Releases a previously acquired intel_wakeref_lock().
205 */
206 static inline void
intel_wakeref_unlock(struct intel_wakeref * wf)207 intel_wakeref_unlock(struct intel_wakeref *wf)
208 __releases(wf->mutex)
209 {
210 mutex_unlock(&wf->mutex);
211 }
212
213 /**
214 * intel_wakeref_unlock_wait: Wait until the active callback is complete
215 * @wf: the wakeref
216 *
217 * Waits for the active callback (under the @wf->mutex or another CPU) is
218 * complete.
219 */
220 static inline void
intel_wakeref_unlock_wait(struct intel_wakeref * wf)221 intel_wakeref_unlock_wait(struct intel_wakeref *wf)
222 {
223 mutex_lock(&wf->mutex);
224 mutex_unlock(&wf->mutex);
225 flush_delayed_work(&wf->work);
226 }
227
228 /**
229 * intel_wakeref_is_active: Query whether the wakeref is currently held
230 * @wf: the wakeref
231 *
232 * Returns: true if the wakeref is currently held.
233 */
234 static inline bool
intel_wakeref_is_active(const struct intel_wakeref * wf)235 intel_wakeref_is_active(const struct intel_wakeref *wf)
236 {
237 return READ_ONCE(wf->wakeref);
238 }
239
240 /**
241 * __intel_wakeref_defer_park: Defer the current park callback
242 * @wf: the wakeref
243 */
244 static inline void
__intel_wakeref_defer_park(struct intel_wakeref * wf)245 __intel_wakeref_defer_park(struct intel_wakeref *wf)
246 {
247 lockdep_assert_held(&wf->mutex);
248 INTEL_WAKEREF_BUG_ON(atomic_read(&wf->count));
249 atomic_set_release(&wf->count, 1);
250 }
251
252 /**
253 * intel_wakeref_wait_for_idle: Wait until the wakeref is idle
254 * @wf: the wakeref
255 *
256 * Wait for the earlier asynchronous release of the wakeref. Note
257 * this will wait for any third party as well, so make sure you only wait
258 * when you have control over the wakeref and trust no one else is acquiring
259 * it.
260 *
261 * Return: 0 on success, error code if killed.
262 */
263 int intel_wakeref_wait_for_idle(struct intel_wakeref *wf);
264
265 struct intel_wakeref_auto {
266 struct drm_i915_private *i915;
267 struct timeout timer;
268 intel_wakeref_t wakeref;
269 spinlock_t lock;
270 refcount_t count;
271 };
272
273 /**
274 * intel_wakeref_auto: Delay the runtime-pm autosuspend
275 * @wf: the wakeref
276 * @timeout: relative timeout in jiffies
277 *
278 * The runtime-pm core uses a suspend delay after the last wakeref
279 * is released before triggering runtime suspend of the device. That
280 * delay is configurable via sysfs with little regard to the device
281 * characteristics. Instead, we want to tune the autosuspend based on our
282 * HW knowledge. intel_wakeref_auto() delays the sleep by the supplied
283 * timeout.
284 *
285 * Pass @timeout = 0 to cancel a previous autosuspend by executing the
286 * suspend immediately.
287 */
288 void intel_wakeref_auto(struct intel_wakeref_auto *wf, unsigned long timeout);
289
290 void intel_wakeref_auto_init(struct intel_wakeref_auto *wf,
291 struct drm_i915_private *i915);
292 void intel_wakeref_auto_fini(struct intel_wakeref_auto *wf);
293
294 #endif /* INTEL_WAKEREF_H */
295