nouveau_fence.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 = atomic_read(&chan->fence.last_sequence_irq);
  61. if (chan->fence.sequence_ack == sequence)
  62. return;
  63. chan->fence.sequence_ack = sequence;
  64. spin_lock(&chan->fence.lock);
  65. list_for_each_safe(entry, tmp, &chan->fence.pending) {
  66. fence = list_entry(entry, struct nouveau_fence, entry);
  67. sequence = fence->sequence;
  68. fence->signalled = true;
  69. list_del(&fence->entry);
  70. kref_put(&fence->refcount, nouveau_fence_del);
  71. if (sequence == chan->fence.sequence_ack)
  72. break;
  73. }
  74. spin_unlock(&chan->fence.lock);
  75. }
  76. int
  77. nouveau_fence_new(struct nouveau_channel *chan, struct nouveau_fence **pfence,
  78. bool emit)
  79. {
  80. struct nouveau_fence *fence;
  81. int ret = 0;
  82. fence = kzalloc(sizeof(*fence), GFP_KERNEL);
  83. if (!fence)
  84. return -ENOMEM;
  85. kref_init(&fence->refcount);
  86. fence->channel = chan;
  87. if (emit)
  88. ret = nouveau_fence_emit(fence);
  89. if (ret)
  90. nouveau_fence_unref((void *)&fence);
  91. *pfence = fence;
  92. return ret;
  93. }
  94. struct nouveau_channel *
  95. nouveau_fence_channel(struct nouveau_fence *fence)
  96. {
  97. return fence ? fence->channel : NULL;
  98. }
  99. int
  100. nouveau_fence_emit(struct nouveau_fence *fence)
  101. {
  102. struct drm_nouveau_private *dev_priv = fence->channel->dev->dev_private;
  103. struct nouveau_channel *chan = fence->channel;
  104. int ret;
  105. ret = RING_SPACE(chan, 2);
  106. if (ret)
  107. return ret;
  108. if (unlikely(chan->fence.sequence == chan->fence.sequence_ack - 1)) {
  109. nouveau_fence_update(chan);
  110. BUG_ON(chan->fence.sequence ==
  111. chan->fence.sequence_ack - 1);
  112. }
  113. fence->sequence = ++chan->fence.sequence;
  114. kref_get(&fence->refcount);
  115. spin_lock(&chan->fence.lock);
  116. list_add_tail(&fence->entry, &chan->fence.pending);
  117. spin_unlock(&chan->fence.lock);
  118. BEGIN_RING(chan, NvSubSw, USE_REFCNT ? 0x0050 : 0x0150, 1);
  119. OUT_RING(chan, fence->sequence);
  120. FIRE_RING(chan);
  121. return 0;
  122. }
  123. void
  124. nouveau_fence_unref(void **sync_obj)
  125. {
  126. struct nouveau_fence *fence = nouveau_fence(*sync_obj);
  127. if (fence)
  128. kref_put(&fence->refcount, nouveau_fence_del);
  129. *sync_obj = NULL;
  130. }
  131. void *
  132. nouveau_fence_ref(void *sync_obj)
  133. {
  134. struct nouveau_fence *fence = nouveau_fence(sync_obj);
  135. kref_get(&fence->refcount);
  136. return sync_obj;
  137. }
  138. bool
  139. nouveau_fence_signalled(void *sync_obj, void *sync_arg)
  140. {
  141. struct nouveau_fence *fence = nouveau_fence(sync_obj);
  142. struct nouveau_channel *chan = fence->channel;
  143. if (fence->signalled)
  144. return true;
  145. nouveau_fence_update(chan);
  146. return fence->signalled;
  147. }
  148. int
  149. nouveau_fence_wait(void *sync_obj, void *sync_arg, bool lazy, bool intr)
  150. {
  151. unsigned long timeout = jiffies + (3 * DRM_HZ);
  152. int ret = 0;
  153. while (1) {
  154. if (nouveau_fence_signalled(sync_obj, sync_arg))
  155. break;
  156. if (time_after_eq(jiffies, timeout)) {
  157. ret = -EBUSY;
  158. break;
  159. }
  160. __set_current_state(intr ? TASK_INTERRUPTIBLE
  161. : TASK_UNINTERRUPTIBLE);
  162. if (lazy)
  163. schedule_timeout(1);
  164. if (intr && signal_pending(current)) {
  165. ret = -ERESTARTSYS;
  166. break;
  167. }
  168. }
  169. __set_current_state(TASK_RUNNING);
  170. return ret;
  171. }
  172. int
  173. nouveau_fence_flush(void *sync_obj, void *sync_arg)
  174. {
  175. return 0;
  176. }
  177. int
  178. nouveau_fence_init(struct nouveau_channel *chan)
  179. {
  180. INIT_LIST_HEAD(&chan->fence.pending);
  181. spin_lock_init(&chan->fence.lock);
  182. atomic_set(&chan->fence.last_sequence_irq, 0);
  183. return 0;
  184. }
  185. void
  186. nouveau_fence_fini(struct nouveau_channel *chan)
  187. {
  188. struct list_head *entry, *tmp;
  189. struct nouveau_fence *fence;
  190. list_for_each_safe(entry, tmp, &chan->fence.pending) {
  191. fence = list_entry(entry, struct nouveau_fence, entry);
  192. fence->signalled = true;
  193. list_del(&fence->entry);
  194. kref_put(&fence->refcount, nouveau_fence_del);
  195. }
  196. }