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