i915_sw_fence.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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/fence.h>
  11. #include <linux/reservation.h>
  12. #include "i915_sw_fence.h"
  13. static DEFINE_SPINLOCK(i915_sw_fence_lock);
  14. static int __i915_sw_fence_notify(struct i915_sw_fence *fence,
  15. enum i915_sw_fence_notify state)
  16. {
  17. i915_sw_fence_notify_t fn;
  18. fn = (i915_sw_fence_notify_t)(fence->flags & I915_SW_FENCE_MASK);
  19. return fn(fence, state);
  20. }
  21. static void i915_sw_fence_free(struct kref *kref)
  22. {
  23. struct i915_sw_fence *fence = container_of(kref, typeof(*fence), kref);
  24. WARN_ON(atomic_read(&fence->pending) > 0);
  25. if (fence->flags & I915_SW_FENCE_MASK)
  26. __i915_sw_fence_notify(fence, FENCE_FREE);
  27. else
  28. kfree(fence);
  29. }
  30. static void i915_sw_fence_put(struct i915_sw_fence *fence)
  31. {
  32. kref_put(&fence->kref, i915_sw_fence_free);
  33. }
  34. static struct i915_sw_fence *i915_sw_fence_get(struct i915_sw_fence *fence)
  35. {
  36. kref_get(&fence->kref);
  37. return fence;
  38. }
  39. static void __i915_sw_fence_wake_up_all(struct i915_sw_fence *fence,
  40. struct list_head *continuation)
  41. {
  42. wait_queue_head_t *x = &fence->wait;
  43. wait_queue_t *pos, *next;
  44. unsigned long flags;
  45. atomic_set_release(&fence->pending, -1); /* 0 -> -1 [done] */
  46. /*
  47. * To prevent unbounded recursion as we traverse the graph of
  48. * i915_sw_fences, we move the task_list from this, the next ready
  49. * fence, to the tail of the original fence's task_list
  50. * (and so added to the list to be woken).
  51. */
  52. spin_lock_irqsave_nested(&x->lock, flags, 1 + !!continuation);
  53. if (continuation) {
  54. list_for_each_entry_safe(pos, next, &x->task_list, task_list) {
  55. if (pos->func == autoremove_wake_function)
  56. pos->func(pos, TASK_NORMAL, 0, continuation);
  57. else
  58. list_move_tail(&pos->task_list, continuation);
  59. }
  60. } else {
  61. LIST_HEAD(extra);
  62. do {
  63. list_for_each_entry_safe(pos, next,
  64. &x->task_list, task_list)
  65. pos->func(pos, TASK_NORMAL, 0, &extra);
  66. if (list_empty(&extra))
  67. break;
  68. list_splice_tail_init(&extra, &x->task_list);
  69. } while (1);
  70. }
  71. spin_unlock_irqrestore(&x->lock, flags);
  72. }
  73. static void __i915_sw_fence_complete(struct i915_sw_fence *fence,
  74. struct list_head *continuation)
  75. {
  76. if (!atomic_dec_and_test(&fence->pending))
  77. return;
  78. if (fence->flags & I915_SW_FENCE_MASK &&
  79. __i915_sw_fence_notify(fence, FENCE_COMPLETE) != NOTIFY_DONE)
  80. return;
  81. __i915_sw_fence_wake_up_all(fence, continuation);
  82. }
  83. static void i915_sw_fence_complete(struct i915_sw_fence *fence)
  84. {
  85. if (WARN_ON(i915_sw_fence_done(fence)))
  86. return;
  87. __i915_sw_fence_complete(fence, NULL);
  88. }
  89. static void i915_sw_fence_await(struct i915_sw_fence *fence)
  90. {
  91. WARN_ON(atomic_inc_return(&fence->pending) <= 1);
  92. }
  93. void i915_sw_fence_init(struct i915_sw_fence *fence, i915_sw_fence_notify_t fn)
  94. {
  95. BUG_ON((unsigned long)fn & ~I915_SW_FENCE_MASK);
  96. init_waitqueue_head(&fence->wait);
  97. kref_init(&fence->kref);
  98. atomic_set(&fence->pending, 1);
  99. fence->flags = (unsigned long)fn;
  100. }
  101. void i915_sw_fence_commit(struct i915_sw_fence *fence)
  102. {
  103. i915_sw_fence_complete(fence);
  104. i915_sw_fence_put(fence);
  105. }
  106. static int i915_sw_fence_wake(wait_queue_t *wq, unsigned mode, int flags, void *key)
  107. {
  108. list_del(&wq->task_list);
  109. __i915_sw_fence_complete(wq->private, key);
  110. i915_sw_fence_put(wq->private);
  111. return 0;
  112. }
  113. static bool __i915_sw_fence_check_if_after(struct i915_sw_fence *fence,
  114. const struct i915_sw_fence * const signaler)
  115. {
  116. wait_queue_t *wq;
  117. if (__test_and_set_bit(I915_SW_FENCE_CHECKED_BIT, &fence->flags))
  118. return false;
  119. if (fence == signaler)
  120. return true;
  121. list_for_each_entry(wq, &fence->wait.task_list, task_list) {
  122. if (wq->func != i915_sw_fence_wake)
  123. continue;
  124. if (__i915_sw_fence_check_if_after(wq->private, signaler))
  125. return true;
  126. }
  127. return false;
  128. }
  129. static void __i915_sw_fence_clear_checked_bit(struct i915_sw_fence *fence)
  130. {
  131. wait_queue_t *wq;
  132. if (!__test_and_clear_bit(I915_SW_FENCE_CHECKED_BIT, &fence->flags))
  133. return;
  134. list_for_each_entry(wq, &fence->wait.task_list, task_list) {
  135. if (wq->func != i915_sw_fence_wake)
  136. continue;
  137. __i915_sw_fence_clear_checked_bit(wq->private);
  138. }
  139. }
  140. static bool i915_sw_fence_check_if_after(struct i915_sw_fence *fence,
  141. const struct i915_sw_fence * const signaler)
  142. {
  143. unsigned long flags;
  144. bool err;
  145. if (!IS_ENABLED(CONFIG_I915_SW_FENCE_CHECK_DAG))
  146. return false;
  147. spin_lock_irqsave(&i915_sw_fence_lock, flags);
  148. err = __i915_sw_fence_check_if_after(fence, signaler);
  149. __i915_sw_fence_clear_checked_bit(fence);
  150. spin_unlock_irqrestore(&i915_sw_fence_lock, flags);
  151. return err;
  152. }
  153. int i915_sw_fence_await_sw_fence(struct i915_sw_fence *fence,
  154. struct i915_sw_fence *signaler,
  155. wait_queue_t *wq)
  156. {
  157. unsigned long flags;
  158. int pending;
  159. if (i915_sw_fence_done(signaler))
  160. return 0;
  161. /* The dependency graph must be acyclic. */
  162. if (unlikely(i915_sw_fence_check_if_after(fence, signaler)))
  163. return -EINVAL;
  164. INIT_LIST_HEAD(&wq->task_list);
  165. wq->flags = 0;
  166. wq->func = i915_sw_fence_wake;
  167. wq->private = i915_sw_fence_get(fence);
  168. i915_sw_fence_await(fence);
  169. spin_lock_irqsave(&signaler->wait.lock, flags);
  170. if (likely(!i915_sw_fence_done(signaler))) {
  171. __add_wait_queue_tail(&signaler->wait, wq);
  172. pending = 1;
  173. } else {
  174. i915_sw_fence_wake(wq, 0, 0, NULL);
  175. pending = 0;
  176. }
  177. spin_unlock_irqrestore(&signaler->wait.lock, flags);
  178. return pending;
  179. }
  180. struct dma_fence_cb {
  181. struct fence_cb base;
  182. struct i915_sw_fence *fence;
  183. struct fence *dma;
  184. struct timer_list timer;
  185. };
  186. static void timer_i915_sw_fence_wake(unsigned long data)
  187. {
  188. struct dma_fence_cb *cb = (struct dma_fence_cb *)data;
  189. printk(KERN_WARNING "asynchronous wait on fence %s:%s:%x timed out\n",
  190. cb->dma->ops->get_driver_name(cb->dma),
  191. cb->dma->ops->get_timeline_name(cb->dma),
  192. cb->dma->seqno);
  193. fence_put(cb->dma);
  194. cb->dma = NULL;
  195. i915_sw_fence_commit(cb->fence);
  196. cb->timer.function = NULL;
  197. }
  198. static void dma_i915_sw_fence_wake(struct fence *dma, struct fence_cb *data)
  199. {
  200. struct dma_fence_cb *cb = container_of(data, typeof(*cb), base);
  201. del_timer_sync(&cb->timer);
  202. if (cb->timer.function)
  203. i915_sw_fence_commit(cb->fence);
  204. fence_put(cb->dma);
  205. kfree(cb);
  206. }
  207. int i915_sw_fence_await_dma_fence(struct i915_sw_fence *fence,
  208. struct fence *dma,
  209. unsigned long timeout,
  210. gfp_t gfp)
  211. {
  212. struct dma_fence_cb *cb;
  213. int ret;
  214. if (fence_is_signaled(dma))
  215. return 0;
  216. cb = kmalloc(sizeof(*cb), gfp);
  217. if (!cb) {
  218. if (!gfpflags_allow_blocking(gfp))
  219. return -ENOMEM;
  220. return fence_wait(dma, false);
  221. }
  222. cb->fence = i915_sw_fence_get(fence);
  223. i915_sw_fence_await(fence);
  224. cb->dma = NULL;
  225. __setup_timer(&cb->timer,
  226. timer_i915_sw_fence_wake, (unsigned long)cb,
  227. TIMER_IRQSAFE);
  228. if (timeout) {
  229. cb->dma = fence_get(dma);
  230. mod_timer(&cb->timer, round_jiffies_up(jiffies + timeout));
  231. }
  232. ret = fence_add_callback(dma, &cb->base, dma_i915_sw_fence_wake);
  233. if (ret == 0) {
  234. ret = 1;
  235. } else {
  236. dma_i915_sw_fence_wake(dma, &cb->base);
  237. if (ret == -ENOENT) /* fence already signaled */
  238. ret = 0;
  239. }
  240. return ret;
  241. }
  242. int i915_sw_fence_await_reservation(struct i915_sw_fence *fence,
  243. struct reservation_object *resv,
  244. const struct fence_ops *exclude,
  245. bool write,
  246. unsigned long timeout,
  247. gfp_t gfp)
  248. {
  249. struct fence *excl;
  250. int ret = 0, pending;
  251. if (write) {
  252. struct fence **shared;
  253. unsigned int count, i;
  254. ret = reservation_object_get_fences_rcu(resv,
  255. &excl, &count, &shared);
  256. if (ret)
  257. return ret;
  258. for (i = 0; i < count; i++) {
  259. if (shared[i]->ops == exclude)
  260. continue;
  261. pending = i915_sw_fence_await_dma_fence(fence,
  262. shared[i],
  263. timeout,
  264. gfp);
  265. if (pending < 0) {
  266. ret = pending;
  267. break;
  268. }
  269. ret |= pending;
  270. }
  271. for (i = 0; i < count; i++)
  272. fence_put(shared[i]);
  273. kfree(shared);
  274. } else {
  275. excl = reservation_object_get_excl_rcu(resv);
  276. }
  277. if (ret >= 0 && excl && excl->ops != exclude) {
  278. pending = i915_sw_fence_await_dma_fence(fence,
  279. excl,
  280. timeout,
  281. gfp);
  282. if (pending < 0)
  283. ret = pending;
  284. else
  285. ret |= pending;
  286. }
  287. fence_put(excl);
  288. return ret;
  289. }