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