gpu_scheduler.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  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_gpu_scheduler *sched,
  67. struct drm_sched_rq *rq)
  68. {
  69. spin_lock_init(&rq->lock);
  70. INIT_LIST_HEAD(&rq->entities);
  71. rq->current_entity = NULL;
  72. rq->sched = sched;
  73. }
  74. /**
  75. * drm_sched_rq_add_entity - add an entity
  76. *
  77. * @rq: scheduler run queue
  78. * @entity: scheduler entity
  79. *
  80. * Adds a scheduler entity to the run queue.
  81. */
  82. static void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
  83. struct drm_sched_entity *entity)
  84. {
  85. if (!list_empty(&entity->list))
  86. return;
  87. spin_lock(&rq->lock);
  88. list_add_tail(&entity->list, &rq->entities);
  89. spin_unlock(&rq->lock);
  90. }
  91. /**
  92. * drm_sched_rq_remove_entity - remove an entity
  93. *
  94. * @rq: scheduler run queue
  95. * @entity: scheduler entity
  96. *
  97. * Removes a scheduler entity from the run queue.
  98. */
  99. static void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
  100. struct drm_sched_entity *entity)
  101. {
  102. if (list_empty(&entity->list))
  103. return;
  104. spin_lock(&rq->lock);
  105. list_del_init(&entity->list);
  106. if (rq->current_entity == entity)
  107. rq->current_entity = NULL;
  108. spin_unlock(&rq->lock);
  109. }
  110. /**
  111. * drm_sched_rq_select_entity - Select an entity which could provide a job to run
  112. *
  113. * @rq: scheduler run queue to check.
  114. *
  115. * Try to find a ready entity, returns NULL if none found.
  116. */
  117. static struct drm_sched_entity *
  118. drm_sched_rq_select_entity(struct drm_sched_rq *rq)
  119. {
  120. struct drm_sched_entity *entity;
  121. spin_lock(&rq->lock);
  122. entity = rq->current_entity;
  123. if (entity) {
  124. list_for_each_entry_continue(entity, &rq->entities, list) {
  125. if (drm_sched_entity_is_ready(entity)) {
  126. rq->current_entity = entity;
  127. spin_unlock(&rq->lock);
  128. return entity;
  129. }
  130. }
  131. }
  132. list_for_each_entry(entity, &rq->entities, list) {
  133. if (drm_sched_entity_is_ready(entity)) {
  134. rq->current_entity = entity;
  135. spin_unlock(&rq->lock);
  136. return entity;
  137. }
  138. if (entity == rq->current_entity)
  139. break;
  140. }
  141. spin_unlock(&rq->lock);
  142. return NULL;
  143. }
  144. /**
  145. * drm_sched_entity_init - Init a context entity used by scheduler when
  146. * submit to HW ring.
  147. *
  148. * @entity: scheduler entity to init
  149. * @rq_list: the list of run queue on which jobs from this
  150. * entity can be submitted
  151. * @num_rq_list: number of run queue in rq_list
  152. * @guilty: atomic_t set to 1 when a job on this queue
  153. * is found to be guilty causing a timeout
  154. *
  155. * Note: the rq_list should have atleast one element to schedule
  156. * the entity
  157. *
  158. * Returns 0 on success or a negative error code on failure.
  159. */
  160. int drm_sched_entity_init(struct drm_sched_entity *entity,
  161. struct drm_sched_rq **rq_list,
  162. unsigned int num_rq_list,
  163. atomic_t *guilty)
  164. {
  165. if (!(entity && rq_list && num_rq_list > 0 && rq_list[0]))
  166. return -EINVAL;
  167. memset(entity, 0, sizeof(struct drm_sched_entity));
  168. INIT_LIST_HEAD(&entity->list);
  169. entity->rq = rq_list[0];
  170. entity->guilty = guilty;
  171. entity->last_scheduled = NULL;
  172. spin_lock_init(&entity->rq_lock);
  173. spsc_queue_init(&entity->job_queue);
  174. atomic_set(&entity->fence_seq, 0);
  175. entity->fence_context = dma_fence_context_alloc(2);
  176. return 0;
  177. }
  178. EXPORT_SYMBOL(drm_sched_entity_init);
  179. /**
  180. * drm_sched_entity_is_idle - Check if entity is idle
  181. *
  182. * @entity: scheduler entity
  183. *
  184. * Returns true if the entity does not have any unscheduled jobs.
  185. */
  186. static bool drm_sched_entity_is_idle(struct drm_sched_entity *entity)
  187. {
  188. rmb();
  189. if (list_empty(&entity->list) ||
  190. spsc_queue_peek(&entity->job_queue) == NULL)
  191. return true;
  192. return false;
  193. }
  194. /**
  195. * drm_sched_entity_is_ready - Check if entity is ready
  196. *
  197. * @entity: scheduler entity
  198. *
  199. * Return true if entity could provide a job.
  200. */
  201. static bool drm_sched_entity_is_ready(struct drm_sched_entity *entity)
  202. {
  203. if (spsc_queue_peek(&entity->job_queue) == NULL)
  204. return false;
  205. if (READ_ONCE(entity->dependency))
  206. return false;
  207. return true;
  208. }
  209. static void drm_sched_entity_kill_jobs_cb(struct dma_fence *f,
  210. struct dma_fence_cb *cb)
  211. {
  212. struct drm_sched_job *job = container_of(cb, struct drm_sched_job,
  213. finish_cb);
  214. drm_sched_fence_finished(job->s_fence);
  215. WARN_ON(job->s_fence->parent);
  216. dma_fence_put(&job->s_fence->finished);
  217. job->sched->ops->free_job(job);
  218. }
  219. /**
  220. * drm_sched_entity_flush - Flush a context entity
  221. *
  222. * @entity: scheduler entity
  223. * @timeout: time to wait in for Q to become empty in jiffies.
  224. *
  225. * Splitting drm_sched_entity_fini() into two functions, The first one does the waiting,
  226. * removes the entity from the runqueue and returns an error when the process was killed.
  227. *
  228. * Returns the remaining time in jiffies left from the input timeout
  229. */
  230. long drm_sched_entity_flush(struct drm_sched_entity *entity, long timeout)
  231. {
  232. struct drm_gpu_scheduler *sched;
  233. struct task_struct *last_user;
  234. long ret = timeout;
  235. sched = entity->rq->sched;
  236. /**
  237. * The client will not queue more IBs during this fini, consume existing
  238. * queued IBs or discard them on SIGKILL
  239. */
  240. if (current->flags & PF_EXITING) {
  241. if (timeout)
  242. ret = wait_event_timeout(
  243. sched->job_scheduled,
  244. drm_sched_entity_is_idle(entity),
  245. timeout);
  246. } else
  247. wait_event_killable(sched->job_scheduled, drm_sched_entity_is_idle(entity));
  248. /* For killed process disable any more IBs enqueue right now */
  249. last_user = cmpxchg(&entity->last_user, current->group_leader, NULL);
  250. if ((!last_user || last_user == current->group_leader) &&
  251. (current->flags & PF_EXITING) && (current->exit_code == SIGKILL))
  252. drm_sched_rq_remove_entity(entity->rq, entity);
  253. return ret;
  254. }
  255. EXPORT_SYMBOL(drm_sched_entity_flush);
  256. /**
  257. * drm_sched_entity_cleanup - Destroy a context entity
  258. *
  259. * @entity: scheduler entity
  260. *
  261. * This should be called after @drm_sched_entity_do_release. It goes over the
  262. * entity and signals all jobs with an error code if the process was killed.
  263. *
  264. */
  265. void drm_sched_entity_fini(struct drm_sched_entity *entity)
  266. {
  267. struct drm_gpu_scheduler *sched;
  268. sched = entity->rq->sched;
  269. drm_sched_rq_remove_entity(entity->rq, entity);
  270. /* Consumption of existing IBs wasn't completed. Forcefully
  271. * remove them here.
  272. */
  273. if (spsc_queue_peek(&entity->job_queue)) {
  274. struct drm_sched_job *job;
  275. int r;
  276. /* Park the kernel for a moment to make sure it isn't processing
  277. * our enity.
  278. */
  279. kthread_park(sched->thread);
  280. kthread_unpark(sched->thread);
  281. if (entity->dependency) {
  282. dma_fence_remove_callback(entity->dependency,
  283. &entity->cb);
  284. dma_fence_put(entity->dependency);
  285. entity->dependency = NULL;
  286. }
  287. while ((job = to_drm_sched_job(spsc_queue_pop(&entity->job_queue)))) {
  288. struct drm_sched_fence *s_fence = job->s_fence;
  289. drm_sched_fence_scheduled(s_fence);
  290. dma_fence_set_error(&s_fence->finished, -ESRCH);
  291. /*
  292. * When pipe is hanged by older entity, new entity might
  293. * not even have chance to submit it's first job to HW
  294. * and so entity->last_scheduled will remain NULL
  295. */
  296. if (!entity->last_scheduled) {
  297. drm_sched_entity_kill_jobs_cb(NULL, &job->finish_cb);
  298. } else {
  299. r = dma_fence_add_callback(entity->last_scheduled, &job->finish_cb,
  300. drm_sched_entity_kill_jobs_cb);
  301. if (r == -ENOENT)
  302. drm_sched_entity_kill_jobs_cb(NULL, &job->finish_cb);
  303. else if (r)
  304. DRM_ERROR("fence add callback failed (%d)\n", r);
  305. }
  306. }
  307. }
  308. dma_fence_put(entity->last_scheduled);
  309. entity->last_scheduled = NULL;
  310. }
  311. EXPORT_SYMBOL(drm_sched_entity_fini);
  312. /**
  313. * drm_sched_entity_fini - Destroy a context entity
  314. *
  315. * @entity: scheduler entity
  316. *
  317. * Calls drm_sched_entity_do_release() and drm_sched_entity_cleanup()
  318. */
  319. void drm_sched_entity_destroy(struct drm_sched_entity *entity)
  320. {
  321. drm_sched_entity_flush(entity, MAX_WAIT_SCHED_ENTITY_Q_EMPTY);
  322. drm_sched_entity_fini(entity);
  323. }
  324. EXPORT_SYMBOL(drm_sched_entity_destroy);
  325. static void drm_sched_entity_wakeup(struct dma_fence *f, struct dma_fence_cb *cb)
  326. {
  327. struct drm_sched_entity *entity =
  328. container_of(cb, struct drm_sched_entity, cb);
  329. entity->dependency = NULL;
  330. dma_fence_put(f);
  331. drm_sched_wakeup(entity->rq->sched);
  332. }
  333. static void drm_sched_entity_clear_dep(struct dma_fence *f, struct dma_fence_cb *cb)
  334. {
  335. struct drm_sched_entity *entity =
  336. container_of(cb, struct drm_sched_entity, cb);
  337. entity->dependency = NULL;
  338. dma_fence_put(f);
  339. }
  340. /**
  341. * drm_sched_entity_set_rq - Sets the run queue for an entity
  342. *
  343. * @entity: scheduler entity
  344. * @rq: scheduler run queue
  345. *
  346. * Sets the run queue for an entity and removes the entity from the previous
  347. * run queue in which was present.
  348. */
  349. void drm_sched_entity_set_rq(struct drm_sched_entity *entity,
  350. struct drm_sched_rq *rq)
  351. {
  352. if (entity->rq == rq)
  353. return;
  354. BUG_ON(!rq);
  355. spin_lock(&entity->rq_lock);
  356. drm_sched_rq_remove_entity(entity->rq, entity);
  357. entity->rq = rq;
  358. drm_sched_rq_add_entity(rq, entity);
  359. spin_unlock(&entity->rq_lock);
  360. }
  361. EXPORT_SYMBOL(drm_sched_entity_set_rq);
  362. /**
  363. * drm_sched_dependency_optimized
  364. *
  365. * @fence: the dependency fence
  366. * @entity: the entity which depends on the above fence
  367. *
  368. * Returns true if the dependency can be optimized and false otherwise
  369. */
  370. bool drm_sched_dependency_optimized(struct dma_fence* fence,
  371. struct drm_sched_entity *entity)
  372. {
  373. struct drm_gpu_scheduler *sched = entity->rq->sched;
  374. struct drm_sched_fence *s_fence;
  375. if (!fence || dma_fence_is_signaled(fence))
  376. return false;
  377. if (fence->context == entity->fence_context)
  378. return true;
  379. s_fence = to_drm_sched_fence(fence);
  380. if (s_fence && s_fence->sched == sched)
  381. return true;
  382. return false;
  383. }
  384. EXPORT_SYMBOL(drm_sched_dependency_optimized);
  385. static bool drm_sched_entity_add_dependency_cb(struct drm_sched_entity *entity)
  386. {
  387. struct drm_gpu_scheduler *sched = entity->rq->sched;
  388. struct dma_fence * fence = entity->dependency;
  389. struct drm_sched_fence *s_fence;
  390. if (fence->context == entity->fence_context ||
  391. fence->context == entity->fence_context + 1) {
  392. /*
  393. * Fence is a scheduled/finished fence from a job
  394. * which belongs to the same entity, we can ignore
  395. * fences from ourself
  396. */
  397. dma_fence_put(entity->dependency);
  398. return false;
  399. }
  400. s_fence = to_drm_sched_fence(fence);
  401. if (s_fence && s_fence->sched == sched) {
  402. /*
  403. * Fence is from the same scheduler, only need to wait for
  404. * it to be scheduled
  405. */
  406. fence = dma_fence_get(&s_fence->scheduled);
  407. dma_fence_put(entity->dependency);
  408. entity->dependency = fence;
  409. if (!dma_fence_add_callback(fence, &entity->cb,
  410. drm_sched_entity_clear_dep))
  411. return true;
  412. /* Ignore it when it is already scheduled */
  413. dma_fence_put(fence);
  414. return false;
  415. }
  416. if (!dma_fence_add_callback(entity->dependency, &entity->cb,
  417. drm_sched_entity_wakeup))
  418. return true;
  419. dma_fence_put(entity->dependency);
  420. return false;
  421. }
  422. static struct drm_sched_job *
  423. drm_sched_entity_pop_job(struct drm_sched_entity *entity)
  424. {
  425. struct drm_gpu_scheduler *sched = entity->rq->sched;
  426. struct drm_sched_job *sched_job = to_drm_sched_job(
  427. spsc_queue_peek(&entity->job_queue));
  428. if (!sched_job)
  429. return NULL;
  430. while ((entity->dependency = sched->ops->dependency(sched_job, entity)))
  431. if (drm_sched_entity_add_dependency_cb(entity))
  432. return NULL;
  433. /* skip jobs from entity that marked guilty */
  434. if (entity->guilty && atomic_read(entity->guilty))
  435. dma_fence_set_error(&sched_job->s_fence->finished, -ECANCELED);
  436. dma_fence_put(entity->last_scheduled);
  437. entity->last_scheduled = dma_fence_get(&sched_job->s_fence->finished);
  438. spsc_queue_pop(&entity->job_queue);
  439. return sched_job;
  440. }
  441. /**
  442. * drm_sched_entity_push_job - Submit a job to the entity's job queue
  443. *
  444. * @sched_job: job to submit
  445. * @entity: scheduler entity
  446. *
  447. * Note: To guarantee that the order of insertion to queue matches
  448. * the job's fence sequence number this function should be
  449. * called with drm_sched_job_init under common lock.
  450. *
  451. * Returns 0 for success, negative error code otherwise.
  452. */
  453. void drm_sched_entity_push_job(struct drm_sched_job *sched_job,
  454. struct drm_sched_entity *entity)
  455. {
  456. struct drm_gpu_scheduler *sched = sched_job->sched;
  457. bool first = false;
  458. trace_drm_sched_job(sched_job, entity);
  459. WRITE_ONCE(entity->last_user, current->group_leader);
  460. first = spsc_queue_push(&entity->job_queue, &sched_job->queue_node);
  461. /* first job wakes up scheduler */
  462. if (first) {
  463. /* Add the entity to the run queue */
  464. spin_lock(&entity->rq_lock);
  465. if (!entity->rq) {
  466. DRM_ERROR("Trying to push to a killed entity\n");
  467. spin_unlock(&entity->rq_lock);
  468. return;
  469. }
  470. drm_sched_rq_add_entity(entity->rq, entity);
  471. spin_unlock(&entity->rq_lock);
  472. drm_sched_wakeup(sched);
  473. }
  474. }
  475. EXPORT_SYMBOL(drm_sched_entity_push_job);
  476. /* job_finish is called after hw fence signaled
  477. */
  478. static void drm_sched_job_finish(struct work_struct *work)
  479. {
  480. struct drm_sched_job *s_job = container_of(work, struct drm_sched_job,
  481. finish_work);
  482. struct drm_gpu_scheduler *sched = s_job->sched;
  483. /*
  484. * Canceling the timeout without removing our job from the ring mirror
  485. * list is safe, as we will only end up in this worker if our jobs
  486. * finished fence has been signaled. So even if some another worker
  487. * manages to find this job as the next job in the list, the fence
  488. * signaled check below will prevent the timeout to be restarted.
  489. */
  490. cancel_delayed_work_sync(&s_job->work_tdr);
  491. spin_lock(&sched->job_list_lock);
  492. /* queue TDR for next job */
  493. if (sched->timeout != MAX_SCHEDULE_TIMEOUT &&
  494. !list_is_last(&s_job->node, &sched->ring_mirror_list)) {
  495. struct drm_sched_job *next = list_next_entry(s_job, node);
  496. if (!dma_fence_is_signaled(&next->s_fence->finished))
  497. schedule_delayed_work(&next->work_tdr, sched->timeout);
  498. }
  499. /* remove job from ring_mirror_list */
  500. list_del(&s_job->node);
  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. * @entity: scheduler entity to use
  630. * @owner: job owner for debugging
  631. *
  632. * Refer to drm_sched_entity_push_job() documentation
  633. * for locking considerations.
  634. *
  635. * Returns 0 for success, negative error code otherwise.
  636. */
  637. int drm_sched_job_init(struct drm_sched_job *job,
  638. struct drm_sched_entity *entity,
  639. void *owner)
  640. {
  641. struct drm_gpu_scheduler *sched = entity->rq->sched;
  642. job->sched = sched;
  643. job->entity = entity;
  644. job->s_priority = entity->rq - sched->sched_rq;
  645. job->s_fence = drm_sched_fence_create(entity, owner);
  646. if (!job->s_fence)
  647. return -ENOMEM;
  648. job->id = atomic64_inc_return(&sched->job_id_count);
  649. INIT_WORK(&job->finish_work, drm_sched_job_finish);
  650. INIT_LIST_HEAD(&job->node);
  651. INIT_DELAYED_WORK(&job->work_tdr, drm_sched_job_timedout);
  652. return 0;
  653. }
  654. EXPORT_SYMBOL(drm_sched_job_init);
  655. /**
  656. * drm_sched_ready - is the scheduler ready
  657. *
  658. * @sched: scheduler instance
  659. *
  660. * Return true if we can push more jobs to the hw, otherwise false.
  661. */
  662. static bool drm_sched_ready(struct drm_gpu_scheduler *sched)
  663. {
  664. return atomic_read(&sched->hw_rq_count) <
  665. sched->hw_submission_limit;
  666. }
  667. /**
  668. * drm_sched_wakeup - Wake up the scheduler when it is ready
  669. *
  670. * @sched: scheduler instance
  671. *
  672. */
  673. static void drm_sched_wakeup(struct drm_gpu_scheduler *sched)
  674. {
  675. if (drm_sched_ready(sched))
  676. wake_up_interruptible(&sched->wake_up_worker);
  677. }
  678. /**
  679. * drm_sched_select_entity - Select next entity to process
  680. *
  681. * @sched: scheduler instance
  682. *
  683. * Returns the entity to process or NULL if none are found.
  684. */
  685. static struct drm_sched_entity *
  686. drm_sched_select_entity(struct drm_gpu_scheduler *sched)
  687. {
  688. struct drm_sched_entity *entity;
  689. int i;
  690. if (!drm_sched_ready(sched))
  691. return NULL;
  692. /* Kernel run queue has higher priority than normal run queue*/
  693. for (i = DRM_SCHED_PRIORITY_MAX - 1; i >= DRM_SCHED_PRIORITY_MIN; i--) {
  694. entity = drm_sched_rq_select_entity(&sched->sched_rq[i]);
  695. if (entity)
  696. break;
  697. }
  698. return entity;
  699. }
  700. /**
  701. * drm_sched_process_job - process a job
  702. *
  703. * @f: fence
  704. * @cb: fence callbacks
  705. *
  706. * Called after job has finished execution.
  707. */
  708. static void drm_sched_process_job(struct dma_fence *f, struct dma_fence_cb *cb)
  709. {
  710. struct drm_sched_fence *s_fence =
  711. container_of(cb, struct drm_sched_fence, cb);
  712. struct drm_gpu_scheduler *sched = s_fence->sched;
  713. dma_fence_get(&s_fence->finished);
  714. atomic_dec(&sched->hw_rq_count);
  715. drm_sched_fence_finished(s_fence);
  716. trace_drm_sched_process_job(s_fence);
  717. dma_fence_put(&s_fence->finished);
  718. wake_up_interruptible(&sched->wake_up_worker);
  719. }
  720. /**
  721. * drm_sched_blocked - check if the scheduler is blocked
  722. *
  723. * @sched: scheduler instance
  724. *
  725. * Returns true if blocked, otherwise false.
  726. */
  727. static bool drm_sched_blocked(struct drm_gpu_scheduler *sched)
  728. {
  729. if (kthread_should_park()) {
  730. kthread_parkme();
  731. return true;
  732. }
  733. return false;
  734. }
  735. /**
  736. * drm_sched_main - main scheduler thread
  737. *
  738. * @param: scheduler instance
  739. *
  740. * Returns 0.
  741. */
  742. static int drm_sched_main(void *param)
  743. {
  744. struct sched_param sparam = {.sched_priority = 1};
  745. struct drm_gpu_scheduler *sched = (struct drm_gpu_scheduler *)param;
  746. int r;
  747. sched_setscheduler(current, SCHED_FIFO, &sparam);
  748. while (!kthread_should_stop()) {
  749. struct drm_sched_entity *entity = NULL;
  750. struct drm_sched_fence *s_fence;
  751. struct drm_sched_job *sched_job;
  752. struct dma_fence *fence;
  753. wait_event_interruptible(sched->wake_up_worker,
  754. (!drm_sched_blocked(sched) &&
  755. (entity = drm_sched_select_entity(sched))) ||
  756. kthread_should_stop());
  757. if (!entity)
  758. continue;
  759. sched_job = drm_sched_entity_pop_job(entity);
  760. if (!sched_job)
  761. continue;
  762. s_fence = sched_job->s_fence;
  763. atomic_inc(&sched->hw_rq_count);
  764. drm_sched_job_begin(sched_job);
  765. fence = sched->ops->run_job(sched_job);
  766. drm_sched_fence_scheduled(s_fence);
  767. if (fence) {
  768. s_fence->parent = dma_fence_get(fence);
  769. r = dma_fence_add_callback(fence, &s_fence->cb,
  770. drm_sched_process_job);
  771. if (r == -ENOENT)
  772. drm_sched_process_job(fence, &s_fence->cb);
  773. else if (r)
  774. DRM_ERROR("fence add callback failed (%d)\n",
  775. r);
  776. dma_fence_put(fence);
  777. } else {
  778. drm_sched_process_job(NULL, &s_fence->cb);
  779. }
  780. wake_up(&sched->job_scheduled);
  781. }
  782. return 0;
  783. }
  784. /**
  785. * drm_sched_init - Init a gpu scheduler instance
  786. *
  787. * @sched: scheduler instance
  788. * @ops: backend operations for this scheduler
  789. * @hw_submission: number of hw submissions that can be in flight
  790. * @hang_limit: number of times to allow a job to hang before dropping it
  791. * @timeout: timeout value in jiffies for the scheduler
  792. * @name: name used for debugging
  793. *
  794. * Return 0 on success, otherwise error code.
  795. */
  796. int drm_sched_init(struct drm_gpu_scheduler *sched,
  797. const struct drm_sched_backend_ops *ops,
  798. unsigned hw_submission,
  799. unsigned hang_limit,
  800. long timeout,
  801. const char *name)
  802. {
  803. int i;
  804. sched->ops = ops;
  805. sched->hw_submission_limit = hw_submission;
  806. sched->name = name;
  807. sched->timeout = timeout;
  808. sched->hang_limit = hang_limit;
  809. for (i = DRM_SCHED_PRIORITY_MIN; i < DRM_SCHED_PRIORITY_MAX; i++)
  810. drm_sched_rq_init(sched, &sched->sched_rq[i]);
  811. init_waitqueue_head(&sched->wake_up_worker);
  812. init_waitqueue_head(&sched->job_scheduled);
  813. INIT_LIST_HEAD(&sched->ring_mirror_list);
  814. spin_lock_init(&sched->job_list_lock);
  815. atomic_set(&sched->hw_rq_count, 0);
  816. atomic64_set(&sched->job_id_count, 0);
  817. /* Each scheduler will run on a seperate kernel thread */
  818. sched->thread = kthread_run(drm_sched_main, sched, sched->name);
  819. if (IS_ERR(sched->thread)) {
  820. DRM_ERROR("Failed to create scheduler for %s.\n", name);
  821. return PTR_ERR(sched->thread);
  822. }
  823. return 0;
  824. }
  825. EXPORT_SYMBOL(drm_sched_init);
  826. /**
  827. * drm_sched_fini - Destroy a gpu scheduler
  828. *
  829. * @sched: scheduler instance
  830. *
  831. * Tears down and cleans up the scheduler.
  832. */
  833. void drm_sched_fini(struct drm_gpu_scheduler *sched)
  834. {
  835. if (sched->thread)
  836. kthread_stop(sched->thread);
  837. }
  838. EXPORT_SYMBOL(drm_sched_fini);