18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright IBM Corp. 2019
48c2ecf20Sopenharmony_ci * Author(s): Thomas Richter <tmricht@linux.ibm.com>
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify
78c2ecf20Sopenharmony_ci * it under the terms of the GNU General Public License (version 2 only)
88c2ecf20Sopenharmony_ci * as published by the Free Software Foundation.
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * Architecture specific trace_event function. Save event's bc000 raw data
118c2ecf20Sopenharmony_ci * to file. File name is aux.ctr.## where ## stands for the CPU number the
128c2ecf20Sopenharmony_ci * sample was taken from.
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <unistd.h>
168c2ecf20Sopenharmony_ci#include <stdio.h>
178c2ecf20Sopenharmony_ci#include <string.h>
188c2ecf20Sopenharmony_ci#include <inttypes.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#include <sys/stat.h>
218c2ecf20Sopenharmony_ci#include <linux/compiler.h>
228c2ecf20Sopenharmony_ci#include <asm/byteorder.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#include "debug.h"
258c2ecf20Sopenharmony_ci#include "session.h"
268c2ecf20Sopenharmony_ci#include "evlist.h"
278c2ecf20Sopenharmony_ci#include "color.h"
288c2ecf20Sopenharmony_ci#include "sample-raw.h"
298c2ecf20Sopenharmony_ci#include "s390-cpumcf-kernel.h"
308c2ecf20Sopenharmony_ci#include "pmu-events/pmu-events.h"
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistatic size_t ctrset_size(struct cf_ctrset_entry *set)
338c2ecf20Sopenharmony_ci{
348c2ecf20Sopenharmony_ci	return sizeof(*set) + set->ctr * sizeof(u64);
358c2ecf20Sopenharmony_ci}
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistatic bool ctrset_valid(struct cf_ctrset_entry *set)
388c2ecf20Sopenharmony_ci{
398c2ecf20Sopenharmony_ci	return set->def == S390_CPUMCF_DIAG_DEF;
408c2ecf20Sopenharmony_ci}
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci/* CPU Measurement Counter Facility raw data is a byte stream. It is 8 byte
438c2ecf20Sopenharmony_ci * aligned and might have trailing padding bytes.
448c2ecf20Sopenharmony_ci * Display the raw data on screen.
458c2ecf20Sopenharmony_ci */
468c2ecf20Sopenharmony_cistatic bool s390_cpumcfdg_testctr(struct perf_sample *sample)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	size_t len = sample->raw_size, offset = 0;
498c2ecf20Sopenharmony_ci	unsigned char *buf = sample->raw_data;
508c2ecf20Sopenharmony_ci	struct cf_trailer_entry *te;
518c2ecf20Sopenharmony_ci	struct cf_ctrset_entry *cep, ce;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	if (!len)
548c2ecf20Sopenharmony_ci		return false;
558c2ecf20Sopenharmony_ci	while (offset < len) {
568c2ecf20Sopenharmony_ci		cep = (struct cf_ctrset_entry *)(buf + offset);
578c2ecf20Sopenharmony_ci		ce.def = be16_to_cpu(cep->def);
588c2ecf20Sopenharmony_ci		ce.set = be16_to_cpu(cep->set);
598c2ecf20Sopenharmony_ci		ce.ctr = be16_to_cpu(cep->ctr);
608c2ecf20Sopenharmony_ci		ce.res1 = be16_to_cpu(cep->res1);
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci		if (!ctrset_valid(&ce) || offset + ctrset_size(&ce) > len) {
638c2ecf20Sopenharmony_ci			/* Raw data for counter sets are always multiple of 8
648c2ecf20Sopenharmony_ci			 * bytes. Prepending a 4 bytes size field to the
658c2ecf20Sopenharmony_ci			 * raw data block in the sample causes the perf tool
668c2ecf20Sopenharmony_ci			 * to append 4 padding bytes to make the raw data part
678c2ecf20Sopenharmony_ci			 * of the sample a multiple of eight bytes again.
688c2ecf20Sopenharmony_ci			 *
698c2ecf20Sopenharmony_ci			 * If the last entry (trailer) is 4 bytes off the raw
708c2ecf20Sopenharmony_ci			 * area data end, all is good.
718c2ecf20Sopenharmony_ci			 */
728c2ecf20Sopenharmony_ci			if (len - offset - sizeof(*te) == 4)
738c2ecf20Sopenharmony_ci				break;
748c2ecf20Sopenharmony_ci			pr_err("Invalid counter set entry at %zd\n", offset);
758c2ecf20Sopenharmony_ci			return false;
768c2ecf20Sopenharmony_ci		}
778c2ecf20Sopenharmony_ci		offset += ctrset_size(&ce);
788c2ecf20Sopenharmony_ci	}
798c2ecf20Sopenharmony_ci	return true;
808c2ecf20Sopenharmony_ci}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci/* Dump event bc000 on screen, already tested on correctness. */
838c2ecf20Sopenharmony_cistatic void s390_cpumcfdg_dumptrail(const char *color, size_t offset,
848c2ecf20Sopenharmony_ci				    struct cf_trailer_entry *tep)
858c2ecf20Sopenharmony_ci{
868c2ecf20Sopenharmony_ci	struct cf_trailer_entry  te;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	te.flags = be64_to_cpu(tep->flags);
898c2ecf20Sopenharmony_ci	te.cfvn = be16_to_cpu(tep->cfvn);
908c2ecf20Sopenharmony_ci	te.csvn = be16_to_cpu(tep->csvn);
918c2ecf20Sopenharmony_ci	te.cpu_speed = be32_to_cpu(tep->cpu_speed);
928c2ecf20Sopenharmony_ci	te.timestamp = be64_to_cpu(tep->timestamp);
938c2ecf20Sopenharmony_ci	te.progusage1 = be64_to_cpu(tep->progusage1);
948c2ecf20Sopenharmony_ci	te.progusage2 = be64_to_cpu(tep->progusage2);
958c2ecf20Sopenharmony_ci	te.progusage3 = be64_to_cpu(tep->progusage3);
968c2ecf20Sopenharmony_ci	te.tod_base = be64_to_cpu(tep->tod_base);
978c2ecf20Sopenharmony_ci	te.mach_type = be16_to_cpu(tep->mach_type);
988c2ecf20Sopenharmony_ci	te.res1 = be16_to_cpu(tep->res1);
998c2ecf20Sopenharmony_ci	te.res2 = be32_to_cpu(tep->res2);
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	color_fprintf(stdout, color, "    [%#08zx] Trailer:%c%c%c%c%c"
1028c2ecf20Sopenharmony_ci		      " Cfvn:%d Csvn:%d Speed:%d TOD:%#llx\n",
1038c2ecf20Sopenharmony_ci		      offset, te.clock_base ? 'T' : ' ',
1048c2ecf20Sopenharmony_ci		      te.speed ? 'S' : ' ', te.mtda ? 'M' : ' ',
1058c2ecf20Sopenharmony_ci		      te.caca ? 'C' : ' ', te.lcda ? 'L' : ' ',
1068c2ecf20Sopenharmony_ci		      te.cfvn, te.csvn, te.cpu_speed, te.timestamp);
1078c2ecf20Sopenharmony_ci	color_fprintf(stdout, color, "\t\t1:%lx 2:%lx 3:%lx TOD-Base:%#llx"
1088c2ecf20Sopenharmony_ci		      " Type:%x\n\n",
1098c2ecf20Sopenharmony_ci		      te.progusage1, te.progusage2, te.progusage3,
1108c2ecf20Sopenharmony_ci		      te.tod_base, te.mach_type);
1118c2ecf20Sopenharmony_ci}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci/* Return starting number of a counter set */
1148c2ecf20Sopenharmony_cistatic int get_counterset_start(int setnr)
1158c2ecf20Sopenharmony_ci{
1168c2ecf20Sopenharmony_ci	switch (setnr) {
1178c2ecf20Sopenharmony_ci	case CPUMF_CTR_SET_BASIC:		/* Basic counter set */
1188c2ecf20Sopenharmony_ci		return 0;
1198c2ecf20Sopenharmony_ci	case CPUMF_CTR_SET_USER:		/* Problem state counter set */
1208c2ecf20Sopenharmony_ci		return 32;
1218c2ecf20Sopenharmony_ci	case CPUMF_CTR_SET_CRYPTO:		/* Crypto counter set */
1228c2ecf20Sopenharmony_ci		return 64;
1238c2ecf20Sopenharmony_ci	case CPUMF_CTR_SET_EXT:			/* Extended counter set */
1248c2ecf20Sopenharmony_ci		return 128;
1258c2ecf20Sopenharmony_ci	case CPUMF_CTR_SET_MT_DIAG:		/* Diagnostic counter set */
1268c2ecf20Sopenharmony_ci		return 448;
1278c2ecf20Sopenharmony_ci	default:
1288c2ecf20Sopenharmony_ci		return -1;
1298c2ecf20Sopenharmony_ci	}
1308c2ecf20Sopenharmony_ci}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci/* Scan the PMU table and extract the logical name of a counter from the
1338c2ecf20Sopenharmony_ci * PMU events table. Input is the counter set and counter number with in the
1348c2ecf20Sopenharmony_ci * set. Construct the event number and use this as key. If they match return
1358c2ecf20Sopenharmony_ci * the name of this counter.
1368c2ecf20Sopenharmony_ci * If no match is found a NULL pointer is returned.
1378c2ecf20Sopenharmony_ci */
1388c2ecf20Sopenharmony_cistatic const char *get_counter_name(int set, int nr, struct pmu_events_map *map)
1398c2ecf20Sopenharmony_ci{
1408c2ecf20Sopenharmony_ci	int rc, event_nr, wanted = get_counterset_start(set) + nr;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	if (map) {
1438c2ecf20Sopenharmony_ci		struct pmu_event *evp = map->table;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci		for (; evp->name || evp->event || evp->desc; ++evp) {
1468c2ecf20Sopenharmony_ci			if (evp->name == NULL || evp->event == NULL)
1478c2ecf20Sopenharmony_ci				continue;
1488c2ecf20Sopenharmony_ci			rc = sscanf(evp->event, "event=%x", &event_nr);
1498c2ecf20Sopenharmony_ci			if (rc == 1 && event_nr == wanted)
1508c2ecf20Sopenharmony_ci				return evp->name;
1518c2ecf20Sopenharmony_ci		}
1528c2ecf20Sopenharmony_ci	}
1538c2ecf20Sopenharmony_ci	return NULL;
1548c2ecf20Sopenharmony_ci}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_cistatic void s390_cpumcfdg_dump(struct perf_sample *sample)
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci	size_t i, len = sample->raw_size, offset = 0;
1598c2ecf20Sopenharmony_ci	unsigned char *buf = sample->raw_data;
1608c2ecf20Sopenharmony_ci	const char *color = PERF_COLOR_BLUE;
1618c2ecf20Sopenharmony_ci	struct cf_ctrset_entry *cep, ce;
1628c2ecf20Sopenharmony_ci	struct pmu_events_map *map;
1638c2ecf20Sopenharmony_ci	struct perf_pmu pmu;
1648c2ecf20Sopenharmony_ci	u64 *p;
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	memset(&pmu, 0, sizeof(pmu));
1678c2ecf20Sopenharmony_ci	map = perf_pmu__find_map(&pmu);
1688c2ecf20Sopenharmony_ci	while (offset < len) {
1698c2ecf20Sopenharmony_ci		cep = (struct cf_ctrset_entry *)(buf + offset);
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci		ce.def = be16_to_cpu(cep->def);
1728c2ecf20Sopenharmony_ci		ce.set = be16_to_cpu(cep->set);
1738c2ecf20Sopenharmony_ci		ce.ctr = be16_to_cpu(cep->ctr);
1748c2ecf20Sopenharmony_ci		ce.res1 = be16_to_cpu(cep->res1);
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci		if (!ctrset_valid(&ce)) {	/* Print trailer */
1778c2ecf20Sopenharmony_ci			s390_cpumcfdg_dumptrail(color, offset,
1788c2ecf20Sopenharmony_ci						(struct cf_trailer_entry *)cep);
1798c2ecf20Sopenharmony_ci			return;
1808c2ecf20Sopenharmony_ci		}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci		color_fprintf(stdout, color, "    [%#08zx] Counterset:%d"
1838c2ecf20Sopenharmony_ci			      " Counters:%d\n", offset, ce.set, ce.ctr);
1848c2ecf20Sopenharmony_ci		for (i = 0, p = (u64 *)(cep + 1); i < ce.ctr; ++i, ++p) {
1858c2ecf20Sopenharmony_ci			const char *ev_name = get_counter_name(ce.set, i, map);
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci			color_fprintf(stdout, color,
1888c2ecf20Sopenharmony_ci				      "\tCounter:%03d %s Value:%#018lx\n", i,
1898c2ecf20Sopenharmony_ci				      ev_name ?: "<unknown>", be64_to_cpu(*p));
1908c2ecf20Sopenharmony_ci		}
1918c2ecf20Sopenharmony_ci		offset += ctrset_size(&ce);
1928c2ecf20Sopenharmony_ci	}
1938c2ecf20Sopenharmony_ci}
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci/* S390 specific trace event function. Check for PERF_RECORD_SAMPLE events
1968c2ecf20Sopenharmony_ci * and if the event was triggered by a counter set diagnostic event display
1978c2ecf20Sopenharmony_ci * its raw data.
1988c2ecf20Sopenharmony_ci * The function is only invoked when the dump flag -D is set.
1998c2ecf20Sopenharmony_ci */
2008c2ecf20Sopenharmony_civoid perf_evlist__s390_sample_raw(struct evlist *evlist, union perf_event *event,
2018c2ecf20Sopenharmony_ci				  struct perf_sample *sample)
2028c2ecf20Sopenharmony_ci{
2038c2ecf20Sopenharmony_ci	struct evsel *ev_bc000;
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	if (event->header.type != PERF_RECORD_SAMPLE)
2068c2ecf20Sopenharmony_ci		return;
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	ev_bc000 = perf_evlist__event2evsel(evlist, event);
2098c2ecf20Sopenharmony_ci	if (ev_bc000 == NULL ||
2108c2ecf20Sopenharmony_ci	    ev_bc000->core.attr.config != PERF_EVENT_CPUM_CF_DIAG)
2118c2ecf20Sopenharmony_ci		return;
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	/* Display raw data on screen */
2148c2ecf20Sopenharmony_ci	if (!s390_cpumcfdg_testctr(sample)) {
2158c2ecf20Sopenharmony_ci		pr_err("Invalid counter set data encountered\n");
2168c2ecf20Sopenharmony_ci		return;
2178c2ecf20Sopenharmony_ci	}
2188c2ecf20Sopenharmony_ci	s390_cpumcfdg_dump(sample);
2198c2ecf20Sopenharmony_ci}
220