xref: /linux/tools/perf/arch/arm64/util/hisi-ptt.c (revision 9e906a9dead17d81d6c2687f65e159231d0e3286)
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 <linux/kernel.h>
8 #include <linux/types.h>
9 #include <linux/bitops.h>
10 #include <linux/log2.h>
11 #include <linux/zalloc.h>
12 #include <errno.h>
13 #include <time.h>
14 
15 #include <internal/lib.h> // page_size
16 #include "../../../util/auxtrace.h"
17 #include "../../../util/cpumap.h"
18 #include "../../../util/debug.h"
19 #include "../../../util/event.h"
20 #include "../../../util/evlist.h"
21 #include "../../../util/evsel.h"
22 #include "../../../util/hisi-ptt.h"
23 #include "../../../util/pmu.h"
24 #include "../../../util/record.h"
25 #include "../../../util/session.h"
26 #include "../../../util/tsc.h"
27 
28 #define KiB(x) ((x) * 1024)
29 #define MiB(x) ((x) * 1024 * 1024)
30 
31 struct hisi_ptt_recording {
32 	struct auxtrace_record	itr;
33 	struct perf_pmu *hisi_ptt_pmu;
34 	struct evlist *evlist;
35 };
36 
37 static size_t
hisi_ptt_info_priv_size(struct auxtrace_record * itr __maybe_unused,struct evlist * evlist __maybe_unused)38 hisi_ptt_info_priv_size(struct auxtrace_record *itr __maybe_unused,
39 			struct evlist *evlist __maybe_unused)
40 {
41 	return HISI_PTT_AUXTRACE_PRIV_SIZE;
42 }
43 
hisi_ptt_info_fill(struct auxtrace_record * itr,struct perf_session * session,struct perf_record_auxtrace_info * auxtrace_info,size_t priv_size)44 static int hisi_ptt_info_fill(struct auxtrace_record *itr,
45 			      struct perf_session *session,
46 			      struct perf_record_auxtrace_info *auxtrace_info,
47 			      size_t priv_size)
48 {
49 	struct hisi_ptt_recording *pttr =
50 			container_of(itr, struct hisi_ptt_recording, itr);
51 	struct perf_pmu *hisi_ptt_pmu = pttr->hisi_ptt_pmu;
52 
53 	if (priv_size != HISI_PTT_AUXTRACE_PRIV_SIZE)
54 		return -EINVAL;
55 
56 	if (!session->evlist->core.nr_mmaps)
57 		return -EINVAL;
58 
59 	auxtrace_info->type = PERF_AUXTRACE_HISI_PTT;
60 	auxtrace_info->priv[0] = hisi_ptt_pmu->type;
61 
62 	return 0;
63 }
64 
hisi_ptt_set_auxtrace_mmap_page(struct record_opts * opts)65 static int hisi_ptt_set_auxtrace_mmap_page(struct record_opts *opts)
66 {
67 	bool privileged = perf_event_paranoid_check(-1);
68 
69 	if (!opts->full_auxtrace)
70 		return 0;
71 
72 	if (opts->full_auxtrace && !opts->auxtrace_mmap_pages) {
73 		if (privileged) {
74 			opts->auxtrace_mmap_pages = MiB(16) / page_size;
75 		} else {
76 			opts->auxtrace_mmap_pages = KiB(128) / page_size;
77 			if (opts->mmap_pages == UINT_MAX)
78 				opts->mmap_pages = KiB(256) / page_size;
79 		}
80 	}
81 
82 	/* Validate auxtrace_mmap_pages */
83 	if (opts->auxtrace_mmap_pages) {
84 		size_t sz = opts->auxtrace_mmap_pages * (size_t)page_size;
85 		size_t min_sz = KiB(8);
86 
87 		if (sz < min_sz || !is_power_of_2(sz)) {
88 			pr_err("Invalid mmap size for HISI PTT: must be at least %zuKiB and a power of 2\n",
89 			       min_sz / 1024);
90 			return -EINVAL;
91 		}
92 	}
93 
94 	return 0;
95 }
96 
hisi_ptt_recording_options(struct auxtrace_record * itr,struct evlist * evlist,struct record_opts * opts)97 static int hisi_ptt_recording_options(struct auxtrace_record *itr,
98 				      struct evlist *evlist,
99 				      struct record_opts *opts)
100 {
101 	struct hisi_ptt_recording *pttr =
102 			container_of(itr, struct hisi_ptt_recording, itr);
103 	struct perf_pmu *hisi_ptt_pmu = pttr->hisi_ptt_pmu;
104 	struct evsel *evsel, *hisi_ptt_evsel = NULL;
105 	struct evsel *tracking_evsel;
106 	int err;
107 
108 	pttr->evlist = evlist;
109 	evlist__for_each_entry(evlist, evsel) {
110 		if (evsel->core.attr.type == hisi_ptt_pmu->type) {
111 			if (hisi_ptt_evsel) {
112 				pr_err("There may be only one " HISI_PTT_PMU_NAME "x event\n");
113 				return -EINVAL;
114 			}
115 			evsel->core.attr.freq = 0;
116 			evsel->core.attr.sample_period = 1;
117 			evsel->needs_auxtrace_mmap = true;
118 			hisi_ptt_evsel = evsel;
119 			opts->full_auxtrace = true;
120 		}
121 	}
122 
123 	err = hisi_ptt_set_auxtrace_mmap_page(opts);
124 	if (err)
125 		return err;
126 	/*
127 	 * To obtain the auxtrace buffer file descriptor, the auxtrace event
128 	 * must come first.
129 	 */
130 	evlist__to_front(evlist, hisi_ptt_evsel);
131 	evsel__set_sample_bit(hisi_ptt_evsel, TIME);
132 
133 	/* Add dummy event to keep tracking */
134 	err = parse_event(evlist, "dummy:u");
135 	if (err)
136 		return err;
137 
138 	tracking_evsel = evlist__last(evlist);
139 	evlist__set_tracking_event(evlist, tracking_evsel);
140 
141 	tracking_evsel->core.attr.freq = 0;
142 	tracking_evsel->core.attr.sample_period = 1;
143 	evsel__set_sample_bit(tracking_evsel, TIME);
144 
145 	return 0;
146 }
147 
hisi_ptt_reference(struct auxtrace_record * itr __maybe_unused)148 static u64 hisi_ptt_reference(struct auxtrace_record *itr __maybe_unused)
149 {
150 	return rdtsc();
151 }
152 
hisi_ptt_recording_free(struct auxtrace_record * itr)153 static void hisi_ptt_recording_free(struct auxtrace_record *itr)
154 {
155 	struct hisi_ptt_recording *pttr =
156 			container_of(itr, struct hisi_ptt_recording, itr);
157 
158 	free(pttr);
159 }
160 
hisi_ptt_recording_init(int * err,struct perf_pmu * hisi_ptt_pmu)161 struct auxtrace_record *hisi_ptt_recording_init(int *err,
162 						struct perf_pmu *hisi_ptt_pmu)
163 {
164 	struct hisi_ptt_recording *pttr;
165 
166 	if (!hisi_ptt_pmu) {
167 		*err = -ENODEV;
168 		return NULL;
169 	}
170 
171 	pttr = zalloc(sizeof(*pttr));
172 	if (!pttr) {
173 		*err = -ENOMEM;
174 		return NULL;
175 	}
176 
177 	pttr->hisi_ptt_pmu = hisi_ptt_pmu;
178 	pttr->itr.recording_options = hisi_ptt_recording_options;
179 	pttr->itr.info_priv_size = hisi_ptt_info_priv_size;
180 	pttr->itr.info_fill = hisi_ptt_info_fill;
181 	pttr->itr.free = hisi_ptt_recording_free;
182 	pttr->itr.reference = hisi_ptt_reference;
183 	pttr->itr.read_finish = auxtrace_record__read_finish;
184 	pttr->itr.alignment = 0;
185 
186 	*err = 0;
187 	return &pttr->itr;
188 }
189