1 // SPDX-License-Identifier: MIT
3 * Copyright © 2015 Intel Corporation
8 #include "intel_engine.h"
10 #include "intel_gt_regs.h"
11 #include "intel_mocs.h"
12 #include "intel_ring.h"
14 /* structures required */
15 struct drm_i915_mocs_entry {
21 struct drm_i915_mocs_table {
23 unsigned int n_entries;
24 const struct drm_i915_mocs_entry *table;
26 u8 wb_index; /* Only used on HAS_L3_CCS_READ() platforms */
27 u8 unused_entries_index;
30 /* Defines for the tables (XXX_MOCS_0 - XXX_MOCS_63) */
31 #define _LE_CACHEABILITY(value) ((value) << 0)
32 #define _LE_TGT_CACHE(value) ((value) << 2)
33 #define LE_LRUM(value) ((value) << 4)
34 #define LE_AOM(value) ((value) << 6)
35 #define LE_RSC(value) ((value) << 7)
36 #define LE_SCC(value) ((value) << 8)
37 #define LE_PFM(value) ((value) << 11)
38 #define LE_SCF(value) ((value) << 14)
39 #define LE_COS(value) ((value) << 15)
40 #define LE_SSE(value) ((value) << 17)
42 /* Defines for the tables (LNCFMOCS0 - LNCFMOCS31) - two entries per word */
43 #define L3_ESC(value) ((value) << 0)
44 #define L3_SCC(value) ((value) << 1)
45 #define _L3_CACHEABILITY(value) ((value) << 4)
46 #define L3_GLBGO(value) ((value) << 6)
47 #define L3_LKUP(value) ((value) << 7)
50 #define GEN9_NUM_MOCS_ENTRIES 64 /* 63-64 are reserved, but configured. */
51 #define PVC_NUM_MOCS_ENTRIES 3
53 /* (e)LLC caching options */
55 * Note: LE_0_PAGETABLE works only up to Gen11; for newer gens it means
58 #define LE_0_PAGETABLE _LE_CACHEABILITY(0)
59 #define LE_1_UC _LE_CACHEABILITY(1)
60 #define LE_2_WT _LE_CACHEABILITY(2)
61 #define LE_3_WB _LE_CACHEABILITY(3)
64 #define LE_TC_0_PAGETABLE _LE_TGT_CACHE(0)
65 #define LE_TC_1_LLC _LE_TGT_CACHE(1)
66 #define LE_TC_2_LLC_ELLC _LE_TGT_CACHE(2)
67 #define LE_TC_3_LLC_ELLC_ALT _LE_TGT_CACHE(3)
69 /* L3 caching options */
70 #define L3_0_DIRECT _L3_CACHEABILITY(0)
71 #define L3_1_UC _L3_CACHEABILITY(1)
72 #define L3_2_RESERVED _L3_CACHEABILITY(2)
73 #define L3_3_WB _L3_CACHEABILITY(3)
75 #define MOCS_ENTRY(__idx, __control_value, __l3cc_value) \
77 .control_value = __control_value, \
78 .l3cc_value = __l3cc_value, \
85 * These are the MOCS tables that are programmed across all the rings.
86 * The control value is programmed to all the rings that support the
87 * MOCS registers. While the l3cc_values are only programmed to the
88 * LNCFCMOCS0 - LNCFCMOCS32 registers.
90 * These tables are intended to be kept reasonably consistent across
91 * HW platforms, and for ICL+, be identical across OSes. To achieve
92 * that, for Icelake and above, list of entries is published as part
95 * Entries not part of the following tables are undefined as far as
96 * userspace is concerned and shouldn't be relied upon. For Gen < 12
97 * they will be initialized to PTE. Gen >= 12 don't have a setting for
98 * PTE and those platforms except TGL/RKL will be initialized L3 WB to
99 * catch accidental use of reserved and unused mocs indexes.
101 * The last few entries are reserved by the hardware. For ICL+ they
102 * should be initialized according to bspec and never used, for older
103 * platforms they should never be written to.
105 * NOTE1: These tables are part of bspec and defined as part of hardware
106 * interface for ICL+. For older platforms, they are part of kernel
107 * ABI. It is expected that, for specific hardware platform, existing
108 * entries will remain constant and the table will only be updated by
109 * adding new entries, filling unused positions.
111 * NOTE2: For GEN >= 12 except TGL and RKL, reserved and unspecified MOCS
112 * indices have been set to L3 WB. These reserved entries should never
113 * be used, they may be changed to low performant variants with better
114 * coherency in the future if more entries are needed.
115 * For TGL/RKL, all the unspecified MOCS indexes are mapped to L3 UC.
117 #define GEN9_MOCS_ENTRIES \
118 MOCS_ENTRY(I915_MOCS_UNCACHED, \
119 LE_1_UC | LE_TC_2_LLC_ELLC, \
121 MOCS_ENTRY(I915_MOCS_PTE, \
122 LE_0_PAGETABLE | LE_TC_0_PAGETABLE | LE_LRUM(3), \
125 static const struct drm_i915_mocs_entry skl_mocs_table[] = {
127 MOCS_ENTRY(I915_MOCS_CACHED,
128 LE_3_WB | LE_TC_2_LLC_ELLC | LE_LRUM(3),
133 * - used by the L3 for all of its evictions.
134 * Thus it is expected to allow LLC cacheability to enable coherent
135 * flows to be maintained.
136 * - used to force L3 uncachable cycles.
137 * Thus it is expected to make the surface L3 uncacheable.
140 LE_3_WB | LE_TC_1_LLC | LE_LRUM(3),
144 /* NOTE: the LE_TGT_CACHE is not used on Broxton */
145 static const struct drm_i915_mocs_entry broxton_mocs_table[] = {
147 MOCS_ENTRY(I915_MOCS_CACHED,
148 LE_1_UC | LE_TC_2_LLC_ELLC | LE_LRUM(3),
152 #define GEN11_MOCS_ENTRIES \
153 /* Entries 0 and 1 are defined per-platform */ \
154 /* Base - L3 + LLC */ \
156 LE_3_WB | LE_TC_1_LLC | LE_LRUM(3), \
158 /* Base - Uncached */ \
160 LE_1_UC | LE_TC_1_LLC, \
164 LE_1_UC | LE_TC_1_LLC, \
168 LE_3_WB | LE_TC_1_LLC | LE_LRUM(3), \
172 LE_3_WB | LE_TC_1_LLC | LE_LRUM(1), \
174 /* Age 0 - L3 + LLC */ \
176 LE_3_WB | LE_TC_1_LLC | LE_LRUM(1), \
178 /* Age: Don't Chg. - LLC */ \
180 LE_3_WB | LE_TC_1_LLC | LE_LRUM(2), \
182 /* Age: Don't Chg. - L3 + LLC */ \
184 LE_3_WB | LE_TC_1_LLC | LE_LRUM(2), \
188 LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_AOM(1), \
190 /* No AOM - L3 + LLC */ \
192 LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_AOM(1), \
194 /* No AOM; Age 0 - LLC */ \
196 LE_3_WB | LE_TC_1_LLC | LE_LRUM(1) | LE_AOM(1), \
198 /* No AOM; Age 0 - L3 + LLC */ \
200 LE_3_WB | LE_TC_1_LLC | LE_LRUM(1) | LE_AOM(1), \
202 /* No AOM; Age:DC - LLC */ \
204 LE_3_WB | LE_TC_1_LLC | LE_LRUM(2) | LE_AOM(1), \
206 /* No AOM; Age:DC - L3 + LLC */ \
208 LE_3_WB | LE_TC_1_LLC | LE_LRUM(2) | LE_AOM(1), \
210 /* Self-Snoop - L3 + LLC */ \
212 LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_SSE(3), \
214 /* Skip Caching - L3 + LLC(12.5%) */ \
216 LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_SCC(7), \
218 /* Skip Caching - L3 + LLC(25%) */ \
220 LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_SCC(3), \
222 /* Skip Caching - L3 + LLC(50%) */ \
224 LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_SCC(1), \
226 /* Skip Caching - L3 + LLC(75%) */ \
228 LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_RSC(1) | LE_SCC(3), \
230 /* Skip Caching - L3 + LLC(87.5%) */ \
232 LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_RSC(1) | LE_SCC(7), \
234 /* HW Reserved - SW program but never use */ \
236 LE_3_WB | LE_TC_1_LLC | LE_LRUM(3), \
238 /* HW Reserved - SW program but never use */ \
240 LE_3_WB | LE_TC_1_LLC | LE_LRUM(3), \
243 static const struct drm_i915_mocs_entry tgl_mocs_table[] = {
246 * Reserved and unspecified MOCS indices have been set to (L3 + LCC).
247 * These reserved entries should never be used, they may be changed
248 * to low performant variants with better coherency in the future if
249 * more entries are needed. We are programming index I915_MOCS_PTE(1)
250 * only, __init_mocs_table() take care to program unused index with
253 MOCS_ENTRY(I915_MOCS_PTE,
254 LE_0_PAGETABLE | LE_TC_0_PAGETABLE,
258 /* Implicitly enable L1 - HDC:L1 + L3 + LLC */
260 LE_3_WB | LE_TC_1_LLC | LE_LRUM(3),
262 /* Implicitly enable L1 - HDC:L1 + L3 */
264 LE_1_UC | LE_TC_1_LLC,
266 /* Implicitly enable L1 - HDC:L1 + LLC */
268 LE_3_WB | LE_TC_1_LLC | LE_LRUM(3),
270 /* Implicitly enable L1 - HDC:L1 */
272 LE_1_UC | LE_TC_1_LLC,
274 /* HW Special Case (CCS) */
276 LE_3_WB | LE_TC_1_LLC | LE_LRUM(3),
278 /* HW Special Case (Displayable) */
280 LE_1_UC | LE_TC_1_LLC,
284 static const struct drm_i915_mocs_entry icl_mocs_table[] = {
285 /* Base - Uncached (Deprecated) */
286 MOCS_ENTRY(I915_MOCS_UNCACHED,
287 LE_1_UC | LE_TC_1_LLC,
289 /* Base - L3 + LeCC:PAT (Deprecated) */
290 MOCS_ENTRY(I915_MOCS_PTE,
291 LE_0_PAGETABLE | LE_TC_0_PAGETABLE,
297 static const struct drm_i915_mocs_entry dg1_mocs_table[] = {
300 MOCS_ENTRY(1, 0, L3_1_UC),
302 MOCS_ENTRY(5, 0, L3_3_WB),
304 MOCS_ENTRY(6, 0, L3_ESC(1) | L3_SCC(1) | L3_3_WB),
306 MOCS_ENTRY(7, 0, L3_ESC(1) | L3_SCC(3) | L3_3_WB),
308 MOCS_ENTRY(8, 0, L3_ESC(1) | L3_SCC(7) | L3_3_WB),
311 MOCS_ENTRY(48, 0, L3_3_WB),
313 MOCS_ENTRY(49, 0, L3_1_UC),
316 MOCS_ENTRY(60, 0, L3_1_UC),
317 MOCS_ENTRY(61, 0, L3_1_UC),
318 MOCS_ENTRY(62, 0, L3_1_UC),
319 MOCS_ENTRY(63, 0, L3_1_UC),
322 static const struct drm_i915_mocs_entry gen12_mocs_table[] = {
324 /* Implicitly enable L1 - HDC:L1 + L3 + LLC */
326 LE_3_WB | LE_TC_1_LLC | LE_LRUM(3),
328 /* Implicitly enable L1 - HDC:L1 + L3 */
330 LE_1_UC | LE_TC_1_LLC,
332 /* Implicitly enable L1 - HDC:L1 + LLC */
334 LE_3_WB | LE_TC_1_LLC | LE_LRUM(3),
336 /* Implicitly enable L1 - HDC:L1 */
338 LE_1_UC | LE_TC_1_LLC,
340 /* HW Special Case (CCS) */
342 LE_3_WB | LE_TC_1_LLC | LE_LRUM(3),
344 /* HW Special Case (Displayable) */
346 LE_1_UC | LE_TC_1_LLC,
350 static const struct drm_i915_mocs_entry xehpsdv_mocs_table[] = {
352 MOCS_ENTRY(0, 0, L3_3_WB | L3_LKUP(1)),
354 /* UC - Coherent; GO:L3 */
355 MOCS_ENTRY(1, 0, L3_1_UC | L3_LKUP(1)),
356 /* UC - Coherent; GO:Memory */
357 MOCS_ENTRY(2, 0, L3_1_UC | L3_GLBGO(1) | L3_LKUP(1)),
358 /* UC - Non-Coherent; GO:Memory */
359 MOCS_ENTRY(3, 0, L3_1_UC | L3_GLBGO(1)),
360 /* UC - Non-Coherent; GO:L3 */
361 MOCS_ENTRY(4, 0, L3_1_UC),
364 MOCS_ENTRY(5, 0, L3_3_WB | L3_LKUP(1)),
366 /* HW Reserved - SW program but never use. */
367 MOCS_ENTRY(48, 0, L3_3_WB | L3_LKUP(1)),
368 MOCS_ENTRY(49, 0, L3_1_UC | L3_LKUP(1)),
369 MOCS_ENTRY(60, 0, L3_1_UC),
370 MOCS_ENTRY(61, 0, L3_1_UC),
371 MOCS_ENTRY(62, 0, L3_1_UC),
372 MOCS_ENTRY(63, 0, L3_1_UC),
375 static const struct drm_i915_mocs_entry dg2_mocs_table[] = {
376 /* UC - Coherent; GO:L3 */
377 MOCS_ENTRY(0, 0, L3_1_UC | L3_LKUP(1)),
378 /* UC - Coherent; GO:Memory */
379 MOCS_ENTRY(1, 0, L3_1_UC | L3_GLBGO(1) | L3_LKUP(1)),
380 /* UC - Non-Coherent; GO:Memory */
381 MOCS_ENTRY(2, 0, L3_1_UC | L3_GLBGO(1)),
384 MOCS_ENTRY(3, 0, L3_3_WB | L3_LKUP(1)),
387 static const struct drm_i915_mocs_entry dg2_mocs_table_g10_ax[] = {
388 /* Wa_14011441408: Set Go to Memory for MOCS#0 */
389 MOCS_ENTRY(0, 0, L3_1_UC | L3_GLBGO(1) | L3_LKUP(1)),
390 /* UC - Coherent; GO:Memory */
391 MOCS_ENTRY(1, 0, L3_1_UC | L3_GLBGO(1) | L3_LKUP(1)),
392 /* UC - Non-Coherent; GO:Memory */
393 MOCS_ENTRY(2, 0, L3_1_UC | L3_GLBGO(1)),
396 MOCS_ENTRY(3, 0, L3_3_WB | L3_LKUP(1)),
399 static const struct drm_i915_mocs_entry pvc_mocs_table[] = {
401 MOCS_ENTRY(0, 0, L3_3_WB),
404 MOCS_ENTRY(1, 0, L3_1_UC),
407 MOCS_ENTRY(2, 0, L3_3_WB),
411 HAS_GLOBAL_MOCS = BIT(0),
412 HAS_ENGINE_MOCS = BIT(1),
413 HAS_RENDER_L3CC = BIT(2),
416 static bool has_l3cc(const struct drm_i915_private *i915)
421 static bool has_global_mocs(const struct drm_i915_private *i915)
423 return HAS_GLOBAL_MOCS_REGISTERS(i915);
426 static bool has_mocs(const struct drm_i915_private *i915)
428 return !IS_DGFX(i915);
431 static unsigned int get_mocs_settings(const struct drm_i915_private *i915,
432 struct drm_i915_mocs_table *table)
436 memset(table, 0, sizeof(struct drm_i915_mocs_table));
438 table->unused_entries_index = I915_MOCS_PTE;
439 if (IS_PONTEVECCHIO(i915)) {
440 table->size = ARRAY_SIZE(pvc_mocs_table);
441 table->table = pvc_mocs_table;
442 table->n_entries = PVC_NUM_MOCS_ENTRIES;
445 table->unused_entries_index = 2;
446 } else if (IS_DG2(i915)) {
447 if (IS_DG2_GRAPHICS_STEP(i915, G10, STEP_A0, STEP_B0)) {
448 table->size = ARRAY_SIZE(dg2_mocs_table_g10_ax);
449 table->table = dg2_mocs_table_g10_ax;
451 table->size = ARRAY_SIZE(dg2_mocs_table);
452 table->table = dg2_mocs_table;
455 table->n_entries = GEN9_NUM_MOCS_ENTRIES;
456 table->unused_entries_index = 3;
457 } else if (IS_XEHPSDV(i915)) {
458 table->size = ARRAY_SIZE(xehpsdv_mocs_table);
459 table->table = xehpsdv_mocs_table;
461 table->n_entries = GEN9_NUM_MOCS_ENTRIES;
462 table->unused_entries_index = 5;
463 } else if (IS_DG1(i915)) {
464 table->size = ARRAY_SIZE(dg1_mocs_table);
465 table->table = dg1_mocs_table;
467 table->n_entries = GEN9_NUM_MOCS_ENTRIES;
469 table->unused_entries_index = 5;
470 } else if (IS_TIGERLAKE(i915) || IS_ROCKETLAKE(i915)) {
471 /* For TGL/RKL, Can't be changed now for ABI reasons */
472 table->size = ARRAY_SIZE(tgl_mocs_table);
473 table->table = tgl_mocs_table;
474 table->n_entries = GEN9_NUM_MOCS_ENTRIES;
476 } else if (GRAPHICS_VER(i915) >= 12) {
477 table->size = ARRAY_SIZE(gen12_mocs_table);
478 table->table = gen12_mocs_table;
479 table->n_entries = GEN9_NUM_MOCS_ENTRIES;
481 table->unused_entries_index = 2;
482 } else if (GRAPHICS_VER(i915) == 11) {
483 table->size = ARRAY_SIZE(icl_mocs_table);
484 table->table = icl_mocs_table;
485 table->n_entries = GEN9_NUM_MOCS_ENTRIES;
486 } else if (IS_GEN9_BC(i915)) {
487 table->size = ARRAY_SIZE(skl_mocs_table);
488 table->n_entries = GEN9_NUM_MOCS_ENTRIES;
489 table->table = skl_mocs_table;
490 } else if (IS_GEN9_LP(i915)) {
491 table->size = ARRAY_SIZE(broxton_mocs_table);
492 table->n_entries = GEN9_NUM_MOCS_ENTRIES;
493 table->table = broxton_mocs_table;
495 drm_WARN_ONCE(&i915->drm, GRAPHICS_VER(i915) >= 9,
496 "Platform that should have a MOCS table does not.\n");
500 if (GEM_DEBUG_WARN_ON(table->size > table->n_entries))
503 /* WaDisableSkipCaching:skl,bxt,kbl,glk */
504 if (GRAPHICS_VER(i915) == 9) {
507 for (i = 0; i < table->size; i++)
508 if (GEM_DEBUG_WARN_ON(table->table[i].l3cc_value &
509 (L3_ESC(1) | L3_SCC(0x7))))
514 if (has_mocs(i915)) {
515 if (has_global_mocs(i915))
516 flags |= HAS_GLOBAL_MOCS;
518 flags |= HAS_ENGINE_MOCS;
521 flags |= HAS_RENDER_L3CC;
527 * Get control_value from MOCS entry taking into account when it's not used
528 * then if unused_entries_index is non-zero then its value will be returned
529 * otherwise I915_MOCS_PTE's value is returned in this case.
531 static u32 get_entry_control(const struct drm_i915_mocs_table *table,
534 if (index < table->size && table->table[index].used)
535 return table->table[index].control_value;
536 return table->table[table->unused_entries_index].control_value;
539 #define for_each_mocs(mocs, t, i) \
541 i < (t)->n_entries ? (mocs = get_entry_control((t), i)), 1 : 0;\
544 static void __init_mocs_table(struct intel_uncore *uncore,
545 const struct drm_i915_mocs_table *table,
551 drm_WARN_ONCE(&uncore->i915->drm, !table->unused_entries_index,
552 "Unused entries index should have been defined\n");
553 for_each_mocs(mocs, table, i)
554 intel_uncore_write_fw(uncore, _MMIO(addr + i * 4), mocs);
557 static u32 mocs_offset(const struct intel_engine_cs *engine)
559 static const u32 offset[] = {
560 [RCS0] = __GEN9_RCS0_MOCS0,
561 [VCS0] = __GEN9_VCS0_MOCS0,
562 [VCS1] = __GEN9_VCS1_MOCS0,
563 [VECS0] = __GEN9_VECS0_MOCS0,
564 [BCS0] = __GEN9_BCS0_MOCS0,
565 [VCS2] = __GEN11_VCS2_MOCS0,
568 GEM_BUG_ON(engine->id >= ARRAY_SIZE(offset));
569 return offset[engine->id];
572 static void init_mocs_table(struct intel_engine_cs *engine,
573 const struct drm_i915_mocs_table *table)
575 __init_mocs_table(engine->uncore, table, mocs_offset(engine));
579 * Get l3cc_value from MOCS entry taking into account when it's not used
580 * then if unused_entries_index is not zero then its value will be returned
581 * otherwise I915_MOCS_PTE's value is returned in this case.
583 static u16 get_entry_l3cc(const struct drm_i915_mocs_table *table,
586 if (index < table->size && table->table[index].used)
587 return table->table[index].l3cc_value;
588 return table->table[table->unused_entries_index].l3cc_value;
591 static u32 l3cc_combine(u16 low, u16 high)
593 return low | (u32)high << 16;
596 #define for_each_l3cc(l3cc, t, i) \
598 i < ((t)->n_entries + 1) / 2 ? \
599 (l3cc = l3cc_combine(get_entry_l3cc((t), 2 * i), \
600 get_entry_l3cc((t), 2 * i + 1))), 1 : \
604 static void init_l3cc_table(struct intel_uncore *uncore,
605 const struct drm_i915_mocs_table *table)
610 for_each_l3cc(l3cc, table, i)
611 intel_uncore_write_fw(uncore, GEN9_LNCFCMOCS(i), l3cc);
614 void intel_mocs_init_engine(struct intel_engine_cs *engine)
616 struct drm_i915_mocs_table table;
619 /* Called under a blanket forcewake */
620 assert_forcewakes_active(engine->uncore, FORCEWAKE_ALL);
622 flags = get_mocs_settings(engine->i915, &table);
626 /* Platforms with global MOCS do not need per-engine initialization. */
627 if (flags & HAS_ENGINE_MOCS)
628 init_mocs_table(engine, &table);
630 if (flags & HAS_RENDER_L3CC && engine->class == RENDER_CLASS)
631 init_l3cc_table(engine->uncore, &table);
634 static u32 global_mocs_offset(void)
636 return i915_mmio_reg_offset(GEN12_GLOBAL_MOCS(0));
639 void intel_set_mocs_index(struct intel_gt *gt)
641 struct drm_i915_mocs_table table;
643 get_mocs_settings(gt->i915, &table);
644 gt->mocs.uc_index = table.uc_index;
645 if (HAS_L3_CCS_READ(gt->i915))
646 gt->mocs.wb_index = table.wb_index;
649 void intel_mocs_init(struct intel_gt *gt)
651 struct drm_i915_mocs_table table;
655 * LLC and eDRAM control values are not applicable to dgfx
657 flags = get_mocs_settings(gt->i915, &table);
658 if (flags & HAS_GLOBAL_MOCS)
659 __init_mocs_table(gt->uncore, &table, global_mocs_offset());
662 * Initialize the L3CC table as part of mocs initalization to make
663 * sure the LNCFCMOCSx registers are programmed for the subsequent
664 * memory transactions including guc transactions
666 if (flags & HAS_RENDER_L3CC)
667 init_l3cc_table(gt->uncore, &table);
670 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
671 #include "selftest_mocs.c"