gpu_scheduler.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. extern struct kmem_cache *sched_fence_slab;
  30. extern atomic_t sched_fence_slab_ref;
  31. /**
  32. * A scheduler entity is a wrapper around a job queue or a group
  33. * of other entities. Entities take turns emitting jobs from their
  34. * job queues to corresponding hardware ring based on scheduling
  35. * policy.
  36. */
  37. struct amd_sched_entity {
  38. struct list_head list;
  39. struct amd_sched_rq *rq;
  40. struct amd_gpu_scheduler *sched;
  41. spinlock_t queue_lock;
  42. struct kfifo job_queue;
  43. atomic_t fence_seq;
  44. uint64_t fence_context;
  45. struct dma_fence *dependency;
  46. struct dma_fence_cb cb;
  47. };
  48. /**
  49. * Run queue is a set of entities scheduling command submissions for
  50. * one specific ring. It implements the scheduling policy that selects
  51. * the next entity to emit commands from.
  52. */
  53. struct amd_sched_rq {
  54. spinlock_t lock;
  55. struct list_head entities;
  56. struct amd_sched_entity *current_entity;
  57. };
  58. struct amd_sched_fence {
  59. struct dma_fence scheduled;
  60. struct dma_fence finished;
  61. struct dma_fence_cb cb;
  62. struct dma_fence *parent;
  63. struct amd_gpu_scheduler *sched;
  64. spinlock_t lock;
  65. void *owner;
  66. };
  67. struct amd_sched_job {
  68. struct amd_gpu_scheduler *sched;
  69. struct amd_sched_entity *s_entity;
  70. struct amd_sched_fence *s_fence;
  71. struct dma_fence_cb finish_cb;
  72. struct work_struct finish_work;
  73. struct list_head node;
  74. struct delayed_work work_tdr;
  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. /**
  87. * Define the backend operations called by the scheduler,
  88. * these functions should be implemented in driver side
  89. */
  90. struct amd_sched_backend_ops {
  91. struct dma_fence *(*dependency)(struct amd_sched_job *sched_job);
  92. struct dma_fence *(*run_job)(struct amd_sched_job *sched_job);
  93. void (*timedout_job)(struct amd_sched_job *sched_job);
  94. void (*free_job)(struct amd_sched_job *sched_job);
  95. };
  96. enum amd_sched_priority {
  97. AMD_SCHED_PRIORITY_KERNEL = 0,
  98. AMD_SCHED_PRIORITY_NORMAL,
  99. AMD_SCHED_MAX_PRIORITY
  100. };
  101. /**
  102. * One scheduler is implemented for each hardware ring
  103. */
  104. struct amd_gpu_scheduler {
  105. const struct amd_sched_backend_ops *ops;
  106. uint32_t hw_submission_limit;
  107. long timeout;
  108. const char *name;
  109. struct amd_sched_rq sched_rq[AMD_SCHED_MAX_PRIORITY];
  110. wait_queue_head_t wake_up_worker;
  111. wait_queue_head_t job_scheduled;
  112. atomic_t hw_rq_count;
  113. struct task_struct *thread;
  114. struct list_head ring_mirror_list;
  115. spinlock_t job_list_lock;
  116. };
  117. int amd_sched_init(struct amd_gpu_scheduler *sched,
  118. const struct amd_sched_backend_ops *ops,
  119. uint32_t hw_submission, long timeout, const char *name);
  120. void amd_sched_fini(struct amd_gpu_scheduler *sched);
  121. int amd_sched_entity_init(struct amd_gpu_scheduler *sched,
  122. struct amd_sched_entity *entity,
  123. struct amd_sched_rq *rq,
  124. uint32_t jobs);
  125. void amd_sched_entity_fini(struct amd_gpu_scheduler *sched,
  126. struct amd_sched_entity *entity);
  127. void amd_sched_entity_push_job(struct amd_sched_job *sched_job);
  128. struct amd_sched_fence *amd_sched_fence_create(
  129. struct amd_sched_entity *s_entity, void *owner);
  130. void amd_sched_fence_scheduled(struct amd_sched_fence *fence);
  131. void amd_sched_fence_finished(struct amd_sched_fence *fence);
  132. int amd_sched_job_init(struct amd_sched_job *job,
  133. struct amd_gpu_scheduler *sched,
  134. struct amd_sched_entity *entity,
  135. void *owner);
  136. void amd_sched_hw_job_reset(struct amd_gpu_scheduler *sched);
  137. void amd_sched_job_recovery(struct amd_gpu_scheduler *sched);
  138. #endif