1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-only */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Fence mechanism for dma-buf to allow for asynchronous dma access
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2012 Canonical Ltd
6*4882a593Smuzhiyun * Copyright (C) 2012 Texas Instruments
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Authors:
9*4882a593Smuzhiyun * Rob Clark <robdclark@gmail.com>
10*4882a593Smuzhiyun * Maarten Lankhorst <maarten.lankhorst@canonical.com>
11*4882a593Smuzhiyun */
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #ifndef __LINUX_DMA_FENCE_H
14*4882a593Smuzhiyun #define __LINUX_DMA_FENCE_H
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun #include <linux/err.h>
17*4882a593Smuzhiyun #include <linux/wait.h>
18*4882a593Smuzhiyun #include <linux/list.h>
19*4882a593Smuzhiyun #include <linux/bitops.h>
20*4882a593Smuzhiyun #include <linux/kref.h>
21*4882a593Smuzhiyun #include <linux/sched.h>
22*4882a593Smuzhiyun #include <linux/printk.h>
23*4882a593Smuzhiyun #include <linux/rcupdate.h>
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun struct dma_fence;
26*4882a593Smuzhiyun struct dma_fence_ops;
27*4882a593Smuzhiyun struct dma_fence_cb;
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun /**
30*4882a593Smuzhiyun * struct dma_fence - software synchronization primitive
31*4882a593Smuzhiyun * @refcount: refcount for this fence
32*4882a593Smuzhiyun * @ops: dma_fence_ops associated with this fence
33*4882a593Smuzhiyun * @rcu: used for releasing fence with kfree_rcu
34*4882a593Smuzhiyun * @cb_list: list of all callbacks to call
35*4882a593Smuzhiyun * @lock: spin_lock_irqsave used for locking
36*4882a593Smuzhiyun * @context: execution context this fence belongs to, returned by
37*4882a593Smuzhiyun * dma_fence_context_alloc()
38*4882a593Smuzhiyun * @seqno: the sequence number of this fence inside the execution context,
39*4882a593Smuzhiyun * can be compared to decide which fence would be signaled later.
40*4882a593Smuzhiyun * @flags: A mask of DMA_FENCE_FLAG_* defined below
41*4882a593Smuzhiyun * @timestamp: Timestamp when the fence was signaled.
42*4882a593Smuzhiyun * @error: Optional, only valid if < 0, must be set before calling
43*4882a593Smuzhiyun * dma_fence_signal, indicates that the fence has completed with an error.
44*4882a593Smuzhiyun *
45*4882a593Smuzhiyun * the flags member must be manipulated and read using the appropriate
46*4882a593Smuzhiyun * atomic ops (bit_*), so taking the spinlock will not be needed most
47*4882a593Smuzhiyun * of the time.
48*4882a593Smuzhiyun *
49*4882a593Smuzhiyun * DMA_FENCE_FLAG_SIGNALED_BIT - fence is already signaled
50*4882a593Smuzhiyun * DMA_FENCE_FLAG_TIMESTAMP_BIT - timestamp recorded for fence signaling
51*4882a593Smuzhiyun * DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT - enable_signaling might have been called
52*4882a593Smuzhiyun * DMA_FENCE_FLAG_USER_BITS - start of the unused bits, can be used by the
53*4882a593Smuzhiyun * implementer of the fence for its own purposes. Can be used in different
54*4882a593Smuzhiyun * ways by different fence implementers, so do not rely on this.
55*4882a593Smuzhiyun *
56*4882a593Smuzhiyun * Since atomic bitops are used, this is not guaranteed to be the case.
57*4882a593Smuzhiyun * Particularly, if the bit was set, but dma_fence_signal was called right
58*4882a593Smuzhiyun * before this bit was set, it would have been able to set the
59*4882a593Smuzhiyun * DMA_FENCE_FLAG_SIGNALED_BIT, before enable_signaling was called.
60*4882a593Smuzhiyun * Adding a check for DMA_FENCE_FLAG_SIGNALED_BIT after setting
61*4882a593Smuzhiyun * DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT closes this race, and makes sure that
62*4882a593Smuzhiyun * after dma_fence_signal was called, any enable_signaling call will have either
63*4882a593Smuzhiyun * been completed, or never called at all.
64*4882a593Smuzhiyun */
65*4882a593Smuzhiyun struct dma_fence {
66*4882a593Smuzhiyun spinlock_t *lock;
67*4882a593Smuzhiyun const struct dma_fence_ops *ops;
68*4882a593Smuzhiyun /*
69*4882a593Smuzhiyun * We clear the callback list on kref_put so that by the time we
70*4882a593Smuzhiyun * release the fence it is unused. No one should be adding to the
71*4882a593Smuzhiyun * cb_list that they don't themselves hold a reference for.
72*4882a593Smuzhiyun *
73*4882a593Smuzhiyun * The lifetime of the timestamp is similarly tied to both the
74*4882a593Smuzhiyun * rcu freelist and the cb_list. The timestamp is only set upon
75*4882a593Smuzhiyun * signaling while simultaneously notifying the cb_list. Ergo, we
76*4882a593Smuzhiyun * only use either the cb_list of timestamp. Upon destruction,
77*4882a593Smuzhiyun * neither are accessible, and so we can use the rcu. This means
78*4882a593Smuzhiyun * that the cb_list is *only* valid until the signal bit is set,
79*4882a593Smuzhiyun * and to read either you *must* hold a reference to the fence,
80*4882a593Smuzhiyun * and not just the rcu_read_lock.
81*4882a593Smuzhiyun *
82*4882a593Smuzhiyun * Listed in chronological order.
83*4882a593Smuzhiyun */
84*4882a593Smuzhiyun union {
85*4882a593Smuzhiyun struct list_head cb_list;
86*4882a593Smuzhiyun /* @cb_list replaced by @timestamp on dma_fence_signal() */
87*4882a593Smuzhiyun ktime_t timestamp;
88*4882a593Smuzhiyun /* @timestamp replaced by @rcu on dma_fence_release() */
89*4882a593Smuzhiyun struct rcu_head rcu;
90*4882a593Smuzhiyun };
91*4882a593Smuzhiyun u64 context;
92*4882a593Smuzhiyun u64 seqno;
93*4882a593Smuzhiyun unsigned long flags;
94*4882a593Smuzhiyun struct kref refcount;
95*4882a593Smuzhiyun int error;
96*4882a593Smuzhiyun };
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun enum dma_fence_flag_bits {
99*4882a593Smuzhiyun DMA_FENCE_FLAG_SIGNALED_BIT,
100*4882a593Smuzhiyun DMA_FENCE_FLAG_TIMESTAMP_BIT,
101*4882a593Smuzhiyun DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
102*4882a593Smuzhiyun DMA_FENCE_FLAG_USER_BITS, /* must always be last member */
103*4882a593Smuzhiyun };
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun typedef void (*dma_fence_func_t)(struct dma_fence *fence,
106*4882a593Smuzhiyun struct dma_fence_cb *cb);
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun /**
109*4882a593Smuzhiyun * struct dma_fence_cb - callback for dma_fence_add_callback()
110*4882a593Smuzhiyun * @node: used by dma_fence_add_callback() to append this struct to fence::cb_list
111*4882a593Smuzhiyun * @func: dma_fence_func_t to call
112*4882a593Smuzhiyun *
113*4882a593Smuzhiyun * This struct will be initialized by dma_fence_add_callback(), additional
114*4882a593Smuzhiyun * data can be passed along by embedding dma_fence_cb in another struct.
115*4882a593Smuzhiyun */
116*4882a593Smuzhiyun struct dma_fence_cb {
117*4882a593Smuzhiyun struct list_head node;
118*4882a593Smuzhiyun dma_fence_func_t func;
119*4882a593Smuzhiyun };
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun /**
122*4882a593Smuzhiyun * struct dma_fence_ops - operations implemented for fence
123*4882a593Smuzhiyun *
124*4882a593Smuzhiyun */
125*4882a593Smuzhiyun struct dma_fence_ops {
126*4882a593Smuzhiyun /**
127*4882a593Smuzhiyun * @use_64bit_seqno:
128*4882a593Smuzhiyun *
129*4882a593Smuzhiyun * True if this dma_fence implementation uses 64bit seqno, false
130*4882a593Smuzhiyun * otherwise.
131*4882a593Smuzhiyun */
132*4882a593Smuzhiyun bool use_64bit_seqno;
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun /**
135*4882a593Smuzhiyun * @get_driver_name:
136*4882a593Smuzhiyun *
137*4882a593Smuzhiyun * Returns the driver name. This is a callback to allow drivers to
138*4882a593Smuzhiyun * compute the name at runtime, without having it to store permanently
139*4882a593Smuzhiyun * for each fence, or build a cache of some sort.
140*4882a593Smuzhiyun *
141*4882a593Smuzhiyun * This callback is mandatory.
142*4882a593Smuzhiyun */
143*4882a593Smuzhiyun const char * (*get_driver_name)(struct dma_fence *fence);
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun /**
146*4882a593Smuzhiyun * @get_timeline_name:
147*4882a593Smuzhiyun *
148*4882a593Smuzhiyun * Return the name of the context this fence belongs to. This is a
149*4882a593Smuzhiyun * callback to allow drivers to compute the name at runtime, without
150*4882a593Smuzhiyun * having it to store permanently for each fence, or build a cache of
151*4882a593Smuzhiyun * some sort.
152*4882a593Smuzhiyun *
153*4882a593Smuzhiyun * This callback is mandatory.
154*4882a593Smuzhiyun */
155*4882a593Smuzhiyun const char * (*get_timeline_name)(struct dma_fence *fence);
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun /**
158*4882a593Smuzhiyun * @enable_signaling:
159*4882a593Smuzhiyun *
160*4882a593Smuzhiyun * Enable software signaling of fence.
161*4882a593Smuzhiyun *
162*4882a593Smuzhiyun * For fence implementations that have the capability for hw->hw
163*4882a593Smuzhiyun * signaling, they can implement this op to enable the necessary
164*4882a593Smuzhiyun * interrupts, or insert commands into cmdstream, etc, to avoid these
165*4882a593Smuzhiyun * costly operations for the common case where only hw->hw
166*4882a593Smuzhiyun * synchronization is required. This is called in the first
167*4882a593Smuzhiyun * dma_fence_wait() or dma_fence_add_callback() path to let the fence
168*4882a593Smuzhiyun * implementation know that there is another driver waiting on the
169*4882a593Smuzhiyun * signal (ie. hw->sw case).
170*4882a593Smuzhiyun *
171*4882a593Smuzhiyun * This function can be called from atomic context, but not
172*4882a593Smuzhiyun * from irq context, so normal spinlocks can be used.
173*4882a593Smuzhiyun *
174*4882a593Smuzhiyun * A return value of false indicates the fence already passed,
175*4882a593Smuzhiyun * or some failure occurred that made it impossible to enable
176*4882a593Smuzhiyun * signaling. True indicates successful enabling.
177*4882a593Smuzhiyun *
178*4882a593Smuzhiyun * &dma_fence.error may be set in enable_signaling, but only when false
179*4882a593Smuzhiyun * is returned.
180*4882a593Smuzhiyun *
181*4882a593Smuzhiyun * Since many implementations can call dma_fence_signal() even when before
182*4882a593Smuzhiyun * @enable_signaling has been called there's a race window, where the
183*4882a593Smuzhiyun * dma_fence_signal() might result in the final fence reference being
184*4882a593Smuzhiyun * released and its memory freed. To avoid this, implementations of this
185*4882a593Smuzhiyun * callback should grab their own reference using dma_fence_get(), to be
186*4882a593Smuzhiyun * released when the fence is signalled (through e.g. the interrupt
187*4882a593Smuzhiyun * handler).
188*4882a593Smuzhiyun *
189*4882a593Smuzhiyun * This callback is optional. If this callback is not present, then the
190*4882a593Smuzhiyun * driver must always have signaling enabled.
191*4882a593Smuzhiyun */
192*4882a593Smuzhiyun bool (*enable_signaling)(struct dma_fence *fence);
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun /**
195*4882a593Smuzhiyun * @signaled:
196*4882a593Smuzhiyun *
197*4882a593Smuzhiyun * Peek whether the fence is signaled, as a fastpath optimization for
198*4882a593Smuzhiyun * e.g. dma_fence_wait() or dma_fence_add_callback(). Note that this
199*4882a593Smuzhiyun * callback does not need to make any guarantees beyond that a fence
200*4882a593Smuzhiyun * once indicates as signalled must always return true from this
201*4882a593Smuzhiyun * callback. This callback may return false even if the fence has
202*4882a593Smuzhiyun * completed already, in this case information hasn't propogated throug
203*4882a593Smuzhiyun * the system yet. See also dma_fence_is_signaled().
204*4882a593Smuzhiyun *
205*4882a593Smuzhiyun * May set &dma_fence.error if returning true.
206*4882a593Smuzhiyun *
207*4882a593Smuzhiyun * This callback is optional.
208*4882a593Smuzhiyun */
209*4882a593Smuzhiyun bool (*signaled)(struct dma_fence *fence);
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun /**
212*4882a593Smuzhiyun * @wait:
213*4882a593Smuzhiyun *
214*4882a593Smuzhiyun * Custom wait implementation, defaults to dma_fence_default_wait() if
215*4882a593Smuzhiyun * not set.
216*4882a593Smuzhiyun *
217*4882a593Smuzhiyun * The dma_fence_default_wait implementation should work for any fence, as long
218*4882a593Smuzhiyun * as @enable_signaling works correctly. This hook allows drivers to
219*4882a593Smuzhiyun * have an optimized version for the case where a process context is
220*4882a593Smuzhiyun * already available, e.g. if @enable_signaling for the general case
221*4882a593Smuzhiyun * needs to set up a worker thread.
222*4882a593Smuzhiyun *
223*4882a593Smuzhiyun * Must return -ERESTARTSYS if the wait is intr = true and the wait was
224*4882a593Smuzhiyun * interrupted, and remaining jiffies if fence has signaled, or 0 if wait
225*4882a593Smuzhiyun * timed out. Can also return other error values on custom implementations,
226*4882a593Smuzhiyun * which should be treated as if the fence is signaled. For example a hardware
227*4882a593Smuzhiyun * lockup could be reported like that.
228*4882a593Smuzhiyun *
229*4882a593Smuzhiyun * This callback is optional.
230*4882a593Smuzhiyun */
231*4882a593Smuzhiyun signed long (*wait)(struct dma_fence *fence,
232*4882a593Smuzhiyun bool intr, signed long timeout);
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun /**
235*4882a593Smuzhiyun * @release:
236*4882a593Smuzhiyun *
237*4882a593Smuzhiyun * Called on destruction of fence to release additional resources.
238*4882a593Smuzhiyun * Can be called from irq context. This callback is optional. If it is
239*4882a593Smuzhiyun * NULL, then dma_fence_free() is instead called as the default
240*4882a593Smuzhiyun * implementation.
241*4882a593Smuzhiyun */
242*4882a593Smuzhiyun void (*release)(struct dma_fence *fence);
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun /**
245*4882a593Smuzhiyun * @fence_value_str:
246*4882a593Smuzhiyun *
247*4882a593Smuzhiyun * Callback to fill in free-form debug info specific to this fence, like
248*4882a593Smuzhiyun * the sequence number.
249*4882a593Smuzhiyun *
250*4882a593Smuzhiyun * This callback is optional.
251*4882a593Smuzhiyun */
252*4882a593Smuzhiyun void (*fence_value_str)(struct dma_fence *fence, char *str, int size);
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun /**
255*4882a593Smuzhiyun * @timeline_value_str:
256*4882a593Smuzhiyun *
257*4882a593Smuzhiyun * Fills in the current value of the timeline as a string, like the
258*4882a593Smuzhiyun * sequence number. Note that the specific fence passed to this function
259*4882a593Smuzhiyun * should not matter, drivers should only use it to look up the
260*4882a593Smuzhiyun * corresponding timeline structures.
261*4882a593Smuzhiyun */
262*4882a593Smuzhiyun void (*timeline_value_str)(struct dma_fence *fence,
263*4882a593Smuzhiyun char *str, int size);
264*4882a593Smuzhiyun };
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun void dma_fence_init(struct dma_fence *fence, const struct dma_fence_ops *ops,
267*4882a593Smuzhiyun spinlock_t *lock, u64 context, u64 seqno);
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun void dma_fence_release(struct kref *kref);
270*4882a593Smuzhiyun void dma_fence_free(struct dma_fence *fence);
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun /**
273*4882a593Smuzhiyun * dma_fence_put - decreases refcount of the fence
274*4882a593Smuzhiyun * @fence: fence to reduce refcount of
275*4882a593Smuzhiyun */
dma_fence_put(struct dma_fence * fence)276*4882a593Smuzhiyun static inline void dma_fence_put(struct dma_fence *fence)
277*4882a593Smuzhiyun {
278*4882a593Smuzhiyun if (fence)
279*4882a593Smuzhiyun kref_put(&fence->refcount, dma_fence_release);
280*4882a593Smuzhiyun }
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun /**
283*4882a593Smuzhiyun * dma_fence_get - increases refcount of the fence
284*4882a593Smuzhiyun * @fence: fence to increase refcount of
285*4882a593Smuzhiyun *
286*4882a593Smuzhiyun * Returns the same fence, with refcount increased by 1.
287*4882a593Smuzhiyun */
dma_fence_get(struct dma_fence * fence)288*4882a593Smuzhiyun static inline struct dma_fence *dma_fence_get(struct dma_fence *fence)
289*4882a593Smuzhiyun {
290*4882a593Smuzhiyun if (fence)
291*4882a593Smuzhiyun kref_get(&fence->refcount);
292*4882a593Smuzhiyun return fence;
293*4882a593Smuzhiyun }
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun /**
296*4882a593Smuzhiyun * dma_fence_get_rcu - get a fence from a dma_resv_list with
297*4882a593Smuzhiyun * rcu read lock
298*4882a593Smuzhiyun * @fence: fence to increase refcount of
299*4882a593Smuzhiyun *
300*4882a593Smuzhiyun * Function returns NULL if no refcount could be obtained, or the fence.
301*4882a593Smuzhiyun */
dma_fence_get_rcu(struct dma_fence * fence)302*4882a593Smuzhiyun static inline struct dma_fence *dma_fence_get_rcu(struct dma_fence *fence)
303*4882a593Smuzhiyun {
304*4882a593Smuzhiyun if (kref_get_unless_zero(&fence->refcount))
305*4882a593Smuzhiyun return fence;
306*4882a593Smuzhiyun else
307*4882a593Smuzhiyun return NULL;
308*4882a593Smuzhiyun }
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun /**
311*4882a593Smuzhiyun * dma_fence_get_rcu_safe - acquire a reference to an RCU tracked fence
312*4882a593Smuzhiyun * @fencep: pointer to fence to increase refcount of
313*4882a593Smuzhiyun *
314*4882a593Smuzhiyun * Function returns NULL if no refcount could be obtained, or the fence.
315*4882a593Smuzhiyun * This function handles acquiring a reference to a fence that may be
316*4882a593Smuzhiyun * reallocated within the RCU grace period (such as with SLAB_TYPESAFE_BY_RCU),
317*4882a593Smuzhiyun * so long as the caller is using RCU on the pointer to the fence.
318*4882a593Smuzhiyun *
319*4882a593Smuzhiyun * An alternative mechanism is to employ a seqlock to protect a bunch of
320*4882a593Smuzhiyun * fences, such as used by struct dma_resv. When using a seqlock,
321*4882a593Smuzhiyun * the seqlock must be taken before and checked after a reference to the
322*4882a593Smuzhiyun * fence is acquired (as shown here).
323*4882a593Smuzhiyun *
324*4882a593Smuzhiyun * The caller is required to hold the RCU read lock.
325*4882a593Smuzhiyun */
326*4882a593Smuzhiyun static inline struct dma_fence *
dma_fence_get_rcu_safe(struct dma_fence __rcu ** fencep)327*4882a593Smuzhiyun dma_fence_get_rcu_safe(struct dma_fence __rcu **fencep)
328*4882a593Smuzhiyun {
329*4882a593Smuzhiyun do {
330*4882a593Smuzhiyun struct dma_fence *fence;
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun fence = rcu_dereference(*fencep);
333*4882a593Smuzhiyun if (!fence)
334*4882a593Smuzhiyun return NULL;
335*4882a593Smuzhiyun
336*4882a593Smuzhiyun if (!dma_fence_get_rcu(fence))
337*4882a593Smuzhiyun continue;
338*4882a593Smuzhiyun
339*4882a593Smuzhiyun /* The atomic_inc_not_zero() inside dma_fence_get_rcu()
340*4882a593Smuzhiyun * provides a full memory barrier upon success (such as now).
341*4882a593Smuzhiyun * This is paired with the write barrier from assigning
342*4882a593Smuzhiyun * to the __rcu protected fence pointer so that if that
343*4882a593Smuzhiyun * pointer still matches the current fence, we know we
344*4882a593Smuzhiyun * have successfully acquire a reference to it. If it no
345*4882a593Smuzhiyun * longer matches, we are holding a reference to some other
346*4882a593Smuzhiyun * reallocated pointer. This is possible if the allocator
347*4882a593Smuzhiyun * is using a freelist like SLAB_TYPESAFE_BY_RCU where the
348*4882a593Smuzhiyun * fence remains valid for the RCU grace period, but it
349*4882a593Smuzhiyun * may be reallocated. When using such allocators, we are
350*4882a593Smuzhiyun * responsible for ensuring the reference we get is to
351*4882a593Smuzhiyun * the right fence, as below.
352*4882a593Smuzhiyun */
353*4882a593Smuzhiyun if (fence == rcu_access_pointer(*fencep))
354*4882a593Smuzhiyun return rcu_pointer_handoff(fence);
355*4882a593Smuzhiyun
356*4882a593Smuzhiyun dma_fence_put(fence);
357*4882a593Smuzhiyun } while (1);
358*4882a593Smuzhiyun }
359*4882a593Smuzhiyun
360*4882a593Smuzhiyun #ifdef CONFIG_LOCKDEP
361*4882a593Smuzhiyun bool dma_fence_begin_signalling(void);
362*4882a593Smuzhiyun void dma_fence_end_signalling(bool cookie);
363*4882a593Smuzhiyun void __dma_fence_might_wait(void);
364*4882a593Smuzhiyun #else
dma_fence_begin_signalling(void)365*4882a593Smuzhiyun static inline bool dma_fence_begin_signalling(void)
366*4882a593Smuzhiyun {
367*4882a593Smuzhiyun return true;
368*4882a593Smuzhiyun }
dma_fence_end_signalling(bool cookie)369*4882a593Smuzhiyun static inline void dma_fence_end_signalling(bool cookie) {}
__dma_fence_might_wait(void)370*4882a593Smuzhiyun static inline void __dma_fence_might_wait(void) {}
371*4882a593Smuzhiyun #endif
372*4882a593Smuzhiyun
373*4882a593Smuzhiyun int dma_fence_signal(struct dma_fence *fence);
374*4882a593Smuzhiyun int dma_fence_signal_locked(struct dma_fence *fence);
375*4882a593Smuzhiyun int dma_fence_signal_timestamp(struct dma_fence *fence, ktime_t timestamp);
376*4882a593Smuzhiyun int dma_fence_signal_timestamp_locked(struct dma_fence *fence,
377*4882a593Smuzhiyun ktime_t timestamp);
378*4882a593Smuzhiyun signed long dma_fence_default_wait(struct dma_fence *fence,
379*4882a593Smuzhiyun bool intr, signed long timeout);
380*4882a593Smuzhiyun int dma_fence_add_callback(struct dma_fence *fence,
381*4882a593Smuzhiyun struct dma_fence_cb *cb,
382*4882a593Smuzhiyun dma_fence_func_t func);
383*4882a593Smuzhiyun bool dma_fence_remove_callback(struct dma_fence *fence,
384*4882a593Smuzhiyun struct dma_fence_cb *cb);
385*4882a593Smuzhiyun void dma_fence_enable_sw_signaling(struct dma_fence *fence);
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun /**
388*4882a593Smuzhiyun * dma_fence_is_signaled_locked - Return an indication if the fence
389*4882a593Smuzhiyun * is signaled yet.
390*4882a593Smuzhiyun * @fence: the fence to check
391*4882a593Smuzhiyun *
392*4882a593Smuzhiyun * Returns true if the fence was already signaled, false if not. Since this
393*4882a593Smuzhiyun * function doesn't enable signaling, it is not guaranteed to ever return
394*4882a593Smuzhiyun * true if dma_fence_add_callback(), dma_fence_wait() or
395*4882a593Smuzhiyun * dma_fence_enable_sw_signaling() haven't been called before.
396*4882a593Smuzhiyun *
397*4882a593Smuzhiyun * This function requires &dma_fence.lock to be held.
398*4882a593Smuzhiyun *
399*4882a593Smuzhiyun * See also dma_fence_is_signaled().
400*4882a593Smuzhiyun */
401*4882a593Smuzhiyun static inline bool
dma_fence_is_signaled_locked(struct dma_fence * fence)402*4882a593Smuzhiyun dma_fence_is_signaled_locked(struct dma_fence *fence)
403*4882a593Smuzhiyun {
404*4882a593Smuzhiyun if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
405*4882a593Smuzhiyun return true;
406*4882a593Smuzhiyun
407*4882a593Smuzhiyun if (fence->ops->signaled && fence->ops->signaled(fence)) {
408*4882a593Smuzhiyun dma_fence_signal_locked(fence);
409*4882a593Smuzhiyun return true;
410*4882a593Smuzhiyun }
411*4882a593Smuzhiyun
412*4882a593Smuzhiyun return false;
413*4882a593Smuzhiyun }
414*4882a593Smuzhiyun
415*4882a593Smuzhiyun /**
416*4882a593Smuzhiyun * dma_fence_is_signaled - Return an indication if the fence is signaled yet.
417*4882a593Smuzhiyun * @fence: the fence to check
418*4882a593Smuzhiyun *
419*4882a593Smuzhiyun * Returns true if the fence was already signaled, false if not. Since this
420*4882a593Smuzhiyun * function doesn't enable signaling, it is not guaranteed to ever return
421*4882a593Smuzhiyun * true if dma_fence_add_callback(), dma_fence_wait() or
422*4882a593Smuzhiyun * dma_fence_enable_sw_signaling() haven't been called before.
423*4882a593Smuzhiyun *
424*4882a593Smuzhiyun * It's recommended for seqno fences to call dma_fence_signal when the
425*4882a593Smuzhiyun * operation is complete, it makes it possible to prevent issues from
426*4882a593Smuzhiyun * wraparound between time of issue and time of use by checking the return
427*4882a593Smuzhiyun * value of this function before calling hardware-specific wait instructions.
428*4882a593Smuzhiyun *
429*4882a593Smuzhiyun * See also dma_fence_is_signaled_locked().
430*4882a593Smuzhiyun */
431*4882a593Smuzhiyun static inline bool
dma_fence_is_signaled(struct dma_fence * fence)432*4882a593Smuzhiyun dma_fence_is_signaled(struct dma_fence *fence)
433*4882a593Smuzhiyun {
434*4882a593Smuzhiyun if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
435*4882a593Smuzhiyun return true;
436*4882a593Smuzhiyun
437*4882a593Smuzhiyun if (fence->ops->signaled && fence->ops->signaled(fence)) {
438*4882a593Smuzhiyun dma_fence_signal(fence);
439*4882a593Smuzhiyun return true;
440*4882a593Smuzhiyun }
441*4882a593Smuzhiyun
442*4882a593Smuzhiyun return false;
443*4882a593Smuzhiyun }
444*4882a593Smuzhiyun
445*4882a593Smuzhiyun /**
446*4882a593Smuzhiyun * __dma_fence_is_later - return if f1 is chronologically later than f2
447*4882a593Smuzhiyun * @f1: the first fence's seqno
448*4882a593Smuzhiyun * @f2: the second fence's seqno from the same context
449*4882a593Smuzhiyun * @ops: dma_fence_ops associated with the seqno
450*4882a593Smuzhiyun *
451*4882a593Smuzhiyun * Returns true if f1 is chronologically later than f2. Both fences must be
452*4882a593Smuzhiyun * from the same context, since a seqno is not common across contexts.
453*4882a593Smuzhiyun */
__dma_fence_is_later(u64 f1,u64 f2,const struct dma_fence_ops * ops)454*4882a593Smuzhiyun static inline bool __dma_fence_is_later(u64 f1, u64 f2,
455*4882a593Smuzhiyun const struct dma_fence_ops *ops)
456*4882a593Smuzhiyun {
457*4882a593Smuzhiyun /* This is for backward compatibility with drivers which can only handle
458*4882a593Smuzhiyun * 32bit sequence numbers. Use a 64bit compare when the driver says to
459*4882a593Smuzhiyun * do so.
460*4882a593Smuzhiyun */
461*4882a593Smuzhiyun if (ops->use_64bit_seqno)
462*4882a593Smuzhiyun return f1 > f2;
463*4882a593Smuzhiyun
464*4882a593Smuzhiyun return (int)(lower_32_bits(f1) - lower_32_bits(f2)) > 0;
465*4882a593Smuzhiyun }
466*4882a593Smuzhiyun
467*4882a593Smuzhiyun /**
468*4882a593Smuzhiyun * dma_fence_is_later - return if f1 is chronologically later than f2
469*4882a593Smuzhiyun * @f1: the first fence from the same context
470*4882a593Smuzhiyun * @f2: the second fence from the same context
471*4882a593Smuzhiyun *
472*4882a593Smuzhiyun * Returns true if f1 is chronologically later than f2. Both fences must be
473*4882a593Smuzhiyun * from the same context, since a seqno is not re-used across contexts.
474*4882a593Smuzhiyun */
dma_fence_is_later(struct dma_fence * f1,struct dma_fence * f2)475*4882a593Smuzhiyun static inline bool dma_fence_is_later(struct dma_fence *f1,
476*4882a593Smuzhiyun struct dma_fence *f2)
477*4882a593Smuzhiyun {
478*4882a593Smuzhiyun if (WARN_ON(f1->context != f2->context))
479*4882a593Smuzhiyun return false;
480*4882a593Smuzhiyun
481*4882a593Smuzhiyun return __dma_fence_is_later(f1->seqno, f2->seqno, f1->ops);
482*4882a593Smuzhiyun }
483*4882a593Smuzhiyun
484*4882a593Smuzhiyun /**
485*4882a593Smuzhiyun * dma_fence_later - return the chronologically later fence
486*4882a593Smuzhiyun * @f1: the first fence from the same context
487*4882a593Smuzhiyun * @f2: the second fence from the same context
488*4882a593Smuzhiyun *
489*4882a593Smuzhiyun * Returns NULL if both fences are signaled, otherwise the fence that would be
490*4882a593Smuzhiyun * signaled last. Both fences must be from the same context, since a seqno is
491*4882a593Smuzhiyun * not re-used across contexts.
492*4882a593Smuzhiyun */
dma_fence_later(struct dma_fence * f1,struct dma_fence * f2)493*4882a593Smuzhiyun static inline struct dma_fence *dma_fence_later(struct dma_fence *f1,
494*4882a593Smuzhiyun struct dma_fence *f2)
495*4882a593Smuzhiyun {
496*4882a593Smuzhiyun if (WARN_ON(f1->context != f2->context))
497*4882a593Smuzhiyun return NULL;
498*4882a593Smuzhiyun
499*4882a593Smuzhiyun /*
500*4882a593Smuzhiyun * Can't check just DMA_FENCE_FLAG_SIGNALED_BIT here, it may never
501*4882a593Smuzhiyun * have been set if enable_signaling wasn't called, and enabling that
502*4882a593Smuzhiyun * here is overkill.
503*4882a593Smuzhiyun */
504*4882a593Smuzhiyun if (dma_fence_is_later(f1, f2))
505*4882a593Smuzhiyun return dma_fence_is_signaled(f1) ? NULL : f1;
506*4882a593Smuzhiyun else
507*4882a593Smuzhiyun return dma_fence_is_signaled(f2) ? NULL : f2;
508*4882a593Smuzhiyun }
509*4882a593Smuzhiyun
510*4882a593Smuzhiyun /**
511*4882a593Smuzhiyun * dma_fence_get_status_locked - returns the status upon completion
512*4882a593Smuzhiyun * @fence: the dma_fence to query
513*4882a593Smuzhiyun *
514*4882a593Smuzhiyun * Drivers can supply an optional error status condition before they signal
515*4882a593Smuzhiyun * the fence (to indicate whether the fence was completed due to an error
516*4882a593Smuzhiyun * rather than success). The value of the status condition is only valid
517*4882a593Smuzhiyun * if the fence has been signaled, dma_fence_get_status_locked() first checks
518*4882a593Smuzhiyun * the signal state before reporting the error status.
519*4882a593Smuzhiyun *
520*4882a593Smuzhiyun * Returns 0 if the fence has not yet been signaled, 1 if the fence has
521*4882a593Smuzhiyun * been signaled without an error condition, or a negative error code
522*4882a593Smuzhiyun * if the fence has been completed in err.
523*4882a593Smuzhiyun */
dma_fence_get_status_locked(struct dma_fence * fence)524*4882a593Smuzhiyun static inline int dma_fence_get_status_locked(struct dma_fence *fence)
525*4882a593Smuzhiyun {
526*4882a593Smuzhiyun if (dma_fence_is_signaled_locked(fence))
527*4882a593Smuzhiyun return fence->error ?: 1;
528*4882a593Smuzhiyun else
529*4882a593Smuzhiyun return 0;
530*4882a593Smuzhiyun }
531*4882a593Smuzhiyun
532*4882a593Smuzhiyun int dma_fence_get_status(struct dma_fence *fence);
533*4882a593Smuzhiyun
534*4882a593Smuzhiyun /**
535*4882a593Smuzhiyun * dma_fence_set_error - flag an error condition on the fence
536*4882a593Smuzhiyun * @fence: the dma_fence
537*4882a593Smuzhiyun * @error: the error to store
538*4882a593Smuzhiyun *
539*4882a593Smuzhiyun * Drivers can supply an optional error status condition before they signal
540*4882a593Smuzhiyun * the fence, to indicate that the fence was completed due to an error
541*4882a593Smuzhiyun * rather than success. This must be set before signaling (so that the value
542*4882a593Smuzhiyun * is visible before any waiters on the signal callback are woken). This
543*4882a593Smuzhiyun * helper exists to help catching erroneous setting of #dma_fence.error.
544*4882a593Smuzhiyun */
dma_fence_set_error(struct dma_fence * fence,int error)545*4882a593Smuzhiyun static inline void dma_fence_set_error(struct dma_fence *fence,
546*4882a593Smuzhiyun int error)
547*4882a593Smuzhiyun {
548*4882a593Smuzhiyun WARN_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags));
549*4882a593Smuzhiyun WARN_ON(error >= 0 || error < -MAX_ERRNO);
550*4882a593Smuzhiyun
551*4882a593Smuzhiyun fence->error = error;
552*4882a593Smuzhiyun }
553*4882a593Smuzhiyun
554*4882a593Smuzhiyun signed long dma_fence_wait_timeout(struct dma_fence *,
555*4882a593Smuzhiyun bool intr, signed long timeout);
556*4882a593Smuzhiyun signed long dma_fence_wait_any_timeout(struct dma_fence **fences,
557*4882a593Smuzhiyun uint32_t count,
558*4882a593Smuzhiyun bool intr, signed long timeout,
559*4882a593Smuzhiyun uint32_t *idx);
560*4882a593Smuzhiyun
561*4882a593Smuzhiyun /**
562*4882a593Smuzhiyun * dma_fence_wait - sleep until the fence gets signaled
563*4882a593Smuzhiyun * @fence: the fence to wait on
564*4882a593Smuzhiyun * @intr: if true, do an interruptible wait
565*4882a593Smuzhiyun *
566*4882a593Smuzhiyun * This function will return -ERESTARTSYS if interrupted by a signal,
567*4882a593Smuzhiyun * or 0 if the fence was signaled. Other error values may be
568*4882a593Smuzhiyun * returned on custom implementations.
569*4882a593Smuzhiyun *
570*4882a593Smuzhiyun * Performs a synchronous wait on this fence. It is assumed the caller
571*4882a593Smuzhiyun * directly or indirectly holds a reference to the fence, otherwise the
572*4882a593Smuzhiyun * fence might be freed before return, resulting in undefined behavior.
573*4882a593Smuzhiyun *
574*4882a593Smuzhiyun * See also dma_fence_wait_timeout() and dma_fence_wait_any_timeout().
575*4882a593Smuzhiyun */
dma_fence_wait(struct dma_fence * fence,bool intr)576*4882a593Smuzhiyun static inline signed long dma_fence_wait(struct dma_fence *fence, bool intr)
577*4882a593Smuzhiyun {
578*4882a593Smuzhiyun signed long ret;
579*4882a593Smuzhiyun
580*4882a593Smuzhiyun /* Since dma_fence_wait_timeout cannot timeout with
581*4882a593Smuzhiyun * MAX_SCHEDULE_TIMEOUT, only valid return values are
582*4882a593Smuzhiyun * -ERESTARTSYS and MAX_SCHEDULE_TIMEOUT.
583*4882a593Smuzhiyun */
584*4882a593Smuzhiyun ret = dma_fence_wait_timeout(fence, intr, MAX_SCHEDULE_TIMEOUT);
585*4882a593Smuzhiyun
586*4882a593Smuzhiyun return ret < 0 ? ret : 0;
587*4882a593Smuzhiyun }
588*4882a593Smuzhiyun
589*4882a593Smuzhiyun struct dma_fence *dma_fence_get_stub(void);
590*4882a593Smuzhiyun u64 dma_fence_context_alloc(unsigned num);
591*4882a593Smuzhiyun
592*4882a593Smuzhiyun #define DMA_FENCE_TRACE(f, fmt, args...) \
593*4882a593Smuzhiyun do { \
594*4882a593Smuzhiyun struct dma_fence *__ff = (f); \
595*4882a593Smuzhiyun if (IS_ENABLED(CONFIG_DMA_FENCE_TRACE)) \
596*4882a593Smuzhiyun pr_info("f %llu#%llu: " fmt, \
597*4882a593Smuzhiyun __ff->context, __ff->seqno, ##args); \
598*4882a593Smuzhiyun } while (0)
599*4882a593Smuzhiyun
600*4882a593Smuzhiyun #define DMA_FENCE_WARN(f, fmt, args...) \
601*4882a593Smuzhiyun do { \
602*4882a593Smuzhiyun struct dma_fence *__ff = (f); \
603*4882a593Smuzhiyun pr_warn("f %llu#%llu: " fmt, __ff->context, __ff->seqno,\
604*4882a593Smuzhiyun ##args); \
605*4882a593Smuzhiyun } while (0)
606*4882a593Smuzhiyun
607*4882a593Smuzhiyun #define DMA_FENCE_ERR(f, fmt, args...) \
608*4882a593Smuzhiyun do { \
609*4882a593Smuzhiyun struct dma_fence *__ff = (f); \
610*4882a593Smuzhiyun pr_err("f %llu#%llu: " fmt, __ff->context, __ff->seqno, \
611*4882a593Smuzhiyun ##args); \
612*4882a593Smuzhiyun } while (0)
613*4882a593Smuzhiyun
614*4882a593Smuzhiyun #endif /* __LINUX_DMA_FENCE_H */
615