2 * Copyright © 2008-2018 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 #ifndef I915_REQUEST_H
26 #define I915_REQUEST_H
28 #include <linux/dma-fence.h>
29 #include <linux/irq_work.h>
30 #include <linux/lockdep.h>
32 #include "gem/i915_gem_context_types.h"
33 #include "gt/intel_context_types.h"
34 #include "gt/intel_engine_types.h"
35 #include "gt/intel_timeline_types.h"
38 #include "i915_scheduler.h"
39 #include "i915_selftest.h"
40 #include "i915_sw_fence.h"
42 #include <uapi/drm/i915_drm.h>
45 struct drm_i915_gem_object;
49 struct i915_capture_list {
50 struct i915_capture_list *next;
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__); \
63 * I915_FENCE_FLAG_ACTIVE - this request is currently submitted to HW.
65 * Set by __i915_request_submit() on handing over to HW, and cleared
66 * by __i915_request_unsubmit() if we preempt this request.
68 * Finally cleared for consistency on retiring the request, when
69 * we know the HW is no longer running this request.
71 * See i915_request_is_active()
73 I915_FENCE_FLAG_ACTIVE = DMA_FENCE_FLAG_USER_BITS,
76 * I915_FENCE_FLAG_PQUEUE - this request is ready for execution
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.
83 * See i915_request_in_priority_queue()
85 I915_FENCE_FLAG_PQUEUE,
88 * I915_FENCE_FLAG_HOLD - this request is currently on hold
90 * This request has been suspended, pending an ongoing investigation.
95 * I915_FENCE_FLAG_INITIAL_BREADCRUMB - this request has the initial
96 * breadcrumb that marks the end of semaphore waits and start of the
99 I915_FENCE_FLAG_INITIAL_BREADCRUMB,
102 * I915_FENCE_FLAG_SIGNAL - this request is currently on signal_list
104 * Internal bookkeeping used by the breadcrumb code to track when
105 * a request is on the various signal_list.
107 I915_FENCE_FLAG_SIGNAL,
110 * I915_FENCE_FLAG_NOPREEMPT - this request should not be preempted
112 * The execution of some requests should not be interrupted. This is
113 * a sensitive operation as it makes the request super important,
114 * blocking other higher priority work. Abuse of this flag will
115 * lead to quality of service issues.
117 I915_FENCE_FLAG_NOPREEMPT,
120 * I915_FENCE_FLAG_SENTINEL - this request should be last in the queue
122 * A high priority sentinel request may be submitted to clear the
123 * submission queue. As it will be the only request in-flight, upon
124 * execution all other active requests will have been preempted and
125 * unsubmitted. This preemptive pulse is used to re-evaluate the
126 * in-flight requests, particularly in cases where an active context
127 * is banned and those active requests need to be cancelled.
129 I915_FENCE_FLAG_SENTINEL,
132 * I915_FENCE_FLAG_BOOST - upclock the gpu for this request
134 * Some requests are more important than others! In particular, a
135 * request that the user is waiting on is typically required for
136 * interactive latency, for which we want to minimise by upclocking
137 * the GPU. Here we track such boost requests on a per-request basis.
139 I915_FENCE_FLAG_BOOST,
143 * Request queue structure.
145 * The request queue allows us to note sequence numbers that have been emitted
146 * and may be associated with active buffers to be retired.
148 * By keeping this list, we can avoid having to do questionable sequence
149 * number comparisons on buffer last_read|write_seqno. It also allows an
150 * emission time to be associated with the request for tracking how far ahead
151 * of the GPU the submission is.
153 * When modifying this structure be very aware that we perform a lockless
154 * RCU lookup of it that may race against reallocation of the struct
155 * from the slab freelist. We intentionally do not zero the structure on
156 * allocation so that the lookup can use the dangling pointers (and is
157 * cogniscent that those pointers may be wrong). Instead, everything that
158 * needs to be initialised must be done so explicitly.
160 * The requests are reference counted.
162 struct i915_request {
163 struct dma_fence fence;
167 * Context and ring buffer related to this request
168 * Contexts are refcounted, so when this request is associated with a
169 * context, we must increment the context's refcount, to guarantee that
170 * it persists while any request is linked to it. Requests themselves
171 * are also refcounted, so the request will only be freed when the last
172 * reference to it is dismissed, and the code in
173 * i915_request_free() will then decrement the refcount on the
176 struct intel_engine_cs *engine;
177 struct intel_context *context;
178 struct intel_ring *ring;
179 struct intel_timeline __rcu *timeline;
181 struct list_head signal_link;
182 struct llist_node signal_node;
185 * The rcu epoch of when this request was allocated. Used to judiciously
186 * apply backpressure on future allocations to ensure that under
187 * mempressure there is sufficient RCU ticks for us to reclaim our
188 * RCU protected slabs.
190 unsigned long rcustate;
193 * We pin the timeline->mutex while constructing the request to
194 * ensure that no caller accidentally drops it during construction.
195 * The timeline->mutex must be held to ensure that only this caller
196 * can use the ring and manipulate the associated timeline during
199 struct pin_cookie cookie;
202 * Fences for the various phases in the request's lifetime.
204 * The submit fence is used to await upon all of the request's
205 * dependencies. When it is signaled, the request is ready to run.
206 * It is used by the driver to then queue the request for execution.
208 struct i915_sw_fence submit;
210 wait_queue_entry_t submitq;
211 struct i915_sw_dma_fence_cb dmaq;
212 struct i915_request_duration_cb {
213 struct dma_fence_cb cb;
217 struct llist_head execute_cb;
218 struct i915_sw_fence semaphore;
221 * A list of everyone we wait upon, and everyone who waits upon us.
222 * Even though we will not be submitted to the hardware before the
223 * submit fence is signaled (it waits for all external events as well
224 * as our own requests), the scheduler still needs to know the
225 * dependency tree for the lifetime of the request (from execbuf
226 * to retirement), i.e. bidirectional dependency information for the
227 * request not tied to individual fences.
229 struct i915_sched_node sched;
230 struct i915_dependency dep;
231 intel_engine_mask_t execution_mask;
234 * A convenience pointer to the current breadcrumb value stored in
235 * the HW status page (or our timeline's local equivalent). The full
236 * path would be rq->hw_context->ring->timeline->hwsp_seqno.
238 const u32 *hwsp_seqno;
241 * If we need to access the timeline's seqno for this request in
242 * another request, we need to keep a read reference to this associated
243 * cacheline, so that we do not free and recycle it before the foreign
244 * observers have completed. Hence, we keep a pointer to the cacheline
245 * inside the timeline's HWSP vma, but it is only valid while this
246 * request has not completed and guarded by the timeline mutex.
248 struct intel_timeline_cacheline __rcu *hwsp_cacheline;
250 /** Position in the ring of the start of the request */
253 /** Position in the ring of the start of the user packets */
257 * Position in the ring of the start of the postfix.
258 * This is required to calculate the maximum available ring space
259 * without overwriting the postfix.
263 /** Position in the ring of the end of the whole request */
266 /** Position in the ring of the end of any workarounds after the tail */
269 /** Preallocate space in the ring for the emitting the request */
272 /** Batch buffer related to this request if any (used for
273 * error state dump only).
275 struct i915_vma *batch;
277 * Additional buffers requested by userspace to be captured upon
278 * a GPU hang. The vma/obj on this list are protected by their
279 * active reference - all objects on this list must also be
280 * on the active_list (of their final request).
282 struct i915_capture_list *capture_list;
284 /** Time at which this request was emitted, in jiffies. */
285 unsigned long emitted_jiffies;
287 /** timeline->request entry for this request */
288 struct list_head link;
290 I915_SELFTEST_DECLARE(struct {
291 struct list_head link;
296 #define I915_FENCE_GFP (GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN)
298 extern const struct dma_fence_ops i915_fence_ops;
300 static inline bool dma_fence_is_i915(const struct dma_fence *fence)
302 return fence->ops == &i915_fence_ops;
305 struct kmem_cache *i915_request_slab_cache(void);
307 struct i915_request * __must_check
308 __i915_request_create(struct intel_context *ce, gfp_t gfp);
309 struct i915_request * __must_check
310 i915_request_create(struct intel_context *ce);
312 void __i915_request_skip(struct i915_request *rq);
313 void i915_request_set_error_once(struct i915_request *rq, int error);
314 void i915_request_mark_eio(struct i915_request *rq);
316 struct i915_request *__i915_request_commit(struct i915_request *request);
317 void __i915_request_queue(struct i915_request *rq,
318 const struct i915_sched_attr *attr);
319 void __i915_request_queue_bh(struct i915_request *rq);
321 bool i915_request_retire(struct i915_request *rq);
322 void i915_request_retire_upto(struct i915_request *rq);
324 static inline struct i915_request *
325 to_request(struct dma_fence *fence)
327 /* We assume that NULL fence/request are interoperable */
328 BUILD_BUG_ON(offsetof(struct i915_request, fence) != 0);
329 GEM_BUG_ON(fence && !dma_fence_is_i915(fence));
330 return container_of(fence, struct i915_request, fence);
333 static inline struct i915_request *
334 i915_request_get(struct i915_request *rq)
336 return to_request(dma_fence_get(&rq->fence));
339 static inline struct i915_request *
340 i915_request_get_rcu(struct i915_request *rq)
342 return to_request(dma_fence_get_rcu(&rq->fence));
346 i915_request_put(struct i915_request *rq)
348 dma_fence_put(&rq->fence);
351 int i915_request_await_object(struct i915_request *to,
352 struct drm_i915_gem_object *obj,
354 int i915_request_await_dma_fence(struct i915_request *rq,
355 struct dma_fence *fence);
356 int i915_request_await_execution(struct i915_request *rq,
357 struct dma_fence *fence,
358 void (*hook)(struct i915_request *rq,
359 struct dma_fence *signal));
361 void i915_request_add(struct i915_request *rq);
363 bool __i915_request_submit(struct i915_request *request);
364 void i915_request_submit(struct i915_request *request);
366 void __i915_request_unsubmit(struct i915_request *request);
367 void i915_request_unsubmit(struct i915_request *request);
369 long i915_request_wait(struct i915_request *rq,
372 __attribute__((nonnull(1)));
373 #define I915_WAIT_INTERRUPTIBLE BIT(0)
374 #define I915_WAIT_PRIORITY BIT(1) /* small priority bump for the request */
375 #define I915_WAIT_ALL BIT(2) /* used by i915_gem_object_wait() */
377 void i915_request_show(struct drm_printer *m,
378 const struct i915_request *rq,
382 static inline bool i915_request_signaled(const struct i915_request *rq)
384 /* The request may live longer than its HWSP, so check flags first! */
385 return test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &rq->fence.flags);
388 static inline bool i915_request_is_active(const struct i915_request *rq)
390 return test_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags);
393 static inline bool i915_request_in_priority_queue(const struct i915_request *rq)
395 return test_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
399 i915_request_has_initial_breadcrumb(const struct i915_request *rq)
401 return test_bit(I915_FENCE_FLAG_INITIAL_BREADCRUMB, &rq->fence.flags);
405 * Returns true if seq1 is later than seq2.
407 static inline bool i915_seqno_passed(u32 seq1, u32 seq2)
409 return (s32)(seq1 - seq2) >= 0;
412 static inline u32 __hwsp_seqno(const struct i915_request *rq)
414 const u32 *hwsp = READ_ONCE(rq->hwsp_seqno);
416 return READ_ONCE(*hwsp);
420 * hwsp_seqno - the current breadcrumb value in the HW status page
421 * @rq: the request, to chase the relevant HW status page
423 * The emphasis in naming here is that hwsp_seqno() is not a property of the
424 * request, but an indication of the current HW state (associated with this
425 * request). Its value will change as the GPU executes more requests.
427 * Returns the current breadcrumb value in the associated HW status page (or
428 * the local timeline's equivalent) for this request. The request itself
429 * has the associated breadcrumb value of rq->fence.seqno, when the HW
430 * status page has that breadcrumb or later, this request is complete.
432 static inline u32 hwsp_seqno(const struct i915_request *rq)
436 rcu_read_lock(); /* the HWSP may be freed at runtime */
437 seqno = __hwsp_seqno(rq);
443 static inline bool __i915_request_has_started(const struct i915_request *rq)
445 return i915_seqno_passed(__hwsp_seqno(rq), rq->fence.seqno - 1);
449 * i915_request_started - check if the request has begun being executed
452 * If the timeline is not using initial breadcrumbs, a request is
453 * considered started if the previous request on its timeline (i.e.
454 * context) has been signaled.
456 * If the timeline is using semaphores, it will also be emitting an
457 * "initial breadcrumb" after the semaphores are complete and just before
458 * it began executing the user payload. A request can therefore be active
459 * on the HW and not yet started as it is still busywaiting on its
460 * dependencies (via HW semaphores).
462 * If the request has started, its dependencies will have been signaled
463 * (either by fences or by semaphores) and it will have begun processing
466 * However, even if a request has started, it may have been preempted and
467 * so no longer active, or it may have already completed.
469 * See also i915_request_is_active().
471 * Returns true if the request has begun executing the user payload, or
474 static inline bool i915_request_started(const struct i915_request *rq)
478 if (i915_request_signaled(rq))
482 rcu_read_lock(); /* the HWSP may be freed at runtime */
483 if (likely(!i915_request_signaled(rq)))
484 /* Remember: started but may have since been preempted! */
485 result = __i915_request_has_started(rq);
492 * i915_request_is_running - check if the request may actually be executing
495 * Returns true if the request is currently submitted to hardware, has passed
496 * its start point (i.e. the context is setup and not busywaiting). Note that
497 * it may no longer be running by the time the function returns!
499 static inline bool i915_request_is_running(const struct i915_request *rq)
503 if (!i915_request_is_active(rq))
507 result = __i915_request_has_started(rq) && i915_request_is_active(rq);
514 * i915_request_is_ready - check if the request is ready for execution
517 * Upon construction, the request is instructed to wait upon various
518 * signals before it is ready to be executed by the HW. That is, we do
519 * not want to start execution and read data before it is written. In practice,
520 * this is controlled with a mixture of interrupts and semaphores. Once
521 * the submit fence is completed, the backend scheduler will place the
522 * request into its queue and from there submit it for execution. So we
523 * can detect when a request is eligible for execution (and is under control
524 * of the scheduler) by querying where it is in any of the scheduler's lists.
526 * Returns true if the request is ready for execution (it may be inflight),
529 static inline bool i915_request_is_ready(const struct i915_request *rq)
531 return !list_empty(&rq->sched.link);
534 static inline bool __i915_request_is_complete(const struct i915_request *rq)
536 return i915_seqno_passed(__hwsp_seqno(rq), rq->fence.seqno);
539 static inline bool i915_request_completed(const struct i915_request *rq)
543 if (i915_request_signaled(rq))
547 rcu_read_lock(); /* the HWSP may be freed at runtime */
548 if (likely(!i915_request_signaled(rq)))
549 result = __i915_request_is_complete(rq);
555 static inline void i915_request_mark_complete(struct i915_request *rq)
557 WRITE_ONCE(rq->hwsp_seqno, /* decouple from HWSP */
558 (u32 *)&rq->fence.seqno);
561 static inline bool i915_request_has_waitboost(const struct i915_request *rq)
563 return test_bit(I915_FENCE_FLAG_BOOST, &rq->fence.flags);
566 static inline bool i915_request_has_nopreempt(const struct i915_request *rq)
568 /* Preemption should only be disabled very rarely */
569 return unlikely(test_bit(I915_FENCE_FLAG_NOPREEMPT, &rq->fence.flags));
572 static inline bool i915_request_has_sentinel(const struct i915_request *rq)
574 return unlikely(test_bit(I915_FENCE_FLAG_SENTINEL, &rq->fence.flags));
577 static inline bool i915_request_on_hold(const struct i915_request *rq)
579 return unlikely(test_bit(I915_FENCE_FLAG_HOLD, &rq->fence.flags));
582 static inline void i915_request_set_hold(struct i915_request *rq)
584 set_bit(I915_FENCE_FLAG_HOLD, &rq->fence.flags);
587 static inline void i915_request_clear_hold(struct i915_request *rq)
589 clear_bit(I915_FENCE_FLAG_HOLD, &rq->fence.flags);
592 static inline struct intel_timeline *
593 i915_request_timeline(const struct i915_request *rq)
595 /* Valid only while the request is being constructed (or retired). */
596 return rcu_dereference_protected(rq->timeline,
597 lockdep_is_held(&rcu_access_pointer(rq->timeline)->mutex));
600 static inline struct i915_gem_context *
601 i915_request_gem_context(const struct i915_request *rq)
603 /* Valid only while the request is being constructed (or retired). */
604 return rcu_dereference_protected(rq->context->gem_context, true);
607 static inline struct intel_timeline *
608 i915_request_active_timeline(const struct i915_request *rq)
611 * When in use during submission, we are protected by a guarantee that
612 * the context/timeline is pinned and must remain pinned until after
615 return rcu_dereference_protected(rq->timeline,
616 lockdep_is_held(&rq->engine->active.lock));
619 #endif /* I915_REQUEST_H */