gpu_scheduler.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. */
  24. #include <linux/kthread.h>
  25. #include <linux/wait.h>
  26. #include <linux/sched.h>
  27. #include <drm/drmP.h>
  28. #include "gpu_scheduler.h"
  29. #define CREATE_TRACE_POINTS
  30. #include "gpu_sched_trace.h"
  31. static struct amd_sched_job *
  32. amd_sched_entity_pop_job(struct amd_sched_entity *entity);
  33. static void amd_sched_wakeup(struct amd_gpu_scheduler *sched);
  34. /* Initialize a given run queue struct */
  35. static void amd_sched_rq_init(struct amd_sched_rq *rq)
  36. {
  37. spin_lock_init(&rq->lock);
  38. INIT_LIST_HEAD(&rq->entities);
  39. rq->current_entity = NULL;
  40. }
  41. static void amd_sched_rq_add_entity(struct amd_sched_rq *rq,
  42. struct amd_sched_entity *entity)
  43. {
  44. spin_lock(&rq->lock);
  45. list_add_tail(&entity->list, &rq->entities);
  46. spin_unlock(&rq->lock);
  47. }
  48. static void amd_sched_rq_remove_entity(struct amd_sched_rq *rq,
  49. struct amd_sched_entity *entity)
  50. {
  51. spin_lock(&rq->lock);
  52. list_del_init(&entity->list);
  53. if (rq->current_entity == entity)
  54. rq->current_entity = NULL;
  55. spin_unlock(&rq->lock);
  56. }
  57. /**
  58. * Select next job from a specified run queue with round robin policy.
  59. * Return NULL if nothing available.
  60. */
  61. static struct amd_sched_job *
  62. amd_sched_rq_select_job(struct amd_sched_rq *rq)
  63. {
  64. struct amd_sched_entity *entity;
  65. struct amd_sched_job *sched_job;
  66. spin_lock(&rq->lock);
  67. entity = rq->current_entity;
  68. if (entity) {
  69. list_for_each_entry_continue(entity, &rq->entities, list) {
  70. sched_job = amd_sched_entity_pop_job(entity);
  71. if (sched_job) {
  72. rq->current_entity = entity;
  73. spin_unlock(&rq->lock);
  74. return sched_job;
  75. }
  76. }
  77. }
  78. list_for_each_entry(entity, &rq->entities, list) {
  79. sched_job = amd_sched_entity_pop_job(entity);
  80. if (sched_job) {
  81. rq->current_entity = entity;
  82. spin_unlock(&rq->lock);
  83. return sched_job;
  84. }
  85. if (entity == rq->current_entity)
  86. break;
  87. }
  88. spin_unlock(&rq->lock);
  89. return NULL;
  90. }
  91. /**
  92. * Init a context entity used by scheduler when submit to HW ring.
  93. *
  94. * @sched The pointer to the scheduler
  95. * @entity The pointer to a valid amd_sched_entity
  96. * @rq The run queue this entity belongs
  97. * @kernel If this is an entity for the kernel
  98. * @jobs The max number of jobs in the job queue
  99. *
  100. * return 0 if succeed. negative error code on failure
  101. */
  102. int amd_sched_entity_init(struct amd_gpu_scheduler *sched,
  103. struct amd_sched_entity *entity,
  104. struct amd_sched_rq *rq,
  105. uint32_t jobs)
  106. {
  107. int r;
  108. if (!(sched && entity && rq))
  109. return -EINVAL;
  110. memset(entity, 0, sizeof(struct amd_sched_entity));
  111. INIT_LIST_HEAD(&entity->list);
  112. entity->rq = rq;
  113. entity->sched = sched;
  114. spin_lock_init(&entity->queue_lock);
  115. r = kfifo_alloc(&entity->job_queue, jobs * sizeof(void *), GFP_KERNEL);
  116. if (r)
  117. return r;
  118. atomic_set(&entity->fence_seq, 0);
  119. entity->fence_context = fence_context_alloc(1);
  120. /* Add the entity to the run queue */
  121. amd_sched_rq_add_entity(rq, entity);
  122. return 0;
  123. }
  124. /**
  125. * Query if entity is initialized
  126. *
  127. * @sched Pointer to scheduler instance
  128. * @entity The pointer to a valid scheduler entity
  129. *
  130. * return true if entity is initialized, false otherwise
  131. */
  132. static bool amd_sched_entity_is_initialized(struct amd_gpu_scheduler *sched,
  133. struct amd_sched_entity *entity)
  134. {
  135. return entity->sched == sched &&
  136. entity->rq != NULL;
  137. }
  138. /**
  139. * Check if entity is idle
  140. *
  141. * @entity The pointer to a valid scheduler entity
  142. *
  143. * Return true if entity don't has any unscheduled jobs.
  144. */
  145. static bool amd_sched_entity_is_idle(struct amd_sched_entity *entity)
  146. {
  147. rmb();
  148. if (kfifo_is_empty(&entity->job_queue))
  149. return true;
  150. return false;
  151. }
  152. /**
  153. * Destroy a context entity
  154. *
  155. * @sched Pointer to scheduler instance
  156. * @entity The pointer to a valid scheduler entity
  157. *
  158. * Cleanup and free the allocated resources.
  159. */
  160. void amd_sched_entity_fini(struct amd_gpu_scheduler *sched,
  161. struct amd_sched_entity *entity)
  162. {
  163. struct amd_sched_rq *rq = entity->rq;
  164. if (!amd_sched_entity_is_initialized(sched, entity))
  165. return;
  166. /**
  167. * The client will not queue more IBs during this fini, consume existing
  168. * queued IBs
  169. */
  170. wait_event(sched->job_scheduled, amd_sched_entity_is_idle(entity));
  171. amd_sched_rq_remove_entity(rq, entity);
  172. kfifo_free(&entity->job_queue);
  173. }
  174. static void amd_sched_entity_wakeup(struct fence *f, struct fence_cb *cb)
  175. {
  176. struct amd_sched_entity *entity =
  177. container_of(cb, struct amd_sched_entity, cb);
  178. entity->dependency = NULL;
  179. fence_put(f);
  180. amd_sched_wakeup(entity->sched);
  181. }
  182. static struct amd_sched_job *
  183. amd_sched_entity_pop_job(struct amd_sched_entity *entity)
  184. {
  185. struct amd_gpu_scheduler *sched = entity->sched;
  186. struct amd_sched_job *sched_job;
  187. if (ACCESS_ONCE(entity->dependency))
  188. return NULL;
  189. if (!kfifo_out_peek(&entity->job_queue, &sched_job, sizeof(sched_job)))
  190. return NULL;
  191. while ((entity->dependency = sched->ops->dependency(sched_job))) {
  192. if (fence_add_callback(entity->dependency, &entity->cb,
  193. amd_sched_entity_wakeup))
  194. fence_put(entity->dependency);
  195. else
  196. return NULL;
  197. }
  198. return sched_job;
  199. }
  200. /**
  201. * Helper to submit a job to the job queue
  202. *
  203. * @sched_job The pointer to job required to submit
  204. *
  205. * Returns true if we could submit the job.
  206. */
  207. static bool amd_sched_entity_in(struct amd_sched_job *sched_job)
  208. {
  209. struct amd_sched_entity *entity = sched_job->s_entity;
  210. bool added, first = false;
  211. spin_lock(&entity->queue_lock);
  212. added = kfifo_in(&entity->job_queue, &sched_job,
  213. sizeof(sched_job)) == sizeof(sched_job);
  214. if (added && kfifo_len(&entity->job_queue) == sizeof(sched_job))
  215. first = true;
  216. spin_unlock(&entity->queue_lock);
  217. /* first job wakes up scheduler */
  218. if (first)
  219. amd_sched_wakeup(sched_job->sched);
  220. return added;
  221. }
  222. /**
  223. * Submit a job to the job queue
  224. *
  225. * @sched_job The pointer to job required to submit
  226. *
  227. * Returns 0 for success, negative error code otherwise.
  228. */
  229. int amd_sched_entity_push_job(struct amd_sched_job *sched_job)
  230. {
  231. struct amd_sched_entity *entity = sched_job->s_entity;
  232. struct amd_sched_fence *fence = amd_sched_fence_create(
  233. entity, sched_job->owner);
  234. if (!fence)
  235. return -ENOMEM;
  236. fence_get(&fence->base);
  237. sched_job->s_fence = fence;
  238. wait_event(entity->sched->job_scheduled,
  239. amd_sched_entity_in(sched_job));
  240. trace_amd_sched_job(sched_job);
  241. return 0;
  242. }
  243. /**
  244. * Return ture if we can push more jobs to the hw.
  245. */
  246. static bool amd_sched_ready(struct amd_gpu_scheduler *sched)
  247. {
  248. return atomic_read(&sched->hw_rq_count) <
  249. sched->hw_submission_limit;
  250. }
  251. /**
  252. * Wake up the scheduler when it is ready
  253. */
  254. static void amd_sched_wakeup(struct amd_gpu_scheduler *sched)
  255. {
  256. if (amd_sched_ready(sched))
  257. wake_up_interruptible(&sched->wake_up_worker);
  258. }
  259. /**
  260. * Select next to run
  261. */
  262. static struct amd_sched_job *
  263. amd_sched_select_job(struct amd_gpu_scheduler *sched)
  264. {
  265. struct amd_sched_job *sched_job;
  266. if (!amd_sched_ready(sched))
  267. return NULL;
  268. /* Kernel run queue has higher priority than normal run queue*/
  269. sched_job = amd_sched_rq_select_job(&sched->kernel_rq);
  270. if (sched_job == NULL)
  271. sched_job = amd_sched_rq_select_job(&sched->sched_rq);
  272. return sched_job;
  273. }
  274. static void amd_sched_process_job(struct fence *f, struct fence_cb *cb)
  275. {
  276. struct amd_sched_fence *s_fence =
  277. container_of(cb, struct amd_sched_fence, cb);
  278. struct amd_gpu_scheduler *sched = s_fence->sched;
  279. atomic_dec(&sched->hw_rq_count);
  280. amd_sched_fence_signal(s_fence);
  281. fence_put(&s_fence->base);
  282. wake_up_interruptible(&sched->wake_up_worker);
  283. }
  284. static int amd_sched_main(void *param)
  285. {
  286. struct sched_param sparam = {.sched_priority = 1};
  287. struct amd_gpu_scheduler *sched = (struct amd_gpu_scheduler *)param;
  288. int r, count;
  289. sched_setscheduler(current, SCHED_FIFO, &sparam);
  290. while (!kthread_should_stop()) {
  291. struct amd_sched_entity *entity;
  292. struct amd_sched_fence *s_fence;
  293. struct amd_sched_job *sched_job;
  294. struct fence *fence;
  295. wait_event_interruptible(sched->wake_up_worker,
  296. kthread_should_stop() ||
  297. (sched_job = amd_sched_select_job(sched)));
  298. if (!sched_job)
  299. continue;
  300. entity = sched_job->s_entity;
  301. s_fence = sched_job->s_fence;
  302. atomic_inc(&sched->hw_rq_count);
  303. fence = sched->ops->run_job(sched_job);
  304. if (fence) {
  305. r = fence_add_callback(fence, &s_fence->cb,
  306. amd_sched_process_job);
  307. if (r == -ENOENT)
  308. amd_sched_process_job(fence, &s_fence->cb);
  309. else if (r)
  310. DRM_ERROR("fence add callback failed (%d)\n", r);
  311. fence_put(fence);
  312. } else {
  313. DRM_ERROR("Failed to run job!\n");
  314. amd_sched_process_job(NULL, &s_fence->cb);
  315. }
  316. count = kfifo_out(&entity->job_queue, &sched_job,
  317. sizeof(sched_job));
  318. WARN_ON(count != sizeof(sched_job));
  319. wake_up(&sched->job_scheduled);
  320. }
  321. return 0;
  322. }
  323. /**
  324. * Init a gpu scheduler instance
  325. *
  326. * @sched The pointer to the scheduler
  327. * @ops The backend operations for this scheduler.
  328. * @hw_submissions Number of hw submissions to do.
  329. * @name Name used for debugging
  330. *
  331. * Return 0 on success, otherwise error code.
  332. */
  333. int amd_sched_init(struct amd_gpu_scheduler *sched,
  334. struct amd_sched_backend_ops *ops,
  335. unsigned hw_submission, const char *name)
  336. {
  337. sched->ops = ops;
  338. sched->hw_submission_limit = hw_submission;
  339. sched->name = name;
  340. amd_sched_rq_init(&sched->sched_rq);
  341. amd_sched_rq_init(&sched->kernel_rq);
  342. init_waitqueue_head(&sched->wake_up_worker);
  343. init_waitqueue_head(&sched->job_scheduled);
  344. atomic_set(&sched->hw_rq_count, 0);
  345. /* Each scheduler will run on a seperate kernel thread */
  346. sched->thread = kthread_run(amd_sched_main, sched, sched->name);
  347. if (IS_ERR(sched->thread)) {
  348. DRM_ERROR("Failed to create scheduler for %s.\n", name);
  349. return PTR_ERR(sched->thread);
  350. }
  351. return 0;
  352. }
  353. /**
  354. * Destroy a gpu scheduler
  355. *
  356. * @sched The pointer to the scheduler
  357. */
  358. void amd_sched_fini(struct amd_gpu_scheduler *sched)
  359. {
  360. kthread_stop(sched->thread);
  361. }