nouveau_fence.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 "nouveau_drv.h"
  29. #include "nouveau_dma.h"
  30. #define USE_REFCNT (dev_priv->card_type >= NV_10)
  31. struct nouveau_fence {
  32. struct nouveau_channel *channel;
  33. struct kref refcount;
  34. struct list_head entry;
  35. uint32_t sequence;
  36. bool signalled;
  37. };
  38. static inline struct nouveau_fence *
  39. nouveau_fence(void *sync_obj)
  40. {
  41. return (struct nouveau_fence *)sync_obj;
  42. }
  43. static void
  44. nouveau_fence_del(struct kref *ref)
  45. {
  46. struct nouveau_fence *fence =
  47. container_of(ref, struct nouveau_fence, refcount);
  48. kfree(fence);
  49. }
  50. void
  51. nouveau_fence_update(struct nouveau_channel *chan)
  52. {
  53. struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
  54. struct list_head *entry, *tmp;
  55. struct nouveau_fence *fence;
  56. uint32_t sequence;
  57. if (USE_REFCNT)
  58. sequence = nvchan_rd32(chan, 0x48);
  59. else
  60. sequence = chan->fence.last_sequence_irq;
  61. if (chan->fence.sequence_ack == sequence)
  62. return;
  63. chan->fence.sequence_ack = sequence;
  64. list_for_each_safe(entry, tmp, &chan->fence.pending) {
  65. fence = list_entry(entry, struct nouveau_fence, entry);
  66. sequence = fence->sequence;
  67. fence->signalled = true;
  68. list_del(&fence->entry);
  69. kref_put(&fence->refcount, nouveau_fence_del);
  70. if (sequence == chan->fence.sequence_ack)
  71. break;
  72. }
  73. }
  74. int
  75. nouveau_fence_new(struct nouveau_channel *chan, struct nouveau_fence **pfence,
  76. bool emit)
  77. {
  78. struct nouveau_fence *fence;
  79. int ret = 0;
  80. fence = kzalloc(sizeof(*fence), GFP_KERNEL);
  81. if (!fence)
  82. return -ENOMEM;
  83. kref_init(&fence->refcount);
  84. fence->channel = chan;
  85. if (emit)
  86. ret = nouveau_fence_emit(fence);
  87. if (ret)
  88. nouveau_fence_unref((void *)&fence);
  89. *pfence = fence;
  90. return ret;
  91. }
  92. struct nouveau_channel *
  93. nouveau_fence_channel(struct nouveau_fence *fence)
  94. {
  95. return fence ? fence->channel : NULL;
  96. }
  97. int
  98. nouveau_fence_emit(struct nouveau_fence *fence)
  99. {
  100. struct drm_nouveau_private *dev_priv = fence->channel->dev->dev_private;
  101. struct nouveau_channel *chan = fence->channel;
  102. unsigned long flags;
  103. int ret;
  104. ret = RING_SPACE(chan, 2);
  105. if (ret)
  106. return ret;
  107. if (unlikely(chan->fence.sequence == chan->fence.sequence_ack - 1)) {
  108. spin_lock_irqsave(&chan->fence.lock, flags);
  109. nouveau_fence_update(chan);
  110. spin_unlock_irqrestore(&chan->fence.lock, flags);
  111. BUG_ON(chan->fence.sequence ==
  112. chan->fence.sequence_ack - 1);
  113. }
  114. fence->sequence = ++chan->fence.sequence;
  115. kref_get(&fence->refcount);
  116. spin_lock_irqsave(&chan->fence.lock, flags);
  117. list_add_tail(&fence->entry, &chan->fence.pending);
  118. spin_unlock_irqrestore(&chan->fence.lock, flags);
  119. BEGIN_RING(chan, NvSubSw, USE_REFCNT ? 0x0050 : 0x0150, 1);
  120. OUT_RING(chan, fence->sequence);
  121. FIRE_RING(chan);
  122. return 0;
  123. }
  124. void
  125. nouveau_fence_unref(void **sync_obj)
  126. {
  127. struct nouveau_fence *fence = nouveau_fence(*sync_obj);
  128. if (fence)
  129. kref_put(&fence->refcount, nouveau_fence_del);
  130. *sync_obj = NULL;
  131. }
  132. void *
  133. nouveau_fence_ref(void *sync_obj)
  134. {
  135. struct nouveau_fence *fence = nouveau_fence(sync_obj);
  136. kref_get(&fence->refcount);
  137. return sync_obj;
  138. }
  139. bool
  140. nouveau_fence_signalled(void *sync_obj, void *sync_arg)
  141. {
  142. struct nouveau_fence *fence = nouveau_fence(sync_obj);
  143. struct nouveau_channel *chan = fence->channel;
  144. unsigned long flags;
  145. if (fence->signalled)
  146. return true;
  147. spin_lock_irqsave(&chan->fence.lock, flags);
  148. nouveau_fence_update(chan);
  149. spin_unlock_irqrestore(&chan->fence.lock, flags);
  150. return fence->signalled;
  151. }
  152. int
  153. nouveau_fence_wait(void *sync_obj, void *sync_arg, bool lazy, bool intr)
  154. {
  155. unsigned long timeout = jiffies + (3 * DRM_HZ);
  156. int ret = 0;
  157. __set_current_state(intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
  158. while (1) {
  159. if (nouveau_fence_signalled(sync_obj, sync_arg))
  160. break;
  161. if (time_after_eq(jiffies, timeout)) {
  162. ret = -EBUSY;
  163. break;
  164. }
  165. if (lazy)
  166. schedule_timeout(1);
  167. if (intr && signal_pending(current)) {
  168. ret = -ERESTARTSYS;
  169. break;
  170. }
  171. }
  172. __set_current_state(TASK_RUNNING);
  173. return ret;
  174. }
  175. int
  176. nouveau_fence_flush(void *sync_obj, void *sync_arg)
  177. {
  178. return 0;
  179. }
  180. void
  181. nouveau_fence_handler(struct drm_device *dev, int channel)
  182. {
  183. struct drm_nouveau_private *dev_priv = dev->dev_private;
  184. struct nouveau_channel *chan = NULL;
  185. if (channel >= 0 && channel < dev_priv->engine.fifo.channels)
  186. chan = dev_priv->fifos[channel];
  187. if (chan) {
  188. spin_lock_irq(&chan->fence.lock);
  189. nouveau_fence_update(chan);
  190. spin_unlock_irq(&chan->fence.lock);
  191. }
  192. }
  193. int
  194. nouveau_fence_init(struct nouveau_channel *chan)
  195. {
  196. INIT_LIST_HEAD(&chan->fence.pending);
  197. spin_lock_init(&chan->fence.lock);
  198. return 0;
  199. }
  200. void
  201. nouveau_fence_fini(struct nouveau_channel *chan)
  202. {
  203. struct list_head *entry, *tmp;
  204. struct nouveau_fence *fence;
  205. list_for_each_safe(entry, tmp, &chan->fence.pending) {
  206. fence = list_entry(entry, struct nouveau_fence, entry);
  207. fence->signalled = true;
  208. list_del(&fence->entry);
  209. kref_put(&fence->refcount, nouveau_fence_del);
  210. }
  211. }