gpu_scheduler.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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 bool amd_sched_entity_is_ready(struct amd_sched_entity *entity);
  32. static void amd_sched_wakeup(struct amd_gpu_scheduler *sched);
  33. static void amd_sched_process_job(struct dma_fence *f, struct dma_fence_cb *cb);
  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. if (!list_empty(&entity->list))
  45. return;
  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. if (list_empty(&entity->list))
  54. return;
  55. spin_lock(&rq->lock);
  56. list_del_init(&entity->list);
  57. if (rq->current_entity == entity)
  58. rq->current_entity = NULL;
  59. spin_unlock(&rq->lock);
  60. }
  61. /**
  62. * Select an entity which could provide a job to run
  63. *
  64. * @rq The run queue to check.
  65. *
  66. * Try to find a ready entity, returns NULL if none found.
  67. */
  68. static struct amd_sched_entity *
  69. amd_sched_rq_select_entity(struct amd_sched_rq *rq)
  70. {
  71. struct amd_sched_entity *entity;
  72. spin_lock(&rq->lock);
  73. entity = rq->current_entity;
  74. if (entity) {
  75. list_for_each_entry_continue(entity, &rq->entities, list) {
  76. if (amd_sched_entity_is_ready(entity)) {
  77. rq->current_entity = entity;
  78. spin_unlock(&rq->lock);
  79. return entity;
  80. }
  81. }
  82. }
  83. list_for_each_entry(entity, &rq->entities, list) {
  84. if (amd_sched_entity_is_ready(entity)) {
  85. rq->current_entity = entity;
  86. spin_unlock(&rq->lock);
  87. return entity;
  88. }
  89. if (entity == rq->current_entity)
  90. break;
  91. }
  92. spin_unlock(&rq->lock);
  93. return NULL;
  94. }
  95. /**
  96. * Init a context entity used by scheduler when submit to HW ring.
  97. *
  98. * @sched The pointer to the scheduler
  99. * @entity The pointer to a valid amd_sched_entity
  100. * @rq The run queue this entity belongs
  101. * @kernel If this is an entity for the kernel
  102. * @jobs The max number of jobs in the job queue
  103. *
  104. * return 0 if succeed. negative error code on failure
  105. */
  106. int amd_sched_entity_init(struct amd_gpu_scheduler *sched,
  107. struct amd_sched_entity *entity,
  108. struct amd_sched_rq *rq,
  109. uint32_t jobs)
  110. {
  111. int r;
  112. if (!(sched && entity && rq))
  113. return -EINVAL;
  114. memset(entity, 0, sizeof(struct amd_sched_entity));
  115. INIT_LIST_HEAD(&entity->list);
  116. entity->rq = rq;
  117. entity->sched = sched;
  118. spin_lock_init(&entity->queue_lock);
  119. r = kfifo_alloc(&entity->job_queue, jobs * sizeof(void *), GFP_KERNEL);
  120. if (r)
  121. return r;
  122. atomic_set(&entity->fence_seq, 0);
  123. entity->fence_context = dma_fence_context_alloc(2);
  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. * Check if entity is ready
  156. *
  157. * @entity The pointer to a valid scheduler entity
  158. *
  159. * Return true if entity could provide a job.
  160. */
  161. static bool amd_sched_entity_is_ready(struct amd_sched_entity *entity)
  162. {
  163. if (kfifo_is_empty(&entity->job_queue))
  164. return false;
  165. if (ACCESS_ONCE(entity->dependency))
  166. return false;
  167. return true;
  168. }
  169. /**
  170. * Destroy a context entity
  171. *
  172. * @sched Pointer to scheduler instance
  173. * @entity The pointer to a valid scheduler entity
  174. *
  175. * Cleanup and free the allocated resources.
  176. */
  177. void amd_sched_entity_fini(struct amd_gpu_scheduler *sched,
  178. struct amd_sched_entity *entity)
  179. {
  180. struct amd_sched_rq *rq = entity->rq;
  181. if (!amd_sched_entity_is_initialized(sched, entity))
  182. return;
  183. /**
  184. * The client will not queue more IBs during this fini, consume existing
  185. * queued IBs
  186. */
  187. wait_event(sched->job_scheduled, amd_sched_entity_is_idle(entity));
  188. amd_sched_rq_remove_entity(rq, entity);
  189. kfifo_free(&entity->job_queue);
  190. }
  191. static void amd_sched_entity_wakeup(struct dma_fence *f, struct dma_fence_cb *cb)
  192. {
  193. struct amd_sched_entity *entity =
  194. container_of(cb, struct amd_sched_entity, cb);
  195. entity->dependency = NULL;
  196. dma_fence_put(f);
  197. amd_sched_wakeup(entity->sched);
  198. }
  199. static void amd_sched_entity_clear_dep(struct dma_fence *f, struct dma_fence_cb *cb)
  200. {
  201. struct amd_sched_entity *entity =
  202. container_of(cb, struct amd_sched_entity, cb);
  203. entity->dependency = NULL;
  204. dma_fence_put(f);
  205. }
  206. static bool amd_sched_entity_add_dependency_cb(struct amd_sched_entity *entity)
  207. {
  208. struct amd_gpu_scheduler *sched = entity->sched;
  209. struct dma_fence * fence = entity->dependency;
  210. struct amd_sched_fence *s_fence;
  211. if (fence->context == entity->fence_context) {
  212. /* We can ignore fences from ourself */
  213. dma_fence_put(entity->dependency);
  214. return false;
  215. }
  216. s_fence = to_amd_sched_fence(fence);
  217. if (s_fence && s_fence->sched == sched) {
  218. /*
  219. * Fence is from the same scheduler, only need to wait for
  220. * it to be scheduled
  221. */
  222. fence = dma_fence_get(&s_fence->scheduled);
  223. dma_fence_put(entity->dependency);
  224. entity->dependency = fence;
  225. if (!dma_fence_add_callback(fence, &entity->cb,
  226. amd_sched_entity_clear_dep))
  227. return true;
  228. /* Ignore it when it is already scheduled */
  229. dma_fence_put(fence);
  230. return false;
  231. }
  232. if (!dma_fence_add_callback(entity->dependency, &entity->cb,
  233. amd_sched_entity_wakeup))
  234. return true;
  235. dma_fence_put(entity->dependency);
  236. return false;
  237. }
  238. static struct amd_sched_job *
  239. amd_sched_entity_pop_job(struct amd_sched_entity *entity)
  240. {
  241. struct amd_gpu_scheduler *sched = entity->sched;
  242. struct amd_sched_job *sched_job;
  243. if (!kfifo_out_peek(&entity->job_queue, &sched_job, sizeof(sched_job)))
  244. return NULL;
  245. while ((entity->dependency = sched->ops->dependency(sched_job)))
  246. if (amd_sched_entity_add_dependency_cb(entity))
  247. return NULL;
  248. return sched_job;
  249. }
  250. /**
  251. * Helper to submit a job to the job queue
  252. *
  253. * @sched_job The pointer to job required to submit
  254. *
  255. * Returns true if we could submit the job.
  256. */
  257. static bool amd_sched_entity_in(struct amd_sched_job *sched_job)
  258. {
  259. struct amd_gpu_scheduler *sched = sched_job->sched;
  260. struct amd_sched_entity *entity = sched_job->s_entity;
  261. bool added, first = false;
  262. spin_lock(&entity->queue_lock);
  263. added = kfifo_in(&entity->job_queue, &sched_job,
  264. sizeof(sched_job)) == sizeof(sched_job);
  265. if (added && kfifo_len(&entity->job_queue) == sizeof(sched_job))
  266. first = true;
  267. spin_unlock(&entity->queue_lock);
  268. /* first job wakes up scheduler */
  269. if (first) {
  270. /* Add the entity to the run queue */
  271. amd_sched_rq_add_entity(entity->rq, entity);
  272. amd_sched_wakeup(sched);
  273. }
  274. return added;
  275. }
  276. /* job_finish is called after hw fence signaled, and
  277. * the job had already been deleted from ring_mirror_list
  278. */
  279. static void amd_sched_job_finish(struct work_struct *work)
  280. {
  281. struct amd_sched_job *s_job = container_of(work, struct amd_sched_job,
  282. finish_work);
  283. struct amd_gpu_scheduler *sched = s_job->sched;
  284. /* remove job from ring_mirror_list */
  285. spin_lock(&sched->job_list_lock);
  286. list_del_init(&s_job->node);
  287. if (sched->timeout != MAX_SCHEDULE_TIMEOUT) {
  288. struct amd_sched_job *next;
  289. spin_unlock(&sched->job_list_lock);
  290. cancel_delayed_work_sync(&s_job->work_tdr);
  291. spin_lock(&sched->job_list_lock);
  292. /* queue TDR for next job */
  293. next = list_first_entry_or_null(&sched->ring_mirror_list,
  294. struct amd_sched_job, node);
  295. if (next)
  296. schedule_delayed_work(&next->work_tdr, sched->timeout);
  297. }
  298. spin_unlock(&sched->job_list_lock);
  299. sched->ops->free_job(s_job);
  300. }
  301. static void amd_sched_job_finish_cb(struct dma_fence *f,
  302. struct dma_fence_cb *cb)
  303. {
  304. struct amd_sched_job *job = container_of(cb, struct amd_sched_job,
  305. finish_cb);
  306. schedule_work(&job->finish_work);
  307. }
  308. static void amd_sched_job_begin(struct amd_sched_job *s_job)
  309. {
  310. struct amd_gpu_scheduler *sched = s_job->sched;
  311. spin_lock(&sched->job_list_lock);
  312. list_add_tail(&s_job->node, &sched->ring_mirror_list);
  313. if (sched->timeout != MAX_SCHEDULE_TIMEOUT &&
  314. list_first_entry_or_null(&sched->ring_mirror_list,
  315. struct amd_sched_job, node) == s_job)
  316. schedule_delayed_work(&s_job->work_tdr, sched->timeout);
  317. spin_unlock(&sched->job_list_lock);
  318. }
  319. static void amd_sched_job_timedout(struct work_struct *work)
  320. {
  321. struct amd_sched_job *job = container_of(work, struct amd_sched_job,
  322. work_tdr.work);
  323. job->sched->ops->timedout_job(job);
  324. }
  325. void amd_sched_hw_job_reset(struct amd_gpu_scheduler *sched)
  326. {
  327. struct amd_sched_job *s_job;
  328. spin_lock(&sched->job_list_lock);
  329. list_for_each_entry_reverse(s_job, &sched->ring_mirror_list, node) {
  330. if (dma_fence_remove_callback(s_job->s_fence->parent, &s_job->s_fence->cb)) {
  331. dma_fence_put(s_job->s_fence->parent);
  332. s_job->s_fence->parent = NULL;
  333. }
  334. }
  335. atomic_set(&sched->hw_rq_count, 0);
  336. spin_unlock(&sched->job_list_lock);
  337. }
  338. void amd_sched_job_recovery(struct amd_gpu_scheduler *sched)
  339. {
  340. struct amd_sched_job *s_job, *tmp;
  341. int r;
  342. spin_lock(&sched->job_list_lock);
  343. s_job = list_first_entry_or_null(&sched->ring_mirror_list,
  344. struct amd_sched_job, node);
  345. if (s_job && sched->timeout != MAX_SCHEDULE_TIMEOUT)
  346. schedule_delayed_work(&s_job->work_tdr, sched->timeout);
  347. list_for_each_entry_safe(s_job, tmp, &sched->ring_mirror_list, node) {
  348. struct amd_sched_fence *s_fence = s_job->s_fence;
  349. struct dma_fence *fence;
  350. spin_unlock(&sched->job_list_lock);
  351. fence = sched->ops->run_job(s_job);
  352. atomic_inc(&sched->hw_rq_count);
  353. if (fence) {
  354. s_fence->parent = dma_fence_get(fence);
  355. r = dma_fence_add_callback(fence, &s_fence->cb,
  356. amd_sched_process_job);
  357. if (r == -ENOENT)
  358. amd_sched_process_job(fence, &s_fence->cb);
  359. else if (r)
  360. DRM_ERROR("fence add callback failed (%d)\n",
  361. r);
  362. dma_fence_put(fence);
  363. } else {
  364. DRM_ERROR("Failed to run job!\n");
  365. amd_sched_process_job(NULL, &s_fence->cb);
  366. }
  367. spin_lock(&sched->job_list_lock);
  368. }
  369. spin_unlock(&sched->job_list_lock);
  370. }
  371. /**
  372. * Submit a job to the job queue
  373. *
  374. * @sched_job The pointer to job required to submit
  375. *
  376. * Returns 0 for success, negative error code otherwise.
  377. */
  378. void amd_sched_entity_push_job(struct amd_sched_job *sched_job)
  379. {
  380. struct amd_sched_entity *entity = sched_job->s_entity;
  381. trace_amd_sched_job(sched_job);
  382. dma_fence_add_callback(&sched_job->s_fence->finished, &sched_job->finish_cb,
  383. amd_sched_job_finish_cb);
  384. wait_event(entity->sched->job_scheduled,
  385. amd_sched_entity_in(sched_job));
  386. }
  387. /* init a sched_job with basic field */
  388. int amd_sched_job_init(struct amd_sched_job *job,
  389. struct amd_gpu_scheduler *sched,
  390. struct amd_sched_entity *entity,
  391. void *owner)
  392. {
  393. job->sched = sched;
  394. job->s_entity = entity;
  395. job->s_fence = amd_sched_fence_create(entity, owner);
  396. if (!job->s_fence)
  397. return -ENOMEM;
  398. INIT_WORK(&job->finish_work, amd_sched_job_finish);
  399. INIT_LIST_HEAD(&job->node);
  400. INIT_DELAYED_WORK(&job->work_tdr, amd_sched_job_timedout);
  401. return 0;
  402. }
  403. /**
  404. * Return ture if we can push more jobs to the hw.
  405. */
  406. static bool amd_sched_ready(struct amd_gpu_scheduler *sched)
  407. {
  408. return atomic_read(&sched->hw_rq_count) <
  409. sched->hw_submission_limit;
  410. }
  411. /**
  412. * Wake up the scheduler when it is ready
  413. */
  414. static void amd_sched_wakeup(struct amd_gpu_scheduler *sched)
  415. {
  416. if (amd_sched_ready(sched))
  417. wake_up_interruptible(&sched->wake_up_worker);
  418. }
  419. /**
  420. * Select next entity to process
  421. */
  422. static struct amd_sched_entity *
  423. amd_sched_select_entity(struct amd_gpu_scheduler *sched)
  424. {
  425. struct amd_sched_entity *entity;
  426. int i;
  427. if (!amd_sched_ready(sched))
  428. return NULL;
  429. /* Kernel run queue has higher priority than normal run queue*/
  430. for (i = 0; i < AMD_SCHED_MAX_PRIORITY; i++) {
  431. entity = amd_sched_rq_select_entity(&sched->sched_rq[i]);
  432. if (entity)
  433. break;
  434. }
  435. return entity;
  436. }
  437. static void amd_sched_process_job(struct dma_fence *f, struct dma_fence_cb *cb)
  438. {
  439. struct amd_sched_fence *s_fence =
  440. container_of(cb, struct amd_sched_fence, cb);
  441. struct amd_gpu_scheduler *sched = s_fence->sched;
  442. atomic_dec(&sched->hw_rq_count);
  443. amd_sched_fence_finished(s_fence);
  444. trace_amd_sched_process_job(s_fence);
  445. dma_fence_put(&s_fence->finished);
  446. wake_up_interruptible(&sched->wake_up_worker);
  447. }
  448. static bool amd_sched_blocked(struct amd_gpu_scheduler *sched)
  449. {
  450. if (kthread_should_park()) {
  451. kthread_parkme();
  452. return true;
  453. }
  454. return false;
  455. }
  456. static int amd_sched_main(void *param)
  457. {
  458. struct sched_param sparam = {.sched_priority = 1};
  459. struct amd_gpu_scheduler *sched = (struct amd_gpu_scheduler *)param;
  460. int r, count;
  461. sched_setscheduler(current, SCHED_FIFO, &sparam);
  462. while (!kthread_should_stop()) {
  463. struct amd_sched_entity *entity = NULL;
  464. struct amd_sched_fence *s_fence;
  465. struct amd_sched_job *sched_job;
  466. struct dma_fence *fence;
  467. wait_event_interruptible(sched->wake_up_worker,
  468. (!amd_sched_blocked(sched) &&
  469. (entity = amd_sched_select_entity(sched))) ||
  470. kthread_should_stop());
  471. if (!entity)
  472. continue;
  473. sched_job = amd_sched_entity_pop_job(entity);
  474. if (!sched_job)
  475. continue;
  476. s_fence = sched_job->s_fence;
  477. atomic_inc(&sched->hw_rq_count);
  478. amd_sched_job_begin(sched_job);
  479. fence = sched->ops->run_job(sched_job);
  480. amd_sched_fence_scheduled(s_fence);
  481. if (fence) {
  482. s_fence->parent = dma_fence_get(fence);
  483. r = dma_fence_add_callback(fence, &s_fence->cb,
  484. amd_sched_process_job);
  485. if (r == -ENOENT)
  486. amd_sched_process_job(fence, &s_fence->cb);
  487. else if (r)
  488. DRM_ERROR("fence add callback failed (%d)\n",
  489. r);
  490. dma_fence_put(fence);
  491. } else {
  492. DRM_ERROR("Failed to run job!\n");
  493. amd_sched_process_job(NULL, &s_fence->cb);
  494. }
  495. count = kfifo_out(&entity->job_queue, &sched_job,
  496. sizeof(sched_job));
  497. WARN_ON(count != sizeof(sched_job));
  498. wake_up(&sched->job_scheduled);
  499. }
  500. return 0;
  501. }
  502. /**
  503. * Init a gpu scheduler instance
  504. *
  505. * @sched The pointer to the scheduler
  506. * @ops The backend operations for this scheduler.
  507. * @hw_submissions Number of hw submissions to do.
  508. * @name Name used for debugging
  509. *
  510. * Return 0 on success, otherwise error code.
  511. */
  512. int amd_sched_init(struct amd_gpu_scheduler *sched,
  513. const struct amd_sched_backend_ops *ops,
  514. unsigned hw_submission, long timeout, const char *name)
  515. {
  516. int i;
  517. sched->ops = ops;
  518. sched->hw_submission_limit = hw_submission;
  519. sched->name = name;
  520. sched->timeout = timeout;
  521. for (i = 0; i < AMD_SCHED_MAX_PRIORITY; i++)
  522. amd_sched_rq_init(&sched->sched_rq[i]);
  523. init_waitqueue_head(&sched->wake_up_worker);
  524. init_waitqueue_head(&sched->job_scheduled);
  525. INIT_LIST_HEAD(&sched->ring_mirror_list);
  526. spin_lock_init(&sched->job_list_lock);
  527. atomic_set(&sched->hw_rq_count, 0);
  528. /* Each scheduler will run on a seperate kernel thread */
  529. sched->thread = kthread_run(amd_sched_main, sched, sched->name);
  530. if (IS_ERR(sched->thread)) {
  531. DRM_ERROR("Failed to create scheduler for %s.\n", name);
  532. return PTR_ERR(sched->thread);
  533. }
  534. return 0;
  535. }
  536. /**
  537. * Destroy a gpu scheduler
  538. *
  539. * @sched The pointer to the scheduler
  540. */
  541. void amd_sched_fini(struct amd_gpu_scheduler *sched)
  542. {
  543. if (sched->thread)
  544. kthread_stop(sched->thread);
  545. }