fence.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. trace_fence_wait_start(fence);
  142. ret = fence->ops->wait(fence, intr, timeout);
  143. trace_fence_wait_end(fence);
  144. return ret;
  145. }
  146. EXPORT_SYMBOL(fence_wait_timeout);
  147. void fence_release(struct kref *kref)
  148. {
  149. struct fence *fence =
  150. container_of(kref, struct fence, refcount);
  151. trace_fence_destroy(fence);
  152. BUG_ON(!list_empty(&fence->cb_list));
  153. if (fence->ops->release)
  154. fence->ops->release(fence);
  155. else
  156. fence_free(fence);
  157. }
  158. EXPORT_SYMBOL(fence_release);
  159. void fence_free(struct fence *fence)
  160. {
  161. kfree_rcu(fence, rcu);
  162. }
  163. EXPORT_SYMBOL(fence_free);
  164. /**
  165. * fence_enable_sw_signaling - enable signaling on fence
  166. * @fence: [in] the fence to enable
  167. *
  168. * this will request for sw signaling to be enabled, to make the fence
  169. * complete as soon as possible
  170. */
  171. void fence_enable_sw_signaling(struct fence *fence)
  172. {
  173. unsigned long flags;
  174. if (!test_and_set_bit(FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags) &&
  175. !test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
  176. trace_fence_enable_signal(fence);
  177. spin_lock_irqsave(fence->lock, flags);
  178. if (!fence->ops->enable_signaling(fence))
  179. fence_signal_locked(fence);
  180. spin_unlock_irqrestore(fence->lock, flags);
  181. }
  182. }
  183. EXPORT_SYMBOL(fence_enable_sw_signaling);
  184. /**
  185. * fence_add_callback - add a callback to be called when the fence
  186. * is signaled
  187. * @fence: [in] the fence to wait on
  188. * @cb: [in] the callback to register
  189. * @func: [in] the function to call
  190. *
  191. * cb will be initialized by fence_add_callback, no initialization
  192. * by the caller is required. Any number of callbacks can be registered
  193. * to a fence, but a callback can only be registered to one fence at a time.
  194. *
  195. * Note that the callback can be called from an atomic context. If
  196. * fence is already signaled, this function will return -ENOENT (and
  197. * *not* call the callback)
  198. *
  199. * Add a software callback to the fence. Same restrictions apply to
  200. * refcount as it does to fence_wait, however the caller doesn't need to
  201. * keep a refcount to fence afterwards: when software access is enabled,
  202. * the creator of the fence is required to keep the fence alive until
  203. * after it signals with fence_signal. The callback itself can be called
  204. * from irq context.
  205. *
  206. */
  207. int fence_add_callback(struct fence *fence, struct fence_cb *cb,
  208. fence_func_t func)
  209. {
  210. unsigned long flags;
  211. int ret = 0;
  212. bool was_set;
  213. if (WARN_ON(!fence || !func))
  214. return -EINVAL;
  215. if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
  216. INIT_LIST_HEAD(&cb->node);
  217. return -ENOENT;
  218. }
  219. spin_lock_irqsave(fence->lock, flags);
  220. was_set = test_and_set_bit(FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags);
  221. if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  222. ret = -ENOENT;
  223. else if (!was_set) {
  224. trace_fence_enable_signal(fence);
  225. if (!fence->ops->enable_signaling(fence)) {
  226. fence_signal_locked(fence);
  227. ret = -ENOENT;
  228. }
  229. }
  230. if (!ret) {
  231. cb->func = func;
  232. list_add_tail(&cb->node, &fence->cb_list);
  233. } else
  234. INIT_LIST_HEAD(&cb->node);
  235. spin_unlock_irqrestore(fence->lock, flags);
  236. return ret;
  237. }
  238. EXPORT_SYMBOL(fence_add_callback);
  239. /**
  240. * fence_remove_callback - remove a callback from the signaling list
  241. * @fence: [in] the fence to wait on
  242. * @cb: [in] the callback to remove
  243. *
  244. * Remove a previously queued callback from the fence. This function returns
  245. * true if the callback is succesfully removed, or false if the fence has
  246. * already been signaled.
  247. *
  248. * *WARNING*:
  249. * Cancelling a callback should only be done if you really know what you're
  250. * doing, since deadlocks and race conditions could occur all too easily. For
  251. * this reason, it should only ever be done on hardware lockup recovery,
  252. * with a reference held to the fence.
  253. */
  254. bool
  255. fence_remove_callback(struct fence *fence, struct fence_cb *cb)
  256. {
  257. unsigned long flags;
  258. bool ret;
  259. spin_lock_irqsave(fence->lock, flags);
  260. ret = !list_empty(&cb->node);
  261. if (ret)
  262. list_del_init(&cb->node);
  263. spin_unlock_irqrestore(fence->lock, flags);
  264. return ret;
  265. }
  266. EXPORT_SYMBOL(fence_remove_callback);
  267. struct default_wait_cb {
  268. struct fence_cb base;
  269. struct task_struct *task;
  270. };
  271. static void
  272. fence_default_wait_cb(struct fence *fence, struct fence_cb *cb)
  273. {
  274. struct default_wait_cb *wait =
  275. container_of(cb, struct default_wait_cb, base);
  276. wake_up_state(wait->task, TASK_NORMAL);
  277. }
  278. /**
  279. * fence_default_wait - default sleep until the fence gets signaled
  280. * or until timeout elapses
  281. * @fence: [in] the fence to wait on
  282. * @intr: [in] if true, do an interruptible wait
  283. * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
  284. *
  285. * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
  286. * remaining timeout in jiffies on success.
  287. */
  288. signed long
  289. fence_default_wait(struct fence *fence, bool intr, signed long timeout)
  290. {
  291. struct default_wait_cb cb;
  292. unsigned long flags;
  293. signed long ret = timeout;
  294. bool was_set;
  295. if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  296. return timeout;
  297. spin_lock_irqsave(fence->lock, flags);
  298. if (intr && signal_pending(current)) {
  299. ret = -ERESTARTSYS;
  300. goto out;
  301. }
  302. was_set = test_and_set_bit(FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags);
  303. if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  304. goto out;
  305. if (!was_set) {
  306. trace_fence_enable_signal(fence);
  307. if (!fence->ops->enable_signaling(fence)) {
  308. fence_signal_locked(fence);
  309. goto out;
  310. }
  311. }
  312. cb.base.func = fence_default_wait_cb;
  313. cb.task = current;
  314. list_add(&cb.base.node, &fence->cb_list);
  315. while (!test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags) && ret > 0) {
  316. if (intr)
  317. __set_current_state(TASK_INTERRUPTIBLE);
  318. else
  319. __set_current_state(TASK_UNINTERRUPTIBLE);
  320. spin_unlock_irqrestore(fence->lock, flags);
  321. ret = schedule_timeout(ret);
  322. spin_lock_irqsave(fence->lock, flags);
  323. if (ret > 0 && intr && signal_pending(current))
  324. ret = -ERESTARTSYS;
  325. }
  326. if (!list_empty(&cb.base.node))
  327. list_del(&cb.base.node);
  328. __set_current_state(TASK_RUNNING);
  329. out:
  330. spin_unlock_irqrestore(fence->lock, flags);
  331. return ret;
  332. }
  333. EXPORT_SYMBOL(fence_default_wait);
  334. /**
  335. * fence_init - Initialize a custom fence.
  336. * @fence: [in] the fence to initialize
  337. * @ops: [in] the fence_ops for operations on this fence
  338. * @lock: [in] the irqsafe spinlock to use for locking this fence
  339. * @context: [in] the execution context this fence is run on
  340. * @seqno: [in] a linear increasing sequence number for this context
  341. *
  342. * Initializes an allocated fence, the caller doesn't have to keep its
  343. * refcount after committing with this fence, but it will need to hold a
  344. * refcount again if fence_ops.enable_signaling gets called. This can
  345. * be used for other implementing other types of fence.
  346. *
  347. * context and seqno are used for easy comparison between fences, allowing
  348. * to check which fence is later by simply using fence_later.
  349. */
  350. void
  351. fence_init(struct fence *fence, const struct fence_ops *ops,
  352. spinlock_t *lock, unsigned context, unsigned seqno)
  353. {
  354. BUG_ON(!lock);
  355. BUG_ON(!ops || !ops->wait || !ops->enable_signaling ||
  356. !ops->get_driver_name || !ops->get_timeline_name);
  357. kref_init(&fence->refcount);
  358. fence->ops = ops;
  359. INIT_LIST_HEAD(&fence->cb_list);
  360. fence->lock = lock;
  361. fence->context = context;
  362. fence->seqno = seqno;
  363. fence->flags = 0UL;
  364. trace_fence_init(fence);
  365. }
  366. EXPORT_SYMBOL(fence_init);