dma-fence.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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/dma-fence.h>
  24. #define CREATE_TRACE_POINTS
  25. #include <trace/events/dma_fence.h>
  26. EXPORT_TRACEPOINT_SYMBOL(dma_fence_annotate_wait_on);
  27. EXPORT_TRACEPOINT_SYMBOL(dma_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 dma_fence_context_counter = ATOMIC64_INIT(0);
  35. /**
  36. * dma_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 dma_fence_context_alloc(unsigned num)
  43. {
  44. BUG_ON(!num);
  45. return atomic64_add_return(num, &dma_fence_context_counter) - num;
  46. }
  47. EXPORT_SYMBOL(dma_fence_context_alloc);
  48. /**
  49. * dma_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. * dma_fence_wait() calls and run all the callbacks added with
  54. * dma_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 dma_fence_signal, this function must be called with fence->lock held.
  59. */
  60. int dma_fence_signal_locked(struct dma_fence *fence)
  61. {
  62. struct dma_fence_cb *cur, *tmp;
  63. int ret = 0;
  64. lockdep_assert_held(fence->lock);
  65. if (WARN_ON(!fence))
  66. return -EINVAL;
  67. if (!ktime_to_ns(fence->timestamp)) {
  68. fence->timestamp = ktime_get();
  69. smp_mb__before_atomic();
  70. }
  71. if (test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
  72. ret = -EINVAL;
  73. /*
  74. * we might have raced with the unlocked dma_fence_signal,
  75. * still run through all callbacks
  76. */
  77. } else
  78. trace_dma_fence_signaled(fence);
  79. list_for_each_entry_safe(cur, tmp, &fence->cb_list, node) {
  80. list_del_init(&cur->node);
  81. cur->func(fence, cur);
  82. }
  83. return ret;
  84. }
  85. EXPORT_SYMBOL(dma_fence_signal_locked);
  86. /**
  87. * dma_fence_signal - signal completion of a fence
  88. * @fence: the fence to signal
  89. *
  90. * Signal completion for software callbacks on a fence, this will unblock
  91. * dma_fence_wait() calls and run all the callbacks added with
  92. * dma_fence_add_callback(). Can be called multiple times, but since a fence
  93. * can only go from unsignaled to signaled state, it will only be effective
  94. * the first time.
  95. */
  96. int dma_fence_signal(struct dma_fence *fence)
  97. {
  98. unsigned long flags;
  99. if (!fence)
  100. return -EINVAL;
  101. if (!ktime_to_ns(fence->timestamp)) {
  102. fence->timestamp = ktime_get();
  103. smp_mb__before_atomic();
  104. }
  105. if (test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  106. return -EINVAL;
  107. trace_dma_fence_signaled(fence);
  108. if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags)) {
  109. struct dma_fence_cb *cur, *tmp;
  110. spin_lock_irqsave(fence->lock, flags);
  111. list_for_each_entry_safe(cur, tmp, &fence->cb_list, node) {
  112. list_del_init(&cur->node);
  113. cur->func(fence, cur);
  114. }
  115. spin_unlock_irqrestore(fence->lock, flags);
  116. }
  117. return 0;
  118. }
  119. EXPORT_SYMBOL(dma_fence_signal);
  120. /**
  121. * dma_fence_wait_timeout - sleep until the fence gets signaled
  122. * or until timeout elapses
  123. * @fence: [in] the fence to wait on
  124. * @intr: [in] if true, do an interruptible wait
  125. * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
  126. *
  127. * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
  128. * remaining timeout in jiffies on success. Other error values may be
  129. * returned on custom implementations.
  130. *
  131. * Performs a synchronous wait on this fence. It is assumed the caller
  132. * directly or indirectly (buf-mgr between reservation and committing)
  133. * holds a reference to the fence, otherwise the fence might be
  134. * freed before return, resulting in undefined behavior.
  135. */
  136. signed long
  137. dma_fence_wait_timeout(struct dma_fence *fence, bool intr, signed long timeout)
  138. {
  139. signed long ret;
  140. if (WARN_ON(timeout < 0))
  141. return -EINVAL;
  142. trace_dma_fence_wait_start(fence);
  143. ret = fence->ops->wait(fence, intr, timeout);
  144. trace_dma_fence_wait_end(fence);
  145. return ret;
  146. }
  147. EXPORT_SYMBOL(dma_fence_wait_timeout);
  148. void dma_fence_release(struct kref *kref)
  149. {
  150. struct dma_fence *fence =
  151. container_of(kref, struct dma_fence, refcount);
  152. trace_dma_fence_destroy(fence);
  153. BUG_ON(!list_empty(&fence->cb_list));
  154. if (fence->ops->release)
  155. fence->ops->release(fence);
  156. else
  157. dma_fence_free(fence);
  158. }
  159. EXPORT_SYMBOL(dma_fence_release);
  160. void dma_fence_free(struct dma_fence *fence)
  161. {
  162. kfree_rcu(fence, rcu);
  163. }
  164. EXPORT_SYMBOL(dma_fence_free);
  165. /**
  166. * dma_fence_enable_sw_signaling - enable signaling on fence
  167. * @fence: [in] the fence to enable
  168. *
  169. * this will request for sw signaling to be enabled, to make the fence
  170. * complete as soon as possible
  171. */
  172. void dma_fence_enable_sw_signaling(struct dma_fence *fence)
  173. {
  174. unsigned long flags;
  175. if (!test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
  176. &fence->flags) &&
  177. !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
  178. trace_dma_fence_enable_signal(fence);
  179. spin_lock_irqsave(fence->lock, flags);
  180. if (!fence->ops->enable_signaling(fence))
  181. dma_fence_signal_locked(fence);
  182. spin_unlock_irqrestore(fence->lock, flags);
  183. }
  184. }
  185. EXPORT_SYMBOL(dma_fence_enable_sw_signaling);
  186. /**
  187. * dma_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 dma_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 dma_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 dma_fence_signal. The callback itself can be called
  206. * from irq context.
  207. *
  208. */
  209. int dma_fence_add_callback(struct dma_fence *fence, struct dma_fence_cb *cb,
  210. dma_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(DMA_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(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
  223. &fence->flags);
  224. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  225. ret = -ENOENT;
  226. else if (!was_set) {
  227. trace_dma_fence_enable_signal(fence);
  228. if (!fence->ops->enable_signaling(fence)) {
  229. dma_fence_signal_locked(fence);
  230. ret = -ENOENT;
  231. }
  232. }
  233. if (!ret) {
  234. cb->func = func;
  235. list_add_tail(&cb->node, &fence->cb_list);
  236. } else
  237. INIT_LIST_HEAD(&cb->node);
  238. spin_unlock_irqrestore(fence->lock, flags);
  239. return ret;
  240. }
  241. EXPORT_SYMBOL(dma_fence_add_callback);
  242. /**
  243. * dma_fence_get_status - returns the status upon completion
  244. * @fence: [in] the dma_fence to query
  245. *
  246. * This wraps dma_fence_get_status_locked() to return the error status
  247. * condition on a signaled fence. See dma_fence_get_status_locked() for more
  248. * details.
  249. *
  250. * Returns 0 if the fence has not yet been signaled, 1 if the fence has
  251. * been signaled without an error condition, or a negative error code
  252. * if the fence has been completed in err.
  253. */
  254. int dma_fence_get_status(struct dma_fence *fence)
  255. {
  256. unsigned long flags;
  257. int status;
  258. spin_lock_irqsave(fence->lock, flags);
  259. status = dma_fence_get_status_locked(fence);
  260. spin_unlock_irqrestore(fence->lock, flags);
  261. return status;
  262. }
  263. EXPORT_SYMBOL(dma_fence_get_status);
  264. /**
  265. * dma_fence_remove_callback - remove a callback from the signaling list
  266. * @fence: [in] the fence to wait on
  267. * @cb: [in] the callback to remove
  268. *
  269. * Remove a previously queued callback from the fence. This function returns
  270. * true if the callback is successfully removed, or false if the fence has
  271. * already been signaled.
  272. *
  273. * *WARNING*:
  274. * Cancelling a callback should only be done if you really know what you're
  275. * doing, since deadlocks and race conditions could occur all too easily. For
  276. * this reason, it should only ever be done on hardware lockup recovery,
  277. * with a reference held to the fence.
  278. */
  279. bool
  280. dma_fence_remove_callback(struct dma_fence *fence, struct dma_fence_cb *cb)
  281. {
  282. unsigned long flags;
  283. bool ret;
  284. spin_lock_irqsave(fence->lock, flags);
  285. ret = !list_empty(&cb->node);
  286. if (ret)
  287. list_del_init(&cb->node);
  288. spin_unlock_irqrestore(fence->lock, flags);
  289. return ret;
  290. }
  291. EXPORT_SYMBOL(dma_fence_remove_callback);
  292. struct default_wait_cb {
  293. struct dma_fence_cb base;
  294. struct task_struct *task;
  295. };
  296. static void
  297. dma_fence_default_wait_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
  298. {
  299. struct default_wait_cb *wait =
  300. container_of(cb, struct default_wait_cb, base);
  301. wake_up_state(wait->task, TASK_NORMAL);
  302. }
  303. /**
  304. * dma_fence_default_wait - default sleep until the fence gets signaled
  305. * or until timeout elapses
  306. * @fence: [in] the fence to wait on
  307. * @intr: [in] if true, do an interruptible wait
  308. * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
  309. *
  310. * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
  311. * remaining timeout in jiffies on success. If timeout is zero the value one is
  312. * returned if the fence is already signaled for consistency with other
  313. * functions taking a jiffies timeout.
  314. */
  315. signed long
  316. dma_fence_default_wait(struct dma_fence *fence, bool intr, signed long timeout)
  317. {
  318. struct default_wait_cb cb;
  319. unsigned long flags;
  320. signed long ret = timeout ? timeout : 1;
  321. bool was_set;
  322. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  323. return ret;
  324. spin_lock_irqsave(fence->lock, flags);
  325. if (intr && signal_pending(current)) {
  326. ret = -ERESTARTSYS;
  327. goto out;
  328. }
  329. was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
  330. &fence->flags);
  331. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  332. goto out;
  333. if (!was_set) {
  334. trace_dma_fence_enable_signal(fence);
  335. if (!fence->ops->enable_signaling(fence)) {
  336. dma_fence_signal_locked(fence);
  337. goto out;
  338. }
  339. }
  340. cb.base.func = dma_fence_default_wait_cb;
  341. cb.task = current;
  342. list_add(&cb.base.node, &fence->cb_list);
  343. while (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags) && ret > 0) {
  344. if (intr)
  345. __set_current_state(TASK_INTERRUPTIBLE);
  346. else
  347. __set_current_state(TASK_UNINTERRUPTIBLE);
  348. spin_unlock_irqrestore(fence->lock, flags);
  349. ret = schedule_timeout(ret);
  350. spin_lock_irqsave(fence->lock, flags);
  351. if (ret > 0 && intr && signal_pending(current))
  352. ret = -ERESTARTSYS;
  353. }
  354. if (!list_empty(&cb.base.node))
  355. list_del(&cb.base.node);
  356. __set_current_state(TASK_RUNNING);
  357. out:
  358. spin_unlock_irqrestore(fence->lock, flags);
  359. return ret;
  360. }
  361. EXPORT_SYMBOL(dma_fence_default_wait);
  362. static bool
  363. dma_fence_test_signaled_any(struct dma_fence **fences, uint32_t count,
  364. uint32_t *idx)
  365. {
  366. int i;
  367. for (i = 0; i < count; ++i) {
  368. struct dma_fence *fence = fences[i];
  369. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
  370. if (idx)
  371. *idx = i;
  372. return true;
  373. }
  374. }
  375. return false;
  376. }
  377. /**
  378. * dma_fence_wait_any_timeout - sleep until any fence gets signaled
  379. * or until timeout elapses
  380. * @fences: [in] array of fences to wait on
  381. * @count: [in] number of fences to wait on
  382. * @intr: [in] if true, do an interruptible wait
  383. * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
  384. * @idx: [out] the first signaled fence index, meaningful only on
  385. * positive return
  386. *
  387. * Returns -EINVAL on custom fence wait implementation, -ERESTARTSYS if
  388. * interrupted, 0 if the wait timed out, or the remaining timeout in jiffies
  389. * on success.
  390. *
  391. * Synchronous waits for the first fence in the array to be signaled. The
  392. * caller needs to hold a reference to all fences in the array, otherwise a
  393. * fence might be freed before return, resulting in undefined behavior.
  394. */
  395. signed long
  396. dma_fence_wait_any_timeout(struct dma_fence **fences, uint32_t count,
  397. bool intr, signed long timeout, uint32_t *idx)
  398. {
  399. struct default_wait_cb *cb;
  400. signed long ret = timeout;
  401. unsigned i;
  402. if (WARN_ON(!fences || !count || timeout < 0))
  403. return -EINVAL;
  404. if (timeout == 0) {
  405. for (i = 0; i < count; ++i)
  406. if (dma_fence_is_signaled(fences[i])) {
  407. if (idx)
  408. *idx = i;
  409. return 1;
  410. }
  411. return 0;
  412. }
  413. cb = kcalloc(count, sizeof(struct default_wait_cb), GFP_KERNEL);
  414. if (cb == NULL) {
  415. ret = -ENOMEM;
  416. goto err_free_cb;
  417. }
  418. for (i = 0; i < count; ++i) {
  419. struct dma_fence *fence = fences[i];
  420. if (fence->ops->wait != dma_fence_default_wait) {
  421. ret = -EINVAL;
  422. goto fence_rm_cb;
  423. }
  424. cb[i].task = current;
  425. if (dma_fence_add_callback(fence, &cb[i].base,
  426. dma_fence_default_wait_cb)) {
  427. /* This fence is already signaled */
  428. if (idx)
  429. *idx = i;
  430. goto fence_rm_cb;
  431. }
  432. }
  433. while (ret > 0) {
  434. if (intr)
  435. set_current_state(TASK_INTERRUPTIBLE);
  436. else
  437. set_current_state(TASK_UNINTERRUPTIBLE);
  438. if (dma_fence_test_signaled_any(fences, count, idx))
  439. break;
  440. ret = schedule_timeout(ret);
  441. if (ret > 0 && intr && signal_pending(current))
  442. ret = -ERESTARTSYS;
  443. }
  444. __set_current_state(TASK_RUNNING);
  445. fence_rm_cb:
  446. while (i-- > 0)
  447. dma_fence_remove_callback(fences[i], &cb[i].base);
  448. err_free_cb:
  449. kfree(cb);
  450. return ret;
  451. }
  452. EXPORT_SYMBOL(dma_fence_wait_any_timeout);
  453. /**
  454. * dma_fence_init - Initialize a custom fence.
  455. * @fence: [in] the fence to initialize
  456. * @ops: [in] the dma_fence_ops for operations on this fence
  457. * @lock: [in] the irqsafe spinlock to use for locking this fence
  458. * @context: [in] the execution context this fence is run on
  459. * @seqno: [in] a linear increasing sequence number for this context
  460. *
  461. * Initializes an allocated fence, the caller doesn't have to keep its
  462. * refcount after committing with this fence, but it will need to hold a
  463. * refcount again if dma_fence_ops.enable_signaling gets called. This can
  464. * be used for other implementing other types of fence.
  465. *
  466. * context and seqno are used for easy comparison between fences, allowing
  467. * to check which fence is later by simply using dma_fence_later.
  468. */
  469. void
  470. dma_fence_init(struct dma_fence *fence, const struct dma_fence_ops *ops,
  471. spinlock_t *lock, u64 context, unsigned seqno)
  472. {
  473. BUG_ON(!lock);
  474. BUG_ON(!ops || !ops->wait || !ops->enable_signaling ||
  475. !ops->get_driver_name || !ops->get_timeline_name);
  476. kref_init(&fence->refcount);
  477. fence->ops = ops;
  478. INIT_LIST_HEAD(&fence->cb_list);
  479. fence->lock = lock;
  480. fence->context = context;
  481. fence->seqno = seqno;
  482. fence->flags = 0UL;
  483. fence->error = 0;
  484. trace_dma_fence_init(fence);
  485. }
  486. EXPORT_SYMBOL(dma_fence_init);