Home | History | Annotate | Line # | Download | only in gt
      1  1.1  riastrad /*	$NetBSD: intel_mocs.c,v 1.2 2021/12/18 23:45:30 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright (c) 2015 Intel Corporation
      5  1.1  riastrad  *
      6  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      7  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
      8  1.1  riastrad  * to deal in the Software without restriction, including without limitation
      9  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     11  1.1  riastrad  * Software is furnished to do so, subject to the following conditions: *
     12  1.1  riastrad  * The above copyright notice and this permission notice (including the next
     13  1.1  riastrad  * paragraph) shall be included in all copies or substantial portions of the
     14  1.1  riastrad  * Software.
     15  1.1  riastrad  *
     16  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     19  1.1  riastrad  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     20  1.1  riastrad  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     21  1.1  riastrad  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     22  1.1  riastrad  * SOFTWARE.
     23  1.1  riastrad  */
     24  1.1  riastrad 
     25  1.1  riastrad #include <sys/cdefs.h>
     26  1.1  riastrad __KERNEL_RCSID(0, "$NetBSD: intel_mocs.c,v 1.2 2021/12/18 23:45:30 riastradh Exp $");
     27  1.1  riastrad 
     28  1.1  riastrad #include "i915_drv.h"
     29  1.1  riastrad 
     30  1.1  riastrad #include "intel_engine.h"
     31  1.1  riastrad #include "intel_gt.h"
     32  1.1  riastrad #include "intel_mocs.h"
     33  1.1  riastrad #include "intel_lrc.h"
     34  1.1  riastrad #include "intel_ring.h"
     35  1.1  riastrad 
     36  1.1  riastrad /* structures required */
     37  1.1  riastrad struct drm_i915_mocs_entry {
     38  1.1  riastrad 	u32 control_value;
     39  1.1  riastrad 	u16 l3cc_value;
     40  1.1  riastrad 	u16 used;
     41  1.1  riastrad };
     42  1.1  riastrad 
     43  1.1  riastrad struct drm_i915_mocs_table {
     44  1.1  riastrad 	unsigned int size;
     45  1.1  riastrad 	unsigned int n_entries;
     46  1.1  riastrad 	const struct drm_i915_mocs_entry *table;
     47  1.1  riastrad };
     48  1.1  riastrad 
     49  1.1  riastrad /* Defines for the tables (XXX_MOCS_0 - XXX_MOCS_63) */
     50  1.1  riastrad #define _LE_CACHEABILITY(value)	((value) << 0)
     51  1.1  riastrad #define _LE_TGT_CACHE(value)	((value) << 2)
     52  1.1  riastrad #define LE_LRUM(value)		((value) << 4)
     53  1.1  riastrad #define LE_AOM(value)		((value) << 6)
     54  1.1  riastrad #define LE_RSC(value)		((value) << 7)
     55  1.1  riastrad #define LE_SCC(value)		((value) << 8)
     56  1.1  riastrad #define LE_PFM(value)		((value) << 11)
     57  1.1  riastrad #define LE_SCF(value)		((value) << 14)
     58  1.1  riastrad #define LE_COS(value)		((value) << 15)
     59  1.1  riastrad #define LE_SSE(value)		((value) << 17)
     60  1.1  riastrad 
     61  1.1  riastrad /* Defines for the tables (LNCFMOCS0 - LNCFMOCS31) - two entries per word */
     62  1.1  riastrad #define L3_ESC(value)		((value) << 0)
     63  1.1  riastrad #define L3_SCC(value)		((value) << 1)
     64  1.1  riastrad #define _L3_CACHEABILITY(value)	((value) << 4)
     65  1.1  riastrad 
     66  1.1  riastrad /* Helper defines */
     67  1.1  riastrad #define GEN9_NUM_MOCS_ENTRIES	62  /* 62 out of 64 - 63 & 64 are reserved. */
     68  1.1  riastrad #define GEN11_NUM_MOCS_ENTRIES	64  /* 63-64 are reserved, but configured. */
     69  1.1  riastrad 
     70  1.1  riastrad /* (e)LLC caching options */
     71  1.1  riastrad /*
     72  1.1  riastrad  * Note: LE_0_PAGETABLE works only up to Gen11; for newer gens it means
     73  1.1  riastrad  * the same as LE_UC
     74  1.1  riastrad  */
     75  1.1  riastrad #define LE_0_PAGETABLE		_LE_CACHEABILITY(0)
     76  1.1  riastrad #define LE_1_UC			_LE_CACHEABILITY(1)
     77  1.1  riastrad #define LE_2_WT			_LE_CACHEABILITY(2)
     78  1.1  riastrad #define LE_3_WB			_LE_CACHEABILITY(3)
     79  1.1  riastrad 
     80  1.1  riastrad /* Target cache */
     81  1.1  riastrad #define LE_TC_0_PAGETABLE	_LE_TGT_CACHE(0)
     82  1.1  riastrad #define LE_TC_1_LLC		_LE_TGT_CACHE(1)
     83  1.1  riastrad #define LE_TC_2_LLC_ELLC	_LE_TGT_CACHE(2)
     84  1.1  riastrad #define LE_TC_3_LLC_ELLC_ALT	_LE_TGT_CACHE(3)
     85  1.1  riastrad 
     86  1.1  riastrad /* L3 caching options */
     87  1.1  riastrad #define L3_0_DIRECT		_L3_CACHEABILITY(0)
     88  1.1  riastrad #define L3_1_UC			_L3_CACHEABILITY(1)
     89  1.1  riastrad #define L3_2_RESERVED		_L3_CACHEABILITY(2)
     90  1.1  riastrad #define L3_3_WB			_L3_CACHEABILITY(3)
     91  1.1  riastrad 
     92  1.1  riastrad #define MOCS_ENTRY(__idx, __control_value, __l3cc_value) \
     93  1.1  riastrad 	[__idx] = { \
     94  1.1  riastrad 		.control_value = __control_value, \
     95  1.1  riastrad 		.l3cc_value = __l3cc_value, \
     96  1.1  riastrad 		.used = 1, \
     97  1.1  riastrad 	}
     98  1.1  riastrad 
     99  1.1  riastrad /*
    100  1.1  riastrad  * MOCS tables
    101  1.1  riastrad  *
    102  1.1  riastrad  * These are the MOCS tables that are programmed across all the rings.
    103  1.1  riastrad  * The control value is programmed to all the rings that support the
    104  1.1  riastrad  * MOCS registers. While the l3cc_values are only programmed to the
    105  1.1  riastrad  * LNCFCMOCS0 - LNCFCMOCS32 registers.
    106  1.1  riastrad  *
    107  1.1  riastrad  * These tables are intended to be kept reasonably consistent across
    108  1.1  riastrad  * HW platforms, and for ICL+, be identical across OSes. To achieve
    109  1.1  riastrad  * that, for Icelake and above, list of entries is published as part
    110  1.1  riastrad  * of bspec.
    111  1.1  riastrad  *
    112  1.1  riastrad  * Entries not part of the following tables are undefined as far as
    113  1.1  riastrad  * userspace is concerned and shouldn't be relied upon.  For Gen < 12
    114  1.1  riastrad  * they will be initialized to PTE. Gen >= 12 onwards don't have a setting for
    115  1.1  riastrad  * PTE and will be initialized to an invalid value.
    116  1.1  riastrad  *
    117  1.1  riastrad  * The last two entries are reserved by the hardware. For ICL+ they
    118  1.1  riastrad  * should be initialized according to bspec and never used, for older
    119  1.1  riastrad  * platforms they should never be written to.
    120  1.1  riastrad  *
    121  1.1  riastrad  * NOTE: These tables are part of bspec and defined as part of hardware
    122  1.1  riastrad  *       interface for ICL+. For older platforms, they are part of kernel
    123  1.1  riastrad  *       ABI. It is expected that, for specific hardware platform, existing
    124  1.1  riastrad  *       entries will remain constant and the table will only be updated by
    125  1.1  riastrad  *       adding new entries, filling unused positions.
    126  1.1  riastrad  */
    127  1.1  riastrad #define GEN9_MOCS_ENTRIES \
    128  1.1  riastrad 	MOCS_ENTRY(I915_MOCS_UNCACHED, \
    129  1.1  riastrad 		   LE_1_UC | LE_TC_2_LLC_ELLC, \
    130  1.1  riastrad 		   L3_1_UC), \
    131  1.1  riastrad 	MOCS_ENTRY(I915_MOCS_PTE, \
    132  1.1  riastrad 		   LE_0_PAGETABLE | LE_TC_2_LLC_ELLC | LE_LRUM(3), \
    133  1.1  riastrad 		   L3_3_WB)
    134  1.1  riastrad 
    135  1.1  riastrad static const struct drm_i915_mocs_entry skl_mocs_table[] = {
    136  1.1  riastrad 	GEN9_MOCS_ENTRIES,
    137  1.1  riastrad 	MOCS_ENTRY(I915_MOCS_CACHED,
    138  1.1  riastrad 		   LE_3_WB | LE_TC_2_LLC_ELLC | LE_LRUM(3),
    139  1.1  riastrad 		   L3_3_WB)
    140  1.1  riastrad };
    141  1.1  riastrad 
    142  1.1  riastrad /* NOTE: the LE_TGT_CACHE is not used on Broxton */
    143  1.1  riastrad static const struct drm_i915_mocs_entry broxton_mocs_table[] = {
    144  1.1  riastrad 	GEN9_MOCS_ENTRIES,
    145  1.1  riastrad 	MOCS_ENTRY(I915_MOCS_CACHED,
    146  1.1  riastrad 		   LE_1_UC | LE_TC_2_LLC_ELLC | LE_LRUM(3),
    147  1.1  riastrad 		   L3_3_WB)
    148  1.1  riastrad };
    149  1.1  riastrad 
    150  1.1  riastrad #define GEN11_MOCS_ENTRIES \
    151  1.1  riastrad 	/* Entries 0 and 1 are defined per-platform */ \
    152  1.1  riastrad 	/* Base - L3 + LLC */ \
    153  1.1  riastrad 	MOCS_ENTRY(2, \
    154  1.1  riastrad 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3), \
    155  1.1  riastrad 		   L3_3_WB), \
    156  1.1  riastrad 	/* Base - Uncached */ \
    157  1.1  riastrad 	MOCS_ENTRY(3, \
    158  1.1  riastrad 		   LE_1_UC | LE_TC_1_LLC, \
    159  1.1  riastrad 		   L3_1_UC), \
    160  1.1  riastrad 	/* Base - L3 */ \
    161  1.1  riastrad 	MOCS_ENTRY(4, \
    162  1.1  riastrad 		   LE_1_UC | LE_TC_1_LLC, \
    163  1.1  riastrad 		   L3_3_WB), \
    164  1.1  riastrad 	/* Base - LLC */ \
    165  1.1  riastrad 	MOCS_ENTRY(5, \
    166  1.1  riastrad 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3), \
    167  1.1  riastrad 		   L3_1_UC), \
    168  1.1  riastrad 	/* Age 0 - LLC */ \
    169  1.1  riastrad 	MOCS_ENTRY(6, \
    170  1.1  riastrad 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(1), \
    171  1.1  riastrad 		   L3_1_UC), \
    172  1.1  riastrad 	/* Age 0 - L3 + LLC */ \
    173  1.1  riastrad 	MOCS_ENTRY(7, \
    174  1.1  riastrad 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(1), \
    175  1.1  riastrad 		   L3_3_WB), \
    176  1.1  riastrad 	/* Age: Don't Chg. - LLC */ \
    177  1.1  riastrad 	MOCS_ENTRY(8, \
    178  1.1  riastrad 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(2), \
    179  1.1  riastrad 		   L3_1_UC), \
    180  1.1  riastrad 	/* Age: Don't Chg. - L3 + LLC */ \
    181  1.1  riastrad 	MOCS_ENTRY(9, \
    182  1.1  riastrad 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(2), \
    183  1.1  riastrad 		   L3_3_WB), \
    184  1.1  riastrad 	/* No AOM - LLC */ \
    185  1.1  riastrad 	MOCS_ENTRY(10, \
    186  1.1  riastrad 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_AOM(1), \
    187  1.1  riastrad 		   L3_1_UC), \
    188  1.1  riastrad 	/* No AOM - L3 + LLC */ \
    189  1.1  riastrad 	MOCS_ENTRY(11, \
    190  1.1  riastrad 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_AOM(1), \
    191  1.1  riastrad 		   L3_3_WB), \
    192  1.1  riastrad 	/* No AOM; Age 0 - LLC */ \
    193  1.1  riastrad 	MOCS_ENTRY(12, \
    194  1.1  riastrad 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(1) | LE_AOM(1), \
    195  1.1  riastrad 		   L3_1_UC), \
    196  1.1  riastrad 	/* No AOM; Age 0 - L3 + LLC */ \
    197  1.1  riastrad 	MOCS_ENTRY(13, \
    198  1.1  riastrad 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(1) | LE_AOM(1), \
    199  1.1  riastrad 		   L3_3_WB), \
    200  1.1  riastrad 	/* No AOM; Age:DC - LLC */ \
    201  1.1  riastrad 	MOCS_ENTRY(14, \
    202  1.1  riastrad 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(2) | LE_AOM(1), \
    203  1.1  riastrad 		   L3_1_UC), \
    204  1.1  riastrad 	/* No AOM; Age:DC - L3 + LLC */ \
    205  1.1  riastrad 	MOCS_ENTRY(15, \
    206  1.1  riastrad 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(2) | LE_AOM(1), \
    207  1.1  riastrad 		   L3_3_WB), \
    208  1.1  riastrad 	/* Self-Snoop - L3 + LLC */ \
    209  1.1  riastrad 	MOCS_ENTRY(18, \
    210  1.1  riastrad 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_SSE(3), \
    211  1.1  riastrad 		   L3_3_WB), \
    212  1.1  riastrad 	/* Skip Caching - L3 + LLC(12.5%) */ \
    213  1.1  riastrad 	MOCS_ENTRY(19, \
    214  1.1  riastrad 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_SCC(7), \
    215  1.1  riastrad 		   L3_3_WB), \
    216  1.1  riastrad 	/* Skip Caching - L3 + LLC(25%) */ \
    217  1.1  riastrad 	MOCS_ENTRY(20, \
    218  1.1  riastrad 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_SCC(3), \
    219  1.1  riastrad 		   L3_3_WB), \
    220  1.1  riastrad 	/* Skip Caching - L3 + LLC(50%) */ \
    221  1.1  riastrad 	MOCS_ENTRY(21, \
    222  1.1  riastrad 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_SCC(1), \
    223  1.1  riastrad 		   L3_3_WB), \
    224  1.1  riastrad 	/* Skip Caching - L3 + LLC(75%) */ \
    225  1.1  riastrad 	MOCS_ENTRY(22, \
    226  1.1  riastrad 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_RSC(1) | LE_SCC(3), \
    227  1.1  riastrad 		   L3_3_WB), \
    228  1.1  riastrad 	/* Skip Caching - L3 + LLC(87.5%) */ \
    229  1.1  riastrad 	MOCS_ENTRY(23, \
    230  1.1  riastrad 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_RSC(1) | LE_SCC(7), \
    231  1.1  riastrad 		   L3_3_WB), \
    232  1.1  riastrad 	/* HW Reserved - SW program but never use */ \
    233  1.1  riastrad 	MOCS_ENTRY(62, \
    234  1.1  riastrad 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3), \
    235  1.1  riastrad 		   L3_1_UC), \
    236  1.1  riastrad 	/* HW Reserved - SW program but never use */ \
    237  1.1  riastrad 	MOCS_ENTRY(63, \
    238  1.1  riastrad 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3), \
    239  1.1  riastrad 		   L3_1_UC)
    240  1.1  riastrad 
    241  1.1  riastrad static const struct drm_i915_mocs_entry tgl_mocs_table[] = {
    242  1.1  riastrad 	/* Base - Error (Reserved for Non-Use) */
    243  1.1  riastrad 	MOCS_ENTRY(0, 0x0, 0x0),
    244  1.1  riastrad 	/* Base - Reserved */
    245  1.1  riastrad 	MOCS_ENTRY(1, 0x0, 0x0),
    246  1.1  riastrad 
    247  1.1  riastrad 	GEN11_MOCS_ENTRIES,
    248  1.1  riastrad 
    249  1.1  riastrad 	/* Implicitly enable L1 - HDC:L1 + L3 + LLC */
    250  1.1  riastrad 	MOCS_ENTRY(48,
    251  1.1  riastrad 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3),
    252  1.1  riastrad 		   L3_3_WB),
    253  1.1  riastrad 	/* Implicitly enable L1 - HDC:L1 + L3 */
    254  1.1  riastrad 	MOCS_ENTRY(49,
    255  1.1  riastrad 		   LE_1_UC | LE_TC_1_LLC,
    256  1.1  riastrad 		   L3_3_WB),
    257  1.1  riastrad 	/* Implicitly enable L1 - HDC:L1 + LLC */
    258  1.1  riastrad 	MOCS_ENTRY(50,
    259  1.1  riastrad 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3),
    260  1.1  riastrad 		   L3_1_UC),
    261  1.1  riastrad 	/* Implicitly enable L1 - HDC:L1 */
    262  1.1  riastrad 	MOCS_ENTRY(51,
    263  1.1  riastrad 		   LE_1_UC | LE_TC_1_LLC,
    264  1.1  riastrad 		   L3_1_UC),
    265  1.1  riastrad 	/* HW Special Case (CCS) */
    266  1.1  riastrad 	MOCS_ENTRY(60,
    267  1.1  riastrad 		   LE_3_WB | LE_TC_1_LLC | LE_LRUM(3),
    268  1.1  riastrad 		   L3_1_UC),
    269  1.1  riastrad 	/* HW Special Case (Displayable) */
    270  1.1  riastrad 	MOCS_ENTRY(61,
    271  1.1  riastrad 		   LE_1_UC | LE_TC_1_LLC,
    272  1.1  riastrad 		   L3_3_WB),
    273  1.1  riastrad };
    274  1.1  riastrad 
    275  1.1  riastrad static const struct drm_i915_mocs_entry icl_mocs_table[] = {
    276  1.1  riastrad 	/* Base - Uncached (Deprecated) */
    277  1.1  riastrad 	MOCS_ENTRY(I915_MOCS_UNCACHED,
    278  1.1  riastrad 		   LE_1_UC | LE_TC_1_LLC,
    279  1.1  riastrad 		   L3_1_UC),
    280  1.1  riastrad 	/* Base - L3 + LeCC:PAT (Deprecated) */
    281  1.1  riastrad 	MOCS_ENTRY(I915_MOCS_PTE,
    282  1.1  riastrad 		   LE_0_PAGETABLE | LE_TC_1_LLC,
    283  1.1  riastrad 		   L3_3_WB),
    284  1.1  riastrad 
    285  1.1  riastrad 	GEN11_MOCS_ENTRIES
    286  1.1  riastrad };
    287  1.1  riastrad 
    288  1.1  riastrad static bool get_mocs_settings(const struct drm_i915_private *i915,
    289  1.1  riastrad 			      struct drm_i915_mocs_table *table)
    290  1.1  riastrad {
    291  1.1  riastrad 	if (INTEL_GEN(i915) >= 12) {
    292  1.1  riastrad 		table->size  = ARRAY_SIZE(tgl_mocs_table);
    293  1.1  riastrad 		table->table = tgl_mocs_table;
    294  1.1  riastrad 		table->n_entries = GEN11_NUM_MOCS_ENTRIES;
    295  1.1  riastrad 	} else if (IS_GEN(i915, 11)) {
    296  1.1  riastrad 		table->size  = ARRAY_SIZE(icl_mocs_table);
    297  1.1  riastrad 		table->table = icl_mocs_table;
    298  1.1  riastrad 		table->n_entries = GEN11_NUM_MOCS_ENTRIES;
    299  1.1  riastrad 	} else if (IS_GEN9_BC(i915) || IS_CANNONLAKE(i915)) {
    300  1.1  riastrad 		table->size  = ARRAY_SIZE(skl_mocs_table);
    301  1.1  riastrad 		table->n_entries = GEN9_NUM_MOCS_ENTRIES;
    302  1.1  riastrad 		table->table = skl_mocs_table;
    303  1.1  riastrad 	} else if (IS_GEN9_LP(i915)) {
    304  1.1  riastrad 		table->size  = ARRAY_SIZE(broxton_mocs_table);
    305  1.1  riastrad 		table->n_entries = GEN9_NUM_MOCS_ENTRIES;
    306  1.1  riastrad 		table->table = broxton_mocs_table;
    307  1.1  riastrad 	} else {
    308  1.1  riastrad 		WARN_ONCE(INTEL_GEN(i915) >= 9,
    309  1.1  riastrad 			  "Platform that should have a MOCS table does not.\n");
    310  1.1  riastrad 		return false;
    311  1.1  riastrad 	}
    312  1.1  riastrad 
    313  1.1  riastrad 	if (GEM_DEBUG_WARN_ON(table->size > table->n_entries))
    314  1.1  riastrad 		return false;
    315  1.1  riastrad 
    316  1.1  riastrad 	/* WaDisableSkipCaching:skl,bxt,kbl,glk */
    317  1.1  riastrad 	if (IS_GEN(i915, 9)) {
    318  1.1  riastrad 		int i;
    319  1.1  riastrad 
    320  1.1  riastrad 		for (i = 0; i < table->size; i++)
    321  1.1  riastrad 			if (GEM_DEBUG_WARN_ON(table->table[i].l3cc_value &
    322  1.1  riastrad 					      (L3_ESC(1) | L3_SCC(0x7))))
    323  1.1  riastrad 				return false;
    324  1.1  riastrad 	}
    325  1.1  riastrad 
    326  1.1  riastrad 	return true;
    327  1.1  riastrad }
    328  1.1  riastrad 
    329  1.1  riastrad /*
    330  1.1  riastrad  * Get control_value from MOCS entry taking into account when it's not used:
    331  1.1  riastrad  * I915_MOCS_PTE's value is returned in this case.
    332  1.1  riastrad  */
    333  1.1  riastrad static u32 get_entry_control(const struct drm_i915_mocs_table *table,
    334  1.1  riastrad 			     unsigned int index)
    335  1.1  riastrad {
    336  1.1  riastrad 	if (index < table->size && table->table[index].used)
    337  1.1  riastrad 		return table->table[index].control_value;
    338  1.1  riastrad 
    339  1.1  riastrad 	return table->table[I915_MOCS_PTE].control_value;
    340  1.1  riastrad }
    341  1.1  riastrad 
    342  1.1  riastrad #define for_each_mocs(mocs, t, i) \
    343  1.1  riastrad 	for (i = 0; \
    344  1.1  riastrad 	     i < (t)->n_entries ? (mocs = get_entry_control((t), i)), 1 : 0;\
    345  1.1  riastrad 	     i++)
    346  1.1  riastrad 
    347  1.1  riastrad static void __init_mocs_table(struct intel_uncore *uncore,
    348  1.1  riastrad 			      const struct drm_i915_mocs_table *table,
    349  1.1  riastrad 			      u32 addr)
    350  1.1  riastrad {
    351  1.1  riastrad 	unsigned int i;
    352  1.1  riastrad 	u32 mocs;
    353  1.1  riastrad 
    354  1.1  riastrad 	for_each_mocs(mocs, table, i)
    355  1.1  riastrad 		intel_uncore_write_fw(uncore, _MMIO(addr + i * 4), mocs);
    356  1.1  riastrad }
    357  1.1  riastrad 
    358  1.1  riastrad static u32 mocs_offset(const struct intel_engine_cs *engine)
    359  1.1  riastrad {
    360  1.1  riastrad 	static const u32 offset[] = {
    361  1.1  riastrad 		[RCS0]  =  __GEN9_RCS0_MOCS0,
    362  1.1  riastrad 		[VCS0]  =  __GEN9_VCS0_MOCS0,
    363  1.1  riastrad 		[VCS1]  =  __GEN9_VCS1_MOCS0,
    364  1.1  riastrad 		[VECS0] =  __GEN9_VECS0_MOCS0,
    365  1.1  riastrad 		[BCS0]  =  __GEN9_BCS0_MOCS0,
    366  1.1  riastrad 		[VCS2]  = __GEN11_VCS2_MOCS0,
    367  1.1  riastrad 	};
    368  1.1  riastrad 
    369  1.1  riastrad 	GEM_BUG_ON(engine->id >= ARRAY_SIZE(offset));
    370  1.1  riastrad 	return offset[engine->id];
    371  1.1  riastrad }
    372  1.1  riastrad 
    373  1.1  riastrad static void init_mocs_table(struct intel_engine_cs *engine,
    374  1.1  riastrad 			    const struct drm_i915_mocs_table *table)
    375  1.1  riastrad {
    376  1.1  riastrad 	__init_mocs_table(engine->uncore, table, mocs_offset(engine));
    377  1.1  riastrad }
    378  1.1  riastrad 
    379  1.1  riastrad /*
    380  1.1  riastrad  * Get l3cc_value from MOCS entry taking into account when it's not used:
    381  1.1  riastrad  * I915_MOCS_PTE's value is returned in this case.
    382  1.1  riastrad  */
    383  1.1  riastrad static u16 get_entry_l3cc(const struct drm_i915_mocs_table *table,
    384  1.1  riastrad 			  unsigned int index)
    385  1.1  riastrad {
    386  1.1  riastrad 	if (index < table->size && table->table[index].used)
    387  1.1  riastrad 		return table->table[index].l3cc_value;
    388  1.1  riastrad 
    389  1.1  riastrad 	return table->table[I915_MOCS_PTE].l3cc_value;
    390  1.1  riastrad }
    391  1.1  riastrad 
    392  1.1  riastrad static inline u32 l3cc_combine(u16 low, u16 high)
    393  1.1  riastrad {
    394  1.1  riastrad 	return low | (u32)high << 16;
    395  1.1  riastrad }
    396  1.1  riastrad 
    397  1.1  riastrad #define for_each_l3cc(l3cc, t, i) \
    398  1.1  riastrad 	for (i = 0; \
    399  1.1  riastrad 	     i < ((t)->n_entries + 1) / 2 ? \
    400  1.1  riastrad 	     (l3cc = l3cc_combine(get_entry_l3cc((t), 2 * i), \
    401  1.1  riastrad 				  get_entry_l3cc((t), 2 * i + 1))), 1 : \
    402  1.1  riastrad 	     0; \
    403  1.1  riastrad 	     i++)
    404  1.1  riastrad 
    405  1.1  riastrad static void init_l3cc_table(struct intel_engine_cs *engine,
    406  1.1  riastrad 			    const struct drm_i915_mocs_table *table)
    407  1.1  riastrad {
    408  1.1  riastrad 	struct intel_uncore *uncore = engine->uncore;
    409  1.1  riastrad 	unsigned int i;
    410  1.1  riastrad 	u32 l3cc;
    411  1.1  riastrad 
    412  1.1  riastrad 	for_each_l3cc(l3cc, table, i)
    413  1.1  riastrad 		intel_uncore_write_fw(uncore, GEN9_LNCFCMOCS(i), l3cc);
    414  1.1  riastrad }
    415  1.1  riastrad 
    416  1.1  riastrad void intel_mocs_init_engine(struct intel_engine_cs *engine)
    417  1.1  riastrad {
    418  1.1  riastrad 	struct drm_i915_mocs_table table;
    419  1.1  riastrad 
    420  1.1  riastrad 	/* Called under a blanket forcewake */
    421  1.1  riastrad 	assert_forcewakes_active(engine->uncore, FORCEWAKE_ALL);
    422  1.1  riastrad 
    423  1.1  riastrad 	if (!get_mocs_settings(engine->i915, &table))
    424  1.1  riastrad 		return;
    425  1.1  riastrad 
    426  1.1  riastrad 	/* Platforms with global MOCS do not need per-engine initialization. */
    427  1.1  riastrad 	if (!HAS_GLOBAL_MOCS_REGISTERS(engine->i915))
    428  1.1  riastrad 		init_mocs_table(engine, &table);
    429  1.1  riastrad 
    430  1.1  riastrad 	if (engine->class == RENDER_CLASS)
    431  1.1  riastrad 		init_l3cc_table(engine, &table);
    432  1.1  riastrad }
    433  1.1  riastrad 
    434  1.1  riastrad static u32 global_mocs_offset(void)
    435  1.1  riastrad {
    436  1.1  riastrad 	return i915_mmio_reg_offset(GEN12_GLOBAL_MOCS(0));
    437  1.1  riastrad }
    438  1.1  riastrad 
    439  1.1  riastrad static void init_global_mocs(struct intel_gt *gt)
    440  1.1  riastrad {
    441  1.1  riastrad 	struct drm_i915_mocs_table table;
    442  1.1  riastrad 
    443  1.1  riastrad 	/*
    444  1.1  riastrad 	 * LLC and eDRAM control values are not applicable to dgfx
    445  1.1  riastrad 	 */
    446  1.1  riastrad 	if (IS_DGFX(gt->i915))
    447  1.1  riastrad 		return;
    448  1.1  riastrad 
    449  1.1  riastrad 	if (!get_mocs_settings(gt->i915, &table))
    450  1.1  riastrad 		return;
    451  1.1  riastrad 
    452  1.1  riastrad 	__init_mocs_table(gt->uncore, &table, global_mocs_offset());
    453  1.1  riastrad }
    454  1.1  riastrad 
    455  1.1  riastrad void intel_mocs_init(struct intel_gt *gt)
    456  1.1  riastrad {
    457  1.1  riastrad 	if (HAS_GLOBAL_MOCS_REGISTERS(gt->i915))
    458  1.1  riastrad 		init_global_mocs(gt);
    459  1.1  riastrad }
    460  1.1  riastrad 
    461  1.1  riastrad #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
    462  1.1  riastrad #include "selftest_mocs.c"
    463  1.1  riastrad #endif
    464