sched_fence.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright 2015 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. */
  23. #include <linux/kthread.h>
  24. #include <linux/module.h>
  25. #include <linux/sched.h>
  26. #include <linux/slab.h>
  27. #include <linux/wait.h>
  28. #include <drm/gpu_scheduler.h>
  29. static struct kmem_cache *sched_fence_slab;
  30. static int __init drm_sched_fence_slab_init(void)
  31. {
  32. sched_fence_slab = KMEM_CACHE(drm_sched_fence, SLAB_HWCACHE_ALIGN);
  33. if (!sched_fence_slab)
  34. return -ENOMEM;
  35. return 0;
  36. }
  37. static void __exit drm_sched_fence_slab_fini(void)
  38. {
  39. rcu_barrier();
  40. kmem_cache_destroy(sched_fence_slab);
  41. }
  42. static void drm_sched_fence_set_parent(struct drm_sched_fence *s_fence,
  43. struct dma_fence *fence)
  44. {
  45. /*
  46. * smp_store_release() to ensure another thread racing us
  47. * in drm_sched_fence_set_deadline_finished() sees the
  48. * fence's parent set before test_bit()
  49. */
  50. smp_store_release(&s_fence->parent, dma_fence_get(fence));
  51. if (test_bit(DRM_SCHED_FENCE_FLAG_HAS_DEADLINE_BIT,
  52. &s_fence->finished.flags))
  53. dma_fence_set_deadline(fence, s_fence->deadline);
  54. }
  55. void drm_sched_fence_scheduled(struct drm_sched_fence *fence,
  56. struct dma_fence *parent)
  57. {
  58. /* Set the parent before signaling the scheduled fence, such that,
  59. * any waiter expecting the parent to be filled after the job has
  60. * been scheduled (which is the case for drivers delegating waits
  61. * to some firmware) doesn't have to busy wait for parent to show
  62. * up.
  63. */
  64. if (!IS_ERR_OR_NULL(parent))
  65. drm_sched_fence_set_parent(fence, parent);
  66. dma_fence_signal(&fence->scheduled);
  67. }
  68. void drm_sched_fence_finished(struct drm_sched_fence *fence, int result)
  69. {
  70. if (result)
  71. dma_fence_set_error(&fence->finished, result);
  72. dma_fence_signal(&fence->finished);
  73. }
  74. static const char *drm_sched_fence_get_driver_name(struct dma_fence *fence)
  75. {
  76. return "drm_sched";
  77. }
  78. static const char *drm_sched_fence_get_timeline_name(struct dma_fence *f)
  79. {
  80. struct drm_sched_fence *fence = to_drm_sched_fence(f);
  81. return (const char *)fence->sched->name;
  82. }
  83. static void drm_sched_fence_free_rcu(struct rcu_head *rcu)
  84. {
  85. struct dma_fence *f = container_of(rcu, struct dma_fence, rcu);
  86. struct drm_sched_fence *fence = to_drm_sched_fence(f);
  87. if (!WARN_ON_ONCE(!fence))
  88. kmem_cache_free(sched_fence_slab, fence);
  89. }
  90. /**
  91. * drm_sched_fence_free - free up an uninitialized fence
  92. *
  93. * @fence: fence to free
  94. *
  95. * Free up the fence memory. Should only be used if drm_sched_fence_init()
  96. * has not been called yet.
  97. */
  98. void drm_sched_fence_free(struct drm_sched_fence *fence)
  99. {
  100. /* This function should not be called if the fence has been initialized. */
  101. if (!WARN_ON_ONCE(fence->sched))
  102. kmem_cache_free(sched_fence_slab, fence);
  103. }
  104. /**
  105. * drm_sched_fence_release_scheduled - callback that fence can be freed
  106. *
  107. * @f: fence
  108. *
  109. * This function is called when the reference count becomes zero.
  110. * It just RCU schedules freeing up the fence.
  111. */
  112. static void drm_sched_fence_release_scheduled(struct dma_fence *f)
  113. {
  114. struct drm_sched_fence *fence = to_drm_sched_fence(f);
  115. dma_fence_put(fence->parent);
  116. call_rcu(&fence->finished.rcu, drm_sched_fence_free_rcu);
  117. }
  118. /**
  119. * drm_sched_fence_release_finished - drop extra reference
  120. *
  121. * @f: fence
  122. *
  123. * Drop the extra reference from the scheduled fence to the base fence.
  124. */
  125. static void drm_sched_fence_release_finished(struct dma_fence *f)
  126. {
  127. struct drm_sched_fence *fence = to_drm_sched_fence(f);
  128. dma_fence_put(&fence->scheduled);
  129. }
  130. static void drm_sched_fence_set_deadline_finished(struct dma_fence *f,
  131. ktime_t deadline)
  132. {
  133. struct drm_sched_fence *fence = to_drm_sched_fence(f);
  134. struct dma_fence *parent;
  135. unsigned long flags;
  136. spin_lock_irqsave(&fence->lock, flags);
  137. /* If we already have an earlier deadline, keep it: */
  138. if (test_bit(DRM_SCHED_FENCE_FLAG_HAS_DEADLINE_BIT, &f->flags) &&
  139. ktime_before(fence->deadline, deadline)) {
  140. spin_unlock_irqrestore(&fence->lock, flags);
  141. return;
  142. }
  143. fence->deadline = deadline;
  144. set_bit(DRM_SCHED_FENCE_FLAG_HAS_DEADLINE_BIT, &f->flags);
  145. spin_unlock_irqrestore(&fence->lock, flags);
  146. /*
  147. * smp_load_aquire() to ensure that if we are racing another
  148. * thread calling drm_sched_fence_set_parent(), that we see
  149. * the parent set before it calls test_bit(HAS_DEADLINE_BIT)
  150. */
  151. parent = smp_load_acquire(&fence->parent);
  152. if (parent)
  153. dma_fence_set_deadline(parent, deadline);
  154. }
  155. static const struct dma_fence_ops drm_sched_fence_ops_scheduled = {
  156. .get_driver_name = drm_sched_fence_get_driver_name,
  157. .get_timeline_name = drm_sched_fence_get_timeline_name,
  158. .release = drm_sched_fence_release_scheduled,
  159. };
  160. static const struct dma_fence_ops drm_sched_fence_ops_finished = {
  161. .get_driver_name = drm_sched_fence_get_driver_name,
  162. .get_timeline_name = drm_sched_fence_get_timeline_name,
  163. .release = drm_sched_fence_release_finished,
  164. .set_deadline = drm_sched_fence_set_deadline_finished,
  165. };
  166. struct drm_sched_fence *to_drm_sched_fence(struct dma_fence *f)
  167. {
  168. if (f->ops == &drm_sched_fence_ops_scheduled)
  169. return container_of(f, struct drm_sched_fence, scheduled);
  170. if (f->ops == &drm_sched_fence_ops_finished)
  171. return container_of(f, struct drm_sched_fence, finished);
  172. return NULL;
  173. }
  174. EXPORT_SYMBOL(to_drm_sched_fence);
  175. struct drm_sched_fence *drm_sched_fence_alloc(struct drm_sched_entity *entity,
  176. void *owner)
  177. {
  178. struct drm_sched_fence *fence = NULL;
  179. fence = kmem_cache_zalloc(sched_fence_slab, GFP_KERNEL);
  180. if (fence == NULL)
  181. return NULL;
  182. fence->owner = owner;
  183. spin_lock_init(&fence->lock);
  184. return fence;
  185. }
  186. void drm_sched_fence_init(struct drm_sched_fence *fence,
  187. struct drm_sched_entity *entity)
  188. {
  189. unsigned seq;
  190. fence->sched = entity->rq->sched;
  191. seq = atomic_inc_return(&entity->fence_seq);
  192. dma_fence_init(&fence->scheduled, &drm_sched_fence_ops_scheduled,
  193. &fence->lock, entity->fence_context, seq);
  194. dma_fence_init(&fence->finished, &drm_sched_fence_ops_finished,
  195. &fence->lock, entity->fence_context + 1, seq);
  196. }
  197. module_init(drm_sched_fence_slab_init);
  198. module_exit(drm_sched_fence_slab_fini);
  199. MODULE_DESCRIPTION("DRM GPU scheduler");
  200. MODULE_LICENSE("GPL and additional rights");