nouveau_fence.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Copyright (C) 2007 Ben Skeggs.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the
  14. * next paragraph) shall be included in all copies or substantial
  15. * portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. */
  26. #include <drm/drmP.h>
  27. #include <linux/ktime.h>
  28. #include <linux/hrtimer.h>
  29. #include "nouveau_drm.h"
  30. #include "nouveau_dma.h"
  31. #include "nouveau_fence.h"
  32. #include <engine/fifo.h>
  33. void
  34. nouveau_fence_context_del(struct nouveau_fence_chan *fctx)
  35. {
  36. struct nouveau_fence *fence, *fnext;
  37. spin_lock(&fctx->lock);
  38. list_for_each_entry_safe(fence, fnext, &fctx->pending, head) {
  39. if (fence->work)
  40. fence->work(fence->priv, false);
  41. fence->channel = NULL;
  42. list_del(&fence->head);
  43. nouveau_fence_unref(&fence);
  44. }
  45. spin_unlock(&fctx->lock);
  46. }
  47. void
  48. nouveau_fence_context_new(struct nouveau_fence_chan *fctx)
  49. {
  50. INIT_LIST_HEAD(&fctx->flip);
  51. INIT_LIST_HEAD(&fctx->pending);
  52. spin_lock_init(&fctx->lock);
  53. }
  54. static void
  55. nouveau_fence_update(struct nouveau_channel *chan)
  56. {
  57. struct nouveau_fence_chan *fctx = chan->fence;
  58. struct nouveau_fence *fence, *fnext;
  59. spin_lock(&fctx->lock);
  60. list_for_each_entry_safe(fence, fnext, &fctx->pending, head) {
  61. if (fctx->read(chan) < fence->sequence)
  62. break;
  63. if (fence->work)
  64. fence->work(fence->priv, true);
  65. fence->channel = NULL;
  66. list_del(&fence->head);
  67. nouveau_fence_unref(&fence);
  68. }
  69. spin_unlock(&fctx->lock);
  70. }
  71. int
  72. nouveau_fence_emit(struct nouveau_fence *fence, struct nouveau_channel *chan)
  73. {
  74. struct nouveau_fence_chan *fctx = chan->fence;
  75. int ret;
  76. fence->channel = chan;
  77. fence->timeout = jiffies + (3 * DRM_HZ);
  78. fence->sequence = ++fctx->sequence;
  79. ret = fctx->emit(fence);
  80. if (!ret) {
  81. kref_get(&fence->kref);
  82. spin_lock(&fctx->lock);
  83. list_add_tail(&fence->head, &fctx->pending);
  84. spin_unlock(&fctx->lock);
  85. }
  86. return ret;
  87. }
  88. bool
  89. nouveau_fence_done(struct nouveau_fence *fence)
  90. {
  91. if (fence->channel)
  92. nouveau_fence_update(fence->channel);
  93. return !fence->channel;
  94. }
  95. struct nouveau_fence_uevent {
  96. struct nouveau_eventh handler;
  97. struct nouveau_fence_priv *priv;
  98. };
  99. static int
  100. nouveau_fence_wait_uevent_handler(struct nouveau_eventh *event, int index)
  101. {
  102. struct nouveau_fence_uevent *uevent =
  103. container_of(event, struct nouveau_fence_uevent, handler);
  104. wake_up_all(&uevent->priv->waiting);
  105. return NVKM_EVENT_KEEP;
  106. }
  107. static int
  108. nouveau_fence_wait_uevent(struct nouveau_fence *fence, bool intr)
  109. {
  110. struct nouveau_channel *chan = fence->channel;
  111. struct nouveau_fifo *pfifo = nouveau_fifo(chan->drm->device);
  112. struct nouveau_fence_priv *priv = chan->drm->fence;
  113. struct nouveau_fence_uevent uevent = {
  114. .handler.func = nouveau_fence_wait_uevent_handler,
  115. .priv = priv,
  116. };
  117. int ret = 0;
  118. nouveau_event_get(pfifo->uevent, 0, &uevent.handler);
  119. if (fence->timeout) {
  120. unsigned long timeout = fence->timeout - jiffies;
  121. if (time_before(jiffies, fence->timeout)) {
  122. if (intr) {
  123. ret = wait_event_interruptible_timeout(
  124. priv->waiting,
  125. nouveau_fence_done(fence),
  126. timeout);
  127. } else {
  128. ret = wait_event_timeout(priv->waiting,
  129. nouveau_fence_done(fence),
  130. timeout);
  131. }
  132. }
  133. if (ret >= 0) {
  134. fence->timeout = jiffies + ret;
  135. if (time_after_eq(jiffies, fence->timeout))
  136. ret = -EBUSY;
  137. }
  138. } else {
  139. if (intr) {
  140. ret = wait_event_interruptible(priv->waiting,
  141. nouveau_fence_done(fence));
  142. } else {
  143. wait_event(priv->waiting, nouveau_fence_done(fence));
  144. }
  145. }
  146. nouveau_event_put(pfifo->uevent, 0, &uevent.handler);
  147. if (unlikely(ret < 0))
  148. return ret;
  149. return 0;
  150. }
  151. int
  152. nouveau_fence_wait(struct nouveau_fence *fence, bool lazy, bool intr)
  153. {
  154. struct nouveau_channel *chan = fence->channel;
  155. struct nouveau_fence_priv *priv = chan ? chan->drm->fence : NULL;
  156. unsigned long sleep_time = NSEC_PER_MSEC / 1000;
  157. ktime_t t;
  158. int ret = 0;
  159. while (priv && priv->uevent && lazy && !nouveau_fence_done(fence)) {
  160. ret = nouveau_fence_wait_uevent(fence, intr);
  161. if (ret < 0)
  162. return ret;
  163. }
  164. while (!nouveau_fence_done(fence)) {
  165. if (fence->timeout && time_after_eq(jiffies, fence->timeout)) {
  166. ret = -EBUSY;
  167. break;
  168. }
  169. __set_current_state(intr ? TASK_INTERRUPTIBLE :
  170. TASK_UNINTERRUPTIBLE);
  171. if (lazy) {
  172. t = ktime_set(0, sleep_time);
  173. schedule_hrtimeout(&t, HRTIMER_MODE_REL);
  174. sleep_time *= 2;
  175. if (sleep_time > NSEC_PER_MSEC)
  176. sleep_time = NSEC_PER_MSEC;
  177. }
  178. if (intr && signal_pending(current)) {
  179. ret = -ERESTARTSYS;
  180. break;
  181. }
  182. }
  183. __set_current_state(TASK_RUNNING);
  184. return ret;
  185. }
  186. int
  187. nouveau_fence_sync(struct nouveau_fence *fence, struct nouveau_channel *chan)
  188. {
  189. struct nouveau_fence_chan *fctx = chan->fence;
  190. struct nouveau_channel *prev;
  191. int ret = 0;
  192. prev = fence ? fence->channel : NULL;
  193. if (prev) {
  194. if (unlikely(prev != chan && !nouveau_fence_done(fence))) {
  195. ret = fctx->sync(fence, prev, chan);
  196. if (unlikely(ret))
  197. ret = nouveau_fence_wait(fence, true, false);
  198. }
  199. }
  200. return ret;
  201. }
  202. static void
  203. nouveau_fence_del(struct kref *kref)
  204. {
  205. struct nouveau_fence *fence = container_of(kref, typeof(*fence), kref);
  206. kfree(fence);
  207. }
  208. void
  209. nouveau_fence_unref(struct nouveau_fence **pfence)
  210. {
  211. if (*pfence)
  212. kref_put(&(*pfence)->kref, nouveau_fence_del);
  213. *pfence = NULL;
  214. }
  215. struct nouveau_fence *
  216. nouveau_fence_ref(struct nouveau_fence *fence)
  217. {
  218. kref_get(&fence->kref);
  219. return fence;
  220. }
  221. int
  222. nouveau_fence_new(struct nouveau_channel *chan, struct nouveau_fence **pfence)
  223. {
  224. struct nouveau_fence *fence;
  225. int ret = 0;
  226. if (unlikely(!chan->fence))
  227. return -ENODEV;
  228. fence = kzalloc(sizeof(*fence), GFP_KERNEL);
  229. if (!fence)
  230. return -ENOMEM;
  231. kref_init(&fence->kref);
  232. ret = nouveau_fence_emit(fence, chan);
  233. if (ret)
  234. nouveau_fence_unref(&fence);
  235. *pfence = fence;
  236. return ret;
  237. }