gpu_scheduler.c 9.8 KB

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