nouveau_fence.c 5.2 KB

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