gpu_scheduler.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. spin_lock(&rq->lock);
  46. list_add_tail(&entity->list, &rq->entities);
  47. spin_unlock(&rq->lock);
  48. }
  49. static void amd_sched_rq_remove_entity(struct amd_sched_rq *rq,
  50. struct amd_sched_entity *entity)
  51. {
  52. spin_lock(&rq->lock);
  53. list_del_init(&entity->list);
  54. if (rq->current_entity == entity)
  55. rq->current_entity = NULL;
  56. spin_unlock(&rq->lock);
  57. }
  58. /**
  59. * Select an entity which could provide a job to run
  60. *
  61. * @rq The run queue to check.
  62. *
  63. * Try to find a ready entity, returns NULL if none found.
  64. */
  65. static struct amd_sched_entity *
  66. amd_sched_rq_select_entity(struct amd_sched_rq *rq)
  67. {
  68. struct amd_sched_entity *entity;
  69. spin_lock(&rq->lock);
  70. entity = rq->current_entity;
  71. if (entity) {
  72. list_for_each_entry_continue(entity, &rq->entities, list) {
  73. if (amd_sched_entity_is_ready(entity)) {
  74. rq->current_entity = entity;
  75. spin_unlock(&rq->lock);
  76. return entity;
  77. }
  78. }
  79. }
  80. list_for_each_entry(entity, &rq->entities, list) {
  81. if (amd_sched_entity_is_ready(entity)) {
  82. rq->current_entity = entity;
  83. spin_unlock(&rq->lock);
  84. return entity;
  85. }
  86. if (entity == rq->current_entity)
  87. break;
  88. }
  89. spin_unlock(&rq->lock);
  90. return NULL;
  91. }
  92. /**
  93. * Init a context entity used by scheduler when submit to HW ring.
  94. *
  95. * @sched The pointer to the scheduler
  96. * @entity The pointer to a valid amd_sched_entity
  97. * @rq The run queue this entity belongs
  98. * @kernel If this is an entity for the kernel
  99. * @jobs The max number of jobs in the job queue
  100. *
  101. * return 0 if succeed. negative error code on failure
  102. */
  103. int amd_sched_entity_init(struct amd_gpu_scheduler *sched,
  104. struct amd_sched_entity *entity,
  105. struct amd_sched_rq *rq,
  106. uint32_t jobs)
  107. {
  108. int r;
  109. if (!(sched && entity && rq))
  110. return -EINVAL;
  111. memset(entity, 0, sizeof(struct amd_sched_entity));
  112. INIT_LIST_HEAD(&entity->list);
  113. entity->rq = rq;
  114. entity->sched = sched;
  115. spin_lock_init(&entity->queue_lock);
  116. r = kfifo_alloc(&entity->job_queue, jobs * sizeof(void *), GFP_KERNEL);
  117. if (r)
  118. return r;
  119. atomic_set(&entity->fence_seq, 0);
  120. entity->fence_context = fence_context_alloc(1);
  121. /* Add the entity to the run queue */
  122. amd_sched_rq_add_entity(rq, entity);
  123. return 0;
  124. }
  125. /**
  126. * Query if entity is initialized
  127. *
  128. * @sched Pointer to scheduler instance
  129. * @entity The pointer to a valid scheduler entity
  130. *
  131. * return true if entity is initialized, false otherwise
  132. */
  133. static bool amd_sched_entity_is_initialized(struct amd_gpu_scheduler *sched,
  134. struct amd_sched_entity *entity)
  135. {
  136. return entity->sched == sched &&
  137. entity->rq != NULL;
  138. }
  139. /**
  140. * Check if entity is idle
  141. *
  142. * @entity The pointer to a valid scheduler entity
  143. *
  144. * Return true if entity don't has any unscheduled jobs.
  145. */
  146. static bool amd_sched_entity_is_idle(struct amd_sched_entity *entity)
  147. {
  148. rmb();
  149. if (kfifo_is_empty(&entity->job_queue))
  150. return true;
  151. return false;
  152. }
  153. /**
  154. * Check if entity is ready
  155. *
  156. * @entity The pointer to a valid scheduler entity
  157. *
  158. * Return true if entity could provide a job.
  159. */
  160. static bool amd_sched_entity_is_ready(struct amd_sched_entity *entity)
  161. {
  162. if (kfifo_is_empty(&entity->job_queue))
  163. return false;
  164. if (ACCESS_ONCE(entity->dependency))
  165. return false;
  166. return true;
  167. }
  168. /**
  169. * Destroy a context entity
  170. *
  171. * @sched Pointer to scheduler instance
  172. * @entity The pointer to a valid scheduler entity
  173. *
  174. * Cleanup and free the allocated resources.
  175. */
  176. void amd_sched_entity_fini(struct amd_gpu_scheduler *sched,
  177. struct amd_sched_entity *entity)
  178. {
  179. struct amd_sched_rq *rq = entity->rq;
  180. if (!amd_sched_entity_is_initialized(sched, entity))
  181. return;
  182. /**
  183. * The client will not queue more IBs during this fini, consume existing
  184. * queued IBs
  185. */
  186. wait_event(sched->job_scheduled, amd_sched_entity_is_idle(entity));
  187. amd_sched_rq_remove_entity(rq, entity);
  188. kfifo_free(&entity->job_queue);
  189. }
  190. static void amd_sched_entity_wakeup(struct fence *f, struct fence_cb *cb)
  191. {
  192. struct amd_sched_entity *entity =
  193. container_of(cb, struct amd_sched_entity, cb);
  194. entity->dependency = NULL;
  195. fence_put(f);
  196. amd_sched_wakeup(entity->sched);
  197. }
  198. static bool amd_sched_entity_add_dependency_cb(struct amd_sched_entity *entity)
  199. {
  200. struct amd_gpu_scheduler *sched = entity->sched;
  201. struct fence * fence = entity->dependency;
  202. struct amd_sched_fence *s_fence;
  203. if (fence->context == entity->fence_context) {
  204. /* We can ignore fences from ourself */
  205. fence_put(entity->dependency);
  206. return false;
  207. }
  208. s_fence = to_amd_sched_fence(fence);
  209. if (s_fence && s_fence->sched == sched) {
  210. /* Fence is from the same scheduler */
  211. if (test_bit(AMD_SCHED_FENCE_SCHEDULED_BIT, &fence->flags)) {
  212. /* Ignore it when it is already scheduled */
  213. fence_put(entity->dependency);
  214. return false;
  215. }
  216. /* Wait for fence to be scheduled */
  217. entity->cb.func = amd_sched_entity_wakeup;
  218. list_add_tail(&entity->cb.node, &s_fence->scheduled_cb);
  219. return true;
  220. }
  221. if (!fence_add_callback(entity->dependency, &entity->cb,
  222. amd_sched_entity_wakeup))
  223. return true;
  224. fence_put(entity->dependency);
  225. return false;
  226. }
  227. static struct amd_sched_job *
  228. amd_sched_entity_pop_job(struct amd_sched_entity *entity)
  229. {
  230. struct amd_gpu_scheduler *sched = entity->sched;
  231. struct amd_sched_job *sched_job;
  232. if (!kfifo_out_peek(&entity->job_queue, &sched_job, sizeof(sched_job)))
  233. return NULL;
  234. while ((entity->dependency = sched->ops->dependency(sched_job)))
  235. if (amd_sched_entity_add_dependency_cb(entity))
  236. return NULL;
  237. return sched_job;
  238. }
  239. /**
  240. * Helper to submit a job to the job queue
  241. *
  242. * @sched_job The pointer to job required to submit
  243. *
  244. * Returns true if we could submit the job.
  245. */
  246. static bool amd_sched_entity_in(struct amd_sched_job *sched_job)
  247. {
  248. struct amd_gpu_scheduler *sched = sched_job->sched;
  249. struct amd_sched_entity *entity = sched_job->s_entity;
  250. bool added, first = false;
  251. spin_lock(&entity->queue_lock);
  252. added = kfifo_in(&entity->job_queue, &sched_job,
  253. sizeof(sched_job)) == sizeof(sched_job);
  254. if (added && kfifo_len(&entity->job_queue) == sizeof(sched_job))
  255. first = true;
  256. spin_unlock(&entity->queue_lock);
  257. /* first job wakes up scheduler */
  258. if (first)
  259. amd_sched_wakeup(sched);
  260. return added;
  261. }
  262. /**
  263. * Submit a job to the job queue
  264. *
  265. * @sched_job The pointer to job required to submit
  266. *
  267. * Returns 0 for success, negative error code otherwise.
  268. */
  269. void amd_sched_entity_push_job(struct amd_sched_job *sched_job)
  270. {
  271. struct amd_sched_entity *entity = sched_job->s_entity;
  272. trace_amd_sched_job(sched_job);
  273. wait_event(entity->sched->job_scheduled,
  274. amd_sched_entity_in(sched_job));
  275. }
  276. /**
  277. * Return ture if we can push more jobs to the hw.
  278. */
  279. static bool amd_sched_ready(struct amd_gpu_scheduler *sched)
  280. {
  281. return atomic_read(&sched->hw_rq_count) <
  282. sched->hw_submission_limit;
  283. }
  284. /**
  285. * Wake up the scheduler when it is ready
  286. */
  287. static void amd_sched_wakeup(struct amd_gpu_scheduler *sched)
  288. {
  289. if (amd_sched_ready(sched))
  290. wake_up_interruptible(&sched->wake_up_worker);
  291. }
  292. /**
  293. * Select next entity to process
  294. */
  295. static struct amd_sched_entity *
  296. amd_sched_select_entity(struct amd_gpu_scheduler *sched)
  297. {
  298. struct amd_sched_entity *entity;
  299. if (!amd_sched_ready(sched))
  300. return NULL;
  301. /* Kernel run queue has higher priority than normal run queue*/
  302. entity = amd_sched_rq_select_entity(&sched->kernel_rq);
  303. if (entity == NULL)
  304. entity = amd_sched_rq_select_entity(&sched->sched_rq);
  305. return entity;
  306. }
  307. static void amd_sched_process_job(struct fence *f, struct fence_cb *cb)
  308. {
  309. struct amd_sched_fence *s_fence =
  310. container_of(cb, struct amd_sched_fence, cb);
  311. struct amd_gpu_scheduler *sched = s_fence->sched;
  312. unsigned long flags;
  313. atomic_dec(&sched->hw_rq_count);
  314. amd_sched_fence_signal(s_fence);
  315. if (sched->timeout != MAX_SCHEDULE_TIMEOUT) {
  316. cancel_delayed_work(&s_fence->dwork);
  317. spin_lock_irqsave(&sched->fence_list_lock, flags);
  318. list_del_init(&s_fence->list);
  319. spin_unlock_irqrestore(&sched->fence_list_lock, flags);
  320. }
  321. trace_amd_sched_process_job(s_fence);
  322. fence_put(&s_fence->base);
  323. wake_up_interruptible(&sched->wake_up_worker);
  324. }
  325. static void amd_sched_fence_work_func(struct work_struct *work)
  326. {
  327. struct amd_sched_fence *s_fence =
  328. container_of(work, struct amd_sched_fence, dwork.work);
  329. struct amd_gpu_scheduler *sched = s_fence->sched;
  330. struct amd_sched_fence *entity, *tmp;
  331. unsigned long flags;
  332. DRM_ERROR("[%s] scheduler is timeout!\n", sched->name);
  333. /* Clean all pending fences */
  334. spin_lock_irqsave(&sched->fence_list_lock, flags);
  335. list_for_each_entry_safe(entity, tmp, &sched->fence_list, list) {
  336. DRM_ERROR(" fence no %d\n", entity->base.seqno);
  337. cancel_delayed_work(&entity->dwork);
  338. list_del_init(&entity->list);
  339. fence_put(&entity->base);
  340. }
  341. spin_unlock_irqrestore(&sched->fence_list_lock, flags);
  342. }
  343. static int amd_sched_main(void *param)
  344. {
  345. struct sched_param sparam = {.sched_priority = 1};
  346. struct amd_gpu_scheduler *sched = (struct amd_gpu_scheduler *)param;
  347. int r, count;
  348. spin_lock_init(&sched->fence_list_lock);
  349. INIT_LIST_HEAD(&sched->fence_list);
  350. sched_setscheduler(current, SCHED_FIFO, &sparam);
  351. while (!kthread_should_stop()) {
  352. struct amd_sched_entity *entity;
  353. struct amd_sched_fence *s_fence;
  354. struct amd_sched_job *sched_job;
  355. struct fence *fence;
  356. unsigned long flags;
  357. wait_event_interruptible(sched->wake_up_worker,
  358. (entity = amd_sched_select_entity(sched)) ||
  359. kthread_should_stop());
  360. if (!entity)
  361. continue;
  362. sched_job = amd_sched_entity_pop_job(entity);
  363. if (!sched_job)
  364. continue;
  365. s_fence = sched_job->s_fence;
  366. if (sched->timeout != MAX_SCHEDULE_TIMEOUT) {
  367. INIT_DELAYED_WORK(&s_fence->dwork, amd_sched_fence_work_func);
  368. schedule_delayed_work(&s_fence->dwork, sched->timeout);
  369. spin_lock_irqsave(&sched->fence_list_lock, flags);
  370. list_add_tail(&s_fence->list, &sched->fence_list);
  371. spin_unlock_irqrestore(&sched->fence_list_lock, flags);
  372. }
  373. atomic_inc(&sched->hw_rq_count);
  374. fence = sched->ops->run_job(sched_job);
  375. amd_sched_fence_scheduled(s_fence);
  376. if (fence) {
  377. r = fence_add_callback(fence, &s_fence->cb,
  378. amd_sched_process_job);
  379. if (r == -ENOENT)
  380. amd_sched_process_job(fence, &s_fence->cb);
  381. else if (r)
  382. DRM_ERROR("fence add callback failed (%d)\n", r);
  383. fence_put(fence);
  384. } else {
  385. DRM_ERROR("Failed to run job!\n");
  386. amd_sched_process_job(NULL, &s_fence->cb);
  387. }
  388. count = kfifo_out(&entity->job_queue, &sched_job,
  389. sizeof(sched_job));
  390. WARN_ON(count != sizeof(sched_job));
  391. wake_up(&sched->job_scheduled);
  392. }
  393. return 0;
  394. }
  395. /**
  396. * Init a gpu scheduler instance
  397. *
  398. * @sched The pointer to the scheduler
  399. * @ops The backend operations for this scheduler.
  400. * @hw_submissions Number of hw submissions to do.
  401. * @name Name used for debugging
  402. *
  403. * Return 0 on success, otherwise error code.
  404. */
  405. int amd_sched_init(struct amd_gpu_scheduler *sched,
  406. struct amd_sched_backend_ops *ops,
  407. unsigned hw_submission, long timeout, const char *name)
  408. {
  409. sched->ops = ops;
  410. sched->hw_submission_limit = hw_submission;
  411. sched->name = name;
  412. sched->timeout = timeout;
  413. amd_sched_rq_init(&sched->sched_rq);
  414. amd_sched_rq_init(&sched->kernel_rq);
  415. init_waitqueue_head(&sched->wake_up_worker);
  416. init_waitqueue_head(&sched->job_scheduled);
  417. atomic_set(&sched->hw_rq_count, 0);
  418. if (atomic_inc_return(&sched_fence_slab_ref) == 1) {
  419. sched_fence_slab = kmem_cache_create(
  420. "amd_sched_fence", sizeof(struct amd_sched_fence), 0,
  421. SLAB_HWCACHE_ALIGN, NULL);
  422. if (!sched_fence_slab)
  423. return -ENOMEM;
  424. }
  425. /* Each scheduler will run on a seperate kernel thread */
  426. sched->thread = kthread_run(amd_sched_main, sched, sched->name);
  427. if (IS_ERR(sched->thread)) {
  428. DRM_ERROR("Failed to create scheduler for %s.\n", name);
  429. return PTR_ERR(sched->thread);
  430. }
  431. return 0;
  432. }
  433. /**
  434. * Destroy a gpu scheduler
  435. *
  436. * @sched The pointer to the scheduler
  437. */
  438. void amd_sched_fini(struct amd_gpu_scheduler *sched)
  439. {
  440. if (sched->thread)
  441. kthread_stop(sched->thread);
  442. if (atomic_dec_and_test(&sched_fence_slab_ref))
  443. kmem_cache_destroy(sched_fence_slab);
  444. }