xref: /linux/drivers/gpu/drm/i915/gt/intel_mocs.c (revision 2993c9b04e616df0848b655d7202a707a70fc876)
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 
30 /* structures required */
31 struct drm_i915_mocs_entry {
32 	u32 control_value;
33 	u16 l3cc_value;
34 	u16 used;
35 };
36 
37 struct drm_i915_mocs_table {
38 	unsigned int size;
39 	unsigned int n_entries;
40 	const struct drm_i915_mocs_entry *table;
41 };
42 
43 /* Defines for the tables (XXX_MOCS_0 - XXX_MOCS_63) */
44 #define _LE_CACHEABILITY(value)	((value) << 0)
45 #define _LE_TGT_CACHE(value)	((value) << 2)
46 #define LE_LRUM(value)		((value) << 4)
47 #define LE_AOM(value)		((value) << 6)
48 #define LE_RSC(value)		((value) << 7)
49 #define LE_SCC(value)		((value) << 8)
50 #define LE_PFM(value)		((value) << 11)
51 #define LE_SCF(value)		((value) << 14)
52 #define LE_COS(value)		((value) << 15)
53 #define LE_SSE(value)		((value) << 17)
54 
55 /* Defines for the tables (LNCFMOCS0 - LNCFMOCS31) - two entries per word */
56 #define L3_ESC(value)		((value) << 0)
57 #define L3_SCC(value)		((value) << 1)
58 #define _L3_CACHEABILITY(value)	((value) << 4)
59 
60 /* Helper defines */
61 #define GEN9_NUM_MOCS_ENTRIES	62  /* 62 out of 64 - 63 & 64 are reserved. */
62 #define GEN11_NUM_MOCS_ENTRIES	64  /* 63-64 are reserved, but configured. */
63 
64 /* (e)LLC caching options */
65 /*
66  * Note: LE_0_PAGETABLE works only up to Gen11; for newer gens it means
67  * the same as LE_UC
68  */
69 #define LE_0_PAGETABLE		_LE_CACHEABILITY(0)
70 #define LE_1_UC			_LE_CACHEABILITY(1)
71 #define LE_2_WT			_LE_CACHEABILITY(2)
72 #define LE_3_WB			_LE_CACHEABILITY(3)
73 
74 /* Target cache */
75 #define LE_TC_0_PAGETABLE	_LE_TGT_CACHE(0)
76 #define LE_TC_1_LLC		_LE_TGT_CACHE(1)
77 #define LE_TC_2_LLC_ELLC	_LE_TGT_CACHE(2)
78 #define LE_TC_3_LLC_ELLC_ALT	_LE_TGT_CACHE(3)
79 
80 /* L3 caching options */
81 #define L3_0_DIRECT		_L3_CACHEABILITY(0)
82 #define L3_1_UC			_L3_CACHEABILITY(1)
83 #define L3_2_RESERVED		_L3_CACHEABILITY(2)
84 #define L3_3_WB			_L3_CACHEABILITY(3)
85 
86 #define MOCS_ENTRY(__idx, __control_value, __l3cc_value) \
87 	[__idx] = { \
88 		.control_value = __control_value, \
89 		.l3cc_value = __l3cc_value, \
90 		.used = 1, \
91 	}
92 
93 /*
94  * MOCS tables
95  *
96  * These are the MOCS tables that are programmed across all the rings.
97  * The control value is programmed to all the rings that support the
98  * MOCS registers. While the l3cc_values are only programmed to the
99  * LNCFCMOCS0 - LNCFCMOCS32 registers.
100  *
101  * These tables are intended to be kept reasonably consistent across
102  * HW platforms, and for ICL+, be identical across OSes. To achieve
103  * that, for Icelake and above, list of entries is published as part
104  * of bspec.
105  *
106  * Entries not part of the following tables are undefined as far as
107  * userspace is concerned and shouldn't be relied upon.  For Gen < 12
108  * they will be initialized to PTE. Gen >= 12 onwards don't have a setting for
109  * PTE and will be initialized to an invalid value.
110  *
111  * The last two entries are reserved by the hardware. For ICL+ they
112  * should be initialized according to bspec and never used, for older
113  * platforms they should never be written to.
114  *
115  * NOTE: These tables are part of bspec and defined as part of hardware
116  *       interface for ICL+. For older platforms, they are part of kernel
117  *       ABI. It is expected that, for specific hardware platform, existing
118  *       entries will remain constant and the table will only be updated by
119  *       adding new entries, filling unused positions.
120  */
121 #define GEN9_MOCS_ENTRIES \
122 	MOCS_ENTRY(I915_MOCS_UNCACHED, \
123 		   LE_1_UC | LE_TC_2_LLC_ELLC, \
124 		   L3_1_UC), \
125 	MOCS_ENTRY(I915_MOCS_PTE, \
126 		   LE_0_PAGETABLE | LE_TC_2_LLC_ELLC | LE_LRUM(3), \
127 		   L3_3_WB)
128 
129 static const struct drm_i915_mocs_entry skylake_mocs_table[] = {
130 	GEN9_MOCS_ENTRIES,
131 	MOCS_ENTRY(I915_MOCS_CACHED,
132 		   LE_3_WB | LE_TC_2_LLC_ELLC | LE_LRUM(3),
133 		   L3_3_WB)
134 };
135 
136 /* NOTE: the LE_TGT_CACHE is not used on Broxton */
137 static const struct drm_i915_mocs_entry broxton_mocs_table[] = {
138 	GEN9_MOCS_ENTRIES,
139 	MOCS_ENTRY(I915_MOCS_CACHED,
140 		   LE_1_UC | LE_TC_2_LLC_ELLC | LE_LRUM(3),
141 		   L3_3_WB)
142 };
143 
144 #define GEN11_MOCS_ENTRIES \
145 	/* Entries 0 and 1 are defined per-platform */ \
146 	/* Base - L3 + LLC */ \
147 	MOCS_ENTRY(2, \
148 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3), \
149 		   L3_3_WB), \
150 	/* Base - Uncached */ \
151 	MOCS_ENTRY(3, \
152 		   LE_1_UC | LE_TC_1_LLC, \
153 		   L3_1_UC), \
154 	/* Base - L3 */ \
155 	MOCS_ENTRY(4, \
156 		   LE_1_UC | LE_TC_1_LLC, \
157 		   L3_3_WB), \
158 	/* Base - LLC */ \
159 	MOCS_ENTRY(5, \
160 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3), \
161 		   L3_1_UC), \
162 	/* Age 0 - LLC */ \
163 	MOCS_ENTRY(6, \
164 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(1), \
165 		   L3_1_UC), \
166 	/* Age 0 - L3 + LLC */ \
167 	MOCS_ENTRY(7, \
168 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(1), \
169 		   L3_3_WB), \
170 	/* Age: Don't Chg. - LLC */ \
171 	MOCS_ENTRY(8, \
172 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(2), \
173 		   L3_1_UC), \
174 	/* Age: Don't Chg. - L3 + LLC */ \
175 	MOCS_ENTRY(9, \
176 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(2), \
177 		   L3_3_WB), \
178 	/* No AOM - LLC */ \
179 	MOCS_ENTRY(10, \
180 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_AOM(1), \
181 		   L3_1_UC), \
182 	/* No AOM - L3 + LLC */ \
183 	MOCS_ENTRY(11, \
184 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_AOM(1), \
185 		   L3_3_WB), \
186 	/* No AOM; Age 0 - LLC */ \
187 	MOCS_ENTRY(12, \
188 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(1) | LE_AOM(1), \
189 		   L3_1_UC), \
190 	/* No AOM; Age 0 - L3 + LLC */ \
191 	MOCS_ENTRY(13, \
192 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(1) | LE_AOM(1), \
193 		   L3_3_WB), \
194 	/* No AOM; Age:DC - LLC */ \
195 	MOCS_ENTRY(14, \
196 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(2) | LE_AOM(1), \
197 		   L3_1_UC), \
198 	/* No AOM; Age:DC - L3 + LLC */ \
199 	MOCS_ENTRY(15, \
200 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(2) | LE_AOM(1), \
201 		   L3_3_WB), \
202 	/* Bypass LLC - Uncached (EHL+) */ \
203 	MOCS_ENTRY(16, \
204 		   LE_1_UC | LE_TC_1_LLC | LE_SCF(1), \
205 		   L3_1_UC), \
206 	/* Bypass LLC - L3 (Read-Only) (EHL+) */ \
207 	MOCS_ENTRY(17, \
208 		   LE_1_UC | LE_TC_1_LLC | LE_SCF(1), \
209 		   L3_3_WB), \
210 	/* Self-Snoop - L3 + LLC */ \
211 	MOCS_ENTRY(18, \
212 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_SSE(3), \
213 		   L3_3_WB), \
214 	/* Skip Caching - L3 + LLC(12.5%) */ \
215 	MOCS_ENTRY(19, \
216 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_SCC(7), \
217 		   L3_3_WB), \
218 	/* Skip Caching - L3 + LLC(25%) */ \
219 	MOCS_ENTRY(20, \
220 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_SCC(3), \
221 		   L3_3_WB), \
222 	/* Skip Caching - L3 + LLC(50%) */ \
223 	MOCS_ENTRY(21, \
224 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_SCC(1), \
225 		   L3_3_WB), \
226 	/* Skip Caching - L3 + LLC(75%) */ \
227 	MOCS_ENTRY(22, \
228 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_RSC(1) | LE_SCC(3), \
229 		   L3_3_WB), \
230 	/* Skip Caching - L3 + LLC(87.5%) */ \
231 	MOCS_ENTRY(23, \
232 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_RSC(1) | LE_SCC(7), \
233 		   L3_3_WB), \
234 	/* HW Reserved - SW program but never use */ \
235 	MOCS_ENTRY(62, \
236 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3), \
237 		   L3_1_UC), \
238 	/* HW Reserved - SW program but never use */ \
239 	MOCS_ENTRY(63, \
240 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3), \
241 		   L3_1_UC)
242 
243 static const struct drm_i915_mocs_entry tigerlake_mocs_table[] = {
244 	/* Base - Error (Reserved for Non-Use) */
245 	MOCS_ENTRY(0, 0x0, 0x0),
246 	/* Base - Reserved */
247 	MOCS_ENTRY(1, 0x0, 0x0),
248 
249 	GEN11_MOCS_ENTRIES,
250 
251 	/* Implicitly enable L1 - HDC:L1 + L3 + LLC */
252 	MOCS_ENTRY(48,
253 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3),
254 		   L3_3_WB),
255 	/* Implicitly enable L1 - HDC:L1 + L3 */
256 	MOCS_ENTRY(49,
257 		   LE_1_UC | LE_TC_1_LLC,
258 		   L3_3_WB),
259 	/* Implicitly enable L1 - HDC:L1 + LLC */
260 	MOCS_ENTRY(50,
261 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3),
262 		   L3_1_UC),
263 	/* Implicitly enable L1 - HDC:L1 */
264 	MOCS_ENTRY(51,
265 		   LE_1_UC | LE_TC_1_LLC,
266 		   L3_1_UC),
267 	/* HW Special Case (CCS) */
268 	MOCS_ENTRY(60,
269 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3),
270 		   L3_1_UC),
271 	/* HW Special Case (Displayable) */
272 	MOCS_ENTRY(61,
273 		   LE_1_UC | LE_TC_1_LLC | LE_SCF(1),
274 		   L3_3_WB),
275 };
276 
277 static const struct drm_i915_mocs_entry icelake_mocs_table[] = {
278 	/* Base - Uncached (Deprecated) */
279 	MOCS_ENTRY(I915_MOCS_UNCACHED,
280 		   LE_1_UC | LE_TC_1_LLC,
281 		   L3_1_UC),
282 	/* Base - L3 + LeCC:PAT (Deprecated) */
283 	MOCS_ENTRY(I915_MOCS_PTE,
284 		   LE_0_PAGETABLE | LE_TC_1_LLC,
285 		   L3_3_WB),
286 
287 	GEN11_MOCS_ENTRIES
288 };
289 
290 static bool get_mocs_settings(struct intel_gt *gt,
291 			      struct drm_i915_mocs_table *table)
292 {
293 	struct drm_i915_private *i915 = gt->i915;
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(enum intel_engine_id engine_id, 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 /**
369  * intel_mocs_init_engine() - emit the mocs control table
370  * @engine:	The engine for whom to emit the registers.
371  *
372  * This function simply emits a MI_LOAD_REGISTER_IMM command for the
373  * given table starting at the given address.
374  */
375 void intel_mocs_init_engine(struct intel_engine_cs *engine)
376 {
377 	struct intel_gt *gt = engine->gt;
378 	struct intel_uncore *uncore = gt->uncore;
379 	struct drm_i915_mocs_table table;
380 	unsigned int index;
381 	u32 unused_value;
382 
383 	/* Platforms with global MOCS do not need per-engine initialization. */
384 	if (HAS_GLOBAL_MOCS_REGISTERS(gt->i915))
385 		return;
386 
387 	/* Called under a blanket forcewake */
388 	assert_forcewakes_active(uncore, FORCEWAKE_ALL);
389 
390 	if (!get_mocs_settings(gt, &table))
391 		return;
392 
393 	/* Set unused values to PTE */
394 	unused_value = table.table[I915_MOCS_PTE].control_value;
395 
396 	for (index = 0; index < table.size; index++) {
397 		u32 value = get_entry_control(&table, index);
398 
399 		intel_uncore_write_fw(uncore,
400 				      mocs_register(engine->id, index),
401 				      value);
402 	}
403 
404 	/* All remaining entries are also unused */
405 	for (; index < table.n_entries; index++)
406 		intel_uncore_write_fw(uncore,
407 				      mocs_register(engine->id, index),
408 				      unused_value);
409 }
410 
411 static void intel_mocs_init_global(struct intel_gt *gt)
412 {
413 	struct intel_uncore *uncore = gt->uncore;
414 	struct drm_i915_mocs_table table;
415 	unsigned int index;
416 
417 	GEM_BUG_ON(!HAS_GLOBAL_MOCS_REGISTERS(gt->i915));
418 
419 	if (!get_mocs_settings(gt, &table))
420 		return;
421 
422 	if (GEM_DEBUG_WARN_ON(table.size > table.n_entries))
423 		return;
424 
425 	for (index = 0; index < table.size; index++)
426 		intel_uncore_write(uncore,
427 				   GEN12_GLOBAL_MOCS(index),
428 				   table.table[index].control_value);
429 
430 	/*
431 	 * Ok, now set the unused entries to the invalid entry (index 0). These
432 	 * entries are officially undefined and no contract for the contents and
433 	 * settings is given for these entries.
434 	 */
435 	for (; index < table.n_entries; index++)
436 		intel_uncore_write(uncore,
437 				   GEN12_GLOBAL_MOCS(index),
438 				   table.table[0].control_value);
439 }
440 
441 static int emit_mocs_control_table(struct i915_request *rq,
442 				   const struct drm_i915_mocs_table *table)
443 {
444 	enum intel_engine_id engine = rq->engine->id;
445 	unsigned int index;
446 	u32 unused_value;
447 	u32 *cs;
448 
449 	if (GEM_WARN_ON(table->size > table->n_entries))
450 		return -ENODEV;
451 
452 	/* Set unused values to PTE */
453 	unused_value = table->table[I915_MOCS_PTE].control_value;
454 
455 	cs = intel_ring_begin(rq, 2 + 2 * table->n_entries);
456 	if (IS_ERR(cs))
457 		return PTR_ERR(cs);
458 
459 	*cs++ = MI_LOAD_REGISTER_IMM(table->n_entries);
460 
461 	for (index = 0; index < table->size; index++) {
462 		u32 value = get_entry_control(table, index);
463 
464 		*cs++ = i915_mmio_reg_offset(mocs_register(engine, index));
465 		*cs++ = value;
466 	}
467 
468 	/* All remaining entries are also unused */
469 	for (; index < table->n_entries; index++) {
470 		*cs++ = i915_mmio_reg_offset(mocs_register(engine, index));
471 		*cs++ = unused_value;
472 	}
473 
474 	*cs++ = MI_NOOP;
475 	intel_ring_advance(rq, cs);
476 
477 	return 0;
478 }
479 
480 /*
481  * Get l3cc_value from MOCS entry taking into account when it's not used:
482  * I915_MOCS_PTE's value is returned in this case.
483  */
484 static u16 get_entry_l3cc(const struct drm_i915_mocs_table *table,
485 			  unsigned int index)
486 {
487 	if (table->table[index].used)
488 		return table->table[index].l3cc_value;
489 
490 	return table->table[I915_MOCS_PTE].l3cc_value;
491 }
492 
493 static inline u32 l3cc_combine(const struct drm_i915_mocs_table *table,
494 			       u16 low,
495 			       u16 high)
496 {
497 	return low | high << 16;
498 }
499 
500 static int emit_mocs_l3cc_table(struct i915_request *rq,
501 				const struct drm_i915_mocs_table *table)
502 {
503 	u16 unused_value;
504 	unsigned int i;
505 	u32 *cs;
506 
507 	if (GEM_WARN_ON(table->size > table->n_entries))
508 		return -ENODEV;
509 
510 	/* Set unused values to PTE */
511 	unused_value = table->table[I915_MOCS_PTE].l3cc_value;
512 
513 	cs = intel_ring_begin(rq, 2 + table->n_entries);
514 	if (IS_ERR(cs))
515 		return PTR_ERR(cs);
516 
517 	*cs++ = MI_LOAD_REGISTER_IMM(table->n_entries / 2);
518 
519 	for (i = 0; i < table->size / 2; i++) {
520 		u16 low = get_entry_l3cc(table, 2 * i);
521 		u16 high = get_entry_l3cc(table, 2 * i + 1);
522 
523 		*cs++ = i915_mmio_reg_offset(GEN9_LNCFCMOCS(i));
524 		*cs++ = l3cc_combine(table, low, high);
525 	}
526 
527 	/* Odd table size - 1 left over */
528 	if (table->size & 0x01) {
529 		u16 low = get_entry_l3cc(table, 2 * i);
530 
531 		*cs++ = i915_mmio_reg_offset(GEN9_LNCFCMOCS(i));
532 		*cs++ = l3cc_combine(table, low, unused_value);
533 		i++;
534 	}
535 
536 	/* All remaining entries are also unused */
537 	for (; i < table->n_entries / 2; i++) {
538 		*cs++ = i915_mmio_reg_offset(GEN9_LNCFCMOCS(i));
539 		*cs++ = l3cc_combine(table, unused_value, unused_value);
540 	}
541 
542 	*cs++ = MI_NOOP;
543 	intel_ring_advance(rq, cs);
544 
545 	return 0;
546 }
547 
548 static void intel_mocs_init_l3cc_table(struct intel_gt *gt)
549 {
550 	struct intel_uncore *uncore = gt->uncore;
551 	struct drm_i915_mocs_table table;
552 	unsigned int i;
553 	u16 unused_value;
554 
555 	if (!get_mocs_settings(gt, &table))
556 		return;
557 
558 	/* Set unused values to PTE */
559 	unused_value = table.table[I915_MOCS_PTE].l3cc_value;
560 
561 	for (i = 0; i < table.size / 2; i++) {
562 		u16 low = get_entry_l3cc(&table, 2 * i);
563 		u16 high = get_entry_l3cc(&table, 2 * i + 1);
564 
565 		intel_uncore_write(uncore,
566 				   GEN9_LNCFCMOCS(i),
567 				   l3cc_combine(&table, low, high));
568 	}
569 
570 	/* Odd table size - 1 left over */
571 	if (table.size & 0x01) {
572 		u16 low = get_entry_l3cc(&table, 2 * i);
573 
574 		intel_uncore_write(uncore,
575 				   GEN9_LNCFCMOCS(i),
576 				   l3cc_combine(&table, low, unused_value));
577 		i++;
578 	}
579 
580 	/* All remaining entries are also unused */
581 	for (; i < table.n_entries / 2; i++)
582 		intel_uncore_write(uncore,
583 				   GEN9_LNCFCMOCS(i),
584 				   l3cc_combine(&table, unused_value,
585 						unused_value));
586 }
587 
588 /**
589  * intel_mocs_emit() - program the MOCS register.
590  * @rq:	Request to use to set up the MOCS tables.
591  *
592  * This function will emit a batch buffer with the values required for
593  * programming the MOCS register values for all the currently supported
594  * rings.
595  *
596  * These registers are partially stored in the RCS context, so they are
597  * emitted at the same time so that when a context is created these registers
598  * are set up. These registers have to be emitted into the start of the
599  * context as setting the ELSP will re-init some of these registers back
600  * to the hw values.
601  *
602  * Return: 0 on success, otherwise the error status.
603  */
604 int intel_mocs_emit(struct i915_request *rq)
605 {
606 	struct drm_i915_mocs_table t;
607 	int ret;
608 
609 	if (HAS_GLOBAL_MOCS_REGISTERS(rq->i915) ||
610 	    rq->engine->class != RENDER_CLASS)
611 		return 0;
612 
613 	if (get_mocs_settings(rq->engine->gt, &t)) {
614 		/* Program the RCS control registers */
615 		ret = emit_mocs_control_table(rq, &t);
616 		if (ret)
617 			return ret;
618 
619 		/* Now program the l3cc registers */
620 		ret = emit_mocs_l3cc_table(rq, &t);
621 		if (ret)
622 			return ret;
623 	}
624 
625 	return 0;
626 }
627 
628 void intel_mocs_init(struct intel_gt *gt)
629 {
630 	intel_mocs_init_l3cc_table(gt);
631 
632 	if (HAS_GLOBAL_MOCS_REGISTERS(gt->i915))
633 		intel_mocs_init_global(gt);
634 }
635