• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013 Advanced Micro Devices, Inc.
3  *
4  * SPDX-License-Identifier: MIT
5  */
6 
7 #include "ac_rtld.h"
8 #include "amd_kernel_code_t.h"
9 #include "nir/tgsi_to_nir.h"
10 #include "si_build_pm4.h"
11 #include "si_shader_internal.h"
12 #include "util/u_async_debug.h"
13 #include "util/u_memory.h"
14 #include "util/u_upload_mgr.h"
15 #include "si_tracepoints.h"
16 
17 #define COMPUTE_DBG(sscreen, fmt, args...)                                                         \
18    do {                                                                                            \
19       if ((sscreen->debug_flags & DBG(COMPUTE)))                                                   \
20          fprintf(stderr, fmt, ##args);                                                             \
21    } while (0);
22 
23 struct dispatch_packet {
24    uint16_t header;
25    uint16_t setup;
26    uint16_t workgroup_size_x;
27    uint16_t workgroup_size_y;
28    uint16_t workgroup_size_z;
29    uint16_t reserved0;
30    uint32_t grid_size_x;
31    uint32_t grid_size_y;
32    uint32_t grid_size_z;
33    uint32_t group_segment_size;
34    uint64_t kernel_object;
35    uint64_t kernarg_address;
36    uint64_t reserved2;
37 };
38 
si_compute_get_code_object(const struct si_compute * program,uint64_t symbol_offset)39 static const amd_kernel_code_t *si_compute_get_code_object(const struct si_compute *program,
40                                                            uint64_t symbol_offset)
41 {
42    const struct si_shader_selector *sel = &program->sel;
43 
44    if (program->ir_type != PIPE_SHADER_IR_NATIVE)
45       return NULL;
46 
47    struct ac_rtld_binary rtld;
48    if (!ac_rtld_open(&rtld,
49                      (struct ac_rtld_open_info){.info = &sel->screen->info,
50                                                 .shader_type = MESA_SHADER_COMPUTE,
51                                                 .num_parts = 1,
52                                                 .elf_ptrs = &program->shader.binary.code_buffer,
53                                                 .elf_sizes = &program->shader.binary.code_size}))
54       return NULL;
55 
56    const amd_kernel_code_t *result = NULL;
57    const char *text;
58    size_t size;
59    if (!ac_rtld_get_section_by_name(&rtld, ".text", &text, &size))
60       goto out;
61 
62    if (symbol_offset + sizeof(amd_kernel_code_t) > size)
63       goto out;
64 
65    result = (const amd_kernel_code_t *)(text + symbol_offset);
66 
67 out:
68    ac_rtld_close(&rtld);
69    return result;
70 }
71 
code_object_to_config(const amd_kernel_code_t * code_object,struct ac_shader_config * out_config)72 static void code_object_to_config(const amd_kernel_code_t *code_object,
73                                   struct ac_shader_config *out_config)
74 {
75 
76    uint32_t rsrc1 = code_object->compute_pgm_resource_registers;
77    uint32_t rsrc2 = code_object->compute_pgm_resource_registers >> 32;
78    out_config->num_sgprs = code_object->wavefront_sgpr_count;
79    out_config->num_vgprs = code_object->workitem_vgpr_count;
80    out_config->float_mode = G_00B028_FLOAT_MODE(rsrc1);
81    out_config->rsrc1 = rsrc1;
82    out_config->lds_size = MAX2(out_config->lds_size, G_00B84C_LDS_SIZE(rsrc2));
83    out_config->rsrc2 = rsrc2;
84    out_config->scratch_bytes_per_wave =
85       align(code_object->workitem_private_segment_byte_size * 64, 1024);
86 }
87 
88 /* Asynchronous compute shader compilation. */
si_create_compute_state_async(void * job,void * gdata,int thread_index)89 static void si_create_compute_state_async(void *job, void *gdata, int thread_index)
90 {
91    struct si_compute *program = (struct si_compute *)job;
92    struct si_shader_selector *sel = &program->sel;
93    struct si_shader *shader = &program->shader;
94    struct ac_llvm_compiler **compiler;
95    struct util_debug_callback *debug = &sel->compiler_ctx_state.debug;
96    struct si_screen *sscreen = sel->screen;
97 
98    assert(!debug->debug_message || debug->async);
99    assert(thread_index >= 0);
100    assert(thread_index < ARRAY_SIZE(sscreen->compiler));
101    compiler = &sscreen->compiler[thread_index];
102 
103    if (!sscreen->use_aco && !*compiler)
104       *compiler = si_create_llvm_compiler(sscreen);
105 
106    assert(program->ir_type == PIPE_SHADER_IR_NIR);
107    si_nir_scan_shader(sscreen, sel->nir, &sel->info);
108 
109    si_get_active_slot_masks(sscreen, &sel->info, &sel->active_const_and_shader_buffers,
110                             &sel->active_samplers_and_images);
111 
112    program->shader.is_monolithic = true;
113    program->shader.wave_size = si_determine_wave_size(sscreen, &program->shader);
114 
115    /* Variable block sizes need 10 bits (1 + log2(SI_MAX_VARIABLE_THREADS_PER_BLOCK)) per dim.
116     * We pack them into a single user SGPR.
117     */
118    unsigned user_sgprs = SI_NUM_RESOURCE_SGPRS + (sel->info.uses_grid_size ? 3 : 0) +
119                          (sel->info.uses_variable_block_size ? 1 : 0) +
120                          sel->info.base.cs.user_data_components_amd;
121 
122    /* Fast path for compute shaders - some descriptors passed via user SGPRs. */
123    /* Shader buffers in user SGPRs. */
124    for (unsigned i = 0; i < MIN2(3, sel->info.base.num_ssbos) && user_sgprs <= 12; i++) {
125       user_sgprs = align(user_sgprs, 4);
126       if (i == 0)
127          sel->cs_shaderbufs_sgpr_index = user_sgprs;
128       user_sgprs += 4;
129       sel->cs_num_shaderbufs_in_user_sgprs++;
130    }
131 
132    /* Images in user SGPRs. */
133    unsigned non_fmask_images = u_bit_consecutive(0, sel->info.base.num_images);
134 
135    /* Remove images with FMASK from the bitmask.  We only care about the first
136     * 3 anyway, so we can take msaa_images[0] and ignore the rest.
137     */
138    if (sscreen->info.gfx_level < GFX11)
139       non_fmask_images &= ~sel->info.base.msaa_images[0];
140 
141    for (unsigned i = 0; i < 3 && non_fmask_images & (1 << i); i++) {
142       unsigned num_sgprs = BITSET_TEST(sel->info.base.image_buffers, i) ? 4 : 8;
143 
144       if (align(user_sgprs, num_sgprs) + num_sgprs > 16)
145          break;
146 
147       user_sgprs = align(user_sgprs, num_sgprs);
148       if (i == 0)
149          sel->cs_images_sgpr_index = user_sgprs;
150       user_sgprs += num_sgprs;
151       sel->cs_num_images_in_user_sgprs++;
152    }
153    sel->cs_images_num_sgprs = user_sgprs - sel->cs_images_sgpr_index;
154    assert(user_sgprs <= 16);
155 
156    unsigned char ir_sha1_cache_key[20];
157    si_get_ir_cache_key(sel, false, false, shader->wave_size, ir_sha1_cache_key);
158 
159    /* Try to load the shader from the shader cache. */
160    simple_mtx_lock(&sscreen->shader_cache_mutex);
161 
162    if (si_shader_cache_load_shader(sscreen, ir_sha1_cache_key, shader)) {
163       simple_mtx_unlock(&sscreen->shader_cache_mutex);
164 
165       if (!si_shader_binary_upload(sscreen, shader, 0))
166          program->shader.compilation_failed = true;
167 
168       si_shader_dump_stats_for_shader_db(sscreen, shader, debug);
169       si_shader_dump(sscreen, shader, debug, stderr, true);
170    } else {
171       simple_mtx_unlock(&sscreen->shader_cache_mutex);
172 
173       if (!si_create_shader_variant(sscreen, *compiler, &program->shader, debug)) {
174          program->shader.compilation_failed = true;
175          return;
176       }
177 
178       shader->config.rsrc1 = S_00B848_VGPRS((shader->config.num_vgprs - 1) /
179                                             ((shader->wave_size == 32 ||
180                                               sscreen->info.wave64_vgpr_alloc_granularity == 8) ? 8 : 4)) |
181                              S_00B848_DX10_CLAMP(1) |
182                              S_00B848_MEM_ORDERED(si_shader_mem_ordered(shader)) |
183                              S_00B848_FLOAT_MODE(shader->config.float_mode);
184 
185       if (sscreen->info.gfx_level < GFX10) {
186          shader->config.rsrc1 |= S_00B848_SGPRS((shader->config.num_sgprs - 1) / 8);
187       }
188 
189       shader->config.rsrc2 = S_00B84C_USER_SGPR(user_sgprs) |
190                              S_00B84C_SCRATCH_EN(shader->config.scratch_bytes_per_wave > 0) |
191                              S_00B84C_TGID_X_EN(sel->info.uses_block_id[0]) |
192                              S_00B84C_TGID_Y_EN(sel->info.uses_block_id[1]) |
193                              S_00B84C_TGID_Z_EN(sel->info.uses_block_id[2]) |
194                              S_00B84C_TG_SIZE_EN(sel->info.uses_tg_size) |
195                              S_00B84C_TIDIG_COMP_CNT(sel->info.uses_thread_id[2]
196                                                         ? 2
197                                                         : sel->info.uses_thread_id[1] ? 1 : 0) |
198                              S_00B84C_LDS_SIZE(shader->config.lds_size);
199 
200       simple_mtx_lock(&sscreen->shader_cache_mutex);
201       si_shader_cache_insert_shader(sscreen, ir_sha1_cache_key, shader, true);
202       simple_mtx_unlock(&sscreen->shader_cache_mutex);
203    }
204 
205    ralloc_free(sel->nir);
206    sel->nir = NULL;
207 }
208 
si_create_compute_state(struct pipe_context * ctx,const struct pipe_compute_state * cso)209 static void *si_create_compute_state(struct pipe_context *ctx, const struct pipe_compute_state *cso)
210 {
211    struct si_context *sctx = (struct si_context *)ctx;
212    struct si_screen *sscreen = (struct si_screen *)ctx->screen;
213    struct si_compute *program = CALLOC_STRUCT(si_compute);
214    struct si_shader_selector *sel = &program->sel;
215 
216    pipe_reference_init(&sel->base.reference, 1);
217    sel->stage = MESA_SHADER_COMPUTE;
218    sel->screen = sscreen;
219    sel->const_and_shader_buf_descriptors_index =
220       si_const_and_shader_buffer_descriptors_idx(PIPE_SHADER_COMPUTE);
221    sel->sampler_and_images_descriptors_index =
222       si_sampler_and_image_descriptors_idx(PIPE_SHADER_COMPUTE);
223    sel->info.base.shared_size = cso->static_shared_mem;
224    program->shader.selector = &program->sel;
225    program->ir_type = cso->ir_type;
226    program->input_size = cso->req_input_mem;
227 
228    if (cso->ir_type != PIPE_SHADER_IR_NATIVE) {
229       if (cso->ir_type == PIPE_SHADER_IR_TGSI) {
230          program->ir_type = PIPE_SHADER_IR_NIR;
231          sel->nir = tgsi_to_nir(cso->prog, ctx->screen, true);
232       } else {
233          assert(cso->ir_type == PIPE_SHADER_IR_NIR);
234          sel->nir = (struct nir_shader *)cso->prog;
235       }
236 
237       if (si_can_dump_shader(sscreen, sel->stage, SI_DUMP_INIT_NIR))
238          nir_print_shader(sel->nir, stderr);
239 
240       sel->compiler_ctx_state.debug = sctx->debug;
241       sel->compiler_ctx_state.is_debug_context = sctx->is_debug;
242       p_atomic_inc(&sscreen->num_shaders_created);
243 
244       si_schedule_initial_compile(sctx, MESA_SHADER_COMPUTE, &sel->ready, &sel->compiler_ctx_state,
245                                   program, si_create_compute_state_async);
246    } else {
247       const struct pipe_binary_program_header *header;
248       header = cso->prog;
249 
250       program->shader.binary.type = SI_SHADER_BINARY_ELF;
251       program->shader.binary.code_size = header->num_bytes;
252       program->shader.binary.code_buffer = malloc(header->num_bytes);
253       if (!program->shader.binary.code_buffer) {
254          FREE(program);
255          return NULL;
256       }
257       memcpy((void *)program->shader.binary.code_buffer, header->blob, header->num_bytes);
258 
259       const amd_kernel_code_t *code_object = si_compute_get_code_object(program, 0);
260       code_object_to_config(code_object, &program->shader.config);
261 
262       if (AMD_HSA_BITS_GET(code_object->code_properties, AMD_CODE_PROPERTY_ENABLE_WAVEFRONT_SIZE32))
263          program->shader.wave_size = 32;
264       else
265          program->shader.wave_size = 64;
266 
267       bool ok = si_shader_binary_upload(sctx->screen, &program->shader, 0);
268       si_shader_dump(sctx->screen, &program->shader, &sctx->debug, stderr, true);
269 
270       if (!ok) {
271          fprintf(stderr, "LLVM failed to upload shader\n");
272          free((void *)program->shader.binary.code_buffer);
273          FREE(program);
274          return NULL;
275       }
276    }
277 
278    return program;
279 }
280 
si_get_compute_state_info(struct pipe_context * ctx,void * state,struct pipe_compute_state_object_info * info)281 static void si_get_compute_state_info(struct pipe_context *ctx, void *state,
282                                       struct pipe_compute_state_object_info *info)
283 {
284    struct si_compute *program = (struct si_compute *)state;
285    struct si_shader_selector *sel = &program->sel;
286 
287    assert(program->ir_type != PIPE_SHADER_IR_NATIVE);
288 
289    /* Wait because we need the compilation to finish first */
290    util_queue_fence_wait(&sel->ready);
291 
292    uint8_t wave_size = program->shader.wave_size;
293    info->private_memory = DIV_ROUND_UP(program->shader.config.scratch_bytes_per_wave, wave_size);
294    info->preferred_simd_size = wave_size;
295    info->simd_sizes = wave_size;
296    info->max_threads = si_get_max_workgroup_size(&program->shader);
297 }
298 
si_bind_compute_state(struct pipe_context * ctx,void * state)299 static void si_bind_compute_state(struct pipe_context *ctx, void *state)
300 {
301    struct si_context *sctx = (struct si_context *)ctx;
302    struct si_compute *program = (struct si_compute *)state;
303    struct si_shader_selector *sel = &program->sel;
304 
305    sctx->cs_shader_state.program = program;
306    if (!program)
307       return;
308 
309    /* Wait because we need active slot usage masks. */
310    if (program->ir_type != PIPE_SHADER_IR_NATIVE)
311       util_queue_fence_wait(&sel->ready);
312 
313    si_set_active_descriptors(sctx,
314                              SI_DESCS_FIRST_COMPUTE + SI_SHADER_DESCS_CONST_AND_SHADER_BUFFERS,
315                              sel->active_const_and_shader_buffers);
316    si_set_active_descriptors(sctx, SI_DESCS_FIRST_COMPUTE + SI_SHADER_DESCS_SAMPLERS_AND_IMAGES,
317                              sel->active_samplers_and_images);
318 
319    sctx->compute_shaderbuf_sgprs_dirty = true;
320    sctx->compute_image_sgprs_dirty = true;
321 
322    if (unlikely((sctx->screen->debug_flags & DBG(SQTT)) && sctx->sqtt)) {
323       uint32_t pipeline_code_hash = _mesa_hash_data_with_seed(
324          program->shader.binary.code_buffer,
325          program->shader.binary.code_size,
326          0);
327 
328       if (!si_sqtt_pipeline_is_registered(sctx->sqtt, pipeline_code_hash)) {
329          /* Short lived fake pipeline: we don't need to reupload the compute shaders,
330           * as we do for the gfx ones so just create a temp pipeline to be able to
331           * call si_sqtt_register_pipeline, and then drop it.
332           */
333          struct si_sqtt_fake_pipeline pipeline = { 0 };
334          pipeline.code_hash = pipeline_code_hash;
335          pipeline.bo = program->shader.bo;
336 
337          si_sqtt_register_pipeline(sctx, &pipeline, true);
338       }
339 
340       si_sqtt_describe_pipeline_bind(sctx, pipeline_code_hash, 1);
341    }
342 }
343 
si_set_global_binding(struct pipe_context * ctx,unsigned first,unsigned n,struct pipe_resource ** resources,uint32_t ** handles)344 static void si_set_global_binding(struct pipe_context *ctx, unsigned first, unsigned n,
345                                   struct pipe_resource **resources, uint32_t **handles)
346 {
347    unsigned i;
348    struct si_context *sctx = (struct si_context *)ctx;
349    struct si_compute *program = sctx->cs_shader_state.program;
350 
351    if (first + n > program->max_global_buffers) {
352       unsigned old_max = program->max_global_buffers;
353       program->max_global_buffers = first + n;
354       program->global_buffers = realloc(
355          program->global_buffers, program->max_global_buffers * sizeof(program->global_buffers[0]));
356       if (!program->global_buffers) {
357          fprintf(stderr, "radeonsi: failed to allocate compute global_buffers\n");
358          return;
359       }
360 
361       memset(&program->global_buffers[old_max], 0,
362              (program->max_global_buffers - old_max) * sizeof(program->global_buffers[0]));
363    }
364 
365    if (!resources) {
366       for (i = 0; i < n; i++) {
367          pipe_resource_reference(&program->global_buffers[first + i], NULL);
368       }
369       return;
370    }
371 
372    for (i = 0; i < n; i++) {
373       uint64_t va;
374       uint32_t offset;
375       pipe_resource_reference(&program->global_buffers[first + i], resources[i]);
376       va = si_resource(resources[i])->gpu_address;
377       offset = util_le32_to_cpu(*handles[i]);
378       va += offset;
379       va = util_cpu_to_le64(va);
380       memcpy(handles[i], &va, sizeof(va));
381    }
382 }
383 
si_setup_compute_scratch_buffer(struct si_context * sctx,struct si_shader * shader)384 static bool si_setup_compute_scratch_buffer(struct si_context *sctx, struct si_shader *shader)
385 {
386    uint64_t scratch_bo_size, scratch_needed;
387    scratch_bo_size = 0;
388    scratch_needed = sctx->max_seen_compute_scratch_bytes_per_wave * sctx->screen->info.max_scratch_waves;
389    if (sctx->compute_scratch_buffer)
390       scratch_bo_size = sctx->compute_scratch_buffer->b.b.width0;
391 
392    if (scratch_bo_size < scratch_needed) {
393       si_resource_reference(&sctx->compute_scratch_buffer, NULL);
394 
395       sctx->compute_scratch_buffer =
396          si_aligned_buffer_create(&sctx->screen->b,
397                                   PIPE_RESOURCE_FLAG_UNMAPPABLE | SI_RESOURCE_FLAG_DRIVER_INTERNAL |
398                                   SI_RESOURCE_FLAG_DISCARDABLE,
399                                   PIPE_USAGE_DEFAULT,
400                                   scratch_needed, sctx->screen->info.pte_fragment_size);
401 
402       if (!sctx->compute_scratch_buffer)
403          return false;
404    }
405 
406    if (sctx->compute_scratch_buffer != shader->scratch_bo && scratch_needed) {
407       if (sctx->gfx_level < GFX11 &&
408           (sctx->family < CHIP_GFX940 || sctx->screen->info.has_graphics)) {
409          uint64_t scratch_va = sctx->compute_scratch_buffer->gpu_address;
410 
411          if (!si_shader_binary_upload(sctx->screen, shader, scratch_va))
412             return false;
413       }
414       si_resource_reference(&shader->scratch_bo, sctx->compute_scratch_buffer);
415    }
416 
417    return true;
418 }
419 
si_switch_compute_shader(struct si_context * sctx,struct si_compute * program,struct si_shader * shader,const amd_kernel_code_t * code_object,unsigned offset,bool * prefetch,unsigned variable_shared_size)420 static bool si_switch_compute_shader(struct si_context *sctx, struct si_compute *program,
421                                      struct si_shader *shader, const amd_kernel_code_t *code_object,
422                                      unsigned offset, bool *prefetch, unsigned variable_shared_size)
423 {
424    struct radeon_cmdbuf *cs = &sctx->gfx_cs;
425    struct ac_shader_config inline_config = {0};
426    const struct ac_shader_config *config;
427    unsigned rsrc2;
428    uint64_t shader_va;
429    unsigned stage = shader->selector->info.base.stage;
430 
431    *prefetch = false;
432 
433    assert(variable_shared_size == 0 || stage == MESA_SHADER_KERNEL || program->ir_type == PIPE_SHADER_IR_NATIVE);
434    if (sctx->cs_shader_state.emitted_program == program && sctx->cs_shader_state.offset == offset &&
435        sctx->cs_shader_state.variable_shared_size == variable_shared_size)
436       return true;
437 
438    if (program->ir_type != PIPE_SHADER_IR_NATIVE) {
439       config = &shader->config;
440    } else {
441       code_object_to_config(code_object, &inline_config);
442       config = &inline_config;
443    }
444    /* copy rsrc2 so we don't have to change it inside the si_shader object */
445    rsrc2 = config->rsrc2;
446 
447    /* only do this for OpenCL */
448    if (program->ir_type == PIPE_SHADER_IR_NATIVE || stage == MESA_SHADER_KERNEL) {
449       unsigned shared_size = program->sel.info.base.shared_size + variable_shared_size;
450       unsigned lds_blocks;
451 
452       /* Clover uses the compute API differently than other frontends and expects drivers to parse
453        * the shared_size out of the shader headers.
454        */
455       if (program->ir_type == PIPE_SHADER_IR_NATIVE) {
456          lds_blocks = config->lds_size;
457       } else {
458          lds_blocks = 0;
459       }
460 
461       /* XXX: We are over allocating LDS.  For GFX6, the shader reports
462        * LDS in blocks of 256 bytes, so if there are 4 bytes lds
463        * allocated in the shader and 4 bytes allocated by the state
464        * tracker, then we will set LDS_SIZE to 512 bytes rather than 256.
465        */
466       if (sctx->gfx_level <= GFX6) {
467          lds_blocks += align(shared_size, 256) >> 8;
468       } else {
469          lds_blocks += align(shared_size, 512) >> 9;
470       }
471 
472       /* TODO: use si_multiwave_lds_size_workaround */
473       assert(lds_blocks <= 0xFF);
474 
475       rsrc2 &= C_00B84C_LDS_SIZE;
476       rsrc2 |= S_00B84C_LDS_SIZE(lds_blocks);
477    }
478 
479    unsigned tmpring_size;
480    ac_get_scratch_tmpring_size(&sctx->screen->info,
481                                config->scratch_bytes_per_wave,
482                                &sctx->max_seen_compute_scratch_bytes_per_wave, &tmpring_size);
483 
484    if (!si_setup_compute_scratch_buffer(sctx, shader))
485       return false;
486 
487    if (shader->scratch_bo) {
488       radeon_add_to_buffer_list(sctx, &sctx->gfx_cs, shader->scratch_bo,
489                                 RADEON_USAGE_READWRITE | RADEON_PRIO_SCRATCH_BUFFER);
490    }
491 
492    shader_va = shader->bo->gpu_address + offset;
493    if (program->ir_type == PIPE_SHADER_IR_NATIVE) {
494       /* Shader code is placed after the amd_kernel_code_t
495        * struct. */
496       shader_va += sizeof(amd_kernel_code_t);
497    }
498 
499    radeon_add_to_buffer_list(sctx, &sctx->gfx_cs, shader->bo,
500                              RADEON_USAGE_READ | RADEON_PRIO_SHADER_BINARY);
501 
502    if (sctx->screen->info.has_set_sh_pairs_packed) {
503       gfx11_push_compute_sh_reg(R_00B830_COMPUTE_PGM_LO, shader_va >> 8);
504       gfx11_opt_push_compute_sh_reg(R_00B848_COMPUTE_PGM_RSRC1,
505                                     SI_TRACKED_COMPUTE_PGM_RSRC1, config->rsrc1);
506       gfx11_opt_push_compute_sh_reg(R_00B84C_COMPUTE_PGM_RSRC2,
507                                     SI_TRACKED_COMPUTE_PGM_RSRC2, rsrc2);
508       gfx11_opt_push_compute_sh_reg(R_00B8A0_COMPUTE_PGM_RSRC3,
509                                     SI_TRACKED_COMPUTE_PGM_RSRC3,
510                                     S_00B8A0_INST_PREF_SIZE(si_get_shader_prefetch_size(shader)));
511       gfx11_opt_push_compute_sh_reg(R_00B860_COMPUTE_TMPRING_SIZE,
512                                     SI_TRACKED_COMPUTE_TMPRING_SIZE, tmpring_size);
513       if (shader->scratch_bo) {
514          gfx11_opt_push_compute_sh_reg(R_00B840_COMPUTE_DISPATCH_SCRATCH_BASE_LO,
515                                        SI_TRACKED_COMPUTE_DISPATCH_SCRATCH_BASE_LO,
516                                        sctx->compute_scratch_buffer->gpu_address >> 8);
517          gfx11_opt_push_compute_sh_reg(R_00B844_COMPUTE_DISPATCH_SCRATCH_BASE_HI,
518                                        SI_TRACKED_COMPUTE_DISPATCH_SCRATCH_BASE_HI,
519                                        sctx->compute_scratch_buffer->gpu_address >> 40);
520       }
521    } else {
522       radeon_begin(cs);
523       radeon_set_sh_reg(R_00B830_COMPUTE_PGM_LO, shader_va >> 8);
524       radeon_opt_set_sh_reg2(sctx, R_00B848_COMPUTE_PGM_RSRC1,
525                              SI_TRACKED_COMPUTE_PGM_RSRC1,
526                              config->rsrc1, rsrc2);
527       radeon_opt_set_sh_reg(sctx, R_00B860_COMPUTE_TMPRING_SIZE,
528                             SI_TRACKED_COMPUTE_TMPRING_SIZE, tmpring_size);
529 
530       if (shader->scratch_bo &&
531           (sctx->gfx_level >= GFX11 ||
532            (sctx->family >= CHIP_GFX940 && !sctx->screen->info.has_graphics))) {
533          radeon_opt_set_sh_reg2(sctx, R_00B840_COMPUTE_DISPATCH_SCRATCH_BASE_LO,
534                                 SI_TRACKED_COMPUTE_DISPATCH_SCRATCH_BASE_LO,
535                                 sctx->compute_scratch_buffer->gpu_address >> 8,
536                                 sctx->compute_scratch_buffer->gpu_address >> 40);
537       }
538 
539       if (sctx->gfx_level >= GFX11) {
540          radeon_opt_set_sh_reg(sctx, R_00B8A0_COMPUTE_PGM_RSRC3,
541                                SI_TRACKED_COMPUTE_PGM_RSRC3,
542                                S_00B8A0_INST_PREF_SIZE(si_get_shader_prefetch_size(shader)));
543       }
544       radeon_end();
545    }
546 
547    COMPUTE_DBG(sctx->screen,
548                "COMPUTE_PGM_RSRC1: 0x%08x "
549                "COMPUTE_PGM_RSRC2: 0x%08x\n",
550                config->rsrc1, config->rsrc2);
551 
552    sctx->cs_shader_state.emitted_program = program;
553    sctx->cs_shader_state.offset = offset;
554    sctx->cs_shader_state.variable_shared_size = variable_shared_size;
555 
556    *prefetch = true;
557    return true;
558 }
559 
setup_scratch_rsrc_user_sgprs(struct si_context * sctx,const amd_kernel_code_t * code_object,unsigned user_sgpr)560 static void setup_scratch_rsrc_user_sgprs(struct si_context *sctx,
561                                           const amd_kernel_code_t *code_object, unsigned user_sgpr)
562 {
563    struct radeon_cmdbuf *cs = &sctx->gfx_cs;
564    uint64_t scratch_va = sctx->compute_scratch_buffer->gpu_address;
565 
566    unsigned max_private_element_size =
567       AMD_HSA_BITS_GET(code_object->code_properties, AMD_CODE_PROPERTY_PRIVATE_ELEMENT_SIZE);
568 
569    uint32_t scratch_dword0 = scratch_va & 0xffffffff;
570    uint32_t scratch_dword1 = S_008F04_BASE_ADDRESS_HI(scratch_va >> 32);
571 
572    if (sctx->gfx_level >= GFX11)
573       scratch_dword1 |= S_008F04_SWIZZLE_ENABLE_GFX11(1);
574    else
575       scratch_dword1 |= S_008F04_SWIZZLE_ENABLE_GFX6(1);
576 
577    /* Disable address clamping */
578    uint32_t scratch_dword2 = 0xffffffff;
579    uint32_t index_stride = sctx->cs_shader_state.program->shader.wave_size == 32 ? 2 : 3;
580    uint32_t scratch_dword3 = S_008F0C_INDEX_STRIDE(index_stride) | S_008F0C_ADD_TID_ENABLE(1);
581 
582    if (sctx->gfx_level >= GFX9) {
583       assert(max_private_element_size == 1); /* only 4 bytes on GFX9 */
584    } else {
585       scratch_dword3 |= S_008F0C_ELEMENT_SIZE(max_private_element_size);
586 
587       if (sctx->gfx_level < GFX8) {
588          /* BUF_DATA_FORMAT is ignored, but it cannot be
589           * BUF_DATA_FORMAT_INVALID. */
590          scratch_dword3 |= S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_8);
591       }
592    }
593 
594    radeon_begin(cs);
595    radeon_set_sh_reg_seq(R_00B900_COMPUTE_USER_DATA_0 + (user_sgpr * 4), 4);
596    radeon_emit(scratch_dword0);
597    radeon_emit(scratch_dword1);
598    radeon_emit(scratch_dword2);
599    radeon_emit(scratch_dword3);
600    radeon_end();
601 }
602 
si_setup_user_sgprs_co_v2(struct si_context * sctx,const amd_kernel_code_t * code_object,const struct pipe_grid_info * info,uint64_t kernel_args_va)603 static void si_setup_user_sgprs_co_v2(struct si_context *sctx, const amd_kernel_code_t *code_object,
604                                       const struct pipe_grid_info *info, uint64_t kernel_args_va)
605 {
606    struct si_compute *program = sctx->cs_shader_state.program;
607    struct radeon_cmdbuf *cs = &sctx->gfx_cs;
608 
609    static const enum amd_code_property_mask_t workgroup_count_masks[] = {
610       AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_X,
611       AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Y,
612       AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Z};
613 
614    unsigned i, user_sgpr = 0;
615    if (AMD_HSA_BITS_GET(code_object->code_properties,
616                         AMD_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER)) {
617       if (code_object->workitem_private_segment_byte_size > 0) {
618          setup_scratch_rsrc_user_sgprs(sctx, code_object, user_sgpr);
619       }
620       user_sgpr += 4;
621    }
622 
623    radeon_begin(cs);
624 
625    if (AMD_HSA_BITS_GET(code_object->code_properties, AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR)) {
626       struct dispatch_packet dispatch;
627       unsigned dispatch_offset;
628       struct si_resource *dispatch_buf = NULL;
629       uint64_t dispatch_va;
630 
631       /* Upload dispatch ptr */
632       memset(&dispatch, 0, sizeof(dispatch));
633 
634       dispatch.workgroup_size_x = util_cpu_to_le16(info->block[0]);
635       dispatch.workgroup_size_y = util_cpu_to_le16(info->block[1]);
636       dispatch.workgroup_size_z = util_cpu_to_le16(info->block[2]);
637 
638       dispatch.grid_size_x = util_cpu_to_le32(info->grid[0] * info->block[0]);
639       dispatch.grid_size_y = util_cpu_to_le32(info->grid[1] * info->block[1]);
640       dispatch.grid_size_z = util_cpu_to_le32(info->grid[2] * info->block[2]);
641 
642       dispatch.group_segment_size =
643          util_cpu_to_le32(program->sel.info.base.shared_size + info->variable_shared_mem);
644 
645       dispatch.kernarg_address = util_cpu_to_le64(kernel_args_va);
646 
647       u_upload_data(sctx->b.const_uploader, 0, sizeof(dispatch), 256, &dispatch, &dispatch_offset,
648                     (struct pipe_resource **)&dispatch_buf);
649 
650       if (!dispatch_buf) {
651          fprintf(stderr, "Error: Failed to allocate dispatch "
652                          "packet.");
653       }
654       radeon_add_to_buffer_list(sctx, &sctx->gfx_cs, dispatch_buf,
655                                 RADEON_USAGE_READ | RADEON_PRIO_CONST_BUFFER);
656 
657       dispatch_va = dispatch_buf->gpu_address + dispatch_offset;
658 
659       radeon_set_sh_reg_seq(R_00B900_COMPUTE_USER_DATA_0 + (user_sgpr * 4), 2);
660       radeon_emit(dispatch_va);
661       radeon_emit(S_008F04_BASE_ADDRESS_HI(dispatch_va >> 32) | S_008F04_STRIDE(0));
662 
663       si_resource_reference(&dispatch_buf, NULL);
664       user_sgpr += 2;
665    }
666 
667    if (AMD_HSA_BITS_GET(code_object->code_properties,
668                         AMD_CODE_PROPERTY_ENABLE_SGPR_KERNARG_SEGMENT_PTR)) {
669       radeon_set_sh_reg_seq(R_00B900_COMPUTE_USER_DATA_0 + (user_sgpr * 4), 2);
670       radeon_emit(kernel_args_va);
671       radeon_emit(S_008F04_BASE_ADDRESS_HI(kernel_args_va >> 32) | S_008F04_STRIDE(0));
672       user_sgpr += 2;
673    }
674 
675    for (i = 0; i < 3 && user_sgpr < 16; i++) {
676       if (code_object->code_properties & workgroup_count_masks[i]) {
677          radeon_set_sh_reg_seq(R_00B900_COMPUTE_USER_DATA_0 + (user_sgpr * 4), 1);
678          radeon_emit(info->grid[i]);
679          user_sgpr += 1;
680       }
681    }
682    radeon_end();
683 }
684 
si_upload_compute_input(struct si_context * sctx,const amd_kernel_code_t * code_object,const struct pipe_grid_info * info)685 static bool si_upload_compute_input(struct si_context *sctx, const amd_kernel_code_t *code_object,
686                                     const struct pipe_grid_info *info)
687 {
688    struct si_compute *program = sctx->cs_shader_state.program;
689    struct si_resource *input_buffer = NULL;
690    uint32_t kernel_args_offset = 0;
691    uint32_t *kernel_args;
692    void *kernel_args_ptr;
693    uint64_t kernel_args_va;
694 
695    u_upload_alloc(sctx->b.const_uploader, 0, program->input_size,
696                   sctx->screen->info.tcc_cache_line_size, &kernel_args_offset,
697                   (struct pipe_resource **)&input_buffer, &kernel_args_ptr);
698 
699    if (unlikely(!kernel_args_ptr))
700       return false;
701 
702    kernel_args = (uint32_t *)kernel_args_ptr;
703    kernel_args_va = input_buffer->gpu_address + kernel_args_offset;
704 
705    memcpy(kernel_args, info->input, program->input_size);
706 
707    for (unsigned i = 0; i < program->input_size / 4; i++) {
708       COMPUTE_DBG(sctx->screen, "input %u : %u\n", i, kernel_args[i]);
709    }
710 
711    radeon_add_to_buffer_list(sctx, &sctx->gfx_cs, input_buffer,
712                              RADEON_USAGE_READ | RADEON_PRIO_CONST_BUFFER);
713 
714    si_setup_user_sgprs_co_v2(sctx, code_object, info, kernel_args_va);
715    si_resource_reference(&input_buffer, NULL);
716    return true;
717 }
718 
si_setup_nir_user_data(struct si_context * sctx,const struct pipe_grid_info * info)719 static void si_setup_nir_user_data(struct si_context *sctx, const struct pipe_grid_info *info)
720 {
721    struct si_compute *program = sctx->cs_shader_state.program;
722    struct si_shader_selector *sel = &program->sel;
723    struct radeon_cmdbuf *cs = &sctx->gfx_cs;
724    unsigned grid_size_reg = R_00B900_COMPUTE_USER_DATA_0 + 4 * SI_NUM_RESOURCE_SGPRS;
725    unsigned block_size_reg = grid_size_reg +
726                              /* 12 bytes = 3 dwords. */
727                              12 * sel->info.uses_grid_size;
728    unsigned cs_user_data_reg = block_size_reg + 4 * program->sel.info.uses_variable_block_size;
729 
730    if (sel->info.uses_grid_size && info->indirect) {
731       for (unsigned i = 0; i < 3; ++i) {
732          si_cp_copy_data(sctx, &sctx->gfx_cs, COPY_DATA_REG, NULL, (grid_size_reg >> 2) + i,
733                          COPY_DATA_SRC_MEM, si_resource(info->indirect),
734                          info->indirect_offset + 4 * i);
735       }
736    }
737 
738    if (sctx->screen->info.has_set_sh_pairs_packed) {
739       if (sel->info.uses_grid_size && !info->indirect) {
740          gfx11_push_compute_sh_reg(grid_size_reg, info->grid[0]);
741          gfx11_push_compute_sh_reg(grid_size_reg + 4, info->grid[1]);
742          gfx11_push_compute_sh_reg(grid_size_reg + 8, info->grid[2]);
743       }
744 
745       if (sel->info.uses_variable_block_size) {
746          uint32_t value = info->block[0] | (info->block[1] << 10) | (info->block[2] << 20);
747          gfx11_push_compute_sh_reg(block_size_reg, value);
748       }
749 
750       if (sel->info.base.cs.user_data_components_amd) {
751          unsigned num = sel->info.base.cs.user_data_components_amd;
752          for (unsigned i = 0; i < num; i++)
753             gfx11_push_compute_sh_reg(cs_user_data_reg + i * 4, sctx->cs_user_data[i]);
754       }
755    } else {
756       radeon_begin(cs);
757 
758       if (sel->info.uses_grid_size && !info->indirect) {
759          radeon_set_sh_reg_seq(grid_size_reg, 3);
760          radeon_emit(info->grid[0]);
761          radeon_emit(info->grid[1]);
762          radeon_emit(info->grid[2]);
763       }
764 
765       if (sel->info.uses_variable_block_size) {
766          uint32_t value = info->block[0] | (info->block[1] << 10) | (info->block[2] << 20);
767          radeon_set_sh_reg(block_size_reg, value);
768       }
769 
770       if (sel->info.base.cs.user_data_components_amd) {
771          unsigned num = sel->info.base.cs.user_data_components_amd;
772          radeon_set_sh_reg_seq(cs_user_data_reg, num);
773          radeon_emit_array(sctx->cs_user_data, num);
774       }
775       radeon_end();
776    }
777 }
778 
si_emit_dispatch_packets(struct si_context * sctx,const struct pipe_grid_info * info)779 static void si_emit_dispatch_packets(struct si_context *sctx, const struct pipe_grid_info *info)
780 {
781    struct si_screen *sscreen = sctx->screen;
782    struct radeon_cmdbuf *cs = &sctx->gfx_cs;
783    bool render_cond_bit = sctx->render_cond_enabled;
784    unsigned threads_per_threadgroup = info->block[0] * info->block[1] * info->block[2];
785    unsigned waves_per_threadgroup =
786       DIV_ROUND_UP(threads_per_threadgroup, sctx->cs_shader_state.program->shader.wave_size);
787    unsigned threadgroups_per_cu = 1;
788 
789    if (sctx->gfx_level >= GFX10 && waves_per_threadgroup == 1)
790       threadgroups_per_cu = 2;
791 
792    if (unlikely(sctx->sqtt_enabled)) {
793       if (info->indirect) {
794          si_sqtt_write_event_marker(sctx, &sctx->gfx_cs,
795                                     EventCmdDispatchIndirect,
796                                     UINT_MAX, UINT_MAX, UINT_MAX);
797       } else {
798          si_write_event_with_dims_marker(sctx, &sctx->gfx_cs,
799                                          EventCmdDispatch,
800                                          info->grid[0], info->grid[1], info->grid[2]);
801       }
802    }
803 
804    radeon_begin(cs);
805    unsigned compute_resource_limits =
806       ac_get_compute_resource_limits(&sscreen->info, waves_per_threadgroup,
807                                      sctx->cs_max_waves_per_sh,
808                                      threadgroups_per_cu);
809 
810    if (sctx->screen->info.has_set_sh_pairs_packed) {
811       gfx11_opt_push_compute_sh_reg(R_00B854_COMPUTE_RESOURCE_LIMITS,
812                                     SI_TRACKED_COMPUTE_RESOURCE_LIMITS,
813                                     compute_resource_limits);
814    } else {
815       radeon_opt_set_sh_reg(sctx, R_00B854_COMPUTE_RESOURCE_LIMITS,
816                             SI_TRACKED_COMPUTE_RESOURCE_LIMITS,
817                             compute_resource_limits);
818    }
819 
820    unsigned dispatch_initiator = S_00B800_COMPUTE_SHADER_EN(1) | S_00B800_FORCE_START_AT_000(1) |
821                                  /* If the KMD allows it (there is a KMD hw register for it),
822                                   * allow launching waves out-of-order. (same as Vulkan)
823                                   * Not available in gfx940.
824                                   */
825                                  S_00B800_ORDER_MODE(sctx->gfx_level >= GFX7 &&
826                                                      (sctx->family < CHIP_GFX940 || sctx->screen->info.has_graphics)) |
827                                  S_00B800_CS_W32_EN(sctx->cs_shader_state.program->shader.wave_size == 32);
828 
829    const uint *last_block = info->last_block;
830    bool partial_block_en = last_block[0] || last_block[1] || last_block[2];
831    uint32_t num_threads[3];
832 
833    num_threads[0] = S_00B81C_NUM_THREAD_FULL(info->block[0]);
834    num_threads[1] = S_00B820_NUM_THREAD_FULL(info->block[1]);
835    num_threads[2] = S_00B824_NUM_THREAD_FULL(info->block[2]);
836 
837    if (partial_block_en) {
838       unsigned partial[3];
839 
840       /* If no partial_block, these should be an entire block size, not 0. */
841       partial[0] = last_block[0] ? last_block[0] : info->block[0];
842       partial[1] = last_block[1] ? last_block[1] : info->block[1];
843       partial[2] = last_block[2] ? last_block[2] : info->block[2];
844 
845       num_threads[0] |= S_00B81C_NUM_THREAD_PARTIAL(partial[0]);
846       num_threads[1] |= S_00B820_NUM_THREAD_PARTIAL(partial[1]);
847       num_threads[2] |= S_00B824_NUM_THREAD_PARTIAL(partial[2]);
848 
849       dispatch_initiator |= S_00B800_PARTIAL_TG_EN(1);
850    }
851 
852    if (sctx->screen->info.has_set_sh_pairs_packed) {
853       gfx11_opt_push_compute_sh_reg(R_00B81C_COMPUTE_NUM_THREAD_X,
854                                     SI_TRACKED_COMPUTE_NUM_THREAD_X, num_threads[0]);
855       gfx11_opt_push_compute_sh_reg(R_00B820_COMPUTE_NUM_THREAD_Y,
856                                     SI_TRACKED_COMPUTE_NUM_THREAD_Y, num_threads[1]);
857       gfx11_opt_push_compute_sh_reg(R_00B824_COMPUTE_NUM_THREAD_Z,
858                                     SI_TRACKED_COMPUTE_NUM_THREAD_Z, num_threads[2]);
859    } else {
860       radeon_opt_set_sh_reg3(sctx, R_00B81C_COMPUTE_NUM_THREAD_X,
861                              SI_TRACKED_COMPUTE_NUM_THREAD_X,
862                              num_threads[0], num_threads[1], num_threads[2]);
863    }
864 
865    if (sctx->gfx_level >= GFX11) {
866       radeon_end();
867       si_emit_buffered_compute_sh_regs(sctx);
868       radeon_begin_again(cs);
869    }
870 
871    if (info->indirect) {
872       uint64_t base_va = si_resource(info->indirect)->gpu_address;
873 
874       radeon_add_to_buffer_list(sctx, &sctx->gfx_cs, si_resource(info->indirect),
875                                 RADEON_USAGE_READ | RADEON_PRIO_DRAW_INDIRECT);
876 
877       radeon_emit(PKT3(PKT3_SET_BASE, 2, 0) | PKT3_SHADER_TYPE_S(1));
878       radeon_emit(1);
879       radeon_emit(base_va);
880       radeon_emit(base_va >> 32);
881 
882       radeon_emit(PKT3(PKT3_DISPATCH_INDIRECT, 1, render_cond_bit) | PKT3_SHADER_TYPE_S(1));
883       radeon_emit(info->indirect_offset);
884       radeon_emit(dispatch_initiator);
885    } else {
886       radeon_emit(PKT3(PKT3_DISPATCH_DIRECT, 3, render_cond_bit) | PKT3_SHADER_TYPE_S(1));
887       radeon_emit(info->grid[0]);
888       radeon_emit(info->grid[1]);
889       radeon_emit(info->grid[2]);
890       radeon_emit(dispatch_initiator);
891    }
892 
893    if (unlikely(sctx->sqtt_enabled && sctx->gfx_level >= GFX9)) {
894       radeon_emit(PKT3(PKT3_EVENT_WRITE, 0, 0));
895       radeon_emit(EVENT_TYPE(V_028A90_THREAD_TRACE_MARKER) | EVENT_INDEX(0));
896    }
897    radeon_end();
898 }
899 
si_check_needs_implicit_sync(struct si_context * sctx)900 static bool si_check_needs_implicit_sync(struct si_context *sctx)
901 {
902    /* If the compute shader is going to read from a texture/image written by a
903     * previous draw, we must wait for its completion before continuing.
904     * Buffers and image stores (from the draw) are not taken into consideration
905     * because that's the app responsibility.
906     *
907     * The OpenGL 4.6 spec says:
908     *
909     *    buffer object and texture stores performed by shaders are not
910     *    automatically synchronized
911     *
912     * TODO: Bindless textures are not handled, and thus are not synchronized.
913     */
914    struct si_shader_info *info = &sctx->cs_shader_state.program->sel.info;
915    struct si_samplers *samplers = &sctx->samplers[PIPE_SHADER_COMPUTE];
916    unsigned mask = samplers->enabled_mask & info->base.textures_used[0];
917 
918    while (mask) {
919       int i = u_bit_scan(&mask);
920       struct si_sampler_view *sview = (struct si_sampler_view *)samplers->views[i];
921 
922       struct si_resource *res = si_resource(sview->base.texture);
923       if (sctx->ws->cs_is_buffer_referenced(&sctx->gfx_cs, res->buf,
924                                             RADEON_USAGE_NEEDS_IMPLICIT_SYNC))
925          return true;
926    }
927 
928    struct si_images *images = &sctx->images[PIPE_SHADER_COMPUTE];
929    mask = u_bit_consecutive(0, info->base.num_images) & images->enabled_mask;
930 
931    while (mask) {
932       int i = u_bit_scan(&mask);
933       struct pipe_image_view *sview = &images->views[i];
934 
935       struct si_resource *res = si_resource(sview->resource);
936       if (sctx->ws->cs_is_buffer_referenced(&sctx->gfx_cs, res->buf,
937                                             RADEON_USAGE_NEEDS_IMPLICIT_SYNC))
938          return true;
939    }
940    return false;
941 }
942 
si_launch_grid(struct pipe_context * ctx,const struct pipe_grid_info * info)943 static void si_launch_grid(struct pipe_context *ctx, const struct pipe_grid_info *info)
944 {
945    struct si_context *sctx = (struct si_context *)ctx;
946    struct si_screen *sscreen = sctx->screen;
947    struct si_compute *program = sctx->cs_shader_state.program;
948    const amd_kernel_code_t *code_object = si_compute_get_code_object(program, info->pc);
949    int i;
950    bool cs_regalloc_hang = sscreen->info.has_cs_regalloc_hang_bug &&
951                            info->block[0] * info->block[1] * info->block[2] > 256;
952 
953    if (cs_regalloc_hang) {
954       sctx->flags |= SI_CONTEXT_PS_PARTIAL_FLUSH | SI_CONTEXT_CS_PARTIAL_FLUSH;
955       si_mark_atom_dirty(sctx, &sctx->atoms.s.cache_flush);
956    }
957 
958    if (program->ir_type != PIPE_SHADER_IR_NATIVE && program->shader.compilation_failed)
959       return;
960 
961    si_check_dirty_buffers_textures(sctx);
962 
963    if (sctx->has_graphics) {
964       if (sctx->last_num_draw_calls != sctx->num_draw_calls) {
965          si_update_fb_dirtiness_after_rendering(sctx);
966          sctx->last_num_draw_calls = sctx->num_draw_calls;
967 
968          if (sctx->force_cb_shader_coherent || si_check_needs_implicit_sync(sctx))
969             si_make_CB_shader_coherent(sctx, 0,
970                                        sctx->framebuffer.CB_has_shader_readable_metadata,
971                                        sctx->framebuffer.all_DCC_pipe_aligned);
972       }
973 
974       if (sctx->gfx_level < GFX11)
975          gfx6_decompress_textures(sctx, 1 << PIPE_SHADER_COMPUTE);
976       else
977          gfx11_decompress_textures(sctx, 1 << PIPE_SHADER_COMPUTE);
978    }
979 
980    if (info->indirect) {
981       /* Indirect buffers use TC L2 on GFX9, but not older hw. */
982       if (sctx->gfx_level <= GFX8 && si_resource(info->indirect)->TC_L2_dirty) {
983          sctx->flags |= SI_CONTEXT_WB_L2;
984          si_mark_atom_dirty(sctx, &sctx->atoms.s.cache_flush);
985          si_resource(info->indirect)->TC_L2_dirty = false;
986       }
987    }
988 
989    si_need_gfx_cs_space(sctx, 0);
990 
991    /* If we're using a secure context, determine if cs must be secure or not */
992    if (unlikely(radeon_uses_secure_bos(sctx->ws))) {
993       bool secure = si_compute_resources_check_encrypted(sctx);
994       if (secure != sctx->ws->cs_is_secure(&sctx->gfx_cs)) {
995          si_flush_gfx_cs(sctx, RADEON_FLUSH_ASYNC_START_NEXT_GFX_IB_NOW |
996                                RADEON_FLUSH_TOGGLE_SECURE_SUBMISSION,
997                          NULL);
998       }
999    }
1000 
1001    if (u_trace_perfetto_active(&sctx->ds.trace_context))
1002       trace_si_begin_compute(&sctx->trace);
1003 
1004    if (sctx->bo_list_add_all_compute_resources)
1005       si_compute_resources_add_all_to_bo_list(sctx);
1006 
1007    /* Skipping setting redundant registers on compute queues breaks compute. */
1008    if (!sctx->has_graphics) {
1009       BITSET_CLEAR_RANGE(sctx->tracked_regs.reg_saved_mask,
1010                          SI_FIRST_TRACKED_OTHER_REG, SI_NUM_ALL_TRACKED_REGS - 1);
1011    }
1012 
1013    /* First emit registers. */
1014    bool prefetch;
1015    if (!si_switch_compute_shader(sctx, program, &program->shader, code_object, info->pc, &prefetch,
1016                                  info->variable_shared_mem))
1017       return;
1018 
1019    si_emit_compute_shader_pointers(sctx);
1020 
1021    if (program->ir_type == PIPE_SHADER_IR_NATIVE &&
1022        unlikely(!si_upload_compute_input(sctx, code_object, info)))
1023       return;
1024 
1025    /* Global buffers */
1026    for (i = 0; i < program->max_global_buffers; i++) {
1027       struct si_resource *buffer = si_resource(program->global_buffers[i]);
1028       if (!buffer) {
1029          continue;
1030       }
1031       radeon_add_to_buffer_list(sctx, &sctx->gfx_cs, buffer,
1032                                 RADEON_USAGE_READWRITE | RADEON_PRIO_SHADER_RW_BUFFER);
1033    }
1034 
1035    /* Registers that are not read from memory should be set before this: */
1036    if (sctx->flags)
1037       si_emit_cache_flush_direct(sctx);
1038 
1039    if (sctx->has_graphics && si_is_atom_dirty(sctx, &sctx->atoms.s.render_cond)) {
1040       sctx->atoms.s.render_cond.emit(sctx, -1);
1041       si_set_atom_dirty(sctx, &sctx->atoms.s.render_cond, false);
1042    }
1043 
1044    /* Prefetch the compute shader to L2. */
1045    if (sctx->gfx_level >= GFX7 && prefetch)
1046       si_cp_dma_prefetch(sctx, &program->shader.bo->b.b, 0, program->shader.bo->b.b.width0);
1047 
1048    if (program->ir_type != PIPE_SHADER_IR_NATIVE)
1049       si_setup_nir_user_data(sctx, info);
1050 
1051    si_emit_dispatch_packets(sctx, info);
1052 
1053    if (unlikely(sctx->current_saved_cs)) {
1054       si_trace_emit(sctx);
1055       si_log_compute_state(sctx, sctx->log);
1056    }
1057 
1058    /* Mark displayable DCC as dirty for bound images. */
1059    unsigned display_dcc_store_mask = sctx->images[PIPE_SHADER_COMPUTE].display_dcc_store_mask &
1060                                BITFIELD_MASK(program->sel.info.base.num_images);
1061    while (display_dcc_store_mask) {
1062       struct si_texture *tex = (struct si_texture *)
1063          sctx->images[PIPE_SHADER_COMPUTE].views[u_bit_scan(&display_dcc_store_mask)].resource;
1064 
1065       si_mark_display_dcc_dirty(sctx, tex);
1066    }
1067 
1068    /* TODO: Bindless images don't set displayable_dcc_dirty after image stores. */
1069 
1070    sctx->compute_is_busy = true;
1071    sctx->num_compute_calls++;
1072 
1073    if (u_trace_perfetto_active(&sctx->ds.trace_context))
1074       trace_si_end_compute(&sctx->trace, info->grid[0], info->grid[1], info->grid[2]);
1075 
1076    if (cs_regalloc_hang) {
1077       sctx->flags |= SI_CONTEXT_CS_PARTIAL_FLUSH;
1078       si_mark_atom_dirty(sctx, &sctx->atoms.s.cache_flush);
1079    }
1080 }
1081 
si_destroy_compute(struct si_compute * program)1082 void si_destroy_compute(struct si_compute *program)
1083 {
1084    struct si_shader_selector *sel = &program->sel;
1085 
1086    if (program->ir_type != PIPE_SHADER_IR_NATIVE) {
1087       util_queue_drop_job(&sel->screen->shader_compiler_queue, &sel->ready);
1088       util_queue_fence_destroy(&sel->ready);
1089    }
1090 
1091    for (unsigned i = 0; i < program->max_global_buffers; i++)
1092       pipe_resource_reference(&program->global_buffers[i], NULL);
1093    FREE(program->global_buffers);
1094 
1095    si_shader_destroy(&program->shader);
1096    ralloc_free(program->sel.nir);
1097    FREE(program);
1098 }
1099 
si_delete_compute_state(struct pipe_context * ctx,void * state)1100 static void si_delete_compute_state(struct pipe_context *ctx, void *state)
1101 {
1102    struct si_compute *program = (struct si_compute *)state;
1103    struct si_context *sctx = (struct si_context *)ctx;
1104 
1105    if (!state)
1106       return;
1107 
1108    if (program == sctx->cs_shader_state.program)
1109       sctx->cs_shader_state.program = NULL;
1110 
1111    if (program == sctx->cs_shader_state.emitted_program)
1112       sctx->cs_shader_state.emitted_program = NULL;
1113 
1114    si_compute_reference(&program, NULL);
1115 }
1116 
si_set_compute_resources(struct pipe_context * ctx_,unsigned start,unsigned count,struct pipe_surface ** surfaces)1117 static void si_set_compute_resources(struct pipe_context *ctx_, unsigned start, unsigned count,
1118                                      struct pipe_surface **surfaces)
1119 {
1120 }
1121 
si_init_compute_functions(struct si_context * sctx)1122 void si_init_compute_functions(struct si_context *sctx)
1123 {
1124    sctx->b.create_compute_state = si_create_compute_state;
1125    sctx->b.delete_compute_state = si_delete_compute_state;
1126    sctx->b.bind_compute_state = si_bind_compute_state;
1127    sctx->b.get_compute_state_info = si_get_compute_state_info;
1128    sctx->b.set_compute_resources = si_set_compute_resources;
1129    sctx->b.set_global_binding = si_set_global_binding;
1130    sctx->b.launch_grid = si_launch_grid;
1131 }
1132