]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - drivers/dma-buf/dma-fence.c
dma-fence: Make ->wait callback optional
[mirror_ubuntu-hirsute-kernel.git] / drivers / dma-buf / dma-fence.c
1 /*
2 * Fence mechanism for dma-buf and to allow for asynchronous dma access
3 *
4 * Copyright (C) 2012 Canonical Ltd
5 * Copyright (C) 2012 Texas Instruments
6 *
7 * Authors:
8 * Rob Clark <robdclark@gmail.com>
9 * Maarten Lankhorst <maarten.lankhorst@canonical.com>
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 2 as published by
13 * the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * more details.
19 */
20
21 #include <linux/slab.h>
22 #include <linux/export.h>
23 #include <linux/atomic.h>
24 #include <linux/dma-fence.h>
25 #include <linux/sched/signal.h>
26
27 #define CREATE_TRACE_POINTS
28 #include <trace/events/dma_fence.h>
29
30 EXPORT_TRACEPOINT_SYMBOL(dma_fence_emit);
31 EXPORT_TRACEPOINT_SYMBOL(dma_fence_enable_signal);
32
33 /*
34 * fence context counter: each execution context should have its own
35 * fence context, this allows checking if fences belong to the same
36 * context or not. One device can have multiple separate contexts,
37 * and they're used if some engine can run independently of another.
38 */
39 static atomic64_t dma_fence_context_counter = ATOMIC64_INIT(0);
40
41 /**
42 * dma_fence_context_alloc - allocate an array of fence contexts
43 * @num: [in] amount of contexts to allocate
44 *
45 * This function will return the first index of the number of fences allocated.
46 * The fence context is used for setting fence->context to a unique number.
47 */
48 u64 dma_fence_context_alloc(unsigned num)
49 {
50 WARN_ON(!num);
51 return atomic64_add_return(num, &dma_fence_context_counter) - num;
52 }
53 EXPORT_SYMBOL(dma_fence_context_alloc);
54
55 /**
56 * dma_fence_signal_locked - signal completion of a fence
57 * @fence: the fence to signal
58 *
59 * Signal completion for software callbacks on a fence, this will unblock
60 * dma_fence_wait() calls and run all the callbacks added with
61 * dma_fence_add_callback(). Can be called multiple times, but since a fence
62 * can only go from unsignaled to signaled state, it will only be effective
63 * the first time.
64 *
65 * Unlike dma_fence_signal, this function must be called with fence->lock held.
66 */
67 int dma_fence_signal_locked(struct dma_fence *fence)
68 {
69 struct dma_fence_cb *cur, *tmp;
70 int ret = 0;
71
72 lockdep_assert_held(fence->lock);
73
74 if (WARN_ON(!fence))
75 return -EINVAL;
76
77 if (test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
78 ret = -EINVAL;
79
80 /*
81 * we might have raced with the unlocked dma_fence_signal,
82 * still run through all callbacks
83 */
84 } else {
85 fence->timestamp = ktime_get();
86 set_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags);
87 trace_dma_fence_signaled(fence);
88 }
89
90 list_for_each_entry_safe(cur, tmp, &fence->cb_list, node) {
91 list_del_init(&cur->node);
92 cur->func(fence, cur);
93 }
94 return ret;
95 }
96 EXPORT_SYMBOL(dma_fence_signal_locked);
97
98 /**
99 * dma_fence_signal - signal completion of a fence
100 * @fence: the fence to signal
101 *
102 * Signal completion for software callbacks on a fence, this will unblock
103 * dma_fence_wait() calls and run all the callbacks added with
104 * dma_fence_add_callback(). Can be called multiple times, but since a fence
105 * can only go from unsignaled to signaled state, it will only be effective
106 * the first time.
107 */
108 int dma_fence_signal(struct dma_fence *fence)
109 {
110 unsigned long flags;
111
112 if (!fence)
113 return -EINVAL;
114
115 if (test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
116 return -EINVAL;
117
118 fence->timestamp = ktime_get();
119 set_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags);
120 trace_dma_fence_signaled(fence);
121
122 if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags)) {
123 struct dma_fence_cb *cur, *tmp;
124
125 spin_lock_irqsave(fence->lock, flags);
126 list_for_each_entry_safe(cur, tmp, &fence->cb_list, node) {
127 list_del_init(&cur->node);
128 cur->func(fence, cur);
129 }
130 spin_unlock_irqrestore(fence->lock, flags);
131 }
132 return 0;
133 }
134 EXPORT_SYMBOL(dma_fence_signal);
135
136 /**
137 * dma_fence_wait_timeout - sleep until the fence gets signaled
138 * or until timeout elapses
139 * @fence: [in] the fence to wait on
140 * @intr: [in] if true, do an interruptible wait
141 * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
142 *
143 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
144 * remaining timeout in jiffies on success. Other error values may be
145 * returned on custom implementations.
146 *
147 * Performs a synchronous wait on this fence. It is assumed the caller
148 * directly or indirectly (buf-mgr between reservation and committing)
149 * holds a reference to the fence, otherwise the fence might be
150 * freed before return, resulting in undefined behavior.
151 */
152 signed long
153 dma_fence_wait_timeout(struct dma_fence *fence, bool intr, signed long timeout)
154 {
155 signed long ret;
156
157 if (WARN_ON(timeout < 0))
158 return -EINVAL;
159
160 trace_dma_fence_wait_start(fence);
161 if (fence->ops->wait)
162 ret = fence->ops->wait(fence, intr, timeout);
163 else
164 ret = dma_fence_default_wait(fence, intr, timeout);
165 trace_dma_fence_wait_end(fence);
166 return ret;
167 }
168 EXPORT_SYMBOL(dma_fence_wait_timeout);
169
170 void dma_fence_release(struct kref *kref)
171 {
172 struct dma_fence *fence =
173 container_of(kref, struct dma_fence, refcount);
174
175 trace_dma_fence_destroy(fence);
176
177 /* Failed to signal before release, could be a refcounting issue */
178 WARN_ON(!list_empty(&fence->cb_list));
179
180 if (fence->ops->release)
181 fence->ops->release(fence);
182 else
183 dma_fence_free(fence);
184 }
185 EXPORT_SYMBOL(dma_fence_release);
186
187 /**
188 * dma_fence_free - default release function for &dma_fence.
189 * @fence: fence to release
190 *
191 * This is the default implementation for &dma_fence_ops.release. It calls
192 * kfree_rcu() on @fence.
193 */
194 void dma_fence_free(struct dma_fence *fence)
195 {
196 kfree_rcu(fence, rcu);
197 }
198 EXPORT_SYMBOL(dma_fence_free);
199
200 /**
201 * dma_fence_enable_sw_signaling - enable signaling on fence
202 * @fence: [in] the fence to enable
203 *
204 * this will request for sw signaling to be enabled, to make the fence
205 * complete as soon as possible
206 */
207 void dma_fence_enable_sw_signaling(struct dma_fence *fence)
208 {
209 unsigned long flags;
210
211 if (!test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
212 &fence->flags) &&
213 !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
214 trace_dma_fence_enable_signal(fence);
215
216 spin_lock_irqsave(fence->lock, flags);
217
218 if (!fence->ops->enable_signaling(fence))
219 dma_fence_signal_locked(fence);
220
221 spin_unlock_irqrestore(fence->lock, flags);
222 }
223 }
224 EXPORT_SYMBOL(dma_fence_enable_sw_signaling);
225
226 /**
227 * dma_fence_add_callback - add a callback to be called when the fence
228 * is signaled
229 * @fence: [in] the fence to wait on
230 * @cb: [in] the callback to register
231 * @func: [in] the function to call
232 *
233 * cb will be initialized by dma_fence_add_callback, no initialization
234 * by the caller is required. Any number of callbacks can be registered
235 * to a fence, but a callback can only be registered to one fence at a time.
236 *
237 * Note that the callback can be called from an atomic context. If
238 * fence is already signaled, this function will return -ENOENT (and
239 * *not* call the callback)
240 *
241 * Add a software callback to the fence. Same restrictions apply to
242 * refcount as it does to dma_fence_wait, however the caller doesn't need to
243 * keep a refcount to fence afterwards: when software access is enabled,
244 * the creator of the fence is required to keep the fence alive until
245 * after it signals with dma_fence_signal. The callback itself can be called
246 * from irq context.
247 *
248 * Returns 0 in case of success, -ENOENT if the fence is already signaled
249 * and -EINVAL in case of error.
250 */
251 int dma_fence_add_callback(struct dma_fence *fence, struct dma_fence_cb *cb,
252 dma_fence_func_t func)
253 {
254 unsigned long flags;
255 int ret = 0;
256 bool was_set;
257
258 if (WARN_ON(!fence || !func))
259 return -EINVAL;
260
261 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
262 INIT_LIST_HEAD(&cb->node);
263 return -ENOENT;
264 }
265
266 spin_lock_irqsave(fence->lock, flags);
267
268 was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
269 &fence->flags);
270
271 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
272 ret = -ENOENT;
273 else if (!was_set) {
274 trace_dma_fence_enable_signal(fence);
275
276 if (!fence->ops->enable_signaling(fence)) {
277 dma_fence_signal_locked(fence);
278 ret = -ENOENT;
279 }
280 }
281
282 if (!ret) {
283 cb->func = func;
284 list_add_tail(&cb->node, &fence->cb_list);
285 } else
286 INIT_LIST_HEAD(&cb->node);
287 spin_unlock_irqrestore(fence->lock, flags);
288
289 return ret;
290 }
291 EXPORT_SYMBOL(dma_fence_add_callback);
292
293 /**
294 * dma_fence_get_status - returns the status upon completion
295 * @fence: [in] the dma_fence to query
296 *
297 * This wraps dma_fence_get_status_locked() to return the error status
298 * condition on a signaled fence. See dma_fence_get_status_locked() for more
299 * details.
300 *
301 * Returns 0 if the fence has not yet been signaled, 1 if the fence has
302 * been signaled without an error condition, or a negative error code
303 * if the fence has been completed in err.
304 */
305 int dma_fence_get_status(struct dma_fence *fence)
306 {
307 unsigned long flags;
308 int status;
309
310 spin_lock_irqsave(fence->lock, flags);
311 status = dma_fence_get_status_locked(fence);
312 spin_unlock_irqrestore(fence->lock, flags);
313
314 return status;
315 }
316 EXPORT_SYMBOL(dma_fence_get_status);
317
318 /**
319 * dma_fence_remove_callback - remove a callback from the signaling list
320 * @fence: [in] the fence to wait on
321 * @cb: [in] the callback to remove
322 *
323 * Remove a previously queued callback from the fence. This function returns
324 * true if the callback is successfully removed, or false if the fence has
325 * already been signaled.
326 *
327 * *WARNING*:
328 * Cancelling a callback should only be done if you really know what you're
329 * doing, since deadlocks and race conditions could occur all too easily. For
330 * this reason, it should only ever be done on hardware lockup recovery,
331 * with a reference held to the fence.
332 */
333 bool
334 dma_fence_remove_callback(struct dma_fence *fence, struct dma_fence_cb *cb)
335 {
336 unsigned long flags;
337 bool ret;
338
339 spin_lock_irqsave(fence->lock, flags);
340
341 ret = !list_empty(&cb->node);
342 if (ret)
343 list_del_init(&cb->node);
344
345 spin_unlock_irqrestore(fence->lock, flags);
346
347 return ret;
348 }
349 EXPORT_SYMBOL(dma_fence_remove_callback);
350
351 struct default_wait_cb {
352 struct dma_fence_cb base;
353 struct task_struct *task;
354 };
355
356 static void
357 dma_fence_default_wait_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
358 {
359 struct default_wait_cb *wait =
360 container_of(cb, struct default_wait_cb, base);
361
362 wake_up_state(wait->task, TASK_NORMAL);
363 }
364
365 /**
366 * dma_fence_default_wait - default sleep until the fence gets signaled
367 * or until timeout elapses
368 * @fence: [in] the fence to wait on
369 * @intr: [in] if true, do an interruptible wait
370 * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
371 *
372 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
373 * remaining timeout in jiffies on success. If timeout is zero the value one is
374 * returned if the fence is already signaled for consistency with other
375 * functions taking a jiffies timeout.
376 */
377 signed long
378 dma_fence_default_wait(struct dma_fence *fence, bool intr, signed long timeout)
379 {
380 struct default_wait_cb cb;
381 unsigned long flags;
382 signed long ret = timeout ? timeout : 1;
383 bool was_set;
384
385 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
386 return ret;
387
388 spin_lock_irqsave(fence->lock, flags);
389
390 if (intr && signal_pending(current)) {
391 ret = -ERESTARTSYS;
392 goto out;
393 }
394
395 was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
396 &fence->flags);
397
398 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
399 goto out;
400
401 if (!was_set) {
402 trace_dma_fence_enable_signal(fence);
403
404 if (!fence->ops->enable_signaling(fence)) {
405 dma_fence_signal_locked(fence);
406 goto out;
407 }
408 }
409
410 if (!timeout) {
411 ret = 0;
412 goto out;
413 }
414
415 cb.base.func = dma_fence_default_wait_cb;
416 cb.task = current;
417 list_add(&cb.base.node, &fence->cb_list);
418
419 while (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags) && ret > 0) {
420 if (intr)
421 __set_current_state(TASK_INTERRUPTIBLE);
422 else
423 __set_current_state(TASK_UNINTERRUPTIBLE);
424 spin_unlock_irqrestore(fence->lock, flags);
425
426 ret = schedule_timeout(ret);
427
428 spin_lock_irqsave(fence->lock, flags);
429 if (ret > 0 && intr && signal_pending(current))
430 ret = -ERESTARTSYS;
431 }
432
433 if (!list_empty(&cb.base.node))
434 list_del(&cb.base.node);
435 __set_current_state(TASK_RUNNING);
436
437 out:
438 spin_unlock_irqrestore(fence->lock, flags);
439 return ret;
440 }
441 EXPORT_SYMBOL(dma_fence_default_wait);
442
443 static bool
444 dma_fence_test_signaled_any(struct dma_fence **fences, uint32_t count,
445 uint32_t *idx)
446 {
447 int i;
448
449 for (i = 0; i < count; ++i) {
450 struct dma_fence *fence = fences[i];
451 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
452 if (idx)
453 *idx = i;
454 return true;
455 }
456 }
457 return false;
458 }
459
460 /**
461 * dma_fence_wait_any_timeout - sleep until any fence gets signaled
462 * or until timeout elapses
463 * @fences: [in] array of fences to wait on
464 * @count: [in] number of fences to wait on
465 * @intr: [in] if true, do an interruptible wait
466 * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
467 * @idx: [out] the first signaled fence index, meaningful only on
468 * positive return
469 *
470 * Returns -EINVAL on custom fence wait implementation, -ERESTARTSYS if
471 * interrupted, 0 if the wait timed out, or the remaining timeout in jiffies
472 * on success.
473 *
474 * Synchronous waits for the first fence in the array to be signaled. The
475 * caller needs to hold a reference to all fences in the array, otherwise a
476 * fence might be freed before return, resulting in undefined behavior.
477 */
478 signed long
479 dma_fence_wait_any_timeout(struct dma_fence **fences, uint32_t count,
480 bool intr, signed long timeout, uint32_t *idx)
481 {
482 struct default_wait_cb *cb;
483 signed long ret = timeout;
484 unsigned i;
485
486 if (WARN_ON(!fences || !count || timeout < 0))
487 return -EINVAL;
488
489 if (timeout == 0) {
490 for (i = 0; i < count; ++i)
491 if (dma_fence_is_signaled(fences[i])) {
492 if (idx)
493 *idx = i;
494 return 1;
495 }
496
497 return 0;
498 }
499
500 cb = kcalloc(count, sizeof(struct default_wait_cb), GFP_KERNEL);
501 if (cb == NULL) {
502 ret = -ENOMEM;
503 goto err_free_cb;
504 }
505
506 for (i = 0; i < count; ++i) {
507 struct dma_fence *fence = fences[i];
508
509 cb[i].task = current;
510 if (dma_fence_add_callback(fence, &cb[i].base,
511 dma_fence_default_wait_cb)) {
512 /* This fence is already signaled */
513 if (idx)
514 *idx = i;
515 goto fence_rm_cb;
516 }
517 }
518
519 while (ret > 0) {
520 if (intr)
521 set_current_state(TASK_INTERRUPTIBLE);
522 else
523 set_current_state(TASK_UNINTERRUPTIBLE);
524
525 if (dma_fence_test_signaled_any(fences, count, idx))
526 break;
527
528 ret = schedule_timeout(ret);
529
530 if (ret > 0 && intr && signal_pending(current))
531 ret = -ERESTARTSYS;
532 }
533
534 __set_current_state(TASK_RUNNING);
535
536 fence_rm_cb:
537 while (i-- > 0)
538 dma_fence_remove_callback(fences[i], &cb[i].base);
539
540 err_free_cb:
541 kfree(cb);
542
543 return ret;
544 }
545 EXPORT_SYMBOL(dma_fence_wait_any_timeout);
546
547 /**
548 * dma_fence_init - Initialize a custom fence.
549 * @fence: [in] the fence to initialize
550 * @ops: [in] the dma_fence_ops for operations on this fence
551 * @lock: [in] the irqsafe spinlock to use for locking this fence
552 * @context: [in] the execution context this fence is run on
553 * @seqno: [in] a linear increasing sequence number for this context
554 *
555 * Initializes an allocated fence, the caller doesn't have to keep its
556 * refcount after committing with this fence, but it will need to hold a
557 * refcount again if dma_fence_ops.enable_signaling gets called. This can
558 * be used for other implementing other types of fence.
559 *
560 * context and seqno are used for easy comparison between fences, allowing
561 * to check which fence is later by simply using dma_fence_later.
562 */
563 void
564 dma_fence_init(struct dma_fence *fence, const struct dma_fence_ops *ops,
565 spinlock_t *lock, u64 context, unsigned seqno)
566 {
567 BUG_ON(!lock);
568 BUG_ON(!ops || !ops->wait ||
569 !ops->get_driver_name || !ops->get_timeline_name);
570
571 kref_init(&fence->refcount);
572 fence->ops = ops;
573 INIT_LIST_HEAD(&fence->cb_list);
574 fence->lock = lock;
575 fence->context = context;
576 fence->seqno = seqno;
577 fence->flags = 0UL;
578 fence->error = 0;
579
580 if (!ops->enable_signaling)
581 set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
582 &fence->flags);
583
584 trace_dma_fence_init(fence);
585 }
586 EXPORT_SYMBOL(dma_fence_init);