1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2017 Rob Clark <robclark@freedesktop.org>
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 *    Rob Clark <robclark@freedesktop.org>
25bf215546Sopenharmony_ci */
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "pipe/p_state.h"
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci#include "freedreno_resource.h"
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci#include "fd5_compute.h"
32bf215546Sopenharmony_ci#include "fd5_context.h"
33bf215546Sopenharmony_ci#include "fd5_emit.h"
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci/* maybe move to fd5_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   /* if shader is more than 32*16 instructions, don't preload it.  Similar
44bf215546Sopenharmony_ci    * to the combined restriction of 64*16 for VS+FS
45bf215546Sopenharmony_ci    */
46bf215546Sopenharmony_ci   if (instrlen > 32)
47bf215546Sopenharmony_ci      instrlen = 0;
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A5XX_SP_SP_CNTL, 1);
50bf215546Sopenharmony_ci   OUT_RING(ring, 0x00000000); /* SP_SP_CNTL */
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A5XX_HLSQ_CONTROL_0_REG, 1);
53bf215546Sopenharmony_ci   OUT_RING(ring, A5XX_HLSQ_CONTROL_0_REG_FSTHREADSIZE(TWO_QUADS) |
54bf215546Sopenharmony_ci                     A5XX_HLSQ_CONTROL_0_REG_CSTHREADSIZE(thrsz) |
55bf215546Sopenharmony_ci                     0x00000880 /* XXX */);
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A5XX_SP_CS_CTRL_REG0, 1);
58bf215546Sopenharmony_ci   OUT_RING(ring,
59bf215546Sopenharmony_ci            A5XX_SP_CS_CTRL_REG0_THREADSIZE(thrsz) |
60bf215546Sopenharmony_ci               A5XX_SP_CS_CTRL_REG0_HALFREGFOOTPRINT(i->max_half_reg + 1) |
61bf215546Sopenharmony_ci               A5XX_SP_CS_CTRL_REG0_FULLREGFOOTPRINT(i->max_reg + 1) |
62bf215546Sopenharmony_ci               A5XX_SP_CS_CTRL_REG0_BRANCHSTACK(ir3_shader_branchstack_hw(v)) |
63bf215546Sopenharmony_ci               COND(instrlen != 0, A5XX_SP_CS_CTRL_REG0_BUFFER) |
64bf215546Sopenharmony_ci               0x2 /* XXX */);
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A5XX_HLSQ_CS_CONFIG, 1);
67bf215546Sopenharmony_ci   OUT_RING(ring, A5XX_HLSQ_CS_CONFIG_CONSTOBJECTOFFSET(0) |
68bf215546Sopenharmony_ci                     A5XX_HLSQ_CS_CONFIG_SHADEROBJOFFSET(0) |
69bf215546Sopenharmony_ci                     A5XX_HLSQ_CS_CONFIG_ENABLED);
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A5XX_HLSQ_CS_CNTL, 1);
72bf215546Sopenharmony_ci   OUT_RING(ring, A5XX_HLSQ_CS_CNTL_INSTRLEN(instrlen) |
73bf215546Sopenharmony_ci                     COND(v->has_ssbo, A5XX_HLSQ_CS_CNTL_SSBO_ENABLE));
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A5XX_SP_CS_CONFIG, 1);
76bf215546Sopenharmony_ci   OUT_RING(ring, A5XX_SP_CS_CONFIG_CONSTOBJECTOFFSET(0) |
77bf215546Sopenharmony_ci                     A5XX_SP_CS_CONFIG_SHADEROBJOFFSET(0) |
78bf215546Sopenharmony_ci                     A5XX_SP_CS_CONFIG_ENABLED);
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci   assert(v->constlen % 4 == 0);
81bf215546Sopenharmony_ci   unsigned constlen = v->constlen / 4;
82bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A5XX_HLSQ_CS_CONSTLEN, 2);
83bf215546Sopenharmony_ci   OUT_RING(ring, constlen); /* HLSQ_CS_CONSTLEN */
84bf215546Sopenharmony_ci   OUT_RING(ring, instrlen); /* HLSQ_CS_INSTRLEN */
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A5XX_SP_CS_OBJ_START_LO, 2);
87bf215546Sopenharmony_ci   OUT_RELOC(ring, v->bo, 0, 0, 0); /* SP_CS_OBJ_START_LO/HI */
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A5XX_HLSQ_UPDATE_CNTL, 1);
90bf215546Sopenharmony_ci   OUT_RING(ring, 0x1f00000);
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci   uint32_t local_invocation_id, work_group_id;
93bf215546Sopenharmony_ci   local_invocation_id =
94bf215546Sopenharmony_ci      ir3_find_sysval_regid(v, SYSTEM_VALUE_LOCAL_INVOCATION_ID);
95bf215546Sopenharmony_ci   work_group_id = ir3_find_sysval_regid(v, SYSTEM_VALUE_WORKGROUP_ID);
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A5XX_HLSQ_CS_CNTL_0, 2);
98bf215546Sopenharmony_ci   OUT_RING(ring, A5XX_HLSQ_CS_CNTL_0_WGIDCONSTID(work_group_id) |
99bf215546Sopenharmony_ci                     A5XX_HLSQ_CS_CNTL_0_UNK0(regid(63, 0)) |
100bf215546Sopenharmony_ci                     A5XX_HLSQ_CS_CNTL_0_UNK1(regid(63, 0)) |
101bf215546Sopenharmony_ci                     A5XX_HLSQ_CS_CNTL_0_LOCALIDREGID(local_invocation_id));
102bf215546Sopenharmony_ci   OUT_RING(ring, 0x1); /* HLSQ_CS_CNTL_1 */
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ci   if (instrlen > 0)
105bf215546Sopenharmony_ci      fd5_emit_shader(ring, v);
106bf215546Sopenharmony_ci}
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_cistatic void
109bf215546Sopenharmony_cifd5_launch_grid(struct fd_context *ctx,
110bf215546Sopenharmony_ci                const struct pipe_grid_info *info) assert_dt
111bf215546Sopenharmony_ci{
112bf215546Sopenharmony_ci   struct ir3_shader_key key = {};
113bf215546Sopenharmony_ci   struct ir3_shader_variant *v;
114bf215546Sopenharmony_ci   struct fd_ringbuffer *ring = ctx->batch->draw;
115bf215546Sopenharmony_ci   unsigned nglobal = 0;
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_ci   v =
118bf215546Sopenharmony_ci      ir3_shader_variant(ir3_get_shader(ctx->compute), key, false, &ctx->debug);
119bf215546Sopenharmony_ci   if (!v)
120bf215546Sopenharmony_ci      return;
121bf215546Sopenharmony_ci
122bf215546Sopenharmony_ci   if (ctx->dirty_shader[PIPE_SHADER_COMPUTE] & FD_DIRTY_SHADER_PROG)
123bf215546Sopenharmony_ci      cs_program_emit(ring, v);
124bf215546Sopenharmony_ci
125bf215546Sopenharmony_ci   fd5_emit_cs_state(ctx, ring, v);
126bf215546Sopenharmony_ci   fd5_emit_cs_consts(v, ring, ctx, info);
127bf215546Sopenharmony_ci
128bf215546Sopenharmony_ci   u_foreach_bit (i, ctx->global_bindings.enabled_mask)
129bf215546Sopenharmony_ci      nglobal++;
130bf215546Sopenharmony_ci
131bf215546Sopenharmony_ci   if (nglobal > 0) {
132bf215546Sopenharmony_ci      /* global resources don't otherwise get an OUT_RELOC(), since
133bf215546Sopenharmony_ci       * the raw ptr address is emitted ir ir3_emit_cs_consts().
134bf215546Sopenharmony_ci       * So to make the kernel aware that these buffers are referenced
135bf215546Sopenharmony_ci       * by the batch, emit dummy reloc's as part of a no-op packet
136bf215546Sopenharmony_ci       * payload:
137bf215546Sopenharmony_ci       */
138bf215546Sopenharmony_ci      OUT_PKT7(ring, CP_NOP, 2 * nglobal);
139bf215546Sopenharmony_ci      u_foreach_bit (i, ctx->global_bindings.enabled_mask) {
140bf215546Sopenharmony_ci         struct pipe_resource *prsc = ctx->global_bindings.buf[i];
141bf215546Sopenharmony_ci         OUT_RELOC(ring, fd_resource(prsc)->bo, 0, 0, 0);
142bf215546Sopenharmony_ci      }
143bf215546Sopenharmony_ci   }
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_ci   const unsigned *local_size =
146bf215546Sopenharmony_ci      info->block; // v->shader->nir->info->workgroup_size;
147bf215546Sopenharmony_ci   const unsigned *num_groups = info->grid;
148bf215546Sopenharmony_ci   /* for some reason, mesa/st doesn't set info->work_dim, so just assume 3: */
149bf215546Sopenharmony_ci   const unsigned work_dim = info->work_dim ? info->work_dim : 3;
150bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A5XX_HLSQ_CS_NDRANGE_0, 7);
151bf215546Sopenharmony_ci   OUT_RING(ring, A5XX_HLSQ_CS_NDRANGE_0_KERNELDIM(work_dim) |
152bf215546Sopenharmony_ci                     A5XX_HLSQ_CS_NDRANGE_0_LOCALSIZEX(local_size[0] - 1) |
153bf215546Sopenharmony_ci                     A5XX_HLSQ_CS_NDRANGE_0_LOCALSIZEY(local_size[1] - 1) |
154bf215546Sopenharmony_ci                     A5XX_HLSQ_CS_NDRANGE_0_LOCALSIZEZ(local_size[2] - 1));
155bf215546Sopenharmony_ci   OUT_RING(ring,
156bf215546Sopenharmony_ci            A5XX_HLSQ_CS_NDRANGE_1_GLOBALSIZE_X(local_size[0] * num_groups[0]));
157bf215546Sopenharmony_ci   OUT_RING(ring, 0); /* HLSQ_CS_NDRANGE_2_GLOBALOFF_X */
158bf215546Sopenharmony_ci   OUT_RING(ring,
159bf215546Sopenharmony_ci            A5XX_HLSQ_CS_NDRANGE_3_GLOBALSIZE_Y(local_size[1] * num_groups[1]));
160bf215546Sopenharmony_ci   OUT_RING(ring, 0); /* HLSQ_CS_NDRANGE_4_GLOBALOFF_Y */
161bf215546Sopenharmony_ci   OUT_RING(ring,
162bf215546Sopenharmony_ci            A5XX_HLSQ_CS_NDRANGE_5_GLOBALSIZE_Z(local_size[2] * num_groups[2]));
163bf215546Sopenharmony_ci   OUT_RING(ring, 0); /* HLSQ_CS_NDRANGE_6_GLOBALOFF_Z */
164bf215546Sopenharmony_ci
165bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A5XX_HLSQ_CS_KERNEL_GROUP_X, 3);
166bf215546Sopenharmony_ci   OUT_RING(ring, 1); /* HLSQ_CS_KERNEL_GROUP_X */
167bf215546Sopenharmony_ci   OUT_RING(ring, 1); /* HLSQ_CS_KERNEL_GROUP_Y */
168bf215546Sopenharmony_ci   OUT_RING(ring, 1); /* HLSQ_CS_KERNEL_GROUP_Z */
169bf215546Sopenharmony_ci
170bf215546Sopenharmony_ci   if (info->indirect) {
171bf215546Sopenharmony_ci      struct fd_resource *rsc = fd_resource(info->indirect);
172bf215546Sopenharmony_ci
173bf215546Sopenharmony_ci      fd5_emit_flush(ctx, ring);
174bf215546Sopenharmony_ci
175bf215546Sopenharmony_ci      OUT_PKT7(ring, CP_EXEC_CS_INDIRECT, 4);
176bf215546Sopenharmony_ci      OUT_RING(ring, 0x00000000);
177bf215546Sopenharmony_ci      OUT_RELOC(ring, rsc->bo, info->indirect_offset, 0, 0); /* ADDR_LO/HI */
178bf215546Sopenharmony_ci      OUT_RING(ring,
179bf215546Sopenharmony_ci               A5XX_CP_EXEC_CS_INDIRECT_3_LOCALSIZEX(local_size[0] - 1) |
180bf215546Sopenharmony_ci                  A5XX_CP_EXEC_CS_INDIRECT_3_LOCALSIZEY(local_size[1] - 1) |
181bf215546Sopenharmony_ci                  A5XX_CP_EXEC_CS_INDIRECT_3_LOCALSIZEZ(local_size[2] - 1));
182bf215546Sopenharmony_ci   } else {
183bf215546Sopenharmony_ci      OUT_PKT7(ring, CP_EXEC_CS, 4);
184bf215546Sopenharmony_ci      OUT_RING(ring, 0x00000000);
185bf215546Sopenharmony_ci      OUT_RING(ring, CP_EXEC_CS_1_NGROUPS_X(info->grid[0]));
186bf215546Sopenharmony_ci      OUT_RING(ring, CP_EXEC_CS_2_NGROUPS_Y(info->grid[1]));
187bf215546Sopenharmony_ci      OUT_RING(ring, CP_EXEC_CS_3_NGROUPS_Z(info->grid[2]));
188bf215546Sopenharmony_ci   }
189bf215546Sopenharmony_ci}
190bf215546Sopenharmony_ci
191bf215546Sopenharmony_civoid
192bf215546Sopenharmony_cifd5_compute_init(struct pipe_context *pctx) disable_thread_safety_analysis
193bf215546Sopenharmony_ci{
194bf215546Sopenharmony_ci   struct fd_context *ctx = fd_context(pctx);
195bf215546Sopenharmony_ci   ctx->launch_grid = fd5_launch_grid;
196bf215546Sopenharmony_ci   pctx->create_compute_state = ir3_shader_compute_state_create;
197bf215546Sopenharmony_ci   pctx->delete_compute_state = ir3_shader_state_delete;
198bf215546Sopenharmony_ci}
199