Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2 | #include <sys/types.h> |
| 3 | #include <unistd.h> |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
Arnaldo Carvalho de Melo | 531d241 | 2016-03-23 15:16:55 -0300 | [diff] [blame] | 7 | #include <linux/stringify.h> |
Sukadev Bhattiprolu | 379649c | 2016-03-28 09:31:41 -0300 | [diff] [blame] | 8 | #include "header.h" |
| 9 | #include "util.h" |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 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 | |
| 19 | int |
| 20 | get_cpuid(char *buffer, size_t sz) |
| 21 | { |
| 22 | unsigned long pvr; |
| 23 | int nb; |
| 24 | |
| 25 | pvr = mfspr(SPRN_PVR); |
| 26 | |
Arnaldo Carvalho de Melo | e7f01d1 | 2012-03-14 12:29:29 -0300 | [diff] [blame] | 27 | nb = scnprintf(buffer, sz, "%lu,%lu$", PVR_VER(pvr), PVR_REV(pvr)); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 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 | } |
| 34 | return -1; |
| 35 | } |
Sukadev Bhattiprolu | ce88f27 | 2016-09-15 15:24:41 -0700 | [diff] [blame] | 36 | |
| 37 | char * |
Ganapatrao Kulkarni | 54e32dc | 2017-10-17 00:02:18 +0530 | [diff] [blame] | 38 | get_cpuid_str(struct perf_pmu *pmu __maybe_unused) |
Sukadev Bhattiprolu | ce88f27 | 2016-09-15 15:24:41 -0700 | [diff] [blame] | 39 | { |
| 40 | char *bufp; |
| 41 | |
| 42 | if (asprintf(&bufp, "%.8lx", mfspr(SPRN_PVR)) < 0) |
| 43 | bufp = NULL; |
| 44 | |
| 45 | return bufp; |
| 46 | } |