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"
28 #include "radeon/r600_pipe_common.h"
29 #include "radeon/radeon_elf_util.h"
31 #include "amd_kernel_code_t.h"
32 #include "radeon/r600_cs.h"
34 #include "si_shader.h"
37 #define MAX_GLOBAL_BUFFERS 20
42 unsigned private_size
;
44 struct si_shader shader
;
46 struct pipe_resource
*global_buffers
[MAX_GLOBAL_BUFFERS
];
47 bool use_code_object_v2
;
50 struct dispatch_packet
{
53 uint16_t workgroup_size_x
;
54 uint16_t workgroup_size_y
;
55 uint16_t workgroup_size_z
;
60 uint32_t private_segment_size
;
61 uint32_t group_segment_size
;
62 uint64_t kernel_object
;
63 uint64_t kernarg_address
;
67 static const amd_kernel_code_t
*si_compute_get_code_object(
68 const struct si_compute
*program
,
69 uint64_t symbol_offset
)
71 if (!program
->use_code_object_v2
) {
74 return (const amd_kernel_code_t
*)
75 (program
->shader
.binary
.code
+ symbol_offset
);
78 static void code_object_to_config(const amd_kernel_code_t
*code_object
,
79 struct si_shader_config
*out_config
) {
81 uint32_t rsrc1
= code_object
->compute_pgm_resource_registers
;
82 uint32_t rsrc2
= code_object
->compute_pgm_resource_registers
>> 32;
83 out_config
->num_sgprs
= code_object
->wavefront_sgpr_count
;
84 out_config
->num_vgprs
= code_object
->workitem_vgpr_count
;
85 out_config
->float_mode
= G_00B028_FLOAT_MODE(rsrc1
);
86 out_config
->rsrc1
= rsrc1
;
87 out_config
->lds_size
= MAX2(out_config
->lds_size
, G_00B84C_LDS_SIZE(rsrc2
));
88 out_config
->rsrc2
= rsrc2
;
89 out_config
->scratch_bytes_per_wave
=
90 align(code_object
->workitem_private_segment_byte_size
* 64, 1024);
93 static void *si_create_compute_state(
94 struct pipe_context
*ctx
,
95 const struct pipe_compute_state
*cso
)
97 struct si_context
*sctx
= (struct si_context
*)ctx
;
98 struct si_screen
*sscreen
= (struct si_screen
*)ctx
->screen
;
99 struct si_compute
*program
= CALLOC_STRUCT(si_compute
);
100 struct si_shader
*shader
= &program
->shader
;
103 program
->ir_type
= cso
->ir_type
;
104 program
->local_size
= cso
->req_local_mem
;
105 program
->private_size
= cso
->req_private_mem
;
106 program
->input_size
= cso
->req_input_mem
;
107 program
->use_code_object_v2
= HAVE_LLVM
>= 0x0400 &&
108 cso
->ir_type
== PIPE_SHADER_IR_NATIVE
;
111 if (cso
->ir_type
== PIPE_SHADER_IR_TGSI
) {
112 struct si_shader_selector sel
;
113 bool scratch_enabled
;
115 memset(&sel
, 0, sizeof(sel
));
117 sel
.tokens
= tgsi_dup_tokens(cso
->prog
);
123 tgsi_scan_shader(cso
->prog
, &sel
.info
);
124 sel
.type
= PIPE_SHADER_COMPUTE
;
125 sel
.local_size
= cso
->req_local_mem
;
127 p_atomic_inc(&sscreen
->b
.num_shaders_created
);
129 program
->shader
.selector
= &sel
;
131 if (si_shader_create(sscreen
, sctx
->tm
, &program
->shader
,
138 scratch_enabled
= shader
->config
.scratch_bytes_per_wave
> 0;
140 shader
->config
.rsrc1
=
141 S_00B848_VGPRS((shader
->config
.num_vgprs
- 1) / 4) |
142 S_00B848_SGPRS((shader
->config
.num_sgprs
- 1) / 8) |
143 S_00B848_DX10_CLAMP(1) |
144 S_00B848_FLOAT_MODE(shader
->config
.float_mode
);
146 shader
->config
.rsrc2
= S_00B84C_USER_SGPR(SI_CS_NUM_USER_SGPR
) |
147 S_00B84C_SCRATCH_EN(scratch_enabled
) |
148 S_00B84C_TGID_X_EN(1) | S_00B84C_TGID_Y_EN(1) |
149 S_00B84C_TGID_Z_EN(1) | S_00B84C_TIDIG_COMP_CNT(2) |
150 S_00B84C_LDS_SIZE(shader
->config
.lds_size
);
154 const struct pipe_llvm_program_header
*header
;
157 code
= cso
->prog
+ sizeof(struct pipe_llvm_program_header
);
159 radeon_elf_read(code
, header
->num_bytes
, &program
->shader
.binary
);
160 if (program
->use_code_object_v2
) {
161 const amd_kernel_code_t
*code_object
=
162 si_compute_get_code_object(program
, 0);
163 code_object_to_config(code_object
, &program
->shader
.config
);
165 si_shader_binary_read_config(&program
->shader
.binary
,
166 &program
->shader
.config
, 0);
168 si_shader_dump(sctx
->screen
, &program
->shader
, &sctx
->b
.debug
,
169 PIPE_SHADER_COMPUTE
, stderr
);
170 si_shader_binary_upload(sctx
->screen
, &program
->shader
);
176 static void si_bind_compute_state(struct pipe_context
*ctx
, void *state
)
178 struct si_context
*sctx
= (struct si_context
*)ctx
;
179 sctx
->cs_shader_state
.program
= (struct si_compute
*)state
;
182 static void si_set_global_binding(
183 struct pipe_context
*ctx
, unsigned first
, unsigned n
,
184 struct pipe_resource
**resources
,
188 struct si_context
*sctx
= (struct si_context
*)ctx
;
189 struct si_compute
*program
= sctx
->cs_shader_state
.program
;
192 for (i
= first
; i
< first
+ n
; i
++) {
193 pipe_resource_reference(&program
->global_buffers
[i
], NULL
);
198 for (i
= first
; i
< first
+ n
; i
++) {
201 pipe_resource_reference(&program
->global_buffers
[i
], resources
[i
]);
202 va
= r600_resource(resources
[i
])->gpu_address
;
203 offset
= util_le32_to_cpu(*handles
[i
]);
205 va
= util_cpu_to_le64(va
);
206 memcpy(handles
[i
], &va
, sizeof(va
));
210 static void si_initialize_compute(struct si_context
*sctx
)
212 struct radeon_winsys_cs
*cs
= sctx
->b
.gfx
.cs
;
214 radeon_set_sh_reg_seq(cs
, R_00B810_COMPUTE_START_X
, 3);
219 radeon_set_sh_reg_seq(cs
, R_00B858_COMPUTE_STATIC_THREAD_MGMT_SE0
, 2);
220 /* R_00B858_COMPUTE_STATIC_THREAD_MGMT_SE0 / SE1 */
221 radeon_emit(cs
, S_00B858_SH0_CU_EN(0xffff) | S_00B858_SH1_CU_EN(0xffff));
222 radeon_emit(cs
, S_00B85C_SH0_CU_EN(0xffff) | S_00B85C_SH1_CU_EN(0xffff));
224 if (sctx
->b
.chip_class
>= CIK
) {
225 /* Also set R_00B858_COMPUTE_STATIC_THREAD_MGMT_SE2 / SE3 */
226 radeon_set_sh_reg_seq(cs
,
227 R_00B864_COMPUTE_STATIC_THREAD_MGMT_SE2
, 2);
228 radeon_emit(cs
, S_00B864_SH0_CU_EN(0xffff) |
229 S_00B864_SH1_CU_EN(0xffff));
230 radeon_emit(cs
, S_00B868_SH0_CU_EN(0xffff) |
231 S_00B868_SH1_CU_EN(0xffff));
234 /* This register has been moved to R_00CD20_COMPUTE_MAX_WAVE_ID
235 * and is now per pipe, so it should be handled in the
236 * kernel if we want to use something other than the default value,
237 * which is now 0x22f.
239 if (sctx
->b
.chip_class
<= SI
) {
240 /* XXX: This should be:
241 * (number of compute units) * 4 * (waves per simd) - 1 */
243 radeon_set_sh_reg(cs
, R_00B82C_COMPUTE_MAX_WAVE_ID
,
244 0x190 /* Default value */);
247 sctx
->cs_shader_state
.emitted_program
= NULL
;
248 sctx
->cs_shader_state
.initialized
= true;
251 static bool si_setup_compute_scratch_buffer(struct si_context
*sctx
,
252 struct si_shader
*shader
,
253 struct si_shader_config
*config
)
255 uint64_t scratch_bo_size
, scratch_needed
;
257 scratch_needed
= config
->scratch_bytes_per_wave
* sctx
->scratch_waves
;
258 if (sctx
->compute_scratch_buffer
)
259 scratch_bo_size
= sctx
->compute_scratch_buffer
->b
.b
.width0
;
261 if (scratch_bo_size
< scratch_needed
) {
262 r600_resource_reference(&sctx
->compute_scratch_buffer
, NULL
);
264 sctx
->compute_scratch_buffer
=
265 si_resource_create_custom(&sctx
->screen
->b
.b
,
266 PIPE_USAGE_DEFAULT
, scratch_needed
);
268 if (!sctx
->compute_scratch_buffer
)
272 if (sctx
->compute_scratch_buffer
!= shader
->scratch_bo
&& scratch_needed
) {
273 uint64_t scratch_va
= sctx
->compute_scratch_buffer
->gpu_address
;
275 si_shader_apply_scratch_relocs(sctx
, shader
, config
, scratch_va
);
277 if (si_shader_binary_upload(sctx
->screen
, shader
))
280 r600_resource_reference(&shader
->scratch_bo
,
281 sctx
->compute_scratch_buffer
);
287 static bool si_switch_compute_shader(struct si_context
*sctx
,
288 struct si_compute
*program
,
289 struct si_shader
*shader
,
290 const amd_kernel_code_t
*code_object
,
293 struct radeon_winsys_cs
*cs
= sctx
->b
.gfx
.cs
;
294 struct si_shader_config inline_config
= {0};
295 struct si_shader_config
*config
;
298 if (sctx
->cs_shader_state
.emitted_program
== program
&&
299 sctx
->cs_shader_state
.offset
== offset
)
302 if (program
->ir_type
== PIPE_SHADER_IR_TGSI
) {
303 config
= &shader
->config
;
307 config
= &inline_config
;
309 code_object_to_config(code_object
, config
);
311 si_shader_binary_read_config(&shader
->binary
, config
, offset
);
314 lds_blocks
= config
->lds_size
;
315 /* XXX: We are over allocating LDS. For SI, the shader reports
316 * LDS in blocks of 256 bytes, so if there are 4 bytes lds
317 * allocated in the shader and 4 bytes allocated by the state
318 * tracker, then we will set LDS_SIZE to 512 bytes rather than 256.
320 if (sctx
->b
.chip_class
<= SI
) {
321 lds_blocks
+= align(program
->local_size
, 256) >> 8;
323 lds_blocks
+= align(program
->local_size
, 512) >> 9;
326 assert(lds_blocks
<= 0xFF);
328 config
->rsrc2
&= C_00B84C_LDS_SIZE
;
329 config
->rsrc2
|= S_00B84C_LDS_SIZE(lds_blocks
);
332 if (!si_setup_compute_scratch_buffer(sctx
, shader
, config
))
335 if (shader
->scratch_bo
) {
336 COMPUTE_DBG(sctx
->screen
, "Waves: %u; Scratch per wave: %u bytes; "
337 "Total Scratch: %u bytes\n", sctx
->scratch_waves
,
338 config
->scratch_bytes_per_wave
,
339 config
->scratch_bytes_per_wave
*
340 sctx
->scratch_waves
);
342 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
343 shader
->scratch_bo
, RADEON_USAGE_READWRITE
,
344 RADEON_PRIO_SCRATCH_BUFFER
);
347 shader_va
= shader
->bo
->gpu_address
+ offset
;
348 if (program
->use_code_object_v2
) {
349 /* Shader code is placed after the amd_kernel_code_t
351 shader_va
+= sizeof(amd_kernel_code_t
);
354 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
, shader
->bo
,
355 RADEON_USAGE_READ
, RADEON_PRIO_SHADER_BINARY
);
357 radeon_set_sh_reg_seq(cs
, R_00B830_COMPUTE_PGM_LO
, 2);
358 radeon_emit(cs
, shader_va
>> 8);
359 radeon_emit(cs
, shader_va
>> 40);
361 radeon_set_sh_reg_seq(cs
, R_00B848_COMPUTE_PGM_RSRC1
, 2);
362 radeon_emit(cs
, config
->rsrc1
);
363 radeon_emit(cs
, config
->rsrc2
);
365 COMPUTE_DBG(sctx
->screen
, "COMPUTE_PGM_RSRC1: 0x%08x "
366 "COMPUTE_PGM_RSRC2: 0x%08x\n", config
->rsrc1
, config
->rsrc2
);
368 radeon_set_sh_reg(cs
, R_00B860_COMPUTE_TMPRING_SIZE
,
369 S_00B860_WAVES(sctx
->scratch_waves
)
370 | S_00B860_WAVESIZE(config
->scratch_bytes_per_wave
>> 10));
372 sctx
->cs_shader_state
.emitted_program
= program
;
373 sctx
->cs_shader_state
.offset
= offset
;
374 sctx
->cs_shader_state
.uses_scratch
=
375 config
->scratch_bytes_per_wave
!= 0;
380 static void setup_scratch_rsrc_user_sgprs(struct si_context
*sctx
,
381 const amd_kernel_code_t
*code_object
,
384 struct radeon_winsys_cs
*cs
= sctx
->b
.gfx
.cs
;
385 uint64_t scratch_va
= sctx
->compute_scratch_buffer
->gpu_address
;
387 unsigned max_private_element_size
= AMD_HSA_BITS_GET(
388 code_object
->code_properties
,
389 AMD_CODE_PROPERTY_PRIVATE_ELEMENT_SIZE
);
391 uint32_t scratch_dword0
= scratch_va
& 0xffffffff;
392 uint32_t scratch_dword1
=
393 S_008F04_BASE_ADDRESS_HI(scratch_va
>> 32) |
394 S_008F04_SWIZZLE_ENABLE(1);
396 /* Disable address clamping */
397 uint32_t scratch_dword2
= 0xffffffff;
398 uint32_t scratch_dword3
=
399 S_008F0C_ELEMENT_SIZE(max_private_element_size
) |
400 S_008F0C_INDEX_STRIDE(3) |
401 S_008F0C_ADD_TID_ENABLE(1);
404 if (sctx
->screen
->b
.chip_class
< VI
) {
405 /* BUF_DATA_FORMAT is ignored, but it cannot be
406 BUF_DATA_FORMAT_INVALID. */
408 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_8
);
411 radeon_set_sh_reg_seq(cs
, R_00B900_COMPUTE_USER_DATA_0
+
413 radeon_emit(cs
, scratch_dword0
);
414 radeon_emit(cs
, scratch_dword1
);
415 radeon_emit(cs
, scratch_dword2
);
416 radeon_emit(cs
, scratch_dword3
);
419 static void si_setup_user_sgprs_co_v2(struct si_context
*sctx
,
420 const amd_kernel_code_t
*code_object
,
421 const struct pipe_grid_info
*info
,
422 uint64_t kernel_args_va
)
424 struct si_compute
*program
= sctx
->cs_shader_state
.program
;
425 struct radeon_winsys_cs
*cs
= sctx
->b
.gfx
.cs
;
427 static const enum amd_code_property_mask_t workgroup_count_masks
[] = {
428 AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_X
,
429 AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Y
,
430 AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Z
433 unsigned i
, user_sgpr
= 0;
434 if (AMD_HSA_BITS_GET(code_object
->code_properties
,
435 AMD_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER
)) {
436 if (code_object
->workitem_private_segment_byte_size
> 0) {
437 setup_scratch_rsrc_user_sgprs(sctx
, code_object
,
443 if (AMD_HSA_BITS_GET(code_object
->code_properties
,
444 AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR
)) {
445 struct dispatch_packet dispatch
;
446 unsigned dispatch_offset
;
447 struct r600_resource
*dispatch_buf
= NULL
;
448 uint64_t dispatch_va
;
450 /* Upload dispatch ptr */
451 memset(&dispatch
, 0, sizeof(dispatch
));
453 dispatch
.workgroup_size_x
= info
->block
[0];
454 dispatch
.workgroup_size_y
= info
->block
[1];
455 dispatch
.workgroup_size_z
= info
->block
[2];
457 dispatch
.grid_size_x
= info
->grid
[0] * info
->block
[0];
458 dispatch
.grid_size_y
= info
->grid
[1] * info
->block
[1];
459 dispatch
.grid_size_z
= info
->grid
[2] * info
->block
[2];
461 dispatch
.private_segment_size
= program
->private_size
;
462 dispatch
.group_segment_size
= program
->local_size
;
464 dispatch
.kernarg_address
= kernel_args_va
;
466 u_upload_data(sctx
->b
.uploader
, 0, sizeof(dispatch
), 256,
467 &dispatch
, &dispatch_offset
,
468 (struct pipe_resource
**)&dispatch_buf
);
471 fprintf(stderr
, "Error: Failed to allocate dispatch "
474 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
, dispatch_buf
,
475 RADEON_USAGE_READ
, RADEON_PRIO_CONST_BUFFER
);
477 dispatch_va
= dispatch_buf
->gpu_address
+ dispatch_offset
;
479 radeon_set_sh_reg_seq(cs
, R_00B900_COMPUTE_USER_DATA_0
+
481 radeon_emit(cs
, dispatch_va
);
482 radeon_emit(cs
, S_008F04_BASE_ADDRESS_HI(dispatch_va
>> 32) |
485 r600_resource_reference(&dispatch_buf
, NULL
);
489 if (AMD_HSA_BITS_GET(code_object
->code_properties
,
490 AMD_CODE_PROPERTY_ENABLE_SGPR_KERNARG_SEGMENT_PTR
)) {
491 radeon_set_sh_reg_seq(cs
, R_00B900_COMPUTE_USER_DATA_0
+
493 radeon_emit(cs
, kernel_args_va
);
494 radeon_emit(cs
, S_008F04_BASE_ADDRESS_HI (kernel_args_va
>> 32) |
499 for (i
= 0; i
< 3 && user_sgpr
< 16; i
++) {
500 if (code_object
->code_properties
& workgroup_count_masks
[i
]) {
501 radeon_set_sh_reg_seq(cs
,
502 R_00B900_COMPUTE_USER_DATA_0
+
504 radeon_emit(cs
, info
->grid
[i
]);
510 static void si_upload_compute_input(struct si_context
*sctx
,
511 const amd_kernel_code_t
*code_object
,
512 const struct pipe_grid_info
*info
)
514 struct radeon_winsys_cs
*cs
= sctx
->b
.gfx
.cs
;
515 struct si_compute
*program
= sctx
->cs_shader_state
.program
;
516 struct r600_resource
*input_buffer
= NULL
;
517 unsigned kernel_args_size
;
518 unsigned num_work_size_bytes
= program
->use_code_object_v2
? 0 : 36;
519 uint32_t kernel_args_offset
= 0;
520 uint32_t *kernel_args
;
521 void *kernel_args_ptr
;
522 uint64_t kernel_args_va
;
525 /* The extra num_work_size_bytes are for work group / work item size information */
526 kernel_args_size
= program
->input_size
+ num_work_size_bytes
;
528 u_upload_alloc(sctx
->b
.uploader
, 0, kernel_args_size
, 256,
530 (struct pipe_resource
**)&input_buffer
, &kernel_args_ptr
);
532 kernel_args
= (uint32_t*)kernel_args_ptr
;
533 kernel_args_va
= input_buffer
->gpu_address
+ kernel_args_offset
;
536 for (i
= 0; i
< 3; i
++) {
537 kernel_args
[i
] = info
->grid
[i
];
538 kernel_args
[i
+ 3] = info
->grid
[i
] * info
->block
[i
];
539 kernel_args
[i
+ 6] = info
->block
[i
];
543 memcpy(kernel_args
+ (num_work_size_bytes
/ 4), info
->input
,
544 program
->input_size
);
547 for (i
= 0; i
< (kernel_args_size
/ 4); i
++) {
548 COMPUTE_DBG(sctx
->screen
, "input %u : %u\n", i
,
553 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
, input_buffer
,
554 RADEON_USAGE_READ
, RADEON_PRIO_CONST_BUFFER
);
557 si_setup_user_sgprs_co_v2(sctx
, code_object
, info
, kernel_args_va
);
559 radeon_set_sh_reg_seq(cs
, R_00B900_COMPUTE_USER_DATA_0
, 2);
560 radeon_emit(cs
, kernel_args_va
);
561 radeon_emit(cs
, S_008F04_BASE_ADDRESS_HI (kernel_args_va
>> 32) |
565 r600_resource_reference(&input_buffer
, NULL
);
568 static void si_setup_tgsi_grid(struct si_context
*sctx
,
569 const struct pipe_grid_info
*info
)
571 struct radeon_winsys_cs
*cs
= sctx
->b
.gfx
.cs
;
572 unsigned grid_size_reg
= R_00B900_COMPUTE_USER_DATA_0
+
573 4 * SI_SGPR_GRID_SIZE
;
575 if (info
->indirect
) {
576 uint64_t base_va
= r600_resource(info
->indirect
)->gpu_address
;
577 uint64_t va
= base_va
+ info
->indirect_offset
;
580 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
581 (struct r600_resource
*)info
->indirect
,
582 RADEON_USAGE_READ
, RADEON_PRIO_DRAW_INDIRECT
);
584 for (i
= 0; i
< 3; ++i
) {
585 radeon_emit(cs
, PKT3(PKT3_COPY_DATA
, 4, 0));
586 radeon_emit(cs
, COPY_DATA_SRC_SEL(COPY_DATA_MEM
) |
587 COPY_DATA_DST_SEL(COPY_DATA_REG
));
588 radeon_emit(cs
, (va
+ 4 * i
));
589 radeon_emit(cs
, (va
+ 4 * i
) >> 32);
590 radeon_emit(cs
, (grid_size_reg
>> 2) + i
);
595 radeon_set_sh_reg_seq(cs
, grid_size_reg
, 3);
596 radeon_emit(cs
, info
->grid
[0]);
597 radeon_emit(cs
, info
->grid
[1]);
598 radeon_emit(cs
, info
->grid
[2]);
602 static void si_emit_dispatch_packets(struct si_context
*sctx
,
603 const struct pipe_grid_info
*info
)
605 struct radeon_winsys_cs
*cs
= sctx
->b
.gfx
.cs
;
606 bool render_cond_bit
= sctx
->b
.render_cond
&& !sctx
->b
.render_cond_force_off
;
607 unsigned waves_per_threadgroup
=
608 DIV_ROUND_UP(info
->block
[0] * info
->block
[1] * info
->block
[2], 64);
610 radeon_set_sh_reg(cs
, R_00B854_COMPUTE_RESOURCE_LIMITS
,
611 S_00B854_SIMD_DEST_CNTL(waves_per_threadgroup
% 4 == 0));
613 radeon_set_sh_reg_seq(cs
, R_00B81C_COMPUTE_NUM_THREAD_X
, 3);
614 radeon_emit(cs
, S_00B81C_NUM_THREAD_FULL(info
->block
[0]));
615 radeon_emit(cs
, S_00B820_NUM_THREAD_FULL(info
->block
[1]));
616 radeon_emit(cs
, S_00B824_NUM_THREAD_FULL(info
->block
[2]));
618 if (info
->indirect
) {
619 uint64_t base_va
= r600_resource(info
->indirect
)->gpu_address
;
621 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
622 (struct r600_resource
*)info
->indirect
,
623 RADEON_USAGE_READ
, RADEON_PRIO_DRAW_INDIRECT
);
625 radeon_emit(cs
, PKT3(PKT3_SET_BASE
, 2, 0) |
626 PKT3_SHADER_TYPE_S(1));
628 radeon_emit(cs
, base_va
);
629 radeon_emit(cs
, base_va
>> 32);
631 radeon_emit(cs
, PKT3(PKT3_DISPATCH_INDIRECT
, 1, render_cond_bit
) |
632 PKT3_SHADER_TYPE_S(1));
633 radeon_emit(cs
, info
->indirect_offset
);
636 radeon_emit(cs
, PKT3(PKT3_DISPATCH_DIRECT
, 3, render_cond_bit
) |
637 PKT3_SHADER_TYPE_S(1));
638 radeon_emit(cs
, info
->grid
[0]);
639 radeon_emit(cs
, info
->grid
[1]);
640 radeon_emit(cs
, info
->grid
[2]);
646 static void si_launch_grid(
647 struct pipe_context
*ctx
, const struct pipe_grid_info
*info
)
649 struct si_context
*sctx
= (struct si_context
*)ctx
;
650 struct si_compute
*program
= sctx
->cs_shader_state
.program
;
651 const amd_kernel_code_t
*code_object
=
652 si_compute_get_code_object(program
, info
->pc
);
654 /* HW bug workaround when CS threadgroups > 256 threads and async
655 * compute isn't used, i.e. only one compute job can run at a time.
656 * If async compute is possible, the threadgroup size must be limited
657 * to 256 threads on all queues to avoid the bug.
658 * Only SI and certain CIK chips are affected.
660 bool cs_regalloc_hang
=
661 (sctx
->b
.chip_class
== SI
||
662 sctx
->b
.family
== CHIP_BONAIRE
||
663 sctx
->b
.family
== CHIP_KABINI
) &&
664 info
->block
[0] * info
->block
[1] * info
->block
[2] > 256;
666 if (cs_regalloc_hang
)
667 sctx
->b
.flags
|= SI_CONTEXT_PS_PARTIAL_FLUSH
|
668 SI_CONTEXT_CS_PARTIAL_FLUSH
;
670 si_decompress_compute_textures(sctx
);
672 /* Add buffer sizes for memory checking in need_cs_space. */
673 r600_context_add_resource_size(ctx
, &program
->shader
.bo
->b
.b
);
674 /* TODO: add the scratch buffer */
676 if (info
->indirect
) {
677 r600_context_add_resource_size(ctx
, info
->indirect
);
679 /* The hw doesn't read the indirect buffer via TC L2. */
680 if (r600_resource(info
->indirect
)->TC_L2_dirty
) {
681 sctx
->b
.flags
|= SI_CONTEXT_INV_GLOBAL_L2
;
682 r600_resource(info
->indirect
)->TC_L2_dirty
= false;
686 si_need_cs_space(sctx
);
688 if (!sctx
->cs_shader_state
.initialized
)
689 si_initialize_compute(sctx
);
692 si_emit_cache_flush(sctx
);
694 if (!si_switch_compute_shader(sctx
, program
, &program
->shader
,
695 code_object
, info
->pc
))
698 si_upload_compute_shader_descriptors(sctx
);
699 si_emit_compute_shader_userdata(sctx
);
701 if (si_is_atom_dirty(sctx
, sctx
->atoms
.s
.render_cond
)) {
702 sctx
->atoms
.s
.render_cond
->emit(&sctx
->b
,
703 sctx
->atoms
.s
.render_cond
);
704 si_set_atom_dirty(sctx
, sctx
->atoms
.s
.render_cond
, false);
707 if (program
->input_size
|| program
->ir_type
== PIPE_SHADER_IR_NATIVE
)
708 si_upload_compute_input(sctx
, code_object
, info
);
711 for (i
= 0; i
< MAX_GLOBAL_BUFFERS
; i
++) {
712 struct r600_resource
*buffer
=
713 (struct r600_resource
*)program
->global_buffers
[i
];
717 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
, buffer
,
718 RADEON_USAGE_READWRITE
,
719 RADEON_PRIO_COMPUTE_GLOBAL
);
722 if (program
->ir_type
== PIPE_SHADER_IR_TGSI
)
723 si_setup_tgsi_grid(sctx
, info
);
725 si_ce_pre_draw_synchronization(sctx
);
727 si_emit_dispatch_packets(sctx
, info
);
729 si_ce_post_draw_synchronization(sctx
);
731 sctx
->compute_is_busy
= true;
732 sctx
->b
.num_compute_calls
++;
733 if (sctx
->cs_shader_state
.uses_scratch
)
734 sctx
->b
.num_spill_compute_calls
++;
736 if (cs_regalloc_hang
)
737 sctx
->b
.flags
|= SI_CONTEXT_CS_PARTIAL_FLUSH
;
741 static void si_delete_compute_state(struct pipe_context
*ctx
, void* state
){
742 struct si_compute
*program
= (struct si_compute
*)state
;
743 struct si_context
*sctx
= (struct si_context
*)ctx
;
749 if (program
== sctx
->cs_shader_state
.program
)
750 sctx
->cs_shader_state
.program
= NULL
;
752 if (program
== sctx
->cs_shader_state
.emitted_program
)
753 sctx
->cs_shader_state
.emitted_program
= NULL
;
755 si_shader_destroy(&program
->shader
);
759 static void si_set_compute_resources(struct pipe_context
* ctx_
,
760 unsigned start
, unsigned count
,
761 struct pipe_surface
** surfaces
) { }
763 void si_init_compute_functions(struct si_context
*sctx
)
765 sctx
->b
.b
.create_compute_state
= si_create_compute_state
;
766 sctx
->b
.b
.delete_compute_state
= si_delete_compute_state
;
767 sctx
->b
.b
.bind_compute_state
= si_bind_compute_state
;
768 /* ctx->context.create_sampler_view = evergreen_compute_create_sampler_view; */
769 sctx
->b
.b
.set_compute_resources
= si_set_compute_resources
;
770 sctx
->b
.b
.set_global_binding
= si_set_global_binding
;
771 sctx
->b
.b
.launch_grid
= si_launch_grid
;