gpu_scheduler.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 _DRM_GPU_SCHEDULER_H_
  24. #define _DRM_GPU_SCHEDULER_H_
  25. #include <drm/spsc_queue.h>
  26. #include <linux/dma-fence.h>
  27. #define MAX_WAIT_SCHED_ENTITY_Q_EMPTY msecs_to_jiffies(1000)
  28. struct drm_gpu_scheduler;
  29. struct drm_sched_rq;
  30. enum drm_sched_priority {
  31. DRM_SCHED_PRIORITY_MIN,
  32. DRM_SCHED_PRIORITY_LOW = DRM_SCHED_PRIORITY_MIN,
  33. DRM_SCHED_PRIORITY_NORMAL,
  34. DRM_SCHED_PRIORITY_HIGH_SW,
  35. DRM_SCHED_PRIORITY_HIGH_HW,
  36. DRM_SCHED_PRIORITY_KERNEL,
  37. DRM_SCHED_PRIORITY_MAX,
  38. DRM_SCHED_PRIORITY_INVALID = -1,
  39. DRM_SCHED_PRIORITY_UNSET = -2
  40. };
  41. /**
  42. * struct drm_sched_entity - A wrapper around a job queue (typically
  43. * attached to the DRM file_priv).
  44. *
  45. * @list: used to append this struct to the list of entities in the
  46. * runqueue.
  47. * @rq: runqueue to which this entity belongs.
  48. * @rq_lock: lock to modify the runqueue to which this entity belongs.
  49. * @job_queue: the list of jobs of this entity.
  50. * @fence_seq: a linearly increasing seqno incremented with each
  51. * new &drm_sched_fence which is part of the entity.
  52. * @fence_context: a unique context for all the fences which belong
  53. * to this entity.
  54. * The &drm_sched_fence.scheduled uses the
  55. * fence_context but &drm_sched_fence.finished uses
  56. * fence_context + 1.
  57. * @dependency: the dependency fence of the job which is on the top
  58. * of the job queue.
  59. * @cb: callback for the dependency fence above.
  60. * @guilty: points to ctx's guilty.
  61. * @fini_status: contains the exit status in case the process was signalled.
  62. * @last_scheduled: points to the finished fence of the last scheduled job.
  63. * @last_user: last group leader pushing a job into the entity.
  64. *
  65. * Entities will emit jobs in order to their corresponding hardware
  66. * ring, and the scheduler will alternate between entities based on
  67. * scheduling policy.
  68. */
  69. struct drm_sched_entity {
  70. struct list_head list;
  71. struct drm_sched_rq *rq;
  72. spinlock_t rq_lock;
  73. struct spsc_queue job_queue;
  74. atomic_t fence_seq;
  75. uint64_t fence_context;
  76. struct dma_fence *dependency;
  77. struct dma_fence_cb cb;
  78. atomic_t *guilty;
  79. struct dma_fence *last_scheduled;
  80. struct task_struct *last_user;
  81. };
  82. /**
  83. * struct drm_sched_rq - queue of entities to be scheduled.
  84. *
  85. * @lock: to modify the entities list.
  86. * @sched: the scheduler to which this rq belongs to.
  87. * @entities: list of the entities to be scheduled.
  88. * @current_entity: the entity which is to be scheduled.
  89. *
  90. * Run queue is a set of entities scheduling command submissions for
  91. * one specific ring. It implements the scheduling policy that selects
  92. * the next entity to emit commands from.
  93. */
  94. struct drm_sched_rq {
  95. spinlock_t lock;
  96. struct drm_gpu_scheduler *sched;
  97. struct list_head entities;
  98. struct drm_sched_entity *current_entity;
  99. };
  100. /**
  101. * struct drm_sched_fence - fences corresponding to the scheduling of a job.
  102. */
  103. struct drm_sched_fence {
  104. /**
  105. * @scheduled: this fence is what will be signaled by the scheduler
  106. * when the job is scheduled.
  107. */
  108. struct dma_fence scheduled;
  109. /**
  110. * @finished: this fence is what will be signaled by the scheduler
  111. * when the job is completed.
  112. *
  113. * When setting up an out fence for the job, you should use
  114. * this, since it's available immediately upon
  115. * drm_sched_job_init(), and the fence returned by the driver
  116. * from run_job() won't be created until the dependencies have
  117. * resolved.
  118. */
  119. struct dma_fence finished;
  120. /**
  121. * @cb: the callback for the parent fence below.
  122. */
  123. struct dma_fence_cb cb;
  124. /**
  125. * @parent: the fence returned by &drm_sched_backend_ops.run_job
  126. * when scheduling the job on hardware. We signal the
  127. * &drm_sched_fence.finished fence once parent is signalled.
  128. */
  129. struct dma_fence *parent;
  130. /**
  131. * @sched: the scheduler instance to which the job having this struct
  132. * belongs to.
  133. */
  134. struct drm_gpu_scheduler *sched;
  135. /**
  136. * @lock: the lock used by the scheduled and the finished fences.
  137. */
  138. spinlock_t lock;
  139. /**
  140. * @owner: job owner for debugging
  141. */
  142. void *owner;
  143. };
  144. struct drm_sched_fence *to_drm_sched_fence(struct dma_fence *f);
  145. /**
  146. * struct drm_sched_job - A job to be run by an entity.
  147. *
  148. * @queue_node: used to append this struct to the queue of jobs in an entity.
  149. * @sched: the scheduler instance on which this job is scheduled.
  150. * @s_fence: contains the fences for the scheduling of job.
  151. * @finish_cb: the callback for the finished fence.
  152. * @finish_work: schedules the function @drm_sched_job_finish once the job has
  153. * finished to remove the job from the
  154. * @drm_gpu_scheduler.ring_mirror_list.
  155. * @node: used to append this struct to the @drm_gpu_scheduler.ring_mirror_list.
  156. * @work_tdr: schedules a delayed call to @drm_sched_job_timedout after the timeout
  157. * interval is over.
  158. * @id: a unique id assigned to each job scheduled on the scheduler.
  159. * @karma: increment on every hang caused by this job. If this exceeds the hang
  160. * limit of the scheduler then the job is marked guilty and will not
  161. * be scheduled further.
  162. * @s_priority: the priority of the job.
  163. * @entity: the entity to which this job belongs.
  164. *
  165. * A job is created by the driver using drm_sched_job_init(), and
  166. * should call drm_sched_entity_push_job() once it wants the scheduler
  167. * to schedule the job.
  168. */
  169. struct drm_sched_job {
  170. struct spsc_node queue_node;
  171. struct drm_gpu_scheduler *sched;
  172. struct drm_sched_fence *s_fence;
  173. struct dma_fence_cb finish_cb;
  174. struct work_struct finish_work;
  175. struct list_head node;
  176. struct delayed_work work_tdr;
  177. uint64_t id;
  178. atomic_t karma;
  179. enum drm_sched_priority s_priority;
  180. struct drm_sched_entity *entity;
  181. };
  182. static inline bool drm_sched_invalidate_job(struct drm_sched_job *s_job,
  183. int threshold)
  184. {
  185. return (s_job && atomic_inc_return(&s_job->karma) > threshold);
  186. }
  187. /**
  188. * struct drm_sched_backend_ops
  189. *
  190. * Define the backend operations called by the scheduler,
  191. * these functions should be implemented in driver side.
  192. */
  193. struct drm_sched_backend_ops {
  194. /**
  195. * @dependency: Called when the scheduler is considering scheduling
  196. * this job next, to get another struct dma_fence for this job to
  197. * block on. Once it returns NULL, run_job() may be called.
  198. */
  199. struct dma_fence *(*dependency)(struct drm_sched_job *sched_job,
  200. struct drm_sched_entity *s_entity);
  201. /**
  202. * @run_job: Called to execute the job once all of the dependencies
  203. * have been resolved. This may be called multiple times, if
  204. * timedout_job() has happened and drm_sched_job_recovery()
  205. * decides to try it again.
  206. */
  207. struct dma_fence *(*run_job)(struct drm_sched_job *sched_job);
  208. /**
  209. * @timedout_job: Called when a job has taken too long to execute,
  210. * to trigger GPU recovery.
  211. */
  212. void (*timedout_job)(struct drm_sched_job *sched_job);
  213. /**
  214. * @free_job: Called once the job's finished fence has been signaled
  215. * and it's time to clean it up.
  216. */
  217. void (*free_job)(struct drm_sched_job *sched_job);
  218. };
  219. /**
  220. * struct drm_gpu_scheduler
  221. *
  222. * @ops: backend operations provided by the driver.
  223. * @hw_submission_limit: the max size of the hardware queue.
  224. * @timeout: the time after which a job is removed from the scheduler.
  225. * @name: name of the ring for which this scheduler is being used.
  226. * @sched_rq: priority wise array of run queues.
  227. * @wake_up_worker: the wait queue on which the scheduler sleeps until a job
  228. * is ready to be scheduled.
  229. * @job_scheduled: once @drm_sched_entity_do_release is called the scheduler
  230. * waits on this wait queue until all the scheduled jobs are
  231. * finished.
  232. * @hw_rq_count: the number of jobs currently in the hardware queue.
  233. * @job_id_count: used to assign unique id to the each job.
  234. * @thread: the kthread on which the scheduler which run.
  235. * @ring_mirror_list: the list of jobs which are currently in the job queue.
  236. * @job_list_lock: lock to protect the ring_mirror_list.
  237. * @hang_limit: once the hangs by a job crosses this limit then it is marked
  238. * guilty and it will be considered for scheduling further.
  239. *
  240. * One scheduler is implemented for each hardware ring.
  241. */
  242. struct drm_gpu_scheduler {
  243. const struct drm_sched_backend_ops *ops;
  244. uint32_t hw_submission_limit;
  245. long timeout;
  246. const char *name;
  247. struct drm_sched_rq sched_rq[DRM_SCHED_PRIORITY_MAX];
  248. wait_queue_head_t wake_up_worker;
  249. wait_queue_head_t job_scheduled;
  250. atomic_t hw_rq_count;
  251. atomic64_t job_id_count;
  252. struct task_struct *thread;
  253. struct list_head ring_mirror_list;
  254. spinlock_t job_list_lock;
  255. int hang_limit;
  256. };
  257. int drm_sched_init(struct drm_gpu_scheduler *sched,
  258. const struct drm_sched_backend_ops *ops,
  259. uint32_t hw_submission, unsigned hang_limit, long timeout,
  260. const char *name);
  261. void drm_sched_fini(struct drm_gpu_scheduler *sched);
  262. int drm_sched_entity_init(struct drm_sched_entity *entity,
  263. struct drm_sched_rq **rq_list,
  264. unsigned int num_rq_list,
  265. atomic_t *guilty);
  266. long drm_sched_entity_flush(struct drm_sched_entity *entity, long timeout);
  267. void drm_sched_entity_fini(struct drm_sched_entity *entity);
  268. void drm_sched_entity_destroy(struct drm_sched_entity *entity);
  269. void drm_sched_entity_push_job(struct drm_sched_job *sched_job,
  270. struct drm_sched_entity *entity);
  271. void drm_sched_entity_set_rq(struct drm_sched_entity *entity,
  272. struct drm_sched_rq *rq);
  273. struct drm_sched_fence *drm_sched_fence_create(
  274. struct drm_sched_entity *s_entity, void *owner);
  275. void drm_sched_fence_scheduled(struct drm_sched_fence *fence);
  276. void drm_sched_fence_finished(struct drm_sched_fence *fence);
  277. int drm_sched_job_init(struct drm_sched_job *job,
  278. struct drm_sched_entity *entity,
  279. void *owner);
  280. void drm_sched_hw_job_reset(struct drm_gpu_scheduler *sched,
  281. struct drm_sched_job *job);
  282. void drm_sched_job_recovery(struct drm_gpu_scheduler *sched);
  283. bool drm_sched_dependency_optimized(struct dma_fence* fence,
  284. struct drm_sched_entity *entity);
  285. void drm_sched_job_kickout(struct drm_sched_job *s_job);
  286. #endif