gpu_scheduler.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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_run_queue;
  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_run_queue *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_emitted_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. };
  51. /**
  52. * Run queue is a set of entities scheduling command submissions for
  53. * one specific ring. It implements the scheduling policy that selects
  54. * the next entity to emit commands from.
  55. */
  56. struct amd_run_queue {
  57. struct mutex lock;
  58. atomic_t nr_entity;
  59. struct amd_sched_entity head;
  60. struct amd_sched_entity *current_entity;
  61. /**
  62. * Return 0 means this entity can be scheduled
  63. * Return -1 means this entity cannot be scheduled for reasons,
  64. * i.e, it is the head, or these is no job, etc
  65. */
  66. int (*check_entity_status)(struct amd_sched_entity *entity);
  67. };
  68. struct amd_sched_job {
  69. struct list_head list;
  70. struct fence_cb cb;
  71. struct amd_gpu_scheduler *sched;
  72. void *job;
  73. };
  74. /**
  75. * Define the backend operations called by the scheduler,
  76. * these functions should be implemented in driver side
  77. */
  78. struct amd_sched_backend_ops {
  79. int (*prepare_job)(struct amd_gpu_scheduler *sched,
  80. struct amd_sched_entity *c_entity,
  81. void *job);
  82. struct fence *(*run_job)(struct amd_gpu_scheduler *sched,
  83. struct amd_sched_entity *c_entity,
  84. struct amd_sched_job *job);
  85. void (*process_job)(struct amd_gpu_scheduler *sched, void *job);
  86. };
  87. /**
  88. * One scheduler is implemented for each hardware ring
  89. */
  90. struct amd_gpu_scheduler {
  91. void *device;
  92. struct task_struct *thread;
  93. struct amd_run_queue sched_rq;
  94. struct amd_run_queue kernel_rq;
  95. struct list_head active_hw_rq;
  96. atomic64_t hw_rq_count;
  97. struct amd_sched_backend_ops *ops;
  98. uint32_t ring_id;
  99. uint32_t granularity; /* in ms unit */
  100. uint32_t preemption;
  101. wait_queue_head_t wait_queue;
  102. struct amd_sched_entity *current_entity;
  103. struct mutex sched_lock;
  104. spinlock_t queue_lock;
  105. uint32_t hw_submission_limit;
  106. };
  107. struct amd_gpu_scheduler *amd_sched_create(void *device,
  108. struct amd_sched_backend_ops *ops,
  109. uint32_t ring,
  110. uint32_t granularity,
  111. uint32_t preemption,
  112. uint32_t hw_submission);
  113. int amd_sched_destroy(struct amd_gpu_scheduler *sched);
  114. int amd_sched_push_job(struct amd_gpu_scheduler *sched,
  115. struct amd_sched_entity *c_entity,
  116. void *job);
  117. int amd_sched_wait_emit(struct amd_sched_entity *c_entity,
  118. uint64_t seq,
  119. bool intr,
  120. long timeout);
  121. uint64_t amd_sched_get_handled_seq(struct amd_gpu_scheduler *sched);
  122. int amd_sched_entity_init(struct amd_gpu_scheduler *sched,
  123. struct amd_sched_entity *entity,
  124. struct amd_run_queue *rq,
  125. uint32_t jobs);
  126. int amd_sched_entity_fini(struct amd_gpu_scheduler *sched,
  127. struct amd_sched_entity *entity);
  128. void amd_sched_emit(struct amd_sched_entity *c_entity, uint64_t seq);
  129. uint64_t amd_sched_next_queued_seq(struct amd_sched_entity *c_entity);
  130. #endif