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