blob: 911fc08658c5facfb97fcf2ba7cd131547fad200 [file] [log] [blame]
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +01001/*
2 * Copyright © 2016 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 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 */
24
Chris Wilsonf636edb2017-10-09 12:02:57 +010025#include <drm/drm_print.h>
26
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +010027#include "i915_drv.h"
Weinan Li1fd51d92017-10-15 11:55:25 +080028#include "i915_vgpu.h"
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +010029#include "intel_ringbuffer.h"
30#include "intel_lrc.h"
31
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +030032/* Haswell does have the CXT_SIZE register however it does not appear to be
33 * valid. Now, docs explain in dwords what is in the context object. The full
34 * size is 70720 bytes, however, the power context and execlist context will
35 * never be saved (power context is stored elsewhere, and execlists don't work
36 * on HSW) - so the final size, including the extra state required for the
37 * Resource Streamer, is 66944 bytes, which rounds to 17 pages.
38 */
39#define HSW_CXT_TOTAL_SIZE (17 * PAGE_SIZE)
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +030040
Oscar Mateo7ab4adb2018-01-11 14:55:06 -080041#define DEFAULT_LR_CONTEXT_RENDER_SIZE (22 * PAGE_SIZE)
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +030042#define GEN8_LR_CONTEXT_RENDER_SIZE (20 * PAGE_SIZE)
43#define GEN9_LR_CONTEXT_RENDER_SIZE (22 * PAGE_SIZE)
Oscar Mateo3cf19342017-10-04 08:39:52 -070044#define GEN10_LR_CONTEXT_RENDER_SIZE (18 * PAGE_SIZE)
Tvrtko Ursulinb86aa442018-01-11 14:55:07 -080045#define GEN11_LR_CONTEXT_RENDER_SIZE (14 * PAGE_SIZE)
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +030046
47#define GEN8_LR_CONTEXT_OTHER_SIZE ( 2 * PAGE_SIZE)
48
Oscar Mateob8400f02017-04-10 07:34:32 -070049struct engine_class_info {
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +010050 const char *name;
Oscar Mateob8400f02017-04-10 07:34:32 -070051 int (*init_legacy)(struct intel_engine_cs *engine);
52 int (*init_execlists)(struct intel_engine_cs *engine);
Tvrtko Ursulin1803fcbc2017-11-10 14:26:27 +000053
54 u8 uabi_class;
Oscar Mateob8400f02017-04-10 07:34:32 -070055};
56
57static const struct engine_class_info intel_engine_classes[] = {
58 [RENDER_CLASS] = {
59 .name = "rcs",
60 .init_execlists = logical_render_ring_init,
61 .init_legacy = intel_init_render_ring_buffer,
Tvrtko Ursulin1803fcbc2017-11-10 14:26:27 +000062 .uabi_class = I915_ENGINE_CLASS_RENDER,
Oscar Mateob8400f02017-04-10 07:34:32 -070063 },
64 [COPY_ENGINE_CLASS] = {
65 .name = "bcs",
66 .init_execlists = logical_xcs_ring_init,
67 .init_legacy = intel_init_blt_ring_buffer,
Tvrtko Ursulin1803fcbc2017-11-10 14:26:27 +000068 .uabi_class = I915_ENGINE_CLASS_COPY,
Oscar Mateob8400f02017-04-10 07:34:32 -070069 },
70 [VIDEO_DECODE_CLASS] = {
71 .name = "vcs",
72 .init_execlists = logical_xcs_ring_init,
73 .init_legacy = intel_init_bsd_ring_buffer,
Tvrtko Ursulin1803fcbc2017-11-10 14:26:27 +000074 .uabi_class = I915_ENGINE_CLASS_VIDEO,
Oscar Mateob8400f02017-04-10 07:34:32 -070075 },
76 [VIDEO_ENHANCEMENT_CLASS] = {
77 .name = "vecs",
78 .init_execlists = logical_xcs_ring_init,
79 .init_legacy = intel_init_vebox_ring_buffer,
Tvrtko Ursulin1803fcbc2017-11-10 14:26:27 +000080 .uabi_class = I915_ENGINE_CLASS_VIDEO_ENHANCE,
Oscar Mateob8400f02017-04-10 07:34:32 -070081 },
82};
83
84struct engine_info {
Michal Wajdeczko237ae7c2017-03-01 20:26:15 +000085 unsigned int hw_id;
Chris Wilson1d39f282017-04-11 13:43:06 +010086 unsigned int uabi_id;
Daniele Ceraolo Spurio09081802017-04-10 07:34:29 -070087 u8 class;
88 u8 instance;
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +010089 u32 mmio_base;
90 unsigned irq_shift;
Oscar Mateob8400f02017-04-10 07:34:32 -070091};
92
93static const struct engine_info intel_engines[] = {
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +010094 [RCS] = {
Tvrtko Ursulin5ec2cf72016-08-16 17:04:20 +010095 .hw_id = RCS_HW,
Chris Wilson1d39f282017-04-11 13:43:06 +010096 .uabi_id = I915_EXEC_RENDER,
Daniele Ceraolo Spurio09081802017-04-10 07:34:29 -070097 .class = RENDER_CLASS,
98 .instance = 0,
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +010099 .mmio_base = RENDER_RING_BASE,
100 .irq_shift = GEN8_RCS_IRQ_SHIFT,
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100101 },
102 [BCS] = {
Tvrtko Ursulin5ec2cf72016-08-16 17:04:20 +0100103 .hw_id = BCS_HW,
Chris Wilson1d39f282017-04-11 13:43:06 +0100104 .uabi_id = I915_EXEC_BLT,
Daniele Ceraolo Spurio09081802017-04-10 07:34:29 -0700105 .class = COPY_ENGINE_CLASS,
106 .instance = 0,
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100107 .mmio_base = BLT_RING_BASE,
108 .irq_shift = GEN8_BCS_IRQ_SHIFT,
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100109 },
110 [VCS] = {
Tvrtko Ursulin5ec2cf72016-08-16 17:04:20 +0100111 .hw_id = VCS_HW,
Chris Wilson1d39f282017-04-11 13:43:06 +0100112 .uabi_id = I915_EXEC_BSD,
Daniele Ceraolo Spurio09081802017-04-10 07:34:29 -0700113 .class = VIDEO_DECODE_CLASS,
114 .instance = 0,
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100115 .mmio_base = GEN6_BSD_RING_BASE,
116 .irq_shift = GEN8_VCS1_IRQ_SHIFT,
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100117 },
118 [VCS2] = {
Tvrtko Ursulin5ec2cf72016-08-16 17:04:20 +0100119 .hw_id = VCS2_HW,
Chris Wilson1d39f282017-04-11 13:43:06 +0100120 .uabi_id = I915_EXEC_BSD,
Daniele Ceraolo Spurio09081802017-04-10 07:34:29 -0700121 .class = VIDEO_DECODE_CLASS,
122 .instance = 1,
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100123 .mmio_base = GEN8_BSD2_RING_BASE,
124 .irq_shift = GEN8_VCS2_IRQ_SHIFT,
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100125 },
Oscar Mateo5f79e7c2018-03-02 18:14:57 +0200126 [VCS3] = {
127 .hw_id = VCS3_HW,
128 .uabi_id = I915_EXEC_BSD,
129 .class = VIDEO_DECODE_CLASS,
130 .instance = 2,
131 .mmio_base = GEN11_BSD3_RING_BASE,
132 .irq_shift = 0, /* not used */
133 },
134 [VCS4] = {
135 .hw_id = VCS4_HW,
136 .uabi_id = I915_EXEC_BSD,
137 .class = VIDEO_DECODE_CLASS,
138 .instance = 3,
139 .mmio_base = GEN11_BSD4_RING_BASE,
140 .irq_shift = 0, /* not used */
141 },
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100142 [VECS] = {
Tvrtko Ursulin5ec2cf72016-08-16 17:04:20 +0100143 .hw_id = VECS_HW,
Chris Wilson1d39f282017-04-11 13:43:06 +0100144 .uabi_id = I915_EXEC_VEBOX,
Daniele Ceraolo Spurio09081802017-04-10 07:34:29 -0700145 .class = VIDEO_ENHANCEMENT_CLASS,
146 .instance = 0,
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100147 .mmio_base = VEBOX_RING_BASE,
148 .irq_shift = GEN8_VECS_IRQ_SHIFT,
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100149 },
Oscar Mateo5f79e7c2018-03-02 18:14:57 +0200150 [VECS2] = {
151 .hw_id = VECS2_HW,
152 .uabi_id = I915_EXEC_VEBOX,
153 .class = VIDEO_ENHANCEMENT_CLASS,
154 .instance = 1,
155 .mmio_base = GEN11_VEBOX2_RING_BASE,
156 .irq_shift = 0, /* not used */
157 },
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100158};
159
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +0300160/**
161 * ___intel_engine_context_size() - return the size of the context for an engine
162 * @dev_priv: i915 device private
163 * @class: engine class
164 *
165 * Each engine class may require a different amount of space for a context
166 * image.
167 *
168 * Return: size (in bytes) of an engine class specific context image
169 *
170 * Note: this size includes the HWSP, which is part of the context image
171 * in LRC mode, but does not include the "shared data page" used with
172 * GuC submission. The caller should account for this if using the GuC.
173 */
174static u32
175__intel_engine_context_size(struct drm_i915_private *dev_priv, u8 class)
176{
177 u32 cxt_size;
178
179 BUILD_BUG_ON(I915_GTT_PAGE_SIZE != PAGE_SIZE);
180
181 switch (class) {
182 case RENDER_CLASS:
183 switch (INTEL_GEN(dev_priv)) {
184 default:
185 MISSING_CASE(INTEL_GEN(dev_priv));
Oscar Mateo7ab4adb2018-01-11 14:55:06 -0800186 return DEFAULT_LR_CONTEXT_RENDER_SIZE;
Tvrtko Ursulinb86aa442018-01-11 14:55:07 -0800187 case 11:
188 return GEN11_LR_CONTEXT_RENDER_SIZE;
Rodrigo Vivif65f8412017-07-06 14:06:24 -0700189 case 10:
Oscar Mateo7fd0b1a2017-09-21 16:19:49 -0700190 return GEN10_LR_CONTEXT_RENDER_SIZE;
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +0300191 case 9:
192 return GEN9_LR_CONTEXT_RENDER_SIZE;
193 case 8:
Chris Wilsonfb5c5512017-11-20 20:55:00 +0000194 return GEN8_LR_CONTEXT_RENDER_SIZE;
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +0300195 case 7:
196 if (IS_HASWELL(dev_priv))
197 return HSW_CXT_TOTAL_SIZE;
198
199 cxt_size = I915_READ(GEN7_CXT_SIZE);
200 return round_up(GEN7_CXT_TOTAL_SIZE(cxt_size) * 64,
201 PAGE_SIZE);
202 case 6:
203 cxt_size = I915_READ(CXT_SIZE);
204 return round_up(GEN6_CXT_TOTAL_SIZE(cxt_size) * 64,
205 PAGE_SIZE);
206 case 5:
207 case 4:
208 case 3:
209 case 2:
210 /* For the special day when i810 gets merged. */
211 case 1:
212 return 0;
213 }
214 break;
215 default:
216 MISSING_CASE(class);
217 case VIDEO_DECODE_CLASS:
218 case VIDEO_ENHANCEMENT_CLASS:
219 case COPY_ENGINE_CLASS:
220 if (INTEL_GEN(dev_priv) < 8)
221 return 0;
222 return GEN8_LR_CONTEXT_OTHER_SIZE;
223 }
224}
225
Akash Goel3b3f1652016-10-13 22:44:48 +0530226static int
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100227intel_engine_setup(struct drm_i915_private *dev_priv,
228 enum intel_engine_id id)
229{
230 const struct engine_info *info = &intel_engines[id];
Oscar Mateob8400f02017-04-10 07:34:32 -0700231 const struct engine_class_info *class_info;
Akash Goel3b3f1652016-10-13 22:44:48 +0530232 struct intel_engine_cs *engine;
233
Oscar Mateob8400f02017-04-10 07:34:32 -0700234 GEM_BUG_ON(info->class >= ARRAY_SIZE(intel_engine_classes));
235 class_info = &intel_engine_classes[info->class];
236
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000237 if (GEM_WARN_ON(info->class > MAX_ENGINE_CLASS))
238 return -EINVAL;
239
240 if (GEM_WARN_ON(info->instance > MAX_ENGINE_INSTANCE))
241 return -EINVAL;
242
243 if (GEM_WARN_ON(dev_priv->engine_class[info->class][info->instance]))
244 return -EINVAL;
245
Akash Goel3b3f1652016-10-13 22:44:48 +0530246 GEM_BUG_ON(dev_priv->engine[id]);
247 engine = kzalloc(sizeof(*engine), GFP_KERNEL);
248 if (!engine)
249 return -ENOMEM;
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100250
251 engine->id = id;
252 engine->i915 = dev_priv;
Oscar Mateo6e516142017-04-10 07:34:31 -0700253 WARN_ON(snprintf(engine->name, sizeof(engine->name), "%s%u",
Oscar Mateob8400f02017-04-10 07:34:32 -0700254 class_info->name, info->instance) >=
255 sizeof(engine->name));
Tvrtko Ursulin5ec2cf72016-08-16 17:04:20 +0100256 engine->hw_id = engine->guc_id = info->hw_id;
Oscar Mateo5f79e7c2018-03-02 18:14:57 +0200257 if (INTEL_GEN(dev_priv) >= 11) {
258 switch (engine->id) {
259 case VCS:
260 engine->mmio_base = GEN11_BSD_RING_BASE;
261 break;
262 case VCS2:
263 engine->mmio_base = GEN11_BSD2_RING_BASE;
264 break;
265 case VECS:
266 engine->mmio_base = GEN11_VEBOX_RING_BASE;
267 break;
268 default:
269 /* take the original value for all other engines */
270 engine->mmio_base = info->mmio_base;
271 break;
272 }
273 } else {
274 engine->mmio_base = info->mmio_base;
275 }
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100276 engine->irq_shift = info->irq_shift;
Daniele Ceraolo Spurio09081802017-04-10 07:34:29 -0700277 engine->class = info->class;
278 engine->instance = info->instance;
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100279
Tvrtko Ursulin1803fcbc2017-11-10 14:26:27 +0000280 engine->uabi_id = info->uabi_id;
281 engine->uabi_class = class_info->uabi_class;
282
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +0300283 engine->context_size = __intel_engine_context_size(dev_priv,
284 engine->class);
285 if (WARN_ON(engine->context_size > BIT(20)))
286 engine->context_size = 0;
287
Chris Wilson0de91362016-11-14 20:41:01 +0000288 /* Nothing to do here, execute in order of dependencies */
289 engine->schedule = NULL;
290
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +0000291 spin_lock_init(&engine->stats.lock);
292
Changbin Du3fc03062017-03-13 10:47:11 +0800293 ATOMIC_INIT_NOTIFIER_HEAD(&engine->context_status_notifier);
294
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000295 dev_priv->engine_class[info->class][info->instance] = engine;
Akash Goel3b3f1652016-10-13 22:44:48 +0530296 dev_priv->engine[id] = engine;
297 return 0;
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100298}
299
300/**
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +0300301 * intel_engines_init_mmio() - allocate and prepare the Engine Command Streamers
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +0000302 * @dev_priv: i915 device private
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100303 *
304 * Return: non-zero if the initialization failed.
305 */
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +0300306int intel_engines_init_mmio(struct drm_i915_private *dev_priv)
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100307{
Tvrtko Ursulinc1bb1142016-08-10 16:22:10 +0100308 struct intel_device_info *device_info = mkwrite_device_info(dev_priv);
Chris Wilson5f9be052017-04-11 17:56:58 +0100309 const unsigned int ring_mask = INTEL_INFO(dev_priv)->ring_mask;
Akash Goel3b3f1652016-10-13 22:44:48 +0530310 struct intel_engine_cs *engine;
311 enum intel_engine_id id;
Chris Wilson5f9be052017-04-11 17:56:58 +0100312 unsigned int mask = 0;
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100313 unsigned int i;
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000314 int err;
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100315
Tvrtko Ursulin70006ad2016-10-13 11:02:56 +0100316 WARN_ON(ring_mask == 0);
317 WARN_ON(ring_mask &
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100318 GENMASK(sizeof(mask) * BITS_PER_BYTE - 1, I915_NUM_ENGINES));
319
320 for (i = 0; i < ARRAY_SIZE(intel_engines); i++) {
321 if (!HAS_ENGINE(dev_priv, i))
322 continue;
323
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000324 err = intel_engine_setup(dev_priv, i);
325 if (err)
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100326 goto cleanup;
327
328 mask |= ENGINE_MASK(i);
329 }
330
331 /*
332 * Catch failures to update intel_engines table when the new engines
333 * are added to the driver by a warning and disabling the forgotten
334 * engines.
335 */
Tvrtko Ursulin70006ad2016-10-13 11:02:56 +0100336 if (WARN_ON(mask != ring_mask))
Tvrtko Ursulinc1bb1142016-08-10 16:22:10 +0100337 device_info->ring_mask = mask;
338
Chris Wilson5f9be052017-04-11 17:56:58 +0100339 /* We always presume we have at least RCS available for later probing */
340 if (WARN_ON(!HAS_ENGINE(dev_priv, RCS))) {
341 err = -ENODEV;
342 goto cleanup;
343 }
344
Tvrtko Ursulinc1bb1142016-08-10 16:22:10 +0100345 device_info->num_rings = hweight32(mask);
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100346
Michel Thierryce453b32017-11-10 16:44:47 -0800347 i915_check_and_clear_faults(dev_priv);
348
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100349 return 0;
350
351cleanup:
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000352 for_each_engine(engine, dev_priv, id)
353 kfree(engine);
354 return err;
355}
356
357/**
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +0300358 * intel_engines_init() - init the Engine Command Streamers
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000359 * @dev_priv: i915 device private
360 *
361 * Return: non-zero if the initialization failed.
362 */
363int intel_engines_init(struct drm_i915_private *dev_priv)
364{
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000365 struct intel_engine_cs *engine;
366 enum intel_engine_id id, err_id;
Tvrtko Ursulin33def1f2017-06-16 14:03:38 +0100367 int err;
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000368
Akash Goel3b3f1652016-10-13 22:44:48 +0530369 for_each_engine(engine, dev_priv, id) {
Oscar Mateob8400f02017-04-10 07:34:32 -0700370 const struct engine_class_info *class_info =
371 &intel_engine_classes[engine->class];
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000372 int (*init)(struct intel_engine_cs *engine);
373
Chris Wilsonfb5c5512017-11-20 20:55:00 +0000374 if (HAS_EXECLISTS(dev_priv))
Oscar Mateob8400f02017-04-10 07:34:32 -0700375 init = class_info->init_execlists;
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000376 else
Oscar Mateob8400f02017-04-10 07:34:32 -0700377 init = class_info->init_legacy;
Tvrtko Ursulin33def1f2017-06-16 14:03:38 +0100378
379 err = -EINVAL;
380 err_id = id;
381
382 if (GEM_WARN_ON(!init))
383 goto cleanup;
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000384
385 err = init(engine);
Tvrtko Ursulin33def1f2017-06-16 14:03:38 +0100386 if (err)
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000387 goto cleanup;
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000388
Chris Wilsonff44ad52017-03-16 17:13:03 +0000389 GEM_BUG_ON(!engine->submit_request);
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000390 }
391
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000392 return 0;
393
394cleanup:
395 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursulin33def1f2017-06-16 14:03:38 +0100396 if (id >= err_id) {
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000397 kfree(engine);
Tvrtko Ursulin33def1f2017-06-16 14:03:38 +0100398 dev_priv->engine[id] = NULL;
399 } else {
Tvrtko Ursulin8ee7c6e2017-02-16 12:23:22 +0000400 dev_priv->gt.cleanup_engine(engine);
Tvrtko Ursulin33def1f2017-06-16 14:03:38 +0100401 }
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100402 }
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000403 return err;
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100404}
405
Chris Wilson73cb9702016-10-28 13:58:46 +0100406void intel_engine_init_global_seqno(struct intel_engine_cs *engine, u32 seqno)
Chris Wilson57f275a2016-08-15 10:49:00 +0100407{
408 struct drm_i915_private *dev_priv = engine->i915;
409
410 /* Our semaphore implementation is strictly monotonic (i.e. we proceed
411 * so long as the semaphore value in the register/page is greater
412 * than the sync value), so whenever we reset the seqno,
413 * so long as we reset the tracking semaphore value to 0, it will
414 * always be before the next request's seqno. If we don't reset
415 * the semaphore value, then when the seqno moves backwards all
416 * future waits will complete instantly (causing rendering corruption).
417 */
418 if (IS_GEN6(dev_priv) || IS_GEN7(dev_priv)) {
419 I915_WRITE(RING_SYNC_0(engine->mmio_base), 0);
420 I915_WRITE(RING_SYNC_1(engine->mmio_base), 0);
421 if (HAS_VEBOX(dev_priv))
422 I915_WRITE(RING_SYNC_2(engine->mmio_base), 0);
423 }
Chris Wilson57f275a2016-08-15 10:49:00 +0100424
425 intel_write_status_page(engine, I915_GEM_HWS_INDEX, seqno);
Chris Wilson14a6bbf2017-03-14 11:14:52 +0000426 clear_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted);
Chris Wilson73cb9702016-10-28 13:58:46 +0100427
Chris Wilson57f275a2016-08-15 10:49:00 +0100428 /* After manually advancing the seqno, fake the interrupt in case
429 * there are any waiters for that seqno.
430 */
431 intel_engine_wakeup(engine);
Chris Wilson2ca9faa2017-04-05 16:30:54 +0100432
433 GEM_BUG_ON(intel_engine_get_seqno(engine) != seqno);
Chris Wilson57f275a2016-08-15 10:49:00 +0100434}
435
Chris Wilson73cb9702016-10-28 13:58:46 +0100436static void intel_engine_init_timeline(struct intel_engine_cs *engine)
Chris Wilsondcff85c2016-08-05 10:14:11 +0100437{
Chris Wilson73cb9702016-10-28 13:58:46 +0100438 engine->timeline = &engine->i915->gt.global_timeline.engine[engine->id];
Chris Wilsondcff85c2016-08-05 10:14:11 +0100439}
440
Mika Kuoppala19df9a52017-09-22 15:43:04 +0300441static bool csb_force_mmio(struct drm_i915_private *i915)
442{
Mika Kuoppala19df9a52017-09-22 15:43:04 +0300443 /*
444 * IOMMU adds unpredictable latency causing the CSB write (from the
445 * GPU into the HWSP) to only be visible some time after the interrupt
446 * (missed breadcrumb syndrome).
447 */
448 if (intel_vtd_active())
449 return true;
450
Weinan Li1fd51d92017-10-15 11:55:25 +0800451 /* Older GVT emulation depends upon intercepting CSB mmio */
452 if (intel_vgpu_active(i915) && !intel_vgpu_has_hwsp_emulation(i915))
453 return true;
454
Mika Kuoppala19df9a52017-09-22 15:43:04 +0300455 return false;
456}
457
458static void intel_engine_init_execlist(struct intel_engine_cs *engine)
459{
460 struct intel_engine_execlists * const execlists = &engine->execlists;
461
462 execlists->csb_use_mmio = csb_force_mmio(engine->i915);
463
Mika Kuoppala76e70082017-09-22 15:43:07 +0300464 execlists->port_mask = 1;
465 BUILD_BUG_ON_NOT_POWER_OF_2(execlists_num_ports(execlists));
466 GEM_BUG_ON(execlists_num_ports(execlists) > EXECLIST_MAX_PORTS);
467
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000468 execlists->queue_priority = INT_MIN;
Mika Kuoppala19df9a52017-09-22 15:43:04 +0300469 execlists->queue = RB_ROOT;
470 execlists->first = NULL;
471}
472
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100473/**
474 * intel_engines_setup_common - setup engine state not requiring hw access
475 * @engine: Engine to setup.
476 *
477 * Initializes @engine@ structure members shared between legacy and execlists
478 * submission modes which do not require hardware access.
479 *
480 * Typically done early in the submission mode specific engine setup stage.
481 */
482void intel_engine_setup_common(struct intel_engine_cs *engine)
483{
Mika Kuoppala19df9a52017-09-22 15:43:04 +0300484 intel_engine_init_execlist(engine);
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100485
Chris Wilson73cb9702016-10-28 13:58:46 +0100486 intel_engine_init_timeline(engine);
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100487 intel_engine_init_hangcheck(engine);
Chris Wilson115003e92016-08-04 16:32:19 +0100488 i915_gem_batch_pool_init(engine, &engine->batch_pool);
Chris Wilson7756e452016-08-18 17:17:10 +0100489
490 intel_engine_init_cmd_parser(engine);
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100491}
492
Chris Wilsonadc320c2016-08-15 10:48:59 +0100493int intel_engine_create_scratch(struct intel_engine_cs *engine, int size)
494{
495 struct drm_i915_gem_object *obj;
496 struct i915_vma *vma;
497 int ret;
498
499 WARN_ON(engine->scratch);
500
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000501 obj = i915_gem_object_create_stolen(engine->i915, size);
Chris Wilsonadc320c2016-08-15 10:48:59 +0100502 if (!obj)
Chris Wilson920cf412016-10-28 13:58:30 +0100503 obj = i915_gem_object_create_internal(engine->i915, size);
Chris Wilsonadc320c2016-08-15 10:48:59 +0100504 if (IS_ERR(obj)) {
505 DRM_ERROR("Failed to allocate scratch page\n");
506 return PTR_ERR(obj);
507 }
508
Chris Wilsona01cb372017-01-16 15:21:30 +0000509 vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL);
Chris Wilsonadc320c2016-08-15 10:48:59 +0100510 if (IS_ERR(vma)) {
511 ret = PTR_ERR(vma);
512 goto err_unref;
513 }
514
515 ret = i915_vma_pin(vma, 0, 4096, PIN_GLOBAL | PIN_HIGH);
516 if (ret)
517 goto err_unref;
518
519 engine->scratch = vma;
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100520 DRM_DEBUG_DRIVER("%s pipe control offset: 0x%08x\n",
521 engine->name, i915_ggtt_offset(vma));
Chris Wilsonadc320c2016-08-15 10:48:59 +0100522 return 0;
523
524err_unref:
525 i915_gem_object_put(obj);
526 return ret;
527}
528
529static void intel_engine_cleanup_scratch(struct intel_engine_cs *engine)
530{
Chris Wilson19880c42016-08-15 10:49:05 +0100531 i915_vma_unpin_and_release(&engine->scratch);
Chris Wilsonadc320c2016-08-15 10:48:59 +0100532}
533
Daniele Ceraolo Spurio486e93f2017-09-13 09:56:02 +0100534static void cleanup_phys_status_page(struct intel_engine_cs *engine)
535{
536 struct drm_i915_private *dev_priv = engine->i915;
537
538 if (!dev_priv->status_page_dmah)
539 return;
540
541 drm_pci_free(&dev_priv->drm, dev_priv->status_page_dmah);
542 engine->status_page.page_addr = NULL;
543}
544
545static void cleanup_status_page(struct intel_engine_cs *engine)
546{
547 struct i915_vma *vma;
548 struct drm_i915_gem_object *obj;
549
550 vma = fetch_and_zero(&engine->status_page.vma);
551 if (!vma)
552 return;
553
554 obj = vma->obj;
555
556 i915_vma_unpin(vma);
557 i915_vma_close(vma);
558
559 i915_gem_object_unpin_map(obj);
560 __i915_gem_object_release_unless_active(obj);
561}
562
563static int init_status_page(struct intel_engine_cs *engine)
564{
565 struct drm_i915_gem_object *obj;
566 struct i915_vma *vma;
567 unsigned int flags;
568 void *vaddr;
569 int ret;
570
571 obj = i915_gem_object_create_internal(engine->i915, PAGE_SIZE);
572 if (IS_ERR(obj)) {
573 DRM_ERROR("Failed to allocate status page\n");
574 return PTR_ERR(obj);
575 }
576
577 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
578 if (ret)
579 goto err;
580
581 vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL);
582 if (IS_ERR(vma)) {
583 ret = PTR_ERR(vma);
584 goto err;
585 }
586
587 flags = PIN_GLOBAL;
588 if (!HAS_LLC(engine->i915))
589 /* On g33, we cannot place HWS above 256MiB, so
590 * restrict its pinning to the low mappable arena.
591 * Though this restriction is not documented for
592 * gen4, gen5, or byt, they also behave similarly
593 * and hang if the HWS is placed at the top of the
594 * GTT. To generalise, it appears that all !llc
595 * platforms have issues with us placing the HWS
596 * above the mappable region (even though we never
597 * actually map it).
598 */
599 flags |= PIN_MAPPABLE;
Chris Wilson34a04e52017-09-13 09:56:03 +0100600 else
601 flags |= PIN_HIGH;
Daniele Ceraolo Spurio486e93f2017-09-13 09:56:02 +0100602 ret = i915_vma_pin(vma, 0, 4096, flags);
603 if (ret)
604 goto err;
605
606 vaddr = i915_gem_object_pin_map(obj, I915_MAP_WB);
607 if (IS_ERR(vaddr)) {
608 ret = PTR_ERR(vaddr);
609 goto err_unpin;
610 }
611
612 engine->status_page.vma = vma;
613 engine->status_page.ggtt_offset = i915_ggtt_offset(vma);
614 engine->status_page.page_addr = memset(vaddr, 0, PAGE_SIZE);
615
616 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
617 engine->name, i915_ggtt_offset(vma));
618 return 0;
619
620err_unpin:
621 i915_vma_unpin(vma);
622err:
623 i915_gem_object_put(obj);
624 return ret;
625}
626
627static int init_phys_status_page(struct intel_engine_cs *engine)
628{
629 struct drm_i915_private *dev_priv = engine->i915;
630
631 GEM_BUG_ON(engine->id != RCS);
632
633 dev_priv->status_page_dmah =
634 drm_pci_alloc(&dev_priv->drm, PAGE_SIZE, PAGE_SIZE);
635 if (!dev_priv->status_page_dmah)
636 return -ENOMEM;
637
638 engine->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
639 memset(engine->status_page.page_addr, 0, PAGE_SIZE);
640
641 return 0;
642}
643
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100644/**
645 * intel_engines_init_common - initialize cengine state which might require hw access
646 * @engine: Engine to initialize.
647 *
648 * Initializes @engine@ structure members shared between legacy and execlists
649 * submission modes which do require hardware access.
650 *
651 * Typcally done at later stages of submission mode specific engine setup.
652 *
653 * Returns zero on success or an error code on failure.
654 */
655int intel_engine_init_common(struct intel_engine_cs *engine)
656{
Chris Wilson266a2402017-05-04 10:33:08 +0100657 struct intel_ring *ring;
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100658 int ret;
659
Chris Wilsonff44ad52017-03-16 17:13:03 +0000660 engine->set_default_submission(engine);
661
Chris Wilsone8a9c582016-12-18 15:37:20 +0000662 /* We may need to do things with the shrinker which
663 * require us to immediately switch back to the default
664 * context. This can cause a problem as pinning the
665 * default context also requires GTT space which may not
666 * be available. To avoid this we always pin the default
667 * context.
668 */
Chris Wilson266a2402017-05-04 10:33:08 +0100669 ring = engine->context_pin(engine, engine->i915->kernel_context);
670 if (IS_ERR(ring))
671 return PTR_ERR(ring);
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100672
Chris Wilsone7af3112017-10-03 21:34:48 +0100673 /*
674 * Similarly the preempt context must always be available so that
675 * we can interrupt the engine at any time.
676 */
Chris Wilsond6376372018-02-07 21:05:44 +0000677 if (engine->i915->preempt_context) {
Chris Wilsone7af3112017-10-03 21:34:48 +0100678 ring = engine->context_pin(engine,
679 engine->i915->preempt_context);
680 if (IS_ERR(ring)) {
681 ret = PTR_ERR(ring);
682 goto err_unpin_kernel;
683 }
684 }
685
Chris Wilsone8a9c582016-12-18 15:37:20 +0000686 ret = intel_engine_init_breadcrumbs(engine);
687 if (ret)
Chris Wilsone7af3112017-10-03 21:34:48 +0100688 goto err_unpin_preempt;
Chris Wilsone8a9c582016-12-18 15:37:20 +0000689
Daniele Ceraolo Spurio486e93f2017-09-13 09:56:02 +0100690 if (HWS_NEEDS_PHYSICAL(engine->i915))
691 ret = init_phys_status_page(engine);
692 else
693 ret = init_status_page(engine);
694 if (ret)
Chris Wilson7c2fa7f2017-11-10 14:26:34 +0000695 goto err_breadcrumbs;
Chris Wilson4e50f082016-10-28 13:58:31 +0100696
Chris Wilson7756e452016-08-18 17:17:10 +0100697 return 0;
Chris Wilsone8a9c582016-12-18 15:37:20 +0000698
Daniele Ceraolo Spurio486e93f2017-09-13 09:56:02 +0100699err_breadcrumbs:
700 intel_engine_fini_breadcrumbs(engine);
Chris Wilsone7af3112017-10-03 21:34:48 +0100701err_unpin_preempt:
Chris Wilsond6376372018-02-07 21:05:44 +0000702 if (engine->i915->preempt_context)
Chris Wilsone7af3112017-10-03 21:34:48 +0100703 engine->context_unpin(engine, engine->i915->preempt_context);
704err_unpin_kernel:
Chris Wilsone8a9c582016-12-18 15:37:20 +0000705 engine->context_unpin(engine, engine->i915->kernel_context);
706 return ret;
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100707}
Chris Wilson96a945a2016-08-03 13:19:16 +0100708
709/**
710 * intel_engines_cleanup_common - cleans up the engine state created by
711 * the common initiailizers.
712 * @engine: Engine to cleanup.
713 *
714 * This cleans up everything created by the common helpers.
715 */
716void intel_engine_cleanup_common(struct intel_engine_cs *engine)
717{
Chris Wilsonadc320c2016-08-15 10:48:59 +0100718 intel_engine_cleanup_scratch(engine);
719
Daniele Ceraolo Spurio486e93f2017-09-13 09:56:02 +0100720 if (HWS_NEEDS_PHYSICAL(engine->i915))
721 cleanup_phys_status_page(engine);
722 else
723 cleanup_status_page(engine);
724
Chris Wilson96a945a2016-08-03 13:19:16 +0100725 intel_engine_fini_breadcrumbs(engine);
Chris Wilson7756e452016-08-18 17:17:10 +0100726 intel_engine_cleanup_cmd_parser(engine);
Chris Wilson96a945a2016-08-03 13:19:16 +0100727 i915_gem_batch_pool_fini(&engine->batch_pool);
Chris Wilsone8a9c582016-12-18 15:37:20 +0000728
Chris Wilsond2b4b972017-11-10 14:26:33 +0000729 if (engine->default_state)
730 i915_gem_object_put(engine->default_state);
731
Chris Wilsond6376372018-02-07 21:05:44 +0000732 if (engine->i915->preempt_context)
Chris Wilsone7af3112017-10-03 21:34:48 +0100733 engine->context_unpin(engine, engine->i915->preempt_context);
Chris Wilsone8a9c582016-12-18 15:37:20 +0000734 engine->context_unpin(engine, engine->i915->kernel_context);
Chris Wilson96a945a2016-08-03 13:19:16 +0100735}
Chris Wilson1b365952016-10-04 21:11:31 +0100736
Chris Wilson3ceda3a2018-02-12 10:24:15 +0000737u64 intel_engine_get_active_head(const struct intel_engine_cs *engine)
Chris Wilson1b365952016-10-04 21:11:31 +0100738{
739 struct drm_i915_private *dev_priv = engine->i915;
740 u64 acthd;
741
742 if (INTEL_GEN(dev_priv) >= 8)
743 acthd = I915_READ64_2x32(RING_ACTHD(engine->mmio_base),
744 RING_ACTHD_UDW(engine->mmio_base));
745 else if (INTEL_GEN(dev_priv) >= 4)
746 acthd = I915_READ(RING_ACTHD(engine->mmio_base));
747 else
748 acthd = I915_READ(ACTHD);
749
750 return acthd;
751}
752
Chris Wilson3ceda3a2018-02-12 10:24:15 +0000753u64 intel_engine_get_last_batch_head(const struct intel_engine_cs *engine)
Chris Wilson1b365952016-10-04 21:11:31 +0100754{
755 struct drm_i915_private *dev_priv = engine->i915;
756 u64 bbaddr;
757
758 if (INTEL_GEN(dev_priv) >= 8)
759 bbaddr = I915_READ64_2x32(RING_BBADDR(engine->mmio_base),
760 RING_BBADDR_UDW(engine->mmio_base));
761 else
762 bbaddr = I915_READ(RING_BBADDR(engine->mmio_base));
763
764 return bbaddr;
765}
Chris Wilson0e704472016-10-12 10:05:17 +0100766
767const char *i915_cache_level_str(struct drm_i915_private *i915, int type)
768{
769 switch (type) {
770 case I915_CACHE_NONE: return " uncached";
771 case I915_CACHE_LLC: return HAS_LLC(i915) ? " LLC" : " snooped";
772 case I915_CACHE_L3_LLC: return " L3+LLC";
773 case I915_CACHE_WT: return " WT";
774 default: return "";
775 }
776}
777
778static inline uint32_t
779read_subslice_reg(struct drm_i915_private *dev_priv, int slice,
780 int subslice, i915_reg_t reg)
781{
782 uint32_t mcr;
783 uint32_t ret;
784 enum forcewake_domains fw_domains;
785
786 fw_domains = intel_uncore_forcewake_for_reg(dev_priv, reg,
787 FW_REG_READ);
788 fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
789 GEN8_MCR_SELECTOR,
790 FW_REG_READ | FW_REG_WRITE);
791
792 spin_lock_irq(&dev_priv->uncore.lock);
793 intel_uncore_forcewake_get__locked(dev_priv, fw_domains);
794
795 mcr = I915_READ_FW(GEN8_MCR_SELECTOR);
796 /*
797 * The HW expects the slice and sublice selectors to be reset to 0
798 * after reading out the registers.
799 */
800 WARN_ON_ONCE(mcr & (GEN8_MCR_SLICE_MASK | GEN8_MCR_SUBSLICE_MASK));
801 mcr &= ~(GEN8_MCR_SLICE_MASK | GEN8_MCR_SUBSLICE_MASK);
802 mcr |= GEN8_MCR_SLICE(slice) | GEN8_MCR_SUBSLICE(subslice);
803 I915_WRITE_FW(GEN8_MCR_SELECTOR, mcr);
804
805 ret = I915_READ_FW(reg);
806
807 mcr &= ~(GEN8_MCR_SLICE_MASK | GEN8_MCR_SUBSLICE_MASK);
808 I915_WRITE_FW(GEN8_MCR_SELECTOR, mcr);
809
810 intel_uncore_forcewake_put__locked(dev_priv, fw_domains);
811 spin_unlock_irq(&dev_priv->uncore.lock);
812
813 return ret;
814}
815
816/* NB: please notice the memset */
817void intel_engine_get_instdone(struct intel_engine_cs *engine,
818 struct intel_instdone *instdone)
819{
820 struct drm_i915_private *dev_priv = engine->i915;
821 u32 mmio_base = engine->mmio_base;
822 int slice;
823 int subslice;
824
825 memset(instdone, 0, sizeof(*instdone));
826
827 switch (INTEL_GEN(dev_priv)) {
828 default:
829 instdone->instdone = I915_READ(RING_INSTDONE(mmio_base));
830
831 if (engine->id != RCS)
832 break;
833
834 instdone->slice_common = I915_READ(GEN7_SC_INSTDONE);
835 for_each_instdone_slice_subslice(dev_priv, slice, subslice) {
836 instdone->sampler[slice][subslice] =
837 read_subslice_reg(dev_priv, slice, subslice,
838 GEN7_SAMPLER_INSTDONE);
839 instdone->row[slice][subslice] =
840 read_subslice_reg(dev_priv, slice, subslice,
841 GEN7_ROW_INSTDONE);
842 }
843 break;
844 case 7:
845 instdone->instdone = I915_READ(RING_INSTDONE(mmio_base));
846
847 if (engine->id != RCS)
848 break;
849
850 instdone->slice_common = I915_READ(GEN7_SC_INSTDONE);
851 instdone->sampler[0][0] = I915_READ(GEN7_SAMPLER_INSTDONE);
852 instdone->row[0][0] = I915_READ(GEN7_ROW_INSTDONE);
853
854 break;
855 case 6:
856 case 5:
857 case 4:
858 instdone->instdone = I915_READ(RING_INSTDONE(mmio_base));
859
860 if (engine->id == RCS)
861 /* HACK: Using the wrong struct member */
862 instdone->slice_common = I915_READ(GEN4_INSTDONE1);
863 break;
864 case 3:
865 case 2:
866 instdone->instdone = I915_READ(GEN2_INSTDONE);
867 break;
868 }
869}
Chris Wilsonf97fbf92017-02-13 17:15:14 +0000870
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +0000871static int wa_add(struct drm_i915_private *dev_priv,
872 i915_reg_t addr,
873 const u32 mask, const u32 val)
874{
875 const u32 idx = dev_priv->workarounds.count;
876
877 if (WARN_ON(idx >= I915_MAX_WA_REGS))
878 return -ENOSPC;
879
880 dev_priv->workarounds.reg[idx].addr = addr;
881 dev_priv->workarounds.reg[idx].value = val;
882 dev_priv->workarounds.reg[idx].mask = mask;
883
884 dev_priv->workarounds.count++;
885
886 return 0;
887}
888
889#define WA_REG(addr, mask, val) do { \
890 const int r = wa_add(dev_priv, (addr), (mask), (val)); \
891 if (r) \
892 return r; \
893 } while (0)
894
895#define WA_SET_BIT_MASKED(addr, mask) \
896 WA_REG(addr, (mask), _MASKED_BIT_ENABLE(mask))
897
898#define WA_CLR_BIT_MASKED(addr, mask) \
899 WA_REG(addr, (mask), _MASKED_BIT_DISABLE(mask))
900
901#define WA_SET_FIELD_MASKED(addr, mask, value) \
902 WA_REG(addr, mask, _MASKED_FIELD(mask, value))
903
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +0000904static int wa_ring_whitelist_reg(struct intel_engine_cs *engine,
905 i915_reg_t reg)
906{
907 struct drm_i915_private *dev_priv = engine->i915;
908 struct i915_workarounds *wa = &dev_priv->workarounds;
909 const uint32_t index = wa->hw_whitelist_count[engine->id];
910
911 if (WARN_ON(index >= RING_MAX_NONPRIV_SLOTS))
912 return -EINVAL;
913
Oscar Mateo32ced392017-09-28 15:40:39 -0700914 I915_WRITE(RING_FORCE_TO_NONPRIV(engine->mmio_base, index),
915 i915_mmio_reg_offset(reg));
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +0000916 wa->hw_whitelist_count[engine->id]++;
917
918 return 0;
919}
920
921static int gen8_init_workarounds(struct intel_engine_cs *engine)
922{
923 struct drm_i915_private *dev_priv = engine->i915;
924
925 WA_SET_BIT_MASKED(INSTPM, INSTPM_FORCE_ORDERING);
926
927 /* WaDisableAsyncFlipPerfMode:bdw,chv */
928 WA_SET_BIT_MASKED(MI_MODE, ASYNC_FLIP_PERF_DISABLE);
929
930 /* WaDisablePartialInstShootdown:bdw,chv */
931 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
932 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
933
934 /* Use Force Non-Coherent whenever executing a 3D context. This is a
935 * workaround for for a possible hang in the unlikely event a TLB
936 * invalidation occurs during a PSD flush.
937 */
938 /* WaForceEnableNonCoherent:bdw,chv */
939 /* WaHdcDisableFetchWhenMasked:bdw,chv */
940 WA_SET_BIT_MASKED(HDC_CHICKEN0,
941 HDC_DONOT_FETCH_MEM_WHEN_MASKED |
942 HDC_FORCE_NON_COHERENT);
943
944 /* From the Haswell PRM, Command Reference: Registers, CACHE_MODE_0:
945 * "The Hierarchical Z RAW Stall Optimization allows non-overlapping
946 * polygons in the same 8x4 pixel/sample area to be processed without
947 * stalling waiting for the earlier ones to write to Hierarchical Z
948 * buffer."
949 *
950 * This optimization is off by default for BDW and CHV; turn it on.
951 */
952 WA_CLR_BIT_MASKED(CACHE_MODE_0_GEN7, HIZ_RAW_STALL_OPT_DISABLE);
953
954 /* Wa4x4STCOptimizationDisable:bdw,chv */
955 WA_SET_BIT_MASKED(CACHE_MODE_1, GEN8_4x4_STC_OPTIMIZATION_DISABLE);
956
957 /*
958 * BSpec recommends 8x4 when MSAA is used,
959 * however in practice 16x4 seems fastest.
960 *
961 * Note that PS/WM thread counts depend on the WIZ hashing
962 * disable bit, which we don't touch here, but it's good
963 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
964 */
965 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
966 GEN6_WIZ_HASHING_MASK,
967 GEN6_WIZ_HASHING_16x4);
968
969 return 0;
970}
971
972static int bdw_init_workarounds(struct intel_engine_cs *engine)
973{
974 struct drm_i915_private *dev_priv = engine->i915;
975 int ret;
976
977 ret = gen8_init_workarounds(engine);
978 if (ret)
979 return ret;
980
981 /* WaDisableThreadStallDopClockGating:bdw (pre-production) */
982 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
983
984 /* WaDisableDopClockGating:bdw
985 *
986 * Also see the related UCGTCL1 write in broadwell_init_clock_gating()
987 * to disable EUTC clock gating.
988 */
989 WA_SET_BIT_MASKED(GEN7_ROW_CHICKEN2,
990 DOP_CLOCK_GATING_DISABLE);
991
992 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
993 GEN8_SAMPLER_POWER_BYPASS_DIS);
994
995 WA_SET_BIT_MASKED(HDC_CHICKEN0,
996 /* WaForceContextSaveRestoreNonCoherent:bdw */
997 HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
998 /* WaDisableFenceDestinationToSLM:bdw (pre-prod) */
999 (IS_BDW_GT3(dev_priv) ? HDC_FENCE_DEST_SLM_DISABLE : 0));
1000
1001 return 0;
1002}
1003
1004static int chv_init_workarounds(struct intel_engine_cs *engine)
1005{
1006 struct drm_i915_private *dev_priv = engine->i915;
1007 int ret;
1008
1009 ret = gen8_init_workarounds(engine);
1010 if (ret)
1011 return ret;
1012
1013 /* WaDisableThreadStallDopClockGating:chv */
1014 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
1015
1016 /* Improve HiZ throughput on CHV. */
1017 WA_SET_BIT_MASKED(HIZ_CHICKEN, CHV_HZ_8X8_MODE_IN_1X);
1018
1019 return 0;
1020}
1021
1022static int gen9_init_workarounds(struct intel_engine_cs *engine)
1023{
1024 struct drm_i915_private *dev_priv = engine->i915;
1025 int ret;
1026
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001027 /* WaConextSwitchWithConcurrentTLBInvalidate:skl,bxt,kbl,glk,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001028 I915_WRITE(GEN9_CSFE_CHICKEN1_RCS, _MASKED_BIT_ENABLE(GEN9_PREEMPT_GPGPU_SYNC_SWITCH_DISABLE));
1029
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001030 /* WaEnableLbsSlaRetryTimerDecrement:skl,bxt,kbl,glk,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001031 I915_WRITE(BDW_SCRATCH1, I915_READ(BDW_SCRATCH1) |
1032 GEN9_LBS_SLA_RETRY_TIMER_DECREMENT_ENABLE);
1033
Rodrigo Vivi98eed3d2017-06-19 14:21:47 -07001034 /* WaDisableKillLogic:bxt,skl,kbl */
1035 if (!IS_COFFEELAKE(dev_priv))
1036 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
1037 ECOCHK_DIS_TLB);
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001038
Ville Syrjälä93564042017-08-24 22:10:51 +03001039 if (HAS_LLC(dev_priv)) {
1040 /* WaCompressedResourceSamplerPbeMediaNewHashMode:skl,kbl
1041 *
1042 * Must match Display Engine. See
1043 * WaCompressedResourceDisplayNewHashMode.
1044 */
1045 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1046 GEN9_PBE_COMPRESSED_HASH_SELECTION);
1047 WA_SET_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN7,
1048 GEN9_SAMPLER_HASH_COMPRESSED_READ_ADDR);
Chris Wilson53221e12017-10-04 13:41:52 +01001049
1050 I915_WRITE(MMCD_MISC_CTRL,
1051 I915_READ(MMCD_MISC_CTRL) |
1052 MMCD_PCLA |
1053 MMCD_HOTSPOT_EN);
Ville Syrjälä93564042017-08-24 22:10:51 +03001054 }
1055
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001056 /* WaClearFlowControlGpgpuContextSave:skl,bxt,kbl,glk,cfl */
1057 /* WaDisablePartialInstShootdown:skl,bxt,kbl,glk,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001058 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
1059 FLOW_CONTROL_ENABLE |
1060 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
1061
1062 /* Syncing dependencies between camera and graphics:skl,bxt,kbl */
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001063 if (!IS_COFFEELAKE(dev_priv))
1064 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
1065 GEN9_DISABLE_OCL_OOB_SUPPRESS_LOGIC);
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001066
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001067 /* WaEnableYV12BugFixInHalfSliceChicken7:skl,bxt,kbl,glk,cfl */
1068 /* WaEnableSamplerGPGPUPreemptionSupport:skl,bxt,kbl,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001069 WA_SET_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN7,
Arkadiusz Hiler0b71cea2017-05-12 13:20:15 +02001070 GEN9_ENABLE_YV12_BUGFIX |
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001071 GEN9_ENABLE_GPGPU_PREEMPTION);
1072
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001073 /* Wa4x4STCOptimizationDisable:skl,bxt,kbl,glk,cfl */
1074 /* WaDisablePartialResolveInVc:skl,bxt,kbl,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001075 WA_SET_BIT_MASKED(CACHE_MODE_1, (GEN8_4x4_STC_OPTIMIZATION_DISABLE |
1076 GEN9_PARTIAL_RESOLVE_IN_VC_DISABLE));
1077
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001078 /* WaCcsTlbPrefetchDisable:skl,bxt,kbl,glk,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001079 WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
1080 GEN9_CCS_TLB_PREFETCH_ENABLE);
1081
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001082 /* WaForceContextSaveRestoreNonCoherent:skl,bxt,kbl,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001083 WA_SET_BIT_MASKED(HDC_CHICKEN0,
1084 HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
1085 HDC_FORCE_CSR_NON_COHERENT_OVR_DISABLE);
1086
1087 /* WaForceEnableNonCoherent and WaDisableHDCInvalidation are
1088 * both tied to WaForceContextSaveRestoreNonCoherent
1089 * in some hsds for skl. We keep the tie for all gen9. The
1090 * documentation is a bit hazy and so we want to get common behaviour,
1091 * even though there is no clear evidence we would need both on kbl/bxt.
1092 * This area has been source of system hangs so we play it safe
1093 * and mimic the skl regardless of what bspec says.
1094 *
1095 * Use Force Non-Coherent whenever executing a 3D context. This
1096 * is a workaround for a possible hang in the unlikely event
1097 * a TLB invalidation occurs during a PSD flush.
1098 */
1099
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001100 /* WaForceEnableNonCoherent:skl,bxt,kbl,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001101 WA_SET_BIT_MASKED(HDC_CHICKEN0,
1102 HDC_FORCE_NON_COHERENT);
1103
Rodrigo Vivi98eed3d2017-06-19 14:21:47 -07001104 /* WaDisableHDCInvalidation:skl,bxt,kbl,cfl */
1105 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
1106 BDW_DISABLE_HDC_INVALIDATION);
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001107
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001108 /* WaDisableSamplerPowerBypassForSOPingPong:skl,bxt,kbl,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001109 if (IS_SKYLAKE(dev_priv) ||
1110 IS_KABYLAKE(dev_priv) ||
Chris Wilsonf3e2b2c2017-11-14 13:43:39 +00001111 IS_COFFEELAKE(dev_priv))
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001112 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
1113 GEN8_SAMPLER_POWER_BYPASS_DIS);
1114
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001115 /* WaDisableSTUnitPowerOptimization:skl,bxt,kbl,glk,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001116 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN2, GEN8_ST_PO_DISABLE);
1117
Valtteri Rantala74368302017-11-28 16:45:05 +02001118 /* WaProgramL3SqcReg1DefaultForPerf:bxt,glk */
1119 if (IS_GEN9_LP(dev_priv)) {
1120 u32 val = I915_READ(GEN8_L3SQCREG1);
1121
1122 val &= ~L3_PRIO_CREDITS_MASK;
1123 val |= L3_GENERAL_PRIO_CREDITS(62) | L3_HIGH_PRIO_CREDITS(2);
1124 I915_WRITE(GEN8_L3SQCREG1, val);
1125 }
1126
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001127 /* WaOCLCoherentLineFlush:skl,bxt,kbl,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001128 I915_WRITE(GEN8_L3SQCREG4, (I915_READ(GEN8_L3SQCREG4) |
1129 GEN8_LQSC_FLUSH_COHERENT_LINES));
1130
Michał Winiarski5152def2017-10-03 21:34:46 +01001131 /*
1132 * Supporting preemption with fine-granularity requires changes in the
1133 * batch buffer programming. Since we can't break old userspace, we
1134 * need to set our default preemption level to safe value. Userspace is
1135 * still able to use more fine-grained preemption levels, since in
1136 * WaEnablePreemptionGranularityControlByUMD we're whitelisting the
1137 * per-ctx register. As such, WaDisable{3D,GPGPU}MidCmdPreemption are
1138 * not real HW workarounds, but merely a way to start using preemption
1139 * while maintaining old contract with userspace.
1140 */
1141
1142 /* WaDisable3DMidCmdPreemption:skl,bxt,glk,cfl,[cnl] */
1143 WA_CLR_BIT_MASKED(GEN8_CS_CHICKEN1, GEN9_PREEMPT_3D_OBJECT_LEVEL);
1144
1145 /* WaDisableGPGPUMidCmdPreemption:skl,bxt,blk,cfl,[cnl] */
1146 WA_SET_FIELD_MASKED(GEN8_CS_CHICKEN1, GEN9_PREEMPT_GPGPU_LEVEL_MASK,
1147 GEN9_PREEMPT_GPGPU_COMMAND_LEVEL);
1148
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001149 /* WaVFEStateAfterPipeControlwithMediaStateClear:skl,bxt,glk,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001150 ret = wa_ring_whitelist_reg(engine, GEN9_CTX_PREEMPT_REG);
1151 if (ret)
1152 return ret;
1153
Jeff McGee1e998342017-10-03 21:34:45 +01001154 /* WaEnablePreemptionGranularityControlByUMD:skl,bxt,kbl,cfl,[cnl] */
1155 I915_WRITE(GEN7_FF_SLICE_CS_CHICKEN1,
1156 _MASKED_BIT_ENABLE(GEN9_FFSC_PERCTX_PREEMPT_CTRL));
1157 ret = wa_ring_whitelist_reg(engine, GEN8_CS_CHICKEN1);
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001158 if (ret)
1159 return ret;
1160
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001161 /* WaAllowUMDToModifyHDCChicken1:skl,bxt,kbl,glk,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001162 ret = wa_ring_whitelist_reg(engine, GEN8_HDC_CHICKEN1);
1163 if (ret)
1164 return ret;
1165
1166 return 0;
1167}
1168
1169static int skl_tune_iz_hashing(struct intel_engine_cs *engine)
1170{
1171 struct drm_i915_private *dev_priv = engine->i915;
1172 u8 vals[3] = { 0, 0, 0 };
1173 unsigned int i;
1174
1175 for (i = 0; i < 3; i++) {
1176 u8 ss;
1177
1178 /*
1179 * Only consider slices where one, and only one, subslice has 7
1180 * EUs
1181 */
1182 if (!is_power_of_2(INTEL_INFO(dev_priv)->sseu.subslice_7eu[i]))
1183 continue;
1184
1185 /*
1186 * subslice_7eu[i] != 0 (because of the check above) and
1187 * ss_max == 4 (maximum number of subslices possible per slice)
1188 *
1189 * -> 0 <= ss <= 3;
1190 */
1191 ss = ffs(INTEL_INFO(dev_priv)->sseu.subslice_7eu[i]) - 1;
1192 vals[i] = 3 - ss;
1193 }
1194
1195 if (vals[0] == 0 && vals[1] == 0 && vals[2] == 0)
1196 return 0;
1197
1198 /* Tune IZ hashing. See intel_device_info_runtime_init() */
1199 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
1200 GEN9_IZ_HASHING_MASK(2) |
1201 GEN9_IZ_HASHING_MASK(1) |
1202 GEN9_IZ_HASHING_MASK(0),
1203 GEN9_IZ_HASHING(2, vals[2]) |
1204 GEN9_IZ_HASHING(1, vals[1]) |
1205 GEN9_IZ_HASHING(0, vals[0]));
1206
1207 return 0;
1208}
1209
1210static int skl_init_workarounds(struct intel_engine_cs *engine)
1211{
1212 struct drm_i915_private *dev_priv = engine->i915;
1213 int ret;
1214
1215 ret = gen9_init_workarounds(engine);
1216 if (ret)
1217 return ret;
1218
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001219 /* WaEnableGapsTsvCreditFix:skl */
1220 I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
1221 GEN9_GAPS_TSV_CREDIT_DISABLE));
1222
1223 /* WaDisableGafsUnitClkGating:skl */
Oscar Mateo4827c542017-09-07 08:40:07 -07001224 I915_WRITE(GEN7_UCGCTL4, (I915_READ(GEN7_UCGCTL4) |
1225 GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE));
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001226
1227 /* WaInPlaceDecompressionHang:skl */
1228 if (IS_SKL_REVID(dev_priv, SKL_REVID_H0, REVID_FOREVER))
Oscar Mateoefc886c2017-09-07 08:40:04 -07001229 I915_WRITE(GEN9_GAMT_ECO_REG_RW_IA,
1230 (I915_READ(GEN9_GAMT_ECO_REG_RW_IA) |
1231 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS));
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001232
1233 /* WaDisableLSQCROPERFforOCL:skl */
1234 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
1235 if (ret)
1236 return ret;
1237
1238 return skl_tune_iz_hashing(engine);
1239}
1240
1241static int bxt_init_workarounds(struct intel_engine_cs *engine)
1242{
1243 struct drm_i915_private *dev_priv = engine->i915;
1244 int ret;
1245
1246 ret = gen9_init_workarounds(engine);
1247 if (ret)
1248 return ret;
1249
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001250 /* WaDisableThreadStallDopClockGating:bxt */
1251 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
1252 STALL_DOP_GATING_DISABLE);
1253
1254 /* WaDisablePooledEuLoadBalancingFix:bxt */
Chris Wilson70a84f32017-11-14 13:43:40 +00001255 I915_WRITE(FF_SLICE_CS_CHICKEN2,
1256 _MASKED_BIT_ENABLE(GEN9_POOLED_EU_LOAD_BALANCING_FIX_DISABLE));
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001257
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001258 /* WaToEnableHwFixForPushConstHWBug:bxt */
Chris Wilson70a84f32017-11-14 13:43:40 +00001259 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1260 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001261
1262 /* WaInPlaceDecompressionHang:bxt */
Chris Wilson70a84f32017-11-14 13:43:40 +00001263 I915_WRITE(GEN9_GAMT_ECO_REG_RW_IA,
1264 (I915_READ(GEN9_GAMT_ECO_REG_RW_IA) |
1265 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS));
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001266
1267 return 0;
1268}
1269
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07001270static int cnl_init_workarounds(struct intel_engine_cs *engine)
1271{
1272 struct drm_i915_private *dev_priv = engine->i915;
1273 int ret;
1274
Oscar Mateo6cf20a02017-09-07 08:40:05 -07001275 /* WaDisableI2mCycleOnWRPort:cnl (pre-prod) */
Rodrigo Vivi86ebb012017-08-29 16:07:51 -07001276 if (IS_CNL_REVID(dev_priv, CNL_REVID_B0, CNL_REVID_B0))
Oscar Mateo6cf20a02017-09-07 08:40:05 -07001277 I915_WRITE(GAMT_CHKN_BIT_REG,
1278 (I915_READ(GAMT_CHKN_BIT_REG) |
1279 GAMT_CHKN_DISABLE_I2M_CYCLE_ON_WR_PORT));
Rodrigo Vivi86ebb012017-08-29 16:07:51 -07001280
Rodrigo Viviacfb5552017-08-23 13:35:04 -07001281 /* WaForceContextSaveRestoreNonCoherent:cnl */
1282 WA_SET_BIT_MASKED(CNL_HDC_CHICKEN0,
1283 HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT);
1284
Rodrigo Viviaa9f4c42017-09-06 15:03:25 -07001285 /* WaThrottleEUPerfToAvoidTDBackPressure:cnl(pre-prod) */
1286 if (IS_CNL_REVID(dev_priv, CNL_REVID_B0, CNL_REVID_B0))
1287 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, THROTTLE_12_5);
1288
Rodrigo Vivie6d1a4f2017-08-15 16:16:49 -07001289 /* WaDisableReplayBufferBankArbitrationOptimization:cnl */
1290 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1291 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
1292
Rodrigo Vivid1d24752017-08-15 16:16:50 -07001293 /* WaDisableEnhancedSBEVertexCaching:cnl (pre-prod) */
1294 if (IS_CNL_REVID(dev_priv, 0, CNL_REVID_B0))
1295 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1296 GEN8_CSC2_SBE_VUE_CACHE_CONSERVATIVE);
1297
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07001298 /* WaInPlaceDecompressionHang:cnl */
Oscar Mateoefc886c2017-09-07 08:40:04 -07001299 I915_WRITE(GEN9_GAMT_ECO_REG_RW_IA,
1300 (I915_READ(GEN9_GAMT_ECO_REG_RW_IA) |
1301 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS));
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07001302
Oscar Mateo2cbecff2017-08-23 12:56:31 -07001303 /* WaPushConstantDereferenceHoldDisable:cnl */
Oscar Mateob27f5902017-09-07 08:40:06 -07001304 WA_SET_BIT_MASKED(GEN7_ROW_CHICKEN2, PUSH_CONSTANT_DEREF_DISABLE);
Oscar Mateo2cbecff2017-08-23 12:56:31 -07001305
Rodrigo Vivi392572f2017-08-29 16:07:23 -07001306 /* FtrEnableFastAnisoL1BankingFix: cnl */
1307 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3, CNL_FAST_ANISO_L1_BANKING_FIX);
1308
Michał Winiarski5152def2017-10-03 21:34:46 +01001309 /* WaDisable3DMidCmdPreemption:cnl */
1310 WA_CLR_BIT_MASKED(GEN8_CS_CHICKEN1, GEN9_PREEMPT_3D_OBJECT_LEVEL);
1311
1312 /* WaDisableGPGPUMidCmdPreemption:cnl */
1313 WA_SET_FIELD_MASKED(GEN8_CS_CHICKEN1, GEN9_PREEMPT_GPGPU_LEVEL_MASK,
1314 GEN9_PREEMPT_GPGPU_COMMAND_LEVEL);
1315
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07001316 /* WaEnablePreemptionGranularityControlByUMD:cnl */
Jeff McGee1e998342017-10-03 21:34:45 +01001317 I915_WRITE(GEN7_FF_SLICE_CS_CHICKEN1,
1318 _MASKED_BIT_ENABLE(GEN9_FFSC_PERCTX_PREEMPT_CTRL));
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07001319 ret= wa_ring_whitelist_reg(engine, GEN8_CS_CHICKEN1);
1320 if (ret)
1321 return ret;
1322
Rafael Antognollia2b16582017-12-15 16:11:17 -08001323 /* WaDisableEarlyEOT:cnl */
1324 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, DISABLE_EARLY_EOT);
1325
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07001326 return 0;
1327}
1328
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001329static int kbl_init_workarounds(struct intel_engine_cs *engine)
1330{
1331 struct drm_i915_private *dev_priv = engine->i915;
1332 int ret;
1333
1334 ret = gen9_init_workarounds(engine);
1335 if (ret)
1336 return ret;
1337
1338 /* WaEnableGapsTsvCreditFix:kbl */
1339 I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
1340 GEN9_GAPS_TSV_CREDIT_DISABLE));
1341
1342 /* WaDisableDynamicCreditSharing:kbl */
1343 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
Oscar Mateoc6ea497c2017-09-07 08:40:08 -07001344 I915_WRITE(GAMT_CHKN_BIT_REG,
1345 (I915_READ(GAMT_CHKN_BIT_REG) |
1346 GAMT_CHKN_DISABLE_DYNAMIC_CREDIT_SHARING));
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001347
1348 /* WaDisableFenceDestinationToSLM:kbl (pre-prod) */
1349 if (IS_KBL_REVID(dev_priv, KBL_REVID_A0, KBL_REVID_A0))
1350 WA_SET_BIT_MASKED(HDC_CHICKEN0,
1351 HDC_FENCE_DEST_SLM_DISABLE);
1352
1353 /* WaToEnableHwFixForPushConstHWBug:kbl */
1354 if (IS_KBL_REVID(dev_priv, KBL_REVID_C0, REVID_FOREVER))
1355 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1356 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
1357
1358 /* WaDisableGafsUnitClkGating:kbl */
Oscar Mateo4827c542017-09-07 08:40:07 -07001359 I915_WRITE(GEN7_UCGCTL4, (I915_READ(GEN7_UCGCTL4) |
1360 GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE));
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001361
1362 /* WaDisableSbeCacheDispatchPortSharing:kbl */
1363 WA_SET_BIT_MASKED(
1364 GEN7_HALF_SLICE_CHICKEN1,
1365 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
1366
1367 /* WaInPlaceDecompressionHang:kbl */
Oscar Mateoefc886c2017-09-07 08:40:04 -07001368 I915_WRITE(GEN9_GAMT_ECO_REG_RW_IA,
1369 (I915_READ(GEN9_GAMT_ECO_REG_RW_IA) |
1370 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS));
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001371
1372 /* WaDisableLSQCROPERFforOCL:kbl */
1373 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
1374 if (ret)
1375 return ret;
1376
1377 return 0;
1378}
1379
1380static int glk_init_workarounds(struct intel_engine_cs *engine)
1381{
1382 struct drm_i915_private *dev_priv = engine->i915;
1383 int ret;
1384
1385 ret = gen9_init_workarounds(engine);
1386 if (ret)
1387 return ret;
1388
Kenneth Graunkeab062632018-01-05 00:59:05 -08001389 /* WA #0862: Userspace has to set "Barrier Mode" to avoid hangs. */
1390 ret = wa_ring_whitelist_reg(engine, GEN9_SLICE_COMMON_ECO_CHICKEN1);
1391 if (ret)
1392 return ret;
1393
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001394 /* WaToEnableHwFixForPushConstHWBug:glk */
1395 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1396 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
1397
1398 return 0;
1399}
1400
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001401static int cfl_init_workarounds(struct intel_engine_cs *engine)
1402{
1403 struct drm_i915_private *dev_priv = engine->i915;
1404 int ret;
1405
1406 ret = gen9_init_workarounds(engine);
1407 if (ret)
1408 return ret;
1409
1410 /* WaEnableGapsTsvCreditFix:cfl */
1411 I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
1412 GEN9_GAPS_TSV_CREDIT_DISABLE));
1413
1414 /* WaToEnableHwFixForPushConstHWBug:cfl */
1415 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1416 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
1417
1418 /* WaDisableGafsUnitClkGating:cfl */
Oscar Mateo4827c542017-09-07 08:40:07 -07001419 I915_WRITE(GEN7_UCGCTL4, (I915_READ(GEN7_UCGCTL4) |
1420 GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE));
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001421
1422 /* WaDisableSbeCacheDispatchPortSharing:cfl */
1423 WA_SET_BIT_MASKED(
1424 GEN7_HALF_SLICE_CHICKEN1,
1425 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
1426
1427 /* WaInPlaceDecompressionHang:cfl */
Oscar Mateoefc886c2017-09-07 08:40:04 -07001428 I915_WRITE(GEN9_GAMT_ECO_REG_RW_IA,
1429 (I915_READ(GEN9_GAMT_ECO_REG_RW_IA) |
1430 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS));
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001431
1432 return 0;
1433}
1434
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001435int init_workarounds_ring(struct intel_engine_cs *engine)
1436{
1437 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson02e012f2017-03-01 12:11:31 +00001438 int err;
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001439
Tvrtko Ursulinae504be2018-01-19 10:00:03 +00001440 if (GEM_WARN_ON(engine->id != RCS))
1441 return -EINVAL;
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001442
1443 dev_priv->workarounds.count = 0;
Chris Wilson02e012f2017-03-01 12:11:31 +00001444 dev_priv->workarounds.hw_whitelist_count[engine->id] = 0;
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001445
1446 if (IS_BROADWELL(dev_priv))
Chris Wilson02e012f2017-03-01 12:11:31 +00001447 err = bdw_init_workarounds(engine);
1448 else if (IS_CHERRYVIEW(dev_priv))
1449 err = chv_init_workarounds(engine);
1450 else if (IS_SKYLAKE(dev_priv))
1451 err = skl_init_workarounds(engine);
1452 else if (IS_BROXTON(dev_priv))
1453 err = bxt_init_workarounds(engine);
1454 else if (IS_KABYLAKE(dev_priv))
1455 err = kbl_init_workarounds(engine);
1456 else if (IS_GEMINILAKE(dev_priv))
1457 err = glk_init_workarounds(engine);
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001458 else if (IS_COFFEELAKE(dev_priv))
1459 err = cfl_init_workarounds(engine);
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07001460 else if (IS_CANNONLAKE(dev_priv))
1461 err = cnl_init_workarounds(engine);
Chris Wilson02e012f2017-03-01 12:11:31 +00001462 else
1463 err = 0;
1464 if (err)
1465 return err;
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001466
Chris Wilson02e012f2017-03-01 12:11:31 +00001467 DRM_DEBUG_DRIVER("%s: Number of context specific w/a: %d\n",
1468 engine->name, dev_priv->workarounds.count);
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001469 return 0;
1470}
1471
Chris Wilsone61e0f52018-02-21 09:56:36 +00001472int intel_ring_workarounds_emit(struct i915_request *rq)
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001473{
Chris Wilsone61e0f52018-02-21 09:56:36 +00001474 struct i915_workarounds *w = &rq->i915->workarounds;
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001475 u32 *cs;
1476 int ret, i;
1477
1478 if (w->count == 0)
1479 return 0;
1480
Chris Wilsone61e0f52018-02-21 09:56:36 +00001481 ret = rq->engine->emit_flush(rq, EMIT_BARRIER);
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001482 if (ret)
1483 return ret;
1484
Chris Wilsone61e0f52018-02-21 09:56:36 +00001485 cs = intel_ring_begin(rq, w->count * 2 + 2);
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001486 if (IS_ERR(cs))
1487 return PTR_ERR(cs);
1488
1489 *cs++ = MI_LOAD_REGISTER_IMM(w->count);
1490 for (i = 0; i < w->count; i++) {
1491 *cs++ = i915_mmio_reg_offset(w->reg[i].addr);
1492 *cs++ = w->reg[i].value;
1493 }
1494 *cs++ = MI_NOOP;
1495
Chris Wilsone61e0f52018-02-21 09:56:36 +00001496 intel_ring_advance(rq, cs);
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001497
Chris Wilsone61e0f52018-02-21 09:56:36 +00001498 ret = rq->engine->emit_flush(rq, EMIT_BARRIER);
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001499 if (ret)
1500 return ret;
1501
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001502 return 0;
1503}
1504
Chris Wilsona091d4e2017-05-30 13:13:33 +01001505static bool ring_is_idle(struct intel_engine_cs *engine)
1506{
1507 struct drm_i915_private *dev_priv = engine->i915;
1508 bool idle = true;
1509
Chris Wilson74d00d22018-02-12 09:39:28 +00001510 /* If the whole device is asleep, the engine must be idle */
1511 if (!intel_runtime_pm_get_if_in_use(dev_priv))
1512 return true;
Chris Wilsona091d4e2017-05-30 13:13:33 +01001513
Chris Wilsonaed2fc12017-05-30 13:13:34 +01001514 /* First check that no commands are left in the ring */
1515 if ((I915_READ_HEAD(engine) & HEAD_ADDR) !=
1516 (I915_READ_TAIL(engine) & TAIL_ADDR))
1517 idle = false;
1518
Chris Wilsona091d4e2017-05-30 13:13:33 +01001519 /* No bit for gen2, so assume the CS parser is idle */
1520 if (INTEL_GEN(dev_priv) > 2 && !(I915_READ_MODE(engine) & MODE_IDLE))
1521 idle = false;
1522
1523 intel_runtime_pm_put(dev_priv);
1524
1525 return idle;
1526}
1527
Chris Wilson54003672017-03-03 12:19:46 +00001528/**
1529 * intel_engine_is_idle() - Report if the engine has finished process all work
1530 * @engine: the intel_engine_cs
1531 *
1532 * Return true if there are no requests pending, nothing left to be submitted
1533 * to hardware, and that the engine is idle.
1534 */
1535bool intel_engine_is_idle(struct intel_engine_cs *engine)
1536{
1537 struct drm_i915_private *dev_priv = engine->i915;
1538
Chris Wilsona8e9a412017-04-11 20:00:42 +01001539 /* More white lies, if wedged, hw state is inconsistent */
1540 if (i915_terminally_wedged(&dev_priv->gpu_error))
1541 return true;
1542
Chris Wilson54003672017-03-03 12:19:46 +00001543 /* Any inflight/incomplete requests? */
1544 if (!i915_seqno_passed(intel_engine_get_seqno(engine),
1545 intel_engine_last_submit(engine)))
1546 return false;
1547
Chris Wilson8968a362017-04-12 00:44:26 +01001548 if (I915_SELFTEST_ONLY(engine->breadcrumbs.mock))
1549 return true;
1550
Chris Wilson4a118ec2017-10-23 22:32:36 +01001551 /* Waiting to drain ELSP? */
1552 if (READ_ONCE(engine->execlists.active))
Chris Wilson54003672017-03-03 12:19:46 +00001553 return false;
1554
Chris Wilsond6edb6e2017-07-21 13:32:24 +01001555 /* ELSP is empty, but there are ready requests? */
Mika Kuoppalab620e872017-09-22 15:43:03 +03001556 if (READ_ONCE(engine->execlists.first))
Chris Wilsond6edb6e2017-07-21 13:32:24 +01001557 return false;
1558
Chris Wilson54003672017-03-03 12:19:46 +00001559 /* Ring stopped? */
Chris Wilsona091d4e2017-05-30 13:13:33 +01001560 if (!ring_is_idle(engine))
Chris Wilson54003672017-03-03 12:19:46 +00001561 return false;
1562
1563 return true;
1564}
1565
Chris Wilson05425242017-03-03 12:19:47 +00001566bool intel_engines_are_idle(struct drm_i915_private *dev_priv)
1567{
1568 struct intel_engine_cs *engine;
1569 enum intel_engine_id id;
1570
Chris Wilsond7dc4132017-12-12 13:21:48 +00001571 /*
1572 * If the driver is wedged, HW state may be very inconsistent and
Chris Wilson8490ae202017-03-30 15:50:37 +01001573 * report that it is still busy, even though we have stopped using it.
1574 */
1575 if (i915_terminally_wedged(&dev_priv->gpu_error))
1576 return true;
1577
Chris Wilson05425242017-03-03 12:19:47 +00001578 for_each_engine(engine, dev_priv, id) {
1579 if (!intel_engine_is_idle(engine))
1580 return false;
1581 }
1582
1583 return true;
1584}
1585
Chris Wilsonae6c4572017-11-10 14:26:28 +00001586/**
1587 * intel_engine_has_kernel_context:
1588 * @engine: the engine
1589 *
1590 * Returns true if the last context to be executed on this engine, or has been
1591 * executed if the engine is already idle, is the kernel context
1592 * (#i915.kernel_context).
1593 */
Chris Wilson20ccd4d2017-10-24 23:08:55 +01001594bool intel_engine_has_kernel_context(const struct intel_engine_cs *engine)
1595{
Chris Wilsonae6c4572017-11-10 14:26:28 +00001596 const struct i915_gem_context * const kernel_context =
1597 engine->i915->kernel_context;
Chris Wilsone61e0f52018-02-21 09:56:36 +00001598 struct i915_request *rq;
Chris Wilsonae6c4572017-11-10 14:26:28 +00001599
1600 lockdep_assert_held(&engine->i915->drm.struct_mutex);
1601
1602 /*
1603 * Check the last context seen by the engine. If active, it will be
1604 * the last request that remains in the timeline. When idle, it is
1605 * the last executed context as tracked by retirement.
1606 */
1607 rq = __i915_gem_active_peek(&engine->timeline->last_request);
1608 if (rq)
1609 return rq->ctx == kernel_context;
1610 else
1611 return engine->last_retired_context == kernel_context;
Chris Wilson20ccd4d2017-10-24 23:08:55 +01001612}
1613
Chris Wilsonff44ad52017-03-16 17:13:03 +00001614void intel_engines_reset_default_submission(struct drm_i915_private *i915)
1615{
1616 struct intel_engine_cs *engine;
1617 enum intel_engine_id id;
1618
1619 for_each_engine(engine, i915, id)
1620 engine->set_default_submission(engine);
1621}
1622
Chris Wilsonaba5e272017-10-25 15:39:41 +01001623/**
1624 * intel_engines_park: called when the GT is transitioning from busy->idle
1625 * @i915: the i915 device
1626 *
1627 * The GT is now idle and about to go to sleep (maybe never to wake again?).
1628 * Time for us to tidy and put away our toys (release resources back to the
1629 * system).
1630 */
1631void intel_engines_park(struct drm_i915_private *i915)
Chris Wilson6c067572017-05-17 13:10:03 +01001632{
1633 struct intel_engine_cs *engine;
1634 enum intel_engine_id id;
1635
1636 for_each_engine(engine, i915, id) {
Chris Wilson820c5bb2017-11-01 20:21:49 +00001637 /* Flush the residual irq tasklets first. */
1638 intel_engine_disarm_breadcrumbs(engine);
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05301639 tasklet_kill(&engine->execlists.tasklet);
Chris Wilson820c5bb2017-11-01 20:21:49 +00001640
Chris Wilson32651242017-10-27 12:06:17 +01001641 /*
1642 * We are committed now to parking the engines, make sure there
1643 * will be no more interrupts arriving later and the engines
1644 * are truly idle.
1645 */
Chris Wilson30b29402017-11-10 11:25:50 +00001646 if (wait_for(intel_engine_is_idle(engine), 10)) {
Chris Wilson32651242017-10-27 12:06:17 +01001647 struct drm_printer p = drm_debug_printer(__func__);
1648
Chris Wilson30b29402017-11-10 11:25:50 +00001649 dev_err(i915->drm.dev,
1650 "%s is not idle before parking\n",
1651 engine->name);
Chris Wilson0db18b12017-12-08 01:23:00 +00001652 intel_engine_dump(engine, &p, NULL);
Chris Wilson32651242017-10-27 12:06:17 +01001653 }
1654
Chris Wilsonaba5e272017-10-25 15:39:41 +01001655 if (engine->park)
1656 engine->park(engine);
1657
Chris Wilsonaba5e272017-10-25 15:39:41 +01001658 i915_gem_batch_pool_fini(&engine->batch_pool);
Mika Kuoppalab620e872017-09-22 15:43:03 +03001659 engine->execlists.no_priolist = false;
Chris Wilson6c067572017-05-17 13:10:03 +01001660 }
1661}
1662
Chris Wilsonaba5e272017-10-25 15:39:41 +01001663/**
1664 * intel_engines_unpark: called when the GT is transitioning from idle->busy
1665 * @i915: the i915 device
1666 *
1667 * The GT was idle and now about to fire up with some new user requests.
1668 */
1669void intel_engines_unpark(struct drm_i915_private *i915)
1670{
1671 struct intel_engine_cs *engine;
1672 enum intel_engine_id id;
1673
1674 for_each_engine(engine, i915, id) {
1675 if (engine->unpark)
1676 engine->unpark(engine);
1677 }
1678}
1679
Chris Wilson90cad092017-09-06 16:28:59 +01001680bool intel_engine_can_store_dword(struct intel_engine_cs *engine)
1681{
1682 switch (INTEL_GEN(engine->i915)) {
1683 case 2:
1684 return false; /* uses physical not virtual addresses */
1685 case 3:
1686 /* maybe only uses physical not virtual addresses */
1687 return !(IS_I915G(engine->i915) || IS_I915GM(engine->i915));
1688 case 6:
1689 return engine->class != VIDEO_DECODE_CLASS; /* b0rked */
1690 default:
1691 return true;
1692 }
1693}
1694
Chris Wilsond2b4b972017-11-10 14:26:33 +00001695unsigned int intel_engines_has_context_isolation(struct drm_i915_private *i915)
1696{
1697 struct intel_engine_cs *engine;
1698 enum intel_engine_id id;
1699 unsigned int which;
1700
1701 which = 0;
1702 for_each_engine(engine, i915, id)
1703 if (engine->default_state)
1704 which |= BIT(engine->uabi_class);
1705
1706 return which;
1707}
1708
Chris Wilsonf636edb2017-10-09 12:02:57 +01001709static void print_request(struct drm_printer *m,
Chris Wilsone61e0f52018-02-21 09:56:36 +00001710 struct i915_request *rq,
Chris Wilsonf636edb2017-10-09 12:02:57 +01001711 const char *prefix)
1712{
Chris Wilson367a35a2018-02-28 09:47:32 +00001713 drm_printf(m, "%s%x%s [%llx:%x] prio=%d @ %dms: %s\n", prefix,
Chris Wilsona27d5a42017-10-15 21:43:10 +01001714 rq->global_seqno,
Chris Wilsone61e0f52018-02-21 09:56:36 +00001715 i915_request_completed(rq) ? "!" : "",
Chris Wilson367a35a2018-02-28 09:47:32 +00001716 rq->fence.context, rq->fence.seqno,
Chris Wilsonf636edb2017-10-09 12:02:57 +01001717 rq->priotree.priority,
1718 jiffies_to_msecs(jiffies - rq->emitted_jiffies),
1719 rq->timeline->common->name);
1720}
1721
Chris Wilsonc1bf2722017-12-22 18:25:21 +00001722static void hexdump(struct drm_printer *m, const void *buf, size_t len)
1723{
1724 const size_t rowsize = 8 * sizeof(u32);
1725 const void *prev = NULL;
1726 bool skip = false;
1727 size_t pos;
1728
1729 for (pos = 0; pos < len; pos += rowsize) {
1730 char line[128];
1731
1732 if (prev && !memcmp(prev, buf + pos, rowsize)) {
1733 if (!skip) {
1734 drm_printf(m, "*\n");
1735 skip = true;
1736 }
1737 continue;
1738 }
1739
1740 WARN_ON_ONCE(hex_dump_to_buffer(buf + pos, len - pos,
1741 rowsize, sizeof(u32),
1742 line, sizeof(line),
1743 false) >= sizeof(line));
1744 drm_printf(m, "%08zx %s\n", pos, line);
1745
1746 prev = buf + pos;
1747 skip = false;
1748 }
1749}
1750
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001751static void intel_engine_print_registers(const struct intel_engine_cs *engine,
1752 struct drm_printer *m)
Chris Wilsonf636edb2017-10-09 12:02:57 +01001753{
Chris Wilsonf636edb2017-10-09 12:02:57 +01001754 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001755 const struct intel_engine_execlists * const execlists =
1756 &engine->execlists;
Chris Wilsonf636edb2017-10-09 12:02:57 +01001757 u64 addr;
1758
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001759 drm_printf(m, "\tRING_START: 0x%08x\n",
1760 I915_READ(RING_START(engine->mmio_base)));
1761 drm_printf(m, "\tRING_HEAD: 0x%08x\n",
1762 I915_READ(RING_HEAD(engine->mmio_base)) & HEAD_ADDR);
1763 drm_printf(m, "\tRING_TAIL: 0x%08x\n",
1764 I915_READ(RING_TAIL(engine->mmio_base)) & TAIL_ADDR);
Chris Wilson3c75de52017-10-26 12:50:48 +01001765 drm_printf(m, "\tRING_CTL: 0x%08x%s\n",
Chris Wilsonf636edb2017-10-09 12:02:57 +01001766 I915_READ(RING_CTL(engine->mmio_base)),
Chris Wilson3c75de52017-10-26 12:50:48 +01001767 I915_READ(RING_CTL(engine->mmio_base)) & (RING_WAIT | RING_WAIT_SEMAPHORE) ? " [waiting]" : "");
1768 if (INTEL_GEN(engine->i915) > 2) {
1769 drm_printf(m, "\tRING_MODE: 0x%08x%s\n",
1770 I915_READ(RING_MI_MODE(engine->mmio_base)),
1771 I915_READ(RING_MI_MODE(engine->mmio_base)) & (MODE_IDLE) ? " [idle]" : "");
1772 }
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001773
1774 if (INTEL_GEN(dev_priv) >= 6) {
1775 drm_printf(m, "\tRING_IMR: %08x\n", I915_READ_IMR(engine));
1776 }
1777
Chris Wilson93c6e962017-11-20 20:55:04 +00001778 if (HAS_LEGACY_SEMAPHORES(dev_priv)) {
Chris Wilsonaf9ff6c2017-11-20 20:55:03 +00001779 drm_printf(m, "\tSYNC_0: 0x%08x\n",
1780 I915_READ(RING_SYNC_0(engine->mmio_base)));
1781 drm_printf(m, "\tSYNC_1: 0x%08x\n",
1782 I915_READ(RING_SYNC_1(engine->mmio_base)));
1783 if (HAS_VEBOX(dev_priv))
1784 drm_printf(m, "\tSYNC_2: 0x%08x\n",
1785 I915_READ(RING_SYNC_2(engine->mmio_base)));
1786 }
Chris Wilsonf636edb2017-10-09 12:02:57 +01001787
Chris Wilsonf636edb2017-10-09 12:02:57 +01001788 addr = intel_engine_get_active_head(engine);
1789 drm_printf(m, "\tACTHD: 0x%08x_%08x\n",
1790 upper_32_bits(addr), lower_32_bits(addr));
1791 addr = intel_engine_get_last_batch_head(engine);
1792 drm_printf(m, "\tBBADDR: 0x%08x_%08x\n",
1793 upper_32_bits(addr), lower_32_bits(addr));
Chris Wilsona0cf5792017-12-18 12:39:14 +00001794 if (INTEL_GEN(dev_priv) >= 8)
1795 addr = I915_READ64_2x32(RING_DMA_FADD(engine->mmio_base),
1796 RING_DMA_FADD_UDW(engine->mmio_base));
1797 else if (INTEL_GEN(dev_priv) >= 4)
1798 addr = I915_READ(RING_DMA_FADD(engine->mmio_base));
1799 else
1800 addr = I915_READ(DMA_FADD_I8XX);
1801 drm_printf(m, "\tDMA_FADDR: 0x%08x_%08x\n",
1802 upper_32_bits(addr), lower_32_bits(addr));
1803 if (INTEL_GEN(dev_priv) >= 4) {
1804 drm_printf(m, "\tIPEIR: 0x%08x\n",
1805 I915_READ(RING_IPEIR(engine->mmio_base)));
1806 drm_printf(m, "\tIPEHR: 0x%08x\n",
1807 I915_READ(RING_IPEHR(engine->mmio_base)));
1808 } else {
1809 drm_printf(m, "\tIPEIR: 0x%08x\n", I915_READ(IPEIR));
1810 drm_printf(m, "\tIPEHR: 0x%08x\n", I915_READ(IPEHR));
1811 }
Chris Wilsonf636edb2017-10-09 12:02:57 +01001812
Chris Wilsonfb5c5512017-11-20 20:55:00 +00001813 if (HAS_EXECLISTS(dev_priv)) {
Chris Wilsonf636edb2017-10-09 12:02:57 +01001814 const u32 *hws = &engine->status_page.page_addr[I915_HWS_CSB_BUF0_INDEX];
Chris Wilsonf636edb2017-10-09 12:02:57 +01001815 u32 ptr, read, write;
1816 unsigned int idx;
1817
1818 drm_printf(m, "\tExeclist status: 0x%08x %08x\n",
1819 I915_READ(RING_EXECLIST_STATUS_LO(engine)),
1820 I915_READ(RING_EXECLIST_STATUS_HI(engine)));
1821
1822 ptr = I915_READ(RING_CONTEXT_STATUS_PTR(engine));
1823 read = GEN8_CSB_READ_PTR(ptr);
1824 write = GEN8_CSB_WRITE_PTR(ptr);
1825 drm_printf(m, "\tExeclist CSB read %d [%d cached], write %d [%d from hws], interrupt posted? %s\n",
1826 read, execlists->csb_head,
1827 write,
1828 intel_read_status_page(engine, intel_hws_csb_write_index(engine->i915)),
1829 yesno(test_bit(ENGINE_IRQ_EXECLIST,
1830 &engine->irq_posted)));
1831 if (read >= GEN8_CSB_ENTRIES)
1832 read = 0;
1833 if (write >= GEN8_CSB_ENTRIES)
1834 write = 0;
1835 if (read > write)
1836 write += GEN8_CSB_ENTRIES;
1837 while (read < write) {
1838 idx = ++read % GEN8_CSB_ENTRIES;
1839 drm_printf(m, "\tExeclist CSB[%d]: 0x%08x [0x%08x in hwsp], context: %d [%d in hwsp]\n",
1840 idx,
1841 I915_READ(RING_CONTEXT_STATUS_BUF_LO(engine, idx)),
1842 hws[idx * 2],
1843 I915_READ(RING_CONTEXT_STATUS_BUF_HI(engine, idx)),
1844 hws[idx * 2 + 1]);
1845 }
1846
1847 rcu_read_lock();
1848 for (idx = 0; idx < execlists_num_ports(execlists); idx++) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00001849 struct i915_request *rq;
Chris Wilsonf636edb2017-10-09 12:02:57 +01001850 unsigned int count;
1851
1852 rq = port_unpack(&execlists->port[idx], &count);
1853 if (rq) {
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001854 char hdr[80];
1855
Chris Wilsone8a70ca2017-12-08 01:22:59 +00001856 snprintf(hdr, sizeof(hdr),
1857 "\t\tELSP[%d] count=%d, rq: ",
1858 idx, count);
1859 print_request(m, rq, hdr);
Chris Wilsonf636edb2017-10-09 12:02:57 +01001860 } else {
Chris Wilsone8a70ca2017-12-08 01:22:59 +00001861 drm_printf(m, "\t\tELSP[%d] idle\n", idx);
Chris Wilsonf636edb2017-10-09 12:02:57 +01001862 }
1863 }
Chris Wilson4a118ec2017-10-23 22:32:36 +01001864 drm_printf(m, "\t\tHW active? 0x%x\n", execlists->active);
Chris Wilsonf636edb2017-10-09 12:02:57 +01001865 rcu_read_unlock();
Chris Wilsonf636edb2017-10-09 12:02:57 +01001866 } else if (INTEL_GEN(dev_priv) > 6) {
1867 drm_printf(m, "\tPP_DIR_BASE: 0x%08x\n",
1868 I915_READ(RING_PP_DIR_BASE(engine)));
1869 drm_printf(m, "\tPP_DIR_BASE_READ: 0x%08x\n",
1870 I915_READ(RING_PP_DIR_BASE_READ(engine)));
1871 drm_printf(m, "\tPP_DIR_DCLV: 0x%08x\n",
1872 I915_READ(RING_PP_DIR_DCLV(engine)));
1873 }
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001874}
1875
1876void intel_engine_dump(struct intel_engine_cs *engine,
1877 struct drm_printer *m,
1878 const char *header, ...)
1879{
1880 struct intel_breadcrumbs * const b = &engine->breadcrumbs;
1881 const struct intel_engine_execlists * const execlists = &engine->execlists;
1882 struct i915_gpu_error * const error = &engine->i915->gpu_error;
Chris Wilsone61e0f52018-02-21 09:56:36 +00001883 struct i915_request *rq;
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001884 struct rb_node *rb;
1885
1886 if (header) {
1887 va_list ap;
1888
1889 va_start(ap, header);
1890 drm_vprintf(m, header, &ap);
1891 va_end(ap);
1892 }
1893
1894 if (i915_terminally_wedged(&engine->i915->gpu_error))
1895 drm_printf(m, "*** WEDGED ***\n");
1896
1897 drm_printf(m, "\tcurrent seqno %x, last %x, hangcheck %x [%d ms], inflight %d\n",
1898 intel_engine_get_seqno(engine),
1899 intel_engine_last_submit(engine),
1900 engine->hangcheck.seqno,
1901 jiffies_to_msecs(jiffies - engine->hangcheck.action_timestamp),
1902 engine->timeline->inflight_seqnos);
1903 drm_printf(m, "\tReset count: %d (global %d)\n",
1904 i915_reset_engine_count(error, engine),
1905 i915_reset_count(error));
1906
1907 rcu_read_lock();
1908
1909 drm_printf(m, "\tRequests:\n");
1910
1911 rq = list_first_entry(&engine->timeline->requests,
Chris Wilsone61e0f52018-02-21 09:56:36 +00001912 struct i915_request, link);
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001913 if (&rq->link != &engine->timeline->requests)
1914 print_request(m, rq, "\t\tfirst ");
1915
1916 rq = list_last_entry(&engine->timeline->requests,
Chris Wilsone61e0f52018-02-21 09:56:36 +00001917 struct i915_request, link);
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001918 if (&rq->link != &engine->timeline->requests)
1919 print_request(m, rq, "\t\tlast ");
1920
1921 rq = i915_gem_find_active_request(engine);
1922 if (rq) {
1923 print_request(m, rq, "\t\tactive ");
1924 drm_printf(m,
1925 "\t\t[head %04x, postfix %04x, tail %04x, batch 0x%08x_%08x]\n",
1926 rq->head, rq->postfix, rq->tail,
1927 rq->batch ? upper_32_bits(rq->batch->node.start) : ~0u,
1928 rq->batch ? lower_32_bits(rq->batch->node.start) : ~0u);
1929 drm_printf(m, "\t\tring->start: 0x%08x\n",
1930 i915_ggtt_offset(rq->ring->vma));
1931 drm_printf(m, "\t\tring->head: 0x%08x\n",
1932 rq->ring->head);
1933 drm_printf(m, "\t\tring->tail: 0x%08x\n",
1934 rq->ring->tail);
1935 }
1936
1937 rcu_read_unlock();
1938
1939 if (intel_runtime_pm_get_if_in_use(engine->i915)) {
1940 intel_engine_print_registers(engine, m);
1941 intel_runtime_pm_put(engine->i915);
1942 } else {
1943 drm_printf(m, "\tDevice is asleep; skipping register dump\n");
1944 }
Chris Wilsonf636edb2017-10-09 12:02:57 +01001945
Chris Wilsona27d5a42017-10-15 21:43:10 +01001946 spin_lock_irq(&engine->timeline->lock);
1947 list_for_each_entry(rq, &engine->timeline->requests, link)
1948 print_request(m, rq, "\t\tE ");
Chris Wilsonf6322ed2018-02-22 14:22:29 +00001949 drm_printf(m, "\t\tQueue priority: %d\n", execlists->queue_priority);
Chris Wilsona27d5a42017-10-15 21:43:10 +01001950 for (rb = execlists->first; rb; rb = rb_next(rb)) {
1951 struct i915_priolist *p =
1952 rb_entry(rb, typeof(*p), node);
1953
1954 list_for_each_entry(rq, &p->requests, priotree.link)
1955 print_request(m, rq, "\t\tQ ");
1956 }
1957 spin_unlock_irq(&engine->timeline->lock);
1958
Chris Wilsonf636edb2017-10-09 12:02:57 +01001959 spin_lock_irq(&b->rb_lock);
1960 for (rb = rb_first(&b->waiters); rb; rb = rb_next(rb)) {
1961 struct intel_wait *w = rb_entry(rb, typeof(*w), node);
1962
1963 drm_printf(m, "\t%s [%d] waiting for %x\n",
1964 w->tsk->comm, w->tsk->pid, w->seqno);
1965 }
1966 spin_unlock_irq(&b->rb_lock);
1967
Chris Wilson832265d2017-12-08 01:23:01 +00001968 drm_printf(m, "IRQ? 0x%lx (breadcrumbs? %s) (execlists? %s)\n",
1969 engine->irq_posted,
1970 yesno(test_bit(ENGINE_IRQ_BREADCRUMB,
1971 &engine->irq_posted)),
1972 yesno(test_bit(ENGINE_IRQ_EXECLIST,
1973 &engine->irq_posted)));
Chris Wilsonc1bf2722017-12-22 18:25:21 +00001974
1975 drm_printf(m, "HWSP:\n");
1976 hexdump(m, engine->status_page.page_addr, PAGE_SIZE);
1977
Chris Wilsonc400cc22017-11-07 15:22:11 +00001978 drm_printf(m, "Idle? %s\n", yesno(intel_engine_is_idle(engine)));
Chris Wilsonf636edb2017-10-09 12:02:57 +01001979}
1980
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +00001981static u8 user_class_map[] = {
1982 [I915_ENGINE_CLASS_RENDER] = RENDER_CLASS,
1983 [I915_ENGINE_CLASS_COPY] = COPY_ENGINE_CLASS,
1984 [I915_ENGINE_CLASS_VIDEO] = VIDEO_DECODE_CLASS,
1985 [I915_ENGINE_CLASS_VIDEO_ENHANCE] = VIDEO_ENHANCEMENT_CLASS,
1986};
1987
1988struct intel_engine_cs *
1989intel_engine_lookup_user(struct drm_i915_private *i915, u8 class, u8 instance)
1990{
1991 if (class >= ARRAY_SIZE(user_class_map))
1992 return NULL;
1993
1994 class = user_class_map[class];
1995
1996 GEM_BUG_ON(class > MAX_ENGINE_CLASS);
1997
1998 if (instance > MAX_ENGINE_INSTANCE)
1999 return NULL;
2000
2001 return i915->engine_class[class][instance];
2002}
2003
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00002004/**
2005 * intel_enable_engine_stats() - Enable engine busy tracking on engine
2006 * @engine: engine to enable stats collection
2007 *
2008 * Start collecting the engine busyness data for @engine.
2009 *
2010 * Returns 0 on success or a negative error code.
2011 */
2012int intel_enable_engine_stats(struct intel_engine_cs *engine)
2013{
Chris Wilson99e48bf2018-01-15 09:20:41 +00002014 struct intel_engine_execlists *execlists = &engine->execlists;
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00002015 unsigned long flags;
Chris Wilson99e48bf2018-01-15 09:20:41 +00002016 int err = 0;
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00002017
Tvrtko Ursulincf669b42017-11-29 10:28:05 +00002018 if (!intel_engine_supports_stats(engine))
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00002019 return -ENODEV;
2020
Chris Wilson99e48bf2018-01-15 09:20:41 +00002021 tasklet_disable(&execlists->tasklet);
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00002022 spin_lock_irqsave(&engine->stats.lock, flags);
Chris Wilson99e48bf2018-01-15 09:20:41 +00002023
2024 if (unlikely(engine->stats.enabled == ~0)) {
2025 err = -EBUSY;
2026 goto unlock;
2027 }
2028
Chris Wilson49007272018-01-11 07:30:31 +00002029 if (engine->stats.enabled++ == 0) {
Chris Wilson49007272018-01-11 07:30:31 +00002030 const struct execlist_port *port = execlists->port;
2031 unsigned int num_ports = execlists_num_ports(execlists);
2032
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00002033 engine->stats.enabled_at = ktime_get();
Chris Wilson49007272018-01-11 07:30:31 +00002034
2035 /* XXX submission method oblivious? */
2036 while (num_ports-- && port_isset(port)) {
2037 engine->stats.active++;
2038 port++;
2039 }
2040
2041 if (engine->stats.active)
2042 engine->stats.start = engine->stats.enabled_at;
2043 }
Chris Wilson99e48bf2018-01-15 09:20:41 +00002044
2045unlock:
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00002046 spin_unlock_irqrestore(&engine->stats.lock, flags);
Chris Wilson99e48bf2018-01-15 09:20:41 +00002047 tasklet_enable(&execlists->tasklet);
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00002048
Chris Wilson99e48bf2018-01-15 09:20:41 +00002049 return err;
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00002050}
2051
2052static ktime_t __intel_engine_get_busy_time(struct intel_engine_cs *engine)
2053{
2054 ktime_t total = engine->stats.total;
2055
2056 /*
2057 * If the engine is executing something at the moment
2058 * add it to the total.
2059 */
2060 if (engine->stats.active)
2061 total = ktime_add(total,
2062 ktime_sub(ktime_get(), engine->stats.start));
2063
2064 return total;
2065}
2066
2067/**
2068 * intel_engine_get_busy_time() - Return current accumulated engine busyness
2069 * @engine: engine to report on
2070 *
2071 * Returns accumulated time @engine was busy since engine stats were enabled.
2072 */
2073ktime_t intel_engine_get_busy_time(struct intel_engine_cs *engine)
2074{
2075 ktime_t total;
2076 unsigned long flags;
2077
2078 spin_lock_irqsave(&engine->stats.lock, flags);
2079 total = __intel_engine_get_busy_time(engine);
2080 spin_unlock_irqrestore(&engine->stats.lock, flags);
2081
2082 return total;
2083}
2084
2085/**
2086 * intel_disable_engine_stats() - Disable engine busy tracking on engine
2087 * @engine: engine to disable stats collection
2088 *
2089 * Stops collecting the engine busyness data for @engine.
2090 */
2091void intel_disable_engine_stats(struct intel_engine_cs *engine)
2092{
2093 unsigned long flags;
2094
Tvrtko Ursulincf669b42017-11-29 10:28:05 +00002095 if (!intel_engine_supports_stats(engine))
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00002096 return;
2097
2098 spin_lock_irqsave(&engine->stats.lock, flags);
2099 WARN_ON_ONCE(engine->stats.enabled == 0);
2100 if (--engine->stats.enabled == 0) {
2101 engine->stats.total = __intel_engine_get_busy_time(engine);
2102 engine->stats.active = 0;
2103 }
2104 spin_unlock_irqrestore(&engine->stats.lock, flags);
2105}
2106
Chris Wilsonf97fbf92017-02-13 17:15:14 +00002107#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
2108#include "selftests/mock_engine.c"
2109#endif