Blame view

include/linux/dma-fence.h 17.3 KB
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  /*
   * Fence mechanism for dma-buf to allow for asynchronous dma access
   *
   * Copyright (C) 2012 Canonical Ltd
   * Copyright (C) 2012 Texas Instruments
   *
   * Authors:
   * Rob Clark <robdclark@gmail.com>
   * Maarten Lankhorst <maarten.lankhorst@canonical.com>
   *
   * This program is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 as published by
   * the Free Software Foundation.
   *
   * This program is distributed in the hope that it will be useful, but WITHOUT
   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   * more details.
   */
f54d18670   Chris Wilson   dma-buf: Rename s...
20
21
  #ifndef __LINUX_DMA_FENCE_H
  #define __LINUX_DMA_FENCE_H
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
22
23
24
25
26
27
28
29
  
  #include <linux/err.h>
  #include <linux/wait.h>
  #include <linux/list.h>
  #include <linux/bitops.h>
  #include <linux/kref.h>
  #include <linux/sched.h>
  #include <linux/printk.h>
3c3b177a9   Maarten Lankhorst   reservation: add ...
30
  #include <linux/rcupdate.h>
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
31

f54d18670   Chris Wilson   dma-buf: Rename s...
32
33
34
  struct dma_fence;
  struct dma_fence_ops;
  struct dma_fence_cb;
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
35
36
  
  /**
f54d18670   Chris Wilson   dma-buf: Rename s...
37
   * struct dma_fence - software synchronization primitive
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
38
   * @refcount: refcount for this fence
f54d18670   Chris Wilson   dma-buf: Rename s...
39
   * @ops: dma_fence_ops associated with this fence
3c3b177a9   Maarten Lankhorst   reservation: add ...
40
   * @rcu: used for releasing fence with kfree_rcu
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
41
42
43
   * @cb_list: list of all callbacks to call
   * @lock: spin_lock_irqsave used for locking
   * @context: execution context this fence belongs to, returned by
f54d18670   Chris Wilson   dma-buf: Rename s...
44
   *           dma_fence_context_alloc()
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
45
46
   * @seqno: the sequence number of this fence inside the execution context,
   * can be compared to decide which fence would be signaled later.
f54d18670   Chris Wilson   dma-buf: Rename s...
47
   * @flags: A mask of DMA_FENCE_FLAG_* defined below
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
48
   * @timestamp: Timestamp when the fence was signaled.
a009e975d   Chris Wilson   dma-fence: Introd...
49
   * @error: Optional, only valid if < 0, must be set before calling
f54d18670   Chris Wilson   dma-buf: Rename s...
50
   * dma_fence_signal, indicates that the fence has completed with an error.
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
51
52
53
54
55
   *
   * the flags member must be manipulated and read using the appropriate
   * atomic ops (bit_*), so taking the spinlock will not be needed most
   * of the time.
   *
f54d18670   Chris Wilson   dma-buf: Rename s...
56
   * DMA_FENCE_FLAG_SIGNALED_BIT - fence is already signaled
76250f2b7   Chris Wilson   dma-buf/fence: Av...
57
   * DMA_FENCE_FLAG_TIMESTAMP_BIT - timestamp recorded for fence signaling
f54d18670   Chris Wilson   dma-buf: Rename s...
58
59
   * DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT - enable_signaling might have been called
   * DMA_FENCE_FLAG_USER_BITS - start of the unused bits, can be used by the
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
60
61
62
   * implementer of the fence for its own purposes. Can be used in different
   * ways by different fence implementers, so do not rely on this.
   *
3590d50e2   Sumit Semwal   dma-buf/fence: ke...
63
   * Since atomic bitops are used, this is not guaranteed to be the case.
f54d18670   Chris Wilson   dma-buf: Rename s...
64
   * Particularly, if the bit was set, but dma_fence_signal was called right
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
65
   * before this bit was set, it would have been able to set the
f54d18670   Chris Wilson   dma-buf: Rename s...
66
67
68
69
   * DMA_FENCE_FLAG_SIGNALED_BIT, before enable_signaling was called.
   * Adding a check for DMA_FENCE_FLAG_SIGNALED_BIT after setting
   * DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT closes this race, and makes sure that
   * after dma_fence_signal was called, any enable_signaling call will have either
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
70
71
   * been completed, or never called at all.
   */
f54d18670   Chris Wilson   dma-buf: Rename s...
72
  struct dma_fence {
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
73
  	struct kref refcount;
f54d18670   Chris Wilson   dma-buf: Rename s...
74
  	const struct dma_fence_ops *ops;
3c3b177a9   Maarten Lankhorst   reservation: add ...
75
  	struct rcu_head rcu;
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
76
77
  	struct list_head cb_list;
  	spinlock_t *lock;
76bf0db55   Christian König   dma-buf/fence: ma...
78
79
  	u64 context;
  	unsigned seqno;
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
80
81
  	unsigned long flags;
  	ktime_t timestamp;
a009e975d   Chris Wilson   dma-fence: Introd...
82
  	int error;
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
83
  };
f54d18670   Chris Wilson   dma-buf: Rename s...
84
85
  enum dma_fence_flag_bits {
  	DMA_FENCE_FLAG_SIGNALED_BIT,
76250f2b7   Chris Wilson   dma-buf/fence: Av...
86
  	DMA_FENCE_FLAG_TIMESTAMP_BIT,
f54d18670   Chris Wilson   dma-buf: Rename s...
87
88
  	DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
  	DMA_FENCE_FLAG_USER_BITS, /* must always be last member */
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
89
  };
f54d18670   Chris Wilson   dma-buf: Rename s...
90
91
  typedef void (*dma_fence_func_t)(struct dma_fence *fence,
  				 struct dma_fence_cb *cb);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
92
93
  
  /**
f54d18670   Chris Wilson   dma-buf: Rename s...
94
95
96
   * struct dma_fence_cb - callback for dma_fence_add_callback
   * @node: used by dma_fence_add_callback to append this struct to fence::cb_list
   * @func: dma_fence_func_t to call
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
97
   *
f54d18670   Chris Wilson   dma-buf: Rename s...
98
99
   * This struct will be initialized by dma_fence_add_callback, additional
   * data can be passed along by embedding dma_fence_cb in another struct.
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
100
   */
f54d18670   Chris Wilson   dma-buf: Rename s...
101
  struct dma_fence_cb {
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
102
  	struct list_head node;
f54d18670   Chris Wilson   dma-buf: Rename s...
103
  	dma_fence_func_t func;
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
104
105
106
  };
  
  /**
f54d18670   Chris Wilson   dma-buf: Rename s...
107
   * struct dma_fence_ops - operations implemented for fence
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
108
109
110
111
   * @get_driver_name: returns the driver name.
   * @get_timeline_name: return the name of the context this fence belongs to.
   * @enable_signaling: enable software signaling of fence.
   * @signaled: [optional] peek whether the fence is signaled, can be null.
f54d18670   Chris Wilson   dma-buf: Rename s...
112
   * @wait: custom wait implementation, or dma_fence_default_wait.
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
   * @release: [optional] called on destruction of fence, can be null
   * @fill_driver_data: [optional] callback to fill in free-form debug info
   * Returns amount of bytes filled, or -errno.
   * @fence_value_str: [optional] fills in the value of the fence as a string
   * @timeline_value_str: [optional] fills in the current value of the timeline
   * as a string
   *
   * Notes on enable_signaling:
   * For fence implementations that have the capability for hw->hw
   * signaling, they can implement this op to enable the necessary
   * irqs, or insert commands into cmdstream, etc.  This is called
   * in the first wait() or add_callback() path to let the fence
   * implementation know that there is another driver waiting on
   * the signal (ie. hw->sw case).
   *
   * This function can be called called from atomic context, but not
   * from irq context, so normal spinlocks can be used.
   *
   * A return value of false indicates the fence already passed,
f353d71f7   Masanari Iida   treewide: Fix typ...
132
133
   * or some failure occurred that made it impossible to enable
   * signaling. True indicates successful enabling.
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
134
   *
a009e975d   Chris Wilson   dma-fence: Introd...
135
   * fence->error may be set in enable_signaling, but only when false is
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
136
137
   * returned.
   *
f54d18670   Chris Wilson   dma-buf: Rename s...
138
   * Calling dma_fence_signal before enable_signaling is called allows
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
139
   * for a tiny race window in which enable_signaling is called during,
f54d18670   Chris Wilson   dma-buf: Rename s...
140
   * before, or after dma_fence_signal. To fight this, it is recommended
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
141
142
   * that before enable_signaling returns true an extra reference is
   * taken on the fence, to be released when the fence is signaled.
f54d18670   Chris Wilson   dma-buf: Rename s...
143
   * This will mean dma_fence_signal will still be called twice, but
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
144
145
146
   * the second time will be a noop since it was already signaled.
   *
   * Notes on signaled:
a009e975d   Chris Wilson   dma-fence: Introd...
147
   * May set fence->error if returning true.
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
148
149
   *
   * Notes on wait:
f54d18670   Chris Wilson   dma-buf: Rename s...
150
151
   * Must not be NULL, set to dma_fence_default_wait for default implementation.
   * the dma_fence_default_wait implementation should work for any fence, as long
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
152
153
154
155
156
157
158
159
160
161
162
163
164
   * as enable_signaling works correctly.
   *
   * Must return -ERESTARTSYS if the wait is intr = true and the wait was
   * interrupted, and remaining jiffies if fence has signaled, or 0 if wait
   * timed out. Can also return other error values on custom implementations,
   * which should be treated as if the fence is signaled. For example a hardware
   * lockup could be reported like that.
   *
   * Notes on release:
   * Can be NULL, this function allows additional commands to run on
   * destruction of the fence. Can be called from irq context.
   * If pointer is set to NULL, kfree will get called instead.
   */
f54d18670   Chris Wilson   dma-buf: Rename s...
165
166
167
168
169
170
171
172
173
174
175
176
177
  struct dma_fence_ops {
  	const char * (*get_driver_name)(struct dma_fence *fence);
  	const char * (*get_timeline_name)(struct dma_fence *fence);
  	bool (*enable_signaling)(struct dma_fence *fence);
  	bool (*signaled)(struct dma_fence *fence);
  	signed long (*wait)(struct dma_fence *fence,
  			    bool intr, signed long timeout);
  	void (*release)(struct dma_fence *fence);
  
  	int (*fill_driver_data)(struct dma_fence *fence, void *data, int size);
  	void (*fence_value_str)(struct dma_fence *fence, char *str, int size);
  	void (*timeline_value_str)(struct dma_fence *fence,
  				   char *str, int size);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
178
  };
f54d18670   Chris Wilson   dma-buf: Rename s...
179
180
  void dma_fence_init(struct dma_fence *fence, const struct dma_fence_ops *ops,
  		    spinlock_t *lock, u64 context, unsigned seqno);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
181

f54d18670   Chris Wilson   dma-buf: Rename s...
182
183
  void dma_fence_release(struct kref *kref);
  void dma_fence_free(struct dma_fence *fence);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
184
185
  
  /**
f54d18670   Chris Wilson   dma-buf: Rename s...
186
   * dma_fence_put - decreases refcount of the fence
4be054207   Chris Wilson   dma-buf: Introduc...
187
188
   * @fence:	[in]	fence to reduce refcount of
   */
f54d18670   Chris Wilson   dma-buf: Rename s...
189
  static inline void dma_fence_put(struct dma_fence *fence)
4be054207   Chris Wilson   dma-buf: Introduc...
190
191
  {
  	if (fence)
f54d18670   Chris Wilson   dma-buf: Rename s...
192
  		kref_put(&fence->refcount, dma_fence_release);
4be054207   Chris Wilson   dma-buf: Introduc...
193
194
195
  }
  
  /**
f54d18670   Chris Wilson   dma-buf: Rename s...
196
   * dma_fence_get - increases refcount of the fence
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
197
198
199
200
   * @fence:	[in]	fence to increase refcount of
   *
   * Returns the same fence, with refcount increased by 1.
   */
f54d18670   Chris Wilson   dma-buf: Rename s...
201
  static inline struct dma_fence *dma_fence_get(struct dma_fence *fence)
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
202
203
204
205
206
207
208
  {
  	if (fence)
  		kref_get(&fence->refcount);
  	return fence;
  }
  
  /**
f54d18670   Chris Wilson   dma-buf: Rename s...
209
210
   * dma_fence_get_rcu - get a fence from a reservation_object_list with
   *                     rcu read lock
3c3b177a9   Maarten Lankhorst   reservation: add ...
211
212
213
214
   * @fence:	[in]	fence to increase refcount of
   *
   * Function returns NULL if no refcount could be obtained, or the fence.
   */
f54d18670   Chris Wilson   dma-buf: Rename s...
215
  static inline struct dma_fence *dma_fence_get_rcu(struct dma_fence *fence)
3c3b177a9   Maarten Lankhorst   reservation: add ...
216
217
218
219
220
221
222
223
  {
  	if (kref_get_unless_zero(&fence->refcount))
  		return fence;
  	else
  		return NULL;
  }
  
  /**
f54d18670   Chris Wilson   dma-buf: Rename s...
224
   * dma_fence_get_rcu_safe  - acquire a reference to an RCU tracked fence
8a5846bf5   Daniel Vetter   doc/dma-buf: Fix ...
225
   * @fencep:	[in]	pointer to fence to increase refcount of
4be054207   Chris Wilson   dma-buf: Introduc...
226
227
228
   *
   * Function returns NULL if no refcount could be obtained, or the fence.
   * This function handles acquiring a reference to a fence that may be
5f0d5a3ae   Paul E. McKenney   mm: Rename SLAB_D...
229
   * reallocated within the RCU grace period (such as with SLAB_TYPESAFE_BY_RCU),
4be054207   Chris Wilson   dma-buf: Introduc...
230
231
232
233
234
235
236
237
   * so long as the caller is using RCU on the pointer to the fence.
   *
   * An alternative mechanism is to employ a seqlock to protect a bunch of
   * fences, such as used by struct reservation_object. When using a seqlock,
   * the seqlock must be taken before and checked after a reference to the
   * fence is acquired (as shown here).
   *
   * The caller is required to hold the RCU read lock.
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
238
   */
f54d18670   Chris Wilson   dma-buf: Rename s...
239
240
  static inline struct dma_fence *
  dma_fence_get_rcu_safe(struct dma_fence * __rcu *fencep)
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
241
  {
4be054207   Chris Wilson   dma-buf: Introduc...
242
  	do {
f54d18670   Chris Wilson   dma-buf: Rename s...
243
  		struct dma_fence *fence;
4be054207   Chris Wilson   dma-buf: Introduc...
244
245
  
  		fence = rcu_dereference(*fencep);
f54d18670   Chris Wilson   dma-buf: Rename s...
246
  		if (!fence || !dma_fence_get_rcu(fence))
4be054207   Chris Wilson   dma-buf: Introduc...
247
  			return NULL;
f54d18670   Chris Wilson   dma-buf: Rename s...
248
  		/* The atomic_inc_not_zero() inside dma_fence_get_rcu()
4be054207   Chris Wilson   dma-buf: Introduc...
249
250
251
252
253
254
255
  		 * provides a full memory barrier upon success (such as now).
  		 * This is paired with the write barrier from assigning
  		 * to the __rcu protected fence pointer so that if that
  		 * pointer still matches the current fence, we know we
  		 * have successfully acquire a reference to it. If it no
  		 * longer matches, we are holding a reference to some other
  		 * reallocated pointer. This is possible if the allocator
5f0d5a3ae   Paul E. McKenney   mm: Rename SLAB_D...
256
  		 * is using a freelist like SLAB_TYPESAFE_BY_RCU where the
4be054207   Chris Wilson   dma-buf: Introduc...
257
258
259
260
261
262
263
  		 * fence remains valid for the RCU grace period, but it
  		 * may be reallocated. When using such allocators, we are
  		 * responsible for ensuring the reference we get is to
  		 * the right fence, as below.
  		 */
  		if (fence == rcu_access_pointer(*fencep))
  			return rcu_pointer_handoff(fence);
f54d18670   Chris Wilson   dma-buf: Rename s...
264
  		dma_fence_put(fence);
4be054207   Chris Wilson   dma-buf: Introduc...
265
  	} while (1);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
266
  }
f54d18670   Chris Wilson   dma-buf: Rename s...
267
268
269
270
271
272
273
274
275
276
  int dma_fence_signal(struct dma_fence *fence);
  int dma_fence_signal_locked(struct dma_fence *fence);
  signed long dma_fence_default_wait(struct dma_fence *fence,
  				   bool intr, signed long timeout);
  int dma_fence_add_callback(struct dma_fence *fence,
  			   struct dma_fence_cb *cb,
  			   dma_fence_func_t func);
  bool dma_fence_remove_callback(struct dma_fence *fence,
  			       struct dma_fence_cb *cb);
  void dma_fence_enable_sw_signaling(struct dma_fence *fence);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
277
278
  
  /**
f54d18670   Chris Wilson   dma-buf: Rename s...
279
280
   * dma_fence_is_signaled_locked - Return an indication if the fence
   *                                is signaled yet.
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
281
282
283
284
   * @fence:	[in]	the fence to check
   *
   * Returns true if the fence was already signaled, false if not. Since this
   * function doesn't enable signaling, it is not guaranteed to ever return
f54d18670   Chris Wilson   dma-buf: Rename s...
285
286
   * true if dma_fence_add_callback, dma_fence_wait or
   * dma_fence_enable_sw_signaling haven't been called before.
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
287
288
289
290
   *
   * This function requires fence->lock to be held.
   */
  static inline bool
f54d18670   Chris Wilson   dma-buf: Rename s...
291
  dma_fence_is_signaled_locked(struct dma_fence *fence)
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
292
  {
f54d18670   Chris Wilson   dma-buf: Rename s...
293
  	if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
294
295
296
  		return true;
  
  	if (fence->ops->signaled && fence->ops->signaled(fence)) {
f54d18670   Chris Wilson   dma-buf: Rename s...
297
  		dma_fence_signal_locked(fence);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
298
299
300
301
302
303
304
  		return true;
  	}
  
  	return false;
  }
  
  /**
f54d18670   Chris Wilson   dma-buf: Rename s...
305
   * dma_fence_is_signaled - Return an indication if the fence is signaled yet.
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
306
307
308
309
   * @fence:	[in]	the fence to check
   *
   * Returns true if the fence was already signaled, false if not. Since this
   * function doesn't enable signaling, it is not guaranteed to ever return
f54d18670   Chris Wilson   dma-buf: Rename s...
310
311
   * true if dma_fence_add_callback, dma_fence_wait or
   * dma_fence_enable_sw_signaling haven't been called before.
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
312
   *
f54d18670   Chris Wilson   dma-buf: Rename s...
313
   * It's recommended for seqno fences to call dma_fence_signal when the
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
314
315
316
317
318
   * operation is complete, it makes it possible to prevent issues from
   * wraparound between time of issue and time of use by checking the return
   * value of this function before calling hardware-specific wait instructions.
   */
  static inline bool
f54d18670   Chris Wilson   dma-buf: Rename s...
319
  dma_fence_is_signaled(struct dma_fence *fence)
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
320
  {
f54d18670   Chris Wilson   dma-buf: Rename s...
321
  	if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
322
323
324
  		return true;
  
  	if (fence->ops->signaled && fence->ops->signaled(fence)) {
f54d18670   Chris Wilson   dma-buf: Rename s...
325
  		dma_fence_signal(fence);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
326
327
328
329
330
331
332
  		return true;
  	}
  
  	return false;
  }
  
  /**
811147766   Chris Wilson   dma-buf/dma-fence...
333
334
335
336
337
338
339
340
341
342
343
344
345
   * __dma_fence_is_later - return if f1 is chronologically later than f2
   * @f1:	[in]	the first fence's seqno
   * @f2:	[in]	the second fence's seqno from the same context
   *
   * Returns true if f1 is chronologically later than f2. Both fences must be
   * from the same context, since a seqno is not common across contexts.
   */
  static inline bool __dma_fence_is_later(u32 f1, u32 f2)
  {
  	return (int)(f1 - f2) > 0;
  }
  
  /**
f54d18670   Chris Wilson   dma-buf: Rename s...
346
   * dma_fence_is_later - return if f1 is chronologically later than f2
6c455ac17   Christian König   dma-buf/fence: ad...
347
348
349
350
351
352
   * @f1:	[in]	the first fence from the same context
   * @f2:	[in]	the second fence from the same context
   *
   * Returns true if f1 is chronologically later than f2. Both fences must be
   * from the same context, since a seqno is not re-used across contexts.
   */
f54d18670   Chris Wilson   dma-buf: Rename s...
353
354
  static inline bool dma_fence_is_later(struct dma_fence *f1,
  				      struct dma_fence *f2)
6c455ac17   Christian König   dma-buf/fence: ad...
355
356
357
  {
  	if (WARN_ON(f1->context != f2->context))
  		return false;
811147766   Chris Wilson   dma-buf/dma-fence...
358
  	return __dma_fence_is_later(f1->seqno, f2->seqno);
6c455ac17   Christian König   dma-buf/fence: ad...
359
360
361
  }
  
  /**
f54d18670   Chris Wilson   dma-buf: Rename s...
362
   * dma_fence_later - return the chronologically later fence
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
363
364
365
366
367
368
369
   * @f1:	[in]	the first fence from the same context
   * @f2:	[in]	the second fence from the same context
   *
   * Returns NULL if both fences are signaled, otherwise the fence that would be
   * signaled last. Both fences must be from the same context, since a seqno is
   * not re-used across contexts.
   */
f54d18670   Chris Wilson   dma-buf: Rename s...
370
371
  static inline struct dma_fence *dma_fence_later(struct dma_fence *f1,
  						struct dma_fence *f2)
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
372
373
374
375
376
  {
  	if (WARN_ON(f1->context != f2->context))
  		return NULL;
  
  	/*
f54d18670   Chris Wilson   dma-buf: Rename s...
377
378
379
  	 * Can't check just DMA_FENCE_FLAG_SIGNALED_BIT here, it may never
  	 * have been set if enable_signaling wasn't called, and enabling that
  	 * here is overkill.
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
380
  	 */
f54d18670   Chris Wilson   dma-buf: Rename s...
381
382
  	if (dma_fence_is_later(f1, f2))
  		return dma_fence_is_signaled(f1) ? NULL : f1;
6c455ac17   Christian König   dma-buf/fence: ad...
383
  	else
f54d18670   Chris Wilson   dma-buf: Rename s...
384
  		return dma_fence_is_signaled(f2) ? NULL : f2;
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
385
  }
d6c99f4bf   Chris Wilson   dma-fence: Wrap q...
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
  /**
   * dma_fence_get_status_locked - returns the status upon completion
   * @fence: [in]	the dma_fence to query
   *
   * Drivers can supply an optional error status condition before they signal
   * the fence (to indicate whether the fence was completed due to an error
   * rather than success). The value of the status condition is only valid
   * if the fence has been signaled, dma_fence_get_status_locked() first checks
   * the signal state before reporting the error status.
   *
   * Returns 0 if the fence has not yet been signaled, 1 if the fence has
   * been signaled without an error condition, or a negative error code
   * if the fence has been completed in err.
   */
  static inline int dma_fence_get_status_locked(struct dma_fence *fence)
  {
  	if (dma_fence_is_signaled_locked(fence))
a009e975d   Chris Wilson   dma-fence: Introd...
403
  		return fence->error ?: 1;
d6c99f4bf   Chris Wilson   dma-fence: Wrap q...
404
405
406
407
408
  	else
  		return 0;
  }
  
  int dma_fence_get_status(struct dma_fence *fence);
a009e975d   Chris Wilson   dma-fence: Introd...
409
410
411
412
413
414
415
416
417
418
419
420
421
422
  /**
   * dma_fence_set_error - flag an error condition on the fence
   * @fence: [in]	the dma_fence
   * @error: [in]	the error to store
   *
   * Drivers can supply an optional error status condition before they signal
   * the fence, to indicate that the fence was completed due to an error
   * rather than success. This must be set before signaling (so that the value
   * is visible before any waiters on the signal callback are woken). This
   * helper exists to help catching erroneous setting of #dma_fence.error.
   */
  static inline void dma_fence_set_error(struct dma_fence *fence,
  				       int error)
  {
6ce31263c   Daniel Vetter   dma-fence: Don't ...
423
424
  	WARN_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags));
  	WARN_ON(error >= 0 || error < -MAX_ERRNO);
a009e975d   Chris Wilson   dma-fence: Introd...
425
426
427
  
  	fence->error = error;
  }
f54d18670   Chris Wilson   dma-buf: Rename s...
428
  signed long dma_fence_wait_timeout(struct dma_fence *,
a519435a9   Christian König   dma-buf/fence: ad...
429
  				   bool intr, signed long timeout);
f54d18670   Chris Wilson   dma-buf: Rename s...
430
431
  signed long dma_fence_wait_any_timeout(struct dma_fence **fences,
  				       uint32_t count,
7392b4bb7   monk.liu   dma-buf: return i...
432
433
  				       bool intr, signed long timeout,
  				       uint32_t *idx);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
434
435
  
  /**
f54d18670   Chris Wilson   dma-buf: Rename s...
436
   * dma_fence_wait - sleep until the fence gets signaled
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
437
438
439
440
441
442
443
444
445
446
447
   * @fence:	[in]	the fence to wait on
   * @intr:	[in]	if true, do an interruptible wait
   *
   * This function will return -ERESTARTSYS if interrupted by a signal,
   * or 0 if the fence was signaled. Other error values may be
   * returned on custom implementations.
   *
   * Performs a synchronous wait on this fence. It is assumed the caller
   * directly or indirectly holds a reference to the fence, otherwise the
   * fence might be freed before return, resulting in undefined behavior.
   */
f54d18670   Chris Wilson   dma-buf: Rename s...
448
  static inline signed long dma_fence_wait(struct dma_fence *fence, bool intr)
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
449
450
  {
  	signed long ret;
f54d18670   Chris Wilson   dma-buf: Rename s...
451
  	/* Since dma_fence_wait_timeout cannot timeout with
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
452
453
454
  	 * MAX_SCHEDULE_TIMEOUT, only valid return values are
  	 * -ERESTARTSYS and MAX_SCHEDULE_TIMEOUT.
  	 */
f54d18670   Chris Wilson   dma-buf: Rename s...
455
  	ret = dma_fence_wait_timeout(fence, intr, MAX_SCHEDULE_TIMEOUT);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
456
457
458
  
  	return ret < 0 ? ret : 0;
  }
f54d18670   Chris Wilson   dma-buf: Rename s...
459
  u64 dma_fence_context_alloc(unsigned num);
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
460

f54d18670   Chris Wilson   dma-buf: Rename s...
461
  #define DMA_FENCE_TRACE(f, fmt, args...) \
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
462
  	do {								\
f54d18670   Chris Wilson   dma-buf: Rename s...
463
464
  		struct dma_fence *__ff = (f);				\
  		if (IS_ENABLED(CONFIG_DMA_FENCE_TRACE))			\
76bf0db55   Christian König   dma-buf/fence: ma...
465
  			pr_info("f %llu#%u: " fmt,			\
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
466
467
  				__ff->context, __ff->seqno, ##args);	\
  	} while (0)
f54d18670   Chris Wilson   dma-buf: Rename s...
468
  #define DMA_FENCE_WARN(f, fmt, args...) \
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
469
  	do {								\
f54d18670   Chris Wilson   dma-buf: Rename s...
470
  		struct dma_fence *__ff = (f);				\
76bf0db55   Christian König   dma-buf/fence: ma...
471
  		pr_warn("f %llu#%u: " fmt, __ff->context, __ff->seqno,	\
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
472
473
  			 ##args);					\
  	} while (0)
f54d18670   Chris Wilson   dma-buf: Rename s...
474
  #define DMA_FENCE_ERR(f, fmt, args...) \
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
475
  	do {								\
f54d18670   Chris Wilson   dma-buf: Rename s...
476
  		struct dma_fence *__ff = (f);				\
76bf0db55   Christian König   dma-buf/fence: ma...
477
  		pr_err("f %llu#%u: " fmt, __ff->context, __ff->seqno,	\
e941759c7   Maarten Lankhorst   fence: dma-buf cr...
478
479
  			##args);					\
  	} while (0)
f54d18670   Chris Wilson   dma-buf: Rename s...
480
  #endif /* __LINUX_DMA_FENCE_H */