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