]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - drivers/gpu/drm/i915/intel_engine_cs.c
drm/i915: Use safer intel_uncore_wait_for_register in ring-init
[mirror_ubuntu-focal-kernel.git] / drivers / gpu / drm / i915 / intel_engine_cs.c
CommitLineData
88d2ba2e
TU
1/*
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
25#include "i915_drv.h"
26#include "intel_ringbuffer.h"
27#include "intel_lrc.h"
28
29static const struct engine_info {
30 const char *name;
237ae7c7
MW
31 unsigned int exec_id;
32 unsigned int hw_id;
88d2ba2e
TU
33 u32 mmio_base;
34 unsigned irq_shift;
35 int (*init_legacy)(struct intel_engine_cs *engine);
36 int (*init_execlists)(struct intel_engine_cs *engine);
37} intel_engines[] = {
38 [RCS] = {
17ab792a 39 .name = "rcs",
5ec2cf7e 40 .hw_id = RCS_HW,
17ab792a 41 .exec_id = I915_EXEC_RENDER,
88d2ba2e
TU
42 .mmio_base = RENDER_RING_BASE,
43 .irq_shift = GEN8_RCS_IRQ_SHIFT,
44 .init_execlists = logical_render_ring_init,
45 .init_legacy = intel_init_render_ring_buffer,
46 },
47 [BCS] = {
17ab792a 48 .name = "bcs",
5ec2cf7e 49 .hw_id = BCS_HW,
17ab792a 50 .exec_id = I915_EXEC_BLT,
88d2ba2e
TU
51 .mmio_base = BLT_RING_BASE,
52 .irq_shift = GEN8_BCS_IRQ_SHIFT,
53 .init_execlists = logical_xcs_ring_init,
54 .init_legacy = intel_init_blt_ring_buffer,
55 },
56 [VCS] = {
17ab792a 57 .name = "vcs",
5ec2cf7e 58 .hw_id = VCS_HW,
17ab792a 59 .exec_id = I915_EXEC_BSD,
88d2ba2e
TU
60 .mmio_base = GEN6_BSD_RING_BASE,
61 .irq_shift = GEN8_VCS1_IRQ_SHIFT,
62 .init_execlists = logical_xcs_ring_init,
63 .init_legacy = intel_init_bsd_ring_buffer,
64 },
65 [VCS2] = {
17ab792a 66 .name = "vcs2",
5ec2cf7e 67 .hw_id = VCS2_HW,
17ab792a 68 .exec_id = I915_EXEC_BSD,
88d2ba2e
TU
69 .mmio_base = GEN8_BSD2_RING_BASE,
70 .irq_shift = GEN8_VCS2_IRQ_SHIFT,
71 .init_execlists = logical_xcs_ring_init,
72 .init_legacy = intel_init_bsd2_ring_buffer,
73 },
74 [VECS] = {
17ab792a 75 .name = "vecs",
5ec2cf7e 76 .hw_id = VECS_HW,
17ab792a 77 .exec_id = I915_EXEC_VEBOX,
88d2ba2e
TU
78 .mmio_base = VEBOX_RING_BASE,
79 .irq_shift = GEN8_VECS_IRQ_SHIFT,
80 .init_execlists = logical_xcs_ring_init,
81 .init_legacy = intel_init_vebox_ring_buffer,
82 },
83};
84
3b3f1650 85static int
88d2ba2e
TU
86intel_engine_setup(struct drm_i915_private *dev_priv,
87 enum intel_engine_id id)
88{
89 const struct engine_info *info = &intel_engines[id];
3b3f1650
AG
90 struct intel_engine_cs *engine;
91
92 GEM_BUG_ON(dev_priv->engine[id]);
93 engine = kzalloc(sizeof(*engine), GFP_KERNEL);
94 if (!engine)
95 return -ENOMEM;
88d2ba2e
TU
96
97 engine->id = id;
98 engine->i915 = dev_priv;
99 engine->name = info->name;
100 engine->exec_id = info->exec_id;
5ec2cf7e 101 engine->hw_id = engine->guc_id = info->hw_id;
88d2ba2e
TU
102 engine->mmio_base = info->mmio_base;
103 engine->irq_shift = info->irq_shift;
104
0de9136d
CW
105 /* Nothing to do here, execute in order of dependencies */
106 engine->schedule = NULL;
107
3fc03069
CD
108 ATOMIC_INIT_NOTIFIER_HEAD(&engine->context_status_notifier);
109
3b3f1650
AG
110 dev_priv->engine[id] = engine;
111 return 0;
88d2ba2e
TU
112}
113
114/**
bb8f0f5a 115 * intel_engines_init_early() - allocate the Engine Command Streamers
bf9e8429 116 * @dev_priv: i915 device private
88d2ba2e
TU
117 *
118 * Return: non-zero if the initialization failed.
119 */
bb8f0f5a 120int intel_engines_init_early(struct drm_i915_private *dev_priv)
88d2ba2e 121{
c1bb1145 122 struct intel_device_info *device_info = mkwrite_device_info(dev_priv);
70006ad6 123 unsigned int ring_mask = INTEL_INFO(dev_priv)->ring_mask;
88d2ba2e 124 unsigned int mask = 0;
3b3f1650
AG
125 struct intel_engine_cs *engine;
126 enum intel_engine_id id;
88d2ba2e 127 unsigned int i;
bb8f0f5a 128 int err;
88d2ba2e 129
70006ad6
TU
130 WARN_ON(ring_mask == 0);
131 WARN_ON(ring_mask &
88d2ba2e
TU
132 GENMASK(sizeof(mask) * BITS_PER_BYTE - 1, I915_NUM_ENGINES));
133
134 for (i = 0; i < ARRAY_SIZE(intel_engines); i++) {
135 if (!HAS_ENGINE(dev_priv, i))
136 continue;
137
bb8f0f5a
CW
138 err = intel_engine_setup(dev_priv, i);
139 if (err)
140 goto cleanup;
141
142 mask |= ENGINE_MASK(i);
143 }
144
145 /*
146 * Catch failures to update intel_engines table when the new engines
147 * are added to the driver by a warning and disabling the forgotten
148 * engines.
149 */
150 if (WARN_ON(mask != ring_mask))
151 device_info->ring_mask = mask;
152
153 device_info->num_rings = hweight32(mask);
154
155 return 0;
156
157cleanup:
158 for_each_engine(engine, dev_priv, id)
159 kfree(engine);
160 return err;
161}
162
163/**
164 * intel_engines_init() - allocate, populate and init the Engine Command Streamers
165 * @dev_priv: i915 device private
166 *
167 * Return: non-zero if the initialization failed.
168 */
169int intel_engines_init(struct drm_i915_private *dev_priv)
170{
171 struct intel_device_info *device_info = mkwrite_device_info(dev_priv);
172 struct intel_engine_cs *engine;
173 enum intel_engine_id id, err_id;
174 unsigned int mask = 0;
175 int err = 0;
176
177 for_each_engine(engine, dev_priv, id) {
178 int (*init)(struct intel_engine_cs *engine);
179
88d2ba2e 180 if (i915.enable_execlists)
bb8f0f5a 181 init = intel_engines[id].init_execlists;
88d2ba2e 182 else
bb8f0f5a
CW
183 init = intel_engines[id].init_legacy;
184 if (!init) {
185 kfree(engine);
186 dev_priv->engine[id] = NULL;
88d2ba2e 187 continue;
bb8f0f5a 188 }
88d2ba2e 189
bb8f0f5a
CW
190 err = init(engine);
191 if (err) {
192 err_id = id;
88d2ba2e 193 goto cleanup;
bb8f0f5a 194 }
88d2ba2e 195
ff44ad51 196 GEM_BUG_ON(!engine->submit_request);
bb8f0f5a 197 mask |= ENGINE_MASK(id);
88d2ba2e
TU
198 }
199
200 /*
201 * Catch failures to update intel_engines table when the new engines
202 * are added to the driver by a warning and disabling the forgotten
203 * engines.
204 */
bb8f0f5a 205 if (WARN_ON(mask != INTEL_INFO(dev_priv)->ring_mask))
c1bb1145
TU
206 device_info->ring_mask = mask;
207
208 device_info->num_rings = hweight32(mask);
88d2ba2e
TU
209
210 return 0;
211
212cleanup:
3b3f1650 213 for_each_engine(engine, dev_priv, id) {
bb8f0f5a
CW
214 if (id >= err_id)
215 kfree(engine);
88d2ba2e 216 else
8ee7c6e2 217 dev_priv->gt.cleanup_engine(engine);
88d2ba2e 218 }
bb8f0f5a 219 return err;
88d2ba2e
TU
220}
221
73cb9701 222void intel_engine_init_global_seqno(struct intel_engine_cs *engine, u32 seqno)
57f275a2
CW
223{
224 struct drm_i915_private *dev_priv = engine->i915;
225
2ca9faa5
CW
226 GEM_BUG_ON(!intel_engine_is_idle(engine));
227
57f275a2
CW
228 /* Our semaphore implementation is strictly monotonic (i.e. we proceed
229 * so long as the semaphore value in the register/page is greater
230 * than the sync value), so whenever we reset the seqno,
231 * so long as we reset the tracking semaphore value to 0, it will
232 * always be before the next request's seqno. If we don't reset
233 * the semaphore value, then when the seqno moves backwards all
234 * future waits will complete instantly (causing rendering corruption).
235 */
236 if (IS_GEN6(dev_priv) || IS_GEN7(dev_priv)) {
237 I915_WRITE(RING_SYNC_0(engine->mmio_base), 0);
238 I915_WRITE(RING_SYNC_1(engine->mmio_base), 0);
239 if (HAS_VEBOX(dev_priv))
240 I915_WRITE(RING_SYNC_2(engine->mmio_base), 0);
241 }
51d545d0
CW
242 if (dev_priv->semaphore) {
243 struct page *page = i915_vma_first_page(dev_priv->semaphore);
244 void *semaphores;
245
246 /* Semaphores are in noncoherent memory, flush to be safe */
24caf655 247 semaphores = kmap_atomic(page);
57f275a2
CW
248 memset(semaphores + GEN8_SEMAPHORE_OFFSET(engine->id, 0),
249 0, I915_NUM_ENGINES * gen8_semaphore_seqno_size);
51d545d0
CW
250 drm_clflush_virt_range(semaphores + GEN8_SEMAPHORE_OFFSET(engine->id, 0),
251 I915_NUM_ENGINES * gen8_semaphore_seqno_size);
24caf655 252 kunmap_atomic(semaphores);
57f275a2 253 }
57f275a2
CW
254
255 intel_write_status_page(engine, I915_GEM_HWS_INDEX, seqno);
14a6bbf9 256 clear_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted);
73cb9701
CW
257
258 GEM_BUG_ON(i915_gem_active_isset(&engine->timeline->last_request));
57f275a2
CW
259 engine->hangcheck.seqno = seqno;
260
261 /* After manually advancing the seqno, fake the interrupt in case
262 * there are any waiters for that seqno.
263 */
264 intel_engine_wakeup(engine);
2ca9faa5
CW
265
266 GEM_BUG_ON(intel_engine_get_seqno(engine) != seqno);
57f275a2
CW
267}
268
73cb9701 269static void intel_engine_init_timeline(struct intel_engine_cs *engine)
dcff85c8 270{
73cb9701 271 engine->timeline = &engine->i915->gt.global_timeline.engine[engine->id];
dcff85c8
CW
272}
273
019bf277
TU
274/**
275 * intel_engines_setup_common - setup engine state not requiring hw access
276 * @engine: Engine to setup.
277 *
278 * Initializes @engine@ structure members shared between legacy and execlists
279 * submission modes which do not require hardware access.
280 *
281 * Typically done early in the submission mode specific engine setup stage.
282 */
283void intel_engine_setup_common(struct intel_engine_cs *engine)
284{
20311bd3
CW
285 engine->execlist_queue = RB_ROOT;
286 engine->execlist_first = NULL;
019bf277 287
73cb9701 288 intel_engine_init_timeline(engine);
019bf277 289 intel_engine_init_hangcheck(engine);
115003e9 290 i915_gem_batch_pool_init(engine, &engine->batch_pool);
7756e454
CW
291
292 intel_engine_init_cmd_parser(engine);
019bf277
TU
293}
294
adc320c4
CW
295int intel_engine_create_scratch(struct intel_engine_cs *engine, int size)
296{
297 struct drm_i915_gem_object *obj;
298 struct i915_vma *vma;
299 int ret;
300
301 WARN_ON(engine->scratch);
302
187685cb 303 obj = i915_gem_object_create_stolen(engine->i915, size);
adc320c4 304 if (!obj)
920cf419 305 obj = i915_gem_object_create_internal(engine->i915, size);
adc320c4
CW
306 if (IS_ERR(obj)) {
307 DRM_ERROR("Failed to allocate scratch page\n");
308 return PTR_ERR(obj);
309 }
310
a01cb37a 311 vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL);
adc320c4
CW
312 if (IS_ERR(vma)) {
313 ret = PTR_ERR(vma);
314 goto err_unref;
315 }
316
317 ret = i915_vma_pin(vma, 0, 4096, PIN_GLOBAL | PIN_HIGH);
318 if (ret)
319 goto err_unref;
320
321 engine->scratch = vma;
bde13ebd
CW
322 DRM_DEBUG_DRIVER("%s pipe control offset: 0x%08x\n",
323 engine->name, i915_ggtt_offset(vma));
adc320c4
CW
324 return 0;
325
326err_unref:
327 i915_gem_object_put(obj);
328 return ret;
329}
330
331static void intel_engine_cleanup_scratch(struct intel_engine_cs *engine)
332{
19880c4a 333 i915_vma_unpin_and_release(&engine->scratch);
adc320c4
CW
334}
335
019bf277
TU
336/**
337 * intel_engines_init_common - initialize cengine state which might require hw access
338 * @engine: Engine to initialize.
339 *
340 * Initializes @engine@ structure members shared between legacy and execlists
341 * submission modes which do require hardware access.
342 *
343 * Typcally done at later stages of submission mode specific engine setup.
344 *
345 * Returns zero on success or an error code on failure.
346 */
347int intel_engine_init_common(struct intel_engine_cs *engine)
348{
349 int ret;
350
ff44ad51
CW
351 engine->set_default_submission(engine);
352
e8a9c58f
CW
353 /* We may need to do things with the shrinker which
354 * require us to immediately switch back to the default
355 * context. This can cause a problem as pinning the
356 * default context also requires GTT space which may not
357 * be available. To avoid this we always pin the default
358 * context.
359 */
360 ret = engine->context_pin(engine, engine->i915->kernel_context);
019bf277
TU
361 if (ret)
362 return ret;
363
e8a9c58f
CW
364 ret = intel_engine_init_breadcrumbs(engine);
365 if (ret)
366 goto err_unpin;
367
4e50f082
CW
368 ret = i915_gem_render_state_init(engine);
369 if (ret)
e8a9c58f 370 goto err_unpin;
4e50f082 371
7756e454 372 return 0;
e8a9c58f
CW
373
374err_unpin:
375 engine->context_unpin(engine, engine->i915->kernel_context);
376 return ret;
019bf277 377}
96a945aa
CW
378
379/**
380 * intel_engines_cleanup_common - cleans up the engine state created by
381 * the common initiailizers.
382 * @engine: Engine to cleanup.
383 *
384 * This cleans up everything created by the common helpers.
385 */
386void intel_engine_cleanup_common(struct intel_engine_cs *engine)
387{
adc320c4
CW
388 intel_engine_cleanup_scratch(engine);
389
4e50f082 390 i915_gem_render_state_fini(engine);
96a945aa 391 intel_engine_fini_breadcrumbs(engine);
7756e454 392 intel_engine_cleanup_cmd_parser(engine);
96a945aa 393 i915_gem_batch_pool_fini(&engine->batch_pool);
e8a9c58f
CW
394
395 engine->context_unpin(engine, engine->i915->kernel_context);
96a945aa 396}
1b36595f
CW
397
398u64 intel_engine_get_active_head(struct intel_engine_cs *engine)
399{
400 struct drm_i915_private *dev_priv = engine->i915;
401 u64 acthd;
402
403 if (INTEL_GEN(dev_priv) >= 8)
404 acthd = I915_READ64_2x32(RING_ACTHD(engine->mmio_base),
405 RING_ACTHD_UDW(engine->mmio_base));
406 else if (INTEL_GEN(dev_priv) >= 4)
407 acthd = I915_READ(RING_ACTHD(engine->mmio_base));
408 else
409 acthd = I915_READ(ACTHD);
410
411 return acthd;
412}
413
414u64 intel_engine_get_last_batch_head(struct intel_engine_cs *engine)
415{
416 struct drm_i915_private *dev_priv = engine->i915;
417 u64 bbaddr;
418
419 if (INTEL_GEN(dev_priv) >= 8)
420 bbaddr = I915_READ64_2x32(RING_BBADDR(engine->mmio_base),
421 RING_BBADDR_UDW(engine->mmio_base));
422 else
423 bbaddr = I915_READ(RING_BBADDR(engine->mmio_base));
424
425 return bbaddr;
426}
0e704476
CW
427
428const char *i915_cache_level_str(struct drm_i915_private *i915, int type)
429{
430 switch (type) {
431 case I915_CACHE_NONE: return " uncached";
432 case I915_CACHE_LLC: return HAS_LLC(i915) ? " LLC" : " snooped";
433 case I915_CACHE_L3_LLC: return " L3+LLC";
434 case I915_CACHE_WT: return " WT";
435 default: return "";
436 }
437}
438
439static inline uint32_t
440read_subslice_reg(struct drm_i915_private *dev_priv, int slice,
441 int subslice, i915_reg_t reg)
442{
443 uint32_t mcr;
444 uint32_t ret;
445 enum forcewake_domains fw_domains;
446
447 fw_domains = intel_uncore_forcewake_for_reg(dev_priv, reg,
448 FW_REG_READ);
449 fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
450 GEN8_MCR_SELECTOR,
451 FW_REG_READ | FW_REG_WRITE);
452
453 spin_lock_irq(&dev_priv->uncore.lock);
454 intel_uncore_forcewake_get__locked(dev_priv, fw_domains);
455
456 mcr = I915_READ_FW(GEN8_MCR_SELECTOR);
457 /*
458 * The HW expects the slice and sublice selectors to be reset to 0
459 * after reading out the registers.
460 */
461 WARN_ON_ONCE(mcr & (GEN8_MCR_SLICE_MASK | GEN8_MCR_SUBSLICE_MASK));
462 mcr &= ~(GEN8_MCR_SLICE_MASK | GEN8_MCR_SUBSLICE_MASK);
463 mcr |= GEN8_MCR_SLICE(slice) | GEN8_MCR_SUBSLICE(subslice);
464 I915_WRITE_FW(GEN8_MCR_SELECTOR, mcr);
465
466 ret = I915_READ_FW(reg);
467
468 mcr &= ~(GEN8_MCR_SLICE_MASK | GEN8_MCR_SUBSLICE_MASK);
469 I915_WRITE_FW(GEN8_MCR_SELECTOR, mcr);
470
471 intel_uncore_forcewake_put__locked(dev_priv, fw_domains);
472 spin_unlock_irq(&dev_priv->uncore.lock);
473
474 return ret;
475}
476
477/* NB: please notice the memset */
478void intel_engine_get_instdone(struct intel_engine_cs *engine,
479 struct intel_instdone *instdone)
480{
481 struct drm_i915_private *dev_priv = engine->i915;
482 u32 mmio_base = engine->mmio_base;
483 int slice;
484 int subslice;
485
486 memset(instdone, 0, sizeof(*instdone));
487
488 switch (INTEL_GEN(dev_priv)) {
489 default:
490 instdone->instdone = I915_READ(RING_INSTDONE(mmio_base));
491
492 if (engine->id != RCS)
493 break;
494
495 instdone->slice_common = I915_READ(GEN7_SC_INSTDONE);
496 for_each_instdone_slice_subslice(dev_priv, slice, subslice) {
497 instdone->sampler[slice][subslice] =
498 read_subslice_reg(dev_priv, slice, subslice,
499 GEN7_SAMPLER_INSTDONE);
500 instdone->row[slice][subslice] =
501 read_subslice_reg(dev_priv, slice, subslice,
502 GEN7_ROW_INSTDONE);
503 }
504 break;
505 case 7:
506 instdone->instdone = I915_READ(RING_INSTDONE(mmio_base));
507
508 if (engine->id != RCS)
509 break;
510
511 instdone->slice_common = I915_READ(GEN7_SC_INSTDONE);
512 instdone->sampler[0][0] = I915_READ(GEN7_SAMPLER_INSTDONE);
513 instdone->row[0][0] = I915_READ(GEN7_ROW_INSTDONE);
514
515 break;
516 case 6:
517 case 5:
518 case 4:
519 instdone->instdone = I915_READ(RING_INSTDONE(mmio_base));
520
521 if (engine->id == RCS)
522 /* HACK: Using the wrong struct member */
523 instdone->slice_common = I915_READ(GEN4_INSTDONE1);
524 break;
525 case 3:
526 case 2:
527 instdone->instdone = I915_READ(GEN2_INSTDONE);
528 break;
529 }
530}
f97fbf96 531
133b4bd7
TU
532static int wa_add(struct drm_i915_private *dev_priv,
533 i915_reg_t addr,
534 const u32 mask, const u32 val)
535{
536 const u32 idx = dev_priv->workarounds.count;
537
538 if (WARN_ON(idx >= I915_MAX_WA_REGS))
539 return -ENOSPC;
540
541 dev_priv->workarounds.reg[idx].addr = addr;
542 dev_priv->workarounds.reg[idx].value = val;
543 dev_priv->workarounds.reg[idx].mask = mask;
544
545 dev_priv->workarounds.count++;
546
547 return 0;
548}
549
550#define WA_REG(addr, mask, val) do { \
551 const int r = wa_add(dev_priv, (addr), (mask), (val)); \
552 if (r) \
553 return r; \
554 } while (0)
555
556#define WA_SET_BIT_MASKED(addr, mask) \
557 WA_REG(addr, (mask), _MASKED_BIT_ENABLE(mask))
558
559#define WA_CLR_BIT_MASKED(addr, mask) \
560 WA_REG(addr, (mask), _MASKED_BIT_DISABLE(mask))
561
562#define WA_SET_FIELD_MASKED(addr, mask, value) \
563 WA_REG(addr, mask, _MASKED_FIELD(mask, value))
564
565#define WA_SET_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) | (mask))
566#define WA_CLR_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) & ~(mask))
567
568#define WA_WRITE(addr, val) WA_REG(addr, 0xffffffff, val)
569
570static int wa_ring_whitelist_reg(struct intel_engine_cs *engine,
571 i915_reg_t reg)
572{
573 struct drm_i915_private *dev_priv = engine->i915;
574 struct i915_workarounds *wa = &dev_priv->workarounds;
575 const uint32_t index = wa->hw_whitelist_count[engine->id];
576
577 if (WARN_ON(index >= RING_MAX_NONPRIV_SLOTS))
578 return -EINVAL;
579
580 WA_WRITE(RING_FORCE_TO_NONPRIV(engine->mmio_base, index),
581 i915_mmio_reg_offset(reg));
582 wa->hw_whitelist_count[engine->id]++;
583
584 return 0;
585}
586
587static int gen8_init_workarounds(struct intel_engine_cs *engine)
588{
589 struct drm_i915_private *dev_priv = engine->i915;
590
591 WA_SET_BIT_MASKED(INSTPM, INSTPM_FORCE_ORDERING);
592
593 /* WaDisableAsyncFlipPerfMode:bdw,chv */
594 WA_SET_BIT_MASKED(MI_MODE, ASYNC_FLIP_PERF_DISABLE);
595
596 /* WaDisablePartialInstShootdown:bdw,chv */
597 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
598 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
599
600 /* Use Force Non-Coherent whenever executing a 3D context. This is a
601 * workaround for for a possible hang in the unlikely event a TLB
602 * invalidation occurs during a PSD flush.
603 */
604 /* WaForceEnableNonCoherent:bdw,chv */
605 /* WaHdcDisableFetchWhenMasked:bdw,chv */
606 WA_SET_BIT_MASKED(HDC_CHICKEN0,
607 HDC_DONOT_FETCH_MEM_WHEN_MASKED |
608 HDC_FORCE_NON_COHERENT);
609
610 /* From the Haswell PRM, Command Reference: Registers, CACHE_MODE_0:
611 * "The Hierarchical Z RAW Stall Optimization allows non-overlapping
612 * polygons in the same 8x4 pixel/sample area to be processed without
613 * stalling waiting for the earlier ones to write to Hierarchical Z
614 * buffer."
615 *
616 * This optimization is off by default for BDW and CHV; turn it on.
617 */
618 WA_CLR_BIT_MASKED(CACHE_MODE_0_GEN7, HIZ_RAW_STALL_OPT_DISABLE);
619
620 /* Wa4x4STCOptimizationDisable:bdw,chv */
621 WA_SET_BIT_MASKED(CACHE_MODE_1, GEN8_4x4_STC_OPTIMIZATION_DISABLE);
622
623 /*
624 * BSpec recommends 8x4 when MSAA is used,
625 * however in practice 16x4 seems fastest.
626 *
627 * Note that PS/WM thread counts depend on the WIZ hashing
628 * disable bit, which we don't touch here, but it's good
629 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
630 */
631 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
632 GEN6_WIZ_HASHING_MASK,
633 GEN6_WIZ_HASHING_16x4);
634
635 return 0;
636}
637
638static int bdw_init_workarounds(struct intel_engine_cs *engine)
639{
640 struct drm_i915_private *dev_priv = engine->i915;
641 int ret;
642
643 ret = gen8_init_workarounds(engine);
644 if (ret)
645 return ret;
646
647 /* WaDisableThreadStallDopClockGating:bdw (pre-production) */
648 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
649
650 /* WaDisableDopClockGating:bdw
651 *
652 * Also see the related UCGTCL1 write in broadwell_init_clock_gating()
653 * to disable EUTC clock gating.
654 */
655 WA_SET_BIT_MASKED(GEN7_ROW_CHICKEN2,
656 DOP_CLOCK_GATING_DISABLE);
657
658 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
659 GEN8_SAMPLER_POWER_BYPASS_DIS);
660
661 WA_SET_BIT_MASKED(HDC_CHICKEN0,
662 /* WaForceContextSaveRestoreNonCoherent:bdw */
663 HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
664 /* WaDisableFenceDestinationToSLM:bdw (pre-prod) */
665 (IS_BDW_GT3(dev_priv) ? HDC_FENCE_DEST_SLM_DISABLE : 0));
666
667 return 0;
668}
669
670static int chv_init_workarounds(struct intel_engine_cs *engine)
671{
672 struct drm_i915_private *dev_priv = engine->i915;
673 int ret;
674
675 ret = gen8_init_workarounds(engine);
676 if (ret)
677 return ret;
678
679 /* WaDisableThreadStallDopClockGating:chv */
680 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
681
682 /* Improve HiZ throughput on CHV. */
683 WA_SET_BIT_MASKED(HIZ_CHICKEN, CHV_HZ_8X8_MODE_IN_1X);
684
685 return 0;
686}
687
688static int gen9_init_workarounds(struct intel_engine_cs *engine)
689{
690 struct drm_i915_private *dev_priv = engine->i915;
691 int ret;
692
693 /* WaConextSwitchWithConcurrentTLBInvalidate:skl,bxt,kbl,glk */
694 I915_WRITE(GEN9_CSFE_CHICKEN1_RCS, _MASKED_BIT_ENABLE(GEN9_PREEMPT_GPGPU_SYNC_SWITCH_DISABLE));
695
696 /* WaEnableLbsSlaRetryTimerDecrement:skl,bxt,kbl,glk */
697 I915_WRITE(BDW_SCRATCH1, I915_READ(BDW_SCRATCH1) |
698 GEN9_LBS_SLA_RETRY_TIMER_DECREMENT_ENABLE);
699
700 /* WaDisableKillLogic:bxt,skl,kbl */
701 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
702 ECOCHK_DIS_TLB);
703
704 /* WaClearFlowControlGpgpuContextSave:skl,bxt,kbl,glk */
705 /* WaDisablePartialInstShootdown:skl,bxt,kbl,glk */
706 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
707 FLOW_CONTROL_ENABLE |
708 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
709
710 /* Syncing dependencies between camera and graphics:skl,bxt,kbl */
711 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
712 GEN9_DISABLE_OCL_OOB_SUPPRESS_LOGIC);
713
714 /* WaDisableDgMirrorFixInHalfSliceChicken5:bxt */
715 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
716 WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
717 GEN9_DG_MIRROR_FIX_ENABLE);
718
719 /* WaSetDisablePixMaskCammingAndRhwoInCommonSliceChicken:bxt */
720 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
721 WA_SET_BIT_MASKED(GEN7_COMMON_SLICE_CHICKEN1,
722 GEN9_RHWO_OPTIMIZATION_DISABLE);
723 /*
724 * WA also requires GEN9_SLICE_COMMON_ECO_CHICKEN0[14:14] to be set
725 * but we do that in per ctx batchbuffer as there is an issue
726 * with this register not getting restored on ctx restore
727 */
728 }
729
730 /* WaEnableSamplerGPGPUPreemptionSupport:skl,bxt,kbl */
731 WA_SET_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN7,
732 GEN9_ENABLE_GPGPU_PREEMPTION);
733
734 /* Wa4x4STCOptimizationDisable:skl,bxt,kbl,glk */
735 /* WaDisablePartialResolveInVc:skl,bxt,kbl */
736 WA_SET_BIT_MASKED(CACHE_MODE_1, (GEN8_4x4_STC_OPTIMIZATION_DISABLE |
737 GEN9_PARTIAL_RESOLVE_IN_VC_DISABLE));
738
739 /* WaCcsTlbPrefetchDisable:skl,bxt,kbl,glk */
740 WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
741 GEN9_CCS_TLB_PREFETCH_ENABLE);
742
743 /* WaDisableMaskBasedCammingInRCC:bxt */
744 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
745 WA_SET_BIT_MASKED(SLICE_ECO_CHICKEN0,
746 PIXEL_MASK_CAMMING_DISABLE);
747
748 /* WaForceContextSaveRestoreNonCoherent:skl,bxt,kbl */
749 WA_SET_BIT_MASKED(HDC_CHICKEN0,
750 HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
751 HDC_FORCE_CSR_NON_COHERENT_OVR_DISABLE);
752
753 /* WaForceEnableNonCoherent and WaDisableHDCInvalidation are
754 * both tied to WaForceContextSaveRestoreNonCoherent
755 * in some hsds for skl. We keep the tie for all gen9. The
756 * documentation is a bit hazy and so we want to get common behaviour,
757 * even though there is no clear evidence we would need both on kbl/bxt.
758 * This area has been source of system hangs so we play it safe
759 * and mimic the skl regardless of what bspec says.
760 *
761 * Use Force Non-Coherent whenever executing a 3D context. This
762 * is a workaround for a possible hang in the unlikely event
763 * a TLB invalidation occurs during a PSD flush.
764 */
765
766 /* WaForceEnableNonCoherent:skl,bxt,kbl */
767 WA_SET_BIT_MASKED(HDC_CHICKEN0,
768 HDC_FORCE_NON_COHERENT);
769
770 /* WaDisableHDCInvalidation:skl,bxt,kbl */
771 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
772 BDW_DISABLE_HDC_INVALIDATION);
773
774 /* WaDisableSamplerPowerBypassForSOPingPong:skl,bxt,kbl */
775 if (IS_SKYLAKE(dev_priv) ||
776 IS_KABYLAKE(dev_priv) ||
777 IS_BXT_REVID(dev_priv, 0, BXT_REVID_B0))
778 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
779 GEN8_SAMPLER_POWER_BYPASS_DIS);
780
781 /* WaDisableSTUnitPowerOptimization:skl,bxt,kbl,glk */
782 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN2, GEN8_ST_PO_DISABLE);
783
784 /* WaOCLCoherentLineFlush:skl,bxt,kbl */
785 I915_WRITE(GEN8_L3SQCREG4, (I915_READ(GEN8_L3SQCREG4) |
786 GEN8_LQSC_FLUSH_COHERENT_LINES));
787
788 /* WaVFEStateAfterPipeControlwithMediaStateClear:skl,bxt,glk */
789 ret = wa_ring_whitelist_reg(engine, GEN9_CTX_PREEMPT_REG);
790 if (ret)
791 return ret;
792
793 /* WaEnablePreemptionGranularityControlByUMD:skl,bxt,kbl */
794 ret= wa_ring_whitelist_reg(engine, GEN8_CS_CHICKEN1);
795 if (ret)
796 return ret;
797
798 /* WaAllowUMDToModifyHDCChicken1:skl,bxt,kbl,glk */
799 ret = wa_ring_whitelist_reg(engine, GEN8_HDC_CHICKEN1);
800 if (ret)
801 return ret;
802
803 return 0;
804}
805
806static int skl_tune_iz_hashing(struct intel_engine_cs *engine)
807{
808 struct drm_i915_private *dev_priv = engine->i915;
809 u8 vals[3] = { 0, 0, 0 };
810 unsigned int i;
811
812 for (i = 0; i < 3; i++) {
813 u8 ss;
814
815 /*
816 * Only consider slices where one, and only one, subslice has 7
817 * EUs
818 */
819 if (!is_power_of_2(INTEL_INFO(dev_priv)->sseu.subslice_7eu[i]))
820 continue;
821
822 /*
823 * subslice_7eu[i] != 0 (because of the check above) and
824 * ss_max == 4 (maximum number of subslices possible per slice)
825 *
826 * -> 0 <= ss <= 3;
827 */
828 ss = ffs(INTEL_INFO(dev_priv)->sseu.subslice_7eu[i]) - 1;
829 vals[i] = 3 - ss;
830 }
831
832 if (vals[0] == 0 && vals[1] == 0 && vals[2] == 0)
833 return 0;
834
835 /* Tune IZ hashing. See intel_device_info_runtime_init() */
836 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
837 GEN9_IZ_HASHING_MASK(2) |
838 GEN9_IZ_HASHING_MASK(1) |
839 GEN9_IZ_HASHING_MASK(0),
840 GEN9_IZ_HASHING(2, vals[2]) |
841 GEN9_IZ_HASHING(1, vals[1]) |
842 GEN9_IZ_HASHING(0, vals[0]));
843
844 return 0;
845}
846
847static int skl_init_workarounds(struct intel_engine_cs *engine)
848{
849 struct drm_i915_private *dev_priv = engine->i915;
850 int ret;
851
852 ret = gen9_init_workarounds(engine);
853 if (ret)
854 return ret;
855
856 /*
857 * Actual WA is to disable percontext preemption granularity control
858 * until D0 which is the default case so this is equivalent to
859 * !WaDisablePerCtxtPreemptionGranularityControl:skl
860 */
861 I915_WRITE(GEN7_FF_SLICE_CS_CHICKEN1,
862 _MASKED_BIT_ENABLE(GEN9_FFSC_PERCTX_PREEMPT_CTRL));
863
864 /* WaEnableGapsTsvCreditFix:skl */
865 I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
866 GEN9_GAPS_TSV_CREDIT_DISABLE));
867
868 /* WaDisableGafsUnitClkGating:skl */
869 WA_SET_BIT(GEN7_UCGCTL4, GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE);
870
871 /* WaInPlaceDecompressionHang:skl */
872 if (IS_SKL_REVID(dev_priv, SKL_REVID_H0, REVID_FOREVER))
873 WA_SET_BIT(GEN9_GAMT_ECO_REG_RW_IA,
874 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS);
875
876 /* WaDisableLSQCROPERFforOCL:skl */
877 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
878 if (ret)
879 return ret;
880
881 return skl_tune_iz_hashing(engine);
882}
883
884static int bxt_init_workarounds(struct intel_engine_cs *engine)
885{
886 struct drm_i915_private *dev_priv = engine->i915;
887 int ret;
888
889 ret = gen9_init_workarounds(engine);
890 if (ret)
891 return ret;
892
893 /* WaStoreMultiplePTEenable:bxt */
894 /* This is a requirement according to Hardware specification */
895 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
896 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_TLBPF);
897
898 /* WaSetClckGatingDisableMedia:bxt */
899 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
900 I915_WRITE(GEN7_MISCCPCTL, (I915_READ(GEN7_MISCCPCTL) &
901 ~GEN8_DOP_CLOCK_GATE_MEDIA_ENABLE));
902 }
903
904 /* WaDisableThreadStallDopClockGating:bxt */
905 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
906 STALL_DOP_GATING_DISABLE);
907
908 /* WaDisablePooledEuLoadBalancingFix:bxt */
909 if (IS_BXT_REVID(dev_priv, BXT_REVID_B0, REVID_FOREVER)) {
910 WA_SET_BIT_MASKED(FF_SLICE_CS_CHICKEN2,
911 GEN9_POOLED_EU_LOAD_BALANCING_FIX_DISABLE);
912 }
913
914 /* WaDisableSbeCacheDispatchPortSharing:bxt */
915 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_B0)) {
916 WA_SET_BIT_MASKED(
917 GEN7_HALF_SLICE_CHICKEN1,
918 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
919 }
920
921 /* WaDisableObjectLevelPreemptionForTrifanOrPolygon:bxt */
922 /* WaDisableObjectLevelPreemptionForInstancedDraw:bxt */
923 /* WaDisableObjectLevelPreemtionForInstanceId:bxt */
924 /* WaDisableLSQCROPERFforOCL:bxt */
925 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
926 ret = wa_ring_whitelist_reg(engine, GEN9_CS_DEBUG_MODE1);
927 if (ret)
928 return ret;
929
930 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
931 if (ret)
932 return ret;
933 }
934
935 /* WaProgramL3SqcReg1DefaultForPerf:bxt */
936 if (IS_BXT_REVID(dev_priv, BXT_REVID_B0, REVID_FOREVER))
937 I915_WRITE(GEN8_L3SQCREG1, L3_GENERAL_PRIO_CREDITS(62) |
938 L3_HIGH_PRIO_CREDITS(2));
939
940 /* WaToEnableHwFixForPushConstHWBug:bxt */
941 if (IS_BXT_REVID(dev_priv, BXT_REVID_C0, REVID_FOREVER))
942 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
943 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
944
945 /* WaInPlaceDecompressionHang:bxt */
946 if (IS_BXT_REVID(dev_priv, BXT_REVID_C0, REVID_FOREVER))
947 WA_SET_BIT(GEN9_GAMT_ECO_REG_RW_IA,
948 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS);
949
950 return 0;
951}
952
953static int kbl_init_workarounds(struct intel_engine_cs *engine)
954{
955 struct drm_i915_private *dev_priv = engine->i915;
956 int ret;
957
958 ret = gen9_init_workarounds(engine);
959 if (ret)
960 return ret;
961
962 /* WaEnableGapsTsvCreditFix:kbl */
963 I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
964 GEN9_GAPS_TSV_CREDIT_DISABLE));
965
966 /* WaDisableDynamicCreditSharing:kbl */
967 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
968 WA_SET_BIT(GAMT_CHKN_BIT_REG,
969 GAMT_CHKN_DISABLE_DYNAMIC_CREDIT_SHARING);
970
971 /* WaDisableFenceDestinationToSLM:kbl (pre-prod) */
972 if (IS_KBL_REVID(dev_priv, KBL_REVID_A0, KBL_REVID_A0))
973 WA_SET_BIT_MASKED(HDC_CHICKEN0,
974 HDC_FENCE_DEST_SLM_DISABLE);
975
976 /* WaToEnableHwFixForPushConstHWBug:kbl */
977 if (IS_KBL_REVID(dev_priv, KBL_REVID_C0, REVID_FOREVER))
978 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
979 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
980
981 /* WaDisableGafsUnitClkGating:kbl */
982 WA_SET_BIT(GEN7_UCGCTL4, GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE);
983
984 /* WaDisableSbeCacheDispatchPortSharing:kbl */
985 WA_SET_BIT_MASKED(
986 GEN7_HALF_SLICE_CHICKEN1,
987 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
988
989 /* WaInPlaceDecompressionHang:kbl */
990 WA_SET_BIT(GEN9_GAMT_ECO_REG_RW_IA,
991 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS);
992
993 /* WaDisableLSQCROPERFforOCL:kbl */
994 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
995 if (ret)
996 return ret;
997
998 return 0;
999}
1000
1001static int glk_init_workarounds(struct intel_engine_cs *engine)
1002{
1003 struct drm_i915_private *dev_priv = engine->i915;
1004 int ret;
1005
1006 ret = gen9_init_workarounds(engine);
1007 if (ret)
1008 return ret;
1009
1010 /* WaToEnableHwFixForPushConstHWBug:glk */
1011 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1012 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
1013
1014 return 0;
1015}
1016
1017int init_workarounds_ring(struct intel_engine_cs *engine)
1018{
1019 struct drm_i915_private *dev_priv = engine->i915;
02e012f1 1020 int err;
133b4bd7
TU
1021
1022 WARN_ON(engine->id != RCS);
1023
1024 dev_priv->workarounds.count = 0;
02e012f1 1025 dev_priv->workarounds.hw_whitelist_count[engine->id] = 0;
133b4bd7
TU
1026
1027 if (IS_BROADWELL(dev_priv))
02e012f1
CW
1028 err = bdw_init_workarounds(engine);
1029 else if (IS_CHERRYVIEW(dev_priv))
1030 err = chv_init_workarounds(engine);
1031 else if (IS_SKYLAKE(dev_priv))
1032 err = skl_init_workarounds(engine);
1033 else if (IS_BROXTON(dev_priv))
1034 err = bxt_init_workarounds(engine);
1035 else if (IS_KABYLAKE(dev_priv))
1036 err = kbl_init_workarounds(engine);
1037 else if (IS_GEMINILAKE(dev_priv))
1038 err = glk_init_workarounds(engine);
1039 else
1040 err = 0;
1041 if (err)
1042 return err;
133b4bd7 1043
02e012f1
CW
1044 DRM_DEBUG_DRIVER("%s: Number of context specific w/a: %d\n",
1045 engine->name, dev_priv->workarounds.count);
133b4bd7
TU
1046 return 0;
1047}
1048
1049int intel_ring_workarounds_emit(struct drm_i915_gem_request *req)
1050{
1051 struct i915_workarounds *w = &req->i915->workarounds;
1052 u32 *cs;
1053 int ret, i;
1054
1055 if (w->count == 0)
1056 return 0;
1057
1058 ret = req->engine->emit_flush(req, EMIT_BARRIER);
1059 if (ret)
1060 return ret;
1061
1062 cs = intel_ring_begin(req, (w->count * 2 + 2));
1063 if (IS_ERR(cs))
1064 return PTR_ERR(cs);
1065
1066 *cs++ = MI_LOAD_REGISTER_IMM(w->count);
1067 for (i = 0; i < w->count; i++) {
1068 *cs++ = i915_mmio_reg_offset(w->reg[i].addr);
1069 *cs++ = w->reg[i].value;
1070 }
1071 *cs++ = MI_NOOP;
1072
1073 intel_ring_advance(req, cs);
1074
1075 ret = req->engine->emit_flush(req, EMIT_BARRIER);
1076 if (ret)
1077 return ret;
1078
133b4bd7
TU
1079 return 0;
1080}
1081
5400367a
CW
1082/**
1083 * intel_engine_is_idle() - Report if the engine has finished process all work
1084 * @engine: the intel_engine_cs
1085 *
1086 * Return true if there are no requests pending, nothing left to be submitted
1087 * to hardware, and that the engine is idle.
1088 */
1089bool intel_engine_is_idle(struct intel_engine_cs *engine)
1090{
1091 struct drm_i915_private *dev_priv = engine->i915;
1092
1093 /* Any inflight/incomplete requests? */
1094 if (!i915_seqno_passed(intel_engine_get_seqno(engine),
1095 intel_engine_last_submit(engine)))
1096 return false;
1097
1098 /* Interrupt/tasklet pending? */
1099 if (test_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted))
1100 return false;
1101
1102 /* Both ports drained, no more ELSP submission? */
1103 if (engine->execlist_port[0].request)
1104 return false;
1105
1106 /* Ring stopped? */
1107 if (INTEL_GEN(dev_priv) > 2 && !(I915_READ_MODE(engine) & MODE_IDLE))
1108 return false;
1109
1110 return true;
1111}
1112
05425249
CW
1113bool intel_engines_are_idle(struct drm_i915_private *dev_priv)
1114{
1115 struct intel_engine_cs *engine;
1116 enum intel_engine_id id;
1117
8490ae20
CW
1118 if (READ_ONCE(dev_priv->gt.active_requests))
1119 return false;
1120
1121 /* If the driver is wedged, HW state may be very inconsistent and
1122 * report that it is still busy, even though we have stopped using it.
1123 */
1124 if (i915_terminally_wedged(&dev_priv->gpu_error))
1125 return true;
1126
05425249
CW
1127 for_each_engine(engine, dev_priv, id) {
1128 if (!intel_engine_is_idle(engine))
1129 return false;
1130 }
1131
1132 return true;
1133}
1134
ff44ad51
CW
1135void intel_engines_reset_default_submission(struct drm_i915_private *i915)
1136{
1137 struct intel_engine_cs *engine;
1138 enum intel_engine_id id;
1139
1140 for_each_engine(engine, i915, id)
1141 engine->set_default_submission(engine);
1142}
1143
f97fbf96
CW
1144#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1145#include "selftests/mock_engine.c"
1146#endif