blob: 3c5e59e6e01e0c1d2b194ec183f01930e9ffe7cf [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 */
15
16#ifndef included_clib_cpu_h
17#define included_clib_cpu_h
18
Damjan Marion0a78fa12019-01-19 23:45:36 +010019#include <sys/syscall.h>
Damjan Marion1c80e832016-05-11 23:07:18 +020020#include <vppinfra/format.h>
21
Damjan Mariona31698b2021-03-10 14:35:28 +010022#if defined(__x86_64__)
23#define foreach_march_variant \
24 _ (hsw, "Intel Haswell") \
25 _ (trm, "Intel Tremont") \
26 _ (skx, "Intel Skylake (server) / Cascade Lake") \
27 _ (icl, "Intel Ice Lake")
28#elif defined(__aarch64__)
29#define foreach_march_variant \
30 _ (octeontx2, "Marvell Octeon TX2") \
31 _ (thunderx2t99, "Marvell ThunderX2 T99") \
32 _ (qdf24xx, "Qualcomm CentriqTM 2400") \
33 _ (cortexa72, "ARM Cortex-A72") \
34 _ (neoversen1, "ARM Neoverse N1")
Damjan Marion1c80e832016-05-11 23:07:18 +020035#else
Damjan Mariona31698b2021-03-10 14:35:28 +010036#define foreach_march_variant
Damjan Marion1c80e832016-05-11 23:07:18 +020037#endif
38
Damjan Mariona31698b2021-03-10 14:35:28 +010039typedef enum
40{
41 CLIB_MARCH_VARIANT_TYPE = 0,
42#define _(s, n) CLIB_MARCH_VARIANT_TYPE_##s,
43 foreach_march_variant
44#undef _
45 CLIB_MARCH_TYPE_N_VARIANTS
46} clib_march_variant_type_t;
Damjan Marion1c80e832016-05-11 23:07:18 +020047
Damjan Marion812b32d2018-05-28 21:26:47 +020048#ifdef CLIB_MARCH_VARIANT
Damjan Marion04f3db32017-11-10 21:55:45 +010049#define __CLIB_MULTIARCH_FN(a,b) a##_##b
50#define _CLIB_MULTIARCH_FN(a,b) __CLIB_MULTIARCH_FN(a,b)
Damjan Marion812b32d2018-05-28 21:26:47 +020051#define CLIB_MULTIARCH_FN(fn) _CLIB_MULTIARCH_FN(fn,CLIB_MARCH_VARIANT)
Damjan Marion04f3db32017-11-10 21:55:45 +010052#else
53#define CLIB_MULTIARCH_FN(fn) fn
54#endif
Damjan Marion1c80e832016-05-11 23:07:18 +020055
Damjan Marion812b32d2018-05-28 21:26:47 +020056#define CLIB_MARCH_SFX CLIB_MULTIARCH_FN
57
Damjan Marion910d3692019-01-21 11:48:34 +010058typedef struct _clib_march_fn_registration
59{
60 void *function;
61 int priority;
62 struct _clib_march_fn_registration *next;
63 char *name;
64} clib_march_fn_registration;
65
66static_always_inline void *
67clib_march_select_fn_ptr (clib_march_fn_registration * r)
68{
69 void *rv = 0;
70 int last_prio = -1;
71
72 while (r)
73 {
74 if (last_prio < r->priority)
75 {
76 last_prio = r->priority;
77 rv = r->function;
78 }
79 r = r->next;
80 }
81 return rv;
82}
83
Damjan Marion11e0d752021-05-05 20:06:39 +020084#define CLIB_MARCH_FN_POINTER(fn) \
85 (__typeof__ (fn) *) clib_march_select_fn_ptr (fn##_march_fn_registrations);
Damjan Marion910d3692019-01-21 11:48:34 +010086
Mohammed Hawarie7149262022-05-18 10:08:47 +020087#define CLIB_MARCH_FN_VOID_POINTER(fn) \
88 clib_march_select_fn_ptr (fn##_march_fn_registrations);
89
Damjan Marion910d3692019-01-21 11:48:34 +010090#define _CLIB_MARCH_FN_REGISTRATION(fn) \
91static clib_march_fn_registration \
92CLIB_MARCH_SFX(fn##_march_fn_registration) = \
93{ \
94 .name = CLIB_MARCH_VARIANT_STR \
95}; \
96\
97static void __clib_constructor \
98fn##_march_register () \
99{ \
100 clib_march_fn_registration *r; \
101 r = & CLIB_MARCH_SFX (fn##_march_fn_registration); \
102 r->priority = CLIB_MARCH_FN_PRIORITY(); \
103 r->next = fn##_march_fn_registrations; \
104 r->function = CLIB_MARCH_SFX (fn); \
105 fn##_march_fn_registrations = r; \
106}
107
108#ifdef CLIB_MARCH_VARIANT
109#define CLIB_MARCH_FN_REGISTRATION(fn) \
110extern clib_march_fn_registration *fn##_march_fn_registrations; \
111_CLIB_MARCH_FN_REGISTRATION(fn)
112#else
113#define CLIB_MARCH_FN_REGISTRATION(fn) \
114clib_march_fn_registration *fn##_march_fn_registrations = 0; \
115_CLIB_MARCH_FN_REGISTRATION(fn)
116#endif
Radu Nicolaue1480a22021-01-14 10:25:02 +0000117#define foreach_x86_64_flags \
118 _ (sse3, 1, ecx, 0) \
119 _ (pclmulqdq, 1, ecx, 1) \
120 _ (ssse3, 1, ecx, 9) \
121 _ (sse41, 1, ecx, 19) \
122 _ (sse42, 1, ecx, 20) \
123 _ (avx, 1, ecx, 28) \
124 _ (rdrand, 1, ecx, 30) \
125 _ (avx2, 7, ebx, 5) \
126 _ (rtm, 7, ebx, 11) \
127 _ (pqm, 7, ebx, 12) \
128 _ (pqe, 7, ebx, 15) \
129 _ (avx512f, 7, ebx, 16) \
130 _ (rdseed, 7, ebx, 18) \
131 _ (x86_aes, 1, ecx, 25) \
132 _ (sha, 7, ebx, 29) \
133 _ (vaes, 7, ecx, 9) \
134 _ (vpclmulqdq, 7, ecx, 10) \
135 _ (avx512_vnni, 7, ecx, 11) \
136 _ (avx512_bitalg, 7, ecx, 12) \
137 _ (avx512_vpopcntdq, 7, ecx, 14) \
138 _ (movdiri, 7, ecx, 27) \
139 _ (movdir64b, 7, ecx, 28) \
Ray Kinsella0d27e3e2021-10-15 12:48:31 +0100140 _ (avx512_fp16, 7, edx, 23) \
Radu Nicolaue1480a22021-01-14 10:25:02 +0000141 _ (invariant_tsc, 0x80000007, edx, 8)
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100142
143#define foreach_aarch64_flags \
144_ (fp, 0) \
145_ (asimd, 1) \
146_ (evtstrm, 2) \
147_ (aarch64_aes, 3) \
148_ (pmull, 4) \
149_ (sha1, 5) \
150_ (sha2, 6) \
151_ (crc32, 7) \
152_ (atomics, 8) \
153_ (fphp, 9) \
154_ (asimdhp, 10) \
155_ (cpuid, 11) \
156_ (asimdrdm, 12) \
157_ (jscvt, 13) \
158_ (fcma, 14) \
159_ (lrcpc, 15) \
160_ (dcpop, 16) \
161_ (sha3, 17) \
162_ (sm3, 18) \
163_ (sm4, 19) \
164_ (asimddp, 20) \
165_ (sha512, 21) \
166_ (sve, 22)
167
Damjan Marionf8cb7012020-10-09 17:16:55 +0200168u32 clib_get_current_cpu_id ();
169u32 clib_get_current_numa_node ();
Damjan Marion0a78fa12019-01-19 23:45:36 +0100170
Zachary Leaf268d7be2022-05-12 02:26:00 -0500171typedef int (*clib_cpu_supports_func_t) ();
172
Christophe Fontaine33e81952016-12-19 14:41:52 +0100173#if defined(__x86_64__)
174#include "cpuid.h"
175
Damjan Marion1c80e832016-05-11 23:07:18 +0200176static inline int
Dave Barachc3799992016-08-15 11:12:27 -0400177clib_get_cpuid (const u32 lev, u32 * eax, u32 * ebx, u32 * ecx, u32 * edx)
Damjan Marion1c80e832016-05-11 23:07:18 +0200178{
179 if ((u32) __get_cpuid_max (0x80000000 & lev, 0) < lev)
180 return 0;
181 if (lev == 7)
Dave Barachc3799992016-08-15 11:12:27 -0400182 __cpuid_count (lev, 0, *eax, *ebx, *ecx, *edx);
Damjan Marion1c80e832016-05-11 23:07:18 +0200183 else
Dave Barachc3799992016-08-15 11:12:27 -0400184 __cpuid (lev, *eax, *ebx, *ecx, *edx);
Damjan Marion1c80e832016-05-11 23:07:18 +0200185 return 1;
186}
187
Damjan Marion1c80e832016-05-11 23:07:18 +0200188#define _(flag, func, reg, bit) \
189static inline int \
190clib_cpu_supports_ ## flag() \
191{ \
192 u32 __attribute__((unused)) eax, ebx = 0, ecx = 0, edx = 0; \
193 clib_get_cpuid (func, &eax, &ebx, &ecx, &edx); \
194 \
195 return ((reg & (1 << bit)) != 0); \
196}
Dave Barachc3799992016-08-15 11:12:27 -0400197foreach_x86_64_flags
Damjan Marion1c80e832016-05-11 23:07:18 +0200198#undef _
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100199#else /* __x86_64__ */
Christophe Fontaine33e81952016-12-19 14:41:52 +0100200
201#define _(flag, func, reg, bit) \
202static inline int clib_cpu_supports_ ## flag() { return 0; }
203foreach_x86_64_flags
204#undef _
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100205#endif /* __x86_64__ */
206#if defined(__aarch64__)
207#include <sys/auxv.h>
208#define _(flag, bit) \
209static inline int \
210clib_cpu_supports_ ## flag() \
211{ \
212 unsigned long hwcap = getauxval(AT_HWCAP); \
213 return (hwcap & (1 << bit)); \
214}
215 foreach_aarch64_flags
216#undef _
217#else /* ! __x86_64__ && !__aarch64__ */
218#define _(flag, bit) \
219static inline int clib_cpu_supports_ ## flag() { return 0; }
220 foreach_aarch64_flags
221#undef _
222#endif /* __x86_64__, __aarch64__ */
223/*
224 * aes is the only feature with the same name in both flag lists
225 * handle this by prefixing it with the arch name, and handling it
226 * with the custom function below
227 */
228 static inline int
229clib_cpu_supports_aes ()
230{
Zhiyong Yang7f4fd222019-04-19 03:04:41 -0400231#if defined(__x86_64__)
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100232 return clib_cpu_supports_x86_aes ();
233#elif defined (__aarch64__)
234 return clib_cpu_supports_aarch64_aes ();
235#else
236 return 0;
Christophe Fontaine33e81952016-12-19 14:41:52 +0100237#endif
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100238}
239
Damjan Marion812b32d2018-05-28 21:26:47 +0200240static inline int
Damjan Marion162330f2020-04-29 21:28:15 +0200241clib_cpu_march_priority_icl ()
Damjan Marion812b32d2018-05-28 21:26:47 +0200242{
Damjan Marion162330f2020-04-29 21:28:15 +0200243 if (clib_cpu_supports_avx512_bitalg ())
244 return 200;
Damjan Marion812b32d2018-05-28 21:26:47 +0200245 return -1;
246}
247
248static inline int
Damjan Marion162330f2020-04-29 21:28:15 +0200249clib_cpu_march_priority_skx ()
250{
251 if (clib_cpu_supports_avx512f ())
252 return 100;
253 return -1;
254}
255
256static inline int
Radu Nicolaue1480a22021-01-14 10:25:02 +0000257clib_cpu_march_priority_trm ()
258{
259 if (clib_cpu_supports_movdiri ())
260 return 60;
261 return -1;
262}
263
264static inline int
Damjan Marion162330f2020-04-29 21:28:15 +0200265clib_cpu_march_priority_hsw ()
Damjan Marion812b32d2018-05-28 21:26:47 +0200266{
267 if (clib_cpu_supports_avx2 ())
Damjan Marion64593152019-03-12 19:59:22 +0100268 return 50;
Damjan Marion812b32d2018-05-28 21:26:47 +0200269 return -1;
270}
271
Ray Kinsella0024e532021-11-26 14:57:35 +0000272#define X86_CPU_ARCH_PERF_FUNC 0xA
273
274static inline int
275clib_get_pmu_counter_count (u8 *fixed, u8 *general)
276{
277#if defined(__x86_64__)
278 u32 __clib_unused eax = 0, ebx = 0, ecx = 0, edx = 0;
279 clib_get_cpuid (X86_CPU_ARCH_PERF_FUNC, &eax, &ebx, &ecx, &edx);
280
281 *general = (eax & 0xFF00) >> 8;
282 *fixed = (edx & 0xF);
283
284 return 1;
285#else
286 return 0;
287#endif
288}
289
Lijian Zhang2e237212018-09-10 17:13:56 +0800290static inline u32
291clib_cpu_implementer ()
292{
293 char buf[128];
294 static u32 implementer = -1;
295
296 if (-1 != implementer)
297 return implementer;
298
299 FILE *fp = fopen ("/proc/cpuinfo", "r");
300 if (!fp)
301 return implementer;
302
303 while (!feof (fp))
304 {
305 if (!fgets (buf, sizeof (buf), fp))
306 break;
307 buf[127] = '\0';
308 if (strstr (buf, "CPU implementer"))
309 implementer = (u32) strtol (memchr (buf, ':', 128) + 2, NULL, 0);
310 if (-1 != implementer)
311 break;
312 }
313 fclose (fp);
314
315 return implementer;
316}
317
318static inline u32
319clib_cpu_part ()
320{
321 char buf[128];
322 static u32 part = -1;
323
324 if (-1 != part)
325 return part;
326
327 FILE *fp = fopen ("/proc/cpuinfo", "r");
328 if (!fp)
329 return part;
330
331 while (!feof (fp))
332 {
333 if (!fgets (buf, sizeof (buf), fp))
334 break;
335 buf[127] = '\0';
336 if (strstr (buf, "CPU part"))
337 part = (u32) strtol (memchr (buf, ':', 128) + 2, NULL, 0);
338 if (-1 != part)
339 break;
340 }
341 fclose (fp);
342
343 return part;
344}
345
Nitin Saxenae2f52362020-08-25 19:58:37 +0530346#define AARCH64_CPU_IMPLEMENTER_CAVIUM 0x43
347#define AARCH64_CPU_PART_THUNDERX2 0x0af
348#define AARCH64_CPU_PART_OCTEONTX2T96 0x0b2
349#define AARCH64_CPU_PART_OCTEONTX2T98 0x0b1
Lijian Zhang2e237212018-09-10 17:13:56 +0800350#define AARCH64_CPU_IMPLEMENTER_QDF24XX 0x51
351#define AARCH64_CPU_PART_QDF24XX 0xc00
352#define AARCH64_CPU_IMPLEMENTER_CORTEXA72 0x41
353#define AARCH64_CPU_PART_CORTEXA72 0xd08
Lijian.Zhang690ce862020-02-18 19:58:19 +0800354#define AARCH64_CPU_IMPLEMENTER_NEOVERSEN1 0x41
355#define AARCH64_CPU_PART_NEOVERSEN1 0xd0c
Lijian Zhang2e237212018-09-10 17:13:56 +0800356
357static inline int
Nitin Saxenae2f52362020-08-25 19:58:37 +0530358clib_cpu_march_priority_octeontx2 ()
359{
360 if ((AARCH64_CPU_IMPLEMENTER_CAVIUM == clib_cpu_implementer ()) &&
361 ((AARCH64_CPU_PART_OCTEONTX2T96 == clib_cpu_part ())
362 || AARCH64_CPU_PART_OCTEONTX2T98 == clib_cpu_part ()))
363 return 20;
364 return -1;
365}
366
367static inline int
Lijian Zhang2e237212018-09-10 17:13:56 +0800368clib_cpu_march_priority_thunderx2t99 ()
369{
Nitin Saxenae2f52362020-08-25 19:58:37 +0530370 if ((AARCH64_CPU_IMPLEMENTER_CAVIUM == clib_cpu_implementer ()) &&
371 (AARCH64_CPU_PART_THUNDERX2 == clib_cpu_part ()))
Lijian Zhang2e237212018-09-10 17:13:56 +0800372 return 20;
373 return -1;
374}
375
376static inline int
377clib_cpu_march_priority_qdf24xx ()
378{
379 if ((AARCH64_CPU_IMPLEMENTER_QDF24XX == clib_cpu_implementer ()) &&
380 (AARCH64_CPU_PART_QDF24XX == clib_cpu_part ()))
381 return 20;
382 return -1;
383}
384
385static inline int
386clib_cpu_march_priority_cortexa72 ()
387{
388 if ((AARCH64_CPU_IMPLEMENTER_CORTEXA72 == clib_cpu_implementer ()) &&
389 (AARCH64_CPU_PART_CORTEXA72 == clib_cpu_part ()))
390 return 10;
391 return -1;
392}
393
Lijian.Zhang690ce862020-02-18 19:58:19 +0800394static inline int
395clib_cpu_march_priority_neoversen1 ()
396{
397 if ((AARCH64_CPU_IMPLEMENTER_NEOVERSEN1 == clib_cpu_implementer ()) &&
398 (AARCH64_CPU_PART_NEOVERSEN1 == clib_cpu_part ()))
399 return 10;
400 return -1;
401}
402
Damjan Marion812b32d2018-05-28 21:26:47 +0200403#ifdef CLIB_MARCH_VARIANT
404#define CLIB_MARCH_FN_PRIORITY() CLIB_MARCH_SFX(clib_cpu_march_priority)()
405#else
406#define CLIB_MARCH_FN_PRIORITY() 0
407#endif
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100408#endif /* included_clib_cpu_h */
409
Florin Coras983cc7d2018-09-18 23:11:55 -0700410#define CLIB_MARCH_FN_CONSTRUCTOR(fn) \
411static void __clib_constructor \
412CLIB_MARCH_SFX(fn ## _march_constructor) (void) \
413{ \
414 if (CLIB_MARCH_FN_PRIORITY() > fn ## _selected_priority) \
415 { \
416 fn ## _selected = & CLIB_MARCH_SFX (fn ## _ma); \
417 fn ## _selected_priority = CLIB_MARCH_FN_PRIORITY(); \
418 } \
419} \
420
421#ifndef CLIB_MARCH_VARIANT
Damjan Marion1bb67ab2021-04-30 11:11:08 +0200422#define CLIB_MARCH_FN(fn, rtype, _args...) \
423 static rtype CLIB_MARCH_SFX (fn##_ma) (_args); \
424 rtype (*fn##_selected) (_args) = &CLIB_MARCH_SFX (fn##_ma); \
425 int fn##_selected_priority = 0; \
426 static inline rtype CLIB_MARCH_SFX (fn##_ma) (_args)
Florin Coras983cc7d2018-09-18 23:11:55 -0700427#else
Damjan Marion1bb67ab2021-04-30 11:11:08 +0200428#define CLIB_MARCH_FN(fn, rtype, _args...) \
429 static rtype CLIB_MARCH_SFX (fn##_ma) (_args); \
430 extern rtype (*fn##_selected) (_args); \
431 extern int fn##_selected_priority; \
432 CLIB_MARCH_FN_CONSTRUCTOR (fn) \
433 static rtype CLIB_MARCH_SFX (fn##_ma) (_args)
Florin Coras983cc7d2018-09-18 23:11:55 -0700434#endif
435
436#define CLIB_MARCH_FN_SELECT(fn) (* fn ## _selected)
437
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100438format_function_t format_cpu_uarch;
Damjan Marion522e4862016-03-04 12:44:14 +0100439format_function_t format_cpu_model_name;
Damjan Marion1c80e832016-05-11 23:07:18 +0200440format_function_t format_cpu_flags;
Damjan Marione3e35552021-05-06 17:34:49 +0200441format_function_t format_march_variant;
Damjan Marion522e4862016-03-04 12:44:14 +0100442
Dave Barachc3799992016-08-15 11:12:27 -0400443/*
444 * fd.io coding-style-patch-verification: ON
445 *
446 * Local Variables:
447 * eval: (c-set-style "gnu")
448 * End:
449 */