Path: blob/21.2-virgl/src/gallium/drivers/radeonsi/si_compute.c
4570 views
/*1* Copyright 2013 Advanced Micro Devices, Inc.2* All Rights Reserved.3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* on the rights to use, copy, modify, merge, publish, distribute, sub8* license, and/or sell copies of the Software, and to permit persons to whom9* the Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL18* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,19* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR20* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE21* USE OR OTHER DEALINGS IN THE SOFTWARE.22*23*/2425#include "si_compute.h"2627#include "ac_rtld.h"28#include "amd_kernel_code_t.h"29#include "nir/tgsi_to_nir.h"30#include "si_build_pm4.h"31#include "util/u_async_debug.h"32#include "util/u_memory.h"33#include "util/u_upload_mgr.h"3435#define COMPUTE_DBG(sscreen, fmt, args...) \36do { \37if ((sscreen->debug_flags & DBG(COMPUTE))) \38fprintf(stderr, fmt, ##args); \39} while (0);4041struct dispatch_packet {42uint16_t header;43uint16_t setup;44uint16_t workgroup_size_x;45uint16_t workgroup_size_y;46uint16_t workgroup_size_z;47uint16_t reserved0;48uint32_t grid_size_x;49uint32_t grid_size_y;50uint32_t grid_size_z;51uint32_t private_segment_size;52uint32_t group_segment_size;53uint64_t kernel_object;54uint64_t kernarg_address;55uint64_t reserved2;56};5758static const amd_kernel_code_t *si_compute_get_code_object(const struct si_compute *program,59uint64_t symbol_offset)60{61const struct si_shader_selector *sel = &program->sel;6263if (program->ir_type != PIPE_SHADER_IR_NATIVE)64return NULL;6566struct ac_rtld_binary rtld;67if (!ac_rtld_open(&rtld,68(struct ac_rtld_open_info){.info = &sel->screen->info,69.shader_type = MESA_SHADER_COMPUTE,70.wave_size = sel->screen->compute_wave_size,71.num_parts = 1,72.elf_ptrs = &program->shader.binary.elf_buffer,73.elf_sizes = &program->shader.binary.elf_size}))74return NULL;7576const amd_kernel_code_t *result = NULL;77const char *text;78size_t size;79if (!ac_rtld_get_section_by_name(&rtld, ".text", &text, &size))80goto out;8182if (symbol_offset + sizeof(amd_kernel_code_t) > size)83goto out;8485result = (const amd_kernel_code_t *)(text + symbol_offset);8687out:88ac_rtld_close(&rtld);89return result;90}9192static void code_object_to_config(const amd_kernel_code_t *code_object,93struct ac_shader_config *out_config)94{9596uint32_t rsrc1 = code_object->compute_pgm_resource_registers;97uint32_t rsrc2 = code_object->compute_pgm_resource_registers >> 32;98out_config->num_sgprs = code_object->wavefront_sgpr_count;99out_config->num_vgprs = code_object->workitem_vgpr_count;100out_config->float_mode = G_00B028_FLOAT_MODE(rsrc1);101out_config->rsrc1 = rsrc1;102out_config->lds_size = MAX2(out_config->lds_size, G_00B84C_LDS_SIZE(rsrc2));103out_config->rsrc2 = rsrc2;104out_config->scratch_bytes_per_wave =105align(code_object->workitem_private_segment_byte_size * 64, 1024);106}107108/* Asynchronous compute shader compilation. */109static void si_create_compute_state_async(void *job, void *gdata, int thread_index)110{111struct si_compute *program = (struct si_compute *)job;112struct si_shader_selector *sel = &program->sel;113struct si_shader *shader = &program->shader;114struct ac_llvm_compiler *compiler;115struct pipe_debug_callback *debug = &sel->compiler_ctx_state.debug;116struct si_screen *sscreen = sel->screen;117118assert(!debug->debug_message || debug->async);119assert(thread_index >= 0);120assert(thread_index < ARRAY_SIZE(sscreen->compiler));121compiler = &sscreen->compiler[thread_index];122123if (!compiler->passes)124si_init_compiler(sscreen, compiler);125126assert(program->ir_type == PIPE_SHADER_IR_NIR);127si_nir_scan_shader(sel->nir, &sel->info);128129si_get_active_slot_masks(&sel->info, &sel->active_const_and_shader_buffers,130&sel->active_samplers_and_images);131132program->shader.is_monolithic = true;133134/* Variable block sizes need 10 bits (1 + log2(SI_MAX_VARIABLE_THREADS_PER_BLOCK)) per dim.135* We pack them into a single user SGPR.136*/137unsigned user_sgprs = SI_NUM_RESOURCE_SGPRS + (sel->info.uses_grid_size ? 3 : 0) +138(sel->info.uses_variable_block_size ? 1 : 0) +139sel->info.base.cs.user_data_components_amd;140141/* Fast path for compute shaders - some descriptors passed via user SGPRs. */142/* Shader buffers in user SGPRs. */143for (unsigned i = 0; i < MIN2(3, sel->info.base.num_ssbos) && user_sgprs <= 12; i++) {144user_sgprs = align(user_sgprs, 4);145if (i == 0)146sel->cs_shaderbufs_sgpr_index = user_sgprs;147user_sgprs += 4;148sel->cs_num_shaderbufs_in_user_sgprs++;149}150151/* Images in user SGPRs. */152unsigned non_msaa_images = u_bit_consecutive(0, sel->info.base.num_images) &153~sel->info.base.msaa_images;154155for (unsigned i = 0; i < 3 && non_msaa_images & (1 << i); i++) {156unsigned num_sgprs = sel->info.base.image_buffers & (1 << i) ? 4 : 8;157158if (align(user_sgprs, num_sgprs) + num_sgprs > 16)159break;160161user_sgprs = align(user_sgprs, num_sgprs);162if (i == 0)163sel->cs_images_sgpr_index = user_sgprs;164user_sgprs += num_sgprs;165sel->cs_num_images_in_user_sgprs++;166}167sel->cs_images_num_sgprs = user_sgprs - sel->cs_images_sgpr_index;168assert(user_sgprs <= 16);169170unsigned char ir_sha1_cache_key[20];171si_get_ir_cache_key(sel, false, false, ir_sha1_cache_key);172173/* Try to load the shader from the shader cache. */174simple_mtx_lock(&sscreen->shader_cache_mutex);175176if (si_shader_cache_load_shader(sscreen, ir_sha1_cache_key, shader)) {177simple_mtx_unlock(&sscreen->shader_cache_mutex);178179si_shader_dump_stats_for_shader_db(sscreen, shader, debug);180si_shader_dump(sscreen, shader, debug, stderr, true);181182if (!si_shader_binary_upload(sscreen, shader, 0))183program->shader.compilation_failed = true;184} else {185simple_mtx_unlock(&sscreen->shader_cache_mutex);186187if (!si_create_shader_variant(sscreen, compiler, &program->shader, debug)) {188program->shader.compilation_failed = true;189return;190}191192bool scratch_enabled = shader->config.scratch_bytes_per_wave > 0;193194shader->config.rsrc1 = S_00B848_VGPRS((shader->config.num_vgprs - 1) /195((sscreen->compute_wave_size == 32 ||196sscreen->info.wave64_vgpr_alloc_granularity == 8) ? 8 : 4)) |197S_00B848_DX10_CLAMP(1) |198S_00B848_MEM_ORDERED(si_shader_mem_ordered(shader)) |199S_00B848_WGP_MODE(sscreen->info.chip_class >= GFX10) |200S_00B848_FLOAT_MODE(shader->config.float_mode);201202if (sscreen->info.chip_class < GFX10) {203shader->config.rsrc1 |= S_00B848_SGPRS((shader->config.num_sgprs - 1) / 8);204}205206shader->config.rsrc2 = S_00B84C_USER_SGPR(user_sgprs) | S_00B84C_SCRATCH_EN(scratch_enabled) |207S_00B84C_TGID_X_EN(sel->info.uses_block_id[0]) |208S_00B84C_TGID_Y_EN(sel->info.uses_block_id[1]) |209S_00B84C_TGID_Z_EN(sel->info.uses_block_id[2]) |210S_00B84C_TG_SIZE_EN(sel->info.uses_subgroup_info) |211S_00B84C_TIDIG_COMP_CNT(sel->info.uses_thread_id[2]212? 2213: sel->info.uses_thread_id[1] ? 1 : 0) |214S_00B84C_LDS_SIZE(shader->config.lds_size);215216simple_mtx_lock(&sscreen->shader_cache_mutex);217si_shader_cache_insert_shader(sscreen, ir_sha1_cache_key, shader, true);218simple_mtx_unlock(&sscreen->shader_cache_mutex);219}220221ralloc_free(sel->nir);222sel->nir = NULL;223}224225static void *si_create_compute_state(struct pipe_context *ctx, const struct pipe_compute_state *cso)226{227struct si_context *sctx = (struct si_context *)ctx;228struct si_screen *sscreen = (struct si_screen *)ctx->screen;229struct si_compute *program = CALLOC_STRUCT(si_compute);230struct si_shader_selector *sel = &program->sel;231232pipe_reference_init(&sel->base.reference, 1);233sel->info.stage = MESA_SHADER_COMPUTE;234sel->screen = sscreen;235sel->const_and_shader_buf_descriptors_index =236si_const_and_shader_buffer_descriptors_idx(PIPE_SHADER_COMPUTE);237sel->sampler_and_images_descriptors_index =238si_sampler_and_image_descriptors_idx(PIPE_SHADER_COMPUTE);239sel->info.base.shared_size = cso->req_local_mem;240program->shader.selector = &program->sel;241program->ir_type = cso->ir_type;242program->private_size = cso->req_private_mem;243program->input_size = cso->req_input_mem;244245if (cso->ir_type != PIPE_SHADER_IR_NATIVE) {246if (cso->ir_type == PIPE_SHADER_IR_TGSI) {247program->ir_type = PIPE_SHADER_IR_NIR;248sel->nir = tgsi_to_nir(cso->prog, ctx->screen, true);249} else {250assert(cso->ir_type == PIPE_SHADER_IR_NIR);251sel->nir = (struct nir_shader *)cso->prog;252}253254sel->compiler_ctx_state.debug = sctx->debug;255sel->compiler_ctx_state.is_debug_context = sctx->is_debug;256p_atomic_inc(&sscreen->num_shaders_created);257258si_schedule_initial_compile(sctx, MESA_SHADER_COMPUTE, &sel->ready, &sel->compiler_ctx_state,259program, si_create_compute_state_async);260} else {261const struct pipe_binary_program_header *header;262header = cso->prog;263264program->shader.binary.elf_size = header->num_bytes;265program->shader.binary.elf_buffer = malloc(header->num_bytes);266if (!program->shader.binary.elf_buffer) {267FREE(program);268return NULL;269}270memcpy((void *)program->shader.binary.elf_buffer, header->blob, header->num_bytes);271272const amd_kernel_code_t *code_object = si_compute_get_code_object(program, 0);273code_object_to_config(code_object, &program->shader.config);274275si_shader_dump(sctx->screen, &program->shader, &sctx->debug, stderr, true);276if (!si_shader_binary_upload(sctx->screen, &program->shader, 0)) {277fprintf(stderr, "LLVM failed to upload shader\n");278free((void *)program->shader.binary.elf_buffer);279FREE(program);280return NULL;281}282}283284return program;285}286287static void si_bind_compute_state(struct pipe_context *ctx, void *state)288{289struct si_context *sctx = (struct si_context *)ctx;290struct si_compute *program = (struct si_compute *)state;291struct si_shader_selector *sel = &program->sel;292293sctx->cs_shader_state.program = program;294if (!program)295return;296297/* Wait because we need active slot usage masks. */298if (program->ir_type != PIPE_SHADER_IR_NATIVE)299util_queue_fence_wait(&sel->ready);300301si_set_active_descriptors(sctx,302SI_DESCS_FIRST_COMPUTE + SI_SHADER_DESCS_CONST_AND_SHADER_BUFFERS,303sel->active_const_and_shader_buffers);304si_set_active_descriptors(sctx, SI_DESCS_FIRST_COMPUTE + SI_SHADER_DESCS_SAMPLERS_AND_IMAGES,305sel->active_samplers_and_images);306307sctx->compute_shaderbuf_sgprs_dirty = true;308sctx->compute_image_sgprs_dirty = true;309310if (unlikely((sctx->screen->debug_flags & DBG(SQTT)) && sctx->thread_trace)) {311uint32_t pipeline_code_hash = _mesa_hash_data_with_seed(312program->shader.binary.elf_buffer,313program->shader.binary.elf_size,3140);315uint64_t base_address = program->shader.bo->gpu_address;316317struct ac_thread_trace_data *thread_trace_data = sctx->thread_trace;318if (!si_sqtt_pipeline_is_registered(thread_trace_data, pipeline_code_hash)) {319si_sqtt_register_pipeline(sctx, pipeline_code_hash, base_address, true);320}321322si_sqtt_describe_pipeline_bind(sctx, pipeline_code_hash, 1);323}324}325326static void si_set_global_binding(struct pipe_context *ctx, unsigned first, unsigned n,327struct pipe_resource **resources, uint32_t **handles)328{329unsigned i;330struct si_context *sctx = (struct si_context *)ctx;331struct si_compute *program = sctx->cs_shader_state.program;332333if (first + n > program->max_global_buffers) {334unsigned old_max = program->max_global_buffers;335program->max_global_buffers = first + n;336program->global_buffers = realloc(337program->global_buffers, program->max_global_buffers * sizeof(program->global_buffers[0]));338if (!program->global_buffers) {339fprintf(stderr, "radeonsi: failed to allocate compute global_buffers\n");340return;341}342343memset(&program->global_buffers[old_max], 0,344(program->max_global_buffers - old_max) * sizeof(program->global_buffers[0]));345}346347if (!resources) {348for (i = 0; i < n; i++) {349pipe_resource_reference(&program->global_buffers[first + i], NULL);350}351return;352}353354for (i = 0; i < n; i++) {355uint64_t va;356uint32_t offset;357pipe_resource_reference(&program->global_buffers[first + i], resources[i]);358va = si_resource(resources[i])->gpu_address;359offset = util_le32_to_cpu(*handles[i]);360va += offset;361va = util_cpu_to_le64(va);362memcpy(handles[i], &va, sizeof(va));363}364}365366void si_emit_initial_compute_regs(struct si_context *sctx, struct radeon_cmdbuf *cs)367{368radeon_begin(cs);369radeon_set_sh_reg_seq(cs, R_00B858_COMPUTE_STATIC_THREAD_MGMT_SE0, 2);370/* R_00B858_COMPUTE_STATIC_THREAD_MGMT_SE0 / SE1,371* renamed COMPUTE_DESTINATION_EN_SEn on gfx10. */372radeon_emit(cs, S_00B858_SH0_CU_EN(0xffff) | S_00B858_SH1_CU_EN(0xffff));373radeon_emit(cs, S_00B858_SH0_CU_EN(0xffff) | S_00B858_SH1_CU_EN(0xffff));374375if (sctx->chip_class == GFX6) {376/* This register has been moved to R_00CD20_COMPUTE_MAX_WAVE_ID377* and is now per pipe, so it should be handled in the378* kernel if we want to use something other than the default value.379*380* TODO: This should be:381* (number of compute units) * 4 * (waves per simd) - 1382*/383radeon_set_sh_reg(cs, R_00B82C_COMPUTE_MAX_WAVE_ID, 0x190 /* Default value */);384385if (sctx->screen->info.si_TA_CS_BC_BASE_ADDR_allowed) {386uint64_t bc_va = sctx->border_color_buffer->gpu_address;387388radeon_set_config_reg(cs, R_00950C_TA_CS_BC_BASE_ADDR, bc_va >> 8);389}390}391392if (sctx->chip_class >= GFX7) {393/* Also set R_00B858_COMPUTE_STATIC_THREAD_MGMT_SE2 / SE3 */394radeon_set_sh_reg_seq(cs, R_00B864_COMPUTE_STATIC_THREAD_MGMT_SE2, 2);395radeon_emit(cs, S_00B858_SH0_CU_EN(0xffff) | S_00B858_SH1_CU_EN(0xffff));396radeon_emit(cs, S_00B858_SH0_CU_EN(0xffff) | S_00B858_SH1_CU_EN(0xffff));397398/* Disable profiling on compute queues. */399if (cs != &sctx->gfx_cs || !sctx->screen->info.has_graphics) {400radeon_set_sh_reg(cs, R_00B82C_COMPUTE_PERFCOUNT_ENABLE, 0);401radeon_set_sh_reg(cs, R_00B878_COMPUTE_THREAD_TRACE_ENABLE, 0);402}403404/* Set the pointer to border colors. */405/* Aldebaran doesn't support border colors. */406if (sctx->border_color_buffer) {407uint64_t bc_va = sctx->border_color_buffer->gpu_address;408409radeon_set_uconfig_reg_seq(cs, R_030E00_TA_CS_BC_BASE_ADDR, 2, false);410radeon_emit(cs, bc_va >> 8); /* R_030E00_TA_CS_BC_BASE_ADDR */411radeon_emit(cs, S_030E04_ADDRESS(bc_va >> 40)); /* R_030E04_TA_CS_BC_BASE_ADDR_HI */412}413}414415/* cs_preamble_state initializes this for the gfx queue, so only do this416* if we are on a compute queue.417*/418if (sctx->chip_class >= GFX9 &&419(cs != &sctx->gfx_cs || !sctx->screen->info.has_graphics)) {420radeon_set_uconfig_reg(cs, R_0301EC_CP_COHER_START_DELAY,421sctx->chip_class >= GFX10 ? 0x20 : 0);422}423424if (sctx->chip_class >= GFX10) {425radeon_set_sh_reg(cs, R_00B890_COMPUTE_USER_ACCUM_0, 0);426radeon_set_sh_reg(cs, R_00B894_COMPUTE_USER_ACCUM_1, 0);427radeon_set_sh_reg(cs, R_00B898_COMPUTE_USER_ACCUM_2, 0);428radeon_set_sh_reg(cs, R_00B89C_COMPUTE_USER_ACCUM_3, 0);429radeon_set_sh_reg(cs, R_00B8A0_COMPUTE_PGM_RSRC3, 0);430radeon_set_sh_reg(cs, R_00B9F4_COMPUTE_DISPATCH_TUNNEL, 0);431}432radeon_end();433}434435static bool si_setup_compute_scratch_buffer(struct si_context *sctx, struct si_shader *shader,436struct ac_shader_config *config)437{438uint64_t scratch_bo_size, scratch_needed;439scratch_bo_size = 0;440scratch_needed = config->scratch_bytes_per_wave * sctx->scratch_waves;441if (sctx->compute_scratch_buffer)442scratch_bo_size = sctx->compute_scratch_buffer->b.b.width0;443444if (scratch_bo_size < scratch_needed) {445si_resource_reference(&sctx->compute_scratch_buffer, NULL);446447sctx->compute_scratch_buffer =448si_aligned_buffer_create(&sctx->screen->b,449SI_RESOURCE_FLAG_UNMAPPABLE | SI_RESOURCE_FLAG_DRIVER_INTERNAL,450PIPE_USAGE_DEFAULT,451scratch_needed, sctx->screen->info.pte_fragment_size);452453if (!sctx->compute_scratch_buffer)454return false;455}456457if (sctx->compute_scratch_buffer != shader->scratch_bo && scratch_needed) {458uint64_t scratch_va = sctx->compute_scratch_buffer->gpu_address;459460if (!si_shader_binary_upload(sctx->screen, shader, scratch_va))461return false;462463si_resource_reference(&shader->scratch_bo, sctx->compute_scratch_buffer);464}465466return true;467}468469static bool si_switch_compute_shader(struct si_context *sctx, struct si_compute *program,470struct si_shader *shader, const amd_kernel_code_t *code_object,471unsigned offset, bool *prefetch)472{473struct radeon_cmdbuf *cs = &sctx->gfx_cs;474struct ac_shader_config inline_config = {0};475struct ac_shader_config *config;476uint64_t shader_va;477478*prefetch = false;479480if (sctx->cs_shader_state.emitted_program == program && sctx->cs_shader_state.offset == offset)481return true;482483if (program->ir_type != PIPE_SHADER_IR_NATIVE) {484config = &shader->config;485} else {486unsigned lds_blocks;487488config = &inline_config;489code_object_to_config(code_object, config);490491lds_blocks = config->lds_size;492/* XXX: We are over allocating LDS. For GFX6, the shader reports493* LDS in blocks of 256 bytes, so if there are 4 bytes lds494* allocated in the shader and 4 bytes allocated by the state495* tracker, then we will set LDS_SIZE to 512 bytes rather than 256.496*/497if (sctx->chip_class <= GFX6) {498lds_blocks += align(program->sel.info.base.shared_size, 256) >> 8;499} else {500lds_blocks += align(program->sel.info.base.shared_size, 512) >> 9;501}502503/* TODO: use si_multiwave_lds_size_workaround */504assert(lds_blocks <= 0xFF);505506config->rsrc2 &= C_00B84C_LDS_SIZE;507config->rsrc2 |= S_00B84C_LDS_SIZE(lds_blocks);508}509510if (!si_setup_compute_scratch_buffer(sctx, shader, config))511return false;512513if (shader->scratch_bo) {514COMPUTE_DBG(sctx->screen,515"Waves: %u; Scratch per wave: %u bytes; "516"Total Scratch: %u bytes\n",517sctx->scratch_waves, config->scratch_bytes_per_wave,518config->scratch_bytes_per_wave * sctx->scratch_waves);519520radeon_add_to_buffer_list(sctx, &sctx->gfx_cs, shader->scratch_bo, RADEON_USAGE_READWRITE,521RADEON_PRIO_SCRATCH_BUFFER);522}523524shader_va = shader->bo->gpu_address + offset;525if (program->ir_type == PIPE_SHADER_IR_NATIVE) {526/* Shader code is placed after the amd_kernel_code_t527* struct. */528shader_va += sizeof(amd_kernel_code_t);529}530531radeon_add_to_buffer_list(sctx, &sctx->gfx_cs, shader->bo, RADEON_USAGE_READ,532RADEON_PRIO_SHADER_BINARY);533534radeon_begin(cs);535radeon_set_sh_reg_seq(cs, R_00B830_COMPUTE_PGM_LO, 2);536radeon_emit(cs, shader_va >> 8);537radeon_emit(cs, S_00B834_DATA(shader_va >> 40));538539radeon_set_sh_reg_seq(cs, R_00B848_COMPUTE_PGM_RSRC1, 2);540radeon_emit(cs, config->rsrc1);541radeon_emit(cs, config->rsrc2);542543COMPUTE_DBG(sctx->screen,544"COMPUTE_PGM_RSRC1: 0x%08x "545"COMPUTE_PGM_RSRC2: 0x%08x\n",546config->rsrc1, config->rsrc2);547548sctx->max_seen_compute_scratch_bytes_per_wave =549MAX2(sctx->max_seen_compute_scratch_bytes_per_wave, config->scratch_bytes_per_wave);550551radeon_set_sh_reg(cs, R_00B860_COMPUTE_TMPRING_SIZE,552S_00B860_WAVES(sctx->scratch_waves) |553S_00B860_WAVESIZE(sctx->max_seen_compute_scratch_bytes_per_wave >> 10));554radeon_end();555556sctx->cs_shader_state.emitted_program = program;557sctx->cs_shader_state.offset = offset;558sctx->cs_shader_state.uses_scratch = config->scratch_bytes_per_wave != 0;559560*prefetch = true;561return true;562}563564static void setup_scratch_rsrc_user_sgprs(struct si_context *sctx,565const amd_kernel_code_t *code_object, unsigned user_sgpr)566{567struct radeon_cmdbuf *cs = &sctx->gfx_cs;568uint64_t scratch_va = sctx->compute_scratch_buffer->gpu_address;569570unsigned max_private_element_size =571AMD_HSA_BITS_GET(code_object->code_properties, AMD_CODE_PROPERTY_PRIVATE_ELEMENT_SIZE);572573uint32_t scratch_dword0 = scratch_va & 0xffffffff;574uint32_t scratch_dword1 =575S_008F04_BASE_ADDRESS_HI(scratch_va >> 32) | S_008F04_SWIZZLE_ENABLE(1);576577/* Disable address clamping */578uint32_t scratch_dword2 = 0xffffffff;579uint32_t scratch_dword3 = S_008F0C_INDEX_STRIDE(3) | S_008F0C_ADD_TID_ENABLE(1);580581if (sctx->chip_class >= GFX9) {582assert(max_private_element_size == 1); /* always 4 bytes on GFX9 */583} else {584scratch_dword3 |= S_008F0C_ELEMENT_SIZE(max_private_element_size);585586if (sctx->chip_class < GFX8) {587/* BUF_DATA_FORMAT is ignored, but it cannot be588* BUF_DATA_FORMAT_INVALID. */589scratch_dword3 |= S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_8);590}591}592593radeon_begin(cs);594radeon_set_sh_reg_seq(cs, R_00B900_COMPUTE_USER_DATA_0 + (user_sgpr * 4), 4);595radeon_emit(cs, scratch_dword0);596radeon_emit(cs, scratch_dword1);597radeon_emit(cs, scratch_dword2);598radeon_emit(cs, scratch_dword3);599radeon_end();600}601602static void si_setup_user_sgprs_co_v2(struct si_context *sctx, const amd_kernel_code_t *code_object,603const struct pipe_grid_info *info, uint64_t kernel_args_va)604{605struct si_compute *program = sctx->cs_shader_state.program;606struct radeon_cmdbuf *cs = &sctx->gfx_cs;607608static const enum amd_code_property_mask_t workgroup_count_masks[] = {609AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_X,610AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Y,611AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Z};612613unsigned i, user_sgpr = 0;614if (AMD_HSA_BITS_GET(code_object->code_properties,615AMD_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER)) {616if (code_object->workitem_private_segment_byte_size > 0) {617setup_scratch_rsrc_user_sgprs(sctx, code_object, user_sgpr);618}619user_sgpr += 4;620}621622radeon_begin(cs);623624if (AMD_HSA_BITS_GET(code_object->code_properties, AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR)) {625struct dispatch_packet dispatch;626unsigned dispatch_offset;627struct si_resource *dispatch_buf = NULL;628uint64_t dispatch_va;629630/* Upload dispatch ptr */631memset(&dispatch, 0, sizeof(dispatch));632633dispatch.workgroup_size_x = util_cpu_to_le16(info->block[0]);634dispatch.workgroup_size_y = util_cpu_to_le16(info->block[1]);635dispatch.workgroup_size_z = util_cpu_to_le16(info->block[2]);636637dispatch.grid_size_x = util_cpu_to_le32(info->grid[0] * info->block[0]);638dispatch.grid_size_y = util_cpu_to_le32(info->grid[1] * info->block[1]);639dispatch.grid_size_z = util_cpu_to_le32(info->grid[2] * info->block[2]);640641dispatch.private_segment_size = util_cpu_to_le32(program->private_size);642dispatch.group_segment_size = util_cpu_to_le32(program->sel.info.base.shared_size);643644dispatch.kernarg_address = util_cpu_to_le64(kernel_args_va);645646u_upload_data(sctx->b.const_uploader, 0, sizeof(dispatch), 256, &dispatch, &dispatch_offset,647(struct pipe_resource **)&dispatch_buf);648649if (!dispatch_buf) {650fprintf(stderr, "Error: Failed to allocate dispatch "651"packet.");652}653radeon_add_to_buffer_list(sctx, &sctx->gfx_cs, dispatch_buf, RADEON_USAGE_READ,654RADEON_PRIO_CONST_BUFFER);655656dispatch_va = dispatch_buf->gpu_address + dispatch_offset;657658radeon_set_sh_reg_seq(cs, R_00B900_COMPUTE_USER_DATA_0 + (user_sgpr * 4), 2);659radeon_emit(cs, dispatch_va);660radeon_emit(cs, S_008F04_BASE_ADDRESS_HI(dispatch_va >> 32) | S_008F04_STRIDE(0));661662si_resource_reference(&dispatch_buf, NULL);663user_sgpr += 2;664}665666if (AMD_HSA_BITS_GET(code_object->code_properties,667AMD_CODE_PROPERTY_ENABLE_SGPR_KERNARG_SEGMENT_PTR)) {668radeon_set_sh_reg_seq(cs, R_00B900_COMPUTE_USER_DATA_0 + (user_sgpr * 4), 2);669radeon_emit(cs, kernel_args_va);670radeon_emit(cs, S_008F04_BASE_ADDRESS_HI(kernel_args_va >> 32) | S_008F04_STRIDE(0));671user_sgpr += 2;672}673674for (i = 0; i < 3 && user_sgpr < 16; i++) {675if (code_object->code_properties & workgroup_count_masks[i]) {676radeon_set_sh_reg_seq(cs, R_00B900_COMPUTE_USER_DATA_0 + (user_sgpr * 4), 1);677radeon_emit(cs, info->grid[i]);678user_sgpr += 1;679}680}681radeon_end();682}683684static bool si_upload_compute_input(struct si_context *sctx, const amd_kernel_code_t *code_object,685const struct pipe_grid_info *info)686{687struct si_compute *program = sctx->cs_shader_state.program;688struct si_resource *input_buffer = NULL;689uint32_t kernel_args_offset = 0;690uint32_t *kernel_args;691void *kernel_args_ptr;692uint64_t kernel_args_va;693694u_upload_alloc(sctx->b.const_uploader, 0, program->input_size,695sctx->screen->info.tcc_cache_line_size, &kernel_args_offset,696(struct pipe_resource **)&input_buffer, &kernel_args_ptr);697698if (unlikely(!kernel_args_ptr))699return false;700701kernel_args = (uint32_t *)kernel_args_ptr;702kernel_args_va = input_buffer->gpu_address + kernel_args_offset;703704memcpy(kernel_args, info->input, program->input_size);705706for (unsigned i = 0; i < program->input_size / 4; i++) {707COMPUTE_DBG(sctx->screen, "input %u : %u\n", i, kernel_args[i]);708}709710radeon_add_to_buffer_list(sctx, &sctx->gfx_cs, input_buffer, RADEON_USAGE_READ,711RADEON_PRIO_CONST_BUFFER);712713si_setup_user_sgprs_co_v2(sctx, code_object, info, kernel_args_va);714si_resource_reference(&input_buffer, NULL);715return true;716}717718static void si_setup_nir_user_data(struct si_context *sctx, const struct pipe_grid_info *info)719{720struct si_compute *program = sctx->cs_shader_state.program;721struct si_shader_selector *sel = &program->sel;722struct radeon_cmdbuf *cs = &sctx->gfx_cs;723unsigned grid_size_reg = R_00B900_COMPUTE_USER_DATA_0 + 4 * SI_NUM_RESOURCE_SGPRS;724unsigned block_size_reg = grid_size_reg +725/* 12 bytes = 3 dwords. */72612 * sel->info.uses_grid_size;727unsigned cs_user_data_reg = block_size_reg + 4 * program->sel.info.uses_variable_block_size;728729radeon_begin(cs);730731if (sel->info.uses_grid_size) {732if (info->indirect) {733radeon_end();734735for (unsigned i = 0; i < 3; ++i) {736si_cp_copy_data(sctx, &sctx->gfx_cs, COPY_DATA_REG, NULL, (grid_size_reg >> 2) + i,737COPY_DATA_SRC_MEM, si_resource(info->indirect),738info->indirect_offset + 4 * i);739}740radeon_begin_again(cs);741} else {742radeon_set_sh_reg_seq(cs, grid_size_reg, 3);743radeon_emit(cs, info->grid[0]);744radeon_emit(cs, info->grid[1]);745radeon_emit(cs, info->grid[2]);746}747}748749if (sel->info.uses_variable_block_size) {750radeon_set_sh_reg(cs, block_size_reg,751info->block[0] | (info->block[1] << 10) | (info->block[2] << 20));752}753754if (sel->info.base.cs.user_data_components_amd) {755radeon_set_sh_reg_seq(cs, cs_user_data_reg, sel->info.base.cs.user_data_components_amd);756radeon_emit_array(cs, sctx->cs_user_data, sel->info.base.cs.user_data_components_amd);757}758radeon_end();759}760761static void si_emit_dispatch_packets(struct si_context *sctx, const struct pipe_grid_info *info)762{763struct si_screen *sscreen = sctx->screen;764struct radeon_cmdbuf *cs = &sctx->gfx_cs;765bool render_cond_bit = sctx->render_cond_enabled;766unsigned threads_per_threadgroup = info->block[0] * info->block[1] * info->block[2];767unsigned waves_per_threadgroup =768DIV_ROUND_UP(threads_per_threadgroup, sscreen->compute_wave_size);769unsigned threadgroups_per_cu = 1;770771if (sctx->chip_class >= GFX10 && waves_per_threadgroup == 1)772threadgroups_per_cu = 2;773774if (unlikely(sctx->thread_trace_enabled)) {775si_write_event_with_dims_marker(sctx, &sctx->gfx_cs,776info->indirect ? EventCmdDispatchIndirect : EventCmdDispatch,777info->grid[0], info->grid[1], info->grid[2]);778}779780radeon_begin(cs);781radeon_set_sh_reg(782cs, R_00B854_COMPUTE_RESOURCE_LIMITS,783ac_get_compute_resource_limits(&sscreen->info, waves_per_threadgroup,784sctx->cs_max_waves_per_sh, threadgroups_per_cu));785786unsigned dispatch_initiator = S_00B800_COMPUTE_SHADER_EN(1) | S_00B800_FORCE_START_AT_000(1) |787/* If the KMD allows it (there is a KMD hw register for it),788* allow launching waves out-of-order. (same as Vulkan) */789S_00B800_ORDER_MODE(sctx->chip_class >= GFX7) |790S_00B800_CS_W32_EN(sscreen->compute_wave_size == 32);791792const uint *last_block = info->last_block;793bool partial_block_en = last_block[0] || last_block[1] || last_block[2];794795radeon_set_sh_reg_seq(cs, R_00B81C_COMPUTE_NUM_THREAD_X, 3);796797if (partial_block_en) {798unsigned partial[3];799800/* If no partial_block, these should be an entire block size, not 0. */801partial[0] = last_block[0] ? last_block[0] : info->block[0];802partial[1] = last_block[1] ? last_block[1] : info->block[1];803partial[2] = last_block[2] ? last_block[2] : info->block[2];804805radeon_emit(806cs, S_00B81C_NUM_THREAD_FULL(info->block[0]) | S_00B81C_NUM_THREAD_PARTIAL(partial[0]));807radeon_emit(808cs, S_00B820_NUM_THREAD_FULL(info->block[1]) | S_00B820_NUM_THREAD_PARTIAL(partial[1]));809radeon_emit(810cs, S_00B824_NUM_THREAD_FULL(info->block[2]) | S_00B824_NUM_THREAD_PARTIAL(partial[2]));811812dispatch_initiator |= S_00B800_PARTIAL_TG_EN(1);813} else {814radeon_emit(cs, S_00B81C_NUM_THREAD_FULL(info->block[0]));815radeon_emit(cs, S_00B820_NUM_THREAD_FULL(info->block[1]));816radeon_emit(cs, S_00B824_NUM_THREAD_FULL(info->block[2]));817}818819if (info->indirect) {820uint64_t base_va = si_resource(info->indirect)->gpu_address;821822radeon_add_to_buffer_list(sctx, &sctx->gfx_cs, si_resource(info->indirect), RADEON_USAGE_READ,823RADEON_PRIO_DRAW_INDIRECT);824825radeon_emit(cs, PKT3(PKT3_SET_BASE, 2, 0) | PKT3_SHADER_TYPE_S(1));826radeon_emit(cs, 1);827radeon_emit(cs, base_va);828radeon_emit(cs, base_va >> 32);829830radeon_emit(cs, PKT3(PKT3_DISPATCH_INDIRECT, 1, render_cond_bit) | PKT3_SHADER_TYPE_S(1));831radeon_emit(cs, info->indirect_offset);832radeon_emit(cs, dispatch_initiator);833} else {834radeon_emit(cs, PKT3(PKT3_DISPATCH_DIRECT, 3, render_cond_bit) | PKT3_SHADER_TYPE_S(1));835radeon_emit(cs, info->grid[0]);836radeon_emit(cs, info->grid[1]);837radeon_emit(cs, info->grid[2]);838radeon_emit(cs, dispatch_initiator);839}840841if (unlikely(sctx->thread_trace_enabled && sctx->chip_class >= GFX9)) {842radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));843radeon_emit(cs, EVENT_TYPE(V_028A90_THREAD_TRACE_MARKER) | EVENT_INDEX(0));844}845radeon_end();846}847848static bool si_check_needs_implicit_sync(struct si_context *sctx)849{850/* If the compute shader is going to read from a texture/image written by a851* previous draw, we must wait for its completion before continuing.852* Buffers and image stores (from the draw) are not taken into consideration853* because that's the app responsibility.854*855* The OpenGL 4.6 spec says:856*857* buffer object and texture stores performed by shaders are not858* automatically synchronized859*/860struct si_shader_info *info = &sctx->cs_shader_state.program->sel.info;861struct si_samplers *samplers = &sctx->samplers[PIPE_SHADER_COMPUTE];862unsigned mask = samplers->enabled_mask & info->base.textures_used[0];863864while (mask) {865int i = u_bit_scan(&mask);866struct si_sampler_view *sview = (struct si_sampler_view *)samplers->views[i];867868struct si_resource *res = si_resource(sview->base.texture);869if (sctx->ws->cs_is_buffer_referenced(&sctx->gfx_cs, res->buf,870RADEON_USAGE_NEEDS_IMPLICIT_SYNC))871return true;872}873874struct si_images *images = &sctx->images[PIPE_SHADER_COMPUTE];875mask = u_bit_consecutive(0, info->base.num_images) & images->enabled_mask;876877while (mask) {878int i = u_bit_scan(&mask);879struct pipe_image_view *sview = &images->views[i];880881struct si_resource *res = si_resource(sview->resource);882if (sctx->ws->cs_is_buffer_referenced(&sctx->gfx_cs, res->buf,883RADEON_USAGE_NEEDS_IMPLICIT_SYNC))884return true;885}886return false;887}888889static void si_launch_grid(struct pipe_context *ctx, const struct pipe_grid_info *info)890{891struct si_context *sctx = (struct si_context *)ctx;892struct si_screen *sscreen = sctx->screen;893struct si_compute *program = sctx->cs_shader_state.program;894const amd_kernel_code_t *code_object = si_compute_get_code_object(program, info->pc);895int i;896bool cs_regalloc_hang = sscreen->info.has_cs_regalloc_hang_bug &&897info->block[0] * info->block[1] * info->block[2] > 256;898899if (cs_regalloc_hang)900sctx->flags |= SI_CONTEXT_PS_PARTIAL_FLUSH | SI_CONTEXT_CS_PARTIAL_FLUSH;901902if (program->ir_type != PIPE_SHADER_IR_NATIVE && program->shader.compilation_failed)903return;904905if (sctx->has_graphics) {906if (sctx->last_num_draw_calls != sctx->num_draw_calls) {907si_update_fb_dirtiness_after_rendering(sctx);908sctx->last_num_draw_calls = sctx->num_draw_calls;909910if (sctx->force_cb_shader_coherent || si_check_needs_implicit_sync(sctx))911si_make_CB_shader_coherent(sctx, 0,912sctx->framebuffer.CB_has_shader_readable_metadata,913sctx->framebuffer.all_DCC_pipe_aligned);914}915916si_decompress_textures(sctx, 1 << PIPE_SHADER_COMPUTE);917}918919/* Add buffer sizes for memory checking in need_cs_space. */920si_context_add_resource_size(sctx, &program->shader.bo->b.b);921/* TODO: add the scratch buffer */922923if (info->indirect) {924si_context_add_resource_size(sctx, info->indirect);925926/* Indirect buffers use TC L2 on GFX9, but not older hw. */927if (sctx->chip_class <= GFX8 && si_resource(info->indirect)->TC_L2_dirty) {928sctx->flags |= SI_CONTEXT_WB_L2;929si_resource(info->indirect)->TC_L2_dirty = false;930}931}932933si_need_gfx_cs_space(sctx, 0);934935/* If we're using a secure context, determine if cs must be secure or not */936if (unlikely(radeon_uses_secure_bos(sctx->ws))) {937bool secure = si_compute_resources_check_encrypted(sctx);938if (secure != sctx->ws->cs_is_secure(&sctx->gfx_cs)) {939si_flush_gfx_cs(sctx, RADEON_FLUSH_ASYNC_START_NEXT_GFX_IB_NOW |940RADEON_FLUSH_TOGGLE_SECURE_SUBMISSION,941NULL);942}943}944945if (sctx->bo_list_add_all_compute_resources)946si_compute_resources_add_all_to_bo_list(sctx);947948if (!sctx->cs_shader_state.initialized) {949si_emit_initial_compute_regs(sctx, &sctx->gfx_cs);950951sctx->cs_shader_state.emitted_program = NULL;952sctx->cs_shader_state.initialized = true;953}954955/* First emit registers. */956bool prefetch;957if (!si_switch_compute_shader(sctx, program, &program->shader, code_object, info->pc, &prefetch))958return;959960si_upload_compute_shader_descriptors(sctx);961si_emit_compute_shader_pointers(sctx);962963if (program->ir_type == PIPE_SHADER_IR_NATIVE &&964unlikely(!si_upload_compute_input(sctx, code_object, info)))965return;966967/* Global buffers */968for (i = 0; i < program->max_global_buffers; i++) {969struct si_resource *buffer = si_resource(program->global_buffers[i]);970if (!buffer) {971continue;972}973radeon_add_to_buffer_list(sctx, &sctx->gfx_cs, buffer, RADEON_USAGE_READWRITE,974RADEON_PRIO_COMPUTE_GLOBAL);975}976977/* Registers that are not read from memory should be set before this: */978if (sctx->flags)979sctx->emit_cache_flush(sctx, &sctx->gfx_cs);980981if (sctx->has_graphics && si_is_atom_dirty(sctx, &sctx->atoms.s.render_cond)) {982sctx->atoms.s.render_cond.emit(sctx);983si_set_atom_dirty(sctx, &sctx->atoms.s.render_cond, false);984}985986/* Prefetch the compute shader to L2. */987if (sctx->chip_class >= GFX7 && prefetch)988si_cp_dma_prefetch(sctx, &program->shader.bo->b.b, 0, program->shader.bo->b.b.width0);989990if (program->ir_type != PIPE_SHADER_IR_NATIVE)991si_setup_nir_user_data(sctx, info);992993si_emit_dispatch_packets(sctx, info);994995if (unlikely(sctx->current_saved_cs)) {996si_trace_emit(sctx);997si_log_compute_state(sctx, sctx->log);998}9991000/* Mark displayable DCC as dirty for bound images. */1001unsigned display_dcc_store_mask = sctx->images[PIPE_SHADER_COMPUTE].display_dcc_store_mask &1002BITFIELD_MASK(program->sel.info.base.num_images);1003while (display_dcc_store_mask) {1004struct si_texture *tex = (struct si_texture *)1005sctx->images[PIPE_SHADER_COMPUTE].views[u_bit_scan(&display_dcc_store_mask)].resource;10061007si_mark_display_dcc_dirty(sctx, tex);1008}10091010/* TODO: Bindless images don't set displayable_dcc_dirty after image stores. */10111012sctx->compute_is_busy = true;1013sctx->num_compute_calls++;10141015if (cs_regalloc_hang)1016sctx->flags |= SI_CONTEXT_CS_PARTIAL_FLUSH;1017}10181019void si_destroy_compute(struct si_compute *program)1020{1021struct si_shader_selector *sel = &program->sel;10221023if (program->ir_type != PIPE_SHADER_IR_NATIVE) {1024util_queue_drop_job(&sel->screen->shader_compiler_queue, &sel->ready);1025util_queue_fence_destroy(&sel->ready);1026}10271028for (unsigned i = 0; i < program->max_global_buffers; i++)1029pipe_resource_reference(&program->global_buffers[i], NULL);1030FREE(program->global_buffers);10311032si_shader_destroy(&program->shader);1033ralloc_free(program->sel.nir);1034FREE(program);1035}10361037static void si_delete_compute_state(struct pipe_context *ctx, void *state)1038{1039struct si_compute *program = (struct si_compute *)state;1040struct si_context *sctx = (struct si_context *)ctx;10411042if (!state)1043return;10441045if (program == sctx->cs_shader_state.program)1046sctx->cs_shader_state.program = NULL;10471048if (program == sctx->cs_shader_state.emitted_program)1049sctx->cs_shader_state.emitted_program = NULL;10501051si_compute_reference(&program, NULL);1052}10531054static void si_set_compute_resources(struct pipe_context *ctx_, unsigned start, unsigned count,1055struct pipe_surface **surfaces)1056{1057}10581059void si_init_compute_functions(struct si_context *sctx)1060{1061sctx->b.create_compute_state = si_create_compute_state;1062sctx->b.delete_compute_state = si_delete_compute_state;1063sctx->b.bind_compute_state = si_bind_compute_state;1064sctx->b.set_compute_resources = si_set_compute_resources;1065sctx->b.set_global_binding = si_set_global_binding;1066sctx->b.launch_grid = si_launch_grid;1067}106810691070