interface: Add interface monitor cli
[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_REGISTRATION(fn) \
88 static clib_march_fn_registration \
89 CLIB_MARCH_SFX(fn##_march_fn_registration) = \
90 { \
91   .name = CLIB_MARCH_VARIANT_STR \
92 }; \
93 \
94 static void __clib_constructor \
95 fn##_march_register () \
96 { \
97   clib_march_fn_registration *r; \
98   r = & CLIB_MARCH_SFX (fn##_march_fn_registration); \
99   r->priority = CLIB_MARCH_FN_PRIORITY(); \
100   r->next = fn##_march_fn_registrations; \
101   r->function = CLIB_MARCH_SFX (fn); \
102   fn##_march_fn_registrations = r; \
103 }
104
105 #ifdef CLIB_MARCH_VARIANT
106 #define CLIB_MARCH_FN_REGISTRATION(fn) \
107 extern clib_march_fn_registration *fn##_march_fn_registrations; \
108 _CLIB_MARCH_FN_REGISTRATION(fn)
109 #else
110 #define CLIB_MARCH_FN_REGISTRATION(fn) \
111 clib_march_fn_registration *fn##_march_fn_registrations = 0; \
112 _CLIB_MARCH_FN_REGISTRATION(fn)
113 #endif
114 #define foreach_x86_64_flags                                                  \
115   _ (sse3, 1, ecx, 0)                                                         \
116   _ (pclmulqdq, 1, ecx, 1)                                                    \
117   _ (ssse3, 1, ecx, 9)                                                        \
118   _ (sse41, 1, ecx, 19)                                                       \
119   _ (sse42, 1, ecx, 20)                                                       \
120   _ (avx, 1, ecx, 28)                                                         \
121   _ (rdrand, 1, ecx, 30)                                                      \
122   _ (avx2, 7, ebx, 5)                                                         \
123   _ (rtm, 7, ebx, 11)                                                         \
124   _ (pqm, 7, ebx, 12)                                                         \
125   _ (pqe, 7, ebx, 15)                                                         \
126   _ (avx512f, 7, ebx, 16)                                                     \
127   _ (rdseed, 7, ebx, 18)                                                      \
128   _ (x86_aes, 1, ecx, 25)                                                     \
129   _ (sha, 7, ebx, 29)                                                         \
130   _ (vaes, 7, ecx, 9)                                                         \
131   _ (vpclmulqdq, 7, ecx, 10)                                                  \
132   _ (avx512_vnni, 7, ecx, 11)                                                 \
133   _ (avx512_bitalg, 7, ecx, 12)                                               \
134   _ (avx512_vpopcntdq, 7, ecx, 14)                                            \
135   _ (movdiri, 7, ecx, 27)                                                     \
136   _ (movdir64b, 7, ecx, 28)                                                   \
137   _ (avx512_fp16, 7, edx, 23)                                                 \
138   _ (invariant_tsc, 0x80000007, edx, 8)
139
140 #define foreach_aarch64_flags \
141 _ (fp,          0) \
142 _ (asimd,       1) \
143 _ (evtstrm,     2) \
144 _ (aarch64_aes, 3) \
145 _ (pmull,       4) \
146 _ (sha1,        5) \
147 _ (sha2,        6) \
148 _ (crc32,       7) \
149 _ (atomics,     8) \
150 _ (fphp,        9) \
151 _ (asimdhp,    10) \
152 _ (cpuid,      11) \
153 _ (asimdrdm,   12) \
154 _ (jscvt,      13) \
155 _ (fcma,       14) \
156 _ (lrcpc,      15) \
157 _ (dcpop,      16) \
158 _ (sha3,       17) \
159 _ (sm3,        18) \
160 _ (sm4,        19) \
161 _ (asimddp,    20) \
162 _ (sha512,     21) \
163 _ (sve,        22)
164
165 u32 clib_get_current_cpu_id ();
166 u32 clib_get_current_numa_node ();
167
168 #if defined(__x86_64__)
169 #include "cpuid.h"
170
171 static inline int
172 clib_get_cpuid (const u32 lev, u32 * eax, u32 * ebx, u32 * ecx, u32 * edx)
173 {
174   if ((u32) __get_cpuid_max (0x80000000 & lev, 0) < lev)
175     return 0;
176   if (lev == 7)
177     __cpuid_count (lev, 0, *eax, *ebx, *ecx, *edx);
178   else
179     __cpuid (lev, *eax, *ebx, *ecx, *edx);
180   return 1;
181 }
182
183 typedef int (*clib_cpu_supports_func_t) ();
184
185 #define _(flag, func, reg, bit) \
186 static inline int                                                       \
187 clib_cpu_supports_ ## flag()                                            \
188 {                                                                       \
189   u32 __attribute__((unused)) eax, ebx = 0, ecx = 0, edx  = 0;          \
190   clib_get_cpuid (func, &eax, &ebx, &ecx, &edx);                        \
191                                                                         \
192   return ((reg & (1 << bit)) != 0);                                     \
193 }
194 foreach_x86_64_flags
195 #undef _
196 #else /* __x86_64__ */
197
198 #define _(flag, func, reg, bit) \
199 static inline int clib_cpu_supports_ ## flag() { return 0; }
200 foreach_x86_64_flags
201 #undef _
202 #endif /* __x86_64__ */
203 #if defined(__aarch64__)
204 #include <sys/auxv.h>
205 #define _(flag, bit) \
206 static inline int                                                       \
207 clib_cpu_supports_ ## flag()                                            \
208 {                                                                       \
209   unsigned long hwcap = getauxval(AT_HWCAP);                            \
210   return (hwcap & (1 << bit));                                          \
211 }
212   foreach_aarch64_flags
213 #undef _
214 #else /* ! __x86_64__ && !__aarch64__ */
215 #define _(flag, bit) \
216 static inline int clib_cpu_supports_ ## flag() { return 0; }
217   foreach_aarch64_flags
218 #undef _
219 #endif /* __x86_64__, __aarch64__ */
220 /*
221  * aes is the only feature with the same name in both flag lists
222  * handle this by prefixing it with the arch name, and handling it
223  * with the custom function below
224  */
225   static inline int
226 clib_cpu_supports_aes ()
227 {
228 #if defined(__x86_64__)
229   return clib_cpu_supports_x86_aes ();
230 #elif defined (__aarch64__)
231   return clib_cpu_supports_aarch64_aes ();
232 #else
233   return 0;
234 #endif
235 }
236
237 static inline int
238 clib_cpu_march_priority_icl ()
239 {
240   if (clib_cpu_supports_avx512_bitalg ())
241     return 200;
242   return -1;
243 }
244
245 static inline int
246 clib_cpu_march_priority_skx ()
247 {
248   if (clib_cpu_supports_avx512f ())
249     return 100;
250   return -1;
251 }
252
253 static inline int
254 clib_cpu_march_priority_trm ()
255 {
256   if (clib_cpu_supports_movdiri ())
257     return 60;
258   return -1;
259 }
260
261 static inline int
262 clib_cpu_march_priority_hsw ()
263 {
264   if (clib_cpu_supports_avx2 ())
265     return 50;
266   return -1;
267 }
268
269 static inline u32
270 clib_cpu_implementer ()
271 {
272   char buf[128];
273   static u32 implementer = -1;
274
275   if (-1 != implementer)
276     return implementer;
277
278   FILE *fp = fopen ("/proc/cpuinfo", "r");
279   if (!fp)
280     return implementer;
281
282   while (!feof (fp))
283     {
284       if (!fgets (buf, sizeof (buf), fp))
285         break;
286       buf[127] = '\0';
287       if (strstr (buf, "CPU implementer"))
288         implementer = (u32) strtol (memchr (buf, ':', 128) + 2, NULL, 0);
289       if (-1 != implementer)
290         break;
291     }
292   fclose (fp);
293
294   return implementer;
295 }
296
297 static inline u32
298 clib_cpu_part ()
299 {
300   char buf[128];
301   static u32 part = -1;
302
303   if (-1 != part)
304     return part;
305
306   FILE *fp = fopen ("/proc/cpuinfo", "r");
307   if (!fp)
308     return part;
309
310   while (!feof (fp))
311     {
312       if (!fgets (buf, sizeof (buf), fp))
313         break;
314       buf[127] = '\0';
315       if (strstr (buf, "CPU part"))
316         part = (u32) strtol (memchr (buf, ':', 128) + 2, NULL, 0);
317       if (-1 != part)
318         break;
319     }
320   fclose (fp);
321
322   return part;
323 }
324
325 #define AARCH64_CPU_IMPLEMENTER_CAVIUM      0x43
326 #define AARCH64_CPU_PART_THUNDERX2          0x0af
327 #define AARCH64_CPU_PART_OCTEONTX2T96       0x0b2
328 #define AARCH64_CPU_PART_OCTEONTX2T98       0x0b1
329 #define AARCH64_CPU_IMPLEMENTER_QDF24XX     0x51
330 #define AARCH64_CPU_PART_QDF24XX            0xc00
331 #define AARCH64_CPU_IMPLEMENTER_CORTEXA72   0x41
332 #define AARCH64_CPU_PART_CORTEXA72          0xd08
333 #define AARCH64_CPU_IMPLEMENTER_NEOVERSEN1  0x41
334 #define AARCH64_CPU_PART_NEOVERSEN1         0xd0c
335
336 static inline int
337 clib_cpu_march_priority_octeontx2 ()
338 {
339   if ((AARCH64_CPU_IMPLEMENTER_CAVIUM == clib_cpu_implementer ()) &&
340       ((AARCH64_CPU_PART_OCTEONTX2T96 == clib_cpu_part ())
341        || AARCH64_CPU_PART_OCTEONTX2T98 == clib_cpu_part ()))
342     return 20;
343   return -1;
344 }
345
346 static inline int
347 clib_cpu_march_priority_thunderx2t99 ()
348 {
349   if ((AARCH64_CPU_IMPLEMENTER_CAVIUM == clib_cpu_implementer ()) &&
350       (AARCH64_CPU_PART_THUNDERX2 == clib_cpu_part ()))
351     return 20;
352   return -1;
353 }
354
355 static inline int
356 clib_cpu_march_priority_qdf24xx ()
357 {
358   if ((AARCH64_CPU_IMPLEMENTER_QDF24XX == clib_cpu_implementer ()) &&
359       (AARCH64_CPU_PART_QDF24XX == clib_cpu_part ()))
360     return 20;
361   return -1;
362 }
363
364 static inline int
365 clib_cpu_march_priority_cortexa72 ()
366 {
367   if ((AARCH64_CPU_IMPLEMENTER_CORTEXA72 == clib_cpu_implementer ()) &&
368       (AARCH64_CPU_PART_CORTEXA72 == clib_cpu_part ()))
369     return 10;
370   return -1;
371 }
372
373 static inline int
374 clib_cpu_march_priority_neoversen1 ()
375 {
376   if ((AARCH64_CPU_IMPLEMENTER_NEOVERSEN1 == clib_cpu_implementer ()) &&
377       (AARCH64_CPU_PART_NEOVERSEN1 == clib_cpu_part ()))
378     return 10;
379   return -1;
380 }
381
382 #ifdef CLIB_MARCH_VARIANT
383 #define CLIB_MARCH_FN_PRIORITY() CLIB_MARCH_SFX(clib_cpu_march_priority)()
384 #else
385 #define CLIB_MARCH_FN_PRIORITY() 0
386 #endif
387 #endif /* included_clib_cpu_h */
388
389 #define CLIB_MARCH_FN_CONSTRUCTOR(fn)                                   \
390 static void __clib_constructor                                          \
391 CLIB_MARCH_SFX(fn ## _march_constructor) (void)                         \
392 {                                                                       \
393   if (CLIB_MARCH_FN_PRIORITY() > fn ## _selected_priority)              \
394     {                                                                   \
395       fn ## _selected = & CLIB_MARCH_SFX (fn ## _ma);                   \
396       fn ## _selected_priority = CLIB_MARCH_FN_PRIORITY();              \
397     }                                                                   \
398 }                                                                       \
399
400 #ifndef CLIB_MARCH_VARIANT
401 #define CLIB_MARCH_FN(fn, rtype, _args...)                                    \
402   static rtype CLIB_MARCH_SFX (fn##_ma) (_args);                              \
403   rtype (*fn##_selected) (_args) = &CLIB_MARCH_SFX (fn##_ma);                 \
404   int fn##_selected_priority = 0;                                             \
405   static inline rtype CLIB_MARCH_SFX (fn##_ma) (_args)
406 #else
407 #define CLIB_MARCH_FN(fn, rtype, _args...)                                    \
408   static rtype CLIB_MARCH_SFX (fn##_ma) (_args);                              \
409   extern rtype (*fn##_selected) (_args);                                      \
410   extern int fn##_selected_priority;                                          \
411   CLIB_MARCH_FN_CONSTRUCTOR (fn)                                              \
412   static rtype CLIB_MARCH_SFX (fn##_ma) (_args)
413 #endif
414
415 #define CLIB_MARCH_FN_SELECT(fn) (* fn ## _selected)
416
417 format_function_t format_cpu_uarch;
418 format_function_t format_cpu_model_name;
419 format_function_t format_cpu_flags;
420 format_function_t format_march_variant;
421
422 /*
423  * fd.io coding-style-patch-verification: ON
424  *
425  * Local Variables:
426  * eval: (c-set-style "gnu")
427  * End:
428  */