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