xref: /dflybsd-src/sys/dev/drm/i915/i915_gem_request.h (revision 71f41f3eaecc1bac4c44cc81622db5b5dddc7dc4)
187df8fc6SFrançois Tigeot /*
287df8fc6SFrançois Tigeot  * Copyright © 2008-2015 Intel Corporation
387df8fc6SFrançois Tigeot  *
487df8fc6SFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
587df8fc6SFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
687df8fc6SFrançois Tigeot  * to deal in the Software without restriction, including without limitation
787df8fc6SFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
887df8fc6SFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
987df8fc6SFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
1087df8fc6SFrançois Tigeot  *
1187df8fc6SFrançois Tigeot  * The above copyright notice and this permission notice (including the next
1287df8fc6SFrançois Tigeot  * paragraph) shall be included in all copies or substantial portions of the
1387df8fc6SFrançois Tigeot  * Software.
1487df8fc6SFrançois Tigeot  *
1587df8fc6SFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1687df8fc6SFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1787df8fc6SFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1887df8fc6SFrançois Tigeot  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1987df8fc6SFrançois Tigeot  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2087df8fc6SFrançois Tigeot  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2187df8fc6SFrançois Tigeot  * IN THE SOFTWARE.
2287df8fc6SFrançois Tigeot  *
2387df8fc6SFrançois Tigeot  */
2487df8fc6SFrançois Tigeot 
2587df8fc6SFrançois Tigeot #ifndef I915_GEM_REQUEST_H
2687df8fc6SFrançois Tigeot #define I915_GEM_REQUEST_H
2787df8fc6SFrançois Tigeot 
2887df8fc6SFrançois Tigeot #include <linux/fence.h>
2987df8fc6SFrançois Tigeot 
3087df8fc6SFrançois Tigeot #include "i915_gem.h"
3187df8fc6SFrançois Tigeot 
32*71f41f3eSFrançois Tigeot struct intel_wait {
33*71f41f3eSFrançois Tigeot 	struct rb_node node;
34*71f41f3eSFrançois Tigeot 	struct task_struct *tsk;
35*71f41f3eSFrançois Tigeot 	u32 seqno;
36*71f41f3eSFrançois Tigeot };
37*71f41f3eSFrançois Tigeot 
38*71f41f3eSFrançois Tigeot struct intel_signal_node {
39*71f41f3eSFrançois Tigeot 	struct rb_node node;
40*71f41f3eSFrançois Tigeot 	struct intel_wait wait;
41*71f41f3eSFrançois Tigeot };
42*71f41f3eSFrançois Tigeot 
4387df8fc6SFrançois Tigeot /**
4487df8fc6SFrançois Tigeot  * Request queue structure.
4587df8fc6SFrançois Tigeot  *
4687df8fc6SFrançois Tigeot  * The request queue allows us to note sequence numbers that have been emitted
4787df8fc6SFrançois Tigeot  * and may be associated with active buffers to be retired.
4887df8fc6SFrançois Tigeot  *
4987df8fc6SFrançois Tigeot  * By keeping this list, we can avoid having to do questionable sequence
5087df8fc6SFrançois Tigeot  * number comparisons on buffer last_read|write_seqno. It also allows an
5187df8fc6SFrançois Tigeot  * emission time to be associated with the request for tracking how far ahead
5287df8fc6SFrançois Tigeot  * of the GPU the submission is.
5387df8fc6SFrançois Tigeot  *
54*71f41f3eSFrançois Tigeot  * When modifying this structure be very aware that we perform a lockless
55*71f41f3eSFrançois Tigeot  * RCU lookup of it that may race against reallocation of the struct
56*71f41f3eSFrançois Tigeot  * from the slab freelist. We intentionally do not zero the structure on
57*71f41f3eSFrançois Tigeot  * allocation so that the lookup can use the dangling pointers (and is
58*71f41f3eSFrançois Tigeot  * cogniscent that those pointers may be wrong). Instead, everything that
59*71f41f3eSFrançois Tigeot  * needs to be initialised must be done so explicitly.
60*71f41f3eSFrançois Tigeot  *
6187df8fc6SFrançois Tigeot  * The requests are reference counted.
6287df8fc6SFrançois Tigeot  */
6387df8fc6SFrançois Tigeot struct drm_i915_gem_request {
6487df8fc6SFrançois Tigeot 	struct fence fence;
65*71f41f3eSFrançois Tigeot 	spinlock_t lock;
6687df8fc6SFrançois Tigeot 
6787df8fc6SFrançois Tigeot 	/** On Which ring this request was generated */
6887df8fc6SFrançois Tigeot 	struct drm_i915_private *i915;
6987df8fc6SFrançois Tigeot 
7087df8fc6SFrançois Tigeot 	/**
7187df8fc6SFrançois Tigeot 	 * Context and ring buffer related to this request
7287df8fc6SFrançois Tigeot 	 * Contexts are refcounted, so when this request is associated with a
7387df8fc6SFrançois Tigeot 	 * context, we must increment the context's refcount, to guarantee that
7487df8fc6SFrançois Tigeot 	 * it persists while any request is linked to it. Requests themselves
7587df8fc6SFrançois Tigeot 	 * are also refcounted, so the request will only be freed when the last
7687df8fc6SFrançois Tigeot 	 * reference to it is dismissed, and the code in
7787df8fc6SFrançois Tigeot 	 * i915_gem_request_free() will then decrement the refcount on the
7887df8fc6SFrançois Tigeot 	 * context.
7987df8fc6SFrançois Tigeot 	 */
8087df8fc6SFrançois Tigeot 	struct i915_gem_context *ctx;
8187df8fc6SFrançois Tigeot 	struct intel_engine_cs *engine;
82*71f41f3eSFrançois Tigeot 	struct intel_ring *ring;
8387df8fc6SFrançois Tigeot 	struct intel_signal_node signaling;
8487df8fc6SFrançois Tigeot 
8587df8fc6SFrançois Tigeot 	/** GEM sequence number associated with the previous request,
8687df8fc6SFrançois Tigeot 	 * when the HWS breadcrumb is equal to this the GPU is processing
8787df8fc6SFrançois Tigeot 	 * this request.
8887df8fc6SFrançois Tigeot 	 */
8987df8fc6SFrançois Tigeot 	u32 previous_seqno;
9087df8fc6SFrançois Tigeot 
9187df8fc6SFrançois Tigeot 	/** Position in the ringbuffer of the start of the request */
9287df8fc6SFrançois Tigeot 	u32 head;
9387df8fc6SFrançois Tigeot 
9487df8fc6SFrançois Tigeot 	/**
9587df8fc6SFrançois Tigeot 	 * Position in the ringbuffer of the start of the postfix.
9687df8fc6SFrançois Tigeot 	 * This is required to calculate the maximum available ringbuffer
9787df8fc6SFrançois Tigeot 	 * space without overwriting the postfix.
9887df8fc6SFrançois Tigeot 	 */
9987df8fc6SFrançois Tigeot 	u32 postfix;
10087df8fc6SFrançois Tigeot 
10187df8fc6SFrançois Tigeot 	/** Position in the ringbuffer of the end of the whole request */
10287df8fc6SFrançois Tigeot 	u32 tail;
10387df8fc6SFrançois Tigeot 
10487df8fc6SFrançois Tigeot 	/** Preallocate space in the ringbuffer for the emitting the request */
10587df8fc6SFrançois Tigeot 	u32 reserved_space;
10687df8fc6SFrançois Tigeot 
10787df8fc6SFrançois Tigeot 	/**
10887df8fc6SFrançois Tigeot 	 * Context related to the previous request.
10987df8fc6SFrançois Tigeot 	 * As the contexts are accessed by the hardware until the switch is
11087df8fc6SFrançois Tigeot 	 * completed to a new context, the hardware may still be writing
11187df8fc6SFrançois Tigeot 	 * to the context object after the breadcrumb is visible. We must
11287df8fc6SFrançois Tigeot 	 * not unpin/unbind/prune that object whilst still active and so
11387df8fc6SFrançois Tigeot 	 * we keep the previous context pinned until the following (this)
11487df8fc6SFrançois Tigeot 	 * request is retired.
11587df8fc6SFrançois Tigeot 	 */
11687df8fc6SFrançois Tigeot 	struct i915_gem_context *previous_context;
11787df8fc6SFrançois Tigeot 
11887df8fc6SFrançois Tigeot 	/** Batch buffer related to this request if any (used for
11987df8fc6SFrançois Tigeot 	 * error state dump only).
12087df8fc6SFrançois Tigeot 	 */
12187df8fc6SFrançois Tigeot 	struct drm_i915_gem_object *batch_obj;
122*71f41f3eSFrançois Tigeot 	struct list_head active_list;
12387df8fc6SFrançois Tigeot 
12487df8fc6SFrançois Tigeot 	/** Time at which this request was emitted, in jiffies. */
12587df8fc6SFrançois Tigeot 	unsigned long emitted_jiffies;
12687df8fc6SFrançois Tigeot 
127*71f41f3eSFrançois Tigeot 	/** engine->request_list entry for this request */
128*71f41f3eSFrançois Tigeot 	struct list_head link;
129*71f41f3eSFrançois Tigeot 
130*71f41f3eSFrançois Tigeot 	/** ring->request_list entry for this request */
131*71f41f3eSFrançois Tigeot 	struct list_head ring_link;
13287df8fc6SFrançois Tigeot 
13387df8fc6SFrançois Tigeot 	struct drm_i915_file_private *file_priv;
13487df8fc6SFrançois Tigeot 	/** file_priv list entry for this request */
13587df8fc6SFrançois Tigeot 	struct list_head client_list;
13687df8fc6SFrançois Tigeot 
13787df8fc6SFrançois Tigeot 	/** process identifier submitting this request */
13887df8fc6SFrançois Tigeot 	pid_t pid;
13987df8fc6SFrançois Tigeot 
14087df8fc6SFrançois Tigeot 	/**
14187df8fc6SFrançois Tigeot 	 * The ELSP only accepts two elements at a time, so we queue
14287df8fc6SFrançois Tigeot 	 * context/tail pairs on a given queue (ring->execlist_queue) until the
14387df8fc6SFrançois Tigeot 	 * hardware is available. The queue serves a double purpose: we also use
14487df8fc6SFrançois Tigeot 	 * it to keep track of the up to 2 contexts currently in the hardware
14587df8fc6SFrançois Tigeot 	 * (usually one in execution and the other queued up by the GPU): We
14687df8fc6SFrançois Tigeot 	 * only remove elements from the head of the queue when the hardware
14787df8fc6SFrançois Tigeot 	 * informs us that an element has been completed.
14887df8fc6SFrançois Tigeot 	 *
14987df8fc6SFrançois Tigeot 	 * All accesses to the queue are mediated by a spinlock
15087df8fc6SFrançois Tigeot 	 * (ring->execlist_lock).
15187df8fc6SFrançois Tigeot 	 */
15287df8fc6SFrançois Tigeot 
15387df8fc6SFrançois Tigeot 	/** Execlist link in the submission queue.*/
15487df8fc6SFrançois Tigeot 	struct list_head execlist_link;
15587df8fc6SFrançois Tigeot 
15687df8fc6SFrançois Tigeot 	/** Execlists no. of times this request has been sent to the ELSP */
15787df8fc6SFrançois Tigeot 	int elsp_submitted;
15887df8fc6SFrançois Tigeot 
15987df8fc6SFrançois Tigeot 	/** Execlists context hardware id. */
16087df8fc6SFrançois Tigeot 	unsigned int ctx_hw_id;
16187df8fc6SFrançois Tigeot };
16287df8fc6SFrançois Tigeot 
16387df8fc6SFrançois Tigeot extern const struct fence_ops i915_fence_ops;
16487df8fc6SFrançois Tigeot 
16587df8fc6SFrançois Tigeot static inline bool fence_is_i915(struct fence *fence)
16687df8fc6SFrançois Tigeot {
16787df8fc6SFrançois Tigeot 	return fence->ops == &i915_fence_ops;
16887df8fc6SFrançois Tigeot }
16987df8fc6SFrançois Tigeot 
17087df8fc6SFrançois Tigeot struct drm_i915_gem_request * __must_check
17187df8fc6SFrançois Tigeot i915_gem_request_alloc(struct intel_engine_cs *engine,
17287df8fc6SFrançois Tigeot 		       struct i915_gem_context *ctx);
17387df8fc6SFrançois Tigeot int i915_gem_request_add_to_client(struct drm_i915_gem_request *req,
17487df8fc6SFrançois Tigeot 				   struct drm_file *file);
17587df8fc6SFrançois Tigeot void i915_gem_request_retire_upto(struct drm_i915_gem_request *req);
17687df8fc6SFrançois Tigeot 
17787df8fc6SFrançois Tigeot static inline u32
17887df8fc6SFrançois Tigeot i915_gem_request_get_seqno(struct drm_i915_gem_request *req)
17987df8fc6SFrançois Tigeot {
18087df8fc6SFrançois Tigeot 	return req ? req->fence.seqno : 0;
18187df8fc6SFrançois Tigeot }
18287df8fc6SFrançois Tigeot 
18387df8fc6SFrançois Tigeot static inline struct intel_engine_cs *
18487df8fc6SFrançois Tigeot i915_gem_request_get_engine(struct drm_i915_gem_request *req)
18587df8fc6SFrançois Tigeot {
18687df8fc6SFrançois Tigeot 	return req ? req->engine : NULL;
18787df8fc6SFrançois Tigeot }
18887df8fc6SFrançois Tigeot 
18987df8fc6SFrançois Tigeot static inline struct drm_i915_gem_request *
19087df8fc6SFrançois Tigeot to_request(struct fence *fence)
19187df8fc6SFrançois Tigeot {
19287df8fc6SFrançois Tigeot 	/* We assume that NULL fence/request are interoperable */
19387df8fc6SFrançois Tigeot 	BUILD_BUG_ON(offsetof(struct drm_i915_gem_request, fence) != 0);
19487df8fc6SFrançois Tigeot 	GEM_BUG_ON(fence && !fence_is_i915(fence));
19587df8fc6SFrançois Tigeot 	return container_of(fence, struct drm_i915_gem_request, fence);
19687df8fc6SFrançois Tigeot }
19787df8fc6SFrançois Tigeot 
19887df8fc6SFrançois Tigeot static inline struct drm_i915_gem_request *
19987df8fc6SFrançois Tigeot i915_gem_request_get(struct drm_i915_gem_request *req)
20087df8fc6SFrançois Tigeot {
20187df8fc6SFrançois Tigeot 	return to_request(fence_get(&req->fence));
20287df8fc6SFrançois Tigeot }
20387df8fc6SFrançois Tigeot 
204*71f41f3eSFrançois Tigeot static inline struct drm_i915_gem_request *
205*71f41f3eSFrançois Tigeot i915_gem_request_get_rcu(struct drm_i915_gem_request *req)
206*71f41f3eSFrançois Tigeot {
207*71f41f3eSFrançois Tigeot 	return to_request(fence_get_rcu(&req->fence));
208*71f41f3eSFrançois Tigeot }
209*71f41f3eSFrançois Tigeot 
21087df8fc6SFrançois Tigeot static inline void
21187df8fc6SFrançois Tigeot i915_gem_request_put(struct drm_i915_gem_request *req)
21287df8fc6SFrançois Tigeot {
21387df8fc6SFrançois Tigeot 	fence_put(&req->fence);
21487df8fc6SFrançois Tigeot }
21587df8fc6SFrançois Tigeot 
21687df8fc6SFrançois Tigeot static inline void i915_gem_request_assign(struct drm_i915_gem_request **pdst,
21787df8fc6SFrançois Tigeot 					   struct drm_i915_gem_request *src)
21887df8fc6SFrançois Tigeot {
21987df8fc6SFrançois Tigeot 	if (src)
22087df8fc6SFrançois Tigeot 		i915_gem_request_get(src);
22187df8fc6SFrançois Tigeot 
22287df8fc6SFrançois Tigeot 	if (*pdst)
22387df8fc6SFrançois Tigeot 		i915_gem_request_put(*pdst);
22487df8fc6SFrançois Tigeot 
22587df8fc6SFrançois Tigeot 	*pdst = src;
22687df8fc6SFrançois Tigeot }
22787df8fc6SFrançois Tigeot 
22887df8fc6SFrançois Tigeot void __i915_add_request(struct drm_i915_gem_request *req,
22987df8fc6SFrançois Tigeot 			struct drm_i915_gem_object *batch_obj,
23087df8fc6SFrançois Tigeot 			bool flush_caches);
23187df8fc6SFrançois Tigeot #define i915_add_request(req) \
23287df8fc6SFrançois Tigeot 	__i915_add_request(req, NULL, true)
23387df8fc6SFrançois Tigeot #define i915_add_request_no_flush(req) \
23487df8fc6SFrançois Tigeot 	__i915_add_request(req, NULL, false)
23587df8fc6SFrançois Tigeot 
23687df8fc6SFrançois Tigeot struct intel_rps_client;
23787df8fc6SFrançois Tigeot #define NO_WAITBOOST ERR_PTR(-1)
23887df8fc6SFrançois Tigeot #define IS_RPS_CLIENT(p) (!IS_ERR(p))
23987df8fc6SFrançois Tigeot #define IS_RPS_USER(p) (!IS_ERR_OR_NULL(p))
24087df8fc6SFrançois Tigeot 
241*71f41f3eSFrançois Tigeot int i915_wait_request(struct drm_i915_gem_request *req,
24287df8fc6SFrançois Tigeot 		      bool interruptible,
24387df8fc6SFrançois Tigeot 		      s64 *timeout,
244*71f41f3eSFrançois Tigeot 		      struct intel_rps_client *rps)
245*71f41f3eSFrançois Tigeot 	__attribute__((nonnull(1)));
24687df8fc6SFrançois Tigeot 
247*71f41f3eSFrançois Tigeot static inline u32 intel_engine_get_seqno(struct intel_engine_cs *engine);
24887df8fc6SFrançois Tigeot 
24987df8fc6SFrançois Tigeot /**
25087df8fc6SFrançois Tigeot  * Returns true if seq1 is later than seq2.
25187df8fc6SFrançois Tigeot  */
25287df8fc6SFrançois Tigeot static inline bool i915_seqno_passed(u32 seq1, u32 seq2)
25387df8fc6SFrançois Tigeot {
25487df8fc6SFrançois Tigeot 	return (s32)(seq1 - seq2) >= 0;
25587df8fc6SFrançois Tigeot }
25687df8fc6SFrançois Tigeot 
25787df8fc6SFrançois Tigeot static inline bool
25887df8fc6SFrançois Tigeot i915_gem_request_started(const struct drm_i915_gem_request *req)
25987df8fc6SFrançois Tigeot {
26087df8fc6SFrançois Tigeot 	return i915_seqno_passed(intel_engine_get_seqno(req->engine),
26187df8fc6SFrançois Tigeot 				 req->previous_seqno);
26287df8fc6SFrançois Tigeot }
26387df8fc6SFrançois Tigeot 
26487df8fc6SFrançois Tigeot static inline bool
26587df8fc6SFrançois Tigeot i915_gem_request_completed(const struct drm_i915_gem_request *req)
26687df8fc6SFrançois Tigeot {
26787df8fc6SFrançois Tigeot 	return i915_seqno_passed(intel_engine_get_seqno(req->engine),
26887df8fc6SFrançois Tigeot 				 req->fence.seqno);
26987df8fc6SFrançois Tigeot }
27087df8fc6SFrançois Tigeot 
27187df8fc6SFrançois Tigeot bool __i915_spin_request(const struct drm_i915_gem_request *request,
27287df8fc6SFrançois Tigeot 			 int state, unsigned long timeout_us);
27387df8fc6SFrançois Tigeot static inline bool i915_spin_request(const struct drm_i915_gem_request *request,
27487df8fc6SFrançois Tigeot 				     int state, unsigned long timeout_us)
27587df8fc6SFrançois Tigeot {
27687df8fc6SFrançois Tigeot 	return (i915_gem_request_started(request) &&
27787df8fc6SFrançois Tigeot 		__i915_spin_request(request, state, timeout_us));
27887df8fc6SFrançois Tigeot }
27987df8fc6SFrançois Tigeot 
280*71f41f3eSFrançois Tigeot /* We treat requests as fences. This is not be to confused with our
281*71f41f3eSFrançois Tigeot  * "fence registers" but pipeline synchronisation objects ala GL_ARB_sync.
282*71f41f3eSFrançois Tigeot  * We use the fences to synchronize access from the CPU with activity on the
283*71f41f3eSFrançois Tigeot  * GPU, for example, we should not rewrite an object's PTE whilst the GPU
284*71f41f3eSFrançois Tigeot  * is reading them. We also track fences at a higher level to provide
285*71f41f3eSFrançois Tigeot  * implicit synchronisation around GEM objects, e.g. set-domain will wait
286*71f41f3eSFrançois Tigeot  * for outstanding GPU rendering before marking the object ready for CPU
287*71f41f3eSFrançois Tigeot  * access, or a pageflip will wait until the GPU is complete before showing
288*71f41f3eSFrançois Tigeot  * the frame on the scanout.
289*71f41f3eSFrançois Tigeot  *
290*71f41f3eSFrançois Tigeot  * In order to use a fence, the object must track the fence it needs to
291*71f41f3eSFrançois Tigeot  * serialise with. For example, GEM objects want to track both read and
292*71f41f3eSFrançois Tigeot  * write access so that we can perform concurrent read operations between
293*71f41f3eSFrançois Tigeot  * the CPU and GPU engines, as well as waiting for all rendering to
294*71f41f3eSFrançois Tigeot  * complete, or waiting for the last GPU user of a "fence register". The
295*71f41f3eSFrançois Tigeot  * object then embeds a #i915_gem_active to track the most recent (in
296*71f41f3eSFrançois Tigeot  * retirement order) request relevant for the desired mode of access.
297*71f41f3eSFrançois Tigeot  * The #i915_gem_active is updated with i915_gem_active_set() to track the
298*71f41f3eSFrançois Tigeot  * most recent fence request, typically this is done as part of
299*71f41f3eSFrançois Tigeot  * i915_vma_move_to_active().
300*71f41f3eSFrançois Tigeot  *
301*71f41f3eSFrançois Tigeot  * When the #i915_gem_active completes (is retired), it will
302*71f41f3eSFrançois Tigeot  * signal its completion to the owner through a callback as well as mark
303*71f41f3eSFrançois Tigeot  * itself as idle (i915_gem_active.request == NULL). The owner
304*71f41f3eSFrançois Tigeot  * can then perform any action, such as delayed freeing of an active
305*71f41f3eSFrançois Tigeot  * resource including itself.
306*71f41f3eSFrançois Tigeot  */
307*71f41f3eSFrançois Tigeot struct i915_gem_active;
308*71f41f3eSFrançois Tigeot 
309*71f41f3eSFrançois Tigeot typedef void (*i915_gem_retire_fn)(struct i915_gem_active *,
310*71f41f3eSFrançois Tigeot 				   struct drm_i915_gem_request *);
311*71f41f3eSFrançois Tigeot 
312*71f41f3eSFrançois Tigeot struct i915_gem_active {
313*71f41f3eSFrançois Tigeot 	struct drm_i915_gem_request __rcu *request;
314*71f41f3eSFrançois Tigeot 	struct list_head link;
315*71f41f3eSFrançois Tigeot 	i915_gem_retire_fn retire;
316*71f41f3eSFrançois Tigeot };
317*71f41f3eSFrançois Tigeot 
318*71f41f3eSFrançois Tigeot void i915_gem_retire_noop(struct i915_gem_active *,
319*71f41f3eSFrançois Tigeot 			  struct drm_i915_gem_request *request);
320*71f41f3eSFrançois Tigeot 
321*71f41f3eSFrançois Tigeot /**
322*71f41f3eSFrançois Tigeot  * init_request_active - prepares the activity tracker for use
323*71f41f3eSFrançois Tigeot  * @active - the active tracker
324*71f41f3eSFrançois Tigeot  * @func - a callback when then the tracker is retired (becomes idle),
325*71f41f3eSFrançois Tigeot  *         can be NULL
326*71f41f3eSFrançois Tigeot  *
327*71f41f3eSFrançois Tigeot  * init_request_active() prepares the embedded @active struct for use as
328*71f41f3eSFrançois Tigeot  * an activity tracker, that is for tracking the last known active request
329*71f41f3eSFrançois Tigeot  * associated with it. When the last request becomes idle, when it is retired
330*71f41f3eSFrançois Tigeot  * after completion, the optional callback @func is invoked.
331*71f41f3eSFrançois Tigeot  */
332*71f41f3eSFrançois Tigeot static inline void
333*71f41f3eSFrançois Tigeot init_request_active(struct i915_gem_active *active,
334*71f41f3eSFrançois Tigeot 		    i915_gem_retire_fn retire)
335*71f41f3eSFrançois Tigeot {
336*71f41f3eSFrançois Tigeot 	INIT_LIST_HEAD(&active->link);
337*71f41f3eSFrançois Tigeot 	active->retire = retire ?: i915_gem_retire_noop;
338*71f41f3eSFrançois Tigeot }
339*71f41f3eSFrançois Tigeot 
340*71f41f3eSFrançois Tigeot /**
341*71f41f3eSFrançois Tigeot  * i915_gem_active_set - updates the tracker to watch the current request
342*71f41f3eSFrançois Tigeot  * @active - the active tracker
343*71f41f3eSFrançois Tigeot  * @request - the request to watch
344*71f41f3eSFrançois Tigeot  *
345*71f41f3eSFrançois Tigeot  * i915_gem_active_set() watches the given @request for completion. Whilst
346*71f41f3eSFrançois Tigeot  * that @request is busy, the @active reports busy. When that @request is
347*71f41f3eSFrançois Tigeot  * retired, the @active tracker is updated to report idle.
348*71f41f3eSFrançois Tigeot  */
349*71f41f3eSFrançois Tigeot static inline void
350*71f41f3eSFrançois Tigeot i915_gem_active_set(struct i915_gem_active *active,
351*71f41f3eSFrançois Tigeot 		    struct drm_i915_gem_request *request)
352*71f41f3eSFrançois Tigeot {
353*71f41f3eSFrançois Tigeot 	list_move(&active->link, &request->active_list);
354*71f41f3eSFrançois Tigeot 	rcu_assign_pointer(active->request, request);
355*71f41f3eSFrançois Tigeot }
356*71f41f3eSFrançois Tigeot 
357*71f41f3eSFrançois Tigeot static inline struct drm_i915_gem_request *
358*71f41f3eSFrançois Tigeot __i915_gem_active_peek(const struct i915_gem_active *active)
359*71f41f3eSFrançois Tigeot {
360*71f41f3eSFrançois Tigeot 	/* Inside the error capture (running with the driver in an unknown
361*71f41f3eSFrançois Tigeot 	 * state), we want to bend the rules slightly (a lot).
362*71f41f3eSFrançois Tigeot 	 *
363*71f41f3eSFrançois Tigeot 	 * Work is in progress to make it safer, in the meantime this keeps
364*71f41f3eSFrançois Tigeot 	 * the known issue from spamming the logs.
365*71f41f3eSFrançois Tigeot 	 */
366*71f41f3eSFrançois Tigeot 	return rcu_dereference_protected(active->request, 1);
367*71f41f3eSFrançois Tigeot }
368*71f41f3eSFrançois Tigeot 
369*71f41f3eSFrançois Tigeot /**
370*71f41f3eSFrançois Tigeot  * i915_gem_active_raw - return the active request
371*71f41f3eSFrançois Tigeot  * @active - the active tracker
372*71f41f3eSFrançois Tigeot  *
373*71f41f3eSFrançois Tigeot  * i915_gem_active_raw() returns the current request being tracked, or NULL.
374*71f41f3eSFrançois Tigeot  * It does not obtain a reference on the request for the caller, so the caller
375*71f41f3eSFrançois Tigeot  * must hold struct_mutex.
376*71f41f3eSFrançois Tigeot  */
377*71f41f3eSFrançois Tigeot static inline struct drm_i915_gem_request *
378*71f41f3eSFrançois Tigeot i915_gem_active_raw(const struct i915_gem_active *active, struct lock *mutex)
379*71f41f3eSFrançois Tigeot {
380*71f41f3eSFrançois Tigeot 	return rcu_dereference_protected(active->request,
381*71f41f3eSFrançois Tigeot 					 lockdep_is_held(mutex));
382*71f41f3eSFrançois Tigeot }
383*71f41f3eSFrançois Tigeot 
384*71f41f3eSFrançois Tigeot /**
385*71f41f3eSFrançois Tigeot  * i915_gem_active_peek - report the active request being monitored
386*71f41f3eSFrançois Tigeot  * @active - the active tracker
387*71f41f3eSFrançois Tigeot  *
388*71f41f3eSFrançois Tigeot  * i915_gem_active_peek() returns the current request being tracked if
389*71f41f3eSFrançois Tigeot  * still active, or NULL. It does not obtain a reference on the request
390*71f41f3eSFrançois Tigeot  * for the caller, so the caller must hold struct_mutex.
391*71f41f3eSFrançois Tigeot  */
392*71f41f3eSFrançois Tigeot static inline struct drm_i915_gem_request *
393*71f41f3eSFrançois Tigeot i915_gem_active_peek(const struct i915_gem_active *active, struct lock *mutex)
394*71f41f3eSFrançois Tigeot {
395*71f41f3eSFrançois Tigeot 	struct drm_i915_gem_request *request;
396*71f41f3eSFrançois Tigeot 
397*71f41f3eSFrançois Tigeot 	request = i915_gem_active_raw(active, mutex);
398*71f41f3eSFrançois Tigeot 	if (!request || i915_gem_request_completed(request))
399*71f41f3eSFrançois Tigeot 		return NULL;
400*71f41f3eSFrançois Tigeot 
401*71f41f3eSFrançois Tigeot 	return request;
402*71f41f3eSFrançois Tigeot }
403*71f41f3eSFrançois Tigeot 
404*71f41f3eSFrançois Tigeot /**
405*71f41f3eSFrançois Tigeot  * i915_gem_active_get - return a reference to the active request
406*71f41f3eSFrançois Tigeot  * @active - the active tracker
407*71f41f3eSFrançois Tigeot  *
408*71f41f3eSFrançois Tigeot  * i915_gem_active_get() returns a reference to the active request, or NULL
409*71f41f3eSFrançois Tigeot  * if the active tracker is idle. The caller must hold struct_mutex.
410*71f41f3eSFrançois Tigeot  */
411*71f41f3eSFrançois Tigeot static inline struct drm_i915_gem_request *
412*71f41f3eSFrançois Tigeot i915_gem_active_get(const struct i915_gem_active *active, struct lock *mutex)
413*71f41f3eSFrançois Tigeot {
414*71f41f3eSFrançois Tigeot 	return i915_gem_request_get(i915_gem_active_peek(active, mutex));
415*71f41f3eSFrançois Tigeot }
416*71f41f3eSFrançois Tigeot 
417*71f41f3eSFrançois Tigeot /**
418*71f41f3eSFrançois Tigeot  * __i915_gem_active_get_rcu - return a reference to the active request
419*71f41f3eSFrançois Tigeot  * @active - the active tracker
420*71f41f3eSFrançois Tigeot  *
421*71f41f3eSFrançois Tigeot  * __i915_gem_active_get() returns a reference to the active request, or NULL
422*71f41f3eSFrançois Tigeot  * if the active tracker is idle. The caller must hold the RCU read lock, but
423*71f41f3eSFrançois Tigeot  * the returned pointer is safe to use outside of RCU.
424*71f41f3eSFrançois Tigeot  */
425*71f41f3eSFrançois Tigeot static inline struct drm_i915_gem_request *
426*71f41f3eSFrançois Tigeot __i915_gem_active_get_rcu(const struct i915_gem_active *active)
427*71f41f3eSFrançois Tigeot {
428*71f41f3eSFrançois Tigeot 	/* Performing a lockless retrieval of the active request is super
429*71f41f3eSFrançois Tigeot 	 * tricky. SLAB_DESTROY_BY_RCU merely guarantees that the backing
430*71f41f3eSFrançois Tigeot 	 * slab of request objects will not be freed whilst we hold the
431*71f41f3eSFrançois Tigeot 	 * RCU read lock. It does not guarantee that the request itself
432*71f41f3eSFrançois Tigeot 	 * will not be freed and then *reused*. Viz,
433*71f41f3eSFrançois Tigeot 	 *
434*71f41f3eSFrançois Tigeot 	 * Thread A			Thread B
435*71f41f3eSFrançois Tigeot 	 *
436*71f41f3eSFrançois Tigeot 	 * req = active.request
437*71f41f3eSFrançois Tigeot 	 *				retire(req) -> free(req);
438*71f41f3eSFrançois Tigeot 	 *				(req is now first on the slab freelist)
439*71f41f3eSFrançois Tigeot 	 *				active.request = NULL
440*71f41f3eSFrançois Tigeot 	 *
441*71f41f3eSFrançois Tigeot 	 *				req = new submission on a new object
442*71f41f3eSFrançois Tigeot 	 * ref(req)
443*71f41f3eSFrançois Tigeot 	 *
444*71f41f3eSFrançois Tigeot 	 * To prevent the request from being reused whilst the caller
445*71f41f3eSFrançois Tigeot 	 * uses it, we take a reference like normal. Whilst acquiring
446*71f41f3eSFrançois Tigeot 	 * the reference we check that it is not in a destroyed state
447*71f41f3eSFrançois Tigeot 	 * (refcnt == 0). That prevents the request being reallocated
448*71f41f3eSFrançois Tigeot 	 * whilst the caller holds on to it. To check that the request
449*71f41f3eSFrançois Tigeot 	 * was not reallocated as we acquired the reference we have to
450*71f41f3eSFrançois Tigeot 	 * check that our request remains the active request across
451*71f41f3eSFrançois Tigeot 	 * the lookup, in the same manner as a seqlock. The visibility
452*71f41f3eSFrançois Tigeot 	 * of the pointer versus the reference counting is controlled
453*71f41f3eSFrançois Tigeot 	 * by using RCU barriers (rcu_dereference and rcu_assign_pointer).
454*71f41f3eSFrançois Tigeot 	 *
455*71f41f3eSFrançois Tigeot 	 * In the middle of all that, we inspect whether the request is
456*71f41f3eSFrançois Tigeot 	 * complete. Retiring is lazy so the request may be completed long
457*71f41f3eSFrançois Tigeot 	 * before the active tracker is updated. Querying whether the
458*71f41f3eSFrançois Tigeot 	 * request is complete is far cheaper (as it involves no locked
459*71f41f3eSFrançois Tigeot 	 * instructions setting cachelines to exclusive) than acquiring
460*71f41f3eSFrançois Tigeot 	 * the reference, so we do it first. The RCU read lock ensures the
461*71f41f3eSFrançois Tigeot 	 * pointer dereference is valid, but does not ensure that the
462*71f41f3eSFrançois Tigeot 	 * seqno nor HWS is the right one! However, if the request was
463*71f41f3eSFrançois Tigeot 	 * reallocated, that means the active tracker's request was complete.
464*71f41f3eSFrançois Tigeot 	 * If the new request is also complete, then both are and we can
465*71f41f3eSFrançois Tigeot 	 * just report the active tracker is idle. If the new request is
466*71f41f3eSFrançois Tigeot 	 * incomplete, then we acquire a reference on it and check that
467*71f41f3eSFrançois Tigeot 	 * it remained the active request.
468*71f41f3eSFrançois Tigeot 	 *
469*71f41f3eSFrançois Tigeot 	 * It is then imperative that we do not zero the request on
470*71f41f3eSFrançois Tigeot 	 * reallocation, so that we can chase the dangling pointers!
471*71f41f3eSFrançois Tigeot 	 * See i915_gem_request_alloc().
472*71f41f3eSFrançois Tigeot 	 */
473*71f41f3eSFrançois Tigeot 	do {
474*71f41f3eSFrançois Tigeot 		struct drm_i915_gem_request *request;
475*71f41f3eSFrançois Tigeot 
476*71f41f3eSFrançois Tigeot 		request = rcu_dereference(active->request);
477*71f41f3eSFrançois Tigeot 		if (!request || i915_gem_request_completed(request))
478*71f41f3eSFrançois Tigeot 			return NULL;
479*71f41f3eSFrançois Tigeot 
480*71f41f3eSFrançois Tigeot 		request = i915_gem_request_get_rcu(request);
481*71f41f3eSFrançois Tigeot 
482*71f41f3eSFrançois Tigeot 		/* What stops the following rcu_access_pointer() from occurring
483*71f41f3eSFrançois Tigeot 		 * before the above i915_gem_request_get_rcu()? If we were
484*71f41f3eSFrançois Tigeot 		 * to read the value before pausing to get the reference to
485*71f41f3eSFrançois Tigeot 		 * the request, we may not notice a change in the active
486*71f41f3eSFrançois Tigeot 		 * tracker.
487*71f41f3eSFrançois Tigeot 		 *
488*71f41f3eSFrançois Tigeot 		 * The rcu_access_pointer() is a mere compiler barrier, which
489*71f41f3eSFrançois Tigeot 		 * means both the CPU and compiler are free to perform the
490*71f41f3eSFrançois Tigeot 		 * memory read without constraint. The compiler only has to
491*71f41f3eSFrançois Tigeot 		 * ensure that any operations after the rcu_access_pointer()
492*71f41f3eSFrançois Tigeot 		 * occur afterwards in program order. This means the read may
493*71f41f3eSFrançois Tigeot 		 * be performed earlier by an out-of-order CPU, or adventurous
494*71f41f3eSFrançois Tigeot 		 * compiler.
495*71f41f3eSFrançois Tigeot 		 *
496*71f41f3eSFrançois Tigeot 		 * The atomic operation at the heart of
497*71f41f3eSFrançois Tigeot 		 * i915_gem_request_get_rcu(), see fence_get_rcu(), is
498*71f41f3eSFrançois Tigeot 		 * atomic_inc_not_zero() which is only a full memory barrier
499*71f41f3eSFrançois Tigeot 		 * when successful. That is, if i915_gem_request_get_rcu()
500*71f41f3eSFrançois Tigeot 		 * returns the request (and so with the reference counted
501*71f41f3eSFrançois Tigeot 		 * incremented) then the following read for rcu_access_pointer()
502*71f41f3eSFrançois Tigeot 		 * must occur after the atomic operation and so confirm
503*71f41f3eSFrançois Tigeot 		 * that this request is the one currently being tracked.
504*71f41f3eSFrançois Tigeot 		 *
505*71f41f3eSFrançois Tigeot 		 * The corresponding write barrier is part of
506*71f41f3eSFrançois Tigeot 		 * rcu_assign_pointer().
507*71f41f3eSFrançois Tigeot 		 */
508*71f41f3eSFrançois Tigeot 		if (!request || request == rcu_access_pointer(active->request))
509*71f41f3eSFrançois Tigeot 			return rcu_pointer_handoff(request);
510*71f41f3eSFrançois Tigeot 
511*71f41f3eSFrançois Tigeot 		i915_gem_request_put(request);
512*71f41f3eSFrançois Tigeot 	} while (1);
513*71f41f3eSFrançois Tigeot }
514*71f41f3eSFrançois Tigeot 
515*71f41f3eSFrançois Tigeot /**
516*71f41f3eSFrançois Tigeot  * i915_gem_active_get_unlocked - return a reference to the active request
517*71f41f3eSFrançois Tigeot  * @active - the active tracker
518*71f41f3eSFrançois Tigeot  *
519*71f41f3eSFrançois Tigeot  * i915_gem_active_get_unlocked() returns a reference to the active request,
520*71f41f3eSFrançois Tigeot  * or NULL if the active tracker is idle. The reference is obtained under RCU,
521*71f41f3eSFrançois Tigeot  * so no locking is required by the caller.
522*71f41f3eSFrançois Tigeot  *
523*71f41f3eSFrançois Tigeot  * The reference should be freed with i915_gem_request_put().
524*71f41f3eSFrançois Tigeot  */
525*71f41f3eSFrançois Tigeot static inline struct drm_i915_gem_request *
526*71f41f3eSFrançois Tigeot i915_gem_active_get_unlocked(const struct i915_gem_active *active)
527*71f41f3eSFrançois Tigeot {
528*71f41f3eSFrançois Tigeot 	struct drm_i915_gem_request *request;
529*71f41f3eSFrançois Tigeot 
530*71f41f3eSFrançois Tigeot 	rcu_read_lock();
531*71f41f3eSFrançois Tigeot 	request = __i915_gem_active_get_rcu(active);
532*71f41f3eSFrançois Tigeot 	rcu_read_unlock();
533*71f41f3eSFrançois Tigeot 
534*71f41f3eSFrançois Tigeot 	return request;
535*71f41f3eSFrançois Tigeot }
536*71f41f3eSFrançois Tigeot 
537*71f41f3eSFrançois Tigeot /**
538*71f41f3eSFrançois Tigeot  * i915_gem_active_isset - report whether the active tracker is assigned
539*71f41f3eSFrançois Tigeot  * @active - the active tracker
540*71f41f3eSFrançois Tigeot  *
541*71f41f3eSFrançois Tigeot  * i915_gem_active_isset() returns true if the active tracker is currently
542*71f41f3eSFrançois Tigeot  * assigned to a request. Due to the lazy retiring, that request may be idle
543*71f41f3eSFrançois Tigeot  * and this may report stale information.
544*71f41f3eSFrançois Tigeot  */
545*71f41f3eSFrançois Tigeot static inline bool
546*71f41f3eSFrançois Tigeot i915_gem_active_isset(const struct i915_gem_active *active)
547*71f41f3eSFrançois Tigeot {
548*71f41f3eSFrançois Tigeot 	return rcu_access_pointer(active->request);
549*71f41f3eSFrançois Tigeot }
550*71f41f3eSFrançois Tigeot 
551*71f41f3eSFrançois Tigeot /**
552*71f41f3eSFrançois Tigeot  * i915_gem_active_is_idle - report whether the active tracker is idle
553*71f41f3eSFrançois Tigeot  * @active - the active tracker
554*71f41f3eSFrançois Tigeot  *
555*71f41f3eSFrançois Tigeot  * i915_gem_active_is_idle() returns true if the active tracker is currently
556*71f41f3eSFrançois Tigeot  * unassigned or if the request is complete (but not yet retired). Requires
557*71f41f3eSFrançois Tigeot  * the caller to hold struct_mutex (but that can be relaxed if desired).
558*71f41f3eSFrançois Tigeot  */
559*71f41f3eSFrançois Tigeot static inline bool
560*71f41f3eSFrançois Tigeot i915_gem_active_is_idle(const struct i915_gem_active *active,
561*71f41f3eSFrançois Tigeot 			struct lock *mutex)
562*71f41f3eSFrançois Tigeot {
563*71f41f3eSFrançois Tigeot 	return !i915_gem_active_peek(active, mutex);
564*71f41f3eSFrançois Tigeot }
565*71f41f3eSFrançois Tigeot 
566*71f41f3eSFrançois Tigeot /**
567*71f41f3eSFrançois Tigeot  * i915_gem_active_wait - waits until the request is completed
568*71f41f3eSFrançois Tigeot  * @active - the active request on which to wait
569*71f41f3eSFrançois Tigeot  *
570*71f41f3eSFrançois Tigeot  * i915_gem_active_wait() waits until the request is completed before
571*71f41f3eSFrançois Tigeot  * returning. Note that it does not guarantee that the request is
572*71f41f3eSFrançois Tigeot  * retired first, see i915_gem_active_retire().
573*71f41f3eSFrançois Tigeot  *
574*71f41f3eSFrançois Tigeot  * i915_gem_active_wait() returns immediately if the active
575*71f41f3eSFrançois Tigeot  * request is already complete.
576*71f41f3eSFrançois Tigeot  */
577*71f41f3eSFrançois Tigeot static inline int __must_check
578*71f41f3eSFrançois Tigeot i915_gem_active_wait(const struct i915_gem_active *active, struct lock *mutex)
579*71f41f3eSFrançois Tigeot {
580*71f41f3eSFrançois Tigeot 	struct drm_i915_gem_request *request;
581*71f41f3eSFrançois Tigeot 
582*71f41f3eSFrançois Tigeot 	request = i915_gem_active_peek(active, mutex);
583*71f41f3eSFrançois Tigeot 	if (!request)
584*71f41f3eSFrançois Tigeot 		return 0;
585*71f41f3eSFrançois Tigeot 
586*71f41f3eSFrançois Tigeot 	return i915_wait_request(request, true, NULL, NULL);
587*71f41f3eSFrançois Tigeot }
588*71f41f3eSFrançois Tigeot 
589*71f41f3eSFrançois Tigeot /**
590*71f41f3eSFrançois Tigeot  * i915_gem_active_wait_unlocked - waits until the request is completed
591*71f41f3eSFrançois Tigeot  * @active - the active request on which to wait
592*71f41f3eSFrançois Tigeot  * @interruptible - whether the wait can be woken by a userspace signal
593*71f41f3eSFrançois Tigeot  * @timeout - how long to wait at most
594*71f41f3eSFrançois Tigeot  * @rps - userspace client to charge for a waitboost
595*71f41f3eSFrançois Tigeot  *
596*71f41f3eSFrançois Tigeot  * i915_gem_active_wait_unlocked() waits until the request is completed before
597*71f41f3eSFrançois Tigeot  * returning, without requiring any locks to be held. Note that it does not
598*71f41f3eSFrançois Tigeot  * retire any requests before returning.
599*71f41f3eSFrançois Tigeot  *
600*71f41f3eSFrançois Tigeot  * This function relies on RCU in order to acquire the reference to the active
601*71f41f3eSFrançois Tigeot  * request without holding any locks. See __i915_gem_active_get_rcu() for the
602*71f41f3eSFrançois Tigeot  * glory details on how that is managed. Once the reference is acquired, we
603*71f41f3eSFrançois Tigeot  * can then wait upon the request, and afterwards release our reference,
604*71f41f3eSFrançois Tigeot  * free of any locking.
605*71f41f3eSFrançois Tigeot  *
606*71f41f3eSFrançois Tigeot  * This function wraps i915_wait_request(), see it for the full details on
607*71f41f3eSFrançois Tigeot  * the arguments.
608*71f41f3eSFrançois Tigeot  *
609*71f41f3eSFrançois Tigeot  * Returns 0 if successful, or a negative error code.
610*71f41f3eSFrançois Tigeot  */
611*71f41f3eSFrançois Tigeot static inline int
612*71f41f3eSFrançois Tigeot i915_gem_active_wait_unlocked(const struct i915_gem_active *active,
613*71f41f3eSFrançois Tigeot 			      bool interruptible,
614*71f41f3eSFrançois Tigeot 			      s64 *timeout,
615*71f41f3eSFrançois Tigeot 			      struct intel_rps_client *rps)
616*71f41f3eSFrançois Tigeot {
617*71f41f3eSFrançois Tigeot 	struct drm_i915_gem_request *request;
618*71f41f3eSFrançois Tigeot 	int ret = 0;
619*71f41f3eSFrançois Tigeot 
620*71f41f3eSFrançois Tigeot 	request = i915_gem_active_get_unlocked(active);
621*71f41f3eSFrançois Tigeot 	if (request) {
622*71f41f3eSFrançois Tigeot 		ret = i915_wait_request(request, interruptible, timeout, rps);
623*71f41f3eSFrançois Tigeot 		i915_gem_request_put(request);
624*71f41f3eSFrançois Tigeot 	}
625*71f41f3eSFrançois Tigeot 
626*71f41f3eSFrançois Tigeot 	return ret;
627*71f41f3eSFrançois Tigeot }
628*71f41f3eSFrançois Tigeot 
629*71f41f3eSFrançois Tigeot /**
630*71f41f3eSFrançois Tigeot  * i915_gem_active_retire - waits until the request is retired
631*71f41f3eSFrançois Tigeot  * @active - the active request on which to wait
632*71f41f3eSFrançois Tigeot  *
633*71f41f3eSFrançois Tigeot  * i915_gem_active_retire() waits until the request is completed,
634*71f41f3eSFrançois Tigeot  * and then ensures that at least the retirement handler for this
635*71f41f3eSFrançois Tigeot  * @active tracker is called before returning. If the @active
636*71f41f3eSFrançois Tigeot  * tracker is idle, the function returns immediately.
637*71f41f3eSFrançois Tigeot  */
638*71f41f3eSFrançois Tigeot static inline int __must_check
639*71f41f3eSFrançois Tigeot i915_gem_active_retire(struct i915_gem_active *active,
640*71f41f3eSFrançois Tigeot 		       struct lock *mutex)
641*71f41f3eSFrançois Tigeot {
642*71f41f3eSFrançois Tigeot 	struct drm_i915_gem_request *request;
643*71f41f3eSFrançois Tigeot 	int ret;
644*71f41f3eSFrançois Tigeot 
645*71f41f3eSFrançois Tigeot 	request = i915_gem_active_raw(active, mutex);
646*71f41f3eSFrançois Tigeot 	if (!request)
647*71f41f3eSFrançois Tigeot 		return 0;
648*71f41f3eSFrançois Tigeot 
649*71f41f3eSFrançois Tigeot 	ret = i915_wait_request(request, true, NULL, NULL);
650*71f41f3eSFrançois Tigeot 	if (ret)
651*71f41f3eSFrançois Tigeot 		return ret;
652*71f41f3eSFrançois Tigeot 
653*71f41f3eSFrançois Tigeot 	list_del_init(&active->link);
654*71f41f3eSFrançois Tigeot 	RCU_INIT_POINTER(active->request, NULL);
655*71f41f3eSFrançois Tigeot 
656*71f41f3eSFrançois Tigeot 	active->retire(active, request);
657*71f41f3eSFrançois Tigeot 
658*71f41f3eSFrançois Tigeot 	return 0;
659*71f41f3eSFrançois Tigeot }
660*71f41f3eSFrançois Tigeot 
661*71f41f3eSFrançois Tigeot /* Convenience functions for peeking at state inside active's request whilst
662*71f41f3eSFrançois Tigeot  * guarded by the struct_mutex.
663*71f41f3eSFrançois Tigeot  */
664*71f41f3eSFrançois Tigeot 
665*71f41f3eSFrançois Tigeot static inline uint32_t
666*71f41f3eSFrançois Tigeot i915_gem_active_get_seqno(const struct i915_gem_active *active,
667*71f41f3eSFrançois Tigeot 			  struct lock *mutex)
668*71f41f3eSFrançois Tigeot {
669*71f41f3eSFrançois Tigeot 	return i915_gem_request_get_seqno(i915_gem_active_peek(active, mutex));
670*71f41f3eSFrançois Tigeot }
671*71f41f3eSFrançois Tigeot 
672*71f41f3eSFrançois Tigeot static inline struct intel_engine_cs *
673*71f41f3eSFrançois Tigeot i915_gem_active_get_engine(const struct i915_gem_active *active,
674*71f41f3eSFrançois Tigeot 			   struct lock *mutex)
675*71f41f3eSFrançois Tigeot {
676*71f41f3eSFrançois Tigeot 	return i915_gem_request_get_engine(i915_gem_active_peek(active, mutex));
677*71f41f3eSFrançois Tigeot }
678*71f41f3eSFrançois Tigeot 
679*71f41f3eSFrançois Tigeot #define for_each_active(mask, idx) \
680*71f41f3eSFrançois Tigeot 	for (; mask ? idx = ffs(mask) - 1, 1 : 0; mask &= ~BIT(idx))
681*71f41f3eSFrançois Tigeot 
68287df8fc6SFrançois Tigeot #endif /* I915_GEM_REQUEST_H */
683