gpu_scheduler.c 11 KB

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