gpu_scheduler.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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 <uapi/linux/sched/types.h>
  28. #include <drm/drmP.h>
  29. #include "gpu_scheduler.h"
  30. #include "spsc_queue.h"
  31. #define CREATE_TRACE_POINTS
  32. #include "gpu_sched_trace.h"
  33. #define to_amd_sched_job(sched_job) \
  34. container_of((sched_job), struct amd_sched_job, queue_node)
  35. static bool amd_sched_entity_is_ready(struct amd_sched_entity *entity);
  36. static void amd_sched_wakeup(struct amd_gpu_scheduler *sched);
  37. static void amd_sched_process_job(struct dma_fence *f, struct dma_fence_cb *cb);
  38. /* Initialize a given run queue struct */
  39. static void amd_sched_rq_init(struct amd_sched_rq *rq)
  40. {
  41. spin_lock_init(&rq->lock);
  42. INIT_LIST_HEAD(&rq->entities);
  43. rq->current_entity = NULL;
  44. }
  45. static void amd_sched_rq_add_entity(struct amd_sched_rq *rq,
  46. struct amd_sched_entity *entity)
  47. {
  48. if (!list_empty(&entity->list))
  49. return;
  50. spin_lock(&rq->lock);
  51. list_add_tail(&entity->list, &rq->entities);
  52. spin_unlock(&rq->lock);
  53. }
  54. static void amd_sched_rq_remove_entity(struct amd_sched_rq *rq,
  55. struct amd_sched_entity *entity)
  56. {
  57. if (list_empty(&entity->list))
  58. return;
  59. spin_lock(&rq->lock);
  60. list_del_init(&entity->list);
  61. if (rq->current_entity == entity)
  62. rq->current_entity = NULL;
  63. spin_unlock(&rq->lock);
  64. }
  65. /**
  66. * Select an entity which could provide a job to run
  67. *
  68. * @rq The run queue to check.
  69. *
  70. * Try to find a ready entity, returns NULL if none found.
  71. */
  72. static struct amd_sched_entity *
  73. amd_sched_rq_select_entity(struct amd_sched_rq *rq)
  74. {
  75. struct amd_sched_entity *entity;
  76. spin_lock(&rq->lock);
  77. entity = rq->current_entity;
  78. if (entity) {
  79. list_for_each_entry_continue(entity, &rq->entities, list) {
  80. if (amd_sched_entity_is_ready(entity)) {
  81. rq->current_entity = entity;
  82. spin_unlock(&rq->lock);
  83. return entity;
  84. }
  85. }
  86. }
  87. list_for_each_entry(entity, &rq->entities, list) {
  88. if (amd_sched_entity_is_ready(entity)) {
  89. rq->current_entity = entity;
  90. spin_unlock(&rq->lock);
  91. return entity;
  92. }
  93. if (entity == rq->current_entity)
  94. break;
  95. }
  96. spin_unlock(&rq->lock);
  97. return NULL;
  98. }
  99. /**
  100. * Init a context entity used by scheduler when submit to HW ring.
  101. *
  102. * @sched The pointer to the scheduler
  103. * @entity The pointer to a valid amd_sched_entity
  104. * @rq The run queue this entity belongs
  105. * @kernel If this is an entity for the kernel
  106. * @jobs The max number of jobs in the job queue
  107. *
  108. * return 0 if succeed. negative error code on failure
  109. */
  110. int amd_sched_entity_init(struct amd_gpu_scheduler *sched,
  111. struct amd_sched_entity *entity,
  112. struct amd_sched_rq *rq,
  113. uint32_t jobs, atomic_t *guilty)
  114. {
  115. if (!(sched && entity && rq))
  116. return -EINVAL;
  117. memset(entity, 0, sizeof(struct amd_sched_entity));
  118. INIT_LIST_HEAD(&entity->list);
  119. entity->rq = rq;
  120. entity->sched = sched;
  121. entity->guilty = guilty;
  122. spin_lock_init(&entity->rq_lock);
  123. spin_lock_init(&entity->queue_lock);
  124. spsc_queue_init(&entity->job_queue);
  125. atomic_set(&entity->fence_seq, 0);
  126. entity->fence_context = dma_fence_context_alloc(2);
  127. return 0;
  128. }
  129. /**
  130. * Query if entity is initialized
  131. *
  132. * @sched Pointer to scheduler instance
  133. * @entity The pointer to a valid scheduler entity
  134. *
  135. * return true if entity is initialized, false otherwise
  136. */
  137. static bool amd_sched_entity_is_initialized(struct amd_gpu_scheduler *sched,
  138. struct amd_sched_entity *entity)
  139. {
  140. return entity->sched == sched &&
  141. entity->rq != NULL;
  142. }
  143. /**
  144. * Check if entity is idle
  145. *
  146. * @entity The pointer to a valid scheduler entity
  147. *
  148. * Return true if entity don't has any unscheduled jobs.
  149. */
  150. static bool amd_sched_entity_is_idle(struct amd_sched_entity *entity)
  151. {
  152. rmb();
  153. if (spsc_queue_peek(&entity->job_queue) == NULL)
  154. return true;
  155. return false;
  156. }
  157. /**
  158. * Check if entity is ready
  159. *
  160. * @entity The pointer to a valid scheduler entity
  161. *
  162. * Return true if entity could provide a job.
  163. */
  164. static bool amd_sched_entity_is_ready(struct amd_sched_entity *entity)
  165. {
  166. if (spsc_queue_peek(&entity->job_queue) == NULL)
  167. return false;
  168. if (READ_ONCE(entity->dependency))
  169. return false;
  170. return true;
  171. }
  172. /**
  173. * Destroy a context entity
  174. *
  175. * @sched Pointer to scheduler instance
  176. * @entity The pointer to a valid scheduler entity
  177. *
  178. * Cleanup and free the allocated resources.
  179. */
  180. void amd_sched_entity_fini(struct amd_gpu_scheduler *sched,
  181. struct amd_sched_entity *entity)
  182. {
  183. int r;
  184. if (!amd_sched_entity_is_initialized(sched, entity))
  185. return;
  186. /**
  187. * The client will not queue more IBs during this fini, consume existing
  188. * queued IBs or discard them on SIGKILL
  189. */
  190. if ((current->flags & PF_SIGNALED) && current->exit_code == SIGKILL)
  191. r = -ERESTARTSYS;
  192. else
  193. r = wait_event_killable(sched->job_scheduled,
  194. amd_sched_entity_is_idle(entity));
  195. amd_sched_entity_set_rq(entity, NULL);
  196. if (r) {
  197. struct amd_sched_job *job;
  198. /* Park the kernel for a moment to make sure it isn't processing
  199. * our enity.
  200. */
  201. kthread_park(sched->thread);
  202. kthread_unpark(sched->thread);
  203. while ((job = to_amd_sched_job(spsc_queue_pop(&entity->job_queue)))) {
  204. struct amd_sched_fence *s_fence = job->s_fence;
  205. amd_sched_fence_scheduled(s_fence);
  206. dma_fence_set_error(&s_fence->finished, -ESRCH);
  207. amd_sched_fence_finished(s_fence);
  208. dma_fence_put(&s_fence->finished);
  209. sched->ops->free_job(job);
  210. }
  211. }
  212. }
  213. static void amd_sched_entity_wakeup(struct dma_fence *f, struct dma_fence_cb *cb)
  214. {
  215. struct amd_sched_entity *entity =
  216. container_of(cb, struct amd_sched_entity, cb);
  217. entity->dependency = NULL;
  218. dma_fence_put(f);
  219. amd_sched_wakeup(entity->sched);
  220. }
  221. static void amd_sched_entity_clear_dep(struct dma_fence *f, struct dma_fence_cb *cb)
  222. {
  223. struct amd_sched_entity *entity =
  224. container_of(cb, struct amd_sched_entity, cb);
  225. entity->dependency = NULL;
  226. dma_fence_put(f);
  227. }
  228. void amd_sched_entity_set_rq(struct amd_sched_entity *entity,
  229. struct amd_sched_rq *rq)
  230. {
  231. if (entity->rq == rq)
  232. return;
  233. spin_lock(&entity->rq_lock);
  234. if (entity->rq)
  235. amd_sched_rq_remove_entity(entity->rq, entity);
  236. entity->rq = rq;
  237. if (rq)
  238. amd_sched_rq_add_entity(rq, entity);
  239. spin_unlock(&entity->rq_lock);
  240. }
  241. bool amd_sched_dependency_optimized(struct dma_fence* fence,
  242. struct amd_sched_entity *entity)
  243. {
  244. struct amd_gpu_scheduler *sched = entity->sched;
  245. struct amd_sched_fence *s_fence;
  246. if (!fence || dma_fence_is_signaled(fence))
  247. return false;
  248. if (fence->context == entity->fence_context)
  249. return true;
  250. s_fence = to_amd_sched_fence(fence);
  251. if (s_fence && s_fence->sched == sched)
  252. return true;
  253. return false;
  254. }
  255. static bool amd_sched_entity_add_dependency_cb(struct amd_sched_entity *entity)
  256. {
  257. struct amd_gpu_scheduler *sched = entity->sched;
  258. struct dma_fence * fence = entity->dependency;
  259. struct amd_sched_fence *s_fence;
  260. if (fence->context == entity->fence_context) {
  261. /* We can ignore fences from ourself */
  262. dma_fence_put(entity->dependency);
  263. return false;
  264. }
  265. s_fence = to_amd_sched_fence(fence);
  266. if (s_fence && s_fence->sched == sched) {
  267. /*
  268. * Fence is from the same scheduler, only need to wait for
  269. * it to be scheduled
  270. */
  271. fence = dma_fence_get(&s_fence->scheduled);
  272. dma_fence_put(entity->dependency);
  273. entity->dependency = fence;
  274. if (!dma_fence_add_callback(fence, &entity->cb,
  275. amd_sched_entity_clear_dep))
  276. return true;
  277. /* Ignore it when it is already scheduled */
  278. dma_fence_put(fence);
  279. return false;
  280. }
  281. if (!dma_fence_add_callback(entity->dependency, &entity->cb,
  282. amd_sched_entity_wakeup))
  283. return true;
  284. dma_fence_put(entity->dependency);
  285. return false;
  286. }
  287. static struct amd_sched_job *
  288. amd_sched_entity_pop_job(struct amd_sched_entity *entity)
  289. {
  290. struct amd_gpu_scheduler *sched = entity->sched;
  291. struct amd_sched_job *sched_job = to_amd_sched_job(
  292. spsc_queue_peek(&entity->job_queue));
  293. if (!sched_job)
  294. return NULL;
  295. while ((entity->dependency = sched->ops->dependency(sched_job, entity)))
  296. if (amd_sched_entity_add_dependency_cb(entity))
  297. return NULL;
  298. /* skip jobs from entity that marked guilty */
  299. if (entity->guilty && atomic_read(entity->guilty))
  300. dma_fence_set_error(&sched_job->s_fence->finished, -ECANCELED);
  301. spsc_queue_pop(&entity->job_queue);
  302. return sched_job;
  303. }
  304. /**
  305. * Submit a job to the job queue
  306. *
  307. * @sched_job The pointer to job required to submit
  308. *
  309. * Returns 0 for success, negative error code otherwise.
  310. */
  311. void amd_sched_entity_push_job(struct amd_sched_job *sched_job,
  312. struct amd_sched_entity *entity)
  313. {
  314. struct amd_gpu_scheduler *sched = sched_job->sched;
  315. bool first = false;
  316. trace_amd_sched_job(sched_job, entity);
  317. spin_lock(&entity->queue_lock);
  318. first = spsc_queue_push(&entity->job_queue, &sched_job->queue_node);
  319. spin_unlock(&entity->queue_lock);
  320. /* first job wakes up scheduler */
  321. if (first) {
  322. /* Add the entity to the run queue */
  323. spin_lock(&entity->rq_lock);
  324. amd_sched_rq_add_entity(entity->rq, entity);
  325. spin_unlock(&entity->rq_lock);
  326. amd_sched_wakeup(sched);
  327. }
  328. }
  329. /* job_finish is called after hw fence signaled
  330. */
  331. static void amd_sched_job_finish(struct work_struct *work)
  332. {
  333. struct amd_sched_job *s_job = container_of(work, struct amd_sched_job,
  334. finish_work);
  335. struct amd_gpu_scheduler *sched = s_job->sched;
  336. /* remove job from ring_mirror_list */
  337. spin_lock(&sched->job_list_lock);
  338. list_del_init(&s_job->node);
  339. if (sched->timeout != MAX_SCHEDULE_TIMEOUT) {
  340. struct amd_sched_job *next;
  341. spin_unlock(&sched->job_list_lock);
  342. cancel_delayed_work_sync(&s_job->work_tdr);
  343. spin_lock(&sched->job_list_lock);
  344. /* queue TDR for next job */
  345. next = list_first_entry_or_null(&sched->ring_mirror_list,
  346. struct amd_sched_job, node);
  347. if (next)
  348. schedule_delayed_work(&next->work_tdr, sched->timeout);
  349. }
  350. spin_unlock(&sched->job_list_lock);
  351. dma_fence_put(&s_job->s_fence->finished);
  352. sched->ops->free_job(s_job);
  353. }
  354. static void amd_sched_job_finish_cb(struct dma_fence *f,
  355. struct dma_fence_cb *cb)
  356. {
  357. struct amd_sched_job *job = container_of(cb, struct amd_sched_job,
  358. finish_cb);
  359. schedule_work(&job->finish_work);
  360. }
  361. static void amd_sched_job_begin(struct amd_sched_job *s_job)
  362. {
  363. struct amd_gpu_scheduler *sched = s_job->sched;
  364. dma_fence_add_callback(&s_job->s_fence->finished, &s_job->finish_cb,
  365. amd_sched_job_finish_cb);
  366. spin_lock(&sched->job_list_lock);
  367. list_add_tail(&s_job->node, &sched->ring_mirror_list);
  368. if (sched->timeout != MAX_SCHEDULE_TIMEOUT &&
  369. list_first_entry_or_null(&sched->ring_mirror_list,
  370. struct amd_sched_job, node) == s_job)
  371. schedule_delayed_work(&s_job->work_tdr, sched->timeout);
  372. spin_unlock(&sched->job_list_lock);
  373. }
  374. static void amd_sched_job_timedout(struct work_struct *work)
  375. {
  376. struct amd_sched_job *job = container_of(work, struct amd_sched_job,
  377. work_tdr.work);
  378. job->sched->ops->timedout_job(job);
  379. }
  380. void amd_sched_hw_job_reset(struct amd_gpu_scheduler *sched, struct amd_sched_job *bad)
  381. {
  382. struct amd_sched_job *s_job;
  383. struct amd_sched_entity *entity, *tmp;
  384. int i;;
  385. spin_lock(&sched->job_list_lock);
  386. list_for_each_entry_reverse(s_job, &sched->ring_mirror_list, node) {
  387. if (s_job->s_fence->parent &&
  388. dma_fence_remove_callback(s_job->s_fence->parent,
  389. &s_job->s_fence->cb)) {
  390. dma_fence_put(s_job->s_fence->parent);
  391. s_job->s_fence->parent = NULL;
  392. atomic_dec(&sched->hw_rq_count);
  393. }
  394. }
  395. spin_unlock(&sched->job_list_lock);
  396. if (bad && bad->s_priority != AMD_SCHED_PRIORITY_KERNEL) {
  397. atomic_inc(&bad->karma);
  398. /* don't increase @bad's karma if it's from KERNEL RQ,
  399. * becuase sometimes GPU hang would cause kernel jobs (like VM updating jobs)
  400. * corrupt but keep in mind that kernel jobs always considered good.
  401. */
  402. for (i = AMD_SCHED_PRIORITY_MIN; i < AMD_SCHED_PRIORITY_KERNEL; i++ ) {
  403. struct amd_sched_rq *rq = &sched->sched_rq[i];
  404. spin_lock(&rq->lock);
  405. list_for_each_entry_safe(entity, tmp, &rq->entities, list) {
  406. if (bad->s_fence->scheduled.context == entity->fence_context) {
  407. if (atomic_read(&bad->karma) > bad->sched->hang_limit)
  408. if (entity->guilty)
  409. atomic_set(entity->guilty, 1);
  410. break;
  411. }
  412. }
  413. spin_unlock(&rq->lock);
  414. if (&entity->list != &rq->entities)
  415. break;
  416. }
  417. }
  418. }
  419. void amd_sched_job_kickout(struct amd_sched_job *s_job)
  420. {
  421. struct amd_gpu_scheduler *sched = s_job->sched;
  422. spin_lock(&sched->job_list_lock);
  423. list_del_init(&s_job->node);
  424. spin_unlock(&sched->job_list_lock);
  425. }
  426. void amd_sched_job_recovery(struct amd_gpu_scheduler *sched)
  427. {
  428. struct amd_sched_job *s_job, *tmp;
  429. bool found_guilty = false;
  430. int r;
  431. spin_lock(&sched->job_list_lock);
  432. s_job = list_first_entry_or_null(&sched->ring_mirror_list,
  433. struct amd_sched_job, node);
  434. if (s_job && sched->timeout != MAX_SCHEDULE_TIMEOUT)
  435. schedule_delayed_work(&s_job->work_tdr, sched->timeout);
  436. list_for_each_entry_safe(s_job, tmp, &sched->ring_mirror_list, node) {
  437. struct amd_sched_fence *s_fence = s_job->s_fence;
  438. struct dma_fence *fence;
  439. uint64_t guilty_context;
  440. if (!found_guilty && atomic_read(&s_job->karma) > sched->hang_limit) {
  441. found_guilty = true;
  442. guilty_context = s_job->s_fence->scheduled.context;
  443. }
  444. if (found_guilty && s_job->s_fence->scheduled.context == guilty_context)
  445. dma_fence_set_error(&s_fence->finished, -ECANCELED);
  446. spin_unlock(&sched->job_list_lock);
  447. fence = sched->ops->run_job(s_job);
  448. atomic_inc(&sched->hw_rq_count);
  449. if (fence) {
  450. s_fence->parent = dma_fence_get(fence);
  451. r = dma_fence_add_callback(fence, &s_fence->cb,
  452. amd_sched_process_job);
  453. if (r == -ENOENT)
  454. amd_sched_process_job(fence, &s_fence->cb);
  455. else if (r)
  456. DRM_ERROR("fence add callback failed (%d)\n",
  457. r);
  458. dma_fence_put(fence);
  459. } else {
  460. amd_sched_process_job(NULL, &s_fence->cb);
  461. }
  462. spin_lock(&sched->job_list_lock);
  463. }
  464. spin_unlock(&sched->job_list_lock);
  465. }
  466. /* init a sched_job with basic field */
  467. int amd_sched_job_init(struct amd_sched_job *job,
  468. struct amd_gpu_scheduler *sched,
  469. struct amd_sched_entity *entity,
  470. void *owner)
  471. {
  472. job->sched = sched;
  473. job->s_priority = entity->rq - sched->sched_rq;
  474. job->s_fence = amd_sched_fence_create(entity, owner);
  475. if (!job->s_fence)
  476. return -ENOMEM;
  477. job->id = atomic64_inc_return(&sched->job_id_count);
  478. INIT_WORK(&job->finish_work, amd_sched_job_finish);
  479. INIT_LIST_HEAD(&job->node);
  480. INIT_DELAYED_WORK(&job->work_tdr, amd_sched_job_timedout);
  481. return 0;
  482. }
  483. /**
  484. * Return ture if we can push more jobs to the hw.
  485. */
  486. static bool amd_sched_ready(struct amd_gpu_scheduler *sched)
  487. {
  488. return atomic_read(&sched->hw_rq_count) <
  489. sched->hw_submission_limit;
  490. }
  491. /**
  492. * Wake up the scheduler when it is ready
  493. */
  494. static void amd_sched_wakeup(struct amd_gpu_scheduler *sched)
  495. {
  496. if (amd_sched_ready(sched))
  497. wake_up_interruptible(&sched->wake_up_worker);
  498. }
  499. /**
  500. * Select next entity to process
  501. */
  502. static struct amd_sched_entity *
  503. amd_sched_select_entity(struct amd_gpu_scheduler *sched)
  504. {
  505. struct amd_sched_entity *entity;
  506. int i;
  507. if (!amd_sched_ready(sched))
  508. return NULL;
  509. /* Kernel run queue has higher priority than normal run queue*/
  510. for (i = AMD_SCHED_PRIORITY_MAX - 1; i >= AMD_SCHED_PRIORITY_MIN; i--) {
  511. entity = amd_sched_rq_select_entity(&sched->sched_rq[i]);
  512. if (entity)
  513. break;
  514. }
  515. return entity;
  516. }
  517. static void amd_sched_process_job(struct dma_fence *f, struct dma_fence_cb *cb)
  518. {
  519. struct amd_sched_fence *s_fence =
  520. container_of(cb, struct amd_sched_fence, cb);
  521. struct amd_gpu_scheduler *sched = s_fence->sched;
  522. dma_fence_get(&s_fence->finished);
  523. atomic_dec(&sched->hw_rq_count);
  524. amd_sched_fence_finished(s_fence);
  525. trace_amd_sched_process_job(s_fence);
  526. dma_fence_put(&s_fence->finished);
  527. wake_up_interruptible(&sched->wake_up_worker);
  528. }
  529. static bool amd_sched_blocked(struct amd_gpu_scheduler *sched)
  530. {
  531. if (kthread_should_park()) {
  532. kthread_parkme();
  533. return true;
  534. }
  535. return false;
  536. }
  537. static int amd_sched_main(void *param)
  538. {
  539. struct sched_param sparam = {.sched_priority = 1};
  540. struct amd_gpu_scheduler *sched = (struct amd_gpu_scheduler *)param;
  541. int r;
  542. sched_setscheduler(current, SCHED_FIFO, &sparam);
  543. while (!kthread_should_stop()) {
  544. struct amd_sched_entity *entity = NULL;
  545. struct amd_sched_fence *s_fence;
  546. struct amd_sched_job *sched_job;
  547. struct dma_fence *fence;
  548. wait_event_interruptible(sched->wake_up_worker,
  549. (!amd_sched_blocked(sched) &&
  550. (entity = amd_sched_select_entity(sched))) ||
  551. kthread_should_stop());
  552. if (!entity)
  553. continue;
  554. sched_job = amd_sched_entity_pop_job(entity);
  555. if (!sched_job)
  556. continue;
  557. s_fence = sched_job->s_fence;
  558. atomic_inc(&sched->hw_rq_count);
  559. amd_sched_job_begin(sched_job);
  560. fence = sched->ops->run_job(sched_job);
  561. amd_sched_fence_scheduled(s_fence);
  562. if (fence) {
  563. s_fence->parent = dma_fence_get(fence);
  564. r = dma_fence_add_callback(fence, &s_fence->cb,
  565. amd_sched_process_job);
  566. if (r == -ENOENT)
  567. amd_sched_process_job(fence, &s_fence->cb);
  568. else if (r)
  569. DRM_ERROR("fence add callback failed (%d)\n",
  570. r);
  571. dma_fence_put(fence);
  572. } else {
  573. amd_sched_process_job(NULL, &s_fence->cb);
  574. }
  575. wake_up(&sched->job_scheduled);
  576. }
  577. return 0;
  578. }
  579. /**
  580. * Init a gpu scheduler instance
  581. *
  582. * @sched The pointer to the scheduler
  583. * @ops The backend operations for this scheduler.
  584. * @hw_submissions Number of hw submissions to do.
  585. * @name Name used for debugging
  586. *
  587. * Return 0 on success, otherwise error code.
  588. */
  589. int amd_sched_init(struct amd_gpu_scheduler *sched,
  590. const struct amd_sched_backend_ops *ops,
  591. unsigned hw_submission,
  592. unsigned hang_limit,
  593. long timeout,
  594. const char *name)
  595. {
  596. int i;
  597. sched->ops = ops;
  598. sched->hw_submission_limit = hw_submission;
  599. sched->name = name;
  600. sched->timeout = timeout;
  601. sched->hang_limit = hang_limit;
  602. for (i = AMD_SCHED_PRIORITY_MIN; i < AMD_SCHED_PRIORITY_MAX; i++)
  603. amd_sched_rq_init(&sched->sched_rq[i]);
  604. init_waitqueue_head(&sched->wake_up_worker);
  605. init_waitqueue_head(&sched->job_scheduled);
  606. INIT_LIST_HEAD(&sched->ring_mirror_list);
  607. spin_lock_init(&sched->job_list_lock);
  608. atomic_set(&sched->hw_rq_count, 0);
  609. atomic64_set(&sched->job_id_count, 0);
  610. /* Each scheduler will run on a seperate kernel thread */
  611. sched->thread = kthread_run(amd_sched_main, sched, sched->name);
  612. if (IS_ERR(sched->thread)) {
  613. DRM_ERROR("Failed to create scheduler for %s.\n", name);
  614. return PTR_ERR(sched->thread);
  615. }
  616. return 0;
  617. }
  618. /**
  619. * Destroy a gpu scheduler
  620. *
  621. * @sched The pointer to the scheduler
  622. */
  623. void amd_sched_fini(struct amd_gpu_scheduler *sched)
  624. {
  625. if (sched->thread)
  626. kthread_stop(sched->thread);
  627. }