sched_entity.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. #include <linux/kthread.h>
  24. #include <linux/slab.h>
  25. #include <linux/completion.h>
  26. #include <drm/drm_print.h>
  27. #include <drm/gpu_scheduler.h>
  28. #include "gpu_scheduler_trace.h"
  29. #define to_drm_sched_job(sched_job) \
  30. container_of((sched_job), struct drm_sched_job, queue_node)
  31. /**
  32. * drm_sched_entity_init - Init a context entity used by scheduler when
  33. * submit to HW ring.
  34. *
  35. * @entity: scheduler entity to init
  36. * @priority: priority of the entity
  37. * @sched_list: the list of drm scheds on which jobs from this
  38. * entity can be submitted
  39. * @num_sched_list: number of drm sched in sched_list
  40. * @guilty: atomic_t set to 1 when a job on this queue
  41. * is found to be guilty causing a timeout
  42. *
  43. * Note that the &sched_list must have at least one element to schedule the entity.
  44. *
  45. * For changing @priority later on at runtime see
  46. * drm_sched_entity_set_priority(). For changing the set of schedulers
  47. * @sched_list at runtime see drm_sched_entity_modify_sched().
  48. *
  49. * An entity is cleaned up by callind drm_sched_entity_fini(). See also
  50. * drm_sched_entity_destroy().
  51. *
  52. * Returns 0 on success or a negative error code on failure.
  53. */
  54. int drm_sched_entity_init(struct drm_sched_entity *entity,
  55. enum drm_sched_priority priority,
  56. struct drm_gpu_scheduler **sched_list,
  57. unsigned int num_sched_list,
  58. atomic_t *guilty)
  59. {
  60. if (!(entity && sched_list && (num_sched_list == 0 || sched_list[0])))
  61. return -EINVAL;
  62. memset(entity, 0, sizeof(struct drm_sched_entity));
  63. INIT_LIST_HEAD(&entity->list);
  64. entity->rq = NULL;
  65. entity->guilty = guilty;
  66. entity->num_sched_list = num_sched_list;
  67. entity->priority = priority;
  68. entity->sched_list = num_sched_list > 1 ? sched_list : NULL;
  69. entity->last_scheduled = NULL;
  70. if(num_sched_list)
  71. entity->rq = &sched_list[0]->sched_rq[entity->priority];
  72. init_completion(&entity->entity_idle);
  73. /* We start in an idle state. */
  74. complete(&entity->entity_idle);
  75. spin_lock_init(&entity->rq_lock);
  76. spsc_queue_init(&entity->job_queue);
  77. atomic_set(&entity->fence_seq, 0);
  78. entity->fence_context = dma_fence_context_alloc(2);
  79. return 0;
  80. }
  81. EXPORT_SYMBOL(drm_sched_entity_init);
  82. /**
  83. * drm_sched_entity_modify_sched - Modify sched of an entity
  84. * @entity: scheduler entity to init
  85. * @sched_list: the list of new drm scheds which will replace
  86. * existing entity->sched_list
  87. * @num_sched_list: number of drm sched in sched_list
  88. *
  89. * Note that this must be called under the same common lock for @entity as
  90. * drm_sched_job_arm() and drm_sched_entity_push_job(), or the driver needs to
  91. * guarantee through some other means that this is never called while new jobs
  92. * can be pushed to @entity.
  93. */
  94. void drm_sched_entity_modify_sched(struct drm_sched_entity *entity,
  95. struct drm_gpu_scheduler **sched_list,
  96. unsigned int num_sched_list)
  97. {
  98. WARN_ON(!num_sched_list || !sched_list);
  99. entity->sched_list = sched_list;
  100. entity->num_sched_list = num_sched_list;
  101. }
  102. EXPORT_SYMBOL(drm_sched_entity_modify_sched);
  103. static bool drm_sched_entity_is_idle(struct drm_sched_entity *entity)
  104. {
  105. rmb(); /* for list_empty to work without lock */
  106. if (list_empty(&entity->list) ||
  107. spsc_queue_count(&entity->job_queue) == 0 ||
  108. entity->stopped)
  109. return true;
  110. return false;
  111. }
  112. /* Return true if entity could provide a job. */
  113. bool drm_sched_entity_is_ready(struct drm_sched_entity *entity)
  114. {
  115. if (spsc_queue_peek(&entity->job_queue) == NULL)
  116. return false;
  117. if (READ_ONCE(entity->dependency))
  118. return false;
  119. return true;
  120. }
  121. /**
  122. * drm_sched_entity_flush - Flush a context entity
  123. *
  124. * @entity: scheduler entity
  125. * @timeout: time to wait in for Q to become empty in jiffies.
  126. *
  127. * Splitting drm_sched_entity_fini() into two functions, The first one does the
  128. * waiting, removes the entity from the runqueue and returns an error when the
  129. * process was killed.
  130. *
  131. * Returns the remaining time in jiffies left from the input timeout
  132. */
  133. long drm_sched_entity_flush(struct drm_sched_entity *entity, long timeout)
  134. {
  135. struct drm_gpu_scheduler *sched;
  136. struct task_struct *last_user;
  137. long ret = timeout;
  138. if (!entity->rq)
  139. return 0;
  140. sched = entity->rq->sched;
  141. /**
  142. * The client will not queue more IBs during this fini, consume existing
  143. * queued IBs or discard them on SIGKILL
  144. */
  145. if (current->flags & PF_EXITING) {
  146. if (timeout)
  147. ret = wait_event_timeout(
  148. sched->job_scheduled,
  149. drm_sched_entity_is_idle(entity),
  150. timeout);
  151. } else {
  152. wait_event_killable(sched->job_scheduled,
  153. drm_sched_entity_is_idle(entity));
  154. }
  155. /* For killed process disable any more IBs enqueue right now */
  156. last_user = cmpxchg(&entity->last_user, current->group_leader, NULL);
  157. if ((!last_user || last_user == current->group_leader) &&
  158. (current->flags & PF_EXITING) && (current->exit_code == SIGKILL)) {
  159. spin_lock(&entity->rq_lock);
  160. entity->stopped = true;
  161. drm_sched_rq_remove_entity(entity->rq, entity);
  162. spin_unlock(&entity->rq_lock);
  163. }
  164. return ret;
  165. }
  166. EXPORT_SYMBOL(drm_sched_entity_flush);
  167. static void drm_sched_entity_kill_jobs_work(struct work_struct *wrk)
  168. {
  169. struct drm_sched_job *job = container_of(wrk, typeof(*job), work);
  170. drm_sched_fence_finished(job->s_fence);
  171. WARN_ON(job->s_fence->parent);
  172. job->sched->ops->free_job(job);
  173. }
  174. /* Signal the scheduler finished fence when the entity in question is killed. */
  175. static void drm_sched_entity_kill_jobs_cb(struct dma_fence *f,
  176. struct dma_fence_cb *cb)
  177. {
  178. struct drm_sched_job *job = container_of(cb, struct drm_sched_job,
  179. finish_cb);
  180. dma_fence_put(f);
  181. INIT_WORK(&job->work, drm_sched_entity_kill_jobs_work);
  182. schedule_work(&job->work);
  183. }
  184. static struct dma_fence *
  185. drm_sched_job_dependency(struct drm_sched_job *job,
  186. struct drm_sched_entity *entity)
  187. {
  188. if (!xa_empty(&job->dependencies))
  189. return xa_erase(&job->dependencies, job->last_dependency++);
  190. if (job->sched->ops->dependency)
  191. return job->sched->ops->dependency(job, entity);
  192. return NULL;
  193. }
  194. static void drm_sched_entity_kill_jobs(struct drm_sched_entity *entity)
  195. {
  196. struct drm_sched_job *job;
  197. struct dma_fence *f;
  198. int r;
  199. while ((job = to_drm_sched_job(spsc_queue_pop(&entity->job_queue)))) {
  200. struct drm_sched_fence *s_fence = job->s_fence;
  201. /* Wait for all dependencies to avoid data corruptions */
  202. while ((f = drm_sched_job_dependency(job, entity))) {
  203. dma_fence_wait(f, false);
  204. dma_fence_put(f);
  205. }
  206. drm_sched_fence_scheduled(s_fence);
  207. dma_fence_set_error(&s_fence->finished, -ESRCH);
  208. /*
  209. * When pipe is hanged by older entity, new entity might
  210. * not even have chance to submit it's first job to HW
  211. * and so entity->last_scheduled will remain NULL
  212. */
  213. if (!entity->last_scheduled) {
  214. drm_sched_entity_kill_jobs_cb(NULL, &job->finish_cb);
  215. continue;
  216. }
  217. dma_fence_get(entity->last_scheduled);
  218. r = dma_fence_add_callback(entity->last_scheduled,
  219. &job->finish_cb,
  220. drm_sched_entity_kill_jobs_cb);
  221. if (r == -ENOENT)
  222. drm_sched_entity_kill_jobs_cb(NULL, &job->finish_cb);
  223. else if (r)
  224. DRM_ERROR("fence add callback failed (%d)\n", r);
  225. }
  226. }
  227. /**
  228. * drm_sched_entity_fini - Destroy a context entity
  229. *
  230. * @entity: scheduler entity
  231. *
  232. * Cleanups up @entity which has been initialized by drm_sched_entity_init().
  233. *
  234. * If there are potentially job still in flight or getting newly queued
  235. * drm_sched_entity_flush() must be called first. This function then goes over
  236. * the entity and signals all jobs with an error code if the process was killed.
  237. */
  238. void drm_sched_entity_fini(struct drm_sched_entity *entity)
  239. {
  240. struct drm_gpu_scheduler *sched = NULL;
  241. if (entity->rq) {
  242. sched = entity->rq->sched;
  243. drm_sched_rq_remove_entity(entity->rq, entity);
  244. }
  245. /* Consumption of existing IBs wasn't completed. Forcefully
  246. * remove them here.
  247. */
  248. if (spsc_queue_count(&entity->job_queue)) {
  249. if (sched) {
  250. /*
  251. * Wait for thread to idle to make sure it isn't processing
  252. * this entity.
  253. */
  254. wait_for_completion(&entity->entity_idle);
  255. }
  256. if (entity->dependency) {
  257. dma_fence_remove_callback(entity->dependency,
  258. &entity->cb);
  259. dma_fence_put(entity->dependency);
  260. entity->dependency = NULL;
  261. }
  262. drm_sched_entity_kill_jobs(entity);
  263. }
  264. dma_fence_put(entity->last_scheduled);
  265. entity->last_scheduled = NULL;
  266. }
  267. EXPORT_SYMBOL(drm_sched_entity_fini);
  268. /**
  269. * drm_sched_entity_destroy - Destroy a context entity
  270. * @entity: scheduler entity
  271. *
  272. * Calls drm_sched_entity_flush() and drm_sched_entity_fini() as a
  273. * convenience wrapper.
  274. */
  275. void drm_sched_entity_destroy(struct drm_sched_entity *entity)
  276. {
  277. drm_sched_entity_flush(entity, MAX_WAIT_SCHED_ENTITY_Q_EMPTY);
  278. drm_sched_entity_fini(entity);
  279. }
  280. EXPORT_SYMBOL(drm_sched_entity_destroy);
  281. /* drm_sched_entity_clear_dep - callback to clear the entities dependency */
  282. static void drm_sched_entity_clear_dep(struct dma_fence *f,
  283. struct dma_fence_cb *cb)
  284. {
  285. struct drm_sched_entity *entity =
  286. container_of(cb, struct drm_sched_entity, cb);
  287. entity->dependency = NULL;
  288. dma_fence_put(f);
  289. }
  290. /*
  291. * drm_sched_entity_clear_dep - callback to clear the entities dependency and
  292. * wake up scheduler
  293. */
  294. static void drm_sched_entity_wakeup(struct dma_fence *f,
  295. struct dma_fence_cb *cb)
  296. {
  297. struct drm_sched_entity *entity =
  298. container_of(cb, struct drm_sched_entity, cb);
  299. drm_sched_entity_clear_dep(f, cb);
  300. drm_sched_wakeup(entity->rq->sched);
  301. }
  302. /**
  303. * drm_sched_entity_set_priority - Sets priority of the entity
  304. *
  305. * @entity: scheduler entity
  306. * @priority: scheduler priority
  307. *
  308. * Update the priority of runqueus used for the entity.
  309. */
  310. void drm_sched_entity_set_priority(struct drm_sched_entity *entity,
  311. enum drm_sched_priority priority)
  312. {
  313. spin_lock(&entity->rq_lock);
  314. entity->priority = priority;
  315. spin_unlock(&entity->rq_lock);
  316. }
  317. EXPORT_SYMBOL(drm_sched_entity_set_priority);
  318. /*
  319. * Add a callback to the current dependency of the entity to wake up the
  320. * scheduler when the entity becomes available.
  321. */
  322. static bool drm_sched_entity_add_dependency_cb(struct drm_sched_entity *entity)
  323. {
  324. struct drm_gpu_scheduler *sched = entity->rq->sched;
  325. struct dma_fence *fence = entity->dependency;
  326. struct drm_sched_fence *s_fence;
  327. if (fence->context == entity->fence_context ||
  328. fence->context == entity->fence_context + 1) {
  329. /*
  330. * Fence is a scheduled/finished fence from a job
  331. * which belongs to the same entity, we can ignore
  332. * fences from ourself
  333. */
  334. dma_fence_put(entity->dependency);
  335. return false;
  336. }
  337. s_fence = to_drm_sched_fence(fence);
  338. if (s_fence && s_fence->sched == sched &&
  339. !test_bit(DRM_SCHED_FENCE_DONT_PIPELINE, &fence->flags)) {
  340. /*
  341. * Fence is from the same scheduler, only need to wait for
  342. * it to be scheduled
  343. */
  344. fence = dma_fence_get(&s_fence->scheduled);
  345. dma_fence_put(entity->dependency);
  346. entity->dependency = fence;
  347. if (!dma_fence_add_callback(fence, &entity->cb,
  348. drm_sched_entity_clear_dep))
  349. return true;
  350. /* Ignore it when it is already scheduled */
  351. dma_fence_put(fence);
  352. return false;
  353. }
  354. if (!dma_fence_add_callback(entity->dependency, &entity->cb,
  355. drm_sched_entity_wakeup))
  356. return true;
  357. dma_fence_put(entity->dependency);
  358. return false;
  359. }
  360. struct drm_sched_job *drm_sched_entity_pop_job(struct drm_sched_entity *entity)
  361. {
  362. struct drm_sched_job *sched_job;
  363. sched_job = to_drm_sched_job(spsc_queue_peek(&entity->job_queue));
  364. if (!sched_job)
  365. return NULL;
  366. while ((entity->dependency =
  367. drm_sched_job_dependency(sched_job, entity))) {
  368. trace_drm_sched_job_wait_dep(sched_job, entity->dependency);
  369. if (drm_sched_entity_add_dependency_cb(entity))
  370. return NULL;
  371. }
  372. /* skip jobs from entity that marked guilty */
  373. if (entity->guilty && atomic_read(entity->guilty))
  374. dma_fence_set_error(&sched_job->s_fence->finished, -ECANCELED);
  375. dma_fence_put(entity->last_scheduled);
  376. entity->last_scheduled = dma_fence_get(&sched_job->s_fence->finished);
  377. /*
  378. * If the queue is empty we allow drm_sched_entity_select_rq() to
  379. * locklessly access ->last_scheduled. This only works if we set the
  380. * pointer before we dequeue and if we a write barrier here.
  381. */
  382. smp_wmb();
  383. spsc_queue_pop(&entity->job_queue);
  384. return sched_job;
  385. }
  386. void drm_sched_entity_select_rq(struct drm_sched_entity *entity)
  387. {
  388. struct dma_fence *fence;
  389. struct drm_gpu_scheduler *sched;
  390. struct drm_sched_rq *rq;
  391. /* single possible engine and already selected */
  392. if (!entity->sched_list)
  393. return;
  394. /* queue non-empty, stay on the same engine */
  395. if (spsc_queue_count(&entity->job_queue))
  396. return;
  397. /*
  398. * Only when the queue is empty are we guaranteed that the scheduler
  399. * thread cannot change ->last_scheduled. To enforce ordering we need
  400. * a read barrier here. See drm_sched_entity_pop_job() for the other
  401. * side.
  402. */
  403. smp_rmb();
  404. fence = entity->last_scheduled;
  405. /* stay on the same engine if the previous job hasn't finished */
  406. if (fence && !dma_fence_is_signaled(fence))
  407. return;
  408. spin_lock(&entity->rq_lock);
  409. sched = drm_sched_pick_best(entity->sched_list, entity->num_sched_list);
  410. rq = sched ? &sched->sched_rq[entity->priority] : NULL;
  411. if (rq != entity->rq) {
  412. drm_sched_rq_remove_entity(entity->rq, entity);
  413. entity->rq = rq;
  414. }
  415. spin_unlock(&entity->rq_lock);
  416. if (entity->num_sched_list == 1)
  417. entity->sched_list = NULL;
  418. }
  419. /**
  420. * drm_sched_entity_push_job - Submit a job to the entity's job queue
  421. * @sched_job: job to submit
  422. *
  423. * Note: To guarantee that the order of insertion to queue matches the job's
  424. * fence sequence number this function should be called with drm_sched_job_arm()
  425. * under common lock for the struct drm_sched_entity that was set up for
  426. * @sched_job in drm_sched_job_init().
  427. *
  428. * Returns 0 for success, negative error code otherwise.
  429. */
  430. void drm_sched_entity_push_job(struct drm_sched_job *sched_job)
  431. {
  432. struct drm_sched_entity *entity = sched_job->entity;
  433. bool first;
  434. trace_drm_sched_job(sched_job, entity);
  435. atomic_inc(entity->rq->sched->score);
  436. WRITE_ONCE(entity->last_user, current->group_leader);
  437. first = spsc_queue_push(&entity->job_queue, &sched_job->queue_node);
  438. /* first job wakes up scheduler */
  439. if (first) {
  440. /* Add the entity to the run queue */
  441. spin_lock(&entity->rq_lock);
  442. if (entity->stopped) {
  443. spin_unlock(&entity->rq_lock);
  444. DRM_ERROR("Trying to push to a killed entity\n");
  445. return;
  446. }
  447. drm_sched_rq_add_entity(entity->rq, entity);
  448. spin_unlock(&entity->rq_lock);
  449. drm_sched_wakeup(entity->rq->sched);
  450. }
  451. }
  452. EXPORT_SYMBOL(drm_sched_entity_push_job);