blob: 6a812180008c85a864496ddbf37cdc85974f3629 [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
22/*
23 * multiarchitecture support. Adding new entry will produce
24 * new graph node function variant optimized for specific cpu
25 * microarchitecture.
26 * Order is important for runtime selection, as 1st match wins...
27 */
28
29#if __x86_64__ && CLIB_DEBUG == 0
30#define foreach_march_variant(macro, x) \
31 macro(avx2, x, "arch=core-avx2")
32#else
33#define foreach_march_variant(macro, x)
34#endif
35
36
Damjan Marion5ad75f52018-11-29 14:40:30 +010037#if __GNUC__ > 4 && !__clang__ && CLIB_DEBUG == 0
Damjan Marion24223172018-05-28 16:22:14 +020038#define CLIB_CPU_OPTIMIZED __attribute__ ((optimize ("O3")))
Damjan Marion1c80e832016-05-11 23:07:18 +020039#else
40#define CLIB_CPU_OPTIMIZED
41#endif
42
43
44#define CLIB_MULTIARCH_ARCH_CHECK(arch, fn, tgt) \
45 if (clib_cpu_supports_ ## arch()) \
46 return & fn ## _ ##arch;
47
Damjan Marion652d2e12019-02-02 00:15:27 +010048/* FIXME to be removed */
49#define CLIB_MULTIARCH_SELECT_FN(fn,...)
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
87#define CLIB_MARCH_FN_POINTER(fn) \
88 clib_march_select_fn_ptr (fn##_march_fn_registrations);
89
90#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) \
140 _ (invariant_tsc, 0x80000007, edx, 8)
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100141
142#define foreach_aarch64_flags \
143_ (fp, 0) \
144_ (asimd, 1) \
145_ (evtstrm, 2) \
146_ (aarch64_aes, 3) \
147_ (pmull, 4) \
148_ (sha1, 5) \
149_ (sha2, 6) \
150_ (crc32, 7) \
151_ (atomics, 8) \
152_ (fphp, 9) \
153_ (asimdhp, 10) \
154_ (cpuid, 11) \
155_ (asimdrdm, 12) \
156_ (jscvt, 13) \
157_ (fcma, 14) \
158_ (lrcpc, 15) \
159_ (dcpop, 16) \
160_ (sha3, 17) \
161_ (sm3, 18) \
162_ (sm4, 19) \
163_ (asimddp, 20) \
164_ (sha512, 21) \
165_ (sve, 22)
166
Damjan Marion0a78fa12019-01-19 23:45:36 +0100167static inline u32
Damjan Marionee721412019-01-27 17:54:11 +0100168clib_get_current_cpu_id ()
Damjan Marion0a78fa12019-01-19 23:45:36 +0100169{
170 unsigned cpu, node;
171 syscall (__NR_getcpu, &cpu, &node, 0);
172 return cpu;
173}
174
175static inline u32
176clib_get_current_numa_node ()
177{
178 unsigned cpu, node;
179 syscall (__NR_getcpu, &cpu, &node, 0);
180 return node;
181}
182
Christophe Fontaine33e81952016-12-19 14:41:52 +0100183#if defined(__x86_64__)
184#include "cpuid.h"
185
Damjan Marion1c80e832016-05-11 23:07:18 +0200186static inline int
Dave Barachc3799992016-08-15 11:12:27 -0400187clib_get_cpuid (const u32 lev, u32 * eax, u32 * ebx, u32 * ecx, u32 * edx)
Damjan Marion1c80e832016-05-11 23:07:18 +0200188{
189 if ((u32) __get_cpuid_max (0x80000000 & lev, 0) < lev)
190 return 0;
191 if (lev == 7)
Dave Barachc3799992016-08-15 11:12:27 -0400192 __cpuid_count (lev, 0, *eax, *ebx, *ecx, *edx);
Damjan Marion1c80e832016-05-11 23:07:18 +0200193 else
Dave Barachc3799992016-08-15 11:12:27 -0400194 __cpuid (lev, *eax, *ebx, *ecx, *edx);
Damjan Marion1c80e832016-05-11 23:07:18 +0200195 return 1;
196}
197
198
199#define _(flag, func, reg, bit) \
200static inline int \
201clib_cpu_supports_ ## flag() \
202{ \
203 u32 __attribute__((unused)) eax, ebx = 0, ecx = 0, edx = 0; \
204 clib_get_cpuid (func, &eax, &ebx, &ecx, &edx); \
205 \
206 return ((reg & (1 << bit)) != 0); \
207}
Dave Barachc3799992016-08-15 11:12:27 -0400208foreach_x86_64_flags
Damjan Marion1c80e832016-05-11 23:07:18 +0200209#undef _
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100210#else /* __x86_64__ */
Christophe Fontaine33e81952016-12-19 14:41:52 +0100211
212#define _(flag, func, reg, bit) \
213static inline int clib_cpu_supports_ ## flag() { return 0; }
214foreach_x86_64_flags
215#undef _
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100216#endif /* __x86_64__ */
217#if defined(__aarch64__)
218#include <sys/auxv.h>
219#define _(flag, bit) \
220static inline int \
221clib_cpu_supports_ ## flag() \
222{ \
223 unsigned long hwcap = getauxval(AT_HWCAP); \
224 return (hwcap & (1 << bit)); \
225}
226 foreach_aarch64_flags
227#undef _
228#else /* ! __x86_64__ && !__aarch64__ */
229#define _(flag, bit) \
230static inline int clib_cpu_supports_ ## flag() { return 0; }
231 foreach_aarch64_flags
232#undef _
233#endif /* __x86_64__, __aarch64__ */
234/*
235 * aes is the only feature with the same name in both flag lists
236 * handle this by prefixing it with the arch name, and handling it
237 * with the custom function below
238 */
239 static inline int
240clib_cpu_supports_aes ()
241{
Zhiyong Yang7f4fd222019-04-19 03:04:41 -0400242#if defined(__x86_64__)
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100243 return clib_cpu_supports_x86_aes ();
244#elif defined (__aarch64__)
245 return clib_cpu_supports_aarch64_aes ();
246#else
247 return 0;
Christophe Fontaine33e81952016-12-19 14:41:52 +0100248#endif
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100249}
250
Damjan Marion812b32d2018-05-28 21:26:47 +0200251static inline int
Damjan Marion162330f2020-04-29 21:28:15 +0200252clib_cpu_march_priority_icl ()
Damjan Marion812b32d2018-05-28 21:26:47 +0200253{
Damjan Marion162330f2020-04-29 21:28:15 +0200254 if (clib_cpu_supports_avx512_bitalg ())
255 return 200;
Damjan Marion812b32d2018-05-28 21:26:47 +0200256 return -1;
257}
258
259static inline int
Damjan Marion162330f2020-04-29 21:28:15 +0200260clib_cpu_march_priority_skx ()
261{
262 if (clib_cpu_supports_avx512f ())
263 return 100;
264 return -1;
265}
266
267static inline int
Radu Nicolaue1480a22021-01-14 10:25:02 +0000268clib_cpu_march_priority_trm ()
269{
270 if (clib_cpu_supports_movdiri ())
271 return 60;
272 return -1;
273}
274
275static inline int
Damjan Marion162330f2020-04-29 21:28:15 +0200276clib_cpu_march_priority_hsw ()
Damjan Marion812b32d2018-05-28 21:26:47 +0200277{
278 if (clib_cpu_supports_avx2 ())
Damjan Marion64593152019-03-12 19:59:22 +0100279 return 50;
Damjan Marion812b32d2018-05-28 21:26:47 +0200280 return -1;
281}
282
Lijian Zhang2e237212018-09-10 17:13:56 +0800283static inline u32
284clib_cpu_implementer ()
285{
286 char buf[128];
287 static u32 implementer = -1;
288
289 if (-1 != implementer)
290 return implementer;
291
292 FILE *fp = fopen ("/proc/cpuinfo", "r");
293 if (!fp)
294 return implementer;
295
296 while (!feof (fp))
297 {
298 if (!fgets (buf, sizeof (buf), fp))
299 break;
300 buf[127] = '\0';
301 if (strstr (buf, "CPU implementer"))
302 implementer = (u32) strtol (memchr (buf, ':', 128) + 2, NULL, 0);
303 if (-1 != implementer)
304 break;
305 }
306 fclose (fp);
307
308 return implementer;
309}
310
311static inline u32
312clib_cpu_part ()
313{
314 char buf[128];
315 static u32 part = -1;
316
317 if (-1 != part)
318 return part;
319
320 FILE *fp = fopen ("/proc/cpuinfo", "r");
321 if (!fp)
322 return part;
323
324 while (!feof (fp))
325 {
326 if (!fgets (buf, sizeof (buf), fp))
327 break;
328 buf[127] = '\0';
329 if (strstr (buf, "CPU part"))
330 part = (u32) strtol (memchr (buf, ':', 128) + 2, NULL, 0);
331 if (-1 != part)
332 break;
333 }
334 fclose (fp);
335
336 return part;
337}
338
Nitin Saxenae2f52362020-08-25 19:58:37 +0530339#define AARCH64_CPU_IMPLEMENTER_CAVIUM 0x43
340#define AARCH64_CPU_PART_THUNDERX2 0x0af
341#define AARCH64_CPU_PART_OCTEONTX2T96 0x0b2
342#define AARCH64_CPU_PART_OCTEONTX2T98 0x0b1
Lijian Zhang2e237212018-09-10 17:13:56 +0800343#define AARCH64_CPU_IMPLEMENTER_QDF24XX 0x51
344#define AARCH64_CPU_PART_QDF24XX 0xc00
345#define AARCH64_CPU_IMPLEMENTER_CORTEXA72 0x41
346#define AARCH64_CPU_PART_CORTEXA72 0xd08
Lijian.Zhang690ce862020-02-18 19:58:19 +0800347#define AARCH64_CPU_IMPLEMENTER_NEOVERSEN1 0x41
348#define AARCH64_CPU_PART_NEOVERSEN1 0xd0c
Lijian Zhang2e237212018-09-10 17:13:56 +0800349
350static inline int
Nitin Saxenae2f52362020-08-25 19:58:37 +0530351clib_cpu_march_priority_octeontx2 ()
352{
353 if ((AARCH64_CPU_IMPLEMENTER_CAVIUM == clib_cpu_implementer ()) &&
354 ((AARCH64_CPU_PART_OCTEONTX2T96 == clib_cpu_part ())
355 || AARCH64_CPU_PART_OCTEONTX2T98 == clib_cpu_part ()))
356 return 20;
357 return -1;
358}
359
360static inline int
Lijian Zhang2e237212018-09-10 17:13:56 +0800361clib_cpu_march_priority_thunderx2t99 ()
362{
Nitin Saxenae2f52362020-08-25 19:58:37 +0530363 if ((AARCH64_CPU_IMPLEMENTER_CAVIUM == clib_cpu_implementer ()) &&
364 (AARCH64_CPU_PART_THUNDERX2 == clib_cpu_part ()))
Lijian Zhang2e237212018-09-10 17:13:56 +0800365 return 20;
366 return -1;
367}
368
369static inline int
370clib_cpu_march_priority_qdf24xx ()
371{
372 if ((AARCH64_CPU_IMPLEMENTER_QDF24XX == clib_cpu_implementer ()) &&
373 (AARCH64_CPU_PART_QDF24XX == clib_cpu_part ()))
374 return 20;
375 return -1;
376}
377
378static inline int
379clib_cpu_march_priority_cortexa72 ()
380{
381 if ((AARCH64_CPU_IMPLEMENTER_CORTEXA72 == clib_cpu_implementer ()) &&
382 (AARCH64_CPU_PART_CORTEXA72 == clib_cpu_part ()))
383 return 10;
384 return -1;
385}
386
Lijian.Zhang690ce862020-02-18 19:58:19 +0800387static inline int
388clib_cpu_march_priority_neoversen1 ()
389{
390 if ((AARCH64_CPU_IMPLEMENTER_NEOVERSEN1 == clib_cpu_implementer ()) &&
391 (AARCH64_CPU_PART_NEOVERSEN1 == clib_cpu_part ()))
392 return 10;
393 return -1;
394}
395
Damjan Marion812b32d2018-05-28 21:26:47 +0200396#ifdef CLIB_MARCH_VARIANT
397#define CLIB_MARCH_FN_PRIORITY() CLIB_MARCH_SFX(clib_cpu_march_priority)()
398#else
399#define CLIB_MARCH_FN_PRIORITY() 0
400#endif
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100401#endif /* included_clib_cpu_h */
402
Florin Coras983cc7d2018-09-18 23:11:55 -0700403#define CLIB_MARCH_FN_CONSTRUCTOR(fn) \
404static void __clib_constructor \
405CLIB_MARCH_SFX(fn ## _march_constructor) (void) \
406{ \
407 if (CLIB_MARCH_FN_PRIORITY() > fn ## _selected_priority) \
408 { \
409 fn ## _selected = & CLIB_MARCH_SFX (fn ## _ma); \
410 fn ## _selected_priority = CLIB_MARCH_FN_PRIORITY(); \
411 } \
412} \
413
414#ifndef CLIB_MARCH_VARIANT
415#define CLIB_MARCH_FN(fn, rtype, _args...) \
416 static rtype CLIB_CPU_OPTIMIZED CLIB_MARCH_SFX (fn ## _ma)(_args); \
417 rtype (*fn ## _selected) (_args) = & CLIB_MARCH_SFX (fn ## _ma); \
418 int fn ## _selected_priority = 0; \
419 static inline rtype CLIB_CPU_OPTIMIZED \
420 CLIB_MARCH_SFX (fn ## _ma)(_args)
421#else
422#define CLIB_MARCH_FN(fn, rtype, _args...) \
423 static rtype CLIB_CPU_OPTIMIZED CLIB_MARCH_SFX (fn ## _ma)(_args); \
Florin Coras5fc939e2019-03-20 08:11:14 -0700424 extern rtype (*fn ## _selected) (_args); \
Florin Coras983cc7d2018-09-18 23:11:55 -0700425 extern int fn ## _selected_priority; \
426 CLIB_MARCH_FN_CONSTRUCTOR (fn) \
427 static rtype CLIB_CPU_OPTIMIZED CLIB_MARCH_SFX (fn ## _ma)(_args)
428#endif
429
430#define CLIB_MARCH_FN_SELECT(fn) (* fn ## _selected)
431
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100432format_function_t format_cpu_uarch;
Damjan Marion522e4862016-03-04 12:44:14 +0100433format_function_t format_cpu_model_name;
Damjan Marion1c80e832016-05-11 23:07:18 +0200434format_function_t format_cpu_flags;
Damjan Marion522e4862016-03-04 12:44:14 +0100435
Dave Barachc3799992016-08-15 11:12:27 -0400436/*
437 * fd.io coding-style-patch-verification: ON
438 *
439 * Local Variables:
440 * eval: (c-set-style "gnu")
441 * End:
442 */