gpu_scheduler.c 11 KB

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