2 * Copyright (c) 2015 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions: *
10 * The above copyright notice and this permission notice (including the next
11 * paragraph) shall be included in all copies or substantial portions of the
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 #include "intel_engine.h"
26 #include "intel_mocs.h"
27 #include "intel_lrc.h"
29 /* structures required */
30 struct drm_i915_mocs_entry {
36 struct drm_i915_mocs_table {
38 unsigned int n_entries;
39 const struct drm_i915_mocs_entry *table;
42 /* Defines for the tables (XXX_MOCS_0 - XXX_MOCS_63) */
43 #define _LE_CACHEABILITY(value) ((value) << 0)
44 #define _LE_TGT_CACHE(value) ((value) << 2)
45 #define LE_LRUM(value) ((value) << 4)
46 #define LE_AOM(value) ((value) << 6)
47 #define LE_RSC(value) ((value) << 7)
48 #define LE_SCC(value) ((value) << 8)
49 #define LE_PFM(value) ((value) << 11)
50 #define LE_SCF(value) ((value) << 14)
51 #define LE_COS(value) ((value) << 15)
52 #define LE_SSE(value) ((value) << 17)
54 /* Defines for the tables (LNCFMOCS0 - LNCFMOCS31) - two entries per word */
55 #define L3_ESC(value) ((value) << 0)
56 #define L3_SCC(value) ((value) << 1)
57 #define _L3_CACHEABILITY(value) ((value) << 4)
60 #define GEN9_NUM_MOCS_ENTRIES 62 /* 62 out of 64 - 63 & 64 are reserved. */
61 #define GEN11_NUM_MOCS_ENTRIES 64 /* 63-64 are reserved, but configured. */
63 /* (e)LLC caching options */
64 #define LE_0_PAGETABLE _LE_CACHEABILITY(0)
65 #define LE_1_UC _LE_CACHEABILITY(1)
66 #define LE_2_WT _LE_CACHEABILITY(2)
67 #define LE_3_WB _LE_CACHEABILITY(3)
70 #define LE_TC_0_PAGETABLE _LE_TGT_CACHE(0)
71 #define LE_TC_1_LLC _LE_TGT_CACHE(1)
72 #define LE_TC_2_LLC_ELLC _LE_TGT_CACHE(2)
73 #define LE_TC_3_LLC_ELLC_ALT _LE_TGT_CACHE(3)
75 /* L3 caching options */
76 #define L3_0_DIRECT _L3_CACHEABILITY(0)
77 #define L3_1_UC _L3_CACHEABILITY(1)
78 #define L3_2_RESERVED _L3_CACHEABILITY(2)
79 #define L3_3_WB _L3_CACHEABILITY(3)
81 #define MOCS_ENTRY(__idx, __control_value, __l3cc_value) \
83 .control_value = __control_value, \
84 .l3cc_value = __l3cc_value, \
91 * These are the MOCS tables that are programmed across all the rings.
92 * The control value is programmed to all the rings that support the
93 * MOCS registers. While the l3cc_values are only programmed to the
94 * LNCFCMOCS0 - LNCFCMOCS32 registers.
96 * These tables are intended to be kept reasonably consistent across
97 * HW platforms, and for ICL+, be identical across OSes. To achieve
98 * that, for Icelake and above, list of entries is published as part
101 * Entries not part of the following tables are undefined as far as
102 * userspace is concerned and shouldn't be relied upon. For the time
103 * being they will be initialized to PTE.
105 * The last two entries are reserved by the hardware. For ICL+ they
106 * should be initialized according to bspec and never used, for older
107 * platforms they should never be written to.
109 * NOTE: These tables are part of bspec and defined as part of hardware
110 * interface for ICL+. For older platforms, they are part of kernel
111 * ABI. It is expected that, for specific hardware platform, existing
112 * entries will remain constant and the table will only be updated by
113 * adding new entries, filling unused positions.
115 #define GEN9_MOCS_ENTRIES \
116 MOCS_ENTRY(I915_MOCS_UNCACHED, \
117 LE_1_UC | LE_TC_2_LLC_ELLC, \
119 MOCS_ENTRY(I915_MOCS_PTE, \
120 LE_0_PAGETABLE | LE_TC_2_LLC_ELLC | LE_LRUM(3), \
123 static const struct drm_i915_mocs_entry skylake_mocs_table[] = {
125 MOCS_ENTRY(I915_MOCS_CACHED,
126 LE_3_WB | LE_TC_2_LLC_ELLC | LE_LRUM(3),
130 /* NOTE: the LE_TGT_CACHE is not used on Broxton */
131 static const struct drm_i915_mocs_entry broxton_mocs_table[] = {
133 MOCS_ENTRY(I915_MOCS_CACHED,
134 LE_1_UC | LE_TC_2_LLC_ELLC | LE_LRUM(3),
138 #define GEN11_MOCS_ENTRIES \
139 /* Base - Uncached (Deprecated) */ \
140 MOCS_ENTRY(I915_MOCS_UNCACHED, \
141 LE_1_UC | LE_TC_1_LLC, \
143 /* Base - L3 + LeCC:PAT (Deprecated) */ \
144 MOCS_ENTRY(I915_MOCS_PTE, \
145 LE_0_PAGETABLE | LE_TC_1_LLC, \
147 /* Base - L3 + LLC */ \
149 LE_3_WB | LE_TC_1_LLC | LE_LRUM(3), \
151 /* Base - Uncached */ \
153 LE_1_UC | LE_TC_1_LLC, \
157 LE_1_UC | LE_TC_1_LLC, \
161 LE_3_WB | LE_TC_1_LLC | LE_LRUM(3), \
165 LE_3_WB | LE_TC_1_LLC | LE_LRUM(1), \
167 /* Age 0 - L3 + LLC */ \
169 LE_3_WB | LE_TC_1_LLC | LE_LRUM(1), \
171 /* Age: Don't Chg. - LLC */ \
173 LE_3_WB | LE_TC_1_LLC | LE_LRUM(2), \
175 /* Age: Don't Chg. - L3 + LLC */ \
177 LE_3_WB | LE_TC_1_LLC | LE_LRUM(2), \
181 LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_AOM(1), \
183 /* No AOM - L3 + LLC */ \
185 LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_AOM(1), \
187 /* No AOM; Age 0 - LLC */ \
189 LE_3_WB | LE_TC_1_LLC | LE_LRUM(1) | LE_AOM(1), \
191 /* No AOM; Age 0 - L3 + LLC */ \
193 LE_3_WB | LE_TC_1_LLC | LE_LRUM(1) | LE_AOM(1), \
195 /* No AOM; Age:DC - LLC */ \
197 LE_3_WB | LE_TC_1_LLC | LE_LRUM(2) | LE_AOM(1), \
199 /* No AOM; Age:DC - L3 + LLC */ \
201 LE_3_WB | LE_TC_1_LLC | LE_LRUM(2) | LE_AOM(1), \
203 /* Bypass LLC - Uncached (EHL+) */ \
205 LE_1_UC | LE_TC_1_LLC | LE_SCF(1), \
207 /* Bypass LLC - L3 (Read-Only) (EHL+) */ \
209 LE_1_UC | LE_TC_1_LLC | LE_SCF(1), \
211 /* Self-Snoop - L3 + LLC */ \
213 LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_SSE(3), \
215 /* Skip Caching - L3 + LLC(12.5%) */ \
217 LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_SCC(7), \
219 /* Skip Caching - L3 + LLC(25%) */ \
221 LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_SCC(3), \
223 /* Skip Caching - L3 + LLC(50%) */ \
225 LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_SCC(1), \
227 /* Skip Caching - L3 + LLC(75%) */ \
229 LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_RSC(1) | LE_SCC(3), \
231 /* Skip Caching - L3 + LLC(87.5%) */ \
233 LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_RSC(1) | LE_SCC(7), \
235 /* HW Reserved - SW program but never use */ \
237 LE_3_WB | LE_TC_1_LLC | LE_LRUM(3), \
239 /* HW Reserved - SW program but never use */ \
241 LE_3_WB | LE_TC_1_LLC | LE_LRUM(3), \
244 static const struct drm_i915_mocs_entry icelake_mocs_table[] = {
249 * get_mocs_settings()
250 * @dev_priv: i915 device.
251 * @table: Output table that will be made to point at appropriate
252 * MOCS values for the device.
254 * This function will return the values of the MOCS table that needs to
255 * be programmed for the platform. It will return the values that need
256 * to be programmed and if they need to be programmed.
258 * Return: true if there are applicable MOCS settings for the device.
260 static bool get_mocs_settings(struct drm_i915_private *dev_priv,
261 struct drm_i915_mocs_table *table)
265 if (INTEL_GEN(dev_priv) >= 11) {
266 table->size = ARRAY_SIZE(icelake_mocs_table);
267 table->table = icelake_mocs_table;
268 table->n_entries = GEN11_NUM_MOCS_ENTRIES;
270 } else if (IS_GEN9_BC(dev_priv) || IS_CANNONLAKE(dev_priv)) {
271 table->size = ARRAY_SIZE(skylake_mocs_table);
272 table->n_entries = GEN9_NUM_MOCS_ENTRIES;
273 table->table = skylake_mocs_table;
275 } else if (IS_GEN9_LP(dev_priv)) {
276 table->size = ARRAY_SIZE(broxton_mocs_table);
277 table->n_entries = GEN9_NUM_MOCS_ENTRIES;
278 table->table = broxton_mocs_table;
281 WARN_ONCE(INTEL_GEN(dev_priv) >= 9,
282 "Platform that should have a MOCS table does not.\n");
285 /* WaDisableSkipCaching:skl,bxt,kbl,glk */
286 if (IS_GEN(dev_priv, 9)) {
289 for (i = 0; i < table->size; i++)
290 if (WARN_ON(table->table[i].l3cc_value &
291 (L3_ESC(1) | L3_SCC(0x7))))
298 static i915_reg_t mocs_register(enum intel_engine_id engine_id, int index)
302 return GEN9_GFX_MOCS(index);
304 return GEN9_MFX0_MOCS(index);
306 return GEN9_BLT_MOCS(index);
308 return GEN9_VEBOX_MOCS(index);
310 return GEN9_MFX1_MOCS(index);
312 return GEN11_MFX2_MOCS(index);
314 MISSING_CASE(engine_id);
315 return INVALID_MMIO_REG;
320 * Get control_value from MOCS entry taking into account when it's not used:
321 * I915_MOCS_PTE's value is returned in this case.
323 static u32 get_entry_control(const struct drm_i915_mocs_table *table,
326 if (table->table[index].used)
327 return table->table[index].control_value;
329 return table->table[I915_MOCS_PTE].control_value;
333 * intel_mocs_init_engine() - emit the mocs control table
334 * @engine: The engine for whom to emit the registers.
336 * This function simply emits a MI_LOAD_REGISTER_IMM command for the
337 * given table starting at the given address.
339 void intel_mocs_init_engine(struct intel_engine_cs *engine)
341 struct drm_i915_private *dev_priv = engine->i915;
342 struct drm_i915_mocs_table table;
346 if (!get_mocs_settings(dev_priv, &table))
349 /* Set unused values to PTE */
350 unused_value = table.table[I915_MOCS_PTE].control_value;
352 for (index = 0; index < table.size; index++) {
353 u32 value = get_entry_control(&table, index);
355 I915_WRITE(mocs_register(engine->id, index), value);
358 /* All remaining entries are also unused */
359 for (; index < table.n_entries; index++)
360 I915_WRITE(mocs_register(engine->id, index), unused_value);
364 * emit_mocs_control_table() - emit the mocs control table
365 * @rq: Request to set up the MOCS table for.
366 * @table: The values to program into the control regs.
368 * This function simply emits a MI_LOAD_REGISTER_IMM command for the
369 * given table starting at the given address.
371 * Return: 0 on success, otherwise the error status.
373 static int emit_mocs_control_table(struct i915_request *rq,
374 const struct drm_i915_mocs_table *table)
376 enum intel_engine_id engine = rq->engine->id;
381 if (GEM_WARN_ON(table->size > table->n_entries))
384 /* Set unused values to PTE */
385 unused_value = table->table[I915_MOCS_PTE].control_value;
387 cs = intel_ring_begin(rq, 2 + 2 * table->n_entries);
391 *cs++ = MI_LOAD_REGISTER_IMM(table->n_entries);
393 for (index = 0; index < table->size; index++) {
394 u32 value = get_entry_control(table, index);
396 *cs++ = i915_mmio_reg_offset(mocs_register(engine, index));
400 /* All remaining entries are also unused */
401 for (; index < table->n_entries; index++) {
402 *cs++ = i915_mmio_reg_offset(mocs_register(engine, index));
403 *cs++ = unused_value;
407 intel_ring_advance(rq, cs);
413 * Get l3cc_value from MOCS entry taking into account when it's not used:
414 * I915_MOCS_PTE's value is returned in this case.
416 static u16 get_entry_l3cc(const struct drm_i915_mocs_table *table,
419 if (table->table[index].used)
420 return table->table[index].l3cc_value;
422 return table->table[I915_MOCS_PTE].l3cc_value;
425 static inline u32 l3cc_combine(const struct drm_i915_mocs_table *table,
429 return low | high << 16;
433 * emit_mocs_l3cc_table() - emit the mocs control table
434 * @rq: Request to set up the MOCS table for.
435 * @table: The values to program into the control regs.
437 * This function simply emits a MI_LOAD_REGISTER_IMM command for the
438 * given table starting at the given address. This register set is
439 * programmed in pairs.
441 * Return: 0 on success, otherwise the error status.
443 static int emit_mocs_l3cc_table(struct i915_request *rq,
444 const struct drm_i915_mocs_table *table)
450 if (GEM_WARN_ON(table->size > table->n_entries))
453 /* Set unused values to PTE */
454 unused_value = table->table[I915_MOCS_PTE].l3cc_value;
456 cs = intel_ring_begin(rq, 2 + table->n_entries);
460 *cs++ = MI_LOAD_REGISTER_IMM(table->n_entries / 2);
462 for (i = 0; i < table->size / 2; i++) {
463 u16 low = get_entry_l3cc(table, 2 * i);
464 u16 high = get_entry_l3cc(table, 2 * i + 1);
466 *cs++ = i915_mmio_reg_offset(GEN9_LNCFCMOCS(i));
467 *cs++ = l3cc_combine(table, low, high);
470 /* Odd table size - 1 left over */
471 if (table->size & 0x01) {
472 u16 low = get_entry_l3cc(table, 2 * i);
474 *cs++ = i915_mmio_reg_offset(GEN9_LNCFCMOCS(i));
475 *cs++ = l3cc_combine(table, low, unused_value);
479 /* All remaining entries are also unused */
480 for (; i < table->n_entries / 2; i++) {
481 *cs++ = i915_mmio_reg_offset(GEN9_LNCFCMOCS(i));
482 *cs++ = l3cc_combine(table, unused_value, unused_value);
486 intel_ring_advance(rq, cs);
492 * intel_mocs_init_l3cc_table() - program the mocs control table
493 * @dev_priv: i915 device private
495 * This function simply programs the mocs registers for the given table
496 * starting at the given address. This register set is programmed in pairs.
498 * These registers may get programmed more than once, it is simpler to
499 * re-program 32 registers than maintain the state of when they were programmed.
500 * We are always reprogramming with the same values and this only on context
505 void intel_mocs_init_l3cc_table(struct drm_i915_private *dev_priv)
507 struct drm_i915_mocs_table table;
511 if (!get_mocs_settings(dev_priv, &table))
514 /* Set unused values to PTE */
515 unused_value = table.table[I915_MOCS_PTE].l3cc_value;
517 for (i = 0; i < table.size / 2; i++) {
518 u16 low = get_entry_l3cc(&table, 2 * i);
519 u16 high = get_entry_l3cc(&table, 2 * i + 1);
521 I915_WRITE(GEN9_LNCFCMOCS(i),
522 l3cc_combine(&table, low, high));
525 /* Odd table size - 1 left over */
526 if (table.size & 0x01) {
527 u16 low = get_entry_l3cc(&table, 2 * i);
529 I915_WRITE(GEN9_LNCFCMOCS(i),
530 l3cc_combine(&table, low, unused_value));
534 /* All remaining entries are also unused */
535 for (; i < table.n_entries / 2; i++)
536 I915_WRITE(GEN9_LNCFCMOCS(i),
537 l3cc_combine(&table, unused_value, unused_value));
541 * intel_rcs_context_init_mocs() - program the MOCS register.
542 * @rq: Request to set up the MOCS tables for.
544 * This function will emit a batch buffer with the values required for
545 * programming the MOCS register values for all the currently supported
548 * These registers are partially stored in the RCS context, so they are
549 * emitted at the same time so that when a context is created these registers
550 * are set up. These registers have to be emitted into the start of the
551 * context as setting the ELSP will re-init some of these registers back
554 * Return: 0 on success, otherwise the error status.
556 int intel_rcs_context_init_mocs(struct i915_request *rq)
558 struct drm_i915_mocs_table t;
561 if (get_mocs_settings(rq->i915, &t)) {
562 /* Program the RCS control registers */
563 ret = emit_mocs_control_table(rq, &t);
567 /* Now program the l3cc registers */
568 ret = emit_mocs_l3cc_table(rq, &t);