2 * Copyright 2013 Advanced Micro Devices, Inc.
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:
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
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.
26 #include "tgsi/tgsi_parse.h"
27 #include "util/u_async_debug.h"
28 #include "util/u_memory.h"
29 #include "util/u_upload_mgr.h"
31 #include "amd_kernel_code_t.h"
32 #include "si_build_pm4.h"
33 #include "si_compute.h"
35 #define COMPUTE_DBG(rscreen, fmt, args...) \
37 if ((rscreen->debug_flags & DBG(COMPUTE))) fprintf(stderr, fmt, ##args); \
40 struct dispatch_packet
{
43 uint16_t workgroup_size_x
;
44 uint16_t workgroup_size_y
;
45 uint16_t workgroup_size_z
;
50 uint32_t private_segment_size
;
51 uint32_t group_segment_size
;
52 uint64_t kernel_object
;
53 uint64_t kernarg_address
;
57 static const amd_kernel_code_t
*si_compute_get_code_object(
58 const struct si_compute
*program
,
59 uint64_t symbol_offset
)
61 if (!program
->use_code_object_v2
) {
64 return (const amd_kernel_code_t
*)
65 (program
->shader
.binary
.code
+ symbol_offset
);
68 static void code_object_to_config(const amd_kernel_code_t
*code_object
,
69 struct si_shader_config
*out_config
) {
71 uint32_t rsrc1
= code_object
->compute_pgm_resource_registers
;
72 uint32_t rsrc2
= code_object
->compute_pgm_resource_registers
>> 32;
73 out_config
->num_sgprs
= code_object
->wavefront_sgpr_count
;
74 out_config
->num_vgprs
= code_object
->workitem_vgpr_count
;
75 out_config
->float_mode
= G_00B028_FLOAT_MODE(rsrc1
);
76 out_config
->rsrc1
= rsrc1
;
77 out_config
->lds_size
= MAX2(out_config
->lds_size
, G_00B84C_LDS_SIZE(rsrc2
));
78 out_config
->rsrc2
= rsrc2
;
79 out_config
->scratch_bytes_per_wave
=
80 align(code_object
->workitem_private_segment_byte_size
* 64, 1024);
83 /* Asynchronous compute shader compilation. */
84 static void si_create_compute_state_async(void *job
, int thread_index
)
86 struct si_compute
*program
= (struct si_compute
*)job
;
87 struct si_shader
*shader
= &program
->shader
;
88 struct si_shader_selector sel
;
89 struct si_compiler
*compiler
;
90 struct pipe_debug_callback
*debug
= &program
->compiler_ctx_state
.debug
;
91 struct si_screen
*sscreen
= program
->screen
;
93 assert(!debug
->debug_message
|| debug
->async
);
94 assert(thread_index
>= 0);
95 assert(thread_index
< ARRAY_SIZE(sscreen
->compiler
));
96 compiler
= &sscreen
->compiler
[thread_index
];
98 memset(&sel
, 0, sizeof(sel
));
100 sel
.screen
= sscreen
;
102 if (program
->ir_type
== PIPE_SHADER_IR_TGSI
) {
103 tgsi_scan_shader(program
->ir
.tgsi
, &sel
.info
);
104 sel
.tokens
= program
->ir
.tgsi
;
106 assert(program
->ir_type
== PIPE_SHADER_IR_NIR
);
107 sel
.nir
= program
->ir
.nir
;
109 si_nir_scan_shader(sel
.nir
, &sel
.info
);
113 /* Store the declared LDS size into tgsi_shader_info for the shader
114 * cache to include it.
116 sel
.info
.properties
[TGSI_PROPERTY_CS_LOCAL_SIZE
] = program
->local_size
;
118 sel
.type
= PIPE_SHADER_COMPUTE
;
119 si_get_active_slot_masks(&sel
.info
,
120 &program
->active_const_and_shader_buffers
,
121 &program
->active_samplers_and_images
);
123 program
->shader
.selector
= &sel
;
124 program
->shader
.is_monolithic
= true;
125 program
->uses_grid_size
= sel
.info
.uses_grid_size
;
126 program
->uses_block_size
= sel
.info
.uses_block_size
;
127 program
->uses_bindless_samplers
= sel
.info
.uses_bindless_samplers
;
128 program
->uses_bindless_images
= sel
.info
.uses_bindless_images
;
129 program
->variable_group_size
=
130 sel
.info
.properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
] == 0;
132 void *ir_binary
= si_get_ir_binary(&sel
);
134 /* Try to load the shader from the shader cache. */
135 mtx_lock(&sscreen
->shader_cache_mutex
);
138 si_shader_cache_load_shader(sscreen
, ir_binary
, shader
)) {
139 mtx_unlock(&sscreen
->shader_cache_mutex
);
141 si_shader_dump_stats_for_shader_db(shader
, debug
);
142 si_shader_dump(sscreen
, shader
, debug
, PIPE_SHADER_COMPUTE
,
145 if (si_shader_binary_upload(sscreen
, shader
))
146 program
->shader
.compilation_failed
= true;
148 mtx_unlock(&sscreen
->shader_cache_mutex
);
150 if (si_shader_create(sscreen
, compiler
, &program
->shader
, debug
)) {
151 program
->shader
.compilation_failed
= true;
153 if (program
->ir_type
== PIPE_SHADER_IR_TGSI
)
154 FREE(program
->ir
.tgsi
);
155 program
->shader
.selector
= NULL
;
159 bool scratch_enabled
= shader
->config
.scratch_bytes_per_wave
> 0;
160 unsigned user_sgprs
= SI_NUM_RESOURCE_SGPRS
+
161 (sel
.info
.uses_grid_size
? 3 : 0) +
162 (sel
.info
.uses_block_size
? 3 : 0);
164 shader
->config
.rsrc1
=
165 S_00B848_VGPRS((shader
->config
.num_vgprs
- 1) / 4) |
166 S_00B848_SGPRS((shader
->config
.num_sgprs
- 1) / 8) |
167 S_00B848_DX10_CLAMP(1) |
168 S_00B848_FLOAT_MODE(shader
->config
.float_mode
);
170 shader
->config
.rsrc2
=
171 S_00B84C_USER_SGPR(user_sgprs
) |
172 S_00B84C_SCRATCH_EN(scratch_enabled
) |
173 S_00B84C_TGID_X_EN(sel
.info
.uses_block_id
[0]) |
174 S_00B84C_TGID_Y_EN(sel
.info
.uses_block_id
[1]) |
175 S_00B84C_TGID_Z_EN(sel
.info
.uses_block_id
[2]) |
176 S_00B84C_TIDIG_COMP_CNT(sel
.info
.uses_thread_id
[2] ? 2 :
177 sel
.info
.uses_thread_id
[1] ? 1 : 0) |
178 S_00B84C_LDS_SIZE(shader
->config
.lds_size
);
181 mtx_lock(&sscreen
->shader_cache_mutex
);
182 if (!si_shader_cache_insert_shader(sscreen
, ir_binary
, shader
, true))
184 mtx_unlock(&sscreen
->shader_cache_mutex
);
188 if (program
->ir_type
== PIPE_SHADER_IR_TGSI
)
189 FREE(program
->ir
.tgsi
);
191 program
->shader
.selector
= NULL
;
194 static void *si_create_compute_state(
195 struct pipe_context
*ctx
,
196 const struct pipe_compute_state
*cso
)
198 struct si_context
*sctx
= (struct si_context
*)ctx
;
199 struct si_screen
*sscreen
= (struct si_screen
*)ctx
->screen
;
200 struct si_compute
*program
= CALLOC_STRUCT(si_compute
);
202 pipe_reference_init(&program
->reference
, 1);
203 program
->screen
= (struct si_screen
*)ctx
->screen
;
204 program
->ir_type
= cso
->ir_type
;
205 program
->local_size
= cso
->req_local_mem
;
206 program
->private_size
= cso
->req_private_mem
;
207 program
->input_size
= cso
->req_input_mem
;
208 program
->use_code_object_v2
= cso
->ir_type
== PIPE_SHADER_IR_NATIVE
;
210 if (cso
->ir_type
!= PIPE_SHADER_IR_NATIVE
) {
211 if (cso
->ir_type
== PIPE_SHADER_IR_TGSI
) {
212 program
->ir
.tgsi
= tgsi_dup_tokens(cso
->prog
);
213 if (!program
->ir
.tgsi
) {
218 assert(cso
->ir_type
== PIPE_SHADER_IR_NIR
);
219 program
->ir
.nir
= (struct nir_shader
*) cso
->prog
;
222 program
->compiler_ctx_state
.debug
= sctx
->debug
;
223 program
->compiler_ctx_state
.is_debug_context
= sctx
->is_debug
;
224 p_atomic_inc(&sscreen
->num_shaders_created
);
226 si_schedule_initial_compile(sctx
, PIPE_SHADER_COMPUTE
,
228 &program
->compiler_ctx_state
,
229 program
, si_create_compute_state_async
);
231 const struct pipe_llvm_program_header
*header
;
234 code
= cso
->prog
+ sizeof(struct pipe_llvm_program_header
);
236 ac_elf_read(code
, header
->num_bytes
, &program
->shader
.binary
);
237 if (program
->use_code_object_v2
) {
238 const amd_kernel_code_t
*code_object
=
239 si_compute_get_code_object(program
, 0);
240 code_object_to_config(code_object
, &program
->shader
.config
);
242 si_shader_binary_read_config(&program
->shader
.binary
,
243 &program
->shader
.config
, 0);
245 si_shader_dump(sctx
->screen
, &program
->shader
, &sctx
->debug
,
246 PIPE_SHADER_COMPUTE
, stderr
, true);
247 if (si_shader_binary_upload(sctx
->screen
, &program
->shader
) < 0) {
248 fprintf(stderr
, "LLVM failed to upload shader\n");
257 static void si_bind_compute_state(struct pipe_context
*ctx
, void *state
)
259 struct si_context
*sctx
= (struct si_context
*)ctx
;
260 struct si_compute
*program
= (struct si_compute
*)state
;
262 sctx
->cs_shader_state
.program
= program
;
266 /* Wait because we need active slot usage masks. */
267 if (program
->ir_type
!= PIPE_SHADER_IR_NATIVE
)
268 util_queue_fence_wait(&program
->ready
);
270 si_set_active_descriptors(sctx
,
271 SI_DESCS_FIRST_COMPUTE
+
272 SI_SHADER_DESCS_CONST_AND_SHADER_BUFFERS
,
273 program
->active_const_and_shader_buffers
);
274 si_set_active_descriptors(sctx
,
275 SI_DESCS_FIRST_COMPUTE
+
276 SI_SHADER_DESCS_SAMPLERS_AND_IMAGES
,
277 program
->active_samplers_and_images
);
280 static void si_set_global_binding(
281 struct pipe_context
*ctx
, unsigned first
, unsigned n
,
282 struct pipe_resource
**resources
,
286 struct si_context
*sctx
= (struct si_context
*)ctx
;
287 struct si_compute
*program
= sctx
->cs_shader_state
.program
;
289 assert(first
+ n
<= MAX_GLOBAL_BUFFERS
);
292 for (i
= 0; i
< n
; i
++) {
293 pipe_resource_reference(&program
->global_buffers
[first
+ i
], NULL
);
298 for (i
= 0; i
< n
; i
++) {
301 pipe_resource_reference(&program
->global_buffers
[first
+ i
], resources
[i
]);
302 va
= r600_resource(resources
[i
])->gpu_address
;
303 offset
= util_le32_to_cpu(*handles
[i
]);
305 va
= util_cpu_to_le64(va
);
306 memcpy(handles
[i
], &va
, sizeof(va
));
310 static void si_initialize_compute(struct si_context
*sctx
)
312 struct radeon_cmdbuf
*cs
= sctx
->gfx_cs
;
315 radeon_set_sh_reg_seq(cs
, R_00B858_COMPUTE_STATIC_THREAD_MGMT_SE0
, 2);
316 /* R_00B858_COMPUTE_STATIC_THREAD_MGMT_SE0 / SE1 */
317 radeon_emit(cs
, S_00B858_SH0_CU_EN(0xffff) | S_00B858_SH1_CU_EN(0xffff));
318 radeon_emit(cs
, S_00B85C_SH0_CU_EN(0xffff) | S_00B85C_SH1_CU_EN(0xffff));
320 if (sctx
->chip_class
>= CIK
) {
321 /* Also set R_00B858_COMPUTE_STATIC_THREAD_MGMT_SE2 / SE3 */
322 radeon_set_sh_reg_seq(cs
,
323 R_00B864_COMPUTE_STATIC_THREAD_MGMT_SE2
, 2);
324 radeon_emit(cs
, S_00B864_SH0_CU_EN(0xffff) |
325 S_00B864_SH1_CU_EN(0xffff));
326 radeon_emit(cs
, S_00B868_SH0_CU_EN(0xffff) |
327 S_00B868_SH1_CU_EN(0xffff));
330 /* This register has been moved to R_00CD20_COMPUTE_MAX_WAVE_ID
331 * and is now per pipe, so it should be handled in the
332 * kernel if we want to use something other than the default value,
333 * which is now 0x22f.
335 if (sctx
->chip_class
<= SI
) {
336 /* XXX: This should be:
337 * (number of compute units) * 4 * (waves per simd) - 1 */
339 radeon_set_sh_reg(cs
, R_00B82C_COMPUTE_MAX_WAVE_ID
,
340 0x190 /* Default value */);
343 /* Set the pointer to border colors. */
344 bc_va
= sctx
->border_color_buffer
->gpu_address
;
346 if (sctx
->chip_class
>= CIK
) {
347 radeon_set_uconfig_reg_seq(cs
, R_030E00_TA_CS_BC_BASE_ADDR
, 2);
348 radeon_emit(cs
, bc_va
>> 8); /* R_030E00_TA_CS_BC_BASE_ADDR */
349 radeon_emit(cs
, S_030E04_ADDRESS(bc_va
>> 40)); /* R_030E04_TA_CS_BC_BASE_ADDR_HI */
351 if (sctx
->screen
->info
.si_TA_CS_BC_BASE_ADDR_allowed
) {
352 radeon_set_config_reg(cs
, R_00950C_TA_CS_BC_BASE_ADDR
,
357 sctx
->cs_shader_state
.emitted_program
= NULL
;
358 sctx
->cs_shader_state
.initialized
= true;
361 static bool si_setup_compute_scratch_buffer(struct si_context
*sctx
,
362 struct si_shader
*shader
,
363 struct si_shader_config
*config
)
365 uint64_t scratch_bo_size
, scratch_needed
;
367 scratch_needed
= config
->scratch_bytes_per_wave
* sctx
->scratch_waves
;
368 if (sctx
->compute_scratch_buffer
)
369 scratch_bo_size
= sctx
->compute_scratch_buffer
->b
.b
.width0
;
371 if (scratch_bo_size
< scratch_needed
) {
372 r600_resource_reference(&sctx
->compute_scratch_buffer
, NULL
);
374 sctx
->compute_scratch_buffer
=
375 si_aligned_buffer_create(&sctx
->screen
->b
,
376 SI_RESOURCE_FLAG_UNMAPPABLE
,
378 scratch_needed
, 256);
380 if (!sctx
->compute_scratch_buffer
)
384 if (sctx
->compute_scratch_buffer
!= shader
->scratch_bo
&& scratch_needed
) {
385 uint64_t scratch_va
= sctx
->compute_scratch_buffer
->gpu_address
;
387 si_shader_apply_scratch_relocs(shader
, scratch_va
);
389 if (si_shader_binary_upload(sctx
->screen
, shader
))
392 r600_resource_reference(&shader
->scratch_bo
,
393 sctx
->compute_scratch_buffer
);
399 static bool si_switch_compute_shader(struct si_context
*sctx
,
400 struct si_compute
*program
,
401 struct si_shader
*shader
,
402 const amd_kernel_code_t
*code_object
,
405 struct radeon_cmdbuf
*cs
= sctx
->gfx_cs
;
406 struct si_shader_config inline_config
= {0};
407 struct si_shader_config
*config
;
410 if (sctx
->cs_shader_state
.emitted_program
== program
&&
411 sctx
->cs_shader_state
.offset
== offset
)
414 if (program
->ir_type
!= PIPE_SHADER_IR_NATIVE
) {
415 config
= &shader
->config
;
419 config
= &inline_config
;
421 code_object_to_config(code_object
, config
);
423 si_shader_binary_read_config(&shader
->binary
, config
, offset
);
426 lds_blocks
= config
->lds_size
;
427 /* XXX: We are over allocating LDS. For SI, the shader reports
428 * LDS in blocks of 256 bytes, so if there are 4 bytes lds
429 * allocated in the shader and 4 bytes allocated by the state
430 * tracker, then we will set LDS_SIZE to 512 bytes rather than 256.
432 if (sctx
->chip_class
<= SI
) {
433 lds_blocks
+= align(program
->local_size
, 256) >> 8;
435 lds_blocks
+= align(program
->local_size
, 512) >> 9;
438 /* TODO: use si_multiwave_lds_size_workaround */
439 assert(lds_blocks
<= 0xFF);
441 config
->rsrc2
&= C_00B84C_LDS_SIZE
;
442 config
->rsrc2
|= S_00B84C_LDS_SIZE(lds_blocks
);
445 if (!si_setup_compute_scratch_buffer(sctx
, shader
, config
))
448 if (shader
->scratch_bo
) {
449 COMPUTE_DBG(sctx
->screen
, "Waves: %u; Scratch per wave: %u bytes; "
450 "Total Scratch: %u bytes\n", sctx
->scratch_waves
,
451 config
->scratch_bytes_per_wave
,
452 config
->scratch_bytes_per_wave
*
453 sctx
->scratch_waves
);
455 radeon_add_to_buffer_list(sctx
, sctx
->gfx_cs
,
456 shader
->scratch_bo
, RADEON_USAGE_READWRITE
,
457 RADEON_PRIO_SCRATCH_BUFFER
);
460 /* Prefetch the compute shader to TC L2.
462 * We should also prefetch graphics shaders if a compute dispatch was
463 * the last command, and the compute shader if a draw call was the last
464 * command. However, that would add more complexity and we're likely
465 * to get a shader state change in that case anyway.
467 if (sctx
->chip_class
>= CIK
) {
468 cik_prefetch_TC_L2_async(sctx
, &program
->shader
.bo
->b
.b
,
469 0, program
->shader
.bo
->b
.b
.width0
);
472 shader_va
= shader
->bo
->gpu_address
+ offset
;
473 if (program
->use_code_object_v2
) {
474 /* Shader code is placed after the amd_kernel_code_t
476 shader_va
+= sizeof(amd_kernel_code_t
);
479 radeon_add_to_buffer_list(sctx
, sctx
->gfx_cs
, shader
->bo
,
480 RADEON_USAGE_READ
, RADEON_PRIO_SHADER_BINARY
);
482 radeon_set_sh_reg_seq(cs
, R_00B830_COMPUTE_PGM_LO
, 2);
483 radeon_emit(cs
, shader_va
>> 8);
484 radeon_emit(cs
, S_00B834_DATA(shader_va
>> 40));
486 radeon_set_sh_reg_seq(cs
, R_00B848_COMPUTE_PGM_RSRC1
, 2);
487 radeon_emit(cs
, config
->rsrc1
);
488 radeon_emit(cs
, config
->rsrc2
);
490 COMPUTE_DBG(sctx
->screen
, "COMPUTE_PGM_RSRC1: 0x%08x "
491 "COMPUTE_PGM_RSRC2: 0x%08x\n", config
->rsrc1
, config
->rsrc2
);
493 radeon_set_sh_reg(cs
, R_00B860_COMPUTE_TMPRING_SIZE
,
494 S_00B860_WAVES(sctx
->scratch_waves
)
495 | S_00B860_WAVESIZE(config
->scratch_bytes_per_wave
>> 10));
497 sctx
->cs_shader_state
.emitted_program
= program
;
498 sctx
->cs_shader_state
.offset
= offset
;
499 sctx
->cs_shader_state
.uses_scratch
=
500 config
->scratch_bytes_per_wave
!= 0;
505 static void setup_scratch_rsrc_user_sgprs(struct si_context
*sctx
,
506 const amd_kernel_code_t
*code_object
,
509 struct radeon_cmdbuf
*cs
= sctx
->gfx_cs
;
510 uint64_t scratch_va
= sctx
->compute_scratch_buffer
->gpu_address
;
512 unsigned max_private_element_size
= AMD_HSA_BITS_GET(
513 code_object
->code_properties
,
514 AMD_CODE_PROPERTY_PRIVATE_ELEMENT_SIZE
);
516 uint32_t scratch_dword0
= scratch_va
& 0xffffffff;
517 uint32_t scratch_dword1
=
518 S_008F04_BASE_ADDRESS_HI(scratch_va
>> 32) |
519 S_008F04_SWIZZLE_ENABLE(1);
521 /* Disable address clamping */
522 uint32_t scratch_dword2
= 0xffffffff;
523 uint32_t scratch_dword3
=
524 S_008F0C_INDEX_STRIDE(3) |
525 S_008F0C_ADD_TID_ENABLE(1);
527 if (sctx
->chip_class
>= GFX9
) {
528 assert(max_private_element_size
== 1); /* always 4 bytes on GFX9 */
530 scratch_dword3
|= S_008F0C_ELEMENT_SIZE(max_private_element_size
);
532 if (sctx
->chip_class
< VI
) {
533 /* BUF_DATA_FORMAT is ignored, but it cannot be
534 * BUF_DATA_FORMAT_INVALID. */
536 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_8
);
540 radeon_set_sh_reg_seq(cs
, R_00B900_COMPUTE_USER_DATA_0
+
542 radeon_emit(cs
, scratch_dword0
);
543 radeon_emit(cs
, scratch_dword1
);
544 radeon_emit(cs
, scratch_dword2
);
545 radeon_emit(cs
, scratch_dword3
);
548 static void si_setup_user_sgprs_co_v2(struct si_context
*sctx
,
549 const amd_kernel_code_t
*code_object
,
550 const struct pipe_grid_info
*info
,
551 uint64_t kernel_args_va
)
553 struct si_compute
*program
= sctx
->cs_shader_state
.program
;
554 struct radeon_cmdbuf
*cs
= sctx
->gfx_cs
;
556 static const enum amd_code_property_mask_t workgroup_count_masks
[] = {
557 AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_X
,
558 AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Y
,
559 AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Z
562 unsigned i
, user_sgpr
= 0;
563 if (AMD_HSA_BITS_GET(code_object
->code_properties
,
564 AMD_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER
)) {
565 if (code_object
->workitem_private_segment_byte_size
> 0) {
566 setup_scratch_rsrc_user_sgprs(sctx
, code_object
,
572 if (AMD_HSA_BITS_GET(code_object
->code_properties
,
573 AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR
)) {
574 struct dispatch_packet dispatch
;
575 unsigned dispatch_offset
;
576 struct r600_resource
*dispatch_buf
= NULL
;
577 uint64_t dispatch_va
;
579 /* Upload dispatch ptr */
580 memset(&dispatch
, 0, sizeof(dispatch
));
582 dispatch
.workgroup_size_x
= util_cpu_to_le16(info
->block
[0]);
583 dispatch
.workgroup_size_y
= util_cpu_to_le16(info
->block
[1]);
584 dispatch
.workgroup_size_z
= util_cpu_to_le16(info
->block
[2]);
586 dispatch
.grid_size_x
= util_cpu_to_le32(info
->grid
[0] * info
->block
[0]);
587 dispatch
.grid_size_y
= util_cpu_to_le32(info
->grid
[1] * info
->block
[1]);
588 dispatch
.grid_size_z
= util_cpu_to_le32(info
->grid
[2] * info
->block
[2]);
590 dispatch
.private_segment_size
= util_cpu_to_le32(program
->private_size
);
591 dispatch
.group_segment_size
= util_cpu_to_le32(program
->local_size
);
593 dispatch
.kernarg_address
= util_cpu_to_le64(kernel_args_va
);
595 u_upload_data(sctx
->b
.const_uploader
, 0, sizeof(dispatch
),
596 256, &dispatch
, &dispatch_offset
,
597 (struct pipe_resource
**)&dispatch_buf
);
600 fprintf(stderr
, "Error: Failed to allocate dispatch "
603 radeon_add_to_buffer_list(sctx
, sctx
->gfx_cs
, dispatch_buf
,
604 RADEON_USAGE_READ
, RADEON_PRIO_CONST_BUFFER
);
606 dispatch_va
= dispatch_buf
->gpu_address
+ dispatch_offset
;
608 radeon_set_sh_reg_seq(cs
, R_00B900_COMPUTE_USER_DATA_0
+
610 radeon_emit(cs
, dispatch_va
);
611 radeon_emit(cs
, S_008F04_BASE_ADDRESS_HI(dispatch_va
>> 32) |
614 r600_resource_reference(&dispatch_buf
, NULL
);
618 if (AMD_HSA_BITS_GET(code_object
->code_properties
,
619 AMD_CODE_PROPERTY_ENABLE_SGPR_KERNARG_SEGMENT_PTR
)) {
620 radeon_set_sh_reg_seq(cs
, R_00B900_COMPUTE_USER_DATA_0
+
622 radeon_emit(cs
, kernel_args_va
);
623 radeon_emit(cs
, S_008F04_BASE_ADDRESS_HI (kernel_args_va
>> 32) |
628 for (i
= 0; i
< 3 && user_sgpr
< 16; i
++) {
629 if (code_object
->code_properties
& workgroup_count_masks
[i
]) {
630 radeon_set_sh_reg_seq(cs
,
631 R_00B900_COMPUTE_USER_DATA_0
+
633 radeon_emit(cs
, info
->grid
[i
]);
639 static bool si_upload_compute_input(struct si_context
*sctx
,
640 const amd_kernel_code_t
*code_object
,
641 const struct pipe_grid_info
*info
)
643 struct radeon_cmdbuf
*cs
= sctx
->gfx_cs
;
644 struct si_compute
*program
= sctx
->cs_shader_state
.program
;
645 struct r600_resource
*input_buffer
= NULL
;
646 unsigned kernel_args_size
;
647 unsigned num_work_size_bytes
= program
->use_code_object_v2
? 0 : 36;
648 uint32_t kernel_args_offset
= 0;
649 uint32_t *kernel_args
;
650 void *kernel_args_ptr
;
651 uint64_t kernel_args_va
;
654 /* The extra num_work_size_bytes are for work group / work item size information */
655 kernel_args_size
= program
->input_size
+ num_work_size_bytes
;
657 u_upload_alloc(sctx
->b
.const_uploader
, 0, kernel_args_size
,
658 sctx
->screen
->info
.tcc_cache_line_size
,
660 (struct pipe_resource
**)&input_buffer
, &kernel_args_ptr
);
662 if (unlikely(!kernel_args_ptr
))
665 kernel_args
= (uint32_t*)kernel_args_ptr
;
666 kernel_args_va
= input_buffer
->gpu_address
+ kernel_args_offset
;
669 for (i
= 0; i
< 3; i
++) {
670 kernel_args
[i
] = util_cpu_to_le32(info
->grid
[i
]);
671 kernel_args
[i
+ 3] = util_cpu_to_le32(info
->grid
[i
] * info
->block
[i
]);
672 kernel_args
[i
+ 6] = util_cpu_to_le32(info
->block
[i
]);
676 memcpy(kernel_args
+ (num_work_size_bytes
/ 4), info
->input
,
677 program
->input_size
);
680 for (i
= 0; i
< (kernel_args_size
/ 4); i
++) {
681 COMPUTE_DBG(sctx
->screen
, "input %u : %u\n", i
,
686 radeon_add_to_buffer_list(sctx
, sctx
->gfx_cs
, input_buffer
,
687 RADEON_USAGE_READ
, RADEON_PRIO_CONST_BUFFER
);
690 si_setup_user_sgprs_co_v2(sctx
, code_object
, info
, kernel_args_va
);
692 radeon_set_sh_reg_seq(cs
, R_00B900_COMPUTE_USER_DATA_0
, 2);
693 radeon_emit(cs
, kernel_args_va
);
694 radeon_emit(cs
, S_008F04_BASE_ADDRESS_HI (kernel_args_va
>> 32) |
698 r600_resource_reference(&input_buffer
, NULL
);
703 static void si_setup_tgsi_grid(struct si_context
*sctx
,
704 const struct pipe_grid_info
*info
)
706 struct si_compute
*program
= sctx
->cs_shader_state
.program
;
707 struct radeon_cmdbuf
*cs
= sctx
->gfx_cs
;
708 unsigned grid_size_reg
= R_00B900_COMPUTE_USER_DATA_0
+
709 4 * SI_NUM_RESOURCE_SGPRS
;
710 unsigned block_size_reg
= grid_size_reg
+
711 /* 12 bytes = 3 dwords. */
712 12 * program
->uses_grid_size
;
714 if (info
->indirect
) {
715 if (program
->uses_grid_size
) {
716 uint64_t base_va
= r600_resource(info
->indirect
)->gpu_address
;
717 uint64_t va
= base_va
+ info
->indirect_offset
;
720 radeon_add_to_buffer_list(sctx
, sctx
->gfx_cs
,
721 r600_resource(info
->indirect
),
722 RADEON_USAGE_READ
, RADEON_PRIO_DRAW_INDIRECT
);
724 for (i
= 0; i
< 3; ++i
) {
725 radeon_emit(cs
, PKT3(PKT3_COPY_DATA
, 4, 0));
726 radeon_emit(cs
, COPY_DATA_SRC_SEL(COPY_DATA_MEM
) |
727 COPY_DATA_DST_SEL(COPY_DATA_REG
));
728 radeon_emit(cs
, (va
+ 4 * i
));
729 radeon_emit(cs
, (va
+ 4 * i
) >> 32);
730 radeon_emit(cs
, (grid_size_reg
>> 2) + i
);
735 if (program
->uses_grid_size
) {
736 radeon_set_sh_reg_seq(cs
, grid_size_reg
, 3);
737 radeon_emit(cs
, info
->grid
[0]);
738 radeon_emit(cs
, info
->grid
[1]);
739 radeon_emit(cs
, info
->grid
[2]);
741 if (program
->variable_group_size
&& program
->uses_block_size
) {
742 radeon_set_sh_reg_seq(cs
, block_size_reg
, 3);
743 radeon_emit(cs
, info
->block
[0]);
744 radeon_emit(cs
, info
->block
[1]);
745 radeon_emit(cs
, info
->block
[2]);
750 static void si_emit_dispatch_packets(struct si_context
*sctx
,
751 const struct pipe_grid_info
*info
)
753 struct si_screen
*sscreen
= sctx
->screen
;
754 struct radeon_cmdbuf
*cs
= sctx
->gfx_cs
;
755 bool render_cond_bit
= sctx
->render_cond
&& !sctx
->render_cond_force_off
;
756 unsigned waves_per_threadgroup
=
757 DIV_ROUND_UP(info
->block
[0] * info
->block
[1] * info
->block
[2], 64);
758 unsigned compute_resource_limits
=
759 S_00B854_SIMD_DEST_CNTL(waves_per_threadgroup
% 4 == 0);
761 if (sctx
->chip_class
>= CIK
) {
762 unsigned num_cu_per_se
= sscreen
->info
.num_good_compute_units
/
763 sscreen
->info
.max_se
;
765 /* Force even distribution on all SIMDs in CU if the workgroup
766 * size is 64. This has shown some good improvements if # of CUs
767 * per SE is not a multiple of 4.
769 if (num_cu_per_se
% 4 && waves_per_threadgroup
== 1)
770 compute_resource_limits
|= S_00B854_FORCE_SIMD_DIST(1);
773 radeon_set_sh_reg(cs
, R_00B854_COMPUTE_RESOURCE_LIMITS
,
774 compute_resource_limits
);
776 radeon_set_sh_reg_seq(cs
, R_00B81C_COMPUTE_NUM_THREAD_X
, 3);
777 radeon_emit(cs
, S_00B81C_NUM_THREAD_FULL(info
->block
[0]));
778 radeon_emit(cs
, S_00B820_NUM_THREAD_FULL(info
->block
[1]));
779 radeon_emit(cs
, S_00B824_NUM_THREAD_FULL(info
->block
[2]));
781 unsigned dispatch_initiator
=
782 S_00B800_COMPUTE_SHADER_EN(1) |
783 S_00B800_FORCE_START_AT_000(1) |
784 /* If the KMD allows it (there is a KMD hw register for it),
785 * allow launching waves out-of-order. (same as Vulkan) */
786 S_00B800_ORDER_MODE(sctx
->chip_class
>= CIK
);
788 if (info
->indirect
) {
789 uint64_t base_va
= r600_resource(info
->indirect
)->gpu_address
;
791 radeon_add_to_buffer_list(sctx
, sctx
->gfx_cs
,
792 r600_resource(info
->indirect
),
793 RADEON_USAGE_READ
, RADEON_PRIO_DRAW_INDIRECT
);
795 radeon_emit(cs
, PKT3(PKT3_SET_BASE
, 2, 0) |
796 PKT3_SHADER_TYPE_S(1));
798 radeon_emit(cs
, base_va
);
799 radeon_emit(cs
, base_va
>> 32);
801 radeon_emit(cs
, PKT3(PKT3_DISPATCH_INDIRECT
, 1, render_cond_bit
) |
802 PKT3_SHADER_TYPE_S(1));
803 radeon_emit(cs
, info
->indirect_offset
);
804 radeon_emit(cs
, dispatch_initiator
);
806 radeon_emit(cs
, PKT3(PKT3_DISPATCH_DIRECT
, 3, render_cond_bit
) |
807 PKT3_SHADER_TYPE_S(1));
808 radeon_emit(cs
, info
->grid
[0]);
809 radeon_emit(cs
, info
->grid
[1]);
810 radeon_emit(cs
, info
->grid
[2]);
811 radeon_emit(cs
, dispatch_initiator
);
816 static void si_launch_grid(
817 struct pipe_context
*ctx
, const struct pipe_grid_info
*info
)
819 struct si_context
*sctx
= (struct si_context
*)ctx
;
820 struct si_compute
*program
= sctx
->cs_shader_state
.program
;
821 const amd_kernel_code_t
*code_object
=
822 si_compute_get_code_object(program
, info
->pc
);
824 /* HW bug workaround when CS threadgroups > 256 threads and async
825 * compute isn't used, i.e. only one compute job can run at a time.
826 * If async compute is possible, the threadgroup size must be limited
827 * to 256 threads on all queues to avoid the bug.
828 * Only SI and certain CIK chips are affected.
830 bool cs_regalloc_hang
=
831 (sctx
->chip_class
== SI
||
832 sctx
->family
== CHIP_BONAIRE
||
833 sctx
->family
== CHIP_KABINI
) &&
834 info
->block
[0] * info
->block
[1] * info
->block
[2] > 256;
836 if (cs_regalloc_hang
)
837 sctx
->flags
|= SI_CONTEXT_PS_PARTIAL_FLUSH
|
838 SI_CONTEXT_CS_PARTIAL_FLUSH
;
840 if (program
->ir_type
!= PIPE_SHADER_IR_NATIVE
&&
841 program
->shader
.compilation_failed
)
844 if (sctx
->last_num_draw_calls
!= sctx
->num_draw_calls
) {
845 si_update_fb_dirtiness_after_rendering(sctx
);
846 sctx
->last_num_draw_calls
= sctx
->num_draw_calls
;
849 si_decompress_textures(sctx
, 1 << PIPE_SHADER_COMPUTE
);
851 /* Add buffer sizes for memory checking in need_cs_space. */
852 si_context_add_resource_size(sctx
, &program
->shader
.bo
->b
.b
);
853 /* TODO: add the scratch buffer */
855 if (info
->indirect
) {
856 si_context_add_resource_size(sctx
, info
->indirect
);
858 /* Indirect buffers use TC L2 on GFX9, but not older hw. */
859 if (sctx
->chip_class
<= VI
&&
860 r600_resource(info
->indirect
)->TC_L2_dirty
) {
861 sctx
->flags
|= SI_CONTEXT_WRITEBACK_GLOBAL_L2
;
862 r600_resource(info
->indirect
)->TC_L2_dirty
= false;
866 si_need_gfx_cs_space(sctx
);
868 if (!sctx
->cs_shader_state
.initialized
)
869 si_initialize_compute(sctx
);
872 si_emit_cache_flush(sctx
);
874 if (!si_switch_compute_shader(sctx
, program
, &program
->shader
,
875 code_object
, info
->pc
))
878 si_upload_compute_shader_descriptors(sctx
);
879 si_emit_compute_shader_pointers(sctx
);
881 if (si_is_atom_dirty(sctx
, &sctx
->atoms
.s
.render_cond
)) {
882 sctx
->atoms
.s
.render_cond
.emit(sctx
);
883 si_set_atom_dirty(sctx
, &sctx
->atoms
.s
.render_cond
, false);
886 if ((program
->input_size
||
887 program
->ir_type
== PIPE_SHADER_IR_NATIVE
) &&
888 unlikely(!si_upload_compute_input(sctx
, code_object
, info
))) {
893 for (i
= 0; i
< MAX_GLOBAL_BUFFERS
; i
++) {
894 struct r600_resource
*buffer
=
895 r600_resource(program
->global_buffers
[i
]);
899 radeon_add_to_buffer_list(sctx
, sctx
->gfx_cs
, buffer
,
900 RADEON_USAGE_READWRITE
,
901 RADEON_PRIO_COMPUTE_GLOBAL
);
904 if (program
->ir_type
!= PIPE_SHADER_IR_NATIVE
)
905 si_setup_tgsi_grid(sctx
, info
);
907 si_emit_dispatch_packets(sctx
, info
);
909 if (unlikely(sctx
->current_saved_cs
)) {
911 si_log_compute_state(sctx
, sctx
->log
);
914 sctx
->compute_is_busy
= true;
915 sctx
->num_compute_calls
++;
916 if (sctx
->cs_shader_state
.uses_scratch
)
917 sctx
->num_spill_compute_calls
++;
919 if (cs_regalloc_hang
)
920 sctx
->flags
|= SI_CONTEXT_CS_PARTIAL_FLUSH
;
923 void si_destroy_compute(struct si_compute
*program
)
925 if (program
->ir_type
!= PIPE_SHADER_IR_NATIVE
) {
926 util_queue_drop_job(&program
->screen
->shader_compiler_queue
,
928 util_queue_fence_destroy(&program
->ready
);
931 si_shader_destroy(&program
->shader
);
935 static void si_delete_compute_state(struct pipe_context
*ctx
, void* state
){
936 struct si_compute
*program
= (struct si_compute
*)state
;
937 struct si_context
*sctx
= (struct si_context
*)ctx
;
942 if (program
== sctx
->cs_shader_state
.program
)
943 sctx
->cs_shader_state
.program
= NULL
;
945 if (program
== sctx
->cs_shader_state
.emitted_program
)
946 sctx
->cs_shader_state
.emitted_program
= NULL
;
948 si_compute_reference(&program
, NULL
);
951 static void si_set_compute_resources(struct pipe_context
* ctx_
,
952 unsigned start
, unsigned count
,
953 struct pipe_surface
** surfaces
) { }
955 void si_init_compute_functions(struct si_context
*sctx
)
957 sctx
->b
.create_compute_state
= si_create_compute_state
;
958 sctx
->b
.delete_compute_state
= si_delete_compute_state
;
959 sctx
->b
.bind_compute_state
= si_bind_compute_state
;
960 /* ctx->context.create_sampler_view = evergreen_compute_create_sampler_view; */
961 sctx
->b
.set_compute_resources
= si_set_compute_resources
;
962 sctx
->b
.set_global_binding
= si_set_global_binding
;
963 sctx
->b
.launch_grid
= si_launch_grid
;