193115d32SThomas Richter // SPDX-License-Identifier: GPL-2.0
293115d32SThomas Richter /*
393115d32SThomas Richter * Copyright IBM Corp. 2019
493115d32SThomas Richter * Author(s): Thomas Richter <tmricht@linux.ibm.com>
593115d32SThomas Richter *
693115d32SThomas Richter * This program is free software; you can redistribute it and/or modify
793115d32SThomas Richter * it under the terms of the GNU General Public License (version 2 only)
893115d32SThomas Richter * as published by the Free Software Foundation.
993115d32SThomas Richter *
1093115d32SThomas Richter * Architecture specific trace_event function. Save event's bc000 raw data
1193115d32SThomas Richter * to file. File name is aux.ctr.## where ## stands for the CPU number the
1293115d32SThomas Richter * sample was taken from.
1393115d32SThomas Richter */
1493115d32SThomas Richter
1593115d32SThomas Richter #include <unistd.h>
1693115d32SThomas Richter #include <stdio.h>
1793115d32SThomas Richter #include <string.h>
1893115d32SThomas Richter #include <inttypes.h>
1993115d32SThomas Richter
2093115d32SThomas Richter #include <sys/stat.h>
2193115d32SThomas Richter #include <linux/compiler.h>
2293115d32SThomas Richter #include <asm/byteorder.h>
2393115d32SThomas Richter
2493115d32SThomas Richter #include "debug.h"
2593115d32SThomas Richter #include "session.h"
2693115d32SThomas Richter #include "evlist.h"
2793115d32SThomas Richter #include "color.h"
2893115d32SThomas Richter #include "sample-raw.h"
2993115d32SThomas Richter #include "s390-cpumcf-kernel.h"
30c4ac7f75SIan Rogers #include "util/pmu.h"
319823147dSArnaldo Carvalho de Melo #include "util/sample.h"
3293115d32SThomas Richter
ctrset_size(struct cf_ctrset_entry * set)3393115d32SThomas Richter static size_t ctrset_size(struct cf_ctrset_entry *set)
3493115d32SThomas Richter {
3593115d32SThomas Richter return sizeof(*set) + set->ctr * sizeof(u64);
3693115d32SThomas Richter }
3793115d32SThomas Richter
ctrset_valid(struct cf_ctrset_entry * set)3893115d32SThomas Richter static bool ctrset_valid(struct cf_ctrset_entry *set)
3993115d32SThomas Richter {
4093115d32SThomas Richter return set->def == S390_CPUMCF_DIAG_DEF;
4193115d32SThomas Richter }
4293115d32SThomas Richter
4393115d32SThomas Richter /* CPU Measurement Counter Facility raw data is a byte stream. It is 8 byte
4493115d32SThomas Richter * aligned and might have trailing padding bytes.
4593115d32SThomas Richter * Display the raw data on screen.
4693115d32SThomas Richter */
s390_cpumcfdg_testctr(struct perf_sample * sample)4793115d32SThomas Richter static bool s390_cpumcfdg_testctr(struct perf_sample *sample)
4893115d32SThomas Richter {
4993115d32SThomas Richter size_t len = sample->raw_size, offset = 0;
5093115d32SThomas Richter unsigned char *buf = sample->raw_data;
5193115d32SThomas Richter struct cf_trailer_entry *te;
5293115d32SThomas Richter struct cf_ctrset_entry *cep, ce;
5393115d32SThomas Richter
5493115d32SThomas Richter while (offset < len) {
5593115d32SThomas Richter cep = (struct cf_ctrset_entry *)(buf + offset);
5693115d32SThomas Richter ce.def = be16_to_cpu(cep->def);
5793115d32SThomas Richter ce.set = be16_to_cpu(cep->set);
5893115d32SThomas Richter ce.ctr = be16_to_cpu(cep->ctr);
5993115d32SThomas Richter ce.res1 = be16_to_cpu(cep->res1);
6093115d32SThomas Richter
6193115d32SThomas Richter if (!ctrset_valid(&ce) || offset + ctrset_size(&ce) > len) {
6293115d32SThomas Richter /* Raw data for counter sets are always multiple of 8
6393115d32SThomas Richter * bytes. Prepending a 4 bytes size field to the
6493115d32SThomas Richter * raw data block in the sample causes the perf tool
6593115d32SThomas Richter * to append 4 padding bytes to make the raw data part
6693115d32SThomas Richter * of the sample a multiple of eight bytes again.
6793115d32SThomas Richter *
6893115d32SThomas Richter * If the last entry (trailer) is 4 bytes off the raw
6993115d32SThomas Richter * area data end, all is good.
7093115d32SThomas Richter */
7193115d32SThomas Richter if (len - offset - sizeof(*te) == 4)
7293115d32SThomas Richter break;
7393115d32SThomas Richter pr_err("Invalid counter set entry at %zd\n", offset);
7493115d32SThomas Richter return false;
7593115d32SThomas Richter }
7693115d32SThomas Richter offset += ctrset_size(&ce);
7793115d32SThomas Richter }
7893115d32SThomas Richter return true;
7993115d32SThomas Richter }
8093115d32SThomas Richter
8193115d32SThomas Richter /* Dump event bc000 on screen, already tested on correctness. */
s390_cpumcfdg_dumptrail(const char * color,size_t offset,struct cf_trailer_entry * tep)8293115d32SThomas Richter static void s390_cpumcfdg_dumptrail(const char *color, size_t offset,
8393115d32SThomas Richter struct cf_trailer_entry *tep)
8493115d32SThomas Richter {
8593115d32SThomas Richter struct cf_trailer_entry te;
8693115d32SThomas Richter
8793115d32SThomas Richter te.flags = be64_to_cpu(tep->flags);
8893115d32SThomas Richter te.cfvn = be16_to_cpu(tep->cfvn);
8993115d32SThomas Richter te.csvn = be16_to_cpu(tep->csvn);
9093115d32SThomas Richter te.cpu_speed = be32_to_cpu(tep->cpu_speed);
9193115d32SThomas Richter te.timestamp = be64_to_cpu(tep->timestamp);
9293115d32SThomas Richter te.progusage1 = be64_to_cpu(tep->progusage1);
9393115d32SThomas Richter te.progusage2 = be64_to_cpu(tep->progusage2);
9493115d32SThomas Richter te.progusage3 = be64_to_cpu(tep->progusage3);
9593115d32SThomas Richter te.tod_base = be64_to_cpu(tep->tod_base);
9693115d32SThomas Richter te.mach_type = be16_to_cpu(tep->mach_type);
9793115d32SThomas Richter te.res1 = be16_to_cpu(tep->res1);
9893115d32SThomas Richter te.res2 = be32_to_cpu(tep->res2);
9993115d32SThomas Richter
10093115d32SThomas Richter color_fprintf(stdout, color, " [%#08zx] Trailer:%c%c%c%c%c"
10193115d32SThomas Richter " Cfvn:%d Csvn:%d Speed:%d TOD:%#llx\n",
10293115d32SThomas Richter offset, te.clock_base ? 'T' : ' ',
10393115d32SThomas Richter te.speed ? 'S' : ' ', te.mtda ? 'M' : ' ',
10493115d32SThomas Richter te.caca ? 'C' : ' ', te.lcda ? 'L' : ' ',
10593115d32SThomas Richter te.cfvn, te.csvn, te.cpu_speed, te.timestamp);
10693115d32SThomas Richter color_fprintf(stdout, color, "\t\t1:%lx 2:%lx 3:%lx TOD-Base:%#llx"
10793115d32SThomas Richter " Type:%x\n\n",
10893115d32SThomas Richter te.progusage1, te.progusage2, te.progusage3,
10993115d32SThomas Richter te.tod_base, te.mach_type);
11093115d32SThomas Richter }
11193115d32SThomas Richter
1123e4a1c53SThomas Richter /* Return starting number of a counter set */
get_counterset_start(int setnr)1133e4a1c53SThomas Richter static int get_counterset_start(int setnr)
1143e4a1c53SThomas Richter {
1153e4a1c53SThomas Richter switch (setnr) {
1163e4a1c53SThomas Richter case CPUMF_CTR_SET_BASIC: /* Basic counter set */
1173e4a1c53SThomas Richter return 0;
1183e4a1c53SThomas Richter case CPUMF_CTR_SET_USER: /* Problem state counter set */
1193e4a1c53SThomas Richter return 32;
1203e4a1c53SThomas Richter case CPUMF_CTR_SET_CRYPTO: /* Crypto counter set */
1213e4a1c53SThomas Richter return 64;
1223e4a1c53SThomas Richter case CPUMF_CTR_SET_EXT: /* Extended counter set */
1233e4a1c53SThomas Richter return 128;
1243e4a1c53SThomas Richter case CPUMF_CTR_SET_MT_DIAG: /* Diagnostic counter set */
1253e4a1c53SThomas Richter return 448;
126b539deafSThomas Richter case PERF_EVENT_PAI_NNPA_ALL: /* PAI NNPA counter set */
127b539deafSThomas Richter case PERF_EVENT_PAI_CRYPTO_ALL: /* PAI CRYPTO counter set */
128b539deafSThomas Richter return setnr;
1293e4a1c53SThomas Richter default:
1303e4a1c53SThomas Richter return -1;
1313e4a1c53SThomas Richter }
1323e4a1c53SThomas Richter }
1333e4a1c53SThomas Richter
134660842e4SIan Rogers struct get_counter_name_data {
135660842e4SIan Rogers int wanted;
136c4ac7f75SIan Rogers char *result;
137660842e4SIan Rogers };
138660842e4SIan Rogers
get_counter_name_callback(void * vdata,struct pmu_event_info * info)139c4ac7f75SIan Rogers static int get_counter_name_callback(void *vdata, struct pmu_event_info *info)
140660842e4SIan Rogers {
141660842e4SIan Rogers struct get_counter_name_data *data = vdata;
142660842e4SIan Rogers int rc, event_nr;
143c4ac7f75SIan Rogers const char *event_str;
144660842e4SIan Rogers
145c4ac7f75SIan Rogers if (info->str == NULL)
146660842e4SIan Rogers return 0;
147c4ac7f75SIan Rogers
148c4ac7f75SIan Rogers event_str = strstr(info->str, "event=");
149c4ac7f75SIan Rogers if (!event_str)
150c4ac7f75SIan Rogers return 0;
151c4ac7f75SIan Rogers
152c4ac7f75SIan Rogers rc = sscanf(event_str, "event=%x", &event_nr);
153660842e4SIan Rogers if (rc == 1 && event_nr == data->wanted) {
154c4ac7f75SIan Rogers data->result = strdup(info->name);
155660842e4SIan Rogers return 1; /* Terminate the search. */
156660842e4SIan Rogers }
157660842e4SIan Rogers return 0;
158660842e4SIan Rogers }
159660842e4SIan Rogers
160c4ac7f75SIan Rogers /* Scan the PMU and extract the logical name of a counter from the event. Input
161c4ac7f75SIan Rogers * is the counter set and counter number with in the set. Construct the event
162c4ac7f75SIan Rogers * number and use this as key. If they match return the name of this counter.
1633e4a1c53SThomas Richter * If no match is found a NULL pointer is returned.
1643e4a1c53SThomas Richter */
get_counter_name(int set,int nr,struct perf_pmu * pmu)165c4ac7f75SIan Rogers static char *get_counter_name(int set, int nr, struct perf_pmu *pmu)
1663e4a1c53SThomas Richter {
167660842e4SIan Rogers struct get_counter_name_data data = {
168660842e4SIan Rogers .wanted = get_counterset_start(set) + nr,
169660842e4SIan Rogers .result = NULL,
170660842e4SIan Rogers };
1713e4a1c53SThomas Richter
172c4ac7f75SIan Rogers if (!pmu)
1733e4a1c53SThomas Richter return NULL;
174660842e4SIan Rogers
175cd4e1efbSIan Rogers perf_pmu__for_each_event(pmu, /*skip_duplicate_pmus=*/ true,
176cd4e1efbSIan Rogers &data, get_counter_name_callback);
177660842e4SIan Rogers return data.result;
1783e4a1c53SThomas Richter }
1793e4a1c53SThomas Richter
s390_cpumcfdg_dump(struct perf_pmu * pmu,struct perf_sample * sample)180c4ac7f75SIan Rogers static void s390_cpumcfdg_dump(struct perf_pmu *pmu, struct perf_sample *sample)
18193115d32SThomas Richter {
18293115d32SThomas Richter size_t i, len = sample->raw_size, offset = 0;
18393115d32SThomas Richter unsigned char *buf = sample->raw_data;
18493115d32SThomas Richter const char *color = PERF_COLOR_BLUE;
18593115d32SThomas Richter struct cf_ctrset_entry *cep, ce;
18693115d32SThomas Richter u64 *p;
18793115d32SThomas Richter
18893115d32SThomas Richter while (offset < len) {
18993115d32SThomas Richter cep = (struct cf_ctrset_entry *)(buf + offset);
19093115d32SThomas Richter
19193115d32SThomas Richter ce.def = be16_to_cpu(cep->def);
19293115d32SThomas Richter ce.set = be16_to_cpu(cep->set);
19393115d32SThomas Richter ce.ctr = be16_to_cpu(cep->ctr);
19493115d32SThomas Richter ce.res1 = be16_to_cpu(cep->res1);
19593115d32SThomas Richter
19693115d32SThomas Richter if (!ctrset_valid(&ce)) { /* Print trailer */
19793115d32SThomas Richter s390_cpumcfdg_dumptrail(color, offset,
19893115d32SThomas Richter (struct cf_trailer_entry *)cep);
19993115d32SThomas Richter return;
20093115d32SThomas Richter }
20193115d32SThomas Richter
20293115d32SThomas Richter color_fprintf(stdout, color, " [%#08zx] Counterset:%d"
20393115d32SThomas Richter " Counters:%d\n", offset, ce.set, ce.ctr);
2043e4a1c53SThomas Richter for (i = 0, p = (u64 *)(cep + 1); i < ce.ctr; ++i, ++p) {
205c4ac7f75SIan Rogers char *ev_name = get_counter_name(ce.set, i, pmu);
2063e4a1c53SThomas Richter
20793115d32SThomas Richter color_fprintf(stdout, color,
2083e4a1c53SThomas Richter "\tCounter:%03d %s Value:%#018lx\n", i,
2093e4a1c53SThomas Richter ev_name ?: "<unknown>", be64_to_cpu(*p));
210c4ac7f75SIan Rogers free(ev_name);
2113e4a1c53SThomas Richter }
21293115d32SThomas Richter offset += ctrset_size(&ce);
21393115d32SThomas Richter }
21493115d32SThomas Richter }
21593115d32SThomas Richter
216b539deafSThomas Richter #pragma GCC diagnostic push
217b539deafSThomas Richter #pragma GCC diagnostic ignored "-Wpacked"
218b539deafSThomas Richter #pragma GCC diagnostic ignored "-Wattributes"
219b539deafSThomas Richter /*
220b539deafSThomas Richter * Check for consistency of PAI_CRYPTO/PAI_NNPA raw data.
22193115d32SThomas Richter */
222b539deafSThomas Richter struct pai_data { /* Event number and value */
223b539deafSThomas Richter u16 event_nr;
224b539deafSThomas Richter u64 event_val;
225b539deafSThomas Richter } __packed;
226b539deafSThomas Richter
227b539deafSThomas Richter #pragma GCC diagnostic pop
228b539deafSThomas Richter
229b539deafSThomas Richter /*
230b539deafSThomas Richter * Test for valid raw data. At least one PAI event should be in the raw
231b539deafSThomas Richter * data section.
232b539deafSThomas Richter */
s390_pai_all_test(struct perf_sample * sample)233b539deafSThomas Richter static bool s390_pai_all_test(struct perf_sample *sample)
23493115d32SThomas Richter {
235b539deafSThomas Richter size_t len = sample->raw_size;
236b539deafSThomas Richter
237*8aa1e6e2SThomas Richter if (len < 0xa)
238b539deafSThomas Richter return false;
239b539deafSThomas Richter return true;
240b539deafSThomas Richter }
241b539deafSThomas Richter
s390_pai_all_dump(struct evsel * evsel,struct perf_sample * sample)242b539deafSThomas Richter static void s390_pai_all_dump(struct evsel *evsel, struct perf_sample *sample)
243b539deafSThomas Richter {
244b539deafSThomas Richter size_t len = sample->raw_size, offset = 0;
245b539deafSThomas Richter unsigned char *p = sample->raw_data;
246b539deafSThomas Richter const char *color = PERF_COLOR_BLUE;
247b539deafSThomas Richter struct pai_data pai_data;
248b539deafSThomas Richter char *ev_name;
249b539deafSThomas Richter
250b539deafSThomas Richter while (offset < len) {
251b539deafSThomas Richter memcpy(&pai_data.event_nr, p, sizeof(pai_data.event_nr));
252b539deafSThomas Richter pai_data.event_nr = be16_to_cpu(pai_data.event_nr);
253b539deafSThomas Richter p += sizeof(pai_data.event_nr);
254b539deafSThomas Richter offset += sizeof(pai_data.event_nr);
255b539deafSThomas Richter
256b539deafSThomas Richter memcpy(&pai_data.event_val, p, sizeof(pai_data.event_val));
257b539deafSThomas Richter pai_data.event_val = be64_to_cpu(pai_data.event_val);
258b539deafSThomas Richter p += sizeof(pai_data.event_val);
259b539deafSThomas Richter offset += sizeof(pai_data.event_val);
260b539deafSThomas Richter
261b539deafSThomas Richter ev_name = get_counter_name(evsel->core.attr.config,
262b539deafSThomas Richter pai_data.event_nr, evsel->pmu);
263b539deafSThomas Richter color_fprintf(stdout, color, "\tCounter:%03d %s Value:%#018lx\n",
264b539deafSThomas Richter pai_data.event_nr, ev_name ?: "<unknown>",
265b539deafSThomas Richter pai_data.event_val);
266b539deafSThomas Richter free(ev_name);
267b539deafSThomas Richter
268b539deafSThomas Richter if (offset + 0xa > len)
269b539deafSThomas Richter break;
270b539deafSThomas Richter }
271b539deafSThomas Richter color_fprintf(stdout, color, "\n");
272b539deafSThomas Richter }
273b539deafSThomas Richter
274b539deafSThomas Richter /* S390 specific trace event function. Check for PERF_RECORD_SAMPLE events
275b539deafSThomas Richter * and if the event was triggered by a
276b539deafSThomas Richter * - counter set diagnostic event
277b539deafSThomas Richter * - processor activity assist (PAI) crypto counter event
278b539deafSThomas Richter * - processor activity assist (PAI) neural network processor assist (NNPA)
279b539deafSThomas Richter * counter event
280b539deafSThomas Richter * display its raw data.
281b539deafSThomas Richter * The function is only invoked when the dump flag -D is set.
282b539deafSThomas Richter *
283b539deafSThomas Richter * Function evlist__s390_sample_raw() is defined as call back after it has
284b539deafSThomas Richter * been verified that the perf.data file was created on s390 platform.
285b539deafSThomas Richter */
evlist__s390_sample_raw(struct evlist * evlist,union perf_event * event,struct perf_sample * sample)286b539deafSThomas Richter void evlist__s390_sample_raw(struct evlist *evlist, union perf_event *event,
287b539deafSThomas Richter struct perf_sample *sample)
288b539deafSThomas Richter {
289b539deafSThomas Richter const char *pai_name;
290c4ac7f75SIan Rogers struct evsel *evsel;
29193115d32SThomas Richter
29293115d32SThomas Richter if (event->header.type != PERF_RECORD_SAMPLE)
29393115d32SThomas Richter return;
29493115d32SThomas Richter
295c4ac7f75SIan Rogers evsel = evlist__event2evsel(evlist, event);
296b539deafSThomas Richter if (!evsel)
29793115d32SThomas Richter return;
29893115d32SThomas Richter
299*8aa1e6e2SThomas Richter /* Check for raw data in sample */
300*8aa1e6e2SThomas Richter if (!sample->raw_size || !sample->raw_data)
301*8aa1e6e2SThomas Richter return;
302*8aa1e6e2SThomas Richter
30393115d32SThomas Richter /* Display raw data on screen */
304b539deafSThomas Richter if (evsel->core.attr.config == PERF_EVENT_CPUM_CF_DIAG) {
305b539deafSThomas Richter if (!evsel->pmu)
306b539deafSThomas Richter evsel->pmu = perf_pmus__find("cpum_cf");
307b539deafSThomas Richter if (!s390_cpumcfdg_testctr(sample))
30893115d32SThomas Richter pr_err("Invalid counter set data encountered\n");
309b539deafSThomas Richter else
310b539deafSThomas Richter s390_cpumcfdg_dump(evsel->pmu, sample);
31193115d32SThomas Richter return;
31293115d32SThomas Richter }
313b539deafSThomas Richter
314b539deafSThomas Richter switch (evsel->core.attr.config) {
315b539deafSThomas Richter case PERF_EVENT_PAI_NNPA_ALL:
316b539deafSThomas Richter pai_name = "NNPA_ALL";
317b539deafSThomas Richter break;
318b539deafSThomas Richter case PERF_EVENT_PAI_CRYPTO_ALL:
319b539deafSThomas Richter pai_name = "CRYPTO_ALL";
320b539deafSThomas Richter break;
321b539deafSThomas Richter default:
322b539deafSThomas Richter return;
323b539deafSThomas Richter }
324b539deafSThomas Richter
325b539deafSThomas Richter if (!s390_pai_all_test(sample)) {
326b539deafSThomas Richter pr_err("Invalid %s raw data encountered\n", pai_name);
327b539deafSThomas Richter } else {
328b539deafSThomas Richter if (!evsel->pmu)
329b539deafSThomas Richter evsel->pmu = perf_pmus__find_by_type(evsel->core.attr.type);
330b539deafSThomas Richter s390_pai_all_dump(evsel, sample);
331b539deafSThomas Richter }
33293115d32SThomas Richter }
333