xref: /openbmc/linux/tools/perf/arch/powerpc/util/header.c (revision 82df5b73)
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 "metricgroup.h"
11 #include <api/fs/fs.h>
12 
13 #define mfspr(rn)       ({unsigned long rval; \
14 			 asm volatile("mfspr %0," __stringify(rn) \
15 				      : "=r" (rval)); rval; })
16 
17 #define SPRN_PVR        0x11F	/* Processor Version Register */
18 #define PVR_VER(pvr)    (((pvr) >>  16) & 0xFFFF) /* Version field */
19 #define PVR_REV(pvr)    (((pvr) >>   0) & 0xFFFF) /* Revison field */
20 
21 int
22 get_cpuid(char *buffer, size_t sz)
23 {
24 	unsigned long pvr;
25 	int nb;
26 
27 	pvr = mfspr(SPRN_PVR);
28 
29 	nb = scnprintf(buffer, sz, "%lu,%lu$", PVR_VER(pvr), PVR_REV(pvr));
30 
31 	/* look for end marker to ensure the entire data fit */
32 	if (strchr(buffer, '$')) {
33 		buffer[nb-1] = '\0';
34 		return 0;
35 	}
36 	return ENOBUFS;
37 }
38 
39 char *
40 get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
41 {
42 	char *bufp;
43 
44 	if (asprintf(&bufp, "%.8lx", mfspr(SPRN_PVR)) < 0)
45 		bufp = NULL;
46 
47 	return bufp;
48 }
49 
50 int arch_get_runtimeparam(void)
51 {
52 	int count;
53 	return sysfs__read_int("/devices/hv_24x7/interface/sockets", &count) < 0 ? 1 : count;
54 }
55