fence.h 13 KB

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