1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright 2015 Advanced Micro Devices, Inc.
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8bf215546Sopenharmony_ci * license, and/or sell copies of the Software, and to permit persons to whom
9bf215546Sopenharmony_ci * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19bf215546Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20bf215546Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21bf215546Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include "ac_debug.h"
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#ifdef HAVE_VALGRIND
27bf215546Sopenharmony_ci#include <memcheck.h>
28bf215546Sopenharmony_ci#include <valgrind.h>
29bf215546Sopenharmony_ci#define VG(x) x
30bf215546Sopenharmony_ci#else
31bf215546Sopenharmony_ci#define VG(x) ((void)0)
32bf215546Sopenharmony_ci#endif
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci#include "sid.h"
35bf215546Sopenharmony_ci#include "sid_tables.h"
36bf215546Sopenharmony_ci#include "util/compiler.h"
37bf215546Sopenharmony_ci#include "util/memstream.h"
38bf215546Sopenharmony_ci#include "util/u_math.h"
39bf215546Sopenharmony_ci#include "util/u_memory.h"
40bf215546Sopenharmony_ci#include "util/u_string.h"
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci#include <assert.h>
43bf215546Sopenharmony_ci#include <inttypes.h>
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_ciDEBUG_GET_ONCE_BOOL_OPTION(color, "AMD_COLOR", true);
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci/* Parsed IBs are difficult to read without colors. Use "less -R file" to
48bf215546Sopenharmony_ci * read them, or use "aha -b -f file" to convert them to html.
49bf215546Sopenharmony_ci */
50bf215546Sopenharmony_ci#define COLOR_RESET  "\033[0m"
51bf215546Sopenharmony_ci#define COLOR_RED    "\033[31m"
52bf215546Sopenharmony_ci#define COLOR_GREEN  "\033[1;32m"
53bf215546Sopenharmony_ci#define COLOR_YELLOW "\033[1;33m"
54bf215546Sopenharmony_ci#define COLOR_CYAN   "\033[1;36m"
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci#define O_COLOR_RESET  (debug_get_option_color() ? COLOR_RESET : "")
57bf215546Sopenharmony_ci#define O_COLOR_RED    (debug_get_option_color() ? COLOR_RED : "")
58bf215546Sopenharmony_ci#define O_COLOR_GREEN  (debug_get_option_color() ? COLOR_GREEN : "")
59bf215546Sopenharmony_ci#define O_COLOR_YELLOW (debug_get_option_color() ? COLOR_YELLOW : "")
60bf215546Sopenharmony_ci#define O_COLOR_CYAN   (debug_get_option_color() ? COLOR_CYAN : "")
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ci#define INDENT_PKT 8
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_cistruct ac_ib_parser {
65bf215546Sopenharmony_ci   FILE *f;
66bf215546Sopenharmony_ci   uint32_t *ib;
67bf215546Sopenharmony_ci   unsigned num_dw;
68bf215546Sopenharmony_ci   const int *trace_ids;
69bf215546Sopenharmony_ci   unsigned trace_id_count;
70bf215546Sopenharmony_ci   enum amd_gfx_level gfx_level;
71bf215546Sopenharmony_ci   ac_debug_addr_callback addr_callback;
72bf215546Sopenharmony_ci   void *addr_callback_data;
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci   unsigned cur_dw;
75bf215546Sopenharmony_ci};
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_cistatic void ac_do_parse_ib(FILE *f, struct ac_ib_parser *ib);
78bf215546Sopenharmony_ci
79bf215546Sopenharmony_cistatic void print_spaces(FILE *f, unsigned num)
80bf215546Sopenharmony_ci{
81bf215546Sopenharmony_ci   fprintf(f, "%*s", num, "");
82bf215546Sopenharmony_ci}
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_cistatic void print_value(FILE *file, uint32_t value, int bits)
85bf215546Sopenharmony_ci{
86bf215546Sopenharmony_ci   /* Guess if it's int or float */
87bf215546Sopenharmony_ci   if (value <= (1 << 15)) {
88bf215546Sopenharmony_ci      if (value <= 9)
89bf215546Sopenharmony_ci         fprintf(file, "%u\n", value);
90bf215546Sopenharmony_ci      else
91bf215546Sopenharmony_ci         fprintf(file, "%u (0x%0*x)\n", value, bits / 4, value);
92bf215546Sopenharmony_ci   } else {
93bf215546Sopenharmony_ci      float f = uif(value);
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_ci      if (fabs(f) < 100000 && f * 10 == floor(f * 10))
96bf215546Sopenharmony_ci         fprintf(file, "%.1ff (0x%0*x)\n", f, bits / 4, value);
97bf215546Sopenharmony_ci      else
98bf215546Sopenharmony_ci         /* Don't print more leading zeros than there are bits. */
99bf215546Sopenharmony_ci         fprintf(file, "0x%0*x\n", bits / 4, value);
100bf215546Sopenharmony_ci   }
101bf215546Sopenharmony_ci}
102bf215546Sopenharmony_ci
103bf215546Sopenharmony_cistatic void print_named_value(FILE *file, const char *name, uint32_t value, int bits)
104bf215546Sopenharmony_ci{
105bf215546Sopenharmony_ci   print_spaces(file, INDENT_PKT);
106bf215546Sopenharmony_ci   fprintf(file, "%s%s%s <- ",
107bf215546Sopenharmony_ci           O_COLOR_YELLOW, name,
108bf215546Sopenharmony_ci           O_COLOR_RESET);
109bf215546Sopenharmony_ci   print_value(file, value, bits);
110bf215546Sopenharmony_ci}
111bf215546Sopenharmony_ci
112bf215546Sopenharmony_cistatic const struct si_reg *find_register(enum amd_gfx_level gfx_level, unsigned offset)
113bf215546Sopenharmony_ci{
114bf215546Sopenharmony_ci   const struct si_reg *table;
115bf215546Sopenharmony_ci   unsigned table_size;
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_ci   switch (gfx_level) {
118bf215546Sopenharmony_ci   case GFX11:
119bf215546Sopenharmony_ci      table = gfx11_reg_table;
120bf215546Sopenharmony_ci      table_size = ARRAY_SIZE(gfx11_reg_table);
121bf215546Sopenharmony_ci      break;
122bf215546Sopenharmony_ci   case GFX10_3:
123bf215546Sopenharmony_ci   case GFX10:
124bf215546Sopenharmony_ci      table = gfx10_reg_table;
125bf215546Sopenharmony_ci      table_size = ARRAY_SIZE(gfx10_reg_table);
126bf215546Sopenharmony_ci      break;
127bf215546Sopenharmony_ci   case GFX9:
128bf215546Sopenharmony_ci      table = gfx9_reg_table;
129bf215546Sopenharmony_ci      table_size = ARRAY_SIZE(gfx9_reg_table);
130bf215546Sopenharmony_ci      break;
131bf215546Sopenharmony_ci   case GFX8:
132bf215546Sopenharmony_ci      table = gfx8_reg_table;
133bf215546Sopenharmony_ci      table_size = ARRAY_SIZE(gfx8_reg_table);
134bf215546Sopenharmony_ci      break;
135bf215546Sopenharmony_ci   case GFX7:
136bf215546Sopenharmony_ci      table = gfx7_reg_table;
137bf215546Sopenharmony_ci      table_size = ARRAY_SIZE(gfx7_reg_table);
138bf215546Sopenharmony_ci      break;
139bf215546Sopenharmony_ci   case GFX6:
140bf215546Sopenharmony_ci      table = gfx6_reg_table;
141bf215546Sopenharmony_ci      table_size = ARRAY_SIZE(gfx6_reg_table);
142bf215546Sopenharmony_ci      break;
143bf215546Sopenharmony_ci   default:
144bf215546Sopenharmony_ci      return NULL;
145bf215546Sopenharmony_ci   }
146bf215546Sopenharmony_ci
147bf215546Sopenharmony_ci   for (unsigned i = 0; i < table_size; i++) {
148bf215546Sopenharmony_ci      const struct si_reg *reg = &table[i];
149bf215546Sopenharmony_ci
150bf215546Sopenharmony_ci      if (reg->offset == offset)
151bf215546Sopenharmony_ci         return reg;
152bf215546Sopenharmony_ci   }
153bf215546Sopenharmony_ci
154bf215546Sopenharmony_ci   return NULL;
155bf215546Sopenharmony_ci}
156bf215546Sopenharmony_ci
157bf215546Sopenharmony_ciconst char *ac_get_register_name(enum amd_gfx_level gfx_level, unsigned offset)
158bf215546Sopenharmony_ci{
159bf215546Sopenharmony_ci   const struct si_reg *reg = find_register(gfx_level, offset);
160bf215546Sopenharmony_ci
161bf215546Sopenharmony_ci   return reg ? sid_strings + reg->name_offset : "(no name)";
162bf215546Sopenharmony_ci}
163bf215546Sopenharmony_ci
164bf215546Sopenharmony_civoid ac_dump_reg(FILE *file, enum amd_gfx_level gfx_level, unsigned offset, uint32_t value,
165bf215546Sopenharmony_ci                 uint32_t field_mask)
166bf215546Sopenharmony_ci{
167bf215546Sopenharmony_ci   const struct si_reg *reg = find_register(gfx_level, offset);
168bf215546Sopenharmony_ci
169bf215546Sopenharmony_ci   if (reg) {
170bf215546Sopenharmony_ci      const char *reg_name = sid_strings + reg->name_offset;
171bf215546Sopenharmony_ci      bool first_field = true;
172bf215546Sopenharmony_ci
173bf215546Sopenharmony_ci      print_spaces(file, INDENT_PKT);
174bf215546Sopenharmony_ci      fprintf(file, "%s%s%s <- ",
175bf215546Sopenharmony_ci              O_COLOR_YELLOW, reg_name,
176bf215546Sopenharmony_ci              O_COLOR_RESET);
177bf215546Sopenharmony_ci
178bf215546Sopenharmony_ci      if (!reg->num_fields) {
179bf215546Sopenharmony_ci         print_value(file, value, 32);
180bf215546Sopenharmony_ci         return;
181bf215546Sopenharmony_ci      }
182bf215546Sopenharmony_ci
183bf215546Sopenharmony_ci      for (unsigned f = 0; f < reg->num_fields; f++) {
184bf215546Sopenharmony_ci         const struct si_field *field = sid_fields_table + reg->fields_offset + f;
185bf215546Sopenharmony_ci         const int *values_offsets = sid_strings_offsets + field->values_offset;
186bf215546Sopenharmony_ci         uint32_t val = (value & field->mask) >> (ffs(field->mask) - 1);
187bf215546Sopenharmony_ci
188bf215546Sopenharmony_ci         if (!(field->mask & field_mask))
189bf215546Sopenharmony_ci            continue;
190bf215546Sopenharmony_ci
191bf215546Sopenharmony_ci         /* Indent the field. */
192bf215546Sopenharmony_ci         if (!first_field)
193bf215546Sopenharmony_ci            print_spaces(file, INDENT_PKT + strlen(reg_name) + 4);
194bf215546Sopenharmony_ci
195bf215546Sopenharmony_ci         /* Print the field. */
196bf215546Sopenharmony_ci         fprintf(file, "%s = ", sid_strings + field->name_offset);
197bf215546Sopenharmony_ci
198bf215546Sopenharmony_ci         if (val < field->num_values && values_offsets[val] >= 0)
199bf215546Sopenharmony_ci            fprintf(file, "%s\n", sid_strings + values_offsets[val]);
200bf215546Sopenharmony_ci         else
201bf215546Sopenharmony_ci            print_value(file, val, util_bitcount(field->mask));
202bf215546Sopenharmony_ci
203bf215546Sopenharmony_ci         first_field = false;
204bf215546Sopenharmony_ci      }
205bf215546Sopenharmony_ci      return;
206bf215546Sopenharmony_ci   }
207bf215546Sopenharmony_ci
208bf215546Sopenharmony_ci   print_spaces(file, INDENT_PKT);
209bf215546Sopenharmony_ci   fprintf(file, "%s0x%05x%s <- 0x%08x\n",
210bf215546Sopenharmony_ci           O_COLOR_YELLOW, offset,
211bf215546Sopenharmony_ci           O_COLOR_RESET, value);
212bf215546Sopenharmony_ci}
213bf215546Sopenharmony_ci
214bf215546Sopenharmony_cistatic uint32_t ac_ib_get(struct ac_ib_parser *ib)
215bf215546Sopenharmony_ci{
216bf215546Sopenharmony_ci   uint32_t v = 0;
217bf215546Sopenharmony_ci
218bf215546Sopenharmony_ci   if (ib->cur_dw < ib->num_dw) {
219bf215546Sopenharmony_ci      v = ib->ib[ib->cur_dw];
220bf215546Sopenharmony_ci#ifdef HAVE_VALGRIND
221bf215546Sopenharmony_ci      /* Help figure out where garbage data is written to IBs.
222bf215546Sopenharmony_ci       *
223bf215546Sopenharmony_ci       * Arguably we should do this already when the IBs are written,
224bf215546Sopenharmony_ci       * see RADEON_VALGRIND. The problem is that client-requests to
225bf215546Sopenharmony_ci       * Valgrind have an overhead even when Valgrind isn't running,
226bf215546Sopenharmony_ci       * and radeon_emit is performance sensitive...
227bf215546Sopenharmony_ci       */
228bf215546Sopenharmony_ci      if (VALGRIND_CHECK_VALUE_IS_DEFINED(v))
229bf215546Sopenharmony_ci         fprintf(ib->f, "%sValgrind: The next DWORD is garbage%s\n",
230bf215546Sopenharmony_ci                 debug_get_option_color() ? COLOR_RED : "", O_COLOR_RESET);
231bf215546Sopenharmony_ci#endif
232bf215546Sopenharmony_ci      fprintf(ib->f, "\n\035#%08x ", v);
233bf215546Sopenharmony_ci   } else {
234bf215546Sopenharmony_ci      fprintf(ib->f, "\n\035#???????? ");
235bf215546Sopenharmony_ci   }
236bf215546Sopenharmony_ci
237bf215546Sopenharmony_ci   ib->cur_dw++;
238bf215546Sopenharmony_ci   return v;
239bf215546Sopenharmony_ci}
240bf215546Sopenharmony_ci
241bf215546Sopenharmony_cistatic void ac_parse_set_reg_packet(FILE *f, unsigned count, unsigned reg_offset,
242bf215546Sopenharmony_ci                                    struct ac_ib_parser *ib)
243bf215546Sopenharmony_ci{
244bf215546Sopenharmony_ci   unsigned reg_dw = ac_ib_get(ib);
245bf215546Sopenharmony_ci   unsigned reg = ((reg_dw & 0xFFFF) << 2) + reg_offset;
246bf215546Sopenharmony_ci   unsigned index = reg_dw >> 28;
247bf215546Sopenharmony_ci   int i;
248bf215546Sopenharmony_ci
249bf215546Sopenharmony_ci   if (index != 0) {
250bf215546Sopenharmony_ci      print_spaces(f, INDENT_PKT);
251bf215546Sopenharmony_ci      fprintf(f, "INDEX = %u\n", index);
252bf215546Sopenharmony_ci   }
253bf215546Sopenharmony_ci
254bf215546Sopenharmony_ci   for (i = 0; i < count; i++)
255bf215546Sopenharmony_ci      ac_dump_reg(f, ib->gfx_level, reg + i * 4, ac_ib_get(ib), ~0);
256bf215546Sopenharmony_ci}
257bf215546Sopenharmony_ci
258bf215546Sopenharmony_cistatic void ac_parse_packet3(FILE *f, uint32_t header, struct ac_ib_parser *ib,
259bf215546Sopenharmony_ci                             int *current_trace_id)
260bf215546Sopenharmony_ci{
261bf215546Sopenharmony_ci   unsigned first_dw = ib->cur_dw;
262bf215546Sopenharmony_ci   int count = PKT_COUNT_G(header);
263bf215546Sopenharmony_ci   unsigned op = PKT3_IT_OPCODE_G(header);
264bf215546Sopenharmony_ci   const char *predicate = PKT3_PREDICATE(header) ? "(predicate)" : "";
265bf215546Sopenharmony_ci   int i;
266bf215546Sopenharmony_ci
267bf215546Sopenharmony_ci   /* Print the name first. */
268bf215546Sopenharmony_ci   for (i = 0; i < ARRAY_SIZE(packet3_table); i++)
269bf215546Sopenharmony_ci      if (packet3_table[i].op == op)
270bf215546Sopenharmony_ci         break;
271bf215546Sopenharmony_ci
272bf215546Sopenharmony_ci   if (i < ARRAY_SIZE(packet3_table)) {
273bf215546Sopenharmony_ci      const char *name = sid_strings + packet3_table[i].name_offset;
274bf215546Sopenharmony_ci
275bf215546Sopenharmony_ci      if (op == PKT3_SET_CONTEXT_REG || op == PKT3_SET_CONFIG_REG || op == PKT3_SET_UCONFIG_REG ||
276bf215546Sopenharmony_ci          op == PKT3_SET_UCONFIG_REG_INDEX || op == PKT3_SET_SH_REG || op == PKT3_SET_SH_REG_INDEX)
277bf215546Sopenharmony_ci         fprintf(f, "%s%s%s%s:\n", O_COLOR_CYAN, name, predicate, O_COLOR_RESET);
278bf215546Sopenharmony_ci      else
279bf215546Sopenharmony_ci         fprintf(f, "%s%s%s%s:\n", O_COLOR_GREEN, name, predicate, O_COLOR_RESET);
280bf215546Sopenharmony_ci   } else
281bf215546Sopenharmony_ci      fprintf(f, "%sPKT3_UNKNOWN 0x%x%s%s:\n", O_COLOR_RED, op, predicate, O_COLOR_RESET);
282bf215546Sopenharmony_ci
283bf215546Sopenharmony_ci   /* Print the contents. */
284bf215546Sopenharmony_ci   switch (op) {
285bf215546Sopenharmony_ci   case PKT3_SET_CONTEXT_REG:
286bf215546Sopenharmony_ci      ac_parse_set_reg_packet(f, count, SI_CONTEXT_REG_OFFSET, ib);
287bf215546Sopenharmony_ci      break;
288bf215546Sopenharmony_ci   case PKT3_SET_CONFIG_REG:
289bf215546Sopenharmony_ci      ac_parse_set_reg_packet(f, count, SI_CONFIG_REG_OFFSET, ib);
290bf215546Sopenharmony_ci      break;
291bf215546Sopenharmony_ci   case PKT3_SET_UCONFIG_REG:
292bf215546Sopenharmony_ci   case PKT3_SET_UCONFIG_REG_INDEX:
293bf215546Sopenharmony_ci      ac_parse_set_reg_packet(f, count, CIK_UCONFIG_REG_OFFSET, ib);
294bf215546Sopenharmony_ci      break;
295bf215546Sopenharmony_ci   case PKT3_SET_SH_REG:
296bf215546Sopenharmony_ci   case PKT3_SET_SH_REG_INDEX:
297bf215546Sopenharmony_ci      ac_parse_set_reg_packet(f, count, SI_SH_REG_OFFSET, ib);
298bf215546Sopenharmony_ci      break;
299bf215546Sopenharmony_ci   case PKT3_ACQUIRE_MEM:
300bf215546Sopenharmony_ci      if (ib->gfx_level >= GFX11 && G_585_PWS_ENA(ib->ib[ib->cur_dw + 5])) {
301bf215546Sopenharmony_ci         ac_dump_reg(f, ib->gfx_level, R_580_ACQUIRE_MEM_PWS_2, ac_ib_get(ib), ~0);
302bf215546Sopenharmony_ci         print_named_value(f, "GCR_SIZE", ac_ib_get(ib), 32);
303bf215546Sopenharmony_ci         print_named_value(f, "GCR_SIZE_HI", ac_ib_get(ib), 25);
304bf215546Sopenharmony_ci         print_named_value(f, "GCR_BASE_LO", ac_ib_get(ib), 32);
305bf215546Sopenharmony_ci         print_named_value(f, "GCR_BASE_HI", ac_ib_get(ib), 32);
306bf215546Sopenharmony_ci         ac_dump_reg(f, ib->gfx_level, R_585_ACQUIRE_MEM_PWS_7, ac_ib_get(ib), ~0);
307bf215546Sopenharmony_ci         ac_dump_reg(f, ib->gfx_level, R_586_GCR_CNTL, ac_ib_get(ib), ~0);
308bf215546Sopenharmony_ci         break;
309bf215546Sopenharmony_ci      }
310bf215546Sopenharmony_ci      ac_dump_reg(f, ib->gfx_level, R_0301F0_CP_COHER_CNTL, ac_ib_get(ib), ~0);
311bf215546Sopenharmony_ci      ac_dump_reg(f, ib->gfx_level, R_0301F4_CP_COHER_SIZE, ac_ib_get(ib), ~0);
312bf215546Sopenharmony_ci      ac_dump_reg(f, ib->gfx_level, R_030230_CP_COHER_SIZE_HI, ac_ib_get(ib), ~0);
313bf215546Sopenharmony_ci      ac_dump_reg(f, ib->gfx_level, R_0301F8_CP_COHER_BASE, ac_ib_get(ib), ~0);
314bf215546Sopenharmony_ci      ac_dump_reg(f, ib->gfx_level, R_0301E4_CP_COHER_BASE_HI, ac_ib_get(ib), ~0);
315bf215546Sopenharmony_ci      print_named_value(f, "POLL_INTERVAL", ac_ib_get(ib), 16);
316bf215546Sopenharmony_ci      if (ib->gfx_level >= GFX10)
317bf215546Sopenharmony_ci         ac_dump_reg(f, ib->gfx_level, R_586_GCR_CNTL, ac_ib_get(ib), ~0);
318bf215546Sopenharmony_ci      break;
319bf215546Sopenharmony_ci   case PKT3_SURFACE_SYNC:
320bf215546Sopenharmony_ci      if (ib->gfx_level >= GFX7) {
321bf215546Sopenharmony_ci         ac_dump_reg(f, ib->gfx_level, R_0301F0_CP_COHER_CNTL, ac_ib_get(ib), ~0);
322bf215546Sopenharmony_ci         ac_dump_reg(f, ib->gfx_level, R_0301F4_CP_COHER_SIZE, ac_ib_get(ib), ~0);
323bf215546Sopenharmony_ci         ac_dump_reg(f, ib->gfx_level, R_0301F8_CP_COHER_BASE, ac_ib_get(ib), ~0);
324bf215546Sopenharmony_ci      } else {
325bf215546Sopenharmony_ci         ac_dump_reg(f, ib->gfx_level, R_0085F0_CP_COHER_CNTL, ac_ib_get(ib), ~0);
326bf215546Sopenharmony_ci         ac_dump_reg(f, ib->gfx_level, R_0085F4_CP_COHER_SIZE, ac_ib_get(ib), ~0);
327bf215546Sopenharmony_ci         ac_dump_reg(f, ib->gfx_level, R_0085F8_CP_COHER_BASE, ac_ib_get(ib), ~0);
328bf215546Sopenharmony_ci      }
329bf215546Sopenharmony_ci      print_named_value(f, "POLL_INTERVAL", ac_ib_get(ib), 16);
330bf215546Sopenharmony_ci      break;
331bf215546Sopenharmony_ci   case PKT3_EVENT_WRITE: {
332bf215546Sopenharmony_ci      uint32_t event_dw = ac_ib_get(ib);
333bf215546Sopenharmony_ci      ac_dump_reg(f, ib->gfx_level, R_028A90_VGT_EVENT_INITIATOR, event_dw,
334bf215546Sopenharmony_ci                  S_028A90_EVENT_TYPE(~0));
335bf215546Sopenharmony_ci      print_named_value(f, "EVENT_INDEX", (event_dw >> 8) & 0xf, 4);
336bf215546Sopenharmony_ci      print_named_value(f, "INV_L2", (event_dw >> 20) & 0x1, 1);
337bf215546Sopenharmony_ci      if (count > 0) {
338bf215546Sopenharmony_ci         print_named_value(f, "ADDRESS_LO", ac_ib_get(ib), 32);
339bf215546Sopenharmony_ci         print_named_value(f, "ADDRESS_HI", ac_ib_get(ib), 16);
340bf215546Sopenharmony_ci      }
341bf215546Sopenharmony_ci      break;
342bf215546Sopenharmony_ci   }
343bf215546Sopenharmony_ci   case PKT3_EVENT_WRITE_EOP: {
344bf215546Sopenharmony_ci      uint32_t event_dw = ac_ib_get(ib);
345bf215546Sopenharmony_ci      ac_dump_reg(f, ib->gfx_level, R_028A90_VGT_EVENT_INITIATOR, event_dw,
346bf215546Sopenharmony_ci                  S_028A90_EVENT_TYPE(~0));
347bf215546Sopenharmony_ci      print_named_value(f, "EVENT_INDEX", (event_dw >> 8) & 0xf, 4);
348bf215546Sopenharmony_ci      print_named_value(f, "TCL1_VOL_ACTION_ENA", (event_dw >> 12) & 0x1, 1);
349bf215546Sopenharmony_ci      print_named_value(f, "TC_VOL_ACTION_ENA", (event_dw >> 13) & 0x1, 1);
350bf215546Sopenharmony_ci      print_named_value(f, "TC_WB_ACTION_ENA", (event_dw >> 15) & 0x1, 1);
351bf215546Sopenharmony_ci      print_named_value(f, "TCL1_ACTION_ENA", (event_dw >> 16) & 0x1, 1);
352bf215546Sopenharmony_ci      print_named_value(f, "TC_ACTION_ENA", (event_dw >> 17) & 0x1, 1);
353bf215546Sopenharmony_ci      print_named_value(f, "ADDRESS_LO", ac_ib_get(ib), 32);
354bf215546Sopenharmony_ci      uint32_t addr_hi_dw = ac_ib_get(ib);
355bf215546Sopenharmony_ci      print_named_value(f, "ADDRESS_HI", addr_hi_dw, 16);
356bf215546Sopenharmony_ci      print_named_value(f, "DST_SEL", (addr_hi_dw >> 16) & 0x3, 2);
357bf215546Sopenharmony_ci      print_named_value(f, "INT_SEL", (addr_hi_dw >> 24) & 0x7, 3);
358bf215546Sopenharmony_ci      print_named_value(f, "DATA_SEL", addr_hi_dw >> 29, 3);
359bf215546Sopenharmony_ci      print_named_value(f, "DATA_LO", ac_ib_get(ib), 32);
360bf215546Sopenharmony_ci      print_named_value(f, "DATA_HI", ac_ib_get(ib), 32);
361bf215546Sopenharmony_ci      break;
362bf215546Sopenharmony_ci   }
363bf215546Sopenharmony_ci   case PKT3_RELEASE_MEM: {
364bf215546Sopenharmony_ci      uint32_t event_dw = ac_ib_get(ib);
365bf215546Sopenharmony_ci      if (ib->gfx_level >= GFX10) {
366bf215546Sopenharmony_ci         ac_dump_reg(f, ib->gfx_level, R_490_RELEASE_MEM_OP, event_dw, ~0u);
367bf215546Sopenharmony_ci      } else {
368bf215546Sopenharmony_ci         ac_dump_reg(f, ib->gfx_level, R_028A90_VGT_EVENT_INITIATOR, event_dw,
369bf215546Sopenharmony_ci                     S_028A90_EVENT_TYPE(~0));
370bf215546Sopenharmony_ci         print_named_value(f, "EVENT_INDEX", (event_dw >> 8) & 0xf, 4);
371bf215546Sopenharmony_ci         print_named_value(f, "TCL1_VOL_ACTION_ENA", (event_dw >> 12) & 0x1, 1);
372bf215546Sopenharmony_ci         print_named_value(f, "TC_VOL_ACTION_ENA", (event_dw >> 13) & 0x1, 1);
373bf215546Sopenharmony_ci         print_named_value(f, "TC_WB_ACTION_ENA", (event_dw >> 15) & 0x1, 1);
374bf215546Sopenharmony_ci         print_named_value(f, "TCL1_ACTION_ENA", (event_dw >> 16) & 0x1, 1);
375bf215546Sopenharmony_ci         print_named_value(f, "TC_ACTION_ENA", (event_dw >> 17) & 0x1, 1);
376bf215546Sopenharmony_ci         print_named_value(f, "TC_NC_ACTION_ENA", (event_dw >> 19) & 0x1, 1);
377bf215546Sopenharmony_ci         print_named_value(f, "TC_WC_ACTION_ENA", (event_dw >> 20) & 0x1, 1);
378bf215546Sopenharmony_ci         print_named_value(f, "TC_MD_ACTION_ENA", (event_dw >> 21) & 0x1, 1);
379bf215546Sopenharmony_ci      }
380bf215546Sopenharmony_ci      uint32_t sel_dw = ac_ib_get(ib);
381bf215546Sopenharmony_ci      print_named_value(f, "DST_SEL", (sel_dw >> 16) & 0x3, 2);
382bf215546Sopenharmony_ci      print_named_value(f, "INT_SEL", (sel_dw >> 24) & 0x7, 3);
383bf215546Sopenharmony_ci      print_named_value(f, "DATA_SEL", sel_dw >> 29, 3);
384bf215546Sopenharmony_ci      print_named_value(f, "ADDRESS_LO", ac_ib_get(ib), 32);
385bf215546Sopenharmony_ci      print_named_value(f, "ADDRESS_HI", ac_ib_get(ib), 32);
386bf215546Sopenharmony_ci      print_named_value(f, "DATA_LO", ac_ib_get(ib), 32);
387bf215546Sopenharmony_ci      print_named_value(f, "DATA_HI", ac_ib_get(ib), 32);
388bf215546Sopenharmony_ci      print_named_value(f, "CTXID", ac_ib_get(ib), 32);
389bf215546Sopenharmony_ci      break;
390bf215546Sopenharmony_ci   }
391bf215546Sopenharmony_ci   case PKT3_WAIT_REG_MEM:
392bf215546Sopenharmony_ci      print_named_value(f, "OP", ac_ib_get(ib), 32);
393bf215546Sopenharmony_ci      print_named_value(f, "ADDRESS_LO", ac_ib_get(ib), 32);
394bf215546Sopenharmony_ci      print_named_value(f, "ADDRESS_HI", ac_ib_get(ib), 32);
395bf215546Sopenharmony_ci      print_named_value(f, "REF", ac_ib_get(ib), 32);
396bf215546Sopenharmony_ci      print_named_value(f, "MASK", ac_ib_get(ib), 32);
397bf215546Sopenharmony_ci      print_named_value(f, "POLL_INTERVAL", ac_ib_get(ib), 16);
398bf215546Sopenharmony_ci      break;
399bf215546Sopenharmony_ci   case PKT3_DRAW_INDEX_AUTO:
400bf215546Sopenharmony_ci      ac_dump_reg(f, ib->gfx_level, R_030930_VGT_NUM_INDICES, ac_ib_get(ib), ~0);
401bf215546Sopenharmony_ci      ac_dump_reg(f, ib->gfx_level, R_0287F0_VGT_DRAW_INITIATOR, ac_ib_get(ib), ~0);
402bf215546Sopenharmony_ci      break;
403bf215546Sopenharmony_ci   case PKT3_DRAW_INDEX_2:
404bf215546Sopenharmony_ci      ac_dump_reg(f, ib->gfx_level, R_028A78_VGT_DMA_MAX_SIZE, ac_ib_get(ib), ~0);
405bf215546Sopenharmony_ci      ac_dump_reg(f, ib->gfx_level, R_0287E8_VGT_DMA_BASE, ac_ib_get(ib), ~0);
406bf215546Sopenharmony_ci      ac_dump_reg(f, ib->gfx_level, R_0287E4_VGT_DMA_BASE_HI, ac_ib_get(ib), ~0);
407bf215546Sopenharmony_ci      ac_dump_reg(f, ib->gfx_level, R_030930_VGT_NUM_INDICES, ac_ib_get(ib), ~0);
408bf215546Sopenharmony_ci      ac_dump_reg(f, ib->gfx_level, R_0287F0_VGT_DRAW_INITIATOR, ac_ib_get(ib), ~0);
409bf215546Sopenharmony_ci      break;
410bf215546Sopenharmony_ci   case PKT3_INDEX_TYPE:
411bf215546Sopenharmony_ci      ac_dump_reg(f, ib->gfx_level, R_028A7C_VGT_DMA_INDEX_TYPE, ac_ib_get(ib), ~0);
412bf215546Sopenharmony_ci      break;
413bf215546Sopenharmony_ci   case PKT3_NUM_INSTANCES:
414bf215546Sopenharmony_ci      ac_dump_reg(f, ib->gfx_level, R_030934_VGT_NUM_INSTANCES, ac_ib_get(ib), ~0);
415bf215546Sopenharmony_ci      break;
416bf215546Sopenharmony_ci   case PKT3_WRITE_DATA:
417bf215546Sopenharmony_ci      ac_dump_reg(f, ib->gfx_level, R_370_CONTROL, ac_ib_get(ib), ~0);
418bf215546Sopenharmony_ci      ac_dump_reg(f, ib->gfx_level, R_371_DST_ADDR_LO, ac_ib_get(ib), ~0);
419bf215546Sopenharmony_ci      ac_dump_reg(f, ib->gfx_level, R_372_DST_ADDR_HI, ac_ib_get(ib), ~0);
420bf215546Sopenharmony_ci      /* The payload is written automatically */
421bf215546Sopenharmony_ci      break;
422bf215546Sopenharmony_ci   case PKT3_CP_DMA:
423bf215546Sopenharmony_ci      ac_dump_reg(f, ib->gfx_level, R_410_CP_DMA_WORD0, ac_ib_get(ib), ~0);
424bf215546Sopenharmony_ci      ac_dump_reg(f, ib->gfx_level, R_411_CP_DMA_WORD1, ac_ib_get(ib), ~0);
425bf215546Sopenharmony_ci      ac_dump_reg(f, ib->gfx_level, R_412_CP_DMA_WORD2, ac_ib_get(ib), ~0);
426bf215546Sopenharmony_ci      ac_dump_reg(f, ib->gfx_level, R_413_CP_DMA_WORD3, ac_ib_get(ib), ~0);
427bf215546Sopenharmony_ci      ac_dump_reg(f, ib->gfx_level, R_415_COMMAND, ac_ib_get(ib), ~0);
428bf215546Sopenharmony_ci      break;
429bf215546Sopenharmony_ci   case PKT3_DMA_DATA:
430bf215546Sopenharmony_ci      ac_dump_reg(f, ib->gfx_level, R_500_DMA_DATA_WORD0, ac_ib_get(ib), ~0);
431bf215546Sopenharmony_ci      ac_dump_reg(f, ib->gfx_level, R_501_SRC_ADDR_LO, ac_ib_get(ib), ~0);
432bf215546Sopenharmony_ci      ac_dump_reg(f, ib->gfx_level, R_502_SRC_ADDR_HI, ac_ib_get(ib), ~0);
433bf215546Sopenharmony_ci      ac_dump_reg(f, ib->gfx_level, R_503_DST_ADDR_LO, ac_ib_get(ib), ~0);
434bf215546Sopenharmony_ci      ac_dump_reg(f, ib->gfx_level, R_504_DST_ADDR_HI, ac_ib_get(ib), ~0);
435bf215546Sopenharmony_ci      ac_dump_reg(f, ib->gfx_level, R_415_COMMAND, ac_ib_get(ib), ~0);
436bf215546Sopenharmony_ci      break;
437bf215546Sopenharmony_ci   case PKT3_INDIRECT_BUFFER_SI:
438bf215546Sopenharmony_ci   case PKT3_INDIRECT_BUFFER_CONST:
439bf215546Sopenharmony_ci   case PKT3_INDIRECT_BUFFER_CIK: {
440bf215546Sopenharmony_ci      uint32_t base_lo_dw = ac_ib_get(ib);
441bf215546Sopenharmony_ci      ac_dump_reg(f, ib->gfx_level, R_3F0_IB_BASE_LO, base_lo_dw, ~0);
442bf215546Sopenharmony_ci      uint32_t base_hi_dw = ac_ib_get(ib);
443bf215546Sopenharmony_ci      ac_dump_reg(f, ib->gfx_level, R_3F1_IB_BASE_HI, base_hi_dw, ~0);
444bf215546Sopenharmony_ci      uint32_t control_dw = ac_ib_get(ib);
445bf215546Sopenharmony_ci      ac_dump_reg(f, ib->gfx_level, R_3F2_IB_CONTROL, control_dw, ~0);
446bf215546Sopenharmony_ci
447bf215546Sopenharmony_ci      if (!ib->addr_callback)
448bf215546Sopenharmony_ci         break;
449bf215546Sopenharmony_ci
450bf215546Sopenharmony_ci      uint64_t addr = ((uint64_t)base_hi_dw << 32) | base_lo_dw;
451bf215546Sopenharmony_ci      void *data = ib->addr_callback(ib->addr_callback_data, addr);
452bf215546Sopenharmony_ci      if (!data)
453bf215546Sopenharmony_ci         break;
454bf215546Sopenharmony_ci
455bf215546Sopenharmony_ci      if (G_3F2_CHAIN(control_dw)) {
456bf215546Sopenharmony_ci         ib->ib = data;
457bf215546Sopenharmony_ci         ib->num_dw = G_3F2_IB_SIZE(control_dw);
458bf215546Sopenharmony_ci         ib->cur_dw = 0;
459bf215546Sopenharmony_ci         return;
460bf215546Sopenharmony_ci      }
461bf215546Sopenharmony_ci
462bf215546Sopenharmony_ci      struct ac_ib_parser ib_recurse;
463bf215546Sopenharmony_ci      memcpy(&ib_recurse, ib, sizeof(ib_recurse));
464bf215546Sopenharmony_ci      ib_recurse.ib = data;
465bf215546Sopenharmony_ci      ib_recurse.num_dw = G_3F2_IB_SIZE(control_dw);
466bf215546Sopenharmony_ci      ib_recurse.cur_dw = 0;
467bf215546Sopenharmony_ci      if (ib_recurse.trace_id_count) {
468bf215546Sopenharmony_ci         if (*current_trace_id == *ib->trace_ids) {
469bf215546Sopenharmony_ci            ++ib_recurse.trace_ids;
470bf215546Sopenharmony_ci            --ib_recurse.trace_id_count;
471bf215546Sopenharmony_ci         } else {
472bf215546Sopenharmony_ci            ib_recurse.trace_id_count = 0;
473bf215546Sopenharmony_ci         }
474bf215546Sopenharmony_ci      }
475bf215546Sopenharmony_ci
476bf215546Sopenharmony_ci      fprintf(f, "\n\035>------------------ nested begin ------------------\n");
477bf215546Sopenharmony_ci      ac_do_parse_ib(f, &ib_recurse);
478bf215546Sopenharmony_ci      fprintf(f, "\n\035<------------------- nested end -------------------\n");
479bf215546Sopenharmony_ci      break;
480bf215546Sopenharmony_ci   }
481bf215546Sopenharmony_ci   case PKT3_CLEAR_STATE:
482bf215546Sopenharmony_ci   case PKT3_INCREMENT_DE_COUNTER:
483bf215546Sopenharmony_ci   case PKT3_PFP_SYNC_ME:
484bf215546Sopenharmony_ci      break;
485bf215546Sopenharmony_ci   case PKT3_NOP:
486bf215546Sopenharmony_ci      if (header == PKT3_NOP_PAD) {
487bf215546Sopenharmony_ci         count = -1; /* One dword NOP. */
488bf215546Sopenharmony_ci      } else if (count == 0 && ib->cur_dw < ib->num_dw && AC_IS_TRACE_POINT(ib->ib[ib->cur_dw])) {
489bf215546Sopenharmony_ci         unsigned packet_id = AC_GET_TRACE_POINT_ID(ib->ib[ib->cur_dw]);
490bf215546Sopenharmony_ci
491bf215546Sopenharmony_ci         print_spaces(f, INDENT_PKT);
492bf215546Sopenharmony_ci         fprintf(f, "%sTrace point ID: %u%s\n", O_COLOR_RED, packet_id, O_COLOR_RESET);
493bf215546Sopenharmony_ci
494bf215546Sopenharmony_ci         if (!ib->trace_id_count)
495bf215546Sopenharmony_ci            break; /* tracing was disabled */
496bf215546Sopenharmony_ci
497bf215546Sopenharmony_ci         *current_trace_id = packet_id;
498bf215546Sopenharmony_ci
499bf215546Sopenharmony_ci         print_spaces(f, INDENT_PKT);
500bf215546Sopenharmony_ci         if (packet_id < *ib->trace_ids) {
501bf215546Sopenharmony_ci            fprintf(f, "%sThis trace point was reached by the CP.%s\n",
502bf215546Sopenharmony_ci                    O_COLOR_RED, O_COLOR_RESET);
503bf215546Sopenharmony_ci         } else if (packet_id == *ib->trace_ids) {
504bf215546Sopenharmony_ci            fprintf(f, "%s!!!!! This is the last trace point that "
505bf215546Sopenharmony_ci                                 "was reached by the CP !!!!!%s\n",
506bf215546Sopenharmony_ci                    O_COLOR_RED, O_COLOR_RESET);
507bf215546Sopenharmony_ci         } else if (packet_id + 1 == *ib->trace_ids) {
508bf215546Sopenharmony_ci            fprintf(f, "%s!!!!! This is the first trace point that "
509bf215546Sopenharmony_ci                                 "was NOT been reached by the CP !!!!!%s\n",
510bf215546Sopenharmony_ci                    O_COLOR_RED, O_COLOR_RESET);
511bf215546Sopenharmony_ci         } else {
512bf215546Sopenharmony_ci            fprintf(f, "%s!!!!! This trace point was NOT reached "
513bf215546Sopenharmony_ci                                 "by the CP !!!!!%s\n",
514bf215546Sopenharmony_ci                    O_COLOR_RED, O_COLOR_RESET);
515bf215546Sopenharmony_ci         }
516bf215546Sopenharmony_ci         break;
517bf215546Sopenharmony_ci      }
518bf215546Sopenharmony_ci      break;
519bf215546Sopenharmony_ci   }
520bf215546Sopenharmony_ci
521bf215546Sopenharmony_ci   /* print additional dwords */
522bf215546Sopenharmony_ci   while (ib->cur_dw <= first_dw + count)
523bf215546Sopenharmony_ci      ac_ib_get(ib);
524bf215546Sopenharmony_ci
525bf215546Sopenharmony_ci   if (ib->cur_dw > first_dw + count + 1)
526bf215546Sopenharmony_ci      fprintf(f, "%s !!!!! count in header too low !!!!!%s\n",
527bf215546Sopenharmony_ci              O_COLOR_RED, O_COLOR_RESET);
528bf215546Sopenharmony_ci}
529bf215546Sopenharmony_ci
530bf215546Sopenharmony_ci/**
531bf215546Sopenharmony_ci * Parse and print an IB into a file.
532bf215546Sopenharmony_ci */
533bf215546Sopenharmony_cistatic void ac_do_parse_ib(FILE *f, struct ac_ib_parser *ib)
534bf215546Sopenharmony_ci{
535bf215546Sopenharmony_ci   int current_trace_id = -1;
536bf215546Sopenharmony_ci
537bf215546Sopenharmony_ci   while (ib->cur_dw < ib->num_dw) {
538bf215546Sopenharmony_ci      uint32_t header = ac_ib_get(ib);
539bf215546Sopenharmony_ci      unsigned type = PKT_TYPE_G(header);
540bf215546Sopenharmony_ci
541bf215546Sopenharmony_ci      switch (type) {
542bf215546Sopenharmony_ci      case 3:
543bf215546Sopenharmony_ci         ac_parse_packet3(f, header, ib, &current_trace_id);
544bf215546Sopenharmony_ci         break;
545bf215546Sopenharmony_ci      case 2:
546bf215546Sopenharmony_ci         /* type-2 nop */
547bf215546Sopenharmony_ci         if (header == 0x80000000) {
548bf215546Sopenharmony_ci            fprintf(f, "%sNOP (type 2)%s\n",
549bf215546Sopenharmony_ci                    O_COLOR_GREEN, O_COLOR_RESET);
550bf215546Sopenharmony_ci            break;
551bf215546Sopenharmony_ci         }
552bf215546Sopenharmony_ci         FALLTHROUGH;
553bf215546Sopenharmony_ci      default:
554bf215546Sopenharmony_ci         fprintf(f, "Unknown packet type %i\n", type);
555bf215546Sopenharmony_ci         break;
556bf215546Sopenharmony_ci      }
557bf215546Sopenharmony_ci   }
558bf215546Sopenharmony_ci}
559bf215546Sopenharmony_ci
560bf215546Sopenharmony_cistatic void format_ib_output(FILE *f, char *out)
561bf215546Sopenharmony_ci{
562bf215546Sopenharmony_ci   unsigned depth = 0;
563bf215546Sopenharmony_ci
564bf215546Sopenharmony_ci   for (;;) {
565bf215546Sopenharmony_ci      char op = 0;
566bf215546Sopenharmony_ci
567bf215546Sopenharmony_ci      if (out[0] == '\n' && out[1] == '\035')
568bf215546Sopenharmony_ci         out++;
569bf215546Sopenharmony_ci      if (out[0] == '\035') {
570bf215546Sopenharmony_ci         op = out[1];
571bf215546Sopenharmony_ci         out += 2;
572bf215546Sopenharmony_ci      }
573bf215546Sopenharmony_ci
574bf215546Sopenharmony_ci      if (op == '<')
575bf215546Sopenharmony_ci         depth--;
576bf215546Sopenharmony_ci
577bf215546Sopenharmony_ci      unsigned indent = 4 * depth;
578bf215546Sopenharmony_ci      if (op != '#')
579bf215546Sopenharmony_ci         indent += 9;
580bf215546Sopenharmony_ci
581bf215546Sopenharmony_ci      if (indent)
582bf215546Sopenharmony_ci         print_spaces(f, indent);
583bf215546Sopenharmony_ci
584bf215546Sopenharmony_ci      char *end = strchrnul(out, '\n');
585bf215546Sopenharmony_ci      fwrite(out, end - out, 1, f);
586bf215546Sopenharmony_ci      fputc('\n', f); /* always end with a new line */
587bf215546Sopenharmony_ci      if (!*end)
588bf215546Sopenharmony_ci         break;
589bf215546Sopenharmony_ci
590bf215546Sopenharmony_ci      out = end + 1;
591bf215546Sopenharmony_ci
592bf215546Sopenharmony_ci      if (op == '>')
593bf215546Sopenharmony_ci         depth++;
594bf215546Sopenharmony_ci   }
595bf215546Sopenharmony_ci}
596bf215546Sopenharmony_ci
597bf215546Sopenharmony_ci/**
598bf215546Sopenharmony_ci * Parse and print an IB into a file.
599bf215546Sopenharmony_ci *
600bf215546Sopenharmony_ci * \param f            file
601bf215546Sopenharmony_ci * \param ib_ptr       IB
602bf215546Sopenharmony_ci * \param num_dw       size of the IB
603bf215546Sopenharmony_ci * \param gfx_level   gfx level
604bf215546Sopenharmony_ci * \param trace_ids	the last trace IDs that are known to have been reached
605bf215546Sopenharmony_ci *			and executed by the CP, typically read from a buffer
606bf215546Sopenharmony_ci * \param trace_id_count The number of entries in the trace_ids array.
607bf215546Sopenharmony_ci * \param addr_callback Get a mapped pointer of the IB at a given address. Can
608bf215546Sopenharmony_ci *                      be NULL.
609bf215546Sopenharmony_ci * \param addr_callback_data user data for addr_callback
610bf215546Sopenharmony_ci */
611bf215546Sopenharmony_civoid ac_parse_ib_chunk(FILE *f, uint32_t *ib_ptr, int num_dw, const int *trace_ids,
612bf215546Sopenharmony_ci                       unsigned trace_id_count, enum amd_gfx_level gfx_level,
613bf215546Sopenharmony_ci                       ac_debug_addr_callback addr_callback, void *addr_callback_data)
614bf215546Sopenharmony_ci{
615bf215546Sopenharmony_ci   struct ac_ib_parser ib = {0};
616bf215546Sopenharmony_ci   ib.ib = ib_ptr;
617bf215546Sopenharmony_ci   ib.num_dw = num_dw;
618bf215546Sopenharmony_ci   ib.trace_ids = trace_ids;
619bf215546Sopenharmony_ci   ib.trace_id_count = trace_id_count;
620bf215546Sopenharmony_ci   ib.gfx_level = gfx_level;
621bf215546Sopenharmony_ci   ib.addr_callback = addr_callback;
622bf215546Sopenharmony_ci   ib.addr_callback_data = addr_callback_data;
623bf215546Sopenharmony_ci
624bf215546Sopenharmony_ci   char *out;
625bf215546Sopenharmony_ci   size_t outsize;
626bf215546Sopenharmony_ci   struct u_memstream mem;
627bf215546Sopenharmony_ci   u_memstream_open(&mem, &out, &outsize);
628bf215546Sopenharmony_ci   FILE *const memf = u_memstream_get(&mem);
629bf215546Sopenharmony_ci   ib.f = memf;
630bf215546Sopenharmony_ci   ac_do_parse_ib(memf, &ib);
631bf215546Sopenharmony_ci   u_memstream_close(&mem);
632bf215546Sopenharmony_ci
633bf215546Sopenharmony_ci   if (out) {
634bf215546Sopenharmony_ci      format_ib_output(f, out);
635bf215546Sopenharmony_ci      free(out);
636bf215546Sopenharmony_ci   }
637bf215546Sopenharmony_ci
638bf215546Sopenharmony_ci   if (ib.cur_dw > ib.num_dw) {
639bf215546Sopenharmony_ci      printf("\nPacket ends after the end of IB.\n");
640bf215546Sopenharmony_ci      exit(1);
641bf215546Sopenharmony_ci   }
642bf215546Sopenharmony_ci}
643bf215546Sopenharmony_ci
644bf215546Sopenharmony_ci/**
645bf215546Sopenharmony_ci * Parse and print an IB into a file.
646bf215546Sopenharmony_ci *
647bf215546Sopenharmony_ci * \param f		file
648bf215546Sopenharmony_ci * \param ib		IB
649bf215546Sopenharmony_ci * \param num_dw	size of the IB
650bf215546Sopenharmony_ci * \param gfx_level	gfx level
651bf215546Sopenharmony_ci * \param trace_ids	the last trace IDs that are known to have been reached
652bf215546Sopenharmony_ci *			and executed by the CP, typically read from a buffer
653bf215546Sopenharmony_ci * \param trace_id_count The number of entries in the trace_ids array.
654bf215546Sopenharmony_ci * \param addr_callback Get a mapped pointer of the IB at a given address. Can
655bf215546Sopenharmony_ci *                      be NULL.
656bf215546Sopenharmony_ci * \param addr_callback_data user data for addr_callback
657bf215546Sopenharmony_ci */
658bf215546Sopenharmony_civoid ac_parse_ib(FILE *f, uint32_t *ib, int num_dw, const int *trace_ids, unsigned trace_id_count,
659bf215546Sopenharmony_ci                 const char *name, enum amd_gfx_level gfx_level, ac_debug_addr_callback addr_callback,
660bf215546Sopenharmony_ci                 void *addr_callback_data)
661bf215546Sopenharmony_ci{
662bf215546Sopenharmony_ci   fprintf(f, "------------------ %s begin ------------------\n", name);
663bf215546Sopenharmony_ci
664bf215546Sopenharmony_ci   ac_parse_ib_chunk(f, ib, num_dw, trace_ids, trace_id_count, gfx_level, addr_callback,
665bf215546Sopenharmony_ci                     addr_callback_data);
666bf215546Sopenharmony_ci
667bf215546Sopenharmony_ci   fprintf(f, "------------------- %s end -------------------\n\n", name);
668bf215546Sopenharmony_ci}
669bf215546Sopenharmony_ci
670bf215546Sopenharmony_ci/**
671bf215546Sopenharmony_ci * Parse dmesg and return TRUE if a VM fault has been detected.
672bf215546Sopenharmony_ci *
673bf215546Sopenharmony_ci * \param gfx_level		gfx level
674bf215546Sopenharmony_ci * \param old_dmesg_timestamp	previous dmesg timestamp parsed at init time
675bf215546Sopenharmony_ci * \param out_addr		detected VM fault addr
676bf215546Sopenharmony_ci */
677bf215546Sopenharmony_cibool ac_vm_fault_occured(enum amd_gfx_level gfx_level, uint64_t *old_dmesg_timestamp,
678bf215546Sopenharmony_ci                         uint64_t *out_addr)
679bf215546Sopenharmony_ci{
680bf215546Sopenharmony_ci#ifdef _WIN32
681bf215546Sopenharmony_ci   return false;
682bf215546Sopenharmony_ci#else
683bf215546Sopenharmony_ci   char line[2000];
684bf215546Sopenharmony_ci   unsigned sec, usec;
685bf215546Sopenharmony_ci   int progress = 0;
686bf215546Sopenharmony_ci   uint64_t dmesg_timestamp = 0;
687bf215546Sopenharmony_ci   bool fault = false;
688bf215546Sopenharmony_ci
689bf215546Sopenharmony_ci   FILE *p = popen("dmesg", "r");
690bf215546Sopenharmony_ci   if (!p)
691bf215546Sopenharmony_ci      return false;
692bf215546Sopenharmony_ci
693bf215546Sopenharmony_ci   while (fgets(line, sizeof(line), p)) {
694bf215546Sopenharmony_ci      char *msg, len;
695bf215546Sopenharmony_ci
696bf215546Sopenharmony_ci      if (!line[0] || line[0] == '\n')
697bf215546Sopenharmony_ci         continue;
698bf215546Sopenharmony_ci
699bf215546Sopenharmony_ci      /* Get the timestamp. */
700bf215546Sopenharmony_ci      if (sscanf(line, "[%u.%u]", &sec, &usec) != 2) {
701bf215546Sopenharmony_ci         static bool hit = false;
702bf215546Sopenharmony_ci         if (!hit) {
703bf215546Sopenharmony_ci            fprintf(stderr, "%s: failed to parse line '%s'\n", __func__, line);
704bf215546Sopenharmony_ci            hit = true;
705bf215546Sopenharmony_ci         }
706bf215546Sopenharmony_ci         continue;
707bf215546Sopenharmony_ci      }
708bf215546Sopenharmony_ci      dmesg_timestamp = sec * 1000000ull + usec;
709bf215546Sopenharmony_ci
710bf215546Sopenharmony_ci      /* If just updating the timestamp. */
711bf215546Sopenharmony_ci      if (!out_addr)
712bf215546Sopenharmony_ci         continue;
713bf215546Sopenharmony_ci
714bf215546Sopenharmony_ci      /* Process messages only if the timestamp is newer. */
715bf215546Sopenharmony_ci      if (dmesg_timestamp <= *old_dmesg_timestamp)
716bf215546Sopenharmony_ci         continue;
717bf215546Sopenharmony_ci
718bf215546Sopenharmony_ci      /* Only process the first VM fault. */
719bf215546Sopenharmony_ci      if (fault)
720bf215546Sopenharmony_ci         continue;
721bf215546Sopenharmony_ci
722bf215546Sopenharmony_ci      /* Remove trailing \n */
723bf215546Sopenharmony_ci      len = strlen(line);
724bf215546Sopenharmony_ci      if (len && line[len - 1] == '\n')
725bf215546Sopenharmony_ci         line[len - 1] = 0;
726bf215546Sopenharmony_ci
727bf215546Sopenharmony_ci      /* Get the message part. */
728bf215546Sopenharmony_ci      msg = strchr(line, ']');
729bf215546Sopenharmony_ci      if (!msg)
730bf215546Sopenharmony_ci         continue;
731bf215546Sopenharmony_ci      msg++;
732bf215546Sopenharmony_ci
733bf215546Sopenharmony_ci      const char *header_line, *addr_line_prefix, *addr_line_format;
734bf215546Sopenharmony_ci
735bf215546Sopenharmony_ci      if (gfx_level >= GFX9) {
736bf215546Sopenharmony_ci         /* Match this:
737bf215546Sopenharmony_ci          * ..: [gfxhub] VMC page fault (src_id:0 ring:158 vm_id:2 pas_id:0)
738bf215546Sopenharmony_ci          * ..:   at page 0x0000000219f8f000 from 27
739bf215546Sopenharmony_ci          * ..: VM_L2_PROTECTION_FAULT_STATUS:0x0020113C
740bf215546Sopenharmony_ci          */
741bf215546Sopenharmony_ci         header_line = "VMC page fault";
742bf215546Sopenharmony_ci         addr_line_prefix = "   at page";
743bf215546Sopenharmony_ci         addr_line_format = "%" PRIx64;
744bf215546Sopenharmony_ci      } else {
745bf215546Sopenharmony_ci         header_line = "GPU fault detected:";
746bf215546Sopenharmony_ci         addr_line_prefix = "VM_CONTEXT1_PROTECTION_FAULT_ADDR";
747bf215546Sopenharmony_ci         addr_line_format = "%" PRIX64;
748bf215546Sopenharmony_ci      }
749bf215546Sopenharmony_ci
750bf215546Sopenharmony_ci      switch (progress) {
751bf215546Sopenharmony_ci      case 0:
752bf215546Sopenharmony_ci         if (strstr(msg, header_line))
753bf215546Sopenharmony_ci            progress = 1;
754bf215546Sopenharmony_ci         break;
755bf215546Sopenharmony_ci      case 1:
756bf215546Sopenharmony_ci         msg = strstr(msg, addr_line_prefix);
757bf215546Sopenharmony_ci         if (msg) {
758bf215546Sopenharmony_ci            msg = strstr(msg, "0x");
759bf215546Sopenharmony_ci            if (msg) {
760bf215546Sopenharmony_ci               msg += 2;
761bf215546Sopenharmony_ci               if (sscanf(msg, addr_line_format, out_addr) == 1)
762bf215546Sopenharmony_ci                  fault = true;
763bf215546Sopenharmony_ci            }
764bf215546Sopenharmony_ci         }
765bf215546Sopenharmony_ci         progress = 0;
766bf215546Sopenharmony_ci         break;
767bf215546Sopenharmony_ci      default:
768bf215546Sopenharmony_ci         progress = 0;
769bf215546Sopenharmony_ci      }
770bf215546Sopenharmony_ci   }
771bf215546Sopenharmony_ci   pclose(p);
772bf215546Sopenharmony_ci
773bf215546Sopenharmony_ci   if (dmesg_timestamp > *old_dmesg_timestamp)
774bf215546Sopenharmony_ci      *old_dmesg_timestamp = dmesg_timestamp;
775bf215546Sopenharmony_ci
776bf215546Sopenharmony_ci   return fault;
777bf215546Sopenharmony_ci#endif
778bf215546Sopenharmony_ci}
779bf215546Sopenharmony_ci
780bf215546Sopenharmony_cistatic int compare_wave(const void *p1, const void *p2)
781bf215546Sopenharmony_ci{
782bf215546Sopenharmony_ci   struct ac_wave_info *w1 = (struct ac_wave_info *)p1;
783bf215546Sopenharmony_ci   struct ac_wave_info *w2 = (struct ac_wave_info *)p2;
784bf215546Sopenharmony_ci
785bf215546Sopenharmony_ci   /* Sort waves according to PC and then SE, SH, CU, etc. */
786bf215546Sopenharmony_ci   if (w1->pc < w2->pc)
787bf215546Sopenharmony_ci      return -1;
788bf215546Sopenharmony_ci   if (w1->pc > w2->pc)
789bf215546Sopenharmony_ci      return 1;
790bf215546Sopenharmony_ci   if (w1->se < w2->se)
791bf215546Sopenharmony_ci      return -1;
792bf215546Sopenharmony_ci   if (w1->se > w2->se)
793bf215546Sopenharmony_ci      return 1;
794bf215546Sopenharmony_ci   if (w1->sh < w2->sh)
795bf215546Sopenharmony_ci      return -1;
796bf215546Sopenharmony_ci   if (w1->sh > w2->sh)
797bf215546Sopenharmony_ci      return 1;
798bf215546Sopenharmony_ci   if (w1->cu < w2->cu)
799bf215546Sopenharmony_ci      return -1;
800bf215546Sopenharmony_ci   if (w1->cu > w2->cu)
801bf215546Sopenharmony_ci      return 1;
802bf215546Sopenharmony_ci   if (w1->simd < w2->simd)
803bf215546Sopenharmony_ci      return -1;
804bf215546Sopenharmony_ci   if (w1->simd > w2->simd)
805bf215546Sopenharmony_ci      return 1;
806bf215546Sopenharmony_ci   if (w1->wave < w2->wave)
807bf215546Sopenharmony_ci      return -1;
808bf215546Sopenharmony_ci   if (w1->wave > w2->wave)
809bf215546Sopenharmony_ci      return 1;
810bf215546Sopenharmony_ci
811bf215546Sopenharmony_ci   return 0;
812bf215546Sopenharmony_ci}
813bf215546Sopenharmony_ci
814bf215546Sopenharmony_ci/* Return wave information. "waves" should be a large enough array. */
815bf215546Sopenharmony_ciunsigned ac_get_wave_info(enum amd_gfx_level gfx_level,
816bf215546Sopenharmony_ci                          struct ac_wave_info waves[AC_MAX_WAVES_PER_CHIP])
817bf215546Sopenharmony_ci{
818bf215546Sopenharmony_ci#ifdef _WIN32
819bf215546Sopenharmony_ci   return 0;
820bf215546Sopenharmony_ci#else
821bf215546Sopenharmony_ci   char line[2000], cmd[128];
822bf215546Sopenharmony_ci   unsigned num_waves = 0;
823bf215546Sopenharmony_ci
824bf215546Sopenharmony_ci   sprintf(cmd, "umr -O halt_waves -wa %s", gfx_level >= GFX10 ? "gfx_0.0.0" : "gfx");
825bf215546Sopenharmony_ci
826bf215546Sopenharmony_ci   FILE *p = popen(cmd, "r");
827bf215546Sopenharmony_ci   if (!p)
828bf215546Sopenharmony_ci      return 0;
829bf215546Sopenharmony_ci
830bf215546Sopenharmony_ci   if (!fgets(line, sizeof(line), p) || strncmp(line, "SE", 2) != 0) {
831bf215546Sopenharmony_ci      pclose(p);
832bf215546Sopenharmony_ci      return 0;
833bf215546Sopenharmony_ci   }
834bf215546Sopenharmony_ci
835bf215546Sopenharmony_ci   while (fgets(line, sizeof(line), p)) {
836bf215546Sopenharmony_ci      struct ac_wave_info *w;
837bf215546Sopenharmony_ci      uint32_t pc_hi, pc_lo, exec_hi, exec_lo;
838bf215546Sopenharmony_ci
839bf215546Sopenharmony_ci      assert(num_waves < AC_MAX_WAVES_PER_CHIP);
840bf215546Sopenharmony_ci      w = &waves[num_waves];
841bf215546Sopenharmony_ci
842bf215546Sopenharmony_ci      if (sscanf(line, "%u %u %u %u %u %x %x %x %x %x %x %x", &w->se, &w->sh, &w->cu, &w->simd,
843bf215546Sopenharmony_ci                 &w->wave, &w->status, &pc_hi, &pc_lo, &w->inst_dw0, &w->inst_dw1, &exec_hi,
844bf215546Sopenharmony_ci                 &exec_lo) == 12) {
845bf215546Sopenharmony_ci         w->pc = ((uint64_t)pc_hi << 32) | pc_lo;
846bf215546Sopenharmony_ci         w->exec = ((uint64_t)exec_hi << 32) | exec_lo;
847bf215546Sopenharmony_ci         w->matched = false;
848bf215546Sopenharmony_ci         num_waves++;
849bf215546Sopenharmony_ci      }
850bf215546Sopenharmony_ci   }
851bf215546Sopenharmony_ci
852bf215546Sopenharmony_ci   qsort(waves, num_waves, sizeof(struct ac_wave_info), compare_wave);
853bf215546Sopenharmony_ci
854bf215546Sopenharmony_ci   pclose(p);
855bf215546Sopenharmony_ci   return num_waves;
856bf215546Sopenharmony_ci#endif
857bf215546Sopenharmony_ci}
858