gpu_scheduler.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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 (entity->dependency->context == entity->fence_context) {
  193. /* We can ignore fences from ourself */
  194. fence_put(entity->dependency);
  195. continue;
  196. }
  197. if (fence_add_callback(entity->dependency, &entity->cb,
  198. amd_sched_entity_wakeup))
  199. fence_put(entity->dependency);
  200. else
  201. return NULL;
  202. }
  203. return sched_job;
  204. }
  205. /**
  206. * Helper to submit a job to the job queue
  207. *
  208. * @sched_job The pointer to job required to submit
  209. *
  210. * Returns true if we could submit the job.
  211. */
  212. static bool amd_sched_entity_in(struct amd_sched_job *sched_job)
  213. {
  214. struct amd_sched_entity *entity = sched_job->s_entity;
  215. bool added, first = false;
  216. spin_lock(&entity->queue_lock);
  217. added = kfifo_in(&entity->job_queue, &sched_job,
  218. sizeof(sched_job)) == sizeof(sched_job);
  219. if (added && kfifo_len(&entity->job_queue) == sizeof(sched_job))
  220. first = true;
  221. spin_unlock(&entity->queue_lock);
  222. /* first job wakes up scheduler */
  223. if (first)
  224. amd_sched_wakeup(sched_job->sched);
  225. return added;
  226. }
  227. /**
  228. * Submit a job to the job queue
  229. *
  230. * @sched_job The pointer to job required to submit
  231. *
  232. * Returns 0 for success, negative error code otherwise.
  233. */
  234. int amd_sched_entity_push_job(struct amd_sched_job *sched_job)
  235. {
  236. struct amd_sched_entity *entity = sched_job->s_entity;
  237. struct amd_sched_fence *fence = amd_sched_fence_create(
  238. entity, sched_job->owner);
  239. if (!fence)
  240. return -ENOMEM;
  241. fence_get(&fence->base);
  242. sched_job->s_fence = fence;
  243. wait_event(entity->sched->job_scheduled,
  244. amd_sched_entity_in(sched_job));
  245. trace_amd_sched_job(sched_job);
  246. return 0;
  247. }
  248. /**
  249. * Return ture if we can push more jobs to the hw.
  250. */
  251. static bool amd_sched_ready(struct amd_gpu_scheduler *sched)
  252. {
  253. return atomic_read(&sched->hw_rq_count) <
  254. sched->hw_submission_limit;
  255. }
  256. /**
  257. * Wake up the scheduler when it is ready
  258. */
  259. static void amd_sched_wakeup(struct amd_gpu_scheduler *sched)
  260. {
  261. if (amd_sched_ready(sched))
  262. wake_up_interruptible(&sched->wake_up_worker);
  263. }
  264. /**
  265. * Select next to run
  266. */
  267. static struct amd_sched_job *
  268. amd_sched_select_job(struct amd_gpu_scheduler *sched)
  269. {
  270. struct amd_sched_job *sched_job;
  271. if (!amd_sched_ready(sched))
  272. return NULL;
  273. /* Kernel run queue has higher priority than normal run queue*/
  274. sched_job = amd_sched_rq_select_job(&sched->kernel_rq);
  275. if (sched_job == NULL)
  276. sched_job = amd_sched_rq_select_job(&sched->sched_rq);
  277. return sched_job;
  278. }
  279. static void amd_sched_process_job(struct fence *f, struct fence_cb *cb)
  280. {
  281. struct amd_sched_fence *s_fence =
  282. container_of(cb, struct amd_sched_fence, cb);
  283. struct amd_gpu_scheduler *sched = s_fence->sched;
  284. unsigned long flags;
  285. atomic_dec(&sched->hw_rq_count);
  286. amd_sched_fence_signal(s_fence);
  287. if (sched->timeout != MAX_SCHEDULE_TIMEOUT) {
  288. cancel_delayed_work_sync(&s_fence->dwork);
  289. spin_lock_irqsave(&sched->fence_list_lock, flags);
  290. list_del_init(&s_fence->list);
  291. spin_unlock_irqrestore(&sched->fence_list_lock, flags);
  292. }
  293. fence_put(&s_fence->base);
  294. wake_up_interruptible(&sched->wake_up_worker);
  295. }
  296. static void amd_sched_fence_work_func(struct work_struct *work)
  297. {
  298. struct amd_sched_fence *s_fence =
  299. container_of(work, struct amd_sched_fence, dwork.work);
  300. struct amd_gpu_scheduler *sched = s_fence->sched;
  301. struct amd_sched_fence *entity, *tmp;
  302. unsigned long flags;
  303. DRM_ERROR("[%s] scheduler is timeout!\n", sched->name);
  304. /* Clean all pending fences */
  305. spin_lock_irqsave(&sched->fence_list_lock, flags);
  306. list_for_each_entry_safe(entity, tmp, &sched->fence_list, list) {
  307. DRM_ERROR(" fence no %d\n", entity->base.seqno);
  308. cancel_delayed_work(&entity->dwork);
  309. list_del_init(&entity->list);
  310. fence_put(&entity->base);
  311. }
  312. spin_unlock_irqrestore(&sched->fence_list_lock, flags);
  313. }
  314. static int amd_sched_main(void *param)
  315. {
  316. struct sched_param sparam = {.sched_priority = 1};
  317. struct amd_gpu_scheduler *sched = (struct amd_gpu_scheduler *)param;
  318. int r, count;
  319. spin_lock_init(&sched->fence_list_lock);
  320. INIT_LIST_HEAD(&sched->fence_list);
  321. sched_setscheduler(current, SCHED_FIFO, &sparam);
  322. while (!kthread_should_stop()) {
  323. struct amd_sched_entity *entity;
  324. struct amd_sched_fence *s_fence;
  325. struct amd_sched_job *sched_job;
  326. struct fence *fence;
  327. unsigned long flags;
  328. wait_event_interruptible(sched->wake_up_worker,
  329. kthread_should_stop() ||
  330. (sched_job = amd_sched_select_job(sched)));
  331. if (!sched_job)
  332. continue;
  333. entity = sched_job->s_entity;
  334. s_fence = sched_job->s_fence;
  335. if (sched->timeout != MAX_SCHEDULE_TIMEOUT) {
  336. INIT_DELAYED_WORK(&s_fence->dwork, amd_sched_fence_work_func);
  337. schedule_delayed_work(&s_fence->dwork, sched->timeout);
  338. spin_lock_irqsave(&sched->fence_list_lock, flags);
  339. list_add_tail(&s_fence->list, &sched->fence_list);
  340. spin_unlock_irqrestore(&sched->fence_list_lock, flags);
  341. }
  342. atomic_inc(&sched->hw_rq_count);
  343. fence = sched->ops->run_job(sched_job);
  344. if (fence) {
  345. r = fence_add_callback(fence, &s_fence->cb,
  346. amd_sched_process_job);
  347. if (r == -ENOENT)
  348. amd_sched_process_job(fence, &s_fence->cb);
  349. else if (r)
  350. DRM_ERROR("fence add callback failed (%d)\n", r);
  351. fence_put(fence);
  352. } else {
  353. DRM_ERROR("Failed to run job!\n");
  354. amd_sched_process_job(NULL, &s_fence->cb);
  355. }
  356. count = kfifo_out(&entity->job_queue, &sched_job,
  357. sizeof(sched_job));
  358. WARN_ON(count != sizeof(sched_job));
  359. wake_up(&sched->job_scheduled);
  360. }
  361. return 0;
  362. }
  363. /**
  364. * Init a gpu scheduler instance
  365. *
  366. * @sched The pointer to the scheduler
  367. * @ops The backend operations for this scheduler.
  368. * @hw_submissions Number of hw submissions to do.
  369. * @name Name used for debugging
  370. *
  371. * Return 0 on success, otherwise error code.
  372. */
  373. int amd_sched_init(struct amd_gpu_scheduler *sched,
  374. struct amd_sched_backend_ops *ops,
  375. unsigned hw_submission, long timeout, const char *name)
  376. {
  377. sched->ops = ops;
  378. sched->hw_submission_limit = hw_submission;
  379. sched->name = name;
  380. sched->timeout = timeout;
  381. amd_sched_rq_init(&sched->sched_rq);
  382. amd_sched_rq_init(&sched->kernel_rq);
  383. init_waitqueue_head(&sched->wake_up_worker);
  384. init_waitqueue_head(&sched->job_scheduled);
  385. atomic_set(&sched->hw_rq_count, 0);
  386. /* Each scheduler will run on a seperate kernel thread */
  387. sched->thread = kthread_run(amd_sched_main, sched, sched->name);
  388. if (IS_ERR(sched->thread)) {
  389. DRM_ERROR("Failed to create scheduler for %s.\n", name);
  390. return PTR_ERR(sched->thread);
  391. }
  392. return 0;
  393. }
  394. /**
  395. * Destroy a gpu scheduler
  396. *
  397. * @sched The pointer to the scheduler
  398. */
  399. void amd_sched_fini(struct amd_gpu_scheduler *sched)
  400. {
  401. kthread_stop(sched->thread);
  402. }