blob: 57da14c15987fc019d1786ec0a4b0bd65077cf99 [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_annotate_wait_on);
31EXPORT_TRACEPOINT_SYMBOL(dma_fence_emit);
Chris Wilson8c96c672017-01-24 11:57:58 +000032EXPORT_TRACEPOINT_SYMBOL(dma_fence_enable_signal);
Maarten Lankhorste9417592014-07-01 12:57:14 +020033
Thierry Redinge9f3b792014-08-08 12:42:32 +020034/*
Maarten Lankhorste9417592014-07-01 12:57:14 +020035 * fence context counter: each execution context should have its own
36 * fence context, this allows checking if fences belong to the same
37 * context or not. One device can have multiple separate contexts,
38 * and they're used if some engine can run independently of another.
39 */
Chris Wilsonf54d1862016-10-25 13:00:45 +010040static atomic64_t dma_fence_context_counter = ATOMIC64_INIT(0);
Maarten Lankhorste9417592014-07-01 12:57:14 +020041
42/**
Chris Wilsonf54d1862016-10-25 13:00:45 +010043 * dma_fence_context_alloc - allocate an array of fence contexts
Maarten Lankhorste9417592014-07-01 12:57:14 +020044 * @num: [in] amount of contexts to allocate
45 *
46 * This function will return the first index of the number of fences allocated.
47 * The fence context is used for setting fence->context to a unique number.
48 */
Chris Wilsonf54d1862016-10-25 13:00:45 +010049u64 dma_fence_context_alloc(unsigned num)
Maarten Lankhorste9417592014-07-01 12:57:14 +020050{
51 BUG_ON(!num);
Chris Wilsonf54d1862016-10-25 13:00:45 +010052 return atomic64_add_return(num, &dma_fence_context_counter) - num;
Maarten Lankhorste9417592014-07-01 12:57:14 +020053}
Chris Wilsonf54d1862016-10-25 13:00:45 +010054EXPORT_SYMBOL(dma_fence_context_alloc);
Maarten Lankhorste9417592014-07-01 12:57:14 +020055
56/**
Chris Wilsonf54d1862016-10-25 13:00:45 +010057 * dma_fence_signal_locked - signal completion of a fence
Maarten Lankhorste9417592014-07-01 12:57:14 +020058 * @fence: the fence to signal
59 *
60 * Signal completion for software callbacks on a fence, this will unblock
Chris Wilsonf54d1862016-10-25 13:00:45 +010061 * dma_fence_wait() calls and run all the callbacks added with
62 * dma_fence_add_callback(). Can be called multiple times, but since a fence
Maarten Lankhorste9417592014-07-01 12:57:14 +020063 * can only go from unsignaled to signaled state, it will only be effective
64 * the first time.
65 *
Chris Wilsonf54d1862016-10-25 13:00:45 +010066 * Unlike dma_fence_signal, this function must be called with fence->lock held.
Maarten Lankhorste9417592014-07-01 12:57:14 +020067 */
Chris Wilsonf54d1862016-10-25 13:00:45 +010068int dma_fence_signal_locked(struct dma_fence *fence)
Maarten Lankhorste9417592014-07-01 12:57:14 +020069{
Chris Wilsonf54d1862016-10-25 13:00:45 +010070 struct dma_fence_cb *cur, *tmp;
Maarten Lankhorste9417592014-07-01 12:57:14 +020071 int ret = 0;
72
Rob Clark78010cd2016-10-24 15:57:10 -040073 lockdep_assert_held(fence->lock);
74
Maarten Lankhorste9417592014-07-01 12:57:14 +020075 if (WARN_ON(!fence))
76 return -EINVAL;
77
78 if (!ktime_to_ns(fence->timestamp)) {
79 fence->timestamp = ktime_get();
80 smp_mb__before_atomic();
81 }
82
Chris Wilsonf54d1862016-10-25 13:00:45 +010083 if (test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
Maarten Lankhorste9417592014-07-01 12:57:14 +020084 ret = -EINVAL;
85
86 /*
Chris Wilsonf54d1862016-10-25 13:00:45 +010087 * we might have raced with the unlocked dma_fence_signal,
Maarten Lankhorste9417592014-07-01 12:57:14 +020088 * still run through all callbacks
89 */
90 } else
Chris Wilsonf54d1862016-10-25 13:00:45 +010091 trace_dma_fence_signaled(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +020092
93 list_for_each_entry_safe(cur, tmp, &fence->cb_list, node) {
94 list_del_init(&cur->node);
95 cur->func(fence, cur);
96 }
97 return ret;
98}
Chris Wilsonf54d1862016-10-25 13:00:45 +010099EXPORT_SYMBOL(dma_fence_signal_locked);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200100
101/**
Chris Wilsonf54d1862016-10-25 13:00:45 +0100102 * dma_fence_signal - signal completion of a fence
Maarten Lankhorste9417592014-07-01 12:57:14 +0200103 * @fence: the fence to signal
104 *
105 * Signal completion for software callbacks on a fence, this will unblock
Chris Wilsonf54d1862016-10-25 13:00:45 +0100106 * dma_fence_wait() calls and run all the callbacks added with
107 * dma_fence_add_callback(). Can be called multiple times, but since a fence
Maarten Lankhorste9417592014-07-01 12:57:14 +0200108 * can only go from unsignaled to signaled state, it will only be effective
109 * the first time.
110 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100111int dma_fence_signal(struct dma_fence *fence)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200112{
113 unsigned long flags;
114
115 if (!fence)
116 return -EINVAL;
117
118 if (!ktime_to_ns(fence->timestamp)) {
119 fence->timestamp = ktime_get();
120 smp_mb__before_atomic();
121 }
122
Chris Wilsonf54d1862016-10-25 13:00:45 +0100123 if (test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
Maarten Lankhorste9417592014-07-01 12:57:14 +0200124 return -EINVAL;
125
Chris Wilsonf54d1862016-10-25 13:00:45 +0100126 trace_dma_fence_signaled(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200127
Chris Wilsonf54d1862016-10-25 13:00:45 +0100128 if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags)) {
129 struct dma_fence_cb *cur, *tmp;
Maarten Lankhorste9417592014-07-01 12:57:14 +0200130
131 spin_lock_irqsave(fence->lock, flags);
132 list_for_each_entry_safe(cur, tmp, &fence->cb_list, node) {
133 list_del_init(&cur->node);
134 cur->func(fence, cur);
135 }
136 spin_unlock_irqrestore(fence->lock, flags);
137 }
138 return 0;
139}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100140EXPORT_SYMBOL(dma_fence_signal);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200141
142/**
Chris Wilsonf54d1862016-10-25 13:00:45 +0100143 * dma_fence_wait_timeout - sleep until the fence gets signaled
Maarten Lankhorste9417592014-07-01 12:57:14 +0200144 * or until timeout elapses
145 * @fence: [in] the fence to wait on
146 * @intr: [in] if true, do an interruptible wait
147 * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
148 *
149 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
150 * remaining timeout in jiffies on success. Other error values may be
151 * returned on custom implementations.
152 *
153 * Performs a synchronous wait on this fence. It is assumed the caller
154 * directly or indirectly (buf-mgr between reservation and committing)
155 * holds a reference to the fence, otherwise the fence might be
156 * freed before return, resulting in undefined behavior.
157 */
158signed long
Chris Wilsonf54d1862016-10-25 13:00:45 +0100159dma_fence_wait_timeout(struct dma_fence *fence, bool intr, signed long timeout)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200160{
161 signed long ret;
162
163 if (WARN_ON(timeout < 0))
164 return -EINVAL;
165
Chris Wilsonf54d1862016-10-25 13:00:45 +0100166 trace_dma_fence_wait_start(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200167 ret = fence->ops->wait(fence, intr, timeout);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100168 trace_dma_fence_wait_end(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200169 return ret;
170}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100171EXPORT_SYMBOL(dma_fence_wait_timeout);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200172
Chris Wilsonf54d1862016-10-25 13:00:45 +0100173void dma_fence_release(struct kref *kref)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200174{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100175 struct dma_fence *fence =
176 container_of(kref, struct dma_fence, refcount);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200177
Chris Wilsonf54d1862016-10-25 13:00:45 +0100178 trace_dma_fence_destroy(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200179
180 BUG_ON(!list_empty(&fence->cb_list));
181
182 if (fence->ops->release)
183 fence->ops->release(fence);
184 else
Chris Wilsonf54d1862016-10-25 13:00:45 +0100185 dma_fence_free(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200186}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100187EXPORT_SYMBOL(dma_fence_release);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200188
Chris Wilsonf54d1862016-10-25 13:00:45 +0100189void dma_fence_free(struct dma_fence *fence)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200190{
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200191 kfree_rcu(fence, rcu);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200192}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100193EXPORT_SYMBOL(dma_fence_free);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200194
195/**
Chris Wilsonf54d1862016-10-25 13:00:45 +0100196 * dma_fence_enable_sw_signaling - enable signaling on fence
Maarten Lankhorste9417592014-07-01 12:57:14 +0200197 * @fence: [in] the fence to enable
198 *
199 * this will request for sw signaling to be enabled, to make the fence
200 * complete as soon as possible
201 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100202void dma_fence_enable_sw_signaling(struct dma_fence *fence)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200203{
204 unsigned long flags;
205
Chris Wilsonf54d1862016-10-25 13:00:45 +0100206 if (!test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
207 &fence->flags) &&
208 !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
209 trace_dma_fence_enable_signal(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200210
211 spin_lock_irqsave(fence->lock, flags);
212
213 if (!fence->ops->enable_signaling(fence))
Chris Wilsonf54d1862016-10-25 13:00:45 +0100214 dma_fence_signal_locked(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200215
216 spin_unlock_irqrestore(fence->lock, flags);
217 }
218}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100219EXPORT_SYMBOL(dma_fence_enable_sw_signaling);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200220
221/**
Chris Wilsonf54d1862016-10-25 13:00:45 +0100222 * dma_fence_add_callback - add a callback to be called when the fence
Maarten Lankhorste9417592014-07-01 12:57:14 +0200223 * is signaled
224 * @fence: [in] the fence to wait on
225 * @cb: [in] the callback to register
226 * @func: [in] the function to call
227 *
Chris Wilsonf54d1862016-10-25 13:00:45 +0100228 * cb will be initialized by dma_fence_add_callback, no initialization
Maarten Lankhorste9417592014-07-01 12:57:14 +0200229 * by the caller is required. Any number of callbacks can be registered
230 * to a fence, but a callback can only be registered to one fence at a time.
231 *
232 * Note that the callback can be called from an atomic context. If
233 * fence is already signaled, this function will return -ENOENT (and
234 * *not* call the callback)
235 *
236 * Add a software callback to the fence. Same restrictions apply to
Chris Wilsonf54d1862016-10-25 13:00:45 +0100237 * refcount as it does to dma_fence_wait, however the caller doesn't need to
Maarten Lankhorste9417592014-07-01 12:57:14 +0200238 * keep a refcount to fence afterwards: when software access is enabled,
239 * the creator of the fence is required to keep the fence alive until
Chris Wilsonf54d1862016-10-25 13:00:45 +0100240 * after it signals with dma_fence_signal. The callback itself can be called
Maarten Lankhorste9417592014-07-01 12:57:14 +0200241 * from irq context.
242 *
Gustavo Padovanf642de12017-02-15 15:57:25 -0200243 * Returns 0 in case of success, -ENOENT if the fence is already signaled
244 * and -EINVAL in case of error.
Maarten Lankhorste9417592014-07-01 12:57:14 +0200245 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100246int dma_fence_add_callback(struct dma_fence *fence, struct dma_fence_cb *cb,
247 dma_fence_func_t func)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200248{
249 unsigned long flags;
250 int ret = 0;
251 bool was_set;
252
253 if (WARN_ON(!fence || !func))
254 return -EINVAL;
255
Chris Wilsonf54d1862016-10-25 13:00:45 +0100256 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
Maarten Lankhorste9417592014-07-01 12:57:14 +0200257 INIT_LIST_HEAD(&cb->node);
258 return -ENOENT;
259 }
260
261 spin_lock_irqsave(fence->lock, flags);
262
Chris Wilsonf54d1862016-10-25 13:00:45 +0100263 was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
264 &fence->flags);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200265
Chris Wilsonf54d1862016-10-25 13:00:45 +0100266 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
Maarten Lankhorste9417592014-07-01 12:57:14 +0200267 ret = -ENOENT;
268 else if (!was_set) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100269 trace_dma_fence_enable_signal(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200270
271 if (!fence->ops->enable_signaling(fence)) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100272 dma_fence_signal_locked(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200273 ret = -ENOENT;
274 }
275 }
276
277 if (!ret) {
278 cb->func = func;
279 list_add_tail(&cb->node, &fence->cb_list);
280 } else
281 INIT_LIST_HEAD(&cb->node);
282 spin_unlock_irqrestore(fence->lock, flags);
283
284 return ret;
285}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100286EXPORT_SYMBOL(dma_fence_add_callback);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200287
288/**
Chris Wilsond6c99f42017-01-04 14:12:21 +0000289 * dma_fence_get_status - returns the status upon completion
290 * @fence: [in] the dma_fence to query
291 *
292 * This wraps dma_fence_get_status_locked() to return the error status
293 * condition on a signaled fence. See dma_fence_get_status_locked() for more
294 * details.
295 *
296 * Returns 0 if the fence has not yet been signaled, 1 if the fence has
297 * been signaled without an error condition, or a negative error code
298 * if the fence has been completed in err.
299 */
300int dma_fence_get_status(struct dma_fence *fence)
301{
302 unsigned long flags;
303 int status;
304
305 spin_lock_irqsave(fence->lock, flags);
306 status = dma_fence_get_status_locked(fence);
307 spin_unlock_irqrestore(fence->lock, flags);
308
309 return status;
310}
311EXPORT_SYMBOL(dma_fence_get_status);
312
313/**
Chris Wilsonf54d1862016-10-25 13:00:45 +0100314 * dma_fence_remove_callback - remove a callback from the signaling list
Maarten Lankhorste9417592014-07-01 12:57:14 +0200315 * @fence: [in] the fence to wait on
316 * @cb: [in] the callback to remove
317 *
318 * Remove a previously queued callback from the fence. This function returns
Masanari Iidaf353d712014-10-22 00:00:14 +0900319 * true if the callback is successfully removed, or false if the fence has
Maarten Lankhorste9417592014-07-01 12:57:14 +0200320 * already been signaled.
321 *
322 * *WARNING*:
323 * Cancelling a callback should only be done if you really know what you're
324 * doing, since deadlocks and race conditions could occur all too easily. For
325 * this reason, it should only ever be done on hardware lockup recovery,
326 * with a reference held to the fence.
327 */
328bool
Chris Wilsonf54d1862016-10-25 13:00:45 +0100329dma_fence_remove_callback(struct dma_fence *fence, struct dma_fence_cb *cb)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200330{
331 unsigned long flags;
332 bool ret;
333
334 spin_lock_irqsave(fence->lock, flags);
335
336 ret = !list_empty(&cb->node);
337 if (ret)
338 list_del_init(&cb->node);
339
340 spin_unlock_irqrestore(fence->lock, flags);
341
342 return ret;
343}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100344EXPORT_SYMBOL(dma_fence_remove_callback);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200345
346struct default_wait_cb {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100347 struct dma_fence_cb base;
Maarten Lankhorste9417592014-07-01 12:57:14 +0200348 struct task_struct *task;
349};
350
351static void
Chris Wilsonf54d1862016-10-25 13:00:45 +0100352dma_fence_default_wait_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200353{
354 struct default_wait_cb *wait =
355 container_of(cb, struct default_wait_cb, base);
356
357 wake_up_state(wait->task, TASK_NORMAL);
358}
359
360/**
Chris Wilsonf54d1862016-10-25 13:00:45 +0100361 * dma_fence_default_wait - default sleep until the fence gets signaled
Maarten Lankhorste9417592014-07-01 12:57:14 +0200362 * or until timeout elapses
363 * @fence: [in] the fence to wait on
364 * @intr: [in] if true, do an interruptible wait
365 * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
366 *
367 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
Alex Deucherbcc004b2016-11-07 16:16:13 -0500368 * remaining timeout in jiffies on success. If timeout is zero the value one is
369 * returned if the fence is already signaled for consistency with other
370 * functions taking a jiffies timeout.
Maarten Lankhorste9417592014-07-01 12:57:14 +0200371 */
372signed long
Chris Wilsonf54d1862016-10-25 13:00:45 +0100373dma_fence_default_wait(struct dma_fence *fence, bool intr, signed long timeout)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200374{
375 struct default_wait_cb cb;
376 unsigned long flags;
Alex Deucherbcc004b2016-11-07 16:16:13 -0500377 signed long ret = timeout ? timeout : 1;
Maarten Lankhorste9417592014-07-01 12:57:14 +0200378 bool was_set;
379
Chris Wilsonf54d1862016-10-25 13:00:45 +0100380 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
Alex Deucherbcc004b2016-11-07 16:16:13 -0500381 return ret;
Maarten Lankhorste9417592014-07-01 12:57:14 +0200382
383 spin_lock_irqsave(fence->lock, flags);
384
385 if (intr && signal_pending(current)) {
386 ret = -ERESTARTSYS;
387 goto out;
388 }
389
Chris Wilsonf54d1862016-10-25 13:00:45 +0100390 was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
391 &fence->flags);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200392
Chris Wilsonf54d1862016-10-25 13:00:45 +0100393 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
Maarten Lankhorste9417592014-07-01 12:57:14 +0200394 goto out;
395
396 if (!was_set) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100397 trace_dma_fence_enable_signal(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200398
399 if (!fence->ops->enable_signaling(fence)) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100400 dma_fence_signal_locked(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200401 goto out;
402 }
403 }
404
Andres Rodriguez03c0c5f2017-04-26 10:46:20 -0400405 if (!timeout) {
406 ret = 0;
407 goto out;
408 }
409
Chris Wilsonf54d1862016-10-25 13:00:45 +0100410 cb.base.func = dma_fence_default_wait_cb;
Maarten Lankhorste9417592014-07-01 12:57:14 +0200411 cb.task = current;
412 list_add(&cb.base.node, &fence->cb_list);
413
Chris Wilsonf54d1862016-10-25 13:00:45 +0100414 while (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags) && ret > 0) {
Maarten Lankhorste9417592014-07-01 12:57:14 +0200415 if (intr)
416 __set_current_state(TASK_INTERRUPTIBLE);
417 else
418 __set_current_state(TASK_UNINTERRUPTIBLE);
419 spin_unlock_irqrestore(fence->lock, flags);
420
421 ret = schedule_timeout(ret);
422
423 spin_lock_irqsave(fence->lock, flags);
424 if (ret > 0 && intr && signal_pending(current))
425 ret = -ERESTARTSYS;
426 }
427
428 if (!list_empty(&cb.base.node))
429 list_del(&cb.base.node);
430 __set_current_state(TASK_RUNNING);
431
432out:
433 spin_unlock_irqrestore(fence->lock, flags);
434 return ret;
435}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100436EXPORT_SYMBOL(dma_fence_default_wait);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200437
Christian Königa5194352015-10-20 16:34:16 +0200438static bool
monk.liu7392b4b2016-11-04 16:16:09 -0400439dma_fence_test_signaled_any(struct dma_fence **fences, uint32_t count,
440 uint32_t *idx)
Christian Königa5194352015-10-20 16:34:16 +0200441{
442 int i;
443
444 for (i = 0; i < count; ++i) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100445 struct dma_fence *fence = fences[i];
monk.liu7392b4b2016-11-04 16:16:09 -0400446 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
447 if (idx)
448 *idx = i;
Christian Königa5194352015-10-20 16:34:16 +0200449 return true;
monk.liu7392b4b2016-11-04 16:16:09 -0400450 }
Christian Königa5194352015-10-20 16:34:16 +0200451 }
452 return false;
453}
454
455/**
Chris Wilsonf54d1862016-10-25 13:00:45 +0100456 * dma_fence_wait_any_timeout - sleep until any fence gets signaled
Christian Königa5194352015-10-20 16:34:16 +0200457 * or until timeout elapses
458 * @fences: [in] array of fences to wait on
459 * @count: [in] number of fences to wait on
460 * @intr: [in] if true, do an interruptible wait
461 * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
monk.liu7392b4b2016-11-04 16:16:09 -0400462 * @idx: [out] the first signaled fence index, meaningful only on
463 * positive return
Christian Königa5194352015-10-20 16:34:16 +0200464 *
465 * Returns -EINVAL on custom fence wait implementation, -ERESTARTSYS if
466 * interrupted, 0 if the wait timed out, or the remaining timeout in jiffies
467 * on success.
468 *
469 * Synchronous waits for the first fence in the array to be signaled. The
470 * caller needs to hold a reference to all fences in the array, otherwise a
471 * fence might be freed before return, resulting in undefined behavior.
472 */
473signed long
Chris Wilsonf54d1862016-10-25 13:00:45 +0100474dma_fence_wait_any_timeout(struct dma_fence **fences, uint32_t count,
monk.liu7392b4b2016-11-04 16:16:09 -0400475 bool intr, signed long timeout, uint32_t *idx)
Christian Königa5194352015-10-20 16:34:16 +0200476{
477 struct default_wait_cb *cb;
478 signed long ret = timeout;
479 unsigned i;
480
481 if (WARN_ON(!fences || !count || timeout < 0))
482 return -EINVAL;
483
484 if (timeout == 0) {
485 for (i = 0; i < count; ++i)
monk.liu7392b4b2016-11-04 16:16:09 -0400486 if (dma_fence_is_signaled(fences[i])) {
487 if (idx)
488 *idx = i;
Christian Königa5194352015-10-20 16:34:16 +0200489 return 1;
monk.liu7392b4b2016-11-04 16:16:09 -0400490 }
Christian Königa5194352015-10-20 16:34:16 +0200491
492 return 0;
493 }
494
495 cb = kcalloc(count, sizeof(struct default_wait_cb), GFP_KERNEL);
496 if (cb == NULL) {
497 ret = -ENOMEM;
498 goto err_free_cb;
499 }
500
501 for (i = 0; i < count; ++i) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100502 struct dma_fence *fence = fences[i];
Christian Königa5194352015-10-20 16:34:16 +0200503
Chris Wilsonf54d1862016-10-25 13:00:45 +0100504 if (fence->ops->wait != dma_fence_default_wait) {
Christian Königa5194352015-10-20 16:34:16 +0200505 ret = -EINVAL;
506 goto fence_rm_cb;
507 }
508
509 cb[i].task = current;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100510 if (dma_fence_add_callback(fence, &cb[i].base,
511 dma_fence_default_wait_cb)) {
Christian Königa5194352015-10-20 16:34:16 +0200512 /* This fence is already signaled */
monk.liu7392b4b2016-11-04 16:16:09 -0400513 if (idx)
514 *idx = i;
Christian Königa5194352015-10-20 16:34:16 +0200515 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
monk.liu7392b4b2016-11-04 16:16:09 -0400525 if (dma_fence_test_signaled_any(fences, count, idx))
Christian Königa5194352015-10-20 16:34:16 +0200526 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
536fence_rm_cb:
537 while (i-- > 0)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100538 dma_fence_remove_callback(fences[i], &cb[i].base);
Christian Königa5194352015-10-20 16:34:16 +0200539
540err_free_cb:
541 kfree(cb);
542
543 return ret;
544}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100545EXPORT_SYMBOL(dma_fence_wait_any_timeout);
Christian Königa5194352015-10-20 16:34:16 +0200546
Maarten Lankhorste9417592014-07-01 12:57:14 +0200547/**
Chris Wilsonf54d1862016-10-25 13:00:45 +0100548 * dma_fence_init - Initialize a custom fence.
Maarten Lankhorste9417592014-07-01 12:57:14 +0200549 * @fence: [in] the fence to initialize
Chris Wilsonf54d1862016-10-25 13:00:45 +0100550 * @ops: [in] the dma_fence_ops for operations on this fence
Maarten Lankhorste9417592014-07-01 12:57:14 +0200551 * @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
Chris Wilsonf54d1862016-10-25 13:00:45 +0100557 * refcount again if dma_fence_ops.enable_signaling gets called. This can
Maarten Lankhorste9417592014-07-01 12:57:14 +0200558 * be used for other implementing other types of fence.
559 *
560 * context and seqno are used for easy comparison between fences, allowing
Chris Wilsonf54d1862016-10-25 13:00:45 +0100561 * to check which fence is later by simply using dma_fence_later.
Maarten Lankhorste9417592014-07-01 12:57:14 +0200562 */
563void
Chris Wilsonf54d1862016-10-25 13:00:45 +0100564dma_fence_init(struct dma_fence *fence, const struct dma_fence_ops *ops,
565 spinlock_t *lock, u64 context, unsigned seqno)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200566{
567 BUG_ON(!lock);
568 BUG_ON(!ops || !ops->wait || !ops->enable_signaling ||
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;
Chris Wilsona009e972017-01-04 14:12:22 +0000578 fence->error = 0;
Maarten Lankhorste9417592014-07-01 12:57:14 +0200579
Chris Wilsonf54d1862016-10-25 13:00:45 +0100580 trace_dma_fence_init(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200581}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100582EXPORT_SYMBOL(dma_fence_init);