nouveau_fence.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. void
  33. nouveau_fence_context_del(struct nouveau_fence_chan *fctx)
  34. {
  35. struct nouveau_fence *fence, *fnext;
  36. spin_lock(&fctx->lock);
  37. list_for_each_entry_safe(fence, fnext, &fctx->pending, head) {
  38. if (fence->work)
  39. fence->work(fence->priv, false);
  40. fence->channel = NULL;
  41. list_del(&fence->head);
  42. nouveau_fence_unref(&fence);
  43. }
  44. spin_unlock(&fctx->lock);
  45. }
  46. void
  47. nouveau_fence_context_new(struct nouveau_fence_chan *fctx)
  48. {
  49. INIT_LIST_HEAD(&fctx->flip);
  50. INIT_LIST_HEAD(&fctx->pending);
  51. spin_lock_init(&fctx->lock);
  52. }
  53. static void
  54. nouveau_fence_update(struct nouveau_channel *chan)
  55. {
  56. struct nouveau_fence_priv *priv = chan->drm->fence;
  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 (priv->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_priv *priv = chan->drm->fence;
  75. struct nouveau_fence_chan *fctx = chan->fence;
  76. int ret;
  77. fence->channel = chan;
  78. fence->timeout = jiffies + (3 * DRM_HZ);
  79. fence->sequence = ++fctx->sequence;
  80. ret = priv->emit(fence);
  81. if (!ret) {
  82. kref_get(&fence->kref);
  83. spin_lock(&fctx->lock);
  84. list_add_tail(&fence->head, &fctx->pending);
  85. spin_unlock(&fctx->lock);
  86. }
  87. return ret;
  88. }
  89. bool
  90. nouveau_fence_done(struct nouveau_fence *fence)
  91. {
  92. if (fence->channel)
  93. nouveau_fence_update(fence->channel);
  94. return !fence->channel;
  95. }
  96. int
  97. nouveau_fence_wait(struct nouveau_fence *fence, bool lazy, bool intr)
  98. {
  99. unsigned long sleep_time = NSEC_PER_MSEC / 1000;
  100. ktime_t t;
  101. int ret = 0;
  102. while (!nouveau_fence_done(fence)) {
  103. if (fence->timeout && time_after_eq(jiffies, fence->timeout)) {
  104. ret = -EBUSY;
  105. break;
  106. }
  107. __set_current_state(intr ? TASK_INTERRUPTIBLE :
  108. TASK_UNINTERRUPTIBLE);
  109. if (lazy) {
  110. t = ktime_set(0, sleep_time);
  111. schedule_hrtimeout(&t, HRTIMER_MODE_REL);
  112. sleep_time *= 2;
  113. if (sleep_time > NSEC_PER_MSEC)
  114. sleep_time = NSEC_PER_MSEC;
  115. }
  116. if (intr && signal_pending(current)) {
  117. ret = -ERESTARTSYS;
  118. break;
  119. }
  120. }
  121. __set_current_state(TASK_RUNNING);
  122. return ret;
  123. }
  124. int
  125. nouveau_fence_sync(struct nouveau_fence *fence, struct nouveau_channel *chan)
  126. {
  127. struct nouveau_fence_priv *priv = chan->drm->fence;
  128. struct nouveau_channel *prev;
  129. int ret = 0;
  130. prev = fence ? fence->channel : NULL;
  131. if (prev) {
  132. if (unlikely(prev != chan && !nouveau_fence_done(fence))) {
  133. ret = priv->sync(fence, prev, chan);
  134. if (unlikely(ret))
  135. ret = nouveau_fence_wait(fence, true, false);
  136. }
  137. }
  138. return ret;
  139. }
  140. static void
  141. nouveau_fence_del(struct kref *kref)
  142. {
  143. struct nouveau_fence *fence = container_of(kref, typeof(*fence), kref);
  144. kfree(fence);
  145. }
  146. void
  147. nouveau_fence_unref(struct nouveau_fence **pfence)
  148. {
  149. if (*pfence)
  150. kref_put(&(*pfence)->kref, nouveau_fence_del);
  151. *pfence = NULL;
  152. }
  153. struct nouveau_fence *
  154. nouveau_fence_ref(struct nouveau_fence *fence)
  155. {
  156. kref_get(&fence->kref);
  157. return fence;
  158. }
  159. int
  160. nouveau_fence_new(struct nouveau_channel *chan, struct nouveau_fence **pfence)
  161. {
  162. struct nouveau_fence *fence;
  163. int ret = 0;
  164. if (unlikely(!chan->fence))
  165. return -ENODEV;
  166. fence = kzalloc(sizeof(*fence), GFP_KERNEL);
  167. if (!fence)
  168. return -ENOMEM;
  169. kref_init(&fence->kref);
  170. if (chan) {
  171. ret = nouveau_fence_emit(fence, chan);
  172. if (ret)
  173. nouveau_fence_unref(&fence);
  174. }
  175. *pfence = fence;
  176. return ret;
  177. }