intel_mocs.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. // SPDX-License-Identifier: MIT
  2. /*
  3. * Copyright © 2015 Intel Corporation
  4. */
  5. #include <drm/drm_print.h>
  6. #include "i915_drv.h"
  7. #include "intel_engine.h"
  8. #include "intel_gt.h"
  9. #include "intel_gt_mcr.h"
  10. #include "intel_gt_regs.h"
  11. #include "intel_mocs.h"
  12. #include "intel_ring.h"
  13. /* structures required */
  14. struct drm_i915_mocs_entry {
  15. u32 control_value;
  16. u16 l3cc_value;
  17. u16 used;
  18. };
  19. struct drm_i915_mocs_table {
  20. unsigned int size;
  21. unsigned int n_entries;
  22. const struct drm_i915_mocs_entry *table;
  23. u8 uc_index;
  24. u8 wb_index; /* Only used on HAS_L3_CCS_READ() platforms */
  25. u8 unused_entries_index;
  26. };
  27. /* Defines for the tables (XXX_MOCS_0 - XXX_MOCS_63) */
  28. #define _LE_CACHEABILITY(value) ((value) << 0)
  29. #define _LE_TGT_CACHE(value) ((value) << 2)
  30. #define LE_LRUM(value) ((value) << 4)
  31. #define LE_AOM(value) ((value) << 6)
  32. #define LE_RSC(value) ((value) << 7)
  33. #define LE_SCC(value) ((value) << 8)
  34. #define LE_PFM(value) ((value) << 11)
  35. #define LE_SCF(value) ((value) << 14)
  36. #define LE_COS(value) ((value) << 15)
  37. #define LE_SSE(value) ((value) << 17)
  38. /* Defines for the tables (GLOB_MOCS_0 - GLOB_MOCS_16) */
  39. #define _L4_CACHEABILITY(value) ((value) << 2)
  40. #define IG_PAT(value) ((value) << 8)
  41. /* Defines for the tables (LNCFMOCS0 - LNCFMOCS31) - two entries per word */
  42. #define L3_ESC(value) ((value) << 0)
  43. #define L3_SCC(value) ((value) << 1)
  44. #define _L3_CACHEABILITY(value) ((value) << 4)
  45. #define L3_GLBGO(value) ((value) << 6)
  46. #define L3_LKUP(value) ((value) << 7)
  47. /* Helper defines */
  48. #define GEN9_NUM_MOCS_ENTRIES 64 /* 63-64 are reserved, but configured. */
  49. #define MTL_NUM_MOCS_ENTRIES 16
  50. /* (e)LLC caching options */
  51. /*
  52. * Note: LE_0_PAGETABLE works only up to Gen11; for newer gens it means
  53. * the same as LE_UC
  54. */
  55. #define LE_0_PAGETABLE _LE_CACHEABILITY(0)
  56. #define LE_1_UC _LE_CACHEABILITY(1)
  57. #define LE_2_WT _LE_CACHEABILITY(2)
  58. #define LE_3_WB _LE_CACHEABILITY(3)
  59. /* Target cache */
  60. #define LE_TC_0_PAGETABLE _LE_TGT_CACHE(0)
  61. #define LE_TC_1_LLC _LE_TGT_CACHE(1)
  62. #define LE_TC_2_LLC_ELLC _LE_TGT_CACHE(2)
  63. #define LE_TC_3_LLC_ELLC_ALT _LE_TGT_CACHE(3)
  64. /* L3 caching options */
  65. #define L3_0_DIRECT _L3_CACHEABILITY(0)
  66. #define L3_1_UC _L3_CACHEABILITY(1)
  67. #define L3_2_RESERVED _L3_CACHEABILITY(2)
  68. #define L3_3_WB _L3_CACHEABILITY(3)
  69. /* L4 caching options */
  70. #define L4_0_WB _L4_CACHEABILITY(0)
  71. #define L4_1_WT _L4_CACHEABILITY(1)
  72. #define L4_2_RESERVED _L4_CACHEABILITY(2)
  73. #define L4_3_UC _L4_CACHEABILITY(3)
  74. #define MOCS_ENTRY(__idx, __control_value, __l3cc_value) \
  75. [__idx] = { \
  76. .control_value = __control_value, \
  77. .l3cc_value = __l3cc_value, \
  78. .used = 1, \
  79. }
  80. /*
  81. * MOCS tables
  82. *
  83. * These are the MOCS tables that are programmed across all the rings.
  84. * The control value is programmed to all the rings that support the
  85. * MOCS registers. While the l3cc_values are only programmed to the
  86. * LNCFCMOCS0 - LNCFCMOCS32 registers.
  87. *
  88. * These tables are intended to be kept reasonably consistent across
  89. * HW platforms, and for ICL+, be identical across OSes. To achieve
  90. * that, for Icelake and above, list of entries is published as part
  91. * of bspec.
  92. *
  93. * Entries not part of the following tables are undefined as far as
  94. * userspace is concerned and shouldn't be relied upon. For Gen < 12
  95. * they will be initialized to PTE. Gen >= 12 don't have a setting for
  96. * PTE and those platforms except TGL/RKL will be initialized L3 WB to
  97. * catch accidental use of reserved and unused mocs indexes.
  98. *
  99. * The last few entries are reserved by the hardware. For ICL+ they
  100. * should be initialized according to bspec and never used, for older
  101. * platforms they should never be written to.
  102. *
  103. * NOTE1: These tables are part of bspec and defined as part of hardware
  104. * interface for ICL+. For older platforms, they are part of kernel
  105. * ABI. It is expected that, for specific hardware platform, existing
  106. * entries will remain constant and the table will only be updated by
  107. * adding new entries, filling unused positions.
  108. *
  109. * NOTE2: For GEN >= 12 except TGL and RKL, reserved and unspecified MOCS
  110. * indices have been set to L3 WB. These reserved entries should never
  111. * be used, they may be changed to low performant variants with better
  112. * coherency in the future if more entries are needed.
  113. * For TGL/RKL, all the unspecified MOCS indexes are mapped to L3 UC.
  114. */
  115. #define GEN9_MOCS_ENTRIES \
  116. MOCS_ENTRY(I915_MOCS_UNCACHED, \
  117. LE_1_UC | LE_TC_2_LLC_ELLC, \
  118. L3_1_UC), \
  119. MOCS_ENTRY(I915_MOCS_PTE, \
  120. LE_0_PAGETABLE | LE_TC_0_PAGETABLE | LE_LRUM(3), \
  121. L3_3_WB)
  122. static const struct drm_i915_mocs_entry skl_mocs_table[] = {
  123. GEN9_MOCS_ENTRIES,
  124. MOCS_ENTRY(I915_MOCS_CACHED,
  125. LE_3_WB | LE_TC_2_LLC_ELLC | LE_LRUM(3),
  126. L3_3_WB),
  127. /*
  128. * mocs:63
  129. * - used by the L3 for all of its evictions.
  130. * Thus it is expected to allow LLC cacheability to enable coherent
  131. * flows to be maintained.
  132. * - used to force L3 uncachable cycles.
  133. * Thus it is expected to make the surface L3 uncacheable.
  134. */
  135. MOCS_ENTRY(63,
  136. LE_3_WB | LE_TC_1_LLC | LE_LRUM(3),
  137. L3_1_UC)
  138. };
  139. /* NOTE: the LE_TGT_CACHE is not used on Broxton */
  140. static const struct drm_i915_mocs_entry broxton_mocs_table[] = {
  141. GEN9_MOCS_ENTRIES,
  142. MOCS_ENTRY(I915_MOCS_CACHED,
  143. LE_1_UC | LE_TC_2_LLC_ELLC | LE_LRUM(3),
  144. L3_3_WB)
  145. };
  146. #define GEN11_MOCS_ENTRIES \
  147. /* Entries 0 and 1 are defined per-platform */ \
  148. /* Base - L3 + LLC */ \
  149. MOCS_ENTRY(2, \
  150. LE_3_WB | LE_TC_1_LLC | LE_LRUM(3), \
  151. L3_3_WB), \
  152. /* Base - Uncached */ \
  153. MOCS_ENTRY(3, \
  154. LE_1_UC | LE_TC_1_LLC, \
  155. L3_1_UC), \
  156. /* Base - L3 */ \
  157. MOCS_ENTRY(4, \
  158. LE_1_UC | LE_TC_1_LLC, \
  159. L3_3_WB), \
  160. /* Base - LLC */ \
  161. MOCS_ENTRY(5, \
  162. LE_3_WB | LE_TC_1_LLC | LE_LRUM(3), \
  163. L3_1_UC), \
  164. /* Age 0 - LLC */ \
  165. MOCS_ENTRY(6, \
  166. LE_3_WB | LE_TC_1_LLC | LE_LRUM(1), \
  167. L3_1_UC), \
  168. /* Age 0 - L3 + LLC */ \
  169. MOCS_ENTRY(7, \
  170. LE_3_WB | LE_TC_1_LLC | LE_LRUM(1), \
  171. L3_3_WB), \
  172. /* Age: Don't Chg. - LLC */ \
  173. MOCS_ENTRY(8, \
  174. LE_3_WB | LE_TC_1_LLC | LE_LRUM(2), \
  175. L3_1_UC), \
  176. /* Age: Don't Chg. - L3 + LLC */ \
  177. MOCS_ENTRY(9, \
  178. LE_3_WB | LE_TC_1_LLC | LE_LRUM(2), \
  179. L3_3_WB), \
  180. /* No AOM - LLC */ \
  181. MOCS_ENTRY(10, \
  182. LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_AOM(1), \
  183. L3_1_UC), \
  184. /* No AOM - L3 + LLC */ \
  185. MOCS_ENTRY(11, \
  186. LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_AOM(1), \
  187. L3_3_WB), \
  188. /* No AOM; Age 0 - LLC */ \
  189. MOCS_ENTRY(12, \
  190. LE_3_WB | LE_TC_1_LLC | LE_LRUM(1) | LE_AOM(1), \
  191. L3_1_UC), \
  192. /* No AOM; Age 0 - L3 + LLC */ \
  193. MOCS_ENTRY(13, \
  194. LE_3_WB | LE_TC_1_LLC | LE_LRUM(1) | LE_AOM(1), \
  195. L3_3_WB), \
  196. /* No AOM; Age:DC - LLC */ \
  197. MOCS_ENTRY(14, \
  198. LE_3_WB | LE_TC_1_LLC | LE_LRUM(2) | LE_AOM(1), \
  199. L3_1_UC), \
  200. /* No AOM; Age:DC - L3 + LLC */ \
  201. MOCS_ENTRY(15, \
  202. LE_3_WB | LE_TC_1_LLC | LE_LRUM(2) | LE_AOM(1), \
  203. L3_3_WB), \
  204. /* Bypass LLC - Uncached (EHL+) */ \
  205. MOCS_ENTRY(16, \
  206. LE_1_UC | LE_TC_1_LLC | LE_SCF(1), \
  207. L3_1_UC), \
  208. /* Bypass LLC - L3 (Read-Only) (EHL+) */ \
  209. MOCS_ENTRY(17, \
  210. LE_1_UC | LE_TC_1_LLC | LE_SCF(1), \
  211. L3_3_WB), \
  212. /* Self-Snoop - L3 + LLC */ \
  213. MOCS_ENTRY(18, \
  214. LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_SSE(3), \
  215. L3_3_WB), \
  216. /* Skip Caching - L3 + LLC(12.5%) */ \
  217. MOCS_ENTRY(19, \
  218. LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_SCC(7), \
  219. L3_3_WB), \
  220. /* Skip Caching - L3 + LLC(25%) */ \
  221. MOCS_ENTRY(20, \
  222. LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_SCC(3), \
  223. L3_3_WB), \
  224. /* Skip Caching - L3 + LLC(50%) */ \
  225. MOCS_ENTRY(21, \
  226. LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_SCC(1), \
  227. L3_3_WB), \
  228. /* Skip Caching - L3 + LLC(75%) */ \
  229. MOCS_ENTRY(22, \
  230. LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_RSC(1) | LE_SCC(3), \
  231. L3_3_WB), \
  232. /* Skip Caching - L3 + LLC(87.5%) */ \
  233. MOCS_ENTRY(23, \
  234. LE_3_WB | LE_TC_1_LLC | LE_LRUM(3) | LE_RSC(1) | LE_SCC(7), \
  235. L3_3_WB), \
  236. /* HW Reserved - SW program but never use */ \
  237. MOCS_ENTRY(62, \
  238. LE_3_WB | LE_TC_1_LLC | LE_LRUM(3), \
  239. L3_1_UC), \
  240. /* HW Reserved - SW program but never use */ \
  241. MOCS_ENTRY(63, \
  242. LE_3_WB | LE_TC_1_LLC | LE_LRUM(3), \
  243. L3_1_UC)
  244. static const struct drm_i915_mocs_entry tgl_mocs_table[] = {
  245. /*
  246. * NOTE:
  247. * Reserved and unspecified MOCS indices have been set to (L3 + LCC).
  248. * These reserved entries should never be used, they may be changed
  249. * to low performant variants with better coherency in the future if
  250. * more entries are needed. We are programming index I915_MOCS_PTE(1)
  251. * only, __init_mocs_table() take care to program unused index with
  252. * this entry.
  253. */
  254. MOCS_ENTRY(I915_MOCS_PTE,
  255. LE_0_PAGETABLE | LE_TC_0_PAGETABLE,
  256. L3_1_UC),
  257. GEN11_MOCS_ENTRIES,
  258. /* Implicitly enable L1 - HDC:L1 + L3 + LLC */
  259. MOCS_ENTRY(48,
  260. LE_3_WB | LE_TC_1_LLC | LE_LRUM(3),
  261. L3_3_WB),
  262. /* Implicitly enable L1 - HDC:L1 + L3 */
  263. MOCS_ENTRY(49,
  264. LE_1_UC | LE_TC_1_LLC,
  265. L3_3_WB),
  266. /* Implicitly enable L1 - HDC:L1 + LLC */
  267. MOCS_ENTRY(50,
  268. LE_3_WB | LE_TC_1_LLC | LE_LRUM(3),
  269. L3_1_UC),
  270. /* Implicitly enable L1 - HDC:L1 */
  271. MOCS_ENTRY(51,
  272. LE_1_UC | LE_TC_1_LLC,
  273. L3_1_UC),
  274. /* HW Special Case (CCS) */
  275. MOCS_ENTRY(60,
  276. LE_3_WB | LE_TC_1_LLC | LE_LRUM(3),
  277. L3_1_UC),
  278. /* HW Special Case (Displayable) */
  279. MOCS_ENTRY(61,
  280. LE_1_UC | LE_TC_1_LLC,
  281. L3_3_WB),
  282. };
  283. static const struct drm_i915_mocs_entry icl_mocs_table[] = {
  284. /* Base - Uncached (Deprecated) */
  285. MOCS_ENTRY(I915_MOCS_UNCACHED,
  286. LE_1_UC | LE_TC_1_LLC,
  287. L3_1_UC),
  288. /* Base - L3 + LeCC:PAT (Deprecated) */
  289. MOCS_ENTRY(I915_MOCS_PTE,
  290. LE_0_PAGETABLE | LE_TC_0_PAGETABLE,
  291. L3_3_WB),
  292. GEN11_MOCS_ENTRIES
  293. };
  294. static const struct drm_i915_mocs_entry dg1_mocs_table[] = {
  295. /* UC */
  296. MOCS_ENTRY(1, 0, L3_1_UC),
  297. /* WB - L3 */
  298. MOCS_ENTRY(5, 0, L3_3_WB),
  299. /* WB - L3 50% */
  300. MOCS_ENTRY(6, 0, L3_ESC(1) | L3_SCC(1) | L3_3_WB),
  301. /* WB - L3 25% */
  302. MOCS_ENTRY(7, 0, L3_ESC(1) | L3_SCC(3) | L3_3_WB),
  303. /* WB - L3 12.5% */
  304. MOCS_ENTRY(8, 0, L3_ESC(1) | L3_SCC(7) | L3_3_WB),
  305. /* HDC:L1 + L3 */
  306. MOCS_ENTRY(48, 0, L3_3_WB),
  307. /* HDC:L1 */
  308. MOCS_ENTRY(49, 0, L3_1_UC),
  309. /* HW Reserved */
  310. MOCS_ENTRY(60, 0, L3_1_UC),
  311. MOCS_ENTRY(61, 0, L3_1_UC),
  312. MOCS_ENTRY(62, 0, L3_1_UC),
  313. MOCS_ENTRY(63, 0, L3_1_UC),
  314. };
  315. static const struct drm_i915_mocs_entry gen12_mocs_table[] = {
  316. GEN11_MOCS_ENTRIES,
  317. /* Implicitly enable L1 - HDC:L1 + L3 + LLC */
  318. MOCS_ENTRY(48,
  319. LE_3_WB | LE_TC_1_LLC | LE_LRUM(3),
  320. L3_3_WB),
  321. /* Implicitly enable L1 - HDC:L1 + L3 */
  322. MOCS_ENTRY(49,
  323. LE_1_UC | LE_TC_1_LLC,
  324. L3_3_WB),
  325. /* Implicitly enable L1 - HDC:L1 + LLC */
  326. MOCS_ENTRY(50,
  327. LE_3_WB | LE_TC_1_LLC | LE_LRUM(3),
  328. L3_1_UC),
  329. /* Implicitly enable L1 - HDC:L1 */
  330. MOCS_ENTRY(51,
  331. LE_1_UC | LE_TC_1_LLC,
  332. L3_1_UC),
  333. /* HW Special Case (CCS) */
  334. MOCS_ENTRY(60,
  335. LE_3_WB | LE_TC_1_LLC | LE_LRUM(3),
  336. L3_1_UC),
  337. /* HW Special Case (Displayable) */
  338. MOCS_ENTRY(61,
  339. LE_1_UC | LE_TC_1_LLC,
  340. L3_3_WB),
  341. };
  342. static const struct drm_i915_mocs_entry dg2_mocs_table[] = {
  343. /* UC - Coherent; GO:L3 */
  344. MOCS_ENTRY(0, 0, L3_1_UC | L3_LKUP(1)),
  345. /* UC - Coherent; GO:Memory */
  346. MOCS_ENTRY(1, 0, L3_1_UC | L3_GLBGO(1) | L3_LKUP(1)),
  347. /* UC - Non-Coherent; GO:Memory */
  348. MOCS_ENTRY(2, 0, L3_1_UC | L3_GLBGO(1)),
  349. /* WB - LC */
  350. MOCS_ENTRY(3, 0, L3_3_WB | L3_LKUP(1)),
  351. };
  352. static const struct drm_i915_mocs_entry mtl_mocs_table[] = {
  353. /* Error - Reserved for Non-Use */
  354. MOCS_ENTRY(0,
  355. IG_PAT(0),
  356. L3_LKUP(1) | L3_3_WB),
  357. /* Cached - L3 + L4 */
  358. MOCS_ENTRY(1,
  359. IG_PAT(1),
  360. L3_LKUP(1) | L3_3_WB),
  361. /* L4 - GO:L3 */
  362. MOCS_ENTRY(2,
  363. IG_PAT(1),
  364. L3_LKUP(1) | L3_1_UC),
  365. /* Uncached - GO:L3 */
  366. MOCS_ENTRY(3,
  367. IG_PAT(1) | L4_3_UC,
  368. L3_LKUP(1) | L3_1_UC),
  369. /* L4 - GO:Mem */
  370. MOCS_ENTRY(4,
  371. IG_PAT(1),
  372. L3_LKUP(1) | L3_GLBGO(1) | L3_1_UC),
  373. /* Uncached - GO:Mem */
  374. MOCS_ENTRY(5,
  375. IG_PAT(1) | L4_3_UC,
  376. L3_LKUP(1) | L3_GLBGO(1) | L3_1_UC),
  377. /* L4 - L3:NoLKUP; GO:L3 */
  378. MOCS_ENTRY(6,
  379. IG_PAT(1),
  380. L3_1_UC),
  381. /* Uncached - L3:NoLKUP; GO:L3 */
  382. MOCS_ENTRY(7,
  383. IG_PAT(1) | L4_3_UC,
  384. L3_1_UC),
  385. /* L4 - L3:NoLKUP; GO:Mem */
  386. MOCS_ENTRY(8,
  387. IG_PAT(1),
  388. L3_GLBGO(1) | L3_1_UC),
  389. /* Uncached - L3:NoLKUP; GO:Mem */
  390. MOCS_ENTRY(9,
  391. IG_PAT(1) | L4_3_UC,
  392. L3_GLBGO(1) | L3_1_UC),
  393. /* Display - L3; L4:WT */
  394. MOCS_ENTRY(14,
  395. IG_PAT(1) | L4_1_WT,
  396. L3_LKUP(1) | L3_3_WB),
  397. /* CCS - Non-Displayable */
  398. MOCS_ENTRY(15,
  399. IG_PAT(1),
  400. L3_GLBGO(1) | L3_1_UC),
  401. };
  402. enum {
  403. HAS_GLOBAL_MOCS = BIT(0),
  404. HAS_ENGINE_MOCS = BIT(1),
  405. HAS_RENDER_L3CC = BIT(2),
  406. };
  407. static bool has_l3cc(const struct drm_i915_private *i915)
  408. {
  409. return true;
  410. }
  411. static bool has_global_mocs(const struct drm_i915_private *i915)
  412. {
  413. return HAS_GLOBAL_MOCS_REGISTERS(i915);
  414. }
  415. static bool has_mocs(const struct drm_i915_private *i915)
  416. {
  417. return !IS_DGFX(i915);
  418. }
  419. static unsigned int get_mocs_settings(struct drm_i915_private *i915,
  420. struct drm_i915_mocs_table *table)
  421. {
  422. unsigned int flags;
  423. memset(table, 0, sizeof(struct drm_i915_mocs_table));
  424. table->unused_entries_index = I915_MOCS_PTE;
  425. if (IS_GFX_GT_IP_RANGE(to_gt(i915), IP_VER(12, 70), IP_VER(12, 74))) {
  426. table->size = ARRAY_SIZE(mtl_mocs_table);
  427. table->table = mtl_mocs_table;
  428. table->n_entries = MTL_NUM_MOCS_ENTRIES;
  429. table->uc_index = 9;
  430. table->unused_entries_index = 1;
  431. } else if (IS_DG2(i915)) {
  432. table->size = ARRAY_SIZE(dg2_mocs_table);
  433. table->table = dg2_mocs_table;
  434. table->uc_index = 1;
  435. table->n_entries = GEN9_NUM_MOCS_ENTRIES;
  436. table->unused_entries_index = 3;
  437. } else if (IS_DG1(i915)) {
  438. table->size = ARRAY_SIZE(dg1_mocs_table);
  439. table->table = dg1_mocs_table;
  440. table->uc_index = 1;
  441. table->n_entries = GEN9_NUM_MOCS_ENTRIES;
  442. table->uc_index = 1;
  443. table->unused_entries_index = 5;
  444. } else if (IS_TIGERLAKE(i915) || IS_ROCKETLAKE(i915)) {
  445. /* For TGL/RKL, Can't be changed now for ABI reasons */
  446. table->size = ARRAY_SIZE(tgl_mocs_table);
  447. table->table = tgl_mocs_table;
  448. table->n_entries = GEN9_NUM_MOCS_ENTRIES;
  449. table->uc_index = 3;
  450. } else if (GRAPHICS_VER(i915) >= 12) {
  451. table->size = ARRAY_SIZE(gen12_mocs_table);
  452. table->table = gen12_mocs_table;
  453. table->n_entries = GEN9_NUM_MOCS_ENTRIES;
  454. table->uc_index = 3;
  455. table->unused_entries_index = 2;
  456. } else if (GRAPHICS_VER(i915) == 11) {
  457. table->size = ARRAY_SIZE(icl_mocs_table);
  458. table->table = icl_mocs_table;
  459. table->n_entries = GEN9_NUM_MOCS_ENTRIES;
  460. } else if (IS_GEN9_BC(i915)) {
  461. table->size = ARRAY_SIZE(skl_mocs_table);
  462. table->n_entries = GEN9_NUM_MOCS_ENTRIES;
  463. table->table = skl_mocs_table;
  464. } else if (IS_GEN9_LP(i915)) {
  465. table->size = ARRAY_SIZE(broxton_mocs_table);
  466. table->n_entries = GEN9_NUM_MOCS_ENTRIES;
  467. table->table = broxton_mocs_table;
  468. } else {
  469. drm_WARN_ONCE(&i915->drm, GRAPHICS_VER(i915) >= 9,
  470. "Platform that should have a MOCS table does not.\n");
  471. return 0;
  472. }
  473. if (GEM_DEBUG_WARN_ON(table->size > table->n_entries))
  474. return 0;
  475. /* WaDisableSkipCaching:skl,bxt,kbl,glk */
  476. if (GRAPHICS_VER(i915) == 9) {
  477. int i;
  478. for (i = 0; i < table->size; i++)
  479. if (GEM_DEBUG_WARN_ON(table->table[i].l3cc_value &
  480. (L3_ESC(1) | L3_SCC(0x7))))
  481. return 0;
  482. }
  483. flags = 0;
  484. if (has_mocs(i915)) {
  485. if (has_global_mocs(i915))
  486. flags |= HAS_GLOBAL_MOCS;
  487. else
  488. flags |= HAS_ENGINE_MOCS;
  489. }
  490. if (has_l3cc(i915))
  491. flags |= HAS_RENDER_L3CC;
  492. return flags;
  493. }
  494. /*
  495. * Get control_value from MOCS entry taking into account when it's not used
  496. * then if unused_entries_index is non-zero then its value will be returned
  497. * otherwise I915_MOCS_PTE's value is returned in this case.
  498. */
  499. static u32 get_entry_control(const struct drm_i915_mocs_table *table,
  500. unsigned int index)
  501. {
  502. if (index < table->size && table->table[index].used)
  503. return table->table[index].control_value;
  504. return table->table[table->unused_entries_index].control_value;
  505. }
  506. #define for_each_mocs(mocs, t, i) \
  507. for (i = 0; \
  508. i < (t)->n_entries ? (mocs = get_entry_control((t), i)), 1 : 0;\
  509. i++)
  510. static void __init_mocs_table(struct intel_uncore *uncore,
  511. const struct drm_i915_mocs_table *table,
  512. u32 addr)
  513. {
  514. unsigned int i;
  515. u32 mocs;
  516. drm_WARN_ONCE(&uncore->i915->drm, !table->unused_entries_index,
  517. "Unused entries index should have been defined\n");
  518. for_each_mocs(mocs, table, i)
  519. intel_uncore_write_fw(uncore, _MMIO(addr + i * 4), mocs);
  520. }
  521. static u32 mocs_offset(const struct intel_engine_cs *engine)
  522. {
  523. static const u32 offset[] = {
  524. [RCS0] = __GEN9_RCS0_MOCS0,
  525. [VCS0] = __GEN9_VCS0_MOCS0,
  526. [VCS1] = __GEN9_VCS1_MOCS0,
  527. [VECS0] = __GEN9_VECS0_MOCS0,
  528. [BCS0] = __GEN9_BCS0_MOCS0,
  529. [VCS2] = __GEN11_VCS2_MOCS0,
  530. };
  531. GEM_BUG_ON(engine->id >= ARRAY_SIZE(offset));
  532. return offset[engine->id];
  533. }
  534. static void init_mocs_table(struct intel_engine_cs *engine,
  535. const struct drm_i915_mocs_table *table)
  536. {
  537. __init_mocs_table(engine->uncore, table, mocs_offset(engine));
  538. }
  539. /*
  540. * Get l3cc_value from MOCS entry taking into account when it's not used
  541. * then if unused_entries_index is not zero then its value will be returned
  542. * otherwise I915_MOCS_PTE's value is returned in this case.
  543. */
  544. static u16 get_entry_l3cc(const struct drm_i915_mocs_table *table,
  545. unsigned int index)
  546. {
  547. if (index < table->size && table->table[index].used)
  548. return table->table[index].l3cc_value;
  549. return table->table[table->unused_entries_index].l3cc_value;
  550. }
  551. static u32 l3cc_combine(u16 low, u16 high)
  552. {
  553. return low | (u32)high << 16;
  554. }
  555. #define for_each_l3cc(l3cc, t, i) \
  556. for (i = 0; \
  557. i < ((t)->n_entries + 1) / 2 ? \
  558. (l3cc = l3cc_combine(get_entry_l3cc((t), 2 * i), \
  559. get_entry_l3cc((t), 2 * i + 1))), 1 : \
  560. 0; \
  561. i++)
  562. static void init_l3cc_table(struct intel_gt *gt,
  563. const struct drm_i915_mocs_table *table)
  564. {
  565. unsigned long flags;
  566. unsigned int i;
  567. u32 l3cc;
  568. intel_gt_mcr_lock(gt, &flags);
  569. for_each_l3cc(l3cc, table, i)
  570. if (GRAPHICS_VER_FULL(gt->i915) >= IP_VER(12, 55))
  571. intel_gt_mcr_multicast_write_fw(gt, XEHP_LNCFCMOCS(i), l3cc);
  572. else
  573. intel_uncore_write_fw(gt->uncore, GEN9_LNCFCMOCS(i), l3cc);
  574. intel_gt_mcr_unlock(gt, flags);
  575. }
  576. void intel_mocs_init_engine(struct intel_engine_cs *engine)
  577. {
  578. struct drm_i915_mocs_table table;
  579. unsigned int flags;
  580. /* Called under a blanket forcewake */
  581. assert_forcewakes_active(engine->uncore, FORCEWAKE_ALL);
  582. flags = get_mocs_settings(engine->i915, &table);
  583. if (!flags)
  584. return;
  585. /* Platforms with global MOCS do not need per-engine initialization. */
  586. if (flags & HAS_ENGINE_MOCS)
  587. init_mocs_table(engine, &table);
  588. if (flags & HAS_RENDER_L3CC && engine->class == RENDER_CLASS)
  589. init_l3cc_table(engine->gt, &table);
  590. }
  591. static u32 global_mocs_offset(void)
  592. {
  593. return i915_mmio_reg_offset(GEN12_GLOBAL_MOCS(0));
  594. }
  595. void intel_set_mocs_index(struct intel_gt *gt)
  596. {
  597. struct drm_i915_mocs_table table;
  598. get_mocs_settings(gt->i915, &table);
  599. gt->mocs.uc_index = table.uc_index;
  600. if (HAS_L3_CCS_READ(gt->i915))
  601. gt->mocs.wb_index = table.wb_index;
  602. }
  603. void intel_mocs_init(struct intel_gt *gt)
  604. {
  605. struct drm_i915_mocs_table table;
  606. unsigned int flags;
  607. /*
  608. * LLC and eDRAM control values are not applicable to dgfx
  609. */
  610. flags = get_mocs_settings(gt->i915, &table);
  611. if (flags & HAS_GLOBAL_MOCS)
  612. __init_mocs_table(gt->uncore, &table, global_mocs_offset());
  613. /*
  614. * Initialize the L3CC table as part of mocs initialization to make
  615. * sure the LNCFCMOCSx registers are programmed for the subsequent
  616. * memory transactions including guc transactions
  617. */
  618. if (flags & HAS_RENDER_L3CC)
  619. init_l3cc_table(gt, &table);
  620. }
  621. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  622. #include "selftest_mocs.c"
  623. #endif