From: Zenghui Yu yuzenghui@huawei.com
euleros inclusion category: feature feature: perf kvm stat on arm64
picked from https://patchwork.kernel.org/patch/10989123/.
-------------------------------------------------
The get_cpuid() function returns the MIDR string of the first CPU.
Signed-off-by: Zenghui Yu yuzenghui@huawei.com Reviewed-by: Hailiang Zhang zhang.zhanghailiang@huawei.com Signed-off-by: Zenghui Yu yuzenghui@huawei.com Signed-off-by: Yang Yingliang yangyingliang@huawei.com --- tools/perf/arch/arm64/util/header.c | 74 +++++++++++++++++++++++-------------- 1 file changed, 47 insertions(+), 27 deletions(-)
diff --git a/tools/perf/arch/arm64/util/header.c b/tools/perf/arch/arm64/util/header.c index 534cd25..5c17b86 100644 --- a/tools/perf/arch/arm64/util/header.c +++ b/tools/perf/arch/arm64/util/header.c @@ -9,17 +9,56 @@ #define MIDR_VARIANT_SHIFT 20 #define MIDR_VARIANT_MASK (0xf << MIDR_VARIANT_SHIFT)
-char *get_cpuid_str(struct perf_pmu *pmu) +/* Return value of midr_el1 if success, else return 0. */ +static int read_midr_el1(char *buf, int cpu_nr) { - char *buf = NULL; char path[PATH_MAX]; const char *sysfs = sysfs__mountpoint(); + u64 midr = 0; + FILE *file; + + if (!sysfs) + return 0; + + scnprintf(path, PATH_MAX, "%s/devices/system/cpu/cpu%d"MIDR, + sysfs, cpu_nr); + + file = fopen(path, "r"); + if (!file) { + pr_debug("fopen failed for file %s\n", path); + return 0; + } + + if (!fgets(buf, MIDR_SIZE, file)) { + fclose(file); + return 0; + } + fclose(file); + + /* Ignore/clear Variant[23:20] and Revision[3:0] of MIDR */ + midr = strtoul(buf, NULL, 16); + midr &= (~(MIDR_VARIANT_MASK | MIDR_REVISION_MASK)); + scnprintf(buf, MIDR_SIZE, "0x%016lx", midr); + + return midr; +} + +int get_cpuid(char *buffer, size_t sz __maybe_unused) +{ + if (read_midr_el1(buffer, 0)) + return 0; + + return -1; +} + +char *get_cpuid_str(struct perf_pmu *pmu) +{ + char *buf = NULL; int cpu; u64 midr = 0; struct cpu_map *cpus; - FILE *file;
- if (!sysfs || !pmu || !pmu->cpus) + if (!pmu || !pmu->cpus) return NULL;
buf = malloc(MIDR_SIZE); @@ -29,29 +68,10 @@ char *get_cpuid_str(struct perf_pmu *pmu) /* read midr from list of cpus mapped to this pmu */ cpus = cpu_map__get(pmu->cpus); for (cpu = 0; cpu < cpus->nr; cpu++) { - scnprintf(path, PATH_MAX, "%s/devices/system/cpu/cpu%d"MIDR, - sysfs, cpus->map[cpu]); - - file = fopen(path, "r"); - if (!file) { - pr_debug("fopen failed for file %s\n", path); - continue; - } - - if (!fgets(buf, MIDR_SIZE, file)) { - fclose(file); - continue; - } - fclose(file); - - /* Ignore/clear Variant[23:20] and - * Revision[3:0] of MIDR - */ - midr = strtoul(buf, NULL, 16); - midr &= (~(MIDR_VARIANT_MASK | MIDR_REVISION_MASK)); - scnprintf(buf, MIDR_SIZE, "0x%016lx", midr); - /* got midr break loop */ - break; + midr = read_midr_el1(buf, cpus->map[cpu]); + if (midr) + /* got midr break loop */ + break; }
if (!midr) {