dma-fence.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. #ifndef __LINUX_DMA_FENCE_H
  21. #define __LINUX_DMA_FENCE_H
  22. #include <linux/err.h>
  23. #include <linux/wait.h>
  24. #include <linux/list.h>
  25. #include <linux/bitops.h>
  26. #include <linux/kref.h>
  27. #include <linux/sched.h>
  28. #include <linux/printk.h>
  29. #include <linux/rcupdate.h>
  30. struct dma_fence;
  31. struct dma_fence_ops;
  32. struct dma_fence_cb;
  33. /**
  34. * struct dma_fence - software synchronization primitive
  35. * @refcount: refcount for this fence
  36. * @ops: dma_fence_ops associated with this fence
  37. * @rcu: used for releasing fence with kfree_rcu
  38. * @cb_list: list of all callbacks to call
  39. * @lock: spin_lock_irqsave used for locking
  40. * @context: execution context this fence belongs to, returned by
  41. * dma_fence_context_alloc()
  42. * @seqno: the sequence number of this fence inside the execution context,
  43. * can be compared to decide which fence would be signaled later.
  44. * @flags: A mask of DMA_FENCE_FLAG_* defined below
  45. * @timestamp: Timestamp when the fence was signaled.
  46. * @status: Optional, only valid if < 0, must be set before calling
  47. * dma_fence_signal, indicates that the fence has completed with an error.
  48. *
  49. * the flags member must be manipulated and read using the appropriate
  50. * atomic ops (bit_*), so taking the spinlock will not be needed most
  51. * of the time.
  52. *
  53. * DMA_FENCE_FLAG_SIGNALED_BIT - fence is already signaled
  54. * DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT - enable_signaling might have been called
  55. * DMA_FENCE_FLAG_USER_BITS - start of the unused bits, can be used by the
  56. * implementer of the fence for its own purposes. Can be used in different
  57. * ways by different fence implementers, so do not rely on this.
  58. *
  59. * Since atomic bitops are used, this is not guaranteed to be the case.
  60. * Particularly, if the bit was set, but dma_fence_signal was called right
  61. * before this bit was set, it would have been able to set the
  62. * DMA_FENCE_FLAG_SIGNALED_BIT, before enable_signaling was called.
  63. * Adding a check for DMA_FENCE_FLAG_SIGNALED_BIT after setting
  64. * DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT closes this race, and makes sure that
  65. * after dma_fence_signal was called, any enable_signaling call will have either
  66. * been completed, or never called at all.
  67. */
  68. struct dma_fence {
  69. struct kref refcount;
  70. const struct dma_fence_ops *ops;
  71. struct rcu_head rcu;
  72. struct list_head cb_list;
  73. spinlock_t *lock;
  74. u64 context;
  75. unsigned seqno;
  76. unsigned long flags;
  77. ktime_t timestamp;
  78. int status;
  79. };
  80. enum dma_fence_flag_bits {
  81. DMA_FENCE_FLAG_SIGNALED_BIT,
  82. DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
  83. DMA_FENCE_FLAG_USER_BITS, /* must always be last member */
  84. };
  85. typedef void (*dma_fence_func_t)(struct dma_fence *fence,
  86. struct dma_fence_cb *cb);
  87. /**
  88. * struct dma_fence_cb - callback for dma_fence_add_callback
  89. * @node: used by dma_fence_add_callback to append this struct to fence::cb_list
  90. * @func: dma_fence_func_t to call
  91. *
  92. * This struct will be initialized by dma_fence_add_callback, additional
  93. * data can be passed along by embedding dma_fence_cb in another struct.
  94. */
  95. struct dma_fence_cb {
  96. struct list_head node;
  97. dma_fence_func_t func;
  98. };
  99. /**
  100. * struct dma_fence_ops - operations implemented for fence
  101. * @get_driver_name: returns the driver name.
  102. * @get_timeline_name: return the name of the context this fence belongs to.
  103. * @enable_signaling: enable software signaling of fence.
  104. * @signaled: [optional] peek whether the fence is signaled, can be null.
  105. * @wait: custom wait implementation, or dma_fence_default_wait.
  106. * @release: [optional] called on destruction of fence, can be null
  107. * @fill_driver_data: [optional] callback to fill in free-form debug info
  108. * Returns amount of bytes filled, or -errno.
  109. * @fence_value_str: [optional] fills in the value of the fence as a string
  110. * @timeline_value_str: [optional] fills in the current value of the timeline
  111. * as a string
  112. *
  113. * Notes on enable_signaling:
  114. * For fence implementations that have the capability for hw->hw
  115. * signaling, they can implement this op to enable the necessary
  116. * irqs, or insert commands into cmdstream, etc. This is called
  117. * in the first wait() or add_callback() path to let the fence
  118. * implementation know that there is another driver waiting on
  119. * the signal (ie. hw->sw case).
  120. *
  121. * This function can be called called from atomic context, but not
  122. * from irq context, so normal spinlocks can be used.
  123. *
  124. * A return value of false indicates the fence already passed,
  125. * or some failure occurred that made it impossible to enable
  126. * signaling. True indicates successful enabling.
  127. *
  128. * fence->status may be set in enable_signaling, but only when false is
  129. * returned.
  130. *
  131. * Calling dma_fence_signal before enable_signaling is called allows
  132. * for a tiny race window in which enable_signaling is called during,
  133. * before, or after dma_fence_signal. To fight this, it is recommended
  134. * that before enable_signaling returns true an extra reference is
  135. * taken on the fence, to be released when the fence is signaled.
  136. * This will mean dma_fence_signal will still be called twice, but
  137. * the second time will be a noop since it was already signaled.
  138. *
  139. * Notes on signaled:
  140. * May set fence->status if returning true.
  141. *
  142. * Notes on wait:
  143. * Must not be NULL, set to dma_fence_default_wait for default implementation.
  144. * the dma_fence_default_wait implementation should work for any fence, as long
  145. * as enable_signaling works correctly.
  146. *
  147. * Must return -ERESTARTSYS if the wait is intr = true and the wait was
  148. * interrupted, and remaining jiffies if fence has signaled, or 0 if wait
  149. * timed out. Can also return other error values on custom implementations,
  150. * which should be treated as if the fence is signaled. For example a hardware
  151. * lockup could be reported like that.
  152. *
  153. * Notes on release:
  154. * Can be NULL, this function allows additional commands to run on
  155. * destruction of the fence. Can be called from irq context.
  156. * If pointer is set to NULL, kfree will get called instead.
  157. */
  158. struct dma_fence_ops {
  159. const char * (*get_driver_name)(struct dma_fence *fence);
  160. const char * (*get_timeline_name)(struct dma_fence *fence);
  161. bool (*enable_signaling)(struct dma_fence *fence);
  162. bool (*signaled)(struct dma_fence *fence);
  163. signed long (*wait)(struct dma_fence *fence,
  164. bool intr, signed long timeout);
  165. void (*release)(struct dma_fence *fence);
  166. int (*fill_driver_data)(struct dma_fence *fence, void *data, int size);
  167. void (*fence_value_str)(struct dma_fence *fence, char *str, int size);
  168. void (*timeline_value_str)(struct dma_fence *fence,
  169. char *str, int size);
  170. };
  171. void dma_fence_init(struct dma_fence *fence, const struct dma_fence_ops *ops,
  172. spinlock_t *lock, u64 context, unsigned seqno);
  173. void dma_fence_release(struct kref *kref);
  174. void dma_fence_free(struct dma_fence *fence);
  175. /**
  176. * dma_fence_put - decreases refcount of the fence
  177. * @fence: [in] fence to reduce refcount of
  178. */
  179. static inline void dma_fence_put(struct dma_fence *fence)
  180. {
  181. if (fence)
  182. kref_put(&fence->refcount, dma_fence_release);
  183. }
  184. /**
  185. * dma_fence_get - increases refcount of the fence
  186. * @fence: [in] fence to increase refcount of
  187. *
  188. * Returns the same fence, with refcount increased by 1.
  189. */
  190. static inline struct dma_fence *dma_fence_get(struct dma_fence *fence)
  191. {
  192. if (fence)
  193. kref_get(&fence->refcount);
  194. return fence;
  195. }
  196. /**
  197. * dma_fence_get_rcu - get a fence from a reservation_object_list with
  198. * rcu read lock
  199. * @fence: [in] fence to increase refcount of
  200. *
  201. * Function returns NULL if no refcount could be obtained, or the fence.
  202. */
  203. static inline struct dma_fence *dma_fence_get_rcu(struct dma_fence *fence)
  204. {
  205. if (kref_get_unless_zero(&fence->refcount))
  206. return fence;
  207. else
  208. return NULL;
  209. }
  210. /**
  211. * dma_fence_get_rcu_safe - acquire a reference to an RCU tracked fence
  212. * @fencep: [in] pointer to fence to increase refcount of
  213. *
  214. * Function returns NULL if no refcount could be obtained, or the fence.
  215. * This function handles acquiring a reference to a fence that may be
  216. * reallocated within the RCU grace period (such as with SLAB_DESTROY_BY_RCU),
  217. * so long as the caller is using RCU on the pointer to the fence.
  218. *
  219. * An alternative mechanism is to employ a seqlock to protect a bunch of
  220. * fences, such as used by struct reservation_object. When using a seqlock,
  221. * the seqlock must be taken before and checked after a reference to the
  222. * fence is acquired (as shown here).
  223. *
  224. * The caller is required to hold the RCU read lock.
  225. */
  226. static inline struct dma_fence *
  227. dma_fence_get_rcu_safe(struct dma_fence * __rcu *fencep)
  228. {
  229. do {
  230. struct dma_fence *fence;
  231. fence = rcu_dereference(*fencep);
  232. if (!fence || !dma_fence_get_rcu(fence))
  233. return NULL;
  234. /* The atomic_inc_not_zero() inside dma_fence_get_rcu()
  235. * provides a full memory barrier upon success (such as now).
  236. * This is paired with the write barrier from assigning
  237. * to the __rcu protected fence pointer so that if that
  238. * pointer still matches the current fence, we know we
  239. * have successfully acquire a reference to it. If it no
  240. * longer matches, we are holding a reference to some other
  241. * reallocated pointer. This is possible if the allocator
  242. * is using a freelist like SLAB_DESTROY_BY_RCU where the
  243. * fence remains valid for the RCU grace period, but it
  244. * may be reallocated. When using such allocators, we are
  245. * responsible for ensuring the reference we get is to
  246. * the right fence, as below.
  247. */
  248. if (fence == rcu_access_pointer(*fencep))
  249. return rcu_pointer_handoff(fence);
  250. dma_fence_put(fence);
  251. } while (1);
  252. }
  253. int dma_fence_signal(struct dma_fence *fence);
  254. int dma_fence_signal_locked(struct dma_fence *fence);
  255. signed long dma_fence_default_wait(struct dma_fence *fence,
  256. bool intr, signed long timeout);
  257. int dma_fence_add_callback(struct dma_fence *fence,
  258. struct dma_fence_cb *cb,
  259. dma_fence_func_t func);
  260. bool dma_fence_remove_callback(struct dma_fence *fence,
  261. struct dma_fence_cb *cb);
  262. void dma_fence_enable_sw_signaling(struct dma_fence *fence);
  263. /**
  264. * dma_fence_is_signaled_locked - Return an indication if the fence
  265. * is signaled yet.
  266. * @fence: [in] the fence to check
  267. *
  268. * Returns true if the fence was already signaled, false if not. Since this
  269. * function doesn't enable signaling, it is not guaranteed to ever return
  270. * true if dma_fence_add_callback, dma_fence_wait or
  271. * dma_fence_enable_sw_signaling haven't been called before.
  272. *
  273. * This function requires fence->lock to be held.
  274. */
  275. static inline bool
  276. dma_fence_is_signaled_locked(struct dma_fence *fence)
  277. {
  278. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  279. return true;
  280. if (fence->ops->signaled && fence->ops->signaled(fence)) {
  281. dma_fence_signal_locked(fence);
  282. return true;
  283. }
  284. return false;
  285. }
  286. /**
  287. * dma_fence_is_signaled - Return an indication if the fence is signaled yet.
  288. * @fence: [in] the fence to check
  289. *
  290. * Returns true if the fence was already signaled, false if not. Since this
  291. * function doesn't enable signaling, it is not guaranteed to ever return
  292. * true if dma_fence_add_callback, dma_fence_wait or
  293. * dma_fence_enable_sw_signaling haven't been called before.
  294. *
  295. * It's recommended for seqno fences to call dma_fence_signal when the
  296. * operation is complete, it makes it possible to prevent issues from
  297. * wraparound between time of issue and time of use by checking the return
  298. * value of this function before calling hardware-specific wait instructions.
  299. */
  300. static inline bool
  301. dma_fence_is_signaled(struct dma_fence *fence)
  302. {
  303. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  304. return true;
  305. if (fence->ops->signaled && fence->ops->signaled(fence)) {
  306. dma_fence_signal(fence);
  307. return true;
  308. }
  309. return false;
  310. }
  311. /**
  312. * dma_fence_is_later - return if f1 is chronologically later than f2
  313. * @f1: [in] the first fence from the same context
  314. * @f2: [in] the second fence from the same context
  315. *
  316. * Returns true if f1 is chronologically later than f2. Both fences must be
  317. * from the same context, since a seqno is not re-used across contexts.
  318. */
  319. static inline bool dma_fence_is_later(struct dma_fence *f1,
  320. struct dma_fence *f2)
  321. {
  322. if (WARN_ON(f1->context != f2->context))
  323. return false;
  324. return (int)(f1->seqno - f2->seqno) > 0;
  325. }
  326. /**
  327. * dma_fence_later - return the chronologically later fence
  328. * @f1: [in] the first fence from the same context
  329. * @f2: [in] the second fence from the same context
  330. *
  331. * Returns NULL if both fences are signaled, otherwise the fence that would be
  332. * signaled last. Both fences must be from the same context, since a seqno is
  333. * not re-used across contexts.
  334. */
  335. static inline struct dma_fence *dma_fence_later(struct dma_fence *f1,
  336. struct dma_fence *f2)
  337. {
  338. if (WARN_ON(f1->context != f2->context))
  339. return NULL;
  340. /*
  341. * Can't check just DMA_FENCE_FLAG_SIGNALED_BIT here, it may never
  342. * have been set if enable_signaling wasn't called, and enabling that
  343. * here is overkill.
  344. */
  345. if (dma_fence_is_later(f1, f2))
  346. return dma_fence_is_signaled(f1) ? NULL : f1;
  347. else
  348. return dma_fence_is_signaled(f2) ? NULL : f2;
  349. }
  350. signed long dma_fence_wait_timeout(struct dma_fence *,
  351. bool intr, signed long timeout);
  352. signed long dma_fence_wait_any_timeout(struct dma_fence **fences,
  353. uint32_t count,
  354. bool intr, signed long timeout,
  355. uint32_t *idx);
  356. /**
  357. * dma_fence_wait - sleep until the fence gets signaled
  358. * @fence: [in] the fence to wait on
  359. * @intr: [in] if true, do an interruptible wait
  360. *
  361. * This function will return -ERESTARTSYS if interrupted by a signal,
  362. * or 0 if the fence was signaled. Other error values may be
  363. * returned on custom implementations.
  364. *
  365. * Performs a synchronous wait on this fence. It is assumed the caller
  366. * directly or indirectly holds a reference to the fence, otherwise the
  367. * fence might be freed before return, resulting in undefined behavior.
  368. */
  369. static inline signed long dma_fence_wait(struct dma_fence *fence, bool intr)
  370. {
  371. signed long ret;
  372. /* Since dma_fence_wait_timeout cannot timeout with
  373. * MAX_SCHEDULE_TIMEOUT, only valid return values are
  374. * -ERESTARTSYS and MAX_SCHEDULE_TIMEOUT.
  375. */
  376. ret = dma_fence_wait_timeout(fence, intr, MAX_SCHEDULE_TIMEOUT);
  377. return ret < 0 ? ret : 0;
  378. }
  379. u64 dma_fence_context_alloc(unsigned num);
  380. #define DMA_FENCE_TRACE(f, fmt, args...) \
  381. do { \
  382. struct dma_fence *__ff = (f); \
  383. if (IS_ENABLED(CONFIG_DMA_FENCE_TRACE)) \
  384. pr_info("f %llu#%u: " fmt, \
  385. __ff->context, __ff->seqno, ##args); \
  386. } while (0)
  387. #define DMA_FENCE_WARN(f, fmt, args...) \
  388. do { \
  389. struct dma_fence *__ff = (f); \
  390. pr_warn("f %llu#%u: " fmt, __ff->context, __ff->seqno, \
  391. ##args); \
  392. } while (0)
  393. #define DMA_FENCE_ERR(f, fmt, args...) \
  394. do { \
  395. struct dma_fence *__ff = (f); \
  396. pr_err("f %llu#%u: " fmt, __ff->context, __ff->seqno, \
  397. ##args); \
  398. } while (0)
  399. #endif /* __LINUX_DMA_FENCE_H */