fence.h 13 KB

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