header.c 1003 B

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