blob: bef2a029e947824560775001ddb73796320051af [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
Radu Nicolau5a48b3b2020-12-02 11:45:11 +000087static_always_inline void *
88clib_march_select_fn_ptr_by_name (clib_march_fn_registration * r, char *name)
89{
90 void *rv = 0;
91
92 while (r)
93 {
94 if (strncmp (name, r->name, vec_len (r->name) - 1) == 0)
95 {
96 rv = r->function;
97 break;
98 }
99 r = r->next;
100 }
101 return rv;
102}
103
Damjan Marion910d3692019-01-21 11:48:34 +0100104#define CLIB_MARCH_FN_POINTER(fn) \
105 clib_march_select_fn_ptr (fn##_march_fn_registrations);
106
Radu Nicolau5a48b3b2020-12-02 11:45:11 +0000107#define CLIB_MARCH_FN_POINTER_BY_NAME(fn, name) \
108 clib_march_select_fn_ptr_by_name (fn##_march_fn_registrations, name);
109
Damjan Marion910d3692019-01-21 11:48:34 +0100110#define _CLIB_MARCH_FN_REGISTRATION(fn) \
111static clib_march_fn_registration \
112CLIB_MARCH_SFX(fn##_march_fn_registration) = \
113{ \
114 .name = CLIB_MARCH_VARIANT_STR \
115}; \
116\
117static void __clib_constructor \
118fn##_march_register () \
119{ \
120 clib_march_fn_registration *r; \
121 r = & CLIB_MARCH_SFX (fn##_march_fn_registration); \
122 r->priority = CLIB_MARCH_FN_PRIORITY(); \
123 r->next = fn##_march_fn_registrations; \
124 r->function = CLIB_MARCH_SFX (fn); \
125 fn##_march_fn_registrations = r; \
126}
127
128#ifdef CLIB_MARCH_VARIANT
129#define CLIB_MARCH_FN_REGISTRATION(fn) \
130extern clib_march_fn_registration *fn##_march_fn_registrations; \
131_CLIB_MARCH_FN_REGISTRATION(fn)
132#else
133#define CLIB_MARCH_FN_REGISTRATION(fn) \
134clib_march_fn_registration *fn##_march_fn_registrations = 0; \
135_CLIB_MARCH_FN_REGISTRATION(fn)
136#endif
Radu Nicolaue1480a22021-01-14 10:25:02 +0000137#define foreach_x86_64_flags \
138 _ (sse3, 1, ecx, 0) \
139 _ (pclmulqdq, 1, ecx, 1) \
140 _ (ssse3, 1, ecx, 9) \
141 _ (sse41, 1, ecx, 19) \
142 _ (sse42, 1, ecx, 20) \
143 _ (avx, 1, ecx, 28) \
144 _ (rdrand, 1, ecx, 30) \
145 _ (avx2, 7, ebx, 5) \
146 _ (rtm, 7, ebx, 11) \
147 _ (pqm, 7, ebx, 12) \
148 _ (pqe, 7, ebx, 15) \
149 _ (avx512f, 7, ebx, 16) \
150 _ (rdseed, 7, ebx, 18) \
151 _ (x86_aes, 1, ecx, 25) \
152 _ (sha, 7, ebx, 29) \
153 _ (vaes, 7, ecx, 9) \
154 _ (vpclmulqdq, 7, ecx, 10) \
155 _ (avx512_vnni, 7, ecx, 11) \
156 _ (avx512_bitalg, 7, ecx, 12) \
157 _ (avx512_vpopcntdq, 7, ecx, 14) \
158 _ (movdiri, 7, ecx, 27) \
159 _ (movdir64b, 7, ecx, 28) \
160 _ (invariant_tsc, 0x80000007, edx, 8)
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100161
162#define foreach_aarch64_flags \
163_ (fp, 0) \
164_ (asimd, 1) \
165_ (evtstrm, 2) \
166_ (aarch64_aes, 3) \
167_ (pmull, 4) \
168_ (sha1, 5) \
169_ (sha2, 6) \
170_ (crc32, 7) \
171_ (atomics, 8) \
172_ (fphp, 9) \
173_ (asimdhp, 10) \
174_ (cpuid, 11) \
175_ (asimdrdm, 12) \
176_ (jscvt, 13) \
177_ (fcma, 14) \
178_ (lrcpc, 15) \
179_ (dcpop, 16) \
180_ (sha3, 17) \
181_ (sm3, 18) \
182_ (sm4, 19) \
183_ (asimddp, 20) \
184_ (sha512, 21) \
185_ (sve, 22)
186
Damjan Marion0a78fa12019-01-19 23:45:36 +0100187static inline u32
Damjan Marionee721412019-01-27 17:54:11 +0100188clib_get_current_cpu_id ()
Damjan Marion0a78fa12019-01-19 23:45:36 +0100189{
190 unsigned cpu, node;
191 syscall (__NR_getcpu, &cpu, &node, 0);
192 return cpu;
193}
194
195static inline u32
196clib_get_current_numa_node ()
197{
198 unsigned cpu, node;
199 syscall (__NR_getcpu, &cpu, &node, 0);
200 return node;
201}
202
Christophe Fontaine33e81952016-12-19 14:41:52 +0100203#if defined(__x86_64__)
204#include "cpuid.h"
205
Damjan Marion1c80e832016-05-11 23:07:18 +0200206static inline int
Dave Barachc3799992016-08-15 11:12:27 -0400207clib_get_cpuid (const u32 lev, u32 * eax, u32 * ebx, u32 * ecx, u32 * edx)
Damjan Marion1c80e832016-05-11 23:07:18 +0200208{
209 if ((u32) __get_cpuid_max (0x80000000 & lev, 0) < lev)
210 return 0;
211 if (lev == 7)
Dave Barachc3799992016-08-15 11:12:27 -0400212 __cpuid_count (lev, 0, *eax, *ebx, *ecx, *edx);
Damjan Marion1c80e832016-05-11 23:07:18 +0200213 else
Dave Barachc3799992016-08-15 11:12:27 -0400214 __cpuid (lev, *eax, *ebx, *ecx, *edx);
Damjan Marion1c80e832016-05-11 23:07:18 +0200215 return 1;
216}
217
218
219#define _(flag, func, reg, bit) \
220static inline int \
221clib_cpu_supports_ ## flag() \
222{ \
223 u32 __attribute__((unused)) eax, ebx = 0, ecx = 0, edx = 0; \
224 clib_get_cpuid (func, &eax, &ebx, &ecx, &edx); \
225 \
226 return ((reg & (1 << bit)) != 0); \
227}
Dave Barachc3799992016-08-15 11:12:27 -0400228foreach_x86_64_flags
Damjan Marion1c80e832016-05-11 23:07:18 +0200229#undef _
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100230#else /* __x86_64__ */
Christophe Fontaine33e81952016-12-19 14:41:52 +0100231
232#define _(flag, func, reg, bit) \
233static inline int clib_cpu_supports_ ## flag() { return 0; }
234foreach_x86_64_flags
235#undef _
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100236#endif /* __x86_64__ */
237#if defined(__aarch64__)
238#include <sys/auxv.h>
239#define _(flag, bit) \
240static inline int \
241clib_cpu_supports_ ## flag() \
242{ \
243 unsigned long hwcap = getauxval(AT_HWCAP); \
244 return (hwcap & (1 << bit)); \
245}
246 foreach_aarch64_flags
247#undef _
248#else /* ! __x86_64__ && !__aarch64__ */
249#define _(flag, bit) \
250static inline int clib_cpu_supports_ ## flag() { return 0; }
251 foreach_aarch64_flags
252#undef _
253#endif /* __x86_64__, __aarch64__ */
254/*
255 * aes is the only feature with the same name in both flag lists
256 * handle this by prefixing it with the arch name, and handling it
257 * with the custom function below
258 */
259 static inline int
260clib_cpu_supports_aes ()
261{
Zhiyong Yang7f4fd222019-04-19 03:04:41 -0400262#if defined(__x86_64__)
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100263 return clib_cpu_supports_x86_aes ();
264#elif defined (__aarch64__)
265 return clib_cpu_supports_aarch64_aes ();
266#else
267 return 0;
Christophe Fontaine33e81952016-12-19 14:41:52 +0100268#endif
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100269}
270
Damjan Marion812b32d2018-05-28 21:26:47 +0200271static inline int
Damjan Marion162330f2020-04-29 21:28:15 +0200272clib_cpu_march_priority_icl ()
Damjan Marion812b32d2018-05-28 21:26:47 +0200273{
Damjan Marion162330f2020-04-29 21:28:15 +0200274 if (clib_cpu_supports_avx512_bitalg ())
275 return 200;
Damjan Marion812b32d2018-05-28 21:26:47 +0200276 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 ())
291 return 60;
292 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
Lijian Zhang2e237212018-09-10 17:13:56 +0800303static inline u32
304clib_cpu_implementer ()
305{
306 char buf[128];
307 static u32 implementer = -1;
308
309 if (-1 != implementer)
310 return implementer;
311
312 FILE *fp = fopen ("/proc/cpuinfo", "r");
313 if (!fp)
314 return implementer;
315
316 while (!feof (fp))
317 {
318 if (!fgets (buf, sizeof (buf), fp))
319 break;
320 buf[127] = '\0';
321 if (strstr (buf, "CPU implementer"))
322 implementer = (u32) strtol (memchr (buf, ':', 128) + 2, NULL, 0);
323 if (-1 != implementer)
324 break;
325 }
326 fclose (fp);
327
328 return implementer;
329}
330
331static inline u32
332clib_cpu_part ()
333{
334 char buf[128];
335 static u32 part = -1;
336
337 if (-1 != part)
338 return part;
339
340 FILE *fp = fopen ("/proc/cpuinfo", "r");
341 if (!fp)
342 return part;
343
344 while (!feof (fp))
345 {
346 if (!fgets (buf, sizeof (buf), fp))
347 break;
348 buf[127] = '\0';
349 if (strstr (buf, "CPU part"))
350 part = (u32) strtol (memchr (buf, ':', 128) + 2, NULL, 0);
351 if (-1 != part)
352 break;
353 }
354 fclose (fp);
355
356 return part;
357}
358
Nitin Saxenae2f52362020-08-25 19:58:37 +0530359#define AARCH64_CPU_IMPLEMENTER_CAVIUM 0x43
360#define AARCH64_CPU_PART_THUNDERX2 0x0af
361#define AARCH64_CPU_PART_OCTEONTX2T96 0x0b2
362#define AARCH64_CPU_PART_OCTEONTX2T98 0x0b1
Lijian Zhang2e237212018-09-10 17:13:56 +0800363#define AARCH64_CPU_IMPLEMENTER_QDF24XX 0x51
364#define AARCH64_CPU_PART_QDF24XX 0xc00
365#define AARCH64_CPU_IMPLEMENTER_CORTEXA72 0x41
366#define AARCH64_CPU_PART_CORTEXA72 0xd08
Lijian.Zhang690ce862020-02-18 19:58:19 +0800367#define AARCH64_CPU_IMPLEMENTER_NEOVERSEN1 0x41
368#define AARCH64_CPU_PART_NEOVERSEN1 0xd0c
Lijian Zhang2e237212018-09-10 17:13:56 +0800369
370static inline int
Nitin Saxenae2f52362020-08-25 19:58:37 +0530371clib_cpu_march_priority_octeontx2 ()
372{
373 if ((AARCH64_CPU_IMPLEMENTER_CAVIUM == clib_cpu_implementer ()) &&
374 ((AARCH64_CPU_PART_OCTEONTX2T96 == clib_cpu_part ())
375 || AARCH64_CPU_PART_OCTEONTX2T98 == clib_cpu_part ()))
376 return 20;
377 return -1;
378}
379
380static inline int
Lijian Zhang2e237212018-09-10 17:13:56 +0800381clib_cpu_march_priority_thunderx2t99 ()
382{
Nitin Saxenae2f52362020-08-25 19:58:37 +0530383 if ((AARCH64_CPU_IMPLEMENTER_CAVIUM == clib_cpu_implementer ()) &&
384 (AARCH64_CPU_PART_THUNDERX2 == clib_cpu_part ()))
Lijian Zhang2e237212018-09-10 17:13:56 +0800385 return 20;
386 return -1;
387}
388
389static inline int
390clib_cpu_march_priority_qdf24xx ()
391{
392 if ((AARCH64_CPU_IMPLEMENTER_QDF24XX == clib_cpu_implementer ()) &&
393 (AARCH64_CPU_PART_QDF24XX == clib_cpu_part ()))
394 return 20;
395 return -1;
396}
397
398static inline int
399clib_cpu_march_priority_cortexa72 ()
400{
401 if ((AARCH64_CPU_IMPLEMENTER_CORTEXA72 == clib_cpu_implementer ()) &&
402 (AARCH64_CPU_PART_CORTEXA72 == clib_cpu_part ()))
403 return 10;
404 return -1;
405}
406
Lijian.Zhang690ce862020-02-18 19:58:19 +0800407static inline int
408clib_cpu_march_priority_neoversen1 ()
409{
410 if ((AARCH64_CPU_IMPLEMENTER_NEOVERSEN1 == clib_cpu_implementer ()) &&
411 (AARCH64_CPU_PART_NEOVERSEN1 == clib_cpu_part ()))
412 return 10;
413 return -1;
414}
415
Damjan Marion812b32d2018-05-28 21:26:47 +0200416#ifdef CLIB_MARCH_VARIANT
417#define CLIB_MARCH_FN_PRIORITY() CLIB_MARCH_SFX(clib_cpu_march_priority)()
418#else
419#define CLIB_MARCH_FN_PRIORITY() 0
420#endif
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100421#endif /* included_clib_cpu_h */
422
Florin Coras983cc7d2018-09-18 23:11:55 -0700423#define CLIB_MARCH_FN_CONSTRUCTOR(fn) \
424static void __clib_constructor \
425CLIB_MARCH_SFX(fn ## _march_constructor) (void) \
426{ \
427 if (CLIB_MARCH_FN_PRIORITY() > fn ## _selected_priority) \
428 { \
429 fn ## _selected = & CLIB_MARCH_SFX (fn ## _ma); \
430 fn ## _selected_priority = CLIB_MARCH_FN_PRIORITY(); \
431 } \
432} \
433
434#ifndef CLIB_MARCH_VARIANT
435#define CLIB_MARCH_FN(fn, rtype, _args...) \
436 static rtype CLIB_CPU_OPTIMIZED CLIB_MARCH_SFX (fn ## _ma)(_args); \
437 rtype (*fn ## _selected) (_args) = & CLIB_MARCH_SFX (fn ## _ma); \
438 int fn ## _selected_priority = 0; \
439 static inline rtype CLIB_CPU_OPTIMIZED \
440 CLIB_MARCH_SFX (fn ## _ma)(_args)
441#else
442#define CLIB_MARCH_FN(fn, rtype, _args...) \
443 static rtype CLIB_CPU_OPTIMIZED CLIB_MARCH_SFX (fn ## _ma)(_args); \
Florin Coras5fc939e2019-03-20 08:11:14 -0700444 extern rtype (*fn ## _selected) (_args); \
Florin Coras983cc7d2018-09-18 23:11:55 -0700445 extern int fn ## _selected_priority; \
446 CLIB_MARCH_FN_CONSTRUCTOR (fn) \
447 static rtype CLIB_CPU_OPTIMIZED CLIB_MARCH_SFX (fn ## _ma)(_args)
448#endif
449
450#define CLIB_MARCH_FN_SELECT(fn) (* fn ## _selected)
451
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100452format_function_t format_cpu_uarch;
Damjan Marion522e4862016-03-04 12:44:14 +0100453format_function_t format_cpu_model_name;
Damjan Marion1c80e832016-05-11 23:07:18 +0200454format_function_t format_cpu_flags;
Damjan Marion522e4862016-03-04 12:44:14 +0100455
Dave Barachc3799992016-08-15 11:12:27 -0400456/*
457 * fd.io coding-style-patch-verification: ON
458 *
459 * Local Variables:
460 * eval: (c-set-style "gnu")
461 * End:
462 */