header.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include <linux/kernel.h>
  2. #include <linux/bits.h>
  3. #include <linux/bitfield.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <perf/cpumap.h>
  7. #include <api/fs/fs.h>
  8. #include <errno.h>
  9. #include "debug.h"
  10. #include "header.h"
  11. #define MIDR "/regs/identification/midr_el1"
  12. #define MIDR_SIZE 19
  13. #define MIDR_REVISION_MASK GENMASK(3, 0)
  14. #define MIDR_VARIANT_MASK GENMASK(23, 20)
  15. static int _get_cpuid(char *buf, size_t sz, struct perf_cpu_map *cpus)
  16. {
  17. const char *sysfs = sysfs__mountpoint();
  18. struct perf_cpu cpu;
  19. int idx, ret = EINVAL;
  20. if (!sysfs || sz < MIDR_SIZE)
  21. return EINVAL;
  22. perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
  23. char path[PATH_MAX];
  24. FILE *file;
  25. scnprintf(path, PATH_MAX, "%s/devices/system/cpu/cpu%d" MIDR,
  26. sysfs, cpu.cpu);
  27. file = fopen(path, "r");
  28. if (!file) {
  29. pr_debug("fopen failed for file %s\n", path);
  30. continue;
  31. }
  32. if (!fgets(buf, MIDR_SIZE, file)) {
  33. fclose(file);
  34. continue;
  35. }
  36. fclose(file);
  37. /* got midr break loop */
  38. ret = 0;
  39. break;
  40. }
  41. return ret;
  42. }
  43. int get_cpuid(char *buf, size_t sz)
  44. {
  45. struct perf_cpu_map *cpus = perf_cpu_map__new_online_cpus();
  46. int ret;
  47. if (!cpus)
  48. return EINVAL;
  49. ret = _get_cpuid(buf, sz, cpus);
  50. perf_cpu_map__put(cpus);
  51. return ret;
  52. }
  53. char *get_cpuid_str(struct perf_pmu *pmu)
  54. {
  55. char *buf = NULL;
  56. int res;
  57. if (!pmu || !pmu->cpus)
  58. return NULL;
  59. buf = malloc(MIDR_SIZE);
  60. if (!buf)
  61. return NULL;
  62. /* read midr from list of cpus mapped to this pmu */
  63. res = _get_cpuid(buf, MIDR_SIZE, pmu->cpus);
  64. if (res) {
  65. pr_err("failed to get cpuid string for PMU %s\n", pmu->name);
  66. free(buf);
  67. buf = NULL;
  68. }
  69. return buf;
  70. }
  71. /*
  72. * Return 0 if idstr is a higher or equal to version of the same part as
  73. * mapcpuid. Therefore, if mapcpuid has 0 for revision and variant then any
  74. * version of idstr will match as long as it's the same CPU type.
  75. *
  76. * Return 1 if the CPU type is different or the version of idstr is lower.
  77. */
  78. int strcmp_cpuid_str(const char *mapcpuid, const char *idstr)
  79. {
  80. u64 map_id = strtoull(mapcpuid, NULL, 16);
  81. char map_id_variant = FIELD_GET(MIDR_VARIANT_MASK, map_id);
  82. char map_id_revision = FIELD_GET(MIDR_REVISION_MASK, map_id);
  83. u64 id = strtoull(idstr, NULL, 16);
  84. char id_variant = FIELD_GET(MIDR_VARIANT_MASK, id);
  85. char id_revision = FIELD_GET(MIDR_REVISION_MASK, id);
  86. u64 id_fields = ~(MIDR_VARIANT_MASK | MIDR_REVISION_MASK);
  87. /* Compare without version first */
  88. if ((map_id & id_fields) != (id & id_fields))
  89. return 1;
  90. /*
  91. * ID matches, now compare version.
  92. *
  93. * Arm revisions (like r0p0) are compared here like two digit semver
  94. * values eg. 1.3 < 2.0 < 2.1 < 2.2.
  95. *
  96. * r = high value = 'Variant' field in MIDR
  97. * p = low value = 'Revision' field in MIDR
  98. *
  99. */
  100. if (id_variant > map_id_variant)
  101. return 0;
  102. if (id_variant == map_id_variant && id_revision >= map_id_revision)
  103. return 0;
  104. /*
  105. * variant is less than mapfile variant or variants are the same but
  106. * the revision doesn't match. Return no match.
  107. */
  108. return 1;
  109. }