i915_request.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. /*
  2. * Copyright © 2008-2018 Intel Corporation
  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 (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. */
  24. #ifndef I915_REQUEST_H
  25. #define I915_REQUEST_H
  26. #include <linux/dma-fence.h>
  27. #include "i915_gem.h"
  28. #include "i915_scheduler.h"
  29. #include "i915_sw_fence.h"
  30. #include "i915_scheduler.h"
  31. #include <uapi/drm/i915_drm.h>
  32. struct drm_file;
  33. struct drm_i915_gem_object;
  34. struct i915_request;
  35. struct i915_timeline;
  36. struct intel_wait {
  37. struct rb_node node;
  38. struct task_struct *tsk;
  39. struct i915_request *request;
  40. u32 seqno;
  41. };
  42. struct intel_signal_node {
  43. struct intel_wait wait;
  44. struct list_head link;
  45. };
  46. struct i915_capture_list {
  47. struct i915_capture_list *next;
  48. struct i915_vma *vma;
  49. };
  50. /**
  51. * Request queue structure.
  52. *
  53. * The request queue allows us to note sequence numbers that have been emitted
  54. * and may be associated with active buffers to be retired.
  55. *
  56. * By keeping this list, we can avoid having to do questionable sequence
  57. * number comparisons on buffer last_read|write_seqno. It also allows an
  58. * emission time to be associated with the request for tracking how far ahead
  59. * of the GPU the submission is.
  60. *
  61. * When modifying this structure be very aware that we perform a lockless
  62. * RCU lookup of it that may race against reallocation of the struct
  63. * from the slab freelist. We intentionally do not zero the structure on
  64. * allocation so that the lookup can use the dangling pointers (and is
  65. * cogniscent that those pointers may be wrong). Instead, everything that
  66. * needs to be initialised must be done so explicitly.
  67. *
  68. * The requests are reference counted.
  69. */
  70. struct i915_request {
  71. struct dma_fence fence;
  72. spinlock_t lock;
  73. /** On Which ring this request was generated */
  74. struct drm_i915_private *i915;
  75. /**
  76. * Context and ring buffer related to this request
  77. * Contexts are refcounted, so when this request is associated with a
  78. * context, we must increment the context's refcount, to guarantee that
  79. * it persists while any request is linked to it. Requests themselves
  80. * are also refcounted, so the request will only be freed when the last
  81. * reference to it is dismissed, and the code in
  82. * i915_request_free() will then decrement the refcount on the
  83. * context.
  84. */
  85. struct i915_gem_context *gem_context;
  86. struct intel_engine_cs *engine;
  87. struct intel_context *hw_context;
  88. struct intel_ring *ring;
  89. struct i915_timeline *timeline;
  90. struct intel_signal_node signaling;
  91. /*
  92. * The rcu epoch of when this request was allocated. Used to judiciously
  93. * apply backpressure on future allocations to ensure that under
  94. * mempressure there is sufficient RCU ticks for us to reclaim our
  95. * RCU protected slabs.
  96. */
  97. unsigned long rcustate;
  98. /*
  99. * Fences for the various phases in the request's lifetime.
  100. *
  101. * The submit fence is used to await upon all of the request's
  102. * dependencies. When it is signaled, the request is ready to run.
  103. * It is used by the driver to then queue the request for execution.
  104. */
  105. struct i915_sw_fence submit;
  106. wait_queue_entry_t submitq;
  107. wait_queue_head_t execute;
  108. /*
  109. * A list of everyone we wait upon, and everyone who waits upon us.
  110. * Even though we will not be submitted to the hardware before the
  111. * submit fence is signaled (it waits for all external events as well
  112. * as our own requests), the scheduler still needs to know the
  113. * dependency tree for the lifetime of the request (from execbuf
  114. * to retirement), i.e. bidirectional dependency information for the
  115. * request not tied to individual fences.
  116. */
  117. struct i915_sched_node sched;
  118. struct i915_dependency dep;
  119. /**
  120. * GEM sequence number associated with this request on the
  121. * global execution timeline. It is zero when the request is not
  122. * on the HW queue (i.e. not on the engine timeline list).
  123. * Its value is guarded by the timeline spinlock.
  124. */
  125. u32 global_seqno;
  126. /** Position in the ring of the start of the request */
  127. u32 head;
  128. /** Position in the ring of the start of the user packets */
  129. u32 infix;
  130. /**
  131. * Position in the ring of the start of the postfix.
  132. * This is required to calculate the maximum available ring space
  133. * without overwriting the postfix.
  134. */
  135. u32 postfix;
  136. /** Position in the ring of the end of the whole request */
  137. u32 tail;
  138. /** Position in the ring of the end of any workarounds after the tail */
  139. u32 wa_tail;
  140. /** Preallocate space in the ring for the emitting the request */
  141. u32 reserved_space;
  142. /** Batch buffer related to this request if any (used for
  143. * error state dump only).
  144. */
  145. struct i915_vma *batch;
  146. /**
  147. * Additional buffers requested by userspace to be captured upon
  148. * a GPU hang. The vma/obj on this list are protected by their
  149. * active reference - all objects on this list must also be
  150. * on the active_list (of their final request).
  151. */
  152. struct i915_capture_list *capture_list;
  153. struct list_head active_list;
  154. /** Time at which this request was emitted, in jiffies. */
  155. unsigned long emitted_jiffies;
  156. bool waitboost;
  157. /** engine->request_list entry for this request */
  158. struct list_head link;
  159. /** ring->request_list entry for this request */
  160. struct list_head ring_link;
  161. struct drm_i915_file_private *file_priv;
  162. /** file_priv list entry for this request */
  163. struct list_head client_link;
  164. };
  165. #define I915_FENCE_GFP (GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN)
  166. extern const struct dma_fence_ops i915_fence_ops;
  167. static inline bool dma_fence_is_i915(const struct dma_fence *fence)
  168. {
  169. return fence->ops == &i915_fence_ops;
  170. }
  171. struct i915_request * __must_check
  172. i915_request_alloc(struct intel_engine_cs *engine,
  173. struct i915_gem_context *ctx);
  174. void i915_request_retire_upto(struct i915_request *rq);
  175. static inline struct i915_request *
  176. to_request(struct dma_fence *fence)
  177. {
  178. /* We assume that NULL fence/request are interoperable */
  179. BUILD_BUG_ON(offsetof(struct i915_request, fence) != 0);
  180. GEM_BUG_ON(fence && !dma_fence_is_i915(fence));
  181. return container_of(fence, struct i915_request, fence);
  182. }
  183. static inline struct i915_request *
  184. i915_request_get(struct i915_request *rq)
  185. {
  186. return to_request(dma_fence_get(&rq->fence));
  187. }
  188. static inline struct i915_request *
  189. i915_request_get_rcu(struct i915_request *rq)
  190. {
  191. return to_request(dma_fence_get_rcu(&rq->fence));
  192. }
  193. static inline void
  194. i915_request_put(struct i915_request *rq)
  195. {
  196. dma_fence_put(&rq->fence);
  197. }
  198. /**
  199. * i915_request_global_seqno - report the current global seqno
  200. * @request - the request
  201. *
  202. * A request is assigned a global seqno only when it is on the hardware
  203. * execution queue. The global seqno can be used to maintain a list of
  204. * requests on the same engine in retirement order, for example for
  205. * constructing a priority queue for waiting. Prior to its execution, or
  206. * if it is subsequently removed in the event of preemption, its global
  207. * seqno is zero. As both insertion and removal from the execution queue
  208. * may operate in IRQ context, it is not guarded by the usual struct_mutex
  209. * BKL. Instead those relying on the global seqno must be prepared for its
  210. * value to change between reads. Only when the request is complete can
  211. * the global seqno be stable (due to the memory barriers on submitting
  212. * the commands to the hardware to write the breadcrumb, if the HWS shows
  213. * that it has passed the global seqno and the global seqno is unchanged
  214. * after the read, it is indeed complete).
  215. */
  216. static u32
  217. i915_request_global_seqno(const struct i915_request *request)
  218. {
  219. return READ_ONCE(request->global_seqno);
  220. }
  221. int i915_request_await_object(struct i915_request *to,
  222. struct drm_i915_gem_object *obj,
  223. bool write);
  224. int i915_request_await_dma_fence(struct i915_request *rq,
  225. struct dma_fence *fence);
  226. void i915_request_add(struct i915_request *rq);
  227. void __i915_request_submit(struct i915_request *request);
  228. void i915_request_submit(struct i915_request *request);
  229. void i915_request_skip(struct i915_request *request, int error);
  230. void __i915_request_unsubmit(struct i915_request *request);
  231. void i915_request_unsubmit(struct i915_request *request);
  232. long i915_request_wait(struct i915_request *rq,
  233. unsigned int flags,
  234. long timeout)
  235. __attribute__((nonnull(1)));
  236. #define I915_WAIT_INTERRUPTIBLE BIT(0)
  237. #define I915_WAIT_LOCKED BIT(1) /* struct_mutex held, handle GPU reset */
  238. #define I915_WAIT_ALL BIT(2) /* used by i915_gem_object_wait() */
  239. #define I915_WAIT_FOR_IDLE_BOOST BIT(3)
  240. static inline bool intel_engine_has_started(struct intel_engine_cs *engine,
  241. u32 seqno);
  242. static inline bool intel_engine_has_completed(struct intel_engine_cs *engine,
  243. u32 seqno);
  244. /**
  245. * Returns true if seq1 is later than seq2.
  246. */
  247. static inline bool i915_seqno_passed(u32 seq1, u32 seq2)
  248. {
  249. return (s32)(seq1 - seq2) >= 0;
  250. }
  251. /**
  252. * i915_request_started - check if the request has begun being executed
  253. * @rq: the request
  254. *
  255. * Returns true if the request has been submitted to hardware, and the hardware
  256. * has advanced passed the end of the previous request and so should be either
  257. * currently processing the request (though it may be preempted and so
  258. * not necessarily the next request to complete) or have completed the request.
  259. */
  260. static inline bool i915_request_started(const struct i915_request *rq)
  261. {
  262. u32 seqno;
  263. seqno = i915_request_global_seqno(rq);
  264. if (!seqno) /* not yet submitted to HW */
  265. return false;
  266. return intel_engine_has_started(rq->engine, seqno);
  267. }
  268. static inline bool
  269. __i915_request_completed(const struct i915_request *rq, u32 seqno)
  270. {
  271. GEM_BUG_ON(!seqno);
  272. return intel_engine_has_completed(rq->engine, seqno) &&
  273. seqno == i915_request_global_seqno(rq);
  274. }
  275. static inline bool i915_request_completed(const struct i915_request *rq)
  276. {
  277. u32 seqno;
  278. seqno = i915_request_global_seqno(rq);
  279. if (!seqno)
  280. return false;
  281. return __i915_request_completed(rq, seqno);
  282. }
  283. static inline bool i915_sched_node_signaled(const struct i915_sched_node *node)
  284. {
  285. const struct i915_request *rq =
  286. container_of(node, const struct i915_request, sched);
  287. return i915_request_completed(rq);
  288. }
  289. void i915_retire_requests(struct drm_i915_private *i915);
  290. /*
  291. * We treat requests as fences. This is not be to confused with our
  292. * "fence registers" but pipeline synchronisation objects ala GL_ARB_sync.
  293. * We use the fences to synchronize access from the CPU with activity on the
  294. * GPU, for example, we should not rewrite an object's PTE whilst the GPU
  295. * is reading them. We also track fences at a higher level to provide
  296. * implicit synchronisation around GEM objects, e.g. set-domain will wait
  297. * for outstanding GPU rendering before marking the object ready for CPU
  298. * access, or a pageflip will wait until the GPU is complete before showing
  299. * the frame on the scanout.
  300. *
  301. * In order to use a fence, the object must track the fence it needs to
  302. * serialise with. For example, GEM objects want to track both read and
  303. * write access so that we can perform concurrent read operations between
  304. * the CPU and GPU engines, as well as waiting for all rendering to
  305. * complete, or waiting for the last GPU user of a "fence register". The
  306. * object then embeds a #i915_gem_active to track the most recent (in
  307. * retirement order) request relevant for the desired mode of access.
  308. * The #i915_gem_active is updated with i915_gem_active_set() to track the
  309. * most recent fence request, typically this is done as part of
  310. * i915_vma_move_to_active().
  311. *
  312. * When the #i915_gem_active completes (is retired), it will
  313. * signal its completion to the owner through a callback as well as mark
  314. * itself as idle (i915_gem_active.request == NULL). The owner
  315. * can then perform any action, such as delayed freeing of an active
  316. * resource including itself.
  317. */
  318. struct i915_gem_active;
  319. typedef void (*i915_gem_retire_fn)(struct i915_gem_active *,
  320. struct i915_request *);
  321. struct i915_gem_active {
  322. struct i915_request __rcu *request;
  323. struct list_head link;
  324. i915_gem_retire_fn retire;
  325. };
  326. void i915_gem_retire_noop(struct i915_gem_active *,
  327. struct i915_request *request);
  328. /**
  329. * init_request_active - prepares the activity tracker for use
  330. * @active - the active tracker
  331. * @func - a callback when then the tracker is retired (becomes idle),
  332. * can be NULL
  333. *
  334. * init_request_active() prepares the embedded @active struct for use as
  335. * an activity tracker, that is for tracking the last known active request
  336. * associated with it. When the last request becomes idle, when it is retired
  337. * after completion, the optional callback @func is invoked.
  338. */
  339. static inline void
  340. init_request_active(struct i915_gem_active *active,
  341. i915_gem_retire_fn retire)
  342. {
  343. RCU_INIT_POINTER(active->request, NULL);
  344. INIT_LIST_HEAD(&active->link);
  345. active->retire = retire ?: i915_gem_retire_noop;
  346. }
  347. /**
  348. * i915_gem_active_set - updates the tracker to watch the current request
  349. * @active - the active tracker
  350. * @request - the request to watch
  351. *
  352. * i915_gem_active_set() watches the given @request for completion. Whilst
  353. * that @request is busy, the @active reports busy. When that @request is
  354. * retired, the @active tracker is updated to report idle.
  355. */
  356. static inline void
  357. i915_gem_active_set(struct i915_gem_active *active,
  358. struct i915_request *request)
  359. {
  360. list_move(&active->link, &request->active_list);
  361. rcu_assign_pointer(active->request, request);
  362. }
  363. /**
  364. * i915_gem_active_set_retire_fn - updates the retirement callback
  365. * @active - the active tracker
  366. * @fn - the routine called when the request is retired
  367. * @mutex - struct_mutex used to guard retirements
  368. *
  369. * i915_gem_active_set_retire_fn() updates the function pointer that
  370. * is called when the final request associated with the @active tracker
  371. * is retired.
  372. */
  373. static inline void
  374. i915_gem_active_set_retire_fn(struct i915_gem_active *active,
  375. i915_gem_retire_fn fn,
  376. struct mutex *mutex)
  377. {
  378. lockdep_assert_held(mutex);
  379. active->retire = fn ?: i915_gem_retire_noop;
  380. }
  381. static inline struct i915_request *
  382. __i915_gem_active_peek(const struct i915_gem_active *active)
  383. {
  384. /*
  385. * Inside the error capture (running with the driver in an unknown
  386. * state), we want to bend the rules slightly (a lot).
  387. *
  388. * Work is in progress to make it safer, in the meantime this keeps
  389. * the known issue from spamming the logs.
  390. */
  391. return rcu_dereference_protected(active->request, 1);
  392. }
  393. /**
  394. * i915_gem_active_raw - return the active request
  395. * @active - the active tracker
  396. *
  397. * i915_gem_active_raw() returns the current request being tracked, or NULL.
  398. * It does not obtain a reference on the request for the caller, so the caller
  399. * must hold struct_mutex.
  400. */
  401. static inline struct i915_request *
  402. i915_gem_active_raw(const struct i915_gem_active *active, struct mutex *mutex)
  403. {
  404. return rcu_dereference_protected(active->request,
  405. lockdep_is_held(mutex));
  406. }
  407. /**
  408. * i915_gem_active_peek - report the active request being monitored
  409. * @active - the active tracker
  410. *
  411. * i915_gem_active_peek() returns the current request being tracked if
  412. * still active, or NULL. It does not obtain a reference on the request
  413. * for the caller, so the caller must hold struct_mutex.
  414. */
  415. static inline struct i915_request *
  416. i915_gem_active_peek(const struct i915_gem_active *active, struct mutex *mutex)
  417. {
  418. struct i915_request *request;
  419. request = i915_gem_active_raw(active, mutex);
  420. if (!request || i915_request_completed(request))
  421. return NULL;
  422. return request;
  423. }
  424. /**
  425. * i915_gem_active_get - return a reference to the active request
  426. * @active - the active tracker
  427. *
  428. * i915_gem_active_get() returns a reference to the active request, or NULL
  429. * if the active tracker is idle. The caller must hold struct_mutex.
  430. */
  431. static inline struct i915_request *
  432. i915_gem_active_get(const struct i915_gem_active *active, struct mutex *mutex)
  433. {
  434. return i915_request_get(i915_gem_active_peek(active, mutex));
  435. }
  436. /**
  437. * __i915_gem_active_get_rcu - return a reference to the active request
  438. * @active - the active tracker
  439. *
  440. * __i915_gem_active_get() returns a reference to the active request, or NULL
  441. * if the active tracker is idle. The caller must hold the RCU read lock, but
  442. * the returned pointer is safe to use outside of RCU.
  443. */
  444. static inline struct i915_request *
  445. __i915_gem_active_get_rcu(const struct i915_gem_active *active)
  446. {
  447. /*
  448. * Performing a lockless retrieval of the active request is super
  449. * tricky. SLAB_TYPESAFE_BY_RCU merely guarantees that the backing
  450. * slab of request objects will not be freed whilst we hold the
  451. * RCU read lock. It does not guarantee that the request itself
  452. * will not be freed and then *reused*. Viz,
  453. *
  454. * Thread A Thread B
  455. *
  456. * rq = active.request
  457. * retire(rq) -> free(rq);
  458. * (rq is now first on the slab freelist)
  459. * active.request = NULL
  460. *
  461. * rq = new submission on a new object
  462. * ref(rq)
  463. *
  464. * To prevent the request from being reused whilst the caller
  465. * uses it, we take a reference like normal. Whilst acquiring
  466. * the reference we check that it is not in a destroyed state
  467. * (refcnt == 0). That prevents the request being reallocated
  468. * whilst the caller holds on to it. To check that the request
  469. * was not reallocated as we acquired the reference we have to
  470. * check that our request remains the active request across
  471. * the lookup, in the same manner as a seqlock. The visibility
  472. * of the pointer versus the reference counting is controlled
  473. * by using RCU barriers (rcu_dereference and rcu_assign_pointer).
  474. *
  475. * In the middle of all that, we inspect whether the request is
  476. * complete. Retiring is lazy so the request may be completed long
  477. * before the active tracker is updated. Querying whether the
  478. * request is complete is far cheaper (as it involves no locked
  479. * instructions setting cachelines to exclusive) than acquiring
  480. * the reference, so we do it first. The RCU read lock ensures the
  481. * pointer dereference is valid, but does not ensure that the
  482. * seqno nor HWS is the right one! However, if the request was
  483. * reallocated, that means the active tracker's request was complete.
  484. * If the new request is also complete, then both are and we can
  485. * just report the active tracker is idle. If the new request is
  486. * incomplete, then we acquire a reference on it and check that
  487. * it remained the active request.
  488. *
  489. * It is then imperative that we do not zero the request on
  490. * reallocation, so that we can chase the dangling pointers!
  491. * See i915_request_alloc().
  492. */
  493. do {
  494. struct i915_request *request;
  495. request = rcu_dereference(active->request);
  496. if (!request || i915_request_completed(request))
  497. return NULL;
  498. /*
  499. * An especially silly compiler could decide to recompute the
  500. * result of i915_request_completed, more specifically
  501. * re-emit the load for request->fence.seqno. A race would catch
  502. * a later seqno value, which could flip the result from true to
  503. * false. Which means part of the instructions below might not
  504. * be executed, while later on instructions are executed. Due to
  505. * barriers within the refcounting the inconsistency can't reach
  506. * past the call to i915_request_get_rcu, but not executing
  507. * that while still executing i915_request_put() creates
  508. * havoc enough. Prevent this with a compiler barrier.
  509. */
  510. barrier();
  511. request = i915_request_get_rcu(request);
  512. /*
  513. * What stops the following rcu_access_pointer() from occurring
  514. * before the above i915_request_get_rcu()? If we were
  515. * to read the value before pausing to get the reference to
  516. * the request, we may not notice a change in the active
  517. * tracker.
  518. *
  519. * The rcu_access_pointer() is a mere compiler barrier, which
  520. * means both the CPU and compiler are free to perform the
  521. * memory read without constraint. The compiler only has to
  522. * ensure that any operations after the rcu_access_pointer()
  523. * occur afterwards in program order. This means the read may
  524. * be performed earlier by an out-of-order CPU, or adventurous
  525. * compiler.
  526. *
  527. * The atomic operation at the heart of
  528. * i915_request_get_rcu(), see dma_fence_get_rcu(), is
  529. * atomic_inc_not_zero() which is only a full memory barrier
  530. * when successful. That is, if i915_request_get_rcu()
  531. * returns the request (and so with the reference counted
  532. * incremented) then the following read for rcu_access_pointer()
  533. * must occur after the atomic operation and so confirm
  534. * that this request is the one currently being tracked.
  535. *
  536. * The corresponding write barrier is part of
  537. * rcu_assign_pointer().
  538. */
  539. if (!request || request == rcu_access_pointer(active->request))
  540. return rcu_pointer_handoff(request);
  541. i915_request_put(request);
  542. } while (1);
  543. }
  544. /**
  545. * i915_gem_active_get_unlocked - return a reference to the active request
  546. * @active - the active tracker
  547. *
  548. * i915_gem_active_get_unlocked() returns a reference to the active request,
  549. * or NULL if the active tracker is idle. The reference is obtained under RCU,
  550. * so no locking is required by the caller.
  551. *
  552. * The reference should be freed with i915_request_put().
  553. */
  554. static inline struct i915_request *
  555. i915_gem_active_get_unlocked(const struct i915_gem_active *active)
  556. {
  557. struct i915_request *request;
  558. rcu_read_lock();
  559. request = __i915_gem_active_get_rcu(active);
  560. rcu_read_unlock();
  561. return request;
  562. }
  563. /**
  564. * i915_gem_active_isset - report whether the active tracker is assigned
  565. * @active - the active tracker
  566. *
  567. * i915_gem_active_isset() returns true if the active tracker is currently
  568. * assigned to a request. Due to the lazy retiring, that request may be idle
  569. * and this may report stale information.
  570. */
  571. static inline bool
  572. i915_gem_active_isset(const struct i915_gem_active *active)
  573. {
  574. return rcu_access_pointer(active->request);
  575. }
  576. /**
  577. * i915_gem_active_wait - waits until the request is completed
  578. * @active - the active request on which to wait
  579. * @flags - how to wait
  580. * @timeout - how long to wait at most
  581. * @rps - userspace client to charge for a waitboost
  582. *
  583. * i915_gem_active_wait() waits until the request is completed before
  584. * returning, without requiring any locks to be held. Note that it does not
  585. * retire any requests before returning.
  586. *
  587. * This function relies on RCU in order to acquire the reference to the active
  588. * request without holding any locks. See __i915_gem_active_get_rcu() for the
  589. * glory details on how that is managed. Once the reference is acquired, we
  590. * can then wait upon the request, and afterwards release our reference,
  591. * free of any locking.
  592. *
  593. * This function wraps i915_request_wait(), see it for the full details on
  594. * the arguments.
  595. *
  596. * Returns 0 if successful, or a negative error code.
  597. */
  598. static inline int
  599. i915_gem_active_wait(const struct i915_gem_active *active, unsigned int flags)
  600. {
  601. struct i915_request *request;
  602. long ret = 0;
  603. request = i915_gem_active_get_unlocked(active);
  604. if (request) {
  605. ret = i915_request_wait(request, flags, MAX_SCHEDULE_TIMEOUT);
  606. i915_request_put(request);
  607. }
  608. return ret < 0 ? ret : 0;
  609. }
  610. /**
  611. * i915_gem_active_retire - waits until the request is retired
  612. * @active - the active request on which to wait
  613. *
  614. * i915_gem_active_retire() waits until the request is completed,
  615. * and then ensures that at least the retirement handler for this
  616. * @active tracker is called before returning. If the @active
  617. * tracker is idle, the function returns immediately.
  618. */
  619. static inline int __must_check
  620. i915_gem_active_retire(struct i915_gem_active *active,
  621. struct mutex *mutex)
  622. {
  623. struct i915_request *request;
  624. long ret;
  625. request = i915_gem_active_raw(active, mutex);
  626. if (!request)
  627. return 0;
  628. ret = i915_request_wait(request,
  629. I915_WAIT_INTERRUPTIBLE | I915_WAIT_LOCKED,
  630. MAX_SCHEDULE_TIMEOUT);
  631. if (ret < 0)
  632. return ret;
  633. list_del_init(&active->link);
  634. RCU_INIT_POINTER(active->request, NULL);
  635. active->retire(active, request);
  636. return 0;
  637. }
  638. #define for_each_active(mask, idx) \
  639. for (; mask ? idx = ffs(mask) - 1, 1 : 0; mask &= ~BIT(idx))
  640. #endif /* I915_REQUEST_H */