Path: blob/21.2-virgl/src/amd/vulkan/radv_sqtt.c
7354 views
/*1* Copyright © 2020 Valve Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include <inttypes.h>2425#include "radv_cs.h"26#include "radv_private.h"27#include "sid.h"2829#define SQTT_BUFFER_ALIGN_SHIFT 123031static bool32radv_se_is_disabled(struct radv_device *device, unsigned se)33{34/* No active CU on the SE means it is disabled. */35return device->physical_device->rad_info.cu_mask[se][0] == 0;36}3738static void39radv_emit_thread_trace_start(struct radv_device *device, struct radeon_cmdbuf *cs,40uint32_t queue_family_index)41{42uint32_t shifted_size = device->thread_trace.buffer_size >> SQTT_BUFFER_ALIGN_SHIFT;43struct radeon_info *rad_info = &device->physical_device->rad_info;44unsigned max_se = rad_info->max_se;4546assert(device->physical_device->rad_info.chip_class >= GFX8);4748for (unsigned se = 0; se < max_se; se++) {49uint64_t va = radv_buffer_get_va(device->thread_trace.bo);50uint64_t data_va = ac_thread_trace_get_data_va(rad_info, &device->thread_trace, va, se);51uint64_t shifted_va = data_va >> SQTT_BUFFER_ALIGN_SHIFT;52int first_active_cu = ffs(device->physical_device->rad_info.cu_mask[se][0]);5354if (radv_se_is_disabled(device, se))55continue;5657/* Target SEx and SH0. */58radeon_set_uconfig_reg(59cs, R_030800_GRBM_GFX_INDEX,60S_030800_SE_INDEX(se) | S_030800_SH_INDEX(0) | S_030800_INSTANCE_BROADCAST_WRITES(1));6162if (device->physical_device->rad_info.chip_class >= GFX10) {63/* Order seems important for the following 2 registers. */64radeon_set_privileged_config_reg(65cs, R_008D04_SQ_THREAD_TRACE_BUF0_SIZE,66S_008D04_SIZE(shifted_size) | S_008D04_BASE_HI(shifted_va >> 32));6768radeon_set_privileged_config_reg(cs, R_008D00_SQ_THREAD_TRACE_BUF0_BASE, shifted_va);6970radeon_set_privileged_config_reg(71cs, R_008D14_SQ_THREAD_TRACE_MASK,72S_008D14_WTYPE_INCLUDE(0x7f) | /* all shader stages */73S_008D14_SA_SEL(0) | S_008D14_WGP_SEL(first_active_cu / 2) | S_008D14_SIMD_SEL(0));7475uint32_t thread_trace_token_mask = S_008D18_REG_INCLUDE(76V_008D18_REG_INCLUDE_SQDEC | V_008D18_REG_INCLUDE_SHDEC | V_008D18_REG_INCLUDE_GFXUDEC |77V_008D18_REG_INCLUDE_COMP | V_008D18_REG_INCLUDE_CONTEXT | V_008D18_REG_INCLUDE_CONFIG);7879/* Performance counters with SQTT are considered80* deprecated.81*/82thread_trace_token_mask |= S_008D18_TOKEN_EXCLUDE(V_008D18_TOKEN_EXCLUDE_PERF);8384radeon_set_privileged_config_reg(cs, R_008D18_SQ_THREAD_TRACE_TOKEN_MASK,85thread_trace_token_mask);8687uint32_t thread_trace_ctrl = S_008D1C_MODE(1) | S_008D1C_HIWATER(5) |88S_008D1C_UTIL_TIMER(1) | S_008D1C_RT_FREQ(2) | /* 4096 clk */89S_008D1C_DRAW_EVENT_EN(1) | S_008D1C_REG_STALL_EN(1) |90S_008D1C_SPI_STALL_EN(1) | S_008D1C_SQ_STALL_EN(1) |91S_008D1C_REG_DROP_ON_STALL(0);9293if (device->physical_device->rad_info.chip_class == GFX10_3)94thread_trace_ctrl |= S_008D1C_LOWATER_OFFSET(4);9596/* Should be emitted last (it enables thread traces). */97radeon_set_privileged_config_reg(cs, R_008D1C_SQ_THREAD_TRACE_CTRL, thread_trace_ctrl);98} else {99/* Order seems important for the following 4 registers. */100radeon_set_uconfig_reg(cs, R_030CDC_SQ_THREAD_TRACE_BASE2,101S_030CDC_ADDR_HI(shifted_va >> 32));102103radeon_set_uconfig_reg(cs, R_030CC0_SQ_THREAD_TRACE_BASE, shifted_va);104105radeon_set_uconfig_reg(cs, R_030CC4_SQ_THREAD_TRACE_SIZE, S_030CC4_SIZE(shifted_size));106107radeon_set_uconfig_reg(cs, R_030CD4_SQ_THREAD_TRACE_CTRL, S_030CD4_RESET_BUFFER(1));108109uint32_t thread_trace_mask = S_030CC8_CU_SEL(first_active_cu) | S_030CC8_SH_SEL(0) |110S_030CC8_SIMD_EN(0xf) | S_030CC8_VM_ID_MASK(0) |111S_030CC8_REG_STALL_EN(1) | S_030CC8_SPI_STALL_EN(1) |112S_030CC8_SQ_STALL_EN(1);113114if (device->physical_device->rad_info.chip_class < GFX9) {115thread_trace_mask |= S_030CC8_RANDOM_SEED(0xffff);116}117118radeon_set_uconfig_reg(cs, R_030CC8_SQ_THREAD_TRACE_MASK, thread_trace_mask);119120/* Trace all tokens and registers. */121radeon_set_uconfig_reg(122cs, R_030CCC_SQ_THREAD_TRACE_TOKEN_MASK,123S_030CCC_TOKEN_MASK(0xbfff) | S_030CCC_REG_MASK(0xff) | S_030CCC_REG_DROP_ON_STALL(0));124125/* Enable SQTT perf counters for all CUs. */126radeon_set_uconfig_reg(cs, R_030CD0_SQ_THREAD_TRACE_PERF_MASK,127S_030CD0_SH0_MASK(0xffff) | S_030CD0_SH1_MASK(0xffff));128129radeon_set_uconfig_reg(cs, R_030CE0_SQ_THREAD_TRACE_TOKEN_MASK2, 0xffffffff);130131radeon_set_uconfig_reg(cs, R_030CEC_SQ_THREAD_TRACE_HIWATER, S_030CEC_HIWATER(4));132133if (device->physical_device->rad_info.chip_class == GFX9) {134/* Reset thread trace status errors. */135radeon_set_uconfig_reg(cs, R_030CE8_SQ_THREAD_TRACE_STATUS, S_030CE8_UTC_ERROR(0));136}137138/* Enable the thread trace mode. */139uint32_t thread_trace_mode =140S_030CD8_MASK_PS(1) | S_030CD8_MASK_VS(1) | S_030CD8_MASK_GS(1) | S_030CD8_MASK_ES(1) |141S_030CD8_MASK_HS(1) | S_030CD8_MASK_LS(1) | S_030CD8_MASK_CS(1) |142S_030CD8_AUTOFLUSH_EN(1) | /* periodically flush SQTT data to memory */143S_030CD8_MODE(1);144145if (device->physical_device->rad_info.chip_class == GFX9) {146/* Count SQTT traffic in TCC perf counters. */147thread_trace_mode |= S_030CD8_TC_PERF_EN(1);148}149150radeon_set_uconfig_reg(cs, R_030CD8_SQ_THREAD_TRACE_MODE, thread_trace_mode);151}152}153154/* Restore global broadcasting. */155radeon_set_uconfig_reg(cs, R_030800_GRBM_GFX_INDEX,156S_030800_SE_BROADCAST_WRITES(1) | S_030800_SH_BROADCAST_WRITES(1) |157S_030800_INSTANCE_BROADCAST_WRITES(1));158159/* Start the thread trace with a different event based on the queue. */160if (queue_family_index == RADV_QUEUE_COMPUTE &&161device->physical_device->rad_info.chip_class >= GFX7) {162radeon_set_sh_reg(cs, R_00B878_COMPUTE_THREAD_TRACE_ENABLE, S_00B878_THREAD_TRACE_ENABLE(1));163} else {164radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));165radeon_emit(cs, EVENT_TYPE(V_028A90_THREAD_TRACE_START) | EVENT_INDEX(0));166}167}168169static const uint32_t gfx8_thread_trace_info_regs[] = {170R_030CE4_SQ_THREAD_TRACE_WPTR,171R_030CE8_SQ_THREAD_TRACE_STATUS,172R_008E40_SQ_THREAD_TRACE_CNTR,173};174175static const uint32_t gfx9_thread_trace_info_regs[] = {176R_030CE4_SQ_THREAD_TRACE_WPTR,177R_030CE8_SQ_THREAD_TRACE_STATUS,178R_030CF0_SQ_THREAD_TRACE_CNTR,179};180181static const uint32_t gfx10_thread_trace_info_regs[] = {182R_008D10_SQ_THREAD_TRACE_WPTR,183R_008D20_SQ_THREAD_TRACE_STATUS,184R_008D24_SQ_THREAD_TRACE_DROPPED_CNTR,185};186187static void188radv_copy_thread_trace_info_regs(struct radv_device *device, struct radeon_cmdbuf *cs,189unsigned se_index)190{191const uint32_t *thread_trace_info_regs = NULL;192193if (device->physical_device->rad_info.chip_class >= GFX10) {194thread_trace_info_regs = gfx10_thread_trace_info_regs;195} else if (device->physical_device->rad_info.chip_class == GFX9) {196thread_trace_info_regs = gfx9_thread_trace_info_regs;197} else {198assert(device->physical_device->rad_info.chip_class == GFX8);199thread_trace_info_regs = gfx8_thread_trace_info_regs;200}201202/* Get the VA where the info struct is stored for this SE. */203uint64_t va = radv_buffer_get_va(device->thread_trace.bo);204uint64_t info_va = ac_thread_trace_get_info_va(va, se_index);205206/* Copy back the info struct one DWORD at a time. */207for (unsigned i = 0; i < 3; i++) {208radeon_emit(cs, PKT3(PKT3_COPY_DATA, 4, 0));209radeon_emit(cs, COPY_DATA_SRC_SEL(COPY_DATA_PERF) | COPY_DATA_DST_SEL(COPY_DATA_TC_L2) |210COPY_DATA_WR_CONFIRM);211radeon_emit(cs, thread_trace_info_regs[i] >> 2);212radeon_emit(cs, 0); /* unused */213radeon_emit(cs, (info_va + i * 4));214radeon_emit(cs, (info_va + i * 4) >> 32);215}216}217218static void219radv_emit_thread_trace_stop(struct radv_device *device, struct radeon_cmdbuf *cs,220uint32_t queue_family_index)221{222unsigned max_se = device->physical_device->rad_info.max_se;223224assert(device->physical_device->rad_info.chip_class >= GFX8);225226/* Stop the thread trace with a different event based on the queue. */227if (queue_family_index == RADV_QUEUE_COMPUTE &&228device->physical_device->rad_info.chip_class >= GFX7) {229radeon_set_sh_reg(cs, R_00B878_COMPUTE_THREAD_TRACE_ENABLE, S_00B878_THREAD_TRACE_ENABLE(0));230} else {231radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));232radeon_emit(cs, EVENT_TYPE(V_028A90_THREAD_TRACE_STOP) | EVENT_INDEX(0));233}234235radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));236radeon_emit(cs, EVENT_TYPE(V_028A90_THREAD_TRACE_FINISH) | EVENT_INDEX(0));237238for (unsigned se = 0; se < max_se; se++) {239if (radv_se_is_disabled(device, se))240continue;241242/* Target SEi and SH0. */243radeon_set_uconfig_reg(244cs, R_030800_GRBM_GFX_INDEX,245S_030800_SE_INDEX(se) | S_030800_SH_INDEX(0) | S_030800_INSTANCE_BROADCAST_WRITES(1));246247if (device->physical_device->rad_info.chip_class >= GFX10) {248/* Make sure to wait for the trace buffer. */249radeon_emit(cs, PKT3(PKT3_WAIT_REG_MEM, 5, 0));250radeon_emit(251cs,252WAIT_REG_MEM_NOT_EQUAL); /* wait until the register is equal to the reference value */253radeon_emit(cs, R_008D20_SQ_THREAD_TRACE_STATUS >> 2); /* register */254radeon_emit(cs, 0);255radeon_emit(cs, 0); /* reference value */256radeon_emit(cs, S_008D20_FINISH_DONE(1)); /* mask */257radeon_emit(cs, 4); /* poll interval */258259/* Disable the thread trace mode. */260radeon_set_privileged_config_reg(cs, R_008D1C_SQ_THREAD_TRACE_CTRL, S_008D1C_MODE(0));261262/* Wait for thread trace completion. */263radeon_emit(cs, PKT3(PKT3_WAIT_REG_MEM, 5, 0));264radeon_emit(265cs, WAIT_REG_MEM_EQUAL); /* wait until the register is equal to the reference value */266radeon_emit(cs, R_008D20_SQ_THREAD_TRACE_STATUS >> 2); /* register */267radeon_emit(cs, 0);268radeon_emit(cs, 0); /* reference value */269radeon_emit(cs, S_008D20_BUSY(1)); /* mask */270radeon_emit(cs, 4); /* poll interval */271} else {272/* Disable the thread trace mode. */273radeon_set_uconfig_reg(cs, R_030CD8_SQ_THREAD_TRACE_MODE, S_030CD8_MODE(0));274275/* Wait for thread trace completion. */276radeon_emit(cs, PKT3(PKT3_WAIT_REG_MEM, 5, 0));277radeon_emit(278cs, WAIT_REG_MEM_EQUAL); /* wait until the register is equal to the reference value */279radeon_emit(cs, R_030CE8_SQ_THREAD_TRACE_STATUS >> 2); /* register */280radeon_emit(cs, 0);281radeon_emit(cs, 0); /* reference value */282radeon_emit(cs, S_030CE8_BUSY(1)); /* mask */283radeon_emit(cs, 4); /* poll interval */284}285286radv_copy_thread_trace_info_regs(device, cs, se);287}288289/* Restore global broadcasting. */290radeon_set_uconfig_reg(cs, R_030800_GRBM_GFX_INDEX,291S_030800_SE_BROADCAST_WRITES(1) | S_030800_SH_BROADCAST_WRITES(1) |292S_030800_INSTANCE_BROADCAST_WRITES(1));293}294295void296radv_emit_thread_trace_userdata(const struct radv_device *device, struct radeon_cmdbuf *cs,297const void *data, uint32_t num_dwords)298{299const uint32_t *dwords = (uint32_t *)data;300301while (num_dwords > 0) {302uint32_t count = MIN2(num_dwords, 2);303304radeon_check_space(device->ws, cs, 2 + count);305306/* Without the perfctr bit the CP might not always pass the307* write on correctly. */308if (device->physical_device->rad_info.chip_class >= GFX10)309radeon_set_uconfig_reg_seq_perfctr(cs, R_030D08_SQ_THREAD_TRACE_USERDATA_2, count);310else311radeon_set_uconfig_reg_seq(cs, R_030D08_SQ_THREAD_TRACE_USERDATA_2, count);312radeon_emit_array(cs, dwords, count);313314dwords += count;315num_dwords -= count;316}317}318319static void320radv_emit_spi_config_cntl(struct radv_device *device, struct radeon_cmdbuf *cs, bool enable)321{322if (device->physical_device->rad_info.chip_class >= GFX9) {323uint32_t spi_config_cntl =324S_031100_GPR_WRITE_PRIORITY(0x2c688) | S_031100_EXP_PRIORITY_ORDER(3) |325S_031100_ENABLE_SQG_TOP_EVENTS(enable) | S_031100_ENABLE_SQG_BOP_EVENTS(enable);326327if (device->physical_device->rad_info.chip_class >= GFX10)328spi_config_cntl |= S_031100_PS_PKR_PRIORITY_CNTL(3);329330radeon_set_uconfig_reg(cs, R_031100_SPI_CONFIG_CNTL, spi_config_cntl);331} else {332/* SPI_CONFIG_CNTL is a protected register on GFX6-GFX8. */333radeon_set_privileged_config_reg(334cs, R_009100_SPI_CONFIG_CNTL,335S_009100_ENABLE_SQG_TOP_EVENTS(enable) | S_009100_ENABLE_SQG_BOP_EVENTS(enable));336}337}338339static void340radv_emit_inhibit_clockgating(struct radv_device *device, struct radeon_cmdbuf *cs, bool inhibit)341{342if (device->physical_device->rad_info.chip_class >= GFX10) {343radeon_set_uconfig_reg(cs, R_037390_RLC_PERFMON_CLK_CNTL,344S_037390_PERFMON_CLOCK_STATE(inhibit));345} else if (device->physical_device->rad_info.chip_class >= GFX8) {346radeon_set_uconfig_reg(cs, R_0372FC_RLC_PERFMON_CLK_CNTL,347S_0372FC_PERFMON_CLOCK_STATE(inhibit));348}349}350351static void352radv_emit_wait_for_idle(struct radv_device *device, struct radeon_cmdbuf *cs, int family)353{354enum rgp_flush_bits sqtt_flush_bits = 0;355si_cs_emit_cache_flush(356cs, device->physical_device->rad_info.chip_class, NULL, 0,357family == RING_COMPUTE && device->physical_device->rad_info.chip_class >= GFX7,358(family == RADV_QUEUE_COMPUTE359? RADV_CMD_FLAG_CS_PARTIAL_FLUSH360: (RADV_CMD_FLAG_CS_PARTIAL_FLUSH | RADV_CMD_FLAG_PS_PARTIAL_FLUSH)) |361RADV_CMD_FLAG_INV_ICACHE | RADV_CMD_FLAG_INV_SCACHE | RADV_CMD_FLAG_INV_VCACHE |362RADV_CMD_FLAG_INV_L2,363&sqtt_flush_bits, 0);364}365366static bool367radv_thread_trace_init_bo(struct radv_device *device)368{369unsigned max_se = device->physical_device->rad_info.max_se;370struct radeon_winsys *ws = device->ws;371uint64_t size;372373/* The buffer size and address need to be aligned in HW regs. Align the374* size as early as possible so that we do all the allocation & addressing375* correctly. */376device->thread_trace.buffer_size =377align64(device->thread_trace.buffer_size, 1u << SQTT_BUFFER_ALIGN_SHIFT);378379/* Compute total size of the thread trace BO for all SEs. */380size = align64(sizeof(struct ac_thread_trace_info) * max_se, 1 << SQTT_BUFFER_ALIGN_SHIFT);381size += device->thread_trace.buffer_size * (uint64_t)max_se;382383struct radeon_winsys_bo *bo = NULL;384VkResult result = ws->buffer_create(385ws, size, 4096, RADEON_DOMAIN_VRAM,386RADEON_FLAG_CPU_ACCESS | RADEON_FLAG_NO_INTERPROCESS_SHARING | RADEON_FLAG_ZERO_VRAM,387RADV_BO_PRIORITY_SCRATCH, 0, &bo);388device->thread_trace.bo = bo;389if (result != VK_SUCCESS)390return false;391392device->thread_trace.ptr = ws->buffer_map(device->thread_trace.bo);393if (!device->thread_trace.ptr)394return false;395396return true;397}398399bool400radv_thread_trace_init(struct radv_device *device)401{402struct ac_thread_trace_data *thread_trace_data = &device->thread_trace;403404/* Default buffer size set to 32MB per SE. */405device->thread_trace.buffer_size =406radv_get_int_debug_option("RADV_THREAD_TRACE_BUFFER_SIZE", 32 * 1024 * 1024);407device->thread_trace.start_frame = radv_get_int_debug_option("RADV_THREAD_TRACE", -1);408409const char *trigger_file = getenv("RADV_THREAD_TRACE_TRIGGER");410if (trigger_file)411device->thread_trace.trigger_file = strdup(trigger_file);412413if (!radv_thread_trace_init_bo(device))414return false;415416list_inithead(&thread_trace_data->rgp_pso_correlation.record);417simple_mtx_init(&thread_trace_data->rgp_pso_correlation.lock, mtx_plain);418419list_inithead(&thread_trace_data->rgp_loader_events.record);420simple_mtx_init(&thread_trace_data->rgp_loader_events.lock, mtx_plain);421422list_inithead(&thread_trace_data->rgp_code_object.record);423simple_mtx_init(&thread_trace_data->rgp_code_object.lock, mtx_plain);424425return true;426}427428void429radv_thread_trace_finish(struct radv_device *device)430{431struct ac_thread_trace_data *thread_trace_data = &device->thread_trace;432struct radeon_winsys *ws = device->ws;433434if (unlikely(device->thread_trace.bo))435ws->buffer_destroy(ws, device->thread_trace.bo);436437for (unsigned i = 0; i < 2; i++) {438if (device->thread_trace.start_cs[i])439ws->cs_destroy(device->thread_trace.start_cs[i]);440if (device->thread_trace.stop_cs[i])441ws->cs_destroy(device->thread_trace.stop_cs[i]);442}443444assert(thread_trace_data->rgp_pso_correlation.record_count == 0);445simple_mtx_destroy(&thread_trace_data->rgp_pso_correlation.lock);446447assert(thread_trace_data->rgp_loader_events.record_count == 0);448simple_mtx_destroy(&thread_trace_data->rgp_loader_events.lock);449450assert(thread_trace_data->rgp_code_object.record_count == 0);451simple_mtx_destroy(&thread_trace_data->rgp_code_object.lock);452}453454static bool455radv_thread_trace_resize_bo(struct radv_device *device)456{457struct radeon_winsys *ws = device->ws;458459/* Destroy the previous thread trace BO. */460ws->buffer_destroy(ws, device->thread_trace.bo);461462/* Double the size of the thread trace buffer per SE. */463device->thread_trace.buffer_size *= 2;464465fprintf(stderr,466"Failed to get the thread trace because the buffer "467"was too small, resizing to %d KB\n",468device->thread_trace.buffer_size / 1024);469470/* Re-create the thread trace BO. */471return radv_thread_trace_init_bo(device);472}473474bool475radv_begin_thread_trace(struct radv_queue *queue)476{477struct radv_device *device = queue->device;478int family = queue->queue_family_index;479struct radeon_winsys *ws = device->ws;480struct radeon_cmdbuf *cs;481VkResult result;482483/* Destroy the previous start CS and create a new one. */484if (device->thread_trace.start_cs[family]) {485ws->cs_destroy(device->thread_trace.start_cs[family]);486device->thread_trace.start_cs[family] = NULL;487}488489cs = ws->cs_create(ws, family);490if (!cs)491return false;492493switch (family) {494case RADV_QUEUE_GENERAL:495radeon_emit(cs, PKT3(PKT3_CONTEXT_CONTROL, 1, 0));496radeon_emit(cs, CC0_UPDATE_LOAD_ENABLES(1));497radeon_emit(cs, CC1_UPDATE_SHADOW_ENABLES(1));498break;499case RADV_QUEUE_COMPUTE:500radeon_emit(cs, PKT3(PKT3_NOP, 0, 0));501radeon_emit(cs, 0);502break;503}504505radv_cs_add_buffer(ws, cs, device->thread_trace.bo);506507/* Make sure to wait-for-idle before starting SQTT. */508radv_emit_wait_for_idle(device, cs, family);509510/* Disable clock gating before starting SQTT. */511radv_emit_inhibit_clockgating(device, cs, true);512513/* Enable SQG events that collects thread trace data. */514radv_emit_spi_config_cntl(device, cs, true);515516/* Start SQTT. */517radv_emit_thread_trace_start(device, cs, family);518519result = ws->cs_finalize(cs);520if (result != VK_SUCCESS) {521ws->cs_destroy(cs);522return false;523}524525device->thread_trace.start_cs[family] = cs;526527return radv_queue_internal_submit(queue, cs);528}529530bool531radv_end_thread_trace(struct radv_queue *queue)532{533struct radv_device *device = queue->device;534int family = queue->queue_family_index;535struct radeon_winsys *ws = device->ws;536struct radeon_cmdbuf *cs;537VkResult result;538539/* Destroy the previous stop CS and create a new one. */540if (queue->device->thread_trace.stop_cs[family]) {541ws->cs_destroy(device->thread_trace.stop_cs[family]);542device->thread_trace.stop_cs[family] = NULL;543}544545cs = ws->cs_create(ws, family);546if (!cs)547return false;548549switch (family) {550case RADV_QUEUE_GENERAL:551radeon_emit(cs, PKT3(PKT3_CONTEXT_CONTROL, 1, 0));552radeon_emit(cs, CC0_UPDATE_LOAD_ENABLES(1));553radeon_emit(cs, CC1_UPDATE_SHADOW_ENABLES(1));554break;555case RADV_QUEUE_COMPUTE:556radeon_emit(cs, PKT3(PKT3_NOP, 0, 0));557radeon_emit(cs, 0);558break;559}560561radv_cs_add_buffer(ws, cs, device->thread_trace.bo);562563/* Make sure to wait-for-idle before stopping SQTT. */564radv_emit_wait_for_idle(device, cs, family);565566/* Stop SQTT. */567radv_emit_thread_trace_stop(device, cs, family);568569/* Restore previous state by disabling SQG events. */570radv_emit_spi_config_cntl(device, cs, false);571572/* Restore previous state by re-enabling clock gating. */573radv_emit_inhibit_clockgating(device, cs, false);574575result = ws->cs_finalize(cs);576if (result != VK_SUCCESS) {577ws->cs_destroy(cs);578return false;579}580581device->thread_trace.stop_cs[family] = cs;582583return radv_queue_internal_submit(queue, cs);584}585586bool587radv_get_thread_trace(struct radv_queue *queue, struct ac_thread_trace *thread_trace)588{589struct radv_device *device = queue->device;590struct radeon_info *rad_info = &device->physical_device->rad_info;591unsigned max_se = rad_info->max_se;592void *thread_trace_ptr = device->thread_trace.ptr;593594memset(thread_trace, 0, sizeof(*thread_trace));595596for (unsigned se = 0; se < max_se; se++) {597uint64_t info_offset = ac_thread_trace_get_info_offset(se);598uint64_t data_offset = ac_thread_trace_get_data_offset(rad_info, &device->thread_trace, se);599void *info_ptr = (uint8_t *)thread_trace_ptr + info_offset;600void *data_ptr = (uint8_t *)thread_trace_ptr + data_offset;601struct ac_thread_trace_info *info = (struct ac_thread_trace_info *)info_ptr;602struct ac_thread_trace_se thread_trace_se = {0};603int first_active_cu = ffs(device->physical_device->rad_info.cu_mask[se][0]);604605if (radv_se_is_disabled(device, se))606continue;607608if (!ac_is_thread_trace_complete(&device->physical_device->rad_info, &device->thread_trace,609info)) {610if (!radv_thread_trace_resize_bo(device)) {611fprintf(stderr, "Failed to resize the thread "612"trace buffer.\n");613abort();614}615return false;616}617618thread_trace_se.data_ptr = data_ptr;619thread_trace_se.info = *info;620thread_trace_se.shader_engine = se;621622/* RGP seems to expect units of WGP on GFX10+. */623thread_trace_se.compute_unit = device->physical_device->rad_info.chip_class >= GFX10624? (first_active_cu / 2)625: first_active_cu;626627thread_trace->traces[thread_trace->num_traces] = thread_trace_se;628thread_trace->num_traces++;629}630631thread_trace->data = &device->thread_trace;632return true;633}634635636