i915_request.h 25 KB

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