fence.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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_FENCE_H
  21. #define __LINUX_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 fence;
  31. struct fence_ops;
  32. struct fence_cb;
  33. /**
  34. * struct fence - software synchronization primitive
  35. * @refcount: refcount for this fence
  36. * @ops: 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. * 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 FENCE_FLAG_* defined below
  45. * @timestamp: Timestamp when the fence was signaled.
  46. * @error: Optional, only valid if < 0, must be set before calling
  47. * 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. * FENCE_FLAG_SIGNALED_BIT - fence is already signaled
  54. * FENCE_FLAG_ENABLE_SIGNAL_BIT - enable_signaling might have been called*
  55. * 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 fence_signal was called right
  61. * before this bit was set, it would have been able to set the
  62. * FENCE_FLAG_SIGNALED_BIT, before enable_signaling was called.
  63. * Adding a check for FENCE_FLAG_SIGNALED_BIT after setting
  64. * FENCE_FLAG_ENABLE_SIGNAL_BIT closes this race, and makes sure that
  65. * after fence_signal was called, any enable_signaling call will have either
  66. * been completed, or never called at all.
  67. */
  68. struct fence {
  69. struct kref refcount;
  70. const struct 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 error;
  79. };
  80. enum fence_flag_bits {
  81. FENCE_FLAG_SIGNALED_BIT,
  82. FENCE_FLAG_ENABLE_SIGNAL_BIT,
  83. FENCE_FLAG_USER_BITS, /* must always be last member */
  84. };
  85. typedef void (*fence_func_t)(struct fence *fence, struct fence_cb *cb);
  86. /**
  87. * struct fence_cb - callback for fence_add_callback
  88. * @node: used by fence_add_callback to append this struct to fence::cb_list
  89. * @func: fence_func_t to call
  90. *
  91. * This struct will be initialized by fence_add_callback, additional
  92. * data can be passed along by embedding fence_cb in another struct.
  93. */
  94. struct fence_cb {
  95. struct list_head node;
  96. fence_func_t func;
  97. };
  98. /**
  99. * struct fence_ops - operations implemented for fence
  100. * @get_driver_name: returns the driver name.
  101. * @get_timeline_name: return the name of the context this fence belongs to.
  102. * @enable_signaling: enable software signaling of fence.
  103. * @signaled: [optional] peek whether the fence is signaled, can be null.
  104. * @wait: custom wait implementation, or fence_default_wait.
  105. * @release: [optional] called on destruction of fence, can be null
  106. * @fill_driver_data: [optional] callback to fill in free-form debug info
  107. * Returns amount of bytes filled, or -errno.
  108. * @fence_value_str: [optional] fills in the value of the fence as a string
  109. * @timeline_value_str: [optional] fills in the current value of the timeline
  110. * as a string
  111. *
  112. * Notes on enable_signaling:
  113. * For fence implementations that have the capability for hw->hw
  114. * signaling, they can implement this op to enable the necessary
  115. * irqs, or insert commands into cmdstream, etc. This is called
  116. * in the first wait() or add_callback() path to let the fence
  117. * implementation know that there is another driver waiting on
  118. * the signal (ie. hw->sw case).
  119. *
  120. * This function can be called called from atomic context, but not
  121. * from irq context, so normal spinlocks can be used.
  122. *
  123. * A return value of false indicates the fence already passed,
  124. * or some failure occurred that made it impossible to enable
  125. * signaling. True indicates successful enabling.
  126. *
  127. * fence->error may be set in enable_signaling, but only when false is
  128. * returned.
  129. *
  130. * Calling fence_signal before enable_signaling is called allows
  131. * for a tiny race window in which enable_signaling is called during,
  132. * before, or after fence_signal. To fight this, it is recommended
  133. * that before enable_signaling returns true an extra reference is
  134. * taken on the fence, to be released when the fence is signaled.
  135. * This will mean fence_signal will still be called twice, but
  136. * the second time will be a noop since it was already signaled.
  137. *
  138. * Notes on signaled:
  139. * May set fence->error if returning true.
  140. *
  141. * Notes on wait:
  142. * Must not be NULL, set to fence_default_wait for default implementation.
  143. * the fence_default_wait implementation should work for any fence, as long
  144. * as enable_signaling works correctly.
  145. *
  146. * Must return -ERESTARTSYS if the wait is intr = true and the wait was
  147. * interrupted, and remaining jiffies if fence has signaled, or 0 if wait
  148. * timed out. Can also return other error values on custom implementations,
  149. * which should be treated as if the fence is signaled. For example a hardware
  150. * lockup could be reported like that.
  151. *
  152. * Notes on release:
  153. * Can be NULL, this function allows additional commands to run on
  154. * destruction of the fence. Can be called from irq context.
  155. * If pointer is set to NULL, kfree will get called instead.
  156. */
  157. struct fence_ops {
  158. const char * (*get_driver_name)(struct fence *fence);
  159. const char * (*get_timeline_name)(struct fence *fence);
  160. bool (*enable_signaling)(struct fence *fence);
  161. bool (*signaled)(struct fence *fence);
  162. signed long (*wait)(struct fence *fence, bool intr, signed long timeout);
  163. void (*release)(struct fence *fence);
  164. int (*fill_driver_data)(struct fence *fence, void *data, int size);
  165. void (*fence_value_str)(struct fence *fence, char *str, int size);
  166. void (*timeline_value_str)(struct fence *fence, char *str, int size);
  167. };
  168. void fence_init(struct fence *fence, const struct fence_ops *ops,
  169. spinlock_t *lock, u64 context, unsigned seqno);
  170. void fence_release(struct kref *kref);
  171. void fence_free(struct fence *fence);
  172. /**
  173. * fence_get - increases refcount of the fence
  174. * @fence: [in] fence to increase refcount of
  175. *
  176. * Returns the same fence, with refcount increased by 1.
  177. */
  178. static inline struct fence *fence_get(struct fence *fence)
  179. {
  180. if (fence)
  181. kref_get(&fence->refcount);
  182. return fence;
  183. }
  184. /**
  185. * fence_get_rcu - get a fence from a reservation_object_list with rcu read lock
  186. * @fence: [in] fence to increase refcount of
  187. *
  188. * Function returns NULL if no refcount could be obtained, or the fence.
  189. */
  190. static inline struct fence *fence_get_rcu(struct fence *fence)
  191. {
  192. if (kref_get_unless_zero(&fence->refcount))
  193. return fence;
  194. else
  195. return NULL;
  196. }
  197. /**
  198. * fence_put - decreases refcount of the fence
  199. * @fence: [in] fence to reduce refcount of
  200. */
  201. static inline void fence_put(struct fence *fence)
  202. {
  203. if (fence)
  204. kref_put(&fence->refcount, fence_release);
  205. }
  206. int fence_signal(struct fence *fence);
  207. int fence_signal_locked(struct fence *fence);
  208. signed long fence_default_wait(struct fence *fence, bool intr, signed long timeout);
  209. int fence_add_callback(struct fence *fence, struct fence_cb *cb,
  210. fence_func_t func);
  211. bool fence_remove_callback(struct fence *fence, struct fence_cb *cb);
  212. void fence_enable_sw_signaling(struct fence *fence);
  213. /**
  214. * fence_is_signaled_locked - Return an indication if the fence is signaled yet.
  215. * @fence: [in] the fence to check
  216. *
  217. * Returns true if the fence was already signaled, false if not. Since this
  218. * function doesn't enable signaling, it is not guaranteed to ever return
  219. * true if fence_add_callback, fence_wait or fence_enable_sw_signaling
  220. * haven't been called before.
  221. *
  222. * This function requires fence->lock to be held.
  223. */
  224. static inline bool
  225. fence_is_signaled_locked(struct fence *fence)
  226. {
  227. if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  228. return true;
  229. if (fence->ops->signaled && fence->ops->signaled(fence)) {
  230. fence_signal_locked(fence);
  231. return true;
  232. }
  233. return false;
  234. }
  235. /**
  236. * fence_is_signaled - Return an indication if the fence is signaled yet.
  237. * @fence: [in] the fence to check
  238. *
  239. * Returns true if the fence was already signaled, false if not. Since this
  240. * function doesn't enable signaling, it is not guaranteed to ever return
  241. * true if fence_add_callback, fence_wait or fence_enable_sw_signaling
  242. * haven't been called before.
  243. *
  244. * It's recommended for seqno fences to call fence_signal when the
  245. * operation is complete, it makes it possible to prevent issues from
  246. * wraparound between time of issue and time of use by checking the return
  247. * value of this function before calling hardware-specific wait instructions.
  248. */
  249. static inline bool
  250. fence_is_signaled(struct fence *fence)
  251. {
  252. if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  253. return true;
  254. if (fence->ops->signaled && fence->ops->signaled(fence)) {
  255. fence_signal(fence);
  256. return true;
  257. }
  258. return false;
  259. }
  260. /**
  261. * __fence_is_later - return if f1 is chronologically later than f2
  262. * @f1: [in] the first fence's seqno
  263. * @f2: [in] the second fence's seqno from the same context
  264. *
  265. * Returns true if f1 is chronologically later than f2. Both fences must be
  266. * from the same context, since a seqno is not common across contexts.
  267. */
  268. static inline bool __fence_is_later(u32 f1, u32 f2)
  269. {
  270. return (int)(f1 - f2) > 0;
  271. }
  272. /**
  273. * fence_is_later - return if f1 is chronologically later than f2
  274. * @f1: [in] the first fence from the same context
  275. * @f2: [in] the second fence from the same context
  276. *
  277. * Returns true if f1 is chronologically later than f2. Both fences must be
  278. * from the same context, since a seqno is not re-used across contexts.
  279. */
  280. static inline bool fence_is_later(struct fence *f1, struct fence *f2)
  281. {
  282. if (WARN_ON(f1->context != f2->context))
  283. return false;
  284. return __fence_is_later(f1->seqno, f2->seqno);
  285. }
  286. /**
  287. * fence_later - return the chronologically later fence
  288. * @f1: [in] the first fence from the same context
  289. * @f2: [in] the second fence from the same context
  290. *
  291. * Returns NULL if both fences are signaled, otherwise the fence that would be
  292. * signaled last. Both fences must be from the same context, since a seqno is
  293. * not re-used across contexts.
  294. */
  295. static inline struct fence *fence_later(struct fence *f1, struct fence *f2)
  296. {
  297. if (WARN_ON(f1->context != f2->context))
  298. return NULL;
  299. /*
  300. * can't check just FENCE_FLAG_SIGNALED_BIT here, it may never have been
  301. * set if enable_signaling wasn't called, and enabling that here is
  302. * overkill.
  303. */
  304. if (fence_is_later(f1, f2))
  305. return fence_is_signaled(f1) ? NULL : f1;
  306. else
  307. return fence_is_signaled(f2) ? NULL : f2;
  308. }
  309. /**
  310. * fence_get_status_locked - returns the status upon completion
  311. * @fence: [in] the fence to query
  312. *
  313. * Drivers can supply an optional error status condition before they signal
  314. * the fence (to indicate whether the fence was completed due to an error
  315. * rather than success). The value of the status condition is only valid
  316. * if the fence has been signaled, fence_get_status_locked() first checks
  317. * the signal state before reporting the error status.
  318. *
  319. * Returns 0 if the fence has not yet been signaled, 1 if the fence has
  320. * been signaled without an error condition, or a negative error code
  321. * if the fence has been completed in err.
  322. */
  323. static inline int fence_get_status_locked(struct fence *fence)
  324. {
  325. if (fence_is_signaled_locked(fence))
  326. return fence->error ?: 1;
  327. else
  328. return 0;
  329. }
  330. int fence_get_status(struct fence *fence);
  331. /**
  332. * fence_set_error - flag an error condition on the fence
  333. * @fence: [in] the fence
  334. * @error: [in] the error to store
  335. *
  336. * Drivers can supply an optional error status condition before they signal
  337. * the fence, to indicate that the fence was completed due to an error
  338. * rather than success. This must be set before signaling (so that the value
  339. * is visible before any waiters on the signal callback are woken). This
  340. * helper exists to help catching erroneous setting of #fence.error.
  341. */
  342. static inline void fence_set_error(struct fence *fence,
  343. int error)
  344. {
  345. BUG_ON(test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags));
  346. BUG_ON(error >= 0 || error < -MAX_ERRNO);
  347. fence->error = error;
  348. }
  349. signed long fence_wait_timeout(struct fence *, bool intr, signed long timeout);
  350. signed long fence_wait_any_timeout(struct fence **fences, uint32_t count,
  351. bool intr, signed long timeout);
  352. /**
  353. * fence_wait - sleep until the fence gets signaled
  354. * @fence: [in] the fence to wait on
  355. * @intr: [in] if true, do an interruptible wait
  356. *
  357. * This function will return -ERESTARTSYS if interrupted by a signal,
  358. * or 0 if the fence was signaled. Other error values may be
  359. * returned on custom implementations.
  360. *
  361. * Performs a synchronous wait on this fence. It is assumed the caller
  362. * directly or indirectly holds a reference to the fence, otherwise the
  363. * fence might be freed before return, resulting in undefined behavior.
  364. */
  365. static inline signed long fence_wait(struct fence *fence, bool intr)
  366. {
  367. signed long ret;
  368. /* Since fence_wait_timeout cannot timeout with
  369. * MAX_SCHEDULE_TIMEOUT, only valid return values are
  370. * -ERESTARTSYS and MAX_SCHEDULE_TIMEOUT.
  371. */
  372. ret = fence_wait_timeout(fence, intr, MAX_SCHEDULE_TIMEOUT);
  373. return ret < 0 ? ret : 0;
  374. }
  375. u64 fence_context_alloc(unsigned num);
  376. #define FENCE_TRACE(f, fmt, args...) \
  377. do { \
  378. struct fence *__ff = (f); \
  379. if (IS_ENABLED(CONFIG_FENCE_TRACE)) \
  380. pr_info("f %llu#%u: " fmt, \
  381. __ff->context, __ff->seqno, ##args); \
  382. } while (0)
  383. #define FENCE_WARN(f, fmt, args...) \
  384. do { \
  385. struct fence *__ff = (f); \
  386. pr_warn("f %llu#%u: " fmt, __ff->context, __ff->seqno, \
  387. ##args); \
  388. } while (0)
  389. #define FENCE_ERR(f, fmt, args...) \
  390. do { \
  391. struct fence *__ff = (f); \
  392. pr_err("f %llu#%u: " fmt, __ff->context, __ff->seqno, \
  393. ##args); \
  394. } while (0)
  395. #endif /* __LINUX_FENCE_H */