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