1 // SPDX-License-Identifier: GPL-2.0
10 #include "../../../util/debug.h"
11 #include "../../../util/header.h"
14 cpuid(unsigned int op, unsigned int *a, unsigned int *b, unsigned int *c,
17 __asm__ __volatile__ (".byte 0x53\n\tcpuid\n\t"
18 "movl %%ebx, %%esi\n\t.byte 0x5b"
27 __get_cpuid(char *buffer, size_t sz, const char *fmt)
29 unsigned int a, b, c, d, lvl;
30 int family = -1, model = -1, step = -1;
34 cpuid(0, &lvl, &b, &c, &d);
35 strncpy(&vendor[0], (char *)(&b), 4);
36 strncpy(&vendor[4], (char *)(&d), 4);
37 strncpy(&vendor[8], (char *)(&c), 4);
41 cpuid(1, &a, &b, &c, &d);
43 family = (a >> 8) & 0xf; /* bits 11 - 8 */
44 model = (a >> 4) & 0xf; /* Bits 7 - 4 */
49 family += (a >> 20) & 0xff;
53 model += ((a >> 16) & 0xf) << 4;
55 nb = scnprintf(buffer, sz, fmt, vendor, family, model, step);
57 /* look for end marker to ensure the entire data fit */
58 if (strchr(buffer, '$')) {
66 get_cpuid(char *buffer, size_t sz)
68 return __get_cpuid(buffer, sz, "%s,%u,%u,%u$");
72 get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
74 char *buf = malloc(128);
76 if (buf && __get_cpuid(buf, 128, "%s-%u-%X-%X$") < 0) {
83 /* Full CPUID format for x86 is vendor-family-model-stepping */
84 static bool is_full_cpuid(const char *id)
89 while ((tmp = strchr(tmp, '-')) != NULL) {
100 int strcmp_cpuid_str(const char *mapcpuid, const char *id)
103 regmatch_t pmatch[1];
105 bool full_mapcpuid = is_full_cpuid(mapcpuid);
106 bool full_cpuid = is_full_cpuid(id);
109 * Full CPUID format is required to identify a platform.
110 * Error out if the cpuid string is incomplete.
112 if (full_mapcpuid && !full_cpuid) {
113 pr_info("Invalid CPUID %s. Full CPUID is required, "
114 "vendor-family-model-stepping\n", id);
118 if (regcomp(&re, mapcpuid, REG_EXTENDED) != 0) {
119 /* Warn unable to generate match particular string. */
120 pr_info("Invalid regular expression %s\n", mapcpuid);
124 match = !regexec(&re, id, 1, pmatch, 0);
127 size_t match_len = (pmatch[0].rm_eo - pmatch[0].rm_so);
130 /* If the full CPUID format isn't required,
131 * ignoring the stepping.
133 if (!full_mapcpuid && full_cpuid)
134 cpuid_len = strrchr(id, '-') - id;
136 cpuid_len = strlen(id);
138 /* Verify the entire string matched. */
139 if (match_len == cpuid_len)