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