vlib: refactor node function variants
[vpp.git] / src / vppinfra / cpu.h
1 /*
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
19 #include <sys/syscall.h>
20 #include <vppinfra/format.h>
21
22 #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")
35 #else
36 #define foreach_march_variant
37 #endif
38
39 typedef 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;
47
48 #if __GNUC__ > 4  && !__clang__ && CLIB_DEBUG == 0
49 #define CLIB_CPU_OPTIMIZED __attribute__ ((optimize ("O3")))
50 #else
51 #define CLIB_CPU_OPTIMIZED
52 #endif
53
54 #ifdef CLIB_MARCH_VARIANT
55 #define __CLIB_MULTIARCH_FN(a,b) a##_##b
56 #define _CLIB_MULTIARCH_FN(a,b) __CLIB_MULTIARCH_FN(a,b)
57 #define CLIB_MULTIARCH_FN(fn) _CLIB_MULTIARCH_FN(fn,CLIB_MARCH_VARIANT)
58 #else
59 #define CLIB_MULTIARCH_FN(fn) fn
60 #endif
61
62 #define CLIB_MARCH_SFX CLIB_MULTIARCH_FN
63
64 typedef 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
72 static_always_inline void *
73 clib_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) \
94 static clib_march_fn_registration \
95 CLIB_MARCH_SFX(fn##_march_fn_registration) = \
96 { \
97   .name = CLIB_MARCH_VARIANT_STR \
98 }; \
99 \
100 static void __clib_constructor \
101 fn##_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) \
113 extern clib_march_fn_registration *fn##_march_fn_registrations; \
114 _CLIB_MARCH_FN_REGISTRATION(fn)
115 #else
116 #define CLIB_MARCH_FN_REGISTRATION(fn) \
117 clib_march_fn_registration *fn##_march_fn_registrations = 0; \
118 _CLIB_MARCH_FN_REGISTRATION(fn)
119 #endif
120 #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)
144
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
170 static inline u32
171 clib_get_current_cpu_id ()
172 {
173   unsigned cpu, node;
174   syscall (__NR_getcpu, &cpu, &node, 0);
175   return cpu;
176 }
177
178 static inline u32
179 clib_get_current_numa_node ()
180 {
181   unsigned cpu, node;
182   syscall (__NR_getcpu, &cpu, &node, 0);
183   return node;
184 }
185
186 #if defined(__x86_64__)
187 #include "cpuid.h"
188
189 static inline int
190 clib_get_cpuid (const u32 lev, u32 * eax, u32 * ebx, u32 * ecx, u32 * edx)
191 {
192   if ((u32) __get_cpuid_max (0x80000000 & lev, 0) < lev)
193     return 0;
194   if (lev == 7)
195     __cpuid_count (lev, 0, *eax, *ebx, *ecx, *edx);
196   else
197     __cpuid (lev, *eax, *ebx, *ecx, *edx);
198   return 1;
199 }
200
201
202 #define _(flag, func, reg, bit) \
203 static inline int                                                       \
204 clib_cpu_supports_ ## flag()                                            \
205 {                                                                       \
206   u32 __attribute__((unused)) eax, ebx = 0, ecx = 0, edx  = 0;          \
207   clib_get_cpuid (func, &eax, &ebx, &ecx, &edx);                        \
208                                                                         \
209   return ((reg & (1 << bit)) != 0);                                     \
210 }
211 foreach_x86_64_flags
212 #undef _
213 #else /* __x86_64__ */
214
215 #define _(flag, func, reg, bit) \
216 static inline int clib_cpu_supports_ ## flag() { return 0; }
217 foreach_x86_64_flags
218 #undef _
219 #endif /* __x86_64__ */
220 #if defined(__aarch64__)
221 #include <sys/auxv.h>
222 #define _(flag, bit) \
223 static inline int                                                       \
224 clib_cpu_supports_ ## flag()                                            \
225 {                                                                       \
226   unsigned long hwcap = getauxval(AT_HWCAP);                            \
227   return (hwcap & (1 << bit));                                          \
228 }
229   foreach_aarch64_flags
230 #undef _
231 #else /* ! __x86_64__ && !__aarch64__ */
232 #define _(flag, bit) \
233 static inline int clib_cpu_supports_ ## flag() { return 0; }
234   foreach_aarch64_flags
235 #undef _
236 #endif /* __x86_64__, __aarch64__ */
237 /*
238  * aes is the only feature with the same name in both flag lists
239  * handle this by prefixing it with the arch name, and handling it
240  * with the custom function below
241  */
242   static inline int
243 clib_cpu_supports_aes ()
244 {
245 #if defined(__x86_64__)
246   return clib_cpu_supports_x86_aes ();
247 #elif defined (__aarch64__)
248   return clib_cpu_supports_aarch64_aes ();
249 #else
250   return 0;
251 #endif
252 }
253
254 static inline int
255 clib_cpu_march_priority_icl ()
256 {
257   if (clib_cpu_supports_avx512_bitalg ())
258     return 200;
259   return -1;
260 }
261
262 static inline int
263 clib_cpu_march_priority_skx ()
264 {
265   if (clib_cpu_supports_avx512f ())
266     return 100;
267   return -1;
268 }
269
270 static inline int
271 clib_cpu_march_priority_trm ()
272 {
273   if (clib_cpu_supports_movdiri ())
274     return 60;
275   return -1;
276 }
277
278 static inline int
279 clib_cpu_march_priority_hsw ()
280 {
281   if (clib_cpu_supports_avx2 ())
282     return 50;
283   return -1;
284 }
285
286 static inline u32
287 clib_cpu_implementer ()
288 {
289   char buf[128];
290   static u32 implementer = -1;
291
292   if (-1 != implementer)
293     return implementer;
294
295   FILE *fp = fopen ("/proc/cpuinfo", "r");
296   if (!fp)
297     return implementer;
298
299   while (!feof (fp))
300     {
301       if (!fgets (buf, sizeof (buf), fp))
302         break;
303       buf[127] = '\0';
304       if (strstr (buf, "CPU implementer"))
305         implementer = (u32) strtol (memchr (buf, ':', 128) + 2, NULL, 0);
306       if (-1 != implementer)
307         break;
308     }
309   fclose (fp);
310
311   return implementer;
312 }
313
314 static inline u32
315 clib_cpu_part ()
316 {
317   char buf[128];
318   static u32 part = -1;
319
320   if (-1 != part)
321     return part;
322
323   FILE *fp = fopen ("/proc/cpuinfo", "r");
324   if (!fp)
325     return part;
326
327   while (!feof (fp))
328     {
329       if (!fgets (buf, sizeof (buf), fp))
330         break;
331       buf[127] = '\0';
332       if (strstr (buf, "CPU part"))
333         part = (u32) strtol (memchr (buf, ':', 128) + 2, NULL, 0);
334       if (-1 != part)
335         break;
336     }
337   fclose (fp);
338
339   return part;
340 }
341
342 #define AARCH64_CPU_IMPLEMENTER_CAVIUM      0x43
343 #define AARCH64_CPU_PART_THUNDERX2          0x0af
344 #define AARCH64_CPU_PART_OCTEONTX2T96       0x0b2
345 #define AARCH64_CPU_PART_OCTEONTX2T98       0x0b1
346 #define AARCH64_CPU_IMPLEMENTER_QDF24XX     0x51
347 #define AARCH64_CPU_PART_QDF24XX            0xc00
348 #define AARCH64_CPU_IMPLEMENTER_CORTEXA72   0x41
349 #define AARCH64_CPU_PART_CORTEXA72          0xd08
350 #define AARCH64_CPU_IMPLEMENTER_NEOVERSEN1  0x41
351 #define AARCH64_CPU_PART_NEOVERSEN1         0xd0c
352
353 static inline int
354 clib_cpu_march_priority_octeontx2 ()
355 {
356   if ((AARCH64_CPU_IMPLEMENTER_CAVIUM == clib_cpu_implementer ()) &&
357       ((AARCH64_CPU_PART_OCTEONTX2T96 == clib_cpu_part ())
358        || AARCH64_CPU_PART_OCTEONTX2T98 == clib_cpu_part ()))
359     return 20;
360   return -1;
361 }
362
363 static inline int
364 clib_cpu_march_priority_thunderx2t99 ()
365 {
366   if ((AARCH64_CPU_IMPLEMENTER_CAVIUM == clib_cpu_implementer ()) &&
367       (AARCH64_CPU_PART_THUNDERX2 == clib_cpu_part ()))
368     return 20;
369   return -1;
370 }
371
372 static inline int
373 clib_cpu_march_priority_qdf24xx ()
374 {
375   if ((AARCH64_CPU_IMPLEMENTER_QDF24XX == clib_cpu_implementer ()) &&
376       (AARCH64_CPU_PART_QDF24XX == clib_cpu_part ()))
377     return 20;
378   return -1;
379 }
380
381 static inline int
382 clib_cpu_march_priority_cortexa72 ()
383 {
384   if ((AARCH64_CPU_IMPLEMENTER_CORTEXA72 == clib_cpu_implementer ()) &&
385       (AARCH64_CPU_PART_CORTEXA72 == clib_cpu_part ()))
386     return 10;
387   return -1;
388 }
389
390 static inline int
391 clib_cpu_march_priority_neoversen1 ()
392 {
393   if ((AARCH64_CPU_IMPLEMENTER_NEOVERSEN1 == clib_cpu_implementer ()) &&
394       (AARCH64_CPU_PART_NEOVERSEN1 == clib_cpu_part ()))
395     return 10;
396   return -1;
397 }
398
399 #ifdef CLIB_MARCH_VARIANT
400 #define CLIB_MARCH_FN_PRIORITY() CLIB_MARCH_SFX(clib_cpu_march_priority)()
401 #else
402 #define CLIB_MARCH_FN_PRIORITY() 0
403 #endif
404 #endif /* included_clib_cpu_h */
405
406 #define CLIB_MARCH_FN_CONSTRUCTOR(fn)                                   \
407 static void __clib_constructor                                          \
408 CLIB_MARCH_SFX(fn ## _march_constructor) (void)                         \
409 {                                                                       \
410   if (CLIB_MARCH_FN_PRIORITY() > fn ## _selected_priority)              \
411     {                                                                   \
412       fn ## _selected = & CLIB_MARCH_SFX (fn ## _ma);                   \
413       fn ## _selected_priority = CLIB_MARCH_FN_PRIORITY();              \
414     }                                                                   \
415 }                                                                       \
416
417 #ifndef CLIB_MARCH_VARIANT
418 #define CLIB_MARCH_FN(fn, rtype, _args...)                              \
419   static rtype CLIB_CPU_OPTIMIZED CLIB_MARCH_SFX (fn ## _ma)(_args);    \
420   rtype (*fn ## _selected) (_args) = & CLIB_MARCH_SFX (fn ## _ma);      \
421   int fn ## _selected_priority = 0;                                     \
422   static inline rtype CLIB_CPU_OPTIMIZED                                \
423   CLIB_MARCH_SFX (fn ## _ma)(_args)
424 #else
425 #define CLIB_MARCH_FN(fn, rtype, _args...)                              \
426   static rtype CLIB_CPU_OPTIMIZED CLIB_MARCH_SFX (fn ## _ma)(_args);    \
427   extern rtype (*fn ## _selected) (_args);                              \
428   extern int fn ## _selected_priority;                                  \
429   CLIB_MARCH_FN_CONSTRUCTOR (fn)                                        \
430   static rtype CLIB_CPU_OPTIMIZED CLIB_MARCH_SFX (fn ## _ma)(_args)
431 #endif
432
433 #define CLIB_MARCH_FN_SELECT(fn) (* fn ## _selected)
434
435 format_function_t format_cpu_uarch;
436 format_function_t format_cpu_model_name;
437 format_function_t format_cpu_flags;
438
439 /*
440  * fd.io coding-style-patch-verification: ON
441  *
442  * Local Variables:
443  * eval: (c-set-style "gnu")
444  * End:
445  */