2 * Copyright © 2016 Intel Corporation
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:
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
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
25 #include <drm/drm_print.h>
28 #include "i915_reset.h"
29 #include "intel_ringbuffer.h"
30 #include "intel_lrc.h"
32 /* 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.
39 #define HSW_CXT_TOTAL_SIZE (17 * PAGE_SIZE)
41 #define DEFAULT_LR_CONTEXT_RENDER_SIZE (22 * PAGE_SIZE)
42 #define GEN8_LR_CONTEXT_RENDER_SIZE (20 * PAGE_SIZE)
43 #define GEN9_LR_CONTEXT_RENDER_SIZE (22 * PAGE_SIZE)
44 #define GEN10_LR_CONTEXT_RENDER_SIZE (18 * PAGE_SIZE)
45 #define GEN11_LR_CONTEXT_RENDER_SIZE (14 * PAGE_SIZE)
47 #define GEN8_LR_CONTEXT_OTHER_SIZE ( 2 * PAGE_SIZE)
49 struct engine_class_info {
51 int (*init_legacy)(struct intel_engine_cs *engine);
52 int (*init_execlists)(struct intel_engine_cs *engine);
57 static const struct engine_class_info intel_engine_classes[] = {
60 .init_execlists = logical_render_ring_init,
61 .init_legacy = intel_init_render_ring_buffer,
62 .uabi_class = I915_ENGINE_CLASS_RENDER,
64 [COPY_ENGINE_CLASS] = {
66 .init_execlists = logical_xcs_ring_init,
67 .init_legacy = intel_init_blt_ring_buffer,
68 .uabi_class = I915_ENGINE_CLASS_COPY,
70 [VIDEO_DECODE_CLASS] = {
72 .init_execlists = logical_xcs_ring_init,
73 .init_legacy = intel_init_bsd_ring_buffer,
74 .uabi_class = I915_ENGINE_CLASS_VIDEO,
76 [VIDEO_ENHANCEMENT_CLASS] = {
78 .init_execlists = logical_xcs_ring_init,
79 .init_legacy = intel_init_vebox_ring_buffer,
80 .uabi_class = I915_ENGINE_CLASS_VIDEO_ENHANCE,
84 #define MAX_MMIO_BASES 3
89 /* mmio bases table *must* be sorted in reverse gen order */
90 struct engine_mmio_base {
93 } mmio_bases[MAX_MMIO_BASES];
96 static const struct engine_info intel_engines[] = {
99 .class = RENDER_CLASS,
102 { .gen = 1, .base = RENDER_RING_BASE }
107 .class = COPY_ENGINE_CLASS,
110 { .gen = 6, .base = BLT_RING_BASE }
115 .class = VIDEO_DECODE_CLASS,
118 { .gen = 11, .base = GEN11_BSD_RING_BASE },
119 { .gen = 6, .base = GEN6_BSD_RING_BASE },
120 { .gen = 4, .base = BSD_RING_BASE }
125 .class = VIDEO_DECODE_CLASS,
128 { .gen = 11, .base = GEN11_BSD2_RING_BASE },
129 { .gen = 8, .base = GEN8_BSD2_RING_BASE }
134 .class = VIDEO_DECODE_CLASS,
137 { .gen = 11, .base = GEN11_BSD3_RING_BASE }
142 .class = VIDEO_DECODE_CLASS,
145 { .gen = 11, .base = GEN11_BSD4_RING_BASE }
150 .class = VIDEO_ENHANCEMENT_CLASS,
153 { .gen = 11, .base = GEN11_VEBOX_RING_BASE },
154 { .gen = 7, .base = VEBOX_RING_BASE }
159 .class = VIDEO_ENHANCEMENT_CLASS,
162 { .gen = 11, .base = GEN11_VEBOX2_RING_BASE }
168 * ___intel_engine_context_size() - return the size of the context for an engine
169 * @dev_priv: i915 device private
170 * @class: engine class
172 * Each engine class may require a different amount of space for a context
175 * Return: size (in bytes) of an engine class specific context image
177 * Note: this size includes the HWSP, which is part of the context image
178 * in LRC mode, but does not include the "shared data page" used with
179 * GuC submission. The caller should account for this if using the GuC.
182 __intel_engine_context_size(struct drm_i915_private *dev_priv, u8 class)
186 BUILD_BUG_ON(I915_GTT_PAGE_SIZE != PAGE_SIZE);
190 switch (INTEL_GEN(dev_priv)) {
192 MISSING_CASE(INTEL_GEN(dev_priv));
193 return DEFAULT_LR_CONTEXT_RENDER_SIZE;
195 return GEN11_LR_CONTEXT_RENDER_SIZE;
197 return GEN10_LR_CONTEXT_RENDER_SIZE;
199 return GEN9_LR_CONTEXT_RENDER_SIZE;
201 return GEN8_LR_CONTEXT_RENDER_SIZE;
203 if (IS_HASWELL(dev_priv))
204 return HSW_CXT_TOTAL_SIZE;
206 cxt_size = I915_READ(GEN7_CXT_SIZE);
207 return round_up(GEN7_CXT_TOTAL_SIZE(cxt_size) * 64,
210 cxt_size = I915_READ(CXT_SIZE);
211 return round_up(GEN6_CXT_TOTAL_SIZE(cxt_size) * 64,
217 /* For the special day when i810 gets merged. */
225 case VIDEO_DECODE_CLASS:
226 case VIDEO_ENHANCEMENT_CLASS:
227 case COPY_ENGINE_CLASS:
228 if (INTEL_GEN(dev_priv) < 8)
230 return GEN8_LR_CONTEXT_OTHER_SIZE;
234 static u32 __engine_mmio_base(struct drm_i915_private *i915,
235 const struct engine_mmio_base *bases)
239 for (i = 0; i < MAX_MMIO_BASES; i++)
240 if (INTEL_GEN(i915) >= bases[i].gen)
243 GEM_BUG_ON(i == MAX_MMIO_BASES);
244 GEM_BUG_ON(!bases[i].base);
246 return bases[i].base;
249 static void __sprint_engine_name(char *name, const struct engine_info *info)
251 WARN_ON(snprintf(name, INTEL_ENGINE_CS_MAX_NAME, "%s%u",
252 intel_engine_classes[info->class].name,
253 info->instance) >= INTEL_ENGINE_CS_MAX_NAME);
256 void intel_engine_set_hwsp_writemask(struct intel_engine_cs *engine, u32 mask)
259 * Though they added more rings on g4x/ilk, they did not add
260 * per-engine HWSTAM until gen6.
262 if (INTEL_GEN(engine->i915) < 6 && engine->class != RENDER_CLASS)
265 if (INTEL_GEN(engine->i915) >= 3)
266 ENGINE_WRITE(engine, RING_HWSTAM, mask);
268 ENGINE_WRITE16(engine, RING_HWSTAM, mask);
271 static void intel_engine_sanitize_mmio(struct intel_engine_cs *engine)
273 /* Mask off all writes into the unknown HWSP */
274 intel_engine_set_hwsp_writemask(engine, ~0u);
278 intel_engine_setup(struct drm_i915_private *dev_priv,
279 enum intel_engine_id id)
281 const struct engine_info *info = &intel_engines[id];
282 struct intel_engine_cs *engine;
284 GEM_BUG_ON(info->class >= ARRAY_SIZE(intel_engine_classes));
286 BUILD_BUG_ON(MAX_ENGINE_CLASS >= BIT(GEN11_ENGINE_CLASS_WIDTH));
287 BUILD_BUG_ON(MAX_ENGINE_INSTANCE >= BIT(GEN11_ENGINE_INSTANCE_WIDTH));
289 if (GEM_DEBUG_WARN_ON(info->class > MAX_ENGINE_CLASS))
292 if (GEM_DEBUG_WARN_ON(info->instance > MAX_ENGINE_INSTANCE))
295 if (GEM_DEBUG_WARN_ON(dev_priv->engine_class[info->class][info->instance]))
298 GEM_BUG_ON(dev_priv->engine[id]);
299 engine = kzalloc(sizeof(*engine), GFP_KERNEL);
303 BUILD_BUG_ON(BITS_PER_TYPE(engine->mask) < I915_NUM_ENGINES);
306 engine->mask = BIT(id);
307 engine->i915 = dev_priv;
308 engine->uncore = &dev_priv->uncore;
309 __sprint_engine_name(engine->name, info);
310 engine->hw_id = engine->guc_id = info->hw_id;
311 engine->mmio_base = __engine_mmio_base(dev_priv, info->mmio_bases);
312 engine->class = info->class;
313 engine->instance = info->instance;
315 engine->uabi_class = intel_engine_classes[info->class].uabi_class;
317 engine->context_size = __intel_engine_context_size(dev_priv,
319 if (WARN_ON(engine->context_size > BIT(20)))
320 engine->context_size = 0;
321 if (engine->context_size)
322 DRIVER_CAPS(dev_priv)->has_logical_contexts = true;
324 /* Nothing to do here, execute in order of dependencies */
325 engine->schedule = NULL;
327 seqlock_init(&engine->stats.lock);
329 ATOMIC_INIT_NOTIFIER_HEAD(&engine->context_status_notifier);
331 /* Scrub mmio state on takeover */
332 intel_engine_sanitize_mmio(engine);
334 dev_priv->engine_class[info->class][info->instance] = engine;
335 dev_priv->engine[id] = engine;
340 * intel_engines_init_mmio() - allocate and prepare the Engine Command Streamers
341 * @dev_priv: i915 device private
343 * Return: non-zero if the initialization failed.
345 int intel_engines_init_mmio(struct drm_i915_private *dev_priv)
347 struct intel_device_info *device_info = mkwrite_device_info(dev_priv);
348 const unsigned int engine_mask = INTEL_INFO(dev_priv)->engine_mask;
349 struct intel_engine_cs *engine;
350 enum intel_engine_id id;
351 unsigned int mask = 0;
355 WARN_ON(engine_mask == 0);
356 WARN_ON(engine_mask &
357 GENMASK(BITS_PER_TYPE(mask) - 1, I915_NUM_ENGINES));
359 if (i915_inject_load_failure())
362 for (i = 0; i < ARRAY_SIZE(intel_engines); i++) {
363 if (!HAS_ENGINE(dev_priv, i))
366 err = intel_engine_setup(dev_priv, i);
374 * Catch failures to update intel_engines table when the new engines
375 * are added to the driver by a warning and disabling the forgotten
378 if (WARN_ON(mask != engine_mask))
379 device_info->engine_mask = mask;
381 /* We always presume we have at least RCS available for later probing */
382 if (WARN_ON(!HAS_ENGINE(dev_priv, RCS0))) {
387 RUNTIME_INFO(dev_priv)->num_engines = hweight32(mask);
389 i915_check_and_clear_faults(dev_priv);
394 for_each_engine(engine, dev_priv, id)
400 * intel_engines_init() - init the Engine Command Streamers
401 * @dev_priv: i915 device private
403 * Return: non-zero if the initialization failed.
405 int intel_engines_init(struct drm_i915_private *dev_priv)
407 struct intel_engine_cs *engine;
408 enum intel_engine_id id, err_id;
411 for_each_engine(engine, dev_priv, id) {
412 const struct engine_class_info *class_info =
413 &intel_engine_classes[engine->class];
414 int (*init)(struct intel_engine_cs *engine);
416 if (HAS_EXECLISTS(dev_priv))
417 init = class_info->init_execlists;
419 init = class_info->init_legacy;
424 if (GEM_DEBUG_WARN_ON(!init))
431 GEM_BUG_ON(!engine->submit_request);
437 for_each_engine(engine, dev_priv, id) {
440 dev_priv->engine[id] = NULL;
442 dev_priv->gt.cleanup_engine(engine);
448 static void intel_engine_init_batch_pool(struct intel_engine_cs *engine)
450 i915_gem_batch_pool_init(&engine->batch_pool, engine);
453 static void intel_engine_init_execlist(struct intel_engine_cs *engine)
455 struct intel_engine_execlists * const execlists = &engine->execlists;
457 execlists->port_mask = 1;
458 GEM_BUG_ON(!is_power_of_2(execlists_num_ports(execlists)));
459 GEM_BUG_ON(execlists_num_ports(execlists) > EXECLIST_MAX_PORTS);
461 execlists->queue_priority_hint = INT_MIN;
462 execlists->queue = RB_ROOT_CACHED;
465 static void cleanup_status_page(struct intel_engine_cs *engine)
467 struct i915_vma *vma;
469 /* Prevent writes into HWSP after returning the page to the system */
470 intel_engine_set_hwsp_writemask(engine, ~0u);
472 vma = fetch_and_zero(&engine->status_page.vma);
476 if (!HWS_NEEDS_PHYSICAL(engine->i915))
479 i915_gem_object_unpin_map(vma->obj);
480 __i915_gem_object_release_unless_active(vma->obj);
483 static int pin_ggtt_status_page(struct intel_engine_cs *engine,
484 struct i915_vma *vma)
489 if (!HAS_LLC(engine->i915))
491 * On g33, we cannot place HWS above 256MiB, so
492 * restrict its pinning to the low mappable arena.
493 * Though this restriction is not documented for
494 * gen4, gen5, or byt, they also behave similarly
495 * and hang if the HWS is placed at the top of the
496 * GTT. To generalise, it appears that all !llc
497 * platforms have issues with us placing the HWS
498 * above the mappable region (even though we never
501 flags |= PIN_MAPPABLE;
505 return i915_vma_pin(vma, 0, 0, flags);
508 static int init_status_page(struct intel_engine_cs *engine)
510 struct drm_i915_gem_object *obj;
511 struct i915_vma *vma;
516 * Though the HWS register does support 36bit addresses, historically
517 * we have had hangs and corruption reported due to wild writes if
518 * the HWS is placed above 4G. We only allow objects to be allocated
519 * in GFP_DMA32 for i965, and no earlier physical address users had
520 * access to more than 4G.
522 obj = i915_gem_object_create_internal(engine->i915, PAGE_SIZE);
524 DRM_ERROR("Failed to allocate status page\n");
528 i915_gem_object_set_cache_coherency(obj, I915_CACHE_LLC);
530 vma = i915_vma_instance(obj, &engine->i915->ggtt.vm, NULL);
536 vaddr = i915_gem_object_pin_map(obj, I915_MAP_WB);
538 ret = PTR_ERR(vaddr);
542 engine->status_page.addr = memset(vaddr, 0, PAGE_SIZE);
543 engine->status_page.vma = vma;
545 if (!HWS_NEEDS_PHYSICAL(engine->i915)) {
546 ret = pin_ggtt_status_page(engine, vma);
554 i915_gem_object_unpin_map(obj);
556 i915_gem_object_put(obj);
561 * intel_engines_setup_common - setup engine state not requiring hw access
562 * @engine: Engine to setup.
564 * Initializes @engine@ structure members shared between legacy and execlists
565 * submission modes which do not require hardware access.
567 * Typically done early in the submission mode specific engine setup stage.
569 int intel_engine_setup_common(struct intel_engine_cs *engine)
573 err = init_status_page(engine);
577 err = i915_timeline_init(engine->i915,
579 engine->status_page.vma);
583 i915_timeline_set_subclass(&engine->timeline, TIMELINE_ENGINE);
585 intel_engine_init_breadcrumbs(engine);
586 intel_engine_init_execlist(engine);
587 intel_engine_init_hangcheck(engine);
588 intel_engine_init_batch_pool(engine);
589 intel_engine_init_cmd_parser(engine);
594 cleanup_status_page(engine);
598 void intel_engines_set_scheduler_caps(struct drm_i915_private *i915)
600 static const struct {
604 #define MAP(x, y) { ilog2(I915_ENGINE_HAS_##x), ilog2(I915_SCHEDULER_CAP_##y) }
605 MAP(PREEMPTION, PREEMPTION),
606 MAP(SEMAPHORES, SEMAPHORES),
609 struct intel_engine_cs *engine;
610 enum intel_engine_id id;
611 u32 enabled, disabled;
615 for_each_engine(engine, i915, id) { /* all engines must agree! */
618 if (engine->schedule)
619 enabled |= (I915_SCHEDULER_CAP_ENABLED |
620 I915_SCHEDULER_CAP_PRIORITY);
622 disabled |= (I915_SCHEDULER_CAP_ENABLED |
623 I915_SCHEDULER_CAP_PRIORITY);
625 for (i = 0; i < ARRAY_SIZE(map); i++) {
626 if (engine->flags & BIT(map[i].engine))
627 enabled |= BIT(map[i].sched);
629 disabled |= BIT(map[i].sched);
633 i915->caps.scheduler = enabled & ~disabled;
634 if (!(i915->caps.scheduler & I915_SCHEDULER_CAP_ENABLED))
635 i915->caps.scheduler = 0;
638 struct measure_breadcrumb {
639 struct i915_request rq;
640 struct i915_timeline timeline;
641 struct intel_ring ring;
645 static int measure_breadcrumb_dw(struct intel_engine_cs *engine)
647 struct measure_breadcrumb *frame;
650 GEM_BUG_ON(!engine->i915->gt.scratch);
652 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
656 if (i915_timeline_init(engine->i915,
658 engine->status_page.vma))
661 INIT_LIST_HEAD(&frame->ring.request_list);
662 frame->ring.timeline = &frame->timeline;
663 frame->ring.vaddr = frame->cs;
664 frame->ring.size = sizeof(frame->cs);
665 frame->ring.effective_size = frame->ring.size;
666 intel_ring_update_space(&frame->ring);
668 frame->rq.i915 = engine->i915;
669 frame->rq.engine = engine;
670 frame->rq.ring = &frame->ring;
671 frame->rq.timeline = &frame->timeline;
673 dw = i915_timeline_pin(&frame->timeline);
677 dw = engine->emit_fini_breadcrumb(&frame->rq, frame->cs) - frame->cs;
679 i915_timeline_unpin(&frame->timeline);
682 i915_timeline_fini(&frame->timeline);
688 static int pin_context(struct i915_gem_context *ctx,
689 struct intel_engine_cs *engine,
690 struct intel_context **out)
692 struct intel_context *ce;
694 ce = intel_context_pin(ctx, engine);
703 * intel_engines_init_common - initialize cengine state which might require hw access
704 * @engine: Engine to initialize.
706 * Initializes @engine@ structure members shared between legacy and execlists
707 * submission modes which do require hardware access.
709 * Typcally done at later stages of submission mode specific engine setup.
711 * Returns zero on success or an error code on failure.
713 int intel_engine_init_common(struct intel_engine_cs *engine)
715 struct drm_i915_private *i915 = engine->i915;
718 /* We may need to do things with the shrinker which
719 * require us to immediately switch back to the default
720 * context. This can cause a problem as pinning the
721 * default context also requires GTT space which may not
722 * be available. To avoid this we always pin the default
725 ret = pin_context(i915->kernel_context, engine,
726 &engine->kernel_context);
731 * Similarly the preempt context must always be available so that
732 * we can interrupt the engine at any time. However, as preemption
733 * is optional, we allow it to fail.
735 if (i915->preempt_context)
736 pin_context(i915->preempt_context, engine,
737 &engine->preempt_context);
739 ret = measure_breadcrumb_dw(engine);
743 engine->emit_fini_breadcrumb_dw = ret;
745 engine->set_default_submission(engine);
750 if (engine->preempt_context)
751 intel_context_unpin(engine->preempt_context);
752 intel_context_unpin(engine->kernel_context);
756 void intel_gt_resume(struct drm_i915_private *i915)
758 struct intel_engine_cs *engine;
759 enum intel_engine_id id;
762 * After resume, we may need to poke into the pinned kernel
763 * contexts to paper over any damage caused by the sudden suspend.
764 * Only the kernel contexts should remain pinned over suspend,
765 * allowing us to fixup the user contexts on their first pin.
767 for_each_engine(engine, i915, id) {
768 struct intel_context *ce;
770 ce = engine->kernel_context;
774 ce = engine->preempt_context;
781 * intel_engines_cleanup_common - cleans up the engine state created by
782 * the common initiailizers.
783 * @engine: Engine to cleanup.
785 * This cleans up everything created by the common helpers.
787 void intel_engine_cleanup_common(struct intel_engine_cs *engine)
789 cleanup_status_page(engine);
791 intel_engine_fini_breadcrumbs(engine);
792 intel_engine_cleanup_cmd_parser(engine);
793 i915_gem_batch_pool_fini(&engine->batch_pool);
795 if (engine->default_state)
796 i915_gem_object_put(engine->default_state);
798 if (engine->preempt_context)
799 intel_context_unpin(engine->preempt_context);
800 intel_context_unpin(engine->kernel_context);
802 i915_timeline_fini(&engine->timeline);
804 intel_wa_list_free(&engine->ctx_wa_list);
805 intel_wa_list_free(&engine->wa_list);
806 intel_wa_list_free(&engine->whitelist);
809 u64 intel_engine_get_active_head(const struct intel_engine_cs *engine)
811 struct drm_i915_private *i915 = engine->i915;
815 if (INTEL_GEN(i915) >= 8)
816 acthd = ENGINE_READ64(engine, RING_ACTHD, RING_ACTHD_UDW);
817 else if (INTEL_GEN(i915) >= 4)
818 acthd = ENGINE_READ(engine, RING_ACTHD);
820 acthd = ENGINE_READ(engine, ACTHD);
825 u64 intel_engine_get_last_batch_head(const struct intel_engine_cs *engine)
829 if (INTEL_GEN(engine->i915) >= 8)
830 bbaddr = ENGINE_READ64(engine, RING_BBADDR, RING_BBADDR_UDW);
832 bbaddr = ENGINE_READ(engine, RING_BBADDR);
837 int intel_engine_stop_cs(struct intel_engine_cs *engine)
839 struct intel_uncore *uncore = engine->uncore;
840 const u32 base = engine->mmio_base;
841 const i915_reg_t mode = RING_MI_MODE(base);
844 if (INTEL_GEN(engine->i915) < 3)
847 GEM_TRACE("%s\n", engine->name);
849 intel_uncore_write_fw(uncore, mode, _MASKED_BIT_ENABLE(STOP_RING));
852 if (__intel_wait_for_register_fw(uncore,
853 mode, MODE_IDLE, MODE_IDLE,
856 GEM_TRACE("%s: timed out on STOP_RING -> IDLE\n", engine->name);
860 /* A final mmio read to let GPU writes be hopefully flushed to memory */
861 intel_uncore_posting_read_fw(uncore, mode);
866 void intel_engine_cancel_stop_cs(struct intel_engine_cs *engine)
868 GEM_TRACE("%s\n", engine->name);
870 ENGINE_WRITE_FW(engine, RING_MI_MODE, _MASKED_BIT_DISABLE(STOP_RING));
873 const char *i915_cache_level_str(struct drm_i915_private *i915, int type)
876 case I915_CACHE_NONE: return " uncached";
877 case I915_CACHE_LLC: return HAS_LLC(i915) ? " LLC" : " snooped";
878 case I915_CACHE_L3_LLC: return " L3+LLC";
879 case I915_CACHE_WT: return " WT";
884 u32 intel_calculate_mcr_s_ss_select(struct drm_i915_private *dev_priv)
886 const struct sseu_dev_info *sseu = &RUNTIME_INFO(dev_priv)->sseu;
888 u32 slice = fls(sseu->slice_mask);
889 u32 subslice = fls(sseu->subslice_mask[slice]);
891 if (IS_GEN(dev_priv, 10))
892 mcr_s_ss_select = GEN8_MCR_SLICE(slice) |
893 GEN8_MCR_SUBSLICE(subslice);
894 else if (INTEL_GEN(dev_priv) >= 11)
895 mcr_s_ss_select = GEN11_MCR_SLICE(slice) |
896 GEN11_MCR_SUBSLICE(subslice);
900 return mcr_s_ss_select;
904 read_subslice_reg(struct drm_i915_private *dev_priv, int slice,
905 int subslice, i915_reg_t reg)
907 struct intel_uncore *uncore = &dev_priv->uncore;
908 u32 mcr_slice_subslice_mask;
909 u32 mcr_slice_subslice_select;
910 u32 default_mcr_s_ss_select;
913 enum forcewake_domains fw_domains;
915 if (INTEL_GEN(dev_priv) >= 11) {
916 mcr_slice_subslice_mask = GEN11_MCR_SLICE_MASK |
917 GEN11_MCR_SUBSLICE_MASK;
918 mcr_slice_subslice_select = GEN11_MCR_SLICE(slice) |
919 GEN11_MCR_SUBSLICE(subslice);
921 mcr_slice_subslice_mask = GEN8_MCR_SLICE_MASK |
922 GEN8_MCR_SUBSLICE_MASK;
923 mcr_slice_subslice_select = GEN8_MCR_SLICE(slice) |
924 GEN8_MCR_SUBSLICE(subslice);
927 default_mcr_s_ss_select = intel_calculate_mcr_s_ss_select(dev_priv);
929 fw_domains = intel_uncore_forcewake_for_reg(uncore, reg,
931 fw_domains |= intel_uncore_forcewake_for_reg(uncore,
933 FW_REG_READ | FW_REG_WRITE);
935 spin_lock_irq(&uncore->lock);
936 intel_uncore_forcewake_get__locked(uncore, fw_domains);
938 mcr = intel_uncore_read_fw(uncore, GEN8_MCR_SELECTOR);
940 WARN_ON_ONCE((mcr & mcr_slice_subslice_mask) !=
941 default_mcr_s_ss_select);
943 mcr &= ~mcr_slice_subslice_mask;
944 mcr |= mcr_slice_subslice_select;
945 intel_uncore_write_fw(uncore, GEN8_MCR_SELECTOR, mcr);
947 ret = intel_uncore_read_fw(uncore, reg);
949 mcr &= ~mcr_slice_subslice_mask;
950 mcr |= default_mcr_s_ss_select;
952 intel_uncore_write_fw(uncore, GEN8_MCR_SELECTOR, mcr);
954 intel_uncore_forcewake_put__locked(uncore, fw_domains);
955 spin_unlock_irq(&uncore->lock);
960 /* NB: please notice the memset */
961 void intel_engine_get_instdone(struct intel_engine_cs *engine,
962 struct intel_instdone *instdone)
964 struct drm_i915_private *dev_priv = engine->i915;
965 struct intel_uncore *uncore = engine->uncore;
966 u32 mmio_base = engine->mmio_base;
970 memset(instdone, 0, sizeof(*instdone));
972 switch (INTEL_GEN(dev_priv)) {
975 intel_uncore_read(uncore, RING_INSTDONE(mmio_base));
977 if (engine->id != RCS0)
980 instdone->slice_common =
981 intel_uncore_read(uncore, GEN7_SC_INSTDONE);
982 for_each_instdone_slice_subslice(dev_priv, slice, subslice) {
983 instdone->sampler[slice][subslice] =
984 read_subslice_reg(dev_priv, slice, subslice,
985 GEN7_SAMPLER_INSTDONE);
986 instdone->row[slice][subslice] =
987 read_subslice_reg(dev_priv, slice, subslice,
993 intel_uncore_read(uncore, RING_INSTDONE(mmio_base));
995 if (engine->id != RCS0)
998 instdone->slice_common =
999 intel_uncore_read(uncore, GEN7_SC_INSTDONE);
1000 instdone->sampler[0][0] =
1001 intel_uncore_read(uncore, GEN7_SAMPLER_INSTDONE);
1002 instdone->row[0][0] =
1003 intel_uncore_read(uncore, GEN7_ROW_INSTDONE);
1009 instdone->instdone =
1010 intel_uncore_read(uncore, RING_INSTDONE(mmio_base));
1011 if (engine->id == RCS0)
1012 /* HACK: Using the wrong struct member */
1013 instdone->slice_common =
1014 intel_uncore_read(uncore, GEN4_INSTDONE1);
1018 instdone->instdone = intel_uncore_read(uncore, GEN2_INSTDONE);
1023 static bool ring_is_idle(struct intel_engine_cs *engine)
1025 struct drm_i915_private *dev_priv = engine->i915;
1026 intel_wakeref_t wakeref;
1029 if (I915_SELFTEST_ONLY(!engine->mmio_base))
1032 /* If the whole device is asleep, the engine must be idle */
1033 wakeref = intel_runtime_pm_get_if_in_use(dev_priv);
1037 /* First check that no commands are left in the ring */
1038 if ((ENGINE_READ(engine, RING_HEAD) & HEAD_ADDR) !=
1039 (ENGINE_READ(engine, RING_TAIL) & TAIL_ADDR))
1042 /* No bit for gen2, so assume the CS parser is idle */
1043 if (INTEL_GEN(dev_priv) > 2 &&
1044 !(ENGINE_READ(engine, RING_MI_MODE) & MODE_IDLE))
1047 intel_runtime_pm_put(dev_priv, wakeref);
1053 * intel_engine_is_idle() - Report if the engine has finished process all work
1054 * @engine: the intel_engine_cs
1056 * Return true if there are no requests pending, nothing left to be submitted
1057 * to hardware, and that the engine is idle.
1059 bool intel_engine_is_idle(struct intel_engine_cs *engine)
1061 /* More white lies, if wedged, hw state is inconsistent */
1062 if (i915_reset_failed(engine->i915))
1065 /* Waiting to drain ELSP? */
1066 if (READ_ONCE(engine->execlists.active)) {
1067 struct tasklet_struct *t = &engine->execlists.tasklet;
1070 if (tasklet_trylock(t)) {
1071 /* Must wait for any GPU reset in progress. */
1072 if (__tasklet_is_enabled(t))
1078 /* Otherwise flush the tasklet if it was on another cpu */
1079 tasklet_unlock_wait(t);
1081 if (READ_ONCE(engine->execlists.active))
1085 /* ELSP is empty, but there are ready requests? E.g. after reset */
1086 if (!RB_EMPTY_ROOT(&engine->execlists.queue.rb_root))
1090 return ring_is_idle(engine);
1093 bool intel_engines_are_idle(struct drm_i915_private *i915)
1095 struct intel_engine_cs *engine;
1096 enum intel_engine_id id;
1099 * If the driver is wedged, HW state may be very inconsistent and
1100 * report that it is still busy, even though we have stopped using it.
1102 if (i915_reset_failed(i915))
1105 /* Already parked (and passed an idleness test); must still be idle */
1106 if (!READ_ONCE(i915->gt.awake))
1109 for_each_engine(engine, i915, id) {
1110 if (!intel_engine_is_idle(engine))
1117 void intel_engines_reset_default_submission(struct drm_i915_private *i915)
1119 struct intel_engine_cs *engine;
1120 enum intel_engine_id id;
1122 for_each_engine(engine, i915, id)
1123 engine->set_default_submission(engine);
1126 static bool reset_engines(struct drm_i915_private *i915)
1128 if (INTEL_INFO(i915)->gpu_reset_clobbers_display)
1131 return intel_gpu_reset(i915, ALL_ENGINES) == 0;
1135 * intel_engines_sanitize: called after the GPU has lost power
1136 * @i915: the i915 device
1137 * @force: ignore a failed reset and sanitize engine state anyway
1139 * Anytime we reset the GPU, either with an explicit GPU reset or through a
1140 * PCI power cycle, the GPU loses state and we must reset our state tracking
1141 * to match. Note that calling intel_engines_sanitize() if the GPU has not
1142 * been reset results in much confusion!
1144 void intel_engines_sanitize(struct drm_i915_private *i915, bool force)
1146 struct intel_engine_cs *engine;
1147 enum intel_engine_id id;
1151 if (!reset_engines(i915) && !force)
1154 for_each_engine(engine, i915, id)
1155 intel_engine_reset(engine, false);
1159 * intel_engines_park: called when the GT is transitioning from busy->idle
1160 * @i915: the i915 device
1162 * The GT is now idle and about to go to sleep (maybe never to wake again?).
1163 * Time for us to tidy and put away our toys (release resources back to the
1166 void intel_engines_park(struct drm_i915_private *i915)
1168 struct intel_engine_cs *engine;
1169 enum intel_engine_id id;
1171 for_each_engine(engine, i915, id) {
1172 /* Flush the residual irq tasklets first. */
1173 intel_engine_disarm_breadcrumbs(engine);
1174 tasklet_kill(&engine->execlists.tasklet);
1177 * We are committed now to parking the engines, make sure there
1178 * will be no more interrupts arriving later and the engines
1181 if (wait_for(intel_engine_is_idle(engine), 10)) {
1182 struct drm_printer p = drm_debug_printer(__func__);
1184 dev_err(i915->drm.dev,
1185 "%s is not idle before parking\n",
1187 intel_engine_dump(engine, &p, NULL);
1190 /* Must be reset upon idling, or we may miss the busy wakeup. */
1191 GEM_BUG_ON(engine->execlists.queue_priority_hint != INT_MIN);
1194 engine->park(engine);
1196 if (engine->pinned_default_state) {
1197 i915_gem_object_unpin_map(engine->default_state);
1198 engine->pinned_default_state = NULL;
1201 i915_gem_batch_pool_fini(&engine->batch_pool);
1202 engine->execlists.no_priolist = false;
1205 i915->gt.active_engines = 0;
1209 * intel_engines_unpark: called when the GT is transitioning from idle->busy
1210 * @i915: the i915 device
1212 * The GT was idle and now about to fire up with some new user requests.
1214 void intel_engines_unpark(struct drm_i915_private *i915)
1216 struct intel_engine_cs *engine;
1217 enum intel_engine_id id;
1219 for_each_engine(engine, i915, id) {
1222 /* Pin the default state for fast resets from atomic context. */
1224 if (engine->default_state)
1225 map = i915_gem_object_pin_map(engine->default_state,
1227 if (!IS_ERR_OR_NULL(map))
1228 engine->pinned_default_state = map;
1231 engine->unpark(engine);
1233 intel_engine_init_hangcheck(engine);
1238 * intel_engine_lost_context: called when the GPU is reset into unknown state
1239 * @engine: the engine
1241 * We have either reset the GPU or otherwise about to lose state tracking of
1242 * the current GPU logical state (e.g. suspend). On next use, it is therefore
1243 * imperative that we make no presumptions about the current state and load
1246 void intel_engine_lost_context(struct intel_engine_cs *engine)
1248 struct intel_context *ce;
1250 lockdep_assert_held(&engine->i915->drm.struct_mutex);
1252 ce = fetch_and_zero(&engine->last_retired_context);
1254 intel_context_unpin(ce);
1257 bool intel_engine_can_store_dword(struct intel_engine_cs *engine)
1259 switch (INTEL_GEN(engine->i915)) {
1261 return false; /* uses physical not virtual addresses */
1263 /* maybe only uses physical not virtual addresses */
1264 return !(IS_I915G(engine->i915) || IS_I915GM(engine->i915));
1266 return engine->class != VIDEO_DECODE_CLASS; /* b0rked */
1272 unsigned int intel_engines_has_context_isolation(struct drm_i915_private *i915)
1274 struct intel_engine_cs *engine;
1275 enum intel_engine_id id;
1279 for_each_engine(engine, i915, id)
1280 if (engine->default_state)
1281 which |= BIT(engine->uabi_class);
1286 static int print_sched_attr(struct drm_i915_private *i915,
1287 const struct i915_sched_attr *attr,
1288 char *buf, int x, int len)
1290 if (attr->priority == I915_PRIORITY_INVALID)
1293 x += snprintf(buf + x, len - x,
1294 " prio=%d", attr->priority);
1299 static void print_request(struct drm_printer *m,
1300 struct i915_request *rq,
1303 const char *name = rq->fence.ops->get_timeline_name(&rq->fence);
1307 x = print_sched_attr(rq->i915, &rq->sched.attr, buf, x, sizeof(buf));
1309 drm_printf(m, "%s %llx:%llx%s%s %s @ %dms: %s\n",
1311 rq->fence.context, rq->fence.seqno,
1312 i915_request_completed(rq) ? "!" :
1313 i915_request_started(rq) ? "*" :
1315 test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
1316 &rq->fence.flags) ? "+" : "",
1318 jiffies_to_msecs(jiffies - rq->emitted_jiffies),
1322 static void hexdump(struct drm_printer *m, const void *buf, size_t len)
1324 const size_t rowsize = 8 * sizeof(u32);
1325 const void *prev = NULL;
1329 for (pos = 0; pos < len; pos += rowsize) {
1332 if (prev && !memcmp(prev, buf + pos, rowsize)) {
1334 drm_printf(m, "*\n");
1340 WARN_ON_ONCE(hex_dump_to_buffer(buf + pos, len - pos,
1341 rowsize, sizeof(u32),
1343 false) >= sizeof(line));
1344 drm_printf(m, "[%04zx] %s\n", pos, line);
1351 static void intel_engine_print_registers(const struct intel_engine_cs *engine,
1352 struct drm_printer *m)
1354 struct drm_i915_private *dev_priv = engine->i915;
1355 const struct intel_engine_execlists * const execlists =
1359 if (engine->id == RCS0 && IS_GEN_RANGE(dev_priv, 4, 7))
1360 drm_printf(m, "\tCCID: 0x%08x\n", ENGINE_READ(engine, CCID));
1361 drm_printf(m, "\tRING_START: 0x%08x\n",
1362 ENGINE_READ(engine, RING_START));
1363 drm_printf(m, "\tRING_HEAD: 0x%08x\n",
1364 ENGINE_READ(engine, RING_HEAD) & HEAD_ADDR);
1365 drm_printf(m, "\tRING_TAIL: 0x%08x\n",
1366 ENGINE_READ(engine, RING_TAIL) & TAIL_ADDR);
1367 drm_printf(m, "\tRING_CTL: 0x%08x%s\n",
1368 ENGINE_READ(engine, RING_CTL),
1369 ENGINE_READ(engine, RING_CTL) & (RING_WAIT | RING_WAIT_SEMAPHORE) ? " [waiting]" : "");
1370 if (INTEL_GEN(engine->i915) > 2) {
1371 drm_printf(m, "\tRING_MODE: 0x%08x%s\n",
1372 ENGINE_READ(engine, RING_MI_MODE),
1373 ENGINE_READ(engine, RING_MI_MODE) & (MODE_IDLE) ? " [idle]" : "");
1376 if (INTEL_GEN(dev_priv) >= 6) {
1377 drm_printf(m, "\tRING_IMR: %08x\n",
1378 ENGINE_READ(engine, RING_IMR));
1381 addr = intel_engine_get_active_head(engine);
1382 drm_printf(m, "\tACTHD: 0x%08x_%08x\n",
1383 upper_32_bits(addr), lower_32_bits(addr));
1384 addr = intel_engine_get_last_batch_head(engine);
1385 drm_printf(m, "\tBBADDR: 0x%08x_%08x\n",
1386 upper_32_bits(addr), lower_32_bits(addr));
1387 if (INTEL_GEN(dev_priv) >= 8)
1388 addr = ENGINE_READ64(engine, RING_DMA_FADD, RING_DMA_FADD_UDW);
1389 else if (INTEL_GEN(dev_priv) >= 4)
1390 addr = ENGINE_READ(engine, RING_DMA_FADD);
1392 addr = ENGINE_READ(engine, DMA_FADD_I8XX);
1393 drm_printf(m, "\tDMA_FADDR: 0x%08x_%08x\n",
1394 upper_32_bits(addr), lower_32_bits(addr));
1395 if (INTEL_GEN(dev_priv) >= 4) {
1396 drm_printf(m, "\tIPEIR: 0x%08x\n",
1397 ENGINE_READ(engine, RING_IPEIR));
1398 drm_printf(m, "\tIPEHR: 0x%08x\n",
1399 ENGINE_READ(engine, RING_IPEHR));
1401 drm_printf(m, "\tIPEIR: 0x%08x\n", ENGINE_READ(engine, IPEIR));
1402 drm_printf(m, "\tIPEHR: 0x%08x\n", ENGINE_READ(engine, IPEHR));
1405 if (HAS_EXECLISTS(dev_priv)) {
1407 &engine->status_page.addr[I915_HWS_CSB_BUF0_INDEX];
1408 const u8 num_entries = execlists->csb_size;
1412 drm_printf(m, "\tExeclist status: 0x%08x %08x, entries %u\n",
1413 ENGINE_READ(engine, RING_EXECLIST_STATUS_LO),
1414 ENGINE_READ(engine, RING_EXECLIST_STATUS_HI),
1417 read = execlists->csb_head;
1418 write = READ_ONCE(*execlists->csb_write);
1420 drm_printf(m, "\tExeclist CSB read %d, write %d, tasklet queued? %s (%s)\n",
1422 yesno(test_bit(TASKLET_STATE_SCHED,
1423 &engine->execlists.tasklet.state)),
1424 enableddisabled(!atomic_read(&engine->execlists.tasklet.count)));
1425 if (read >= num_entries)
1427 if (write >= num_entries)
1430 write += num_entries;
1431 while (read < write) {
1432 idx = ++read % num_entries;
1433 drm_printf(m, "\tExeclist CSB[%d]: 0x%08x, context: %d\n",
1434 idx, hws[idx * 2], hws[idx * 2 + 1]);
1438 for (idx = 0; idx < execlists_num_ports(execlists); idx++) {
1439 struct i915_request *rq;
1442 rq = port_unpack(&execlists->port[idx], &count);
1446 snprintf(hdr, sizeof(hdr),
1447 "\t\tELSP[%d] count=%d, ring:{start:%08x, hwsp:%08x, seqno:%08x}, rq: ",
1449 i915_ggtt_offset(rq->ring->vma),
1450 rq->timeline->hwsp_offset,
1452 print_request(m, rq, hdr);
1454 drm_printf(m, "\t\tELSP[%d] idle\n", idx);
1457 drm_printf(m, "\t\tHW active? 0x%x\n", execlists->active);
1459 } else if (INTEL_GEN(dev_priv) > 6) {
1460 drm_printf(m, "\tPP_DIR_BASE: 0x%08x\n",
1461 ENGINE_READ(engine, RING_PP_DIR_BASE));
1462 drm_printf(m, "\tPP_DIR_BASE_READ: 0x%08x\n",
1463 ENGINE_READ(engine, RING_PP_DIR_BASE_READ));
1464 drm_printf(m, "\tPP_DIR_DCLV: 0x%08x\n",
1465 ENGINE_READ(engine, RING_PP_DIR_DCLV));
1469 static void print_request_ring(struct drm_printer *m, struct i915_request *rq)
1475 "[head %04x, postfix %04x, tail %04x, batch 0x%08x_%08x]:\n",
1476 rq->head, rq->postfix, rq->tail,
1477 rq->batch ? upper_32_bits(rq->batch->node.start) : ~0u,
1478 rq->batch ? lower_32_bits(rq->batch->node.start) : ~0u);
1480 size = rq->tail - rq->head;
1481 if (rq->tail < rq->head)
1482 size += rq->ring->size;
1484 ring = kmalloc(size, GFP_ATOMIC);
1486 const void *vaddr = rq->ring->vaddr;
1487 unsigned int head = rq->head;
1488 unsigned int len = 0;
1490 if (rq->tail < head) {
1491 len = rq->ring->size - head;
1492 memcpy(ring, vaddr + head, len);
1495 memcpy(ring + len, vaddr + head, size - len);
1497 hexdump(m, ring, size);
1502 void intel_engine_dump(struct intel_engine_cs *engine,
1503 struct drm_printer *m,
1504 const char *header, ...)
1506 struct i915_gpu_error * const error = &engine->i915->gpu_error;
1507 struct i915_request *rq;
1508 intel_wakeref_t wakeref;
1513 va_start(ap, header);
1514 drm_vprintf(m, header, &ap);
1518 if (i915_reset_failed(engine->i915))
1519 drm_printf(m, "*** WEDGED ***\n");
1521 drm_printf(m, "\tHangcheck %x:%x [%d ms]\n",
1522 engine->hangcheck.last_seqno,
1523 engine->hangcheck.next_seqno,
1524 jiffies_to_msecs(jiffies - engine->hangcheck.action_timestamp));
1525 drm_printf(m, "\tReset count: %d (global %d)\n",
1526 i915_reset_engine_count(error, engine),
1527 i915_reset_count(error));
1531 drm_printf(m, "\tRequests:\n");
1533 rq = list_first_entry(&engine->timeline.requests,
1534 struct i915_request, link);
1535 if (&rq->link != &engine->timeline.requests)
1536 print_request(m, rq, "\t\tfirst ");
1538 rq = list_last_entry(&engine->timeline.requests,
1539 struct i915_request, link);
1540 if (&rq->link != &engine->timeline.requests)
1541 print_request(m, rq, "\t\tlast ");
1543 rq = intel_engine_find_active_request(engine);
1545 print_request(m, rq, "\t\tactive ");
1547 drm_printf(m, "\t\tring->start: 0x%08x\n",
1548 i915_ggtt_offset(rq->ring->vma));
1549 drm_printf(m, "\t\tring->head: 0x%08x\n",
1551 drm_printf(m, "\t\tring->tail: 0x%08x\n",
1553 drm_printf(m, "\t\tring->emit: 0x%08x\n",
1555 drm_printf(m, "\t\tring->space: 0x%08x\n",
1557 drm_printf(m, "\t\tring->hwsp: 0x%08x\n",
1558 rq->timeline->hwsp_offset);
1560 print_request_ring(m, rq);
1565 wakeref = intel_runtime_pm_get_if_in_use(engine->i915);
1567 intel_engine_print_registers(engine, m);
1568 intel_runtime_pm_put(engine->i915, wakeref);
1570 drm_printf(m, "\tDevice is asleep; skipping register dump\n");
1573 intel_execlists_show_requests(engine, m, print_request, 8);
1575 drm_printf(m, "HWSP:\n");
1576 hexdump(m, engine->status_page.addr, PAGE_SIZE);
1578 drm_printf(m, "Idle? %s\n", yesno(intel_engine_is_idle(engine)));
1580 intel_engine_print_breadcrumbs(engine, m);
1583 static u8 user_class_map[] = {
1584 [I915_ENGINE_CLASS_RENDER] = RENDER_CLASS,
1585 [I915_ENGINE_CLASS_COPY] = COPY_ENGINE_CLASS,
1586 [I915_ENGINE_CLASS_VIDEO] = VIDEO_DECODE_CLASS,
1587 [I915_ENGINE_CLASS_VIDEO_ENHANCE] = VIDEO_ENHANCEMENT_CLASS,
1590 struct intel_engine_cs *
1591 intel_engine_lookup_user(struct drm_i915_private *i915, u8 class, u8 instance)
1593 if (class >= ARRAY_SIZE(user_class_map))
1596 class = user_class_map[class];
1598 GEM_BUG_ON(class > MAX_ENGINE_CLASS);
1600 if (instance > MAX_ENGINE_INSTANCE)
1603 return i915->engine_class[class][instance];
1607 * intel_enable_engine_stats() - Enable engine busy tracking on engine
1608 * @engine: engine to enable stats collection
1610 * Start collecting the engine busyness data for @engine.
1612 * Returns 0 on success or a negative error code.
1614 int intel_enable_engine_stats(struct intel_engine_cs *engine)
1616 struct intel_engine_execlists *execlists = &engine->execlists;
1617 unsigned long flags;
1620 if (!intel_engine_supports_stats(engine))
1623 spin_lock_irqsave(&engine->timeline.lock, flags);
1624 write_seqlock(&engine->stats.lock);
1626 if (unlikely(engine->stats.enabled == ~0)) {
1631 if (engine->stats.enabled++ == 0) {
1632 const struct execlist_port *port = execlists->port;
1633 unsigned int num_ports = execlists_num_ports(execlists);
1635 engine->stats.enabled_at = ktime_get();
1637 /* XXX submission method oblivious? */
1638 while (num_ports-- && port_isset(port)) {
1639 engine->stats.active++;
1643 if (engine->stats.active)
1644 engine->stats.start = engine->stats.enabled_at;
1648 write_sequnlock(&engine->stats.lock);
1649 spin_unlock_irqrestore(&engine->timeline.lock, flags);
1654 static ktime_t __intel_engine_get_busy_time(struct intel_engine_cs *engine)
1656 ktime_t total = engine->stats.total;
1659 * If the engine is executing something at the moment
1660 * add it to the total.
1662 if (engine->stats.active)
1663 total = ktime_add(total,
1664 ktime_sub(ktime_get(), engine->stats.start));
1670 * intel_engine_get_busy_time() - Return current accumulated engine busyness
1671 * @engine: engine to report on
1673 * Returns accumulated time @engine was busy since engine stats were enabled.
1675 ktime_t intel_engine_get_busy_time(struct intel_engine_cs *engine)
1681 seq = read_seqbegin(&engine->stats.lock);
1682 total = __intel_engine_get_busy_time(engine);
1683 } while (read_seqretry(&engine->stats.lock, seq));
1689 * intel_disable_engine_stats() - Disable engine busy tracking on engine
1690 * @engine: engine to disable stats collection
1692 * Stops collecting the engine busyness data for @engine.
1694 void intel_disable_engine_stats(struct intel_engine_cs *engine)
1696 unsigned long flags;
1698 if (!intel_engine_supports_stats(engine))
1701 write_seqlock_irqsave(&engine->stats.lock, flags);
1702 WARN_ON_ONCE(engine->stats.enabled == 0);
1703 if (--engine->stats.enabled == 0) {
1704 engine->stats.total = __intel_engine_get_busy_time(engine);
1705 engine->stats.active = 0;
1707 write_sequnlock_irqrestore(&engine->stats.lock, flags);
1710 static bool match_ring(struct i915_request *rq)
1712 u32 ring = ENGINE_READ(rq->engine, RING_START);
1714 return ring == i915_ggtt_offset(rq->ring->vma);
1717 struct i915_request *
1718 intel_engine_find_active_request(struct intel_engine_cs *engine)
1720 struct i915_request *request, *active = NULL;
1721 unsigned long flags;
1724 * We are called by the error capture, reset and to dump engine
1725 * state at random points in time. In particular, note that neither is
1726 * crucially ordered with an interrupt. After a hang, the GPU is dead
1727 * and we assume that no more writes can happen (we waited long enough
1728 * for all writes that were in transaction to be flushed) - adding an
1729 * extra delay for a recent interrupt is pointless. Hence, we do
1730 * not need an engine->irq_seqno_barrier() before the seqno reads.
1731 * At all other times, we must assume the GPU is still running, but
1732 * we only care about the snapshot of this moment.
1734 spin_lock_irqsave(&engine->timeline.lock, flags);
1735 list_for_each_entry(request, &engine->timeline.requests, link) {
1736 if (i915_request_completed(request))
1739 if (!i915_request_started(request))
1742 /* More than one preemptible request may match! */
1743 if (!match_ring(request))
1749 spin_unlock_irqrestore(&engine->timeline.lock, flags);
1754 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1755 #include "selftests/mock_engine.c"
1756 #include "selftests/intel_engine_cs.c"