Linux 5.1.15
[linux/fpc-iii.git] / tools / perf / arch / powerpc / util / header.c
blob0b242664f5ea78ca8c97f92f5ff904b4b096b602
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 #include "util.h"
11 #define mfspr(rn) ({unsigned long rval; \
12 asm volatile("mfspr %0," __stringify(rn) \
13 : "=r" (rval)); rval; })
15 #define SPRN_PVR 0x11F /* Processor Version Register */
16 #define PVR_VER(pvr) (((pvr) >> 16) & 0xFFFF) /* Version field */
17 #define PVR_REV(pvr) (((pvr) >> 0) & 0xFFFF) /* Revison field */
19 int
20 get_cpuid(char *buffer, size_t sz)
22 unsigned long pvr;
23 int nb;
25 pvr = mfspr(SPRN_PVR);
27 nb = scnprintf(buffer, sz, "%lu,%lu$", PVR_VER(pvr), PVR_REV(pvr));
29 /* look for end marker to ensure the entire data fit */
30 if (strchr(buffer, '$')) {
31 buffer[nb-1] = '\0';
32 return 0;
34 return -1;
37 char *
38 get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
40 char *bufp;
42 if (asprintf(&bufp, "%.8lx", mfspr(SPRN_PVR)) < 0)
43 bufp = NULL;
45 return bufp;