Merge tag 'clk-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / tools / perf / arch / powerpc / util / header.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
fbe96f29 2#include <sys/types.h>
f67001a4 3#include <errno.h>
fbe96f29
SE
4#include <unistd.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
531d2410 8#include <linux/stringify.h>
379649cf 9#include "header.h"
fbe96f29
SE
10
11#define mfspr(rn) ({unsigned long rval; \
12 asm volatile("mfspr %0," __stringify(rn) \
13 : "=r" (rval)); rval; })
14
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 */
18
19int
20get_cpuid(char *buffer, size_t sz)
21{
22 unsigned long pvr;
23 int nb;
24
25 pvr = mfspr(SPRN_PVR);
26
e7f01d1e 27 nb = scnprintf(buffer, sz, "%lu,%lu$", PVR_VER(pvr), PVR_REV(pvr));
fbe96f29
SE
28
29 /* look for end marker to ensure the entire data fit */
30 if (strchr(buffer, '$')) {
31 buffer[nb-1] = '\0';
32 return 0;
33 }
f67001a4 34 return ENOBUFS;
fbe96f29 35}
ce88f27c
SB
36
37char *
54e32dc0 38get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
ce88f27c
SB
39{
40 char *bufp;
41
42 if (asprintf(&bufp, "%.8lx", mfspr(SPRN_PVR)) < 0)
43 bufp = NULL;
44
45 return bufp;
46}