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