dma-fence.c 16 KB

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