gpu_scheduler.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /*
  2. * Copyright 2015 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. *
  23. */
  24. #include <linux/kthread.h>
  25. #include <linux/wait.h>
  26. #include <linux/sched.h>
  27. #include <drm/drmP.h>
  28. #include "gpu_scheduler.h"
  29. #define CREATE_TRACE_POINTS
  30. #include "gpu_sched_trace.h"
  31. static bool amd_sched_entity_is_ready(struct amd_sched_entity *entity);
  32. static void amd_sched_wakeup(struct amd_gpu_scheduler *sched);
  33. struct kmem_cache *sched_fence_slab;
  34. atomic_t sched_fence_slab_ref = ATOMIC_INIT(0);
  35. /* Initialize a given run queue struct */
  36. static void amd_sched_rq_init(struct amd_sched_rq *rq)
  37. {
  38. spin_lock_init(&rq->lock);
  39. INIT_LIST_HEAD(&rq->entities);
  40. rq->current_entity = NULL;
  41. }
  42. static void amd_sched_rq_add_entity(struct amd_sched_rq *rq,
  43. struct amd_sched_entity *entity)
  44. {
  45. if (!list_empty(&entity->list))
  46. return;
  47. spin_lock(&rq->lock);
  48. list_add_tail(&entity->list, &rq->entities);
  49. spin_unlock(&rq->lock);
  50. }
  51. static void amd_sched_rq_remove_entity(struct amd_sched_rq *rq,
  52. struct amd_sched_entity *entity)
  53. {
  54. if (list_empty(&entity->list))
  55. return;
  56. spin_lock(&rq->lock);
  57. list_del_init(&entity->list);
  58. if (rq->current_entity == entity)
  59. rq->current_entity = NULL;
  60. spin_unlock(&rq->lock);
  61. }
  62. /**
  63. * Select an entity which could provide a job to run
  64. *
  65. * @rq The run queue to check.
  66. *
  67. * Try to find a ready entity, returns NULL if none found.
  68. */
  69. static struct amd_sched_entity *
  70. amd_sched_rq_select_entity(struct amd_sched_rq *rq)
  71. {
  72. struct amd_sched_entity *entity;
  73. spin_lock(&rq->lock);
  74. entity = rq->current_entity;
  75. if (entity) {
  76. list_for_each_entry_continue(entity, &rq->entities, list) {
  77. if (amd_sched_entity_is_ready(entity)) {
  78. rq->current_entity = entity;
  79. spin_unlock(&rq->lock);
  80. return entity;
  81. }
  82. }
  83. }
  84. list_for_each_entry(entity, &rq->entities, list) {
  85. if (amd_sched_entity_is_ready(entity)) {
  86. rq->current_entity = entity;
  87. spin_unlock(&rq->lock);
  88. return entity;
  89. }
  90. if (entity == rq->current_entity)
  91. break;
  92. }
  93. spin_unlock(&rq->lock);
  94. return NULL;
  95. }
  96. /**
  97. * Init a context entity used by scheduler when submit to HW ring.
  98. *
  99. * @sched The pointer to the scheduler
  100. * @entity The pointer to a valid amd_sched_entity
  101. * @rq The run queue this entity belongs
  102. * @kernel If this is an entity for the kernel
  103. * @jobs The max number of jobs in the job queue
  104. *
  105. * return 0 if succeed. negative error code on failure
  106. */
  107. int amd_sched_entity_init(struct amd_gpu_scheduler *sched,
  108. struct amd_sched_entity *entity,
  109. struct amd_sched_rq *rq,
  110. uint32_t jobs)
  111. {
  112. int r;
  113. if (!(sched && entity && rq))
  114. return -EINVAL;
  115. memset(entity, 0, sizeof(struct amd_sched_entity));
  116. INIT_LIST_HEAD(&entity->list);
  117. entity->rq = rq;
  118. entity->sched = sched;
  119. spin_lock_init(&entity->queue_lock);
  120. r = kfifo_alloc(&entity->job_queue, jobs * sizeof(void *), GFP_KERNEL);
  121. if (r)
  122. return r;
  123. atomic_set(&entity->fence_seq, 0);
  124. entity->fence_context = fence_context_alloc(1);
  125. return 0;
  126. }
  127. /**
  128. * Query if entity is initialized
  129. *
  130. * @sched Pointer to scheduler instance
  131. * @entity The pointer to a valid scheduler entity
  132. *
  133. * return true if entity is initialized, false otherwise
  134. */
  135. static bool amd_sched_entity_is_initialized(struct amd_gpu_scheduler *sched,
  136. struct amd_sched_entity *entity)
  137. {
  138. return entity->sched == sched &&
  139. entity->rq != NULL;
  140. }
  141. /**
  142. * Check if entity is idle
  143. *
  144. * @entity The pointer to a valid scheduler entity
  145. *
  146. * Return true if entity don't has any unscheduled jobs.
  147. */
  148. static bool amd_sched_entity_is_idle(struct amd_sched_entity *entity)
  149. {
  150. rmb();
  151. if (kfifo_is_empty(&entity->job_queue))
  152. return true;
  153. return false;
  154. }
  155. /**
  156. * Check if entity is ready
  157. *
  158. * @entity The pointer to a valid scheduler entity
  159. *
  160. * Return true if entity could provide a job.
  161. */
  162. static bool amd_sched_entity_is_ready(struct amd_sched_entity *entity)
  163. {
  164. if (kfifo_is_empty(&entity->job_queue))
  165. return false;
  166. if (ACCESS_ONCE(entity->dependency))
  167. return false;
  168. return true;
  169. }
  170. /**
  171. * Destroy a context entity
  172. *
  173. * @sched Pointer to scheduler instance
  174. * @entity The pointer to a valid scheduler entity
  175. *
  176. * Cleanup and free the allocated resources.
  177. */
  178. void amd_sched_entity_fini(struct amd_gpu_scheduler *sched,
  179. struct amd_sched_entity *entity)
  180. {
  181. struct amd_sched_rq *rq = entity->rq;
  182. if (!amd_sched_entity_is_initialized(sched, entity))
  183. return;
  184. /**
  185. * The client will not queue more IBs during this fini, consume existing
  186. * queued IBs
  187. */
  188. wait_event(sched->job_scheduled, amd_sched_entity_is_idle(entity));
  189. amd_sched_rq_remove_entity(rq, entity);
  190. kfifo_free(&entity->job_queue);
  191. }
  192. static void amd_sched_entity_wakeup(struct fence *f, struct fence_cb *cb)
  193. {
  194. struct amd_sched_entity *entity =
  195. container_of(cb, struct amd_sched_entity, cb);
  196. entity->dependency = NULL;
  197. fence_put(f);
  198. amd_sched_wakeup(entity->sched);
  199. }
  200. static bool amd_sched_entity_add_dependency_cb(struct amd_sched_entity *entity)
  201. {
  202. struct amd_gpu_scheduler *sched = entity->sched;
  203. struct fence * fence = entity->dependency;
  204. struct amd_sched_fence *s_fence;
  205. if (fence->context == entity->fence_context) {
  206. /* We can ignore fences from ourself */
  207. fence_put(entity->dependency);
  208. return false;
  209. }
  210. s_fence = to_amd_sched_fence(fence);
  211. if (s_fence && s_fence->sched == sched) {
  212. /* Fence is from the same scheduler */
  213. if (test_bit(AMD_SCHED_FENCE_SCHEDULED_BIT, &fence->flags)) {
  214. /* Ignore it when it is already scheduled */
  215. fence_put(entity->dependency);
  216. return false;
  217. }
  218. /* Wait for fence to be scheduled */
  219. entity->cb.func = amd_sched_entity_wakeup;
  220. list_add_tail(&entity->cb.node, &s_fence->scheduled_cb);
  221. return true;
  222. }
  223. if (!fence_add_callback(entity->dependency, &entity->cb,
  224. amd_sched_entity_wakeup))
  225. return true;
  226. fence_put(entity->dependency);
  227. return false;
  228. }
  229. static struct amd_sched_job *
  230. amd_sched_entity_pop_job(struct amd_sched_entity *entity)
  231. {
  232. struct amd_gpu_scheduler *sched = entity->sched;
  233. struct amd_sched_job *sched_job;
  234. if (!kfifo_out_peek(&entity->job_queue, &sched_job, sizeof(sched_job)))
  235. return NULL;
  236. while ((entity->dependency = sched->ops->dependency(sched_job)))
  237. if (amd_sched_entity_add_dependency_cb(entity))
  238. return NULL;
  239. return sched_job;
  240. }
  241. /**
  242. * Helper to submit a job to the job queue
  243. *
  244. * @sched_job The pointer to job required to submit
  245. *
  246. * Returns true if we could submit the job.
  247. */
  248. static bool amd_sched_entity_in(struct amd_sched_job *sched_job)
  249. {
  250. struct amd_gpu_scheduler *sched = sched_job->sched;
  251. struct amd_sched_entity *entity = sched_job->s_entity;
  252. bool added, first = false;
  253. spin_lock(&entity->queue_lock);
  254. added = kfifo_in(&entity->job_queue, &sched_job,
  255. sizeof(sched_job)) == sizeof(sched_job);
  256. if (added && kfifo_len(&entity->job_queue) == sizeof(sched_job))
  257. first = true;
  258. spin_unlock(&entity->queue_lock);
  259. /* first job wakes up scheduler */
  260. if (first) {
  261. /* Add the entity to the run queue */
  262. amd_sched_rq_add_entity(entity->rq, entity);
  263. amd_sched_wakeup(sched);
  264. }
  265. return added;
  266. }
  267. /**
  268. * Submit a job to the job queue
  269. *
  270. * @sched_job The pointer to job required to submit
  271. *
  272. * Returns 0 for success, negative error code otherwise.
  273. */
  274. void amd_sched_entity_push_job(struct amd_sched_job *sched_job)
  275. {
  276. struct amd_sched_entity *entity = sched_job->s_entity;
  277. trace_amd_sched_job(sched_job);
  278. wait_event(entity->sched->job_scheduled,
  279. amd_sched_entity_in(sched_job));
  280. }
  281. /**
  282. * Return ture if we can push more jobs to the hw.
  283. */
  284. static bool amd_sched_ready(struct amd_gpu_scheduler *sched)
  285. {
  286. return atomic_read(&sched->hw_rq_count) <
  287. sched->hw_submission_limit;
  288. }
  289. /**
  290. * Wake up the scheduler when it is ready
  291. */
  292. static void amd_sched_wakeup(struct amd_gpu_scheduler *sched)
  293. {
  294. if (amd_sched_ready(sched))
  295. wake_up_interruptible(&sched->wake_up_worker);
  296. }
  297. /**
  298. * Select next entity to process
  299. */
  300. static struct amd_sched_entity *
  301. amd_sched_select_entity(struct amd_gpu_scheduler *sched)
  302. {
  303. struct amd_sched_entity *entity;
  304. int i;
  305. if (!amd_sched_ready(sched))
  306. return NULL;
  307. /* Kernel run queue has higher priority than normal run queue*/
  308. for (i = 0; i < AMD_SCHED_MAX_PRIORITY; i++) {
  309. entity = amd_sched_rq_select_entity(&sched->sched_rq[i]);
  310. if (entity)
  311. break;
  312. }
  313. return entity;
  314. }
  315. static void amd_sched_process_job(struct fence *f, struct fence_cb *cb)
  316. {
  317. struct amd_sched_fence *s_fence =
  318. container_of(cb, struct amd_sched_fence, cb);
  319. struct amd_gpu_scheduler *sched = s_fence->sched;
  320. unsigned long flags;
  321. atomic_dec(&sched->hw_rq_count);
  322. amd_sched_fence_signal(s_fence);
  323. if (sched->timeout != MAX_SCHEDULE_TIMEOUT) {
  324. cancel_delayed_work(&s_fence->dwork);
  325. spin_lock_irqsave(&sched->fence_list_lock, flags);
  326. list_del_init(&s_fence->list);
  327. spin_unlock_irqrestore(&sched->fence_list_lock, flags);
  328. }
  329. trace_amd_sched_process_job(s_fence);
  330. fence_put(&s_fence->base);
  331. wake_up_interruptible(&sched->wake_up_worker);
  332. }
  333. static void amd_sched_fence_work_func(struct work_struct *work)
  334. {
  335. struct amd_sched_fence *s_fence =
  336. container_of(work, struct amd_sched_fence, dwork.work);
  337. struct amd_gpu_scheduler *sched = s_fence->sched;
  338. struct amd_sched_fence *entity, *tmp;
  339. unsigned long flags;
  340. DRM_ERROR("[%s] scheduler is timeout!\n", sched->name);
  341. /* Clean all pending fences */
  342. spin_lock_irqsave(&sched->fence_list_lock, flags);
  343. list_for_each_entry_safe(entity, tmp, &sched->fence_list, list) {
  344. DRM_ERROR(" fence no %d\n", entity->base.seqno);
  345. cancel_delayed_work(&entity->dwork);
  346. list_del_init(&entity->list);
  347. fence_put(&entity->base);
  348. }
  349. spin_unlock_irqrestore(&sched->fence_list_lock, flags);
  350. }
  351. static int amd_sched_main(void *param)
  352. {
  353. struct sched_param sparam = {.sched_priority = 1};
  354. struct amd_gpu_scheduler *sched = (struct amd_gpu_scheduler *)param;
  355. int r, count;
  356. spin_lock_init(&sched->fence_list_lock);
  357. INIT_LIST_HEAD(&sched->fence_list);
  358. sched_setscheduler(current, SCHED_FIFO, &sparam);
  359. while (!kthread_should_stop()) {
  360. struct amd_sched_entity *entity;
  361. struct amd_sched_fence *s_fence;
  362. struct amd_sched_job *sched_job;
  363. struct fence *fence;
  364. unsigned long flags;
  365. wait_event_interruptible(sched->wake_up_worker,
  366. (entity = amd_sched_select_entity(sched)) ||
  367. kthread_should_stop());
  368. if (!entity)
  369. continue;
  370. sched_job = amd_sched_entity_pop_job(entity);
  371. if (!sched_job)
  372. continue;
  373. s_fence = sched_job->s_fence;
  374. if (sched->timeout != MAX_SCHEDULE_TIMEOUT) {
  375. INIT_DELAYED_WORK(&s_fence->dwork, amd_sched_fence_work_func);
  376. schedule_delayed_work(&s_fence->dwork, sched->timeout);
  377. spin_lock_irqsave(&sched->fence_list_lock, flags);
  378. list_add_tail(&s_fence->list, &sched->fence_list);
  379. spin_unlock_irqrestore(&sched->fence_list_lock, flags);
  380. }
  381. atomic_inc(&sched->hw_rq_count);
  382. fence = sched->ops->run_job(sched_job);
  383. amd_sched_fence_scheduled(s_fence);
  384. if (fence) {
  385. r = fence_add_callback(fence, &s_fence->cb,
  386. amd_sched_process_job);
  387. if (r == -ENOENT)
  388. amd_sched_process_job(fence, &s_fence->cb);
  389. else if (r)
  390. DRM_ERROR("fence add callback failed (%d)\n", r);
  391. fence_put(fence);
  392. } else {
  393. DRM_ERROR("Failed to run job!\n");
  394. amd_sched_process_job(NULL, &s_fence->cb);
  395. }
  396. count = kfifo_out(&entity->job_queue, &sched_job,
  397. sizeof(sched_job));
  398. WARN_ON(count != sizeof(sched_job));
  399. wake_up(&sched->job_scheduled);
  400. }
  401. return 0;
  402. }
  403. /**
  404. * Init a gpu scheduler instance
  405. *
  406. * @sched The pointer to the scheduler
  407. * @ops The backend operations for this scheduler.
  408. * @hw_submissions Number of hw submissions to do.
  409. * @name Name used for debugging
  410. *
  411. * Return 0 on success, otherwise error code.
  412. */
  413. int amd_sched_init(struct amd_gpu_scheduler *sched,
  414. struct amd_sched_backend_ops *ops,
  415. unsigned hw_submission, long timeout, const char *name)
  416. {
  417. int i;
  418. sched->ops = ops;
  419. sched->hw_submission_limit = hw_submission;
  420. sched->name = name;
  421. sched->timeout = timeout;
  422. for (i = 0; i < AMD_SCHED_MAX_PRIORITY; i++)
  423. amd_sched_rq_init(&sched->sched_rq[i]);
  424. init_waitqueue_head(&sched->wake_up_worker);
  425. init_waitqueue_head(&sched->job_scheduled);
  426. atomic_set(&sched->hw_rq_count, 0);
  427. if (atomic_inc_return(&sched_fence_slab_ref) == 1) {
  428. sched_fence_slab = kmem_cache_create(
  429. "amd_sched_fence", sizeof(struct amd_sched_fence), 0,
  430. SLAB_HWCACHE_ALIGN, NULL);
  431. if (!sched_fence_slab)
  432. return -ENOMEM;
  433. }
  434. /* Each scheduler will run on a seperate kernel thread */
  435. sched->thread = kthread_run(amd_sched_main, sched, sched->name);
  436. if (IS_ERR(sched->thread)) {
  437. DRM_ERROR("Failed to create scheduler for %s.\n", name);
  438. return PTR_ERR(sched->thread);
  439. }
  440. return 0;
  441. }
  442. /**
  443. * Destroy a gpu scheduler
  444. *
  445. * @sched The pointer to the scheduler
  446. */
  447. void amd_sched_fini(struct amd_gpu_scheduler *sched)
  448. {
  449. if (sched->thread)
  450. kthread_stop(sched->thread);
  451. if (atomic_dec_and_test(&sched_fence_slab_ref))
  452. kmem_cache_destroy(sched_fence_slab);
  453. }