gpu_scheduler.h 4.9 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/fence.h>
  27. #define AMD_GPU_WAIT_IDLE_TIMEOUT_IN_MS 3000
  28. struct amd_gpu_scheduler;
  29. struct amd_sched_rq;
  30. /**
  31. * A scheduler entity is a wrapper around a job queue or a group
  32. * of other entities. Entities take turns emitting jobs from their
  33. * job queues to corresponding hardware ring based on scheduling
  34. * policy.
  35. */
  36. struct amd_sched_entity {
  37. struct list_head list;
  38. struct amd_sched_rq *belongto_rq;
  39. spinlock_t lock;
  40. /* the virtual_seq is unique per context per ring */
  41. atomic64_t last_queued_v_seq;
  42. atomic64_t last_signaled_v_seq;
  43. /* the job_queue maintains the jobs submitted by clients */
  44. struct kfifo job_queue;
  45. spinlock_t queue_lock;
  46. struct amd_gpu_scheduler *scheduler;
  47. wait_queue_head_t wait_queue;
  48. wait_queue_head_t wait_emit;
  49. bool is_pending;
  50. uint64_t fence_context;
  51. char name[20];
  52. bool need_wakeup;
  53. };
  54. /**
  55. * Run queue is a set of entities scheduling command submissions for
  56. * one specific ring. It implements the scheduling policy that selects
  57. * the next entity to emit commands from.
  58. */
  59. struct amd_sched_rq {
  60. struct mutex lock;
  61. struct list_head entities;
  62. struct amd_sched_entity *current_entity;
  63. };
  64. struct amd_sched_fence {
  65. struct fence base;
  66. struct fence_cb cb;
  67. struct amd_sched_entity *entity;
  68. uint64_t v_seq;
  69. spinlock_t lock;
  70. };
  71. struct amd_sched_job {
  72. struct list_head list;
  73. struct fence_cb cb;
  74. struct amd_gpu_scheduler *sched;
  75. struct amd_sched_entity *s_entity;
  76. struct amd_sched_fence *s_fence;
  77. };
  78. extern const struct fence_ops amd_sched_fence_ops;
  79. static inline struct amd_sched_fence *to_amd_sched_fence(struct fence *f)
  80. {
  81. struct amd_sched_fence *__f = container_of(f, struct amd_sched_fence, base);
  82. if (__f->base.ops == &amd_sched_fence_ops)
  83. return __f;
  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. int (*prepare_job)(struct amd_gpu_scheduler *sched,
  92. struct amd_sched_entity *c_entity,
  93. struct amd_sched_job *job);
  94. struct fence *(*run_job)(struct amd_gpu_scheduler *sched,
  95. struct amd_sched_entity *c_entity,
  96. struct amd_sched_job *job);
  97. void (*process_job)(struct amd_gpu_scheduler *sched,
  98. struct amd_sched_job *job);
  99. };
  100. /**
  101. * One scheduler is implemented for each hardware ring
  102. */
  103. struct amd_gpu_scheduler {
  104. void *device;
  105. struct task_struct *thread;
  106. struct amd_sched_rq sched_rq;
  107. struct amd_sched_rq kernel_rq;
  108. struct list_head active_hw_rq;
  109. atomic64_t hw_rq_count;
  110. struct amd_sched_backend_ops *ops;
  111. uint32_t ring_id;
  112. uint32_t granularity; /* in ms unit */
  113. uint32_t preemption;
  114. wait_queue_head_t wait_queue;
  115. struct amd_sched_entity *current_entity;
  116. struct mutex sched_lock;
  117. spinlock_t queue_lock;
  118. uint32_t hw_submission_limit;
  119. };
  120. struct amd_gpu_scheduler *amd_sched_create(void *device,
  121. struct amd_sched_backend_ops *ops,
  122. uint32_t ring,
  123. uint32_t granularity,
  124. uint32_t preemption,
  125. uint32_t hw_submission);
  126. int amd_sched_destroy(struct amd_gpu_scheduler *sched);
  127. int amd_sched_push_job(struct amd_sched_job *sched_job);
  128. int amd_sched_entity_init(struct amd_gpu_scheduler *sched,
  129. struct amd_sched_entity *entity,
  130. struct amd_sched_rq *rq,
  131. uint32_t jobs);
  132. int amd_sched_entity_fini(struct amd_gpu_scheduler *sched,
  133. struct amd_sched_entity *entity);
  134. uint64_t amd_sched_next_queued_seq(struct amd_sched_entity *c_entity);
  135. struct amd_sched_fence *amd_sched_fence_create(
  136. struct amd_sched_entity *s_entity);
  137. void amd_sched_fence_signal(struct amd_sched_fence *fence);
  138. #endif