1 // SPDX-License-Identifier: MIT
3 * Copyright © 2014 Intel Corporation
6 #include "gen8_engine_cs.h"
9 #include "intel_engine.h"
10 #include "intel_gpu_commands.h"
12 #include "intel_lrc.h"
13 #include "intel_lrc_reg.h"
14 #include "intel_ring.h"
15 #include "shmem_utils.h"
17 static void set_offsets(u32 *regs,
19 const struct intel_engine_cs *engine,
21 #define NOP(x) (BIT(7) | (x))
22 #define LRI(count, flags) ((flags) << 6 | (count) | BUILD_BUG_ON_ZERO(count >= BIT(6)))
24 #define REG(x) (((x) >> 2) | BUILD_BUG_ON_ZERO(x >= 0x200))
26 (((x) >> 9) | BIT(7) | BUILD_BUG_ON_ZERO(x >= 0x10000)), \
30 const u32 base = engine->mmio_base;
35 if (*data & BIT(7)) { /* skip */
36 count = *data++ & ~BIT(7);
45 *regs = MI_LOAD_REGISTER_IMM(count);
47 *regs |= MI_LRI_FORCE_POSTED;
48 if (INTEL_GEN(engine->i915) >= 11)
49 *regs |= MI_LRI_LRM_CS_MMIO;
60 offset |= v & ~BIT(7);
63 regs[0] = base + (offset << 2);
69 /* Close the batch; used mainly by live_lrc_layout() */
70 *regs = MI_BATCH_BUFFER_END;
71 if (INTEL_GEN(engine->i915) >= 10)
76 static const u8 gen8_xcs_offsets[] = {
111 static const u8 gen9_xcs_offsets[] = {
195 static const u8 gen12_xcs_offsets[] = {
227 static const u8 gen8_rcs_offsets[] = {
264 static const u8 gen9_rcs_offsets[] = {
348 static const u8 gen11_rcs_offsets[] = {
389 static const u8 gen12_rcs_offsets[] = {
491 static const u8 *reg_offsets(const struct intel_engine_cs *engine)
494 * The gen12+ lists only have the registers we program in the basic
495 * default state. We rely on the context image using relative
496 * addressing to automatic fixup the register state between the
497 * physical engines for virtual engine.
499 GEM_BUG_ON(INTEL_GEN(engine->i915) >= 12 &&
500 !intel_engine_has_relative_mmio(engine));
502 if (engine->class == RENDER_CLASS) {
503 if (INTEL_GEN(engine->i915) >= 12)
504 return gen12_rcs_offsets;
505 else if (INTEL_GEN(engine->i915) >= 11)
506 return gen11_rcs_offsets;
507 else if (INTEL_GEN(engine->i915) >= 9)
508 return gen9_rcs_offsets;
510 return gen8_rcs_offsets;
512 if (INTEL_GEN(engine->i915) >= 12)
513 return gen12_xcs_offsets;
514 else if (INTEL_GEN(engine->i915) >= 9)
515 return gen9_xcs_offsets;
517 return gen8_xcs_offsets;
521 static int lrc_ring_mi_mode(const struct intel_engine_cs *engine)
523 if (INTEL_GEN(engine->i915) >= 12)
525 else if (INTEL_GEN(engine->i915) >= 9)
527 else if (engine->class == RENDER_CLASS)
533 static int lrc_ring_gpr0(const struct intel_engine_cs *engine)
535 if (INTEL_GEN(engine->i915) >= 12)
537 else if (INTEL_GEN(engine->i915) >= 9)
539 else if (engine->class == RENDER_CLASS)
545 static int lrc_ring_wa_bb_per_ctx(const struct intel_engine_cs *engine)
547 if (INTEL_GEN(engine->i915) >= 12)
549 else if (INTEL_GEN(engine->i915) >= 9 || engine->class == RENDER_CLASS)
555 static int lrc_ring_indirect_ptr(const struct intel_engine_cs *engine)
559 x = lrc_ring_wa_bb_per_ctx(engine);
566 static int lrc_ring_indirect_offset(const struct intel_engine_cs *engine)
570 x = lrc_ring_indirect_ptr(engine);
577 static int lrc_ring_cmd_buf_cctl(const struct intel_engine_cs *engine)
579 if (engine->class != RENDER_CLASS)
582 if (INTEL_GEN(engine->i915) >= 12)
584 else if (INTEL_GEN(engine->i915) >= 11)
591 lrc_ring_indirect_offset_default(const struct intel_engine_cs *engine)
593 switch (INTEL_GEN(engine->i915)) {
595 MISSING_CASE(INTEL_GEN(engine->i915));
598 return GEN12_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
600 return GEN11_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
602 return GEN10_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
604 return GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
606 return GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
611 lrc_setup_indirect_ctx(u32 *regs,
612 const struct intel_engine_cs *engine,
613 u32 ctx_bb_ggtt_addr,
617 GEM_BUG_ON(!IS_ALIGNED(size, CACHELINE_BYTES));
618 GEM_BUG_ON(lrc_ring_indirect_ptr(engine) == -1);
619 regs[lrc_ring_indirect_ptr(engine) + 1] =
620 ctx_bb_ggtt_addr | (size / CACHELINE_BYTES);
622 GEM_BUG_ON(lrc_ring_indirect_offset(engine) == -1);
623 regs[lrc_ring_indirect_offset(engine) + 1] =
624 lrc_ring_indirect_offset_default(engine) << 6;
627 static void init_common_regs(u32 * const regs,
628 const struct intel_context *ce,
629 const struct intel_engine_cs *engine,
634 ctl = _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH);
635 ctl |= _MASKED_BIT_DISABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT);
637 ctl |= CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT;
638 if (INTEL_GEN(engine->i915) < 11)
639 ctl |= _MASKED_BIT_DISABLE(CTX_CTRL_ENGINE_CTX_SAVE_INHIBIT |
640 CTX_CTRL_RS_CTX_ENABLE);
641 regs[CTX_CONTEXT_CONTROL] = ctl;
643 regs[CTX_TIMESTAMP] = ce->runtime.last;
646 static void init_wa_bb_regs(u32 * const regs,
647 const struct intel_engine_cs *engine)
649 const struct i915_ctx_workarounds * const wa_ctx = &engine->wa_ctx;
651 if (wa_ctx->per_ctx.size) {
652 const u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
654 GEM_BUG_ON(lrc_ring_wa_bb_per_ctx(engine) == -1);
655 regs[lrc_ring_wa_bb_per_ctx(engine) + 1] =
656 (ggtt_offset + wa_ctx->per_ctx.offset) | 0x01;
659 if (wa_ctx->indirect_ctx.size) {
660 lrc_setup_indirect_ctx(regs, engine,
661 i915_ggtt_offset(wa_ctx->vma) +
662 wa_ctx->indirect_ctx.offset,
663 wa_ctx->indirect_ctx.size);
667 static void init_ppgtt_regs(u32 *regs, const struct i915_ppgtt *ppgtt)
669 if (i915_vm_is_4lvl(&ppgtt->vm)) {
670 /* 64b PPGTT (48bit canonical)
671 * PDP0_DESCRIPTOR contains the base address to PML4 and
672 * other PDP Descriptors are ignored.
674 ASSIGN_CTX_PML4(ppgtt, regs);
676 ASSIGN_CTX_PDP(ppgtt, regs, 3);
677 ASSIGN_CTX_PDP(ppgtt, regs, 2);
678 ASSIGN_CTX_PDP(ppgtt, regs, 1);
679 ASSIGN_CTX_PDP(ppgtt, regs, 0);
683 static struct i915_ppgtt *vm_alias(struct i915_address_space *vm)
685 if (i915_is_ggtt(vm))
686 return i915_vm_to_ggtt(vm)->alias;
688 return i915_vm_to_ppgtt(vm);
691 static void __reset_stop_ring(u32 *regs, const struct intel_engine_cs *engine)
695 x = lrc_ring_mi_mode(engine);
697 regs[x + 1] &= ~STOP_RING;
698 regs[x + 1] |= STOP_RING << 16;
702 static void __lrc_init_regs(u32 *regs,
703 const struct intel_context *ce,
704 const struct intel_engine_cs *engine,
708 * A context is actually a big batch buffer with several
709 * MI_LOAD_REGISTER_IMM commands followed by (reg, value) pairs. The
710 * values we are setting here are only for the first context restore:
711 * on a subsequent save, the GPU will recreate this batchbuffer with new
712 * values (including all the missing MI_LOAD_REGISTER_IMM commands that
713 * we are not initializing here).
715 * Must keep consistent with virtual_update_register_offsets().
719 memset(regs, 0, PAGE_SIZE);
721 set_offsets(regs, reg_offsets(engine), engine, inhibit);
723 init_common_regs(regs, ce, engine, inhibit);
724 init_ppgtt_regs(regs, vm_alias(ce->vm));
726 init_wa_bb_regs(regs, engine);
728 __reset_stop_ring(regs, engine);
731 void lrc_init_regs(const struct intel_context *ce,
732 const struct intel_engine_cs *engine,
735 __lrc_init_regs(ce->lrc_reg_state, ce, engine, inhibit);
738 void lrc_reset_regs(const struct intel_context *ce,
739 const struct intel_engine_cs *engine)
741 __reset_stop_ring(ce->lrc_reg_state, engine);
745 set_redzone(void *vaddr, const struct intel_engine_cs *engine)
747 if (!IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
750 vaddr += engine->context_size;
752 memset(vaddr, CONTEXT_REDZONE, I915_GTT_PAGE_SIZE);
756 check_redzone(const void *vaddr, const struct intel_engine_cs *engine)
758 if (!IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
761 vaddr += engine->context_size;
763 if (memchr_inv(vaddr, CONTEXT_REDZONE, I915_GTT_PAGE_SIZE))
764 drm_err_once(&engine->i915->drm,
765 "%s context redzone overwritten!\n",
769 void lrc_init_state(struct intel_context *ce,
770 struct intel_engine_cs *engine,
775 set_redzone(state, engine);
777 if (engine->default_state) {
778 shmem_read(engine->default_state, 0,
779 state, engine->context_size);
780 __set_bit(CONTEXT_VALID_BIT, &ce->flags);
784 /* Clear the ppHWSP (inc. per-context counters) */
785 memset(state, 0, PAGE_SIZE);
788 * The second page of the context object contains some registers which
789 * must be set up prior to the first execution.
791 __lrc_init_regs(state + LRC_STATE_OFFSET, ce, engine, inhibit);
794 static struct i915_vma *
795 __lrc_alloc_state(struct intel_context *ce, struct intel_engine_cs *engine)
797 struct drm_i915_gem_object *obj;
798 struct i915_vma *vma;
801 context_size = round_up(engine->context_size, I915_GTT_PAGE_SIZE);
803 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
804 context_size += I915_GTT_PAGE_SIZE; /* for redzone */
806 if (INTEL_GEN(engine->i915) == 12) {
807 ce->wa_bb_page = context_size / PAGE_SIZE;
808 context_size += PAGE_SIZE;
811 obj = i915_gem_object_create_shmem(engine->i915, context_size);
813 return ERR_CAST(obj);
815 vma = i915_vma_instance(obj, &engine->gt->ggtt->vm, NULL);
817 i915_gem_object_put(obj);
824 static struct intel_timeline *
825 pinned_timeline(struct intel_context *ce, struct intel_engine_cs *engine)
827 struct intel_timeline *tl = fetch_and_zero(&ce->timeline);
829 return intel_timeline_create_from_engine(engine, page_unmask_bits(tl));
832 int lrc_alloc(struct intel_context *ce, struct intel_engine_cs *engine)
834 struct intel_ring *ring;
835 struct i915_vma *vma;
838 GEM_BUG_ON(ce->state);
840 vma = __lrc_alloc_state(ce, engine);
844 ring = intel_engine_create_ring(engine, (unsigned long)ce->ring);
850 if (!page_mask_bits(ce->timeline)) {
851 struct intel_timeline *tl;
854 * Use the static global HWSP for the kernel context, and
855 * a dynamically allocated cacheline for everyone else.
857 if (unlikely(ce->timeline))
858 tl = pinned_timeline(ce, engine);
860 tl = intel_timeline_create(engine->gt);
875 intel_ring_put(ring);
881 void lrc_reset(struct intel_context *ce)
883 GEM_BUG_ON(!intel_context_is_pinned(ce));
885 intel_ring_reset(ce->ring, ce->ring->emit);
887 /* Scrub away the garbage */
888 lrc_init_regs(ce, ce->engine, true);
889 ce->lrc.lrca = lrc_update_regs(ce, ce->engine, ce->ring->tail);
893 lrc_pre_pin(struct intel_context *ce,
894 struct intel_engine_cs *engine,
895 struct i915_gem_ww_ctx *ww,
898 GEM_BUG_ON(!ce->state);
899 GEM_BUG_ON(!i915_vma_is_pinned(ce->state));
901 *vaddr = i915_gem_object_pin_map(ce->state->obj,
902 i915_coherent_map_type(ce->engine->i915) |
905 return PTR_ERR_OR_ZERO(*vaddr);
909 lrc_pin(struct intel_context *ce,
910 struct intel_engine_cs *engine,
913 ce->lrc_reg_state = vaddr + LRC_STATE_OFFSET;
915 if (!__test_and_set_bit(CONTEXT_INIT_BIT, &ce->flags))
916 lrc_init_state(ce, engine, vaddr);
918 ce->lrc.lrca = lrc_update_regs(ce, engine, ce->ring->tail);
922 void lrc_unpin(struct intel_context *ce)
924 check_redzone((void *)ce->lrc_reg_state - LRC_STATE_OFFSET,
928 void lrc_post_unpin(struct intel_context *ce)
930 i915_gem_object_unpin_map(ce->state->obj);
933 void lrc_fini(struct intel_context *ce)
938 intel_ring_put(fetch_and_zero(&ce->ring));
939 i915_vma_put(fetch_and_zero(&ce->state));
942 void lrc_destroy(struct kref *kref)
944 struct intel_context *ce = container_of(kref, typeof(*ce), ref);
946 GEM_BUG_ON(!i915_active_is_idle(&ce->active));
947 GEM_BUG_ON(intel_context_is_pinned(ce));
951 intel_context_fini(ce);
952 intel_context_free(ce);
956 gen12_emit_timestamp_wa(const struct intel_context *ce, u32 *cs)
958 *cs++ = MI_LOAD_REGISTER_MEM_GEN8 |
959 MI_SRM_LRM_GLOBAL_GTT |
961 *cs++ = i915_mmio_reg_offset(GEN8_RING_CS_GPR(0, 0));
962 *cs++ = i915_ggtt_offset(ce->state) + LRC_STATE_OFFSET +
963 CTX_TIMESTAMP * sizeof(u32);
966 *cs++ = MI_LOAD_REGISTER_REG |
967 MI_LRR_SOURCE_CS_MMIO |
969 *cs++ = i915_mmio_reg_offset(GEN8_RING_CS_GPR(0, 0));
970 *cs++ = i915_mmio_reg_offset(RING_CTX_TIMESTAMP(0));
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));
982 gen12_emit_restore_scratch(const struct intel_context *ce, u32 *cs)
984 GEM_BUG_ON(lrc_ring_gpr0(ce->engine) == -1);
986 *cs++ = MI_LOAD_REGISTER_MEM_GEN8 |
987 MI_SRM_LRM_GLOBAL_GTT |
989 *cs++ = i915_mmio_reg_offset(GEN8_RING_CS_GPR(0, 0));
990 *cs++ = i915_ggtt_offset(ce->state) + LRC_STATE_OFFSET +
991 (lrc_ring_gpr0(ce->engine) + 1) * sizeof(u32);
998 gen12_emit_cmd_buf_wa(const struct intel_context *ce, u32 *cs)
1000 GEM_BUG_ON(lrc_ring_cmd_buf_cctl(ce->engine) == -1);
1002 *cs++ = MI_LOAD_REGISTER_MEM_GEN8 |
1003 MI_SRM_LRM_GLOBAL_GTT |
1005 *cs++ = i915_mmio_reg_offset(GEN8_RING_CS_GPR(0, 0));
1006 *cs++ = i915_ggtt_offset(ce->state) + LRC_STATE_OFFSET +
1007 (lrc_ring_cmd_buf_cctl(ce->engine) + 1) * sizeof(u32);
1010 *cs++ = MI_LOAD_REGISTER_REG |
1011 MI_LRR_SOURCE_CS_MMIO |
1013 *cs++ = i915_mmio_reg_offset(GEN8_RING_CS_GPR(0, 0));
1014 *cs++ = i915_mmio_reg_offset(RING_CMD_BUF_CCTL(0));
1020 gen12_emit_indirect_ctx_rcs(const struct intel_context *ce, u32 *cs)
1022 cs = gen12_emit_timestamp_wa(ce, cs);
1023 cs = gen12_emit_cmd_buf_wa(ce, cs);
1024 cs = gen12_emit_restore_scratch(ce, cs);
1030 gen12_emit_indirect_ctx_xcs(const struct intel_context *ce, u32 *cs)
1032 cs = gen12_emit_timestamp_wa(ce, cs);
1033 cs = gen12_emit_restore_scratch(ce, cs);
1038 static u32 context_wa_bb_offset(const struct intel_context *ce)
1040 return PAGE_SIZE * ce->wa_bb_page;
1043 static u32 *context_indirect_bb(const struct intel_context *ce)
1047 GEM_BUG_ON(!ce->wa_bb_page);
1049 ptr = ce->lrc_reg_state;
1050 ptr -= LRC_STATE_OFFSET; /* back to start of context image */
1051 ptr += context_wa_bb_offset(ce);
1057 setup_indirect_ctx_bb(const struct intel_context *ce,
1058 const struct intel_engine_cs *engine,
1059 u32 *(*emit)(const struct intel_context *, u32 *))
1061 u32 * const start = context_indirect_bb(ce);
1064 cs = emit(ce, start);
1065 GEM_BUG_ON(cs - start > I915_GTT_PAGE_SIZE / sizeof(*cs));
1066 while ((unsigned long)cs % CACHELINE_BYTES)
1069 lrc_setup_indirect_ctx(ce->lrc_reg_state, engine,
1070 i915_ggtt_offset(ce->state) +
1071 context_wa_bb_offset(ce),
1072 (cs - start) * sizeof(*cs));
1076 * The context descriptor encodes various attributes of a context,
1077 * including its GTT address and some flags. Because it's fairly
1078 * expensive to calculate, we'll just do it once and cache the result,
1079 * which remains valid until the context is unpinned.
1081 * This is what a descriptor looks like, from LSB to MSB::
1083 * bits 0-11: flags, GEN8_CTX_* (cached in ctx->desc_template)
1084 * bits 12-31: LRCA, GTT address of (the HWSP of) this context
1085 * bits 32-52: ctx ID, a globally unique tag (highest bit used by GuC)
1086 * bits 53-54: mbz, reserved for use by hardware
1087 * bits 55-63: group ID, currently unused and set to 0
1089 * Starting from Gen11, the upper dword of the descriptor has a new format:
1091 * bits 32-36: reserved
1092 * bits 37-47: SW context ID
1093 * bits 48:53: engine instance
1094 * bit 54: mbz, reserved for use by hardware
1095 * bits 55-60: SW counter
1096 * bits 61-63: engine class
1098 * engine info, SW context ID and SW counter need to form a unique number
1099 * (Context ID) per lrc.
1101 static u32 lrc_descriptor(const struct intel_context *ce)
1105 desc = INTEL_LEGACY_32B_CONTEXT;
1106 if (i915_vm_is_4lvl(ce->vm))
1107 desc = INTEL_LEGACY_64B_CONTEXT;
1108 desc <<= GEN8_CTX_ADDRESSING_MODE_SHIFT;
1110 desc |= GEN8_CTX_VALID | GEN8_CTX_PRIVILEGE;
1111 if (IS_GEN(ce->vm->i915, 8))
1112 desc |= GEN8_CTX_L3LLC_COHERENT;
1114 return i915_ggtt_offset(ce->state) | desc;
1117 u32 lrc_update_regs(const struct intel_context *ce,
1118 const struct intel_engine_cs *engine,
1121 struct intel_ring *ring = ce->ring;
1122 u32 *regs = ce->lrc_reg_state;
1124 GEM_BUG_ON(!intel_ring_offset_valid(ring, head));
1125 GEM_BUG_ON(!intel_ring_offset_valid(ring, ring->tail));
1127 regs[CTX_RING_START] = i915_ggtt_offset(ring->vma);
1128 regs[CTX_RING_HEAD] = head;
1129 regs[CTX_RING_TAIL] = ring->tail;
1130 regs[CTX_RING_CTL] = RING_CTL_SIZE(ring->size) | RING_VALID;
1133 if (engine->class == RENDER_CLASS) {
1134 regs[CTX_R_PWR_CLK_STATE] =
1135 intel_sseu_make_rpcs(engine->gt, &ce->sseu);
1137 i915_oa_init_reg_state(ce, engine);
1140 if (ce->wa_bb_page) {
1141 u32 *(*fn)(const struct intel_context *ce, u32 *cs);
1143 fn = gen12_emit_indirect_ctx_xcs;
1144 if (ce->engine->class == RENDER_CLASS)
1145 fn = gen12_emit_indirect_ctx_rcs;
1147 /* Mutually exclusive wrt to global indirect bb */
1148 GEM_BUG_ON(engine->wa_ctx.indirect_ctx.size);
1149 setup_indirect_ctx_bb(ce, engine, fn);
1152 return lrc_descriptor(ce) | CTX_DESC_FORCE_RESTORE;
1155 void lrc_update_offsets(struct intel_context *ce,
1156 struct intel_engine_cs *engine)
1158 set_offsets(ce->lrc_reg_state, reg_offsets(engine), engine, false);
1161 void lrc_check_regs(const struct intel_context *ce,
1162 const struct intel_engine_cs *engine,
1165 const struct intel_ring *ring = ce->ring;
1166 u32 *regs = ce->lrc_reg_state;
1170 if (regs[CTX_RING_START] != i915_ggtt_offset(ring->vma)) {
1171 pr_err("%s: context submitted with incorrect RING_START [%08x], expected %08x\n",
1173 regs[CTX_RING_START],
1174 i915_ggtt_offset(ring->vma));
1175 regs[CTX_RING_START] = i915_ggtt_offset(ring->vma);
1179 if ((regs[CTX_RING_CTL] & ~(RING_WAIT | RING_WAIT_SEMAPHORE)) !=
1180 (RING_CTL_SIZE(ring->size) | RING_VALID)) {
1181 pr_err("%s: context submitted with incorrect RING_CTL [%08x], expected %08x\n",
1184 (u32)(RING_CTL_SIZE(ring->size) | RING_VALID));
1185 regs[CTX_RING_CTL] = RING_CTL_SIZE(ring->size) | RING_VALID;
1189 x = lrc_ring_mi_mode(engine);
1190 if (x != -1 && regs[x + 1] & (regs[x + 1] >> 16) & STOP_RING) {
1191 pr_err("%s: context submitted with STOP_RING [%08x] in RING_MI_MODE\n",
1192 engine->name, regs[x + 1]);
1193 regs[x + 1] &= ~STOP_RING;
1194 regs[x + 1] |= STOP_RING << 16;
1198 WARN_ONCE(!valid, "Invalid lrc state found %s submission\n", when);
1202 * In this WA we need to set GEN8_L3SQCREG4[21:21] and reset it after
1203 * PIPE_CONTROL instruction. This is required for the flush to happen correctly
1204 * but there is a slight complication as this is applied in WA batch where the
1205 * values are only initialized once so we cannot take register value at the
1206 * beginning and reuse it further; hence we save its value to memory, upload a
1207 * constant value with bit21 set and then we restore it back with the saved value.
1208 * To simplify the WA, a constant value is formed by using the default value
1209 * of this register. This shouldn't be a problem because we are only modifying
1210 * it for a short period and this batch in non-premptible. We can ofcourse
1211 * use additional instructions that read the actual value of the register
1212 * at that time and set our bit of interest but it makes the WA complicated.
1214 * This WA is also required for Gen9 so extracting as a function avoids
1218 gen8_emit_flush_coherentl3_wa(struct intel_engine_cs *engine, u32 *batch)
1220 /* NB no one else is allowed to scribble over scratch + 256! */
1221 *batch++ = MI_STORE_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
1222 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1223 *batch++ = intel_gt_scratch_offset(engine->gt,
1224 INTEL_GT_SCRATCH_FIELD_COHERENTL3_WA);
1227 *batch++ = MI_LOAD_REGISTER_IMM(1);
1228 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1229 *batch++ = 0x40400000 | GEN8_LQSC_FLUSH_COHERENT_LINES;
1231 batch = gen8_emit_pipe_control(batch,
1232 PIPE_CONTROL_CS_STALL |
1233 PIPE_CONTROL_DC_FLUSH_ENABLE,
1236 *batch++ = MI_LOAD_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
1237 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1238 *batch++ = intel_gt_scratch_offset(engine->gt,
1239 INTEL_GT_SCRATCH_FIELD_COHERENTL3_WA);
1246 * Typically we only have one indirect_ctx and per_ctx batch buffer which are
1247 * initialized at the beginning and shared across all contexts but this field
1248 * helps us to have multiple batches at different offsets and select them based
1249 * on a criteria. At the moment this batch always start at the beginning of the page
1250 * and at this point we don't have multiple wa_ctx batch buffers.
1252 * The number of WA applied are not known at the beginning; we use this field
1253 * to return the no of DWORDS written.
1255 * It is to be noted that this batch does not contain MI_BATCH_BUFFER_END
1256 * so it adds NOOPs as padding to make it cacheline aligned.
1257 * MI_BATCH_BUFFER_END will be added to perctx batch and both of them together
1258 * makes a complete batch buffer.
1260 static u32 *gen8_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
1262 /* WaDisableCtxRestoreArbitration:bdw,chv */
1263 *batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
1265 /* WaFlushCoherentL3CacheLinesAtContextSwitch:bdw */
1266 if (IS_BROADWELL(engine->i915))
1267 batch = gen8_emit_flush_coherentl3_wa(engine, batch);
1269 /* WaClearSlmSpaceAtContextSwitch:bdw,chv */
1270 /* Actual scratch location is at 128 bytes offset */
1271 batch = gen8_emit_pipe_control(batch,
1272 PIPE_CONTROL_FLUSH_L3 |
1273 PIPE_CONTROL_STORE_DATA_INDEX |
1274 PIPE_CONTROL_CS_STALL |
1275 PIPE_CONTROL_QW_WRITE,
1276 LRC_PPHWSP_SCRATCH_ADDR);
1278 *batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1280 /* Pad to end of cacheline */
1281 while ((unsigned long)batch % CACHELINE_BYTES)
1285 * MI_BATCH_BUFFER_END is not required in Indirect ctx BB because
1286 * execution depends on the length specified in terms of cache lines
1287 * in the register CTX_RCS_INDIRECT_CTX
1298 static u32 *emit_lri(u32 *batch, const struct lri *lri, unsigned int count)
1300 GEM_BUG_ON(!count || count > 63);
1302 *batch++ = MI_LOAD_REGISTER_IMM(count);
1304 *batch++ = i915_mmio_reg_offset(lri->reg);
1305 *batch++ = lri->value;
1306 } while (lri++, --count);
1312 static u32 *gen9_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
1314 static const struct lri lri[] = {
1315 /* WaDisableGatherAtSetShaderCommonSlice:skl,bxt,kbl,glk */
1317 COMMON_SLICE_CHICKEN2,
1318 __MASKED_FIELD(GEN9_DISABLE_GATHER_AT_SET_SHADER_COMMON_SLICE,
1325 __MASKED_FIELD(FF_SLICE_CHICKEN_CL_PROVOKING_VERTEX_FIX,
1326 FF_SLICE_CHICKEN_CL_PROVOKING_VERTEX_FIX),
1332 __MASKED_FIELD(_3D_CHICKEN_SF_PROVOKING_VERTEX_FIX,
1333 _3D_CHICKEN_SF_PROVOKING_VERTEX_FIX),
1337 *batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
1339 /* WaFlushCoherentL3CacheLinesAtContextSwitch:skl,bxt,glk */
1340 batch = gen8_emit_flush_coherentl3_wa(engine, batch);
1342 /* WaClearSlmSpaceAtContextSwitch:skl,bxt,kbl,glk,cfl */
1343 batch = gen8_emit_pipe_control(batch,
1344 PIPE_CONTROL_FLUSH_L3 |
1345 PIPE_CONTROL_STORE_DATA_INDEX |
1346 PIPE_CONTROL_CS_STALL |
1347 PIPE_CONTROL_QW_WRITE,
1348 LRC_PPHWSP_SCRATCH_ADDR);
1350 batch = emit_lri(batch, lri, ARRAY_SIZE(lri));
1352 /* WaMediaPoolStateCmdInWABB:bxt,glk */
1353 if (HAS_POOLED_EU(engine->i915)) {
1355 * EU pool configuration is setup along with golden context
1356 * during context initialization. This value depends on
1357 * device type (2x6 or 3x6) and needs to be updated based
1358 * on which subslice is disabled especially for 2x6
1359 * devices, however it is safe to load default
1360 * configuration of 3x6 device instead of masking off
1361 * corresponding bits because HW ignores bits of a disabled
1362 * subslice and drops down to appropriate config. Please
1363 * see render_state_setup() in i915_gem_render_state.c for
1364 * possible configurations, to avoid duplication they are
1365 * not shown here again.
1367 *batch++ = GEN9_MEDIA_POOL_STATE;
1368 *batch++ = GEN9_MEDIA_POOL_ENABLE;
1369 *batch++ = 0x00777000;
1375 *batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1377 /* Pad to end of cacheline */
1378 while ((unsigned long)batch % CACHELINE_BYTES)
1385 gen10_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
1390 * WaPipeControlBefore3DStateSamplePattern: cnl
1392 * Ensure the engine is idle prior to programming a
1393 * 3DSTATE_SAMPLE_PATTERN during a context restore.
1395 batch = gen8_emit_pipe_control(batch,
1396 PIPE_CONTROL_CS_STALL,
1399 * WaPipeControlBefore3DStateSamplePattern says we need 4 dwords for
1400 * the PIPE_CONTROL followed by 12 dwords of 0x0, so 16 dwords in
1401 * total. However, a PIPE_CONTROL is 6 dwords long, not 4, which is
1402 * confusing. Since gen8_emit_pipe_control() already advances the
1403 * batch by 6 dwords, we advance the other 10 here, completing a
1404 * cacheline. It's not clear if the workaround requires this padding
1405 * before other commands, or if it's just the regular padding we would
1406 * already have for the workaround bb, so leave it here for now.
1408 for (i = 0; i < 10; i++)
1411 /* Pad to end of cacheline */
1412 while ((unsigned long)batch % CACHELINE_BYTES)
1418 #define CTX_WA_BB_SIZE (PAGE_SIZE)
1420 static int lrc_setup_wa_ctx(struct intel_engine_cs *engine)
1422 struct drm_i915_gem_object *obj;
1423 struct i915_vma *vma;
1426 obj = i915_gem_object_create_shmem(engine->i915, CTX_WA_BB_SIZE);
1428 return PTR_ERR(obj);
1430 vma = i915_vma_instance(obj, &engine->gt->ggtt->vm, NULL);
1436 err = i915_ggtt_pin(vma, NULL, 0, PIN_HIGH);
1440 engine->wa_ctx.vma = vma;
1444 i915_gem_object_put(obj);
1448 void lrc_fini_wa_ctx(struct intel_engine_cs *engine)
1450 i915_vma_unpin_and_release(&engine->wa_ctx.vma, 0);
1452 /* Called on error unwind, clear all flags to prevent further use */
1453 memset(&engine->wa_ctx, 0, sizeof(engine->wa_ctx));
1456 typedef u32 *(*wa_bb_func_t)(struct intel_engine_cs *engine, u32 *batch);
1458 void lrc_init_wa_ctx(struct intel_engine_cs *engine)
1460 struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
1461 struct i915_wa_ctx_bb *wa_bb[] = {
1462 &wa_ctx->indirect_ctx, &wa_ctx->per_ctx
1464 wa_bb_func_t wa_bb_fn[ARRAY_SIZE(wa_bb)];
1465 void *batch, *batch_ptr;
1469 if (engine->class != RENDER_CLASS)
1472 switch (INTEL_GEN(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(INTEL_GEN(engine->i915));
1493 err = lrc_setup_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 batch = i915_gem_object_pin_map(wa_ctx->vma->obj, I915_MAP_WB);
1509 * Emit the two workaround batch buffers, recording the offset from the
1510 * start of the workaround batch buffer object for each and their
1514 for (i = 0; i < ARRAY_SIZE(wa_bb_fn); i++) {
1515 wa_bb[i]->offset = batch_ptr - batch;
1516 if (GEM_DEBUG_WARN_ON(!IS_ALIGNED(wa_bb[i]->offset,
1517 CACHELINE_BYTES))) {
1522 batch_ptr = wa_bb_fn[i](engine, batch_ptr);
1523 wa_bb[i]->size = batch_ptr - (batch + wa_bb[i]->offset);
1525 GEM_BUG_ON(batch_ptr - batch > CTX_WA_BB_SIZE);
1527 __i915_gem_object_flush_map(wa_ctx->vma->obj, 0, batch_ptr - batch);
1528 __i915_gem_object_release_map(wa_ctx->vma->obj);
1530 /* Verify that we can handle failure to setup the wa_ctx */
1531 if (err || i915_inject_probe_error(engine->i915, -ENODEV))
1532 lrc_fini_wa_ctx(engine);
1535 static void st_update_runtime_underflow(struct intel_context *ce, s32 dt)
1537 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1538 ce->runtime.num_underflow++;
1539 ce->runtime.max_underflow = max_t(u32, ce->runtime.max_underflow, -dt);
1543 void lrc_update_runtime(struct intel_context *ce)
1548 if (intel_context_is_barrier(ce))
1551 old = ce->runtime.last;
1552 ce->runtime.last = lrc_get_runtime(ce);
1553 dt = ce->runtime.last - old;
1555 if (unlikely(dt < 0)) {
1556 CE_TRACE(ce, "runtime underflow: last=%u, new=%u, delta=%d\n",
1557 old, ce->runtime.last, dt);
1558 st_update_runtime_underflow(ce, dt);
1562 ewma_runtime_add(&ce->runtime.avg, dt);
1563 ce->runtime.total += dt;
1566 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1567 #include "selftest_lrc.c"