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_engine_regs.h"
13 #include "intel_gpu_commands.h"
15 #include "intel_lrc.h"
16 #include "intel_lrc_reg.h"
17 #include "intel_ring.h"
18 #include "shmem_utils.h"
20 static void set_offsets(u32 *regs,
22 const struct intel_engine_cs *engine,
24 #define NOP(x) (BIT(7) | (x))
25 #define LRI(count, flags) ((flags) << 6 | (count) | BUILD_BUG_ON_ZERO(count >= BIT(6)))
27 #define REG(x) (((x) >> 2) | BUILD_BUG_ON_ZERO(x >= 0x200))
29 (((x) >> 9) | BIT(7) | BUILD_BUG_ON_ZERO(x >= 0x10000)), \
33 const u32 base = engine->mmio_base;
38 if (*data & BIT(7)) { /* skip */
39 count = *data++ & ~BIT(7);
48 *regs = MI_LOAD_REGISTER_IMM(count);
50 *regs |= MI_LRI_FORCE_POSTED;
51 if (GRAPHICS_VER(engine->i915) >= 11)
52 *regs |= MI_LRI_LRM_CS_MMIO;
63 offset |= v & ~BIT(7);
66 regs[0] = base + (offset << 2);
72 /* Close the batch; used mainly by live_lrc_layout() */
73 *regs = MI_BATCH_BUFFER_END;
74 if (GRAPHICS_VER(engine->i915) >= 11)
79 static const u8 gen8_xcs_offsets[] = {
114 static const u8 gen9_xcs_offsets[] = {
198 static const u8 gen12_xcs_offsets[] = {
230 static const u8 dg2_xcs_offsets[] = {
264 static const u8 gen8_rcs_offsets[] = {
301 static const u8 gen9_rcs_offsets[] = {
385 static const u8 gen11_rcs_offsets[] = {
426 static const u8 gen12_rcs_offsets[] = {
522 static const u8 xehp_rcs_offsets[] = {
563 static const u8 dg2_rcs_offsets[] = {
612 static const u8 *reg_offsets(const struct intel_engine_cs *engine)
615 * The gen12+ lists only have the registers we program in the basic
616 * default state. We rely on the context image using relative
617 * addressing to automatic fixup the register state between the
618 * physical engines for virtual engine.
620 GEM_BUG_ON(GRAPHICS_VER(engine->i915) >= 12 &&
621 !intel_engine_has_relative_mmio(engine));
623 if (engine->class == RENDER_CLASS) {
624 if (GRAPHICS_VER_FULL(engine->i915) >= IP_VER(12, 55))
625 return dg2_rcs_offsets;
626 else if (GRAPHICS_VER_FULL(engine->i915) >= IP_VER(12, 50))
627 return xehp_rcs_offsets;
628 else if (GRAPHICS_VER(engine->i915) >= 12)
629 return gen12_rcs_offsets;
630 else if (GRAPHICS_VER(engine->i915) >= 11)
631 return gen11_rcs_offsets;
632 else if (GRAPHICS_VER(engine->i915) >= 9)
633 return gen9_rcs_offsets;
635 return gen8_rcs_offsets;
637 if (GRAPHICS_VER_FULL(engine->i915) >= IP_VER(12, 55))
638 return dg2_xcs_offsets;
639 else if (GRAPHICS_VER(engine->i915) >= 12)
640 return gen12_xcs_offsets;
641 else if (GRAPHICS_VER(engine->i915) >= 9)
642 return gen9_xcs_offsets;
644 return gen8_xcs_offsets;
648 static int lrc_ring_mi_mode(const struct intel_engine_cs *engine)
650 if (GRAPHICS_VER_FULL(engine->i915) >= IP_VER(12, 50))
652 else if (GRAPHICS_VER(engine->i915) >= 12)
654 else if (GRAPHICS_VER(engine->i915) >= 9)
656 else if (engine->class == RENDER_CLASS)
662 static int lrc_ring_gpr0(const struct intel_engine_cs *engine)
664 if (GRAPHICS_VER_FULL(engine->i915) >= IP_VER(12, 50))
666 else if (GRAPHICS_VER(engine->i915) >= 12)
668 else if (GRAPHICS_VER(engine->i915) >= 9)
670 else if (engine->class == RENDER_CLASS)
676 static int lrc_ring_wa_bb_per_ctx(const struct intel_engine_cs *engine)
678 if (GRAPHICS_VER(engine->i915) >= 12)
680 else if (GRAPHICS_VER(engine->i915) >= 9 || engine->class == RENDER_CLASS)
686 static int lrc_ring_indirect_ptr(const struct intel_engine_cs *engine)
690 x = lrc_ring_wa_bb_per_ctx(engine);
697 static int lrc_ring_indirect_offset(const struct intel_engine_cs *engine)
701 x = lrc_ring_indirect_ptr(engine);
708 static int lrc_ring_cmd_buf_cctl(const struct intel_engine_cs *engine)
711 if (GRAPHICS_VER_FULL(engine->i915) >= IP_VER(12, 50))
713 * Note that the CSFE context has a dummy slot for CMD_BUF_CCTL
714 * simply to match the RCS context image layout.
717 else if (engine->class != RENDER_CLASS)
719 else if (GRAPHICS_VER(engine->i915) >= 12)
721 else if (GRAPHICS_VER(engine->i915) >= 11)
728 lrc_ring_indirect_offset_default(const struct intel_engine_cs *engine)
730 switch (GRAPHICS_VER(engine->i915)) {
732 MISSING_CASE(GRAPHICS_VER(engine->i915));
735 return GEN12_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
737 return GEN11_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
739 return GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
741 return GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
746 lrc_setup_indirect_ctx(u32 *regs,
747 const struct intel_engine_cs *engine,
748 u32 ctx_bb_ggtt_addr,
752 GEM_BUG_ON(!IS_ALIGNED(size, CACHELINE_BYTES));
753 GEM_BUG_ON(lrc_ring_indirect_ptr(engine) == -1);
754 regs[lrc_ring_indirect_ptr(engine) + 1] =
755 ctx_bb_ggtt_addr | (size / CACHELINE_BYTES);
757 GEM_BUG_ON(lrc_ring_indirect_offset(engine) == -1);
758 regs[lrc_ring_indirect_offset(engine) + 1] =
759 lrc_ring_indirect_offset_default(engine) << 6;
762 static void init_common_regs(u32 * const regs,
763 const struct intel_context *ce,
764 const struct intel_engine_cs *engine,
769 ctl = _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH);
770 ctl |= _MASKED_BIT_DISABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT);
772 ctl |= CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT;
773 if (GRAPHICS_VER(engine->i915) < 11)
774 ctl |= _MASKED_BIT_DISABLE(CTX_CTRL_ENGINE_CTX_SAVE_INHIBIT |
775 CTX_CTRL_RS_CTX_ENABLE);
776 regs[CTX_CONTEXT_CONTROL] = ctl;
778 regs[CTX_TIMESTAMP] = ce->runtime.last;
781 static void init_wa_bb_regs(u32 * const regs,
782 const struct intel_engine_cs *engine)
784 const struct i915_ctx_workarounds * const wa_ctx = &engine->wa_ctx;
786 if (wa_ctx->per_ctx.size) {
787 const u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
789 GEM_BUG_ON(lrc_ring_wa_bb_per_ctx(engine) == -1);
790 regs[lrc_ring_wa_bb_per_ctx(engine) + 1] =
791 (ggtt_offset + wa_ctx->per_ctx.offset) | 0x01;
794 if (wa_ctx->indirect_ctx.size) {
795 lrc_setup_indirect_ctx(regs, engine,
796 i915_ggtt_offset(wa_ctx->vma) +
797 wa_ctx->indirect_ctx.offset,
798 wa_ctx->indirect_ctx.size);
802 static void init_ppgtt_regs(u32 *regs, const struct i915_ppgtt *ppgtt)
804 if (i915_vm_is_4lvl(&ppgtt->vm)) {
805 /* 64b PPGTT (48bit canonical)
806 * PDP0_DESCRIPTOR contains the base address to PML4 and
807 * other PDP Descriptors are ignored.
809 ASSIGN_CTX_PML4(ppgtt, regs);
811 ASSIGN_CTX_PDP(ppgtt, regs, 3);
812 ASSIGN_CTX_PDP(ppgtt, regs, 2);
813 ASSIGN_CTX_PDP(ppgtt, regs, 1);
814 ASSIGN_CTX_PDP(ppgtt, regs, 0);
818 static struct i915_ppgtt *vm_alias(struct i915_address_space *vm)
820 if (i915_is_ggtt(vm))
821 return i915_vm_to_ggtt(vm)->alias;
823 return i915_vm_to_ppgtt(vm);
826 static void __reset_stop_ring(u32 *regs, const struct intel_engine_cs *engine)
830 x = lrc_ring_mi_mode(engine);
832 regs[x + 1] &= ~STOP_RING;
833 regs[x + 1] |= STOP_RING << 16;
837 static void __lrc_init_regs(u32 *regs,
838 const struct intel_context *ce,
839 const struct intel_engine_cs *engine,
843 * A context is actually a big batch buffer with several
844 * MI_LOAD_REGISTER_IMM commands followed by (reg, value) pairs. The
845 * values we are setting here are only for the first context restore:
846 * on a subsequent save, the GPU will recreate this batchbuffer with new
847 * values (including all the missing MI_LOAD_REGISTER_IMM commands that
848 * we are not initializing here).
850 * Must keep consistent with virtual_update_register_offsets().
854 memset(regs, 0, PAGE_SIZE);
856 set_offsets(regs, reg_offsets(engine), engine, inhibit);
858 init_common_regs(regs, ce, engine, inhibit);
859 init_ppgtt_regs(regs, vm_alias(ce->vm));
861 init_wa_bb_regs(regs, engine);
863 __reset_stop_ring(regs, engine);
866 void lrc_init_regs(const struct intel_context *ce,
867 const struct intel_engine_cs *engine,
870 __lrc_init_regs(ce->lrc_reg_state, ce, engine, inhibit);
873 void lrc_reset_regs(const struct intel_context *ce,
874 const struct intel_engine_cs *engine)
876 __reset_stop_ring(ce->lrc_reg_state, engine);
880 set_redzone(void *vaddr, const struct intel_engine_cs *engine)
882 if (!IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
885 vaddr += engine->context_size;
887 memset(vaddr, CONTEXT_REDZONE, I915_GTT_PAGE_SIZE);
891 check_redzone(const void *vaddr, const struct intel_engine_cs *engine)
893 if (!IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
896 vaddr += engine->context_size;
898 if (memchr_inv(vaddr, CONTEXT_REDZONE, I915_GTT_PAGE_SIZE))
899 drm_err_once(&engine->i915->drm,
900 "%s context redzone overwritten!\n",
904 void lrc_init_state(struct intel_context *ce,
905 struct intel_engine_cs *engine,
910 set_redzone(state, engine);
912 if (engine->default_state) {
913 shmem_read(engine->default_state, 0,
914 state, engine->context_size);
915 __set_bit(CONTEXT_VALID_BIT, &ce->flags);
919 /* Clear the ppHWSP (inc. per-context counters) */
920 memset(state, 0, PAGE_SIZE);
923 * The second page of the context object contains some registers which
924 * must be set up prior to the first execution.
926 __lrc_init_regs(state + LRC_STATE_OFFSET, ce, engine, inhibit);
929 static struct i915_vma *
930 __lrc_alloc_state(struct intel_context *ce, struct intel_engine_cs *engine)
932 struct drm_i915_gem_object *obj;
933 struct i915_vma *vma;
936 context_size = round_up(engine->context_size, I915_GTT_PAGE_SIZE);
938 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
939 context_size += I915_GTT_PAGE_SIZE; /* for redzone */
941 if (GRAPHICS_VER(engine->i915) == 12) {
942 ce->wa_bb_page = context_size / PAGE_SIZE;
943 context_size += PAGE_SIZE;
946 if (intel_context_is_parent(ce) && intel_engine_uses_guc(engine)) {
947 ce->parallel.guc.parent_page = context_size / PAGE_SIZE;
948 context_size += PARENT_SCRATCH_SIZE;
951 obj = i915_gem_object_create_lmem(engine->i915, context_size,
952 I915_BO_ALLOC_PM_VOLATILE);
954 obj = i915_gem_object_create_shmem(engine->i915, context_size);
956 return ERR_CAST(obj);
958 vma = i915_vma_instance(obj, &engine->gt->ggtt->vm, NULL);
960 i915_gem_object_put(obj);
967 static struct intel_timeline *
968 pinned_timeline(struct intel_context *ce, struct intel_engine_cs *engine)
970 struct intel_timeline *tl = fetch_and_zero(&ce->timeline);
972 return intel_timeline_create_from_engine(engine, page_unmask_bits(tl));
975 int lrc_alloc(struct intel_context *ce, struct intel_engine_cs *engine)
977 struct intel_ring *ring;
978 struct i915_vma *vma;
981 GEM_BUG_ON(ce->state);
983 vma = __lrc_alloc_state(ce, engine);
987 ring = intel_engine_create_ring(engine, ce->ring_size);
993 if (!page_mask_bits(ce->timeline)) {
994 struct intel_timeline *tl;
997 * Use the static global HWSP for the kernel context, and
998 * a dynamically allocated cacheline for everyone else.
1000 if (unlikely(ce->timeline))
1001 tl = pinned_timeline(ce, engine);
1003 tl = intel_timeline_create(engine->gt);
1018 intel_ring_put(ring);
1024 void lrc_reset(struct intel_context *ce)
1026 GEM_BUG_ON(!intel_context_is_pinned(ce));
1028 intel_ring_reset(ce->ring, ce->ring->emit);
1030 /* Scrub away the garbage */
1031 lrc_init_regs(ce, ce->engine, true);
1032 ce->lrc.lrca = lrc_update_regs(ce, ce->engine, ce->ring->tail);
1036 lrc_pre_pin(struct intel_context *ce,
1037 struct intel_engine_cs *engine,
1038 struct i915_gem_ww_ctx *ww,
1041 GEM_BUG_ON(!ce->state);
1042 GEM_BUG_ON(!i915_vma_is_pinned(ce->state));
1044 *vaddr = i915_gem_object_pin_map(ce->state->obj,
1045 i915_coherent_map_type(ce->engine->i915,
1050 return PTR_ERR_OR_ZERO(*vaddr);
1054 lrc_pin(struct intel_context *ce,
1055 struct intel_engine_cs *engine,
1058 ce->lrc_reg_state = vaddr + LRC_STATE_OFFSET;
1060 if (!__test_and_set_bit(CONTEXT_INIT_BIT, &ce->flags))
1061 lrc_init_state(ce, engine, vaddr);
1063 ce->lrc.lrca = lrc_update_regs(ce, engine, ce->ring->tail);
1067 void lrc_unpin(struct intel_context *ce)
1069 check_redzone((void *)ce->lrc_reg_state - LRC_STATE_OFFSET,
1073 void lrc_post_unpin(struct intel_context *ce)
1075 i915_gem_object_unpin_map(ce->state->obj);
1078 void lrc_fini(struct intel_context *ce)
1083 intel_ring_put(fetch_and_zero(&ce->ring));
1084 i915_vma_put(fetch_and_zero(&ce->state));
1087 void lrc_destroy(struct kref *kref)
1089 struct intel_context *ce = container_of(kref, typeof(*ce), ref);
1091 GEM_BUG_ON(!i915_active_is_idle(&ce->active));
1092 GEM_BUG_ON(intel_context_is_pinned(ce));
1096 intel_context_fini(ce);
1097 intel_context_free(ce);
1101 gen12_emit_timestamp_wa(const struct intel_context *ce, u32 *cs)
1103 *cs++ = MI_LOAD_REGISTER_MEM_GEN8 |
1104 MI_SRM_LRM_GLOBAL_GTT |
1106 *cs++ = i915_mmio_reg_offset(GEN8_RING_CS_GPR(0, 0));
1107 *cs++ = i915_ggtt_offset(ce->state) + LRC_STATE_OFFSET +
1108 CTX_TIMESTAMP * sizeof(u32);
1111 *cs++ = MI_LOAD_REGISTER_REG |
1112 MI_LRR_SOURCE_CS_MMIO |
1114 *cs++ = i915_mmio_reg_offset(GEN8_RING_CS_GPR(0, 0));
1115 *cs++ = i915_mmio_reg_offset(RING_CTX_TIMESTAMP(0));
1117 *cs++ = MI_LOAD_REGISTER_REG |
1118 MI_LRR_SOURCE_CS_MMIO |
1120 *cs++ = i915_mmio_reg_offset(GEN8_RING_CS_GPR(0, 0));
1121 *cs++ = i915_mmio_reg_offset(RING_CTX_TIMESTAMP(0));
1127 gen12_emit_restore_scratch(const struct intel_context *ce, u32 *cs)
1129 GEM_BUG_ON(lrc_ring_gpr0(ce->engine) == -1);
1131 *cs++ = MI_LOAD_REGISTER_MEM_GEN8 |
1132 MI_SRM_LRM_GLOBAL_GTT |
1134 *cs++ = i915_mmio_reg_offset(GEN8_RING_CS_GPR(0, 0));
1135 *cs++ = i915_ggtt_offset(ce->state) + LRC_STATE_OFFSET +
1136 (lrc_ring_gpr0(ce->engine) + 1) * sizeof(u32);
1143 gen12_emit_cmd_buf_wa(const struct intel_context *ce, u32 *cs)
1145 GEM_BUG_ON(lrc_ring_cmd_buf_cctl(ce->engine) == -1);
1147 *cs++ = MI_LOAD_REGISTER_MEM_GEN8 |
1148 MI_SRM_LRM_GLOBAL_GTT |
1150 *cs++ = i915_mmio_reg_offset(GEN8_RING_CS_GPR(0, 0));
1151 *cs++ = i915_ggtt_offset(ce->state) + LRC_STATE_OFFSET +
1152 (lrc_ring_cmd_buf_cctl(ce->engine) + 1) * sizeof(u32);
1155 *cs++ = MI_LOAD_REGISTER_REG |
1156 MI_LRR_SOURCE_CS_MMIO |
1158 *cs++ = i915_mmio_reg_offset(GEN8_RING_CS_GPR(0, 0));
1159 *cs++ = i915_mmio_reg_offset(RING_CMD_BUF_CCTL(0));
1165 gen12_emit_indirect_ctx_rcs(const struct intel_context *ce, u32 *cs)
1167 cs = gen12_emit_timestamp_wa(ce, cs);
1168 cs = gen12_emit_cmd_buf_wa(ce, cs);
1169 cs = gen12_emit_restore_scratch(ce, cs);
1171 /* Wa_16013000631:dg2 */
1172 if (IS_DG2_GRAPHICS_STEP(ce->engine->i915, G10, STEP_B0, STEP_C0) ||
1173 IS_DG2_G11(ce->engine->i915))
1174 cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE, 0);
1180 gen12_emit_indirect_ctx_xcs(const struct intel_context *ce, u32 *cs)
1182 cs = gen12_emit_timestamp_wa(ce, cs);
1183 cs = gen12_emit_restore_scratch(ce, cs);
1188 static u32 context_wa_bb_offset(const struct intel_context *ce)
1190 return PAGE_SIZE * ce->wa_bb_page;
1193 static u32 *context_indirect_bb(const struct intel_context *ce)
1197 GEM_BUG_ON(!ce->wa_bb_page);
1199 ptr = ce->lrc_reg_state;
1200 ptr -= LRC_STATE_OFFSET; /* back to start of context image */
1201 ptr += context_wa_bb_offset(ce);
1207 setup_indirect_ctx_bb(const struct intel_context *ce,
1208 const struct intel_engine_cs *engine,
1209 u32 *(*emit)(const struct intel_context *, u32 *))
1211 u32 * const start = context_indirect_bb(ce);
1214 cs = emit(ce, start);
1215 GEM_BUG_ON(cs - start > I915_GTT_PAGE_SIZE / sizeof(*cs));
1216 while ((unsigned long)cs % CACHELINE_BYTES)
1219 lrc_setup_indirect_ctx(ce->lrc_reg_state, engine,
1220 i915_ggtt_offset(ce->state) +
1221 context_wa_bb_offset(ce),
1222 (cs - start) * sizeof(*cs));
1226 * The context descriptor encodes various attributes of a context,
1227 * including its GTT address and some flags. Because it's fairly
1228 * expensive to calculate, we'll just do it once and cache the result,
1229 * which remains valid until the context is unpinned.
1231 * This is what a descriptor looks like, from LSB to MSB::
1233 * bits 0-11: flags, GEN8_CTX_* (cached in ctx->desc_template)
1234 * bits 12-31: LRCA, GTT address of (the HWSP of) this context
1235 * bits 32-52: ctx ID, a globally unique tag (highest bit used by GuC)
1236 * bits 53-54: mbz, reserved for use by hardware
1237 * bits 55-63: group ID, currently unused and set to 0
1239 * Starting from Gen11, the upper dword of the descriptor has a new format:
1241 * bits 32-36: reserved
1242 * bits 37-47: SW context ID
1243 * bits 48:53: engine instance
1244 * bit 54: mbz, reserved for use by hardware
1245 * bits 55-60: SW counter
1246 * bits 61-63: engine class
1248 * On Xe_HP, the upper dword of the descriptor has a new format:
1250 * bits 32-37: virtual function number
1251 * bit 38: mbz, reserved for use by hardware
1252 * bits 39-54: SW context ID
1253 * bits 55-57: reserved
1254 * bits 58-63: SW counter
1256 * engine info, SW context ID and SW counter need to form a unique number
1257 * (Context ID) per lrc.
1259 static u32 lrc_descriptor(const struct intel_context *ce)
1263 desc = INTEL_LEGACY_32B_CONTEXT;
1264 if (i915_vm_is_4lvl(ce->vm))
1265 desc = INTEL_LEGACY_64B_CONTEXT;
1266 desc <<= GEN8_CTX_ADDRESSING_MODE_SHIFT;
1268 desc |= GEN8_CTX_VALID | GEN8_CTX_PRIVILEGE;
1269 if (GRAPHICS_VER(ce->vm->i915) == 8)
1270 desc |= GEN8_CTX_L3LLC_COHERENT;
1272 return i915_ggtt_offset(ce->state) | desc;
1275 u32 lrc_update_regs(const struct intel_context *ce,
1276 const struct intel_engine_cs *engine,
1279 struct intel_ring *ring = ce->ring;
1280 u32 *regs = ce->lrc_reg_state;
1282 GEM_BUG_ON(!intel_ring_offset_valid(ring, head));
1283 GEM_BUG_ON(!intel_ring_offset_valid(ring, ring->tail));
1285 regs[CTX_RING_START] = i915_ggtt_offset(ring->vma);
1286 regs[CTX_RING_HEAD] = head;
1287 regs[CTX_RING_TAIL] = ring->tail;
1288 regs[CTX_RING_CTL] = RING_CTL_SIZE(ring->size) | RING_VALID;
1291 if (engine->class == RENDER_CLASS) {
1292 regs[CTX_R_PWR_CLK_STATE] =
1293 intel_sseu_make_rpcs(engine->gt, &ce->sseu);
1295 i915_oa_init_reg_state(ce, engine);
1298 if (ce->wa_bb_page) {
1299 u32 *(*fn)(const struct intel_context *ce, u32 *cs);
1301 fn = gen12_emit_indirect_ctx_xcs;
1302 if (ce->engine->class == RENDER_CLASS)
1303 fn = gen12_emit_indirect_ctx_rcs;
1305 /* Mutually exclusive wrt to global indirect bb */
1306 GEM_BUG_ON(engine->wa_ctx.indirect_ctx.size);
1307 setup_indirect_ctx_bb(ce, engine, fn);
1310 return lrc_descriptor(ce) | CTX_DESC_FORCE_RESTORE;
1313 void lrc_update_offsets(struct intel_context *ce,
1314 struct intel_engine_cs *engine)
1316 set_offsets(ce->lrc_reg_state, reg_offsets(engine), engine, false);
1319 void lrc_check_regs(const struct intel_context *ce,
1320 const struct intel_engine_cs *engine,
1323 const struct intel_ring *ring = ce->ring;
1324 u32 *regs = ce->lrc_reg_state;
1328 if (regs[CTX_RING_START] != i915_ggtt_offset(ring->vma)) {
1329 pr_err("%s: context submitted with incorrect RING_START [%08x], expected %08x\n",
1331 regs[CTX_RING_START],
1332 i915_ggtt_offset(ring->vma));
1333 regs[CTX_RING_START] = i915_ggtt_offset(ring->vma);
1337 if ((regs[CTX_RING_CTL] & ~(RING_WAIT | RING_WAIT_SEMAPHORE)) !=
1338 (RING_CTL_SIZE(ring->size) | RING_VALID)) {
1339 pr_err("%s: context submitted with incorrect RING_CTL [%08x], expected %08x\n",
1342 (u32)(RING_CTL_SIZE(ring->size) | RING_VALID));
1343 regs[CTX_RING_CTL] = RING_CTL_SIZE(ring->size) | RING_VALID;
1347 x = lrc_ring_mi_mode(engine);
1348 if (x != -1 && regs[x + 1] & (regs[x + 1] >> 16) & STOP_RING) {
1349 pr_err("%s: context submitted with STOP_RING [%08x] in RING_MI_MODE\n",
1350 engine->name, regs[x + 1]);
1351 regs[x + 1] &= ~STOP_RING;
1352 regs[x + 1] |= STOP_RING << 16;
1356 WARN_ONCE(!valid, "Invalid lrc state found %s submission\n", when);
1360 * In this WA we need to set GEN8_L3SQCREG4[21:21] and reset it after
1361 * PIPE_CONTROL instruction. This is required for the flush to happen correctly
1362 * but there is a slight complication as this is applied in WA batch where the
1363 * values are only initialized once so we cannot take register value at the
1364 * beginning and reuse it further; hence we save its value to memory, upload a
1365 * constant value with bit21 set and then we restore it back with the saved value.
1366 * To simplify the WA, a constant value is formed by using the default value
1367 * of this register. This shouldn't be a problem because we are only modifying
1368 * it for a short period and this batch in non-premptible. We can ofcourse
1369 * use additional instructions that read the actual value of the register
1370 * at that time and set our bit of interest but it makes the WA complicated.
1372 * This WA is also required for Gen9 so extracting as a function avoids
1376 gen8_emit_flush_coherentl3_wa(struct intel_engine_cs *engine, u32 *batch)
1378 /* NB no one else is allowed to scribble over scratch + 256! */
1379 *batch++ = MI_STORE_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
1380 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1381 *batch++ = intel_gt_scratch_offset(engine->gt,
1382 INTEL_GT_SCRATCH_FIELD_COHERENTL3_WA);
1385 *batch++ = MI_LOAD_REGISTER_IMM(1);
1386 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1387 *batch++ = 0x40400000 | GEN8_LQSC_FLUSH_COHERENT_LINES;
1389 batch = gen8_emit_pipe_control(batch,
1390 PIPE_CONTROL_CS_STALL |
1391 PIPE_CONTROL_DC_FLUSH_ENABLE,
1394 *batch++ = MI_LOAD_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
1395 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1396 *batch++ = intel_gt_scratch_offset(engine->gt,
1397 INTEL_GT_SCRATCH_FIELD_COHERENTL3_WA);
1404 * Typically we only have one indirect_ctx and per_ctx batch buffer which are
1405 * initialized at the beginning and shared across all contexts but this field
1406 * helps us to have multiple batches at different offsets and select them based
1407 * on a criteria. At the moment this batch always start at the beginning of the page
1408 * and at this point we don't have multiple wa_ctx batch buffers.
1410 * The number of WA applied are not known at the beginning; we use this field
1411 * to return the no of DWORDS written.
1413 * It is to be noted that this batch does not contain MI_BATCH_BUFFER_END
1414 * so it adds NOOPs as padding to make it cacheline aligned.
1415 * MI_BATCH_BUFFER_END will be added to perctx batch and both of them together
1416 * makes a complete batch buffer.
1418 static u32 *gen8_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
1420 /* WaDisableCtxRestoreArbitration:bdw,chv */
1421 *batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
1423 /* WaFlushCoherentL3CacheLinesAtContextSwitch:bdw */
1424 if (IS_BROADWELL(engine->i915))
1425 batch = gen8_emit_flush_coherentl3_wa(engine, batch);
1427 /* WaClearSlmSpaceAtContextSwitch:bdw,chv */
1428 /* Actual scratch location is at 128 bytes offset */
1429 batch = gen8_emit_pipe_control(batch,
1430 PIPE_CONTROL_FLUSH_L3 |
1431 PIPE_CONTROL_STORE_DATA_INDEX |
1432 PIPE_CONTROL_CS_STALL |
1433 PIPE_CONTROL_QW_WRITE,
1434 LRC_PPHWSP_SCRATCH_ADDR);
1436 *batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1438 /* Pad to end of cacheline */
1439 while ((unsigned long)batch % CACHELINE_BYTES)
1443 * MI_BATCH_BUFFER_END is not required in Indirect ctx BB because
1444 * execution depends on the length specified in terms of cache lines
1445 * in the register CTX_RCS_INDIRECT_CTX
1456 static u32 *emit_lri(u32 *batch, const struct lri *lri, unsigned int count)
1458 GEM_BUG_ON(!count || count > 63);
1460 *batch++ = MI_LOAD_REGISTER_IMM(count);
1462 *batch++ = i915_mmio_reg_offset(lri->reg);
1463 *batch++ = lri->value;
1464 } while (lri++, --count);
1470 static u32 *gen9_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
1472 static const struct lri lri[] = {
1473 /* WaDisableGatherAtSetShaderCommonSlice:skl,bxt,kbl,glk */
1475 COMMON_SLICE_CHICKEN2,
1476 __MASKED_FIELD(GEN9_DISABLE_GATHER_AT_SET_SHADER_COMMON_SLICE,
1483 __MASKED_FIELD(FF_SLICE_CHICKEN_CL_PROVOKING_VERTEX_FIX,
1484 FF_SLICE_CHICKEN_CL_PROVOKING_VERTEX_FIX),
1490 __MASKED_FIELD(_3D_CHICKEN_SF_PROVOKING_VERTEX_FIX,
1491 _3D_CHICKEN_SF_PROVOKING_VERTEX_FIX),
1495 *batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
1497 /* WaFlushCoherentL3CacheLinesAtContextSwitch:skl,bxt,glk */
1498 batch = gen8_emit_flush_coherentl3_wa(engine, batch);
1500 /* WaClearSlmSpaceAtContextSwitch:skl,bxt,kbl,glk,cfl */
1501 batch = gen8_emit_pipe_control(batch,
1502 PIPE_CONTROL_FLUSH_L3 |
1503 PIPE_CONTROL_STORE_DATA_INDEX |
1504 PIPE_CONTROL_CS_STALL |
1505 PIPE_CONTROL_QW_WRITE,
1506 LRC_PPHWSP_SCRATCH_ADDR);
1508 batch = emit_lri(batch, lri, ARRAY_SIZE(lri));
1510 /* WaMediaPoolStateCmdInWABB:bxt,glk */
1511 if (HAS_POOLED_EU(engine->i915)) {
1513 * EU pool configuration is setup along with golden context
1514 * during context initialization. This value depends on
1515 * device type (2x6 or 3x6) and needs to be updated based
1516 * on which subslice is disabled especially for 2x6
1517 * devices, however it is safe to load default
1518 * configuration of 3x6 device instead of masking off
1519 * corresponding bits because HW ignores bits of a disabled
1520 * subslice and drops down to appropriate config. Please
1521 * see render_state_setup() in i915_gem_render_state.c for
1522 * possible configurations, to avoid duplication they are
1523 * not shown here again.
1525 *batch++ = GEN9_MEDIA_POOL_STATE;
1526 *batch++ = GEN9_MEDIA_POOL_ENABLE;
1527 *batch++ = 0x00777000;
1533 *batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1535 /* Pad to end of cacheline */
1536 while ((unsigned long)batch % CACHELINE_BYTES)
1542 #define CTX_WA_BB_SIZE (PAGE_SIZE)
1544 static int lrc_create_wa_ctx(struct intel_engine_cs *engine)
1546 struct drm_i915_gem_object *obj;
1547 struct i915_vma *vma;
1550 obj = i915_gem_object_create_shmem(engine->i915, CTX_WA_BB_SIZE);
1552 return PTR_ERR(obj);
1554 vma = i915_vma_instance(obj, &engine->gt->ggtt->vm, NULL);
1560 engine->wa_ctx.vma = vma;
1564 i915_gem_object_put(obj);
1568 void lrc_fini_wa_ctx(struct intel_engine_cs *engine)
1570 i915_vma_unpin_and_release(&engine->wa_ctx.vma, 0);
1573 typedef u32 *(*wa_bb_func_t)(struct intel_engine_cs *engine, u32 *batch);
1575 void lrc_init_wa_ctx(struct intel_engine_cs *engine)
1577 struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
1578 struct i915_wa_ctx_bb *wa_bb[] = {
1579 &wa_ctx->indirect_ctx, &wa_ctx->per_ctx
1581 wa_bb_func_t wa_bb_fn[ARRAY_SIZE(wa_bb)];
1582 struct i915_gem_ww_ctx ww;
1583 void *batch, *batch_ptr;
1587 if (engine->class != RENDER_CLASS)
1590 switch (GRAPHICS_VER(engine->i915)) {
1595 wa_bb_fn[0] = gen9_init_indirectctx_bb;
1599 wa_bb_fn[0] = gen8_init_indirectctx_bb;
1603 MISSING_CASE(GRAPHICS_VER(engine->i915));
1607 err = lrc_create_wa_ctx(engine);
1610 * We continue even if we fail to initialize WA batch
1611 * because we only expect rare glitches but nothing
1612 * critical to prevent us from using GPU
1614 drm_err(&engine->i915->drm,
1615 "Ignoring context switch w/a allocation error:%d\n",
1620 if (!engine->wa_ctx.vma)
1623 i915_gem_ww_ctx_init(&ww, true);
1625 err = i915_gem_object_lock(wa_ctx->vma->obj, &ww);
1627 err = i915_ggtt_pin(wa_ctx->vma, &ww, 0, PIN_HIGH);
1631 batch = i915_gem_object_pin_map(wa_ctx->vma->obj, I915_MAP_WB);
1632 if (IS_ERR(batch)) {
1633 err = PTR_ERR(batch);
1638 * Emit the two workaround batch buffers, recording the offset from the
1639 * start of the workaround batch buffer object for each and their
1643 for (i = 0; i < ARRAY_SIZE(wa_bb_fn); i++) {
1644 wa_bb[i]->offset = batch_ptr - batch;
1645 if (GEM_DEBUG_WARN_ON(!IS_ALIGNED(wa_bb[i]->offset,
1646 CACHELINE_BYTES))) {
1651 batch_ptr = wa_bb_fn[i](engine, batch_ptr);
1652 wa_bb[i]->size = batch_ptr - (batch + wa_bb[i]->offset);
1654 GEM_BUG_ON(batch_ptr - batch > CTX_WA_BB_SIZE);
1656 __i915_gem_object_flush_map(wa_ctx->vma->obj, 0, batch_ptr - batch);
1657 __i915_gem_object_release_map(wa_ctx->vma->obj);
1659 /* Verify that we can handle failure to setup the wa_ctx */
1661 err = i915_inject_probe_error(engine->i915, -ENODEV);
1665 i915_vma_unpin(wa_ctx->vma);
1667 if (err == -EDEADLK) {
1668 err = i915_gem_ww_ctx_backoff(&ww);
1672 i915_gem_ww_ctx_fini(&ww);
1675 i915_vma_put(engine->wa_ctx.vma);
1677 /* Clear all flags to prevent further use */
1678 memset(wa_ctx, 0, sizeof(*wa_ctx));
1682 static void st_update_runtime_underflow(struct intel_context *ce, s32 dt)
1684 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1685 ce->runtime.num_underflow++;
1686 ce->runtime.max_underflow = max_t(u32, ce->runtime.max_underflow, -dt);
1690 void lrc_update_runtime(struct intel_context *ce)
1695 if (intel_context_is_barrier(ce))
1698 old = ce->runtime.last;
1699 ce->runtime.last = lrc_get_runtime(ce);
1700 dt = ce->runtime.last - old;
1702 if (unlikely(dt < 0)) {
1703 CE_TRACE(ce, "runtime underflow: last=%u, new=%u, delta=%d\n",
1704 old, ce->runtime.last, dt);
1705 st_update_runtime_underflow(ce, dt);
1709 ewma_runtime_add(&ce->runtime.avg, dt);
1710 ce->runtime.total += dt;
1713 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1714 #include "selftest_lrc.c"