1 // SPDX-License-Identifier: GPL-2.0
2 #include <sys/types.h>
3 #include <errno.h>
4 #include <unistd.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <linux/stringify.h>
9 #include "header.h"
10 #include "utils_header.h"
11 #include "metricgroup.h"
12 #include <api/fs/fs.h>
13 #include <sys/auxv.h>
14
is_compat_mode(void)15 static bool is_compat_mode(void)
16 {
17 unsigned long base_platform = getauxval(AT_BASE_PLATFORM);
18 unsigned long platform = getauxval(AT_PLATFORM);
19
20 if (!strcmp((char *)platform, (char *)base_platform))
21 return false;
22
23 return true;
24 }
25
26 int
get_cpuid(char * buffer,size_t sz,struct perf_cpu cpu __maybe_unused)27 get_cpuid(char *buffer, size_t sz, struct perf_cpu cpu __maybe_unused)
28 {
29 unsigned long pvr;
30 int nb;
31
32 pvr = mfspr(SPRN_PVR);
33
34 nb = scnprintf(buffer, sz, "%lu,%lu$", PVR_VER(pvr), PVR_REV(pvr));
35
36 /* look for end marker to ensure the entire data fit */
37 if (strchr(buffer, '$')) {
38 buffer[nb-1] = '\0';
39 return 0;
40 }
41 return ENOBUFS;
42 }
43
44 char *
get_cpuid_str(struct perf_cpu cpu __maybe_unused)45 get_cpuid_str(struct perf_cpu cpu __maybe_unused)
46 {
47 char *bufp;
48 unsigned long pvr;
49
50 /*
51 * IBM Power System supports compatible mode. That is
52 * Nth generation platform can support previous generation
53 * OS in a mode called compatibile mode. For ex. LPAR can be
54 * booted in a Power9 mode when the system is a Power10.
55 *
56 * In the compatible mode, care must be taken when generating
57 * PVR value. When read, PVR will be of the AT_BASE_PLATFORM
58 * To support generic events, return 0x00ffffff as pvr when
59 * booted in compat mode. Based on this pvr value, json will
60 * pick events from pmu-events/arch/powerpc/compat
61 */
62 if (!is_compat_mode())
63 pvr = mfspr(SPRN_PVR);
64 else
65 pvr = 0x00ffffff;
66
67 if (asprintf(&bufp, "0x%.8lx", pvr) < 0)
68 bufp = NULL;
69
70 return bufp;
71 }
72
arch_get_runtimeparam(const struct pmu_metric * pm)73 int arch_get_runtimeparam(const struct pmu_metric *pm)
74 {
75 int count;
76 char path[PATH_MAX] = "/devices/hv_24x7/interface/";
77
78 strcat(path, pm->aggr_mode == PerChip ? "sockets" : "coresperchip");
79 return sysfs__read_int(path, &count) < 0 ? 1 : count;
80 }
81