124f90d66SChris Wilson // SPDX-License-Identifier: MIT
22006058eSChris Wilson /*
32006058eSChris Wilson * Copyright © 2014 Intel Corporation
42006058eSChris Wilson */
52006058eSChris Wilson
6b508d01fSJani Nikula #include "gem/i915_gem_internal.h"
7b508d01fSJani Nikula
82006058eSChris Wilson #include "i915_drv.h"
92006058eSChris Wilson #include "intel_renderstate.h"
1045233ab2SChris Wilson #include "intel_context.h"
1145233ab2SChris Wilson #include "intel_gpu_commands.h"
122871ea85SChris Wilson #include "intel_ring.h"
132006058eSChris Wilson
142006058eSChris Wilson static const struct intel_renderstate_rodata *
render_state_get_rodata(const struct intel_engine_cs * engine)152006058eSChris Wilson render_state_get_rodata(const struct intel_engine_cs *engine)
162006058eSChris Wilson {
17a5627721SChris Wilson if (engine->class != RENDER_CLASS)
182006058eSChris Wilson return NULL;
192006058eSChris Wilson
20c816723bSLucas De Marchi switch (GRAPHICS_VER(engine->i915)) {
212006058eSChris Wilson case 6:
222006058eSChris Wilson return &gen6_null_state;
232006058eSChris Wilson case 7:
242006058eSChris Wilson return &gen7_null_state;
252006058eSChris Wilson case 8:
262006058eSChris Wilson return &gen8_null_state;
272006058eSChris Wilson case 9:
282006058eSChris Wilson return &gen9_null_state;
292006058eSChris Wilson }
302006058eSChris Wilson
312006058eSChris Wilson return NULL;
322006058eSChris Wilson }
332006058eSChris Wilson
342006058eSChris Wilson /*
352006058eSChris Wilson * Macro to add commands to auxiliary batch.
362006058eSChris Wilson * This macro only checks for page overflow before inserting the commands,
372006058eSChris Wilson * this is sufficient as the null state generator makes the final batch
382006058eSChris Wilson * with two passes to build command and state separately. At this point
392006058eSChris Wilson * the size of both are known and it compacts them by relocating the state
402006058eSChris Wilson * right after the commands taking care of alignment so we should sufficient
412006058eSChris Wilson * space below them for adding new commands.
422006058eSChris Wilson */
432006058eSChris Wilson #define OUT_BATCH(batch, i, val) \
442006058eSChris Wilson do { \
452006058eSChris Wilson if ((i) >= PAGE_SIZE / sizeof(u32)) \
46cf46143fSChris Wilson goto out; \
472006058eSChris Wilson (batch)[(i)++] = (val); \
482006058eSChris Wilson } while (0)
492006058eSChris Wilson
render_state_setup(struct intel_renderstate * so,struct drm_i915_private * i915)502006058eSChris Wilson static int render_state_setup(struct intel_renderstate *so,
512006058eSChris Wilson struct drm_i915_private *i915)
522006058eSChris Wilson {
532006058eSChris Wilson const struct intel_renderstate_rodata *rodata = so->rodata;
542006058eSChris Wilson unsigned int i = 0, reloc_index = 0;
55cf46143fSChris Wilson int ret = -EINVAL;
562006058eSChris Wilson u32 *d;
572006058eSChris Wilson
58cf46143fSChris Wilson d = i915_gem_object_pin_map(so->vma->obj, I915_MAP_WB);
59cf46143fSChris Wilson if (IS_ERR(d))
60cf46143fSChris Wilson return PTR_ERR(d);
612006058eSChris Wilson
622006058eSChris Wilson while (i < rodata->batch_items) {
632006058eSChris Wilson u32 s = rodata->batch[i];
642006058eSChris Wilson
652006058eSChris Wilson if (i * 4 == rodata->reloc[reloc_index]) {
66*8e4ee5e8SChris Wilson u64 r = s + i915_vma_offset(so->vma);
672f8aa3b8SChris Wilson
682006058eSChris Wilson s = lower_32_bits(r);
692006058eSChris Wilson if (HAS_64BIT_RELOC(i915)) {
702006058eSChris Wilson if (i + 1 >= rodata->batch_items ||
712006058eSChris Wilson rodata->batch[i + 1] != 0)
72cf46143fSChris Wilson goto out;
732006058eSChris Wilson
742006058eSChris Wilson d[i++] = s;
752006058eSChris Wilson s = upper_32_bits(r);
762006058eSChris Wilson }
772006058eSChris Wilson
782006058eSChris Wilson reloc_index++;
792006058eSChris Wilson }
802006058eSChris Wilson
812006058eSChris Wilson d[i++] = s;
822006058eSChris Wilson }
832006058eSChris Wilson
842006058eSChris Wilson if (rodata->reloc[reloc_index] != -1) {
85edf040f4SWambui Karuga drm_err(&i915->drm, "only %d relocs resolved\n", reloc_index);
86cf46143fSChris Wilson goto out;
872006058eSChris Wilson }
882006058eSChris Wilson
892006058eSChris Wilson so->batch_offset = i915_ggtt_offset(so->vma);
902006058eSChris Wilson so->batch_size = rodata->batch_items * sizeof(u32);
912006058eSChris Wilson
922006058eSChris Wilson while (i % CACHELINE_DWORDS)
932006058eSChris Wilson OUT_BATCH(d, i, MI_NOOP);
942006058eSChris Wilson
952006058eSChris Wilson so->aux_offset = i * sizeof(u32);
962006058eSChris Wilson
972006058eSChris Wilson if (HAS_POOLED_EU(i915)) {
982006058eSChris Wilson /*
992006058eSChris Wilson * We always program 3x6 pool config but depending upon which
1002006058eSChris Wilson * subslice is disabled HW drops down to appropriate config
1012006058eSChris Wilson * shown below.
1022006058eSChris Wilson *
1032006058eSChris Wilson * In the below table 2x6 config always refers to
1042006058eSChris Wilson * fused-down version, native 2x6 is not available and can
1052006058eSChris Wilson * be ignored
1062006058eSChris Wilson *
1072006058eSChris Wilson * SNo subslices config eu pool configuration
1082006058eSChris Wilson * -----------------------------------------------------------
1092006058eSChris Wilson * 1 3 subslices enabled (3x6) - 0x00777000 (9+9)
1102006058eSChris Wilson * 2 ss0 disabled (2x6) - 0x00777000 (3+9)
1112006058eSChris Wilson * 3 ss1 disabled (2x6) - 0x00770000 (6+6)
1122006058eSChris Wilson * 4 ss2 disabled (2x6) - 0x00007000 (9+3)
1132006058eSChris Wilson */
1142006058eSChris Wilson u32 eu_pool_config = 0x00777000;
1152006058eSChris Wilson
1162006058eSChris Wilson OUT_BATCH(d, i, GEN9_MEDIA_POOL_STATE);
1172006058eSChris Wilson OUT_BATCH(d, i, GEN9_MEDIA_POOL_ENABLE);
1182006058eSChris Wilson OUT_BATCH(d, i, eu_pool_config);
1192006058eSChris Wilson OUT_BATCH(d, i, 0);
1202006058eSChris Wilson OUT_BATCH(d, i, 0);
1212006058eSChris Wilson OUT_BATCH(d, i, 0);
1222006058eSChris Wilson }
1232006058eSChris Wilson
1242006058eSChris Wilson OUT_BATCH(d, i, MI_BATCH_BUFFER_END);
1252006058eSChris Wilson so->aux_size = i * sizeof(u32) - so->aux_offset;
1262006058eSChris Wilson so->aux_offset += so->batch_offset;
1272006058eSChris Wilson /*
1282006058eSChris Wilson * Since we are sending length, we need to strictly conform to
1292006058eSChris Wilson * all requirements. For Gen2 this must be a multiple of 8.
1302006058eSChris Wilson */
1312006058eSChris Wilson so->aux_size = ALIGN(so->aux_size, 8);
1322006058eSChris Wilson
1332006058eSChris Wilson ret = 0;
1342006058eSChris Wilson out:
135cf46143fSChris Wilson __i915_gem_object_flush_map(so->vma->obj, 0, i * sizeof(u32));
13689d19b2bSChris Wilson __i915_gem_object_release_map(so->vma->obj);
1372006058eSChris Wilson return ret;
1382006058eSChris Wilson }
1392006058eSChris Wilson
1402006058eSChris Wilson #undef OUT_BATCH
1412006058eSChris Wilson
intel_renderstate_init(struct intel_renderstate * so,struct intel_context * ce)14242d10511SChris Wilson int intel_renderstate_init(struct intel_renderstate *so,
143bfdf8b1dSMaarten Lankhorst struct intel_context *ce)
1442006058eSChris Wilson {
145bfdf8b1dSMaarten Lankhorst struct intel_engine_cs *engine = ce->engine;
146bfdf8b1dSMaarten Lankhorst struct drm_i915_gem_object *obj = NULL;
1472006058eSChris Wilson int err;
1482006058eSChris Wilson
14942d10511SChris Wilson memset(so, 0, sizeof(*so));
15042d10511SChris Wilson
15142d10511SChris Wilson so->rodata = render_state_get_rodata(engine);
152bfdf8b1dSMaarten Lankhorst if (so->rodata) {
15342d10511SChris Wilson if (so->rodata->batch_items * 4 > PAGE_SIZE)
1542006058eSChris Wilson return -EINVAL;
1552006058eSChris Wilson
15642d10511SChris Wilson obj = i915_gem_object_create_internal(engine->i915, PAGE_SIZE);
15742d10511SChris Wilson if (IS_ERR(obj))
15842d10511SChris Wilson return PTR_ERR(obj);
1592006058eSChris Wilson
16042d10511SChris Wilson so->vma = i915_vma_instance(obj, &engine->gt->ggtt->vm, NULL);
16142d10511SChris Wilson if (IS_ERR(so->vma)) {
16242d10511SChris Wilson err = PTR_ERR(so->vma);
1632006058eSChris Wilson goto err_obj;
1642006058eSChris Wilson }
165bfdf8b1dSMaarten Lankhorst }
166bfdf8b1dSMaarten Lankhorst
167bfdf8b1dSMaarten Lankhorst i915_gem_ww_ctx_init(&so->ww, true);
168bfdf8b1dSMaarten Lankhorst retry:
16947b08693SMaarten Lankhorst err = intel_context_pin_ww(ce, &so->ww);
170bfdf8b1dSMaarten Lankhorst if (err)
171bfdf8b1dSMaarten Lankhorst goto err_fini;
172bfdf8b1dSMaarten Lankhorst
173bfdf8b1dSMaarten Lankhorst /* return early if there's nothing to setup */
174bfdf8b1dSMaarten Lankhorst if (!err && !so->rodata)
175bfdf8b1dSMaarten Lankhorst return 0;
176bfdf8b1dSMaarten Lankhorst
177bfdf8b1dSMaarten Lankhorst err = i915_gem_object_lock(so->vma->obj, &so->ww);
178bfdf8b1dSMaarten Lankhorst if (err)
179bfdf8b1dSMaarten Lankhorst goto err_context;
1802006058eSChris Wilson
1811eef0de1SMaarten Lankhorst err = i915_vma_pin_ww(so->vma, &so->ww, 0, 0, PIN_GLOBAL | PIN_HIGH);
1822006058eSChris Wilson if (err)
183bfdf8b1dSMaarten Lankhorst goto err_context;
1842006058eSChris Wilson
18542d10511SChris Wilson err = render_state_setup(so, engine->i915);
1862006058eSChris Wilson if (err)
1872006058eSChris Wilson goto err_unpin;
1882006058eSChris Wilson
18942d10511SChris Wilson return 0;
1902006058eSChris Wilson
19142d10511SChris Wilson err_unpin:
19242d10511SChris Wilson i915_vma_unpin(so->vma);
193bfdf8b1dSMaarten Lankhorst err_context:
194bfdf8b1dSMaarten Lankhorst intel_context_unpin(ce);
195bfdf8b1dSMaarten Lankhorst err_fini:
196bfdf8b1dSMaarten Lankhorst if (err == -EDEADLK) {
197bfdf8b1dSMaarten Lankhorst err = i915_gem_ww_ctx_backoff(&so->ww);
198bfdf8b1dSMaarten Lankhorst if (!err)
199bfdf8b1dSMaarten Lankhorst goto retry;
200bfdf8b1dSMaarten Lankhorst }
201bfdf8b1dSMaarten Lankhorst i915_gem_ww_ctx_fini(&so->ww);
20242d10511SChris Wilson err_obj:
203bfdf8b1dSMaarten Lankhorst if (obj)
20442d10511SChris Wilson i915_gem_object_put(obj);
20542d10511SChris Wilson so->vma = NULL;
20642d10511SChris Wilson return err;
2072006058eSChris Wilson }
2082006058eSChris Wilson
intel_renderstate_emit(struct intel_renderstate * so,struct i915_request * rq)20942d10511SChris Wilson int intel_renderstate_emit(struct intel_renderstate *so,
21042d10511SChris Wilson struct i915_request *rq)
21142d10511SChris Wilson {
21242d10511SChris Wilson struct intel_engine_cs *engine = rq->engine;
21342d10511SChris Wilson int err;
21442d10511SChris Wilson
21542d10511SChris Wilson if (!so->vma)
21642d10511SChris Wilson return 0;
21742d10511SChris Wilson
218b0a997aeSChris Wilson err = i915_vma_move_to_active(so->vma, rq, 0);
219b0a997aeSChris Wilson if (err)
220b0a997aeSChris Wilson return err;
221b0a997aeSChris Wilson
22242d10511SChris Wilson err = engine->emit_bb_start(rq,
22342d10511SChris Wilson so->batch_offset, so->batch_size,
22442d10511SChris Wilson I915_DISPATCH_SECURE);
22542d10511SChris Wilson if (err)
2262006058eSChris Wilson return err;
22742d10511SChris Wilson
22842d10511SChris Wilson if (so->aux_size > 8) {
22942d10511SChris Wilson err = engine->emit_bb_start(rq,
23042d10511SChris Wilson so->aux_offset, so->aux_size,
23142d10511SChris Wilson I915_DISPATCH_SECURE);
23242d10511SChris Wilson if (err)
23342d10511SChris Wilson return err;
23442d10511SChris Wilson }
23542d10511SChris Wilson
236b0a997aeSChris Wilson return 0;
23742d10511SChris Wilson }
23842d10511SChris Wilson
intel_renderstate_fini(struct intel_renderstate * so,struct intel_context * ce)239bfdf8b1dSMaarten Lankhorst void intel_renderstate_fini(struct intel_renderstate *so,
240bfdf8b1dSMaarten Lankhorst struct intel_context *ce)
24142d10511SChris Wilson {
242bfdf8b1dSMaarten Lankhorst if (so->vma) {
243bfdf8b1dSMaarten Lankhorst i915_vma_unpin(so->vma);
244bfdf8b1dSMaarten Lankhorst i915_vma_close(so->vma);
245bfdf8b1dSMaarten Lankhorst }
246bfdf8b1dSMaarten Lankhorst
247bfdf8b1dSMaarten Lankhorst intel_context_unpin(ce);
248bfdf8b1dSMaarten Lankhorst i915_gem_ww_ctx_fini(&so->ww);
249bfdf8b1dSMaarten Lankhorst
250bfdf8b1dSMaarten Lankhorst if (so->vma)
251bfdf8b1dSMaarten Lankhorst i915_gem_object_put(so->vma->obj);
2522006058eSChris Wilson }
253