linux_dma_fence.c revision 1.14 1 1.14 riastrad /* $NetBSD: linux_dma_fence.c,v 1.14 2021/12/19 11:09:09 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * This code is derived from software contributed to The NetBSD Foundation
8 1.1 riastrad * by Taylor R. Campbell.
9 1.1 riastrad *
10 1.1 riastrad * Redistribution and use in source and binary forms, with or without
11 1.1 riastrad * modification, are permitted provided that the following conditions
12 1.1 riastrad * are met:
13 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
14 1.1 riastrad * notice, this list of conditions and the following disclaimer.
15 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
17 1.1 riastrad * documentation and/or other materials provided with the distribution.
18 1.1 riastrad *
19 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
30 1.1 riastrad */
31 1.1 riastrad
32 1.1 riastrad #include <sys/cdefs.h>
33 1.14 riastrad __KERNEL_RCSID(0, "$NetBSD: linux_dma_fence.c,v 1.14 2021/12/19 11:09:09 riastradh Exp $");
34 1.1 riastrad
35 1.1 riastrad #include <sys/atomic.h>
36 1.1 riastrad #include <sys/condvar.h>
37 1.1 riastrad #include <sys/queue.h>
38 1.1 riastrad
39 1.1 riastrad #include <linux/atomic.h>
40 1.2 riastrad #include <linux/dma-fence.h>
41 1.1 riastrad #include <linux/errno.h>
42 1.1 riastrad #include <linux/kref.h>
43 1.1 riastrad #include <linux/sched.h>
44 1.1 riastrad #include <linux/spinlock.h>
45 1.1 riastrad
46 1.1 riastrad /*
47 1.2 riastrad * linux_dma_fence_trace
48 1.1 riastrad *
49 1.2 riastrad * True if we print DMA_FENCE_TRACE messages, false if not. These
50 1.2 riastrad * are extremely noisy, too much even for AB_VERBOSE and AB_DEBUG
51 1.2 riastrad * in boothowto.
52 1.1 riastrad */
53 1.2 riastrad int linux_dma_fence_trace = 0;
54 1.1 riastrad
55 1.1 riastrad /*
56 1.2 riastrad * dma_fence_referenced_p(fence)
57 1.1 riastrad *
58 1.1 riastrad * True if fence has a positive reference count. True after
59 1.2 riastrad * dma_fence_init; after the last dma_fence_put, this becomes
60 1.2 riastrad * false.
61 1.1 riastrad */
62 1.1 riastrad static inline bool __diagused
63 1.2 riastrad dma_fence_referenced_p(struct dma_fence *fence)
64 1.1 riastrad {
65 1.1 riastrad
66 1.1 riastrad return kref_referenced_p(&fence->refcount);
67 1.1 riastrad }
68 1.1 riastrad
69 1.1 riastrad /*
70 1.2 riastrad * dma_fence_init(fence, ops, lock, context, seqno)
71 1.1 riastrad *
72 1.2 riastrad * Initialize fence. Caller should call dma_fence_destroy when
73 1.2 riastrad * done, after all references have been released.
74 1.1 riastrad */
75 1.1 riastrad void
76 1.2 riastrad dma_fence_init(struct dma_fence *fence, const struct dma_fence_ops *ops,
77 1.2 riastrad spinlock_t *lock, unsigned context, unsigned seqno)
78 1.1 riastrad {
79 1.1 riastrad
80 1.1 riastrad kref_init(&fence->refcount);
81 1.1 riastrad fence->lock = lock;
82 1.1 riastrad fence->flags = 0;
83 1.1 riastrad fence->context = context;
84 1.1 riastrad fence->seqno = seqno;
85 1.1 riastrad fence->ops = ops;
86 1.1 riastrad TAILQ_INIT(&fence->f_callbacks);
87 1.2 riastrad cv_init(&fence->f_cv, "dmafence");
88 1.1 riastrad }
89 1.1 riastrad
90 1.1 riastrad /*
91 1.2 riastrad * dma_fence_destroy(fence)
92 1.1 riastrad *
93 1.2 riastrad * Clean up memory initialized with dma_fence_init. This is meant
94 1.2 riastrad * to be used after a fence release callback.
95 1.1 riastrad */
96 1.1 riastrad void
97 1.2 riastrad dma_fence_destroy(struct dma_fence *fence)
98 1.1 riastrad {
99 1.1 riastrad
100 1.2 riastrad KASSERT(!dma_fence_referenced_p(fence));
101 1.1 riastrad
102 1.1 riastrad KASSERT(TAILQ_EMPTY(&fence->f_callbacks));
103 1.1 riastrad cv_destroy(&fence->f_cv);
104 1.1 riastrad }
105 1.1 riastrad
106 1.1 riastrad static void
107 1.2 riastrad dma_fence_free_cb(struct rcu_head *rcu)
108 1.1 riastrad {
109 1.2 riastrad struct dma_fence *fence = container_of(rcu, struct dma_fence, f_rcu);
110 1.1 riastrad
111 1.2 riastrad KASSERT(!dma_fence_referenced_p(fence));
112 1.1 riastrad
113 1.2 riastrad dma_fence_destroy(fence);
114 1.1 riastrad kfree(fence);
115 1.1 riastrad }
116 1.1 riastrad
117 1.1 riastrad /*
118 1.2 riastrad * dma_fence_free(fence)
119 1.1 riastrad *
120 1.1 riastrad * Schedule fence to be destroyed and then freed with kfree after
121 1.1 riastrad * any pending RCU read sections on all CPUs have completed.
122 1.1 riastrad * Caller must guarantee all references have been released. This
123 1.1 riastrad * is meant to be used after a fence release callback.
124 1.1 riastrad *
125 1.1 riastrad * NOTE: Callers assume kfree will be used. We don't even use
126 1.1 riastrad * kmalloc to allocate these -- caller is expected to allocate
127 1.2 riastrad * memory with kmalloc to be initialized with dma_fence_init.
128 1.1 riastrad */
129 1.1 riastrad void
130 1.2 riastrad dma_fence_free(struct dma_fence *fence)
131 1.1 riastrad {
132 1.1 riastrad
133 1.2 riastrad KASSERT(!dma_fence_referenced_p(fence));
134 1.1 riastrad
135 1.2 riastrad call_rcu(&fence->f_rcu, &dma_fence_free_cb);
136 1.1 riastrad }
137 1.1 riastrad
138 1.1 riastrad /*
139 1.2 riastrad * dma_fence_context_alloc(n)
140 1.1 riastrad *
141 1.1 riastrad * Return the first of a contiguous sequence of unique
142 1.1 riastrad * identifiers, at least until the system wraps around.
143 1.1 riastrad */
144 1.1 riastrad unsigned
145 1.2 riastrad dma_fence_context_alloc(unsigned n)
146 1.1 riastrad {
147 1.1 riastrad static volatile unsigned next_context = 0;
148 1.1 riastrad
149 1.1 riastrad return atomic_add_int_nv(&next_context, n) - n;
150 1.1 riastrad }
151 1.1 riastrad
152 1.1 riastrad /*
153 1.2 riastrad * dma_fence_is_later(a, b)
154 1.1 riastrad *
155 1.1 riastrad * True if the sequence number of fence a is later than the
156 1.1 riastrad * sequence number of fence b. Since sequence numbers wrap
157 1.1 riastrad * around, we define this to mean that the sequence number of
158 1.1 riastrad * fence a is no more than INT_MAX past the sequence number of
159 1.1 riastrad * fence b.
160 1.1 riastrad *
161 1.1 riastrad * The two fences must have the same context.
162 1.1 riastrad */
163 1.1 riastrad bool
164 1.2 riastrad dma_fence_is_later(struct dma_fence *a, struct dma_fence *b)
165 1.1 riastrad {
166 1.1 riastrad
167 1.1 riastrad KASSERTMSG(a->context == b->context, "incommensurate fences"
168 1.1 riastrad ": %u @ %p =/= %u @ %p", a->context, a, b->context, b);
169 1.1 riastrad
170 1.1 riastrad return a->seqno - b->seqno < INT_MAX;
171 1.1 riastrad }
172 1.1 riastrad
173 1.1 riastrad /*
174 1.9 riastrad * dma_fence_get_stub()
175 1.9 riastrad *
176 1.9 riastrad * Return a dma fence that is always already signalled.
177 1.9 riastrad */
178 1.9 riastrad struct dma_fence *
179 1.9 riastrad dma_fence_get_stub(void)
180 1.9 riastrad {
181 1.9 riastrad /*
182 1.9 riastrad * XXX This probably isn't good enough -- caller may try
183 1.9 riastrad * operations on this that require the lock, which will
184 1.9 riastrad * require us to create and destroy the lock on module
185 1.9 riastrad * load/unload.
186 1.9 riastrad */
187 1.9 riastrad static struct dma_fence fence = {
188 1.9 riastrad .refcount = {1}, /* always referenced */
189 1.9 riastrad .flags = 1u << DMA_FENCE_FLAG_SIGNALED_BIT,
190 1.9 riastrad };
191 1.9 riastrad
192 1.9 riastrad return dma_fence_get(&fence);
193 1.9 riastrad }
194 1.9 riastrad
195 1.9 riastrad /*
196 1.2 riastrad * dma_fence_get(fence)
197 1.1 riastrad *
198 1.1 riastrad * Acquire a reference to fence. The fence must not be being
199 1.1 riastrad * destroyed. Return the fence.
200 1.1 riastrad */
201 1.2 riastrad struct dma_fence *
202 1.2 riastrad dma_fence_get(struct dma_fence *fence)
203 1.1 riastrad {
204 1.1 riastrad
205 1.1 riastrad if (fence)
206 1.1 riastrad kref_get(&fence->refcount);
207 1.1 riastrad return fence;
208 1.1 riastrad }
209 1.1 riastrad
210 1.1 riastrad /*
211 1.2 riastrad * dma_fence_get_rcu(fence)
212 1.1 riastrad *
213 1.1 riastrad * Attempt to acquire a reference to a fence that may be about to
214 1.1 riastrad * be destroyed, during a read section. Return the fence on
215 1.1 riastrad * success, or NULL on failure.
216 1.1 riastrad */
217 1.2 riastrad struct dma_fence *
218 1.2 riastrad dma_fence_get_rcu(struct dma_fence *fence)
219 1.1 riastrad {
220 1.1 riastrad
221 1.8 riastrad __insn_barrier();
222 1.1 riastrad if (!kref_get_unless_zero(&fence->refcount))
223 1.1 riastrad return NULL;
224 1.1 riastrad return fence;
225 1.1 riastrad }
226 1.1 riastrad
227 1.3 riastrad /*
228 1.3 riastrad * dma_fence_get_rcu_safe(fencep)
229 1.3 riastrad *
230 1.3 riastrad * Attempt to acquire a reference to the fence *fencep, which may
231 1.3 riastrad * be about to be destroyed, during a read section. If the value
232 1.3 riastrad * of *fencep changes after we read *fencep but before we
233 1.3 riastrad * increment its reference count, retry. Return *fencep on
234 1.3 riastrad * success, or NULL on failure.
235 1.3 riastrad */
236 1.3 riastrad struct dma_fence *
237 1.7 riastrad dma_fence_get_rcu_safe(struct dma_fence *volatile const *fencep)
238 1.3 riastrad {
239 1.3 riastrad struct dma_fence *fence, *fence0;
240 1.3 riastrad
241 1.3 riastrad retry:
242 1.3 riastrad fence = *fencep;
243 1.3 riastrad
244 1.3 riastrad /* Load fence only once. */
245 1.3 riastrad __insn_barrier();
246 1.3 riastrad
247 1.3 riastrad /* If there's nothing there, give up. */
248 1.3 riastrad if (fence == NULL)
249 1.3 riastrad return NULL;
250 1.3 riastrad
251 1.3 riastrad /* Make sure we don't load stale fence guts. */
252 1.3 riastrad membar_datadep_consumer();
253 1.3 riastrad
254 1.3 riastrad /* Try to acquire a reference. If we can't, try again. */
255 1.3 riastrad if (!dma_fence_get_rcu(fence))
256 1.3 riastrad goto retry;
257 1.3 riastrad
258 1.3 riastrad /*
259 1.3 riastrad * Confirm that it's still the same fence. If not, release it
260 1.3 riastrad * and retry.
261 1.3 riastrad */
262 1.3 riastrad fence0 = *fencep;
263 1.3 riastrad __insn_barrier();
264 1.3 riastrad if (fence != fence0) {
265 1.3 riastrad dma_fence_put(fence);
266 1.3 riastrad goto retry;
267 1.3 riastrad }
268 1.3 riastrad
269 1.3 riastrad /* Success! */
270 1.3 riastrad return fence;
271 1.3 riastrad }
272 1.3 riastrad
273 1.1 riastrad static void
274 1.2 riastrad dma_fence_release(struct kref *refcount)
275 1.1 riastrad {
276 1.2 riastrad struct dma_fence *fence = container_of(refcount, struct dma_fence,
277 1.2 riastrad refcount);
278 1.1 riastrad
279 1.2 riastrad KASSERT(!dma_fence_referenced_p(fence));
280 1.1 riastrad
281 1.1 riastrad if (fence->ops->release)
282 1.1 riastrad (*fence->ops->release)(fence);
283 1.1 riastrad else
284 1.2 riastrad dma_fence_free(fence);
285 1.1 riastrad }
286 1.1 riastrad
287 1.1 riastrad /*
288 1.2 riastrad * dma_fence_put(fence)
289 1.1 riastrad *
290 1.1 riastrad * Release a reference to fence. If this was the last one, call
291 1.1 riastrad * the fence's release callback.
292 1.1 riastrad */
293 1.1 riastrad void
294 1.2 riastrad dma_fence_put(struct dma_fence *fence)
295 1.1 riastrad {
296 1.1 riastrad
297 1.1 riastrad if (fence == NULL)
298 1.1 riastrad return;
299 1.2 riastrad KASSERT(dma_fence_referenced_p(fence));
300 1.2 riastrad kref_put(&fence->refcount, &dma_fence_release);
301 1.1 riastrad }
302 1.1 riastrad
303 1.1 riastrad /*
304 1.2 riastrad * dma_fence_ensure_signal_enabled(fence)
305 1.1 riastrad *
306 1.1 riastrad * Internal subroutine. If the fence was already signalled,
307 1.1 riastrad * return -ENOENT. Otherwise, if the enable signalling callback
308 1.1 riastrad * has not been called yet, call it. If fails, signal the fence
309 1.1 riastrad * and return -ENOENT. If it succeeds, or if it had already been
310 1.1 riastrad * called, return zero to indicate success.
311 1.1 riastrad *
312 1.1 riastrad * Caller must hold the fence's lock.
313 1.1 riastrad */
314 1.1 riastrad static int
315 1.2 riastrad dma_fence_ensure_signal_enabled(struct dma_fence *fence)
316 1.1 riastrad {
317 1.1 riastrad
318 1.2 riastrad KASSERT(dma_fence_referenced_p(fence));
319 1.1 riastrad KASSERT(spin_is_locked(fence->lock));
320 1.1 riastrad
321 1.1 riastrad /* If the fence was already signalled, fail with -ENOENT. */
322 1.2 riastrad if (fence->flags & (1u << DMA_FENCE_FLAG_SIGNALED_BIT))
323 1.1 riastrad return -ENOENT;
324 1.1 riastrad
325 1.1 riastrad /*
326 1.1 riastrad * If the enable signaling callback has been called, success.
327 1.1 riastrad * Otherwise, set the bit indicating it.
328 1.1 riastrad */
329 1.2 riastrad if (test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags))
330 1.1 riastrad return 0;
331 1.1 riastrad
332 1.1 riastrad /* Otherwise, note that we've called it and call it. */
333 1.1 riastrad if (!(*fence->ops->enable_signaling)(fence)) {
334 1.1 riastrad /* If it failed, signal and return -ENOENT. */
335 1.2 riastrad dma_fence_signal_locked(fence);
336 1.1 riastrad return -ENOENT;
337 1.1 riastrad }
338 1.1 riastrad
339 1.1 riastrad /* Success! */
340 1.1 riastrad return 0;
341 1.1 riastrad }
342 1.1 riastrad
343 1.1 riastrad /*
344 1.2 riastrad * dma_fence_add_callback(fence, fcb, fn)
345 1.1 riastrad *
346 1.1 riastrad * If fence has been signalled, return -ENOENT. If the enable
347 1.1 riastrad * signalling callback hasn't been called yet, call it; if it
348 1.1 riastrad * fails, return -ENOENT. Otherwise, arrange to call fn(fence,
349 1.1 riastrad * fcb) when it is signalled, and return 0.
350 1.1 riastrad *
351 1.1 riastrad * The fence uses memory allocated by the caller in fcb from the
352 1.2 riastrad * time of dma_fence_add_callback either to the time of
353 1.2 riastrad * dma_fence_remove_callback, or just before calling fn.
354 1.1 riastrad */
355 1.1 riastrad int
356 1.2 riastrad dma_fence_add_callback(struct dma_fence *fence, struct dma_fence_cb *fcb,
357 1.2 riastrad dma_fence_func_t fn)
358 1.1 riastrad {
359 1.1 riastrad int ret;
360 1.1 riastrad
361 1.2 riastrad KASSERT(dma_fence_referenced_p(fence));
362 1.1 riastrad
363 1.1 riastrad /* Optimistically try to skip the lock if it's already signalled. */
364 1.2 riastrad if (fence->flags & (1u << DMA_FENCE_FLAG_SIGNALED_BIT)) {
365 1.1 riastrad ret = -ENOENT;
366 1.1 riastrad goto out0;
367 1.1 riastrad }
368 1.1 riastrad
369 1.1 riastrad /* Acquire the lock. */
370 1.1 riastrad spin_lock(fence->lock);
371 1.1 riastrad
372 1.1 riastrad /* Ensure signalling is enabled, or fail if we can't. */
373 1.2 riastrad ret = dma_fence_ensure_signal_enabled(fence);
374 1.1 riastrad if (ret)
375 1.1 riastrad goto out1;
376 1.1 riastrad
377 1.1 riastrad /* Insert the callback. */
378 1.4 riastrad fcb->func = fn;
379 1.1 riastrad TAILQ_INSERT_TAIL(&fence->f_callbacks, fcb, fcb_entry);
380 1.1 riastrad fcb->fcb_onqueue = true;
381 1.1 riastrad
382 1.1 riastrad /* Release the lock and we're done. */
383 1.1 riastrad out1: spin_unlock(fence->lock);
384 1.1 riastrad out0: return ret;
385 1.1 riastrad }
386 1.1 riastrad
387 1.1 riastrad /*
388 1.2 riastrad * dma_fence_remove_callback(fence, fcb)
389 1.1 riastrad *
390 1.1 riastrad * Remove the callback fcb from fence. Return true if it was
391 1.1 riastrad * removed from the list, or false if it had already run and so
392 1.1 riastrad * was no longer queued anyway. Caller must have already called
393 1.2 riastrad * dma_fence_add_callback(fence, fcb).
394 1.1 riastrad */
395 1.1 riastrad bool
396 1.2 riastrad dma_fence_remove_callback(struct dma_fence *fence, struct dma_fence_cb *fcb)
397 1.1 riastrad {
398 1.1 riastrad bool onqueue;
399 1.1 riastrad
400 1.2 riastrad KASSERT(dma_fence_referenced_p(fence));
401 1.1 riastrad
402 1.1 riastrad spin_lock(fence->lock);
403 1.1 riastrad onqueue = fcb->fcb_onqueue;
404 1.1 riastrad if (onqueue) {
405 1.1 riastrad TAILQ_REMOVE(&fence->f_callbacks, fcb, fcb_entry);
406 1.1 riastrad fcb->fcb_onqueue = false;
407 1.1 riastrad }
408 1.1 riastrad spin_unlock(fence->lock);
409 1.1 riastrad
410 1.1 riastrad return onqueue;
411 1.1 riastrad }
412 1.1 riastrad
413 1.1 riastrad /*
414 1.2 riastrad * dma_fence_enable_sw_signaling(fence)
415 1.1 riastrad *
416 1.1 riastrad * If it hasn't been called yet and the fence hasn't been
417 1.1 riastrad * signalled yet, call the fence's enable_sw_signaling callback.
418 1.1 riastrad * If when that happens, the callback indicates failure by
419 1.1 riastrad * returning false, signal the fence.
420 1.1 riastrad */
421 1.1 riastrad void
422 1.2 riastrad dma_fence_enable_sw_signaling(struct dma_fence *fence)
423 1.1 riastrad {
424 1.1 riastrad
425 1.2 riastrad KASSERT(dma_fence_referenced_p(fence));
426 1.1 riastrad
427 1.1 riastrad spin_lock(fence->lock);
428 1.2 riastrad (void)dma_fence_ensure_signal_enabled(fence);
429 1.1 riastrad spin_unlock(fence->lock);
430 1.1 riastrad }
431 1.1 riastrad
432 1.1 riastrad /*
433 1.2 riastrad * dma_fence_is_signaled(fence)
434 1.1 riastrad *
435 1.1 riastrad * Test whether the fence has been signalled. If it has been
436 1.2 riastrad * signalled by dma_fence_signal(_locked), return true. If the
437 1.1 riastrad * signalled callback returns true indicating that some implicit
438 1.1 riastrad * external condition has changed, call the callbacks as if with
439 1.2 riastrad * dma_fence_signal.
440 1.1 riastrad */
441 1.1 riastrad bool
442 1.2 riastrad dma_fence_is_signaled(struct dma_fence *fence)
443 1.1 riastrad {
444 1.1 riastrad bool signaled;
445 1.1 riastrad
446 1.2 riastrad KASSERT(dma_fence_referenced_p(fence));
447 1.1 riastrad
448 1.1 riastrad spin_lock(fence->lock);
449 1.2 riastrad signaled = dma_fence_is_signaled_locked(fence);
450 1.1 riastrad spin_unlock(fence->lock);
451 1.1 riastrad
452 1.1 riastrad return signaled;
453 1.1 riastrad }
454 1.1 riastrad
455 1.1 riastrad /*
456 1.2 riastrad * dma_fence_is_signaled_locked(fence)
457 1.1 riastrad *
458 1.1 riastrad * Test whether the fence has been signalled. Like
459 1.2 riastrad * dma_fence_is_signaleed, but caller already holds the fence's lock.
460 1.1 riastrad */
461 1.1 riastrad bool
462 1.2 riastrad dma_fence_is_signaled_locked(struct dma_fence *fence)
463 1.1 riastrad {
464 1.1 riastrad
465 1.2 riastrad KASSERT(dma_fence_referenced_p(fence));
466 1.1 riastrad KASSERT(spin_is_locked(fence->lock));
467 1.1 riastrad
468 1.1 riastrad /* Check whether we already set the signalled bit. */
469 1.2 riastrad if (fence->flags & (1u << DMA_FENCE_FLAG_SIGNALED_BIT))
470 1.1 riastrad return true;
471 1.1 riastrad
472 1.1 riastrad /* If there's a signalled callback, test it. */
473 1.1 riastrad if (fence->ops->signaled) {
474 1.1 riastrad if ((*fence->ops->signaled)(fence)) {
475 1.1 riastrad /*
476 1.1 riastrad * It's been signalled implicitly by some
477 1.1 riastrad * external phenomonen. Act as though someone
478 1.2 riastrad * has called dma_fence_signal.
479 1.1 riastrad */
480 1.2 riastrad dma_fence_signal_locked(fence);
481 1.1 riastrad return true;
482 1.1 riastrad }
483 1.1 riastrad }
484 1.1 riastrad
485 1.1 riastrad return false;
486 1.1 riastrad }
487 1.1 riastrad
488 1.1 riastrad /*
489 1.5 riastrad * dma_fence_set_error(fence, error)
490 1.5 riastrad *
491 1.5 riastrad * Set an error code prior to dma_fence_signal for use by a
492 1.5 riastrad * waiter to learn about success or failure of the fence.
493 1.5 riastrad */
494 1.5 riastrad void
495 1.5 riastrad dma_fence_set_error(struct dma_fence *fence, int error)
496 1.5 riastrad {
497 1.5 riastrad
498 1.5 riastrad KASSERT(!(fence->flags & (1u << DMA_FENCE_FLAG_SIGNALED_BIT)));
499 1.6 riastrad KASSERTMSG(error >= -ELAST, "%d", error);
500 1.5 riastrad KASSERTMSG(error < 0, "%d", error);
501 1.5 riastrad
502 1.5 riastrad fence->error = error;
503 1.5 riastrad }
504 1.5 riastrad
505 1.5 riastrad /*
506 1.10 riastrad * dma_fence_get_status(fence)
507 1.10 riastrad *
508 1.10 riastrad * Return 0 if fence has yet to be signalled, 1 if it has been
509 1.10 riastrad * signalled without error, or negative error code if
510 1.10 riastrad * dma_fence_set_error was used.
511 1.10 riastrad */
512 1.10 riastrad int
513 1.10 riastrad dma_fence_get_status(struct dma_fence *fence)
514 1.10 riastrad {
515 1.10 riastrad int ret;
516 1.10 riastrad
517 1.10 riastrad spin_lock(fence->lock);
518 1.10 riastrad if (!dma_fence_is_signaled_locked(fence)) {
519 1.10 riastrad ret = 0;
520 1.10 riastrad } else if (fence->error) {
521 1.10 riastrad ret = fence->error;
522 1.10 riastrad KASSERTMSG(ret < 0, "%d", ret);
523 1.10 riastrad } else {
524 1.10 riastrad ret = 1;
525 1.10 riastrad }
526 1.10 riastrad spin_unlock(fence->lock);
527 1.10 riastrad
528 1.10 riastrad return ret;
529 1.10 riastrad }
530 1.10 riastrad
531 1.10 riastrad /*
532 1.2 riastrad * dma_fence_signal(fence)
533 1.1 riastrad *
534 1.1 riastrad * Signal the fence. If it has already been signalled, return
535 1.1 riastrad * -EINVAL. If it has not been signalled, call the enable
536 1.1 riastrad * signalling callback if it hasn't been called yet, and remove
537 1.1 riastrad * each registered callback from the queue and call it; then
538 1.1 riastrad * return 0.
539 1.1 riastrad */
540 1.1 riastrad int
541 1.2 riastrad dma_fence_signal(struct dma_fence *fence)
542 1.1 riastrad {
543 1.1 riastrad int ret;
544 1.1 riastrad
545 1.2 riastrad KASSERT(dma_fence_referenced_p(fence));
546 1.1 riastrad
547 1.1 riastrad spin_lock(fence->lock);
548 1.2 riastrad ret = dma_fence_signal_locked(fence);
549 1.1 riastrad spin_unlock(fence->lock);
550 1.1 riastrad
551 1.1 riastrad return ret;
552 1.1 riastrad }
553 1.1 riastrad
554 1.1 riastrad /*
555 1.2 riastrad * dma_fence_signal_locked(fence)
556 1.1 riastrad *
557 1.2 riastrad * Signal the fence. Like dma_fence_signal, but caller already
558 1.2 riastrad * holds the fence's lock.
559 1.1 riastrad */
560 1.1 riastrad int
561 1.2 riastrad dma_fence_signal_locked(struct dma_fence *fence)
562 1.1 riastrad {
563 1.2 riastrad struct dma_fence_cb *fcb, *next;
564 1.1 riastrad
565 1.2 riastrad KASSERT(dma_fence_referenced_p(fence));
566 1.1 riastrad KASSERT(spin_is_locked(fence->lock));
567 1.1 riastrad
568 1.1 riastrad /* If it's been signalled, fail; otherwise set the signalled bit. */
569 1.2 riastrad if (test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
570 1.1 riastrad return -EINVAL;
571 1.1 riastrad
572 1.1 riastrad /* Wake waiters. */
573 1.1 riastrad cv_broadcast(&fence->f_cv);
574 1.1 riastrad
575 1.1 riastrad /* Remove and call the callbacks. */
576 1.1 riastrad TAILQ_FOREACH_SAFE(fcb, &fence->f_callbacks, fcb_entry, next) {
577 1.1 riastrad TAILQ_REMOVE(&fence->f_callbacks, fcb, fcb_entry);
578 1.1 riastrad fcb->fcb_onqueue = false;
579 1.4 riastrad (*fcb->func)(fence, fcb);
580 1.1 riastrad }
581 1.1 riastrad
582 1.1 riastrad /* Success! */
583 1.1 riastrad return 0;
584 1.1 riastrad }
585 1.1 riastrad
586 1.1 riastrad struct wait_any {
587 1.2 riastrad struct dma_fence_cb fcb;
588 1.1 riastrad struct wait_any1 {
589 1.1 riastrad kmutex_t lock;
590 1.1 riastrad kcondvar_t cv;
591 1.1 riastrad bool done;
592 1.11 riastrad uint32_t *ip;
593 1.11 riastrad struct wait_any *cb;
594 1.1 riastrad } *common;
595 1.1 riastrad };
596 1.1 riastrad
597 1.1 riastrad static void
598 1.2 riastrad wait_any_cb(struct dma_fence *fence, struct dma_fence_cb *fcb)
599 1.1 riastrad {
600 1.1 riastrad struct wait_any *cb = container_of(fcb, struct wait_any, fcb);
601 1.1 riastrad
602 1.2 riastrad KASSERT(dma_fence_referenced_p(fence));
603 1.1 riastrad
604 1.1 riastrad mutex_enter(&cb->common->lock);
605 1.1 riastrad cb->common->done = true;
606 1.11 riastrad if (cb->common->ip)
607 1.11 riastrad *cb->common->ip = cb - cb->common->cb;
608 1.1 riastrad cv_broadcast(&cb->common->cv);
609 1.1 riastrad mutex_exit(&cb->common->lock);
610 1.1 riastrad }
611 1.1 riastrad
612 1.1 riastrad /*
613 1.11 riastrad * dma_fence_wait_any_timeout(fence, nfences, intr, timeout, ip)
614 1.1 riastrad *
615 1.1 riastrad * Wait for any of fences[0], fences[1], fences[2], ...,
616 1.13 riastrad * fences[nfences-1] to be signalled. If ip is nonnull, set *ip
617 1.13 riastrad * to the index of the first one.
618 1.1 riastrad */
619 1.1 riastrad long
620 1.2 riastrad dma_fence_wait_any_timeout(struct dma_fence **fences, uint32_t nfences,
621 1.11 riastrad bool intr, long timeout, uint32_t *ip)
622 1.1 riastrad {
623 1.1 riastrad struct wait_any1 common;
624 1.1 riastrad struct wait_any *cb;
625 1.1 riastrad uint32_t i, j;
626 1.1 riastrad int start, end;
627 1.1 riastrad long ret = 0;
628 1.1 riastrad
629 1.1 riastrad /* Allocate an array of callback records. */
630 1.1 riastrad cb = kcalloc(nfences, sizeof(cb[0]), GFP_KERNEL);
631 1.1 riastrad if (cb == NULL) {
632 1.1 riastrad ret = -ENOMEM;
633 1.1 riastrad goto out0;
634 1.1 riastrad }
635 1.1 riastrad
636 1.1 riastrad /* Initialize a mutex and condvar for the common wait. */
637 1.1 riastrad mutex_init(&common.lock, MUTEX_DEFAULT, IPL_VM);
638 1.1 riastrad cv_init(&common.cv, "fence");
639 1.1 riastrad common.done = false;
640 1.11 riastrad common.ip = ip;
641 1.11 riastrad common.cb = cb;
642 1.1 riastrad
643 1.1 riastrad /* Add a callback to each of the fences, or stop here if we can't. */
644 1.1 riastrad for (i = 0; i < nfences; i++) {
645 1.1 riastrad cb[i].common = &common;
646 1.2 riastrad KASSERT(dma_fence_referenced_p(fences[i]));
647 1.2 riastrad ret = dma_fence_add_callback(fences[i], &cb[i].fcb,
648 1.2 riastrad &wait_any_cb);
649 1.1 riastrad if (ret)
650 1.1 riastrad goto out1;
651 1.1 riastrad }
652 1.1 riastrad
653 1.1 riastrad /*
654 1.1 riastrad * Test whether any of the fences has been signalled. If they
655 1.1 riastrad * have, stop here. If the haven't, we are guaranteed to be
656 1.1 riastrad * notified by one of the callbacks when they have.
657 1.1 riastrad */
658 1.1 riastrad for (j = 0; j < nfences; j++) {
659 1.11 riastrad if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fences[j]->flags)) {
660 1.11 riastrad if (ip)
661 1.11 riastrad *ip = j;
662 1.11 riastrad ret = 0;
663 1.1 riastrad goto out1;
664 1.11 riastrad }
665 1.1 riastrad }
666 1.1 riastrad
667 1.1 riastrad /*
668 1.1 riastrad * None of them was ready immediately. Wait for one of the
669 1.1 riastrad * callbacks to notify us when it is done.
670 1.1 riastrad */
671 1.1 riastrad mutex_enter(&common.lock);
672 1.1 riastrad while (timeout > 0 && !common.done) {
673 1.1 riastrad start = getticks();
674 1.1 riastrad __insn_barrier();
675 1.1 riastrad if (intr) {
676 1.1 riastrad if (timeout != MAX_SCHEDULE_TIMEOUT) {
677 1.1 riastrad ret = -cv_timedwait_sig(&common.cv,
678 1.1 riastrad &common.lock, MIN(timeout, /* paranoia */
679 1.1 riastrad MAX_SCHEDULE_TIMEOUT));
680 1.1 riastrad } else {
681 1.1 riastrad ret = -cv_wait_sig(&common.cv, &common.lock);
682 1.1 riastrad }
683 1.1 riastrad } else {
684 1.1 riastrad if (timeout != MAX_SCHEDULE_TIMEOUT) {
685 1.1 riastrad ret = -cv_timedwait(&common.cv,
686 1.1 riastrad &common.lock, MIN(timeout, /* paranoia */
687 1.1 riastrad MAX_SCHEDULE_TIMEOUT));
688 1.1 riastrad } else {
689 1.1 riastrad cv_wait(&common.cv, &common.lock);
690 1.1 riastrad ret = 0;
691 1.1 riastrad }
692 1.1 riastrad }
693 1.1 riastrad end = getticks();
694 1.1 riastrad __insn_barrier();
695 1.1 riastrad if (ret) {
696 1.1 riastrad if (ret == -ERESTART)
697 1.1 riastrad ret = -ERESTARTSYS;
698 1.1 riastrad break;
699 1.1 riastrad }
700 1.1 riastrad timeout -= MIN(timeout, (unsigned)end - (unsigned)start);
701 1.1 riastrad }
702 1.1 riastrad mutex_exit(&common.lock);
703 1.1 riastrad
704 1.1 riastrad /*
705 1.1 riastrad * Massage the return code: if we were interrupted, return
706 1.1 riastrad * ERESTARTSYS; if cv_timedwait timed out, return 0; otherwise
707 1.1 riastrad * return the remaining time.
708 1.1 riastrad */
709 1.1 riastrad if (ret < 0) {
710 1.1 riastrad if (ret == -EINTR || ret == -ERESTART)
711 1.1 riastrad ret = -ERESTARTSYS;
712 1.1 riastrad if (ret == -EWOULDBLOCK)
713 1.1 riastrad ret = 0;
714 1.1 riastrad } else {
715 1.1 riastrad KASSERT(ret == 0);
716 1.1 riastrad ret = timeout;
717 1.1 riastrad }
718 1.1 riastrad
719 1.1 riastrad out1: while (i --> 0)
720 1.2 riastrad (void)dma_fence_remove_callback(fences[i], &cb[i].fcb);
721 1.1 riastrad cv_destroy(&common.cv);
722 1.1 riastrad mutex_destroy(&common.lock);
723 1.1 riastrad kfree(cb);
724 1.1 riastrad out0: return ret;
725 1.1 riastrad }
726 1.1 riastrad
727 1.1 riastrad /*
728 1.2 riastrad * dma_fence_wait_timeout(fence, intr, timeout)
729 1.1 riastrad *
730 1.1 riastrad * Wait until fence is signalled; or until interrupt, if intr is
731 1.1 riastrad * true; or until timeout, if positive. Return -ERESTARTSYS if
732 1.1 riastrad * interrupted, negative error code on any other error, zero on
733 1.1 riastrad * timeout, or positive number of ticks remaining if the fence is
734 1.1 riastrad * signalled before the timeout. Works by calling the fence wait
735 1.1 riastrad * callback.
736 1.1 riastrad *
737 1.1 riastrad * The timeout must be nonnegative and less than
738 1.1 riastrad * MAX_SCHEDULE_TIMEOUT.
739 1.1 riastrad */
740 1.1 riastrad long
741 1.2 riastrad dma_fence_wait_timeout(struct dma_fence *fence, bool intr, long timeout)
742 1.1 riastrad {
743 1.1 riastrad
744 1.2 riastrad KASSERT(dma_fence_referenced_p(fence));
745 1.1 riastrad KASSERT(timeout >= 0);
746 1.1 riastrad KASSERT(timeout < MAX_SCHEDULE_TIMEOUT);
747 1.1 riastrad
748 1.14 riastrad if (fence->ops->wait)
749 1.14 riastrad return (*fence->ops->wait)(fence, intr, timeout);
750 1.14 riastrad else
751 1.14 riastrad return dma_fence_default_wait(fence, intr, timeout);
752 1.1 riastrad }
753 1.1 riastrad
754 1.1 riastrad /*
755 1.2 riastrad * dma_fence_wait(fence, intr)
756 1.1 riastrad *
757 1.1 riastrad * Wait until fence is signalled; or until interrupt, if intr is
758 1.1 riastrad * true. Return -ERESTARTSYS if interrupted, negative error code
759 1.1 riastrad * on any other error, zero on sucess. Works by calling the fence
760 1.1 riastrad * wait callback with MAX_SCHEDULE_TIMEOUT.
761 1.1 riastrad */
762 1.1 riastrad long
763 1.2 riastrad dma_fence_wait(struct dma_fence *fence, bool intr)
764 1.1 riastrad {
765 1.1 riastrad long ret;
766 1.1 riastrad
767 1.2 riastrad KASSERT(dma_fence_referenced_p(fence));
768 1.1 riastrad
769 1.1 riastrad ret = (*fence->ops->wait)(fence, intr, MAX_SCHEDULE_TIMEOUT);
770 1.1 riastrad KASSERT(ret != 0);
771 1.1 riastrad
772 1.1 riastrad return (ret < 0 ? ret : 0);
773 1.1 riastrad }
774 1.1 riastrad
775 1.1 riastrad /*
776 1.2 riastrad * dma_fence_default_wait(fence, intr, timeout)
777 1.1 riastrad *
778 1.1 riastrad * Default implementation of fence wait callback using a condition
779 1.1 riastrad * variable. If the fence is already signalled, return timeout,
780 1.1 riastrad * or 1 if no timeout. If the enable signalling callback hasn't
781 1.1 riastrad * been called, call it, and if it fails, act as if the fence had
782 1.1 riastrad * been signalled. Otherwise, wait on the internal condvar. If
783 1.1 riastrad * timeout is MAX_SCHEDULE_TIMEOUT, treat it as no timeout.
784 1.1 riastrad */
785 1.1 riastrad long
786 1.2 riastrad dma_fence_default_wait(struct dma_fence *fence, bool intr, long timeout)
787 1.1 riastrad {
788 1.1 riastrad int starttime = 0, now = 0, deadline = 0; /* XXXGCC */
789 1.1 riastrad kmutex_t *lock = &fence->lock->sl_lock;
790 1.1 riastrad long ret = 0;
791 1.1 riastrad
792 1.2 riastrad KASSERT(dma_fence_referenced_p(fence));
793 1.1 riastrad KASSERTMSG(timeout >= 0, "timeout %ld", timeout);
794 1.1 riastrad KASSERTMSG(timeout <= MAX_SCHEDULE_TIMEOUT, "timeout %ld", timeout);
795 1.1 riastrad
796 1.1 riastrad /* Optimistically try to skip the lock if it's already signalled. */
797 1.2 riastrad if (fence->flags & (1u << DMA_FENCE_FLAG_SIGNALED_BIT))
798 1.1 riastrad return (timeout < MAX_SCHEDULE_TIMEOUT ? timeout : 1);
799 1.1 riastrad
800 1.1 riastrad /* Acquire the lock. */
801 1.1 riastrad spin_lock(fence->lock);
802 1.1 riastrad
803 1.1 riastrad /* Ensure signalling is enabled, or fail if we can't. */
804 1.2 riastrad ret = dma_fence_ensure_signal_enabled(fence);
805 1.1 riastrad if (ret)
806 1.1 riastrad goto out;
807 1.1 riastrad
808 1.1 riastrad /* Find out what our deadline is so we can handle spurious wakeup. */
809 1.1 riastrad if (timeout < MAX_SCHEDULE_TIMEOUT) {
810 1.1 riastrad now = getticks();
811 1.1 riastrad __insn_barrier();
812 1.1 riastrad starttime = now;
813 1.1 riastrad deadline = starttime + timeout;
814 1.1 riastrad }
815 1.1 riastrad
816 1.1 riastrad /* Wait until the signalled bit is set. */
817 1.2 riastrad while (!(fence->flags & (1u << DMA_FENCE_FLAG_SIGNALED_BIT))) {
818 1.1 riastrad /*
819 1.1 riastrad * If there's a timeout and we've passed the deadline,
820 1.1 riastrad * give up.
821 1.1 riastrad */
822 1.1 riastrad if (timeout < MAX_SCHEDULE_TIMEOUT) {
823 1.1 riastrad now = getticks();
824 1.1 riastrad __insn_barrier();
825 1.1 riastrad if (deadline <= now)
826 1.1 riastrad break;
827 1.1 riastrad }
828 1.1 riastrad if (intr) {
829 1.1 riastrad if (timeout < MAX_SCHEDULE_TIMEOUT) {
830 1.1 riastrad ret = -cv_timedwait_sig(&fence->f_cv, lock,
831 1.1 riastrad deadline - now);
832 1.1 riastrad } else {
833 1.1 riastrad ret = -cv_wait_sig(&fence->f_cv, lock);
834 1.1 riastrad }
835 1.1 riastrad } else {
836 1.1 riastrad if (timeout < MAX_SCHEDULE_TIMEOUT) {
837 1.1 riastrad ret = -cv_timedwait(&fence->f_cv, lock,
838 1.1 riastrad deadline - now);
839 1.1 riastrad } else {
840 1.1 riastrad cv_wait(&fence->f_cv, lock);
841 1.1 riastrad ret = 0;
842 1.1 riastrad }
843 1.1 riastrad }
844 1.1 riastrad /* If the wait failed, give up. */
845 1.1 riastrad if (ret) {
846 1.1 riastrad if (ret == -ERESTART)
847 1.1 riastrad ret = -ERESTARTSYS;
848 1.1 riastrad break;
849 1.1 riastrad }
850 1.1 riastrad }
851 1.1 riastrad
852 1.1 riastrad out:
853 1.1 riastrad /* All done. Release the lock. */
854 1.1 riastrad spin_unlock(fence->lock);
855 1.1 riastrad
856 1.1 riastrad /* If cv_timedwait gave up, return 0 meaning timeout. */
857 1.1 riastrad if (ret == -EWOULDBLOCK) {
858 1.1 riastrad /* Only cv_timedwait and cv_timedwait_sig can return this. */
859 1.1 riastrad KASSERT(timeout < MAX_SCHEDULE_TIMEOUT);
860 1.1 riastrad return 0;
861 1.1 riastrad }
862 1.1 riastrad
863 1.1 riastrad /* If there was a timeout and the deadline passed, return 0. */
864 1.1 riastrad if (timeout < MAX_SCHEDULE_TIMEOUT) {
865 1.1 riastrad if (deadline <= now)
866 1.1 riastrad return 0;
867 1.1 riastrad }
868 1.1 riastrad
869 1.1 riastrad /* If we were interrupted, return -ERESTARTSYS. */
870 1.1 riastrad if (ret == -EINTR || ret == -ERESTART)
871 1.1 riastrad return -ERESTARTSYS;
872 1.1 riastrad
873 1.1 riastrad /* If there was any other kind of error, fail. */
874 1.1 riastrad if (ret)
875 1.1 riastrad return ret;
876 1.1 riastrad
877 1.1 riastrad /*
878 1.1 riastrad * Success! Return the number of ticks left, at least 1, or 1
879 1.1 riastrad * if no timeout.
880 1.1 riastrad */
881 1.1 riastrad return (timeout < MAX_SCHEDULE_TIMEOUT ? MIN(deadline - now, 1) : 1);
882 1.1 riastrad }
883 1.12 riastrad
884 1.12 riastrad /*
885 1.12 riastrad * __dma_fence_signal(fence)
886 1.12 riastrad *
887 1.12 riastrad * Set fence's signalled bit, without waking waiters yet. Return
888 1.12 riastrad * true if it was newly set, false if it was already set.
889 1.12 riastrad */
890 1.12 riastrad bool
891 1.12 riastrad __dma_fence_signal(struct dma_fence *fence)
892 1.12 riastrad {
893 1.12 riastrad
894 1.12 riastrad if (test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
895 1.12 riastrad return false;
896 1.12 riastrad
897 1.12 riastrad return true;
898 1.12 riastrad }
899 1.12 riastrad
900 1.12 riastrad /*
901 1.12 riastrad * __dma_fence_signal_wake(fence)
902 1.12 riastrad *
903 1.12 riastrad * Wake fence's waiters. Caller must have previously called
904 1.12 riastrad * __dma_fence_signal and it must have previously returned true.
905 1.12 riastrad */
906 1.12 riastrad void
907 1.12 riastrad __dma_fence_signal_wake(struct dma_fence *fence, ktime_t timestamp)
908 1.12 riastrad {
909 1.12 riastrad struct dma_fence_cb *fcb, *next;
910 1.12 riastrad
911 1.12 riastrad spin_lock(fence->lock);
912 1.12 riastrad
913 1.12 riastrad KASSERT(fence->flags & DMA_FENCE_FLAG_SIGNALED_BIT);
914 1.12 riastrad
915 1.12 riastrad /* Wake waiters. */
916 1.12 riastrad cv_broadcast(&fence->f_cv);
917 1.12 riastrad
918 1.12 riastrad /* Remove and call the callbacks. */
919 1.12 riastrad TAILQ_FOREACH_SAFE(fcb, &fence->f_callbacks, fcb_entry, next) {
920 1.12 riastrad TAILQ_REMOVE(&fence->f_callbacks, fcb, fcb_entry);
921 1.12 riastrad fcb->fcb_onqueue = false;
922 1.12 riastrad (*fcb->func)(fence, fcb);
923 1.12 riastrad }
924 1.12 riastrad
925 1.12 riastrad spin_unlock(fence->lock);
926 1.12 riastrad }
927