header.c 1.0 KB

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