dma-fence.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /*
  2. * Fence mechanism for dma-buf 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. #ifndef __LINUX_DMA_FENCE_H
  21. #define __LINUX_DMA_FENCE_H
  22. #include <linux/err.h>
  23. #include <linux/wait.h>
  24. #include <linux/list.h>
  25. #include <linux/bitops.h>
  26. #include <linux/kref.h>
  27. #include <linux/sched.h>
  28. #include <linux/printk.h>
  29. #include <linux/rcupdate.h>
  30. struct dma_fence;
  31. struct dma_fence_ops;
  32. struct dma_fence_cb;
  33. /**
  34. * struct dma_fence - software synchronization primitive
  35. * @refcount: refcount for this fence
  36. * @ops: dma_fence_ops associated with this fence
  37. * @rcu: used for releasing fence with kfree_rcu
  38. * @cb_list: list of all callbacks to call
  39. * @lock: spin_lock_irqsave used for locking
  40. * @context: execution context this fence belongs to, returned by
  41. * dma_fence_context_alloc()
  42. * @seqno: the sequence number of this fence inside the execution context,
  43. * can be compared to decide which fence would be signaled later.
  44. * @flags: A mask of DMA_FENCE_FLAG_* defined below
  45. * @timestamp: Timestamp when the fence was signaled.
  46. * @error: Optional, only valid if < 0, must be set before calling
  47. * dma_fence_signal, indicates that the fence has completed with an error.
  48. *
  49. * the flags member must be manipulated and read using the appropriate
  50. * atomic ops (bit_*), so taking the spinlock will not be needed most
  51. * of the time.
  52. *
  53. * DMA_FENCE_FLAG_SIGNALED_BIT - fence is already signaled
  54. * DMA_FENCE_FLAG_TIMESTAMP_BIT - timestamp recorded for fence signaling
  55. * DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT - enable_signaling might have been called
  56. * DMA_FENCE_FLAG_USER_BITS - start of the unused bits, can be used by the
  57. * implementer of the fence for its own purposes. Can be used in different
  58. * ways by different fence implementers, so do not rely on this.
  59. *
  60. * Since atomic bitops are used, this is not guaranteed to be the case.
  61. * Particularly, if the bit was set, but dma_fence_signal was called right
  62. * before this bit was set, it would have been able to set the
  63. * DMA_FENCE_FLAG_SIGNALED_BIT, before enable_signaling was called.
  64. * Adding a check for DMA_FENCE_FLAG_SIGNALED_BIT after setting
  65. * DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT closes this race, and makes sure that
  66. * after dma_fence_signal was called, any enable_signaling call will have either
  67. * been completed, or never called at all.
  68. */
  69. struct dma_fence {
  70. struct kref refcount;
  71. const struct dma_fence_ops *ops;
  72. struct rcu_head rcu;
  73. struct list_head cb_list;
  74. spinlock_t *lock;
  75. u64 context;
  76. unsigned seqno;
  77. unsigned long flags;
  78. ktime_t timestamp;
  79. int error;
  80. };
  81. enum dma_fence_flag_bits {
  82. DMA_FENCE_FLAG_SIGNALED_BIT,
  83. DMA_FENCE_FLAG_TIMESTAMP_BIT,
  84. DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
  85. DMA_FENCE_FLAG_USER_BITS, /* must always be last member */
  86. };
  87. typedef void (*dma_fence_func_t)(struct dma_fence *fence,
  88. struct dma_fence_cb *cb);
  89. /**
  90. * struct dma_fence_cb - callback for dma_fence_add_callback()
  91. * @node: used by dma_fence_add_callback() to append this struct to fence::cb_list
  92. * @func: dma_fence_func_t to call
  93. *
  94. * This struct will be initialized by dma_fence_add_callback(), additional
  95. * data can be passed along by embedding dma_fence_cb in another struct.
  96. */
  97. struct dma_fence_cb {
  98. struct list_head node;
  99. dma_fence_func_t func;
  100. };
  101. /**
  102. * struct dma_fence_ops - operations implemented for fence
  103. *
  104. */
  105. struct dma_fence_ops {
  106. /**
  107. * @get_driver_name:
  108. *
  109. * Returns the driver name. This is a callback to allow drivers to
  110. * compute the name at runtime, without having it to store permanently
  111. * for each fence, or build a cache of some sort.
  112. *
  113. * This callback is mandatory.
  114. */
  115. const char * (*get_driver_name)(struct dma_fence *fence);
  116. /**
  117. * @get_timeline_name:
  118. *
  119. * Return the name of the context this fence belongs to. This is a
  120. * callback to allow drivers to compute the name at runtime, without
  121. * having it to store permanently for each fence, or build a cache of
  122. * some sort.
  123. *
  124. * This callback is mandatory.
  125. */
  126. const char * (*get_timeline_name)(struct dma_fence *fence);
  127. /**
  128. * @enable_signaling:
  129. *
  130. * Enable software signaling of fence.
  131. *
  132. * For fence implementations that have the capability for hw->hw
  133. * signaling, they can implement this op to enable the necessary
  134. * interrupts, or insert commands into cmdstream, etc, to avoid these
  135. * costly operations for the common case where only hw->hw
  136. * synchronization is required. This is called in the first
  137. * dma_fence_wait() or dma_fence_add_callback() path to let the fence
  138. * implementation know that there is another driver waiting on the
  139. * signal (ie. hw->sw case).
  140. *
  141. * This function can be called from atomic context, but not
  142. * from irq context, so normal spinlocks can be used.
  143. *
  144. * A return value of false indicates the fence already passed,
  145. * or some failure occurred that made it impossible to enable
  146. * signaling. True indicates successful enabling.
  147. *
  148. * &dma_fence.error may be set in enable_signaling, but only when false
  149. * is returned.
  150. *
  151. * Since many implementations can call dma_fence_signal() even when before
  152. * @enable_signaling has been called there's a race window, where the
  153. * dma_fence_signal() might result in the final fence reference being
  154. * released and its memory freed. To avoid this, implementations of this
  155. * callback should grab their own reference using dma_fence_get(), to be
  156. * released when the fence is signalled (through e.g. the interrupt
  157. * handler).
  158. *
  159. * This callback is optional. If this callback is not present, then the
  160. * driver must always have signaling enabled.
  161. */
  162. bool (*enable_signaling)(struct dma_fence *fence);
  163. /**
  164. * @signaled:
  165. *
  166. * Peek whether the fence is signaled, as a fastpath optimization for
  167. * e.g. dma_fence_wait() or dma_fence_add_callback(). Note that this
  168. * callback does not need to make any guarantees beyond that a fence
  169. * once indicates as signalled must always return true from this
  170. * callback. This callback may return false even if the fence has
  171. * completed already, in this case information hasn't propogated throug
  172. * the system yet. See also dma_fence_is_signaled().
  173. *
  174. * May set &dma_fence.error if returning true.
  175. *
  176. * This callback is optional.
  177. */
  178. bool (*signaled)(struct dma_fence *fence);
  179. /**
  180. * @wait:
  181. *
  182. * Custom wait implementation, defaults to dma_fence_default_wait() if
  183. * not set.
  184. *
  185. * The dma_fence_default_wait implementation should work for any fence, as long
  186. * as @enable_signaling works correctly. This hook allows drivers to
  187. * have an optimized version for the case where a process context is
  188. * already available, e.g. if @enable_signaling for the general case
  189. * needs to set up a worker thread.
  190. *
  191. * Must return -ERESTARTSYS if the wait is intr = true and the wait was
  192. * interrupted, and remaining jiffies if fence has signaled, or 0 if wait
  193. * timed out. Can also return other error values on custom implementations,
  194. * which should be treated as if the fence is signaled. For example a hardware
  195. * lockup could be reported like that.
  196. *
  197. * This callback is optional.
  198. */
  199. signed long (*wait)(struct dma_fence *fence,
  200. bool intr, signed long timeout);
  201. /**
  202. * @release:
  203. *
  204. * Called on destruction of fence to release additional resources.
  205. * Can be called from irq context. This callback is optional. If it is
  206. * NULL, then dma_fence_free() is instead called as the default
  207. * implementation.
  208. */
  209. void (*release)(struct dma_fence *fence);
  210. /**
  211. * @fence_value_str:
  212. *
  213. * Callback to fill in free-form debug info specific to this fence, like
  214. * the sequence number.
  215. *
  216. * This callback is optional.
  217. */
  218. void (*fence_value_str)(struct dma_fence *fence, char *str, int size);
  219. /**
  220. * @timeline_value_str:
  221. *
  222. * Fills in the current value of the timeline as a string, like the
  223. * sequence number. Note that the specific fence passed to this function
  224. * should not matter, drivers should only use it to look up the
  225. * corresponding timeline structures.
  226. */
  227. void (*timeline_value_str)(struct dma_fence *fence,
  228. char *str, int size);
  229. };
  230. void dma_fence_init(struct dma_fence *fence, const struct dma_fence_ops *ops,
  231. spinlock_t *lock, u64 context, unsigned seqno);
  232. void dma_fence_release(struct kref *kref);
  233. void dma_fence_free(struct dma_fence *fence);
  234. /**
  235. * dma_fence_put - decreases refcount of the fence
  236. * @fence: fence to reduce refcount of
  237. */
  238. static inline void dma_fence_put(struct dma_fence *fence)
  239. {
  240. if (fence)
  241. kref_put(&fence->refcount, dma_fence_release);
  242. }
  243. /**
  244. * dma_fence_get - increases refcount of the fence
  245. * @fence: fence to increase refcount of
  246. *
  247. * Returns the same fence, with refcount increased by 1.
  248. */
  249. static inline struct dma_fence *dma_fence_get(struct dma_fence *fence)
  250. {
  251. if (fence)
  252. kref_get(&fence->refcount);
  253. return fence;
  254. }
  255. /**
  256. * dma_fence_get_rcu - get a fence from a reservation_object_list with
  257. * rcu read lock
  258. * @fence: fence to increase refcount of
  259. *
  260. * Function returns NULL if no refcount could be obtained, or the fence.
  261. */
  262. static inline struct dma_fence *dma_fence_get_rcu(struct dma_fence *fence)
  263. {
  264. if (kref_get_unless_zero(&fence->refcount))
  265. return fence;
  266. else
  267. return NULL;
  268. }
  269. /**
  270. * dma_fence_get_rcu_safe - acquire a reference to an RCU tracked fence
  271. * @fencep: pointer to fence to increase refcount of
  272. *
  273. * Function returns NULL if no refcount could be obtained, or the fence.
  274. * This function handles acquiring a reference to a fence that may be
  275. * reallocated within the RCU grace period (such as with SLAB_TYPESAFE_BY_RCU),
  276. * so long as the caller is using RCU on the pointer to the fence.
  277. *
  278. * An alternative mechanism is to employ a seqlock to protect a bunch of
  279. * fences, such as used by struct reservation_object. When using a seqlock,
  280. * the seqlock must be taken before and checked after a reference to the
  281. * fence is acquired (as shown here).
  282. *
  283. * The caller is required to hold the RCU read lock.
  284. */
  285. static inline struct dma_fence *
  286. dma_fence_get_rcu_safe(struct dma_fence __rcu **fencep)
  287. {
  288. do {
  289. struct dma_fence *fence;
  290. fence = rcu_dereference(*fencep);
  291. if (!fence)
  292. return NULL;
  293. if (!dma_fence_get_rcu(fence))
  294. continue;
  295. /* The atomic_inc_not_zero() inside dma_fence_get_rcu()
  296. * provides a full memory barrier upon success (such as now).
  297. * This is paired with the write barrier from assigning
  298. * to the __rcu protected fence pointer so that if that
  299. * pointer still matches the current fence, we know we
  300. * have successfully acquire a reference to it. If it no
  301. * longer matches, we are holding a reference to some other
  302. * reallocated pointer. This is possible if the allocator
  303. * is using a freelist like SLAB_TYPESAFE_BY_RCU where the
  304. * fence remains valid for the RCU grace period, but it
  305. * may be reallocated. When using such allocators, we are
  306. * responsible for ensuring the reference we get is to
  307. * the right fence, as below.
  308. */
  309. if (fence == rcu_access_pointer(*fencep))
  310. return rcu_pointer_handoff(fence);
  311. dma_fence_put(fence);
  312. } while (1);
  313. }
  314. int dma_fence_signal(struct dma_fence *fence);
  315. int dma_fence_signal_locked(struct dma_fence *fence);
  316. signed long dma_fence_default_wait(struct dma_fence *fence,
  317. bool intr, signed long timeout);
  318. int dma_fence_add_callback(struct dma_fence *fence,
  319. struct dma_fence_cb *cb,
  320. dma_fence_func_t func);
  321. bool dma_fence_remove_callback(struct dma_fence *fence,
  322. struct dma_fence_cb *cb);
  323. void dma_fence_enable_sw_signaling(struct dma_fence *fence);
  324. /**
  325. * dma_fence_is_signaled_locked - Return an indication if the fence
  326. * is signaled yet.
  327. * @fence: the fence to check
  328. *
  329. * Returns true if the fence was already signaled, false if not. Since this
  330. * function doesn't enable signaling, it is not guaranteed to ever return
  331. * true if dma_fence_add_callback(), dma_fence_wait() or
  332. * dma_fence_enable_sw_signaling() haven't been called before.
  333. *
  334. * This function requires &dma_fence.lock to be held.
  335. *
  336. * See also dma_fence_is_signaled().
  337. */
  338. static inline bool
  339. dma_fence_is_signaled_locked(struct dma_fence *fence)
  340. {
  341. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  342. return true;
  343. if (fence->ops->signaled && fence->ops->signaled(fence)) {
  344. dma_fence_signal_locked(fence);
  345. return true;
  346. }
  347. return false;
  348. }
  349. /**
  350. * dma_fence_is_signaled - Return an indication if the fence is signaled yet.
  351. * @fence: the fence to check
  352. *
  353. * Returns true if the fence was already signaled, false if not. Since this
  354. * function doesn't enable signaling, it is not guaranteed to ever return
  355. * true if dma_fence_add_callback(), dma_fence_wait() or
  356. * dma_fence_enable_sw_signaling() haven't been called before.
  357. *
  358. * It's recommended for seqno fences to call dma_fence_signal when the
  359. * operation is complete, it makes it possible to prevent issues from
  360. * wraparound between time of issue and time of use by checking the return
  361. * value of this function before calling hardware-specific wait instructions.
  362. *
  363. * See also dma_fence_is_signaled_locked().
  364. */
  365. static inline bool
  366. dma_fence_is_signaled(struct dma_fence *fence)
  367. {
  368. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  369. return true;
  370. if (fence->ops->signaled && fence->ops->signaled(fence)) {
  371. dma_fence_signal(fence);
  372. return true;
  373. }
  374. return false;
  375. }
  376. /**
  377. * __dma_fence_is_later - return if f1 is chronologically later than f2
  378. * @f1: the first fence's seqno
  379. * @f2: the second fence's seqno from the same context
  380. *
  381. * Returns true if f1 is chronologically later than f2. Both fences must be
  382. * from the same context, since a seqno is not common across contexts.
  383. */
  384. static inline bool __dma_fence_is_later(u32 f1, u32 f2)
  385. {
  386. return (int)(f1 - f2) > 0;
  387. }
  388. /**
  389. * dma_fence_is_later - return if f1 is chronologically later than f2
  390. * @f1: the first fence from the same context
  391. * @f2: the second fence from the same context
  392. *
  393. * Returns true if f1 is chronologically later than f2. Both fences must be
  394. * from the same context, since a seqno is not re-used across contexts.
  395. */
  396. static inline bool dma_fence_is_later(struct dma_fence *f1,
  397. struct dma_fence *f2)
  398. {
  399. if (WARN_ON(f1->context != f2->context))
  400. return false;
  401. return __dma_fence_is_later(f1->seqno, f2->seqno);
  402. }
  403. /**
  404. * dma_fence_later - return the chronologically later fence
  405. * @f1: the first fence from the same context
  406. * @f2: the second fence from the same context
  407. *
  408. * Returns NULL if both fences are signaled, otherwise the fence that would be
  409. * signaled last. Both fences must be from the same context, since a seqno is
  410. * not re-used across contexts.
  411. */
  412. static inline struct dma_fence *dma_fence_later(struct dma_fence *f1,
  413. struct dma_fence *f2)
  414. {
  415. if (WARN_ON(f1->context != f2->context))
  416. return NULL;
  417. /*
  418. * Can't check just DMA_FENCE_FLAG_SIGNALED_BIT here, it may never
  419. * have been set if enable_signaling wasn't called, and enabling that
  420. * here is overkill.
  421. */
  422. if (dma_fence_is_later(f1, f2))
  423. return dma_fence_is_signaled(f1) ? NULL : f1;
  424. else
  425. return dma_fence_is_signaled(f2) ? NULL : f2;
  426. }
  427. /**
  428. * dma_fence_get_status_locked - returns the status upon completion
  429. * @fence: the dma_fence to query
  430. *
  431. * Drivers can supply an optional error status condition before they signal
  432. * the fence (to indicate whether the fence was completed due to an error
  433. * rather than success). The value of the status condition is only valid
  434. * if the fence has been signaled, dma_fence_get_status_locked() first checks
  435. * the signal state before reporting the error status.
  436. *
  437. * Returns 0 if the fence has not yet been signaled, 1 if the fence has
  438. * been signaled without an error condition, or a negative error code
  439. * if the fence has been completed in err.
  440. */
  441. static inline int dma_fence_get_status_locked(struct dma_fence *fence)
  442. {
  443. if (dma_fence_is_signaled_locked(fence))
  444. return fence->error ?: 1;
  445. else
  446. return 0;
  447. }
  448. int dma_fence_get_status(struct dma_fence *fence);
  449. /**
  450. * dma_fence_set_error - flag an error condition on the fence
  451. * @fence: the dma_fence
  452. * @error: the error to store
  453. *
  454. * Drivers can supply an optional error status condition before they signal
  455. * the fence, to indicate that the fence was completed due to an error
  456. * rather than success. This must be set before signaling (so that the value
  457. * is visible before any waiters on the signal callback are woken). This
  458. * helper exists to help catching erroneous setting of #dma_fence.error.
  459. */
  460. static inline void dma_fence_set_error(struct dma_fence *fence,
  461. int error)
  462. {
  463. WARN_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags));
  464. WARN_ON(error >= 0 || error < -MAX_ERRNO);
  465. fence->error = error;
  466. }
  467. signed long dma_fence_wait_timeout(struct dma_fence *,
  468. bool intr, signed long timeout);
  469. signed long dma_fence_wait_any_timeout(struct dma_fence **fences,
  470. uint32_t count,
  471. bool intr, signed long timeout,
  472. uint32_t *idx);
  473. /**
  474. * dma_fence_wait - sleep until the fence gets signaled
  475. * @fence: the fence to wait on
  476. * @intr: if true, do an interruptible wait
  477. *
  478. * This function will return -ERESTARTSYS if interrupted by a signal,
  479. * or 0 if the fence was signaled. Other error values may be
  480. * returned on custom implementations.
  481. *
  482. * Performs a synchronous wait on this fence. It is assumed the caller
  483. * directly or indirectly holds a reference to the fence, otherwise the
  484. * fence might be freed before return, resulting in undefined behavior.
  485. *
  486. * See also dma_fence_wait_timeout() and dma_fence_wait_any_timeout().
  487. */
  488. static inline signed long dma_fence_wait(struct dma_fence *fence, bool intr)
  489. {
  490. signed long ret;
  491. /* Since dma_fence_wait_timeout cannot timeout with
  492. * MAX_SCHEDULE_TIMEOUT, only valid return values are
  493. * -ERESTARTSYS and MAX_SCHEDULE_TIMEOUT.
  494. */
  495. ret = dma_fence_wait_timeout(fence, intr, MAX_SCHEDULE_TIMEOUT);
  496. return ret < 0 ? ret : 0;
  497. }
  498. u64 dma_fence_context_alloc(unsigned num);
  499. #define DMA_FENCE_TRACE(f, fmt, args...) \
  500. do { \
  501. struct dma_fence *__ff = (f); \
  502. if (IS_ENABLED(CONFIG_DMA_FENCE_TRACE)) \
  503. pr_info("f %llu#%u: " fmt, \
  504. __ff->context, __ff->seqno, ##args); \
  505. } while (0)
  506. #define DMA_FENCE_WARN(f, fmt, args...) \
  507. do { \
  508. struct dma_fence *__ff = (f); \
  509. pr_warn("f %llu#%u: " fmt, __ff->context, __ff->seqno, \
  510. ##args); \
  511. } while (0)
  512. #define DMA_FENCE_ERR(f, fmt, args...) \
  513. do { \
  514. struct dma_fence *__ff = (f); \
  515. pr_err("f %llu#%u: " fmt, __ff->context, __ff->seqno, \
  516. ##args); \
  517. } while (0)
  518. #endif /* __LINUX_DMA_FENCE_H */