• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2020 Google, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 
24 #include "ir3/ir3_compiler.h"
25 
26 #include "util/u_math.h"
27 
28 #include "adreno_pm4.xml.h"
29 #include "adreno_common.xml.h"
30 #include "a6xx.xml.h"
31 
32 #include "common/freedreno_dev_info.h"
33 
34 #include "ir3_asm.h"
35 #include "main.h"
36 
37 #define FD_BO_NO_HARDPIN 1
38 #include "common/fd6_pack.h"
39 
40 struct a6xx_backend {
41    struct backend base;
42 
43    struct ir3_compiler *compiler;
44    struct fd_device *dev;
45 
46    const struct fd_dev_info *info;
47 
48    unsigned seqno;
49    struct fd_bo *control_mem;
50 
51    struct fd_bo *query_mem;
52    const struct perfcntr *perfcntrs;
53    unsigned num_perfcntrs;
54 };
55 define_cast(backend, a6xx_backend);
56 
57 /*
58  * Data structures shared with GPU:
59  */
60 
61 /* This struct defines the layout of the fd6_context::control buffer: */
62 struct fd6_control {
63    uint32_t seqno; /* seqno for async CP_EVENT_WRITE, etc */
64    uint32_t _pad0;
65    volatile uint32_t vsc_overflow;
66    uint32_t _pad1;
67    /* flag set from cmdstream when VSC overflow detected: */
68    uint32_t vsc_scratch;
69    uint32_t _pad2;
70    uint32_t _pad3;
71    uint32_t _pad4;
72 
73    /* scratch space for VPC_SO[i].FLUSH_BASE_LO/HI, start on 32 byte boundary. */
74    struct {
75       uint32_t offset;
76       uint32_t pad[7];
77    } flush_base[4];
78 };
79 
80 #define control_ptr(a6xx_backend, member)                                      \
81    (a6xx_backend)->control_mem, offsetof(struct fd6_control, member), 0, 0
82 
83 struct PACKED fd6_query_sample {
84    uint64_t start;
85    uint64_t result;
86    uint64_t stop;
87 };
88 
89 /* offset of a single field of an array of fd6_query_sample: */
90 #define query_sample_idx(a6xx_backend, idx, field)                             \
91    (a6xx_backend)->query_mem,                                                  \
92       (idx * sizeof(struct fd6_query_sample)) +                                \
93          offsetof(struct fd6_query_sample, field),                             \
94       0, 0
95 
96 /*
97  * Backend implementation:
98  */
99 
100 static struct kernel *
a6xx_assemble(struct backend * b,FILE * in)101 a6xx_assemble(struct backend *b, FILE *in)
102 {
103    struct a6xx_backend *a6xx_backend = to_a6xx_backend(b);
104    struct ir3_kernel *ir3_kernel = ir3_asm_assemble(a6xx_backend->compiler, in);
105    ir3_kernel->backend = b;
106    return &ir3_kernel->base;
107 }
108 
109 static void
a6xx_disassemble(struct kernel * kernel,FILE * out)110 a6xx_disassemble(struct kernel *kernel, FILE *out)
111 {
112    ir3_asm_disassemble(to_ir3_kernel(kernel), out);
113 }
114 
115 template<chip CHIP>
116 static void
cs_program_emit(struct fd_ringbuffer * ring,struct kernel * kernel)117 cs_program_emit(struct fd_ringbuffer *ring, struct kernel *kernel)
118 {
119    struct ir3_kernel *ir3_kernel = to_ir3_kernel(kernel);
120    struct a6xx_backend *a6xx_backend = to_a6xx_backend(ir3_kernel->backend);
121    struct ir3_shader_variant *v = ir3_kernel->v;
122    const unsigned *local_size = kernel->local_size;
123    const struct ir3_info *i = &v->info;
124    enum a6xx_threadsize thrsz = i->double_threadsize ? THREAD128 : THREAD64;
125 
126    OUT_REG(ring, A6XX_SP_MODE_CONTROL(.constant_demotion_enable = true,
127                                       .isammode = ISAMMODE_GL,
128                                       .shared_consts_enable = false));
129 
130    OUT_PKT4(ring, REG_A6XX_SP_PERFCTR_ENABLE, 1);
131    OUT_RING(ring, A6XX_SP_PERFCTR_ENABLE_CS);
132 
133    OUT_PKT4(ring, REG_A6XX_SP_FLOAT_CNTL, 1);
134    OUT_RING(ring, 0);
135 
136    for (size_t i = 0; i < ARRAY_SIZE(a6xx_backend->info->a6xx.magic_raw); i++) {
137       auto magic_reg = a6xx_backend->info->a6xx.magic_raw[i];
138       if (!magic_reg.reg)
139          break;
140 
141       OUT_PKT4(ring, magic_reg.reg, 1);
142       OUT_RING(ring, magic_reg.value);
143    }
144 
145    OUT_REG(ring, HLSQ_INVALIDATE_CMD(CHIP,
146       .vs_state = true,
147       .hs_state = true,
148       .ds_state = true,
149       .gs_state = true,
150       .fs_state = true,
151       .cs_state = true,
152       .gfx_ibo = true,
153    ));
154 
155    unsigned constlen = align(v->constlen, 4);
156    OUT_REG(ring, HLSQ_CS_CNTL(CHIP, .constlen = constlen, .enabled = true, ));
157 
158    OUT_PKT4(ring, REG_A6XX_SP_CS_CONFIG, 2);
159    OUT_RING(ring, A6XX_SP_CS_CONFIG_ENABLED |
160                      A6XX_SP_CS_CONFIG_NIBO(kernel->num_bufs) |
161                      A6XX_SP_CS_CONFIG_NTEX(v->num_samp) |
162                      A6XX_SP_CS_CONFIG_NSAMP(v->num_samp)); /* SP_VS_CONFIG */
163    OUT_RING(ring, v->instrlen);                             /* SP_VS_INSTRLEN */
164 
165    OUT_PKT4(ring, REG_A6XX_SP_CS_CTRL_REG0, 1);
166    OUT_RING(ring,
167             A6XX_SP_CS_CTRL_REG0_THREADSIZE(thrsz) |
168                A6XX_SP_CS_CTRL_REG0_FULLREGFOOTPRINT(i->max_reg + 1) |
169                A6XX_SP_CS_CTRL_REG0_HALFREGFOOTPRINT(i->max_half_reg + 1) |
170                COND(v->mergedregs, A6XX_SP_CS_CTRL_REG0_MERGEDREGS) |
171                COND(ir3_kernel->info.early_preamble, A6XX_SP_CS_CTRL_REG0_EARLYPREAMBLE) |
172                A6XX_SP_CS_CTRL_REG0_BRANCHSTACK(ir3_shader_branchstack_hw(v)));
173    if (CHIP == A7XX) {
174       OUT_REG(ring, HLSQ_FS_CNTL_0(CHIP, .threadsize = THREAD64));
175 
176       OUT_REG(ring, HLSQ_CONTROL_2_REG(CHIP, .dword = 0xfcfcfcfc),
177               HLSQ_CONTROL_3_REG(CHIP, .dword = 0xfcfcfcfc),
178               HLSQ_CONTROL_4_REG(CHIP, .dword = 0xfcfcfcfc),
179               HLSQ_CONTROL_5_REG(CHIP, .dword = 0x0000fc00), );
180    }
181 
182    OUT_PKT4(ring, REG_A6XX_SP_CS_UNKNOWN_A9B1, 1);
183    OUT_RING(ring, A6XX_SP_CS_UNKNOWN_A9B1_SHARED_SIZE(1) |
184                   A6XX_SP_CS_UNKNOWN_A9B1_UNK6);
185 
186    if (CHIP == A6XX && a6xx_backend->info->a6xx.has_lpac) {
187       OUT_PKT4(ring, REG_A6XX_HLSQ_CS_UNKNOWN_B9D0, 1);
188       OUT_RING(ring, A6XX_HLSQ_CS_UNKNOWN_B9D0_SHARED_SIZE(1) |
189                         A6XX_HLSQ_CS_UNKNOWN_B9D0_UNK6);
190    }
191 
192    uint32_t local_invocation_id, work_group_id;
193    local_invocation_id =
194       ir3_find_sysval_regid(v, SYSTEM_VALUE_LOCAL_INVOCATION_ID);
195    work_group_id = ir3_find_sysval_regid(v, SYSTEM_VALUE_WORKGROUP_ID);
196 
197    if (CHIP == A6XX) {
198       OUT_PKT4(ring, REG_A6XX_HLSQ_CS_CNTL_0, 2);
199       OUT_RING(ring, A6XX_HLSQ_CS_CNTL_0_WGIDCONSTID(work_group_id) |
200                         A6XX_HLSQ_CS_CNTL_0_WGSIZECONSTID(regid(63, 0)) |
201                         A6XX_HLSQ_CS_CNTL_0_WGOFFSETCONSTID(regid(63, 0)) |
202                         A6XX_HLSQ_CS_CNTL_0_LOCALIDREGID(local_invocation_id));
203       OUT_RING(ring, A6XX_HLSQ_CS_CNTL_1_LINEARLOCALIDREGID(regid(63, 0)) |
204                         A6XX_HLSQ_CS_CNTL_1_THREADSIZE(thrsz));
205    } else {
206       enum a7xx_cs_yalign yalign = (local_size[1] % 8 == 0)   ? CS_YALIGN_8
207                                    : (local_size[1] % 4 == 0) ? CS_YALIGN_4
208                                    : (local_size[1] % 2 == 0) ? CS_YALIGN_2
209                                                               : CS_YALIGN_1;
210 
211       OUT_REG(ring, A7XX_HLSQ_CS_CNTL_1(.linearlocalidregid = regid(63, 0),
212                                         .threadsize = thrsz,
213                                         .unk11 = true,
214                                         .unk22 = true,
215                                         .yalign = yalign, ));
216    }
217 
218    if (CHIP == A7XX || a6xx_backend->info->a6xx.has_lpac) {
219       OUT_PKT4(ring, REG_A6XX_SP_CS_CNTL_0, 1);
220       OUT_RING(ring, A6XX_SP_CS_CNTL_0_WGIDCONSTID(work_group_id) |
221                         A6XX_SP_CS_CNTL_0_WGSIZECONSTID(regid(63, 0)) |
222                         A6XX_SP_CS_CNTL_0_WGOFFSETCONSTID(regid(63, 0)) |
223                         A6XX_SP_CS_CNTL_0_LOCALIDREGID(local_invocation_id));
224       OUT_REG(ring,
225          SP_CS_CNTL_1(CHIP, .linearlocalidregid = regid(63, 0),
226                             .threadsize = thrsz, ));
227    }
228 
229    OUT_PKT4(ring, REG_A6XX_SP_CS_OBJ_START, 2);
230    OUT_RELOC(ring, v->bo, 0, 0, 0); /* SP_CS_OBJ_START_LO/HI */
231 
232    OUT_PKT4(ring, REG_A6XX_SP_CS_INSTRLEN, 1);
233    OUT_RING(ring, v->instrlen);
234 
235    OUT_PKT4(ring, REG_A6XX_SP_CS_OBJ_START, 2);
236    OUT_RELOC(ring, v->bo, 0, 0, 0);
237 
238    uint32_t shader_preload_size =
239       MIN2(v->instrlen, a6xx_backend->info->a6xx.instr_cache_size);
240    OUT_PKT7(ring, CP_LOAD_STATE6_FRAG, 3);
241    OUT_RING(ring, CP_LOAD_STATE6_0_DST_OFF(0) |
242                      CP_LOAD_STATE6_0_STATE_TYPE(ST6_SHADER) |
243                      CP_LOAD_STATE6_0_STATE_SRC(SS6_INDIRECT) |
244                      CP_LOAD_STATE6_0_STATE_BLOCK(SB6_CS_SHADER) |
245                      CP_LOAD_STATE6_0_NUM_UNIT(shader_preload_size));
246    OUT_RELOC(ring, v->bo, 0, 0, 0);
247 
248    if (v->pvtmem_size > 0) {
249       uint32_t per_fiber_size = v->pvtmem_size;
250       uint32_t per_sp_size =
251          ALIGN(per_fiber_size * a6xx_backend->info->fibers_per_sp, 1 << 12);
252       uint32_t total_size = per_sp_size * a6xx_backend->info->num_sp_cores;
253 
254       struct fd_bo *pvtmem = fd_bo_new(a6xx_backend->dev, total_size, 0, "pvtmem");
255       OUT_PKT4(ring, REG_A6XX_SP_CS_PVT_MEM_PARAM, 4);
256       OUT_RING(ring, A6XX_SP_CS_PVT_MEM_PARAM_MEMSIZEPERITEM(per_fiber_size));
257       OUT_RELOC(ring, pvtmem, 0, 0, 0);
258       OUT_RING(ring, A6XX_SP_CS_PVT_MEM_SIZE_TOTALPVTMEMSIZE(per_sp_size) |
259                      COND(v->pvtmem_per_wave,
260                           A6XX_SP_CS_PVT_MEM_SIZE_PERWAVEMEMLAYOUT));
261 
262       OUT_PKT4(ring, REG_A6XX_SP_CS_PVT_MEM_HW_STACK_OFFSET, 1);
263       OUT_RING(ring, A6XX_SP_CS_PVT_MEM_HW_STACK_OFFSET_OFFSET(per_sp_size));
264    }
265 }
266 
267 template<chip CHIP>
268 static void
emit_const(struct fd_ringbuffer * ring,uint32_t regid,uint32_t sizedwords,const uint32_t * dwords)269 emit_const(struct fd_ringbuffer *ring, uint32_t regid, uint32_t sizedwords,
270            const uint32_t *dwords)
271 {
272    uint32_t align_sz;
273 
274    assert((regid % 4) == 0);
275 
276    align_sz = align(sizedwords, 4);
277 
278    OUT_PKT7(ring, CP_LOAD_STATE6_FRAG, 3 + align_sz);
279    OUT_RING(ring, CP_LOAD_STATE6_0_DST_OFF(regid / 4) |
280                      CP_LOAD_STATE6_0_STATE_TYPE(ST6_CONSTANTS) |
281                      CP_LOAD_STATE6_0_STATE_SRC(SS6_DIRECT) |
282                      CP_LOAD_STATE6_0_STATE_BLOCK(SB6_CS_SHADER) |
283                      CP_LOAD_STATE6_0_NUM_UNIT(DIV_ROUND_UP(sizedwords, 4)));
284    OUT_RING(ring, CP_LOAD_STATE6_1_EXT_SRC_ADDR(0));
285    OUT_RING(ring, CP_LOAD_STATE6_2_EXT_SRC_ADDR_HI(0));
286 
287    for (uint32_t i = 0; i < sizedwords; i++) {
288       OUT_RING(ring, dwords[i]);
289    }
290 
291    /* Zero-pad to multiple of 4 dwords */
292    for (uint32_t i = sizedwords; i < align_sz; i++) {
293       OUT_RING(ring, 0);
294    }
295 }
296 
297 template<chip CHIP>
298 static void
cs_const_emit(struct fd_ringbuffer * ring,struct kernel * kernel,uint32_t grid[3])299 cs_const_emit(struct fd_ringbuffer *ring, struct kernel *kernel,
300               uint32_t grid[3])
301 {
302    struct ir3_kernel *ir3_kernel = to_ir3_kernel(kernel);
303    struct ir3_shader_variant *v = ir3_kernel->v;
304 
305    const struct ir3_const_state *const_state = ir3_const_state(v);
306    uint32_t base = const_state->offsets.immediate;
307    int size = DIV_ROUND_UP(const_state->immediates_count, 4);
308 
309    if (ir3_kernel->info.numwg != INVALID_REG) {
310       assert((ir3_kernel->info.numwg & 0x3) == 0);
311       int idx = ir3_kernel->info.numwg >> 2;
312       const_state->immediates[idx * 4 + 0] = grid[0];
313       const_state->immediates[idx * 4 + 1] = grid[1];
314       const_state->immediates[idx * 4 + 2] = grid[2];
315    }
316 
317    for (int i = 0; i < MAX_BUFS; i++) {
318       if (kernel->buf_addr_regs[i] != INVALID_REG) {
319          assert((kernel->buf_addr_regs[i] & 0x3) == 0);
320          int idx = kernel->buf_addr_regs[i] >> 2;
321 
322          uint64_t iova = fd_bo_get_iova(kernel->bufs[i]);
323 
324          const_state->immediates[idx * 4 + 1] = iova >> 32;
325          const_state->immediates[idx * 4 + 0] = (iova << 32) >> 32;
326       }
327    }
328 
329    /* truncate size to avoid writing constants that shader
330     * does not use:
331     */
332    size = MIN2(size + base, v->constlen) - base;
333 
334    /* convert out of vec4: */
335    base *= 4;
336    size *= 4;
337 
338    if (size > 0) {
339       emit_const<CHIP>(ring, base, size, const_state->immediates);
340    }
341 }
342 
343 template<chip CHIP>
344 static void
cs_ibo_emit(struct fd_ringbuffer * ring,struct fd_submit * submit,struct kernel * kernel)345 cs_ibo_emit(struct fd_ringbuffer *ring, struct fd_submit *submit,
346             struct kernel *kernel)
347 {
348    struct fd_ringbuffer *state = fd_submit_new_ringbuffer(
349       submit, kernel->num_bufs * 16 * 4, FD_RINGBUFFER_STREAMING);
350 
351    for (unsigned i = 0; i < kernel->num_bufs; i++) {
352       /* size is encoded with low 15b in WIDTH and high bits in HEIGHT,
353        * in units of elements:
354        */
355       unsigned sz = kernel->buf_sizes[i];
356       unsigned width = sz & MASK(15);
357       unsigned height = sz >> 15;
358 
359       OUT_RING(state, A6XX_TEX_CONST_0_FMT(FMT6_32_UINT) | A6XX_TEX_CONST_0_TILE_MODE(TILE6_LINEAR));
360       OUT_RING(state, A6XX_TEX_CONST_1_WIDTH(width) | A6XX_TEX_CONST_1_HEIGHT(height));
361       OUT_RING(state, A6XX_TEX_CONST_2_PITCH(0) |
362                       A6XX_TEX_CONST_2_STRUCTSIZETEXELS(1) |
363                       A6XX_TEX_CONST_2_TYPE(A6XX_TEX_BUFFER));
364       OUT_RING(state, A6XX_TEX_CONST_3_ARRAY_PITCH(0));
365       OUT_RELOC(state, kernel->bufs[i], 0, 0, 0);
366       OUT_RING(state, 0x00000000);
367       OUT_RING(state, 0x00000000);
368       OUT_RING(state, 0x00000000);
369       OUT_RING(state, 0x00000000);
370       OUT_RING(state, 0x00000000);
371       OUT_RING(state, 0x00000000);
372       OUT_RING(state, 0x00000000);
373       OUT_RING(state, 0x00000000);
374       OUT_RING(state, 0x00000000);
375       OUT_RING(state, 0x00000000);
376    }
377 
378    OUT_PKT7(ring, CP_LOAD_STATE6_FRAG, 3);
379    OUT_RING(ring, CP_LOAD_STATE6_0_DST_OFF(0) |
380                      CP_LOAD_STATE6_0_STATE_TYPE(ST6_IBO) |
381                      CP_LOAD_STATE6_0_STATE_SRC(SS6_INDIRECT) |
382                      CP_LOAD_STATE6_0_STATE_BLOCK(SB6_CS_SHADER) |
383                      CP_LOAD_STATE6_0_NUM_UNIT(kernel->num_bufs));
384    OUT_RB(ring, state);
385 
386    OUT_PKT4(ring, REG_A6XX_SP_CS_IBO, 2);
387    OUT_RB(ring, state);
388 
389    OUT_PKT4(ring, REG_A6XX_SP_CS_IBO_COUNT, 1);
390    OUT_RING(ring, kernel->num_bufs);
391 
392    fd_ringbuffer_del(state);
393 }
394 
395 template<chip CHIP>
396 static inline unsigned
event_write(struct fd_ringbuffer * ring,struct kernel * kernel,enum vgt_event_type evt,bool timestamp)397 event_write(struct fd_ringbuffer *ring, struct kernel *kernel,
398             enum vgt_event_type evt, bool timestamp)
399 {
400    unsigned seqno = 0;
401 
402    if (CHIP == A6XX) {
403       OUT_PKT7(ring, CP_EVENT_WRITE, timestamp ? 4 : 1);
404       OUT_RING(ring, CP_EVENT_WRITE_0_EVENT(evt));
405    } else {
406       OUT_PKT7(ring, CP_EVENT_WRITE7, timestamp ? 4 : 1);
407       OUT_RING(ring,
408          CP_EVENT_WRITE7_0_EVENT(evt) |
409             COND(timestamp, CP_EVENT_WRITE7_0_WRITE_ENABLED |
410                                CP_EVENT_WRITE7_0_WRITE_SRC(EV_WRITE_USER_32B)));
411    }
412 
413    if (timestamp) {
414       struct ir3_kernel *ir3_kernel = to_ir3_kernel(kernel);
415       struct a6xx_backend *a6xx_backend = to_a6xx_backend(ir3_kernel->backend);
416       seqno = ++a6xx_backend->seqno;
417       OUT_RELOC(ring, control_ptr(a6xx_backend, seqno)); /* ADDR_LO/HI */
418       OUT_RING(ring, seqno);
419    }
420 
421    return seqno;
422 }
423 
424 template<chip CHIP>
425 static inline void
cache_flush(struct fd_ringbuffer * ring,struct kernel * kernel)426 cache_flush(struct fd_ringbuffer *ring, struct kernel *kernel)
427 {
428    struct ir3_kernel *ir3_kernel = to_ir3_kernel(kernel);
429    struct a6xx_backend *a6xx_backend = to_a6xx_backend(ir3_kernel->backend);
430    unsigned seqno;
431 
432    seqno = event_write<CHIP>(ring, kernel, RB_DONE_TS, true);
433 
434    OUT_PKT7(ring, CP_WAIT_REG_MEM, 6);
435    OUT_RING(ring, CP_WAIT_REG_MEM_0_FUNCTION(WRITE_EQ) |
436                      CP_WAIT_REG_MEM_0_POLL(POLL_MEMORY));
437    OUT_RELOC(ring, control_ptr(a6xx_backend, seqno));
438    OUT_RING(ring, CP_WAIT_REG_MEM_3_REF(seqno));
439    OUT_RING(ring, CP_WAIT_REG_MEM_4_MASK(~0));
440    OUT_RING(ring, CP_WAIT_REG_MEM_5_DELAY_LOOP_CYCLES(16));
441 
442    if (CHIP == A6XX) {
443       seqno = event_write<CHIP>(ring, kernel, CACHE_FLUSH_TS, true);
444 
445       OUT_PKT7(ring, CP_WAIT_MEM_GTE, 4);
446       OUT_RING(ring, CP_WAIT_MEM_GTE_0_RESERVED(0));
447       OUT_RELOC(ring, control_ptr(a6xx_backend, seqno));
448       OUT_RING(ring, CP_WAIT_MEM_GTE_3_REF(seqno));
449    } else {
450       event_write<CHIP>(ring, kernel, CACHE_FLUSH7, false);
451    }
452 }
453 
454 template<chip CHIP>
455 static void
a6xx_emit_grid(struct kernel * kernel,uint32_t grid[3],struct fd_submit * submit)456 a6xx_emit_grid(struct kernel *kernel, uint32_t grid[3],
457                struct fd_submit *submit)
458 {
459    struct ir3_kernel *ir3_kernel = to_ir3_kernel(kernel);
460    struct a6xx_backend *a6xx_backend = to_a6xx_backend(ir3_kernel->backend);
461    struct fd_ringbuffer *ring = fd_submit_new_ringbuffer(
462       submit, 0,
463       (enum fd_ringbuffer_flags)(FD_RINGBUFFER_PRIMARY | FD_RINGBUFFER_GROWABLE));
464 
465    cs_program_emit<CHIP>(ring, kernel);
466    cs_const_emit<CHIP>(ring, kernel, grid);
467    cs_ibo_emit<CHIP>(ring, submit, kernel);
468 
469    OUT_PKT7(ring, CP_SET_MARKER, 1);
470    OUT_RING(ring, A6XX_CP_SET_MARKER_0_MODE(RM6_COMPUTE));
471 
472    const unsigned *local_size = kernel->local_size;
473    const unsigned *num_groups = grid;
474 
475    unsigned work_dim = 0;
476    for (int i = 0; i < 3; i++) {
477       if (!grid[i])
478          break;
479       work_dim++;
480    }
481 
482    OUT_REG(ring, HLSQ_CS_NDRANGE_0(CHIP,
483                     .kerneldim = work_dim,
484                     .localsizex = local_size[0] - 1,
485                     .localsizey = local_size[1] - 1,
486                     .localsizez = local_size[2] - 1,
487                  ));
488    if (CHIP == A7XX) {
489       OUT_REG(ring, A7XX_HLSQ_CS_LOCAL_SIZE(.localsizex = local_size[0] - 1,
490                                             .localsizey = local_size[1] - 1,
491                                             .localsizez = local_size[2] - 1, ));
492    }
493 
494    OUT_REG(ring, HLSQ_CS_NDRANGE_1(CHIP,
495                     .globalsize_x = local_size[0] * num_groups[0],
496                  ));
497    OUT_REG(ring, HLSQ_CS_NDRANGE_2(CHIP, 0));
498    OUT_REG(ring, HLSQ_CS_NDRANGE_3(CHIP,
499                     .globalsize_y = local_size[1] * num_groups[1],
500                  ));
501    OUT_REG(ring, HLSQ_CS_NDRANGE_4(CHIP, 0));
502    OUT_REG(ring, HLSQ_CS_NDRANGE_5(CHIP,
503                     .globalsize_z = local_size[2] * num_groups[2],
504                  ));
505    OUT_REG(ring, HLSQ_CS_NDRANGE_6(CHIP, 0));
506 
507    OUT_REG(ring, HLSQ_CS_KERNEL_GROUP_X(CHIP, 1));
508    OUT_REG(ring, HLSQ_CS_KERNEL_GROUP_Y(CHIP, 1));
509    OUT_REG(ring, HLSQ_CS_KERNEL_GROUP_Z(CHIP, 1));
510 
511    if (a6xx_backend->num_perfcntrs > 0) {
512       a6xx_backend->query_mem = fd_bo_new(
513          a6xx_backend->dev,
514          a6xx_backend->num_perfcntrs * sizeof(struct fd6_query_sample), 0, "query");
515 
516       /* configure the performance counters to count the requested
517        * countables:
518        */
519       for (unsigned i = 0; i < a6xx_backend->num_perfcntrs; i++) {
520          const struct perfcntr *counter = &a6xx_backend->perfcntrs[i];
521 
522          OUT_PKT4(ring, counter->select_reg, 1);
523          OUT_RING(ring, counter->selector);
524       }
525 
526       OUT_PKT7(ring, CP_WAIT_FOR_IDLE, 0);
527 
528       /* and snapshot the start values: */
529       for (unsigned i = 0; i < a6xx_backend->num_perfcntrs; i++) {
530          const struct perfcntr *counter = &a6xx_backend->perfcntrs[i];
531 
532          OUT_PKT7(ring, CP_REG_TO_MEM, 3);
533          OUT_RING(ring, CP_REG_TO_MEM_0_64B |
534                            CP_REG_TO_MEM_0_REG(counter->counter_reg_lo));
535          OUT_RELOC(ring, query_sample_idx(a6xx_backend, i, start));
536       }
537    }
538 
539    OUT_PKT7(ring, CP_EXEC_CS, 4);
540    OUT_RING(ring, 0x00000000);
541    OUT_RING(ring, CP_EXEC_CS_1_NGROUPS_X(grid[0]));
542    OUT_RING(ring, CP_EXEC_CS_2_NGROUPS_Y(grid[1]));
543    OUT_RING(ring, CP_EXEC_CS_3_NGROUPS_Z(grid[2]));
544 
545    OUT_PKT7(ring, CP_WAIT_FOR_IDLE, 0);
546 
547    if (a6xx_backend->num_perfcntrs > 0) {
548       /* snapshot the end values: */
549       for (unsigned i = 0; i < a6xx_backend->num_perfcntrs; i++) {
550          const struct perfcntr *counter = &a6xx_backend->perfcntrs[i];
551 
552          OUT_PKT7(ring, CP_REG_TO_MEM, 3);
553          OUT_RING(ring, CP_REG_TO_MEM_0_64B |
554                            CP_REG_TO_MEM_0_REG(counter->counter_reg_lo));
555          OUT_RELOC(ring, query_sample_idx(a6xx_backend, i, stop));
556       }
557 
558       /* and compute the result: */
559       for (unsigned i = 0; i < a6xx_backend->num_perfcntrs; i++) {
560          /* result += stop - start: */
561          OUT_PKT7(ring, CP_MEM_TO_MEM, 9);
562          OUT_RING(ring, CP_MEM_TO_MEM_0_DOUBLE | CP_MEM_TO_MEM_0_NEG_C);
563          OUT_RELOC(ring, query_sample_idx(a6xx_backend, i, result)); /* dst */
564          OUT_RELOC(ring, query_sample_idx(a6xx_backend, i, result)); /* srcA */
565          OUT_RELOC(ring, query_sample_idx(a6xx_backend, i, stop));   /* srcB */
566          OUT_RELOC(ring, query_sample_idx(a6xx_backend, i, start));  /* srcC */
567       }
568    }
569 
570    cache_flush<CHIP>(ring, kernel);
571 }
572 
573 static void
a6xx_set_perfcntrs(struct backend * b,const struct perfcntr * perfcntrs,unsigned num_perfcntrs)574 a6xx_set_perfcntrs(struct backend *b, const struct perfcntr *perfcntrs,
575                    unsigned num_perfcntrs)
576 {
577    struct a6xx_backend *a6xx_backend = to_a6xx_backend(b);
578 
579    a6xx_backend->perfcntrs = perfcntrs;
580    a6xx_backend->num_perfcntrs = num_perfcntrs;
581 }
582 
583 static void
a6xx_read_perfcntrs(struct backend * b,uint64_t * results)584 a6xx_read_perfcntrs(struct backend *b, uint64_t *results)
585 {
586    struct a6xx_backend *a6xx_backend = to_a6xx_backend(b);
587 
588    fd_bo_cpu_prep(a6xx_backend->query_mem, NULL, FD_BO_PREP_READ);
589    struct fd6_query_sample *samples =
590       (struct fd6_query_sample *)fd_bo_map(a6xx_backend->query_mem);
591 
592    for (unsigned i = 0; i < a6xx_backend->num_perfcntrs; i++) {
593       results[i] = samples[i].result;
594    }
595 }
596 
597 template<chip CHIP>
598 struct backend *
a6xx_init(struct fd_device * dev,const struct fd_dev_id * dev_id)599 a6xx_init(struct fd_device *dev, const struct fd_dev_id *dev_id)
600 {
601    struct a6xx_backend *a6xx_backend =
602       (struct a6xx_backend *)calloc(1, sizeof(*a6xx_backend));
603 
604    a6xx_backend->base = (struct backend){
605       .assemble = a6xx_assemble,
606       .disassemble = a6xx_disassemble,
607       .emit_grid = a6xx_emit_grid<CHIP>,
608       .set_perfcntrs = a6xx_set_perfcntrs,
609       .read_perfcntrs = a6xx_read_perfcntrs,
610    };
611 
612    struct ir3_compiler_options compiler_options = {};
613    a6xx_backend->compiler =
614       ir3_compiler_create(dev, dev_id, fd_dev_info_raw(dev_id), &compiler_options);
615    a6xx_backend->dev = dev;
616 
617    a6xx_backend->info = fd_dev_info_raw(dev_id);
618 
619    a6xx_backend->control_mem =
620       fd_bo_new(dev, 0x1000, 0, "control");
621 
622    return &a6xx_backend->base;
623 }
624 
625 template
626 struct backend *a6xx_init<A6XX>(struct fd_device *dev, const struct fd_dev_id *dev_id);
627 
628 template
629 struct backend *a6xx_init<A7XX>(struct fd_device *dev, const struct fd_dev_id *dev_id);
630