gpu_scheduler.c 14 KB

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