gpu_scheduler.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. atomic_t fence_seq;
  41. /* the job_queue maintains the jobs submitted by clients */
  42. struct kfifo job_queue;
  43. spinlock_t queue_lock;
  44. struct amd_gpu_scheduler *scheduler;
  45. wait_queue_head_t wait_queue;
  46. wait_queue_head_t wait_emit;
  47. bool is_pending;
  48. uint64_t fence_context;
  49. char name[20];
  50. bool need_wakeup;
  51. };
  52. /**
  53. * Run queue is a set of entities scheduling command submissions for
  54. * one specific ring. It implements the scheduling policy that selects
  55. * the next entity to emit commands from.
  56. */
  57. struct amd_sched_rq {
  58. spinlock_t lock;
  59. struct list_head entities;
  60. struct amd_sched_entity *current_entity;
  61. };
  62. struct amd_sched_fence {
  63. struct fence base;
  64. struct fence_cb cb;
  65. struct amd_sched_entity *entity;
  66. spinlock_t lock;
  67. };
  68. struct amd_sched_job {
  69. struct list_head list;
  70. struct fence_cb cb;
  71. struct amd_gpu_scheduler *sched;
  72. struct amd_sched_entity *s_entity;
  73. struct amd_sched_fence *s_fence;
  74. };
  75. extern const struct fence_ops amd_sched_fence_ops;
  76. static inline struct amd_sched_fence *to_amd_sched_fence(struct fence *f)
  77. {
  78. struct amd_sched_fence *__f = container_of(f, struct amd_sched_fence, base);
  79. if (__f->base.ops == &amd_sched_fence_ops)
  80. return __f;
  81. return NULL;
  82. }
  83. /**
  84. * Define the backend operations called by the scheduler,
  85. * these functions should be implemented in driver side
  86. */
  87. struct amd_sched_backend_ops {
  88. int (*prepare_job)(struct amd_gpu_scheduler *sched,
  89. struct amd_sched_entity *c_entity,
  90. struct amd_sched_job *job);
  91. struct fence *(*run_job)(struct amd_gpu_scheduler *sched,
  92. struct amd_sched_entity *c_entity,
  93. struct amd_sched_job *job);
  94. void (*process_job)(struct amd_gpu_scheduler *sched,
  95. struct amd_sched_job *job);
  96. };
  97. /**
  98. * One scheduler is implemented for each hardware ring
  99. */
  100. struct amd_gpu_scheduler {
  101. void *device;
  102. struct task_struct *thread;
  103. struct amd_sched_rq sched_rq;
  104. struct amd_sched_rq kernel_rq;
  105. struct list_head active_hw_rq;
  106. atomic64_t hw_rq_count;
  107. struct amd_sched_backend_ops *ops;
  108. uint32_t ring_id;
  109. uint32_t granularity; /* in ms unit */
  110. uint32_t preemption;
  111. wait_queue_head_t wait_queue;
  112. struct amd_sched_entity *current_entity;
  113. struct mutex sched_lock;
  114. spinlock_t queue_lock;
  115. uint32_t hw_submission_limit;
  116. };
  117. struct amd_gpu_scheduler *amd_sched_create(void *device,
  118. struct amd_sched_backend_ops *ops,
  119. uint32_t ring,
  120. uint32_t granularity,
  121. uint32_t preemption,
  122. uint32_t hw_submission);
  123. int amd_sched_destroy(struct amd_gpu_scheduler *sched);
  124. int amd_sched_push_job(struct amd_sched_job *sched_job);
  125. int amd_sched_entity_init(struct amd_gpu_scheduler *sched,
  126. struct amd_sched_entity *entity,
  127. struct amd_sched_rq *rq,
  128. uint32_t jobs);
  129. int amd_sched_entity_fini(struct amd_gpu_scheduler *sched,
  130. struct amd_sched_entity *entity);
  131. struct amd_sched_fence *amd_sched_fence_create(
  132. struct amd_sched_entity *s_entity);
  133. void amd_sched_fence_signal(struct amd_sched_fence *fence);
  134. #endif