Merge tag 'tag-chrome-platform-fixes-for-v5.3-rc6' of git://git.kernel.org/pub/scm...
[sfrench/cifs-2.6.git] / drivers / gpu / drm / i915 / gt / intel_engine_types.h
1 /*
2  * SPDX-License-Identifier: MIT
3  *
4  * Copyright © 2019 Intel Corporation
5  */
6
7 #ifndef __INTEL_ENGINE_TYPES__
8 #define __INTEL_ENGINE_TYPES__
9
10 #include <linux/hashtable.h>
11 #include <linux/irq_work.h>
12 #include <linux/kref.h>
13 #include <linux/list.h>
14 #include <linux/llist.h>
15 #include <linux/types.h>
16
17 #include "i915_gem.h"
18 #include "i915_gem_batch_pool.h"
19 #include "i915_pmu.h"
20 #include "i915_priolist_types.h"
21 #include "i915_selftest.h"
22 #include "i915_timeline_types.h"
23 #include "intel_sseu.h"
24 #include "intel_wakeref.h"
25 #include "intel_workarounds_types.h"
26
27 #define I915_MAX_SLICES 3
28 #define I915_MAX_SUBSLICES 8
29
30 #define I915_CMD_HASH_ORDER 9
31
32 struct dma_fence;
33 struct drm_i915_gem_object;
34 struct drm_i915_reg_table;
35 struct i915_gem_context;
36 struct i915_request;
37 struct i915_sched_attr;
38 struct intel_uncore;
39
40 typedef u8 intel_engine_mask_t;
41 #define ALL_ENGINES ((intel_engine_mask_t)~0ul)
42
43 struct intel_hw_status_page {
44         struct i915_vma *vma;
45         u32 *addr;
46 };
47
48 struct intel_instdone {
49         u32 instdone;
50         /* The following exist only in the RCS engine */
51         u32 slice_common;
52         u32 sampler[I915_MAX_SLICES][I915_MAX_SUBSLICES];
53         u32 row[I915_MAX_SLICES][I915_MAX_SUBSLICES];
54 };
55
56 struct intel_engine_hangcheck {
57         u64 acthd;
58         u32 last_ring;
59         u32 last_head;
60         unsigned long action_timestamp;
61         struct intel_instdone instdone;
62 };
63
64 struct intel_ring {
65         struct kref ref;
66         struct i915_vma *vma;
67         void *vaddr;
68
69         struct i915_timeline *timeline;
70         struct list_head request_list;
71         struct list_head active_link;
72
73         /*
74          * As we have two types of rings, one global to the engine used
75          * by ringbuffer submission and those that are exclusive to a
76          * context used by execlists, we have to play safe and allow
77          * atomic updates to the pin_count. However, the actual pinning
78          * of the context is either done during initialisation for
79          * ringbuffer submission or serialised as part of the context
80          * pinning for execlists, and so we do not need a mutex ourselves
81          * to serialise intel_ring_pin/intel_ring_unpin.
82          */
83         atomic_t pin_count;
84
85         u32 head;
86         u32 tail;
87         u32 emit;
88
89         u32 space;
90         u32 size;
91         u32 effective_size;
92 };
93
94 /*
95  * we use a single page to load ctx workarounds so all of these
96  * values are referred in terms of dwords
97  *
98  * struct i915_wa_ctx_bb:
99  *  offset: specifies batch starting position, also helpful in case
100  *    if we want to have multiple batches at different offsets based on
101  *    some criteria. It is not a requirement at the moment but provides
102  *    an option for future use.
103  *  size: size of the batch in DWORDS
104  */
105 struct i915_ctx_workarounds {
106         struct i915_wa_ctx_bb {
107                 u32 offset;
108                 u32 size;
109         } indirect_ctx, per_ctx;
110         struct i915_vma *vma;
111 };
112
113 #define I915_MAX_VCS    4
114 #define I915_MAX_VECS   2
115
116 /*
117  * Engine IDs definitions.
118  * Keep instances of the same type engine together.
119  */
120 enum intel_engine_id {
121         RCS0 = 0,
122         BCS0,
123         VCS0,
124         VCS1,
125         VCS2,
126         VCS3,
127 #define _VCS(n) (VCS0 + (n))
128         VECS0,
129         VECS1,
130 #define _VECS(n) (VECS0 + (n))
131         I915_NUM_ENGINES
132 };
133
134 struct st_preempt_hang {
135         struct completion completion;
136         unsigned int count;
137         bool inject_hang;
138 };
139
140 /**
141  * struct intel_engine_execlists - execlist submission queue and port state
142  *
143  * The struct intel_engine_execlists represents the combined logical state of
144  * driver and the hardware state for execlist mode of submission.
145  */
146 struct intel_engine_execlists {
147         /**
148          * @tasklet: softirq tasklet for bottom handler
149          */
150         struct tasklet_struct tasklet;
151
152         /**
153          * @default_priolist: priority list for I915_PRIORITY_NORMAL
154          */
155         struct i915_priolist default_priolist;
156
157         /**
158          * @no_priolist: priority lists disabled
159          */
160         bool no_priolist;
161
162         /**
163          * @submit_reg: gen-specific execlist submission register
164          * set to the ExecList Submission Port (elsp) register pre-Gen11 and to
165          * the ExecList Submission Queue Contents register array for Gen11+
166          */
167         u32 __iomem *submit_reg;
168
169         /**
170          * @ctrl_reg: the enhanced execlists control register, used to load the
171          * submit queue on the HW and to request preemptions to idle
172          */
173         u32 __iomem *ctrl_reg;
174
175         /**
176          * @port: execlist port states
177          *
178          * For each hardware ELSP (ExecList Submission Port) we keep
179          * track of the last request and the number of times we submitted
180          * that port to hw. We then count the number of times the hw reports
181          * a context completion or preemption. As only one context can
182          * be active on hw, we limit resubmission of context to port[0]. This
183          * is called Lite Restore, of the context.
184          */
185         struct execlist_port {
186                 /**
187                  * @request_count: combined request and submission count
188                  */
189                 struct i915_request *request_count;
190 #define EXECLIST_COUNT_BITS 2
191 #define port_request(p) ptr_mask_bits((p)->request_count, EXECLIST_COUNT_BITS)
192 #define port_count(p) ptr_unmask_bits((p)->request_count, EXECLIST_COUNT_BITS)
193 #define port_pack(rq, count) ptr_pack_bits(rq, count, EXECLIST_COUNT_BITS)
194 #define port_unpack(p, count) ptr_unpack_bits((p)->request_count, count, EXECLIST_COUNT_BITS)
195 #define port_set(p, packed) ((p)->request_count = (packed))
196 #define port_isset(p) ((p)->request_count)
197 #define port_index(p, execlists) ((p) - (execlists)->port)
198
199                 /**
200                  * @context_id: context ID for port
201                  */
202                 GEM_DEBUG_DECL(u32 context_id);
203
204 #define EXECLIST_MAX_PORTS 2
205         } port[EXECLIST_MAX_PORTS];
206
207         /**
208          * @active: is the HW active? We consider the HW as active after
209          * submitting any context for execution and until we have seen the
210          * last context completion event. After that, we do not expect any
211          * more events until we submit, and so can park the HW.
212          *
213          * As we have a small number of different sources from which we feed
214          * the HW, we track the state of each inside a single bitfield.
215          */
216         unsigned int active;
217 #define EXECLISTS_ACTIVE_USER 0
218 #define EXECLISTS_ACTIVE_PREEMPT 1
219 #define EXECLISTS_ACTIVE_HWACK 2
220
221         /**
222          * @port_mask: number of execlist ports - 1
223          */
224         unsigned int port_mask;
225
226         /**
227          * @queue_priority_hint: Highest pending priority.
228          *
229          * When we add requests into the queue, or adjust the priority of
230          * executing requests, we compute the maximum priority of those
231          * pending requests. We can then use this value to determine if
232          * we need to preempt the executing requests to service the queue.
233          * However, since the we may have recorded the priority of an inflight
234          * request we wanted to preempt but since completed, at the time of
235          * dequeuing the priority hint may no longer may match the highest
236          * available request priority.
237          */
238         int queue_priority_hint;
239
240         /**
241          * @queue: queue of requests, in priority lists
242          */
243         struct rb_root_cached queue;
244         struct rb_root_cached virtual;
245
246         /**
247          * @csb_write: control register for Context Switch buffer
248          *
249          * Note this register may be either mmio or HWSP shadow.
250          */
251         u32 *csb_write;
252
253         /**
254          * @csb_status: status array for Context Switch buffer
255          *
256          * Note these register may be either mmio or HWSP shadow.
257          */
258         u32 *csb_status;
259
260         /**
261          * @preempt_complete_status: expected CSB upon completing preemption
262          */
263         u32 preempt_complete_status;
264
265         /**
266          * @csb_size: context status buffer FIFO size
267          */
268         u8 csb_size;
269
270         /**
271          * @csb_head: context status buffer head
272          */
273         u8 csb_head;
274
275         I915_SELFTEST_DECLARE(struct st_preempt_hang preempt_hang;)
276 };
277
278 #define INTEL_ENGINE_CS_MAX_NAME 8
279
280 struct intel_engine_cs {
281         struct drm_i915_private *i915;
282         struct intel_uncore *uncore;
283         char name[INTEL_ENGINE_CS_MAX_NAME];
284
285         enum intel_engine_id id;
286         unsigned int hw_id;
287         unsigned int guc_id;
288         intel_engine_mask_t mask;
289
290         u8 uabi_class;
291
292         u8 class;
293         u8 instance;
294         u32 context_size;
295         u32 mmio_base;
296
297         u32 uabi_capabilities;
298
299         struct intel_sseu sseu;
300
301         struct intel_ring *buffer;
302
303         struct {
304                 spinlock_t lock;
305                 struct list_head requests;
306         } active;
307
308         struct llist_head barrier_tasks;
309
310         struct intel_context *kernel_context; /* pinned */
311         struct intel_context *preempt_context; /* pinned; optional */
312
313         intel_engine_mask_t saturated; /* submitting semaphores too late? */
314
315         unsigned long serial;
316
317         unsigned long wakeref_serial;
318         struct intel_wakeref wakeref;
319         struct drm_i915_gem_object *default_state;
320         void *pinned_default_state;
321
322         /* Rather than have every client wait upon all user interrupts,
323          * with the herd waking after every interrupt and each doing the
324          * heavyweight seqno dance, we delegate the task (of being the
325          * bottom-half of the user interrupt) to the first client. After
326          * every interrupt, we wake up one client, who does the heavyweight
327          * coherent seqno read and either goes back to sleep (if incomplete),
328          * or wakes up all the completed clients in parallel, before then
329          * transferring the bottom-half status to the next client in the queue.
330          *
331          * Compared to walking the entire list of waiters in a single dedicated
332          * bottom-half, we reduce the latency of the first waiter by avoiding
333          * a context switch, but incur additional coherent seqno reads when
334          * following the chain of request breadcrumbs. Since it is most likely
335          * that we have a single client waiting on each seqno, then reducing
336          * the overhead of waking that client is much preferred.
337          */
338         struct intel_breadcrumbs {
339                 spinlock_t irq_lock;
340                 struct list_head signalers;
341
342                 struct irq_work irq_work; /* for use from inside irq_lock */
343
344                 unsigned int irq_enabled;
345
346                 bool irq_armed;
347         } breadcrumbs;
348
349         struct intel_engine_pmu {
350                 /**
351                  * @enable: Bitmask of enable sample events on this engine.
352                  *
353                  * Bits correspond to sample event types, for instance
354                  * I915_SAMPLE_QUEUED is bit 0 etc.
355                  */
356                 u32 enable;
357                 /**
358                  * @enable_count: Reference count for the enabled samplers.
359                  *
360                  * Index number corresponds to @enum drm_i915_pmu_engine_sample.
361                  */
362                 unsigned int enable_count[I915_ENGINE_SAMPLE_COUNT];
363                 /**
364                  * @sample: Counter values for sampling events.
365                  *
366                  * Our internal timer stores the current counters in this field.
367                  *
368                  * Index number corresponds to @enum drm_i915_pmu_engine_sample.
369                  */
370                 struct i915_pmu_sample sample[I915_ENGINE_SAMPLE_COUNT];
371         } pmu;
372
373         /*
374          * A pool of objects to use as shadow copies of client batch buffers
375          * when the command parser is enabled. Prevents the client from
376          * modifying the batch contents after software parsing.
377          */
378         struct i915_gem_batch_pool batch_pool;
379
380         struct intel_hw_status_page status_page;
381         struct i915_ctx_workarounds wa_ctx;
382         struct i915_wa_list ctx_wa_list;
383         struct i915_wa_list wa_list;
384         struct i915_wa_list whitelist;
385
386         u32             irq_keep_mask; /* always keep these interrupts */
387         u32             irq_enable_mask; /* bitmask to enable ring interrupt */
388         void            (*irq_enable)(struct intel_engine_cs *engine);
389         void            (*irq_disable)(struct intel_engine_cs *engine);
390
391         int             (*resume)(struct intel_engine_cs *engine);
392
393         struct {
394                 void (*prepare)(struct intel_engine_cs *engine);
395                 void (*reset)(struct intel_engine_cs *engine, bool stalled);
396                 void (*finish)(struct intel_engine_cs *engine);
397         } reset;
398
399         void            (*park)(struct intel_engine_cs *engine);
400         void            (*unpark)(struct intel_engine_cs *engine);
401
402         void            (*set_default_submission)(struct intel_engine_cs *engine);
403
404         const struct intel_context_ops *cops;
405
406         int             (*request_alloc)(struct i915_request *rq);
407         int             (*init_context)(struct i915_request *rq);
408
409         int             (*emit_flush)(struct i915_request *request, u32 mode);
410 #define EMIT_INVALIDATE BIT(0)
411 #define EMIT_FLUSH      BIT(1)
412 #define EMIT_BARRIER    (EMIT_INVALIDATE | EMIT_FLUSH)
413         int             (*emit_bb_start)(struct i915_request *rq,
414                                          u64 offset, u32 length,
415                                          unsigned int dispatch_flags);
416 #define I915_DISPATCH_SECURE BIT(0)
417 #define I915_DISPATCH_PINNED BIT(1)
418         int              (*emit_init_breadcrumb)(struct i915_request *rq);
419         u32             *(*emit_fini_breadcrumb)(struct i915_request *rq,
420                                                  u32 *cs);
421         unsigned int    emit_fini_breadcrumb_dw;
422
423         /* Pass the request to the hardware queue (e.g. directly into
424          * the legacy ringbuffer or to the end of an execlist).
425          *
426          * This is called from an atomic context with irqs disabled; must
427          * be irq safe.
428          */
429         void            (*submit_request)(struct i915_request *rq);
430
431         /*
432          * Called on signaling of a SUBMIT_FENCE, passing along the signaling
433          * request down to the bonded pairs.
434          */
435         void            (*bond_execute)(struct i915_request *rq,
436                                         struct dma_fence *signal);
437
438         /*
439          * Call when the priority on a request has changed and it and its
440          * dependencies may need rescheduling. Note the request itself may
441          * not be ready to run!
442          */
443         void            (*schedule)(struct i915_request *request,
444                                     const struct i915_sched_attr *attr);
445
446         /*
447          * Cancel all requests on the hardware, or queued for execution.
448          * This should only cancel the ready requests that have been
449          * submitted to the engine (via the engine->submit_request callback).
450          * This is called when marking the device as wedged.
451          */
452         void            (*cancel_requests)(struct intel_engine_cs *engine);
453
454         void            (*destroy)(struct intel_engine_cs *engine);
455
456         struct intel_engine_execlists execlists;
457
458         /* status_notifier: list of callbacks for context-switch changes */
459         struct atomic_notifier_head context_status_notifier;
460
461         struct intel_engine_hangcheck hangcheck;
462
463 #define I915_ENGINE_NEEDS_CMD_PARSER BIT(0)
464 #define I915_ENGINE_SUPPORTS_STATS   BIT(1)
465 #define I915_ENGINE_HAS_PREEMPTION   BIT(2)
466 #define I915_ENGINE_HAS_SEMAPHORES   BIT(3)
467 #define I915_ENGINE_NEEDS_BREADCRUMB_TASKLET BIT(4)
468 #define I915_ENGINE_IS_VIRTUAL       BIT(5)
469         unsigned int flags;
470
471         /*
472          * Table of commands the command parser needs to know about
473          * for this engine.
474          */
475         DECLARE_HASHTABLE(cmd_hash, I915_CMD_HASH_ORDER);
476
477         /*
478          * Table of registers allowed in commands that read/write registers.
479          */
480         const struct drm_i915_reg_table *reg_tables;
481         int reg_table_count;
482
483         /*
484          * Returns the bitmask for the length field of the specified command.
485          * Return 0 for an unrecognized/invalid command.
486          *
487          * If the command parser finds an entry for a command in the engine's
488          * cmd_tables, it gets the command's length based on the table entry.
489          * If not, it calls this function to determine the per-engine length
490          * field encoding for the command (i.e. different opcode ranges use
491          * certain bits to encode the command length in the header).
492          */
493         u32 (*get_cmd_length_mask)(u32 cmd_header);
494
495         struct {
496                 /**
497                  * @lock: Lock protecting the below fields.
498                  */
499                 seqlock_t lock;
500                 /**
501                  * @enabled: Reference count indicating number of listeners.
502                  */
503                 unsigned int enabled;
504                 /**
505                  * @active: Number of contexts currently scheduled in.
506                  */
507                 unsigned int active;
508                 /**
509                  * @enabled_at: Timestamp when busy stats were enabled.
510                  */
511                 ktime_t enabled_at;
512                 /**
513                  * @start: Timestamp of the last idle to active transition.
514                  *
515                  * Idle is defined as active == 0, active is active > 0.
516                  */
517                 ktime_t start;
518                 /**
519                  * @total: Total time this engine was busy.
520                  *
521                  * Accumulated time not counting the most recent block in cases
522                  * where engine is currently busy (active > 0).
523                  */
524                 ktime_t total;
525         } stats;
526 };
527
528 static inline bool
529 intel_engine_needs_cmd_parser(const struct intel_engine_cs *engine)
530 {
531         return engine->flags & I915_ENGINE_NEEDS_CMD_PARSER;
532 }
533
534 static inline bool
535 intel_engine_supports_stats(const struct intel_engine_cs *engine)
536 {
537         return engine->flags & I915_ENGINE_SUPPORTS_STATS;
538 }
539
540 static inline bool
541 intel_engine_has_preemption(const struct intel_engine_cs *engine)
542 {
543         return engine->flags & I915_ENGINE_HAS_PREEMPTION;
544 }
545
546 static inline bool
547 intel_engine_has_semaphores(const struct intel_engine_cs *engine)
548 {
549         return engine->flags & I915_ENGINE_HAS_SEMAPHORES;
550 }
551
552 static inline bool
553 intel_engine_needs_breadcrumb_tasklet(const struct intel_engine_cs *engine)
554 {
555         return engine->flags & I915_ENGINE_NEEDS_BREADCRUMB_TASKLET;
556 }
557
558 static inline bool
559 intel_engine_is_virtual(const struct intel_engine_cs *engine)
560 {
561         return engine->flags & I915_ENGINE_IS_VIRTUAL;
562 }
563
564 #define instdone_slice_mask(dev_priv__) \
565         (IS_GEN(dev_priv__, 7) ? \
566          1 : RUNTIME_INFO(dev_priv__)->sseu.slice_mask)
567
568 #define instdone_subslice_mask(dev_priv__) \
569         (IS_GEN(dev_priv__, 7) ? \
570          1 : RUNTIME_INFO(dev_priv__)->sseu.subslice_mask[0])
571
572 #define for_each_instdone_slice_subslice(dev_priv__, slice__, subslice__) \
573         for ((slice__) = 0, (subslice__) = 0; \
574              (slice__) < I915_MAX_SLICES; \
575              (subslice__) = ((subslice__) + 1) < I915_MAX_SUBSLICES ? (subslice__) + 1 : 0, \
576                (slice__) += ((subslice__) == 0)) \
577                 for_each_if((BIT(slice__) & instdone_slice_mask(dev_priv__)) && \
578                             (BIT(subslice__) & instdone_subslice_mask(dev_priv__)))
579
580 #endif /* __INTEL_ENGINE_TYPES_H__ */