fence.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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 atomic64_t fence_context_counter = ATOMIC64_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. u64 fence_context_alloc(unsigned num)
  43. {
  44. BUG_ON(!num);
  45. return atomic64_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_get_status - returns the status upon completion
  243. * @fence: [in] the fence to query
  244. *
  245. * This wraps fence_get_status_locked() to return the error status
  246. * condition on a signaled fence. See fence_get_status_locked() for more
  247. * details.
  248. *
  249. * Returns 0 if the fence has not yet been signaled, 1 if the fence has
  250. * been signaled without an error condition, or a negative error code
  251. * if the fence has been completed in err.
  252. */
  253. int fence_get_status(struct fence *fence)
  254. {
  255. unsigned long flags;
  256. int status;
  257. spin_lock_irqsave(fence->lock, flags);
  258. status = fence_get_status_locked(fence);
  259. spin_unlock_irqrestore(fence->lock, flags);
  260. return status;
  261. }
  262. EXPORT_SYMBOL(fence_get_status);
  263. /**
  264. * fence_remove_callback - remove a callback from the signaling list
  265. * @fence: [in] the fence to wait on
  266. * @cb: [in] the callback to remove
  267. *
  268. * Remove a previously queued callback from the fence. This function returns
  269. * true if the callback is successfully removed, or false if the fence has
  270. * already been signaled.
  271. *
  272. * *WARNING*:
  273. * Cancelling a callback should only be done if you really know what you're
  274. * doing, since deadlocks and race conditions could occur all too easily. For
  275. * this reason, it should only ever be done on hardware lockup recovery,
  276. * with a reference held to the fence.
  277. */
  278. bool
  279. fence_remove_callback(struct fence *fence, struct fence_cb *cb)
  280. {
  281. unsigned long flags;
  282. bool ret;
  283. spin_lock_irqsave(fence->lock, flags);
  284. ret = !list_empty(&cb->node);
  285. if (ret)
  286. list_del_init(&cb->node);
  287. spin_unlock_irqrestore(fence->lock, flags);
  288. return ret;
  289. }
  290. EXPORT_SYMBOL(fence_remove_callback);
  291. struct default_wait_cb {
  292. struct fence_cb base;
  293. struct task_struct *task;
  294. };
  295. static void
  296. fence_default_wait_cb(struct fence *fence, struct fence_cb *cb)
  297. {
  298. struct default_wait_cb *wait =
  299. container_of(cb, struct default_wait_cb, base);
  300. wake_up_state(wait->task, TASK_NORMAL);
  301. }
  302. /**
  303. * fence_default_wait - default sleep until the fence gets signaled
  304. * or until timeout elapses
  305. * @fence: [in] the fence to wait on
  306. * @intr: [in] if true, do an interruptible wait
  307. * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
  308. *
  309. * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
  310. * remaining timeout in jiffies on success.
  311. */
  312. signed long
  313. fence_default_wait(struct fence *fence, bool intr, signed long timeout)
  314. {
  315. struct default_wait_cb cb;
  316. unsigned long flags;
  317. signed long ret = timeout;
  318. bool was_set;
  319. if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  320. return timeout;
  321. spin_lock_irqsave(fence->lock, flags);
  322. if (intr && signal_pending(current)) {
  323. ret = -ERESTARTSYS;
  324. goto out;
  325. }
  326. was_set = test_and_set_bit(FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags);
  327. if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  328. goto out;
  329. if (!was_set) {
  330. trace_fence_enable_signal(fence);
  331. if (!fence->ops->enable_signaling(fence)) {
  332. fence_signal_locked(fence);
  333. goto out;
  334. }
  335. }
  336. cb.base.func = fence_default_wait_cb;
  337. cb.task = current;
  338. list_add(&cb.base.node, &fence->cb_list);
  339. while (!test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags) && ret > 0) {
  340. if (intr)
  341. __set_current_state(TASK_INTERRUPTIBLE);
  342. else
  343. __set_current_state(TASK_UNINTERRUPTIBLE);
  344. spin_unlock_irqrestore(fence->lock, flags);
  345. ret = schedule_timeout(ret);
  346. spin_lock_irqsave(fence->lock, flags);
  347. if (ret > 0 && intr && signal_pending(current))
  348. ret = -ERESTARTSYS;
  349. }
  350. if (!list_empty(&cb.base.node))
  351. list_del(&cb.base.node);
  352. __set_current_state(TASK_RUNNING);
  353. out:
  354. spin_unlock_irqrestore(fence->lock, flags);
  355. return ret;
  356. }
  357. EXPORT_SYMBOL(fence_default_wait);
  358. static bool
  359. fence_test_signaled_any(struct fence **fences, uint32_t count)
  360. {
  361. int i;
  362. for (i = 0; i < count; ++i) {
  363. struct fence *fence = fences[i];
  364. if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  365. return true;
  366. }
  367. return false;
  368. }
  369. /**
  370. * fence_wait_any_timeout - sleep until any fence gets signaled
  371. * or until timeout elapses
  372. * @fences: [in] array of fences to wait on
  373. * @count: [in] number of fences to wait on
  374. * @intr: [in] if true, do an interruptible wait
  375. * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
  376. *
  377. * Returns -EINVAL on custom fence wait implementation, -ERESTARTSYS if
  378. * interrupted, 0 if the wait timed out, or the remaining timeout in jiffies
  379. * on success.
  380. *
  381. * Synchronous waits for the first fence in the array to be signaled. The
  382. * caller needs to hold a reference to all fences in the array, otherwise a
  383. * fence might be freed before return, resulting in undefined behavior.
  384. */
  385. signed long
  386. fence_wait_any_timeout(struct fence **fences, uint32_t count,
  387. bool intr, signed long timeout)
  388. {
  389. struct default_wait_cb *cb;
  390. signed long ret = timeout;
  391. unsigned i;
  392. if (WARN_ON(!fences || !count || timeout < 0))
  393. return -EINVAL;
  394. if (timeout == 0) {
  395. for (i = 0; i < count; ++i)
  396. if (fence_is_signaled(fences[i]))
  397. return 1;
  398. return 0;
  399. }
  400. cb = kcalloc(count, sizeof(struct default_wait_cb), GFP_KERNEL);
  401. if (cb == NULL) {
  402. ret = -ENOMEM;
  403. goto err_free_cb;
  404. }
  405. for (i = 0; i < count; ++i) {
  406. struct fence *fence = fences[i];
  407. if (fence->ops->wait != fence_default_wait) {
  408. ret = -EINVAL;
  409. goto fence_rm_cb;
  410. }
  411. cb[i].task = current;
  412. if (fence_add_callback(fence, &cb[i].base,
  413. fence_default_wait_cb)) {
  414. /* This fence is already signaled */
  415. goto fence_rm_cb;
  416. }
  417. }
  418. while (ret > 0) {
  419. if (intr)
  420. set_current_state(TASK_INTERRUPTIBLE);
  421. else
  422. set_current_state(TASK_UNINTERRUPTIBLE);
  423. if (fence_test_signaled_any(fences, count))
  424. break;
  425. ret = schedule_timeout(ret);
  426. if (ret > 0 && intr && signal_pending(current))
  427. ret = -ERESTARTSYS;
  428. }
  429. __set_current_state(TASK_RUNNING);
  430. fence_rm_cb:
  431. while (i-- > 0)
  432. fence_remove_callback(fences[i], &cb[i].base);
  433. err_free_cb:
  434. kfree(cb);
  435. return ret;
  436. }
  437. EXPORT_SYMBOL(fence_wait_any_timeout);
  438. /**
  439. * fence_init - Initialize a custom fence.
  440. * @fence: [in] the fence to initialize
  441. * @ops: [in] the fence_ops for operations on this fence
  442. * @lock: [in] the irqsafe spinlock to use for locking this fence
  443. * @context: [in] the execution context this fence is run on
  444. * @seqno: [in] a linear increasing sequence number for this context
  445. *
  446. * Initializes an allocated fence, the caller doesn't have to keep its
  447. * refcount after committing with this fence, but it will need to hold a
  448. * refcount again if fence_ops.enable_signaling gets called. This can
  449. * be used for other implementing other types of fence.
  450. *
  451. * context and seqno are used for easy comparison between fences, allowing
  452. * to check which fence is later by simply using fence_later.
  453. */
  454. void
  455. fence_init(struct fence *fence, const struct fence_ops *ops,
  456. spinlock_t *lock, u64 context, unsigned seqno)
  457. {
  458. BUG_ON(!lock);
  459. BUG_ON(!ops || !ops->wait || !ops->enable_signaling ||
  460. !ops->get_driver_name || !ops->get_timeline_name);
  461. kref_init(&fence->refcount);
  462. fence->ops = ops;
  463. INIT_LIST_HEAD(&fence->cb_list);
  464. fence->lock = lock;
  465. fence->context = context;
  466. fence->seqno = seqno;
  467. fence->flags = 0UL;
  468. fence->error = 0;
  469. trace_fence_init(fence);
  470. }
  471. EXPORT_SYMBOL(fence_init);