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