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. 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. spin_unlock_irqrestore(fence->lock, flags);
  290. return ret;
  291. }
  292. EXPORT_SYMBOL(dma_fence_remove_callback);
  293. struct default_wait_cb {
  294. struct dma_fence_cb base;
  295. struct task_struct *task;
  296. };
  297. static void
  298. dma_fence_default_wait_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
  299. {
  300. struct default_wait_cb *wait =
  301. container_of(cb, struct default_wait_cb, base);
  302. wake_up_state(wait->task, TASK_NORMAL);
  303. }
  304. /**
  305. * dma_fence_default_wait - default sleep until the fence gets signaled
  306. * or until timeout elapses
  307. * @fence: [in] the fence to wait on
  308. * @intr: [in] if true, do an interruptible wait
  309. * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
  310. *
  311. * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
  312. * remaining timeout in jiffies on success. If timeout is zero the value one is
  313. * returned if the fence is already signaled for consistency with other
  314. * functions taking a jiffies timeout.
  315. */
  316. signed long
  317. dma_fence_default_wait(struct dma_fence *fence, bool intr, signed long timeout)
  318. {
  319. struct default_wait_cb cb;
  320. unsigned long flags;
  321. signed long ret = timeout ? timeout : 1;
  322. bool was_set;
  323. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  324. return ret;
  325. spin_lock_irqsave(fence->lock, flags);
  326. if (intr && signal_pending(current)) {
  327. ret = -ERESTARTSYS;
  328. goto out;
  329. }
  330. was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
  331. &fence->flags);
  332. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  333. goto out;
  334. if (!was_set) {
  335. trace_dma_fence_enable_signal(fence);
  336. if (!fence->ops->enable_signaling(fence)) {
  337. dma_fence_signal_locked(fence);
  338. goto out;
  339. }
  340. }
  341. if (!timeout) {
  342. ret = 0;
  343. goto out;
  344. }
  345. cb.base.func = dma_fence_default_wait_cb;
  346. cb.task = current;
  347. list_add(&cb.base.node, &fence->cb_list);
  348. while (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags) && ret > 0) {
  349. if (intr)
  350. __set_current_state(TASK_INTERRUPTIBLE);
  351. else
  352. __set_current_state(TASK_UNINTERRUPTIBLE);
  353. spin_unlock_irqrestore(fence->lock, flags);
  354. ret = schedule_timeout(ret);
  355. spin_lock_irqsave(fence->lock, flags);
  356. if (ret > 0 && intr && signal_pending(current))
  357. ret = -ERESTARTSYS;
  358. }
  359. if (!list_empty(&cb.base.node))
  360. list_del(&cb.base.node);
  361. __set_current_state(TASK_RUNNING);
  362. out:
  363. spin_unlock_irqrestore(fence->lock, flags);
  364. return ret;
  365. }
  366. EXPORT_SYMBOL(dma_fence_default_wait);
  367. static bool
  368. dma_fence_test_signaled_any(struct dma_fence **fences, uint32_t count,
  369. uint32_t *idx)
  370. {
  371. int i;
  372. for (i = 0; i < count; ++i) {
  373. struct dma_fence *fence = fences[i];
  374. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
  375. if (idx)
  376. *idx = i;
  377. return true;
  378. }
  379. }
  380. return false;
  381. }
  382. /**
  383. * dma_fence_wait_any_timeout - sleep until any fence gets signaled
  384. * or until timeout elapses
  385. * @fences: [in] array of fences to wait on
  386. * @count: [in] number of fences to wait on
  387. * @intr: [in] if true, do an interruptible wait
  388. * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
  389. * @idx: [out] the first signaled fence index, meaningful only on
  390. * positive return
  391. *
  392. * Returns -EINVAL on custom fence wait implementation, -ERESTARTSYS if
  393. * interrupted, 0 if the wait timed out, or the remaining timeout in jiffies
  394. * on success.
  395. *
  396. * Synchronous waits for the first fence in the array to be signaled. The
  397. * caller needs to hold a reference to all fences in the array, otherwise a
  398. * fence might be freed before return, resulting in undefined behavior.
  399. */
  400. signed long
  401. dma_fence_wait_any_timeout(struct dma_fence **fences, uint32_t count,
  402. bool intr, signed long timeout, uint32_t *idx)
  403. {
  404. struct default_wait_cb *cb;
  405. signed long ret = timeout;
  406. unsigned i;
  407. if (WARN_ON(!fences || !count || timeout < 0))
  408. return -EINVAL;
  409. if (timeout == 0) {
  410. for (i = 0; i < count; ++i)
  411. if (dma_fence_is_signaled(fences[i])) {
  412. if (idx)
  413. *idx = i;
  414. return 1;
  415. }
  416. return 0;
  417. }
  418. cb = kcalloc(count, sizeof(struct default_wait_cb), GFP_KERNEL);
  419. if (cb == NULL) {
  420. ret = -ENOMEM;
  421. goto err_free_cb;
  422. }
  423. for (i = 0; i < count; ++i) {
  424. struct dma_fence *fence = fences[i];
  425. if (fence->ops->wait != dma_fence_default_wait) {
  426. ret = -EINVAL;
  427. goto fence_rm_cb;
  428. }
  429. cb[i].task = current;
  430. if (dma_fence_add_callback(fence, &cb[i].base,
  431. dma_fence_default_wait_cb)) {
  432. /* This fence is already signaled */
  433. if (idx)
  434. *idx = i;
  435. goto fence_rm_cb;
  436. }
  437. }
  438. while (ret > 0) {
  439. if (intr)
  440. set_current_state(TASK_INTERRUPTIBLE);
  441. else
  442. set_current_state(TASK_UNINTERRUPTIBLE);
  443. if (dma_fence_test_signaled_any(fences, count, idx))
  444. break;
  445. ret = schedule_timeout(ret);
  446. if (ret > 0 && intr && signal_pending(current))
  447. ret = -ERESTARTSYS;
  448. }
  449. __set_current_state(TASK_RUNNING);
  450. fence_rm_cb:
  451. while (i-- > 0)
  452. dma_fence_remove_callback(fences[i], &cb[i].base);
  453. err_free_cb:
  454. kfree(cb);
  455. return ret;
  456. }
  457. EXPORT_SYMBOL(dma_fence_wait_any_timeout);
  458. /**
  459. * dma_fence_init - Initialize a custom fence.
  460. * @fence: [in] the fence to initialize
  461. * @ops: [in] the dma_fence_ops for operations on this fence
  462. * @lock: [in] the irqsafe spinlock to use for locking this fence
  463. * @context: [in] the execution context this fence is run on
  464. * @seqno: [in] a linear increasing sequence number for this context
  465. *
  466. * Initializes an allocated fence, the caller doesn't have to keep its
  467. * refcount after committing with this fence, but it will need to hold a
  468. * refcount again if dma_fence_ops.enable_signaling gets called. This can
  469. * be used for other implementing other types of fence.
  470. *
  471. * context and seqno are used for easy comparison between fences, allowing
  472. * to check which fence is later by simply using dma_fence_later.
  473. */
  474. void
  475. dma_fence_init(struct dma_fence *fence, const struct dma_fence_ops *ops,
  476. spinlock_t *lock, u64 context, unsigned seqno)
  477. {
  478. BUG_ON(!lock);
  479. BUG_ON(!ops || !ops->wait || !ops->enable_signaling ||
  480. !ops->get_driver_name || !ops->get_timeline_name);
  481. kref_init(&fence->refcount);
  482. fence->ops = ops;
  483. INIT_LIST_HEAD(&fence->cb_list);
  484. fence->lock = lock;
  485. fence->context = context;
  486. fence->seqno = seqno;
  487. fence->flags = 0UL;
  488. fence->error = 0;
  489. trace_dma_fence_init(fence);
  490. }
  491. EXPORT_SYMBOL(dma_fence_init);