gpu_scheduler.h 5.1 KB

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