gpu_scheduler.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #define AMD_KERNEL_CONTEXT_ID 0
  27. #define AMD_KERNEL_PROCESS_ID 0
  28. #define AMD_GPU_WAIT_IDLE_TIMEOUT_IN_MS 3000
  29. struct amd_gpu_scheduler;
  30. struct amd_run_queue;
  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_run_queue *belongto_rq;
  40. struct amd_sched_entity *parent;
  41. };
  42. /**
  43. * Run queue is a set of entities scheduling command submissions for
  44. * one specific ring. It implements the scheduling policy that selects
  45. * the next entity to emit commands from.
  46. */
  47. struct amd_run_queue {
  48. struct mutex lock;
  49. atomic_t nr_entity;
  50. struct amd_sched_entity head;
  51. struct amd_sched_entity *current_entity;
  52. /**
  53. * Return 0 means this entity can be scheduled
  54. * Return -1 means this entity cannot be scheduled for reasons,
  55. * i.e, it is the head, or these is no job, etc
  56. */
  57. int (*check_entity_status)(struct amd_sched_entity *entity);
  58. };
  59. /**
  60. * Context based scheduler entity, there can be multiple entities for
  61. * each context, and one entity per ring
  62. */
  63. struct amd_context_entity {
  64. struct amd_sched_entity generic_entity;
  65. spinlock_t lock;
  66. /* the virtual_seq is unique per context per ring */
  67. atomic64_t last_queued_v_seq;
  68. atomic64_t last_emitted_v_seq;
  69. atomic64_t last_signaled_v_seq;
  70. pid_t tgid;
  71. uint32_t context_id;
  72. /* the job_queue maintains the jobs submitted by clients */
  73. struct kfifo job_queue;
  74. spinlock_t queue_lock;
  75. struct amd_gpu_scheduler *scheduler;
  76. wait_queue_head_t wait_queue;
  77. wait_queue_head_t wait_emit;
  78. bool is_pending;
  79. };
  80. /**
  81. * Define the backend operations called by the scheduler,
  82. * these functions should be implemented in driver side
  83. */
  84. struct amd_sched_backend_ops {
  85. int (*prepare_job)(struct amd_gpu_scheduler *sched,
  86. struct amd_context_entity *c_entity,
  87. void *job);
  88. void (*run_job)(struct amd_gpu_scheduler *sched,
  89. struct amd_context_entity *c_entity,
  90. void *job);
  91. void (*process_job)(struct amd_gpu_scheduler *sched, void *job);
  92. };
  93. /**
  94. * One scheduler is implemented for each hardware ring
  95. */
  96. struct amd_gpu_scheduler {
  97. void *device;
  98. struct task_struct *thread;
  99. struct amd_run_queue sched_rq;
  100. struct amd_run_queue kernel_rq;
  101. struct kfifo active_hw_rq;
  102. struct amd_sched_backend_ops *ops;
  103. uint32_t ring_id;
  104. uint32_t granularity; /* in ms unit */
  105. uint32_t preemption;
  106. uint64_t last_handled_seq;
  107. wait_queue_head_t wait_queue;
  108. struct amd_context_entity *current_entity;
  109. struct mutex sched_lock;
  110. spinlock_t queue_lock;
  111. };
  112. struct amd_gpu_scheduler *amd_sched_create(void *device,
  113. struct amd_sched_backend_ops *ops,
  114. uint32_t ring,
  115. uint32_t granularity,
  116. uint32_t preemption,
  117. uint32_t hw_submission);
  118. int amd_sched_destroy(struct amd_gpu_scheduler *sched);
  119. int amd_sched_push_job(struct amd_gpu_scheduler *sched,
  120. struct amd_context_entity *c_entity,
  121. void *job);
  122. int amd_sched_check_ts(struct amd_context_entity *c_entity, uint64_t seq);
  123. int amd_sched_wait_signal(struct amd_context_entity *c_entity,
  124. uint64_t seq, bool intr, long timeout);
  125. int amd_sched_wait_emit(struct amd_context_entity *c_entity,
  126. uint64_t seq,
  127. bool intr,
  128. long timeout);
  129. void amd_sched_isr(struct amd_gpu_scheduler *sched);
  130. uint64_t amd_sched_get_handled_seq(struct amd_gpu_scheduler *sched);
  131. int amd_context_entity_fini(struct amd_gpu_scheduler *sched,
  132. struct amd_context_entity *entity);
  133. int amd_context_entity_init(struct amd_gpu_scheduler *sched,
  134. struct amd_context_entity *entity,
  135. struct amd_sched_entity *parent,
  136. struct amd_run_queue *rq,
  137. uint32_t context_id,
  138. uint32_t jobs);
  139. #endif