dma-fence.c 16 KB

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