intel_gt_mcr.c 27.2 KB
Newer Older
1 2 3 4 5
// SPDX-License-Identifier: MIT
/*
 * Copyright © 2022 Intel Corporation
 */

6
#include "intel_gt.h"
7
#include "intel_gt_mcr.h"
8
#include "intel_gt_print.h"
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#include "intel_gt_regs.h"

/**
 * DOC: GT Multicast/Replicated (MCR) Register Support
 *
 * Some GT registers are designed as "multicast" or "replicated" registers:
 * multiple instances of the same register share a single MMIO offset.  MCR
 * registers are generally used when the hardware needs to potentially track
 * independent values of a register per hardware unit (e.g., per-subslice,
 * per-L3bank, etc.).  The specific types of replication that exist vary
 * per-platform.
 *
 * MMIO accesses to MCR registers are controlled according to the settings
 * programmed in the platform's MCR_SELECTOR register(s).  MMIO writes to MCR
 * registers can be done in either a (i.e., a single write updates all
 * instances of the register to the same value) or unicast (a write updates only
 * one specific instance).  Reads of MCR registers always operate in a unicast
 * manner regardless of how the multicast/unicast bit is set in MCR_SELECTOR.
 * Selection of a specific MCR instance for unicast operations is referred to
 * as "steering."
 *
 * If MCR register operations are steered toward a hardware unit that is
 * fused off or currently powered down due to power gating, the MMIO operation
 * is "terminated" by the hardware.  Terminated read operations will return a
 * value of zero and terminated unicast write operations will be silently
 * ignored.
 */

37
#define HAS_MSLICE_STEERING(i915)	(INTEL_INFO(i915)->has_mslice_steering)
38 39 40 41 42

static const char * const intel_steering_types[] = {
	"L3BANK",
	"MSLICE",
	"LNCF",
43
	"GAM",
44
	"DSS",
45
	"OADDRM",
46 47 48 49 50 51 52 53
	"INSTANCE 0",
};

static const struct intel_mmio_range icl_l3bank_steering_table[] = {
	{ 0x00B100, 0x00B3FF },
	{},
};

54 55 56 57 58
/*
 * Although the bspec lists more "MSLICE" ranges than shown here, some of those
 * are of a "GAM" subclass that has special rules.  Thus we use a separate
 * GAM table farther down for those.
 */
59 60 61 62 63 64
static const struct intel_mmio_range xehpsdv_mslice_steering_table[] = {
	{ 0x00DD00, 0x00DDFF },
	{ 0x00E900, 0x00FFFF }, /* 0xEA00 - OxEFFF is unused */
	{},
};

65 66 67 68 69 70
static const struct intel_mmio_range xehpsdv_gam_steering_table[] = {
	{ 0x004000, 0x004AFF },
	{ 0x00C800, 0x00CFFF },
	{},
};

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
static const struct intel_mmio_range xehpsdv_lncf_steering_table[] = {
	{ 0x00B000, 0x00B0FF },
	{ 0x00D800, 0x00D8FF },
	{},
};

static const struct intel_mmio_range dg2_lncf_steering_table[] = {
	{ 0x00B000, 0x00B0FF },
	{ 0x00D880, 0x00D8FF },
	{},
};

/*
 * We have several types of MCR registers on PVC where steering to (0,0)
 * will always provide us with a non-terminated value.  We'll stick them
 * all in the same table for simplicity.
 */
static const struct intel_mmio_range pvc_instance0_steering_table[] = {
	{ 0x004000, 0x004AFF },		/* HALF-BSLICE */
	{ 0x008800, 0x00887F },		/* CC */
	{ 0x008A80, 0x008AFF },		/* TILEPSMI */
	{ 0x00B000, 0x00B0FF },		/* HALF-BSLICE */
	{ 0x00B100, 0x00B3FF },		/* L3BANK */
	{ 0x00C800, 0x00CFFF },		/* HALF-BSLICE */
	{ 0x00D800, 0x00D8FF },		/* HALF-BSLICE */
	{ 0x00DD00, 0x00DDFF },		/* BSLICE */
	{ 0x00E900, 0x00E9FF },		/* HALF-BSLICE */
	{ 0x00EC00, 0x00EEFF },		/* HALF-BSLICE */
	{ 0x00F000, 0x00FFFF },		/* HALF-BSLICE */
	{ 0x024180, 0x0241FF },		/* HALF-BSLICE */
	{},
};

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
static const struct intel_mmio_range xelpg_instance0_steering_table[] = {
	{ 0x000B00, 0x000BFF },         /* SQIDI */
	{ 0x001000, 0x001FFF },         /* SQIDI */
	{ 0x004000, 0x0048FF },         /* GAM */
	{ 0x008700, 0x0087FF },         /* SQIDI */
	{ 0x00B000, 0x00B0FF },         /* NODE */
	{ 0x00C800, 0x00CFFF },         /* GAM */
	{ 0x00D880, 0x00D8FF },         /* NODE */
	{ 0x00DD00, 0x00DDFF },         /* OAAL2 */
	{},
};

static const struct intel_mmio_range xelpg_l3bank_steering_table[] = {
	{ 0x00B100, 0x00B3FF },
	{},
};

/* DSS steering is used for SLICE ranges as well */
static const struct intel_mmio_range xelpg_dss_steering_table[] = {
	{ 0x005200, 0x0052FF },		/* SLICE */
	{ 0x005500, 0x007FFF },		/* SLICE */
	{ 0x008140, 0x00815F },		/* SLICE (0x8140-0x814F), DSS (0x8150-0x815F) */
	{ 0x0094D0, 0x00955F },		/* SLICE (0x94D0-0x951F), DSS (0x9520-0x955F) */
	{ 0x009680, 0x0096FF },		/* DSS */
	{ 0x00D800, 0x00D87F },		/* SLICE */
	{ 0x00DC00, 0x00DCFF },		/* SLICE */
	{ 0x00DE80, 0x00E8FF },		/* DSS (0xE000-0xE0FF reserved) */
131
	{},
132 133
};

134 135 136
static const struct intel_mmio_range xelpmp_oaddrm_steering_table[] = {
	{ 0x393200, 0x39323F },
	{ 0x393400, 0x3934FF },
137
	{},
138 139
};

140 141 142
void intel_gt_mcr_init(struct intel_gt *gt)
{
	struct drm_i915_private *i915 = gt->i915;
143 144
	unsigned long fuse;
	int i;
145

146 147
	spin_lock_init(&gt->mcr_lock);

148 149 150 151 152 153 154 155 156 157 158 159 160
	/*
	 * An mslice is unavailable only if both the meml3 for the slice is
	 * disabled *and* all of the DSS in the slice (quadrant) are disabled.
	 */
	if (HAS_MSLICE_STEERING(i915)) {
		gt->info.mslice_mask =
			intel_slicemask_from_xehp_dssmask(gt->info.sseu.subslice_mask,
							  GEN_DSS_PER_MSLICE);
		gt->info.mslice_mask |=
			(intel_uncore_read(gt->uncore, GEN10_MIRROR_FUSE3) &
			 GEN12_MEML3_EN_MASK);

		if (!gt->info.mslice_mask) /* should be impossible! */
161
			gt_warn(gt, "mslice mask all zero!\n");
162 163
	}

164 165 166
	if (MEDIA_VER(i915) >= 13 && gt->type == GT_MEDIA) {
		gt->steering_table[OADDRM] = xelpmp_oaddrm_steering_table;
	} else if (GRAPHICS_VER_FULL(i915) >= IP_VER(12, 70)) {
167
		/* Wa_14016747170 */
168 169
		if (IS_GFX_GT_IP_STEP(gt, IP_VER(12, 70), STEP_A0, STEP_B0) ||
		    IS_GFX_GT_IP_STEP(gt, IP_VER(12, 71), STEP_A0, STEP_B0))
170 171 172 173 174 175
			fuse = REG_FIELD_GET(MTL_GT_L3_EXC_MASK,
					     intel_uncore_read(gt->uncore,
							       MTL_GT_ACTIVITY_FACTOR));
		else
			fuse = REG_FIELD_GET(GT_L3_EXC_MASK,
					     intel_uncore_read(gt->uncore, XEHP_FUSE4));
176 177 178 179 180 181 182 183 184 185 186 187

		/*
		 * Despite the register field being named "exclude mask" the
		 * bits actually represent enabled banks (two banks per bit).
		 */
		for_each_set_bit(i, &fuse, 3)
			gt->info.l3bank_mask |= 0x3 << 2 * i;

		gt->steering_table[INSTANCE0] = xelpg_instance0_steering_table;
		gt->steering_table[L3BANK] = xelpg_l3bank_steering_table;
		gt->steering_table[DSS] = xelpg_dss_steering_table;
	} else if (IS_PONTEVECCHIO(i915)) {
188 189 190 191
		gt->steering_table[INSTANCE0] = pvc_instance0_steering_table;
	} else if (IS_DG2(i915)) {
		gt->steering_table[MSLICE] = xehpsdv_mslice_steering_table;
		gt->steering_table[LNCF] = dg2_lncf_steering_table;
192 193 194 195 196
		/*
		 * No need to hook up the GAM table since it has a dedicated
		 * steering control register on DG2 and can use implicit
		 * steering.
		 */
197 198 199
	} else if (IS_XEHPSDV(i915)) {
		gt->steering_table[MSLICE] = xehpsdv_mslice_steering_table;
		gt->steering_table[LNCF] = xehpsdv_lncf_steering_table;
200
		gt->steering_table[GAM] = xehpsdv_gam_steering_table;
201 202 203 204 205 206 207
	} else if (GRAPHICS_VER(i915) >= 11 &&
		   GRAPHICS_VER_FULL(i915) < IP_VER(12, 50)) {
		gt->steering_table[L3BANK] = icl_l3bank_steering_table;
		gt->info.l3bank_mask =
			~intel_uncore_read(gt->uncore, GEN10_MIRROR_FUSE3) &
			GEN10_L3BANK_MASK;
		if (!gt->info.l3bank_mask) /* should be impossible! */
208
			gt_warn(gt, "L3 bank mask is all zero!\n");
209 210 211 212 213 214 215 216 217
	} else if (GRAPHICS_VER(i915) >= 11) {
		/*
		 * We expect all modern platforms to have at least some
		 * type of steering that needs to be initialized.
		 */
		MISSING_CASE(INTEL_INFO(i915)->platform);
	}
}

218 219 220 221 222 223 224 225 226 227 228 229 230
/*
 * Although the rest of the driver should use MCR-specific functions to
 * read/write MCR registers, we still use the regular intel_uncore_* functions
 * internally to implement those, so we need a way for the functions in this
 * file to "cast" an i915_mcr_reg_t into an i915_reg_t.
 */
static i915_reg_t mcr_reg_cast(const i915_mcr_reg_t mcr)
{
	i915_reg_t r = { .reg = mcr.reg };

	return r;
}

231 232
/*
 * rw_with_mcr_steering_fw - Access a register with specific MCR steering
233
 * @gt: GT to read register from
234 235
 * @reg: register being accessed
 * @rw_flag: FW_REG_READ for read access or FW_REG_WRITE for write access
236 237
 * @group: group number (documented as "sliceid" on older platforms)
 * @instance: instance number (documented as "subsliceid" on older platforms)
238 239
 * @value: register value to be written (ignored for read)
 *
240
 * Context: The caller must hold the MCR lock
241 242 243 244
 * Return: 0 for write access. register value for read access.
 *
 * Caller needs to make sure the relevant forcewake wells are up.
 */
245
static u32 rw_with_mcr_steering_fw(struct intel_gt *gt,
246
				   i915_mcr_reg_t reg, u8 rw_flag,
247
				   int group, int instance, u32 value)
248
{
249
	struct intel_uncore *uncore = gt->uncore;
250 251
	u32 mcr_mask, mcr_ss, mcr, old_mcr, val = 0;

252
	lockdep_assert_held(&gt->mcr_lock);
253

254 255 256 257 258 259 260 261 262 263 264
	if (GRAPHICS_VER_FULL(uncore->i915) >= IP_VER(12, 70)) {
		/*
		 * Always leave the hardware in multicast mode when doing reads
		 * (see comment about Wa_22013088509 below) and only change it
		 * to unicast mode when doing writes of a specific instance.
		 *
		 * No need to save old steering reg value.
		 */
		intel_uncore_write_fw(uncore, MTL_MCR_SELECTOR,
				      REG_FIELD_PREP(MTL_MCR_GROUPID, group) |
				      REG_FIELD_PREP(MTL_MCR_INSTANCEID, instance) |
265
				      (rw_flag == FW_REG_READ ? GEN11_MCR_MULTICAST : 0));
266
	} else if (GRAPHICS_VER(uncore->i915) >= 11) {
267
		mcr_mask = GEN11_MCR_SLICE_MASK | GEN11_MCR_SUBSLICE_MASK;
268
		mcr_ss = GEN11_MCR_SLICE(group) | GEN11_MCR_SUBSLICE(instance);
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283

		/*
		 * Wa_22013088509
		 *
		 * The setting of the multicast/unicast bit usually wouldn't
		 * matter for read operations (which always return the value
		 * from a single register instance regardless of how that bit
		 * is set), but some platforms have a workaround requiring us
		 * to remain in multicast mode for reads.  There's no real
		 * downside to this, so we'll just go ahead and do so on all
		 * platforms; we'll only clear the multicast bit from the mask
		 * when exlicitly doing a write operation.
		 */
		if (rw_flag == FW_REG_WRITE)
			mcr_mask |= GEN11_MCR_MULTICAST;
284 285 286 287 288 289 290

		mcr = intel_uncore_read_fw(uncore, GEN8_MCR_SELECTOR);
		old_mcr = mcr;

		mcr &= ~mcr_mask;
		mcr |= mcr_ss;
		intel_uncore_write_fw(uncore, GEN8_MCR_SELECTOR, mcr);
291 292
	} else {
		mcr_mask = GEN8_MCR_SLICE_MASK | GEN8_MCR_SUBSLICE_MASK;
293
		mcr_ss = GEN8_MCR_SLICE(group) | GEN8_MCR_SUBSLICE(instance);
294

295 296
		mcr = intel_uncore_read_fw(uncore, GEN8_MCR_SELECTOR);
		old_mcr = mcr;
297

298 299 300 301
		mcr &= ~mcr_mask;
		mcr |= mcr_ss;
		intel_uncore_write_fw(uncore, GEN8_MCR_SELECTOR, mcr);
	}
302 303

	if (rw_flag == FW_REG_READ)
304
		val = intel_uncore_read_fw(uncore, mcr_reg_cast(reg));
305
	else
306
		intel_uncore_write_fw(uncore, mcr_reg_cast(reg), value);
307

308 309 310 311 312 313 314 315 316 317
	/*
	 * For pre-MTL platforms, we need to restore the old value of the
	 * steering control register to ensure that implicit steering continues
	 * to behave as expected.  For MTL and beyond, we need only reinstate
	 * the 'multicast' bit (and only if we did a write that cleared it).
	 */
	if (GRAPHICS_VER_FULL(uncore->i915) >= IP_VER(12, 70) && rw_flag == FW_REG_WRITE)
		intel_uncore_write_fw(uncore, MTL_MCR_SELECTOR, GEN11_MCR_MULTICAST);
	else if (GRAPHICS_VER_FULL(uncore->i915) < IP_VER(12, 70))
		intel_uncore_write_fw(uncore, GEN8_MCR_SELECTOR, old_mcr);
318 319 320 321

	return val;
}

322
static u32 rw_with_mcr_steering(struct intel_gt *gt,
323
				i915_mcr_reg_t reg, u8 rw_flag,
324 325
				int group, int instance,
				u32 value)
326
{
327
	struct intel_uncore *uncore = gt->uncore;
328
	enum forcewake_domains fw_domains;
329
	unsigned long flags;
330 331
	u32 val;

332
	fw_domains = intel_uncore_forcewake_for_reg(uncore, mcr_reg_cast(reg),
333 334 335 336 337
						    rw_flag);
	fw_domains |= intel_uncore_forcewake_for_reg(uncore,
						     GEN8_MCR_SELECTOR,
						     FW_REG_READ | FW_REG_WRITE);

338 339
	intel_gt_mcr_lock(gt, &flags);
	spin_lock(&uncore->lock);
340 341
	intel_uncore_forcewake_get__locked(uncore, fw_domains);

342
	val = rw_with_mcr_steering_fw(gt, reg, rw_flag, group, instance, value);
343 344

	intel_uncore_forcewake_put__locked(uncore, fw_domains);
345 346
	spin_unlock(&uncore->lock);
	intel_gt_mcr_unlock(gt, flags);
347 348 349 350

	return val;
}

351 352 353 354 355 356
/**
 * intel_gt_mcr_lock - Acquire MCR steering lock
 * @gt: GT structure
 * @flags: storage to save IRQ flags to
 *
 * Performs locking to protect the steering for the duration of an MCR
357 358 359
 * operation.  On MTL and beyond, a hardware lock will also be taken to
 * serialize access not only for the driver, but also for external hardware and
 * firmware agents.
360 361 362 363 364 365
 *
 * Context: Takes gt->mcr_lock.  uncore->lock should *not* be held when this
 *          function is called, although it may be acquired after this
 *          function call.
 */
void intel_gt_mcr_lock(struct intel_gt *gt, unsigned long *flags)
366
	__acquires(&gt->mcr_lock)
367 368
{
	unsigned long __flags;
369
	int err = 0;
370 371 372

	lockdep_assert_not_held(&gt->uncore->lock);

373 374 375 376 377
	/*
	 * Starting with MTL, we need to coordinate not only with other
	 * driver threads, but also with hardware/firmware agents.  A dedicated
	 * locking register is used.
	 */
378
	if (GRAPHICS_VER_FULL(gt->i915) >= IP_VER(12, 70))
379 380 381 382 383 384 385 386 387
		err = wait_for(intel_uncore_read_fw(gt->uncore,
						    MTL_STEER_SEMAPHORE) == 0x1, 100);

	/*
	 * Even on platforms with a hardware lock, we'll continue to grab
	 * a software spinlock too for lockdep purposes.  If the hardware lock
	 * was already acquired, there should never be contention on the
	 * software lock.
	 */
388 389 390
	spin_lock_irqsave(&gt->mcr_lock, __flags);

	*flags = __flags;
391 392 393 394 395 396 397

	/*
	 * In theory we should never fail to acquire the HW semaphore; this
	 * would indicate some hardware/firmware is misbehaving and not
	 * releasing it properly.
	 */
	if (err == -ETIMEDOUT) {
398
		gt_err_ratelimited(gt, "hardware MCR steering semaphore timed out");
399 400
		add_taint_for_CI(gt->i915, TAINT_WARN);  /* CI is now unreliable */
	}
401 402 403 404 405 406 407 408 409 410 411 412
}

/**
 * intel_gt_mcr_unlock - Release MCR steering lock
 * @gt: GT structure
 * @flags: IRQ flags to restore
 *
 * Releases the lock acquired by intel_gt_mcr_lock().
 *
 * Context: Releases gt->mcr_lock
 */
void intel_gt_mcr_unlock(struct intel_gt *gt, unsigned long flags)
413
	__releases(&gt->mcr_lock)
414 415
{
	spin_unlock_irqrestore(&gt->mcr_lock, flags);
416

417
	if (GRAPHICS_VER_FULL(gt->i915) >= IP_VER(12, 70))
418
		intel_uncore_write_fw(gt->uncore, MTL_STEER_SEMAPHORE, 0x1);
419 420
}

421 422 423 424 425 426 427
/**
 * intel_gt_mcr_read - read a specific instance of an MCR register
 * @gt: GT structure
 * @reg: the MCR register to read
 * @group: the MCR group
 * @instance: the MCR instance
 *
428 429
 * Context: Takes and releases gt->mcr_lock
 *
430 431 432 433
 * Returns the value read from an MCR register after steering toward a specific
 * group/instance.
 */
u32 intel_gt_mcr_read(struct intel_gt *gt,
434
		      i915_mcr_reg_t reg,
435
		      int group, int instance)
436
{
437
	return rw_with_mcr_steering(gt, reg, FW_REG_READ, group, instance, 0);
438 439
}

440 441 442 443 444 445 446 447 448 449
/**
 * intel_gt_mcr_unicast_write - write a specific instance of an MCR register
 * @gt: GT structure
 * @reg: the MCR register to write
 * @value: value to write
 * @group: the MCR group
 * @instance: the MCR instance
 *
 * Write an MCR register in unicast mode after steering toward a specific
 * group/instance.
450 451
 *
 * Context: Calls a function that takes and releases gt->mcr_lock
452
 */
453
void intel_gt_mcr_unicast_write(struct intel_gt *gt, i915_mcr_reg_t reg, u32 value,
454
				int group, int instance)
455
{
456
	rw_with_mcr_steering(gt, reg, FW_REG_WRITE, group, instance, value);
457 458
}

459 460 461 462 463 464 465
/**
 * intel_gt_mcr_multicast_write - write a value to all instances of an MCR register
 * @gt: GT structure
 * @reg: the MCR register to write
 * @value: value to write
 *
 * Write an MCR register in multicast mode to update all instances.
466 467
 *
 * Context: Takes and releases gt->mcr_lock
468 469
 */
void intel_gt_mcr_multicast_write(struct intel_gt *gt,
470
				  i915_mcr_reg_t reg, u32 value)
471
{
472 473 474 475
	unsigned long flags;

	intel_gt_mcr_lock(gt, &flags);

476 477 478 479 480 481 482
	/*
	 * Ensure we have multicast behavior, just in case some non-i915 agent
	 * left the hardware in unicast mode.
	 */
	if (GRAPHICS_VER_FULL(gt->i915) >= IP_VER(12, 70))
		intel_uncore_write_fw(gt->uncore, MTL_MCR_SELECTOR, GEN11_MCR_MULTICAST);

483
	intel_uncore_write(gt->uncore, mcr_reg_cast(reg), value);
484 485

	intel_gt_mcr_unlock(gt, flags);
486 487 488
}

/**
489 490 491 492 493 494 495 496 497
 * intel_gt_mcr_multicast_write_fw - write a value to all instances of an MCR register
 * @gt: GT structure
 * @reg: the MCR register to write
 * @value: value to write
 *
 * Write an MCR register in multicast mode to update all instances.  This
 * function assumes the caller is already holding any necessary forcewake
 * domains; use intel_gt_mcr_multicast_write() in cases where forcewake should
 * be obtained automatically.
498 499
 *
 * Context: The caller must hold gt->mcr_lock.
500
 */
501
void intel_gt_mcr_multicast_write_fw(struct intel_gt *gt, i915_mcr_reg_t reg, u32 value)
502
{
503 504
	lockdep_assert_held(&gt->mcr_lock);

505 506 507 508 509 510 511
	/*
	 * Ensure we have multicast behavior, just in case some non-i915 agent
	 * left the hardware in unicast mode.
	 */
	if (GRAPHICS_VER_FULL(gt->i915) >= IP_VER(12, 70))
		intel_uncore_write_fw(gt->uncore, MTL_MCR_SELECTOR, GEN11_MCR_MULTICAST);

512
	intel_uncore_write_fw(gt->uncore, mcr_reg_cast(reg), value);
513 514
}

515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
/**
 * intel_gt_mcr_multicast_rmw - Performs a multicast RMW operations
 * @gt: GT structure
 * @reg: the MCR register to read and write
 * @clear: bits to clear during RMW
 * @set: bits to set during RMW
 *
 * Performs a read-modify-write on an MCR register in a multicast manner.
 * This operation only makes sense on MCR registers where all instances are
 * expected to have the same value.  The read will target any non-terminated
 * instance and the write will be applied to all instances.
 *
 * This function assumes the caller is already holding any necessary forcewake
 * domains; use intel_gt_mcr_multicast_rmw() in cases where forcewake should
 * be obtained automatically.
 *
531 532
 * Context: Calls functions that take and release gt->mcr_lock
 *
533 534
 * Returns the old (unmodified) value read.
 */
535
u32 intel_gt_mcr_multicast_rmw(struct intel_gt *gt, i915_mcr_reg_t reg,
536 537 538 539 540 541 542 543 544
			       u32 clear, u32 set)
{
	u32 val = intel_gt_mcr_read_any(gt, reg);

	intel_gt_mcr_multicast_write(gt, reg, (val & ~clear) | set);

	return val;
}

545 546 547
/*
 * reg_needs_read_steering - determine whether a register read requires
 *     explicit steering
548 549 550 551 552 553 554 555 556 557 558
 * @gt: GT structure
 * @reg: the register to check steering requirements for
 * @type: type of multicast steering to check
 *
 * Determines whether @reg needs explicit steering of a specific type for
 * reads.
 *
 * Returns false if @reg does not belong to a register range of the given
 * steering type, or if the default (subslice-based) steering IDs are suitable
 * for @type steering too.
 */
559
static bool reg_needs_read_steering(struct intel_gt *gt,
560
				    i915_mcr_reg_t reg,
561
				    enum intel_steering_type type)
562
{
563
	u32 offset = i915_mmio_reg_offset(reg);
564 565
	const struct intel_mmio_range *entry;

566
	if (likely(!gt->steering_table[type]))
567 568
		return false;

569 570 571
	if (IS_GSI_REG(offset))
		offset += gt->uncore->gsi_offset;

572 573 574 575 576 577 578 579
	for (entry = gt->steering_table[type]; entry->end; entry++) {
		if (offset >= entry->start && offset <= entry->end)
			return true;
	}

	return false;
}

580 581
/*
 * get_nonterminated_steering - determines valid IDs for a class of MCR steering
582 583
 * @gt: GT structure
 * @type: multicast register type
584 585
 * @group: Group ID returned
 * @instance: Instance ID returned
586
 *
587 588
 * Determines group and instance values that will steer reads of the specified
 * MCR class to a non-terminated instance.
589
 */
590 591 592
static void get_nonterminated_steering(struct intel_gt *gt,
				       enum intel_steering_type type,
				       u8 *group, u8 *instance)
593
{
594 595
	u32 dss;

596 597
	switch (type) {
	case L3BANK:
598 599
		*group = 0;		/* unused */
		*instance = __ffs(gt->info.l3bank_mask);
600 601 602
		break;
	case MSLICE:
		GEM_WARN_ON(!HAS_MSLICE_STEERING(gt->i915));
603 604
		*group = __ffs(gt->info.mslice_mask);
		*instance = 0;	/* unused */
605 606 607 608 609 610 611
		break;
	case LNCF:
		/*
		 * An LNCF is always present if its mslice is present, so we
		 * can safely just steer to LNCF 0 in all cases.
		 */
		GEM_WARN_ON(!HAS_MSLICE_STEERING(gt->i915));
612 613
		*group = __ffs(gt->info.mslice_mask) << 1;
		*instance = 0;	/* unused */
614
		break;
615 616 617 618
	case GAM:
		*group = IS_DG2(gt->i915) ? 1 : 0;
		*instance = 0;
		break;
619 620 621 622 623
	case DSS:
		dss = intel_sseu_find_first_xehp_dss(&gt->info.sseu, 0, 0);
		*group = dss / GEN_DSS_PER_GSLICE;
		*instance = dss % GEN_DSS_PER_GSLICE;
		break;
624 625 626 627 628
	case INSTANCE0:
		/*
		 * There are a lot of MCR types for which instance (0, 0)
		 * will always provide a non-terminated value.
		 */
629 630
		*group = 0;
		*instance = 0;
631 632 633 634 635 636 637
		break;
	case OADDRM:
		if ((VDBOX_MASK(gt) | VEBOX_MASK(gt) | gt->info.sfc_mask) & BIT(0))
			*group = 0;
		else
			*group = 1;
		*instance = 0;
638 639 640
		break;
	default:
		MISSING_CASE(type);
641 642
		*group = 0;
		*instance = 0;
643 644 645 646
	}
}

/**
647 648
 * intel_gt_mcr_get_nonterminated_steering - find group/instance values that
 *    will steer a register to a non-terminated instance
649 650
 * @gt: GT structure
 * @reg: register for which the steering is required
651 652
 * @group: return variable for group steering
 * @instance: return variable for instance steering
653
 *
654
 * This function returns a group/instance pair that is guaranteed to work for
655 656 657 658
 * read steering of the given register. Note that a value will be returned even
 * if the register is not replicated and therefore does not actually require
 * steering.
 */
659
void intel_gt_mcr_get_nonterminated_steering(struct intel_gt *gt,
660
					     i915_mcr_reg_t reg,
661
					     u8 *group, u8 *instance)
662 663 664 665
{
	int type;

	for (type = 0; type < NUM_STEERING_TYPES; type++) {
666 667
		if (reg_needs_read_steering(gt, reg, type)) {
			get_nonterminated_steering(gt, type, group, instance);
668 669 670 671
			return;
		}
	}

672 673
	*group = gt->default_steering.groupid;
	*instance = gt->default_steering.instanceid;
674 675 676
}

/**
677
 * intel_gt_mcr_read_any_fw - reads one instance of an MCR register
678 679 680
 * @gt: GT structure
 * @reg: register to read
 *
681 682 683 684 685
 * Reads a GT MCR register.  The read will be steered to a non-terminated
 * instance (i.e., one that isn't fused off or powered down by power gating).
 * This function assumes the caller is already holding any necessary forcewake
 * domains; use intel_gt_mcr_read_any() in cases where forcewake should be
 * obtained automatically.
686
 *
687 688
 * Context: The caller must hold gt->mcr_lock.
 *
689
 * Returns the value from a non-terminated instance of @reg.
690
 */
691
u32 intel_gt_mcr_read_any_fw(struct intel_gt *gt, i915_mcr_reg_t reg)
692 693
{
	int type;
694
	u8 group, instance;
695

696 697
	lockdep_assert_held(&gt->mcr_lock);

698
	for (type = 0; type < NUM_STEERING_TYPES; type++) {
699 700
		if (reg_needs_read_steering(gt, reg, type)) {
			get_nonterminated_steering(gt, type, &group, &instance);
701
			return rw_with_mcr_steering_fw(gt, reg,
702 703
						       FW_REG_READ,
						       group, instance, 0);
704 705 706
		}
	}

707
	return intel_uncore_read_fw(gt->uncore, mcr_reg_cast(reg));
708 709
}

710 711 712 713 714 715 716 717
/**
 * intel_gt_mcr_read_any - reads one instance of an MCR register
 * @gt: GT structure
 * @reg: register to read
 *
 * Reads a GT MCR register.  The read will be steered to a non-terminated
 * instance (i.e., one that isn't fused off or powered down by power gating).
 *
718 719
 * Context: Calls a function that takes and releases gt->mcr_lock.
 *
720 721
 * Returns the value from a non-terminated instance of @reg.
 */
722
u32 intel_gt_mcr_read_any(struct intel_gt *gt, i915_mcr_reg_t reg)
723 724
{
	int type;
725
	u8 group, instance;
726 727

	for (type = 0; type < NUM_STEERING_TYPES; type++) {
728 729
		if (reg_needs_read_steering(gt, reg, type)) {
			get_nonterminated_steering(gt, type, &group, &instance);
730
			return rw_with_mcr_steering(gt, reg,
731 732
						    FW_REG_READ,
						    group, instance, 0);
733 734 735
		}
	}

736
	return intel_uncore_read(gt->uncore, mcr_reg_cast(reg));
737 738 739 740 741 742 743 744
}

static void report_steering_type(struct drm_printer *p,
				 struct intel_gt *gt,
				 enum intel_steering_type type,
				 bool dump_table)
{
	const struct intel_mmio_range *entry;
745
	u8 group, instance;
746 747 748 749 750 751 752 753 754

	BUILD_BUG_ON(ARRAY_SIZE(intel_steering_types) != NUM_STEERING_TYPES);

	if (!gt->steering_table[type]) {
		drm_printf(p, "%s steering: uses default steering\n",
			   intel_steering_types[type]);
		return;
	}

755 756 757
	get_nonterminated_steering(gt, type, &group, &instance);
	drm_printf(p, "%s steering: group=0x%x, instance=0x%x\n",
		   intel_steering_types[type], group, instance);
758 759 760 761 762 763 764 765

	if (!dump_table)
		return;

	for (entry = gt->steering_table[type]; entry->end; entry++)
		drm_printf(p, "\t0x%06x - 0x%06x\n", entry->start, entry->end);
}

766 767
void intel_gt_mcr_report_steering(struct drm_printer *p, struct intel_gt *gt,
				  bool dump_table)
768
{
769 770 771 772 773 774 775 776 777 778 779 780 781 782
	/*
	 * Starting with MTL we no longer have default steering;
	 * all ranges are explicitly steered.
	 */
	if (GRAPHICS_VER_FULL(gt->i915) < IP_VER(12, 70))
		drm_printf(p, "Default steering: group=0x%x, instance=0x%x\n",
			   gt->default_steering.groupid,
			   gt->default_steering.instanceid);

	if (GRAPHICS_VER_FULL(gt->i915) >= IP_VER(12, 70)) {
		for (int i = 0; i < NUM_STEERING_TYPES; i++)
			if (gt->steering_table[i])
				report_steering_type(p, gt, i, dump_table);
	} else if (IS_PONTEVECCHIO(gt->i915)) {
783 784 785 786 787 788 789
		report_steering_type(p, gt, INSTANCE0, dump_table);
	} else if (HAS_MSLICE_STEERING(gt->i915)) {
		report_steering_type(p, gt, MSLICE, dump_table);
		report_steering_type(p, gt, LNCF, dump_table);
	}
}

790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
/**
 * intel_gt_mcr_get_ss_steering - returns the group/instance steering for a SS
 * @gt: GT structure
 * @dss: DSS ID to obtain steering for
 * @group: pointer to storage for steering group ID
 * @instance: pointer to storage for steering instance ID
 *
 * Returns the steering IDs (via the @group and @instance parameters) that
 * correspond to a specific subslice/DSS ID.
 */
void intel_gt_mcr_get_ss_steering(struct intel_gt *gt, unsigned int dss,
				   unsigned int *group, unsigned int *instance)
{
	if (IS_PONTEVECCHIO(gt->i915)) {
		*group = dss / GEN_DSS_PER_CSLICE;
		*instance = dss % GEN_DSS_PER_CSLICE;
	} else if (GRAPHICS_VER_FULL(gt->i915) >= IP_VER(12, 50)) {
		*group = dss / GEN_DSS_PER_GSLICE;
		*instance = dss % GEN_DSS_PER_GSLICE;
	} else {
810
		*group = dss / GEN_MAX_SS_PER_HSW_SLICE;
811 812 813 814
		*instance = dss % GEN_MAX_SS_PER_HSW_SLICE;
		return;
	}
}
815 816

/**
817
 * intel_gt_mcr_wait_for_reg - wait until MCR register matches expected state
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
 * @gt: GT structure
 * @reg: the register to read
 * @mask: mask to apply to register value
 * @value: value to wait for
 * @fast_timeout_us: fast timeout in microsecond for atomic/tight wait
 * @slow_timeout_ms: slow timeout in millisecond
 *
 * This routine waits until the target register @reg contains the expected
 * @value after applying the @mask, i.e. it waits until ::
 *
 *     (intel_gt_mcr_read_any_fw(gt, reg) & mask) == value
 *
 * Otherwise, the wait will timeout after @slow_timeout_ms milliseconds.
 * For atomic context @slow_timeout_ms must be zero and @fast_timeout_us
 * must be not larger than 20,0000 microseconds.
 *
 * This function is basically an MCR-friendly version of
 * __intel_wait_for_register_fw().  Generally this function will only be used
 * on GAM registers which are a bit special --- although they're MCR registers,
 * reads (e.g., waiting for status updates) are always directed to the primary
 * instance.
 *
 * Note that this routine assumes the caller holds forcewake asserted, it is
 * not suitable for very long waits.
 *
843
 * Context: Calls a function that takes and releases gt->mcr_lock
844 845
 * Return: 0 if the register matches the desired condition, or -ETIMEDOUT.
 */
846 847 848 849 850 851
int intel_gt_mcr_wait_for_reg(struct intel_gt *gt,
			      i915_mcr_reg_t reg,
			      u32 mask,
			      u32 value,
			      unsigned int fast_timeout_us,
			      unsigned int slow_timeout_ms)
852 853 854
{
	int ret;

855
	lockdep_assert_not_held(&gt->mcr_lock);
856 857 858

#define done ((intel_gt_mcr_read_any(gt, reg) & mask) == value)

859 860 861 862 863 864 865 866 867 868 869 870 871 872
	/* Catch any overuse of this function */
	might_sleep_if(slow_timeout_ms);
	GEM_BUG_ON(fast_timeout_us > 20000);
	GEM_BUG_ON(!fast_timeout_us && !slow_timeout_ms);

	ret = -ETIMEDOUT;
	if (fast_timeout_us && fast_timeout_us <= 20000)
		ret = _wait_for_atomic(done, fast_timeout_us, 0);
	if (ret && slow_timeout_ms)
		ret = wait_for(done, slow_timeout_ms);

	return ret;
#undef done
}