blob: 70325e0824e375bd3fc37047b7267cb3869d3621 [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
Daniele Ceraolo Spurio80b216b2018-03-14 11:26:50 -070084#define MAX_MMIO_BASES 3
Oscar Mateob8400f02017-04-10 07:34:32 -070085struct engine_info {
Michal Wajdeczko237ae7c2017-03-01 20:26:15 +000086 unsigned int hw_id;
Chris Wilson1d39f282017-04-11 13:43:06 +010087 unsigned int uabi_id;
Daniele Ceraolo Spurio09081802017-04-10 07:34:29 -070088 u8 class;
89 u8 instance;
Daniele Ceraolo Spurio80b216b2018-03-14 11:26:50 -070090 /* mmio bases table *must* be sorted in reverse gen order */
91 struct engine_mmio_base {
92 u32 gen : 8;
93 u32 base : 24;
94 } mmio_bases[MAX_MMIO_BASES];
Oscar Mateob8400f02017-04-10 07:34:32 -070095};
96
97static const struct engine_info intel_engines[] = {
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +010098 [RCS] = {
Tvrtko Ursulin5ec2cf72016-08-16 17:04:20 +010099 .hw_id = RCS_HW,
Chris Wilson1d39f282017-04-11 13:43:06 +0100100 .uabi_id = I915_EXEC_RENDER,
Daniele Ceraolo Spurio09081802017-04-10 07:34:29 -0700101 .class = RENDER_CLASS,
102 .instance = 0,
Daniele Ceraolo Spurio80b216b2018-03-14 11:26:50 -0700103 .mmio_bases = {
104 { .gen = 1, .base = RENDER_RING_BASE }
105 },
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100106 },
107 [BCS] = {
Tvrtko Ursulin5ec2cf72016-08-16 17:04:20 +0100108 .hw_id = BCS_HW,
Chris Wilson1d39f282017-04-11 13:43:06 +0100109 .uabi_id = I915_EXEC_BLT,
Daniele Ceraolo Spurio09081802017-04-10 07:34:29 -0700110 .class = COPY_ENGINE_CLASS,
111 .instance = 0,
Daniele Ceraolo Spurio80b216b2018-03-14 11:26:50 -0700112 .mmio_bases = {
113 { .gen = 6, .base = BLT_RING_BASE }
114 },
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100115 },
116 [VCS] = {
Tvrtko Ursulin5ec2cf72016-08-16 17:04:20 +0100117 .hw_id = VCS_HW,
Chris Wilson1d39f282017-04-11 13:43:06 +0100118 .uabi_id = I915_EXEC_BSD,
Daniele Ceraolo Spurio09081802017-04-10 07:34:29 -0700119 .class = VIDEO_DECODE_CLASS,
120 .instance = 0,
Daniele Ceraolo Spurio80b216b2018-03-14 11:26:50 -0700121 .mmio_bases = {
122 { .gen = 11, .base = GEN11_BSD_RING_BASE },
123 { .gen = 6, .base = GEN6_BSD_RING_BASE },
124 { .gen = 4, .base = BSD_RING_BASE }
125 },
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100126 },
127 [VCS2] = {
Tvrtko Ursulin5ec2cf72016-08-16 17:04:20 +0100128 .hw_id = VCS2_HW,
Chris Wilson1d39f282017-04-11 13:43:06 +0100129 .uabi_id = I915_EXEC_BSD,
Daniele Ceraolo Spurio09081802017-04-10 07:34:29 -0700130 .class = VIDEO_DECODE_CLASS,
131 .instance = 1,
Daniele Ceraolo Spurio80b216b2018-03-14 11:26:50 -0700132 .mmio_bases = {
133 { .gen = 11, .base = GEN11_BSD2_RING_BASE },
134 { .gen = 8, .base = GEN8_BSD2_RING_BASE }
135 },
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100136 },
Oscar Mateo5f79e7c2018-03-02 18:14:57 +0200137 [VCS3] = {
138 .hw_id = VCS3_HW,
139 .uabi_id = I915_EXEC_BSD,
140 .class = VIDEO_DECODE_CLASS,
141 .instance = 2,
Daniele Ceraolo Spurio80b216b2018-03-14 11:26:50 -0700142 .mmio_bases = {
143 { .gen = 11, .base = GEN11_BSD3_RING_BASE }
144 },
Oscar Mateo5f79e7c2018-03-02 18:14:57 +0200145 },
146 [VCS4] = {
147 .hw_id = VCS4_HW,
148 .uabi_id = I915_EXEC_BSD,
149 .class = VIDEO_DECODE_CLASS,
150 .instance = 3,
Daniele Ceraolo Spurio80b216b2018-03-14 11:26:50 -0700151 .mmio_bases = {
152 { .gen = 11, .base = GEN11_BSD4_RING_BASE }
153 },
Oscar Mateo5f79e7c2018-03-02 18:14:57 +0200154 },
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100155 [VECS] = {
Tvrtko Ursulin5ec2cf72016-08-16 17:04:20 +0100156 .hw_id = VECS_HW,
Chris Wilson1d39f282017-04-11 13:43:06 +0100157 .uabi_id = I915_EXEC_VEBOX,
Daniele Ceraolo Spurio09081802017-04-10 07:34:29 -0700158 .class = VIDEO_ENHANCEMENT_CLASS,
159 .instance = 0,
Daniele Ceraolo Spurio80b216b2018-03-14 11:26:50 -0700160 .mmio_bases = {
161 { .gen = 11, .base = GEN11_VEBOX_RING_BASE },
162 { .gen = 7, .base = VEBOX_RING_BASE }
163 },
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100164 },
Oscar Mateo5f79e7c2018-03-02 18:14:57 +0200165 [VECS2] = {
166 .hw_id = VECS2_HW,
167 .uabi_id = I915_EXEC_VEBOX,
168 .class = VIDEO_ENHANCEMENT_CLASS,
169 .instance = 1,
Daniele Ceraolo Spurio80b216b2018-03-14 11:26:50 -0700170 .mmio_bases = {
171 { .gen = 11, .base = GEN11_VEBOX2_RING_BASE }
172 },
Oscar Mateo5f79e7c2018-03-02 18:14:57 +0200173 },
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100174};
175
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +0300176/**
177 * ___intel_engine_context_size() - return the size of the context for an engine
178 * @dev_priv: i915 device private
179 * @class: engine class
180 *
181 * Each engine class may require a different amount of space for a context
182 * image.
183 *
184 * Return: size (in bytes) of an engine class specific context image
185 *
186 * Note: this size includes the HWSP, which is part of the context image
187 * in LRC mode, but does not include the "shared data page" used with
188 * GuC submission. The caller should account for this if using the GuC.
189 */
190static u32
191__intel_engine_context_size(struct drm_i915_private *dev_priv, u8 class)
192{
193 u32 cxt_size;
194
195 BUILD_BUG_ON(I915_GTT_PAGE_SIZE != PAGE_SIZE);
196
197 switch (class) {
198 case RENDER_CLASS:
199 switch (INTEL_GEN(dev_priv)) {
200 default:
201 MISSING_CASE(INTEL_GEN(dev_priv));
Oscar Mateo7ab4adb2018-01-11 14:55:06 -0800202 return DEFAULT_LR_CONTEXT_RENDER_SIZE;
Tvrtko Ursulinb86aa442018-01-11 14:55:07 -0800203 case 11:
204 return GEN11_LR_CONTEXT_RENDER_SIZE;
Rodrigo Vivif65f8412017-07-06 14:06:24 -0700205 case 10:
Oscar Mateo7fd0b1a2017-09-21 16:19:49 -0700206 return GEN10_LR_CONTEXT_RENDER_SIZE;
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +0300207 case 9:
208 return GEN9_LR_CONTEXT_RENDER_SIZE;
209 case 8:
Chris Wilsonfb5c5512017-11-20 20:55:00 +0000210 return GEN8_LR_CONTEXT_RENDER_SIZE;
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +0300211 case 7:
212 if (IS_HASWELL(dev_priv))
213 return HSW_CXT_TOTAL_SIZE;
214
215 cxt_size = I915_READ(GEN7_CXT_SIZE);
216 return round_up(GEN7_CXT_TOTAL_SIZE(cxt_size) * 64,
217 PAGE_SIZE);
218 case 6:
219 cxt_size = I915_READ(CXT_SIZE);
220 return round_up(GEN6_CXT_TOTAL_SIZE(cxt_size) * 64,
221 PAGE_SIZE);
222 case 5:
223 case 4:
224 case 3:
225 case 2:
226 /* For the special day when i810 gets merged. */
227 case 1:
228 return 0;
229 }
230 break;
231 default:
232 MISSING_CASE(class);
233 case VIDEO_DECODE_CLASS:
234 case VIDEO_ENHANCEMENT_CLASS:
235 case COPY_ENGINE_CLASS:
236 if (INTEL_GEN(dev_priv) < 8)
237 return 0;
238 return GEN8_LR_CONTEXT_OTHER_SIZE;
239 }
240}
241
Daniele Ceraolo Spurio80b216b2018-03-14 11:26:50 -0700242static u32 __engine_mmio_base(struct drm_i915_private *i915,
243 const struct engine_mmio_base *bases)
244{
245 int i;
246
247 for (i = 0; i < MAX_MMIO_BASES; i++)
248 if (INTEL_GEN(i915) >= bases[i].gen)
249 break;
250
251 GEM_BUG_ON(i == MAX_MMIO_BASES);
252 GEM_BUG_ON(!bases[i].base);
253
254 return bases[i].base;
255}
256
Daniele Ceraolo Spurio74419da2018-03-14 11:26:51 -0700257static void __sprint_engine_name(char *name, const struct engine_info *info)
258{
259 WARN_ON(snprintf(name, INTEL_ENGINE_CS_MAX_NAME, "%s%u",
260 intel_engine_classes[info->class].name,
261 info->instance) >= INTEL_ENGINE_CS_MAX_NAME);
262}
263
Akash Goel3b3f1652016-10-13 22:44:48 +0530264static int
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100265intel_engine_setup(struct drm_i915_private *dev_priv,
266 enum intel_engine_id id)
267{
268 const struct engine_info *info = &intel_engines[id];
Akash Goel3b3f1652016-10-13 22:44:48 +0530269 struct intel_engine_cs *engine;
270
Oscar Mateob8400f02017-04-10 07:34:32 -0700271 GEM_BUG_ON(info->class >= ARRAY_SIZE(intel_engine_classes));
Oscar Mateob8400f02017-04-10 07:34:32 -0700272
Daniele Ceraolo Spurioac52da62018-03-02 18:14:58 +0200273 BUILD_BUG_ON(MAX_ENGINE_CLASS >= BIT(GEN11_ENGINE_CLASS_WIDTH));
274 BUILD_BUG_ON(MAX_ENGINE_INSTANCE >= BIT(GEN11_ENGINE_INSTANCE_WIDTH));
275
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000276 if (GEM_WARN_ON(info->class > MAX_ENGINE_CLASS))
277 return -EINVAL;
278
279 if (GEM_WARN_ON(info->instance > MAX_ENGINE_INSTANCE))
280 return -EINVAL;
281
282 if (GEM_WARN_ON(dev_priv->engine_class[info->class][info->instance]))
283 return -EINVAL;
284
Akash Goel3b3f1652016-10-13 22:44:48 +0530285 GEM_BUG_ON(dev_priv->engine[id]);
286 engine = kzalloc(sizeof(*engine), GFP_KERNEL);
287 if (!engine)
288 return -ENOMEM;
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100289
290 engine->id = id;
291 engine->i915 = dev_priv;
Daniele Ceraolo Spurio74419da2018-03-14 11:26:51 -0700292 __sprint_engine_name(engine->name, info);
Tvrtko Ursulin5ec2cf72016-08-16 17:04:20 +0100293 engine->hw_id = engine->guc_id = info->hw_id;
Daniele Ceraolo Spurio80b216b2018-03-14 11:26:50 -0700294 engine->mmio_base = __engine_mmio_base(dev_priv, info->mmio_bases);
Daniele Ceraolo Spurio09081802017-04-10 07:34:29 -0700295 engine->class = info->class;
296 engine->instance = info->instance;
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100297
Tvrtko Ursulin1803fcbc2017-11-10 14:26:27 +0000298 engine->uabi_id = info->uabi_id;
Daniele Ceraolo Spurio74419da2018-03-14 11:26:51 -0700299 engine->uabi_class = intel_engine_classes[info->class].uabi_class;
Tvrtko Ursulin1803fcbc2017-11-10 14:26:27 +0000300
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +0300301 engine->context_size = __intel_engine_context_size(dev_priv,
302 engine->class);
303 if (WARN_ON(engine->context_size > BIT(20)))
304 engine->context_size = 0;
305
Chris Wilson0de91362016-11-14 20:41:01 +0000306 /* Nothing to do here, execute in order of dependencies */
307 engine->schedule = NULL;
308
Tvrtko Ursulin741258c2018-04-26 08:47:16 +0100309 seqlock_init(&engine->stats.lock);
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +0000310
Changbin Du3fc03062017-03-13 10:47:11 +0800311 ATOMIC_INIT_NOTIFIER_HEAD(&engine->context_status_notifier);
312
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000313 dev_priv->engine_class[info->class][info->instance] = engine;
Akash Goel3b3f1652016-10-13 22:44:48 +0530314 dev_priv->engine[id] = engine;
315 return 0;
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100316}
317
318/**
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +0300319 * intel_engines_init_mmio() - allocate and prepare the Engine Command Streamers
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +0000320 * @dev_priv: i915 device private
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100321 *
322 * Return: non-zero if the initialization failed.
323 */
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +0300324int intel_engines_init_mmio(struct drm_i915_private *dev_priv)
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100325{
Tvrtko Ursulinc1bb1142016-08-10 16:22:10 +0100326 struct intel_device_info *device_info = mkwrite_device_info(dev_priv);
Chris Wilson5f9be052017-04-11 17:56:58 +0100327 const unsigned int ring_mask = INTEL_INFO(dev_priv)->ring_mask;
Akash Goel3b3f1652016-10-13 22:44:48 +0530328 struct intel_engine_cs *engine;
329 enum intel_engine_id id;
Chris Wilson5f9be052017-04-11 17:56:58 +0100330 unsigned int mask = 0;
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100331 unsigned int i;
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000332 int err;
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100333
Tvrtko Ursulin70006ad2016-10-13 11:02:56 +0100334 WARN_ON(ring_mask == 0);
335 WARN_ON(ring_mask &
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100336 GENMASK(sizeof(mask) * BITS_PER_BYTE - 1, I915_NUM_ENGINES));
337
338 for (i = 0; i < ARRAY_SIZE(intel_engines); i++) {
339 if (!HAS_ENGINE(dev_priv, i))
340 continue;
341
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000342 err = intel_engine_setup(dev_priv, i);
343 if (err)
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100344 goto cleanup;
345
346 mask |= ENGINE_MASK(i);
347 }
348
349 /*
350 * Catch failures to update intel_engines table when the new engines
351 * are added to the driver by a warning and disabling the forgotten
352 * engines.
353 */
Tvrtko Ursulin70006ad2016-10-13 11:02:56 +0100354 if (WARN_ON(mask != ring_mask))
Tvrtko Ursulinc1bb1142016-08-10 16:22:10 +0100355 device_info->ring_mask = mask;
356
Chris Wilson5f9be052017-04-11 17:56:58 +0100357 /* We always presume we have at least RCS available for later probing */
358 if (WARN_ON(!HAS_ENGINE(dev_priv, RCS))) {
359 err = -ENODEV;
360 goto cleanup;
361 }
362
Tvrtko Ursulinc1bb1142016-08-10 16:22:10 +0100363 device_info->num_rings = hweight32(mask);
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100364
Michel Thierryce453b32017-11-10 16:44:47 -0800365 i915_check_and_clear_faults(dev_priv);
366
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100367 return 0;
368
369cleanup:
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000370 for_each_engine(engine, dev_priv, id)
371 kfree(engine);
372 return err;
373}
374
375/**
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +0300376 * intel_engines_init() - init the Engine Command Streamers
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000377 * @dev_priv: i915 device private
378 *
379 * Return: non-zero if the initialization failed.
380 */
381int intel_engines_init(struct drm_i915_private *dev_priv)
382{
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000383 struct intel_engine_cs *engine;
384 enum intel_engine_id id, err_id;
Tvrtko Ursulin33def1f2017-06-16 14:03:38 +0100385 int err;
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000386
Akash Goel3b3f1652016-10-13 22:44:48 +0530387 for_each_engine(engine, dev_priv, id) {
Oscar Mateob8400f02017-04-10 07:34:32 -0700388 const struct engine_class_info *class_info =
389 &intel_engine_classes[engine->class];
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000390 int (*init)(struct intel_engine_cs *engine);
391
Chris Wilsonfb5c5512017-11-20 20:55:00 +0000392 if (HAS_EXECLISTS(dev_priv))
Oscar Mateob8400f02017-04-10 07:34:32 -0700393 init = class_info->init_execlists;
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000394 else
Oscar Mateob8400f02017-04-10 07:34:32 -0700395 init = class_info->init_legacy;
Tvrtko Ursulin33def1f2017-06-16 14:03:38 +0100396
397 err = -EINVAL;
398 err_id = id;
399
400 if (GEM_WARN_ON(!init))
401 goto cleanup;
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000402
403 err = init(engine);
Tvrtko Ursulin33def1f2017-06-16 14:03:38 +0100404 if (err)
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000405 goto cleanup;
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000406
Chris Wilsonff44ad52017-03-16 17:13:03 +0000407 GEM_BUG_ON(!engine->submit_request);
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000408 }
409
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000410 return 0;
411
412cleanup:
413 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursulin33def1f2017-06-16 14:03:38 +0100414 if (id >= err_id) {
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000415 kfree(engine);
Tvrtko Ursulin33def1f2017-06-16 14:03:38 +0100416 dev_priv->engine[id] = NULL;
417 } else {
Tvrtko Ursulin8ee7c6e2017-02-16 12:23:22 +0000418 dev_priv->gt.cleanup_engine(engine);
Tvrtko Ursulin33def1f2017-06-16 14:03:38 +0100419 }
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100420 }
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000421 return err;
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100422}
423
Chris Wilson73cb9702016-10-28 13:58:46 +0100424void intel_engine_init_global_seqno(struct intel_engine_cs *engine, u32 seqno)
Chris Wilson57f275a2016-08-15 10:49:00 +0100425{
426 struct drm_i915_private *dev_priv = engine->i915;
427
428 /* Our semaphore implementation is strictly monotonic (i.e. we proceed
429 * so long as the semaphore value in the register/page is greater
430 * than the sync value), so whenever we reset the seqno,
431 * so long as we reset the tracking semaphore value to 0, it will
432 * always be before the next request's seqno. If we don't reset
433 * the semaphore value, then when the seqno moves backwards all
434 * future waits will complete instantly (causing rendering corruption).
435 */
436 if (IS_GEN6(dev_priv) || IS_GEN7(dev_priv)) {
437 I915_WRITE(RING_SYNC_0(engine->mmio_base), 0);
438 I915_WRITE(RING_SYNC_1(engine->mmio_base), 0);
439 if (HAS_VEBOX(dev_priv))
440 I915_WRITE(RING_SYNC_2(engine->mmio_base), 0);
441 }
Chris Wilson57f275a2016-08-15 10:49:00 +0100442
443 intel_write_status_page(engine, I915_GEM_HWS_INDEX, seqno);
Chris Wilson14a6bbf2017-03-14 11:14:52 +0000444 clear_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted);
Chris Wilson73cb9702016-10-28 13:58:46 +0100445
Chris Wilson57f275a2016-08-15 10:49:00 +0100446 /* After manually advancing the seqno, fake the interrupt in case
447 * there are any waiters for that seqno.
448 */
449 intel_engine_wakeup(engine);
Chris Wilson2ca9faa2017-04-05 16:30:54 +0100450
451 GEM_BUG_ON(intel_engine_get_seqno(engine) != seqno);
Chris Wilson57f275a2016-08-15 10:49:00 +0100452}
453
Michal Wajdeczkoc5781352018-03-08 09:50:35 +0000454static void intel_engine_init_batch_pool(struct intel_engine_cs *engine)
455{
456 i915_gem_batch_pool_init(&engine->batch_pool, engine);
457}
458
Mika Kuoppala19df9a52017-09-22 15:43:04 +0300459static bool csb_force_mmio(struct drm_i915_private *i915)
460{
Mika Kuoppala19df9a52017-09-22 15:43:04 +0300461 /*
462 * IOMMU adds unpredictable latency causing the CSB write (from the
463 * GPU into the HWSP) to only be visible some time after the interrupt
464 * (missed breadcrumb syndrome).
465 */
466 if (intel_vtd_active())
467 return true;
468
Weinan Li1fd51d92017-10-15 11:55:25 +0800469 /* Older GVT emulation depends upon intercepting CSB mmio */
470 if (intel_vgpu_active(i915) && !intel_vgpu_has_hwsp_emulation(i915))
471 return true;
472
Mika Kuoppala61bf9712018-04-12 17:58:02 +0300473 if (IS_CANNONLAKE(i915))
474 return true;
475
Mika Kuoppala19df9a52017-09-22 15:43:04 +0300476 return false;
477}
478
479static void intel_engine_init_execlist(struct intel_engine_cs *engine)
480{
481 struct intel_engine_execlists * const execlists = &engine->execlists;
482
483 execlists->csb_use_mmio = csb_force_mmio(engine->i915);
484
Mika Kuoppala76e70082017-09-22 15:43:07 +0300485 execlists->port_mask = 1;
486 BUILD_BUG_ON_NOT_POWER_OF_2(execlists_num_ports(execlists));
487 GEM_BUG_ON(execlists_num_ports(execlists) > EXECLIST_MAX_PORTS);
488
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000489 execlists->queue_priority = INT_MIN;
Mika Kuoppala19df9a52017-09-22 15:43:04 +0300490 execlists->queue = RB_ROOT;
491 execlists->first = NULL;
492}
493
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100494/**
495 * intel_engines_setup_common - setup engine state not requiring hw access
496 * @engine: Engine to setup.
497 *
498 * Initializes @engine@ structure members shared between legacy and execlists
499 * submission modes which do not require hardware access.
500 *
501 * Typically done early in the submission mode specific engine setup stage.
502 */
503void intel_engine_setup_common(struct intel_engine_cs *engine)
504{
Chris Wilsona89d1f92018-05-02 17:38:39 +0100505 i915_timeline_init(engine->i915, &engine->timeline, engine->name);
506
Mika Kuoppala19df9a52017-09-22 15:43:04 +0300507 intel_engine_init_execlist(engine);
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100508 intel_engine_init_hangcheck(engine);
Michal Wajdeczkoc5781352018-03-08 09:50:35 +0000509 intel_engine_init_batch_pool(engine);
Chris Wilson7756e452016-08-18 17:17:10 +0100510 intel_engine_init_cmd_parser(engine);
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100511}
512
Chris Wilsonadc320c2016-08-15 10:48:59 +0100513int intel_engine_create_scratch(struct intel_engine_cs *engine, int size)
514{
515 struct drm_i915_gem_object *obj;
516 struct i915_vma *vma;
517 int ret;
518
519 WARN_ON(engine->scratch);
520
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000521 obj = i915_gem_object_create_stolen(engine->i915, size);
Chris Wilsonadc320c2016-08-15 10:48:59 +0100522 if (!obj)
Chris Wilson920cf412016-10-28 13:58:30 +0100523 obj = i915_gem_object_create_internal(engine->i915, size);
Chris Wilsonadc320c2016-08-15 10:48:59 +0100524 if (IS_ERR(obj)) {
525 DRM_ERROR("Failed to allocate scratch page\n");
526 return PTR_ERR(obj);
527 }
528
Chris Wilsona01cb372017-01-16 15:21:30 +0000529 vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL);
Chris Wilsonadc320c2016-08-15 10:48:59 +0100530 if (IS_ERR(vma)) {
531 ret = PTR_ERR(vma);
532 goto err_unref;
533 }
534
535 ret = i915_vma_pin(vma, 0, 4096, PIN_GLOBAL | PIN_HIGH);
536 if (ret)
537 goto err_unref;
538
539 engine->scratch = vma;
Chris Wilsonadc320c2016-08-15 10:48:59 +0100540 return 0;
541
542err_unref:
543 i915_gem_object_put(obj);
544 return ret;
545}
546
547static void intel_engine_cleanup_scratch(struct intel_engine_cs *engine)
548{
Chris Wilson19880c42016-08-15 10:49:05 +0100549 i915_vma_unpin_and_release(&engine->scratch);
Chris Wilsonadc320c2016-08-15 10:48:59 +0100550}
551
Daniele Ceraolo Spurio486e93f2017-09-13 09:56:02 +0100552static void cleanup_phys_status_page(struct intel_engine_cs *engine)
553{
554 struct drm_i915_private *dev_priv = engine->i915;
555
556 if (!dev_priv->status_page_dmah)
557 return;
558
559 drm_pci_free(&dev_priv->drm, dev_priv->status_page_dmah);
560 engine->status_page.page_addr = NULL;
561}
562
563static void cleanup_status_page(struct intel_engine_cs *engine)
564{
565 struct i915_vma *vma;
566 struct drm_i915_gem_object *obj;
567
568 vma = fetch_and_zero(&engine->status_page.vma);
569 if (!vma)
570 return;
571
572 obj = vma->obj;
573
574 i915_vma_unpin(vma);
575 i915_vma_close(vma);
576
577 i915_gem_object_unpin_map(obj);
578 __i915_gem_object_release_unless_active(obj);
579}
580
581static int init_status_page(struct intel_engine_cs *engine)
582{
583 struct drm_i915_gem_object *obj;
584 struct i915_vma *vma;
585 unsigned int flags;
586 void *vaddr;
587 int ret;
588
589 obj = i915_gem_object_create_internal(engine->i915, PAGE_SIZE);
590 if (IS_ERR(obj)) {
591 DRM_ERROR("Failed to allocate status page\n");
592 return PTR_ERR(obj);
593 }
594
595 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
596 if (ret)
597 goto err;
598
599 vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL);
600 if (IS_ERR(vma)) {
601 ret = PTR_ERR(vma);
602 goto err;
603 }
604
605 flags = PIN_GLOBAL;
606 if (!HAS_LLC(engine->i915))
607 /* On g33, we cannot place HWS above 256MiB, so
608 * restrict its pinning to the low mappable arena.
609 * Though this restriction is not documented for
610 * gen4, gen5, or byt, they also behave similarly
611 * and hang if the HWS is placed at the top of the
612 * GTT. To generalise, it appears that all !llc
613 * platforms have issues with us placing the HWS
614 * above the mappable region (even though we never
615 * actually map it).
616 */
617 flags |= PIN_MAPPABLE;
Chris Wilson34a04e52017-09-13 09:56:03 +0100618 else
619 flags |= PIN_HIGH;
Daniele Ceraolo Spurio486e93f2017-09-13 09:56:02 +0100620 ret = i915_vma_pin(vma, 0, 4096, flags);
621 if (ret)
622 goto err;
623
624 vaddr = i915_gem_object_pin_map(obj, I915_MAP_WB);
625 if (IS_ERR(vaddr)) {
626 ret = PTR_ERR(vaddr);
627 goto err_unpin;
628 }
629
630 engine->status_page.vma = vma;
631 engine->status_page.ggtt_offset = i915_ggtt_offset(vma);
632 engine->status_page.page_addr = memset(vaddr, 0, PAGE_SIZE);
Daniele Ceraolo Spurio486e93f2017-09-13 09:56:02 +0100633 return 0;
634
635err_unpin:
636 i915_vma_unpin(vma);
637err:
638 i915_gem_object_put(obj);
639 return ret;
640}
641
642static int init_phys_status_page(struct intel_engine_cs *engine)
643{
644 struct drm_i915_private *dev_priv = engine->i915;
645
646 GEM_BUG_ON(engine->id != RCS);
647
648 dev_priv->status_page_dmah =
649 drm_pci_alloc(&dev_priv->drm, PAGE_SIZE, PAGE_SIZE);
650 if (!dev_priv->status_page_dmah)
651 return -ENOMEM;
652
653 engine->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
654 memset(engine->status_page.page_addr, 0, PAGE_SIZE);
655
656 return 0;
657}
658
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100659/**
660 * intel_engines_init_common - initialize cengine state which might require hw access
661 * @engine: Engine to initialize.
662 *
663 * Initializes @engine@ structure members shared between legacy and execlists
664 * submission modes which do require hardware access.
665 *
666 * Typcally done at later stages of submission mode specific engine setup.
667 *
668 * Returns zero on success or an error code on failure.
669 */
670int intel_engine_init_common(struct intel_engine_cs *engine)
671{
Chris Wilson266a2402017-05-04 10:33:08 +0100672 struct intel_ring *ring;
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100673 int ret;
674
Chris Wilsonff44ad52017-03-16 17:13:03 +0000675 engine->set_default_submission(engine);
676
Chris Wilsone8a9c582016-12-18 15:37:20 +0000677 /* We may need to do things with the shrinker which
678 * require us to immediately switch back to the default
679 * context. This can cause a problem as pinning the
680 * default context also requires GTT space which may not
681 * be available. To avoid this we always pin the default
682 * context.
683 */
Chris Wilsonab82a062018-04-30 14:15:01 +0100684 ring = intel_context_pin(engine->i915->kernel_context, engine);
Chris Wilson266a2402017-05-04 10:33:08 +0100685 if (IS_ERR(ring))
686 return PTR_ERR(ring);
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100687
Chris Wilsone7af3112017-10-03 21:34:48 +0100688 /*
689 * Similarly the preempt context must always be available so that
690 * we can interrupt the engine at any time.
691 */
Chris Wilsond6376372018-02-07 21:05:44 +0000692 if (engine->i915->preempt_context) {
Chris Wilsonab82a062018-04-30 14:15:01 +0100693 ring = intel_context_pin(engine->i915->preempt_context, engine);
Chris Wilsone7af3112017-10-03 21:34:48 +0100694 if (IS_ERR(ring)) {
695 ret = PTR_ERR(ring);
696 goto err_unpin_kernel;
697 }
698 }
699
Chris Wilsone8a9c582016-12-18 15:37:20 +0000700 ret = intel_engine_init_breadcrumbs(engine);
701 if (ret)
Chris Wilsone7af3112017-10-03 21:34:48 +0100702 goto err_unpin_preempt;
Chris Wilsone8a9c582016-12-18 15:37:20 +0000703
Daniele Ceraolo Spurio486e93f2017-09-13 09:56:02 +0100704 if (HWS_NEEDS_PHYSICAL(engine->i915))
705 ret = init_phys_status_page(engine);
706 else
707 ret = init_status_page(engine);
708 if (ret)
Chris Wilson7c2fa7f2017-11-10 14:26:34 +0000709 goto err_breadcrumbs;
Chris Wilson4e50f082016-10-28 13:58:31 +0100710
Chris Wilson7756e452016-08-18 17:17:10 +0100711 return 0;
Chris Wilsone8a9c582016-12-18 15:37:20 +0000712
Daniele Ceraolo Spurio486e93f2017-09-13 09:56:02 +0100713err_breadcrumbs:
714 intel_engine_fini_breadcrumbs(engine);
Chris Wilsone7af3112017-10-03 21:34:48 +0100715err_unpin_preempt:
Chris Wilsond6376372018-02-07 21:05:44 +0000716 if (engine->i915->preempt_context)
Chris Wilsonab82a062018-04-30 14:15:01 +0100717 intel_context_unpin(engine->i915->preempt_context, engine);
Chris Wilsone7af3112017-10-03 21:34:48 +0100718err_unpin_kernel:
Chris Wilsonab82a062018-04-30 14:15:01 +0100719 intel_context_unpin(engine->i915->kernel_context, engine);
Chris Wilsone8a9c582016-12-18 15:37:20 +0000720 return ret;
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100721}
Chris Wilson96a945a2016-08-03 13:19:16 +0100722
723/**
724 * intel_engines_cleanup_common - cleans up the engine state created by
725 * the common initiailizers.
726 * @engine: Engine to cleanup.
727 *
728 * This cleans up everything created by the common helpers.
729 */
730void intel_engine_cleanup_common(struct intel_engine_cs *engine)
731{
Chris Wilsonadc320c2016-08-15 10:48:59 +0100732 intel_engine_cleanup_scratch(engine);
733
Daniele Ceraolo Spurio486e93f2017-09-13 09:56:02 +0100734 if (HWS_NEEDS_PHYSICAL(engine->i915))
735 cleanup_phys_status_page(engine);
736 else
737 cleanup_status_page(engine);
738
Chris Wilson96a945a2016-08-03 13:19:16 +0100739 intel_engine_fini_breadcrumbs(engine);
Chris Wilson7756e452016-08-18 17:17:10 +0100740 intel_engine_cleanup_cmd_parser(engine);
Chris Wilson96a945a2016-08-03 13:19:16 +0100741 i915_gem_batch_pool_fini(&engine->batch_pool);
Chris Wilsone8a9c582016-12-18 15:37:20 +0000742
Chris Wilsond2b4b972017-11-10 14:26:33 +0000743 if (engine->default_state)
744 i915_gem_object_put(engine->default_state);
745
Chris Wilsond6376372018-02-07 21:05:44 +0000746 if (engine->i915->preempt_context)
Chris Wilsonab82a062018-04-30 14:15:01 +0100747 intel_context_unpin(engine->i915->preempt_context, engine);
748 intel_context_unpin(engine->i915->kernel_context, engine);
Chris Wilsona89d1f92018-05-02 17:38:39 +0100749
750 i915_timeline_fini(&engine->timeline);
Chris Wilson96a945a2016-08-03 13:19:16 +0100751}
Chris Wilson1b365952016-10-04 21:11:31 +0100752
Chris Wilson3ceda3a2018-02-12 10:24:15 +0000753u64 intel_engine_get_active_head(const struct intel_engine_cs *engine)
Chris Wilson1b365952016-10-04 21:11:31 +0100754{
755 struct drm_i915_private *dev_priv = engine->i915;
756 u64 acthd;
757
758 if (INTEL_GEN(dev_priv) >= 8)
759 acthd = I915_READ64_2x32(RING_ACTHD(engine->mmio_base),
760 RING_ACTHD_UDW(engine->mmio_base));
761 else if (INTEL_GEN(dev_priv) >= 4)
762 acthd = I915_READ(RING_ACTHD(engine->mmio_base));
763 else
764 acthd = I915_READ(ACTHD);
765
766 return acthd;
767}
768
Chris Wilson3ceda3a2018-02-12 10:24:15 +0000769u64 intel_engine_get_last_batch_head(const struct intel_engine_cs *engine)
Chris Wilson1b365952016-10-04 21:11:31 +0100770{
771 struct drm_i915_private *dev_priv = engine->i915;
772 u64 bbaddr;
773
774 if (INTEL_GEN(dev_priv) >= 8)
775 bbaddr = I915_READ64_2x32(RING_BBADDR(engine->mmio_base),
776 RING_BBADDR_UDW(engine->mmio_base));
777 else
778 bbaddr = I915_READ(RING_BBADDR(engine->mmio_base));
779
780 return bbaddr;
781}
Chris Wilson0e704472016-10-12 10:05:17 +0100782
783const char *i915_cache_level_str(struct drm_i915_private *i915, int type)
784{
785 switch (type) {
786 case I915_CACHE_NONE: return " uncached";
787 case I915_CACHE_LLC: return HAS_LLC(i915) ? " LLC" : " snooped";
788 case I915_CACHE_L3_LLC: return " L3+LLC";
789 case I915_CACHE_WT: return " WT";
790 default: return "";
791 }
792}
793
794static inline uint32_t
795read_subslice_reg(struct drm_i915_private *dev_priv, int slice,
796 int subslice, i915_reg_t reg)
797{
Kelvin Gardinerd3d57922018-03-16 14:14:51 +0200798 uint32_t mcr_slice_subslice_mask;
799 uint32_t mcr_slice_subslice_select;
Chris Wilson0e704472016-10-12 10:05:17 +0100800 uint32_t mcr;
801 uint32_t ret;
802 enum forcewake_domains fw_domains;
803
Kelvin Gardinerd3d57922018-03-16 14:14:51 +0200804 if (INTEL_GEN(dev_priv) >= 11) {
805 mcr_slice_subslice_mask = GEN11_MCR_SLICE_MASK |
806 GEN11_MCR_SUBSLICE_MASK;
807 mcr_slice_subslice_select = GEN11_MCR_SLICE(slice) |
808 GEN11_MCR_SUBSLICE(subslice);
809 } else {
810 mcr_slice_subslice_mask = GEN8_MCR_SLICE_MASK |
811 GEN8_MCR_SUBSLICE_MASK;
812 mcr_slice_subslice_select = GEN8_MCR_SLICE(slice) |
813 GEN8_MCR_SUBSLICE(subslice);
814 }
815
Chris Wilson0e704472016-10-12 10:05:17 +0100816 fw_domains = intel_uncore_forcewake_for_reg(dev_priv, reg,
817 FW_REG_READ);
818 fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
819 GEN8_MCR_SELECTOR,
820 FW_REG_READ | FW_REG_WRITE);
821
822 spin_lock_irq(&dev_priv->uncore.lock);
823 intel_uncore_forcewake_get__locked(dev_priv, fw_domains);
824
825 mcr = I915_READ_FW(GEN8_MCR_SELECTOR);
826 /*
827 * The HW expects the slice and sublice selectors to be reset to 0
828 * after reading out the registers.
829 */
Kelvin Gardinerd3d57922018-03-16 14:14:51 +0200830 WARN_ON_ONCE(mcr & mcr_slice_subslice_mask);
831 mcr &= ~mcr_slice_subslice_mask;
832 mcr |= mcr_slice_subslice_select;
Chris Wilson0e704472016-10-12 10:05:17 +0100833 I915_WRITE_FW(GEN8_MCR_SELECTOR, mcr);
834
835 ret = I915_READ_FW(reg);
836
Kelvin Gardinerd3d57922018-03-16 14:14:51 +0200837 mcr &= ~mcr_slice_subslice_mask;
Chris Wilson0e704472016-10-12 10:05:17 +0100838 I915_WRITE_FW(GEN8_MCR_SELECTOR, mcr);
839
840 intel_uncore_forcewake_put__locked(dev_priv, fw_domains);
841 spin_unlock_irq(&dev_priv->uncore.lock);
842
843 return ret;
844}
845
846/* NB: please notice the memset */
847void intel_engine_get_instdone(struct intel_engine_cs *engine,
848 struct intel_instdone *instdone)
849{
850 struct drm_i915_private *dev_priv = engine->i915;
851 u32 mmio_base = engine->mmio_base;
852 int slice;
853 int subslice;
854
855 memset(instdone, 0, sizeof(*instdone));
856
857 switch (INTEL_GEN(dev_priv)) {
858 default:
859 instdone->instdone = I915_READ(RING_INSTDONE(mmio_base));
860
861 if (engine->id != RCS)
862 break;
863
864 instdone->slice_common = I915_READ(GEN7_SC_INSTDONE);
865 for_each_instdone_slice_subslice(dev_priv, slice, subslice) {
866 instdone->sampler[slice][subslice] =
867 read_subslice_reg(dev_priv, slice, subslice,
868 GEN7_SAMPLER_INSTDONE);
869 instdone->row[slice][subslice] =
870 read_subslice_reg(dev_priv, slice, subslice,
871 GEN7_ROW_INSTDONE);
872 }
873 break;
874 case 7:
875 instdone->instdone = I915_READ(RING_INSTDONE(mmio_base));
876
877 if (engine->id != RCS)
878 break;
879
880 instdone->slice_common = I915_READ(GEN7_SC_INSTDONE);
881 instdone->sampler[0][0] = I915_READ(GEN7_SAMPLER_INSTDONE);
882 instdone->row[0][0] = I915_READ(GEN7_ROW_INSTDONE);
883
884 break;
885 case 6:
886 case 5:
887 case 4:
888 instdone->instdone = I915_READ(RING_INSTDONE(mmio_base));
889
890 if (engine->id == RCS)
891 /* HACK: Using the wrong struct member */
892 instdone->slice_common = I915_READ(GEN4_INSTDONE1);
893 break;
894 case 3:
895 case 2:
896 instdone->instdone = I915_READ(GEN2_INSTDONE);
897 break;
898 }
899}
Chris Wilsonf97fbf92017-02-13 17:15:14 +0000900
Chris Wilsona091d4e2017-05-30 13:13:33 +0100901static bool ring_is_idle(struct intel_engine_cs *engine)
902{
903 struct drm_i915_private *dev_priv = engine->i915;
904 bool idle = true;
905
Chris Wilson74d00d22018-02-12 09:39:28 +0000906 /* If the whole device is asleep, the engine must be idle */
907 if (!intel_runtime_pm_get_if_in_use(dev_priv))
908 return true;
Chris Wilsona091d4e2017-05-30 13:13:33 +0100909
Chris Wilsonaed2fc12017-05-30 13:13:34 +0100910 /* First check that no commands are left in the ring */
911 if ((I915_READ_HEAD(engine) & HEAD_ADDR) !=
912 (I915_READ_TAIL(engine) & TAIL_ADDR))
913 idle = false;
914
Chris Wilsona091d4e2017-05-30 13:13:33 +0100915 /* No bit for gen2, so assume the CS parser is idle */
916 if (INTEL_GEN(dev_priv) > 2 && !(I915_READ_MODE(engine) & MODE_IDLE))
917 idle = false;
918
919 intel_runtime_pm_put(dev_priv);
920
921 return idle;
922}
923
Chris Wilson54003672017-03-03 12:19:46 +0000924/**
925 * intel_engine_is_idle() - Report if the engine has finished process all work
926 * @engine: the intel_engine_cs
927 *
928 * Return true if there are no requests pending, nothing left to be submitted
929 * to hardware, and that the engine is idle.
930 */
931bool intel_engine_is_idle(struct intel_engine_cs *engine)
932{
933 struct drm_i915_private *dev_priv = engine->i915;
934
Chris Wilsona8e9a412017-04-11 20:00:42 +0100935 /* More white lies, if wedged, hw state is inconsistent */
936 if (i915_terminally_wedged(&dev_priv->gpu_error))
937 return true;
938
Chris Wilson54003672017-03-03 12:19:46 +0000939 /* Any inflight/incomplete requests? */
940 if (!i915_seqno_passed(intel_engine_get_seqno(engine),
941 intel_engine_last_submit(engine)))
942 return false;
943
Chris Wilson8968a362017-04-12 00:44:26 +0100944 if (I915_SELFTEST_ONLY(engine->breadcrumbs.mock))
945 return true;
946
Chris Wilson4a118ec2017-10-23 22:32:36 +0100947 /* Waiting to drain ELSP? */
948 if (READ_ONCE(engine->execlists.active))
Chris Wilson54003672017-03-03 12:19:46 +0000949 return false;
950
Chris Wilsond6edb6e2017-07-21 13:32:24 +0100951 /* ELSP is empty, but there are ready requests? */
Mika Kuoppalab620e872017-09-22 15:43:03 +0300952 if (READ_ONCE(engine->execlists.first))
Chris Wilsond6edb6e2017-07-21 13:32:24 +0100953 return false;
954
Chris Wilson54003672017-03-03 12:19:46 +0000955 /* Ring stopped? */
Chris Wilsona091d4e2017-05-30 13:13:33 +0100956 if (!ring_is_idle(engine))
Chris Wilson54003672017-03-03 12:19:46 +0000957 return false;
958
959 return true;
960}
961
Chris Wilson05425242017-03-03 12:19:47 +0000962bool intel_engines_are_idle(struct drm_i915_private *dev_priv)
963{
964 struct intel_engine_cs *engine;
965 enum intel_engine_id id;
966
Chris Wilsond7dc4132017-12-12 13:21:48 +0000967 /*
968 * If the driver is wedged, HW state may be very inconsistent and
Chris Wilson8490ae202017-03-30 15:50:37 +0100969 * report that it is still busy, even though we have stopped using it.
970 */
971 if (i915_terminally_wedged(&dev_priv->gpu_error))
972 return true;
973
Chris Wilson05425242017-03-03 12:19:47 +0000974 for_each_engine(engine, dev_priv, id) {
975 if (!intel_engine_is_idle(engine))
976 return false;
977 }
978
979 return true;
980}
981
Chris Wilsonae6c4572017-11-10 14:26:28 +0000982/**
983 * intel_engine_has_kernel_context:
984 * @engine: the engine
985 *
986 * Returns true if the last context to be executed on this engine, or has been
987 * executed if the engine is already idle, is the kernel context
988 * (#i915.kernel_context).
989 */
Chris Wilson20ccd4d2017-10-24 23:08:55 +0100990bool intel_engine_has_kernel_context(const struct intel_engine_cs *engine)
991{
Chris Wilsonae6c4572017-11-10 14:26:28 +0000992 const struct i915_gem_context * const kernel_context =
993 engine->i915->kernel_context;
Chris Wilsone61e0f52018-02-21 09:56:36 +0000994 struct i915_request *rq;
Chris Wilsonae6c4572017-11-10 14:26:28 +0000995
996 lockdep_assert_held(&engine->i915->drm.struct_mutex);
997
998 /*
999 * Check the last context seen by the engine. If active, it will be
1000 * the last request that remains in the timeline. When idle, it is
1001 * the last executed context as tracked by retirement.
1002 */
Chris Wilsona89d1f92018-05-02 17:38:39 +01001003 rq = __i915_gem_active_peek(&engine->timeline.last_request);
Chris Wilsonae6c4572017-11-10 14:26:28 +00001004 if (rq)
1005 return rq->ctx == kernel_context;
1006 else
1007 return engine->last_retired_context == kernel_context;
Chris Wilson20ccd4d2017-10-24 23:08:55 +01001008}
1009
Chris Wilsonff44ad52017-03-16 17:13:03 +00001010void intel_engines_reset_default_submission(struct drm_i915_private *i915)
1011{
1012 struct intel_engine_cs *engine;
1013 enum intel_engine_id id;
1014
1015 for_each_engine(engine, i915, id)
1016 engine->set_default_submission(engine);
1017}
1018
Chris Wilsonaba5e272017-10-25 15:39:41 +01001019/**
1020 * intel_engines_park: called when the GT is transitioning from busy->idle
1021 * @i915: the i915 device
1022 *
1023 * The GT is now idle and about to go to sleep (maybe never to wake again?).
1024 * Time for us to tidy and put away our toys (release resources back to the
1025 * system).
1026 */
1027void intel_engines_park(struct drm_i915_private *i915)
Chris Wilson6c067572017-05-17 13:10:03 +01001028{
1029 struct intel_engine_cs *engine;
1030 enum intel_engine_id id;
1031
1032 for_each_engine(engine, i915, id) {
Chris Wilson820c5bb2017-11-01 20:21:49 +00001033 /* Flush the residual irq tasklets first. */
1034 intel_engine_disarm_breadcrumbs(engine);
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05301035 tasklet_kill(&engine->execlists.tasklet);
Chris Wilson820c5bb2017-11-01 20:21:49 +00001036
Chris Wilson32651242017-10-27 12:06:17 +01001037 /*
1038 * We are committed now to parking the engines, make sure there
1039 * will be no more interrupts arriving later and the engines
1040 * are truly idle.
1041 */
Chris Wilson30b29402017-11-10 11:25:50 +00001042 if (wait_for(intel_engine_is_idle(engine), 10)) {
Chris Wilson32651242017-10-27 12:06:17 +01001043 struct drm_printer p = drm_debug_printer(__func__);
1044
Chris Wilson30b29402017-11-10 11:25:50 +00001045 dev_err(i915->drm.dev,
1046 "%s is not idle before parking\n",
1047 engine->name);
Chris Wilson0db18b12017-12-08 01:23:00 +00001048 intel_engine_dump(engine, &p, NULL);
Chris Wilson32651242017-10-27 12:06:17 +01001049 }
1050
Chris Wilson15c83c42018-04-11 11:39:29 +01001051 /* Must be reset upon idling, or we may miss the busy wakeup. */
1052 GEM_BUG_ON(engine->execlists.queue_priority != INT_MIN);
1053
Chris Wilsonaba5e272017-10-25 15:39:41 +01001054 if (engine->park)
1055 engine->park(engine);
1056
Chris Wilsonaba5e272017-10-25 15:39:41 +01001057 i915_gem_batch_pool_fini(&engine->batch_pool);
Mika Kuoppalab620e872017-09-22 15:43:03 +03001058 engine->execlists.no_priolist = false;
Chris Wilson6c067572017-05-17 13:10:03 +01001059 }
1060}
1061
Chris Wilsonaba5e272017-10-25 15:39:41 +01001062/**
1063 * intel_engines_unpark: called when the GT is transitioning from idle->busy
1064 * @i915: the i915 device
1065 *
1066 * The GT was idle and now about to fire up with some new user requests.
1067 */
1068void intel_engines_unpark(struct drm_i915_private *i915)
1069{
1070 struct intel_engine_cs *engine;
1071 enum intel_engine_id id;
1072
1073 for_each_engine(engine, i915, id) {
1074 if (engine->unpark)
1075 engine->unpark(engine);
Chris Wilsone21b1412018-05-02 23:03:13 +01001076
1077 intel_engine_init_hangcheck(engine);
Chris Wilsonaba5e272017-10-25 15:39:41 +01001078 }
1079}
1080
Chris Wilson90cad092017-09-06 16:28:59 +01001081bool intel_engine_can_store_dword(struct intel_engine_cs *engine)
1082{
1083 switch (INTEL_GEN(engine->i915)) {
1084 case 2:
1085 return false; /* uses physical not virtual addresses */
1086 case 3:
1087 /* maybe only uses physical not virtual addresses */
1088 return !(IS_I915G(engine->i915) || IS_I915GM(engine->i915));
1089 case 6:
1090 return engine->class != VIDEO_DECODE_CLASS; /* b0rked */
1091 default:
1092 return true;
1093 }
1094}
1095
Chris Wilsond2b4b972017-11-10 14:26:33 +00001096unsigned int intel_engines_has_context_isolation(struct drm_i915_private *i915)
1097{
1098 struct intel_engine_cs *engine;
1099 enum intel_engine_id id;
1100 unsigned int which;
1101
1102 which = 0;
1103 for_each_engine(engine, i915, id)
1104 if (engine->default_state)
1105 which |= BIT(engine->uabi_class);
1106
1107 return which;
1108}
1109
Chris Wilson247870a2018-04-24 02:08:39 +01001110static int print_sched_attr(struct drm_i915_private *i915,
1111 const struct i915_sched_attr *attr,
1112 char *buf, int x, int len)
Chris Wilsonb7268c52018-04-18 19:40:52 +01001113{
1114 if (attr->priority == I915_PRIORITY_INVALID)
Chris Wilson247870a2018-04-24 02:08:39 +01001115 return x;
Chris Wilsonb7268c52018-04-18 19:40:52 +01001116
Chris Wilson247870a2018-04-24 02:08:39 +01001117 x += snprintf(buf + x, len - x,
1118 " prio=%d", attr->priority);
1119
1120 return x;
Chris Wilsonb7268c52018-04-18 19:40:52 +01001121}
1122
Chris Wilsonf636edb2017-10-09 12:02:57 +01001123static void print_request(struct drm_printer *m,
Chris Wilsone61e0f52018-02-21 09:56:36 +00001124 struct i915_request *rq,
Chris Wilsonf636edb2017-10-09 12:02:57 +01001125 const char *prefix)
1126{
Chris Wilsonab268152018-03-14 10:16:30 +00001127 const char *name = rq->fence.ops->get_timeline_name(&rq->fence);
Chris Wilson247870a2018-04-24 02:08:39 +01001128 char buf[80];
1129 int x = 0;
Chris Wilsonab268152018-03-14 10:16:30 +00001130
Chris Wilson247870a2018-04-24 02:08:39 +01001131 x = print_sched_attr(rq->i915, &rq->sched.attr, buf, x, sizeof(buf));
1132
1133 drm_printf(m, "%s%x%s [%llx:%x]%s @ %dms: %s\n",
Chris Wilsonb7268c52018-04-18 19:40:52 +01001134 prefix,
Chris Wilsona27d5a42017-10-15 21:43:10 +01001135 rq->global_seqno,
Chris Wilsone61e0f52018-02-21 09:56:36 +00001136 i915_request_completed(rq) ? "!" : "",
Chris Wilson247870a2018-04-24 02:08:39 +01001137 rq->fence.context, rq->fence.seqno,
1138 buf,
Chris Wilsonf636edb2017-10-09 12:02:57 +01001139 jiffies_to_msecs(jiffies - rq->emitted_jiffies),
Chris Wilsonab268152018-03-14 10:16:30 +00001140 name);
Chris Wilsonf636edb2017-10-09 12:02:57 +01001141}
1142
Chris Wilsonc1bf2722017-12-22 18:25:21 +00001143static void hexdump(struct drm_printer *m, const void *buf, size_t len)
1144{
1145 const size_t rowsize = 8 * sizeof(u32);
1146 const void *prev = NULL;
1147 bool skip = false;
1148 size_t pos;
1149
1150 for (pos = 0; pos < len; pos += rowsize) {
1151 char line[128];
1152
1153 if (prev && !memcmp(prev, buf + pos, rowsize)) {
1154 if (!skip) {
1155 drm_printf(m, "*\n");
1156 skip = true;
1157 }
1158 continue;
1159 }
1160
1161 WARN_ON_ONCE(hex_dump_to_buffer(buf + pos, len - pos,
1162 rowsize, sizeof(u32),
1163 line, sizeof(line),
1164 false) >= sizeof(line));
1165 drm_printf(m, "%08zx %s\n", pos, line);
1166
1167 prev = buf + pos;
1168 skip = false;
1169 }
1170}
1171
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001172static void intel_engine_print_registers(const struct intel_engine_cs *engine,
1173 struct drm_printer *m)
Chris Wilsonf636edb2017-10-09 12:02:57 +01001174{
Chris Wilsonf636edb2017-10-09 12:02:57 +01001175 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001176 const struct intel_engine_execlists * const execlists =
1177 &engine->execlists;
Chris Wilsonf636edb2017-10-09 12:02:57 +01001178 u64 addr;
1179
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001180 drm_printf(m, "\tRING_START: 0x%08x\n",
1181 I915_READ(RING_START(engine->mmio_base)));
1182 drm_printf(m, "\tRING_HEAD: 0x%08x\n",
1183 I915_READ(RING_HEAD(engine->mmio_base)) & HEAD_ADDR);
1184 drm_printf(m, "\tRING_TAIL: 0x%08x\n",
1185 I915_READ(RING_TAIL(engine->mmio_base)) & TAIL_ADDR);
Chris Wilson3c75de52017-10-26 12:50:48 +01001186 drm_printf(m, "\tRING_CTL: 0x%08x%s\n",
Chris Wilsonf636edb2017-10-09 12:02:57 +01001187 I915_READ(RING_CTL(engine->mmio_base)),
Chris Wilson3c75de52017-10-26 12:50:48 +01001188 I915_READ(RING_CTL(engine->mmio_base)) & (RING_WAIT | RING_WAIT_SEMAPHORE) ? " [waiting]" : "");
1189 if (INTEL_GEN(engine->i915) > 2) {
1190 drm_printf(m, "\tRING_MODE: 0x%08x%s\n",
1191 I915_READ(RING_MI_MODE(engine->mmio_base)),
1192 I915_READ(RING_MI_MODE(engine->mmio_base)) & (MODE_IDLE) ? " [idle]" : "");
1193 }
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001194
1195 if (INTEL_GEN(dev_priv) >= 6) {
1196 drm_printf(m, "\tRING_IMR: %08x\n", I915_READ_IMR(engine));
1197 }
1198
Chris Wilson93c6e962017-11-20 20:55:04 +00001199 if (HAS_LEGACY_SEMAPHORES(dev_priv)) {
Chris Wilsonaf9ff6c2017-11-20 20:55:03 +00001200 drm_printf(m, "\tSYNC_0: 0x%08x\n",
1201 I915_READ(RING_SYNC_0(engine->mmio_base)));
1202 drm_printf(m, "\tSYNC_1: 0x%08x\n",
1203 I915_READ(RING_SYNC_1(engine->mmio_base)));
1204 if (HAS_VEBOX(dev_priv))
1205 drm_printf(m, "\tSYNC_2: 0x%08x\n",
1206 I915_READ(RING_SYNC_2(engine->mmio_base)));
1207 }
Chris Wilsonf636edb2017-10-09 12:02:57 +01001208
Chris Wilsonf636edb2017-10-09 12:02:57 +01001209 addr = intel_engine_get_active_head(engine);
1210 drm_printf(m, "\tACTHD: 0x%08x_%08x\n",
1211 upper_32_bits(addr), lower_32_bits(addr));
1212 addr = intel_engine_get_last_batch_head(engine);
1213 drm_printf(m, "\tBBADDR: 0x%08x_%08x\n",
1214 upper_32_bits(addr), lower_32_bits(addr));
Chris Wilsona0cf5792017-12-18 12:39:14 +00001215 if (INTEL_GEN(dev_priv) >= 8)
1216 addr = I915_READ64_2x32(RING_DMA_FADD(engine->mmio_base),
1217 RING_DMA_FADD_UDW(engine->mmio_base));
1218 else if (INTEL_GEN(dev_priv) >= 4)
1219 addr = I915_READ(RING_DMA_FADD(engine->mmio_base));
1220 else
1221 addr = I915_READ(DMA_FADD_I8XX);
1222 drm_printf(m, "\tDMA_FADDR: 0x%08x_%08x\n",
1223 upper_32_bits(addr), lower_32_bits(addr));
1224 if (INTEL_GEN(dev_priv) >= 4) {
1225 drm_printf(m, "\tIPEIR: 0x%08x\n",
1226 I915_READ(RING_IPEIR(engine->mmio_base)));
1227 drm_printf(m, "\tIPEHR: 0x%08x\n",
1228 I915_READ(RING_IPEHR(engine->mmio_base)));
1229 } else {
1230 drm_printf(m, "\tIPEIR: 0x%08x\n", I915_READ(IPEIR));
1231 drm_printf(m, "\tIPEHR: 0x%08x\n", I915_READ(IPEHR));
1232 }
Chris Wilsonf636edb2017-10-09 12:02:57 +01001233
Chris Wilsonfb5c5512017-11-20 20:55:00 +00001234 if (HAS_EXECLISTS(dev_priv)) {
Chris Wilsonf636edb2017-10-09 12:02:57 +01001235 const u32 *hws = &engine->status_page.page_addr[I915_HWS_CSB_BUF0_INDEX];
Chris Wilsonf636edb2017-10-09 12:02:57 +01001236 u32 ptr, read, write;
1237 unsigned int idx;
1238
1239 drm_printf(m, "\tExeclist status: 0x%08x %08x\n",
1240 I915_READ(RING_EXECLIST_STATUS_LO(engine)),
1241 I915_READ(RING_EXECLIST_STATUS_HI(engine)));
1242
1243 ptr = I915_READ(RING_CONTEXT_STATUS_PTR(engine));
1244 read = GEN8_CSB_READ_PTR(ptr);
1245 write = GEN8_CSB_WRITE_PTR(ptr);
Chris Wilson90408712018-03-26 12:50:36 +01001246 drm_printf(m, "\tExeclist CSB read %d [%d cached], write %d [%d from hws], interrupt posted? %s, tasklet queued? %s (%s)\n",
Chris Wilsonf636edb2017-10-09 12:02:57 +01001247 read, execlists->csb_head,
1248 write,
1249 intel_read_status_page(engine, intel_hws_csb_write_index(engine->i915)),
1250 yesno(test_bit(ENGINE_IRQ_EXECLIST,
Chris Wilson90408712018-03-26 12:50:36 +01001251 &engine->irq_posted)),
1252 yesno(test_bit(TASKLET_STATE_SCHED,
1253 &engine->execlists.tasklet.state)),
1254 enableddisabled(!atomic_read(&engine->execlists.tasklet.count)));
Chris Wilsonf636edb2017-10-09 12:02:57 +01001255 if (read >= GEN8_CSB_ENTRIES)
1256 read = 0;
1257 if (write >= GEN8_CSB_ENTRIES)
1258 write = 0;
1259 if (read > write)
1260 write += GEN8_CSB_ENTRIES;
1261 while (read < write) {
1262 idx = ++read % GEN8_CSB_ENTRIES;
1263 drm_printf(m, "\tExeclist CSB[%d]: 0x%08x [0x%08x in hwsp], context: %d [%d in hwsp]\n",
1264 idx,
1265 I915_READ(RING_CONTEXT_STATUS_BUF_LO(engine, idx)),
1266 hws[idx * 2],
1267 I915_READ(RING_CONTEXT_STATUS_BUF_HI(engine, idx)),
1268 hws[idx * 2 + 1]);
1269 }
1270
1271 rcu_read_lock();
1272 for (idx = 0; idx < execlists_num_ports(execlists); idx++) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00001273 struct i915_request *rq;
Chris Wilsonf636edb2017-10-09 12:02:57 +01001274 unsigned int count;
1275
1276 rq = port_unpack(&execlists->port[idx], &count);
1277 if (rq) {
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001278 char hdr[80];
1279
Chris Wilsone8a70ca2017-12-08 01:22:59 +00001280 snprintf(hdr, sizeof(hdr),
Chris Wilson3a068722018-05-02 11:41:50 +01001281 "\t\tELSP[%d] count=%d, ring->start=%08x, rq: ",
1282 idx, count,
1283 i915_ggtt_offset(rq->ring->vma));
Chris Wilsone8a70ca2017-12-08 01:22:59 +00001284 print_request(m, rq, hdr);
Chris Wilsonf636edb2017-10-09 12:02:57 +01001285 } else {
Chris Wilsone8a70ca2017-12-08 01:22:59 +00001286 drm_printf(m, "\t\tELSP[%d] idle\n", idx);
Chris Wilsonf636edb2017-10-09 12:02:57 +01001287 }
1288 }
Chris Wilson4a118ec2017-10-23 22:32:36 +01001289 drm_printf(m, "\t\tHW active? 0x%x\n", execlists->active);
Chris Wilsonf636edb2017-10-09 12:02:57 +01001290 rcu_read_unlock();
Chris Wilsonf636edb2017-10-09 12:02:57 +01001291 } else if (INTEL_GEN(dev_priv) > 6) {
1292 drm_printf(m, "\tPP_DIR_BASE: 0x%08x\n",
1293 I915_READ(RING_PP_DIR_BASE(engine)));
1294 drm_printf(m, "\tPP_DIR_BASE_READ: 0x%08x\n",
1295 I915_READ(RING_PP_DIR_BASE_READ(engine)));
1296 drm_printf(m, "\tPP_DIR_DCLV: 0x%08x\n",
1297 I915_READ(RING_PP_DIR_DCLV(engine)));
1298 }
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001299}
1300
1301void intel_engine_dump(struct intel_engine_cs *engine,
1302 struct drm_printer *m,
1303 const char *header, ...)
1304{
Chris Wilson56021f42018-04-24 09:16:00 +01001305 const int MAX_REQUESTS_TO_SHOW = 8;
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001306 struct intel_breadcrumbs * const b = &engine->breadcrumbs;
1307 const struct intel_engine_execlists * const execlists = &engine->execlists;
1308 struct i915_gpu_error * const error = &engine->i915->gpu_error;
Chris Wilson56021f42018-04-24 09:16:00 +01001309 struct i915_request *rq, *last;
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001310 struct rb_node *rb;
Chris Wilson56021f42018-04-24 09:16:00 +01001311 int count;
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001312
1313 if (header) {
1314 va_list ap;
1315
1316 va_start(ap, header);
1317 drm_vprintf(m, header, &ap);
1318 va_end(ap);
1319 }
1320
1321 if (i915_terminally_wedged(&engine->i915->gpu_error))
1322 drm_printf(m, "*** WEDGED ***\n");
1323
Chris Wilson52d7f162018-04-30 14:15:00 +01001324 drm_printf(m, "\tcurrent seqno %x, last %x, hangcheck %x [%d ms]\n",
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001325 intel_engine_get_seqno(engine),
1326 intel_engine_last_submit(engine),
1327 engine->hangcheck.seqno,
Chris Wilson52d7f162018-04-30 14:15:00 +01001328 jiffies_to_msecs(jiffies - engine->hangcheck.action_timestamp));
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001329 drm_printf(m, "\tReset count: %d (global %d)\n",
1330 i915_reset_engine_count(error, engine),
1331 i915_reset_count(error));
1332
1333 rcu_read_lock();
1334
1335 drm_printf(m, "\tRequests:\n");
1336
Chris Wilsona89d1f92018-05-02 17:38:39 +01001337 rq = list_first_entry(&engine->timeline.requests,
Chris Wilsone61e0f52018-02-21 09:56:36 +00001338 struct i915_request, link);
Chris Wilsona89d1f92018-05-02 17:38:39 +01001339 if (&rq->link != &engine->timeline.requests)
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001340 print_request(m, rq, "\t\tfirst ");
1341
Chris Wilsona89d1f92018-05-02 17:38:39 +01001342 rq = list_last_entry(&engine->timeline.requests,
Chris Wilsone61e0f52018-02-21 09:56:36 +00001343 struct i915_request, link);
Chris Wilsona89d1f92018-05-02 17:38:39 +01001344 if (&rq->link != &engine->timeline.requests)
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001345 print_request(m, rq, "\t\tlast ");
1346
1347 rq = i915_gem_find_active_request(engine);
1348 if (rq) {
1349 print_request(m, rq, "\t\tactive ");
1350 drm_printf(m,
1351 "\t\t[head %04x, postfix %04x, tail %04x, batch 0x%08x_%08x]\n",
1352 rq->head, rq->postfix, rq->tail,
1353 rq->batch ? upper_32_bits(rq->batch->node.start) : ~0u,
1354 rq->batch ? lower_32_bits(rq->batch->node.start) : ~0u);
Chris Wilsonef5032a2018-03-07 13:42:24 +00001355 drm_printf(m, "\t\tring->start: 0x%08x\n",
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001356 i915_ggtt_offset(rq->ring->vma));
Chris Wilsonef5032a2018-03-07 13:42:24 +00001357 drm_printf(m, "\t\tring->head: 0x%08x\n",
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001358 rq->ring->head);
Chris Wilsonef5032a2018-03-07 13:42:24 +00001359 drm_printf(m, "\t\tring->tail: 0x%08x\n",
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001360 rq->ring->tail);
Chris Wilsonef5032a2018-03-07 13:42:24 +00001361 drm_printf(m, "\t\tring->emit: 0x%08x\n",
1362 rq->ring->emit);
1363 drm_printf(m, "\t\tring->space: 0x%08x\n",
1364 rq->ring->space);
Chris Wilson3ceda3a2018-02-12 10:24:15 +00001365 }
1366
1367 rcu_read_unlock();
1368
1369 if (intel_runtime_pm_get_if_in_use(engine->i915)) {
1370 intel_engine_print_registers(engine, m);
1371 intel_runtime_pm_put(engine->i915);
1372 } else {
1373 drm_printf(m, "\tDevice is asleep; skipping register dump\n");
1374 }
Chris Wilsonf636edb2017-10-09 12:02:57 +01001375
Chris Wilsona89d1f92018-05-02 17:38:39 +01001376 spin_lock_irq(&engine->timeline.lock);
Chris Wilson56021f42018-04-24 09:16:00 +01001377
1378 last = NULL;
1379 count = 0;
Chris Wilsona89d1f92018-05-02 17:38:39 +01001380 list_for_each_entry(rq, &engine->timeline.requests, link) {
Chris Wilson56021f42018-04-24 09:16:00 +01001381 if (count++ < MAX_REQUESTS_TO_SHOW - 1)
1382 print_request(m, rq, "\t\tE ");
1383 else
1384 last = rq;
1385 }
1386 if (last) {
1387 if (count > MAX_REQUESTS_TO_SHOW) {
1388 drm_printf(m,
1389 "\t\t...skipping %d executing requests...\n",
1390 count - MAX_REQUESTS_TO_SHOW);
1391 }
1392 print_request(m, last, "\t\tE ");
1393 }
1394
1395 last = NULL;
1396 count = 0;
Chris Wilsonf6322ed2018-02-22 14:22:29 +00001397 drm_printf(m, "\t\tQueue priority: %d\n", execlists->queue_priority);
Chris Wilsona27d5a42017-10-15 21:43:10 +01001398 for (rb = execlists->first; rb; rb = rb_next(rb)) {
1399 struct i915_priolist *p =
1400 rb_entry(rb, typeof(*p), node);
1401
Chris Wilson56021f42018-04-24 09:16:00 +01001402 list_for_each_entry(rq, &p->requests, sched.link) {
1403 if (count++ < MAX_REQUESTS_TO_SHOW - 1)
1404 print_request(m, rq, "\t\tQ ");
1405 else
1406 last = rq;
1407 }
Chris Wilsona27d5a42017-10-15 21:43:10 +01001408 }
Chris Wilson56021f42018-04-24 09:16:00 +01001409 if (last) {
1410 if (count > MAX_REQUESTS_TO_SHOW) {
1411 drm_printf(m,
1412 "\t\t...skipping %d queued requests...\n",
1413 count - MAX_REQUESTS_TO_SHOW);
1414 }
1415 print_request(m, last, "\t\tQ ");
1416 }
1417
Chris Wilsona89d1f92018-05-02 17:38:39 +01001418 spin_unlock_irq(&engine->timeline.lock);
Chris Wilsona27d5a42017-10-15 21:43:10 +01001419
Chris Wilsonf636edb2017-10-09 12:02:57 +01001420 spin_lock_irq(&b->rb_lock);
1421 for (rb = rb_first(&b->waiters); rb; rb = rb_next(rb)) {
1422 struct intel_wait *w = rb_entry(rb, typeof(*w), node);
1423
1424 drm_printf(m, "\t%s [%d] waiting for %x\n",
1425 w->tsk->comm, w->tsk->pid, w->seqno);
1426 }
1427 spin_unlock_irq(&b->rb_lock);
1428
Chris Wilson832265d2017-12-08 01:23:01 +00001429 drm_printf(m, "IRQ? 0x%lx (breadcrumbs? %s) (execlists? %s)\n",
1430 engine->irq_posted,
1431 yesno(test_bit(ENGINE_IRQ_BREADCRUMB,
1432 &engine->irq_posted)),
1433 yesno(test_bit(ENGINE_IRQ_EXECLIST,
1434 &engine->irq_posted)));
Chris Wilsonc1bf2722017-12-22 18:25:21 +00001435
1436 drm_printf(m, "HWSP:\n");
1437 hexdump(m, engine->status_page.page_addr, PAGE_SIZE);
1438
Chris Wilsonc400cc22017-11-07 15:22:11 +00001439 drm_printf(m, "Idle? %s\n", yesno(intel_engine_is_idle(engine)));
Chris Wilsonf636edb2017-10-09 12:02:57 +01001440}
1441
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +00001442static u8 user_class_map[] = {
1443 [I915_ENGINE_CLASS_RENDER] = RENDER_CLASS,
1444 [I915_ENGINE_CLASS_COPY] = COPY_ENGINE_CLASS,
1445 [I915_ENGINE_CLASS_VIDEO] = VIDEO_DECODE_CLASS,
1446 [I915_ENGINE_CLASS_VIDEO_ENHANCE] = VIDEO_ENHANCEMENT_CLASS,
1447};
1448
1449struct intel_engine_cs *
1450intel_engine_lookup_user(struct drm_i915_private *i915, u8 class, u8 instance)
1451{
1452 if (class >= ARRAY_SIZE(user_class_map))
1453 return NULL;
1454
1455 class = user_class_map[class];
1456
1457 GEM_BUG_ON(class > MAX_ENGINE_CLASS);
1458
1459 if (instance > MAX_ENGINE_INSTANCE)
1460 return NULL;
1461
1462 return i915->engine_class[class][instance];
1463}
1464
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00001465/**
1466 * intel_enable_engine_stats() - Enable engine busy tracking on engine
1467 * @engine: engine to enable stats collection
1468 *
1469 * Start collecting the engine busyness data for @engine.
1470 *
1471 * Returns 0 on success or a negative error code.
1472 */
1473int intel_enable_engine_stats(struct intel_engine_cs *engine)
1474{
Chris Wilson99e48bf2018-01-15 09:20:41 +00001475 struct intel_engine_execlists *execlists = &engine->execlists;
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00001476 unsigned long flags;
Chris Wilson99e48bf2018-01-15 09:20:41 +00001477 int err = 0;
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00001478
Tvrtko Ursulincf669b42017-11-29 10:28:05 +00001479 if (!intel_engine_supports_stats(engine))
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00001480 return -ENODEV;
1481
Chris Wilson99e48bf2018-01-15 09:20:41 +00001482 tasklet_disable(&execlists->tasklet);
Tvrtko Ursulin741258c2018-04-26 08:47:16 +01001483 write_seqlock_irqsave(&engine->stats.lock, flags);
Chris Wilson99e48bf2018-01-15 09:20:41 +00001484
1485 if (unlikely(engine->stats.enabled == ~0)) {
1486 err = -EBUSY;
1487 goto unlock;
1488 }
1489
Chris Wilson49007272018-01-11 07:30:31 +00001490 if (engine->stats.enabled++ == 0) {
Chris Wilson49007272018-01-11 07:30:31 +00001491 const struct execlist_port *port = execlists->port;
1492 unsigned int num_ports = execlists_num_ports(execlists);
1493
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00001494 engine->stats.enabled_at = ktime_get();
Chris Wilson49007272018-01-11 07:30:31 +00001495
1496 /* XXX submission method oblivious? */
1497 while (num_ports-- && port_isset(port)) {
1498 engine->stats.active++;
1499 port++;
1500 }
1501
1502 if (engine->stats.active)
1503 engine->stats.start = engine->stats.enabled_at;
1504 }
Chris Wilson99e48bf2018-01-15 09:20:41 +00001505
1506unlock:
Tvrtko Ursulin741258c2018-04-26 08:47:16 +01001507 write_sequnlock_irqrestore(&engine->stats.lock, flags);
Chris Wilson99e48bf2018-01-15 09:20:41 +00001508 tasklet_enable(&execlists->tasklet);
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00001509
Chris Wilson99e48bf2018-01-15 09:20:41 +00001510 return err;
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00001511}
1512
1513static ktime_t __intel_engine_get_busy_time(struct intel_engine_cs *engine)
1514{
1515 ktime_t total = engine->stats.total;
1516
1517 /*
1518 * If the engine is executing something at the moment
1519 * add it to the total.
1520 */
1521 if (engine->stats.active)
1522 total = ktime_add(total,
1523 ktime_sub(ktime_get(), engine->stats.start));
1524
1525 return total;
1526}
1527
1528/**
1529 * intel_engine_get_busy_time() - Return current accumulated engine busyness
1530 * @engine: engine to report on
1531 *
1532 * Returns accumulated time @engine was busy since engine stats were enabled.
1533 */
1534ktime_t intel_engine_get_busy_time(struct intel_engine_cs *engine)
1535{
Tvrtko Ursulin741258c2018-04-26 08:47:16 +01001536 unsigned int seq;
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00001537 ktime_t total;
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00001538
Tvrtko Ursulin741258c2018-04-26 08:47:16 +01001539 do {
1540 seq = read_seqbegin(&engine->stats.lock);
1541 total = __intel_engine_get_busy_time(engine);
1542 } while (read_seqretry(&engine->stats.lock, seq));
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00001543
1544 return total;
1545}
1546
1547/**
1548 * intel_disable_engine_stats() - Disable engine busy tracking on engine
1549 * @engine: engine to disable stats collection
1550 *
1551 * Stops collecting the engine busyness data for @engine.
1552 */
1553void intel_disable_engine_stats(struct intel_engine_cs *engine)
1554{
1555 unsigned long flags;
1556
Tvrtko Ursulincf669b42017-11-29 10:28:05 +00001557 if (!intel_engine_supports_stats(engine))
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00001558 return;
1559
Tvrtko Ursulin741258c2018-04-26 08:47:16 +01001560 write_seqlock_irqsave(&engine->stats.lock, flags);
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00001561 WARN_ON_ONCE(engine->stats.enabled == 0);
1562 if (--engine->stats.enabled == 0) {
1563 engine->stats.total = __intel_engine_get_busy_time(engine);
1564 engine->stats.active = 0;
1565 }
Tvrtko Ursulin741258c2018-04-26 08:47:16 +01001566 write_sequnlock_irqrestore(&engine->stats.lock, flags);
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +00001567}
1568
Chris Wilsonf97fbf92017-02-13 17:15:14 +00001569#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1570#include "selftests/mock_engine.c"
Daniele Ceraolo Spurio74419da2018-03-14 11:26:51 -07001571#include "selftests/intel_engine_cs.c"
Chris Wilsonf97fbf92017-02-13 17:15:14 +00001572#endif