gpu_scheduler.c 14 KB

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