blob: 5e66dfece03cb8c928e490be4aa1384d8b6f9ad4 [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);
Maarten Lankhorste9417592014-07-01 12:57:14 +020031
Thierry Redinge9f3b792014-08-08 12:42:32 +020032/*
Maarten Lankhorste9417592014-07-01 12:57:14 +020033 * fence context counter: each execution context should have its own
34 * fence context, this allows checking if fences belong to the same
35 * context or not. One device can have multiple separate contexts,
36 * and they're used if some engine can run independently of another.
37 */
Chris Wilsonf54d1862016-10-25 13:00:45 +010038static atomic64_t dma_fence_context_counter = ATOMIC64_INIT(0);
Maarten Lankhorste9417592014-07-01 12:57:14 +020039
40/**
Chris Wilsonf54d1862016-10-25 13:00:45 +010041 * dma_fence_context_alloc - allocate an array of fence contexts
Maarten Lankhorste9417592014-07-01 12:57:14 +020042 * @num: [in] amount of contexts to allocate
43 *
44 * This function will return the first index of the number of fences allocated.
45 * The fence context is used for setting fence->context to a unique number.
46 */
Chris Wilsonf54d1862016-10-25 13:00:45 +010047u64 dma_fence_context_alloc(unsigned num)
Maarten Lankhorste9417592014-07-01 12:57:14 +020048{
49 BUG_ON(!num);
Chris Wilsonf54d1862016-10-25 13:00:45 +010050 return atomic64_add_return(num, &dma_fence_context_counter) - num;
Maarten Lankhorste9417592014-07-01 12:57:14 +020051}
Chris Wilsonf54d1862016-10-25 13:00:45 +010052EXPORT_SYMBOL(dma_fence_context_alloc);
Maarten Lankhorste9417592014-07-01 12:57:14 +020053
54/**
Chris Wilsonf54d1862016-10-25 13:00:45 +010055 * dma_fence_signal_locked - signal completion of a fence
Maarten Lankhorste9417592014-07-01 12:57:14 +020056 * @fence: the fence to signal
57 *
58 * Signal completion for software callbacks on a fence, this will unblock
Chris Wilsonf54d1862016-10-25 13:00:45 +010059 * dma_fence_wait() calls and run all the callbacks added with
60 * dma_fence_add_callback(). Can be called multiple times, but since a fence
Maarten Lankhorste9417592014-07-01 12:57:14 +020061 * can only go from unsignaled to signaled state, it will only be effective
62 * the first time.
63 *
Chris Wilsonf54d1862016-10-25 13:00:45 +010064 * Unlike dma_fence_signal, this function must be called with fence->lock held.
Maarten Lankhorste9417592014-07-01 12:57:14 +020065 */
Chris Wilsonf54d1862016-10-25 13:00:45 +010066int dma_fence_signal_locked(struct dma_fence *fence)
Maarten Lankhorste9417592014-07-01 12:57:14 +020067{
Chris Wilsonf54d1862016-10-25 13:00:45 +010068 struct dma_fence_cb *cur, *tmp;
Maarten Lankhorste9417592014-07-01 12:57:14 +020069 int ret = 0;
70
Rob Clark78010cd2016-10-24 15:57:10 -040071 lockdep_assert_held(fence->lock);
72
Maarten Lankhorste9417592014-07-01 12:57:14 +020073 if (WARN_ON(!fence))
74 return -EINVAL;
75
76 if (!ktime_to_ns(fence->timestamp)) {
77 fence->timestamp = ktime_get();
78 smp_mb__before_atomic();
79 }
80
Chris Wilsonf54d1862016-10-25 13:00:45 +010081 if (test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
Maarten Lankhorste9417592014-07-01 12:57:14 +020082 ret = -EINVAL;
83
84 /*
Chris Wilsonf54d1862016-10-25 13:00:45 +010085 * we might have raced with the unlocked dma_fence_signal,
Maarten Lankhorste9417592014-07-01 12:57:14 +020086 * still run through all callbacks
87 */
88 } else
Chris Wilsonf54d1862016-10-25 13:00:45 +010089 trace_dma_fence_signaled(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +020090
91 list_for_each_entry_safe(cur, tmp, &fence->cb_list, node) {
92 list_del_init(&cur->node);
93 cur->func(fence, cur);
94 }
95 return ret;
96}
Chris Wilsonf54d1862016-10-25 13:00:45 +010097EXPORT_SYMBOL(dma_fence_signal_locked);
Maarten Lankhorste9417592014-07-01 12:57:14 +020098
99/**
Chris Wilsonf54d1862016-10-25 13:00:45 +0100100 * dma_fence_signal - signal completion of a fence
Maarten Lankhorste9417592014-07-01 12:57:14 +0200101 * @fence: the fence to signal
102 *
103 * Signal completion for software callbacks on a fence, this will unblock
Chris Wilsonf54d1862016-10-25 13:00:45 +0100104 * dma_fence_wait() calls and run all the callbacks added with
105 * dma_fence_add_callback(). Can be called multiple times, but since a fence
Maarten Lankhorste9417592014-07-01 12:57:14 +0200106 * can only go from unsignaled to signaled state, it will only be effective
107 * the first time.
108 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100109int dma_fence_signal(struct dma_fence *fence)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200110{
111 unsigned long flags;
112
113 if (!fence)
114 return -EINVAL;
115
116 if (!ktime_to_ns(fence->timestamp)) {
117 fence->timestamp = ktime_get();
118 smp_mb__before_atomic();
119 }
120
Chris Wilsonf54d1862016-10-25 13:00:45 +0100121 if (test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
Maarten Lankhorste9417592014-07-01 12:57:14 +0200122 return -EINVAL;
123
Chris Wilsonf54d1862016-10-25 13:00:45 +0100124 trace_dma_fence_signaled(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200125
Chris Wilsonf54d1862016-10-25 13:00:45 +0100126 if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags)) {
127 struct dma_fence_cb *cur, *tmp;
Maarten Lankhorste9417592014-07-01 12:57:14 +0200128
129 spin_lock_irqsave(fence->lock, flags);
130 list_for_each_entry_safe(cur, tmp, &fence->cb_list, node) {
131 list_del_init(&cur->node);
132 cur->func(fence, cur);
133 }
134 spin_unlock_irqrestore(fence->lock, flags);
135 }
136 return 0;
137}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100138EXPORT_SYMBOL(dma_fence_signal);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200139
140/**
Chris Wilsonf54d1862016-10-25 13:00:45 +0100141 * dma_fence_wait_timeout - sleep until the fence gets signaled
Maarten Lankhorste9417592014-07-01 12:57:14 +0200142 * or until timeout elapses
143 * @fence: [in] the fence to wait on
144 * @intr: [in] if true, do an interruptible wait
145 * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
146 *
147 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
148 * remaining timeout in jiffies on success. Other error values may be
149 * returned on custom implementations.
150 *
151 * Performs a synchronous wait on this fence. It is assumed the caller
152 * directly or indirectly (buf-mgr between reservation and committing)
153 * holds a reference to the fence, otherwise the fence might be
154 * freed before return, resulting in undefined behavior.
155 */
156signed long
Chris Wilsonf54d1862016-10-25 13:00:45 +0100157dma_fence_wait_timeout(struct dma_fence *fence, bool intr, signed long timeout)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200158{
159 signed long ret;
160
161 if (WARN_ON(timeout < 0))
162 return -EINVAL;
163
Chris Wilsonf54d1862016-10-25 13:00:45 +0100164 trace_dma_fence_wait_start(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200165 ret = fence->ops->wait(fence, intr, timeout);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100166 trace_dma_fence_wait_end(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200167 return ret;
168}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100169EXPORT_SYMBOL(dma_fence_wait_timeout);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200170
Chris Wilsonf54d1862016-10-25 13:00:45 +0100171void dma_fence_release(struct kref *kref)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200172{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100173 struct dma_fence *fence =
174 container_of(kref, struct dma_fence, refcount);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200175
Chris Wilsonf54d1862016-10-25 13:00:45 +0100176 trace_dma_fence_destroy(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200177
178 BUG_ON(!list_empty(&fence->cb_list));
179
180 if (fence->ops->release)
181 fence->ops->release(fence);
182 else
Chris Wilsonf54d1862016-10-25 13:00:45 +0100183 dma_fence_free(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200184}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100185EXPORT_SYMBOL(dma_fence_release);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200186
Chris Wilsonf54d1862016-10-25 13:00:45 +0100187void dma_fence_free(struct dma_fence *fence)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200188{
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200189 kfree_rcu(fence, rcu);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200190}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100191EXPORT_SYMBOL(dma_fence_free);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200192
193/**
Chris Wilsonf54d1862016-10-25 13:00:45 +0100194 * dma_fence_enable_sw_signaling - enable signaling on fence
Maarten Lankhorste9417592014-07-01 12:57:14 +0200195 * @fence: [in] the fence to enable
196 *
197 * this will request for sw signaling to be enabled, to make the fence
198 * complete as soon as possible
199 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100200void dma_fence_enable_sw_signaling(struct dma_fence *fence)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200201{
202 unsigned long flags;
203
Chris Wilsonf54d1862016-10-25 13:00:45 +0100204 if (!test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
205 &fence->flags) &&
206 !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
207 trace_dma_fence_enable_signal(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200208
209 spin_lock_irqsave(fence->lock, flags);
210
211 if (!fence->ops->enable_signaling(fence))
Chris Wilsonf54d1862016-10-25 13:00:45 +0100212 dma_fence_signal_locked(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200213
214 spin_unlock_irqrestore(fence->lock, flags);
215 }
216}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100217EXPORT_SYMBOL(dma_fence_enable_sw_signaling);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200218
219/**
Chris Wilsonf54d1862016-10-25 13:00:45 +0100220 * dma_fence_add_callback - add a callback to be called when the fence
Maarten Lankhorste9417592014-07-01 12:57:14 +0200221 * is signaled
222 * @fence: [in] the fence to wait on
223 * @cb: [in] the callback to register
224 * @func: [in] the function to call
225 *
Chris Wilsonf54d1862016-10-25 13:00:45 +0100226 * cb will be initialized by dma_fence_add_callback, no initialization
Maarten Lankhorste9417592014-07-01 12:57:14 +0200227 * by the caller is required. Any number of callbacks can be registered
228 * to a fence, but a callback can only be registered to one fence at a time.
229 *
230 * Note that the callback can be called from an atomic context. If
231 * fence is already signaled, this function will return -ENOENT (and
232 * *not* call the callback)
233 *
234 * Add a software callback to the fence. Same restrictions apply to
Chris Wilsonf54d1862016-10-25 13:00:45 +0100235 * refcount as it does to dma_fence_wait, however the caller doesn't need to
Maarten Lankhorste9417592014-07-01 12:57:14 +0200236 * keep a refcount to fence afterwards: when software access is enabled,
237 * the creator of the fence is required to keep the fence alive until
Chris Wilsonf54d1862016-10-25 13:00:45 +0100238 * after it signals with dma_fence_signal. The callback itself can be called
Maarten Lankhorste9417592014-07-01 12:57:14 +0200239 * from irq context.
240 *
241 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100242int dma_fence_add_callback(struct dma_fence *fence, struct dma_fence_cb *cb,
243 dma_fence_func_t func)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200244{
245 unsigned long flags;
246 int ret = 0;
247 bool was_set;
248
249 if (WARN_ON(!fence || !func))
250 return -EINVAL;
251
Chris Wilsonf54d1862016-10-25 13:00:45 +0100252 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
Maarten Lankhorste9417592014-07-01 12:57:14 +0200253 INIT_LIST_HEAD(&cb->node);
254 return -ENOENT;
255 }
256
257 spin_lock_irqsave(fence->lock, flags);
258
Chris Wilsonf54d1862016-10-25 13:00:45 +0100259 was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
260 &fence->flags);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200261
Chris Wilsonf54d1862016-10-25 13:00:45 +0100262 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
Maarten Lankhorste9417592014-07-01 12:57:14 +0200263 ret = -ENOENT;
264 else if (!was_set) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100265 trace_dma_fence_enable_signal(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200266
267 if (!fence->ops->enable_signaling(fence)) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100268 dma_fence_signal_locked(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200269 ret = -ENOENT;
270 }
271 }
272
273 if (!ret) {
274 cb->func = func;
275 list_add_tail(&cb->node, &fence->cb_list);
276 } else
277 INIT_LIST_HEAD(&cb->node);
278 spin_unlock_irqrestore(fence->lock, flags);
279
280 return ret;
281}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100282EXPORT_SYMBOL(dma_fence_add_callback);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200283
284/**
Chris Wilsond6c99f42017-01-04 14:12:21 +0000285 * dma_fence_get_status - returns the status upon completion
286 * @fence: [in] the dma_fence to query
287 *
288 * This wraps dma_fence_get_status_locked() to return the error status
289 * condition on a signaled fence. See dma_fence_get_status_locked() for more
290 * details.
291 *
292 * Returns 0 if the fence has not yet been signaled, 1 if the fence has
293 * been signaled without an error condition, or a negative error code
294 * if the fence has been completed in err.
295 */
296int dma_fence_get_status(struct dma_fence *fence)
297{
298 unsigned long flags;
299 int status;
300
301 spin_lock_irqsave(fence->lock, flags);
302 status = dma_fence_get_status_locked(fence);
303 spin_unlock_irqrestore(fence->lock, flags);
304
305 return status;
306}
307EXPORT_SYMBOL(dma_fence_get_status);
308
309/**
Chris Wilsonf54d1862016-10-25 13:00:45 +0100310 * dma_fence_remove_callback - remove a callback from the signaling list
Maarten Lankhorste9417592014-07-01 12:57:14 +0200311 * @fence: [in] the fence to wait on
312 * @cb: [in] the callback to remove
313 *
314 * Remove a previously queued callback from the fence. This function returns
Masanari Iidaf353d712014-10-22 00:00:14 +0900315 * true if the callback is successfully removed, or false if the fence has
Maarten Lankhorste9417592014-07-01 12:57:14 +0200316 * already been signaled.
317 *
318 * *WARNING*:
319 * Cancelling a callback should only be done if you really know what you're
320 * doing, since deadlocks and race conditions could occur all too easily. For
321 * this reason, it should only ever be done on hardware lockup recovery,
322 * with a reference held to the fence.
323 */
324bool
Chris Wilsonf54d1862016-10-25 13:00:45 +0100325dma_fence_remove_callback(struct dma_fence *fence, struct dma_fence_cb *cb)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200326{
327 unsigned long flags;
328 bool ret;
329
330 spin_lock_irqsave(fence->lock, flags);
331
332 ret = !list_empty(&cb->node);
333 if (ret)
334 list_del_init(&cb->node);
335
336 spin_unlock_irqrestore(fence->lock, flags);
337
338 return ret;
339}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100340EXPORT_SYMBOL(dma_fence_remove_callback);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200341
342struct default_wait_cb {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100343 struct dma_fence_cb base;
Maarten Lankhorste9417592014-07-01 12:57:14 +0200344 struct task_struct *task;
345};
346
347static void
Chris Wilsonf54d1862016-10-25 13:00:45 +0100348dma_fence_default_wait_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200349{
350 struct default_wait_cb *wait =
351 container_of(cb, struct default_wait_cb, base);
352
353 wake_up_state(wait->task, TASK_NORMAL);
354}
355
356/**
Chris Wilsonf54d1862016-10-25 13:00:45 +0100357 * dma_fence_default_wait - default sleep until the fence gets signaled
Maarten Lankhorste9417592014-07-01 12:57:14 +0200358 * or until timeout elapses
359 * @fence: [in] the fence to wait on
360 * @intr: [in] if true, do an interruptible wait
361 * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
362 *
363 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
Alex Deucherbcc004b2016-11-07 16:16:13 -0500364 * remaining timeout in jiffies on success. If timeout is zero the value one is
365 * returned if the fence is already signaled for consistency with other
366 * functions taking a jiffies timeout.
Maarten Lankhorste9417592014-07-01 12:57:14 +0200367 */
368signed long
Chris Wilsonf54d1862016-10-25 13:00:45 +0100369dma_fence_default_wait(struct dma_fence *fence, bool intr, signed long timeout)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200370{
371 struct default_wait_cb cb;
372 unsigned long flags;
Alex Deucherbcc004b2016-11-07 16:16:13 -0500373 signed long ret = timeout ? timeout : 1;
Maarten Lankhorste9417592014-07-01 12:57:14 +0200374 bool was_set;
375
Chris Wilsonf54d1862016-10-25 13:00:45 +0100376 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
Alex Deucherbcc004b2016-11-07 16:16:13 -0500377 return ret;
Maarten Lankhorste9417592014-07-01 12:57:14 +0200378
379 spin_lock_irqsave(fence->lock, flags);
380
381 if (intr && signal_pending(current)) {
382 ret = -ERESTARTSYS;
383 goto out;
384 }
385
Chris Wilsonf54d1862016-10-25 13:00:45 +0100386 was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
387 &fence->flags);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200388
Chris Wilsonf54d1862016-10-25 13:00:45 +0100389 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
Maarten Lankhorste9417592014-07-01 12:57:14 +0200390 goto out;
391
392 if (!was_set) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100393 trace_dma_fence_enable_signal(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200394
395 if (!fence->ops->enable_signaling(fence)) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100396 dma_fence_signal_locked(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200397 goto out;
398 }
399 }
400
Chris Wilsonf54d1862016-10-25 13:00:45 +0100401 cb.base.func = dma_fence_default_wait_cb;
Maarten Lankhorste9417592014-07-01 12:57:14 +0200402 cb.task = current;
403 list_add(&cb.base.node, &fence->cb_list);
404
Chris Wilsonf54d1862016-10-25 13:00:45 +0100405 while (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags) && ret > 0) {
Maarten Lankhorste9417592014-07-01 12:57:14 +0200406 if (intr)
407 __set_current_state(TASK_INTERRUPTIBLE);
408 else
409 __set_current_state(TASK_UNINTERRUPTIBLE);
410 spin_unlock_irqrestore(fence->lock, flags);
411
412 ret = schedule_timeout(ret);
413
414 spin_lock_irqsave(fence->lock, flags);
415 if (ret > 0 && intr && signal_pending(current))
416 ret = -ERESTARTSYS;
417 }
418
419 if (!list_empty(&cb.base.node))
420 list_del(&cb.base.node);
421 __set_current_state(TASK_RUNNING);
422
423out:
424 spin_unlock_irqrestore(fence->lock, flags);
425 return ret;
426}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100427EXPORT_SYMBOL(dma_fence_default_wait);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200428
Christian Königa5194352015-10-20 16:34:16 +0200429static bool
monk.liu7392b4b2016-11-04 16:16:09 -0400430dma_fence_test_signaled_any(struct dma_fence **fences, uint32_t count,
431 uint32_t *idx)
Christian Königa5194352015-10-20 16:34:16 +0200432{
433 int i;
434
435 for (i = 0; i < count; ++i) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100436 struct dma_fence *fence = fences[i];
monk.liu7392b4b2016-11-04 16:16:09 -0400437 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
438 if (idx)
439 *idx = i;
Christian Königa5194352015-10-20 16:34:16 +0200440 return true;
monk.liu7392b4b2016-11-04 16:16:09 -0400441 }
Christian Königa5194352015-10-20 16:34:16 +0200442 }
443 return false;
444}
445
446/**
Chris Wilsonf54d1862016-10-25 13:00:45 +0100447 * dma_fence_wait_any_timeout - sleep until any fence gets signaled
Christian Königa5194352015-10-20 16:34:16 +0200448 * or until timeout elapses
449 * @fences: [in] array of fences to wait on
450 * @count: [in] number of fences to wait on
451 * @intr: [in] if true, do an interruptible wait
452 * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
monk.liu7392b4b2016-11-04 16:16:09 -0400453 * @idx: [out] the first signaled fence index, meaningful only on
454 * positive return
Christian Königa5194352015-10-20 16:34:16 +0200455 *
456 * Returns -EINVAL on custom fence wait implementation, -ERESTARTSYS if
457 * interrupted, 0 if the wait timed out, or the remaining timeout in jiffies
458 * on success.
459 *
460 * Synchronous waits for the first fence in the array to be signaled. The
461 * caller needs to hold a reference to all fences in the array, otherwise a
462 * fence might be freed before return, resulting in undefined behavior.
463 */
464signed long
Chris Wilsonf54d1862016-10-25 13:00:45 +0100465dma_fence_wait_any_timeout(struct dma_fence **fences, uint32_t count,
monk.liu7392b4b2016-11-04 16:16:09 -0400466 bool intr, signed long timeout, uint32_t *idx)
Christian Königa5194352015-10-20 16:34:16 +0200467{
468 struct default_wait_cb *cb;
469 signed long ret = timeout;
470 unsigned i;
471
472 if (WARN_ON(!fences || !count || timeout < 0))
473 return -EINVAL;
474
475 if (timeout == 0) {
476 for (i = 0; i < count; ++i)
monk.liu7392b4b2016-11-04 16:16:09 -0400477 if (dma_fence_is_signaled(fences[i])) {
478 if (idx)
479 *idx = i;
Christian Königa5194352015-10-20 16:34:16 +0200480 return 1;
monk.liu7392b4b2016-11-04 16:16:09 -0400481 }
Christian Königa5194352015-10-20 16:34:16 +0200482
483 return 0;
484 }
485
486 cb = kcalloc(count, sizeof(struct default_wait_cb), GFP_KERNEL);
487 if (cb == NULL) {
488 ret = -ENOMEM;
489 goto err_free_cb;
490 }
491
492 for (i = 0; i < count; ++i) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100493 struct dma_fence *fence = fences[i];
Christian Königa5194352015-10-20 16:34:16 +0200494
Chris Wilsonf54d1862016-10-25 13:00:45 +0100495 if (fence->ops->wait != dma_fence_default_wait) {
Christian Königa5194352015-10-20 16:34:16 +0200496 ret = -EINVAL;
497 goto fence_rm_cb;
498 }
499
500 cb[i].task = current;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100501 if (dma_fence_add_callback(fence, &cb[i].base,
502 dma_fence_default_wait_cb)) {
Christian Königa5194352015-10-20 16:34:16 +0200503 /* This fence is already signaled */
monk.liu7392b4b2016-11-04 16:16:09 -0400504 if (idx)
505 *idx = i;
Christian Königa5194352015-10-20 16:34:16 +0200506 goto fence_rm_cb;
507 }
508 }
509
510 while (ret > 0) {
511 if (intr)
512 set_current_state(TASK_INTERRUPTIBLE);
513 else
514 set_current_state(TASK_UNINTERRUPTIBLE);
515
monk.liu7392b4b2016-11-04 16:16:09 -0400516 if (dma_fence_test_signaled_any(fences, count, idx))
Christian Königa5194352015-10-20 16:34:16 +0200517 break;
518
519 ret = schedule_timeout(ret);
520
521 if (ret > 0 && intr && signal_pending(current))
522 ret = -ERESTARTSYS;
523 }
524
525 __set_current_state(TASK_RUNNING);
526
527fence_rm_cb:
528 while (i-- > 0)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100529 dma_fence_remove_callback(fences[i], &cb[i].base);
Christian Königa5194352015-10-20 16:34:16 +0200530
531err_free_cb:
532 kfree(cb);
533
534 return ret;
535}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100536EXPORT_SYMBOL(dma_fence_wait_any_timeout);
Christian Königa5194352015-10-20 16:34:16 +0200537
Maarten Lankhorste9417592014-07-01 12:57:14 +0200538/**
Chris Wilsonf54d1862016-10-25 13:00:45 +0100539 * dma_fence_init - Initialize a custom fence.
Maarten Lankhorste9417592014-07-01 12:57:14 +0200540 * @fence: [in] the fence to initialize
Chris Wilsonf54d1862016-10-25 13:00:45 +0100541 * @ops: [in] the dma_fence_ops for operations on this fence
Maarten Lankhorste9417592014-07-01 12:57:14 +0200542 * @lock: [in] the irqsafe spinlock to use for locking this fence
543 * @context: [in] the execution context this fence is run on
544 * @seqno: [in] a linear increasing sequence number for this context
545 *
546 * Initializes an allocated fence, the caller doesn't have to keep its
547 * refcount after committing with this fence, but it will need to hold a
Chris Wilsonf54d1862016-10-25 13:00:45 +0100548 * refcount again if dma_fence_ops.enable_signaling gets called. This can
Maarten Lankhorste9417592014-07-01 12:57:14 +0200549 * be used for other implementing other types of fence.
550 *
551 * context and seqno are used for easy comparison between fences, allowing
Chris Wilsonf54d1862016-10-25 13:00:45 +0100552 * to check which fence is later by simply using dma_fence_later.
Maarten Lankhorste9417592014-07-01 12:57:14 +0200553 */
554void
Chris Wilsonf54d1862016-10-25 13:00:45 +0100555dma_fence_init(struct dma_fence *fence, const struct dma_fence_ops *ops,
556 spinlock_t *lock, u64 context, unsigned seqno)
Maarten Lankhorste9417592014-07-01 12:57:14 +0200557{
558 BUG_ON(!lock);
559 BUG_ON(!ops || !ops->wait || !ops->enable_signaling ||
560 !ops->get_driver_name || !ops->get_timeline_name);
561
562 kref_init(&fence->refcount);
563 fence->ops = ops;
564 INIT_LIST_HEAD(&fence->cb_list);
565 fence->lock = lock;
566 fence->context = context;
567 fence->seqno = seqno;
568 fence->flags = 0UL;
Chris Wilson83dd1372017-01-04 14:12:20 +0000569 fence->status = 0;
Maarten Lankhorste9417592014-07-01 12:57:14 +0200570
Chris Wilsonf54d1862016-10-25 13:00:45 +0100571 trace_dma_fence_init(fence);
Maarten Lankhorste9417592014-07-01 12:57:14 +0200572}
Chris Wilsonf54d1862016-10-25 13:00:45 +0100573EXPORT_SYMBOL(dma_fence_init);