xref: /linux/drivers/gpu/drm/i915/gt/intel_engine_cs.c (revision 5b0764a53235d3c77e1ed674e836e1b3fe1a4b1f)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2016 Intel Corporation
4  */
5 
6 #include <linux/error-injection.h>
7 #include <linux/string_helpers.h>
8 
9 #include <drm/drm_print.h>
10 
11 #include "gem/i915_gem_context.h"
12 #include "gem/i915_gem_internal.h"
13 #include "gt/intel_gt_print.h"
14 #include "gt/intel_gt_regs.h"
15 
16 #include "i915_cmd_parser.h"
17 #include "i915_drv.h"
18 #include "i915_irq.h"
19 #include "i915_reg.h"
20 #include "intel_breadcrumbs.h"
21 #include "intel_context.h"
22 #include "intel_engine.h"
23 #include "intel_engine_pm.h"
24 #include "intel_engine_regs.h"
25 #include "intel_engine_user.h"
26 #include "intel_execlists_submission.h"
27 #include "intel_gt.h"
28 #include "intel_gt_mcr.h"
29 #include "intel_gt_pm.h"
30 #include "intel_gt_requests.h"
31 #include "intel_lrc.h"
32 #include "intel_lrc_reg.h"
33 #include "intel_reset.h"
34 #include "intel_ring.h"
35 #include "uc/intel_guc_submission.h"
36 
37 /* Haswell does have the CXT_SIZE register however it does not appear to be
38  * valid. Now, docs explain in dwords what is in the context object. The full
39  * size is 70720 bytes, however, the power context and execlist context will
40  * never be saved (power context is stored elsewhere, and execlists don't work
41  * on HSW) - so the final size, including the extra state required for the
42  * Resource Streamer, is 66944 bytes, which rounds to 17 pages.
43  */
44 #define HSW_CXT_TOTAL_SIZE		(17 * PAGE_SIZE)
45 
46 #define DEFAULT_LR_CONTEXT_RENDER_SIZE	(22 * PAGE_SIZE)
47 #define GEN8_LR_CONTEXT_RENDER_SIZE	(20 * PAGE_SIZE)
48 #define GEN9_LR_CONTEXT_RENDER_SIZE	(22 * PAGE_SIZE)
49 #define GEN11_LR_CONTEXT_RENDER_SIZE	(14 * PAGE_SIZE)
50 
51 #define GEN8_LR_CONTEXT_OTHER_SIZE	(2 * PAGE_SIZE)
52 
53 #define MAX_MMIO_BASES 3
54 struct engine_info {
55 	u8 class;
56 	u8 instance;
57 	/* mmio bases table *must* be sorted in reverse graphics_ver order */
58 	struct engine_mmio_base {
59 		u32 graphics_ver : 8;
60 		u32 base : 24;
61 	} mmio_bases[MAX_MMIO_BASES];
62 };
63 
64 static const struct engine_info intel_engines[] = {
65 	[RCS0] = {
66 		.class = RENDER_CLASS,
67 		.instance = 0,
68 		.mmio_bases = {
69 			{ .graphics_ver = 1, .base = RENDER_RING_BASE }
70 		},
71 	},
72 	[BCS0] = {
73 		.class = COPY_ENGINE_CLASS,
74 		.instance = 0,
75 		.mmio_bases = {
76 			{ .graphics_ver = 6, .base = BLT_RING_BASE }
77 		},
78 	},
79 	[BCS1] = {
80 		.class = COPY_ENGINE_CLASS,
81 		.instance = 1,
82 		.mmio_bases = {
83 			{ .graphics_ver = 12, .base = XEHPC_BCS1_RING_BASE }
84 		},
85 	},
86 	[BCS2] = {
87 		.class = COPY_ENGINE_CLASS,
88 		.instance = 2,
89 		.mmio_bases = {
90 			{ .graphics_ver = 12, .base = XEHPC_BCS2_RING_BASE }
91 		},
92 	},
93 	[BCS3] = {
94 		.class = COPY_ENGINE_CLASS,
95 		.instance = 3,
96 		.mmio_bases = {
97 			{ .graphics_ver = 12, .base = XEHPC_BCS3_RING_BASE }
98 		},
99 	},
100 	[BCS4] = {
101 		.class = COPY_ENGINE_CLASS,
102 		.instance = 4,
103 		.mmio_bases = {
104 			{ .graphics_ver = 12, .base = XEHPC_BCS4_RING_BASE }
105 		},
106 	},
107 	[BCS5] = {
108 		.class = COPY_ENGINE_CLASS,
109 		.instance = 5,
110 		.mmio_bases = {
111 			{ .graphics_ver = 12, .base = XEHPC_BCS5_RING_BASE }
112 		},
113 	},
114 	[BCS6] = {
115 		.class = COPY_ENGINE_CLASS,
116 		.instance = 6,
117 		.mmio_bases = {
118 			{ .graphics_ver = 12, .base = XEHPC_BCS6_RING_BASE }
119 		},
120 	},
121 	[BCS7] = {
122 		.class = COPY_ENGINE_CLASS,
123 		.instance = 7,
124 		.mmio_bases = {
125 			{ .graphics_ver = 12, .base = XEHPC_BCS7_RING_BASE }
126 		},
127 	},
128 	[BCS8] = {
129 		.class = COPY_ENGINE_CLASS,
130 		.instance = 8,
131 		.mmio_bases = {
132 			{ .graphics_ver = 12, .base = XEHPC_BCS8_RING_BASE }
133 		},
134 	},
135 	[VCS0] = {
136 		.class = VIDEO_DECODE_CLASS,
137 		.instance = 0,
138 		.mmio_bases = {
139 			{ .graphics_ver = 11, .base = GEN11_BSD_RING_BASE },
140 			{ .graphics_ver = 6, .base = GEN6_BSD_RING_BASE },
141 			{ .graphics_ver = 4, .base = BSD_RING_BASE }
142 		},
143 	},
144 	[VCS1] = {
145 		.class = VIDEO_DECODE_CLASS,
146 		.instance = 1,
147 		.mmio_bases = {
148 			{ .graphics_ver = 11, .base = GEN11_BSD2_RING_BASE },
149 			{ .graphics_ver = 8, .base = GEN8_BSD2_RING_BASE }
150 		},
151 	},
152 	[VCS2] = {
153 		.class = VIDEO_DECODE_CLASS,
154 		.instance = 2,
155 		.mmio_bases = {
156 			{ .graphics_ver = 11, .base = GEN11_BSD3_RING_BASE }
157 		},
158 	},
159 	[VCS3] = {
160 		.class = VIDEO_DECODE_CLASS,
161 		.instance = 3,
162 		.mmio_bases = {
163 			{ .graphics_ver = 11, .base = GEN11_BSD4_RING_BASE }
164 		},
165 	},
166 	[VCS4] = {
167 		.class = VIDEO_DECODE_CLASS,
168 		.instance = 4,
169 		.mmio_bases = {
170 			{ .graphics_ver = 12, .base = XEHP_BSD5_RING_BASE }
171 		},
172 	},
173 	[VCS5] = {
174 		.class = VIDEO_DECODE_CLASS,
175 		.instance = 5,
176 		.mmio_bases = {
177 			{ .graphics_ver = 12, .base = XEHP_BSD6_RING_BASE }
178 		},
179 	},
180 	[VCS6] = {
181 		.class = VIDEO_DECODE_CLASS,
182 		.instance = 6,
183 		.mmio_bases = {
184 			{ .graphics_ver = 12, .base = XEHP_BSD7_RING_BASE }
185 		},
186 	},
187 	[VCS7] = {
188 		.class = VIDEO_DECODE_CLASS,
189 		.instance = 7,
190 		.mmio_bases = {
191 			{ .graphics_ver = 12, .base = XEHP_BSD8_RING_BASE }
192 		},
193 	},
194 	[VECS0] = {
195 		.class = VIDEO_ENHANCEMENT_CLASS,
196 		.instance = 0,
197 		.mmio_bases = {
198 			{ .graphics_ver = 11, .base = GEN11_VEBOX_RING_BASE },
199 			{ .graphics_ver = 7, .base = VEBOX_RING_BASE }
200 		},
201 	},
202 	[VECS1] = {
203 		.class = VIDEO_ENHANCEMENT_CLASS,
204 		.instance = 1,
205 		.mmio_bases = {
206 			{ .graphics_ver = 11, .base = GEN11_VEBOX2_RING_BASE }
207 		},
208 	},
209 	[VECS2] = {
210 		.class = VIDEO_ENHANCEMENT_CLASS,
211 		.instance = 2,
212 		.mmio_bases = {
213 			{ .graphics_ver = 12, .base = XEHP_VEBOX3_RING_BASE }
214 		},
215 	},
216 	[VECS3] = {
217 		.class = VIDEO_ENHANCEMENT_CLASS,
218 		.instance = 3,
219 		.mmio_bases = {
220 			{ .graphics_ver = 12, .base = XEHP_VEBOX4_RING_BASE }
221 		},
222 	},
223 	[CCS0] = {
224 		.class = COMPUTE_CLASS,
225 		.instance = 0,
226 		.mmio_bases = {
227 			{ .graphics_ver = 12, .base = GEN12_COMPUTE0_RING_BASE }
228 		}
229 	},
230 	[CCS1] = {
231 		.class = COMPUTE_CLASS,
232 		.instance = 1,
233 		.mmio_bases = {
234 			{ .graphics_ver = 12, .base = GEN12_COMPUTE1_RING_BASE }
235 		}
236 	},
237 	[CCS2] = {
238 		.class = COMPUTE_CLASS,
239 		.instance = 2,
240 		.mmio_bases = {
241 			{ .graphics_ver = 12, .base = GEN12_COMPUTE2_RING_BASE }
242 		}
243 	},
244 	[CCS3] = {
245 		.class = COMPUTE_CLASS,
246 		.instance = 3,
247 		.mmio_bases = {
248 			{ .graphics_ver = 12, .base = GEN12_COMPUTE3_RING_BASE }
249 		}
250 	},
251 	[GSC0] = {
252 		.class = OTHER_CLASS,
253 		.instance = OTHER_GSC_INSTANCE,
254 		.mmio_bases = {
255 			{ .graphics_ver = 12, .base = MTL_GSC_RING_BASE }
256 		}
257 	},
258 };
259 
260 /**
261  * intel_engine_context_size() - return the size of the context for an engine
262  * @gt: the gt
263  * @class: engine class
264  *
265  * Each engine class may require a different amount of space for a context
266  * image.
267  *
268  * Return: size (in bytes) of an engine class specific context image
269  *
270  * Note: this size includes the HWSP, which is part of the context image
271  * in LRC mode, but does not include the "shared data page" used with
272  * GuC submission. The caller should account for this if using the GuC.
273  */
274 u32 intel_engine_context_size(struct intel_gt *gt, u8 class)
275 {
276 	struct intel_uncore *uncore = gt->uncore;
277 	u32 cxt_size;
278 
279 	BUILD_BUG_ON(I915_GTT_PAGE_SIZE != PAGE_SIZE);
280 
281 	switch (class) {
282 	case COMPUTE_CLASS:
283 		fallthrough;
284 	case RENDER_CLASS:
285 		switch (GRAPHICS_VER(gt->i915)) {
286 		default:
287 			MISSING_CASE(GRAPHICS_VER(gt->i915));
288 			return DEFAULT_LR_CONTEXT_RENDER_SIZE;
289 		case 12:
290 		case 11:
291 			return GEN11_LR_CONTEXT_RENDER_SIZE;
292 		case 9:
293 			return GEN9_LR_CONTEXT_RENDER_SIZE;
294 		case 8:
295 			return GEN8_LR_CONTEXT_RENDER_SIZE;
296 		case 7:
297 			if (IS_HASWELL(gt->i915))
298 				return HSW_CXT_TOTAL_SIZE;
299 
300 			cxt_size = intel_uncore_read(uncore, GEN7_CXT_SIZE);
301 			return round_up(GEN7_CXT_TOTAL_SIZE(cxt_size) * 64,
302 					PAGE_SIZE);
303 		case 6:
304 			cxt_size = intel_uncore_read(uncore, CXT_SIZE);
305 			return round_up(GEN6_CXT_TOTAL_SIZE(cxt_size) * 64,
306 					PAGE_SIZE);
307 		case 5:
308 		case 4:
309 			/*
310 			 * There is a discrepancy here between the size reported
311 			 * by the register and the size of the context layout
312 			 * in the docs. Both are described as authoritative!
313 			 *
314 			 * The discrepancy is on the order of a few cachelines,
315 			 * but the total is under one page (4k), which is our
316 			 * minimum allocation anyway so it should all come
317 			 * out in the wash.
318 			 */
319 			cxt_size = intel_uncore_read(uncore, CXT_SIZE) + 1;
320 			gt_dbg(gt, "graphics_ver = %d CXT_SIZE = %d bytes [0x%08x]\n",
321 			       GRAPHICS_VER(gt->i915), cxt_size * 64,
322 			       cxt_size - 1);
323 			return round_up(cxt_size * 64, PAGE_SIZE);
324 		case 3:
325 		case 2:
326 		/* For the special day when i810 gets merged. */
327 		case 1:
328 			return 0;
329 		}
330 		break;
331 	default:
332 		MISSING_CASE(class);
333 		fallthrough;
334 	case VIDEO_DECODE_CLASS:
335 	case VIDEO_ENHANCEMENT_CLASS:
336 	case COPY_ENGINE_CLASS:
337 	case OTHER_CLASS:
338 		if (GRAPHICS_VER(gt->i915) < 8)
339 			return 0;
340 		return GEN8_LR_CONTEXT_OTHER_SIZE;
341 	}
342 }
343 
344 static u32 __engine_mmio_base(struct drm_i915_private *i915,
345 			      const struct engine_mmio_base *bases)
346 {
347 	int i;
348 
349 	for (i = 0; i < MAX_MMIO_BASES; i++)
350 		if (GRAPHICS_VER(i915) >= bases[i].graphics_ver)
351 			break;
352 
353 	GEM_BUG_ON(i == MAX_MMIO_BASES);
354 	GEM_BUG_ON(!bases[i].base);
355 
356 	return bases[i].base;
357 }
358 
359 static void __sprint_engine_name(struct intel_engine_cs *engine)
360 {
361 	/*
362 	 * Before we know what the uABI name for this engine will be,
363 	 * we still would like to keep track of this engine in the debug logs.
364 	 * We throw in a ' here as a reminder that this isn't its final name.
365 	 */
366 	GEM_WARN_ON(snprintf(engine->name, sizeof(engine->name), "%s'%u",
367 			     intel_engine_class_repr(engine->class),
368 			     engine->instance) >= sizeof(engine->name));
369 }
370 
371 void intel_engine_set_hwsp_writemask(struct intel_engine_cs *engine, u32 mask)
372 {
373 	/*
374 	 * Though they added more rings on g4x/ilk, they did not add
375 	 * per-engine HWSTAM until gen6.
376 	 */
377 	if (GRAPHICS_VER(engine->i915) < 6 && engine->class != RENDER_CLASS)
378 		return;
379 
380 	if (GRAPHICS_VER(engine->i915) >= 3)
381 		ENGINE_WRITE(engine, RING_HWSTAM, mask);
382 	else
383 		ENGINE_WRITE16(engine, RING_HWSTAM, mask);
384 }
385 
386 static void intel_engine_sanitize_mmio(struct intel_engine_cs *engine)
387 {
388 	/* Mask off all writes into the unknown HWSP */
389 	intel_engine_set_hwsp_writemask(engine, ~0u);
390 }
391 
392 static void nop_irq_handler(struct intel_engine_cs *engine, u16 iir)
393 {
394 	GEM_DEBUG_WARN_ON(iir);
395 }
396 
397 static u32 get_reset_domain(u8 ver, enum intel_engine_id id)
398 {
399 	u32 reset_domain;
400 
401 	if (ver >= 11) {
402 		static const u32 engine_reset_domains[] = {
403 			[RCS0]  = GEN11_GRDOM_RENDER,
404 			[BCS0]  = GEN11_GRDOM_BLT,
405 			[BCS1]  = XEHPC_GRDOM_BLT1,
406 			[BCS2]  = XEHPC_GRDOM_BLT2,
407 			[BCS3]  = XEHPC_GRDOM_BLT3,
408 			[BCS4]  = XEHPC_GRDOM_BLT4,
409 			[BCS5]  = XEHPC_GRDOM_BLT5,
410 			[BCS6]  = XEHPC_GRDOM_BLT6,
411 			[BCS7]  = XEHPC_GRDOM_BLT7,
412 			[BCS8]  = XEHPC_GRDOM_BLT8,
413 			[VCS0]  = GEN11_GRDOM_MEDIA,
414 			[VCS1]  = GEN11_GRDOM_MEDIA2,
415 			[VCS2]  = GEN11_GRDOM_MEDIA3,
416 			[VCS3]  = GEN11_GRDOM_MEDIA4,
417 			[VCS4]  = GEN11_GRDOM_MEDIA5,
418 			[VCS5]  = GEN11_GRDOM_MEDIA6,
419 			[VCS6]  = GEN11_GRDOM_MEDIA7,
420 			[VCS7]  = GEN11_GRDOM_MEDIA8,
421 			[VECS0] = GEN11_GRDOM_VECS,
422 			[VECS1] = GEN11_GRDOM_VECS2,
423 			[VECS2] = GEN11_GRDOM_VECS3,
424 			[VECS3] = GEN11_GRDOM_VECS4,
425 			[CCS0]  = GEN11_GRDOM_RENDER,
426 			[CCS1]  = GEN11_GRDOM_RENDER,
427 			[CCS2]  = GEN11_GRDOM_RENDER,
428 			[CCS3]  = GEN11_GRDOM_RENDER,
429 			[GSC0]  = GEN12_GRDOM_GSC,
430 		};
431 		GEM_BUG_ON(id >= ARRAY_SIZE(engine_reset_domains) ||
432 			   !engine_reset_domains[id]);
433 		reset_domain = engine_reset_domains[id];
434 	} else {
435 		static const u32 engine_reset_domains[] = {
436 			[RCS0]  = GEN6_GRDOM_RENDER,
437 			[BCS0]  = GEN6_GRDOM_BLT,
438 			[VCS0]  = GEN6_GRDOM_MEDIA,
439 			[VCS1]  = GEN8_GRDOM_MEDIA2,
440 			[VECS0] = GEN6_GRDOM_VECS,
441 		};
442 		GEM_BUG_ON(id >= ARRAY_SIZE(engine_reset_domains) ||
443 			   !engine_reset_domains[id]);
444 		reset_domain = engine_reset_domains[id];
445 	}
446 
447 	return reset_domain;
448 }
449 
450 static int intel_engine_setup(struct intel_gt *gt, enum intel_engine_id id,
451 			      u8 logical_instance)
452 {
453 	const struct engine_info *info = &intel_engines[id];
454 	struct drm_i915_private *i915 = gt->i915;
455 	struct intel_engine_cs *engine;
456 	u8 guc_class;
457 
458 	BUILD_BUG_ON(MAX_ENGINE_CLASS >= BIT(GEN11_ENGINE_CLASS_WIDTH));
459 	BUILD_BUG_ON(MAX_ENGINE_INSTANCE >= BIT(GEN11_ENGINE_INSTANCE_WIDTH));
460 	BUILD_BUG_ON(I915_MAX_VCS > (MAX_ENGINE_INSTANCE + 1));
461 	BUILD_BUG_ON(I915_MAX_VECS > (MAX_ENGINE_INSTANCE + 1));
462 
463 	if (GEM_DEBUG_WARN_ON(id >= ARRAY_SIZE(gt->engine)))
464 		return -EINVAL;
465 
466 	if (GEM_DEBUG_WARN_ON(info->class > MAX_ENGINE_CLASS))
467 		return -EINVAL;
468 
469 	if (GEM_DEBUG_WARN_ON(info->instance > MAX_ENGINE_INSTANCE))
470 		return -EINVAL;
471 
472 	if (GEM_DEBUG_WARN_ON(gt->engine_class[info->class][info->instance]))
473 		return -EINVAL;
474 
475 	engine = kzalloc_obj(*engine);
476 	if (!engine)
477 		return -ENOMEM;
478 
479 	BUILD_BUG_ON(BITS_PER_TYPE(engine->mask) < I915_NUM_ENGINES);
480 
481 	INIT_LIST_HEAD(&engine->pinned_contexts_list);
482 	engine->id = id;
483 	engine->legacy_idx = INVALID_ENGINE;
484 	engine->mask = BIT(id);
485 	engine->reset_domain = get_reset_domain(GRAPHICS_VER(gt->i915),
486 						id);
487 	engine->i915 = i915;
488 	engine->gt = gt;
489 	engine->uncore = gt->uncore;
490 	guc_class = engine_class_to_guc_class(info->class);
491 	engine->guc_id = MAKE_GUC_ID(guc_class, info->instance);
492 	engine->mmio_base = __engine_mmio_base(i915, info->mmio_bases);
493 
494 	engine->irq_handler = nop_irq_handler;
495 
496 	engine->class = info->class;
497 	engine->instance = info->instance;
498 	engine->logical_mask = BIT(logical_instance);
499 	__sprint_engine_name(engine);
500 
501 	if ((engine->class == COMPUTE_CLASS || engine->class == RENDER_CLASS) &&
502 	    __ffs(CCS_MASK(engine->gt) | RCS_MASK(engine->gt)) == engine->instance)
503 		engine->flags |= I915_ENGINE_FIRST_RENDER_COMPUTE;
504 
505 	/* features common between engines sharing EUs */
506 	if (engine->class == RENDER_CLASS || engine->class == COMPUTE_CLASS) {
507 		engine->flags |= I915_ENGINE_HAS_RCS_REG_STATE;
508 		engine->flags |= I915_ENGINE_HAS_EU_PRIORITY;
509 	}
510 
511 	engine->props.heartbeat_interval_ms =
512 		CONFIG_DRM_I915_HEARTBEAT_INTERVAL;
513 	engine->props.max_busywait_duration_ns =
514 		CONFIG_DRM_I915_MAX_REQUEST_BUSYWAIT;
515 	engine->props.preempt_timeout_ms =
516 		CONFIG_DRM_I915_PREEMPT_TIMEOUT;
517 	engine->props.stop_timeout_ms =
518 		CONFIG_DRM_I915_STOP_TIMEOUT;
519 	engine->props.timeslice_duration_ms =
520 		CONFIG_DRM_I915_TIMESLICE_DURATION;
521 
522 	/*
523 	 * Mid-thread pre-emption is not available in Gen12. Unfortunately,
524 	 * some compute workloads run quite long threads. That means they get
525 	 * reset due to not pre-empting in a timely manner. So, bump the
526 	 * pre-emption timeout value to be much higher for compute engines.
527 	 */
528 	if (GRAPHICS_VER(i915) == 12 && (engine->flags & I915_ENGINE_HAS_RCS_REG_STATE))
529 		engine->props.preempt_timeout_ms = CONFIG_DRM_I915_PREEMPT_TIMEOUT_COMPUTE;
530 
531 	/* Cap properties according to any system limits */
532 #define CLAMP_PROP(field) \
533 	do { \
534 		u64 clamp = intel_clamp_##field(engine, engine->props.field); \
535 		if (clamp != engine->props.field) { \
536 			drm_notice(&engine->i915->drm, \
537 				   "Warning, clamping %s to %lld to prevent overflow\n", \
538 				   #field, clamp); \
539 			engine->props.field = clamp; \
540 		} \
541 	} while (0)
542 
543 	CLAMP_PROP(heartbeat_interval_ms);
544 	CLAMP_PROP(max_busywait_duration_ns);
545 	CLAMP_PROP(preempt_timeout_ms);
546 	CLAMP_PROP(stop_timeout_ms);
547 	CLAMP_PROP(timeslice_duration_ms);
548 
549 #undef CLAMP_PROP
550 
551 	engine->defaults = engine->props; /* never to change again */
552 
553 	engine->context_size = intel_engine_context_size(gt, engine->class);
554 	if (WARN_ON(engine->context_size > BIT(20)))
555 		engine->context_size = 0;
556 	if (engine->context_size)
557 		DRIVER_CAPS(i915)->has_logical_contexts = true;
558 
559 	ewma__engine_latency_init(&engine->latency);
560 
561 	ATOMIC_INIT_NOTIFIER_HEAD(&engine->context_status_notifier);
562 
563 	/* Scrub mmio state on takeover */
564 	intel_engine_sanitize_mmio(engine);
565 
566 	gt->engine_class[info->class][info->instance] = engine;
567 	gt->engine[id] = engine;
568 
569 	return 0;
570 }
571 
572 u64 intel_clamp_heartbeat_interval_ms(struct intel_engine_cs *engine, u64 value)
573 {
574 	value = min_t(u64, value, jiffies_to_msecs(MAX_SCHEDULE_TIMEOUT));
575 
576 	return value;
577 }
578 
579 u64 intel_clamp_max_busywait_duration_ns(struct intel_engine_cs *engine, u64 value)
580 {
581 	value = min(value, jiffies_to_nsecs(2));
582 
583 	return value;
584 }
585 
586 u64 intel_clamp_preempt_timeout_ms(struct intel_engine_cs *engine, u64 value)
587 {
588 	/*
589 	 * NB: The GuC API only supports 32bit values. However, the limit is further
590 	 * reduced due to internal calculations which would otherwise overflow.
591 	 */
592 	if (intel_guc_submission_is_wanted(gt_to_guc(engine->gt)))
593 		value = min_t(u64, value, guc_policy_max_preempt_timeout_ms());
594 
595 	value = min_t(u64, value, jiffies_to_msecs(MAX_SCHEDULE_TIMEOUT));
596 
597 	return value;
598 }
599 
600 u64 intel_clamp_stop_timeout_ms(struct intel_engine_cs *engine, u64 value)
601 {
602 	value = min_t(u64, value, jiffies_to_msecs(MAX_SCHEDULE_TIMEOUT));
603 
604 	return value;
605 }
606 
607 u64 intel_clamp_timeslice_duration_ms(struct intel_engine_cs *engine, u64 value)
608 {
609 	/*
610 	 * NB: The GuC API only supports 32bit values. However, the limit is further
611 	 * reduced due to internal calculations which would otherwise overflow.
612 	 */
613 	if (intel_guc_submission_is_wanted(gt_to_guc(engine->gt)))
614 		value = min_t(u64, value, guc_policy_max_exec_quantum_ms());
615 
616 	value = min_t(u64, value, jiffies_to_msecs(MAX_SCHEDULE_TIMEOUT));
617 
618 	return value;
619 }
620 
621 static void __setup_engine_capabilities(struct intel_engine_cs *engine)
622 {
623 	struct drm_i915_private *i915 = engine->i915;
624 
625 	if (engine->class == VIDEO_DECODE_CLASS) {
626 		/*
627 		 * HEVC support is present on first engine instance
628 		 * before Gen11 and on all instances afterwards.
629 		 */
630 		if (GRAPHICS_VER(i915) >= 11 ||
631 		    (GRAPHICS_VER(i915) >= 9 && engine->instance == 0))
632 			engine->uabi_capabilities |=
633 				I915_VIDEO_CLASS_CAPABILITY_HEVC;
634 
635 		/*
636 		 * SFC block is present only on even logical engine
637 		 * instances.
638 		 */
639 		if ((GRAPHICS_VER(i915) >= 11 &&
640 		     (engine->gt->info.vdbox_sfc_access &
641 		      BIT(engine->instance))) ||
642 		    (GRAPHICS_VER(i915) >= 9 && engine->instance == 0))
643 			engine->uabi_capabilities |=
644 				I915_VIDEO_AND_ENHANCE_CLASS_CAPABILITY_SFC;
645 	} else if (engine->class == VIDEO_ENHANCEMENT_CLASS) {
646 		if (GRAPHICS_VER(i915) >= 9 &&
647 		    engine->gt->info.sfc_mask & BIT(engine->instance))
648 			engine->uabi_capabilities |=
649 				I915_VIDEO_AND_ENHANCE_CLASS_CAPABILITY_SFC;
650 	}
651 }
652 
653 static void intel_setup_engine_capabilities(struct intel_gt *gt)
654 {
655 	struct intel_engine_cs *engine;
656 	enum intel_engine_id id;
657 
658 	for_each_engine(engine, gt, id)
659 		__setup_engine_capabilities(engine);
660 }
661 
662 /**
663  * intel_engines_release() - free the resources allocated for Command Streamers
664  * @gt: pointer to struct intel_gt
665  */
666 void intel_engines_release(struct intel_gt *gt)
667 {
668 	struct intel_engine_cs *engine;
669 	enum intel_engine_id id;
670 
671 	/*
672 	 * Before we release the resources held by engine, we must be certain
673 	 * that the HW is no longer accessing them -- having the GPU scribble
674 	 * to or read from a page being used for something else causes no end
675 	 * of fun.
676 	 *
677 	 * The GPU should be reset by this point, but assume the worst just
678 	 * in case we aborted before completely initialising the engines.
679 	 */
680 	GEM_BUG_ON(intel_gt_pm_is_awake(gt));
681 	if (!intel_gt_gpu_reset_clobbers_display(gt))
682 		intel_gt_reset_all_engines(gt);
683 
684 	/* Decouple the backend; but keep the layout for late GPU resets */
685 	for_each_engine(engine, gt, id) {
686 		if (!engine->release)
687 			continue;
688 
689 		intel_wakeref_wait_for_idle(&engine->wakeref);
690 		GEM_BUG_ON(intel_engine_pm_is_awake(engine));
691 
692 		engine->release(engine);
693 		engine->release = NULL;
694 
695 		memset(&engine->reset, 0, sizeof(engine->reset));
696 	}
697 
698 	llist_del_all(&gt->i915->uabi_engines_llist);
699 }
700 
701 void intel_engine_free_request_pool(struct intel_engine_cs *engine)
702 {
703 	if (!engine->request_pool)
704 		return;
705 
706 	kmem_cache_free(i915_request_slab_cache(), engine->request_pool);
707 }
708 
709 void intel_engines_free(struct intel_gt *gt)
710 {
711 	struct intel_engine_cs *engine;
712 	enum intel_engine_id id;
713 
714 	/* Free the requests! dma-resv keeps fences around for an eternity */
715 	rcu_barrier();
716 
717 	for_each_engine(engine, gt, id) {
718 		intel_engine_free_request_pool(engine);
719 		kfree(engine);
720 		gt->engine[id] = NULL;
721 	}
722 }
723 
724 static
725 bool gen11_vdbox_has_sfc(struct intel_gt *gt,
726 			 unsigned int physical_vdbox,
727 			 unsigned int logical_vdbox, u16 vdbox_mask)
728 {
729 	struct drm_i915_private *i915 = gt->i915;
730 
731 	/*
732 	 * In Gen11, only even numbered logical VDBOXes are hooked
733 	 * up to an SFC (Scaler & Format Converter) unit.
734 	 * In Gen12, Even numbered physical instance always are connected
735 	 * to an SFC. Odd numbered physical instances have SFC only if
736 	 * previous even instance is fused off.
737 	 *
738 	 * Starting with Xe_HP, there's also a dedicated SFC_ENABLE field
739 	 * in the fuse register that tells us whether a specific SFC is present.
740 	 */
741 	if ((gt->info.sfc_mask & BIT(physical_vdbox / 2)) == 0)
742 		return false;
743 	else if (MEDIA_VER(i915) >= 12)
744 		return (physical_vdbox % 2 == 0) ||
745 			!(BIT(physical_vdbox - 1) & vdbox_mask);
746 	else if (MEDIA_VER(i915) == 11)
747 		return logical_vdbox % 2 == 0;
748 
749 	return false;
750 }
751 
752 static void engine_mask_apply_media_fuses(struct intel_gt *gt)
753 {
754 	struct drm_i915_private *i915 = gt->i915;
755 	unsigned int logical_vdbox = 0;
756 	unsigned int i;
757 	u32 media_fuse, fuse1;
758 	u16 vdbox_mask;
759 	u16 vebox_mask;
760 
761 	if (MEDIA_VER(gt->i915) < 11)
762 		return;
763 
764 	/*
765 	 * On newer platforms the fusing register is called 'enable' and has
766 	 * enable semantics, while on older platforms it is called 'disable'
767 	 * and bits have disable semantices.
768 	 */
769 	media_fuse = intel_uncore_read(gt->uncore, GEN11_GT_VEBOX_VDBOX_DISABLE);
770 	if (MEDIA_VER_FULL(i915) < IP_VER(12, 55))
771 		media_fuse = ~media_fuse;
772 
773 	vdbox_mask = REG_FIELD_GET(GEN11_GT_VDBOX_DISABLE_MASK, media_fuse);
774 	vebox_mask = REG_FIELD_GET(GEN11_GT_VEBOX_DISABLE_MASK, media_fuse);
775 
776 	if (MEDIA_VER_FULL(i915) >= IP_VER(12, 55)) {
777 		fuse1 = intel_uncore_read(gt->uncore, HSW_PAVP_FUSE1);
778 		gt->info.sfc_mask = REG_FIELD_GET(XEHP_SFC_ENABLE_MASK, fuse1);
779 	} else {
780 		gt->info.sfc_mask = ~0;
781 	}
782 
783 	for (i = 0; i < I915_MAX_VCS; i++) {
784 		if (!HAS_ENGINE(gt, _VCS(i))) {
785 			vdbox_mask &= ~BIT(i);
786 			continue;
787 		}
788 
789 		if (!(BIT(i) & vdbox_mask)) {
790 			gt->info.engine_mask &= ~BIT(_VCS(i));
791 			gt_dbg(gt, "vcs%u fused off\n", i);
792 			continue;
793 		}
794 
795 		if (gen11_vdbox_has_sfc(gt, i, logical_vdbox, vdbox_mask))
796 			gt->info.vdbox_sfc_access |= BIT(i);
797 		logical_vdbox++;
798 	}
799 	gt_dbg(gt, "vdbox enable: %04x, instances: %04lx\n", vdbox_mask, VDBOX_MASK(gt));
800 	GEM_BUG_ON(vdbox_mask != VDBOX_MASK(gt));
801 
802 	for (i = 0; i < I915_MAX_VECS; i++) {
803 		if (!HAS_ENGINE(gt, _VECS(i))) {
804 			vebox_mask &= ~BIT(i);
805 			continue;
806 		}
807 
808 		if (!(BIT(i) & vebox_mask)) {
809 			gt->info.engine_mask &= ~BIT(_VECS(i));
810 			gt_dbg(gt, "vecs%u fused off\n", i);
811 		}
812 	}
813 	gt_dbg(gt, "vebox enable: %04x, instances: %04lx\n", vebox_mask, VEBOX_MASK(gt));
814 	GEM_BUG_ON(vebox_mask != VEBOX_MASK(gt));
815 }
816 
817 static void engine_mask_apply_compute_fuses(struct intel_gt *gt)
818 {
819 	struct drm_i915_private *i915 = gt->i915;
820 	struct intel_gt_info *info = &gt->info;
821 	int ss_per_ccs = info->sseu.max_subslices / I915_MAX_CCS;
822 	unsigned long ccs_mask;
823 	unsigned int i;
824 
825 	if (GRAPHICS_VER(i915) < 11)
826 		return;
827 
828 	if (hweight32(CCS_MASK(gt)) <= 1)
829 		return;
830 
831 	ccs_mask = intel_slicemask_from_xehp_dssmask(info->sseu.compute_subslice_mask,
832 						     ss_per_ccs);
833 	/*
834 	 * If all DSS in a quadrant are fused off, the corresponding CCS
835 	 * engine is not available for use.
836 	 */
837 	for_each_clear_bit(i, &ccs_mask, I915_MAX_CCS) {
838 		info->engine_mask &= ~BIT(_CCS(i));
839 		gt_dbg(gt, "ccs%u fused off\n", i);
840 	}
841 }
842 
843 /*
844  * Determine which engines are fused off in our particular hardware.
845  * Note that we have a catch-22 situation where we need to be able to access
846  * the blitter forcewake domain to read the engine fuses, but at the same time
847  * we need to know which engines are available on the system to know which
848  * forcewake domains are present. We solve this by initializing the forcewake
849  * domains based on the full engine mask in the platform capabilities before
850  * calling this function and pruning the domains for fused-off engines
851  * afterwards.
852  */
853 static intel_engine_mask_t init_engine_mask(struct intel_gt *gt)
854 {
855 	struct intel_gt_info *info = &gt->info;
856 
857 	GEM_BUG_ON(!info->engine_mask);
858 
859 	engine_mask_apply_media_fuses(gt);
860 	engine_mask_apply_compute_fuses(gt);
861 
862 	/*
863 	 * The only use of the GSC CS is to load and communicate with the GSC
864 	 * FW, so we have no use for it if we don't have the FW.
865 	 *
866 	 * IMPORTANT: in cases where we don't have the GSC FW, we have a
867 	 * catch-22 situation that breaks media C6 due to 2 requirements:
868 	 * 1) once turned on, the GSC power well will not go to sleep unless the
869 	 *    GSC FW is loaded.
870 	 * 2) to enable idling (which is required for media C6) we need to
871 	 *    initialize the IDLE_MSG register for the GSC CS and do at least 1
872 	 *    submission, which will wake up the GSC power well.
873 	 */
874 	if (__HAS_ENGINE(info->engine_mask, GSC0) && !intel_uc_wants_gsc_uc(&gt->uc)) {
875 		gt_notice(gt, "No GSC FW selected, disabling GSC CS and media C6\n");
876 		info->engine_mask &= ~BIT(GSC0);
877 	}
878 
879 	/*
880 	 * Do not create the command streamer for CCS slices beyond the first.
881 	 * All the workload submitted to the first engine will be shared among
882 	 * all the slices.
883 	 *
884 	 * Once the user will be allowed to customize the CCS mode, then this
885 	 * check needs to be removed.
886 	 */
887 	if (IS_DG2(gt->i915)) {
888 		u8 first_ccs = __ffs(CCS_MASK(gt));
889 
890 		/*
891 		 * Store the number of active cslices before
892 		 * changing the CCS engine configuration
893 		 */
894 		gt->ccs.cslices = CCS_MASK(gt);
895 
896 		/* Mask off all the CCS engine */
897 		info->engine_mask &= ~GENMASK(CCS3, CCS0);
898 		/* Put back in the first CCS engine */
899 		info->engine_mask |= BIT(_CCS(first_ccs));
900 	}
901 
902 	return info->engine_mask;
903 }
904 
905 static void populate_logical_ids(struct intel_gt *gt, u8 *logical_ids,
906 				 u8 class, const u8 *map, u8 num_instances)
907 {
908 	int i, j;
909 	u8 current_logical_id = 0;
910 
911 	for (j = 0; j < num_instances; ++j) {
912 		for (i = 0; i < ARRAY_SIZE(intel_engines); ++i) {
913 			if (!HAS_ENGINE(gt, i) ||
914 			    intel_engines[i].class != class)
915 				continue;
916 
917 			if (intel_engines[i].instance == map[j]) {
918 				logical_ids[intel_engines[i].instance] =
919 					current_logical_id++;
920 				break;
921 			}
922 		}
923 	}
924 }
925 
926 static void setup_logical_ids(struct intel_gt *gt, u8 *logical_ids, u8 class)
927 {
928 	/*
929 	 * Logical to physical mapping is needed for proper support
930 	 * to split-frame feature.
931 	 */
932 	if (MEDIA_VER(gt->i915) >= 11 && class == VIDEO_DECODE_CLASS) {
933 		const u8 map[] = { 0, 2, 4, 6, 1, 3, 5, 7 };
934 
935 		populate_logical_ids(gt, logical_ids, class,
936 				     map, ARRAY_SIZE(map));
937 	} else {
938 		int i;
939 		u8 map[MAX_ENGINE_INSTANCE + 1];
940 
941 		for (i = 0; i < MAX_ENGINE_INSTANCE + 1; ++i)
942 			map[i] = i;
943 		populate_logical_ids(gt, logical_ids, class,
944 				     map, ARRAY_SIZE(map));
945 	}
946 }
947 
948 /**
949  * intel_engines_init_mmio() - allocate and prepare the Engine Command Streamers
950  * @gt: pointer to struct intel_gt
951  *
952  * Return: non-zero if the initialization failed.
953  */
954 int intel_engines_init_mmio(struct intel_gt *gt)
955 {
956 	struct drm_i915_private *i915 = gt->i915;
957 	const unsigned int engine_mask = init_engine_mask(gt);
958 	unsigned int mask = 0;
959 	unsigned int i, class;
960 	u8 logical_ids[MAX_ENGINE_INSTANCE + 1];
961 	int err;
962 
963 	drm_WARN_ON(&i915->drm, engine_mask == 0);
964 	drm_WARN_ON(&i915->drm, engine_mask &
965 		    GENMASK(BITS_PER_TYPE(mask) - 1, I915_NUM_ENGINES));
966 
967 	for (class = 0; class < MAX_ENGINE_CLASS + 1; ++class) {
968 		setup_logical_ids(gt, logical_ids, class);
969 
970 		for (i = 0; i < ARRAY_SIZE(intel_engines); ++i) {
971 			u8 instance = intel_engines[i].instance;
972 
973 			if (intel_engines[i].class != class ||
974 			    !HAS_ENGINE(gt, i))
975 				continue;
976 
977 			err = intel_engine_setup(gt, i,
978 						 logical_ids[instance]);
979 			if (err)
980 				goto cleanup;
981 
982 			mask |= BIT(i);
983 		}
984 	}
985 
986 	/*
987 	 * Catch failures to update intel_engines table when the new engines
988 	 * are added to the driver by a warning and disabling the forgotten
989 	 * engines.
990 	 */
991 	if (drm_WARN_ON(&i915->drm, mask != engine_mask))
992 		gt->info.engine_mask = mask;
993 
994 	gt->info.num_engines = hweight32(mask);
995 
996 	intel_gt_check_and_clear_faults(gt);
997 
998 	intel_setup_engine_capabilities(gt);
999 
1000 	intel_uncore_prune_engine_fw_domains(gt->uncore, gt);
1001 
1002 	return 0;
1003 
1004 cleanup:
1005 	intel_engines_free(gt);
1006 	return err;
1007 }
1008 ALLOW_ERROR_INJECTION(intel_engines_init_mmio, ERRNO);
1009 
1010 void intel_engine_init_execlists(struct intel_engine_cs *engine)
1011 {
1012 	struct intel_engine_execlists * const execlists = &engine->execlists;
1013 
1014 	execlists->port_mask = 1;
1015 	GEM_BUG_ON(!is_power_of_2(execlists_num_ports(execlists)));
1016 	GEM_BUG_ON(execlists_num_ports(execlists) > EXECLIST_MAX_PORTS);
1017 
1018 	memset(execlists->pending, 0, sizeof(execlists->pending));
1019 	execlists->active =
1020 		memset(execlists->inflight, 0, sizeof(execlists->inflight));
1021 }
1022 
1023 static void cleanup_status_page(struct intel_engine_cs *engine)
1024 {
1025 	struct i915_vma *vma;
1026 
1027 	/* Prevent writes into HWSP after returning the page to the system */
1028 	intel_engine_set_hwsp_writemask(engine, ~0u);
1029 
1030 	vma = fetch_and_zero(&engine->status_page.vma);
1031 	if (!vma)
1032 		return;
1033 
1034 	if (!HWS_NEEDS_PHYSICAL(engine->i915))
1035 		i915_vma_unpin(vma);
1036 
1037 	i915_gem_object_unpin_map(vma->obj);
1038 	i915_gem_object_put(vma->obj);
1039 }
1040 
1041 static int pin_ggtt_status_page(struct intel_engine_cs *engine,
1042 				struct i915_gem_ww_ctx *ww,
1043 				struct i915_vma *vma)
1044 {
1045 	unsigned int flags;
1046 
1047 	if (!HAS_LLC(engine->i915) && i915_ggtt_has_aperture(engine->gt->ggtt))
1048 		/*
1049 		 * On g33, we cannot place HWS above 256MiB, so
1050 		 * restrict its pinning to the low mappable arena.
1051 		 * Though this restriction is not documented for
1052 		 * gen4, gen5, or byt, they also behave similarly
1053 		 * and hang if the HWS is placed at the top of the
1054 		 * GTT. To generalise, it appears that all !llc
1055 		 * platforms have issues with us placing the HWS
1056 		 * above the mappable region (even though we never
1057 		 * actually map it).
1058 		 */
1059 		flags = PIN_MAPPABLE;
1060 	else
1061 		flags = PIN_HIGH;
1062 
1063 	return i915_ggtt_pin(vma, ww, 0, flags);
1064 }
1065 
1066 static int init_status_page(struct intel_engine_cs *engine)
1067 {
1068 	struct drm_i915_gem_object *obj;
1069 	struct i915_gem_ww_ctx ww;
1070 	struct i915_vma *vma;
1071 	void *vaddr;
1072 	int ret;
1073 
1074 	INIT_LIST_HEAD(&engine->status_page.timelines);
1075 
1076 	/*
1077 	 * Though the HWS register does support 36bit addresses, historically
1078 	 * we have had hangs and corruption reported due to wild writes if
1079 	 * the HWS is placed above 4G. We only allow objects to be allocated
1080 	 * in GFP_DMA32 for i965, and no earlier physical address users had
1081 	 * access to more than 4G.
1082 	 */
1083 	obj = i915_gem_object_create_internal(engine->i915, PAGE_SIZE);
1084 	if (IS_ERR(obj)) {
1085 		gt_err(engine->gt, "Failed to allocate status page\n");
1086 		return PTR_ERR(obj);
1087 	}
1088 
1089 	i915_gem_object_set_cache_coherency(obj, I915_CACHE_LLC);
1090 
1091 	vma = i915_vma_instance(obj, &engine->gt->ggtt->vm, NULL);
1092 	if (IS_ERR(vma)) {
1093 		ret = PTR_ERR(vma);
1094 		goto err_put;
1095 	}
1096 
1097 	i915_gem_ww_ctx_init(&ww, true);
1098 retry:
1099 	ret = i915_gem_object_lock(obj, &ww);
1100 	if (!ret && !HWS_NEEDS_PHYSICAL(engine->i915))
1101 		ret = pin_ggtt_status_page(engine, &ww, vma);
1102 	if (ret)
1103 		goto err;
1104 
1105 	vaddr = i915_gem_object_pin_map(obj, I915_MAP_WB);
1106 	if (IS_ERR(vaddr)) {
1107 		ret = PTR_ERR(vaddr);
1108 		goto err_unpin;
1109 	}
1110 
1111 	engine->status_page.addr = memset(vaddr, 0, PAGE_SIZE);
1112 	engine->status_page.vma = vma;
1113 
1114 err_unpin:
1115 	if (ret)
1116 		i915_vma_unpin(vma);
1117 err:
1118 	if (ret == -EDEADLK) {
1119 		ret = i915_gem_ww_ctx_backoff(&ww);
1120 		if (!ret)
1121 			goto retry;
1122 	}
1123 	i915_gem_ww_ctx_fini(&ww);
1124 err_put:
1125 	if (ret)
1126 		i915_gem_object_put(obj);
1127 	return ret;
1128 }
1129 
1130 static int intel_engine_init_tlb_invalidation(struct intel_engine_cs *engine)
1131 {
1132 	static const union intel_engine_tlb_inv_reg gen8_regs[] = {
1133 		[RENDER_CLASS].reg		= GEN8_RTCR,
1134 		[VIDEO_DECODE_CLASS].reg	= GEN8_M1TCR, /* , GEN8_M2TCR */
1135 		[VIDEO_ENHANCEMENT_CLASS].reg	= GEN8_VTCR,
1136 		[COPY_ENGINE_CLASS].reg		= GEN8_BTCR,
1137 	};
1138 	static const union intel_engine_tlb_inv_reg gen12_regs[] = {
1139 		[RENDER_CLASS].reg		= GEN12_GFX_TLB_INV_CR,
1140 		[VIDEO_DECODE_CLASS].reg	= GEN12_VD_TLB_INV_CR,
1141 		[VIDEO_ENHANCEMENT_CLASS].reg	= GEN12_VE_TLB_INV_CR,
1142 		[COPY_ENGINE_CLASS].reg		= GEN12_BLT_TLB_INV_CR,
1143 		[COMPUTE_CLASS].reg		= GEN12_COMPCTX_TLB_INV_CR,
1144 	};
1145 	static const union intel_engine_tlb_inv_reg xehp_regs[] = {
1146 		[RENDER_CLASS].mcr_reg		  = XEHP_GFX_TLB_INV_CR,
1147 		[VIDEO_DECODE_CLASS].mcr_reg	  = XEHP_VD_TLB_INV_CR,
1148 		[VIDEO_ENHANCEMENT_CLASS].mcr_reg = XEHP_VE_TLB_INV_CR,
1149 		[COPY_ENGINE_CLASS].mcr_reg	  = XEHP_BLT_TLB_INV_CR,
1150 		[COMPUTE_CLASS].mcr_reg		  = XEHP_COMPCTX_TLB_INV_CR,
1151 	};
1152 	static const union intel_engine_tlb_inv_reg xelpmp_regs[] = {
1153 		[VIDEO_DECODE_CLASS].reg	  = GEN12_VD_TLB_INV_CR,
1154 		[VIDEO_ENHANCEMENT_CLASS].reg     = GEN12_VE_TLB_INV_CR,
1155 		[OTHER_CLASS].reg		  = XELPMP_GSC_TLB_INV_CR,
1156 	};
1157 	struct drm_i915_private *i915 = engine->i915;
1158 	const unsigned int instance = engine->instance;
1159 	const unsigned int class = engine->class;
1160 	const union intel_engine_tlb_inv_reg *regs;
1161 	union intel_engine_tlb_inv_reg reg;
1162 	unsigned int num = 0;
1163 	u32 val;
1164 
1165 	/*
1166 	 * New platforms should not be added with catch-all-newer (>=)
1167 	 * condition so that any later platform added triggers the below warning
1168 	 * and in turn mandates a human cross-check of whether the invalidation
1169 	 * flows have compatible semantics.
1170 	 *
1171 	 * For instance with the 11.00 -> 12.00 transition three out of five
1172 	 * respective engine registers were moved to masked type. Then after the
1173 	 * 12.00 -> 12.50 transition multi cast handling is required too.
1174 	 */
1175 
1176 	if (engine->gt->type == GT_MEDIA) {
1177 		if (MEDIA_VER_FULL(i915) == IP_VER(13, 0)) {
1178 			regs = xelpmp_regs;
1179 			num = ARRAY_SIZE(xelpmp_regs);
1180 		}
1181 	} else {
1182 		if (GRAPHICS_VER_FULL(i915) == IP_VER(12, 74) ||
1183 		    GRAPHICS_VER_FULL(i915) == IP_VER(12, 71) ||
1184 		    GRAPHICS_VER_FULL(i915) == IP_VER(12, 70) ||
1185 		    GRAPHICS_VER_FULL(i915) == IP_VER(12, 55)) {
1186 			regs = xehp_regs;
1187 			num = ARRAY_SIZE(xehp_regs);
1188 		} else if (GRAPHICS_VER_FULL(i915) == IP_VER(12, 0) ||
1189 			   GRAPHICS_VER_FULL(i915) == IP_VER(12, 10)) {
1190 			regs = gen12_regs;
1191 			num = ARRAY_SIZE(gen12_regs);
1192 		} else if (GRAPHICS_VER(i915) >= 8 && GRAPHICS_VER(i915) <= 11) {
1193 			regs = gen8_regs;
1194 			num = ARRAY_SIZE(gen8_regs);
1195 		} else if (GRAPHICS_VER(i915) < 8) {
1196 			return 0;
1197 		}
1198 	}
1199 
1200 	if (gt_WARN_ONCE(engine->gt, !num,
1201 			 "Platform does not implement TLB invalidation!"))
1202 		return -ENODEV;
1203 
1204 	if (gt_WARN_ON_ONCE(engine->gt,
1205 			    class >= num ||
1206 			    (!regs[class].reg.reg &&
1207 			     !regs[class].mcr_reg.reg)))
1208 		return -ERANGE;
1209 
1210 	reg = regs[class];
1211 
1212 	if (regs == xelpmp_regs && class == OTHER_CLASS) {
1213 		/*
1214 		 * There's only a single GSC instance, but it uses register bit
1215 		 * 1 instead of either 0 or OTHER_GSC_INSTANCE.
1216 		 */
1217 		GEM_WARN_ON(instance != OTHER_GSC_INSTANCE);
1218 		val = 1;
1219 	} else if (regs == gen8_regs && class == VIDEO_DECODE_CLASS && instance == 1) {
1220 		reg.reg = GEN8_M2TCR;
1221 		val = 0;
1222 	} else {
1223 		val = instance;
1224 	}
1225 
1226 	val = BIT(val);
1227 
1228 	engine->tlb_inv.mcr = regs == xehp_regs;
1229 	engine->tlb_inv.reg = reg;
1230 	engine->tlb_inv.done = val;
1231 
1232 	if (GRAPHICS_VER(i915) >= 12 &&
1233 	    (engine->class == VIDEO_DECODE_CLASS ||
1234 	     engine->class == VIDEO_ENHANCEMENT_CLASS ||
1235 	     engine->class == COMPUTE_CLASS ||
1236 	     engine->class == OTHER_CLASS))
1237 		engine->tlb_inv.request = REG_MASKED_FIELD_ENABLE(val);
1238 	else
1239 		engine->tlb_inv.request = val;
1240 
1241 	return 0;
1242 }
1243 
1244 static int engine_setup_common(struct intel_engine_cs *engine)
1245 {
1246 	int err;
1247 
1248 	init_llist_head(&engine->barrier_tasks);
1249 
1250 	err = intel_engine_init_tlb_invalidation(engine);
1251 	if (err)
1252 		return err;
1253 
1254 	err = init_status_page(engine);
1255 	if (err)
1256 		return err;
1257 
1258 	engine->breadcrumbs = intel_breadcrumbs_create(engine);
1259 	if (!engine->breadcrumbs) {
1260 		err = -ENOMEM;
1261 		goto err_status;
1262 	}
1263 
1264 	engine->sched_engine = i915_sched_engine_create(ENGINE_PHYSICAL);
1265 	if (!engine->sched_engine) {
1266 		err = -ENOMEM;
1267 		goto err_sched_engine;
1268 	}
1269 	engine->sched_engine->private_data = engine;
1270 
1271 	err = intel_engine_init_cmd_parser(engine);
1272 	if (err)
1273 		goto err_cmd_parser;
1274 
1275 	intel_engine_init_execlists(engine);
1276 	intel_engine_init__pm(engine);
1277 	intel_engine_init_retire(engine);
1278 
1279 	/* Use the whole device by default */
1280 	engine->sseu =
1281 		intel_sseu_from_device_info(&engine->gt->info.sseu);
1282 
1283 	intel_engine_init_workarounds(engine);
1284 	intel_engine_init_whitelist(engine);
1285 	intel_engine_init_ctx_wa(engine);
1286 
1287 	if (GRAPHICS_VER(engine->i915) >= 12)
1288 		engine->flags |= I915_ENGINE_HAS_RELATIVE_MMIO;
1289 
1290 	return 0;
1291 
1292 err_cmd_parser:
1293 	i915_sched_engine_put(engine->sched_engine);
1294 err_sched_engine:
1295 	intel_breadcrumbs_put(engine->breadcrumbs);
1296 err_status:
1297 	cleanup_status_page(engine);
1298 	return err;
1299 }
1300 
1301 struct measure_breadcrumb {
1302 	struct i915_request rq;
1303 	struct intel_ring ring;
1304 	u32 cs[2048];
1305 };
1306 
1307 static int measure_breadcrumb_dw(struct intel_context *ce)
1308 {
1309 	struct intel_engine_cs *engine = ce->engine;
1310 	struct measure_breadcrumb *frame;
1311 	int dw;
1312 
1313 	GEM_BUG_ON(!engine->gt->scratch);
1314 
1315 	frame = kzalloc_obj(*frame);
1316 	if (!frame)
1317 		return -ENOMEM;
1318 
1319 	frame->rq.i915 = engine->i915;
1320 	frame->rq.engine = engine;
1321 	frame->rq.context = ce;
1322 	rcu_assign_pointer(frame->rq.timeline, ce->timeline);
1323 	frame->rq.hwsp_seqno = ce->timeline->hwsp_seqno;
1324 
1325 	frame->ring.vaddr = frame->cs;
1326 	frame->ring.size = sizeof(frame->cs);
1327 	frame->ring.wrap =
1328 		BITS_PER_TYPE(frame->ring.size) - ilog2(frame->ring.size);
1329 	frame->ring.effective_size = frame->ring.size;
1330 	intel_ring_update_space(&frame->ring);
1331 	frame->rq.ring = &frame->ring;
1332 
1333 	mutex_lock(&ce->timeline->mutex);
1334 	spin_lock_irq(&engine->sched_engine->lock);
1335 
1336 	dw = engine->emit_fini_breadcrumb(&frame->rq, frame->cs) - frame->cs;
1337 
1338 	spin_unlock_irq(&engine->sched_engine->lock);
1339 	mutex_unlock(&ce->timeline->mutex);
1340 
1341 	GEM_BUG_ON(dw & 1); /* RING_TAIL must be qword aligned */
1342 
1343 	kfree(frame);
1344 	return dw;
1345 }
1346 
1347 struct intel_context *
1348 intel_engine_create_pinned_context(struct intel_engine_cs *engine,
1349 				   struct i915_address_space *vm,
1350 				   unsigned int ring_size,
1351 				   unsigned int hwsp,
1352 				   struct lock_class_key *key,
1353 				   const char *name)
1354 {
1355 	struct intel_context *ce;
1356 	int err;
1357 
1358 	ce = intel_context_create(engine);
1359 	if (IS_ERR(ce))
1360 		return ce;
1361 
1362 	__set_bit(CONTEXT_BARRIER_BIT, &ce->flags);
1363 	ce->timeline = page_pack_bits(NULL, hwsp);
1364 	ce->ring = NULL;
1365 	ce->ring_size = ring_size;
1366 
1367 	i915_vm_put(ce->vm);
1368 	ce->vm = i915_vm_get(vm);
1369 
1370 	err = intel_context_pin(ce); /* perma-pin so it is always available */
1371 	if (err) {
1372 		intel_context_put(ce);
1373 		return ERR_PTR(err);
1374 	}
1375 
1376 	list_add_tail(&ce->pinned_contexts_link, &engine->pinned_contexts_list);
1377 
1378 	/*
1379 	 * Give our perma-pinned kernel timelines a separate lockdep class,
1380 	 * so that we can use them from within the normal user timelines
1381 	 * should we need to inject GPU operations during their request
1382 	 * construction.
1383 	 */
1384 	lockdep_set_class_and_name(&ce->timeline->mutex, key, name);
1385 
1386 	return ce;
1387 }
1388 
1389 void intel_engine_destroy_pinned_context(struct intel_context *ce)
1390 {
1391 	struct intel_engine_cs *engine = ce->engine;
1392 	struct i915_vma *hwsp = engine->status_page.vma;
1393 
1394 	GEM_BUG_ON(ce->timeline->hwsp_ggtt != hwsp);
1395 
1396 	mutex_lock(&hwsp->vm->mutex);
1397 	list_del(&ce->timeline->engine_link);
1398 	mutex_unlock(&hwsp->vm->mutex);
1399 
1400 	list_del(&ce->pinned_contexts_link);
1401 	intel_context_unpin(ce);
1402 	intel_context_put(ce);
1403 }
1404 
1405 static struct intel_context *
1406 create_ggtt_bind_context(struct intel_engine_cs *engine)
1407 {
1408 	static struct lock_class_key kernel;
1409 
1410 	/*
1411 	 * MI_UPDATE_GTT can insert up to 511 PTE entries and there could be multiple
1412 	 * bind requests at a time so get a bigger ring.
1413 	 */
1414 	return intel_engine_create_pinned_context(engine, engine->gt->vm, SZ_512K,
1415 						  I915_GEM_HWS_GGTT_BIND_ADDR,
1416 						  &kernel, "ggtt_bind_context");
1417 }
1418 
1419 static struct intel_context *
1420 create_kernel_context(struct intel_engine_cs *engine)
1421 {
1422 	static struct lock_class_key kernel;
1423 
1424 	return intel_engine_create_pinned_context(engine, engine->gt->vm, SZ_4K,
1425 						  I915_GEM_HWS_SEQNO_ADDR,
1426 						  &kernel, "kernel_context");
1427 }
1428 
1429 /*
1430  * engine_init_common - initialize engine state which might require hw access
1431  * @engine: Engine to initialize.
1432  *
1433  * Initializes @engine@ structure members shared between legacy and execlists
1434  * submission modes which do require hardware access.
1435  *
1436  * Typcally done at later stages of submission mode specific engine setup.
1437  *
1438  * Returns zero on success or an error code on failure.
1439  */
1440 static int engine_init_common(struct intel_engine_cs *engine)
1441 {
1442 	struct intel_context *ce, *bce = NULL;
1443 	int ret;
1444 
1445 	engine->set_default_submission(engine);
1446 
1447 	/*
1448 	 * We may need to do things with the shrinker which
1449 	 * require us to immediately switch back to the default
1450 	 * context. This can cause a problem as pinning the
1451 	 * default context also requires GTT space which may not
1452 	 * be available. To avoid this we always pin the default
1453 	 * context.
1454 	 */
1455 	ce = create_kernel_context(engine);
1456 	if (IS_ERR(ce))
1457 		return PTR_ERR(ce);
1458 	/*
1459 	 * Create a separate pinned context for GGTT update with blitter engine
1460 	 * if a platform require such service. MI_UPDATE_GTT works on other
1461 	 * engines as well but BCS should be less busy engine so pick that for
1462 	 * GGTT updates.
1463 	 */
1464 	if (i915_ggtt_require_binder(engine->i915) && engine->id == BCS0) {
1465 		bce = create_ggtt_bind_context(engine);
1466 		if (IS_ERR(bce)) {
1467 			ret = PTR_ERR(bce);
1468 			goto err_ce_context;
1469 		}
1470 	}
1471 
1472 	ret = measure_breadcrumb_dw(ce);
1473 	if (ret < 0)
1474 		goto err_bce_context;
1475 
1476 	engine->emit_fini_breadcrumb_dw = ret;
1477 	engine->kernel_context = ce;
1478 	engine->bind_context = bce;
1479 
1480 	return 0;
1481 
1482 err_bce_context:
1483 	if (bce)
1484 		intel_engine_destroy_pinned_context(bce);
1485 err_ce_context:
1486 	intel_engine_destroy_pinned_context(ce);
1487 	return ret;
1488 }
1489 
1490 int intel_engines_init(struct intel_gt *gt)
1491 {
1492 	int (*setup)(struct intel_engine_cs *engine);
1493 	struct intel_engine_cs *engine;
1494 	enum intel_engine_id id;
1495 	int err;
1496 
1497 	if (intel_uc_uses_guc_submission(&gt->uc)) {
1498 		gt->submission_method = INTEL_SUBMISSION_GUC;
1499 		setup = intel_guc_submission_setup;
1500 	} else if (HAS_EXECLISTS(gt->i915)) {
1501 		gt->submission_method = INTEL_SUBMISSION_ELSP;
1502 		setup = intel_execlists_submission_setup;
1503 	} else {
1504 		gt->submission_method = INTEL_SUBMISSION_RING;
1505 		setup = intel_ring_submission_setup;
1506 	}
1507 
1508 	for_each_engine(engine, gt, id) {
1509 		err = engine_setup_common(engine);
1510 		if (err)
1511 			return err;
1512 
1513 		err = setup(engine);
1514 		if (err) {
1515 			intel_engine_cleanup_common(engine);
1516 			return err;
1517 		}
1518 
1519 		/* The backend should now be responsible for cleanup */
1520 		GEM_BUG_ON(engine->release == NULL);
1521 
1522 		err = engine_init_common(engine);
1523 		if (err)
1524 			return err;
1525 
1526 		intel_engine_add_user(engine);
1527 	}
1528 
1529 	return 0;
1530 }
1531 
1532 /**
1533  * intel_engine_cleanup_common - cleans up the engine state created by
1534  *                                the common initializers.
1535  * @engine: Engine to cleanup.
1536  *
1537  * This cleans up everything created by the common helpers.
1538  */
1539 void intel_engine_cleanup_common(struct intel_engine_cs *engine)
1540 {
1541 	GEM_BUG_ON(!list_empty(&engine->sched_engine->requests));
1542 
1543 	i915_sched_engine_put(engine->sched_engine);
1544 	intel_breadcrumbs_put(engine->breadcrumbs);
1545 
1546 	intel_engine_fini_retire(engine);
1547 	intel_engine_cleanup_cmd_parser(engine);
1548 
1549 	if (engine->default_state)
1550 		fput(engine->default_state);
1551 
1552 	if (engine->kernel_context)
1553 		intel_engine_destroy_pinned_context(engine->kernel_context);
1554 
1555 	if (engine->bind_context)
1556 		intel_engine_destroy_pinned_context(engine->bind_context);
1557 
1558 
1559 	GEM_BUG_ON(!llist_empty(&engine->barrier_tasks));
1560 	cleanup_status_page(engine);
1561 
1562 	intel_wa_list_free(&engine->ctx_wa_list);
1563 	intel_wa_list_free(&engine->wa_list);
1564 	intel_wa_list_free(&engine->whitelist);
1565 }
1566 
1567 /**
1568  * intel_engine_resume - re-initializes the HW state of the engine
1569  * @engine: Engine to resume.
1570  *
1571  * Returns zero on success or an error code on failure.
1572  */
1573 int intel_engine_resume(struct intel_engine_cs *engine)
1574 {
1575 	intel_engine_apply_workarounds(engine);
1576 	intel_engine_apply_whitelist(engine);
1577 
1578 	return engine->resume(engine);
1579 }
1580 
1581 u64 intel_engine_get_active_head(const struct intel_engine_cs *engine)
1582 {
1583 	struct drm_i915_private *i915 = engine->i915;
1584 
1585 	u64 acthd;
1586 
1587 	if (GRAPHICS_VER(i915) >= 8)
1588 		acthd = ENGINE_READ64(engine, RING_ACTHD, RING_ACTHD_UDW);
1589 	else if (GRAPHICS_VER(i915) >= 4)
1590 		acthd = ENGINE_READ(engine, RING_ACTHD);
1591 	else
1592 		acthd = ENGINE_READ(engine, ACTHD);
1593 
1594 	return acthd;
1595 }
1596 
1597 u64 intel_engine_get_last_batch_head(const struct intel_engine_cs *engine)
1598 {
1599 	u64 bbaddr;
1600 
1601 	if (GRAPHICS_VER(engine->i915) >= 8)
1602 		bbaddr = ENGINE_READ64(engine, RING_BBADDR, RING_BBADDR_UDW);
1603 	else
1604 		bbaddr = ENGINE_READ(engine, RING_BBADDR);
1605 
1606 	return bbaddr;
1607 }
1608 
1609 static unsigned long stop_timeout(const struct intel_engine_cs *engine)
1610 {
1611 	if (in_atomic() || irqs_disabled() || rcu_preempt_depth()) /* inside atomic preempt-reset? */
1612 		return 0;
1613 
1614 	/*
1615 	 * If we are doing a normal GPU reset, we can take our time and allow
1616 	 * the engine to quiesce. We've stopped submission to the engine, and
1617 	 * if we wait long enough an innocent context should complete and
1618 	 * leave the engine idle. So they should not be caught unaware by
1619 	 * the forthcoming GPU reset (which usually follows the stop_cs)!
1620 	 */
1621 	return READ_ONCE(engine->props.stop_timeout_ms);
1622 }
1623 
1624 static int __intel_engine_stop_cs(struct intel_engine_cs *engine,
1625 				  int fast_timeout_us,
1626 				  int slow_timeout_ms)
1627 {
1628 	struct intel_uncore *uncore = engine->uncore;
1629 	const i915_reg_t mode = RING_MI_MODE(engine->mmio_base);
1630 	int err;
1631 
1632 	intel_uncore_write_fw(uncore, mode, REG_MASKED_FIELD_ENABLE(STOP_RING));
1633 
1634 	/*
1635 	 * Wa_22011802037: Prior to doing a reset, ensure CS is
1636 	 * stopped, set ring stop bit and prefetch disable bit to halt CS
1637 	 */
1638 	if (intel_engine_reset_needs_wa_22011802037(engine->gt))
1639 		intel_uncore_write_fw(uncore, RING_MODE_GEN7(engine->mmio_base),
1640 				      REG_MASKED_FIELD_ENABLE(GEN12_GFX_PREFETCH_DISABLE));
1641 
1642 	err = __intel_wait_for_register_fw(engine->uncore, mode,
1643 					   MODE_IDLE, MODE_IDLE,
1644 					   fast_timeout_us,
1645 					   slow_timeout_ms,
1646 					   NULL);
1647 
1648 	/* A final mmio read to let GPU writes be hopefully flushed to memory */
1649 	intel_uncore_posting_read_fw(uncore, mode);
1650 	return err;
1651 }
1652 
1653 int intel_engine_stop_cs(struct intel_engine_cs *engine)
1654 {
1655 	int err = 0;
1656 
1657 	if (GRAPHICS_VER(engine->i915) < 3)
1658 		return -ENODEV;
1659 
1660 	ENGINE_TRACE(engine, "\n");
1661 	/*
1662 	 * TODO: Find out why occasionally stopping the CS times out. Seen
1663 	 * especially with gem_eio tests.
1664 	 *
1665 	 * Occasionally trying to stop the cs times out, but does not adversely
1666 	 * affect functionality. The timeout is set as a config parameter that
1667 	 * defaults to 100ms. In most cases the follow up operation is to wait
1668 	 * for pending MI_FORCE_WAKES. The assumption is that this timeout is
1669 	 * sufficient for any pending MI_FORCEWAKEs to complete. Once root
1670 	 * caused, the caller must check and handle the return from this
1671 	 * function.
1672 	 */
1673 	if (__intel_engine_stop_cs(engine, 1000, stop_timeout(engine))) {
1674 		ENGINE_TRACE(engine,
1675 			     "timed out on STOP_RING -> IDLE; HEAD:%04x, TAIL:%04x\n",
1676 			     ENGINE_READ_FW(engine, RING_HEAD) & HEAD_ADDR,
1677 			     ENGINE_READ_FW(engine, RING_TAIL) & TAIL_ADDR);
1678 
1679 		/*
1680 		 * Sometimes we observe that the idle flag is not
1681 		 * set even though the ring is empty. So double
1682 		 * check before giving up.
1683 		 */
1684 		if ((ENGINE_READ_FW(engine, RING_HEAD) & HEAD_ADDR) !=
1685 		    (ENGINE_READ_FW(engine, RING_TAIL) & TAIL_ADDR))
1686 			err = -ETIMEDOUT;
1687 	}
1688 
1689 	return err;
1690 }
1691 
1692 void intel_engine_cancel_stop_cs(struct intel_engine_cs *engine)
1693 {
1694 	ENGINE_TRACE(engine, "\n");
1695 
1696 	ENGINE_WRITE_FW(engine, RING_MI_MODE, REG_MASKED_FIELD_DISABLE(STOP_RING));
1697 }
1698 
1699 static u32 __cs_pending_mi_force_wakes(struct intel_engine_cs *engine)
1700 {
1701 	static const i915_reg_t _reg[I915_NUM_ENGINES] = {
1702 		[RCS0] = MSG_IDLE_CS,
1703 		[BCS0] = MSG_IDLE_BCS,
1704 		[VCS0] = MSG_IDLE_VCS0,
1705 		[VCS1] = MSG_IDLE_VCS1,
1706 		[VCS2] = MSG_IDLE_VCS2,
1707 		[VCS3] = MSG_IDLE_VCS3,
1708 		[VCS4] = MSG_IDLE_VCS4,
1709 		[VCS5] = MSG_IDLE_VCS5,
1710 		[VCS6] = MSG_IDLE_VCS6,
1711 		[VCS7] = MSG_IDLE_VCS7,
1712 		[VECS0] = MSG_IDLE_VECS0,
1713 		[VECS1] = MSG_IDLE_VECS1,
1714 		[VECS2] = MSG_IDLE_VECS2,
1715 		[VECS3] = MSG_IDLE_VECS3,
1716 		[CCS0] = MSG_IDLE_CS,
1717 		[CCS1] = MSG_IDLE_CS,
1718 		[CCS2] = MSG_IDLE_CS,
1719 		[CCS3] = MSG_IDLE_CS,
1720 	};
1721 	u32 val;
1722 
1723 	if (!_reg[engine->id].reg)
1724 		return 0;
1725 
1726 	val = intel_uncore_read(engine->uncore, _reg[engine->id]);
1727 
1728 	/* bits[29:25] & bits[13:9] >> shift */
1729 	return (val & (val >> 16) & MSG_IDLE_FW_MASK) >> MSG_IDLE_FW_SHIFT;
1730 }
1731 
1732 static void __gpm_wait_for_fw_complete(struct intel_gt *gt, u32 fw_mask)
1733 {
1734 	int ret;
1735 
1736 	/* Ensure GPM receives fw up/down after CS is stopped */
1737 	udelay(1);
1738 
1739 	/* Wait for forcewake request to complete in GPM */
1740 	ret =  __intel_wait_for_register_fw(gt->uncore,
1741 					    GEN9_PWRGT_DOMAIN_STATUS,
1742 					    fw_mask, fw_mask, 5000, 0, NULL);
1743 
1744 	/* Ensure CS receives fw ack from GPM */
1745 	udelay(1);
1746 
1747 	if (ret)
1748 		GT_TRACE(gt, "Failed to complete pending forcewake %d\n", ret);
1749 }
1750 
1751 /*
1752  * Wa_22011802037:gen12: In addition to stopping the cs, we need to wait for any
1753  * pending MI_FORCE_WAKEUP requests that the CS has initiated to complete. The
1754  * pending status is indicated by bits[13:9] (masked by bits[29:25]) in the
1755  * MSG_IDLE register. There's one MSG_IDLE register per reset domain. Since we
1756  * are concerned only with the gt reset here, we use a logical OR of pending
1757  * forcewakeups from all reset domains and then wait for them to complete by
1758  * querying PWRGT_DOMAIN_STATUS.
1759  */
1760 void intel_engine_wait_for_pending_mi_fw(struct intel_engine_cs *engine)
1761 {
1762 	u32 fw_pending = __cs_pending_mi_force_wakes(engine);
1763 
1764 	if (fw_pending)
1765 		__gpm_wait_for_fw_complete(engine->gt, fw_pending);
1766 }
1767 
1768 /* NB: please notice the memset */
1769 void intel_engine_get_instdone(const struct intel_engine_cs *engine,
1770 			       struct intel_instdone *instdone)
1771 {
1772 	struct drm_i915_private *i915 = engine->i915;
1773 	struct intel_uncore *uncore = engine->uncore;
1774 	u32 mmio_base = engine->mmio_base;
1775 	int slice;
1776 	int subslice;
1777 	int iter;
1778 
1779 	memset(instdone, 0, sizeof(*instdone));
1780 
1781 	if (GRAPHICS_VER(i915) >= 8) {
1782 		instdone->instdone =
1783 			intel_uncore_read(uncore, RING_INSTDONE(mmio_base));
1784 
1785 		if (engine->id != RCS0)
1786 			return;
1787 
1788 		instdone->slice_common =
1789 			intel_uncore_read(uncore, GEN7_SC_INSTDONE);
1790 		if (GRAPHICS_VER(i915) >= 12) {
1791 			instdone->slice_common_extra[0] =
1792 				intel_uncore_read(uncore, GEN12_SC_INSTDONE_EXTRA);
1793 			instdone->slice_common_extra[1] =
1794 				intel_uncore_read(uncore, GEN12_SC_INSTDONE_EXTRA2);
1795 		}
1796 
1797 		for_each_ss_steering(iter, engine->gt, slice, subslice) {
1798 			instdone->sampler[slice][subslice] =
1799 				intel_gt_mcr_read(engine->gt,
1800 						  GEN8_SAMPLER_INSTDONE,
1801 						  slice, subslice);
1802 			instdone->row[slice][subslice] =
1803 				intel_gt_mcr_read(engine->gt,
1804 						  GEN8_ROW_INSTDONE,
1805 						  slice, subslice);
1806 		}
1807 
1808 		if (GRAPHICS_VER_FULL(i915) >= IP_VER(12, 55)) {
1809 			for_each_ss_steering(iter, engine->gt, slice, subslice)
1810 				instdone->geom_svg[slice][subslice] =
1811 					intel_gt_mcr_read(engine->gt,
1812 							  XEHPG_INSTDONE_GEOM_SVG,
1813 							  slice, subslice);
1814 		}
1815 	} else if (GRAPHICS_VER(i915) >= 7) {
1816 		instdone->instdone =
1817 			intel_uncore_read(uncore, RING_INSTDONE(mmio_base));
1818 
1819 		if (engine->id != RCS0)
1820 			return;
1821 
1822 		instdone->slice_common =
1823 			intel_uncore_read(uncore, GEN7_SC_INSTDONE);
1824 		instdone->sampler[0][0] =
1825 			intel_uncore_read(uncore, GEN7_SAMPLER_INSTDONE);
1826 		instdone->row[0][0] =
1827 			intel_uncore_read(uncore, GEN7_ROW_INSTDONE);
1828 	} else if (GRAPHICS_VER(i915) >= 4) {
1829 		instdone->instdone =
1830 			intel_uncore_read(uncore, RING_INSTDONE(mmio_base));
1831 		if (engine->id == RCS0)
1832 			/* HACK: Using the wrong struct member */
1833 			instdone->slice_common =
1834 				intel_uncore_read(uncore, GEN4_INSTDONE1);
1835 	} else {
1836 		instdone->instdone = intel_uncore_read(uncore, GEN2_INSTDONE);
1837 	}
1838 }
1839 
1840 static bool ring_is_idle(struct intel_engine_cs *engine)
1841 {
1842 	bool idle = true;
1843 
1844 	if (I915_SELFTEST_ONLY(!engine->mmio_base))
1845 		return true;
1846 
1847 	if (!intel_engine_pm_get_if_awake(engine))
1848 		return true;
1849 
1850 	/* First check that no commands are left in the ring */
1851 	if ((ENGINE_READ(engine, RING_HEAD) & HEAD_ADDR) !=
1852 	    (ENGINE_READ(engine, RING_TAIL) & TAIL_ADDR))
1853 		idle = false;
1854 
1855 	/* No bit for gen2, so assume the CS parser is idle */
1856 	if (GRAPHICS_VER(engine->i915) > 2 &&
1857 	    !(ENGINE_READ(engine, RING_MI_MODE) & MODE_IDLE))
1858 		idle = false;
1859 
1860 	intel_engine_pm_put(engine);
1861 
1862 	return idle;
1863 }
1864 
1865 void __intel_engine_flush_submission(struct intel_engine_cs *engine, bool sync)
1866 {
1867 	struct tasklet_struct *t = &engine->sched_engine->tasklet;
1868 
1869 	if (!t->callback)
1870 		return;
1871 
1872 	local_bh_disable();
1873 	if (tasklet_trylock(t)) {
1874 		/* Must wait for any GPU reset in progress. */
1875 		if (__tasklet_is_enabled(t))
1876 			t->callback(t);
1877 		tasklet_unlock(t);
1878 	}
1879 	local_bh_enable();
1880 
1881 	/* Synchronise and wait for the tasklet on another CPU */
1882 	if (sync)
1883 		tasklet_unlock_wait(t);
1884 }
1885 
1886 /**
1887  * intel_engine_is_idle() - Report if the engine has finished process all work
1888  * @engine: the intel_engine_cs
1889  *
1890  * Return true if there are no requests pending, nothing left to be submitted
1891  * to hardware, and that the engine is idle.
1892  */
1893 bool intel_engine_is_idle(struct intel_engine_cs *engine)
1894 {
1895 	/* More white lies, if wedged, hw state is inconsistent */
1896 	if (intel_gt_is_wedged(engine->gt))
1897 		return true;
1898 
1899 	if (!intel_engine_pm_is_awake(engine))
1900 		return true;
1901 
1902 	/* Waiting to drain ELSP? */
1903 	intel_synchronize_hardirq(engine->i915);
1904 	intel_engine_flush_submission(engine);
1905 
1906 	/* ELSP is empty, but there are ready requests? E.g. after reset */
1907 	if (!i915_sched_engine_is_empty(engine->sched_engine))
1908 		return false;
1909 
1910 	/* Ring stopped? */
1911 	return ring_is_idle(engine);
1912 }
1913 
1914 bool intel_engines_are_idle(struct intel_gt *gt)
1915 {
1916 	struct intel_engine_cs *engine;
1917 	enum intel_engine_id id;
1918 
1919 	/*
1920 	 * If the driver is wedged, HW state may be very inconsistent and
1921 	 * report that it is still busy, even though we have stopped using it.
1922 	 */
1923 	if (intel_gt_is_wedged(gt))
1924 		return true;
1925 
1926 	/* Already parked (and passed an idleness test); must still be idle */
1927 	if (!READ_ONCE(gt->awake))
1928 		return true;
1929 
1930 	for_each_engine(engine, gt, id) {
1931 		if (!intel_engine_is_idle(engine))
1932 			return false;
1933 	}
1934 
1935 	return true;
1936 }
1937 
1938 bool intel_engine_irq_enable(struct intel_engine_cs *engine)
1939 {
1940 	if (!engine->irq_enable)
1941 		return false;
1942 
1943 	/* Caller disables interrupts */
1944 	spin_lock(engine->gt->irq_lock);
1945 	engine->irq_enable(engine);
1946 	spin_unlock(engine->gt->irq_lock);
1947 
1948 	return true;
1949 }
1950 
1951 void intel_engine_irq_disable(struct intel_engine_cs *engine)
1952 {
1953 	if (!engine->irq_disable)
1954 		return;
1955 
1956 	/* Caller disables interrupts */
1957 	spin_lock(engine->gt->irq_lock);
1958 	engine->irq_disable(engine);
1959 	spin_unlock(engine->gt->irq_lock);
1960 }
1961 
1962 void intel_engines_reset_default_submission(struct intel_gt *gt)
1963 {
1964 	struct intel_engine_cs *engine;
1965 	enum intel_engine_id id;
1966 
1967 	for_each_engine(engine, gt, id) {
1968 		if (engine->sanitize)
1969 			engine->sanitize(engine);
1970 
1971 		if (engine->set_default_submission)
1972 			engine->set_default_submission(engine);
1973 	}
1974 }
1975 
1976 bool intel_engine_can_store_dword(struct intel_engine_cs *engine)
1977 {
1978 	switch (GRAPHICS_VER(engine->i915)) {
1979 	case 2:
1980 		return false; /* uses physical not virtual addresses */
1981 	case 3:
1982 		/* maybe only uses physical not virtual addresses */
1983 		return !(IS_I915G(engine->i915) || IS_I915GM(engine->i915));
1984 	case 4:
1985 		return !IS_I965G(engine->i915); /* who knows! */
1986 	case 6:
1987 		return engine->class != VIDEO_DECODE_CLASS; /* b0rked */
1988 	default:
1989 		return true;
1990 	}
1991 }
1992 
1993 static struct intel_timeline *get_timeline(struct i915_request *rq)
1994 {
1995 	struct intel_timeline *tl;
1996 
1997 	/*
1998 	 * Even though we are holding the engine->sched_engine->lock here, there
1999 	 * is no control over the submission queue per-se and we are
2000 	 * inspecting the active state at a random point in time, with an
2001 	 * unknown queue. Play safe and make sure the timeline remains valid.
2002 	 * (Only being used for pretty printing, one extra kref shouldn't
2003 	 * cause a camel stampede!)
2004 	 */
2005 	rcu_read_lock();
2006 	tl = rcu_dereference(rq->timeline);
2007 	if (!kref_get_unless_zero(&tl->kref))
2008 		tl = NULL;
2009 	rcu_read_unlock();
2010 
2011 	return tl;
2012 }
2013 
2014 static int print_ring(char *buf, int sz, struct i915_request *rq)
2015 {
2016 	int len = 0;
2017 
2018 	if (!i915_request_signaled(rq)) {
2019 		struct intel_timeline *tl = get_timeline(rq);
2020 
2021 		len = scnprintf(buf, sz,
2022 				"ring:{start:%08x, hwsp:%08x, seqno:%08x, runtime:%llums}, ",
2023 				i915_ggtt_offset(rq->ring->vma),
2024 				tl ? tl->hwsp_offset : 0,
2025 				hwsp_seqno(rq),
2026 				DIV_ROUND_CLOSEST_ULL(intel_context_get_total_runtime_ns(rq->context),
2027 						      1000 * 1000));
2028 
2029 		if (tl)
2030 			intel_timeline_put(tl);
2031 	}
2032 
2033 	return len;
2034 }
2035 
2036 static void hexdump(struct drm_printer *m, const void *buf, size_t len)
2037 {
2038 	const size_t rowsize = 8 * sizeof(u32);
2039 	const void *prev = NULL;
2040 	bool skip = false;
2041 	size_t pos;
2042 
2043 	for (pos = 0; pos < len; pos += rowsize) {
2044 		char line[128];
2045 
2046 		if (prev && !memcmp(prev, buf + pos, rowsize)) {
2047 			if (!skip) {
2048 				drm_printf(m, "*\n");
2049 				skip = true;
2050 			}
2051 			continue;
2052 		}
2053 
2054 		WARN_ON_ONCE(hex_dump_to_buffer(buf + pos, len - pos,
2055 						rowsize, sizeof(u32),
2056 						line, sizeof(line),
2057 						false) >= sizeof(line));
2058 		drm_printf(m, "[%04zx] %s\n", pos, line);
2059 
2060 		prev = buf + pos;
2061 		skip = false;
2062 	}
2063 }
2064 
2065 static const char *repr_timer(const struct timer_list *t)
2066 {
2067 	if (!READ_ONCE(t->expires))
2068 		return "inactive";
2069 
2070 	if (timer_pending(t))
2071 		return "active";
2072 
2073 	return "expired";
2074 }
2075 
2076 static void intel_engine_print_registers(struct intel_engine_cs *engine,
2077 					 struct drm_printer *m)
2078 {
2079 	struct drm_i915_private *i915 = engine->i915;
2080 	struct intel_engine_execlists * const execlists = &engine->execlists;
2081 	u64 addr;
2082 
2083 	if (engine->id == RENDER_CLASS && IS_GRAPHICS_VER(i915, 4, 7))
2084 		drm_printf(m, "\tCCID: 0x%08x\n", ENGINE_READ(engine, CCID));
2085 	if (HAS_EXECLISTS(i915)) {
2086 		drm_printf(m, "\tEL_STAT_HI: 0x%08x\n",
2087 			   ENGINE_READ(engine, RING_EXECLIST_STATUS_HI));
2088 		drm_printf(m, "\tEL_STAT_LO: 0x%08x\n",
2089 			   ENGINE_READ(engine, RING_EXECLIST_STATUS_LO));
2090 	}
2091 	drm_printf(m, "\tRING_START: 0x%08x\n",
2092 		   ENGINE_READ(engine, RING_START));
2093 	drm_printf(m, "\tRING_HEAD:  0x%08x\n",
2094 		   ENGINE_READ(engine, RING_HEAD) & HEAD_ADDR);
2095 	drm_printf(m, "\tRING_TAIL:  0x%08x\n",
2096 		   ENGINE_READ(engine, RING_TAIL) & TAIL_ADDR);
2097 	drm_printf(m, "\tRING_CTL:   0x%08x%s\n",
2098 		   ENGINE_READ(engine, RING_CTL),
2099 		   ENGINE_READ(engine, RING_CTL) & (RING_WAIT | RING_WAIT_SEMAPHORE) ? " [waiting]" : "");
2100 	if (GRAPHICS_VER(engine->i915) > 2) {
2101 		drm_printf(m, "\tRING_MODE:  0x%08x%s\n",
2102 			   ENGINE_READ(engine, RING_MI_MODE),
2103 			   ENGINE_READ(engine, RING_MI_MODE) & (MODE_IDLE) ? " [idle]" : "");
2104 	}
2105 
2106 	if (GRAPHICS_VER(i915) >= 6) {
2107 		drm_printf(m, "\tRING_IMR:   0x%08x\n",
2108 			   ENGINE_READ(engine, RING_IMR));
2109 		drm_printf(m, "\tRING_ESR:   0x%08x\n",
2110 			   ENGINE_READ(engine, RING_ESR));
2111 		drm_printf(m, "\tRING_EMR:   0x%08x\n",
2112 			   ENGINE_READ(engine, RING_EMR));
2113 		drm_printf(m, "\tRING_EIR:   0x%08x\n",
2114 			   ENGINE_READ(engine, RING_EIR));
2115 	}
2116 
2117 	addr = intel_engine_get_active_head(engine);
2118 	drm_printf(m, "\tACTHD:  0x%08x_%08x\n",
2119 		   upper_32_bits(addr), lower_32_bits(addr));
2120 	addr = intel_engine_get_last_batch_head(engine);
2121 	drm_printf(m, "\tBBADDR: 0x%08x_%08x\n",
2122 		   upper_32_bits(addr), lower_32_bits(addr));
2123 	if (GRAPHICS_VER(i915) >= 8)
2124 		addr = ENGINE_READ64(engine, RING_DMA_FADD, RING_DMA_FADD_UDW);
2125 	else if (GRAPHICS_VER(i915) >= 4)
2126 		addr = ENGINE_READ(engine, RING_DMA_FADD);
2127 	else
2128 		addr = ENGINE_READ(engine, DMA_FADD_I8XX);
2129 	drm_printf(m, "\tDMA_FADDR: 0x%08x_%08x\n",
2130 		   upper_32_bits(addr), lower_32_bits(addr));
2131 	if (GRAPHICS_VER(i915) >= 4) {
2132 		drm_printf(m, "\tIPEIR: 0x%08x\n",
2133 			   ENGINE_READ(engine, RING_IPEIR));
2134 		drm_printf(m, "\tIPEHR: 0x%08x\n",
2135 			   ENGINE_READ(engine, RING_IPEHR));
2136 	} else {
2137 		drm_printf(m, "\tIPEIR: 0x%08x\n", ENGINE_READ(engine, IPEIR));
2138 		drm_printf(m, "\tIPEHR: 0x%08x\n", ENGINE_READ(engine, IPEHR));
2139 	}
2140 
2141 	if (HAS_EXECLISTS(i915) && !intel_engine_uses_guc(engine)) {
2142 		struct i915_request * const *port, *rq;
2143 		const u32 *hws =
2144 			&engine->status_page.addr[I915_HWS_CSB_BUF0_INDEX];
2145 		const u8 num_entries = execlists->csb_size;
2146 		unsigned int idx;
2147 		u8 read, write;
2148 
2149 		drm_printf(m, "\tExeclist tasklet queued? %s (%s), preempt? %s, timeslice? %s\n",
2150 			   str_yes_no(test_bit(TASKLET_STATE_SCHED, &engine->sched_engine->tasklet.state)),
2151 			   str_enabled_disabled(!atomic_read(&engine->sched_engine->tasklet.count)),
2152 			   repr_timer(&engine->execlists.preempt),
2153 			   repr_timer(&engine->execlists.timer));
2154 
2155 		read = execlists->csb_head;
2156 		write = READ_ONCE(*execlists->csb_write);
2157 
2158 		drm_printf(m, "\tExeclist status: 0x%08x %08x; CSB read:%d, write:%d, entries:%d\n",
2159 			   ENGINE_READ(engine, RING_EXECLIST_STATUS_LO),
2160 			   ENGINE_READ(engine, RING_EXECLIST_STATUS_HI),
2161 			   read, write, num_entries);
2162 
2163 		if (read >= num_entries)
2164 			read = 0;
2165 		if (write >= num_entries)
2166 			write = 0;
2167 		if (read > write)
2168 			write += num_entries;
2169 		while (read < write) {
2170 			idx = ++read % num_entries;
2171 			drm_printf(m, "\tExeclist CSB[%d]: 0x%08x, context: %d\n",
2172 				   idx, hws[idx * 2], hws[idx * 2 + 1]);
2173 		}
2174 
2175 		i915_sched_engine_active_lock_bh(engine->sched_engine);
2176 		rcu_read_lock();
2177 		for (port = execlists->active; (rq = *port); port++) {
2178 			char hdr[160];
2179 			int len;
2180 
2181 			len = scnprintf(hdr, sizeof(hdr),
2182 					"\t\tActive[%d]:  ccid:%08x%s%s, ",
2183 					(int)(port - execlists->active),
2184 					rq->context->lrc.ccid,
2185 					intel_context_is_closed(rq->context) ? "!" : "",
2186 					intel_context_is_banned(rq->context) ? "*" : "");
2187 			len += print_ring(hdr + len, sizeof(hdr) - len, rq);
2188 			scnprintf(hdr + len, sizeof(hdr) - len, "rq: ");
2189 			i915_request_show(m, rq, hdr, 0);
2190 		}
2191 		for (port = execlists->pending; (rq = *port); port++) {
2192 			char hdr[160];
2193 			int len;
2194 
2195 			len = scnprintf(hdr, sizeof(hdr),
2196 					"\t\tPending[%d]: ccid:%08x%s%s, ",
2197 					(int)(port - execlists->pending),
2198 					rq->context->lrc.ccid,
2199 					intel_context_is_closed(rq->context) ? "!" : "",
2200 					intel_context_is_banned(rq->context) ? "*" : "");
2201 			len += print_ring(hdr + len, sizeof(hdr) - len, rq);
2202 			scnprintf(hdr + len, sizeof(hdr) - len, "rq: ");
2203 			i915_request_show(m, rq, hdr, 0);
2204 		}
2205 		rcu_read_unlock();
2206 		i915_sched_engine_active_unlock_bh(engine->sched_engine);
2207 	} else if (GRAPHICS_VER(i915) > 6) {
2208 		drm_printf(m, "\tPP_DIR_BASE: 0x%08x\n",
2209 			   ENGINE_READ(engine, RING_PP_DIR_BASE));
2210 		drm_printf(m, "\tPP_DIR_BASE_READ: 0x%08x\n",
2211 			   ENGINE_READ(engine, RING_PP_DIR_BASE_READ));
2212 		drm_printf(m, "\tPP_DIR_DCLV: 0x%08x\n",
2213 			   ENGINE_READ(engine, RING_PP_DIR_DCLV));
2214 	}
2215 }
2216 
2217 static void print_request_ring(struct drm_printer *m, struct i915_request *rq)
2218 {
2219 	struct i915_vma_resource *vma_res = rq->batch_res;
2220 	void *ring;
2221 	int size;
2222 
2223 	drm_printf(m,
2224 		   "[head %04x, postfix %04x, tail %04x, batch 0x%08x_%08x]:\n",
2225 		   rq->head, rq->postfix, rq->tail,
2226 		   vma_res ? upper_32_bits(vma_res->start) : ~0u,
2227 		   vma_res ? lower_32_bits(vma_res->start) : ~0u);
2228 
2229 	size = rq->tail - rq->head;
2230 	if (rq->tail < rq->head)
2231 		size += rq->ring->size;
2232 
2233 	ring = kmalloc(size, GFP_ATOMIC);
2234 	if (ring) {
2235 		const void *vaddr = rq->ring->vaddr;
2236 		unsigned int head = rq->head;
2237 		unsigned int len = 0;
2238 
2239 		if (rq->tail < head) {
2240 			len = rq->ring->size - head;
2241 			memcpy(ring, vaddr + head, len);
2242 			head = 0;
2243 		}
2244 		memcpy(ring + len, vaddr + head, size - len);
2245 
2246 		hexdump(m, ring, size);
2247 		kfree(ring);
2248 	}
2249 }
2250 
2251 static unsigned long read_ul(void *p, size_t x)
2252 {
2253 	return *(unsigned long *)(p + x);
2254 }
2255 
2256 static void print_properties(struct intel_engine_cs *engine,
2257 			     struct drm_printer *m)
2258 {
2259 	static const struct pmap {
2260 		size_t offset;
2261 		const char *name;
2262 	} props[] = {
2263 #define P(x) { \
2264 	.offset = offsetof(typeof(engine->props), x), \
2265 	.name = #x \
2266 }
2267 		P(heartbeat_interval_ms),
2268 		P(max_busywait_duration_ns),
2269 		P(preempt_timeout_ms),
2270 		P(stop_timeout_ms),
2271 		P(timeslice_duration_ms),
2272 
2273 		{},
2274 #undef P
2275 	};
2276 	const struct pmap *p;
2277 
2278 	drm_printf(m, "\tProperties:\n");
2279 	for (p = props; p->name; p++)
2280 		drm_printf(m, "\t\t%s: %lu [default %lu]\n",
2281 			   p->name,
2282 			   read_ul(&engine->props, p->offset),
2283 			   read_ul(&engine->defaults, p->offset));
2284 }
2285 
2286 static void engine_dump_request(struct i915_request *rq, struct drm_printer *m, const char *msg)
2287 {
2288 	struct intel_timeline *tl = get_timeline(rq);
2289 
2290 	i915_request_show(m, rq, msg, 0);
2291 
2292 	drm_printf(m, "\t\tring->start:  0x%08x\n",
2293 		   i915_ggtt_offset(rq->ring->vma));
2294 	drm_printf(m, "\t\tring->head:   0x%08x\n",
2295 		   rq->ring->head);
2296 	drm_printf(m, "\t\tring->tail:   0x%08x\n",
2297 		   rq->ring->tail);
2298 	drm_printf(m, "\t\tring->emit:   0x%08x\n",
2299 		   rq->ring->emit);
2300 	drm_printf(m, "\t\tring->space:  0x%08x\n",
2301 		   rq->ring->space);
2302 
2303 	if (tl) {
2304 		drm_printf(m, "\t\tring->hwsp:   0x%08x\n",
2305 			   tl->hwsp_offset);
2306 		intel_timeline_put(tl);
2307 	}
2308 
2309 	print_request_ring(m, rq);
2310 
2311 	if (rq->context->lrc_reg_state) {
2312 		drm_printf(m, "Logical Ring Context:\n");
2313 		hexdump(m, rq->context->lrc_reg_state, PAGE_SIZE);
2314 	}
2315 }
2316 
2317 void intel_engine_dump_active_requests(struct list_head *requests,
2318 				       struct i915_request *hung_rq,
2319 				       struct drm_printer *m)
2320 {
2321 	struct i915_request *rq;
2322 	const char *msg;
2323 	enum i915_request_state state;
2324 
2325 	list_for_each_entry(rq, requests, sched.link) {
2326 		if (rq == hung_rq)
2327 			continue;
2328 
2329 		state = i915_test_request_state(rq);
2330 		if (state < I915_REQUEST_QUEUED)
2331 			continue;
2332 
2333 		if (state == I915_REQUEST_ACTIVE)
2334 			msg = "\t\tactive on engine";
2335 		else
2336 			msg = "\t\tactive in queue";
2337 
2338 		engine_dump_request(rq, m, msg);
2339 	}
2340 }
2341 
2342 static void engine_dump_active_requests(struct intel_engine_cs *engine,
2343 					struct drm_printer *m)
2344 {
2345 	struct intel_context *hung_ce = NULL;
2346 	struct i915_request *hung_rq = NULL;
2347 
2348 	/*
2349 	 * No need for an engine->irq_seqno_barrier() before the seqno reads.
2350 	 * The GPU is still running so requests are still executing and any
2351 	 * hardware reads will be out of date by the time they are reported.
2352 	 * But the intention here is just to report an instantaneous snapshot
2353 	 * so that's fine.
2354 	 */
2355 	intel_engine_get_hung_entity(engine, &hung_ce, &hung_rq);
2356 
2357 	drm_printf(m, "\tRequests:\n");
2358 
2359 	if (hung_rq)
2360 		engine_dump_request(hung_rq, m, "\t\thung");
2361 	else if (hung_ce)
2362 		drm_printf(m, "\t\tGot hung ce but no hung rq!\n");
2363 
2364 	if (intel_uc_uses_guc_submission(&engine->gt->uc))
2365 		intel_guc_dump_active_requests(engine, hung_rq, m);
2366 	else
2367 		intel_execlists_dump_active_requests(engine, hung_rq, m);
2368 
2369 	if (hung_rq)
2370 		i915_request_put(hung_rq);
2371 }
2372 
2373 void intel_engine_dump(struct intel_engine_cs *engine,
2374 		       struct drm_printer *m,
2375 		       const char *header, ...)
2376 {
2377 	struct i915_gpu_error * const error = &engine->i915->gpu_error;
2378 	struct i915_request *rq;
2379 	intel_wakeref_t wakeref;
2380 	ktime_t dummy;
2381 
2382 	if (header) {
2383 		va_list ap;
2384 
2385 		va_start(ap, header);
2386 		drm_vprintf(m, header, &ap);
2387 		va_end(ap);
2388 	}
2389 
2390 	if (intel_gt_is_wedged(engine->gt))
2391 		drm_printf(m, "*** WEDGED ***\n");
2392 
2393 	drm_printf(m, "\tAwake? %d\n", atomic_read(&engine->wakeref.count));
2394 	drm_printf(m, "\tBarriers?: %s\n",
2395 		   str_yes_no(!llist_empty(&engine->barrier_tasks)));
2396 	drm_printf(m, "\tLatency: %luus\n",
2397 		   ewma__engine_latency_read(&engine->latency));
2398 	if (intel_engine_supports_stats(engine))
2399 		drm_printf(m, "\tRuntime: %llums\n",
2400 			   ktime_to_ms(intel_engine_get_busy_time(engine,
2401 								  &dummy)));
2402 	drm_printf(m, "\tForcewake: %x domains, %d active\n",
2403 		   engine->fw_domain, READ_ONCE(engine->fw_active));
2404 
2405 	rcu_read_lock();
2406 	rq = READ_ONCE(engine->heartbeat.systole);
2407 	if (rq)
2408 		drm_printf(m, "\tHeartbeat: %d ms ago\n",
2409 			   jiffies_to_msecs(jiffies - rq->emitted_jiffies));
2410 	rcu_read_unlock();
2411 	drm_printf(m, "\tReset count: %d (global %d)\n",
2412 		   i915_reset_engine_count(error, engine),
2413 		   i915_reset_count(error));
2414 	print_properties(engine, m);
2415 
2416 	engine_dump_active_requests(engine, m);
2417 
2418 	drm_printf(m, "\tMMIO base:  0x%08x\n", engine->mmio_base);
2419 	wakeref = intel_runtime_pm_get_if_in_use(engine->uncore->rpm);
2420 	if (wakeref) {
2421 		intel_engine_print_registers(engine, m);
2422 		intel_runtime_pm_put(engine->uncore->rpm, wakeref);
2423 	} else {
2424 		drm_printf(m, "\tDevice is asleep; skipping register dump\n");
2425 	}
2426 
2427 	intel_execlists_show_requests(engine, m, i915_request_show, 8);
2428 
2429 	drm_printf(m, "HWSP:\n");
2430 	hexdump(m, engine->status_page.addr, PAGE_SIZE);
2431 
2432 	drm_printf(m, "Idle? %s\n", str_yes_no(intel_engine_is_idle(engine)));
2433 
2434 	intel_engine_print_breadcrumbs(engine, m);
2435 }
2436 
2437 /**
2438  * intel_engine_get_busy_time() - Return current accumulated engine busyness
2439  * @engine: engine to report on
2440  * @now: monotonic timestamp of sampling
2441  *
2442  * Returns accumulated time @engine was busy since engine stats were enabled.
2443  */
2444 ktime_t intel_engine_get_busy_time(struct intel_engine_cs *engine, ktime_t *now)
2445 {
2446 	return engine->busyness(engine, now);
2447 }
2448 
2449 struct intel_context *
2450 intel_engine_create_virtual(struct intel_engine_cs **siblings,
2451 			    unsigned int count, unsigned long flags)
2452 {
2453 	if (count == 0)
2454 		return ERR_PTR(-EINVAL);
2455 
2456 	if (count == 1 && !(flags & FORCE_VIRTUAL))
2457 		return intel_context_create(siblings[0]);
2458 
2459 	GEM_BUG_ON(!siblings[0]->cops->create_virtual);
2460 	return siblings[0]->cops->create_virtual(siblings, count, flags);
2461 }
2462 
2463 static struct i915_request *engine_execlist_find_hung_request(struct intel_engine_cs *engine)
2464 {
2465 	struct i915_request *request, *active = NULL;
2466 
2467 	/*
2468 	 * This search does not work in GuC submission mode. However, the GuC
2469 	 * will report the hanging context directly to the driver itself. So
2470 	 * the driver should never get here when in GuC mode.
2471 	 */
2472 	GEM_BUG_ON(intel_uc_uses_guc_submission(&engine->gt->uc));
2473 
2474 	/*
2475 	 * We are called by the error capture, reset and to dump engine
2476 	 * state at random points in time. In particular, note that neither is
2477 	 * crucially ordered with an interrupt. After a hang, the GPU is dead
2478 	 * and we assume that no more writes can happen (we waited long enough
2479 	 * for all writes that were in transaction to be flushed) - adding an
2480 	 * extra delay for a recent interrupt is pointless. Hence, we do
2481 	 * not need an engine->irq_seqno_barrier() before the seqno reads.
2482 	 * At all other times, we must assume the GPU is still running, but
2483 	 * we only care about the snapshot of this moment.
2484 	 */
2485 	lockdep_assert_held(&engine->sched_engine->lock);
2486 
2487 	rcu_read_lock();
2488 	request = execlists_active(&engine->execlists);
2489 	if (request) {
2490 		struct intel_timeline *tl = request->context->timeline;
2491 
2492 		list_for_each_entry_from_reverse(request, &tl->requests, link) {
2493 			if (__i915_request_is_complete(request))
2494 				break;
2495 
2496 			active = request;
2497 		}
2498 	}
2499 	rcu_read_unlock();
2500 	if (active)
2501 		return active;
2502 
2503 	list_for_each_entry(request, &engine->sched_engine->requests,
2504 			    sched.link) {
2505 		if (i915_test_request_state(request) != I915_REQUEST_ACTIVE)
2506 			continue;
2507 
2508 		active = request;
2509 		break;
2510 	}
2511 
2512 	return active;
2513 }
2514 
2515 void intel_engine_get_hung_entity(struct intel_engine_cs *engine,
2516 				  struct intel_context **ce, struct i915_request **rq)
2517 {
2518 	unsigned long flags;
2519 
2520 	*ce = intel_engine_get_hung_context(engine);
2521 	if (*ce) {
2522 		intel_engine_clear_hung_context(engine);
2523 
2524 		*rq = intel_context_get_active_request(*ce);
2525 		return;
2526 	}
2527 
2528 	/*
2529 	 * Getting here with GuC enabled means it is a forced error capture
2530 	 * with no actual hang. So, no need to attempt the execlist search.
2531 	 */
2532 	if (intel_uc_uses_guc_submission(&engine->gt->uc))
2533 		return;
2534 
2535 	spin_lock_irqsave(&engine->sched_engine->lock, flags);
2536 	*rq = engine_execlist_find_hung_request(engine);
2537 	if (*rq)
2538 		*rq = i915_request_get_rcu(*rq);
2539 	spin_unlock_irqrestore(&engine->sched_engine->lock, flags);
2540 }
2541 
2542 void xehp_enable_ccs_engines(struct intel_engine_cs *engine)
2543 {
2544 	/*
2545 	 * If there are any non-fused-off CCS engines, we need to enable CCS
2546 	 * support in the RCU_MODE register.  This only needs to be done once,
2547 	 * so for simplicity we'll take care of this in the RCS engine's
2548 	 * resume handler; since the RCS and all CCS engines belong to the
2549 	 * same reset domain and are reset together, this will also take care
2550 	 * of re-applying the setting after i915-triggered resets.
2551 	 */
2552 	if (!CCS_MASK(engine->gt))
2553 		return;
2554 
2555 	intel_uncore_write(engine->uncore, GEN12_RCU_MODE,
2556 			   REG_MASKED_FIELD_ENABLE(GEN12_RCU_MODE_CCS_ENABLE));
2557 }
2558 
2559 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
2560 #include "mock_engine.c"
2561 #include "selftest_engine.c"
2562 #include "selftest_engine_cs.c"
2563 #endif
2564