intel_mocs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. #include "intel_mocs.h"
  23. #include "intel_lrc.h"
  24. #include "intel_ringbuffer.h"
  25. /* structures required */
  26. struct drm_i915_mocs_entry {
  27. u32 control_value;
  28. u16 l3cc_value;
  29. };
  30. struct drm_i915_mocs_table {
  31. u32 size;
  32. const struct drm_i915_mocs_entry *table;
  33. };
  34. /* Defines for the tables (XXX_MOCS_0 - XXX_MOCS_63) */
  35. #define LE_CACHEABILITY(value) ((value) << 0)
  36. #define LE_TGT_CACHE(value) ((value) << 2)
  37. #define LE_LRUM(value) ((value) << 4)
  38. #define LE_AOM(value) ((value) << 6)
  39. #define LE_RSC(value) ((value) << 7)
  40. #define LE_SCC(value) ((value) << 8)
  41. #define LE_PFM(value) ((value) << 11)
  42. #define LE_SCF(value) ((value) << 14)
  43. /* Defines for the tables (LNCFMOCS0 - LNCFMOCS31) - two entries per word */
  44. #define L3_ESC(value) ((value) << 0)
  45. #define L3_SCC(value) ((value) << 1)
  46. #define L3_CACHEABILITY(value) ((value) << 4)
  47. /* Helper defines */
  48. #define GEN9_NUM_MOCS_ENTRIES 62 /* 62 out of 64 - 63 & 64 are reserved. */
  49. /* (e)LLC caching options */
  50. #define LE_PAGETABLE 0
  51. #define LE_UC 1
  52. #define LE_WT 2
  53. #define LE_WB 3
  54. /* L3 caching options */
  55. #define L3_DIRECT 0
  56. #define L3_UC 1
  57. #define L3_RESERVED 2
  58. #define L3_WB 3
  59. /* Target cache */
  60. #define LE_TC_PAGETABLE 0
  61. #define LE_TC_LLC 1
  62. #define LE_TC_LLC_ELLC 2
  63. #define LE_TC_LLC_ELLC_ALT 3
  64. /*
  65. * MOCS tables
  66. *
  67. * These are the MOCS tables that are programmed across all the rings.
  68. * The control value is programmed to all the rings that support the
  69. * MOCS registers. While the l3cc_values are only programmed to the
  70. * LNCFCMOCS0 - LNCFCMOCS32 registers.
  71. *
  72. * These tables are intended to be kept reasonably consistent across
  73. * platforms. However some of the fields are not applicable to all of
  74. * them.
  75. *
  76. * Entries not part of the following tables are undefined as far as
  77. * userspace is concerned and shouldn't be relied upon. For the time
  78. * being they will be implicitly initialized to the strictest caching
  79. * configuration (uncached) to guarantee forwards compatibility with
  80. * userspace programs written against more recent kernels providing
  81. * additional MOCS entries.
  82. *
  83. * NOTE: These tables MUST start with being uncached and the length
  84. * MUST be less than 63 as the last two registers are reserved
  85. * by the hardware. These tables are part of the kernel ABI and
  86. * may only be updated incrementally by adding entries at the
  87. * end.
  88. */
  89. static const struct drm_i915_mocs_entry skylake_mocs_table[] = {
  90. { /* 0x00000009 */
  91. .control_value = LE_CACHEABILITY(LE_UC) |
  92. LE_TGT_CACHE(LE_TC_LLC_ELLC) |
  93. LE_LRUM(0) | LE_AOM(0) | LE_RSC(0) | LE_SCC(0) |
  94. LE_PFM(0) | LE_SCF(0),
  95. /* 0x0010 */
  96. .l3cc_value = L3_ESC(0) | L3_SCC(0) | L3_CACHEABILITY(L3_UC),
  97. },
  98. {
  99. /* 0x00000038 */
  100. .control_value = LE_CACHEABILITY(LE_PAGETABLE) |
  101. LE_TGT_CACHE(LE_TC_LLC_ELLC) |
  102. LE_LRUM(3) | LE_AOM(0) | LE_RSC(0) | LE_SCC(0) |
  103. LE_PFM(0) | LE_SCF(0),
  104. /* 0x0030 */
  105. .l3cc_value = L3_ESC(0) | L3_SCC(0) | L3_CACHEABILITY(L3_WB),
  106. },
  107. {
  108. /* 0x0000003b */
  109. .control_value = LE_CACHEABILITY(LE_WB) |
  110. LE_TGT_CACHE(LE_TC_LLC_ELLC) |
  111. LE_LRUM(3) | LE_AOM(0) | LE_RSC(0) | LE_SCC(0) |
  112. LE_PFM(0) | LE_SCF(0),
  113. /* 0x0030 */
  114. .l3cc_value = L3_ESC(0) | L3_SCC(0) | L3_CACHEABILITY(L3_WB),
  115. },
  116. };
  117. /* NOTE: the LE_TGT_CACHE is not used on Broxton */
  118. static const struct drm_i915_mocs_entry broxton_mocs_table[] = {
  119. {
  120. /* 0x00000009 */
  121. .control_value = LE_CACHEABILITY(LE_UC) |
  122. LE_TGT_CACHE(LE_TC_LLC_ELLC) |
  123. LE_LRUM(0) | LE_AOM(0) | LE_RSC(0) | LE_SCC(0) |
  124. LE_PFM(0) | LE_SCF(0),
  125. /* 0x0010 */
  126. .l3cc_value = L3_ESC(0) | L3_SCC(0) | L3_CACHEABILITY(L3_UC),
  127. },
  128. {
  129. /* 0x00000038 */
  130. .control_value = LE_CACHEABILITY(LE_PAGETABLE) |
  131. LE_TGT_CACHE(LE_TC_LLC_ELLC) |
  132. LE_LRUM(3) | LE_AOM(0) | LE_RSC(0) | LE_SCC(0) |
  133. LE_PFM(0) | LE_SCF(0),
  134. /* 0x0030 */
  135. .l3cc_value = L3_ESC(0) | L3_SCC(0) | L3_CACHEABILITY(L3_WB),
  136. },
  137. {
  138. /* 0x00000039 */
  139. .control_value = LE_CACHEABILITY(LE_UC) |
  140. LE_TGT_CACHE(LE_TC_LLC_ELLC) |
  141. LE_LRUM(3) | LE_AOM(0) | LE_RSC(0) | LE_SCC(0) |
  142. LE_PFM(0) | LE_SCF(0),
  143. /* 0x0030 */
  144. .l3cc_value = L3_ESC(0) | L3_SCC(0) | L3_CACHEABILITY(L3_WB),
  145. },
  146. };
  147. /**
  148. * get_mocs_settings()
  149. * @dev_priv: i915 device.
  150. * @table: Output table that will be made to point at appropriate
  151. * MOCS values for the device.
  152. *
  153. * This function will return the values of the MOCS table that needs to
  154. * be programmed for the platform. It will return the values that need
  155. * to be programmed and if they need to be programmed.
  156. *
  157. * Return: true if there are applicable MOCS settings for the device.
  158. */
  159. static bool get_mocs_settings(struct drm_i915_private *dev_priv,
  160. struct drm_i915_mocs_table *table)
  161. {
  162. bool result = false;
  163. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
  164. table->size = ARRAY_SIZE(skylake_mocs_table);
  165. table->table = skylake_mocs_table;
  166. result = true;
  167. } else if (IS_BROXTON(dev_priv)) {
  168. table->size = ARRAY_SIZE(broxton_mocs_table);
  169. table->table = broxton_mocs_table;
  170. result = true;
  171. } else {
  172. WARN_ONCE(INTEL_INFO(dev_priv)->gen >= 9,
  173. "Platform that should have a MOCS table does not.\n");
  174. }
  175. /* WaDisableSkipCaching:skl,bxt,kbl */
  176. if (IS_GEN9(dev_priv)) {
  177. int i;
  178. for (i = 0; i < table->size; i++)
  179. if (WARN_ON(table->table[i].l3cc_value &
  180. (L3_ESC(1) | L3_SCC(0x7))))
  181. return false;
  182. }
  183. return result;
  184. }
  185. static i915_reg_t mocs_register(enum intel_engine_id ring, int index)
  186. {
  187. switch (ring) {
  188. case RCS:
  189. return GEN9_GFX_MOCS(index);
  190. case VCS:
  191. return GEN9_MFX0_MOCS(index);
  192. case BCS:
  193. return GEN9_BLT_MOCS(index);
  194. case VECS:
  195. return GEN9_VEBOX_MOCS(index);
  196. case VCS2:
  197. return GEN9_MFX1_MOCS(index);
  198. default:
  199. MISSING_CASE(ring);
  200. return INVALID_MMIO_REG;
  201. }
  202. }
  203. /**
  204. * intel_mocs_init_engine() - emit the mocs control table
  205. * @engine: The engine for whom to emit the registers.
  206. *
  207. * This function simply emits a MI_LOAD_REGISTER_IMM command for the
  208. * given table starting at the given address.
  209. *
  210. * Return: 0 on success, otherwise the error status.
  211. */
  212. int intel_mocs_init_engine(struct intel_engine_cs *engine)
  213. {
  214. struct drm_i915_private *dev_priv = engine->i915;
  215. struct drm_i915_mocs_table table;
  216. unsigned int index;
  217. if (!get_mocs_settings(dev_priv, &table))
  218. return 0;
  219. if (WARN_ON(table.size > GEN9_NUM_MOCS_ENTRIES))
  220. return -ENODEV;
  221. for (index = 0; index < table.size; index++)
  222. I915_WRITE(mocs_register(engine->id, index),
  223. table.table[index].control_value);
  224. /*
  225. * Ok, now set the unused entries to uncached. These entries
  226. * are officially undefined and no contract for the contents
  227. * and settings is given for these entries.
  228. *
  229. * Entry 0 in the table is uncached - so we are just writing
  230. * that value to all the used entries.
  231. */
  232. for (; index < GEN9_NUM_MOCS_ENTRIES; index++)
  233. I915_WRITE(mocs_register(engine->id, index),
  234. table.table[0].control_value);
  235. return 0;
  236. }
  237. /**
  238. * emit_mocs_control_table() - emit the mocs control table
  239. * @req: Request to set up the MOCS table for.
  240. * @table: The values to program into the control regs.
  241. *
  242. * This function simply emits a MI_LOAD_REGISTER_IMM command for the
  243. * given table starting at the given address.
  244. *
  245. * Return: 0 on success, otherwise the error status.
  246. */
  247. static int emit_mocs_control_table(struct drm_i915_gem_request *req,
  248. const struct drm_i915_mocs_table *table)
  249. {
  250. struct intel_ringbuffer *ringbuf = req->ringbuf;
  251. enum intel_engine_id engine = req->engine->id;
  252. unsigned int index;
  253. int ret;
  254. if (WARN_ON(table->size > GEN9_NUM_MOCS_ENTRIES))
  255. return -ENODEV;
  256. ret = intel_ring_begin(req, 2 + 2 * GEN9_NUM_MOCS_ENTRIES);
  257. if (ret)
  258. return ret;
  259. intel_logical_ring_emit(ringbuf,
  260. MI_LOAD_REGISTER_IMM(GEN9_NUM_MOCS_ENTRIES));
  261. for (index = 0; index < table->size; index++) {
  262. intel_logical_ring_emit_reg(ringbuf,
  263. mocs_register(engine, index));
  264. intel_logical_ring_emit(ringbuf,
  265. table->table[index].control_value);
  266. }
  267. /*
  268. * Ok, now set the unused entries to uncached. These entries
  269. * are officially undefined and no contract for the contents
  270. * and settings is given for these entries.
  271. *
  272. * Entry 0 in the table is uncached - so we are just writing
  273. * that value to all the used entries.
  274. */
  275. for (; index < GEN9_NUM_MOCS_ENTRIES; index++) {
  276. intel_logical_ring_emit_reg(ringbuf,
  277. mocs_register(engine, index));
  278. intel_logical_ring_emit(ringbuf,
  279. table->table[0].control_value);
  280. }
  281. intel_logical_ring_emit(ringbuf, MI_NOOP);
  282. intel_logical_ring_advance(ringbuf);
  283. return 0;
  284. }
  285. static inline u32 l3cc_combine(const struct drm_i915_mocs_table *table,
  286. u16 low,
  287. u16 high)
  288. {
  289. return table->table[low].l3cc_value |
  290. table->table[high].l3cc_value << 16;
  291. }
  292. /**
  293. * emit_mocs_l3cc_table() - emit the mocs control table
  294. * @req: Request to set up the MOCS table for.
  295. * @table: The values to program into the control regs.
  296. *
  297. * This function simply emits a MI_LOAD_REGISTER_IMM command for the
  298. * given table starting at the given address. This register set is
  299. * programmed in pairs.
  300. *
  301. * Return: 0 on success, otherwise the error status.
  302. */
  303. static int emit_mocs_l3cc_table(struct drm_i915_gem_request *req,
  304. const struct drm_i915_mocs_table *table)
  305. {
  306. struct intel_ringbuffer *ringbuf = req->ringbuf;
  307. unsigned int i;
  308. int ret;
  309. if (WARN_ON(table->size > GEN9_NUM_MOCS_ENTRIES))
  310. return -ENODEV;
  311. ret = intel_ring_begin(req, 2 + GEN9_NUM_MOCS_ENTRIES);
  312. if (ret)
  313. return ret;
  314. intel_logical_ring_emit(ringbuf,
  315. MI_LOAD_REGISTER_IMM(GEN9_NUM_MOCS_ENTRIES / 2));
  316. for (i = 0; i < table->size/2; i++) {
  317. intel_logical_ring_emit_reg(ringbuf, GEN9_LNCFCMOCS(i));
  318. intel_logical_ring_emit(ringbuf,
  319. l3cc_combine(table, 2*i, 2*i+1));
  320. }
  321. if (table->size & 0x01) {
  322. /* Odd table size - 1 left over */
  323. intel_logical_ring_emit_reg(ringbuf, GEN9_LNCFCMOCS(i));
  324. intel_logical_ring_emit(ringbuf, l3cc_combine(table, 2*i, 0));
  325. i++;
  326. }
  327. /*
  328. * Now set the rest of the table to uncached - use entry 0 as
  329. * this will be uncached. Leave the last pair uninitialised as
  330. * they are reserved by the hardware.
  331. */
  332. for (; i < GEN9_NUM_MOCS_ENTRIES / 2; i++) {
  333. intel_logical_ring_emit_reg(ringbuf, GEN9_LNCFCMOCS(i));
  334. intel_logical_ring_emit(ringbuf, l3cc_combine(table, 0, 0));
  335. }
  336. intel_logical_ring_emit(ringbuf, MI_NOOP);
  337. intel_logical_ring_advance(ringbuf);
  338. return 0;
  339. }
  340. /**
  341. * intel_mocs_init_l3cc_table() - program the mocs control table
  342. * @dev: The the device to be programmed.
  343. *
  344. * This function simply programs the mocs registers for the given table
  345. * starting at the given address. This register set is programmed in pairs.
  346. *
  347. * These registers may get programmed more than once, it is simpler to
  348. * re-program 32 registers than maintain the state of when they were programmed.
  349. * We are always reprogramming with the same values and this only on context
  350. * start.
  351. *
  352. * Return: Nothing.
  353. */
  354. void intel_mocs_init_l3cc_table(struct drm_device *dev)
  355. {
  356. struct drm_i915_private *dev_priv = to_i915(dev);
  357. struct drm_i915_mocs_table table;
  358. unsigned int i;
  359. if (!get_mocs_settings(dev_priv, &table))
  360. return;
  361. for (i = 0; i < table.size/2; i++)
  362. I915_WRITE(GEN9_LNCFCMOCS(i), l3cc_combine(&table, 2*i, 2*i+1));
  363. /* Odd table size - 1 left over */
  364. if (table.size & 0x01) {
  365. I915_WRITE(GEN9_LNCFCMOCS(i), l3cc_combine(&table, 2*i, 0));
  366. i++;
  367. }
  368. /*
  369. * Now set the rest of the table to uncached - use entry 0 as
  370. * this will be uncached. Leave the last pair as initialised as
  371. * they are reserved by the hardware.
  372. */
  373. for (; i < (GEN9_NUM_MOCS_ENTRIES / 2); i++)
  374. I915_WRITE(GEN9_LNCFCMOCS(i), l3cc_combine(&table, 0, 0));
  375. }
  376. /**
  377. * intel_rcs_context_init_mocs() - program the MOCS register.
  378. * @req: Request to set up the MOCS tables for.
  379. *
  380. * This function will emit a batch buffer with the values required for
  381. * programming the MOCS register values for all the currently supported
  382. * rings.
  383. *
  384. * These registers are partially stored in the RCS context, so they are
  385. * emitted at the same time so that when a context is created these registers
  386. * are set up. These registers have to be emitted into the start of the
  387. * context as setting the ELSP will re-init some of these registers back
  388. * to the hw values.
  389. *
  390. * Return: 0 on success, otherwise the error status.
  391. */
  392. int intel_rcs_context_init_mocs(struct drm_i915_gem_request *req)
  393. {
  394. struct drm_i915_mocs_table t;
  395. int ret;
  396. if (get_mocs_settings(req->i915, &t)) {
  397. /* Program the RCS control registers */
  398. ret = emit_mocs_control_table(req, &t);
  399. if (ret)
  400. return ret;
  401. /* Now program the l3cc registers */
  402. ret = emit_mocs_l3cc_table(req, &t);
  403. if (ret)
  404. return ret;
  405. }
  406. return 0;
  407. }