2 * Copyright 2013 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #include "tgsi/tgsi_parse.h"
26 #include "util/u_memory.h"
27 #include "util/u_upload_mgr.h"
29 #include "amd_kernel_code_t.h"
30 #include "radeon/r600_cs.h"
32 #include "si_compute.h"
35 struct dispatch_packet
{
38 uint16_t workgroup_size_x
;
39 uint16_t workgroup_size_y
;
40 uint16_t workgroup_size_z
;
45 uint32_t private_segment_size
;
46 uint32_t group_segment_size
;
47 uint64_t kernel_object
;
48 uint64_t kernarg_address
;
52 static const amd_kernel_code_t
*si_compute_get_code_object(
53 const struct si_compute
*program
,
54 uint64_t symbol_offset
)
56 if (!program
->use_code_object_v2
) {
59 return (const amd_kernel_code_t
*)
60 (program
->shader
.binary
.code
+ symbol_offset
);
63 static void code_object_to_config(const amd_kernel_code_t
*code_object
,
64 struct si_shader_config
*out_config
) {
66 uint32_t rsrc1
= code_object
->compute_pgm_resource_registers
;
67 uint32_t rsrc2
= code_object
->compute_pgm_resource_registers
>> 32;
68 out_config
->num_sgprs
= code_object
->wavefront_sgpr_count
;
69 out_config
->num_vgprs
= code_object
->workitem_vgpr_count
;
70 out_config
->float_mode
= G_00B028_FLOAT_MODE(rsrc1
);
71 out_config
->rsrc1
= rsrc1
;
72 out_config
->lds_size
= MAX2(out_config
->lds_size
, G_00B84C_LDS_SIZE(rsrc2
));
73 out_config
->rsrc2
= rsrc2
;
74 out_config
->scratch_bytes_per_wave
=
75 align(code_object
->workitem_private_segment_byte_size
* 64, 1024);
78 /* Asynchronous compute shader compilation. */
79 static void si_create_compute_state_async(void *job
, int thread_index
)
81 struct si_compute
*program
= (struct si_compute
*)job
;
82 struct si_shader
*shader
= &program
->shader
;
83 struct si_shader_selector sel
;
84 LLVMTargetMachineRef tm
;
85 struct pipe_debug_callback
*debug
= &program
->compiler_ctx_state
.debug
;
87 if (thread_index
>= 0) {
88 assert(thread_index
< ARRAY_SIZE(program
->screen
->tm
));
89 tm
= program
->screen
->tm
[thread_index
];
93 tm
= program
->compiler_ctx_state
.tm
;
96 memset(&sel
, 0, sizeof(sel
));
98 sel
.screen
= program
->screen
;
99 tgsi_scan_shader(program
->tokens
, &sel
.info
);
100 sel
.tokens
= program
->tokens
;
101 sel
.type
= PIPE_SHADER_COMPUTE
;
102 sel
.local_size
= program
->local_size
;
103 si_get_active_slot_masks(&sel
.info
,
104 &program
->active_const_and_shader_buffers
,
105 &program
->active_samplers_and_images
);
107 program
->shader
.selector
= &sel
;
108 program
->shader
.is_monolithic
= true;
109 program
->uses_grid_size
= sel
.info
.uses_grid_size
;
110 program
->uses_block_size
= sel
.info
.uses_block_size
;
111 program
->uses_bindless_samplers
= sel
.info
.uses_bindless_samplers
;
112 program
->uses_bindless_images
= sel
.info
.uses_bindless_images
;
114 if (si_shader_create(program
->screen
, tm
, &program
->shader
, debug
)) {
115 program
->shader
.compilation_failed
= true;
117 bool scratch_enabled
= shader
->config
.scratch_bytes_per_wave
> 0;
118 unsigned user_sgprs
= SI_NUM_RESOURCE_SGPRS
+
119 (sel
.info
.uses_grid_size
? 3 : 0) +
120 (sel
.info
.uses_block_size
? 3 : 0);
122 shader
->config
.rsrc1
=
123 S_00B848_VGPRS((shader
->config
.num_vgprs
- 1) / 4) |
124 S_00B848_SGPRS((shader
->config
.num_sgprs
- 1) / 8) |
125 S_00B848_DX10_CLAMP(1) |
126 S_00B848_FLOAT_MODE(shader
->config
.float_mode
);
128 shader
->config
.rsrc2
=
129 S_00B84C_USER_SGPR(user_sgprs
) |
130 S_00B84C_SCRATCH_EN(scratch_enabled
) |
131 S_00B84C_TGID_X_EN(sel
.info
.uses_block_id
[0]) |
132 S_00B84C_TGID_Y_EN(sel
.info
.uses_block_id
[1]) |
133 S_00B84C_TGID_Z_EN(sel
.info
.uses_block_id
[2]) |
134 S_00B84C_TIDIG_COMP_CNT(sel
.info
.uses_thread_id
[2] ? 2 :
135 sel
.info
.uses_thread_id
[1] ? 1 : 0) |
136 S_00B84C_LDS_SIZE(shader
->config
.lds_size
);
138 program
->variable_group_size
=
139 sel
.info
.properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
] == 0;
142 FREE(program
->tokens
);
143 program
->shader
.selector
= NULL
;
146 static void *si_create_compute_state(
147 struct pipe_context
*ctx
,
148 const struct pipe_compute_state
*cso
)
150 struct si_context
*sctx
= (struct si_context
*)ctx
;
151 struct si_screen
*sscreen
= (struct si_screen
*)ctx
->screen
;
152 struct si_compute
*program
= CALLOC_STRUCT(si_compute
);
154 pipe_reference_init(&program
->reference
, 1);
155 program
->screen
= (struct si_screen
*)ctx
->screen
;
156 program
->ir_type
= cso
->ir_type
;
157 program
->local_size
= cso
->req_local_mem
;
158 program
->private_size
= cso
->req_private_mem
;
159 program
->input_size
= cso
->req_input_mem
;
160 program
->use_code_object_v2
= HAVE_LLVM
>= 0x0400 &&
161 cso
->ir_type
== PIPE_SHADER_IR_NATIVE
;
163 if (cso
->ir_type
== PIPE_SHADER_IR_TGSI
) {
164 program
->tokens
= tgsi_dup_tokens(cso
->prog
);
165 if (!program
->tokens
) {
170 program
->compiler_ctx_state
.tm
= sctx
->tm
;
171 program
->compiler_ctx_state
.debug
= sctx
->b
.debug
;
172 program
->compiler_ctx_state
.is_debug_context
= sctx
->is_debug
;
173 p_atomic_inc(&sscreen
->b
.num_shaders_created
);
174 util_queue_fence_init(&program
->ready
);
176 if ((sctx
->b
.debug
.debug_message
&& !sctx
->b
.debug
.async
) ||
178 r600_can_dump_shader(&sscreen
->b
, PIPE_SHADER_COMPUTE
))
179 si_create_compute_state_async(program
, -1);
181 util_queue_add_job(&sscreen
->shader_compiler_queue
,
182 program
, &program
->ready
,
183 si_create_compute_state_async
, NULL
);
185 const struct pipe_llvm_program_header
*header
;
188 code
= cso
->prog
+ sizeof(struct pipe_llvm_program_header
);
190 ac_elf_read(code
, header
->num_bytes
, &program
->shader
.binary
);
191 if (program
->use_code_object_v2
) {
192 const amd_kernel_code_t
*code_object
=
193 si_compute_get_code_object(program
, 0);
194 code_object_to_config(code_object
, &program
->shader
.config
);
196 si_shader_binary_read_config(&program
->shader
.binary
,
197 &program
->shader
.config
, 0);
199 si_shader_dump(sctx
->screen
, &program
->shader
, &sctx
->b
.debug
,
200 PIPE_SHADER_COMPUTE
, stderr
, true);
201 if (si_shader_binary_upload(sctx
->screen
, &program
->shader
) < 0) {
202 fprintf(stderr
, "LLVM failed to upload shader\n");
211 static void si_bind_compute_state(struct pipe_context
*ctx
, void *state
)
213 struct si_context
*sctx
= (struct si_context
*)ctx
;
214 struct si_compute
*program
= (struct si_compute
*)state
;
216 sctx
->cs_shader_state
.program
= program
;
220 /* Wait because we need active slot usage masks. */
221 if (program
->ir_type
== PIPE_SHADER_IR_TGSI
)
222 util_queue_fence_wait(&program
->ready
);
224 si_set_active_descriptors(sctx
,
225 SI_DESCS_FIRST_COMPUTE
+
226 SI_SHADER_DESCS_CONST_AND_SHADER_BUFFERS
,
227 program
->active_const_and_shader_buffers
);
228 si_set_active_descriptors(sctx
,
229 SI_DESCS_FIRST_COMPUTE
+
230 SI_SHADER_DESCS_SAMPLERS_AND_IMAGES
,
231 program
->active_samplers_and_images
);
234 static void si_set_global_binding(
235 struct pipe_context
*ctx
, unsigned first
, unsigned n
,
236 struct pipe_resource
**resources
,
240 struct si_context
*sctx
= (struct si_context
*)ctx
;
241 struct si_compute
*program
= sctx
->cs_shader_state
.program
;
243 assert(first
+ n
<= MAX_GLOBAL_BUFFERS
);
246 for (i
= 0; i
< n
; i
++) {
247 pipe_resource_reference(&program
->global_buffers
[first
+ i
], NULL
);
252 for (i
= 0; i
< n
; i
++) {
255 pipe_resource_reference(&program
->global_buffers
[first
+ i
], resources
[i
]);
256 va
= r600_resource(resources
[i
])->gpu_address
;
257 offset
= util_le32_to_cpu(*handles
[i
]);
259 va
= util_cpu_to_le64(va
);
260 memcpy(handles
[i
], &va
, sizeof(va
));
264 static void si_initialize_compute(struct si_context
*sctx
)
266 struct radeon_winsys_cs
*cs
= sctx
->b
.gfx
.cs
;
269 radeon_set_sh_reg_seq(cs
, R_00B858_COMPUTE_STATIC_THREAD_MGMT_SE0
, 2);
270 /* R_00B858_COMPUTE_STATIC_THREAD_MGMT_SE0 / SE1 */
271 radeon_emit(cs
, S_00B858_SH0_CU_EN(0xffff) | S_00B858_SH1_CU_EN(0xffff));
272 radeon_emit(cs
, S_00B85C_SH0_CU_EN(0xffff) | S_00B85C_SH1_CU_EN(0xffff));
274 if (sctx
->b
.chip_class
>= CIK
) {
275 /* Also set R_00B858_COMPUTE_STATIC_THREAD_MGMT_SE2 / SE3 */
276 radeon_set_sh_reg_seq(cs
,
277 R_00B864_COMPUTE_STATIC_THREAD_MGMT_SE2
, 2);
278 radeon_emit(cs
, S_00B864_SH0_CU_EN(0xffff) |
279 S_00B864_SH1_CU_EN(0xffff));
280 radeon_emit(cs
, S_00B868_SH0_CU_EN(0xffff) |
281 S_00B868_SH1_CU_EN(0xffff));
284 /* This register has been moved to R_00CD20_COMPUTE_MAX_WAVE_ID
285 * and is now per pipe, so it should be handled in the
286 * kernel if we want to use something other than the default value,
287 * which is now 0x22f.
289 if (sctx
->b
.chip_class
<= SI
) {
290 /* XXX: This should be:
291 * (number of compute units) * 4 * (waves per simd) - 1 */
293 radeon_set_sh_reg(cs
, R_00B82C_COMPUTE_MAX_WAVE_ID
,
294 0x190 /* Default value */);
297 /* Set the pointer to border colors. */
298 bc_va
= sctx
->border_color_buffer
->gpu_address
;
300 if (sctx
->b
.chip_class
>= CIK
) {
301 radeon_set_uconfig_reg_seq(cs
, R_030E00_TA_CS_BC_BASE_ADDR
, 2);
302 radeon_emit(cs
, bc_va
>> 8); /* R_030E00_TA_CS_BC_BASE_ADDR */
303 radeon_emit(cs
, bc_va
>> 40); /* R_030E04_TA_CS_BC_BASE_ADDR_HI */
305 if (sctx
->screen
->b
.info
.drm_major
== 3 ||
306 (sctx
->screen
->b
.info
.drm_major
== 2 &&
307 sctx
->screen
->b
.info
.drm_minor
>= 48)) {
308 radeon_set_config_reg(cs
, R_00950C_TA_CS_BC_BASE_ADDR
,
313 sctx
->cs_shader_state
.emitted_program
= NULL
;
314 sctx
->cs_shader_state
.initialized
= true;
317 static bool si_setup_compute_scratch_buffer(struct si_context
*sctx
,
318 struct si_shader
*shader
,
319 struct si_shader_config
*config
)
321 uint64_t scratch_bo_size
, scratch_needed
;
323 scratch_needed
= config
->scratch_bytes_per_wave
* sctx
->scratch_waves
;
324 if (sctx
->compute_scratch_buffer
)
325 scratch_bo_size
= sctx
->compute_scratch_buffer
->b
.b
.width0
;
327 if (scratch_bo_size
< scratch_needed
) {
328 r600_resource_reference(&sctx
->compute_scratch_buffer
, NULL
);
330 sctx
->compute_scratch_buffer
= (struct r600_resource
*)
331 r600_aligned_buffer_create(&sctx
->screen
->b
.b
,
332 R600_RESOURCE_FLAG_UNMAPPABLE
,
334 scratch_needed
, 256);
336 if (!sctx
->compute_scratch_buffer
)
340 if (sctx
->compute_scratch_buffer
!= shader
->scratch_bo
&& scratch_needed
) {
341 uint64_t scratch_va
= sctx
->compute_scratch_buffer
->gpu_address
;
343 si_shader_apply_scratch_relocs(shader
, scratch_va
);
345 if (si_shader_binary_upload(sctx
->screen
, shader
))
348 r600_resource_reference(&shader
->scratch_bo
,
349 sctx
->compute_scratch_buffer
);
355 static bool si_switch_compute_shader(struct si_context
*sctx
,
356 struct si_compute
*program
,
357 struct si_shader
*shader
,
358 const amd_kernel_code_t
*code_object
,
361 struct radeon_winsys_cs
*cs
= sctx
->b
.gfx
.cs
;
362 struct si_shader_config inline_config
= {0};
363 struct si_shader_config
*config
;
366 if (sctx
->cs_shader_state
.emitted_program
== program
&&
367 sctx
->cs_shader_state
.offset
== offset
)
370 if (program
->ir_type
== PIPE_SHADER_IR_TGSI
) {
371 config
= &shader
->config
;
375 config
= &inline_config
;
377 code_object_to_config(code_object
, config
);
379 si_shader_binary_read_config(&shader
->binary
, config
, offset
);
382 lds_blocks
= config
->lds_size
;
383 /* XXX: We are over allocating LDS. For SI, the shader reports
384 * LDS in blocks of 256 bytes, so if there are 4 bytes lds
385 * allocated in the shader and 4 bytes allocated by the state
386 * tracker, then we will set LDS_SIZE to 512 bytes rather than 256.
388 if (sctx
->b
.chip_class
<= SI
) {
389 lds_blocks
+= align(program
->local_size
, 256) >> 8;
391 lds_blocks
+= align(program
->local_size
, 512) >> 9;
394 /* TODO: use si_multiwave_lds_size_workaround */
395 assert(lds_blocks
<= 0xFF);
397 config
->rsrc2
&= C_00B84C_LDS_SIZE
;
398 config
->rsrc2
|= S_00B84C_LDS_SIZE(lds_blocks
);
401 if (!si_setup_compute_scratch_buffer(sctx
, shader
, config
))
404 if (shader
->scratch_bo
) {
405 COMPUTE_DBG(sctx
->screen
, "Waves: %u; Scratch per wave: %u bytes; "
406 "Total Scratch: %u bytes\n", sctx
->scratch_waves
,
407 config
->scratch_bytes_per_wave
,
408 config
->scratch_bytes_per_wave
*
409 sctx
->scratch_waves
);
411 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
412 shader
->scratch_bo
, RADEON_USAGE_READWRITE
,
413 RADEON_PRIO_SCRATCH_BUFFER
);
416 /* Prefetch the compute shader to TC L2.
418 * We should also prefetch graphics shaders if a compute dispatch was
419 * the last command, and the compute shader if a draw call was the last
420 * command. However, that would add more complexity and we're likely
421 * to get a shader state change in that case anyway.
423 if (sctx
->b
.chip_class
>= CIK
) {
424 cik_prefetch_TC_L2_async(sctx
, &program
->shader
.bo
->b
.b
,
425 0, program
->shader
.bo
->b
.b
.width0
);
428 shader_va
= shader
->bo
->gpu_address
+ offset
;
429 if (program
->use_code_object_v2
) {
430 /* Shader code is placed after the amd_kernel_code_t
432 shader_va
+= sizeof(amd_kernel_code_t
);
435 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
, shader
->bo
,
436 RADEON_USAGE_READ
, RADEON_PRIO_SHADER_BINARY
);
438 radeon_set_sh_reg_seq(cs
, R_00B830_COMPUTE_PGM_LO
, 2);
439 radeon_emit(cs
, shader_va
>> 8);
440 radeon_emit(cs
, shader_va
>> 40);
442 radeon_set_sh_reg_seq(cs
, R_00B848_COMPUTE_PGM_RSRC1
, 2);
443 radeon_emit(cs
, config
->rsrc1
);
444 radeon_emit(cs
, config
->rsrc2
);
446 COMPUTE_DBG(sctx
->screen
, "COMPUTE_PGM_RSRC1: 0x%08x "
447 "COMPUTE_PGM_RSRC2: 0x%08x\n", config
->rsrc1
, config
->rsrc2
);
449 radeon_set_sh_reg(cs
, R_00B860_COMPUTE_TMPRING_SIZE
,
450 S_00B860_WAVES(sctx
->scratch_waves
)
451 | S_00B860_WAVESIZE(config
->scratch_bytes_per_wave
>> 10));
453 sctx
->cs_shader_state
.emitted_program
= program
;
454 sctx
->cs_shader_state
.offset
= offset
;
455 sctx
->cs_shader_state
.uses_scratch
=
456 config
->scratch_bytes_per_wave
!= 0;
461 static void setup_scratch_rsrc_user_sgprs(struct si_context
*sctx
,
462 const amd_kernel_code_t
*code_object
,
465 struct radeon_winsys_cs
*cs
= sctx
->b
.gfx
.cs
;
466 uint64_t scratch_va
= sctx
->compute_scratch_buffer
->gpu_address
;
468 unsigned max_private_element_size
= AMD_HSA_BITS_GET(
469 code_object
->code_properties
,
470 AMD_CODE_PROPERTY_PRIVATE_ELEMENT_SIZE
);
472 uint32_t scratch_dword0
= scratch_va
& 0xffffffff;
473 uint32_t scratch_dword1
=
474 S_008F04_BASE_ADDRESS_HI(scratch_va
>> 32) |
475 S_008F04_SWIZZLE_ENABLE(1);
477 /* Disable address clamping */
478 uint32_t scratch_dword2
= 0xffffffff;
479 uint32_t scratch_dword3
=
480 S_008F0C_INDEX_STRIDE(3) |
481 S_008F0C_ADD_TID_ENABLE(1);
483 if (sctx
->b
.chip_class
>= GFX9
) {
484 assert(max_private_element_size
== 1); /* always 4 bytes on GFX9 */
486 scratch_dword3
|= S_008F0C_ELEMENT_SIZE(max_private_element_size
);
488 if (sctx
->b
.chip_class
< VI
) {
489 /* BUF_DATA_FORMAT is ignored, but it cannot be
490 * BUF_DATA_FORMAT_INVALID. */
492 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_8
);
496 radeon_set_sh_reg_seq(cs
, R_00B900_COMPUTE_USER_DATA_0
+
498 radeon_emit(cs
, scratch_dword0
);
499 radeon_emit(cs
, scratch_dword1
);
500 radeon_emit(cs
, scratch_dword2
);
501 radeon_emit(cs
, scratch_dword3
);
504 static void si_setup_user_sgprs_co_v2(struct si_context
*sctx
,
505 const amd_kernel_code_t
*code_object
,
506 const struct pipe_grid_info
*info
,
507 uint64_t kernel_args_va
)
509 struct si_compute
*program
= sctx
->cs_shader_state
.program
;
510 struct radeon_winsys_cs
*cs
= sctx
->b
.gfx
.cs
;
512 static const enum amd_code_property_mask_t workgroup_count_masks
[] = {
513 AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_X
,
514 AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Y
,
515 AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Z
518 unsigned i
, user_sgpr
= 0;
519 if (AMD_HSA_BITS_GET(code_object
->code_properties
,
520 AMD_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER
)) {
521 if (code_object
->workitem_private_segment_byte_size
> 0) {
522 setup_scratch_rsrc_user_sgprs(sctx
, code_object
,
528 if (AMD_HSA_BITS_GET(code_object
->code_properties
,
529 AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR
)) {
530 struct dispatch_packet dispatch
;
531 unsigned dispatch_offset
;
532 struct r600_resource
*dispatch_buf
= NULL
;
533 uint64_t dispatch_va
;
535 /* Upload dispatch ptr */
536 memset(&dispatch
, 0, sizeof(dispatch
));
538 dispatch
.workgroup_size_x
= info
->block
[0];
539 dispatch
.workgroup_size_y
= info
->block
[1];
540 dispatch
.workgroup_size_z
= info
->block
[2];
542 dispatch
.grid_size_x
= info
->grid
[0] * info
->block
[0];
543 dispatch
.grid_size_y
= info
->grid
[1] * info
->block
[1];
544 dispatch
.grid_size_z
= info
->grid
[2] * info
->block
[2];
546 dispatch
.private_segment_size
= program
->private_size
;
547 dispatch
.group_segment_size
= program
->local_size
;
549 dispatch
.kernarg_address
= kernel_args_va
;
551 u_upload_data(sctx
->b
.b
.const_uploader
, 0, sizeof(dispatch
),
552 256, &dispatch
, &dispatch_offset
,
553 (struct pipe_resource
**)&dispatch_buf
);
556 fprintf(stderr
, "Error: Failed to allocate dispatch "
559 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
, dispatch_buf
,
560 RADEON_USAGE_READ
, RADEON_PRIO_CONST_BUFFER
);
562 dispatch_va
= dispatch_buf
->gpu_address
+ dispatch_offset
;
564 radeon_set_sh_reg_seq(cs
, R_00B900_COMPUTE_USER_DATA_0
+
566 radeon_emit(cs
, dispatch_va
);
567 radeon_emit(cs
, S_008F04_BASE_ADDRESS_HI(dispatch_va
>> 32) |
570 r600_resource_reference(&dispatch_buf
, NULL
);
574 if (AMD_HSA_BITS_GET(code_object
->code_properties
,
575 AMD_CODE_PROPERTY_ENABLE_SGPR_KERNARG_SEGMENT_PTR
)) {
576 radeon_set_sh_reg_seq(cs
, R_00B900_COMPUTE_USER_DATA_0
+
578 radeon_emit(cs
, kernel_args_va
);
579 radeon_emit(cs
, S_008F04_BASE_ADDRESS_HI (kernel_args_va
>> 32) |
584 for (i
= 0; i
< 3 && user_sgpr
< 16; i
++) {
585 if (code_object
->code_properties
& workgroup_count_masks
[i
]) {
586 radeon_set_sh_reg_seq(cs
,
587 R_00B900_COMPUTE_USER_DATA_0
+
589 radeon_emit(cs
, info
->grid
[i
]);
595 static bool si_upload_compute_input(struct si_context
*sctx
,
596 const amd_kernel_code_t
*code_object
,
597 const struct pipe_grid_info
*info
)
599 struct radeon_winsys_cs
*cs
= sctx
->b
.gfx
.cs
;
600 struct si_compute
*program
= sctx
->cs_shader_state
.program
;
601 struct r600_resource
*input_buffer
= NULL
;
602 unsigned kernel_args_size
;
603 unsigned num_work_size_bytes
= program
->use_code_object_v2
? 0 : 36;
604 uint32_t kernel_args_offset
= 0;
605 uint32_t *kernel_args
;
606 void *kernel_args_ptr
;
607 uint64_t kernel_args_va
;
610 /* The extra num_work_size_bytes are for work group / work item size information */
611 kernel_args_size
= program
->input_size
+ num_work_size_bytes
;
613 u_upload_alloc(sctx
->b
.b
.const_uploader
, 0, kernel_args_size
,
614 sctx
->screen
->b
.info
.tcc_cache_line_size
,
616 (struct pipe_resource
**)&input_buffer
, &kernel_args_ptr
);
618 if (unlikely(!kernel_args_ptr
))
621 kernel_args
= (uint32_t*)kernel_args_ptr
;
622 kernel_args_va
= input_buffer
->gpu_address
+ kernel_args_offset
;
625 for (i
= 0; i
< 3; i
++) {
626 kernel_args
[i
] = info
->grid
[i
];
627 kernel_args
[i
+ 3] = info
->grid
[i
] * info
->block
[i
];
628 kernel_args
[i
+ 6] = info
->block
[i
];
632 memcpy(kernel_args
+ (num_work_size_bytes
/ 4), info
->input
,
633 program
->input_size
);
636 for (i
= 0; i
< (kernel_args_size
/ 4); i
++) {
637 COMPUTE_DBG(sctx
->screen
, "input %u : %u\n", i
,
642 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
, input_buffer
,
643 RADEON_USAGE_READ
, RADEON_PRIO_CONST_BUFFER
);
646 si_setup_user_sgprs_co_v2(sctx
, code_object
, info
, kernel_args_va
);
648 radeon_set_sh_reg_seq(cs
, R_00B900_COMPUTE_USER_DATA_0
, 2);
649 radeon_emit(cs
, kernel_args_va
);
650 radeon_emit(cs
, S_008F04_BASE_ADDRESS_HI (kernel_args_va
>> 32) |
654 r600_resource_reference(&input_buffer
, NULL
);
659 static void si_setup_tgsi_grid(struct si_context
*sctx
,
660 const struct pipe_grid_info
*info
)
662 struct si_compute
*program
= sctx
->cs_shader_state
.program
;
663 struct radeon_winsys_cs
*cs
= sctx
->b
.gfx
.cs
;
664 unsigned grid_size_reg
= R_00B900_COMPUTE_USER_DATA_0
+
665 4 * SI_NUM_RESOURCE_SGPRS
;
666 unsigned block_size_reg
= grid_size_reg
+
667 /* 12 bytes = 3 dwords. */
668 12 * program
->uses_grid_size
;
670 if (info
->indirect
) {
671 if (program
->uses_grid_size
) {
672 uint64_t base_va
= r600_resource(info
->indirect
)->gpu_address
;
673 uint64_t va
= base_va
+ info
->indirect_offset
;
676 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
677 (struct r600_resource
*)info
->indirect
,
678 RADEON_USAGE_READ
, RADEON_PRIO_DRAW_INDIRECT
);
680 for (i
= 0; i
< 3; ++i
) {
681 radeon_emit(cs
, PKT3(PKT3_COPY_DATA
, 4, 0));
682 radeon_emit(cs
, COPY_DATA_SRC_SEL(COPY_DATA_MEM
) |
683 COPY_DATA_DST_SEL(COPY_DATA_REG
));
684 radeon_emit(cs
, (va
+ 4 * i
));
685 radeon_emit(cs
, (va
+ 4 * i
) >> 32);
686 radeon_emit(cs
, (grid_size_reg
>> 2) + i
);
691 if (program
->uses_grid_size
) {
692 radeon_set_sh_reg_seq(cs
, grid_size_reg
, 3);
693 radeon_emit(cs
, info
->grid
[0]);
694 radeon_emit(cs
, info
->grid
[1]);
695 radeon_emit(cs
, info
->grid
[2]);
697 if (program
->variable_group_size
&& program
->uses_block_size
) {
698 radeon_set_sh_reg_seq(cs
, block_size_reg
, 3);
699 radeon_emit(cs
, info
->block
[0]);
700 radeon_emit(cs
, info
->block
[1]);
701 radeon_emit(cs
, info
->block
[2]);
706 static void si_emit_dispatch_packets(struct si_context
*sctx
,
707 const struct pipe_grid_info
*info
)
709 struct radeon_winsys_cs
*cs
= sctx
->b
.gfx
.cs
;
710 bool render_cond_bit
= sctx
->b
.render_cond
&& !sctx
->b
.render_cond_force_off
;
711 unsigned waves_per_threadgroup
=
712 DIV_ROUND_UP(info
->block
[0] * info
->block
[1] * info
->block
[2], 64);
714 radeon_set_sh_reg(cs
, R_00B854_COMPUTE_RESOURCE_LIMITS
,
715 S_00B854_SIMD_DEST_CNTL(waves_per_threadgroup
% 4 == 0));
717 radeon_set_sh_reg_seq(cs
, R_00B81C_COMPUTE_NUM_THREAD_X
, 3);
718 radeon_emit(cs
, S_00B81C_NUM_THREAD_FULL(info
->block
[0]));
719 radeon_emit(cs
, S_00B820_NUM_THREAD_FULL(info
->block
[1]));
720 radeon_emit(cs
, S_00B824_NUM_THREAD_FULL(info
->block
[2]));
722 unsigned dispatch_initiator
=
723 S_00B800_COMPUTE_SHADER_EN(1) |
724 S_00B800_FORCE_START_AT_000(1) |
725 /* If the KMD allows it (there is a KMD hw register for it),
726 * allow launching waves out-of-order. (same as Vulkan) */
727 S_00B800_ORDER_MODE(sctx
->b
.chip_class
>= CIK
);
729 if (info
->indirect
) {
730 uint64_t base_va
= r600_resource(info
->indirect
)->gpu_address
;
732 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
733 (struct r600_resource
*)info
->indirect
,
734 RADEON_USAGE_READ
, RADEON_PRIO_DRAW_INDIRECT
);
736 radeon_emit(cs
, PKT3(PKT3_SET_BASE
, 2, 0) |
737 PKT3_SHADER_TYPE_S(1));
739 radeon_emit(cs
, base_va
);
740 radeon_emit(cs
, base_va
>> 32);
742 radeon_emit(cs
, PKT3(PKT3_DISPATCH_INDIRECT
, 1, render_cond_bit
) |
743 PKT3_SHADER_TYPE_S(1));
744 radeon_emit(cs
, info
->indirect_offset
);
745 radeon_emit(cs
, dispatch_initiator
);
747 radeon_emit(cs
, PKT3(PKT3_DISPATCH_DIRECT
, 3, render_cond_bit
) |
748 PKT3_SHADER_TYPE_S(1));
749 radeon_emit(cs
, info
->grid
[0]);
750 radeon_emit(cs
, info
->grid
[1]);
751 radeon_emit(cs
, info
->grid
[2]);
752 radeon_emit(cs
, dispatch_initiator
);
757 static void si_launch_grid(
758 struct pipe_context
*ctx
, const struct pipe_grid_info
*info
)
760 struct si_context
*sctx
= (struct si_context
*)ctx
;
761 struct si_compute
*program
= sctx
->cs_shader_state
.program
;
762 const amd_kernel_code_t
*code_object
=
763 si_compute_get_code_object(program
, info
->pc
);
765 /* HW bug workaround when CS threadgroups > 256 threads and async
766 * compute isn't used, i.e. only one compute job can run at a time.
767 * If async compute is possible, the threadgroup size must be limited
768 * to 256 threads on all queues to avoid the bug.
769 * Only SI and certain CIK chips are affected.
771 bool cs_regalloc_hang
=
772 (sctx
->b
.chip_class
== SI
||
773 sctx
->b
.family
== CHIP_BONAIRE
||
774 sctx
->b
.family
== CHIP_KABINI
) &&
775 info
->block
[0] * info
->block
[1] * info
->block
[2] > 256;
777 if (cs_regalloc_hang
)
778 sctx
->b
.flags
|= SI_CONTEXT_PS_PARTIAL_FLUSH
|
779 SI_CONTEXT_CS_PARTIAL_FLUSH
;
781 if (program
->ir_type
== PIPE_SHADER_IR_TGSI
&&
782 program
->shader
.compilation_failed
)
785 if (sctx
->b
.last_num_draw_calls
!= sctx
->b
.num_draw_calls
) {
786 si_update_fb_dirtiness_after_rendering(sctx
);
787 sctx
->b
.last_num_draw_calls
= sctx
->b
.num_draw_calls
;
790 si_decompress_compute_textures(sctx
);
792 /* Add buffer sizes for memory checking in need_cs_space. */
793 r600_context_add_resource_size(ctx
, &program
->shader
.bo
->b
.b
);
794 /* TODO: add the scratch buffer */
796 if (info
->indirect
) {
797 r600_context_add_resource_size(ctx
, info
->indirect
);
799 /* Indirect buffers use TC L2 on GFX9, but not older hw. */
800 if (sctx
->b
.chip_class
<= VI
&&
801 r600_resource(info
->indirect
)->TC_L2_dirty
) {
802 sctx
->b
.flags
|= SI_CONTEXT_WRITEBACK_GLOBAL_L2
;
803 r600_resource(info
->indirect
)->TC_L2_dirty
= false;
807 si_need_cs_space(sctx
);
810 si_log_compute_state(sctx
, sctx
->b
.log
);
812 if (!sctx
->cs_shader_state
.initialized
)
813 si_initialize_compute(sctx
);
816 si_emit_cache_flush(sctx
);
818 if (!si_switch_compute_shader(sctx
, program
, &program
->shader
,
819 code_object
, info
->pc
))
822 si_upload_compute_shader_descriptors(sctx
);
823 si_emit_compute_shader_pointers(sctx
);
825 if (si_is_atom_dirty(sctx
, sctx
->atoms
.s
.render_cond
)) {
826 sctx
->atoms
.s
.render_cond
->emit(&sctx
->b
,
827 sctx
->atoms
.s
.render_cond
);
828 si_set_atom_dirty(sctx
, sctx
->atoms
.s
.render_cond
, false);
831 if ((program
->input_size
||
832 program
->ir_type
== PIPE_SHADER_IR_NATIVE
) &&
833 unlikely(!si_upload_compute_input(sctx
, code_object
, info
))) {
838 for (i
= 0; i
< MAX_GLOBAL_BUFFERS
; i
++) {
839 struct r600_resource
*buffer
=
840 (struct r600_resource
*)program
->global_buffers
[i
];
844 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
, buffer
,
845 RADEON_USAGE_READWRITE
,
846 RADEON_PRIO_COMPUTE_GLOBAL
);
849 if (program
->ir_type
== PIPE_SHADER_IR_TGSI
)
850 si_setup_tgsi_grid(sctx
, info
);
852 si_emit_dispatch_packets(sctx
, info
);
854 if (unlikely(sctx
->current_saved_cs
))
857 sctx
->compute_is_busy
= true;
858 sctx
->b
.num_compute_calls
++;
859 if (sctx
->cs_shader_state
.uses_scratch
)
860 sctx
->b
.num_spill_compute_calls
++;
862 if (cs_regalloc_hang
)
863 sctx
->b
.flags
|= SI_CONTEXT_CS_PARTIAL_FLUSH
;
866 void si_destroy_compute(struct si_compute
*program
)
868 if (program
->ir_type
== PIPE_SHADER_IR_TGSI
) {
869 util_queue_drop_job(&program
->screen
->shader_compiler_queue
,
871 util_queue_fence_destroy(&program
->ready
);
874 si_shader_destroy(&program
->shader
);
878 static void si_delete_compute_state(struct pipe_context
*ctx
, void* state
){
879 struct si_compute
*program
= (struct si_compute
*)state
;
880 struct si_context
*sctx
= (struct si_context
*)ctx
;
885 if (program
== sctx
->cs_shader_state
.program
)
886 sctx
->cs_shader_state
.program
= NULL
;
888 if (program
== sctx
->cs_shader_state
.emitted_program
)
889 sctx
->cs_shader_state
.emitted_program
= NULL
;
891 si_compute_reference(&program
, NULL
);
894 static void si_set_compute_resources(struct pipe_context
* ctx_
,
895 unsigned start
, unsigned count
,
896 struct pipe_surface
** surfaces
) { }
898 void si_init_compute_functions(struct si_context
*sctx
)
900 sctx
->b
.b
.create_compute_state
= si_create_compute_state
;
901 sctx
->b
.b
.delete_compute_state
= si_delete_compute_state
;
902 sctx
->b
.b
.bind_compute_state
= si_bind_compute_state
;
903 /* ctx->context.create_sampler_view = evergreen_compute_create_sampler_view; */
904 sctx
->b
.b
.set_compute_resources
= si_set_compute_resources
;
905 sctx
->b
.b
.set_global_binding
= si_set_global_binding
;
906 sctx
->b
.b
.launch_grid
= si_launch_grid
;