sched_main.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  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. * DOC: Overview
  25. *
  26. * The GPU scheduler provides entities which allow userspace to push jobs
  27. * into software queues which are then scheduled on a hardware run queue.
  28. * The software queues have a priority among them. The scheduler selects the entities
  29. * from the run queue using a FIFO. The scheduler provides dependency handling
  30. * features among jobs. The driver is supposed to provide callback functions for
  31. * backend operations to the scheduler like submitting a job to hardware run queue,
  32. * returning the dependencies of a job etc.
  33. *
  34. * The organisation of the scheduler is the following:
  35. *
  36. * 1. Each hw run queue has one scheduler
  37. * 2. Each scheduler has multiple run queues with different priorities
  38. * (e.g., HIGH_HW,HIGH_SW, KERNEL, NORMAL)
  39. * 3. Each scheduler run queue has a queue of entities to schedule
  40. * 4. Entities themselves maintain a queue of jobs that will be scheduled on
  41. * the hardware.
  42. *
  43. * The jobs in a entity are always scheduled in the order that they were pushed.
  44. */
  45. #include <linux/kthread.h>
  46. #include <linux/wait.h>
  47. #include <linux/sched.h>
  48. #include <linux/completion.h>
  49. #include <linux/dma-resv.h>
  50. #include <uapi/linux/sched/types.h>
  51. #include <drm/drm_print.h>
  52. #include <drm/drm_gem.h>
  53. #include <drm/gpu_scheduler.h>
  54. #include <drm/spsc_queue.h>
  55. #define CREATE_TRACE_POINTS
  56. #include "gpu_scheduler_trace.h"
  57. #define to_drm_sched_job(sched_job) \
  58. container_of((sched_job), struct drm_sched_job, queue_node)
  59. /**
  60. * drm_sched_rq_init - initialize a given run queue struct
  61. *
  62. * @sched: scheduler instance to associate with this run queue
  63. * @rq: scheduler run queue
  64. *
  65. * Initializes a scheduler runqueue.
  66. */
  67. static void drm_sched_rq_init(struct drm_gpu_scheduler *sched,
  68. struct drm_sched_rq *rq)
  69. {
  70. spin_lock_init(&rq->lock);
  71. INIT_LIST_HEAD(&rq->entities);
  72. rq->current_entity = NULL;
  73. rq->sched = sched;
  74. }
  75. /**
  76. * drm_sched_rq_add_entity - add an entity
  77. *
  78. * @rq: scheduler run queue
  79. * @entity: scheduler entity
  80. *
  81. * Adds a scheduler entity to the run queue.
  82. */
  83. void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
  84. struct drm_sched_entity *entity)
  85. {
  86. if (!list_empty(&entity->list))
  87. return;
  88. spin_lock(&rq->lock);
  89. atomic_inc(rq->sched->score);
  90. list_add_tail(&entity->list, &rq->entities);
  91. spin_unlock(&rq->lock);
  92. }
  93. /**
  94. * drm_sched_rq_remove_entity - remove an entity
  95. *
  96. * @rq: scheduler run queue
  97. * @entity: scheduler entity
  98. *
  99. * Removes a scheduler entity from the run queue.
  100. */
  101. void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
  102. struct drm_sched_entity *entity)
  103. {
  104. if (list_empty(&entity->list))
  105. return;
  106. spin_lock(&rq->lock);
  107. atomic_dec(rq->sched->score);
  108. list_del_init(&entity->list);
  109. if (rq->current_entity == entity)
  110. rq->current_entity = NULL;
  111. spin_unlock(&rq->lock);
  112. }
  113. /**
  114. * drm_sched_rq_select_entity - Select an entity which could provide a job to run
  115. *
  116. * @rq: scheduler run queue to check.
  117. *
  118. * Try to find a ready entity, returns NULL if none found.
  119. */
  120. static struct drm_sched_entity *
  121. drm_sched_rq_select_entity(struct drm_sched_rq *rq)
  122. {
  123. struct drm_sched_entity *entity;
  124. spin_lock(&rq->lock);
  125. entity = rq->current_entity;
  126. if (entity) {
  127. list_for_each_entry_continue(entity, &rq->entities, list) {
  128. if (drm_sched_entity_is_ready(entity)) {
  129. rq->current_entity = entity;
  130. reinit_completion(&entity->entity_idle);
  131. spin_unlock(&rq->lock);
  132. return entity;
  133. }
  134. }
  135. }
  136. list_for_each_entry(entity, &rq->entities, list) {
  137. if (drm_sched_entity_is_ready(entity)) {
  138. rq->current_entity = entity;
  139. reinit_completion(&entity->entity_idle);
  140. spin_unlock(&rq->lock);
  141. return entity;
  142. }
  143. if (entity == rq->current_entity)
  144. break;
  145. }
  146. spin_unlock(&rq->lock);
  147. return NULL;
  148. }
  149. /**
  150. * drm_sched_job_done - complete a job
  151. * @s_job: pointer to the job which is done
  152. *
  153. * Finish the job's fence and wake up the worker thread.
  154. */
  155. static void drm_sched_job_done(struct drm_sched_job *s_job)
  156. {
  157. struct drm_sched_fence *s_fence = s_job->s_fence;
  158. struct drm_gpu_scheduler *sched = s_fence->sched;
  159. atomic_dec(&sched->hw_rq_count);
  160. atomic_dec(sched->score);
  161. trace_drm_sched_process_job(s_fence);
  162. dma_fence_get(&s_fence->finished);
  163. drm_sched_fence_finished(s_fence);
  164. dma_fence_put(&s_fence->finished);
  165. wake_up_interruptible(&sched->wake_up_worker);
  166. }
  167. /**
  168. * drm_sched_job_done_cb - the callback for a done job
  169. * @f: fence
  170. * @cb: fence callbacks
  171. */
  172. static void drm_sched_job_done_cb(struct dma_fence *f, struct dma_fence_cb *cb)
  173. {
  174. struct drm_sched_job *s_job = container_of(cb, struct drm_sched_job, cb);
  175. drm_sched_job_done(s_job);
  176. }
  177. /**
  178. * drm_sched_dependency_optimized - test if the dependency can be optimized
  179. *
  180. * @fence: the dependency fence
  181. * @entity: the entity which depends on the above fence
  182. *
  183. * Returns true if the dependency can be optimized and false otherwise
  184. */
  185. bool drm_sched_dependency_optimized(struct dma_fence* fence,
  186. struct drm_sched_entity *entity)
  187. {
  188. struct drm_gpu_scheduler *sched = entity->rq->sched;
  189. struct drm_sched_fence *s_fence;
  190. if (!fence || dma_fence_is_signaled(fence))
  191. return false;
  192. if (fence->context == entity->fence_context)
  193. return true;
  194. s_fence = to_drm_sched_fence(fence);
  195. if (s_fence && s_fence->sched == sched)
  196. return true;
  197. return false;
  198. }
  199. EXPORT_SYMBOL(drm_sched_dependency_optimized);
  200. /**
  201. * drm_sched_start_timeout - start timeout for reset worker
  202. *
  203. * @sched: scheduler instance to start the worker for
  204. *
  205. * Start the timeout for the given scheduler.
  206. */
  207. static void drm_sched_start_timeout(struct drm_gpu_scheduler *sched)
  208. {
  209. if (sched->timeout != MAX_SCHEDULE_TIMEOUT &&
  210. !list_empty(&sched->pending_list))
  211. queue_delayed_work(sched->timeout_wq, &sched->work_tdr, sched->timeout);
  212. }
  213. /**
  214. * drm_sched_fault - immediately start timeout handler
  215. *
  216. * @sched: scheduler where the timeout handling should be started.
  217. *
  218. * Start timeout handling immediately when the driver detects a hardware fault.
  219. */
  220. void drm_sched_fault(struct drm_gpu_scheduler *sched)
  221. {
  222. mod_delayed_work(sched->timeout_wq, &sched->work_tdr, 0);
  223. }
  224. EXPORT_SYMBOL(drm_sched_fault);
  225. /**
  226. * drm_sched_suspend_timeout - Suspend scheduler job timeout
  227. *
  228. * @sched: scheduler instance for which to suspend the timeout
  229. *
  230. * Suspend the delayed work timeout for the scheduler. This is done by
  231. * modifying the delayed work timeout to an arbitrary large value,
  232. * MAX_SCHEDULE_TIMEOUT in this case.
  233. *
  234. * Returns the timeout remaining
  235. *
  236. */
  237. unsigned long drm_sched_suspend_timeout(struct drm_gpu_scheduler *sched)
  238. {
  239. unsigned long sched_timeout, now = jiffies;
  240. sched_timeout = sched->work_tdr.timer.expires;
  241. /*
  242. * Modify the timeout to an arbitrarily large value. This also prevents
  243. * the timeout to be restarted when new submissions arrive
  244. */
  245. if (mod_delayed_work(sched->timeout_wq, &sched->work_tdr, MAX_SCHEDULE_TIMEOUT)
  246. && time_after(sched_timeout, now))
  247. return sched_timeout - now;
  248. else
  249. return sched->timeout;
  250. }
  251. EXPORT_SYMBOL(drm_sched_suspend_timeout);
  252. /**
  253. * drm_sched_resume_timeout - Resume scheduler job timeout
  254. *
  255. * @sched: scheduler instance for which to resume the timeout
  256. * @remaining: remaining timeout
  257. *
  258. * Resume the delayed work timeout for the scheduler.
  259. */
  260. void drm_sched_resume_timeout(struct drm_gpu_scheduler *sched,
  261. unsigned long remaining)
  262. {
  263. spin_lock(&sched->job_list_lock);
  264. if (list_empty(&sched->pending_list))
  265. cancel_delayed_work(&sched->work_tdr);
  266. else
  267. mod_delayed_work(sched->timeout_wq, &sched->work_tdr, remaining);
  268. spin_unlock(&sched->job_list_lock);
  269. }
  270. EXPORT_SYMBOL(drm_sched_resume_timeout);
  271. static void drm_sched_job_begin(struct drm_sched_job *s_job)
  272. {
  273. struct drm_gpu_scheduler *sched = s_job->sched;
  274. spin_lock(&sched->job_list_lock);
  275. list_add_tail(&s_job->list, &sched->pending_list);
  276. drm_sched_start_timeout(sched);
  277. spin_unlock(&sched->job_list_lock);
  278. }
  279. static void drm_sched_job_timedout(struct work_struct *work)
  280. {
  281. struct drm_gpu_scheduler *sched;
  282. struct drm_sched_job *job;
  283. enum drm_gpu_sched_stat status = DRM_GPU_SCHED_STAT_NOMINAL;
  284. sched = container_of(work, struct drm_gpu_scheduler, work_tdr.work);
  285. /* Protects against concurrent deletion in drm_sched_get_cleanup_job */
  286. spin_lock(&sched->job_list_lock);
  287. job = list_first_entry_or_null(&sched->pending_list,
  288. struct drm_sched_job, list);
  289. if (job) {
  290. /*
  291. * Remove the bad job so it cannot be freed by concurrent
  292. * drm_sched_cleanup_jobs. It will be reinserted back after sched->thread
  293. * is parked at which point it's safe.
  294. */
  295. list_del_init(&job->list);
  296. spin_unlock(&sched->job_list_lock);
  297. status = job->sched->ops->timedout_job(job);
  298. /*
  299. * Guilty job did complete and hence needs to be manually removed
  300. * See drm_sched_stop doc.
  301. */
  302. if (sched->free_guilty) {
  303. job->sched->ops->free_job(job);
  304. sched->free_guilty = false;
  305. }
  306. } else {
  307. spin_unlock(&sched->job_list_lock);
  308. }
  309. if (status != DRM_GPU_SCHED_STAT_ENODEV) {
  310. spin_lock(&sched->job_list_lock);
  311. drm_sched_start_timeout(sched);
  312. spin_unlock(&sched->job_list_lock);
  313. }
  314. }
  315. /**
  316. * drm_sched_increase_karma - Update sched_entity guilty flag
  317. *
  318. * @bad: The job guilty of time out
  319. *
  320. * Increment on every hang caused by the 'bad' job. If this exceeds the hang
  321. * limit of the scheduler then the respective sched entity is marked guilty and
  322. * jobs from it will not be scheduled further
  323. */
  324. void drm_sched_increase_karma(struct drm_sched_job *bad)
  325. {
  326. drm_sched_increase_karma_ext(bad, 1);
  327. }
  328. EXPORT_SYMBOL(drm_sched_increase_karma);
  329. void drm_sched_reset_karma(struct drm_sched_job *bad)
  330. {
  331. drm_sched_increase_karma_ext(bad, 0);
  332. }
  333. EXPORT_SYMBOL(drm_sched_reset_karma);
  334. /**
  335. * drm_sched_stop - stop the scheduler
  336. *
  337. * @sched: scheduler instance
  338. * @bad: job which caused the time out
  339. *
  340. * Stop the scheduler and also removes and frees all completed jobs.
  341. * Note: bad job will not be freed as it might be used later and so it's
  342. * callers responsibility to release it manually if it's not part of the
  343. * pending list any more.
  344. *
  345. */
  346. void drm_sched_stop(struct drm_gpu_scheduler *sched, struct drm_sched_job *bad)
  347. {
  348. struct drm_sched_job *s_job, *tmp;
  349. kthread_park(sched->thread);
  350. /*
  351. * Reinsert back the bad job here - now it's safe as
  352. * drm_sched_get_cleanup_job cannot race against us and release the
  353. * bad job at this point - we parked (waited for) any in progress
  354. * (earlier) cleanups and drm_sched_get_cleanup_job will not be called
  355. * now until the scheduler thread is unparked.
  356. */
  357. if (bad && bad->sched == sched)
  358. /*
  359. * Add at the head of the queue to reflect it was the earliest
  360. * job extracted.
  361. */
  362. list_add(&bad->list, &sched->pending_list);
  363. /*
  364. * Iterate the job list from later to earlier one and either deactive
  365. * their HW callbacks or remove them from pending list if they already
  366. * signaled.
  367. * This iteration is thread safe as sched thread is stopped.
  368. */
  369. list_for_each_entry_safe_reverse(s_job, tmp, &sched->pending_list,
  370. list) {
  371. if (s_job->s_fence->parent &&
  372. dma_fence_remove_callback(s_job->s_fence->parent,
  373. &s_job->cb)) {
  374. dma_fence_put(s_job->s_fence->parent);
  375. s_job->s_fence->parent = NULL;
  376. atomic_dec(&sched->hw_rq_count);
  377. } else {
  378. /*
  379. * remove job from pending_list.
  380. * Locking here is for concurrent resume timeout
  381. */
  382. spin_lock(&sched->job_list_lock);
  383. list_del_init(&s_job->list);
  384. spin_unlock(&sched->job_list_lock);
  385. /*
  386. * Wait for job's HW fence callback to finish using s_job
  387. * before releasing it.
  388. *
  389. * Job is still alive so fence refcount at least 1
  390. */
  391. dma_fence_wait(&s_job->s_fence->finished, false);
  392. /*
  393. * We must keep bad job alive for later use during
  394. * recovery by some of the drivers but leave a hint
  395. * that the guilty job must be released.
  396. */
  397. if (bad != s_job)
  398. sched->ops->free_job(s_job);
  399. else
  400. sched->free_guilty = true;
  401. }
  402. }
  403. /*
  404. * Stop pending timer in flight as we rearm it in drm_sched_start. This
  405. * avoids the pending timeout work in progress to fire right away after
  406. * this TDR finished and before the newly restarted jobs had a
  407. * chance to complete.
  408. */
  409. cancel_delayed_work(&sched->work_tdr);
  410. }
  411. EXPORT_SYMBOL(drm_sched_stop);
  412. /**
  413. * drm_sched_start - recover jobs after a reset
  414. *
  415. * @sched: scheduler instance
  416. * @full_recovery: proceed with complete sched restart
  417. *
  418. */
  419. void drm_sched_start(struct drm_gpu_scheduler *sched, bool full_recovery)
  420. {
  421. struct drm_sched_job *s_job, *tmp;
  422. int r;
  423. /*
  424. * Locking the list is not required here as the sched thread is parked
  425. * so no new jobs are being inserted or removed. Also concurrent
  426. * GPU recovers can't run in parallel.
  427. */
  428. list_for_each_entry_safe(s_job, tmp, &sched->pending_list, list) {
  429. struct dma_fence *fence = s_job->s_fence->parent;
  430. atomic_inc(&sched->hw_rq_count);
  431. if (!full_recovery)
  432. continue;
  433. if (fence) {
  434. r = dma_fence_add_callback(fence, &s_job->cb,
  435. drm_sched_job_done_cb);
  436. if (r == -ENOENT)
  437. drm_sched_job_done(s_job);
  438. else if (r)
  439. DRM_DEV_ERROR(sched->dev, "fence add callback failed (%d)\n",
  440. r);
  441. } else
  442. drm_sched_job_done(s_job);
  443. }
  444. if (full_recovery) {
  445. spin_lock(&sched->job_list_lock);
  446. drm_sched_start_timeout(sched);
  447. spin_unlock(&sched->job_list_lock);
  448. }
  449. kthread_unpark(sched->thread);
  450. }
  451. EXPORT_SYMBOL(drm_sched_start);
  452. /**
  453. * drm_sched_resubmit_jobs - helper to relaunch jobs from the pending list
  454. *
  455. * @sched: scheduler instance
  456. *
  457. */
  458. void drm_sched_resubmit_jobs(struct drm_gpu_scheduler *sched)
  459. {
  460. drm_sched_resubmit_jobs_ext(sched, INT_MAX);
  461. }
  462. EXPORT_SYMBOL(drm_sched_resubmit_jobs);
  463. /**
  464. * drm_sched_resubmit_jobs_ext - helper to relunch certain number of jobs from mirror ring list
  465. *
  466. * @sched: scheduler instance
  467. * @max: job numbers to relaunch
  468. *
  469. */
  470. void drm_sched_resubmit_jobs_ext(struct drm_gpu_scheduler *sched, int max)
  471. {
  472. struct drm_sched_job *s_job, *tmp;
  473. uint64_t guilty_context;
  474. bool found_guilty = false;
  475. struct dma_fence *fence;
  476. int i = 0;
  477. list_for_each_entry_safe(s_job, tmp, &sched->pending_list, list) {
  478. struct drm_sched_fence *s_fence = s_job->s_fence;
  479. if (i >= max)
  480. break;
  481. if (!found_guilty && atomic_read(&s_job->karma) > sched->hang_limit) {
  482. found_guilty = true;
  483. guilty_context = s_job->s_fence->scheduled.context;
  484. }
  485. if (found_guilty && s_job->s_fence->scheduled.context == guilty_context)
  486. dma_fence_set_error(&s_fence->finished, -ECANCELED);
  487. fence = sched->ops->run_job(s_job);
  488. i++;
  489. if (IS_ERR_OR_NULL(fence)) {
  490. if (IS_ERR(fence))
  491. dma_fence_set_error(&s_fence->finished, PTR_ERR(fence));
  492. s_job->s_fence->parent = NULL;
  493. } else {
  494. s_job->s_fence->parent = dma_fence_get(fence);
  495. /* Drop for orignal kref_init */
  496. dma_fence_put(fence);
  497. }
  498. }
  499. }
  500. EXPORT_SYMBOL(drm_sched_resubmit_jobs_ext);
  501. /**
  502. * drm_sched_job_init - init a scheduler job
  503. * @job: scheduler job to init
  504. * @entity: scheduler entity to use
  505. * @owner: job owner for debugging
  506. *
  507. * Refer to drm_sched_entity_push_job() documentation
  508. * for locking considerations.
  509. *
  510. * Drivers must make sure drm_sched_job_cleanup() if this function returns
  511. * successfully, even when @job is aborted before drm_sched_job_arm() is called.
  512. *
  513. * WARNING: amdgpu abuses &drm_sched.ready to signal when the hardware
  514. * has died, which can mean that there's no valid runqueue for a @entity.
  515. * This function returns -ENOENT in this case (which probably should be -EIO as
  516. * a more meanigful return value).
  517. *
  518. * Returns 0 for success, negative error code otherwise.
  519. */
  520. int drm_sched_job_init(struct drm_sched_job *job,
  521. struct drm_sched_entity *entity,
  522. void *owner)
  523. {
  524. if (!entity->rq)
  525. return -ENOENT;
  526. job->entity = entity;
  527. job->s_fence = drm_sched_fence_alloc(entity, owner);
  528. if (!job->s_fence)
  529. return -ENOMEM;
  530. INIT_LIST_HEAD(&job->list);
  531. xa_init_flags(&job->dependencies, XA_FLAGS_ALLOC);
  532. return 0;
  533. }
  534. EXPORT_SYMBOL(drm_sched_job_init);
  535. /**
  536. * drm_sched_job_arm - arm a scheduler job for execution
  537. * @job: scheduler job to arm
  538. *
  539. * This arms a scheduler job for execution. Specifically it initializes the
  540. * &drm_sched_job.s_fence of @job, so that it can be attached to struct dma_resv
  541. * or other places that need to track the completion of this job.
  542. *
  543. * Refer to drm_sched_entity_push_job() documentation for locking
  544. * considerations.
  545. *
  546. * This can only be called if drm_sched_job_init() succeeded.
  547. */
  548. void drm_sched_job_arm(struct drm_sched_job *job)
  549. {
  550. struct drm_gpu_scheduler *sched;
  551. struct drm_sched_entity *entity = job->entity;
  552. BUG_ON(!entity);
  553. drm_sched_entity_select_rq(entity);
  554. sched = entity->rq->sched;
  555. job->sched = sched;
  556. job->s_priority = entity->rq - sched->sched_rq;
  557. job->id = atomic64_inc_return(&sched->job_id_count);
  558. drm_sched_fence_init(job->s_fence, job->entity);
  559. }
  560. EXPORT_SYMBOL(drm_sched_job_arm);
  561. /**
  562. * drm_sched_job_add_dependency - adds the fence as a job dependency
  563. * @job: scheduler job to add the dependencies to
  564. * @fence: the dma_fence to add to the list of dependencies.
  565. *
  566. * Note that @fence is consumed in both the success and error cases.
  567. *
  568. * Returns:
  569. * 0 on success, or an error on failing to expand the array.
  570. */
  571. int drm_sched_job_add_dependency(struct drm_sched_job *job,
  572. struct dma_fence *fence)
  573. {
  574. struct dma_fence *entry;
  575. unsigned long index;
  576. u32 id = 0;
  577. int ret;
  578. if (!fence)
  579. return 0;
  580. /* Deduplicate if we already depend on a fence from the same context.
  581. * This lets the size of the array of deps scale with the number of
  582. * engines involved, rather than the number of BOs.
  583. */
  584. xa_for_each(&job->dependencies, index, entry) {
  585. if (entry->context != fence->context)
  586. continue;
  587. if (dma_fence_is_later(fence, entry)) {
  588. dma_fence_put(entry);
  589. xa_store(&job->dependencies, index, fence, GFP_KERNEL);
  590. } else {
  591. dma_fence_put(fence);
  592. }
  593. return 0;
  594. }
  595. ret = xa_alloc(&job->dependencies, &id, fence, xa_limit_32b, GFP_KERNEL);
  596. if (ret != 0)
  597. dma_fence_put(fence);
  598. return ret;
  599. }
  600. EXPORT_SYMBOL(drm_sched_job_add_dependency);
  601. /**
  602. * drm_sched_job_add_implicit_dependencies - adds implicit dependencies as job
  603. * dependencies
  604. * @job: scheduler job to add the dependencies to
  605. * @obj: the gem object to add new dependencies from.
  606. * @write: whether the job might write the object (so we need to depend on
  607. * shared fences in the reservation object).
  608. *
  609. * This should be called after drm_gem_lock_reservations() on your array of
  610. * GEM objects used in the job but before updating the reservations with your
  611. * own fences.
  612. *
  613. * Returns:
  614. * 0 on success, or an error on failing to expand the array.
  615. */
  616. int drm_sched_job_add_implicit_dependencies(struct drm_sched_job *job,
  617. struct drm_gem_object *obj,
  618. bool write)
  619. {
  620. struct dma_resv_iter cursor;
  621. struct dma_fence *fence;
  622. int ret;
  623. dma_resv_assert_held(obj->resv);
  624. dma_resv_for_each_fence(&cursor, obj->resv, dma_resv_usage_rw(write),
  625. fence) {
  626. /* Make sure to grab an additional ref on the added fence */
  627. dma_fence_get(fence);
  628. ret = drm_sched_job_add_dependency(job, fence);
  629. if (ret) {
  630. dma_fence_put(fence);
  631. return ret;
  632. }
  633. }
  634. return 0;
  635. }
  636. EXPORT_SYMBOL(drm_sched_job_add_implicit_dependencies);
  637. /**
  638. * drm_sched_job_cleanup - clean up scheduler job resources
  639. * @job: scheduler job to clean up
  640. *
  641. * Cleans up the resources allocated with drm_sched_job_init().
  642. *
  643. * Drivers should call this from their error unwind code if @job is aborted
  644. * before drm_sched_job_arm() is called.
  645. *
  646. * After that point of no return @job is committed to be executed by the
  647. * scheduler, and this function should be called from the
  648. * &drm_sched_backend_ops.free_job callback.
  649. */
  650. void drm_sched_job_cleanup(struct drm_sched_job *job)
  651. {
  652. struct dma_fence *fence;
  653. unsigned long index;
  654. if (kref_read(&job->s_fence->finished.refcount)) {
  655. /* drm_sched_job_arm() has been called */
  656. dma_fence_put(&job->s_fence->finished);
  657. } else {
  658. /* aborted job before committing to run it */
  659. drm_sched_fence_free(job->s_fence);
  660. }
  661. job->s_fence = NULL;
  662. xa_for_each(&job->dependencies, index, fence) {
  663. dma_fence_put(fence);
  664. }
  665. xa_destroy(&job->dependencies);
  666. }
  667. EXPORT_SYMBOL(drm_sched_job_cleanup);
  668. /**
  669. * drm_sched_ready - is the scheduler ready
  670. *
  671. * @sched: scheduler instance
  672. *
  673. * Return true if we can push more jobs to the hw, otherwise false.
  674. */
  675. static bool drm_sched_ready(struct drm_gpu_scheduler *sched)
  676. {
  677. return atomic_read(&sched->hw_rq_count) <
  678. sched->hw_submission_limit;
  679. }
  680. /**
  681. * drm_sched_wakeup - Wake up the scheduler when it is ready
  682. *
  683. * @sched: scheduler instance
  684. *
  685. */
  686. void drm_sched_wakeup(struct drm_gpu_scheduler *sched)
  687. {
  688. if (drm_sched_ready(sched))
  689. wake_up_interruptible(&sched->wake_up_worker);
  690. }
  691. /**
  692. * drm_sched_select_entity - Select next entity to process
  693. *
  694. * @sched: scheduler instance
  695. *
  696. * Returns the entity to process or NULL if none are found.
  697. */
  698. static struct drm_sched_entity *
  699. drm_sched_select_entity(struct drm_gpu_scheduler *sched)
  700. {
  701. struct drm_sched_entity *entity;
  702. int i;
  703. if (!drm_sched_ready(sched))
  704. return NULL;
  705. /* Kernel run queue has higher priority than normal run queue*/
  706. for (i = DRM_SCHED_PRIORITY_COUNT - 1; i >= DRM_SCHED_PRIORITY_MIN; i--) {
  707. entity = drm_sched_rq_select_entity(&sched->sched_rq[i]);
  708. if (entity)
  709. break;
  710. }
  711. return entity;
  712. }
  713. /**
  714. * drm_sched_get_cleanup_job - fetch the next finished job to be destroyed
  715. *
  716. * @sched: scheduler instance
  717. *
  718. * Returns the next finished job from the pending list (if there is one)
  719. * ready for it to be destroyed.
  720. */
  721. static struct drm_sched_job *
  722. drm_sched_get_cleanup_job(struct drm_gpu_scheduler *sched)
  723. {
  724. struct drm_sched_job *job, *next;
  725. spin_lock(&sched->job_list_lock);
  726. job = list_first_entry_or_null(&sched->pending_list,
  727. struct drm_sched_job, list);
  728. if (job && dma_fence_is_signaled(&job->s_fence->finished)) {
  729. /* remove job from pending_list */
  730. list_del_init(&job->list);
  731. /* cancel this job's TO timer */
  732. cancel_delayed_work(&sched->work_tdr);
  733. /* make the scheduled timestamp more accurate */
  734. next = list_first_entry_or_null(&sched->pending_list,
  735. typeof(*next), list);
  736. if (next) {
  737. next->s_fence->scheduled.timestamp =
  738. dma_fence_timestamp(&job->s_fence->finished);
  739. /* start TO timer for next job */
  740. drm_sched_start_timeout(sched);
  741. }
  742. } else {
  743. job = NULL;
  744. }
  745. spin_unlock(&sched->job_list_lock);
  746. return job;
  747. }
  748. /**
  749. * drm_sched_pick_best - Get a drm sched from a sched_list with the least load
  750. * @sched_list: list of drm_gpu_schedulers
  751. * @num_sched_list: number of drm_gpu_schedulers in the sched_list
  752. *
  753. * Returns pointer of the sched with the least load or NULL if none of the
  754. * drm_gpu_schedulers are ready
  755. */
  756. struct drm_gpu_scheduler *
  757. drm_sched_pick_best(struct drm_gpu_scheduler **sched_list,
  758. unsigned int num_sched_list)
  759. {
  760. struct drm_gpu_scheduler *sched, *picked_sched = NULL;
  761. int i;
  762. unsigned int min_score = UINT_MAX, num_score;
  763. for (i = 0; i < num_sched_list; ++i) {
  764. sched = sched_list[i];
  765. if (!sched->ready) {
  766. DRM_WARN("scheduler %s is not ready, skipping",
  767. sched->name);
  768. continue;
  769. }
  770. num_score = atomic_read(sched->score);
  771. if (num_score < min_score) {
  772. min_score = num_score;
  773. picked_sched = sched;
  774. }
  775. }
  776. return picked_sched;
  777. }
  778. EXPORT_SYMBOL(drm_sched_pick_best);
  779. /**
  780. * drm_sched_blocked - check if the scheduler is blocked
  781. *
  782. * @sched: scheduler instance
  783. *
  784. * Returns true if blocked, otherwise false.
  785. */
  786. static bool drm_sched_blocked(struct drm_gpu_scheduler *sched)
  787. {
  788. if (kthread_should_park()) {
  789. kthread_parkme();
  790. return true;
  791. }
  792. return false;
  793. }
  794. /**
  795. * drm_sched_main - main scheduler thread
  796. *
  797. * @param: scheduler instance
  798. *
  799. * Returns 0.
  800. */
  801. static int drm_sched_main(void *param)
  802. {
  803. struct drm_gpu_scheduler *sched = (struct drm_gpu_scheduler *)param;
  804. int r;
  805. sched_set_fifo_low(current);
  806. while (!kthread_should_stop()) {
  807. struct drm_sched_entity *entity = NULL;
  808. struct drm_sched_fence *s_fence;
  809. struct drm_sched_job *sched_job;
  810. struct dma_fence *fence;
  811. struct drm_sched_job *cleanup_job = NULL;
  812. wait_event_interruptible(sched->wake_up_worker,
  813. (cleanup_job = drm_sched_get_cleanup_job(sched)) ||
  814. (!drm_sched_blocked(sched) &&
  815. (entity = drm_sched_select_entity(sched))) ||
  816. kthread_should_stop());
  817. if (cleanup_job)
  818. sched->ops->free_job(cleanup_job);
  819. if (!entity)
  820. continue;
  821. sched_job = drm_sched_entity_pop_job(entity);
  822. if (!sched_job) {
  823. complete(&entity->entity_idle);
  824. continue;
  825. }
  826. s_fence = sched_job->s_fence;
  827. atomic_inc(&sched->hw_rq_count);
  828. drm_sched_job_begin(sched_job);
  829. trace_drm_run_job(sched_job, entity);
  830. fence = sched->ops->run_job(sched_job);
  831. complete(&entity->entity_idle);
  832. drm_sched_fence_scheduled(s_fence);
  833. if (!IS_ERR_OR_NULL(fence)) {
  834. s_fence->parent = dma_fence_get(fence);
  835. /* Drop for original kref_init of the fence */
  836. dma_fence_put(fence);
  837. r = dma_fence_add_callback(fence, &sched_job->cb,
  838. drm_sched_job_done_cb);
  839. if (r == -ENOENT)
  840. drm_sched_job_done(sched_job);
  841. else if (r)
  842. DRM_DEV_ERROR(sched->dev, "fence add callback failed (%d)\n",
  843. r);
  844. } else {
  845. if (IS_ERR(fence))
  846. dma_fence_set_error(&s_fence->finished, PTR_ERR(fence));
  847. drm_sched_job_done(sched_job);
  848. }
  849. wake_up(&sched->job_scheduled);
  850. }
  851. return 0;
  852. }
  853. /**
  854. * drm_sched_init - Init a gpu scheduler instance
  855. *
  856. * @sched: scheduler instance
  857. * @ops: backend operations for this scheduler
  858. * @hw_submission: number of hw submissions that can be in flight
  859. * @hang_limit: number of times to allow a job to hang before dropping it
  860. * @timeout: timeout value in jiffies for the scheduler
  861. * @timeout_wq: workqueue to use for timeout work. If NULL, the system_wq is
  862. * used
  863. * @score: optional score atomic shared with other schedulers
  864. * @name: name used for debugging
  865. * @dev: target &struct device
  866. *
  867. * Return 0 on success, otherwise error code.
  868. */
  869. int drm_sched_init(struct drm_gpu_scheduler *sched,
  870. const struct drm_sched_backend_ops *ops,
  871. unsigned hw_submission, unsigned hang_limit,
  872. long timeout, struct workqueue_struct *timeout_wq,
  873. atomic_t *score, const char *name, struct device *dev)
  874. {
  875. int i, ret;
  876. sched->ops = ops;
  877. sched->hw_submission_limit = hw_submission;
  878. sched->name = name;
  879. sched->timeout = timeout;
  880. sched->timeout_wq = timeout_wq ? : system_wq;
  881. sched->hang_limit = hang_limit;
  882. sched->score = score ? score : &sched->_score;
  883. sched->dev = dev;
  884. for (i = DRM_SCHED_PRIORITY_MIN; i < DRM_SCHED_PRIORITY_COUNT; i++)
  885. drm_sched_rq_init(sched, &sched->sched_rq[i]);
  886. init_waitqueue_head(&sched->wake_up_worker);
  887. init_waitqueue_head(&sched->job_scheduled);
  888. INIT_LIST_HEAD(&sched->pending_list);
  889. spin_lock_init(&sched->job_list_lock);
  890. atomic_set(&sched->hw_rq_count, 0);
  891. INIT_DELAYED_WORK(&sched->work_tdr, drm_sched_job_timedout);
  892. atomic_set(&sched->_score, 0);
  893. atomic64_set(&sched->job_id_count, 0);
  894. /* Each scheduler will run on a seperate kernel thread */
  895. sched->thread = kthread_run(drm_sched_main, sched, sched->name);
  896. if (IS_ERR(sched->thread)) {
  897. ret = PTR_ERR(sched->thread);
  898. sched->thread = NULL;
  899. DRM_DEV_ERROR(sched->dev, "Failed to create scheduler for %s.\n", name);
  900. return ret;
  901. }
  902. sched->ready = true;
  903. return 0;
  904. }
  905. EXPORT_SYMBOL(drm_sched_init);
  906. /**
  907. * drm_sched_fini - Destroy a gpu scheduler
  908. *
  909. * @sched: scheduler instance
  910. *
  911. * Tears down and cleans up the scheduler.
  912. */
  913. void drm_sched_fini(struct drm_gpu_scheduler *sched)
  914. {
  915. struct drm_sched_entity *s_entity;
  916. int i;
  917. if (sched->thread)
  918. kthread_stop(sched->thread);
  919. for (i = DRM_SCHED_PRIORITY_COUNT - 1; i >= DRM_SCHED_PRIORITY_MIN; i--) {
  920. struct drm_sched_rq *rq = &sched->sched_rq[i];
  921. if (!rq)
  922. continue;
  923. spin_lock(&rq->lock);
  924. list_for_each_entry(s_entity, &rq->entities, list)
  925. /*
  926. * Prevents reinsertion and marks job_queue as idle,
  927. * it will removed from rq in drm_sched_entity_fini
  928. * eventually
  929. */
  930. s_entity->stopped = true;
  931. spin_unlock(&rq->lock);
  932. }
  933. /* Wakeup everyone stuck in drm_sched_entity_flush for this scheduler */
  934. wake_up_all(&sched->job_scheduled);
  935. /* Confirm no work left behind accessing device structures */
  936. cancel_delayed_work_sync(&sched->work_tdr);
  937. sched->ready = false;
  938. }
  939. EXPORT_SYMBOL(drm_sched_fini);
  940. /**
  941. * drm_sched_increase_karma_ext - Update sched_entity guilty flag
  942. *
  943. * @bad: The job guilty of time out
  944. * @type: type for increase/reset karma
  945. *
  946. */
  947. void drm_sched_increase_karma_ext(struct drm_sched_job *bad, int type)
  948. {
  949. int i;
  950. struct drm_sched_entity *tmp;
  951. struct drm_sched_entity *entity;
  952. struct drm_gpu_scheduler *sched = bad->sched;
  953. /* don't change @bad's karma if it's from KERNEL RQ,
  954. * because sometimes GPU hang would cause kernel jobs (like VM updating jobs)
  955. * corrupt but keep in mind that kernel jobs always considered good.
  956. */
  957. if (bad->s_priority != DRM_SCHED_PRIORITY_KERNEL) {
  958. if (type == 0)
  959. atomic_set(&bad->karma, 0);
  960. else if (type == 1)
  961. atomic_inc(&bad->karma);
  962. for (i = DRM_SCHED_PRIORITY_MIN; i < DRM_SCHED_PRIORITY_KERNEL;
  963. i++) {
  964. struct drm_sched_rq *rq = &sched->sched_rq[i];
  965. spin_lock(&rq->lock);
  966. list_for_each_entry_safe(entity, tmp, &rq->entities, list) {
  967. if (bad->s_fence->scheduled.context ==
  968. entity->fence_context) {
  969. if (entity->guilty)
  970. atomic_set(entity->guilty, type);
  971. break;
  972. }
  973. }
  974. spin_unlock(&rq->lock);
  975. if (&entity->list != &rq->entities)
  976. break;
  977. }
  978. }
  979. }
  980. EXPORT_SYMBOL(drm_sched_increase_karma_ext);