xref: /OK3568_Linux_fs/kernel/tools/perf/util/s390-cpumsf.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright IBM Corp. 2018
4*4882a593Smuzhiyun  * Auxtrace support for s390 CPU-Measurement Sampling Facility
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Author(s):  Thomas Richter <tmricht@linux.ibm.com>
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Auxiliary traces are collected during 'perf record' using rbd000 event.
9*4882a593Smuzhiyun  * Several PERF_RECORD_XXX are generated during recording:
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * PERF_RECORD_AUX:
12*4882a593Smuzhiyun  *	Records that new data landed in the AUX buffer part.
13*4882a593Smuzhiyun  * PERF_RECORD_AUXTRACE:
14*4882a593Smuzhiyun  *	Defines auxtrace data. Followed by the actual data. The contents of
15*4882a593Smuzhiyun  *	the auxtrace data is dependent on the event and the CPU.
16*4882a593Smuzhiyun  *	This record is generated by perf record command. For details
17*4882a593Smuzhiyun  *	see Documentation/perf.data-file-format.txt.
18*4882a593Smuzhiyun  * PERF_RECORD_AUXTRACE_INFO:
19*4882a593Smuzhiyun  *	Defines a table of contains for PERF_RECORD_AUXTRACE records. This
20*4882a593Smuzhiyun  *	record is generated during 'perf record' command. Each record contains
21*4882a593Smuzhiyun  *	up to 256 entries describing offset and size of the AUXTRACE data in the
22*4882a593Smuzhiyun  *	perf.data file.
23*4882a593Smuzhiyun  * PERF_RECORD_AUXTRACE_ERROR:
24*4882a593Smuzhiyun  *	Indicates an error during AUXTRACE collection such as buffer overflow.
25*4882a593Smuzhiyun  * PERF_RECORD_FINISHED_ROUND:
26*4882a593Smuzhiyun  *	Perf events are not necessarily in time stamp order, as they can be
27*4882a593Smuzhiyun  *	collected in parallel on different CPUs. If the events should be
28*4882a593Smuzhiyun  *	processed in time order they need to be sorted first.
29*4882a593Smuzhiyun  *	Perf report guarantees that there is no reordering over a
30*4882a593Smuzhiyun  *	PERF_RECORD_FINISHED_ROUND boundary event. All perf records with a
31*4882a593Smuzhiyun  *	time stamp lower than this record are processed (and displayed) before
32*4882a593Smuzhiyun  *	the succeeding perf record are processed.
33*4882a593Smuzhiyun  *
34*4882a593Smuzhiyun  * These records are evaluated during perf report command.
35*4882a593Smuzhiyun  *
36*4882a593Smuzhiyun  * 1. PERF_RECORD_AUXTRACE_INFO is used to set up the infrastructure for
37*4882a593Smuzhiyun  * auxiliary trace data processing. See s390_cpumsf_process_auxtrace_info()
38*4882a593Smuzhiyun  * below.
39*4882a593Smuzhiyun  * Auxiliary trace data is collected per CPU. To merge the data into the report
40*4882a593Smuzhiyun  * an auxtrace_queue is created for each CPU. It is assumed that the auxtrace
41*4882a593Smuzhiyun  * data is in ascending order.
42*4882a593Smuzhiyun  *
43*4882a593Smuzhiyun  * Each queue has a double linked list of auxtrace_buffers. This list contains
44*4882a593Smuzhiyun  * the offset and size of a CPU's auxtrace data. During auxtrace processing
45*4882a593Smuzhiyun  * the data portion is mmap()'ed.
46*4882a593Smuzhiyun  *
47*4882a593Smuzhiyun  * To sort the queues in chronological order, all queue access is controlled
48*4882a593Smuzhiyun  * by the auxtrace_heap. This is basicly a stack, each stack element has two
49*4882a593Smuzhiyun  * entries, the queue number and a time stamp. However the stack is sorted by
50*4882a593Smuzhiyun  * the time stamps. The highest time stamp is at the bottom the lowest
51*4882a593Smuzhiyun  * (nearest) time stamp is at the top. That sort order is maintained at all
52*4882a593Smuzhiyun  * times!
53*4882a593Smuzhiyun  *
54*4882a593Smuzhiyun  * After the auxtrace infrastructure has been setup, the auxtrace queues are
55*4882a593Smuzhiyun  * filled with data (offset/size pairs) and the auxtrace_heap is populated.
56*4882a593Smuzhiyun  *
57*4882a593Smuzhiyun  * 2. PERF_RECORD_XXX processing triggers access to the auxtrace_queues.
58*4882a593Smuzhiyun  * Each record is handled by s390_cpumsf_process_event(). The time stamp of
59*4882a593Smuzhiyun  * the perf record is compared with the time stamp located on the auxtrace_heap
60*4882a593Smuzhiyun  * top element. If that time stamp is lower than the time stamp from the
61*4882a593Smuzhiyun  * record sample, the auxtrace queues will be processed. As auxtrace queues
62*4882a593Smuzhiyun  * control many auxtrace_buffers and each buffer can be quite large, the
63*4882a593Smuzhiyun  * auxtrace buffer might be processed only partially. In this case the
64*4882a593Smuzhiyun  * position in the auxtrace_buffer of that queue is remembered and the time
65*4882a593Smuzhiyun  * stamp of the last processed entry of the auxtrace_buffer replaces the
66*4882a593Smuzhiyun  * current auxtrace_heap top.
67*4882a593Smuzhiyun  *
68*4882a593Smuzhiyun  * 3. Auxtrace_queues might run of out data and are feeded by the
69*4882a593Smuzhiyun  * PERF_RECORD_AUXTRACE handling, see s390_cpumsf_process_auxtrace_event().
70*4882a593Smuzhiyun  *
71*4882a593Smuzhiyun  * Event Generation
72*4882a593Smuzhiyun  * Each sampling-data entry in the auxilary trace data generates a perf sample.
73*4882a593Smuzhiyun  * This sample is filled
74*4882a593Smuzhiyun  * with data from the auxtrace such as PID/TID, instruction address, CPU state,
75*4882a593Smuzhiyun  * etc. This sample is processed with perf_session__deliver_synth_event() to
76*4882a593Smuzhiyun  * be included into the GUI.
77*4882a593Smuzhiyun  *
78*4882a593Smuzhiyun  * 4. PERF_RECORD_FINISHED_ROUND event is used to process all the remaining
79*4882a593Smuzhiyun  * auxiliary traces entries until the time stamp of this record is reached
80*4882a593Smuzhiyun  * auxtrace_heap top. This is triggered by ordered_event->deliver().
81*4882a593Smuzhiyun  *
82*4882a593Smuzhiyun  *
83*4882a593Smuzhiyun  * Perf event processing.
84*4882a593Smuzhiyun  * Event processing of PERF_RECORD_XXX entries relies on time stamp entries.
85*4882a593Smuzhiyun  * This is the function call sequence:
86*4882a593Smuzhiyun  *
87*4882a593Smuzhiyun  * __cmd_report()
88*4882a593Smuzhiyun  * |
89*4882a593Smuzhiyun  * perf_session__process_events()
90*4882a593Smuzhiyun  * |
91*4882a593Smuzhiyun  * __perf_session__process_events()
92*4882a593Smuzhiyun  * |
93*4882a593Smuzhiyun  * perf_session__process_event()
94*4882a593Smuzhiyun  * |  This functions splits the PERF_RECORD_XXX records.
95*4882a593Smuzhiyun  * |  - Those generated by perf record command (type number equal or higher
96*4882a593Smuzhiyun  * |    than PERF_RECORD_USER_TYPE_START) are handled by
97*4882a593Smuzhiyun  * |    perf_session__process_user_event(see below)
98*4882a593Smuzhiyun  * |  - Those generated by the kernel are handled by
99*4882a593Smuzhiyun  * |    perf_evlist__parse_sample_timestamp()
100*4882a593Smuzhiyun  * |
101*4882a593Smuzhiyun  * perf_evlist__parse_sample_timestamp()
102*4882a593Smuzhiyun  * |  Extract time stamp from sample data.
103*4882a593Smuzhiyun  * |
104*4882a593Smuzhiyun  * perf_session__queue_event()
105*4882a593Smuzhiyun  * |  If timestamp is positive the sample is entered into an ordered_event
106*4882a593Smuzhiyun  * |  list, sort order is the timestamp. The event processing is deferred until
107*4882a593Smuzhiyun  * |  later (see perf_session__process_user_event()).
108*4882a593Smuzhiyun  * |  Other timestamps (0 or -1) are handled immediately by
109*4882a593Smuzhiyun  * |  perf_session__deliver_event(). These are events generated at start up
110*4882a593Smuzhiyun  * |  of command perf record. They create PERF_RECORD_COMM and PERF_RECORD_MMAP*
111*4882a593Smuzhiyun  * |  records. They are needed to create a list of running processes and its
112*4882a593Smuzhiyun  * |  memory mappings and layout. They are needed at the beginning to enable
113*4882a593Smuzhiyun  * |  command perf report to create process trees and memory mappings.
114*4882a593Smuzhiyun  * |
115*4882a593Smuzhiyun  * perf_session__deliver_event()
116*4882a593Smuzhiyun  * |  Delivers a PERF_RECORD_XXX entry for handling.
117*4882a593Smuzhiyun  * |
118*4882a593Smuzhiyun  * auxtrace__process_event()
119*4882a593Smuzhiyun  * |  The timestamp of the PERF_RECORD_XXX entry is taken to correlate with
120*4882a593Smuzhiyun  * |  time stamps from the auxiliary trace buffers. This enables
121*4882a593Smuzhiyun  * |  synchronization between auxiliary trace data and the events on the
122*4882a593Smuzhiyun  * |  perf.data file.
123*4882a593Smuzhiyun  * |
124*4882a593Smuzhiyun  * machine__deliver_event()
125*4882a593Smuzhiyun  * |  Handles the PERF_RECORD_XXX event. This depends on the record type.
126*4882a593Smuzhiyun  *    It might update the process tree, update a process memory map or enter
127*4882a593Smuzhiyun  *    a sample with IP and call back chain data into GUI data pool.
128*4882a593Smuzhiyun  *
129*4882a593Smuzhiyun  *
130*4882a593Smuzhiyun  * Deferred processing determined by perf_session__process_user_event() is
131*4882a593Smuzhiyun  * finally processed when a PERF_RECORD_FINISHED_ROUND is encountered. These
132*4882a593Smuzhiyun  * are generated during command perf record.
133*4882a593Smuzhiyun  * The timestamp of PERF_RECORD_FINISHED_ROUND event is taken to process all
134*4882a593Smuzhiyun  * PERF_RECORD_XXX entries stored in the ordered_event list. This list was
135*4882a593Smuzhiyun  * built up while reading the perf.data file.
136*4882a593Smuzhiyun  * Each event is now processed by calling perf_session__deliver_event().
137*4882a593Smuzhiyun  * This enables time synchronization between the data in the perf.data file and
138*4882a593Smuzhiyun  * the data in the auxiliary trace buffers.
139*4882a593Smuzhiyun  */
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun #include <endian.h>
142*4882a593Smuzhiyun #include <errno.h>
143*4882a593Smuzhiyun #include <byteswap.h>
144*4882a593Smuzhiyun #include <inttypes.h>
145*4882a593Smuzhiyun #include <linux/kernel.h>
146*4882a593Smuzhiyun #include <linux/types.h>
147*4882a593Smuzhiyun #include <linux/bitops.h>
148*4882a593Smuzhiyun #include <linux/log2.h>
149*4882a593Smuzhiyun #include <linux/zalloc.h>
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun #include <sys/stat.h>
152*4882a593Smuzhiyun #include <sys/types.h>
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun #include "color.h"
155*4882a593Smuzhiyun #include "evsel.h"
156*4882a593Smuzhiyun #include "evlist.h"
157*4882a593Smuzhiyun #include "machine.h"
158*4882a593Smuzhiyun #include "session.h"
159*4882a593Smuzhiyun #include "tool.h"
160*4882a593Smuzhiyun #include "debug.h"
161*4882a593Smuzhiyun #include "auxtrace.h"
162*4882a593Smuzhiyun #include "s390-cpumsf.h"
163*4882a593Smuzhiyun #include "s390-cpumsf-kernel.h"
164*4882a593Smuzhiyun #include "s390-cpumcf-kernel.h"
165*4882a593Smuzhiyun #include "config.h"
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun struct s390_cpumsf {
168*4882a593Smuzhiyun 	struct auxtrace		auxtrace;
169*4882a593Smuzhiyun 	struct auxtrace_queues	queues;
170*4882a593Smuzhiyun 	struct auxtrace_heap	heap;
171*4882a593Smuzhiyun 	struct perf_session	*session;
172*4882a593Smuzhiyun 	struct machine		*machine;
173*4882a593Smuzhiyun 	u32			auxtrace_type;
174*4882a593Smuzhiyun 	u32			pmu_type;
175*4882a593Smuzhiyun 	u16			machine_type;
176*4882a593Smuzhiyun 	bool			data_queued;
177*4882a593Smuzhiyun 	bool			use_logfile;
178*4882a593Smuzhiyun 	char			*logdir;
179*4882a593Smuzhiyun };
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun struct s390_cpumsf_queue {
182*4882a593Smuzhiyun 	struct s390_cpumsf	*sf;
183*4882a593Smuzhiyun 	unsigned int		queue_nr;
184*4882a593Smuzhiyun 	struct auxtrace_buffer	*buffer;
185*4882a593Smuzhiyun 	int			cpu;
186*4882a593Smuzhiyun 	FILE			*logfile;
187*4882a593Smuzhiyun 	FILE			*logfile_ctr;
188*4882a593Smuzhiyun };
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun /* Check if the raw data should be dumped to file. If this is the case and
191*4882a593Smuzhiyun  * the file to dump to has not been opened for writing, do so.
192*4882a593Smuzhiyun  *
193*4882a593Smuzhiyun  * Return 0 on success and greater zero on error so processing continues.
194*4882a593Smuzhiyun  */
s390_cpumcf_dumpctr(struct s390_cpumsf * sf,struct perf_sample * sample)195*4882a593Smuzhiyun static int s390_cpumcf_dumpctr(struct s390_cpumsf *sf,
196*4882a593Smuzhiyun 			       struct perf_sample *sample)
197*4882a593Smuzhiyun {
198*4882a593Smuzhiyun 	struct s390_cpumsf_queue *sfq;
199*4882a593Smuzhiyun 	struct auxtrace_queue *q;
200*4882a593Smuzhiyun 	int rc = 0;
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	if (!sf->use_logfile || sf->queues.nr_queues <= sample->cpu)
203*4882a593Smuzhiyun 		return rc;
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	q = &sf->queues.queue_array[sample->cpu];
206*4882a593Smuzhiyun 	sfq = q->priv;
207*4882a593Smuzhiyun 	if (!sfq)		/* Queue not yet allocated */
208*4882a593Smuzhiyun 		return rc;
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	if (!sfq->logfile_ctr) {
211*4882a593Smuzhiyun 		char *name;
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 		rc = (sf->logdir)
214*4882a593Smuzhiyun 			? asprintf(&name, "%s/aux.ctr.%02x",
215*4882a593Smuzhiyun 				 sf->logdir, sample->cpu)
216*4882a593Smuzhiyun 			: asprintf(&name, "aux.ctr.%02x", sample->cpu);
217*4882a593Smuzhiyun 		if (rc > 0)
218*4882a593Smuzhiyun 			sfq->logfile_ctr = fopen(name, "w");
219*4882a593Smuzhiyun 		if (sfq->logfile_ctr == NULL) {
220*4882a593Smuzhiyun 			pr_err("Failed to open counter set log file %s, "
221*4882a593Smuzhiyun 			       "continue...\n", name);
222*4882a593Smuzhiyun 			rc = 1;
223*4882a593Smuzhiyun 		}
224*4882a593Smuzhiyun 		free(name);
225*4882a593Smuzhiyun 	}
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun 	if (sfq->logfile_ctr) {
228*4882a593Smuzhiyun 		/* See comment above for -4 */
229*4882a593Smuzhiyun 		size_t n = fwrite(sample->raw_data, sample->raw_size - 4, 1,
230*4882a593Smuzhiyun 				  sfq->logfile_ctr);
231*4882a593Smuzhiyun 		if (n != 1) {
232*4882a593Smuzhiyun 			pr_err("Failed to write counter set data\n");
233*4882a593Smuzhiyun 			rc = 1;
234*4882a593Smuzhiyun 		}
235*4882a593Smuzhiyun 	}
236*4882a593Smuzhiyun 	return rc;
237*4882a593Smuzhiyun }
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun /* Display s390 CPU measurement facility basic-sampling data entry
240*4882a593Smuzhiyun  * Data written on s390 in big endian byte order and contains bit
241*4882a593Smuzhiyun  * fields across byte boundaries.
242*4882a593Smuzhiyun  */
s390_cpumsf_basic_show(const char * color,size_t pos,struct hws_basic_entry * basicp)243*4882a593Smuzhiyun static bool s390_cpumsf_basic_show(const char *color, size_t pos,
244*4882a593Smuzhiyun 				   struct hws_basic_entry *basicp)
245*4882a593Smuzhiyun {
246*4882a593Smuzhiyun 	struct hws_basic_entry *basic = basicp;
247*4882a593Smuzhiyun #if __BYTE_ORDER == __LITTLE_ENDIAN
248*4882a593Smuzhiyun 	struct hws_basic_entry local;
249*4882a593Smuzhiyun 	unsigned long long word = be64toh(*(unsigned long long *)basicp);
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	memset(&local, 0, sizeof(local));
252*4882a593Smuzhiyun 	local.def = be16toh(basicp->def);
253*4882a593Smuzhiyun 	local.prim_asn = word & 0xffff;
254*4882a593Smuzhiyun 	local.CL = word >> 30 & 0x3;
255*4882a593Smuzhiyun 	local.I = word >> 32 & 0x1;
256*4882a593Smuzhiyun 	local.AS = word >> 33 & 0x3;
257*4882a593Smuzhiyun 	local.P = word >> 35 & 0x1;
258*4882a593Smuzhiyun 	local.W = word >> 36 & 0x1;
259*4882a593Smuzhiyun 	local.T = word >> 37 & 0x1;
260*4882a593Smuzhiyun 	local.U = word >> 40 & 0xf;
261*4882a593Smuzhiyun 	local.ia = be64toh(basicp->ia);
262*4882a593Smuzhiyun 	local.gpp = be64toh(basicp->gpp);
263*4882a593Smuzhiyun 	local.hpp = be64toh(basicp->hpp);
264*4882a593Smuzhiyun 	basic = &local;
265*4882a593Smuzhiyun #endif
266*4882a593Smuzhiyun 	if (basic->def != 1) {
267*4882a593Smuzhiyun 		pr_err("Invalid AUX trace basic entry [%#08zx]\n", pos);
268*4882a593Smuzhiyun 		return false;
269*4882a593Smuzhiyun 	}
270*4882a593Smuzhiyun 	color_fprintf(stdout, color, "    [%#08zx] Basic   Def:%04x Inst:%#04x"
271*4882a593Smuzhiyun 		      " %c%c%c%c AS:%d ASN:%#04x IA:%#018llx\n"
272*4882a593Smuzhiyun 		      "\t\tCL:%d HPP:%#018llx GPP:%#018llx\n",
273*4882a593Smuzhiyun 		      pos, basic->def, basic->U,
274*4882a593Smuzhiyun 		      basic->T ? 'T' : ' ',
275*4882a593Smuzhiyun 		      basic->W ? 'W' : ' ',
276*4882a593Smuzhiyun 		      basic->P ? 'P' : ' ',
277*4882a593Smuzhiyun 		      basic->I ? 'I' : ' ',
278*4882a593Smuzhiyun 		      basic->AS, basic->prim_asn, basic->ia, basic->CL,
279*4882a593Smuzhiyun 		      basic->hpp, basic->gpp);
280*4882a593Smuzhiyun 	return true;
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun /* Display s390 CPU measurement facility diagnostic-sampling data entry.
284*4882a593Smuzhiyun  * Data written on s390 in big endian byte order and contains bit
285*4882a593Smuzhiyun  * fields across byte boundaries.
286*4882a593Smuzhiyun  */
s390_cpumsf_diag_show(const char * color,size_t pos,struct hws_diag_entry * diagp)287*4882a593Smuzhiyun static bool s390_cpumsf_diag_show(const char *color, size_t pos,
288*4882a593Smuzhiyun 				  struct hws_diag_entry *diagp)
289*4882a593Smuzhiyun {
290*4882a593Smuzhiyun 	struct hws_diag_entry *diag = diagp;
291*4882a593Smuzhiyun #if __BYTE_ORDER == __LITTLE_ENDIAN
292*4882a593Smuzhiyun 	struct hws_diag_entry local;
293*4882a593Smuzhiyun 	unsigned long long word = be64toh(*(unsigned long long *)diagp);
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 	local.def = be16toh(diagp->def);
296*4882a593Smuzhiyun 	local.I = word >> 32 & 0x1;
297*4882a593Smuzhiyun 	diag = &local;
298*4882a593Smuzhiyun #endif
299*4882a593Smuzhiyun 	if (diag->def < S390_CPUMSF_DIAG_DEF_FIRST) {
300*4882a593Smuzhiyun 		pr_err("Invalid AUX trace diagnostic entry [%#08zx]\n", pos);
301*4882a593Smuzhiyun 		return false;
302*4882a593Smuzhiyun 	}
303*4882a593Smuzhiyun 	color_fprintf(stdout, color, "    [%#08zx] Diag    Def:%04x %c\n",
304*4882a593Smuzhiyun 		      pos, diag->def, diag->I ? 'I' : ' ');
305*4882a593Smuzhiyun 	return true;
306*4882a593Smuzhiyun }
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun /* Return TOD timestamp contained in an trailer entry */
trailer_timestamp(struct hws_trailer_entry * te,int idx)309*4882a593Smuzhiyun static unsigned long long trailer_timestamp(struct hws_trailer_entry *te,
310*4882a593Smuzhiyun 					    int idx)
311*4882a593Smuzhiyun {
312*4882a593Smuzhiyun 	/* te->t set: TOD in STCKE format, bytes 8-15
313*4882a593Smuzhiyun 	 * to->t not set: TOD in STCK format, bytes 0-7
314*4882a593Smuzhiyun 	 */
315*4882a593Smuzhiyun 	unsigned long long ts;
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 	memcpy(&ts, &te->timestamp[idx], sizeof(ts));
318*4882a593Smuzhiyun 	return be64toh(ts);
319*4882a593Smuzhiyun }
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun /* Display s390 CPU measurement facility trailer entry */
s390_cpumsf_trailer_show(const char * color,size_t pos,struct hws_trailer_entry * te)322*4882a593Smuzhiyun static bool s390_cpumsf_trailer_show(const char *color, size_t pos,
323*4882a593Smuzhiyun 				     struct hws_trailer_entry *te)
324*4882a593Smuzhiyun {
325*4882a593Smuzhiyun #if __BYTE_ORDER == __LITTLE_ENDIAN
326*4882a593Smuzhiyun 	struct hws_trailer_entry local;
327*4882a593Smuzhiyun 	const unsigned long long flags = be64toh(te->flags);
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 	memset(&local, 0, sizeof(local));
330*4882a593Smuzhiyun 	local.f = flags >> 63 & 0x1;
331*4882a593Smuzhiyun 	local.a = flags >> 62 & 0x1;
332*4882a593Smuzhiyun 	local.t = flags >> 61 & 0x1;
333*4882a593Smuzhiyun 	local.bsdes = be16toh((flags >> 16 & 0xffff));
334*4882a593Smuzhiyun 	local.dsdes = be16toh((flags & 0xffff));
335*4882a593Smuzhiyun 	memcpy(&local.timestamp, te->timestamp, sizeof(te->timestamp));
336*4882a593Smuzhiyun 	local.overflow = be64toh(te->overflow);
337*4882a593Smuzhiyun 	local.clock_base = be64toh(te->progusage[0]) >> 63 & 1;
338*4882a593Smuzhiyun 	local.progusage2 = be64toh(te->progusage2);
339*4882a593Smuzhiyun 	te = &local;
340*4882a593Smuzhiyun #endif
341*4882a593Smuzhiyun 	if (te->bsdes != sizeof(struct hws_basic_entry)) {
342*4882a593Smuzhiyun 		pr_err("Invalid AUX trace trailer entry [%#08zx]\n", pos);
343*4882a593Smuzhiyun 		return false;
344*4882a593Smuzhiyun 	}
345*4882a593Smuzhiyun 	color_fprintf(stdout, color, "    [%#08zx] Trailer %c%c%c bsdes:%d"
346*4882a593Smuzhiyun 		      " dsdes:%d Overflow:%lld Time:%#llx\n"
347*4882a593Smuzhiyun 		      "\t\tC:%d TOD:%#lx\n",
348*4882a593Smuzhiyun 		      pos,
349*4882a593Smuzhiyun 		      te->f ? 'F' : ' ',
350*4882a593Smuzhiyun 		      te->a ? 'A' : ' ',
351*4882a593Smuzhiyun 		      te->t ? 'T' : ' ',
352*4882a593Smuzhiyun 		      te->bsdes, te->dsdes, te->overflow,
353*4882a593Smuzhiyun 		      trailer_timestamp(te, te->clock_base),
354*4882a593Smuzhiyun 		      te->clock_base, te->progusage2);
355*4882a593Smuzhiyun 	return true;
356*4882a593Smuzhiyun }
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun /* Test a sample data block. It must be 4KB or a multiple thereof in size and
359*4882a593Smuzhiyun  * 4KB page aligned. Each sample data page has a trailer entry at the
360*4882a593Smuzhiyun  * end which contains the sample entry data sizes.
361*4882a593Smuzhiyun  *
362*4882a593Smuzhiyun  * Return true if the sample data block passes the checks and set the
363*4882a593Smuzhiyun  * basic set entry size and diagnostic set entry size.
364*4882a593Smuzhiyun  *
365*4882a593Smuzhiyun  * Return false on failure.
366*4882a593Smuzhiyun  *
367*4882a593Smuzhiyun  * Note: Old hardware does not set the basic or diagnostic entry sizes
368*4882a593Smuzhiyun  * in the trailer entry. Use the type number instead.
369*4882a593Smuzhiyun  */
s390_cpumsf_validate(int machine_type,unsigned char * buf,size_t len,unsigned short * bsdes,unsigned short * dsdes)370*4882a593Smuzhiyun static bool s390_cpumsf_validate(int machine_type,
371*4882a593Smuzhiyun 				 unsigned char *buf, size_t len,
372*4882a593Smuzhiyun 				 unsigned short *bsdes,
373*4882a593Smuzhiyun 				 unsigned short *dsdes)
374*4882a593Smuzhiyun {
375*4882a593Smuzhiyun 	struct hws_basic_entry *basic = (struct hws_basic_entry *)buf;
376*4882a593Smuzhiyun 	struct hws_trailer_entry *te;
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun 	*dsdes = *bsdes = 0;
379*4882a593Smuzhiyun 	if (len & (S390_CPUMSF_PAGESZ - 1))	/* Illegal size */
380*4882a593Smuzhiyun 		return false;
381*4882a593Smuzhiyun 	if (be16toh(basic->def) != 1)	/* No basic set entry, must be first */
382*4882a593Smuzhiyun 		return false;
383*4882a593Smuzhiyun 	/* Check for trailer entry at end of SDB */
384*4882a593Smuzhiyun 	te = (struct hws_trailer_entry *)(buf + S390_CPUMSF_PAGESZ
385*4882a593Smuzhiyun 					      - sizeof(*te));
386*4882a593Smuzhiyun 	*bsdes = be16toh(te->bsdes);
387*4882a593Smuzhiyun 	*dsdes = be16toh(te->dsdes);
388*4882a593Smuzhiyun 	if (!te->bsdes && !te->dsdes) {
389*4882a593Smuzhiyun 		/* Very old hardware, use CPUID */
390*4882a593Smuzhiyun 		switch (machine_type) {
391*4882a593Smuzhiyun 		case 2097:
392*4882a593Smuzhiyun 		case 2098:
393*4882a593Smuzhiyun 			*dsdes = 64;
394*4882a593Smuzhiyun 			*bsdes = 32;
395*4882a593Smuzhiyun 			break;
396*4882a593Smuzhiyun 		case 2817:
397*4882a593Smuzhiyun 		case 2818:
398*4882a593Smuzhiyun 			*dsdes = 74;
399*4882a593Smuzhiyun 			*bsdes = 32;
400*4882a593Smuzhiyun 			break;
401*4882a593Smuzhiyun 		case 2827:
402*4882a593Smuzhiyun 		case 2828:
403*4882a593Smuzhiyun 			*dsdes = 85;
404*4882a593Smuzhiyun 			*bsdes = 32;
405*4882a593Smuzhiyun 			break;
406*4882a593Smuzhiyun 		case 2964:
407*4882a593Smuzhiyun 		case 2965:
408*4882a593Smuzhiyun 			*dsdes = 112;
409*4882a593Smuzhiyun 			*bsdes = 32;
410*4882a593Smuzhiyun 			break;
411*4882a593Smuzhiyun 		default:
412*4882a593Smuzhiyun 			/* Illegal trailer entry */
413*4882a593Smuzhiyun 			return false;
414*4882a593Smuzhiyun 		}
415*4882a593Smuzhiyun 	}
416*4882a593Smuzhiyun 	return true;
417*4882a593Smuzhiyun }
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun /* Return true if there is room for another entry */
s390_cpumsf_reached_trailer(size_t entry_sz,size_t pos)420*4882a593Smuzhiyun static bool s390_cpumsf_reached_trailer(size_t entry_sz, size_t pos)
421*4882a593Smuzhiyun {
422*4882a593Smuzhiyun 	size_t payload = S390_CPUMSF_PAGESZ - sizeof(struct hws_trailer_entry);
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun 	if (payload - (pos & (S390_CPUMSF_PAGESZ - 1)) < entry_sz)
425*4882a593Smuzhiyun 		return false;
426*4882a593Smuzhiyun 	return true;
427*4882a593Smuzhiyun }
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun /* Dump an auxiliary buffer. These buffers are multiple of
430*4882a593Smuzhiyun  * 4KB SDB pages.
431*4882a593Smuzhiyun  */
s390_cpumsf_dump(struct s390_cpumsf * sf,unsigned char * buf,size_t len)432*4882a593Smuzhiyun static void s390_cpumsf_dump(struct s390_cpumsf *sf,
433*4882a593Smuzhiyun 			     unsigned char *buf, size_t len)
434*4882a593Smuzhiyun {
435*4882a593Smuzhiyun 	const char *color = PERF_COLOR_BLUE;
436*4882a593Smuzhiyun 	struct hws_basic_entry *basic;
437*4882a593Smuzhiyun 	struct hws_diag_entry *diag;
438*4882a593Smuzhiyun 	unsigned short bsdes, dsdes;
439*4882a593Smuzhiyun 	size_t pos = 0;
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun 	color_fprintf(stdout, color,
442*4882a593Smuzhiyun 		      ". ... s390 AUX data: size %zu bytes\n",
443*4882a593Smuzhiyun 		      len);
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun 	if (!s390_cpumsf_validate(sf->machine_type, buf, len, &bsdes,
446*4882a593Smuzhiyun 				  &dsdes)) {
447*4882a593Smuzhiyun 		pr_err("Invalid AUX trace data block size:%zu"
448*4882a593Smuzhiyun 		       " (type:%d bsdes:%hd dsdes:%hd)\n",
449*4882a593Smuzhiyun 		       len, sf->machine_type, bsdes, dsdes);
450*4882a593Smuzhiyun 		return;
451*4882a593Smuzhiyun 	}
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun 	/* s390 kernel always returns 4KB blocks fully occupied,
454*4882a593Smuzhiyun 	 * no partially filled SDBs.
455*4882a593Smuzhiyun 	 */
456*4882a593Smuzhiyun 	while (pos < len) {
457*4882a593Smuzhiyun 		/* Handle Basic entry */
458*4882a593Smuzhiyun 		basic = (struct hws_basic_entry *)(buf + pos);
459*4882a593Smuzhiyun 		if (s390_cpumsf_basic_show(color, pos, basic))
460*4882a593Smuzhiyun 			pos += bsdes;
461*4882a593Smuzhiyun 		else
462*4882a593Smuzhiyun 			return;
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun 		/* Handle Diagnostic entry */
465*4882a593Smuzhiyun 		diag = (struct hws_diag_entry *)(buf + pos);
466*4882a593Smuzhiyun 		if (s390_cpumsf_diag_show(color, pos, diag))
467*4882a593Smuzhiyun 			pos += dsdes;
468*4882a593Smuzhiyun 		else
469*4882a593Smuzhiyun 			return;
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun 		/* Check for trailer entry */
472*4882a593Smuzhiyun 		if (!s390_cpumsf_reached_trailer(bsdes + dsdes, pos)) {
473*4882a593Smuzhiyun 			/* Show trailer entry */
474*4882a593Smuzhiyun 			struct hws_trailer_entry te;
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun 			pos = (pos + S390_CPUMSF_PAGESZ)
477*4882a593Smuzhiyun 			       & ~(S390_CPUMSF_PAGESZ - 1);
478*4882a593Smuzhiyun 			pos -= sizeof(te);
479*4882a593Smuzhiyun 			memcpy(&te, buf + pos, sizeof(te));
480*4882a593Smuzhiyun 			/* Set descriptor sizes in case of old hardware
481*4882a593Smuzhiyun 			 * where these values are not set.
482*4882a593Smuzhiyun 			 */
483*4882a593Smuzhiyun 			te.bsdes = bsdes;
484*4882a593Smuzhiyun 			te.dsdes = dsdes;
485*4882a593Smuzhiyun 			if (s390_cpumsf_trailer_show(color, pos, &te))
486*4882a593Smuzhiyun 				pos += sizeof(te);
487*4882a593Smuzhiyun 			else
488*4882a593Smuzhiyun 				return;
489*4882a593Smuzhiyun 		}
490*4882a593Smuzhiyun 	}
491*4882a593Smuzhiyun }
492*4882a593Smuzhiyun 
s390_cpumsf_dump_event(struct s390_cpumsf * sf,unsigned char * buf,size_t len)493*4882a593Smuzhiyun static void s390_cpumsf_dump_event(struct s390_cpumsf *sf, unsigned char *buf,
494*4882a593Smuzhiyun 				   size_t len)
495*4882a593Smuzhiyun {
496*4882a593Smuzhiyun 	printf(".\n");
497*4882a593Smuzhiyun 	s390_cpumsf_dump(sf, buf, len);
498*4882a593Smuzhiyun }
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun #define	S390_LPP_PID_MASK	0xffffffff
501*4882a593Smuzhiyun 
s390_cpumsf_make_event(size_t pos,struct hws_basic_entry * basic,struct s390_cpumsf_queue * sfq)502*4882a593Smuzhiyun static bool s390_cpumsf_make_event(size_t pos,
503*4882a593Smuzhiyun 				   struct hws_basic_entry *basic,
504*4882a593Smuzhiyun 				   struct s390_cpumsf_queue *sfq)
505*4882a593Smuzhiyun {
506*4882a593Smuzhiyun 	struct perf_sample sample = {
507*4882a593Smuzhiyun 				.ip = basic->ia,
508*4882a593Smuzhiyun 				.pid = basic->hpp & S390_LPP_PID_MASK,
509*4882a593Smuzhiyun 				.tid = basic->hpp & S390_LPP_PID_MASK,
510*4882a593Smuzhiyun 				.cpumode = PERF_RECORD_MISC_CPUMODE_UNKNOWN,
511*4882a593Smuzhiyun 				.cpu = sfq->cpu,
512*4882a593Smuzhiyun 				.period = 1
513*4882a593Smuzhiyun 			    };
514*4882a593Smuzhiyun 	union perf_event event;
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun 	memset(&event, 0, sizeof(event));
517*4882a593Smuzhiyun 	if (basic->CL == 1)	/* Native LPAR mode */
518*4882a593Smuzhiyun 		sample.cpumode = basic->P ? PERF_RECORD_MISC_USER
519*4882a593Smuzhiyun 					  : PERF_RECORD_MISC_KERNEL;
520*4882a593Smuzhiyun 	else if (basic->CL == 2)	/* Guest kernel/user space */
521*4882a593Smuzhiyun 		sample.cpumode = basic->P ? PERF_RECORD_MISC_GUEST_USER
522*4882a593Smuzhiyun 					  : PERF_RECORD_MISC_GUEST_KERNEL;
523*4882a593Smuzhiyun 	else if (basic->gpp || basic->prim_asn != 0xffff)
524*4882a593Smuzhiyun 		/* Use heuristics on old hardware */
525*4882a593Smuzhiyun 		sample.cpumode = basic->P ? PERF_RECORD_MISC_GUEST_USER
526*4882a593Smuzhiyun 					  : PERF_RECORD_MISC_GUEST_KERNEL;
527*4882a593Smuzhiyun 	else
528*4882a593Smuzhiyun 		sample.cpumode = basic->P ? PERF_RECORD_MISC_USER
529*4882a593Smuzhiyun 					  : PERF_RECORD_MISC_KERNEL;
530*4882a593Smuzhiyun 
531*4882a593Smuzhiyun 	event.sample.header.type = PERF_RECORD_SAMPLE;
532*4882a593Smuzhiyun 	event.sample.header.misc = sample.cpumode;
533*4882a593Smuzhiyun 	event.sample.header.size = sizeof(struct perf_event_header);
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun 	pr_debug4("%s pos:%#zx ip:%#" PRIx64 " P:%d CL:%d pid:%d.%d cpumode:%d cpu:%d\n",
536*4882a593Smuzhiyun 		 __func__, pos, sample.ip, basic->P, basic->CL, sample.pid,
537*4882a593Smuzhiyun 		 sample.tid, sample.cpumode, sample.cpu);
538*4882a593Smuzhiyun 	if (perf_session__deliver_synth_event(sfq->sf->session, &event,
539*4882a593Smuzhiyun 					      &sample)) {
540*4882a593Smuzhiyun 		pr_err("s390 Auxiliary Trace: failed to deliver event\n");
541*4882a593Smuzhiyun 		return false;
542*4882a593Smuzhiyun 	}
543*4882a593Smuzhiyun 	return true;
544*4882a593Smuzhiyun }
545*4882a593Smuzhiyun 
get_trailer_time(const unsigned char * buf)546*4882a593Smuzhiyun static unsigned long long get_trailer_time(const unsigned char *buf)
547*4882a593Smuzhiyun {
548*4882a593Smuzhiyun 	struct hws_trailer_entry *te;
549*4882a593Smuzhiyun 	unsigned long long aux_time, progusage2;
550*4882a593Smuzhiyun 	bool clock_base;
551*4882a593Smuzhiyun 
552*4882a593Smuzhiyun 	te = (struct hws_trailer_entry *)(buf + S390_CPUMSF_PAGESZ
553*4882a593Smuzhiyun 					      - sizeof(*te));
554*4882a593Smuzhiyun 
555*4882a593Smuzhiyun #if __BYTE_ORDER == __LITTLE_ENDIAN
556*4882a593Smuzhiyun 	clock_base = be64toh(te->progusage[0]) >> 63 & 0x1;
557*4882a593Smuzhiyun 	progusage2 = be64toh(te->progusage[1]);
558*4882a593Smuzhiyun #else
559*4882a593Smuzhiyun 	clock_base = te->clock_base;
560*4882a593Smuzhiyun 	progusage2 = te->progusage2;
561*4882a593Smuzhiyun #endif
562*4882a593Smuzhiyun 	if (!clock_base)	/* TOD_CLOCK_BASE value missing */
563*4882a593Smuzhiyun 		return 0;
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun 	/* Correct calculation to convert time stamp in trailer entry to
566*4882a593Smuzhiyun 	 * nano seconds (taken from arch/s390 function tod_to_ns()).
567*4882a593Smuzhiyun 	 * TOD_CLOCK_BASE is stored in trailer entry member progusage2.
568*4882a593Smuzhiyun 	 */
569*4882a593Smuzhiyun 	aux_time = trailer_timestamp(te, clock_base) - progusage2;
570*4882a593Smuzhiyun 	aux_time = (aux_time >> 9) * 125 + (((aux_time & 0x1ff) * 125) >> 9);
571*4882a593Smuzhiyun 	return aux_time;
572*4882a593Smuzhiyun }
573*4882a593Smuzhiyun 
574*4882a593Smuzhiyun /* Process the data samples of a single queue. The first parameter is a
575*4882a593Smuzhiyun  * pointer to the queue, the second parameter is the time stamp. This
576*4882a593Smuzhiyun  * is the time stamp:
577*4882a593Smuzhiyun  * - of the event that triggered this processing.
578*4882a593Smuzhiyun  * - or the time stamp when the last proccesing of this queue stopped.
579*4882a593Smuzhiyun  *   In this case it stopped at a 4KB page boundary and record the
580*4882a593Smuzhiyun  *   position on where to continue processing on the next invocation
581*4882a593Smuzhiyun  *   (see buffer->use_data and buffer->use_size).
582*4882a593Smuzhiyun  *
583*4882a593Smuzhiyun  * When this function returns the second parameter is updated to
584*4882a593Smuzhiyun  * reflect the time stamp of the last processed auxiliary data entry
585*4882a593Smuzhiyun  * (taken from the trailer entry of that page). The caller uses this
586*4882a593Smuzhiyun  * returned time stamp to record the last processed entry in this
587*4882a593Smuzhiyun  * queue.
588*4882a593Smuzhiyun  *
589*4882a593Smuzhiyun  * The function returns:
590*4882a593Smuzhiyun  * 0:  Processing successful. The second parameter returns the
591*4882a593Smuzhiyun  *     time stamp from the trailer entry until which position
592*4882a593Smuzhiyun  *     processing took place. Subsequent calls resume from this
593*4882a593Smuzhiyun  *     position.
594*4882a593Smuzhiyun  * <0: An error occurred during processing. The second parameter
595*4882a593Smuzhiyun  *     returns the maximum time stamp.
596*4882a593Smuzhiyun  * >0: Done on this queue. The second parameter returns the
597*4882a593Smuzhiyun  *     maximum time stamp.
598*4882a593Smuzhiyun  */
s390_cpumsf_samples(struct s390_cpumsf_queue * sfq,u64 * ts)599*4882a593Smuzhiyun static int s390_cpumsf_samples(struct s390_cpumsf_queue *sfq, u64 *ts)
600*4882a593Smuzhiyun {
601*4882a593Smuzhiyun 	struct s390_cpumsf *sf = sfq->sf;
602*4882a593Smuzhiyun 	unsigned char *buf = sfq->buffer->use_data;
603*4882a593Smuzhiyun 	size_t len = sfq->buffer->use_size;
604*4882a593Smuzhiyun 	struct hws_basic_entry *basic;
605*4882a593Smuzhiyun 	unsigned short bsdes, dsdes;
606*4882a593Smuzhiyun 	size_t pos = 0;
607*4882a593Smuzhiyun 	int err = 1;
608*4882a593Smuzhiyun 	u64 aux_ts;
609*4882a593Smuzhiyun 
610*4882a593Smuzhiyun 	if (!s390_cpumsf_validate(sf->machine_type, buf, len, &bsdes,
611*4882a593Smuzhiyun 				  &dsdes)) {
612*4882a593Smuzhiyun 		*ts = ~0ULL;
613*4882a593Smuzhiyun 		return -1;
614*4882a593Smuzhiyun 	}
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun 	/* Get trailer entry time stamp and check if entries in
617*4882a593Smuzhiyun 	 * this auxiliary page are ready for processing. If the
618*4882a593Smuzhiyun 	 * time stamp of the first entry is too high, whole buffer
619*4882a593Smuzhiyun 	 * can be skipped. In this case return time stamp.
620*4882a593Smuzhiyun 	 */
621*4882a593Smuzhiyun 	aux_ts = get_trailer_time(buf);
622*4882a593Smuzhiyun 	if (!aux_ts) {
623*4882a593Smuzhiyun 		pr_err("[%#08" PRIx64 "] Invalid AUX trailer entry TOD clock base\n",
624*4882a593Smuzhiyun 		       (s64)sfq->buffer->data_offset);
625*4882a593Smuzhiyun 		aux_ts = ~0ULL;
626*4882a593Smuzhiyun 		goto out;
627*4882a593Smuzhiyun 	}
628*4882a593Smuzhiyun 	if (aux_ts > *ts) {
629*4882a593Smuzhiyun 		*ts = aux_ts;
630*4882a593Smuzhiyun 		return 0;
631*4882a593Smuzhiyun 	}
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun 	while (pos < len) {
634*4882a593Smuzhiyun 		/* Handle Basic entry */
635*4882a593Smuzhiyun 		basic = (struct hws_basic_entry *)(buf + pos);
636*4882a593Smuzhiyun 		if (s390_cpumsf_make_event(pos, basic, sfq))
637*4882a593Smuzhiyun 			pos += bsdes;
638*4882a593Smuzhiyun 		else {
639*4882a593Smuzhiyun 			err = -EBADF;
640*4882a593Smuzhiyun 			goto out;
641*4882a593Smuzhiyun 		}
642*4882a593Smuzhiyun 
643*4882a593Smuzhiyun 		pos += dsdes;	/* Skip diagnositic entry */
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun 		/* Check for trailer entry */
646*4882a593Smuzhiyun 		if (!s390_cpumsf_reached_trailer(bsdes + dsdes, pos)) {
647*4882a593Smuzhiyun 			pos = (pos + S390_CPUMSF_PAGESZ)
648*4882a593Smuzhiyun 			       & ~(S390_CPUMSF_PAGESZ - 1);
649*4882a593Smuzhiyun 			/* Check existence of next page */
650*4882a593Smuzhiyun 			if (pos >= len)
651*4882a593Smuzhiyun 				break;
652*4882a593Smuzhiyun 			aux_ts = get_trailer_time(buf + pos);
653*4882a593Smuzhiyun 			if (!aux_ts) {
654*4882a593Smuzhiyun 				aux_ts = ~0ULL;
655*4882a593Smuzhiyun 				goto out;
656*4882a593Smuzhiyun 			}
657*4882a593Smuzhiyun 			if (aux_ts > *ts) {
658*4882a593Smuzhiyun 				*ts = aux_ts;
659*4882a593Smuzhiyun 				sfq->buffer->use_data += pos;
660*4882a593Smuzhiyun 				sfq->buffer->use_size -= pos;
661*4882a593Smuzhiyun 				return 0;
662*4882a593Smuzhiyun 			}
663*4882a593Smuzhiyun 		}
664*4882a593Smuzhiyun 	}
665*4882a593Smuzhiyun out:
666*4882a593Smuzhiyun 	*ts = aux_ts;
667*4882a593Smuzhiyun 	sfq->buffer->use_size = 0;
668*4882a593Smuzhiyun 	sfq->buffer->use_data = NULL;
669*4882a593Smuzhiyun 	return err;	/* Buffer completely scanned or error */
670*4882a593Smuzhiyun }
671*4882a593Smuzhiyun 
672*4882a593Smuzhiyun /* Run the s390 auxiliary trace decoder.
673*4882a593Smuzhiyun  * Select the queue buffer to operate on, the caller already selected
674*4882a593Smuzhiyun  * the proper queue, depending on second parameter 'ts'.
675*4882a593Smuzhiyun  * This is the time stamp until which the auxiliary entries should
676*4882a593Smuzhiyun  * be processed. This value is updated by called functions and
677*4882a593Smuzhiyun  * returned to the caller.
678*4882a593Smuzhiyun  *
679*4882a593Smuzhiyun  * Resume processing in the current buffer. If there is no buffer
680*4882a593Smuzhiyun  * get a new buffer from the queue and setup start position for
681*4882a593Smuzhiyun  * processing.
682*4882a593Smuzhiyun  * When a buffer is completely processed remove it from the queue
683*4882a593Smuzhiyun  * before returning.
684*4882a593Smuzhiyun  *
685*4882a593Smuzhiyun  * This function returns
686*4882a593Smuzhiyun  * 1: When the queue is empty. Second parameter will be set to
687*4882a593Smuzhiyun  *    maximum time stamp.
688*4882a593Smuzhiyun  * 0: Normal processing done.
689*4882a593Smuzhiyun  * <0: Error during queue buffer setup. This causes the caller
690*4882a593Smuzhiyun  *     to stop processing completely.
691*4882a593Smuzhiyun  */
s390_cpumsf_run_decoder(struct s390_cpumsf_queue * sfq,u64 * ts)692*4882a593Smuzhiyun static int s390_cpumsf_run_decoder(struct s390_cpumsf_queue *sfq,
693*4882a593Smuzhiyun 				   u64 *ts)
694*4882a593Smuzhiyun {
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun 	struct auxtrace_buffer *buffer;
697*4882a593Smuzhiyun 	struct auxtrace_queue *queue;
698*4882a593Smuzhiyun 	int err;
699*4882a593Smuzhiyun 
700*4882a593Smuzhiyun 	queue = &sfq->sf->queues.queue_array[sfq->queue_nr];
701*4882a593Smuzhiyun 
702*4882a593Smuzhiyun 	/* Get buffer and last position in buffer to resume
703*4882a593Smuzhiyun 	 * decoding the auxiliary entries. One buffer might be large
704*4882a593Smuzhiyun 	 * and decoding might stop in between. This depends on the time
705*4882a593Smuzhiyun 	 * stamp of the trailer entry in each page of the auxiliary
706*4882a593Smuzhiyun 	 * data and the time stamp of the event triggering the decoding.
707*4882a593Smuzhiyun 	 */
708*4882a593Smuzhiyun 	if (sfq->buffer == NULL) {
709*4882a593Smuzhiyun 		sfq->buffer = buffer = auxtrace_buffer__next(queue,
710*4882a593Smuzhiyun 							     sfq->buffer);
711*4882a593Smuzhiyun 		if (!buffer) {
712*4882a593Smuzhiyun 			*ts = ~0ULL;
713*4882a593Smuzhiyun 			return 1;	/* Processing done on this queue */
714*4882a593Smuzhiyun 		}
715*4882a593Smuzhiyun 		/* Start with a new buffer on this queue */
716*4882a593Smuzhiyun 		if (buffer->data) {
717*4882a593Smuzhiyun 			buffer->use_size = buffer->size;
718*4882a593Smuzhiyun 			buffer->use_data = buffer->data;
719*4882a593Smuzhiyun 		}
720*4882a593Smuzhiyun 		if (sfq->logfile) {	/* Write into log file */
721*4882a593Smuzhiyun 			size_t rc = fwrite(buffer->data, buffer->size, 1,
722*4882a593Smuzhiyun 					   sfq->logfile);
723*4882a593Smuzhiyun 			if (rc != 1)
724*4882a593Smuzhiyun 				pr_err("Failed to write auxiliary data\n");
725*4882a593Smuzhiyun 		}
726*4882a593Smuzhiyun 	} else
727*4882a593Smuzhiyun 		buffer = sfq->buffer;
728*4882a593Smuzhiyun 
729*4882a593Smuzhiyun 	if (!buffer->data) {
730*4882a593Smuzhiyun 		int fd = perf_data__fd(sfq->sf->session->data);
731*4882a593Smuzhiyun 
732*4882a593Smuzhiyun 		buffer->data = auxtrace_buffer__get_data(buffer, fd);
733*4882a593Smuzhiyun 		if (!buffer->data)
734*4882a593Smuzhiyun 			return -ENOMEM;
735*4882a593Smuzhiyun 		buffer->use_size = buffer->size;
736*4882a593Smuzhiyun 		buffer->use_data = buffer->data;
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun 		if (sfq->logfile) {	/* Write into log file */
739*4882a593Smuzhiyun 			size_t rc = fwrite(buffer->data, buffer->size, 1,
740*4882a593Smuzhiyun 					   sfq->logfile);
741*4882a593Smuzhiyun 			if (rc != 1)
742*4882a593Smuzhiyun 				pr_err("Failed to write auxiliary data\n");
743*4882a593Smuzhiyun 		}
744*4882a593Smuzhiyun 	}
745*4882a593Smuzhiyun 	pr_debug4("%s queue_nr:%d buffer:%" PRId64 " offset:%#" PRIx64 " size:%#zx rest:%#zx\n",
746*4882a593Smuzhiyun 		  __func__, sfq->queue_nr, buffer->buffer_nr, buffer->offset,
747*4882a593Smuzhiyun 		  buffer->size, buffer->use_size);
748*4882a593Smuzhiyun 	err = s390_cpumsf_samples(sfq, ts);
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun 	/* If non-zero, there is either an error (err < 0) or the buffer is
751*4882a593Smuzhiyun 	 * completely done (err > 0). The error is unrecoverable, usually
752*4882a593Smuzhiyun 	 * some descriptors could not be read successfully, so continue with
753*4882a593Smuzhiyun 	 * the next buffer.
754*4882a593Smuzhiyun 	 * In both cases the parameter 'ts' has been updated.
755*4882a593Smuzhiyun 	 */
756*4882a593Smuzhiyun 	if (err) {
757*4882a593Smuzhiyun 		sfq->buffer = NULL;
758*4882a593Smuzhiyun 		list_del_init(&buffer->list);
759*4882a593Smuzhiyun 		auxtrace_buffer__free(buffer);
760*4882a593Smuzhiyun 		if (err > 0)		/* Buffer done, no error */
761*4882a593Smuzhiyun 			err = 0;
762*4882a593Smuzhiyun 	}
763*4882a593Smuzhiyun 	return err;
764*4882a593Smuzhiyun }
765*4882a593Smuzhiyun 
766*4882a593Smuzhiyun static struct s390_cpumsf_queue *
s390_cpumsf_alloc_queue(struct s390_cpumsf * sf,unsigned int queue_nr)767*4882a593Smuzhiyun s390_cpumsf_alloc_queue(struct s390_cpumsf *sf, unsigned int queue_nr)
768*4882a593Smuzhiyun {
769*4882a593Smuzhiyun 	struct s390_cpumsf_queue *sfq;
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun 	sfq = zalloc(sizeof(struct s390_cpumsf_queue));
772*4882a593Smuzhiyun 	if (sfq == NULL)
773*4882a593Smuzhiyun 		return NULL;
774*4882a593Smuzhiyun 
775*4882a593Smuzhiyun 	sfq->sf = sf;
776*4882a593Smuzhiyun 	sfq->queue_nr = queue_nr;
777*4882a593Smuzhiyun 	sfq->cpu = -1;
778*4882a593Smuzhiyun 	if (sf->use_logfile) {
779*4882a593Smuzhiyun 		char *name;
780*4882a593Smuzhiyun 		int rc;
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun 		rc = (sf->logdir)
783*4882a593Smuzhiyun 			? asprintf(&name, "%s/aux.smp.%02x",
784*4882a593Smuzhiyun 				 sf->logdir, queue_nr)
785*4882a593Smuzhiyun 			: asprintf(&name, "aux.smp.%02x", queue_nr);
786*4882a593Smuzhiyun 		if (rc > 0)
787*4882a593Smuzhiyun 			sfq->logfile = fopen(name, "w");
788*4882a593Smuzhiyun 		if (sfq->logfile == NULL) {
789*4882a593Smuzhiyun 			pr_err("Failed to open auxiliary log file %s,"
790*4882a593Smuzhiyun 			       "continue...\n", name);
791*4882a593Smuzhiyun 			sf->use_logfile = false;
792*4882a593Smuzhiyun 		}
793*4882a593Smuzhiyun 		free(name);
794*4882a593Smuzhiyun 	}
795*4882a593Smuzhiyun 	return sfq;
796*4882a593Smuzhiyun }
797*4882a593Smuzhiyun 
s390_cpumsf_setup_queue(struct s390_cpumsf * sf,struct auxtrace_queue * queue,unsigned int queue_nr,u64 ts)798*4882a593Smuzhiyun static int s390_cpumsf_setup_queue(struct s390_cpumsf *sf,
799*4882a593Smuzhiyun 				   struct auxtrace_queue *queue,
800*4882a593Smuzhiyun 				   unsigned int queue_nr, u64 ts)
801*4882a593Smuzhiyun {
802*4882a593Smuzhiyun 	struct s390_cpumsf_queue *sfq = queue->priv;
803*4882a593Smuzhiyun 
804*4882a593Smuzhiyun 	if (list_empty(&queue->head))
805*4882a593Smuzhiyun 		return 0;
806*4882a593Smuzhiyun 
807*4882a593Smuzhiyun 	if (sfq == NULL) {
808*4882a593Smuzhiyun 		sfq = s390_cpumsf_alloc_queue(sf, queue_nr);
809*4882a593Smuzhiyun 		if (!sfq)
810*4882a593Smuzhiyun 			return -ENOMEM;
811*4882a593Smuzhiyun 		queue->priv = sfq;
812*4882a593Smuzhiyun 
813*4882a593Smuzhiyun 		if (queue->cpu != -1)
814*4882a593Smuzhiyun 			sfq->cpu = queue->cpu;
815*4882a593Smuzhiyun 	}
816*4882a593Smuzhiyun 	return auxtrace_heap__add(&sf->heap, queue_nr, ts);
817*4882a593Smuzhiyun }
818*4882a593Smuzhiyun 
s390_cpumsf_setup_queues(struct s390_cpumsf * sf,u64 ts)819*4882a593Smuzhiyun static int s390_cpumsf_setup_queues(struct s390_cpumsf *sf, u64 ts)
820*4882a593Smuzhiyun {
821*4882a593Smuzhiyun 	unsigned int i;
822*4882a593Smuzhiyun 	int ret = 0;
823*4882a593Smuzhiyun 
824*4882a593Smuzhiyun 	for (i = 0; i < sf->queues.nr_queues; i++) {
825*4882a593Smuzhiyun 		ret = s390_cpumsf_setup_queue(sf, &sf->queues.queue_array[i],
826*4882a593Smuzhiyun 					      i, ts);
827*4882a593Smuzhiyun 		if (ret)
828*4882a593Smuzhiyun 			break;
829*4882a593Smuzhiyun 	}
830*4882a593Smuzhiyun 	return ret;
831*4882a593Smuzhiyun }
832*4882a593Smuzhiyun 
s390_cpumsf_update_queues(struct s390_cpumsf * sf,u64 ts)833*4882a593Smuzhiyun static int s390_cpumsf_update_queues(struct s390_cpumsf *sf, u64 ts)
834*4882a593Smuzhiyun {
835*4882a593Smuzhiyun 	if (!sf->queues.new_data)
836*4882a593Smuzhiyun 		return 0;
837*4882a593Smuzhiyun 
838*4882a593Smuzhiyun 	sf->queues.new_data = false;
839*4882a593Smuzhiyun 	return s390_cpumsf_setup_queues(sf, ts);
840*4882a593Smuzhiyun }
841*4882a593Smuzhiyun 
s390_cpumsf_process_queues(struct s390_cpumsf * sf,u64 timestamp)842*4882a593Smuzhiyun static int s390_cpumsf_process_queues(struct s390_cpumsf *sf, u64 timestamp)
843*4882a593Smuzhiyun {
844*4882a593Smuzhiyun 	unsigned int queue_nr;
845*4882a593Smuzhiyun 	u64 ts;
846*4882a593Smuzhiyun 	int ret;
847*4882a593Smuzhiyun 
848*4882a593Smuzhiyun 	while (1) {
849*4882a593Smuzhiyun 		struct auxtrace_queue *queue;
850*4882a593Smuzhiyun 		struct s390_cpumsf_queue *sfq;
851*4882a593Smuzhiyun 
852*4882a593Smuzhiyun 		if (!sf->heap.heap_cnt)
853*4882a593Smuzhiyun 			return 0;
854*4882a593Smuzhiyun 
855*4882a593Smuzhiyun 		if (sf->heap.heap_array[0].ordinal >= timestamp)
856*4882a593Smuzhiyun 			return 0;
857*4882a593Smuzhiyun 
858*4882a593Smuzhiyun 		queue_nr = sf->heap.heap_array[0].queue_nr;
859*4882a593Smuzhiyun 		queue = &sf->queues.queue_array[queue_nr];
860*4882a593Smuzhiyun 		sfq = queue->priv;
861*4882a593Smuzhiyun 
862*4882a593Smuzhiyun 		auxtrace_heap__pop(&sf->heap);
863*4882a593Smuzhiyun 		if (sf->heap.heap_cnt) {
864*4882a593Smuzhiyun 			ts = sf->heap.heap_array[0].ordinal + 1;
865*4882a593Smuzhiyun 			if (ts > timestamp)
866*4882a593Smuzhiyun 				ts = timestamp;
867*4882a593Smuzhiyun 		} else {
868*4882a593Smuzhiyun 			ts = timestamp;
869*4882a593Smuzhiyun 		}
870*4882a593Smuzhiyun 
871*4882a593Smuzhiyun 		ret = s390_cpumsf_run_decoder(sfq, &ts);
872*4882a593Smuzhiyun 		if (ret < 0) {
873*4882a593Smuzhiyun 			auxtrace_heap__add(&sf->heap, queue_nr, ts);
874*4882a593Smuzhiyun 			return ret;
875*4882a593Smuzhiyun 		}
876*4882a593Smuzhiyun 		if (!ret) {
877*4882a593Smuzhiyun 			ret = auxtrace_heap__add(&sf->heap, queue_nr, ts);
878*4882a593Smuzhiyun 			if (ret < 0)
879*4882a593Smuzhiyun 				return ret;
880*4882a593Smuzhiyun 		}
881*4882a593Smuzhiyun 	}
882*4882a593Smuzhiyun 	return 0;
883*4882a593Smuzhiyun }
884*4882a593Smuzhiyun 
s390_cpumsf_synth_error(struct s390_cpumsf * sf,int code,int cpu,pid_t pid,pid_t tid,u64 ip,u64 timestamp)885*4882a593Smuzhiyun static int s390_cpumsf_synth_error(struct s390_cpumsf *sf, int code, int cpu,
886*4882a593Smuzhiyun 				   pid_t pid, pid_t tid, u64 ip, u64 timestamp)
887*4882a593Smuzhiyun {
888*4882a593Smuzhiyun 	char msg[MAX_AUXTRACE_ERROR_MSG];
889*4882a593Smuzhiyun 	union perf_event event;
890*4882a593Smuzhiyun 	int err;
891*4882a593Smuzhiyun 
892*4882a593Smuzhiyun 	strncpy(msg, "Lost Auxiliary Trace Buffer", sizeof(msg) - 1);
893*4882a593Smuzhiyun 	auxtrace_synth_error(&event.auxtrace_error, PERF_AUXTRACE_ERROR_ITRACE,
894*4882a593Smuzhiyun 			     code, cpu, pid, tid, ip, msg, timestamp);
895*4882a593Smuzhiyun 
896*4882a593Smuzhiyun 	err = perf_session__deliver_synth_event(sf->session, &event, NULL);
897*4882a593Smuzhiyun 	if (err)
898*4882a593Smuzhiyun 		pr_err("s390 Auxiliary Trace: failed to deliver error event,"
899*4882a593Smuzhiyun 			"error %d\n", err);
900*4882a593Smuzhiyun 	return err;
901*4882a593Smuzhiyun }
902*4882a593Smuzhiyun 
s390_cpumsf_lost(struct s390_cpumsf * sf,struct perf_sample * sample)903*4882a593Smuzhiyun static int s390_cpumsf_lost(struct s390_cpumsf *sf, struct perf_sample *sample)
904*4882a593Smuzhiyun {
905*4882a593Smuzhiyun 	return s390_cpumsf_synth_error(sf, 1, sample->cpu,
906*4882a593Smuzhiyun 				       sample->pid, sample->tid, 0,
907*4882a593Smuzhiyun 				       sample->time);
908*4882a593Smuzhiyun }
909*4882a593Smuzhiyun 
910*4882a593Smuzhiyun static int
s390_cpumsf_process_event(struct perf_session * session,union perf_event * event,struct perf_sample * sample,struct perf_tool * tool)911*4882a593Smuzhiyun s390_cpumsf_process_event(struct perf_session *session,
912*4882a593Smuzhiyun 			  union perf_event *event,
913*4882a593Smuzhiyun 			  struct perf_sample *sample,
914*4882a593Smuzhiyun 			  struct perf_tool *tool)
915*4882a593Smuzhiyun {
916*4882a593Smuzhiyun 	struct s390_cpumsf *sf = container_of(session->auxtrace,
917*4882a593Smuzhiyun 					      struct s390_cpumsf,
918*4882a593Smuzhiyun 					      auxtrace);
919*4882a593Smuzhiyun 	u64 timestamp = sample->time;
920*4882a593Smuzhiyun 	struct evsel *ev_bc000;
921*4882a593Smuzhiyun 
922*4882a593Smuzhiyun 	int err = 0;
923*4882a593Smuzhiyun 
924*4882a593Smuzhiyun 	if (dump_trace)
925*4882a593Smuzhiyun 		return 0;
926*4882a593Smuzhiyun 
927*4882a593Smuzhiyun 	if (!tool->ordered_events) {
928*4882a593Smuzhiyun 		pr_err("s390 Auxiliary Trace requires ordered events\n");
929*4882a593Smuzhiyun 		return -EINVAL;
930*4882a593Smuzhiyun 	}
931*4882a593Smuzhiyun 
932*4882a593Smuzhiyun 	if (event->header.type == PERF_RECORD_SAMPLE &&
933*4882a593Smuzhiyun 	    sample->raw_size) {
934*4882a593Smuzhiyun 		/* Handle event with raw data */
935*4882a593Smuzhiyun 		ev_bc000 = perf_evlist__event2evsel(session->evlist, event);
936*4882a593Smuzhiyun 		if (ev_bc000 &&
937*4882a593Smuzhiyun 		    ev_bc000->core.attr.config == PERF_EVENT_CPUM_CF_DIAG)
938*4882a593Smuzhiyun 			err = s390_cpumcf_dumpctr(sf, sample);
939*4882a593Smuzhiyun 		return err;
940*4882a593Smuzhiyun 	}
941*4882a593Smuzhiyun 
942*4882a593Smuzhiyun 	if (event->header.type == PERF_RECORD_AUX &&
943*4882a593Smuzhiyun 	    event->aux.flags & PERF_AUX_FLAG_TRUNCATED)
944*4882a593Smuzhiyun 		return s390_cpumsf_lost(sf, sample);
945*4882a593Smuzhiyun 
946*4882a593Smuzhiyun 	if (timestamp) {
947*4882a593Smuzhiyun 		err = s390_cpumsf_update_queues(sf, timestamp);
948*4882a593Smuzhiyun 		if (!err)
949*4882a593Smuzhiyun 			err = s390_cpumsf_process_queues(sf, timestamp);
950*4882a593Smuzhiyun 	}
951*4882a593Smuzhiyun 	return err;
952*4882a593Smuzhiyun }
953*4882a593Smuzhiyun 
954*4882a593Smuzhiyun struct s390_cpumsf_synth {
955*4882a593Smuzhiyun 	struct perf_tool cpumsf_tool;
956*4882a593Smuzhiyun 	struct perf_session *session;
957*4882a593Smuzhiyun };
958*4882a593Smuzhiyun 
959*4882a593Smuzhiyun static int
s390_cpumsf_process_auxtrace_event(struct perf_session * session,union perf_event * event __maybe_unused,struct perf_tool * tool __maybe_unused)960*4882a593Smuzhiyun s390_cpumsf_process_auxtrace_event(struct perf_session *session,
961*4882a593Smuzhiyun 				   union perf_event *event __maybe_unused,
962*4882a593Smuzhiyun 				   struct perf_tool *tool __maybe_unused)
963*4882a593Smuzhiyun {
964*4882a593Smuzhiyun 	struct s390_cpumsf *sf = container_of(session->auxtrace,
965*4882a593Smuzhiyun 					      struct s390_cpumsf,
966*4882a593Smuzhiyun 					      auxtrace);
967*4882a593Smuzhiyun 
968*4882a593Smuzhiyun 	int fd = perf_data__fd(session->data);
969*4882a593Smuzhiyun 	struct auxtrace_buffer *buffer;
970*4882a593Smuzhiyun 	off_t data_offset;
971*4882a593Smuzhiyun 	int err;
972*4882a593Smuzhiyun 
973*4882a593Smuzhiyun 	if (sf->data_queued)
974*4882a593Smuzhiyun 		return 0;
975*4882a593Smuzhiyun 
976*4882a593Smuzhiyun 	if (perf_data__is_pipe(session->data)) {
977*4882a593Smuzhiyun 		data_offset = 0;
978*4882a593Smuzhiyun 	} else {
979*4882a593Smuzhiyun 		data_offset = lseek(fd, 0, SEEK_CUR);
980*4882a593Smuzhiyun 		if (data_offset == -1)
981*4882a593Smuzhiyun 			return -errno;
982*4882a593Smuzhiyun 	}
983*4882a593Smuzhiyun 
984*4882a593Smuzhiyun 	err = auxtrace_queues__add_event(&sf->queues, session, event,
985*4882a593Smuzhiyun 					 data_offset, &buffer);
986*4882a593Smuzhiyun 	if (err)
987*4882a593Smuzhiyun 		return err;
988*4882a593Smuzhiyun 
989*4882a593Smuzhiyun 	/* Dump here after copying piped trace out of the pipe */
990*4882a593Smuzhiyun 	if (dump_trace) {
991*4882a593Smuzhiyun 		if (auxtrace_buffer__get_data(buffer, fd)) {
992*4882a593Smuzhiyun 			s390_cpumsf_dump_event(sf, buffer->data,
993*4882a593Smuzhiyun 					       buffer->size);
994*4882a593Smuzhiyun 			auxtrace_buffer__put_data(buffer);
995*4882a593Smuzhiyun 		}
996*4882a593Smuzhiyun 	}
997*4882a593Smuzhiyun 	return 0;
998*4882a593Smuzhiyun }
999*4882a593Smuzhiyun 
s390_cpumsf_free_events(struct perf_session * session __maybe_unused)1000*4882a593Smuzhiyun static void s390_cpumsf_free_events(struct perf_session *session __maybe_unused)
1001*4882a593Smuzhiyun {
1002*4882a593Smuzhiyun }
1003*4882a593Smuzhiyun 
s390_cpumsf_flush(struct perf_session * session __maybe_unused,struct perf_tool * tool __maybe_unused)1004*4882a593Smuzhiyun static int s390_cpumsf_flush(struct perf_session *session __maybe_unused,
1005*4882a593Smuzhiyun 			     struct perf_tool *tool __maybe_unused)
1006*4882a593Smuzhiyun {
1007*4882a593Smuzhiyun 	return 0;
1008*4882a593Smuzhiyun }
1009*4882a593Smuzhiyun 
s390_cpumsf_free_queues(struct perf_session * session)1010*4882a593Smuzhiyun static void s390_cpumsf_free_queues(struct perf_session *session)
1011*4882a593Smuzhiyun {
1012*4882a593Smuzhiyun 	struct s390_cpumsf *sf = container_of(session->auxtrace,
1013*4882a593Smuzhiyun 					      struct s390_cpumsf,
1014*4882a593Smuzhiyun 					      auxtrace);
1015*4882a593Smuzhiyun 	struct auxtrace_queues *queues = &sf->queues;
1016*4882a593Smuzhiyun 	unsigned int i;
1017*4882a593Smuzhiyun 
1018*4882a593Smuzhiyun 	for (i = 0; i < queues->nr_queues; i++) {
1019*4882a593Smuzhiyun 		struct s390_cpumsf_queue *sfq = (struct s390_cpumsf_queue *)
1020*4882a593Smuzhiyun 						queues->queue_array[i].priv;
1021*4882a593Smuzhiyun 
1022*4882a593Smuzhiyun 		if (sfq != NULL) {
1023*4882a593Smuzhiyun 			if (sfq->logfile) {
1024*4882a593Smuzhiyun 				fclose(sfq->logfile);
1025*4882a593Smuzhiyun 				sfq->logfile = NULL;
1026*4882a593Smuzhiyun 			}
1027*4882a593Smuzhiyun 			if (sfq->logfile_ctr) {
1028*4882a593Smuzhiyun 				fclose(sfq->logfile_ctr);
1029*4882a593Smuzhiyun 				sfq->logfile_ctr = NULL;
1030*4882a593Smuzhiyun 			}
1031*4882a593Smuzhiyun 		}
1032*4882a593Smuzhiyun 		zfree(&queues->queue_array[i].priv);
1033*4882a593Smuzhiyun 	}
1034*4882a593Smuzhiyun 	auxtrace_queues__free(queues);
1035*4882a593Smuzhiyun }
1036*4882a593Smuzhiyun 
s390_cpumsf_free(struct perf_session * session)1037*4882a593Smuzhiyun static void s390_cpumsf_free(struct perf_session *session)
1038*4882a593Smuzhiyun {
1039*4882a593Smuzhiyun 	struct s390_cpumsf *sf = container_of(session->auxtrace,
1040*4882a593Smuzhiyun 					      struct s390_cpumsf,
1041*4882a593Smuzhiyun 					      auxtrace);
1042*4882a593Smuzhiyun 
1043*4882a593Smuzhiyun 	auxtrace_heap__free(&sf->heap);
1044*4882a593Smuzhiyun 	s390_cpumsf_free_queues(session);
1045*4882a593Smuzhiyun 	session->auxtrace = NULL;
1046*4882a593Smuzhiyun 	zfree(&sf->logdir);
1047*4882a593Smuzhiyun 	free(sf);
1048*4882a593Smuzhiyun }
1049*4882a593Smuzhiyun 
1050*4882a593Smuzhiyun static bool
s390_cpumsf_evsel_is_auxtrace(struct perf_session * session __maybe_unused,struct evsel * evsel)1051*4882a593Smuzhiyun s390_cpumsf_evsel_is_auxtrace(struct perf_session *session __maybe_unused,
1052*4882a593Smuzhiyun 			      struct evsel *evsel)
1053*4882a593Smuzhiyun {
1054*4882a593Smuzhiyun 	return evsel->core.attr.type == PERF_TYPE_RAW &&
1055*4882a593Smuzhiyun 	       evsel->core.attr.config == PERF_EVENT_CPUM_SF_DIAG;
1056*4882a593Smuzhiyun }
1057*4882a593Smuzhiyun 
s390_cpumsf_get_type(const char * cpuid)1058*4882a593Smuzhiyun static int s390_cpumsf_get_type(const char *cpuid)
1059*4882a593Smuzhiyun {
1060*4882a593Smuzhiyun 	int ret, family = 0;
1061*4882a593Smuzhiyun 
1062*4882a593Smuzhiyun 	ret = sscanf(cpuid, "%*[^,],%u", &family);
1063*4882a593Smuzhiyun 	return (ret == 1) ? family : 0;
1064*4882a593Smuzhiyun }
1065*4882a593Smuzhiyun 
1066*4882a593Smuzhiyun /* Check itrace options set on perf report command.
1067*4882a593Smuzhiyun  * Return true, if none are set or all options specified can be
1068*4882a593Smuzhiyun  * handled on s390 (currently only option 'd' for logging.
1069*4882a593Smuzhiyun  * Return false otherwise.
1070*4882a593Smuzhiyun  */
check_auxtrace_itrace(struct itrace_synth_opts * itops)1071*4882a593Smuzhiyun static bool check_auxtrace_itrace(struct itrace_synth_opts *itops)
1072*4882a593Smuzhiyun {
1073*4882a593Smuzhiyun 	bool ison = false;
1074*4882a593Smuzhiyun 
1075*4882a593Smuzhiyun 	if (!itops || !itops->set)
1076*4882a593Smuzhiyun 		return true;
1077*4882a593Smuzhiyun 	ison = itops->inject || itops->instructions || itops->branches ||
1078*4882a593Smuzhiyun 		itops->transactions || itops->ptwrites ||
1079*4882a593Smuzhiyun 		itops->pwr_events || itops->errors ||
1080*4882a593Smuzhiyun 		itops->dont_decode || itops->calls || itops->returns ||
1081*4882a593Smuzhiyun 		itops->callchain || itops->thread_stack ||
1082*4882a593Smuzhiyun 		itops->last_branch || itops->add_callchain ||
1083*4882a593Smuzhiyun 		itops->add_last_branch;
1084*4882a593Smuzhiyun 	if (!ison)
1085*4882a593Smuzhiyun 		return true;
1086*4882a593Smuzhiyun 	pr_err("Unsupported --itrace options specified\n");
1087*4882a593Smuzhiyun 	return false;
1088*4882a593Smuzhiyun }
1089*4882a593Smuzhiyun 
1090*4882a593Smuzhiyun /* Check for AUXTRACE dump directory if it is needed.
1091*4882a593Smuzhiyun  * On failure print an error message but continue.
1092*4882a593Smuzhiyun  * Return 0 on wrong keyword in config file and 1 otherwise.
1093*4882a593Smuzhiyun  */
s390_cpumsf__config(const char * var,const char * value,void * cb)1094*4882a593Smuzhiyun static int s390_cpumsf__config(const char *var, const char *value, void *cb)
1095*4882a593Smuzhiyun {
1096*4882a593Smuzhiyun 	struct s390_cpumsf *sf = cb;
1097*4882a593Smuzhiyun 	struct stat stbuf;
1098*4882a593Smuzhiyun 	int rc;
1099*4882a593Smuzhiyun 
1100*4882a593Smuzhiyun 	if (strcmp(var, "auxtrace.dumpdir"))
1101*4882a593Smuzhiyun 		return 0;
1102*4882a593Smuzhiyun 	sf->logdir = strdup(value);
1103*4882a593Smuzhiyun 	if (sf->logdir == NULL) {
1104*4882a593Smuzhiyun 		pr_err("Failed to find auxtrace log directory %s,"
1105*4882a593Smuzhiyun 		       " continue with current directory...\n", value);
1106*4882a593Smuzhiyun 		return 1;
1107*4882a593Smuzhiyun 	}
1108*4882a593Smuzhiyun 	rc = stat(sf->logdir, &stbuf);
1109*4882a593Smuzhiyun 	if (rc == -1 || !S_ISDIR(stbuf.st_mode)) {
1110*4882a593Smuzhiyun 		pr_err("Missing auxtrace log directory %s,"
1111*4882a593Smuzhiyun 		       " continue with current directory...\n", value);
1112*4882a593Smuzhiyun 		zfree(&sf->logdir);
1113*4882a593Smuzhiyun 	}
1114*4882a593Smuzhiyun 	return 1;
1115*4882a593Smuzhiyun }
1116*4882a593Smuzhiyun 
s390_cpumsf_process_auxtrace_info(union perf_event * event,struct perf_session * session)1117*4882a593Smuzhiyun int s390_cpumsf_process_auxtrace_info(union perf_event *event,
1118*4882a593Smuzhiyun 				      struct perf_session *session)
1119*4882a593Smuzhiyun {
1120*4882a593Smuzhiyun 	struct perf_record_auxtrace_info *auxtrace_info = &event->auxtrace_info;
1121*4882a593Smuzhiyun 	struct s390_cpumsf *sf;
1122*4882a593Smuzhiyun 	int err;
1123*4882a593Smuzhiyun 
1124*4882a593Smuzhiyun 	if (auxtrace_info->header.size < sizeof(struct perf_record_auxtrace_info))
1125*4882a593Smuzhiyun 		return -EINVAL;
1126*4882a593Smuzhiyun 
1127*4882a593Smuzhiyun 	sf = zalloc(sizeof(struct s390_cpumsf));
1128*4882a593Smuzhiyun 	if (sf == NULL)
1129*4882a593Smuzhiyun 		return -ENOMEM;
1130*4882a593Smuzhiyun 
1131*4882a593Smuzhiyun 	if (!check_auxtrace_itrace(session->itrace_synth_opts)) {
1132*4882a593Smuzhiyun 		err = -EINVAL;
1133*4882a593Smuzhiyun 		goto err_free;
1134*4882a593Smuzhiyun 	}
1135*4882a593Smuzhiyun 	sf->use_logfile = session->itrace_synth_opts->log;
1136*4882a593Smuzhiyun 	if (sf->use_logfile)
1137*4882a593Smuzhiyun 		perf_config(s390_cpumsf__config, sf);
1138*4882a593Smuzhiyun 
1139*4882a593Smuzhiyun 	err = auxtrace_queues__init(&sf->queues);
1140*4882a593Smuzhiyun 	if (err)
1141*4882a593Smuzhiyun 		goto err_free;
1142*4882a593Smuzhiyun 
1143*4882a593Smuzhiyun 	sf->session = session;
1144*4882a593Smuzhiyun 	sf->machine = &session->machines.host; /* No kvm support */
1145*4882a593Smuzhiyun 	sf->auxtrace_type = auxtrace_info->type;
1146*4882a593Smuzhiyun 	sf->pmu_type = PERF_TYPE_RAW;
1147*4882a593Smuzhiyun 	sf->machine_type = s390_cpumsf_get_type(session->evlist->env->cpuid);
1148*4882a593Smuzhiyun 
1149*4882a593Smuzhiyun 	sf->auxtrace.process_event = s390_cpumsf_process_event;
1150*4882a593Smuzhiyun 	sf->auxtrace.process_auxtrace_event = s390_cpumsf_process_auxtrace_event;
1151*4882a593Smuzhiyun 	sf->auxtrace.flush_events = s390_cpumsf_flush;
1152*4882a593Smuzhiyun 	sf->auxtrace.free_events = s390_cpumsf_free_events;
1153*4882a593Smuzhiyun 	sf->auxtrace.free = s390_cpumsf_free;
1154*4882a593Smuzhiyun 	sf->auxtrace.evsel_is_auxtrace = s390_cpumsf_evsel_is_auxtrace;
1155*4882a593Smuzhiyun 	session->auxtrace = &sf->auxtrace;
1156*4882a593Smuzhiyun 
1157*4882a593Smuzhiyun 	if (dump_trace)
1158*4882a593Smuzhiyun 		return 0;
1159*4882a593Smuzhiyun 
1160*4882a593Smuzhiyun 	err = auxtrace_queues__process_index(&sf->queues, session);
1161*4882a593Smuzhiyun 	if (err)
1162*4882a593Smuzhiyun 		goto err_free_queues;
1163*4882a593Smuzhiyun 
1164*4882a593Smuzhiyun 	if (sf->queues.populated)
1165*4882a593Smuzhiyun 		sf->data_queued = true;
1166*4882a593Smuzhiyun 
1167*4882a593Smuzhiyun 	return 0;
1168*4882a593Smuzhiyun 
1169*4882a593Smuzhiyun err_free_queues:
1170*4882a593Smuzhiyun 	auxtrace_queues__free(&sf->queues);
1171*4882a593Smuzhiyun 	session->auxtrace = NULL;
1172*4882a593Smuzhiyun err_free:
1173*4882a593Smuzhiyun 	zfree(&sf->logdir);
1174*4882a593Smuzhiyun 	free(sf);
1175*4882a593Smuzhiyun 	return err;
1176*4882a593Smuzhiyun }
1177