Home | History | Annotate | Line # | Download | only in i915
i915_request.h revision 1.3
      1 /*	$NetBSD: i915_request.h,v 1.3 2021/12/19 11:20:02 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright  2008-2018 Intel Corporation
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a
      7  * copy of this software and associated documentation files (the "Software"),
      8  * to deal in the Software without restriction, including without limitation
      9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  * and/or sell copies of the Software, and to permit persons to whom the
     11  * Software is furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice (including the next
     14  * paragraph) shall be included in all copies or substantial portions of the
     15  * Software.
     16  *
     17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     23  * IN THE SOFTWARE.
     24  *
     25  */
     26 
     27 #ifndef I915_REQUEST_H
     28 #define I915_REQUEST_H
     29 
     30 #include <linux/dma-fence.h>
     31 #include <linux/lockdep.h>
     32 
     33 #include "gem/i915_gem_context_types.h"
     34 #include "gt/intel_context_types.h"
     35 #include "gt/intel_engine_types.h"
     36 #include "gt/intel_timeline_types.h"
     37 
     38 #include "i915_gem.h"
     39 #include "i915_scheduler.h"
     40 #include "i915_selftest.h"
     41 #include "i915_sw_fence.h"
     42 
     43 #include <uapi/drm/i915_drm.h>
     44 
     45 struct drm_file;
     46 struct drm_i915_gem_object;
     47 struct i915_request;
     48 
     49 struct i915_capture_list {
     50 	struct i915_capture_list *next;
     51 	struct i915_vma *vma;
     52 };
     53 
     54 #define RQ_TRACE(rq, fmt, ...) do {					\
     55 	const struct i915_request *rq__ = (rq);				\
     56 	ENGINE_TRACE(rq__->engine, "fence %llx:%lld, current %d " fmt,	\
     57 		     rq__->fence.context, rq__->fence.seqno,		\
     58 		     hwsp_seqno(rq__), ##__VA_ARGS__);			\
     59 } while (0)
     60 
     61 enum {
     62 	/*
     63 	 * I915_FENCE_FLAG_ACTIVE - this request is currently submitted to HW.
     64 	 *
     65 	 * Set by __i915_request_submit() on handing over to HW, and cleared
     66 	 * by __i915_request_unsubmit() if we preempt this request.
     67 	 *
     68 	 * Finally cleared for consistency on retiring the request, when
     69 	 * we know the HW is no longer running this request.
     70 	 *
     71 	 * See i915_request_is_active()
     72 	 */
     73 	I915_FENCE_FLAG_ACTIVE = DMA_FENCE_FLAG_USER_BITS,
     74 
     75 	/*
     76 	 * I915_FENCE_FLAG_PQUEUE - this request is ready for execution
     77 	 *
     78 	 * Using the scheduler, when a request is ready for execution it is put
     79 	 * into the priority queue, and removed from that queue when transferred
     80 	 * to the HW runlists. We want to track its membership within the
     81 	 * priority queue so that we can easily check before rescheduling.
     82 	 *
     83 	 * See i915_request_in_priority_queue()
     84 	 */
     85 	I915_FENCE_FLAG_PQUEUE,
     86 
     87 	/*
     88 	 * I915_FENCE_FLAG_SIGNAL - this request is currently on signal_list
     89 	 *
     90 	 * Internal bookkeeping used by the breadcrumb code to track when
     91 	 * a request is on the various signal_list.
     92 	 */
     93 	I915_FENCE_FLAG_SIGNAL,
     94 
     95 	/*
     96 	 * I915_FENCE_FLAG_HOLD - this request is currently on hold
     97 	 *
     98 	 * This request has been suspended, pending an ongoing investigation.
     99 	 */
    100 	I915_FENCE_FLAG_HOLD,
    101 
    102 	/*
    103 	 * I915_FENCE_FLAG_NOPREEMPT - this request should not be preempted
    104 	 *
    105 	 * The execution of some requests should not be interrupted. This is
    106 	 * a sensitive operation as it makes the request super important,
    107 	 * blocking other higher priority work. Abuse of this flag will
    108 	 * lead to quality of service issues.
    109 	 */
    110 	I915_FENCE_FLAG_NOPREEMPT,
    111 
    112 	/*
    113 	 * I915_FENCE_FLAG_SENTINEL - this request should be last in the queue
    114 	 *
    115 	 * A high priority sentinel request may be submitted to clear the
    116 	 * submission queue. As it will be the only request in-flight, upon
    117 	 * execution all other active requests will have been preempted and
    118 	 * unsubmitted. This preemptive pulse is used to re-evaluate the
    119 	 * in-flight requests, particularly in cases where an active context
    120 	 * is banned and those active requests need to be cancelled.
    121 	 */
    122 	I915_FENCE_FLAG_SENTINEL,
    123 
    124 	/*
    125 	 * I915_FENCE_FLAG_BOOST - upclock the gpu for this request
    126 	 *
    127 	 * Some requests are more important than others! In particular, a
    128 	 * request that the user is waiting on is typically required for
    129 	 * interactive latency, for which we want to minimise by upclocking
    130 	 * the GPU. Here we track such boost requests on a per-request basis.
    131 	 */
    132 	I915_FENCE_FLAG_BOOST,
    133 };
    134 
    135 /**
    136  * Request queue structure.
    137  *
    138  * The request queue allows us to note sequence numbers that have been emitted
    139  * and may be associated with active buffers to be retired.
    140  *
    141  * By keeping this list, we can avoid having to do questionable sequence
    142  * number comparisons on buffer last_read|write_seqno. It also allows an
    143  * emission time to be associated with the request for tracking how far ahead
    144  * of the GPU the submission is.
    145  *
    146  * When modifying this structure be very aware that we perform a lockless
    147  * RCU lookup of it that may race against reallocation of the struct
    148  * from the slab freelist. We intentionally do not zero the structure on
    149  * allocation so that the lookup can use the dangling pointers (and is
    150  * cogniscent that those pointers may be wrong). Instead, everything that
    151  * needs to be initialised must be done so explicitly.
    152  *
    153  * The requests are reference counted.
    154  */
    155 struct i915_request {
    156 	struct dma_fence fence;
    157 	spinlock_t lock;
    158 
    159 	/** On Which ring this request was generated */
    160 	struct drm_i915_private *i915;
    161 
    162 	/**
    163 	 * Context and ring buffer related to this request
    164 	 * Contexts are refcounted, so when this request is associated with a
    165 	 * context, we must increment the context's refcount, to guarantee that
    166 	 * it persists while any request is linked to it. Requests themselves
    167 	 * are also refcounted, so the request will only be freed when the last
    168 	 * reference to it is dismissed, and the code in
    169 	 * i915_request_free() will then decrement the refcount on the
    170 	 * context.
    171 	 */
    172 	struct intel_engine_cs *engine;
    173 	struct intel_context *context;
    174 	struct intel_ring *ring;
    175 	struct intel_timeline __rcu *timeline;
    176 	struct list_head signal_link;
    177 
    178 	/*
    179 	 * The rcu epoch of when this request was allocated. Used to judiciously
    180 	 * apply backpressure on future allocations to ensure that under
    181 	 * mempressure there is sufficient RCU ticks for us to reclaim our
    182 	 * RCU protected slabs.
    183 	 */
    184 	unsigned long rcustate;
    185 
    186 	/*
    187 	 * We pin the timeline->mutex while constructing the request to
    188 	 * ensure that no caller accidentally drops it during construction.
    189 	 * The timeline->mutex must be held to ensure that only this caller
    190 	 * can use the ring and manipulate the associated timeline during
    191 	 * construction.
    192 	 */
    193 	struct pin_cookie cookie;
    194 
    195 	/*
    196 	 * Fences for the various phases in the request's lifetime.
    197 	 *
    198 	 * The submit fence is used to await upon all of the request's
    199 	 * dependencies. When it is signaled, the request is ready to run.
    200 	 * It is used by the driver to then queue the request for execution.
    201 	 */
    202 	struct i915_sw_fence submit;
    203 	union {
    204 #ifndef __NetBSD__		/* XXX */
    205 		wait_queue_entry_t submitq;
    206 #endif
    207 		struct i915_sw_dma_fence_cb dmaq;
    208 		struct i915_request_duration_cb {
    209 			struct dma_fence_cb cb;
    210 			ktime_t emitted;
    211 		} duration;
    212 	};
    213 	struct list_head execute_cb;
    214 	struct i915_sw_fence semaphore;
    215 
    216 	/*
    217 	 * A list of everyone we wait upon, and everyone who waits upon us.
    218 	 * Even though we will not be submitted to the hardware before the
    219 	 * submit fence is signaled (it waits for all external events as well
    220 	 * as our own requests), the scheduler still needs to know the
    221 	 * dependency tree for the lifetime of the request (from execbuf
    222 	 * to retirement), i.e. bidirectional dependency information for the
    223 	 * request not tied to individual fences.
    224 	 */
    225 	struct i915_sched_node sched;
    226 	struct i915_dependency dep;
    227 	intel_engine_mask_t execution_mask;
    228 
    229 	/*
    230 	 * A convenience pointer to the current breadcrumb value stored in
    231 	 * the HW status page (or our timeline's local equivalent). The full
    232 	 * path would be rq->hw_context->ring->timeline->hwsp_seqno.
    233 	 */
    234 	const u32 *hwsp_seqno;
    235 
    236 	/*
    237 	 * If we need to access the timeline's seqno for this request in
    238 	 * another request, we need to keep a read reference to this associated
    239 	 * cacheline, so that we do not free and recycle it before the foreign
    240 	 * observers have completed. Hence, we keep a pointer to the cacheline
    241 	 * inside the timeline's HWSP vma, but it is only valid while this
    242 	 * request has not completed and guarded by the timeline mutex.
    243 	 */
    244 	struct intel_timeline_cacheline __rcu *hwsp_cacheline;
    245 
    246 	/** Position in the ring of the start of the request */
    247 	u32 head;
    248 
    249 	/** Position in the ring of the start of the user packets */
    250 	u32 infix;
    251 
    252 	/**
    253 	 * Position in the ring of the start of the postfix.
    254 	 * This is required to calculate the maximum available ring space
    255 	 * without overwriting the postfix.
    256 	 */
    257 	u32 postfix;
    258 
    259 	/** Position in the ring of the end of the whole request */
    260 	u32 tail;
    261 
    262 	/** Position in the ring of the end of any workarounds after the tail */
    263 	u32 wa_tail;
    264 
    265 	/** Preallocate space in the ring for the emitting the request */
    266 	u32 reserved_space;
    267 
    268 	/** Batch buffer related to this request if any (used for
    269 	 * error state dump only).
    270 	 */
    271 	struct i915_vma *batch;
    272 	/**
    273 	 * Additional buffers requested by userspace to be captured upon
    274 	 * a GPU hang. The vma/obj on this list are protected by their
    275 	 * active reference - all objects on this list must also be
    276 	 * on the active_list (of their final request).
    277 	 */
    278 	struct i915_capture_list *capture_list;
    279 
    280 	/** Time at which this request was emitted, in jiffies. */
    281 	unsigned long emitted_jiffies;
    282 
    283 	/** timeline->request entry for this request */
    284 	struct list_head link;
    285 
    286 	struct drm_i915_file_private *file_priv;
    287 	/** file_priv list entry for this request */
    288 	struct list_head client_link;
    289 
    290 	I915_SELFTEST_DECLARE(struct {
    291 		struct list_head link;
    292 		unsigned long delay;
    293 	} mock;)
    294 };
    295 
    296 #define I915_FENCE_GFP (GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN)
    297 
    298 extern const struct dma_fence_ops i915_fence_ops;
    299 
    300 static inline bool dma_fence_is_i915(const struct dma_fence *fence)
    301 {
    302 	return fence->ops == &i915_fence_ops;
    303 }
    304 
    305 struct i915_request * __must_check
    306 __i915_request_create(struct intel_context *ce, gfp_t gfp);
    307 struct i915_request * __must_check
    308 i915_request_create(struct intel_context *ce);
    309 
    310 struct i915_request *__i915_request_commit(struct i915_request *request);
    311 void __i915_request_queue(struct i915_request *rq,
    312 			  const struct i915_sched_attr *attr);
    313 
    314 bool i915_request_retire(struct i915_request *rq);
    315 void i915_request_retire_upto(struct i915_request *rq);
    316 
    317 static inline struct i915_request *
    318 to_request(struct dma_fence *fence)
    319 {
    320 	/* We assume that NULL fence/request are interoperable */
    321 	BUILD_BUG_ON(offsetof(struct i915_request, fence) != 0);
    322 	GEM_BUG_ON(fence && !dma_fence_is_i915(fence));
    323 	return container_of(fence, struct i915_request, fence);
    324 }
    325 
    326 static inline struct i915_request *
    327 i915_request_get(struct i915_request *rq)
    328 {
    329 	return to_request(dma_fence_get(&rq->fence));
    330 }
    331 
    332 static inline struct i915_request *
    333 i915_request_get_rcu(struct i915_request *rq)
    334 {
    335 	return to_request(dma_fence_get_rcu(&rq->fence));
    336 }
    337 
    338 static inline void
    339 i915_request_put(struct i915_request *rq)
    340 {
    341 	dma_fence_put(&rq->fence);
    342 }
    343 
    344 int i915_request_await_object(struct i915_request *to,
    345 			      struct drm_i915_gem_object *obj,
    346 			      bool write);
    347 int i915_request_await_dma_fence(struct i915_request *rq,
    348 				 struct dma_fence *fence);
    349 int i915_request_await_execution(struct i915_request *rq,
    350 				 struct dma_fence *fence,
    351 				 void (*hook)(struct i915_request *rq,
    352 					      struct dma_fence *signal));
    353 
    354 void i915_request_add(struct i915_request *rq);
    355 
    356 bool __i915_request_submit(struct i915_request *request);
    357 void i915_request_submit(struct i915_request *request);
    358 
    359 void i915_request_skip(struct i915_request *request, int error);
    360 
    361 void __i915_request_unsubmit(struct i915_request *request);
    362 void i915_request_unsubmit(struct i915_request *request);
    363 
    364 /* Note: part of the intel_breadcrumbs family */
    365 bool i915_request_enable_breadcrumb(struct i915_request *request);
    366 void i915_request_cancel_breadcrumb(struct i915_request *request);
    367 
    368 long i915_request_wait(struct i915_request *rq,
    369 		       unsigned int flags,
    370 		       long timeout)
    371 	__attribute__((nonnull(1)));
    372 #define I915_WAIT_INTERRUPTIBLE	BIT(0)
    373 #define I915_WAIT_PRIORITY	BIT(1) /* small priority bump for the request */
    374 #define I915_WAIT_ALL		BIT(2) /* used by i915_gem_object_wait() */
    375 
    376 static inline bool i915_request_signaled(const struct i915_request *rq)
    377 {
    378 	/* The request may live longer than its HWSP, so check flags first! */
    379 	return test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &rq->fence.flags);
    380 }
    381 
    382 static inline bool i915_request_is_active(const struct i915_request *rq)
    383 {
    384 	return test_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags);
    385 }
    386 
    387 static inline bool i915_request_in_priority_queue(const struct i915_request *rq)
    388 {
    389 	return test_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
    390 }
    391 
    392 /**
    393  * Returns true if seq1 is later than seq2.
    394  */
    395 static inline bool i915_seqno_passed(u32 seq1, u32 seq2)
    396 {
    397 	return (s32)(seq1 - seq2) >= 0;
    398 }
    399 
    400 static inline u32 __hwsp_seqno(const struct i915_request *rq)
    401 {
    402 	return READ_ONCE(*rq->hwsp_seqno);
    403 }
    404 
    405 /**
    406  * hwsp_seqno - the current breadcrumb value in the HW status page
    407  * @rq: the request, to chase the relevant HW status page
    408  *
    409  * The emphasis in naming here is that hwsp_seqno() is not a property of the
    410  * request, but an indication of the current HW state (associated with this
    411  * request). Its value will change as the GPU executes more requests.
    412  *
    413  * Returns the current breadcrumb value in the associated HW status page (or
    414  * the local timeline's equivalent) for this request. The request itself
    415  * has the associated breadcrumb value of rq->fence.seqno, when the HW
    416  * status page has that breadcrumb or later, this request is complete.
    417  */
    418 static inline u32 hwsp_seqno(const struct i915_request *rq)
    419 {
    420 	u32 seqno;
    421 
    422 	rcu_read_lock(); /* the HWSP may be freed at runtime */
    423 	seqno = __hwsp_seqno(rq);
    424 	rcu_read_unlock();
    425 
    426 	return seqno;
    427 }
    428 
    429 static inline bool __i915_request_has_started(const struct i915_request *rq)
    430 {
    431 	return i915_seqno_passed(hwsp_seqno(rq), rq->fence.seqno - 1);
    432 }
    433 
    434 /**
    435  * i915_request_started - check if the request has begun being executed
    436  * @rq: the request
    437  *
    438  * If the timeline is not using initial breadcrumbs, a request is
    439  * considered started if the previous request on its timeline (i.e.
    440  * context) has been signaled.
    441  *
    442  * If the timeline is using semaphores, it will also be emitting an
    443  * "initial breadcrumb" after the semaphores are complete and just before
    444  * it began executing the user payload. A request can therefore be active
    445  * on the HW and not yet started as it is still busywaiting on its
    446  * dependencies (via HW semaphores).
    447  *
    448  * If the request has started, its dependencies will have been signaled
    449  * (either by fences or by semaphores) and it will have begun processing
    450  * the user payload.
    451  *
    452  * However, even if a request has started, it may have been preempted and
    453  * so no longer active, or it may have already completed.
    454  *
    455  * See also i915_request_is_active().
    456  *
    457  * Returns true if the request has begun executing the user payload, or
    458  * has completed:
    459  */
    460 static inline bool i915_request_started(const struct i915_request *rq)
    461 {
    462 	if (i915_request_signaled(rq))
    463 		return true;
    464 
    465 	/* Remember: started but may have since been preempted! */
    466 	return __i915_request_has_started(rq);
    467 }
    468 
    469 /**
    470  * i915_request_is_running - check if the request may actually be executing
    471  * @rq: the request
    472  *
    473  * Returns true if the request is currently submitted to hardware, has passed
    474  * its start point (i.e. the context is setup and not busywaiting). Note that
    475  * it may no longer be running by the time the function returns!
    476  */
    477 static inline bool i915_request_is_running(const struct i915_request *rq)
    478 {
    479 	if (!i915_request_is_active(rq))
    480 		return false;
    481 
    482 	return __i915_request_has_started(rq);
    483 }
    484 
    485 /**
    486  * i915_request_is_running - check if the request is ready for execution
    487  * @rq: the request
    488  *
    489  * Upon construction, the request is instructed to wait upon various
    490  * signals before it is ready to be executed by the HW. That is, we do
    491  * not want to start execution and read data before it is written. In practice,
    492  * this is controlled with a mixture of interrupts and semaphores. Once
    493  * the submit fence is completed, the backend scheduler will place the
    494  * request into its queue and from there submit it for execution. So we
    495  * can detect when a request is eligible for execution (and is under control
    496  * of the scheduler) by querying where it is in any of the scheduler's lists.
    497  *
    498  * Returns true if the request is ready for execution (it may be inflight),
    499  * false otherwise.
    500  */
    501 static inline bool i915_request_is_ready(const struct i915_request *rq)
    502 {
    503 	return !list_empty(&rq->sched.link);
    504 }
    505 
    506 static inline bool i915_request_completed(const struct i915_request *rq)
    507 {
    508 	if (i915_request_signaled(rq))
    509 		return true;
    510 
    511 	return i915_seqno_passed(hwsp_seqno(rq), rq->fence.seqno);
    512 }
    513 
    514 static inline void i915_request_mark_complete(struct i915_request *rq)
    515 {
    516 	rq->hwsp_seqno = (u32 *)&rq->fence.seqno; /* decouple from HWSP */
    517 }
    518 
    519 static inline bool i915_request_has_waitboost(const struct i915_request *rq)
    520 {
    521 	return test_bit(I915_FENCE_FLAG_BOOST, &rq->fence.flags);
    522 }
    523 
    524 static inline bool i915_request_has_nopreempt(const struct i915_request *rq)
    525 {
    526 	/* Preemption should only be disabled very rarely */
    527 	return unlikely(test_bit(I915_FENCE_FLAG_NOPREEMPT, &rq->fence.flags));
    528 }
    529 
    530 static inline bool i915_request_has_sentinel(const struct i915_request *rq)
    531 {
    532 	return unlikely(test_bit(I915_FENCE_FLAG_SENTINEL, &rq->fence.flags));
    533 }
    534 
    535 static inline bool i915_request_on_hold(const struct i915_request *rq)
    536 {
    537 	return unlikely(test_bit(I915_FENCE_FLAG_HOLD, &rq->fence.flags));
    538 }
    539 
    540 static inline void i915_request_set_hold(struct i915_request *rq)
    541 {
    542 	set_bit(I915_FENCE_FLAG_HOLD, &rq->fence.flags);
    543 }
    544 
    545 static inline void i915_request_clear_hold(struct i915_request *rq)
    546 {
    547 	clear_bit(I915_FENCE_FLAG_HOLD, &rq->fence.flags);
    548 }
    549 
    550 static inline struct intel_timeline *
    551 i915_request_timeline(struct i915_request *rq)
    552 {
    553 	/* Valid only while the request is being constructed (or retired). */
    554 	return rcu_dereference_protected(rq->timeline,
    555 					 lockdep_is_held(&rcu_access_pointer(rq->timeline)->mutex));
    556 }
    557 
    558 static inline struct i915_gem_context *
    559 i915_request_gem_context(struct i915_request *rq)
    560 {
    561 	/* Valid only while the request is being constructed (or retired). */
    562 	return rcu_dereference_protected(rq->context->gem_context, true);
    563 }
    564 
    565 static inline struct intel_timeline *
    566 i915_request_active_timeline(struct i915_request *rq)
    567 {
    568 	/*
    569 	 * When in use during submission, we are protected by a guarantee that
    570 	 * the context/timeline is pinned and must remain pinned until after
    571 	 * this submission.
    572 	 */
    573 	return rcu_dereference_protected(rq->timeline,
    574 					 lockdep_is_held(&rq->engine->active.lock));
    575 }
    576 
    577 #endif /* I915_REQUEST_H */
    578