• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
intel_engine_context_size(struct intel_gt * gt,u8 class)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 
__engine_mmio_base(struct drm_i915_private * i915,const struct engine_mmio_base * bases)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 
__sprint_engine_name(struct intel_engine_cs * engine)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 
intel_engine_set_hwsp_writemask(struct intel_engine_cs * engine,u32 mask)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 
intel_engine_sanitize_mmio(struct intel_engine_cs * engine)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 
nop_irq_handler(struct intel_engine_cs * engine,u16 iir)288 static void nop_irq_handler(struct intel_engine_cs *engine, u16 iir)
289 {
290 	GEM_DEBUG_WARN_ON(iir);
291 }
292 
intel_engine_setup(struct intel_gt * gt,enum intel_engine_id id)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 	INIT_LIST_HEAD(&engine->pinned_contexts_list);
324 	engine->id = id;
325 	engine->legacy_idx = INVALID_ENGINE;
326 	engine->mask = BIT(id);
327 	engine->i915 = i915;
328 	engine->gt = gt;
329 	engine->uncore = gt->uncore;
330 	guc_class = engine_class_to_guc_class(info->class);
331 	engine->guc_id = MAKE_GUC_ID(guc_class, info->instance);
332 	engine->mmio_base = __engine_mmio_base(i915, info->mmio_bases);
333 
334 	engine->irq_handler = nop_irq_handler;
335 
336 	engine->class = info->class;
337 	engine->instance = info->instance;
338 	__sprint_engine_name(engine);
339 
340 	engine->props.heartbeat_interval_ms =
341 		CONFIG_DRM_I915_HEARTBEAT_INTERVAL;
342 	engine->props.max_busywait_duration_ns =
343 		CONFIG_DRM_I915_MAX_REQUEST_BUSYWAIT;
344 	engine->props.preempt_timeout_ms =
345 		CONFIG_DRM_I915_PREEMPT_TIMEOUT;
346 	engine->props.stop_timeout_ms =
347 		CONFIG_DRM_I915_STOP_TIMEOUT;
348 	engine->props.timeslice_duration_ms =
349 		CONFIG_DRM_I915_TIMESLICE_DURATION;
350 
351 	/* Override to uninterruptible for OpenCL workloads. */
352 	if (GRAPHICS_VER(i915) == 12 && engine->class == RENDER_CLASS)
353 		engine->props.preempt_timeout_ms = 0;
354 
355 	engine->defaults = engine->props; /* never to change again */
356 
357 	engine->context_size = intel_engine_context_size(gt, engine->class);
358 	if (WARN_ON(engine->context_size > BIT(20)))
359 		engine->context_size = 0;
360 	if (engine->context_size)
361 		DRIVER_CAPS(i915)->has_logical_contexts = true;
362 
363 	ewma__engine_latency_init(&engine->latency);
364 	seqcount_init(&engine->stats.lock);
365 
366 	ATOMIC_INIT_NOTIFIER_HEAD(&engine->context_status_notifier);
367 
368 	/* Scrub mmio state on takeover */
369 	intel_engine_sanitize_mmio(engine);
370 
371 	gt->engine_class[info->class][info->instance] = engine;
372 	gt->engine[id] = engine;
373 
374 	return 0;
375 }
376 
__setup_engine_capabilities(struct intel_engine_cs * engine)377 static void __setup_engine_capabilities(struct intel_engine_cs *engine)
378 {
379 	struct drm_i915_private *i915 = engine->i915;
380 
381 	if (engine->class == VIDEO_DECODE_CLASS) {
382 		/*
383 		 * HEVC support is present on first engine instance
384 		 * before Gen11 and on all instances afterwards.
385 		 */
386 		if (GRAPHICS_VER(i915) >= 11 ||
387 		    (GRAPHICS_VER(i915) >= 9 && engine->instance == 0))
388 			engine->uabi_capabilities |=
389 				I915_VIDEO_CLASS_CAPABILITY_HEVC;
390 
391 		/*
392 		 * SFC block is present only on even logical engine
393 		 * instances.
394 		 */
395 		if ((GRAPHICS_VER(i915) >= 11 &&
396 		     (engine->gt->info.vdbox_sfc_access &
397 		      BIT(engine->instance))) ||
398 		    (GRAPHICS_VER(i915) >= 9 && engine->instance == 0))
399 			engine->uabi_capabilities |=
400 				I915_VIDEO_AND_ENHANCE_CLASS_CAPABILITY_SFC;
401 	} else if (engine->class == VIDEO_ENHANCEMENT_CLASS) {
402 		if (GRAPHICS_VER(i915) >= 9)
403 			engine->uabi_capabilities |=
404 				I915_VIDEO_AND_ENHANCE_CLASS_CAPABILITY_SFC;
405 	}
406 }
407 
intel_setup_engine_capabilities(struct intel_gt * gt)408 static void intel_setup_engine_capabilities(struct intel_gt *gt)
409 {
410 	struct intel_engine_cs *engine;
411 	enum intel_engine_id id;
412 
413 	for_each_engine(engine, gt, id)
414 		__setup_engine_capabilities(engine);
415 }
416 
417 /**
418  * intel_engines_release() - free the resources allocated for Command Streamers
419  * @gt: pointer to struct intel_gt
420  */
intel_engines_release(struct intel_gt * gt)421 void intel_engines_release(struct intel_gt *gt)
422 {
423 	struct intel_engine_cs *engine;
424 	enum intel_engine_id id;
425 
426 	/*
427 	 * Before we release the resources held by engine, we must be certain
428 	 * that the HW is no longer accessing them -- having the GPU scribble
429 	 * to or read from a page being used for something else causes no end
430 	 * of fun.
431 	 *
432 	 * The GPU should be reset by this point, but assume the worst just
433 	 * in case we aborted before completely initialising the engines.
434 	 */
435 	GEM_BUG_ON(intel_gt_pm_is_awake(gt));
436 	if (!INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
437 		__intel_gt_reset(gt, ALL_ENGINES);
438 
439 	/* Decouple the backend; but keep the layout for late GPU resets */
440 	for_each_engine(engine, gt, id) {
441 		if (!engine->release)
442 			continue;
443 
444 		intel_wakeref_wait_for_idle(&engine->wakeref);
445 		GEM_BUG_ON(intel_engine_pm_is_awake(engine));
446 
447 		engine->release(engine);
448 		engine->release = NULL;
449 
450 		memset(&engine->reset, 0, sizeof(engine->reset));
451 	}
452 }
453 
intel_engine_free_request_pool(struct intel_engine_cs * engine)454 void intel_engine_free_request_pool(struct intel_engine_cs *engine)
455 {
456 	if (!engine->request_pool)
457 		return;
458 
459 	kmem_cache_free(i915_request_slab_cache(), engine->request_pool);
460 }
461 
intel_engines_free(struct intel_gt * gt)462 void intel_engines_free(struct intel_gt *gt)
463 {
464 	struct intel_engine_cs *engine;
465 	enum intel_engine_id id;
466 
467 	/* Free the requests! dma-resv keeps fences around for an eternity */
468 	rcu_barrier();
469 
470 	for_each_engine(engine, gt, id) {
471 		intel_engine_free_request_pool(engine);
472 		kfree(engine);
473 		gt->engine[id] = NULL;
474 	}
475 }
476 
477 static
gen11_vdbox_has_sfc(struct drm_i915_private * i915,unsigned int physical_vdbox,unsigned int logical_vdbox,u16 vdbox_mask)478 bool gen11_vdbox_has_sfc(struct drm_i915_private *i915,
479 			 unsigned int physical_vdbox,
480 			 unsigned int logical_vdbox, u16 vdbox_mask)
481 {
482 	/*
483 	 * In Gen11, only even numbered logical VDBOXes are hooked
484 	 * up to an SFC (Scaler & Format Converter) unit.
485 	 * In Gen12, Even numbered physical instance always are connected
486 	 * to an SFC. Odd numbered physical instances have SFC only if
487 	 * previous even instance is fused off.
488 	 */
489 	if (GRAPHICS_VER(i915) == 12)
490 		return (physical_vdbox % 2 == 0) ||
491 			!(BIT(physical_vdbox - 1) & vdbox_mask);
492 	else if (GRAPHICS_VER(i915) == 11)
493 		return logical_vdbox % 2 == 0;
494 
495 	MISSING_CASE(GRAPHICS_VER(i915));
496 	return false;
497 }
498 
499 /*
500  * Determine which engines are fused off in our particular hardware.
501  * Note that we have a catch-22 situation where we need to be able to access
502  * the blitter forcewake domain to read the engine fuses, but at the same time
503  * we need to know which engines are available on the system to know which
504  * forcewake domains are present. We solve this by intializing the forcewake
505  * domains based on the full engine mask in the platform capabilities before
506  * calling this function and pruning the domains for fused-off engines
507  * afterwards.
508  */
init_engine_mask(struct intel_gt * gt)509 static intel_engine_mask_t init_engine_mask(struct intel_gt *gt)
510 {
511 	struct drm_i915_private *i915 = gt->i915;
512 	struct intel_gt_info *info = &gt->info;
513 	struct intel_uncore *uncore = gt->uncore;
514 	unsigned int logical_vdbox = 0;
515 	unsigned int i;
516 	u32 media_fuse;
517 	u16 vdbox_mask;
518 	u16 vebox_mask;
519 
520 	info->engine_mask = INTEL_INFO(i915)->platform_engine_mask;
521 
522 	if (GRAPHICS_VER(i915) < 11)
523 		return info->engine_mask;
524 
525 	/*
526 	 * On newer platforms the fusing register is called 'enable' and has
527 	 * enable semantics, while on older platforms it is called 'disable'
528 	 * and bits have disable semantices.
529 	 */
530 	media_fuse = intel_uncore_read(uncore, GEN11_GT_VEBOX_VDBOX_DISABLE);
531 	if (GRAPHICS_VER_FULL(i915) < IP_VER(12, 50))
532 		media_fuse = ~media_fuse;
533 
534 	vdbox_mask = media_fuse & GEN11_GT_VDBOX_DISABLE_MASK;
535 	vebox_mask = (media_fuse & GEN11_GT_VEBOX_DISABLE_MASK) >>
536 		      GEN11_GT_VEBOX_DISABLE_SHIFT;
537 
538 	for (i = 0; i < I915_MAX_VCS; i++) {
539 		if (!HAS_ENGINE(gt, _VCS(i))) {
540 			vdbox_mask &= ~BIT(i);
541 			continue;
542 		}
543 
544 		if (!(BIT(i) & vdbox_mask)) {
545 			info->engine_mask &= ~BIT(_VCS(i));
546 			drm_dbg(&i915->drm, "vcs%u fused off\n", i);
547 			continue;
548 		}
549 
550 		if (gen11_vdbox_has_sfc(i915, i, logical_vdbox, vdbox_mask))
551 			gt->info.vdbox_sfc_access |= BIT(i);
552 		logical_vdbox++;
553 	}
554 	drm_dbg(&i915->drm, "vdbox enable: %04x, instances: %04lx\n",
555 		vdbox_mask, VDBOX_MASK(gt));
556 	GEM_BUG_ON(vdbox_mask != VDBOX_MASK(gt));
557 
558 	for (i = 0; i < I915_MAX_VECS; i++) {
559 		if (!HAS_ENGINE(gt, _VECS(i))) {
560 			vebox_mask &= ~BIT(i);
561 			continue;
562 		}
563 
564 		if (!(BIT(i) & vebox_mask)) {
565 			info->engine_mask &= ~BIT(_VECS(i));
566 			drm_dbg(&i915->drm, "vecs%u fused off\n", i);
567 		}
568 	}
569 	drm_dbg(&i915->drm, "vebox enable: %04x, instances: %04lx\n",
570 		vebox_mask, VEBOX_MASK(gt));
571 	GEM_BUG_ON(vebox_mask != VEBOX_MASK(gt));
572 
573 	return info->engine_mask;
574 }
575 
576 /**
577  * intel_engines_init_mmio() - allocate and prepare the Engine Command Streamers
578  * @gt: pointer to struct intel_gt
579  *
580  * Return: non-zero if the initialization failed.
581  */
intel_engines_init_mmio(struct intel_gt * gt)582 int intel_engines_init_mmio(struct intel_gt *gt)
583 {
584 	struct drm_i915_private *i915 = gt->i915;
585 	const unsigned int engine_mask = init_engine_mask(gt);
586 	unsigned int mask = 0;
587 	unsigned int i;
588 	int err;
589 
590 	drm_WARN_ON(&i915->drm, engine_mask == 0);
591 	drm_WARN_ON(&i915->drm, engine_mask &
592 		    GENMASK(BITS_PER_TYPE(mask) - 1, I915_NUM_ENGINES));
593 
594 	if (i915_inject_probe_failure(i915))
595 		return -ENODEV;
596 
597 	for (i = 0; i < ARRAY_SIZE(intel_engines); i++) {
598 		if (!HAS_ENGINE(gt, i))
599 			continue;
600 
601 		err = intel_engine_setup(gt, i);
602 		if (err)
603 			goto cleanup;
604 
605 		mask |= BIT(i);
606 	}
607 
608 	/*
609 	 * Catch failures to update intel_engines table when the new engines
610 	 * are added to the driver by a warning and disabling the forgotten
611 	 * engines.
612 	 */
613 	if (drm_WARN_ON(&i915->drm, mask != engine_mask))
614 		gt->info.engine_mask = mask;
615 
616 	gt->info.num_engines = hweight32(mask);
617 
618 	intel_gt_check_and_clear_faults(gt);
619 
620 	intel_setup_engine_capabilities(gt);
621 
622 	intel_uncore_prune_engine_fw_domains(gt->uncore, gt);
623 
624 	return 0;
625 
626 cleanup:
627 	intel_engines_free(gt);
628 	return err;
629 }
630 
intel_engine_init_execlists(struct intel_engine_cs * engine)631 void intel_engine_init_execlists(struct intel_engine_cs *engine)
632 {
633 	struct intel_engine_execlists * const execlists = &engine->execlists;
634 
635 	execlists->port_mask = 1;
636 	GEM_BUG_ON(!is_power_of_2(execlists_num_ports(execlists)));
637 	GEM_BUG_ON(execlists_num_ports(execlists) > EXECLIST_MAX_PORTS);
638 
639 	memset(execlists->pending, 0, sizeof(execlists->pending));
640 	execlists->active =
641 		memset(execlists->inflight, 0, sizeof(execlists->inflight));
642 }
643 
cleanup_status_page(struct intel_engine_cs * engine)644 static void cleanup_status_page(struct intel_engine_cs *engine)
645 {
646 	struct i915_vma *vma;
647 
648 	/* Prevent writes into HWSP after returning the page to the system */
649 	intel_engine_set_hwsp_writemask(engine, ~0u);
650 
651 	vma = fetch_and_zero(&engine->status_page.vma);
652 	if (!vma)
653 		return;
654 
655 	if (!HWS_NEEDS_PHYSICAL(engine->i915))
656 		i915_vma_unpin(vma);
657 
658 	i915_gem_object_unpin_map(vma->obj);
659 	i915_gem_object_put(vma->obj);
660 }
661 
pin_ggtt_status_page(struct intel_engine_cs * engine,struct i915_gem_ww_ctx * ww,struct i915_vma * vma)662 static int pin_ggtt_status_page(struct intel_engine_cs *engine,
663 				struct i915_gem_ww_ctx *ww,
664 				struct i915_vma *vma)
665 {
666 	unsigned int flags;
667 
668 	if (!HAS_LLC(engine->i915) && i915_ggtt_has_aperture(engine->gt->ggtt))
669 		/*
670 		 * On g33, we cannot place HWS above 256MiB, so
671 		 * restrict its pinning to the low mappable arena.
672 		 * Though this restriction is not documented for
673 		 * gen4, gen5, or byt, they also behave similarly
674 		 * and hang if the HWS is placed at the top of the
675 		 * GTT. To generalise, it appears that all !llc
676 		 * platforms have issues with us placing the HWS
677 		 * above the mappable region (even though we never
678 		 * actually map it).
679 		 */
680 		flags = PIN_MAPPABLE;
681 	else
682 		flags = PIN_HIGH;
683 
684 	return i915_ggtt_pin(vma, ww, 0, flags);
685 }
686 
init_status_page(struct intel_engine_cs * engine)687 static int init_status_page(struct intel_engine_cs *engine)
688 {
689 	struct drm_i915_gem_object *obj;
690 	struct i915_gem_ww_ctx ww;
691 	struct i915_vma *vma;
692 	void *vaddr;
693 	int ret;
694 
695 	INIT_LIST_HEAD(&engine->status_page.timelines);
696 
697 	/*
698 	 * Though the HWS register does support 36bit addresses, historically
699 	 * we have had hangs and corruption reported due to wild writes if
700 	 * the HWS is placed above 4G. We only allow objects to be allocated
701 	 * in GFP_DMA32 for i965, and no earlier physical address users had
702 	 * access to more than 4G.
703 	 */
704 	obj = i915_gem_object_create_internal(engine->i915, PAGE_SIZE);
705 	if (IS_ERR(obj)) {
706 		drm_err(&engine->i915->drm,
707 			"Failed to allocate status page\n");
708 		return PTR_ERR(obj);
709 	}
710 
711 	i915_gem_object_set_cache_coherency(obj, I915_CACHE_LLC);
712 
713 	vma = i915_vma_instance(obj, &engine->gt->ggtt->vm, NULL);
714 	if (IS_ERR(vma)) {
715 		ret = PTR_ERR(vma);
716 		goto err_put;
717 	}
718 
719 	i915_gem_ww_ctx_init(&ww, true);
720 retry:
721 	ret = i915_gem_object_lock(obj, &ww);
722 	if (!ret && !HWS_NEEDS_PHYSICAL(engine->i915))
723 		ret = pin_ggtt_status_page(engine, &ww, vma);
724 	if (ret)
725 		goto err;
726 
727 	vaddr = i915_gem_object_pin_map(obj, I915_MAP_WB);
728 	if (IS_ERR(vaddr)) {
729 		ret = PTR_ERR(vaddr);
730 		goto err_unpin;
731 	}
732 
733 	engine->status_page.addr = memset(vaddr, 0, PAGE_SIZE);
734 	engine->status_page.vma = vma;
735 
736 err_unpin:
737 	if (ret)
738 		i915_vma_unpin(vma);
739 err:
740 	if (ret == -EDEADLK) {
741 		ret = i915_gem_ww_ctx_backoff(&ww);
742 		if (!ret)
743 			goto retry;
744 	}
745 	i915_gem_ww_ctx_fini(&ww);
746 err_put:
747 	if (ret)
748 		i915_gem_object_put(obj);
749 	return ret;
750 }
751 
engine_setup_common(struct intel_engine_cs * engine)752 static int engine_setup_common(struct intel_engine_cs *engine)
753 {
754 	int err;
755 
756 	init_llist_head(&engine->barrier_tasks);
757 
758 	err = init_status_page(engine);
759 	if (err)
760 		return err;
761 
762 	engine->breadcrumbs = intel_breadcrumbs_create(engine);
763 	if (!engine->breadcrumbs) {
764 		err = -ENOMEM;
765 		goto err_status;
766 	}
767 
768 	engine->sched_engine = i915_sched_engine_create(ENGINE_PHYSICAL);
769 	if (!engine->sched_engine) {
770 		err = -ENOMEM;
771 		goto err_sched_engine;
772 	}
773 	engine->sched_engine->private_data = engine;
774 
775 	err = intel_engine_init_cmd_parser(engine);
776 	if (err)
777 		goto err_cmd_parser;
778 
779 	intel_engine_init_execlists(engine);
780 	intel_engine_init__pm(engine);
781 	intel_engine_init_retire(engine);
782 
783 	/* Use the whole device by default */
784 	engine->sseu =
785 		intel_sseu_from_device_info(&engine->gt->info.sseu);
786 
787 	intel_engine_init_workarounds(engine);
788 	intel_engine_init_whitelist(engine);
789 	intel_engine_init_ctx_wa(engine);
790 
791 	if (GRAPHICS_VER(engine->i915) >= 12)
792 		engine->flags |= I915_ENGINE_HAS_RELATIVE_MMIO;
793 
794 	return 0;
795 
796 err_cmd_parser:
797 	i915_sched_engine_put(engine->sched_engine);
798 err_sched_engine:
799 	intel_breadcrumbs_put(engine->breadcrumbs);
800 err_status:
801 	cleanup_status_page(engine);
802 	return err;
803 }
804 
805 struct measure_breadcrumb {
806 	struct i915_request rq;
807 	struct intel_ring ring;
808 	u32 cs[2048];
809 };
810 
measure_breadcrumb_dw(struct intel_context * ce)811 static int measure_breadcrumb_dw(struct intel_context *ce)
812 {
813 	struct intel_engine_cs *engine = ce->engine;
814 	struct measure_breadcrumb *frame;
815 	int dw;
816 
817 	GEM_BUG_ON(!engine->gt->scratch);
818 
819 	frame = kzalloc(sizeof(*frame), GFP_KERNEL);
820 	if (!frame)
821 		return -ENOMEM;
822 
823 	frame->rq.engine = engine;
824 	frame->rq.context = ce;
825 	rcu_assign_pointer(frame->rq.timeline, ce->timeline);
826 	frame->rq.hwsp_seqno = ce->timeline->hwsp_seqno;
827 
828 	frame->ring.vaddr = frame->cs;
829 	frame->ring.size = sizeof(frame->cs);
830 	frame->ring.wrap =
831 		BITS_PER_TYPE(frame->ring.size) - ilog2(frame->ring.size);
832 	frame->ring.effective_size = frame->ring.size;
833 	intel_ring_update_space(&frame->ring);
834 	frame->rq.ring = &frame->ring;
835 
836 	mutex_lock(&ce->timeline->mutex);
837 	spin_lock_irq(&engine->sched_engine->lock);
838 
839 	dw = engine->emit_fini_breadcrumb(&frame->rq, frame->cs) - frame->cs;
840 
841 	spin_unlock_irq(&engine->sched_engine->lock);
842 	mutex_unlock(&ce->timeline->mutex);
843 
844 	GEM_BUG_ON(dw & 1); /* RING_TAIL must be qword aligned */
845 
846 	kfree(frame);
847 	return dw;
848 }
849 
850 struct intel_context *
intel_engine_create_pinned_context(struct intel_engine_cs * engine,struct i915_address_space * vm,unsigned int ring_size,unsigned int hwsp,struct lock_class_key * key,const char * name)851 intel_engine_create_pinned_context(struct intel_engine_cs *engine,
852 				   struct i915_address_space *vm,
853 				   unsigned int ring_size,
854 				   unsigned int hwsp,
855 				   struct lock_class_key *key,
856 				   const char *name)
857 {
858 	struct intel_context *ce;
859 	int err;
860 
861 	ce = intel_context_create(engine);
862 	if (IS_ERR(ce))
863 		return ce;
864 
865 	__set_bit(CONTEXT_BARRIER_BIT, &ce->flags);
866 	ce->timeline = page_pack_bits(NULL, hwsp);
867 	ce->ring = NULL;
868 	ce->ring_size = ring_size;
869 
870 	i915_vm_put(ce->vm);
871 	ce->vm = i915_vm_get(vm);
872 
873 	err = intel_context_pin(ce); /* perma-pin so it is always available */
874 	if (err) {
875 		intel_context_put(ce);
876 		return ERR_PTR(err);
877 	}
878 
879 	list_add_tail(&ce->pinned_contexts_link, &engine->pinned_contexts_list);
880 
881 	/*
882 	 * Give our perma-pinned kernel timelines a separate lockdep class,
883 	 * so that we can use them from within the normal user timelines
884 	 * should we need to inject GPU operations during their request
885 	 * construction.
886 	 */
887 	lockdep_set_class_and_name(&ce->timeline->mutex, key, name);
888 
889 	return ce;
890 }
891 
intel_engine_destroy_pinned_context(struct intel_context * ce)892 void intel_engine_destroy_pinned_context(struct intel_context *ce)
893 {
894 	struct intel_engine_cs *engine = ce->engine;
895 	struct i915_vma *hwsp = engine->status_page.vma;
896 
897 	GEM_BUG_ON(ce->timeline->hwsp_ggtt != hwsp);
898 
899 	mutex_lock(&hwsp->vm->mutex);
900 	list_del(&ce->timeline->engine_link);
901 	mutex_unlock(&hwsp->vm->mutex);
902 
903 	list_del(&ce->pinned_contexts_link);
904 	intel_context_unpin(ce);
905 	intel_context_put(ce);
906 }
907 
908 static struct intel_context *
create_kernel_context(struct intel_engine_cs * engine)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  */
engine_init_common(struct intel_engine_cs * engine)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 
intel_engines_init(struct intel_gt * gt)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  */
intel_engine_cleanup_common(struct intel_engine_cs * engine)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 		fput(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  */
intel_engine_resume(struct intel_engine_cs * engine)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 
intel_engine_get_active_head(const struct intel_engine_cs * engine)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 
intel_engine_get_last_batch_head(const struct intel_engine_cs * engine)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 
stop_timeout(const struct intel_engine_cs * engine)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 
__intel_engine_stop_cs(struct intel_engine_cs * engine,int fast_timeout_us,int slow_timeout_ms)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 
intel_engine_stop_cs(struct intel_engine_cs * engine)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 
intel_engine_cancel_stop_cs(struct intel_engine_cs * engine)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 
i915_cache_level_str(struct drm_i915_private * i915,int type)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
read_subslice_reg(const struct intel_engine_cs * engine,int slice,int subslice,i915_reg_t reg)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 */
intel_engine_get_instdone(const struct intel_engine_cs * engine,struct intel_instdone * instdone)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 
ring_is_idle(struct intel_engine_cs * engine)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 
__intel_engine_flush_submission(struct intel_engine_cs * engine,bool sync)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  */
intel_engine_is_idle(struct intel_engine_cs * engine)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 
intel_engines_are_idle(struct intel_gt * gt)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 
intel_engine_irq_enable(struct intel_engine_cs * engine)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 
intel_engine_irq_disable(struct intel_engine_cs * engine)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 
intel_engines_reset_default_submission(struct intel_gt * gt)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 
intel_engine_can_store_dword(struct intel_engine_cs * engine)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 
get_timeline(struct i915_request * rq)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 
print_ring(char * buf,int sz,struct i915_request * rq)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 
hexdump(struct drm_printer * m,const void * buf,size_t len)1425 static void hexdump(struct drm_printer *m, const void *buf, size_t len)
1426 {
1427 	const size_t rowsize = 8 * sizeof(u32);
1428 	const void *prev = NULL;
1429 	bool skip = false;
1430 	size_t pos;
1431 
1432 	for (pos = 0; pos < len; pos += rowsize) {
1433 		char line[128];
1434 
1435 		if (prev && !memcmp(prev, buf + pos, rowsize)) {
1436 			if (!skip) {
1437 				drm_printf(m, "*\n");
1438 				skip = true;
1439 			}
1440 			continue;
1441 		}
1442 
1443 		WARN_ON_ONCE(hex_dump_to_buffer(buf + pos, len - pos,
1444 						rowsize, sizeof(u32),
1445 						line, sizeof(line),
1446 						false) >= sizeof(line));
1447 		drm_printf(m, "[%04zx] %s\n", pos, line);
1448 
1449 		prev = buf + pos;
1450 		skip = false;
1451 	}
1452 }
1453 
repr_timer(const struct timer_list * t)1454 static const char *repr_timer(const struct timer_list *t)
1455 {
1456 	if (!READ_ONCE(t->expires))
1457 		return "inactive";
1458 
1459 	if (timer_pending(t))
1460 		return "active";
1461 
1462 	return "expired";
1463 }
1464 
intel_engine_print_registers(struct intel_engine_cs * engine,struct drm_printer * m)1465 static void intel_engine_print_registers(struct intel_engine_cs *engine,
1466 					 struct drm_printer *m)
1467 {
1468 	struct drm_i915_private *dev_priv = engine->i915;
1469 	struct intel_engine_execlists * const execlists = &engine->execlists;
1470 	u64 addr;
1471 
1472 	if (engine->id == RENDER_CLASS && IS_GRAPHICS_VER(dev_priv, 4, 7))
1473 		drm_printf(m, "\tCCID: 0x%08x\n", ENGINE_READ(engine, CCID));
1474 	if (HAS_EXECLISTS(dev_priv)) {
1475 		drm_printf(m, "\tEL_STAT_HI: 0x%08x\n",
1476 			   ENGINE_READ(engine, RING_EXECLIST_STATUS_HI));
1477 		drm_printf(m, "\tEL_STAT_LO: 0x%08x\n",
1478 			   ENGINE_READ(engine, RING_EXECLIST_STATUS_LO));
1479 	}
1480 	drm_printf(m, "\tRING_START: 0x%08x\n",
1481 		   ENGINE_READ(engine, RING_START));
1482 	drm_printf(m, "\tRING_HEAD:  0x%08x\n",
1483 		   ENGINE_READ(engine, RING_HEAD) & HEAD_ADDR);
1484 	drm_printf(m, "\tRING_TAIL:  0x%08x\n",
1485 		   ENGINE_READ(engine, RING_TAIL) & TAIL_ADDR);
1486 	drm_printf(m, "\tRING_CTL:   0x%08x%s\n",
1487 		   ENGINE_READ(engine, RING_CTL),
1488 		   ENGINE_READ(engine, RING_CTL) & (RING_WAIT | RING_WAIT_SEMAPHORE) ? " [waiting]" : "");
1489 	if (GRAPHICS_VER(engine->i915) > 2) {
1490 		drm_printf(m, "\tRING_MODE:  0x%08x%s\n",
1491 			   ENGINE_READ(engine, RING_MI_MODE),
1492 			   ENGINE_READ(engine, RING_MI_MODE) & (MODE_IDLE) ? " [idle]" : "");
1493 	}
1494 
1495 	if (GRAPHICS_VER(dev_priv) >= 6) {
1496 		drm_printf(m, "\tRING_IMR:   0x%08x\n",
1497 			   ENGINE_READ(engine, RING_IMR));
1498 		drm_printf(m, "\tRING_ESR:   0x%08x\n",
1499 			   ENGINE_READ(engine, RING_ESR));
1500 		drm_printf(m, "\tRING_EMR:   0x%08x\n",
1501 			   ENGINE_READ(engine, RING_EMR));
1502 		drm_printf(m, "\tRING_EIR:   0x%08x\n",
1503 			   ENGINE_READ(engine, RING_EIR));
1504 	}
1505 
1506 	addr = intel_engine_get_active_head(engine);
1507 	drm_printf(m, "\tACTHD:  0x%08x_%08x\n",
1508 		   upper_32_bits(addr), lower_32_bits(addr));
1509 	addr = intel_engine_get_last_batch_head(engine);
1510 	drm_printf(m, "\tBBADDR: 0x%08x_%08x\n",
1511 		   upper_32_bits(addr), lower_32_bits(addr));
1512 	if (GRAPHICS_VER(dev_priv) >= 8)
1513 		addr = ENGINE_READ64(engine, RING_DMA_FADD, RING_DMA_FADD_UDW);
1514 	else if (GRAPHICS_VER(dev_priv) >= 4)
1515 		addr = ENGINE_READ(engine, RING_DMA_FADD);
1516 	else
1517 		addr = ENGINE_READ(engine, DMA_FADD_I8XX);
1518 	drm_printf(m, "\tDMA_FADDR: 0x%08x_%08x\n",
1519 		   upper_32_bits(addr), lower_32_bits(addr));
1520 	if (GRAPHICS_VER(dev_priv) >= 4) {
1521 		drm_printf(m, "\tIPEIR: 0x%08x\n",
1522 			   ENGINE_READ(engine, RING_IPEIR));
1523 		drm_printf(m, "\tIPEHR: 0x%08x\n",
1524 			   ENGINE_READ(engine, RING_IPEHR));
1525 	} else {
1526 		drm_printf(m, "\tIPEIR: 0x%08x\n", ENGINE_READ(engine, IPEIR));
1527 		drm_printf(m, "\tIPEHR: 0x%08x\n", ENGINE_READ(engine, IPEHR));
1528 	}
1529 
1530 	if (intel_engine_uses_guc(engine)) {
1531 		/* nothing to print yet */
1532 	} else if (HAS_EXECLISTS(dev_priv)) {
1533 		struct i915_request * const *port, *rq;
1534 		const u32 *hws =
1535 			&engine->status_page.addr[I915_HWS_CSB_BUF0_INDEX];
1536 		const u8 num_entries = execlists->csb_size;
1537 		unsigned int idx;
1538 		u8 read, write;
1539 
1540 		drm_printf(m, "\tExeclist tasklet queued? %s (%s), preempt? %s, timeslice? %s\n",
1541 			   yesno(test_bit(TASKLET_STATE_SCHED,
1542 					  &engine->sched_engine->tasklet.state)),
1543 			   enableddisabled(!atomic_read(&engine->sched_engine->tasklet.count)),
1544 			   repr_timer(&engine->execlists.preempt),
1545 			   repr_timer(&engine->execlists.timer));
1546 
1547 		read = execlists->csb_head;
1548 		write = READ_ONCE(*execlists->csb_write);
1549 
1550 		drm_printf(m, "\tExeclist status: 0x%08x %08x; CSB read:%d, write:%d, entries:%d\n",
1551 			   ENGINE_READ(engine, RING_EXECLIST_STATUS_LO),
1552 			   ENGINE_READ(engine, RING_EXECLIST_STATUS_HI),
1553 			   read, write, num_entries);
1554 
1555 		if (read >= num_entries)
1556 			read = 0;
1557 		if (write >= num_entries)
1558 			write = 0;
1559 		if (read > write)
1560 			write += num_entries;
1561 		while (read < write) {
1562 			idx = ++read % num_entries;
1563 			drm_printf(m, "\tExeclist CSB[%d]: 0x%08x, context: %d\n",
1564 				   idx, hws[idx * 2], hws[idx * 2 + 1]);
1565 		}
1566 
1567 		i915_sched_engine_active_lock_bh(engine->sched_engine);
1568 		rcu_read_lock();
1569 		for (port = execlists->active; (rq = *port); port++) {
1570 			char hdr[160];
1571 			int len;
1572 
1573 			len = scnprintf(hdr, sizeof(hdr),
1574 					"\t\tActive[%d]:  ccid:%08x%s%s, ",
1575 					(int)(port - execlists->active),
1576 					rq->context->lrc.ccid,
1577 					intel_context_is_closed(rq->context) ? "!" : "",
1578 					intel_context_is_banned(rq->context) ? "*" : "");
1579 			len += print_ring(hdr + len, sizeof(hdr) - len, rq);
1580 			scnprintf(hdr + len, sizeof(hdr) - len, "rq: ");
1581 			i915_request_show(m, rq, hdr, 0);
1582 		}
1583 		for (port = execlists->pending; (rq = *port); port++) {
1584 			char hdr[160];
1585 			int len;
1586 
1587 			len = scnprintf(hdr, sizeof(hdr),
1588 					"\t\tPending[%d]: ccid:%08x%s%s, ",
1589 					(int)(port - execlists->pending),
1590 					rq->context->lrc.ccid,
1591 					intel_context_is_closed(rq->context) ? "!" : "",
1592 					intel_context_is_banned(rq->context) ? "*" : "");
1593 			len += print_ring(hdr + len, sizeof(hdr) - len, rq);
1594 			scnprintf(hdr + len, sizeof(hdr) - len, "rq: ");
1595 			i915_request_show(m, rq, hdr, 0);
1596 		}
1597 		rcu_read_unlock();
1598 		i915_sched_engine_active_unlock_bh(engine->sched_engine);
1599 	} else if (GRAPHICS_VER(dev_priv) > 6) {
1600 		drm_printf(m, "\tPP_DIR_BASE: 0x%08x\n",
1601 			   ENGINE_READ(engine, RING_PP_DIR_BASE));
1602 		drm_printf(m, "\tPP_DIR_BASE_READ: 0x%08x\n",
1603 			   ENGINE_READ(engine, RING_PP_DIR_BASE_READ));
1604 		drm_printf(m, "\tPP_DIR_DCLV: 0x%08x\n",
1605 			   ENGINE_READ(engine, RING_PP_DIR_DCLV));
1606 	}
1607 }
1608 
print_request_ring(struct drm_printer * m,struct i915_request * rq)1609 static void print_request_ring(struct drm_printer *m, struct i915_request *rq)
1610 {
1611 	void *ring;
1612 	int size;
1613 
1614 	drm_printf(m,
1615 		   "[head %04x, postfix %04x, tail %04x, batch 0x%08x_%08x]:\n",
1616 		   rq->head, rq->postfix, rq->tail,
1617 		   rq->batch ? upper_32_bits(rq->batch->node.start) : ~0u,
1618 		   rq->batch ? lower_32_bits(rq->batch->node.start) : ~0u);
1619 
1620 	size = rq->tail - rq->head;
1621 	if (rq->tail < rq->head)
1622 		size += rq->ring->size;
1623 
1624 	ring = kmalloc(size, GFP_ATOMIC);
1625 	if (ring) {
1626 		const void *vaddr = rq->ring->vaddr;
1627 		unsigned int head = rq->head;
1628 		unsigned int len = 0;
1629 
1630 		if (rq->tail < head) {
1631 			len = rq->ring->size - head;
1632 			memcpy(ring, vaddr + head, len);
1633 			head = 0;
1634 		}
1635 		memcpy(ring + len, vaddr + head, size - len);
1636 
1637 		hexdump(m, ring, size);
1638 		kfree(ring);
1639 	}
1640 }
1641 
list_count(struct list_head * list)1642 static unsigned long list_count(struct list_head *list)
1643 {
1644 	struct list_head *pos;
1645 	unsigned long count = 0;
1646 
1647 	list_for_each(pos, list)
1648 		count++;
1649 
1650 	return count;
1651 }
1652 
read_ul(void * p,size_t x)1653 static unsigned long read_ul(void *p, size_t x)
1654 {
1655 	return *(unsigned long *)(p + x);
1656 }
1657 
print_properties(struct intel_engine_cs * engine,struct drm_printer * m)1658 static void print_properties(struct intel_engine_cs *engine,
1659 			     struct drm_printer *m)
1660 {
1661 	static const struct pmap {
1662 		size_t offset;
1663 		const char *name;
1664 	} props[] = {
1665 #define P(x) { \
1666 	.offset = offsetof(typeof(engine->props), x), \
1667 	.name = #x \
1668 }
1669 		P(heartbeat_interval_ms),
1670 		P(max_busywait_duration_ns),
1671 		P(preempt_timeout_ms),
1672 		P(stop_timeout_ms),
1673 		P(timeslice_duration_ms),
1674 
1675 		{},
1676 #undef P
1677 	};
1678 	const struct pmap *p;
1679 
1680 	drm_printf(m, "\tProperties:\n");
1681 	for (p = props; p->name; p++)
1682 		drm_printf(m, "\t\t%s: %lu [default %lu]\n",
1683 			   p->name,
1684 			   read_ul(&engine->props, p->offset),
1685 			   read_ul(&engine->defaults, p->offset));
1686 }
1687 
engine_dump_request(struct i915_request * rq,struct drm_printer * m,const char * msg)1688 static void engine_dump_request(struct i915_request *rq, struct drm_printer *m, const char *msg)
1689 {
1690 	struct intel_timeline *tl = get_timeline(rq);
1691 
1692 	i915_request_show(m, rq, msg, 0);
1693 
1694 	drm_printf(m, "\t\tring->start:  0x%08x\n",
1695 		   i915_ggtt_offset(rq->ring->vma));
1696 	drm_printf(m, "\t\tring->head:   0x%08x\n",
1697 		   rq->ring->head);
1698 	drm_printf(m, "\t\tring->tail:   0x%08x\n",
1699 		   rq->ring->tail);
1700 	drm_printf(m, "\t\tring->emit:   0x%08x\n",
1701 		   rq->ring->emit);
1702 	drm_printf(m, "\t\tring->space:  0x%08x\n",
1703 		   rq->ring->space);
1704 
1705 	if (tl) {
1706 		drm_printf(m, "\t\tring->hwsp:   0x%08x\n",
1707 			   tl->hwsp_offset);
1708 		intel_timeline_put(tl);
1709 	}
1710 
1711 	print_request_ring(m, rq);
1712 
1713 	if (rq->context->lrc_reg_state) {
1714 		drm_printf(m, "Logical Ring Context:\n");
1715 		hexdump(m, rq->context->lrc_reg_state, PAGE_SIZE);
1716 	}
1717 }
1718 
intel_engine_dump_active_requests(struct list_head * requests,struct i915_request * hung_rq,struct drm_printer * m)1719 void intel_engine_dump_active_requests(struct list_head *requests,
1720 				       struct i915_request *hung_rq,
1721 				       struct drm_printer *m)
1722 {
1723 	struct i915_request *rq;
1724 	const char *msg;
1725 	enum i915_request_state state;
1726 
1727 	list_for_each_entry(rq, requests, sched.link) {
1728 		if (rq == hung_rq)
1729 			continue;
1730 
1731 		state = i915_test_request_state(rq);
1732 		if (state < I915_REQUEST_QUEUED)
1733 			continue;
1734 
1735 		if (state == I915_REQUEST_ACTIVE)
1736 			msg = "\t\tactive on engine";
1737 		else
1738 			msg = "\t\tactive in queue";
1739 
1740 		engine_dump_request(rq, m, msg);
1741 	}
1742 }
1743 
engine_dump_active_requests(struct intel_engine_cs * engine,struct drm_printer * m)1744 static void engine_dump_active_requests(struct intel_engine_cs *engine, struct drm_printer *m)
1745 {
1746 	struct i915_request *hung_rq = NULL;
1747 	struct intel_context *ce;
1748 	bool guc;
1749 
1750 	/*
1751 	 * No need for an engine->irq_seqno_barrier() before the seqno reads.
1752 	 * The GPU is still running so requests are still executing and any
1753 	 * hardware reads will be out of date by the time they are reported.
1754 	 * But the intention here is just to report an instantaneous snapshot
1755 	 * so that's fine.
1756 	 */
1757 	lockdep_assert_held(&engine->sched_engine->lock);
1758 
1759 	drm_printf(m, "\tRequests:\n");
1760 
1761 	guc = intel_uc_uses_guc_submission(&engine->gt->uc);
1762 	if (guc) {
1763 		ce = intel_engine_get_hung_context(engine);
1764 		if (ce)
1765 			hung_rq = intel_context_find_active_request(ce);
1766 	} else {
1767 		hung_rq = intel_engine_execlist_find_hung_request(engine);
1768 	}
1769 
1770 	if (hung_rq)
1771 		engine_dump_request(hung_rq, m, "\t\thung");
1772 
1773 	if (guc)
1774 		intel_guc_dump_active_requests(engine, hung_rq, m);
1775 	else
1776 		intel_engine_dump_active_requests(&engine->sched_engine->requests,
1777 						  hung_rq, m);
1778 }
1779 
intel_engine_dump(struct intel_engine_cs * engine,struct drm_printer * m,const char * header,...)1780 void intel_engine_dump(struct intel_engine_cs *engine,
1781 		       struct drm_printer *m,
1782 		       const char *header, ...)
1783 {
1784 	struct i915_gpu_error * const error = &engine->i915->gpu_error;
1785 	struct i915_request *rq;
1786 	intel_wakeref_t wakeref;
1787 	unsigned long flags;
1788 	ktime_t dummy;
1789 
1790 	if (header) {
1791 		va_list ap;
1792 
1793 		va_start(ap, header);
1794 		drm_vprintf(m, header, &ap);
1795 		va_end(ap);
1796 	}
1797 
1798 	if (intel_gt_is_wedged(engine->gt))
1799 		drm_printf(m, "*** WEDGED ***\n");
1800 
1801 	drm_printf(m, "\tAwake? %d\n", atomic_read(&engine->wakeref.count));
1802 	drm_printf(m, "\tBarriers?: %s\n",
1803 		   yesno(!llist_empty(&engine->barrier_tasks)));
1804 	drm_printf(m, "\tLatency: %luus\n",
1805 		   ewma__engine_latency_read(&engine->latency));
1806 	if (intel_engine_supports_stats(engine))
1807 		drm_printf(m, "\tRuntime: %llums\n",
1808 			   ktime_to_ms(intel_engine_get_busy_time(engine,
1809 								  &dummy)));
1810 	drm_printf(m, "\tForcewake: %x domains, %d active\n",
1811 		   engine->fw_domain, READ_ONCE(engine->fw_active));
1812 
1813 	rcu_read_lock();
1814 	rq = READ_ONCE(engine->heartbeat.systole);
1815 	if (rq)
1816 		drm_printf(m, "\tHeartbeat: %d ms ago\n",
1817 			   jiffies_to_msecs(jiffies - rq->emitted_jiffies));
1818 	rcu_read_unlock();
1819 	drm_printf(m, "\tReset count: %d (global %d)\n",
1820 		   i915_reset_engine_count(error, engine),
1821 		   i915_reset_count(error));
1822 	print_properties(engine, m);
1823 
1824 	spin_lock_irqsave(&engine->sched_engine->lock, flags);
1825 	engine_dump_active_requests(engine, m);
1826 
1827 	drm_printf(m, "\tOn hold?: %lu\n",
1828 		   list_count(&engine->sched_engine->hold));
1829 	spin_unlock_irqrestore(&engine->sched_engine->lock, flags);
1830 
1831 	drm_printf(m, "\tMMIO base:  0x%08x\n", engine->mmio_base);
1832 	wakeref = intel_runtime_pm_get_if_in_use(engine->uncore->rpm);
1833 	if (wakeref) {
1834 		intel_engine_print_registers(engine, m);
1835 		intel_runtime_pm_put(engine->uncore->rpm, wakeref);
1836 	} else {
1837 		drm_printf(m, "\tDevice is asleep; skipping register dump\n");
1838 	}
1839 
1840 	intel_execlists_show_requests(engine, m, i915_request_show, 8);
1841 
1842 	drm_printf(m, "HWSP:\n");
1843 	hexdump(m, engine->status_page.addr, PAGE_SIZE);
1844 
1845 	drm_printf(m, "Idle? %s\n", yesno(intel_engine_is_idle(engine)));
1846 
1847 	intel_engine_print_breadcrumbs(engine, m);
1848 }
1849 
__intel_engine_get_busy_time(struct intel_engine_cs * engine,ktime_t * now)1850 static ktime_t __intel_engine_get_busy_time(struct intel_engine_cs *engine,
1851 					    ktime_t *now)
1852 {
1853 	ktime_t total = engine->stats.total;
1854 
1855 	/*
1856 	 * If the engine is executing something at the moment
1857 	 * add it to the total.
1858 	 */
1859 	*now = ktime_get();
1860 	if (READ_ONCE(engine->stats.active))
1861 		total = ktime_add(total, ktime_sub(*now, engine->stats.start));
1862 
1863 	return total;
1864 }
1865 
1866 /**
1867  * intel_engine_get_busy_time() - Return current accumulated engine busyness
1868  * @engine: engine to report on
1869  * @now: monotonic timestamp of sampling
1870  *
1871  * Returns accumulated time @engine was busy since engine stats were enabled.
1872  */
intel_engine_get_busy_time(struct intel_engine_cs * engine,ktime_t * now)1873 ktime_t intel_engine_get_busy_time(struct intel_engine_cs *engine, ktime_t *now)
1874 {
1875 	unsigned int seq;
1876 	ktime_t total;
1877 
1878 	do {
1879 		seq = read_seqcount_begin(&engine->stats.lock);
1880 		total = __intel_engine_get_busy_time(engine, now);
1881 	} while (read_seqcount_retry(&engine->stats.lock, seq));
1882 
1883 	return total;
1884 }
1885 
1886 struct intel_context *
intel_engine_create_virtual(struct intel_engine_cs ** siblings,unsigned int count)1887 intel_engine_create_virtual(struct intel_engine_cs **siblings,
1888 			    unsigned int count)
1889 {
1890 	if (count == 0)
1891 		return ERR_PTR(-EINVAL);
1892 
1893 	if (count == 1)
1894 		return intel_context_create(siblings[0]);
1895 
1896 	GEM_BUG_ON(!siblings[0]->cops->create_virtual);
1897 	return siblings[0]->cops->create_virtual(siblings, count);
1898 }
1899 
1900 struct i915_request *
intel_engine_execlist_find_hung_request(struct intel_engine_cs * engine)1901 intel_engine_execlist_find_hung_request(struct intel_engine_cs *engine)
1902 {
1903 	struct i915_request *request, *active = NULL;
1904 
1905 	/*
1906 	 * This search does not work in GuC submission mode. However, the GuC
1907 	 * will report the hanging context directly to the driver itself. So
1908 	 * the driver should never get here when in GuC mode.
1909 	 */
1910 	GEM_BUG_ON(intel_uc_uses_guc_submission(&engine->gt->uc));
1911 
1912 	/*
1913 	 * We are called by the error capture, reset and to dump engine
1914 	 * state at random points in time. In particular, note that neither is
1915 	 * crucially ordered with an interrupt. After a hang, the GPU is dead
1916 	 * and we assume that no more writes can happen (we waited long enough
1917 	 * for all writes that were in transaction to be flushed) - adding an
1918 	 * extra delay for a recent interrupt is pointless. Hence, we do
1919 	 * not need an engine->irq_seqno_barrier() before the seqno reads.
1920 	 * At all other times, we must assume the GPU is still running, but
1921 	 * we only care about the snapshot of this moment.
1922 	 */
1923 	lockdep_assert_held(&engine->sched_engine->lock);
1924 
1925 	rcu_read_lock();
1926 	request = execlists_active(&engine->execlists);
1927 	if (request) {
1928 		struct intel_timeline *tl = request->context->timeline;
1929 
1930 		list_for_each_entry_from_reverse(request, &tl->requests, link) {
1931 			if (__i915_request_is_complete(request))
1932 				break;
1933 
1934 			active = request;
1935 		}
1936 	}
1937 	rcu_read_unlock();
1938 	if (active)
1939 		return active;
1940 
1941 	list_for_each_entry(request, &engine->sched_engine->requests,
1942 			    sched.link) {
1943 		if (i915_test_request_state(request) != I915_REQUEST_ACTIVE)
1944 			continue;
1945 
1946 		active = request;
1947 		break;
1948 	}
1949 
1950 	return active;
1951 }
1952 
1953 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1954 #include "mock_engine.c"
1955 #include "selftest_engine.c"
1956 #include "selftest_engine_cs.c"
1957 #endif
1958