blob: 136ec04d683f1c5b80598301f8fab897cd99b5e4 [file] [log] [blame]
Maarten Lankhorste9417592014-07-01 12:57:14 +02001/*
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>
Chris Wilsonf54d1862016-10-25 13:00:45 +010024#include <linux/dma-fence.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010025#include <linux/sched/signal.h>
Maarten Lankhorste9417592014-07-01 12:57:14 +020026
27#define CREATE_TRACE_POINTS
Chris Wilsonf54d1862016-10-25 13:00:45 +010028#include <trace/events/dma_fence.h>
Maarten Lankhorste9417592014-07-01 12:57:14 +020029
Chris Wilsonf54d1862016-10-25 13:00:45 +010030EXPORT_TRACEPOINT_SYMBOL(dma_fence_emit);
Chris Wilson8c96c672017-01-24 11:57:58 +000031EXPORT_TRACEPOINT_SYMBOL(dma_fence_enable_signal);
Maarten Lankhorste9417592014-07-01 12:57:14 +020032
Christian König078dec332018-12-03 13:36:14 +010033static DEFINE_SPINLOCK(dma_fence_stub_lock);
34static struct dma_fence dma_fence_stub;
35
Thierry Redinge9f3b792014-08-08 12:42:32 +020036/*
Maarten Lankhorste9417592014-07-01 12:57:14 +020037 * fence context counter: each execution context should have its own
38 * fence context, this allows checking if fences belong to the same
39 * context or not. One device can have multiple separate contexts,
40 * and they're used if some engine can run independently of another.
41 */
Christian König078dec332018-12-03 13:36:14 +010042static atomic64_t dma_fence_context_counter = ATOMIC64_INIT(1);
Maarten Lankhorste9417592014-07-01 12:57:14 +020043
44/**
Daniel Vetter4dd3cdb2018-07-04 11:29:09 +020045 * DOC: DMA fences overview
Maarten Lankhorste9417592014-07-01 12:57:14 +020046 *
Daniel Vetter4dd3cdb2018-07-04 11:29:09 +020047 * DMA fences, represented by &struct dma_fence, are the kernel internal
48 * synchronization primitive for DMA operations like GPU rendering, video
49 * encoding/decoding, or displaying buffers on a screen.
50 *
51 * A fence is initialized using dma_fence_init() and completed using
52 * dma_fence_signal(). Fences are associated with a context, allocated through
53 * dma_fence_context_alloc(), and all fences on the same context are
54 * fully ordered.
55 *
56 * Since the purposes of fences is to facilitate cross-device and
57 * cross-application synchronization, there's multiple ways to use one:
58 *
59 * - Individual fences can be exposed as a &sync_file, accessed as a file
60 * descriptor from userspace, created by calling sync_file_create(). This is
61 * called explicit fencing, since userspace passes around explicit
62 * synchronization points.
63 *
64 * - Some subsystems also have their own explicit fencing primitives, like
65 * &drm_syncobj. Compared to &sync_file, a &drm_syncobj allows the underlying
66 * fence to be updated.
67 *
68 * - Then there's also implicit fencing, where the synchronization points are
69 * implicitly passed around as part of shared &dma_buf instances. Such
70 * implicit fences are stored in &struct reservation_object through the
71 * &dma_buf.resv pointer.
72 */
73
Christian König078dec332018-12-03 13:36:14 +010074static const char *dma_fence_stub_get_name(struct dma_fence *fence)
75{
76 return "stub";
77}
78
79static const struct dma_fence_ops dma_fence_stub_ops = {
80 .get_driver_name = dma_fence_stub_get_name,
81 .get_timeline_name = dma_fence_stub_get_name,
82};
83
84/**
85 * dma_fence_get_stub - return a signaled fence
86 *
87 * Return a stub fence which is already signaled.
88 */
89struct dma_fence *dma_fence_get_stub(void)
90{
91 spin_lock(&dma_fence_stub_lock);
92 if (!dma_fence_stub.ops) {
93 dma_fence_init(&dma_fence_stub,
94 &dma_fence_stub_ops,
95 &dma_fence_stub_lock,
96 0, 0);
97 dma_fence_signal_locked(&dma_fence_stub);
98 }
99 spin_unlock(&dma_fence_stub_lock);
100
101 return dma_fence_get(&dma_fence_stub);
102}
103EXPORT_SYMBOL(dma_fence_get_stub);
104
Daniel Vetter4dd3cdb2018-07-04 11:29:09 +0200105/**
106 * dma_fence_context_alloc - allocate an array of fence contexts
107 * @num: amount of contexts to allocate
108 *
109 * This function will return the first index of the number of fence contexts
110 * allocated. The fence context is used for setting &dma_fence.context to a
111 * unique number by passing the context to dma_fence_init().
Maarten Lankhorste9417592014-07-01 12:57:14 +0200112 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100113u64 dma_fence_context_alloc(unsigned num)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200114{
Daniel Vetter6ce31262017-07-20 14:51:07 +0200115 WARN_ON(!num);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100116 return atomic64_add_return(num, &dma_fence_context_counter) - num;
Maarten Lankhorste9417592014-07-01 12:57:14 +0200117}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100118EXPORT_SYMBOL(dma_fence_context_alloc);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200119
120/**
Chris Wilsonf54d1862016-10-25 13:00:45 +0100121 * dma_fence_signal_locked - signal completion of a fence
Maarten Lankhorste9417592014-07-01 12:57:14 +0200122 * @fence: the fence to signal
123 *
124 * Signal completion for software callbacks on a fence, this will unblock
Chris Wilsonf54d1862016-10-25 13:00:45 +0100125 * dma_fence_wait() calls and run all the callbacks added with
126 * dma_fence_add_callback(). Can be called multiple times, but since a fence
Daniel Vetter4dd3cdb2018-07-04 11:29:09 +0200127 * can only go from the unsignaled to the signaled state and not back, it will
128 * only be effective the first time.
Maarten Lankhorste9417592014-07-01 12:57:14 +0200129 *
Daniel Vetter4dd3cdb2018-07-04 11:29:09 +0200130 * Unlike dma_fence_signal(), this function must be called with &dma_fence.lock
131 * held.
132 *
133 * Returns 0 on success and a negative error value when @fence has been
134 * signalled already.
Maarten Lankhorste9417592014-07-01 12:57:14 +0200135 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100136int dma_fence_signal_locked(struct dma_fence *fence)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200137{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100138 struct dma_fence_cb *cur, *tmp;
Maarten Lankhorste9417592014-07-01 12:57:14 +0200139 int ret = 0;
140
Rob Clark78010cd2016-10-24 15:57:10 -0400141 lockdep_assert_held(fence->lock);
142
Maarten Lankhorste9417592014-07-01 12:57:14 +0200143 if (WARN_ON(!fence))
144 return -EINVAL;
145
Chris Wilsonf54d1862016-10-25 13:00:45 +0100146 if (test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
Maarten Lankhorste9417592014-07-01 12:57:14 +0200147 ret = -EINVAL;
148
149 /*
Chris Wilsonf54d1862016-10-25 13:00:45 +0100150 * we might have raced with the unlocked dma_fence_signal,
Maarten Lankhorste9417592014-07-01 12:57:14 +0200151 * still run through all callbacks
152 */
Chris Wilson76250f22017-02-14 12:40:01 +0000153 } else {
154 fence->timestamp = ktime_get();
155 set_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100156 trace_dma_fence_signaled(fence);
Chris Wilson76250f22017-02-14 12:40:01 +0000157 }
Maarten Lankhorste9417592014-07-01 12:57:14 +0200158
159 list_for_each_entry_safe(cur, tmp, &fence->cb_list, node) {
160 list_del_init(&cur->node);
161 cur->func(fence, cur);
162 }
163 return ret;
164}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100165EXPORT_SYMBOL(dma_fence_signal_locked);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200166
167/**
Chris Wilsonf54d1862016-10-25 13:00:45 +0100168 * dma_fence_signal - signal completion of a fence
Maarten Lankhorste9417592014-07-01 12:57:14 +0200169 * @fence: the fence to signal
170 *
171 * Signal completion for software callbacks on a fence, this will unblock
Chris Wilsonf54d1862016-10-25 13:00:45 +0100172 * dma_fence_wait() calls and run all the callbacks added with
173 * dma_fence_add_callback(). Can be called multiple times, but since a fence
Daniel Vetter4dd3cdb2018-07-04 11:29:09 +0200174 * can only go from the unsignaled to the signaled state and not back, it will
175 * only be effective the first time.
176 *
177 * Returns 0 on success and a negative error value when @fence has been
178 * signalled already.
Maarten Lankhorste9417592014-07-01 12:57:14 +0200179 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100180int dma_fence_signal(struct dma_fence *fence)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200181{
182 unsigned long flags;
183
184 if (!fence)
185 return -EINVAL;
186
Chris Wilsonf54d1862016-10-25 13:00:45 +0100187 if (test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
Maarten Lankhorste9417592014-07-01 12:57:14 +0200188 return -EINVAL;
189
Chris Wilson76250f22017-02-14 12:40:01 +0000190 fence->timestamp = ktime_get();
191 set_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100192 trace_dma_fence_signaled(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200193
Chris Wilsonf54d1862016-10-25 13:00:45 +0100194 if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags)) {
195 struct dma_fence_cb *cur, *tmp;
Maarten Lankhorste9417592014-07-01 12:57:14 +0200196
197 spin_lock_irqsave(fence->lock, flags);
198 list_for_each_entry_safe(cur, tmp, &fence->cb_list, node) {
199 list_del_init(&cur->node);
200 cur->func(fence, cur);
201 }
202 spin_unlock_irqrestore(fence->lock, flags);
203 }
204 return 0;
205}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100206EXPORT_SYMBOL(dma_fence_signal);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200207
208/**
Chris Wilsonf54d1862016-10-25 13:00:45 +0100209 * dma_fence_wait_timeout - sleep until the fence gets signaled
Maarten Lankhorste9417592014-07-01 12:57:14 +0200210 * or until timeout elapses
Daniel Vetter4dd3cdb2018-07-04 11:29:09 +0200211 * @fence: the fence to wait on
212 * @intr: if true, do an interruptible wait
213 * @timeout: timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
Maarten Lankhorste9417592014-07-01 12:57:14 +0200214 *
215 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
216 * remaining timeout in jiffies on success. Other error values may be
217 * returned on custom implementations.
218 *
219 * Performs a synchronous wait on this fence. It is assumed the caller
220 * directly or indirectly (buf-mgr between reservation and committing)
221 * holds a reference to the fence, otherwise the fence might be
222 * freed before return, resulting in undefined behavior.
Daniel Vetter4dd3cdb2018-07-04 11:29:09 +0200223 *
224 * See also dma_fence_wait() and dma_fence_wait_any_timeout().
Maarten Lankhorste9417592014-07-01 12:57:14 +0200225 */
226signed long
Chris Wilsonf54d1862016-10-25 13:00:45 +0100227dma_fence_wait_timeout(struct dma_fence *fence, bool intr, signed long timeout)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200228{
229 signed long ret;
230
231 if (WARN_ON(timeout < 0))
232 return -EINVAL;
233
Chris Wilsonf54d1862016-10-25 13:00:45 +0100234 trace_dma_fence_wait_start(fence);
Daniel Vetter418cc6c2018-05-03 16:25:52 +0200235 if (fence->ops->wait)
236 ret = fence->ops->wait(fence, intr, timeout);
237 else
238 ret = dma_fence_default_wait(fence, intr, timeout);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100239 trace_dma_fence_wait_end(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200240 return ret;
241}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100242EXPORT_SYMBOL(dma_fence_wait_timeout);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200243
Daniel Vetter4dd3cdb2018-07-04 11:29:09 +0200244/**
245 * dma_fence_release - default relese function for fences
246 * @kref: &dma_fence.recfount
247 *
248 * This is the default release functions for &dma_fence. Drivers shouldn't call
249 * this directly, but instead call dma_fence_put().
250 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100251void dma_fence_release(struct kref *kref)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200252{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100253 struct dma_fence *fence =
254 container_of(kref, struct dma_fence, refcount);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200255
Chris Wilsonf54d1862016-10-25 13:00:45 +0100256 trace_dma_fence_destroy(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200257
Oded Gabbay4518cd22018-01-29 17:31:40 +0200258 /* Failed to signal before release, could be a refcounting issue */
Daniel Vetter6ce31262017-07-20 14:51:07 +0200259 WARN_ON(!list_empty(&fence->cb_list));
Maarten Lankhorste9417592014-07-01 12:57:14 +0200260
261 if (fence->ops->release)
262 fence->ops->release(fence);
263 else
Chris Wilsonf54d1862016-10-25 13:00:45 +0100264 dma_fence_free(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200265}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100266EXPORT_SYMBOL(dma_fence_release);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200267
Daniel Vetter4dd3cdb2018-07-04 11:29:09 +0200268/**
269 * dma_fence_free - default release function for &dma_fence.
270 * @fence: fence to release
271 *
272 * This is the default implementation for &dma_fence_ops.release. It calls
273 * kfree_rcu() on @fence.
274 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100275void dma_fence_free(struct dma_fence *fence)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200276{
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200277 kfree_rcu(fence, rcu);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200278}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100279EXPORT_SYMBOL(dma_fence_free);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200280
281/**
Chris Wilsonf54d1862016-10-25 13:00:45 +0100282 * dma_fence_enable_sw_signaling - enable signaling on fence
Daniel Vetter4dd3cdb2018-07-04 11:29:09 +0200283 * @fence: the fence to enable
Maarten Lankhorste9417592014-07-01 12:57:14 +0200284 *
Daniel Vetter4dd3cdb2018-07-04 11:29:09 +0200285 * This will request for sw signaling to be enabled, to make the fence
286 * complete as soon as possible. This calls &dma_fence_ops.enable_signaling
287 * internally.
Maarten Lankhorste9417592014-07-01 12:57:14 +0200288 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100289void dma_fence_enable_sw_signaling(struct dma_fence *fence)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200290{
291 unsigned long flags;
292
Chris Wilsonf54d1862016-10-25 13:00:45 +0100293 if (!test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
294 &fence->flags) &&
Daniel Vetterc7013172018-05-04 16:10:34 +0200295 !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags) &&
296 fence->ops->enable_signaling) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100297 trace_dma_fence_enable_signal(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200298
299 spin_lock_irqsave(fence->lock, flags);
300
301 if (!fence->ops->enable_signaling(fence))
Chris Wilsonf54d1862016-10-25 13:00:45 +0100302 dma_fence_signal_locked(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200303
304 spin_unlock_irqrestore(fence->lock, flags);
305 }
306}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100307EXPORT_SYMBOL(dma_fence_enable_sw_signaling);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200308
309/**
Chris Wilsonf54d1862016-10-25 13:00:45 +0100310 * dma_fence_add_callback - add a callback to be called when the fence
Maarten Lankhorste9417592014-07-01 12:57:14 +0200311 * is signaled
Daniel Vetter4dd3cdb2018-07-04 11:29:09 +0200312 * @fence: the fence to wait on
313 * @cb: the callback to register
314 * @func: the function to call
Maarten Lankhorste9417592014-07-01 12:57:14 +0200315 *
Daniel Vetter4dd3cdb2018-07-04 11:29:09 +0200316 * @cb will be initialized by dma_fence_add_callback(), no initialization
Maarten Lankhorste9417592014-07-01 12:57:14 +0200317 * by the caller is required. Any number of callbacks can be registered
318 * to a fence, but a callback can only be registered to one fence at a time.
319 *
320 * Note that the callback can be called from an atomic context. If
321 * fence is already signaled, this function will return -ENOENT (and
Daniel Vetter4dd3cdb2018-07-04 11:29:09 +0200322 * *not* call the callback).
Maarten Lankhorste9417592014-07-01 12:57:14 +0200323 *
324 * Add a software callback to the fence. Same restrictions apply to
Daniel Vetter4dd3cdb2018-07-04 11:29:09 +0200325 * refcount as it does to dma_fence_wait(), however the caller doesn't need to
326 * keep a refcount to fence afterward dma_fence_add_callback() has returned:
327 * when software access is enabled, the creator of the fence is required to keep
328 * the fence alive until after it signals with dma_fence_signal(). The callback
329 * itself can be called from irq context.
Maarten Lankhorste9417592014-07-01 12:57:14 +0200330 *
Gustavo Padovanf642de12017-02-15 15:57:25 -0200331 * Returns 0 in case of success, -ENOENT if the fence is already signaled
332 * and -EINVAL in case of error.
Maarten Lankhorste9417592014-07-01 12:57:14 +0200333 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100334int dma_fence_add_callback(struct dma_fence *fence, struct dma_fence_cb *cb,
335 dma_fence_func_t func)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200336{
337 unsigned long flags;
338 int ret = 0;
339 bool was_set;
340
341 if (WARN_ON(!fence || !func))
342 return -EINVAL;
343
Chris Wilsonf54d1862016-10-25 13:00:45 +0100344 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
Maarten Lankhorste9417592014-07-01 12:57:14 +0200345 INIT_LIST_HEAD(&cb->node);
346 return -ENOENT;
347 }
348
349 spin_lock_irqsave(fence->lock, flags);
350
Chris Wilsonf54d1862016-10-25 13:00:45 +0100351 was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
352 &fence->flags);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200353
Chris Wilsonf54d1862016-10-25 13:00:45 +0100354 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
Maarten Lankhorste9417592014-07-01 12:57:14 +0200355 ret = -ENOENT;
Daniel Vetterc7013172018-05-04 16:10:34 +0200356 else if (!was_set && fence->ops->enable_signaling) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100357 trace_dma_fence_enable_signal(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200358
359 if (!fence->ops->enable_signaling(fence)) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100360 dma_fence_signal_locked(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200361 ret = -ENOENT;
362 }
363 }
364
365 if (!ret) {
366 cb->func = func;
367 list_add_tail(&cb->node, &fence->cb_list);
368 } else
369 INIT_LIST_HEAD(&cb->node);
370 spin_unlock_irqrestore(fence->lock, flags);
371
372 return ret;
373}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100374EXPORT_SYMBOL(dma_fence_add_callback);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200375
376/**
Chris Wilsond6c99f42017-01-04 14:12:21 +0000377 * dma_fence_get_status - returns the status upon completion
Daniel Vetter4dd3cdb2018-07-04 11:29:09 +0200378 * @fence: the dma_fence to query
Chris Wilsond6c99f42017-01-04 14:12:21 +0000379 *
380 * This wraps dma_fence_get_status_locked() to return the error status
381 * condition on a signaled fence. See dma_fence_get_status_locked() for more
382 * details.
383 *
384 * Returns 0 if the fence has not yet been signaled, 1 if the fence has
385 * been signaled without an error condition, or a negative error code
386 * if the fence has been completed in err.
387 */
388int dma_fence_get_status(struct dma_fence *fence)
389{
390 unsigned long flags;
391 int status;
392
393 spin_lock_irqsave(fence->lock, flags);
394 status = dma_fence_get_status_locked(fence);
395 spin_unlock_irqrestore(fence->lock, flags);
396
397 return status;
398}
399EXPORT_SYMBOL(dma_fence_get_status);
400
401/**
Chris Wilsonf54d1862016-10-25 13:00:45 +0100402 * dma_fence_remove_callback - remove a callback from the signaling list
Daniel Vetter4dd3cdb2018-07-04 11:29:09 +0200403 * @fence: the fence to wait on
404 * @cb: the callback to remove
Maarten Lankhorste9417592014-07-01 12:57:14 +0200405 *
406 * Remove a previously queued callback from the fence. This function returns
Masanari Iidaf353d712014-10-22 00:00:14 +0900407 * true if the callback is successfully removed, or false if the fence has
Maarten Lankhorste9417592014-07-01 12:57:14 +0200408 * already been signaled.
409 *
410 * *WARNING*:
411 * Cancelling a callback should only be done if you really know what you're
412 * doing, since deadlocks and race conditions could occur all too easily. For
413 * this reason, it should only ever be done on hardware lockup recovery,
414 * with a reference held to the fence.
Daniel Vetter4dd3cdb2018-07-04 11:29:09 +0200415 *
416 * Behaviour is undefined if @cb has not been added to @fence using
417 * dma_fence_add_callback() beforehand.
Maarten Lankhorste9417592014-07-01 12:57:14 +0200418 */
419bool
Chris Wilsonf54d1862016-10-25 13:00:45 +0100420dma_fence_remove_callback(struct dma_fence *fence, struct dma_fence_cb *cb)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200421{
422 unsigned long flags;
423 bool ret;
424
425 spin_lock_irqsave(fence->lock, flags);
426
427 ret = !list_empty(&cb->node);
428 if (ret)
429 list_del_init(&cb->node);
430
431 spin_unlock_irqrestore(fence->lock, flags);
432
433 return ret;
434}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100435EXPORT_SYMBOL(dma_fence_remove_callback);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200436
437struct default_wait_cb {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100438 struct dma_fence_cb base;
Maarten Lankhorste9417592014-07-01 12:57:14 +0200439 struct task_struct *task;
440};
441
442static void
Chris Wilsonf54d1862016-10-25 13:00:45 +0100443dma_fence_default_wait_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200444{
445 struct default_wait_cb *wait =
446 container_of(cb, struct default_wait_cb, base);
447
448 wake_up_state(wait->task, TASK_NORMAL);
449}
450
451/**
Chris Wilsonf54d1862016-10-25 13:00:45 +0100452 * dma_fence_default_wait - default sleep until the fence gets signaled
Maarten Lankhorste9417592014-07-01 12:57:14 +0200453 * or until timeout elapses
Daniel Vetter4dd3cdb2018-07-04 11:29:09 +0200454 * @fence: the fence to wait on
455 * @intr: if true, do an interruptible wait
456 * @timeout: timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
Maarten Lankhorste9417592014-07-01 12:57:14 +0200457 *
458 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
Alex Deucherbcc004b2016-11-07 16:16:13 -0500459 * remaining timeout in jiffies on success. If timeout is zero the value one is
460 * returned if the fence is already signaled for consistency with other
461 * functions taking a jiffies timeout.
Maarten Lankhorste9417592014-07-01 12:57:14 +0200462 */
463signed long
Chris Wilsonf54d1862016-10-25 13:00:45 +0100464dma_fence_default_wait(struct dma_fence *fence, bool intr, signed long timeout)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200465{
466 struct default_wait_cb cb;
467 unsigned long flags;
Alex Deucherbcc004b2016-11-07 16:16:13 -0500468 signed long ret = timeout ? timeout : 1;
Maarten Lankhorste9417592014-07-01 12:57:14 +0200469 bool was_set;
470
Chris Wilsonf54d1862016-10-25 13:00:45 +0100471 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
Alex Deucherbcc004b2016-11-07 16:16:13 -0500472 return ret;
Maarten Lankhorste9417592014-07-01 12:57:14 +0200473
474 spin_lock_irqsave(fence->lock, flags);
475
476 if (intr && signal_pending(current)) {
477 ret = -ERESTARTSYS;
478 goto out;
479 }
480
Chris Wilsonf54d1862016-10-25 13:00:45 +0100481 was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
482 &fence->flags);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200483
Chris Wilsonf54d1862016-10-25 13:00:45 +0100484 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
Maarten Lankhorste9417592014-07-01 12:57:14 +0200485 goto out;
486
Daniel Vetterc7013172018-05-04 16:10:34 +0200487 if (!was_set && fence->ops->enable_signaling) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100488 trace_dma_fence_enable_signal(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200489
490 if (!fence->ops->enable_signaling(fence)) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100491 dma_fence_signal_locked(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200492 goto out;
493 }
494 }
495
Andres Rodriguez03c0c5f2017-04-26 10:46:20 -0400496 if (!timeout) {
497 ret = 0;
498 goto out;
499 }
500
Chris Wilsonf54d1862016-10-25 13:00:45 +0100501 cb.base.func = dma_fence_default_wait_cb;
Maarten Lankhorste9417592014-07-01 12:57:14 +0200502 cb.task = current;
503 list_add(&cb.base.node, &fence->cb_list);
504
Chris Wilsonf54d1862016-10-25 13:00:45 +0100505 while (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags) && ret > 0) {
Maarten Lankhorste9417592014-07-01 12:57:14 +0200506 if (intr)
507 __set_current_state(TASK_INTERRUPTIBLE);
508 else
509 __set_current_state(TASK_UNINTERRUPTIBLE);
510 spin_unlock_irqrestore(fence->lock, flags);
511
512 ret = schedule_timeout(ret);
513
514 spin_lock_irqsave(fence->lock, flags);
515 if (ret > 0 && intr && signal_pending(current))
516 ret = -ERESTARTSYS;
517 }
518
519 if (!list_empty(&cb.base.node))
520 list_del(&cb.base.node);
521 __set_current_state(TASK_RUNNING);
522
523out:
524 spin_unlock_irqrestore(fence->lock, flags);
525 return ret;
526}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100527EXPORT_SYMBOL(dma_fence_default_wait);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200528
Christian Königa5194352015-10-20 16:34:16 +0200529static bool
monk.liu7392b4b2016-11-04 16:16:09 -0400530dma_fence_test_signaled_any(struct dma_fence **fences, uint32_t count,
531 uint32_t *idx)
Christian Königa5194352015-10-20 16:34:16 +0200532{
533 int i;
534
535 for (i = 0; i < count; ++i) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100536 struct dma_fence *fence = fences[i];
monk.liu7392b4b2016-11-04 16:16:09 -0400537 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
538 if (idx)
539 *idx = i;
Christian Königa5194352015-10-20 16:34:16 +0200540 return true;
monk.liu7392b4b2016-11-04 16:16:09 -0400541 }
Christian Königa5194352015-10-20 16:34:16 +0200542 }
543 return false;
544}
545
546/**
Chris Wilsonf54d1862016-10-25 13:00:45 +0100547 * dma_fence_wait_any_timeout - sleep until any fence gets signaled
Christian Königa5194352015-10-20 16:34:16 +0200548 * or until timeout elapses
Daniel Vetter4dd3cdb2018-07-04 11:29:09 +0200549 * @fences: array of fences to wait on
550 * @count: number of fences to wait on
551 * @intr: if true, do an interruptible wait
552 * @timeout: timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
553 * @idx: used to store the first signaled fence index, meaningful only on
554 * positive return
Christian Königa5194352015-10-20 16:34:16 +0200555 *
556 * Returns -EINVAL on custom fence wait implementation, -ERESTARTSYS if
557 * interrupted, 0 if the wait timed out, or the remaining timeout in jiffies
558 * on success.
559 *
560 * Synchronous waits for the first fence in the array to be signaled. The
561 * caller needs to hold a reference to all fences in the array, otherwise a
562 * fence might be freed before return, resulting in undefined behavior.
Daniel Vetter4dd3cdb2018-07-04 11:29:09 +0200563 *
564 * See also dma_fence_wait() and dma_fence_wait_timeout().
Christian Königa5194352015-10-20 16:34:16 +0200565 */
566signed long
Chris Wilsonf54d1862016-10-25 13:00:45 +0100567dma_fence_wait_any_timeout(struct dma_fence **fences, uint32_t count,
monk.liu7392b4b2016-11-04 16:16:09 -0400568 bool intr, signed long timeout, uint32_t *idx)
Christian Königa5194352015-10-20 16:34:16 +0200569{
570 struct default_wait_cb *cb;
571 signed long ret = timeout;
572 unsigned i;
573
574 if (WARN_ON(!fences || !count || timeout < 0))
575 return -EINVAL;
576
577 if (timeout == 0) {
578 for (i = 0; i < count; ++i)
monk.liu7392b4b2016-11-04 16:16:09 -0400579 if (dma_fence_is_signaled(fences[i])) {
580 if (idx)
581 *idx = i;
Christian Königa5194352015-10-20 16:34:16 +0200582 return 1;
monk.liu7392b4b2016-11-04 16:16:09 -0400583 }
Christian Königa5194352015-10-20 16:34:16 +0200584
585 return 0;
586 }
587
588 cb = kcalloc(count, sizeof(struct default_wait_cb), GFP_KERNEL);
589 if (cb == NULL) {
590 ret = -ENOMEM;
591 goto err_free_cb;
592 }
593
594 for (i = 0; i < count; ++i) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100595 struct dma_fence *fence = fences[i];
Christian Königa5194352015-10-20 16:34:16 +0200596
Christian Königa5194352015-10-20 16:34:16 +0200597 cb[i].task = current;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100598 if (dma_fence_add_callback(fence, &cb[i].base,
599 dma_fence_default_wait_cb)) {
Christian Königa5194352015-10-20 16:34:16 +0200600 /* This fence is already signaled */
monk.liu7392b4b2016-11-04 16:16:09 -0400601 if (idx)
602 *idx = i;
Christian Königa5194352015-10-20 16:34:16 +0200603 goto fence_rm_cb;
604 }
605 }
606
607 while (ret > 0) {
608 if (intr)
609 set_current_state(TASK_INTERRUPTIBLE);
610 else
611 set_current_state(TASK_UNINTERRUPTIBLE);
612
monk.liu7392b4b2016-11-04 16:16:09 -0400613 if (dma_fence_test_signaled_any(fences, count, idx))
Christian Königa5194352015-10-20 16:34:16 +0200614 break;
615
616 ret = schedule_timeout(ret);
617
618 if (ret > 0 && intr && signal_pending(current))
619 ret = -ERESTARTSYS;
620 }
621
622 __set_current_state(TASK_RUNNING);
623
624fence_rm_cb:
625 while (i-- > 0)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100626 dma_fence_remove_callback(fences[i], &cb[i].base);
Christian Königa5194352015-10-20 16:34:16 +0200627
628err_free_cb:
629 kfree(cb);
630
631 return ret;
632}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100633EXPORT_SYMBOL(dma_fence_wait_any_timeout);
Christian Königa5194352015-10-20 16:34:16 +0200634
Maarten Lankhorste9417592014-07-01 12:57:14 +0200635/**
Chris Wilsonf54d1862016-10-25 13:00:45 +0100636 * dma_fence_init - Initialize a custom fence.
Daniel Vetter4dd3cdb2018-07-04 11:29:09 +0200637 * @fence: the fence to initialize
638 * @ops: the dma_fence_ops for operations on this fence
639 * @lock: the irqsafe spinlock to use for locking this fence
640 * @context: the execution context this fence is run on
641 * @seqno: a linear increasing sequence number for this context
Maarten Lankhorste9417592014-07-01 12:57:14 +0200642 *
643 * Initializes an allocated fence, the caller doesn't have to keep its
644 * refcount after committing with this fence, but it will need to hold a
Daniel Vetter4dd3cdb2018-07-04 11:29:09 +0200645 * refcount again if &dma_fence_ops.enable_signaling gets called.
Maarten Lankhorste9417592014-07-01 12:57:14 +0200646 *
647 * context and seqno are used for easy comparison between fences, allowing
Daniel Vetter4dd3cdb2018-07-04 11:29:09 +0200648 * to check which fence is later by simply using dma_fence_later().
Maarten Lankhorste9417592014-07-01 12:57:14 +0200649 */
650void
Chris Wilsonf54d1862016-10-25 13:00:45 +0100651dma_fence_init(struct dma_fence *fence, const struct dma_fence_ops *ops,
652 spinlock_t *lock, u64 context, unsigned seqno)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200653{
654 BUG_ON(!lock);
Daniel Vetter418cc6c2018-05-03 16:25:52 +0200655 BUG_ON(!ops || !ops->get_driver_name || !ops->get_timeline_name);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200656
657 kref_init(&fence->refcount);
658 fence->ops = ops;
659 INIT_LIST_HEAD(&fence->cb_list);
660 fence->lock = lock;
661 fence->context = context;
662 fence->seqno = seqno;
663 fence->flags = 0UL;
Chris Wilsona009e972017-01-04 14:12:22 +0000664 fence->error = 0;
Maarten Lankhorste9417592014-07-01 12:57:14 +0200665
Chris Wilsonf54d1862016-10-25 13:00:45 +0100666 trace_dma_fence_init(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200667}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100668EXPORT_SYMBOL(dma_fence_init);