blob: 6925d584e52695d721bfb9373682eb0be7a695b3 [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 Marion5ad75f52018-11-29 14:40:30 +010048#if __GNUC__ > 4 && !__clang__ && CLIB_DEBUG == 0
Damjan Marion24223172018-05-28 16:22:14 +020049#define CLIB_CPU_OPTIMIZED __attribute__ ((optimize ("O3")))
Damjan Marion1c80e832016-05-11 23:07:18 +020050#else
51#define CLIB_CPU_OPTIMIZED
52#endif
53
Damjan Marion812b32d2018-05-28 21:26:47 +020054#ifdef CLIB_MARCH_VARIANT
Damjan Marion04f3db32017-11-10 21:55:45 +010055#define __CLIB_MULTIARCH_FN(a,b) a##_##b
56#define _CLIB_MULTIARCH_FN(a,b) __CLIB_MULTIARCH_FN(a,b)
Damjan Marion812b32d2018-05-28 21:26:47 +020057#define CLIB_MULTIARCH_FN(fn) _CLIB_MULTIARCH_FN(fn,CLIB_MARCH_VARIANT)
Damjan Marion04f3db32017-11-10 21:55:45 +010058#else
59#define CLIB_MULTIARCH_FN(fn) fn
60#endif
Damjan Marion1c80e832016-05-11 23:07:18 +020061
Damjan Marion812b32d2018-05-28 21:26:47 +020062#define CLIB_MARCH_SFX CLIB_MULTIARCH_FN
63
Damjan Marion910d3692019-01-21 11:48:34 +010064typedef struct _clib_march_fn_registration
65{
66 void *function;
67 int priority;
68 struct _clib_march_fn_registration *next;
69 char *name;
70} clib_march_fn_registration;
71
72static_always_inline void *
73clib_march_select_fn_ptr (clib_march_fn_registration * r)
74{
75 void *rv = 0;
76 int last_prio = -1;
77
78 while (r)
79 {
80 if (last_prio < r->priority)
81 {
82 last_prio = r->priority;
83 rv = r->function;
84 }
85 r = r->next;
86 }
87 return rv;
88}
89
90#define CLIB_MARCH_FN_POINTER(fn) \
91 clib_march_select_fn_ptr (fn##_march_fn_registrations);
92
93#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) \
129 _ (rtm, 7, ebx, 11) \
130 _ (pqm, 7, ebx, 12) \
131 _ (pqe, 7, ebx, 15) \
132 _ (avx512f, 7, ebx, 16) \
133 _ (rdseed, 7, ebx, 18) \
134 _ (x86_aes, 1, ecx, 25) \
135 _ (sha, 7, ebx, 29) \
136 _ (vaes, 7, ecx, 9) \
137 _ (vpclmulqdq, 7, ecx, 10) \
138 _ (avx512_vnni, 7, ecx, 11) \
139 _ (avx512_bitalg, 7, ecx, 12) \
140 _ (avx512_vpopcntdq, 7, ecx, 14) \
141 _ (movdiri, 7, ecx, 27) \
142 _ (movdir64b, 7, ecx, 28) \
143 _ (invariant_tsc, 0x80000007, edx, 8)
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100144
145#define foreach_aarch64_flags \
146_ (fp, 0) \
147_ (asimd, 1) \
148_ (evtstrm, 2) \
149_ (aarch64_aes, 3) \
150_ (pmull, 4) \
151_ (sha1, 5) \
152_ (sha2, 6) \
153_ (crc32, 7) \
154_ (atomics, 8) \
155_ (fphp, 9) \
156_ (asimdhp, 10) \
157_ (cpuid, 11) \
158_ (asimdrdm, 12) \
159_ (jscvt, 13) \
160_ (fcma, 14) \
161_ (lrcpc, 15) \
162_ (dcpop, 16) \
163_ (sha3, 17) \
164_ (sm3, 18) \
165_ (sm4, 19) \
166_ (asimddp, 20) \
167_ (sha512, 21) \
168_ (sve, 22)
169
Damjan Marionf8cb7012020-10-09 17:16:55 +0200170u32 clib_get_current_cpu_id ();
171u32 clib_get_current_numa_node ();
Damjan Marion0a78fa12019-01-19 23:45:36 +0100172
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
188
189#define _(flag, func, reg, bit) \
190static inline int \
191clib_cpu_supports_ ## flag() \
192{ \
193 u32 __attribute__((unused)) eax, ebx = 0, ecx = 0, edx = 0; \
194 clib_get_cpuid (func, &eax, &ebx, &ecx, &edx); \
195 \
196 return ((reg & (1 << bit)) != 0); \
197}
Dave Barachc3799992016-08-15 11:12:27 -0400198foreach_x86_64_flags
Damjan Marion1c80e832016-05-11 23:07:18 +0200199#undef _
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100200#else /* __x86_64__ */
Christophe Fontaine33e81952016-12-19 14:41:52 +0100201
202#define _(flag, func, reg, bit) \
203static inline int clib_cpu_supports_ ## flag() { return 0; }
204foreach_x86_64_flags
205#undef _
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100206#endif /* __x86_64__ */
207#if defined(__aarch64__)
208#include <sys/auxv.h>
209#define _(flag, bit) \
210static inline int \
211clib_cpu_supports_ ## flag() \
212{ \
213 unsigned long hwcap = getauxval(AT_HWCAP); \
214 return (hwcap & (1 << bit)); \
215}
216 foreach_aarch64_flags
217#undef _
218#else /* ! __x86_64__ && !__aarch64__ */
219#define _(flag, bit) \
220static inline int clib_cpu_supports_ ## flag() { return 0; }
221 foreach_aarch64_flags
222#undef _
223#endif /* __x86_64__, __aarch64__ */
224/*
225 * aes is the only feature with the same name in both flag lists
226 * handle this by prefixing it with the arch name, and handling it
227 * with the custom function below
228 */
229 static inline int
230clib_cpu_supports_aes ()
231{
Zhiyong Yang7f4fd222019-04-19 03:04:41 -0400232#if defined(__x86_64__)
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100233 return clib_cpu_supports_x86_aes ();
234#elif defined (__aarch64__)
235 return clib_cpu_supports_aarch64_aes ();
236#else
237 return 0;
Christophe Fontaine33e81952016-12-19 14:41:52 +0100238#endif
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100239}
240
Damjan Marion812b32d2018-05-28 21:26:47 +0200241static inline int
Damjan Marion162330f2020-04-29 21:28:15 +0200242clib_cpu_march_priority_icl ()
Damjan Marion812b32d2018-05-28 21:26:47 +0200243{
Damjan Marion162330f2020-04-29 21:28:15 +0200244 if (clib_cpu_supports_avx512_bitalg ())
245 return 200;
Damjan Marion812b32d2018-05-28 21:26:47 +0200246 return -1;
247}
248
249static inline int
Damjan Marion162330f2020-04-29 21:28:15 +0200250clib_cpu_march_priority_skx ()
251{
252 if (clib_cpu_supports_avx512f ())
253 return 100;
254 return -1;
255}
256
257static inline int
Radu Nicolaue1480a22021-01-14 10:25:02 +0000258clib_cpu_march_priority_trm ()
259{
260 if (clib_cpu_supports_movdiri ())
261 return 60;
262 return -1;
263}
264
265static inline int
Damjan Marion162330f2020-04-29 21:28:15 +0200266clib_cpu_march_priority_hsw ()
Damjan Marion812b32d2018-05-28 21:26:47 +0200267{
268 if (clib_cpu_supports_avx2 ())
Damjan Marion64593152019-03-12 19:59:22 +0100269 return 50;
Damjan Marion812b32d2018-05-28 21:26:47 +0200270 return -1;
271}
272
Lijian Zhang2e237212018-09-10 17:13:56 +0800273static inline u32
274clib_cpu_implementer ()
275{
276 char buf[128];
277 static u32 implementer = -1;
278
279 if (-1 != implementer)
280 return implementer;
281
282 FILE *fp = fopen ("/proc/cpuinfo", "r");
283 if (!fp)
284 return implementer;
285
286 while (!feof (fp))
287 {
288 if (!fgets (buf, sizeof (buf), fp))
289 break;
290 buf[127] = '\0';
291 if (strstr (buf, "CPU implementer"))
292 implementer = (u32) strtol (memchr (buf, ':', 128) + 2, NULL, 0);
293 if (-1 != implementer)
294 break;
295 }
296 fclose (fp);
297
298 return implementer;
299}
300
301static inline u32
302clib_cpu_part ()
303{
304 char buf[128];
305 static u32 part = -1;
306
307 if (-1 != part)
308 return part;
309
310 FILE *fp = fopen ("/proc/cpuinfo", "r");
311 if (!fp)
312 return part;
313
314 while (!feof (fp))
315 {
316 if (!fgets (buf, sizeof (buf), fp))
317 break;
318 buf[127] = '\0';
319 if (strstr (buf, "CPU part"))
320 part = (u32) strtol (memchr (buf, ':', 128) + 2, NULL, 0);
321 if (-1 != part)
322 break;
323 }
324 fclose (fp);
325
326 return part;
327}
328
Nitin Saxenae2f52362020-08-25 19:58:37 +0530329#define AARCH64_CPU_IMPLEMENTER_CAVIUM 0x43
330#define AARCH64_CPU_PART_THUNDERX2 0x0af
331#define AARCH64_CPU_PART_OCTEONTX2T96 0x0b2
332#define AARCH64_CPU_PART_OCTEONTX2T98 0x0b1
Lijian Zhang2e237212018-09-10 17:13:56 +0800333#define AARCH64_CPU_IMPLEMENTER_QDF24XX 0x51
334#define AARCH64_CPU_PART_QDF24XX 0xc00
335#define AARCH64_CPU_IMPLEMENTER_CORTEXA72 0x41
336#define AARCH64_CPU_PART_CORTEXA72 0xd08
Lijian.Zhang690ce862020-02-18 19:58:19 +0800337#define AARCH64_CPU_IMPLEMENTER_NEOVERSEN1 0x41
338#define AARCH64_CPU_PART_NEOVERSEN1 0xd0c
Lijian Zhang2e237212018-09-10 17:13:56 +0800339
340static inline int
Nitin Saxenae2f52362020-08-25 19:58:37 +0530341clib_cpu_march_priority_octeontx2 ()
342{
343 if ((AARCH64_CPU_IMPLEMENTER_CAVIUM == clib_cpu_implementer ()) &&
344 ((AARCH64_CPU_PART_OCTEONTX2T96 == clib_cpu_part ())
345 || AARCH64_CPU_PART_OCTEONTX2T98 == clib_cpu_part ()))
346 return 20;
347 return -1;
348}
349
350static inline int
Lijian Zhang2e237212018-09-10 17:13:56 +0800351clib_cpu_march_priority_thunderx2t99 ()
352{
Nitin Saxenae2f52362020-08-25 19:58:37 +0530353 if ((AARCH64_CPU_IMPLEMENTER_CAVIUM == clib_cpu_implementer ()) &&
354 (AARCH64_CPU_PART_THUNDERX2 == clib_cpu_part ()))
Lijian Zhang2e237212018-09-10 17:13:56 +0800355 return 20;
356 return -1;
357}
358
359static inline int
360clib_cpu_march_priority_qdf24xx ()
361{
362 if ((AARCH64_CPU_IMPLEMENTER_QDF24XX == clib_cpu_implementer ()) &&
363 (AARCH64_CPU_PART_QDF24XX == clib_cpu_part ()))
364 return 20;
365 return -1;
366}
367
368static inline int
369clib_cpu_march_priority_cortexa72 ()
370{
371 if ((AARCH64_CPU_IMPLEMENTER_CORTEXA72 == clib_cpu_implementer ()) &&
372 (AARCH64_CPU_PART_CORTEXA72 == clib_cpu_part ()))
373 return 10;
374 return -1;
375}
376
Lijian.Zhang690ce862020-02-18 19:58:19 +0800377static inline int
378clib_cpu_march_priority_neoversen1 ()
379{
380 if ((AARCH64_CPU_IMPLEMENTER_NEOVERSEN1 == clib_cpu_implementer ()) &&
381 (AARCH64_CPU_PART_NEOVERSEN1 == clib_cpu_part ()))
382 return 10;
383 return -1;
384}
385
Damjan Marion812b32d2018-05-28 21:26:47 +0200386#ifdef CLIB_MARCH_VARIANT
387#define CLIB_MARCH_FN_PRIORITY() CLIB_MARCH_SFX(clib_cpu_march_priority)()
388#else
389#define CLIB_MARCH_FN_PRIORITY() 0
390#endif
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100391#endif /* included_clib_cpu_h */
392
Florin Coras983cc7d2018-09-18 23:11:55 -0700393#define CLIB_MARCH_FN_CONSTRUCTOR(fn) \
394static void __clib_constructor \
395CLIB_MARCH_SFX(fn ## _march_constructor) (void) \
396{ \
397 if (CLIB_MARCH_FN_PRIORITY() > fn ## _selected_priority) \
398 { \
399 fn ## _selected = & CLIB_MARCH_SFX (fn ## _ma); \
400 fn ## _selected_priority = CLIB_MARCH_FN_PRIORITY(); \
401 } \
402} \
403
404#ifndef CLIB_MARCH_VARIANT
405#define CLIB_MARCH_FN(fn, rtype, _args...) \
406 static rtype CLIB_CPU_OPTIMIZED CLIB_MARCH_SFX (fn ## _ma)(_args); \
407 rtype (*fn ## _selected) (_args) = & CLIB_MARCH_SFX (fn ## _ma); \
408 int fn ## _selected_priority = 0; \
409 static inline rtype CLIB_CPU_OPTIMIZED \
410 CLIB_MARCH_SFX (fn ## _ma)(_args)
411#else
412#define CLIB_MARCH_FN(fn, rtype, _args...) \
413 static rtype CLIB_CPU_OPTIMIZED CLIB_MARCH_SFX (fn ## _ma)(_args); \
Florin Coras5fc939e2019-03-20 08:11:14 -0700414 extern rtype (*fn ## _selected) (_args); \
Florin Coras983cc7d2018-09-18 23:11:55 -0700415 extern int fn ## _selected_priority; \
416 CLIB_MARCH_FN_CONSTRUCTOR (fn) \
417 static rtype CLIB_CPU_OPTIMIZED CLIB_MARCH_SFX (fn ## _ma)(_args)
418#endif
419
420#define CLIB_MARCH_FN_SELECT(fn) (* fn ## _selected)
421
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100422format_function_t format_cpu_uarch;
Damjan Marion522e4862016-03-04 12:44:14 +0100423format_function_t format_cpu_model_name;
Damjan Marion1c80e832016-05-11 23:07:18 +0200424format_function_t format_cpu_flags;
Damjan Marion522e4862016-03-04 12:44:14 +0100425
Dave Barachc3799992016-08-15 11:12:27 -0400426/*
427 * fd.io coding-style-patch-verification: ON
428 *
429 * Local Variables:
430 * eval: (c-set-style "gnu")
431 * End:
432 */