1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2020 Google, Inc.
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
24bf215546Sopenharmony_ci#include "ir3/ir3_compiler.h"
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#include "util/u_math.h"
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#include "adreno_pm4.xml.h"
29bf215546Sopenharmony_ci#include "adreno_common.xml.h"
30bf215546Sopenharmony_ci#include "a6xx.xml.h"
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci#include "common/freedreno_dev_info.h"
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci#include "ir3_asm.h"
35bf215546Sopenharmony_ci#include "main.h"
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_cistruct a6xx_backend {
38bf215546Sopenharmony_ci   struct backend base;
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_ci   struct ir3_compiler *compiler;
41bf215546Sopenharmony_ci   struct fd_device *dev;
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci   const struct fd_dev_info *info;
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_ci   unsigned seqno;
46bf215546Sopenharmony_ci   struct fd_bo *control_mem;
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci   struct fd_bo *query_mem;
49bf215546Sopenharmony_ci   const struct perfcntr *perfcntrs;
50bf215546Sopenharmony_ci   unsigned num_perfcntrs;
51bf215546Sopenharmony_ci};
52bf215546Sopenharmony_cidefine_cast(backend, a6xx_backend);
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci/*
55bf215546Sopenharmony_ci * Data structures shared with GPU:
56bf215546Sopenharmony_ci */
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci/* This struct defines the layout of the fd6_context::control buffer: */
59bf215546Sopenharmony_cistruct fd6_control {
60bf215546Sopenharmony_ci   uint32_t seqno; /* seqno for async CP_EVENT_WRITE, etc */
61bf215546Sopenharmony_ci   uint32_t _pad0;
62bf215546Sopenharmony_ci   volatile uint32_t vsc_overflow;
63bf215546Sopenharmony_ci   uint32_t _pad1;
64bf215546Sopenharmony_ci   /* flag set from cmdstream when VSC overflow detected: */
65bf215546Sopenharmony_ci   uint32_t vsc_scratch;
66bf215546Sopenharmony_ci   uint32_t _pad2;
67bf215546Sopenharmony_ci   uint32_t _pad3;
68bf215546Sopenharmony_ci   uint32_t _pad4;
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci   /* scratch space for VPC_SO[i].FLUSH_BASE_LO/HI, start on 32 byte boundary. */
71bf215546Sopenharmony_ci   struct {
72bf215546Sopenharmony_ci      uint32_t offset;
73bf215546Sopenharmony_ci      uint32_t pad[7];
74bf215546Sopenharmony_ci   } flush_base[4];
75bf215546Sopenharmony_ci};
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ci#define control_ptr(a6xx_backend, member)                                      \
78bf215546Sopenharmony_ci   (a6xx_backend)->control_mem, offsetof(struct fd6_control, member), 0, 0
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_cistruct PACKED fd6_query_sample {
81bf215546Sopenharmony_ci   uint64_t start;
82bf215546Sopenharmony_ci   uint64_t result;
83bf215546Sopenharmony_ci   uint64_t stop;
84bf215546Sopenharmony_ci};
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci/* offset of a single field of an array of fd6_query_sample: */
87bf215546Sopenharmony_ci#define query_sample_idx(a6xx_backend, idx, field)                             \
88bf215546Sopenharmony_ci   (a6xx_backend)->query_mem,                                                  \
89bf215546Sopenharmony_ci      (idx * sizeof(struct fd6_query_sample)) +                                \
90bf215546Sopenharmony_ci         offsetof(struct fd6_query_sample, field),                             \
91bf215546Sopenharmony_ci      0, 0
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci/*
94bf215546Sopenharmony_ci * Backend implementation:
95bf215546Sopenharmony_ci */
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_cistatic struct kernel *
98bf215546Sopenharmony_cia6xx_assemble(struct backend *b, FILE *in)
99bf215546Sopenharmony_ci{
100bf215546Sopenharmony_ci   struct a6xx_backend *a6xx_backend = to_a6xx_backend(b);
101bf215546Sopenharmony_ci   struct ir3_kernel *ir3_kernel = ir3_asm_assemble(a6xx_backend->compiler, in);
102bf215546Sopenharmony_ci   ir3_kernel->backend = b;
103bf215546Sopenharmony_ci   return &ir3_kernel->base;
104bf215546Sopenharmony_ci}
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_cistatic void
107bf215546Sopenharmony_cia6xx_disassemble(struct kernel *kernel, FILE *out)
108bf215546Sopenharmony_ci{
109bf215546Sopenharmony_ci   ir3_asm_disassemble(to_ir3_kernel(kernel), out);
110bf215546Sopenharmony_ci}
111bf215546Sopenharmony_ci
112bf215546Sopenharmony_cistatic void
113bf215546Sopenharmony_cics_program_emit(struct fd_ringbuffer *ring, struct kernel *kernel)
114bf215546Sopenharmony_ci{
115bf215546Sopenharmony_ci   struct ir3_kernel *ir3_kernel = to_ir3_kernel(kernel);
116bf215546Sopenharmony_ci   struct a6xx_backend *a6xx_backend = to_a6xx_backend(ir3_kernel->backend);
117bf215546Sopenharmony_ci   struct ir3_shader_variant *v = ir3_kernel->v;
118bf215546Sopenharmony_ci   const struct ir3_info *i = &v->info;
119bf215546Sopenharmony_ci   enum a6xx_threadsize thrsz = i->double_threadsize ? THREAD128 : THREAD64;
120bf215546Sopenharmony_ci
121bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A6XX_SP_MODE_CONTROL, 1);
122bf215546Sopenharmony_ci   OUT_RING(ring, A6XX_SP_MODE_CONTROL_CONSTANT_DEMOTION_ENABLE | 4);
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A6XX_SP_PERFCTR_ENABLE, 1);
125bf215546Sopenharmony_ci   OUT_RING(ring, A6XX_SP_PERFCTR_ENABLE_CS);
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A6XX_SP_FLOAT_CNTL, 1);
128bf215546Sopenharmony_ci   OUT_RING(ring, 0);
129bf215546Sopenharmony_ci
130bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A6XX_HLSQ_INVALIDATE_CMD, 1);
131bf215546Sopenharmony_ci   OUT_RING(
132bf215546Sopenharmony_ci      ring,
133bf215546Sopenharmony_ci      A6XX_HLSQ_INVALIDATE_CMD_VS_STATE | A6XX_HLSQ_INVALIDATE_CMD_HS_STATE |
134bf215546Sopenharmony_ci         A6XX_HLSQ_INVALIDATE_CMD_DS_STATE | A6XX_HLSQ_INVALIDATE_CMD_GS_STATE |
135bf215546Sopenharmony_ci         A6XX_HLSQ_INVALIDATE_CMD_FS_STATE | A6XX_HLSQ_INVALIDATE_CMD_CS_STATE |
136bf215546Sopenharmony_ci         A6XX_HLSQ_INVALIDATE_CMD_CS_IBO | A6XX_HLSQ_INVALIDATE_CMD_GFX_IBO);
137bf215546Sopenharmony_ci
138bf215546Sopenharmony_ci   unsigned constlen = align(v->constlen, 4);
139bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A6XX_HLSQ_CS_CNTL, 1);
140bf215546Sopenharmony_ci   OUT_RING(ring,
141bf215546Sopenharmony_ci            A6XX_HLSQ_CS_CNTL_CONSTLEN(constlen) | A6XX_HLSQ_CS_CNTL_ENABLED);
142bf215546Sopenharmony_ci
143bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A6XX_SP_CS_CONFIG, 2);
144bf215546Sopenharmony_ci   OUT_RING(ring, A6XX_SP_CS_CONFIG_ENABLED |
145bf215546Sopenharmony_ci                     A6XX_SP_CS_CONFIG_NIBO(kernel->num_bufs) |
146bf215546Sopenharmony_ci                     A6XX_SP_CS_CONFIG_NTEX(v->num_samp) |
147bf215546Sopenharmony_ci                     A6XX_SP_CS_CONFIG_NSAMP(v->num_samp)); /* SP_VS_CONFIG */
148bf215546Sopenharmony_ci   OUT_RING(ring, v->instrlen);                             /* SP_VS_INSTRLEN */
149bf215546Sopenharmony_ci
150bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A6XX_SP_CS_CTRL_REG0, 1);
151bf215546Sopenharmony_ci   OUT_RING(ring,
152bf215546Sopenharmony_ci            A6XX_SP_CS_CTRL_REG0_THREADSIZE(thrsz) |
153bf215546Sopenharmony_ci               A6XX_SP_CS_CTRL_REG0_FULLREGFOOTPRINT(i->max_reg + 1) |
154bf215546Sopenharmony_ci               A6XX_SP_CS_CTRL_REG0_HALFREGFOOTPRINT(i->max_half_reg + 1) |
155bf215546Sopenharmony_ci               COND(v->mergedregs, A6XX_SP_CS_CTRL_REG0_MERGEDREGS) |
156bf215546Sopenharmony_ci               COND(ir3_kernel->info.early_preamble, A6XX_SP_CS_CTRL_REG0_EARLYPREAMBLE) |
157bf215546Sopenharmony_ci               A6XX_SP_CS_CTRL_REG0_BRANCHSTACK(ir3_shader_branchstack_hw(v)));
158bf215546Sopenharmony_ci
159bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A6XX_SP_CS_UNKNOWN_A9B1, 1);
160bf215546Sopenharmony_ci   OUT_RING(ring, 0x41);
161bf215546Sopenharmony_ci
162bf215546Sopenharmony_ci   if (a6xx_backend->info->a6xx.has_lpac) {
163bf215546Sopenharmony_ci      OUT_PKT4(ring, REG_A6XX_HLSQ_CS_UNKNOWN_B9D0, 1);
164bf215546Sopenharmony_ci      OUT_RING(ring, A6XX_HLSQ_CS_UNKNOWN_B9D0_SHARED_SIZE(1) |
165bf215546Sopenharmony_ci                        A6XX_HLSQ_CS_UNKNOWN_B9D0_UNK6);
166bf215546Sopenharmony_ci   }
167bf215546Sopenharmony_ci
168bf215546Sopenharmony_ci   uint32_t local_invocation_id, work_group_id;
169bf215546Sopenharmony_ci   local_invocation_id =
170bf215546Sopenharmony_ci      ir3_find_sysval_regid(v, SYSTEM_VALUE_LOCAL_INVOCATION_ID);
171bf215546Sopenharmony_ci   work_group_id = ir3_find_sysval_regid(v, SYSTEM_VALUE_WORKGROUP_ID);
172bf215546Sopenharmony_ci
173bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A6XX_HLSQ_CS_CNTL_0, 2);
174bf215546Sopenharmony_ci   OUT_RING(ring, A6XX_HLSQ_CS_CNTL_0_WGIDCONSTID(work_group_id) |
175bf215546Sopenharmony_ci                     A6XX_HLSQ_CS_CNTL_0_WGSIZECONSTID(regid(63, 0)) |
176bf215546Sopenharmony_ci                     A6XX_HLSQ_CS_CNTL_0_WGOFFSETCONSTID(regid(63, 0)) |
177bf215546Sopenharmony_ci                     A6XX_HLSQ_CS_CNTL_0_LOCALIDREGID(local_invocation_id));
178bf215546Sopenharmony_ci   OUT_RING(ring, A6XX_HLSQ_CS_CNTL_1_LINEARLOCALIDREGID(regid(63, 0)) |
179bf215546Sopenharmony_ci                     A6XX_HLSQ_CS_CNTL_1_THREADSIZE(thrsz));
180bf215546Sopenharmony_ci
181bf215546Sopenharmony_ci   if (a6xx_backend->info->a6xx.has_lpac) {
182bf215546Sopenharmony_ci      OUT_PKT4(ring, REG_A6XX_SP_CS_CNTL_0, 2);
183bf215546Sopenharmony_ci      OUT_RING(ring, A6XX_SP_CS_CNTL_0_WGIDCONSTID(work_group_id) |
184bf215546Sopenharmony_ci                        A6XX_SP_CS_CNTL_0_WGSIZECONSTID(regid(63, 0)) |
185bf215546Sopenharmony_ci                        A6XX_SP_CS_CNTL_0_WGOFFSETCONSTID(regid(63, 0)) |
186bf215546Sopenharmony_ci                        A6XX_SP_CS_CNTL_0_LOCALIDREGID(local_invocation_id));
187bf215546Sopenharmony_ci      OUT_RING(ring, A6XX_SP_CS_CNTL_1_LINEARLOCALIDREGID(regid(63, 0)) |
188bf215546Sopenharmony_ci                        A6XX_SP_CS_CNTL_1_THREADSIZE(thrsz));
189bf215546Sopenharmony_ci   }
190bf215546Sopenharmony_ci
191bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A6XX_SP_CS_OBJ_START, 2);
192bf215546Sopenharmony_ci   OUT_RELOC(ring, v->bo, 0, 0, 0); /* SP_CS_OBJ_START_LO/HI */
193bf215546Sopenharmony_ci
194bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A6XX_SP_CS_INSTRLEN, 1);
195bf215546Sopenharmony_ci   OUT_RING(ring, v->instrlen);
196bf215546Sopenharmony_ci
197bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A6XX_SP_CS_OBJ_START, 2);
198bf215546Sopenharmony_ci   OUT_RELOC(ring, v->bo, 0, 0, 0);
199bf215546Sopenharmony_ci
200bf215546Sopenharmony_ci   uint32_t shader_preload_size =
201bf215546Sopenharmony_ci      MIN2(v->instrlen, a6xx_backend->info->a6xx.instr_cache_size);
202bf215546Sopenharmony_ci   OUT_PKT7(ring, CP_LOAD_STATE6_FRAG, 3);
203bf215546Sopenharmony_ci   OUT_RING(ring, CP_LOAD_STATE6_0_DST_OFF(0) |
204bf215546Sopenharmony_ci                     CP_LOAD_STATE6_0_STATE_TYPE(ST6_SHADER) |
205bf215546Sopenharmony_ci                     CP_LOAD_STATE6_0_STATE_SRC(SS6_INDIRECT) |
206bf215546Sopenharmony_ci                     CP_LOAD_STATE6_0_STATE_BLOCK(SB6_CS_SHADER) |
207bf215546Sopenharmony_ci                     CP_LOAD_STATE6_0_NUM_UNIT(shader_preload_size));
208bf215546Sopenharmony_ci   OUT_RELOC(ring, v->bo, 0, 0, 0);
209bf215546Sopenharmony_ci
210bf215546Sopenharmony_ci   if (v->pvtmem_size > 0) {
211bf215546Sopenharmony_ci      uint32_t per_fiber_size = ALIGN(v->pvtmem_size, 512);
212bf215546Sopenharmony_ci      uint32_t per_sp_size =
213bf215546Sopenharmony_ci         ALIGN(per_fiber_size * a6xx_backend->info->a6xx.fibers_per_sp, 1 << 12);
214bf215546Sopenharmony_ci      uint32_t total_size = per_sp_size * a6xx_backend->info->num_sp_cores;
215bf215546Sopenharmony_ci
216bf215546Sopenharmony_ci      struct fd_bo *pvtmem = fd_bo_new(a6xx_backend->dev, total_size, 0, "pvtmem");
217bf215546Sopenharmony_ci      OUT_PKT4(ring, REG_A6XX_SP_CS_PVT_MEM_PARAM, 4);
218bf215546Sopenharmony_ci      OUT_RING(ring, A6XX_SP_CS_PVT_MEM_PARAM_MEMSIZEPERITEM(per_fiber_size));
219bf215546Sopenharmony_ci      OUT_RELOC(ring, pvtmem, 0, 0, 0);
220bf215546Sopenharmony_ci      OUT_RING(ring, A6XX_SP_CS_PVT_MEM_SIZE_TOTALPVTMEMSIZE(per_sp_size) |
221bf215546Sopenharmony_ci                     COND(v->pvtmem_per_wave,
222bf215546Sopenharmony_ci                          A6XX_SP_CS_PVT_MEM_SIZE_PERWAVEMEMLAYOUT));
223bf215546Sopenharmony_ci
224bf215546Sopenharmony_ci      OUT_PKT4(ring, REG_A6XX_SP_CS_PVT_MEM_HW_STACK_OFFSET, 1);
225bf215546Sopenharmony_ci      OUT_RING(ring, A6XX_SP_CS_PVT_MEM_HW_STACK_OFFSET_OFFSET(per_sp_size));
226bf215546Sopenharmony_ci   }
227bf215546Sopenharmony_ci}
228bf215546Sopenharmony_ci
229bf215546Sopenharmony_cistatic void
230bf215546Sopenharmony_ciemit_const(struct fd_ringbuffer *ring, uint32_t regid, uint32_t sizedwords,
231bf215546Sopenharmony_ci           const uint32_t *dwords)
232bf215546Sopenharmony_ci{
233bf215546Sopenharmony_ci   uint32_t align_sz;
234bf215546Sopenharmony_ci
235bf215546Sopenharmony_ci   assert((regid % 4) == 0);
236bf215546Sopenharmony_ci
237bf215546Sopenharmony_ci   align_sz = align(sizedwords, 4);
238bf215546Sopenharmony_ci
239bf215546Sopenharmony_ci   OUT_PKT7(ring, CP_LOAD_STATE6_FRAG, 3 + align_sz);
240bf215546Sopenharmony_ci   OUT_RING(ring, CP_LOAD_STATE6_0_DST_OFF(regid / 4) |
241bf215546Sopenharmony_ci                     CP_LOAD_STATE6_0_STATE_TYPE(ST6_CONSTANTS) |
242bf215546Sopenharmony_ci                     CP_LOAD_STATE6_0_STATE_SRC(SS6_DIRECT) |
243bf215546Sopenharmony_ci                     CP_LOAD_STATE6_0_STATE_BLOCK(SB6_CS_SHADER) |
244bf215546Sopenharmony_ci                     CP_LOAD_STATE6_0_NUM_UNIT(DIV_ROUND_UP(sizedwords, 4)));
245bf215546Sopenharmony_ci   OUT_RING(ring, CP_LOAD_STATE6_1_EXT_SRC_ADDR(0));
246bf215546Sopenharmony_ci   OUT_RING(ring, CP_LOAD_STATE6_2_EXT_SRC_ADDR_HI(0));
247bf215546Sopenharmony_ci
248bf215546Sopenharmony_ci   for (uint32_t i = 0; i < sizedwords; i++) {
249bf215546Sopenharmony_ci      OUT_RING(ring, dwords[i]);
250bf215546Sopenharmony_ci   }
251bf215546Sopenharmony_ci
252bf215546Sopenharmony_ci   /* Zero-pad to multiple of 4 dwords */
253bf215546Sopenharmony_ci   for (uint32_t i = sizedwords; i < align_sz; i++) {
254bf215546Sopenharmony_ci      OUT_RING(ring, 0);
255bf215546Sopenharmony_ci   }
256bf215546Sopenharmony_ci}
257bf215546Sopenharmony_ci
258bf215546Sopenharmony_cistatic void
259bf215546Sopenharmony_cics_const_emit(struct fd_ringbuffer *ring, struct kernel *kernel,
260bf215546Sopenharmony_ci              uint32_t grid[3])
261bf215546Sopenharmony_ci{
262bf215546Sopenharmony_ci   struct ir3_kernel *ir3_kernel = to_ir3_kernel(kernel);
263bf215546Sopenharmony_ci   struct ir3_shader_variant *v = ir3_kernel->v;
264bf215546Sopenharmony_ci
265bf215546Sopenharmony_ci   const struct ir3_const_state *const_state = ir3_const_state(v);
266bf215546Sopenharmony_ci   uint32_t base = const_state->offsets.immediate;
267bf215546Sopenharmony_ci   int size = DIV_ROUND_UP(const_state->immediates_count, 4);
268bf215546Sopenharmony_ci
269bf215546Sopenharmony_ci   if (ir3_kernel->info.numwg != INVALID_REG) {
270bf215546Sopenharmony_ci      assert((ir3_kernel->info.numwg & 0x3) == 0);
271bf215546Sopenharmony_ci      int idx = ir3_kernel->info.numwg >> 2;
272bf215546Sopenharmony_ci      const_state->immediates[idx * 4 + 0] = grid[0];
273bf215546Sopenharmony_ci      const_state->immediates[idx * 4 + 1] = grid[1];
274bf215546Sopenharmony_ci      const_state->immediates[idx * 4 + 2] = grid[2];
275bf215546Sopenharmony_ci   }
276bf215546Sopenharmony_ci
277bf215546Sopenharmony_ci   for (int i = 0; i < MAX_BUFS; i++) {
278bf215546Sopenharmony_ci      if (kernel->buf_addr_regs[i] != INVALID_REG) {
279bf215546Sopenharmony_ci         assert((kernel->buf_addr_regs[i] & 0x3) == 0);
280bf215546Sopenharmony_ci         int idx = kernel->buf_addr_regs[i] >> 2;
281bf215546Sopenharmony_ci
282bf215546Sopenharmony_ci         uint64_t iova = fd_bo_get_iova(kernel->bufs[i]);
283bf215546Sopenharmony_ci
284bf215546Sopenharmony_ci         const_state->immediates[idx * 4 + 1] = iova >> 32;
285bf215546Sopenharmony_ci         const_state->immediates[idx * 4 + 0] = (iova << 32) >> 32;
286bf215546Sopenharmony_ci      }
287bf215546Sopenharmony_ci   }
288bf215546Sopenharmony_ci
289bf215546Sopenharmony_ci   /* truncate size to avoid writing constants that shader
290bf215546Sopenharmony_ci    * does not use:
291bf215546Sopenharmony_ci    */
292bf215546Sopenharmony_ci   size = MIN2(size + base, v->constlen) - base;
293bf215546Sopenharmony_ci
294bf215546Sopenharmony_ci   /* convert out of vec4: */
295bf215546Sopenharmony_ci   base *= 4;
296bf215546Sopenharmony_ci   size *= 4;
297bf215546Sopenharmony_ci
298bf215546Sopenharmony_ci   if (size > 0) {
299bf215546Sopenharmony_ci      emit_const(ring, base, size, const_state->immediates);
300bf215546Sopenharmony_ci   }
301bf215546Sopenharmony_ci}
302bf215546Sopenharmony_ci
303bf215546Sopenharmony_cistatic void
304bf215546Sopenharmony_cics_ibo_emit(struct fd_ringbuffer *ring, struct fd_submit *submit,
305bf215546Sopenharmony_ci            struct kernel *kernel)
306bf215546Sopenharmony_ci{
307bf215546Sopenharmony_ci   struct fd_ringbuffer *state = fd_submit_new_ringbuffer(
308bf215546Sopenharmony_ci      submit, kernel->num_bufs * 16 * 4, FD_RINGBUFFER_STREAMING);
309bf215546Sopenharmony_ci
310bf215546Sopenharmony_ci   for (unsigned i = 0; i < kernel->num_bufs; i++) {
311bf215546Sopenharmony_ci      /* size is encoded with low 15b in WIDTH and high bits in HEIGHT,
312bf215546Sopenharmony_ci       * in units of elements:
313bf215546Sopenharmony_ci       */
314bf215546Sopenharmony_ci      unsigned sz = kernel->buf_sizes[i];
315bf215546Sopenharmony_ci      unsigned width = sz & MASK(15);
316bf215546Sopenharmony_ci      unsigned height = sz >> 15;
317bf215546Sopenharmony_ci
318bf215546Sopenharmony_ci      OUT_RING(state, A6XX_TEX_CONST_0_FMT(FMT6_32_UINT) | A6XX_TEX_CONST_0_TILE_MODE(0));
319bf215546Sopenharmony_ci      OUT_RING(state, A6XX_TEX_CONST_1_WIDTH(width) | A6XX_TEX_CONST_1_HEIGHT(height));
320bf215546Sopenharmony_ci      OUT_RING(state, A6XX_TEX_CONST_2_PITCH(0) | A6XX_TEX_CONST_2_BUFFER |
321bf215546Sopenharmony_ci                         A6XX_TEX_CONST_2_TYPE(A6XX_TEX_BUFFER));
322bf215546Sopenharmony_ci      OUT_RING(state, A6XX_TEX_CONST_3_ARRAY_PITCH(0));
323bf215546Sopenharmony_ci      OUT_RELOC(state, kernel->bufs[i], 0, 0, 0);
324bf215546Sopenharmony_ci      OUT_RING(state, 0x00000000);
325bf215546Sopenharmony_ci      OUT_RING(state, 0x00000000);
326bf215546Sopenharmony_ci      OUT_RING(state, 0x00000000);
327bf215546Sopenharmony_ci      OUT_RING(state, 0x00000000);
328bf215546Sopenharmony_ci      OUT_RING(state, 0x00000000);
329bf215546Sopenharmony_ci      OUT_RING(state, 0x00000000);
330bf215546Sopenharmony_ci      OUT_RING(state, 0x00000000);
331bf215546Sopenharmony_ci      OUT_RING(state, 0x00000000);
332bf215546Sopenharmony_ci      OUT_RING(state, 0x00000000);
333bf215546Sopenharmony_ci      OUT_RING(state, 0x00000000);
334bf215546Sopenharmony_ci   }
335bf215546Sopenharmony_ci
336bf215546Sopenharmony_ci   OUT_PKT7(ring, CP_LOAD_STATE6_FRAG, 3);
337bf215546Sopenharmony_ci   OUT_RING(ring, CP_LOAD_STATE6_0_DST_OFF(0) |
338bf215546Sopenharmony_ci                     CP_LOAD_STATE6_0_STATE_TYPE(ST6_IBO) |
339bf215546Sopenharmony_ci                     CP_LOAD_STATE6_0_STATE_SRC(SS6_INDIRECT) |
340bf215546Sopenharmony_ci                     CP_LOAD_STATE6_0_STATE_BLOCK(SB6_CS_SHADER) |
341bf215546Sopenharmony_ci                     CP_LOAD_STATE6_0_NUM_UNIT(kernel->num_bufs));
342bf215546Sopenharmony_ci   OUT_RB(ring, state);
343bf215546Sopenharmony_ci
344bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A6XX_SP_CS_IBO, 2);
345bf215546Sopenharmony_ci   OUT_RB(ring, state);
346bf215546Sopenharmony_ci
347bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A6XX_SP_CS_IBO_COUNT, 1);
348bf215546Sopenharmony_ci   OUT_RING(ring, kernel->num_bufs);
349bf215546Sopenharmony_ci
350bf215546Sopenharmony_ci   fd_ringbuffer_del(state);
351bf215546Sopenharmony_ci}
352bf215546Sopenharmony_ci
353bf215546Sopenharmony_cistatic inline unsigned
354bf215546Sopenharmony_cievent_write(struct fd_ringbuffer *ring, struct kernel *kernel,
355bf215546Sopenharmony_ci            enum vgt_event_type evt, bool timestamp)
356bf215546Sopenharmony_ci{
357bf215546Sopenharmony_ci   unsigned seqno = 0;
358bf215546Sopenharmony_ci
359bf215546Sopenharmony_ci   OUT_PKT7(ring, CP_EVENT_WRITE, timestamp ? 4 : 1);
360bf215546Sopenharmony_ci   OUT_RING(ring, CP_EVENT_WRITE_0_EVENT(evt));
361bf215546Sopenharmony_ci   if (timestamp) {
362bf215546Sopenharmony_ci      struct ir3_kernel *ir3_kernel = to_ir3_kernel(kernel);
363bf215546Sopenharmony_ci      struct a6xx_backend *a6xx_backend = to_a6xx_backend(ir3_kernel->backend);
364bf215546Sopenharmony_ci      seqno = ++a6xx_backend->seqno;
365bf215546Sopenharmony_ci      OUT_RELOC(ring, control_ptr(a6xx_backend, seqno)); /* ADDR_LO/HI */
366bf215546Sopenharmony_ci      OUT_RING(ring, seqno);
367bf215546Sopenharmony_ci   }
368bf215546Sopenharmony_ci
369bf215546Sopenharmony_ci   return seqno;
370bf215546Sopenharmony_ci}
371bf215546Sopenharmony_ci
372bf215546Sopenharmony_cistatic inline void
373bf215546Sopenharmony_cicache_flush(struct fd_ringbuffer *ring, struct kernel *kernel)
374bf215546Sopenharmony_ci{
375bf215546Sopenharmony_ci   struct ir3_kernel *ir3_kernel = to_ir3_kernel(kernel);
376bf215546Sopenharmony_ci   struct a6xx_backend *a6xx_backend = to_a6xx_backend(ir3_kernel->backend);
377bf215546Sopenharmony_ci   unsigned seqno;
378bf215546Sopenharmony_ci
379bf215546Sopenharmony_ci   seqno = event_write(ring, kernel, RB_DONE_TS, true);
380bf215546Sopenharmony_ci
381bf215546Sopenharmony_ci   OUT_PKT7(ring, CP_WAIT_REG_MEM, 6);
382bf215546Sopenharmony_ci   OUT_RING(ring, CP_WAIT_REG_MEM_0_FUNCTION(WRITE_EQ) |
383bf215546Sopenharmony_ci                     CP_WAIT_REG_MEM_0_POLL_MEMORY);
384bf215546Sopenharmony_ci   OUT_RELOC(ring, control_ptr(a6xx_backend, seqno));
385bf215546Sopenharmony_ci   OUT_RING(ring, CP_WAIT_REG_MEM_3_REF(seqno));
386bf215546Sopenharmony_ci   OUT_RING(ring, CP_WAIT_REG_MEM_4_MASK(~0));
387bf215546Sopenharmony_ci   OUT_RING(ring, CP_WAIT_REG_MEM_5_DELAY_LOOP_CYCLES(16));
388bf215546Sopenharmony_ci
389bf215546Sopenharmony_ci   seqno = event_write(ring, kernel, CACHE_FLUSH_TS, true);
390bf215546Sopenharmony_ci
391bf215546Sopenharmony_ci   OUT_PKT7(ring, CP_WAIT_MEM_GTE, 4);
392bf215546Sopenharmony_ci   OUT_RING(ring, CP_WAIT_MEM_GTE_0_RESERVED(0));
393bf215546Sopenharmony_ci   OUT_RELOC(ring, control_ptr(a6xx_backend, seqno));
394bf215546Sopenharmony_ci   OUT_RING(ring, CP_WAIT_MEM_GTE_3_REF(seqno));
395bf215546Sopenharmony_ci}
396bf215546Sopenharmony_ci
397bf215546Sopenharmony_cistatic void
398bf215546Sopenharmony_cia6xx_emit_grid(struct kernel *kernel, uint32_t grid[3],
399bf215546Sopenharmony_ci               struct fd_submit *submit)
400bf215546Sopenharmony_ci{
401bf215546Sopenharmony_ci   struct ir3_kernel *ir3_kernel = to_ir3_kernel(kernel);
402bf215546Sopenharmony_ci   struct a6xx_backend *a6xx_backend = to_a6xx_backend(ir3_kernel->backend);
403bf215546Sopenharmony_ci   struct fd_ringbuffer *ring = fd_submit_new_ringbuffer(
404bf215546Sopenharmony_ci      submit, 0, FD_RINGBUFFER_PRIMARY | FD_RINGBUFFER_GROWABLE);
405bf215546Sopenharmony_ci
406bf215546Sopenharmony_ci   cs_program_emit(ring, kernel);
407bf215546Sopenharmony_ci   cs_const_emit(ring, kernel, grid);
408bf215546Sopenharmony_ci   cs_ibo_emit(ring, submit, kernel);
409bf215546Sopenharmony_ci
410bf215546Sopenharmony_ci   OUT_PKT7(ring, CP_SET_MARKER, 1);
411bf215546Sopenharmony_ci   OUT_RING(ring, A6XX_CP_SET_MARKER_0_MODE(RM6_COMPUTE));
412bf215546Sopenharmony_ci
413bf215546Sopenharmony_ci   const unsigned *local_size = kernel->local_size;
414bf215546Sopenharmony_ci   const unsigned *num_groups = grid;
415bf215546Sopenharmony_ci
416bf215546Sopenharmony_ci   unsigned work_dim = 0;
417bf215546Sopenharmony_ci   for (int i = 0; i < 3; i++) {
418bf215546Sopenharmony_ci      if (!grid[i])
419bf215546Sopenharmony_ci         break;
420bf215546Sopenharmony_ci      work_dim++;
421bf215546Sopenharmony_ci   }
422bf215546Sopenharmony_ci
423bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A6XX_HLSQ_CS_NDRANGE_0, 7);
424bf215546Sopenharmony_ci   OUT_RING(ring, A6XX_HLSQ_CS_NDRANGE_0_KERNELDIM(work_dim) |
425bf215546Sopenharmony_ci                     A6XX_HLSQ_CS_NDRANGE_0_LOCALSIZEX(local_size[0] - 1) |
426bf215546Sopenharmony_ci                     A6XX_HLSQ_CS_NDRANGE_0_LOCALSIZEY(local_size[1] - 1) |
427bf215546Sopenharmony_ci                     A6XX_HLSQ_CS_NDRANGE_0_LOCALSIZEZ(local_size[2] - 1));
428bf215546Sopenharmony_ci   OUT_RING(ring,
429bf215546Sopenharmony_ci            A6XX_HLSQ_CS_NDRANGE_1_GLOBALSIZE_X(local_size[0] * num_groups[0]));
430bf215546Sopenharmony_ci   OUT_RING(ring, 0); /* HLSQ_CS_NDRANGE_2_GLOBALOFF_X */
431bf215546Sopenharmony_ci   OUT_RING(ring,
432bf215546Sopenharmony_ci            A6XX_HLSQ_CS_NDRANGE_3_GLOBALSIZE_Y(local_size[1] * num_groups[1]));
433bf215546Sopenharmony_ci   OUT_RING(ring, 0); /* HLSQ_CS_NDRANGE_4_GLOBALOFF_Y */
434bf215546Sopenharmony_ci   OUT_RING(ring,
435bf215546Sopenharmony_ci            A6XX_HLSQ_CS_NDRANGE_5_GLOBALSIZE_Z(local_size[2] * num_groups[2]));
436bf215546Sopenharmony_ci   OUT_RING(ring, 0); /* HLSQ_CS_NDRANGE_6_GLOBALOFF_Z */
437bf215546Sopenharmony_ci
438bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A6XX_HLSQ_CS_KERNEL_GROUP_X, 3);
439bf215546Sopenharmony_ci   OUT_RING(ring, 1); /* HLSQ_CS_KERNEL_GROUP_X */
440bf215546Sopenharmony_ci   OUT_RING(ring, 1); /* HLSQ_CS_KERNEL_GROUP_Y */
441bf215546Sopenharmony_ci   OUT_RING(ring, 1); /* HLSQ_CS_KERNEL_GROUP_Z */
442bf215546Sopenharmony_ci
443bf215546Sopenharmony_ci   if (a6xx_backend->num_perfcntrs > 0) {
444bf215546Sopenharmony_ci      a6xx_backend->query_mem = fd_bo_new(
445bf215546Sopenharmony_ci         a6xx_backend->dev,
446bf215546Sopenharmony_ci         a6xx_backend->num_perfcntrs * sizeof(struct fd6_query_sample), 0, "query");
447bf215546Sopenharmony_ci
448bf215546Sopenharmony_ci      /* configure the performance counters to count the requested
449bf215546Sopenharmony_ci       * countables:
450bf215546Sopenharmony_ci       */
451bf215546Sopenharmony_ci      for (unsigned i = 0; i < a6xx_backend->num_perfcntrs; i++) {
452bf215546Sopenharmony_ci         const struct perfcntr *counter = &a6xx_backend->perfcntrs[i];
453bf215546Sopenharmony_ci
454bf215546Sopenharmony_ci         OUT_PKT4(ring, counter->select_reg, 1);
455bf215546Sopenharmony_ci         OUT_RING(ring, counter->selector);
456bf215546Sopenharmony_ci      }
457bf215546Sopenharmony_ci
458bf215546Sopenharmony_ci      OUT_PKT7(ring, CP_WAIT_FOR_IDLE, 0);
459bf215546Sopenharmony_ci
460bf215546Sopenharmony_ci      /* and snapshot the start values: */
461bf215546Sopenharmony_ci      for (unsigned i = 0; i < a6xx_backend->num_perfcntrs; i++) {
462bf215546Sopenharmony_ci         const struct perfcntr *counter = &a6xx_backend->perfcntrs[i];
463bf215546Sopenharmony_ci
464bf215546Sopenharmony_ci         OUT_PKT7(ring, CP_REG_TO_MEM, 3);
465bf215546Sopenharmony_ci         OUT_RING(ring, CP_REG_TO_MEM_0_64B |
466bf215546Sopenharmony_ci                           CP_REG_TO_MEM_0_REG(counter->counter_reg_lo));
467bf215546Sopenharmony_ci         OUT_RELOC(ring, query_sample_idx(a6xx_backend, i, start));
468bf215546Sopenharmony_ci      }
469bf215546Sopenharmony_ci   }
470bf215546Sopenharmony_ci
471bf215546Sopenharmony_ci   OUT_PKT7(ring, CP_EXEC_CS, 4);
472bf215546Sopenharmony_ci   OUT_RING(ring, 0x00000000);
473bf215546Sopenharmony_ci   OUT_RING(ring, CP_EXEC_CS_1_NGROUPS_X(grid[0]));
474bf215546Sopenharmony_ci   OUT_RING(ring, CP_EXEC_CS_2_NGROUPS_Y(grid[1]));
475bf215546Sopenharmony_ci   OUT_RING(ring, CP_EXEC_CS_3_NGROUPS_Z(grid[2]));
476bf215546Sopenharmony_ci
477bf215546Sopenharmony_ci   OUT_PKT7(ring, CP_WAIT_FOR_IDLE, 0);
478bf215546Sopenharmony_ci
479bf215546Sopenharmony_ci   if (a6xx_backend->num_perfcntrs > 0) {
480bf215546Sopenharmony_ci      /* snapshot the end values: */
481bf215546Sopenharmony_ci      for (unsigned i = 0; i < a6xx_backend->num_perfcntrs; i++) {
482bf215546Sopenharmony_ci         const struct perfcntr *counter = &a6xx_backend->perfcntrs[i];
483bf215546Sopenharmony_ci
484bf215546Sopenharmony_ci         OUT_PKT7(ring, CP_REG_TO_MEM, 3);
485bf215546Sopenharmony_ci         OUT_RING(ring, CP_REG_TO_MEM_0_64B |
486bf215546Sopenharmony_ci                           CP_REG_TO_MEM_0_REG(counter->counter_reg_lo));
487bf215546Sopenharmony_ci         OUT_RELOC(ring, query_sample_idx(a6xx_backend, i, stop));
488bf215546Sopenharmony_ci      }
489bf215546Sopenharmony_ci
490bf215546Sopenharmony_ci      /* and compute the result: */
491bf215546Sopenharmony_ci      for (unsigned i = 0; i < a6xx_backend->num_perfcntrs; i++) {
492bf215546Sopenharmony_ci         /* result += stop - start: */
493bf215546Sopenharmony_ci         OUT_PKT7(ring, CP_MEM_TO_MEM, 9);
494bf215546Sopenharmony_ci         OUT_RING(ring, CP_MEM_TO_MEM_0_DOUBLE | CP_MEM_TO_MEM_0_NEG_C);
495bf215546Sopenharmony_ci         OUT_RELOC(ring, query_sample_idx(a6xx_backend, i, result)); /* dst */
496bf215546Sopenharmony_ci         OUT_RELOC(ring, query_sample_idx(a6xx_backend, i, result)); /* srcA */
497bf215546Sopenharmony_ci         OUT_RELOC(ring, query_sample_idx(a6xx_backend, i, stop));   /* srcB */
498bf215546Sopenharmony_ci         OUT_RELOC(ring, query_sample_idx(a6xx_backend, i, start));  /* srcC */
499bf215546Sopenharmony_ci      }
500bf215546Sopenharmony_ci   }
501bf215546Sopenharmony_ci
502bf215546Sopenharmony_ci   cache_flush(ring, kernel);
503bf215546Sopenharmony_ci}
504bf215546Sopenharmony_ci
505bf215546Sopenharmony_cistatic void
506bf215546Sopenharmony_cia6xx_set_perfcntrs(struct backend *b, const struct perfcntr *perfcntrs,
507bf215546Sopenharmony_ci                   unsigned num_perfcntrs)
508bf215546Sopenharmony_ci{
509bf215546Sopenharmony_ci   struct a6xx_backend *a6xx_backend = to_a6xx_backend(b);
510bf215546Sopenharmony_ci
511bf215546Sopenharmony_ci   a6xx_backend->perfcntrs = perfcntrs;
512bf215546Sopenharmony_ci   a6xx_backend->num_perfcntrs = num_perfcntrs;
513bf215546Sopenharmony_ci}
514bf215546Sopenharmony_ci
515bf215546Sopenharmony_cistatic void
516bf215546Sopenharmony_cia6xx_read_perfcntrs(struct backend *b, uint64_t *results)
517bf215546Sopenharmony_ci{
518bf215546Sopenharmony_ci   struct a6xx_backend *a6xx_backend = to_a6xx_backend(b);
519bf215546Sopenharmony_ci
520bf215546Sopenharmony_ci   fd_bo_cpu_prep(a6xx_backend->query_mem, NULL, FD_BO_PREP_READ);
521bf215546Sopenharmony_ci   struct fd6_query_sample *samples = fd_bo_map(a6xx_backend->query_mem);
522bf215546Sopenharmony_ci
523bf215546Sopenharmony_ci   for (unsigned i = 0; i < a6xx_backend->num_perfcntrs; i++) {
524bf215546Sopenharmony_ci      results[i] = samples[i].result;
525bf215546Sopenharmony_ci   }
526bf215546Sopenharmony_ci}
527bf215546Sopenharmony_ci
528bf215546Sopenharmony_cistruct backend *
529bf215546Sopenharmony_cia6xx_init(struct fd_device *dev, const struct fd_dev_id *dev_id)
530bf215546Sopenharmony_ci{
531bf215546Sopenharmony_ci   struct a6xx_backend *a6xx_backend = calloc(1, sizeof(*a6xx_backend));
532bf215546Sopenharmony_ci
533bf215546Sopenharmony_ci   a6xx_backend->base = (struct backend){
534bf215546Sopenharmony_ci      .assemble = a6xx_assemble,
535bf215546Sopenharmony_ci      .disassemble = a6xx_disassemble,
536bf215546Sopenharmony_ci      .emit_grid = a6xx_emit_grid,
537bf215546Sopenharmony_ci      .set_perfcntrs = a6xx_set_perfcntrs,
538bf215546Sopenharmony_ci      .read_perfcntrs = a6xx_read_perfcntrs,
539bf215546Sopenharmony_ci   };
540bf215546Sopenharmony_ci
541bf215546Sopenharmony_ci   a6xx_backend->compiler = ir3_compiler_create(dev, dev_id,
542bf215546Sopenharmony_ci                                                &(struct ir3_compiler_options){});
543bf215546Sopenharmony_ci   a6xx_backend->dev = dev;
544bf215546Sopenharmony_ci
545bf215546Sopenharmony_ci   a6xx_backend->info = fd_dev_info(dev_id);
546bf215546Sopenharmony_ci
547bf215546Sopenharmony_ci   a6xx_backend->control_mem =
548bf215546Sopenharmony_ci      fd_bo_new(dev, 0x1000, 0, "control");
549bf215546Sopenharmony_ci
550bf215546Sopenharmony_ci   return &a6xx_backend->base;
551bf215546Sopenharmony_ci}
552