xref: /openbmc/linux/drivers/gpu/drm/i915/gt/intel_mocs.c (revision 2b77dcc5)
1 /*
2  * Copyright (c) 2015 Intel Corporation
3  *
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
12  * Software.
13  *
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
20  * SOFTWARE.
21  */
22 
23 #include "i915_drv.h"
24 
25 #include "intel_engine.h"
26 #include "intel_gt.h"
27 #include "intel_mocs.h"
28 #include "intel_lrc.h"
29 #include "intel_ring.h"
30 
31 /* structures required */
32 struct drm_i915_mocs_entry {
33 	u32 control_value;
34 	u16 l3cc_value;
35 	u16 used;
36 };
37 
38 struct drm_i915_mocs_table {
39 	unsigned int size;
40 	unsigned int n_entries;
41 	const struct drm_i915_mocs_entry *table;
42 };
43 
44 /* Defines for the tables (XXX_MOCS_0 - XXX_MOCS_63) */
45 #define _LE_CACHEABILITY(value)	((value) << 0)
46 #define _LE_TGT_CACHE(value)	((value) << 2)
47 #define LE_LRUM(value)		((value) << 4)
48 #define LE_AOM(value)		((value) << 6)
49 #define LE_RSC(value)		((value) << 7)
50 #define LE_SCC(value)		((value) << 8)
51 #define LE_PFM(value)		((value) << 11)
52 #define LE_SCF(value)		((value) << 14)
53 #define LE_COS(value)		((value) << 15)
54 #define LE_SSE(value)		((value) << 17)
55 
56 /* Defines for the tables (LNCFMOCS0 - LNCFMOCS31) - two entries per word */
57 #define L3_ESC(value)		((value) << 0)
58 #define L3_SCC(value)		((value) << 1)
59 #define _L3_CACHEABILITY(value)	((value) << 4)
60 
61 /* Helper defines */
62 #define GEN9_NUM_MOCS_ENTRIES	62  /* 62 out of 64 - 63 & 64 are reserved. */
63 #define GEN11_NUM_MOCS_ENTRIES	64  /* 63-64 are reserved, but configured. */
64 
65 /* (e)LLC caching options */
66 /*
67  * Note: LE_0_PAGETABLE works only up to Gen11; for newer gens it means
68  * the same as LE_UC
69  */
70 #define LE_0_PAGETABLE		_LE_CACHEABILITY(0)
71 #define LE_1_UC			_LE_CACHEABILITY(1)
72 #define LE_2_WT			_LE_CACHEABILITY(2)
73 #define LE_3_WB			_LE_CACHEABILITY(3)
74 
75 /* Target cache */
76 #define LE_TC_0_PAGETABLE	_LE_TGT_CACHE(0)
77 #define LE_TC_1_LLC		_LE_TGT_CACHE(1)
78 #define LE_TC_2_LLC_ELLC	_LE_TGT_CACHE(2)
79 #define LE_TC_3_LLC_ELLC_ALT	_LE_TGT_CACHE(3)
80 
81 /* L3 caching options */
82 #define L3_0_DIRECT		_L3_CACHEABILITY(0)
83 #define L3_1_UC			_L3_CACHEABILITY(1)
84 #define L3_2_RESERVED		_L3_CACHEABILITY(2)
85 #define L3_3_WB			_L3_CACHEABILITY(3)
86 
87 #define MOCS_ENTRY(__idx, __control_value, __l3cc_value) \
88 	[__idx] = { \
89 		.control_value = __control_value, \
90 		.l3cc_value = __l3cc_value, \
91 		.used = 1, \
92 	}
93 
94 /*
95  * MOCS tables
96  *
97  * These are the MOCS tables that are programmed across all the rings.
98  * The control value is programmed to all the rings that support the
99  * MOCS registers. While the l3cc_values are only programmed to the
100  * LNCFCMOCS0 - LNCFCMOCS32 registers.
101  *
102  * These tables are intended to be kept reasonably consistent across
103  * HW platforms, and for ICL+, be identical across OSes. To achieve
104  * that, for Icelake and above, list of entries is published as part
105  * of bspec.
106  *
107  * Entries not part of the following tables are undefined as far as
108  * userspace is concerned and shouldn't be relied upon.  For Gen < 12
109  * they will be initialized to PTE. Gen >= 12 onwards don't have a setting for
110  * PTE and will be initialized to an invalid value.
111  *
112  * The last two entries are reserved by the hardware. For ICL+ they
113  * should be initialized according to bspec and never used, for older
114  * platforms they should never be written to.
115  *
116  * NOTE: These tables are part of bspec and defined as part of hardware
117  *       interface for ICL+. For older platforms, they are part of kernel
118  *       ABI. It is expected that, for specific hardware platform, existing
119  *       entries will remain constant and the table will only be updated by
120  *       adding new entries, filling unused positions.
121  */
122 #define GEN9_MOCS_ENTRIES \
123 	MOCS_ENTRY(I915_MOCS_UNCACHED, \
124 		   LE_1_UC | LE_TC_2_LLC_ELLC, \
125 		   L3_1_UC), \
126 	MOCS_ENTRY(I915_MOCS_PTE, \
127 		   LE_0_PAGETABLE | LE_TC_2_LLC_ELLC | LE_LRUM(3), \
128 		   L3_3_WB)
129 
130 static const struct drm_i915_mocs_entry skylake_mocs_table[] = {
131 	GEN9_MOCS_ENTRIES,
132 	MOCS_ENTRY(I915_MOCS_CACHED,
133 		   LE_3_WB | LE_TC_2_LLC_ELLC | LE_LRUM(3),
134 		   L3_3_WB)
135 };
136 
137 /* NOTE: the LE_TGT_CACHE is not used on Broxton */
138 static const struct drm_i915_mocs_entry broxton_mocs_table[] = {
139 	GEN9_MOCS_ENTRIES,
140 	MOCS_ENTRY(I915_MOCS_CACHED,
141 		   LE_1_UC | LE_TC_2_LLC_ELLC | LE_LRUM(3),
142 		   L3_3_WB)
143 };
144 
145 #define GEN11_MOCS_ENTRIES \
146 	/* Entries 0 and 1 are defined per-platform */ \
147 	/* Base - L3 + LLC */ \
148 	MOCS_ENTRY(2, \
149 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3), \
150 		   L3_3_WB), \
151 	/* Base - Uncached */ \
152 	MOCS_ENTRY(3, \
153 		   LE_1_UC | LE_TC_1_LLC, \
154 		   L3_1_UC), \
155 	/* Base - L3 */ \
156 	MOCS_ENTRY(4, \
157 		   LE_1_UC | LE_TC_1_LLC, \
158 		   L3_3_WB), \
159 	/* Base - LLC */ \
160 	MOCS_ENTRY(5, \
161 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3), \
162 		   L3_1_UC), \
163 	/* Age 0 - LLC */ \
164 	MOCS_ENTRY(6, \
165 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(1), \
166 		   L3_1_UC), \
167 	/* Age 0 - L3 + LLC */ \
168 	MOCS_ENTRY(7, \
169 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(1), \
170 		   L3_3_WB), \
171 	/* Age: Don't Chg. - LLC */ \
172 	MOCS_ENTRY(8, \
173 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(2), \
174 		   L3_1_UC), \
175 	/* Age: Don't Chg. - L3 + LLC */ \
176 	MOCS_ENTRY(9, \
177 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(2), \
178 		   L3_3_WB), \
179 	/* No AOM - LLC */ \
180 	MOCS_ENTRY(10, \
181 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_AOM(1), \
182 		   L3_1_UC), \
183 	/* No AOM - L3 + LLC */ \
184 	MOCS_ENTRY(11, \
185 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_AOM(1), \
186 		   L3_3_WB), \
187 	/* No AOM; Age 0 - LLC */ \
188 	MOCS_ENTRY(12, \
189 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(1) | LE_AOM(1), \
190 		   L3_1_UC), \
191 	/* No AOM; Age 0 - L3 + LLC */ \
192 	MOCS_ENTRY(13, \
193 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(1) | LE_AOM(1), \
194 		   L3_3_WB), \
195 	/* No AOM; Age:DC - LLC */ \
196 	MOCS_ENTRY(14, \
197 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(2) | LE_AOM(1), \
198 		   L3_1_UC), \
199 	/* No AOM; Age:DC - L3 + LLC */ \
200 	MOCS_ENTRY(15, \
201 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(2) | LE_AOM(1), \
202 		   L3_3_WB), \
203 	/* Bypass LLC - Uncached (EHL+) */ \
204 	MOCS_ENTRY(16, \
205 		   LE_1_UC | LE_TC_1_LLC | LE_SCF(1), \
206 		   L3_1_UC), \
207 	/* Bypass LLC - L3 (Read-Only) (EHL+) */ \
208 	MOCS_ENTRY(17, \
209 		   LE_1_UC | LE_TC_1_LLC | LE_SCF(1), \
210 		   L3_3_WB), \
211 	/* Self-Snoop - L3 + LLC */ \
212 	MOCS_ENTRY(18, \
213 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_SSE(3), \
214 		   L3_3_WB), \
215 	/* Skip Caching - L3 + LLC(12.5%) */ \
216 	MOCS_ENTRY(19, \
217 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_SCC(7), \
218 		   L3_3_WB), \
219 	/* Skip Caching - L3 + LLC(25%) */ \
220 	MOCS_ENTRY(20, \
221 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_SCC(3), \
222 		   L3_3_WB), \
223 	/* Skip Caching - L3 + LLC(50%) */ \
224 	MOCS_ENTRY(21, \
225 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_SCC(1), \
226 		   L3_3_WB), \
227 	/* Skip Caching - L3 + LLC(75%) */ \
228 	MOCS_ENTRY(22, \
229 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_RSC(1) | LE_SCC(3), \
230 		   L3_3_WB), \
231 	/* Skip Caching - L3 + LLC(87.5%) */ \
232 	MOCS_ENTRY(23, \
233 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_RSC(1) | LE_SCC(7), \
234 		   L3_3_WB), \
235 	/* HW Reserved - SW program but never use */ \
236 	MOCS_ENTRY(62, \
237 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3), \
238 		   L3_1_UC), \
239 	/* HW Reserved - SW program but never use */ \
240 	MOCS_ENTRY(63, \
241 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3), \
242 		   L3_1_UC)
243 
244 static const struct drm_i915_mocs_entry tigerlake_mocs_table[] = {
245 	/* Base - Error (Reserved for Non-Use) */
246 	MOCS_ENTRY(0, 0x0, 0x0),
247 	/* Base - Reserved */
248 	MOCS_ENTRY(1, 0x0, 0x0),
249 
250 	GEN11_MOCS_ENTRIES,
251 
252 	/* Implicitly enable L1 - HDC:L1 + L3 + LLC */
253 	MOCS_ENTRY(48,
254 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3),
255 		   L3_3_WB),
256 	/* Implicitly enable L1 - HDC:L1 + L3 */
257 	MOCS_ENTRY(49,
258 		   LE_1_UC | LE_TC_1_LLC,
259 		   L3_3_WB),
260 	/* Implicitly enable L1 - HDC:L1 + LLC */
261 	MOCS_ENTRY(50,
262 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3),
263 		   L3_1_UC),
264 	/* Implicitly enable L1 - HDC:L1 */
265 	MOCS_ENTRY(51,
266 		   LE_1_UC | LE_TC_1_LLC,
267 		   L3_1_UC),
268 	/* HW Special Case (CCS) */
269 	MOCS_ENTRY(60,
270 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3),
271 		   L3_1_UC),
272 	/* HW Special Case (Displayable) */
273 	MOCS_ENTRY(61,
274 		   LE_1_UC | LE_TC_1_LLC | LE_SCF(1),
275 		   L3_3_WB),
276 };
277 
278 static const struct drm_i915_mocs_entry icelake_mocs_table[] = {
279 	/* Base - Uncached (Deprecated) */
280 	MOCS_ENTRY(I915_MOCS_UNCACHED,
281 		   LE_1_UC | LE_TC_1_LLC,
282 		   L3_1_UC),
283 	/* Base - L3 + LeCC:PAT (Deprecated) */
284 	MOCS_ENTRY(I915_MOCS_PTE,
285 		   LE_0_PAGETABLE | LE_TC_1_LLC,
286 		   L3_3_WB),
287 
288 	GEN11_MOCS_ENTRIES
289 };
290 
291 static bool get_mocs_settings(const struct drm_i915_private *i915,
292 			      struct drm_i915_mocs_table *table)
293 {
294 	bool result = false;
295 
296 	if (INTEL_GEN(i915) >= 12) {
297 		table->size  = ARRAY_SIZE(tigerlake_mocs_table);
298 		table->table = tigerlake_mocs_table;
299 		table->n_entries = GEN11_NUM_MOCS_ENTRIES;
300 		result = true;
301 	} else if (IS_GEN(i915, 11)) {
302 		table->size  = ARRAY_SIZE(icelake_mocs_table);
303 		table->table = icelake_mocs_table;
304 		table->n_entries = GEN11_NUM_MOCS_ENTRIES;
305 		result = true;
306 	} else if (IS_GEN9_BC(i915) || IS_CANNONLAKE(i915)) {
307 		table->size  = ARRAY_SIZE(skylake_mocs_table);
308 		table->n_entries = GEN9_NUM_MOCS_ENTRIES;
309 		table->table = skylake_mocs_table;
310 		result = true;
311 	} else if (IS_GEN9_LP(i915)) {
312 		table->size  = ARRAY_SIZE(broxton_mocs_table);
313 		table->n_entries = GEN9_NUM_MOCS_ENTRIES;
314 		table->table = broxton_mocs_table;
315 		result = true;
316 	} else {
317 		WARN_ONCE(INTEL_GEN(i915) >= 9,
318 			  "Platform that should have a MOCS table does not.\n");
319 	}
320 
321 	/* WaDisableSkipCaching:skl,bxt,kbl,glk */
322 	if (IS_GEN(i915, 9)) {
323 		int i;
324 
325 		for (i = 0; i < table->size; i++)
326 			if (WARN_ON(table->table[i].l3cc_value &
327 				    (L3_ESC(1) | L3_SCC(0x7))))
328 				return false;
329 	}
330 
331 	return result;
332 }
333 
334 static i915_reg_t mocs_register(const struct intel_engine_cs *engine, int index)
335 {
336 	switch (engine->id) {
337 	case RCS0:
338 		return GEN9_GFX_MOCS(index);
339 	case VCS0:
340 		return GEN9_MFX0_MOCS(index);
341 	case BCS0:
342 		return GEN9_BLT_MOCS(index);
343 	case VECS0:
344 		return GEN9_VEBOX_MOCS(index);
345 	case VCS1:
346 		return GEN9_MFX1_MOCS(index);
347 	case VCS2:
348 		return GEN11_MFX2_MOCS(index);
349 	default:
350 		MISSING_CASE(engine->id);
351 		return INVALID_MMIO_REG;
352 	}
353 }
354 
355 /*
356  * Get control_value from MOCS entry taking into account when it's not used:
357  * I915_MOCS_PTE's value is returned in this case.
358  */
359 static u32 get_entry_control(const struct drm_i915_mocs_table *table,
360 			     unsigned int index)
361 {
362 	if (table->table[index].used)
363 		return table->table[index].control_value;
364 
365 	return table->table[I915_MOCS_PTE].control_value;
366 }
367 
368 static void init_mocs_table(struct intel_engine_cs *engine,
369 			    const struct drm_i915_mocs_table *table)
370 {
371 	struct intel_uncore *uncore = engine->uncore;
372 	u32 unused_value = table->table[I915_MOCS_PTE].control_value;
373 	unsigned int i;
374 
375 	for (i = 0; i < table->size; i++)
376 		intel_uncore_write_fw(uncore,
377 				      mocs_register(engine, i),
378 				      get_entry_control(table, i));
379 
380 	/* All remaining entries are unused */
381 	for (; i < table->n_entries; i++)
382 		intel_uncore_write_fw(uncore,
383 				      mocs_register(engine, i),
384 				      unused_value);
385 }
386 
387 /*
388  * Get l3cc_value from MOCS entry taking into account when it's not used:
389  * I915_MOCS_PTE's value is returned in this case.
390  */
391 static u16 get_entry_l3cc(const struct drm_i915_mocs_table *table,
392 			  unsigned int index)
393 {
394 	if (table->table[index].used)
395 		return table->table[index].l3cc_value;
396 
397 	return table->table[I915_MOCS_PTE].l3cc_value;
398 }
399 
400 static inline u32 l3cc_combine(const struct drm_i915_mocs_table *table,
401 			       u16 low,
402 			       u16 high)
403 {
404 	return low | (u32)high << 16;
405 }
406 
407 static void init_l3cc_table(struct intel_engine_cs *engine,
408 			    const struct drm_i915_mocs_table *table)
409 {
410 	struct intel_uncore *uncore = engine->uncore;
411 	u16 unused_value = table->table[I915_MOCS_PTE].l3cc_value;
412 	unsigned int i;
413 
414 	for (i = 0; i < table->size / 2; i++) {
415 		u16 low = get_entry_l3cc(table, 2 * i);
416 		u16 high = get_entry_l3cc(table, 2 * i + 1);
417 
418 		intel_uncore_write(uncore,
419 				   GEN9_LNCFCMOCS(i),
420 				   l3cc_combine(table, low, high));
421 	}
422 
423 	/* Odd table size - 1 left over */
424 	if (table->size & 1) {
425 		u16 low = get_entry_l3cc(table, 2 * i);
426 
427 		intel_uncore_write(uncore,
428 				   GEN9_LNCFCMOCS(i),
429 				   l3cc_combine(table, low, unused_value));
430 		i++;
431 	}
432 
433 	/* All remaining entries are also unused */
434 	for (; i < table->n_entries / 2; i++)
435 		intel_uncore_write(uncore,
436 				   GEN9_LNCFCMOCS(i),
437 				   l3cc_combine(table, unused_value,
438 						unused_value));
439 }
440 
441 void intel_mocs_init_engine(struct intel_engine_cs *engine)
442 {
443 	struct drm_i915_mocs_table table;
444 
445 	/* Called under a blanket forcewake */
446 	assert_forcewakes_active(engine->uncore, FORCEWAKE_ALL);
447 
448 	if (!get_mocs_settings(engine->i915, &table))
449 		return;
450 
451 	/* Platforms with global MOCS do not need per-engine initialization. */
452 	if (!HAS_GLOBAL_MOCS_REGISTERS(engine->i915))
453 		init_mocs_table(engine, &table);
454 
455 	if (engine->class == RENDER_CLASS)
456 		init_l3cc_table(engine, &table);
457 }
458 
459 static void intel_mocs_init_global(struct intel_gt *gt)
460 {
461 	struct intel_uncore *uncore = gt->uncore;
462 	struct drm_i915_mocs_table table;
463 	unsigned int index;
464 
465 	/*
466 	 * LLC and eDRAM control values are not applicable to dgfx
467 	 */
468 	if (IS_DGFX(gt->i915))
469 		return;
470 
471 	GEM_BUG_ON(!HAS_GLOBAL_MOCS_REGISTERS(gt->i915));
472 
473 	if (!get_mocs_settings(gt->i915, &table))
474 		return;
475 
476 	if (GEM_DEBUG_WARN_ON(table.size > table.n_entries))
477 		return;
478 
479 	for (index = 0; index < table.size; index++)
480 		intel_uncore_write(uncore,
481 				   GEN12_GLOBAL_MOCS(index),
482 				   table.table[index].control_value);
483 
484 	/*
485 	 * Ok, now set the unused entries to the invalid entry (index 0). These
486 	 * entries are officially undefined and no contract for the contents and
487 	 * settings is given for these entries.
488 	 */
489 	for (; index < table.n_entries; index++)
490 		intel_uncore_write(uncore,
491 				   GEN12_GLOBAL_MOCS(index),
492 				   table.table[0].control_value);
493 }
494 
495 void intel_mocs_init(struct intel_gt *gt)
496 {
497 	if (HAS_GLOBAL_MOCS_REGISTERS(gt->i915))
498 		intel_mocs_init_global(gt);
499 }
500