Home | History | Annotate | Line # | Download | only in gt
intel_renderstate.c revision 1.1
      1 /*	$NetBSD: intel_renderstate.c,v 1.1 2021/12/18 20:15:32 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright  2014 Intel Corporation
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a
      7  * copy of this software and associated documentation files (the "Software"),
      8  * to deal in the Software without restriction, including without limitation
      9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  * and/or sell copies of the Software, and to permit persons to whom the
     11  * Software is furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice (including the next
     14  * paragraph) shall be included in all copies or substantial portions of the
     15  * Software.
     16  *
     17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     23  * IN THE SOFTWARE.
     24  *
     25  * Authors:
     26  *    Mika Kuoppala <mika.kuoppala (at) intel.com>
     27  *
     28  */
     29 
     30 #include <sys/cdefs.h>
     31 __KERNEL_RCSID(0, "$NetBSD: intel_renderstate.c,v 1.1 2021/12/18 20:15:32 riastradh Exp $");
     32 
     33 #include "i915_drv.h"
     34 #include "intel_renderstate.h"
     35 #include "intel_ring.h"
     36 
     37 static const struct intel_renderstate_rodata *
     38 render_state_get_rodata(const struct intel_engine_cs *engine)
     39 {
     40 	if (engine->class != RENDER_CLASS)
     41 		return NULL;
     42 
     43 	switch (INTEL_GEN(engine->i915)) {
     44 	case 6:
     45 		return &gen6_null_state;
     46 	case 7:
     47 		return &gen7_null_state;
     48 	case 8:
     49 		return &gen8_null_state;
     50 	case 9:
     51 		return &gen9_null_state;
     52 	}
     53 
     54 	return NULL;
     55 }
     56 
     57 /*
     58  * Macro to add commands to auxiliary batch.
     59  * This macro only checks for page overflow before inserting the commands,
     60  * this is sufficient as the null state generator makes the final batch
     61  * with two passes to build command and state separately. At this point
     62  * the size of both are known and it compacts them by relocating the state
     63  * right after the commands taking care of alignment so we should sufficient
     64  * space below them for adding new commands.
     65  */
     66 #define OUT_BATCH(batch, i, val)				\
     67 	do {							\
     68 		if ((i) >= PAGE_SIZE / sizeof(u32))		\
     69 			goto err;				\
     70 		(batch)[(i)++] = (val);				\
     71 	} while(0)
     72 
     73 static int render_state_setup(struct intel_renderstate *so,
     74 			      struct drm_i915_private *i915)
     75 {
     76 	const struct intel_renderstate_rodata *rodata = so->rodata;
     77 	unsigned int i = 0, reloc_index = 0;
     78 	unsigned int needs_clflush;
     79 	u32 *d;
     80 	int ret;
     81 
     82 	ret = i915_gem_object_prepare_write(so->vma->obj, &needs_clflush);
     83 	if (ret)
     84 		return ret;
     85 
     86 	d = kmap_atomic(i915_gem_object_get_dirty_page(so->vma->obj, 0));
     87 
     88 	while (i < rodata->batch_items) {
     89 		u32 s = rodata->batch[i];
     90 
     91 		if (i * 4  == rodata->reloc[reloc_index]) {
     92 			u64 r = s + so->vma->node.start;
     93 			s = lower_32_bits(r);
     94 			if (HAS_64BIT_RELOC(i915)) {
     95 				if (i + 1 >= rodata->batch_items ||
     96 				    rodata->batch[i + 1] != 0)
     97 					goto err;
     98 
     99 				d[i++] = s;
    100 				s = upper_32_bits(r);
    101 			}
    102 
    103 			reloc_index++;
    104 		}
    105 
    106 		d[i++] = s;
    107 	}
    108 
    109 	if (rodata->reloc[reloc_index] != -1) {
    110 		DRM_ERROR("only %d relocs resolved\n", reloc_index);
    111 		goto err;
    112 	}
    113 
    114 	so->batch_offset = i915_ggtt_offset(so->vma);
    115 	so->batch_size = rodata->batch_items * sizeof(u32);
    116 
    117 	while (i % CACHELINE_DWORDS)
    118 		OUT_BATCH(d, i, MI_NOOP);
    119 
    120 	so->aux_offset = i * sizeof(u32);
    121 
    122 	if (HAS_POOLED_EU(i915)) {
    123 		/*
    124 		 * We always program 3x6 pool config but depending upon which
    125 		 * subslice is disabled HW drops down to appropriate config
    126 		 * shown below.
    127 		 *
    128 		 * In the below table 2x6 config always refers to
    129 		 * fused-down version, native 2x6 is not available and can
    130 		 * be ignored
    131 		 *
    132 		 * SNo  subslices config                eu pool configuration
    133 		 * -----------------------------------------------------------
    134 		 * 1    3 subslices enabled (3x6)  -    0x00777000  (9+9)
    135 		 * 2    ss0 disabled (2x6)         -    0x00777000  (3+9)
    136 		 * 3    ss1 disabled (2x6)         -    0x00770000  (6+6)
    137 		 * 4    ss2 disabled (2x6)         -    0x00007000  (9+3)
    138 		 */
    139 		u32 eu_pool_config = 0x00777000;
    140 
    141 		OUT_BATCH(d, i, GEN9_MEDIA_POOL_STATE);
    142 		OUT_BATCH(d, i, GEN9_MEDIA_POOL_ENABLE);
    143 		OUT_BATCH(d, i, eu_pool_config);
    144 		OUT_BATCH(d, i, 0);
    145 		OUT_BATCH(d, i, 0);
    146 		OUT_BATCH(d, i, 0);
    147 	}
    148 
    149 	OUT_BATCH(d, i, MI_BATCH_BUFFER_END);
    150 	so->aux_size = i * sizeof(u32) - so->aux_offset;
    151 	so->aux_offset += so->batch_offset;
    152 	/*
    153 	 * Since we are sending length, we need to strictly conform to
    154 	 * all requirements. For Gen2 this must be a multiple of 8.
    155 	 */
    156 	so->aux_size = ALIGN(so->aux_size, 8);
    157 
    158 	if (needs_clflush)
    159 		drm_clflush_virt_range(d, i * sizeof(u32));
    160 	kunmap_atomic(d);
    161 
    162 	ret = 0;
    163 out:
    164 	i915_gem_object_finish_access(so->vma->obj);
    165 	return ret;
    166 
    167 err:
    168 	kunmap_atomic(d);
    169 	ret = -EINVAL;
    170 	goto out;
    171 }
    172 
    173 #undef OUT_BATCH
    174 
    175 int intel_renderstate_init(struct intel_renderstate *so,
    176 			   struct intel_engine_cs *engine)
    177 {
    178 	struct drm_i915_gem_object *obj;
    179 	int err;
    180 
    181 	memset(so, 0, sizeof(*so));
    182 
    183 	so->rodata = render_state_get_rodata(engine);
    184 	if (!so->rodata)
    185 		return 0;
    186 
    187 	if (so->rodata->batch_items * 4 > PAGE_SIZE)
    188 		return -EINVAL;
    189 
    190 	obj = i915_gem_object_create_internal(engine->i915, PAGE_SIZE);
    191 	if (IS_ERR(obj))
    192 		return PTR_ERR(obj);
    193 
    194 	so->vma = i915_vma_instance(obj, &engine->gt->ggtt->vm, NULL);
    195 	if (IS_ERR(so->vma)) {
    196 		err = PTR_ERR(so->vma);
    197 		goto err_obj;
    198 	}
    199 
    200 	err = i915_vma_pin(so->vma, 0, 0, PIN_GLOBAL | PIN_HIGH);
    201 	if (err)
    202 		goto err_vma;
    203 
    204 	err = render_state_setup(so, engine->i915);
    205 	if (err)
    206 		goto err_unpin;
    207 
    208 	return 0;
    209 
    210 err_unpin:
    211 	i915_vma_unpin(so->vma);
    212 err_vma:
    213 	i915_vma_close(so->vma);
    214 err_obj:
    215 	i915_gem_object_put(obj);
    216 	so->vma = NULL;
    217 	return err;
    218 }
    219 
    220 int intel_renderstate_emit(struct intel_renderstate *so,
    221 			   struct i915_request *rq)
    222 {
    223 	struct intel_engine_cs *engine = rq->engine;
    224 	int err;
    225 
    226 	if (!so->vma)
    227 		return 0;
    228 
    229 	err = engine->emit_bb_start(rq,
    230 				    so->batch_offset, so->batch_size,
    231 				    I915_DISPATCH_SECURE);
    232 	if (err)
    233 		return err;
    234 
    235 	if (so->aux_size > 8) {
    236 		err = engine->emit_bb_start(rq,
    237 					    so->aux_offset, so->aux_size,
    238 					    I915_DISPATCH_SECURE);
    239 		if (err)
    240 			return err;
    241 	}
    242 
    243 	i915_vma_lock(so->vma);
    244 	err = i915_request_await_object(rq, so->vma->obj, false);
    245 	if (err == 0)
    246 		err = i915_vma_move_to_active(so->vma, rq, 0);
    247 	i915_vma_unlock(so->vma);
    248 
    249 	return err;
    250 }
    251 
    252 void intel_renderstate_fini(struct intel_renderstate *so)
    253 {
    254 	i915_vma_unpin_and_release(&so->vma, 0);
    255 }
    256