sched_main.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604
  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 an entity are always scheduled in the order in which they were pushed.
  44. *
  45. * Note that once a job was taken from the entities queue and pushed to the
  46. * hardware, i.e. the pending queue, the entity must not be referenced anymore
  47. * through the jobs entity pointer.
  48. */
  49. /**
  50. * DOC: Flow Control
  51. *
  52. * The DRM GPU scheduler provides a flow control mechanism to regulate the rate
  53. * in which the jobs fetched from scheduler entities are executed.
  54. *
  55. * In this context the &drm_gpu_scheduler keeps track of a driver specified
  56. * credit limit representing the capacity of this scheduler and a credit count;
  57. * every &drm_sched_job carries a driver specified number of credits.
  58. *
  59. * Once a job is executed (but not yet finished), the job's credits contribute
  60. * to the scheduler's credit count until the job is finished. If by executing
  61. * one more job the scheduler's credit count would exceed the scheduler's
  62. * credit limit, the job won't be executed. Instead, the scheduler will wait
  63. * until the credit count has decreased enough to not overflow its credit limit.
  64. * This implies waiting for previously executed jobs.
  65. */
  66. #include <linux/export.h>
  67. #include <linux/wait.h>
  68. #include <linux/sched.h>
  69. #include <linux/completion.h>
  70. #include <linux/dma-resv.h>
  71. #include <uapi/linux/sched/types.h>
  72. #include <drm/drm_print.h>
  73. #include <drm/drm_gem.h>
  74. #include <drm/drm_syncobj.h>
  75. #include <drm/gpu_scheduler.h>
  76. #include <drm/spsc_queue.h>
  77. #include "sched_internal.h"
  78. #define CREATE_TRACE_POINTS
  79. #include "gpu_scheduler_trace.h"
  80. int drm_sched_policy = DRM_SCHED_POLICY_FIFO;
  81. /**
  82. * DOC: sched_policy (int)
  83. * Used to override default entities scheduling policy in a run queue.
  84. */
  85. MODULE_PARM_DESC(sched_policy, "Specify the scheduling policy for entities on a run-queue, " __stringify(DRM_SCHED_POLICY_RR) " = Round Robin, " __stringify(DRM_SCHED_POLICY_FIFO) " = FIFO (default).");
  86. module_param_named(sched_policy, drm_sched_policy, int, 0444);
  87. static u32 drm_sched_available_credits(struct drm_gpu_scheduler *sched)
  88. {
  89. u32 credits;
  90. WARN_ON(check_sub_overflow(sched->credit_limit,
  91. atomic_read(&sched->credit_count),
  92. &credits));
  93. return credits;
  94. }
  95. /**
  96. * drm_sched_can_queue -- Can we queue more to the hardware?
  97. * @sched: scheduler instance
  98. * @entity: the scheduler entity
  99. *
  100. * Return true if we can push at least one more job from @entity, false
  101. * otherwise.
  102. */
  103. static bool drm_sched_can_queue(struct drm_gpu_scheduler *sched,
  104. struct drm_sched_entity *entity)
  105. {
  106. struct drm_sched_job *s_job;
  107. s_job = drm_sched_entity_queue_peek(entity);
  108. if (!s_job)
  109. return false;
  110. /* If a job exceeds the credit limit, truncate it to the credit limit
  111. * itself to guarantee forward progress.
  112. */
  113. if (s_job->credits > sched->credit_limit) {
  114. dev_WARN(sched->dev,
  115. "Jobs may not exceed the credit limit, truncate.\n");
  116. s_job->credits = sched->credit_limit;
  117. }
  118. return drm_sched_available_credits(sched) >= s_job->credits;
  119. }
  120. static __always_inline bool drm_sched_entity_compare_before(struct rb_node *a,
  121. const struct rb_node *b)
  122. {
  123. struct drm_sched_entity *ent_a = rb_entry((a), struct drm_sched_entity, rb_tree_node);
  124. struct drm_sched_entity *ent_b = rb_entry((b), struct drm_sched_entity, rb_tree_node);
  125. return ktime_before(ent_a->oldest_job_waiting, ent_b->oldest_job_waiting);
  126. }
  127. static void drm_sched_rq_remove_fifo_locked(struct drm_sched_entity *entity,
  128. struct drm_sched_rq *rq)
  129. {
  130. if (!RB_EMPTY_NODE(&entity->rb_tree_node)) {
  131. rb_erase_cached(&entity->rb_tree_node, &rq->rb_tree_root);
  132. RB_CLEAR_NODE(&entity->rb_tree_node);
  133. }
  134. }
  135. void drm_sched_rq_update_fifo_locked(struct drm_sched_entity *entity,
  136. struct drm_sched_rq *rq,
  137. ktime_t ts)
  138. {
  139. /*
  140. * Both locks need to be grabbed, one to protect from entity->rq change
  141. * for entity from within concurrent drm_sched_entity_select_rq and the
  142. * other to update the rb tree structure.
  143. */
  144. lockdep_assert_held(&entity->lock);
  145. lockdep_assert_held(&rq->lock);
  146. drm_sched_rq_remove_fifo_locked(entity, rq);
  147. entity->oldest_job_waiting = ts;
  148. rb_add_cached(&entity->rb_tree_node, &rq->rb_tree_root,
  149. drm_sched_entity_compare_before);
  150. }
  151. /**
  152. * drm_sched_rq_init - initialize a given run queue struct
  153. *
  154. * @sched: scheduler instance to associate with this run queue
  155. * @rq: scheduler run queue
  156. *
  157. * Initializes a scheduler runqueue.
  158. */
  159. static void drm_sched_rq_init(struct drm_gpu_scheduler *sched,
  160. struct drm_sched_rq *rq)
  161. {
  162. spin_lock_init(&rq->lock);
  163. INIT_LIST_HEAD(&rq->entities);
  164. rq->rb_tree_root = RB_ROOT_CACHED;
  165. rq->current_entity = NULL;
  166. rq->sched = sched;
  167. }
  168. /**
  169. * drm_sched_rq_add_entity - add an entity
  170. *
  171. * @rq: scheduler run queue
  172. * @entity: scheduler entity
  173. *
  174. * Adds a scheduler entity to the run queue.
  175. */
  176. void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
  177. struct drm_sched_entity *entity)
  178. {
  179. lockdep_assert_held(&entity->lock);
  180. lockdep_assert_held(&rq->lock);
  181. if (!list_empty(&entity->list))
  182. return;
  183. atomic_inc(rq->sched->score);
  184. list_add_tail(&entity->list, &rq->entities);
  185. }
  186. /**
  187. * drm_sched_rq_remove_entity - remove an entity
  188. *
  189. * @rq: scheduler run queue
  190. * @entity: scheduler entity
  191. *
  192. * Removes a scheduler entity from the run queue.
  193. */
  194. void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
  195. struct drm_sched_entity *entity)
  196. {
  197. lockdep_assert_held(&entity->lock);
  198. if (list_empty(&entity->list))
  199. return;
  200. spin_lock(&rq->lock);
  201. atomic_dec(rq->sched->score);
  202. list_del_init(&entity->list);
  203. if (rq->current_entity == entity)
  204. rq->current_entity = NULL;
  205. if (drm_sched_policy == DRM_SCHED_POLICY_FIFO)
  206. drm_sched_rq_remove_fifo_locked(entity, rq);
  207. spin_unlock(&rq->lock);
  208. }
  209. /**
  210. * drm_sched_rq_select_entity_rr - Select an entity which could provide a job to run
  211. *
  212. * @sched: the gpu scheduler
  213. * @rq: scheduler run queue to check.
  214. *
  215. * Try to find the next ready entity.
  216. *
  217. * Return an entity if one is found; return an error-pointer (!NULL) if an
  218. * entity was ready, but the scheduler had insufficient credits to accommodate
  219. * its job; return NULL, if no ready entity was found.
  220. */
  221. static struct drm_sched_entity *
  222. drm_sched_rq_select_entity_rr(struct drm_gpu_scheduler *sched,
  223. struct drm_sched_rq *rq)
  224. {
  225. struct drm_sched_entity *entity;
  226. spin_lock(&rq->lock);
  227. entity = rq->current_entity;
  228. if (entity) {
  229. list_for_each_entry_continue(entity, &rq->entities, list) {
  230. if (drm_sched_entity_is_ready(entity))
  231. goto found;
  232. }
  233. }
  234. list_for_each_entry(entity, &rq->entities, list) {
  235. if (drm_sched_entity_is_ready(entity))
  236. goto found;
  237. if (entity == rq->current_entity)
  238. break;
  239. }
  240. spin_unlock(&rq->lock);
  241. return NULL;
  242. found:
  243. if (!drm_sched_can_queue(sched, entity)) {
  244. /*
  245. * If scheduler cannot take more jobs signal the caller to not
  246. * consider lower priority queues.
  247. */
  248. entity = ERR_PTR(-ENOSPC);
  249. } else {
  250. rq->current_entity = entity;
  251. reinit_completion(&entity->entity_idle);
  252. }
  253. spin_unlock(&rq->lock);
  254. return entity;
  255. }
  256. /**
  257. * drm_sched_rq_select_entity_fifo - Select an entity which provides a job to run
  258. *
  259. * @sched: the gpu scheduler
  260. * @rq: scheduler run queue to check.
  261. *
  262. * Find oldest waiting ready entity.
  263. *
  264. * Return an entity if one is found; return an error-pointer (!NULL) if an
  265. * entity was ready, but the scheduler had insufficient credits to accommodate
  266. * its job; return NULL, if no ready entity was found.
  267. */
  268. static struct drm_sched_entity *
  269. drm_sched_rq_select_entity_fifo(struct drm_gpu_scheduler *sched,
  270. struct drm_sched_rq *rq)
  271. {
  272. struct rb_node *rb;
  273. spin_lock(&rq->lock);
  274. for (rb = rb_first_cached(&rq->rb_tree_root); rb; rb = rb_next(rb)) {
  275. struct drm_sched_entity *entity;
  276. entity = rb_entry(rb, struct drm_sched_entity, rb_tree_node);
  277. if (drm_sched_entity_is_ready(entity)) {
  278. /* If we can't queue yet, preserve the current entity in
  279. * terms of fairness.
  280. */
  281. if (!drm_sched_can_queue(sched, entity)) {
  282. spin_unlock(&rq->lock);
  283. return ERR_PTR(-ENOSPC);
  284. }
  285. reinit_completion(&entity->entity_idle);
  286. break;
  287. }
  288. }
  289. spin_unlock(&rq->lock);
  290. return rb ? rb_entry(rb, struct drm_sched_entity, rb_tree_node) : NULL;
  291. }
  292. /**
  293. * drm_sched_run_job_queue - enqueue run-job work
  294. * @sched: scheduler instance
  295. */
  296. static void drm_sched_run_job_queue(struct drm_gpu_scheduler *sched)
  297. {
  298. if (!drm_sched_is_stopped(sched))
  299. queue_work(sched->submit_wq, &sched->work_run_job);
  300. }
  301. /**
  302. * drm_sched_run_free_queue - enqueue free-job work
  303. * @sched: scheduler instance
  304. */
  305. static void drm_sched_run_free_queue(struct drm_gpu_scheduler *sched)
  306. {
  307. if (!drm_sched_is_stopped(sched))
  308. queue_work(sched->submit_wq, &sched->work_free_job);
  309. }
  310. /**
  311. * drm_sched_job_done - complete a job
  312. * @s_job: pointer to the job which is done
  313. * @result: 0 on success, -ERRNO on error
  314. *
  315. * Finish the job's fence and resubmit the work items.
  316. */
  317. static void drm_sched_job_done(struct drm_sched_job *s_job, int result)
  318. {
  319. struct drm_sched_fence *s_fence = s_job->s_fence;
  320. struct drm_gpu_scheduler *sched = s_fence->sched;
  321. atomic_sub(s_job->credits, &sched->credit_count);
  322. atomic_dec(sched->score);
  323. trace_drm_sched_job_done(s_fence);
  324. dma_fence_get(&s_fence->finished);
  325. drm_sched_fence_finished(s_fence, result);
  326. dma_fence_put(&s_fence->finished);
  327. drm_sched_run_free_queue(sched);
  328. }
  329. /**
  330. * drm_sched_job_done_cb - the callback for a done job
  331. * @f: fence
  332. * @cb: fence callbacks
  333. */
  334. static void drm_sched_job_done_cb(struct dma_fence *f, struct dma_fence_cb *cb)
  335. {
  336. struct drm_sched_job *s_job = container_of(cb, struct drm_sched_job, cb);
  337. drm_sched_job_done(s_job, f->error);
  338. }
  339. /**
  340. * drm_sched_start_timeout - start timeout for reset worker
  341. *
  342. * @sched: scheduler instance to start the worker for
  343. *
  344. * Start the timeout for the given scheduler.
  345. */
  346. static void drm_sched_start_timeout(struct drm_gpu_scheduler *sched)
  347. {
  348. lockdep_assert_held(&sched->job_list_lock);
  349. if (sched->timeout != MAX_SCHEDULE_TIMEOUT &&
  350. !list_empty(&sched->pending_list))
  351. mod_delayed_work(sched->timeout_wq, &sched->work_tdr, sched->timeout);
  352. }
  353. static void drm_sched_start_timeout_unlocked(struct drm_gpu_scheduler *sched)
  354. {
  355. spin_lock(&sched->job_list_lock);
  356. drm_sched_start_timeout(sched);
  357. spin_unlock(&sched->job_list_lock);
  358. }
  359. /**
  360. * drm_sched_tdr_queue_imm: - immediately start job timeout handler
  361. *
  362. * @sched: scheduler for which the timeout handling should be started.
  363. *
  364. * Start timeout handling immediately for the named scheduler.
  365. */
  366. void drm_sched_tdr_queue_imm(struct drm_gpu_scheduler *sched)
  367. {
  368. spin_lock(&sched->job_list_lock);
  369. sched->timeout = 0;
  370. drm_sched_start_timeout(sched);
  371. spin_unlock(&sched->job_list_lock);
  372. }
  373. EXPORT_SYMBOL(drm_sched_tdr_queue_imm);
  374. /**
  375. * drm_sched_fault - immediately start timeout handler
  376. *
  377. * @sched: scheduler where the timeout handling should be started.
  378. *
  379. * Start timeout handling immediately when the driver detects a hardware fault.
  380. */
  381. void drm_sched_fault(struct drm_gpu_scheduler *sched)
  382. {
  383. if (sched->timeout_wq)
  384. mod_delayed_work(sched->timeout_wq, &sched->work_tdr, 0);
  385. }
  386. EXPORT_SYMBOL(drm_sched_fault);
  387. /**
  388. * drm_sched_suspend_timeout - Suspend scheduler job timeout
  389. *
  390. * @sched: scheduler instance for which to suspend the timeout
  391. *
  392. * Suspend the delayed work timeout for the scheduler. This is done by
  393. * modifying the delayed work timeout to an arbitrary large value,
  394. * MAX_SCHEDULE_TIMEOUT in this case.
  395. *
  396. * Returns the timeout remaining
  397. *
  398. */
  399. unsigned long drm_sched_suspend_timeout(struct drm_gpu_scheduler *sched)
  400. {
  401. unsigned long sched_timeout, now = jiffies;
  402. sched_timeout = sched->work_tdr.timer.expires;
  403. /*
  404. * Modify the timeout to an arbitrarily large value. This also prevents
  405. * the timeout to be restarted when new submissions arrive
  406. */
  407. if (mod_delayed_work(sched->timeout_wq, &sched->work_tdr, MAX_SCHEDULE_TIMEOUT)
  408. && time_after(sched_timeout, now))
  409. return sched_timeout - now;
  410. else
  411. return sched->timeout;
  412. }
  413. EXPORT_SYMBOL(drm_sched_suspend_timeout);
  414. /**
  415. * drm_sched_resume_timeout - Resume scheduler job timeout
  416. *
  417. * @sched: scheduler instance for which to resume the timeout
  418. * @remaining: remaining timeout
  419. *
  420. * Resume the delayed work timeout for the scheduler.
  421. */
  422. void drm_sched_resume_timeout(struct drm_gpu_scheduler *sched,
  423. unsigned long remaining)
  424. {
  425. spin_lock(&sched->job_list_lock);
  426. if (list_empty(&sched->pending_list))
  427. cancel_delayed_work(&sched->work_tdr);
  428. else
  429. mod_delayed_work(sched->timeout_wq, &sched->work_tdr, remaining);
  430. spin_unlock(&sched->job_list_lock);
  431. }
  432. EXPORT_SYMBOL(drm_sched_resume_timeout);
  433. static void drm_sched_job_begin(struct drm_sched_job *s_job)
  434. {
  435. struct drm_gpu_scheduler *sched = s_job->sched;
  436. spin_lock(&sched->job_list_lock);
  437. list_add_tail(&s_job->list, &sched->pending_list);
  438. drm_sched_start_timeout(sched);
  439. spin_unlock(&sched->job_list_lock);
  440. }
  441. /**
  442. * drm_sched_job_reinsert_on_false_timeout - reinsert the job on a false timeout
  443. * @sched: scheduler instance
  444. * @job: job to be reinserted on the pending list
  445. *
  446. * In the case of a "false timeout" - when a timeout occurs but the GPU isn't
  447. * hung and is making progress, the scheduler must reinsert the job back into
  448. * @sched->pending_list. Otherwise, the job and its resources won't be freed
  449. * through the &struct drm_sched_backend_ops.free_job callback.
  450. *
  451. * This function must be used in "false timeout" cases only.
  452. */
  453. static void drm_sched_job_reinsert_on_false_timeout(struct drm_gpu_scheduler *sched,
  454. struct drm_sched_job *job)
  455. {
  456. spin_lock(&sched->job_list_lock);
  457. list_add(&job->list, &sched->pending_list);
  458. /* After reinserting the job, the scheduler enqueues the free-job work
  459. * again if ready. Otherwise, a signaled job could be added to the
  460. * pending list, but never freed.
  461. */
  462. drm_sched_run_free_queue(sched);
  463. spin_unlock(&sched->job_list_lock);
  464. }
  465. static void drm_sched_job_timedout(struct work_struct *work)
  466. {
  467. struct drm_gpu_scheduler *sched;
  468. struct drm_sched_job *job;
  469. enum drm_gpu_sched_stat status = DRM_GPU_SCHED_STAT_RESET;
  470. sched = container_of(work, struct drm_gpu_scheduler, work_tdr.work);
  471. /* Protects against concurrent deletion in drm_sched_get_finished_job */
  472. spin_lock(&sched->job_list_lock);
  473. job = list_first_entry_or_null(&sched->pending_list,
  474. struct drm_sched_job, list);
  475. if (job) {
  476. /*
  477. * Remove the bad job so it cannot be freed by a concurrent
  478. * &struct drm_sched_backend_ops.free_job. It will be
  479. * reinserted after the scheduler's work items have been
  480. * cancelled, at which point it's safe.
  481. */
  482. list_del_init(&job->list);
  483. spin_unlock(&sched->job_list_lock);
  484. status = job->sched->ops->timedout_job(job);
  485. /*
  486. * Guilty job did complete and hence needs to be manually removed
  487. * See drm_sched_stop doc.
  488. */
  489. if (sched->free_guilty) {
  490. job->sched->ops->free_job(job);
  491. sched->free_guilty = false;
  492. }
  493. if (status == DRM_GPU_SCHED_STAT_NO_HANG)
  494. drm_sched_job_reinsert_on_false_timeout(sched, job);
  495. } else {
  496. spin_unlock(&sched->job_list_lock);
  497. }
  498. if (status != DRM_GPU_SCHED_STAT_ENODEV)
  499. drm_sched_start_timeout_unlocked(sched);
  500. }
  501. /**
  502. * drm_sched_stop - stop the scheduler
  503. *
  504. * @sched: scheduler instance
  505. * @bad: job which caused the time out
  506. *
  507. * Stop the scheduler and also removes and frees all completed jobs.
  508. * Note: bad job will not be freed as it might be used later and so it's
  509. * callers responsibility to release it manually if it's not part of the
  510. * pending list any more.
  511. *
  512. * This function is typically used for reset recovery (see the docu of
  513. * drm_sched_backend_ops.timedout_job() for details). Do not call it for
  514. * scheduler teardown, i.e., before calling drm_sched_fini().
  515. *
  516. * As it's only used for reset recovery, drivers must not call this function
  517. * in their &struct drm_sched_backend_ops.timedout_job callback when they
  518. * skip a reset using &enum drm_gpu_sched_stat.DRM_GPU_SCHED_STAT_NO_HANG.
  519. */
  520. void drm_sched_stop(struct drm_gpu_scheduler *sched, struct drm_sched_job *bad)
  521. {
  522. struct drm_sched_job *s_job, *tmp;
  523. drm_sched_wqueue_stop(sched);
  524. /*
  525. * Reinsert back the bad job here - now it's safe as
  526. * drm_sched_get_finished_job() cannot race against us and release the
  527. * bad job at this point - we parked (waited for) any in progress
  528. * (earlier) cleanups and drm_sched_get_finished_job() will not be
  529. * called now until the scheduler's work items are submitted again.
  530. */
  531. if (bad && bad->sched == sched)
  532. /*
  533. * Add at the head of the queue to reflect it was the earliest
  534. * job extracted.
  535. */
  536. list_add(&bad->list, &sched->pending_list);
  537. /*
  538. * Iterate the job list from later to earlier one and either deactive
  539. * their HW callbacks or remove them from pending list if they already
  540. * signaled.
  541. * This iteration is thread safe as the scheduler's work items have been
  542. * cancelled.
  543. */
  544. list_for_each_entry_safe_reverse(s_job, tmp, &sched->pending_list,
  545. list) {
  546. if (s_job->s_fence->parent &&
  547. dma_fence_remove_callback(s_job->s_fence->parent,
  548. &s_job->cb)) {
  549. dma_fence_put(s_job->s_fence->parent);
  550. s_job->s_fence->parent = NULL;
  551. atomic_sub(s_job->credits, &sched->credit_count);
  552. } else {
  553. /*
  554. * remove job from pending_list.
  555. * Locking here is for concurrent resume timeout
  556. */
  557. spin_lock(&sched->job_list_lock);
  558. list_del_init(&s_job->list);
  559. spin_unlock(&sched->job_list_lock);
  560. /*
  561. * Wait for job's HW fence callback to finish using s_job
  562. * before releasing it.
  563. *
  564. * Job is still alive so fence refcount at least 1
  565. */
  566. dma_fence_wait(&s_job->s_fence->finished, false);
  567. /*
  568. * We must keep bad job alive for later use during
  569. * recovery by some of the drivers but leave a hint
  570. * that the guilty job must be released.
  571. */
  572. if (bad != s_job)
  573. sched->ops->free_job(s_job);
  574. else
  575. sched->free_guilty = true;
  576. }
  577. }
  578. /*
  579. * Stop pending timer in flight as we rearm it in drm_sched_start. This
  580. * avoids the pending timeout work in progress to fire right away after
  581. * this TDR finished and before the newly restarted jobs had a
  582. * chance to complete.
  583. */
  584. cancel_delayed_work(&sched->work_tdr);
  585. }
  586. EXPORT_SYMBOL(drm_sched_stop);
  587. /**
  588. * drm_sched_start - recover jobs after a reset
  589. *
  590. * @sched: scheduler instance
  591. * @errno: error to set on the pending fences
  592. *
  593. * This function is typically used for reset recovery (see the docu of
  594. * drm_sched_backend_ops.timedout_job() for details). Do not call it for
  595. * scheduler startup. The scheduler itself is fully operational after
  596. * drm_sched_init() succeeded.
  597. *
  598. * As it's only used for reset recovery, drivers must not call this function
  599. * in their &struct drm_sched_backend_ops.timedout_job callback when they
  600. * skip a reset using &enum drm_gpu_sched_stat.DRM_GPU_SCHED_STAT_NO_HANG.
  601. */
  602. void drm_sched_start(struct drm_gpu_scheduler *sched, int errno)
  603. {
  604. struct drm_sched_job *s_job, *tmp;
  605. /*
  606. * Locking the list is not required here as the scheduler's work items
  607. * are currently not running, so no new jobs are being inserted or
  608. * removed. Also concurrent GPU recovers can't run in parallel.
  609. */
  610. list_for_each_entry_safe(s_job, tmp, &sched->pending_list, list) {
  611. struct dma_fence *fence = s_job->s_fence->parent;
  612. atomic_add(s_job->credits, &sched->credit_count);
  613. if (!fence) {
  614. drm_sched_job_done(s_job, errno ?: -ECANCELED);
  615. continue;
  616. }
  617. if (dma_fence_add_callback(fence, &s_job->cb,
  618. drm_sched_job_done_cb))
  619. drm_sched_job_done(s_job, fence->error ?: errno);
  620. }
  621. drm_sched_start_timeout_unlocked(sched);
  622. drm_sched_wqueue_start(sched);
  623. }
  624. EXPORT_SYMBOL(drm_sched_start);
  625. /**
  626. * drm_sched_resubmit_jobs - Deprecated, don't use in new code!
  627. *
  628. * @sched: scheduler instance
  629. *
  630. * Re-submitting jobs was a concept AMD came up as cheap way to implement
  631. * recovery after a job timeout.
  632. *
  633. * This turned out to be not working very well. First of all there are many
  634. * problem with the dma_fence implementation and requirements. Either the
  635. * implementation is risking deadlocks with core memory management or violating
  636. * documented implementation details of the dma_fence object.
  637. *
  638. * Drivers can still save and restore their state for recovery operations, but
  639. * we shouldn't make this a general scheduler feature around the dma_fence
  640. * interface. The suggested driver-side replacement is to use
  641. * drm_sched_for_each_pending_job() after stopping the scheduler and implement
  642. * their own recovery operations.
  643. */
  644. void drm_sched_resubmit_jobs(struct drm_gpu_scheduler *sched)
  645. {
  646. struct drm_sched_job *s_job, *tmp;
  647. uint64_t guilty_context;
  648. bool found_guilty = false;
  649. struct dma_fence *fence;
  650. list_for_each_entry_safe(s_job, tmp, &sched->pending_list, list) {
  651. struct drm_sched_fence *s_fence = s_job->s_fence;
  652. if (!found_guilty && atomic_read(&s_job->karma) > sched->hang_limit) {
  653. found_guilty = true;
  654. guilty_context = s_job->s_fence->scheduled.context;
  655. }
  656. if (found_guilty && s_job->s_fence->scheduled.context == guilty_context)
  657. dma_fence_set_error(&s_fence->finished, -ECANCELED);
  658. fence = sched->ops->run_job(s_job);
  659. if (IS_ERR_OR_NULL(fence)) {
  660. if (IS_ERR(fence))
  661. dma_fence_set_error(&s_fence->finished, PTR_ERR(fence));
  662. s_job->s_fence->parent = NULL;
  663. } else {
  664. s_job->s_fence->parent = dma_fence_get(fence);
  665. /* Drop for orignal kref_init */
  666. dma_fence_put(fence);
  667. }
  668. }
  669. }
  670. EXPORT_SYMBOL(drm_sched_resubmit_jobs);
  671. /**
  672. * drm_sched_job_init - init a scheduler job
  673. * @job: scheduler job to init
  674. * @entity: scheduler entity to use
  675. * @credits: the number of credits this job contributes to the schedulers
  676. * credit limit
  677. * @owner: job owner for debugging
  678. * @drm_client_id: &struct drm_file.client_id of the owner (used by trace
  679. * events)
  680. *
  681. * Refer to drm_sched_entity_push_job() documentation
  682. * for locking considerations.
  683. *
  684. * Drivers must make sure drm_sched_job_cleanup() if this function returns
  685. * successfully, even when @job is aborted before drm_sched_job_arm() is called.
  686. *
  687. * Note that this function does not assign a valid value to each struct member
  688. * of struct drm_sched_job. Take a look at that struct's documentation to see
  689. * who sets which struct member with what lifetime.
  690. *
  691. * WARNING: amdgpu abuses &drm_sched.ready to signal when the hardware
  692. * has died, which can mean that there's no valid runqueue for a @entity.
  693. * This function returns -ENOENT in this case (which probably should be -EIO as
  694. * a more meanigful return value).
  695. *
  696. * Returns 0 for success, negative error code otherwise.
  697. */
  698. int drm_sched_job_init(struct drm_sched_job *job,
  699. struct drm_sched_entity *entity,
  700. u32 credits, void *owner,
  701. uint64_t drm_client_id)
  702. {
  703. if (!entity->rq) {
  704. /* This will most likely be followed by missing frames
  705. * or worse--a blank screen--leave a trail in the
  706. * logs, so this can be debugged easier.
  707. */
  708. dev_err(job->sched->dev, "%s: entity has no rq!\n", __func__);
  709. return -ENOENT;
  710. }
  711. if (unlikely(!credits)) {
  712. pr_err("*ERROR* %s: credits cannot be 0!\n", __func__);
  713. return -EINVAL;
  714. }
  715. /*
  716. * We don't know for sure how the user has allocated. Thus, zero the
  717. * struct so that unallowed (i.e., too early) usage of pointers that
  718. * this function does not set is guaranteed to lead to a NULL pointer
  719. * exception instead of UB.
  720. */
  721. memset(job, 0, sizeof(*job));
  722. job->entity = entity;
  723. job->credits = credits;
  724. job->s_fence = drm_sched_fence_alloc(entity, owner, drm_client_id);
  725. if (!job->s_fence)
  726. return -ENOMEM;
  727. INIT_LIST_HEAD(&job->list);
  728. xa_init_flags(&job->dependencies, XA_FLAGS_ALLOC);
  729. return 0;
  730. }
  731. EXPORT_SYMBOL(drm_sched_job_init);
  732. /**
  733. * drm_sched_job_arm - arm a scheduler job for execution
  734. * @job: scheduler job to arm
  735. *
  736. * This arms a scheduler job for execution. Specifically it initializes the
  737. * &drm_sched_job.s_fence of @job, so that it can be attached to struct dma_resv
  738. * or other places that need to track the completion of this job. It also
  739. * initializes sequence numbers, which are fundamental for fence ordering.
  740. *
  741. * Refer to drm_sched_entity_push_job() documentation for locking
  742. * considerations.
  743. *
  744. * Once this function was called, you *must* submit @job with
  745. * drm_sched_entity_push_job().
  746. *
  747. * This can only be called if drm_sched_job_init() succeeded.
  748. */
  749. void drm_sched_job_arm(struct drm_sched_job *job)
  750. {
  751. struct drm_gpu_scheduler *sched;
  752. struct drm_sched_entity *entity = job->entity;
  753. BUG_ON(!entity);
  754. drm_sched_entity_select_rq(entity);
  755. sched = entity->rq->sched;
  756. job->sched = sched;
  757. job->s_priority = entity->priority;
  758. drm_sched_fence_init(job->s_fence, job->entity);
  759. }
  760. EXPORT_SYMBOL(drm_sched_job_arm);
  761. /**
  762. * drm_sched_job_add_dependency - adds the fence as a job dependency
  763. * @job: scheduler job to add the dependencies to
  764. * @fence: the dma_fence to add to the list of dependencies.
  765. *
  766. * Note that @fence is consumed in both the success and error cases.
  767. *
  768. * Returns:
  769. * 0 on success, or an error on failing to expand the array.
  770. */
  771. int drm_sched_job_add_dependency(struct drm_sched_job *job,
  772. struct dma_fence *fence)
  773. {
  774. struct dma_fence *entry;
  775. unsigned long index;
  776. u32 id = 0;
  777. int ret;
  778. if (!fence)
  779. return 0;
  780. /* Deduplicate if we already depend on a fence from the same context.
  781. * This lets the size of the array of deps scale with the number of
  782. * engines involved, rather than the number of BOs.
  783. */
  784. xa_for_each(&job->dependencies, index, entry) {
  785. if (entry->context != fence->context)
  786. continue;
  787. if (dma_fence_is_later(fence, entry)) {
  788. dma_fence_put(entry);
  789. xa_store(&job->dependencies, index, fence, GFP_KERNEL);
  790. } else {
  791. dma_fence_put(fence);
  792. }
  793. return 0;
  794. }
  795. ret = xa_alloc(&job->dependencies, &id, fence, xa_limit_32b, GFP_KERNEL);
  796. if (ret != 0)
  797. dma_fence_put(fence);
  798. return ret;
  799. }
  800. EXPORT_SYMBOL(drm_sched_job_add_dependency);
  801. /**
  802. * drm_sched_job_add_syncobj_dependency - adds a syncobj's fence as a job dependency
  803. * @job: scheduler job to add the dependencies to
  804. * @file: drm file private pointer
  805. * @handle: syncobj handle to lookup
  806. * @point: timeline point
  807. *
  808. * This adds the fence matching the given syncobj to @job.
  809. *
  810. * Returns:
  811. * 0 on success, or an error on failing to expand the array.
  812. */
  813. int drm_sched_job_add_syncobj_dependency(struct drm_sched_job *job,
  814. struct drm_file *file,
  815. u32 handle,
  816. u32 point)
  817. {
  818. struct dma_fence *fence;
  819. int ret;
  820. ret = drm_syncobj_find_fence(file, handle, point, 0, &fence);
  821. if (ret)
  822. return ret;
  823. return drm_sched_job_add_dependency(job, fence);
  824. }
  825. EXPORT_SYMBOL(drm_sched_job_add_syncobj_dependency);
  826. /**
  827. * drm_sched_job_add_resv_dependencies - add all fences from the resv to the job
  828. * @job: scheduler job to add the dependencies to
  829. * @resv: the dma_resv object to get the fences from
  830. * @usage: the dma_resv_usage to use to filter the fences
  831. *
  832. * This adds all fences matching the given usage from @resv to @job.
  833. * Must be called with the @resv lock held.
  834. *
  835. * Returns:
  836. * 0 on success, or an error on failing to expand the array.
  837. */
  838. int drm_sched_job_add_resv_dependencies(struct drm_sched_job *job,
  839. struct dma_resv *resv,
  840. enum dma_resv_usage usage)
  841. {
  842. struct dma_resv_iter cursor;
  843. struct dma_fence *fence;
  844. int ret;
  845. dma_resv_assert_held(resv);
  846. dma_resv_for_each_fence(&cursor, resv, usage, fence) {
  847. /*
  848. * As drm_sched_job_add_dependency always consumes the fence
  849. * reference (even when it fails), and dma_resv_for_each_fence
  850. * is not obtaining one, we need to grab one before calling.
  851. */
  852. ret = drm_sched_job_add_dependency(job, dma_fence_get(fence));
  853. if (ret)
  854. return ret;
  855. }
  856. return 0;
  857. }
  858. EXPORT_SYMBOL(drm_sched_job_add_resv_dependencies);
  859. /**
  860. * drm_sched_job_add_implicit_dependencies - adds implicit dependencies as job
  861. * dependencies
  862. * @job: scheduler job to add the dependencies to
  863. * @obj: the gem object to add new dependencies from.
  864. * @write: whether the job might write the object (so we need to depend on
  865. * shared fences in the reservation object).
  866. *
  867. * This should be called after drm_gem_lock_reservations() on your array of
  868. * GEM objects used in the job but before updating the reservations with your
  869. * own fences.
  870. *
  871. * Returns:
  872. * 0 on success, or an error on failing to expand the array.
  873. */
  874. int drm_sched_job_add_implicit_dependencies(struct drm_sched_job *job,
  875. struct drm_gem_object *obj,
  876. bool write)
  877. {
  878. return drm_sched_job_add_resv_dependencies(job, obj->resv,
  879. dma_resv_usage_rw(write));
  880. }
  881. EXPORT_SYMBOL(drm_sched_job_add_implicit_dependencies);
  882. /**
  883. * drm_sched_job_has_dependency - check whether fence is the job's dependency
  884. * @job: scheduler job to check
  885. * @fence: fence to look for
  886. *
  887. * Returns:
  888. * True if @fence is found within the job's dependencies, or otherwise false.
  889. */
  890. bool drm_sched_job_has_dependency(struct drm_sched_job *job,
  891. struct dma_fence *fence)
  892. {
  893. struct dma_fence *f;
  894. unsigned long index;
  895. xa_for_each(&job->dependencies, index, f) {
  896. if (f == fence)
  897. return true;
  898. }
  899. return false;
  900. }
  901. EXPORT_SYMBOL(drm_sched_job_has_dependency);
  902. /**
  903. * drm_sched_job_cleanup - clean up scheduler job resources
  904. * @job: scheduler job to clean up
  905. *
  906. * Cleans up the resources allocated with drm_sched_job_init().
  907. *
  908. * Drivers should call this from their error unwind code if @job is aborted
  909. * before drm_sched_job_arm() is called.
  910. *
  911. * drm_sched_job_arm() is a point of no return since it initializes the fences
  912. * and their sequence number etc. Once that function has been called, you *must*
  913. * submit it with drm_sched_entity_push_job() and cannot simply abort it by
  914. * calling drm_sched_job_cleanup().
  915. *
  916. * This function should be called in the &drm_sched_backend_ops.free_job callback.
  917. */
  918. void drm_sched_job_cleanup(struct drm_sched_job *job)
  919. {
  920. struct dma_fence *fence;
  921. unsigned long index;
  922. if (kref_read(&job->s_fence->finished.refcount)) {
  923. /* The job has been processed by the scheduler, i.e.,
  924. * drm_sched_job_arm() and drm_sched_entity_push_job() have
  925. * been called.
  926. */
  927. dma_fence_put(&job->s_fence->finished);
  928. } else {
  929. /* The job was aborted before it has been committed to be run;
  930. * notably, drm_sched_job_arm() has not been called.
  931. */
  932. drm_sched_fence_free(job->s_fence);
  933. }
  934. job->s_fence = NULL;
  935. xa_for_each(&job->dependencies, index, fence) {
  936. dma_fence_put(fence);
  937. }
  938. xa_destroy(&job->dependencies);
  939. }
  940. EXPORT_SYMBOL(drm_sched_job_cleanup);
  941. /**
  942. * drm_sched_wakeup - Wake up the scheduler if it is ready to queue
  943. * @sched: scheduler instance
  944. *
  945. * Wake up the scheduler if we can queue jobs.
  946. */
  947. void drm_sched_wakeup(struct drm_gpu_scheduler *sched)
  948. {
  949. drm_sched_run_job_queue(sched);
  950. }
  951. /**
  952. * drm_sched_select_entity - Select next entity to process
  953. *
  954. * @sched: scheduler instance
  955. *
  956. * Return an entity to process or NULL if none are found.
  957. *
  958. * Note, that we break out of the for-loop when "entity" is non-null, which can
  959. * also be an error-pointer--this assures we don't process lower priority
  960. * run-queues. See comments in the respectively called functions.
  961. */
  962. static struct drm_sched_entity *
  963. drm_sched_select_entity(struct drm_gpu_scheduler *sched)
  964. {
  965. struct drm_sched_entity *entity;
  966. int i;
  967. /* Start with the highest priority.
  968. */
  969. for (i = DRM_SCHED_PRIORITY_KERNEL; i < sched->num_rqs; i++) {
  970. entity = drm_sched_policy == DRM_SCHED_POLICY_FIFO ?
  971. drm_sched_rq_select_entity_fifo(sched, sched->sched_rq[i]) :
  972. drm_sched_rq_select_entity_rr(sched, sched->sched_rq[i]);
  973. if (entity)
  974. break;
  975. }
  976. return IS_ERR(entity) ? NULL : entity;
  977. }
  978. /**
  979. * drm_sched_get_finished_job - fetch the next finished job to be destroyed
  980. *
  981. * @sched: scheduler instance
  982. * @have_more: are there more finished jobs on the list
  983. *
  984. * Informs the caller through @have_more whether there are more finished jobs
  985. * besides the returned one.
  986. *
  987. * Returns the next finished job from the pending list (if there is one)
  988. * ready for it to be destroyed.
  989. */
  990. static struct drm_sched_job *
  991. drm_sched_get_finished_job(struct drm_gpu_scheduler *sched, bool *have_more)
  992. {
  993. struct drm_sched_job *job, *next;
  994. spin_lock(&sched->job_list_lock);
  995. job = list_first_entry_or_null(&sched->pending_list,
  996. struct drm_sched_job, list);
  997. if (job && dma_fence_is_signaled(&job->s_fence->finished)) {
  998. /* remove job from pending_list */
  999. list_del_init(&job->list);
  1000. /* cancel this job's TO timer */
  1001. cancel_delayed_work(&sched->work_tdr);
  1002. *have_more = false;
  1003. next = list_first_entry_or_null(&sched->pending_list,
  1004. typeof(*next), list);
  1005. if (next) {
  1006. /* make the scheduled timestamp more accurate */
  1007. if (test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT,
  1008. &next->s_fence->scheduled.flags))
  1009. next->s_fence->scheduled.timestamp =
  1010. dma_fence_timestamp(&job->s_fence->finished);
  1011. *have_more = dma_fence_is_signaled(&next->s_fence->finished);
  1012. /* start TO timer for next job */
  1013. drm_sched_start_timeout(sched);
  1014. }
  1015. } else {
  1016. job = NULL;
  1017. }
  1018. spin_unlock(&sched->job_list_lock);
  1019. return job;
  1020. }
  1021. /**
  1022. * drm_sched_pick_best - Get a drm sched from a sched_list with the least load
  1023. * @sched_list: list of drm_gpu_schedulers
  1024. * @num_sched_list: number of drm_gpu_schedulers in the sched_list
  1025. *
  1026. * Returns pointer of the sched with the least load or NULL if none of the
  1027. * drm_gpu_schedulers are ready
  1028. */
  1029. struct drm_gpu_scheduler *
  1030. drm_sched_pick_best(struct drm_gpu_scheduler **sched_list,
  1031. unsigned int num_sched_list)
  1032. {
  1033. struct drm_gpu_scheduler *sched, *picked_sched = NULL;
  1034. int i;
  1035. unsigned int min_score = UINT_MAX, num_score;
  1036. for (i = 0; i < num_sched_list; ++i) {
  1037. sched = sched_list[i];
  1038. if (!sched->ready) {
  1039. DRM_WARN("scheduler %s is not ready, skipping",
  1040. sched->name);
  1041. continue;
  1042. }
  1043. num_score = atomic_read(sched->score);
  1044. if (num_score < min_score) {
  1045. min_score = num_score;
  1046. picked_sched = sched;
  1047. }
  1048. }
  1049. return picked_sched;
  1050. }
  1051. EXPORT_SYMBOL(drm_sched_pick_best);
  1052. /**
  1053. * drm_sched_free_job_work - worker to call free_job
  1054. *
  1055. * @w: free job work
  1056. */
  1057. static void drm_sched_free_job_work(struct work_struct *w)
  1058. {
  1059. struct drm_gpu_scheduler *sched =
  1060. container_of(w, struct drm_gpu_scheduler, work_free_job);
  1061. struct drm_sched_job *job;
  1062. bool have_more;
  1063. job = drm_sched_get_finished_job(sched, &have_more);
  1064. if (job) {
  1065. sched->ops->free_job(job);
  1066. if (have_more)
  1067. drm_sched_run_free_queue(sched);
  1068. }
  1069. drm_sched_run_job_queue(sched);
  1070. }
  1071. /**
  1072. * drm_sched_run_job_work - worker to call run_job
  1073. *
  1074. * @w: run job work
  1075. */
  1076. static void drm_sched_run_job_work(struct work_struct *w)
  1077. {
  1078. struct drm_gpu_scheduler *sched =
  1079. container_of(w, struct drm_gpu_scheduler, work_run_job);
  1080. struct drm_sched_entity *entity;
  1081. struct dma_fence *fence;
  1082. struct drm_sched_fence *s_fence;
  1083. struct drm_sched_job *sched_job;
  1084. int r;
  1085. /* Find entity with a ready job */
  1086. entity = drm_sched_select_entity(sched);
  1087. if (!entity) {
  1088. /*
  1089. * Either no more work to do, or the next ready job needs more
  1090. * credits than the scheduler has currently available.
  1091. */
  1092. return;
  1093. }
  1094. sched_job = drm_sched_entity_pop_job(entity);
  1095. if (!sched_job) {
  1096. complete_all(&entity->entity_idle);
  1097. drm_sched_run_job_queue(sched);
  1098. return;
  1099. }
  1100. s_fence = sched_job->s_fence;
  1101. atomic_add(sched_job->credits, &sched->credit_count);
  1102. drm_sched_job_begin(sched_job);
  1103. trace_drm_sched_job_run(sched_job, entity);
  1104. /*
  1105. * The run_job() callback must by definition return a fence whose
  1106. * refcount has been incremented for the scheduler already.
  1107. */
  1108. fence = sched->ops->run_job(sched_job);
  1109. complete_all(&entity->entity_idle);
  1110. drm_sched_fence_scheduled(s_fence, fence);
  1111. if (!IS_ERR_OR_NULL(fence)) {
  1112. r = dma_fence_add_callback(fence, &sched_job->cb,
  1113. drm_sched_job_done_cb);
  1114. if (r == -ENOENT)
  1115. drm_sched_job_done(sched_job, fence->error);
  1116. else if (r)
  1117. DRM_DEV_ERROR(sched->dev, "fence add callback failed (%d)\n", r);
  1118. dma_fence_put(fence);
  1119. } else {
  1120. drm_sched_job_done(sched_job, IS_ERR(fence) ?
  1121. PTR_ERR(fence) : 0);
  1122. }
  1123. wake_up(&sched->job_scheduled);
  1124. drm_sched_run_job_queue(sched);
  1125. }
  1126. static struct workqueue_struct *drm_sched_alloc_wq(const char *name)
  1127. {
  1128. #if (IS_ENABLED(CONFIG_LOCKDEP))
  1129. static struct lockdep_map map = {
  1130. .name = "drm_sched_lockdep_map"
  1131. };
  1132. /*
  1133. * Avoid leaking a lockdep map on each drm sched creation and
  1134. * destruction by using a single lockdep map for all drm sched
  1135. * allocated submit_wq.
  1136. */
  1137. return alloc_ordered_workqueue_lockdep_map(name, WQ_MEM_RECLAIM, &map);
  1138. #else
  1139. return alloc_ordered_workqueue(name, WQ_MEM_RECLAIM);
  1140. #endif
  1141. }
  1142. /**
  1143. * drm_sched_init - Init a gpu scheduler instance
  1144. *
  1145. * @sched: scheduler instance
  1146. * @args: scheduler initialization arguments
  1147. *
  1148. * Return 0 on success, otherwise error code.
  1149. */
  1150. int drm_sched_init(struct drm_gpu_scheduler *sched, const struct drm_sched_init_args *args)
  1151. {
  1152. int i;
  1153. sched->ops = args->ops;
  1154. sched->credit_limit = args->credit_limit;
  1155. sched->name = args->name;
  1156. sched->timeout = args->timeout;
  1157. sched->hang_limit = args->hang_limit;
  1158. sched->timeout_wq = args->timeout_wq ? args->timeout_wq : system_percpu_wq;
  1159. sched->score = args->score ? args->score : &sched->_score;
  1160. sched->dev = args->dev;
  1161. if (args->num_rqs > DRM_SCHED_PRIORITY_COUNT) {
  1162. /* This is a gross violation--tell drivers what the problem is.
  1163. */
  1164. dev_err(sched->dev, "%s: num_rqs cannot be greater than DRM_SCHED_PRIORITY_COUNT\n",
  1165. __func__);
  1166. return -EINVAL;
  1167. } else if (sched->sched_rq) {
  1168. /* Not an error, but warn anyway so drivers can
  1169. * fine-tune their DRM calling order, and return all
  1170. * is good.
  1171. */
  1172. dev_warn(sched->dev, "%s: scheduler already initialized!\n", __func__);
  1173. return 0;
  1174. }
  1175. if (args->submit_wq) {
  1176. sched->submit_wq = args->submit_wq;
  1177. sched->own_submit_wq = false;
  1178. } else {
  1179. sched->submit_wq = drm_sched_alloc_wq(args->name);
  1180. if (!sched->submit_wq)
  1181. return -ENOMEM;
  1182. sched->own_submit_wq = true;
  1183. }
  1184. sched->sched_rq = kmalloc_objs(*sched->sched_rq, args->num_rqs,
  1185. GFP_KERNEL | __GFP_ZERO);
  1186. if (!sched->sched_rq)
  1187. goto Out_check_own;
  1188. sched->num_rqs = args->num_rqs;
  1189. for (i = DRM_SCHED_PRIORITY_KERNEL; i < sched->num_rqs; i++) {
  1190. sched->sched_rq[i] = kzalloc_obj(*sched->sched_rq[i]);
  1191. if (!sched->sched_rq[i])
  1192. goto Out_unroll;
  1193. drm_sched_rq_init(sched, sched->sched_rq[i]);
  1194. }
  1195. init_waitqueue_head(&sched->job_scheduled);
  1196. INIT_LIST_HEAD(&sched->pending_list);
  1197. spin_lock_init(&sched->job_list_lock);
  1198. atomic_set(&sched->credit_count, 0);
  1199. INIT_DELAYED_WORK(&sched->work_tdr, drm_sched_job_timedout);
  1200. INIT_WORK(&sched->work_run_job, drm_sched_run_job_work);
  1201. INIT_WORK(&sched->work_free_job, drm_sched_free_job_work);
  1202. atomic_set(&sched->_score, 0);
  1203. atomic64_set(&sched->job_id_count, 0);
  1204. sched->pause_submit = false;
  1205. sched->ready = true;
  1206. return 0;
  1207. Out_unroll:
  1208. for (--i ; i >= DRM_SCHED_PRIORITY_KERNEL; i--)
  1209. kfree(sched->sched_rq[i]);
  1210. kfree(sched->sched_rq);
  1211. sched->sched_rq = NULL;
  1212. Out_check_own:
  1213. if (sched->own_submit_wq)
  1214. destroy_workqueue(sched->submit_wq);
  1215. dev_err(sched->dev, "%s: Failed to setup GPU scheduler--out of memory\n", __func__);
  1216. return -ENOMEM;
  1217. }
  1218. EXPORT_SYMBOL(drm_sched_init);
  1219. static void drm_sched_cancel_remaining_jobs(struct drm_gpu_scheduler *sched)
  1220. {
  1221. struct drm_sched_job *job, *tmp;
  1222. /* All other accessors are stopped. No locking necessary. */
  1223. list_for_each_entry_safe_reverse(job, tmp, &sched->pending_list, list) {
  1224. sched->ops->cancel_job(job);
  1225. list_del(&job->list);
  1226. sched->ops->free_job(job);
  1227. }
  1228. }
  1229. /**
  1230. * drm_sched_fini - Destroy a gpu scheduler
  1231. *
  1232. * @sched: scheduler instance
  1233. *
  1234. * Tears down and cleans up the scheduler.
  1235. *
  1236. * This stops submission of new jobs to the hardware through &struct
  1237. * drm_sched_backend_ops.run_job. If &struct drm_sched_backend_ops.cancel_job
  1238. * is implemented, all jobs will be canceled through it and afterwards cleaned
  1239. * up through &struct drm_sched_backend_ops.free_job. If cancel_job is not
  1240. * implemented, memory could leak.
  1241. */
  1242. void drm_sched_fini(struct drm_gpu_scheduler *sched)
  1243. {
  1244. struct drm_sched_entity *s_entity;
  1245. int i;
  1246. drm_sched_wqueue_stop(sched);
  1247. for (i = DRM_SCHED_PRIORITY_KERNEL; i < sched->num_rqs; i++) {
  1248. struct drm_sched_rq *rq = sched->sched_rq[i];
  1249. spin_lock(&rq->lock);
  1250. list_for_each_entry(s_entity, &rq->entities, list) {
  1251. /*
  1252. * Prevents reinsertion and marks job_queue as idle,
  1253. * it will be removed from the rq in drm_sched_entity_fini()
  1254. * eventually
  1255. *
  1256. * FIXME:
  1257. * This lacks the proper spin_lock(&s_entity->lock) and
  1258. * is, therefore, a race condition. Most notably, it
  1259. * can race with drm_sched_entity_push_job(). The lock
  1260. * cannot be taken here, however, because this would
  1261. * lead to lock inversion -> deadlock.
  1262. *
  1263. * The best solution probably is to enforce the life
  1264. * time rule of all entities having to be torn down
  1265. * before their scheduler. Then, however, locking could
  1266. * be dropped alltogether from this function.
  1267. *
  1268. * For now, this remains a potential race in all
  1269. * drivers that keep entities alive for longer than
  1270. * the scheduler.
  1271. *
  1272. * The READ_ONCE() is there to make the lockless read
  1273. * (warning about the lockless write below) slightly
  1274. * less broken...
  1275. */
  1276. if (!READ_ONCE(s_entity->stopped))
  1277. dev_warn(sched->dev, "Tearing down scheduler with active entities!\n");
  1278. s_entity->stopped = true;
  1279. }
  1280. spin_unlock(&rq->lock);
  1281. kfree(sched->sched_rq[i]);
  1282. }
  1283. /* Wakeup everyone stuck in drm_sched_entity_flush for this scheduler */
  1284. wake_up_all(&sched->job_scheduled);
  1285. /* Confirm no work left behind accessing device structures */
  1286. cancel_delayed_work_sync(&sched->work_tdr);
  1287. /* Avoid memory leaks if supported by the driver. */
  1288. if (sched->ops->cancel_job)
  1289. drm_sched_cancel_remaining_jobs(sched);
  1290. if (sched->own_submit_wq)
  1291. destroy_workqueue(sched->submit_wq);
  1292. sched->ready = false;
  1293. kfree(sched->sched_rq);
  1294. sched->sched_rq = NULL;
  1295. if (!list_empty(&sched->pending_list))
  1296. dev_warn(sched->dev, "Tearing down scheduler while jobs are pending!\n");
  1297. }
  1298. EXPORT_SYMBOL(drm_sched_fini);
  1299. /**
  1300. * drm_sched_increase_karma - Update sched_entity guilty flag
  1301. *
  1302. * @bad: The job guilty of time out
  1303. *
  1304. * Increment on every hang caused by the 'bad' job. If this exceeds the hang
  1305. * limit of the scheduler then the respective sched entity is marked guilty and
  1306. * jobs from it will not be scheduled further
  1307. */
  1308. void drm_sched_increase_karma(struct drm_sched_job *bad)
  1309. {
  1310. int i;
  1311. struct drm_sched_entity *tmp;
  1312. struct drm_sched_entity *entity;
  1313. struct drm_gpu_scheduler *sched = bad->sched;
  1314. /* don't change @bad's karma if it's from KERNEL RQ,
  1315. * because sometimes GPU hang would cause kernel jobs (like VM updating jobs)
  1316. * corrupt but keep in mind that kernel jobs always considered good.
  1317. */
  1318. if (bad->s_priority != DRM_SCHED_PRIORITY_KERNEL) {
  1319. atomic_inc(&bad->karma);
  1320. for (i = DRM_SCHED_PRIORITY_HIGH; i < sched->num_rqs; i++) {
  1321. struct drm_sched_rq *rq = sched->sched_rq[i];
  1322. spin_lock(&rq->lock);
  1323. list_for_each_entry_safe(entity, tmp, &rq->entities, list) {
  1324. if (bad->s_fence->scheduled.context ==
  1325. entity->fence_context) {
  1326. if (entity->guilty)
  1327. atomic_set(entity->guilty, 1);
  1328. break;
  1329. }
  1330. }
  1331. spin_unlock(&rq->lock);
  1332. if (&entity->list != &rq->entities)
  1333. break;
  1334. }
  1335. }
  1336. }
  1337. EXPORT_SYMBOL(drm_sched_increase_karma);
  1338. /**
  1339. * drm_sched_wqueue_ready - Is the scheduler ready for submission
  1340. *
  1341. * @sched: scheduler instance
  1342. *
  1343. * Returns true if submission is ready
  1344. */
  1345. bool drm_sched_wqueue_ready(struct drm_gpu_scheduler *sched)
  1346. {
  1347. return sched->ready;
  1348. }
  1349. EXPORT_SYMBOL(drm_sched_wqueue_ready);
  1350. /**
  1351. * drm_sched_wqueue_stop - stop scheduler submission
  1352. * @sched: scheduler instance
  1353. *
  1354. * Stops the scheduler from pulling new jobs from entities. It also stops
  1355. * freeing jobs automatically through drm_sched_backend_ops.free_job().
  1356. */
  1357. void drm_sched_wqueue_stop(struct drm_gpu_scheduler *sched)
  1358. {
  1359. WRITE_ONCE(sched->pause_submit, true);
  1360. cancel_work_sync(&sched->work_run_job);
  1361. cancel_work_sync(&sched->work_free_job);
  1362. }
  1363. EXPORT_SYMBOL(drm_sched_wqueue_stop);
  1364. /**
  1365. * drm_sched_wqueue_start - start scheduler submission
  1366. * @sched: scheduler instance
  1367. *
  1368. * Restarts the scheduler after drm_sched_wqueue_stop() has stopped it.
  1369. *
  1370. * This function is not necessary for 'conventional' startup. The scheduler is
  1371. * fully operational after drm_sched_init() succeeded.
  1372. */
  1373. void drm_sched_wqueue_start(struct drm_gpu_scheduler *sched)
  1374. {
  1375. WRITE_ONCE(sched->pause_submit, false);
  1376. queue_work(sched->submit_wq, &sched->work_run_job);
  1377. queue_work(sched->submit_wq, &sched->work_free_job);
  1378. }
  1379. EXPORT_SYMBOL(drm_sched_wqueue_start);
  1380. /**
  1381. * drm_sched_is_stopped() - Checks whether drm_sched is stopped
  1382. * @sched: DRM scheduler
  1383. *
  1384. * Return: true if sched is stopped, false otherwise
  1385. */
  1386. bool drm_sched_is_stopped(struct drm_gpu_scheduler *sched)
  1387. {
  1388. return READ_ONCE(sched->pause_submit);
  1389. }
  1390. EXPORT_SYMBOL(drm_sched_is_stopped);
  1391. /**
  1392. * drm_sched_job_is_signaled() - DRM scheduler job is signaled
  1393. * @job: DRM scheduler job
  1394. *
  1395. * Determine if DRM scheduler job is signaled. DRM scheduler should be stopped
  1396. * to obtain a stable snapshot of state. Both parent fence (hardware fence) and
  1397. * finished fence (software fence) are checked to determine signaling state.
  1398. *
  1399. * Return: true if job is signaled, false otherwise
  1400. */
  1401. bool drm_sched_job_is_signaled(struct drm_sched_job *job)
  1402. {
  1403. struct drm_sched_fence *s_fence = job->s_fence;
  1404. WARN_ON(!drm_sched_is_stopped(job->sched));
  1405. return (s_fence->parent && dma_fence_is_signaled(s_fence->parent)) ||
  1406. dma_fence_is_signaled(&s_fence->finished);
  1407. }
  1408. EXPORT_SYMBOL(drm_sched_job_is_signaled);