1
2
3
4
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
26struct intel_runtime_pm;
27struct intel_wakeref;
28
29typedef depot_stack_handle_t intel_wakeref_t;
30
31struct intel_wakeref_ops {
32 int (*get)(struct intel_wakeref *wf);
33 int (*put)(struct intel_wakeref *wf);
34};
35
36struct intel_wakeref {
37 atomic_t count;
38 struct mutex mutex;
39
40 intel_wakeref_t wakeref;
41
42 struct intel_runtime_pm *rpm;
43 const struct intel_wakeref_ops *ops;
44
45 struct delayed_work work;
46};
47
48struct intel_wakeref_lockclass {
49 struct lock_class_key mutex;
50 struct lock_class_key work;
51};
52
53void __intel_wakeref_init(struct intel_wakeref *wf,
54 struct intel_runtime_pm *rpm,
55 const struct intel_wakeref_ops *ops,
56 struct intel_wakeref_lockclass *key);
57#define intel_wakeref_init(wf, rpm, ops) do { \
58 static struct intel_wakeref_lockclass __key; \
59 \
60 __intel_wakeref_init((wf), (rpm), (ops), &__key); \
61} while (0)
62
63int __intel_wakeref_get_first(struct intel_wakeref *wf);
64void __intel_wakeref_put_last(struct intel_wakeref *wf, unsigned long flags);
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80static inline int
81intel_wakeref_get(struct intel_wakeref *wf)
82{
83 might_sleep();
84 if (unlikely(!atomic_inc_not_zero(&wf->count)))
85 return __intel_wakeref_get_first(wf);
86
87 return 0;
88}
89
90
91
92
93
94
95
96
97
98
99static inline void
100__intel_wakeref_get(struct intel_wakeref *wf)
101{
102 INTEL_WAKEREF_BUG_ON(atomic_read(&wf->count) <= 0);
103 atomic_inc(&wf->count);
104}
105
106
107
108
109
110
111
112
113
114
115static inline bool
116intel_wakeref_get_if_active(struct intel_wakeref *wf)
117{
118 return atomic_inc_not_zero(&wf->count);
119}
120
121enum {
122 INTEL_WAKEREF_PUT_ASYNC_BIT = 0,
123 __INTEL_WAKEREF_PUT_LAST_BIT__
124};
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141static inline void
142__intel_wakeref_put(struct intel_wakeref *wf, unsigned long flags)
143#define INTEL_WAKEREF_PUT_ASYNC BIT(INTEL_WAKEREF_PUT_ASYNC_BIT)
144#define INTEL_WAKEREF_PUT_DELAY \
145 GENMASK(BITS_PER_LONG - 1, __INTEL_WAKEREF_PUT_LAST_BIT__)
146{
147 INTEL_WAKEREF_BUG_ON(atomic_read(&wf->count) <= 0);
148 if (unlikely(!atomic_add_unless(&wf->count, -1, 1)))
149 __intel_wakeref_put_last(wf, flags);
150}
151
152static inline void
153intel_wakeref_put(struct intel_wakeref *wf)
154{
155 might_sleep();
156 __intel_wakeref_put(wf, 0);
157}
158
159static inline void
160intel_wakeref_put_async(struct intel_wakeref *wf)
161{
162 __intel_wakeref_put(wf, INTEL_WAKEREF_PUT_ASYNC);
163}
164
165static inline void
166intel_wakeref_put_delay(struct intel_wakeref *wf, unsigned long delay)
167{
168 __intel_wakeref_put(wf,
169 INTEL_WAKEREF_PUT_ASYNC |
170 FIELD_PREP(INTEL_WAKEREF_PUT_DELAY, delay));
171}
172
173
174
175
176
177
178
179
180
181static inline void
182intel_wakeref_lock(struct intel_wakeref *wf)
183 __acquires(wf->mutex)
184{
185 mutex_lock(&wf->mutex);
186}
187
188
189
190
191
192
193
194static inline void
195intel_wakeref_unlock(struct intel_wakeref *wf)
196 __releases(wf->mutex)
197{
198 mutex_unlock(&wf->mutex);
199}
200
201
202
203
204
205
206
207
208static inline void
209intel_wakeref_unlock_wait(struct intel_wakeref *wf)
210{
211 mutex_lock(&wf->mutex);
212 mutex_unlock(&wf->mutex);
213 flush_delayed_work(&wf->work);
214}
215
216
217
218
219
220
221
222static inline bool
223intel_wakeref_is_active(const struct intel_wakeref *wf)
224{
225 return READ_ONCE(wf->wakeref);
226}
227
228
229
230
231
232static inline void
233__intel_wakeref_defer_park(struct intel_wakeref *wf)
234{
235 lockdep_assert_held(&wf->mutex);
236 INTEL_WAKEREF_BUG_ON(atomic_read(&wf->count));
237 atomic_set_release(&wf->count, 1);
238}
239
240
241
242
243
244
245
246
247
248
249
250
251int intel_wakeref_wait_for_idle(struct intel_wakeref *wf);
252
253struct intel_wakeref_auto {
254 struct intel_runtime_pm *rpm;
255 struct timer_list timer;
256 intel_wakeref_t wakeref;
257 spinlock_t lock;
258 refcount_t count;
259};
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276void intel_wakeref_auto(struct intel_wakeref_auto *wf, unsigned long timeout);
277
278void intel_wakeref_auto_init(struct intel_wakeref_auto *wf,
279 struct intel_runtime_pm *rpm);
280void intel_wakeref_auto_fini(struct intel_wakeref_auto *wf);
281
282#endif
283