nouveau_fence.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 "nouveau_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 nouveau_fence_priv *priv = nv_engine(dev, NVOBJ_ENGINE_FENCE);
  60. struct nouveau_fence_chan *fctx = chan->engctx[NVOBJ_ENGINE_FENCE];
  61. struct nouveau_fence *fence, *fnext;
  62. spin_lock(&fctx->lock);
  63. list_for_each_entry_safe(fence, fnext, &fctx->pending, head) {
  64. if (priv->read(chan) < fence->sequence)
  65. break;
  66. if (fence->work)
  67. fence->work(fence->priv, true);
  68. fence->channel = NULL;
  69. list_del(&fence->head);
  70. nouveau_fence_unref(&fence);
  71. }
  72. spin_unlock(&fctx->lock);
  73. }
  74. int
  75. nouveau_fence_emit(struct nouveau_fence *fence, struct nouveau_channel *chan)
  76. {
  77. struct drm_device *dev = chan->dev;
  78. struct nouveau_fence_priv *priv = nv_engine(dev, NVOBJ_ENGINE_FENCE);
  79. struct nouveau_fence_chan *fctx = chan->engctx[NVOBJ_ENGINE_FENCE];
  80. int ret;
  81. fence->channel = chan;
  82. fence->timeout = jiffies + (3 * DRM_HZ);
  83. fence->sequence = ++fctx->sequence;
  84. ret = priv->emit(fence);
  85. if (!ret) {
  86. kref_get(&fence->kref);
  87. spin_lock(&fctx->lock);
  88. list_add_tail(&fence->head, &fctx->pending);
  89. spin_unlock(&fctx->lock);
  90. }
  91. return ret;
  92. }
  93. bool
  94. nouveau_fence_done(struct nouveau_fence *fence)
  95. {
  96. if (fence->channel)
  97. nouveau_fence_update(fence->channel);
  98. return !fence->channel;
  99. }
  100. int
  101. nouveau_fence_wait(struct nouveau_fence *fence, bool lazy, bool intr)
  102. {
  103. unsigned long sleep_time = NSEC_PER_MSEC / 1000;
  104. ktime_t t;
  105. int ret = 0;
  106. while (!nouveau_fence_done(fence)) {
  107. if (fence->timeout && time_after_eq(jiffies, fence->timeout)) {
  108. ret = -EBUSY;
  109. break;
  110. }
  111. __set_current_state(intr ? TASK_INTERRUPTIBLE :
  112. TASK_UNINTERRUPTIBLE);
  113. if (lazy) {
  114. t = ktime_set(0, sleep_time);
  115. schedule_hrtimeout(&t, HRTIMER_MODE_REL);
  116. sleep_time *= 2;
  117. if (sleep_time > NSEC_PER_MSEC)
  118. sleep_time = NSEC_PER_MSEC;
  119. }
  120. if (intr && signal_pending(current)) {
  121. ret = -ERESTARTSYS;
  122. break;
  123. }
  124. }
  125. __set_current_state(TASK_RUNNING);
  126. return ret;
  127. }
  128. int
  129. nouveau_fence_sync(struct nouveau_fence *fence, struct nouveau_channel *chan)
  130. {
  131. struct drm_device *dev = chan->dev;
  132. struct nouveau_fence_priv *priv = nv_engine(dev, NVOBJ_ENGINE_FENCE);
  133. struct nouveau_channel *prev;
  134. int ret = 0;
  135. prev = fence ? nouveau_channel_get_unlocked(fence->channel) : NULL;
  136. if (prev) {
  137. if (unlikely(prev != chan && !nouveau_fence_done(fence))) {
  138. ret = priv->sync(fence, prev, chan);
  139. if (unlikely(ret))
  140. ret = nouveau_fence_wait(fence, true, false);
  141. }
  142. nouveau_channel_put_unlocked(&prev);
  143. }
  144. return ret;
  145. }
  146. static void
  147. nouveau_fence_del(struct kref *kref)
  148. {
  149. struct nouveau_fence *fence = container_of(kref, typeof(*fence), kref);
  150. kfree(fence);
  151. }
  152. void
  153. nouveau_fence_unref(struct nouveau_fence **pfence)
  154. {
  155. if (*pfence)
  156. kref_put(&(*pfence)->kref, nouveau_fence_del);
  157. *pfence = NULL;
  158. }
  159. struct nouveau_fence *
  160. nouveau_fence_ref(struct nouveau_fence *fence)
  161. {
  162. kref_get(&fence->kref);
  163. return fence;
  164. }
  165. int
  166. nouveau_fence_new(struct nouveau_channel *chan, struct nouveau_fence **pfence)
  167. {
  168. struct nouveau_fence *fence;
  169. int ret = 0;
  170. if (unlikely(!chan->engctx[NVOBJ_ENGINE_FENCE]))
  171. return -ENODEV;
  172. fence = kzalloc(sizeof(*fence), GFP_KERNEL);
  173. if (!fence)
  174. return -ENOMEM;
  175. kref_init(&fence->kref);
  176. if (chan) {
  177. ret = nouveau_fence_emit(fence, chan);
  178. if (ret)
  179. nouveau_fence_unref(&fence);
  180. }
  181. *pfence = fence;
  182. return ret;
  183. }