xref: /third_party/mesa3d/src/amd/vulkan/radv_sqtt.c (revision bf215546)
1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2020 Valve Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include <inttypes.h>
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#include "radv_cs.h"
27bf215546Sopenharmony_ci#include "radv_private.h"
28bf215546Sopenharmony_ci#include "sid.h"
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#define SQTT_BUFFER_ALIGN_SHIFT 12
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_cibool
33bf215546Sopenharmony_ciradv_is_instruction_timing_enabled(void)
34bf215546Sopenharmony_ci{
35bf215546Sopenharmony_ci   return debug_get_bool_option("RADV_THREAD_TRACE_INSTRUCTION_TIMING", true);
36bf215546Sopenharmony_ci}
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_cistatic bool
39bf215546Sopenharmony_ciradv_se_is_disabled(struct radv_device *device, unsigned se)
40bf215546Sopenharmony_ci{
41bf215546Sopenharmony_ci   /* No active CU on the SE means it is disabled. */
42bf215546Sopenharmony_ci   return device->physical_device->rad_info.cu_mask[se][0] == 0;
43bf215546Sopenharmony_ci}
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_cistatic uint32_t
46bf215546Sopenharmony_cigfx10_get_thread_trace_ctrl(struct radv_device *device, bool enable)
47bf215546Sopenharmony_ci{
48bf215546Sopenharmony_ci   uint32_t thread_trace_ctrl = S_008D1C_MODE(enable) | S_008D1C_HIWATER(5) |
49bf215546Sopenharmony_ci                                S_008D1C_UTIL_TIMER(1) | S_008D1C_RT_FREQ(2) | /* 4096 clk */
50bf215546Sopenharmony_ci                                S_008D1C_DRAW_EVENT_EN(1) | S_008D1C_REG_STALL_EN(1) |
51bf215546Sopenharmony_ci                                S_008D1C_SPI_STALL_EN(1) | S_008D1C_SQ_STALL_EN(1) |
52bf215546Sopenharmony_ci                                S_008D1C_REG_DROP_ON_STALL(0);
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci   if (device->physical_device->rad_info.gfx_level == GFX10_3)
55bf215546Sopenharmony_ci      thread_trace_ctrl |= S_008D1C_LOWATER_OFFSET(4);
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci   if (device->physical_device->rad_info.has_sqtt_auto_flush_mode_bug)
58bf215546Sopenharmony_ci      thread_trace_ctrl |= S_008D1C_AUTO_FLUSH_MODE(1);
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_ci   return thread_trace_ctrl;
61bf215546Sopenharmony_ci}
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_cistatic void
64bf215546Sopenharmony_ciradv_emit_wait_for_idle(struct radv_device *device, struct radeon_cmdbuf *cs, int family)
65bf215546Sopenharmony_ci{
66bf215546Sopenharmony_ci   enum rgp_flush_bits sqtt_flush_bits = 0;
67bf215546Sopenharmony_ci   si_cs_emit_cache_flush(
68bf215546Sopenharmony_ci      cs, device->physical_device->rad_info.gfx_level, NULL, 0,
69bf215546Sopenharmony_ci      family == AMD_IP_COMPUTE && device->physical_device->rad_info.gfx_level >= GFX7,
70bf215546Sopenharmony_ci      (family == RADV_QUEUE_COMPUTE
71bf215546Sopenharmony_ci          ? RADV_CMD_FLAG_CS_PARTIAL_FLUSH
72bf215546Sopenharmony_ci          : (RADV_CMD_FLAG_CS_PARTIAL_FLUSH | RADV_CMD_FLAG_PS_PARTIAL_FLUSH)) |
73bf215546Sopenharmony_ci         RADV_CMD_FLAG_INV_ICACHE | RADV_CMD_FLAG_INV_SCACHE | RADV_CMD_FLAG_INV_VCACHE |
74bf215546Sopenharmony_ci         RADV_CMD_FLAG_INV_L2,
75bf215546Sopenharmony_ci      &sqtt_flush_bits, 0);
76bf215546Sopenharmony_ci}
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_cistatic void
79bf215546Sopenharmony_ciradv_emit_thread_trace_start(struct radv_device *device, struct radeon_cmdbuf *cs,
80bf215546Sopenharmony_ci                             enum radv_queue_family qf)
81bf215546Sopenharmony_ci{
82bf215546Sopenharmony_ci   uint32_t shifted_size = device->thread_trace.buffer_size >> SQTT_BUFFER_ALIGN_SHIFT;
83bf215546Sopenharmony_ci   struct radeon_info *rad_info = &device->physical_device->rad_info;
84bf215546Sopenharmony_ci   unsigned max_se = rad_info->max_se;
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci   for (unsigned se = 0; se < max_se; se++) {
87bf215546Sopenharmony_ci      uint64_t va = radv_buffer_get_va(device->thread_trace.bo);
88bf215546Sopenharmony_ci      uint64_t data_va = ac_thread_trace_get_data_va(rad_info, &device->thread_trace, va, se);
89bf215546Sopenharmony_ci      uint64_t shifted_va = data_va >> SQTT_BUFFER_ALIGN_SHIFT;
90bf215546Sopenharmony_ci      int first_active_cu = ffs(device->physical_device->rad_info.cu_mask[se][0]);
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci      if (radv_se_is_disabled(device, se))
93bf215546Sopenharmony_ci         continue;
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_ci      /* Target SEx and SH0. */
96bf215546Sopenharmony_ci      radeon_set_uconfig_reg(
97bf215546Sopenharmony_ci         cs, R_030800_GRBM_GFX_INDEX,
98bf215546Sopenharmony_ci         S_030800_SE_INDEX(se) | S_030800_SH_INDEX(0) | S_030800_INSTANCE_BROADCAST_WRITES(1));
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_ci      if (device->physical_device->rad_info.gfx_level >= GFX10) {
101bf215546Sopenharmony_ci         /* Order seems important for the following 2 registers. */
102bf215546Sopenharmony_ci         radeon_set_privileged_config_reg(
103bf215546Sopenharmony_ci            cs, R_008D04_SQ_THREAD_TRACE_BUF0_SIZE,
104bf215546Sopenharmony_ci            S_008D04_SIZE(shifted_size) | S_008D04_BASE_HI(shifted_va >> 32));
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci         radeon_set_privileged_config_reg(cs, R_008D00_SQ_THREAD_TRACE_BUF0_BASE, shifted_va);
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_ci         radeon_set_privileged_config_reg(
109bf215546Sopenharmony_ci            cs, R_008D14_SQ_THREAD_TRACE_MASK,
110bf215546Sopenharmony_ci            S_008D14_WTYPE_INCLUDE(0x7f) | /* all shader stages */
111bf215546Sopenharmony_ci               S_008D14_SA_SEL(0) | S_008D14_WGP_SEL(first_active_cu / 2) | S_008D14_SIMD_SEL(0));
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_ci         uint32_t thread_trace_token_mask = S_008D18_REG_INCLUDE(
114bf215546Sopenharmony_ci            V_008D18_REG_INCLUDE_SQDEC | V_008D18_REG_INCLUDE_SHDEC | V_008D18_REG_INCLUDE_GFXUDEC |
115bf215546Sopenharmony_ci            V_008D18_REG_INCLUDE_COMP | V_008D18_REG_INCLUDE_CONTEXT | V_008D18_REG_INCLUDE_CONFIG);
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_ci         /* Performance counters with SQTT are considered deprecated. */
118bf215546Sopenharmony_ci         uint32_t token_exclude = V_008D18_TOKEN_EXCLUDE_PERF;
119bf215546Sopenharmony_ci
120bf215546Sopenharmony_ci         if (!radv_is_instruction_timing_enabled()) {
121bf215546Sopenharmony_ci            /* Reduce SQTT traffic when instruction timing isn't enabled. */
122bf215546Sopenharmony_ci            token_exclude |= V_008D18_TOKEN_EXCLUDE_VMEMEXEC |
123bf215546Sopenharmony_ci                             V_008D18_TOKEN_EXCLUDE_ALUEXEC |
124bf215546Sopenharmony_ci                             V_008D18_TOKEN_EXCLUDE_VALUINST |
125bf215546Sopenharmony_ci                             V_008D18_TOKEN_EXCLUDE_IMMEDIATE |
126bf215546Sopenharmony_ci                             V_008D18_TOKEN_EXCLUDE_INST;
127bf215546Sopenharmony_ci         }
128bf215546Sopenharmony_ci         thread_trace_token_mask |= S_008D18_TOKEN_EXCLUDE(token_exclude);
129bf215546Sopenharmony_ci
130bf215546Sopenharmony_ci         radeon_set_privileged_config_reg(cs, R_008D18_SQ_THREAD_TRACE_TOKEN_MASK,
131bf215546Sopenharmony_ci                                          thread_trace_token_mask);
132bf215546Sopenharmony_ci
133bf215546Sopenharmony_ci         /* Should be emitted last (it enables thread traces). */
134bf215546Sopenharmony_ci         radeon_set_privileged_config_reg(cs, R_008D1C_SQ_THREAD_TRACE_CTRL,
135bf215546Sopenharmony_ci                                          gfx10_get_thread_trace_ctrl(device, true));
136bf215546Sopenharmony_ci      } else {
137bf215546Sopenharmony_ci         /* Order seems important for the following 4 registers. */
138bf215546Sopenharmony_ci         radeon_set_uconfig_reg(cs, R_030CDC_SQ_THREAD_TRACE_BASE2,
139bf215546Sopenharmony_ci                                S_030CDC_ADDR_HI(shifted_va >> 32));
140bf215546Sopenharmony_ci
141bf215546Sopenharmony_ci         radeon_set_uconfig_reg(cs, R_030CC0_SQ_THREAD_TRACE_BASE, shifted_va);
142bf215546Sopenharmony_ci
143bf215546Sopenharmony_ci         radeon_set_uconfig_reg(cs, R_030CC4_SQ_THREAD_TRACE_SIZE, S_030CC4_SIZE(shifted_size));
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_ci         radeon_set_uconfig_reg(cs, R_030CD4_SQ_THREAD_TRACE_CTRL, S_030CD4_RESET_BUFFER(1));
146bf215546Sopenharmony_ci
147bf215546Sopenharmony_ci         uint32_t thread_trace_mask = S_030CC8_CU_SEL(first_active_cu) | S_030CC8_SH_SEL(0) |
148bf215546Sopenharmony_ci                                      S_030CC8_SIMD_EN(0xf) | S_030CC8_VM_ID_MASK(0) |
149bf215546Sopenharmony_ci                                      S_030CC8_REG_STALL_EN(1) | S_030CC8_SPI_STALL_EN(1) |
150bf215546Sopenharmony_ci                                      S_030CC8_SQ_STALL_EN(1);
151bf215546Sopenharmony_ci
152bf215546Sopenharmony_ci         if (device->physical_device->rad_info.gfx_level < GFX9) {
153bf215546Sopenharmony_ci            thread_trace_mask |= S_030CC8_RANDOM_SEED(0xffff);
154bf215546Sopenharmony_ci         }
155bf215546Sopenharmony_ci
156bf215546Sopenharmony_ci         radeon_set_uconfig_reg(cs, R_030CC8_SQ_THREAD_TRACE_MASK, thread_trace_mask);
157bf215546Sopenharmony_ci
158bf215546Sopenharmony_ci         /* Trace all tokens and registers. */
159bf215546Sopenharmony_ci         radeon_set_uconfig_reg(
160bf215546Sopenharmony_ci            cs, R_030CCC_SQ_THREAD_TRACE_TOKEN_MASK,
161bf215546Sopenharmony_ci            S_030CCC_TOKEN_MASK(0xbfff) | S_030CCC_REG_MASK(0xff) | S_030CCC_REG_DROP_ON_STALL(0));
162bf215546Sopenharmony_ci
163bf215546Sopenharmony_ci         /* Enable SQTT perf counters for all CUs. */
164bf215546Sopenharmony_ci         radeon_set_uconfig_reg(cs, R_030CD0_SQ_THREAD_TRACE_PERF_MASK,
165bf215546Sopenharmony_ci                                S_030CD0_SH0_MASK(0xffff) | S_030CD0_SH1_MASK(0xffff));
166bf215546Sopenharmony_ci
167bf215546Sopenharmony_ci         radeon_set_uconfig_reg(cs, R_030CE0_SQ_THREAD_TRACE_TOKEN_MASK2, 0xffffffff);
168bf215546Sopenharmony_ci
169bf215546Sopenharmony_ci         radeon_set_uconfig_reg(cs, R_030CEC_SQ_THREAD_TRACE_HIWATER, S_030CEC_HIWATER(4));
170bf215546Sopenharmony_ci
171bf215546Sopenharmony_ci         if (device->physical_device->rad_info.gfx_level == GFX9) {
172bf215546Sopenharmony_ci            /* Reset thread trace status errors. */
173bf215546Sopenharmony_ci            radeon_set_uconfig_reg(cs, R_030CE8_SQ_THREAD_TRACE_STATUS, S_030CE8_UTC_ERROR(0));
174bf215546Sopenharmony_ci         }
175bf215546Sopenharmony_ci
176bf215546Sopenharmony_ci         /* Enable the thread trace mode. */
177bf215546Sopenharmony_ci         uint32_t thread_trace_mode =
178bf215546Sopenharmony_ci            S_030CD8_MASK_PS(1) | S_030CD8_MASK_VS(1) | S_030CD8_MASK_GS(1) | S_030CD8_MASK_ES(1) |
179bf215546Sopenharmony_ci            S_030CD8_MASK_HS(1) | S_030CD8_MASK_LS(1) | S_030CD8_MASK_CS(1) |
180bf215546Sopenharmony_ci            S_030CD8_AUTOFLUSH_EN(1) | /* periodically flush SQTT data to memory */
181bf215546Sopenharmony_ci            S_030CD8_MODE(1);
182bf215546Sopenharmony_ci
183bf215546Sopenharmony_ci         if (device->physical_device->rad_info.gfx_level == GFX9) {
184bf215546Sopenharmony_ci            /* Count SQTT traffic in TCC perf counters. */
185bf215546Sopenharmony_ci            thread_trace_mode |= S_030CD8_TC_PERF_EN(1);
186bf215546Sopenharmony_ci         }
187bf215546Sopenharmony_ci
188bf215546Sopenharmony_ci         radeon_set_uconfig_reg(cs, R_030CD8_SQ_THREAD_TRACE_MODE, thread_trace_mode);
189bf215546Sopenharmony_ci      }
190bf215546Sopenharmony_ci   }
191bf215546Sopenharmony_ci
192bf215546Sopenharmony_ci   /* Restore global broadcasting. */
193bf215546Sopenharmony_ci   radeon_set_uconfig_reg(cs, R_030800_GRBM_GFX_INDEX,
194bf215546Sopenharmony_ci                          S_030800_SE_BROADCAST_WRITES(1) | S_030800_SH_BROADCAST_WRITES(1) |
195bf215546Sopenharmony_ci                             S_030800_INSTANCE_BROADCAST_WRITES(1));
196bf215546Sopenharmony_ci
197bf215546Sopenharmony_ci   /* Start the thread trace with a different event based on the queue. */
198bf215546Sopenharmony_ci   if (qf == RADV_QUEUE_COMPUTE) {
199bf215546Sopenharmony_ci      radeon_set_sh_reg(cs, R_00B878_COMPUTE_THREAD_TRACE_ENABLE, S_00B878_THREAD_TRACE_ENABLE(1));
200bf215546Sopenharmony_ci   } else {
201bf215546Sopenharmony_ci      radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
202bf215546Sopenharmony_ci      radeon_emit(cs, EVENT_TYPE(V_028A90_THREAD_TRACE_START) | EVENT_INDEX(0));
203bf215546Sopenharmony_ci   }
204bf215546Sopenharmony_ci}
205bf215546Sopenharmony_ci
206bf215546Sopenharmony_cistatic const uint32_t gfx8_thread_trace_info_regs[] = {
207bf215546Sopenharmony_ci   R_030CE4_SQ_THREAD_TRACE_WPTR,
208bf215546Sopenharmony_ci   R_030CE8_SQ_THREAD_TRACE_STATUS,
209bf215546Sopenharmony_ci   R_008E40_SQ_THREAD_TRACE_CNTR,
210bf215546Sopenharmony_ci};
211bf215546Sopenharmony_ci
212bf215546Sopenharmony_cistatic const uint32_t gfx9_thread_trace_info_regs[] = {
213bf215546Sopenharmony_ci   R_030CE4_SQ_THREAD_TRACE_WPTR,
214bf215546Sopenharmony_ci   R_030CE8_SQ_THREAD_TRACE_STATUS,
215bf215546Sopenharmony_ci   R_030CF0_SQ_THREAD_TRACE_CNTR,
216bf215546Sopenharmony_ci};
217bf215546Sopenharmony_ci
218bf215546Sopenharmony_cistatic const uint32_t gfx10_thread_trace_info_regs[] = {
219bf215546Sopenharmony_ci   R_008D10_SQ_THREAD_TRACE_WPTR,
220bf215546Sopenharmony_ci   R_008D20_SQ_THREAD_TRACE_STATUS,
221bf215546Sopenharmony_ci   R_008D24_SQ_THREAD_TRACE_DROPPED_CNTR,
222bf215546Sopenharmony_ci};
223bf215546Sopenharmony_ci
224bf215546Sopenharmony_cistatic void
225bf215546Sopenharmony_ciradv_copy_thread_trace_info_regs(struct radv_device *device, struct radeon_cmdbuf *cs,
226bf215546Sopenharmony_ci                                 unsigned se_index)
227bf215546Sopenharmony_ci{
228bf215546Sopenharmony_ci   const uint32_t *thread_trace_info_regs = NULL;
229bf215546Sopenharmony_ci
230bf215546Sopenharmony_ci   if (device->physical_device->rad_info.gfx_level >= GFX10) {
231bf215546Sopenharmony_ci      thread_trace_info_regs = gfx10_thread_trace_info_regs;
232bf215546Sopenharmony_ci   } else if (device->physical_device->rad_info.gfx_level == GFX9) {
233bf215546Sopenharmony_ci      thread_trace_info_regs = gfx9_thread_trace_info_regs;
234bf215546Sopenharmony_ci   } else {
235bf215546Sopenharmony_ci      assert(device->physical_device->rad_info.gfx_level == GFX8);
236bf215546Sopenharmony_ci      thread_trace_info_regs = gfx8_thread_trace_info_regs;
237bf215546Sopenharmony_ci   }
238bf215546Sopenharmony_ci
239bf215546Sopenharmony_ci   /* Get the VA where the info struct is stored for this SE. */
240bf215546Sopenharmony_ci   uint64_t va = radv_buffer_get_va(device->thread_trace.bo);
241bf215546Sopenharmony_ci   uint64_t info_va = ac_thread_trace_get_info_va(va, se_index);
242bf215546Sopenharmony_ci
243bf215546Sopenharmony_ci   /* Copy back the info struct one DWORD at a time. */
244bf215546Sopenharmony_ci   for (unsigned i = 0; i < 3; i++) {
245bf215546Sopenharmony_ci      radeon_emit(cs, PKT3(PKT3_COPY_DATA, 4, 0));
246bf215546Sopenharmony_ci      radeon_emit(cs, COPY_DATA_SRC_SEL(COPY_DATA_PERF) | COPY_DATA_DST_SEL(COPY_DATA_TC_L2) |
247bf215546Sopenharmony_ci                         COPY_DATA_WR_CONFIRM);
248bf215546Sopenharmony_ci      radeon_emit(cs, thread_trace_info_regs[i] >> 2);
249bf215546Sopenharmony_ci      radeon_emit(cs, 0); /* unused */
250bf215546Sopenharmony_ci      radeon_emit(cs, (info_va + i * 4));
251bf215546Sopenharmony_ci      radeon_emit(cs, (info_va + i * 4) >> 32);
252bf215546Sopenharmony_ci   }
253bf215546Sopenharmony_ci}
254bf215546Sopenharmony_ci
255bf215546Sopenharmony_cistatic void
256bf215546Sopenharmony_ciradv_emit_thread_trace_stop(struct radv_device *device, struct radeon_cmdbuf *cs,
257bf215546Sopenharmony_ci                            enum radv_queue_family qf)
258bf215546Sopenharmony_ci{
259bf215546Sopenharmony_ci   unsigned max_se = device->physical_device->rad_info.max_se;
260bf215546Sopenharmony_ci
261bf215546Sopenharmony_ci   /* Stop the thread trace with a different event based on the queue. */
262bf215546Sopenharmony_ci   if (qf == RADV_QUEUE_COMPUTE) {
263bf215546Sopenharmony_ci      radeon_set_sh_reg(cs, R_00B878_COMPUTE_THREAD_TRACE_ENABLE, S_00B878_THREAD_TRACE_ENABLE(0));
264bf215546Sopenharmony_ci   } else {
265bf215546Sopenharmony_ci      radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
266bf215546Sopenharmony_ci      radeon_emit(cs, EVENT_TYPE(V_028A90_THREAD_TRACE_STOP) | EVENT_INDEX(0));
267bf215546Sopenharmony_ci   }
268bf215546Sopenharmony_ci
269bf215546Sopenharmony_ci   radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
270bf215546Sopenharmony_ci   radeon_emit(cs, EVENT_TYPE(V_028A90_THREAD_TRACE_FINISH) | EVENT_INDEX(0));
271bf215546Sopenharmony_ci
272bf215546Sopenharmony_ci   if (device->physical_device->rad_info.has_sqtt_rb_harvest_bug) {
273bf215546Sopenharmony_ci      /* Some chips with disabled RBs should wait for idle because FINISH_DONE doesn't work. */
274bf215546Sopenharmony_ci      radv_emit_wait_for_idle(device, cs, qf);
275bf215546Sopenharmony_ci   }
276bf215546Sopenharmony_ci
277bf215546Sopenharmony_ci   for (unsigned se = 0; se < max_se; se++) {
278bf215546Sopenharmony_ci      if (radv_se_is_disabled(device, se))
279bf215546Sopenharmony_ci         continue;
280bf215546Sopenharmony_ci
281bf215546Sopenharmony_ci      /* Target SEi and SH0. */
282bf215546Sopenharmony_ci      radeon_set_uconfig_reg(
283bf215546Sopenharmony_ci         cs, R_030800_GRBM_GFX_INDEX,
284bf215546Sopenharmony_ci         S_030800_SE_INDEX(se) | S_030800_SH_INDEX(0) | S_030800_INSTANCE_BROADCAST_WRITES(1));
285bf215546Sopenharmony_ci
286bf215546Sopenharmony_ci      if (device->physical_device->rad_info.gfx_level >= GFX10) {
287bf215546Sopenharmony_ci         if (!device->physical_device->rad_info.has_sqtt_rb_harvest_bug) {
288bf215546Sopenharmony_ci            /* Make sure to wait for the trace buffer. */
289bf215546Sopenharmony_ci            radeon_emit(cs, PKT3(PKT3_WAIT_REG_MEM, 5, 0));
290bf215546Sopenharmony_ci            radeon_emit(
291bf215546Sopenharmony_ci               cs,
292bf215546Sopenharmony_ci               WAIT_REG_MEM_NOT_EQUAL); /* wait until the register is equal to the reference value */
293bf215546Sopenharmony_ci            radeon_emit(cs, R_008D20_SQ_THREAD_TRACE_STATUS >> 2); /* register */
294bf215546Sopenharmony_ci            radeon_emit(cs, 0);
295bf215546Sopenharmony_ci            radeon_emit(cs, 0);                       /* reference value */
296bf215546Sopenharmony_ci            radeon_emit(cs, ~C_008D20_FINISH_DONE);
297bf215546Sopenharmony_ci            radeon_emit(cs, 4);                       /* poll interval */
298bf215546Sopenharmony_ci         }
299bf215546Sopenharmony_ci
300bf215546Sopenharmony_ci         /* Disable the thread trace mode. */
301bf215546Sopenharmony_ci         radeon_set_privileged_config_reg(cs, R_008D1C_SQ_THREAD_TRACE_CTRL,
302bf215546Sopenharmony_ci                                          gfx10_get_thread_trace_ctrl(device, false));
303bf215546Sopenharmony_ci
304bf215546Sopenharmony_ci         /* Wait for thread trace completion. */
305bf215546Sopenharmony_ci         radeon_emit(cs, PKT3(PKT3_WAIT_REG_MEM, 5, 0));
306bf215546Sopenharmony_ci         radeon_emit(
307bf215546Sopenharmony_ci            cs, WAIT_REG_MEM_EQUAL); /* wait until the register is equal to the reference value */
308bf215546Sopenharmony_ci         radeon_emit(cs, R_008D20_SQ_THREAD_TRACE_STATUS >> 2); /* register */
309bf215546Sopenharmony_ci         radeon_emit(cs, 0);
310bf215546Sopenharmony_ci         radeon_emit(cs, 0);                /* reference value */
311bf215546Sopenharmony_ci         radeon_emit(cs, ~C_008D20_BUSY); /* mask */
312bf215546Sopenharmony_ci         radeon_emit(cs, 4);                /* poll interval */
313bf215546Sopenharmony_ci      } else {
314bf215546Sopenharmony_ci         /* Disable the thread trace mode. */
315bf215546Sopenharmony_ci         radeon_set_uconfig_reg(cs, R_030CD8_SQ_THREAD_TRACE_MODE, S_030CD8_MODE(0));
316bf215546Sopenharmony_ci
317bf215546Sopenharmony_ci         /* Wait for thread trace completion. */
318bf215546Sopenharmony_ci         radeon_emit(cs, PKT3(PKT3_WAIT_REG_MEM, 5, 0));
319bf215546Sopenharmony_ci         radeon_emit(
320bf215546Sopenharmony_ci            cs, WAIT_REG_MEM_EQUAL); /* wait until the register is equal to the reference value */
321bf215546Sopenharmony_ci         radeon_emit(cs, R_030CE8_SQ_THREAD_TRACE_STATUS >> 2); /* register */
322bf215546Sopenharmony_ci         radeon_emit(cs, 0);
323bf215546Sopenharmony_ci         radeon_emit(cs, 0);                /* reference value */
324bf215546Sopenharmony_ci         radeon_emit(cs, ~C_030CE8_BUSY); /* mask */
325bf215546Sopenharmony_ci         radeon_emit(cs, 4);                /* poll interval */
326bf215546Sopenharmony_ci      }
327bf215546Sopenharmony_ci
328bf215546Sopenharmony_ci      radv_copy_thread_trace_info_regs(device, cs, se);
329bf215546Sopenharmony_ci   }
330bf215546Sopenharmony_ci
331bf215546Sopenharmony_ci   /* Restore global broadcasting. */
332bf215546Sopenharmony_ci   radeon_set_uconfig_reg(cs, R_030800_GRBM_GFX_INDEX,
333bf215546Sopenharmony_ci                          S_030800_SE_BROADCAST_WRITES(1) | S_030800_SH_BROADCAST_WRITES(1) |
334bf215546Sopenharmony_ci                             S_030800_INSTANCE_BROADCAST_WRITES(1));
335bf215546Sopenharmony_ci}
336bf215546Sopenharmony_ci
337bf215546Sopenharmony_civoid
338bf215546Sopenharmony_ciradv_emit_thread_trace_userdata(struct radv_cmd_buffer *cmd_buffer, const void *data,
339bf215546Sopenharmony_ci                                uint32_t num_dwords)
340bf215546Sopenharmony_ci{
341bf215546Sopenharmony_ci   struct radv_device *device = cmd_buffer->device;
342bf215546Sopenharmony_ci   struct radeon_cmdbuf *cs = cmd_buffer->cs;
343bf215546Sopenharmony_ci   const uint32_t *dwords = (uint32_t *)data;
344bf215546Sopenharmony_ci
345bf215546Sopenharmony_ci   /* SQTT user data packets aren't supported on SDMA queues. */
346bf215546Sopenharmony_ci   if (cmd_buffer->qf == RADV_QUEUE_TRANSFER)
347bf215546Sopenharmony_ci      return;
348bf215546Sopenharmony_ci
349bf215546Sopenharmony_ci   while (num_dwords > 0) {
350bf215546Sopenharmony_ci      uint32_t count = MIN2(num_dwords, 2);
351bf215546Sopenharmony_ci
352bf215546Sopenharmony_ci      radeon_check_space(device->ws, cs, 2 + count);
353bf215546Sopenharmony_ci
354bf215546Sopenharmony_ci      /* Without the perfctr bit the CP might not always pass the
355bf215546Sopenharmony_ci       * write on correctly. */
356bf215546Sopenharmony_ci      if (device->physical_device->rad_info.gfx_level >= GFX10)
357bf215546Sopenharmony_ci         radeon_set_uconfig_reg_seq_perfctr(cs, R_030D08_SQ_THREAD_TRACE_USERDATA_2, count);
358bf215546Sopenharmony_ci      else
359bf215546Sopenharmony_ci         radeon_set_uconfig_reg_seq(cs, R_030D08_SQ_THREAD_TRACE_USERDATA_2, count);
360bf215546Sopenharmony_ci      radeon_emit_array(cs, dwords, count);
361bf215546Sopenharmony_ci
362bf215546Sopenharmony_ci      dwords += count;
363bf215546Sopenharmony_ci      num_dwords -= count;
364bf215546Sopenharmony_ci   }
365bf215546Sopenharmony_ci}
366bf215546Sopenharmony_ci
367bf215546Sopenharmony_civoid
368bf215546Sopenharmony_ciradv_emit_spi_config_cntl(struct radv_device *device, struct radeon_cmdbuf *cs, bool enable)
369bf215546Sopenharmony_ci{
370bf215546Sopenharmony_ci   if (device->physical_device->rad_info.gfx_level >= GFX9) {
371bf215546Sopenharmony_ci      uint32_t spi_config_cntl =
372bf215546Sopenharmony_ci         S_031100_GPR_WRITE_PRIORITY(0x2c688) | S_031100_EXP_PRIORITY_ORDER(3) |
373bf215546Sopenharmony_ci         S_031100_ENABLE_SQG_TOP_EVENTS(enable) | S_031100_ENABLE_SQG_BOP_EVENTS(enable);
374bf215546Sopenharmony_ci
375bf215546Sopenharmony_ci      if (device->physical_device->rad_info.gfx_level >= GFX10)
376bf215546Sopenharmony_ci         spi_config_cntl |= S_031100_PS_PKR_PRIORITY_CNTL(3);
377bf215546Sopenharmony_ci
378bf215546Sopenharmony_ci      radeon_set_uconfig_reg(cs, R_031100_SPI_CONFIG_CNTL, spi_config_cntl);
379bf215546Sopenharmony_ci   } else {
380bf215546Sopenharmony_ci      /* SPI_CONFIG_CNTL is a protected register on GFX6-GFX8. */
381bf215546Sopenharmony_ci      radeon_set_privileged_config_reg(
382bf215546Sopenharmony_ci         cs, R_009100_SPI_CONFIG_CNTL,
383bf215546Sopenharmony_ci         S_009100_ENABLE_SQG_TOP_EVENTS(enable) | S_009100_ENABLE_SQG_BOP_EVENTS(enable));
384bf215546Sopenharmony_ci   }
385bf215546Sopenharmony_ci}
386bf215546Sopenharmony_ci
387bf215546Sopenharmony_civoid
388bf215546Sopenharmony_ciradv_emit_inhibit_clockgating(struct radv_device *device, struct radeon_cmdbuf *cs, bool inhibit)
389bf215546Sopenharmony_ci{
390bf215546Sopenharmony_ci   if (device->physical_device->rad_info.gfx_level >= GFX11)
391bf215546Sopenharmony_ci      return; /* not needed */
392bf215546Sopenharmony_ci
393bf215546Sopenharmony_ci   if (device->physical_device->rad_info.gfx_level >= GFX10) {
394bf215546Sopenharmony_ci      radeon_set_uconfig_reg(cs, R_037390_RLC_PERFMON_CLK_CNTL,
395bf215546Sopenharmony_ci                             S_037390_PERFMON_CLOCK_STATE(inhibit));
396bf215546Sopenharmony_ci   } else if (device->physical_device->rad_info.gfx_level >= GFX8) {
397bf215546Sopenharmony_ci      radeon_set_uconfig_reg(cs, R_0372FC_RLC_PERFMON_CLK_CNTL,
398bf215546Sopenharmony_ci                             S_0372FC_PERFMON_CLOCK_STATE(inhibit));
399bf215546Sopenharmony_ci   }
400bf215546Sopenharmony_ci}
401bf215546Sopenharmony_ci
402bf215546Sopenharmony_cistatic bool
403bf215546Sopenharmony_ciradv_thread_trace_init_bo(struct radv_device *device)
404bf215546Sopenharmony_ci{
405bf215546Sopenharmony_ci   unsigned max_se = device->physical_device->rad_info.max_se;
406bf215546Sopenharmony_ci   struct radeon_winsys *ws = device->ws;
407bf215546Sopenharmony_ci   VkResult result;
408bf215546Sopenharmony_ci   uint64_t size;
409bf215546Sopenharmony_ci
410bf215546Sopenharmony_ci   /* The buffer size and address need to be aligned in HW regs. Align the
411bf215546Sopenharmony_ci    * size as early as possible so that we do all the allocation & addressing
412bf215546Sopenharmony_ci    * correctly. */
413bf215546Sopenharmony_ci   device->thread_trace.buffer_size =
414bf215546Sopenharmony_ci      align64(device->thread_trace.buffer_size, 1u << SQTT_BUFFER_ALIGN_SHIFT);
415bf215546Sopenharmony_ci
416bf215546Sopenharmony_ci   /* Compute total size of the thread trace BO for all SEs. */
417bf215546Sopenharmony_ci   size = align64(sizeof(struct ac_thread_trace_info) * max_se, 1 << SQTT_BUFFER_ALIGN_SHIFT);
418bf215546Sopenharmony_ci   size += device->thread_trace.buffer_size * (uint64_t)max_se;
419bf215546Sopenharmony_ci
420bf215546Sopenharmony_ci   struct radeon_winsys_bo *bo = NULL;
421bf215546Sopenharmony_ci   result = ws->buffer_create(
422bf215546Sopenharmony_ci      ws, size, 4096, RADEON_DOMAIN_VRAM,
423bf215546Sopenharmony_ci      RADEON_FLAG_CPU_ACCESS | RADEON_FLAG_NO_INTERPROCESS_SHARING | RADEON_FLAG_ZERO_VRAM,
424bf215546Sopenharmony_ci      RADV_BO_PRIORITY_SCRATCH, 0, &bo);
425bf215546Sopenharmony_ci   device->thread_trace.bo = bo;
426bf215546Sopenharmony_ci   if (result != VK_SUCCESS)
427bf215546Sopenharmony_ci      return false;
428bf215546Sopenharmony_ci
429bf215546Sopenharmony_ci   result = ws->buffer_make_resident(ws, device->thread_trace.bo, true);
430bf215546Sopenharmony_ci   if (result != VK_SUCCESS)
431bf215546Sopenharmony_ci      return false;
432bf215546Sopenharmony_ci
433bf215546Sopenharmony_ci   device->thread_trace.ptr = ws->buffer_map(device->thread_trace.bo);
434bf215546Sopenharmony_ci   if (!device->thread_trace.ptr)
435bf215546Sopenharmony_ci      return false;
436bf215546Sopenharmony_ci
437bf215546Sopenharmony_ci   return true;
438bf215546Sopenharmony_ci}
439bf215546Sopenharmony_ci
440bf215546Sopenharmony_cistatic void
441bf215546Sopenharmony_ciradv_thread_trace_finish_bo(struct radv_device *device)
442bf215546Sopenharmony_ci{
443bf215546Sopenharmony_ci   struct radeon_winsys *ws = device->ws;
444bf215546Sopenharmony_ci
445bf215546Sopenharmony_ci   if (unlikely(device->thread_trace.bo)) {
446bf215546Sopenharmony_ci      ws->buffer_make_resident(ws, device->thread_trace.bo, false);
447bf215546Sopenharmony_ci      ws->buffer_destroy(ws, device->thread_trace.bo);
448bf215546Sopenharmony_ci   }
449bf215546Sopenharmony_ci}
450bf215546Sopenharmony_ci
451bf215546Sopenharmony_cibool
452bf215546Sopenharmony_ciradv_thread_trace_init(struct radv_device *device)
453bf215546Sopenharmony_ci{
454bf215546Sopenharmony_ci   struct ac_thread_trace_data *thread_trace_data = &device->thread_trace;
455bf215546Sopenharmony_ci
456bf215546Sopenharmony_ci   /* Default buffer size set to 32MB per SE. */
457bf215546Sopenharmony_ci   device->thread_trace.buffer_size =
458bf215546Sopenharmony_ci      radv_get_int_debug_option("RADV_THREAD_TRACE_BUFFER_SIZE", 32 * 1024 * 1024);
459bf215546Sopenharmony_ci   device->thread_trace.start_frame = radv_get_int_debug_option("RADV_THREAD_TRACE", -1);
460bf215546Sopenharmony_ci
461bf215546Sopenharmony_ci   const char *trigger_file = getenv("RADV_THREAD_TRACE_TRIGGER");
462bf215546Sopenharmony_ci   if (trigger_file)
463bf215546Sopenharmony_ci      device->thread_trace.trigger_file = strdup(trigger_file);
464bf215546Sopenharmony_ci
465bf215546Sopenharmony_ci   if (!radv_thread_trace_init_bo(device))
466bf215546Sopenharmony_ci      return false;
467bf215546Sopenharmony_ci
468bf215546Sopenharmony_ci   if (!radv_device_acquire_performance_counters(device))
469bf215546Sopenharmony_ci      return false;
470bf215546Sopenharmony_ci
471bf215546Sopenharmony_ci   list_inithead(&thread_trace_data->rgp_pso_correlation.record);
472bf215546Sopenharmony_ci   simple_mtx_init(&thread_trace_data->rgp_pso_correlation.lock, mtx_plain);
473bf215546Sopenharmony_ci
474bf215546Sopenharmony_ci   list_inithead(&thread_trace_data->rgp_loader_events.record);
475bf215546Sopenharmony_ci   simple_mtx_init(&thread_trace_data->rgp_loader_events.lock, mtx_plain);
476bf215546Sopenharmony_ci
477bf215546Sopenharmony_ci   list_inithead(&thread_trace_data->rgp_code_object.record);
478bf215546Sopenharmony_ci   simple_mtx_init(&thread_trace_data->rgp_code_object.lock, mtx_plain);
479bf215546Sopenharmony_ci
480bf215546Sopenharmony_ci   return true;
481bf215546Sopenharmony_ci}
482bf215546Sopenharmony_ci
483bf215546Sopenharmony_civoid
484bf215546Sopenharmony_ciradv_thread_trace_finish(struct radv_device *device)
485bf215546Sopenharmony_ci{
486bf215546Sopenharmony_ci   struct ac_thread_trace_data *thread_trace_data = &device->thread_trace;
487bf215546Sopenharmony_ci   struct radeon_winsys *ws = device->ws;
488bf215546Sopenharmony_ci
489bf215546Sopenharmony_ci   free(device->thread_trace.trigger_file);
490bf215546Sopenharmony_ci
491bf215546Sopenharmony_ci   radv_thread_trace_finish_bo(device);
492bf215546Sopenharmony_ci
493bf215546Sopenharmony_ci   for (unsigned i = 0; i < 2; i++) {
494bf215546Sopenharmony_ci      if (device->thread_trace.start_cs[i])
495bf215546Sopenharmony_ci         ws->cs_destroy(device->thread_trace.start_cs[i]);
496bf215546Sopenharmony_ci      if (device->thread_trace.stop_cs[i])
497bf215546Sopenharmony_ci         ws->cs_destroy(device->thread_trace.stop_cs[i]);
498bf215546Sopenharmony_ci   }
499bf215546Sopenharmony_ci
500bf215546Sopenharmony_ci   assert(thread_trace_data->rgp_pso_correlation.record_count == 0);
501bf215546Sopenharmony_ci   simple_mtx_destroy(&thread_trace_data->rgp_pso_correlation.lock);
502bf215546Sopenharmony_ci
503bf215546Sopenharmony_ci   assert(thread_trace_data->rgp_loader_events.record_count == 0);
504bf215546Sopenharmony_ci   simple_mtx_destroy(&thread_trace_data->rgp_loader_events.lock);
505bf215546Sopenharmony_ci
506bf215546Sopenharmony_ci   assert(thread_trace_data->rgp_code_object.record_count == 0);
507bf215546Sopenharmony_ci   simple_mtx_destroy(&thread_trace_data->rgp_code_object.lock);
508bf215546Sopenharmony_ci}
509bf215546Sopenharmony_ci
510bf215546Sopenharmony_cistatic bool
511bf215546Sopenharmony_ciradv_thread_trace_resize_bo(struct radv_device *device)
512bf215546Sopenharmony_ci{
513bf215546Sopenharmony_ci   /* Destroy the previous thread trace BO. */
514bf215546Sopenharmony_ci   radv_thread_trace_finish_bo(device);
515bf215546Sopenharmony_ci
516bf215546Sopenharmony_ci   /* Double the size of the thread trace buffer per SE. */
517bf215546Sopenharmony_ci   device->thread_trace.buffer_size *= 2;
518bf215546Sopenharmony_ci
519bf215546Sopenharmony_ci   fprintf(stderr,
520bf215546Sopenharmony_ci           "Failed to get the thread trace because the buffer "
521bf215546Sopenharmony_ci           "was too small, resizing to %d KB\n",
522bf215546Sopenharmony_ci           device->thread_trace.buffer_size / 1024);
523bf215546Sopenharmony_ci
524bf215546Sopenharmony_ci   /* Re-create the thread trace BO. */
525bf215546Sopenharmony_ci   return radv_thread_trace_init_bo(device);
526bf215546Sopenharmony_ci}
527bf215546Sopenharmony_ci
528bf215546Sopenharmony_cibool
529bf215546Sopenharmony_ciradv_begin_thread_trace(struct radv_queue *queue)
530bf215546Sopenharmony_ci{
531bf215546Sopenharmony_ci   struct radv_device *device = queue->device;
532bf215546Sopenharmony_ci   enum radv_queue_family family = queue->state.qf;
533bf215546Sopenharmony_ci   struct radeon_winsys *ws = device->ws;
534bf215546Sopenharmony_ci   struct radeon_cmdbuf *cs;
535bf215546Sopenharmony_ci   VkResult result;
536bf215546Sopenharmony_ci
537bf215546Sopenharmony_ci   /* Destroy the previous start CS and create a new one. */
538bf215546Sopenharmony_ci   if (device->thread_trace.start_cs[family]) {
539bf215546Sopenharmony_ci      ws->cs_destroy(device->thread_trace.start_cs[family]);
540bf215546Sopenharmony_ci      device->thread_trace.start_cs[family] = NULL;
541bf215546Sopenharmony_ci   }
542bf215546Sopenharmony_ci
543bf215546Sopenharmony_ci   cs = ws->cs_create(ws, radv_queue_ring(queue));
544bf215546Sopenharmony_ci   if (!cs)
545bf215546Sopenharmony_ci      return false;
546bf215546Sopenharmony_ci
547bf215546Sopenharmony_ci   switch (family) {
548bf215546Sopenharmony_ci   case RADV_QUEUE_GENERAL:
549bf215546Sopenharmony_ci      radeon_emit(cs, PKT3(PKT3_CONTEXT_CONTROL, 1, 0));
550bf215546Sopenharmony_ci      radeon_emit(cs, CC0_UPDATE_LOAD_ENABLES(1));
551bf215546Sopenharmony_ci      radeon_emit(cs, CC1_UPDATE_SHADOW_ENABLES(1));
552bf215546Sopenharmony_ci      break;
553bf215546Sopenharmony_ci   case RADV_QUEUE_COMPUTE:
554bf215546Sopenharmony_ci      radeon_emit(cs, PKT3(PKT3_NOP, 0, 0));
555bf215546Sopenharmony_ci      radeon_emit(cs, 0);
556bf215546Sopenharmony_ci      break;
557bf215546Sopenharmony_ci   default:
558bf215546Sopenharmony_ci      unreachable("Incorrect queue family");
559bf215546Sopenharmony_ci      break;
560bf215546Sopenharmony_ci   }
561bf215546Sopenharmony_ci
562bf215546Sopenharmony_ci   /* Make sure to wait-for-idle before starting SQTT. */
563bf215546Sopenharmony_ci   radv_emit_wait_for_idle(device, cs, family);
564bf215546Sopenharmony_ci
565bf215546Sopenharmony_ci   /* Disable clock gating before starting SQTT. */
566bf215546Sopenharmony_ci   radv_emit_inhibit_clockgating(device, cs, true);
567bf215546Sopenharmony_ci
568bf215546Sopenharmony_ci   /* Enable SQG events that collects thread trace data. */
569bf215546Sopenharmony_ci   radv_emit_spi_config_cntl(device, cs, true);
570bf215546Sopenharmony_ci
571bf215546Sopenharmony_ci   radv_perfcounter_emit_spm_reset(cs);
572bf215546Sopenharmony_ci
573bf215546Sopenharmony_ci   if (device->spm_trace.bo) {
574bf215546Sopenharmony_ci      /* Enable all shader stages by default. */
575bf215546Sopenharmony_ci      radv_perfcounter_emit_shaders(cs, 0x7f);
576bf215546Sopenharmony_ci
577bf215546Sopenharmony_ci      radv_emit_spm_setup(device, cs);
578bf215546Sopenharmony_ci   }
579bf215546Sopenharmony_ci
580bf215546Sopenharmony_ci   /* Start SQTT. */
581bf215546Sopenharmony_ci   radv_emit_thread_trace_start(device, cs, family);
582bf215546Sopenharmony_ci
583bf215546Sopenharmony_ci   if (device->spm_trace.bo)
584bf215546Sopenharmony_ci      radv_perfcounter_emit_spm_start(device, cs, family);
585bf215546Sopenharmony_ci
586bf215546Sopenharmony_ci   result = ws->cs_finalize(cs);
587bf215546Sopenharmony_ci   if (result != VK_SUCCESS) {
588bf215546Sopenharmony_ci      ws->cs_destroy(cs);
589bf215546Sopenharmony_ci      return false;
590bf215546Sopenharmony_ci   }
591bf215546Sopenharmony_ci
592bf215546Sopenharmony_ci   device->thread_trace.start_cs[family] = cs;
593bf215546Sopenharmony_ci
594bf215546Sopenharmony_ci   return radv_queue_internal_submit(queue, cs);
595bf215546Sopenharmony_ci}
596bf215546Sopenharmony_ci
597bf215546Sopenharmony_cibool
598bf215546Sopenharmony_ciradv_end_thread_trace(struct radv_queue *queue)
599bf215546Sopenharmony_ci{
600bf215546Sopenharmony_ci   struct radv_device *device = queue->device;
601bf215546Sopenharmony_ci   enum radv_queue_family family = queue->state.qf;
602bf215546Sopenharmony_ci   struct radeon_winsys *ws = device->ws;
603bf215546Sopenharmony_ci   struct radeon_cmdbuf *cs;
604bf215546Sopenharmony_ci   VkResult result;
605bf215546Sopenharmony_ci
606bf215546Sopenharmony_ci   /* Destroy the previous stop CS and create a new one. */
607bf215546Sopenharmony_ci   if (queue->device->thread_trace.stop_cs[family]) {
608bf215546Sopenharmony_ci      ws->cs_destroy(device->thread_trace.stop_cs[family]);
609bf215546Sopenharmony_ci      device->thread_trace.stop_cs[family] = NULL;
610bf215546Sopenharmony_ci   }
611bf215546Sopenharmony_ci
612bf215546Sopenharmony_ci   cs = ws->cs_create(ws, radv_queue_ring(queue));
613bf215546Sopenharmony_ci   if (!cs)
614bf215546Sopenharmony_ci      return false;
615bf215546Sopenharmony_ci
616bf215546Sopenharmony_ci   switch (family) {
617bf215546Sopenharmony_ci   case RADV_QUEUE_GENERAL:
618bf215546Sopenharmony_ci      radeon_emit(cs, PKT3(PKT3_CONTEXT_CONTROL, 1, 0));
619bf215546Sopenharmony_ci      radeon_emit(cs, CC0_UPDATE_LOAD_ENABLES(1));
620bf215546Sopenharmony_ci      radeon_emit(cs, CC1_UPDATE_SHADOW_ENABLES(1));
621bf215546Sopenharmony_ci      break;
622bf215546Sopenharmony_ci   case RADV_QUEUE_COMPUTE:
623bf215546Sopenharmony_ci      radeon_emit(cs, PKT3(PKT3_NOP, 0, 0));
624bf215546Sopenharmony_ci      radeon_emit(cs, 0);
625bf215546Sopenharmony_ci      break;
626bf215546Sopenharmony_ci   default:
627bf215546Sopenharmony_ci      unreachable("Incorrect queue family");
628bf215546Sopenharmony_ci      break;
629bf215546Sopenharmony_ci   }
630bf215546Sopenharmony_ci
631bf215546Sopenharmony_ci   /* Make sure to wait-for-idle before stopping SQTT. */
632bf215546Sopenharmony_ci   radv_emit_wait_for_idle(device, cs, family);
633bf215546Sopenharmony_ci
634bf215546Sopenharmony_ci   if (device->spm_trace.bo)
635bf215546Sopenharmony_ci      radv_perfcounter_emit_spm_stop(device, cs, family);
636bf215546Sopenharmony_ci
637bf215546Sopenharmony_ci   /* Stop SQTT. */
638bf215546Sopenharmony_ci   radv_emit_thread_trace_stop(device, cs, family);
639bf215546Sopenharmony_ci
640bf215546Sopenharmony_ci   radv_perfcounter_emit_spm_reset(cs);
641bf215546Sopenharmony_ci
642bf215546Sopenharmony_ci   /* Restore previous state by disabling SQG events. */
643bf215546Sopenharmony_ci   radv_emit_spi_config_cntl(device, cs, false);
644bf215546Sopenharmony_ci
645bf215546Sopenharmony_ci   /* Restore previous state by re-enabling clock gating. */
646bf215546Sopenharmony_ci   radv_emit_inhibit_clockgating(device, cs, false);
647bf215546Sopenharmony_ci
648bf215546Sopenharmony_ci   result = ws->cs_finalize(cs);
649bf215546Sopenharmony_ci   if (result != VK_SUCCESS) {
650bf215546Sopenharmony_ci      ws->cs_destroy(cs);
651bf215546Sopenharmony_ci      return false;
652bf215546Sopenharmony_ci   }
653bf215546Sopenharmony_ci
654bf215546Sopenharmony_ci   device->thread_trace.stop_cs[family] = cs;
655bf215546Sopenharmony_ci
656bf215546Sopenharmony_ci   return radv_queue_internal_submit(queue, cs);
657bf215546Sopenharmony_ci}
658bf215546Sopenharmony_ci
659bf215546Sopenharmony_cibool
660bf215546Sopenharmony_ciradv_get_thread_trace(struct radv_queue *queue, struct ac_thread_trace *thread_trace)
661bf215546Sopenharmony_ci{
662bf215546Sopenharmony_ci   struct radv_device *device = queue->device;
663bf215546Sopenharmony_ci   struct radeon_info *rad_info = &device->physical_device->rad_info;
664bf215546Sopenharmony_ci   unsigned max_se = rad_info->max_se;
665bf215546Sopenharmony_ci   void *thread_trace_ptr = device->thread_trace.ptr;
666bf215546Sopenharmony_ci
667bf215546Sopenharmony_ci   memset(thread_trace, 0, sizeof(*thread_trace));
668bf215546Sopenharmony_ci
669bf215546Sopenharmony_ci   for (unsigned se = 0; se < max_se; se++) {
670bf215546Sopenharmony_ci      uint64_t info_offset = ac_thread_trace_get_info_offset(se);
671bf215546Sopenharmony_ci      uint64_t data_offset = ac_thread_trace_get_data_offset(rad_info, &device->thread_trace, se);
672bf215546Sopenharmony_ci      void *info_ptr = (uint8_t *)thread_trace_ptr + info_offset;
673bf215546Sopenharmony_ci      void *data_ptr = (uint8_t *)thread_trace_ptr + data_offset;
674bf215546Sopenharmony_ci      struct ac_thread_trace_info *info = (struct ac_thread_trace_info *)info_ptr;
675bf215546Sopenharmony_ci      struct ac_thread_trace_se thread_trace_se = {0};
676bf215546Sopenharmony_ci      int first_active_cu = ffs(device->physical_device->rad_info.cu_mask[se][0]);
677bf215546Sopenharmony_ci
678bf215546Sopenharmony_ci      if (radv_se_is_disabled(device, se))
679bf215546Sopenharmony_ci         continue;
680bf215546Sopenharmony_ci
681bf215546Sopenharmony_ci      if (!ac_is_thread_trace_complete(&device->physical_device->rad_info, &device->thread_trace,
682bf215546Sopenharmony_ci                                       info)) {
683bf215546Sopenharmony_ci         if (!radv_thread_trace_resize_bo(device)) {
684bf215546Sopenharmony_ci            fprintf(stderr, "Failed to resize the thread "
685bf215546Sopenharmony_ci                            "trace buffer.\n");
686bf215546Sopenharmony_ci            abort();
687bf215546Sopenharmony_ci         }
688bf215546Sopenharmony_ci         return false;
689bf215546Sopenharmony_ci      }
690bf215546Sopenharmony_ci
691bf215546Sopenharmony_ci      thread_trace_se.data_ptr = data_ptr;
692bf215546Sopenharmony_ci      thread_trace_se.info = *info;
693bf215546Sopenharmony_ci      thread_trace_se.shader_engine = se;
694bf215546Sopenharmony_ci
695bf215546Sopenharmony_ci      /* RGP seems to expect units of WGP on GFX10+. */
696bf215546Sopenharmony_ci      thread_trace_se.compute_unit = device->physical_device->rad_info.gfx_level >= GFX10
697bf215546Sopenharmony_ci                                        ? (first_active_cu / 2)
698bf215546Sopenharmony_ci                                        : first_active_cu;
699bf215546Sopenharmony_ci
700bf215546Sopenharmony_ci      thread_trace->traces[thread_trace->num_traces] = thread_trace_se;
701bf215546Sopenharmony_ci      thread_trace->num_traces++;
702bf215546Sopenharmony_ci   }
703bf215546Sopenharmony_ci
704bf215546Sopenharmony_ci   thread_trace->data = &device->thread_trace;
705bf215546Sopenharmony_ci   return true;
706bf215546Sopenharmony_ci}
707