vppinfra: keep AddressSanitizer happy
[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 #ifdef CLIB_MARCH_VARIANT
49 #define __CLIB_MULTIARCH_FN(a,b) a##_##b
50 #define _CLIB_MULTIARCH_FN(a,b) __CLIB_MULTIARCH_FN(a,b)
51 #define CLIB_MULTIARCH_FN(fn) _CLIB_MULTIARCH_FN(fn,CLIB_MARCH_VARIANT)
52 #else
53 #define CLIB_MULTIARCH_FN(fn) fn
54 #endif
55
56 #define CLIB_MARCH_SFX CLIB_MULTIARCH_FN
57
58 typedef struct _clib_march_fn_registration
59 {
60   void *function;
61   int priority;
62   struct _clib_march_fn_registration *next;
63   char *name;
64 } clib_march_fn_registration;
65
66 static_always_inline void *
67 clib_march_select_fn_ptr (clib_march_fn_registration * r)
68 {
69   void *rv = 0;
70   int last_prio = -1;
71
72   while (r)
73     {
74       if (last_prio < r->priority)
75         {
76           last_prio = r->priority;
77           rv = r->function;
78         }
79       r = r->next;
80     }
81   return rv;
82 }
83
84 #define CLIB_MARCH_FN_POINTER(fn)                                             \
85   (__typeof__ (fn) *) clib_march_select_fn_ptr (fn##_march_fn_registrations);
86
87 #define CLIB_MARCH_FN_VOID_POINTER(fn)                                        \
88   clib_march_select_fn_ptr (fn##_march_fn_registrations);
89
90 #define _CLIB_MARCH_FN_REGISTRATION(fn) \
91 static clib_march_fn_registration \
92 CLIB_MARCH_SFX(fn##_march_fn_registration) = \
93 { \
94   .name = CLIB_MARCH_VARIANT_STR \
95 }; \
96 \
97 static void __clib_constructor \
98 fn##_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) \
110 extern clib_march_fn_registration *fn##_march_fn_registrations; \
111 _CLIB_MARCH_FN_REGISTRATION(fn)
112 #else
113 #define CLIB_MARCH_FN_REGISTRATION(fn) \
114 clib_march_fn_registration *fn##_march_fn_registrations = 0; \
115 _CLIB_MARCH_FN_REGISTRATION(fn)
116 #endif
117 #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   _ (bmi2, 7, ebx, 8)                                                         \
127   _ (rtm, 7, ebx, 11)                                                         \
128   _ (pqm, 7, ebx, 12)                                                         \
129   _ (pqe, 7, ebx, 15)                                                         \
130   _ (avx512f, 7, ebx, 16)                                                     \
131   _ (rdseed, 7, ebx, 18)                                                      \
132   _ (x86_aes, 1, ecx, 25)                                                     \
133   _ (sha, 7, ebx, 29)                                                         \
134   _ (vaes, 7, ecx, 9)                                                         \
135   _ (vpclmulqdq, 7, ecx, 10)                                                  \
136   _ (avx512_vnni, 7, ecx, 11)                                                 \
137   _ (avx512_bitalg, 7, ecx, 12)                                               \
138   _ (avx512_vpopcntdq, 7, ecx, 14)                                            \
139   _ (movdiri, 7, ecx, 27)                                                     \
140   _ (movdir64b, 7, ecx, 28)                                                   \
141   _ (avx512_fp16, 7, edx, 23)                                                 \
142   _ (invariant_tsc, 0x80000007, edx, 8)
143
144 #define foreach_aarch64_flags \
145 _ (fp,          0) \
146 _ (asimd,       1) \
147 _ (evtstrm,     2) \
148 _ (aarch64_aes, 3) \
149 _ (pmull,       4) \
150 _ (sha1,        5) \
151 _ (sha2,        6) \
152 _ (crc32,       7) \
153 _ (atomics,     8) \
154 _ (fphp,        9) \
155 _ (asimdhp,    10) \
156 _ (cpuid,      11) \
157 _ (asimdrdm,   12) \
158 _ (jscvt,      13) \
159 _ (fcma,       14) \
160 _ (lrcpc,      15) \
161 _ (dcpop,      16) \
162 _ (sha3,       17) \
163 _ (sm3,        18) \
164 _ (sm4,        19) \
165 _ (asimddp,    20) \
166 _ (sha512,     21) \
167 _ (sve,        22)
168
169 u32 clib_get_current_cpu_id (void);
170 u32 clib_get_current_numa_node (void);
171
172 typedef int (*clib_cpu_supports_func_t) (void);
173
174 #if defined(__x86_64__)
175 #include "cpuid.h"
176
177 static inline int
178 clib_get_cpuid (const u32 lev, u32 * eax, u32 * ebx, u32 * ecx, u32 * edx)
179 {
180   if ((u32) __get_cpuid_max (0x80000000 & lev, 0) < lev)
181     return 0;
182   if (lev == 7)
183     __cpuid_count (lev, 0, *eax, *ebx, *ecx, *edx);
184   else
185     __cpuid (lev, *eax, *ebx, *ecx, *edx);
186   return 1;
187 }
188
189 #define _(flag, func, reg, bit) \
190 static inline int                                                       \
191 clib_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 }
198 foreach_x86_64_flags
199 #undef _
200 #else /* __x86_64__ */
201
202 #define _(flag, func, reg, bit) \
203 static inline int clib_cpu_supports_ ## flag() { return 0; }
204 foreach_x86_64_flags
205 #undef _
206 #endif /* __x86_64__ */
207 #if defined(__aarch64__)
208 #include <sys/auxv.h>
209 #define _(flag, bit) \
210 static inline int                                                       \
211 clib_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) \
220 static 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
230 clib_cpu_supports_aes ()
231 {
232 #if defined(__x86_64__)
233   return clib_cpu_supports_x86_aes ();
234 #elif defined (__aarch64__)
235   return clib_cpu_supports_aarch64_aes ();
236 #else
237   return 0;
238 #endif
239 }
240
241 static inline int
242 clib_cpu_march_priority_icl ()
243 {
244   if (clib_cpu_supports_avx512_bitalg ())
245     return 200;
246   return -1;
247 }
248
249 static inline int
250 clib_cpu_march_priority_skx ()
251 {
252   if (clib_cpu_supports_avx512f ())
253     return 100;
254   return -1;
255 }
256
257 static inline int
258 clib_cpu_march_priority_trm ()
259 {
260   if (clib_cpu_supports_movdiri ())
261     return 60;
262   return -1;
263 }
264
265 static inline int
266 clib_cpu_march_priority_hsw ()
267 {
268   if (clib_cpu_supports_avx2 ())
269     return 50;
270   return -1;
271 }
272
273 #define X86_CPU_ARCH_PERF_FUNC 0xA
274
275 static inline int
276 clib_get_pmu_counter_count (u8 *fixed, u8 *general)
277 {
278 #if defined(__x86_64__)
279   u32 __clib_unused eax = 0, ebx = 0, ecx = 0, edx = 0;
280   clib_get_cpuid (X86_CPU_ARCH_PERF_FUNC, &eax, &ebx, &ecx, &edx);
281
282   *general = (eax & 0xFF00) >> 8;
283   *fixed = (edx & 0xF);
284
285   return 1;
286 #else
287   return 0;
288 #endif
289 }
290
291 static inline u32
292 clib_cpu_implementer ()
293 {
294   char buf[128];
295   static u32 implementer = -1;
296
297   if (-1 != implementer)
298     return implementer;
299
300   FILE *fp = fopen ("/proc/cpuinfo", "r");
301   if (!fp)
302     return implementer;
303
304   while (!feof (fp))
305     {
306       if (!fgets (buf, sizeof (buf), fp))
307         break;
308       buf[127] = '\0';
309       if (strstr (buf, "CPU implementer"))
310         implementer = (u32) strtol (memchr (buf, ':', 128) + 2, NULL, 0);
311       if (-1 != implementer)
312         break;
313     }
314   fclose (fp);
315
316   return implementer;
317 }
318
319 static inline u32
320 clib_cpu_part ()
321 {
322   char buf[128];
323   static u32 part = -1;
324
325   if (-1 != part)
326     return part;
327
328   FILE *fp = fopen ("/proc/cpuinfo", "r");
329   if (!fp)
330     return part;
331
332   while (!feof (fp))
333     {
334       if (!fgets (buf, sizeof (buf), fp))
335         break;
336       buf[127] = '\0';
337       if (strstr (buf, "CPU part"))
338         part = (u32) strtol (memchr (buf, ':', 128) + 2, NULL, 0);
339       if (-1 != part)
340         break;
341     }
342   fclose (fp);
343
344   return part;
345 }
346
347 #define AARCH64_CPU_IMPLEMENTER_CAVIUM      0x43
348 #define AARCH64_CPU_PART_THUNDERX2          0x0af
349 #define AARCH64_CPU_PART_OCTEONTX2T96       0x0b2
350 #define AARCH64_CPU_PART_OCTEONTX2T98       0x0b1
351 #define AARCH64_CPU_IMPLEMENTER_QDF24XX     0x51
352 #define AARCH64_CPU_PART_QDF24XX            0xc00
353 #define AARCH64_CPU_IMPLEMENTER_CORTEXA72   0x41
354 #define AARCH64_CPU_PART_CORTEXA72          0xd08
355 #define AARCH64_CPU_IMPLEMENTER_NEOVERSEN1  0x41
356 #define AARCH64_CPU_PART_NEOVERSEN1         0xd0c
357
358 static inline int
359 clib_cpu_march_priority_octeontx2 ()
360 {
361   if ((AARCH64_CPU_IMPLEMENTER_CAVIUM == clib_cpu_implementer ()) &&
362       ((AARCH64_CPU_PART_OCTEONTX2T96 == clib_cpu_part ())
363        || AARCH64_CPU_PART_OCTEONTX2T98 == clib_cpu_part ()))
364     return 20;
365   return -1;
366 }
367
368 static inline int
369 clib_cpu_march_priority_thunderx2t99 ()
370 {
371   if ((AARCH64_CPU_IMPLEMENTER_CAVIUM == clib_cpu_implementer ()) &&
372       (AARCH64_CPU_PART_THUNDERX2 == clib_cpu_part ()))
373     return 20;
374   return -1;
375 }
376
377 static inline int
378 clib_cpu_march_priority_qdf24xx ()
379 {
380   if ((AARCH64_CPU_IMPLEMENTER_QDF24XX == clib_cpu_implementer ()) &&
381       (AARCH64_CPU_PART_QDF24XX == clib_cpu_part ()))
382     return 20;
383   return -1;
384 }
385
386 static inline int
387 clib_cpu_march_priority_cortexa72 ()
388 {
389   if ((AARCH64_CPU_IMPLEMENTER_CORTEXA72 == clib_cpu_implementer ()) &&
390       (AARCH64_CPU_PART_CORTEXA72 == clib_cpu_part ()))
391     return 10;
392   return -1;
393 }
394
395 static inline int
396 clib_cpu_march_priority_neoversen1 ()
397 {
398   if ((AARCH64_CPU_IMPLEMENTER_NEOVERSEN1 == clib_cpu_implementer ()) &&
399       (AARCH64_CPU_PART_NEOVERSEN1 == clib_cpu_part ()))
400     return 10;
401   return -1;
402 }
403
404 #ifdef CLIB_MARCH_VARIANT
405 #define CLIB_MARCH_FN_PRIORITY() CLIB_MARCH_SFX(clib_cpu_march_priority)()
406 #else
407 #define CLIB_MARCH_FN_PRIORITY() 0
408 #endif
409 #endif /* included_clib_cpu_h */
410
411 #define CLIB_MARCH_FN_CONSTRUCTOR(fn)                                   \
412 static void __clib_constructor                                          \
413 CLIB_MARCH_SFX(fn ## _march_constructor) (void)                         \
414 {                                                                       \
415   if (CLIB_MARCH_FN_PRIORITY() > fn ## _selected_priority)              \
416     {                                                                   \
417       fn ## _selected = & CLIB_MARCH_SFX (fn ## _ma);                   \
418       fn ## _selected_priority = CLIB_MARCH_FN_PRIORITY();              \
419     }                                                                   \
420 }                                                                       \
421
422 #ifndef CLIB_MARCH_VARIANT
423 #define CLIB_MARCH_FN(fn, rtype, _args...)                                    \
424   static rtype CLIB_MARCH_SFX (fn##_ma) (_args);                              \
425   rtype (*fn##_selected) (_args) = &CLIB_MARCH_SFX (fn##_ma);                 \
426   int fn##_selected_priority = 0;                                             \
427   static inline rtype CLIB_MARCH_SFX (fn##_ma) (_args)
428 #else
429 #define CLIB_MARCH_FN(fn, rtype, _args...)                                    \
430   static rtype CLIB_MARCH_SFX (fn##_ma) (_args);                              \
431   extern rtype (*fn##_selected) (_args);                                      \
432   extern int fn##_selected_priority;                                          \
433   CLIB_MARCH_FN_CONSTRUCTOR (fn)                                              \
434   static rtype CLIB_MARCH_SFX (fn##_ma) (_args)
435 #endif
436
437 #define CLIB_MARCH_FN_SELECT(fn) (* fn ## _selected)
438
439 format_function_t format_cpu_uarch;
440 format_function_t format_cpu_model_name;
441 format_function_t format_cpu_flags;
442 format_function_t format_march_variant;
443
444 /*
445  * fd.io coding-style-patch-verification: ON
446  *
447  * Local Variables:
448  * eval: (c-set-style "gnu")
449  * End:
450  */