radeonsi: Refuse to accept code with unhandled relocations
[mesa.git] / src / gallium / drivers / radeonsi / si_compute.c
1 /*
2 * Copyright 2013 Advanced Micro Devices, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 */
25
26 #include "tgsi/tgsi_parse.h"
27 #include "util/u_async_debug.h"
28 #include "util/u_memory.h"
29 #include "util/u_upload_mgr.h"
30
31 #include "amd_kernel_code_t.h"
32 #include "si_build_pm4.h"
33 #include "si_compute.h"
34
35 #define COMPUTE_DBG(rscreen, fmt, args...) \
36 do { \
37 if ((rscreen->debug_flags & DBG(COMPUTE))) fprintf(stderr, fmt, ##args); \
38 } while (0);
39
40 struct dispatch_packet {
41 uint16_t header;
42 uint16_t setup;
43 uint16_t workgroup_size_x;
44 uint16_t workgroup_size_y;
45 uint16_t workgroup_size_z;
46 uint16_t reserved0;
47 uint32_t grid_size_x;
48 uint32_t grid_size_y;
49 uint32_t grid_size_z;
50 uint32_t private_segment_size;
51 uint32_t group_segment_size;
52 uint64_t kernel_object;
53 uint64_t kernarg_address;
54 uint64_t reserved2;
55 };
56
57 static const amd_kernel_code_t *si_compute_get_code_object(
58 const struct si_compute *program,
59 uint64_t symbol_offset)
60 {
61 if (!program->use_code_object_v2) {
62 return NULL;
63 }
64 return (const amd_kernel_code_t*)
65 (program->shader.binary.code + symbol_offset);
66 }
67
68 static void code_object_to_config(const amd_kernel_code_t *code_object,
69 struct si_shader_config *out_config) {
70
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);
81 }
82
83 /* Asynchronous compute shader compilation. */
84 static void si_create_compute_state_async(void *job, int thread_index)
85 {
86 struct si_compute *program = (struct si_compute *)job;
87 struct si_shader *shader = &program->shader;
88 struct si_shader_selector sel;
89 struct ac_llvm_compiler *compiler;
90 struct pipe_debug_callback *debug = &program->compiler_ctx_state.debug;
91 struct si_screen *sscreen = program->screen;
92
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];
97
98 memset(&sel, 0, sizeof(sel));
99
100 sel.screen = sscreen;
101
102 if (program->ir_type == PIPE_SHADER_IR_TGSI) {
103 tgsi_scan_shader(program->ir.tgsi, &sel.info);
104 sel.tokens = program->ir.tgsi;
105 } else {
106 assert(program->ir_type == PIPE_SHADER_IR_NIR);
107 sel.nir = program->ir.nir;
108
109 si_nir_scan_shader(sel.nir, &sel.info);
110 si_lower_nir(&sel);
111 }
112
113 /* Store the declared LDS size into tgsi_shader_info for the shader
114 * cache to include it.
115 */
116 sel.info.properties[TGSI_PROPERTY_CS_LOCAL_SIZE] = program->local_size;
117
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);
122
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;
131
132 void *ir_binary = si_get_ir_binary(&sel);
133
134 /* Try to load the shader from the shader cache. */
135 mtx_lock(&sscreen->shader_cache_mutex);
136
137 if (ir_binary &&
138 si_shader_cache_load_shader(sscreen, ir_binary, shader)) {
139 mtx_unlock(&sscreen->shader_cache_mutex);
140
141 si_shader_dump_stats_for_shader_db(shader, debug);
142 si_shader_dump(sscreen, shader, debug, PIPE_SHADER_COMPUTE,
143 stderr, true);
144
145 if (si_shader_binary_upload(sscreen, shader))
146 program->shader.compilation_failed = true;
147 } else {
148 mtx_unlock(&sscreen->shader_cache_mutex);
149
150 if (si_shader_create(sscreen, compiler, &program->shader, debug)) {
151 program->shader.compilation_failed = true;
152
153 if (program->ir_type == PIPE_SHADER_IR_TGSI)
154 FREE(program->ir.tgsi);
155 program->shader.selector = NULL;
156 return;
157 }
158
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);
163
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);
169
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);
179
180 if (ir_binary) {
181 mtx_lock(&sscreen->shader_cache_mutex);
182 if (!si_shader_cache_insert_shader(sscreen, ir_binary, shader, true))
183 FREE(ir_binary);
184 mtx_unlock(&sscreen->shader_cache_mutex);
185 }
186 }
187
188 if (program->ir_type == PIPE_SHADER_IR_TGSI)
189 FREE(program->ir.tgsi);
190
191 program->shader.selector = NULL;
192 }
193
194 static void *si_create_compute_state(
195 struct pipe_context *ctx,
196 const struct pipe_compute_state *cso)
197 {
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);
201
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;
209
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) {
214 FREE(program);
215 return NULL;
216 }
217 } else {
218 assert(cso->ir_type == PIPE_SHADER_IR_NIR);
219 program->ir.nir = (struct nir_shader *) cso->prog;
220 }
221
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);
225
226 si_schedule_initial_compile(sctx, PIPE_SHADER_COMPUTE,
227 &program->ready,
228 &program->compiler_ctx_state,
229 program, si_create_compute_state_async);
230 } else {
231 const struct pipe_llvm_program_header *header;
232 const char *code;
233 header = cso->prog;
234 code = cso->prog + sizeof(struct pipe_llvm_program_header);
235
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);
241 if (program->shader.binary.reloc_count != 0) {
242 fprintf(stderr, "Error: %d unsupported relocations\n",
243 program->shader.binary.reloc_count);
244 FREE(program);
245 return NULL;
246 }
247 } else {
248 si_shader_binary_read_config(&program->shader.binary,
249 &program->shader.config, 0);
250 }
251 si_shader_dump(sctx->screen, &program->shader, &sctx->debug,
252 PIPE_SHADER_COMPUTE, stderr, true);
253 if (si_shader_binary_upload(sctx->screen, &program->shader) < 0) {
254 fprintf(stderr, "LLVM failed to upload shader\n");
255 FREE(program);
256 return NULL;
257 }
258 }
259
260 return program;
261 }
262
263 static void si_bind_compute_state(struct pipe_context *ctx, void *state)
264 {
265 struct si_context *sctx = (struct si_context*)ctx;
266 struct si_compute *program = (struct si_compute*)state;
267
268 sctx->cs_shader_state.program = program;
269 if (!program)
270 return;
271
272 /* Wait because we need active slot usage masks. */
273 if (program->ir_type != PIPE_SHADER_IR_NATIVE)
274 util_queue_fence_wait(&program->ready);
275
276 si_set_active_descriptors(sctx,
277 SI_DESCS_FIRST_COMPUTE +
278 SI_SHADER_DESCS_CONST_AND_SHADER_BUFFERS,
279 program->active_const_and_shader_buffers);
280 si_set_active_descriptors(sctx,
281 SI_DESCS_FIRST_COMPUTE +
282 SI_SHADER_DESCS_SAMPLERS_AND_IMAGES,
283 program->active_samplers_and_images);
284 }
285
286 static void si_set_global_binding(
287 struct pipe_context *ctx, unsigned first, unsigned n,
288 struct pipe_resource **resources,
289 uint32_t **handles)
290 {
291 unsigned i;
292 struct si_context *sctx = (struct si_context*)ctx;
293 struct si_compute *program = sctx->cs_shader_state.program;
294
295 assert(first + n <= MAX_GLOBAL_BUFFERS);
296
297 if (!resources) {
298 for (i = 0; i < n; i++) {
299 pipe_resource_reference(&program->global_buffers[first + i], NULL);
300 }
301 return;
302 }
303
304 for (i = 0; i < n; i++) {
305 uint64_t va;
306 uint32_t offset;
307 pipe_resource_reference(&program->global_buffers[first + i], resources[i]);
308 va = r600_resource(resources[i])->gpu_address;
309 offset = util_le32_to_cpu(*handles[i]);
310 va += offset;
311 va = util_cpu_to_le64(va);
312 memcpy(handles[i], &va, sizeof(va));
313 }
314 }
315
316 static void si_initialize_compute(struct si_context *sctx)
317 {
318 struct radeon_cmdbuf *cs = sctx->gfx_cs;
319 uint64_t bc_va;
320
321 radeon_set_sh_reg_seq(cs, R_00B858_COMPUTE_STATIC_THREAD_MGMT_SE0, 2);
322 /* R_00B858_COMPUTE_STATIC_THREAD_MGMT_SE0 / SE1 */
323 radeon_emit(cs, S_00B858_SH0_CU_EN(0xffff) | S_00B858_SH1_CU_EN(0xffff));
324 radeon_emit(cs, S_00B85C_SH0_CU_EN(0xffff) | S_00B85C_SH1_CU_EN(0xffff));
325
326 if (sctx->chip_class >= CIK) {
327 /* Also set R_00B858_COMPUTE_STATIC_THREAD_MGMT_SE2 / SE3 */
328 radeon_set_sh_reg_seq(cs,
329 R_00B864_COMPUTE_STATIC_THREAD_MGMT_SE2, 2);
330 radeon_emit(cs, S_00B864_SH0_CU_EN(0xffff) |
331 S_00B864_SH1_CU_EN(0xffff));
332 radeon_emit(cs, S_00B868_SH0_CU_EN(0xffff) |
333 S_00B868_SH1_CU_EN(0xffff));
334 }
335
336 /* This register has been moved to R_00CD20_COMPUTE_MAX_WAVE_ID
337 * and is now per pipe, so it should be handled in the
338 * kernel if we want to use something other than the default value,
339 * which is now 0x22f.
340 */
341 if (sctx->chip_class <= SI) {
342 /* XXX: This should be:
343 * (number of compute units) * 4 * (waves per simd) - 1 */
344
345 radeon_set_sh_reg(cs, R_00B82C_COMPUTE_MAX_WAVE_ID,
346 0x190 /* Default value */);
347 }
348
349 /* Set the pointer to border colors. */
350 bc_va = sctx->border_color_buffer->gpu_address;
351
352 if (sctx->chip_class >= CIK) {
353 radeon_set_uconfig_reg_seq(cs, R_030E00_TA_CS_BC_BASE_ADDR, 2);
354 radeon_emit(cs, bc_va >> 8); /* R_030E00_TA_CS_BC_BASE_ADDR */
355 radeon_emit(cs, S_030E04_ADDRESS(bc_va >> 40)); /* R_030E04_TA_CS_BC_BASE_ADDR_HI */
356 } else {
357 if (sctx->screen->info.si_TA_CS_BC_BASE_ADDR_allowed) {
358 radeon_set_config_reg(cs, R_00950C_TA_CS_BC_BASE_ADDR,
359 bc_va >> 8);
360 }
361 }
362
363 sctx->cs_shader_state.emitted_program = NULL;
364 sctx->cs_shader_state.initialized = true;
365 }
366
367 static bool si_setup_compute_scratch_buffer(struct si_context *sctx,
368 struct si_shader *shader,
369 struct si_shader_config *config)
370 {
371 uint64_t scratch_bo_size, scratch_needed;
372 scratch_bo_size = 0;
373 scratch_needed = config->scratch_bytes_per_wave * sctx->scratch_waves;
374 if (sctx->compute_scratch_buffer)
375 scratch_bo_size = sctx->compute_scratch_buffer->b.b.width0;
376
377 if (scratch_bo_size < scratch_needed) {
378 r600_resource_reference(&sctx->compute_scratch_buffer, NULL);
379
380 sctx->compute_scratch_buffer =
381 si_aligned_buffer_create(&sctx->screen->b,
382 SI_RESOURCE_FLAG_UNMAPPABLE,
383 PIPE_USAGE_DEFAULT,
384 scratch_needed, 256);
385
386 if (!sctx->compute_scratch_buffer)
387 return false;
388 }
389
390 if (sctx->compute_scratch_buffer != shader->scratch_bo && scratch_needed) {
391 uint64_t scratch_va = sctx->compute_scratch_buffer->gpu_address;
392
393 si_shader_apply_scratch_relocs(shader, scratch_va);
394
395 if (si_shader_binary_upload(sctx->screen, shader))
396 return false;
397
398 r600_resource_reference(&shader->scratch_bo,
399 sctx->compute_scratch_buffer);
400 }
401
402 return true;
403 }
404
405 static bool si_switch_compute_shader(struct si_context *sctx,
406 struct si_compute *program,
407 struct si_shader *shader,
408 const amd_kernel_code_t *code_object,
409 unsigned offset)
410 {
411 struct radeon_cmdbuf *cs = sctx->gfx_cs;
412 struct si_shader_config inline_config = {0};
413 struct si_shader_config *config;
414 uint64_t shader_va;
415
416 if (sctx->cs_shader_state.emitted_program == program &&
417 sctx->cs_shader_state.offset == offset)
418 return true;
419
420 if (program->ir_type != PIPE_SHADER_IR_NATIVE) {
421 config = &shader->config;
422 } else {
423 unsigned lds_blocks;
424
425 config = &inline_config;
426 if (code_object) {
427 code_object_to_config(code_object, config);
428 } else {
429 si_shader_binary_read_config(&shader->binary, config, offset);
430 }
431
432 lds_blocks = config->lds_size;
433 /* XXX: We are over allocating LDS. For SI, the shader reports
434 * LDS in blocks of 256 bytes, so if there are 4 bytes lds
435 * allocated in the shader and 4 bytes allocated by the state
436 * tracker, then we will set LDS_SIZE to 512 bytes rather than 256.
437 */
438 if (sctx->chip_class <= SI) {
439 lds_blocks += align(program->local_size, 256) >> 8;
440 } else {
441 lds_blocks += align(program->local_size, 512) >> 9;
442 }
443
444 /* TODO: use si_multiwave_lds_size_workaround */
445 assert(lds_blocks <= 0xFF);
446
447 config->rsrc2 &= C_00B84C_LDS_SIZE;
448 config->rsrc2 |= S_00B84C_LDS_SIZE(lds_blocks);
449 }
450
451 if (!si_setup_compute_scratch_buffer(sctx, shader, config))
452 return false;
453
454 if (shader->scratch_bo) {
455 COMPUTE_DBG(sctx->screen, "Waves: %u; Scratch per wave: %u bytes; "
456 "Total Scratch: %u bytes\n", sctx->scratch_waves,
457 config->scratch_bytes_per_wave,
458 config->scratch_bytes_per_wave *
459 sctx->scratch_waves);
460
461 radeon_add_to_buffer_list(sctx, sctx->gfx_cs,
462 shader->scratch_bo, RADEON_USAGE_READWRITE,
463 RADEON_PRIO_SCRATCH_BUFFER);
464 }
465
466 /* Prefetch the compute shader to TC L2.
467 *
468 * We should also prefetch graphics shaders if a compute dispatch was
469 * the last command, and the compute shader if a draw call was the last
470 * command. However, that would add more complexity and we're likely
471 * to get a shader state change in that case anyway.
472 */
473 if (sctx->chip_class >= CIK) {
474 cik_prefetch_TC_L2_async(sctx, &program->shader.bo->b.b,
475 0, program->shader.bo->b.b.width0);
476 }
477
478 shader_va = shader->bo->gpu_address + offset;
479 if (program->use_code_object_v2) {
480 /* Shader code is placed after the amd_kernel_code_t
481 * struct. */
482 shader_va += sizeof(amd_kernel_code_t);
483 }
484
485 radeon_add_to_buffer_list(sctx, sctx->gfx_cs, shader->bo,
486 RADEON_USAGE_READ, RADEON_PRIO_SHADER_BINARY);
487
488 radeon_set_sh_reg_seq(cs, R_00B830_COMPUTE_PGM_LO, 2);
489 radeon_emit(cs, shader_va >> 8);
490 radeon_emit(cs, S_00B834_DATA(shader_va >> 40));
491
492 radeon_set_sh_reg_seq(cs, R_00B848_COMPUTE_PGM_RSRC1, 2);
493 radeon_emit(cs, config->rsrc1);
494 radeon_emit(cs, config->rsrc2);
495
496 COMPUTE_DBG(sctx->screen, "COMPUTE_PGM_RSRC1: 0x%08x "
497 "COMPUTE_PGM_RSRC2: 0x%08x\n", config->rsrc1, config->rsrc2);
498
499 radeon_set_sh_reg(cs, R_00B860_COMPUTE_TMPRING_SIZE,
500 S_00B860_WAVES(sctx->scratch_waves)
501 | S_00B860_WAVESIZE(config->scratch_bytes_per_wave >> 10));
502
503 sctx->cs_shader_state.emitted_program = program;
504 sctx->cs_shader_state.offset = offset;
505 sctx->cs_shader_state.uses_scratch =
506 config->scratch_bytes_per_wave != 0;
507
508 return true;
509 }
510
511 static void setup_scratch_rsrc_user_sgprs(struct si_context *sctx,
512 const amd_kernel_code_t *code_object,
513 unsigned user_sgpr)
514 {
515 struct radeon_cmdbuf *cs = sctx->gfx_cs;
516 uint64_t scratch_va = sctx->compute_scratch_buffer->gpu_address;
517
518 unsigned max_private_element_size = AMD_HSA_BITS_GET(
519 code_object->code_properties,
520 AMD_CODE_PROPERTY_PRIVATE_ELEMENT_SIZE);
521
522 uint32_t scratch_dword0 = scratch_va & 0xffffffff;
523 uint32_t scratch_dword1 =
524 S_008F04_BASE_ADDRESS_HI(scratch_va >> 32) |
525 S_008F04_SWIZZLE_ENABLE(1);
526
527 /* Disable address clamping */
528 uint32_t scratch_dword2 = 0xffffffff;
529 uint32_t scratch_dword3 =
530 S_008F0C_INDEX_STRIDE(3) |
531 S_008F0C_ADD_TID_ENABLE(1);
532
533 if (sctx->chip_class >= GFX9) {
534 assert(max_private_element_size == 1); /* always 4 bytes on GFX9 */
535 } else {
536 scratch_dword3 |= S_008F0C_ELEMENT_SIZE(max_private_element_size);
537
538 if (sctx->chip_class < VI) {
539 /* BUF_DATA_FORMAT is ignored, but it cannot be
540 * BUF_DATA_FORMAT_INVALID. */
541 scratch_dword3 |=
542 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_8);
543 }
544 }
545
546 radeon_set_sh_reg_seq(cs, R_00B900_COMPUTE_USER_DATA_0 +
547 (user_sgpr * 4), 4);
548 radeon_emit(cs, scratch_dword0);
549 radeon_emit(cs, scratch_dword1);
550 radeon_emit(cs, scratch_dword2);
551 radeon_emit(cs, scratch_dword3);
552 }
553
554 static void si_setup_user_sgprs_co_v2(struct si_context *sctx,
555 const amd_kernel_code_t *code_object,
556 const struct pipe_grid_info *info,
557 uint64_t kernel_args_va)
558 {
559 struct si_compute *program = sctx->cs_shader_state.program;
560 struct radeon_cmdbuf *cs = sctx->gfx_cs;
561
562 static const enum amd_code_property_mask_t workgroup_count_masks [] = {
563 AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_X,
564 AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Y,
565 AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Z
566 };
567
568 unsigned i, user_sgpr = 0;
569 if (AMD_HSA_BITS_GET(code_object->code_properties,
570 AMD_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER)) {
571 if (code_object->workitem_private_segment_byte_size > 0) {
572 setup_scratch_rsrc_user_sgprs(sctx, code_object,
573 user_sgpr);
574 }
575 user_sgpr += 4;
576 }
577
578 if (AMD_HSA_BITS_GET(code_object->code_properties,
579 AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR)) {
580 struct dispatch_packet dispatch;
581 unsigned dispatch_offset;
582 struct r600_resource *dispatch_buf = NULL;
583 uint64_t dispatch_va;
584
585 /* Upload dispatch ptr */
586 memset(&dispatch, 0, sizeof(dispatch));
587
588 dispatch.workgroup_size_x = util_cpu_to_le16(info->block[0]);
589 dispatch.workgroup_size_y = util_cpu_to_le16(info->block[1]);
590 dispatch.workgroup_size_z = util_cpu_to_le16(info->block[2]);
591
592 dispatch.grid_size_x = util_cpu_to_le32(info->grid[0] * info->block[0]);
593 dispatch.grid_size_y = util_cpu_to_le32(info->grid[1] * info->block[1]);
594 dispatch.grid_size_z = util_cpu_to_le32(info->grid[2] * info->block[2]);
595
596 dispatch.private_segment_size = util_cpu_to_le32(program->private_size);
597 dispatch.group_segment_size = util_cpu_to_le32(program->local_size);
598
599 dispatch.kernarg_address = util_cpu_to_le64(kernel_args_va);
600
601 u_upload_data(sctx->b.const_uploader, 0, sizeof(dispatch),
602 256, &dispatch, &dispatch_offset,
603 (struct pipe_resource**)&dispatch_buf);
604
605 if (!dispatch_buf) {
606 fprintf(stderr, "Error: Failed to allocate dispatch "
607 "packet.");
608 }
609 radeon_add_to_buffer_list(sctx, sctx->gfx_cs, dispatch_buf,
610 RADEON_USAGE_READ, RADEON_PRIO_CONST_BUFFER);
611
612 dispatch_va = dispatch_buf->gpu_address + dispatch_offset;
613
614 radeon_set_sh_reg_seq(cs, R_00B900_COMPUTE_USER_DATA_0 +
615 (user_sgpr * 4), 2);
616 radeon_emit(cs, dispatch_va);
617 radeon_emit(cs, S_008F04_BASE_ADDRESS_HI(dispatch_va >> 32) |
618 S_008F04_STRIDE(0));
619
620 r600_resource_reference(&dispatch_buf, NULL);
621 user_sgpr += 2;
622 }
623
624 if (AMD_HSA_BITS_GET(code_object->code_properties,
625 AMD_CODE_PROPERTY_ENABLE_SGPR_KERNARG_SEGMENT_PTR)) {
626 radeon_set_sh_reg_seq(cs, R_00B900_COMPUTE_USER_DATA_0 +
627 (user_sgpr * 4), 2);
628 radeon_emit(cs, kernel_args_va);
629 radeon_emit(cs, S_008F04_BASE_ADDRESS_HI (kernel_args_va >> 32) |
630 S_008F04_STRIDE(0));
631 user_sgpr += 2;
632 }
633
634 for (i = 0; i < 3 && user_sgpr < 16; i++) {
635 if (code_object->code_properties & workgroup_count_masks[i]) {
636 radeon_set_sh_reg_seq(cs,
637 R_00B900_COMPUTE_USER_DATA_0 +
638 (user_sgpr * 4), 1);
639 radeon_emit(cs, info->grid[i]);
640 user_sgpr += 1;
641 }
642 }
643 }
644
645 static bool si_upload_compute_input(struct si_context *sctx,
646 const amd_kernel_code_t *code_object,
647 const struct pipe_grid_info *info)
648 {
649 struct radeon_cmdbuf *cs = sctx->gfx_cs;
650 struct si_compute *program = sctx->cs_shader_state.program;
651 struct r600_resource *input_buffer = NULL;
652 unsigned kernel_args_size;
653 unsigned num_work_size_bytes = program->use_code_object_v2 ? 0 : 36;
654 uint32_t kernel_args_offset = 0;
655 uint32_t *kernel_args;
656 void *kernel_args_ptr;
657 uint64_t kernel_args_va;
658 unsigned i;
659
660 /* The extra num_work_size_bytes are for work group / work item size information */
661 kernel_args_size = program->input_size + num_work_size_bytes;
662
663 u_upload_alloc(sctx->b.const_uploader, 0, kernel_args_size,
664 sctx->screen->info.tcc_cache_line_size,
665 &kernel_args_offset,
666 (struct pipe_resource**)&input_buffer, &kernel_args_ptr);
667
668 if (unlikely(!kernel_args_ptr))
669 return false;
670
671 kernel_args = (uint32_t*)kernel_args_ptr;
672 kernel_args_va = input_buffer->gpu_address + kernel_args_offset;
673
674 if (!code_object) {
675 for (i = 0; i < 3; i++) {
676 kernel_args[i] = util_cpu_to_le32(info->grid[i]);
677 kernel_args[i + 3] = util_cpu_to_le32(info->grid[i] * info->block[i]);
678 kernel_args[i + 6] = util_cpu_to_le32(info->block[i]);
679 }
680 }
681
682 memcpy(kernel_args + (num_work_size_bytes / 4), info->input,
683 program->input_size);
684
685
686 for (i = 0; i < (kernel_args_size / 4); i++) {
687 COMPUTE_DBG(sctx->screen, "input %u : %u\n", i,
688 kernel_args[i]);
689 }
690
691
692 radeon_add_to_buffer_list(sctx, sctx->gfx_cs, input_buffer,
693 RADEON_USAGE_READ, RADEON_PRIO_CONST_BUFFER);
694
695 if (code_object) {
696 si_setup_user_sgprs_co_v2(sctx, code_object, info, kernel_args_va);
697 } else {
698 radeon_set_sh_reg_seq(cs, R_00B900_COMPUTE_USER_DATA_0, 2);
699 radeon_emit(cs, kernel_args_va);
700 radeon_emit(cs, S_008F04_BASE_ADDRESS_HI (kernel_args_va >> 32) |
701 S_008F04_STRIDE(0));
702 }
703
704 r600_resource_reference(&input_buffer, NULL);
705
706 return true;
707 }
708
709 static void si_setup_tgsi_grid(struct si_context *sctx,
710 const struct pipe_grid_info *info)
711 {
712 struct si_compute *program = sctx->cs_shader_state.program;
713 struct radeon_cmdbuf *cs = sctx->gfx_cs;
714 unsigned grid_size_reg = R_00B900_COMPUTE_USER_DATA_0 +
715 4 * SI_NUM_RESOURCE_SGPRS;
716 unsigned block_size_reg = grid_size_reg +
717 /* 12 bytes = 3 dwords. */
718 12 * program->uses_grid_size;
719
720 if (info->indirect) {
721 if (program->uses_grid_size) {
722 uint64_t base_va = r600_resource(info->indirect)->gpu_address;
723 uint64_t va = base_va + info->indirect_offset;
724 int i;
725
726 radeon_add_to_buffer_list(sctx, sctx->gfx_cs,
727 r600_resource(info->indirect),
728 RADEON_USAGE_READ, RADEON_PRIO_DRAW_INDIRECT);
729
730 for (i = 0; i < 3; ++i) {
731 radeon_emit(cs, PKT3(PKT3_COPY_DATA, 4, 0));
732 radeon_emit(cs, COPY_DATA_SRC_SEL(COPY_DATA_MEM) |
733 COPY_DATA_DST_SEL(COPY_DATA_REG));
734 radeon_emit(cs, (va + 4 * i));
735 radeon_emit(cs, (va + 4 * i) >> 32);
736 radeon_emit(cs, (grid_size_reg >> 2) + i);
737 radeon_emit(cs, 0);
738 }
739 }
740 } else {
741 if (program->uses_grid_size) {
742 radeon_set_sh_reg_seq(cs, grid_size_reg, 3);
743 radeon_emit(cs, info->grid[0]);
744 radeon_emit(cs, info->grid[1]);
745 radeon_emit(cs, info->grid[2]);
746 }
747 if (program->variable_group_size && program->uses_block_size) {
748 radeon_set_sh_reg_seq(cs, block_size_reg, 3);
749 radeon_emit(cs, info->block[0]);
750 radeon_emit(cs, info->block[1]);
751 radeon_emit(cs, info->block[2]);
752 }
753 }
754 }
755
756 static void si_emit_dispatch_packets(struct si_context *sctx,
757 const struct pipe_grid_info *info)
758 {
759 struct si_screen *sscreen = sctx->screen;
760 struct radeon_cmdbuf *cs = sctx->gfx_cs;
761 bool render_cond_bit = sctx->render_cond && !sctx->render_cond_force_off;
762 unsigned waves_per_threadgroup =
763 DIV_ROUND_UP(info->block[0] * info->block[1] * info->block[2], 64);
764 unsigned compute_resource_limits =
765 S_00B854_SIMD_DEST_CNTL(waves_per_threadgroup % 4 == 0);
766
767 if (sctx->chip_class >= CIK) {
768 unsigned num_cu_per_se = sscreen->info.num_good_compute_units /
769 sscreen->info.max_se;
770
771 /* Force even distribution on all SIMDs in CU if the workgroup
772 * size is 64. This has shown some good improvements if # of CUs
773 * per SE is not a multiple of 4.
774 */
775 if (num_cu_per_se % 4 && waves_per_threadgroup == 1)
776 compute_resource_limits |= S_00B854_FORCE_SIMD_DIST(1);
777 }
778
779 radeon_set_sh_reg(cs, R_00B854_COMPUTE_RESOURCE_LIMITS,
780 compute_resource_limits);
781
782 radeon_set_sh_reg_seq(cs, R_00B81C_COMPUTE_NUM_THREAD_X, 3);
783 radeon_emit(cs, S_00B81C_NUM_THREAD_FULL(info->block[0]));
784 radeon_emit(cs, S_00B820_NUM_THREAD_FULL(info->block[1]));
785 radeon_emit(cs, S_00B824_NUM_THREAD_FULL(info->block[2]));
786
787 unsigned dispatch_initiator =
788 S_00B800_COMPUTE_SHADER_EN(1) |
789 S_00B800_FORCE_START_AT_000(1) |
790 /* If the KMD allows it (there is a KMD hw register for it),
791 * allow launching waves out-of-order. (same as Vulkan) */
792 S_00B800_ORDER_MODE(sctx->chip_class >= CIK);
793
794 if (info->indirect) {
795 uint64_t base_va = r600_resource(info->indirect)->gpu_address;
796
797 radeon_add_to_buffer_list(sctx, sctx->gfx_cs,
798 r600_resource(info->indirect),
799 RADEON_USAGE_READ, RADEON_PRIO_DRAW_INDIRECT);
800
801 radeon_emit(cs, PKT3(PKT3_SET_BASE, 2, 0) |
802 PKT3_SHADER_TYPE_S(1));
803 radeon_emit(cs, 1);
804 radeon_emit(cs, base_va);
805 radeon_emit(cs, base_va >> 32);
806
807 radeon_emit(cs, PKT3(PKT3_DISPATCH_INDIRECT, 1, render_cond_bit) |
808 PKT3_SHADER_TYPE_S(1));
809 radeon_emit(cs, info->indirect_offset);
810 radeon_emit(cs, dispatch_initiator);
811 } else {
812 radeon_emit(cs, PKT3(PKT3_DISPATCH_DIRECT, 3, render_cond_bit) |
813 PKT3_SHADER_TYPE_S(1));
814 radeon_emit(cs, info->grid[0]);
815 radeon_emit(cs, info->grid[1]);
816 radeon_emit(cs, info->grid[2]);
817 radeon_emit(cs, dispatch_initiator);
818 }
819 }
820
821
822 static void si_launch_grid(
823 struct pipe_context *ctx, const struct pipe_grid_info *info)
824 {
825 struct si_context *sctx = (struct si_context*)ctx;
826 struct si_compute *program = sctx->cs_shader_state.program;
827 const amd_kernel_code_t *code_object =
828 si_compute_get_code_object(program, info->pc);
829 int i;
830 /* HW bug workaround when CS threadgroups > 256 threads and async
831 * compute isn't used, i.e. only one compute job can run at a time.
832 * If async compute is possible, the threadgroup size must be limited
833 * to 256 threads on all queues to avoid the bug.
834 * Only SI and certain CIK chips are affected.
835 */
836 bool cs_regalloc_hang =
837 (sctx->chip_class == SI ||
838 sctx->family == CHIP_BONAIRE ||
839 sctx->family == CHIP_KABINI) &&
840 info->block[0] * info->block[1] * info->block[2] > 256;
841
842 if (cs_regalloc_hang)
843 sctx->flags |= SI_CONTEXT_PS_PARTIAL_FLUSH |
844 SI_CONTEXT_CS_PARTIAL_FLUSH;
845
846 if (program->ir_type != PIPE_SHADER_IR_NATIVE &&
847 program->shader.compilation_failed)
848 return;
849
850 if (sctx->last_num_draw_calls != sctx->num_draw_calls) {
851 si_update_fb_dirtiness_after_rendering(sctx);
852 sctx->last_num_draw_calls = sctx->num_draw_calls;
853 }
854
855 si_decompress_textures(sctx, 1 << PIPE_SHADER_COMPUTE);
856
857 /* Add buffer sizes for memory checking in need_cs_space. */
858 si_context_add_resource_size(sctx, &program->shader.bo->b.b);
859 /* TODO: add the scratch buffer */
860
861 if (info->indirect) {
862 si_context_add_resource_size(sctx, info->indirect);
863
864 /* Indirect buffers use TC L2 on GFX9, but not older hw. */
865 if (sctx->chip_class <= VI &&
866 r600_resource(info->indirect)->TC_L2_dirty) {
867 sctx->flags |= SI_CONTEXT_WRITEBACK_GLOBAL_L2;
868 r600_resource(info->indirect)->TC_L2_dirty = false;
869 }
870 }
871
872 si_need_gfx_cs_space(sctx);
873
874 if (!sctx->cs_shader_state.initialized)
875 si_initialize_compute(sctx);
876
877 if (sctx->flags)
878 si_emit_cache_flush(sctx);
879
880 if (!si_switch_compute_shader(sctx, program, &program->shader,
881 code_object, info->pc))
882 return;
883
884 si_upload_compute_shader_descriptors(sctx);
885 si_emit_compute_shader_pointers(sctx);
886
887 if (si_is_atom_dirty(sctx, &sctx->atoms.s.render_cond)) {
888 sctx->atoms.s.render_cond.emit(sctx);
889 si_set_atom_dirty(sctx, &sctx->atoms.s.render_cond, false);
890 }
891
892 if ((program->input_size ||
893 program->ir_type == PIPE_SHADER_IR_NATIVE) &&
894 unlikely(!si_upload_compute_input(sctx, code_object, info))) {
895 return;
896 }
897
898 /* Global buffers */
899 for (i = 0; i < MAX_GLOBAL_BUFFERS; i++) {
900 struct r600_resource *buffer =
901 r600_resource(program->global_buffers[i]);
902 if (!buffer) {
903 continue;
904 }
905 radeon_add_to_buffer_list(sctx, sctx->gfx_cs, buffer,
906 RADEON_USAGE_READWRITE,
907 RADEON_PRIO_COMPUTE_GLOBAL);
908 }
909
910 if (program->ir_type != PIPE_SHADER_IR_NATIVE)
911 si_setup_tgsi_grid(sctx, info);
912
913 si_emit_dispatch_packets(sctx, info);
914
915 if (unlikely(sctx->current_saved_cs)) {
916 si_trace_emit(sctx);
917 si_log_compute_state(sctx, sctx->log);
918 }
919
920 sctx->compute_is_busy = true;
921 sctx->num_compute_calls++;
922 if (sctx->cs_shader_state.uses_scratch)
923 sctx->num_spill_compute_calls++;
924
925 if (cs_regalloc_hang)
926 sctx->flags |= SI_CONTEXT_CS_PARTIAL_FLUSH;
927 }
928
929 void si_destroy_compute(struct si_compute *program)
930 {
931 if (program->ir_type != PIPE_SHADER_IR_NATIVE) {
932 util_queue_drop_job(&program->screen->shader_compiler_queue,
933 &program->ready);
934 util_queue_fence_destroy(&program->ready);
935 }
936
937 si_shader_destroy(&program->shader);
938 FREE(program);
939 }
940
941 static void si_delete_compute_state(struct pipe_context *ctx, void* state){
942 struct si_compute *program = (struct si_compute *)state;
943 struct si_context *sctx = (struct si_context*)ctx;
944
945 if (!state)
946 return;
947
948 if (program == sctx->cs_shader_state.program)
949 sctx->cs_shader_state.program = NULL;
950
951 if (program == sctx->cs_shader_state.emitted_program)
952 sctx->cs_shader_state.emitted_program = NULL;
953
954 si_compute_reference(&program, NULL);
955 }
956
957 static void si_set_compute_resources(struct pipe_context * ctx_,
958 unsigned start, unsigned count,
959 struct pipe_surface ** surfaces) { }
960
961 void si_init_compute_functions(struct si_context *sctx)
962 {
963 sctx->b.create_compute_state = si_create_compute_state;
964 sctx->b.delete_compute_state = si_delete_compute_state;
965 sctx->b.bind_compute_state = si_bind_compute_state;
966 sctx->b.set_compute_resources = si_set_compute_resources;
967 sctx->b.set_global_binding = si_set_global_binding;
968 sctx->b.launch_grid = si_launch_grid;
969 }