intel_mocs.c 17.3 KB
Newer Older
C
Chris Wilson 已提交
1
// SPDX-License-Identifier: MIT
2
/*
C
Chris Wilson 已提交
3
 * Copyright © 2015 Intel Corporation
4 5
 */

6 7 8
#include "i915_drv.h"

#include "intel_engine.h"
9
#include "intel_gt.h"
10
#include "intel_lrc_reg.h"
11
#include "intel_mocs.h"
12
#include "intel_ring.h"
13 14 15 16 17

/* structures required */
struct drm_i915_mocs_entry {
	u32 control_value;
	u16 l3cc_value;
18
	u16 used;
19 20 21
};

struct drm_i915_mocs_table {
22 23
	unsigned int size;
	unsigned int n_entries;
24
	const struct drm_i915_mocs_entry *table;
25
	u8 uc_index;
26
	u8 unused_entries_index;
27 28 29
};

/* Defines for the tables (XXX_MOCS_0 - XXX_MOCS_63) */
30 31
#define _LE_CACHEABILITY(value)	((value) << 0)
#define _LE_TGT_CACHE(value)	((value) << 2)
32 33 34 35 36 37
#define LE_LRUM(value)		((value) << 4)
#define LE_AOM(value)		((value) << 6)
#define LE_RSC(value)		((value) << 7)
#define LE_SCC(value)		((value) << 8)
#define LE_PFM(value)		((value) << 11)
#define LE_SCF(value)		((value) << 14)
38 39
#define LE_COS(value)		((value) << 15)
#define LE_SSE(value)		((value) << 17)
40 41 42 43

/* Defines for the tables (LNCFMOCS0 - LNCFMOCS31) - two entries per word */
#define L3_ESC(value)		((value) << 0)
#define L3_SCC(value)		((value) << 1)
44
#define _L3_CACHEABILITY(value)	((value) << 4)
45 46
#define L3_GLBGO(value)		((value) << 6)
#define L3_LKUP(value)		((value) << 7)
47 48

/* Helper defines */
49
#define GEN9_NUM_MOCS_ENTRIES	64  /* 63-64 are reserved, but configured. */
50 51

/* (e)LLC caching options */
52 53 54 55
/*
 * Note: LE_0_PAGETABLE works only up to Gen11; for newer gens it means
 * the same as LE_UC
 */
56 57 58 59
#define LE_0_PAGETABLE		_LE_CACHEABILITY(0)
#define LE_1_UC			_LE_CACHEABILITY(1)
#define LE_2_WT			_LE_CACHEABILITY(2)
#define LE_3_WB			_LE_CACHEABILITY(3)
60 61

/* Target cache */
62 63 64 65 66 67 68 69 70 71
#define LE_TC_0_PAGETABLE	_LE_TGT_CACHE(0)
#define LE_TC_1_LLC		_LE_TGT_CACHE(1)
#define LE_TC_2_LLC_ELLC	_LE_TGT_CACHE(2)
#define LE_TC_3_LLC_ELLC_ALT	_LE_TGT_CACHE(3)

/* L3 caching options */
#define L3_0_DIRECT		_L3_CACHEABILITY(0)
#define L3_1_UC			_L3_CACHEABILITY(1)
#define L3_2_RESERVED		_L3_CACHEABILITY(2)
#define L3_3_WB			_L3_CACHEABILITY(3)
72

73 74 75 76
#define MOCS_ENTRY(__idx, __control_value, __l3cc_value) \
	[__idx] = { \
		.control_value = __control_value, \
		.l3cc_value = __l3cc_value, \
77
		.used = 1, \
78 79
	}

80 81 82 83 84 85 86 87 88
/*
 * MOCS tables
 *
 * These are the MOCS tables that are programmed across all the rings.
 * The control value is programmed to all the rings that support the
 * MOCS registers. While the l3cc_values are only programmed to the
 * LNCFCMOCS0 - LNCFCMOCS32 registers.
 *
 * These tables are intended to be kept reasonably consistent across
89 90 91
 * HW platforms, and for ICL+, be identical across OSes. To achieve
 * that, for Icelake and above, list of entries is published as part
 * of bspec.
92 93
 *
 * Entries not part of the following tables are undefined as far as
94
 * userspace is concerned and shouldn't be relied upon.  For Gen < 12
95 96 97
 * they will be initialized to PTE. Gen >= 12 don't have a setting for
 * PTE and those platforms except TGL/RKL will be initialized L3 WB to
 * catch accidental use of reserved and unused mocs indexes.
98
 *
99
 * The last few entries are reserved by the hardware. For ICL+ they
100 101 102
 * should be initialized according to bspec and never used, for older
 * platforms they should never be written to.
 *
103
 * NOTE1: These tables are part of bspec and defined as part of hardware
104 105 106 107
 *       interface for ICL+. For older platforms, they are part of kernel
 *       ABI. It is expected that, for specific hardware platform, existing
 *       entries will remain constant and the table will only be updated by
 *       adding new entries, filling unused positions.
108 109 110 111 112 113
 *
 * NOTE2: For GEN >= 12 except TGL and RKL, reserved and unspecified MOCS
 *       indices have been set to L3 WB. These reserved entries should never
 *       be used, they may be changed to low performant variants with better
 *       coherency in the future if more entries are needed.
 *       For TGL/RKL, all the unspecified MOCS indexes are mapped to L3 UC.
114
 */
115
#define GEN9_MOCS_ENTRIES \
116 117 118 119
	MOCS_ENTRY(I915_MOCS_UNCACHED, \
		   LE_1_UC | LE_TC_2_LLC_ELLC, \
		   L3_1_UC), \
	MOCS_ENTRY(I915_MOCS_PTE, \
120
		   LE_0_PAGETABLE | LE_TC_0_PAGETABLE | LE_LRUM(3), \
121
		   L3_3_WB)
122

123
static const struct drm_i915_mocs_entry skl_mocs_table[] = {
124
	GEN9_MOCS_ENTRIES,
125 126
	MOCS_ENTRY(I915_MOCS_CACHED,
		   LE_3_WB | LE_TC_2_LLC_ELLC | LE_LRUM(3),
127 128 129 130 131 132 133 134 135 136 137 138 139
		   L3_3_WB),

	/*
	 * mocs:63
	 * - used by the L3 for all of its evictions.
	 *   Thus it is expected to allow LLC cacheability to enable coherent
	 *   flows to be maintained.
	 * - used to force L3 uncachable cycles.
	 *   Thus it is expected to make the surface L3 uncacheable.
	 */
	MOCS_ENTRY(63,
		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3),
		   L3_1_UC)
140 141 142 143
};

/* NOTE: the LE_TGT_CACHE is not used on Broxton */
static const struct drm_i915_mocs_entry broxton_mocs_table[] = {
144
	GEN9_MOCS_ENTRIES,
145 146 147
	MOCS_ENTRY(I915_MOCS_CACHED,
		   LE_1_UC | LE_TC_2_LLC_ELLC | LE_LRUM(3),
		   L3_3_WB)
148 149
};

150
#define GEN11_MOCS_ENTRIES \
151
	/* Entries 0 and 1 are defined per-platform */ \
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
	/* Base - L3 + LLC */ \
	MOCS_ENTRY(2, \
		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3), \
		   L3_3_WB), \
	/* Base - Uncached */ \
	MOCS_ENTRY(3, \
		   LE_1_UC | LE_TC_1_LLC, \
		   L3_1_UC), \
	/* Base - L3 */ \
	MOCS_ENTRY(4, \
		   LE_1_UC | LE_TC_1_LLC, \
		   L3_3_WB), \
	/* Base - LLC */ \
	MOCS_ENTRY(5, \
		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3), \
		   L3_1_UC), \
	/* Age 0 - LLC */ \
	MOCS_ENTRY(6, \
		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(1), \
		   L3_1_UC), \
	/* Age 0 - L3 + LLC */ \
	MOCS_ENTRY(7, \
		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(1), \
		   L3_3_WB), \
	/* Age: Don't Chg. - LLC */ \
	MOCS_ENTRY(8, \
		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(2), \
		   L3_1_UC), \
	/* Age: Don't Chg. - L3 + LLC */ \
	MOCS_ENTRY(9, \
		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(2), \
		   L3_3_WB), \
	/* No AOM - LLC */ \
	MOCS_ENTRY(10, \
		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_AOM(1), \
		   L3_1_UC), \
	/* No AOM - L3 + LLC */ \
	MOCS_ENTRY(11, \
		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_AOM(1), \
		   L3_3_WB), \
	/* No AOM; Age 0 - LLC */ \
	MOCS_ENTRY(12, \
		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(1) | LE_AOM(1), \
		   L3_1_UC), \
	/* No AOM; Age 0 - L3 + LLC */ \
	MOCS_ENTRY(13, \
		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(1) | LE_AOM(1), \
		   L3_3_WB), \
	/* No AOM; Age:DC - LLC */ \
	MOCS_ENTRY(14, \
		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(2) | LE_AOM(1), \
		   L3_1_UC), \
	/* No AOM; Age:DC - L3 + LLC */ \
	MOCS_ENTRY(15, \
		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(2) | LE_AOM(1), \
		   L3_3_WB), \
	/* Self-Snoop - L3 + LLC */ \
	MOCS_ENTRY(18, \
		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_SSE(3), \
		   L3_3_WB), \
	/* Skip Caching - L3 + LLC(12.5%) */ \
	MOCS_ENTRY(19, \
		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_SCC(7), \
		   L3_3_WB), \
	/* Skip Caching - L3 + LLC(25%) */ \
	MOCS_ENTRY(20, \
		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_SCC(3), \
		   L3_3_WB), \
	/* Skip Caching - L3 + LLC(50%) */ \
	MOCS_ENTRY(21, \
		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_SCC(1), \
		   L3_3_WB), \
	/* Skip Caching - L3 + LLC(75%) */ \
	MOCS_ENTRY(22, \
		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_RSC(1) | LE_SCC(3), \
		   L3_3_WB), \
	/* Skip Caching - L3 + LLC(87.5%) */ \
	MOCS_ENTRY(23, \
		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_RSC(1) | LE_SCC(7), \
		   L3_3_WB), \
	/* HW Reserved - SW program but never use */ \
	MOCS_ENTRY(62, \
		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3), \
		   L3_1_UC), \
	/* HW Reserved - SW program but never use */ \
	MOCS_ENTRY(63, \
		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3), \
		   L3_1_UC)

241
static const struct drm_i915_mocs_entry tgl_mocs_table[] = {
242 243 244 245 246 247 248 249 250
	/*
	 * NOTE:
	 * Reserved and unspecified MOCS indices have been set to (L3 + LCC).
	 * These reserved entries should never be used, they may be changed
	 * to low performant variants with better coherency in the future if
	 * more entries are needed. We are programming index I915_MOCS_PTE(1)
	 * only, __init_mocs_table() take care to program unused index with
	 * this entry.
	 */
251 252 253
	MOCS_ENTRY(I915_MOCS_PTE,
		   LE_0_PAGETABLE | LE_TC_0_PAGETABLE,
		   L3_1_UC),
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
	GEN11_MOCS_ENTRIES,

	/* Implicitly enable L1 - HDC:L1 + L3 + LLC */
	MOCS_ENTRY(48,
		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3),
		   L3_3_WB),
	/* Implicitly enable L1 - HDC:L1 + L3 */
	MOCS_ENTRY(49,
		   LE_1_UC | LE_TC_1_LLC,
		   L3_3_WB),
	/* Implicitly enable L1 - HDC:L1 + LLC */
	MOCS_ENTRY(50,
		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3),
		   L3_1_UC),
	/* Implicitly enable L1 - HDC:L1 */
	MOCS_ENTRY(51,
		   LE_1_UC | LE_TC_1_LLC,
		   L3_1_UC),
	/* HW Special Case (CCS) */
	MOCS_ENTRY(60,
		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3),
		   L3_1_UC),
	/* HW Special Case (Displayable) */
	MOCS_ENTRY(61,
M
Matt Roper 已提交
278
		   LE_1_UC | LE_TC_1_LLC,
279 280 281
		   L3_3_WB),
};

282
static const struct drm_i915_mocs_entry icl_mocs_table[] = {
283 284 285 286 287 288
	/* Base - Uncached (Deprecated) */
	MOCS_ENTRY(I915_MOCS_UNCACHED,
		   LE_1_UC | LE_TC_1_LLC,
		   L3_1_UC),
	/* Base - L3 + LeCC:PAT (Deprecated) */
	MOCS_ENTRY(I915_MOCS_PTE,
289
		   LE_0_PAGETABLE | LE_TC_0_PAGETABLE,
290 291
		   L3_3_WB),

292 293 294
	GEN11_MOCS_ENTRIES
};

295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
static const struct drm_i915_mocs_entry dg1_mocs_table[] = {

	/* UC */
	MOCS_ENTRY(1, 0, L3_1_UC),
	/* WB - L3 */
	MOCS_ENTRY(5, 0, L3_3_WB),
	/* WB - L3 50% */
	MOCS_ENTRY(6, 0, L3_ESC(1) | L3_SCC(1) | L3_3_WB),
	/* WB - L3 25% */
	MOCS_ENTRY(7, 0, L3_ESC(1) | L3_SCC(3) | L3_3_WB),
	/* WB - L3 12.5% */
	MOCS_ENTRY(8, 0, L3_ESC(1) | L3_SCC(7) | L3_3_WB),

	/* HDC:L1 + L3 */
	MOCS_ENTRY(48, 0, L3_3_WB),
	/* HDC:L1 */
	MOCS_ENTRY(49, 0, L3_1_UC),

	/* HW Reserved */
	MOCS_ENTRY(60, 0, L3_1_UC),
	MOCS_ENTRY(61, 0, L3_1_UC),
	MOCS_ENTRY(62, 0, L3_1_UC),
	MOCS_ENTRY(63, 0, L3_1_UC),
};

320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
static const struct drm_i915_mocs_entry gen12_mocs_table[] = {
	GEN11_MOCS_ENTRIES,
	/* Implicitly enable L1 - HDC:L1 + L3 + LLC */
	MOCS_ENTRY(48,
		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3),
		   L3_3_WB),
	/* Implicitly enable L1 - HDC:L1 + L3 */
	MOCS_ENTRY(49,
		   LE_1_UC | LE_TC_1_LLC,
		   L3_3_WB),
	/* Implicitly enable L1 - HDC:L1 + LLC */
	MOCS_ENTRY(50,
		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3),
		   L3_1_UC),
	/* Implicitly enable L1 - HDC:L1 */
	MOCS_ENTRY(51,
		   LE_1_UC | LE_TC_1_LLC,
		   L3_1_UC),
	/* HW Special Case (CCS) */
	MOCS_ENTRY(60,
		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3),
		   L3_1_UC),
	/* HW Special Case (Displayable) */
	MOCS_ENTRY(61,
		   LE_1_UC | LE_TC_1_LLC,
		   L3_3_WB),
};

348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
static const struct drm_i915_mocs_entry xehpsdv_mocs_table[] = {
	/* wa_1608975824 */
	MOCS_ENTRY(0, 0, L3_3_WB | L3_LKUP(1)),

	/* UC - Coherent; GO:L3 */
	MOCS_ENTRY(1, 0, L3_1_UC | L3_LKUP(1)),
	/* UC - Coherent; GO:Memory */
	MOCS_ENTRY(2, 0, L3_1_UC | L3_GLBGO(1) | L3_LKUP(1)),
	/* UC - Non-Coherent; GO:Memory */
	MOCS_ENTRY(3, 0, L3_1_UC | L3_GLBGO(1)),
	/* UC - Non-Coherent; GO:L3 */
	MOCS_ENTRY(4, 0, L3_1_UC),

	/* WB */
	MOCS_ENTRY(5, 0, L3_3_WB | L3_LKUP(1)),

	/* HW Reserved - SW program but never use. */
	MOCS_ENTRY(48, 0, L3_3_WB | L3_LKUP(1)),
	MOCS_ENTRY(49, 0, L3_1_UC | L3_LKUP(1)),
	MOCS_ENTRY(60, 0, L3_1_UC),
	MOCS_ENTRY(61, 0, L3_1_UC),
	MOCS_ENTRY(62, 0, L3_1_UC),
	MOCS_ENTRY(63, 0, L3_1_UC),
};

373 374 375 376 377 378 379
enum {
	HAS_GLOBAL_MOCS = BIT(0),
	HAS_ENGINE_MOCS = BIT(1),
	HAS_RENDER_L3CC = BIT(2),
};

static bool has_l3cc(const struct drm_i915_private *i915)
380
{
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
	return true;
}

static bool has_global_mocs(const struct drm_i915_private *i915)
{
	return HAS_GLOBAL_MOCS_REGISTERS(i915);
}

static bool has_mocs(const struct drm_i915_private *i915)
{
	return !IS_DGFX(i915);
}

static unsigned int get_mocs_settings(const struct drm_i915_private *i915,
				      struct drm_i915_mocs_table *table)
{
	unsigned int flags;

399 400
	memset(table, 0, sizeof(struct drm_i915_mocs_table));

401
	table->unused_entries_index = I915_MOCS_PTE;
402 403 404 405 406 407 408
	if (IS_XEHPSDV(i915)) {
		table->size = ARRAY_SIZE(xehpsdv_mocs_table);
		table->table = xehpsdv_mocs_table;
		table->uc_index = 2;
		table->n_entries = GEN9_NUM_MOCS_ENTRIES;
		table->unused_entries_index = 5;
	} else if (IS_DG1(i915)) {
409 410
		table->size = ARRAY_SIZE(dg1_mocs_table);
		table->table = dg1_mocs_table;
411
		table->uc_index = 1;
412
		table->n_entries = GEN9_NUM_MOCS_ENTRIES;
413 414
		table->uc_index = 1;
		table->unused_entries_index = 5;
415 416
	} else if (IS_TIGERLAKE(i915) || IS_ROCKETLAKE(i915)) {
		/* For TGL/RKL, Can't be changed now for ABI reasons */
417 418
		table->size  = ARRAY_SIZE(tgl_mocs_table);
		table->table = tgl_mocs_table;
419
		table->n_entries = GEN9_NUM_MOCS_ENTRIES;
420
		table->uc_index = 3;
421 422 423 424 425 426
	} else if (GRAPHICS_VER(i915) >= 12) {
		table->size  = ARRAY_SIZE(gen12_mocs_table);
		table->table = gen12_mocs_table;
		table->n_entries = GEN9_NUM_MOCS_ENTRIES;
		table->uc_index = 3;
		table->unused_entries_index = 2;
427
	} else if (GRAPHICS_VER(i915) == 11) {
428 429
		table->size  = ARRAY_SIZE(icl_mocs_table);
		table->table = icl_mocs_table;
430
		table->n_entries = GEN9_NUM_MOCS_ENTRIES;
431
	} else if (IS_GEN9_BC(i915)) {
432
		table->size  = ARRAY_SIZE(skl_mocs_table);
433
		table->n_entries = GEN9_NUM_MOCS_ENTRIES;
434
		table->table = skl_mocs_table;
435
	} else if (IS_GEN9_LP(i915)) {
436
		table->size  = ARRAY_SIZE(broxton_mocs_table);
437
		table->n_entries = GEN9_NUM_MOCS_ENTRIES;
438 439
		table->table = broxton_mocs_table;
	} else {
440
		drm_WARN_ONCE(&i915->drm, GRAPHICS_VER(i915) >= 9,
441
			      "Platform that should have a MOCS table does not.\n");
442
		return 0;
443 444
	}

445
	if (GEM_DEBUG_WARN_ON(table->size > table->n_entries))
446
		return 0;
447

448
	/* WaDisableSkipCaching:skl,bxt,kbl,glk */
449
	if (GRAPHICS_VER(i915) == 9) {
450 451 452
		int i;

		for (i = 0; i < table->size; i++)
453 454
			if (GEM_DEBUG_WARN_ON(table->table[i].l3cc_value &
					      (L3_ESC(1) | L3_SCC(0x7))))
455
				return 0;
456 457
	}

458 459 460 461 462 463 464 465 466 467 468
	flags = 0;
	if (has_mocs(i915)) {
		if (has_global_mocs(i915))
			flags |= HAS_GLOBAL_MOCS;
		else
			flags |= HAS_ENGINE_MOCS;
	}
	if (has_l3cc(i915))
		flags |= HAS_RENDER_L3CC;

	return flags;
469 470
}

471
/*
472 473 474
 * Get control_value from MOCS entry taking into account when it's not used
 * then if unused_entries_index is non-zero then its value will be returned
 * otherwise I915_MOCS_PTE's value is returned in this case.
475 476 477 478
 */
static u32 get_entry_control(const struct drm_i915_mocs_table *table,
			     unsigned int index)
{
479
	if (index < table->size && table->table[index].used)
480
		return table->table[index].control_value;
481
	return table->table[table->unused_entries_index].control_value;
482 483
}

484 485 486 487 488 489 490 491
#define for_each_mocs(mocs, t, i) \
	for (i = 0; \
	     i < (t)->n_entries ? (mocs = get_entry_control((t), i)), 1 : 0;\
	     i++)

static void __init_mocs_table(struct intel_uncore *uncore,
			      const struct drm_i915_mocs_table *table,
			      u32 addr)
492
{
493
	unsigned int i;
494
	u32 mocs;
495

496 497
	drm_WARN_ONCE(&uncore->i915->drm, !table->unused_entries_index,
		      "Unused entries index should have been defined\n");
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
	for_each_mocs(mocs, table, i)
		intel_uncore_write_fw(uncore, _MMIO(addr + i * 4), mocs);
}

static u32 mocs_offset(const struct intel_engine_cs *engine)
{
	static const u32 offset[] = {
		[RCS0]  =  __GEN9_RCS0_MOCS0,
		[VCS0]  =  __GEN9_VCS0_MOCS0,
		[VCS1]  =  __GEN9_VCS1_MOCS0,
		[VECS0] =  __GEN9_VECS0_MOCS0,
		[BCS0]  =  __GEN9_BCS0_MOCS0,
		[VCS2]  = __GEN11_VCS2_MOCS0,
	};

	GEM_BUG_ON(engine->id >= ARRAY_SIZE(offset));
	return offset[engine->id];
}
516

517 518 519 520
static void init_mocs_table(struct intel_engine_cs *engine,
			    const struct drm_i915_mocs_table *table)
{
	__init_mocs_table(engine->uncore, table, mocs_offset(engine));
521 522
}

523
/*
524 525 526
 * Get l3cc_value from MOCS entry taking into account when it's not used
 * then if unused_entries_index is not zero then its value will be returned
 * otherwise I915_MOCS_PTE's value is returned in this case.
527 528 529 530
 */
static u16 get_entry_l3cc(const struct drm_i915_mocs_table *table,
			  unsigned int index)
{
531
	if (index < table->size && table->table[index].used)
532
		return table->table[index].l3cc_value;
533
	return table->table[table->unused_entries_index].l3cc_value;
534 535
}

C
Chris Wilson 已提交
536
static u32 l3cc_combine(u16 low, u16 high)
537
{
538
	return low | (u32)high << 16;
539 540
}

541 542 543 544 545 546 547 548
#define for_each_l3cc(l3cc, t, i) \
	for (i = 0; \
	     i < ((t)->n_entries + 1) / 2 ? \
	     (l3cc = l3cc_combine(get_entry_l3cc((t), 2 * i), \
				  get_entry_l3cc((t), 2 * i + 1))), 1 : \
	     0; \
	     i++)

549
static void init_l3cc_table(struct intel_uncore *uncore,
550
			    const struct drm_i915_mocs_table *table)
551 552
{
	unsigned int i;
553
	u32 l3cc;
554

555 556
	for_each_l3cc(l3cc, table, i)
		intel_uncore_write_fw(uncore, GEN9_LNCFCMOCS(i), l3cc);
557 558
}

559
void intel_mocs_init_engine(struct intel_engine_cs *engine)
560 561
{
	struct drm_i915_mocs_table table;
562
	unsigned int flags;
563

564 565 566
	/* Called under a blanket forcewake */
	assert_forcewakes_active(engine->uncore, FORCEWAKE_ALL);

567 568
	flags = get_mocs_settings(engine->i915, &table);
	if (!flags)
569 570
		return;

571
	/* Platforms with global MOCS do not need per-engine initialization. */
572
	if (flags & HAS_ENGINE_MOCS)
573
		init_mocs_table(engine, &table);
574

575
	if (flags & HAS_RENDER_L3CC && engine->class == RENDER_CLASS)
576
		init_l3cc_table(engine->uncore, &table);
577
}
578

579 580 581 582 583
static u32 global_mocs_offset(void)
{
	return i915_mmio_reg_offset(GEN12_GLOBAL_MOCS(0));
}

584 585 586 587 588 589 590 591
void set_mocs_index(struct intel_gt *gt)
{
	struct drm_i915_mocs_table table;

	get_mocs_settings(gt->i915, &table);
	gt->mocs.uc_index = table.uc_index;
}

592
void intel_mocs_init(struct intel_gt *gt)
593 594
{
	struct drm_i915_mocs_table table;
595
	unsigned int flags;
596

597 598 599
	/*
	 * LLC and eDRAM control values are not applicable to dgfx
	 */
600 601 602
	flags = get_mocs_settings(gt->i915, &table);
	if (flags & HAS_GLOBAL_MOCS)
		__init_mocs_table(gt->uncore, &table, global_mocs_offset());
603 604 605 606 607 608 609 610

	/*
	 * Initialize the L3CC table as part of mocs initalization to make
	 * sure the LNCFCMOCSx registers are programmed for the subsequent
	 * memory transactions including guc transactions
	 */
	if (flags & HAS_RENDER_L3CC)
		init_l3cc_table(gt->uncore, &table);
611
}
612 613 614 615

#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
#include "selftest_mocs.c"
#endif