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