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