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