fence.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * Fence mechanism for dma-buf and 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. #include <linux/slab.h>
  21. #include <linux/export.h>
  22. #include <linux/atomic.h>
  23. #include <linux/fence.h>
  24. #define CREATE_TRACE_POINTS
  25. #include <trace/events/fence.h>
  26. EXPORT_TRACEPOINT_SYMBOL(fence_annotate_wait_on);
  27. EXPORT_TRACEPOINT_SYMBOL(fence_emit);
  28. /*
  29. * fence context counter: each execution context should have its own
  30. * fence context, this allows checking if fences belong to the same
  31. * context or not. One device can have multiple separate contexts,
  32. * and they're used if some engine can run independently of another.
  33. */
  34. static atomic_t fence_context_counter = ATOMIC_INIT(0);
  35. /**
  36. * fence_context_alloc - allocate an array of fence contexts
  37. * @num: [in] amount of contexts to allocate
  38. *
  39. * This function will return the first index of the number of fences allocated.
  40. * The fence context is used for setting fence->context to a unique number.
  41. */
  42. unsigned fence_context_alloc(unsigned num)
  43. {
  44. BUG_ON(!num);
  45. return atomic_add_return(num, &fence_context_counter) - num;
  46. }
  47. EXPORT_SYMBOL(fence_context_alloc);
  48. /**
  49. * fence_signal_locked - signal completion of a fence
  50. * @fence: the fence to signal
  51. *
  52. * Signal completion for software callbacks on a fence, this will unblock
  53. * fence_wait() calls and run all the callbacks added with
  54. * fence_add_callback(). Can be called multiple times, but since a fence
  55. * can only go from unsignaled to signaled state, it will only be effective
  56. * the first time.
  57. *
  58. * Unlike fence_signal, this function must be called with fence->lock held.
  59. */
  60. int fence_signal_locked(struct fence *fence)
  61. {
  62. struct fence_cb *cur, *tmp;
  63. int ret = 0;
  64. if (WARN_ON(!fence))
  65. return -EINVAL;
  66. if (!ktime_to_ns(fence->timestamp)) {
  67. fence->timestamp = ktime_get();
  68. smp_mb__before_atomic();
  69. }
  70. if (test_and_set_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
  71. ret = -EINVAL;
  72. /*
  73. * we might have raced with the unlocked fence_signal,
  74. * still run through all callbacks
  75. */
  76. } else
  77. trace_fence_signaled(fence);
  78. list_for_each_entry_safe(cur, tmp, &fence->cb_list, node) {
  79. list_del_init(&cur->node);
  80. cur->func(fence, cur);
  81. }
  82. return ret;
  83. }
  84. EXPORT_SYMBOL(fence_signal_locked);
  85. /**
  86. * fence_signal - signal completion of a fence
  87. * @fence: the fence to signal
  88. *
  89. * Signal completion for software callbacks on a fence, this will unblock
  90. * fence_wait() calls and run all the callbacks added with
  91. * fence_add_callback(). Can be called multiple times, but since a fence
  92. * can only go from unsignaled to signaled state, it will only be effective
  93. * the first time.
  94. */
  95. int fence_signal(struct fence *fence)
  96. {
  97. unsigned long flags;
  98. if (!fence)
  99. return -EINVAL;
  100. if (!ktime_to_ns(fence->timestamp)) {
  101. fence->timestamp = ktime_get();
  102. smp_mb__before_atomic();
  103. }
  104. if (test_and_set_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  105. return -EINVAL;
  106. trace_fence_signaled(fence);
  107. if (test_bit(FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags)) {
  108. struct fence_cb *cur, *tmp;
  109. spin_lock_irqsave(fence->lock, flags);
  110. list_for_each_entry_safe(cur, tmp, &fence->cb_list, node) {
  111. list_del_init(&cur->node);
  112. cur->func(fence, cur);
  113. }
  114. spin_unlock_irqrestore(fence->lock, flags);
  115. }
  116. return 0;
  117. }
  118. EXPORT_SYMBOL(fence_signal);
  119. /**
  120. * fence_wait_timeout - sleep until the fence gets signaled
  121. * or until timeout elapses
  122. * @fence: [in] the fence to wait on
  123. * @intr: [in] if true, do an interruptible wait
  124. * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
  125. *
  126. * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
  127. * remaining timeout in jiffies on success. Other error values may be
  128. * returned on custom implementations.
  129. *
  130. * Performs a synchronous wait on this fence. It is assumed the caller
  131. * directly or indirectly (buf-mgr between reservation and committing)
  132. * holds a reference to the fence, otherwise the fence might be
  133. * freed before return, resulting in undefined behavior.
  134. */
  135. signed long
  136. fence_wait_timeout(struct fence *fence, bool intr, signed long timeout)
  137. {
  138. signed long ret;
  139. if (WARN_ON(timeout < 0))
  140. return -EINVAL;
  141. if (timeout == 0)
  142. return fence_is_signaled(fence);
  143. trace_fence_wait_start(fence);
  144. ret = fence->ops->wait(fence, intr, timeout);
  145. trace_fence_wait_end(fence);
  146. return ret;
  147. }
  148. EXPORT_SYMBOL(fence_wait_timeout);
  149. void fence_release(struct kref *kref)
  150. {
  151. struct fence *fence =
  152. container_of(kref, struct fence, refcount);
  153. trace_fence_destroy(fence);
  154. BUG_ON(!list_empty(&fence->cb_list));
  155. if (fence->ops->release)
  156. fence->ops->release(fence);
  157. else
  158. fence_free(fence);
  159. }
  160. EXPORT_SYMBOL(fence_release);
  161. void fence_free(struct fence *fence)
  162. {
  163. kfree_rcu(fence, rcu);
  164. }
  165. EXPORT_SYMBOL(fence_free);
  166. /**
  167. * fence_enable_sw_signaling - enable signaling on fence
  168. * @fence: [in] the fence to enable
  169. *
  170. * this will request for sw signaling to be enabled, to make the fence
  171. * complete as soon as possible
  172. */
  173. void fence_enable_sw_signaling(struct fence *fence)
  174. {
  175. unsigned long flags;
  176. if (!test_and_set_bit(FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags) &&
  177. !test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
  178. trace_fence_enable_signal(fence);
  179. spin_lock_irqsave(fence->lock, flags);
  180. if (!fence->ops->enable_signaling(fence))
  181. fence_signal_locked(fence);
  182. spin_unlock_irqrestore(fence->lock, flags);
  183. }
  184. }
  185. EXPORT_SYMBOL(fence_enable_sw_signaling);
  186. /**
  187. * fence_add_callback - add a callback to be called when the fence
  188. * is signaled
  189. * @fence: [in] the fence to wait on
  190. * @cb: [in] the callback to register
  191. * @func: [in] the function to call
  192. *
  193. * cb will be initialized by fence_add_callback, no initialization
  194. * by the caller is required. Any number of callbacks can be registered
  195. * to a fence, but a callback can only be registered to one fence at a time.
  196. *
  197. * Note that the callback can be called from an atomic context. If
  198. * fence is already signaled, this function will return -ENOENT (and
  199. * *not* call the callback)
  200. *
  201. * Add a software callback to the fence. Same restrictions apply to
  202. * refcount as it does to fence_wait, however the caller doesn't need to
  203. * keep a refcount to fence afterwards: when software access is enabled,
  204. * the creator of the fence is required to keep the fence alive until
  205. * after it signals with fence_signal. The callback itself can be called
  206. * from irq context.
  207. *
  208. */
  209. int fence_add_callback(struct fence *fence, struct fence_cb *cb,
  210. fence_func_t func)
  211. {
  212. unsigned long flags;
  213. int ret = 0;
  214. bool was_set;
  215. if (WARN_ON(!fence || !func))
  216. return -EINVAL;
  217. if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
  218. INIT_LIST_HEAD(&cb->node);
  219. return -ENOENT;
  220. }
  221. spin_lock_irqsave(fence->lock, flags);
  222. was_set = test_and_set_bit(FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags);
  223. if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  224. ret = -ENOENT;
  225. else if (!was_set) {
  226. trace_fence_enable_signal(fence);
  227. if (!fence->ops->enable_signaling(fence)) {
  228. fence_signal_locked(fence);
  229. ret = -ENOENT;
  230. }
  231. }
  232. if (!ret) {
  233. cb->func = func;
  234. list_add_tail(&cb->node, &fence->cb_list);
  235. } else
  236. INIT_LIST_HEAD(&cb->node);
  237. spin_unlock_irqrestore(fence->lock, flags);
  238. return ret;
  239. }
  240. EXPORT_SYMBOL(fence_add_callback);
  241. /**
  242. * fence_remove_callback - remove a callback from the signaling list
  243. * @fence: [in] the fence to wait on
  244. * @cb: [in] the callback to remove
  245. *
  246. * Remove a previously queued callback from the fence. This function returns
  247. * true if the callback is successfully removed, or false if the fence has
  248. * already been signaled.
  249. *
  250. * *WARNING*:
  251. * Cancelling a callback should only be done if you really know what you're
  252. * doing, since deadlocks and race conditions could occur all too easily. For
  253. * this reason, it should only ever be done on hardware lockup recovery,
  254. * with a reference held to the fence.
  255. */
  256. bool
  257. fence_remove_callback(struct fence *fence, struct fence_cb *cb)
  258. {
  259. unsigned long flags;
  260. bool ret;
  261. spin_lock_irqsave(fence->lock, flags);
  262. ret = !list_empty(&cb->node);
  263. if (ret)
  264. list_del_init(&cb->node);
  265. spin_unlock_irqrestore(fence->lock, flags);
  266. return ret;
  267. }
  268. EXPORT_SYMBOL(fence_remove_callback);
  269. struct default_wait_cb {
  270. struct fence_cb base;
  271. struct task_struct *task;
  272. };
  273. static void
  274. fence_default_wait_cb(struct fence *fence, struct fence_cb *cb)
  275. {
  276. struct default_wait_cb *wait =
  277. container_of(cb, struct default_wait_cb, base);
  278. wake_up_state(wait->task, TASK_NORMAL);
  279. }
  280. /**
  281. * fence_default_wait - default sleep until the fence gets signaled
  282. * or until timeout elapses
  283. * @fence: [in] the fence to wait on
  284. * @intr: [in] if true, do an interruptible wait
  285. * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
  286. *
  287. * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
  288. * remaining timeout in jiffies on success.
  289. */
  290. signed long
  291. fence_default_wait(struct fence *fence, bool intr, signed long timeout)
  292. {
  293. struct default_wait_cb cb;
  294. unsigned long flags;
  295. signed long ret = timeout;
  296. bool was_set;
  297. if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  298. return timeout;
  299. spin_lock_irqsave(fence->lock, flags);
  300. if (intr && signal_pending(current)) {
  301. ret = -ERESTARTSYS;
  302. goto out;
  303. }
  304. was_set = test_and_set_bit(FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags);
  305. if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  306. goto out;
  307. if (!was_set) {
  308. trace_fence_enable_signal(fence);
  309. if (!fence->ops->enable_signaling(fence)) {
  310. fence_signal_locked(fence);
  311. goto out;
  312. }
  313. }
  314. cb.base.func = fence_default_wait_cb;
  315. cb.task = current;
  316. list_add(&cb.base.node, &fence->cb_list);
  317. while (!test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags) && ret > 0) {
  318. if (intr)
  319. __set_current_state(TASK_INTERRUPTIBLE);
  320. else
  321. __set_current_state(TASK_UNINTERRUPTIBLE);
  322. spin_unlock_irqrestore(fence->lock, flags);
  323. ret = schedule_timeout(ret);
  324. spin_lock_irqsave(fence->lock, flags);
  325. if (ret > 0 && intr && signal_pending(current))
  326. ret = -ERESTARTSYS;
  327. }
  328. if (!list_empty(&cb.base.node))
  329. list_del(&cb.base.node);
  330. __set_current_state(TASK_RUNNING);
  331. out:
  332. spin_unlock_irqrestore(fence->lock, flags);
  333. return ret;
  334. }
  335. EXPORT_SYMBOL(fence_default_wait);
  336. /**
  337. * fence_init - Initialize a custom fence.
  338. * @fence: [in] the fence to initialize
  339. * @ops: [in] the fence_ops for operations on this fence
  340. * @lock: [in] the irqsafe spinlock to use for locking this fence
  341. * @context: [in] the execution context this fence is run on
  342. * @seqno: [in] a linear increasing sequence number for this context
  343. *
  344. * Initializes an allocated fence, the caller doesn't have to keep its
  345. * refcount after committing with this fence, but it will need to hold a
  346. * refcount again if fence_ops.enable_signaling gets called. This can
  347. * be used for other implementing other types of fence.
  348. *
  349. * context and seqno are used for easy comparison between fences, allowing
  350. * to check which fence is later by simply using fence_later.
  351. */
  352. void
  353. fence_init(struct fence *fence, const struct fence_ops *ops,
  354. spinlock_t *lock, unsigned context, unsigned seqno)
  355. {
  356. BUG_ON(!lock);
  357. BUG_ON(!ops || !ops->wait || !ops->enable_signaling ||
  358. !ops->get_driver_name || !ops->get_timeline_name);
  359. kref_init(&fence->refcount);
  360. fence->ops = ops;
  361. INIT_LIST_HEAD(&fence->cb_list);
  362. fence->lock = lock;
  363. fence->context = context;
  364. fence->seqno = seqno;
  365. fence->flags = 0UL;
  366. trace_fence_init(fence);
  367. }
  368. EXPORT_SYMBOL(fence_init);