gpu_scheduler.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. #ifndef _DRM_GPU_SCHEDULER_H_
  24. #define _DRM_GPU_SCHEDULER_H_
  25. #include <drm/spsc_queue.h>
  26. #include <linux/dma-fence.h>
  27. struct drm_gpu_scheduler;
  28. struct drm_sched_rq;
  29. enum drm_sched_priority {
  30. DRM_SCHED_PRIORITY_MIN,
  31. DRM_SCHED_PRIORITY_LOW = DRM_SCHED_PRIORITY_MIN,
  32. DRM_SCHED_PRIORITY_NORMAL,
  33. DRM_SCHED_PRIORITY_HIGH_SW,
  34. DRM_SCHED_PRIORITY_HIGH_HW,
  35. DRM_SCHED_PRIORITY_KERNEL,
  36. DRM_SCHED_PRIORITY_MAX,
  37. DRM_SCHED_PRIORITY_INVALID = -1,
  38. DRM_SCHED_PRIORITY_UNSET = -2
  39. };
  40. /**
  41. * A scheduler entity is a wrapper around a job queue or a group
  42. * of other entities. Entities take turns emitting jobs from their
  43. * job queues to corresponding hardware ring based on scheduling
  44. * policy.
  45. */
  46. struct drm_sched_entity {
  47. struct list_head list;
  48. struct drm_sched_rq *rq;
  49. spinlock_t rq_lock;
  50. struct drm_gpu_scheduler *sched;
  51. spinlock_t queue_lock;
  52. struct spsc_queue job_queue;
  53. atomic_t fence_seq;
  54. uint64_t fence_context;
  55. struct dma_fence *dependency;
  56. struct dma_fence_cb cb;
  57. atomic_t *guilty; /* points to ctx's guilty */
  58. };
  59. /**
  60. * Run queue is a set of entities scheduling command submissions for
  61. * one specific ring. It implements the scheduling policy that selects
  62. * the next entity to emit commands from.
  63. */
  64. struct drm_sched_rq {
  65. spinlock_t lock;
  66. struct list_head entities;
  67. struct drm_sched_entity *current_entity;
  68. };
  69. struct drm_sched_fence {
  70. struct dma_fence scheduled;
  71. struct dma_fence finished;
  72. struct dma_fence_cb cb;
  73. struct dma_fence *parent;
  74. struct drm_gpu_scheduler *sched;
  75. spinlock_t lock;
  76. void *owner;
  77. };
  78. struct drm_sched_fence *to_drm_sched_fence(struct dma_fence *f);
  79. struct drm_sched_job {
  80. struct spsc_node queue_node;
  81. struct drm_gpu_scheduler *sched;
  82. struct drm_sched_fence *s_fence;
  83. struct dma_fence_cb finish_cb;
  84. struct work_struct finish_work;
  85. struct list_head node;
  86. struct delayed_work work_tdr;
  87. uint64_t id;
  88. atomic_t karma;
  89. enum drm_sched_priority s_priority;
  90. };
  91. static inline bool drm_sched_invalidate_job(struct drm_sched_job *s_job,
  92. int threshold)
  93. {
  94. return (s_job && atomic_inc_return(&s_job->karma) > threshold);
  95. }
  96. /**
  97. * Define the backend operations called by the scheduler,
  98. * these functions should be implemented in driver side
  99. */
  100. struct drm_sched_backend_ops {
  101. struct dma_fence *(*dependency)(struct drm_sched_job *sched_job,
  102. struct drm_sched_entity *s_entity);
  103. struct dma_fence *(*run_job)(struct drm_sched_job *sched_job);
  104. void (*timedout_job)(struct drm_sched_job *sched_job);
  105. void (*free_job)(struct drm_sched_job *sched_job);
  106. };
  107. /**
  108. * One scheduler is implemented for each hardware ring
  109. */
  110. struct drm_gpu_scheduler {
  111. const struct drm_sched_backend_ops *ops;
  112. uint32_t hw_submission_limit;
  113. long timeout;
  114. const char *name;
  115. struct drm_sched_rq sched_rq[DRM_SCHED_PRIORITY_MAX];
  116. wait_queue_head_t wake_up_worker;
  117. wait_queue_head_t job_scheduled;
  118. atomic_t hw_rq_count;
  119. atomic64_t job_id_count;
  120. struct task_struct *thread;
  121. struct list_head ring_mirror_list;
  122. spinlock_t job_list_lock;
  123. int hang_limit;
  124. };
  125. int drm_sched_init(struct drm_gpu_scheduler *sched,
  126. const struct drm_sched_backend_ops *ops,
  127. uint32_t hw_submission, unsigned hang_limit, long timeout,
  128. const char *name);
  129. void drm_sched_fini(struct drm_gpu_scheduler *sched);
  130. int drm_sched_entity_init(struct drm_gpu_scheduler *sched,
  131. struct drm_sched_entity *entity,
  132. struct drm_sched_rq *rq,
  133. uint32_t jobs, atomic_t *guilty);
  134. void drm_sched_entity_fini(struct drm_gpu_scheduler *sched,
  135. struct drm_sched_entity *entity);
  136. void drm_sched_entity_push_job(struct drm_sched_job *sched_job,
  137. struct drm_sched_entity *entity);
  138. void drm_sched_entity_set_rq(struct drm_sched_entity *entity,
  139. struct drm_sched_rq *rq);
  140. struct drm_sched_fence *drm_sched_fence_create(
  141. struct drm_sched_entity *s_entity, void *owner);
  142. void drm_sched_fence_scheduled(struct drm_sched_fence *fence);
  143. void drm_sched_fence_finished(struct drm_sched_fence *fence);
  144. int drm_sched_job_init(struct drm_sched_job *job,
  145. struct drm_gpu_scheduler *sched,
  146. struct drm_sched_entity *entity,
  147. void *owner);
  148. void drm_sched_hw_job_reset(struct drm_gpu_scheduler *sched,
  149. struct drm_sched_job *job);
  150. void drm_sched_job_recovery(struct drm_gpu_scheduler *sched);
  151. bool drm_sched_dependency_optimized(struct dma_fence* fence,
  152. struct drm_sched_entity *entity);
  153. void drm_sched_job_kickout(struct drm_sched_job *s_job);
  154. #endif