blob: 29992bb11ebeeff6beca964a6229aa91495b4a7b [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
Sivaprasad Tummala6a1a8322023-04-17 05:16:11 -070042#define amd_vendor(t1, t2, t3) \
43 ((t1 == 0x68747541) && /* htuA */ \
44 (t2 == 0x444d4163) && /* DMAc */ \
45 (t3 == 0x69746e65)) /* itne */
Damjan Mariona31698b2021-03-10 14:35:28 +010046typedef enum
47{
48 CLIB_MARCH_VARIANT_TYPE = 0,
49#define _(s, n) CLIB_MARCH_VARIANT_TYPE_##s,
50 foreach_march_variant
51#undef _
52 CLIB_MARCH_TYPE_N_VARIANTS
53} clib_march_variant_type_t;
Damjan Marion1c80e832016-05-11 23:07:18 +020054
Damjan Marion812b32d2018-05-28 21:26:47 +020055#ifdef CLIB_MARCH_VARIANT
Damjan Marion04f3db32017-11-10 21:55:45 +010056#define __CLIB_MULTIARCH_FN(a,b) a##_##b
57#define _CLIB_MULTIARCH_FN(a,b) __CLIB_MULTIARCH_FN(a,b)
Damjan Marion812b32d2018-05-28 21:26:47 +020058#define CLIB_MULTIARCH_FN(fn) _CLIB_MULTIARCH_FN(fn,CLIB_MARCH_VARIANT)
Damjan Marion04f3db32017-11-10 21:55:45 +010059#else
60#define CLIB_MULTIARCH_FN(fn) fn
61#endif
Damjan Marion1c80e832016-05-11 23:07:18 +020062
Damjan Marion812b32d2018-05-28 21:26:47 +020063#define CLIB_MARCH_SFX CLIB_MULTIARCH_FN
64
Damjan Marion910d3692019-01-21 11:48:34 +010065typedef struct _clib_march_fn_registration
66{
67 void *function;
68 int priority;
69 struct _clib_march_fn_registration *next;
70 char *name;
71} clib_march_fn_registration;
72
73static_always_inline void *
74clib_march_select_fn_ptr (clib_march_fn_registration * r)
75{
76 void *rv = 0;
77 int last_prio = -1;
78
79 while (r)
80 {
81 if (last_prio < r->priority)
82 {
83 last_prio = r->priority;
84 rv = r->function;
85 }
86 r = r->next;
87 }
88 return rv;
89}
90
Damjan Marion11e0d752021-05-05 20:06:39 +020091#define CLIB_MARCH_FN_POINTER(fn) \
92 (__typeof__ (fn) *) clib_march_select_fn_ptr (fn##_march_fn_registrations);
Damjan Marion910d3692019-01-21 11:48:34 +010093
Mohammed Hawarie7149262022-05-18 10:08:47 +020094#define CLIB_MARCH_FN_VOID_POINTER(fn) \
95 clib_march_select_fn_ptr (fn##_march_fn_registrations);
96
Damjan Marion910d3692019-01-21 11:48:34 +010097#define _CLIB_MARCH_FN_REGISTRATION(fn) \
98static clib_march_fn_registration \
99CLIB_MARCH_SFX(fn##_march_fn_registration) = \
100{ \
101 .name = CLIB_MARCH_VARIANT_STR \
102}; \
103\
104static void __clib_constructor \
105fn##_march_register () \
106{ \
107 clib_march_fn_registration *r; \
108 r = & CLIB_MARCH_SFX (fn##_march_fn_registration); \
109 r->priority = CLIB_MARCH_FN_PRIORITY(); \
110 r->next = fn##_march_fn_registrations; \
111 r->function = CLIB_MARCH_SFX (fn); \
112 fn##_march_fn_registrations = r; \
113}
114
115#ifdef CLIB_MARCH_VARIANT
116#define CLIB_MARCH_FN_REGISTRATION(fn) \
117extern clib_march_fn_registration *fn##_march_fn_registrations; \
118_CLIB_MARCH_FN_REGISTRATION(fn)
119#else
120#define CLIB_MARCH_FN_REGISTRATION(fn) \
121clib_march_fn_registration *fn##_march_fn_registrations = 0; \
122_CLIB_MARCH_FN_REGISTRATION(fn)
123#endif
Radu Nicolaue1480a22021-01-14 10:25:02 +0000124#define foreach_x86_64_flags \
125 _ (sse3, 1, ecx, 0) \
126 _ (pclmulqdq, 1, ecx, 1) \
127 _ (ssse3, 1, ecx, 9) \
128 _ (sse41, 1, ecx, 19) \
129 _ (sse42, 1, ecx, 20) \
130 _ (avx, 1, ecx, 28) \
131 _ (rdrand, 1, ecx, 30) \
132 _ (avx2, 7, ebx, 5) \
Maros Ondrejicka568ef462022-11-10 14:11:40 +0100133 _ (bmi2, 7, ebx, 8) \
Radu Nicolaue1480a22021-01-14 10:25:02 +0000134 _ (rtm, 7, ebx, 11) \
135 _ (pqm, 7, ebx, 12) \
136 _ (pqe, 7, ebx, 15) \
137 _ (avx512f, 7, ebx, 16) \
138 _ (rdseed, 7, ebx, 18) \
139 _ (x86_aes, 1, ecx, 25) \
140 _ (sha, 7, ebx, 29) \
141 _ (vaes, 7, ecx, 9) \
142 _ (vpclmulqdq, 7, ecx, 10) \
143 _ (avx512_vnni, 7, ecx, 11) \
144 _ (avx512_bitalg, 7, ecx, 12) \
145 _ (avx512_vpopcntdq, 7, ecx, 14) \
146 _ (movdiri, 7, ecx, 27) \
147 _ (movdir64b, 7, ecx, 28) \
Damjan Marion15522282023-03-14 13:34:59 +0100148 _ (enqcmd, 7, ecx, 29) \
Ray Kinsella0d27e3e2021-10-15 12:48:31 +0100149 _ (avx512_fp16, 7, edx, 23) \
Radu Nicolaue1480a22021-01-14 10:25:02 +0000150 _ (invariant_tsc, 0x80000007, edx, 8)
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100151
152#define foreach_aarch64_flags \
153_ (fp, 0) \
154_ (asimd, 1) \
155_ (evtstrm, 2) \
156_ (aarch64_aes, 3) \
157_ (pmull, 4) \
158_ (sha1, 5) \
159_ (sha2, 6) \
160_ (crc32, 7) \
161_ (atomics, 8) \
162_ (fphp, 9) \
163_ (asimdhp, 10) \
164_ (cpuid, 11) \
165_ (asimdrdm, 12) \
166_ (jscvt, 13) \
167_ (fcma, 14) \
168_ (lrcpc, 15) \
169_ (dcpop, 16) \
170_ (sha3, 17) \
171_ (sm3, 18) \
172_ (sm4, 19) \
173_ (asimddp, 20) \
174_ (sha512, 21) \
175_ (sve, 22)
176
Dave Barach6c89a352022-12-26 14:01:36 -0500177u32 clib_get_current_cpu_id (void);
178u32 clib_get_current_numa_node (void);
Damjan Marion0a78fa12019-01-19 23:45:36 +0100179
Dave Barach6c89a352022-12-26 14:01:36 -0500180typedef int (*clib_cpu_supports_func_t) (void);
Zachary Leaf268d7be2022-05-12 02:26:00 -0500181
Christophe Fontaine33e81952016-12-19 14:41:52 +0100182#if defined(__x86_64__)
183#include "cpuid.h"
184
Damjan Marion1c80e832016-05-11 23:07:18 +0200185static inline int
Dave Barachc3799992016-08-15 11:12:27 -0400186clib_get_cpuid (const u32 lev, u32 * eax, u32 * ebx, u32 * ecx, u32 * edx)
Damjan Marion1c80e832016-05-11 23:07:18 +0200187{
188 if ((u32) __get_cpuid_max (0x80000000 & lev, 0) < lev)
189 return 0;
190 if (lev == 7)
Dave Barachc3799992016-08-15 11:12:27 -0400191 __cpuid_count (lev, 0, *eax, *ebx, *ecx, *edx);
Damjan Marion1c80e832016-05-11 23:07:18 +0200192 else
Dave Barachc3799992016-08-15 11:12:27 -0400193 __cpuid (lev, *eax, *ebx, *ecx, *edx);
Damjan Marion1c80e832016-05-11 23:07:18 +0200194 return 1;
195}
196
Damjan Marion1c80e832016-05-11 23:07:18 +0200197#define _(flag, func, reg, bit) \
198static inline int \
199clib_cpu_supports_ ## flag() \
200{ \
201 u32 __attribute__((unused)) eax, ebx = 0, ecx = 0, edx = 0; \
202 clib_get_cpuid (func, &eax, &ebx, &ecx, &edx); \
203 \
204 return ((reg & (1 << bit)) != 0); \
205}
Dave Barachc3799992016-08-15 11:12:27 -0400206foreach_x86_64_flags
Damjan Marion1c80e832016-05-11 23:07:18 +0200207#undef _
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100208#else /* __x86_64__ */
Christophe Fontaine33e81952016-12-19 14:41:52 +0100209
210#define _(flag, func, reg, bit) \
211static inline int clib_cpu_supports_ ## flag() { return 0; }
212foreach_x86_64_flags
213#undef _
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100214#endif /* __x86_64__ */
215#if defined(__aarch64__)
216#include <sys/auxv.h>
217#define _(flag, bit) \
218static inline int \
219clib_cpu_supports_ ## flag() \
220{ \
221 unsigned long hwcap = getauxval(AT_HWCAP); \
222 return (hwcap & (1 << bit)); \
223}
224 foreach_aarch64_flags
225#undef _
226#else /* ! __x86_64__ && !__aarch64__ */
227#define _(flag, bit) \
228static inline int clib_cpu_supports_ ## flag() { return 0; }
229 foreach_aarch64_flags
230#undef _
231#endif /* __x86_64__, __aarch64__ */
232/*
233 * aes is the only feature with the same name in both flag lists
234 * handle this by prefixing it with the arch name, and handling it
235 * with the custom function below
236 */
237 static inline int
238clib_cpu_supports_aes ()
239{
Zhiyong Yang7f4fd222019-04-19 03:04:41 -0400240#if defined(__x86_64__)
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100241 return clib_cpu_supports_x86_aes ();
242#elif defined (__aarch64__)
243 return clib_cpu_supports_aarch64_aes ();
244#else
245 return 0;
Christophe Fontaine33e81952016-12-19 14:41:52 +0100246#endif
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100247}
248
Damjan Marion812b32d2018-05-28 21:26:47 +0200249static inline int
Damjan Marion98f7f0a2023-04-17 09:38:11 +0000250clib_cpu_march_priority_scalar ()
251{
252 return 1;
253}
254
255static inline int
Damjan Marion15522282023-03-14 13:34:59 +0100256clib_cpu_march_priority_spr ()
257{
258 if (clib_cpu_supports_enqcmd ())
259 return 300;
260 return -1;
261}
262
263static inline int
Damjan Marion162330f2020-04-29 21:28:15 +0200264clib_cpu_march_priority_icl ()
Damjan Marion812b32d2018-05-28 21:26:47 +0200265{
Damjan Marion162330f2020-04-29 21:28:15 +0200266 if (clib_cpu_supports_avx512_bitalg ())
267 return 200;
Damjan Marion812b32d2018-05-28 21:26:47 +0200268 return -1;
269}
270
271static inline int
Damjan Marion15522282023-03-14 13:34:59 +0100272clib_cpu_march_priority_adl ()
273{
274 if (clib_cpu_supports_movdiri () && clib_cpu_supports_avx2 ())
275 return 150;
276 return -1;
277}
278
279static inline int
Damjan Marion162330f2020-04-29 21:28:15 +0200280clib_cpu_march_priority_skx ()
281{
282 if (clib_cpu_supports_avx512f ())
283 return 100;
284 return -1;
285}
286
287static inline int
Radu Nicolaue1480a22021-01-14 10:25:02 +0000288clib_cpu_march_priority_trm ()
289{
290 if (clib_cpu_supports_movdiri ())
Damjan Marion15522282023-03-14 13:34:59 +0100291 return 40;
Radu Nicolaue1480a22021-01-14 10:25:02 +0000292 return -1;
293}
294
295static inline int
Damjan Marion162330f2020-04-29 21:28:15 +0200296clib_cpu_march_priority_hsw ()
Damjan Marion812b32d2018-05-28 21:26:47 +0200297{
298 if (clib_cpu_supports_avx2 ())
Damjan Marion64593152019-03-12 19:59:22 +0100299 return 50;
Damjan Marion812b32d2018-05-28 21:26:47 +0200300 return -1;
301}
302
Ray Kinsella0024e532021-11-26 14:57:35 +0000303#define X86_CPU_ARCH_PERF_FUNC 0xA
304
305static inline int
306clib_get_pmu_counter_count (u8 *fixed, u8 *general)
307{
308#if defined(__x86_64__)
309 u32 __clib_unused eax = 0, ebx = 0, ecx = 0, edx = 0;
310 clib_get_cpuid (X86_CPU_ARCH_PERF_FUNC, &eax, &ebx, &ecx, &edx);
311
312 *general = (eax & 0xFF00) >> 8;
313 *fixed = (edx & 0xF);
314
315 return 1;
316#else
317 return 0;
318#endif
319}
320
Lijian Zhang2e237212018-09-10 17:13:56 +0800321static inline u32
322clib_cpu_implementer ()
323{
324 char buf[128];
325 static u32 implementer = -1;
326
327 if (-1 != implementer)
328 return implementer;
329
330 FILE *fp = fopen ("/proc/cpuinfo", "r");
331 if (!fp)
332 return implementer;
333
334 while (!feof (fp))
335 {
336 if (!fgets (buf, sizeof (buf), fp))
337 break;
338 buf[127] = '\0';
339 if (strstr (buf, "CPU implementer"))
340 implementer = (u32) strtol (memchr (buf, ':', 128) + 2, NULL, 0);
341 if (-1 != implementer)
342 break;
343 }
344 fclose (fp);
345
346 return implementer;
347}
348
349static inline u32
350clib_cpu_part ()
351{
352 char buf[128];
353 static u32 part = -1;
354
355 if (-1 != part)
356 return part;
357
358 FILE *fp = fopen ("/proc/cpuinfo", "r");
359 if (!fp)
360 return part;
361
362 while (!feof (fp))
363 {
364 if (!fgets (buf, sizeof (buf), fp))
365 break;
366 buf[127] = '\0';
367 if (strstr (buf, "CPU part"))
368 part = (u32) strtol (memchr (buf, ':', 128) + 2, NULL, 0);
369 if (-1 != part)
370 break;
371 }
372 fclose (fp);
373
374 return part;
375}
376
Nitin Saxenae2f52362020-08-25 19:58:37 +0530377#define AARCH64_CPU_IMPLEMENTER_CAVIUM 0x43
378#define AARCH64_CPU_PART_THUNDERX2 0x0af
379#define AARCH64_CPU_PART_OCTEONTX2T96 0x0b2
380#define AARCH64_CPU_PART_OCTEONTX2T98 0x0b1
Lijian Zhang2e237212018-09-10 17:13:56 +0800381#define AARCH64_CPU_IMPLEMENTER_QDF24XX 0x51
382#define AARCH64_CPU_PART_QDF24XX 0xc00
383#define AARCH64_CPU_IMPLEMENTER_CORTEXA72 0x41
384#define AARCH64_CPU_PART_CORTEXA72 0xd08
Lijian.Zhang690ce862020-02-18 19:58:19 +0800385#define AARCH64_CPU_IMPLEMENTER_NEOVERSEN1 0x41
386#define AARCH64_CPU_PART_NEOVERSEN1 0xd0c
Lijian Zhang2e237212018-09-10 17:13:56 +0800387
388static inline int
Nitin Saxenae2f52362020-08-25 19:58:37 +0530389clib_cpu_march_priority_octeontx2 ()
390{
391 if ((AARCH64_CPU_IMPLEMENTER_CAVIUM == clib_cpu_implementer ()) &&
392 ((AARCH64_CPU_PART_OCTEONTX2T96 == clib_cpu_part ())
393 || AARCH64_CPU_PART_OCTEONTX2T98 == clib_cpu_part ()))
394 return 20;
395 return -1;
396}
397
398static inline int
Lijian Zhang2e237212018-09-10 17:13:56 +0800399clib_cpu_march_priority_thunderx2t99 ()
400{
Nitin Saxenae2f52362020-08-25 19:58:37 +0530401 if ((AARCH64_CPU_IMPLEMENTER_CAVIUM == clib_cpu_implementer ()) &&
402 (AARCH64_CPU_PART_THUNDERX2 == clib_cpu_part ()))
Lijian Zhang2e237212018-09-10 17:13:56 +0800403 return 20;
404 return -1;
405}
406
407static inline int
408clib_cpu_march_priority_qdf24xx ()
409{
410 if ((AARCH64_CPU_IMPLEMENTER_QDF24XX == clib_cpu_implementer ()) &&
411 (AARCH64_CPU_PART_QDF24XX == clib_cpu_part ()))
412 return 20;
413 return -1;
414}
415
416static inline int
417clib_cpu_march_priority_cortexa72 ()
418{
419 if ((AARCH64_CPU_IMPLEMENTER_CORTEXA72 == clib_cpu_implementer ()) &&
420 (AARCH64_CPU_PART_CORTEXA72 == clib_cpu_part ()))
421 return 10;
422 return -1;
423}
424
Lijian.Zhang690ce862020-02-18 19:58:19 +0800425static inline int
426clib_cpu_march_priority_neoversen1 ()
427{
428 if ((AARCH64_CPU_IMPLEMENTER_NEOVERSEN1 == clib_cpu_implementer ()) &&
429 (AARCH64_CPU_PART_NEOVERSEN1 == clib_cpu_part ()))
430 return 10;
431 return -1;
432}
433
Damjan Marion812b32d2018-05-28 21:26:47 +0200434#ifdef CLIB_MARCH_VARIANT
435#define CLIB_MARCH_FN_PRIORITY() CLIB_MARCH_SFX(clib_cpu_march_priority)()
436#else
437#define CLIB_MARCH_FN_PRIORITY() 0
438#endif
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100439#endif /* included_clib_cpu_h */
440
Florin Coras983cc7d2018-09-18 23:11:55 -0700441#define CLIB_MARCH_FN_CONSTRUCTOR(fn) \
442static void __clib_constructor \
443CLIB_MARCH_SFX(fn ## _march_constructor) (void) \
444{ \
445 if (CLIB_MARCH_FN_PRIORITY() > fn ## _selected_priority) \
446 { \
447 fn ## _selected = & CLIB_MARCH_SFX (fn ## _ma); \
448 fn ## _selected_priority = CLIB_MARCH_FN_PRIORITY(); \
449 } \
450} \
451
452#ifndef CLIB_MARCH_VARIANT
Damjan Marion1bb67ab2021-04-30 11:11:08 +0200453#define CLIB_MARCH_FN(fn, rtype, _args...) \
454 static rtype CLIB_MARCH_SFX (fn##_ma) (_args); \
455 rtype (*fn##_selected) (_args) = &CLIB_MARCH_SFX (fn##_ma); \
456 int fn##_selected_priority = 0; \
457 static inline rtype CLIB_MARCH_SFX (fn##_ma) (_args)
Florin Coras983cc7d2018-09-18 23:11:55 -0700458#else
Damjan Marion1bb67ab2021-04-30 11:11:08 +0200459#define CLIB_MARCH_FN(fn, rtype, _args...) \
460 static rtype CLIB_MARCH_SFX (fn##_ma) (_args); \
461 extern rtype (*fn##_selected) (_args); \
462 extern int fn##_selected_priority; \
463 CLIB_MARCH_FN_CONSTRUCTOR (fn) \
464 static rtype CLIB_MARCH_SFX (fn##_ma) (_args)
Florin Coras983cc7d2018-09-18 23:11:55 -0700465#endif
466
467#define CLIB_MARCH_FN_SELECT(fn) (* fn ## _selected)
468
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100469format_function_t format_cpu_uarch;
Damjan Marion522e4862016-03-04 12:44:14 +0100470format_function_t format_cpu_model_name;
Damjan Marion1c80e832016-05-11 23:07:18 +0200471format_function_t format_cpu_flags;
Damjan Marione3e35552021-05-06 17:34:49 +0200472format_function_t format_march_variant;
Damjan Marion522e4862016-03-04 12:44:14 +0100473
Dave Barachc3799992016-08-15 11:12:27 -0400474/*
475 * fd.io coding-style-patch-verification: ON
476 *
477 * Local Variables:
478 * eval: (c-set-style "gnu")
479 * End:
480 */