blob: 60439e0e3caf8e89e2bc404afabff6d848025fbf [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 \
Damjan Marion98f7f0a2023-04-17 09:38:11 +000024 _ (scalar, "Generic (SIMD disabled)") \
Damjan Mariona31698b2021-03-10 14:35:28 +010025 _ (hsw, "Intel Haswell") \
26 _ (trm, "Intel Tremont") \
27 _ (skx, "Intel Skylake (server) / Cascade Lake") \
Damjan Marion1ca68182023-03-15 11:08:53 +000028 _ (icl, "Intel Ice Lake") \
29 _ (adl, "Intel Alder Lake") \
30 _ (spr, "Intel Sapphire Rapids")
Damjan Mariona31698b2021-03-10 14:35:28 +010031#elif defined(__aarch64__)
32#define foreach_march_variant \
33 _ (octeontx2, "Marvell Octeon TX2") \
34 _ (thunderx2t99, "Marvell ThunderX2 T99") \
35 _ (qdf24xx, "Qualcomm CentriqTM 2400") \
36 _ (cortexa72, "ARM Cortex-A72") \
37 _ (neoversen1, "ARM Neoverse N1")
Damjan Marion1c80e832016-05-11 23:07:18 +020038#else
Damjan Mariona31698b2021-03-10 14:35:28 +010039#define foreach_march_variant
Damjan Marion1c80e832016-05-11 23:07:18 +020040#endif
41
Damjan Mariona31698b2021-03-10 14:35:28 +010042typedef enum
43{
44 CLIB_MARCH_VARIANT_TYPE = 0,
45#define _(s, n) CLIB_MARCH_VARIANT_TYPE_##s,
46 foreach_march_variant
47#undef _
48 CLIB_MARCH_TYPE_N_VARIANTS
49} clib_march_variant_type_t;
Damjan Marion1c80e832016-05-11 23:07:18 +020050
Damjan Marion812b32d2018-05-28 21:26:47 +020051#ifdef CLIB_MARCH_VARIANT
Damjan Marion04f3db32017-11-10 21:55:45 +010052#define __CLIB_MULTIARCH_FN(a,b) a##_##b
53#define _CLIB_MULTIARCH_FN(a,b) __CLIB_MULTIARCH_FN(a,b)
Damjan Marion812b32d2018-05-28 21:26:47 +020054#define CLIB_MULTIARCH_FN(fn) _CLIB_MULTIARCH_FN(fn,CLIB_MARCH_VARIANT)
Damjan Marion04f3db32017-11-10 21:55:45 +010055#else
56#define CLIB_MULTIARCH_FN(fn) fn
57#endif
Damjan Marion1c80e832016-05-11 23:07:18 +020058
Damjan Marion812b32d2018-05-28 21:26:47 +020059#define CLIB_MARCH_SFX CLIB_MULTIARCH_FN
60
Damjan Marion910d3692019-01-21 11:48:34 +010061typedef struct _clib_march_fn_registration
62{
63 void *function;
64 int priority;
65 struct _clib_march_fn_registration *next;
66 char *name;
67} clib_march_fn_registration;
68
69static_always_inline void *
70clib_march_select_fn_ptr (clib_march_fn_registration * r)
71{
72 void *rv = 0;
73 int last_prio = -1;
74
75 while (r)
76 {
77 if (last_prio < r->priority)
78 {
79 last_prio = r->priority;
80 rv = r->function;
81 }
82 r = r->next;
83 }
84 return rv;
85}
86
Damjan Marion11e0d752021-05-05 20:06:39 +020087#define CLIB_MARCH_FN_POINTER(fn) \
88 (__typeof__ (fn) *) clib_march_select_fn_ptr (fn##_march_fn_registrations);
Damjan Marion910d3692019-01-21 11:48:34 +010089
Mohammed Hawarie7149262022-05-18 10:08:47 +020090#define CLIB_MARCH_FN_VOID_POINTER(fn) \
91 clib_march_select_fn_ptr (fn##_march_fn_registrations);
92
Damjan Marion910d3692019-01-21 11:48:34 +010093#define _CLIB_MARCH_FN_REGISTRATION(fn) \
94static clib_march_fn_registration \
95CLIB_MARCH_SFX(fn##_march_fn_registration) = \
96{ \
97 .name = CLIB_MARCH_VARIANT_STR \
98}; \
99\
100static void __clib_constructor \
101fn##_march_register () \
102{ \
103 clib_march_fn_registration *r; \
104 r = & CLIB_MARCH_SFX (fn##_march_fn_registration); \
105 r->priority = CLIB_MARCH_FN_PRIORITY(); \
106 r->next = fn##_march_fn_registrations; \
107 r->function = CLIB_MARCH_SFX (fn); \
108 fn##_march_fn_registrations = r; \
109}
110
111#ifdef CLIB_MARCH_VARIANT
112#define CLIB_MARCH_FN_REGISTRATION(fn) \
113extern clib_march_fn_registration *fn##_march_fn_registrations; \
114_CLIB_MARCH_FN_REGISTRATION(fn)
115#else
116#define CLIB_MARCH_FN_REGISTRATION(fn) \
117clib_march_fn_registration *fn##_march_fn_registrations = 0; \
118_CLIB_MARCH_FN_REGISTRATION(fn)
119#endif
Radu Nicolaue1480a22021-01-14 10:25:02 +0000120#define foreach_x86_64_flags \
121 _ (sse3, 1, ecx, 0) \
122 _ (pclmulqdq, 1, ecx, 1) \
123 _ (ssse3, 1, ecx, 9) \
124 _ (sse41, 1, ecx, 19) \
125 _ (sse42, 1, ecx, 20) \
126 _ (avx, 1, ecx, 28) \
127 _ (rdrand, 1, ecx, 30) \
128 _ (avx2, 7, ebx, 5) \
Maros Ondrejicka568ef462022-11-10 14:11:40 +0100129 _ (bmi2, 7, ebx, 8) \
Radu Nicolaue1480a22021-01-14 10:25:02 +0000130 _ (rtm, 7, ebx, 11) \
131 _ (pqm, 7, ebx, 12) \
132 _ (pqe, 7, ebx, 15) \
133 _ (avx512f, 7, ebx, 16) \
134 _ (rdseed, 7, ebx, 18) \
135 _ (x86_aes, 1, ecx, 25) \
136 _ (sha, 7, ebx, 29) \
137 _ (vaes, 7, ecx, 9) \
138 _ (vpclmulqdq, 7, ecx, 10) \
139 _ (avx512_vnni, 7, ecx, 11) \
140 _ (avx512_bitalg, 7, ecx, 12) \
141 _ (avx512_vpopcntdq, 7, ecx, 14) \
142 _ (movdiri, 7, ecx, 27) \
143 _ (movdir64b, 7, ecx, 28) \
Damjan Marion15522282023-03-14 13:34:59 +0100144 _ (enqcmd, 7, ecx, 29) \
Ray Kinsella0d27e3e2021-10-15 12:48:31 +0100145 _ (avx512_fp16, 7, edx, 23) \
Radu Nicolaue1480a22021-01-14 10:25:02 +0000146 _ (invariant_tsc, 0x80000007, edx, 8)
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100147
148#define foreach_aarch64_flags \
149_ (fp, 0) \
150_ (asimd, 1) \
151_ (evtstrm, 2) \
152_ (aarch64_aes, 3) \
153_ (pmull, 4) \
154_ (sha1, 5) \
155_ (sha2, 6) \
156_ (crc32, 7) \
157_ (atomics, 8) \
158_ (fphp, 9) \
159_ (asimdhp, 10) \
160_ (cpuid, 11) \
161_ (asimdrdm, 12) \
162_ (jscvt, 13) \
163_ (fcma, 14) \
164_ (lrcpc, 15) \
165_ (dcpop, 16) \
166_ (sha3, 17) \
167_ (sm3, 18) \
168_ (sm4, 19) \
169_ (asimddp, 20) \
170_ (sha512, 21) \
171_ (sve, 22)
172
Dave Barach6c89a352022-12-26 14:01:36 -0500173u32 clib_get_current_cpu_id (void);
174u32 clib_get_current_numa_node (void);
Damjan Marion0a78fa12019-01-19 23:45:36 +0100175
Dave Barach6c89a352022-12-26 14:01:36 -0500176typedef int (*clib_cpu_supports_func_t) (void);
Zachary Leaf268d7be2022-05-12 02:26:00 -0500177
Christophe Fontaine33e81952016-12-19 14:41:52 +0100178#if defined(__x86_64__)
179#include "cpuid.h"
180
Damjan Marion1c80e832016-05-11 23:07:18 +0200181static inline int
Dave Barachc3799992016-08-15 11:12:27 -0400182clib_get_cpuid (const u32 lev, u32 * eax, u32 * ebx, u32 * ecx, u32 * edx)
Damjan Marion1c80e832016-05-11 23:07:18 +0200183{
184 if ((u32) __get_cpuid_max (0x80000000 & lev, 0) < lev)
185 return 0;
186 if (lev == 7)
Dave Barachc3799992016-08-15 11:12:27 -0400187 __cpuid_count (lev, 0, *eax, *ebx, *ecx, *edx);
Damjan Marion1c80e832016-05-11 23:07:18 +0200188 else
Dave Barachc3799992016-08-15 11:12:27 -0400189 __cpuid (lev, *eax, *ebx, *ecx, *edx);
Damjan Marion1c80e832016-05-11 23:07:18 +0200190 return 1;
191}
192
Damjan Marion1c80e832016-05-11 23:07:18 +0200193#define _(flag, func, reg, bit) \
194static inline int \
195clib_cpu_supports_ ## flag() \
196{ \
197 u32 __attribute__((unused)) eax, ebx = 0, ecx = 0, edx = 0; \
198 clib_get_cpuid (func, &eax, &ebx, &ecx, &edx); \
199 \
200 return ((reg & (1 << bit)) != 0); \
201}
Dave Barachc3799992016-08-15 11:12:27 -0400202foreach_x86_64_flags
Damjan Marion1c80e832016-05-11 23:07:18 +0200203#undef _
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100204#else /* __x86_64__ */
Christophe Fontaine33e81952016-12-19 14:41:52 +0100205
206#define _(flag, func, reg, bit) \
207static inline int clib_cpu_supports_ ## flag() { return 0; }
208foreach_x86_64_flags
209#undef _
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100210#endif /* __x86_64__ */
211#if defined(__aarch64__)
212#include <sys/auxv.h>
213#define _(flag, bit) \
214static inline int \
215clib_cpu_supports_ ## flag() \
216{ \
217 unsigned long hwcap = getauxval(AT_HWCAP); \
218 return (hwcap & (1 << bit)); \
219}
220 foreach_aarch64_flags
221#undef _
222#else /* ! __x86_64__ && !__aarch64__ */
223#define _(flag, bit) \
224static inline int clib_cpu_supports_ ## flag() { return 0; }
225 foreach_aarch64_flags
226#undef _
227#endif /* __x86_64__, __aarch64__ */
228/*
229 * aes is the only feature with the same name in both flag lists
230 * handle this by prefixing it with the arch name, and handling it
231 * with the custom function below
232 */
233 static inline int
234clib_cpu_supports_aes ()
235{
Zhiyong Yang7f4fd222019-04-19 03:04:41 -0400236#if defined(__x86_64__)
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100237 return clib_cpu_supports_x86_aes ();
238#elif defined (__aarch64__)
239 return clib_cpu_supports_aarch64_aes ();
240#else
241 return 0;
Christophe Fontaine33e81952016-12-19 14:41:52 +0100242#endif
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100243}
244
Damjan Marion812b32d2018-05-28 21:26:47 +0200245static inline int
Damjan Marion98f7f0a2023-04-17 09:38:11 +0000246clib_cpu_march_priority_scalar ()
247{
248 return 1;
249}
250
251static inline int
Damjan Marion15522282023-03-14 13:34:59 +0100252clib_cpu_march_priority_spr ()
253{
254 if (clib_cpu_supports_enqcmd ())
255 return 300;
256 return -1;
257}
258
259static inline int
Damjan Marion162330f2020-04-29 21:28:15 +0200260clib_cpu_march_priority_icl ()
Damjan Marion812b32d2018-05-28 21:26:47 +0200261{
Damjan Marion162330f2020-04-29 21:28:15 +0200262 if (clib_cpu_supports_avx512_bitalg ())
263 return 200;
Damjan Marion812b32d2018-05-28 21:26:47 +0200264 return -1;
265}
266
267static inline int
Damjan Marion15522282023-03-14 13:34:59 +0100268clib_cpu_march_priority_adl ()
269{
270 if (clib_cpu_supports_movdiri () && clib_cpu_supports_avx2 ())
271 return 150;
272 return -1;
273}
274
275static inline int
Damjan Marion162330f2020-04-29 21:28:15 +0200276clib_cpu_march_priority_skx ()
277{
278 if (clib_cpu_supports_avx512f ())
279 return 100;
280 return -1;
281}
282
283static inline int
Radu Nicolaue1480a22021-01-14 10:25:02 +0000284clib_cpu_march_priority_trm ()
285{
286 if (clib_cpu_supports_movdiri ())
Damjan Marion15522282023-03-14 13:34:59 +0100287 return 40;
Radu Nicolaue1480a22021-01-14 10:25:02 +0000288 return -1;
289}
290
291static inline int
Damjan Marion162330f2020-04-29 21:28:15 +0200292clib_cpu_march_priority_hsw ()
Damjan Marion812b32d2018-05-28 21:26:47 +0200293{
294 if (clib_cpu_supports_avx2 ())
Damjan Marion64593152019-03-12 19:59:22 +0100295 return 50;
Damjan Marion812b32d2018-05-28 21:26:47 +0200296 return -1;
297}
298
Ray Kinsella0024e532021-11-26 14:57:35 +0000299#define X86_CPU_ARCH_PERF_FUNC 0xA
300
301static inline int
302clib_get_pmu_counter_count (u8 *fixed, u8 *general)
303{
304#if defined(__x86_64__)
305 u32 __clib_unused eax = 0, ebx = 0, ecx = 0, edx = 0;
306 clib_get_cpuid (X86_CPU_ARCH_PERF_FUNC, &eax, &ebx, &ecx, &edx);
307
308 *general = (eax & 0xFF00) >> 8;
309 *fixed = (edx & 0xF);
310
311 return 1;
312#else
313 return 0;
314#endif
315}
316
Lijian Zhang2e237212018-09-10 17:13:56 +0800317static inline u32
318clib_cpu_implementer ()
319{
320 char buf[128];
321 static u32 implementer = -1;
322
323 if (-1 != implementer)
324 return implementer;
325
326 FILE *fp = fopen ("/proc/cpuinfo", "r");
327 if (!fp)
328 return implementer;
329
330 while (!feof (fp))
331 {
332 if (!fgets (buf, sizeof (buf), fp))
333 break;
334 buf[127] = '\0';
335 if (strstr (buf, "CPU implementer"))
336 implementer = (u32) strtol (memchr (buf, ':', 128) + 2, NULL, 0);
337 if (-1 != implementer)
338 break;
339 }
340 fclose (fp);
341
342 return implementer;
343}
344
345static inline u32
346clib_cpu_part ()
347{
348 char buf[128];
349 static u32 part = -1;
350
351 if (-1 != part)
352 return part;
353
354 FILE *fp = fopen ("/proc/cpuinfo", "r");
355 if (!fp)
356 return part;
357
358 while (!feof (fp))
359 {
360 if (!fgets (buf, sizeof (buf), fp))
361 break;
362 buf[127] = '\0';
363 if (strstr (buf, "CPU part"))
364 part = (u32) strtol (memchr (buf, ':', 128) + 2, NULL, 0);
365 if (-1 != part)
366 break;
367 }
368 fclose (fp);
369
370 return part;
371}
372
Nitin Saxenae2f52362020-08-25 19:58:37 +0530373#define AARCH64_CPU_IMPLEMENTER_CAVIUM 0x43
374#define AARCH64_CPU_PART_THUNDERX2 0x0af
375#define AARCH64_CPU_PART_OCTEONTX2T96 0x0b2
376#define AARCH64_CPU_PART_OCTEONTX2T98 0x0b1
Lijian Zhang2e237212018-09-10 17:13:56 +0800377#define AARCH64_CPU_IMPLEMENTER_QDF24XX 0x51
378#define AARCH64_CPU_PART_QDF24XX 0xc00
379#define AARCH64_CPU_IMPLEMENTER_CORTEXA72 0x41
380#define AARCH64_CPU_PART_CORTEXA72 0xd08
Lijian.Zhang690ce862020-02-18 19:58:19 +0800381#define AARCH64_CPU_IMPLEMENTER_NEOVERSEN1 0x41
382#define AARCH64_CPU_PART_NEOVERSEN1 0xd0c
Lijian Zhang2e237212018-09-10 17:13:56 +0800383
384static inline int
Nitin Saxenae2f52362020-08-25 19:58:37 +0530385clib_cpu_march_priority_octeontx2 ()
386{
387 if ((AARCH64_CPU_IMPLEMENTER_CAVIUM == clib_cpu_implementer ()) &&
388 ((AARCH64_CPU_PART_OCTEONTX2T96 == clib_cpu_part ())
389 || AARCH64_CPU_PART_OCTEONTX2T98 == clib_cpu_part ()))
390 return 20;
391 return -1;
392}
393
394static inline int
Lijian Zhang2e237212018-09-10 17:13:56 +0800395clib_cpu_march_priority_thunderx2t99 ()
396{
Nitin Saxenae2f52362020-08-25 19:58:37 +0530397 if ((AARCH64_CPU_IMPLEMENTER_CAVIUM == clib_cpu_implementer ()) &&
398 (AARCH64_CPU_PART_THUNDERX2 == clib_cpu_part ()))
Lijian Zhang2e237212018-09-10 17:13:56 +0800399 return 20;
400 return -1;
401}
402
403static inline int
404clib_cpu_march_priority_qdf24xx ()
405{
406 if ((AARCH64_CPU_IMPLEMENTER_QDF24XX == clib_cpu_implementer ()) &&
407 (AARCH64_CPU_PART_QDF24XX == clib_cpu_part ()))
408 return 20;
409 return -1;
410}
411
412static inline int
413clib_cpu_march_priority_cortexa72 ()
414{
415 if ((AARCH64_CPU_IMPLEMENTER_CORTEXA72 == clib_cpu_implementer ()) &&
416 (AARCH64_CPU_PART_CORTEXA72 == clib_cpu_part ()))
417 return 10;
418 return -1;
419}
420
Lijian.Zhang690ce862020-02-18 19:58:19 +0800421static inline int
422clib_cpu_march_priority_neoversen1 ()
423{
424 if ((AARCH64_CPU_IMPLEMENTER_NEOVERSEN1 == clib_cpu_implementer ()) &&
425 (AARCH64_CPU_PART_NEOVERSEN1 == clib_cpu_part ()))
426 return 10;
427 return -1;
428}
429
Damjan Marion812b32d2018-05-28 21:26:47 +0200430#ifdef CLIB_MARCH_VARIANT
431#define CLIB_MARCH_FN_PRIORITY() CLIB_MARCH_SFX(clib_cpu_march_priority)()
432#else
433#define CLIB_MARCH_FN_PRIORITY() 0
434#endif
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100435#endif /* included_clib_cpu_h */
436
Florin Coras983cc7d2018-09-18 23:11:55 -0700437#define CLIB_MARCH_FN_CONSTRUCTOR(fn) \
438static void __clib_constructor \
439CLIB_MARCH_SFX(fn ## _march_constructor) (void) \
440{ \
441 if (CLIB_MARCH_FN_PRIORITY() > fn ## _selected_priority) \
442 { \
443 fn ## _selected = & CLIB_MARCH_SFX (fn ## _ma); \
444 fn ## _selected_priority = CLIB_MARCH_FN_PRIORITY(); \
445 } \
446} \
447
448#ifndef CLIB_MARCH_VARIANT
Damjan Marion1bb67ab2021-04-30 11:11:08 +0200449#define CLIB_MARCH_FN(fn, rtype, _args...) \
450 static rtype CLIB_MARCH_SFX (fn##_ma) (_args); \
451 rtype (*fn##_selected) (_args) = &CLIB_MARCH_SFX (fn##_ma); \
452 int fn##_selected_priority = 0; \
453 static inline rtype CLIB_MARCH_SFX (fn##_ma) (_args)
Florin Coras983cc7d2018-09-18 23:11:55 -0700454#else
Damjan Marion1bb67ab2021-04-30 11:11:08 +0200455#define CLIB_MARCH_FN(fn, rtype, _args...) \
456 static rtype CLIB_MARCH_SFX (fn##_ma) (_args); \
457 extern rtype (*fn##_selected) (_args); \
458 extern int fn##_selected_priority; \
459 CLIB_MARCH_FN_CONSTRUCTOR (fn) \
460 static rtype CLIB_MARCH_SFX (fn##_ma) (_args)
Florin Coras983cc7d2018-09-18 23:11:55 -0700461#endif
462
463#define CLIB_MARCH_FN_SELECT(fn) (* fn ## _selected)
464
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100465format_function_t format_cpu_uarch;
Damjan Marion522e4862016-03-04 12:44:14 +0100466format_function_t format_cpu_model_name;
Damjan Marion1c80e832016-05-11 23:07:18 +0200467format_function_t format_cpu_flags;
Damjan Marione3e35552021-05-06 17:34:49 +0200468format_function_t format_march_variant;
Damjan Marion522e4862016-03-04 12:44:14 +0100469
Dave Barachc3799992016-08-15 11:12:27 -0400470/*
471 * fd.io coding-style-patch-verification: ON
472 *
473 * Local Variables:
474 * eval: (c-set-style "gnu")
475 * End:
476 */