1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2021 Ilia Mirkin <imirkin@alum.mit.edu>
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21bf215546Sopenharmony_ci * SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci * Authors:
24bf215546Sopenharmony_ci *    Ilia Mirkin <imirkin@alum.mit.edu>
25bf215546Sopenharmony_ci */
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "pipe/p_state.h"
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci#include "freedreno_resource.h"
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci#include "fd4_compute.h"
32bf215546Sopenharmony_ci#include "fd4_context.h"
33bf215546Sopenharmony_ci#include "fd4_emit.h"
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci/* maybe move to fd4_program? */
36bf215546Sopenharmony_cistatic void
37bf215546Sopenharmony_cics_program_emit(struct fd_ringbuffer *ring, struct ir3_shader_variant *v)
38bf215546Sopenharmony_ci{
39bf215546Sopenharmony_ci   const struct ir3_info *i = &v->info;
40bf215546Sopenharmony_ci   enum a3xx_threadsize thrsz = i->double_threadsize ? FOUR_QUADS : TWO_QUADS;
41bf215546Sopenharmony_ci   unsigned instrlen = v->instrlen;
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci   /* XXX verify that this is the case on a4xx */
44bf215546Sopenharmony_ci   /* if shader is more than 32*16 instructions, don't preload it.  Similar
45bf215546Sopenharmony_ci    * to the combined restriction of 64*16 for VS+FS
46bf215546Sopenharmony_ci    */
47bf215546Sopenharmony_ci   if (instrlen > 32)
48bf215546Sopenharmony_ci      instrlen = 0;
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ci   OUT_PKT0(ring, REG_A4XX_SP_SP_CTRL_REG, 1);
51bf215546Sopenharmony_ci   OUT_RING(ring, 0x00860010); /* SP_SP_CTRL_REG */
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_ci   OUT_PKT0(ring, REG_A4XX_HLSQ_CONTROL_0_REG, 1);
54bf215546Sopenharmony_ci   OUT_RING(ring, A4XX_HLSQ_CONTROL_0_REG_FSTHREADSIZE(TWO_QUADS) |
55bf215546Sopenharmony_ci                     A4XX_HLSQ_CONTROL_0_REG_SINGLECONTEXT |
56bf215546Sopenharmony_ci                     0x000001a0 /* XXX */);
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci   OUT_PKT0(ring, REG_A4XX_SP_CS_CTRL_REG0, 1);
59bf215546Sopenharmony_ci   OUT_RING(ring, A4XX_SP_CS_CTRL_REG0_THREADSIZE(thrsz) |
60bf215546Sopenharmony_ci                     A4XX_SP_CS_CTRL_REG0_SUPERTHREADMODE |
61bf215546Sopenharmony_ci                     A4XX_SP_CS_CTRL_REG0_HALFREGFOOTPRINT(i->max_half_reg + 1) |
62bf215546Sopenharmony_ci                     A4XX_SP_CS_CTRL_REG0_FULLREGFOOTPRINT(i->max_reg + 1));
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci   OUT_PKT0(ring, REG_A4XX_HLSQ_UPDATE_CONTROL, 1);
65bf215546Sopenharmony_ci   OUT_RING(ring, 0x00000038); /* HLSQ_UPDATE_CONTROL */
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_ci   OUT_PKT0(ring, REG_A4XX_HLSQ_CS_CONTROL_REG, 1);
68bf215546Sopenharmony_ci   OUT_RING(ring, A4XX_HLSQ_CS_CONTROL_REG_CONSTOBJECTOFFSET(0) |
69bf215546Sopenharmony_ci                     A4XX_HLSQ_CS_CONTROL_REG_SHADEROBJOFFSET(0) |
70bf215546Sopenharmony_ci                     A4XX_HLSQ_CS_CONTROL_REG_ENABLED |
71bf215546Sopenharmony_ci                     A4XX_HLSQ_CS_CONTROL_REG_INSTRLENGTH(1) |
72bf215546Sopenharmony_ci                     COND(v->has_ssbo, A4XX_HLSQ_CS_CONTROL_REG_SSBO_ENABLE) |
73bf215546Sopenharmony_ci                     A4XX_HLSQ_CS_CONTROL_REG_CONSTLENGTH(v->constlen / 4));
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci   uint32_t driver_param_base = v->const_state->offsets.driver_param * 4;
76bf215546Sopenharmony_ci   uint32_t local_invocation_id, work_group_id, local_group_size_id,
77bf215546Sopenharmony_ci      num_wg_id, work_dim_id, unused_id;
78bf215546Sopenharmony_ci   local_invocation_id =
79bf215546Sopenharmony_ci      ir3_find_sysval_regid(v, SYSTEM_VALUE_LOCAL_INVOCATION_ID);
80bf215546Sopenharmony_ci   work_group_id = driver_param_base + IR3_DP_WORKGROUP_ID_X;
81bf215546Sopenharmony_ci   num_wg_id = driver_param_base + IR3_DP_NUM_WORK_GROUPS_X;
82bf215546Sopenharmony_ci   local_group_size_id = driver_param_base + IR3_DP_LOCAL_GROUP_SIZE_X;
83bf215546Sopenharmony_ci   work_dim_id = driver_param_base + IR3_DP_WORK_DIM;
84bf215546Sopenharmony_ci   /* NOTE: At some point we'll want to use this, it's probably WGOFFSETCONSTID */
85bf215546Sopenharmony_ci   unused_id = driver_param_base + IR3_DP_BASE_GROUP_X;
86bf215546Sopenharmony_ci
87bf215546Sopenharmony_ci   OUT_PKT0(ring, REG_A4XX_HLSQ_CL_CONTROL_0, 2);
88bf215546Sopenharmony_ci   OUT_RING(ring, A4XX_HLSQ_CL_CONTROL_0_WGIDCONSTID(work_group_id) |
89bf215546Sopenharmony_ci                     A4XX_HLSQ_CL_CONTROL_0_KERNELDIMCONSTID(work_dim_id) |
90bf215546Sopenharmony_ci                     A4XX_HLSQ_CL_CONTROL_0_LOCALIDREGID(local_invocation_id));
91bf215546Sopenharmony_ci   OUT_RING(ring, A4XX_HLSQ_CL_CONTROL_1_UNK0CONSTID(unused_id) |
92bf215546Sopenharmony_ci                     A4XX_HLSQ_CL_CONTROL_1_WORKGROUPSIZECONSTID(local_group_size_id));
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_ci   OUT_PKT0(ring, REG_A4XX_HLSQ_CL_KERNEL_CONST, 1);
95bf215546Sopenharmony_ci   OUT_RING(ring, A4XX_HLSQ_CL_KERNEL_CONST_UNK0CONSTID(unused_id) |
96bf215546Sopenharmony_ci                     A4XX_HLSQ_CL_KERNEL_CONST_NUMWGCONSTID(num_wg_id));
97bf215546Sopenharmony_ci
98bf215546Sopenharmony_ci   OUT_PKT0(ring, REG_A4XX_HLSQ_CL_WG_OFFSET, 1);
99bf215546Sopenharmony_ci   OUT_RING(ring, A4XX_HLSQ_CL_WG_OFFSET_UNK0CONSTID(unused_id));
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci   OUT_PKT0(ring, REG_A4XX_HLSQ_MODE_CONTROL, 1);
102bf215546Sopenharmony_ci   OUT_RING(ring, 0x00000003); /* HLSQ_MODE_CONTROL */
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ci   OUT_PKT0(ring, REG_A4XX_HLSQ_UPDATE_CONTROL, 1);
105bf215546Sopenharmony_ci   OUT_RING(ring, 0x00000000); /* HLSQ_UPDATE_CONTROL */
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_ci   OUT_PKT0(ring, REG_A4XX_SP_CS_OBJ_START, 1);
108bf215546Sopenharmony_ci   OUT_RELOC(ring, v->bo, 0, 0, 0); /* SP_CS_OBJ_START */
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_ci   OUT_PKT0(ring, REG_A4XX_SP_CS_LENGTH_REG, 1);
111bf215546Sopenharmony_ci   OUT_RING(ring, v->instrlen);
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_ci   if (instrlen > 0)
114bf215546Sopenharmony_ci      fd4_emit_shader(ring, v);
115bf215546Sopenharmony_ci}
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_cistatic void
118bf215546Sopenharmony_cifd4_launch_grid(struct fd_context *ctx,
119bf215546Sopenharmony_ci                const struct pipe_grid_info *info) assert_dt
120bf215546Sopenharmony_ci{
121bf215546Sopenharmony_ci   struct fd4_context *fd4_ctx = fd4_context(ctx);
122bf215546Sopenharmony_ci   struct ir3_shader_key key = {
123bf215546Sopenharmony_ci      .has_per_samp = fd4_ctx->castc_srgb,
124bf215546Sopenharmony_ci      .fastc_srgb = fd4_ctx->castc_srgb,
125bf215546Sopenharmony_ci   };
126bf215546Sopenharmony_ci   struct ir3_shader *shader = ir3_get_shader(ctx->compute);
127bf215546Sopenharmony_ci   struct ir3_shader_variant *v;
128bf215546Sopenharmony_ci   struct fd_ringbuffer *ring = ctx->batch->draw;
129bf215546Sopenharmony_ci   unsigned nglobal = 0;
130bf215546Sopenharmony_ci
131bf215546Sopenharmony_ci   if (ir3_get_shader_info(ctx->compute)->uses_texture_gather) {
132bf215546Sopenharmony_ci      key.has_per_samp = true;
133bf215546Sopenharmony_ci      memcpy(key.fsampler_swizzles, fd4_ctx->csampler_swizzles,
134bf215546Sopenharmony_ci             sizeof(key.fsampler_swizzles));
135bf215546Sopenharmony_ci   }
136bf215546Sopenharmony_ci
137bf215546Sopenharmony_ci   v = ir3_shader_variant(shader, key, false, &ctx->debug);
138bf215546Sopenharmony_ci   if (!v)
139bf215546Sopenharmony_ci      return;
140bf215546Sopenharmony_ci
141bf215546Sopenharmony_ci   if (ctx->dirty_shader[PIPE_SHADER_COMPUTE] & FD_DIRTY_SHADER_PROG)
142bf215546Sopenharmony_ci      cs_program_emit(ring, v);
143bf215546Sopenharmony_ci
144bf215546Sopenharmony_ci   fd4_emit_cs_state(ctx, ring, v);
145bf215546Sopenharmony_ci   fd4_emit_cs_consts(v, ring, ctx, info);
146bf215546Sopenharmony_ci
147bf215546Sopenharmony_ci   u_foreach_bit (i, ctx->global_bindings.enabled_mask)
148bf215546Sopenharmony_ci      nglobal++;
149bf215546Sopenharmony_ci
150bf215546Sopenharmony_ci   if (nglobal > 0) {
151bf215546Sopenharmony_ci      /* global resources don't otherwise get an OUT_RELOC(), since
152bf215546Sopenharmony_ci       * the raw ptr address is emitted ir ir3_emit_cs_consts().
153bf215546Sopenharmony_ci       * So to make the kernel aware that these buffers are referenced
154bf215546Sopenharmony_ci       * by the batch, emit dummy reloc's as part of a no-op packet
155bf215546Sopenharmony_ci       * payload:
156bf215546Sopenharmony_ci       */
157bf215546Sopenharmony_ci      OUT_PKT3(ring, CP_NOP, 2 * nglobal);
158bf215546Sopenharmony_ci      u_foreach_bit (i, ctx->global_bindings.enabled_mask) {
159bf215546Sopenharmony_ci         struct pipe_resource *prsc = ctx->global_bindings.buf[i];
160bf215546Sopenharmony_ci         OUT_RELOC(ring, fd_resource(prsc)->bo, 0, 0, 0);
161bf215546Sopenharmony_ci      }
162bf215546Sopenharmony_ci   }
163bf215546Sopenharmony_ci
164bf215546Sopenharmony_ci   const unsigned *local_size =
165bf215546Sopenharmony_ci      info->block; // v->shader->nir->info->workgroup_size;
166bf215546Sopenharmony_ci   const unsigned *num_groups = info->grid;
167bf215546Sopenharmony_ci   /* for some reason, mesa/st doesn't set info->work_dim, so just assume 3: */
168bf215546Sopenharmony_ci   const unsigned work_dim = info->work_dim ? info->work_dim : 3;
169bf215546Sopenharmony_ci   OUT_PKT0(ring, REG_A4XX_HLSQ_CL_NDRANGE_0, 7);
170bf215546Sopenharmony_ci   OUT_RING(ring, A4XX_HLSQ_CL_NDRANGE_0_KERNELDIM(work_dim) |
171bf215546Sopenharmony_ci                     A4XX_HLSQ_CL_NDRANGE_0_LOCALSIZEX(local_size[0] - 1) |
172bf215546Sopenharmony_ci                     A4XX_HLSQ_CL_NDRANGE_0_LOCALSIZEY(local_size[1] - 1) |
173bf215546Sopenharmony_ci                     A4XX_HLSQ_CL_NDRANGE_0_LOCALSIZEZ(local_size[2] - 1));
174bf215546Sopenharmony_ci   OUT_RING(ring,
175bf215546Sopenharmony_ci            A4XX_HLSQ_CL_NDRANGE_1_SIZE_X(local_size[0] * num_groups[0]));
176bf215546Sopenharmony_ci   OUT_RING(ring, 0); /* HLSQ_CL_NDRANGE_2_GLOBALOFF_X */
177bf215546Sopenharmony_ci   OUT_RING(ring,
178bf215546Sopenharmony_ci            A4XX_HLSQ_CL_NDRANGE_3_SIZE_Y(local_size[1] * num_groups[1]));
179bf215546Sopenharmony_ci   OUT_RING(ring, 0); /* HLSQ_CL_NDRANGE_4_GLOBALOFF_Y */
180bf215546Sopenharmony_ci   OUT_RING(ring,
181bf215546Sopenharmony_ci            A4XX_HLSQ_CL_NDRANGE_5_SIZE_Z(local_size[2] * num_groups[2]));
182bf215546Sopenharmony_ci   OUT_RING(ring, 0); /* HLSQ_CL_NDRANGE_6_GLOBALOFF_Z */
183bf215546Sopenharmony_ci
184bf215546Sopenharmony_ci   if (info->indirect) {
185bf215546Sopenharmony_ci      struct fd_resource *rsc = fd_resource(info->indirect);
186bf215546Sopenharmony_ci
187bf215546Sopenharmony_ci      fd_event_write(ctx->batch, ring, CACHE_FLUSH);
188bf215546Sopenharmony_ci      fd_wfi(ctx->batch, ring);
189bf215546Sopenharmony_ci
190bf215546Sopenharmony_ci      OUT_PKT3(ring, CP_EXEC_CS_INDIRECT, 3);
191bf215546Sopenharmony_ci      OUT_RING(ring, 0x00000000);
192bf215546Sopenharmony_ci      OUT_RELOC(ring, rsc->bo, info->indirect_offset, 0, 0);
193bf215546Sopenharmony_ci      OUT_RING(ring,
194bf215546Sopenharmony_ci               A4XX_CP_EXEC_CS_INDIRECT_2_LOCALSIZEX(local_size[0] - 1) |
195bf215546Sopenharmony_ci                  A4XX_CP_EXEC_CS_INDIRECT_2_LOCALSIZEY(local_size[1] - 1) |
196bf215546Sopenharmony_ci                  A4XX_CP_EXEC_CS_INDIRECT_2_LOCALSIZEZ(local_size[2] - 1));
197bf215546Sopenharmony_ci   } else {
198bf215546Sopenharmony_ci      OUT_PKT3(ring, CP_EXEC_CS, 4);
199bf215546Sopenharmony_ci      OUT_RING(ring, 0x00000000);
200bf215546Sopenharmony_ci      OUT_RING(ring, CP_EXEC_CS_1_NGROUPS_X(info->grid[0]));
201bf215546Sopenharmony_ci      OUT_RING(ring, CP_EXEC_CS_2_NGROUPS_Y(info->grid[1]));
202bf215546Sopenharmony_ci      OUT_RING(ring, CP_EXEC_CS_3_NGROUPS_Z(info->grid[2]));
203bf215546Sopenharmony_ci   }
204bf215546Sopenharmony_ci}
205bf215546Sopenharmony_ci
206bf215546Sopenharmony_civoid
207bf215546Sopenharmony_cifd4_compute_init(struct pipe_context *pctx) disable_thread_safety_analysis
208bf215546Sopenharmony_ci{
209bf215546Sopenharmony_ci   struct fd_context *ctx = fd_context(pctx);
210bf215546Sopenharmony_ci   ctx->launch_grid = fd4_launch_grid;
211bf215546Sopenharmony_ci   pctx->create_compute_state = ir3_shader_compute_state_create;
212bf215546Sopenharmony_ci   pctx->delete_compute_state = ir3_shader_state_delete;
213bf215546Sopenharmony_ci}
214