gpu_scheduler.c 17 KB

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