blob: ce7fcf55ba1867983eb0c7b10bcd8216a8485117 [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 },
126 [VECS] = {
Tvrtko Ursulin5ec2cf72016-08-16 17:04:20 +0100127 .hw_id = VECS_HW,
Chris Wilson1d39f282017-04-11 13:43:06 +0100128 .uabi_id = I915_EXEC_VEBOX,
Daniele Ceraolo Spurio09081802017-04-10 07:34:29 -0700129 .class = VIDEO_ENHANCEMENT_CLASS,
130 .instance = 0,
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100131 .mmio_base = VEBOX_RING_BASE,
132 .irq_shift = GEN8_VECS_IRQ_SHIFT,
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100133 },
134};
135
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +0300136/**
137 * ___intel_engine_context_size() - return the size of the context for an engine
138 * @dev_priv: i915 device private
139 * @class: engine class
140 *
141 * Each engine class may require a different amount of space for a context
142 * image.
143 *
144 * Return: size (in bytes) of an engine class specific context image
145 *
146 * Note: this size includes the HWSP, which is part of the context image
147 * in LRC mode, but does not include the "shared data page" used with
148 * GuC submission. The caller should account for this if using the GuC.
149 */
150static u32
151__intel_engine_context_size(struct drm_i915_private *dev_priv, u8 class)
152{
153 u32 cxt_size;
154
155 BUILD_BUG_ON(I915_GTT_PAGE_SIZE != PAGE_SIZE);
156
157 switch (class) {
158 case RENDER_CLASS:
159 switch (INTEL_GEN(dev_priv)) {
160 default:
161 MISSING_CASE(INTEL_GEN(dev_priv));
Oscar Mateo7ab4adb2018-01-11 14:55:06 -0800162 return DEFAULT_LR_CONTEXT_RENDER_SIZE;
Tvrtko Ursulinb86aa442018-01-11 14:55:07 -0800163 case 11:
164 return GEN11_LR_CONTEXT_RENDER_SIZE;
Rodrigo Vivif65f8412017-07-06 14:06:24 -0700165 case 10:
Oscar Mateo7fd0b1a2017-09-21 16:19:49 -0700166 return GEN10_LR_CONTEXT_RENDER_SIZE;
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +0300167 case 9:
168 return GEN9_LR_CONTEXT_RENDER_SIZE;
169 case 8:
Chris Wilsonfb5c5512017-11-20 20:55:00 +0000170 return GEN8_LR_CONTEXT_RENDER_SIZE;
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +0300171 case 7:
172 if (IS_HASWELL(dev_priv))
173 return HSW_CXT_TOTAL_SIZE;
174
175 cxt_size = I915_READ(GEN7_CXT_SIZE);
176 return round_up(GEN7_CXT_TOTAL_SIZE(cxt_size) * 64,
177 PAGE_SIZE);
178 case 6:
179 cxt_size = I915_READ(CXT_SIZE);
180 return round_up(GEN6_CXT_TOTAL_SIZE(cxt_size) * 64,
181 PAGE_SIZE);
182 case 5:
183 case 4:
184 case 3:
185 case 2:
186 /* For the special day when i810 gets merged. */
187 case 1:
188 return 0;
189 }
190 break;
191 default:
192 MISSING_CASE(class);
193 case VIDEO_DECODE_CLASS:
194 case VIDEO_ENHANCEMENT_CLASS:
195 case COPY_ENGINE_CLASS:
196 if (INTEL_GEN(dev_priv) < 8)
197 return 0;
198 return GEN8_LR_CONTEXT_OTHER_SIZE;
199 }
200}
201
Akash Goel3b3f1652016-10-13 22:44:48 +0530202static int
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100203intel_engine_setup(struct drm_i915_private *dev_priv,
204 enum intel_engine_id id)
205{
206 const struct engine_info *info = &intel_engines[id];
Oscar Mateob8400f02017-04-10 07:34:32 -0700207 const struct engine_class_info *class_info;
Akash Goel3b3f1652016-10-13 22:44:48 +0530208 struct intel_engine_cs *engine;
209
Oscar Mateob8400f02017-04-10 07:34:32 -0700210 GEM_BUG_ON(info->class >= ARRAY_SIZE(intel_engine_classes));
211 class_info = &intel_engine_classes[info->class];
212
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000213 if (GEM_WARN_ON(info->class > MAX_ENGINE_CLASS))
214 return -EINVAL;
215
216 if (GEM_WARN_ON(info->instance > MAX_ENGINE_INSTANCE))
217 return -EINVAL;
218
219 if (GEM_WARN_ON(dev_priv->engine_class[info->class][info->instance]))
220 return -EINVAL;
221
Akash Goel3b3f1652016-10-13 22:44:48 +0530222 GEM_BUG_ON(dev_priv->engine[id]);
223 engine = kzalloc(sizeof(*engine), GFP_KERNEL);
224 if (!engine)
225 return -ENOMEM;
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100226
227 engine->id = id;
228 engine->i915 = dev_priv;
Oscar Mateo6e516142017-04-10 07:34:31 -0700229 WARN_ON(snprintf(engine->name, sizeof(engine->name), "%s%u",
Oscar Mateob8400f02017-04-10 07:34:32 -0700230 class_info->name, info->instance) >=
231 sizeof(engine->name));
Tvrtko Ursulin5ec2cf72016-08-16 17:04:20 +0100232 engine->hw_id = engine->guc_id = info->hw_id;
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100233 engine->mmio_base = info->mmio_base;
234 engine->irq_shift = info->irq_shift;
Daniele Ceraolo Spurio09081802017-04-10 07:34:29 -0700235 engine->class = info->class;
236 engine->instance = info->instance;
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100237
Tvrtko Ursulin1803fcbc2017-11-10 14:26:27 +0000238 engine->uabi_id = info->uabi_id;
239 engine->uabi_class = class_info->uabi_class;
240
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +0300241 engine->context_size = __intel_engine_context_size(dev_priv,
242 engine->class);
243 if (WARN_ON(engine->context_size > BIT(20)))
244 engine->context_size = 0;
245
Chris Wilson0de91362016-11-14 20:41:01 +0000246 /* Nothing to do here, execute in order of dependencies */
247 engine->schedule = NULL;
248
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +0000249 spin_lock_init(&engine->stats.lock);
250
Changbin Du3fc03062017-03-13 10:47:11 +0800251 ATOMIC_INIT_NOTIFIER_HEAD(&engine->context_status_notifier);
252
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000253 dev_priv->engine_class[info->class][info->instance] = engine;
Akash Goel3b3f1652016-10-13 22:44:48 +0530254 dev_priv->engine[id] = engine;
255 return 0;
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100256}
257
258/**
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +0300259 * intel_engines_init_mmio() - allocate and prepare the Engine Command Streamers
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +0000260 * @dev_priv: i915 device private
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100261 *
262 * Return: non-zero if the initialization failed.
263 */
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +0300264int intel_engines_init_mmio(struct drm_i915_private *dev_priv)
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100265{
Tvrtko Ursulinc1bb1142016-08-10 16:22:10 +0100266 struct intel_device_info *device_info = mkwrite_device_info(dev_priv);
Chris Wilson5f9be052017-04-11 17:56:58 +0100267 const unsigned int ring_mask = INTEL_INFO(dev_priv)->ring_mask;
Akash Goel3b3f1652016-10-13 22:44:48 +0530268 struct intel_engine_cs *engine;
269 enum intel_engine_id id;
Chris Wilson5f9be052017-04-11 17:56:58 +0100270 unsigned int mask = 0;
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100271 unsigned int i;
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000272 int err;
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100273
Tvrtko Ursulin70006ad2016-10-13 11:02:56 +0100274 WARN_ON(ring_mask == 0);
275 WARN_ON(ring_mask &
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100276 GENMASK(sizeof(mask) * BITS_PER_BYTE - 1, I915_NUM_ENGINES));
277
278 for (i = 0; i < ARRAY_SIZE(intel_engines); i++) {
279 if (!HAS_ENGINE(dev_priv, i))
280 continue;
281
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000282 err = intel_engine_setup(dev_priv, i);
283 if (err)
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100284 goto cleanup;
285
286 mask |= ENGINE_MASK(i);
287 }
288
289 /*
290 * Catch failures to update intel_engines table when the new engines
291 * are added to the driver by a warning and disabling the forgotten
292 * engines.
293 */
Tvrtko Ursulin70006ad2016-10-13 11:02:56 +0100294 if (WARN_ON(mask != ring_mask))
Tvrtko Ursulinc1bb1142016-08-10 16:22:10 +0100295 device_info->ring_mask = mask;
296
Chris Wilson5f9be052017-04-11 17:56:58 +0100297 /* We always presume we have at least RCS available for later probing */
298 if (WARN_ON(!HAS_ENGINE(dev_priv, RCS))) {
299 err = -ENODEV;
300 goto cleanup;
301 }
302
Tvrtko Ursulinc1bb1142016-08-10 16:22:10 +0100303 device_info->num_rings = hweight32(mask);
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100304
Michel Thierryce453b32017-11-10 16:44:47 -0800305 i915_check_and_clear_faults(dev_priv);
306
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100307 return 0;
308
309cleanup:
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000310 for_each_engine(engine, dev_priv, id)
311 kfree(engine);
312 return err;
313}
314
315/**
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +0300316 * intel_engines_init() - init the Engine Command Streamers
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000317 * @dev_priv: i915 device private
318 *
319 * Return: non-zero if the initialization failed.
320 */
321int intel_engines_init(struct drm_i915_private *dev_priv)
322{
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000323 struct intel_engine_cs *engine;
324 enum intel_engine_id id, err_id;
Tvrtko Ursulin33def1f2017-06-16 14:03:38 +0100325 int err;
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000326
Akash Goel3b3f1652016-10-13 22:44:48 +0530327 for_each_engine(engine, dev_priv, id) {
Oscar Mateob8400f02017-04-10 07:34:32 -0700328 const struct engine_class_info *class_info =
329 &intel_engine_classes[engine->class];
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000330 int (*init)(struct intel_engine_cs *engine);
331
Chris Wilsonfb5c5512017-11-20 20:55:00 +0000332 if (HAS_EXECLISTS(dev_priv))
Oscar Mateob8400f02017-04-10 07:34:32 -0700333 init = class_info->init_execlists;
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000334 else
Oscar Mateob8400f02017-04-10 07:34:32 -0700335 init = class_info->init_legacy;
Tvrtko Ursulin33def1f2017-06-16 14:03:38 +0100336
337 err = -EINVAL;
338 err_id = id;
339
340 if (GEM_WARN_ON(!init))
341 goto cleanup;
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000342
343 err = init(engine);
Tvrtko Ursulin33def1f2017-06-16 14:03:38 +0100344 if (err)
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000345 goto cleanup;
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000346
Chris Wilsonff44ad52017-03-16 17:13:03 +0000347 GEM_BUG_ON(!engine->submit_request);
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000348 }
349
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000350 return 0;
351
352cleanup:
353 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursulin33def1f2017-06-16 14:03:38 +0100354 if (id >= err_id) {
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000355 kfree(engine);
Tvrtko Ursulin33def1f2017-06-16 14:03:38 +0100356 dev_priv->engine[id] = NULL;
357 } else {
Tvrtko Ursulin8ee7c6e2017-02-16 12:23:22 +0000358 dev_priv->gt.cleanup_engine(engine);
Tvrtko Ursulin33def1f2017-06-16 14:03:38 +0100359 }
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100360 }
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000361 return err;
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100362}
363
Chris Wilson73cb9702016-10-28 13:58:46 +0100364void intel_engine_init_global_seqno(struct intel_engine_cs *engine, u32 seqno)
Chris Wilson57f275a2016-08-15 10:49:00 +0100365{
366 struct drm_i915_private *dev_priv = engine->i915;
367
368 /* Our semaphore implementation is strictly monotonic (i.e. we proceed
369 * so long as the semaphore value in the register/page is greater
370 * than the sync value), so whenever we reset the seqno,
371 * so long as we reset the tracking semaphore value to 0, it will
372 * always be before the next request's seqno. If we don't reset
373 * the semaphore value, then when the seqno moves backwards all
374 * future waits will complete instantly (causing rendering corruption).
375 */
376 if (IS_GEN6(dev_priv) || IS_GEN7(dev_priv)) {
377 I915_WRITE(RING_SYNC_0(engine->mmio_base), 0);
378 I915_WRITE(RING_SYNC_1(engine->mmio_base), 0);
379 if (HAS_VEBOX(dev_priv))
380 I915_WRITE(RING_SYNC_2(engine->mmio_base), 0);
381 }
Chris Wilson57f275a2016-08-15 10:49:00 +0100382
383 intel_write_status_page(engine, I915_GEM_HWS_INDEX, seqno);
Chris Wilson14a6bbf2017-03-14 11:14:52 +0000384 clear_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted);
Chris Wilson73cb9702016-10-28 13:58:46 +0100385
Chris Wilson57f275a2016-08-15 10:49:00 +0100386 /* After manually advancing the seqno, fake the interrupt in case
387 * there are any waiters for that seqno.
388 */
389 intel_engine_wakeup(engine);
Chris Wilson2ca9faa2017-04-05 16:30:54 +0100390
391 GEM_BUG_ON(intel_engine_get_seqno(engine) != seqno);
Chris Wilson57f275a2016-08-15 10:49:00 +0100392}
393
Chris Wilson73cb9702016-10-28 13:58:46 +0100394static void intel_engine_init_timeline(struct intel_engine_cs *engine)
Chris Wilsondcff85c2016-08-05 10:14:11 +0100395{
Chris Wilson73cb9702016-10-28 13:58:46 +0100396 engine->timeline = &engine->i915->gt.global_timeline.engine[engine->id];
Chris Wilsondcff85c2016-08-05 10:14:11 +0100397}
398
Mika Kuoppala19df9a52017-09-22 15:43:04 +0300399static bool csb_force_mmio(struct drm_i915_private *i915)
400{
Mika Kuoppala19df9a52017-09-22 15:43:04 +0300401 /*
402 * IOMMU adds unpredictable latency causing the CSB write (from the
403 * GPU into the HWSP) to only be visible some time after the interrupt
404 * (missed breadcrumb syndrome).
405 */
406 if (intel_vtd_active())
407 return true;
408
Weinan Li1fd51d92017-10-15 11:55:25 +0800409 /* Older GVT emulation depends upon intercepting CSB mmio */
410 if (intel_vgpu_active(i915) && !intel_vgpu_has_hwsp_emulation(i915))
411 return true;
412
Mika Kuoppala19df9a52017-09-22 15:43:04 +0300413 return false;
414}
415
416static void intel_engine_init_execlist(struct intel_engine_cs *engine)
417{
418 struct intel_engine_execlists * const execlists = &engine->execlists;
419
420 execlists->csb_use_mmio = csb_force_mmio(engine->i915);
421
Mika Kuoppala76e70082017-09-22 15:43:07 +0300422 execlists->port_mask = 1;
423 BUILD_BUG_ON_NOT_POWER_OF_2(execlists_num_ports(execlists));
424 GEM_BUG_ON(execlists_num_ports(execlists) > EXECLIST_MAX_PORTS);
425
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000426 execlists->queue_priority = INT_MIN;
Mika Kuoppala19df9a52017-09-22 15:43:04 +0300427 execlists->queue = RB_ROOT;
428 execlists->first = NULL;
429}
430
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100431/**
432 * intel_engines_setup_common - setup engine state not requiring hw access
433 * @engine: Engine to setup.
434 *
435 * Initializes @engine@ structure members shared between legacy and execlists
436 * submission modes which do not require hardware access.
437 *
438 * Typically done early in the submission mode specific engine setup stage.
439 */
440void intel_engine_setup_common(struct intel_engine_cs *engine)
441{
Mika Kuoppala19df9a52017-09-22 15:43:04 +0300442 intel_engine_init_execlist(engine);
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100443
Chris Wilson73cb9702016-10-28 13:58:46 +0100444 intel_engine_init_timeline(engine);
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100445 intel_engine_init_hangcheck(engine);
Chris Wilson115003e92016-08-04 16:32:19 +0100446 i915_gem_batch_pool_init(engine, &engine->batch_pool);
Chris Wilson7756e452016-08-18 17:17:10 +0100447
448 intel_engine_init_cmd_parser(engine);
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100449}
450
Chris Wilsonadc320c2016-08-15 10:48:59 +0100451int intel_engine_create_scratch(struct intel_engine_cs *engine, int size)
452{
453 struct drm_i915_gem_object *obj;
454 struct i915_vma *vma;
455 int ret;
456
457 WARN_ON(engine->scratch);
458
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000459 obj = i915_gem_object_create_stolen(engine->i915, size);
Chris Wilsonadc320c2016-08-15 10:48:59 +0100460 if (!obj)
Chris Wilson920cf412016-10-28 13:58:30 +0100461 obj = i915_gem_object_create_internal(engine->i915, size);
Chris Wilsonadc320c2016-08-15 10:48:59 +0100462 if (IS_ERR(obj)) {
463 DRM_ERROR("Failed to allocate scratch page\n");
464 return PTR_ERR(obj);
465 }
466
Chris Wilsona01cb372017-01-16 15:21:30 +0000467 vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL);
Chris Wilsonadc320c2016-08-15 10:48:59 +0100468 if (IS_ERR(vma)) {
469 ret = PTR_ERR(vma);
470 goto err_unref;
471 }
472
473 ret = i915_vma_pin(vma, 0, 4096, PIN_GLOBAL | PIN_HIGH);
474 if (ret)
475 goto err_unref;
476
477 engine->scratch = vma;
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100478 DRM_DEBUG_DRIVER("%s pipe control offset: 0x%08x\n",
479 engine->name, i915_ggtt_offset(vma));
Chris Wilsonadc320c2016-08-15 10:48:59 +0100480 return 0;
481
482err_unref:
483 i915_gem_object_put(obj);
484 return ret;
485}
486
487static void intel_engine_cleanup_scratch(struct intel_engine_cs *engine)
488{
Chris Wilson19880c42016-08-15 10:49:05 +0100489 i915_vma_unpin_and_release(&engine->scratch);
Chris Wilsonadc320c2016-08-15 10:48:59 +0100490}
491
Daniele Ceraolo Spurio486e93f2017-09-13 09:56:02 +0100492static void cleanup_phys_status_page(struct intel_engine_cs *engine)
493{
494 struct drm_i915_private *dev_priv = engine->i915;
495
496 if (!dev_priv->status_page_dmah)
497 return;
498
499 drm_pci_free(&dev_priv->drm, dev_priv->status_page_dmah);
500 engine->status_page.page_addr = NULL;
501}
502
503static void cleanup_status_page(struct intel_engine_cs *engine)
504{
505 struct i915_vma *vma;
506 struct drm_i915_gem_object *obj;
507
508 vma = fetch_and_zero(&engine->status_page.vma);
509 if (!vma)
510 return;
511
512 obj = vma->obj;
513
514 i915_vma_unpin(vma);
515 i915_vma_close(vma);
516
517 i915_gem_object_unpin_map(obj);
518 __i915_gem_object_release_unless_active(obj);
519}
520
521static int init_status_page(struct intel_engine_cs *engine)
522{
523 struct drm_i915_gem_object *obj;
524 struct i915_vma *vma;
525 unsigned int flags;
526 void *vaddr;
527 int ret;
528
529 obj = i915_gem_object_create_internal(engine->i915, PAGE_SIZE);
530 if (IS_ERR(obj)) {
531 DRM_ERROR("Failed to allocate status page\n");
532 return PTR_ERR(obj);
533 }
534
535 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
536 if (ret)
537 goto err;
538
539 vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL);
540 if (IS_ERR(vma)) {
541 ret = PTR_ERR(vma);
542 goto err;
543 }
544
545 flags = PIN_GLOBAL;
546 if (!HAS_LLC(engine->i915))
547 /* On g33, we cannot place HWS above 256MiB, so
548 * restrict its pinning to the low mappable arena.
549 * Though this restriction is not documented for
550 * gen4, gen5, or byt, they also behave similarly
551 * and hang if the HWS is placed at the top of the
552 * GTT. To generalise, it appears that all !llc
553 * platforms have issues with us placing the HWS
554 * above the mappable region (even though we never
555 * actually map it).
556 */
557 flags |= PIN_MAPPABLE;
Chris Wilson34a04e52017-09-13 09:56:03 +0100558 else
559 flags |= PIN_HIGH;
Daniele Ceraolo Spurio486e93f2017-09-13 09:56:02 +0100560 ret = i915_vma_pin(vma, 0, 4096, flags);
561 if (ret)
562 goto err;
563
564 vaddr = i915_gem_object_pin_map(obj, I915_MAP_WB);
565 if (IS_ERR(vaddr)) {
566 ret = PTR_ERR(vaddr);
567 goto err_unpin;
568 }
569
570 engine->status_page.vma = vma;
571 engine->status_page.ggtt_offset = i915_ggtt_offset(vma);
572 engine->status_page.page_addr = memset(vaddr, 0, PAGE_SIZE);
573
574 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
575 engine->name, i915_ggtt_offset(vma));
576 return 0;
577
578err_unpin:
579 i915_vma_unpin(vma);
580err:
581 i915_gem_object_put(obj);
582 return ret;
583}
584
585static int init_phys_status_page(struct intel_engine_cs *engine)
586{
587 struct drm_i915_private *dev_priv = engine->i915;
588
589 GEM_BUG_ON(engine->id != RCS);
590
591 dev_priv->status_page_dmah =
592 drm_pci_alloc(&dev_priv->drm, PAGE_SIZE, PAGE_SIZE);
593 if (!dev_priv->status_page_dmah)
594 return -ENOMEM;
595
596 engine->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
597 memset(engine->status_page.page_addr, 0, PAGE_SIZE);
598
599 return 0;
600}
601
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100602/**
603 * intel_engines_init_common - initialize cengine state which might require hw access
604 * @engine: Engine to initialize.
605 *
606 * Initializes @engine@ structure members shared between legacy and execlists
607 * submission modes which do require hardware access.
608 *
609 * Typcally done at later stages of submission mode specific engine setup.
610 *
611 * Returns zero on success or an error code on failure.
612 */
613int intel_engine_init_common(struct intel_engine_cs *engine)
614{
Chris Wilson266a2402017-05-04 10:33:08 +0100615 struct intel_ring *ring;
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100616 int ret;
617
Chris Wilsonff44ad52017-03-16 17:13:03 +0000618 engine->set_default_submission(engine);
619
Chris Wilsone8a9c582016-12-18 15:37:20 +0000620 /* We may need to do things with the shrinker which
621 * require us to immediately switch back to the default
622 * context. This can cause a problem as pinning the
623 * default context also requires GTT space which may not
624 * be available. To avoid this we always pin the default
625 * context.
626 */
Chris Wilson266a2402017-05-04 10:33:08 +0100627 ring = engine->context_pin(engine, engine->i915->kernel_context);
628 if (IS_ERR(ring))
629 return PTR_ERR(ring);
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100630
Chris Wilsone7af3112017-10-03 21:34:48 +0100631 /*
632 * Similarly the preempt context must always be available so that
633 * we can interrupt the engine at any time.
634 */
Chris Wilsond6376372018-02-07 21:05:44 +0000635 if (engine->i915->preempt_context) {
Chris Wilsone7af3112017-10-03 21:34:48 +0100636 ring = engine->context_pin(engine,
637 engine->i915->preempt_context);
638 if (IS_ERR(ring)) {
639 ret = PTR_ERR(ring);
640 goto err_unpin_kernel;
641 }
642 }
643
Chris Wilsone8a9c582016-12-18 15:37:20 +0000644 ret = intel_engine_init_breadcrumbs(engine);
645 if (ret)
Chris Wilsone7af3112017-10-03 21:34:48 +0100646 goto err_unpin_preempt;
Chris Wilsone8a9c582016-12-18 15:37:20 +0000647
Daniele Ceraolo Spurio486e93f2017-09-13 09:56:02 +0100648 if (HWS_NEEDS_PHYSICAL(engine->i915))
649 ret = init_phys_status_page(engine);
650 else
651 ret = init_status_page(engine);
652 if (ret)
Chris Wilson7c2fa7f2017-11-10 14:26:34 +0000653 goto err_breadcrumbs;
Chris Wilson4e50f082016-10-28 13:58:31 +0100654
Chris Wilson7756e452016-08-18 17:17:10 +0100655 return 0;
Chris Wilsone8a9c582016-12-18 15:37:20 +0000656
Daniele Ceraolo Spurio486e93f2017-09-13 09:56:02 +0100657err_breadcrumbs:
658 intel_engine_fini_breadcrumbs(engine);
Chris Wilsone7af3112017-10-03 21:34:48 +0100659err_unpin_preempt:
Chris Wilsond6376372018-02-07 21:05:44 +0000660 if (engine->i915->preempt_context)
Chris Wilsone7af3112017-10-03 21:34:48 +0100661 engine->context_unpin(engine, engine->i915->preempt_context);
662err_unpin_kernel:
Chris Wilsone8a9c582016-12-18 15:37:20 +0000663 engine->context_unpin(engine, engine->i915->kernel_context);
664 return ret;
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100665}
Chris Wilson96a945a2016-08-03 13:19:16 +0100666
667/**
668 * intel_engines_cleanup_common - cleans up the engine state created by
669 * the common initiailizers.
670 * @engine: Engine to cleanup.
671 *
672 * This cleans up everything created by the common helpers.
673 */
674void intel_engine_cleanup_common(struct intel_engine_cs *engine)
675{
Chris Wilsonadc320c2016-08-15 10:48:59 +0100676 intel_engine_cleanup_scratch(engine);
677
Daniele Ceraolo Spurio486e93f2017-09-13 09:56:02 +0100678 if (HWS_NEEDS_PHYSICAL(engine->i915))
679 cleanup_phys_status_page(engine);
680 else
681 cleanup_status_page(engine);
682
Chris Wilson96a945a2016-08-03 13:19:16 +0100683 intel_engine_fini_breadcrumbs(engine);
Chris Wilson7756e452016-08-18 17:17:10 +0100684 intel_engine_cleanup_cmd_parser(engine);
Chris Wilson96a945a2016-08-03 13:19:16 +0100685 i915_gem_batch_pool_fini(&engine->batch_pool);
Chris Wilsone8a9c582016-12-18 15:37:20 +0000686
Chris Wilsond2b4b972017-11-10 14:26:33 +0000687 if (engine->default_state)
688 i915_gem_object_put(engine->default_state);
689
Chris Wilsond6376372018-02-07 21:05:44 +0000690 if (engine->i915->preempt_context)
Chris Wilsone7af3112017-10-03 21:34:48 +0100691 engine->context_unpin(engine, engine->i915->preempt_context);
Chris Wilsone8a9c582016-12-18 15:37:20 +0000692 engine->context_unpin(engine, engine->i915->kernel_context);
Chris Wilson96a945a2016-08-03 13:19:16 +0100693}
Chris Wilson1b365952016-10-04 21:11:31 +0100694
Chris Wilson3ceda3a2018-02-12 10:24:15 +0000695u64 intel_engine_get_active_head(const struct intel_engine_cs *engine)
Chris Wilson1b365952016-10-04 21:11:31 +0100696{
697 struct drm_i915_private *dev_priv = engine->i915;
698 u64 acthd;
699
700 if (INTEL_GEN(dev_priv) >= 8)
701 acthd = I915_READ64_2x32(RING_ACTHD(engine->mmio_base),
702 RING_ACTHD_UDW(engine->mmio_base));
703 else if (INTEL_GEN(dev_priv) >= 4)
704 acthd = I915_READ(RING_ACTHD(engine->mmio_base));
705 else
706 acthd = I915_READ(ACTHD);
707
708 return acthd;
709}
710
Chris Wilson3ceda3a2018-02-12 10:24:15 +0000711u64 intel_engine_get_last_batch_head(const struct intel_engine_cs *engine)
Chris Wilson1b365952016-10-04 21:11:31 +0100712{
713 struct drm_i915_private *dev_priv = engine->i915;
714 u64 bbaddr;
715
716 if (INTEL_GEN(dev_priv) >= 8)
717 bbaddr = I915_READ64_2x32(RING_BBADDR(engine->mmio_base),
718 RING_BBADDR_UDW(engine->mmio_base));
719 else
720 bbaddr = I915_READ(RING_BBADDR(engine->mmio_base));
721
722 return bbaddr;
723}
Chris Wilson0e704472016-10-12 10:05:17 +0100724
725const char *i915_cache_level_str(struct drm_i915_private *i915, int type)
726{
727 switch (type) {
728 case I915_CACHE_NONE: return " uncached";
729 case I915_CACHE_LLC: return HAS_LLC(i915) ? " LLC" : " snooped";
730 case I915_CACHE_L3_LLC: return " L3+LLC";
731 case I915_CACHE_WT: return " WT";
732 default: return "";
733 }
734}
735
736static inline uint32_t
737read_subslice_reg(struct drm_i915_private *dev_priv, int slice,
738 int subslice, i915_reg_t reg)
739{
740 uint32_t mcr;
741 uint32_t ret;
742 enum forcewake_domains fw_domains;
743
744 fw_domains = intel_uncore_forcewake_for_reg(dev_priv, reg,
745 FW_REG_READ);
746 fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
747 GEN8_MCR_SELECTOR,
748 FW_REG_READ | FW_REG_WRITE);
749
750 spin_lock_irq(&dev_priv->uncore.lock);
751 intel_uncore_forcewake_get__locked(dev_priv, fw_domains);
752
753 mcr = I915_READ_FW(GEN8_MCR_SELECTOR);
754 /*
755 * The HW expects the slice and sublice selectors to be reset to 0
756 * after reading out the registers.
757 */
758 WARN_ON_ONCE(mcr & (GEN8_MCR_SLICE_MASK | GEN8_MCR_SUBSLICE_MASK));
759 mcr &= ~(GEN8_MCR_SLICE_MASK | GEN8_MCR_SUBSLICE_MASK);
760 mcr |= GEN8_MCR_SLICE(slice) | GEN8_MCR_SUBSLICE(subslice);
761 I915_WRITE_FW(GEN8_MCR_SELECTOR, mcr);
762
763 ret = I915_READ_FW(reg);
764
765 mcr &= ~(GEN8_MCR_SLICE_MASK | GEN8_MCR_SUBSLICE_MASK);
766 I915_WRITE_FW(GEN8_MCR_SELECTOR, mcr);
767
768 intel_uncore_forcewake_put__locked(dev_priv, fw_domains);
769 spin_unlock_irq(&dev_priv->uncore.lock);
770
771 return ret;
772}
773
774/* NB: please notice the memset */
775void intel_engine_get_instdone(struct intel_engine_cs *engine,
776 struct intel_instdone *instdone)
777{
778 struct drm_i915_private *dev_priv = engine->i915;
779 u32 mmio_base = engine->mmio_base;
780 int slice;
781 int subslice;
782
783 memset(instdone, 0, sizeof(*instdone));
784
785 switch (INTEL_GEN(dev_priv)) {
786 default:
787 instdone->instdone = I915_READ(RING_INSTDONE(mmio_base));
788
789 if (engine->id != RCS)
790 break;
791
792 instdone->slice_common = I915_READ(GEN7_SC_INSTDONE);
793 for_each_instdone_slice_subslice(dev_priv, slice, subslice) {
794 instdone->sampler[slice][subslice] =
795 read_subslice_reg(dev_priv, slice, subslice,
796 GEN7_SAMPLER_INSTDONE);
797 instdone->row[slice][subslice] =
798 read_subslice_reg(dev_priv, slice, subslice,
799 GEN7_ROW_INSTDONE);
800 }
801 break;
802 case 7:
803 instdone->instdone = I915_READ(RING_INSTDONE(mmio_base));
804
805 if (engine->id != RCS)
806 break;
807
808 instdone->slice_common = I915_READ(GEN7_SC_INSTDONE);
809 instdone->sampler[0][0] = I915_READ(GEN7_SAMPLER_INSTDONE);
810 instdone->row[0][0] = I915_READ(GEN7_ROW_INSTDONE);
811
812 break;
813 case 6:
814 case 5:
815 case 4:
816 instdone->instdone = I915_READ(RING_INSTDONE(mmio_base));
817
818 if (engine->id == RCS)
819 /* HACK: Using the wrong struct member */
820 instdone->slice_common = I915_READ(GEN4_INSTDONE1);
821 break;
822 case 3:
823 case 2:
824 instdone->instdone = I915_READ(GEN2_INSTDONE);
825 break;
826 }
827}
Chris Wilsonf97fbf92017-02-13 17:15:14 +0000828
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +0000829static int wa_add(struct drm_i915_private *dev_priv,
830 i915_reg_t addr,
831 const u32 mask, const u32 val)
832{
833 const u32 idx = dev_priv->workarounds.count;
834
835 if (WARN_ON(idx >= I915_MAX_WA_REGS))
836 return -ENOSPC;
837
838 dev_priv->workarounds.reg[idx].addr = addr;
839 dev_priv->workarounds.reg[idx].value = val;
840 dev_priv->workarounds.reg[idx].mask = mask;
841
842 dev_priv->workarounds.count++;
843
844 return 0;
845}
846
847#define WA_REG(addr, mask, val) do { \
848 const int r = wa_add(dev_priv, (addr), (mask), (val)); \
849 if (r) \
850 return r; \
851 } while (0)
852
853#define WA_SET_BIT_MASKED(addr, mask) \
854 WA_REG(addr, (mask), _MASKED_BIT_ENABLE(mask))
855
856#define WA_CLR_BIT_MASKED(addr, mask) \
857 WA_REG(addr, (mask), _MASKED_BIT_DISABLE(mask))
858
859#define WA_SET_FIELD_MASKED(addr, mask, value) \
860 WA_REG(addr, mask, _MASKED_FIELD(mask, value))
861
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +0000862static int wa_ring_whitelist_reg(struct intel_engine_cs *engine,
863 i915_reg_t reg)
864{
865 struct drm_i915_private *dev_priv = engine->i915;
866 struct i915_workarounds *wa = &dev_priv->workarounds;
867 const uint32_t index = wa->hw_whitelist_count[engine->id];
868
869 if (WARN_ON(index >= RING_MAX_NONPRIV_SLOTS))
870 return -EINVAL;
871
Oscar Mateo32ced392017-09-28 15:40:39 -0700872 I915_WRITE(RING_FORCE_TO_NONPRIV(engine->mmio_base, index),
873 i915_mmio_reg_offset(reg));
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +0000874 wa->hw_whitelist_count[engine->id]++;
875
876 return 0;
877}
878
879static int gen8_init_workarounds(struct intel_engine_cs *engine)
880{
881 struct drm_i915_private *dev_priv = engine->i915;
882
883 WA_SET_BIT_MASKED(INSTPM, INSTPM_FORCE_ORDERING);
884
885 /* WaDisableAsyncFlipPerfMode:bdw,chv */
886 WA_SET_BIT_MASKED(MI_MODE, ASYNC_FLIP_PERF_DISABLE);
887
888 /* WaDisablePartialInstShootdown:bdw,chv */
889 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
890 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
891
892 /* Use Force Non-Coherent whenever executing a 3D context. This is a
893 * workaround for for a possible hang in the unlikely event a TLB
894 * invalidation occurs during a PSD flush.
895 */
896 /* WaForceEnableNonCoherent:bdw,chv */
897 /* WaHdcDisableFetchWhenMasked:bdw,chv */
898 WA_SET_BIT_MASKED(HDC_CHICKEN0,
899 HDC_DONOT_FETCH_MEM_WHEN_MASKED |
900 HDC_FORCE_NON_COHERENT);
901
902 /* From the Haswell PRM, Command Reference: Registers, CACHE_MODE_0:
903 * "The Hierarchical Z RAW Stall Optimization allows non-overlapping
904 * polygons in the same 8x4 pixel/sample area to be processed without
905 * stalling waiting for the earlier ones to write to Hierarchical Z
906 * buffer."
907 *
908 * This optimization is off by default for BDW and CHV; turn it on.
909 */
910 WA_CLR_BIT_MASKED(CACHE_MODE_0_GEN7, HIZ_RAW_STALL_OPT_DISABLE);
911
912 /* Wa4x4STCOptimizationDisable:bdw,chv */
913 WA_SET_BIT_MASKED(CACHE_MODE_1, GEN8_4x4_STC_OPTIMIZATION_DISABLE);
914
915 /*
916 * BSpec recommends 8x4 when MSAA is used,
917 * however in practice 16x4 seems fastest.
918 *
919 * Note that PS/WM thread counts depend on the WIZ hashing
920 * disable bit, which we don't touch here, but it's good
921 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
922 */
923 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
924 GEN6_WIZ_HASHING_MASK,
925 GEN6_WIZ_HASHING_16x4);
926
927 return 0;
928}
929
930static int bdw_init_workarounds(struct intel_engine_cs *engine)
931{
932 struct drm_i915_private *dev_priv = engine->i915;
933 int ret;
934
935 ret = gen8_init_workarounds(engine);
936 if (ret)
937 return ret;
938
939 /* WaDisableThreadStallDopClockGating:bdw (pre-production) */
940 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
941
942 /* WaDisableDopClockGating:bdw
943 *
944 * Also see the related UCGTCL1 write in broadwell_init_clock_gating()
945 * to disable EUTC clock gating.
946 */
947 WA_SET_BIT_MASKED(GEN7_ROW_CHICKEN2,
948 DOP_CLOCK_GATING_DISABLE);
949
950 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
951 GEN8_SAMPLER_POWER_BYPASS_DIS);
952
953 WA_SET_BIT_MASKED(HDC_CHICKEN0,
954 /* WaForceContextSaveRestoreNonCoherent:bdw */
955 HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
956 /* WaDisableFenceDestinationToSLM:bdw (pre-prod) */
957 (IS_BDW_GT3(dev_priv) ? HDC_FENCE_DEST_SLM_DISABLE : 0));
958
959 return 0;
960}
961
962static int chv_init_workarounds(struct intel_engine_cs *engine)
963{
964 struct drm_i915_private *dev_priv = engine->i915;
965 int ret;
966
967 ret = gen8_init_workarounds(engine);
968 if (ret)
969 return ret;
970
971 /* WaDisableThreadStallDopClockGating:chv */
972 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
973
974 /* Improve HiZ throughput on CHV. */
975 WA_SET_BIT_MASKED(HIZ_CHICKEN, CHV_HZ_8X8_MODE_IN_1X);
976
977 return 0;
978}
979
980static int gen9_init_workarounds(struct intel_engine_cs *engine)
981{
982 struct drm_i915_private *dev_priv = engine->i915;
983 int ret;
984
Rodrigo Vivi46c26662017-06-16 15:49:58 -0700985 /* WaConextSwitchWithConcurrentTLBInvalidate:skl,bxt,kbl,glk,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +0000986 I915_WRITE(GEN9_CSFE_CHICKEN1_RCS, _MASKED_BIT_ENABLE(GEN9_PREEMPT_GPGPU_SYNC_SWITCH_DISABLE));
987
Rodrigo Vivi46c26662017-06-16 15:49:58 -0700988 /* WaEnableLbsSlaRetryTimerDecrement:skl,bxt,kbl,glk,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +0000989 I915_WRITE(BDW_SCRATCH1, I915_READ(BDW_SCRATCH1) |
990 GEN9_LBS_SLA_RETRY_TIMER_DECREMENT_ENABLE);
991
Rodrigo Vivi98eed3d2017-06-19 14:21:47 -0700992 /* WaDisableKillLogic:bxt,skl,kbl */
993 if (!IS_COFFEELAKE(dev_priv))
994 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
995 ECOCHK_DIS_TLB);
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +0000996
Ville Syrjälä93564042017-08-24 22:10:51 +0300997 if (HAS_LLC(dev_priv)) {
998 /* WaCompressedResourceSamplerPbeMediaNewHashMode:skl,kbl
999 *
1000 * Must match Display Engine. See
1001 * WaCompressedResourceDisplayNewHashMode.
1002 */
1003 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1004 GEN9_PBE_COMPRESSED_HASH_SELECTION);
1005 WA_SET_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN7,
1006 GEN9_SAMPLER_HASH_COMPRESSED_READ_ADDR);
Chris Wilson53221e12017-10-04 13:41:52 +01001007
1008 I915_WRITE(MMCD_MISC_CTRL,
1009 I915_READ(MMCD_MISC_CTRL) |
1010 MMCD_PCLA |
1011 MMCD_HOTSPOT_EN);
Ville Syrjälä93564042017-08-24 22:10:51 +03001012 }
1013
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001014 /* WaClearFlowControlGpgpuContextSave:skl,bxt,kbl,glk,cfl */
1015 /* WaDisablePartialInstShootdown:skl,bxt,kbl,glk,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001016 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
1017 FLOW_CONTROL_ENABLE |
1018 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
1019
1020 /* Syncing dependencies between camera and graphics:skl,bxt,kbl */
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001021 if (!IS_COFFEELAKE(dev_priv))
1022 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
1023 GEN9_DISABLE_OCL_OOB_SUPPRESS_LOGIC);
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001024
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001025 /* WaEnableYV12BugFixInHalfSliceChicken7:skl,bxt,kbl,glk,cfl */
1026 /* WaEnableSamplerGPGPUPreemptionSupport:skl,bxt,kbl,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001027 WA_SET_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN7,
Arkadiusz Hiler0b71cea2017-05-12 13:20:15 +02001028 GEN9_ENABLE_YV12_BUGFIX |
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001029 GEN9_ENABLE_GPGPU_PREEMPTION);
1030
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001031 /* Wa4x4STCOptimizationDisable:skl,bxt,kbl,glk,cfl */
1032 /* WaDisablePartialResolveInVc:skl,bxt,kbl,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001033 WA_SET_BIT_MASKED(CACHE_MODE_1, (GEN8_4x4_STC_OPTIMIZATION_DISABLE |
1034 GEN9_PARTIAL_RESOLVE_IN_VC_DISABLE));
1035
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001036 /* WaCcsTlbPrefetchDisable:skl,bxt,kbl,glk,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001037 WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
1038 GEN9_CCS_TLB_PREFETCH_ENABLE);
1039
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001040 /* WaForceContextSaveRestoreNonCoherent:skl,bxt,kbl,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001041 WA_SET_BIT_MASKED(HDC_CHICKEN0,
1042 HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
1043 HDC_FORCE_CSR_NON_COHERENT_OVR_DISABLE);
1044
1045 /* WaForceEnableNonCoherent and WaDisableHDCInvalidation are
1046 * both tied to WaForceContextSaveRestoreNonCoherent
1047 * in some hsds for skl. We keep the tie for all gen9. The
1048 * documentation is a bit hazy and so we want to get common behaviour,
1049 * even though there is no clear evidence we would need both on kbl/bxt.
1050 * This area has been source of system hangs so we play it safe
1051 * and mimic the skl regardless of what bspec says.
1052 *
1053 * Use Force Non-Coherent whenever executing a 3D context. This
1054 * is a workaround for a possible hang in the unlikely event
1055 * a TLB invalidation occurs during a PSD flush.
1056 */
1057
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001058 /* WaForceEnableNonCoherent:skl,bxt,kbl,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001059 WA_SET_BIT_MASKED(HDC_CHICKEN0,
1060 HDC_FORCE_NON_COHERENT);
1061
Rodrigo Vivi98eed3d2017-06-19 14:21:47 -07001062 /* WaDisableHDCInvalidation:skl,bxt,kbl,cfl */
1063 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
1064 BDW_DISABLE_HDC_INVALIDATION);
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001065
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001066 /* WaDisableSamplerPowerBypassForSOPingPong:skl,bxt,kbl,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001067 if (IS_SKYLAKE(dev_priv) ||
1068 IS_KABYLAKE(dev_priv) ||
Chris Wilsonf3e2b2c2017-11-14 13:43:39 +00001069 IS_COFFEELAKE(dev_priv))
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001070 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
1071 GEN8_SAMPLER_POWER_BYPASS_DIS);
1072
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001073 /* WaDisableSTUnitPowerOptimization:skl,bxt,kbl,glk,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001074 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN2, GEN8_ST_PO_DISABLE);
1075
Valtteri Rantala74368302017-11-28 16:45:05 +02001076 /* WaProgramL3SqcReg1DefaultForPerf:bxt,glk */
1077 if (IS_GEN9_LP(dev_priv)) {
1078 u32 val = I915_READ(GEN8_L3SQCREG1);
1079
1080 val &= ~L3_PRIO_CREDITS_MASK;
1081 val |= L3_GENERAL_PRIO_CREDITS(62) | L3_HIGH_PRIO_CREDITS(2);
1082 I915_WRITE(GEN8_L3SQCREG1, val);
1083 }
1084
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001085 /* WaOCLCoherentLineFlush:skl,bxt,kbl,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001086 I915_WRITE(GEN8_L3SQCREG4, (I915_READ(GEN8_L3SQCREG4) |
1087 GEN8_LQSC_FLUSH_COHERENT_LINES));
1088
Michał Winiarski5152def2017-10-03 21:34:46 +01001089 /*
1090 * Supporting preemption with fine-granularity requires changes in the
1091 * batch buffer programming. Since we can't break old userspace, we
1092 * need to set our default preemption level to safe value. Userspace is
1093 * still able to use more fine-grained preemption levels, since in
1094 * WaEnablePreemptionGranularityControlByUMD we're whitelisting the
1095 * per-ctx register. As such, WaDisable{3D,GPGPU}MidCmdPreemption are
1096 * not real HW workarounds, but merely a way to start using preemption
1097 * while maintaining old contract with userspace.
1098 */
1099
1100 /* WaDisable3DMidCmdPreemption:skl,bxt,glk,cfl,[cnl] */
1101 WA_CLR_BIT_MASKED(GEN8_CS_CHICKEN1, GEN9_PREEMPT_3D_OBJECT_LEVEL);
1102
1103 /* WaDisableGPGPUMidCmdPreemption:skl,bxt,blk,cfl,[cnl] */
1104 WA_SET_FIELD_MASKED(GEN8_CS_CHICKEN1, GEN9_PREEMPT_GPGPU_LEVEL_MASK,
1105 GEN9_PREEMPT_GPGPU_COMMAND_LEVEL);
1106
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001107 /* WaVFEStateAfterPipeControlwithMediaStateClear:skl,bxt,glk,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001108 ret = wa_ring_whitelist_reg(engine, GEN9_CTX_PREEMPT_REG);
1109 if (ret)
1110 return ret;
1111
Jeff McGee1e998342017-10-03 21:34:45 +01001112 /* WaEnablePreemptionGranularityControlByUMD:skl,bxt,kbl,cfl,[cnl] */
1113 I915_WRITE(GEN7_FF_SLICE_CS_CHICKEN1,
1114 _MASKED_BIT_ENABLE(GEN9_FFSC_PERCTX_PREEMPT_CTRL));
1115 ret = wa_ring_whitelist_reg(engine, GEN8_CS_CHICKEN1);
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001116 if (ret)
1117 return ret;
1118
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001119 /* WaAllowUMDToModifyHDCChicken1:skl,bxt,kbl,glk,cfl */
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001120 ret = wa_ring_whitelist_reg(engine, GEN8_HDC_CHICKEN1);
1121 if (ret)
1122 return ret;
1123
1124 return 0;
1125}
1126
1127static int skl_tune_iz_hashing(struct intel_engine_cs *engine)
1128{
1129 struct drm_i915_private *dev_priv = engine->i915;
1130 u8 vals[3] = { 0, 0, 0 };
1131 unsigned int i;
1132
1133 for (i = 0; i < 3; i++) {
1134 u8 ss;
1135
1136 /*
1137 * Only consider slices where one, and only one, subslice has 7
1138 * EUs
1139 */
1140 if (!is_power_of_2(INTEL_INFO(dev_priv)->sseu.subslice_7eu[i]))
1141 continue;
1142
1143 /*
1144 * subslice_7eu[i] != 0 (because of the check above) and
1145 * ss_max == 4 (maximum number of subslices possible per slice)
1146 *
1147 * -> 0 <= ss <= 3;
1148 */
1149 ss = ffs(INTEL_INFO(dev_priv)->sseu.subslice_7eu[i]) - 1;
1150 vals[i] = 3 - ss;
1151 }
1152
1153 if (vals[0] == 0 && vals[1] == 0 && vals[2] == 0)
1154 return 0;
1155
1156 /* Tune IZ hashing. See intel_device_info_runtime_init() */
1157 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
1158 GEN9_IZ_HASHING_MASK(2) |
1159 GEN9_IZ_HASHING_MASK(1) |
1160 GEN9_IZ_HASHING_MASK(0),
1161 GEN9_IZ_HASHING(2, vals[2]) |
1162 GEN9_IZ_HASHING(1, vals[1]) |
1163 GEN9_IZ_HASHING(0, vals[0]));
1164
1165 return 0;
1166}
1167
1168static int skl_init_workarounds(struct intel_engine_cs *engine)
1169{
1170 struct drm_i915_private *dev_priv = engine->i915;
1171 int ret;
1172
1173 ret = gen9_init_workarounds(engine);
1174 if (ret)
1175 return ret;
1176
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001177 /* WaEnableGapsTsvCreditFix:skl */
1178 I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
1179 GEN9_GAPS_TSV_CREDIT_DISABLE));
1180
1181 /* WaDisableGafsUnitClkGating:skl */
Oscar Mateo4827c542017-09-07 08:40:07 -07001182 I915_WRITE(GEN7_UCGCTL4, (I915_READ(GEN7_UCGCTL4) |
1183 GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE));
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001184
1185 /* WaInPlaceDecompressionHang:skl */
1186 if (IS_SKL_REVID(dev_priv, SKL_REVID_H0, REVID_FOREVER))
Oscar Mateoefc886c2017-09-07 08:40:04 -07001187 I915_WRITE(GEN9_GAMT_ECO_REG_RW_IA,
1188 (I915_READ(GEN9_GAMT_ECO_REG_RW_IA) |
1189 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS));
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001190
1191 /* WaDisableLSQCROPERFforOCL:skl */
1192 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
1193 if (ret)
1194 return ret;
1195
1196 return skl_tune_iz_hashing(engine);
1197}
1198
1199static int bxt_init_workarounds(struct intel_engine_cs *engine)
1200{
1201 struct drm_i915_private *dev_priv = engine->i915;
1202 int ret;
1203
1204 ret = gen9_init_workarounds(engine);
1205 if (ret)
1206 return ret;
1207
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001208 /* WaDisableThreadStallDopClockGating:bxt */
1209 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
1210 STALL_DOP_GATING_DISABLE);
1211
1212 /* WaDisablePooledEuLoadBalancingFix:bxt */
Chris Wilson70a84f32017-11-14 13:43:40 +00001213 I915_WRITE(FF_SLICE_CS_CHICKEN2,
1214 _MASKED_BIT_ENABLE(GEN9_POOLED_EU_LOAD_BALANCING_FIX_DISABLE));
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001215
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001216 /* WaToEnableHwFixForPushConstHWBug:bxt */
Chris Wilson70a84f32017-11-14 13:43:40 +00001217 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1218 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001219
1220 /* WaInPlaceDecompressionHang:bxt */
Chris Wilson70a84f32017-11-14 13:43:40 +00001221 I915_WRITE(GEN9_GAMT_ECO_REG_RW_IA,
1222 (I915_READ(GEN9_GAMT_ECO_REG_RW_IA) |
1223 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS));
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001224
1225 return 0;
1226}
1227
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07001228static int cnl_init_workarounds(struct intel_engine_cs *engine)
1229{
1230 struct drm_i915_private *dev_priv = engine->i915;
1231 int ret;
1232
Oscar Mateo6cf20a02017-09-07 08:40:05 -07001233 /* WaDisableI2mCycleOnWRPort:cnl (pre-prod) */
Rodrigo Vivi86ebb012017-08-29 16:07:51 -07001234 if (IS_CNL_REVID(dev_priv, CNL_REVID_B0, CNL_REVID_B0))
Oscar Mateo6cf20a02017-09-07 08:40:05 -07001235 I915_WRITE(GAMT_CHKN_BIT_REG,
1236 (I915_READ(GAMT_CHKN_BIT_REG) |
1237 GAMT_CHKN_DISABLE_I2M_CYCLE_ON_WR_PORT));
Rodrigo Vivi86ebb012017-08-29 16:07:51 -07001238
Rodrigo Viviacfb5552017-08-23 13:35:04 -07001239 /* WaForceContextSaveRestoreNonCoherent:cnl */
1240 WA_SET_BIT_MASKED(CNL_HDC_CHICKEN0,
1241 HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT);
1242
Rodrigo Viviaa9f4c42017-09-06 15:03:25 -07001243 /* WaThrottleEUPerfToAvoidTDBackPressure:cnl(pre-prod) */
1244 if (IS_CNL_REVID(dev_priv, CNL_REVID_B0, CNL_REVID_B0))
1245 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, THROTTLE_12_5);
1246
Rodrigo Vivie6d1a4f2017-08-15 16:16:49 -07001247 /* WaDisableReplayBufferBankArbitrationOptimization:cnl */
1248 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1249 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
1250
Rodrigo Vivid1d24752017-08-15 16:16:50 -07001251 /* WaDisableEnhancedSBEVertexCaching:cnl (pre-prod) */
1252 if (IS_CNL_REVID(dev_priv, 0, CNL_REVID_B0))
1253 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1254 GEN8_CSC2_SBE_VUE_CACHE_CONSERVATIVE);
1255
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07001256 /* WaInPlaceDecompressionHang:cnl */
Oscar Mateoefc886c2017-09-07 08:40:04 -07001257 I915_WRITE(GEN9_GAMT_ECO_REG_RW_IA,
1258 (I915_READ(GEN9_GAMT_ECO_REG_RW_IA) |
1259 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS));
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07001260
Oscar Mateo2cbecff2017-08-23 12:56:31 -07001261 /* WaPushConstantDereferenceHoldDisable:cnl */
Oscar Mateob27f5902017-09-07 08:40:06 -07001262 WA_SET_BIT_MASKED(GEN7_ROW_CHICKEN2, PUSH_CONSTANT_DEREF_DISABLE);
Oscar Mateo2cbecff2017-08-23 12:56:31 -07001263
Rodrigo Vivi392572f2017-08-29 16:07:23 -07001264 /* FtrEnableFastAnisoL1BankingFix: cnl */
1265 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3, CNL_FAST_ANISO_L1_BANKING_FIX);
1266
Michał Winiarski5152def2017-10-03 21:34:46 +01001267 /* WaDisable3DMidCmdPreemption:cnl */
1268 WA_CLR_BIT_MASKED(GEN8_CS_CHICKEN1, GEN9_PREEMPT_3D_OBJECT_LEVEL);
1269
1270 /* WaDisableGPGPUMidCmdPreemption:cnl */
1271 WA_SET_FIELD_MASKED(GEN8_CS_CHICKEN1, GEN9_PREEMPT_GPGPU_LEVEL_MASK,
1272 GEN9_PREEMPT_GPGPU_COMMAND_LEVEL);
1273
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07001274 /* WaEnablePreemptionGranularityControlByUMD:cnl */
Jeff McGee1e998342017-10-03 21:34:45 +01001275 I915_WRITE(GEN7_FF_SLICE_CS_CHICKEN1,
1276 _MASKED_BIT_ENABLE(GEN9_FFSC_PERCTX_PREEMPT_CTRL));
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07001277 ret= wa_ring_whitelist_reg(engine, GEN8_CS_CHICKEN1);
1278 if (ret)
1279 return ret;
1280
Rafael Antognollia2b16582017-12-15 16:11:17 -08001281 /* WaDisableEarlyEOT:cnl */
1282 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, DISABLE_EARLY_EOT);
1283
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07001284 return 0;
1285}
1286
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001287static int kbl_init_workarounds(struct intel_engine_cs *engine)
1288{
1289 struct drm_i915_private *dev_priv = engine->i915;
1290 int ret;
1291
1292 ret = gen9_init_workarounds(engine);
1293 if (ret)
1294 return ret;
1295
1296 /* WaEnableGapsTsvCreditFix:kbl */
1297 I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
1298 GEN9_GAPS_TSV_CREDIT_DISABLE));
1299
1300 /* WaDisableDynamicCreditSharing:kbl */
1301 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
Oscar Mateoc6ea497c2017-09-07 08:40:08 -07001302 I915_WRITE(GAMT_CHKN_BIT_REG,
1303 (I915_READ(GAMT_CHKN_BIT_REG) |
1304 GAMT_CHKN_DISABLE_DYNAMIC_CREDIT_SHARING));
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001305
1306 /* WaDisableFenceDestinationToSLM:kbl (pre-prod) */
1307 if (IS_KBL_REVID(dev_priv, KBL_REVID_A0, KBL_REVID_A0))
1308 WA_SET_BIT_MASKED(HDC_CHICKEN0,
1309 HDC_FENCE_DEST_SLM_DISABLE);
1310
1311 /* WaToEnableHwFixForPushConstHWBug:kbl */
1312 if (IS_KBL_REVID(dev_priv, KBL_REVID_C0, REVID_FOREVER))
1313 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1314 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
1315
1316 /* WaDisableGafsUnitClkGating:kbl */
Oscar Mateo4827c542017-09-07 08:40:07 -07001317 I915_WRITE(GEN7_UCGCTL4, (I915_READ(GEN7_UCGCTL4) |
1318 GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE));
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001319
1320 /* WaDisableSbeCacheDispatchPortSharing:kbl */
1321 WA_SET_BIT_MASKED(
1322 GEN7_HALF_SLICE_CHICKEN1,
1323 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
1324
1325 /* WaInPlaceDecompressionHang:kbl */
Oscar Mateoefc886c2017-09-07 08:40:04 -07001326 I915_WRITE(GEN9_GAMT_ECO_REG_RW_IA,
1327 (I915_READ(GEN9_GAMT_ECO_REG_RW_IA) |
1328 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS));
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001329
1330 /* WaDisableLSQCROPERFforOCL:kbl */
1331 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
1332 if (ret)
1333 return ret;
1334
1335 return 0;
1336}
1337
1338static int glk_init_workarounds(struct intel_engine_cs *engine)
1339{
1340 struct drm_i915_private *dev_priv = engine->i915;
1341 int ret;
1342
1343 ret = gen9_init_workarounds(engine);
1344 if (ret)
1345 return ret;
1346
Kenneth Graunkeab062632018-01-05 00:59:05 -08001347 /* WA #0862: Userspace has to set "Barrier Mode" to avoid hangs. */
1348 ret = wa_ring_whitelist_reg(engine, GEN9_SLICE_COMMON_ECO_CHICKEN1);
1349 if (ret)
1350 return ret;
1351
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001352 /* WaToEnableHwFixForPushConstHWBug:glk */
1353 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1354 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
1355
1356 return 0;
1357}
1358
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001359static int cfl_init_workarounds(struct intel_engine_cs *engine)
1360{
1361 struct drm_i915_private *dev_priv = engine->i915;
1362 int ret;
1363
1364 ret = gen9_init_workarounds(engine);
1365 if (ret)
1366 return ret;
1367
1368 /* WaEnableGapsTsvCreditFix:cfl */
1369 I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
1370 GEN9_GAPS_TSV_CREDIT_DISABLE));
1371
1372 /* WaToEnableHwFixForPushConstHWBug:cfl */
1373 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1374 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
1375
1376 /* WaDisableGafsUnitClkGating:cfl */
Oscar Mateo4827c542017-09-07 08:40:07 -07001377 I915_WRITE(GEN7_UCGCTL4, (I915_READ(GEN7_UCGCTL4) |
1378 GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE));
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001379
1380 /* WaDisableSbeCacheDispatchPortSharing:cfl */
1381 WA_SET_BIT_MASKED(
1382 GEN7_HALF_SLICE_CHICKEN1,
1383 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
1384
1385 /* WaInPlaceDecompressionHang:cfl */
Oscar Mateoefc886c2017-09-07 08:40:04 -07001386 I915_WRITE(GEN9_GAMT_ECO_REG_RW_IA,
1387 (I915_READ(GEN9_GAMT_ECO_REG_RW_IA) |
1388 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS));
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001389
1390 return 0;
1391}
1392
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001393int init_workarounds_ring(struct intel_engine_cs *engine)
1394{
1395 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson02e012f2017-03-01 12:11:31 +00001396 int err;
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001397
Tvrtko Ursulinae504be2018-01-19 10:00:03 +00001398 if (GEM_WARN_ON(engine->id != RCS))
1399 return -EINVAL;
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001400
1401 dev_priv->workarounds.count = 0;
Chris Wilson02e012f2017-03-01 12:11:31 +00001402 dev_priv->workarounds.hw_whitelist_count[engine->id] = 0;
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001403
1404 if (IS_BROADWELL(dev_priv))
Chris Wilson02e012f2017-03-01 12:11:31 +00001405 err = bdw_init_workarounds(engine);
1406 else if (IS_CHERRYVIEW(dev_priv))
1407 err = chv_init_workarounds(engine);
1408 else if (IS_SKYLAKE(dev_priv))
1409 err = skl_init_workarounds(engine);
1410 else if (IS_BROXTON(dev_priv))
1411 err = bxt_init_workarounds(engine);
1412 else if (IS_KABYLAKE(dev_priv))
1413 err = kbl_init_workarounds(engine);
1414 else if (IS_GEMINILAKE(dev_priv))
1415 err = glk_init_workarounds(engine);
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001416 else if (IS_COFFEELAKE(dev_priv))
1417 err = cfl_init_workarounds(engine);
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07001418 else if (IS_CANNONLAKE(dev_priv))
1419 err = cnl_init_workarounds(engine);
Chris Wilson02e012f2017-03-01 12:11:31 +00001420 else
1421 err = 0;
1422 if (err)
1423 return err;
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001424
Chris Wilson02e012f2017-03-01 12:11:31 +00001425 DRM_DEBUG_DRIVER("%s: Number of context specific w/a: %d\n",
1426 engine->name, dev_priv->workarounds.count);
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001427 return 0;
1428}
1429
Chris Wilsone61e0f52018-02-21 09:56:36 +00001430int intel_ring_workarounds_emit(struct i915_request *rq)
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001431{
Chris Wilsone61e0f52018-02-21 09:56:36 +00001432 struct i915_workarounds *w = &rq->i915->workarounds;
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001433 u32 *cs;
1434 int ret, i;
1435
1436 if (w->count == 0)
1437 return 0;
1438
Chris Wilsone61e0f52018-02-21 09:56:36 +00001439 ret = rq->engine->emit_flush(rq, EMIT_BARRIER);
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001440 if (ret)
1441 return ret;
1442
Chris Wilsone61e0f52018-02-21 09:56:36 +00001443 cs = intel_ring_begin(rq, w->count * 2 + 2);
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001444 if (IS_ERR(cs))
1445 return PTR_ERR(cs);
1446
1447 *cs++ = MI_LOAD_REGISTER_IMM(w->count);
1448 for (i = 0; i < w->count; i++) {
1449 *cs++ = i915_mmio_reg_offset(w->reg[i].addr);
1450 *cs++ = w->reg[i].value;
1451 }
1452 *cs++ = MI_NOOP;
1453
Chris Wilsone61e0f52018-02-21 09:56:36 +00001454 intel_ring_advance(rq, cs);
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001455
Chris Wilsone61e0f52018-02-21 09:56:36 +00001456 ret = rq->engine->emit_flush(rq, EMIT_BARRIER);
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001457 if (ret)
1458 return ret;
1459
Tvrtko Ursulin133b4bd2017-02-16 12:23:23 +00001460 return 0;
1461}
1462
Chris Wilsona091d4e2017-05-30 13:13:33 +01001463static bool ring_is_idle(struct intel_engine_cs *engine)
1464{
1465 struct drm_i915_private *dev_priv = engine->i915;
1466 bool idle = true;
1467
Chris Wilson74d00d22018-02-12 09:39:28 +00001468 /* If the whole device is asleep, the engine must be idle */
1469 if (!intel_runtime_pm_get_if_in_use(dev_priv))
1470 return true;
Chris Wilsona091d4e2017-05-30 13:13:33 +01001471
Chris Wilsonaed2fc12017-05-30 13:13:34 +01001472 /* First check that no commands are left in the ring */
1473 if ((I915_READ_HEAD(engine) & HEAD_ADDR) !=
1474 (I915_READ_TAIL(engine) & TAIL_ADDR))
1475 idle = false;
1476
Chris Wilsona091d4e2017-05-30 13:13:33 +01001477 /* No bit for gen2, so assume the CS parser is idle */
1478 if (INTEL_GEN(dev_priv) > 2 && !(I915_READ_MODE(engine) & MODE_IDLE))
1479 idle = false;
1480
1481 intel_runtime_pm_put(dev_priv);
1482
1483 return idle;
1484}
1485
Chris Wilson54003672017-03-03 12:19:46 +00001486/**
1487 * intel_engine_is_idle() - Report if the engine has finished process all work
1488 * @engine: the intel_engine_cs
1489 *
1490 * Return true if there are no requests pending, nothing left to be submitted
1491 * to hardware, and that the engine is idle.
1492 */
1493bool intel_engine_is_idle(struct intel_engine_cs *engine)
1494{
1495 struct drm_i915_private *dev_priv = engine->i915;
1496
Chris Wilsona8e9a412017-04-11 20:00:42 +01001497 /* More white lies, if wedged, hw state is inconsistent */
1498 if (i915_terminally_wedged(&dev_priv->gpu_error))
1499 return true;
1500
Chris Wilson54003672017-03-03 12:19:46 +00001501 /* Any inflight/incomplete requests? */
1502 if (!i915_seqno_passed(intel_engine_get_seqno(engine),
1503 intel_engine_last_submit(engine)))
1504 return false;
1505
Chris Wilson8968a362017-04-12 00:44:26 +01001506 if (I915_SELFTEST_ONLY(engine->breadcrumbs.mock))
1507 return true;
1508
Chris Wilson4a118ec2017-10-23 22:32:36 +01001509 /* Waiting to drain ELSP? */
1510 if (READ_ONCE(engine->execlists.active))
Chris Wilson54003672017-03-03 12:19:46 +00001511 return false;
1512
Chris Wilsond6edb6e2017-07-21 13:32:24 +01001513 /* ELSP is empty, but there are ready requests? */
Mika Kuoppalab620e872017-09-22 15:43:03 +03001514 if (READ_ONCE(engine->execlists.first))
Chris Wilsond6edb6e2017-07-21 13:32:24 +01001515 return false;
1516
Chris Wilson54003672017-03-03 12:19:46 +00001517 /* Ring stopped? */
Chris Wilsona091d4e2017-05-30 13:13:33 +01001518 if (!ring_is_idle(engine))
Chris Wilson54003672017-03-03 12:19:46 +00001519 return false;
1520
1521 return true;
1522}
1523
Chris Wilson05425242017-03-03 12:19:47 +00001524bool intel_engines_are_idle(struct drm_i915_private *dev_priv)
1525{
1526 struct intel_engine_cs *engine;
1527 enum intel_engine_id id;
1528
Chris Wilsond7dc4132017-12-12 13:21:48 +00001529 /*
1530 * If the driver is wedged, HW state may be very inconsistent and
Chris Wilson8490ae202017-03-30 15:50:37 +01001531 * report that it is still busy, even though we have stopped using it.
1532 */
1533 if (i915_terminally_wedged(&dev_priv->gpu_error))
1534 return true;
1535
Chris Wilson05425242017-03-03 12:19:47 +00001536 for_each_engine(engine, dev_priv, id) {
1537 if (!intel_engine_is_idle(engine))
1538 return false;
1539 }
1540
1541 return true;
1542}
1543
Chris Wilsonae6c4572017-11-10 14:26:28 +00001544/**
1545 * intel_engine_has_kernel_context:
1546 * @engine: the engine
1547 *
1548 * Returns true if the last context to be executed on this engine, or has been
1549 * executed if the engine is already idle, is the kernel context
1550 * (#i915.kernel_context).
1551 */
Chris Wilson20ccd4d2017-10-24 23:08:55 +01001552bool intel_engine_has_kernel_context(const struct intel_engine_cs *engine)
1553{
Chris Wilsonae6c4572017-11-10 14:26:28 +00001554 const struct i915_gem_context * const kernel_context =
1555 engine->i915->kernel_context;
Chris Wilsone61e0f52018-02-21 09:56:36 +00001556 struct i915_request *rq;
Chris Wilsonae6c4572017-11-10 14:26:28 +00001557
1558 lockdep_assert_held(&engine->i915->drm.struct_mutex);
1559
1560 /*
1561 * Check the last context seen by the engine. If active, it will be
1562 * the last request that remains in the timeline. When idle, it is
1563 * the last executed context as tracked by retirement.
1564 */
1565 rq = __i915_gem_active_peek(&engine->timeline->last_request);
1566 if (rq)
1567 return rq->ctx == kernel_context;
1568 else
1569 return engine->last_retired_context == kernel_context;
Chris Wilson20ccd4d2017-10-24 23:08:55 +01001570}
1571
Chris Wilsonff44ad52017-03-16 17:13:03 +00001572void intel_engines_reset_default_submission(struct drm_i915_private *i915)
1573{
1574 struct intel_engine_cs *engine;
1575 enum intel_engine_id id;
1576
1577 for_each_engine(engine, i915, id)
1578 engine->set_default_submission(engine);
1579}
1580
Chris Wilsonaba5e272017-10-25 15:39:41 +01001581/**
1582 * intel_engines_park: called when the GT is transitioning from busy->idle
1583 * @i915: the i915 device
1584 *
1585 * The GT is now idle and about to go to sleep (maybe never to wake again?).
1586 * Time for us to tidy and put away our toys (release resources back to the
1587 * system).
1588 */
1589void intel_engines_park(struct drm_i915_private *i915)
Chris Wilson6c067572017-05-17 13:10:03 +01001590{
1591 struct intel_engine_cs *engine;
1592 enum intel_engine_id id;
1593
1594 for_each_engine(engine, i915, id) {
Chris Wilson820c5bb2017-11-01 20:21:49 +00001595 /* Flush the residual irq tasklets first. */
1596 intel_engine_disarm_breadcrumbs(engine);
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05301597 tasklet_kill(&engine->execlists.tasklet);
Chris Wilson820c5bb2017-11-01 20:21:49 +00001598
Chris Wilson32651242017-10-27 12:06:17 +01001599 /*
1600 * We are committed now to parking the engines, make sure there
1601 * will be no more interrupts arriving later and the engines
1602 * are truly idle.
1603 */
Chris Wilson30b29402017-11-10 11:25:50 +00001604 if (wait_for(intel_engine_is_idle(engine), 10)) {
Chris Wilson32651242017-10-27 12:06:17 +01001605 struct drm_printer p = drm_debug_printer(__func__);
1606
Chris Wilson30b29402017-11-10 11:25:50 +00001607 dev_err(i915->drm.dev,
1608 "%s is not idle before parking\n",
1609 engine->name);
Chris Wilson0db18b12017-12-08 01:23:00 +00001610 intel_engine_dump(engine, &p, NULL);
Chris Wilson32651242017-10-27 12:06:17 +01001611 }
1612
Chris Wilsonaba5e272017-10-25 15:39:41 +01001613 if (engine->park)
1614 engine->park(engine);
1615
Chris Wilsonaba5e272017-10-25 15:39:41 +01001616 i915_gem_batch_pool_fini(&engine->batch_pool);
Mika Kuoppalab620e872017-09-22 15:43:03 +03001617 engine->execlists.no_priolist = false;
Chris Wilson6c067572017-05-17 13:10:03 +01001618 }
1619}
1620
Chris Wilsonaba5e272017-10-25 15:39:41 +01001621/**
1622 * intel_engines_unpark: called when the GT is transitioning from idle->busy
1623 * @i915: the i915 device
1624 *
1625 * The GT was idle and now about to fire up with some new user requests.
1626 */
1627void intel_engines_unpark(struct drm_i915_private *i915)
1628{
1629 struct intel_engine_cs *engine;
1630 enum intel_engine_id id;
1631
1632 for_each_engine(engine, i915, id) {
1633 if (engine->unpark)
1634 engine->unpark(engine);
1635 }
1636}
1637
Chris Wilson90cad092017-09-06 16:28:59 +01001638bool intel_engine_can_store_dword(struct intel_engine_cs *engine)
1639{
1640 switch (INTEL_GEN(engine->i915)) {
1641 case 2:
1642 return false; /* uses physical not virtual addresses */
1643 case 3:
1644 /* maybe only uses physical not virtual addresses */
1645 return !(IS_I915G(engine->i915) || IS_I915GM(engine->i915));
1646 case 6:
1647 return engine->class != VIDEO_DECODE_CLASS; /* b0rked */
1648 default:
1649 return true;
1650 }
1651}
1652
Chris Wilsond2b4b972017-11-10 14:26:33 +00001653unsigned int intel_engines_has_context_isolation(struct drm_i915_private *i915)
1654{
1655 struct intel_engine_cs *engine;
1656 enum intel_engine_id id;
1657 unsigned int which;
1658
1659 which = 0;
1660 for_each_engine(engine, i915, id)
1661 if (engine->default_state)
1662 which |= BIT(engine->uabi_class);
1663
1664 return which;
1665}
1666
Chris Wilsonf636edb2017-10-09 12:02:57 +01001667static void print_request(struct drm_printer *m,
Chris Wilsone61e0f52018-02-21 09:56:36 +00001668 struct i915_request *rq,
Chris Wilsonf636edb2017-10-09 12:02:57 +01001669 const char *prefix)
1670{
Chris Wilsona27d5a42017-10-15 21:43:10 +01001671 drm_printf(m, "%s%x%s [%x:%x] prio=%d @ %dms: %s\n", prefix,
1672 rq->global_seqno,
Chris Wilsone61e0f52018-02-21 09:56:36 +00001673 i915_request_completed(rq) ? "!" : "",
Chris Wilsona27d5a42017-10-15 21:43:10 +01001674 rq->ctx->hw_id, rq->fence.seqno,
Chris Wilsonf636edb2017-10-09 12:02:57 +01001675 rq->priotree.priority,
1676 jiffies_to_msecs(jiffies - rq->emitted_jiffies),
1677 rq->timeline->common->name);
1678}
1679
Chris Wilsonc1bf2722017-12-22 18:25:21 +00001680static void hexdump(struct drm_printer *m, const void *buf, size_t len)
1681{
1682 const size_t rowsize = 8 * sizeof(u32);
1683 const void *prev = NULL;
1684 bool skip = false;
1685 size_t pos;
1686
1687 for (pos = 0; pos < len; pos += rowsize) {
1688 char line[128];
1689
1690 if (prev && !memcmp(prev, buf + pos, rowsize)) {
1691 if (!skip) {
1692 drm_printf(m, "*\n");
1693 skip = true;
1694 }
1695 continue;
1696 }
1697
1698 WARN_ON_ONCE(hex_dump_to_buffer(buf + pos, len - pos,
1699 rowsize, sizeof(u32),
1700 line, sizeof(line),
1701 false) >= sizeof(line));
1702 drm_printf(m, "%08zx %s\n", pos, line);
1703
1704 prev = buf + pos;
1705 skip = false;
1706 }
1707}
1708
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001709static void intel_engine_print_registers(const struct intel_engine_cs *engine,
1710 struct drm_printer *m)
Chris Wilsonf636edb2017-10-09 12:02:57 +01001711{
Chris Wilsonf636edb2017-10-09 12:02:57 +01001712 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001713 const struct intel_engine_execlists * const execlists =
1714 &engine->execlists;
Chris Wilsonf636edb2017-10-09 12:02:57 +01001715 u64 addr;
1716
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001717 drm_printf(m, "\tRING_START: 0x%08x\n",
1718 I915_READ(RING_START(engine->mmio_base)));
1719 drm_printf(m, "\tRING_HEAD: 0x%08x\n",
1720 I915_READ(RING_HEAD(engine->mmio_base)) & HEAD_ADDR);
1721 drm_printf(m, "\tRING_TAIL: 0x%08x\n",
1722 I915_READ(RING_TAIL(engine->mmio_base)) & TAIL_ADDR);
Chris Wilson3c75de52017-10-26 12:50:48 +01001723 drm_printf(m, "\tRING_CTL: 0x%08x%s\n",
Chris Wilsonf636edb2017-10-09 12:02:57 +01001724 I915_READ(RING_CTL(engine->mmio_base)),
Chris Wilson3c75de52017-10-26 12:50:48 +01001725 I915_READ(RING_CTL(engine->mmio_base)) & (RING_WAIT | RING_WAIT_SEMAPHORE) ? " [waiting]" : "");
1726 if (INTEL_GEN(engine->i915) > 2) {
1727 drm_printf(m, "\tRING_MODE: 0x%08x%s\n",
1728 I915_READ(RING_MI_MODE(engine->mmio_base)),
1729 I915_READ(RING_MI_MODE(engine->mmio_base)) & (MODE_IDLE) ? " [idle]" : "");
1730 }
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001731
1732 if (INTEL_GEN(dev_priv) >= 6) {
1733 drm_printf(m, "\tRING_IMR: %08x\n", I915_READ_IMR(engine));
1734 }
1735
Chris Wilson93c6e962017-11-20 20:55:04 +00001736 if (HAS_LEGACY_SEMAPHORES(dev_priv)) {
Chris Wilsonaf9ff6c2017-11-20 20:55:03 +00001737 drm_printf(m, "\tSYNC_0: 0x%08x\n",
1738 I915_READ(RING_SYNC_0(engine->mmio_base)));
1739 drm_printf(m, "\tSYNC_1: 0x%08x\n",
1740 I915_READ(RING_SYNC_1(engine->mmio_base)));
1741 if (HAS_VEBOX(dev_priv))
1742 drm_printf(m, "\tSYNC_2: 0x%08x\n",
1743 I915_READ(RING_SYNC_2(engine->mmio_base)));
1744 }
Chris Wilsonf636edb2017-10-09 12:02:57 +01001745
Chris Wilsonf636edb2017-10-09 12:02:57 +01001746 addr = intel_engine_get_active_head(engine);
1747 drm_printf(m, "\tACTHD: 0x%08x_%08x\n",
1748 upper_32_bits(addr), lower_32_bits(addr));
1749 addr = intel_engine_get_last_batch_head(engine);
1750 drm_printf(m, "\tBBADDR: 0x%08x_%08x\n",
1751 upper_32_bits(addr), lower_32_bits(addr));
Chris Wilsona0cf5792017-12-18 12:39:14 +00001752 if (INTEL_GEN(dev_priv) >= 8)
1753 addr = I915_READ64_2x32(RING_DMA_FADD(engine->mmio_base),
1754 RING_DMA_FADD_UDW(engine->mmio_base));
1755 else if (INTEL_GEN(dev_priv) >= 4)
1756 addr = I915_READ(RING_DMA_FADD(engine->mmio_base));
1757 else
1758 addr = I915_READ(DMA_FADD_I8XX);
1759 drm_printf(m, "\tDMA_FADDR: 0x%08x_%08x\n",
1760 upper_32_bits(addr), lower_32_bits(addr));
1761 if (INTEL_GEN(dev_priv) >= 4) {
1762 drm_printf(m, "\tIPEIR: 0x%08x\n",
1763 I915_READ(RING_IPEIR(engine->mmio_base)));
1764 drm_printf(m, "\tIPEHR: 0x%08x\n",
1765 I915_READ(RING_IPEHR(engine->mmio_base)));
1766 } else {
1767 drm_printf(m, "\tIPEIR: 0x%08x\n", I915_READ(IPEIR));
1768 drm_printf(m, "\tIPEHR: 0x%08x\n", I915_READ(IPEHR));
1769 }
Chris Wilsonf636edb2017-10-09 12:02:57 +01001770
Chris Wilsonfb5c5512017-11-20 20:55:00 +00001771 if (HAS_EXECLISTS(dev_priv)) {
Chris Wilsonf636edb2017-10-09 12:02:57 +01001772 const u32 *hws = &engine->status_page.page_addr[I915_HWS_CSB_BUF0_INDEX];
Chris Wilsonf636edb2017-10-09 12:02:57 +01001773 u32 ptr, read, write;
1774 unsigned int idx;
1775
1776 drm_printf(m, "\tExeclist status: 0x%08x %08x\n",
1777 I915_READ(RING_EXECLIST_STATUS_LO(engine)),
1778 I915_READ(RING_EXECLIST_STATUS_HI(engine)));
1779
1780 ptr = I915_READ(RING_CONTEXT_STATUS_PTR(engine));
1781 read = GEN8_CSB_READ_PTR(ptr);
1782 write = GEN8_CSB_WRITE_PTR(ptr);
1783 drm_printf(m, "\tExeclist CSB read %d [%d cached], write %d [%d from hws], interrupt posted? %s\n",
1784 read, execlists->csb_head,
1785 write,
1786 intel_read_status_page(engine, intel_hws_csb_write_index(engine->i915)),
1787 yesno(test_bit(ENGINE_IRQ_EXECLIST,
1788 &engine->irq_posted)));
1789 if (read >= GEN8_CSB_ENTRIES)
1790 read = 0;
1791 if (write >= GEN8_CSB_ENTRIES)
1792 write = 0;
1793 if (read > write)
1794 write += GEN8_CSB_ENTRIES;
1795 while (read < write) {
1796 idx = ++read % GEN8_CSB_ENTRIES;
1797 drm_printf(m, "\tExeclist CSB[%d]: 0x%08x [0x%08x in hwsp], context: %d [%d in hwsp]\n",
1798 idx,
1799 I915_READ(RING_CONTEXT_STATUS_BUF_LO(engine, idx)),
1800 hws[idx * 2],
1801 I915_READ(RING_CONTEXT_STATUS_BUF_HI(engine, idx)),
1802 hws[idx * 2 + 1]);
1803 }
1804
1805 rcu_read_lock();
1806 for (idx = 0; idx < execlists_num_ports(execlists); idx++) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00001807 struct i915_request *rq;
Chris Wilsonf636edb2017-10-09 12:02:57 +01001808 unsigned int count;
1809
1810 rq = port_unpack(&execlists->port[idx], &count);
1811 if (rq) {
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001812 char hdr[80];
1813
Chris Wilsone8a70ca2017-12-08 01:22:59 +00001814 snprintf(hdr, sizeof(hdr),
1815 "\t\tELSP[%d] count=%d, rq: ",
1816 idx, count);
1817 print_request(m, rq, hdr);
Chris Wilsonf636edb2017-10-09 12:02:57 +01001818 } else {
Chris Wilsone8a70ca2017-12-08 01:22:59 +00001819 drm_printf(m, "\t\tELSP[%d] idle\n", idx);
Chris Wilsonf636edb2017-10-09 12:02:57 +01001820 }
1821 }
Chris Wilson4a118ec2017-10-23 22:32:36 +01001822 drm_printf(m, "\t\tHW active? 0x%x\n", execlists->active);
Chris Wilsonf636edb2017-10-09 12:02:57 +01001823 rcu_read_unlock();
Chris Wilsonf636edb2017-10-09 12:02:57 +01001824 } else if (INTEL_GEN(dev_priv) > 6) {
1825 drm_printf(m, "\tPP_DIR_BASE: 0x%08x\n",
1826 I915_READ(RING_PP_DIR_BASE(engine)));
1827 drm_printf(m, "\tPP_DIR_BASE_READ: 0x%08x\n",
1828 I915_READ(RING_PP_DIR_BASE_READ(engine)));
1829 drm_printf(m, "\tPP_DIR_DCLV: 0x%08x\n",
1830 I915_READ(RING_PP_DIR_DCLV(engine)));
1831 }
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001832}
1833
1834void intel_engine_dump(struct intel_engine_cs *engine,
1835 struct drm_printer *m,
1836 const char *header, ...)
1837{
1838 struct intel_breadcrumbs * const b = &engine->breadcrumbs;
1839 const struct intel_engine_execlists * const execlists = &engine->execlists;
1840 struct i915_gpu_error * const error = &engine->i915->gpu_error;
Chris Wilsone61e0f52018-02-21 09:56:36 +00001841 struct i915_request *rq;
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001842 struct rb_node *rb;
1843
1844 if (header) {
1845 va_list ap;
1846
1847 va_start(ap, header);
1848 drm_vprintf(m, header, &ap);
1849 va_end(ap);
1850 }
1851
1852 if (i915_terminally_wedged(&engine->i915->gpu_error))
1853 drm_printf(m, "*** WEDGED ***\n");
1854
1855 drm_printf(m, "\tcurrent seqno %x, last %x, hangcheck %x [%d ms], inflight %d\n",
1856 intel_engine_get_seqno(engine),
1857 intel_engine_last_submit(engine),
1858 engine->hangcheck.seqno,
1859 jiffies_to_msecs(jiffies - engine->hangcheck.action_timestamp),
1860 engine->timeline->inflight_seqnos);
1861 drm_printf(m, "\tReset count: %d (global %d)\n",
1862 i915_reset_engine_count(error, engine),
1863 i915_reset_count(error));
1864
1865 rcu_read_lock();
1866
1867 drm_printf(m, "\tRequests:\n");
1868
1869 rq = list_first_entry(&engine->timeline->requests,
Chris Wilsone61e0f52018-02-21 09:56:36 +00001870 struct i915_request, link);
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001871 if (&rq->link != &engine->timeline->requests)
1872 print_request(m, rq, "\t\tfirst ");
1873
1874 rq = list_last_entry(&engine->timeline->requests,
Chris Wilsone61e0f52018-02-21 09:56:36 +00001875 struct i915_request, link);
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001876 if (&rq->link != &engine->timeline->requests)
1877 print_request(m, rq, "\t\tlast ");
1878
1879 rq = i915_gem_find_active_request(engine);
1880 if (rq) {
1881 print_request(m, rq, "\t\tactive ");
1882 drm_printf(m,
1883 "\t\t[head %04x, postfix %04x, tail %04x, batch 0x%08x_%08x]\n",
1884 rq->head, rq->postfix, rq->tail,
1885 rq->batch ? upper_32_bits(rq->batch->node.start) : ~0u,
1886 rq->batch ? lower_32_bits(rq->batch->node.start) : ~0u);
1887 drm_printf(m, "\t\tring->start: 0x%08x\n",
1888 i915_ggtt_offset(rq->ring->vma));
1889 drm_printf(m, "\t\tring->head: 0x%08x\n",
1890 rq->ring->head);
1891 drm_printf(m, "\t\tring->tail: 0x%08x\n",
1892 rq->ring->tail);
1893 }
1894
1895 rcu_read_unlock();
1896
1897 if (intel_runtime_pm_get_if_in_use(engine->i915)) {
1898 intel_engine_print_registers(engine, m);
1899 intel_runtime_pm_put(engine->i915);
1900 } else {
1901 drm_printf(m, "\tDevice is asleep; skipping register dump\n");
1902 }
Chris Wilsonf636edb2017-10-09 12:02:57 +01001903
Chris Wilsona27d5a42017-10-15 21:43:10 +01001904 spin_lock_irq(&engine->timeline->lock);
1905 list_for_each_entry(rq, &engine->timeline->requests, link)
1906 print_request(m, rq, "\t\tE ");
Chris Wilsonf6322ed2018-02-22 14:22:29 +00001907 drm_printf(m, "\t\tQueue priority: %d\n", execlists->queue_priority);
Chris Wilsona27d5a42017-10-15 21:43:10 +01001908 for (rb = execlists->first; rb; rb = rb_next(rb)) {
1909 struct i915_priolist *p =
1910 rb_entry(rb, typeof(*p), node);
1911
1912 list_for_each_entry(rq, &p->requests, priotree.link)
1913 print_request(m, rq, "\t\tQ ");
1914 }
1915 spin_unlock_irq(&engine->timeline->lock);
1916
Chris Wilsonf636edb2017-10-09 12:02:57 +01001917 spin_lock_irq(&b->rb_lock);
1918 for (rb = rb_first(&b->waiters); rb; rb = rb_next(rb)) {
1919 struct intel_wait *w = rb_entry(rb, typeof(*w), node);
1920
1921 drm_printf(m, "\t%s [%d] waiting for %x\n",
1922 w->tsk->comm, w->tsk->pid, w->seqno);
1923 }
1924 spin_unlock_irq(&b->rb_lock);
1925
Chris Wilson832265d2017-12-08 01:23:01 +00001926 drm_printf(m, "IRQ? 0x%lx (breadcrumbs? %s) (execlists? %s)\n",
1927 engine->irq_posted,
1928 yesno(test_bit(ENGINE_IRQ_BREADCRUMB,
1929 &engine->irq_posted)),
1930 yesno(test_bit(ENGINE_IRQ_EXECLIST,
1931 &engine->irq_posted)));
Chris Wilsonc1bf2722017-12-22 18:25:21 +00001932
1933 drm_printf(m, "HWSP:\n");
1934 hexdump(m, engine->status_page.page_addr, PAGE_SIZE);
1935
Chris Wilsonc400cc22017-11-07 15:22:11 +00001936 drm_printf(m, "Idle? %s\n", yesno(intel_engine_is_idle(engine)));
Chris Wilsonf636edb2017-10-09 12:02:57 +01001937}
1938
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +00001939static u8 user_class_map[] = {
1940 [I915_ENGINE_CLASS_RENDER] = RENDER_CLASS,
1941 [I915_ENGINE_CLASS_COPY] = COPY_ENGINE_CLASS,
1942 [I915_ENGINE_CLASS_VIDEO] = VIDEO_DECODE_CLASS,
1943 [I915_ENGINE_CLASS_VIDEO_ENHANCE] = VIDEO_ENHANCEMENT_CLASS,
1944};
1945
1946struct intel_engine_cs *
1947intel_engine_lookup_user(struct drm_i915_private *i915, u8 class, u8 instance)
1948{
1949 if (class >= ARRAY_SIZE(user_class_map))
1950 return NULL;
1951
1952 class = user_class_map[class];
1953
1954 GEM_BUG_ON(class > MAX_ENGINE_CLASS);
1955
1956 if (instance > MAX_ENGINE_INSTANCE)
1957 return NULL;
1958
1959 return i915->engine_class[class][instance];
1960}
1961
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00001962/**
1963 * intel_enable_engine_stats() - Enable engine busy tracking on engine
1964 * @engine: engine to enable stats collection
1965 *
1966 * Start collecting the engine busyness data for @engine.
1967 *
1968 * Returns 0 on success or a negative error code.
1969 */
1970int intel_enable_engine_stats(struct intel_engine_cs *engine)
1971{
Chris Wilson99e48bf2018-01-15 09:20:41 +00001972 struct intel_engine_execlists *execlists = &engine->execlists;
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00001973 unsigned long flags;
Chris Wilson99e48bf2018-01-15 09:20:41 +00001974 int err = 0;
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00001975
Tvrtko Ursulincf669b42017-11-29 10:28:05 +00001976 if (!intel_engine_supports_stats(engine))
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00001977 return -ENODEV;
1978
Chris Wilson99e48bf2018-01-15 09:20:41 +00001979 tasklet_disable(&execlists->tasklet);
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00001980 spin_lock_irqsave(&engine->stats.lock, flags);
Chris Wilson99e48bf2018-01-15 09:20:41 +00001981
1982 if (unlikely(engine->stats.enabled == ~0)) {
1983 err = -EBUSY;
1984 goto unlock;
1985 }
1986
Chris Wilson49007272018-01-11 07:30:31 +00001987 if (engine->stats.enabled++ == 0) {
Chris Wilson49007272018-01-11 07:30:31 +00001988 const struct execlist_port *port = execlists->port;
1989 unsigned int num_ports = execlists_num_ports(execlists);
1990
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00001991 engine->stats.enabled_at = ktime_get();
Chris Wilson49007272018-01-11 07:30:31 +00001992
1993 /* XXX submission method oblivious? */
1994 while (num_ports-- && port_isset(port)) {
1995 engine->stats.active++;
1996 port++;
1997 }
1998
1999 if (engine->stats.active)
2000 engine->stats.start = engine->stats.enabled_at;
2001 }
Chris Wilson99e48bf2018-01-15 09:20:41 +00002002
2003unlock:
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00002004 spin_unlock_irqrestore(&engine->stats.lock, flags);
Chris Wilson99e48bf2018-01-15 09:20:41 +00002005 tasklet_enable(&execlists->tasklet);
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00002006
Chris Wilson99e48bf2018-01-15 09:20:41 +00002007 return err;
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00002008}
2009
2010static ktime_t __intel_engine_get_busy_time(struct intel_engine_cs *engine)
2011{
2012 ktime_t total = engine->stats.total;
2013
2014 /*
2015 * If the engine is executing something at the moment
2016 * add it to the total.
2017 */
2018 if (engine->stats.active)
2019 total = ktime_add(total,
2020 ktime_sub(ktime_get(), engine->stats.start));
2021
2022 return total;
2023}
2024
2025/**
2026 * intel_engine_get_busy_time() - Return current accumulated engine busyness
2027 * @engine: engine to report on
2028 *
2029 * Returns accumulated time @engine was busy since engine stats were enabled.
2030 */
2031ktime_t intel_engine_get_busy_time(struct intel_engine_cs *engine)
2032{
2033 ktime_t total;
2034 unsigned long flags;
2035
2036 spin_lock_irqsave(&engine->stats.lock, flags);
2037 total = __intel_engine_get_busy_time(engine);
2038 spin_unlock_irqrestore(&engine->stats.lock, flags);
2039
2040 return total;
2041}
2042
2043/**
2044 * intel_disable_engine_stats() - Disable engine busy tracking on engine
2045 * @engine: engine to disable stats collection
2046 *
2047 * Stops collecting the engine busyness data for @engine.
2048 */
2049void intel_disable_engine_stats(struct intel_engine_cs *engine)
2050{
2051 unsigned long flags;
2052
Tvrtko Ursulincf669b42017-11-29 10:28:05 +00002053 if (!intel_engine_supports_stats(engine))
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00002054 return;
2055
2056 spin_lock_irqsave(&engine->stats.lock, flags);
2057 WARN_ON_ONCE(engine->stats.enabled == 0);
2058 if (--engine->stats.enabled == 0) {
2059 engine->stats.total = __intel_engine_get_busy_time(engine);
2060 engine->stats.active = 0;
2061 }
2062 spin_unlock_irqrestore(&engine->stats.lock, flags);
2063}
2064
Chris Wilsonf97fbf92017-02-13 17:15:14 +00002065#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
2066#include "selftests/mock_engine.c"
2067#endif