dma-fence.c 16 KB

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