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