gpu_scheduler.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  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. * DOC: Overview
  25. *
  26. * The GPU scheduler provides entities which allow userspace to push jobs
  27. * into software queues which are then scheduled on a hardware run queue.
  28. * The software queues have a priority among them. The scheduler selects the entities
  29. * from the run queue using a FIFO. The scheduler provides dependency handling
  30. * features among jobs. The driver is supposed to provide callback functions for
  31. * backend operations to the scheduler like submitting a job to hardware run queue,
  32. * returning the dependencies of a job etc.
  33. *
  34. * The organisation of the scheduler is the following:
  35. *
  36. * 1. Each hw run queue has one scheduler
  37. * 2. Each scheduler has multiple run queues with different priorities
  38. * (e.g., HIGH_HW,HIGH_SW, KERNEL, NORMAL)
  39. * 3. Each scheduler run queue has a queue of entities to schedule
  40. * 4. Entities themselves maintain a queue of jobs that will be scheduled on
  41. * the hardware.
  42. *
  43. * The jobs in a entity are always scheduled in the order that they were pushed.
  44. */
  45. #include <linux/kthread.h>
  46. #include <linux/wait.h>
  47. #include <linux/sched.h>
  48. #include <uapi/linux/sched/types.h>
  49. #include <drm/drmP.h>
  50. #include <drm/gpu_scheduler.h>
  51. #include <drm/spsc_queue.h>
  52. #define CREATE_TRACE_POINTS
  53. #include "gpu_scheduler_trace.h"
  54. #define to_drm_sched_job(sched_job) \
  55. container_of((sched_job), struct drm_sched_job, queue_node)
  56. static bool drm_sched_entity_is_ready(struct drm_sched_entity *entity);
  57. static void drm_sched_wakeup(struct drm_gpu_scheduler *sched);
  58. static void drm_sched_process_job(struct dma_fence *f, struct dma_fence_cb *cb);
  59. /**
  60. * drm_sched_rq_init - initialize a given run queue struct
  61. *
  62. * @rq: scheduler run queue
  63. *
  64. * Initializes a scheduler runqueue.
  65. */
  66. static void drm_sched_rq_init(struct drm_sched_rq *rq)
  67. {
  68. spin_lock_init(&rq->lock);
  69. INIT_LIST_HEAD(&rq->entities);
  70. rq->current_entity = NULL;
  71. }
  72. /**
  73. * drm_sched_rq_add_entity - add an entity
  74. *
  75. * @rq: scheduler run queue
  76. * @entity: scheduler entity
  77. *
  78. * Adds a scheduler entity to the run queue.
  79. */
  80. static void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
  81. struct drm_sched_entity *entity)
  82. {
  83. if (!list_empty(&entity->list))
  84. return;
  85. spin_lock(&rq->lock);
  86. list_add_tail(&entity->list, &rq->entities);
  87. spin_unlock(&rq->lock);
  88. }
  89. /**
  90. * drm_sched_rq_remove_entity - remove an entity
  91. *
  92. * @rq: scheduler run queue
  93. * @entity: scheduler entity
  94. *
  95. * Removes a scheduler entity from the run queue.
  96. */
  97. static void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
  98. struct drm_sched_entity *entity)
  99. {
  100. if (list_empty(&entity->list))
  101. return;
  102. spin_lock(&rq->lock);
  103. list_del_init(&entity->list);
  104. if (rq->current_entity == entity)
  105. rq->current_entity = NULL;
  106. spin_unlock(&rq->lock);
  107. }
  108. /**
  109. * drm_sched_rq_select_entity - Select an entity which could provide a job to run
  110. *
  111. * @rq: scheduler run queue to check.
  112. *
  113. * Try to find a ready entity, returns NULL if none found.
  114. */
  115. static struct drm_sched_entity *
  116. drm_sched_rq_select_entity(struct drm_sched_rq *rq)
  117. {
  118. struct drm_sched_entity *entity;
  119. spin_lock(&rq->lock);
  120. entity = rq->current_entity;
  121. if (entity) {
  122. list_for_each_entry_continue(entity, &rq->entities, list) {
  123. if (drm_sched_entity_is_ready(entity)) {
  124. rq->current_entity = entity;
  125. spin_unlock(&rq->lock);
  126. return entity;
  127. }
  128. }
  129. }
  130. list_for_each_entry(entity, &rq->entities, list) {
  131. if (drm_sched_entity_is_ready(entity)) {
  132. rq->current_entity = entity;
  133. spin_unlock(&rq->lock);
  134. return entity;
  135. }
  136. if (entity == rq->current_entity)
  137. break;
  138. }
  139. spin_unlock(&rq->lock);
  140. return NULL;
  141. }
  142. /**
  143. * drm_sched_entity_init - Init a context entity used by scheduler when
  144. * submit to HW ring.
  145. *
  146. * @sched: scheduler instance
  147. * @entity: scheduler entity to init
  148. * @rq: the run queue this entity belongs
  149. * @guilty: atomic_t set to 1 when a job on this queue
  150. * is found to be guilty causing a timeout
  151. *
  152. * Returns 0 on success or a negative error code on failure.
  153. */
  154. int drm_sched_entity_init(struct drm_gpu_scheduler *sched,
  155. struct drm_sched_entity *entity,
  156. struct drm_sched_rq *rq,
  157. atomic_t *guilty)
  158. {
  159. if (!(sched && entity && rq))
  160. return -EINVAL;
  161. memset(entity, 0, sizeof(struct drm_sched_entity));
  162. INIT_LIST_HEAD(&entity->list);
  163. entity->rq = rq;
  164. entity->sched = sched;
  165. entity->guilty = guilty;
  166. entity->last_scheduled = NULL;
  167. spin_lock_init(&entity->rq_lock);
  168. spsc_queue_init(&entity->job_queue);
  169. atomic_set(&entity->fence_seq, 0);
  170. entity->fence_context = dma_fence_context_alloc(2);
  171. return 0;
  172. }
  173. EXPORT_SYMBOL(drm_sched_entity_init);
  174. /**
  175. * drm_sched_entity_is_initialized - Query if entity is initialized
  176. *
  177. * @sched: Pointer to scheduler instance
  178. * @entity: The pointer to a valid scheduler entity
  179. *
  180. * return true if entity is initialized, false otherwise
  181. */
  182. static bool drm_sched_entity_is_initialized(struct drm_gpu_scheduler *sched,
  183. struct drm_sched_entity *entity)
  184. {
  185. return entity->sched == sched &&
  186. entity->rq != NULL;
  187. }
  188. /**
  189. * drm_sched_entity_is_idle - Check if entity is idle
  190. *
  191. * @entity: scheduler entity
  192. *
  193. * Returns true if the entity does not have any unscheduled jobs.
  194. */
  195. static bool drm_sched_entity_is_idle(struct drm_sched_entity *entity)
  196. {
  197. rmb();
  198. if (!entity->rq || spsc_queue_peek(&entity->job_queue) == NULL)
  199. return true;
  200. return false;
  201. }
  202. /**
  203. * drm_sched_entity_is_ready - Check if entity is ready
  204. *
  205. * @entity: scheduler entity
  206. *
  207. * Return true if entity could provide a job.
  208. */
  209. static bool drm_sched_entity_is_ready(struct drm_sched_entity *entity)
  210. {
  211. if (spsc_queue_peek(&entity->job_queue) == NULL)
  212. return false;
  213. if (READ_ONCE(entity->dependency))
  214. return false;
  215. return true;
  216. }
  217. static void drm_sched_entity_kill_jobs_cb(struct dma_fence *f,
  218. struct dma_fence_cb *cb)
  219. {
  220. struct drm_sched_job *job = container_of(cb, struct drm_sched_job,
  221. finish_cb);
  222. drm_sched_fence_finished(job->s_fence);
  223. WARN_ON(job->s_fence->parent);
  224. dma_fence_put(&job->s_fence->finished);
  225. job->sched->ops->free_job(job);
  226. }
  227. /**
  228. * drm_sched_entity_do_release - Destroy a context entity
  229. *
  230. * @sched: scheduler instance
  231. * @entity: scheduler entity
  232. * @timeout: time to wait in for Q to become empty in jiffies.
  233. *
  234. * Splitting drm_sched_entity_fini() into two functions, The first one does the waiting,
  235. * removes the entity from the runqueue and returns an error when the process was killed.
  236. *
  237. * Returns the remaining time in jiffies left from the input timeout
  238. */
  239. long drm_sched_entity_do_release(struct drm_gpu_scheduler *sched,
  240. struct drm_sched_entity *entity, long timeout)
  241. {
  242. long ret = timeout;
  243. if (!drm_sched_entity_is_initialized(sched, entity))
  244. return ret;
  245. /**
  246. * The client will not queue more IBs during this fini, consume existing
  247. * queued IBs or discard them on SIGKILL
  248. */
  249. if (current->flags & PF_EXITING) {
  250. if (timeout)
  251. ret = wait_event_timeout(
  252. sched->job_scheduled,
  253. drm_sched_entity_is_idle(entity),
  254. timeout);
  255. } else
  256. wait_event_killable(sched->job_scheduled, drm_sched_entity_is_idle(entity));
  257. /* For killed process disable any more IBs enqueue right now */
  258. if ((current->flags & PF_EXITING) && (current->exit_code == SIGKILL))
  259. drm_sched_entity_set_rq(entity, NULL);
  260. return ret;
  261. }
  262. EXPORT_SYMBOL(drm_sched_entity_do_release);
  263. /**
  264. * drm_sched_entity_cleanup - Destroy a context entity
  265. *
  266. * @sched: scheduler instance
  267. * @entity: scheduler entity
  268. *
  269. * This should be called after @drm_sched_entity_do_release. It goes over the
  270. * entity and signals all jobs with an error code if the process was killed.
  271. *
  272. */
  273. void drm_sched_entity_cleanup(struct drm_gpu_scheduler *sched,
  274. struct drm_sched_entity *entity)
  275. {
  276. drm_sched_entity_set_rq(entity, NULL);
  277. /* Consumption of existing IBs wasn't completed. Forcefully
  278. * remove them here.
  279. */
  280. if (spsc_queue_peek(&entity->job_queue)) {
  281. struct drm_sched_job *job;
  282. int r;
  283. /* Park the kernel for a moment to make sure it isn't processing
  284. * our enity.
  285. */
  286. kthread_park(sched->thread);
  287. kthread_unpark(sched->thread);
  288. if (entity->dependency) {
  289. dma_fence_remove_callback(entity->dependency,
  290. &entity->cb);
  291. dma_fence_put(entity->dependency);
  292. entity->dependency = NULL;
  293. }
  294. while ((job = to_drm_sched_job(spsc_queue_pop(&entity->job_queue)))) {
  295. struct drm_sched_fence *s_fence = job->s_fence;
  296. drm_sched_fence_scheduled(s_fence);
  297. dma_fence_set_error(&s_fence->finished, -ESRCH);
  298. /*
  299. * When pipe is hanged by older entity, new entity might
  300. * not even have chance to submit it's first job to HW
  301. * and so entity->last_scheduled will remain NULL
  302. */
  303. if (!entity->last_scheduled) {
  304. drm_sched_entity_kill_jobs_cb(NULL, &job->finish_cb);
  305. } else {
  306. r = dma_fence_add_callback(entity->last_scheduled, &job->finish_cb,
  307. drm_sched_entity_kill_jobs_cb);
  308. if (r == -ENOENT)
  309. drm_sched_entity_kill_jobs_cb(NULL, &job->finish_cb);
  310. else if (r)
  311. DRM_ERROR("fence add callback failed (%d)\n", r);
  312. }
  313. }
  314. }
  315. dma_fence_put(entity->last_scheduled);
  316. entity->last_scheduled = NULL;
  317. }
  318. EXPORT_SYMBOL(drm_sched_entity_cleanup);
  319. /**
  320. * drm_sched_entity_fini - Destroy a context entity
  321. *
  322. * @sched: scheduler instance
  323. * @entity: scheduler entity
  324. *
  325. * Calls drm_sched_entity_do_release() and drm_sched_entity_cleanup()
  326. */
  327. void drm_sched_entity_fini(struct drm_gpu_scheduler *sched,
  328. struct drm_sched_entity *entity)
  329. {
  330. drm_sched_entity_do_release(sched, entity, MAX_WAIT_SCHED_ENTITY_Q_EMPTY);
  331. drm_sched_entity_cleanup(sched, entity);
  332. }
  333. EXPORT_SYMBOL(drm_sched_entity_fini);
  334. static void drm_sched_entity_wakeup(struct dma_fence *f, struct dma_fence_cb *cb)
  335. {
  336. struct drm_sched_entity *entity =
  337. container_of(cb, struct drm_sched_entity, cb);
  338. entity->dependency = NULL;
  339. dma_fence_put(f);
  340. drm_sched_wakeup(entity->sched);
  341. }
  342. static void drm_sched_entity_clear_dep(struct dma_fence *f, struct dma_fence_cb *cb)
  343. {
  344. struct drm_sched_entity *entity =
  345. container_of(cb, struct drm_sched_entity, cb);
  346. entity->dependency = NULL;
  347. dma_fence_put(f);
  348. }
  349. /**
  350. * drm_sched_entity_set_rq - Sets the run queue for an entity
  351. *
  352. * @entity: scheduler entity
  353. * @rq: scheduler run queue
  354. *
  355. * Sets the run queue for an entity and removes the entity from the previous
  356. * run queue in which was present.
  357. */
  358. void drm_sched_entity_set_rq(struct drm_sched_entity *entity,
  359. struct drm_sched_rq *rq)
  360. {
  361. if (entity->rq == rq)
  362. return;
  363. spin_lock(&entity->rq_lock);
  364. if (entity->rq)
  365. drm_sched_rq_remove_entity(entity->rq, entity);
  366. entity->rq = rq;
  367. if (rq)
  368. drm_sched_rq_add_entity(rq, entity);
  369. spin_unlock(&entity->rq_lock);
  370. }
  371. EXPORT_SYMBOL(drm_sched_entity_set_rq);
  372. /**
  373. * drm_sched_dependency_optimized
  374. *
  375. * @fence: the dependency fence
  376. * @entity: the entity which depends on the above fence
  377. *
  378. * Returns true if the dependency can be optimized and false otherwise
  379. */
  380. bool drm_sched_dependency_optimized(struct dma_fence* fence,
  381. struct drm_sched_entity *entity)
  382. {
  383. struct drm_gpu_scheduler *sched = entity->sched;
  384. struct drm_sched_fence *s_fence;
  385. if (!fence || dma_fence_is_signaled(fence))
  386. return false;
  387. if (fence->context == entity->fence_context)
  388. return true;
  389. s_fence = to_drm_sched_fence(fence);
  390. if (s_fence && s_fence->sched == sched)
  391. return true;
  392. return false;
  393. }
  394. EXPORT_SYMBOL(drm_sched_dependency_optimized);
  395. static bool drm_sched_entity_add_dependency_cb(struct drm_sched_entity *entity)
  396. {
  397. struct drm_gpu_scheduler *sched = entity->sched;
  398. struct dma_fence * fence = entity->dependency;
  399. struct drm_sched_fence *s_fence;
  400. if (fence->context == entity->fence_context ||
  401. fence->context == entity->fence_context + 1) {
  402. /*
  403. * Fence is a scheduled/finished fence from a job
  404. * which belongs to the same entity, we can ignore
  405. * fences from ourself
  406. */
  407. dma_fence_put(entity->dependency);
  408. return false;
  409. }
  410. s_fence = to_drm_sched_fence(fence);
  411. if (s_fence && s_fence->sched == sched) {
  412. /*
  413. * Fence is from the same scheduler, only need to wait for
  414. * it to be scheduled
  415. */
  416. fence = dma_fence_get(&s_fence->scheduled);
  417. dma_fence_put(entity->dependency);
  418. entity->dependency = fence;
  419. if (!dma_fence_add_callback(fence, &entity->cb,
  420. drm_sched_entity_clear_dep))
  421. return true;
  422. /* Ignore it when it is already scheduled */
  423. dma_fence_put(fence);
  424. return false;
  425. }
  426. if (!dma_fence_add_callback(entity->dependency, &entity->cb,
  427. drm_sched_entity_wakeup))
  428. return true;
  429. dma_fence_put(entity->dependency);
  430. return false;
  431. }
  432. static struct drm_sched_job *
  433. drm_sched_entity_pop_job(struct drm_sched_entity *entity)
  434. {
  435. struct drm_gpu_scheduler *sched = entity->sched;
  436. struct drm_sched_job *sched_job = to_drm_sched_job(
  437. spsc_queue_peek(&entity->job_queue));
  438. if (!sched_job)
  439. return NULL;
  440. while ((entity->dependency = sched->ops->dependency(sched_job, entity)))
  441. if (drm_sched_entity_add_dependency_cb(entity))
  442. return NULL;
  443. /* skip jobs from entity that marked guilty */
  444. if (entity->guilty && atomic_read(entity->guilty))
  445. dma_fence_set_error(&sched_job->s_fence->finished, -ECANCELED);
  446. dma_fence_put(entity->last_scheduled);
  447. entity->last_scheduled = dma_fence_get(&sched_job->s_fence->finished);
  448. spsc_queue_pop(&entity->job_queue);
  449. return sched_job;
  450. }
  451. /**
  452. * drm_sched_entity_push_job - Submit a job to the entity's job queue
  453. *
  454. * @sched_job: job to submit
  455. * @entity: scheduler entity
  456. *
  457. * Note: To guarantee that the order of insertion to queue matches
  458. * the job's fence sequence number this function should be
  459. * called with drm_sched_job_init under common lock.
  460. *
  461. * Returns 0 for success, negative error code otherwise.
  462. */
  463. void drm_sched_entity_push_job(struct drm_sched_job *sched_job,
  464. struct drm_sched_entity *entity)
  465. {
  466. struct drm_gpu_scheduler *sched = sched_job->sched;
  467. bool first = false;
  468. trace_drm_sched_job(sched_job, entity);
  469. first = spsc_queue_push(&entity->job_queue, &sched_job->queue_node);
  470. /* first job wakes up scheduler */
  471. if (first) {
  472. /* Add the entity to the run queue */
  473. spin_lock(&entity->rq_lock);
  474. drm_sched_rq_add_entity(entity->rq, entity);
  475. spin_unlock(&entity->rq_lock);
  476. drm_sched_wakeup(sched);
  477. }
  478. }
  479. EXPORT_SYMBOL(drm_sched_entity_push_job);
  480. /* job_finish is called after hw fence signaled
  481. */
  482. static void drm_sched_job_finish(struct work_struct *work)
  483. {
  484. struct drm_sched_job *s_job = container_of(work, struct drm_sched_job,
  485. finish_work);
  486. struct drm_gpu_scheduler *sched = s_job->sched;
  487. /* remove job from ring_mirror_list */
  488. spin_lock(&sched->job_list_lock);
  489. list_del_init(&s_job->node);
  490. if (sched->timeout != MAX_SCHEDULE_TIMEOUT) {
  491. struct drm_sched_job *next;
  492. spin_unlock(&sched->job_list_lock);
  493. cancel_delayed_work_sync(&s_job->work_tdr);
  494. spin_lock(&sched->job_list_lock);
  495. /* queue TDR for next job */
  496. next = list_first_entry_or_null(&sched->ring_mirror_list,
  497. struct drm_sched_job, node);
  498. if (next)
  499. schedule_delayed_work(&next->work_tdr, sched->timeout);
  500. }
  501. spin_unlock(&sched->job_list_lock);
  502. dma_fence_put(&s_job->s_fence->finished);
  503. sched->ops->free_job(s_job);
  504. }
  505. static void drm_sched_job_finish_cb(struct dma_fence *f,
  506. struct dma_fence_cb *cb)
  507. {
  508. struct drm_sched_job *job = container_of(cb, struct drm_sched_job,
  509. finish_cb);
  510. schedule_work(&job->finish_work);
  511. }
  512. static void drm_sched_job_begin(struct drm_sched_job *s_job)
  513. {
  514. struct drm_gpu_scheduler *sched = s_job->sched;
  515. dma_fence_add_callback(&s_job->s_fence->finished, &s_job->finish_cb,
  516. drm_sched_job_finish_cb);
  517. spin_lock(&sched->job_list_lock);
  518. list_add_tail(&s_job->node, &sched->ring_mirror_list);
  519. if (sched->timeout != MAX_SCHEDULE_TIMEOUT &&
  520. list_first_entry_or_null(&sched->ring_mirror_list,
  521. struct drm_sched_job, node) == s_job)
  522. schedule_delayed_work(&s_job->work_tdr, sched->timeout);
  523. spin_unlock(&sched->job_list_lock);
  524. }
  525. static void drm_sched_job_timedout(struct work_struct *work)
  526. {
  527. struct drm_sched_job *job = container_of(work, struct drm_sched_job,
  528. work_tdr.work);
  529. job->sched->ops->timedout_job(job);
  530. }
  531. /**
  532. * drm_sched_hw_job_reset - stop the scheduler if it contains the bad job
  533. *
  534. * @sched: scheduler instance
  535. * @bad: bad scheduler job
  536. *
  537. */
  538. void drm_sched_hw_job_reset(struct drm_gpu_scheduler *sched, struct drm_sched_job *bad)
  539. {
  540. struct drm_sched_job *s_job;
  541. struct drm_sched_entity *entity, *tmp;
  542. int i;
  543. spin_lock(&sched->job_list_lock);
  544. list_for_each_entry_reverse(s_job, &sched->ring_mirror_list, node) {
  545. if (s_job->s_fence->parent &&
  546. dma_fence_remove_callback(s_job->s_fence->parent,
  547. &s_job->s_fence->cb)) {
  548. dma_fence_put(s_job->s_fence->parent);
  549. s_job->s_fence->parent = NULL;
  550. atomic_dec(&sched->hw_rq_count);
  551. }
  552. }
  553. spin_unlock(&sched->job_list_lock);
  554. if (bad && bad->s_priority != DRM_SCHED_PRIORITY_KERNEL) {
  555. atomic_inc(&bad->karma);
  556. /* don't increase @bad's karma if it's from KERNEL RQ,
  557. * becuase sometimes GPU hang would cause kernel jobs (like VM updating jobs)
  558. * corrupt but keep in mind that kernel jobs always considered good.
  559. */
  560. for (i = DRM_SCHED_PRIORITY_MIN; i < DRM_SCHED_PRIORITY_KERNEL; i++ ) {
  561. struct drm_sched_rq *rq = &sched->sched_rq[i];
  562. spin_lock(&rq->lock);
  563. list_for_each_entry_safe(entity, tmp, &rq->entities, list) {
  564. if (bad->s_fence->scheduled.context == entity->fence_context) {
  565. if (atomic_read(&bad->karma) > bad->sched->hang_limit)
  566. if (entity->guilty)
  567. atomic_set(entity->guilty, 1);
  568. break;
  569. }
  570. }
  571. spin_unlock(&rq->lock);
  572. if (&entity->list != &rq->entities)
  573. break;
  574. }
  575. }
  576. }
  577. EXPORT_SYMBOL(drm_sched_hw_job_reset);
  578. /**
  579. * drm_sched_job_recovery - recover jobs after a reset
  580. *
  581. * @sched: scheduler instance
  582. *
  583. */
  584. void drm_sched_job_recovery(struct drm_gpu_scheduler *sched)
  585. {
  586. struct drm_sched_job *s_job, *tmp;
  587. bool found_guilty = false;
  588. int r;
  589. spin_lock(&sched->job_list_lock);
  590. s_job = list_first_entry_or_null(&sched->ring_mirror_list,
  591. struct drm_sched_job, node);
  592. if (s_job && sched->timeout != MAX_SCHEDULE_TIMEOUT)
  593. schedule_delayed_work(&s_job->work_tdr, sched->timeout);
  594. list_for_each_entry_safe(s_job, tmp, &sched->ring_mirror_list, node) {
  595. struct drm_sched_fence *s_fence = s_job->s_fence;
  596. struct dma_fence *fence;
  597. uint64_t guilty_context;
  598. if (!found_guilty && atomic_read(&s_job->karma) > sched->hang_limit) {
  599. found_guilty = true;
  600. guilty_context = s_job->s_fence->scheduled.context;
  601. }
  602. if (found_guilty && s_job->s_fence->scheduled.context == guilty_context)
  603. dma_fence_set_error(&s_fence->finished, -ECANCELED);
  604. spin_unlock(&sched->job_list_lock);
  605. fence = sched->ops->run_job(s_job);
  606. atomic_inc(&sched->hw_rq_count);
  607. if (fence) {
  608. s_fence->parent = dma_fence_get(fence);
  609. r = dma_fence_add_callback(fence, &s_fence->cb,
  610. drm_sched_process_job);
  611. if (r == -ENOENT)
  612. drm_sched_process_job(fence, &s_fence->cb);
  613. else if (r)
  614. DRM_ERROR("fence add callback failed (%d)\n",
  615. r);
  616. dma_fence_put(fence);
  617. } else {
  618. drm_sched_process_job(NULL, &s_fence->cb);
  619. }
  620. spin_lock(&sched->job_list_lock);
  621. }
  622. spin_unlock(&sched->job_list_lock);
  623. }
  624. EXPORT_SYMBOL(drm_sched_job_recovery);
  625. /**
  626. * drm_sched_job_init - init a scheduler job
  627. *
  628. * @job: scheduler job to init
  629. * @sched: scheduler instance
  630. * @entity: scheduler entity to use
  631. * @owner: job owner for debugging
  632. *
  633. * Refer to drm_sched_entity_push_job() documentation
  634. * for locking considerations.
  635. *
  636. * Returns 0 for success, negative error code otherwise.
  637. */
  638. int drm_sched_job_init(struct drm_sched_job *job,
  639. struct drm_gpu_scheduler *sched,
  640. struct drm_sched_entity *entity,
  641. void *owner)
  642. {
  643. job->sched = sched;
  644. job->entity = entity;
  645. job->s_priority = entity->rq - sched->sched_rq;
  646. job->s_fence = drm_sched_fence_create(entity, owner);
  647. if (!job->s_fence)
  648. return -ENOMEM;
  649. job->id = atomic64_inc_return(&sched->job_id_count);
  650. INIT_WORK(&job->finish_work, drm_sched_job_finish);
  651. INIT_LIST_HEAD(&job->node);
  652. INIT_DELAYED_WORK(&job->work_tdr, drm_sched_job_timedout);
  653. return 0;
  654. }
  655. EXPORT_SYMBOL(drm_sched_job_init);
  656. /**
  657. * drm_sched_ready - is the scheduler ready
  658. *
  659. * @sched: scheduler instance
  660. *
  661. * Return true if we can push more jobs to the hw, otherwise false.
  662. */
  663. static bool drm_sched_ready(struct drm_gpu_scheduler *sched)
  664. {
  665. return atomic_read(&sched->hw_rq_count) <
  666. sched->hw_submission_limit;
  667. }
  668. /**
  669. * drm_sched_wakeup - Wake up the scheduler when it is ready
  670. *
  671. * @sched: scheduler instance
  672. *
  673. */
  674. static void drm_sched_wakeup(struct drm_gpu_scheduler *sched)
  675. {
  676. if (drm_sched_ready(sched))
  677. wake_up_interruptible(&sched->wake_up_worker);
  678. }
  679. /**
  680. * drm_sched_select_entity - Select next entity to process
  681. *
  682. * @sched: scheduler instance
  683. *
  684. * Returns the entity to process or NULL if none are found.
  685. */
  686. static struct drm_sched_entity *
  687. drm_sched_select_entity(struct drm_gpu_scheduler *sched)
  688. {
  689. struct drm_sched_entity *entity;
  690. int i;
  691. if (!drm_sched_ready(sched))
  692. return NULL;
  693. /* Kernel run queue has higher priority than normal run queue*/
  694. for (i = DRM_SCHED_PRIORITY_MAX - 1; i >= DRM_SCHED_PRIORITY_MIN; i--) {
  695. entity = drm_sched_rq_select_entity(&sched->sched_rq[i]);
  696. if (entity)
  697. break;
  698. }
  699. return entity;
  700. }
  701. /**
  702. * drm_sched_process_job - process a job
  703. *
  704. * @f: fence
  705. * @cb: fence callbacks
  706. *
  707. * Called after job has finished execution.
  708. */
  709. static void drm_sched_process_job(struct dma_fence *f, struct dma_fence_cb *cb)
  710. {
  711. struct drm_sched_fence *s_fence =
  712. container_of(cb, struct drm_sched_fence, cb);
  713. struct drm_gpu_scheduler *sched = s_fence->sched;
  714. dma_fence_get(&s_fence->finished);
  715. atomic_dec(&sched->hw_rq_count);
  716. drm_sched_fence_finished(s_fence);
  717. trace_drm_sched_process_job(s_fence);
  718. dma_fence_put(&s_fence->finished);
  719. wake_up_interruptible(&sched->wake_up_worker);
  720. }
  721. /**
  722. * drm_sched_blocked - check if the scheduler is blocked
  723. *
  724. * @sched: scheduler instance
  725. *
  726. * Returns true if blocked, otherwise false.
  727. */
  728. static bool drm_sched_blocked(struct drm_gpu_scheduler *sched)
  729. {
  730. if (kthread_should_park()) {
  731. kthread_parkme();
  732. return true;
  733. }
  734. return false;
  735. }
  736. /**
  737. * drm_sched_main - main scheduler thread
  738. *
  739. * @param: scheduler instance
  740. *
  741. * Returns 0.
  742. */
  743. static int drm_sched_main(void *param)
  744. {
  745. struct sched_param sparam = {.sched_priority = 1};
  746. struct drm_gpu_scheduler *sched = (struct drm_gpu_scheduler *)param;
  747. int r;
  748. sched_setscheduler(current, SCHED_FIFO, &sparam);
  749. while (!kthread_should_stop()) {
  750. struct drm_sched_entity *entity = NULL;
  751. struct drm_sched_fence *s_fence;
  752. struct drm_sched_job *sched_job;
  753. struct dma_fence *fence;
  754. wait_event_interruptible(sched->wake_up_worker,
  755. (!drm_sched_blocked(sched) &&
  756. (entity = drm_sched_select_entity(sched))) ||
  757. kthread_should_stop());
  758. if (!entity)
  759. continue;
  760. sched_job = drm_sched_entity_pop_job(entity);
  761. if (!sched_job)
  762. continue;
  763. s_fence = sched_job->s_fence;
  764. atomic_inc(&sched->hw_rq_count);
  765. drm_sched_job_begin(sched_job);
  766. fence = sched->ops->run_job(sched_job);
  767. drm_sched_fence_scheduled(s_fence);
  768. if (fence) {
  769. s_fence->parent = dma_fence_get(fence);
  770. r = dma_fence_add_callback(fence, &s_fence->cb,
  771. drm_sched_process_job);
  772. if (r == -ENOENT)
  773. drm_sched_process_job(fence, &s_fence->cb);
  774. else if (r)
  775. DRM_ERROR("fence add callback failed (%d)\n",
  776. r);
  777. dma_fence_put(fence);
  778. } else {
  779. drm_sched_process_job(NULL, &s_fence->cb);
  780. }
  781. wake_up(&sched->job_scheduled);
  782. }
  783. return 0;
  784. }
  785. /**
  786. * drm_sched_init - Init a gpu scheduler instance
  787. *
  788. * @sched: scheduler instance
  789. * @ops: backend operations for this scheduler
  790. * @hw_submission: number of hw submissions that can be in flight
  791. * @hang_limit: number of times to allow a job to hang before dropping it
  792. * @timeout: timeout value in jiffies for the scheduler
  793. * @name: name used for debugging
  794. *
  795. * Return 0 on success, otherwise error code.
  796. */
  797. int drm_sched_init(struct drm_gpu_scheduler *sched,
  798. const struct drm_sched_backend_ops *ops,
  799. unsigned hw_submission,
  800. unsigned hang_limit,
  801. long timeout,
  802. const char *name)
  803. {
  804. int i;
  805. sched->ops = ops;
  806. sched->hw_submission_limit = hw_submission;
  807. sched->name = name;
  808. sched->timeout = timeout;
  809. sched->hang_limit = hang_limit;
  810. for (i = DRM_SCHED_PRIORITY_MIN; i < DRM_SCHED_PRIORITY_MAX; i++)
  811. drm_sched_rq_init(&sched->sched_rq[i]);
  812. init_waitqueue_head(&sched->wake_up_worker);
  813. init_waitqueue_head(&sched->job_scheduled);
  814. INIT_LIST_HEAD(&sched->ring_mirror_list);
  815. spin_lock_init(&sched->job_list_lock);
  816. atomic_set(&sched->hw_rq_count, 0);
  817. atomic64_set(&sched->job_id_count, 0);
  818. /* Each scheduler will run on a seperate kernel thread */
  819. sched->thread = kthread_run(drm_sched_main, sched, sched->name);
  820. if (IS_ERR(sched->thread)) {
  821. DRM_ERROR("Failed to create scheduler for %s.\n", name);
  822. return PTR_ERR(sched->thread);
  823. }
  824. return 0;
  825. }
  826. EXPORT_SYMBOL(drm_sched_init);
  827. /**
  828. * drm_sched_fini - Destroy a gpu scheduler
  829. *
  830. * @sched: scheduler instance
  831. *
  832. * Tears down and cleans up the scheduler.
  833. */
  834. void drm_sched_fini(struct drm_gpu_scheduler *sched)
  835. {
  836. if (sched->thread)
  837. kthread_stop(sched->thread);
  838. }
  839. EXPORT_SYMBOL(drm_sched_fini);