File Index
Files and directories
Sign in
FILE

xe_mocs.c

File preview with syntax highlighting.

linux-6.19.14/drivers/gpu/drm/xe/xe_mocs.c C
  1// SPDX-License-Identifier: MIT
  2/*
  3 * Copyright © 2022 Intel Corporation
  4 */
  5
  6#include "xe_mocs.h"
  7
  8#include "regs/xe_gt_regs.h"
  9#include "xe_bo.h"
 10#include "xe_device.h"
 11#include "xe_exec_queue.h"
 12#include "xe_force_wake.h"
 13#include "xe_gt.h"
 14#include "xe_gt_mcr.h"
 15#include "xe_gt_printk.h"
 16#include "xe_mmio.h"
 17#include "xe_platform_types.h"
 18#include "xe_pm.h"
 19#include "xe_sriov.h"
 20#include "xe_step_types.h"
 21
 22#if IS_ENABLED(CONFIG_DRM_XE_DEBUG)
 23#define mocs_dbg xe_gt_dbg
 24#else
 25__printf(2, 3)
 26static inline void mocs_dbg(const struct xe_gt *gt,
 27			    const char *format, ...)
 28{ /* noop */ }
 29#endif
 30
 31enum {
 32	HAS_GLOBAL_MOCS = BIT(0),
 33	HAS_LNCF_MOCS = BIT(1),
 34};
 35
 36struct xe_mocs_entry {
 37	u32 control_value;
 38	u16 l3cc_value;
 39	u16 used;
 40};
 41
 42struct xe_mocs_info;
 43
 44struct xe_mocs_ops {
 45	void (*dump)(struct xe_mocs_info *mocs, unsigned int flags,
 46		     struct xe_gt *gt, struct drm_printer *p);
 47};
 48
 49struct xe_mocs_info {
 50	/*
 51	 * Size of the spec's suggested MOCS programming table.  The list of
 52	 * table entries from the spec can potentially be smaller than the
 53	 * number of hardware registers used to program the MOCS table; in such
 54	 * cases the registers for the remaining indices will be programmed to
 55	 * match unused_entries_index.
 56	 */
 57	unsigned int table_size;
 58	/* Number of MOCS entries supported by the hardware */
 59	unsigned int num_mocs_regs;
 60	const struct xe_mocs_entry *table;
 61	const struct xe_mocs_ops *ops;
 62	u8 uc_index;
 63	u8 wb_index;
 64	u8 unused_entries_index;
 65};
 66
 67/* Defines for the tables (GLOB_MOCS_0 - GLOB_MOCS_16) */
 68#define IG_PAT				REG_BIT(8)
 69#define L3_CACHE_POLICY_MASK		REG_GENMASK(5, 4)
 70#define L4_CACHE_POLICY_MASK		REG_GENMASK(3, 2)
 71
 72/* Helper defines */
 73#define XELP_NUM_MOCS_ENTRIES	64  /* 63-64 are reserved, but configured. */
 74#define PVC_NUM_MOCS_ENTRIES	3
 75#define MTL_NUM_MOCS_ENTRIES	16
 76#define XE2_NUM_MOCS_ENTRIES	16
 77
 78/* (e)LLC caching options */
 79/*
 80 * Note: LE_0_PAGETABLE works only up to Gen11; for newer gens it means
 81 * the same as LE_UC
 82 */
 83#define LE_0_PAGETABLE		LE_CACHEABILITY(0)
 84#define LE_1_UC			LE_CACHEABILITY(1)
 85#define LE_2_WT			LE_CACHEABILITY(2)
 86#define LE_3_WB			LE_CACHEABILITY(3)
 87
 88/* Target cache */
 89#define LE_TC_0_PAGETABLE	LE_TGT_CACHE(0)
 90#define LE_TC_1_LLC		LE_TGT_CACHE(1)
 91#define LE_TC_2_LLC_ELLC	LE_TGT_CACHE(2)
 92#define LE_TC_3_LLC_ELLC_ALT	LE_TGT_CACHE(3)
 93
 94/* L3 caching options */
 95#define L3_0_DIRECT		L3_CACHEABILITY(0)
 96#define L3_1_UC			L3_CACHEABILITY(1)
 97#define L3_2_RESERVED		L3_CACHEABILITY(2)
 98#define L3_3_WB			L3_CACHEABILITY(3)
 99
100/* L4 caching options */
101#define L4_0_WB                 REG_FIELD_PREP(L4_CACHE_POLICY_MASK, 0)
102#define L4_1_WT                 REG_FIELD_PREP(L4_CACHE_POLICY_MASK, 1)
103#define L4_3_UC                 REG_FIELD_PREP(L4_CACHE_POLICY_MASK, 3)
104
105#define XE2_L3_0_WB		REG_FIELD_PREP(L3_CACHE_POLICY_MASK, 0)
106/* XD: WB Transient Display */
107#define XE2_L3_1_XD		REG_FIELD_PREP(L3_CACHE_POLICY_MASK, 1)
108#define XE2_L3_3_UC		REG_FIELD_PREP(L3_CACHE_POLICY_MASK, 3)
109
110#define XE2_L3_CLOS_MASK	REG_GENMASK(7, 6)
111
112#define MOCS_ENTRY(__idx, __control_value, __l3cc_value) \
113	[__idx] = { \
114		.control_value = __control_value, \
115		.l3cc_value = __l3cc_value, \
116		.used = 1, \
117	}
118
119/*
120 * MOCS tables
121 *
122 * These are the MOCS tables that are programmed across all the rings.
123 * The control value is programmed to all the rings that support the
124 * MOCS registers. While the l3cc_values are only programmed to the
125 * LNCFCMOCS0 - LNCFCMOCS32 registers.
126 *
127 * These tables are intended to be kept reasonably consistent across
128 * HW platforms, and for ICL+, be identical across OSes. To achieve
129 * that, the list of entries is published as part of bspec.
130 *
131 * Entries not part of the following tables are undefined as far as userspace is
132 * concerned and shouldn't be relied upon. The last few entries are reserved by
133 * the hardware. They should be initialized according to bspec and never used.
134 *
135 * NOTE1: These tables are part of bspec and defined as part of the hardware
136 * interface. It is expected that, for specific hardware platform, existing
137 * entries will remain constant and the table will only be updated by adding new
138 * entries, filling unused positions.
139 *
140 * NOTE2: Reserved and unspecified MOCS indices have been set to L3 WB. These
141 * reserved entries should never be used. They may be changed to low performant
142 * variants with better coherency in the future if more entries are needed.
143 */
144
145static const struct xe_mocs_entry gen12_mocs_desc[] = {
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	/* Self-Snoop - L3 + LLC */
203	MOCS_ENTRY(18,
204		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_SSE(3),
205		   L3_3_WB),
206	/* Skip Caching - L3 + LLC(12.5%) */
207	MOCS_ENTRY(19,
208		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_SCC(7),
209		   L3_3_WB),
210	/* Skip Caching - L3 + LLC(25%) */
211	MOCS_ENTRY(20,
212		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_SCC(3),
213		   L3_3_WB),
214	/* Skip Caching - L3 + LLC(50%) */
215	MOCS_ENTRY(21,
216		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_SCC(1),
217		   L3_3_WB),
218	/* Skip Caching - L3 + LLC(75%) */
219	MOCS_ENTRY(22,
220		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_RSC(1) | LE_SCC(3),
221		   L3_3_WB),
222	/* Skip Caching - L3 + LLC(87.5%) */
223	MOCS_ENTRY(23,
224		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_RSC(1) | LE_SCC(7),
225		   L3_3_WB),
226	/* Implicitly enable L1 - HDC:L1 + L3 + LLC */
227	MOCS_ENTRY(48,
228		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3),
229		   L3_3_WB),
230	/* Implicitly enable L1 - HDC:L1 + L3 */
231	MOCS_ENTRY(49,
232		   LE_1_UC | LE_TC_1_LLC,
233		   L3_3_WB),
234	/* Implicitly enable L1 - HDC:L1 + LLC */
235	MOCS_ENTRY(50,
236		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3),
237		   L3_1_UC),
238	/* Implicitly enable L1 - HDC:L1 */
239	MOCS_ENTRY(51,
240		   LE_1_UC | LE_TC_1_LLC,
241		   L3_1_UC),
242	/* HW Special Case (CCS) */
243	MOCS_ENTRY(60,
244		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3),
245		   L3_1_UC),
246	/* HW Special Case (Displayable) */
247	MOCS_ENTRY(61,
248		   LE_1_UC | LE_TC_1_LLC,
249		   L3_3_WB),
250	/* HW Reserved - SW program but never use */
251	MOCS_ENTRY(62,
252		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3),
253		   L3_1_UC),
254	/* HW Reserved - SW program but never use */
255	MOCS_ENTRY(63,
256		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3),
257		   L3_1_UC)
258};
259
260static bool regs_are_mcr(struct xe_gt *gt)
261{
262	struct xe_device *xe = gt_to_xe(gt);
263
264	if (xe_gt_is_media_type(gt))
265		return MEDIA_VER(xe) >= 20;
266	else
267		return GRAPHICS_VERx100(xe) >= 1250;
268}
269
270static void xelp_lncf_dump(struct xe_mocs_info *info, struct xe_gt *gt, struct drm_printer *p)
271{
272	unsigned int i, j;
273	u32 reg_val;
274
275	drm_printf(p, "LNCFCMOCS[idx] = [ESC, SCC, L3CC] (value)\n\n");
276
277	for (i = 0, j = 0; i < (info->num_mocs_regs + 1) / 2; i++, j++) {
278		if (regs_are_mcr(gt))
279			reg_val = xe_gt_mcr_unicast_read_any(gt, XEHP_LNCFCMOCS(i));
280		else
281			reg_val = xe_mmio_read32(&gt->mmio, XELP_LNCFCMOCS(i));
282
283		drm_printf(p, "LNCFCMOCS[%2d] = [%u, %u, %u] (%#8x)\n",
284			   j++,
285			   !!(reg_val & L3_ESC_MASK),
286			   REG_FIELD_GET(L3_SCC_MASK, reg_val),
287			   REG_FIELD_GET(L3_CACHEABILITY_MASK, reg_val),
288			   reg_val);
289
290		drm_printf(p, "LNCFCMOCS[%2d] = [%u, %u, %u] (%#8x)\n",
291			   j,
292			   !!(reg_val & L3_UPPER_IDX_ESC_MASK),
293			   REG_FIELD_GET(L3_UPPER_IDX_SCC_MASK, reg_val),
294			   REG_FIELD_GET(L3_UPPER_IDX_CACHEABILITY_MASK, reg_val),
295			   reg_val);
296	}
297}
298
299static void xelp_mocs_dump(struct xe_mocs_info *info, unsigned int flags,
300			   struct xe_gt *gt, struct drm_printer *p)
301{
302	unsigned int i;
303	u32 reg_val;
304
305	if (flags & HAS_GLOBAL_MOCS) {
306		drm_printf(p, "Global mocs table configuration:\n");
307		drm_printf(p, "GLOB_MOCS[idx] = [LeCC, TC, LRUM, AOM, RSC, SCC, PFM, SCF, CoS, SSE] (value)\n\n");
308
309		for (i = 0; i < info->num_mocs_regs; i++) {
310			if (regs_are_mcr(gt))
311				reg_val = xe_gt_mcr_unicast_read_any(gt, XEHP_GLOBAL_MOCS(i));
312			else
313				reg_val = xe_mmio_read32(&gt->mmio, XELP_GLOBAL_MOCS(i));
314
315			drm_printf(p, "GLOB_MOCS[%2d] = [%u, %u, %u, %u, %u, %u, %u, %u, %u, %u ] (%#8x)\n",
316				   i,
317				   REG_FIELD_GET(LE_CACHEABILITY_MASK, reg_val),
318				   REG_FIELD_GET(LE_TGT_CACHE_MASK, reg_val),
319				   REG_FIELD_GET(LE_LRUM_MASK, reg_val),
320				   !!(reg_val & LE_AOM_MASK),
321				   !!(reg_val & LE_RSC_MASK),
322				   REG_FIELD_GET(LE_SCC_MASK, reg_val),
323				   REG_FIELD_GET(LE_PFM_MASK, reg_val),
324				   !!(reg_val & LE_SCF_MASK),
325				   REG_FIELD_GET(LE_COS_MASK, reg_val),
326				   REG_FIELD_GET(LE_SSE_MASK, reg_val),
327				   reg_val);
328		}
329	}
330
331	xelp_lncf_dump(info, gt, p);
332}
333
334static const struct xe_mocs_ops xelp_mocs_ops = {
335	.dump = xelp_mocs_dump,
336};
337
338static const struct xe_mocs_entry dg1_mocs_desc[] = {
339	/* UC */
340	MOCS_ENTRY(1, 0, L3_1_UC),
341	/* WB - L3 */
342	MOCS_ENTRY(5, 0, L3_3_WB),
343	/* WB - L3 50% */
344	MOCS_ENTRY(6, 0, L3_ESC(1) | L3_SCC(1) | L3_3_WB),
345	/* WB - L3 25% */
346	MOCS_ENTRY(7, 0, L3_ESC(1) | L3_SCC(3) | L3_3_WB),
347	/* WB - L3 12.5% */
348	MOCS_ENTRY(8, 0, L3_ESC(1) | L3_SCC(7) | L3_3_WB),
349
350	/* HDC:L1 + L3 */
351	MOCS_ENTRY(48, 0, L3_3_WB),
352	/* HDC:L1 */
353	MOCS_ENTRY(49, 0, L3_1_UC),
354
355	/* HW Reserved */
356	MOCS_ENTRY(60, 0, L3_1_UC),
357	MOCS_ENTRY(61, 0, L3_1_UC),
358	MOCS_ENTRY(62, 0, L3_1_UC),
359	MOCS_ENTRY(63, 0, L3_1_UC),
360};
361
362static const struct xe_mocs_entry dg2_mocs_desc[] = {
363	/* UC - Coherent; GO:L3 */
364	MOCS_ENTRY(0, 0, L3_1_UC | L3_LKUP(1)),
365	/* UC - Coherent; GO:Memory */
366	MOCS_ENTRY(1, 0, L3_1_UC | L3_GLBGO(1) | L3_LKUP(1)),
367	/* UC - Non-Coherent; GO:Memory */
368	MOCS_ENTRY(2, 0, L3_1_UC | L3_GLBGO(1)),
369
370	/* WB - LC */
371	MOCS_ENTRY(3, 0, L3_3_WB | L3_LKUP(1)),
372};
373
374static void xehp_lncf_dump(struct xe_mocs_info *info, unsigned int flags,
375			   struct xe_gt *gt, struct drm_printer *p)
376{
377	unsigned int i, j;
378	u32 reg_val;
379
380	drm_printf(p, "LNCFCMOCS[idx] = [UCL3LOOKUP, GLBGO, L3CC] (value)\n\n");
381
382	for (i = 0, j = 0; i < (info->num_mocs_regs + 1) / 2; i++, j++) {
383		if (regs_are_mcr(gt))
384			reg_val = xe_gt_mcr_unicast_read_any(gt, XEHP_LNCFCMOCS(i));
385		else
386			reg_val = xe_mmio_read32(&gt->mmio, XELP_LNCFCMOCS(i));
387
388		drm_printf(p, "LNCFCMOCS[%2d] = [%u, %u, %u] (%#8x)\n",
389			   j++,
390			   !!(reg_val & L3_LKUP_MASK),
391			   !!(reg_val & L3_GLBGO_MASK),
392			   REG_FIELD_GET(L3_CACHEABILITY_MASK, reg_val),
393			   reg_val);
394
395		drm_printf(p, "LNCFCMOCS[%2d] = [%u, %u, %u] (%#8x)\n",
396			   j,
397			   !!(reg_val & L3_UPPER_LKUP_MASK),
398			   !!(reg_val & L3_UPPER_GLBGO_MASK),
399			   REG_FIELD_GET(L3_UPPER_IDX_CACHEABILITY_MASK, reg_val),
400			   reg_val);
401	}
402}
403
404static const struct xe_mocs_ops xehp_mocs_ops = {
405	.dump = xehp_lncf_dump,
406};
407
408static const struct xe_mocs_entry pvc_mocs_desc[] = {
409	/* Error */
410	MOCS_ENTRY(0, 0, L3_3_WB),
411
412	/* UC */
413	MOCS_ENTRY(1, 0, L3_1_UC),
414
415	/* WB */
416	MOCS_ENTRY(2, 0, L3_3_WB),
417};
418
419static void pvc_mocs_dump(struct xe_mocs_info *info, unsigned int flags, struct xe_gt *gt,
420			  struct drm_printer *p)
421{
422	unsigned int i, j;
423	u32 reg_val;
424
425	drm_printf(p, "LNCFCMOCS[idx] = [ L3CC ] (value)\n\n");
426
427	for (i = 0, j = 0; i < (info->num_mocs_regs + 1) / 2; i++, j++) {
428		if (regs_are_mcr(gt))
429			reg_val = xe_gt_mcr_unicast_read_any(gt, XEHP_LNCFCMOCS(i));
430		else
431			reg_val = xe_mmio_read32(&gt->mmio, XELP_LNCFCMOCS(i));
432
433		drm_printf(p, "LNCFCMOCS[%2d] = [ %u ] (%#8x)\n",
434			   j++,
435			   REG_FIELD_GET(L3_CACHEABILITY_MASK, reg_val),
436			   reg_val);
437
438		drm_printf(p, "LNCFCMOCS[%2d] = [ %u ] (%#8x)\n",
439			   j,
440			   REG_FIELD_GET(L3_UPPER_IDX_CACHEABILITY_MASK, reg_val),
441			   reg_val);
442	}
443}
444
445static const struct xe_mocs_ops pvc_mocs_ops = {
446	.dump = pvc_mocs_dump,
447};
448
449static const struct xe_mocs_entry mtl_mocs_desc[] = {
450	/* Error - Reserved for Non-Use */
451	MOCS_ENTRY(0,
452		   0,
453		   L3_LKUP(1) | L3_3_WB),
454	/* Cached - L3 + L4 */
455	MOCS_ENTRY(1,
456		   IG_PAT,
457		   L3_LKUP(1) | L3_3_WB),
458	/* L4 - GO:L3 */
459	MOCS_ENTRY(2,
460		   IG_PAT,
461		   L3_LKUP(1) | L3_1_UC),
462	/* Uncached - GO:L3 */
463	MOCS_ENTRY(3,
464		   IG_PAT | L4_3_UC,
465		   L3_LKUP(1) | L3_1_UC),
466	/* L4 - GO:Mem */
467	MOCS_ENTRY(4,
468		   IG_PAT,
469		   L3_LKUP(1) | L3_GLBGO(1) | L3_1_UC),
470	/* Uncached - GO:Mem */
471	MOCS_ENTRY(5,
472		   IG_PAT | L4_3_UC,
473		   L3_LKUP(1) | L3_GLBGO(1) | L3_1_UC),
474	/* L4 - L3:NoLKUP; GO:L3 */
475	MOCS_ENTRY(6,
476		   IG_PAT,
477		   L3_1_UC),
478	/* Uncached - L3:NoLKUP; GO:L3 */
479	MOCS_ENTRY(7,
480		   IG_PAT | L4_3_UC,
481		   L3_1_UC),
482	/* L4 - L3:NoLKUP; GO:Mem */
483	MOCS_ENTRY(8,
484		   IG_PAT,
485		   L3_GLBGO(1) | L3_1_UC),
486	/* Uncached - L3:NoLKUP; GO:Mem */
487	MOCS_ENTRY(9,
488		   IG_PAT | L4_3_UC,
489		   L3_GLBGO(1) | L3_1_UC),
490	/* Display - L3; L4:WT */
491	MOCS_ENTRY(14,
492		   IG_PAT | L4_1_WT,
493		   L3_LKUP(1) | L3_3_WB),
494	/* CCS - Non-Displayable */
495	MOCS_ENTRY(15,
496		   IG_PAT,
497		   L3_GLBGO(1) | L3_1_UC),
498};
499
500static void mtl_mocs_dump(struct xe_mocs_info *info, unsigned int flags,
501			  struct xe_gt *gt, struct drm_printer *p)
502{
503	unsigned int i;
504	u32 reg_val;
505
506	drm_printf(p, "Global mocs table configuration:\n");
507	drm_printf(p, "GLOB_MOCS[idx] = [IG_PAT, L4_CACHE_POLICY] (value)\n\n");
508
509	for (i = 0; i < info->num_mocs_regs; i++) {
510		if (regs_are_mcr(gt))
511			reg_val = xe_gt_mcr_unicast_read_any(gt, XEHP_GLOBAL_MOCS(i));
512		else
513			reg_val = xe_mmio_read32(&gt->mmio, XELP_GLOBAL_MOCS(i));
514
515		drm_printf(p, "GLOB_MOCS[%2d] = [%u, %u]  (%#8x)\n",
516			   i,
517			   !!(reg_val & IG_PAT),
518			   REG_FIELD_GET(L4_CACHE_POLICY_MASK, reg_val),
519			   reg_val);
520	}
521
522	/* MTL lncf mocs table pattern is similar to that of xehp */
523	xehp_lncf_dump(info, flags, gt, p);
524}
525
526static const struct xe_mocs_ops mtl_mocs_ops = {
527	.dump = mtl_mocs_dump,
528};
529
530static const struct xe_mocs_entry xe2_mocs_table[] = {
531	/* Defer to PAT */
532	MOCS_ENTRY(0, XE2_L3_0_WB | L4_3_UC, 0),
533	/* Cached L3, Uncached L4 */
534	MOCS_ENTRY(1, IG_PAT | XE2_L3_0_WB | L4_3_UC, 0),
535	/* Uncached L3, Cached L4 */
536	MOCS_ENTRY(2, IG_PAT | XE2_L3_3_UC | L4_0_WB, 0),
537	/* Uncached L3 + L4 */
538	MOCS_ENTRY(3, IG_PAT | XE2_L3_3_UC | L4_3_UC, 0),
539	/* Cached L3 + L4 */
540	MOCS_ENTRY(4, IG_PAT | XE2_L3_0_WB | L4_0_WB, 0),
541};
542
543static void xe2_mocs_dump(struct xe_mocs_info *info, unsigned int flags,
544			  struct xe_gt *gt, struct drm_printer *p)
545{
546	unsigned int i;
547	u32 reg_val;
548
549	drm_printf(p, "Global mocs table configuration:\n");
550	drm_printf(p, "GLOB_MOCS[idx] = [IG_PAT, L3_CLOS, L3_CACHE_POLICY, L4_CACHE_POLICY] (value)\n\n");
551
552	for (i = 0; i < info->num_mocs_regs; i++) {
553		if (regs_are_mcr(gt))
554			reg_val = xe_gt_mcr_unicast_read_any(gt, XEHP_GLOBAL_MOCS(i));
555		else
556			reg_val = xe_mmio_read32(&gt->mmio, XELP_GLOBAL_MOCS(i));
557
558		drm_printf(p, "GLOB_MOCS[%2d] = [%u, %u, %u]  (%#8x)\n",
559			   i,
560			   !!(reg_val & IG_PAT),
561			   REG_FIELD_GET(XE2_L3_CLOS_MASK, reg_val),
562			   REG_FIELD_GET(L4_CACHE_POLICY_MASK, reg_val),
563			   reg_val);
564	}
565}
566
567static const struct xe_mocs_ops xe2_mocs_ops = {
568	.dump = xe2_mocs_dump,
569};
570
571/*
572 * Note that the "L3" and "L4" register fields actually control the L2 and L3
573 * caches respectively on this platform.
574 */
575static const struct xe_mocs_entry xe3p_xpc_mocs_table[] = {
576	/* Defer to PAT */
577	MOCS_ENTRY(0, XE2_L3_0_WB | L4_3_UC, 0),
578	/* UC */
579	MOCS_ENTRY(1, IG_PAT | XE2_L3_3_UC | L4_3_UC, 0),
580	/* L2 */
581	MOCS_ENTRY(2, IG_PAT | XE2_L3_0_WB | L4_3_UC, 0),
582	/* L3 */
583	MOCS_ENTRY(3, IG_PAT | XE2_L3_3_UC | L4_0_WB, 0),
584	/* L2 + L3 */
585	MOCS_ENTRY(4, IG_PAT | XE2_L3_0_WB | L4_0_WB, 0),
586};
587
588static unsigned int get_mocs_settings(struct xe_device *xe,
589				      struct xe_mocs_info *info)
590{
591	unsigned int flags = 0;
592
593	memset(info, 0, sizeof(struct xe_mocs_info));
594
595	switch (xe->info.platform) {
596	case XE_CRESCENTISLAND:
597		info->ops = &xe2_mocs_ops;
598		info->table_size = ARRAY_SIZE(xe3p_xpc_mocs_table);
599		info->table = xe3p_xpc_mocs_table;
600		info->num_mocs_regs = XE2_NUM_MOCS_ENTRIES;
601		info->uc_index = 1;
602		info->wb_index = 4;
603		info->unused_entries_index = 4;
604		break;
605	case XE_NOVALAKE_S:
606	case XE_PANTHERLAKE:
607	case XE_LUNARLAKE:
608	case XE_BATTLEMAGE:
609		info->ops = &xe2_mocs_ops;
610		info->table_size = ARRAY_SIZE(xe2_mocs_table);
611		info->table = xe2_mocs_table;
612		info->num_mocs_regs = XE2_NUM_MOCS_ENTRIES;
613		info->uc_index = 3;
614		info->wb_index = 4;
615		info->unused_entries_index = 4;
616		break;
617	case XE_PVC:
618		info->ops = &pvc_mocs_ops;
619		info->table_size = ARRAY_SIZE(pvc_mocs_desc);
620		info->table = pvc_mocs_desc;
621		info->num_mocs_regs = PVC_NUM_MOCS_ENTRIES;
622		info->uc_index = 1;
623		info->wb_index = 2;
624		info->unused_entries_index = 2;
625		break;
626	case XE_METEORLAKE:
627		info->ops = &mtl_mocs_ops;
628		info->table_size = ARRAY_SIZE(mtl_mocs_desc);
629		info->table = mtl_mocs_desc;
630		info->num_mocs_regs = MTL_NUM_MOCS_ENTRIES;
631		info->uc_index = 9;
632		info->unused_entries_index = 1;
633		break;
634	case XE_DG2:
635		info->ops = &xehp_mocs_ops;
636		info->table_size = ARRAY_SIZE(dg2_mocs_desc);
637		info->table = dg2_mocs_desc;
638		info->uc_index = 1;
639		/*
640		 * Last entry is RO on hardware, don't bother with what was
641		 * written when checking later
642		 */
643		info->num_mocs_regs = XELP_NUM_MOCS_ENTRIES - 1;
644		info->unused_entries_index = 3;
645		break;
646	case XE_DG1:
647		info->ops = &xelp_mocs_ops;
648		info->table_size = ARRAY_SIZE(dg1_mocs_desc);
649		info->table = dg1_mocs_desc;
650		info->uc_index = 1;
651		info->num_mocs_regs = XELP_NUM_MOCS_ENTRIES;
652		info->unused_entries_index = 5;
653		break;
654	case XE_TIGERLAKE:
655	case XE_ROCKETLAKE:
656	case XE_ALDERLAKE_S:
657	case XE_ALDERLAKE_P:
658	case XE_ALDERLAKE_N:
659		info->ops = &xelp_mocs_ops;
660		info->table_size  = ARRAY_SIZE(gen12_mocs_desc);
661		info->table = gen12_mocs_desc;
662		info->num_mocs_regs = XELP_NUM_MOCS_ENTRIES;
663		info->uc_index = 3;
664		info->unused_entries_index = 2;
665		break;
666	default:
667		drm_err(&xe->drm, "Platform that should have a MOCS table does not.\n");
668		return 0;
669	}
670
671	/*
672	 * Index 0 is a reserved/unused table entry on most platforms, but
673	 * even on those where it does represent a legitimate MOCS entry, it
674	 * never represents the "most cached, least coherent" behavior we want
675	 * to populate undefined table rows with.  So if unused_entries_index
676	 * is still 0 at this point, we'll assume that it was omitted by
677	 * mistake in the switch statement above.
678	 */
679	xe_assert(xe, info->unused_entries_index != 0);
680
681	xe_assert(xe, info->ops && info->ops->dump);
682	xe_assert(xe, info->table_size <= info->num_mocs_regs);
683
684	if (!IS_DGFX(xe) || GRAPHICS_VER(xe) >= 20)
685		flags |= HAS_GLOBAL_MOCS;
686	if (GRAPHICS_VER(xe) < 20)
687		flags |= HAS_LNCF_MOCS;
688
689	return flags;
690}
691
692/*
693 * Get control_value from MOCS entry.  If the table entry is not defined, the
694 * settings from unused_entries_index will be returned.
695 */
696static u32 get_entry_control(const struct xe_mocs_info *info,
697			     unsigned int index)
698{
699	if (index < info->table_size && info->table[index].used)
700		return info->table[index].control_value;
701	return info->table[info->unused_entries_index].control_value;
702}
703
704static void __init_mocs_table(struct xe_gt *gt,
705			      const struct xe_mocs_info *info)
706{
707	unsigned int i;
708	u32 mocs;
709
710	mocs_dbg(gt, "mocs entries: %d\n", info->num_mocs_regs);
711
712	for (i = 0; i < info->num_mocs_regs; i++) {
713		mocs = get_entry_control(info, i);
714
715		mocs_dbg(gt, "GLOB_MOCS[%d] 0x%x 0x%x\n", i,
716			 XELP_GLOBAL_MOCS(i).addr, mocs);
717
718		if (regs_are_mcr(gt))
719			xe_gt_mcr_multicast_write(gt, XEHP_GLOBAL_MOCS(i), mocs);
720		else
721			xe_mmio_write32(&gt->mmio, XELP_GLOBAL_MOCS(i), mocs);
722	}
723}
724
725/*
726 * Get l3cc_value from MOCS entry taking into account when it's not used
727 * then if unused_entries_index is not zero then its value will be returned
728 * otherwise I915_MOCS_PTE's value is returned in this case.
729 */
730static u16 get_entry_l3cc(const struct xe_mocs_info *info,
731			  unsigned int index)
732{
733	if (index < info->table_size && info->table[index].used)
734		return info->table[index].l3cc_value;
735	return info->table[info->unused_entries_index].l3cc_value;
736}
737
738static u32 l3cc_combine(u16 low, u16 high)
739{
740	return low | (u32)high << 16;
741}
742
743static void init_l3cc_table(struct xe_gt *gt,
744			    const struct xe_mocs_info *info)
745{
746	unsigned int i;
747	u32 l3cc;
748
749	mocs_dbg(gt, "l3cc entries: %d\n", info->num_mocs_regs);
750
751	for (i = 0; i < (info->num_mocs_regs + 1) / 2; i++) {
752		l3cc = l3cc_combine(get_entry_l3cc(info, 2 * i),
753				    get_entry_l3cc(info, 2 * i + 1));
754
755		mocs_dbg(gt, "LNCFCMOCS[%d] 0x%x 0x%x\n", i,
756			 XELP_LNCFCMOCS(i).addr, l3cc);
757
758		if (regs_are_mcr(gt))
759			xe_gt_mcr_multicast_write(gt, XEHP_LNCFCMOCS(i), l3cc);
760		else
761			xe_mmio_write32(&gt->mmio, XELP_LNCFCMOCS(i), l3cc);
762	}
763}
764
765void xe_mocs_init_early(struct xe_gt *gt)
766{
767	struct xe_mocs_info table;
768
769	get_mocs_settings(gt_to_xe(gt), &table);
770	gt->mocs.uc_index = table.uc_index;
771	gt->mocs.wb_index = table.wb_index;
772}
773
774void xe_mocs_init(struct xe_gt *gt)
775{
776	struct xe_mocs_info table;
777	unsigned int flags;
778
779	if (IS_SRIOV_VF(gt_to_xe(gt)))
780		return;
781
782	/*
783	 * MOCS settings are split between "GLOB_MOCS" and/or "LNCFCMOCS"
784	 * registers depending on platform.
785	 *
786	 * These registers should be programmed before GuC initialization
787	 * since their values will affect some of the memory transactions
788	 * performed by the GuC.
789	 */
790	flags = get_mocs_settings(gt_to_xe(gt), &table);
791	mocs_dbg(gt, "flag:0x%x\n", flags);
792
793	if (IS_SRIOV_VF(gt_to_xe(gt)))
794		return;
795
796	if (flags & HAS_GLOBAL_MOCS)
797		__init_mocs_table(gt, &table);
798	if (flags & HAS_LNCF_MOCS)
799		init_l3cc_table(gt, &table);
800}
801
802/**
803 * xe_mocs_dump() - Dump MOCS table.
804 * @gt: the &xe_gt with MOCS table
805 * @p: the &drm_printer to dump info to
806 *
807 * Return: 0 on success or a negative error code on failure.
808 */
809int xe_mocs_dump(struct xe_gt *gt, struct drm_printer *p)
810{
811	struct xe_device *xe = gt_to_xe(gt);
812	enum xe_force_wake_domains domain;
813	struct xe_mocs_info table;
814	unsigned int fw_ref, flags;
815	int err = 0;
816
817	flags = get_mocs_settings(xe, &table);
818
819	domain = flags & HAS_LNCF_MOCS ? XE_FORCEWAKE_ALL : XE_FW_GT;
820	xe_pm_runtime_get_noresume(xe);
821	fw_ref = xe_force_wake_get(gt_to_fw(gt), domain);
822
823	if (!xe_force_wake_ref_has_domain(fw_ref, domain)) {
824		err = -ETIMEDOUT;
825		goto err_fw;
826	}
827
828	table.ops->dump(&table, flags, gt, p);
829
830err_fw:
831	xe_force_wake_put(gt_to_fw(gt), fw_ref);
832	xe_pm_runtime_put(xe);
833	return err;
834}
835
836#if IS_ENABLED(CONFIG_DRM_XE_KUNIT_TEST)
837#include "tests/xe_mocs.c"
838#endif
~/ Read-only file preview.