i915_gem_request.h 24 KB

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