1 /* SPDX-License-Identifier: MIT */
2 /*
3 * Copyright © 2019 Intel Corporation
4 */
5
6 #ifndef __INTEL_ENGINE_TYPES__
7 #define __INTEL_ENGINE_TYPES__
8
9 #include <linux/average.h>
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/rbtree.h>
16 #include <linux/timer.h>
17 #include <linux/types.h>
18 #include <linux/workqueue.h>
19
20 #include "i915_gem.h"
21 #include "i915_pmu.h"
22 #include "i915_priolist_types.h"
23 #include "i915_selftest.h"
24 #include "intel_sseu.h"
25 #include "intel_timeline_types.h"
26 #include "intel_uncore.h"
27 #include "intel_wakeref.h"
28 #include "intel_workarounds_types.h"
29
30 /* HW Engine class + instance */
31 #define RENDER_CLASS 0
32 #define VIDEO_DECODE_CLASS 1
33 #define VIDEO_ENHANCEMENT_CLASS 2
34 #define COPY_ENGINE_CLASS 3
35 #define OTHER_CLASS 4
36 #define MAX_ENGINE_CLASS 4
37 #define MAX_ENGINE_INSTANCE 7
38
39 #define I915_MAX_SLICES 3
40 #define I915_MAX_SUBSLICES 8
41
42 #define I915_CMD_HASH_ORDER 9
43
44 struct dma_fence;
45 struct drm_i915_gem_object;
46 struct drm_i915_reg_table;
47 struct i915_gem_context;
48 struct i915_request;
49 struct i915_sched_attr;
50 struct i915_sched_engine;
51 struct intel_gt;
52 struct intel_ring;
53 struct intel_uncore;
54 struct intel_breadcrumbs;
55
56 typedef u32 intel_engine_mask_t;
57 #define ALL_ENGINES ((intel_engine_mask_t)~0ul)
58
59 struct intel_hw_status_page {
60 struct list_head timelines;
61 struct i915_vma *vma;
62 u32 *addr;
63 };
64
65 struct intel_instdone {
66 u32 instdone;
67 /* The following exist only in the RCS engine */
68 u32 slice_common;
69 u32 slice_common_extra[2];
70 u32 sampler[I915_MAX_SLICES][I915_MAX_SUBSLICES];
71 u32 row[I915_MAX_SLICES][I915_MAX_SUBSLICES];
72 };
73
74 /*
75 * we use a single page to load ctx workarounds so all of these
76 * values are referred in terms of dwords
77 *
78 * struct i915_wa_ctx_bb:
79 * offset: specifies batch starting position, also helpful in case
80 * if we want to have multiple batches at different offsets based on
81 * some criteria. It is not a requirement at the moment but provides
82 * an option for future use.
83 * size: size of the batch in DWORDS
84 */
85 struct i915_ctx_workarounds {
86 struct i915_wa_ctx_bb {
87 u32 offset;
88 u32 size;
89 } indirect_ctx, per_ctx;
90 struct i915_vma *vma;
91 };
92
93 #define I915_MAX_VCS 8
94 #define I915_MAX_VECS 4
95
96 /*
97 * Engine IDs definitions.
98 * Keep instances of the same type engine together.
99 */
100 enum intel_engine_id {
101 RCS0 = 0,
102 BCS0,
103 VCS0,
104 VCS1,
105 VCS2,
106 VCS3,
107 VCS4,
108 VCS5,
109 VCS6,
110 VCS7,
111 #define _VCS(n) (VCS0 + (n))
112 VECS0,
113 VECS1,
114 VECS2,
115 VECS3,
116 #define _VECS(n) (VECS0 + (n))
117 I915_NUM_ENGINES
118 #define INVALID_ENGINE ((enum intel_engine_id)-1)
119 };
120
121 /* A simple estimator for the round-trip latency of an engine */
122 DECLARE_EWMA(_engine_latency, 6, 4)
123
124 struct st_preempt_hang {
125 struct completion completion;
126 unsigned int count;
127 };
128
129 /**
130 * struct intel_engine_execlists - execlist submission queue and port state
131 *
132 * The struct intel_engine_execlists represents the combined logical state of
133 * driver and the hardware state for execlist mode of submission.
134 */
135 struct intel_engine_execlists {
136 /**
137 * @timer: kick the current context if its timeslice expires
138 */
139 struct timer_list timer;
140
141 /**
142 * @preempt: reset the current context if it fails to give way
143 */
144 struct timer_list preempt;
145
146 /**
147 * @preempt_target: active request at the time of the preemption request
148 *
149 * We force a preemption to occur if the pending contexts have not
150 * been promoted to active upon receipt of the CS ack event within
151 * the timeout. This timeout maybe chosen based on the target,
152 * using a very short timeout if the context is no longer schedulable.
153 * That short timeout may not be applicable to other contexts, so
154 * if a context switch should happen within before the preemption
155 * timeout, we may shoot early at an innocent context. To prevent this,
156 * we record which context was active at the time of the preemption
157 * request and only reset that context upon the timeout.
158 */
159 const struct i915_request *preempt_target;
160
161 /**
162 * @ccid: identifier for contexts submitted to this engine
163 */
164 u32 ccid;
165
166 /**
167 * @yield: CCID at the time of the last semaphore-wait interrupt.
168 *
169 * Instead of leaving a semaphore busy-spinning on an engine, we would
170 * like to switch to another ready context, i.e. yielding the semaphore
171 * timeslice.
172 */
173 u32 yield;
174
175 /**
176 * @error_interrupt: CS Master EIR
177 *
178 * The CS generates an interrupt when it detects an error. We capture
179 * the first error interrupt, record the EIR and schedule the tasklet.
180 * In the tasklet, we process the pending CS events to ensure we have
181 * the guilty request, and then reset the engine.
182 *
183 * Low 16b are used by HW, with the upper 16b used as the enabling mask.
184 * Reserve the upper 16b for tracking internal errors.
185 */
186 u32 error_interrupt;
187 #define ERROR_CSB BIT(31)
188 #define ERROR_PREEMPT BIT(30)
189
190 /**
191 * @reset_ccid: Active CCID [EXECLISTS_STATUS_HI] at the time of reset
192 */
193 u32 reset_ccid;
194
195 /**
196 * @submit_reg: gen-specific execlist submission register
197 * set to the ExecList Submission Port (elsp) register pre-Gen11 and to
198 * the ExecList Submission Queue Contents register array for Gen11+
199 */
200 u32 __iomem *submit_reg;
201
202 /**
203 * @ctrl_reg: the enhanced execlists control register, used to load the
204 * submit queue on the HW and to request preemptions to idle
205 */
206 u32 __iomem *ctrl_reg;
207
208 #define EXECLIST_MAX_PORTS 2
209 /**
210 * @active: the currently known context executing on HW
211 */
212 struct i915_request * const *active;
213 /**
214 * @inflight: the set of contexts submitted and acknowleged by HW
215 *
216 * The set of inflight contexts is managed by reading CS events
217 * from the HW. On a context-switch event (not preemption), we
218 * know the HW has transitioned from port0 to port1, and we
219 * advance our inflight/active tracking accordingly.
220 */
221 struct i915_request *inflight[EXECLIST_MAX_PORTS + 1 /* sentinel */];
222 /**
223 * @pending: the next set of contexts submitted to ELSP
224 *
225 * We store the array of contexts that we submit to HW (via ELSP) and
226 * promote them to the inflight array once HW has signaled the
227 * preemption or idle-to-active event.
228 */
229 struct i915_request *pending[EXECLIST_MAX_PORTS + 1];
230
231 /**
232 * @port_mask: number of execlist ports - 1
233 */
234 unsigned int port_mask;
235
236 /**
237 * @virtual: Queue of requets on a virtual engine, sorted by priority.
238 * Each RB entry is a struct i915_priolist containing a list of requests
239 * of the same priority.
240 */
241 struct rb_root_cached virtual;
242
243 /**
244 * @csb_write: control register for Context Switch buffer
245 *
246 * Note this register may be either mmio or HWSP shadow.
247 */
248 u32 *csb_write;
249
250 /**
251 * @csb_status: status array for Context Switch buffer
252 *
253 * Note these register may be either mmio or HWSP shadow.
254 */
255 u64 *csb_status;
256
257 /**
258 * @csb_size: context status buffer FIFO size
259 */
260 u8 csb_size;
261
262 /**
263 * @csb_head: context status buffer head
264 */
265 u8 csb_head;
266
267 I915_SELFTEST_DECLARE(struct st_preempt_hang preempt_hang;)
268 };
269
270 #define INTEL_ENGINE_CS_MAX_NAME 8
271
272 struct intel_engine_cs {
273 struct drm_i915_private *i915;
274 struct intel_gt *gt;
275 struct intel_uncore *uncore;
276 char name[INTEL_ENGINE_CS_MAX_NAME];
277
278 enum intel_engine_id id;
279 enum intel_engine_id legacy_idx;
280
281 unsigned int guc_id;
282
283 intel_engine_mask_t mask;
284
285 u8 class;
286 u8 instance;
287
288 u16 uabi_class;
289 u16 uabi_instance;
290
291 u32 uabi_capabilities;
292 u32 context_size;
293 u32 mmio_base;
294
295 /*
296 * Some w/a require forcewake to be held (which prevents RC6) while
297 * a particular engine is active. If so, we set fw_domain to which
298 * domains need to be held for the duration of request activity,
299 * and 0 if none. We try to limit the duration of the hold as much
300 * as possible.
301 */
302 enum forcewake_domains fw_domain;
303 unsigned int fw_active;
304
305 unsigned long context_tag;
306
307 struct rb_node uabi_node;
308
309 struct intel_sseu sseu;
310
311 struct i915_sched_engine *sched_engine;
312
313 /* keep a request in reserve for a [pm] barrier under oom */
314 struct i915_request *request_pool;
315
316 struct intel_context *hung_ce;
317
318 struct llist_head barrier_tasks;
319
320 struct intel_context *kernel_context; /* pinned */
321
322 /**
323 * pinned_contexts_list: List of pinned contexts. This list is only
324 * assumed to be manipulated during driver load- or unload time and
325 * does therefore not have any additional protection.
326 */
327 struct list_head pinned_contexts_list;
328
329 intel_engine_mask_t saturated; /* submitting semaphores too late? */
330
331 struct {
332 struct delayed_work work;
333 struct i915_request *systole;
334 unsigned long blocked;
335 } heartbeat;
336
337 unsigned long serial;
338
339 unsigned long wakeref_serial;
340 struct intel_wakeref wakeref;
341 struct file *default_state;
342
343 struct {
344 struct intel_ring *ring;
345 struct intel_timeline *timeline;
346 } legacy;
347
348 /*
349 * We track the average duration of the idle pulse on parking the
350 * engine to keep an estimate of the how the fast the engine is
351 * under ideal conditions.
352 */
353 struct ewma__engine_latency latency;
354
355 /* Keep track of all the seqno used, a trail of breadcrumbs */
356 struct intel_breadcrumbs *breadcrumbs;
357
358 struct intel_engine_pmu {
359 /**
360 * @enable: Bitmask of enable sample events on this engine.
361 *
362 * Bits correspond to sample event types, for instance
363 * I915_SAMPLE_QUEUED is bit 0 etc.
364 */
365 u32 enable;
366 /**
367 * @enable_count: Reference count for the enabled samplers.
368 *
369 * Index number corresponds to @enum drm_i915_pmu_engine_sample.
370 */
371 unsigned int enable_count[I915_ENGINE_SAMPLE_COUNT];
372 /**
373 * @sample: Counter values for sampling events.
374 *
375 * Our internal timer stores the current counters in this field.
376 *
377 * Index number corresponds to @enum drm_i915_pmu_engine_sample.
378 */
379 struct i915_pmu_sample sample[I915_ENGINE_SAMPLE_COUNT];
380 } pmu;
381
382 struct intel_hw_status_page status_page;
383 struct i915_ctx_workarounds wa_ctx;
384 struct i915_wa_list ctx_wa_list;
385 struct i915_wa_list wa_list;
386 struct i915_wa_list whitelist;
387
388 u32 irq_keep_mask; /* always keep these interrupts */
389 u32 irq_enable_mask; /* bitmask to enable ring interrupt */
390 void (*irq_enable)(struct intel_engine_cs *engine);
391 void (*irq_disable)(struct intel_engine_cs *engine);
392 void (*irq_handler)(struct intel_engine_cs *engine, u16 iir);
393
394 void (*sanitize)(struct intel_engine_cs *engine);
395 int (*resume)(struct intel_engine_cs *engine);
396
397 struct {
398 void (*prepare)(struct intel_engine_cs *engine);
399
400 void (*rewind)(struct intel_engine_cs *engine, bool stalled);
401 void (*cancel)(struct intel_engine_cs *engine);
402
403 void (*finish)(struct intel_engine_cs *engine);
404 } reset;
405
406 void (*park)(struct intel_engine_cs *engine);
407 void (*unpark)(struct intel_engine_cs *engine);
408
409 void (*bump_serial)(struct intel_engine_cs *engine);
410
411 void (*set_default_submission)(struct intel_engine_cs *engine);
412
413 const struct intel_context_ops *cops;
414
415 int (*request_alloc)(struct i915_request *rq);
416
417 int (*emit_flush)(struct i915_request *request, u32 mode);
418 #define EMIT_INVALIDATE BIT(0)
419 #define EMIT_FLUSH BIT(1)
420 #define EMIT_BARRIER (EMIT_INVALIDATE | EMIT_FLUSH)
421 int (*emit_bb_start)(struct i915_request *rq,
422 u64 offset, u32 length,
423 unsigned int dispatch_flags);
424 #define I915_DISPATCH_SECURE BIT(0)
425 #define I915_DISPATCH_PINNED BIT(1)
426 int (*emit_init_breadcrumb)(struct i915_request *rq);
427 u32 *(*emit_fini_breadcrumb)(struct i915_request *rq,
428 u32 *cs);
429 unsigned int emit_fini_breadcrumb_dw;
430
431 /* Pass the request to the hardware queue (e.g. directly into
432 * the legacy ringbuffer or to the end of an execlist).
433 *
434 * This is called from an atomic context with irqs disabled; must
435 * be irq safe.
436 */
437 void (*submit_request)(struct i915_request *rq);
438
439 void (*release)(struct intel_engine_cs *engine);
440
441 /*
442 * Add / remove request from engine active tracking
443 */
444 void (*add_active_request)(struct i915_request *rq);
445 void (*remove_active_request)(struct i915_request *rq);
446
447 struct intel_engine_execlists execlists;
448
449 /*
450 * Keep track of completed timelines on this engine for early
451 * retirement with the goal of quickly enabling powersaving as
452 * soon as the engine is idle.
453 */
454 struct intel_timeline *retire;
455 struct work_struct retire_work;
456
457 /* status_notifier: list of callbacks for context-switch changes */
458 struct atomic_notifier_head context_status_notifier;
459
460 #define I915_ENGINE_USING_CMD_PARSER BIT(0)
461 #define I915_ENGINE_SUPPORTS_STATS BIT(1)
462 #define I915_ENGINE_HAS_PREEMPTION BIT(2)
463 #define I915_ENGINE_HAS_SEMAPHORES BIT(3)
464 #define I915_ENGINE_HAS_TIMESLICES BIT(4)
465 #define I915_ENGINE_IS_VIRTUAL BIT(5)
466 #define I915_ENGINE_HAS_RELATIVE_MMIO BIT(6)
467 #define I915_ENGINE_REQUIRES_CMD_PARSER BIT(7)
468 #define I915_ENGINE_WANT_FORCED_PREEMPTION BIT(8)
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 * @active: Number of contexts currently scheduled in.
498 */
499 unsigned int active;
500
501 /**
502 * @lock: Lock protecting the below fields.
503 */
504 seqcount_t lock;
505
506 /**
507 * @total: Total time this engine was busy.
508 *
509 * Accumulated time not counting the most recent block in cases
510 * where engine is currently busy (active > 0).
511 */
512 ktime_t total;
513
514 /**
515 * @start: Timestamp of the last idle to active transition.
516 *
517 * Idle is defined as active == 0, active is active > 0.
518 */
519 ktime_t start;
520
521 /**
522 * @rps: Utilisation at last RPS sampling.
523 */
524 ktime_t rps;
525 } stats;
526
527 struct {
528 unsigned long heartbeat_interval_ms;
529 unsigned long max_busywait_duration_ns;
530 unsigned long preempt_timeout_ms;
531 unsigned long stop_timeout_ms;
532 unsigned long timeslice_duration_ms;
533 } props, defaults;
534
535 I915_SELFTEST_DECLARE(struct fault_attr reset_timeout);
536 };
537
538 static inline bool
intel_engine_using_cmd_parser(const struct intel_engine_cs * engine)539 intel_engine_using_cmd_parser(const struct intel_engine_cs *engine)
540 {
541 return engine->flags & I915_ENGINE_USING_CMD_PARSER;
542 }
543
544 static inline bool
intel_engine_requires_cmd_parser(const struct intel_engine_cs * engine)545 intel_engine_requires_cmd_parser(const struct intel_engine_cs *engine)
546 {
547 return engine->flags & I915_ENGINE_REQUIRES_CMD_PARSER;
548 }
549
550 static inline bool
intel_engine_supports_stats(const struct intel_engine_cs * engine)551 intel_engine_supports_stats(const struct intel_engine_cs *engine)
552 {
553 return engine->flags & I915_ENGINE_SUPPORTS_STATS;
554 }
555
556 static inline bool
intel_engine_has_preemption(const struct intel_engine_cs * engine)557 intel_engine_has_preemption(const struct intel_engine_cs *engine)
558 {
559 return engine->flags & I915_ENGINE_HAS_PREEMPTION;
560 }
561
562 static inline bool
intel_engine_has_semaphores(const struct intel_engine_cs * engine)563 intel_engine_has_semaphores(const struct intel_engine_cs *engine)
564 {
565 return engine->flags & I915_ENGINE_HAS_SEMAPHORES;
566 }
567
568 static inline bool
intel_engine_has_timeslices(const struct intel_engine_cs * engine)569 intel_engine_has_timeslices(const struct intel_engine_cs *engine)
570 {
571 if (!IS_ACTIVE(CONFIG_DRM_I915_TIMESLICE_DURATION))
572 return false;
573
574 return engine->flags & I915_ENGINE_HAS_TIMESLICES;
575 }
576
577 static inline bool
intel_engine_is_virtual(const struct intel_engine_cs * engine)578 intel_engine_is_virtual(const struct intel_engine_cs *engine)
579 {
580 return engine->flags & I915_ENGINE_IS_VIRTUAL;
581 }
582
583 static inline bool
intel_engine_has_relative_mmio(const struct intel_engine_cs * const engine)584 intel_engine_has_relative_mmio(const struct intel_engine_cs * const engine)
585 {
586 return engine->flags & I915_ENGINE_HAS_RELATIVE_MMIO;
587 }
588
589 #define instdone_has_slice(dev_priv___, sseu___, slice___) \
590 ((GRAPHICS_VER(dev_priv___) == 7 ? 1 : ((sseu___)->slice_mask)) & BIT(slice___))
591
592 #define instdone_has_subslice(dev_priv__, sseu__, slice__, subslice__) \
593 (GRAPHICS_VER(dev_priv__) == 7 ? (1 & BIT(subslice__)) : \
594 intel_sseu_has_subslice(sseu__, 0, subslice__))
595
596 #define for_each_instdone_slice_subslice(dev_priv_, sseu_, slice_, subslice_) \
597 for ((slice_) = 0, (subslice_) = 0; (slice_) < I915_MAX_SLICES; \
598 (subslice_) = ((subslice_) + 1) % I915_MAX_SUBSLICES, \
599 (slice_) += ((subslice_) == 0)) \
600 for_each_if((instdone_has_slice(dev_priv_, sseu_, slice_)) && \
601 (instdone_has_subslice(dev_priv_, sseu_, slice_, \
602 subslice_)))
603 #endif /* __INTEL_ENGINE_TYPES_H__ */
604