1 // SPDX-License-Identifier: MIT
3 * Copyright © 2014 Intel Corporation
6 #include "gem/i915_gem_lmem.h"
8 #include "gen8_engine_cs.h"
10 #include "i915_perf.h"
11 #include "intel_engine.h"
12 #include "intel_gpu_commands.h"
14 #include "intel_lrc.h"
15 #include "intel_lrc_reg.h"
16 #include "intel_ring.h"
17 #include "shmem_utils.h"
19 static void set_offsets(u32 *regs,
21 const struct intel_engine_cs *engine,
23 #define NOP(x) (BIT(7) | (x))
24 #define LRI(count, flags) ((flags) << 6 | (count) | BUILD_BUG_ON_ZERO(count >= BIT(6)))
26 #define REG(x) (((x) >> 2) | BUILD_BUG_ON_ZERO(x >= 0x200))
28 (((x) >> 9) | BIT(7) | BUILD_BUG_ON_ZERO(x >= 0x10000)), \
32 const u32 base = engine->mmio_base;
37 if (*data & BIT(7)) { /* skip */
38 count = *data++ & ~BIT(7);
47 *regs = MI_LOAD_REGISTER_IMM(count);
49 *regs |= MI_LRI_FORCE_POSTED;
50 if (GRAPHICS_VER(engine->i915) >= 11)
51 *regs |= MI_LRI_LRM_CS_MMIO;
62 offset |= v & ~BIT(7);
65 regs[0] = base + (offset << 2);
71 /* Close the batch; used mainly by live_lrc_layout() */
72 *regs = MI_BATCH_BUFFER_END;
73 if (GRAPHICS_VER(engine->i915) >= 10)
78 static const u8 gen8_xcs_offsets[] = {
113 static const u8 gen9_xcs_offsets[] = {
197 static const u8 gen12_xcs_offsets[] = {
229 static const u8 gen8_rcs_offsets[] = {
266 static const u8 gen9_rcs_offsets[] = {
350 static const u8 gen11_rcs_offsets[] = {
391 static const u8 gen12_rcs_offsets[] = {
493 static const u8 *reg_offsets(const struct intel_engine_cs *engine)
496 * The gen12+ lists only have the registers we program in the basic
497 * default state. We rely on the context image using relative
498 * addressing to automatic fixup the register state between the
499 * physical engines for virtual engine.
501 GEM_BUG_ON(GRAPHICS_VER(engine->i915) >= 12 &&
502 !intel_engine_has_relative_mmio(engine));
504 if (engine->class == RENDER_CLASS) {
505 if (GRAPHICS_VER(engine->i915) >= 12)
506 return gen12_rcs_offsets;
507 else if (GRAPHICS_VER(engine->i915) >= 11)
508 return gen11_rcs_offsets;
509 else if (GRAPHICS_VER(engine->i915) >= 9)
510 return gen9_rcs_offsets;
512 return gen8_rcs_offsets;
514 if (GRAPHICS_VER(engine->i915) >= 12)
515 return gen12_xcs_offsets;
516 else if (GRAPHICS_VER(engine->i915) >= 9)
517 return gen9_xcs_offsets;
519 return gen8_xcs_offsets;
523 static int lrc_ring_mi_mode(const struct intel_engine_cs *engine)
525 if (GRAPHICS_VER(engine->i915) >= 12)
527 else if (GRAPHICS_VER(engine->i915) >= 9)
529 else if (engine->class == RENDER_CLASS)
535 static int lrc_ring_gpr0(const struct intel_engine_cs *engine)
537 if (GRAPHICS_VER(engine->i915) >= 12)
539 else if (GRAPHICS_VER(engine->i915) >= 9)
541 else if (engine->class == RENDER_CLASS)
547 static int lrc_ring_wa_bb_per_ctx(const struct intel_engine_cs *engine)
549 if (GRAPHICS_VER(engine->i915) >= 12)
551 else if (GRAPHICS_VER(engine->i915) >= 9 || engine->class == RENDER_CLASS)
557 static int lrc_ring_indirect_ptr(const struct intel_engine_cs *engine)
561 x = lrc_ring_wa_bb_per_ctx(engine);
568 static int lrc_ring_indirect_offset(const struct intel_engine_cs *engine)
572 x = lrc_ring_indirect_ptr(engine);
579 static int lrc_ring_cmd_buf_cctl(const struct intel_engine_cs *engine)
581 if (engine->class != RENDER_CLASS)
584 if (GRAPHICS_VER(engine->i915) >= 12)
586 else if (GRAPHICS_VER(engine->i915) >= 11)
593 lrc_ring_indirect_offset_default(const struct intel_engine_cs *engine)
595 switch (GRAPHICS_VER(engine->i915)) {
597 MISSING_CASE(GRAPHICS_VER(engine->i915));
600 return GEN12_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
602 return GEN11_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
604 return GEN10_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
606 return GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
608 return GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
613 lrc_setup_indirect_ctx(u32 *regs,
614 const struct intel_engine_cs *engine,
615 u32 ctx_bb_ggtt_addr,
619 GEM_BUG_ON(!IS_ALIGNED(size, CACHELINE_BYTES));
620 GEM_BUG_ON(lrc_ring_indirect_ptr(engine) == -1);
621 regs[lrc_ring_indirect_ptr(engine) + 1] =
622 ctx_bb_ggtt_addr | (size / CACHELINE_BYTES);
624 GEM_BUG_ON(lrc_ring_indirect_offset(engine) == -1);
625 regs[lrc_ring_indirect_offset(engine) + 1] =
626 lrc_ring_indirect_offset_default(engine) << 6;
629 static void init_common_regs(u32 * const regs,
630 const struct intel_context *ce,
631 const struct intel_engine_cs *engine,
636 ctl = _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH);
637 ctl |= _MASKED_BIT_DISABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT);
639 ctl |= CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT;
640 if (GRAPHICS_VER(engine->i915) < 11)
641 ctl |= _MASKED_BIT_DISABLE(CTX_CTRL_ENGINE_CTX_SAVE_INHIBIT |
642 CTX_CTRL_RS_CTX_ENABLE);
643 regs[CTX_CONTEXT_CONTROL] = ctl;
645 regs[CTX_TIMESTAMP] = ce->runtime.last;
648 static void init_wa_bb_regs(u32 * const regs,
649 const struct intel_engine_cs *engine)
651 const struct i915_ctx_workarounds * const wa_ctx = &engine->wa_ctx;
653 if (wa_ctx->per_ctx.size) {
654 const u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
656 GEM_BUG_ON(lrc_ring_wa_bb_per_ctx(engine) == -1);
657 regs[lrc_ring_wa_bb_per_ctx(engine) + 1] =
658 (ggtt_offset + wa_ctx->per_ctx.offset) | 0x01;
661 if (wa_ctx->indirect_ctx.size) {
662 lrc_setup_indirect_ctx(regs, engine,
663 i915_ggtt_offset(wa_ctx->vma) +
664 wa_ctx->indirect_ctx.offset,
665 wa_ctx->indirect_ctx.size);
669 static void init_ppgtt_regs(u32 *regs, const struct i915_ppgtt *ppgtt)
671 if (i915_vm_is_4lvl(&ppgtt->vm)) {
672 /* 64b PPGTT (48bit canonical)
673 * PDP0_DESCRIPTOR contains the base address to PML4 and
674 * other PDP Descriptors are ignored.
676 ASSIGN_CTX_PML4(ppgtt, regs);
678 ASSIGN_CTX_PDP(ppgtt, regs, 3);
679 ASSIGN_CTX_PDP(ppgtt, regs, 2);
680 ASSIGN_CTX_PDP(ppgtt, regs, 1);
681 ASSIGN_CTX_PDP(ppgtt, regs, 0);
685 static struct i915_ppgtt *vm_alias(struct i915_address_space *vm)
687 if (i915_is_ggtt(vm))
688 return i915_vm_to_ggtt(vm)->alias;
690 return i915_vm_to_ppgtt(vm);
693 static void __reset_stop_ring(u32 *regs, const struct intel_engine_cs *engine)
697 x = lrc_ring_mi_mode(engine);
699 regs[x + 1] &= ~STOP_RING;
700 regs[x + 1] |= STOP_RING << 16;
704 static void __lrc_init_regs(u32 *regs,
705 const struct intel_context *ce,
706 const struct intel_engine_cs *engine,
710 * A context is actually a big batch buffer with several
711 * MI_LOAD_REGISTER_IMM commands followed by (reg, value) pairs. The
712 * values we are setting here are only for the first context restore:
713 * on a subsequent save, the GPU will recreate this batchbuffer with new
714 * values (including all the missing MI_LOAD_REGISTER_IMM commands that
715 * we are not initializing here).
717 * Must keep consistent with virtual_update_register_offsets().
721 memset(regs, 0, PAGE_SIZE);
723 set_offsets(regs, reg_offsets(engine), engine, inhibit);
725 init_common_regs(regs, ce, engine, inhibit);
726 init_ppgtt_regs(regs, vm_alias(ce->vm));
728 init_wa_bb_regs(regs, engine);
730 __reset_stop_ring(regs, engine);
733 void lrc_init_regs(const struct intel_context *ce,
734 const struct intel_engine_cs *engine,
737 __lrc_init_regs(ce->lrc_reg_state, ce, engine, inhibit);
740 void lrc_reset_regs(const struct intel_context *ce,
741 const struct intel_engine_cs *engine)
743 __reset_stop_ring(ce->lrc_reg_state, engine);
747 set_redzone(void *vaddr, const struct intel_engine_cs *engine)
749 if (!IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
752 vaddr += engine->context_size;
754 memset(vaddr, CONTEXT_REDZONE, I915_GTT_PAGE_SIZE);
758 check_redzone(const void *vaddr, const struct intel_engine_cs *engine)
760 if (!IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
763 vaddr += engine->context_size;
765 if (memchr_inv(vaddr, CONTEXT_REDZONE, I915_GTT_PAGE_SIZE))
766 drm_err_once(&engine->i915->drm,
767 "%s context redzone overwritten!\n",
771 void lrc_init_state(struct intel_context *ce,
772 struct intel_engine_cs *engine,
777 set_redzone(state, engine);
779 if (engine->default_state) {
780 shmem_read(engine->default_state, 0,
781 state, engine->context_size);
782 __set_bit(CONTEXT_VALID_BIT, &ce->flags);
786 /* Clear the ppHWSP (inc. per-context counters) */
787 memset(state, 0, PAGE_SIZE);
790 * The second page of the context object contains some registers which
791 * must be set up prior to the first execution.
793 __lrc_init_regs(state + LRC_STATE_OFFSET, ce, engine, inhibit);
796 static struct i915_vma *
797 __lrc_alloc_state(struct intel_context *ce, struct intel_engine_cs *engine)
799 struct drm_i915_gem_object *obj;
800 struct i915_vma *vma;
803 context_size = round_up(engine->context_size, I915_GTT_PAGE_SIZE);
805 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
806 context_size += I915_GTT_PAGE_SIZE; /* for redzone */
808 if (GRAPHICS_VER(engine->i915) == 12) {
809 ce->wa_bb_page = context_size / PAGE_SIZE;
810 context_size += PAGE_SIZE;
813 obj = i915_gem_object_create_lmem(engine->i915, context_size, 0);
815 obj = i915_gem_object_create_shmem(engine->i915, context_size);
817 return ERR_CAST(obj);
819 vma = i915_vma_instance(obj, &engine->gt->ggtt->vm, NULL);
821 i915_gem_object_put(obj);
828 static struct intel_timeline *
829 pinned_timeline(struct intel_context *ce, struct intel_engine_cs *engine)
831 struct intel_timeline *tl = fetch_and_zero(&ce->timeline);
833 return intel_timeline_create_from_engine(engine, page_unmask_bits(tl));
836 int lrc_alloc(struct intel_context *ce, struct intel_engine_cs *engine)
838 struct intel_ring *ring;
839 struct i915_vma *vma;
842 GEM_BUG_ON(ce->state);
844 vma = __lrc_alloc_state(ce, engine);
848 ring = intel_engine_create_ring(engine, ce->ring_size);
854 if (!page_mask_bits(ce->timeline)) {
855 struct intel_timeline *tl;
858 * Use the static global HWSP for the kernel context, and
859 * a dynamically allocated cacheline for everyone else.
861 if (unlikely(ce->timeline))
862 tl = pinned_timeline(ce, engine);
864 tl = intel_timeline_create(engine->gt);
879 intel_ring_put(ring);
885 void lrc_reset(struct intel_context *ce)
887 GEM_BUG_ON(!intel_context_is_pinned(ce));
889 intel_ring_reset(ce->ring, ce->ring->emit);
891 /* Scrub away the garbage */
892 lrc_init_regs(ce, ce->engine, true);
893 ce->lrc.lrca = lrc_update_regs(ce, ce->engine, ce->ring->tail);
897 lrc_pre_pin(struct intel_context *ce,
898 struct intel_engine_cs *engine,
899 struct i915_gem_ww_ctx *ww,
902 GEM_BUG_ON(!ce->state);
903 GEM_BUG_ON(!i915_vma_is_pinned(ce->state));
905 *vaddr = i915_gem_object_pin_map(ce->state->obj,
906 i915_coherent_map_type(ce->engine->i915,
911 return PTR_ERR_OR_ZERO(*vaddr);
915 lrc_pin(struct intel_context *ce,
916 struct intel_engine_cs *engine,
919 ce->lrc_reg_state = vaddr + LRC_STATE_OFFSET;
921 if (!__test_and_set_bit(CONTEXT_INIT_BIT, &ce->flags))
922 lrc_init_state(ce, engine, vaddr);
924 ce->lrc.lrca = lrc_update_regs(ce, engine, ce->ring->tail);
928 void lrc_unpin(struct intel_context *ce)
930 check_redzone((void *)ce->lrc_reg_state - LRC_STATE_OFFSET,
934 void lrc_post_unpin(struct intel_context *ce)
936 i915_gem_object_unpin_map(ce->state->obj);
939 void lrc_fini(struct intel_context *ce)
944 intel_ring_put(fetch_and_zero(&ce->ring));
945 i915_vma_put(fetch_and_zero(&ce->state));
948 void lrc_destroy(struct kref *kref)
950 struct intel_context *ce = container_of(kref, typeof(*ce), ref);
952 GEM_BUG_ON(!i915_active_is_idle(&ce->active));
953 GEM_BUG_ON(intel_context_is_pinned(ce));
957 intel_context_fini(ce);
958 intel_context_free(ce);
962 gen12_emit_timestamp_wa(const struct intel_context *ce, u32 *cs)
964 *cs++ = MI_LOAD_REGISTER_MEM_GEN8 |
965 MI_SRM_LRM_GLOBAL_GTT |
967 *cs++ = i915_mmio_reg_offset(GEN8_RING_CS_GPR(0, 0));
968 *cs++ = i915_ggtt_offset(ce->state) + LRC_STATE_OFFSET +
969 CTX_TIMESTAMP * sizeof(u32);
972 *cs++ = MI_LOAD_REGISTER_REG |
973 MI_LRR_SOURCE_CS_MMIO |
975 *cs++ = i915_mmio_reg_offset(GEN8_RING_CS_GPR(0, 0));
976 *cs++ = i915_mmio_reg_offset(RING_CTX_TIMESTAMP(0));
978 *cs++ = MI_LOAD_REGISTER_REG |
979 MI_LRR_SOURCE_CS_MMIO |
981 *cs++ = i915_mmio_reg_offset(GEN8_RING_CS_GPR(0, 0));
982 *cs++ = i915_mmio_reg_offset(RING_CTX_TIMESTAMP(0));
988 gen12_emit_restore_scratch(const struct intel_context *ce, u32 *cs)
990 GEM_BUG_ON(lrc_ring_gpr0(ce->engine) == -1);
992 *cs++ = MI_LOAD_REGISTER_MEM_GEN8 |
993 MI_SRM_LRM_GLOBAL_GTT |
995 *cs++ = i915_mmio_reg_offset(GEN8_RING_CS_GPR(0, 0));
996 *cs++ = i915_ggtt_offset(ce->state) + LRC_STATE_OFFSET +
997 (lrc_ring_gpr0(ce->engine) + 1) * sizeof(u32);
1004 gen12_emit_cmd_buf_wa(const struct intel_context *ce, u32 *cs)
1006 GEM_BUG_ON(lrc_ring_cmd_buf_cctl(ce->engine) == -1);
1008 *cs++ = MI_LOAD_REGISTER_MEM_GEN8 |
1009 MI_SRM_LRM_GLOBAL_GTT |
1011 *cs++ = i915_mmio_reg_offset(GEN8_RING_CS_GPR(0, 0));
1012 *cs++ = i915_ggtt_offset(ce->state) + LRC_STATE_OFFSET +
1013 (lrc_ring_cmd_buf_cctl(ce->engine) + 1) * sizeof(u32);
1016 *cs++ = MI_LOAD_REGISTER_REG |
1017 MI_LRR_SOURCE_CS_MMIO |
1019 *cs++ = i915_mmio_reg_offset(GEN8_RING_CS_GPR(0, 0));
1020 *cs++ = i915_mmio_reg_offset(RING_CMD_BUF_CCTL(0));
1026 gen12_emit_indirect_ctx_rcs(const struct intel_context *ce, u32 *cs)
1028 cs = gen12_emit_timestamp_wa(ce, cs);
1029 cs = gen12_emit_cmd_buf_wa(ce, cs);
1030 cs = gen12_emit_restore_scratch(ce, cs);
1036 gen12_emit_indirect_ctx_xcs(const struct intel_context *ce, u32 *cs)
1038 cs = gen12_emit_timestamp_wa(ce, cs);
1039 cs = gen12_emit_restore_scratch(ce, cs);
1044 static u32 context_wa_bb_offset(const struct intel_context *ce)
1046 return PAGE_SIZE * ce->wa_bb_page;
1049 static u32 *context_indirect_bb(const struct intel_context *ce)
1053 GEM_BUG_ON(!ce->wa_bb_page);
1055 ptr = ce->lrc_reg_state;
1056 ptr -= LRC_STATE_OFFSET; /* back to start of context image */
1057 ptr += context_wa_bb_offset(ce);
1063 setup_indirect_ctx_bb(const struct intel_context *ce,
1064 const struct intel_engine_cs *engine,
1065 u32 *(*emit)(const struct intel_context *, u32 *))
1067 u32 * const start = context_indirect_bb(ce);
1070 cs = emit(ce, start);
1071 GEM_BUG_ON(cs - start > I915_GTT_PAGE_SIZE / sizeof(*cs));
1072 while ((unsigned long)cs % CACHELINE_BYTES)
1075 lrc_setup_indirect_ctx(ce->lrc_reg_state, engine,
1076 i915_ggtt_offset(ce->state) +
1077 context_wa_bb_offset(ce),
1078 (cs - start) * sizeof(*cs));
1082 * The context descriptor encodes various attributes of a context,
1083 * including its GTT address and some flags. Because it's fairly
1084 * expensive to calculate, we'll just do it once and cache the result,
1085 * which remains valid until the context is unpinned.
1087 * This is what a descriptor looks like, from LSB to MSB::
1089 * bits 0-11: flags, GEN8_CTX_* (cached in ctx->desc_template)
1090 * bits 12-31: LRCA, GTT address of (the HWSP of) this context
1091 * bits 32-52: ctx ID, a globally unique tag (highest bit used by GuC)
1092 * bits 53-54: mbz, reserved for use by hardware
1093 * bits 55-63: group ID, currently unused and set to 0
1095 * Starting from Gen11, the upper dword of the descriptor has a new format:
1097 * bits 32-36: reserved
1098 * bits 37-47: SW context ID
1099 * bits 48:53: engine instance
1100 * bit 54: mbz, reserved for use by hardware
1101 * bits 55-60: SW counter
1102 * bits 61-63: engine class
1104 * engine info, SW context ID and SW counter need to form a unique number
1105 * (Context ID) per lrc.
1107 static u32 lrc_descriptor(const struct intel_context *ce)
1111 desc = INTEL_LEGACY_32B_CONTEXT;
1112 if (i915_vm_is_4lvl(ce->vm))
1113 desc = INTEL_LEGACY_64B_CONTEXT;
1114 desc <<= GEN8_CTX_ADDRESSING_MODE_SHIFT;
1116 desc |= GEN8_CTX_VALID | GEN8_CTX_PRIVILEGE;
1117 if (GRAPHICS_VER(ce->vm->i915) == 8)
1118 desc |= GEN8_CTX_L3LLC_COHERENT;
1120 return i915_ggtt_offset(ce->state) | desc;
1123 u32 lrc_update_regs(const struct intel_context *ce,
1124 const struct intel_engine_cs *engine,
1127 struct intel_ring *ring = ce->ring;
1128 u32 *regs = ce->lrc_reg_state;
1130 GEM_BUG_ON(!intel_ring_offset_valid(ring, head));
1131 GEM_BUG_ON(!intel_ring_offset_valid(ring, ring->tail));
1133 regs[CTX_RING_START] = i915_ggtt_offset(ring->vma);
1134 regs[CTX_RING_HEAD] = head;
1135 regs[CTX_RING_TAIL] = ring->tail;
1136 regs[CTX_RING_CTL] = RING_CTL_SIZE(ring->size) | RING_VALID;
1139 if (engine->class == RENDER_CLASS) {
1140 regs[CTX_R_PWR_CLK_STATE] =
1141 intel_sseu_make_rpcs(engine->gt, &ce->sseu);
1143 i915_oa_init_reg_state(ce, engine);
1146 if (ce->wa_bb_page) {
1147 u32 *(*fn)(const struct intel_context *ce, u32 *cs);
1149 fn = gen12_emit_indirect_ctx_xcs;
1150 if (ce->engine->class == RENDER_CLASS)
1151 fn = gen12_emit_indirect_ctx_rcs;
1153 /* Mutually exclusive wrt to global indirect bb */
1154 GEM_BUG_ON(engine->wa_ctx.indirect_ctx.size);
1155 setup_indirect_ctx_bb(ce, engine, fn);
1158 return lrc_descriptor(ce) | CTX_DESC_FORCE_RESTORE;
1161 void lrc_update_offsets(struct intel_context *ce,
1162 struct intel_engine_cs *engine)
1164 set_offsets(ce->lrc_reg_state, reg_offsets(engine), engine, false);
1167 void lrc_check_regs(const struct intel_context *ce,
1168 const struct intel_engine_cs *engine,
1171 const struct intel_ring *ring = ce->ring;
1172 u32 *regs = ce->lrc_reg_state;
1176 if (regs[CTX_RING_START] != i915_ggtt_offset(ring->vma)) {
1177 pr_err("%s: context submitted with incorrect RING_START [%08x], expected %08x\n",
1179 regs[CTX_RING_START],
1180 i915_ggtt_offset(ring->vma));
1181 regs[CTX_RING_START] = i915_ggtt_offset(ring->vma);
1185 if ((regs[CTX_RING_CTL] & ~(RING_WAIT | RING_WAIT_SEMAPHORE)) !=
1186 (RING_CTL_SIZE(ring->size) | RING_VALID)) {
1187 pr_err("%s: context submitted with incorrect RING_CTL [%08x], expected %08x\n",
1190 (u32)(RING_CTL_SIZE(ring->size) | RING_VALID));
1191 regs[CTX_RING_CTL] = RING_CTL_SIZE(ring->size) | RING_VALID;
1195 x = lrc_ring_mi_mode(engine);
1196 if (x != -1 && regs[x + 1] & (regs[x + 1] >> 16) & STOP_RING) {
1197 pr_err("%s: context submitted with STOP_RING [%08x] in RING_MI_MODE\n",
1198 engine->name, regs[x + 1]);
1199 regs[x + 1] &= ~STOP_RING;
1200 regs[x + 1] |= STOP_RING << 16;
1204 WARN_ONCE(!valid, "Invalid lrc state found %s submission\n", when);
1208 * In this WA we need to set GEN8_L3SQCREG4[21:21] and reset it after
1209 * PIPE_CONTROL instruction. This is required for the flush to happen correctly
1210 * but there is a slight complication as this is applied in WA batch where the
1211 * values are only initialized once so we cannot take register value at the
1212 * beginning and reuse it further; hence we save its value to memory, upload a
1213 * constant value with bit21 set and then we restore it back with the saved value.
1214 * To simplify the WA, a constant value is formed by using the default value
1215 * of this register. This shouldn't be a problem because we are only modifying
1216 * it for a short period and this batch in non-premptible. We can ofcourse
1217 * use additional instructions that read the actual value of the register
1218 * at that time and set our bit of interest but it makes the WA complicated.
1220 * This WA is also required for Gen9 so extracting as a function avoids
1224 gen8_emit_flush_coherentl3_wa(struct intel_engine_cs *engine, u32 *batch)
1226 /* NB no one else is allowed to scribble over scratch + 256! */
1227 *batch++ = MI_STORE_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
1228 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1229 *batch++ = intel_gt_scratch_offset(engine->gt,
1230 INTEL_GT_SCRATCH_FIELD_COHERENTL3_WA);
1233 *batch++ = MI_LOAD_REGISTER_IMM(1);
1234 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1235 *batch++ = 0x40400000 | GEN8_LQSC_FLUSH_COHERENT_LINES;
1237 batch = gen8_emit_pipe_control(batch,
1238 PIPE_CONTROL_CS_STALL |
1239 PIPE_CONTROL_DC_FLUSH_ENABLE,
1242 *batch++ = MI_LOAD_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
1243 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1244 *batch++ = intel_gt_scratch_offset(engine->gt,
1245 INTEL_GT_SCRATCH_FIELD_COHERENTL3_WA);
1252 * Typically we only have one indirect_ctx and per_ctx batch buffer which are
1253 * initialized at the beginning and shared across all contexts but this field
1254 * helps us to have multiple batches at different offsets and select them based
1255 * on a criteria. At the moment this batch always start at the beginning of the page
1256 * and at this point we don't have multiple wa_ctx batch buffers.
1258 * The number of WA applied are not known at the beginning; we use this field
1259 * to return the no of DWORDS written.
1261 * It is to be noted that this batch does not contain MI_BATCH_BUFFER_END
1262 * so it adds NOOPs as padding to make it cacheline aligned.
1263 * MI_BATCH_BUFFER_END will be added to perctx batch and both of them together
1264 * makes a complete batch buffer.
1266 static u32 *gen8_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
1268 /* WaDisableCtxRestoreArbitration:bdw,chv */
1269 *batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
1271 /* WaFlushCoherentL3CacheLinesAtContextSwitch:bdw */
1272 if (IS_BROADWELL(engine->i915))
1273 batch = gen8_emit_flush_coherentl3_wa(engine, batch);
1275 /* WaClearSlmSpaceAtContextSwitch:bdw,chv */
1276 /* Actual scratch location is at 128 bytes offset */
1277 batch = gen8_emit_pipe_control(batch,
1278 PIPE_CONTROL_FLUSH_L3 |
1279 PIPE_CONTROL_STORE_DATA_INDEX |
1280 PIPE_CONTROL_CS_STALL |
1281 PIPE_CONTROL_QW_WRITE,
1282 LRC_PPHWSP_SCRATCH_ADDR);
1284 *batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1286 /* Pad to end of cacheline */
1287 while ((unsigned long)batch % CACHELINE_BYTES)
1291 * MI_BATCH_BUFFER_END is not required in Indirect ctx BB because
1292 * execution depends on the length specified in terms of cache lines
1293 * in the register CTX_RCS_INDIRECT_CTX
1304 static u32 *emit_lri(u32 *batch, const struct lri *lri, unsigned int count)
1306 GEM_BUG_ON(!count || count > 63);
1308 *batch++ = MI_LOAD_REGISTER_IMM(count);
1310 *batch++ = i915_mmio_reg_offset(lri->reg);
1311 *batch++ = lri->value;
1312 } while (lri++, --count);
1318 static u32 *gen9_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
1320 static const struct lri lri[] = {
1321 /* WaDisableGatherAtSetShaderCommonSlice:skl,bxt,kbl,glk */
1323 COMMON_SLICE_CHICKEN2,
1324 __MASKED_FIELD(GEN9_DISABLE_GATHER_AT_SET_SHADER_COMMON_SLICE,
1331 __MASKED_FIELD(FF_SLICE_CHICKEN_CL_PROVOKING_VERTEX_FIX,
1332 FF_SLICE_CHICKEN_CL_PROVOKING_VERTEX_FIX),
1338 __MASKED_FIELD(_3D_CHICKEN_SF_PROVOKING_VERTEX_FIX,
1339 _3D_CHICKEN_SF_PROVOKING_VERTEX_FIX),
1343 *batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
1345 /* WaFlushCoherentL3CacheLinesAtContextSwitch:skl,bxt,glk */
1346 batch = gen8_emit_flush_coherentl3_wa(engine, batch);
1348 /* WaClearSlmSpaceAtContextSwitch:skl,bxt,kbl,glk,cfl */
1349 batch = gen8_emit_pipe_control(batch,
1350 PIPE_CONTROL_FLUSH_L3 |
1351 PIPE_CONTROL_STORE_DATA_INDEX |
1352 PIPE_CONTROL_CS_STALL |
1353 PIPE_CONTROL_QW_WRITE,
1354 LRC_PPHWSP_SCRATCH_ADDR);
1356 batch = emit_lri(batch, lri, ARRAY_SIZE(lri));
1358 /* WaMediaPoolStateCmdInWABB:bxt,glk */
1359 if (HAS_POOLED_EU(engine->i915)) {
1361 * EU pool configuration is setup along with golden context
1362 * during context initialization. This value depends on
1363 * device type (2x6 or 3x6) and needs to be updated based
1364 * on which subslice is disabled especially for 2x6
1365 * devices, however it is safe to load default
1366 * configuration of 3x6 device instead of masking off
1367 * corresponding bits because HW ignores bits of a disabled
1368 * subslice and drops down to appropriate config. Please
1369 * see render_state_setup() in i915_gem_render_state.c for
1370 * possible configurations, to avoid duplication they are
1371 * not shown here again.
1373 *batch++ = GEN9_MEDIA_POOL_STATE;
1374 *batch++ = GEN9_MEDIA_POOL_ENABLE;
1375 *batch++ = 0x00777000;
1381 *batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1383 /* Pad to end of cacheline */
1384 while ((unsigned long)batch % CACHELINE_BYTES)
1391 gen10_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
1396 * WaPipeControlBefore3DStateSamplePattern: cnl
1398 * Ensure the engine is idle prior to programming a
1399 * 3DSTATE_SAMPLE_PATTERN during a context restore.
1401 batch = gen8_emit_pipe_control(batch,
1402 PIPE_CONTROL_CS_STALL,
1405 * WaPipeControlBefore3DStateSamplePattern says we need 4 dwords for
1406 * the PIPE_CONTROL followed by 12 dwords of 0x0, so 16 dwords in
1407 * total. However, a PIPE_CONTROL is 6 dwords long, not 4, which is
1408 * confusing. Since gen8_emit_pipe_control() already advances the
1409 * batch by 6 dwords, we advance the other 10 here, completing a
1410 * cacheline. It's not clear if the workaround requires this padding
1411 * before other commands, or if it's just the regular padding we would
1412 * already have for the workaround bb, so leave it here for now.
1414 for (i = 0; i < 10; i++)
1417 /* Pad to end of cacheline */
1418 while ((unsigned long)batch % CACHELINE_BYTES)
1424 #define CTX_WA_BB_SIZE (PAGE_SIZE)
1426 static int lrc_create_wa_ctx(struct intel_engine_cs *engine)
1428 struct drm_i915_gem_object *obj;
1429 struct i915_vma *vma;
1432 obj = i915_gem_object_create_shmem(engine->i915, CTX_WA_BB_SIZE);
1434 return PTR_ERR(obj);
1436 vma = i915_vma_instance(obj, &engine->gt->ggtt->vm, NULL);
1442 engine->wa_ctx.vma = vma;
1446 i915_gem_object_put(obj);
1450 void lrc_fini_wa_ctx(struct intel_engine_cs *engine)
1452 i915_vma_unpin_and_release(&engine->wa_ctx.vma, 0);
1455 typedef u32 *(*wa_bb_func_t)(struct intel_engine_cs *engine, u32 *batch);
1457 void lrc_init_wa_ctx(struct intel_engine_cs *engine)
1459 struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
1460 struct i915_wa_ctx_bb *wa_bb[] = {
1461 &wa_ctx->indirect_ctx, &wa_ctx->per_ctx
1463 wa_bb_func_t wa_bb_fn[ARRAY_SIZE(wa_bb)];
1464 struct i915_gem_ww_ctx ww;
1465 void *batch, *batch_ptr;
1469 if (engine->class != RENDER_CLASS)
1472 switch (GRAPHICS_VER(engine->i915)) {
1477 wa_bb_fn[0] = gen10_init_indirectctx_bb;
1481 wa_bb_fn[0] = gen9_init_indirectctx_bb;
1485 wa_bb_fn[0] = gen8_init_indirectctx_bb;
1489 MISSING_CASE(GRAPHICS_VER(engine->i915));
1493 err = lrc_create_wa_ctx(engine);
1496 * We continue even if we fail to initialize WA batch
1497 * because we only expect rare glitches but nothing
1498 * critical to prevent us from using GPU
1500 drm_err(&engine->i915->drm,
1501 "Ignoring context switch w/a allocation error:%d\n",
1506 if (!engine->wa_ctx.vma)
1509 i915_gem_ww_ctx_init(&ww, true);
1511 err = i915_gem_object_lock(wa_ctx->vma->obj, &ww);
1513 err = i915_ggtt_pin(wa_ctx->vma, &ww, 0, PIN_HIGH);
1517 batch = i915_gem_object_pin_map(wa_ctx->vma->obj, I915_MAP_WB);
1518 if (IS_ERR(batch)) {
1519 err = PTR_ERR(batch);
1524 * Emit the two workaround batch buffers, recording the offset from the
1525 * start of the workaround batch buffer object for each and their
1529 for (i = 0; i < ARRAY_SIZE(wa_bb_fn); i++) {
1530 wa_bb[i]->offset = batch_ptr - batch;
1531 if (GEM_DEBUG_WARN_ON(!IS_ALIGNED(wa_bb[i]->offset,
1532 CACHELINE_BYTES))) {
1537 batch_ptr = wa_bb_fn[i](engine, batch_ptr);
1538 wa_bb[i]->size = batch_ptr - (batch + wa_bb[i]->offset);
1540 GEM_BUG_ON(batch_ptr - batch > CTX_WA_BB_SIZE);
1542 __i915_gem_object_flush_map(wa_ctx->vma->obj, 0, batch_ptr - batch);
1543 __i915_gem_object_release_map(wa_ctx->vma->obj);
1545 /* Verify that we can handle failure to setup the wa_ctx */
1547 err = i915_inject_probe_error(engine->i915, -ENODEV);
1551 i915_vma_unpin(wa_ctx->vma);
1553 if (err == -EDEADLK) {
1554 err = i915_gem_ww_ctx_backoff(&ww);
1558 i915_gem_ww_ctx_fini(&ww);
1561 i915_vma_put(engine->wa_ctx.vma);
1563 /* Clear all flags to prevent further use */
1564 memset(wa_ctx, 0, sizeof(*wa_ctx));
1568 static void st_update_runtime_underflow(struct intel_context *ce, s32 dt)
1570 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1571 ce->runtime.num_underflow++;
1572 ce->runtime.max_underflow = max_t(u32, ce->runtime.max_underflow, -dt);
1576 void lrc_update_runtime(struct intel_context *ce)
1581 if (intel_context_is_barrier(ce))
1584 old = ce->runtime.last;
1585 ce->runtime.last = lrc_get_runtime(ce);
1586 dt = ce->runtime.last - old;
1588 if (unlikely(dt < 0)) {
1589 CE_TRACE(ce, "runtime underflow: last=%u, new=%u, delta=%d\n",
1590 old, ce->runtime.last, dt);
1591 st_update_runtime_underflow(ce, dt);
1595 ewma_runtime_add(&ce->runtime.avg, dt);
1596 ce->runtime.total += dt;
1599 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1600 #include "selftest_lrc.c"