1 // SPDX-License-Identifier: GPL-2.0
3 * HiSilicon PCIe Trace and Tuning (PTT) support
4 * Copyright (c) 2022 HiSilicon Technologies Co., Ltd.
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>
24 #include "hisi-ptt-decoder/hisi-ptt-pkt-decoder.h"
28 #include <internal/lib.h>
31 struct auxtrace auxtrace;
33 struct perf_session *session;
34 struct machine *machine;
38 struct hisi_ptt_queue {
40 struct auxtrace_buffer *buffer;
43 static enum hisi_ptt_pkt_type hisi_ptt_check_packet_type(unsigned char *buf)
45 uint32_t head = *(uint32_t *)buf;
47 if ((HISI_PTT_8DW_CHECK_MASK & head) == HISI_PTT_IS_8DW_PKT)
48 return HISI_PTT_8DW_PKT;
50 return HISI_PTT_4DW_PKT;
53 static void hisi_ptt_dump(struct hisi_ptt *ptt __maybe_unused,
54 unsigned char *buf, size_t len)
56 const char *color = PERF_COLOR_BLUE;
57 enum hisi_ptt_pkt_type type;
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",
67 pkt_len = hisi_ptt_pkt_desc(buf, pos, type);
69 color_fprintf(stdout, color, " Bad packet!\n");
76 static void hisi_ptt_dump_event(struct hisi_ptt *ptt, unsigned char *buf,
81 hisi_ptt_dump(ptt, buf, len);
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)
92 static int hisi_ptt_process_auxtrace_event(struct perf_session *session,
93 union perf_event *event,
94 struct perf_tool *tool __maybe_unused)
96 struct hisi_ptt *ptt = container_of(session->auxtrace, struct hisi_ptt,
98 int fd = perf_data__fd(session->data);
99 int size = event->auxtrace.size;
100 void *data = malloc(size);
107 if (perf_data__is_pipe(session->data)) {
110 data_offset = lseek(fd, 0, SEEK_CUR);
111 if (data_offset == -1) {
117 err = readn(fd, data, size);
118 if (err != (ssize_t)size) {
124 hisi_ptt_dump_event(ptt, data, size);
130 static int hisi_ptt_flush(struct perf_session *session __maybe_unused,
131 struct perf_tool *tool __maybe_unused)
136 static void hisi_ptt_free_events(struct perf_session *session __maybe_unused)
140 static void hisi_ptt_free(struct perf_session *session)
142 struct hisi_ptt *ptt = container_of(session->auxtrace, struct hisi_ptt,
145 session->auxtrace = NULL;
149 static bool hisi_ptt_evsel_is_auxtrace(struct perf_session *session,
152 struct hisi_ptt *ptt = container_of(session->auxtrace, struct hisi_ptt, auxtrace);
154 return evsel->core.attr.type == ptt->pmu_type;
157 static void hisi_ptt_print_info(__u64 type)
162 fprintf(stdout, " PMU Type %" PRId64 "\n", (s64) type);
165 int hisi_ptt_process_auxtrace_info(union perf_event *event,
166 struct perf_session *session)
168 struct perf_record_auxtrace_info *auxtrace_info = &event->auxtrace_info;
169 struct hisi_ptt *ptt;
171 if (auxtrace_info->header.size < HISI_PTT_AUXTRACE_PRIV_SIZE +
172 sizeof(struct perf_record_auxtrace_info))
175 ptt = zalloc(sizeof(*ptt));
179 ptt->session = session;
180 ptt->machine = &session->machines.host; /* No kvm support */
181 ptt->auxtrace_type = auxtrace_info->type;
182 ptt->pmu_type = auxtrace_info->priv[0];
184 ptt->auxtrace.process_event = hisi_ptt_process_event;
185 ptt->auxtrace.process_auxtrace_event = hisi_ptt_process_auxtrace_event;
186 ptt->auxtrace.flush_events = hisi_ptt_flush;
187 ptt->auxtrace.free_events = hisi_ptt_free_events;
188 ptt->auxtrace.free = hisi_ptt_free;
189 ptt->auxtrace.evsel_is_auxtrace = hisi_ptt_evsel_is_auxtrace;
190 session->auxtrace = &ptt->auxtrace;
192 hisi_ptt_print_info(auxtrace_info->priv[0]);