1 /* SPDX-License-Identifier: GPL-2.0-only */ 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> 11 */ 12 13 #ifndef __LINUX_DMA_FENCE_H 14 #define __LINUX_DMA_FENCE_H 15 16 #include <linux/err.h> 17 #include <linux/wait.h> 18 #include <linux/list.h> 19 #include <linux/bitops.h> 20 #include <linux/kref.h> 21 #include <linux/sched.h> 22 #include <linux/printk.h> 23 #include <linux/rcupdate.h> 24 #include <linux/timekeeping.h> 25 26 struct dma_fence; 27 struct dma_fence_ops; 28 struct dma_fence_cb; 29 struct seq_file; 30 31 /** 32 * struct dma_fence - software synchronization primitive 33 * @refcount: refcount for this fence 34 * @ops: dma_fence_ops associated with this fence 35 * @rcu: used for releasing fence with kfree_rcu 36 * @cb_list: list of all callbacks to call 37 * @lock: spin_lock_irqsave used for locking 38 * @context: execution context this fence belongs to, returned by 39 * dma_fence_context_alloc() 40 * @seqno: the sequence number of this fence inside the execution context, 41 * can be compared to decide which fence would be signaled later. 42 * @flags: A mask of DMA_FENCE_FLAG_* defined below 43 * @timestamp: Timestamp when the fence was signaled. 44 * @error: Optional, only valid if < 0, must be set before calling 45 * dma_fence_signal, indicates that the fence has completed with an error. 46 * 47 * the flags member must be manipulated and read using the appropriate 48 * atomic ops (bit_*), so taking the spinlock will not be needed most 49 * of the time. 50 * 51 * DMA_FENCE_FLAG_SIGNALED_BIT - fence is already signaled 52 * DMA_FENCE_FLAG_TIMESTAMP_BIT - timestamp recorded for fence signaling 53 * DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT - enable_signaling might have been called 54 * DMA_FENCE_FLAG_USER_BITS - start of the unused bits, can be used by the 55 * implementer of the fence for its own purposes. Can be used in different 56 * ways by different fence implementers, so do not rely on this. 57 * 58 * Since atomic bitops are used, this is not guaranteed to be the case. 59 * Particularly, if the bit was set, but dma_fence_signal was called right 60 * before this bit was set, it would have been able to set the 61 * DMA_FENCE_FLAG_SIGNALED_BIT, before enable_signaling was called. 62 * Adding a check for DMA_FENCE_FLAG_SIGNALED_BIT after setting 63 * DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT closes this race, and makes sure that 64 * after dma_fence_signal was called, any enable_signaling call will have either 65 * been completed, or never called at all. 66 */ 67 struct dma_fence { 68 spinlock_t *lock; 69 const struct dma_fence_ops *ops; 70 /* 71 * We clear the callback list on kref_put so that by the time we 72 * release the fence it is unused. No one should be adding to the 73 * cb_list that they don't themselves hold a reference for. 74 * 75 * The lifetime of the timestamp is similarly tied to both the 76 * rcu freelist and the cb_list. The timestamp is only set upon 77 * signaling while simultaneously notifying the cb_list. Ergo, we 78 * only use either the cb_list of timestamp. Upon destruction, 79 * neither are accessible, and so we can use the rcu. This means 80 * that the cb_list is *only* valid until the signal bit is set, 81 * and to read either you *must* hold a reference to the fence, 82 * and not just the rcu_read_lock. 83 * 84 * Listed in chronological order. 85 */ 86 union { 87 struct list_head cb_list; 88 /* @cb_list replaced by @timestamp on dma_fence_signal() */ 89 ktime_t timestamp; 90 /* @timestamp replaced by @rcu on dma_fence_release() */ 91 struct rcu_head rcu; 92 }; 93 u64 context; 94 u64 seqno; 95 unsigned long flags; 96 struct kref refcount; 97 int error; 98 }; 99 100 enum dma_fence_flag_bits { 101 DMA_FENCE_FLAG_SEQNO64_BIT, 102 DMA_FENCE_FLAG_SIGNALED_BIT, 103 DMA_FENCE_FLAG_TIMESTAMP_BIT, 104 DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, 105 DMA_FENCE_FLAG_USER_BITS, /* must always be last member */ 106 }; 107 108 typedef void (*dma_fence_func_t)(struct dma_fence *fence, 109 struct dma_fence_cb *cb); 110 111 /** 112 * struct dma_fence_cb - callback for dma_fence_add_callback() 113 * @node: used by dma_fence_add_callback() to append this struct to fence::cb_list 114 * @func: dma_fence_func_t to call 115 * 116 * This struct will be initialized by dma_fence_add_callback(), additional 117 * data can be passed along by embedding dma_fence_cb in another struct. 118 */ 119 struct dma_fence_cb { 120 struct list_head node; 121 dma_fence_func_t func; 122 }; 123 124 /** 125 * struct dma_fence_ops - operations implemented for fence 126 * 127 */ 128 struct dma_fence_ops { 129 /** 130 * @get_driver_name: 131 * 132 * Returns the driver name. This is a callback to allow drivers to 133 * compute the name at runtime, without having it to store permanently 134 * for each fence, or build a cache of some sort. 135 * 136 * This callback is mandatory. 137 */ 138 const char * (*get_driver_name)(struct dma_fence *fence); 139 140 /** 141 * @get_timeline_name: 142 * 143 * Return the name of the context this fence belongs to. This is a 144 * callback to allow drivers to compute the name at runtime, without 145 * having it to store permanently for each fence, or build a cache of 146 * some sort. 147 * 148 * This callback is mandatory. 149 */ 150 const char * (*get_timeline_name)(struct dma_fence *fence); 151 152 /** 153 * @enable_signaling: 154 * 155 * Enable software signaling of fence. 156 * 157 * For fence implementations that have the capability for hw->hw 158 * signaling, they can implement this op to enable the necessary 159 * interrupts, or insert commands into cmdstream, etc, to avoid these 160 * costly operations for the common case where only hw->hw 161 * synchronization is required. This is called in the first 162 * dma_fence_wait() or dma_fence_add_callback() path to let the fence 163 * implementation know that there is another driver waiting on the 164 * signal (ie. hw->sw case). 165 * 166 * This is called with irq's disabled, so only spinlocks which disable 167 * IRQ's can be used in the code outside of this callback. 168 * 169 * A return value of false indicates the fence already passed, 170 * or some failure occurred that made it impossible to enable 171 * signaling. True indicates successful enabling. 172 * 173 * &dma_fence.error may be set in enable_signaling, but only when false 174 * is returned. 175 * 176 * Since many implementations can call dma_fence_signal() even when before 177 * @enable_signaling has been called there's a race window, where the 178 * dma_fence_signal() might result in the final fence reference being 179 * released and its memory freed. To avoid this, implementations of this 180 * callback should grab their own reference using dma_fence_get(), to be 181 * released when the fence is signalled (through e.g. the interrupt 182 * handler). 183 * 184 * This callback is optional. If this callback is not present, then the 185 * driver must always have signaling enabled. 186 */ 187 bool (*enable_signaling)(struct dma_fence *fence); 188 189 /** 190 * @signaled: 191 * 192 * Peek whether the fence is signaled, as a fastpath optimization for 193 * e.g. dma_fence_wait() or dma_fence_add_callback(). Note that this 194 * callback does not need to make any guarantees beyond that a fence 195 * once indicates as signalled must always return true from this 196 * callback. This callback may return false even if the fence has 197 * completed already, in this case information hasn't propogated throug 198 * the system yet. See also dma_fence_is_signaled(). 199 * 200 * May set &dma_fence.error if returning true. 201 * 202 * This callback is optional. 203 */ 204 bool (*signaled)(struct dma_fence *fence); 205 206 /** 207 * @wait: 208 * 209 * Custom wait implementation, defaults to dma_fence_default_wait() if 210 * not set. 211 * 212 * Deprecated and should not be used by new implementations. Only used 213 * by existing implementations which need special handling for their 214 * hardware reset procedure. 215 * 216 * Must return -ERESTARTSYS if the wait is intr = true and the wait was 217 * interrupted, and remaining jiffies if fence has signaled, or 0 if wait 218 * timed out. Can also return other error values on custom implementations, 219 * which should be treated as if the fence is signaled. For example a hardware 220 * lockup could be reported like that. 221 */ 222 signed long (*wait)(struct dma_fence *fence, 223 bool intr, signed long timeout); 224 225 /** 226 * @release: 227 * 228 * Called on destruction of fence to release additional resources. 229 * Can be called from irq context. This callback is optional. If it is 230 * NULL, then dma_fence_free() is instead called as the default 231 * implementation. 232 */ 233 void (*release)(struct dma_fence *fence); 234 235 /** 236 * @set_deadline: 237 * 238 * Callback to allow a fence waiter to inform the fence signaler of 239 * an upcoming deadline, such as vblank, by which point the waiter 240 * would prefer the fence to be signaled by. This is intended to 241 * give feedback to the fence signaler to aid in power management 242 * decisions, such as boosting GPU frequency. 243 * 244 * This is called without &dma_fence.lock held, it can be called 245 * multiple times and from any context. Locking is up to the callee 246 * if it has some state to manage. If multiple deadlines are set, 247 * the expectation is to track the soonest one. If the deadline is 248 * before the current time, it should be interpreted as an immediate 249 * deadline. 250 * 251 * This callback is optional. 252 */ 253 void (*set_deadline)(struct dma_fence *fence, ktime_t deadline); 254 }; 255 256 void dma_fence_init(struct dma_fence *fence, const struct dma_fence_ops *ops, 257 spinlock_t *lock, u64 context, u64 seqno); 258 259 void dma_fence_init64(struct dma_fence *fence, const struct dma_fence_ops *ops, 260 spinlock_t *lock, u64 context, u64 seqno); 261 262 void dma_fence_release(struct kref *kref); 263 void dma_fence_free(struct dma_fence *fence); 264 void dma_fence_describe(struct dma_fence *fence, struct seq_file *seq); 265 266 /** 267 * dma_fence_put - decreases refcount of the fence 268 * @fence: fence to reduce refcount of 269 */ 270 static inline void dma_fence_put(struct dma_fence *fence) 271 { 272 if (fence) 273 kref_put(&fence->refcount, dma_fence_release); 274 } 275 276 /** 277 * dma_fence_get - increases refcount of the fence 278 * @fence: fence to increase refcount of 279 * 280 * Returns the same fence, with refcount increased by 1. 281 */ 282 static inline struct dma_fence *dma_fence_get(struct dma_fence *fence) 283 { 284 if (fence) 285 kref_get(&fence->refcount); 286 return fence; 287 } 288 289 /** 290 * dma_fence_get_rcu - get a fence from a dma_resv_list with 291 * rcu read lock 292 * @fence: fence to increase refcount of 293 * 294 * Function returns NULL if no refcount could be obtained, or the fence. 295 */ 296 static inline struct dma_fence *dma_fence_get_rcu(struct dma_fence *fence) 297 { 298 if (kref_get_unless_zero(&fence->refcount)) 299 return fence; 300 else 301 return NULL; 302 } 303 304 /** 305 * dma_fence_get_rcu_safe - acquire a reference to an RCU tracked fence 306 * @fencep: pointer to fence to increase refcount of 307 * 308 * Function returns NULL if no refcount could be obtained, or the fence. 309 * This function handles acquiring a reference to a fence that may be 310 * reallocated within the RCU grace period (such as with SLAB_TYPESAFE_BY_RCU), 311 * so long as the caller is using RCU on the pointer to the fence. 312 * 313 * An alternative mechanism is to employ a seqlock to protect a bunch of 314 * fences, such as used by struct dma_resv. When using a seqlock, 315 * the seqlock must be taken before and checked after a reference to the 316 * fence is acquired (as shown here). 317 * 318 * The caller is required to hold the RCU read lock. 319 */ 320 static inline struct dma_fence * 321 dma_fence_get_rcu_safe(struct dma_fence __rcu **fencep) 322 { 323 do { 324 struct dma_fence *fence; 325 326 fence = rcu_dereference(*fencep); 327 if (!fence) 328 return NULL; 329 330 if (!dma_fence_get_rcu(fence)) 331 continue; 332 333 /* The atomic_inc_not_zero() inside dma_fence_get_rcu() 334 * provides a full memory barrier upon success (such as now). 335 * This is paired with the write barrier from assigning 336 * to the __rcu protected fence pointer so that if that 337 * pointer still matches the current fence, we know we 338 * have successfully acquire a reference to it. If it no 339 * longer matches, we are holding a reference to some other 340 * reallocated pointer. This is possible if the allocator 341 * is using a freelist like SLAB_TYPESAFE_BY_RCU where the 342 * fence remains valid for the RCU grace period, but it 343 * may be reallocated. When using such allocators, we are 344 * responsible for ensuring the reference we get is to 345 * the right fence, as below. 346 */ 347 if (fence == rcu_access_pointer(*fencep)) 348 return rcu_pointer_handoff(fence); 349 350 dma_fence_put(fence); 351 } while (1); 352 } 353 354 #ifdef CONFIG_LOCKDEP 355 bool dma_fence_begin_signalling(void); 356 void dma_fence_end_signalling(bool cookie); 357 void __dma_fence_might_wait(void); 358 #else 359 static inline bool dma_fence_begin_signalling(void) 360 { 361 return true; 362 } 363 static inline void dma_fence_end_signalling(bool cookie) {} 364 static inline void __dma_fence_might_wait(void) {} 365 #endif 366 367 void dma_fence_signal(struct dma_fence *fence); 368 bool dma_fence_check_and_signal(struct dma_fence *fence); 369 bool dma_fence_check_and_signal_locked(struct dma_fence *fence); 370 void dma_fence_signal_locked(struct dma_fence *fence); 371 void dma_fence_signal_timestamp(struct dma_fence *fence, ktime_t timestamp); 372 void dma_fence_signal_timestamp_locked(struct dma_fence *fence, ktime_t timestamp); 373 signed long dma_fence_default_wait(struct dma_fence *fence, 374 bool intr, signed long timeout); 375 int dma_fence_add_callback(struct dma_fence *fence, 376 struct dma_fence_cb *cb, 377 dma_fence_func_t func); 378 bool dma_fence_remove_callback(struct dma_fence *fence, 379 struct dma_fence_cb *cb); 380 void dma_fence_enable_sw_signaling(struct dma_fence *fence); 381 382 /** 383 * DOC: Safe external access to driver provided object members 384 * 385 * All data not stored directly in the dma-fence object, such as the 386 * &dma_fence.lock and memory potentially accessed by functions in the 387 * &dma_fence.ops table, MUST NOT be accessed after the fence has been signalled 388 * because after that point drivers are allowed to free it. 389 * 390 * All code accessing that data via the dma-fence API (or directly, which is 391 * discouraged), MUST make sure to contain the complete access within a 392 * &rcu_read_lock and &rcu_read_unlock pair. 393 * 394 * Some dma-fence API handles this automatically, while other, as for example 395 * &dma_fence_driver_name and &dma_fence_timeline_name, leave that 396 * responsibility to the caller. 397 * 398 * To enable this scheme to work drivers MUST ensure a RCU grace period elapses 399 * between signalling the fence and freeing the said data. 400 * 401 */ 402 const char __rcu *dma_fence_driver_name(struct dma_fence *fence); 403 const char __rcu *dma_fence_timeline_name(struct dma_fence *fence); 404 405 /* 406 * dma_fence_test_signaled_flag - Only check whether a fence is signaled yet. 407 * @fence: the fence to check 408 * 409 * This function just checks whether @fence is signaled, without interacting 410 * with the fence in any way. The user must, therefore, ensure through other 411 * means that fences get signaled eventually. 412 * 413 * This function uses test_bit(), which is thread-safe. Naturally, this function 414 * should be used opportunistically; a fence could get signaled at any moment 415 * after the check is done. 416 * 417 * Return: true if signaled, false otherwise. 418 */ 419 static inline bool 420 dma_fence_test_signaled_flag(struct dma_fence *fence) 421 { 422 return test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags); 423 } 424 425 /** 426 * dma_fence_is_signaled_locked - Return an indication if the fence 427 * is signaled yet. 428 * @fence: the fence to check 429 * 430 * Returns true if the fence was already signaled, false if not. Since this 431 * function doesn't enable signaling, it is not guaranteed to ever return 432 * true if dma_fence_add_callback(), dma_fence_wait() or 433 * dma_fence_enable_sw_signaling() haven't been called before. 434 * 435 * This function requires &dma_fence.lock to be held. 436 * 437 * See also dma_fence_is_signaled(). 438 */ 439 static inline bool 440 dma_fence_is_signaled_locked(struct dma_fence *fence) 441 { 442 if (dma_fence_test_signaled_flag(fence)) 443 return true; 444 445 if (fence->ops->signaled && fence->ops->signaled(fence)) { 446 dma_fence_signal_locked(fence); 447 return true; 448 } 449 450 return false; 451 } 452 453 /** 454 * dma_fence_is_signaled - Return an indication if the fence is signaled yet. 455 * @fence: the fence to check 456 * 457 * Returns true if the fence was already signaled, false if not. Since this 458 * function doesn't enable signaling, it is not guaranteed to ever return 459 * true if dma_fence_add_callback(), dma_fence_wait() or 460 * dma_fence_enable_sw_signaling() haven't been called before. 461 * 462 * It's recommended for seqno fences to call dma_fence_signal when the 463 * operation is complete, it makes it possible to prevent issues from 464 * wraparound between time of issue and time of use by checking the return 465 * value of this function before calling hardware-specific wait instructions. 466 * 467 * See also dma_fence_is_signaled_locked(). 468 */ 469 static inline bool 470 dma_fence_is_signaled(struct dma_fence *fence) 471 { 472 if (dma_fence_test_signaled_flag(fence)) 473 return true; 474 475 if (fence->ops->signaled && fence->ops->signaled(fence)) { 476 dma_fence_signal(fence); 477 return true; 478 } 479 480 return false; 481 } 482 483 /** 484 * __dma_fence_is_later - return if f1 is chronologically later than f2 485 * @fence: fence in whose context to do the comparison 486 * @f1: the first fence's seqno 487 * @f2: the second fence's seqno from the same context 488 * 489 * Returns true if f1 is chronologically later than f2. Both fences must be 490 * from the same context, since a seqno is not common across contexts. 491 */ 492 static inline bool __dma_fence_is_later(struct dma_fence *fence, u64 f1, u64 f2) 493 { 494 /* This is for backward compatibility with drivers which can only handle 495 * 32bit sequence numbers. Use a 64bit compare when the driver says to 496 * do so. 497 */ 498 if (test_bit(DMA_FENCE_FLAG_SEQNO64_BIT, &fence->flags)) 499 return f1 > f2; 500 501 return (int)(lower_32_bits(f1) - lower_32_bits(f2)) > 0; 502 } 503 504 /** 505 * dma_fence_is_later - return if f1 is chronologically later than f2 506 * @f1: the first fence from the same context 507 * @f2: the second fence from the same context 508 * 509 * Returns true if f1 is chronologically later than f2. Both fences must be 510 * from the same context, since a seqno is not re-used across contexts. 511 */ 512 static inline bool dma_fence_is_later(struct dma_fence *f1, 513 struct dma_fence *f2) 514 { 515 if (WARN_ON(f1->context != f2->context)) 516 return false; 517 518 return __dma_fence_is_later(f1, f1->seqno, f2->seqno); 519 } 520 521 /** 522 * dma_fence_is_later_or_same - return true if f1 is later or same as f2 523 * @f1: the first fence from the same context 524 * @f2: the second fence from the same context 525 * 526 * Returns true if f1 is chronologically later than f2 or the same fence. Both 527 * fences must be from the same context, since a seqno is not re-used across 528 * contexts. 529 */ 530 static inline bool dma_fence_is_later_or_same(struct dma_fence *f1, 531 struct dma_fence *f2) 532 { 533 return f1 == f2 || dma_fence_is_later(f1, f2); 534 } 535 536 /** 537 * dma_fence_later - return the chronologically later fence 538 * @f1: the first fence from the same context 539 * @f2: the second fence from the same context 540 * 541 * Returns NULL if both fences are signaled, otherwise the fence that would be 542 * signaled last. Both fences must be from the same context, since a seqno is 543 * not re-used across contexts. 544 */ 545 static inline struct dma_fence *dma_fence_later(struct dma_fence *f1, 546 struct dma_fence *f2) 547 { 548 if (WARN_ON(f1->context != f2->context)) 549 return NULL; 550 551 /* 552 * Can't check just DMA_FENCE_FLAG_SIGNALED_BIT here, it may never 553 * have been set if enable_signaling wasn't called, and enabling that 554 * here is overkill. 555 */ 556 if (dma_fence_is_later(f1, f2)) 557 return dma_fence_is_signaled(f1) ? NULL : f1; 558 else 559 return dma_fence_is_signaled(f2) ? NULL : f2; 560 } 561 562 /** 563 * dma_fence_get_status_locked - returns the status upon completion 564 * @fence: the dma_fence to query 565 * 566 * Drivers can supply an optional error status condition before they signal 567 * the fence (to indicate whether the fence was completed due to an error 568 * rather than success). The value of the status condition is only valid 569 * if the fence has been signaled, dma_fence_get_status_locked() first checks 570 * the signal state before reporting the error status. 571 * 572 * Returns 0 if the fence has not yet been signaled, 1 if the fence has 573 * been signaled without an error condition, or a negative error code 574 * if the fence has been completed in err. 575 */ 576 static inline int dma_fence_get_status_locked(struct dma_fence *fence) 577 { 578 if (dma_fence_is_signaled_locked(fence)) 579 return fence->error ?: 1; 580 else 581 return 0; 582 } 583 584 int dma_fence_get_status(struct dma_fence *fence); 585 586 /** 587 * dma_fence_set_error - flag an error condition on the fence 588 * @fence: the dma_fence 589 * @error: the error to store 590 * 591 * Drivers can supply an optional error status condition before they signal 592 * the fence, to indicate that the fence was completed due to an error 593 * rather than success. This must be set before signaling (so that the value 594 * is visible before any waiters on the signal callback are woken). This 595 * helper exists to help catching erroneous setting of #dma_fence.error. 596 * 597 * Examples of error codes which drivers should use: 598 * 599 * * %-ENODATA This operation produced no data, no other operation affected. 600 * * %-ECANCELED All operations from the same context have been canceled. 601 * * %-ETIME Operation caused a timeout and potentially device reset. 602 */ 603 static inline void dma_fence_set_error(struct dma_fence *fence, 604 int error) 605 { 606 WARN_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)); 607 WARN_ON(error >= 0 || error < -MAX_ERRNO); 608 609 fence->error = error; 610 } 611 612 /** 613 * dma_fence_timestamp - helper to get the completion timestamp of a fence 614 * @fence: fence to get the timestamp from. 615 * 616 * After a fence is signaled the timestamp is updated with the signaling time, 617 * but setting the timestamp can race with tasks waiting for the signaling. This 618 * helper busy waits for the correct timestamp to appear. 619 */ 620 static inline ktime_t dma_fence_timestamp(struct dma_fence *fence) 621 { 622 if (WARN_ON(!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))) 623 return ktime_get(); 624 625 while (!test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags)) 626 cpu_relax(); 627 628 return fence->timestamp; 629 } 630 631 signed long dma_fence_wait_timeout(struct dma_fence *, 632 bool intr, signed long timeout); 633 signed long dma_fence_wait_any_timeout(struct dma_fence **fences, 634 uint32_t count, 635 bool intr, signed long timeout, 636 uint32_t *idx); 637 638 /** 639 * dma_fence_wait - sleep until the fence gets signaled 640 * @fence: the fence to wait on 641 * @intr: if true, do an interruptible wait 642 * 643 * This function will return -ERESTARTSYS if interrupted by a signal, 644 * or 0 if the fence was signaled. Other error values may be 645 * returned on custom implementations. 646 * 647 * Performs a synchronous wait on this fence. It is assumed the caller 648 * directly or indirectly holds a reference to the fence, otherwise the 649 * fence might be freed before return, resulting in undefined behavior. 650 * 651 * See also dma_fence_wait_timeout() and dma_fence_wait_any_timeout(). 652 */ 653 static inline signed long dma_fence_wait(struct dma_fence *fence, bool intr) 654 { 655 signed long ret; 656 657 /* Since dma_fence_wait_timeout cannot timeout with 658 * MAX_SCHEDULE_TIMEOUT, only valid return values are 659 * -ERESTARTSYS and MAX_SCHEDULE_TIMEOUT. 660 */ 661 ret = dma_fence_wait_timeout(fence, intr, MAX_SCHEDULE_TIMEOUT); 662 663 return ret < 0 ? ret : 0; 664 } 665 666 void dma_fence_set_deadline(struct dma_fence *fence, ktime_t deadline); 667 668 struct dma_fence *dma_fence_get_stub(void); 669 struct dma_fence *dma_fence_allocate_private_stub(ktime_t timestamp); 670 u64 dma_fence_context_alloc(unsigned num); 671 672 extern const struct dma_fence_ops dma_fence_array_ops; 673 extern const struct dma_fence_ops dma_fence_chain_ops; 674 675 /** 676 * dma_fence_is_array - check if a fence is from the array subclass 677 * @fence: the fence to test 678 * 679 * Return true if it is a dma_fence_array and false otherwise. 680 */ 681 static inline bool dma_fence_is_array(struct dma_fence *fence) 682 { 683 return fence->ops == &dma_fence_array_ops; 684 } 685 686 /** 687 * dma_fence_is_chain - check if a fence is from the chain subclass 688 * @fence: the fence to test 689 * 690 * Return true if it is a dma_fence_chain and false otherwise. 691 */ 692 static inline bool dma_fence_is_chain(struct dma_fence *fence) 693 { 694 return fence->ops == &dma_fence_chain_ops; 695 } 696 697 /** 698 * dma_fence_is_container - check if a fence is a container for other fences 699 * @fence: the fence to test 700 * 701 * Return true if this fence is a container for other fences, false otherwise. 702 * This is important since we can't build up large fence structure or otherwise 703 * we run into recursion during operation on those fences. 704 */ 705 static inline bool dma_fence_is_container(struct dma_fence *fence) 706 { 707 return dma_fence_is_array(fence) || dma_fence_is_chain(fence); 708 } 709 710 #endif /* __LINUX_DMA_FENCE_H */ 711