xref: /openbmc/linux/tools/perf/util/s390-cpumsf.c (revision 9823147d)
1b96e6615SThomas Richter // SPDX-License-Identifier: GPL-2.0
2b96e6615SThomas Richter /*
3b96e6615SThomas Richter  * Copyright IBM Corp. 2018
4b96e6615SThomas Richter  * Auxtrace support for s390 CPU-Measurement Sampling Facility
5b96e6615SThomas Richter  *
6b96e6615SThomas Richter  * Author(s):  Thomas Richter <tmricht@linux.ibm.com>
733d9e183SThomas Richter  *
833d9e183SThomas Richter  * Auxiliary traces are collected during 'perf record' using rbd000 event.
933d9e183SThomas Richter  * Several PERF_RECORD_XXX are generated during recording:
1033d9e183SThomas Richter  *
1133d9e183SThomas Richter  * PERF_RECORD_AUX:
1233d9e183SThomas Richter  *	Records that new data landed in the AUX buffer part.
1333d9e183SThomas Richter  * PERF_RECORD_AUXTRACE:
1433d9e183SThomas Richter  *	Defines auxtrace data. Followed by the actual data. The contents of
1533d9e183SThomas Richter  *	the auxtrace data is dependent on the event and the CPU.
1633d9e183SThomas Richter  *	This record is generated by perf record command. For details
1733d9e183SThomas Richter  *	see Documentation/perf.data-file-format.txt.
1833d9e183SThomas Richter  * PERF_RECORD_AUXTRACE_INFO:
1933d9e183SThomas Richter  *	Defines a table of contains for PERF_RECORD_AUXTRACE records. This
20180ca71cSThomas Richter  *	record is generated during 'perf record' command. Each record contains
21180ca71cSThomas Richter  *	up to 256 entries describing offset and size of the AUXTRACE data in the
2233d9e183SThomas Richter  *	perf.data file.
2333d9e183SThomas Richter  * PERF_RECORD_AUXTRACE_ERROR:
2433d9e183SThomas Richter  *	Indicates an error during AUXTRACE collection such as buffer overflow.
2533d9e183SThomas Richter  * PERF_RECORD_FINISHED_ROUND:
2633d9e183SThomas Richter  *	Perf events are not necessarily in time stamp order, as they can be
2733d9e183SThomas Richter  *	collected in parallel on different CPUs. If the events should be
2833d9e183SThomas Richter  *	processed in time order they need to be sorted first.
2933d9e183SThomas Richter  *	Perf report guarantees that there is no reordering over a
3033d9e183SThomas Richter  *	PERF_RECORD_FINISHED_ROUND boundary event. All perf records with a
3133d9e183SThomas Richter  *	time stamp lower than this record are processed (and displayed) before
3233d9e183SThomas Richter  *	the succeeding perf record are processed.
3333d9e183SThomas Richter  *
3433d9e183SThomas Richter  * These records are evaluated during perf report command.
3533d9e183SThomas Richter  *
3633d9e183SThomas Richter  * 1. PERF_RECORD_AUXTRACE_INFO is used to set up the infrastructure for
3733d9e183SThomas Richter  * auxiliary trace data processing. See s390_cpumsf_process_auxtrace_info()
3833d9e183SThomas Richter  * below.
3933d9e183SThomas Richter  * Auxiliary trace data is collected per CPU. To merge the data into the report
4033d9e183SThomas Richter  * an auxtrace_queue is created for each CPU. It is assumed that the auxtrace
4133d9e183SThomas Richter  * data is in ascending order.
4233d9e183SThomas Richter  *
4333d9e183SThomas Richter  * Each queue has a double linked list of auxtrace_buffers. This list contains
4433d9e183SThomas Richter  * the offset and size of a CPU's auxtrace data. During auxtrace processing
4533d9e183SThomas Richter  * the data portion is mmap()'ed.
4633d9e183SThomas Richter  *
4733d9e183SThomas Richter  * To sort the queues in chronological order, all queue access is controlled
484d39c89fSIngo Molnar  * by the auxtrace_heap. This is basically a stack, each stack element has two
4933d9e183SThomas Richter  * entries, the queue number and a time stamp. However the stack is sorted by
5033d9e183SThomas Richter  * the time stamps. The highest time stamp is at the bottom the lowest
5133d9e183SThomas Richter  * (nearest) time stamp is at the top. That sort order is maintained at all
5233d9e183SThomas Richter  * times!
5333d9e183SThomas Richter  *
5433d9e183SThomas Richter  * After the auxtrace infrastructure has been setup, the auxtrace queues are
5533d9e183SThomas Richter  * filled with data (offset/size pairs) and the auxtrace_heap is populated.
5633d9e183SThomas Richter  *
5733d9e183SThomas Richter  * 2. PERF_RECORD_XXX processing triggers access to the auxtrace_queues.
5833d9e183SThomas Richter  * Each record is handled by s390_cpumsf_process_event(). The time stamp of
5933d9e183SThomas Richter  * the perf record is compared with the time stamp located on the auxtrace_heap
6033d9e183SThomas Richter  * top element. If that time stamp is lower than the time stamp from the
6133d9e183SThomas Richter  * record sample, the auxtrace queues will be processed. As auxtrace queues
6233d9e183SThomas Richter  * control many auxtrace_buffers and each buffer can be quite large, the
6333d9e183SThomas Richter  * auxtrace buffer might be processed only partially. In this case the
6433d9e183SThomas Richter  * position in the auxtrace_buffer of that queue is remembered and the time
6533d9e183SThomas Richter  * stamp of the last processed entry of the auxtrace_buffer replaces the
6633d9e183SThomas Richter  * current auxtrace_heap top.
6733d9e183SThomas Richter  *
684d39c89fSIngo Molnar  * 3. Auxtrace_queues might run of out data and are fed by the
6933d9e183SThomas Richter  * PERF_RECORD_AUXTRACE handling, see s390_cpumsf_process_auxtrace_event().
7033d9e183SThomas Richter  *
7133d9e183SThomas Richter  * Event Generation
724d39c89fSIngo Molnar  * Each sampling-data entry in the auxiliary trace data generates a perf sample.
7333d9e183SThomas Richter  * This sample is filled
7433d9e183SThomas Richter  * with data from the auxtrace such as PID/TID, instruction address, CPU state,
7533d9e183SThomas Richter  * etc. This sample is processed with perf_session__deliver_synth_event() to
7633d9e183SThomas Richter  * be included into the GUI.
7733d9e183SThomas Richter  *
7833d9e183SThomas Richter  * 4. PERF_RECORD_FINISHED_ROUND event is used to process all the remaining
7933d9e183SThomas Richter  * auxiliary traces entries until the time stamp of this record is reached
8033d9e183SThomas Richter  * auxtrace_heap top. This is triggered by ordered_event->deliver().
8133d9e183SThomas Richter  *
8233d9e183SThomas Richter  *
8333d9e183SThomas Richter  * Perf event processing.
8433d9e183SThomas Richter  * Event processing of PERF_RECORD_XXX entries relies on time stamp entries.
8533d9e183SThomas Richter  * This is the function call sequence:
8633d9e183SThomas Richter  *
8733d9e183SThomas Richter  * __cmd_report()
8833d9e183SThomas Richter  * |
8933d9e183SThomas Richter  * perf_session__process_events()
9033d9e183SThomas Richter  * |
9133d9e183SThomas Richter  * __perf_session__process_events()
9233d9e183SThomas Richter  * |
9333d9e183SThomas Richter  * perf_session__process_event()
9433d9e183SThomas Richter  * |  This functions splits the PERF_RECORD_XXX records.
9533d9e183SThomas Richter  * |  - Those generated by perf record command (type number equal or higher
9633d9e183SThomas Richter  * |    than PERF_RECORD_USER_TYPE_START) are handled by
9733d9e183SThomas Richter  * |    perf_session__process_user_event(see below)
9833d9e183SThomas Richter  * |  - Those generated by the kernel are handled by
992a6599cdSArnaldo Carvalho de Melo  * |    evlist__parse_sample_timestamp()
10033d9e183SThomas Richter  * |
1012a6599cdSArnaldo Carvalho de Melo  * evlist__parse_sample_timestamp()
10233d9e183SThomas Richter  * |  Extract time stamp from sample data.
10333d9e183SThomas Richter  * |
10433d9e183SThomas Richter  * perf_session__queue_event()
10533d9e183SThomas Richter  * |  If timestamp is positive the sample is entered into an ordered_event
10633d9e183SThomas Richter  * |  list, sort order is the timestamp. The event processing is deferred until
10733d9e183SThomas Richter  * |  later (see perf_session__process_user_event()).
10833d9e183SThomas Richter  * |  Other timestamps (0 or -1) are handled immediately by
10933d9e183SThomas Richter  * |  perf_session__deliver_event(). These are events generated at start up
11033d9e183SThomas Richter  * |  of command perf record. They create PERF_RECORD_COMM and PERF_RECORD_MMAP*
11133d9e183SThomas Richter  * |  records. They are needed to create a list of running processes and its
11233d9e183SThomas Richter  * |  memory mappings and layout. They are needed at the beginning to enable
11333d9e183SThomas Richter  * |  command perf report to create process trees and memory mappings.
11433d9e183SThomas Richter  * |
11533d9e183SThomas Richter  * perf_session__deliver_event()
11633d9e183SThomas Richter  * |  Delivers a PERF_RECORD_XXX entry for handling.
11733d9e183SThomas Richter  * |
11833d9e183SThomas Richter  * auxtrace__process_event()
11933d9e183SThomas Richter  * |  The timestamp of the PERF_RECORD_XXX entry is taken to correlate with
12033d9e183SThomas Richter  * |  time stamps from the auxiliary trace buffers. This enables
12133d9e183SThomas Richter  * |  synchronization between auxiliary trace data and the events on the
12233d9e183SThomas Richter  * |  perf.data file.
12333d9e183SThomas Richter  * |
12433d9e183SThomas Richter  * machine__deliver_event()
12533d9e183SThomas Richter  * |  Handles the PERF_RECORD_XXX event. This depends on the record type.
12633d9e183SThomas Richter  *    It might update the process tree, update a process memory map or enter
12733d9e183SThomas Richter  *    a sample with IP and call back chain data into GUI data pool.
12833d9e183SThomas Richter  *
12933d9e183SThomas Richter  *
13033d9e183SThomas Richter  * Deferred processing determined by perf_session__process_user_event() is
13133d9e183SThomas Richter  * finally processed when a PERF_RECORD_FINISHED_ROUND is encountered. These
13233d9e183SThomas Richter  * are generated during command perf record.
13333d9e183SThomas Richter  * The timestamp of PERF_RECORD_FINISHED_ROUND event is taken to process all
13433d9e183SThomas Richter  * PERF_RECORD_XXX entries stored in the ordered_event list. This list was
13533d9e183SThomas Richter  * built up while reading the perf.data file.
13633d9e183SThomas Richter  * Each event is now processed by calling perf_session__deliver_event().
13733d9e183SThomas Richter  * This enables time synchronization between the data in the perf.data file and
13833d9e183SThomas Richter  * the data in the auxiliary trace buffers.
139b96e6615SThomas Richter  */
140b96e6615SThomas Richter 
141b96e6615SThomas Richter #include <endian.h>
142b96e6615SThomas Richter #include <errno.h>
143b96e6615SThomas Richter #include <byteswap.h>
144b96e6615SThomas Richter #include <inttypes.h>
145b96e6615SThomas Richter #include <linux/kernel.h>
146b96e6615SThomas Richter #include <linux/types.h>
147b96e6615SThomas Richter #include <linux/bitops.h>
148b96e6615SThomas Richter #include <linux/log2.h>
1497f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h>
150b96e6615SThomas Richter 
151766e0618SThomas Richter #include <sys/stat.h>
152766e0618SThomas Richter #include <sys/types.h>
153766e0618SThomas Richter 
154b96e6615SThomas Richter #include "color.h"
155b96e6615SThomas Richter #include "evsel.h"
156b96e6615SThomas Richter #include "evlist.h"
157b96e6615SThomas Richter #include "machine.h"
158b96e6615SThomas Richter #include "session.h"
1594a3cec84SArnaldo Carvalho de Melo #include "tool.h"
160b96e6615SThomas Richter #include "debug.h"
161b96e6615SThomas Richter #include "auxtrace.h"
162b96e6615SThomas Richter #include "s390-cpumsf.h"
1632b1444f2SThomas Richter #include "s390-cpumsf-kernel.h"
1648dabe9c4SThomas Richter #include "s390-cpumcf-kernel.h"
165766e0618SThomas Richter #include "config.h"
166*9823147dSArnaldo Carvalho de Melo #include "util/sample.h"
167b96e6615SThomas Richter 
168b96e6615SThomas Richter struct s390_cpumsf {
169b96e6615SThomas Richter 	struct auxtrace		auxtrace;
170b96e6615SThomas Richter 	struct auxtrace_queues	queues;
171b96e6615SThomas Richter 	struct auxtrace_heap	heap;
172b96e6615SThomas Richter 	struct perf_session	*session;
173b96e6615SThomas Richter 	struct machine		*machine;
174b96e6615SThomas Richter 	u32			auxtrace_type;
175b96e6615SThomas Richter 	u32			pmu_type;
1762b1444f2SThomas Richter 	u16			machine_type;
17733d9e183SThomas Richter 	bool			data_queued;
178766e0618SThomas Richter 	bool			use_logfile;
179766e0618SThomas Richter 	char			*logdir;
18033d9e183SThomas Richter };
18133d9e183SThomas Richter 
18233d9e183SThomas Richter struct s390_cpumsf_queue {
18333d9e183SThomas Richter 	struct s390_cpumsf	*sf;
18433d9e183SThomas Richter 	unsigned int		queue_nr;
18533d9e183SThomas Richter 	struct auxtrace_buffer	*buffer;
18633d9e183SThomas Richter 	int			cpu;
187766e0618SThomas Richter 	FILE			*logfile;
1888dabe9c4SThomas Richter 	FILE			*logfile_ctr;
189b96e6615SThomas Richter };
190b96e6615SThomas Richter 
1918dabe9c4SThomas Richter /* Check if the raw data should be dumped to file. If this is the case and
1928dabe9c4SThomas Richter  * the file to dump to has not been opened for writing, do so.
1938dabe9c4SThomas Richter  *
1948dabe9c4SThomas Richter  * Return 0 on success and greater zero on error so processing continues.
1958dabe9c4SThomas Richter  */
s390_cpumcf_dumpctr(struct s390_cpumsf * sf,struct perf_sample * sample)1968dabe9c4SThomas Richter static int s390_cpumcf_dumpctr(struct s390_cpumsf *sf,
1978dabe9c4SThomas Richter 			       struct perf_sample *sample)
1988dabe9c4SThomas Richter {
1998dabe9c4SThomas Richter 	struct s390_cpumsf_queue *sfq;
2008dabe9c4SThomas Richter 	struct auxtrace_queue *q;
2018dabe9c4SThomas Richter 	int rc = 0;
2028dabe9c4SThomas Richter 
2038dabe9c4SThomas Richter 	if (!sf->use_logfile || sf->queues.nr_queues <= sample->cpu)
2048dabe9c4SThomas Richter 		return rc;
2058dabe9c4SThomas Richter 
2068dabe9c4SThomas Richter 	q = &sf->queues.queue_array[sample->cpu];
2078dabe9c4SThomas Richter 	sfq = q->priv;
2088dabe9c4SThomas Richter 	if (!sfq)		/* Queue not yet allocated */
2098dabe9c4SThomas Richter 		return rc;
2108dabe9c4SThomas Richter 
2118dabe9c4SThomas Richter 	if (!sfq->logfile_ctr) {
2128dabe9c4SThomas Richter 		char *name;
2138dabe9c4SThomas Richter 
2148dabe9c4SThomas Richter 		rc = (sf->logdir)
2158dabe9c4SThomas Richter 			? asprintf(&name, "%s/aux.ctr.%02x",
2168dabe9c4SThomas Richter 				 sf->logdir, sample->cpu)
2178dabe9c4SThomas Richter 			: asprintf(&name, "aux.ctr.%02x", sample->cpu);
2188dabe9c4SThomas Richter 		if (rc > 0)
2198dabe9c4SThomas Richter 			sfq->logfile_ctr = fopen(name, "w");
2208dabe9c4SThomas Richter 		if (sfq->logfile_ctr == NULL) {
2218dabe9c4SThomas Richter 			pr_err("Failed to open counter set log file %s, "
2228dabe9c4SThomas Richter 			       "continue...\n", name);
2238dabe9c4SThomas Richter 			rc = 1;
2248dabe9c4SThomas Richter 		}
2258dabe9c4SThomas Richter 		free(name);
2268dabe9c4SThomas Richter 	}
2278dabe9c4SThomas Richter 
2288dabe9c4SThomas Richter 	if (sfq->logfile_ctr) {
2298dabe9c4SThomas Richter 		/* See comment above for -4 */
2308dabe9c4SThomas Richter 		size_t n = fwrite(sample->raw_data, sample->raw_size - 4, 1,
2318dabe9c4SThomas Richter 				  sfq->logfile_ctr);
2328dabe9c4SThomas Richter 		if (n != 1) {
2338dabe9c4SThomas Richter 			pr_err("Failed to write counter set data\n");
2348dabe9c4SThomas Richter 			rc = 1;
2358dabe9c4SThomas Richter 		}
2368dabe9c4SThomas Richter 	}
2378dabe9c4SThomas Richter 	return rc;
2388dabe9c4SThomas Richter }
2398dabe9c4SThomas Richter 
240180ca71cSThomas Richter /* Display s390 CPU measurement facility basic-sampling data entry
241180ca71cSThomas Richter  * Data written on s390 in big endian byte order and contains bit
242180ca71cSThomas Richter  * fields across byte boundaries.
243180ca71cSThomas Richter  */
s390_cpumsf_basic_show(const char * color,size_t pos,struct hws_basic_entry * basicp)2442b1444f2SThomas Richter static bool s390_cpumsf_basic_show(const char *color, size_t pos,
245180ca71cSThomas Richter 				   struct hws_basic_entry *basicp)
2462b1444f2SThomas Richter {
247180ca71cSThomas Richter 	struct hws_basic_entry *basic = basicp;
2484e88118cSIlya Leoshkevich #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
249180ca71cSThomas Richter 	struct hws_basic_entry local;
250180ca71cSThomas Richter 	unsigned long long word = be64toh(*(unsigned long long *)basicp);
251180ca71cSThomas Richter 
252180ca71cSThomas Richter 	memset(&local, 0, sizeof(local));
253180ca71cSThomas Richter 	local.def = be16toh(basicp->def);
254180ca71cSThomas Richter 	local.prim_asn = word & 0xffff;
255180ca71cSThomas Richter 	local.CL = word >> 30 & 0x3;
256180ca71cSThomas Richter 	local.I = word >> 32 & 0x1;
257180ca71cSThomas Richter 	local.AS = word >> 33 & 0x3;
258180ca71cSThomas Richter 	local.P = word >> 35 & 0x1;
259180ca71cSThomas Richter 	local.W = word >> 36 & 0x1;
260180ca71cSThomas Richter 	local.T = word >> 37 & 0x1;
261180ca71cSThomas Richter 	local.U = word >> 40 & 0xf;
262180ca71cSThomas Richter 	local.ia = be64toh(basicp->ia);
263180ca71cSThomas Richter 	local.gpp = be64toh(basicp->gpp);
264180ca71cSThomas Richter 	local.hpp = be64toh(basicp->hpp);
265180ca71cSThomas Richter 	basic = &local;
266180ca71cSThomas Richter #endif
2672b1444f2SThomas Richter 	if (basic->def != 1) {
2682b1444f2SThomas Richter 		pr_err("Invalid AUX trace basic entry [%#08zx]\n", pos);
2692b1444f2SThomas Richter 		return false;
2702b1444f2SThomas Richter 	}
2712b1444f2SThomas Richter 	color_fprintf(stdout, color, "    [%#08zx] Basic   Def:%04x Inst:%#04x"
2722b1444f2SThomas Richter 		      " %c%c%c%c AS:%d ASN:%#04x IA:%#018llx\n"
2732b1444f2SThomas Richter 		      "\t\tCL:%d HPP:%#018llx GPP:%#018llx\n",
2742b1444f2SThomas Richter 		      pos, basic->def, basic->U,
2752b1444f2SThomas Richter 		      basic->T ? 'T' : ' ',
2762b1444f2SThomas Richter 		      basic->W ? 'W' : ' ',
2772b1444f2SThomas Richter 		      basic->P ? 'P' : ' ',
2782b1444f2SThomas Richter 		      basic->I ? 'I' : ' ',
2792b1444f2SThomas Richter 		      basic->AS, basic->prim_asn, basic->ia, basic->CL,
2802b1444f2SThomas Richter 		      basic->hpp, basic->gpp);
2812b1444f2SThomas Richter 	return true;
2822b1444f2SThomas Richter }
2832b1444f2SThomas Richter 
284180ca71cSThomas Richter /* Display s390 CPU measurement facility diagnostic-sampling data entry.
285180ca71cSThomas Richter  * Data written on s390 in big endian byte order and contains bit
286180ca71cSThomas Richter  * fields across byte boundaries.
287180ca71cSThomas Richter  */
s390_cpumsf_diag_show(const char * color,size_t pos,struct hws_diag_entry * diagp)2882b1444f2SThomas Richter static bool s390_cpumsf_diag_show(const char *color, size_t pos,
289180ca71cSThomas Richter 				  struct hws_diag_entry *diagp)
2902b1444f2SThomas Richter {
291180ca71cSThomas Richter 	struct hws_diag_entry *diag = diagp;
2924e88118cSIlya Leoshkevich #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
293180ca71cSThomas Richter 	struct hws_diag_entry local;
294180ca71cSThomas Richter 	unsigned long long word = be64toh(*(unsigned long long *)diagp);
295180ca71cSThomas Richter 
296180ca71cSThomas Richter 	local.def = be16toh(diagp->def);
297180ca71cSThomas Richter 	local.I = word >> 32 & 0x1;
298180ca71cSThomas Richter 	diag = &local;
299180ca71cSThomas Richter #endif
3002b1444f2SThomas Richter 	if (diag->def < S390_CPUMSF_DIAG_DEF_FIRST) {
3012b1444f2SThomas Richter 		pr_err("Invalid AUX trace diagnostic entry [%#08zx]\n", pos);
3022b1444f2SThomas Richter 		return false;
3032b1444f2SThomas Richter 	}
3042b1444f2SThomas Richter 	color_fprintf(stdout, color, "    [%#08zx] Diag    Def:%04x %c\n",
3052b1444f2SThomas Richter 		      pos, diag->def, diag->I ? 'I' : ' ');
3062b1444f2SThomas Richter 	return true;
3072b1444f2SThomas Richter }
3082b1444f2SThomas Richter 
3092b1444f2SThomas Richter /* Return TOD timestamp contained in an trailer entry */
trailer_timestamp(struct hws_trailer_entry * te,int idx)310180ca71cSThomas Richter static unsigned long long trailer_timestamp(struct hws_trailer_entry *te,
311180ca71cSThomas Richter 					    int idx)
3122b1444f2SThomas Richter {
3132b1444f2SThomas Richter 	/* te->t set: TOD in STCKE format, bytes 8-15
3142b1444f2SThomas Richter 	 * to->t not set: TOD in STCK format, bytes 0-7
3152b1444f2SThomas Richter 	 */
3162b1444f2SThomas Richter 	unsigned long long ts;
3172b1444f2SThomas Richter 
318180ca71cSThomas Richter 	memcpy(&ts, &te->timestamp[idx], sizeof(ts));
319180ca71cSThomas Richter 	return be64toh(ts);
3202b1444f2SThomas Richter }
3212b1444f2SThomas Richter 
3222b1444f2SThomas Richter /* Display s390 CPU measurement facility trailer entry */
s390_cpumsf_trailer_show(const char * color,size_t pos,struct hws_trailer_entry * te)3232b1444f2SThomas Richter static bool s390_cpumsf_trailer_show(const char *color, size_t pos,
3242b1444f2SThomas Richter 				     struct hws_trailer_entry *te)
3252b1444f2SThomas Richter {
3264e88118cSIlya Leoshkevich #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
327180ca71cSThomas Richter 	struct hws_trailer_entry local;
328180ca71cSThomas Richter 	const unsigned long long flags = be64toh(te->flags);
329180ca71cSThomas Richter 
330180ca71cSThomas Richter 	memset(&local, 0, sizeof(local));
331180ca71cSThomas Richter 	local.f = flags >> 63 & 0x1;
332180ca71cSThomas Richter 	local.a = flags >> 62 & 0x1;
333180ca71cSThomas Richter 	local.t = flags >> 61 & 0x1;
334180ca71cSThomas Richter 	local.bsdes = be16toh((flags >> 16 & 0xffff));
335180ca71cSThomas Richter 	local.dsdes = be16toh((flags & 0xffff));
336180ca71cSThomas Richter 	memcpy(&local.timestamp, te->timestamp, sizeof(te->timestamp));
337180ca71cSThomas Richter 	local.overflow = be64toh(te->overflow);
338180ca71cSThomas Richter 	local.clock_base = be64toh(te->progusage[0]) >> 63 & 1;
339180ca71cSThomas Richter 	local.progusage2 = be64toh(te->progusage2);
340180ca71cSThomas Richter 	te = &local;
341180ca71cSThomas Richter #endif
3422b1444f2SThomas Richter 	if (te->bsdes != sizeof(struct hws_basic_entry)) {
3432b1444f2SThomas Richter 		pr_err("Invalid AUX trace trailer entry [%#08zx]\n", pos);
3442b1444f2SThomas Richter 		return false;
3452b1444f2SThomas Richter 	}
3462b1444f2SThomas Richter 	color_fprintf(stdout, color, "    [%#08zx] Trailer %c%c%c bsdes:%d"
3472b1444f2SThomas Richter 		      " dsdes:%d Overflow:%lld Time:%#llx\n"
348180ca71cSThomas Richter 		      "\t\tC:%d TOD:%#lx\n",
3492b1444f2SThomas Richter 		      pos,
3502b1444f2SThomas Richter 		      te->f ? 'F' : ' ',
3512b1444f2SThomas Richter 		      te->a ? 'A' : ' ',
3522b1444f2SThomas Richter 		      te->t ? 'T' : ' ',
3532b1444f2SThomas Richter 		      te->bsdes, te->dsdes, te->overflow,
354180ca71cSThomas Richter 		      trailer_timestamp(te, te->clock_base),
355180ca71cSThomas Richter 		      te->clock_base, te->progusage2);
3562b1444f2SThomas Richter 	return true;
3572b1444f2SThomas Richter }
3582b1444f2SThomas Richter 
3592b1444f2SThomas Richter /* Test a sample data block. It must be 4KB or a multiple thereof in size and
3602b1444f2SThomas Richter  * 4KB page aligned. Each sample data page has a trailer entry at the
3612b1444f2SThomas Richter  * end which contains the sample entry data sizes.
3622b1444f2SThomas Richter  *
3632b1444f2SThomas Richter  * Return true if the sample data block passes the checks and set the
3642b1444f2SThomas Richter  * basic set entry size and diagnostic set entry size.
3652b1444f2SThomas Richter  *
3662b1444f2SThomas Richter  * Return false on failure.
3672b1444f2SThomas Richter  *
3682b1444f2SThomas Richter  * Note: Old hardware does not set the basic or diagnostic entry sizes
3692b1444f2SThomas Richter  * in the trailer entry. Use the type number instead.
3702b1444f2SThomas Richter  */
s390_cpumsf_validate(int machine_type,unsigned char * buf,size_t len,unsigned short * bsdes,unsigned short * dsdes)3712b1444f2SThomas Richter static bool s390_cpumsf_validate(int machine_type,
3722b1444f2SThomas Richter 				 unsigned char *buf, size_t len,
3732b1444f2SThomas Richter 				 unsigned short *bsdes,
3742b1444f2SThomas Richter 				 unsigned short *dsdes)
3752b1444f2SThomas Richter {
3762b1444f2SThomas Richter 	struct hws_basic_entry *basic = (struct hws_basic_entry *)buf;
3772b1444f2SThomas Richter 	struct hws_trailer_entry *te;
3782b1444f2SThomas Richter 
3792b1444f2SThomas Richter 	*dsdes = *bsdes = 0;
3802b1444f2SThomas Richter 	if (len & (S390_CPUMSF_PAGESZ - 1))	/* Illegal size */
3812b1444f2SThomas Richter 		return false;
382180ca71cSThomas Richter 	if (be16toh(basic->def) != 1)	/* No basic set entry, must be first */
3832b1444f2SThomas Richter 		return false;
3842b1444f2SThomas Richter 	/* Check for trailer entry at end of SDB */
3852b1444f2SThomas Richter 	te = (struct hws_trailer_entry *)(buf + S390_CPUMSF_PAGESZ
3862b1444f2SThomas Richter 					      - sizeof(*te));
387180ca71cSThomas Richter 	*bsdes = be16toh(te->bsdes);
388180ca71cSThomas Richter 	*dsdes = be16toh(te->dsdes);
3892b1444f2SThomas Richter 	if (!te->bsdes && !te->dsdes) {
3902b1444f2SThomas Richter 		/* Very old hardware, use CPUID */
3912b1444f2SThomas Richter 		switch (machine_type) {
3922b1444f2SThomas Richter 		case 2097:
3932b1444f2SThomas Richter 		case 2098:
3942b1444f2SThomas Richter 			*dsdes = 64;
3952b1444f2SThomas Richter 			*bsdes = 32;
3962b1444f2SThomas Richter 			break;
3972b1444f2SThomas Richter 		case 2817:
3982b1444f2SThomas Richter 		case 2818:
3992b1444f2SThomas Richter 			*dsdes = 74;
4002b1444f2SThomas Richter 			*bsdes = 32;
4012b1444f2SThomas Richter 			break;
4022b1444f2SThomas Richter 		case 2827:
4032b1444f2SThomas Richter 		case 2828:
4042b1444f2SThomas Richter 			*dsdes = 85;
4052b1444f2SThomas Richter 			*bsdes = 32;
4062b1444f2SThomas Richter 			break;
4072187d87eSThomas Richter 		case 2964:
4082187d87eSThomas Richter 		case 2965:
4092187d87eSThomas Richter 			*dsdes = 112;
4102187d87eSThomas Richter 			*bsdes = 32;
4112187d87eSThomas Richter 			break;
4122b1444f2SThomas Richter 		default:
4132b1444f2SThomas Richter 			/* Illegal trailer entry */
4142b1444f2SThomas Richter 			return false;
4152b1444f2SThomas Richter 		}
4162b1444f2SThomas Richter 	}
4172b1444f2SThomas Richter 	return true;
4182b1444f2SThomas Richter }
4192b1444f2SThomas Richter 
4202b1444f2SThomas Richter /* Return true if there is room for another entry */
s390_cpumsf_reached_trailer(size_t entry_sz,size_t pos)4212b1444f2SThomas Richter static bool s390_cpumsf_reached_trailer(size_t entry_sz, size_t pos)
4222b1444f2SThomas Richter {
4232b1444f2SThomas Richter 	size_t payload = S390_CPUMSF_PAGESZ - sizeof(struct hws_trailer_entry);
4242b1444f2SThomas Richter 
4252b1444f2SThomas Richter 	if (payload - (pos & (S390_CPUMSF_PAGESZ - 1)) < entry_sz)
4262b1444f2SThomas Richter 		return false;
4272b1444f2SThomas Richter 	return true;
4282b1444f2SThomas Richter }
4292b1444f2SThomas Richter 
4302b1444f2SThomas Richter /* Dump an auxiliary buffer. These buffers are multiple of
4312b1444f2SThomas Richter  * 4KB SDB pages.
4322b1444f2SThomas Richter  */
s390_cpumsf_dump(struct s390_cpumsf * sf,unsigned char * buf,size_t len)4332b1444f2SThomas Richter static void s390_cpumsf_dump(struct s390_cpumsf *sf,
4342b1444f2SThomas Richter 			     unsigned char *buf, size_t len)
4352b1444f2SThomas Richter {
4362b1444f2SThomas Richter 	const char *color = PERF_COLOR_BLUE;
4372b1444f2SThomas Richter 	struct hws_basic_entry *basic;
4382b1444f2SThomas Richter 	struct hws_diag_entry *diag;
4392b1444f2SThomas Richter 	unsigned short bsdes, dsdes;
44033d9e183SThomas Richter 	size_t pos = 0;
4412b1444f2SThomas Richter 
4422b1444f2SThomas Richter 	color_fprintf(stdout, color,
4432b1444f2SThomas Richter 		      ". ... s390 AUX data: size %zu bytes\n",
4442b1444f2SThomas Richter 		      len);
4452b1444f2SThomas Richter 
4462b1444f2SThomas Richter 	if (!s390_cpumsf_validate(sf->machine_type, buf, len, &bsdes,
4472b1444f2SThomas Richter 				  &dsdes)) {
4482b1444f2SThomas Richter 		pr_err("Invalid AUX trace data block size:%zu"
4492b1444f2SThomas Richter 		       " (type:%d bsdes:%hd dsdes:%hd)\n",
4502b1444f2SThomas Richter 		       len, sf->machine_type, bsdes, dsdes);
4512b1444f2SThomas Richter 		return;
4522b1444f2SThomas Richter 	}
4532b1444f2SThomas Richter 
4542b1444f2SThomas Richter 	/* s390 kernel always returns 4KB blocks fully occupied,
4552b1444f2SThomas Richter 	 * no partially filled SDBs.
4562b1444f2SThomas Richter 	 */
4572b1444f2SThomas Richter 	while (pos < len) {
4582b1444f2SThomas Richter 		/* Handle Basic entry */
4592b1444f2SThomas Richter 		basic = (struct hws_basic_entry *)(buf + pos);
4602b1444f2SThomas Richter 		if (s390_cpumsf_basic_show(color, pos, basic))
4612b1444f2SThomas Richter 			pos += bsdes;
4622b1444f2SThomas Richter 		else
4632b1444f2SThomas Richter 			return;
4642b1444f2SThomas Richter 
4652b1444f2SThomas Richter 		/* Handle Diagnostic entry */
4662b1444f2SThomas Richter 		diag = (struct hws_diag_entry *)(buf + pos);
4672b1444f2SThomas Richter 		if (s390_cpumsf_diag_show(color, pos, diag))
4682b1444f2SThomas Richter 			pos += dsdes;
4692b1444f2SThomas Richter 		else
4702b1444f2SThomas Richter 			return;
4712b1444f2SThomas Richter 
4722b1444f2SThomas Richter 		/* Check for trailer entry */
4732b1444f2SThomas Richter 		if (!s390_cpumsf_reached_trailer(bsdes + dsdes, pos)) {
4742b1444f2SThomas Richter 			/* Show trailer entry */
4752b1444f2SThomas Richter 			struct hws_trailer_entry te;
4762b1444f2SThomas Richter 
4772b1444f2SThomas Richter 			pos = (pos + S390_CPUMSF_PAGESZ)
4782b1444f2SThomas Richter 			       & ~(S390_CPUMSF_PAGESZ - 1);
4792b1444f2SThomas Richter 			pos -= sizeof(te);
4802b1444f2SThomas Richter 			memcpy(&te, buf + pos, sizeof(te));
4812b1444f2SThomas Richter 			/* Set descriptor sizes in case of old hardware
4822b1444f2SThomas Richter 			 * where these values are not set.
4832b1444f2SThomas Richter 			 */
4842b1444f2SThomas Richter 			te.bsdes = bsdes;
4852b1444f2SThomas Richter 			te.dsdes = dsdes;
4862b1444f2SThomas Richter 			if (s390_cpumsf_trailer_show(color, pos, &te))
4872b1444f2SThomas Richter 				pos += sizeof(te);
4882b1444f2SThomas Richter 			else
4892b1444f2SThomas Richter 				return;
4902b1444f2SThomas Richter 		}
4912b1444f2SThomas Richter 	}
4922b1444f2SThomas Richter }
4932b1444f2SThomas Richter 
s390_cpumsf_dump_event(struct s390_cpumsf * sf,unsigned char * buf,size_t len)4942b1444f2SThomas Richter static void s390_cpumsf_dump_event(struct s390_cpumsf *sf, unsigned char *buf,
4952b1444f2SThomas Richter 				   size_t len)
4962b1444f2SThomas Richter {
4972b1444f2SThomas Richter 	printf(".\n");
4982b1444f2SThomas Richter 	s390_cpumsf_dump(sf, buf, len);
4992b1444f2SThomas Richter }
5002b1444f2SThomas Richter 
50133d9e183SThomas Richter #define	S390_LPP_PID_MASK	0xffffffff
50233d9e183SThomas Richter 
s390_cpumsf_make_event(size_t pos,struct hws_basic_entry * basic,struct s390_cpumsf_queue * sfq)50333d9e183SThomas Richter static bool s390_cpumsf_make_event(size_t pos,
50433d9e183SThomas Richter 				   struct hws_basic_entry *basic,
50533d9e183SThomas Richter 				   struct s390_cpumsf_queue *sfq)
506b96e6615SThomas Richter {
50733d9e183SThomas Richter 	struct perf_sample sample = {
50833d9e183SThomas Richter 				.ip = basic->ia,
50933d9e183SThomas Richter 				.pid = basic->hpp & S390_LPP_PID_MASK,
51033d9e183SThomas Richter 				.tid = basic->hpp & S390_LPP_PID_MASK,
51133d9e183SThomas Richter 				.cpumode = PERF_RECORD_MISC_CPUMODE_UNKNOWN,
51233d9e183SThomas Richter 				.cpu = sfq->cpu,
51333d9e183SThomas Richter 				.period = 1
51433d9e183SThomas Richter 			    };
51533d9e183SThomas Richter 	union perf_event event;
51633d9e183SThomas Richter 
51733d9e183SThomas Richter 	memset(&event, 0, sizeof(event));
51833d9e183SThomas Richter 	if (basic->CL == 1)	/* Native LPAR mode */
51933d9e183SThomas Richter 		sample.cpumode = basic->P ? PERF_RECORD_MISC_USER
52033d9e183SThomas Richter 					  : PERF_RECORD_MISC_KERNEL;
52133d9e183SThomas Richter 	else if (basic->CL == 2)	/* Guest kernel/user space */
52233d9e183SThomas Richter 		sample.cpumode = basic->P ? PERF_RECORD_MISC_GUEST_USER
52333d9e183SThomas Richter 					  : PERF_RECORD_MISC_GUEST_KERNEL;
52433d9e183SThomas Richter 	else if (basic->gpp || basic->prim_asn != 0xffff)
52533d9e183SThomas Richter 		/* Use heuristics on old hardware */
52633d9e183SThomas Richter 		sample.cpumode = basic->P ? PERF_RECORD_MISC_GUEST_USER
52733d9e183SThomas Richter 					  : PERF_RECORD_MISC_GUEST_KERNEL;
52833d9e183SThomas Richter 	else
52933d9e183SThomas Richter 		sample.cpumode = basic->P ? PERF_RECORD_MISC_USER
53033d9e183SThomas Richter 					  : PERF_RECORD_MISC_KERNEL;
53133d9e183SThomas Richter 
53233d9e183SThomas Richter 	event.sample.header.type = PERF_RECORD_SAMPLE;
53333d9e183SThomas Richter 	event.sample.header.misc = sample.cpumode;
53433d9e183SThomas Richter 	event.sample.header.size = sizeof(struct perf_event_header);
53533d9e183SThomas Richter 
53633d9e183SThomas Richter 	pr_debug4("%s pos:%#zx ip:%#" PRIx64 " P:%d CL:%d pid:%d.%d cpumode:%d cpu:%d\n",
53733d9e183SThomas Richter 		 __func__, pos, sample.ip, basic->P, basic->CL, sample.pid,
53833d9e183SThomas Richter 		 sample.tid, sample.cpumode, sample.cpu);
53933d9e183SThomas Richter 	if (perf_session__deliver_synth_event(sfq->sf->session, &event,
54033d9e183SThomas Richter 					      &sample)) {
54133d9e183SThomas Richter 		pr_err("s390 Auxiliary Trace: failed to deliver event\n");
54233d9e183SThomas Richter 		return false;
54333d9e183SThomas Richter 	}
54433d9e183SThomas Richter 	return true;
54533d9e183SThomas Richter }
54633d9e183SThomas Richter 
get_trailer_time(const unsigned char * buf)54733d9e183SThomas Richter static unsigned long long get_trailer_time(const unsigned char *buf)
54833d9e183SThomas Richter {
54933d9e183SThomas Richter 	struct hws_trailer_entry *te;
550180ca71cSThomas Richter 	unsigned long long aux_time, progusage2;
551180ca71cSThomas Richter 	bool clock_base;
55233d9e183SThomas Richter 
55333d9e183SThomas Richter 	te = (struct hws_trailer_entry *)(buf + S390_CPUMSF_PAGESZ
55433d9e183SThomas Richter 					      - sizeof(*te));
55533d9e183SThomas Richter 
5564e88118cSIlya Leoshkevich #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
557180ca71cSThomas Richter 	clock_base = be64toh(te->progusage[0]) >> 63 & 0x1;
558180ca71cSThomas Richter 	progusage2 = be64toh(te->progusage[1]);
559180ca71cSThomas Richter #else
560180ca71cSThomas Richter 	clock_base = te->clock_base;
561180ca71cSThomas Richter 	progusage2 = te->progusage2;
562180ca71cSThomas Richter #endif
563180ca71cSThomas Richter 	if (!clock_base)	/* TOD_CLOCK_BASE value missing */
56433d9e183SThomas Richter 		return 0;
56533d9e183SThomas Richter 
56633d9e183SThomas Richter 	/* Correct calculation to convert time stamp in trailer entry to
56733d9e183SThomas Richter 	 * nano seconds (taken from arch/s390 function tod_to_ns()).
56833d9e183SThomas Richter 	 * TOD_CLOCK_BASE is stored in trailer entry member progusage2.
56933d9e183SThomas Richter 	 */
570180ca71cSThomas Richter 	aux_time = trailer_timestamp(te, clock_base) - progusage2;
57133d9e183SThomas Richter 	aux_time = (aux_time >> 9) * 125 + (((aux_time & 0x1ff) * 125) >> 9);
57233d9e183SThomas Richter 	return aux_time;
57333d9e183SThomas Richter }
57433d9e183SThomas Richter 
57533d9e183SThomas Richter /* Process the data samples of a single queue. The first parameter is a
57633d9e183SThomas Richter  * pointer to the queue, the second parameter is the time stamp. This
57733d9e183SThomas Richter  * is the time stamp:
57833d9e183SThomas Richter  * - of the event that triggered this processing.
5794d39c89fSIngo Molnar  * - or the time stamp when the last processing of this queue stopped.
58033d9e183SThomas Richter  *   In this case it stopped at a 4KB page boundary and record the
58133d9e183SThomas Richter  *   position on where to continue processing on the next invocation
58233d9e183SThomas Richter  *   (see buffer->use_data and buffer->use_size).
58333d9e183SThomas Richter  *
58433d9e183SThomas Richter  * When this function returns the second parameter is updated to
58533d9e183SThomas Richter  * reflect the time stamp of the last processed auxiliary data entry
58633d9e183SThomas Richter  * (taken from the trailer entry of that page). The caller uses this
58733d9e183SThomas Richter  * returned time stamp to record the last processed entry in this
58833d9e183SThomas Richter  * queue.
58933d9e183SThomas Richter  *
59033d9e183SThomas Richter  * The function returns:
59133d9e183SThomas Richter  * 0:  Processing successful. The second parameter returns the
59233d9e183SThomas Richter  *     time stamp from the trailer entry until which position
59333d9e183SThomas Richter  *     processing took place. Subsequent calls resume from this
59433d9e183SThomas Richter  *     position.
59533d9e183SThomas Richter  * <0: An error occurred during processing. The second parameter
59633d9e183SThomas Richter  *     returns the maximum time stamp.
59733d9e183SThomas Richter  * >0: Done on this queue. The second parameter returns the
59833d9e183SThomas Richter  *     maximum time stamp.
59933d9e183SThomas Richter  */
s390_cpumsf_samples(struct s390_cpumsf_queue * sfq,u64 * ts)60033d9e183SThomas Richter static int s390_cpumsf_samples(struct s390_cpumsf_queue *sfq, u64 *ts)
60133d9e183SThomas Richter {
60233d9e183SThomas Richter 	struct s390_cpumsf *sf = sfq->sf;
60333d9e183SThomas Richter 	unsigned char *buf = sfq->buffer->use_data;
60433d9e183SThomas Richter 	size_t len = sfq->buffer->use_size;
60533d9e183SThomas Richter 	struct hws_basic_entry *basic;
60633d9e183SThomas Richter 	unsigned short bsdes, dsdes;
60733d9e183SThomas Richter 	size_t pos = 0;
60833d9e183SThomas Richter 	int err = 1;
60933d9e183SThomas Richter 	u64 aux_ts;
61033d9e183SThomas Richter 
61133d9e183SThomas Richter 	if (!s390_cpumsf_validate(sf->machine_type, buf, len, &bsdes,
61233d9e183SThomas Richter 				  &dsdes)) {
61333d9e183SThomas Richter 		*ts = ~0ULL;
61433d9e183SThomas Richter 		return -1;
61533d9e183SThomas Richter 	}
61633d9e183SThomas Richter 
61733d9e183SThomas Richter 	/* Get trailer entry time stamp and check if entries in
61833d9e183SThomas Richter 	 * this auxiliary page are ready for processing. If the
61933d9e183SThomas Richter 	 * time stamp of the first entry is too high, whole buffer
62033d9e183SThomas Richter 	 * can be skipped. In this case return time stamp.
62133d9e183SThomas Richter 	 */
62233d9e183SThomas Richter 	aux_ts = get_trailer_time(buf);
62333d9e183SThomas Richter 	if (!aux_ts) {
62433d9e183SThomas Richter 		pr_err("[%#08" PRIx64 "] Invalid AUX trailer entry TOD clock base\n",
625866053bbSArnaldo Carvalho de Melo 		       (s64)sfq->buffer->data_offset);
62633d9e183SThomas Richter 		aux_ts = ~0ULL;
62733d9e183SThomas Richter 		goto out;
62833d9e183SThomas Richter 	}
62933d9e183SThomas Richter 	if (aux_ts > *ts) {
63033d9e183SThomas Richter 		*ts = aux_ts;
631b96e6615SThomas Richter 		return 0;
632b96e6615SThomas Richter 	}
633b96e6615SThomas Richter 
63433d9e183SThomas Richter 	while (pos < len) {
63533d9e183SThomas Richter 		/* Handle Basic entry */
63633d9e183SThomas Richter 		basic = (struct hws_basic_entry *)(buf + pos);
63733d9e183SThomas Richter 		if (s390_cpumsf_make_event(pos, basic, sfq))
63833d9e183SThomas Richter 			pos += bsdes;
63933d9e183SThomas Richter 		else {
64033d9e183SThomas Richter 			err = -EBADF;
64133d9e183SThomas Richter 			goto out;
64233d9e183SThomas Richter 		}
64333d9e183SThomas Richter 
6444d39c89fSIngo Molnar 		pos += dsdes;	/* Skip diagnostic entry */
64533d9e183SThomas Richter 
64633d9e183SThomas Richter 		/* Check for trailer entry */
64733d9e183SThomas Richter 		if (!s390_cpumsf_reached_trailer(bsdes + dsdes, pos)) {
64833d9e183SThomas Richter 			pos = (pos + S390_CPUMSF_PAGESZ)
64933d9e183SThomas Richter 			       & ~(S390_CPUMSF_PAGESZ - 1);
65033d9e183SThomas Richter 			/* Check existence of next page */
65133d9e183SThomas Richter 			if (pos >= len)
65233d9e183SThomas Richter 				break;
65333d9e183SThomas Richter 			aux_ts = get_trailer_time(buf + pos);
65433d9e183SThomas Richter 			if (!aux_ts) {
65533d9e183SThomas Richter 				aux_ts = ~0ULL;
65633d9e183SThomas Richter 				goto out;
65733d9e183SThomas Richter 			}
65833d9e183SThomas Richter 			if (aux_ts > *ts) {
65933d9e183SThomas Richter 				*ts = aux_ts;
66033d9e183SThomas Richter 				sfq->buffer->use_data += pos;
66133d9e183SThomas Richter 				sfq->buffer->use_size -= pos;
66233d9e183SThomas Richter 				return 0;
66333d9e183SThomas Richter 			}
66433d9e183SThomas Richter 		}
66533d9e183SThomas Richter 	}
66633d9e183SThomas Richter out:
66733d9e183SThomas Richter 	*ts = aux_ts;
66833d9e183SThomas Richter 	sfq->buffer->use_size = 0;
66933d9e183SThomas Richter 	sfq->buffer->use_data = NULL;
67033d9e183SThomas Richter 	return err;	/* Buffer completely scanned or error */
67133d9e183SThomas Richter }
67233d9e183SThomas Richter 
67333d9e183SThomas Richter /* Run the s390 auxiliary trace decoder.
67433d9e183SThomas Richter  * Select the queue buffer to operate on, the caller already selected
67533d9e183SThomas Richter  * the proper queue, depending on second parameter 'ts'.
67633d9e183SThomas Richter  * This is the time stamp until which the auxiliary entries should
67733d9e183SThomas Richter  * be processed. This value is updated by called functions and
67833d9e183SThomas Richter  * returned to the caller.
67933d9e183SThomas Richter  *
68033d9e183SThomas Richter  * Resume processing in the current buffer. If there is no buffer
68133d9e183SThomas Richter  * get a new buffer from the queue and setup start position for
68233d9e183SThomas Richter  * processing.
68333d9e183SThomas Richter  * When a buffer is completely processed remove it from the queue
68433d9e183SThomas Richter  * before returning.
68533d9e183SThomas Richter  *
68633d9e183SThomas Richter  * This function returns
68733d9e183SThomas Richter  * 1: When the queue is empty. Second parameter will be set to
68833d9e183SThomas Richter  *    maximum time stamp.
68933d9e183SThomas Richter  * 0: Normal processing done.
69033d9e183SThomas Richter  * <0: Error during queue buffer setup. This causes the caller
69133d9e183SThomas Richter  *     to stop processing completely.
69233d9e183SThomas Richter  */
s390_cpumsf_run_decoder(struct s390_cpumsf_queue * sfq,u64 * ts)69333d9e183SThomas Richter static int s390_cpumsf_run_decoder(struct s390_cpumsf_queue *sfq,
69433d9e183SThomas Richter 				   u64 *ts)
69533d9e183SThomas Richter {
69633d9e183SThomas Richter 
69733d9e183SThomas Richter 	struct auxtrace_buffer *buffer;
69833d9e183SThomas Richter 	struct auxtrace_queue *queue;
69933d9e183SThomas Richter 	int err;
70033d9e183SThomas Richter 
70133d9e183SThomas Richter 	queue = &sfq->sf->queues.queue_array[sfq->queue_nr];
70233d9e183SThomas Richter 
70333d9e183SThomas Richter 	/* Get buffer and last position in buffer to resume
70433d9e183SThomas Richter 	 * decoding the auxiliary entries. One buffer might be large
70533d9e183SThomas Richter 	 * and decoding might stop in between. This depends on the time
70633d9e183SThomas Richter 	 * stamp of the trailer entry in each page of the auxiliary
70733d9e183SThomas Richter 	 * data and the time stamp of the event triggering the decoding.
70833d9e183SThomas Richter 	 */
70933d9e183SThomas Richter 	if (sfq->buffer == NULL) {
71033d9e183SThomas Richter 		sfq->buffer = buffer = auxtrace_buffer__next(queue,
71133d9e183SThomas Richter 							     sfq->buffer);
71233d9e183SThomas Richter 		if (!buffer) {
71333d9e183SThomas Richter 			*ts = ~0ULL;
71433d9e183SThomas Richter 			return 1;	/* Processing done on this queue */
71533d9e183SThomas Richter 		}
71633d9e183SThomas Richter 		/* Start with a new buffer on this queue */
71733d9e183SThomas Richter 		if (buffer->data) {
71833d9e183SThomas Richter 			buffer->use_size = buffer->size;
71933d9e183SThomas Richter 			buffer->use_data = buffer->data;
72033d9e183SThomas Richter 		}
721766e0618SThomas Richter 		if (sfq->logfile) {	/* Write into log file */
722766e0618SThomas Richter 			size_t rc = fwrite(buffer->data, buffer->size, 1,
723766e0618SThomas Richter 					   sfq->logfile);
724766e0618SThomas Richter 			if (rc != 1)
725766e0618SThomas Richter 				pr_err("Failed to write auxiliary data\n");
726766e0618SThomas Richter 		}
72733d9e183SThomas Richter 	} else
72833d9e183SThomas Richter 		buffer = sfq->buffer;
72933d9e183SThomas Richter 
73033d9e183SThomas Richter 	if (!buffer->data) {
73133d9e183SThomas Richter 		int fd = perf_data__fd(sfq->sf->session->data);
73233d9e183SThomas Richter 
73333d9e183SThomas Richter 		buffer->data = auxtrace_buffer__get_data(buffer, fd);
73433d9e183SThomas Richter 		if (!buffer->data)
73533d9e183SThomas Richter 			return -ENOMEM;
73633d9e183SThomas Richter 		buffer->use_size = buffer->size;
73733d9e183SThomas Richter 		buffer->use_data = buffer->data;
738766e0618SThomas Richter 
739766e0618SThomas Richter 		if (sfq->logfile) {	/* Write into log file */
740766e0618SThomas Richter 			size_t rc = fwrite(buffer->data, buffer->size, 1,
741766e0618SThomas Richter 					   sfq->logfile);
742766e0618SThomas Richter 			if (rc != 1)
743766e0618SThomas Richter 				pr_err("Failed to write auxiliary data\n");
744766e0618SThomas Richter 		}
74533d9e183SThomas Richter 	}
74633d9e183SThomas Richter 	pr_debug4("%s queue_nr:%d buffer:%" PRId64 " offset:%#" PRIx64 " size:%#zx rest:%#zx\n",
74733d9e183SThomas Richter 		  __func__, sfq->queue_nr, buffer->buffer_nr, buffer->offset,
74833d9e183SThomas Richter 		  buffer->size, buffer->use_size);
74933d9e183SThomas Richter 	err = s390_cpumsf_samples(sfq, ts);
75033d9e183SThomas Richter 
75133d9e183SThomas Richter 	/* If non-zero, there is either an error (err < 0) or the buffer is
75233d9e183SThomas Richter 	 * completely done (err > 0). The error is unrecoverable, usually
75333d9e183SThomas Richter 	 * some descriptors could not be read successfully, so continue with
75433d9e183SThomas Richter 	 * the next buffer.
75533d9e183SThomas Richter 	 * In both cases the parameter 'ts' has been updated.
75633d9e183SThomas Richter 	 */
75733d9e183SThomas Richter 	if (err) {
75833d9e183SThomas Richter 		sfq->buffer = NULL;
759e56fbc9dSArnaldo Carvalho de Melo 		list_del_init(&buffer->list);
76033d9e183SThomas Richter 		auxtrace_buffer__free(buffer);
76133d9e183SThomas Richter 		if (err > 0)		/* Buffer done, no error */
76233d9e183SThomas Richter 			err = 0;
76333d9e183SThomas Richter 	}
76433d9e183SThomas Richter 	return err;
76533d9e183SThomas Richter }
76633d9e183SThomas Richter 
76733d9e183SThomas Richter static struct s390_cpumsf_queue *
s390_cpumsf_alloc_queue(struct s390_cpumsf * sf,unsigned int queue_nr)76833d9e183SThomas Richter s390_cpumsf_alloc_queue(struct s390_cpumsf *sf, unsigned int queue_nr)
76933d9e183SThomas Richter {
77033d9e183SThomas Richter 	struct s390_cpumsf_queue *sfq;
77133d9e183SThomas Richter 
77233d9e183SThomas Richter 	sfq = zalloc(sizeof(struct s390_cpumsf_queue));
77333d9e183SThomas Richter 	if (sfq == NULL)
77433d9e183SThomas Richter 		return NULL;
77533d9e183SThomas Richter 
77633d9e183SThomas Richter 	sfq->sf = sf;
77733d9e183SThomas Richter 	sfq->queue_nr = queue_nr;
77833d9e183SThomas Richter 	sfq->cpu = -1;
779766e0618SThomas Richter 	if (sf->use_logfile) {
780766e0618SThomas Richter 		char *name;
781766e0618SThomas Richter 		int rc;
782766e0618SThomas Richter 
783766e0618SThomas Richter 		rc = (sf->logdir)
784766e0618SThomas Richter 			? asprintf(&name, "%s/aux.smp.%02x",
785766e0618SThomas Richter 				 sf->logdir, queue_nr)
786766e0618SThomas Richter 			: asprintf(&name, "aux.smp.%02x", queue_nr);
787766e0618SThomas Richter 		if (rc > 0)
788766e0618SThomas Richter 			sfq->logfile = fopen(name, "w");
789766e0618SThomas Richter 		if (sfq->logfile == NULL) {
790766e0618SThomas Richter 			pr_err("Failed to open auxiliary log file %s,"
791766e0618SThomas Richter 			       "continue...\n", name);
792766e0618SThomas Richter 			sf->use_logfile = false;
793766e0618SThomas Richter 		}
794766e0618SThomas Richter 		free(name);
795766e0618SThomas Richter 	}
79633d9e183SThomas Richter 	return sfq;
79733d9e183SThomas Richter }
79833d9e183SThomas Richter 
s390_cpumsf_setup_queue(struct s390_cpumsf * sf,struct auxtrace_queue * queue,unsigned int queue_nr,u64 ts)79933d9e183SThomas Richter static int s390_cpumsf_setup_queue(struct s390_cpumsf *sf,
80033d9e183SThomas Richter 				   struct auxtrace_queue *queue,
80133d9e183SThomas Richter 				   unsigned int queue_nr, u64 ts)
80233d9e183SThomas Richter {
80333d9e183SThomas Richter 	struct s390_cpumsf_queue *sfq = queue->priv;
80433d9e183SThomas Richter 
80533d9e183SThomas Richter 	if (list_empty(&queue->head))
80633d9e183SThomas Richter 		return 0;
80733d9e183SThomas Richter 
80833d9e183SThomas Richter 	if (sfq == NULL) {
80933d9e183SThomas Richter 		sfq = s390_cpumsf_alloc_queue(sf, queue_nr);
81033d9e183SThomas Richter 		if (!sfq)
81133d9e183SThomas Richter 			return -ENOMEM;
81233d9e183SThomas Richter 		queue->priv = sfq;
81333d9e183SThomas Richter 
81433d9e183SThomas Richter 		if (queue->cpu != -1)
81533d9e183SThomas Richter 			sfq->cpu = queue->cpu;
81633d9e183SThomas Richter 	}
81733d9e183SThomas Richter 	return auxtrace_heap__add(&sf->heap, queue_nr, ts);
81833d9e183SThomas Richter }
81933d9e183SThomas Richter 
s390_cpumsf_setup_queues(struct s390_cpumsf * sf,u64 ts)82033d9e183SThomas Richter static int s390_cpumsf_setup_queues(struct s390_cpumsf *sf, u64 ts)
82133d9e183SThomas Richter {
82233d9e183SThomas Richter 	unsigned int i;
82333d9e183SThomas Richter 	int ret = 0;
82433d9e183SThomas Richter 
82533d9e183SThomas Richter 	for (i = 0; i < sf->queues.nr_queues; i++) {
82633d9e183SThomas Richter 		ret = s390_cpumsf_setup_queue(sf, &sf->queues.queue_array[i],
82733d9e183SThomas Richter 					      i, ts);
82833d9e183SThomas Richter 		if (ret)
82933d9e183SThomas Richter 			break;
83033d9e183SThomas Richter 	}
83133d9e183SThomas Richter 	return ret;
83233d9e183SThomas Richter }
83333d9e183SThomas Richter 
s390_cpumsf_update_queues(struct s390_cpumsf * sf,u64 ts)83433d9e183SThomas Richter static int s390_cpumsf_update_queues(struct s390_cpumsf *sf, u64 ts)
83533d9e183SThomas Richter {
83633d9e183SThomas Richter 	if (!sf->queues.new_data)
83733d9e183SThomas Richter 		return 0;
83833d9e183SThomas Richter 
83933d9e183SThomas Richter 	sf->queues.new_data = false;
84033d9e183SThomas Richter 	return s390_cpumsf_setup_queues(sf, ts);
84133d9e183SThomas Richter }
84233d9e183SThomas Richter 
s390_cpumsf_process_queues(struct s390_cpumsf * sf,u64 timestamp)84333d9e183SThomas Richter static int s390_cpumsf_process_queues(struct s390_cpumsf *sf, u64 timestamp)
84433d9e183SThomas Richter {
84533d9e183SThomas Richter 	unsigned int queue_nr;
84633d9e183SThomas Richter 	u64 ts;
84733d9e183SThomas Richter 	int ret;
84833d9e183SThomas Richter 
84933d9e183SThomas Richter 	while (1) {
85033d9e183SThomas Richter 		struct auxtrace_queue *queue;
85133d9e183SThomas Richter 		struct s390_cpumsf_queue *sfq;
85233d9e183SThomas Richter 
85333d9e183SThomas Richter 		if (!sf->heap.heap_cnt)
85433d9e183SThomas Richter 			return 0;
85533d9e183SThomas Richter 
85633d9e183SThomas Richter 		if (sf->heap.heap_array[0].ordinal >= timestamp)
85733d9e183SThomas Richter 			return 0;
85833d9e183SThomas Richter 
85933d9e183SThomas Richter 		queue_nr = sf->heap.heap_array[0].queue_nr;
86033d9e183SThomas Richter 		queue = &sf->queues.queue_array[queue_nr];
86133d9e183SThomas Richter 		sfq = queue->priv;
86233d9e183SThomas Richter 
86333d9e183SThomas Richter 		auxtrace_heap__pop(&sf->heap);
86433d9e183SThomas Richter 		if (sf->heap.heap_cnt) {
86533d9e183SThomas Richter 			ts = sf->heap.heap_array[0].ordinal + 1;
86633d9e183SThomas Richter 			if (ts > timestamp)
86733d9e183SThomas Richter 				ts = timestamp;
86833d9e183SThomas Richter 		} else {
86933d9e183SThomas Richter 			ts = timestamp;
87033d9e183SThomas Richter 		}
87133d9e183SThomas Richter 
87233d9e183SThomas Richter 		ret = s390_cpumsf_run_decoder(sfq, &ts);
87333d9e183SThomas Richter 		if (ret < 0) {
87433d9e183SThomas Richter 			auxtrace_heap__add(&sf->heap, queue_nr, ts);
87533d9e183SThomas Richter 			return ret;
87633d9e183SThomas Richter 		}
87733d9e183SThomas Richter 		if (!ret) {
87833d9e183SThomas Richter 			ret = auxtrace_heap__add(&sf->heap, queue_nr, ts);
87933d9e183SThomas Richter 			if (ret < 0)
88033d9e183SThomas Richter 				return ret;
88133d9e183SThomas Richter 		}
88233d9e183SThomas Richter 	}
88333d9e183SThomas Richter 	return 0;
88433d9e183SThomas Richter }
88533d9e183SThomas Richter 
s390_cpumsf_synth_error(struct s390_cpumsf * sf,int code,int cpu,pid_t pid,pid_t tid,u64 ip,u64 timestamp)88633d9e183SThomas Richter static int s390_cpumsf_synth_error(struct s390_cpumsf *sf, int code, int cpu,
88716bd4321SAdrian Hunter 				   pid_t pid, pid_t tid, u64 ip, u64 timestamp)
88833d9e183SThomas Richter {
88933d9e183SThomas Richter 	char msg[MAX_AUXTRACE_ERROR_MSG];
89033d9e183SThomas Richter 	union perf_event event;
89133d9e183SThomas Richter 	int err;
89233d9e183SThomas Richter 
89333d9e183SThomas Richter 	strncpy(msg, "Lost Auxiliary Trace Buffer", sizeof(msg) - 1);
89433d9e183SThomas Richter 	auxtrace_synth_error(&event.auxtrace_error, PERF_AUXTRACE_ERROR_ITRACE,
89516bd4321SAdrian Hunter 			     code, cpu, pid, tid, ip, msg, timestamp);
89633d9e183SThomas Richter 
89733d9e183SThomas Richter 	err = perf_session__deliver_synth_event(sf->session, &event, NULL);
89833d9e183SThomas Richter 	if (err)
89933d9e183SThomas Richter 		pr_err("s390 Auxiliary Trace: failed to deliver error event,"
90033d9e183SThomas Richter 			"error %d\n", err);
90133d9e183SThomas Richter 	return err;
90233d9e183SThomas Richter }
90333d9e183SThomas Richter 
s390_cpumsf_lost(struct s390_cpumsf * sf,struct perf_sample * sample)90433d9e183SThomas Richter static int s390_cpumsf_lost(struct s390_cpumsf *sf, struct perf_sample *sample)
90533d9e183SThomas Richter {
90633d9e183SThomas Richter 	return s390_cpumsf_synth_error(sf, 1, sample->cpu,
90716bd4321SAdrian Hunter 				       sample->pid, sample->tid, 0,
90816bd4321SAdrian Hunter 				       sample->time);
90933d9e183SThomas Richter }
91033d9e183SThomas Richter 
91133d9e183SThomas Richter static int
s390_cpumsf_process_event(struct perf_session * session,union perf_event * event,struct perf_sample * sample,struct perf_tool * tool)9128dabe9c4SThomas Richter s390_cpumsf_process_event(struct perf_session *session,
91333d9e183SThomas Richter 			  union perf_event *event,
91433d9e183SThomas Richter 			  struct perf_sample *sample,
91533d9e183SThomas Richter 			  struct perf_tool *tool)
91633d9e183SThomas Richter {
91733d9e183SThomas Richter 	struct s390_cpumsf *sf = container_of(session->auxtrace,
91833d9e183SThomas Richter 					      struct s390_cpumsf,
91933d9e183SThomas Richter 					      auxtrace);
92033d9e183SThomas Richter 	u64 timestamp = sample->time;
92132dcd021SJiri Olsa 	struct evsel *ev_bc000;
9228dabe9c4SThomas Richter 
92333d9e183SThomas Richter 	int err = 0;
92433d9e183SThomas Richter 
92533d9e183SThomas Richter 	if (dump_trace)
92633d9e183SThomas Richter 		return 0;
92733d9e183SThomas Richter 
92833d9e183SThomas Richter 	if (!tool->ordered_events) {
92933d9e183SThomas Richter 		pr_err("s390 Auxiliary Trace requires ordered events\n");
93033d9e183SThomas Richter 		return -EINVAL;
93133d9e183SThomas Richter 	}
93233d9e183SThomas Richter 
9338dabe9c4SThomas Richter 	if (event->header.type == PERF_RECORD_SAMPLE &&
9348dabe9c4SThomas Richter 	    sample->raw_size) {
9358dabe9c4SThomas Richter 		/* Handle event with raw data */
9363ccf8a7bSArnaldo Carvalho de Melo 		ev_bc000 = evlist__event2evsel(session->evlist, event);
9378dabe9c4SThomas Richter 		if (ev_bc000 &&
9381fc632ceSJiri Olsa 		    ev_bc000->core.attr.config == PERF_EVENT_CPUM_CF_DIAG)
9398dabe9c4SThomas Richter 			err = s390_cpumcf_dumpctr(sf, sample);
9408dabe9c4SThomas Richter 		return err;
9418dabe9c4SThomas Richter 	}
9428dabe9c4SThomas Richter 
94333d9e183SThomas Richter 	if (event->header.type == PERF_RECORD_AUX &&
94433d9e183SThomas Richter 	    event->aux.flags & PERF_AUX_FLAG_TRUNCATED)
94533d9e183SThomas Richter 		return s390_cpumsf_lost(sf, sample);
94633d9e183SThomas Richter 
94733d9e183SThomas Richter 	if (timestamp) {
94833d9e183SThomas Richter 		err = s390_cpumsf_update_queues(sf, timestamp);
94933d9e183SThomas Richter 		if (!err)
95033d9e183SThomas Richter 			err = s390_cpumsf_process_queues(sf, timestamp);
95133d9e183SThomas Richter 	}
95233d9e183SThomas Richter 	return err;
95333d9e183SThomas Richter }
95433d9e183SThomas Richter 
95533d9e183SThomas Richter struct s390_cpumsf_synth {
95633d9e183SThomas Richter 	struct perf_tool cpumsf_tool;
95733d9e183SThomas Richter 	struct perf_session *session;
95833d9e183SThomas Richter };
95933d9e183SThomas Richter 
960b96e6615SThomas Richter static int
s390_cpumsf_process_auxtrace_event(struct perf_session * session,union perf_event * event __maybe_unused,struct perf_tool * tool __maybe_unused)9612b1444f2SThomas Richter s390_cpumsf_process_auxtrace_event(struct perf_session *session,
962b96e6615SThomas Richter 				   union perf_event *event __maybe_unused,
963b96e6615SThomas Richter 				   struct perf_tool *tool __maybe_unused)
964b96e6615SThomas Richter {
9652b1444f2SThomas Richter 	struct s390_cpumsf *sf = container_of(session->auxtrace,
9662b1444f2SThomas Richter 					      struct s390_cpumsf,
9672b1444f2SThomas Richter 					      auxtrace);
9682b1444f2SThomas Richter 
9692b1444f2SThomas Richter 	int fd = perf_data__fd(session->data);
9702b1444f2SThomas Richter 	struct auxtrace_buffer *buffer;
9712b1444f2SThomas Richter 	off_t data_offset;
9722b1444f2SThomas Richter 	int err;
9732b1444f2SThomas Richter 
97433d9e183SThomas Richter 	if (sf->data_queued)
97533d9e183SThomas Richter 		return 0;
97633d9e183SThomas Richter 
9772b1444f2SThomas Richter 	if (perf_data__is_pipe(session->data)) {
9782b1444f2SThomas Richter 		data_offset = 0;
9792b1444f2SThomas Richter 	} else {
9802b1444f2SThomas Richter 		data_offset = lseek(fd, 0, SEEK_CUR);
9812b1444f2SThomas Richter 		if (data_offset == -1)
9822b1444f2SThomas Richter 			return -errno;
9832b1444f2SThomas Richter 	}
9842b1444f2SThomas Richter 
9852b1444f2SThomas Richter 	err = auxtrace_queues__add_event(&sf->queues, session, event,
9862b1444f2SThomas Richter 					 data_offset, &buffer);
9872b1444f2SThomas Richter 	if (err)
9882b1444f2SThomas Richter 		return err;
9892b1444f2SThomas Richter 
9902b1444f2SThomas Richter 	/* Dump here after copying piped trace out of the pipe */
9912b1444f2SThomas Richter 	if (dump_trace) {
9922b1444f2SThomas Richter 		if (auxtrace_buffer__get_data(buffer, fd)) {
9932b1444f2SThomas Richter 			s390_cpumsf_dump_event(sf, buffer->data,
9942b1444f2SThomas Richter 					       buffer->size);
9952b1444f2SThomas Richter 			auxtrace_buffer__put_data(buffer);
9962b1444f2SThomas Richter 		}
9972b1444f2SThomas Richter 	}
998b96e6615SThomas Richter 	return 0;
999b96e6615SThomas Richter }
1000b96e6615SThomas Richter 
s390_cpumsf_free_events(struct perf_session * session __maybe_unused)100133d9e183SThomas Richter static void s390_cpumsf_free_events(struct perf_session *session __maybe_unused)
100233d9e183SThomas Richter {
100333d9e183SThomas Richter }
100433d9e183SThomas Richter 
s390_cpumsf_flush(struct perf_session * session __maybe_unused,struct perf_tool * tool __maybe_unused)1005b96e6615SThomas Richter static int s390_cpumsf_flush(struct perf_session *session __maybe_unused,
1006b96e6615SThomas Richter 			     struct perf_tool *tool __maybe_unused)
1007b96e6615SThomas Richter {
1008b96e6615SThomas Richter 	return 0;
1009b96e6615SThomas Richter }
1010b96e6615SThomas Richter 
s390_cpumsf_free_queues(struct perf_session * session)101133d9e183SThomas Richter static void s390_cpumsf_free_queues(struct perf_session *session)
1012b96e6615SThomas Richter {
1013b96e6615SThomas Richter 	struct s390_cpumsf *sf = container_of(session->auxtrace,
1014b96e6615SThomas Richter 					      struct s390_cpumsf,
1015b96e6615SThomas Richter 					      auxtrace);
1016b96e6615SThomas Richter 	struct auxtrace_queues *queues = &sf->queues;
1017b96e6615SThomas Richter 	unsigned int i;
1018b96e6615SThomas Richter 
1019766e0618SThomas Richter 	for (i = 0; i < queues->nr_queues; i++) {
1020766e0618SThomas Richter 		struct s390_cpumsf_queue *sfq = (struct s390_cpumsf_queue *)
1021766e0618SThomas Richter 						queues->queue_array[i].priv;
1022766e0618SThomas Richter 
10238dabe9c4SThomas Richter 		if (sfq != NULL) {
10248dabe9c4SThomas Richter 			if (sfq->logfile) {
1025766e0618SThomas Richter 				fclose(sfq->logfile);
1026766e0618SThomas Richter 				sfq->logfile = NULL;
1027766e0618SThomas Richter 			}
10288dabe9c4SThomas Richter 			if (sfq->logfile_ctr) {
10298dabe9c4SThomas Richter 				fclose(sfq->logfile_ctr);
10308dabe9c4SThomas Richter 				sfq->logfile_ctr = NULL;
10318dabe9c4SThomas Richter 			}
10328dabe9c4SThomas Richter 		}
1033b96e6615SThomas Richter 		zfree(&queues->queue_array[i].priv);
1034766e0618SThomas Richter 	}
1035b96e6615SThomas Richter 	auxtrace_queues__free(queues);
1036b96e6615SThomas Richter }
1037b96e6615SThomas Richter 
s390_cpumsf_free(struct perf_session * session)1038b96e6615SThomas Richter static void s390_cpumsf_free(struct perf_session *session)
1039b96e6615SThomas Richter {
1040b96e6615SThomas Richter 	struct s390_cpumsf *sf = container_of(session->auxtrace,
1041b96e6615SThomas Richter 					      struct s390_cpumsf,
1042b96e6615SThomas Richter 					      auxtrace);
1043b96e6615SThomas Richter 
1044b96e6615SThomas Richter 	auxtrace_heap__free(&sf->heap);
104533d9e183SThomas Richter 	s390_cpumsf_free_queues(session);
1046b96e6615SThomas Richter 	session->auxtrace = NULL;
1047d8f9da24SArnaldo Carvalho de Melo 	zfree(&sf->logdir);
1048b96e6615SThomas Richter 	free(sf);
1049b96e6615SThomas Richter }
1050b96e6615SThomas Richter 
1051113fcb46SAdrian Hunter static bool
s390_cpumsf_evsel_is_auxtrace(struct perf_session * session __maybe_unused,struct evsel * evsel)1052113fcb46SAdrian Hunter s390_cpumsf_evsel_is_auxtrace(struct perf_session *session __maybe_unused,
1053113fcb46SAdrian Hunter 			      struct evsel *evsel)
1054113fcb46SAdrian Hunter {
1055113fcb46SAdrian Hunter 	return evsel->core.attr.type == PERF_TYPE_RAW &&
1056113fcb46SAdrian Hunter 	       evsel->core.attr.config == PERF_EVENT_CPUM_SF_DIAG;
1057113fcb46SAdrian Hunter }
1058113fcb46SAdrian Hunter 
s390_cpumsf_get_type(const char * cpuid)10592b1444f2SThomas Richter static int s390_cpumsf_get_type(const char *cpuid)
10602b1444f2SThomas Richter {
10612b1444f2SThomas Richter 	int ret, family = 0;
10622b1444f2SThomas Richter 
10632b1444f2SThomas Richter 	ret = sscanf(cpuid, "%*[^,],%u", &family);
10642b1444f2SThomas Richter 	return (ret == 1) ? family : 0;
10652b1444f2SThomas Richter }
10662b1444f2SThomas Richter 
106733d9e183SThomas Richter /* Check itrace options set on perf report command.
106833d9e183SThomas Richter  * Return true, if none are set or all options specified can be
1069766e0618SThomas Richter  * handled on s390 (currently only option 'd' for logging.
107033d9e183SThomas Richter  * Return false otherwise.
107133d9e183SThomas Richter  */
check_auxtrace_itrace(struct itrace_synth_opts * itops)107233d9e183SThomas Richter static bool check_auxtrace_itrace(struct itrace_synth_opts *itops)
107333d9e183SThomas Richter {
1074766e0618SThomas Richter 	bool ison = false;
1075766e0618SThomas Richter 
107633d9e183SThomas Richter 	if (!itops || !itops->set)
107733d9e183SThomas Richter 		return true;
1078766e0618SThomas Richter 	ison = itops->inject || itops->instructions || itops->branches ||
1079766e0618SThomas Richter 		itops->transactions || itops->ptwrites ||
1080766e0618SThomas Richter 		itops->pwr_events || itops->errors ||
1081766e0618SThomas Richter 		itops->dont_decode || itops->calls || itops->returns ||
1082766e0618SThomas Richter 		itops->callchain || itops->thread_stack ||
1083ec90e42cSAdrian Hunter 		itops->last_branch || itops->add_callchain ||
1084ec90e42cSAdrian Hunter 		itops->add_last_branch;
1085766e0618SThomas Richter 	if (!ison)
1086766e0618SThomas Richter 		return true;
1087766e0618SThomas Richter 	pr_err("Unsupported --itrace options specified\n");
108833d9e183SThomas Richter 	return false;
108933d9e183SThomas Richter }
109033d9e183SThomas Richter 
1091766e0618SThomas Richter /* Check for AUXTRACE dump directory if it is needed.
1092766e0618SThomas Richter  * On failure print an error message but continue.
1093766e0618SThomas Richter  * Return 0 on wrong keyword in config file and 1 otherwise.
1094766e0618SThomas Richter  */
s390_cpumsf__config(const char * var,const char * value,void * cb)1095766e0618SThomas Richter static int s390_cpumsf__config(const char *var, const char *value, void *cb)
1096766e0618SThomas Richter {
1097766e0618SThomas Richter 	struct s390_cpumsf *sf = cb;
1098766e0618SThomas Richter 	struct stat stbuf;
1099766e0618SThomas Richter 	int rc;
1100766e0618SThomas Richter 
1101766e0618SThomas Richter 	if (strcmp(var, "auxtrace.dumpdir"))
1102766e0618SThomas Richter 		return 0;
1103766e0618SThomas Richter 	sf->logdir = strdup(value);
1104766e0618SThomas Richter 	if (sf->logdir == NULL) {
1105766e0618SThomas Richter 		pr_err("Failed to find auxtrace log directory %s,"
1106766e0618SThomas Richter 		       " continue with current directory...\n", value);
1107766e0618SThomas Richter 		return 1;
1108766e0618SThomas Richter 	}
1109766e0618SThomas Richter 	rc = stat(sf->logdir, &stbuf);
1110766e0618SThomas Richter 	if (rc == -1 || !S_ISDIR(stbuf.st_mode)) {
1111766e0618SThomas Richter 		pr_err("Missing auxtrace log directory %s,"
1112766e0618SThomas Richter 		       " continue with current directory...\n", value);
1113d8f9da24SArnaldo Carvalho de Melo 		zfree(&sf->logdir);
1114766e0618SThomas Richter 	}
1115766e0618SThomas Richter 	return 1;
1116766e0618SThomas Richter }
1117766e0618SThomas Richter 
s390_cpumsf_process_auxtrace_info(union perf_event * event,struct perf_session * session)1118b96e6615SThomas Richter int s390_cpumsf_process_auxtrace_info(union perf_event *event,
1119b96e6615SThomas Richter 				      struct perf_session *session)
1120b96e6615SThomas Richter {
112172932371SJiri Olsa 	struct perf_record_auxtrace_info *auxtrace_info = &event->auxtrace_info;
1122b96e6615SThomas Richter 	struct s390_cpumsf *sf;
1123b96e6615SThomas Richter 	int err;
1124b96e6615SThomas Richter 
112572932371SJiri Olsa 	if (auxtrace_info->header.size < sizeof(struct perf_record_auxtrace_info))
1126b96e6615SThomas Richter 		return -EINVAL;
1127b96e6615SThomas Richter 
1128b96e6615SThomas Richter 	sf = zalloc(sizeof(struct s390_cpumsf));
1129b96e6615SThomas Richter 	if (sf == NULL)
1130b96e6615SThomas Richter 		return -ENOMEM;
1131b96e6615SThomas Richter 
113233d9e183SThomas Richter 	if (!check_auxtrace_itrace(session->itrace_synth_opts)) {
113333d9e183SThomas Richter 		err = -EINVAL;
113433d9e183SThomas Richter 		goto err_free;
113533d9e183SThomas Richter 	}
1136766e0618SThomas Richter 	sf->use_logfile = session->itrace_synth_opts->log;
1137766e0618SThomas Richter 	if (sf->use_logfile)
1138766e0618SThomas Richter 		perf_config(s390_cpumsf__config, sf);
113933d9e183SThomas Richter 
1140b96e6615SThomas Richter 	err = auxtrace_queues__init(&sf->queues);
1141b96e6615SThomas Richter 	if (err)
1142b96e6615SThomas Richter 		goto err_free;
1143b96e6615SThomas Richter 
1144b96e6615SThomas Richter 	sf->session = session;
1145b96e6615SThomas Richter 	sf->machine = &session->machines.host; /* No kvm support */
1146b96e6615SThomas Richter 	sf->auxtrace_type = auxtrace_info->type;
1147b96e6615SThomas Richter 	sf->pmu_type = PERF_TYPE_RAW;
11482b1444f2SThomas Richter 	sf->machine_type = s390_cpumsf_get_type(session->evlist->env->cpuid);
1149b96e6615SThomas Richter 
1150b96e6615SThomas Richter 	sf->auxtrace.process_event = s390_cpumsf_process_event;
1151b96e6615SThomas Richter 	sf->auxtrace.process_auxtrace_event = s390_cpumsf_process_auxtrace_event;
1152b96e6615SThomas Richter 	sf->auxtrace.flush_events = s390_cpumsf_flush;
1153b96e6615SThomas Richter 	sf->auxtrace.free_events = s390_cpumsf_free_events;
1154b96e6615SThomas Richter 	sf->auxtrace.free = s390_cpumsf_free;
1155113fcb46SAdrian Hunter 	sf->auxtrace.evsel_is_auxtrace = s390_cpumsf_evsel_is_auxtrace;
1156b96e6615SThomas Richter 	session->auxtrace = &sf->auxtrace;
1157b96e6615SThomas Richter 
115833d9e183SThomas Richter 	if (dump_trace)
1159b96e6615SThomas Richter 		return 0;
1160b96e6615SThomas Richter 
116133d9e183SThomas Richter 	err = auxtrace_queues__process_index(&sf->queues, session);
116233d9e183SThomas Richter 	if (err)
116333d9e183SThomas Richter 		goto err_free_queues;
116433d9e183SThomas Richter 
116533d9e183SThomas Richter 	if (sf->queues.populated)
116633d9e183SThomas Richter 		sf->data_queued = true;
116733d9e183SThomas Richter 
116833d9e183SThomas Richter 	return 0;
116933d9e183SThomas Richter 
117033d9e183SThomas Richter err_free_queues:
117133d9e183SThomas Richter 	auxtrace_queues__free(&sf->queues);
117233d9e183SThomas Richter 	session->auxtrace = NULL;
1173b96e6615SThomas Richter err_free:
1174d8f9da24SArnaldo Carvalho de Melo 	zfree(&sf->logdir);
1175b96e6615SThomas Richter 	free(sf);
1176b96e6615SThomas Richter 	return err;
1177b96e6615SThomas Richter }
1178