xref: /dflybsd-src/sys/dev/drm/i915/i915_gem_request.h (revision a85cb24f18e3804e75ab8bcda7692564d0563317)
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 
286559babbSFrançois Tigeot #include <linux/dma-fence.h>
2987df8fc6SFrançois Tigeot 
3087df8fc6SFrançois Tigeot #include "i915_gem.h"
311e12ee3bSFrançois Tigeot #include "i915_sw_fence.h"
3287df8fc6SFrançois Tigeot 
334be47400SFrançois Tigeot struct drm_file;
344be47400SFrançois Tigeot struct drm_i915_gem_object;
35*a85cb24fSFrançois Tigeot struct drm_i915_gem_request;
364be47400SFrançois Tigeot 
3771f41f3eSFrançois Tigeot struct intel_wait {
3871f41f3eSFrançois Tigeot 	struct rb_node node;
3971f41f3eSFrançois Tigeot 	struct task_struct *tsk;
40*a85cb24fSFrançois Tigeot 	struct drm_i915_gem_request *request;
4171f41f3eSFrançois Tigeot 	u32 seqno;
4271f41f3eSFrançois Tigeot };
4371f41f3eSFrançois Tigeot 
4471f41f3eSFrançois Tigeot struct intel_signal_node {
4571f41f3eSFrançois Tigeot 	struct rb_node node;
4671f41f3eSFrançois Tigeot 	struct intel_wait wait;
4771f41f3eSFrançois Tigeot };
4871f41f3eSFrançois Tigeot 
494be47400SFrançois Tigeot struct i915_dependency {
504be47400SFrançois Tigeot 	struct i915_priotree *signaler;
514be47400SFrançois Tigeot 	struct list_head signal_link;
524be47400SFrançois Tigeot 	struct list_head wait_link;
534be47400SFrançois Tigeot 	struct list_head dfs_link;
544be47400SFrançois Tigeot 	unsigned long flags;
554be47400SFrançois Tigeot #define I915_DEPENDENCY_ALLOC BIT(0)
564be47400SFrançois Tigeot };
574be47400SFrançois Tigeot 
584be47400SFrançois Tigeot /* Requests exist in a complex web of interdependencies. Each request
594be47400SFrançois Tigeot  * has to wait for some other request to complete before it is ready to be run
604be47400SFrançois Tigeot  * (e.g. we have to wait until the pixels have been rendering into a texture
614be47400SFrançois Tigeot  * before we can copy from it). We track the readiness of a request in terms
624be47400SFrançois Tigeot  * of fences, but we also need to keep the dependency tree for the lifetime
634be47400SFrançois Tigeot  * of the request (beyond the life of an individual fence). We use the tree
644be47400SFrançois Tigeot  * at various points to reorder the requests whilst keeping the requests
654be47400SFrançois Tigeot  * in order with respect to their various dependencies.
664be47400SFrançois Tigeot  */
674be47400SFrançois Tigeot struct i915_priotree {
684be47400SFrançois Tigeot 	struct list_head signalers_list; /* those before us, we depend upon */
694be47400SFrançois Tigeot 	struct list_head waiters_list; /* those after us, they depend upon us */
704be47400SFrançois Tigeot 	struct rb_node node;
714be47400SFrançois Tigeot 	int priority;
724be47400SFrançois Tigeot #define I915_PRIORITY_MAX 1024
734be47400SFrançois Tigeot #define I915_PRIORITY_MIN (-I915_PRIORITY_MAX)
744be47400SFrançois Tigeot };
754be47400SFrançois Tigeot 
7687df8fc6SFrançois Tigeot /**
7787df8fc6SFrançois Tigeot  * Request queue structure.
7887df8fc6SFrançois Tigeot  *
7987df8fc6SFrançois Tigeot  * The request queue allows us to note sequence numbers that have been emitted
8087df8fc6SFrançois Tigeot  * and may be associated with active buffers to be retired.
8187df8fc6SFrançois Tigeot  *
8287df8fc6SFrançois Tigeot  * By keeping this list, we can avoid having to do questionable sequence
8387df8fc6SFrançois Tigeot  * number comparisons on buffer last_read|write_seqno. It also allows an
8487df8fc6SFrançois Tigeot  * emission time to be associated with the request for tracking how far ahead
8587df8fc6SFrançois Tigeot  * of the GPU the submission is.
8687df8fc6SFrançois Tigeot  *
8771f41f3eSFrançois Tigeot  * When modifying this structure be very aware that we perform a lockless
8871f41f3eSFrançois Tigeot  * RCU lookup of it that may race against reallocation of the struct
8971f41f3eSFrançois Tigeot  * from the slab freelist. We intentionally do not zero the structure on
9071f41f3eSFrançois Tigeot  * allocation so that the lookup can use the dangling pointers (and is
9171f41f3eSFrançois Tigeot  * cogniscent that those pointers may be wrong). Instead, everything that
9271f41f3eSFrançois Tigeot  * needs to be initialised must be done so explicitly.
9371f41f3eSFrançois Tigeot  *
9487df8fc6SFrançois Tigeot  * The requests are reference counted.
9587df8fc6SFrançois Tigeot  */
9687df8fc6SFrançois Tigeot struct drm_i915_gem_request {
976559babbSFrançois Tigeot 	struct dma_fence fence;
986559babbSFrançois Tigeot 	spinlock_t lock;
9987df8fc6SFrançois Tigeot 
10087df8fc6SFrançois Tigeot 	/** On Which ring this request was generated */
10187df8fc6SFrançois Tigeot 	struct drm_i915_private *i915;
10287df8fc6SFrançois Tigeot 
10387df8fc6SFrançois Tigeot 	/**
10487df8fc6SFrançois Tigeot 	 * Context and ring buffer related to this request
10587df8fc6SFrançois Tigeot 	 * Contexts are refcounted, so when this request is associated with a
10687df8fc6SFrançois Tigeot 	 * context, we must increment the context's refcount, to guarantee that
10787df8fc6SFrançois Tigeot 	 * it persists while any request is linked to it. Requests themselves
10887df8fc6SFrançois Tigeot 	 * are also refcounted, so the request will only be freed when the last
10987df8fc6SFrançois Tigeot 	 * reference to it is dismissed, and the code in
11087df8fc6SFrançois Tigeot 	 * i915_gem_request_free() will then decrement the refcount on the
11187df8fc6SFrançois Tigeot 	 * context.
11287df8fc6SFrançois Tigeot 	 */
11387df8fc6SFrançois Tigeot 	struct i915_gem_context *ctx;
11487df8fc6SFrançois Tigeot 	struct intel_engine_cs *engine;
11571f41f3eSFrançois Tigeot 	struct intel_ring *ring;
1164be47400SFrançois Tigeot 	struct intel_timeline *timeline;
11787df8fc6SFrançois Tigeot 	struct intel_signal_node signaling;
11887df8fc6SFrançois Tigeot 
1194be47400SFrançois Tigeot 	/* Fences for the various phases in the request's lifetime.
1204be47400SFrançois Tigeot 	 *
1214be47400SFrançois Tigeot 	 * The submit fence is used to await upon all of the request's
1224be47400SFrançois Tigeot 	 * dependencies. When it is signaled, the request is ready to run.
1234be47400SFrançois Tigeot 	 * It is used by the driver to then queue the request for execution.
1244be47400SFrançois Tigeot 	 */
1251e12ee3bSFrançois Tigeot 	struct i915_sw_fence submit;
1261e12ee3bSFrançois Tigeot 	wait_queue_t submitq;
127*a85cb24fSFrançois Tigeot 	wait_queue_head_t execute;
1284be47400SFrançois Tigeot 
1294be47400SFrançois Tigeot 	/* A list of everyone we wait upon, and everyone who waits upon us.
1304be47400SFrançois Tigeot 	 * Even though we will not be submitted to the hardware before the
1314be47400SFrançois Tigeot 	 * submit fence is signaled (it waits for all external events as well
1324be47400SFrançois Tigeot 	 * as our own requests), the scheduler still needs to know the
1334be47400SFrançois Tigeot 	 * dependency tree for the lifetime of the request (from execbuf
1344be47400SFrançois Tigeot 	 * to retirement), i.e. bidirectional dependency information for the
1354be47400SFrançois Tigeot 	 * request not tied to individual fences.
1364be47400SFrançois Tigeot 	 */
1374be47400SFrançois Tigeot 	struct i915_priotree priotree;
1384be47400SFrançois Tigeot 	struct i915_dependency dep;
1394be47400SFrançois Tigeot 
140*a85cb24fSFrançois Tigeot 	/** GEM sequence number associated with this request on the
141*a85cb24fSFrançois Tigeot 	 * global execution timeline. It is zero when the request is not
142*a85cb24fSFrançois Tigeot 	 * on the HW queue (i.e. not on the engine timeline list).
143*a85cb24fSFrançois Tigeot 	 * Its value is guarded by the timeline spinlock.
14487df8fc6SFrançois Tigeot 	 */
145*a85cb24fSFrançois Tigeot 	u32 global_seqno;
14687df8fc6SFrançois Tigeot 
1471e12ee3bSFrançois Tigeot 	/** Position in the ring of the start of the request */
14887df8fc6SFrançois Tigeot 	u32 head;
14987df8fc6SFrançois Tigeot 
15087df8fc6SFrançois Tigeot 	/**
1511e12ee3bSFrançois Tigeot 	 * Position in the ring of the start of the postfix.
1521e12ee3bSFrançois Tigeot 	 * This is required to calculate the maximum available ring space
1531e12ee3bSFrançois Tigeot 	 * without overwriting the postfix.
15487df8fc6SFrançois Tigeot 	 */
15587df8fc6SFrançois Tigeot 	u32 postfix;
15687df8fc6SFrançois Tigeot 
1571e12ee3bSFrançois Tigeot 	/** Position in the ring of the end of the whole request */
15887df8fc6SFrançois Tigeot 	u32 tail;
15987df8fc6SFrançois Tigeot 
1601e12ee3bSFrançois Tigeot 	/** Position in the ring of the end of any workarounds after the tail */
1611e12ee3bSFrançois Tigeot 	u32 wa_tail;
1621e12ee3bSFrançois Tigeot 
1631e12ee3bSFrançois Tigeot 	/** Preallocate space in the ring for the emitting the request */
16487df8fc6SFrançois Tigeot 	u32 reserved_space;
16587df8fc6SFrançois Tigeot 
16687df8fc6SFrançois Tigeot 	/** Batch buffer related to this request if any (used for
16787df8fc6SFrançois Tigeot 	 * error state dump only).
16887df8fc6SFrançois Tigeot 	 */
1691e12ee3bSFrançois Tigeot 	struct i915_vma *batch;
17071f41f3eSFrançois Tigeot 	struct list_head active_list;
17187df8fc6SFrançois Tigeot 
17287df8fc6SFrançois Tigeot 	/** Time at which this request was emitted, in jiffies. */
17387df8fc6SFrançois Tigeot 	unsigned long emitted_jiffies;
17487df8fc6SFrançois Tigeot 
17571f41f3eSFrançois Tigeot 	/** engine->request_list entry for this request */
17671f41f3eSFrançois Tigeot 	struct list_head link;
17771f41f3eSFrançois Tigeot 
17871f41f3eSFrançois Tigeot 	/** ring->request_list entry for this request */
17971f41f3eSFrançois Tigeot 	struct list_head ring_link;
18087df8fc6SFrançois Tigeot 
18187df8fc6SFrançois Tigeot 	struct drm_i915_file_private *file_priv;
18287df8fc6SFrançois Tigeot 	/** file_priv list entry for this request */
183*a85cb24fSFrançois Tigeot 	struct list_head client_link;
18487df8fc6SFrançois Tigeot };
18587df8fc6SFrançois Tigeot 
1866559babbSFrançois Tigeot extern const struct dma_fence_ops i915_fence_ops;
18787df8fc6SFrançois Tigeot 
1884be47400SFrançois Tigeot static inline bool dma_fence_is_i915(const struct dma_fence *fence)
18987df8fc6SFrançois Tigeot {
19087df8fc6SFrançois Tigeot 	return fence->ops == &i915_fence_ops;
19187df8fc6SFrançois Tigeot }
19287df8fc6SFrançois Tigeot 
19387df8fc6SFrançois Tigeot struct drm_i915_gem_request * __must_check
19487df8fc6SFrançois Tigeot i915_gem_request_alloc(struct intel_engine_cs *engine,
19587df8fc6SFrançois Tigeot 		       struct i915_gem_context *ctx);
19687df8fc6SFrançois Tigeot void i915_gem_request_retire_upto(struct drm_i915_gem_request *req);
19787df8fc6SFrançois Tigeot 
19887df8fc6SFrançois Tigeot static inline struct drm_i915_gem_request *
1996559babbSFrançois Tigeot to_request(struct dma_fence *fence)
20087df8fc6SFrançois Tigeot {
20187df8fc6SFrançois Tigeot 	/* We assume that NULL fence/request are interoperable */
20287df8fc6SFrançois Tigeot 	BUILD_BUG_ON(offsetof(struct drm_i915_gem_request, fence) != 0);
2034be47400SFrançois Tigeot 	GEM_BUG_ON(fence && !dma_fence_is_i915(fence));
20487df8fc6SFrançois Tigeot 	return container_of(fence, struct drm_i915_gem_request, fence);
20587df8fc6SFrançois Tigeot }
20687df8fc6SFrançois Tigeot 
20787df8fc6SFrançois Tigeot static inline struct drm_i915_gem_request *
20887df8fc6SFrançois Tigeot i915_gem_request_get(struct drm_i915_gem_request *req)
20987df8fc6SFrançois Tigeot {
2106559babbSFrançois Tigeot 	return to_request(dma_fence_get(&req->fence));
21187df8fc6SFrançois Tigeot }
21287df8fc6SFrançois Tigeot 
21371f41f3eSFrançois Tigeot static inline struct drm_i915_gem_request *
21471f41f3eSFrançois Tigeot i915_gem_request_get_rcu(struct drm_i915_gem_request *req)
21571f41f3eSFrançois Tigeot {
2166559babbSFrançois Tigeot 	return to_request(dma_fence_get_rcu(&req->fence));
21771f41f3eSFrançois Tigeot }
21871f41f3eSFrançois Tigeot 
21987df8fc6SFrançois Tigeot static inline void
22087df8fc6SFrançois Tigeot i915_gem_request_put(struct drm_i915_gem_request *req)
22187df8fc6SFrançois Tigeot {
2226559babbSFrançois Tigeot 	dma_fence_put(&req->fence);
22387df8fc6SFrançois Tigeot }
22487df8fc6SFrançois Tigeot 
22587df8fc6SFrançois Tigeot static inline void i915_gem_request_assign(struct drm_i915_gem_request **pdst,
22687df8fc6SFrançois Tigeot 					   struct drm_i915_gem_request *src)
22787df8fc6SFrançois Tigeot {
22887df8fc6SFrançois Tigeot 	if (src)
22987df8fc6SFrançois Tigeot 		i915_gem_request_get(src);
23087df8fc6SFrançois Tigeot 
23187df8fc6SFrançois Tigeot 	if (*pdst)
23287df8fc6SFrançois Tigeot 		i915_gem_request_put(*pdst);
23387df8fc6SFrançois Tigeot 
23487df8fc6SFrançois Tigeot 	*pdst = src;
23587df8fc6SFrançois Tigeot }
23687df8fc6SFrançois Tigeot 
237*a85cb24fSFrançois Tigeot /**
238*a85cb24fSFrançois Tigeot  * i915_gem_request_global_seqno - report the current global seqno
239*a85cb24fSFrançois Tigeot  * @request - the request
240*a85cb24fSFrançois Tigeot  *
241*a85cb24fSFrançois Tigeot  * A request is assigned a global seqno only when it is on the hardware
242*a85cb24fSFrançois Tigeot  * execution queue. The global seqno can be used to maintain a list of
243*a85cb24fSFrançois Tigeot  * requests on the same engine in retirement order, for example for
244*a85cb24fSFrançois Tigeot  * constructing a priority queue for waiting. Prior to its execution, or
245*a85cb24fSFrançois Tigeot  * if it is subsequently removed in the event of preemption, its global
246*a85cb24fSFrançois Tigeot  * seqno is zero. As both insertion and removal from the execution queue
247*a85cb24fSFrançois Tigeot  * may operate in IRQ context, it is not guarded by the usual struct_mutex
248*a85cb24fSFrançois Tigeot  * BKL. Instead those relying on the global seqno must be prepared for its
249*a85cb24fSFrançois Tigeot  * value to change between reads. Only when the request is complete can
250*a85cb24fSFrançois Tigeot  * the global seqno be stable (due to the memory barriers on submitting
251*a85cb24fSFrançois Tigeot  * the commands to the hardware to write the breadcrumb, if the HWS shows
252*a85cb24fSFrançois Tigeot  * that it has passed the global seqno and the global seqno is unchanged
253*a85cb24fSFrançois Tigeot  * after the read, it is indeed complete).
254*a85cb24fSFrançois Tigeot  */
255*a85cb24fSFrançois Tigeot static u32
256*a85cb24fSFrançois Tigeot i915_gem_request_global_seqno(const struct drm_i915_gem_request *request)
257*a85cb24fSFrançois Tigeot {
258*a85cb24fSFrançois Tigeot 	return READ_ONCE(request->global_seqno);
259*a85cb24fSFrançois Tigeot }
260*a85cb24fSFrançois Tigeot 
2611e12ee3bSFrançois Tigeot int
2621e12ee3bSFrançois Tigeot i915_gem_request_await_object(struct drm_i915_gem_request *to,
2631e12ee3bSFrançois Tigeot 			      struct drm_i915_gem_object *obj,
2641e12ee3bSFrançois Tigeot 			      bool write);
2654be47400SFrançois Tigeot int i915_gem_request_await_dma_fence(struct drm_i915_gem_request *req,
2664be47400SFrançois Tigeot 				     struct dma_fence *fence);
2671e12ee3bSFrançois Tigeot 
2681e12ee3bSFrançois Tigeot void __i915_add_request(struct drm_i915_gem_request *req, bool flush_caches);
26987df8fc6SFrançois Tigeot #define i915_add_request(req) \
2701e12ee3bSFrançois Tigeot 	__i915_add_request(req, false)
27187df8fc6SFrançois Tigeot 
2724be47400SFrançois Tigeot void __i915_gem_request_submit(struct drm_i915_gem_request *request);
2734be47400SFrançois Tigeot void i915_gem_request_submit(struct drm_i915_gem_request *request);
2744be47400SFrançois Tigeot 
275*a85cb24fSFrançois Tigeot void __i915_gem_request_unsubmit(struct drm_i915_gem_request *request);
276*a85cb24fSFrançois Tigeot void i915_gem_request_unsubmit(struct drm_i915_gem_request *request);
277*a85cb24fSFrançois Tigeot 
27887df8fc6SFrançois Tigeot struct intel_rps_client;
27987df8fc6SFrançois Tigeot #define NO_WAITBOOST ERR_PTR(-1)
28087df8fc6SFrançois Tigeot #define IS_RPS_CLIENT(p) (!IS_ERR(p))
28187df8fc6SFrançois Tigeot #define IS_RPS_USER(p) (!IS_ERR_OR_NULL(p))
28287df8fc6SFrançois Tigeot 
2834be47400SFrançois Tigeot long i915_wait_request(struct drm_i915_gem_request *req,
2841e12ee3bSFrançois Tigeot 		       unsigned int flags,
2854be47400SFrançois Tigeot 		       long timeout)
28671f41f3eSFrançois Tigeot 	__attribute__((nonnull(1)));
2871e12ee3bSFrançois Tigeot #define I915_WAIT_INTERRUPTIBLE	BIT(0)
2881e12ee3bSFrançois Tigeot #define I915_WAIT_LOCKED	BIT(1) /* struct_mutex held, handle GPU reset */
2894be47400SFrançois Tigeot #define I915_WAIT_ALL		BIT(2) /* used by i915_gem_object_wait() */
29087df8fc6SFrançois Tigeot 
29171f41f3eSFrançois Tigeot static inline u32 intel_engine_get_seqno(struct intel_engine_cs *engine);
29287df8fc6SFrançois Tigeot 
29387df8fc6SFrançois Tigeot /**
29487df8fc6SFrançois Tigeot  * Returns true if seq1 is later than seq2.
29587df8fc6SFrançois Tigeot  */
29687df8fc6SFrançois Tigeot static inline bool i915_seqno_passed(u32 seq1, u32 seq2)
29787df8fc6SFrançois Tigeot {
29887df8fc6SFrançois Tigeot 	return (s32)(seq1 - seq2) >= 0;
29987df8fc6SFrançois Tigeot }
30087df8fc6SFrançois Tigeot 
30187df8fc6SFrançois Tigeot static inline bool
302*a85cb24fSFrançois Tigeot __i915_gem_request_started(const struct drm_i915_gem_request *req, u32 seqno)
30387df8fc6SFrançois Tigeot {
304*a85cb24fSFrançois Tigeot 	GEM_BUG_ON(!seqno);
30587df8fc6SFrançois Tigeot 	return i915_seqno_passed(intel_engine_get_seqno(req->engine),
306*a85cb24fSFrançois Tigeot 				 seqno - 1);
30787df8fc6SFrançois Tigeot }
30887df8fc6SFrançois Tigeot 
30987df8fc6SFrançois Tigeot static inline bool
3104be47400SFrançois Tigeot i915_gem_request_started(const struct drm_i915_gem_request *req)
3114be47400SFrançois Tigeot {
312*a85cb24fSFrançois Tigeot 	u32 seqno;
313*a85cb24fSFrançois Tigeot 
314*a85cb24fSFrançois Tigeot 	seqno = i915_gem_request_global_seqno(req);
315*a85cb24fSFrançois Tigeot 	if (!seqno)
3164be47400SFrançois Tigeot 		return false;
3174be47400SFrançois Tigeot 
318*a85cb24fSFrançois Tigeot 	return __i915_gem_request_started(req, seqno);
3194be47400SFrançois Tigeot }
3204be47400SFrançois Tigeot 
3214be47400SFrançois Tigeot static inline bool
322*a85cb24fSFrançois Tigeot __i915_gem_request_completed(const struct drm_i915_gem_request *req, u32 seqno)
3234be47400SFrançois Tigeot {
324*a85cb24fSFrançois Tigeot 	GEM_BUG_ON(!seqno);
325*a85cb24fSFrançois Tigeot 	return i915_seqno_passed(intel_engine_get_seqno(req->engine), seqno) &&
326*a85cb24fSFrançois Tigeot 		seqno == i915_gem_request_global_seqno(req);
3274be47400SFrançois Tigeot }
3284be47400SFrançois Tigeot 
3294be47400SFrançois Tigeot static inline bool
33087df8fc6SFrançois Tigeot i915_gem_request_completed(const struct drm_i915_gem_request *req)
33187df8fc6SFrançois Tigeot {
332*a85cb24fSFrançois Tigeot 	u32 seqno;
333*a85cb24fSFrançois Tigeot 
334*a85cb24fSFrançois Tigeot 	seqno = i915_gem_request_global_seqno(req);
335*a85cb24fSFrançois Tigeot 	if (!seqno)
3364be47400SFrançois Tigeot 		return false;
3374be47400SFrançois Tigeot 
338*a85cb24fSFrançois Tigeot 	return __i915_gem_request_completed(req, seqno);
33987df8fc6SFrançois Tigeot }
34087df8fc6SFrançois Tigeot 
34187df8fc6SFrançois Tigeot bool __i915_spin_request(const struct drm_i915_gem_request *request,
342*a85cb24fSFrançois Tigeot 			 u32 seqno, int state, unsigned long timeout_us);
34387df8fc6SFrançois Tigeot static inline bool i915_spin_request(const struct drm_i915_gem_request *request,
34487df8fc6SFrançois Tigeot 				     int state, unsigned long timeout_us)
34587df8fc6SFrançois Tigeot {
346*a85cb24fSFrançois Tigeot 	u32 seqno;
347*a85cb24fSFrançois Tigeot 
348*a85cb24fSFrançois Tigeot 	seqno = i915_gem_request_global_seqno(request);
349*a85cb24fSFrançois Tigeot 	if (!seqno)
350*a85cb24fSFrançois Tigeot 		return 0;
351*a85cb24fSFrançois Tigeot 
352*a85cb24fSFrançois Tigeot 	return (__i915_gem_request_started(request, seqno) &&
353*a85cb24fSFrançois Tigeot 		__i915_spin_request(request, seqno, state, timeout_us));
35487df8fc6SFrançois Tigeot }
35587df8fc6SFrançois Tigeot 
35671f41f3eSFrançois Tigeot /* We treat requests as fences. This is not be to confused with our
35771f41f3eSFrançois Tigeot  * "fence registers" but pipeline synchronisation objects ala GL_ARB_sync.
35871f41f3eSFrançois Tigeot  * We use the fences to synchronize access from the CPU with activity on the
35971f41f3eSFrançois Tigeot  * GPU, for example, we should not rewrite an object's PTE whilst the GPU
36071f41f3eSFrançois Tigeot  * is reading them. We also track fences at a higher level to provide
36171f41f3eSFrançois Tigeot  * implicit synchronisation around GEM objects, e.g. set-domain will wait
36271f41f3eSFrançois Tigeot  * for outstanding GPU rendering before marking the object ready for CPU
36371f41f3eSFrançois Tigeot  * access, or a pageflip will wait until the GPU is complete before showing
36471f41f3eSFrançois Tigeot  * the frame on the scanout.
36571f41f3eSFrançois Tigeot  *
36671f41f3eSFrançois Tigeot  * In order to use a fence, the object must track the fence it needs to
36771f41f3eSFrançois Tigeot  * serialise with. For example, GEM objects want to track both read and
36871f41f3eSFrançois Tigeot  * write access so that we can perform concurrent read operations between
36971f41f3eSFrançois Tigeot  * the CPU and GPU engines, as well as waiting for all rendering to
37071f41f3eSFrançois Tigeot  * complete, or waiting for the last GPU user of a "fence register". The
37171f41f3eSFrançois Tigeot  * object then embeds a #i915_gem_active to track the most recent (in
37271f41f3eSFrançois Tigeot  * retirement order) request relevant for the desired mode of access.
37371f41f3eSFrançois Tigeot  * The #i915_gem_active is updated with i915_gem_active_set() to track the
37471f41f3eSFrançois Tigeot  * most recent fence request, typically this is done as part of
37571f41f3eSFrançois Tigeot  * i915_vma_move_to_active().
37671f41f3eSFrançois Tigeot  *
37771f41f3eSFrançois Tigeot  * When the #i915_gem_active completes (is retired), it will
37871f41f3eSFrançois Tigeot  * signal its completion to the owner through a callback as well as mark
37971f41f3eSFrançois Tigeot  * itself as idle (i915_gem_active.request == NULL). The owner
38071f41f3eSFrançois Tigeot  * can then perform any action, such as delayed freeing of an active
38171f41f3eSFrançois Tigeot  * resource including itself.
38271f41f3eSFrançois Tigeot  */
38371f41f3eSFrançois Tigeot struct i915_gem_active;
38471f41f3eSFrançois Tigeot 
38571f41f3eSFrançois Tigeot typedef void (*i915_gem_retire_fn)(struct i915_gem_active *,
38671f41f3eSFrançois Tigeot 				   struct drm_i915_gem_request *);
38771f41f3eSFrançois Tigeot 
38871f41f3eSFrançois Tigeot struct i915_gem_active {
38971f41f3eSFrançois Tigeot 	struct drm_i915_gem_request __rcu *request;
39071f41f3eSFrançois Tigeot 	struct list_head link;
39171f41f3eSFrançois Tigeot 	i915_gem_retire_fn retire;
39271f41f3eSFrançois Tigeot };
39371f41f3eSFrançois Tigeot 
39471f41f3eSFrançois Tigeot void i915_gem_retire_noop(struct i915_gem_active *,
39571f41f3eSFrançois Tigeot 			  struct drm_i915_gem_request *request);
39671f41f3eSFrançois Tigeot 
39771f41f3eSFrançois Tigeot /**
39871f41f3eSFrançois Tigeot  * init_request_active - prepares the activity tracker for use
39971f41f3eSFrançois Tigeot  * @active - the active tracker
40071f41f3eSFrançois Tigeot  * @func - a callback when then the tracker is retired (becomes idle),
40171f41f3eSFrançois Tigeot  *         can be NULL
40271f41f3eSFrançois Tigeot  *
40371f41f3eSFrançois Tigeot  * init_request_active() prepares the embedded @active struct for use as
40471f41f3eSFrançois Tigeot  * an activity tracker, that is for tracking the last known active request
40571f41f3eSFrançois Tigeot  * associated with it. When the last request becomes idle, when it is retired
40671f41f3eSFrançois Tigeot  * after completion, the optional callback @func is invoked.
40771f41f3eSFrançois Tigeot  */
40871f41f3eSFrançois Tigeot static inline void
40971f41f3eSFrançois Tigeot init_request_active(struct i915_gem_active *active,
41071f41f3eSFrançois Tigeot 		    i915_gem_retire_fn retire)
41171f41f3eSFrançois Tigeot {
41271f41f3eSFrançois Tigeot 	INIT_LIST_HEAD(&active->link);
41371f41f3eSFrançois Tigeot 	active->retire = retire ?: i915_gem_retire_noop;
41471f41f3eSFrançois Tigeot }
41571f41f3eSFrançois Tigeot 
41671f41f3eSFrançois Tigeot /**
41771f41f3eSFrançois Tigeot  * i915_gem_active_set - updates the tracker to watch the current request
41871f41f3eSFrançois Tigeot  * @active - the active tracker
41971f41f3eSFrançois Tigeot  * @request - the request to watch
42071f41f3eSFrançois Tigeot  *
42171f41f3eSFrançois Tigeot  * i915_gem_active_set() watches the given @request for completion. Whilst
42271f41f3eSFrançois Tigeot  * that @request is busy, the @active reports busy. When that @request is
42371f41f3eSFrançois Tigeot  * retired, the @active tracker is updated to report idle.
42471f41f3eSFrançois Tigeot  */
42571f41f3eSFrançois Tigeot static inline void
42671f41f3eSFrançois Tigeot i915_gem_active_set(struct i915_gem_active *active,
42771f41f3eSFrançois Tigeot 		    struct drm_i915_gem_request *request)
42871f41f3eSFrançois Tigeot {
42971f41f3eSFrançois Tigeot 	list_move(&active->link, &request->active_list);
43071f41f3eSFrançois Tigeot 	rcu_assign_pointer(active->request, request);
43171f41f3eSFrançois Tigeot }
43271f41f3eSFrançois Tigeot 
4334be47400SFrançois Tigeot /**
4344be47400SFrançois Tigeot  * i915_gem_active_set_retire_fn - updates the retirement callback
4354be47400SFrançois Tigeot  * @active - the active tracker
4364be47400SFrançois Tigeot  * @fn - the routine called when the request is retired
4374be47400SFrançois Tigeot  * @mutex - struct_mutex used to guard retirements
4384be47400SFrançois Tigeot  *
4394be47400SFrançois Tigeot  * i915_gem_active_set_retire_fn() updates the function pointer that
4404be47400SFrançois Tigeot  * is called when the final request associated with the @active tracker
4414be47400SFrançois Tigeot  * is retired.
4424be47400SFrançois Tigeot  */
4434be47400SFrançois Tigeot static inline void
4444be47400SFrançois Tigeot i915_gem_active_set_retire_fn(struct i915_gem_active *active,
4454be47400SFrançois Tigeot 			      i915_gem_retire_fn fn,
4464be47400SFrançois Tigeot 			      struct lock *mutex)
4474be47400SFrançois Tigeot {
4484be47400SFrançois Tigeot 	lockdep_assert_held(mutex);
4494be47400SFrançois Tigeot 	active->retire = fn ?: i915_gem_retire_noop;
4504be47400SFrançois Tigeot }
4514be47400SFrançois Tigeot 
45271f41f3eSFrançois Tigeot static inline struct drm_i915_gem_request *
45371f41f3eSFrançois Tigeot __i915_gem_active_peek(const struct i915_gem_active *active)
45471f41f3eSFrançois Tigeot {
45571f41f3eSFrançois Tigeot 	/* Inside the error capture (running with the driver in an unknown
45671f41f3eSFrançois Tigeot 	 * state), we want to bend the rules slightly (a lot).
45771f41f3eSFrançois Tigeot 	 *
45871f41f3eSFrançois Tigeot 	 * Work is in progress to make it safer, in the meantime this keeps
45971f41f3eSFrançois Tigeot 	 * the known issue from spamming the logs.
46071f41f3eSFrançois Tigeot 	 */
46171f41f3eSFrançois Tigeot 	return rcu_dereference_protected(active->request, 1);
46271f41f3eSFrançois Tigeot }
46371f41f3eSFrançois Tigeot 
46471f41f3eSFrançois Tigeot /**
46571f41f3eSFrançois Tigeot  * i915_gem_active_raw - return the active request
46671f41f3eSFrançois Tigeot  * @active - the active tracker
46771f41f3eSFrançois Tigeot  *
46871f41f3eSFrançois Tigeot  * i915_gem_active_raw() returns the current request being tracked, or NULL.
46971f41f3eSFrançois Tigeot  * It does not obtain a reference on the request for the caller, so the caller
47071f41f3eSFrançois Tigeot  * must hold struct_mutex.
47171f41f3eSFrançois Tigeot  */
47271f41f3eSFrançois Tigeot static inline struct drm_i915_gem_request *
47371f41f3eSFrançois Tigeot i915_gem_active_raw(const struct i915_gem_active *active, struct lock *mutex)
47471f41f3eSFrançois Tigeot {
47571f41f3eSFrançois Tigeot 	return rcu_dereference_protected(active->request,
47671f41f3eSFrançois Tigeot 					 lockdep_is_held(mutex));
47771f41f3eSFrançois Tigeot }
47871f41f3eSFrançois Tigeot 
47971f41f3eSFrançois Tigeot /**
48071f41f3eSFrançois Tigeot  * i915_gem_active_peek - report the active request being monitored
48171f41f3eSFrançois Tigeot  * @active - the active tracker
48271f41f3eSFrançois Tigeot  *
48371f41f3eSFrançois Tigeot  * i915_gem_active_peek() returns the current request being tracked if
48471f41f3eSFrançois Tigeot  * still active, or NULL. It does not obtain a reference on the request
48571f41f3eSFrançois Tigeot  * for the caller, so the caller must hold struct_mutex.
48671f41f3eSFrançois Tigeot  */
48771f41f3eSFrançois Tigeot static inline struct drm_i915_gem_request *
48871f41f3eSFrançois Tigeot i915_gem_active_peek(const struct i915_gem_active *active, struct lock *mutex)
48971f41f3eSFrançois Tigeot {
49071f41f3eSFrançois Tigeot 	struct drm_i915_gem_request *request;
49171f41f3eSFrançois Tigeot 
49271f41f3eSFrançois Tigeot 	request = i915_gem_active_raw(active, mutex);
49371f41f3eSFrançois Tigeot 	if (!request || i915_gem_request_completed(request))
49471f41f3eSFrançois Tigeot 		return NULL;
49571f41f3eSFrançois Tigeot 
49671f41f3eSFrançois Tigeot 	return request;
49771f41f3eSFrançois Tigeot }
49871f41f3eSFrançois Tigeot 
49971f41f3eSFrançois Tigeot /**
50071f41f3eSFrançois Tigeot  * i915_gem_active_get - return a reference to the active request
50171f41f3eSFrançois Tigeot  * @active - the active tracker
50271f41f3eSFrançois Tigeot  *
50371f41f3eSFrançois Tigeot  * i915_gem_active_get() returns a reference to the active request, or NULL
50471f41f3eSFrançois Tigeot  * if the active tracker is idle. The caller must hold struct_mutex.
50571f41f3eSFrançois Tigeot  */
50671f41f3eSFrançois Tigeot static inline struct drm_i915_gem_request *
50771f41f3eSFrançois Tigeot i915_gem_active_get(const struct i915_gem_active *active, struct lock *mutex)
50871f41f3eSFrançois Tigeot {
50971f41f3eSFrançois Tigeot 	return i915_gem_request_get(i915_gem_active_peek(active, mutex));
51071f41f3eSFrançois Tigeot }
51171f41f3eSFrançois Tigeot 
51271f41f3eSFrançois Tigeot /**
51371f41f3eSFrançois Tigeot  * __i915_gem_active_get_rcu - return a reference to the active request
51471f41f3eSFrançois Tigeot  * @active - the active tracker
51571f41f3eSFrançois Tigeot  *
51671f41f3eSFrançois Tigeot  * __i915_gem_active_get() returns a reference to the active request, or NULL
51771f41f3eSFrançois Tigeot  * if the active tracker is idle. The caller must hold the RCU read lock, but
51871f41f3eSFrançois Tigeot  * the returned pointer is safe to use outside of RCU.
51971f41f3eSFrançois Tigeot  */
52071f41f3eSFrançois Tigeot static inline struct drm_i915_gem_request *
52171f41f3eSFrançois Tigeot __i915_gem_active_get_rcu(const struct i915_gem_active *active)
52271f41f3eSFrançois Tigeot {
52371f41f3eSFrançois Tigeot 	/* Performing a lockless retrieval of the active request is super
524*a85cb24fSFrançois Tigeot 	 * tricky. SLAB_TYPESAFE_BY_RCU merely guarantees that the backing
52571f41f3eSFrançois Tigeot 	 * slab of request objects will not be freed whilst we hold the
52671f41f3eSFrançois Tigeot 	 * RCU read lock. It does not guarantee that the request itself
52771f41f3eSFrançois Tigeot 	 * will not be freed and then *reused*. Viz,
52871f41f3eSFrançois Tigeot 	 *
52971f41f3eSFrançois Tigeot 	 * Thread A			Thread B
53071f41f3eSFrançois Tigeot 	 *
53171f41f3eSFrançois Tigeot 	 * req = active.request
53271f41f3eSFrançois Tigeot 	 *				retire(req) -> free(req);
53371f41f3eSFrançois Tigeot 	 *				(req is now first on the slab freelist)
53471f41f3eSFrançois Tigeot 	 *				active.request = NULL
53571f41f3eSFrançois Tigeot 	 *
53671f41f3eSFrançois Tigeot 	 *				req = new submission on a new object
53771f41f3eSFrançois Tigeot 	 * ref(req)
53871f41f3eSFrançois Tigeot 	 *
53971f41f3eSFrançois Tigeot 	 * To prevent the request from being reused whilst the caller
54071f41f3eSFrançois Tigeot 	 * uses it, we take a reference like normal. Whilst acquiring
54171f41f3eSFrançois Tigeot 	 * the reference we check that it is not in a destroyed state
54271f41f3eSFrançois Tigeot 	 * (refcnt == 0). That prevents the request being reallocated
54371f41f3eSFrançois Tigeot 	 * whilst the caller holds on to it. To check that the request
54471f41f3eSFrançois Tigeot 	 * was not reallocated as we acquired the reference we have to
54571f41f3eSFrançois Tigeot 	 * check that our request remains the active request across
54671f41f3eSFrançois Tigeot 	 * the lookup, in the same manner as a seqlock. The visibility
54771f41f3eSFrançois Tigeot 	 * of the pointer versus the reference counting is controlled
54871f41f3eSFrançois Tigeot 	 * by using RCU barriers (rcu_dereference and rcu_assign_pointer).
54971f41f3eSFrançois Tigeot 	 *
55071f41f3eSFrançois Tigeot 	 * In the middle of all that, we inspect whether the request is
55171f41f3eSFrançois Tigeot 	 * complete. Retiring is lazy so the request may be completed long
55271f41f3eSFrançois Tigeot 	 * before the active tracker is updated. Querying whether the
55371f41f3eSFrançois Tigeot 	 * request is complete is far cheaper (as it involves no locked
55471f41f3eSFrançois Tigeot 	 * instructions setting cachelines to exclusive) than acquiring
55571f41f3eSFrançois Tigeot 	 * the reference, so we do it first. The RCU read lock ensures the
55671f41f3eSFrançois Tigeot 	 * pointer dereference is valid, but does not ensure that the
55771f41f3eSFrançois Tigeot 	 * seqno nor HWS is the right one! However, if the request was
55871f41f3eSFrançois Tigeot 	 * reallocated, that means the active tracker's request was complete.
55971f41f3eSFrançois Tigeot 	 * If the new request is also complete, then both are and we can
56071f41f3eSFrançois Tigeot 	 * just report the active tracker is idle. If the new request is
56171f41f3eSFrançois Tigeot 	 * incomplete, then we acquire a reference on it and check that
56271f41f3eSFrançois Tigeot 	 * it remained the active request.
56371f41f3eSFrançois Tigeot 	 *
56471f41f3eSFrançois Tigeot 	 * It is then imperative that we do not zero the request on
56571f41f3eSFrançois Tigeot 	 * reallocation, so that we can chase the dangling pointers!
56671f41f3eSFrançois Tigeot 	 * See i915_gem_request_alloc().
56771f41f3eSFrançois Tigeot 	 */
56871f41f3eSFrançois Tigeot 	do {
56971f41f3eSFrançois Tigeot 		struct drm_i915_gem_request *request;
57071f41f3eSFrançois Tigeot 
57171f41f3eSFrançois Tigeot 		request = rcu_dereference(active->request);
57271f41f3eSFrançois Tigeot 		if (!request || i915_gem_request_completed(request))
57371f41f3eSFrançois Tigeot 			return NULL;
57471f41f3eSFrançois Tigeot 
5751e12ee3bSFrançois Tigeot 		/* An especially silly compiler could decide to recompute the
5761e12ee3bSFrançois Tigeot 		 * result of i915_gem_request_completed, more specifically
5771e12ee3bSFrançois Tigeot 		 * re-emit the load for request->fence.seqno. A race would catch
5781e12ee3bSFrançois Tigeot 		 * a later seqno value, which could flip the result from true to
5791e12ee3bSFrançois Tigeot 		 * false. Which means part of the instructions below might not
5801e12ee3bSFrançois Tigeot 		 * be executed, while later on instructions are executed. Due to
5811e12ee3bSFrançois Tigeot 		 * barriers within the refcounting the inconsistency can't reach
5821e12ee3bSFrançois Tigeot 		 * past the call to i915_gem_request_get_rcu, but not executing
5831e12ee3bSFrançois Tigeot 		 * that while still executing i915_gem_request_put() creates
5841e12ee3bSFrançois Tigeot 		 * havoc enough.  Prevent this with a compiler barrier.
5851e12ee3bSFrançois Tigeot 		 */
5861e12ee3bSFrançois Tigeot 		barrier();
5871e12ee3bSFrançois Tigeot 
58871f41f3eSFrançois Tigeot 		request = i915_gem_request_get_rcu(request);
58971f41f3eSFrançois Tigeot 
59071f41f3eSFrançois Tigeot 		/* What stops the following rcu_access_pointer() from occurring
59171f41f3eSFrançois Tigeot 		 * before the above i915_gem_request_get_rcu()? If we were
59271f41f3eSFrançois Tigeot 		 * to read the value before pausing to get the reference to
59371f41f3eSFrançois Tigeot 		 * the request, we may not notice a change in the active
59471f41f3eSFrançois Tigeot 		 * tracker.
59571f41f3eSFrançois Tigeot 		 *
59671f41f3eSFrançois Tigeot 		 * The rcu_access_pointer() is a mere compiler barrier, which
59771f41f3eSFrançois Tigeot 		 * means both the CPU and compiler are free to perform the
59871f41f3eSFrançois Tigeot 		 * memory read without constraint. The compiler only has to
59971f41f3eSFrançois Tigeot 		 * ensure that any operations after the rcu_access_pointer()
60071f41f3eSFrançois Tigeot 		 * occur afterwards in program order. This means the read may
60171f41f3eSFrançois Tigeot 		 * be performed earlier by an out-of-order CPU, or adventurous
60271f41f3eSFrançois Tigeot 		 * compiler.
60371f41f3eSFrançois Tigeot 		 *
60471f41f3eSFrançois Tigeot 		 * The atomic operation at the heart of
6056559babbSFrançois Tigeot 		 * i915_gem_request_get_rcu(), see dma_fence_get_rcu(), is
60671f41f3eSFrançois Tigeot 		 * atomic_inc_not_zero() which is only a full memory barrier
60771f41f3eSFrançois Tigeot 		 * when successful. That is, if i915_gem_request_get_rcu()
60871f41f3eSFrançois Tigeot 		 * returns the request (and so with the reference counted
60971f41f3eSFrançois Tigeot 		 * incremented) then the following read for rcu_access_pointer()
61071f41f3eSFrançois Tigeot 		 * must occur after the atomic operation and so confirm
61171f41f3eSFrançois Tigeot 		 * that this request is the one currently being tracked.
61271f41f3eSFrançois Tigeot 		 *
61371f41f3eSFrançois Tigeot 		 * The corresponding write barrier is part of
61471f41f3eSFrançois Tigeot 		 * rcu_assign_pointer().
61571f41f3eSFrançois Tigeot 		 */
61671f41f3eSFrançois Tigeot 		if (!request || request == rcu_access_pointer(active->request))
61771f41f3eSFrançois Tigeot 			return rcu_pointer_handoff(request);
61871f41f3eSFrançois Tigeot 
61971f41f3eSFrançois Tigeot 		i915_gem_request_put(request);
62071f41f3eSFrançois Tigeot 	} while (1);
62171f41f3eSFrançois Tigeot }
62271f41f3eSFrançois Tigeot 
62371f41f3eSFrançois Tigeot /**
62471f41f3eSFrançois Tigeot  * i915_gem_active_get_unlocked - return a reference to the active request
62571f41f3eSFrançois Tigeot  * @active - the active tracker
62671f41f3eSFrançois Tigeot  *
62771f41f3eSFrançois Tigeot  * i915_gem_active_get_unlocked() returns a reference to the active request,
62871f41f3eSFrançois Tigeot  * or NULL if the active tracker is idle. The reference is obtained under RCU,
62971f41f3eSFrançois Tigeot  * so no locking is required by the caller.
63071f41f3eSFrançois Tigeot  *
63171f41f3eSFrançois Tigeot  * The reference should be freed with i915_gem_request_put().
63271f41f3eSFrançois Tigeot  */
63371f41f3eSFrançois Tigeot static inline struct drm_i915_gem_request *
63471f41f3eSFrançois Tigeot i915_gem_active_get_unlocked(const struct i915_gem_active *active)
63571f41f3eSFrançois Tigeot {
63671f41f3eSFrançois Tigeot 	struct drm_i915_gem_request *request;
63771f41f3eSFrançois Tigeot 
63871f41f3eSFrançois Tigeot 	rcu_read_lock();
63971f41f3eSFrançois Tigeot 	request = __i915_gem_active_get_rcu(active);
64071f41f3eSFrançois Tigeot 	rcu_read_unlock();
64171f41f3eSFrançois Tigeot 
64271f41f3eSFrançois Tigeot 	return request;
64371f41f3eSFrançois Tigeot }
64471f41f3eSFrançois Tigeot 
64571f41f3eSFrançois Tigeot /**
64671f41f3eSFrançois Tigeot  * i915_gem_active_isset - report whether the active tracker is assigned
64771f41f3eSFrançois Tigeot  * @active - the active tracker
64871f41f3eSFrançois Tigeot  *
64971f41f3eSFrançois Tigeot  * i915_gem_active_isset() returns true if the active tracker is currently
65071f41f3eSFrançois Tigeot  * assigned to a request. Due to the lazy retiring, that request may be idle
65171f41f3eSFrançois Tigeot  * and this may report stale information.
65271f41f3eSFrançois Tigeot  */
65371f41f3eSFrançois Tigeot static inline bool
65471f41f3eSFrançois Tigeot i915_gem_active_isset(const struct i915_gem_active *active)
65571f41f3eSFrançois Tigeot {
65671f41f3eSFrançois Tigeot 	return rcu_access_pointer(active->request);
65771f41f3eSFrançois Tigeot }
65871f41f3eSFrançois Tigeot 
65971f41f3eSFrançois Tigeot /**
66071f41f3eSFrançois Tigeot  * i915_gem_active_wait - waits until the request is completed
66171f41f3eSFrançois Tigeot  * @active - the active request on which to wait
6621e12ee3bSFrançois Tigeot  * @flags - how to wait
66371f41f3eSFrançois Tigeot  * @timeout - how long to wait at most
66471f41f3eSFrançois Tigeot  * @rps - userspace client to charge for a waitboost
66571f41f3eSFrançois Tigeot  *
6664be47400SFrançois Tigeot  * i915_gem_active_wait() waits until the request is completed before
66771f41f3eSFrançois Tigeot  * returning, without requiring any locks to be held. Note that it does not
66871f41f3eSFrançois Tigeot  * retire any requests before returning.
66971f41f3eSFrançois Tigeot  *
67071f41f3eSFrançois Tigeot  * This function relies on RCU in order to acquire the reference to the active
67171f41f3eSFrançois Tigeot  * request without holding any locks. See __i915_gem_active_get_rcu() for the
67271f41f3eSFrançois Tigeot  * glory details on how that is managed. Once the reference is acquired, we
67371f41f3eSFrançois Tigeot  * can then wait upon the request, and afterwards release our reference,
67471f41f3eSFrançois Tigeot  * free of any locking.
67571f41f3eSFrançois Tigeot  *
67671f41f3eSFrançois Tigeot  * This function wraps i915_wait_request(), see it for the full details on
67771f41f3eSFrançois Tigeot  * the arguments.
67871f41f3eSFrançois Tigeot  *
67971f41f3eSFrançois Tigeot  * Returns 0 if successful, or a negative error code.
68071f41f3eSFrançois Tigeot  */
68171f41f3eSFrançois Tigeot static inline int
6824be47400SFrançois Tigeot i915_gem_active_wait(const struct i915_gem_active *active, unsigned int flags)
68371f41f3eSFrançois Tigeot {
68471f41f3eSFrançois Tigeot 	struct drm_i915_gem_request *request;
6854be47400SFrançois Tigeot 	long ret = 0;
68671f41f3eSFrançois Tigeot 
68771f41f3eSFrançois Tigeot 	request = i915_gem_active_get_unlocked(active);
68871f41f3eSFrançois Tigeot 	if (request) {
6894be47400SFrançois Tigeot 		ret = i915_wait_request(request, flags, MAX_SCHEDULE_TIMEOUT);
69071f41f3eSFrançois Tigeot 		i915_gem_request_put(request);
69171f41f3eSFrançois Tigeot 	}
69271f41f3eSFrançois Tigeot 
6934be47400SFrançois Tigeot 	return ret < 0 ? ret : 0;
69471f41f3eSFrançois Tigeot }
69571f41f3eSFrançois Tigeot 
69671f41f3eSFrançois Tigeot /**
69771f41f3eSFrançois Tigeot  * i915_gem_active_retire - waits until the request is retired
69871f41f3eSFrançois Tigeot  * @active - the active request on which to wait
69971f41f3eSFrançois Tigeot  *
70071f41f3eSFrançois Tigeot  * i915_gem_active_retire() waits until the request is completed,
70171f41f3eSFrançois Tigeot  * and then ensures that at least the retirement handler for this
70271f41f3eSFrançois Tigeot  * @active tracker is called before returning. If the @active
70371f41f3eSFrançois Tigeot  * tracker is idle, the function returns immediately.
70471f41f3eSFrançois Tigeot  */
70571f41f3eSFrançois Tigeot static inline int __must_check
70671f41f3eSFrançois Tigeot i915_gem_active_retire(struct i915_gem_active *active,
70771f41f3eSFrançois Tigeot 		       struct lock *mutex)
70871f41f3eSFrançois Tigeot {
70971f41f3eSFrançois Tigeot 	struct drm_i915_gem_request *request;
7104be47400SFrançois Tigeot 	long ret;
71171f41f3eSFrançois Tigeot 
71271f41f3eSFrançois Tigeot 	request = i915_gem_active_raw(active, mutex);
71371f41f3eSFrançois Tigeot 	if (!request)
71471f41f3eSFrançois Tigeot 		return 0;
71571f41f3eSFrançois Tigeot 
7161e12ee3bSFrançois Tigeot 	ret = i915_wait_request(request,
7171e12ee3bSFrançois Tigeot 				I915_WAIT_INTERRUPTIBLE | I915_WAIT_LOCKED,
7184be47400SFrançois Tigeot 				MAX_SCHEDULE_TIMEOUT);
7194be47400SFrançois Tigeot 	if (ret < 0)
72071f41f3eSFrançois Tigeot 		return ret;
72171f41f3eSFrançois Tigeot 
72271f41f3eSFrançois Tigeot 	list_del_init(&active->link);
72371f41f3eSFrançois Tigeot 	RCU_INIT_POINTER(active->request, NULL);
72471f41f3eSFrançois Tigeot 
72571f41f3eSFrançois Tigeot 	active->retire(active, request);
72671f41f3eSFrançois Tigeot 
72771f41f3eSFrançois Tigeot 	return 0;
72871f41f3eSFrançois Tigeot }
72971f41f3eSFrançois Tigeot 
73071f41f3eSFrançois Tigeot #define for_each_active(mask, idx) \
73171f41f3eSFrançois Tigeot 	for (; mask ? idx = ffs(mask) - 1, 1 : 0; mask &= ~BIT(idx))
73271f41f3eSFrançois Tigeot 
73387df8fc6SFrançois Tigeot #endif /* I915_GEM_REQUEST_H */
734