xref: /openbmc/linux/tools/perf/arch/arm64/util/arm-spe.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1ffd3d18cSKim Phillips // SPDX-License-Identifier: GPL-2.0
2ffd3d18cSKim Phillips /*
3ffd3d18cSKim Phillips  * Arm Statistical Profiling Extensions (SPE) support
4ffd3d18cSKim Phillips  * Copyright (c) 2017-2018, Arm Ltd.
5ffd3d18cSKim Phillips  */
6ffd3d18cSKim Phillips 
7ffd3d18cSKim Phillips #include <linux/kernel.h>
8ffd3d18cSKim Phillips #include <linux/types.h>
9ffd3d18cSKim Phillips #include <linux/bitops.h>
10ffd3d18cSKim Phillips #include <linux/log2.h>
117f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h>
12ffd3d18cSKim Phillips #include <time.h>
13ffd3d18cSKim Phillips 
14441b62acSIan Rogers #include "../../../util/cpumap.h"
15441b62acSIan Rogers #include "../../../util/event.h"
16441b62acSIan Rogers #include "../../../util/evsel.h"
17f99237e4SLeo Yan #include "../../../util/evsel_config.h"
18441b62acSIan Rogers #include "../../../util/evlist.h"
19441b62acSIan Rogers #include "../../../util/session.h"
2020f2be1dSJiri Olsa #include <internal/lib.h> // page_size
21441b62acSIan Rogers #include "../../../util/pmu.h"
22441b62acSIan Rogers #include "../../../util/debug.h"
23441b62acSIan Rogers #include "../../../util/auxtrace.h"
24441b62acSIan Rogers #include "../../../util/record.h"
25441b62acSIan Rogers #include "../../../util/arm-spe.h"
2656c31cdfSGerman Gomez #include <tools/libc_compat.h> // reallocarray
27ffd3d18cSKim Phillips 
28ffd3d18cSKim Phillips #define KiB(x) ((x) * 1024)
29ffd3d18cSKim Phillips #define MiB(x) ((x) * 1024 * 1024)
30ffd3d18cSKim Phillips 
31ffd3d18cSKim Phillips struct arm_spe_recording {
32ffd3d18cSKim Phillips 	struct auxtrace_record		itr;
33ffd3d18cSKim Phillips 	struct perf_pmu			*arm_spe_pmu;
3463503dbaSJiri Olsa 	struct evlist		*evlist;
3556c31cdfSGerman Gomez 	int			wrapped_cnt;
3656c31cdfSGerman Gomez 	bool			*wrapped;
37ffd3d18cSKim Phillips };
38ffd3d18cSKim Phillips 
39ffd3d18cSKim Phillips static size_t
arm_spe_info_priv_size(struct auxtrace_record * itr __maybe_unused,struct evlist * evlist __maybe_unused)40ffd3d18cSKim Phillips arm_spe_info_priv_size(struct auxtrace_record *itr __maybe_unused,
4163503dbaSJiri Olsa 		       struct evlist *evlist __maybe_unused)
42ffd3d18cSKim Phillips {
43ffd3d18cSKim Phillips 	return ARM_SPE_AUXTRACE_PRIV_SIZE;
44ffd3d18cSKim Phillips }
45ffd3d18cSKim Phillips 
arm_spe_info_fill(struct auxtrace_record * itr,struct perf_session * session,struct perf_record_auxtrace_info * auxtrace_info,size_t priv_size)46ffd3d18cSKim Phillips static int arm_spe_info_fill(struct auxtrace_record *itr,
47ffd3d18cSKim Phillips 			     struct perf_session *session,
4872932371SJiri Olsa 			     struct perf_record_auxtrace_info *auxtrace_info,
49ffd3d18cSKim Phillips 			     size_t priv_size)
50ffd3d18cSKim Phillips {
51ffd3d18cSKim Phillips 	struct arm_spe_recording *sper =
52ffd3d18cSKim Phillips 			container_of(itr, struct arm_spe_recording, itr);
53ffd3d18cSKim Phillips 	struct perf_pmu *arm_spe_pmu = sper->arm_spe_pmu;
54ffd3d18cSKim Phillips 
55ffd3d18cSKim Phillips 	if (priv_size != ARM_SPE_AUXTRACE_PRIV_SIZE)
56ffd3d18cSKim Phillips 		return -EINVAL;
57ffd3d18cSKim Phillips 
58c976ee11SJiri Olsa 	if (!session->evlist->core.nr_mmaps)
59ffd3d18cSKim Phillips 		return -EINVAL;
60ffd3d18cSKim Phillips 
61ffd3d18cSKim Phillips 	auxtrace_info->type = PERF_AUXTRACE_ARM_SPE;
62ffd3d18cSKim Phillips 	auxtrace_info->priv[ARM_SPE_PMU_TYPE] = arm_spe_pmu->type;
63ffd3d18cSKim Phillips 
64ffd3d18cSKim Phillips 	return 0;
65ffd3d18cSKim Phillips }
66ffd3d18cSKim Phillips 
670901b560SGerman Gomez static void
arm_spe_snapshot_resolve_auxtrace_defaults(struct record_opts * opts,bool privileged)680901b560SGerman Gomez arm_spe_snapshot_resolve_auxtrace_defaults(struct record_opts *opts,
690901b560SGerman Gomez 					   bool privileged)
700901b560SGerman Gomez {
710901b560SGerman Gomez 	/*
720901b560SGerman Gomez 	 * The default snapshot size is the auxtrace mmap size. If neither auxtrace mmap size nor
730901b560SGerman Gomez 	 * snapshot size is specified, then the default is 4MiB for privileged users, 128KiB for
740901b560SGerman Gomez 	 * unprivileged users.
750901b560SGerman Gomez 	 *
760901b560SGerman Gomez 	 * The default auxtrace mmap size is 4MiB/page_size for privileged users, 128KiB for
770901b560SGerman Gomez 	 * unprivileged users. If an unprivileged user does not specify mmap pages, the mmap pages
780901b560SGerman Gomez 	 * will be reduced from the default 512KiB/page_size to 256KiB/page_size, otherwise the
790901b560SGerman Gomez 	 * user is likely to get an error as they exceed their mlock limmit.
800901b560SGerman Gomez 	 */
810901b560SGerman Gomez 
820901b560SGerman Gomez 	/*
830901b560SGerman Gomez 	 * No size were given to '-S' or '-m,', so go with the default
840901b560SGerman Gomez 	 */
850901b560SGerman Gomez 	if (!opts->auxtrace_snapshot_size && !opts->auxtrace_mmap_pages) {
860901b560SGerman Gomez 		if (privileged) {
870901b560SGerman Gomez 			opts->auxtrace_mmap_pages = MiB(4) / page_size;
880901b560SGerman Gomez 		} else {
890901b560SGerman Gomez 			opts->auxtrace_mmap_pages = KiB(128) / page_size;
900901b560SGerman Gomez 			if (opts->mmap_pages == UINT_MAX)
910901b560SGerman Gomez 				opts->mmap_pages = KiB(256) / page_size;
920901b560SGerman Gomez 		}
930901b560SGerman Gomez 	} else if (!opts->auxtrace_mmap_pages && !privileged && opts->mmap_pages == UINT_MAX) {
940901b560SGerman Gomez 		opts->mmap_pages = KiB(256) / page_size;
950901b560SGerman Gomez 	}
960901b560SGerman Gomez 
970901b560SGerman Gomez 	/*
980901b560SGerman Gomez 	 * '-m,xyz' was specified but no snapshot size, so make the snapshot size as big as the
990901b560SGerman Gomez 	 * auxtrace mmap area.
1000901b560SGerman Gomez 	 */
1010901b560SGerman Gomez 	if (!opts->auxtrace_snapshot_size)
1020901b560SGerman Gomez 		opts->auxtrace_snapshot_size = opts->auxtrace_mmap_pages * (size_t)page_size;
1030901b560SGerman Gomez 
1040901b560SGerman Gomez 	/*
1050901b560SGerman Gomez 	 * '-Sxyz' was specified but no auxtrace mmap area, so make the auxtrace mmap area big
1060901b560SGerman Gomez 	 * enough to fit the requested snapshot size.
1070901b560SGerman Gomez 	 */
1080901b560SGerman Gomez 	if (!opts->auxtrace_mmap_pages) {
1090901b560SGerman Gomez 		size_t sz = opts->auxtrace_snapshot_size;
1100901b560SGerman Gomez 
1110901b560SGerman Gomez 		sz = round_up(sz, page_size) / page_size;
1120901b560SGerman Gomez 		opts->auxtrace_mmap_pages = roundup_pow_of_two(sz);
1130901b560SGerman Gomez 	}
1140901b560SGerman Gomez }
1150901b560SGerman Gomez 
arm_spe_recording_options(struct auxtrace_record * itr,struct evlist * evlist,struct record_opts * opts)116ffd3d18cSKim Phillips static int arm_spe_recording_options(struct auxtrace_record *itr,
11763503dbaSJiri Olsa 				     struct evlist *evlist,
118ffd3d18cSKim Phillips 				     struct record_opts *opts)
119ffd3d18cSKim Phillips {
120ffd3d18cSKim Phillips 	struct arm_spe_recording *sper =
121ffd3d18cSKim Phillips 			container_of(itr, struct arm_spe_recording, itr);
122ffd3d18cSKim Phillips 	struct perf_pmu *arm_spe_pmu = sper->arm_spe_pmu;
12332dcd021SJiri Olsa 	struct evsel *evsel, *arm_spe_evsel = NULL;
1240df6ade7SIan Rogers 	struct perf_cpu_map *cpus = evlist->core.user_requested_cpus;
125dda1bf8eSIgor Lubashev 	bool privileged = perf_event_paranoid_check(-1);
12632dcd021SJiri Olsa 	struct evsel *tracking_evsel;
127ffd3d18cSKim Phillips 	int err;
1287599b70aSTimothy Hayes 	u64 bit;
129ffd3d18cSKim Phillips 
130ffd3d18cSKim Phillips 	sper->evlist = evlist;
131ffd3d18cSKim Phillips 
132ffd3d18cSKim Phillips 	evlist__for_each_entry(evlist, evsel) {
1331fc632ceSJiri Olsa 		if (evsel->core.attr.type == arm_spe_pmu->type) {
134ffd3d18cSKim Phillips 			if (arm_spe_evsel) {
135ffd3d18cSKim Phillips 				pr_err("There may be only one " ARM_SPE_PMU_NAME "x event\n");
136ffd3d18cSKim Phillips 				return -EINVAL;
137ffd3d18cSKim Phillips 			}
1381fc632ceSJiri Olsa 			evsel->core.attr.freq = 0;
139521f2688SGerman Gomez 			evsel->core.attr.sample_period = arm_spe_pmu->default_config->sample_period;
1407df319e5SAdrian Hunter 			evsel->needs_auxtrace_mmap = true;
141ffd3d18cSKim Phillips 			arm_spe_evsel = evsel;
142ffd3d18cSKim Phillips 			opts->full_auxtrace = true;
143ffd3d18cSKim Phillips 		}
144ffd3d18cSKim Phillips 	}
145ffd3d18cSKim Phillips 
146ffd3d18cSKim Phillips 	if (!opts->full_auxtrace)
147ffd3d18cSKim Phillips 		return 0;
148ffd3d18cSKim Phillips 
1490901b560SGerman Gomez 	/*
1500901b560SGerman Gomez 	 * we are in snapshot mode.
1510901b560SGerman Gomez 	 */
1520901b560SGerman Gomez 	if (opts->auxtrace_snapshot_mode) {
1530901b560SGerman Gomez 		/*
1540901b560SGerman Gomez 		 * Command arguments '-Sxyz' and/or '-m,xyz' are missing, so fill those in with
1550901b560SGerman Gomez 		 * default values.
1560901b560SGerman Gomez 		 */
1570901b560SGerman Gomez 		if (!opts->auxtrace_snapshot_size || !opts->auxtrace_mmap_pages)
1580901b560SGerman Gomez 			arm_spe_snapshot_resolve_auxtrace_defaults(opts, privileged);
1590901b560SGerman Gomez 
1600901b560SGerman Gomez 		/*
1610901b560SGerman Gomez 		 * Snapshot size can't be bigger than the auxtrace area.
1620901b560SGerman Gomez 		 */
1630901b560SGerman Gomez 		if (opts->auxtrace_snapshot_size > opts->auxtrace_mmap_pages * (size_t)page_size) {
1640901b560SGerman Gomez 			pr_err("Snapshot size %zu must not be greater than AUX area tracing mmap size %zu\n",
1650901b560SGerman Gomez 			       opts->auxtrace_snapshot_size,
1660901b560SGerman Gomez 			       opts->auxtrace_mmap_pages * (size_t)page_size);
1670901b560SGerman Gomez 			return -EINVAL;
1680901b560SGerman Gomez 		}
1690901b560SGerman Gomez 
1700901b560SGerman Gomez 		/*
1710901b560SGerman Gomez 		 * Something went wrong somewhere - this shouldn't happen.
1720901b560SGerman Gomez 		 */
1730901b560SGerman Gomez 		if (!opts->auxtrace_snapshot_size || !opts->auxtrace_mmap_pages) {
1740901b560SGerman Gomez 			pr_err("Failed to calculate default snapshot size and/or AUX area tracing mmap pages\n");
1750901b560SGerman Gomez 			return -EINVAL;
1760901b560SGerman Gomez 		}
1770901b560SGerman Gomez 	}
1780901b560SGerman Gomez 
179ffd3d18cSKim Phillips 	/* We are in full trace mode but '-m,xyz' wasn't specified */
180afe360a8SLeo Yan 	if (!opts->auxtrace_mmap_pages) {
181ffd3d18cSKim Phillips 		if (privileged) {
182ffd3d18cSKim Phillips 			opts->auxtrace_mmap_pages = MiB(4) / page_size;
183ffd3d18cSKim Phillips 		} else {
184ffd3d18cSKim Phillips 			opts->auxtrace_mmap_pages = KiB(128) / page_size;
185ffd3d18cSKim Phillips 			if (opts->mmap_pages == UINT_MAX)
186ffd3d18cSKim Phillips 				opts->mmap_pages = KiB(256) / page_size;
187ffd3d18cSKim Phillips 		}
188ffd3d18cSKim Phillips 	}
189ffd3d18cSKim Phillips 
190ffd3d18cSKim Phillips 	/* Validate auxtrace_mmap_pages */
191ffd3d18cSKim Phillips 	if (opts->auxtrace_mmap_pages) {
192ffd3d18cSKim Phillips 		size_t sz = opts->auxtrace_mmap_pages * (size_t)page_size;
193ffd3d18cSKim Phillips 		size_t min_sz = KiB(8);
194ffd3d18cSKim Phillips 
195ffd3d18cSKim Phillips 		if (sz < min_sz || !is_power_of_2(sz)) {
196ffd3d18cSKim Phillips 			pr_err("Invalid mmap size for ARM SPE: must be at least %zuKiB and a power of 2\n",
197ffd3d18cSKim Phillips 			       min_sz / 1024);
198ffd3d18cSKim Phillips 			return -EINVAL;
199ffd3d18cSKim Phillips 		}
200ffd3d18cSKim Phillips 	}
201ffd3d18cSKim Phillips 
2020901b560SGerman Gomez 	if (opts->auxtrace_snapshot_mode)
2030901b560SGerman Gomez 		pr_debug2("%sx snapshot size: %zu\n", ARM_SPE_PMU_NAME,
2040901b560SGerman Gomez 			  opts->auxtrace_snapshot_size);
205ffd3d18cSKim Phillips 
206ffd3d18cSKim Phillips 	/*
207ffd3d18cSKim Phillips 	 * To obtain the auxtrace buffer file descriptor, the auxtrace event
208ffd3d18cSKim Phillips 	 * must come first.
209ffd3d18cSKim Phillips 	 */
210e414fd1aSArnaldo Carvalho de Melo 	evlist__to_front(evlist, arm_spe_evsel);
211ffd3d18cSKim Phillips 
212f99237e4SLeo Yan 	/*
213f99237e4SLeo Yan 	 * In the case of per-cpu mmaps, sample CPU for AUX event;
214f99237e4SLeo Yan 	 * also enable the timestamp tracing for samples correlation.
215f99237e4SLeo Yan 	 */
216f99237e4SLeo Yan 	if (!perf_cpu_map__empty(cpus)) {
217862b2f8fSArnaldo Carvalho de Melo 		evsel__set_sample_bit(arm_spe_evsel, CPU);
2186593f019SJames Clark 		evsel__set_config_if_unset(arm_spe_pmu, arm_spe_evsel,
2196593f019SJames Clark 					   "ts_enable", 1);
220f99237e4SLeo Yan 	}
221ffd3d18cSKim Phillips 
222ffab4870SJames Clark 	/*
223ffab4870SJames Clark 	 * Set this only so that perf report knows that SPE generates memory info. It has no effect
224ffab4870SJames Clark 	 * on the opening of the event or the SPE data produced.
225ffab4870SJames Clark 	 */
226ffab4870SJames Clark 	evsel__set_sample_bit(arm_spe_evsel, DATA_SRC);
227ffab4870SJames Clark 
2287599b70aSTimothy Hayes 	/*
2297599b70aSTimothy Hayes 	 * The PHYS_ADDR flag does not affect the driver behaviour, it is used to
2307599b70aSTimothy Hayes 	 * inform that the resulting output's SPE samples contain physical addresses
2317599b70aSTimothy Hayes 	 * where applicable.
2327599b70aSTimothy Hayes 	 */
233*da6a5afdSIan Rogers 	bit = perf_pmu__format_bits(arm_spe_pmu, "pa_enable");
2347599b70aSTimothy Hayes 	if (arm_spe_evsel->core.attr.config & bit)
2357599b70aSTimothy Hayes 		evsel__set_sample_bit(arm_spe_evsel, PHYS_ADDR);
2367599b70aSTimothy Hayes 
237ffd3d18cSKim Phillips 	/* Add dummy event to keep tracking */
238806731a9SAdrian Hunter 	err = parse_event(evlist, "dummy:u");
239ffd3d18cSKim Phillips 	if (err)
240ffd3d18cSKim Phillips 		return err;
241ffd3d18cSKim Phillips 
242515dbe48SJiri Olsa 	tracking_evsel = evlist__last(evlist);
243e80db255SArnaldo Carvalho de Melo 	evlist__set_tracking_event(evlist, tracking_evsel);
244ffd3d18cSKim Phillips 
2451fc632ceSJiri Olsa 	tracking_evsel->core.attr.freq = 0;
2461fc632ceSJiri Olsa 	tracking_evsel->core.attr.sample_period = 1;
247e582badfSLeo Yan 
248e582badfSLeo Yan 	/* In per-cpu case, always need the time of mmap events etc */
2499dc9855fSNamhyung Kim 	if (!perf_cpu_map__empty(cpus)) {
250862b2f8fSArnaldo Carvalho de Melo 		evsel__set_sample_bit(tracking_evsel, TIME);
2519dc9855fSNamhyung Kim 		evsel__set_sample_bit(tracking_evsel, CPU);
252455c9882SGerman Gomez 
2539dc9855fSNamhyung Kim 		/* also track task context switch */
254455c9882SGerman Gomez 		if (!record_opts__no_switch_events(opts))
2559dc9855fSNamhyung Kim 			tracking_evsel->core.attr.context_switch = 1;
2569dc9855fSNamhyung Kim 	}
257ffd3d18cSKim Phillips 
258ffd3d18cSKim Phillips 	return 0;
259ffd3d18cSKim Phillips }
260ffd3d18cSKim Phillips 
arm_spe_parse_snapshot_options(struct auxtrace_record * itr __maybe_unused,struct record_opts * opts,const char * str)2610901b560SGerman Gomez static int arm_spe_parse_snapshot_options(struct auxtrace_record *itr __maybe_unused,
2620901b560SGerman Gomez 					 struct record_opts *opts,
2630901b560SGerman Gomez 					 const char *str)
2640901b560SGerman Gomez {
2650901b560SGerman Gomez 	unsigned long long snapshot_size = 0;
2660901b560SGerman Gomez 	char *endptr;
2670901b560SGerman Gomez 
2680901b560SGerman Gomez 	if (str) {
2690901b560SGerman Gomez 		snapshot_size = strtoull(str, &endptr, 0);
2700901b560SGerman Gomez 		if (*endptr || snapshot_size > SIZE_MAX)
2710901b560SGerman Gomez 			return -1;
2720901b560SGerman Gomez 	}
2730901b560SGerman Gomez 
2740901b560SGerman Gomez 	opts->auxtrace_snapshot_mode = true;
2750901b560SGerman Gomez 	opts->auxtrace_snapshot_size = snapshot_size;
2760901b560SGerman Gomez 
2770901b560SGerman Gomez 	return 0;
2780901b560SGerman Gomez }
2790901b560SGerman Gomez 
arm_spe_snapshot_start(struct auxtrace_record * itr)2800901b560SGerman Gomez static int arm_spe_snapshot_start(struct auxtrace_record *itr)
2810901b560SGerman Gomez {
2820901b560SGerman Gomez 	struct arm_spe_recording *ptr =
2830901b560SGerman Gomez 			container_of(itr, struct arm_spe_recording, itr);
2840901b560SGerman Gomez 	struct evsel *evsel;
2850901b560SGerman Gomez 
2860901b560SGerman Gomez 	evlist__for_each_entry(ptr->evlist, evsel) {
2870901b560SGerman Gomez 		if (evsel->core.attr.type == ptr->arm_spe_pmu->type)
2880901b560SGerman Gomez 			return evsel__disable(evsel);
2890901b560SGerman Gomez 	}
2900901b560SGerman Gomez 	return -EINVAL;
2910901b560SGerman Gomez }
2920901b560SGerman Gomez 
arm_spe_snapshot_finish(struct auxtrace_record * itr)2930901b560SGerman Gomez static int arm_spe_snapshot_finish(struct auxtrace_record *itr)
2940901b560SGerman Gomez {
2950901b560SGerman Gomez 	struct arm_spe_recording *ptr =
2960901b560SGerman Gomez 			container_of(itr, struct arm_spe_recording, itr);
2970901b560SGerman Gomez 	struct evsel *evsel;
2980901b560SGerman Gomez 
2990901b560SGerman Gomez 	evlist__for_each_entry(ptr->evlist, evsel) {
3000901b560SGerman Gomez 		if (evsel->core.attr.type == ptr->arm_spe_pmu->type)
3010901b560SGerman Gomez 			return evsel__enable(evsel);
3020901b560SGerman Gomez 	}
3030901b560SGerman Gomez 	return -EINVAL;
3040901b560SGerman Gomez }
3050901b560SGerman Gomez 
arm_spe_alloc_wrapped_array(struct arm_spe_recording * ptr,int idx)30656c31cdfSGerman Gomez static int arm_spe_alloc_wrapped_array(struct arm_spe_recording *ptr, int idx)
30756c31cdfSGerman Gomez {
30856c31cdfSGerman Gomez 	bool *wrapped;
30956c31cdfSGerman Gomez 	int cnt = ptr->wrapped_cnt, new_cnt, i;
31056c31cdfSGerman Gomez 
31156c31cdfSGerman Gomez 	/*
31256c31cdfSGerman Gomez 	 * No need to allocate, so return early.
31356c31cdfSGerman Gomez 	 */
31456c31cdfSGerman Gomez 	if (idx < cnt)
31556c31cdfSGerman Gomez 		return 0;
31656c31cdfSGerman Gomez 
31756c31cdfSGerman Gomez 	/*
31856c31cdfSGerman Gomez 	 * Make ptr->wrapped as big as idx.
31956c31cdfSGerman Gomez 	 */
32056c31cdfSGerman Gomez 	new_cnt = idx + 1;
32156c31cdfSGerman Gomez 
32256c31cdfSGerman Gomez 	/*
32356c31cdfSGerman Gomez 	 * Free'ed in arm_spe_recording_free().
32456c31cdfSGerman Gomez 	 */
32556c31cdfSGerman Gomez 	wrapped = reallocarray(ptr->wrapped, new_cnt, sizeof(bool));
32656c31cdfSGerman Gomez 	if (!wrapped)
32756c31cdfSGerman Gomez 		return -ENOMEM;
32856c31cdfSGerman Gomez 
32956c31cdfSGerman Gomez 	/*
33056c31cdfSGerman Gomez 	 * init new allocated values.
33156c31cdfSGerman Gomez 	 */
33256c31cdfSGerman Gomez 	for (i = cnt; i < new_cnt; i++)
33356c31cdfSGerman Gomez 		wrapped[i] = false;
33456c31cdfSGerman Gomez 
33556c31cdfSGerman Gomez 	ptr->wrapped_cnt = new_cnt;
33656c31cdfSGerman Gomez 	ptr->wrapped = wrapped;
33756c31cdfSGerman Gomez 
33856c31cdfSGerman Gomez 	return 0;
33956c31cdfSGerman Gomez }
34056c31cdfSGerman Gomez 
arm_spe_buffer_has_wrapped(unsigned char * buffer,size_t buffer_size,u64 head)34156c31cdfSGerman Gomez static bool arm_spe_buffer_has_wrapped(unsigned char *buffer,
34256c31cdfSGerman Gomez 				      size_t buffer_size, u64 head)
34356c31cdfSGerman Gomez {
34456c31cdfSGerman Gomez 	u64 i, watermark;
34556c31cdfSGerman Gomez 	u64 *buf = (u64 *)buffer;
34656c31cdfSGerman Gomez 	size_t buf_size = buffer_size;
34756c31cdfSGerman Gomez 
34856c31cdfSGerman Gomez 	/*
34956c31cdfSGerman Gomez 	 * Defensively handle the case where head might be continually increasing - if its value is
35056c31cdfSGerman Gomez 	 * equal or greater than the size of the ring buffer, then we can safely determine it has
35156c31cdfSGerman Gomez 	 * wrapped around. Otherwise, continue to detect if head might have wrapped.
35256c31cdfSGerman Gomez 	 */
35356c31cdfSGerman Gomez 	if (head >= buffer_size)
35456c31cdfSGerman Gomez 		return true;
35556c31cdfSGerman Gomez 
35656c31cdfSGerman Gomez 	/*
35756c31cdfSGerman Gomez 	 * We want to look the very last 512 byte (chosen arbitrarily) in the ring buffer.
35856c31cdfSGerman Gomez 	 */
35956c31cdfSGerman Gomez 	watermark = buf_size - 512;
36056c31cdfSGerman Gomez 
36156c31cdfSGerman Gomez 	/*
36256c31cdfSGerman Gomez 	 * The value of head is somewhere within the size of the ring buffer. This can be that there
36356c31cdfSGerman Gomez 	 * hasn't been enough data to fill the ring buffer yet or the trace time was so long that
36456c31cdfSGerman Gomez 	 * head has numerically wrapped around.  To find we need to check if we have data at the
36556c31cdfSGerman Gomez 	 * very end of the ring buffer.  We can reliably do this because mmap'ed pages are zeroed
36656c31cdfSGerman Gomez 	 * out and there is a fresh mapping with every new session.
36756c31cdfSGerman Gomez 	 */
36856c31cdfSGerman Gomez 
36956c31cdfSGerman Gomez 	/*
37056c31cdfSGerman Gomez 	 * head is less than 512 byte from the end of the ring buffer.
37156c31cdfSGerman Gomez 	 */
37256c31cdfSGerman Gomez 	if (head > watermark)
37356c31cdfSGerman Gomez 		watermark = head;
37456c31cdfSGerman Gomez 
37556c31cdfSGerman Gomez 	/*
37656c31cdfSGerman Gomez 	 * Speed things up by using 64 bit transactions (see "u64 *buf" above)
37756c31cdfSGerman Gomez 	 */
37856c31cdfSGerman Gomez 	watermark /= sizeof(u64);
37956c31cdfSGerman Gomez 	buf_size /= sizeof(u64);
38056c31cdfSGerman Gomez 
38156c31cdfSGerman Gomez 	/*
38256c31cdfSGerman Gomez 	 * If we find trace data at the end of the ring buffer, head has been there and has
38356c31cdfSGerman Gomez 	 * numerically wrapped around at least once.
38456c31cdfSGerman Gomez 	 */
38556c31cdfSGerman Gomez 	for (i = watermark; i < buf_size; i++)
38656c31cdfSGerman Gomez 		if (buf[i])
38756c31cdfSGerman Gomez 			return true;
38856c31cdfSGerman Gomez 
38956c31cdfSGerman Gomez 	return false;
39056c31cdfSGerman Gomez }
39156c31cdfSGerman Gomez 
arm_spe_find_snapshot(struct auxtrace_record * itr,int idx,struct auxtrace_mmap * mm,unsigned char * data,u64 * head,u64 * old)39256c31cdfSGerman Gomez static int arm_spe_find_snapshot(struct auxtrace_record *itr, int idx,
39356c31cdfSGerman Gomez 				  struct auxtrace_mmap *mm, unsigned char *data,
39456c31cdfSGerman Gomez 				  u64 *head, u64 *old)
39556c31cdfSGerman Gomez {
39656c31cdfSGerman Gomez 	int err;
39756c31cdfSGerman Gomez 	bool wrapped;
39856c31cdfSGerman Gomez 	struct arm_spe_recording *ptr =
39956c31cdfSGerman Gomez 			container_of(itr, struct arm_spe_recording, itr);
40056c31cdfSGerman Gomez 
40156c31cdfSGerman Gomez 	/*
40256c31cdfSGerman Gomez 	 * Allocate memory to keep track of wrapping if this is the first
40356c31cdfSGerman Gomez 	 * time we deal with this *mm.
40456c31cdfSGerman Gomez 	 */
40556c31cdfSGerman Gomez 	if (idx >= ptr->wrapped_cnt) {
40656c31cdfSGerman Gomez 		err = arm_spe_alloc_wrapped_array(ptr, idx);
40756c31cdfSGerman Gomez 		if (err)
40856c31cdfSGerman Gomez 			return err;
40956c31cdfSGerman Gomez 	}
41056c31cdfSGerman Gomez 
41156c31cdfSGerman Gomez 	/*
41256c31cdfSGerman Gomez 	 * Check to see if *head has wrapped around.  If it hasn't only the
41356c31cdfSGerman Gomez 	 * amount of data between *head and *old is snapshot'ed to avoid
41456c31cdfSGerman Gomez 	 * bloating the perf.data file with zeros.  But as soon as *head has
41556c31cdfSGerman Gomez 	 * wrapped around the entire size of the AUX ring buffer it taken.
41656c31cdfSGerman Gomez 	 */
41756c31cdfSGerman Gomez 	wrapped = ptr->wrapped[idx];
41856c31cdfSGerman Gomez 	if (!wrapped && arm_spe_buffer_has_wrapped(data, mm->len, *head)) {
41956c31cdfSGerman Gomez 		wrapped = true;
42056c31cdfSGerman Gomez 		ptr->wrapped[idx] = true;
42156c31cdfSGerman Gomez 	}
42256c31cdfSGerman Gomez 
42356c31cdfSGerman Gomez 	pr_debug3("%s: mmap index %d old head %zu new head %zu size %zu\n",
42456c31cdfSGerman Gomez 		  __func__, idx, (size_t)*old, (size_t)*head, mm->len);
42556c31cdfSGerman Gomez 
42656c31cdfSGerman Gomez 	/*
42756c31cdfSGerman Gomez 	 * No wrap has occurred, we can just use *head and *old.
42856c31cdfSGerman Gomez 	 */
42956c31cdfSGerman Gomez 	if (!wrapped)
43056c31cdfSGerman Gomez 		return 0;
43156c31cdfSGerman Gomez 
43256c31cdfSGerman Gomez 	/*
43356c31cdfSGerman Gomez 	 * *head has wrapped around - adjust *head and *old to pickup the
43456c31cdfSGerman Gomez 	 * entire content of the AUX buffer.
43556c31cdfSGerman Gomez 	 */
43656c31cdfSGerman Gomez 	if (*head >= mm->len) {
43756c31cdfSGerman Gomez 		*old = *head - mm->len;
43856c31cdfSGerman Gomez 	} else {
43956c31cdfSGerman Gomez 		*head += mm->len;
44056c31cdfSGerman Gomez 		*old = *head - mm->len;
44156c31cdfSGerman Gomez 	}
44256c31cdfSGerman Gomez 
44356c31cdfSGerman Gomez 	return 0;
44456c31cdfSGerman Gomez }
44556c31cdfSGerman Gomez 
arm_spe_reference(struct auxtrace_record * itr __maybe_unused)446ffd3d18cSKim Phillips static u64 arm_spe_reference(struct auxtrace_record *itr __maybe_unused)
447ffd3d18cSKim Phillips {
448ffd3d18cSKim Phillips 	struct timespec ts;
449ffd3d18cSKim Phillips 
450ffd3d18cSKim Phillips 	clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
451ffd3d18cSKim Phillips 
452ffd3d18cSKim Phillips 	return ts.tv_sec ^ ts.tv_nsec;
453ffd3d18cSKim Phillips }
454ffd3d18cSKim Phillips 
arm_spe_recording_free(struct auxtrace_record * itr)455ffd3d18cSKim Phillips static void arm_spe_recording_free(struct auxtrace_record *itr)
456ffd3d18cSKim Phillips {
457ffd3d18cSKim Phillips 	struct arm_spe_recording *sper =
458ffd3d18cSKim Phillips 			container_of(itr, struct arm_spe_recording, itr);
459ffd3d18cSKim Phillips 
4602e384400SArnaldo Carvalho de Melo 	zfree(&sper->wrapped);
461ffd3d18cSKim Phillips 	free(sper);
462ffd3d18cSKim Phillips }
463ffd3d18cSKim Phillips 
arm_spe_recording_init(int * err,struct perf_pmu * arm_spe_pmu)464ffd3d18cSKim Phillips struct auxtrace_record *arm_spe_recording_init(int *err,
465ffd3d18cSKim Phillips 					       struct perf_pmu *arm_spe_pmu)
466ffd3d18cSKim Phillips {
467ffd3d18cSKim Phillips 	struct arm_spe_recording *sper;
468ffd3d18cSKim Phillips 
469ffd3d18cSKim Phillips 	if (!arm_spe_pmu) {
470ffd3d18cSKim Phillips 		*err = -ENODEV;
471ffd3d18cSKim Phillips 		return NULL;
472ffd3d18cSKim Phillips 	}
473ffd3d18cSKim Phillips 
474ffd3d18cSKim Phillips 	sper = zalloc(sizeof(struct arm_spe_recording));
475ffd3d18cSKim Phillips 	if (!sper) {
476ffd3d18cSKim Phillips 		*err = -ENOMEM;
477ffd3d18cSKim Phillips 		return NULL;
478ffd3d18cSKim Phillips 	}
479ffd3d18cSKim Phillips 
480ffd3d18cSKim Phillips 	sper->arm_spe_pmu = arm_spe_pmu;
481ad60ba0cSAdrian Hunter 	sper->itr.pmu = arm_spe_pmu;
4820901b560SGerman Gomez 	sper->itr.snapshot_start = arm_spe_snapshot_start;
4830901b560SGerman Gomez 	sper->itr.snapshot_finish = arm_spe_snapshot_finish;
48456c31cdfSGerman Gomez 	sper->itr.find_snapshot = arm_spe_find_snapshot;
4850901b560SGerman Gomez 	sper->itr.parse_snapshot_options = arm_spe_parse_snapshot_options;
486ffd3d18cSKim Phillips 	sper->itr.recording_options = arm_spe_recording_options;
487ffd3d18cSKim Phillips 	sper->itr.info_priv_size = arm_spe_info_priv_size;
488ffd3d18cSKim Phillips 	sper->itr.info_fill = arm_spe_info_fill;
489ffd3d18cSKim Phillips 	sper->itr.free = arm_spe_recording_free;
490ffd3d18cSKim Phillips 	sper->itr.reference = arm_spe_reference;
491ad60ba0cSAdrian Hunter 	sper->itr.read_finish = auxtrace_record__read_finish;
492ffd3d18cSKim Phillips 	sper->itr.alignment = 0;
493ffd3d18cSKim Phillips 
49434435336SKim Phillips 	*err = 0;
495ffd3d18cSKim Phillips 	return &sper->itr;
496ffd3d18cSKim Phillips }
497ffd3d18cSKim Phillips 
498ffd3d18cSKim Phillips struct perf_event_attr
arm_spe_pmu_default_config(struct perf_pmu * arm_spe_pmu)499ffd3d18cSKim Phillips *arm_spe_pmu_default_config(struct perf_pmu *arm_spe_pmu)
500ffd3d18cSKim Phillips {
501ffd3d18cSKim Phillips 	struct perf_event_attr *attr;
502ffd3d18cSKim Phillips 
503ffd3d18cSKim Phillips 	attr = zalloc(sizeof(struct perf_event_attr));
504ffd3d18cSKim Phillips 	if (!attr) {
505ffd3d18cSKim Phillips 		pr_err("arm_spe default config cannot allocate a perf_event_attr\n");
506ffd3d18cSKim Phillips 		return NULL;
507ffd3d18cSKim Phillips 	}
508ffd3d18cSKim Phillips 
509ffd3d18cSKim Phillips 	/*
510ffd3d18cSKim Phillips 	 * If kernel driver doesn't advertise a minimum,
511ffd3d18cSKim Phillips 	 * use max allowable by PMSIDR_EL1.INTERVAL
512ffd3d18cSKim Phillips 	 */
513ffd3d18cSKim Phillips 	if (perf_pmu__scan_file(arm_spe_pmu, "caps/min_interval", "%llu",
514ffd3d18cSKim Phillips 				  &attr->sample_period) != 1) {
515ffd3d18cSKim Phillips 		pr_debug("arm_spe driver doesn't advertise a min. interval. Using 4096\n");
516ffd3d18cSKim Phillips 		attr->sample_period = 4096;
517ffd3d18cSKim Phillips 	}
518ffd3d18cSKim Phillips 
519ffd3d18cSKim Phillips 	arm_spe_pmu->selectable = true;
520ffd3d18cSKim Phillips 	arm_spe_pmu->is_uncore = false;
521ffd3d18cSKim Phillips 
522ffd3d18cSKim Phillips 	return attr;
523ffd3d18cSKim Phillips }
524