nouveau_fence.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. fence->channel = NULL;
  40. list_del(&fence->head);
  41. nouveau_fence_unref(&fence);
  42. }
  43. spin_unlock(&fctx->lock);
  44. }
  45. void
  46. nouveau_fence_context_new(struct nouveau_fence_chan *fctx)
  47. {
  48. INIT_LIST_HEAD(&fctx->flip);
  49. INIT_LIST_HEAD(&fctx->pending);
  50. spin_lock_init(&fctx->lock);
  51. }
  52. static void
  53. nouveau_fence_update(struct nouveau_channel *chan)
  54. {
  55. struct nouveau_fence_chan *fctx = chan->fence;
  56. struct nouveau_fence *fence, *fnext;
  57. spin_lock(&fctx->lock);
  58. list_for_each_entry_safe(fence, fnext, &fctx->pending, head) {
  59. if (fctx->read(chan) < fence->sequence)
  60. break;
  61. fence->channel = NULL;
  62. list_del(&fence->head);
  63. nouveau_fence_unref(&fence);
  64. }
  65. spin_unlock(&fctx->lock);
  66. }
  67. int
  68. nouveau_fence_emit(struct nouveau_fence *fence, struct nouveau_channel *chan)
  69. {
  70. struct nouveau_fence_chan *fctx = chan->fence;
  71. int ret;
  72. fence->channel = chan;
  73. fence->timeout = jiffies + (3 * DRM_HZ);
  74. fence->sequence = ++fctx->sequence;
  75. ret = fctx->emit(fence);
  76. if (!ret) {
  77. kref_get(&fence->kref);
  78. spin_lock(&fctx->lock);
  79. list_add_tail(&fence->head, &fctx->pending);
  80. spin_unlock(&fctx->lock);
  81. }
  82. return ret;
  83. }
  84. bool
  85. nouveau_fence_done(struct nouveau_fence *fence)
  86. {
  87. if (fence->channel)
  88. nouveau_fence_update(fence->channel);
  89. return !fence->channel;
  90. }
  91. struct nouveau_fence_uevent {
  92. struct nouveau_eventh handler;
  93. struct nouveau_fence_priv *priv;
  94. };
  95. static int
  96. nouveau_fence_wait_uevent_handler(struct nouveau_eventh *event, int index)
  97. {
  98. struct nouveau_fence_uevent *uevent =
  99. container_of(event, struct nouveau_fence_uevent, handler);
  100. wake_up_all(&uevent->priv->waiting);
  101. return NVKM_EVENT_KEEP;
  102. }
  103. static int
  104. nouveau_fence_wait_uevent(struct nouveau_fence *fence, bool intr)
  105. {
  106. struct nouveau_channel *chan = fence->channel;
  107. struct nouveau_fifo *pfifo = nouveau_fifo(chan->drm->device);
  108. struct nouveau_fence_priv *priv = chan->drm->fence;
  109. struct nouveau_fence_uevent uevent = {
  110. .handler.func = nouveau_fence_wait_uevent_handler,
  111. .priv = priv,
  112. };
  113. int ret = 0;
  114. nouveau_event_get(pfifo->uevent, 0, &uevent.handler);
  115. if (fence->timeout) {
  116. unsigned long timeout = fence->timeout - jiffies;
  117. if (time_before(jiffies, fence->timeout)) {
  118. if (intr) {
  119. ret = wait_event_interruptible_timeout(
  120. priv->waiting,
  121. nouveau_fence_done(fence),
  122. timeout);
  123. } else {
  124. ret = wait_event_timeout(priv->waiting,
  125. nouveau_fence_done(fence),
  126. timeout);
  127. }
  128. }
  129. if (ret >= 0) {
  130. fence->timeout = jiffies + ret;
  131. if (time_after_eq(jiffies, fence->timeout))
  132. ret = -EBUSY;
  133. }
  134. } else {
  135. if (intr) {
  136. ret = wait_event_interruptible(priv->waiting,
  137. nouveau_fence_done(fence));
  138. } else {
  139. wait_event(priv->waiting, nouveau_fence_done(fence));
  140. }
  141. }
  142. nouveau_event_put(pfifo->uevent, 0, &uevent.handler);
  143. if (unlikely(ret < 0))
  144. return ret;
  145. return 0;
  146. }
  147. int
  148. nouveau_fence_wait(struct nouveau_fence *fence, bool lazy, bool intr)
  149. {
  150. struct nouveau_channel *chan = fence->channel;
  151. struct nouveau_fence_priv *priv = chan ? chan->drm->fence : NULL;
  152. unsigned long sleep_time = NSEC_PER_MSEC / 1000;
  153. ktime_t t;
  154. int ret = 0;
  155. while (priv && priv->uevent && lazy && !nouveau_fence_done(fence)) {
  156. ret = nouveau_fence_wait_uevent(fence, intr);
  157. if (ret < 0)
  158. return ret;
  159. }
  160. while (!nouveau_fence_done(fence)) {
  161. if (fence->timeout && time_after_eq(jiffies, fence->timeout)) {
  162. ret = -EBUSY;
  163. break;
  164. }
  165. __set_current_state(intr ? TASK_INTERRUPTIBLE :
  166. TASK_UNINTERRUPTIBLE);
  167. if (lazy) {
  168. t = ktime_set(0, sleep_time);
  169. schedule_hrtimeout(&t, HRTIMER_MODE_REL);
  170. sleep_time *= 2;
  171. if (sleep_time > NSEC_PER_MSEC)
  172. sleep_time = NSEC_PER_MSEC;
  173. }
  174. if (intr && signal_pending(current)) {
  175. ret = -ERESTARTSYS;
  176. break;
  177. }
  178. }
  179. __set_current_state(TASK_RUNNING);
  180. return ret;
  181. }
  182. int
  183. nouveau_fence_sync(struct nouveau_fence *fence, struct nouveau_channel *chan)
  184. {
  185. struct nouveau_fence_chan *fctx = chan->fence;
  186. struct nouveau_channel *prev;
  187. int ret = 0;
  188. prev = fence ? fence->channel : NULL;
  189. if (prev) {
  190. if (unlikely(prev != chan && !nouveau_fence_done(fence))) {
  191. ret = fctx->sync(fence, prev, chan);
  192. if (unlikely(ret))
  193. ret = nouveau_fence_wait(fence, true, false);
  194. }
  195. }
  196. return ret;
  197. }
  198. static void
  199. nouveau_fence_del(struct kref *kref)
  200. {
  201. struct nouveau_fence *fence = container_of(kref, typeof(*fence), kref);
  202. kfree(fence);
  203. }
  204. void
  205. nouveau_fence_unref(struct nouveau_fence **pfence)
  206. {
  207. if (*pfence)
  208. kref_put(&(*pfence)->kref, nouveau_fence_del);
  209. *pfence = NULL;
  210. }
  211. struct nouveau_fence *
  212. nouveau_fence_ref(struct nouveau_fence *fence)
  213. {
  214. kref_get(&fence->kref);
  215. return fence;
  216. }
  217. int
  218. nouveau_fence_new(struct nouveau_channel *chan, bool sysmem,
  219. struct nouveau_fence **pfence)
  220. {
  221. struct nouveau_fence *fence;
  222. int ret = 0;
  223. if (unlikely(!chan->fence))
  224. return -ENODEV;
  225. fence = kzalloc(sizeof(*fence), GFP_KERNEL);
  226. if (!fence)
  227. return -ENOMEM;
  228. fence->sysmem = sysmem;
  229. kref_init(&fence->kref);
  230. ret = nouveau_fence_emit(fence, chan);
  231. if (ret)
  232. nouveau_fence_unref(&fence);
  233. *pfence = fence;
  234. return ret;
  235. }