nouveau_fence.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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) (nouveau_private(dev)->chipset >= 0x10)
  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_device *dev = chan->dev;
  54. struct nouveau_fence *tmp, *fence;
  55. uint32_t sequence;
  56. spin_lock(&chan->fence.lock);
  57. if (USE_REFCNT(dev))
  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. goto out;
  63. chan->fence.sequence_ack = sequence;
  64. list_for_each_entry_safe(fence, tmp, &chan->fence.pending, entry) {
  65. sequence = fence->sequence;
  66. fence->signalled = true;
  67. list_del(&fence->entry);
  68. kref_put(&fence->refcount, nouveau_fence_del);
  69. if (sequence == chan->fence.sequence_ack)
  70. break;
  71. }
  72. out:
  73. spin_unlock(&chan->fence.lock);
  74. }
  75. int
  76. nouveau_fence_new(struct nouveau_channel *chan, struct nouveau_fence **pfence,
  77. bool emit)
  78. {
  79. struct nouveau_fence *fence;
  80. int ret = 0;
  81. fence = kzalloc(sizeof(*fence), GFP_KERNEL);
  82. if (!fence)
  83. return -ENOMEM;
  84. kref_init(&fence->refcount);
  85. fence->channel = chan;
  86. if (emit)
  87. ret = nouveau_fence_emit(fence);
  88. if (ret)
  89. nouveau_fence_unref((void *)&fence);
  90. *pfence = fence;
  91. return ret;
  92. }
  93. struct nouveau_channel *
  94. nouveau_fence_channel(struct nouveau_fence *fence)
  95. {
  96. return fence ? fence->channel : NULL;
  97. }
  98. int
  99. nouveau_fence_emit(struct nouveau_fence *fence)
  100. {
  101. struct nouveau_channel *chan = fence->channel;
  102. struct drm_device *dev = chan->dev;
  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. nouveau_fence_update(chan);
  109. BUG_ON(chan->fence.sequence ==
  110. chan->fence.sequence_ack - 1);
  111. }
  112. fence->sequence = ++chan->fence.sequence;
  113. kref_get(&fence->refcount);
  114. spin_lock(&chan->fence.lock);
  115. list_add_tail(&fence->entry, &chan->fence.pending);
  116. spin_unlock(&chan->fence.lock);
  117. BEGIN_RING(chan, NvSubSw, USE_REFCNT(dev) ? 0x0050 : 0x0150, 1);
  118. OUT_RING(chan, fence->sequence);
  119. FIRE_RING(chan);
  120. return 0;
  121. }
  122. void
  123. nouveau_fence_unref(void **sync_obj)
  124. {
  125. struct nouveau_fence *fence = nouveau_fence(*sync_obj);
  126. if (fence)
  127. kref_put(&fence->refcount, nouveau_fence_del);
  128. *sync_obj = NULL;
  129. }
  130. void *
  131. nouveau_fence_ref(void *sync_obj)
  132. {
  133. struct nouveau_fence *fence = nouveau_fence(sync_obj);
  134. kref_get(&fence->refcount);
  135. return sync_obj;
  136. }
  137. bool
  138. nouveau_fence_signalled(void *sync_obj, void *sync_arg)
  139. {
  140. struct nouveau_fence *fence = nouveau_fence(sync_obj);
  141. struct nouveau_channel *chan = fence->channel;
  142. if (fence->signalled)
  143. return true;
  144. nouveau_fence_update(chan);
  145. return fence->signalled;
  146. }
  147. int
  148. nouveau_fence_wait(void *sync_obj, void *sync_arg, bool lazy, bool intr)
  149. {
  150. unsigned long timeout = jiffies + (3 * DRM_HZ);
  151. int ret = 0;
  152. while (1) {
  153. if (nouveau_fence_signalled(sync_obj, sync_arg))
  154. break;
  155. if (time_after_eq(jiffies, timeout)) {
  156. ret = -EBUSY;
  157. break;
  158. }
  159. __set_current_state(intr ? TASK_INTERRUPTIBLE
  160. : TASK_UNINTERRUPTIBLE);
  161. if (lazy)
  162. schedule_timeout(1);
  163. if (intr && signal_pending(current)) {
  164. ret = -ERESTARTSYS;
  165. break;
  166. }
  167. }
  168. __set_current_state(TASK_RUNNING);
  169. return ret;
  170. }
  171. int
  172. nouveau_fence_sync(struct nouveau_fence *fence,
  173. struct nouveau_channel *wchan)
  174. {
  175. struct nouveau_channel *chan = nouveau_fence_channel(fence);
  176. if (likely(!fence || chan == wchan ||
  177. nouveau_fence_signalled(fence, NULL)))
  178. return 0;
  179. return nouveau_fence_wait(fence, NULL, false, false);
  180. }
  181. int
  182. nouveau_fence_flush(void *sync_obj, void *sync_arg)
  183. {
  184. return 0;
  185. }
  186. int
  187. nouveau_fence_channel_init(struct nouveau_channel *chan)
  188. {
  189. struct nouveau_gpuobj *obj = NULL;
  190. int ret;
  191. /* Create an NV_SW object for various sync purposes */
  192. ret = nouveau_gpuobj_sw_new(chan, NV_SW, &obj);
  193. if (ret)
  194. return ret;
  195. ret = nouveau_ramht_insert(chan, NvSw, obj);
  196. nouveau_gpuobj_ref(NULL, &obj);
  197. if (ret)
  198. return ret;
  199. ret = RING_SPACE(chan, 2);
  200. if (ret)
  201. return ret;
  202. BEGIN_RING(chan, NvSubSw, 0, 1);
  203. OUT_RING(chan, NvSw);
  204. FIRE_RING(chan);
  205. INIT_LIST_HEAD(&chan->fence.pending);
  206. spin_lock_init(&chan->fence.lock);
  207. atomic_set(&chan->fence.last_sequence_irq, 0);
  208. return 0;
  209. }
  210. void
  211. nouveau_fence_channel_fini(struct nouveau_channel *chan)
  212. {
  213. struct nouveau_fence *tmp, *fence;
  214. list_for_each_entry_safe(fence, tmp, &chan->fence.pending, entry) {
  215. fence->signalled = true;
  216. list_del(&fence->entry);
  217. kref_put(&fence->refcount, nouveau_fence_del);
  218. }
  219. }