seqno-fence.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * seqno-fence, using a dma-buf to synchronize fencing
  4. *
  5. * Copyright (C) 2012 Texas Instruments
  6. * Copyright (C) 2012 Canonical Ltd
  7. * Authors:
  8. * Rob Clark <robdclark@gmail.com>
  9. * Maarten Lankhorst <maarten.lankhorst@canonical.com>
  10. */
  11. #ifndef __LINUX_SEQNO_FENCE_H
  12. #define __LINUX_SEQNO_FENCE_H
  13. #include <linux/dma-fence.h>
  14. #include <linux/dma-buf.h>
  15. enum seqno_fence_condition {
  16. SEQNO_FENCE_WAIT_GEQUAL,
  17. SEQNO_FENCE_WAIT_NONZERO
  18. };
  19. struct seqno_fence {
  20. struct dma_fence base;
  21. const struct dma_fence_ops *ops;
  22. struct dma_buf *sync_buf;
  23. uint32_t seqno_ofs;
  24. enum seqno_fence_condition condition;
  25. };
  26. extern const struct dma_fence_ops seqno_fence_ops;
  27. /**
  28. * to_seqno_fence - cast a fence to a seqno_fence
  29. * @fence: fence to cast to a seqno_fence
  30. *
  31. * Returns NULL if the fence is not a seqno_fence,
  32. * or the seqno_fence otherwise.
  33. */
  34. static inline struct seqno_fence *
  35. to_seqno_fence(struct dma_fence *fence)
  36. {
  37. if (fence->ops != &seqno_fence_ops)
  38. return NULL;
  39. return container_of(fence, struct seqno_fence, base);
  40. }
  41. /**
  42. * seqno_fence_init - initialize a seqno fence
  43. * @fence: seqno_fence to initialize
  44. * @lock: pointer to spinlock to use for fence
  45. * @sync_buf: buffer containing the memory location to signal on
  46. * @context: the execution context this fence is a part of
  47. * @seqno_ofs: the offset within @sync_buf
  48. * @seqno: the sequence # to signal on
  49. * @cond: fence wait condition
  50. * @ops: the fence_ops for operations on this seqno fence
  51. *
  52. * This function initializes a struct seqno_fence with passed parameters,
  53. * and takes a reference on sync_buf which is released on fence destruction.
  54. *
  55. * A seqno_fence is a dma_fence which can complete in software when
  56. * enable_signaling is called, but it also completes when
  57. * (s32)((sync_buf)[seqno_ofs] - seqno) >= 0 is true
  58. *
  59. * The seqno_fence will take a refcount on the sync_buf until it's
  60. * destroyed, but actual lifetime of sync_buf may be longer if one of the
  61. * callers take a reference to it.
  62. *
  63. * Certain hardware have instructions to insert this type of wait condition
  64. * in the command stream, so no intervention from software would be needed.
  65. * This type of fence can be destroyed before completed, however a reference
  66. * on the sync_buf dma-buf can be taken. It is encouraged to re-use the same
  67. * dma-buf for sync_buf, since mapping or unmapping the sync_buf to the
  68. * device's vm can be expensive.
  69. *
  70. * It is recommended for creators of seqno_fence to call dma_fence_signal()
  71. * before destruction. This will prevent possible issues from wraparound at
  72. * time of issue vs time of check, since users can check dma_fence_is_signaled()
  73. * before submitting instructions for the hardware to wait on the fence.
  74. * However, when ops.enable_signaling is not called, it doesn't have to be
  75. * done as soon as possible, just before there's any real danger of seqno
  76. * wraparound.
  77. */
  78. static inline void
  79. seqno_fence_init(struct seqno_fence *fence, spinlock_t *lock,
  80. struct dma_buf *sync_buf, uint32_t context,
  81. uint32_t seqno_ofs, uint32_t seqno,
  82. enum seqno_fence_condition cond,
  83. const struct dma_fence_ops *ops)
  84. {
  85. BUG_ON(!fence || !sync_buf || !ops);
  86. BUG_ON(!ops->wait || !ops->enable_signaling ||
  87. !ops->get_driver_name || !ops->get_timeline_name);
  88. /*
  89. * ops is used in dma_fence_init for get_driver_name, so needs to be
  90. * initialized first
  91. */
  92. fence->ops = ops;
  93. dma_fence_init(&fence->base, &seqno_fence_ops, lock, context, seqno);
  94. get_dma_buf(sync_buf);
  95. fence->sync_buf = sync_buf;
  96. fence->seqno_ofs = seqno_ofs;
  97. fence->condition = cond;
  98. }
  99. #endif /* __LINUX_SEQNO_FENCE_H */