blob: b66dd4968ad444a4e08d531e5c9f1237d7a353cf [file] [log] [blame]
Damjan Marion522e4862016-03-04 12:44:14 +01001/*
2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
Gabriel Ganneae66b0d2018-01-03 09:18:47 +010015#include <fcntl.h>
Damjan Marion522e4862016-03-04 12:44:14 +010016#include <vppinfra/clib.h>
17#include <vppinfra/format.h>
18#include <vppinfra/cpu.h>
19
Damjan Marion6b69f942021-11-11 17:34:01 +000020#define foreach_x86_cpu_uarch \
21 _ (0x06, 0x9e, "Kaby Lake", "Kaby Lake DT/H/S/X") \
22 _ (0x06, 0x9c, "Tremont", "Jasper Lake") \
23 _ (0x06, 0x9a, "Alder Lake", "Alder Lake L") \
24 _ (0x06, 0x97, "Alder Lake", "Alder Lake") \
25 _ (0x06, 0x96, "Tremont", "Elkhart Lake") \
26 _ (0x06, 0x8f, "Sapphire Rapids", "Sapphire Rapids X") \
27 _ (0x06, 0x8e, "Kaby Lake", "Kaby Lake Y/U") \
28 _ (0x06, 0x8c, "Tiger Lake", "Tiger Lake U") \
29 _ (0x06, 0x86, "Tremont", "Jacobsville") \
30 _ (0x06, 0x85, "Knights Mill", "Knights Mill") \
31 _ (0x06, 0x7e, "Ice Lake", "Ice Lake U") \
32 _ (0x06, 0x7d, "Ice Lake", "Ice Lake Y") \
33 _ (0x06, 0x7a, "Goldmont Plus", "Gemini Lake") \
34 _ (0x06, 0x6c, "Ice Lake", "Ice Lake SP") \
35 _ (0x06, 0x6a, "Ice Lake", "Ice Lake DE") \
36 _ (0x06, 0x66, "Cannon Lake", "Cannon Lake U") \
37 _ (0x06, 0x5f, "Goldmont", "Denverton") \
38 _ (0x06, 0x5e, "Skylake", "Skylake DT/H/S") \
39 _ (0x06, 0x5c, "Goldmont", "Apollo Lake") \
40 _ (0x06, 0x5a, "Silvermont", "Moorefield") \
41 _ (0x06, 0x57, "Knights Landing", "Knights Landing") \
42 _ (0x06, 0x56, "Broadwell", "Broadwell DE") \
43 _ (0x06, 0x55, "Skylake", "Skylake X/SP") \
44 _ (0x06, 0x4f, "Broadwell", "Broadwell EP/EX") \
45 _ (0x06, 0x4e, "Skylake", "Skylake Y/U") \
46 _ (0x06, 0x4d, "Silvermont", "Rangeley") \
47 _ (0x06, 0x4c, "Airmont", "Braswell") \
48 _ (0x06, 0x47, "Broadwell", "Broadwell H") \
49 _ (0x06, 0x46, "Haswell", "Crystalwell") \
50 _ (0x06, 0x45, "Haswell", "Haswell ULT") \
51 _ (0x06, 0x3f, "Haswell", "Haswell E") \
52 _ (0x06, 0x3e, "Ivy Bridge", "Ivy Bridge E/EN/EP") \
53 _ (0x06, 0x3d, "Broadwell", "Broadwell U") \
54 _ (0x06, 0x3c, "Haswell", "Haswell") \
55 _ (0x06, 0x3a, "Ivy Bridge", "IvyBridge") \
56 _ (0x06, 0x37, "Silvermont", "BayTrail") \
57 _ (0x06, 0x36, "Saltwell", "Cedarview,Centerton") \
58 _ (0x06, 0x35, "Saltwell", "Cloverview") \
59 _ (0x06, 0x2f, "Westmere", "Westmere EX") \
60 _ (0x06, 0x2e, "Nehalem", "Nehalem EX") \
61 _ (0x06, 0x2d, "Sandy Bridge", "SandyBridge E/EN/EP") \
62 _ (0x06, 0x2c, "Westmere", "Westmere EP/EX,Gulftown") \
63 _ (0x06, 0x2a, "Sandy Bridge", "Sandy Bridge") \
64 _ (0x06, 0x27, "Saltwell", "Medfield") \
65 _ (0x06, 0x26, "Bonnell", "Tunnel Creek") \
66 _ (0x06, 0x25, "Westmere", "Arrandale,Clarksdale") \
67 _ (0x06, 0x1e, "Nehalem", "Clarksfield,Lynnfield,Jasper Forest") \
68 _ (0x06, 0x1d, "Penryn", "Dunnington") \
69 _ (0x06, 0x1c, "Bonnell", "Pineview,Silverthorne") \
70 _ (0x06, 0x1a, "Nehalem", "Nehalem EP,Bloomfield)") \
71 _ (0x06, 0x17, "Penryn", "Yorkfield,Wolfdale,Penryn,Harpertown")
Damjan Marion522e4862016-03-04 12:44:14 +010072
Nitin Saxenac9122f92019-03-28 14:42:31 +053073/* _(implementor-id, part-id, vendor-name, cpu-name, show CPU pass as string) */
Gabriel Ganneae66b0d2018-01-03 09:18:47 +010074#define foreach_aarch64_cpu_uarch \
Nitin Saxenac9122f92019-03-28 14:42:31 +053075 _(0x41, 0xd03, "ARM", "Cortex-A53", 0) \
76 _(0x41, 0xd07, "ARM", "Cortex-A57", 0) \
77 _(0x41, 0xd08, "ARM", "Cortex-A72", 0) \
78 _(0x41, 0xd09, "ARM", "Cortex-A73", 0) \
Damjan Marion448890e2020-02-11 15:18:57 +010079 _(0x41, 0xd0a, "ARM", "Cortex-A75", 0) \
80 _(0x41, 0xd0b, "ARM", "Cortex-A76", 0) \
81 _(0x41, 0xd0c, "ARM", "Neoverse-N1", 0) \
82 _(0x41, 0xd4a, "ARM", "Neoverse-E1", 0) \
Nitin Saxenac9122f92019-03-28 14:42:31 +053083 _(0x43, 0x0a1, "Marvell", "THUNDERX CN88XX", 0) \
84 _(0x43, 0x0a2, "Marvell", "OCTEON TX CN81XX", 0) \
85 _(0x43, 0x0a3, "Marvell", "OCTEON TX CN83XX", 0) \
86 _(0x43, 0x0af, "Marvell", "THUNDERX2 CN99XX", 1) \
87 _(0x43, 0x0b1, "Marvell", "OCTEON TX2 CN98XX", 1) \
88 _(0x43, 0x0b2, "Marvell", "OCTEON TX2 CN96XX", 1)
Gabriel Ganneae66b0d2018-01-03 09:18:47 +010089
Damjan Mariondae1c7e2020-10-17 13:32:25 +020090__clib_export u8 *
Damjan Marion522e4862016-03-04 12:44:14 +010091format_cpu_uarch (u8 * s, va_list * args)
92{
93#if __x86_64__
Dave Barachc3799992016-08-15 11:12:27 -040094 u32 __attribute__ ((unused)) eax, ebx, ecx, edx;
Paul Vinciguerrad6897c12018-12-30 11:07:36 -080095 u8 model, family, stepping;
Sivaprasad Tummala6a1a8322023-04-17 05:16:11 -070096 u8 amd_vendor = 0;
97
98 if (__get_cpuid (0, &eax, &ebx, &ecx, &edx) == 0)
99 return format (s, "unknown (missing cpuid)");
100
101 if (amd_vendor (ebx, ecx, edx))
102 amd_vendor = 1;
Damjan Marion522e4862016-03-04 12:44:14 +0100103
104 if (__get_cpuid (1, &eax, &ebx, &ecx, &edx) == 0)
105 return format (s, "unknown (missing cpuid)");
106
Paul Vinciguerrad6897c12018-12-30 11:07:36 -0800107 stepping = eax & 0x0f;
Sivaprasad Tummala6a1a8322023-04-17 05:16:11 -0700108 if (amd_vendor)
109 {
110 family = ((eax >> 8) & 0x0f);
111 model = ((eax >> 4) & 0x0f);
112 if (family >= 0xf)
113 {
114 family = family + ((eax >> 20) & 0xf);
115 model = (model | ((eax >> 12) & 0xf0));
116 }
117 return format (s, "Zen (family 0x%02x model 0x%02x)", family, model);
118 }
119 else
120 {
121 model = ((eax >> 4) & 0x0f) | ((eax >> 12) & 0xf0);
122 family = (eax >> 8) & 0x0f;
123 }
Damjan Marion522e4862016-03-04 12:44:14 +0100124
Paul Vinciguerrad6897c12018-12-30 11:07:36 -0800125#define _(f,m,a,c) if ((model == m) && (family == f)) return \
126format(s, "[0x%x] %s ([0x%02x] %s) stepping 0x%x", f, a, m, c, stepping);
Damjan Marion522e4862016-03-04 12:44:14 +0100127 foreach_x86_cpu_uarch
128#undef _
Dave Barachc3799992016-08-15 11:12:27 -0400129 return format (s, "unknown (family 0x%02x model 0x%02x)", family, model);
Damjan Marion522e4862016-03-04 12:44:14 +0100130
Gabriel Ganneae66b0d2018-01-03 09:18:47 +0100131#elif __aarch64__
Gabriel Ganneae66b0d2018-01-03 09:18:47 +0100132 unformat_input_t input;
133 u32 implementer, primary_part_number, variant, revision;
134
Damjan Marion8d0c0c62023-08-06 20:39:38 +0200135 if (unformat_init_file (&input, "/proc/cpuinfo"))
Gabriel Ganneae66b0d2018-01-03 09:18:47 +0100136 {
Damjan Marion8d0c0c62023-08-06 20:39:38 +0200137 while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
138 {
139 if (unformat (&input, "CPU implementer%_: 0x%x", &implementer))
140 ;
141 else if (unformat (&input, "CPU part%_: 0x%x", &primary_part_number))
142 ;
143 else if (unformat (&input, "CPU variant%_: 0x%x", &variant))
144 ;
145 else if (unformat (&input, "CPU revision%_: %u", &revision))
146 ;
147 else
148 unformat_skip_line (&input);
149 }
150 unformat_free (&input);
Gabriel Ganneae66b0d2018-01-03 09:18:47 +0100151 }
Damjan Marion8d0c0c62023-08-06 20:39:38 +0200152 else
153 return format (s, "unknown");
Gabriel Ganneae66b0d2018-01-03 09:18:47 +0100154
Nitin Saxenac9122f92019-03-28 14:42:31 +0530155#define _(i,p,a,c,_format) if ((implementer == i) && (primary_part_number == p)){ \
156 if (_format)\
157 return format(s, "%s (%s PASS %c%u)", a, c, 'A'+variant, revision);\
158 else {\
159 if (implementer == 0x43)\
160 variant++; \
161 return format (s, "%s (%s PASS %u.%u)", a, c, variant, revision);}}
Gabriel Ganneae66b0d2018-01-03 09:18:47 +0100162
Gabriel Ganneae66b0d2018-01-03 09:18:47 +0100163 foreach_aarch64_cpu_uarch
164#undef _
165 return format (s, "unknown (implementer 0x%02x part 0x%03x PASS %u.%u)",
166 implementer, primary_part_number, variant, revision);
167
Damjan Marion522e4862016-03-04 12:44:14 +0100168#else /* ! __x86_64__ */
169 return format (s, "unknown");
170#endif
171}
172
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200173__clib_export u8 *
Damjan Marion522e4862016-03-04 12:44:14 +0100174format_cpu_model_name (u8 * s, va_list * args)
175{
176#if __x86_64__
Dave Barachc3799992016-08-15 11:12:27 -0400177 u32 __attribute__ ((unused)) eax, ebx, ecx, edx;
178 u8 *name = 0;
179 u32 *name_u32;
Damjan Marion522e4862016-03-04 12:44:14 +0100180
181 if (__get_cpuid (1, &eax, &ebx, &ecx, &edx) == 0)
182 return format (s, "unknown (missing cpuid)");
183
184 __get_cpuid (0x80000000, &eax, &ebx, &ecx, &edx);
185 if (eax < 0x80000004)
186 return format (s, "unknown (missing ext feature)");
187
Dave Barachc3799992016-08-15 11:12:27 -0400188 vec_validate (name, 48);
Damjan Marion522e4862016-03-04 12:44:14 +0100189 name_u32 = (u32 *) name;
190
191 __get_cpuid (0x80000002, &eax, &ebx, &ecx, &edx);
192 name_u32[0] = eax;
193 name_u32[1] = ebx;
194 name_u32[2] = ecx;
195 name_u32[3] = edx;
196
197 __get_cpuid (0x80000003, &eax, &ebx, &ecx, &edx);
198 name_u32[4] = eax;
199 name_u32[5] = ebx;
200 name_u32[6] = ecx;
201 name_u32[7] = edx;
202
203 __get_cpuid (0x80000004, &eax, &ebx, &ecx, &edx);
204 name_u32[8] = eax;
205 name_u32[9] = ebx;
206 name_u32[10] = ecx;
207 name_u32[11] = edx;
208
209 s = format (s, "%s", name);
Dave Barachc3799992016-08-15 11:12:27 -0400210 vec_free (name);
Damjan Marion522e4862016-03-04 12:44:14 +0100211 return s;
212
Christophe Fontaine33e81952016-12-19 14:41:52 +0100213#elif defined(__aarch64__)
214 return format (s, "armv8");
Damjan Marion522e4862016-03-04 12:44:14 +0100215#else /* ! __x86_64__ */
216 return format (s, "unknown");
217#endif
Damjan Marion1c80e832016-05-11 23:07:18 +0200218}
219
Damjan Marion63c31e02022-01-19 18:13:34 +0100220#if defined(__x86_64__) || defined(__aarch64__)
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100221static inline char const *
Benoît Gannea7cb3572019-12-16 16:00:14 +0100222flag_skip_prefix (char const *flag, const char *pfx, int len)
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100223{
Benoît Gannea7cb3572019-12-16 16:00:14 +0100224 if (0 == strncmp (flag, pfx, len - 1))
225 return flag + len - 1;
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100226 return flag;
227}
Damjan Marion63c31e02022-01-19 18:13:34 +0100228#endif
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100229
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200230__clib_export u8 *
Damjan Marion63c31e02022-01-19 18:13:34 +0100231format_cpu_flags (u8 *s, va_list *args)
Damjan Marion1c80e832016-05-11 23:07:18 +0200232{
Christophe Fontaine33e81952016-12-19 14:41:52 +0100233#if defined(__x86_64__)
Damjan Marion63c31e02022-01-19 18:13:34 +0100234#define _(flag, func, reg, bit) \
235 if (clib_cpu_supports_##flag ()) \
236 s = format (s, "%s ", flag_skip_prefix (#flag, "x86_", sizeof ("x86_")));
Dave Barachc3799992016-08-15 11:12:27 -0400237 foreach_x86_64_flags return s;
Damjan Marion1c80e832016-05-11 23:07:18 +0200238#undef _
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100239#elif defined(__aarch64__)
Damjan Marion63c31e02022-01-19 18:13:34 +0100240#define _(flag, bit) \
241 if (clib_cpu_supports_##flag ()) \
242 s = format (s, "%s ", \
243 flag_skip_prefix (#flag, "aarch64_", sizeof ("aarch64_")));
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100244 foreach_aarch64_flags return s;
245#undef _
246#else /* ! ! __x86_64__ && ! __aarch64__ */
Damjan Marion1c80e832016-05-11 23:07:18 +0200247 return format (s, "unknown");
248#endif
249}
250
Damjan Marionf8cb7012020-10-09 17:16:55 +0200251__clib_export u32
252clib_get_current_cpu_id ()
253{
254 unsigned cpu, node;
255 syscall (__NR_getcpu, &cpu, &node, 0);
256 return cpu;
257}
Damjan Marion1c80e832016-05-11 23:07:18 +0200258
Damjan Marionf8cb7012020-10-09 17:16:55 +0200259__clib_export u32
260clib_get_current_numa_node ()
261{
262 unsigned cpu, node;
263 syscall (__NR_getcpu, &cpu, &node, 0);
264 return node;
265}
Dave Barachc3799992016-08-15 11:12:27 -0400266
Damjan Marione3e35552021-05-06 17:34:49 +0200267__clib_export u8 *
268format_march_variant (u8 *s, va_list *args)
269{
270 clib_march_variant_type_t t = va_arg (*args, clib_march_variant_type_t);
271 char *variants[] = { [0] = "default",
272#define _(s, n) [CLIB_MARCH_VARIANT_TYPE_##s] = n,
273 foreach_march_variant
274#undef _
275 };
276 return format (s, "%s", variants[t]);
277}
278
Dave Barachc3799992016-08-15 11:12:27 -0400279/*
280 * fd.io coding-style-patch-verification: ON
281 *
282 * Local Variables:
283 * eval: (c-set-style "gnu")
284 * End:
285 */