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