dma-fence.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Fence mechanism for dma-buf and to allow for asynchronous dma access
  4. *
  5. * Copyright (C) 2012 Canonical Ltd
  6. * Copyright (C) 2012 Texas Instruments
  7. *
  8. * Authors:
  9. * Rob Clark <[email protected]>
  10. * Maarten Lankhorst <[email protected]>
  11. */
  12. #include <linux/slab.h>
  13. #include <linux/export.h>
  14. #include <linux/atomic.h>
  15. #include <linux/dma-fence.h>
  16. #include <linux/sched/signal.h>
  17. #include <linux/seq_file.h>
  18. #define CREATE_TRACE_POINTS
  19. #include <trace/events/dma_fence.h>
  20. EXPORT_TRACEPOINT_SYMBOL(dma_fence_emit);
  21. EXPORT_TRACEPOINT_SYMBOL(dma_fence_enable_signal);
  22. EXPORT_TRACEPOINT_SYMBOL(dma_fence_signaled);
  23. static DEFINE_SPINLOCK(dma_fence_stub_lock);
  24. static struct dma_fence dma_fence_stub;
  25. /*
  26. * fence context counter: each execution context should have its own
  27. * fence context, this allows checking if fences belong to the same
  28. * context or not. One device can have multiple separate contexts,
  29. * and they're used if some engine can run independently of another.
  30. */
  31. static atomic64_t dma_fence_context_counter = ATOMIC64_INIT(1);
  32. /**
  33. * DOC: DMA fences overview
  34. *
  35. * DMA fences, represented by &struct dma_fence, are the kernel internal
  36. * synchronization primitive for DMA operations like GPU rendering, video
  37. * encoding/decoding, or displaying buffers on a screen.
  38. *
  39. * A fence is initialized using dma_fence_init() and completed using
  40. * dma_fence_signal(). Fences are associated with a context, allocated through
  41. * dma_fence_context_alloc(), and all fences on the same context are
  42. * fully ordered.
  43. *
  44. * Since the purposes of fences is to facilitate cross-device and
  45. * cross-application synchronization, there's multiple ways to use one:
  46. *
  47. * - Individual fences can be exposed as a &sync_file, accessed as a file
  48. * descriptor from userspace, created by calling sync_file_create(). This is
  49. * called explicit fencing, since userspace passes around explicit
  50. * synchronization points.
  51. *
  52. * - Some subsystems also have their own explicit fencing primitives, like
  53. * &drm_syncobj. Compared to &sync_file, a &drm_syncobj allows the underlying
  54. * fence to be updated.
  55. *
  56. * - Then there's also implicit fencing, where the synchronization points are
  57. * implicitly passed around as part of shared &dma_buf instances. Such
  58. * implicit fences are stored in &struct dma_resv through the
  59. * &dma_buf.resv pointer.
  60. */
  61. /**
  62. * DOC: fence cross-driver contract
  63. *
  64. * Since &dma_fence provide a cross driver contract, all drivers must follow the
  65. * same rules:
  66. *
  67. * * Fences must complete in a reasonable time. Fences which represent kernels
  68. * and shaders submitted by userspace, which could run forever, must be backed
  69. * up by timeout and gpu hang recovery code. Minimally that code must prevent
  70. * further command submission and force complete all in-flight fences, e.g.
  71. * when the driver or hardware do not support gpu reset, or if the gpu reset
  72. * failed for some reason. Ideally the driver supports gpu recovery which only
  73. * affects the offending userspace context, and no other userspace
  74. * submissions.
  75. *
  76. * * Drivers may have different ideas of what completion within a reasonable
  77. * time means. Some hang recovery code uses a fixed timeout, others a mix
  78. * between observing forward progress and increasingly strict timeouts.
  79. * Drivers should not try to second guess timeout handling of fences from
  80. * other drivers.
  81. *
  82. * * To ensure there's no deadlocks of dma_fence_wait() against other locks
  83. * drivers should annotate all code required to reach dma_fence_signal(),
  84. * which completes the fences, with dma_fence_begin_signalling() and
  85. * dma_fence_end_signalling().
  86. *
  87. * * Drivers are allowed to call dma_fence_wait() while holding dma_resv_lock().
  88. * This means any code required for fence completion cannot acquire a
  89. * &dma_resv lock. Note that this also pulls in the entire established
  90. * locking hierarchy around dma_resv_lock() and dma_resv_unlock().
  91. *
  92. * * Drivers are allowed to call dma_fence_wait() from their &shrinker
  93. * callbacks. This means any code required for fence completion cannot
  94. * allocate memory with GFP_KERNEL.
  95. *
  96. * * Drivers are allowed to call dma_fence_wait() from their &mmu_notifier
  97. * respectively &mmu_interval_notifier callbacks. This means any code required
  98. * for fence completeion cannot allocate memory with GFP_NOFS or GFP_NOIO.
  99. * Only GFP_ATOMIC is permissible, which might fail.
  100. *
  101. * Note that only GPU drivers have a reasonable excuse for both requiring
  102. * &mmu_interval_notifier and &shrinker callbacks at the same time as having to
  103. * track asynchronous compute work using &dma_fence. No driver outside of
  104. * drivers/gpu should ever call dma_fence_wait() in such contexts.
  105. */
  106. static const char *dma_fence_stub_get_name(struct dma_fence *fence)
  107. {
  108. return "stub";
  109. }
  110. static const struct dma_fence_ops dma_fence_stub_ops = {
  111. .get_driver_name = dma_fence_stub_get_name,
  112. .get_timeline_name = dma_fence_stub_get_name,
  113. };
  114. /**
  115. * dma_fence_get_stub - return a signaled fence
  116. *
  117. * Return a stub fence which is already signaled. The fence's
  118. * timestamp corresponds to the first time after boot this
  119. * function is called.
  120. */
  121. struct dma_fence *dma_fence_get_stub(void)
  122. {
  123. spin_lock(&dma_fence_stub_lock);
  124. if (!dma_fence_stub.ops) {
  125. dma_fence_init(&dma_fence_stub,
  126. &dma_fence_stub_ops,
  127. &dma_fence_stub_lock,
  128. 0, 0);
  129. set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
  130. &dma_fence_stub.flags);
  131. dma_fence_signal_locked(&dma_fence_stub);
  132. }
  133. spin_unlock(&dma_fence_stub_lock);
  134. return dma_fence_get(&dma_fence_stub);
  135. }
  136. EXPORT_SYMBOL(dma_fence_get_stub);
  137. /**
  138. * dma_fence_allocate_private_stub - return a private, signaled fence
  139. * @timestamp: timestamp when the fence was signaled
  140. *
  141. * Return a newly allocated and signaled stub fence.
  142. */
  143. struct dma_fence *dma_fence_allocate_private_stub(ktime_t timestamp)
  144. {
  145. struct dma_fence *fence;
  146. fence = kzalloc(sizeof(*fence), GFP_KERNEL);
  147. if (fence == NULL)
  148. return NULL;
  149. dma_fence_init(fence,
  150. &dma_fence_stub_ops,
  151. &dma_fence_stub_lock,
  152. 0, 0);
  153. set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
  154. &fence->flags);
  155. dma_fence_signal_timestamp(fence, timestamp);
  156. return fence;
  157. }
  158. EXPORT_SYMBOL(dma_fence_allocate_private_stub);
  159. /**
  160. * dma_fence_context_alloc - allocate an array of fence contexts
  161. * @num: amount of contexts to allocate
  162. *
  163. * This function will return the first index of the number of fence contexts
  164. * allocated. The fence context is used for setting &dma_fence.context to a
  165. * unique number by passing the context to dma_fence_init().
  166. */
  167. u64 dma_fence_context_alloc(unsigned num)
  168. {
  169. WARN_ON(!num);
  170. return atomic64_fetch_add(num, &dma_fence_context_counter);
  171. }
  172. EXPORT_SYMBOL(dma_fence_context_alloc);
  173. /**
  174. * DOC: fence signalling annotation
  175. *
  176. * Proving correctness of all the kernel code around &dma_fence through code
  177. * review and testing is tricky for a few reasons:
  178. *
  179. * * It is a cross-driver contract, and therefore all drivers must follow the
  180. * same rules for lock nesting order, calling contexts for various functions
  181. * and anything else significant for in-kernel interfaces. But it is also
  182. * impossible to test all drivers in a single machine, hence brute-force N vs.
  183. * N testing of all combinations is impossible. Even just limiting to the
  184. * possible combinations is infeasible.
  185. *
  186. * * There is an enormous amount of driver code involved. For render drivers
  187. * there's the tail of command submission, after fences are published,
  188. * scheduler code, interrupt and workers to process job completion,
  189. * and timeout, gpu reset and gpu hang recovery code. Plus for integration
  190. * with core mm with have &mmu_notifier, respectively &mmu_interval_notifier,
  191. * and &shrinker. For modesetting drivers there's the commit tail functions
  192. * between when fences for an atomic modeset are published, and when the
  193. * corresponding vblank completes, including any interrupt processing and
  194. * related workers. Auditing all that code, across all drivers, is not
  195. * feasible.
  196. *
  197. * * Due to how many other subsystems are involved and the locking hierarchies
  198. * this pulls in there is extremely thin wiggle-room for driver-specific
  199. * differences. &dma_fence interacts with almost all of the core memory
  200. * handling through page fault handlers via &dma_resv, dma_resv_lock() and
  201. * dma_resv_unlock(). On the other side it also interacts through all
  202. * allocation sites through &mmu_notifier and &shrinker.
  203. *
  204. * Furthermore lockdep does not handle cross-release dependencies, which means
  205. * any deadlocks between dma_fence_wait() and dma_fence_signal() can't be caught
  206. * at runtime with some quick testing. The simplest example is one thread
  207. * waiting on a &dma_fence while holding a lock::
  208. *
  209. * lock(A);
  210. * dma_fence_wait(B);
  211. * unlock(A);
  212. *
  213. * while the other thread is stuck trying to acquire the same lock, which
  214. * prevents it from signalling the fence the previous thread is stuck waiting
  215. * on::
  216. *
  217. * lock(A);
  218. * unlock(A);
  219. * dma_fence_signal(B);
  220. *
  221. * By manually annotating all code relevant to signalling a &dma_fence we can
  222. * teach lockdep about these dependencies, which also helps with the validation
  223. * headache since now lockdep can check all the rules for us::
  224. *
  225. * cookie = dma_fence_begin_signalling();
  226. * lock(A);
  227. * unlock(A);
  228. * dma_fence_signal(B);
  229. * dma_fence_end_signalling(cookie);
  230. *
  231. * For using dma_fence_begin_signalling() and dma_fence_end_signalling() to
  232. * annotate critical sections the following rules need to be observed:
  233. *
  234. * * All code necessary to complete a &dma_fence must be annotated, from the
  235. * point where a fence is accessible to other threads, to the point where
  236. * dma_fence_signal() is called. Un-annotated code can contain deadlock issues,
  237. * and due to the very strict rules and many corner cases it is infeasible to
  238. * catch these just with review or normal stress testing.
  239. *
  240. * * &struct dma_resv deserves a special note, since the readers are only
  241. * protected by rcu. This means the signalling critical section starts as soon
  242. * as the new fences are installed, even before dma_resv_unlock() is called.
  243. *
  244. * * The only exception are fast paths and opportunistic signalling code, which
  245. * calls dma_fence_signal() purely as an optimization, but is not required to
  246. * guarantee completion of a &dma_fence. The usual example is a wait IOCTL
  247. * which calls dma_fence_signal(), while the mandatory completion path goes
  248. * through a hardware interrupt and possible job completion worker.
  249. *
  250. * * To aid composability of code, the annotations can be freely nested, as long
  251. * as the overall locking hierarchy is consistent. The annotations also work
  252. * both in interrupt and process context. Due to implementation details this
  253. * requires that callers pass an opaque cookie from
  254. * dma_fence_begin_signalling() to dma_fence_end_signalling().
  255. *
  256. * * Validation against the cross driver contract is implemented by priming
  257. * lockdep with the relevant hierarchy at boot-up. This means even just
  258. * testing with a single device is enough to validate a driver, at least as
  259. * far as deadlocks with dma_fence_wait() against dma_fence_signal() are
  260. * concerned.
  261. */
  262. #ifdef CONFIG_LOCKDEP
  263. static struct lockdep_map dma_fence_lockdep_map = {
  264. .name = "dma_fence_map"
  265. };
  266. /**
  267. * dma_fence_begin_signalling - begin a critical DMA fence signalling section
  268. *
  269. * Drivers should use this to annotate the beginning of any code section
  270. * required to eventually complete &dma_fence by calling dma_fence_signal().
  271. *
  272. * The end of these critical sections are annotated with
  273. * dma_fence_end_signalling().
  274. *
  275. * Returns:
  276. *
  277. * Opaque cookie needed by the implementation, which needs to be passed to
  278. * dma_fence_end_signalling().
  279. */
  280. bool dma_fence_begin_signalling(void)
  281. {
  282. /* explicitly nesting ... */
  283. if (lock_is_held_type(&dma_fence_lockdep_map, 1))
  284. return true;
  285. /* rely on might_sleep check for soft/hardirq locks */
  286. if (in_atomic())
  287. return true;
  288. /* ... and non-recursive readlock */
  289. lock_acquire(&dma_fence_lockdep_map, 0, 0, 1, 1, NULL, _RET_IP_);
  290. return false;
  291. }
  292. EXPORT_SYMBOL(dma_fence_begin_signalling);
  293. /**
  294. * dma_fence_end_signalling - end a critical DMA fence signalling section
  295. * @cookie: opaque cookie from dma_fence_begin_signalling()
  296. *
  297. * Closes a critical section annotation opened by dma_fence_begin_signalling().
  298. */
  299. void dma_fence_end_signalling(bool cookie)
  300. {
  301. if (cookie)
  302. return;
  303. lock_release(&dma_fence_lockdep_map, _RET_IP_);
  304. }
  305. EXPORT_SYMBOL(dma_fence_end_signalling);
  306. void __dma_fence_might_wait(void)
  307. {
  308. bool tmp;
  309. tmp = lock_is_held_type(&dma_fence_lockdep_map, 1);
  310. if (tmp)
  311. lock_release(&dma_fence_lockdep_map, _THIS_IP_);
  312. lock_map_acquire(&dma_fence_lockdep_map);
  313. lock_map_release(&dma_fence_lockdep_map);
  314. if (tmp)
  315. lock_acquire(&dma_fence_lockdep_map, 0, 0, 1, 1, NULL, _THIS_IP_);
  316. }
  317. #endif
  318. /**
  319. * dma_fence_signal_timestamp_locked - signal completion of a fence
  320. * @fence: the fence to signal
  321. * @timestamp: fence signal timestamp in kernel's CLOCK_MONOTONIC time domain
  322. *
  323. * Signal completion for software callbacks on a fence, this will unblock
  324. * dma_fence_wait() calls and run all the callbacks added with
  325. * dma_fence_add_callback(). Can be called multiple times, but since a fence
  326. * can only go from the unsignaled to the signaled state and not back, it will
  327. * only be effective the first time. Set the timestamp provided as the fence
  328. * signal timestamp.
  329. *
  330. * Unlike dma_fence_signal_timestamp(), this function must be called with
  331. * &dma_fence.lock held.
  332. *
  333. * Returns 0 on success and a negative error value when @fence has been
  334. * signalled already.
  335. */
  336. int dma_fence_signal_timestamp_locked(struct dma_fence *fence,
  337. ktime_t timestamp)
  338. {
  339. struct dma_fence_cb *cur, *tmp;
  340. struct list_head cb_list;
  341. lockdep_assert_held(fence->lock);
  342. if (unlikely(test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
  343. &fence->flags)))
  344. return -EINVAL;
  345. /* Stash the cb_list before replacing it with the timestamp */
  346. list_replace(&fence->cb_list, &cb_list);
  347. fence->timestamp = timestamp;
  348. set_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags);
  349. trace_dma_fence_signaled(fence);
  350. list_for_each_entry_safe(cur, tmp, &cb_list, node) {
  351. INIT_LIST_HEAD(&cur->node);
  352. cur->func(fence, cur);
  353. }
  354. return 0;
  355. }
  356. EXPORT_SYMBOL(dma_fence_signal_timestamp_locked);
  357. /**
  358. * dma_fence_signal_timestamp - signal completion of a fence
  359. * @fence: the fence to signal
  360. * @timestamp: fence signal timestamp in kernel's CLOCK_MONOTONIC time domain
  361. *
  362. * Signal completion for software callbacks on a fence, this will unblock
  363. * dma_fence_wait() calls and run all the callbacks added with
  364. * dma_fence_add_callback(). Can be called multiple times, but since a fence
  365. * can only go from the unsignaled to the signaled state and not back, it will
  366. * only be effective the first time. Set the timestamp provided as the fence
  367. * signal timestamp.
  368. *
  369. * Returns 0 on success and a negative error value when @fence has been
  370. * signalled already.
  371. */
  372. int dma_fence_signal_timestamp(struct dma_fence *fence, ktime_t timestamp)
  373. {
  374. unsigned long flags;
  375. int ret;
  376. if (!fence)
  377. return -EINVAL;
  378. spin_lock_irqsave(fence->lock, flags);
  379. ret = dma_fence_signal_timestamp_locked(fence, timestamp);
  380. spin_unlock_irqrestore(fence->lock, flags);
  381. return ret;
  382. }
  383. EXPORT_SYMBOL(dma_fence_signal_timestamp);
  384. /**
  385. * dma_fence_signal_locked - signal completion of a fence
  386. * @fence: the fence to signal
  387. *
  388. * Signal completion for software callbacks on a fence, this will unblock
  389. * dma_fence_wait() calls and run all the callbacks added with
  390. * dma_fence_add_callback(). Can be called multiple times, but since a fence
  391. * can only go from the unsignaled to the signaled state and not back, it will
  392. * only be effective the first time.
  393. *
  394. * Unlike dma_fence_signal(), this function must be called with &dma_fence.lock
  395. * held.
  396. *
  397. * Returns 0 on success and a negative error value when @fence has been
  398. * signalled already.
  399. */
  400. int dma_fence_signal_locked(struct dma_fence *fence)
  401. {
  402. return dma_fence_signal_timestamp_locked(fence, ktime_get());
  403. }
  404. EXPORT_SYMBOL(dma_fence_signal_locked);
  405. /**
  406. * dma_fence_signal - signal completion of a fence
  407. * @fence: the fence to signal
  408. *
  409. * Signal completion for software callbacks on a fence, this will unblock
  410. * dma_fence_wait() calls and run all the callbacks added with
  411. * dma_fence_add_callback(). Can be called multiple times, but since a fence
  412. * can only go from the unsignaled to the signaled state and not back, it will
  413. * only be effective the first time.
  414. *
  415. * Returns 0 on success and a negative error value when @fence has been
  416. * signalled already.
  417. */
  418. int dma_fence_signal(struct dma_fence *fence)
  419. {
  420. unsigned long flags;
  421. int ret;
  422. bool tmp;
  423. if (!fence)
  424. return -EINVAL;
  425. tmp = dma_fence_begin_signalling();
  426. spin_lock_irqsave(fence->lock, flags);
  427. ret = dma_fence_signal_timestamp_locked(fence, ktime_get());
  428. spin_unlock_irqrestore(fence->lock, flags);
  429. dma_fence_end_signalling(tmp);
  430. return ret;
  431. }
  432. EXPORT_SYMBOL(dma_fence_signal);
  433. /**
  434. * dma_fence_wait_timeout - sleep until the fence gets signaled
  435. * or until timeout elapses
  436. * @fence: the fence to wait on
  437. * @intr: if true, do an interruptible wait
  438. * @timeout: timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
  439. *
  440. * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
  441. * remaining timeout in jiffies on success. Other error values may be
  442. * returned on custom implementations.
  443. *
  444. * Performs a synchronous wait on this fence. It is assumed the caller
  445. * directly or indirectly (buf-mgr between reservation and committing)
  446. * holds a reference to the fence, otherwise the fence might be
  447. * freed before return, resulting in undefined behavior.
  448. *
  449. * See also dma_fence_wait() and dma_fence_wait_any_timeout().
  450. */
  451. signed long
  452. dma_fence_wait_timeout(struct dma_fence *fence, bool intr, signed long timeout)
  453. {
  454. signed long ret;
  455. if (WARN_ON(timeout < 0))
  456. return -EINVAL;
  457. might_sleep();
  458. __dma_fence_might_wait();
  459. dma_fence_enable_sw_signaling(fence);
  460. trace_dma_fence_wait_start(fence);
  461. if (fence->ops->wait)
  462. ret = fence->ops->wait(fence, intr, timeout);
  463. else
  464. ret = dma_fence_default_wait(fence, intr, timeout);
  465. trace_dma_fence_wait_end(fence);
  466. return ret;
  467. }
  468. EXPORT_SYMBOL(dma_fence_wait_timeout);
  469. /**
  470. * dma_fence_release - default relese function for fences
  471. * @kref: &dma_fence.recfount
  472. *
  473. * This is the default release functions for &dma_fence. Drivers shouldn't call
  474. * this directly, but instead call dma_fence_put().
  475. */
  476. void dma_fence_release(struct kref *kref)
  477. {
  478. struct dma_fence *fence =
  479. container_of(kref, struct dma_fence, refcount);
  480. trace_dma_fence_destroy(fence);
  481. if (WARN(!list_empty(&fence->cb_list) &&
  482. !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags),
  483. "Fence %s:%s:%llx:%llx released with pending signals!\n",
  484. fence->ops->get_driver_name(fence),
  485. fence->ops->get_timeline_name(fence),
  486. fence->context, fence->seqno)) {
  487. unsigned long flags;
  488. /*
  489. * Failed to signal before release, likely a refcounting issue.
  490. *
  491. * This should never happen, but if it does make sure that we
  492. * don't leave chains dangling. We set the error flag first
  493. * so that the callbacks know this signal is due to an error.
  494. */
  495. spin_lock_irqsave(fence->lock, flags);
  496. fence->error = -EDEADLK;
  497. dma_fence_signal_locked(fence);
  498. spin_unlock_irqrestore(fence->lock, flags);
  499. }
  500. if (fence->ops->release)
  501. fence->ops->release(fence);
  502. else
  503. dma_fence_free(fence);
  504. }
  505. EXPORT_SYMBOL(dma_fence_release);
  506. /**
  507. * dma_fence_free - default release function for &dma_fence.
  508. * @fence: fence to release
  509. *
  510. * This is the default implementation for &dma_fence_ops.release. It calls
  511. * kfree_rcu() on @fence.
  512. */
  513. void dma_fence_free(struct dma_fence *fence)
  514. {
  515. kfree_rcu(fence, rcu);
  516. }
  517. EXPORT_SYMBOL(dma_fence_free);
  518. static bool __dma_fence_enable_signaling(struct dma_fence *fence)
  519. {
  520. bool was_set;
  521. lockdep_assert_held(fence->lock);
  522. was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
  523. &fence->flags);
  524. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  525. return false;
  526. if (!was_set && fence->ops->enable_signaling) {
  527. trace_dma_fence_enable_signal(fence);
  528. if (!fence->ops->enable_signaling(fence)) {
  529. dma_fence_signal_locked(fence);
  530. return false;
  531. }
  532. }
  533. return true;
  534. }
  535. /**
  536. * dma_fence_enable_sw_signaling - enable signaling on fence
  537. * @fence: the fence to enable
  538. *
  539. * This will request for sw signaling to be enabled, to make the fence
  540. * complete as soon as possible. This calls &dma_fence_ops.enable_signaling
  541. * internally.
  542. */
  543. void dma_fence_enable_sw_signaling(struct dma_fence *fence)
  544. {
  545. unsigned long flags;
  546. spin_lock_irqsave(fence->lock, flags);
  547. __dma_fence_enable_signaling(fence);
  548. spin_unlock_irqrestore(fence->lock, flags);
  549. }
  550. EXPORT_SYMBOL(dma_fence_enable_sw_signaling);
  551. /**
  552. * dma_fence_add_callback - add a callback to be called when the fence
  553. * is signaled
  554. * @fence: the fence to wait on
  555. * @cb: the callback to register
  556. * @func: the function to call
  557. *
  558. * Add a software callback to the fence. The caller should keep a reference to
  559. * the fence.
  560. *
  561. * @cb will be initialized by dma_fence_add_callback(), no initialization
  562. * by the caller is required. Any number of callbacks can be registered
  563. * to a fence, but a callback can only be registered to one fence at a time.
  564. *
  565. * If fence is already signaled, this function will return -ENOENT (and
  566. * *not* call the callback).
  567. *
  568. * Note that the callback can be called from an atomic context or irq context.
  569. *
  570. * Returns 0 in case of success, -ENOENT if the fence is already signaled
  571. * and -EINVAL in case of error.
  572. */
  573. int dma_fence_add_callback(struct dma_fence *fence, struct dma_fence_cb *cb,
  574. dma_fence_func_t func)
  575. {
  576. unsigned long flags;
  577. int ret = 0;
  578. if (WARN_ON(!fence || !func))
  579. return -EINVAL;
  580. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
  581. INIT_LIST_HEAD(&cb->node);
  582. return -ENOENT;
  583. }
  584. spin_lock_irqsave(fence->lock, flags);
  585. if (__dma_fence_enable_signaling(fence)) {
  586. cb->func = func;
  587. list_add_tail(&cb->node, &fence->cb_list);
  588. } else {
  589. INIT_LIST_HEAD(&cb->node);
  590. ret = -ENOENT;
  591. }
  592. spin_unlock_irqrestore(fence->lock, flags);
  593. return ret;
  594. }
  595. EXPORT_SYMBOL(dma_fence_add_callback);
  596. /**
  597. * dma_fence_get_status - returns the status upon completion
  598. * @fence: the dma_fence to query
  599. *
  600. * This wraps dma_fence_get_status_locked() to return the error status
  601. * condition on a signaled fence. See dma_fence_get_status_locked() for more
  602. * details.
  603. *
  604. * Returns 0 if the fence has not yet been signaled, 1 if the fence has
  605. * been signaled without an error condition, or a negative error code
  606. * if the fence has been completed in err.
  607. */
  608. int dma_fence_get_status(struct dma_fence *fence)
  609. {
  610. unsigned long flags;
  611. int status;
  612. spin_lock_irqsave(fence->lock, flags);
  613. status = dma_fence_get_status_locked(fence);
  614. spin_unlock_irqrestore(fence->lock, flags);
  615. return status;
  616. }
  617. EXPORT_SYMBOL(dma_fence_get_status);
  618. /**
  619. * dma_fence_remove_callback - remove a callback from the signaling list
  620. * @fence: the fence to wait on
  621. * @cb: the callback to remove
  622. *
  623. * Remove a previously queued callback from the fence. This function returns
  624. * true if the callback is successfully removed, or false if the fence has
  625. * already been signaled.
  626. *
  627. * *WARNING*:
  628. * Cancelling a callback should only be done if you really know what you're
  629. * doing, since deadlocks and race conditions could occur all too easily. For
  630. * this reason, it should only ever be done on hardware lockup recovery,
  631. * with a reference held to the fence.
  632. *
  633. * Behaviour is undefined if @cb has not been added to @fence using
  634. * dma_fence_add_callback() beforehand.
  635. */
  636. bool
  637. dma_fence_remove_callback(struct dma_fence *fence, struct dma_fence_cb *cb)
  638. {
  639. unsigned long flags;
  640. bool ret;
  641. spin_lock_irqsave(fence->lock, flags);
  642. ret = !list_empty(&cb->node);
  643. if (ret)
  644. list_del_init(&cb->node);
  645. spin_unlock_irqrestore(fence->lock, flags);
  646. return ret;
  647. }
  648. EXPORT_SYMBOL(dma_fence_remove_callback);
  649. struct default_wait_cb {
  650. struct dma_fence_cb base;
  651. struct task_struct *task;
  652. };
  653. static void
  654. dma_fence_default_wait_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
  655. {
  656. struct default_wait_cb *wait =
  657. container_of(cb, struct default_wait_cb, base);
  658. wake_up_state(wait->task, TASK_NORMAL);
  659. }
  660. /**
  661. * dma_fence_default_wait - default sleep until the fence gets signaled
  662. * or until timeout elapses
  663. * @fence: the fence to wait on
  664. * @intr: if true, do an interruptible wait
  665. * @timeout: timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
  666. *
  667. * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
  668. * remaining timeout in jiffies on success. If timeout is zero the value one is
  669. * returned if the fence is already signaled for consistency with other
  670. * functions taking a jiffies timeout.
  671. */
  672. signed long
  673. dma_fence_default_wait(struct dma_fence *fence, bool intr, signed long timeout)
  674. {
  675. struct default_wait_cb cb;
  676. unsigned long flags;
  677. signed long ret = timeout ? timeout : 1;
  678. spin_lock_irqsave(fence->lock, flags);
  679. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  680. goto out;
  681. if (intr && signal_pending(current)) {
  682. ret = -ERESTARTSYS;
  683. goto out;
  684. }
  685. if (!timeout) {
  686. ret = 0;
  687. goto out;
  688. }
  689. cb.base.func = dma_fence_default_wait_cb;
  690. cb.task = current;
  691. list_add(&cb.base.node, &fence->cb_list);
  692. while (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags) && ret > 0) {
  693. if (intr)
  694. __set_current_state(TASK_INTERRUPTIBLE);
  695. else
  696. __set_current_state(TASK_UNINTERRUPTIBLE);
  697. spin_unlock_irqrestore(fence->lock, flags);
  698. ret = schedule_timeout(ret);
  699. spin_lock_irqsave(fence->lock, flags);
  700. if (ret > 0 && intr && signal_pending(current))
  701. ret = -ERESTARTSYS;
  702. }
  703. if (!list_empty(&cb.base.node))
  704. list_del(&cb.base.node);
  705. __set_current_state(TASK_RUNNING);
  706. out:
  707. spin_unlock_irqrestore(fence->lock, flags);
  708. return ret;
  709. }
  710. EXPORT_SYMBOL(dma_fence_default_wait);
  711. static bool
  712. dma_fence_test_signaled_any(struct dma_fence **fences, uint32_t count,
  713. uint32_t *idx)
  714. {
  715. int i;
  716. for (i = 0; i < count; ++i) {
  717. struct dma_fence *fence = fences[i];
  718. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
  719. if (idx)
  720. *idx = i;
  721. return true;
  722. }
  723. }
  724. return false;
  725. }
  726. /**
  727. * dma_fence_wait_any_timeout - sleep until any fence gets signaled
  728. * or until timeout elapses
  729. * @fences: array of fences to wait on
  730. * @count: number of fences to wait on
  731. * @intr: if true, do an interruptible wait
  732. * @timeout: timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
  733. * @idx: used to store the first signaled fence index, meaningful only on
  734. * positive return
  735. *
  736. * Returns -EINVAL on custom fence wait implementation, -ERESTARTSYS if
  737. * interrupted, 0 if the wait timed out, or the remaining timeout in jiffies
  738. * on success.
  739. *
  740. * Synchronous waits for the first fence in the array to be signaled. The
  741. * caller needs to hold a reference to all fences in the array, otherwise a
  742. * fence might be freed before return, resulting in undefined behavior.
  743. *
  744. * See also dma_fence_wait() and dma_fence_wait_timeout().
  745. */
  746. signed long
  747. dma_fence_wait_any_timeout(struct dma_fence **fences, uint32_t count,
  748. bool intr, signed long timeout, uint32_t *idx)
  749. {
  750. struct default_wait_cb *cb;
  751. signed long ret = timeout;
  752. unsigned i;
  753. if (WARN_ON(!fences || !count || timeout < 0))
  754. return -EINVAL;
  755. if (timeout == 0) {
  756. for (i = 0; i < count; ++i)
  757. if (dma_fence_is_signaled(fences[i])) {
  758. if (idx)
  759. *idx = i;
  760. return 1;
  761. }
  762. return 0;
  763. }
  764. cb = kcalloc(count, sizeof(struct default_wait_cb), GFP_KERNEL);
  765. if (cb == NULL) {
  766. ret = -ENOMEM;
  767. goto err_free_cb;
  768. }
  769. for (i = 0; i < count; ++i) {
  770. struct dma_fence *fence = fences[i];
  771. cb[i].task = current;
  772. if (dma_fence_add_callback(fence, &cb[i].base,
  773. dma_fence_default_wait_cb)) {
  774. /* This fence is already signaled */
  775. if (idx)
  776. *idx = i;
  777. goto fence_rm_cb;
  778. }
  779. }
  780. while (ret > 0) {
  781. if (intr)
  782. set_current_state(TASK_INTERRUPTIBLE);
  783. else
  784. set_current_state(TASK_UNINTERRUPTIBLE);
  785. if (dma_fence_test_signaled_any(fences, count, idx))
  786. break;
  787. ret = schedule_timeout(ret);
  788. if (ret > 0 && intr && signal_pending(current))
  789. ret = -ERESTARTSYS;
  790. }
  791. __set_current_state(TASK_RUNNING);
  792. fence_rm_cb:
  793. while (i-- > 0)
  794. dma_fence_remove_callback(fences[i], &cb[i].base);
  795. err_free_cb:
  796. kfree(cb);
  797. return ret;
  798. }
  799. EXPORT_SYMBOL(dma_fence_wait_any_timeout);
  800. /**
  801. * dma_fence_describe - Dump fence describtion into seq_file
  802. * @fence: the 6fence to describe
  803. * @seq: the seq_file to put the textual description into
  804. *
  805. * Dump a textual description of the fence and it's state into the seq_file.
  806. */
  807. void dma_fence_describe(struct dma_fence *fence, struct seq_file *seq)
  808. {
  809. seq_printf(seq, "%s %s seq %llu %ssignalled\n",
  810. fence->ops->get_driver_name(fence),
  811. fence->ops->get_timeline_name(fence), fence->seqno,
  812. dma_fence_is_signaled(fence) ? "" : "un");
  813. }
  814. EXPORT_SYMBOL(dma_fence_describe);
  815. /**
  816. * dma_fence_init - Initialize a custom fence.
  817. * @fence: the fence to initialize
  818. * @ops: the dma_fence_ops for operations on this fence
  819. * @lock: the irqsafe spinlock to use for locking this fence
  820. * @context: the execution context this fence is run on
  821. * @seqno: a linear increasing sequence number for this context
  822. *
  823. * Initializes an allocated fence, the caller doesn't have to keep its
  824. * refcount after committing with this fence, but it will need to hold a
  825. * refcount again if &dma_fence_ops.enable_signaling gets called.
  826. *
  827. * context and seqno are used for easy comparison between fences, allowing
  828. * to check which fence is later by simply using dma_fence_later().
  829. */
  830. void
  831. dma_fence_init(struct dma_fence *fence, const struct dma_fence_ops *ops,
  832. spinlock_t *lock, u64 context, u64 seqno)
  833. {
  834. BUG_ON(!lock);
  835. BUG_ON(!ops || !ops->get_driver_name || !ops->get_timeline_name);
  836. kref_init(&fence->refcount);
  837. fence->ops = ops;
  838. INIT_LIST_HEAD(&fence->cb_list);
  839. fence->lock = lock;
  840. fence->context = context;
  841. fence->seqno = seqno;
  842. fence->flags = 0UL;
  843. fence->error = 0;
  844. trace_dma_fence_init(fence);
  845. }
  846. EXPORT_SYMBOL(dma_fence_init);