blob: 05d29dbc7e62c12b7d99bb8d4d4e5cccf2e46461 [file] [log] [blame]
Thomas Gleixner1802d0b2019-05-27 08:55:21 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Chris Wilsonf54d1862016-10-25 13:00:45 +01002/*
3 * Fence mechanism for dma-buf to allow for asynchronous dma access
4 *
5 * Copyright (C) 2012 Canonical Ltd
6 * Copyright (C) 2012 Texas Instruments
7 *
8 * Authors:
9 * Rob Clark <robdclark@gmail.com>
10 * Maarten Lankhorst <maarten.lankhorst@canonical.com>
Chris Wilsonf54d1862016-10-25 13:00:45 +010011 */
12
13#ifndef __LINUX_DMA_FENCE_H
14#define __LINUX_DMA_FENCE_H
15
16#include <linux/err.h>
17#include <linux/wait.h>
18#include <linux/list.h>
19#include <linux/bitops.h>
20#include <linux/kref.h>
21#include <linux/sched.h>
22#include <linux/printk.h>
23#include <linux/rcupdate.h>
24
25struct dma_fence;
26struct dma_fence_ops;
27struct dma_fence_cb;
28
29/**
30 * struct dma_fence - software synchronization primitive
31 * @refcount: refcount for this fence
32 * @ops: dma_fence_ops associated with this fence
33 * @rcu: used for releasing fence with kfree_rcu
34 * @cb_list: list of all callbacks to call
35 * @lock: spin_lock_irqsave used for locking
36 * @context: execution context this fence belongs to, returned by
37 * dma_fence_context_alloc()
38 * @seqno: the sequence number of this fence inside the execution context,
39 * can be compared to decide which fence would be signaled later.
40 * @flags: A mask of DMA_FENCE_FLAG_* defined below
41 * @timestamp: Timestamp when the fence was signaled.
Chris Wilsona009e972017-01-04 14:12:22 +000042 * @error: Optional, only valid if < 0, must be set before calling
Chris Wilsonf54d1862016-10-25 13:00:45 +010043 * dma_fence_signal, indicates that the fence has completed with an error.
44 *
45 * the flags member must be manipulated and read using the appropriate
46 * atomic ops (bit_*), so taking the spinlock will not be needed most
47 * of the time.
48 *
49 * DMA_FENCE_FLAG_SIGNALED_BIT - fence is already signaled
Chris Wilson76250f22017-02-14 12:40:01 +000050 * DMA_FENCE_FLAG_TIMESTAMP_BIT - timestamp recorded for fence signaling
Chris Wilsonf54d1862016-10-25 13:00:45 +010051 * DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT - enable_signaling might have been called
52 * DMA_FENCE_FLAG_USER_BITS - start of the unused bits, can be used by the
53 * implementer of the fence for its own purposes. Can be used in different
54 * ways by different fence implementers, so do not rely on this.
55 *
56 * Since atomic bitops are used, this is not guaranteed to be the case.
57 * Particularly, if the bit was set, but dma_fence_signal was called right
58 * before this bit was set, it would have been able to set the
59 * DMA_FENCE_FLAG_SIGNALED_BIT, before enable_signaling was called.
60 * Adding a check for DMA_FENCE_FLAG_SIGNALED_BIT after setting
61 * DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT closes this race, and makes sure that
62 * after dma_fence_signal was called, any enable_signaling call will have either
63 * been completed, or never called at all.
64 */
65struct dma_fence {
66 struct kref refcount;
67 const struct dma_fence_ops *ops;
68 struct rcu_head rcu;
69 struct list_head cb_list;
70 spinlock_t *lock;
71 u64 context;
Christian Königb312d8c2018-11-14 16:11:06 +010072 u64 seqno;
Chris Wilsonf54d1862016-10-25 13:00:45 +010073 unsigned long flags;
74 ktime_t timestamp;
Chris Wilsona009e972017-01-04 14:12:22 +000075 int error;
Chris Wilsonf54d1862016-10-25 13:00:45 +010076};
77
78enum dma_fence_flag_bits {
79 DMA_FENCE_FLAG_SIGNALED_BIT,
Chris Wilson76250f22017-02-14 12:40:01 +000080 DMA_FENCE_FLAG_TIMESTAMP_BIT,
Chris Wilsonf54d1862016-10-25 13:00:45 +010081 DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
82 DMA_FENCE_FLAG_USER_BITS, /* must always be last member */
83};
84
85typedef void (*dma_fence_func_t)(struct dma_fence *fence,
86 struct dma_fence_cb *cb);
87
88/**
Daniel Vetter2c269b02018-04-27 08:17:08 +020089 * struct dma_fence_cb - callback for dma_fence_add_callback()
90 * @node: used by dma_fence_add_callback() to append this struct to fence::cb_list
Chris Wilsonf54d1862016-10-25 13:00:45 +010091 * @func: dma_fence_func_t to call
92 *
Daniel Vetter2c269b02018-04-27 08:17:08 +020093 * This struct will be initialized by dma_fence_add_callback(), additional
Chris Wilsonf54d1862016-10-25 13:00:45 +010094 * data can be passed along by embedding dma_fence_cb in another struct.
95 */
96struct dma_fence_cb {
97 struct list_head node;
98 dma_fence_func_t func;
99};
100
101/**
102 * struct dma_fence_ops - operations implemented for fence
Chris Wilsonf54d1862016-10-25 13:00:45 +0100103 *
Chris Wilsonf54d1862016-10-25 13:00:45 +0100104 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100105struct dma_fence_ops {
Daniel Vetter2c269b02018-04-27 08:17:08 +0200106 /**
Christian König5e498ab2019-04-15 14:46:34 +0200107 * @use_64bit_seqno:
108 *
109 * True if this dma_fence implementation uses 64bit seqno, false
110 * otherwise.
111 */
112 bool use_64bit_seqno;
113
114 /**
Daniel Vetter2c269b02018-04-27 08:17:08 +0200115 * @get_driver_name:
116 *
117 * Returns the driver name. This is a callback to allow drivers to
118 * compute the name at runtime, without having it to store permanently
119 * for each fence, or build a cache of some sort.
120 *
121 * This callback is mandatory.
122 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100123 const char * (*get_driver_name)(struct dma_fence *fence);
Daniel Vetter2c269b02018-04-27 08:17:08 +0200124
125 /**
126 * @get_timeline_name:
127 *
128 * Return the name of the context this fence belongs to. This is a
129 * callback to allow drivers to compute the name at runtime, without
130 * having it to store permanently for each fence, or build a cache of
131 * some sort.
132 *
133 * This callback is mandatory.
134 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100135 const char * (*get_timeline_name)(struct dma_fence *fence);
Daniel Vetter2c269b02018-04-27 08:17:08 +0200136
137 /**
138 * @enable_signaling:
139 *
140 * Enable software signaling of fence.
141 *
142 * For fence implementations that have the capability for hw->hw
143 * signaling, they can implement this op to enable the necessary
144 * interrupts, or insert commands into cmdstream, etc, to avoid these
145 * costly operations for the common case where only hw->hw
146 * synchronization is required. This is called in the first
147 * dma_fence_wait() or dma_fence_add_callback() path to let the fence
148 * implementation know that there is another driver waiting on the
149 * signal (ie. hw->sw case).
150 *
151 * This function can be called from atomic context, but not
152 * from irq context, so normal spinlocks can be used.
153 *
154 * A return value of false indicates the fence already passed,
155 * or some failure occurred that made it impossible to enable
156 * signaling. True indicates successful enabling.
157 *
158 * &dma_fence.error may be set in enable_signaling, but only when false
159 * is returned.
160 *
161 * Since many implementations can call dma_fence_signal() even when before
162 * @enable_signaling has been called there's a race window, where the
163 * dma_fence_signal() might result in the final fence reference being
164 * released and its memory freed. To avoid this, implementations of this
165 * callback should grab their own reference using dma_fence_get(), to be
166 * released when the fence is signalled (through e.g. the interrupt
167 * handler).
168 *
Daniel Vetterc7013172018-05-04 16:10:34 +0200169 * This callback is optional. If this callback is not present, then the
170 * driver must always have signaling enabled.
Daniel Vetter2c269b02018-04-27 08:17:08 +0200171 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100172 bool (*enable_signaling)(struct dma_fence *fence);
Daniel Vetter2c269b02018-04-27 08:17:08 +0200173
174 /**
175 * @signaled:
176 *
177 * Peek whether the fence is signaled, as a fastpath optimization for
178 * e.g. dma_fence_wait() or dma_fence_add_callback(). Note that this
179 * callback does not need to make any guarantees beyond that a fence
180 * once indicates as signalled must always return true from this
181 * callback. This callback may return false even if the fence has
182 * completed already, in this case information hasn't propogated throug
183 * the system yet. See also dma_fence_is_signaled().
184 *
185 * May set &dma_fence.error if returning true.
186 *
187 * This callback is optional.
188 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100189 bool (*signaled)(struct dma_fence *fence);
Daniel Vetter2c269b02018-04-27 08:17:08 +0200190
191 /**
192 * @wait:
193 *
Daniel Vetter418cc6c2018-05-03 16:25:52 +0200194 * Custom wait implementation, defaults to dma_fence_default_wait() if
195 * not set.
Daniel Vetter2c269b02018-04-27 08:17:08 +0200196 *
Daniel Vetter418cc6c2018-05-03 16:25:52 +0200197 * The dma_fence_default_wait implementation should work for any fence, as long
198 * as @enable_signaling works correctly. This hook allows drivers to
199 * have an optimized version for the case where a process context is
200 * already available, e.g. if @enable_signaling for the general case
201 * needs to set up a worker thread.
Daniel Vetter2c269b02018-04-27 08:17:08 +0200202 *
203 * Must return -ERESTARTSYS if the wait is intr = true and the wait was
204 * interrupted, and remaining jiffies if fence has signaled, or 0 if wait
205 * timed out. Can also return other error values on custom implementations,
206 * which should be treated as if the fence is signaled. For example a hardware
207 * lockup could be reported like that.
208 *
Daniel Vetter418cc6c2018-05-03 16:25:52 +0200209 * This callback is optional.
Daniel Vetter2c269b02018-04-27 08:17:08 +0200210 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100211 signed long (*wait)(struct dma_fence *fence,
212 bool intr, signed long timeout);
Daniel Vetter2c269b02018-04-27 08:17:08 +0200213
214 /**
215 * @release:
216 *
217 * Called on destruction of fence to release additional resources.
218 * Can be called from irq context. This callback is optional. If it is
219 * NULL, then dma_fence_free() is instead called as the default
220 * implementation.
221 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100222 void (*release)(struct dma_fence *fence);
223
Daniel Vetter2c269b02018-04-27 08:17:08 +0200224 /**
Daniel Vetter2c269b02018-04-27 08:17:08 +0200225 * @fence_value_str:
226 *
227 * Callback to fill in free-form debug info specific to this fence, like
228 * the sequence number.
229 *
230 * This callback is optional.
231 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100232 void (*fence_value_str)(struct dma_fence *fence, char *str, int size);
Daniel Vetter2c269b02018-04-27 08:17:08 +0200233
234 /**
235 * @timeline_value_str:
236 *
237 * Fills in the current value of the timeline as a string, like the
Daniel Vetter1b48b722018-05-03 16:25:49 +0200238 * sequence number. Note that the specific fence passed to this function
239 * should not matter, drivers should only use it to look up the
240 * corresponding timeline structures.
Daniel Vetter2c269b02018-04-27 08:17:08 +0200241 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100242 void (*timeline_value_str)(struct dma_fence *fence,
243 char *str, int size);
244};
245
246void dma_fence_init(struct dma_fence *fence, const struct dma_fence_ops *ops,
Christian Königb312d8c2018-11-14 16:11:06 +0100247 spinlock_t *lock, u64 context, u64 seqno);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100248
249void dma_fence_release(struct kref *kref);
250void dma_fence_free(struct dma_fence *fence);
251
252/**
253 * dma_fence_put - decreases refcount of the fence
Daniel Vetter2c269b02018-04-27 08:17:08 +0200254 * @fence: fence to reduce refcount of
Chris Wilsonf54d1862016-10-25 13:00:45 +0100255 */
256static inline void dma_fence_put(struct dma_fence *fence)
257{
258 if (fence)
259 kref_put(&fence->refcount, dma_fence_release);
260}
261
262/**
263 * dma_fence_get - increases refcount of the fence
Daniel Vetter2c269b02018-04-27 08:17:08 +0200264 * @fence: fence to increase refcount of
Chris Wilsonf54d1862016-10-25 13:00:45 +0100265 *
266 * Returns the same fence, with refcount increased by 1.
267 */
268static inline struct dma_fence *dma_fence_get(struct dma_fence *fence)
269{
270 if (fence)
271 kref_get(&fence->refcount);
272 return fence;
273}
274
275/**
276 * dma_fence_get_rcu - get a fence from a reservation_object_list with
277 * rcu read lock
Daniel Vetter2c269b02018-04-27 08:17:08 +0200278 * @fence: fence to increase refcount of
Chris Wilsonf54d1862016-10-25 13:00:45 +0100279 *
280 * Function returns NULL if no refcount could be obtained, or the fence.
281 */
282static inline struct dma_fence *dma_fence_get_rcu(struct dma_fence *fence)
283{
284 if (kref_get_unless_zero(&fence->refcount))
285 return fence;
286 else
287 return NULL;
288}
289
290/**
291 * dma_fence_get_rcu_safe - acquire a reference to an RCU tracked fence
Daniel Vetter2c269b02018-04-27 08:17:08 +0200292 * @fencep: pointer to fence to increase refcount of
Chris Wilsonf54d1862016-10-25 13:00:45 +0100293 *
294 * Function returns NULL if no refcount could be obtained, or the fence.
295 * This function handles acquiring a reference to a fence that may be
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -0800296 * reallocated within the RCU grace period (such as with SLAB_TYPESAFE_BY_RCU),
Chris Wilsonf54d1862016-10-25 13:00:45 +0100297 * so long as the caller is using RCU on the pointer to the fence.
298 *
299 * An alternative mechanism is to employ a seqlock to protect a bunch of
300 * fences, such as used by struct reservation_object. When using a seqlock,
301 * the seqlock must be taken before and checked after a reference to the
302 * fence is acquired (as shown here).
303 *
304 * The caller is required to hold the RCU read lock.
305 */
306static inline struct dma_fence *
Chris Wilson5f72db52017-11-02 22:03:34 +0200307dma_fence_get_rcu_safe(struct dma_fence __rcu **fencep)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100308{
309 do {
310 struct dma_fence *fence;
311
312 fence = rcu_dereference(*fencep);
Christian Königf8e07312017-09-15 11:53:07 +0200313 if (!fence)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100314 return NULL;
315
Christian Königf8e07312017-09-15 11:53:07 +0200316 if (!dma_fence_get_rcu(fence))
317 continue;
318
Chris Wilsonf54d1862016-10-25 13:00:45 +0100319 /* The atomic_inc_not_zero() inside dma_fence_get_rcu()
320 * provides a full memory barrier upon success (such as now).
321 * This is paired with the write barrier from assigning
322 * to the __rcu protected fence pointer so that if that
323 * pointer still matches the current fence, we know we
324 * have successfully acquire a reference to it. If it no
325 * longer matches, we are holding a reference to some other
326 * reallocated pointer. This is possible if the allocator
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -0800327 * is using a freelist like SLAB_TYPESAFE_BY_RCU where the
Chris Wilsonf54d1862016-10-25 13:00:45 +0100328 * fence remains valid for the RCU grace period, but it
329 * may be reallocated. When using such allocators, we are
330 * responsible for ensuring the reference we get is to
331 * the right fence, as below.
332 */
333 if (fence == rcu_access_pointer(*fencep))
334 return rcu_pointer_handoff(fence);
335
336 dma_fence_put(fence);
337 } while (1);
338}
339
340int dma_fence_signal(struct dma_fence *fence);
341int dma_fence_signal_locked(struct dma_fence *fence);
342signed long dma_fence_default_wait(struct dma_fence *fence,
343 bool intr, signed long timeout);
344int dma_fence_add_callback(struct dma_fence *fence,
345 struct dma_fence_cb *cb,
346 dma_fence_func_t func);
347bool dma_fence_remove_callback(struct dma_fence *fence,
348 struct dma_fence_cb *cb);
349void dma_fence_enable_sw_signaling(struct dma_fence *fence);
350
351/**
352 * dma_fence_is_signaled_locked - Return an indication if the fence
353 * is signaled yet.
Daniel Vetter2c269b02018-04-27 08:17:08 +0200354 * @fence: the fence to check
Chris Wilsonf54d1862016-10-25 13:00:45 +0100355 *
356 * Returns true if the fence was already signaled, false if not. Since this
357 * function doesn't enable signaling, it is not guaranteed to ever return
Daniel Vetter2c269b02018-04-27 08:17:08 +0200358 * true if dma_fence_add_callback(), dma_fence_wait() or
359 * dma_fence_enable_sw_signaling() haven't been called before.
Chris Wilsonf54d1862016-10-25 13:00:45 +0100360 *
Daniel Vetter2c269b02018-04-27 08:17:08 +0200361 * This function requires &dma_fence.lock to be held.
362 *
363 * See also dma_fence_is_signaled().
Chris Wilsonf54d1862016-10-25 13:00:45 +0100364 */
365static inline bool
366dma_fence_is_signaled_locked(struct dma_fence *fence)
367{
368 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
369 return true;
370
371 if (fence->ops->signaled && fence->ops->signaled(fence)) {
372 dma_fence_signal_locked(fence);
373 return true;
374 }
375
376 return false;
377}
378
379/**
380 * dma_fence_is_signaled - Return an indication if the fence is signaled yet.
Daniel Vetter2c269b02018-04-27 08:17:08 +0200381 * @fence: the fence to check
Chris Wilsonf54d1862016-10-25 13:00:45 +0100382 *
383 * Returns true if the fence was already signaled, false if not. Since this
384 * function doesn't enable signaling, it is not guaranteed to ever return
Daniel Vetter2c269b02018-04-27 08:17:08 +0200385 * true if dma_fence_add_callback(), dma_fence_wait() or
386 * dma_fence_enable_sw_signaling() haven't been called before.
Chris Wilsonf54d1862016-10-25 13:00:45 +0100387 *
388 * It's recommended for seqno fences to call dma_fence_signal when the
389 * operation is complete, it makes it possible to prevent issues from
390 * wraparound between time of issue and time of use by checking the return
391 * value of this function before calling hardware-specific wait instructions.
Daniel Vetter2c269b02018-04-27 08:17:08 +0200392 *
393 * See also dma_fence_is_signaled_locked().
Chris Wilsonf54d1862016-10-25 13:00:45 +0100394 */
395static inline bool
396dma_fence_is_signaled(struct dma_fence *fence)
397{
398 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
399 return true;
400
401 if (fence->ops->signaled && fence->ops->signaled(fence)) {
402 dma_fence_signal(fence);
403 return true;
404 }
405
406 return false;
407}
408
409/**
Chris Wilson81114772017-06-29 13:59:24 +0100410 * __dma_fence_is_later - return if f1 is chronologically later than f2
Daniel Vetter2c269b02018-04-27 08:17:08 +0200411 * @f1: the first fence's seqno
412 * @f2: the second fence's seqno from the same context
Christian König5e498ab2019-04-15 14:46:34 +0200413 * @ops: dma_fence_ops associated with the seqno
Chris Wilson81114772017-06-29 13:59:24 +0100414 *
415 * Returns true if f1 is chronologically later than f2. Both fences must be
416 * from the same context, since a seqno is not common across contexts.
417 */
Christian König5e498ab2019-04-15 14:46:34 +0200418static inline bool __dma_fence_is_later(u64 f1, u64 f2,
419 const struct dma_fence_ops *ops)
Chris Wilson81114772017-06-29 13:59:24 +0100420{
Christian Königb312d8c2018-11-14 16:11:06 +0100421 /* This is for backward compatibility with drivers which can only handle
Christian König5e498ab2019-04-15 14:46:34 +0200422 * 32bit sequence numbers. Use a 64bit compare when the driver says to
423 * do so.
Christian Königb312d8c2018-11-14 16:11:06 +0100424 */
Christian König5e498ab2019-04-15 14:46:34 +0200425 if (ops->use_64bit_seqno)
Christian Königb312d8c2018-11-14 16:11:06 +0100426 return f1 > f2;
427
428 return (int)(lower_32_bits(f1) - lower_32_bits(f2)) > 0;
Chris Wilson81114772017-06-29 13:59:24 +0100429}
430
431/**
Chris Wilsonf54d1862016-10-25 13:00:45 +0100432 * dma_fence_is_later - return if f1 is chronologically later than f2
Daniel Vetter2c269b02018-04-27 08:17:08 +0200433 * @f1: the first fence from the same context
434 * @f2: the second fence from the same context
Chris Wilsonf54d1862016-10-25 13:00:45 +0100435 *
436 * Returns true if f1 is chronologically later than f2. Both fences must be
437 * from the same context, since a seqno is not re-used across contexts.
438 */
439static inline bool dma_fence_is_later(struct dma_fence *f1,
440 struct dma_fence *f2)
441{
442 if (WARN_ON(f1->context != f2->context))
443 return false;
444
Christian König5e498ab2019-04-15 14:46:34 +0200445 return __dma_fence_is_later(f1->seqno, f2->seqno, f1->ops);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100446}
447
448/**
449 * dma_fence_later - return the chronologically later fence
Daniel Vetter2c269b02018-04-27 08:17:08 +0200450 * @f1: the first fence from the same context
451 * @f2: the second fence from the same context
Chris Wilsonf54d1862016-10-25 13:00:45 +0100452 *
453 * Returns NULL if both fences are signaled, otherwise the fence that would be
454 * signaled last. Both fences must be from the same context, since a seqno is
455 * not re-used across contexts.
456 */
457static inline struct dma_fence *dma_fence_later(struct dma_fence *f1,
458 struct dma_fence *f2)
459{
460 if (WARN_ON(f1->context != f2->context))
461 return NULL;
462
463 /*
464 * Can't check just DMA_FENCE_FLAG_SIGNALED_BIT here, it may never
465 * have been set if enable_signaling wasn't called, and enabling that
466 * here is overkill.
467 */
468 if (dma_fence_is_later(f1, f2))
469 return dma_fence_is_signaled(f1) ? NULL : f1;
470 else
471 return dma_fence_is_signaled(f2) ? NULL : f2;
472}
473
Chris Wilsond6c99f42017-01-04 14:12:21 +0000474/**
475 * dma_fence_get_status_locked - returns the status upon completion
Daniel Vetter2c269b02018-04-27 08:17:08 +0200476 * @fence: the dma_fence to query
Chris Wilsond6c99f42017-01-04 14:12:21 +0000477 *
478 * Drivers can supply an optional error status condition before they signal
479 * the fence (to indicate whether the fence was completed due to an error
480 * rather than success). The value of the status condition is only valid
481 * if the fence has been signaled, dma_fence_get_status_locked() first checks
482 * the signal state before reporting the error status.
483 *
484 * Returns 0 if the fence has not yet been signaled, 1 if the fence has
485 * been signaled without an error condition, or a negative error code
486 * if the fence has been completed in err.
487 */
488static inline int dma_fence_get_status_locked(struct dma_fence *fence)
489{
490 if (dma_fence_is_signaled_locked(fence))
Chris Wilsona009e972017-01-04 14:12:22 +0000491 return fence->error ?: 1;
Chris Wilsond6c99f42017-01-04 14:12:21 +0000492 else
493 return 0;
494}
495
496int dma_fence_get_status(struct dma_fence *fence);
497
Chris Wilsona009e972017-01-04 14:12:22 +0000498/**
499 * dma_fence_set_error - flag an error condition on the fence
Daniel Vetter2c269b02018-04-27 08:17:08 +0200500 * @fence: the dma_fence
501 * @error: the error to store
Chris Wilsona009e972017-01-04 14:12:22 +0000502 *
503 * Drivers can supply an optional error status condition before they signal
504 * the fence, to indicate that the fence was completed due to an error
505 * rather than success. This must be set before signaling (so that the value
506 * is visible before any waiters on the signal callback are woken). This
507 * helper exists to help catching erroneous setting of #dma_fence.error.
508 */
509static inline void dma_fence_set_error(struct dma_fence *fence,
510 int error)
511{
Daniel Vetter6ce31262017-07-20 14:51:07 +0200512 WARN_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags));
513 WARN_ON(error >= 0 || error < -MAX_ERRNO);
Chris Wilsona009e972017-01-04 14:12:22 +0000514
515 fence->error = error;
516}
517
Chris Wilsonf54d1862016-10-25 13:00:45 +0100518signed long dma_fence_wait_timeout(struct dma_fence *,
519 bool intr, signed long timeout);
520signed long dma_fence_wait_any_timeout(struct dma_fence **fences,
521 uint32_t count,
monk.liu7392b4b2016-11-04 16:16:09 -0400522 bool intr, signed long timeout,
523 uint32_t *idx);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100524
525/**
526 * dma_fence_wait - sleep until the fence gets signaled
Daniel Vetter2c269b02018-04-27 08:17:08 +0200527 * @fence: the fence to wait on
528 * @intr: if true, do an interruptible wait
Chris Wilsonf54d1862016-10-25 13:00:45 +0100529 *
530 * This function will return -ERESTARTSYS if interrupted by a signal,
531 * or 0 if the fence was signaled. Other error values may be
532 * returned on custom implementations.
533 *
534 * Performs a synchronous wait on this fence. It is assumed the caller
535 * directly or indirectly holds a reference to the fence, otherwise the
536 * fence might be freed before return, resulting in undefined behavior.
Daniel Vetter2c269b02018-04-27 08:17:08 +0200537 *
538 * See also dma_fence_wait_timeout() and dma_fence_wait_any_timeout().
Chris Wilsonf54d1862016-10-25 13:00:45 +0100539 */
540static inline signed long dma_fence_wait(struct dma_fence *fence, bool intr)
541{
542 signed long ret;
543
544 /* Since dma_fence_wait_timeout cannot timeout with
545 * MAX_SCHEDULE_TIMEOUT, only valid return values are
546 * -ERESTARTSYS and MAX_SCHEDULE_TIMEOUT.
547 */
548 ret = dma_fence_wait_timeout(fence, intr, MAX_SCHEDULE_TIMEOUT);
549
550 return ret < 0 ? ret : 0;
551}
552
Christian König078dec332018-12-03 13:36:14 +0100553struct dma_fence *dma_fence_get_stub(void);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100554u64 dma_fence_context_alloc(unsigned num);
555
556#define DMA_FENCE_TRACE(f, fmt, args...) \
557 do { \
558 struct dma_fence *__ff = (f); \
559 if (IS_ENABLED(CONFIG_DMA_FENCE_TRACE)) \
Christian Königb312d8c2018-11-14 16:11:06 +0100560 pr_info("f %llu#%llu: " fmt, \
Chris Wilsonf54d1862016-10-25 13:00:45 +0100561 __ff->context, __ff->seqno, ##args); \
562 } while (0)
563
564#define DMA_FENCE_WARN(f, fmt, args...) \
565 do { \
566 struct dma_fence *__ff = (f); \
Christian Königb312d8c2018-11-14 16:11:06 +0100567 pr_warn("f %llu#%llu: " fmt, __ff->context, __ff->seqno,\
Chris Wilsonf54d1862016-10-25 13:00:45 +0100568 ##args); \
569 } while (0)
570
571#define DMA_FENCE_ERR(f, fmt, args...) \
572 do { \
573 struct dma_fence *__ff = (f); \
Christian Königb312d8c2018-11-14 16:11:06 +0100574 pr_err("f %llu#%llu: " fmt, __ff->context, __ff->seqno, \
Chris Wilsonf54d1862016-10-25 13:00:45 +0100575 ##args); \
576 } while (0)
577
578#endif /* __LINUX_DMA_FENCE_H */