gpu_scheduler.c 12 KB

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