Merge tag 'ntb-5.11' of git://github.com/jonmason/ntb
[linux/fpc-iii.git] / tools / perf / arch / x86 / util / header.c
blob578c8c568ffd6f1210a421052fd2ad5ba5242879
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 <regex.h>
10 #include "../../../util/debug.h"
11 #include "../../../util/header.h"
13 static inline void
14 cpuid(unsigned int op, unsigned int *a, unsigned int *b, unsigned int *c,
15 unsigned int *d)
17 __asm__ __volatile__ (".byte 0x53\n\tcpuid\n\t"
18 "movl %%ebx, %%esi\n\t.byte 0x5b"
19 : "=a" (*a),
20 "=S" (*b),
21 "=c" (*c),
22 "=d" (*d)
23 : "a" (op));
26 static int
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;
31 int nb;
32 char vendor[16];
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);
38 vendor[12] = '\0';
40 if (lvl >= 1) {
41 cpuid(1, &a, &b, &c, &d);
43 family = (a >> 8) & 0xf; /* bits 11 - 8 */
44 model = (a >> 4) & 0xf; /* Bits 7 - 4 */
45 step = a & 0xf;
47 /* extended family */
48 if (family == 0xf)
49 family += (a >> 20) & 0xff;
51 /* extended model */
52 if (family >= 0x6)
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, '$')) {
59 buffer[nb-1] = '\0';
60 return 0;
62 return ENOBUFS;
65 int
66 get_cpuid(char *buffer, size_t sz)
68 return __get_cpuid(buffer, sz, "%s,%u,%u,%u$");
71 char *
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) {
77 free(buf);
78 return NULL;
80 return buf;
83 /* Full CPUID format for x86 is vendor-family-model-stepping */
84 static bool is_full_cpuid(const char *id)
86 const char *tmp = id;
87 int count = 0;
89 while ((tmp = strchr(tmp, '-')) != NULL) {
90 count++;
91 tmp++;
94 if (count == 3)
95 return true;
97 return false;
100 int strcmp_cpuid_str(const char *mapcpuid, const char *id)
102 regex_t re;
103 regmatch_t pmatch[1];
104 int match;
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);
115 return 1;
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);
121 return 1;
124 match = !regexec(&re, id, 1, pmatch, 0);
125 regfree(&re);
126 if (match) {
127 size_t match_len = (pmatch[0].rm_eo - pmatch[0].rm_so);
128 size_t cpuid_len;
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;
135 else
136 cpuid_len = strlen(id);
138 /* Verify the entire string matched. */
139 if (match_len == cpuid_len)
140 return 0;
143 return 1;