gpu_scheduler.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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/fence.h>
  27. #define AMD_SCHED_FENCE_SCHEDULED_BIT FENCE_FLAG_USER_BITS
  28. struct amd_gpu_scheduler;
  29. struct amd_sched_rq;
  30. extern struct kmem_cache *sched_fence_slab;
  31. extern atomic_t sched_fence_slab_ref;
  32. /**
  33. * A scheduler entity is a wrapper around a job queue or a group
  34. * of other entities. Entities take turns emitting jobs from their
  35. * job queues to corresponding hardware ring based on scheduling
  36. * policy.
  37. */
  38. struct amd_sched_entity {
  39. struct list_head list;
  40. struct amd_sched_rq *rq;
  41. struct amd_gpu_scheduler *sched;
  42. spinlock_t queue_lock;
  43. struct kfifo job_queue;
  44. atomic_t fence_seq;
  45. uint64_t fence_context;
  46. struct fence *dependency;
  47. struct fence_cb cb;
  48. };
  49. /**
  50. * Run queue is a set of entities scheduling command submissions for
  51. * one specific ring. It implements the scheduling policy that selects
  52. * the next entity to emit commands from.
  53. */
  54. struct amd_sched_rq {
  55. spinlock_t lock;
  56. struct list_head entities;
  57. struct amd_sched_entity *current_entity;
  58. };
  59. struct amd_sched_fence {
  60. struct fence base;
  61. struct fence_cb cb;
  62. struct list_head scheduled_cb;
  63. struct amd_gpu_scheduler *sched;
  64. spinlock_t lock;
  65. void *owner;
  66. struct amd_sched_job *s_job;
  67. };
  68. struct amd_sched_job {
  69. struct kref refcount;
  70. struct amd_gpu_scheduler *sched;
  71. struct amd_sched_entity *s_entity;
  72. struct amd_sched_fence *s_fence;
  73. struct fence_cb cb_free_job;
  74. struct work_struct work_free_job;
  75. struct list_head node;
  76. struct delayed_work work_tdr;
  77. void (*free_callback)(struct kref *refcount);
  78. };
  79. extern const struct fence_ops amd_sched_fence_ops;
  80. static inline struct amd_sched_fence *to_amd_sched_fence(struct fence *f)
  81. {
  82. struct amd_sched_fence *__f = container_of(f, struct amd_sched_fence,
  83. base);
  84. if (__f->base.ops == &amd_sched_fence_ops)
  85. return __f;
  86. return NULL;
  87. }
  88. /**
  89. * Define the backend operations called by the scheduler,
  90. * these functions should be implemented in driver side
  91. */
  92. struct amd_sched_backend_ops {
  93. struct fence *(*dependency)(struct amd_sched_job *sched_job);
  94. struct fence *(*run_job)(struct amd_sched_job *sched_job);
  95. void (*timedout_job)(struct amd_sched_job *sched_job);
  96. };
  97. enum amd_sched_priority {
  98. AMD_SCHED_PRIORITY_KERNEL = 0,
  99. AMD_SCHED_PRIORITY_NORMAL,
  100. AMD_SCHED_MAX_PRIORITY
  101. };
  102. /**
  103. * One scheduler is implemented for each hardware ring
  104. */
  105. struct amd_gpu_scheduler {
  106. const struct amd_sched_backend_ops *ops;
  107. uint32_t hw_submission_limit;
  108. long timeout;
  109. const char *name;
  110. struct amd_sched_rq sched_rq[AMD_SCHED_MAX_PRIORITY];
  111. wait_queue_head_t wake_up_worker;
  112. wait_queue_head_t job_scheduled;
  113. atomic_t hw_rq_count;
  114. struct task_struct *thread;
  115. struct list_head ring_mirror_list;
  116. spinlock_t job_list_lock;
  117. };
  118. int amd_sched_init(struct amd_gpu_scheduler *sched,
  119. const struct amd_sched_backend_ops *ops,
  120. uint32_t hw_submission, long timeout, const char *name);
  121. void amd_sched_fini(struct amd_gpu_scheduler *sched);
  122. int amd_sched_entity_init(struct amd_gpu_scheduler *sched,
  123. struct amd_sched_entity *entity,
  124. struct amd_sched_rq *rq,
  125. uint32_t jobs);
  126. void amd_sched_entity_fini(struct amd_gpu_scheduler *sched,
  127. struct amd_sched_entity *entity);
  128. void amd_sched_entity_push_job(struct amd_sched_job *sched_job);
  129. struct amd_sched_fence *amd_sched_fence_create(
  130. struct amd_sched_entity *s_entity, void *owner);
  131. void amd_sched_fence_scheduled(struct amd_sched_fence *fence);
  132. void amd_sched_fence_signal(struct amd_sched_fence *fence);
  133. int amd_sched_job_init(struct amd_sched_job *job,
  134. struct amd_gpu_scheduler *sched,
  135. struct amd_sched_entity *entity,
  136. void (*free_cb)(struct kref* refcount),
  137. void *owner, struct fence **fence);
  138. static inline void amd_sched_job_get(struct amd_sched_job *job)
  139. {
  140. if (job)
  141. kref_get(&job->refcount);
  142. }
  143. static inline void amd_sched_job_put(struct amd_sched_job *job)
  144. {
  145. if (job)
  146. kref_put(&job->refcount, job->free_callback);
  147. }
  148. #endif