i915_gem_request.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. /*
  2. * Copyright © 2008-2015 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_GEM_REQUEST_H
  25. #define I915_GEM_REQUEST_H
  26. #include <linux/fence.h>
  27. #include "i915_gem.h"
  28. #include "i915_sw_fence.h"
  29. struct intel_wait {
  30. struct rb_node node;
  31. struct task_struct *tsk;
  32. u32 seqno;
  33. };
  34. struct intel_signal_node {
  35. struct rb_node node;
  36. struct intel_wait wait;
  37. };
  38. /**
  39. * Request queue structure.
  40. *
  41. * The request queue allows us to note sequence numbers that have been emitted
  42. * and may be associated with active buffers to be retired.
  43. *
  44. * By keeping this list, we can avoid having to do questionable sequence
  45. * number comparisons on buffer last_read|write_seqno. It also allows an
  46. * emission time to be associated with the request for tracking how far ahead
  47. * of the GPU the submission is.
  48. *
  49. * When modifying this structure be very aware that we perform a lockless
  50. * RCU lookup of it that may race against reallocation of the struct
  51. * from the slab freelist. We intentionally do not zero the structure on
  52. * allocation so that the lookup can use the dangling pointers (and is
  53. * cogniscent that those pointers may be wrong). Instead, everything that
  54. * needs to be initialised must be done so explicitly.
  55. *
  56. * The requests are reference counted.
  57. */
  58. struct drm_i915_gem_request {
  59. struct fence fence;
  60. spinlock_t lock;
  61. /** On Which ring this request was generated */
  62. struct drm_i915_private *i915;
  63. /**
  64. * Context and ring buffer related to this request
  65. * Contexts are refcounted, so when this request is associated with a
  66. * context, we must increment the context's refcount, to guarantee that
  67. * it persists while any request is linked to it. Requests themselves
  68. * are also refcounted, so the request will only be freed when the last
  69. * reference to it is dismissed, and the code in
  70. * i915_gem_request_free() will then decrement the refcount on the
  71. * context.
  72. */
  73. struct i915_gem_context *ctx;
  74. struct intel_engine_cs *engine;
  75. struct intel_ring *ring;
  76. struct intel_signal_node signaling;
  77. struct i915_sw_fence submit;
  78. wait_queue_t submitq;
  79. /** GEM sequence number associated with the previous request,
  80. * when the HWS breadcrumb is equal to this the GPU is processing
  81. * this request.
  82. */
  83. u32 previous_seqno;
  84. /** Position in the ring of the start of the request */
  85. u32 head;
  86. /**
  87. * Position in the ring of the start of the postfix.
  88. * This is required to calculate the maximum available ring space
  89. * without overwriting the postfix.
  90. */
  91. u32 postfix;
  92. /** Position in the ring of the end of the whole request */
  93. u32 tail;
  94. /** Position in the ring of the end of any workarounds after the tail */
  95. u32 wa_tail;
  96. /** Preallocate space in the ring for the emitting the request */
  97. u32 reserved_space;
  98. /**
  99. * Context related to the previous request.
  100. * As the contexts are accessed by the hardware until the switch is
  101. * completed to a new context, the hardware may still be writing
  102. * to the context object after the breadcrumb is visible. We must
  103. * not unpin/unbind/prune that object whilst still active and so
  104. * we keep the previous context pinned until the following (this)
  105. * request is retired.
  106. */
  107. struct i915_gem_context *previous_context;
  108. /** Batch buffer related to this request if any (used for
  109. * error state dump only).
  110. */
  111. struct i915_vma *batch;
  112. struct list_head active_list;
  113. /** Time at which this request was emitted, in jiffies. */
  114. unsigned long emitted_jiffies;
  115. /** engine->request_list entry for this request */
  116. struct list_head link;
  117. /** ring->request_list entry for this request */
  118. struct list_head ring_link;
  119. struct drm_i915_file_private *file_priv;
  120. /** file_priv list entry for this request */
  121. struct list_head client_list;
  122. /** Link in the execlist submission queue, guarded by execlist_lock. */
  123. struct list_head execlist_link;
  124. };
  125. extern const struct fence_ops i915_fence_ops;
  126. static inline bool fence_is_i915(struct fence *fence)
  127. {
  128. return fence->ops == &i915_fence_ops;
  129. }
  130. struct drm_i915_gem_request * __must_check
  131. i915_gem_request_alloc(struct intel_engine_cs *engine,
  132. struct i915_gem_context *ctx);
  133. int i915_gem_request_add_to_client(struct drm_i915_gem_request *req,
  134. struct drm_file *file);
  135. void i915_gem_request_retire_upto(struct drm_i915_gem_request *req);
  136. static inline u32
  137. i915_gem_request_get_seqno(struct drm_i915_gem_request *req)
  138. {
  139. return req ? req->fence.seqno : 0;
  140. }
  141. static inline struct intel_engine_cs *
  142. i915_gem_request_get_engine(struct drm_i915_gem_request *req)
  143. {
  144. return req ? req->engine : NULL;
  145. }
  146. static inline struct drm_i915_gem_request *
  147. to_request(struct fence *fence)
  148. {
  149. /* We assume that NULL fence/request are interoperable */
  150. BUILD_BUG_ON(offsetof(struct drm_i915_gem_request, fence) != 0);
  151. GEM_BUG_ON(fence && !fence_is_i915(fence));
  152. return container_of(fence, struct drm_i915_gem_request, fence);
  153. }
  154. static inline struct drm_i915_gem_request *
  155. i915_gem_request_get(struct drm_i915_gem_request *req)
  156. {
  157. return to_request(fence_get(&req->fence));
  158. }
  159. static inline struct drm_i915_gem_request *
  160. i915_gem_request_get_rcu(struct drm_i915_gem_request *req)
  161. {
  162. return to_request(fence_get_rcu(&req->fence));
  163. }
  164. static inline void
  165. i915_gem_request_put(struct drm_i915_gem_request *req)
  166. {
  167. fence_put(&req->fence);
  168. }
  169. static inline void i915_gem_request_assign(struct drm_i915_gem_request **pdst,
  170. struct drm_i915_gem_request *src)
  171. {
  172. if (src)
  173. i915_gem_request_get(src);
  174. if (*pdst)
  175. i915_gem_request_put(*pdst);
  176. *pdst = src;
  177. }
  178. int
  179. i915_gem_request_await_object(struct drm_i915_gem_request *to,
  180. struct drm_i915_gem_object *obj,
  181. bool write);
  182. void __i915_add_request(struct drm_i915_gem_request *req, bool flush_caches);
  183. #define i915_add_request(req) \
  184. __i915_add_request(req, true)
  185. #define i915_add_request_no_flush(req) \
  186. __i915_add_request(req, false)
  187. struct intel_rps_client;
  188. #define NO_WAITBOOST ERR_PTR(-1)
  189. #define IS_RPS_CLIENT(p) (!IS_ERR(p))
  190. #define IS_RPS_USER(p) (!IS_ERR_OR_NULL(p))
  191. int i915_wait_request(struct drm_i915_gem_request *req,
  192. unsigned int flags,
  193. s64 *timeout,
  194. struct intel_rps_client *rps)
  195. __attribute__((nonnull(1)));
  196. #define I915_WAIT_INTERRUPTIBLE BIT(0)
  197. #define I915_WAIT_LOCKED BIT(1) /* struct_mutex held, handle GPU reset */
  198. static inline u32 intel_engine_get_seqno(struct intel_engine_cs *engine);
  199. /**
  200. * Returns true if seq1 is later than seq2.
  201. */
  202. static inline bool i915_seqno_passed(u32 seq1, u32 seq2)
  203. {
  204. return (s32)(seq1 - seq2) >= 0;
  205. }
  206. static inline bool
  207. i915_gem_request_started(const struct drm_i915_gem_request *req)
  208. {
  209. return i915_seqno_passed(intel_engine_get_seqno(req->engine),
  210. req->previous_seqno);
  211. }
  212. static inline bool
  213. i915_gem_request_completed(const struct drm_i915_gem_request *req)
  214. {
  215. return i915_seqno_passed(intel_engine_get_seqno(req->engine),
  216. req->fence.seqno);
  217. }
  218. bool __i915_spin_request(const struct drm_i915_gem_request *request,
  219. int state, unsigned long timeout_us);
  220. static inline bool i915_spin_request(const struct drm_i915_gem_request *request,
  221. int state, unsigned long timeout_us)
  222. {
  223. return (i915_gem_request_started(request) &&
  224. __i915_spin_request(request, state, timeout_us));
  225. }
  226. /* We treat requests as fences. This is not be to confused with our
  227. * "fence registers" but pipeline synchronisation objects ala GL_ARB_sync.
  228. * We use the fences to synchronize access from the CPU with activity on the
  229. * GPU, for example, we should not rewrite an object's PTE whilst the GPU
  230. * is reading them. We also track fences at a higher level to provide
  231. * implicit synchronisation around GEM objects, e.g. set-domain will wait
  232. * for outstanding GPU rendering before marking the object ready for CPU
  233. * access, or a pageflip will wait until the GPU is complete before showing
  234. * the frame on the scanout.
  235. *
  236. * In order to use a fence, the object must track the fence it needs to
  237. * serialise with. For example, GEM objects want to track both read and
  238. * write access so that we can perform concurrent read operations between
  239. * the CPU and GPU engines, as well as waiting for all rendering to
  240. * complete, or waiting for the last GPU user of a "fence register". The
  241. * object then embeds a #i915_gem_active to track the most recent (in
  242. * retirement order) request relevant for the desired mode of access.
  243. * The #i915_gem_active is updated with i915_gem_active_set() to track the
  244. * most recent fence request, typically this is done as part of
  245. * i915_vma_move_to_active().
  246. *
  247. * When the #i915_gem_active completes (is retired), it will
  248. * signal its completion to the owner through a callback as well as mark
  249. * itself as idle (i915_gem_active.request == NULL). The owner
  250. * can then perform any action, such as delayed freeing of an active
  251. * resource including itself.
  252. */
  253. struct i915_gem_active;
  254. typedef void (*i915_gem_retire_fn)(struct i915_gem_active *,
  255. struct drm_i915_gem_request *);
  256. struct i915_gem_active {
  257. struct drm_i915_gem_request __rcu *request;
  258. struct list_head link;
  259. i915_gem_retire_fn retire;
  260. };
  261. void i915_gem_retire_noop(struct i915_gem_active *,
  262. struct drm_i915_gem_request *request);
  263. /**
  264. * init_request_active - prepares the activity tracker for use
  265. * @active - the active tracker
  266. * @func - a callback when then the tracker is retired (becomes idle),
  267. * can be NULL
  268. *
  269. * init_request_active() prepares the embedded @active struct for use as
  270. * an activity tracker, that is for tracking the last known active request
  271. * associated with it. When the last request becomes idle, when it is retired
  272. * after completion, the optional callback @func is invoked.
  273. */
  274. static inline void
  275. init_request_active(struct i915_gem_active *active,
  276. i915_gem_retire_fn retire)
  277. {
  278. INIT_LIST_HEAD(&active->link);
  279. active->retire = retire ?: i915_gem_retire_noop;
  280. }
  281. /**
  282. * i915_gem_active_set - updates the tracker to watch the current request
  283. * @active - the active tracker
  284. * @request - the request to watch
  285. *
  286. * i915_gem_active_set() watches the given @request for completion. Whilst
  287. * that @request is busy, the @active reports busy. When that @request is
  288. * retired, the @active tracker is updated to report idle.
  289. */
  290. static inline void
  291. i915_gem_active_set(struct i915_gem_active *active,
  292. struct drm_i915_gem_request *request)
  293. {
  294. list_move(&active->link, &request->active_list);
  295. rcu_assign_pointer(active->request, request);
  296. }
  297. static inline struct drm_i915_gem_request *
  298. __i915_gem_active_peek(const struct i915_gem_active *active)
  299. {
  300. /* Inside the error capture (running with the driver in an unknown
  301. * state), we want to bend the rules slightly (a lot).
  302. *
  303. * Work is in progress to make it safer, in the meantime this keeps
  304. * the known issue from spamming the logs.
  305. */
  306. return rcu_dereference_protected(active->request, 1);
  307. }
  308. /**
  309. * i915_gem_active_raw - return the active request
  310. * @active - the active tracker
  311. *
  312. * i915_gem_active_raw() returns the current request being tracked, or NULL.
  313. * It does not obtain a reference on the request for the caller, so the caller
  314. * must hold struct_mutex.
  315. */
  316. static inline struct drm_i915_gem_request *
  317. i915_gem_active_raw(const struct i915_gem_active *active, struct mutex *mutex)
  318. {
  319. return rcu_dereference_protected(active->request,
  320. lockdep_is_held(mutex));
  321. }
  322. /**
  323. * i915_gem_active_peek - report the active request being monitored
  324. * @active - the active tracker
  325. *
  326. * i915_gem_active_peek() returns the current request being tracked if
  327. * still active, or NULL. It does not obtain a reference on the request
  328. * for the caller, so the caller must hold struct_mutex.
  329. */
  330. static inline struct drm_i915_gem_request *
  331. i915_gem_active_peek(const struct i915_gem_active *active, struct mutex *mutex)
  332. {
  333. struct drm_i915_gem_request *request;
  334. request = i915_gem_active_raw(active, mutex);
  335. if (!request || i915_gem_request_completed(request))
  336. return NULL;
  337. return request;
  338. }
  339. /**
  340. * i915_gem_active_get - return a reference to the active request
  341. * @active - the active tracker
  342. *
  343. * i915_gem_active_get() returns a reference to the active request, or NULL
  344. * if the active tracker is idle. The caller must hold struct_mutex.
  345. */
  346. static inline struct drm_i915_gem_request *
  347. i915_gem_active_get(const struct i915_gem_active *active, struct mutex *mutex)
  348. {
  349. return i915_gem_request_get(i915_gem_active_peek(active, mutex));
  350. }
  351. /**
  352. * __i915_gem_active_get_rcu - return a reference to the active request
  353. * @active - the active tracker
  354. *
  355. * __i915_gem_active_get() returns a reference to the active request, or NULL
  356. * if the active tracker is idle. The caller must hold the RCU read lock, but
  357. * the returned pointer is safe to use outside of RCU.
  358. */
  359. static inline struct drm_i915_gem_request *
  360. __i915_gem_active_get_rcu(const struct i915_gem_active *active)
  361. {
  362. /* Performing a lockless retrieval of the active request is super
  363. * tricky. SLAB_DESTROY_BY_RCU merely guarantees that the backing
  364. * slab of request objects will not be freed whilst we hold the
  365. * RCU read lock. It does not guarantee that the request itself
  366. * will not be freed and then *reused*. Viz,
  367. *
  368. * Thread A Thread B
  369. *
  370. * req = active.request
  371. * retire(req) -> free(req);
  372. * (req is now first on the slab freelist)
  373. * active.request = NULL
  374. *
  375. * req = new submission on a new object
  376. * ref(req)
  377. *
  378. * To prevent the request from being reused whilst the caller
  379. * uses it, we take a reference like normal. Whilst acquiring
  380. * the reference we check that it is not in a destroyed state
  381. * (refcnt == 0). That prevents the request being reallocated
  382. * whilst the caller holds on to it. To check that the request
  383. * was not reallocated as we acquired the reference we have to
  384. * check that our request remains the active request across
  385. * the lookup, in the same manner as a seqlock. The visibility
  386. * of the pointer versus the reference counting is controlled
  387. * by using RCU barriers (rcu_dereference and rcu_assign_pointer).
  388. *
  389. * In the middle of all that, we inspect whether the request is
  390. * complete. Retiring is lazy so the request may be completed long
  391. * before the active tracker is updated. Querying whether the
  392. * request is complete is far cheaper (as it involves no locked
  393. * instructions setting cachelines to exclusive) than acquiring
  394. * the reference, so we do it first. The RCU read lock ensures the
  395. * pointer dereference is valid, but does not ensure that the
  396. * seqno nor HWS is the right one! However, if the request was
  397. * reallocated, that means the active tracker's request was complete.
  398. * If the new request is also complete, then both are and we can
  399. * just report the active tracker is idle. If the new request is
  400. * incomplete, then we acquire a reference on it and check that
  401. * it remained the active request.
  402. *
  403. * It is then imperative that we do not zero the request on
  404. * reallocation, so that we can chase the dangling pointers!
  405. * See i915_gem_request_alloc().
  406. */
  407. do {
  408. struct drm_i915_gem_request *request;
  409. request = rcu_dereference(active->request);
  410. if (!request || i915_gem_request_completed(request))
  411. return NULL;
  412. /* An especially silly compiler could decide to recompute the
  413. * result of i915_gem_request_completed, more specifically
  414. * re-emit the load for request->fence.seqno. A race would catch
  415. * a later seqno value, which could flip the result from true to
  416. * false. Which means part of the instructions below might not
  417. * be executed, while later on instructions are executed. Due to
  418. * barriers within the refcounting the inconsistency can't reach
  419. * past the call to i915_gem_request_get_rcu, but not executing
  420. * that while still executing i915_gem_request_put() creates
  421. * havoc enough. Prevent this with a compiler barrier.
  422. */
  423. barrier();
  424. request = i915_gem_request_get_rcu(request);
  425. /* What stops the following rcu_access_pointer() from occurring
  426. * before the above i915_gem_request_get_rcu()? If we were
  427. * to read the value before pausing to get the reference to
  428. * the request, we may not notice a change in the active
  429. * tracker.
  430. *
  431. * The rcu_access_pointer() is a mere compiler barrier, which
  432. * means both the CPU and compiler are free to perform the
  433. * memory read without constraint. The compiler only has to
  434. * ensure that any operations after the rcu_access_pointer()
  435. * occur afterwards in program order. This means the read may
  436. * be performed earlier by an out-of-order CPU, or adventurous
  437. * compiler.
  438. *
  439. * The atomic operation at the heart of
  440. * i915_gem_request_get_rcu(), see fence_get_rcu(), is
  441. * atomic_inc_not_zero() which is only a full memory barrier
  442. * when successful. That is, if i915_gem_request_get_rcu()
  443. * returns the request (and so with the reference counted
  444. * incremented) then the following read for rcu_access_pointer()
  445. * must occur after the atomic operation and so confirm
  446. * that this request is the one currently being tracked.
  447. *
  448. * The corresponding write barrier is part of
  449. * rcu_assign_pointer().
  450. */
  451. if (!request || request == rcu_access_pointer(active->request))
  452. return rcu_pointer_handoff(request);
  453. i915_gem_request_put(request);
  454. } while (1);
  455. }
  456. /**
  457. * i915_gem_active_get_unlocked - return a reference to the active request
  458. * @active - the active tracker
  459. *
  460. * i915_gem_active_get_unlocked() returns a reference to the active request,
  461. * or NULL if the active tracker is idle. The reference is obtained under RCU,
  462. * so no locking is required by the caller.
  463. *
  464. * The reference should be freed with i915_gem_request_put().
  465. */
  466. static inline struct drm_i915_gem_request *
  467. i915_gem_active_get_unlocked(const struct i915_gem_active *active)
  468. {
  469. struct drm_i915_gem_request *request;
  470. rcu_read_lock();
  471. request = __i915_gem_active_get_rcu(active);
  472. rcu_read_unlock();
  473. return request;
  474. }
  475. /**
  476. * i915_gem_active_isset - report whether the active tracker is assigned
  477. * @active - the active tracker
  478. *
  479. * i915_gem_active_isset() returns true if the active tracker is currently
  480. * assigned to a request. Due to the lazy retiring, that request may be idle
  481. * and this may report stale information.
  482. */
  483. static inline bool
  484. i915_gem_active_isset(const struct i915_gem_active *active)
  485. {
  486. return rcu_access_pointer(active->request);
  487. }
  488. /**
  489. * i915_gem_active_is_idle - report whether the active tracker is idle
  490. * @active - the active tracker
  491. *
  492. * i915_gem_active_is_idle() returns true if the active tracker is currently
  493. * unassigned or if the request is complete (but not yet retired). Requires
  494. * the caller to hold struct_mutex (but that can be relaxed if desired).
  495. */
  496. static inline bool
  497. i915_gem_active_is_idle(const struct i915_gem_active *active,
  498. struct mutex *mutex)
  499. {
  500. return !i915_gem_active_peek(active, mutex);
  501. }
  502. /**
  503. * i915_gem_active_wait - waits until the request is completed
  504. * @active - the active request on which to wait
  505. *
  506. * i915_gem_active_wait() waits until the request is completed before
  507. * returning. Note that it does not guarantee that the request is
  508. * retired first, see i915_gem_active_retire().
  509. *
  510. * i915_gem_active_wait() returns immediately if the active
  511. * request is already complete.
  512. */
  513. static inline int __must_check
  514. i915_gem_active_wait(const struct i915_gem_active *active, struct mutex *mutex)
  515. {
  516. struct drm_i915_gem_request *request;
  517. request = i915_gem_active_peek(active, mutex);
  518. if (!request)
  519. return 0;
  520. return i915_wait_request(request,
  521. I915_WAIT_INTERRUPTIBLE | I915_WAIT_LOCKED,
  522. NULL, NULL);
  523. }
  524. /**
  525. * i915_gem_active_wait_unlocked - waits until the request is completed
  526. * @active - the active request on which to wait
  527. * @flags - how to wait
  528. * @timeout - how long to wait at most
  529. * @rps - userspace client to charge for a waitboost
  530. *
  531. * i915_gem_active_wait_unlocked() waits until the request is completed before
  532. * returning, without requiring any locks to be held. Note that it does not
  533. * retire any requests before returning.
  534. *
  535. * This function relies on RCU in order to acquire the reference to the active
  536. * request without holding any locks. See __i915_gem_active_get_rcu() for the
  537. * glory details on how that is managed. Once the reference is acquired, we
  538. * can then wait upon the request, and afterwards release our reference,
  539. * free of any locking.
  540. *
  541. * This function wraps i915_wait_request(), see it for the full details on
  542. * the arguments.
  543. *
  544. * Returns 0 if successful, or a negative error code.
  545. */
  546. static inline int
  547. i915_gem_active_wait_unlocked(const struct i915_gem_active *active,
  548. unsigned int flags,
  549. s64 *timeout,
  550. struct intel_rps_client *rps)
  551. {
  552. struct drm_i915_gem_request *request;
  553. int ret = 0;
  554. request = i915_gem_active_get_unlocked(active);
  555. if (request) {
  556. ret = i915_wait_request(request, flags, timeout, rps);
  557. i915_gem_request_put(request);
  558. }
  559. return ret;
  560. }
  561. /**
  562. * i915_gem_active_retire - waits until the request is retired
  563. * @active - the active request on which to wait
  564. *
  565. * i915_gem_active_retire() waits until the request is completed,
  566. * and then ensures that at least the retirement handler for this
  567. * @active tracker is called before returning. If the @active
  568. * tracker is idle, the function returns immediately.
  569. */
  570. static inline int __must_check
  571. i915_gem_active_retire(struct i915_gem_active *active,
  572. struct mutex *mutex)
  573. {
  574. struct drm_i915_gem_request *request;
  575. int ret;
  576. request = i915_gem_active_raw(active, mutex);
  577. if (!request)
  578. return 0;
  579. ret = i915_wait_request(request,
  580. I915_WAIT_INTERRUPTIBLE | I915_WAIT_LOCKED,
  581. NULL, NULL);
  582. if (ret)
  583. return ret;
  584. list_del_init(&active->link);
  585. RCU_INIT_POINTER(active->request, NULL);
  586. active->retire(active, request);
  587. return 0;
  588. }
  589. /* Convenience functions for peeking at state inside active's request whilst
  590. * guarded by the struct_mutex.
  591. */
  592. static inline uint32_t
  593. i915_gem_active_get_seqno(const struct i915_gem_active *active,
  594. struct mutex *mutex)
  595. {
  596. return i915_gem_request_get_seqno(i915_gem_active_peek(active, mutex));
  597. }
  598. static inline struct intel_engine_cs *
  599. i915_gem_active_get_engine(const struct i915_gem_active *active,
  600. struct mutex *mutex)
  601. {
  602. return i915_gem_request_get_engine(i915_gem_active_peek(active, mutex));
  603. }
  604. #define for_each_active(mask, idx) \
  605. for (; mask ? idx = ffs(mask) - 1, 1 : 0; mask &= ~BIT(idx))
  606. #endif /* I915_GEM_REQUEST_H */