blob: 809b397e915f155e921ee353fb4e98cf5ee2590a [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>
Maarten Lankhorste9417592014-07-01 12:57:14 +020025
26#define CREATE_TRACE_POINTS
Chris Wilsonf54d1862016-10-25 13:00:45 +010027#include <trace/events/dma_fence.h>
Maarten Lankhorste9417592014-07-01 12:57:14 +020028
Chris Wilsonf54d1862016-10-25 13:00:45 +010029EXPORT_TRACEPOINT_SYMBOL(dma_fence_annotate_wait_on);
30EXPORT_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
Thierry Redinge9f3b792014-08-08 12:42:32 +020033/*
Maarten Lankhorste9417592014-07-01 12:57:14 +020034 * 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 */
Chris Wilsonf54d1862016-10-25 13:00:45 +010039static atomic64_t dma_fence_context_counter = ATOMIC64_INIT(0);
Maarten Lankhorste9417592014-07-01 12:57:14 +020040
41/**
Chris Wilsonf54d1862016-10-25 13:00:45 +010042 * dma_fence_context_alloc - allocate an array of fence contexts
Maarten Lankhorste9417592014-07-01 12:57:14 +020043 * @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 */
Chris Wilsonf54d1862016-10-25 13:00:45 +010048u64 dma_fence_context_alloc(unsigned num)
Maarten Lankhorste9417592014-07-01 12:57:14 +020049{
50 BUG_ON(!num);
Chris Wilsonf54d1862016-10-25 13:00:45 +010051 return atomic64_add_return(num, &dma_fence_context_counter) - num;
Maarten Lankhorste9417592014-07-01 12:57:14 +020052}
Chris Wilsonf54d1862016-10-25 13:00:45 +010053EXPORT_SYMBOL(dma_fence_context_alloc);
Maarten Lankhorste9417592014-07-01 12:57:14 +020054
55/**
Chris Wilsonf54d1862016-10-25 13:00:45 +010056 * dma_fence_signal_locked - signal completion of a fence
Maarten Lankhorste9417592014-07-01 12:57:14 +020057 * @fence: the fence to signal
58 *
59 * Signal completion for software callbacks on a fence, this will unblock
Chris Wilsonf54d1862016-10-25 13:00:45 +010060 * 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
Maarten Lankhorste9417592014-07-01 12:57:14 +020062 * can only go from unsignaled to signaled state, it will only be effective
63 * the first time.
64 *
Chris Wilsonf54d1862016-10-25 13:00:45 +010065 * Unlike dma_fence_signal, this function must be called with fence->lock held.
Maarten Lankhorste9417592014-07-01 12:57:14 +020066 */
Chris Wilsonf54d1862016-10-25 13:00:45 +010067int dma_fence_signal_locked(struct dma_fence *fence)
Maarten Lankhorste9417592014-07-01 12:57:14 +020068{
Chris Wilsonf54d1862016-10-25 13:00:45 +010069 struct dma_fence_cb *cur, *tmp;
Maarten Lankhorste9417592014-07-01 12:57:14 +020070 int ret = 0;
71
Rob Clark78010cd2016-10-24 15:57:10 -040072 lockdep_assert_held(fence->lock);
73
Maarten Lankhorste9417592014-07-01 12:57:14 +020074 if (WARN_ON(!fence))
75 return -EINVAL;
76
77 if (!ktime_to_ns(fence->timestamp)) {
78 fence->timestamp = ktime_get();
79 smp_mb__before_atomic();
80 }
81
Chris Wilsonf54d1862016-10-25 13:00:45 +010082 if (test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
Maarten Lankhorste9417592014-07-01 12:57:14 +020083 ret = -EINVAL;
84
85 /*
Chris Wilsonf54d1862016-10-25 13:00:45 +010086 * we might have raced with the unlocked dma_fence_signal,
Maarten Lankhorste9417592014-07-01 12:57:14 +020087 * still run through all callbacks
88 */
89 } else
Chris Wilsonf54d1862016-10-25 13:00:45 +010090 trace_dma_fence_signaled(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +020091
92 list_for_each_entry_safe(cur, tmp, &fence->cb_list, node) {
93 list_del_init(&cur->node);
94 cur->func(fence, cur);
95 }
96 return ret;
97}
Chris Wilsonf54d1862016-10-25 13:00:45 +010098EXPORT_SYMBOL(dma_fence_signal_locked);
Maarten Lankhorste9417592014-07-01 12:57:14 +020099
100/**
Chris Wilsonf54d1862016-10-25 13:00:45 +0100101 * dma_fence_signal - signal completion of a fence
Maarten Lankhorste9417592014-07-01 12:57:14 +0200102 * @fence: the fence to signal
103 *
104 * Signal completion for software callbacks on a fence, this will unblock
Chris Wilsonf54d1862016-10-25 13:00:45 +0100105 * dma_fence_wait() calls and run all the callbacks added with
106 * dma_fence_add_callback(). Can be called multiple times, but since a fence
Maarten Lankhorste9417592014-07-01 12:57:14 +0200107 * can only go from unsignaled to signaled state, it will only be effective
108 * the first time.
109 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100110int dma_fence_signal(struct dma_fence *fence)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200111{
112 unsigned long flags;
113
114 if (!fence)
115 return -EINVAL;
116
117 if (!ktime_to_ns(fence->timestamp)) {
118 fence->timestamp = ktime_get();
119 smp_mb__before_atomic();
120 }
121
Chris Wilsonf54d1862016-10-25 13:00:45 +0100122 if (test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
Maarten Lankhorste9417592014-07-01 12:57:14 +0200123 return -EINVAL;
124
Chris Wilsonf54d1862016-10-25 13:00:45 +0100125 trace_dma_fence_signaled(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200126
Chris Wilsonf54d1862016-10-25 13:00:45 +0100127 if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags)) {
128 struct dma_fence_cb *cur, *tmp;
Maarten Lankhorste9417592014-07-01 12:57:14 +0200129
130 spin_lock_irqsave(fence->lock, flags);
131 list_for_each_entry_safe(cur, tmp, &fence->cb_list, node) {
132 list_del_init(&cur->node);
133 cur->func(fence, cur);
134 }
135 spin_unlock_irqrestore(fence->lock, flags);
136 }
137 return 0;
138}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100139EXPORT_SYMBOL(dma_fence_signal);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200140
141/**
Chris Wilsonf54d1862016-10-25 13:00:45 +0100142 * dma_fence_wait_timeout - sleep until the fence gets signaled
Maarten Lankhorste9417592014-07-01 12:57:14 +0200143 * or until timeout elapses
144 * @fence: [in] the fence to wait on
145 * @intr: [in] if true, do an interruptible wait
146 * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
147 *
148 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
149 * remaining timeout in jiffies on success. Other error values may be
150 * returned on custom implementations.
151 *
152 * Performs a synchronous wait on this fence. It is assumed the caller
153 * directly or indirectly (buf-mgr between reservation and committing)
154 * holds a reference to the fence, otherwise the fence might be
155 * freed before return, resulting in undefined behavior.
156 */
157signed long
Chris Wilsonf54d1862016-10-25 13:00:45 +0100158dma_fence_wait_timeout(struct dma_fence *fence, bool intr, signed long timeout)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200159{
160 signed long ret;
161
162 if (WARN_ON(timeout < 0))
163 return -EINVAL;
164
Chris Wilsonf54d1862016-10-25 13:00:45 +0100165 trace_dma_fence_wait_start(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200166 ret = fence->ops->wait(fence, intr, timeout);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100167 trace_dma_fence_wait_end(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200168 return ret;
169}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100170EXPORT_SYMBOL(dma_fence_wait_timeout);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200171
Chris Wilsonf54d1862016-10-25 13:00:45 +0100172void dma_fence_release(struct kref *kref)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200173{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100174 struct dma_fence *fence =
175 container_of(kref, struct dma_fence, refcount);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200176
Chris Wilsonf54d1862016-10-25 13:00:45 +0100177 trace_dma_fence_destroy(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200178
179 BUG_ON(!list_empty(&fence->cb_list));
180
181 if (fence->ops->release)
182 fence->ops->release(fence);
183 else
Chris Wilsonf54d1862016-10-25 13:00:45 +0100184 dma_fence_free(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200185}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100186EXPORT_SYMBOL(dma_fence_release);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200187
Chris Wilsonf54d1862016-10-25 13:00:45 +0100188void dma_fence_free(struct dma_fence *fence)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200189{
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200190 kfree_rcu(fence, rcu);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200191}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100192EXPORT_SYMBOL(dma_fence_free);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200193
194/**
Chris Wilsonf54d1862016-10-25 13:00:45 +0100195 * dma_fence_enable_sw_signaling - enable signaling on fence
Maarten Lankhorste9417592014-07-01 12:57:14 +0200196 * @fence: [in] the fence to enable
197 *
198 * this will request for sw signaling to be enabled, to make the fence
199 * complete as soon as possible
200 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100201void dma_fence_enable_sw_signaling(struct dma_fence *fence)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200202{
203 unsigned long flags;
204
Chris Wilsonf54d1862016-10-25 13:00:45 +0100205 if (!test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
206 &fence->flags) &&
207 !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
208 trace_dma_fence_enable_signal(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200209
210 spin_lock_irqsave(fence->lock, flags);
211
212 if (!fence->ops->enable_signaling(fence))
Chris Wilsonf54d1862016-10-25 13:00:45 +0100213 dma_fence_signal_locked(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200214
215 spin_unlock_irqrestore(fence->lock, flags);
216 }
217}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100218EXPORT_SYMBOL(dma_fence_enable_sw_signaling);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200219
220/**
Chris Wilsonf54d1862016-10-25 13:00:45 +0100221 * dma_fence_add_callback - add a callback to be called when the fence
Maarten Lankhorste9417592014-07-01 12:57:14 +0200222 * is signaled
223 * @fence: [in] the fence to wait on
224 * @cb: [in] the callback to register
225 * @func: [in] the function to call
226 *
Chris Wilsonf54d1862016-10-25 13:00:45 +0100227 * cb will be initialized by dma_fence_add_callback, no initialization
Maarten Lankhorste9417592014-07-01 12:57:14 +0200228 * by the caller is required. Any number of callbacks can be registered
229 * to a fence, but a callback can only be registered to one fence at a time.
230 *
231 * Note that the callback can be called from an atomic context. If
232 * fence is already signaled, this function will return -ENOENT (and
233 * *not* call the callback)
234 *
235 * Add a software callback to the fence. Same restrictions apply to
Chris Wilsonf54d1862016-10-25 13:00:45 +0100236 * refcount as it does to dma_fence_wait, however the caller doesn't need to
Maarten Lankhorste9417592014-07-01 12:57:14 +0200237 * keep a refcount to fence afterwards: when software access is enabled,
238 * the creator of the fence is required to keep the fence alive until
Chris Wilsonf54d1862016-10-25 13:00:45 +0100239 * after it signals with dma_fence_signal. The callback itself can be called
Maarten Lankhorste9417592014-07-01 12:57:14 +0200240 * from irq context.
241 *
Gustavo Padovanf642de12017-02-15 15:57:25 -0200242 * Returns 0 in case of success, -ENOENT if the fence is already signaled
243 * and -EINVAL in case of error.
Maarten Lankhorste9417592014-07-01 12:57:14 +0200244 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100245int dma_fence_add_callback(struct dma_fence *fence, struct dma_fence_cb *cb,
246 dma_fence_func_t func)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200247{
248 unsigned long flags;
249 int ret = 0;
250 bool was_set;
251
252 if (WARN_ON(!fence || !func))
253 return -EINVAL;
254
Chris Wilsonf54d1862016-10-25 13:00:45 +0100255 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
Maarten Lankhorste9417592014-07-01 12:57:14 +0200256 INIT_LIST_HEAD(&cb->node);
257 return -ENOENT;
258 }
259
260 spin_lock_irqsave(fence->lock, flags);
261
Chris Wilsonf54d1862016-10-25 13:00:45 +0100262 was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
263 &fence->flags);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200264
Chris Wilsonf54d1862016-10-25 13:00:45 +0100265 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
Maarten Lankhorste9417592014-07-01 12:57:14 +0200266 ret = -ENOENT;
267 else if (!was_set) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100268 trace_dma_fence_enable_signal(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200269
270 if (!fence->ops->enable_signaling(fence)) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100271 dma_fence_signal_locked(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200272 ret = -ENOENT;
273 }
274 }
275
276 if (!ret) {
277 cb->func = func;
278 list_add_tail(&cb->node, &fence->cb_list);
279 } else
280 INIT_LIST_HEAD(&cb->node);
281 spin_unlock_irqrestore(fence->lock, flags);
282
283 return ret;
284}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100285EXPORT_SYMBOL(dma_fence_add_callback);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200286
287/**
Chris Wilsond6c99f42017-01-04 14:12:21 +0000288 * dma_fence_get_status - returns the status upon completion
289 * @fence: [in] the dma_fence to query
290 *
291 * This wraps dma_fence_get_status_locked() to return the error status
292 * condition on a signaled fence. See dma_fence_get_status_locked() for more
293 * details.
294 *
295 * Returns 0 if the fence has not yet been signaled, 1 if the fence has
296 * been signaled without an error condition, or a negative error code
297 * if the fence has been completed in err.
298 */
299int dma_fence_get_status(struct dma_fence *fence)
300{
301 unsigned long flags;
302 int status;
303
304 spin_lock_irqsave(fence->lock, flags);
305 status = dma_fence_get_status_locked(fence);
306 spin_unlock_irqrestore(fence->lock, flags);
307
308 return status;
309}
310EXPORT_SYMBOL(dma_fence_get_status);
311
312/**
Chris Wilsonf54d1862016-10-25 13:00:45 +0100313 * dma_fence_remove_callback - remove a callback from the signaling list
Maarten Lankhorste9417592014-07-01 12:57:14 +0200314 * @fence: [in] the fence to wait on
315 * @cb: [in] the callback to remove
316 *
317 * Remove a previously queued callback from the fence. This function returns
Masanari Iidaf353d712014-10-22 00:00:14 +0900318 * true if the callback is successfully removed, or false if the fence has
Maarten Lankhorste9417592014-07-01 12:57:14 +0200319 * already been signaled.
320 *
321 * *WARNING*:
322 * Cancelling a callback should only be done if you really know what you're
323 * doing, since deadlocks and race conditions could occur all too easily. For
324 * this reason, it should only ever be done on hardware lockup recovery,
325 * with a reference held to the fence.
326 */
327bool
Chris Wilsonf54d1862016-10-25 13:00:45 +0100328dma_fence_remove_callback(struct dma_fence *fence, struct dma_fence_cb *cb)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200329{
330 unsigned long flags;
331 bool ret;
332
333 spin_lock_irqsave(fence->lock, flags);
334
335 ret = !list_empty(&cb->node);
336 if (ret)
337 list_del_init(&cb->node);
338
339 spin_unlock_irqrestore(fence->lock, flags);
340
341 return ret;
342}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100343EXPORT_SYMBOL(dma_fence_remove_callback);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200344
345struct default_wait_cb {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100346 struct dma_fence_cb base;
Maarten Lankhorste9417592014-07-01 12:57:14 +0200347 struct task_struct *task;
348};
349
350static void
Chris Wilsonf54d1862016-10-25 13:00:45 +0100351dma_fence_default_wait_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200352{
353 struct default_wait_cb *wait =
354 container_of(cb, struct default_wait_cb, base);
355
356 wake_up_state(wait->task, TASK_NORMAL);
357}
358
359/**
Chris Wilsonf54d1862016-10-25 13:00:45 +0100360 * dma_fence_default_wait - default sleep until the fence gets signaled
Maarten Lankhorste9417592014-07-01 12:57:14 +0200361 * or until timeout elapses
362 * @fence: [in] the fence to wait on
363 * @intr: [in] if true, do an interruptible wait
364 * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
365 *
366 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
Alex Deucherbcc004b2016-11-07 16:16:13 -0500367 * remaining timeout in jiffies on success. If timeout is zero the value one is
368 * returned if the fence is already signaled for consistency with other
369 * functions taking a jiffies timeout.
Maarten Lankhorste9417592014-07-01 12:57:14 +0200370 */
371signed long
Chris Wilsonf54d1862016-10-25 13:00:45 +0100372dma_fence_default_wait(struct dma_fence *fence, bool intr, signed long timeout)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200373{
374 struct default_wait_cb cb;
375 unsigned long flags;
Alex Deucherbcc004b2016-11-07 16:16:13 -0500376 signed long ret = timeout ? timeout : 1;
Maarten Lankhorste9417592014-07-01 12:57:14 +0200377 bool was_set;
378
Chris Wilsonf54d1862016-10-25 13:00:45 +0100379 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
Alex Deucherbcc004b2016-11-07 16:16:13 -0500380 return ret;
Maarten Lankhorste9417592014-07-01 12:57:14 +0200381
382 spin_lock_irqsave(fence->lock, flags);
383
384 if (intr && signal_pending(current)) {
385 ret = -ERESTARTSYS;
386 goto out;
387 }
388
Chris Wilsonf54d1862016-10-25 13:00:45 +0100389 was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
390 &fence->flags);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200391
Chris Wilsonf54d1862016-10-25 13:00:45 +0100392 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
Maarten Lankhorste9417592014-07-01 12:57:14 +0200393 goto out;
394
395 if (!was_set) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100396 trace_dma_fence_enable_signal(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200397
398 if (!fence->ops->enable_signaling(fence)) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100399 dma_fence_signal_locked(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200400 goto out;
401 }
402 }
403
Chris Wilsonf54d1862016-10-25 13:00:45 +0100404 cb.base.func = dma_fence_default_wait_cb;
Maarten Lankhorste9417592014-07-01 12:57:14 +0200405 cb.task = current;
406 list_add(&cb.base.node, &fence->cb_list);
407
Chris Wilsonf54d1862016-10-25 13:00:45 +0100408 while (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags) && ret > 0) {
Maarten Lankhorste9417592014-07-01 12:57:14 +0200409 if (intr)
410 __set_current_state(TASK_INTERRUPTIBLE);
411 else
412 __set_current_state(TASK_UNINTERRUPTIBLE);
413 spin_unlock_irqrestore(fence->lock, flags);
414
415 ret = schedule_timeout(ret);
416
417 spin_lock_irqsave(fence->lock, flags);
418 if (ret > 0 && intr && signal_pending(current))
419 ret = -ERESTARTSYS;
420 }
421
422 if (!list_empty(&cb.base.node))
423 list_del(&cb.base.node);
424 __set_current_state(TASK_RUNNING);
425
426out:
427 spin_unlock_irqrestore(fence->lock, flags);
428 return ret;
429}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100430EXPORT_SYMBOL(dma_fence_default_wait);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200431
Christian Königa5194352015-10-20 16:34:16 +0200432static bool
monk.liu7392b4b2016-11-04 16:16:09 -0400433dma_fence_test_signaled_any(struct dma_fence **fences, uint32_t count,
434 uint32_t *idx)
Christian Königa5194352015-10-20 16:34:16 +0200435{
436 int i;
437
438 for (i = 0; i < count; ++i) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100439 struct dma_fence *fence = fences[i];
monk.liu7392b4b2016-11-04 16:16:09 -0400440 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
441 if (idx)
442 *idx = i;
Christian Königa5194352015-10-20 16:34:16 +0200443 return true;
monk.liu7392b4b2016-11-04 16:16:09 -0400444 }
Christian Königa5194352015-10-20 16:34:16 +0200445 }
446 return false;
447}
448
449/**
Chris Wilsonf54d1862016-10-25 13:00:45 +0100450 * dma_fence_wait_any_timeout - sleep until any fence gets signaled
Christian Königa5194352015-10-20 16:34:16 +0200451 * or until timeout elapses
452 * @fences: [in] array of fences to wait on
453 * @count: [in] number of fences to wait on
454 * @intr: [in] if true, do an interruptible wait
455 * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
monk.liu7392b4b2016-11-04 16:16:09 -0400456 * @idx: [out] the first signaled fence index, meaningful only on
457 * positive return
Christian Königa5194352015-10-20 16:34:16 +0200458 *
459 * Returns -EINVAL on custom fence wait implementation, -ERESTARTSYS if
460 * interrupted, 0 if the wait timed out, or the remaining timeout in jiffies
461 * on success.
462 *
463 * Synchronous waits for the first fence in the array to be signaled. The
464 * caller needs to hold a reference to all fences in the array, otherwise a
465 * fence might be freed before return, resulting in undefined behavior.
466 */
467signed long
Chris Wilsonf54d1862016-10-25 13:00:45 +0100468dma_fence_wait_any_timeout(struct dma_fence **fences, uint32_t count,
monk.liu7392b4b2016-11-04 16:16:09 -0400469 bool intr, signed long timeout, uint32_t *idx)
Christian Königa5194352015-10-20 16:34:16 +0200470{
471 struct default_wait_cb *cb;
472 signed long ret = timeout;
473 unsigned i;
474
475 if (WARN_ON(!fences || !count || timeout < 0))
476 return -EINVAL;
477
478 if (timeout == 0) {
479 for (i = 0; i < count; ++i)
monk.liu7392b4b2016-11-04 16:16:09 -0400480 if (dma_fence_is_signaled(fences[i])) {
481 if (idx)
482 *idx = i;
Christian Königa5194352015-10-20 16:34:16 +0200483 return 1;
monk.liu7392b4b2016-11-04 16:16:09 -0400484 }
Christian Königa5194352015-10-20 16:34:16 +0200485
486 return 0;
487 }
488
489 cb = kcalloc(count, sizeof(struct default_wait_cb), GFP_KERNEL);
490 if (cb == NULL) {
491 ret = -ENOMEM;
492 goto err_free_cb;
493 }
494
495 for (i = 0; i < count; ++i) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100496 struct dma_fence *fence = fences[i];
Christian Königa5194352015-10-20 16:34:16 +0200497
Chris Wilsonf54d1862016-10-25 13:00:45 +0100498 if (fence->ops->wait != dma_fence_default_wait) {
Christian Königa5194352015-10-20 16:34:16 +0200499 ret = -EINVAL;
500 goto fence_rm_cb;
501 }
502
503 cb[i].task = current;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100504 if (dma_fence_add_callback(fence, &cb[i].base,
505 dma_fence_default_wait_cb)) {
Christian Königa5194352015-10-20 16:34:16 +0200506 /* This fence is already signaled */
monk.liu7392b4b2016-11-04 16:16:09 -0400507 if (idx)
508 *idx = i;
Christian Königa5194352015-10-20 16:34:16 +0200509 goto fence_rm_cb;
510 }
511 }
512
513 while (ret > 0) {
514 if (intr)
515 set_current_state(TASK_INTERRUPTIBLE);
516 else
517 set_current_state(TASK_UNINTERRUPTIBLE);
518
monk.liu7392b4b2016-11-04 16:16:09 -0400519 if (dma_fence_test_signaled_any(fences, count, idx))
Christian Königa5194352015-10-20 16:34:16 +0200520 break;
521
522 ret = schedule_timeout(ret);
523
524 if (ret > 0 && intr && signal_pending(current))
525 ret = -ERESTARTSYS;
526 }
527
528 __set_current_state(TASK_RUNNING);
529
530fence_rm_cb:
531 while (i-- > 0)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100532 dma_fence_remove_callback(fences[i], &cb[i].base);
Christian Königa5194352015-10-20 16:34:16 +0200533
534err_free_cb:
535 kfree(cb);
536
537 return ret;
538}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100539EXPORT_SYMBOL(dma_fence_wait_any_timeout);
Christian Königa5194352015-10-20 16:34:16 +0200540
Maarten Lankhorste9417592014-07-01 12:57:14 +0200541/**
Chris Wilsonf54d1862016-10-25 13:00:45 +0100542 * dma_fence_init - Initialize a custom fence.
Maarten Lankhorste9417592014-07-01 12:57:14 +0200543 * @fence: [in] the fence to initialize
Chris Wilsonf54d1862016-10-25 13:00:45 +0100544 * @ops: [in] the dma_fence_ops for operations on this fence
Maarten Lankhorste9417592014-07-01 12:57:14 +0200545 * @lock: [in] the irqsafe spinlock to use for locking this fence
546 * @context: [in] the execution context this fence is run on
547 * @seqno: [in] a linear increasing sequence number for this context
548 *
549 * Initializes an allocated fence, the caller doesn't have to keep its
550 * refcount after committing with this fence, but it will need to hold a
Chris Wilsonf54d1862016-10-25 13:00:45 +0100551 * refcount again if dma_fence_ops.enable_signaling gets called. This can
Maarten Lankhorste9417592014-07-01 12:57:14 +0200552 * be used for other implementing other types of fence.
553 *
554 * context and seqno are used for easy comparison between fences, allowing
Chris Wilsonf54d1862016-10-25 13:00:45 +0100555 * to check which fence is later by simply using dma_fence_later.
Maarten Lankhorste9417592014-07-01 12:57:14 +0200556 */
557void
Chris Wilsonf54d1862016-10-25 13:00:45 +0100558dma_fence_init(struct dma_fence *fence, const struct dma_fence_ops *ops,
559 spinlock_t *lock, u64 context, unsigned seqno)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200560{
561 BUG_ON(!lock);
562 BUG_ON(!ops || !ops->wait || !ops->enable_signaling ||
563 !ops->get_driver_name || !ops->get_timeline_name);
564
565 kref_init(&fence->refcount);
566 fence->ops = ops;
567 INIT_LIST_HEAD(&fence->cb_list);
568 fence->lock = lock;
569 fence->context = context;
570 fence->seqno = seqno;
571 fence->flags = 0UL;
Chris Wilsona009e972017-01-04 14:12:22 +0000572 fence->error = 0;
Maarten Lankhorste9417592014-07-01 12:57:14 +0200573
Chris Wilsonf54d1862016-10-25 13:00:45 +0100574 trace_dma_fence_init(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200575}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100576EXPORT_SYMBOL(dma_fence_init);