gpu_scheduler.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. struct kmem_cache *sched_fence_slab;
  35. atomic_t sched_fence_slab_ref = ATOMIC_INIT(0);
  36. /* Initialize a given run queue struct */
  37. static void amd_sched_rq_init(struct amd_sched_rq *rq)
  38. {
  39. spin_lock_init(&rq->lock);
  40. INIT_LIST_HEAD(&rq->entities);
  41. rq->current_entity = NULL;
  42. }
  43. static void amd_sched_rq_add_entity(struct amd_sched_rq *rq,
  44. struct amd_sched_entity *entity)
  45. {
  46. spin_lock(&rq->lock);
  47. list_add_tail(&entity->list, &rq->entities);
  48. spin_unlock(&rq->lock);
  49. }
  50. static void amd_sched_rq_remove_entity(struct amd_sched_rq *rq,
  51. struct amd_sched_entity *entity)
  52. {
  53. spin_lock(&rq->lock);
  54. list_del_init(&entity->list);
  55. if (rq->current_entity == entity)
  56. rq->current_entity = NULL;
  57. spin_unlock(&rq->lock);
  58. }
  59. /**
  60. * Select next job from a specified run queue with round robin policy.
  61. * Return NULL if nothing available.
  62. */
  63. static struct amd_sched_job *
  64. amd_sched_rq_select_job(struct amd_sched_rq *rq)
  65. {
  66. struct amd_sched_entity *entity;
  67. struct amd_sched_job *sched_job;
  68. spin_lock(&rq->lock);
  69. entity = rq->current_entity;
  70. if (entity) {
  71. list_for_each_entry_continue(entity, &rq->entities, list) {
  72. sched_job = amd_sched_entity_pop_job(entity);
  73. if (sched_job) {
  74. rq->current_entity = entity;
  75. spin_unlock(&rq->lock);
  76. return sched_job;
  77. }
  78. }
  79. }
  80. list_for_each_entry(entity, &rq->entities, list) {
  81. sched_job = amd_sched_entity_pop_job(entity);
  82. if (sched_job) {
  83. rq->current_entity = entity;
  84. spin_unlock(&rq->lock);
  85. return sched_job;
  86. }
  87. if (entity == rq->current_entity)
  88. break;
  89. }
  90. spin_unlock(&rq->lock);
  91. return NULL;
  92. }
  93. /**
  94. * Init a context entity used by scheduler when submit to HW ring.
  95. *
  96. * @sched The pointer to the scheduler
  97. * @entity The pointer to a valid amd_sched_entity
  98. * @rq The run queue this entity belongs
  99. * @kernel If this is an entity for the kernel
  100. * @jobs The max number of jobs in the job queue
  101. *
  102. * return 0 if succeed. negative error code on failure
  103. */
  104. int amd_sched_entity_init(struct amd_gpu_scheduler *sched,
  105. struct amd_sched_entity *entity,
  106. struct amd_sched_rq *rq,
  107. uint32_t jobs)
  108. {
  109. int r;
  110. if (!(sched && entity && rq))
  111. return -EINVAL;
  112. memset(entity, 0, sizeof(struct amd_sched_entity));
  113. INIT_LIST_HEAD(&entity->list);
  114. entity->rq = rq;
  115. entity->sched = sched;
  116. spin_lock_init(&entity->queue_lock);
  117. r = kfifo_alloc(&entity->job_queue, jobs * sizeof(void *), GFP_KERNEL);
  118. if (r)
  119. return r;
  120. atomic_set(&entity->fence_seq, 0);
  121. entity->fence_context = fence_context_alloc(1);
  122. /* Add the entity to the run queue */
  123. amd_sched_rq_add_entity(rq, entity);
  124. return 0;
  125. }
  126. /**
  127. * Query if entity is initialized
  128. *
  129. * @sched Pointer to scheduler instance
  130. * @entity The pointer to a valid scheduler entity
  131. *
  132. * return true if entity is initialized, false otherwise
  133. */
  134. static bool amd_sched_entity_is_initialized(struct amd_gpu_scheduler *sched,
  135. struct amd_sched_entity *entity)
  136. {
  137. return entity->sched == sched &&
  138. entity->rq != NULL;
  139. }
  140. /**
  141. * Check if entity is idle
  142. *
  143. * @entity The pointer to a valid scheduler entity
  144. *
  145. * Return true if entity don't has any unscheduled jobs.
  146. */
  147. static bool amd_sched_entity_is_idle(struct amd_sched_entity *entity)
  148. {
  149. rmb();
  150. if (kfifo_is_empty(&entity->job_queue))
  151. return true;
  152. return false;
  153. }
  154. /**
  155. * Destroy a context entity
  156. *
  157. * @sched Pointer to scheduler instance
  158. * @entity The pointer to a valid scheduler entity
  159. *
  160. * Cleanup and free the allocated resources.
  161. */
  162. void amd_sched_entity_fini(struct amd_gpu_scheduler *sched,
  163. struct amd_sched_entity *entity)
  164. {
  165. struct amd_sched_rq *rq = entity->rq;
  166. if (!amd_sched_entity_is_initialized(sched, entity))
  167. return;
  168. /**
  169. * The client will not queue more IBs during this fini, consume existing
  170. * queued IBs
  171. */
  172. wait_event(sched->job_scheduled, amd_sched_entity_is_idle(entity));
  173. amd_sched_rq_remove_entity(rq, entity);
  174. kfifo_free(&entity->job_queue);
  175. }
  176. static void amd_sched_entity_wakeup(struct fence *f, struct fence_cb *cb)
  177. {
  178. struct amd_sched_entity *entity =
  179. container_of(cb, struct amd_sched_entity, cb);
  180. entity->dependency = NULL;
  181. fence_put(f);
  182. amd_sched_wakeup(entity->sched);
  183. }
  184. static struct amd_sched_job *
  185. amd_sched_entity_pop_job(struct amd_sched_entity *entity)
  186. {
  187. struct amd_gpu_scheduler *sched = entity->sched;
  188. struct amd_sched_job *sched_job;
  189. if (ACCESS_ONCE(entity->dependency))
  190. return NULL;
  191. if (!kfifo_out_peek(&entity->job_queue, &sched_job, sizeof(sched_job)))
  192. return NULL;
  193. while ((entity->dependency = sched->ops->dependency(sched_job))) {
  194. if (entity->dependency->context == entity->fence_context) {
  195. /* We can ignore fences from ourself */
  196. fence_put(entity->dependency);
  197. continue;
  198. }
  199. if (fence_add_callback(entity->dependency, &entity->cb,
  200. amd_sched_entity_wakeup))
  201. fence_put(entity->dependency);
  202. else
  203. return NULL;
  204. }
  205. return sched_job;
  206. }
  207. /**
  208. * Helper to submit a job to the job queue
  209. *
  210. * @sched_job The pointer to job required to submit
  211. *
  212. * Returns true if we could submit the job.
  213. */
  214. static bool amd_sched_entity_in(struct amd_sched_job *sched_job)
  215. {
  216. struct amd_sched_entity *entity = sched_job->s_entity;
  217. bool added, first = false;
  218. spin_lock(&entity->queue_lock);
  219. added = kfifo_in(&entity->job_queue, &sched_job,
  220. sizeof(sched_job)) == sizeof(sched_job);
  221. if (added && kfifo_len(&entity->job_queue) == sizeof(sched_job))
  222. first = true;
  223. spin_unlock(&entity->queue_lock);
  224. /* first job wakes up scheduler */
  225. if (first)
  226. amd_sched_wakeup(sched_job->sched);
  227. return added;
  228. }
  229. /**
  230. * Submit a job to the job queue
  231. *
  232. * @sched_job The pointer to job required to submit
  233. *
  234. * Returns 0 for success, negative error code otherwise.
  235. */
  236. void amd_sched_entity_push_job(struct amd_sched_job *sched_job)
  237. {
  238. struct amd_sched_entity *entity = sched_job->s_entity;
  239. wait_event(entity->sched->job_scheduled,
  240. amd_sched_entity_in(sched_job));
  241. trace_amd_sched_job(sched_job);
  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. unsigned long flags;
  280. atomic_dec(&sched->hw_rq_count);
  281. amd_sched_fence_signal(s_fence);
  282. if (sched->timeout != MAX_SCHEDULE_TIMEOUT) {
  283. cancel_delayed_work(&s_fence->dwork);
  284. spin_lock_irqsave(&sched->fence_list_lock, flags);
  285. list_del_init(&s_fence->list);
  286. spin_unlock_irqrestore(&sched->fence_list_lock, flags);
  287. }
  288. trace_amd_sched_process_job(s_fence);
  289. fence_put(&s_fence->base);
  290. wake_up_interruptible(&sched->wake_up_worker);
  291. }
  292. static void amd_sched_fence_work_func(struct work_struct *work)
  293. {
  294. struct amd_sched_fence *s_fence =
  295. container_of(work, struct amd_sched_fence, dwork.work);
  296. struct amd_gpu_scheduler *sched = s_fence->sched;
  297. struct amd_sched_fence *entity, *tmp;
  298. unsigned long flags;
  299. DRM_ERROR("[%s] scheduler is timeout!\n", sched->name);
  300. /* Clean all pending fences */
  301. spin_lock_irqsave(&sched->fence_list_lock, flags);
  302. list_for_each_entry_safe(entity, tmp, &sched->fence_list, list) {
  303. DRM_ERROR(" fence no %d\n", entity->base.seqno);
  304. cancel_delayed_work(&entity->dwork);
  305. list_del_init(&entity->list);
  306. fence_put(&entity->base);
  307. }
  308. spin_unlock_irqrestore(&sched->fence_list_lock, flags);
  309. }
  310. static int amd_sched_main(void *param)
  311. {
  312. struct sched_param sparam = {.sched_priority = 1};
  313. struct amd_gpu_scheduler *sched = (struct amd_gpu_scheduler *)param;
  314. int r, count;
  315. spin_lock_init(&sched->fence_list_lock);
  316. INIT_LIST_HEAD(&sched->fence_list);
  317. sched_setscheduler(current, SCHED_FIFO, &sparam);
  318. while (!kthread_should_stop()) {
  319. struct amd_sched_entity *entity;
  320. struct amd_sched_fence *s_fence;
  321. struct amd_sched_job *sched_job;
  322. struct fence *fence;
  323. unsigned long flags;
  324. wait_event_interruptible(sched->wake_up_worker,
  325. kthread_should_stop() ||
  326. (sched_job = amd_sched_select_job(sched)));
  327. if (!sched_job)
  328. continue;
  329. entity = sched_job->s_entity;
  330. s_fence = sched_job->s_fence;
  331. if (sched->timeout != MAX_SCHEDULE_TIMEOUT) {
  332. INIT_DELAYED_WORK(&s_fence->dwork, amd_sched_fence_work_func);
  333. schedule_delayed_work(&s_fence->dwork, sched->timeout);
  334. spin_lock_irqsave(&sched->fence_list_lock, flags);
  335. list_add_tail(&s_fence->list, &sched->fence_list);
  336. spin_unlock_irqrestore(&sched->fence_list_lock, flags);
  337. }
  338. atomic_inc(&sched->hw_rq_count);
  339. fence = sched->ops->run_job(sched_job);
  340. if (fence) {
  341. r = fence_add_callback(fence, &s_fence->cb,
  342. amd_sched_process_job);
  343. if (r == -ENOENT)
  344. amd_sched_process_job(fence, &s_fence->cb);
  345. else if (r)
  346. DRM_ERROR("fence add callback failed (%d)\n", r);
  347. fence_put(fence);
  348. } else {
  349. DRM_ERROR("Failed to run job!\n");
  350. amd_sched_process_job(NULL, &s_fence->cb);
  351. }
  352. count = kfifo_out(&entity->job_queue, &sched_job,
  353. sizeof(sched_job));
  354. WARN_ON(count != sizeof(sched_job));
  355. wake_up(&sched->job_scheduled);
  356. }
  357. return 0;
  358. }
  359. /**
  360. * Init a gpu scheduler instance
  361. *
  362. * @sched The pointer to the scheduler
  363. * @ops The backend operations for this scheduler.
  364. * @hw_submissions Number of hw submissions to do.
  365. * @name Name used for debugging
  366. *
  367. * Return 0 on success, otherwise error code.
  368. */
  369. int amd_sched_init(struct amd_gpu_scheduler *sched,
  370. struct amd_sched_backend_ops *ops,
  371. unsigned hw_submission, long timeout, const char *name)
  372. {
  373. sched->ops = ops;
  374. sched->hw_submission_limit = hw_submission;
  375. sched->name = name;
  376. sched->timeout = timeout;
  377. amd_sched_rq_init(&sched->sched_rq);
  378. amd_sched_rq_init(&sched->kernel_rq);
  379. init_waitqueue_head(&sched->wake_up_worker);
  380. init_waitqueue_head(&sched->job_scheduled);
  381. atomic_set(&sched->hw_rq_count, 0);
  382. if (atomic_inc_return(&sched_fence_slab_ref) == 1) {
  383. sched_fence_slab = kmem_cache_create(
  384. "amd_sched_fence", sizeof(struct amd_sched_fence), 0,
  385. SLAB_HWCACHE_ALIGN, NULL);
  386. if (!sched_fence_slab)
  387. return -ENOMEM;
  388. }
  389. /* Each scheduler will run on a seperate kernel thread */
  390. sched->thread = kthread_run(amd_sched_main, sched, sched->name);
  391. if (IS_ERR(sched->thread)) {
  392. DRM_ERROR("Failed to create scheduler for %s.\n", name);
  393. return PTR_ERR(sched->thread);
  394. }
  395. return 0;
  396. }
  397. /**
  398. * Destroy a gpu scheduler
  399. *
  400. * @sched The pointer to the scheduler
  401. */
  402. void amd_sched_fini(struct amd_gpu_scheduler *sched)
  403. {
  404. if (sched->thread)
  405. kthread_stop(sched->thread);
  406. if (atomic_dec_and_test(&sched_fence_slab_ref))
  407. kmem_cache_destroy(sched_fence_slab);
  408. }