blob: f22c5f72df8d947848ec49f034e3cf2d5666e3c5 [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
Daniele Ceraolo Spurioac52da62018-03-02 18:14:58 +0200237 BUILD_BUG_ON(MAX_ENGINE_CLASS >= BIT(GEN11_ENGINE_CLASS_WIDTH));
238 BUILD_BUG_ON(MAX_ENGINE_INSTANCE >= BIT(GEN11_ENGINE_INSTANCE_WIDTH));
239
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000240 if (GEM_WARN_ON(info->class > MAX_ENGINE_CLASS))
241 return -EINVAL;
242
243 if (GEM_WARN_ON(info->instance > MAX_ENGINE_INSTANCE))
244 return -EINVAL;
245
246 if (GEM_WARN_ON(dev_priv->engine_class[info->class][info->instance]))
247 return -EINVAL;
248
Akash Goel3b3f1652016-10-13 22:44:48 +0530249 GEM_BUG_ON(dev_priv->engine[id]);
250 engine = kzalloc(sizeof(*engine), GFP_KERNEL);
251 if (!engine)
252 return -ENOMEM;
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100253
254 engine->id = id;
255 engine->i915 = dev_priv;
Oscar Mateo6e516142017-04-10 07:34:31 -0700256 WARN_ON(snprintf(engine->name, sizeof(engine->name), "%s%u",
Oscar Mateob8400f02017-04-10 07:34:32 -0700257 class_info->name, info->instance) >=
258 sizeof(engine->name));
Tvrtko Ursulin5ec2cf72016-08-16 17:04:20 +0100259 engine->hw_id = engine->guc_id = info->hw_id;
Oscar Mateo5f79e7c2018-03-02 18:14:57 +0200260 if (INTEL_GEN(dev_priv) >= 11) {
261 switch (engine->id) {
262 case VCS:
263 engine->mmio_base = GEN11_BSD_RING_BASE;
264 break;
265 case VCS2:
266 engine->mmio_base = GEN11_BSD2_RING_BASE;
267 break;
268 case VECS:
269 engine->mmio_base = GEN11_VEBOX_RING_BASE;
270 break;
271 default:
272 /* take the original value for all other engines */
273 engine->mmio_base = info->mmio_base;
274 break;
275 }
276 } else {
277 engine->mmio_base = info->mmio_base;
278 }
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100279 engine->irq_shift = info->irq_shift;
Daniele Ceraolo Spurio09081802017-04-10 07:34:29 -0700280 engine->class = info->class;
281 engine->instance = info->instance;
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100282
Tvrtko Ursulin1803fcbc2017-11-10 14:26:27 +0000283 engine->uabi_id = info->uabi_id;
284 engine->uabi_class = class_info->uabi_class;
285
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +0300286 engine->context_size = __intel_engine_context_size(dev_priv,
287 engine->class);
288 if (WARN_ON(engine->context_size > BIT(20)))
289 engine->context_size = 0;
290
Chris Wilson0de91362016-11-14 20:41:01 +0000291 /* Nothing to do here, execute in order of dependencies */
292 engine->schedule = NULL;
293
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +0000294 spin_lock_init(&engine->stats.lock);
295
Changbin Du3fc03062017-03-13 10:47:11 +0800296 ATOMIC_INIT_NOTIFIER_HEAD(&engine->context_status_notifier);
297
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000298 dev_priv->engine_class[info->class][info->instance] = engine;
Akash Goel3b3f1652016-10-13 22:44:48 +0530299 dev_priv->engine[id] = engine;
300 return 0;
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100301}
302
303/**
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +0300304 * intel_engines_init_mmio() - allocate and prepare the Engine Command Streamers
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +0000305 * @dev_priv: i915 device private
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100306 *
307 * Return: non-zero if the initialization failed.
308 */
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +0300309int intel_engines_init_mmio(struct drm_i915_private *dev_priv)
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100310{
Tvrtko Ursulinc1bb1142016-08-10 16:22:10 +0100311 struct intel_device_info *device_info = mkwrite_device_info(dev_priv);
Chris Wilson5f9be052017-04-11 17:56:58 +0100312 const unsigned int ring_mask = INTEL_INFO(dev_priv)->ring_mask;
Akash Goel3b3f1652016-10-13 22:44:48 +0530313 struct intel_engine_cs *engine;
314 enum intel_engine_id id;
Chris Wilson5f9be052017-04-11 17:56:58 +0100315 unsigned int mask = 0;
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100316 unsigned int i;
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000317 int err;
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100318
Tvrtko Ursulin70006ad2016-10-13 11:02:56 +0100319 WARN_ON(ring_mask == 0);
320 WARN_ON(ring_mask &
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100321 GENMASK(sizeof(mask) * BITS_PER_BYTE - 1, I915_NUM_ENGINES));
322
323 for (i = 0; i < ARRAY_SIZE(intel_engines); i++) {
324 if (!HAS_ENGINE(dev_priv, i))
325 continue;
326
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000327 err = intel_engine_setup(dev_priv, i);
328 if (err)
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100329 goto cleanup;
330
331 mask |= ENGINE_MASK(i);
332 }
333
334 /*
335 * Catch failures to update intel_engines table when the new engines
336 * are added to the driver by a warning and disabling the forgotten
337 * engines.
338 */
Tvrtko Ursulin70006ad2016-10-13 11:02:56 +0100339 if (WARN_ON(mask != ring_mask))
Tvrtko Ursulinc1bb1142016-08-10 16:22:10 +0100340 device_info->ring_mask = mask;
341
Chris Wilson5f9be052017-04-11 17:56:58 +0100342 /* We always presume we have at least RCS available for later probing */
343 if (WARN_ON(!HAS_ENGINE(dev_priv, RCS))) {
344 err = -ENODEV;
345 goto cleanup;
346 }
347
Tvrtko Ursulinc1bb1142016-08-10 16:22:10 +0100348 device_info->num_rings = hweight32(mask);
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100349
Michel Thierryce453b32017-11-10 16:44:47 -0800350 i915_check_and_clear_faults(dev_priv);
351
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100352 return 0;
353
354cleanup:
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000355 for_each_engine(engine, dev_priv, id)
356 kfree(engine);
357 return err;
358}
359
360/**
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +0300361 * intel_engines_init() - init the Engine Command Streamers
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000362 * @dev_priv: i915 device private
363 *
364 * Return: non-zero if the initialization failed.
365 */
366int intel_engines_init(struct drm_i915_private *dev_priv)
367{
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000368 struct intel_engine_cs *engine;
369 enum intel_engine_id id, err_id;
Tvrtko Ursulin33def1f2017-06-16 14:03:38 +0100370 int err;
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000371
Akash Goel3b3f1652016-10-13 22:44:48 +0530372 for_each_engine(engine, dev_priv, id) {
Oscar Mateob8400f02017-04-10 07:34:32 -0700373 const struct engine_class_info *class_info =
374 &intel_engine_classes[engine->class];
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000375 int (*init)(struct intel_engine_cs *engine);
376
Chris Wilsonfb5c5512017-11-20 20:55:00 +0000377 if (HAS_EXECLISTS(dev_priv))
Oscar Mateob8400f02017-04-10 07:34:32 -0700378 init = class_info->init_execlists;
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000379 else
Oscar Mateob8400f02017-04-10 07:34:32 -0700380 init = class_info->init_legacy;
Tvrtko Ursulin33def1f2017-06-16 14:03:38 +0100381
382 err = -EINVAL;
383 err_id = id;
384
385 if (GEM_WARN_ON(!init))
386 goto cleanup;
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000387
388 err = init(engine);
Tvrtko Ursulin33def1f2017-06-16 14:03:38 +0100389 if (err)
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000390 goto cleanup;
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000391
Chris Wilsonff44ad52017-03-16 17:13:03 +0000392 GEM_BUG_ON(!engine->submit_request);
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000393 }
394
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000395 return 0;
396
397cleanup:
398 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursulin33def1f2017-06-16 14:03:38 +0100399 if (id >= err_id) {
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000400 kfree(engine);
Tvrtko Ursulin33def1f2017-06-16 14:03:38 +0100401 dev_priv->engine[id] = NULL;
402 } else {
Tvrtko Ursulin8ee7c6e2017-02-16 12:23:22 +0000403 dev_priv->gt.cleanup_engine(engine);
Tvrtko Ursulin33def1f2017-06-16 14:03:38 +0100404 }
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100405 }
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000406 return err;
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100407}
408
Chris Wilson73cb9702016-10-28 13:58:46 +0100409void intel_engine_init_global_seqno(struct intel_engine_cs *engine, u32 seqno)
Chris Wilson57f275a2016-08-15 10:49:00 +0100410{
411 struct drm_i915_private *dev_priv = engine->i915;
412
413 /* Our semaphore implementation is strictly monotonic (i.e. we proceed
414 * so long as the semaphore value in the register/page is greater
415 * than the sync value), so whenever we reset the seqno,
416 * so long as we reset the tracking semaphore value to 0, it will
417 * always be before the next request's seqno. If we don't reset
418 * the semaphore value, then when the seqno moves backwards all
419 * future waits will complete instantly (causing rendering corruption).
420 */
421 if (IS_GEN6(dev_priv) || IS_GEN7(dev_priv)) {
422 I915_WRITE(RING_SYNC_0(engine->mmio_base), 0);
423 I915_WRITE(RING_SYNC_1(engine->mmio_base), 0);
424 if (HAS_VEBOX(dev_priv))
425 I915_WRITE(RING_SYNC_2(engine->mmio_base), 0);
426 }
Chris Wilson57f275a2016-08-15 10:49:00 +0100427
428 intel_write_status_page(engine, I915_GEM_HWS_INDEX, seqno);
Chris Wilson14a6bbf2017-03-14 11:14:52 +0000429 clear_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted);
Chris Wilson73cb9702016-10-28 13:58:46 +0100430
Chris Wilson57f275a2016-08-15 10:49:00 +0100431 /* After manually advancing the seqno, fake the interrupt in case
432 * there are any waiters for that seqno.
433 */
434 intel_engine_wakeup(engine);
Chris Wilson2ca9faa2017-04-05 16:30:54 +0100435
436 GEM_BUG_ON(intel_engine_get_seqno(engine) != seqno);
Chris Wilson57f275a2016-08-15 10:49:00 +0100437}
438
Chris Wilson73cb9702016-10-28 13:58:46 +0100439static void intel_engine_init_timeline(struct intel_engine_cs *engine)
Chris Wilsondcff85c2016-08-05 10:14:11 +0100440{
Chris Wilson73cb9702016-10-28 13:58:46 +0100441 engine->timeline = &engine->i915->gt.global_timeline.engine[engine->id];
Chris Wilsondcff85c2016-08-05 10:14:11 +0100442}
443
Michal Wajdeczkoc5781352018-03-08 09:50:35 +0000444static void intel_engine_init_batch_pool(struct intel_engine_cs *engine)
445{
446 i915_gem_batch_pool_init(&engine->batch_pool, engine);
447}
448
Mika Kuoppala19df9a52017-09-22 15:43:04 +0300449static bool csb_force_mmio(struct drm_i915_private *i915)
450{
Mika Kuoppala19df9a52017-09-22 15:43:04 +0300451 /*
452 * IOMMU adds unpredictable latency causing the CSB write (from the
453 * GPU into the HWSP) to only be visible some time after the interrupt
454 * (missed breadcrumb syndrome).
455 */
456 if (intel_vtd_active())
457 return true;
458
Weinan Li1fd51d92017-10-15 11:55:25 +0800459 /* Older GVT emulation depends upon intercepting CSB mmio */
460 if (intel_vgpu_active(i915) && !intel_vgpu_has_hwsp_emulation(i915))
461 return true;
462
Mika Kuoppala19df9a52017-09-22 15:43:04 +0300463 return false;
464}
465
466static void intel_engine_init_execlist(struct intel_engine_cs *engine)
467{
468 struct intel_engine_execlists * const execlists = &engine->execlists;
469
470 execlists->csb_use_mmio = csb_force_mmio(engine->i915);
471
Mika Kuoppala76e70082017-09-22 15:43:07 +0300472 execlists->port_mask = 1;
473 BUILD_BUG_ON_NOT_POWER_OF_2(execlists_num_ports(execlists));
474 GEM_BUG_ON(execlists_num_ports(execlists) > EXECLIST_MAX_PORTS);
475
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000476 execlists->queue_priority = INT_MIN;
Mika Kuoppala19df9a52017-09-22 15:43:04 +0300477 execlists->queue = RB_ROOT;
478 execlists->first = NULL;
479}
480
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100481/**
482 * intel_engines_setup_common - setup engine state not requiring hw access
483 * @engine: Engine to setup.
484 *
485 * Initializes @engine@ structure members shared between legacy and execlists
486 * submission modes which do not require hardware access.
487 *
488 * Typically done early in the submission mode specific engine setup stage.
489 */
490void intel_engine_setup_common(struct intel_engine_cs *engine)
491{
Mika Kuoppala19df9a52017-09-22 15:43:04 +0300492 intel_engine_init_execlist(engine);
Chris Wilson73cb9702016-10-28 13:58:46 +0100493 intel_engine_init_timeline(engine);
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100494 intel_engine_init_hangcheck(engine);
Michal Wajdeczkoc5781352018-03-08 09:50:35 +0000495 intel_engine_init_batch_pool(engine);
Chris Wilson7756e452016-08-18 17:17:10 +0100496 intel_engine_init_cmd_parser(engine);
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100497}
498
Chris Wilsonadc320c2016-08-15 10:48:59 +0100499int intel_engine_create_scratch(struct intel_engine_cs *engine, int size)
500{
501 struct drm_i915_gem_object *obj;
502 struct i915_vma *vma;
503 int ret;
504
505 WARN_ON(engine->scratch);
506
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000507 obj = i915_gem_object_create_stolen(engine->i915, size);
Chris Wilsonadc320c2016-08-15 10:48:59 +0100508 if (!obj)
Chris Wilson920cf412016-10-28 13:58:30 +0100509 obj = i915_gem_object_create_internal(engine->i915, size);
Chris Wilsonadc320c2016-08-15 10:48:59 +0100510 if (IS_ERR(obj)) {
511 DRM_ERROR("Failed to allocate scratch page\n");
512 return PTR_ERR(obj);
513 }
514
Chris Wilsona01cb372017-01-16 15:21:30 +0000515 vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL);
Chris Wilsonadc320c2016-08-15 10:48:59 +0100516 if (IS_ERR(vma)) {
517 ret = PTR_ERR(vma);
518 goto err_unref;
519 }
520
521 ret = i915_vma_pin(vma, 0, 4096, PIN_GLOBAL | PIN_HIGH);
522 if (ret)
523 goto err_unref;
524
525 engine->scratch = vma;
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100526 DRM_DEBUG_DRIVER("%s pipe control offset: 0x%08x\n",
527 engine->name, i915_ggtt_offset(vma));
Chris Wilsonadc320c2016-08-15 10:48:59 +0100528 return 0;
529
530err_unref:
531 i915_gem_object_put(obj);
532 return ret;
533}
534
535static void intel_engine_cleanup_scratch(struct intel_engine_cs *engine)
536{
Chris Wilson19880c42016-08-15 10:49:05 +0100537 i915_vma_unpin_and_release(&engine->scratch);
Chris Wilsonadc320c2016-08-15 10:48:59 +0100538}
539
Daniele Ceraolo Spurio486e93f2017-09-13 09:56:02 +0100540static void cleanup_phys_status_page(struct intel_engine_cs *engine)
541{
542 struct drm_i915_private *dev_priv = engine->i915;
543
544 if (!dev_priv->status_page_dmah)
545 return;
546
547 drm_pci_free(&dev_priv->drm, dev_priv->status_page_dmah);
548 engine->status_page.page_addr = NULL;
549}
550
551static void cleanup_status_page(struct intel_engine_cs *engine)
552{
553 struct i915_vma *vma;
554 struct drm_i915_gem_object *obj;
555
556 vma = fetch_and_zero(&engine->status_page.vma);
557 if (!vma)
558 return;
559
560 obj = vma->obj;
561
562 i915_vma_unpin(vma);
563 i915_vma_close(vma);
564
565 i915_gem_object_unpin_map(obj);
566 __i915_gem_object_release_unless_active(obj);
567}
568
569static int init_status_page(struct intel_engine_cs *engine)
570{
571 struct drm_i915_gem_object *obj;
572 struct i915_vma *vma;
573 unsigned int flags;
574 void *vaddr;
575 int ret;
576
577 obj = i915_gem_object_create_internal(engine->i915, PAGE_SIZE);
578 if (IS_ERR(obj)) {
579 DRM_ERROR("Failed to allocate status page\n");
580 return PTR_ERR(obj);
581 }
582
583 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
584 if (ret)
585 goto err;
586
587 vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL);
588 if (IS_ERR(vma)) {
589 ret = PTR_ERR(vma);
590 goto err;
591 }
592
593 flags = PIN_GLOBAL;
594 if (!HAS_LLC(engine->i915))
595 /* On g33, we cannot place HWS above 256MiB, so
596 * restrict its pinning to the low mappable arena.
597 * Though this restriction is not documented for
598 * gen4, gen5, or byt, they also behave similarly
599 * and hang if the HWS is placed at the top of the
600 * GTT. To generalise, it appears that all !llc
601 * platforms have issues with us placing the HWS
602 * above the mappable region (even though we never
603 * actually map it).
604 */
605 flags |= PIN_MAPPABLE;
Chris Wilson34a04e52017-09-13 09:56:03 +0100606 else
607 flags |= PIN_HIGH;
Daniele Ceraolo Spurio486e93f2017-09-13 09:56:02 +0100608 ret = i915_vma_pin(vma, 0, 4096, flags);
609 if (ret)
610 goto err;
611
612 vaddr = i915_gem_object_pin_map(obj, I915_MAP_WB);
613 if (IS_ERR(vaddr)) {
614 ret = PTR_ERR(vaddr);
615 goto err_unpin;
616 }
617
618 engine->status_page.vma = vma;
619 engine->status_page.ggtt_offset = i915_ggtt_offset(vma);
620 engine->status_page.page_addr = memset(vaddr, 0, PAGE_SIZE);
621
622 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
623 engine->name, i915_ggtt_offset(vma));
624 return 0;
625
626err_unpin:
627 i915_vma_unpin(vma);
628err:
629 i915_gem_object_put(obj);
630 return ret;
631}
632
633static int init_phys_status_page(struct intel_engine_cs *engine)
634{
635 struct drm_i915_private *dev_priv = engine->i915;
636
637 GEM_BUG_ON(engine->id != RCS);
638
639 dev_priv->status_page_dmah =
640 drm_pci_alloc(&dev_priv->drm, PAGE_SIZE, PAGE_SIZE);
641 if (!dev_priv->status_page_dmah)
642 return -ENOMEM;
643
644 engine->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
645 memset(engine->status_page.page_addr, 0, PAGE_SIZE);
646
647 return 0;
648}
649
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100650/**
651 * intel_engines_init_common - initialize cengine state which might require hw access
652 * @engine: Engine to initialize.
653 *
654 * Initializes @engine@ structure members shared between legacy and execlists
655 * submission modes which do require hardware access.
656 *
657 * Typcally done at later stages of submission mode specific engine setup.
658 *
659 * Returns zero on success or an error code on failure.
660 */
661int intel_engine_init_common(struct intel_engine_cs *engine)
662{
Chris Wilson266a2402017-05-04 10:33:08 +0100663 struct intel_ring *ring;
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100664 int ret;
665
Chris Wilsonff44ad52017-03-16 17:13:03 +0000666 engine->set_default_submission(engine);
667
Chris Wilsone8a9c582016-12-18 15:37:20 +0000668 /* We may need to do things with the shrinker which
669 * require us to immediately switch back to the default
670 * context. This can cause a problem as pinning the
671 * default context also requires GTT space which may not
672 * be available. To avoid this we always pin the default
673 * context.
674 */
Chris Wilson266a2402017-05-04 10:33:08 +0100675 ring = engine->context_pin(engine, engine->i915->kernel_context);
676 if (IS_ERR(ring))
677 return PTR_ERR(ring);
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100678
Chris Wilsone7af3112017-10-03 21:34:48 +0100679 /*
680 * Similarly the preempt context must always be available so that
681 * we can interrupt the engine at any time.
682 */
Chris Wilsond6376372018-02-07 21:05:44 +0000683 if (engine->i915->preempt_context) {
Chris Wilsone7af3112017-10-03 21:34:48 +0100684 ring = engine->context_pin(engine,
685 engine->i915->preempt_context);
686 if (IS_ERR(ring)) {
687 ret = PTR_ERR(ring);
688 goto err_unpin_kernel;
689 }
690 }
691
Chris Wilsone8a9c582016-12-18 15:37:20 +0000692 ret = intel_engine_init_breadcrumbs(engine);
693 if (ret)
Chris Wilsone7af3112017-10-03 21:34:48 +0100694 goto err_unpin_preempt;
Chris Wilsone8a9c582016-12-18 15:37:20 +0000695
Daniele Ceraolo Spurio486e93f2017-09-13 09:56:02 +0100696 if (HWS_NEEDS_PHYSICAL(engine->i915))
697 ret = init_phys_status_page(engine);
698 else
699 ret = init_status_page(engine);
700 if (ret)
Chris Wilson7c2fa7f2017-11-10 14:26:34 +0000701 goto err_breadcrumbs;
Chris Wilson4e50f082016-10-28 13:58:31 +0100702
Chris Wilson7756e452016-08-18 17:17:10 +0100703 return 0;
Chris Wilsone8a9c582016-12-18 15:37:20 +0000704
Daniele Ceraolo Spurio486e93f2017-09-13 09:56:02 +0100705err_breadcrumbs:
706 intel_engine_fini_breadcrumbs(engine);
Chris Wilsone7af3112017-10-03 21:34:48 +0100707err_unpin_preempt:
Chris Wilsond6376372018-02-07 21:05:44 +0000708 if (engine->i915->preempt_context)
Chris Wilsone7af3112017-10-03 21:34:48 +0100709 engine->context_unpin(engine, engine->i915->preempt_context);
710err_unpin_kernel:
Chris Wilsone8a9c582016-12-18 15:37:20 +0000711 engine->context_unpin(engine, engine->i915->kernel_context);
712 return ret;
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100713}
Chris Wilson96a945a2016-08-03 13:19:16 +0100714
715/**
716 * intel_engines_cleanup_common - cleans up the engine state created by
717 * the common initiailizers.
718 * @engine: Engine to cleanup.
719 *
720 * This cleans up everything created by the common helpers.
721 */
722void intel_engine_cleanup_common(struct intel_engine_cs *engine)
723{
Chris Wilsonadc320c2016-08-15 10:48:59 +0100724 intel_engine_cleanup_scratch(engine);
725
Daniele Ceraolo Spurio486e93f2017-09-13 09:56:02 +0100726 if (HWS_NEEDS_PHYSICAL(engine->i915))
727 cleanup_phys_status_page(engine);
728 else
729 cleanup_status_page(engine);
730
Chris Wilson96a945a2016-08-03 13:19:16 +0100731 intel_engine_fini_breadcrumbs(engine);
Chris Wilson7756e452016-08-18 17:17:10 +0100732 intel_engine_cleanup_cmd_parser(engine);
Chris Wilson96a945a2016-08-03 13:19:16 +0100733 i915_gem_batch_pool_fini(&engine->batch_pool);
Chris Wilsone8a9c582016-12-18 15:37:20 +0000734
Chris Wilsond2b4b972017-11-10 14:26:33 +0000735 if (engine->default_state)
736 i915_gem_object_put(engine->default_state);
737
Chris Wilsond6376372018-02-07 21:05:44 +0000738 if (engine->i915->preempt_context)
Chris Wilsone7af3112017-10-03 21:34:48 +0100739 engine->context_unpin(engine, engine->i915->preempt_context);
Chris Wilsone8a9c582016-12-18 15:37:20 +0000740 engine->context_unpin(engine, engine->i915->kernel_context);
Chris Wilson96a945a2016-08-03 13:19:16 +0100741}
Chris Wilson1b365952016-10-04 21:11:31 +0100742
Chris Wilson3ceda3a2018-02-12 10:24:15 +0000743u64 intel_engine_get_active_head(const struct intel_engine_cs *engine)
Chris Wilson1b365952016-10-04 21:11:31 +0100744{
745 struct drm_i915_private *dev_priv = engine->i915;
746 u64 acthd;
747
748 if (INTEL_GEN(dev_priv) >= 8)
749 acthd = I915_READ64_2x32(RING_ACTHD(engine->mmio_base),
750 RING_ACTHD_UDW(engine->mmio_base));
751 else if (INTEL_GEN(dev_priv) >= 4)
752 acthd = I915_READ(RING_ACTHD(engine->mmio_base));
753 else
754 acthd = I915_READ(ACTHD);
755
756 return acthd;
757}
758
Chris Wilson3ceda3a2018-02-12 10:24:15 +0000759u64 intel_engine_get_last_batch_head(const struct intel_engine_cs *engine)
Chris Wilson1b365952016-10-04 21:11:31 +0100760{
761 struct drm_i915_private *dev_priv = engine->i915;
762 u64 bbaddr;
763
764 if (INTEL_GEN(dev_priv) >= 8)
765 bbaddr = I915_READ64_2x32(RING_BBADDR(engine->mmio_base),
766 RING_BBADDR_UDW(engine->mmio_base));
767 else
768 bbaddr = I915_READ(RING_BBADDR(engine->mmio_base));
769
770 return bbaddr;
771}
Chris Wilson0e704472016-10-12 10:05:17 +0100772
773const char *i915_cache_level_str(struct drm_i915_private *i915, int type)
774{
775 switch (type) {
776 case I915_CACHE_NONE: return " uncached";
777 case I915_CACHE_LLC: return HAS_LLC(i915) ? " LLC" : " snooped";
778 case I915_CACHE_L3_LLC: return " L3+LLC";
779 case I915_CACHE_WT: return " WT";
780 default: return "";
781 }
782}
783
784static inline uint32_t
785read_subslice_reg(struct drm_i915_private *dev_priv, int slice,
786 int subslice, i915_reg_t reg)
787{
788 uint32_t mcr;
789 uint32_t ret;
790 enum forcewake_domains fw_domains;
791
792 fw_domains = intel_uncore_forcewake_for_reg(dev_priv, reg,
793 FW_REG_READ);
794 fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
795 GEN8_MCR_SELECTOR,
796 FW_REG_READ | FW_REG_WRITE);
797
798 spin_lock_irq(&dev_priv->uncore.lock);
799 intel_uncore_forcewake_get__locked(dev_priv, fw_domains);
800
801 mcr = I915_READ_FW(GEN8_MCR_SELECTOR);
802 /*
803 * The HW expects the slice and sublice selectors to be reset to 0
804 * after reading out the registers.
805 */
806 WARN_ON_ONCE(mcr & (GEN8_MCR_SLICE_MASK | GEN8_MCR_SUBSLICE_MASK));
807 mcr &= ~(GEN8_MCR_SLICE_MASK | GEN8_MCR_SUBSLICE_MASK);
808 mcr |= GEN8_MCR_SLICE(slice) | GEN8_MCR_SUBSLICE(subslice);
809 I915_WRITE_FW(GEN8_MCR_SELECTOR, mcr);
810
811 ret = I915_READ_FW(reg);
812
813 mcr &= ~(GEN8_MCR_SLICE_MASK | GEN8_MCR_SUBSLICE_MASK);
814 I915_WRITE_FW(GEN8_MCR_SELECTOR, mcr);
815
816 intel_uncore_forcewake_put__locked(dev_priv, fw_domains);
817 spin_unlock_irq(&dev_priv->uncore.lock);
818
819 return ret;
820}
821
822/* NB: please notice the memset */
823void intel_engine_get_instdone(struct intel_engine_cs *engine,
824 struct intel_instdone *instdone)
825{
826 struct drm_i915_private *dev_priv = engine->i915;
827 u32 mmio_base = engine->mmio_base;
828 int slice;
829 int subslice;
830
831 memset(instdone, 0, sizeof(*instdone));
832
833 switch (INTEL_GEN(dev_priv)) {
834 default:
835 instdone->instdone = I915_READ(RING_INSTDONE(mmio_base));
836
837 if (engine->id != RCS)
838 break;
839
840 instdone->slice_common = I915_READ(GEN7_SC_INSTDONE);
841 for_each_instdone_slice_subslice(dev_priv, slice, subslice) {
842 instdone->sampler[slice][subslice] =
843 read_subslice_reg(dev_priv, slice, subslice,
844 GEN7_SAMPLER_INSTDONE);
845 instdone->row[slice][subslice] =
846 read_subslice_reg(dev_priv, slice, subslice,
847 GEN7_ROW_INSTDONE);
848 }
849 break;
850 case 7:
851 instdone->instdone = I915_READ(RING_INSTDONE(mmio_base));
852
853 if (engine->id != RCS)
854 break;
855
856 instdone->slice_common = I915_READ(GEN7_SC_INSTDONE);
857 instdone->sampler[0][0] = I915_READ(GEN7_SAMPLER_INSTDONE);
858 instdone->row[0][0] = I915_READ(GEN7_ROW_INSTDONE);
859
860 break;
861 case 6:
862 case 5:
863 case 4:
864 instdone->instdone = I915_READ(RING_INSTDONE(mmio_base));
865
866 if (engine->id == RCS)
867 /* HACK: Using the wrong struct member */
868 instdone->slice_common = I915_READ(GEN4_INSTDONE1);
869 break;
870 case 3:
871 case 2:
872 instdone->instdone = I915_READ(GEN2_INSTDONE);
873 break;
874 }
875}
Chris Wilsonf97fbf92017-02-13 17:15:14 +0000876
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +0000877static int wa_add(struct drm_i915_private *dev_priv,
878 i915_reg_t addr,
879 const u32 mask, const u32 val)
880{
881 const u32 idx = dev_priv->workarounds.count;
882
883 if (WARN_ON(idx >= I915_MAX_WA_REGS))
884 return -ENOSPC;
885
886 dev_priv->workarounds.reg[idx].addr = addr;
887 dev_priv->workarounds.reg[idx].value = val;
888 dev_priv->workarounds.reg[idx].mask = mask;
889
890 dev_priv->workarounds.count++;
891
892 return 0;
893}
894
895#define WA_REG(addr, mask, val) do { \
896 const int r = wa_add(dev_priv, (addr), (mask), (val)); \
897 if (r) \
898 return r; \
899 } while (0)
900
901#define WA_SET_BIT_MASKED(addr, mask) \
902 WA_REG(addr, (mask), _MASKED_BIT_ENABLE(mask))
903
904#define WA_CLR_BIT_MASKED(addr, mask) \
905 WA_REG(addr, (mask), _MASKED_BIT_DISABLE(mask))
906
907#define WA_SET_FIELD_MASKED(addr, mask, value) \
908 WA_REG(addr, mask, _MASKED_FIELD(mask, value))
909
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +0000910static int wa_ring_whitelist_reg(struct intel_engine_cs *engine,
911 i915_reg_t reg)
912{
913 struct drm_i915_private *dev_priv = engine->i915;
914 struct i915_workarounds *wa = &dev_priv->workarounds;
915 const uint32_t index = wa->hw_whitelist_count[engine->id];
916
917 if (WARN_ON(index >= RING_MAX_NONPRIV_SLOTS))
918 return -EINVAL;
919
Oscar Mateo32ced392017-09-28 15:40:39 -0700920 I915_WRITE(RING_FORCE_TO_NONPRIV(engine->mmio_base, index),
921 i915_mmio_reg_offset(reg));
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +0000922 wa->hw_whitelist_count[engine->id]++;
923
924 return 0;
925}
926
927static int gen8_init_workarounds(struct intel_engine_cs *engine)
928{
929 struct drm_i915_private *dev_priv = engine->i915;
930
931 WA_SET_BIT_MASKED(INSTPM, INSTPM_FORCE_ORDERING);
932
933 /* WaDisableAsyncFlipPerfMode:bdw,chv */
934 WA_SET_BIT_MASKED(MI_MODE, ASYNC_FLIP_PERF_DISABLE);
935
936 /* WaDisablePartialInstShootdown:bdw,chv */
937 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
938 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
939
940 /* Use Force Non-Coherent whenever executing a 3D context. This is a
941 * workaround for for a possible hang in the unlikely event a TLB
942 * invalidation occurs during a PSD flush.
943 */
944 /* WaForceEnableNonCoherent:bdw,chv */
945 /* WaHdcDisableFetchWhenMasked:bdw,chv */
946 WA_SET_BIT_MASKED(HDC_CHICKEN0,
947 HDC_DONOT_FETCH_MEM_WHEN_MASKED |
948 HDC_FORCE_NON_COHERENT);
949
950 /* From the Haswell PRM, Command Reference: Registers, CACHE_MODE_0:
951 * "The Hierarchical Z RAW Stall Optimization allows non-overlapping
952 * polygons in the same 8x4 pixel/sample area to be processed without
953 * stalling waiting for the earlier ones to write to Hierarchical Z
954 * buffer."
955 *
956 * This optimization is off by default for BDW and CHV; turn it on.
957 */
958 WA_CLR_BIT_MASKED(CACHE_MODE_0_GEN7, HIZ_RAW_STALL_OPT_DISABLE);
959
960 /* Wa4x4STCOptimizationDisable:bdw,chv */
961 WA_SET_BIT_MASKED(CACHE_MODE_1, GEN8_4x4_STC_OPTIMIZATION_DISABLE);
962
963 /*
964 * BSpec recommends 8x4 when MSAA is used,
965 * however in practice 16x4 seems fastest.
966 *
967 * Note that PS/WM thread counts depend on the WIZ hashing
968 * disable bit, which we don't touch here, but it's good
969 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
970 */
971 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
972 GEN6_WIZ_HASHING_MASK,
973 GEN6_WIZ_HASHING_16x4);
974
975 return 0;
976}
977
978static int bdw_init_workarounds(struct intel_engine_cs *engine)
979{
980 struct drm_i915_private *dev_priv = engine->i915;
981 int ret;
982
983 ret = gen8_init_workarounds(engine);
984 if (ret)
985 return ret;
986
987 /* WaDisableThreadStallDopClockGating:bdw (pre-production) */
988 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
989
990 /* WaDisableDopClockGating:bdw
991 *
992 * Also see the related UCGTCL1 write in broadwell_init_clock_gating()
993 * to disable EUTC clock gating.
994 */
995 WA_SET_BIT_MASKED(GEN7_ROW_CHICKEN2,
996 DOP_CLOCK_GATING_DISABLE);
997
998 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
999 GEN8_SAMPLER_POWER_BYPASS_DIS);
1000
1001 WA_SET_BIT_MASKED(HDC_CHICKEN0,
1002 /* WaForceContextSaveRestoreNonCoherent:bdw */
1003 HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
1004 /* WaDisableFenceDestinationToSLM:bdw (pre-prod) */
1005 (IS_BDW_GT3(dev_priv) ? HDC_FENCE_DEST_SLM_DISABLE : 0));
1006
1007 return 0;
1008}
1009
1010static int chv_init_workarounds(struct intel_engine_cs *engine)
1011{
1012 struct drm_i915_private *dev_priv = engine->i915;
1013 int ret;
1014
1015 ret = gen8_init_workarounds(engine);
1016 if (ret)
1017 return ret;
1018
1019 /* WaDisableThreadStallDopClockGating:chv */
1020 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
1021
1022 /* Improve HiZ throughput on CHV. */
1023 WA_SET_BIT_MASKED(HIZ_CHICKEN, CHV_HZ_8X8_MODE_IN_1X);
1024
1025 return 0;
1026}
1027
1028static int gen9_init_workarounds(struct intel_engine_cs *engine)
1029{
1030 struct drm_i915_private *dev_priv = engine->i915;
1031 int ret;
1032
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001033 /* WaConextSwitchWithConcurrentTLBInvalidate:skl,bxt,kbl,glk,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001034 I915_WRITE(GEN9_CSFE_CHICKEN1_RCS, _MASKED_BIT_ENABLE(GEN9_PREEMPT_GPGPU_SYNC_SWITCH_DISABLE));
1035
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001036 /* WaEnableLbsSlaRetryTimerDecrement:skl,bxt,kbl,glk,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001037 I915_WRITE(BDW_SCRATCH1, I915_READ(BDW_SCRATCH1) |
1038 GEN9_LBS_SLA_RETRY_TIMER_DECREMENT_ENABLE);
1039
Rodrigo Vivi98eed3d2017-06-19 14:21:47 -07001040 /* WaDisableKillLogic:bxt,skl,kbl */
1041 if (!IS_COFFEELAKE(dev_priv))
1042 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
1043 ECOCHK_DIS_TLB);
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001044
Ville Syrjälä93564042017-08-24 22:10:51 +03001045 if (HAS_LLC(dev_priv)) {
1046 /* WaCompressedResourceSamplerPbeMediaNewHashMode:skl,kbl
1047 *
1048 * Must match Display Engine. See
1049 * WaCompressedResourceDisplayNewHashMode.
1050 */
1051 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1052 GEN9_PBE_COMPRESSED_HASH_SELECTION);
1053 WA_SET_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN7,
1054 GEN9_SAMPLER_HASH_COMPRESSED_READ_ADDR);
Chris Wilson53221e12017-10-04 13:41:52 +01001055
1056 I915_WRITE(MMCD_MISC_CTRL,
1057 I915_READ(MMCD_MISC_CTRL) |
1058 MMCD_PCLA |
1059 MMCD_HOTSPOT_EN);
Ville Syrjälä93564042017-08-24 22:10:51 +03001060 }
1061
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001062 /* WaClearFlowControlGpgpuContextSave:skl,bxt,kbl,glk,cfl */
1063 /* WaDisablePartialInstShootdown:skl,bxt,kbl,glk,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001064 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
1065 FLOW_CONTROL_ENABLE |
1066 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
1067
1068 /* Syncing dependencies between camera and graphics:skl,bxt,kbl */
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001069 if (!IS_COFFEELAKE(dev_priv))
1070 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
1071 GEN9_DISABLE_OCL_OOB_SUPPRESS_LOGIC);
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001072
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001073 /* WaEnableYV12BugFixInHalfSliceChicken7:skl,bxt,kbl,glk,cfl */
1074 /* WaEnableSamplerGPGPUPreemptionSupport:skl,bxt,kbl,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001075 WA_SET_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN7,
Arkadiusz Hiler0b71cea2017-05-12 13:20:15 +02001076 GEN9_ENABLE_YV12_BUGFIX |
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001077 GEN9_ENABLE_GPGPU_PREEMPTION);
1078
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001079 /* Wa4x4STCOptimizationDisable:skl,bxt,kbl,glk,cfl */
1080 /* WaDisablePartialResolveInVc:skl,bxt,kbl,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001081 WA_SET_BIT_MASKED(CACHE_MODE_1, (GEN8_4x4_STC_OPTIMIZATION_DISABLE |
1082 GEN9_PARTIAL_RESOLVE_IN_VC_DISABLE));
1083
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001084 /* WaCcsTlbPrefetchDisable:skl,bxt,kbl,glk,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001085 WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
1086 GEN9_CCS_TLB_PREFETCH_ENABLE);
1087
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001088 /* WaForceContextSaveRestoreNonCoherent:skl,bxt,kbl,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001089 WA_SET_BIT_MASKED(HDC_CHICKEN0,
1090 HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
1091 HDC_FORCE_CSR_NON_COHERENT_OVR_DISABLE);
1092
1093 /* WaForceEnableNonCoherent and WaDisableHDCInvalidation are
1094 * both tied to WaForceContextSaveRestoreNonCoherent
1095 * in some hsds for skl. We keep the tie for all gen9. The
1096 * documentation is a bit hazy and so we want to get common behaviour,
1097 * even though there is no clear evidence we would need both on kbl/bxt.
1098 * This area has been source of system hangs so we play it safe
1099 * and mimic the skl regardless of what bspec says.
1100 *
1101 * Use Force Non-Coherent whenever executing a 3D context. This
1102 * is a workaround for a possible hang in the unlikely event
1103 * a TLB invalidation occurs during a PSD flush.
1104 */
1105
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001106 /* WaForceEnableNonCoherent:skl,bxt,kbl,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001107 WA_SET_BIT_MASKED(HDC_CHICKEN0,
1108 HDC_FORCE_NON_COHERENT);
1109
Rodrigo Vivi98eed3d2017-06-19 14:21:47 -07001110 /* WaDisableHDCInvalidation:skl,bxt,kbl,cfl */
1111 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
1112 BDW_DISABLE_HDC_INVALIDATION);
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001113
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001114 /* WaDisableSamplerPowerBypassForSOPingPong:skl,bxt,kbl,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001115 if (IS_SKYLAKE(dev_priv) ||
1116 IS_KABYLAKE(dev_priv) ||
Chris Wilsonf3e2b2c2017-11-14 13:43:39 +00001117 IS_COFFEELAKE(dev_priv))
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001118 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
1119 GEN8_SAMPLER_POWER_BYPASS_DIS);
1120
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001121 /* WaDisableSTUnitPowerOptimization:skl,bxt,kbl,glk,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001122 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN2, GEN8_ST_PO_DISABLE);
1123
Valtteri Rantala74368302017-11-28 16:45:05 +02001124 /* WaProgramL3SqcReg1DefaultForPerf:bxt,glk */
1125 if (IS_GEN9_LP(dev_priv)) {
1126 u32 val = I915_READ(GEN8_L3SQCREG1);
1127
1128 val &= ~L3_PRIO_CREDITS_MASK;
1129 val |= L3_GENERAL_PRIO_CREDITS(62) | L3_HIGH_PRIO_CREDITS(2);
1130 I915_WRITE(GEN8_L3SQCREG1, val);
1131 }
1132
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001133 /* WaOCLCoherentLineFlush:skl,bxt,kbl,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001134 I915_WRITE(GEN8_L3SQCREG4, (I915_READ(GEN8_L3SQCREG4) |
1135 GEN8_LQSC_FLUSH_COHERENT_LINES));
1136
Michał Winiarski5152def2017-10-03 21:34:46 +01001137 /*
1138 * Supporting preemption with fine-granularity requires changes in the
1139 * batch buffer programming. Since we can't break old userspace, we
1140 * need to set our default preemption level to safe value. Userspace is
1141 * still able to use more fine-grained preemption levels, since in
1142 * WaEnablePreemptionGranularityControlByUMD we're whitelisting the
1143 * per-ctx register. As such, WaDisable{3D,GPGPU}MidCmdPreemption are
1144 * not real HW workarounds, but merely a way to start using preemption
1145 * while maintaining old contract with userspace.
1146 */
1147
1148 /* WaDisable3DMidCmdPreemption:skl,bxt,glk,cfl,[cnl] */
1149 WA_CLR_BIT_MASKED(GEN8_CS_CHICKEN1, GEN9_PREEMPT_3D_OBJECT_LEVEL);
1150
1151 /* WaDisableGPGPUMidCmdPreemption:skl,bxt,blk,cfl,[cnl] */
1152 WA_SET_FIELD_MASKED(GEN8_CS_CHICKEN1, GEN9_PREEMPT_GPGPU_LEVEL_MASK,
1153 GEN9_PREEMPT_GPGPU_COMMAND_LEVEL);
1154
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001155 /* WaVFEStateAfterPipeControlwithMediaStateClear:skl,bxt,glk,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001156 ret = wa_ring_whitelist_reg(engine, GEN9_CTX_PREEMPT_REG);
1157 if (ret)
1158 return ret;
1159
Jeff McGee1e998342017-10-03 21:34:45 +01001160 /* WaEnablePreemptionGranularityControlByUMD:skl,bxt,kbl,cfl,[cnl] */
1161 I915_WRITE(GEN7_FF_SLICE_CS_CHICKEN1,
1162 _MASKED_BIT_ENABLE(GEN9_FFSC_PERCTX_PREEMPT_CTRL));
1163 ret = wa_ring_whitelist_reg(engine, GEN8_CS_CHICKEN1);
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001164 if (ret)
1165 return ret;
1166
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001167 /* WaAllowUMDToModifyHDCChicken1:skl,bxt,kbl,glk,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001168 ret = wa_ring_whitelist_reg(engine, GEN8_HDC_CHICKEN1);
1169 if (ret)
1170 return ret;
1171
1172 return 0;
1173}
1174
1175static int skl_tune_iz_hashing(struct intel_engine_cs *engine)
1176{
1177 struct drm_i915_private *dev_priv = engine->i915;
1178 u8 vals[3] = { 0, 0, 0 };
1179 unsigned int i;
1180
1181 for (i = 0; i < 3; i++) {
1182 u8 ss;
1183
1184 /*
1185 * Only consider slices where one, and only one, subslice has 7
1186 * EUs
1187 */
1188 if (!is_power_of_2(INTEL_INFO(dev_priv)->sseu.subslice_7eu[i]))
1189 continue;
1190
1191 /*
1192 * subslice_7eu[i] != 0 (because of the check above) and
1193 * ss_max == 4 (maximum number of subslices possible per slice)
1194 *
1195 * -> 0 <= ss <= 3;
1196 */
1197 ss = ffs(INTEL_INFO(dev_priv)->sseu.subslice_7eu[i]) - 1;
1198 vals[i] = 3 - ss;
1199 }
1200
1201 if (vals[0] == 0 && vals[1] == 0 && vals[2] == 0)
1202 return 0;
1203
1204 /* Tune IZ hashing. See intel_device_info_runtime_init() */
1205 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
1206 GEN9_IZ_HASHING_MASK(2) |
1207 GEN9_IZ_HASHING_MASK(1) |
1208 GEN9_IZ_HASHING_MASK(0),
1209 GEN9_IZ_HASHING(2, vals[2]) |
1210 GEN9_IZ_HASHING(1, vals[1]) |
1211 GEN9_IZ_HASHING(0, vals[0]));
1212
1213 return 0;
1214}
1215
1216static int skl_init_workarounds(struct intel_engine_cs *engine)
1217{
1218 struct drm_i915_private *dev_priv = engine->i915;
1219 int ret;
1220
1221 ret = gen9_init_workarounds(engine);
1222 if (ret)
1223 return ret;
1224
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001225 /* WaEnableGapsTsvCreditFix:skl */
1226 I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
1227 GEN9_GAPS_TSV_CREDIT_DISABLE));
1228
1229 /* WaDisableGafsUnitClkGating:skl */
Oscar Mateo4827c542017-09-07 08:40:07 -07001230 I915_WRITE(GEN7_UCGCTL4, (I915_READ(GEN7_UCGCTL4) |
1231 GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE));
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001232
1233 /* WaInPlaceDecompressionHang:skl */
1234 if (IS_SKL_REVID(dev_priv, SKL_REVID_H0, REVID_FOREVER))
Oscar Mateoefc886c2017-09-07 08:40:04 -07001235 I915_WRITE(GEN9_GAMT_ECO_REG_RW_IA,
1236 (I915_READ(GEN9_GAMT_ECO_REG_RW_IA) |
1237 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS));
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001238
1239 /* WaDisableLSQCROPERFforOCL:skl */
1240 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
1241 if (ret)
1242 return ret;
1243
1244 return skl_tune_iz_hashing(engine);
1245}
1246
1247static int bxt_init_workarounds(struct intel_engine_cs *engine)
1248{
1249 struct drm_i915_private *dev_priv = engine->i915;
1250 int ret;
1251
1252 ret = gen9_init_workarounds(engine);
1253 if (ret)
1254 return ret;
1255
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001256 /* WaDisableThreadStallDopClockGating:bxt */
1257 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
1258 STALL_DOP_GATING_DISABLE);
1259
1260 /* WaDisablePooledEuLoadBalancingFix:bxt */
Chris Wilson70a84f32017-11-14 13:43:40 +00001261 I915_WRITE(FF_SLICE_CS_CHICKEN2,
1262 _MASKED_BIT_ENABLE(GEN9_POOLED_EU_LOAD_BALANCING_FIX_DISABLE));
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001263
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001264 /* WaToEnableHwFixForPushConstHWBug:bxt */
Chris Wilson70a84f32017-11-14 13:43:40 +00001265 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1266 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001267
1268 /* WaInPlaceDecompressionHang:bxt */
Chris Wilson70a84f32017-11-14 13:43:40 +00001269 I915_WRITE(GEN9_GAMT_ECO_REG_RW_IA,
1270 (I915_READ(GEN9_GAMT_ECO_REG_RW_IA) |
1271 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS));
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001272
1273 return 0;
1274}
1275
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07001276static int cnl_init_workarounds(struct intel_engine_cs *engine)
1277{
1278 struct drm_i915_private *dev_priv = engine->i915;
1279 int ret;
1280
Oscar Mateo6cf20a02017-09-07 08:40:05 -07001281 /* WaDisableI2mCycleOnWRPort:cnl (pre-prod) */
Rodrigo Vivi86ebb012017-08-29 16:07:51 -07001282 if (IS_CNL_REVID(dev_priv, CNL_REVID_B0, CNL_REVID_B0))
Oscar Mateo6cf20a02017-09-07 08:40:05 -07001283 I915_WRITE(GAMT_CHKN_BIT_REG,
1284 (I915_READ(GAMT_CHKN_BIT_REG) |
1285 GAMT_CHKN_DISABLE_I2M_CYCLE_ON_WR_PORT));
Rodrigo Vivi86ebb012017-08-29 16:07:51 -07001286
Rodrigo Viviacfb5552017-08-23 13:35:04 -07001287 /* WaForceContextSaveRestoreNonCoherent:cnl */
1288 WA_SET_BIT_MASKED(CNL_HDC_CHICKEN0,
1289 HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT);
1290
Rodrigo Viviaa9f4c42017-09-06 15:03:25 -07001291 /* WaThrottleEUPerfToAvoidTDBackPressure:cnl(pre-prod) */
1292 if (IS_CNL_REVID(dev_priv, CNL_REVID_B0, CNL_REVID_B0))
1293 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, THROTTLE_12_5);
1294
Rodrigo Vivie6d1a4f2017-08-15 16:16:49 -07001295 /* WaDisableReplayBufferBankArbitrationOptimization:cnl */
1296 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1297 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
1298
Rodrigo Vivid1d24752017-08-15 16:16:50 -07001299 /* WaDisableEnhancedSBEVertexCaching:cnl (pre-prod) */
1300 if (IS_CNL_REVID(dev_priv, 0, CNL_REVID_B0))
1301 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1302 GEN8_CSC2_SBE_VUE_CACHE_CONSERVATIVE);
1303
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07001304 /* WaInPlaceDecompressionHang:cnl */
Oscar Mateoefc886c2017-09-07 08:40:04 -07001305 I915_WRITE(GEN9_GAMT_ECO_REG_RW_IA,
1306 (I915_READ(GEN9_GAMT_ECO_REG_RW_IA) |
1307 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS));
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07001308
Oscar Mateo2cbecff2017-08-23 12:56:31 -07001309 /* WaPushConstantDereferenceHoldDisable:cnl */
Oscar Mateob27f5902017-09-07 08:40:06 -07001310 WA_SET_BIT_MASKED(GEN7_ROW_CHICKEN2, PUSH_CONSTANT_DEREF_DISABLE);
Oscar Mateo2cbecff2017-08-23 12:56:31 -07001311
Rodrigo Vivi392572f2017-08-29 16:07:23 -07001312 /* FtrEnableFastAnisoL1BankingFix: cnl */
1313 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3, CNL_FAST_ANISO_L1_BANKING_FIX);
1314
Michał Winiarski5152def2017-10-03 21:34:46 +01001315 /* WaDisable3DMidCmdPreemption:cnl */
1316 WA_CLR_BIT_MASKED(GEN8_CS_CHICKEN1, GEN9_PREEMPT_3D_OBJECT_LEVEL);
1317
1318 /* WaDisableGPGPUMidCmdPreemption:cnl */
1319 WA_SET_FIELD_MASKED(GEN8_CS_CHICKEN1, GEN9_PREEMPT_GPGPU_LEVEL_MASK,
1320 GEN9_PREEMPT_GPGPU_COMMAND_LEVEL);
1321
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07001322 /* WaEnablePreemptionGranularityControlByUMD:cnl */
Jeff McGee1e998342017-10-03 21:34:45 +01001323 I915_WRITE(GEN7_FF_SLICE_CS_CHICKEN1,
1324 _MASKED_BIT_ENABLE(GEN9_FFSC_PERCTX_PREEMPT_CTRL));
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07001325 ret= wa_ring_whitelist_reg(engine, GEN8_CS_CHICKEN1);
1326 if (ret)
1327 return ret;
1328
Rafael Antognollia2b16582017-12-15 16:11:17 -08001329 /* WaDisableEarlyEOT:cnl */
1330 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, DISABLE_EARLY_EOT);
1331
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07001332 return 0;
1333}
1334
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001335static int kbl_init_workarounds(struct intel_engine_cs *engine)
1336{
1337 struct drm_i915_private *dev_priv = engine->i915;
1338 int ret;
1339
1340 ret = gen9_init_workarounds(engine);
1341 if (ret)
1342 return ret;
1343
1344 /* WaEnableGapsTsvCreditFix:kbl */
1345 I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
1346 GEN9_GAPS_TSV_CREDIT_DISABLE));
1347
1348 /* WaDisableDynamicCreditSharing:kbl */
1349 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
Oscar Mateoc6ea497c2017-09-07 08:40:08 -07001350 I915_WRITE(GAMT_CHKN_BIT_REG,
1351 (I915_READ(GAMT_CHKN_BIT_REG) |
1352 GAMT_CHKN_DISABLE_DYNAMIC_CREDIT_SHARING));
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001353
1354 /* WaDisableFenceDestinationToSLM:kbl (pre-prod) */
1355 if (IS_KBL_REVID(dev_priv, KBL_REVID_A0, KBL_REVID_A0))
1356 WA_SET_BIT_MASKED(HDC_CHICKEN0,
1357 HDC_FENCE_DEST_SLM_DISABLE);
1358
1359 /* WaToEnableHwFixForPushConstHWBug:kbl */
1360 if (IS_KBL_REVID(dev_priv, KBL_REVID_C0, REVID_FOREVER))
1361 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1362 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
1363
1364 /* WaDisableGafsUnitClkGating:kbl */
Oscar Mateo4827c542017-09-07 08:40:07 -07001365 I915_WRITE(GEN7_UCGCTL4, (I915_READ(GEN7_UCGCTL4) |
1366 GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE));
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001367
1368 /* WaDisableSbeCacheDispatchPortSharing:kbl */
1369 WA_SET_BIT_MASKED(
1370 GEN7_HALF_SLICE_CHICKEN1,
1371 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
1372
1373 /* WaInPlaceDecompressionHang:kbl */
Oscar Mateoefc886c2017-09-07 08:40:04 -07001374 I915_WRITE(GEN9_GAMT_ECO_REG_RW_IA,
1375 (I915_READ(GEN9_GAMT_ECO_REG_RW_IA) |
1376 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS));
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001377
1378 /* WaDisableLSQCROPERFforOCL:kbl */
1379 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
1380 if (ret)
1381 return ret;
1382
1383 return 0;
1384}
1385
1386static int glk_init_workarounds(struct intel_engine_cs *engine)
1387{
1388 struct drm_i915_private *dev_priv = engine->i915;
1389 int ret;
1390
1391 ret = gen9_init_workarounds(engine);
1392 if (ret)
1393 return ret;
1394
Kenneth Graunkeab062632018-01-05 00:59:05 -08001395 /* WA #0862: Userspace has to set "Barrier Mode" to avoid hangs. */
1396 ret = wa_ring_whitelist_reg(engine, GEN9_SLICE_COMMON_ECO_CHICKEN1);
1397 if (ret)
1398 return ret;
1399
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001400 /* WaToEnableHwFixForPushConstHWBug:glk */
1401 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1402 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
1403
1404 return 0;
1405}
1406
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001407static int cfl_init_workarounds(struct intel_engine_cs *engine)
1408{
1409 struct drm_i915_private *dev_priv = engine->i915;
1410 int ret;
1411
1412 ret = gen9_init_workarounds(engine);
1413 if (ret)
1414 return ret;
1415
1416 /* WaEnableGapsTsvCreditFix:cfl */
1417 I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
1418 GEN9_GAPS_TSV_CREDIT_DISABLE));
1419
1420 /* WaToEnableHwFixForPushConstHWBug:cfl */
1421 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1422 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
1423
1424 /* WaDisableGafsUnitClkGating:cfl */
Oscar Mateo4827c542017-09-07 08:40:07 -07001425 I915_WRITE(GEN7_UCGCTL4, (I915_READ(GEN7_UCGCTL4) |
1426 GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE));
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001427
1428 /* WaDisableSbeCacheDispatchPortSharing:cfl */
1429 WA_SET_BIT_MASKED(
1430 GEN7_HALF_SLICE_CHICKEN1,
1431 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
1432
1433 /* WaInPlaceDecompressionHang:cfl */
Oscar Mateoefc886c2017-09-07 08:40:04 -07001434 I915_WRITE(GEN9_GAMT_ECO_REG_RW_IA,
1435 (I915_READ(GEN9_GAMT_ECO_REG_RW_IA) |
1436 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS));
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001437
1438 return 0;
1439}
1440
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001441int init_workarounds_ring(struct intel_engine_cs *engine)
1442{
1443 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson02e012f2017-03-01 12:11:31 +00001444 int err;
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001445
Tvrtko Ursulinae504be2018-01-19 10:00:03 +00001446 if (GEM_WARN_ON(engine->id != RCS))
1447 return -EINVAL;
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001448
1449 dev_priv->workarounds.count = 0;
Chris Wilson02e012f2017-03-01 12:11:31 +00001450 dev_priv->workarounds.hw_whitelist_count[engine->id] = 0;
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001451
1452 if (IS_BROADWELL(dev_priv))
Chris Wilson02e012f2017-03-01 12:11:31 +00001453 err = bdw_init_workarounds(engine);
1454 else if (IS_CHERRYVIEW(dev_priv))
1455 err = chv_init_workarounds(engine);
1456 else if (IS_SKYLAKE(dev_priv))
1457 err = skl_init_workarounds(engine);
1458 else if (IS_BROXTON(dev_priv))
1459 err = bxt_init_workarounds(engine);
1460 else if (IS_KABYLAKE(dev_priv))
1461 err = kbl_init_workarounds(engine);
1462 else if (IS_GEMINILAKE(dev_priv))
1463 err = glk_init_workarounds(engine);
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001464 else if (IS_COFFEELAKE(dev_priv))
1465 err = cfl_init_workarounds(engine);
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07001466 else if (IS_CANNONLAKE(dev_priv))
1467 err = cnl_init_workarounds(engine);
Chris Wilson02e012f2017-03-01 12:11:31 +00001468 else
1469 err = 0;
1470 if (err)
1471 return err;
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001472
Chris Wilson02e012f2017-03-01 12:11:31 +00001473 DRM_DEBUG_DRIVER("%s: Number of context specific w/a: %d\n",
1474 engine->name, dev_priv->workarounds.count);
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001475 return 0;
1476}
1477
Chris Wilsone61e0f52018-02-21 09:56:36 +00001478int intel_ring_workarounds_emit(struct i915_request *rq)
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001479{
Chris Wilsone61e0f52018-02-21 09:56:36 +00001480 struct i915_workarounds *w = &rq->i915->workarounds;
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001481 u32 *cs;
1482 int ret, i;
1483
1484 if (w->count == 0)
1485 return 0;
1486
Chris Wilsone61e0f52018-02-21 09:56:36 +00001487 ret = rq->engine->emit_flush(rq, EMIT_BARRIER);
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001488 if (ret)
1489 return ret;
1490
Chris Wilsone61e0f52018-02-21 09:56:36 +00001491 cs = intel_ring_begin(rq, w->count * 2 + 2);
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001492 if (IS_ERR(cs))
1493 return PTR_ERR(cs);
1494
1495 *cs++ = MI_LOAD_REGISTER_IMM(w->count);
1496 for (i = 0; i < w->count; i++) {
1497 *cs++ = i915_mmio_reg_offset(w->reg[i].addr);
1498 *cs++ = w->reg[i].value;
1499 }
1500 *cs++ = MI_NOOP;
1501
Chris Wilsone61e0f52018-02-21 09:56:36 +00001502 intel_ring_advance(rq, cs);
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001503
Chris Wilsone61e0f52018-02-21 09:56:36 +00001504 ret = rq->engine->emit_flush(rq, EMIT_BARRIER);
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001505 if (ret)
1506 return ret;
1507
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001508 return 0;
1509}
1510
Chris Wilsona091d4e2017-05-30 13:13:33 +01001511static bool ring_is_idle(struct intel_engine_cs *engine)
1512{
1513 struct drm_i915_private *dev_priv = engine->i915;
1514 bool idle = true;
1515
Chris Wilson74d00d22018-02-12 09:39:28 +00001516 /* If the whole device is asleep, the engine must be idle */
1517 if (!intel_runtime_pm_get_if_in_use(dev_priv))
1518 return true;
Chris Wilsona091d4e2017-05-30 13:13:33 +01001519
Chris Wilsonaed2fc12017-05-30 13:13:34 +01001520 /* First check that no commands are left in the ring */
1521 if ((I915_READ_HEAD(engine) & HEAD_ADDR) !=
1522 (I915_READ_TAIL(engine) & TAIL_ADDR))
1523 idle = false;
1524
Chris Wilsona091d4e2017-05-30 13:13:33 +01001525 /* No bit for gen2, so assume the CS parser is idle */
1526 if (INTEL_GEN(dev_priv) > 2 && !(I915_READ_MODE(engine) & MODE_IDLE))
1527 idle = false;
1528
1529 intel_runtime_pm_put(dev_priv);
1530
1531 return idle;
1532}
1533
Chris Wilson54003672017-03-03 12:19:46 +00001534/**
1535 * intel_engine_is_idle() - Report if the engine has finished process all work
1536 * @engine: the intel_engine_cs
1537 *
1538 * Return true if there are no requests pending, nothing left to be submitted
1539 * to hardware, and that the engine is idle.
1540 */
1541bool intel_engine_is_idle(struct intel_engine_cs *engine)
1542{
1543 struct drm_i915_private *dev_priv = engine->i915;
1544
Chris Wilsona8e9a412017-04-11 20:00:42 +01001545 /* More white lies, if wedged, hw state is inconsistent */
1546 if (i915_terminally_wedged(&dev_priv->gpu_error))
1547 return true;
1548
Chris Wilson54003672017-03-03 12:19:46 +00001549 /* Any inflight/incomplete requests? */
1550 if (!i915_seqno_passed(intel_engine_get_seqno(engine),
1551 intel_engine_last_submit(engine)))
1552 return false;
1553
Chris Wilson8968a362017-04-12 00:44:26 +01001554 if (I915_SELFTEST_ONLY(engine->breadcrumbs.mock))
1555 return true;
1556
Chris Wilson4a118ec2017-10-23 22:32:36 +01001557 /* Waiting to drain ELSP? */
1558 if (READ_ONCE(engine->execlists.active))
Chris Wilson54003672017-03-03 12:19:46 +00001559 return false;
1560
Chris Wilsond6edb6e2017-07-21 13:32:24 +01001561 /* ELSP is empty, but there are ready requests? */
Mika Kuoppalab620e872017-09-22 15:43:03 +03001562 if (READ_ONCE(engine->execlists.first))
Chris Wilsond6edb6e2017-07-21 13:32:24 +01001563 return false;
1564
Chris Wilson54003672017-03-03 12:19:46 +00001565 /* Ring stopped? */
Chris Wilsona091d4e2017-05-30 13:13:33 +01001566 if (!ring_is_idle(engine))
Chris Wilson54003672017-03-03 12:19:46 +00001567 return false;
1568
1569 return true;
1570}
1571
Chris Wilson05425242017-03-03 12:19:47 +00001572bool intel_engines_are_idle(struct drm_i915_private *dev_priv)
1573{
1574 struct intel_engine_cs *engine;
1575 enum intel_engine_id id;
1576
Chris Wilsond7dc4132017-12-12 13:21:48 +00001577 /*
1578 * If the driver is wedged, HW state may be very inconsistent and
Chris Wilson8490ae202017-03-30 15:50:37 +01001579 * report that it is still busy, even though we have stopped using it.
1580 */
1581 if (i915_terminally_wedged(&dev_priv->gpu_error))
1582 return true;
1583
Chris Wilson05425242017-03-03 12:19:47 +00001584 for_each_engine(engine, dev_priv, id) {
1585 if (!intel_engine_is_idle(engine))
1586 return false;
1587 }
1588
1589 return true;
1590}
1591
Chris Wilsonae6c4572017-11-10 14:26:28 +00001592/**
1593 * intel_engine_has_kernel_context:
1594 * @engine: the engine
1595 *
1596 * Returns true if the last context to be executed on this engine, or has been
1597 * executed if the engine is already idle, is the kernel context
1598 * (#i915.kernel_context).
1599 */
Chris Wilson20ccd4d2017-10-24 23:08:55 +01001600bool intel_engine_has_kernel_context(const struct intel_engine_cs *engine)
1601{
Chris Wilsonae6c4572017-11-10 14:26:28 +00001602 const struct i915_gem_context * const kernel_context =
1603 engine->i915->kernel_context;
Chris Wilsone61e0f52018-02-21 09:56:36 +00001604 struct i915_request *rq;
Chris Wilsonae6c4572017-11-10 14:26:28 +00001605
1606 lockdep_assert_held(&engine->i915->drm.struct_mutex);
1607
1608 /*
1609 * Check the last context seen by the engine. If active, it will be
1610 * the last request that remains in the timeline. When idle, it is
1611 * the last executed context as tracked by retirement.
1612 */
1613 rq = __i915_gem_active_peek(&engine->timeline->last_request);
1614 if (rq)
1615 return rq->ctx == kernel_context;
1616 else
1617 return engine->last_retired_context == kernel_context;
Chris Wilson20ccd4d2017-10-24 23:08:55 +01001618}
1619
Chris Wilsonff44ad52017-03-16 17:13:03 +00001620void intel_engines_reset_default_submission(struct drm_i915_private *i915)
1621{
1622 struct intel_engine_cs *engine;
1623 enum intel_engine_id id;
1624
1625 for_each_engine(engine, i915, id)
1626 engine->set_default_submission(engine);
1627}
1628
Chris Wilsonaba5e272017-10-25 15:39:41 +01001629/**
1630 * intel_engines_park: called when the GT is transitioning from busy->idle
1631 * @i915: the i915 device
1632 *
1633 * The GT is now idle and about to go to sleep (maybe never to wake again?).
1634 * Time for us to tidy and put away our toys (release resources back to the
1635 * system).
1636 */
1637void intel_engines_park(struct drm_i915_private *i915)
Chris Wilson6c067572017-05-17 13:10:03 +01001638{
1639 struct intel_engine_cs *engine;
1640 enum intel_engine_id id;
1641
1642 for_each_engine(engine, i915, id) {
Chris Wilson820c5bb2017-11-01 20:21:49 +00001643 /* Flush the residual irq tasklets first. */
1644 intel_engine_disarm_breadcrumbs(engine);
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05301645 tasklet_kill(&engine->execlists.tasklet);
Chris Wilson820c5bb2017-11-01 20:21:49 +00001646
Chris Wilson32651242017-10-27 12:06:17 +01001647 /*
1648 * We are committed now to parking the engines, make sure there
1649 * will be no more interrupts arriving later and the engines
1650 * are truly idle.
1651 */
Chris Wilson30b29402017-11-10 11:25:50 +00001652 if (wait_for(intel_engine_is_idle(engine), 10)) {
Chris Wilson32651242017-10-27 12:06:17 +01001653 struct drm_printer p = drm_debug_printer(__func__);
1654
Chris Wilson30b29402017-11-10 11:25:50 +00001655 dev_err(i915->drm.dev,
1656 "%s is not idle before parking\n",
1657 engine->name);
Chris Wilson0db18b12017-12-08 01:23:00 +00001658 intel_engine_dump(engine, &p, NULL);
Chris Wilson32651242017-10-27 12:06:17 +01001659 }
1660
Chris Wilsonaba5e272017-10-25 15:39:41 +01001661 if (engine->park)
1662 engine->park(engine);
1663
Chris Wilsonaba5e272017-10-25 15:39:41 +01001664 i915_gem_batch_pool_fini(&engine->batch_pool);
Mika Kuoppalab620e872017-09-22 15:43:03 +03001665 engine->execlists.no_priolist = false;
Chris Wilson6c067572017-05-17 13:10:03 +01001666 }
1667}
1668
Chris Wilsonaba5e272017-10-25 15:39:41 +01001669/**
1670 * intel_engines_unpark: called when the GT is transitioning from idle->busy
1671 * @i915: the i915 device
1672 *
1673 * The GT was idle and now about to fire up with some new user requests.
1674 */
1675void intel_engines_unpark(struct drm_i915_private *i915)
1676{
1677 struct intel_engine_cs *engine;
1678 enum intel_engine_id id;
1679
1680 for_each_engine(engine, i915, id) {
1681 if (engine->unpark)
1682 engine->unpark(engine);
1683 }
1684}
1685
Chris Wilson90cad092017-09-06 16:28:59 +01001686bool intel_engine_can_store_dword(struct intel_engine_cs *engine)
1687{
1688 switch (INTEL_GEN(engine->i915)) {
1689 case 2:
1690 return false; /* uses physical not virtual addresses */
1691 case 3:
1692 /* maybe only uses physical not virtual addresses */
1693 return !(IS_I915G(engine->i915) || IS_I915GM(engine->i915));
1694 case 6:
1695 return engine->class != VIDEO_DECODE_CLASS; /* b0rked */
1696 default:
1697 return true;
1698 }
1699}
1700
Chris Wilsond2b4b972017-11-10 14:26:33 +00001701unsigned int intel_engines_has_context_isolation(struct drm_i915_private *i915)
1702{
1703 struct intel_engine_cs *engine;
1704 enum intel_engine_id id;
1705 unsigned int which;
1706
1707 which = 0;
1708 for_each_engine(engine, i915, id)
1709 if (engine->default_state)
1710 which |= BIT(engine->uabi_class);
1711
1712 return which;
1713}
1714
Chris Wilsonf636edb2017-10-09 12:02:57 +01001715static void print_request(struct drm_printer *m,
Chris Wilsone61e0f52018-02-21 09:56:36 +00001716 struct i915_request *rq,
Chris Wilsonf636edb2017-10-09 12:02:57 +01001717 const char *prefix)
1718{
Chris Wilsonab268152018-03-14 10:16:30 +00001719 const char *name = rq->fence.ops->get_timeline_name(&rq->fence);
1720
Chris Wilson367a35a2018-02-28 09:47:32 +00001721 drm_printf(m, "%s%x%s [%llx:%x] prio=%d @ %dms: %s\n", prefix,
Chris Wilsona27d5a42017-10-15 21:43:10 +01001722 rq->global_seqno,
Chris Wilsone61e0f52018-02-21 09:56:36 +00001723 i915_request_completed(rq) ? "!" : "",
Chris Wilson367a35a2018-02-28 09:47:32 +00001724 rq->fence.context, rq->fence.seqno,
Chris Wilsonf636edb2017-10-09 12:02:57 +01001725 rq->priotree.priority,
1726 jiffies_to_msecs(jiffies - rq->emitted_jiffies),
Chris Wilsonab268152018-03-14 10:16:30 +00001727 name);
Chris Wilsonf636edb2017-10-09 12:02:57 +01001728}
1729
Chris Wilsonc1bf2722017-12-22 18:25:21 +00001730static void hexdump(struct drm_printer *m, const void *buf, size_t len)
1731{
1732 const size_t rowsize = 8 * sizeof(u32);
1733 const void *prev = NULL;
1734 bool skip = false;
1735 size_t pos;
1736
1737 for (pos = 0; pos < len; pos += rowsize) {
1738 char line[128];
1739
1740 if (prev && !memcmp(prev, buf + pos, rowsize)) {
1741 if (!skip) {
1742 drm_printf(m, "*\n");
1743 skip = true;
1744 }
1745 continue;
1746 }
1747
1748 WARN_ON_ONCE(hex_dump_to_buffer(buf + pos, len - pos,
1749 rowsize, sizeof(u32),
1750 line, sizeof(line),
1751 false) >= sizeof(line));
1752 drm_printf(m, "%08zx %s\n", pos, line);
1753
1754 prev = buf + pos;
1755 skip = false;
1756 }
1757}
1758
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001759static void intel_engine_print_registers(const struct intel_engine_cs *engine,
1760 struct drm_printer *m)
Chris Wilsonf636edb2017-10-09 12:02:57 +01001761{
Chris Wilsonf636edb2017-10-09 12:02:57 +01001762 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001763 const struct intel_engine_execlists * const execlists =
1764 &engine->execlists;
Chris Wilsonf636edb2017-10-09 12:02:57 +01001765 u64 addr;
1766
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001767 drm_printf(m, "\tRING_START: 0x%08x\n",
1768 I915_READ(RING_START(engine->mmio_base)));
1769 drm_printf(m, "\tRING_HEAD: 0x%08x\n",
1770 I915_READ(RING_HEAD(engine->mmio_base)) & HEAD_ADDR);
1771 drm_printf(m, "\tRING_TAIL: 0x%08x\n",
1772 I915_READ(RING_TAIL(engine->mmio_base)) & TAIL_ADDR);
Chris Wilson3c75de52017-10-26 12:50:48 +01001773 drm_printf(m, "\tRING_CTL: 0x%08x%s\n",
Chris Wilsonf636edb2017-10-09 12:02:57 +01001774 I915_READ(RING_CTL(engine->mmio_base)),
Chris Wilson3c75de52017-10-26 12:50:48 +01001775 I915_READ(RING_CTL(engine->mmio_base)) & (RING_WAIT | RING_WAIT_SEMAPHORE) ? " [waiting]" : "");
1776 if (INTEL_GEN(engine->i915) > 2) {
1777 drm_printf(m, "\tRING_MODE: 0x%08x%s\n",
1778 I915_READ(RING_MI_MODE(engine->mmio_base)),
1779 I915_READ(RING_MI_MODE(engine->mmio_base)) & (MODE_IDLE) ? " [idle]" : "");
1780 }
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001781
1782 if (INTEL_GEN(dev_priv) >= 6) {
1783 drm_printf(m, "\tRING_IMR: %08x\n", I915_READ_IMR(engine));
1784 }
1785
Chris Wilson93c6e962017-11-20 20:55:04 +00001786 if (HAS_LEGACY_SEMAPHORES(dev_priv)) {
Chris Wilsonaf9ff6c2017-11-20 20:55:03 +00001787 drm_printf(m, "\tSYNC_0: 0x%08x\n",
1788 I915_READ(RING_SYNC_0(engine->mmio_base)));
1789 drm_printf(m, "\tSYNC_1: 0x%08x\n",
1790 I915_READ(RING_SYNC_1(engine->mmio_base)));
1791 if (HAS_VEBOX(dev_priv))
1792 drm_printf(m, "\tSYNC_2: 0x%08x\n",
1793 I915_READ(RING_SYNC_2(engine->mmio_base)));
1794 }
Chris Wilsonf636edb2017-10-09 12:02:57 +01001795
Chris Wilsonf636edb2017-10-09 12:02:57 +01001796 addr = intel_engine_get_active_head(engine);
1797 drm_printf(m, "\tACTHD: 0x%08x_%08x\n",
1798 upper_32_bits(addr), lower_32_bits(addr));
1799 addr = intel_engine_get_last_batch_head(engine);
1800 drm_printf(m, "\tBBADDR: 0x%08x_%08x\n",
1801 upper_32_bits(addr), lower_32_bits(addr));
Chris Wilsona0cf5792017-12-18 12:39:14 +00001802 if (INTEL_GEN(dev_priv) >= 8)
1803 addr = I915_READ64_2x32(RING_DMA_FADD(engine->mmio_base),
1804 RING_DMA_FADD_UDW(engine->mmio_base));
1805 else if (INTEL_GEN(dev_priv) >= 4)
1806 addr = I915_READ(RING_DMA_FADD(engine->mmio_base));
1807 else
1808 addr = I915_READ(DMA_FADD_I8XX);
1809 drm_printf(m, "\tDMA_FADDR: 0x%08x_%08x\n",
1810 upper_32_bits(addr), lower_32_bits(addr));
1811 if (INTEL_GEN(dev_priv) >= 4) {
1812 drm_printf(m, "\tIPEIR: 0x%08x\n",
1813 I915_READ(RING_IPEIR(engine->mmio_base)));
1814 drm_printf(m, "\tIPEHR: 0x%08x\n",
1815 I915_READ(RING_IPEHR(engine->mmio_base)));
1816 } else {
1817 drm_printf(m, "\tIPEIR: 0x%08x\n", I915_READ(IPEIR));
1818 drm_printf(m, "\tIPEHR: 0x%08x\n", I915_READ(IPEHR));
1819 }
Chris Wilsonf636edb2017-10-09 12:02:57 +01001820
Chris Wilsonfb5c5512017-11-20 20:55:00 +00001821 if (HAS_EXECLISTS(dev_priv)) {
Chris Wilsonf636edb2017-10-09 12:02:57 +01001822 const u32 *hws = &engine->status_page.page_addr[I915_HWS_CSB_BUF0_INDEX];
Chris Wilsonf636edb2017-10-09 12:02:57 +01001823 u32 ptr, read, write;
1824 unsigned int idx;
1825
1826 drm_printf(m, "\tExeclist status: 0x%08x %08x\n",
1827 I915_READ(RING_EXECLIST_STATUS_LO(engine)),
1828 I915_READ(RING_EXECLIST_STATUS_HI(engine)));
1829
1830 ptr = I915_READ(RING_CONTEXT_STATUS_PTR(engine));
1831 read = GEN8_CSB_READ_PTR(ptr);
1832 write = GEN8_CSB_WRITE_PTR(ptr);
1833 drm_printf(m, "\tExeclist CSB read %d [%d cached], write %d [%d from hws], interrupt posted? %s\n",
1834 read, execlists->csb_head,
1835 write,
1836 intel_read_status_page(engine, intel_hws_csb_write_index(engine->i915)),
1837 yesno(test_bit(ENGINE_IRQ_EXECLIST,
1838 &engine->irq_posted)));
1839 if (read >= GEN8_CSB_ENTRIES)
1840 read = 0;
1841 if (write >= GEN8_CSB_ENTRIES)
1842 write = 0;
1843 if (read > write)
1844 write += GEN8_CSB_ENTRIES;
1845 while (read < write) {
1846 idx = ++read % GEN8_CSB_ENTRIES;
1847 drm_printf(m, "\tExeclist CSB[%d]: 0x%08x [0x%08x in hwsp], context: %d [%d in hwsp]\n",
1848 idx,
1849 I915_READ(RING_CONTEXT_STATUS_BUF_LO(engine, idx)),
1850 hws[idx * 2],
1851 I915_READ(RING_CONTEXT_STATUS_BUF_HI(engine, idx)),
1852 hws[idx * 2 + 1]);
1853 }
1854
1855 rcu_read_lock();
1856 for (idx = 0; idx < execlists_num_ports(execlists); idx++) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00001857 struct i915_request *rq;
Chris Wilsonf636edb2017-10-09 12:02:57 +01001858 unsigned int count;
1859
1860 rq = port_unpack(&execlists->port[idx], &count);
1861 if (rq) {
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001862 char hdr[80];
1863
Chris Wilsone8a70ca2017-12-08 01:22:59 +00001864 snprintf(hdr, sizeof(hdr),
1865 "\t\tELSP[%d] count=%d, rq: ",
1866 idx, count);
1867 print_request(m, rq, hdr);
Chris Wilsonf636edb2017-10-09 12:02:57 +01001868 } else {
Chris Wilsone8a70ca2017-12-08 01:22:59 +00001869 drm_printf(m, "\t\tELSP[%d] idle\n", idx);
Chris Wilsonf636edb2017-10-09 12:02:57 +01001870 }
1871 }
Chris Wilson4a118ec2017-10-23 22:32:36 +01001872 drm_printf(m, "\t\tHW active? 0x%x\n", execlists->active);
Chris Wilsonf636edb2017-10-09 12:02:57 +01001873 rcu_read_unlock();
Chris Wilsonf636edb2017-10-09 12:02:57 +01001874 } else if (INTEL_GEN(dev_priv) > 6) {
1875 drm_printf(m, "\tPP_DIR_BASE: 0x%08x\n",
1876 I915_READ(RING_PP_DIR_BASE(engine)));
1877 drm_printf(m, "\tPP_DIR_BASE_READ: 0x%08x\n",
1878 I915_READ(RING_PP_DIR_BASE_READ(engine)));
1879 drm_printf(m, "\tPP_DIR_DCLV: 0x%08x\n",
1880 I915_READ(RING_PP_DIR_DCLV(engine)));
1881 }
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001882}
1883
1884void intel_engine_dump(struct intel_engine_cs *engine,
1885 struct drm_printer *m,
1886 const char *header, ...)
1887{
1888 struct intel_breadcrumbs * const b = &engine->breadcrumbs;
1889 const struct intel_engine_execlists * const execlists = &engine->execlists;
1890 struct i915_gpu_error * const error = &engine->i915->gpu_error;
Chris Wilsone61e0f52018-02-21 09:56:36 +00001891 struct i915_request *rq;
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001892 struct rb_node *rb;
1893
1894 if (header) {
1895 va_list ap;
1896
1897 va_start(ap, header);
1898 drm_vprintf(m, header, &ap);
1899 va_end(ap);
1900 }
1901
1902 if (i915_terminally_wedged(&engine->i915->gpu_error))
1903 drm_printf(m, "*** WEDGED ***\n");
1904
1905 drm_printf(m, "\tcurrent seqno %x, last %x, hangcheck %x [%d ms], inflight %d\n",
1906 intel_engine_get_seqno(engine),
1907 intel_engine_last_submit(engine),
1908 engine->hangcheck.seqno,
1909 jiffies_to_msecs(jiffies - engine->hangcheck.action_timestamp),
1910 engine->timeline->inflight_seqnos);
1911 drm_printf(m, "\tReset count: %d (global %d)\n",
1912 i915_reset_engine_count(error, engine),
1913 i915_reset_count(error));
1914
1915 rcu_read_lock();
1916
1917 drm_printf(m, "\tRequests:\n");
1918
1919 rq = list_first_entry(&engine->timeline->requests,
Chris Wilsone61e0f52018-02-21 09:56:36 +00001920 struct i915_request, link);
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001921 if (&rq->link != &engine->timeline->requests)
1922 print_request(m, rq, "\t\tfirst ");
1923
1924 rq = list_last_entry(&engine->timeline->requests,
Chris Wilsone61e0f52018-02-21 09:56:36 +00001925 struct i915_request, link);
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001926 if (&rq->link != &engine->timeline->requests)
1927 print_request(m, rq, "\t\tlast ");
1928
1929 rq = i915_gem_find_active_request(engine);
1930 if (rq) {
1931 print_request(m, rq, "\t\tactive ");
1932 drm_printf(m,
1933 "\t\t[head %04x, postfix %04x, tail %04x, batch 0x%08x_%08x]\n",
1934 rq->head, rq->postfix, rq->tail,
1935 rq->batch ? upper_32_bits(rq->batch->node.start) : ~0u,
1936 rq->batch ? lower_32_bits(rq->batch->node.start) : ~0u);
Chris Wilsonef5032a2018-03-07 13:42:24 +00001937 drm_printf(m, "\t\tring->start: 0x%08x\n",
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001938 i915_ggtt_offset(rq->ring->vma));
Chris Wilsonef5032a2018-03-07 13:42:24 +00001939 drm_printf(m, "\t\tring->head: 0x%08x\n",
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001940 rq->ring->head);
Chris Wilsonef5032a2018-03-07 13:42:24 +00001941 drm_printf(m, "\t\tring->tail: 0x%08x\n",
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001942 rq->ring->tail);
Chris Wilsonef5032a2018-03-07 13:42:24 +00001943 drm_printf(m, "\t\tring->emit: 0x%08x\n",
1944 rq->ring->emit);
1945 drm_printf(m, "\t\tring->space: 0x%08x\n",
1946 rq->ring->space);
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001947 }
1948
1949 rcu_read_unlock();
1950
1951 if (intel_runtime_pm_get_if_in_use(engine->i915)) {
1952 intel_engine_print_registers(engine, m);
1953 intel_runtime_pm_put(engine->i915);
1954 } else {
1955 drm_printf(m, "\tDevice is asleep; skipping register dump\n");
1956 }
Chris Wilsonf636edb2017-10-09 12:02:57 +01001957
Chris Wilsona27d5a42017-10-15 21:43:10 +01001958 spin_lock_irq(&engine->timeline->lock);
1959 list_for_each_entry(rq, &engine->timeline->requests, link)
1960 print_request(m, rq, "\t\tE ");
Chris Wilsonf6322ed2018-02-22 14:22:29 +00001961 drm_printf(m, "\t\tQueue priority: %d\n", execlists->queue_priority);
Chris Wilsona27d5a42017-10-15 21:43:10 +01001962 for (rb = execlists->first; rb; rb = rb_next(rb)) {
1963 struct i915_priolist *p =
1964 rb_entry(rb, typeof(*p), node);
1965
1966 list_for_each_entry(rq, &p->requests, priotree.link)
1967 print_request(m, rq, "\t\tQ ");
1968 }
1969 spin_unlock_irq(&engine->timeline->lock);
1970
Chris Wilsonf636edb2017-10-09 12:02:57 +01001971 spin_lock_irq(&b->rb_lock);
1972 for (rb = rb_first(&b->waiters); rb; rb = rb_next(rb)) {
1973 struct intel_wait *w = rb_entry(rb, typeof(*w), node);
1974
1975 drm_printf(m, "\t%s [%d] waiting for %x\n",
1976 w->tsk->comm, w->tsk->pid, w->seqno);
1977 }
1978 spin_unlock_irq(&b->rb_lock);
1979
Chris Wilson832265d2017-12-08 01:23:01 +00001980 drm_printf(m, "IRQ? 0x%lx (breadcrumbs? %s) (execlists? %s)\n",
1981 engine->irq_posted,
1982 yesno(test_bit(ENGINE_IRQ_BREADCRUMB,
1983 &engine->irq_posted)),
1984 yesno(test_bit(ENGINE_IRQ_EXECLIST,
1985 &engine->irq_posted)));
Chris Wilsonc1bf2722017-12-22 18:25:21 +00001986
1987 drm_printf(m, "HWSP:\n");
1988 hexdump(m, engine->status_page.page_addr, PAGE_SIZE);
1989
Chris Wilsonc400cc22017-11-07 15:22:11 +00001990 drm_printf(m, "Idle? %s\n", yesno(intel_engine_is_idle(engine)));
Chris Wilsonf636edb2017-10-09 12:02:57 +01001991}
1992
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +00001993static u8 user_class_map[] = {
1994 [I915_ENGINE_CLASS_RENDER] = RENDER_CLASS,
1995 [I915_ENGINE_CLASS_COPY] = COPY_ENGINE_CLASS,
1996 [I915_ENGINE_CLASS_VIDEO] = VIDEO_DECODE_CLASS,
1997 [I915_ENGINE_CLASS_VIDEO_ENHANCE] = VIDEO_ENHANCEMENT_CLASS,
1998};
1999
2000struct intel_engine_cs *
2001intel_engine_lookup_user(struct drm_i915_private *i915, u8 class, u8 instance)
2002{
2003 if (class >= ARRAY_SIZE(user_class_map))
2004 return NULL;
2005
2006 class = user_class_map[class];
2007
2008 GEM_BUG_ON(class > MAX_ENGINE_CLASS);
2009
2010 if (instance > MAX_ENGINE_INSTANCE)
2011 return NULL;
2012
2013 return i915->engine_class[class][instance];
2014}
2015
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00002016/**
2017 * intel_enable_engine_stats() - Enable engine busy tracking on engine
2018 * @engine: engine to enable stats collection
2019 *
2020 * Start collecting the engine busyness data for @engine.
2021 *
2022 * Returns 0 on success or a negative error code.
2023 */
2024int intel_enable_engine_stats(struct intel_engine_cs *engine)
2025{
Chris Wilson99e48bf2018-01-15 09:20:41 +00002026 struct intel_engine_execlists *execlists = &engine->execlists;
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00002027 unsigned long flags;
Chris Wilson99e48bf2018-01-15 09:20:41 +00002028 int err = 0;
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00002029
Tvrtko Ursulincf669b42017-11-29 10:28:05 +00002030 if (!intel_engine_supports_stats(engine))
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00002031 return -ENODEV;
2032
Chris Wilson99e48bf2018-01-15 09:20:41 +00002033 tasklet_disable(&execlists->tasklet);
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00002034 spin_lock_irqsave(&engine->stats.lock, flags);
Chris Wilson99e48bf2018-01-15 09:20:41 +00002035
2036 if (unlikely(engine->stats.enabled == ~0)) {
2037 err = -EBUSY;
2038 goto unlock;
2039 }
2040
Chris Wilson49007272018-01-11 07:30:31 +00002041 if (engine->stats.enabled++ == 0) {
Chris Wilson49007272018-01-11 07:30:31 +00002042 const struct execlist_port *port = execlists->port;
2043 unsigned int num_ports = execlists_num_ports(execlists);
2044
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00002045 engine->stats.enabled_at = ktime_get();
Chris Wilson49007272018-01-11 07:30:31 +00002046
2047 /* XXX submission method oblivious? */
2048 while (num_ports-- && port_isset(port)) {
2049 engine->stats.active++;
2050 port++;
2051 }
2052
2053 if (engine->stats.active)
2054 engine->stats.start = engine->stats.enabled_at;
2055 }
Chris Wilson99e48bf2018-01-15 09:20:41 +00002056
2057unlock:
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00002058 spin_unlock_irqrestore(&engine->stats.lock, flags);
Chris Wilson99e48bf2018-01-15 09:20:41 +00002059 tasklet_enable(&execlists->tasklet);
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00002060
Chris Wilson99e48bf2018-01-15 09:20:41 +00002061 return err;
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00002062}
2063
2064static ktime_t __intel_engine_get_busy_time(struct intel_engine_cs *engine)
2065{
2066 ktime_t total = engine->stats.total;
2067
2068 /*
2069 * If the engine is executing something at the moment
2070 * add it to the total.
2071 */
2072 if (engine->stats.active)
2073 total = ktime_add(total,
2074 ktime_sub(ktime_get(), engine->stats.start));
2075
2076 return total;
2077}
2078
2079/**
2080 * intel_engine_get_busy_time() - Return current accumulated engine busyness
2081 * @engine: engine to report on
2082 *
2083 * Returns accumulated time @engine was busy since engine stats were enabled.
2084 */
2085ktime_t intel_engine_get_busy_time(struct intel_engine_cs *engine)
2086{
2087 ktime_t total;
2088 unsigned long flags;
2089
2090 spin_lock_irqsave(&engine->stats.lock, flags);
2091 total = __intel_engine_get_busy_time(engine);
2092 spin_unlock_irqrestore(&engine->stats.lock, flags);
2093
2094 return total;
2095}
2096
2097/**
2098 * intel_disable_engine_stats() - Disable engine busy tracking on engine
2099 * @engine: engine to disable stats collection
2100 *
2101 * Stops collecting the engine busyness data for @engine.
2102 */
2103void intel_disable_engine_stats(struct intel_engine_cs *engine)
2104{
2105 unsigned long flags;
2106
Tvrtko Ursulincf669b42017-11-29 10:28:05 +00002107 if (!intel_engine_supports_stats(engine))
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00002108 return;
2109
2110 spin_lock_irqsave(&engine->stats.lock, flags);
2111 WARN_ON_ONCE(engine->stats.enabled == 0);
2112 if (--engine->stats.enabled == 0) {
2113 engine->stats.total = __intel_engine_get_busy_time(engine);
2114 engine->stats.active = 0;
2115 }
2116 spin_unlock_irqrestore(&engine->stats.lock, flags);
2117}
2118
Chris Wilsonf97fbf92017-02-13 17:15:14 +00002119#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
2120#include "selftests/mock_engine.c"
2121#endif