xref: /linux/drivers/gpu/drm/xe/xe_hw_engine.c (revision 889dd44491b37c890b1b4ef40876cf19032ca897)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2021 Intel Corporation
4  */
5 
6 #include "xe_hw_engine.h"
7 
8 #include <linux/nospec.h>
9 
10 #include <drm/drm_managed.h>
11 #include <drm/drm_print.h>
12 #include <uapi/drm/xe_drm.h>
13 #include <generated/xe_wa_oob.h>
14 
15 #include "regs/xe_engine_regs.h"
16 #include "regs/xe_gt_regs.h"
17 #include "regs/xe_irq_regs.h"
18 #include "xe_assert.h"
19 #include "xe_bo.h"
20 #include "xe_configfs.h"
21 #include "xe_device.h"
22 #include "xe_execlist.h"
23 #include "xe_force_wake.h"
24 #include "xe_gsc.h"
25 #include "xe_gt.h"
26 #include "xe_gt_ccs_mode.h"
27 #include "xe_gt_clock.h"
28 #include "xe_gt_printk.h"
29 #include "xe_gt_mcr.h"
30 #include "xe_gt_topology.h"
31 #include "xe_guc_capture.h"
32 #include "xe_hw_engine_group.h"
33 #include "xe_hw_fence.h"
34 #include "xe_irq.h"
35 #include "xe_lrc.h"
36 #include "xe_mmio.h"
37 #include "xe_reg_sr.h"
38 #include "xe_reg_whitelist.h"
39 #include "xe_rtp.h"
40 #include "xe_sched_job.h"
41 #include "xe_sriov.h"
42 #include "xe_tuning.h"
43 #include "xe_uc_fw.h"
44 #include "xe_wa.h"
45 
46 #define MAX_MMIO_BASES 3
47 struct engine_info {
48 	const char *name;
49 	unsigned int class : 8;
50 	unsigned int instance : 8;
51 	unsigned int irq_offset : 8;
52 	enum xe_force_wake_domains domain;
53 	u32 mmio_base;
54 };
55 
56 static const struct engine_info engine_infos[] = {
57 	[XE_HW_ENGINE_RCS0] = {
58 		.name = "rcs0",
59 		.class = XE_ENGINE_CLASS_RENDER,
60 		.instance = 0,
61 		.irq_offset = ilog2(INTR_RCS0),
62 		.domain = XE_FW_RENDER,
63 		.mmio_base = RENDER_RING_BASE,
64 	},
65 	[XE_HW_ENGINE_BCS0] = {
66 		.name = "bcs0",
67 		.class = XE_ENGINE_CLASS_COPY,
68 		.instance = 0,
69 		.irq_offset = ilog2(INTR_BCS(0)),
70 		.domain = XE_FW_GT,
71 		.mmio_base = BLT_RING_BASE,
72 	},
73 	[XE_HW_ENGINE_BCS1] = {
74 		.name = "bcs1",
75 		.class = XE_ENGINE_CLASS_COPY,
76 		.instance = 1,
77 		.irq_offset = ilog2(INTR_BCS(1)),
78 		.domain = XE_FW_GT,
79 		.mmio_base = XEHPC_BCS1_RING_BASE,
80 	},
81 	[XE_HW_ENGINE_BCS2] = {
82 		.name = "bcs2",
83 		.class = XE_ENGINE_CLASS_COPY,
84 		.instance = 2,
85 		.irq_offset = ilog2(INTR_BCS(2)),
86 		.domain = XE_FW_GT,
87 		.mmio_base = XEHPC_BCS2_RING_BASE,
88 	},
89 	[XE_HW_ENGINE_BCS3] = {
90 		.name = "bcs3",
91 		.class = XE_ENGINE_CLASS_COPY,
92 		.instance = 3,
93 		.irq_offset = ilog2(INTR_BCS(3)),
94 		.domain = XE_FW_GT,
95 		.mmio_base = XEHPC_BCS3_RING_BASE,
96 	},
97 	[XE_HW_ENGINE_BCS4] = {
98 		.name = "bcs4",
99 		.class = XE_ENGINE_CLASS_COPY,
100 		.instance = 4,
101 		.irq_offset = ilog2(INTR_BCS(4)),
102 		.domain = XE_FW_GT,
103 		.mmio_base = XEHPC_BCS4_RING_BASE,
104 	},
105 	[XE_HW_ENGINE_BCS5] = {
106 		.name = "bcs5",
107 		.class = XE_ENGINE_CLASS_COPY,
108 		.instance = 5,
109 		.irq_offset = ilog2(INTR_BCS(5)),
110 		.domain = XE_FW_GT,
111 		.mmio_base = XEHPC_BCS5_RING_BASE,
112 	},
113 	[XE_HW_ENGINE_BCS6] = {
114 		.name = "bcs6",
115 		.class = XE_ENGINE_CLASS_COPY,
116 		.instance = 6,
117 		.irq_offset = ilog2(INTR_BCS(6)),
118 		.domain = XE_FW_GT,
119 		.mmio_base = XEHPC_BCS6_RING_BASE,
120 	},
121 	[XE_HW_ENGINE_BCS7] = {
122 		.name = "bcs7",
123 		.class = XE_ENGINE_CLASS_COPY,
124 		.irq_offset = ilog2(INTR_BCS(7)),
125 		.instance = 7,
126 		.domain = XE_FW_GT,
127 		.mmio_base = XEHPC_BCS7_RING_BASE,
128 	},
129 	[XE_HW_ENGINE_BCS8] = {
130 		.name = "bcs8",
131 		.class = XE_ENGINE_CLASS_COPY,
132 		.instance = 8,
133 		.irq_offset = ilog2(INTR_BCS8),
134 		.domain = XE_FW_GT,
135 		.mmio_base = XEHPC_BCS8_RING_BASE,
136 	},
137 
138 	[XE_HW_ENGINE_VCS0] = {
139 		.name = "vcs0",
140 		.class = XE_ENGINE_CLASS_VIDEO_DECODE,
141 		.instance = 0,
142 		.irq_offset = 32 + ilog2(INTR_VCS(0)),
143 		.domain = XE_FW_MEDIA_VDBOX0,
144 		.mmio_base = BSD_RING_BASE,
145 	},
146 	[XE_HW_ENGINE_VCS1] = {
147 		.name = "vcs1",
148 		.class = XE_ENGINE_CLASS_VIDEO_DECODE,
149 		.instance = 1,
150 		.irq_offset = 32 + ilog2(INTR_VCS(1)),
151 		.domain = XE_FW_MEDIA_VDBOX1,
152 		.mmio_base = BSD2_RING_BASE,
153 	},
154 	[XE_HW_ENGINE_VCS2] = {
155 		.name = "vcs2",
156 		.class = XE_ENGINE_CLASS_VIDEO_DECODE,
157 		.instance = 2,
158 		.irq_offset = 32 + ilog2(INTR_VCS(2)),
159 		.domain = XE_FW_MEDIA_VDBOX2,
160 		.mmio_base = BSD3_RING_BASE,
161 	},
162 	[XE_HW_ENGINE_VCS3] = {
163 		.name = "vcs3",
164 		.class = XE_ENGINE_CLASS_VIDEO_DECODE,
165 		.instance = 3,
166 		.irq_offset = 32 + ilog2(INTR_VCS(3)),
167 		.domain = XE_FW_MEDIA_VDBOX3,
168 		.mmio_base = BSD4_RING_BASE,
169 	},
170 	[XE_HW_ENGINE_VCS4] = {
171 		.name = "vcs4",
172 		.class = XE_ENGINE_CLASS_VIDEO_DECODE,
173 		.instance = 4,
174 		.irq_offset = 32 + ilog2(INTR_VCS(4)),
175 		.domain = XE_FW_MEDIA_VDBOX4,
176 		.mmio_base = XEHP_BSD5_RING_BASE,
177 	},
178 	[XE_HW_ENGINE_VCS5] = {
179 		.name = "vcs5",
180 		.class = XE_ENGINE_CLASS_VIDEO_DECODE,
181 		.instance = 5,
182 		.irq_offset = 32 + ilog2(INTR_VCS(5)),
183 		.domain = XE_FW_MEDIA_VDBOX5,
184 		.mmio_base = XEHP_BSD6_RING_BASE,
185 	},
186 	[XE_HW_ENGINE_VCS6] = {
187 		.name = "vcs6",
188 		.class = XE_ENGINE_CLASS_VIDEO_DECODE,
189 		.instance = 6,
190 		.irq_offset = 32 + ilog2(INTR_VCS(6)),
191 		.domain = XE_FW_MEDIA_VDBOX6,
192 		.mmio_base = XEHP_BSD7_RING_BASE,
193 	},
194 	[XE_HW_ENGINE_VCS7] = {
195 		.name = "vcs7",
196 		.class = XE_ENGINE_CLASS_VIDEO_DECODE,
197 		.instance = 7,
198 		.irq_offset = 32 + ilog2(INTR_VCS(7)),
199 		.domain = XE_FW_MEDIA_VDBOX7,
200 		.mmio_base = XEHP_BSD8_RING_BASE,
201 	},
202 	[XE_HW_ENGINE_VECS0] = {
203 		.name = "vecs0",
204 		.class = XE_ENGINE_CLASS_VIDEO_ENHANCE,
205 		.instance = 0,
206 		.irq_offset = 32 + ilog2(INTR_VECS(0)),
207 		.domain = XE_FW_MEDIA_VEBOX0,
208 		.mmio_base = VEBOX_RING_BASE,
209 	},
210 	[XE_HW_ENGINE_VECS1] = {
211 		.name = "vecs1",
212 		.class = XE_ENGINE_CLASS_VIDEO_ENHANCE,
213 		.instance = 1,
214 		.irq_offset = 32 + ilog2(INTR_VECS(1)),
215 		.domain = XE_FW_MEDIA_VEBOX1,
216 		.mmio_base = VEBOX2_RING_BASE,
217 	},
218 	[XE_HW_ENGINE_VECS2] = {
219 		.name = "vecs2",
220 		.class = XE_ENGINE_CLASS_VIDEO_ENHANCE,
221 		.instance = 2,
222 		.irq_offset = 32 + ilog2(INTR_VECS(2)),
223 		.domain = XE_FW_MEDIA_VEBOX2,
224 		.mmio_base = XEHP_VEBOX3_RING_BASE,
225 	},
226 	[XE_HW_ENGINE_VECS3] = {
227 		.name = "vecs3",
228 		.class = XE_ENGINE_CLASS_VIDEO_ENHANCE,
229 		.instance = 3,
230 		.irq_offset = 32 + ilog2(INTR_VECS(3)),
231 		.domain = XE_FW_MEDIA_VEBOX3,
232 		.mmio_base = XEHP_VEBOX4_RING_BASE,
233 	},
234 	[XE_HW_ENGINE_CCS0] = {
235 		.name = "ccs0",
236 		.class = XE_ENGINE_CLASS_COMPUTE,
237 		.instance = 0,
238 		.irq_offset = ilog2(INTR_CCS(0)),
239 		.domain = XE_FW_RENDER,
240 		.mmio_base = COMPUTE0_RING_BASE,
241 	},
242 	[XE_HW_ENGINE_CCS1] = {
243 		.name = "ccs1",
244 		.class = XE_ENGINE_CLASS_COMPUTE,
245 		.instance = 1,
246 		.irq_offset = ilog2(INTR_CCS(1)),
247 		.domain = XE_FW_RENDER,
248 		.mmio_base = COMPUTE1_RING_BASE,
249 	},
250 	[XE_HW_ENGINE_CCS2] = {
251 		.name = "ccs2",
252 		.class = XE_ENGINE_CLASS_COMPUTE,
253 		.instance = 2,
254 		.irq_offset = ilog2(INTR_CCS(2)),
255 		.domain = XE_FW_RENDER,
256 		.mmio_base = COMPUTE2_RING_BASE,
257 	},
258 	[XE_HW_ENGINE_CCS3] = {
259 		.name = "ccs3",
260 		.class = XE_ENGINE_CLASS_COMPUTE,
261 		.instance = 3,
262 		.irq_offset = ilog2(INTR_CCS(3)),
263 		.domain = XE_FW_RENDER,
264 		.mmio_base = COMPUTE3_RING_BASE,
265 	},
266 	[XE_HW_ENGINE_GSCCS0] = {
267 		.name = "gsccs0",
268 		.class = XE_ENGINE_CLASS_OTHER,
269 		.instance = OTHER_GSC_INSTANCE,
270 		.domain = XE_FW_GSC,
271 		.mmio_base = GSCCS_RING_BASE,
272 	},
273 };
274 
275 static void hw_engine_fini(void *arg)
276 {
277 	struct xe_hw_engine *hwe = arg;
278 	struct xe_gt *gt = hwe->gt;
279 
280 	if (gt) {
281 		struct xe_device *xe = gt_to_xe(gt);
282 		unsigned long flags;
283 
284 		if (hwe->exl_port)
285 			xe_execlist_port_destroy(hwe->exl_port);
286 
287 		spin_lock_irqsave(&xe->irq.lock, flags);
288 		hwe->name = NULL;
289 		hwe->gt = NULL;
290 		spin_unlock_irqrestore(&xe->irq.lock, flags);
291 	} else {
292 		hwe->name = NULL;
293 	}
294 }
295 
296 /**
297  * xe_hw_engine_mmio_read32() - Read engine register
298  * @hwe: engine
299  * @reg: register to read from
300  *
301  * This function will read from an engine specific register.
302  * Forcewake must be held by the caller.
303  *
304  * Return: value of the 32-bit register.
305  */
306 u32 xe_hw_engine_mmio_read32(struct xe_hw_engine *hwe, struct xe_reg reg)
307 {
308 	xe_gt_assert(hwe->gt, !(reg.addr & hwe->mmio_base));
309 	xe_force_wake_assert_held(gt_to_fw(hwe->gt), hwe->domain);
310 
311 	reg.addr += hwe->mmio_base;
312 
313 	return xe_mmio_read32(&hwe->gt->mmio, reg);
314 }
315 
316 void xe_hw_engine_enable_ring(struct xe_hw_engine *hwe)
317 {
318 	xe_mmio_write32(&hwe->gt->mmio, RING_HWS_PGA(hwe->mmio_base),
319 			xe_bo_ggtt_addr(hwe->hwsp));
320 }
321 
322 static bool xe_hw_engine_match_fixed_cslice_mode(const struct xe_device *xe,
323 						 const struct xe_gt *gt,
324 						 const struct xe_hw_engine *hwe)
325 {
326 	/*
327 	 * Xe3p no longer supports load balance mode, so "fixed cslice" mode
328 	 * is automatic and no RCU_MODE programming is required.
329 	 */
330 	if (GRAPHICS_VER(gt_to_xe(gt)) >= 35)
331 		return false;
332 
333 	return xe_gt_ccs_mode_enabled(gt) &&
334 	       xe_rtp_match_first_render_or_compute(xe, gt, hwe);
335 }
336 
337 static bool xe_rtp_cfeg_wmtp_disabled(const struct xe_device *xe,
338 				      const struct xe_gt *gt,
339 				      const struct xe_hw_engine *hwe)
340 {
341 	if (GRAPHICS_VER(xe) < 20)
342 		return false;
343 
344 	if (hwe->class != XE_ENGINE_CLASS_COMPUTE &&
345 	    hwe->class != XE_ENGINE_CLASS_RENDER)
346 		return false;
347 
348 	return xe_mmio_read32(&hwe->gt->mmio, XEHP_FUSE4) & CFEG_WMTP_DISABLE;
349 }
350 
351 static u32 blit_cctl_val(struct xe_gt *gt, struct xe_hw_engine *hwe)
352 {
353 	return REG_FIELD_PREP(BLIT_CCTL_DST_MOCS_MASK, gt->mocs.uc_index) |
354 		REG_FIELD_PREP(BLIT_CCTL_SRC_MOCS_MASK, gt->mocs.uc_index);
355 }
356 
357 static const struct xe_rtp_table_sr lrc_setup = XE_RTP_TABLE_SR(
358 	/*
359 	 * Some blitter commands do not have a field for MOCS, those
360 	 * commands will use MOCS index pointed by BLIT_CCTL.
361 	 * BLIT_CCTL registers are needed to be programmed to un-cached.
362 	 */
363 	{ XE_RTP_NAME("BLIT_CCTL_default_MOCS"),
364 	  XE_RTP_RULES(GRAPHICS_VERSION_RANGE(1200, 1274),
365 		       ENGINE_CLASS(COPY)),
366 	  XE_RTP_ACTIONS(FIELD_SET_FUNC(BLIT_CCTL(0),
367 					BLIT_CCTL_DST_MOCS_MASK |
368 					BLIT_CCTL_SRC_MOCS_MASK,
369 					blit_cctl_val,
370 					XE_RTP_ACTION_FLAG(ENGINE_BASE)))
371 	},
372 	/* Disable WMTP if HW doesn't support it */
373 	{ XE_RTP_NAME("DISABLE_WMTP_ON_UNSUPPORTED_HW"),
374 	  XE_RTP_RULES(FUNC(xe_rtp_cfeg_wmtp_disabled)),
375 	  XE_RTP_ACTIONS(FIELD_SET(CS_CHICKEN1(0),
376 				   PREEMPT_GPGPU_LEVEL_MASK,
377 				   PREEMPT_GPGPU_THREAD_GROUP_LEVEL)),
378 	  XE_RTP_ENTRY_FLAG(FOREACH_ENGINE)
379 	},
380 );
381 
382 static void
383 hw_engine_setup_default_lrc_state(struct xe_hw_engine *hwe)
384 {
385 	struct xe_rtp_process_ctx ctx = XE_RTP_PROCESS_CTX_INITIALIZER(hwe);
386 
387 	xe_rtp_process_to_sr(&ctx, &lrc_setup, &hwe->reg_lrc, true);
388 }
389 
390 void xe_hw_engine_setup_reg_lrc(struct xe_hw_engine *hwe)
391 {
392 	struct xe_gt *gt = hwe->gt;
393 	struct xe_device *xe = gt_to_xe(gt);
394 
395 	xe_reg_sr_init(&hwe->reg_lrc, hwe->name, xe);
396 	xe_wa_process_lrc(hwe);
397 	hw_engine_setup_default_lrc_state(hwe);
398 	xe_tuning_process_lrc(hwe);
399 }
400 
401 /*
402  * RING_CMD_CCTL specifies the default MOCS entry that will be
403  * used by the command streamer when executing commands that
404  * don't have a way to explicitly specify a MOCS setting.
405  * The default should usually reference whichever MOCS entry
406  * corresponds to uncached behavior, although use of a WB cached
407  * entry is recommended by the spec in certain circumstances on
408  * specific platforms.
409  * Bspec: 72161
410  */
411 static u32 ring_cmd_cctl_val(struct xe_gt *gt, struct xe_hw_engine *hwe)
412 {
413 	struct xe_device *xe = gt_to_xe(gt);
414 	u8 mocs_read_idx = gt->mocs.uc_index;
415 
416 	if (hwe->class == XE_ENGINE_CLASS_COMPUTE && IS_DGFX(xe) &&
417 	    (GRAPHICS_VER(xe) >= 20 || xe->info.platform == XE_PVC))
418 		mocs_read_idx = gt->mocs.wb_index;
419 
420 	return REG_FIELD_PREP(CMD_CCTL_WRITE_OVERRIDE_MASK, gt->mocs.uc_index) |
421 		REG_FIELD_PREP(CMD_CCTL_READ_OVERRIDE_MASK, mocs_read_idx);
422 }
423 
424 static const struct xe_rtp_table_sr engine_sr = XE_RTP_TABLE_SR(
425 	{ XE_RTP_NAME("RING_CMD_CCTL_default_MOCS"),
426 	  XE_RTP_RULES(FUNC(xe_rtp_match_always)),
427 	  XE_RTP_ACTIONS(FIELD_SET_FUNC(RING_CMD_CCTL(0),
428 					CMD_CCTL_WRITE_OVERRIDE_MASK |
429 					CMD_CCTL_READ_OVERRIDE_MASK,
430 					ring_cmd_cctl_val,
431 					XE_RTP_ACTION_FLAG(ENGINE_BASE)))
432 	},
433 	{ XE_RTP_NAME("Disable HW status page updates for interrupts"),
434 	  XE_RTP_RULES(FUNC(xe_rtp_match_always)),
435 	  XE_RTP_ACTIONS(SET(RING_HWSTAM(0), ~0x0,
436 			     XE_RTP_ACTION_FLAG(ENGINE_BASE)))
437 	},
438 	{ XE_RTP_NAME("Disable engine 'legacy' mode"),
439 	  XE_RTP_RULES(FUNC(xe_rtp_match_always)),
440 	  XE_RTP_ACTIONS(SET(GFX_MODE(0), GFX_DISABLE_LEGACY_MODE,
441 			     XE_RTP_ACTION_FLAG(ENGINE_BASE)))
442 	},
443 	/*
444 	 * To allow the GSC engine to go idle on MTL we need to enable
445 	 * idle messaging and set the hysteresis value (we use 0xA=5us
446 	 * as recommended in spec). On platforms after MTL this is
447 	 * enabled by default.
448 	 */
449 	{ XE_RTP_NAME("MTL GSCCS IDLE MSG enable"),
450 	  XE_RTP_RULES(MEDIA_VERSION(1300), ENGINE_CLASS(OTHER)),
451 	  XE_RTP_ACTIONS(CLR(RING_PSMI_CTL(0),
452 			     IDLE_MSG_DISABLE,
453 			     XE_RTP_ACTION_FLAG(ENGINE_BASE)),
454 			 FIELD_SET(RING_PWRCTX_MAXCNT(0),
455 				   IDLE_WAIT_TIME,
456 				   0xA,
457 				   XE_RTP_ACTION_FLAG(ENGINE_BASE)))
458 	},
459 	/* Enable Priority Mem Read */
460 	{ XE_RTP_NAME("Priority_Mem_Read"),
461 	  XE_RTP_RULES(GRAPHICS_VERSION_RANGE(2001, XE_RTP_END_VERSION_UNDEFINED)),
462 	  XE_RTP_ACTIONS(SET(CSFE_CHICKEN1(0), CS_PRIORITY_MEM_READ,
463 			     XE_RTP_ACTION_FLAG(ENGINE_BASE)))
464 	},
465 	{ XE_RTP_NAME("Enable CCS Engine(s)"),
466 	  XE_RTP_RULES(GRAPHICS_VERSION_RANGE(1255, XE_RTP_END_VERSION_UNDEFINED),
467 		       FUNC(xe_rtp_match_first_render_or_compute)),
468 	  XE_RTP_ACTIONS(SET(RCU_MODE, RCU_MODE_CCS_ENABLE))
469 	},
470 	/* Use Fixed slice CCS mode */
471 	{ XE_RTP_NAME("RCU_MODE_FIXED_SLICE_CCS_MODE"),
472 	  XE_RTP_RULES(FUNC(xe_hw_engine_match_fixed_cslice_mode)),
473 	  XE_RTP_ACTIONS(FIELD_SET(RCU_MODE, RCU_MODE_FIXED_SLICE_CCS_MODE,
474 				   RCU_MODE_FIXED_SLICE_CCS_MODE))
475 	},
476 	{ XE_RTP_NAME("Enable MSI-X interrupt support"),
477 	  XE_RTP_RULES(FUNC(xe_rtp_match_has_msix)),
478 	  XE_RTP_ACTIONS(SET(GFX_MODE(0), GFX_MSIX_INTERRUPT_ENABLE,
479 			     XE_RTP_ACTION_FLAG(ENGINE_BASE)))
480 	},
481 );
482 
483 static void
484 hw_engine_setup_default_state(struct xe_hw_engine *hwe)
485 {
486 	struct xe_rtp_process_ctx ctx = XE_RTP_PROCESS_CTX_INITIALIZER(hwe);
487 
488 	xe_rtp_process_to_sr(&ctx, &engine_sr, &hwe->reg_sr, false);
489 }
490 
491 static const struct engine_info *find_engine_info(enum xe_engine_class class, int instance)
492 {
493 	const struct engine_info *info;
494 	enum xe_hw_engine_id id;
495 
496 	for (id = 0; id < XE_NUM_HW_ENGINES; ++id) {
497 		info = &engine_infos[id];
498 		if (info->class == class && info->instance == instance)
499 			return info;
500 	}
501 
502 	return NULL;
503 }
504 
505 static u16 get_msix_irq_offset(struct xe_gt *gt, enum xe_engine_class class)
506 {
507 	/* For MSI-X, hw engines report to offset of engine instance zero */
508 	const struct engine_info *info = find_engine_info(class, 0);
509 
510 	xe_gt_assert(gt, info);
511 
512 	return info ? info->irq_offset : 0;
513 }
514 
515 static void hw_engine_init_early(struct xe_gt *gt, struct xe_hw_engine *hwe,
516 				 enum xe_hw_engine_id id)
517 {
518 	const struct engine_info *info;
519 
520 	if (WARN_ON(id >= ARRAY_SIZE(engine_infos) || !engine_infos[id].name))
521 		return;
522 
523 	if (!(gt->info.engine_mask & BIT(id)))
524 		return;
525 
526 	info = &engine_infos[id];
527 
528 	xe_gt_assert(gt, !hwe->gt);
529 
530 	hwe->gt = gt;
531 	hwe->class = info->class;
532 	hwe->instance = info->instance;
533 	hwe->mmio_base = info->mmio_base;
534 	if (xe_device_has_msix(gt_to_xe(gt))) {
535 		hwe->irq_offset = get_msix_irq_offset(gt, info->class);
536 		hwe->irq_page = info->instance;
537 
538 	} else {
539 		hwe->irq_offset = info->irq_offset;
540 		hwe->irq_page = 0;
541 	}
542 	hwe->domain = info->domain;
543 	hwe->name = info->name;
544 	hwe->fence_irq = &gt->fence_irq[info->class];
545 	hwe->engine_id = id;
546 
547 	hwe->eclass = &gt->eclass[hwe->class];
548 	if (!hwe->eclass->sched_props.job_timeout_ms) {
549 		hwe->eclass->sched_props.job_timeout_ms = 5 * 1000;
550 		hwe->eclass->sched_props.job_timeout_min = XE_HW_ENGINE_JOB_TIMEOUT_MIN;
551 		hwe->eclass->sched_props.job_timeout_max = XE_HW_ENGINE_JOB_TIMEOUT_MAX;
552 		hwe->eclass->sched_props.timeslice_us = 1 * 1000;
553 		hwe->eclass->sched_props.timeslice_min = XE_HW_ENGINE_TIMESLICE_MIN;
554 		hwe->eclass->sched_props.timeslice_max = XE_HW_ENGINE_TIMESLICE_MAX;
555 		hwe->eclass->sched_props.preempt_timeout_us = XE_HW_ENGINE_PREEMPT_TIMEOUT;
556 		hwe->eclass->sched_props.preempt_timeout_min = XE_HW_ENGINE_PREEMPT_TIMEOUT_MIN;
557 		hwe->eclass->sched_props.preempt_timeout_max = XE_HW_ENGINE_PREEMPT_TIMEOUT_MAX;
558 
559 		/*
560 		 * The GSC engine can accept submissions while the GSC shim is
561 		 * being reset, during which time the submission is stalled. In
562 		 * the worst case, the shim reset can take up to the maximum GSC
563 		 * command execution time (250ms), so the request start can be
564 		 * delayed by that much; the request itself can take that long
565 		 * without being preemptible, which means worst case it can
566 		 * theoretically take up to 500ms for a preemption to go through
567 		 * on the GSC engine. Adding to that an extra 100ms as a safety
568 		 * margin, we get a minimum recommended timeout of 600ms.
569 		 * The preempt_timeout value can't be tuned for OTHER_CLASS
570 		 * because the class is reserved for kernel usage, so we just
571 		 * need to make sure that the starting value is above that
572 		 * threshold; since our default value (640ms) is greater than
573 		 * 600ms, the only way we can go below is via a kconfig setting.
574 		 * If that happens, log it in dmesg and update the value.
575 		 */
576 		if (hwe->class == XE_ENGINE_CLASS_OTHER) {
577 			const u32 min_preempt_timeout = 600 * 1000;
578 			if (hwe->eclass->sched_props.preempt_timeout_us < min_preempt_timeout) {
579 				hwe->eclass->sched_props.preempt_timeout_us = min_preempt_timeout;
580 				xe_gt_notice(gt, "Increasing preempt_timeout for GSC to 600ms\n");
581 			}
582 		}
583 
584 		/* Record default props */
585 		hwe->eclass->defaults = hwe->eclass->sched_props;
586 	}
587 
588 	xe_reg_sr_init(&hwe->reg_sr, hwe->name, gt_to_xe(gt));
589 	xe_tuning_process_engine(hwe);
590 	xe_wa_process_engine(hwe);
591 	hw_engine_setup_default_state(hwe);
592 
593 	xe_reg_sr_init(&hwe->reg_whitelist, hwe->name, gt_to_xe(gt));
594 	xe_reg_sr_init(&hwe->oa_whitelist, hwe->name, gt_to_xe(gt));
595 	xe_reg_sr_init(&hwe->oa_sr, hwe->name, gt_to_xe(gt));
596 	xe_reg_whitelist_process_engine(hwe);
597 }
598 
599 static u32 idledly_floor_ticks(u32 idledly_ns, u32 idledly_units_ps)
600 {
601 	return DIV_ROUND_DOWN_ULL((u64)idledly_ns * 1000, idledly_units_ps);
602 }
603 
604 static void adjust_idledly(struct xe_hw_engine *hwe)
605 {
606 	struct xe_gt *gt = hwe->gt;
607 	u32 idledly, idledly_hw, idledly_reg_val, maxcnt;
608 	u32 idledly_units_ps = 8 * gt->info.timestamp_base;
609 	u32 maxcnt_units_ns = 640;
610 	bool inhibit_switch = false;
611 	bool wa_applied = false;
612 	bool clamped_below_maxcnt = false;
613 
614 	if ((!IS_SRIOV_VF(gt_to_xe(gt)) && XE_GT_WA(gt, 16023105232)) ||
615 	    XE_GT_WA(gt, 14025941587)) {
616 		u32 mincnt_idledly_ns = 5000;
617 
618 		/* xe_gt_clock_init() warns and zeroes timestamp_base on unknown crystal clock. */
619 		if (!idledly_units_ps)
620 			return;
621 
622 		idledly_reg_val = xe_mmio_read32(&gt->mmio, RING_IDLEDLY(hwe->mmio_base));
623 		maxcnt = xe_mmio_read32(&gt->mmio, RING_PWRCTX_MAXCNT(hwe->mmio_base));
624 
625 		inhibit_switch = idledly_reg_val & INHIBIT_SWITCH_UNTIL_PREEMPTED;
626 		idledly = REG_FIELD_GET(IDLE_DELAY, idledly_reg_val);
627 		idledly = DIV_ROUND_CLOSEST_ULL((u64)idledly * idledly_units_ps, 1000);
628 		idledly_hw = idledly;
629 		maxcnt = REG_FIELD_GET(IDLE_WAIT_TIME, maxcnt);
630 		maxcnt *= maxcnt_units_ns;
631 
632 		/*
633 		 * Wa_14025941587 is applied before Wa_16023105232, which takes
634 		 * priority if the two ever conflict (not expected in practice).
635 		 */
636 		if (XE_GT_WA(gt, 14025941587) &&
637 		    idledly < mincnt_idledly_ns) {
638 			idledly = mincnt_idledly_ns;
639 			wa_applied = true;
640 		}
641 
642 		if (XE_GT_WA(gt, 16023105232)) {
643 			/* Clear the inhibit switch without disturbing a valid delay. */
644 			if (inhibit_switch) {
645 				idledly_reg_val &= ~INHIBIT_SWITCH_UNTIL_PREEMPTED;
646 				wa_applied = true;
647 			}
648 
649 			/* Warn only on the value read from hardware. */
650 			xe_gt_WARN_ON(gt, idledly_hw >= maxcnt);
651 
652 			if (idledly >= maxcnt) {
653 				/* maxcnt may be 0 if IDLE_WAIT_TIME is unprogrammed. */
654 				idledly = maxcnt ? maxcnt - 1 : 0;
655 				clamped_below_maxcnt = true;
656 				wa_applied = true;
657 			}
658 		}
659 
660 		if (wa_applied) {
661 			u32 idledly_ticks;
662 
663 			/*
664 			 * Wa_16023105232 requires idledly < maxcnt, so floor
665 			 * that clamp; otherwise round up to guarantee the
666 			 * Wa_14025941587 minimum survives tick quantization.
667 			 */
668 			if (clamped_below_maxcnt)
669 				idledly_ticks = idledly_floor_ticks(idledly, idledly_units_ps);
670 			else
671 				idledly_ticks = DIV_ROUND_UP_ULL((u64)idledly * 1000,
672 								 idledly_units_ps);
673 
674 			/*
675 			 * Tick quantization can still push the rounded-up value
676 			 * to/above maxcnt; re-floor here so Wa_16023105232 keeps
677 			 * priority even in that case.
678 			 */
679 			if (!clamped_below_maxcnt && XE_GT_WA(gt, 16023105232) &&
680 			    (u64)idledly_ticks * idledly_units_ps >= (u64)maxcnt * 1000) {
681 				xe_gt_dbg(gt, "idledly %s: %u ticks would exceed maxcnt=%u, so flooring\n",
682 					  hwe->name, idledly_ticks, maxcnt);
683 				idledly = maxcnt ? maxcnt - 1 : 0;
684 				idledly_ticks = idledly_floor_ticks(idledly, idledly_units_ps);
685 			}
686 
687 			idledly_reg_val &= ~IDLE_DELAY;
688 			idledly_reg_val |= REG_FIELD_PREP(IDLE_DELAY, idledly_ticks);
689 			xe_gt_dbg(gt, "idledly %s: set %u max=%u inh=%u ts=%u\n",
690 				  hwe->name, idledly, maxcnt,
691 				  !!inhibit_switch, gt->info.timestamp_base);
692 			xe_mmio_write32(&gt->mmio,
693 					RING_IDLEDLY(hwe->mmio_base),
694 					idledly_reg_val);
695 		}
696 	}
697 }
698 
699 static int hw_engine_init(struct xe_gt *gt, struct xe_hw_engine *hwe,
700 			  enum xe_hw_engine_id id)
701 {
702 	struct xe_device *xe = gt_to_xe(gt);
703 	struct xe_tile *tile = gt_to_tile(gt);
704 	int err;
705 
706 	xe_gt_assert(gt, id < ARRAY_SIZE(engine_infos) && engine_infos[id].name);
707 	xe_gt_assert(gt, gt->info.engine_mask & BIT(id));
708 
709 	xe_reg_sr_apply_mmio(&hwe->reg_sr, gt);
710 
711 	hwe->hwsp = xe_managed_bo_create_pin_map(xe, tile, SZ_4K,
712 						 XE_BO_FLAG_VRAM_IF_DGFX(tile) |
713 						 XE_BO_FLAG_GGTT |
714 						 XE_BO_FLAG_GGTT_INVALIDATE);
715 	if (IS_ERR(hwe->hwsp)) {
716 		err = PTR_ERR(hwe->hwsp);
717 		goto err_name;
718 	}
719 
720 	if (!xe_device_uc_enabled(xe)) {
721 		hwe->exl_port = xe_execlist_port_create(xe, hwe);
722 		if (IS_ERR(hwe->exl_port)) {
723 			err = PTR_ERR(hwe->exl_port);
724 			goto err_name;
725 		}
726 	} else {
727 		/* GSCCS has a special interrupt for reset */
728 		if (hwe->class == XE_ENGINE_CLASS_OTHER)
729 			hwe->irq_handler = xe_gsc_hwe_irq_handler;
730 
731 		if (!IS_SRIOV_VF(xe))
732 			xe_hw_engine_enable_ring(hwe);
733 	}
734 
735 	/* Ensure IDLEDLY is lower than MAXCNT */
736 	adjust_idledly(hwe);
737 
738 	return devm_add_action_or_reset(xe->drm.dev, hw_engine_fini, hwe);
739 
740 err_name:
741 	hwe->name = NULL;
742 
743 	return err;
744 }
745 
746 static int hw_engine_setup_logical_and_paging_mapping(struct xe_gt *gt)
747 {
748 	struct xe_device *xe = gt_to_xe(gt);
749 	unsigned int num_copy_engines = 0, num_paging_engines = 0;
750 	unsigned int reserved_logical_bcs_start;
751 	struct xe_hw_engine *hwe;
752 	enum xe_hw_engine_id id;
753 	int class;
754 
755 	for_each_hw_engine(hwe, gt, id)
756 		if (hwe->class == XE_ENGINE_CLASS_COPY)
757 			num_copy_engines++;
758 
759 	if (num_copy_engines && xe->info.has_usm)
760 		num_paging_engines = 1;
761 
762 	if (IS_SRIOV_VF(xe)) {
763 		u32 vf_num_paging_engines;
764 
765 		/*
766 		 * PF could in theory reserve multiple paging engines, which
767 		 * internally the submission/scheduling backend can load balance
768 		 * from. Not something we currently expect, but we are at the
769 		 * mercy of the PF, so we just need try our best to mirror the
770 		 * paging configuration.
771 		 */
772 		vf_num_paging_engines = xe_gt_sriov_vf_paging_engines(gt);
773 		if (vf_num_paging_engines) {
774 			/* This should only be non-zero on NVL-S+ */
775 			if (xe_gt_WARN_ON(gt, xe->info.platform < XE_NOVALAKE_S))
776 				return -EINVAL;
777 
778 			num_paging_engines = vf_num_paging_engines;
779 		}
780 	}
781 
782 	if (xe_gt_WARN_ON(gt, num_paging_engines > num_copy_engines))
783 		return -EINVAL;
784 
785 	/*
786 	 * On PF, we just reserve the highest BCS instance for USM.
787 	 *
788 	 * Note: This is now a requirement going forward. The PF must ALWAYS
789 	 * reserve BCS instances in top-down order, that way the VF has a chance
790 	 * of discovering the physical BCS instance mappings for paging engines,
791 	 * in conjunction with vf_num_paging_engines. In some places we might
792 	 * only have the physical instance, and from hw pov there is no such
793 	 * thing as a paging engine. For example, the page fault descriptor,
794 	 * which comes directly from the hw, will use the physical engine
795 	 * instance.
796 	 */
797 	reserved_logical_bcs_start = num_copy_engines - num_paging_engines;
798 
799 	/* FIXME: Doing a simple logical mapping that works for most hardware */
800 	for (class = 0; class < XE_ENGINE_CLASS_MAX; ++class) {
801 		int logical_instance = 0;
802 
803 		for_each_hw_engine(hwe, gt, id) {
804 			if (hwe->class == class) {
805 				hwe->logical_instance = logical_instance++;
806 
807 				if (class == XE_ENGINE_CLASS_COPY &&
808 				    hwe->logical_instance >=
809 					    reserved_logical_bcs_start) {
810 					if (!gt->usm.paging_hwe0)
811 						gt->usm.paging_hwe0 = hwe;
812 					gt->usm.paging_logical_mask |=
813 						BIT(hwe->logical_instance);
814 				}
815 			}
816 		}
817 	}
818 
819 	return 0;
820 }
821 
822 static void read_media_fuses(struct xe_gt *gt)
823 {
824 	struct xe_device *xe = gt_to_xe(gt);
825 	u32 media_fuse;
826 	u16 vdbox_mask;
827 	u16 vebox_mask;
828 	int i, j;
829 
830 	xe_force_wake_assert_held(gt_to_fw(gt), XE_FW_GT);
831 
832 	media_fuse = xe_mmio_read32(&gt->mmio, GT_VEBOX_VDBOX_DISABLE);
833 
834 	/*
835 	 * Pre-Xe_HP platforms had register bits representing absent engines,
836 	 * whereas Xe_HP and beyond have bits representing present engines.
837 	 * Invert the polarity on old platforms so that we can use common
838 	 * handling below.
839 	 */
840 	if (GRAPHICS_VERx100(xe) < 1250)
841 		media_fuse = ~media_fuse;
842 
843 	vdbox_mask = REG_FIELD_GET(GT_VDBOX_DISABLE_MASK, media_fuse);
844 	vebox_mask = REG_FIELD_GET(GT_VEBOX_DISABLE_MASK, media_fuse);
845 
846 	for (i = XE_HW_ENGINE_VCS0, j = 0; i <= XE_HW_ENGINE_VCS7; ++i, ++j) {
847 		if (!(gt->info.engine_mask & BIT(i)))
848 			continue;
849 
850 		if (!(BIT(j) & vdbox_mask)) {
851 			gt->info.engine_mask &= ~BIT(i);
852 			xe_gt_info(gt, "vcs%u fused off\n", j);
853 		}
854 	}
855 
856 	for (i = XE_HW_ENGINE_VECS0, j = 0; i <= XE_HW_ENGINE_VECS3; ++i, ++j) {
857 		if (!(gt->info.engine_mask & BIT(i)))
858 			continue;
859 
860 		if (!(BIT(j) & vebox_mask)) {
861 			gt->info.engine_mask &= ~BIT(i);
862 			xe_gt_info(gt, "vecs%u fused off\n", j);
863 		}
864 	}
865 }
866 
867 static u32 infer_svccopy_from_meml3(struct xe_gt *gt)
868 {
869 	u32 meml3 = REG_FIELD_GET(MEML3_EN_MASK,
870 				  xe_mmio_read32(&gt->mmio, MIRROR_FUSE3));
871 	u32 svccopy_mask = 0;
872 
873 	/*
874 	 * Each of the four meml3 bits determines the fusing of two service
875 	 * copy engines.
876 	 */
877 	for (int i = 0; i < 4; i++)
878 		svccopy_mask |= (meml3 & BIT(i)) ? 0b11 << 2 * i : 0;
879 
880 	return svccopy_mask;
881 }
882 
883 static u32 read_svccopy_fuses(struct xe_gt *gt)
884 {
885 	return REG_FIELD_GET(FUSE_SERVICE_COPY_ENABLE_MASK,
886 			     xe_mmio_read32(&gt->mmio, SERVICE_COPY_ENABLE));
887 }
888 
889 static void read_copy_fuses(struct xe_gt *gt)
890 {
891 	struct xe_device *xe = gt_to_xe(gt);
892 	u32 bcs_mask;
893 
894 	xe_force_wake_assert_held(gt_to_fw(gt), XE_FW_GT);
895 
896 	if (GRAPHICS_VER(xe) >= 35)
897 		bcs_mask = read_svccopy_fuses(gt);
898 	else if (GRAPHICS_VERx100(xe) == 1260)
899 		bcs_mask = infer_svccopy_from_meml3(gt);
900 	else
901 		return;
902 
903 	/* Only BCS1-BCS8 may be fused off */
904 	bcs_mask <<= XE_HW_ENGINE_BCS1;
905 	for (int i = XE_HW_ENGINE_BCS1; i <= XE_HW_ENGINE_BCS8; ++i) {
906 		if (!(gt->info.engine_mask & BIT(i)))
907 			continue;
908 
909 		if (!(bcs_mask & BIT(i))) {
910 			gt->info.engine_mask &= ~BIT(i);
911 			xe_gt_info(gt, "bcs%u fused off\n",
912 				   i - XE_HW_ENGINE_BCS0);
913 		}
914 	}
915 }
916 
917 static void read_compute_fuses_from_dss(struct xe_gt *gt)
918 {
919 	/*
920 	 * CCS fusing based on DSS masks only applies to platforms that can
921 	 * have more than one CCS.
922 	 */
923 	if (hweight64(gt->info.engine_mask &
924 		      GENMASK_ULL(XE_HW_ENGINE_CCS3, XE_HW_ENGINE_CCS0)) <= 1)
925 		return;
926 
927 	/*
928 	 * CCS availability on Xe_HP is inferred from the presence of DSS in
929 	 * each quadrant.
930 	 */
931 	for (int i = XE_HW_ENGINE_CCS0, j = 0; i <= XE_HW_ENGINE_CCS3; ++i, ++j) {
932 		if (!(gt->info.engine_mask & BIT(i)))
933 			continue;
934 
935 		if (!xe_gt_topology_has_dss_in_quadrant(gt, j)) {
936 			gt->info.engine_mask &= ~BIT(i);
937 			xe_gt_info(gt, "ccs%u fused off\n", j);
938 		}
939 	}
940 }
941 
942 static void read_compute_fuses_from_reg(struct xe_gt *gt)
943 {
944 	u32 ccs_mask;
945 
946 	ccs_mask = xe_mmio_read32(&gt->mmio, XEHP_FUSE4);
947 	ccs_mask = REG_FIELD_GET(CCS_EN_MASK, ccs_mask);
948 
949 	for (int i = XE_HW_ENGINE_CCS0, j = 0; i <= XE_HW_ENGINE_CCS3; ++i, ++j) {
950 		if (!(gt->info.engine_mask & BIT(i)))
951 			continue;
952 
953 		if ((ccs_mask & BIT(j)) == 0) {
954 			gt->info.engine_mask &= ~BIT(i);
955 			xe_gt_info(gt, "ccs%u fused off\n", j);
956 		}
957 	}
958 }
959 
960 static void read_compute_fuses(struct xe_gt *gt)
961 {
962 	if (GRAPHICS_VER(gt_to_xe(gt)) >= 20)
963 		read_compute_fuses_from_reg(gt);
964 	else
965 		read_compute_fuses_from_dss(gt);
966 }
967 
968 static void check_gsc_availability(struct xe_gt *gt)
969 {
970 	if (!(gt->info.engine_mask & BIT(XE_HW_ENGINE_GSCCS0)))
971 		return;
972 
973 	/*
974 	 * The GSCCS is only used to communicate with the GSC FW, so if we don't
975 	 * have the FW there is nothing we need the engine for and can therefore
976 	 * skip its initialization.
977 	 */
978 	if (!xe_uc_fw_is_available(&gt->uc.gsc.fw)) {
979 		gt->info.engine_mask &= ~BIT(XE_HW_ENGINE_GSCCS0);
980 
981 		/* interrupts where previously enabled, so turn them off */
982 		xe_mmio_write32(&gt->mmio, GUNIT_GSC_INTR_ENABLE, 0);
983 		xe_mmio_write32(&gt->mmio, GUNIT_GSC_INTR_MASK, ~0);
984 
985 		xe_gt_dbg(gt, "GSC FW not used, disabling gsccs\n");
986 	}
987 }
988 
989 static void check_sw_disable(struct xe_gt *gt)
990 {
991 	struct xe_device *xe = gt_to_xe(gt);
992 	u64 sw_allowed = xe_configfs_get_engines_allowed(to_pci_dev(xe->drm.dev));
993 	enum xe_hw_engine_id id;
994 
995 	for (id = 0; id < XE_NUM_HW_ENGINES; ++id) {
996 		if (!(gt->info.engine_mask & BIT(id)))
997 			continue;
998 
999 		if (!(sw_allowed & BIT(id))) {
1000 			gt->info.engine_mask &= ~BIT(id);
1001 			xe_gt_info(gt, "%s disabled via configfs\n",
1002 				   engine_infos[id].name);
1003 		}
1004 	}
1005 }
1006 
1007 int xe_hw_engines_init_early(struct xe_gt *gt)
1008 {
1009 	int i;
1010 
1011 	read_media_fuses(gt);
1012 	read_copy_fuses(gt);
1013 	read_compute_fuses(gt);
1014 	check_gsc_availability(gt);
1015 	check_sw_disable(gt);
1016 
1017 	BUILD_BUG_ON(XE_HW_ENGINE_PREEMPT_TIMEOUT < XE_HW_ENGINE_PREEMPT_TIMEOUT_MIN);
1018 	BUILD_BUG_ON(XE_HW_ENGINE_PREEMPT_TIMEOUT > XE_HW_ENGINE_PREEMPT_TIMEOUT_MAX);
1019 
1020 	for (i = 0; i < ARRAY_SIZE(gt->hw_engines); i++)
1021 		hw_engine_init_early(gt, &gt->hw_engines[i], i);
1022 
1023 	return 0;
1024 }
1025 
1026 int xe_hw_engines_init(struct xe_gt *gt)
1027 {
1028 	int err;
1029 	struct xe_hw_engine *hwe;
1030 	enum xe_hw_engine_id id;
1031 
1032 	for_each_hw_engine(hwe, gt, id) {
1033 		err = hw_engine_init(gt, hwe, id);
1034 		if (err)
1035 			return err;
1036 	}
1037 
1038 	err = hw_engine_setup_logical_and_paging_mapping(gt);
1039 	if (err)
1040 		return err;
1041 
1042 	err = xe_hw_engine_setup_groups(gt);
1043 	if (err)
1044 		return err;
1045 
1046 	return 0;
1047 }
1048 
1049 void xe_hw_engine_handle_irq(struct xe_hw_engine *hwe, u16 intr_vec)
1050 {
1051 	wake_up_all(&gt_to_xe(hwe->gt)->ufence_wq);
1052 
1053 	if (hwe->irq_handler)
1054 		hwe->irq_handler(hwe, intr_vec);
1055 
1056 	if (intr_vec & GT_MI_USER_INTERRUPT)
1057 		xe_hw_fence_irq_run(hwe->fence_irq);
1058 }
1059 
1060 /**
1061  * xe_hw_engine_snapshot_capture - Take a quick snapshot of the HW Engine.
1062  * @hwe: Xe HW Engine.
1063  * @q: The exec queue object.
1064  *
1065  * This can be printed out in a later stage like during dev_coredump
1066  * analysis.
1067  *
1068  * Returns: a Xe HW Engine snapshot object that must be freed by the
1069  * caller, using `xe_hw_engine_snapshot_free`.
1070  */
1071 struct xe_hw_engine_snapshot *
1072 xe_hw_engine_snapshot_capture(struct xe_hw_engine *hwe, struct xe_exec_queue *q)
1073 {
1074 	struct xe_hw_engine_snapshot *snapshot;
1075 	struct __guc_capture_parsed_output *node;
1076 
1077 	if (!xe_hw_engine_is_valid(hwe))
1078 		return NULL;
1079 
1080 	snapshot = kzalloc_obj(*snapshot, GFP_ATOMIC);
1081 
1082 	if (!snapshot)
1083 		return NULL;
1084 
1085 	snapshot->name = kstrdup(hwe->name, GFP_ATOMIC);
1086 	snapshot->hwe = hwe;
1087 	snapshot->logical_instance = hwe->logical_instance;
1088 	snapshot->forcewake.domain = hwe->domain;
1089 	snapshot->forcewake.ref = xe_force_wake_ref(gt_to_fw(hwe->gt),
1090 						    hwe->domain);
1091 	snapshot->mmio_base = hwe->mmio_base;
1092 	snapshot->kernel_reserved = xe_hw_engine_is_reserved(hwe);
1093 
1094 	/* no more VF accessible data below this point */
1095 	if (IS_SRIOV_VF(gt_to_xe(hwe->gt)))
1096 		return snapshot;
1097 
1098 	if (q) {
1099 		/* If got guc capture, set source to GuC */
1100 		node = xe_guc_capture_get_matching_and_lock(q);
1101 		if (node) {
1102 			struct xe_device *xe = gt_to_xe(hwe->gt);
1103 			struct xe_devcoredump *coredump = &xe->devcoredump;
1104 
1105 			coredump->snapshot.matched_node = node;
1106 			xe_gt_dbg(hwe->gt, "Found and locked GuC-err-capture node");
1107 			return snapshot;
1108 		}
1109 	}
1110 
1111 	/* otherwise, do manual capture */
1112 	xe_engine_manual_capture(hwe, snapshot);
1113 	xe_gt_dbg(hwe->gt, "Proceeding with manual engine snapshot");
1114 
1115 	return snapshot;
1116 }
1117 
1118 /**
1119  * xe_hw_engine_snapshot_free - Free all allocated objects for a given snapshot.
1120  * @snapshot: Xe HW Engine snapshot object.
1121  *
1122  * This function free all the memory that needed to be allocated at capture
1123  * time.
1124  */
1125 void xe_hw_engine_snapshot_free(struct xe_hw_engine_snapshot *snapshot)
1126 {
1127 	struct xe_gt *gt;
1128 	if (!snapshot)
1129 		return;
1130 
1131 	gt = snapshot->hwe->gt;
1132 	/*
1133 	 * xe_guc_capture_put_matched_nodes is called here and from
1134 	 * xe_devcoredump_snapshot_free, to cover the 2 calling paths
1135 	 * of hw_engines - debugfs and devcoredump free.
1136 	 */
1137 	xe_guc_capture_put_matched_nodes(&gt->uc.guc);
1138 
1139 	kfree(snapshot->name);
1140 	kfree(snapshot);
1141 }
1142 
1143 /**
1144  * xe_hw_engine_print - Xe HW Engine Print.
1145  * @hwe: Hardware Engine.
1146  * @p: drm_printer.
1147  *
1148  * This function quickly capture a snapshot and immediately print it out.
1149  */
1150 void xe_hw_engine_print(struct xe_hw_engine *hwe, struct drm_printer *p)
1151 {
1152 	struct xe_hw_engine_snapshot *snapshot;
1153 
1154 	snapshot = xe_hw_engine_snapshot_capture(hwe, NULL);
1155 	xe_engine_snapshot_print(snapshot, p);
1156 	xe_hw_engine_snapshot_free(snapshot);
1157 }
1158 
1159 u32 xe_hw_engine_mask_per_class(struct xe_gt *gt,
1160 				enum xe_engine_class engine_class)
1161 {
1162 	u32 mask = 0;
1163 	enum xe_hw_engine_id id;
1164 
1165 	for (id = 0; id < XE_NUM_HW_ENGINES; ++id) {
1166 		if (engine_infos[id].class == engine_class &&
1167 		    gt->info.engine_mask & BIT(id))
1168 			mask |= BIT(engine_infos[id].instance);
1169 	}
1170 	return mask;
1171 }
1172 
1173 bool xe_hw_engine_is_reserved(struct xe_hw_engine *hwe)
1174 {
1175 	struct xe_gt *gt = hwe->gt;
1176 	struct xe_device *xe = gt_to_xe(gt);
1177 
1178 	if (xe_device_is_admin_only(xe))
1179 		return true;
1180 
1181 	if (hwe->class == XE_ENGINE_CLASS_OTHER)
1182 		return true;
1183 
1184 	/* Check for engines disabled by ccs_mode setting */
1185 	if (xe_gt_ccs_mode_enabled(gt) &&
1186 	    hwe->class == XE_ENGINE_CLASS_COMPUTE &&
1187 	    hwe->logical_instance >= gt->ccs_mode)
1188 		return true;
1189 
1190 	return xe_gt_is_usm_hwe(gt, hwe);
1191 }
1192 
1193 const char *xe_hw_engine_class_to_str(enum xe_engine_class class)
1194 {
1195 	switch (class) {
1196 	case XE_ENGINE_CLASS_RENDER:
1197 		return "rcs";
1198 	case XE_ENGINE_CLASS_VIDEO_DECODE:
1199 		return "vcs";
1200 	case XE_ENGINE_CLASS_VIDEO_ENHANCE:
1201 		return "vecs";
1202 	case XE_ENGINE_CLASS_COPY:
1203 		return "bcs";
1204 	case XE_ENGINE_CLASS_OTHER:
1205 		return "other";
1206 	case XE_ENGINE_CLASS_COMPUTE:
1207 		return "ccs";
1208 	case XE_ENGINE_CLASS_MAX:
1209 		break;
1210 	}
1211 
1212 	return NULL;
1213 }
1214 
1215 u64 xe_hw_engine_read_timestamp(struct xe_hw_engine *hwe)
1216 {
1217 	return xe_mmio_read64_2x32(&hwe->gt->mmio, RING_TIMESTAMP(hwe->mmio_base));
1218 }
1219 
1220 enum xe_force_wake_domains xe_hw_engine_to_fw_domain(struct xe_hw_engine *hwe)
1221 {
1222 	return engine_infos[hwe->engine_id].domain;
1223 }
1224 
1225 static const enum xe_engine_class user_to_xe_engine_class[] = {
1226 	[DRM_XE_ENGINE_CLASS_RENDER] = XE_ENGINE_CLASS_RENDER,
1227 	[DRM_XE_ENGINE_CLASS_COPY] = XE_ENGINE_CLASS_COPY,
1228 	[DRM_XE_ENGINE_CLASS_VIDEO_DECODE] = XE_ENGINE_CLASS_VIDEO_DECODE,
1229 	[DRM_XE_ENGINE_CLASS_VIDEO_ENHANCE] = XE_ENGINE_CLASS_VIDEO_ENHANCE,
1230 	[DRM_XE_ENGINE_CLASS_COMPUTE] = XE_ENGINE_CLASS_COMPUTE,
1231 };
1232 
1233 /**
1234  * xe_hw_engine_lookup() - Lookup hardware engine for class:instance
1235  * @xe: xe device
1236  * @eci: engine class and instance
1237  *
1238  * This function will find a hardware engine for given engine
1239  * class and instance.
1240  *
1241  * Return: If found xe_hw_engine pointer, NULL otherwise.
1242  */
1243 struct xe_hw_engine *
1244 xe_hw_engine_lookup(struct xe_device *xe,
1245 		    struct drm_xe_engine_class_instance eci)
1246 {
1247 	struct xe_gt *gt = xe_device_get_gt(xe, eci.gt_id);
1248 	unsigned int idx;
1249 
1250 	if (eci.engine_class >= ARRAY_SIZE(user_to_xe_engine_class))
1251 		return NULL;
1252 
1253 	if (!gt)
1254 		return NULL;
1255 
1256 	idx = array_index_nospec(eci.engine_class,
1257 				 ARRAY_SIZE(user_to_xe_engine_class));
1258 
1259 	return xe_gt_hw_engine(xe_device_get_gt(xe, eci.gt_id),
1260 			       user_to_xe_engine_class[idx],
1261 			       eci.engine_instance, true);
1262 }
1263