gpu_scheduler.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. /* Initialize a given run queue struct */
  30. static void amd_sched_rq_init(struct amd_sched_rq *rq)
  31. {
  32. INIT_LIST_HEAD(&rq->entities);
  33. mutex_init(&rq->lock);
  34. rq->current_entity = NULL;
  35. }
  36. static void amd_sched_rq_add_entity(struct amd_sched_rq *rq,
  37. struct amd_sched_entity *entity)
  38. {
  39. mutex_lock(&rq->lock);
  40. list_add_tail(&entity->list, &rq->entities);
  41. mutex_unlock(&rq->lock);
  42. }
  43. static void amd_sched_rq_remove_entity(struct amd_sched_rq *rq,
  44. struct amd_sched_entity *entity)
  45. {
  46. mutex_lock(&rq->lock);
  47. list_del_init(&entity->list);
  48. if (rq->current_entity == entity)
  49. rq->current_entity = NULL;
  50. mutex_unlock(&rq->lock);
  51. }
  52. /**
  53. * Select next entity from a specified run queue with round robin policy.
  54. * It could return the same entity as current one if current is the only
  55. * available one in the queue. Return NULL if nothing available.
  56. */
  57. static struct amd_sched_entity *
  58. amd_sched_rq_select_entity(struct amd_sched_rq *rq)
  59. {
  60. struct amd_sched_entity *entity = rq->current_entity;
  61. if (entity) {
  62. list_for_each_entry_continue(entity, &rq->entities, list) {
  63. if (!kfifo_is_empty(&entity->job_queue)) {
  64. rq->current_entity = entity;
  65. return rq->current_entity;
  66. }
  67. }
  68. }
  69. list_for_each_entry(entity, &rq->entities, list) {
  70. if (!kfifo_is_empty(&entity->job_queue)) {
  71. rq->current_entity = entity;
  72. return rq->current_entity;
  73. }
  74. if (entity == rq->current_entity)
  75. break;
  76. }
  77. return NULL;
  78. }
  79. /**
  80. * Note: This function should only been called inside scheduler main
  81. * function for thread safety, there is no other protection here.
  82. * return ture if scheduler has something ready to run.
  83. *
  84. * For active_hw_rq, there is only one producer(scheduler thread) and
  85. * one consumer(ISR). It should be safe to use this function in scheduler
  86. * main thread to decide whether to continue emit more IBs.
  87. */
  88. static bool is_scheduler_ready(struct amd_gpu_scheduler *sched)
  89. {
  90. unsigned long flags;
  91. bool full;
  92. spin_lock_irqsave(&sched->queue_lock, flags);
  93. full = atomic64_read(&sched->hw_rq_count) <
  94. sched->hw_submission_limit ? true : false;
  95. spin_unlock_irqrestore(&sched->queue_lock, flags);
  96. return full;
  97. }
  98. /**
  99. * Select next entity from the kernel run queue, if not available,
  100. * return null.
  101. */
  102. static struct amd_sched_entity *
  103. kernel_rq_select_context(struct amd_gpu_scheduler *sched)
  104. {
  105. struct amd_sched_entity *sched_entity;
  106. struct amd_sched_rq *rq = &sched->kernel_rq;
  107. mutex_lock(&rq->lock);
  108. sched_entity = amd_sched_rq_select_entity(rq);
  109. mutex_unlock(&rq->lock);
  110. return sched_entity;
  111. }
  112. /**
  113. * Select next entity containing real IB submissions
  114. */
  115. static struct amd_sched_entity *
  116. select_context(struct amd_gpu_scheduler *sched)
  117. {
  118. struct amd_sched_entity *wake_entity = NULL;
  119. struct amd_sched_entity *tmp;
  120. struct amd_sched_rq *rq;
  121. if (!is_scheduler_ready(sched))
  122. return NULL;
  123. /* Kernel run queue has higher priority than normal run queue*/
  124. tmp = kernel_rq_select_context(sched);
  125. if (tmp != NULL)
  126. goto exit;
  127. rq = &sched->sched_rq;
  128. mutex_lock(&rq->lock);
  129. tmp = amd_sched_rq_select_entity(rq);
  130. mutex_unlock(&rq->lock);
  131. exit:
  132. if (sched->current_entity && (sched->current_entity != tmp))
  133. wake_entity = sched->current_entity;
  134. sched->current_entity = tmp;
  135. if (wake_entity && wake_entity->need_wakeup)
  136. wake_up(&wake_entity->wait_queue);
  137. return tmp;
  138. }
  139. /**
  140. * Init a context entity used by scheduler when submit to HW ring.
  141. *
  142. * @sched The pointer to the scheduler
  143. * @entity The pointer to a valid amd_sched_entity
  144. * @rq The run queue this entity belongs
  145. * @kernel If this is an entity for the kernel
  146. * @jobs The max number of jobs in the job queue
  147. *
  148. * return 0 if succeed. negative error code on failure
  149. */
  150. int amd_sched_entity_init(struct amd_gpu_scheduler *sched,
  151. struct amd_sched_entity *entity,
  152. struct amd_sched_rq *rq,
  153. uint32_t jobs)
  154. {
  155. uint64_t seq_ring = 0;
  156. char name[20];
  157. if (!(sched && entity && rq))
  158. return -EINVAL;
  159. memset(entity, 0, sizeof(struct amd_sched_entity));
  160. seq_ring = ((uint64_t)sched->ring_id) << 60;
  161. spin_lock_init(&entity->lock);
  162. entity->belongto_rq = rq;
  163. entity->scheduler = sched;
  164. init_waitqueue_head(&entity->wait_queue);
  165. init_waitqueue_head(&entity->wait_emit);
  166. entity->fence_context = fence_context_alloc(1);
  167. snprintf(name, sizeof(name), "c_entity[%llu]", entity->fence_context);
  168. memcpy(entity->name, name, 20);
  169. entity->need_wakeup = false;
  170. if(kfifo_alloc(&entity->job_queue,
  171. jobs * sizeof(void *),
  172. GFP_KERNEL))
  173. return -EINVAL;
  174. spin_lock_init(&entity->queue_lock);
  175. atomic64_set(&entity->last_queued_v_seq, seq_ring);
  176. atomic64_set(&entity->last_signaled_v_seq, seq_ring);
  177. /* Add the entity to the run queue */
  178. amd_sched_rq_add_entity(rq, entity);
  179. return 0;
  180. }
  181. /**
  182. * Query if entity is initialized
  183. *
  184. * @sched Pointer to scheduler instance
  185. * @entity The pointer to a valid scheduler entity
  186. *
  187. * return true if entity is initialized, false otherwise
  188. */
  189. static bool is_context_entity_initialized(struct amd_gpu_scheduler *sched,
  190. struct amd_sched_entity *entity)
  191. {
  192. return entity->scheduler == sched &&
  193. entity->belongto_rq != NULL;
  194. }
  195. static bool is_context_entity_idle(struct amd_gpu_scheduler *sched,
  196. struct amd_sched_entity *entity)
  197. {
  198. /**
  199. * Idle means no pending IBs, and the entity is not
  200. * currently being used.
  201. */
  202. barrier();
  203. if ((sched->current_entity != entity) &&
  204. kfifo_is_empty(&entity->job_queue))
  205. return true;
  206. return false;
  207. }
  208. /**
  209. * Destroy a context entity
  210. *
  211. * @sched Pointer to scheduler instance
  212. * @entity The pointer to a valid scheduler entity
  213. *
  214. * return 0 if succeed. negative error code on failure
  215. */
  216. int amd_sched_entity_fini(struct amd_gpu_scheduler *sched,
  217. struct amd_sched_entity *entity)
  218. {
  219. int r = 0;
  220. struct amd_sched_rq *rq = entity->belongto_rq;
  221. if (!is_context_entity_initialized(sched, entity))
  222. return 0;
  223. entity->need_wakeup = true;
  224. /**
  225. * The client will not queue more IBs during this fini, consume existing
  226. * queued IBs
  227. */
  228. r = wait_event_timeout(
  229. entity->wait_queue,
  230. is_context_entity_idle(sched, entity),
  231. msecs_to_jiffies(AMD_GPU_WAIT_IDLE_TIMEOUT_IN_MS)
  232. ) ? 0 : -1;
  233. if (r) {
  234. if (entity->is_pending)
  235. DRM_INFO("Entity %p is in waiting state during fini,\
  236. all pending ibs will be canceled.\n",
  237. entity);
  238. }
  239. amd_sched_rq_remove_entity(rq, entity);
  240. kfifo_free(&entity->job_queue);
  241. return r;
  242. }
  243. /**
  244. * Submit a normal job to the job queue
  245. *
  246. * @sched The pointer to the scheduler
  247. * @c_entity The pointer to amd_sched_entity
  248. * @job The pointer to job required to submit
  249. * return 0 if succeed. -1 if failed.
  250. * -2 indicate queue is full for this client, client should wait untill
  251. * scheduler consum some queued command.
  252. * -1 other fail.
  253. */
  254. int amd_sched_push_job(struct amd_sched_job *sched_job)
  255. {
  256. struct amd_sched_fence *fence =
  257. amd_sched_fence_create(sched_job->s_entity);
  258. if (!fence)
  259. return -EINVAL;
  260. fence_get(&fence->base);
  261. sched_job->s_fence = fence;
  262. while (kfifo_in_spinlocked(&sched_job->s_entity->job_queue,
  263. &sched_job, sizeof(void *),
  264. &sched_job->s_entity->queue_lock) !=
  265. sizeof(void *)) {
  266. /**
  267. * Current context used up all its IB slots
  268. * wait here, or need to check whether GPU is hung
  269. */
  270. schedule();
  271. }
  272. /* first job wake up scheduler */
  273. if ((kfifo_len(&sched_job->s_entity->job_queue) / sizeof(void *)) == 1)
  274. wake_up_interruptible(&sched_job->sched->wait_queue);
  275. return 0;
  276. }
  277. static void amd_sched_process_job(struct fence *f, struct fence_cb *cb)
  278. {
  279. struct amd_sched_job *sched_job =
  280. container_of(cb, struct amd_sched_job, cb);
  281. struct amd_gpu_scheduler *sched;
  282. unsigned long flags;
  283. sched = sched_job->sched;
  284. atomic64_set(&sched_job->s_entity->last_signaled_v_seq,
  285. sched_job->s_fence->v_seq);
  286. amd_sched_fence_signal(sched_job->s_fence);
  287. spin_lock_irqsave(&sched->queue_lock, flags);
  288. list_del(&sched_job->list);
  289. atomic64_dec(&sched->hw_rq_count);
  290. spin_unlock_irqrestore(&sched->queue_lock, flags);
  291. fence_put(&sched_job->s_fence->base);
  292. sched->ops->process_job(sched, sched_job);
  293. wake_up_interruptible(&sched->wait_queue);
  294. }
  295. static int amd_sched_main(void *param)
  296. {
  297. int r;
  298. struct amd_sched_job *job;
  299. struct sched_param sparam = {.sched_priority = 1};
  300. struct amd_sched_entity *c_entity = NULL;
  301. struct amd_gpu_scheduler *sched = (struct amd_gpu_scheduler *)param;
  302. sched_setscheduler(current, SCHED_FIFO, &sparam);
  303. while (!kthread_should_stop()) {
  304. struct fence *fence;
  305. wait_event_interruptible(sched->wait_queue,
  306. is_scheduler_ready(sched) &&
  307. (c_entity = select_context(sched)));
  308. r = kfifo_out(&c_entity->job_queue, &job, sizeof(void *));
  309. if (r != sizeof(void *))
  310. continue;
  311. r = 0;
  312. if (sched->ops->prepare_job)
  313. r = sched->ops->prepare_job(sched, c_entity, job);
  314. if (!r) {
  315. unsigned long flags;
  316. spin_lock_irqsave(&sched->queue_lock, flags);
  317. list_add_tail(&job->list, &sched->active_hw_rq);
  318. atomic64_inc(&sched->hw_rq_count);
  319. spin_unlock_irqrestore(&sched->queue_lock, flags);
  320. }
  321. mutex_lock(&sched->sched_lock);
  322. fence = sched->ops->run_job(sched, c_entity, job);
  323. if (fence) {
  324. r = fence_add_callback(fence, &job->cb,
  325. amd_sched_process_job);
  326. if (r == -ENOENT)
  327. amd_sched_process_job(fence, &job->cb);
  328. else if (r)
  329. DRM_ERROR("fence add callback failed (%d)\n", r);
  330. fence_put(fence);
  331. }
  332. mutex_unlock(&sched->sched_lock);
  333. }
  334. return 0;
  335. }
  336. /**
  337. * Create a gpu scheduler
  338. *
  339. * @device The device context for this scheduler
  340. * @ops The backend operations for this scheduler.
  341. * @id The scheduler is per ring, here is ring id.
  342. * @granularity The minumum ms unit the scheduler will scheduled.
  343. * @preemption Indicate whether this ring support preemption, 0 is no.
  344. *
  345. * return the pointer to scheduler for success, otherwise return NULL
  346. */
  347. struct amd_gpu_scheduler *amd_sched_create(void *device,
  348. struct amd_sched_backend_ops *ops,
  349. unsigned ring,
  350. unsigned granularity,
  351. unsigned preemption,
  352. unsigned hw_submission)
  353. {
  354. struct amd_gpu_scheduler *sched;
  355. char name[20];
  356. sched = kzalloc(sizeof(struct amd_gpu_scheduler), GFP_KERNEL);
  357. if (!sched)
  358. return NULL;
  359. sched->device = device;
  360. sched->ops = ops;
  361. sched->granularity = granularity;
  362. sched->ring_id = ring;
  363. sched->preemption = preemption;
  364. sched->hw_submission_limit = hw_submission;
  365. snprintf(name, sizeof(name), "gpu_sched[%d]", ring);
  366. mutex_init(&sched->sched_lock);
  367. spin_lock_init(&sched->queue_lock);
  368. amd_sched_rq_init(&sched->sched_rq);
  369. amd_sched_rq_init(&sched->kernel_rq);
  370. init_waitqueue_head(&sched->wait_queue);
  371. INIT_LIST_HEAD(&sched->active_hw_rq);
  372. atomic64_set(&sched->hw_rq_count, 0);
  373. /* Each scheduler will run on a seperate kernel thread */
  374. sched->thread = kthread_create(amd_sched_main, sched, name);
  375. if (sched->thread) {
  376. wake_up_process(sched->thread);
  377. return sched;
  378. }
  379. DRM_ERROR("Failed to create scheduler for id %d.\n", ring);
  380. kfree(sched);
  381. return NULL;
  382. }
  383. /**
  384. * Destroy a gpu scheduler
  385. *
  386. * @sched The pointer to the scheduler
  387. *
  388. * return 0 if succeed. -1 if failed.
  389. */
  390. int amd_sched_destroy(struct amd_gpu_scheduler *sched)
  391. {
  392. kthread_stop(sched->thread);
  393. kfree(sched);
  394. return 0;
  395. }
  396. /**
  397. * Get next queued sequence number
  398. *
  399. * @entity The context entity
  400. *
  401. * return the next queued sequence number
  402. */
  403. uint64_t amd_sched_next_queued_seq(struct amd_sched_entity *c_entity)
  404. {
  405. return atomic64_read(&c_entity->last_queued_v_seq) + 1;
  406. }