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
23 #include "intel_mocs.h"
24 #include "intel_lrc.h"
25 #include "intel_ringbuffer.h"
27 /* structures required */
28 struct drm_i915_mocs_entry
{
33 struct drm_i915_mocs_table
{
35 const struct drm_i915_mocs_entry
*table
;
38 /* Defines for the tables (XXX_MOCS_0 - XXX_MOCS_63) */
39 #define LE_CACHEABILITY(value) ((value) << 0)
40 #define LE_TGT_CACHE(value) ((value) << 2)
41 #define LE_LRUM(value) ((value) << 4)
42 #define LE_AOM(value) ((value) << 6)
43 #define LE_RSC(value) ((value) << 7)
44 #define LE_SCC(value) ((value) << 8)
45 #define LE_PFM(value) ((value) << 11)
46 #define LE_SCF(value) ((value) << 14)
48 /* Defines for the tables (LNCFMOCS0 - LNCFMOCS31) - two entries per word */
49 #define L3_ESC(value) ((value) << 0)
50 #define L3_SCC(value) ((value) << 1)
51 #define L3_CACHEABILITY(value) ((value) << 4)
54 #define GEN9_NUM_MOCS_ENTRIES 62 /* 62 out of 64 - 63 & 64 are reserved. */
56 /* (e)LLC caching options */
57 #define LE_PAGETABLE 0
62 /* L3 caching options */
69 #define LE_TC_PAGETABLE 0
71 #define LE_TC_LLC_ELLC 2
72 #define LE_TC_LLC_ELLC_ALT 3
77 * These are the MOCS tables that are programmed across all the rings.
78 * The control value is programmed to all the rings that support the
79 * MOCS registers. While the l3cc_values are only programmed to the
80 * LNCFCMOCS0 - LNCFCMOCS32 registers.
82 * These tables are intended to be kept reasonably consistent across
83 * platforms. However some of the fields are not applicable to all of
86 * Entries not part of the following tables are undefined as far as
87 * userspace is concerned and shouldn't be relied upon. For the time
88 * being they will be implicitly initialized to the strictest caching
89 * configuration (uncached) to guarantee forwards compatibility with
90 * userspace programs written against more recent kernels providing
91 * additional MOCS entries.
93 * NOTE: These tables MUST start with being uncached and the length
94 * MUST be less than 63 as the last two registers are reserved
95 * by the hardware. These tables are part of the kernel ABI and
96 * may only be updated incrementally by adding entries at the
99 static const struct drm_i915_mocs_entry skylake_mocs_table
[] = {
100 [I915_MOCS_UNCACHED
] = {
102 .control_value
= LE_CACHEABILITY(LE_UC
) |
103 LE_TGT_CACHE(LE_TC_LLC_ELLC
) |
104 LE_LRUM(0) | LE_AOM(0) | LE_RSC(0) | LE_SCC(0) |
105 LE_PFM(0) | LE_SCF(0),
108 .l3cc_value
= L3_ESC(0) | L3_SCC(0) | L3_CACHEABILITY(L3_UC
),
112 .control_value
= LE_CACHEABILITY(LE_PAGETABLE
) |
113 LE_TGT_CACHE(LE_TC_LLC_ELLC
) |
114 LE_LRUM(3) | LE_AOM(0) | LE_RSC(0) | LE_SCC(0) |
115 LE_PFM(0) | LE_SCF(0),
117 .l3cc_value
= L3_ESC(0) | L3_SCC(0) | L3_CACHEABILITY(L3_WB
),
119 [I915_MOCS_CACHED
] = {
121 .control_value
= LE_CACHEABILITY(LE_WB
) |
122 LE_TGT_CACHE(LE_TC_LLC_ELLC
) |
123 LE_LRUM(3) | LE_AOM(0) | LE_RSC(0) | LE_SCC(0) |
124 LE_PFM(0) | LE_SCF(0),
126 .l3cc_value
= L3_ESC(0) | L3_SCC(0) | L3_CACHEABILITY(L3_WB
),
130 /* NOTE: the LE_TGT_CACHE is not used on Broxton */
131 static const struct drm_i915_mocs_entry broxton_mocs_table
[] = {
132 [I915_MOCS_UNCACHED
] = {
134 .control_value
= LE_CACHEABILITY(LE_UC
) |
135 LE_TGT_CACHE(LE_TC_LLC_ELLC
) |
136 LE_LRUM(0) | LE_AOM(0) | LE_RSC(0) | LE_SCC(0) |
137 LE_PFM(0) | LE_SCF(0),
140 .l3cc_value
= L3_ESC(0) | L3_SCC(0) | L3_CACHEABILITY(L3_UC
),
144 .control_value
= LE_CACHEABILITY(LE_PAGETABLE
) |
145 LE_TGT_CACHE(LE_TC_LLC_ELLC
) |
146 LE_LRUM(3) | LE_AOM(0) | LE_RSC(0) | LE_SCC(0) |
147 LE_PFM(0) | LE_SCF(0),
150 .l3cc_value
= L3_ESC(0) | L3_SCC(0) | L3_CACHEABILITY(L3_WB
),
152 [I915_MOCS_CACHED
] = {
154 .control_value
= LE_CACHEABILITY(LE_UC
) |
155 LE_TGT_CACHE(LE_TC_LLC_ELLC
) |
156 LE_LRUM(3) | LE_AOM(0) | LE_RSC(0) | LE_SCC(0) |
157 LE_PFM(0) | LE_SCF(0),
160 .l3cc_value
= L3_ESC(0) | L3_SCC(0) | L3_CACHEABILITY(L3_WB
),
165 * get_mocs_settings()
166 * @dev_priv: i915 device.
167 * @table: Output table that will be made to point at appropriate
168 * MOCS values for the device.
170 * This function will return the values of the MOCS table that needs to
171 * be programmed for the platform. It will return the values that need
172 * to be programmed and if they need to be programmed.
174 * Return: true if there are applicable MOCS settings for the device.
176 static bool get_mocs_settings(struct drm_i915_private
*dev_priv
,
177 struct drm_i915_mocs_table
*table
)
181 if (IS_GEN9_BC(dev_priv
) || IS_CANNONLAKE(dev_priv
) ||
182 IS_ICELAKE(dev_priv
)) {
183 table
->size
= ARRAY_SIZE(skylake_mocs_table
);
184 table
->table
= skylake_mocs_table
;
186 } else if (IS_GEN9_LP(dev_priv
)) {
187 table
->size
= ARRAY_SIZE(broxton_mocs_table
);
188 table
->table
= broxton_mocs_table
;
191 WARN_ONCE(INTEL_GEN(dev_priv
) >= 9,
192 "Platform that should have a MOCS table does not.\n");
195 /* WaDisableSkipCaching:skl,bxt,kbl,glk */
196 if (IS_GEN9(dev_priv
)) {
199 for (i
= 0; i
< table
->size
; i
++)
200 if (WARN_ON(table
->table
[i
].l3cc_value
&
201 (L3_ESC(1) | L3_SCC(0x7))))
208 static i915_reg_t
mocs_register(enum intel_engine_id engine_id
, int index
)
212 return GEN9_GFX_MOCS(index
);
214 return GEN9_MFX0_MOCS(index
);
216 return GEN9_BLT_MOCS(index
);
218 return GEN9_VEBOX_MOCS(index
);
220 return GEN9_MFX1_MOCS(index
);
222 return GEN11_MFX2_MOCS(index
);
224 MISSING_CASE(engine_id
);
225 return INVALID_MMIO_REG
;
230 * intel_mocs_init_engine() - emit the mocs control table
231 * @engine: The engine for whom to emit the registers.
233 * This function simply emits a MI_LOAD_REGISTER_IMM command for the
234 * given table starting at the given address.
236 * Return: 0 on success, otherwise the error status.
238 int intel_mocs_init_engine(struct intel_engine_cs
*engine
)
240 struct drm_i915_private
*dev_priv
= engine
->i915
;
241 struct drm_i915_mocs_table table
;
244 if (!get_mocs_settings(dev_priv
, &table
))
247 if (WARN_ON(table
.size
> GEN9_NUM_MOCS_ENTRIES
))
250 for (index
= 0; index
< table
.size
; index
++)
251 I915_WRITE(mocs_register(engine
->id
, index
),
252 table
.table
[index
].control_value
);
255 * Ok, now set the unused entries to uncached. These entries
256 * are officially undefined and no contract for the contents
257 * and settings is given for these entries.
259 * Entry 0 in the table is uncached - so we are just writing
260 * that value to all the used entries.
262 for (; index
< GEN9_NUM_MOCS_ENTRIES
; index
++)
263 I915_WRITE(mocs_register(engine
->id
, index
),
264 table
.table
[0].control_value
);
270 * emit_mocs_control_table() - emit the mocs control table
271 * @rq: Request to set up the MOCS table for.
272 * @table: The values to program into the control regs.
274 * This function simply emits a MI_LOAD_REGISTER_IMM command for the
275 * given table starting at the given address.
277 * Return: 0 on success, otherwise the error status.
279 static int emit_mocs_control_table(struct i915_request
*rq
,
280 const struct drm_i915_mocs_table
*table
)
282 enum intel_engine_id engine
= rq
->engine
->id
;
286 if (WARN_ON(table
->size
> GEN9_NUM_MOCS_ENTRIES
))
289 cs
= intel_ring_begin(rq
, 2 + 2 * GEN9_NUM_MOCS_ENTRIES
);
293 *cs
++ = MI_LOAD_REGISTER_IMM(GEN9_NUM_MOCS_ENTRIES
);
295 for (index
= 0; index
< table
->size
; index
++) {
296 *cs
++ = i915_mmio_reg_offset(mocs_register(engine
, index
));
297 *cs
++ = table
->table
[index
].control_value
;
301 * Ok, now set the unused entries to uncached. These entries
302 * are officially undefined and no contract for the contents
303 * and settings is given for these entries.
305 * Entry 0 in the table is uncached - so we are just writing
306 * that value to all the used entries.
308 for (; index
< GEN9_NUM_MOCS_ENTRIES
; index
++) {
309 *cs
++ = i915_mmio_reg_offset(mocs_register(engine
, index
));
310 *cs
++ = table
->table
[0].control_value
;
314 intel_ring_advance(rq
, cs
);
319 static inline u32
l3cc_combine(const struct drm_i915_mocs_table
*table
,
323 return table
->table
[low
].l3cc_value
|
324 table
->table
[high
].l3cc_value
<< 16;
328 * emit_mocs_l3cc_table() - emit the mocs control table
329 * @rq: Request to set up the MOCS table for.
330 * @table: The values to program into the control regs.
332 * This function simply emits a MI_LOAD_REGISTER_IMM command for the
333 * given table starting at the given address. This register set is
334 * programmed in pairs.
336 * Return: 0 on success, otherwise the error status.
338 static int emit_mocs_l3cc_table(struct i915_request
*rq
,
339 const struct drm_i915_mocs_table
*table
)
344 if (WARN_ON(table
->size
> GEN9_NUM_MOCS_ENTRIES
))
347 cs
= intel_ring_begin(rq
, 2 + GEN9_NUM_MOCS_ENTRIES
);
351 *cs
++ = MI_LOAD_REGISTER_IMM(GEN9_NUM_MOCS_ENTRIES
/ 2);
353 for (i
= 0; i
< table
->size
/2; i
++) {
354 *cs
++ = i915_mmio_reg_offset(GEN9_LNCFCMOCS(i
));
355 *cs
++ = l3cc_combine(table
, 2 * i
, 2 * i
+ 1);
358 if (table
->size
& 0x01) {
359 /* Odd table size - 1 left over */
360 *cs
++ = i915_mmio_reg_offset(GEN9_LNCFCMOCS(i
));
361 *cs
++ = l3cc_combine(table
, 2 * i
, 0);
366 * Now set the rest of the table to uncached - use entry 0 as
367 * this will be uncached. Leave the last pair uninitialised as
368 * they are reserved by the hardware.
370 for (; i
< GEN9_NUM_MOCS_ENTRIES
/ 2; i
++) {
371 *cs
++ = i915_mmio_reg_offset(GEN9_LNCFCMOCS(i
));
372 *cs
++ = l3cc_combine(table
, 0, 0);
376 intel_ring_advance(rq
, cs
);
382 * intel_mocs_init_l3cc_table() - program the mocs control table
383 * @dev_priv: i915 device private
385 * This function simply programs the mocs registers for the given table
386 * starting at the given address. This register set is programmed in pairs.
388 * These registers may get programmed more than once, it is simpler to
389 * re-program 32 registers than maintain the state of when they were programmed.
390 * We are always reprogramming with the same values and this only on context
395 void intel_mocs_init_l3cc_table(struct drm_i915_private
*dev_priv
)
397 struct drm_i915_mocs_table table
;
400 if (!get_mocs_settings(dev_priv
, &table
))
403 for (i
= 0; i
< table
.size
/2; i
++)
404 I915_WRITE(GEN9_LNCFCMOCS(i
), l3cc_combine(&table
, 2*i
, 2*i
+1));
406 /* Odd table size - 1 left over */
407 if (table
.size
& 0x01) {
408 I915_WRITE(GEN9_LNCFCMOCS(i
), l3cc_combine(&table
, 2*i
, 0));
413 * Now set the rest of the table to uncached - use entry 0 as
414 * this will be uncached. Leave the last pair as initialised as
415 * they are reserved by the hardware.
417 for (; i
< (GEN9_NUM_MOCS_ENTRIES
/ 2); i
++)
418 I915_WRITE(GEN9_LNCFCMOCS(i
), l3cc_combine(&table
, 0, 0));
422 * intel_rcs_context_init_mocs() - program the MOCS register.
423 * @rq: Request to set up the MOCS tables for.
425 * This function will emit a batch buffer with the values required for
426 * programming the MOCS register values for all the currently supported
429 * These registers are partially stored in the RCS context, so they are
430 * emitted at the same time so that when a context is created these registers
431 * are set up. These registers have to be emitted into the start of the
432 * context as setting the ELSP will re-init some of these registers back
435 * Return: 0 on success, otherwise the error status.
437 int intel_rcs_context_init_mocs(struct i915_request
*rq
)
439 struct drm_i915_mocs_table t
;
442 if (get_mocs_settings(rq
->i915
, &t
)) {
443 /* Program the RCS control registers */
444 ret
= emit_mocs_control_table(rq
, &t
);
448 /* Now program the l3cc registers */
449 ret
= emit_mocs_l3cc_table(rq
, &t
);