xref: /linux/drivers/gpu/drm/xe/xe_gt_mcr.c (revision f6e8dc9edf963dbc99085e54f6ced6da9daa6100)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2022 Intel Corporation
4  */
5 
6 #include "xe_gt_mcr.h"
7 
8 #include "regs/xe_gt_regs.h"
9 #include "xe_assert.h"
10 #include "xe_gt.h"
11 #include "xe_gt_printk.h"
12 #include "xe_gt_topology.h"
13 #include "xe_gt_types.h"
14 #include "xe_guc_hwconfig.h"
15 #include "xe_mmio.h"
16 #include "xe_sriov.h"
17 
18 /**
19  * DOC: GT Multicast/Replicated (MCR) Register Support
20  *
21  * Some GT registers are designed as "multicast" or "replicated" registers:
22  * multiple instances of the same register share a single MMIO offset.  MCR
23  * registers are generally used when the hardware needs to potentially track
24  * independent values of a register per hardware unit (e.g., per-subslice,
25  * per-L3bank, etc.).  The specific types of replication that exist vary
26  * per-platform.
27  *
28  * MMIO accesses to MCR registers are controlled according to the settings
29  * programmed in the platform's MCR_SELECTOR register(s).  MMIO writes to MCR
30  * registers can be done in either multicast (a single write updates all
31  * instances of the register to the same value) or unicast (a write updates only
32  * one specific instance) form.  Reads of MCR registers always operate in a
33  * unicast manner regardless of how the multicast/unicast bit is set in
34  * MCR_SELECTOR.  Selection of a specific MCR instance for unicast operations is
35  * referred to as "steering."
36  *
37  * If MCR register operations are steered toward a hardware unit that is
38  * fused off or currently powered down due to power gating, the MMIO operation
39  * is "terminated" by the hardware.  Terminated read operations will return a
40  * value of zero and terminated unicast write operations will be silently
41  * ignored. During device initialization, the goal of the various
42  * ``init_steering_*()`` functions is to apply the platform-specific rules for
43  * each MCR register type to identify a steering target that will select a
44  * non-terminated instance.
45  *
46  * MCR registers are not available on Virtual Function (VF).
47  */
48 
49 static inline struct xe_reg to_xe_reg(struct xe_reg_mcr reg_mcr)
50 {
51 	return reg_mcr.__reg;
52 }
53 
54 enum {
55 	MCR_OP_READ,
56 	MCR_OP_WRITE
57 };
58 
59 static const struct xe_mmio_range xelp_l3bank_steering_table[] = {
60 	{ 0x00B100, 0x00B3FF },
61 	{},
62 };
63 
64 static const struct xe_mmio_range xehp_l3bank_steering_table[] = {
65 	{ 0x008C80, 0x008CFF },
66 	{ 0x00B100, 0x00B3FF },
67 	{},
68 };
69 
70 /*
71  * Although the bspec lists more "MSLICE" ranges than shown here, some of those
72  * are of a "GAM" subclass that has special rules and doesn't need to be
73  * included here.
74  */
75 static const struct xe_mmio_range xehp_mslice_steering_table[] = {
76 	{ 0x00DD00, 0x00DDFF },
77 	{ 0x00E900, 0x00FFFF }, /* 0xEA00 - OxEFFF is unused */
78 	{},
79 };
80 
81 static const struct xe_mmio_range xehp_lncf_steering_table[] = {
82 	{ 0x00B000, 0x00B0FF },
83 	{ 0x00D880, 0x00D8FF },
84 	{},
85 };
86 
87 /*
88  * We have several types of MCR registers where steering to (0,0) will always
89  * provide us with a non-terminated value.  We'll stick them all in the same
90  * table for simplicity.
91  */
92 static const struct xe_mmio_range xehpc_instance0_steering_table[] = {
93 	{ 0x004000, 0x004AFF },		/* HALF-BSLICE */
94 	{ 0x008800, 0x00887F },		/* CC */
95 	{ 0x008A80, 0x008AFF },		/* TILEPSMI */
96 	{ 0x00B000, 0x00B0FF },		/* HALF-BSLICE */
97 	{ 0x00B100, 0x00B3FF },		/* L3BANK */
98 	{ 0x00C800, 0x00CFFF },		/* HALF-BSLICE */
99 	{ 0x00D800, 0x00D8FF },		/* HALF-BSLICE */
100 	{ 0x00DD00, 0x00DDFF },		/* BSLICE */
101 	{ 0x00E900, 0x00E9FF },		/* HALF-BSLICE */
102 	{ 0x00EC00, 0x00EEFF },		/* HALF-BSLICE */
103 	{ 0x00F000, 0x00FFFF },		/* HALF-BSLICE */
104 	{ 0x024180, 0x0241FF },		/* HALF-BSLICE */
105 	{},
106 };
107 
108 static const struct xe_mmio_range xelpg_instance0_steering_table[] = {
109 	{ 0x000B00, 0x000BFF },         /* SQIDI */
110 	{ 0x001000, 0x001FFF },         /* SQIDI */
111 	{ 0x004000, 0x0048FF },         /* GAM */
112 	{ 0x008700, 0x0087FF },         /* SQIDI */
113 	{ 0x00B000, 0x00B0FF },         /* NODE */
114 	{ 0x00C800, 0x00CFFF },         /* GAM */
115 	{ 0x00D880, 0x00D8FF },         /* NODE */
116 	{ 0x00DD00, 0x00DDFF },         /* OAAL2 */
117 	{},
118 };
119 
120 static const struct xe_mmio_range xelpg_l3bank_steering_table[] = {
121 	{ 0x00B100, 0x00B3FF },
122 	{},
123 };
124 
125 static const struct xe_mmio_range xelp_dss_steering_table[] = {
126 	{ 0x008150, 0x00815F },
127 	{ 0x009520, 0x00955F },
128 	{ 0x00DE80, 0x00E8FF },
129 	{ 0x024A00, 0x024A7F },
130 	{},
131 };
132 
133 /* DSS steering is used for GSLICE ranges as well */
134 static const struct xe_mmio_range xehp_dss_steering_table[] = {
135 	{ 0x005200, 0x0052FF },		/* GSLICE */
136 	{ 0x005400, 0x007FFF },		/* GSLICE */
137 	{ 0x008140, 0x00815F },		/* GSLICE (0x8140-0x814F), DSS (0x8150-0x815F) */
138 	{ 0x008D00, 0x008DFF },		/* DSS */
139 	{ 0x0094D0, 0x00955F },		/* GSLICE (0x94D0-0x951F), DSS (0x9520-0x955F) */
140 	{ 0x009680, 0x0096FF },		/* DSS */
141 	{ 0x00D800, 0x00D87F },		/* GSLICE */
142 	{ 0x00DC00, 0x00DCFF },		/* GSLICE */
143 	{ 0x00DE80, 0x00E8FF },		/* DSS (0xE000-0xE0FF reserved ) */
144 	{ 0x017000, 0x017FFF },		/* GSLICE */
145 	{ 0x024A00, 0x024A7F },		/* DSS */
146 	{},
147 };
148 
149 /* DSS steering is used for COMPUTE ranges as well */
150 static const struct xe_mmio_range xehpc_dss_steering_table[] = {
151 	{ 0x008140, 0x00817F },		/* COMPUTE (0x8140-0x814F & 0x8160-0x817F), DSS (0x8150-0x815F) */
152 	{ 0x0094D0, 0x00955F },		/* COMPUTE (0x94D0-0x951F), DSS (0x9520-0x955F) */
153 	{ 0x009680, 0x0096FF },		/* DSS */
154 	{ 0x00DC00, 0x00DCFF },		/* COMPUTE */
155 	{ 0x00DE80, 0x00E7FF },		/* DSS (0xDF00-0xE1FF reserved ) */
156 	{},
157 };
158 
159 /* DSS steering is used for SLICE ranges as well */
160 static const struct xe_mmio_range xelpg_dss_steering_table[] = {
161 	{ 0x005200, 0x0052FF },		/* SLICE */
162 	{ 0x005500, 0x007FFF },		/* SLICE */
163 	{ 0x008140, 0x00815F },		/* SLICE (0x8140-0x814F), DSS (0x8150-0x815F) */
164 	{ 0x0094D0, 0x00955F },		/* SLICE (0x94D0-0x951F), DSS (0x9520-0x955F) */
165 	{ 0x009680, 0x0096FF },		/* DSS */
166 	{ 0x00D800, 0x00D87F },		/* SLICE */
167 	{ 0x00DC00, 0x00DCFF },		/* SLICE */
168 	{ 0x00DE80, 0x00E8FF },		/* DSS (0xE000-0xE0FF reserved) */
169 	{},
170 };
171 
172 static const struct xe_mmio_range xe3p_xpc_xecore_steering_table[] = {
173 	{ 0x008140, 0x00817F },         /* SLICE, XeCore, SLICE */
174 	{ 0x009480, 0x00955F },         /* SLICE, XeCore */
175 	{ 0x00D800, 0x00D87F },		/* SLICE */
176 	{ 0x00DC00, 0x00E9FF },         /* SLICE, rsvd, XeCore, rsvd, XeCore, rsvd, XeCore */
177 	{ 0x013000, 0x0135FF },         /* XeCore, SLICE */
178 	{},
179 };
180 
181 static const struct xe_mmio_range xelpmp_oaddrm_steering_table[] = {
182 	{ 0x393200, 0x39323F },
183 	{ 0x393400, 0x3934FF },
184 	{},
185 };
186 
187 static const struct xe_mmio_range dg2_implicit_steering_table[] = {
188 	{ 0x000B00, 0x000BFF },		/* SF (SQIDI replication) */
189 	{ 0x001000, 0x001FFF },		/* SF (SQIDI replication) */
190 	{ 0x004000, 0x004AFF },		/* GAM (MSLICE replication) */
191 	{ 0x008700, 0x0087FF },		/* MCFG (SQIDI replication) */
192 	{ 0x00C800, 0x00CFFF },		/* GAM (MSLICE replication) */
193 	{ 0x00F000, 0x00FFFF },		/* GAM (MSLICE replication) */
194 	{},
195 };
196 
197 static const struct xe_mmio_range xe2lpg_dss_steering_table[] = {
198 	{ 0x005200, 0x0052FF },         /* SLICE */
199 	{ 0x005500, 0x007FFF },         /* SLICE */
200 	{ 0x008140, 0x00815F },         /* SLICE (0x8140-0x814F), DSS (0x8150-0x815F) */
201 	{ 0x0094D0, 0x00955F },         /* SLICE (0x94D0-0x951F), DSS (0x9520-0x955F) */
202 	{ 0x009680, 0x0096FF },         /* DSS */
203 	{ 0x00D800, 0x00D87F },         /* SLICE */
204 	{ 0x00DC00, 0x00DCFF },         /* SLICE */
205 	{ 0x00DE80, 0x00E8FF },         /* DSS (0xE000-0xE0FF reserved) */
206 	{ 0x00E980, 0x00E9FF },         /* SLICE */
207 	{ 0x013000, 0x0133FF },         /* DSS (0x13000-0x131FF), SLICE (0x13200-0x133FF) */
208 	{},
209 };
210 
211 static const struct xe_mmio_range xe2lpg_sqidi_psmi_steering_table[] = {
212 	{ 0x000B00, 0x000BFF },
213 	{ 0x001000, 0x001FFF },
214 	{},
215 };
216 
217 static const struct xe_mmio_range xe2lpg_instance0_steering_table[] = {
218 	{ 0x004000, 0x004AFF },         /* GAM, rsvd, GAMWKR */
219 	{ 0x008700, 0x00887F },         /* SQIDI, MEMPIPE */
220 	{ 0x00B000, 0x00B3FF },         /* NODE, L3BANK */
221 	{ 0x00C800, 0x00CFFF },         /* GAM */
222 	{ 0x00D880, 0x00D8FF },         /* NODE */
223 	{ 0x00DD00, 0x00DDFF },         /* MEMPIPE */
224 	{ 0x00E900, 0x00E97F },         /* MEMPIPE */
225 	{ 0x00F000, 0x00FFFF },         /* GAM, GAMWKR */
226 	{ 0x013400, 0x0135FF },         /* MEMPIPE */
227 	{},
228 };
229 
230 static const struct xe_mmio_range xe2lpm_gpmxmt_steering_table[] = {
231 	{ 0x388160, 0x38817F },
232 	{ 0x389480, 0x3894CF },
233 	{},
234 };
235 
236 static const struct xe_mmio_range xe2lpm_instance0_steering_table[] = {
237 	{ 0x384000, 0x3847DF },         /* GAM, rsvd, GAM */
238 	{ 0x384900, 0x384AFF },         /* GAM */
239 	{ 0x389560, 0x3895FF },         /* MEDIAINF */
240 	{ 0x38B600, 0x38B8FF },         /* L3BANK */
241 	{ 0x38C800, 0x38D07F },         /* GAM, MEDIAINF */
242 	{ 0x38F000, 0x38F0FF },         /* GAM */
243 	{ 0x393C00, 0x393C7F },         /* MEDIAINF */
244 	{},
245 };
246 
247 static const struct xe_mmio_range xe3lpm_instance0_steering_table[] = {
248 	{ 0x384000, 0x3841FF },         /* GAM */
249 	{ 0x384400, 0x3847DF },         /* GAM */
250 	{ 0x384900, 0x384AFF },         /* GAM */
251 	{ 0x389560, 0x3895FF },         /* MEDIAINF */
252 	{ 0x38B600, 0x38B8FF },         /* L3BANK */
253 	{ 0x38C800, 0x38D07F },         /* GAM, MEDIAINF */
254 	{ 0x38D0D0, 0x38F0FF },         /* MEDIAINF, rsvd, GAM */
255 	{ 0x393C00, 0x393C7F },         /* MEDIAINF */
256 	{},
257 };
258 
259 /*
260  * Different "GAM" ranges have different rules; GAMWKRS, STLB, and GAMREQSTRM
261  * range subtypes need to be steered to (1,0), while all other GAM subtypes
262  * are steered to (0,0) and are included in the "INSTANCE0" table farther
263  * down.
264  */
265 static const struct xe_mmio_range xe3p_xpc_gam_grp1_steering_table[] = {
266 	{ 0x004000, 0x004AFF },		/* GAMREQSTRM, rsvd, STLB, GAMWKRS, GAMREQSTRM */
267 	{ 0x00F100, 0x00FFFF },		/* GAMWKRS */
268 	{},
269 };
270 
271 static const struct xe_mmio_range xe3p_xpc_psmi_grp19_steering_table[] = {
272 	{ 0x00B500, 0x00B5FF },
273 	{},
274 };
275 
276 static const struct xe_mmio_range xe3p_xpc_instance0_steering_table[] = {
277 	{ 0x00B600, 0x00B6FF },		/* PSMI0 */
278 	{ 0x00C800, 0x00CFFF },		/* GAMCTRL */
279 	{ 0x00F000, 0x00F0FF },		/* GAMCTRL */
280 	{},
281 };
282 
283 static void init_steering_l3bank(struct xe_gt *gt)
284 {
285 	struct xe_mmio *mmio = &gt->mmio;
286 
287 	if (GRAPHICS_VERx100(gt_to_xe(gt)) >= 1270) {
288 		u32 mslice_mask = REG_FIELD_GET(MEML3_EN_MASK,
289 						xe_mmio_read32(mmio, MIRROR_FUSE3));
290 		u32 bank_mask = REG_FIELD_GET(GT_L3_EXC_MASK,
291 					      xe_mmio_read32(mmio, XEHP_FUSE4));
292 
293 		/*
294 		 * Group selects mslice, instance selects bank within mslice.
295 		 * Bank 0 is always valid _except_ when the bank mask is 010b.
296 		 */
297 		gt->steering[L3BANK].group_target = __ffs(mslice_mask);
298 		gt->steering[L3BANK].instance_target =
299 			bank_mask & BIT(0) ? 0 : 2;
300 	} else if (gt_to_xe(gt)->info.platform == XE_DG2) {
301 		u32 mslice_mask = REG_FIELD_GET(MEML3_EN_MASK,
302 						xe_mmio_read32(mmio, MIRROR_FUSE3));
303 		u32 bank = __ffs(mslice_mask) * 8;
304 
305 		/*
306 		 * Like mslice registers, look for a valid mslice and steer to
307 		 * the first L3BANK of that quad. Access to the Nth L3 bank is
308 		 * split between the first bits of group and instance
309 		 */
310 		gt->steering[L3BANK].group_target = (bank >> 2) & 0x7;
311 		gt->steering[L3BANK].instance_target = bank & 0x3;
312 	} else {
313 		u32 fuse = REG_FIELD_GET(L3BANK_MASK,
314 					 ~xe_mmio_read32(mmio, MIRROR_FUSE3));
315 
316 		gt->steering[L3BANK].group_target = 0;	/* unused */
317 		gt->steering[L3BANK].instance_target = __ffs(fuse);
318 	}
319 }
320 
321 static void init_steering_mslice(struct xe_gt *gt)
322 {
323 	u32 mask = REG_FIELD_GET(MEML3_EN_MASK,
324 				 xe_mmio_read32(&gt->mmio, MIRROR_FUSE3));
325 
326 	/*
327 	 * mslice registers are valid (not terminated) if either the meml3
328 	 * associated with the mslice is present, or at least one DSS associated
329 	 * with the mslice is present.  There will always be at least one meml3
330 	 * so we can just use that to find a non-terminated mslice and ignore
331 	 * the DSS fusing.
332 	 */
333 	gt->steering[MSLICE].group_target = __ffs(mask);
334 	gt->steering[MSLICE].instance_target = 0;	/* unused */
335 
336 	/*
337 	 * LNCF termination is also based on mslice presence, so we'll set
338 	 * it up here.  Either LNCF within a non-terminated mslice will work,
339 	 * so we just always pick LNCF 0 here.
340 	 */
341 	gt->steering[LNCF].group_target = __ffs(mask) << 1;
342 	gt->steering[LNCF].instance_target = 0;		/* unused */
343 }
344 
345 static unsigned int dss_per_group(struct xe_gt *gt)
346 {
347 	struct xe_guc *guc = &gt->uc.guc;
348 	u32 max_slices = 0, max_subslices = 0;
349 	int ret;
350 
351 	/*
352 	 * Try to query the GuC's hwconfig table for the maximum number of
353 	 * slices and subslices.  These don't reflect the platform's actual
354 	 * slice/DSS counts, just the physical layout by which we should
355 	 * determine the steering targets.  On older platforms with older GuC
356 	 * firmware releases it's possible that these attributes may not be
357 	 * included in the table, so we can always fall back to the old
358 	 * hardcoded layouts.
359 	 */
360 #define HWCONFIG_ATTR_MAX_SLICES	1
361 #define HWCONFIG_ATTR_MAX_SUBSLICES	70
362 
363 	ret = xe_guc_hwconfig_lookup_u32(guc, HWCONFIG_ATTR_MAX_SLICES,
364 					 &max_slices);
365 	if (ret < 0 || max_slices == 0)
366 		goto fallback;
367 
368 	ret = xe_guc_hwconfig_lookup_u32(guc, HWCONFIG_ATTR_MAX_SUBSLICES,
369 					 &max_subslices);
370 	if (ret < 0 || max_subslices == 0)
371 		goto fallback;
372 
373 	return DIV_ROUND_UP(max_subslices, max_slices);
374 
375 fallback:
376 	/*
377 	 * Some older platforms don't have tables or don't have complete tables.
378 	 * Newer platforms should always have the required info.
379 	 */
380 	if (GRAPHICS_VERx100(gt_to_xe(gt)) >= 2000 &&
381 	    !gt_to_xe(gt)->info.force_execlist)
382 		xe_gt_err(gt, "Slice/Subslice counts missing from hwconfig table; using typical fallback values\n");
383 
384 	if (gt_to_xe(gt)->info.platform == XE_PVC)
385 		return 8;
386 	else if (GRAPHICS_VERx100(gt_to_xe(gt)) >= 1250)
387 		return 4;
388 	else
389 		return 6;
390 }
391 
392 /**
393  * xe_gt_mcr_get_dss_steering - Get the group/instance steering for a DSS
394  * @gt: GT structure
395  * @dss: DSS ID to obtain steering for
396  * @group: pointer to storage for steering group ID
397  * @instance: pointer to storage for steering instance ID
398  */
399 void xe_gt_mcr_get_dss_steering(const struct xe_gt *gt, unsigned int dss, u16 *group, u16 *instance)
400 {
401 	xe_gt_assert(gt, dss < XE_MAX_DSS_FUSE_BITS);
402 
403 	*group = dss / gt->steering_dss_per_grp;
404 	*instance = dss % gt->steering_dss_per_grp;
405 }
406 
407 /**
408  * xe_gt_mcr_steering_info_to_dss_id - Get DSS ID from group/instance steering
409  * @gt: GT structure
410  * @group: steering group ID
411  * @instance: steering instance ID
412  *
413  * Return: the converted DSS id.
414  */
415 u32 xe_gt_mcr_steering_info_to_dss_id(struct xe_gt *gt, u16 group, u16 instance)
416 {
417 	return group * dss_per_group(gt) + instance;
418 }
419 
420 static void init_steering_dss(struct xe_gt *gt)
421 {
422 	gt->steering_dss_per_grp = dss_per_group(gt);
423 
424 	xe_gt_mcr_get_dss_steering(gt,
425 				   min(xe_dss_mask_group_ffs(gt->fuse_topo.g_dss_mask, 0, 0),
426 				       xe_dss_mask_group_ffs(gt->fuse_topo.c_dss_mask, 0, 0)),
427 				   &gt->steering[DSS].group_target,
428 				   &gt->steering[DSS].instance_target);
429 }
430 
431 static void init_steering_oaddrm(struct xe_gt *gt)
432 {
433 	/*
434 	 * First instance is only terminated if the entire first media slice
435 	 * is absent (i.e., no VCS0 or VECS0).
436 	 */
437 	if (gt->info.engine_mask & (XE_HW_ENGINE_VCS0 | XE_HW_ENGINE_VECS0))
438 		gt->steering[OADDRM].group_target = 0;
439 	else
440 		gt->steering[OADDRM].group_target = 1;
441 
442 	gt->steering[OADDRM].instance_target = 0;	/* unused */
443 }
444 
445 static void init_steering_sqidi_psmi(struct xe_gt *gt)
446 {
447 	u32 mask = REG_FIELD_GET(XE2_NODE_ENABLE_MASK,
448 				 xe_mmio_read32(&gt->mmio, MIRROR_FUSE3));
449 	u32 select = __ffs(mask);
450 
451 	gt->steering[SQIDI_PSMI].group_target = select >> 1;
452 	gt->steering[SQIDI_PSMI].instance_target = select & 0x1;
453 }
454 
455 static void init_steering_psmi(struct xe_gt *gt)
456 {
457 	gt->steering[PSMI19].group_target = 19;
458 	gt->steering[PSMI19].instance_target = 0;
459 }
460 
461 static void init_steering_gam1(struct xe_gt *gt)
462 {
463 	gt->steering[GAM1].group_target = 1;
464 	gt->steering[GAM1].instance_target = 0;
465 }
466 
467 static const struct {
468 	const char *name;
469 	void (*init)(struct xe_gt *gt);
470 } xe_steering_types[] = {
471 	[L3BANK] =	{ "L3BANK",	init_steering_l3bank },
472 	[MSLICE] =	{ "MSLICE",	init_steering_mslice },
473 	[LNCF] =	{ "LNCF",	NULL }, /* initialized by mslice init */
474 	[DSS] =		{ "DSS / XeCore", init_steering_dss },
475 	[OADDRM] =	{ "OADDRM / GPMXMT", init_steering_oaddrm },
476 	[SQIDI_PSMI] =  { "SQIDI_PSMI", init_steering_sqidi_psmi },
477 	[PSMI19] =	{ "PSMI[19]",	init_steering_psmi },
478 	[GAM1] =	{ "GAMWKRS / STLB / GAMREQSTRM", init_steering_gam1 },
479 	[INSTANCE0] =	{ "INSTANCE 0",	NULL },
480 	[IMPLICIT_STEERING] = { "IMPLICIT", NULL },
481 };
482 
483 /**
484  * xe_gt_mcr_init_early - Early initialization of the MCR support
485  * @gt: GT structure
486  *
487  * Perform early software only initialization of the MCR lock to allow
488  * the synchronization on accessing the STEER_SEMAPHORE register and
489  * use the xe_gt_mcr_multicast_write() function, plus the minimum
490  * safe MCR registers required for VRAM/CCS probing.
491  */
492 void xe_gt_mcr_init_early(struct xe_gt *gt)
493 {
494 	struct xe_device *xe = gt_to_xe(gt);
495 
496 	BUILD_BUG_ON(IMPLICIT_STEERING + 1 != NUM_STEERING_TYPES);
497 	BUILD_BUG_ON(ARRAY_SIZE(xe_steering_types) != NUM_STEERING_TYPES);
498 
499 	spin_lock_init(&gt->mcr_lock);
500 
501 	if (IS_SRIOV_VF(xe))
502 		return;
503 
504 	if (gt->info.type == XE_GT_TYPE_MEDIA) {
505 		drm_WARN_ON(&xe->drm, MEDIA_VER(xe) < 13);
506 
507 		if (MEDIA_VER(xe) >= 30) {
508 			gt->steering[OADDRM].ranges = xe2lpm_gpmxmt_steering_table;
509 			gt->steering[INSTANCE0].ranges = xe3lpm_instance0_steering_table;
510 		} else if (MEDIA_VERx100(xe) >= 1301) {
511 			gt->steering[OADDRM].ranges = xe2lpm_gpmxmt_steering_table;
512 			gt->steering[INSTANCE0].ranges = xe2lpm_instance0_steering_table;
513 		} else {
514 			gt->steering[OADDRM].ranges = xelpmp_oaddrm_steering_table;
515 		}
516 	} else {
517 		if (GRAPHICS_VERx100(xe) == 3511) {
518 			/*
519 			 * TODO: there are some ranges in bspec with missing
520 			 * termination: [0x00B000, 0x00B0FF] and
521 			 * [0x00D880, 0x00D8FF] (NODE); [0x00B100, 0x00B3FF]
522 			 * (L3BANK). Update them here once bspec is updated.
523 			 */
524 			gt->steering[DSS].ranges = xe3p_xpc_xecore_steering_table;
525 			gt->steering[GAM1].ranges = xe3p_xpc_gam_grp1_steering_table;
526 			gt->steering[INSTANCE0].ranges = xe3p_xpc_instance0_steering_table;
527 			gt->steering[PSMI19].ranges = xe3p_xpc_psmi_grp19_steering_table;
528 		} else if (GRAPHICS_VER(xe) >= 20) {
529 			gt->steering[DSS].ranges = xe2lpg_dss_steering_table;
530 			gt->steering[SQIDI_PSMI].ranges = xe2lpg_sqidi_psmi_steering_table;
531 			gt->steering[INSTANCE0].ranges = xe2lpg_instance0_steering_table;
532 		} else if (GRAPHICS_VERx100(xe) >= 1270) {
533 			gt->steering[INSTANCE0].ranges = xelpg_instance0_steering_table;
534 			gt->steering[L3BANK].ranges = xelpg_l3bank_steering_table;
535 			gt->steering[DSS].ranges = xelpg_dss_steering_table;
536 		} else if (xe->info.platform == XE_PVC) {
537 			gt->steering[INSTANCE0].ranges = xehpc_instance0_steering_table;
538 			gt->steering[DSS].ranges = xehpc_dss_steering_table;
539 		} else if (xe->info.platform == XE_DG2) {
540 			gt->steering[L3BANK].ranges = xehp_l3bank_steering_table;
541 			gt->steering[MSLICE].ranges = xehp_mslice_steering_table;
542 			gt->steering[LNCF].ranges = xehp_lncf_steering_table;
543 			gt->steering[DSS].ranges = xehp_dss_steering_table;
544 			gt->steering[IMPLICIT_STEERING].ranges = dg2_implicit_steering_table;
545 		} else {
546 			gt->steering[L3BANK].ranges = xelp_l3bank_steering_table;
547 			gt->steering[DSS].ranges = xelp_dss_steering_table;
548 		}
549 	}
550 
551 	/* Mark instance 0 as initialized, we need this early for VRAM and CCS probe. */
552 	gt->steering[INSTANCE0].initialized = true;
553 }
554 
555 /**
556  * xe_gt_mcr_init - Normal initialization of the MCR support
557  * @gt: GT structure
558  *
559  * Perform normal initialization of the MCR for all usages.
560  */
561 void xe_gt_mcr_init(struct xe_gt *gt)
562 {
563 	if (IS_SRIOV_VF(gt_to_xe(gt)))
564 		return;
565 
566 	/* Select non-terminated steering target for each type */
567 	for (int i = 0; i < NUM_STEERING_TYPES; i++) {
568 		gt->steering[i].initialized = true;
569 		if (gt->steering[i].ranges && xe_steering_types[i].init)
570 			xe_steering_types[i].init(gt);
571 	}
572 }
573 
574 /**
575  * xe_gt_mcr_set_implicit_defaults - Initialize steer control registers
576  * @gt: GT structure
577  *
578  * Some register ranges don't need to have their steering control registers
579  * changed on each access - it's sufficient to set them once on initialization.
580  * This function sets those registers for each platform *
581  */
582 void xe_gt_mcr_set_implicit_defaults(struct xe_gt *gt)
583 {
584 	struct xe_device *xe = gt_to_xe(gt);
585 
586 	if (IS_SRIOV_VF(xe))
587 		return;
588 
589 	if (xe->info.platform == XE_DG2) {
590 		u32 steer_val = REG_FIELD_PREP(MCR_SLICE_MASK, 0) |
591 			REG_FIELD_PREP(MCR_SUBSLICE_MASK, 2);
592 
593 		xe_mmio_write32(&gt->mmio, STEER_SEMAPHORE, steer_val);
594 		xe_mmio_write32(&gt->mmio, SF_MCR_SELECTOR, steer_val);
595 		/*
596 		 * For GAM registers, all reads should be directed to instance 1
597 		 * (unicast reads against other instances are not allowed),
598 		 * and instance 1 is already the hardware's default steering
599 		 * target, which we never change
600 		 */
601 	}
602 }
603 
604 /*
605  * xe_gt_mcr_get_nonterminated_steering - find group/instance values that
606  *    will steer a register to a non-terminated instance
607  * @gt: GT structure
608  * @reg: register for which the steering is required
609  * @group: return variable for group steering
610  * @instance: return variable for instance steering
611  *
612  * This function returns a group/instance pair that is guaranteed to work for
613  * read steering of the given register. Note that a value will be returned even
614  * if the register is not replicated and therefore does not actually require
615  * steering.
616  *
617  * Returns true if the caller should steer to the @group/@instance values
618  * returned.  Returns false if the caller need not perform any steering
619  */
620 bool xe_gt_mcr_get_nonterminated_steering(struct xe_gt *gt,
621 					  struct xe_reg_mcr reg_mcr,
622 					  u8 *group, u8 *instance)
623 {
624 	const struct xe_reg reg = to_xe_reg(reg_mcr);
625 	const struct xe_mmio_range *implicit_ranges;
626 
627 	for (int type = 0; type < IMPLICIT_STEERING; type++) {
628 		if (!gt->steering[type].ranges)
629 			continue;
630 
631 		for (int i = 0; gt->steering[type].ranges[i].end > 0; i++) {
632 			if (xe_mmio_in_range(&gt->mmio, &gt->steering[type].ranges[i], reg)) {
633 				drm_WARN(&gt_to_xe(gt)->drm, !gt->steering[type].initialized,
634 					 "Uninitialized usage of MCR register %s/%#x\n",
635 					 xe_steering_types[type].name, reg.addr);
636 
637 				*group = gt->steering[type].group_target;
638 				*instance = gt->steering[type].instance_target;
639 				return true;
640 			}
641 		}
642 	}
643 
644 	implicit_ranges = gt->steering[IMPLICIT_STEERING].ranges;
645 	if (implicit_ranges)
646 		for (int i = 0; implicit_ranges[i].end > 0; i++)
647 			if (xe_mmio_in_range(&gt->mmio, &implicit_ranges[i], reg))
648 				return false;
649 
650 	/*
651 	 * Not found in a steering table and not a register with implicit
652 	 * steering. Just steer to 0/0 as a guess and raise a warning.
653 	 */
654 	drm_WARN(&gt_to_xe(gt)->drm, true,
655 		 "Did not find MCR register %#x in any MCR steering table\n",
656 		 reg.addr);
657 	*group = 0;
658 	*instance = 0;
659 
660 	return true;
661 }
662 
663 /*
664  * Obtain exclusive access to MCR steering.  On MTL and beyond we also need
665  * to synchronize with external clients (e.g., firmware), so a semaphore
666  * register will also need to be taken.
667  */
668 static void mcr_lock(struct xe_gt *gt) __acquires(&gt->mcr_lock)
669 {
670 	struct xe_device *xe = gt_to_xe(gt);
671 	int ret = 0;
672 
673 	spin_lock(&gt->mcr_lock);
674 
675 	/*
676 	 * Starting with MTL we also need to grab a semaphore register
677 	 * to synchronize with external agents (e.g., firmware) that now
678 	 * shares the same steering control register. The semaphore is obtained
679 	 * when a read to the relevant register returns 1.
680 	 */
681 	if (GRAPHICS_VERx100(xe) >= 1270)
682 		ret = xe_mmio_wait32(&gt->mmio, STEER_SEMAPHORE, 0x1, 0x1, 10, NULL,
683 				     true);
684 
685 	drm_WARN_ON_ONCE(&xe->drm, ret == -ETIMEDOUT);
686 }
687 
688 static void mcr_unlock(struct xe_gt *gt) __releases(&gt->mcr_lock)
689 {
690 	/* Release hardware semaphore - this is done by writing 1 to the register */
691 	if (GRAPHICS_VERx100(gt_to_xe(gt)) >= 1270)
692 		xe_mmio_write32(&gt->mmio, STEER_SEMAPHORE, 0x1);
693 
694 	spin_unlock(&gt->mcr_lock);
695 }
696 
697 /*
698  * Access a register with specific MCR steering
699  *
700  * Caller needs to make sure the relevant forcewake wells are up.
701  */
702 static u32 rw_with_mcr_steering(struct xe_gt *gt, struct xe_reg_mcr reg_mcr,
703 				u8 rw_flag, int group, int instance, u32 value)
704 {
705 	const struct xe_reg reg = to_xe_reg(reg_mcr);
706 	struct xe_mmio *mmio = &gt->mmio;
707 	struct xe_reg steer_reg;
708 	u32 steer_val, val = 0;
709 
710 	lockdep_assert_held(&gt->mcr_lock);
711 
712 	if (GRAPHICS_VERx100(gt_to_xe(gt)) >= 1270) {
713 		steer_reg = MTL_MCR_SELECTOR;
714 		steer_val = REG_FIELD_PREP(MTL_MCR_GROUPID, group) |
715 			REG_FIELD_PREP(MTL_MCR_INSTANCEID, instance);
716 	} else {
717 		steer_reg = MCR_SELECTOR;
718 		steer_val = REG_FIELD_PREP(MCR_SLICE_MASK, group) |
719 			REG_FIELD_PREP(MCR_SUBSLICE_MASK, instance);
720 	}
721 
722 	/*
723 	 * Always leave the hardware in multicast mode when doing reads and only
724 	 * change it to unicast mode when doing writes of a specific instance.
725 	 *
726 	 * The setting of the multicast/unicast bit usually wouldn't matter for
727 	 * read operations (which always return the value from a single register
728 	 * instance regardless of how that bit is set), but some platforms may
729 	 * have workarounds requiring us to remain in multicast mode for reads,
730 	 * e.g. Wa_22013088509 on PVC.  There's no real downside to this, so
731 	 * we'll just go ahead and do so on all platforms; we'll only clear the
732 	 * multicast bit from the mask when explicitly doing a write operation.
733 	 *
734 	 * No need to save old steering reg value.
735 	 */
736 	if (rw_flag == MCR_OP_READ)
737 		steer_val |= MCR_MULTICAST;
738 
739 	xe_mmio_write32(mmio, steer_reg, steer_val);
740 
741 	if (rw_flag == MCR_OP_READ)
742 		val = xe_mmio_read32(mmio, reg);
743 	else
744 		xe_mmio_write32(mmio, reg, value);
745 
746 	/*
747 	 * If we turned off the multicast bit (during a write) we're required
748 	 * to turn it back on before finishing.  The group and instance values
749 	 * don't matter since they'll be re-programmed on the next MCR
750 	 * operation.
751 	 */
752 	if (rw_flag == MCR_OP_WRITE)
753 		xe_mmio_write32(mmio, steer_reg, MCR_MULTICAST);
754 
755 	return val;
756 }
757 
758 /**
759  * xe_gt_mcr_unicast_read_any - reads a non-terminated instance of an MCR register
760  * @gt: GT structure
761  * @reg_mcr: register to read
762  *
763  * Reads a GT MCR register.  The read will be steered to a non-terminated
764  * instance (i.e., one that isn't fused off or powered down by power gating).
765  * This function assumes the caller is already holding any necessary forcewake
766  * domains.
767  *
768  * Returns the value from a non-terminated instance of @reg.
769  */
770 u32 xe_gt_mcr_unicast_read_any(struct xe_gt *gt, struct xe_reg_mcr reg_mcr)
771 {
772 	const struct xe_reg reg = to_xe_reg(reg_mcr);
773 	u8 group, instance;
774 	u32 val;
775 	bool steer;
776 
777 	xe_gt_assert(gt, !IS_SRIOV_VF(gt_to_xe(gt)));
778 
779 	steer = xe_gt_mcr_get_nonterminated_steering(gt, reg_mcr,
780 						     &group, &instance);
781 
782 	if (steer) {
783 		mcr_lock(gt);
784 		val = rw_with_mcr_steering(gt, reg_mcr, MCR_OP_READ,
785 					   group, instance, 0);
786 		mcr_unlock(gt);
787 	} else {
788 		val = xe_mmio_read32(&gt->mmio, reg);
789 	}
790 
791 	return val;
792 }
793 
794 /**
795  * xe_gt_mcr_unicast_read - read a specific instance of an MCR register
796  * @gt: GT structure
797  * @reg_mcr: the MCR register to read
798  * @group: the MCR group
799  * @instance: the MCR instance
800  *
801  * Returns the value read from an MCR register after steering toward a specific
802  * group/instance.
803  */
804 u32 xe_gt_mcr_unicast_read(struct xe_gt *gt,
805 			   struct xe_reg_mcr reg_mcr,
806 			   int group, int instance)
807 {
808 	u32 val;
809 
810 	xe_gt_assert(gt, !IS_SRIOV_VF(gt_to_xe(gt)));
811 
812 	mcr_lock(gt);
813 	val = rw_with_mcr_steering(gt, reg_mcr, MCR_OP_READ, group, instance, 0);
814 	mcr_unlock(gt);
815 
816 	return val;
817 }
818 
819 /**
820  * xe_gt_mcr_unicast_write - write a specific instance of an MCR register
821  * @gt: GT structure
822  * @reg_mcr: the MCR register to write
823  * @value: value to write
824  * @group: the MCR group
825  * @instance: the MCR instance
826  *
827  * Write an MCR register in unicast mode after steering toward a specific
828  * group/instance.
829  */
830 void xe_gt_mcr_unicast_write(struct xe_gt *gt, struct xe_reg_mcr reg_mcr,
831 			     u32 value, int group, int instance)
832 {
833 	xe_gt_assert(gt, !IS_SRIOV_VF(gt_to_xe(gt)));
834 
835 	mcr_lock(gt);
836 	rw_with_mcr_steering(gt, reg_mcr, MCR_OP_WRITE, group, instance, value);
837 	mcr_unlock(gt);
838 }
839 
840 /**
841  * xe_gt_mcr_multicast_write - write a value to all instances of an MCR register
842  * @gt: GT structure
843  * @reg_mcr: the MCR register to write
844  * @value: value to write
845  *
846  * Write an MCR register in multicast mode to update all instances.
847  */
848 void xe_gt_mcr_multicast_write(struct xe_gt *gt, struct xe_reg_mcr reg_mcr,
849 			       u32 value)
850 {
851 	struct xe_reg reg = to_xe_reg(reg_mcr);
852 
853 	xe_gt_assert(gt, !IS_SRIOV_VF(gt_to_xe(gt)));
854 
855 	/*
856 	 * Synchronize with any unicast operations.  Once we have exclusive
857 	 * access, the MULTICAST bit should already be set, so there's no need
858 	 * to touch the steering register.
859 	 */
860 	mcr_lock(gt);
861 	xe_mmio_write32(&gt->mmio, reg, value);
862 	mcr_unlock(gt);
863 }
864 
865 void xe_gt_mcr_steering_dump(struct xe_gt *gt, struct drm_printer *p)
866 {
867 	for (int i = 0; i < NUM_STEERING_TYPES; i++) {
868 		if (gt->steering[i].ranges) {
869 			drm_printf(p, "%s steering: group=%#x, instance=%#x\n",
870 				   xe_steering_types[i].name,
871 				   gt->steering[i].group_target,
872 				   gt->steering[i].instance_target);
873 			for (int j = 0; gt->steering[i].ranges[j].end; j++)
874 				drm_printf(p, "\t0x%06x - 0x%06x\n",
875 					   gt->steering[i].ranges[j].start,
876 					   gt->steering[i].ranges[j].end);
877 		}
878 	}
879 }
880