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