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