PageRenderTime 56ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/gpu/drm/i915/intel_mocs.c

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