xref: /openbmc/linux/tools/perf/util/hisi-ptt.c (revision 2d99a7ec)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * HiSilicon PCIe Trace and Tuning (PTT) support
4  * Copyright (c) 2022 HiSilicon Technologies Co., Ltd.
5  */
6 
7 #include <byteswap.h>
8 #include <endian.h>
9 #include <errno.h>
10 #include <inttypes.h>
11 #include <linux/bitops.h>
12 #include <linux/kernel.h>
13 #include <linux/log2.h>
14 #include <linux/types.h>
15 #include <linux/zalloc.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 
19 #include "auxtrace.h"
20 #include "color.h"
21 #include "debug.h"
22 #include "evsel.h"
23 #include "hisi-ptt.h"
24 #include "hisi-ptt-decoder/hisi-ptt-pkt-decoder.h"
25 #include "machine.h"
26 #include "session.h"
27 #include "tool.h"
28 #include <internal/lib.h>
29 
30 struct hisi_ptt {
31 	struct auxtrace auxtrace;
32 	u32 auxtrace_type;
33 	struct perf_session *session;
34 	struct machine *machine;
35 	u32 pmu_type;
36 };
37 
38 struct hisi_ptt_queue {
39 	struct hisi_ptt *ptt;
40 	struct auxtrace_buffer *buffer;
41 };
42 
43 static enum hisi_ptt_pkt_type hisi_ptt_check_packet_type(unsigned char *buf)
44 {
45 	uint32_t head = *(uint32_t *)buf;
46 
47 	if ((HISI_PTT_8DW_CHECK_MASK & head) == HISI_PTT_IS_8DW_PKT)
48 		return HISI_PTT_8DW_PKT;
49 
50 	return HISI_PTT_4DW_PKT;
51 }
52 
53 static void hisi_ptt_dump(struct hisi_ptt *ptt __maybe_unused,
54 			  unsigned char *buf, size_t len)
55 {
56 	const char *color = PERF_COLOR_BLUE;
57 	enum hisi_ptt_pkt_type type;
58 	size_t pos = 0;
59 	int pkt_len;
60 
61 	type = hisi_ptt_check_packet_type(buf);
62 	len = round_down(len, hisi_ptt_pkt_size[type]);
63 	color_fprintf(stdout, color, ". ... HISI PTT data: size %zu bytes\n",
64 		      len);
65 
66 	while (len > 0) {
67 		pkt_len = hisi_ptt_pkt_desc(buf, pos, type);
68 		if (!pkt_len)
69 			color_fprintf(stdout, color, " Bad packet!\n");
70 
71 		pos += pkt_len;
72 		len -= pkt_len;
73 	}
74 }
75 
76 static void hisi_ptt_dump_event(struct hisi_ptt *ptt, unsigned char *buf,
77 				size_t len)
78 {
79 	printf(".\n");
80 
81 	hisi_ptt_dump(ptt, buf, len);
82 }
83 
84 static int hisi_ptt_process_event(struct perf_session *session __maybe_unused,
85 				  union perf_event *event __maybe_unused,
86 				  struct perf_sample *sample __maybe_unused,
87 				  struct perf_tool *tool __maybe_unused)
88 {
89 	return 0;
90 }
91 
92 static int hisi_ptt_process_auxtrace_event(struct perf_session *session,
93 					   union perf_event *event,
94 					   struct perf_tool *tool __maybe_unused)
95 {
96 	struct hisi_ptt *ptt = container_of(session->auxtrace, struct hisi_ptt,
97 					    auxtrace);
98 	int fd = perf_data__fd(session->data);
99 	int size = event->auxtrace.size;
100 	void *data = malloc(size);
101 	off_t data_offset;
102 	int err;
103 
104 	if (!data)
105 		return -errno;
106 
107 	if (perf_data__is_pipe(session->data)) {
108 		data_offset = 0;
109 	} else {
110 		data_offset = lseek(fd, 0, SEEK_CUR);
111 		if (data_offset == -1)
112 			return -errno;
113 	}
114 
115 	err = readn(fd, data, size);
116 	if (err != (ssize_t)size) {
117 		free(data);
118 		return -errno;
119 	}
120 
121 	if (dump_trace)
122 		hisi_ptt_dump_event(ptt, data, size);
123 
124 	return 0;
125 }
126 
127 static int hisi_ptt_flush(struct perf_session *session __maybe_unused,
128 			  struct perf_tool *tool __maybe_unused)
129 {
130 	return 0;
131 }
132 
133 static void hisi_ptt_free_events(struct perf_session *session __maybe_unused)
134 {
135 }
136 
137 static void hisi_ptt_free(struct perf_session *session)
138 {
139 	struct hisi_ptt *ptt = container_of(session->auxtrace, struct hisi_ptt,
140 					    auxtrace);
141 
142 	session->auxtrace = NULL;
143 	free(ptt);
144 }
145 
146 static bool hisi_ptt_evsel_is_auxtrace(struct perf_session *session,
147 				       struct evsel *evsel)
148 {
149 	struct hisi_ptt *ptt = container_of(session->auxtrace, struct hisi_ptt, auxtrace);
150 
151 	return evsel->core.attr.type == ptt->pmu_type;
152 }
153 
154 static void hisi_ptt_print_info(__u64 type)
155 {
156 	if (!dump_trace)
157 		return;
158 
159 	fprintf(stdout, "  PMU Type           %" PRId64 "\n", (s64) type);
160 }
161 
162 int hisi_ptt_process_auxtrace_info(union perf_event *event,
163 				   struct perf_session *session)
164 {
165 	struct perf_record_auxtrace_info *auxtrace_info = &event->auxtrace_info;
166 	struct hisi_ptt *ptt;
167 
168 	if (auxtrace_info->header.size < HISI_PTT_AUXTRACE_PRIV_SIZE +
169 				sizeof(struct perf_record_auxtrace_info))
170 		return -EINVAL;
171 
172 	ptt = zalloc(sizeof(*ptt));
173 	if (!ptt)
174 		return -ENOMEM;
175 
176 	ptt->session = session;
177 	ptt->machine = &session->machines.host; /* No kvm support */
178 	ptt->auxtrace_type = auxtrace_info->type;
179 	ptt->pmu_type = auxtrace_info->priv[0];
180 
181 	ptt->auxtrace.process_event = hisi_ptt_process_event;
182 	ptt->auxtrace.process_auxtrace_event = hisi_ptt_process_auxtrace_event;
183 	ptt->auxtrace.flush_events = hisi_ptt_flush;
184 	ptt->auxtrace.free_events = hisi_ptt_free_events;
185 	ptt->auxtrace.free = hisi_ptt_free;
186 	ptt->auxtrace.evsel_is_auxtrace = hisi_ptt_evsel_is_auxtrace;
187 	session->auxtrace = &ptt->auxtrace;
188 
189 	hisi_ptt_print_info(auxtrace_info->priv[0]);
190 
191 	return 0;
192 }
193