xref: /openbmc/linux/tools/perf/arch/powerpc/util/header.c (revision b9df3997)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <sys/types.h>
3 #include <unistd.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <linux/stringify.h>
8 #include "header.h"
9 
10 #define mfspr(rn)       ({unsigned long rval; \
11 			 asm volatile("mfspr %0," __stringify(rn) \
12 				      : "=r" (rval)); rval; })
13 
14 #define SPRN_PVR        0x11F	/* Processor Version Register */
15 #define PVR_VER(pvr)    (((pvr) >>  16) & 0xFFFF) /* Version field */
16 #define PVR_REV(pvr)    (((pvr) >>   0) & 0xFFFF) /* Revison field */
17 
18 int
19 get_cpuid(char *buffer, size_t sz)
20 {
21 	unsigned long pvr;
22 	int nb;
23 
24 	pvr = mfspr(SPRN_PVR);
25 
26 	nb = scnprintf(buffer, sz, "%lu,%lu$", PVR_VER(pvr), PVR_REV(pvr));
27 
28 	/* look for end marker to ensure the entire data fit */
29 	if (strchr(buffer, '$')) {
30 		buffer[nb-1] = '\0';
31 		return 0;
32 	}
33 	return -1;
34 }
35 
36 char *
37 get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
38 {
39 	char *bufp;
40 
41 	if (asprintf(&bufp, "%.8lx", mfspr(SPRN_PVR)) < 0)
42 		bufp = NULL;
43 
44 	return bufp;
45 }
46