blob: 09e23adb351d5caef568a23886e1340aa6cd581e [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 {
Chris Wilson4fe39972019-08-17 15:47:33 +010066 spinlock_t *lock;
Chris Wilsonf54d1862016-10-25 13:00:45 +010067 const struct dma_fence_ops *ops;
Chris Wilsonf2cb60e2019-08-17 16:30:22 +010068 /*
69 * We clear the callback list on kref_put so that by the time we
70 * release the fence it is unused. No one should be adding to the
71 * cb_list that they don't themselves hold a reference for.
72 *
73 * The lifetime of the timestamp is similarly tied to both the
74 * rcu freelist and the cb_list. The timestamp is only set upon
75 * signaling while simultaneously notifying the cb_list. Ergo, we
76 * only use either the cb_list of timestamp. Upon destruction,
77 * neither are accessible, and so we can use the rcu. This means
78 * that the cb_list is *only* valid until the signal bit is set,
79 * and to read either you *must* hold a reference to the fence,
80 * and not just the rcu_read_lock.
81 *
82 * Listed in chronological order.
Christian König0e2f7332019-08-07 12:31:48 +020083 */
84 union {
Christian König0e2f7332019-08-07 12:31:48 +020085 struct list_head cb_list;
Chris Wilsonf2cb60e2019-08-17 16:30:22 +010086 /* @cb_list replaced by @timestamp on dma_fence_signal() */
87 ktime_t timestamp;
88 /* @timestamp replaced by @rcu on dma_fence_release() */
89 struct rcu_head rcu;
Christian König0e2f7332019-08-07 12:31:48 +020090 };
Chris Wilsonf54d1862016-10-25 13:00:45 +010091 u64 context;
Christian Königb312d8c2018-11-14 16:11:06 +010092 u64 seqno;
Chris Wilson4fe39972019-08-17 15:47:33 +010093 unsigned long flags;
94 struct kref refcount;
Chris Wilsona009e972017-01-04 14:12:22 +000095 int error;
Chris Wilsonf54d1862016-10-25 13:00:45 +010096};
97
98enum dma_fence_flag_bits {
99 DMA_FENCE_FLAG_SIGNALED_BIT,
Chris Wilson76250f22017-02-14 12:40:01 +0000100 DMA_FENCE_FLAG_TIMESTAMP_BIT,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100101 DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
102 DMA_FENCE_FLAG_USER_BITS, /* must always be last member */
103};
104
105typedef void (*dma_fence_func_t)(struct dma_fence *fence,
106 struct dma_fence_cb *cb);
107
108/**
Daniel Vetter2c269b02018-04-27 08:17:08 +0200109 * struct dma_fence_cb - callback for dma_fence_add_callback()
110 * @node: used by dma_fence_add_callback() to append this struct to fence::cb_list
Chris Wilsonf54d1862016-10-25 13:00:45 +0100111 * @func: dma_fence_func_t to call
112 *
Daniel Vetter2c269b02018-04-27 08:17:08 +0200113 * This struct will be initialized by dma_fence_add_callback(), additional
Chris Wilsonf54d1862016-10-25 13:00:45 +0100114 * data can be passed along by embedding dma_fence_cb in another struct.
115 */
116struct dma_fence_cb {
117 struct list_head node;
118 dma_fence_func_t func;
119};
120
121/**
122 * struct dma_fence_ops - operations implemented for fence
Chris Wilsonf54d1862016-10-25 13:00:45 +0100123 *
Chris Wilsonf54d1862016-10-25 13:00:45 +0100124 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100125struct dma_fence_ops {
Daniel Vetter2c269b02018-04-27 08:17:08 +0200126 /**
Christian König5e498ab2019-04-15 14:46:34 +0200127 * @use_64bit_seqno:
128 *
129 * True if this dma_fence implementation uses 64bit seqno, false
130 * otherwise.
131 */
132 bool use_64bit_seqno;
133
134 /**
Daniel Vetter2c269b02018-04-27 08:17:08 +0200135 * @get_driver_name:
136 *
137 * Returns the driver name. This is a callback to allow drivers to
138 * compute the name at runtime, without having it to store permanently
139 * for each fence, or build a cache of some sort.
140 *
141 * This callback is mandatory.
142 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100143 const char * (*get_driver_name)(struct dma_fence *fence);
Daniel Vetter2c269b02018-04-27 08:17:08 +0200144
145 /**
146 * @get_timeline_name:
147 *
148 * Return the name of the context this fence belongs to. This is a
149 * callback to allow drivers to compute the name at runtime, without
150 * having it to store permanently for each fence, or build a cache of
151 * some sort.
152 *
153 * This callback is mandatory.
154 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100155 const char * (*get_timeline_name)(struct dma_fence *fence);
Daniel Vetter2c269b02018-04-27 08:17:08 +0200156
157 /**
158 * @enable_signaling:
159 *
160 * Enable software signaling of fence.
161 *
162 * For fence implementations that have the capability for hw->hw
163 * signaling, they can implement this op to enable the necessary
164 * interrupts, or insert commands into cmdstream, etc, to avoid these
165 * costly operations for the common case where only hw->hw
166 * synchronization is required. This is called in the first
167 * dma_fence_wait() or dma_fence_add_callback() path to let the fence
168 * implementation know that there is another driver waiting on the
169 * signal (ie. hw->sw case).
170 *
171 * This function can be called from atomic context, but not
172 * from irq context, so normal spinlocks can be used.
173 *
174 * A return value of false indicates the fence already passed,
175 * or some failure occurred that made it impossible to enable
176 * signaling. True indicates successful enabling.
177 *
178 * &dma_fence.error may be set in enable_signaling, but only when false
179 * is returned.
180 *
181 * Since many implementations can call dma_fence_signal() even when before
182 * @enable_signaling has been called there's a race window, where the
183 * dma_fence_signal() might result in the final fence reference being
184 * released and its memory freed. To avoid this, implementations of this
185 * callback should grab their own reference using dma_fence_get(), to be
186 * released when the fence is signalled (through e.g. the interrupt
187 * handler).
188 *
Daniel Vetterc7013172018-05-04 16:10:34 +0200189 * This callback is optional. If this callback is not present, then the
190 * driver must always have signaling enabled.
Daniel Vetter2c269b02018-04-27 08:17:08 +0200191 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100192 bool (*enable_signaling)(struct dma_fence *fence);
Daniel Vetter2c269b02018-04-27 08:17:08 +0200193
194 /**
195 * @signaled:
196 *
197 * Peek whether the fence is signaled, as a fastpath optimization for
198 * e.g. dma_fence_wait() or dma_fence_add_callback(). Note that this
199 * callback does not need to make any guarantees beyond that a fence
200 * once indicates as signalled must always return true from this
201 * callback. This callback may return false even if the fence has
202 * completed already, in this case information hasn't propogated throug
203 * the system yet. See also dma_fence_is_signaled().
204 *
205 * May set &dma_fence.error if returning true.
206 *
207 * This callback is optional.
208 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100209 bool (*signaled)(struct dma_fence *fence);
Daniel Vetter2c269b02018-04-27 08:17:08 +0200210
211 /**
212 * @wait:
213 *
Daniel Vetter418cc6c2018-05-03 16:25:52 +0200214 * Custom wait implementation, defaults to dma_fence_default_wait() if
215 * not set.
Daniel Vetter2c269b02018-04-27 08:17:08 +0200216 *
Daniel Vetter418cc6c2018-05-03 16:25:52 +0200217 * The dma_fence_default_wait implementation should work for any fence, as long
218 * as @enable_signaling works correctly. This hook allows drivers to
219 * have an optimized version for the case where a process context is
220 * already available, e.g. if @enable_signaling for the general case
221 * needs to set up a worker thread.
Daniel Vetter2c269b02018-04-27 08:17:08 +0200222 *
223 * Must return -ERESTARTSYS if the wait is intr = true and the wait was
224 * interrupted, and remaining jiffies if fence has signaled, or 0 if wait
225 * timed out. Can also return other error values on custom implementations,
226 * which should be treated as if the fence is signaled. For example a hardware
227 * lockup could be reported like that.
228 *
Daniel Vetter418cc6c2018-05-03 16:25:52 +0200229 * This callback is optional.
Daniel Vetter2c269b02018-04-27 08:17:08 +0200230 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100231 signed long (*wait)(struct dma_fence *fence,
232 bool intr, signed long timeout);
Daniel Vetter2c269b02018-04-27 08:17:08 +0200233
234 /**
235 * @release:
236 *
237 * Called on destruction of fence to release additional resources.
238 * Can be called from irq context. This callback is optional. If it is
239 * NULL, then dma_fence_free() is instead called as the default
240 * implementation.
241 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100242 void (*release)(struct dma_fence *fence);
243
Daniel Vetter2c269b02018-04-27 08:17:08 +0200244 /**
Daniel Vetter2c269b02018-04-27 08:17:08 +0200245 * @fence_value_str:
246 *
247 * Callback to fill in free-form debug info specific to this fence, like
248 * the sequence number.
249 *
250 * This callback is optional.
251 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100252 void (*fence_value_str)(struct dma_fence *fence, char *str, int size);
Daniel Vetter2c269b02018-04-27 08:17:08 +0200253
254 /**
255 * @timeline_value_str:
256 *
257 * Fills in the current value of the timeline as a string, like the
Daniel Vetter1b48b722018-05-03 16:25:49 +0200258 * sequence number. Note that the specific fence passed to this function
259 * should not matter, drivers should only use it to look up the
260 * corresponding timeline structures.
Daniel Vetter2c269b02018-04-27 08:17:08 +0200261 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100262 void (*timeline_value_str)(struct dma_fence *fence,
263 char *str, int size);
264};
265
266void dma_fence_init(struct dma_fence *fence, const struct dma_fence_ops *ops,
Christian Königb312d8c2018-11-14 16:11:06 +0100267 spinlock_t *lock, u64 context, u64 seqno);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100268
269void dma_fence_release(struct kref *kref);
270void dma_fence_free(struct dma_fence *fence);
271
272/**
273 * dma_fence_put - decreases refcount of the fence
Daniel Vetter2c269b02018-04-27 08:17:08 +0200274 * @fence: fence to reduce refcount of
Chris Wilsonf54d1862016-10-25 13:00:45 +0100275 */
276static inline void dma_fence_put(struct dma_fence *fence)
277{
278 if (fence)
279 kref_put(&fence->refcount, dma_fence_release);
280}
281
282/**
283 * dma_fence_get - increases refcount of the fence
Daniel Vetter2c269b02018-04-27 08:17:08 +0200284 * @fence: fence to increase refcount of
Chris Wilsonf54d1862016-10-25 13:00:45 +0100285 *
286 * Returns the same fence, with refcount increased by 1.
287 */
288static inline struct dma_fence *dma_fence_get(struct dma_fence *fence)
289{
290 if (fence)
291 kref_get(&fence->refcount);
292 return fence;
293}
294
295/**
Christian König52791ee2019-08-11 10:06:32 +0200296 * dma_fence_get_rcu - get a fence from a dma_resv_list with
Chris Wilsonf54d1862016-10-25 13:00:45 +0100297 * rcu read lock
Daniel Vetter2c269b02018-04-27 08:17:08 +0200298 * @fence: fence to increase refcount of
Chris Wilsonf54d1862016-10-25 13:00:45 +0100299 *
300 * Function returns NULL if no refcount could be obtained, or the fence.
301 */
302static inline struct dma_fence *dma_fence_get_rcu(struct dma_fence *fence)
303{
304 if (kref_get_unless_zero(&fence->refcount))
305 return fence;
306 else
307 return NULL;
308}
309
310/**
311 * dma_fence_get_rcu_safe - acquire a reference to an RCU tracked fence
Daniel Vetter2c269b02018-04-27 08:17:08 +0200312 * @fencep: pointer to fence to increase refcount of
Chris Wilsonf54d1862016-10-25 13:00:45 +0100313 *
314 * Function returns NULL if no refcount could be obtained, or the fence.
315 * This function handles acquiring a reference to a fence that may be
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -0800316 * reallocated within the RCU grace period (such as with SLAB_TYPESAFE_BY_RCU),
Chris Wilsonf54d1862016-10-25 13:00:45 +0100317 * so long as the caller is using RCU on the pointer to the fence.
318 *
319 * An alternative mechanism is to employ a seqlock to protect a bunch of
Christian König52791ee2019-08-11 10:06:32 +0200320 * fences, such as used by struct dma_resv. When using a seqlock,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100321 * the seqlock must be taken before and checked after a reference to the
322 * fence is acquired (as shown here).
323 *
324 * The caller is required to hold the RCU read lock.
325 */
326static inline struct dma_fence *
Chris Wilson5f72db52017-11-02 22:03:34 +0200327dma_fence_get_rcu_safe(struct dma_fence __rcu **fencep)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100328{
329 do {
330 struct dma_fence *fence;
331
332 fence = rcu_dereference(*fencep);
Christian Königf8e07312017-09-15 11:53:07 +0200333 if (!fence)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100334 return NULL;
335
Christian Königf8e07312017-09-15 11:53:07 +0200336 if (!dma_fence_get_rcu(fence))
337 continue;
338
Chris Wilsonf54d1862016-10-25 13:00:45 +0100339 /* The atomic_inc_not_zero() inside dma_fence_get_rcu()
340 * provides a full memory barrier upon success (such as now).
341 * This is paired with the write barrier from assigning
342 * to the __rcu protected fence pointer so that if that
343 * pointer still matches the current fence, we know we
344 * have successfully acquire a reference to it. If it no
345 * longer matches, we are holding a reference to some other
346 * reallocated pointer. This is possible if the allocator
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -0800347 * is using a freelist like SLAB_TYPESAFE_BY_RCU where the
Chris Wilsonf54d1862016-10-25 13:00:45 +0100348 * fence remains valid for the RCU grace period, but it
349 * may be reallocated. When using such allocators, we are
350 * responsible for ensuring the reference we get is to
351 * the right fence, as below.
352 */
353 if (fence == rcu_access_pointer(*fencep))
354 return rcu_pointer_handoff(fence);
355
356 dma_fence_put(fence);
357 } while (1);
358}
359
Daniel Vetter5fbff812020-07-07 22:12:05 +0200360#ifdef CONFIG_LOCKDEP
361bool dma_fence_begin_signalling(void);
362void dma_fence_end_signalling(bool cookie);
Daniel Vetterd0b9a9a2020-07-07 22:12:06 +0200363void __dma_fence_might_wait(void);
Daniel Vetter5fbff812020-07-07 22:12:05 +0200364#else
365static inline bool dma_fence_begin_signalling(void)
366{
367 return true;
368}
369static inline void dma_fence_end_signalling(bool cookie) {}
370static inline void __dma_fence_might_wait(void) {}
371#endif
372
Chris Wilsonf54d1862016-10-25 13:00:45 +0100373int dma_fence_signal(struct dma_fence *fence);
374int dma_fence_signal_locked(struct dma_fence *fence);
375signed long dma_fence_default_wait(struct dma_fence *fence,
376 bool intr, signed long timeout);
377int dma_fence_add_callback(struct dma_fence *fence,
378 struct dma_fence_cb *cb,
379 dma_fence_func_t func);
380bool dma_fence_remove_callback(struct dma_fence *fence,
381 struct dma_fence_cb *cb);
382void dma_fence_enable_sw_signaling(struct dma_fence *fence);
383
384/**
385 * dma_fence_is_signaled_locked - Return an indication if the fence
386 * is signaled yet.
Daniel Vetter2c269b02018-04-27 08:17:08 +0200387 * @fence: the fence to check
Chris Wilsonf54d1862016-10-25 13:00:45 +0100388 *
389 * Returns true if the fence was already signaled, false if not. Since this
390 * function doesn't enable signaling, it is not guaranteed to ever return
Daniel Vetter2c269b02018-04-27 08:17:08 +0200391 * true if dma_fence_add_callback(), dma_fence_wait() or
392 * dma_fence_enable_sw_signaling() haven't been called before.
Chris Wilsonf54d1862016-10-25 13:00:45 +0100393 *
Daniel Vetter2c269b02018-04-27 08:17:08 +0200394 * This function requires &dma_fence.lock to be held.
395 *
396 * See also dma_fence_is_signaled().
Chris Wilsonf54d1862016-10-25 13:00:45 +0100397 */
398static inline bool
399dma_fence_is_signaled_locked(struct dma_fence *fence)
400{
401 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
402 return true;
403
404 if (fence->ops->signaled && fence->ops->signaled(fence)) {
405 dma_fence_signal_locked(fence);
406 return true;
407 }
408
409 return false;
410}
411
412/**
413 * dma_fence_is_signaled - Return an indication if the fence is signaled yet.
Daniel Vetter2c269b02018-04-27 08:17:08 +0200414 * @fence: the fence to check
Chris Wilsonf54d1862016-10-25 13:00:45 +0100415 *
416 * Returns true if the fence was already signaled, false if not. Since this
417 * function doesn't enable signaling, it is not guaranteed to ever return
Daniel Vetter2c269b02018-04-27 08:17:08 +0200418 * true if dma_fence_add_callback(), dma_fence_wait() or
419 * dma_fence_enable_sw_signaling() haven't been called before.
Chris Wilsonf54d1862016-10-25 13:00:45 +0100420 *
421 * It's recommended for seqno fences to call dma_fence_signal when the
422 * operation is complete, it makes it possible to prevent issues from
423 * wraparound between time of issue and time of use by checking the return
424 * value of this function before calling hardware-specific wait instructions.
Daniel Vetter2c269b02018-04-27 08:17:08 +0200425 *
426 * See also dma_fence_is_signaled_locked().
Chris Wilsonf54d1862016-10-25 13:00:45 +0100427 */
428static inline bool
429dma_fence_is_signaled(struct dma_fence *fence)
430{
431 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
432 return true;
433
434 if (fence->ops->signaled && fence->ops->signaled(fence)) {
435 dma_fence_signal(fence);
436 return true;
437 }
438
439 return false;
440}
441
442/**
Chris Wilson81114772017-06-29 13:59:24 +0100443 * __dma_fence_is_later - return if f1 is chronologically later than f2
Daniel Vetter2c269b02018-04-27 08:17:08 +0200444 * @f1: the first fence's seqno
445 * @f2: the second fence's seqno from the same context
Christian König5e498ab2019-04-15 14:46:34 +0200446 * @ops: dma_fence_ops associated with the seqno
Chris Wilson81114772017-06-29 13:59:24 +0100447 *
448 * Returns true if f1 is chronologically later than f2. Both fences must be
449 * from the same context, since a seqno is not common across contexts.
450 */
Christian König5e498ab2019-04-15 14:46:34 +0200451static inline bool __dma_fence_is_later(u64 f1, u64 f2,
452 const struct dma_fence_ops *ops)
Chris Wilson81114772017-06-29 13:59:24 +0100453{
Christian Königb312d8c2018-11-14 16:11:06 +0100454 /* This is for backward compatibility with drivers which can only handle
Christian König5e498ab2019-04-15 14:46:34 +0200455 * 32bit sequence numbers. Use a 64bit compare when the driver says to
456 * do so.
Christian Königb312d8c2018-11-14 16:11:06 +0100457 */
Christian König5e498ab2019-04-15 14:46:34 +0200458 if (ops->use_64bit_seqno)
Christian Königb312d8c2018-11-14 16:11:06 +0100459 return f1 > f2;
460
461 return (int)(lower_32_bits(f1) - lower_32_bits(f2)) > 0;
Chris Wilson81114772017-06-29 13:59:24 +0100462}
463
464/**
Chris Wilsonf54d1862016-10-25 13:00:45 +0100465 * dma_fence_is_later - return if f1 is chronologically later than f2
Daniel Vetter2c269b02018-04-27 08:17:08 +0200466 * @f1: the first fence from the same context
467 * @f2: the second fence from the same context
Chris Wilsonf54d1862016-10-25 13:00:45 +0100468 *
469 * Returns true if f1 is chronologically later than f2. Both fences must be
470 * from the same context, since a seqno is not re-used across contexts.
471 */
472static inline bool dma_fence_is_later(struct dma_fence *f1,
473 struct dma_fence *f2)
474{
475 if (WARN_ON(f1->context != f2->context))
476 return false;
477
Christian König5e498ab2019-04-15 14:46:34 +0200478 return __dma_fence_is_later(f1->seqno, f2->seqno, f1->ops);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100479}
480
481/**
482 * dma_fence_later - return the chronologically later fence
Daniel Vetter2c269b02018-04-27 08:17:08 +0200483 * @f1: the first fence from the same context
484 * @f2: the second fence from the same context
Chris Wilsonf54d1862016-10-25 13:00:45 +0100485 *
486 * Returns NULL if both fences are signaled, otherwise the fence that would be
487 * signaled last. Both fences must be from the same context, since a seqno is
488 * not re-used across contexts.
489 */
490static inline struct dma_fence *dma_fence_later(struct dma_fence *f1,
491 struct dma_fence *f2)
492{
493 if (WARN_ON(f1->context != f2->context))
494 return NULL;
495
496 /*
497 * Can't check just DMA_FENCE_FLAG_SIGNALED_BIT here, it may never
498 * have been set if enable_signaling wasn't called, and enabling that
499 * here is overkill.
500 */
501 if (dma_fence_is_later(f1, f2))
502 return dma_fence_is_signaled(f1) ? NULL : f1;
503 else
504 return dma_fence_is_signaled(f2) ? NULL : f2;
505}
506
Chris Wilsond6c99f42017-01-04 14:12:21 +0000507/**
508 * dma_fence_get_status_locked - returns the status upon completion
Daniel Vetter2c269b02018-04-27 08:17:08 +0200509 * @fence: the dma_fence to query
Chris Wilsond6c99f42017-01-04 14:12:21 +0000510 *
511 * Drivers can supply an optional error status condition before they signal
512 * the fence (to indicate whether the fence was completed due to an error
513 * rather than success). The value of the status condition is only valid
514 * if the fence has been signaled, dma_fence_get_status_locked() first checks
515 * the signal state before reporting the error status.
516 *
517 * Returns 0 if the fence has not yet been signaled, 1 if the fence has
518 * been signaled without an error condition, or a negative error code
519 * if the fence has been completed in err.
520 */
521static inline int dma_fence_get_status_locked(struct dma_fence *fence)
522{
523 if (dma_fence_is_signaled_locked(fence))
Chris Wilsona009e972017-01-04 14:12:22 +0000524 return fence->error ?: 1;
Chris Wilsond6c99f42017-01-04 14:12:21 +0000525 else
526 return 0;
527}
528
529int dma_fence_get_status(struct dma_fence *fence);
530
Chris Wilsona009e972017-01-04 14:12:22 +0000531/**
532 * dma_fence_set_error - flag an error condition on the fence
Daniel Vetter2c269b02018-04-27 08:17:08 +0200533 * @fence: the dma_fence
534 * @error: the error to store
Chris Wilsona009e972017-01-04 14:12:22 +0000535 *
536 * Drivers can supply an optional error status condition before they signal
537 * the fence, to indicate that the fence was completed due to an error
538 * rather than success. This must be set before signaling (so that the value
539 * is visible before any waiters on the signal callback are woken). This
540 * helper exists to help catching erroneous setting of #dma_fence.error.
541 */
542static inline void dma_fence_set_error(struct dma_fence *fence,
543 int error)
544{
Daniel Vetter6ce31262017-07-20 14:51:07 +0200545 WARN_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags));
546 WARN_ON(error >= 0 || error < -MAX_ERRNO);
Chris Wilsona009e972017-01-04 14:12:22 +0000547
548 fence->error = error;
549}
550
Chris Wilsonf54d1862016-10-25 13:00:45 +0100551signed long dma_fence_wait_timeout(struct dma_fence *,
552 bool intr, signed long timeout);
553signed long dma_fence_wait_any_timeout(struct dma_fence **fences,
554 uint32_t count,
monk.liu7392b4b2016-11-04 16:16:09 -0400555 bool intr, signed long timeout,
556 uint32_t *idx);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100557
558/**
559 * dma_fence_wait - sleep until the fence gets signaled
Daniel Vetter2c269b02018-04-27 08:17:08 +0200560 * @fence: the fence to wait on
561 * @intr: if true, do an interruptible wait
Chris Wilsonf54d1862016-10-25 13:00:45 +0100562 *
563 * This function will return -ERESTARTSYS if interrupted by a signal,
564 * or 0 if the fence was signaled. Other error values may be
565 * returned on custom implementations.
566 *
567 * Performs a synchronous wait on this fence. It is assumed the caller
568 * directly or indirectly holds a reference to the fence, otherwise the
569 * fence might be freed before return, resulting in undefined behavior.
Daniel Vetter2c269b02018-04-27 08:17:08 +0200570 *
571 * See also dma_fence_wait_timeout() and dma_fence_wait_any_timeout().
Chris Wilsonf54d1862016-10-25 13:00:45 +0100572 */
573static inline signed long dma_fence_wait(struct dma_fence *fence, bool intr)
574{
575 signed long ret;
576
577 /* Since dma_fence_wait_timeout cannot timeout with
578 * MAX_SCHEDULE_TIMEOUT, only valid return values are
579 * -ERESTARTSYS and MAX_SCHEDULE_TIMEOUT.
580 */
581 ret = dma_fence_wait_timeout(fence, intr, MAX_SCHEDULE_TIMEOUT);
582
583 return ret < 0 ? ret : 0;
584}
585
Christian König078dec332018-12-03 13:36:14 +0100586struct dma_fence *dma_fence_get_stub(void);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100587u64 dma_fence_context_alloc(unsigned num);
588
589#define DMA_FENCE_TRACE(f, fmt, args...) \
590 do { \
591 struct dma_fence *__ff = (f); \
592 if (IS_ENABLED(CONFIG_DMA_FENCE_TRACE)) \
Christian Königb312d8c2018-11-14 16:11:06 +0100593 pr_info("f %llu#%llu: " fmt, \
Chris Wilsonf54d1862016-10-25 13:00:45 +0100594 __ff->context, __ff->seqno, ##args); \
595 } while (0)
596
597#define DMA_FENCE_WARN(f, fmt, args...) \
598 do { \
599 struct dma_fence *__ff = (f); \
Christian Königb312d8c2018-11-14 16:11:06 +0100600 pr_warn("f %llu#%llu: " fmt, __ff->context, __ff->seqno,\
Chris Wilsonf54d1862016-10-25 13:00:45 +0100601 ##args); \
602 } while (0)
603
604#define DMA_FENCE_ERR(f, fmt, args...) \
605 do { \
606 struct dma_fence *__ff = (f); \
Christian Königb312d8c2018-11-14 16:11:06 +0100607 pr_err("f %llu#%llu: " fmt, __ff->context, __ff->seqno, \
Chris Wilsonf54d1862016-10-25 13:00:45 +0100608 ##args); \
609 } while (0)
610
611#endif /* __LINUX_DMA_FENCE_H */