i915_sw_fence.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. * (C) Copyright 2016 Intel Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; version 2
  7. * of the License.
  8. */
  9. #include <linux/slab.h>
  10. #include <linux/dma-fence.h>
  11. #include <linux/reservation.h>
  12. #include "i915_sw_fence.h"
  13. #include "i915_selftest.h"
  14. #define I915_SW_FENCE_FLAG_ALLOC BIT(3) /* after WQ_FLAG_* for safety */
  15. static DEFINE_SPINLOCK(i915_sw_fence_lock);
  16. enum {
  17. DEBUG_FENCE_IDLE = 0,
  18. DEBUG_FENCE_NOTIFY,
  19. };
  20. #ifdef CONFIG_DRM_I915_SW_FENCE_DEBUG_OBJECTS
  21. static void *i915_sw_fence_debug_hint(void *addr)
  22. {
  23. return (void *)(((struct i915_sw_fence *)addr)->flags & I915_SW_FENCE_MASK);
  24. }
  25. static struct debug_obj_descr i915_sw_fence_debug_descr = {
  26. .name = "i915_sw_fence",
  27. .debug_hint = i915_sw_fence_debug_hint,
  28. };
  29. static inline void debug_fence_init(struct i915_sw_fence *fence)
  30. {
  31. debug_object_init(fence, &i915_sw_fence_debug_descr);
  32. }
  33. static inline void debug_fence_activate(struct i915_sw_fence *fence)
  34. {
  35. debug_object_activate(fence, &i915_sw_fence_debug_descr);
  36. }
  37. static inline void debug_fence_set_state(struct i915_sw_fence *fence,
  38. int old, int new)
  39. {
  40. debug_object_active_state(fence, &i915_sw_fence_debug_descr, old, new);
  41. }
  42. static inline void debug_fence_deactivate(struct i915_sw_fence *fence)
  43. {
  44. debug_object_deactivate(fence, &i915_sw_fence_debug_descr);
  45. }
  46. static inline void debug_fence_destroy(struct i915_sw_fence *fence)
  47. {
  48. debug_object_destroy(fence, &i915_sw_fence_debug_descr);
  49. }
  50. static inline void debug_fence_free(struct i915_sw_fence *fence)
  51. {
  52. debug_object_free(fence, &i915_sw_fence_debug_descr);
  53. smp_wmb(); /* flush the change in state before reallocation */
  54. }
  55. static inline void debug_fence_assert(struct i915_sw_fence *fence)
  56. {
  57. debug_object_assert_init(fence, &i915_sw_fence_debug_descr);
  58. }
  59. #else
  60. static inline void debug_fence_init(struct i915_sw_fence *fence)
  61. {
  62. }
  63. static inline void debug_fence_activate(struct i915_sw_fence *fence)
  64. {
  65. }
  66. static inline void debug_fence_set_state(struct i915_sw_fence *fence,
  67. int old, int new)
  68. {
  69. }
  70. static inline void debug_fence_deactivate(struct i915_sw_fence *fence)
  71. {
  72. }
  73. static inline void debug_fence_destroy(struct i915_sw_fence *fence)
  74. {
  75. }
  76. static inline void debug_fence_free(struct i915_sw_fence *fence)
  77. {
  78. }
  79. static inline void debug_fence_assert(struct i915_sw_fence *fence)
  80. {
  81. }
  82. #endif
  83. static int __i915_sw_fence_notify(struct i915_sw_fence *fence,
  84. enum i915_sw_fence_notify state)
  85. {
  86. i915_sw_fence_notify_t fn;
  87. fn = (i915_sw_fence_notify_t)(fence->flags & I915_SW_FENCE_MASK);
  88. return fn(fence, state);
  89. }
  90. #ifdef CONFIG_DRM_I915_SW_FENCE_DEBUG_OBJECTS
  91. void i915_sw_fence_fini(struct i915_sw_fence *fence)
  92. {
  93. debug_fence_free(fence);
  94. }
  95. #endif
  96. static void __i915_sw_fence_wake_up_all(struct i915_sw_fence *fence,
  97. struct list_head *continuation)
  98. {
  99. wait_queue_head_t *x = &fence->wait;
  100. wait_queue_entry_t *pos, *next;
  101. unsigned long flags;
  102. debug_fence_deactivate(fence);
  103. atomic_set_release(&fence->pending, -1); /* 0 -> -1 [done] */
  104. /*
  105. * To prevent unbounded recursion as we traverse the graph of
  106. * i915_sw_fences, we move the entry list from this, the next ready
  107. * fence, to the tail of the original fence's entry list
  108. * (and so added to the list to be woken).
  109. */
  110. spin_lock_irqsave_nested(&x->lock, flags, 1 + !!continuation);
  111. if (continuation) {
  112. list_for_each_entry_safe(pos, next, &x->head, entry) {
  113. if (pos->func == autoremove_wake_function)
  114. pos->func(pos, TASK_NORMAL, 0, continuation);
  115. else
  116. list_move_tail(&pos->entry, continuation);
  117. }
  118. } else {
  119. LIST_HEAD(extra);
  120. do {
  121. list_for_each_entry_safe(pos, next, &x->head, entry)
  122. pos->func(pos, TASK_NORMAL, 0, &extra);
  123. if (list_empty(&extra))
  124. break;
  125. list_splice_tail_init(&extra, &x->head);
  126. } while (1);
  127. }
  128. spin_unlock_irqrestore(&x->lock, flags);
  129. debug_fence_assert(fence);
  130. }
  131. static void __i915_sw_fence_complete(struct i915_sw_fence *fence,
  132. struct list_head *continuation)
  133. {
  134. debug_fence_assert(fence);
  135. if (!atomic_dec_and_test(&fence->pending))
  136. return;
  137. debug_fence_set_state(fence, DEBUG_FENCE_IDLE, DEBUG_FENCE_NOTIFY);
  138. if (__i915_sw_fence_notify(fence, FENCE_COMPLETE) != NOTIFY_DONE)
  139. return;
  140. debug_fence_set_state(fence, DEBUG_FENCE_NOTIFY, DEBUG_FENCE_IDLE);
  141. __i915_sw_fence_wake_up_all(fence, continuation);
  142. debug_fence_destroy(fence);
  143. __i915_sw_fence_notify(fence, FENCE_FREE);
  144. }
  145. static void i915_sw_fence_complete(struct i915_sw_fence *fence)
  146. {
  147. debug_fence_assert(fence);
  148. if (WARN_ON(i915_sw_fence_done(fence)))
  149. return;
  150. __i915_sw_fence_complete(fence, NULL);
  151. }
  152. static void i915_sw_fence_await(struct i915_sw_fence *fence)
  153. {
  154. debug_fence_assert(fence);
  155. WARN_ON(atomic_inc_return(&fence->pending) <= 1);
  156. }
  157. void __i915_sw_fence_init(struct i915_sw_fence *fence,
  158. i915_sw_fence_notify_t fn,
  159. const char *name,
  160. struct lock_class_key *key)
  161. {
  162. BUG_ON(!fn || (unsigned long)fn & ~I915_SW_FENCE_MASK);
  163. debug_fence_init(fence);
  164. __init_waitqueue_head(&fence->wait, name, key);
  165. atomic_set(&fence->pending, 1);
  166. fence->flags = (unsigned long)fn;
  167. }
  168. void i915_sw_fence_commit(struct i915_sw_fence *fence)
  169. {
  170. debug_fence_activate(fence);
  171. i915_sw_fence_complete(fence);
  172. }
  173. static int i915_sw_fence_wake(wait_queue_entry_t *wq, unsigned mode, int flags, void *key)
  174. {
  175. list_del(&wq->entry);
  176. __i915_sw_fence_complete(wq->private, key);
  177. if (wq->flags & I915_SW_FENCE_FLAG_ALLOC)
  178. kfree(wq);
  179. return 0;
  180. }
  181. static bool __i915_sw_fence_check_if_after(struct i915_sw_fence *fence,
  182. const struct i915_sw_fence * const signaler)
  183. {
  184. wait_queue_entry_t *wq;
  185. if (__test_and_set_bit(I915_SW_FENCE_CHECKED_BIT, &fence->flags))
  186. return false;
  187. if (fence == signaler)
  188. return true;
  189. list_for_each_entry(wq, &fence->wait.head, entry) {
  190. if (wq->func != i915_sw_fence_wake)
  191. continue;
  192. if (__i915_sw_fence_check_if_after(wq->private, signaler))
  193. return true;
  194. }
  195. return false;
  196. }
  197. static void __i915_sw_fence_clear_checked_bit(struct i915_sw_fence *fence)
  198. {
  199. wait_queue_entry_t *wq;
  200. if (!__test_and_clear_bit(I915_SW_FENCE_CHECKED_BIT, &fence->flags))
  201. return;
  202. list_for_each_entry(wq, &fence->wait.head, entry) {
  203. if (wq->func != i915_sw_fence_wake)
  204. continue;
  205. __i915_sw_fence_clear_checked_bit(wq->private);
  206. }
  207. }
  208. static bool i915_sw_fence_check_if_after(struct i915_sw_fence *fence,
  209. const struct i915_sw_fence * const signaler)
  210. {
  211. unsigned long flags;
  212. bool err;
  213. if (!IS_ENABLED(CONFIG_DRM_I915_SW_FENCE_CHECK_DAG))
  214. return false;
  215. spin_lock_irqsave(&i915_sw_fence_lock, flags);
  216. err = __i915_sw_fence_check_if_after(fence, signaler);
  217. __i915_sw_fence_clear_checked_bit(fence);
  218. spin_unlock_irqrestore(&i915_sw_fence_lock, flags);
  219. return err;
  220. }
  221. static int __i915_sw_fence_await_sw_fence(struct i915_sw_fence *fence,
  222. struct i915_sw_fence *signaler,
  223. wait_queue_entry_t *wq, gfp_t gfp)
  224. {
  225. unsigned long flags;
  226. int pending;
  227. debug_fence_assert(fence);
  228. if (i915_sw_fence_done(signaler))
  229. return 0;
  230. debug_fence_assert(signaler);
  231. /* The dependency graph must be acyclic. */
  232. if (unlikely(i915_sw_fence_check_if_after(fence, signaler)))
  233. return -EINVAL;
  234. pending = 0;
  235. if (!wq) {
  236. wq = kmalloc(sizeof(*wq), gfp);
  237. if (!wq) {
  238. if (!gfpflags_allow_blocking(gfp))
  239. return -ENOMEM;
  240. i915_sw_fence_wait(signaler);
  241. return 0;
  242. }
  243. pending |= I915_SW_FENCE_FLAG_ALLOC;
  244. }
  245. INIT_LIST_HEAD(&wq->entry);
  246. wq->flags = pending;
  247. wq->func = i915_sw_fence_wake;
  248. wq->private = fence;
  249. i915_sw_fence_await(fence);
  250. spin_lock_irqsave(&signaler->wait.lock, flags);
  251. if (likely(!i915_sw_fence_done(signaler))) {
  252. __add_wait_queue_entry_tail(&signaler->wait, wq);
  253. pending = 1;
  254. } else {
  255. i915_sw_fence_wake(wq, 0, 0, NULL);
  256. pending = 0;
  257. }
  258. spin_unlock_irqrestore(&signaler->wait.lock, flags);
  259. return pending;
  260. }
  261. int i915_sw_fence_await_sw_fence(struct i915_sw_fence *fence,
  262. struct i915_sw_fence *signaler,
  263. wait_queue_entry_t *wq)
  264. {
  265. return __i915_sw_fence_await_sw_fence(fence, signaler, wq, 0);
  266. }
  267. int i915_sw_fence_await_sw_fence_gfp(struct i915_sw_fence *fence,
  268. struct i915_sw_fence *signaler,
  269. gfp_t gfp)
  270. {
  271. return __i915_sw_fence_await_sw_fence(fence, signaler, NULL, gfp);
  272. }
  273. struct i915_sw_dma_fence_cb {
  274. struct dma_fence_cb base;
  275. struct i915_sw_fence *fence;
  276. struct dma_fence *dma;
  277. struct timer_list timer;
  278. };
  279. static void timer_i915_sw_fence_wake(unsigned long data)
  280. {
  281. struct i915_sw_dma_fence_cb *cb = (struct i915_sw_dma_fence_cb *)data;
  282. pr_warn("asynchronous wait on fence %s:%s:%x timed out\n",
  283. cb->dma->ops->get_driver_name(cb->dma),
  284. cb->dma->ops->get_timeline_name(cb->dma),
  285. cb->dma->seqno);
  286. dma_fence_put(cb->dma);
  287. cb->dma = NULL;
  288. i915_sw_fence_complete(cb->fence);
  289. cb->timer.function = NULL;
  290. }
  291. static void dma_i915_sw_fence_wake(struct dma_fence *dma,
  292. struct dma_fence_cb *data)
  293. {
  294. struct i915_sw_dma_fence_cb *cb = container_of(data, typeof(*cb), base);
  295. del_timer_sync(&cb->timer);
  296. if (cb->timer.function)
  297. i915_sw_fence_complete(cb->fence);
  298. dma_fence_put(cb->dma);
  299. kfree(cb);
  300. }
  301. int i915_sw_fence_await_dma_fence(struct i915_sw_fence *fence,
  302. struct dma_fence *dma,
  303. unsigned long timeout,
  304. gfp_t gfp)
  305. {
  306. struct i915_sw_dma_fence_cb *cb;
  307. int ret;
  308. debug_fence_assert(fence);
  309. if (dma_fence_is_signaled(dma))
  310. return 0;
  311. cb = kmalloc(sizeof(*cb), gfp);
  312. if (!cb) {
  313. if (!gfpflags_allow_blocking(gfp))
  314. return -ENOMEM;
  315. return dma_fence_wait(dma, false);
  316. }
  317. cb->fence = fence;
  318. i915_sw_fence_await(fence);
  319. cb->dma = NULL;
  320. __setup_timer(&cb->timer,
  321. timer_i915_sw_fence_wake, (unsigned long)cb,
  322. TIMER_IRQSAFE);
  323. if (timeout) {
  324. cb->dma = dma_fence_get(dma);
  325. mod_timer(&cb->timer, round_jiffies_up(jiffies + timeout));
  326. }
  327. ret = dma_fence_add_callback(dma, &cb->base, dma_i915_sw_fence_wake);
  328. if (ret == 0) {
  329. ret = 1;
  330. } else {
  331. dma_i915_sw_fence_wake(dma, &cb->base);
  332. if (ret == -ENOENT) /* fence already signaled */
  333. ret = 0;
  334. }
  335. return ret;
  336. }
  337. int i915_sw_fence_await_reservation(struct i915_sw_fence *fence,
  338. struct reservation_object *resv,
  339. const struct dma_fence_ops *exclude,
  340. bool write,
  341. unsigned long timeout,
  342. gfp_t gfp)
  343. {
  344. struct dma_fence *excl;
  345. int ret = 0, pending;
  346. debug_fence_assert(fence);
  347. if (write) {
  348. struct dma_fence **shared;
  349. unsigned int count, i;
  350. ret = reservation_object_get_fences_rcu(resv,
  351. &excl, &count, &shared);
  352. if (ret)
  353. return ret;
  354. for (i = 0; i < count; i++) {
  355. if (shared[i]->ops == exclude)
  356. continue;
  357. pending = i915_sw_fence_await_dma_fence(fence,
  358. shared[i],
  359. timeout,
  360. gfp);
  361. if (pending < 0) {
  362. ret = pending;
  363. break;
  364. }
  365. ret |= pending;
  366. }
  367. for (i = 0; i < count; i++)
  368. dma_fence_put(shared[i]);
  369. kfree(shared);
  370. } else {
  371. excl = reservation_object_get_excl_rcu(resv);
  372. }
  373. if (ret >= 0 && excl && excl->ops != exclude) {
  374. pending = i915_sw_fence_await_dma_fence(fence,
  375. excl,
  376. timeout,
  377. gfp);
  378. if (pending < 0)
  379. ret = pending;
  380. else
  381. ret |= pending;
  382. }
  383. dma_fence_put(excl);
  384. return ret;
  385. }
  386. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  387. #include "selftests/i915_sw_fence.c"
  388. #endif