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"
34 #define MAX_GLOBAL_BUFFERS 22
37 struct si_screen
*screen
;
38 struct tgsi_token
*tokens
;
39 struct util_queue_fence ready
;
40 struct si_compiler_ctx_state compiler_ctx_state
;
44 unsigned private_size
;
46 struct si_shader shader
;
48 struct pipe_resource
*global_buffers
[MAX_GLOBAL_BUFFERS
];
49 unsigned use_code_object_v2
: 1;
50 unsigned variable_group_size
: 1;
51 unsigned uses_grid_size
:1;
52 unsigned uses_block_size
:1;
55 struct dispatch_packet
{
58 uint16_t workgroup_size_x
;
59 uint16_t workgroup_size_y
;
60 uint16_t workgroup_size_z
;
65 uint32_t private_segment_size
;
66 uint32_t group_segment_size
;
67 uint64_t kernel_object
;
68 uint64_t kernarg_address
;
72 static const amd_kernel_code_t
*si_compute_get_code_object(
73 const struct si_compute
*program
,
74 uint64_t symbol_offset
)
76 if (!program
->use_code_object_v2
) {
79 return (const amd_kernel_code_t
*)
80 (program
->shader
.binary
.code
+ symbol_offset
);
83 static void code_object_to_config(const amd_kernel_code_t
*code_object
,
84 struct si_shader_config
*out_config
) {
86 uint32_t rsrc1
= code_object
->compute_pgm_resource_registers
;
87 uint32_t rsrc2
= code_object
->compute_pgm_resource_registers
>> 32;
88 out_config
->num_sgprs
= code_object
->wavefront_sgpr_count
;
89 out_config
->num_vgprs
= code_object
->workitem_vgpr_count
;
90 out_config
->float_mode
= G_00B028_FLOAT_MODE(rsrc1
);
91 out_config
->rsrc1
= rsrc1
;
92 out_config
->lds_size
= MAX2(out_config
->lds_size
, G_00B84C_LDS_SIZE(rsrc2
));
93 out_config
->rsrc2
= rsrc2
;
94 out_config
->scratch_bytes_per_wave
=
95 align(code_object
->workitem_private_segment_byte_size
* 64, 1024);
98 /* Asynchronous compute shader compilation. */
99 static void si_create_compute_state_async(void *job
, int thread_index
)
101 struct si_compute
*program
= (struct si_compute
*)job
;
102 struct si_shader
*shader
= &program
->shader
;
103 struct si_shader_selector sel
;
104 LLVMTargetMachineRef tm
;
105 struct pipe_debug_callback
*debug
= &program
->compiler_ctx_state
.debug
;
107 if (thread_index
>= 0) {
108 assert(thread_index
< ARRAY_SIZE(program
->screen
->tm
));
109 tm
= program
->screen
->tm
[thread_index
];
113 tm
= program
->compiler_ctx_state
.tm
;
116 memset(&sel
, 0, sizeof(sel
));
118 sel
.screen
= program
->screen
;
119 tgsi_scan_shader(program
->tokens
, &sel
.info
);
120 sel
.tokens
= program
->tokens
;
121 sel
.type
= PIPE_SHADER_COMPUTE
;
122 sel
.local_size
= program
->local_size
;
124 program
->shader
.selector
= &sel
;
125 program
->shader
.is_monolithic
= true;
126 program
->uses_grid_size
= sel
.info
.uses_grid_size
;
127 program
->uses_block_size
= sel
.info
.uses_block_size
;
129 if (si_shader_create(program
->screen
, tm
, &program
->shader
, debug
)) {
130 program
->shader
.compilation_failed
= true;
132 bool scratch_enabled
= shader
->config
.scratch_bytes_per_wave
> 0;
133 unsigned user_sgprs
= SI_NUM_RESOURCE_SGPRS
+
134 (sel
.info
.uses_grid_size
? 3 : 0) +
135 (sel
.info
.uses_block_size
? 3 : 0);
137 shader
->config
.rsrc1
=
138 S_00B848_VGPRS((shader
->config
.num_vgprs
- 1) / 4) |
139 S_00B848_SGPRS((shader
->config
.num_sgprs
- 1) / 8) |
140 S_00B848_DX10_CLAMP(1) |
141 S_00B848_FLOAT_MODE(shader
->config
.float_mode
);
143 shader
->config
.rsrc2
=
144 S_00B84C_USER_SGPR(user_sgprs
) |
145 S_00B84C_SCRATCH_EN(scratch_enabled
) |
146 S_00B84C_TGID_X_EN(sel
.info
.uses_block_id
[0]) |
147 S_00B84C_TGID_Y_EN(sel
.info
.uses_block_id
[1]) |
148 S_00B84C_TGID_Z_EN(sel
.info
.uses_block_id
[2]) |
149 S_00B84C_TIDIG_COMP_CNT(sel
.info
.uses_thread_id
[2] ? 2 :
150 sel
.info
.uses_thread_id
[1] ? 1 : 0) |
151 S_00B84C_LDS_SIZE(shader
->config
.lds_size
);
153 program
->variable_group_size
=
154 sel
.info
.properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
] == 0;
157 FREE(program
->tokens
);
158 program
->shader
.selector
= NULL
;
161 static void *si_create_compute_state(
162 struct pipe_context
*ctx
,
163 const struct pipe_compute_state
*cso
)
165 struct si_context
*sctx
= (struct si_context
*)ctx
;
166 struct si_screen
*sscreen
= (struct si_screen
*)ctx
->screen
;
167 struct si_compute
*program
= CALLOC_STRUCT(si_compute
);
169 program
->screen
= (struct si_screen
*)ctx
->screen
;
170 program
->ir_type
= cso
->ir_type
;
171 program
->local_size
= cso
->req_local_mem
;
172 program
->private_size
= cso
->req_private_mem
;
173 program
->input_size
= cso
->req_input_mem
;
174 program
->use_code_object_v2
= HAVE_LLVM
>= 0x0400 &&
175 cso
->ir_type
== PIPE_SHADER_IR_NATIVE
;
177 if (cso
->ir_type
== PIPE_SHADER_IR_TGSI
) {
178 program
->tokens
= tgsi_dup_tokens(cso
->prog
);
179 if (!program
->tokens
) {
184 program
->compiler_ctx_state
.tm
= sctx
->tm
;
185 program
->compiler_ctx_state
.debug
= sctx
->b
.debug
;
186 program
->compiler_ctx_state
.is_debug_context
= sctx
->is_debug
;
187 p_atomic_inc(&sscreen
->b
.num_shaders_created
);
188 util_queue_fence_init(&program
->ready
);
190 if ((sctx
->b
.debug
.debug_message
&& !sctx
->b
.debug
.async
) ||
192 r600_can_dump_shader(&sscreen
->b
, PIPE_SHADER_COMPUTE
))
193 si_create_compute_state_async(program
, -1);
195 util_queue_add_job(&sscreen
->shader_compiler_queue
,
196 program
, &program
->ready
,
197 si_create_compute_state_async
, NULL
);
199 const struct pipe_llvm_program_header
*header
;
202 code
= cso
->prog
+ sizeof(struct pipe_llvm_program_header
);
204 ac_elf_read(code
, header
->num_bytes
, &program
->shader
.binary
);
205 if (program
->use_code_object_v2
) {
206 const amd_kernel_code_t
*code_object
=
207 si_compute_get_code_object(program
, 0);
208 code_object_to_config(code_object
, &program
->shader
.config
);
210 si_shader_binary_read_config(&program
->shader
.binary
,
211 &program
->shader
.config
, 0);
213 si_shader_dump(sctx
->screen
, &program
->shader
, &sctx
->b
.debug
,
214 PIPE_SHADER_COMPUTE
, stderr
, true);
215 if (si_shader_binary_upload(sctx
->screen
, &program
->shader
) < 0) {
216 fprintf(stderr
, "LLVM failed to upload shader\n");
225 static void si_bind_compute_state(struct pipe_context
*ctx
, void *state
)
227 struct si_context
*sctx
= (struct si_context
*)ctx
;
228 sctx
->cs_shader_state
.program
= (struct si_compute
*)state
;
231 static void si_set_global_binding(
232 struct pipe_context
*ctx
, unsigned first
, unsigned n
,
233 struct pipe_resource
**resources
,
237 struct si_context
*sctx
= (struct si_context
*)ctx
;
238 struct si_compute
*program
= sctx
->cs_shader_state
.program
;
240 assert(first
+ n
<= MAX_GLOBAL_BUFFERS
);
243 for (i
= 0; i
< n
; i
++) {
244 pipe_resource_reference(&program
->global_buffers
[first
+ i
], NULL
);
249 for (i
= 0; i
< n
; i
++) {
252 pipe_resource_reference(&program
->global_buffers
[first
+ i
], resources
[i
]);
253 va
= r600_resource(resources
[i
])->gpu_address
;
254 offset
= util_le32_to_cpu(*handles
[i
]);
256 va
= util_cpu_to_le64(va
);
257 memcpy(handles
[i
], &va
, sizeof(va
));
261 static void si_initialize_compute(struct si_context
*sctx
)
263 struct radeon_winsys_cs
*cs
= sctx
->b
.gfx
.cs
;
266 radeon_set_sh_reg_seq(cs
, R_00B810_COMPUTE_START_X
, 3);
271 radeon_set_sh_reg_seq(cs
, R_00B858_COMPUTE_STATIC_THREAD_MGMT_SE0
, 2);
272 /* R_00B858_COMPUTE_STATIC_THREAD_MGMT_SE0 / SE1 */
273 radeon_emit(cs
, S_00B858_SH0_CU_EN(0xffff) | S_00B858_SH1_CU_EN(0xffff));
274 radeon_emit(cs
, S_00B85C_SH0_CU_EN(0xffff) | S_00B85C_SH1_CU_EN(0xffff));
276 if (sctx
->b
.chip_class
>= CIK
) {
277 /* Also set R_00B858_COMPUTE_STATIC_THREAD_MGMT_SE2 / SE3 */
278 radeon_set_sh_reg_seq(cs
,
279 R_00B864_COMPUTE_STATIC_THREAD_MGMT_SE2
, 2);
280 radeon_emit(cs
, S_00B864_SH0_CU_EN(0xffff) |
281 S_00B864_SH1_CU_EN(0xffff));
282 radeon_emit(cs
, S_00B868_SH0_CU_EN(0xffff) |
283 S_00B868_SH1_CU_EN(0xffff));
286 /* This register has been moved to R_00CD20_COMPUTE_MAX_WAVE_ID
287 * and is now per pipe, so it should be handled in the
288 * kernel if we want to use something other than the default value,
289 * which is now 0x22f.
291 if (sctx
->b
.chip_class
<= SI
) {
292 /* XXX: This should be:
293 * (number of compute units) * 4 * (waves per simd) - 1 */
295 radeon_set_sh_reg(cs
, R_00B82C_COMPUTE_MAX_WAVE_ID
,
296 0x190 /* Default value */);
299 /* Set the pointer to border colors. */
300 bc_va
= sctx
->border_color_buffer
->gpu_address
;
302 if (sctx
->b
.chip_class
>= CIK
) {
303 radeon_set_uconfig_reg_seq(cs
, R_030E00_TA_CS_BC_BASE_ADDR
, 2);
304 radeon_emit(cs
, bc_va
>> 8); /* R_030E00_TA_CS_BC_BASE_ADDR */
305 radeon_emit(cs
, bc_va
>> 40); /* R_030E04_TA_CS_BC_BASE_ADDR_HI */
307 if (sctx
->screen
->b
.info
.drm_major
== 3 ||
308 (sctx
->screen
->b
.info
.drm_major
== 2 &&
309 sctx
->screen
->b
.info
.drm_minor
>= 48)) {
310 radeon_set_config_reg(cs
, R_00950C_TA_CS_BC_BASE_ADDR
,
315 sctx
->cs_shader_state
.emitted_program
= NULL
;
316 sctx
->cs_shader_state
.initialized
= true;
319 static bool si_setup_compute_scratch_buffer(struct si_context
*sctx
,
320 struct si_shader
*shader
,
321 struct si_shader_config
*config
)
323 uint64_t scratch_bo_size
, scratch_needed
;
325 scratch_needed
= config
->scratch_bytes_per_wave
* sctx
->scratch_waves
;
326 if (sctx
->compute_scratch_buffer
)
327 scratch_bo_size
= sctx
->compute_scratch_buffer
->b
.b
.width0
;
329 if (scratch_bo_size
< scratch_needed
) {
330 r600_resource_reference(&sctx
->compute_scratch_buffer
, NULL
);
332 sctx
->compute_scratch_buffer
= (struct r600_resource
*)
333 r600_aligned_buffer_create(&sctx
->screen
->b
.b
,
334 R600_RESOURCE_FLAG_UNMAPPABLE
,
336 scratch_needed
, 256);
338 if (!sctx
->compute_scratch_buffer
)
342 if (sctx
->compute_scratch_buffer
!= shader
->scratch_bo
&& scratch_needed
) {
343 uint64_t scratch_va
= sctx
->compute_scratch_buffer
->gpu_address
;
345 si_shader_apply_scratch_relocs(sctx
, shader
, config
, scratch_va
);
347 if (si_shader_binary_upload(sctx
->screen
, shader
))
350 r600_resource_reference(&shader
->scratch_bo
,
351 sctx
->compute_scratch_buffer
);
357 static bool si_switch_compute_shader(struct si_context
*sctx
,
358 struct si_compute
*program
,
359 struct si_shader
*shader
,
360 const amd_kernel_code_t
*code_object
,
363 struct radeon_winsys_cs
*cs
= sctx
->b
.gfx
.cs
;
364 struct si_shader_config inline_config
= {0};
365 struct si_shader_config
*config
;
368 if (sctx
->cs_shader_state
.emitted_program
== program
&&
369 sctx
->cs_shader_state
.offset
== offset
)
372 if (program
->ir_type
== PIPE_SHADER_IR_TGSI
) {
373 config
= &shader
->config
;
377 config
= &inline_config
;
379 code_object_to_config(code_object
, config
);
381 si_shader_binary_read_config(&shader
->binary
, config
, offset
);
384 lds_blocks
= config
->lds_size
;
385 /* XXX: We are over allocating LDS. For SI, the shader reports
386 * LDS in blocks of 256 bytes, so if there are 4 bytes lds
387 * allocated in the shader and 4 bytes allocated by the state
388 * tracker, then we will set LDS_SIZE to 512 bytes rather than 256.
390 if (sctx
->b
.chip_class
<= SI
) {
391 lds_blocks
+= align(program
->local_size
, 256) >> 8;
393 lds_blocks
+= align(program
->local_size
, 512) >> 9;
396 /* TODO: use si_multiwave_lds_size_workaround */
397 assert(lds_blocks
<= 0xFF);
399 config
->rsrc2
&= C_00B84C_LDS_SIZE
;
400 config
->rsrc2
|= S_00B84C_LDS_SIZE(lds_blocks
);
403 if (!si_setup_compute_scratch_buffer(sctx
, shader
, config
))
406 if (shader
->scratch_bo
) {
407 COMPUTE_DBG(sctx
->screen
, "Waves: %u; Scratch per wave: %u bytes; "
408 "Total Scratch: %u bytes\n", sctx
->scratch_waves
,
409 config
->scratch_bytes_per_wave
,
410 config
->scratch_bytes_per_wave
*
411 sctx
->scratch_waves
);
413 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
414 shader
->scratch_bo
, RADEON_USAGE_READWRITE
,
415 RADEON_PRIO_SCRATCH_BUFFER
);
418 /* Prefetch the compute shader to TC L2.
420 * We should also prefetch graphics shaders if a compute dispatch was
421 * the last command, and the compute shader if a draw call was the last
422 * command. However, that would add more complexity and we're likely
423 * to get a shader state change in that case anyway.
425 if (sctx
->b
.chip_class
>= CIK
) {
426 cik_prefetch_TC_L2_async(sctx
, &program
->shader
.bo
->b
.b
,
427 0, program
->shader
.bo
->b
.b
.width0
);
430 shader_va
= shader
->bo
->gpu_address
+ offset
;
431 if (program
->use_code_object_v2
) {
432 /* Shader code is placed after the amd_kernel_code_t
434 shader_va
+= sizeof(amd_kernel_code_t
);
437 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
, shader
->bo
,
438 RADEON_USAGE_READ
, RADEON_PRIO_SHADER_BINARY
);
440 radeon_set_sh_reg_seq(cs
, R_00B830_COMPUTE_PGM_LO
, 2);
441 radeon_emit(cs
, shader_va
>> 8);
442 radeon_emit(cs
, shader_va
>> 40);
444 radeon_set_sh_reg_seq(cs
, R_00B848_COMPUTE_PGM_RSRC1
, 2);
445 radeon_emit(cs
, config
->rsrc1
);
446 radeon_emit(cs
, config
->rsrc2
);
448 COMPUTE_DBG(sctx
->screen
, "COMPUTE_PGM_RSRC1: 0x%08x "
449 "COMPUTE_PGM_RSRC2: 0x%08x\n", config
->rsrc1
, config
->rsrc2
);
451 radeon_set_sh_reg(cs
, R_00B860_COMPUTE_TMPRING_SIZE
,
452 S_00B860_WAVES(sctx
->scratch_waves
)
453 | S_00B860_WAVESIZE(config
->scratch_bytes_per_wave
>> 10));
455 sctx
->cs_shader_state
.emitted_program
= program
;
456 sctx
->cs_shader_state
.offset
= offset
;
457 sctx
->cs_shader_state
.uses_scratch
=
458 config
->scratch_bytes_per_wave
!= 0;
463 static void setup_scratch_rsrc_user_sgprs(struct si_context
*sctx
,
464 const amd_kernel_code_t
*code_object
,
467 struct radeon_winsys_cs
*cs
= sctx
->b
.gfx
.cs
;
468 uint64_t scratch_va
= sctx
->compute_scratch_buffer
->gpu_address
;
470 unsigned max_private_element_size
= AMD_HSA_BITS_GET(
471 code_object
->code_properties
,
472 AMD_CODE_PROPERTY_PRIVATE_ELEMENT_SIZE
);
474 uint32_t scratch_dword0
= scratch_va
& 0xffffffff;
475 uint32_t scratch_dword1
=
476 S_008F04_BASE_ADDRESS_HI(scratch_va
>> 32) |
477 S_008F04_SWIZZLE_ENABLE(1);
479 /* Disable address clamping */
480 uint32_t scratch_dword2
= 0xffffffff;
481 uint32_t scratch_dword3
=
482 S_008F0C_INDEX_STRIDE(3) |
483 S_008F0C_ADD_TID_ENABLE(1);
485 if (sctx
->b
.chip_class
>= GFX9
) {
486 assert(max_private_element_size
== 1); /* always 4 bytes on GFX9 */
488 scratch_dword3
|= S_008F0C_ELEMENT_SIZE(max_private_element_size
);
490 if (sctx
->b
.chip_class
< VI
) {
491 /* BUF_DATA_FORMAT is ignored, but it cannot be
492 * BUF_DATA_FORMAT_INVALID. */
494 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_8
);
498 radeon_set_sh_reg_seq(cs
, R_00B900_COMPUTE_USER_DATA_0
+
500 radeon_emit(cs
, scratch_dword0
);
501 radeon_emit(cs
, scratch_dword1
);
502 radeon_emit(cs
, scratch_dword2
);
503 radeon_emit(cs
, scratch_dword3
);
506 static void si_setup_user_sgprs_co_v2(struct si_context
*sctx
,
507 const amd_kernel_code_t
*code_object
,
508 const struct pipe_grid_info
*info
,
509 uint64_t kernel_args_va
)
511 struct si_compute
*program
= sctx
->cs_shader_state
.program
;
512 struct radeon_winsys_cs
*cs
= sctx
->b
.gfx
.cs
;
514 static const enum amd_code_property_mask_t workgroup_count_masks
[] = {
515 AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_X
,
516 AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Y
,
517 AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Z
520 unsigned i
, user_sgpr
= 0;
521 if (AMD_HSA_BITS_GET(code_object
->code_properties
,
522 AMD_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER
)) {
523 if (code_object
->workitem_private_segment_byte_size
> 0) {
524 setup_scratch_rsrc_user_sgprs(sctx
, code_object
,
530 if (AMD_HSA_BITS_GET(code_object
->code_properties
,
531 AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR
)) {
532 struct dispatch_packet dispatch
;
533 unsigned dispatch_offset
;
534 struct r600_resource
*dispatch_buf
= NULL
;
535 uint64_t dispatch_va
;
537 /* Upload dispatch ptr */
538 memset(&dispatch
, 0, sizeof(dispatch
));
540 dispatch
.workgroup_size_x
= info
->block
[0];
541 dispatch
.workgroup_size_y
= info
->block
[1];
542 dispatch
.workgroup_size_z
= info
->block
[2];
544 dispatch
.grid_size_x
= info
->grid
[0] * info
->block
[0];
545 dispatch
.grid_size_y
= info
->grid
[1] * info
->block
[1];
546 dispatch
.grid_size_z
= info
->grid
[2] * info
->block
[2];
548 dispatch
.private_segment_size
= program
->private_size
;
549 dispatch
.group_segment_size
= program
->local_size
;
551 dispatch
.kernarg_address
= kernel_args_va
;
553 u_upload_data(sctx
->b
.b
.const_uploader
, 0, sizeof(dispatch
),
554 256, &dispatch
, &dispatch_offset
,
555 (struct pipe_resource
**)&dispatch_buf
);
558 fprintf(stderr
, "Error: Failed to allocate dispatch "
561 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
, dispatch_buf
,
562 RADEON_USAGE_READ
, RADEON_PRIO_CONST_BUFFER
);
564 dispatch_va
= dispatch_buf
->gpu_address
+ dispatch_offset
;
566 radeon_set_sh_reg_seq(cs
, R_00B900_COMPUTE_USER_DATA_0
+
568 radeon_emit(cs
, dispatch_va
);
569 radeon_emit(cs
, S_008F04_BASE_ADDRESS_HI(dispatch_va
>> 32) |
572 r600_resource_reference(&dispatch_buf
, NULL
);
576 if (AMD_HSA_BITS_GET(code_object
->code_properties
,
577 AMD_CODE_PROPERTY_ENABLE_SGPR_KERNARG_SEGMENT_PTR
)) {
578 radeon_set_sh_reg_seq(cs
, R_00B900_COMPUTE_USER_DATA_0
+
580 radeon_emit(cs
, kernel_args_va
);
581 radeon_emit(cs
, S_008F04_BASE_ADDRESS_HI (kernel_args_va
>> 32) |
586 for (i
= 0; i
< 3 && user_sgpr
< 16; i
++) {
587 if (code_object
->code_properties
& workgroup_count_masks
[i
]) {
588 radeon_set_sh_reg_seq(cs
,
589 R_00B900_COMPUTE_USER_DATA_0
+
591 radeon_emit(cs
, info
->grid
[i
]);
597 static bool si_upload_compute_input(struct si_context
*sctx
,
598 const amd_kernel_code_t
*code_object
,
599 const struct pipe_grid_info
*info
)
601 struct radeon_winsys_cs
*cs
= sctx
->b
.gfx
.cs
;
602 struct si_compute
*program
= sctx
->cs_shader_state
.program
;
603 struct r600_resource
*input_buffer
= NULL
;
604 unsigned kernel_args_size
;
605 unsigned num_work_size_bytes
= program
->use_code_object_v2
? 0 : 36;
606 uint32_t kernel_args_offset
= 0;
607 uint32_t *kernel_args
;
608 void *kernel_args_ptr
;
609 uint64_t kernel_args_va
;
612 /* The extra num_work_size_bytes are for work group / work item size information */
613 kernel_args_size
= program
->input_size
+ num_work_size_bytes
;
615 u_upload_alloc(sctx
->b
.b
.const_uploader
, 0, kernel_args_size
,
616 sctx
->screen
->b
.info
.tcc_cache_line_size
,
618 (struct pipe_resource
**)&input_buffer
, &kernel_args_ptr
);
620 if (unlikely(!kernel_args_ptr
))
623 kernel_args
= (uint32_t*)kernel_args_ptr
;
624 kernel_args_va
= input_buffer
->gpu_address
+ kernel_args_offset
;
627 for (i
= 0; i
< 3; i
++) {
628 kernel_args
[i
] = info
->grid
[i
];
629 kernel_args
[i
+ 3] = info
->grid
[i
] * info
->block
[i
];
630 kernel_args
[i
+ 6] = info
->block
[i
];
634 memcpy(kernel_args
+ (num_work_size_bytes
/ 4), info
->input
,
635 program
->input_size
);
638 for (i
= 0; i
< (kernel_args_size
/ 4); i
++) {
639 COMPUTE_DBG(sctx
->screen
, "input %u : %u\n", i
,
644 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
, input_buffer
,
645 RADEON_USAGE_READ
, RADEON_PRIO_CONST_BUFFER
);
648 si_setup_user_sgprs_co_v2(sctx
, code_object
, info
, kernel_args_va
);
650 radeon_set_sh_reg_seq(cs
, R_00B900_COMPUTE_USER_DATA_0
, 2);
651 radeon_emit(cs
, kernel_args_va
);
652 radeon_emit(cs
, S_008F04_BASE_ADDRESS_HI (kernel_args_va
>> 32) |
656 r600_resource_reference(&input_buffer
, NULL
);
661 static void si_setup_tgsi_grid(struct si_context
*sctx
,
662 const struct pipe_grid_info
*info
)
664 struct si_compute
*program
= sctx
->cs_shader_state
.program
;
665 struct radeon_winsys_cs
*cs
= sctx
->b
.gfx
.cs
;
666 unsigned grid_size_reg
= R_00B900_COMPUTE_USER_DATA_0
+
667 4 * SI_NUM_RESOURCE_SGPRS
;
668 unsigned block_size_reg
= grid_size_reg
+
669 /* 12 bytes = 3 dwords. */
670 12 * program
->uses_grid_size
;
672 if (info
->indirect
) {
673 if (program
->uses_grid_size
) {
674 uint64_t base_va
= r600_resource(info
->indirect
)->gpu_address
;
675 uint64_t va
= base_va
+ info
->indirect_offset
;
678 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
679 (struct r600_resource
*)info
->indirect
,
680 RADEON_USAGE_READ
, RADEON_PRIO_DRAW_INDIRECT
);
682 for (i
= 0; i
< 3; ++i
) {
683 radeon_emit(cs
, PKT3(PKT3_COPY_DATA
, 4, 0));
684 radeon_emit(cs
, COPY_DATA_SRC_SEL(COPY_DATA_MEM
) |
685 COPY_DATA_DST_SEL(COPY_DATA_REG
));
686 radeon_emit(cs
, (va
+ 4 * i
));
687 radeon_emit(cs
, (va
+ 4 * i
) >> 32);
688 radeon_emit(cs
, (grid_size_reg
>> 2) + i
);
693 if (program
->uses_grid_size
) {
694 radeon_set_sh_reg_seq(cs
, grid_size_reg
, 3);
695 radeon_emit(cs
, info
->grid
[0]);
696 radeon_emit(cs
, info
->grid
[1]);
697 radeon_emit(cs
, info
->grid
[2]);
699 if (program
->variable_group_size
&& program
->uses_block_size
) {
700 radeon_set_sh_reg_seq(cs
, block_size_reg
, 3);
701 radeon_emit(cs
, info
->block
[0]);
702 radeon_emit(cs
, info
->block
[1]);
703 radeon_emit(cs
, info
->block
[2]);
708 static void si_emit_dispatch_packets(struct si_context
*sctx
,
709 const struct pipe_grid_info
*info
)
711 struct radeon_winsys_cs
*cs
= sctx
->b
.gfx
.cs
;
712 bool render_cond_bit
= sctx
->b
.render_cond
&& !sctx
->b
.render_cond_force_off
;
713 unsigned waves_per_threadgroup
=
714 DIV_ROUND_UP(info
->block
[0] * info
->block
[1] * info
->block
[2], 64);
716 radeon_set_sh_reg(cs
, R_00B854_COMPUTE_RESOURCE_LIMITS
,
717 S_00B854_SIMD_DEST_CNTL(waves_per_threadgroup
% 4 == 0));
719 radeon_set_sh_reg_seq(cs
, R_00B81C_COMPUTE_NUM_THREAD_X
, 3);
720 radeon_emit(cs
, S_00B81C_NUM_THREAD_FULL(info
->block
[0]));
721 radeon_emit(cs
, S_00B820_NUM_THREAD_FULL(info
->block
[1]));
722 radeon_emit(cs
, S_00B824_NUM_THREAD_FULL(info
->block
[2]));
724 if (info
->indirect
) {
725 uint64_t base_va
= r600_resource(info
->indirect
)->gpu_address
;
727 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
728 (struct r600_resource
*)info
->indirect
,
729 RADEON_USAGE_READ
, RADEON_PRIO_DRAW_INDIRECT
);
731 radeon_emit(cs
, PKT3(PKT3_SET_BASE
, 2, 0) |
732 PKT3_SHADER_TYPE_S(1));
734 radeon_emit(cs
, base_va
);
735 radeon_emit(cs
, base_va
>> 32);
737 radeon_emit(cs
, PKT3(PKT3_DISPATCH_INDIRECT
, 1, render_cond_bit
) |
738 PKT3_SHADER_TYPE_S(1));
739 radeon_emit(cs
, info
->indirect_offset
);
742 radeon_emit(cs
, PKT3(PKT3_DISPATCH_DIRECT
, 3, render_cond_bit
) |
743 PKT3_SHADER_TYPE_S(1));
744 radeon_emit(cs
, info
->grid
[0]);
745 radeon_emit(cs
, info
->grid
[1]);
746 radeon_emit(cs
, info
->grid
[2]);
752 static void si_launch_grid(
753 struct pipe_context
*ctx
, const struct pipe_grid_info
*info
)
755 struct si_context
*sctx
= (struct si_context
*)ctx
;
756 struct si_compute
*program
= sctx
->cs_shader_state
.program
;
757 const amd_kernel_code_t
*code_object
=
758 si_compute_get_code_object(program
, info
->pc
);
760 /* HW bug workaround when CS threadgroups > 256 threads and async
761 * compute isn't used, i.e. only one compute job can run at a time.
762 * If async compute is possible, the threadgroup size must be limited
763 * to 256 threads on all queues to avoid the bug.
764 * Only SI and certain CIK chips are affected.
766 bool cs_regalloc_hang
=
767 (sctx
->b
.chip_class
== SI
||
768 sctx
->b
.family
== CHIP_BONAIRE
||
769 sctx
->b
.family
== CHIP_KABINI
) &&
770 info
->block
[0] * info
->block
[1] * info
->block
[2] > 256;
772 if (cs_regalloc_hang
)
773 sctx
->b
.flags
|= SI_CONTEXT_PS_PARTIAL_FLUSH
|
774 SI_CONTEXT_CS_PARTIAL_FLUSH
;
776 if (program
->ir_type
== PIPE_SHADER_IR_TGSI
) {
777 util_queue_fence_wait(&program
->ready
);
779 if (program
->shader
.compilation_failed
)
783 si_decompress_compute_textures(sctx
);
785 /* Add buffer sizes for memory checking in need_cs_space. */
786 r600_context_add_resource_size(ctx
, &program
->shader
.bo
->b
.b
);
787 /* TODO: add the scratch buffer */
789 if (info
->indirect
) {
790 r600_context_add_resource_size(ctx
, info
->indirect
);
792 /* The hw doesn't read the indirect buffer via TC L2. */
793 if (r600_resource(info
->indirect
)->TC_L2_dirty
) {
794 sctx
->b
.flags
|= SI_CONTEXT_WRITEBACK_GLOBAL_L2
;
795 r600_resource(info
->indirect
)->TC_L2_dirty
= false;
799 si_need_cs_space(sctx
);
801 if (!sctx
->cs_shader_state
.initialized
)
802 si_initialize_compute(sctx
);
805 si_emit_cache_flush(sctx
);
807 if (!si_switch_compute_shader(sctx
, program
, &program
->shader
,
808 code_object
, info
->pc
))
811 si_upload_compute_shader_descriptors(sctx
);
812 si_emit_compute_shader_userdata(sctx
);
814 if (si_is_atom_dirty(sctx
, sctx
->atoms
.s
.render_cond
)) {
815 sctx
->atoms
.s
.render_cond
->emit(&sctx
->b
,
816 sctx
->atoms
.s
.render_cond
);
817 si_set_atom_dirty(sctx
, sctx
->atoms
.s
.render_cond
, false);
820 if ((program
->input_size
||
821 program
->ir_type
== PIPE_SHADER_IR_NATIVE
) &&
822 unlikely(!si_upload_compute_input(sctx
, code_object
, info
))) {
827 for (i
= 0; i
< MAX_GLOBAL_BUFFERS
; i
++) {
828 struct r600_resource
*buffer
=
829 (struct r600_resource
*)program
->global_buffers
[i
];
833 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
, buffer
,
834 RADEON_USAGE_READWRITE
,
835 RADEON_PRIO_COMPUTE_GLOBAL
);
838 if (program
->ir_type
== PIPE_SHADER_IR_TGSI
)
839 si_setup_tgsi_grid(sctx
, info
);
841 si_ce_pre_draw_synchronization(sctx
);
843 si_emit_dispatch_packets(sctx
, info
);
845 si_ce_post_draw_synchronization(sctx
);
847 sctx
->compute_is_busy
= true;
848 sctx
->b
.num_compute_calls
++;
849 if (sctx
->cs_shader_state
.uses_scratch
)
850 sctx
->b
.num_spill_compute_calls
++;
852 if (cs_regalloc_hang
)
853 sctx
->b
.flags
|= SI_CONTEXT_CS_PARTIAL_FLUSH
;
857 static void si_delete_compute_state(struct pipe_context
*ctx
, void* state
){
858 struct si_compute
*program
= (struct si_compute
*)state
;
859 struct si_context
*sctx
= (struct si_context
*)ctx
;
865 if (program
->ir_type
== PIPE_SHADER_IR_TGSI
) {
866 util_queue_fence_wait(&program
->ready
);
867 util_queue_fence_destroy(&program
->ready
);
870 if (program
== sctx
->cs_shader_state
.program
)
871 sctx
->cs_shader_state
.program
= NULL
;
873 if (program
== sctx
->cs_shader_state
.emitted_program
)
874 sctx
->cs_shader_state
.emitted_program
= NULL
;
876 si_shader_destroy(&program
->shader
);
880 static void si_set_compute_resources(struct pipe_context
* ctx_
,
881 unsigned start
, unsigned count
,
882 struct pipe_surface
** surfaces
) { }
884 void si_init_compute_functions(struct si_context
*sctx
)
886 sctx
->b
.b
.create_compute_state
= si_create_compute_state
;
887 sctx
->b
.b
.delete_compute_state
= si_delete_compute_state
;
888 sctx
->b
.b
.bind_compute_state
= si_bind_compute_state
;
889 /* ctx->context.create_sampler_view = evergreen_compute_create_sampler_view; */
890 sctx
->b
.b
.set_compute_resources
= si_set_compute_resources
;
891 sctx
->b
.b
.set_global_binding
= si_set_global_binding
;
892 sctx
->b
.b
.launch_grid
= si_launch_grid
;