gpu_scheduler.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 _GPU_SCHEDULER_H_
  24. #define _GPU_SCHEDULER_H_
  25. #include <linux/kfifo.h>
  26. #include <linux/dma-fence.h>
  27. struct amd_gpu_scheduler;
  28. struct amd_sched_rq;
  29. /**
  30. * A scheduler entity is a wrapper around a job queue or a group
  31. * of other entities. Entities take turns emitting jobs from their
  32. * job queues to corresponding hardware ring based on scheduling
  33. * policy.
  34. */
  35. struct amd_sched_entity {
  36. struct list_head list;
  37. struct amd_sched_rq *rq;
  38. spinlock_t rq_lock;
  39. struct amd_gpu_scheduler *sched;
  40. spinlock_t queue_lock;
  41. struct kfifo job_queue;
  42. atomic_t fence_seq;
  43. uint64_t fence_context;
  44. struct dma_fence *dependency;
  45. struct dma_fence_cb cb;
  46. };
  47. /**
  48. * Run queue is a set of entities scheduling command submissions for
  49. * one specific ring. It implements the scheduling policy that selects
  50. * the next entity to emit commands from.
  51. */
  52. struct amd_sched_rq {
  53. spinlock_t lock;
  54. struct list_head entities;
  55. struct amd_sched_entity *current_entity;
  56. };
  57. struct amd_sched_fence {
  58. struct dma_fence scheduled;
  59. struct dma_fence finished;
  60. struct dma_fence_cb cb;
  61. struct dma_fence *parent;
  62. struct amd_gpu_scheduler *sched;
  63. spinlock_t lock;
  64. void *owner;
  65. };
  66. struct amd_sched_job {
  67. struct amd_gpu_scheduler *sched;
  68. struct amd_sched_entity *s_entity;
  69. struct amd_sched_fence *s_fence;
  70. struct dma_fence_cb finish_cb;
  71. struct work_struct finish_work;
  72. struct list_head node;
  73. struct delayed_work work_tdr;
  74. uint64_t id;
  75. atomic_t karma;
  76. };
  77. extern const struct dma_fence_ops amd_sched_fence_ops_scheduled;
  78. extern const struct dma_fence_ops amd_sched_fence_ops_finished;
  79. static inline struct amd_sched_fence *to_amd_sched_fence(struct dma_fence *f)
  80. {
  81. if (f->ops == &amd_sched_fence_ops_scheduled)
  82. return container_of(f, struct amd_sched_fence, scheduled);
  83. if (f->ops == &amd_sched_fence_ops_finished)
  84. return container_of(f, struct amd_sched_fence, finished);
  85. return NULL;
  86. }
  87. static inline bool amd_sched_invalidate_job(struct amd_sched_job *s_job, int threshold)
  88. {
  89. return (s_job && atomic_inc_return(&s_job->karma) > threshold);
  90. }
  91. /**
  92. * Define the backend operations called by the scheduler,
  93. * these functions should be implemented in driver side
  94. */
  95. struct amd_sched_backend_ops {
  96. struct dma_fence *(*dependency)(struct amd_sched_job *sched_job);
  97. struct dma_fence *(*run_job)(struct amd_sched_job *sched_job);
  98. void (*timedout_job)(struct amd_sched_job *sched_job);
  99. void (*free_job)(struct amd_sched_job *sched_job);
  100. };
  101. enum amd_sched_priority {
  102. AMD_SCHED_PRIORITY_MIN,
  103. AMD_SCHED_PRIORITY_LOW = AMD_SCHED_PRIORITY_MIN,
  104. AMD_SCHED_PRIORITY_NORMAL,
  105. AMD_SCHED_PRIORITY_HIGH_SW,
  106. AMD_SCHED_PRIORITY_HIGH_HW,
  107. AMD_SCHED_PRIORITY_KERNEL,
  108. AMD_SCHED_PRIORITY_MAX,
  109. AMD_SCHED_PRIORITY_INVALID = -1,
  110. AMD_SCHED_PRIORITY_UNSET = -2
  111. };
  112. /**
  113. * One scheduler is implemented for each hardware ring
  114. */
  115. struct amd_gpu_scheduler {
  116. const struct amd_sched_backend_ops *ops;
  117. uint32_t hw_submission_limit;
  118. long timeout;
  119. const char *name;
  120. struct amd_sched_rq sched_rq[AMD_SCHED_PRIORITY_MAX];
  121. wait_queue_head_t wake_up_worker;
  122. wait_queue_head_t job_scheduled;
  123. atomic_t hw_rq_count;
  124. atomic64_t job_id_count;
  125. struct task_struct *thread;
  126. struct list_head ring_mirror_list;
  127. spinlock_t job_list_lock;
  128. };
  129. int amd_sched_init(struct amd_gpu_scheduler *sched,
  130. const struct amd_sched_backend_ops *ops,
  131. uint32_t hw_submission, long timeout, const char *name);
  132. void amd_sched_fini(struct amd_gpu_scheduler *sched);
  133. int amd_sched_entity_init(struct amd_gpu_scheduler *sched,
  134. struct amd_sched_entity *entity,
  135. struct amd_sched_rq *rq,
  136. uint32_t jobs);
  137. void amd_sched_entity_fini(struct amd_gpu_scheduler *sched,
  138. struct amd_sched_entity *entity);
  139. void amd_sched_entity_push_job(struct amd_sched_job *sched_job);
  140. void amd_sched_entity_set_rq(struct amd_sched_entity *entity,
  141. struct amd_sched_rq *rq);
  142. int amd_sched_fence_slab_init(void);
  143. void amd_sched_fence_slab_fini(void);
  144. struct amd_sched_fence *amd_sched_fence_create(
  145. struct amd_sched_entity *s_entity, void *owner);
  146. void amd_sched_fence_scheduled(struct amd_sched_fence *fence);
  147. void amd_sched_fence_finished(struct amd_sched_fence *fence);
  148. int amd_sched_job_init(struct amd_sched_job *job,
  149. struct amd_gpu_scheduler *sched,
  150. struct amd_sched_entity *entity,
  151. void *owner);
  152. void amd_sched_hw_job_reset(struct amd_gpu_scheduler *sched);
  153. void amd_sched_job_recovery(struct amd_gpu_scheduler *sched);
  154. bool amd_sched_dependency_optimized(struct dma_fence* fence,
  155. struct amd_sched_entity *entity);
  156. void amd_sched_job_kickout(struct amd_sched_job *s_job);
  157. static inline enum amd_sched_priority
  158. amd_sched_get_job_priority(struct amd_sched_job *job)
  159. {
  160. return (job->s_entity->rq - job->sched->sched_rq);
  161. }
  162. #endif