1 // SPDX-License-Identifier: MIT
3 * Copyright © 2014 Intel Corporation
6 #include "gem/i915_gem_internal.h"
9 #include "intel_renderstate.h"
10 #include "intel_context.h"
11 #include "intel_gpu_commands.h"
12 #include "intel_ring.h"
14 static const struct intel_renderstate_rodata *
15 render_state_get_rodata(const struct intel_engine_cs *engine)
17 if (engine->class != RENDER_CLASS)
20 switch (GRAPHICS_VER(engine->i915)) {
22 return &gen6_null_state;
24 return &gen7_null_state;
26 return &gen8_null_state;
28 return &gen9_null_state;
35 * Macro to add commands to auxiliary batch.
36 * This macro only checks for page overflow before inserting the commands,
37 * this is sufficient as the null state generator makes the final batch
38 * with two passes to build command and state separately. At this point
39 * the size of both are known and it compacts them by relocating the state
40 * right after the commands taking care of alignment so we should sufficient
41 * space below them for adding new commands.
43 #define OUT_BATCH(batch, i, val) \
45 if ((i) >= PAGE_SIZE / sizeof(u32)) \
47 (batch)[(i)++] = (val); \
50 static int render_state_setup(struct intel_renderstate *so,
51 struct drm_i915_private *i915)
53 const struct intel_renderstate_rodata *rodata = so->rodata;
54 unsigned int i = 0, reloc_index = 0;
58 d = i915_gem_object_pin_map(so->vma->obj, I915_MAP_WB);
62 while (i < rodata->batch_items) {
63 u32 s = rodata->batch[i];
65 if (i * 4 == rodata->reloc[reloc_index]) {
66 u64 r = s + i915_vma_offset(so->vma);
69 if (HAS_64BIT_RELOC(i915)) {
70 if (i + 1 >= rodata->batch_items ||
71 rodata->batch[i + 1] != 0)
84 if (rodata->reloc[reloc_index] != -1) {
85 drm_err(&i915->drm, "only %d relocs resolved\n", reloc_index);
89 so->batch_offset = i915_ggtt_offset(so->vma);
90 so->batch_size = rodata->batch_items * sizeof(u32);
92 while (i % CACHELINE_DWORDS)
93 OUT_BATCH(d, i, MI_NOOP);
95 so->aux_offset = i * sizeof(u32);
97 if (HAS_POOLED_EU(i915)) {
99 * We always program 3x6 pool config but depending upon which
100 * subslice is disabled HW drops down to appropriate config
103 * In the below table 2x6 config always refers to
104 * fused-down version, native 2x6 is not available and can
107 * SNo subslices config eu pool configuration
108 * -----------------------------------------------------------
109 * 1 3 subslices enabled (3x6) - 0x00777000 (9+9)
110 * 2 ss0 disabled (2x6) - 0x00777000 (3+9)
111 * 3 ss1 disabled (2x6) - 0x00770000 (6+6)
112 * 4 ss2 disabled (2x6) - 0x00007000 (9+3)
114 u32 eu_pool_config = 0x00777000;
116 OUT_BATCH(d, i, GEN9_MEDIA_POOL_STATE);
117 OUT_BATCH(d, i, GEN9_MEDIA_POOL_ENABLE);
118 OUT_BATCH(d, i, eu_pool_config);
124 OUT_BATCH(d, i, MI_BATCH_BUFFER_END);
125 so->aux_size = i * sizeof(u32) - so->aux_offset;
126 so->aux_offset += so->batch_offset;
128 * Since we are sending length, we need to strictly conform to
129 * all requirements. For Gen2 this must be a multiple of 8.
131 so->aux_size = ALIGN(so->aux_size, 8);
135 __i915_gem_object_flush_map(so->vma->obj, 0, i * sizeof(u32));
136 __i915_gem_object_release_map(so->vma->obj);
142 int intel_renderstate_init(struct intel_renderstate *so,
143 struct intel_context *ce)
145 struct intel_engine_cs *engine = ce->engine;
146 struct drm_i915_gem_object *obj = NULL;
149 memset(so, 0, sizeof(*so));
151 so->rodata = render_state_get_rodata(engine);
153 if (so->rodata->batch_items * 4 > PAGE_SIZE)
156 obj = i915_gem_object_create_internal(engine->i915, PAGE_SIZE);
160 so->vma = i915_vma_instance(obj, &engine->gt->ggtt->vm, NULL);
161 if (IS_ERR(so->vma)) {
162 err = PTR_ERR(so->vma);
167 i915_gem_ww_ctx_init(&so->ww, true);
169 err = intel_context_pin_ww(ce, &so->ww);
173 /* return early if there's nothing to setup */
174 if (!err && !so->rodata)
177 err = i915_gem_object_lock(so->vma->obj, &so->ww);
181 err = i915_vma_pin_ww(so->vma, &so->ww, 0, 0, PIN_GLOBAL | PIN_HIGH);
185 err = render_state_setup(so, engine->i915);
192 i915_vma_unpin(so->vma);
194 intel_context_unpin(ce);
196 if (err == -EDEADLK) {
197 err = i915_gem_ww_ctx_backoff(&so->ww);
201 i915_gem_ww_ctx_fini(&so->ww);
204 i915_gem_object_put(obj);
209 int intel_renderstate_emit(struct intel_renderstate *so,
210 struct i915_request *rq)
212 struct intel_engine_cs *engine = rq->engine;
218 err = i915_vma_move_to_active(so->vma, rq, 0);
222 err = engine->emit_bb_start(rq,
223 so->batch_offset, so->batch_size,
224 I915_DISPATCH_SECURE);
228 if (so->aux_size > 8) {
229 err = engine->emit_bb_start(rq,
230 so->aux_offset, so->aux_size,
231 I915_DISPATCH_SECURE);
239 void intel_renderstate_fini(struct intel_renderstate *so,
240 struct intel_context *ce)
243 i915_vma_unpin(so->vma);
244 i915_vma_close(so->vma);
247 intel_context_unpin(ce);
248 i915_gem_ww_ctx_fini(&so->ww);
251 i915_gem_object_put(so->vma->obj);