nouveau_fence.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. spin_lock(&chan->fence.lock);
  58. if (USE_REFCNT)
  59. sequence = nvchan_rd32(chan, 0x48);
  60. else
  61. sequence = atomic_read(&chan->fence.last_sequence_irq);
  62. if (chan->fence.sequence_ack == sequence)
  63. goto out;
  64. chan->fence.sequence_ack = sequence;
  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. out:
  75. spin_unlock(&chan->fence.lock);
  76. }
  77. int
  78. nouveau_fence_new(struct nouveau_channel *chan, struct nouveau_fence **pfence,
  79. bool emit)
  80. {
  81. struct nouveau_fence *fence;
  82. int ret = 0;
  83. fence = kzalloc(sizeof(*fence), GFP_KERNEL);
  84. if (!fence)
  85. return -ENOMEM;
  86. kref_init(&fence->refcount);
  87. fence->channel = chan;
  88. if (emit)
  89. ret = nouveau_fence_emit(fence);
  90. if (ret)
  91. nouveau_fence_unref((void *)&fence);
  92. *pfence = fence;
  93. return ret;
  94. }
  95. struct nouveau_channel *
  96. nouveau_fence_channel(struct nouveau_fence *fence)
  97. {
  98. return fence ? fence->channel : NULL;
  99. }
  100. int
  101. nouveau_fence_emit(struct nouveau_fence *fence)
  102. {
  103. struct drm_nouveau_private *dev_priv = fence->channel->dev->dev_private;
  104. struct nouveau_channel *chan = fence->channel;
  105. int ret;
  106. ret = RING_SPACE(chan, 2);
  107. if (ret)
  108. return ret;
  109. if (unlikely(chan->fence.sequence == chan->fence.sequence_ack - 1)) {
  110. nouveau_fence_update(chan);
  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(&chan->fence.lock);
  117. list_add_tail(&fence->entry, &chan->fence.pending);
  118. spin_unlock(&chan->fence.lock);
  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. if (fence->signalled)
  145. return true;
  146. nouveau_fence_update(chan);
  147. return fence->signalled;
  148. }
  149. int
  150. nouveau_fence_wait(void *sync_obj, void *sync_arg, bool lazy, bool intr)
  151. {
  152. unsigned long timeout = jiffies + (3 * DRM_HZ);
  153. int ret = 0;
  154. while (1) {
  155. if (nouveau_fence_signalled(sync_obj, sync_arg))
  156. break;
  157. if (time_after_eq(jiffies, timeout)) {
  158. ret = -EBUSY;
  159. break;
  160. }
  161. __set_current_state(intr ? TASK_INTERRUPTIBLE
  162. : TASK_UNINTERRUPTIBLE);
  163. if (lazy)
  164. schedule_timeout(1);
  165. if (intr && signal_pending(current)) {
  166. ret = -ERESTARTSYS;
  167. break;
  168. }
  169. }
  170. __set_current_state(TASK_RUNNING);
  171. return ret;
  172. }
  173. int
  174. nouveau_fence_flush(void *sync_obj, void *sync_arg)
  175. {
  176. return 0;
  177. }
  178. int
  179. nouveau_fence_init(struct nouveau_channel *chan)
  180. {
  181. INIT_LIST_HEAD(&chan->fence.pending);
  182. spin_lock_init(&chan->fence.lock);
  183. atomic_set(&chan->fence.last_sequence_irq, 0);
  184. return 0;
  185. }
  186. void
  187. nouveau_fence_fini(struct nouveau_channel *chan)
  188. {
  189. struct list_head *entry, *tmp;
  190. struct nouveau_fence *fence;
  191. list_for_each_safe(entry, tmp, &chan->fence.pending) {
  192. fence = list_entry(entry, struct nouveau_fence, entry);
  193. fence->signalled = true;
  194. list_del(&fence->entry);
  195. kref_put(&fence->refcount, nouveau_fence_del);
  196. }
  197. }