xref: /OK3568_Linux_fs/kernel/tools/perf/util/arm-spe.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Arm Statistical Profiling Extensions (SPE) support
4*4882a593Smuzhiyun  * Copyright (c) 2017-2018, Arm Ltd.
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include <byteswap.h>
8*4882a593Smuzhiyun #include <endian.h>
9*4882a593Smuzhiyun #include <errno.h>
10*4882a593Smuzhiyun #include <inttypes.h>
11*4882a593Smuzhiyun #include <linux/bitops.h>
12*4882a593Smuzhiyun #include <linux/kernel.h>
13*4882a593Smuzhiyun #include <linux/log2.h>
14*4882a593Smuzhiyun #include <linux/types.h>
15*4882a593Smuzhiyun #include <linux/zalloc.h>
16*4882a593Smuzhiyun #include <stdlib.h>
17*4882a593Smuzhiyun #include <unistd.h>
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun #include "auxtrace.h"
20*4882a593Smuzhiyun #include "color.h"
21*4882a593Smuzhiyun #include "debug.h"
22*4882a593Smuzhiyun #include "evlist.h"
23*4882a593Smuzhiyun #include "evsel.h"
24*4882a593Smuzhiyun #include "machine.h"
25*4882a593Smuzhiyun #include "session.h"
26*4882a593Smuzhiyun #include "symbol.h"
27*4882a593Smuzhiyun #include "thread.h"
28*4882a593Smuzhiyun #include "thread-stack.h"
29*4882a593Smuzhiyun #include "tool.h"
30*4882a593Smuzhiyun #include "util/synthetic-events.h"
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun #include "arm-spe.h"
33*4882a593Smuzhiyun #include "arm-spe-decoder/arm-spe-decoder.h"
34*4882a593Smuzhiyun #include "arm-spe-decoder/arm-spe-pkt-decoder.h"
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun #define MAX_TIMESTAMP (~0ULL)
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun struct arm_spe {
39*4882a593Smuzhiyun 	struct auxtrace			auxtrace;
40*4882a593Smuzhiyun 	struct auxtrace_queues		queues;
41*4882a593Smuzhiyun 	struct auxtrace_heap		heap;
42*4882a593Smuzhiyun 	struct itrace_synth_opts        synth_opts;
43*4882a593Smuzhiyun 	u32				auxtrace_type;
44*4882a593Smuzhiyun 	struct perf_session		*session;
45*4882a593Smuzhiyun 	struct machine			*machine;
46*4882a593Smuzhiyun 	u32				pmu_type;
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	u8				timeless_decoding;
49*4882a593Smuzhiyun 	u8				data_queued;
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 	u64				sample_type;
52*4882a593Smuzhiyun 	u8				sample_flc;
53*4882a593Smuzhiyun 	u8				sample_llc;
54*4882a593Smuzhiyun 	u8				sample_tlb;
55*4882a593Smuzhiyun 	u8				sample_branch;
56*4882a593Smuzhiyun 	u8				sample_remote_access;
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	u64				l1d_miss_id;
59*4882a593Smuzhiyun 	u64				l1d_access_id;
60*4882a593Smuzhiyun 	u64				llc_miss_id;
61*4882a593Smuzhiyun 	u64				llc_access_id;
62*4882a593Smuzhiyun 	u64				tlb_miss_id;
63*4882a593Smuzhiyun 	u64				tlb_access_id;
64*4882a593Smuzhiyun 	u64				branch_miss_id;
65*4882a593Smuzhiyun 	u64				remote_access_id;
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	u64				kernel_start;
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 	unsigned long			num_events;
70*4882a593Smuzhiyun };
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun struct arm_spe_queue {
73*4882a593Smuzhiyun 	struct arm_spe			*spe;
74*4882a593Smuzhiyun 	unsigned int			queue_nr;
75*4882a593Smuzhiyun 	struct auxtrace_buffer		*buffer;
76*4882a593Smuzhiyun 	struct auxtrace_buffer		*old_buffer;
77*4882a593Smuzhiyun 	union perf_event		*event_buf;
78*4882a593Smuzhiyun 	bool				on_heap;
79*4882a593Smuzhiyun 	bool				done;
80*4882a593Smuzhiyun 	pid_t				pid;
81*4882a593Smuzhiyun 	pid_t				tid;
82*4882a593Smuzhiyun 	int				cpu;
83*4882a593Smuzhiyun 	struct arm_spe_decoder		*decoder;
84*4882a593Smuzhiyun 	u64				time;
85*4882a593Smuzhiyun 	u64				timestamp;
86*4882a593Smuzhiyun 	struct thread			*thread;
87*4882a593Smuzhiyun };
88*4882a593Smuzhiyun 
arm_spe_dump(struct arm_spe * spe __maybe_unused,unsigned char * buf,size_t len)89*4882a593Smuzhiyun static void arm_spe_dump(struct arm_spe *spe __maybe_unused,
90*4882a593Smuzhiyun 			 unsigned char *buf, size_t len)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun 	struct arm_spe_pkt packet;
93*4882a593Smuzhiyun 	size_t pos = 0;
94*4882a593Smuzhiyun 	int ret, pkt_len, i;
95*4882a593Smuzhiyun 	char desc[ARM_SPE_PKT_DESC_MAX];
96*4882a593Smuzhiyun 	const char *color = PERF_COLOR_BLUE;
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	color_fprintf(stdout, color,
99*4882a593Smuzhiyun 		      ". ... ARM SPE data: size %zu bytes\n",
100*4882a593Smuzhiyun 		      len);
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	while (len) {
103*4882a593Smuzhiyun 		ret = arm_spe_get_packet(buf, len, &packet);
104*4882a593Smuzhiyun 		if (ret > 0)
105*4882a593Smuzhiyun 			pkt_len = ret;
106*4882a593Smuzhiyun 		else
107*4882a593Smuzhiyun 			pkt_len = 1;
108*4882a593Smuzhiyun 		printf(".");
109*4882a593Smuzhiyun 		color_fprintf(stdout, color, "  %08x: ", pos);
110*4882a593Smuzhiyun 		for (i = 0; i < pkt_len; i++)
111*4882a593Smuzhiyun 			color_fprintf(stdout, color, " %02x", buf[i]);
112*4882a593Smuzhiyun 		for (; i < 16; i++)
113*4882a593Smuzhiyun 			color_fprintf(stdout, color, "   ");
114*4882a593Smuzhiyun 		if (ret > 0) {
115*4882a593Smuzhiyun 			ret = arm_spe_pkt_desc(&packet, desc,
116*4882a593Smuzhiyun 					       ARM_SPE_PKT_DESC_MAX);
117*4882a593Smuzhiyun 			if (ret > 0)
118*4882a593Smuzhiyun 				color_fprintf(stdout, color, " %s\n", desc);
119*4882a593Smuzhiyun 		} else {
120*4882a593Smuzhiyun 			color_fprintf(stdout, color, " Bad packet!\n");
121*4882a593Smuzhiyun 		}
122*4882a593Smuzhiyun 		pos += pkt_len;
123*4882a593Smuzhiyun 		buf += pkt_len;
124*4882a593Smuzhiyun 		len -= pkt_len;
125*4882a593Smuzhiyun 	}
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun 
arm_spe_dump_event(struct arm_spe * spe,unsigned char * buf,size_t len)128*4882a593Smuzhiyun static void arm_spe_dump_event(struct arm_spe *spe, unsigned char *buf,
129*4882a593Smuzhiyun 			       size_t len)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun 	printf(".\n");
132*4882a593Smuzhiyun 	arm_spe_dump(spe, buf, len);
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun 
arm_spe_get_trace(struct arm_spe_buffer * b,void * data)135*4882a593Smuzhiyun static int arm_spe_get_trace(struct arm_spe_buffer *b, void *data)
136*4882a593Smuzhiyun {
137*4882a593Smuzhiyun 	struct arm_spe_queue *speq = data;
138*4882a593Smuzhiyun 	struct auxtrace_buffer *buffer = speq->buffer;
139*4882a593Smuzhiyun 	struct auxtrace_buffer *old_buffer = speq->old_buffer;
140*4882a593Smuzhiyun 	struct auxtrace_queue *queue;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	queue = &speq->spe->queues.queue_array[speq->queue_nr];
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	buffer = auxtrace_buffer__next(queue, buffer);
145*4882a593Smuzhiyun 	/* If no more data, drop the previous auxtrace_buffer and return */
146*4882a593Smuzhiyun 	if (!buffer) {
147*4882a593Smuzhiyun 		if (old_buffer)
148*4882a593Smuzhiyun 			auxtrace_buffer__drop_data(old_buffer);
149*4882a593Smuzhiyun 		b->len = 0;
150*4882a593Smuzhiyun 		return 0;
151*4882a593Smuzhiyun 	}
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	speq->buffer = buffer;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	/* If the aux_buffer doesn't have data associated, try to load it */
156*4882a593Smuzhiyun 	if (!buffer->data) {
157*4882a593Smuzhiyun 		/* get the file desc associated with the perf data file */
158*4882a593Smuzhiyun 		int fd = perf_data__fd(speq->spe->session->data);
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 		buffer->data = auxtrace_buffer__get_data(buffer, fd);
161*4882a593Smuzhiyun 		if (!buffer->data)
162*4882a593Smuzhiyun 			return -ENOMEM;
163*4882a593Smuzhiyun 	}
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	b->len = buffer->size;
166*4882a593Smuzhiyun 	b->buf = buffer->data;
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	if (b->len) {
169*4882a593Smuzhiyun 		if (old_buffer)
170*4882a593Smuzhiyun 			auxtrace_buffer__drop_data(old_buffer);
171*4882a593Smuzhiyun 		speq->old_buffer = buffer;
172*4882a593Smuzhiyun 	} else {
173*4882a593Smuzhiyun 		auxtrace_buffer__drop_data(buffer);
174*4882a593Smuzhiyun 		return arm_spe_get_trace(b, data);
175*4882a593Smuzhiyun 	}
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 	return 0;
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun 
arm_spe__alloc_queue(struct arm_spe * spe,unsigned int queue_nr)180*4882a593Smuzhiyun static struct arm_spe_queue *arm_spe__alloc_queue(struct arm_spe *spe,
181*4882a593Smuzhiyun 		unsigned int queue_nr)
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun 	struct arm_spe_params params = { .get_trace = 0, };
184*4882a593Smuzhiyun 	struct arm_spe_queue *speq;
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	speq = zalloc(sizeof(*speq));
187*4882a593Smuzhiyun 	if (!speq)
188*4882a593Smuzhiyun 		return NULL;
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	speq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE);
191*4882a593Smuzhiyun 	if (!speq->event_buf)
192*4882a593Smuzhiyun 		goto out_free;
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun 	speq->spe = spe;
195*4882a593Smuzhiyun 	speq->queue_nr = queue_nr;
196*4882a593Smuzhiyun 	speq->pid = -1;
197*4882a593Smuzhiyun 	speq->tid = -1;
198*4882a593Smuzhiyun 	speq->cpu = -1;
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 	/* params set */
201*4882a593Smuzhiyun 	params.get_trace = arm_spe_get_trace;
202*4882a593Smuzhiyun 	params.data = speq;
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	/* create new decoder */
205*4882a593Smuzhiyun 	speq->decoder = arm_spe_decoder_new(&params);
206*4882a593Smuzhiyun 	if (!speq->decoder)
207*4882a593Smuzhiyun 		goto out_free;
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 	return speq;
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun out_free:
212*4882a593Smuzhiyun 	zfree(&speq->event_buf);
213*4882a593Smuzhiyun 	free(speq);
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 	return NULL;
216*4882a593Smuzhiyun }
217*4882a593Smuzhiyun 
arm_spe_cpumode(struct arm_spe * spe,u64 ip)218*4882a593Smuzhiyun static inline u8 arm_spe_cpumode(struct arm_spe *spe, u64 ip)
219*4882a593Smuzhiyun {
220*4882a593Smuzhiyun 	return ip >= spe->kernel_start ?
221*4882a593Smuzhiyun 		PERF_RECORD_MISC_KERNEL :
222*4882a593Smuzhiyun 		PERF_RECORD_MISC_USER;
223*4882a593Smuzhiyun }
224*4882a593Smuzhiyun 
arm_spe_prep_sample(struct arm_spe * spe,struct arm_spe_queue * speq,union perf_event * event,struct perf_sample * sample)225*4882a593Smuzhiyun static void arm_spe_prep_sample(struct arm_spe *spe,
226*4882a593Smuzhiyun 				struct arm_spe_queue *speq,
227*4882a593Smuzhiyun 				union perf_event *event,
228*4882a593Smuzhiyun 				struct perf_sample *sample)
229*4882a593Smuzhiyun {
230*4882a593Smuzhiyun 	struct arm_spe_record *record = &speq->decoder->record;
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 	if (!spe->timeless_decoding)
233*4882a593Smuzhiyun 		sample->time = speq->timestamp;
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	sample->ip = record->from_ip;
236*4882a593Smuzhiyun 	sample->cpumode = arm_spe_cpumode(spe, sample->ip);
237*4882a593Smuzhiyun 	sample->pid = speq->pid;
238*4882a593Smuzhiyun 	sample->tid = speq->tid;
239*4882a593Smuzhiyun 	sample->addr = record->to_ip;
240*4882a593Smuzhiyun 	sample->period = 1;
241*4882a593Smuzhiyun 	sample->cpu = speq->cpu;
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 	event->sample.header.type = PERF_RECORD_SAMPLE;
244*4882a593Smuzhiyun 	event->sample.header.misc = sample->cpumode;
245*4882a593Smuzhiyun 	event->sample.header.size = sizeof(struct perf_event_header);
246*4882a593Smuzhiyun }
247*4882a593Smuzhiyun 
arm_spe__inject_event(union perf_event * event,struct perf_sample * sample,u64 type)248*4882a593Smuzhiyun static int arm_spe__inject_event(union perf_event *event, struct perf_sample *sample, u64 type)
249*4882a593Smuzhiyun {
250*4882a593Smuzhiyun 	event->header.size = perf_event__sample_event_size(sample, type, 0);
251*4882a593Smuzhiyun 	return perf_event__synthesize_sample(event, type, 0, sample);
252*4882a593Smuzhiyun }
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun static inline int
arm_spe_deliver_synth_event(struct arm_spe * spe,struct arm_spe_queue * speq __maybe_unused,union perf_event * event,struct perf_sample * sample)255*4882a593Smuzhiyun arm_spe_deliver_synth_event(struct arm_spe *spe,
256*4882a593Smuzhiyun 			    struct arm_spe_queue *speq __maybe_unused,
257*4882a593Smuzhiyun 			    union perf_event *event,
258*4882a593Smuzhiyun 			    struct perf_sample *sample)
259*4882a593Smuzhiyun {
260*4882a593Smuzhiyun 	int ret;
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	if (spe->synth_opts.inject) {
263*4882a593Smuzhiyun 		ret = arm_spe__inject_event(event, sample, spe->sample_type);
264*4882a593Smuzhiyun 		if (ret)
265*4882a593Smuzhiyun 			return ret;
266*4882a593Smuzhiyun 	}
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 	ret = perf_session__deliver_synth_event(spe->session, event, sample);
269*4882a593Smuzhiyun 	if (ret)
270*4882a593Smuzhiyun 		pr_err("ARM SPE: failed to deliver event, error %d\n", ret);
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 	return ret;
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun static int
arm_spe_synth_spe_events_sample(struct arm_spe_queue * speq,u64 spe_events_id)276*4882a593Smuzhiyun arm_spe_synth_spe_events_sample(struct arm_spe_queue *speq,
277*4882a593Smuzhiyun 				u64 spe_events_id)
278*4882a593Smuzhiyun {
279*4882a593Smuzhiyun 	struct arm_spe *spe = speq->spe;
280*4882a593Smuzhiyun 	union perf_event *event = speq->event_buf;
281*4882a593Smuzhiyun 	struct perf_sample sample = { .ip = 0, };
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun 	arm_spe_prep_sample(spe, speq, event, &sample);
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	sample.id = spe_events_id;
286*4882a593Smuzhiyun 	sample.stream_id = spe_events_id;
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 	return arm_spe_deliver_synth_event(spe, speq, event, &sample);
289*4882a593Smuzhiyun }
290*4882a593Smuzhiyun 
arm_spe_sample(struct arm_spe_queue * speq)291*4882a593Smuzhiyun static int arm_spe_sample(struct arm_spe_queue *speq)
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun 	const struct arm_spe_record *record = &speq->decoder->record;
294*4882a593Smuzhiyun 	struct arm_spe *spe = speq->spe;
295*4882a593Smuzhiyun 	int err;
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	if (spe->sample_flc) {
298*4882a593Smuzhiyun 		if (record->type & ARM_SPE_L1D_MISS) {
299*4882a593Smuzhiyun 			err = arm_spe_synth_spe_events_sample(
300*4882a593Smuzhiyun 					speq, spe->l1d_miss_id);
301*4882a593Smuzhiyun 			if (err)
302*4882a593Smuzhiyun 				return err;
303*4882a593Smuzhiyun 		}
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun 		if (record->type & ARM_SPE_L1D_ACCESS) {
306*4882a593Smuzhiyun 			err = arm_spe_synth_spe_events_sample(
307*4882a593Smuzhiyun 					speq, spe->l1d_access_id);
308*4882a593Smuzhiyun 			if (err)
309*4882a593Smuzhiyun 				return err;
310*4882a593Smuzhiyun 		}
311*4882a593Smuzhiyun 	}
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun 	if (spe->sample_llc) {
314*4882a593Smuzhiyun 		if (record->type & ARM_SPE_LLC_MISS) {
315*4882a593Smuzhiyun 			err = arm_spe_synth_spe_events_sample(
316*4882a593Smuzhiyun 					speq, spe->llc_miss_id);
317*4882a593Smuzhiyun 			if (err)
318*4882a593Smuzhiyun 				return err;
319*4882a593Smuzhiyun 		}
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 		if (record->type & ARM_SPE_LLC_ACCESS) {
322*4882a593Smuzhiyun 			err = arm_spe_synth_spe_events_sample(
323*4882a593Smuzhiyun 					speq, spe->llc_access_id);
324*4882a593Smuzhiyun 			if (err)
325*4882a593Smuzhiyun 				return err;
326*4882a593Smuzhiyun 		}
327*4882a593Smuzhiyun 	}
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 	if (spe->sample_tlb) {
330*4882a593Smuzhiyun 		if (record->type & ARM_SPE_TLB_MISS) {
331*4882a593Smuzhiyun 			err = arm_spe_synth_spe_events_sample(
332*4882a593Smuzhiyun 					speq, spe->tlb_miss_id);
333*4882a593Smuzhiyun 			if (err)
334*4882a593Smuzhiyun 				return err;
335*4882a593Smuzhiyun 		}
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 		if (record->type & ARM_SPE_TLB_ACCESS) {
338*4882a593Smuzhiyun 			err = arm_spe_synth_spe_events_sample(
339*4882a593Smuzhiyun 					speq, spe->tlb_access_id);
340*4882a593Smuzhiyun 			if (err)
341*4882a593Smuzhiyun 				return err;
342*4882a593Smuzhiyun 		}
343*4882a593Smuzhiyun 	}
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun 	if (spe->sample_branch && (record->type & ARM_SPE_BRANCH_MISS)) {
346*4882a593Smuzhiyun 		err = arm_spe_synth_spe_events_sample(speq,
347*4882a593Smuzhiyun 						      spe->branch_miss_id);
348*4882a593Smuzhiyun 		if (err)
349*4882a593Smuzhiyun 			return err;
350*4882a593Smuzhiyun 	}
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun 	if (spe->sample_remote_access &&
353*4882a593Smuzhiyun 	    (record->type & ARM_SPE_REMOTE_ACCESS)) {
354*4882a593Smuzhiyun 		err = arm_spe_synth_spe_events_sample(speq,
355*4882a593Smuzhiyun 						      spe->remote_access_id);
356*4882a593Smuzhiyun 		if (err)
357*4882a593Smuzhiyun 			return err;
358*4882a593Smuzhiyun 	}
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun 	return 0;
361*4882a593Smuzhiyun }
362*4882a593Smuzhiyun 
arm_spe_run_decoder(struct arm_spe_queue * speq,u64 * timestamp)363*4882a593Smuzhiyun static int arm_spe_run_decoder(struct arm_spe_queue *speq, u64 *timestamp)
364*4882a593Smuzhiyun {
365*4882a593Smuzhiyun 	struct arm_spe *spe = speq->spe;
366*4882a593Smuzhiyun 	int ret;
367*4882a593Smuzhiyun 
368*4882a593Smuzhiyun 	if (!spe->kernel_start)
369*4882a593Smuzhiyun 		spe->kernel_start = machine__kernel_start(spe->machine);
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun 	while (1) {
372*4882a593Smuzhiyun 		ret = arm_spe_decode(speq->decoder);
373*4882a593Smuzhiyun 		if (!ret) {
374*4882a593Smuzhiyun 			pr_debug("No data or all data has been processed.\n");
375*4882a593Smuzhiyun 			return 1;
376*4882a593Smuzhiyun 		}
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun 		/*
379*4882a593Smuzhiyun 		 * Error is detected when decode SPE trace data, continue to
380*4882a593Smuzhiyun 		 * the next trace data and find out more records.
381*4882a593Smuzhiyun 		 */
382*4882a593Smuzhiyun 		if (ret < 0)
383*4882a593Smuzhiyun 			continue;
384*4882a593Smuzhiyun 
385*4882a593Smuzhiyun 		ret = arm_spe_sample(speq);
386*4882a593Smuzhiyun 		if (ret)
387*4882a593Smuzhiyun 			return ret;
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun 		if (!spe->timeless_decoding && speq->timestamp >= *timestamp) {
390*4882a593Smuzhiyun 			*timestamp = speq->timestamp;
391*4882a593Smuzhiyun 			return 0;
392*4882a593Smuzhiyun 		}
393*4882a593Smuzhiyun 	}
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun 	return 0;
396*4882a593Smuzhiyun }
397*4882a593Smuzhiyun 
arm_spe__setup_queue(struct arm_spe * spe,struct auxtrace_queue * queue,unsigned int queue_nr)398*4882a593Smuzhiyun static int arm_spe__setup_queue(struct arm_spe *spe,
399*4882a593Smuzhiyun 			       struct auxtrace_queue *queue,
400*4882a593Smuzhiyun 			       unsigned int queue_nr)
401*4882a593Smuzhiyun {
402*4882a593Smuzhiyun 	struct arm_spe_queue *speq = queue->priv;
403*4882a593Smuzhiyun 	struct arm_spe_record *record;
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun 	if (list_empty(&queue->head) || speq)
406*4882a593Smuzhiyun 		return 0;
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun 	speq = arm_spe__alloc_queue(spe, queue_nr);
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun 	if (!speq)
411*4882a593Smuzhiyun 		return -ENOMEM;
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun 	queue->priv = speq;
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun 	if (queue->cpu != -1)
416*4882a593Smuzhiyun 		speq->cpu = queue->cpu;
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 	if (!speq->on_heap) {
419*4882a593Smuzhiyun 		int ret;
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun 		if (spe->timeless_decoding)
422*4882a593Smuzhiyun 			return 0;
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun retry:
425*4882a593Smuzhiyun 		ret = arm_spe_decode(speq->decoder);
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 		if (!ret)
428*4882a593Smuzhiyun 			return 0;
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun 		if (ret < 0)
431*4882a593Smuzhiyun 			goto retry;
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 		record = &speq->decoder->record;
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun 		speq->timestamp = record->timestamp;
436*4882a593Smuzhiyun 		ret = auxtrace_heap__add(&spe->heap, queue_nr, speq->timestamp);
437*4882a593Smuzhiyun 		if (ret)
438*4882a593Smuzhiyun 			return ret;
439*4882a593Smuzhiyun 		speq->on_heap = true;
440*4882a593Smuzhiyun 	}
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun 	return 0;
443*4882a593Smuzhiyun }
444*4882a593Smuzhiyun 
arm_spe__setup_queues(struct arm_spe * spe)445*4882a593Smuzhiyun static int arm_spe__setup_queues(struct arm_spe *spe)
446*4882a593Smuzhiyun {
447*4882a593Smuzhiyun 	unsigned int i;
448*4882a593Smuzhiyun 	int ret;
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun 	for (i = 0; i < spe->queues.nr_queues; i++) {
451*4882a593Smuzhiyun 		ret = arm_spe__setup_queue(spe, &spe->queues.queue_array[i], i);
452*4882a593Smuzhiyun 		if (ret)
453*4882a593Smuzhiyun 			return ret;
454*4882a593Smuzhiyun 	}
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun 	return 0;
457*4882a593Smuzhiyun }
458*4882a593Smuzhiyun 
arm_spe__update_queues(struct arm_spe * spe)459*4882a593Smuzhiyun static int arm_spe__update_queues(struct arm_spe *spe)
460*4882a593Smuzhiyun {
461*4882a593Smuzhiyun 	if (spe->queues.new_data) {
462*4882a593Smuzhiyun 		spe->queues.new_data = false;
463*4882a593Smuzhiyun 		return arm_spe__setup_queues(spe);
464*4882a593Smuzhiyun 	}
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 	return 0;
467*4882a593Smuzhiyun }
468*4882a593Smuzhiyun 
arm_spe__is_timeless_decoding(struct arm_spe * spe)469*4882a593Smuzhiyun static bool arm_spe__is_timeless_decoding(struct arm_spe *spe)
470*4882a593Smuzhiyun {
471*4882a593Smuzhiyun 	struct evsel *evsel;
472*4882a593Smuzhiyun 	struct evlist *evlist = spe->session->evlist;
473*4882a593Smuzhiyun 	bool timeless_decoding = true;
474*4882a593Smuzhiyun 
475*4882a593Smuzhiyun 	/*
476*4882a593Smuzhiyun 	 * Circle through the list of event and complain if we find one
477*4882a593Smuzhiyun 	 * with the time bit set.
478*4882a593Smuzhiyun 	 */
479*4882a593Smuzhiyun 	evlist__for_each_entry(evlist, evsel) {
480*4882a593Smuzhiyun 		if ((evsel->core.attr.sample_type & PERF_SAMPLE_TIME))
481*4882a593Smuzhiyun 			timeless_decoding = false;
482*4882a593Smuzhiyun 	}
483*4882a593Smuzhiyun 
484*4882a593Smuzhiyun 	return timeless_decoding;
485*4882a593Smuzhiyun }
486*4882a593Smuzhiyun 
arm_spe_set_pid_tid_cpu(struct arm_spe * spe,struct auxtrace_queue * queue)487*4882a593Smuzhiyun static void arm_spe_set_pid_tid_cpu(struct arm_spe *spe,
488*4882a593Smuzhiyun 				    struct auxtrace_queue *queue)
489*4882a593Smuzhiyun {
490*4882a593Smuzhiyun 	struct arm_spe_queue *speq = queue->priv;
491*4882a593Smuzhiyun 	pid_t tid;
492*4882a593Smuzhiyun 
493*4882a593Smuzhiyun 	tid = machine__get_current_tid(spe->machine, speq->cpu);
494*4882a593Smuzhiyun 	if (tid != -1) {
495*4882a593Smuzhiyun 		speq->tid = tid;
496*4882a593Smuzhiyun 		thread__zput(speq->thread);
497*4882a593Smuzhiyun 	} else
498*4882a593Smuzhiyun 		speq->tid = queue->tid;
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun 	if ((!speq->thread) && (speq->tid != -1)) {
501*4882a593Smuzhiyun 		speq->thread = machine__find_thread(spe->machine, -1,
502*4882a593Smuzhiyun 						    speq->tid);
503*4882a593Smuzhiyun 	}
504*4882a593Smuzhiyun 
505*4882a593Smuzhiyun 	if (speq->thread) {
506*4882a593Smuzhiyun 		speq->pid = speq->thread->pid_;
507*4882a593Smuzhiyun 		if (queue->cpu == -1)
508*4882a593Smuzhiyun 			speq->cpu = speq->thread->cpu;
509*4882a593Smuzhiyun 	}
510*4882a593Smuzhiyun }
511*4882a593Smuzhiyun 
arm_spe_process_queues(struct arm_spe * spe,u64 timestamp)512*4882a593Smuzhiyun static int arm_spe_process_queues(struct arm_spe *spe, u64 timestamp)
513*4882a593Smuzhiyun {
514*4882a593Smuzhiyun 	unsigned int queue_nr;
515*4882a593Smuzhiyun 	u64 ts;
516*4882a593Smuzhiyun 	int ret;
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun 	while (1) {
519*4882a593Smuzhiyun 		struct auxtrace_queue *queue;
520*4882a593Smuzhiyun 		struct arm_spe_queue *speq;
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun 		if (!spe->heap.heap_cnt)
523*4882a593Smuzhiyun 			return 0;
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun 		if (spe->heap.heap_array[0].ordinal >= timestamp)
526*4882a593Smuzhiyun 			return 0;
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun 		queue_nr = spe->heap.heap_array[0].queue_nr;
529*4882a593Smuzhiyun 		queue = &spe->queues.queue_array[queue_nr];
530*4882a593Smuzhiyun 		speq = queue->priv;
531*4882a593Smuzhiyun 
532*4882a593Smuzhiyun 		auxtrace_heap__pop(&spe->heap);
533*4882a593Smuzhiyun 
534*4882a593Smuzhiyun 		if (spe->heap.heap_cnt) {
535*4882a593Smuzhiyun 			ts = spe->heap.heap_array[0].ordinal + 1;
536*4882a593Smuzhiyun 			if (ts > timestamp)
537*4882a593Smuzhiyun 				ts = timestamp;
538*4882a593Smuzhiyun 		} else {
539*4882a593Smuzhiyun 			ts = timestamp;
540*4882a593Smuzhiyun 		}
541*4882a593Smuzhiyun 
542*4882a593Smuzhiyun 		arm_spe_set_pid_tid_cpu(spe, queue);
543*4882a593Smuzhiyun 
544*4882a593Smuzhiyun 		ret = arm_spe_run_decoder(speq, &ts);
545*4882a593Smuzhiyun 		if (ret < 0) {
546*4882a593Smuzhiyun 			auxtrace_heap__add(&spe->heap, queue_nr, ts);
547*4882a593Smuzhiyun 			return ret;
548*4882a593Smuzhiyun 		}
549*4882a593Smuzhiyun 
550*4882a593Smuzhiyun 		if (!ret) {
551*4882a593Smuzhiyun 			ret = auxtrace_heap__add(&spe->heap, queue_nr, ts);
552*4882a593Smuzhiyun 			if (ret < 0)
553*4882a593Smuzhiyun 				return ret;
554*4882a593Smuzhiyun 		} else {
555*4882a593Smuzhiyun 			speq->on_heap = false;
556*4882a593Smuzhiyun 		}
557*4882a593Smuzhiyun 	}
558*4882a593Smuzhiyun 
559*4882a593Smuzhiyun 	return 0;
560*4882a593Smuzhiyun }
561*4882a593Smuzhiyun 
arm_spe_process_timeless_queues(struct arm_spe * spe,pid_t tid,u64 time_)562*4882a593Smuzhiyun static int arm_spe_process_timeless_queues(struct arm_spe *spe, pid_t tid,
563*4882a593Smuzhiyun 					    u64 time_)
564*4882a593Smuzhiyun {
565*4882a593Smuzhiyun 	struct auxtrace_queues *queues = &spe->queues;
566*4882a593Smuzhiyun 	unsigned int i;
567*4882a593Smuzhiyun 	u64 ts = 0;
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun 	for (i = 0; i < queues->nr_queues; i++) {
570*4882a593Smuzhiyun 		struct auxtrace_queue *queue = &spe->queues.queue_array[i];
571*4882a593Smuzhiyun 		struct arm_spe_queue *speq = queue->priv;
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun 		if (speq && (tid == -1 || speq->tid == tid)) {
574*4882a593Smuzhiyun 			speq->time = time_;
575*4882a593Smuzhiyun 			arm_spe_set_pid_tid_cpu(spe, queue);
576*4882a593Smuzhiyun 			arm_spe_run_decoder(speq, &ts);
577*4882a593Smuzhiyun 		}
578*4882a593Smuzhiyun 	}
579*4882a593Smuzhiyun 	return 0;
580*4882a593Smuzhiyun }
581*4882a593Smuzhiyun 
arm_spe_process_event(struct perf_session * session,union perf_event * event,struct perf_sample * sample,struct perf_tool * tool)582*4882a593Smuzhiyun static int arm_spe_process_event(struct perf_session *session,
583*4882a593Smuzhiyun 				 union perf_event *event,
584*4882a593Smuzhiyun 				 struct perf_sample *sample,
585*4882a593Smuzhiyun 				 struct perf_tool *tool)
586*4882a593Smuzhiyun {
587*4882a593Smuzhiyun 	int err = 0;
588*4882a593Smuzhiyun 	u64 timestamp;
589*4882a593Smuzhiyun 	struct arm_spe *spe = container_of(session->auxtrace,
590*4882a593Smuzhiyun 			struct arm_spe, auxtrace);
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun 	if (dump_trace)
593*4882a593Smuzhiyun 		return 0;
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun 	if (!tool->ordered_events) {
596*4882a593Smuzhiyun 		pr_err("SPE trace requires ordered events\n");
597*4882a593Smuzhiyun 		return -EINVAL;
598*4882a593Smuzhiyun 	}
599*4882a593Smuzhiyun 
600*4882a593Smuzhiyun 	if (sample->time && (sample->time != (u64) -1))
601*4882a593Smuzhiyun 		timestamp = sample->time;
602*4882a593Smuzhiyun 	else
603*4882a593Smuzhiyun 		timestamp = 0;
604*4882a593Smuzhiyun 
605*4882a593Smuzhiyun 	if (timestamp || spe->timeless_decoding) {
606*4882a593Smuzhiyun 		err = arm_spe__update_queues(spe);
607*4882a593Smuzhiyun 		if (err)
608*4882a593Smuzhiyun 			return err;
609*4882a593Smuzhiyun 	}
610*4882a593Smuzhiyun 
611*4882a593Smuzhiyun 	if (spe->timeless_decoding) {
612*4882a593Smuzhiyun 		if (event->header.type == PERF_RECORD_EXIT) {
613*4882a593Smuzhiyun 			err = arm_spe_process_timeless_queues(spe,
614*4882a593Smuzhiyun 					event->fork.tid,
615*4882a593Smuzhiyun 					sample->time);
616*4882a593Smuzhiyun 		}
617*4882a593Smuzhiyun 	} else if (timestamp) {
618*4882a593Smuzhiyun 		if (event->header.type == PERF_RECORD_EXIT) {
619*4882a593Smuzhiyun 			err = arm_spe_process_queues(spe, timestamp);
620*4882a593Smuzhiyun 			if (err)
621*4882a593Smuzhiyun 				return err;
622*4882a593Smuzhiyun 		}
623*4882a593Smuzhiyun 	}
624*4882a593Smuzhiyun 
625*4882a593Smuzhiyun 	return err;
626*4882a593Smuzhiyun }
627*4882a593Smuzhiyun 
arm_spe_process_auxtrace_event(struct perf_session * session,union perf_event * event,struct perf_tool * tool __maybe_unused)628*4882a593Smuzhiyun static int arm_spe_process_auxtrace_event(struct perf_session *session,
629*4882a593Smuzhiyun 					  union perf_event *event,
630*4882a593Smuzhiyun 					  struct perf_tool *tool __maybe_unused)
631*4882a593Smuzhiyun {
632*4882a593Smuzhiyun 	struct arm_spe *spe = container_of(session->auxtrace, struct arm_spe,
633*4882a593Smuzhiyun 					     auxtrace);
634*4882a593Smuzhiyun 
635*4882a593Smuzhiyun 	if (!spe->data_queued) {
636*4882a593Smuzhiyun 		struct auxtrace_buffer *buffer;
637*4882a593Smuzhiyun 		off_t data_offset;
638*4882a593Smuzhiyun 		int fd = perf_data__fd(session->data);
639*4882a593Smuzhiyun 		int err;
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun 		if (perf_data__is_pipe(session->data)) {
642*4882a593Smuzhiyun 			data_offset = 0;
643*4882a593Smuzhiyun 		} else {
644*4882a593Smuzhiyun 			data_offset = lseek(fd, 0, SEEK_CUR);
645*4882a593Smuzhiyun 			if (data_offset == -1)
646*4882a593Smuzhiyun 				return -errno;
647*4882a593Smuzhiyun 		}
648*4882a593Smuzhiyun 
649*4882a593Smuzhiyun 		err = auxtrace_queues__add_event(&spe->queues, session, event,
650*4882a593Smuzhiyun 				data_offset, &buffer);
651*4882a593Smuzhiyun 		if (err)
652*4882a593Smuzhiyun 			return err;
653*4882a593Smuzhiyun 
654*4882a593Smuzhiyun 		/* Dump here now we have copied a piped trace out of the pipe */
655*4882a593Smuzhiyun 		if (dump_trace) {
656*4882a593Smuzhiyun 			if (auxtrace_buffer__get_data(buffer, fd)) {
657*4882a593Smuzhiyun 				arm_spe_dump_event(spe, buffer->data,
658*4882a593Smuzhiyun 						buffer->size);
659*4882a593Smuzhiyun 				auxtrace_buffer__put_data(buffer);
660*4882a593Smuzhiyun 			}
661*4882a593Smuzhiyun 		}
662*4882a593Smuzhiyun 	}
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun 	return 0;
665*4882a593Smuzhiyun }
666*4882a593Smuzhiyun 
arm_spe_flush(struct perf_session * session __maybe_unused,struct perf_tool * tool __maybe_unused)667*4882a593Smuzhiyun static int arm_spe_flush(struct perf_session *session __maybe_unused,
668*4882a593Smuzhiyun 			 struct perf_tool *tool __maybe_unused)
669*4882a593Smuzhiyun {
670*4882a593Smuzhiyun 	struct arm_spe *spe = container_of(session->auxtrace, struct arm_spe,
671*4882a593Smuzhiyun 			auxtrace);
672*4882a593Smuzhiyun 	int ret;
673*4882a593Smuzhiyun 
674*4882a593Smuzhiyun 	if (dump_trace)
675*4882a593Smuzhiyun 		return 0;
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun 	if (!tool->ordered_events)
678*4882a593Smuzhiyun 		return -EINVAL;
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun 	ret = arm_spe__update_queues(spe);
681*4882a593Smuzhiyun 	if (ret < 0)
682*4882a593Smuzhiyun 		return ret;
683*4882a593Smuzhiyun 
684*4882a593Smuzhiyun 	if (spe->timeless_decoding)
685*4882a593Smuzhiyun 		return arm_spe_process_timeless_queues(spe, -1,
686*4882a593Smuzhiyun 				MAX_TIMESTAMP - 1);
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun 	return arm_spe_process_queues(spe, MAX_TIMESTAMP);
689*4882a593Smuzhiyun }
690*4882a593Smuzhiyun 
arm_spe_free_queue(void * priv)691*4882a593Smuzhiyun static void arm_spe_free_queue(void *priv)
692*4882a593Smuzhiyun {
693*4882a593Smuzhiyun 	struct arm_spe_queue *speq = priv;
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun 	if (!speq)
696*4882a593Smuzhiyun 		return;
697*4882a593Smuzhiyun 	thread__zput(speq->thread);
698*4882a593Smuzhiyun 	arm_spe_decoder_free(speq->decoder);
699*4882a593Smuzhiyun 	zfree(&speq->event_buf);
700*4882a593Smuzhiyun 	free(speq);
701*4882a593Smuzhiyun }
702*4882a593Smuzhiyun 
arm_spe_free_events(struct perf_session * session)703*4882a593Smuzhiyun static void arm_spe_free_events(struct perf_session *session)
704*4882a593Smuzhiyun {
705*4882a593Smuzhiyun 	struct arm_spe *spe = container_of(session->auxtrace, struct arm_spe,
706*4882a593Smuzhiyun 					     auxtrace);
707*4882a593Smuzhiyun 	struct auxtrace_queues *queues = &spe->queues;
708*4882a593Smuzhiyun 	unsigned int i;
709*4882a593Smuzhiyun 
710*4882a593Smuzhiyun 	for (i = 0; i < queues->nr_queues; i++) {
711*4882a593Smuzhiyun 		arm_spe_free_queue(queues->queue_array[i].priv);
712*4882a593Smuzhiyun 		queues->queue_array[i].priv = NULL;
713*4882a593Smuzhiyun 	}
714*4882a593Smuzhiyun 	auxtrace_queues__free(queues);
715*4882a593Smuzhiyun }
716*4882a593Smuzhiyun 
arm_spe_free(struct perf_session * session)717*4882a593Smuzhiyun static void arm_spe_free(struct perf_session *session)
718*4882a593Smuzhiyun {
719*4882a593Smuzhiyun 	struct arm_spe *spe = container_of(session->auxtrace, struct arm_spe,
720*4882a593Smuzhiyun 					     auxtrace);
721*4882a593Smuzhiyun 
722*4882a593Smuzhiyun 	auxtrace_heap__free(&spe->heap);
723*4882a593Smuzhiyun 	arm_spe_free_events(session);
724*4882a593Smuzhiyun 	session->auxtrace = NULL;
725*4882a593Smuzhiyun 	free(spe);
726*4882a593Smuzhiyun }
727*4882a593Smuzhiyun 
arm_spe_evsel_is_auxtrace(struct perf_session * session,struct evsel * evsel)728*4882a593Smuzhiyun static bool arm_spe_evsel_is_auxtrace(struct perf_session *session,
729*4882a593Smuzhiyun 				      struct evsel *evsel)
730*4882a593Smuzhiyun {
731*4882a593Smuzhiyun 	struct arm_spe *spe = container_of(session->auxtrace, struct arm_spe, auxtrace);
732*4882a593Smuzhiyun 
733*4882a593Smuzhiyun 	return evsel->core.attr.type == spe->pmu_type;
734*4882a593Smuzhiyun }
735*4882a593Smuzhiyun 
736*4882a593Smuzhiyun static const char * const arm_spe_info_fmts[] = {
737*4882a593Smuzhiyun 	[ARM_SPE_PMU_TYPE]		= "  PMU Type           %"PRId64"\n",
738*4882a593Smuzhiyun };
739*4882a593Smuzhiyun 
arm_spe_print_info(__u64 * arr)740*4882a593Smuzhiyun static void arm_spe_print_info(__u64 *arr)
741*4882a593Smuzhiyun {
742*4882a593Smuzhiyun 	if (!dump_trace)
743*4882a593Smuzhiyun 		return;
744*4882a593Smuzhiyun 
745*4882a593Smuzhiyun 	fprintf(stdout, arm_spe_info_fmts[ARM_SPE_PMU_TYPE], arr[ARM_SPE_PMU_TYPE]);
746*4882a593Smuzhiyun }
747*4882a593Smuzhiyun 
748*4882a593Smuzhiyun struct arm_spe_synth {
749*4882a593Smuzhiyun 	struct perf_tool dummy_tool;
750*4882a593Smuzhiyun 	struct perf_session *session;
751*4882a593Smuzhiyun };
752*4882a593Smuzhiyun 
arm_spe_event_synth(struct perf_tool * tool,union perf_event * event,struct perf_sample * sample __maybe_unused,struct machine * machine __maybe_unused)753*4882a593Smuzhiyun static int arm_spe_event_synth(struct perf_tool *tool,
754*4882a593Smuzhiyun 			       union perf_event *event,
755*4882a593Smuzhiyun 			       struct perf_sample *sample __maybe_unused,
756*4882a593Smuzhiyun 			       struct machine *machine __maybe_unused)
757*4882a593Smuzhiyun {
758*4882a593Smuzhiyun 	struct arm_spe_synth *arm_spe_synth =
759*4882a593Smuzhiyun 		      container_of(tool, struct arm_spe_synth, dummy_tool);
760*4882a593Smuzhiyun 
761*4882a593Smuzhiyun 	return perf_session__deliver_synth_event(arm_spe_synth->session,
762*4882a593Smuzhiyun 						 event, NULL);
763*4882a593Smuzhiyun }
764*4882a593Smuzhiyun 
arm_spe_synth_event(struct perf_session * session,struct perf_event_attr * attr,u64 id)765*4882a593Smuzhiyun static int arm_spe_synth_event(struct perf_session *session,
766*4882a593Smuzhiyun 			       struct perf_event_attr *attr, u64 id)
767*4882a593Smuzhiyun {
768*4882a593Smuzhiyun 	struct arm_spe_synth arm_spe_synth;
769*4882a593Smuzhiyun 
770*4882a593Smuzhiyun 	memset(&arm_spe_synth, 0, sizeof(struct arm_spe_synth));
771*4882a593Smuzhiyun 	arm_spe_synth.session = session;
772*4882a593Smuzhiyun 
773*4882a593Smuzhiyun 	return perf_event__synthesize_attr(&arm_spe_synth.dummy_tool, attr, 1,
774*4882a593Smuzhiyun 					   &id, arm_spe_event_synth);
775*4882a593Smuzhiyun }
776*4882a593Smuzhiyun 
arm_spe_set_event_name(struct evlist * evlist,u64 id,const char * name)777*4882a593Smuzhiyun static void arm_spe_set_event_name(struct evlist *evlist, u64 id,
778*4882a593Smuzhiyun 				    const char *name)
779*4882a593Smuzhiyun {
780*4882a593Smuzhiyun 	struct evsel *evsel;
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun 	evlist__for_each_entry(evlist, evsel) {
783*4882a593Smuzhiyun 		if (evsel->core.id && evsel->core.id[0] == id) {
784*4882a593Smuzhiyun 			if (evsel->name)
785*4882a593Smuzhiyun 				zfree(&evsel->name);
786*4882a593Smuzhiyun 			evsel->name = strdup(name);
787*4882a593Smuzhiyun 			break;
788*4882a593Smuzhiyun 		}
789*4882a593Smuzhiyun 	}
790*4882a593Smuzhiyun }
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun static int
arm_spe_synth_events(struct arm_spe * spe,struct perf_session * session)793*4882a593Smuzhiyun arm_spe_synth_events(struct arm_spe *spe, struct perf_session *session)
794*4882a593Smuzhiyun {
795*4882a593Smuzhiyun 	struct evlist *evlist = session->evlist;
796*4882a593Smuzhiyun 	struct evsel *evsel;
797*4882a593Smuzhiyun 	struct perf_event_attr attr;
798*4882a593Smuzhiyun 	bool found = false;
799*4882a593Smuzhiyun 	u64 id;
800*4882a593Smuzhiyun 	int err;
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun 	evlist__for_each_entry(evlist, evsel) {
803*4882a593Smuzhiyun 		if (evsel->core.attr.type == spe->pmu_type) {
804*4882a593Smuzhiyun 			found = true;
805*4882a593Smuzhiyun 			break;
806*4882a593Smuzhiyun 		}
807*4882a593Smuzhiyun 	}
808*4882a593Smuzhiyun 
809*4882a593Smuzhiyun 	if (!found) {
810*4882a593Smuzhiyun 		pr_debug("No selected events with SPE trace data\n");
811*4882a593Smuzhiyun 		return 0;
812*4882a593Smuzhiyun 	}
813*4882a593Smuzhiyun 
814*4882a593Smuzhiyun 	memset(&attr, 0, sizeof(struct perf_event_attr));
815*4882a593Smuzhiyun 	attr.size = sizeof(struct perf_event_attr);
816*4882a593Smuzhiyun 	attr.type = PERF_TYPE_HARDWARE;
817*4882a593Smuzhiyun 	attr.sample_type = evsel->core.attr.sample_type & PERF_SAMPLE_MASK;
818*4882a593Smuzhiyun 	attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
819*4882a593Smuzhiyun 		PERF_SAMPLE_PERIOD;
820*4882a593Smuzhiyun 	if (spe->timeless_decoding)
821*4882a593Smuzhiyun 		attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
822*4882a593Smuzhiyun 	else
823*4882a593Smuzhiyun 		attr.sample_type |= PERF_SAMPLE_TIME;
824*4882a593Smuzhiyun 
825*4882a593Smuzhiyun 	spe->sample_type = attr.sample_type;
826*4882a593Smuzhiyun 
827*4882a593Smuzhiyun 	attr.exclude_user = evsel->core.attr.exclude_user;
828*4882a593Smuzhiyun 	attr.exclude_kernel = evsel->core.attr.exclude_kernel;
829*4882a593Smuzhiyun 	attr.exclude_hv = evsel->core.attr.exclude_hv;
830*4882a593Smuzhiyun 	attr.exclude_host = evsel->core.attr.exclude_host;
831*4882a593Smuzhiyun 	attr.exclude_guest = evsel->core.attr.exclude_guest;
832*4882a593Smuzhiyun 	attr.sample_id_all = evsel->core.attr.sample_id_all;
833*4882a593Smuzhiyun 	attr.read_format = evsel->core.attr.read_format;
834*4882a593Smuzhiyun 
835*4882a593Smuzhiyun 	/* create new id val to be a fixed offset from evsel id */
836*4882a593Smuzhiyun 	id = evsel->core.id[0] + 1000000000;
837*4882a593Smuzhiyun 
838*4882a593Smuzhiyun 	if (!id)
839*4882a593Smuzhiyun 		id = 1;
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun 	if (spe->synth_opts.flc) {
842*4882a593Smuzhiyun 		spe->sample_flc = true;
843*4882a593Smuzhiyun 
844*4882a593Smuzhiyun 		/* Level 1 data cache miss */
845*4882a593Smuzhiyun 		err = arm_spe_synth_event(session, &attr, id);
846*4882a593Smuzhiyun 		if (err)
847*4882a593Smuzhiyun 			return err;
848*4882a593Smuzhiyun 		spe->l1d_miss_id = id;
849*4882a593Smuzhiyun 		arm_spe_set_event_name(evlist, id, "l1d-miss");
850*4882a593Smuzhiyun 		id += 1;
851*4882a593Smuzhiyun 
852*4882a593Smuzhiyun 		/* Level 1 data cache access */
853*4882a593Smuzhiyun 		err = arm_spe_synth_event(session, &attr, id);
854*4882a593Smuzhiyun 		if (err)
855*4882a593Smuzhiyun 			return err;
856*4882a593Smuzhiyun 		spe->l1d_access_id = id;
857*4882a593Smuzhiyun 		arm_spe_set_event_name(evlist, id, "l1d-access");
858*4882a593Smuzhiyun 		id += 1;
859*4882a593Smuzhiyun 	}
860*4882a593Smuzhiyun 
861*4882a593Smuzhiyun 	if (spe->synth_opts.llc) {
862*4882a593Smuzhiyun 		spe->sample_llc = true;
863*4882a593Smuzhiyun 
864*4882a593Smuzhiyun 		/* Last level cache miss */
865*4882a593Smuzhiyun 		err = arm_spe_synth_event(session, &attr, id);
866*4882a593Smuzhiyun 		if (err)
867*4882a593Smuzhiyun 			return err;
868*4882a593Smuzhiyun 		spe->llc_miss_id = id;
869*4882a593Smuzhiyun 		arm_spe_set_event_name(evlist, id, "llc-miss");
870*4882a593Smuzhiyun 		id += 1;
871*4882a593Smuzhiyun 
872*4882a593Smuzhiyun 		/* Last level cache access */
873*4882a593Smuzhiyun 		err = arm_spe_synth_event(session, &attr, id);
874*4882a593Smuzhiyun 		if (err)
875*4882a593Smuzhiyun 			return err;
876*4882a593Smuzhiyun 		spe->llc_access_id = id;
877*4882a593Smuzhiyun 		arm_spe_set_event_name(evlist, id, "llc-access");
878*4882a593Smuzhiyun 		id += 1;
879*4882a593Smuzhiyun 	}
880*4882a593Smuzhiyun 
881*4882a593Smuzhiyun 	if (spe->synth_opts.tlb) {
882*4882a593Smuzhiyun 		spe->sample_tlb = true;
883*4882a593Smuzhiyun 
884*4882a593Smuzhiyun 		/* TLB miss */
885*4882a593Smuzhiyun 		err = arm_spe_synth_event(session, &attr, id);
886*4882a593Smuzhiyun 		if (err)
887*4882a593Smuzhiyun 			return err;
888*4882a593Smuzhiyun 		spe->tlb_miss_id = id;
889*4882a593Smuzhiyun 		arm_spe_set_event_name(evlist, id, "tlb-miss");
890*4882a593Smuzhiyun 		id += 1;
891*4882a593Smuzhiyun 
892*4882a593Smuzhiyun 		/* TLB access */
893*4882a593Smuzhiyun 		err = arm_spe_synth_event(session, &attr, id);
894*4882a593Smuzhiyun 		if (err)
895*4882a593Smuzhiyun 			return err;
896*4882a593Smuzhiyun 		spe->tlb_access_id = id;
897*4882a593Smuzhiyun 		arm_spe_set_event_name(evlist, id, "tlb-access");
898*4882a593Smuzhiyun 		id += 1;
899*4882a593Smuzhiyun 	}
900*4882a593Smuzhiyun 
901*4882a593Smuzhiyun 	if (spe->synth_opts.branches) {
902*4882a593Smuzhiyun 		spe->sample_branch = true;
903*4882a593Smuzhiyun 
904*4882a593Smuzhiyun 		/* Branch miss */
905*4882a593Smuzhiyun 		err = arm_spe_synth_event(session, &attr, id);
906*4882a593Smuzhiyun 		if (err)
907*4882a593Smuzhiyun 			return err;
908*4882a593Smuzhiyun 		spe->branch_miss_id = id;
909*4882a593Smuzhiyun 		arm_spe_set_event_name(evlist, id, "branch-miss");
910*4882a593Smuzhiyun 		id += 1;
911*4882a593Smuzhiyun 	}
912*4882a593Smuzhiyun 
913*4882a593Smuzhiyun 	if (spe->synth_opts.remote_access) {
914*4882a593Smuzhiyun 		spe->sample_remote_access = true;
915*4882a593Smuzhiyun 
916*4882a593Smuzhiyun 		/* Remote access */
917*4882a593Smuzhiyun 		err = arm_spe_synth_event(session, &attr, id);
918*4882a593Smuzhiyun 		if (err)
919*4882a593Smuzhiyun 			return err;
920*4882a593Smuzhiyun 		spe->remote_access_id = id;
921*4882a593Smuzhiyun 		arm_spe_set_event_name(evlist, id, "remote-access");
922*4882a593Smuzhiyun 		id += 1;
923*4882a593Smuzhiyun 	}
924*4882a593Smuzhiyun 
925*4882a593Smuzhiyun 	return 0;
926*4882a593Smuzhiyun }
927*4882a593Smuzhiyun 
arm_spe_process_auxtrace_info(union perf_event * event,struct perf_session * session)928*4882a593Smuzhiyun int arm_spe_process_auxtrace_info(union perf_event *event,
929*4882a593Smuzhiyun 				  struct perf_session *session)
930*4882a593Smuzhiyun {
931*4882a593Smuzhiyun 	struct perf_record_auxtrace_info *auxtrace_info = &event->auxtrace_info;
932*4882a593Smuzhiyun 	size_t min_sz = sizeof(u64) * ARM_SPE_AUXTRACE_PRIV_MAX;
933*4882a593Smuzhiyun 	struct arm_spe *spe;
934*4882a593Smuzhiyun 	int err;
935*4882a593Smuzhiyun 
936*4882a593Smuzhiyun 	if (auxtrace_info->header.size < sizeof(struct perf_record_auxtrace_info) +
937*4882a593Smuzhiyun 					min_sz)
938*4882a593Smuzhiyun 		return -EINVAL;
939*4882a593Smuzhiyun 
940*4882a593Smuzhiyun 	spe = zalloc(sizeof(struct arm_spe));
941*4882a593Smuzhiyun 	if (!spe)
942*4882a593Smuzhiyun 		return -ENOMEM;
943*4882a593Smuzhiyun 
944*4882a593Smuzhiyun 	err = auxtrace_queues__init(&spe->queues);
945*4882a593Smuzhiyun 	if (err)
946*4882a593Smuzhiyun 		goto err_free;
947*4882a593Smuzhiyun 
948*4882a593Smuzhiyun 	spe->session = session;
949*4882a593Smuzhiyun 	spe->machine = &session->machines.host; /* No kvm support */
950*4882a593Smuzhiyun 	spe->auxtrace_type = auxtrace_info->type;
951*4882a593Smuzhiyun 	spe->pmu_type = auxtrace_info->priv[ARM_SPE_PMU_TYPE];
952*4882a593Smuzhiyun 
953*4882a593Smuzhiyun 	spe->timeless_decoding = arm_spe__is_timeless_decoding(spe);
954*4882a593Smuzhiyun 	spe->auxtrace.process_event = arm_spe_process_event;
955*4882a593Smuzhiyun 	spe->auxtrace.process_auxtrace_event = arm_spe_process_auxtrace_event;
956*4882a593Smuzhiyun 	spe->auxtrace.flush_events = arm_spe_flush;
957*4882a593Smuzhiyun 	spe->auxtrace.free_events = arm_spe_free_events;
958*4882a593Smuzhiyun 	spe->auxtrace.free = arm_spe_free;
959*4882a593Smuzhiyun 	spe->auxtrace.evsel_is_auxtrace = arm_spe_evsel_is_auxtrace;
960*4882a593Smuzhiyun 	session->auxtrace = &spe->auxtrace;
961*4882a593Smuzhiyun 
962*4882a593Smuzhiyun 	arm_spe_print_info(&auxtrace_info->priv[0]);
963*4882a593Smuzhiyun 
964*4882a593Smuzhiyun 	if (dump_trace)
965*4882a593Smuzhiyun 		return 0;
966*4882a593Smuzhiyun 
967*4882a593Smuzhiyun 	if (session->itrace_synth_opts && session->itrace_synth_opts->set)
968*4882a593Smuzhiyun 		spe->synth_opts = *session->itrace_synth_opts;
969*4882a593Smuzhiyun 	else
970*4882a593Smuzhiyun 		itrace_synth_opts__set_default(&spe->synth_opts, false);
971*4882a593Smuzhiyun 
972*4882a593Smuzhiyun 	err = arm_spe_synth_events(spe, session);
973*4882a593Smuzhiyun 	if (err)
974*4882a593Smuzhiyun 		goto err_free_queues;
975*4882a593Smuzhiyun 
976*4882a593Smuzhiyun 	err = auxtrace_queues__process_index(&spe->queues, session);
977*4882a593Smuzhiyun 	if (err)
978*4882a593Smuzhiyun 		goto err_free_queues;
979*4882a593Smuzhiyun 
980*4882a593Smuzhiyun 	if (spe->queues.populated)
981*4882a593Smuzhiyun 		spe->data_queued = true;
982*4882a593Smuzhiyun 
983*4882a593Smuzhiyun 	return 0;
984*4882a593Smuzhiyun 
985*4882a593Smuzhiyun err_free_queues:
986*4882a593Smuzhiyun 	auxtrace_queues__free(&spe->queues);
987*4882a593Smuzhiyun 	session->auxtrace = NULL;
988*4882a593Smuzhiyun err_free:
989*4882a593Smuzhiyun 	free(spe);
990*4882a593Smuzhiyun 	return err;
991*4882a593Smuzhiyun }
992