vppinfra: add AMD EPYC cpu family details
[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   _ (scalar, "Generic (SIMD disabled)")                                       \
25   _ (hsw, "Intel Haswell")                                                    \
26   _ (trm, "Intel Tremont")                                                    \
27   _ (skx, "Intel Skylake (server) / Cascade Lake")                            \
28   _ (icl, "Intel Ice Lake")                                                   \
29   _ (adl, "Intel Alder Lake")                                                 \
30   _ (spr, "Intel Sapphire Rapids")
31 #elif defined(__aarch64__)
32 #define foreach_march_variant                                                 \
33   _ (octeontx2, "Marvell Octeon TX2")                                         \
34   _ (thunderx2t99, "Marvell ThunderX2 T99")                                   \
35   _ (qdf24xx, "Qualcomm CentriqTM 2400")                                      \
36   _ (cortexa72, "ARM Cortex-A72")                                             \
37   _ (neoversen1, "ARM Neoverse N1")
38 #else
39 #define foreach_march_variant
40 #endif
41
42 #define amd_vendor(t1, t2, t3)                                                \
43   ((t1 == 0x68747541) && /* htuA */                                           \
44    (t2 == 0x444d4163) && /* DMAc */                                           \
45    (t3 == 0x69746e65))   /* itne */
46 typedef enum
47 {
48   CLIB_MARCH_VARIANT_TYPE = 0,
49 #define _(s, n) CLIB_MARCH_VARIANT_TYPE_##s,
50   foreach_march_variant
51 #undef _
52     CLIB_MARCH_TYPE_N_VARIANTS
53 } clib_march_variant_type_t;
54
55 #ifdef CLIB_MARCH_VARIANT
56 #define __CLIB_MULTIARCH_FN(a,b) a##_##b
57 #define _CLIB_MULTIARCH_FN(a,b) __CLIB_MULTIARCH_FN(a,b)
58 #define CLIB_MULTIARCH_FN(fn) _CLIB_MULTIARCH_FN(fn,CLIB_MARCH_VARIANT)
59 #else
60 #define CLIB_MULTIARCH_FN(fn) fn
61 #endif
62
63 #define CLIB_MARCH_SFX CLIB_MULTIARCH_FN
64
65 typedef struct _clib_march_fn_registration
66 {
67   void *function;
68   int priority;
69   struct _clib_march_fn_registration *next;
70   char *name;
71 } clib_march_fn_registration;
72
73 static_always_inline void *
74 clib_march_select_fn_ptr (clib_march_fn_registration * r)
75 {
76   void *rv = 0;
77   int last_prio = -1;
78
79   while (r)
80     {
81       if (last_prio < r->priority)
82         {
83           last_prio = r->priority;
84           rv = r->function;
85         }
86       r = r->next;
87     }
88   return rv;
89 }
90
91 #define CLIB_MARCH_FN_POINTER(fn)                                             \
92   (__typeof__ (fn) *) clib_march_select_fn_ptr (fn##_march_fn_registrations);
93
94 #define CLIB_MARCH_FN_VOID_POINTER(fn)                                        \
95   clib_march_select_fn_ptr (fn##_march_fn_registrations);
96
97 #define _CLIB_MARCH_FN_REGISTRATION(fn) \
98 static clib_march_fn_registration \
99 CLIB_MARCH_SFX(fn##_march_fn_registration) = \
100 { \
101   .name = CLIB_MARCH_VARIANT_STR \
102 }; \
103 \
104 static void __clib_constructor \
105 fn##_march_register () \
106 { \
107   clib_march_fn_registration *r; \
108   r = & CLIB_MARCH_SFX (fn##_march_fn_registration); \
109   r->priority = CLIB_MARCH_FN_PRIORITY(); \
110   r->next = fn##_march_fn_registrations; \
111   r->function = CLIB_MARCH_SFX (fn); \
112   fn##_march_fn_registrations = r; \
113 }
114
115 #ifdef CLIB_MARCH_VARIANT
116 #define CLIB_MARCH_FN_REGISTRATION(fn) \
117 extern clib_march_fn_registration *fn##_march_fn_registrations; \
118 _CLIB_MARCH_FN_REGISTRATION(fn)
119 #else
120 #define CLIB_MARCH_FN_REGISTRATION(fn) \
121 clib_march_fn_registration *fn##_march_fn_registrations = 0; \
122 _CLIB_MARCH_FN_REGISTRATION(fn)
123 #endif
124 #define foreach_x86_64_flags                                                  \
125   _ (sse3, 1, ecx, 0)                                                         \
126   _ (pclmulqdq, 1, ecx, 1)                                                    \
127   _ (ssse3, 1, ecx, 9)                                                        \
128   _ (sse41, 1, ecx, 19)                                                       \
129   _ (sse42, 1, ecx, 20)                                                       \
130   _ (avx, 1, ecx, 28)                                                         \
131   _ (rdrand, 1, ecx, 30)                                                      \
132   _ (avx2, 7, ebx, 5)                                                         \
133   _ (bmi2, 7, ebx, 8)                                                         \
134   _ (rtm, 7, ebx, 11)                                                         \
135   _ (pqm, 7, ebx, 12)                                                         \
136   _ (pqe, 7, ebx, 15)                                                         \
137   _ (avx512f, 7, ebx, 16)                                                     \
138   _ (rdseed, 7, ebx, 18)                                                      \
139   _ (x86_aes, 1, ecx, 25)                                                     \
140   _ (sha, 7, ebx, 29)                                                         \
141   _ (vaes, 7, ecx, 9)                                                         \
142   _ (vpclmulqdq, 7, ecx, 10)                                                  \
143   _ (avx512_vnni, 7, ecx, 11)                                                 \
144   _ (avx512_bitalg, 7, ecx, 12)                                               \
145   _ (avx512_vpopcntdq, 7, ecx, 14)                                            \
146   _ (movdiri, 7, ecx, 27)                                                     \
147   _ (movdir64b, 7, ecx, 28)                                                   \
148   _ (enqcmd, 7, ecx, 29)                                                      \
149   _ (avx512_fp16, 7, edx, 23)                                                 \
150   _ (invariant_tsc, 0x80000007, edx, 8)
151
152 #define foreach_aarch64_flags \
153 _ (fp,          0) \
154 _ (asimd,       1) \
155 _ (evtstrm,     2) \
156 _ (aarch64_aes, 3) \
157 _ (pmull,       4) \
158 _ (sha1,        5) \
159 _ (sha2,        6) \
160 _ (crc32,       7) \
161 _ (atomics,     8) \
162 _ (fphp,        9) \
163 _ (asimdhp,    10) \
164 _ (cpuid,      11) \
165 _ (asimdrdm,   12) \
166 _ (jscvt,      13) \
167 _ (fcma,       14) \
168 _ (lrcpc,      15) \
169 _ (dcpop,      16) \
170 _ (sha3,       17) \
171 _ (sm3,        18) \
172 _ (sm4,        19) \
173 _ (asimddp,    20) \
174 _ (sha512,     21) \
175 _ (sve,        22)
176
177 u32 clib_get_current_cpu_id (void);
178 u32 clib_get_current_numa_node (void);
179
180 typedef int (*clib_cpu_supports_func_t) (void);
181
182 #if defined(__x86_64__)
183 #include "cpuid.h"
184
185 static inline int
186 clib_get_cpuid (const u32 lev, u32 * eax, u32 * ebx, u32 * ecx, u32 * edx)
187 {
188   if ((u32) __get_cpuid_max (0x80000000 & lev, 0) < lev)
189     return 0;
190   if (lev == 7)
191     __cpuid_count (lev, 0, *eax, *ebx, *ecx, *edx);
192   else
193     __cpuid (lev, *eax, *ebx, *ecx, *edx);
194   return 1;
195 }
196
197 #define _(flag, func, reg, bit) \
198 static inline int                                                       \
199 clib_cpu_supports_ ## flag()                                            \
200 {                                                                       \
201   u32 __attribute__((unused)) eax, ebx = 0, ecx = 0, edx  = 0;          \
202   clib_get_cpuid (func, &eax, &ebx, &ecx, &edx);                        \
203                                                                         \
204   return ((reg & (1 << bit)) != 0);                                     \
205 }
206 foreach_x86_64_flags
207 #undef _
208 #else /* __x86_64__ */
209
210 #define _(flag, func, reg, bit) \
211 static inline int clib_cpu_supports_ ## flag() { return 0; }
212 foreach_x86_64_flags
213 #undef _
214 #endif /* __x86_64__ */
215 #if defined(__aarch64__)
216 #include <sys/auxv.h>
217 #define _(flag, bit) \
218 static inline int                                                       \
219 clib_cpu_supports_ ## flag()                                            \
220 {                                                                       \
221   unsigned long hwcap = getauxval(AT_HWCAP);                            \
222   return (hwcap & (1 << bit));                                          \
223 }
224   foreach_aarch64_flags
225 #undef _
226 #else /* ! __x86_64__ && !__aarch64__ */
227 #define _(flag, bit) \
228 static inline int clib_cpu_supports_ ## flag() { return 0; }
229   foreach_aarch64_flags
230 #undef _
231 #endif /* __x86_64__, __aarch64__ */
232 /*
233  * aes is the only feature with the same name in both flag lists
234  * handle this by prefixing it with the arch name, and handling it
235  * with the custom function below
236  */
237   static inline int
238 clib_cpu_supports_aes ()
239 {
240 #if defined(__x86_64__)
241   return clib_cpu_supports_x86_aes ();
242 #elif defined (__aarch64__)
243   return clib_cpu_supports_aarch64_aes ();
244 #else
245   return 0;
246 #endif
247 }
248
249 static inline int
250 clib_cpu_march_priority_scalar ()
251 {
252   return 1;
253 }
254
255 static inline int
256 clib_cpu_march_priority_spr ()
257 {
258   if (clib_cpu_supports_enqcmd ())
259     return 300;
260   return -1;
261 }
262
263 static inline int
264 clib_cpu_march_priority_icl ()
265 {
266   if (clib_cpu_supports_avx512_bitalg ())
267     return 200;
268   return -1;
269 }
270
271 static inline int
272 clib_cpu_march_priority_adl ()
273 {
274   if (clib_cpu_supports_movdiri () && clib_cpu_supports_avx2 ())
275     return 150;
276   return -1;
277 }
278
279 static inline int
280 clib_cpu_march_priority_skx ()
281 {
282   if (clib_cpu_supports_avx512f ())
283     return 100;
284   return -1;
285 }
286
287 static inline int
288 clib_cpu_march_priority_trm ()
289 {
290   if (clib_cpu_supports_movdiri ())
291     return 40;
292   return -1;
293 }
294
295 static inline int
296 clib_cpu_march_priority_hsw ()
297 {
298   if (clib_cpu_supports_avx2 ())
299     return 50;
300   return -1;
301 }
302
303 #define X86_CPU_ARCH_PERF_FUNC 0xA
304
305 static inline int
306 clib_get_pmu_counter_count (u8 *fixed, u8 *general)
307 {
308 #if defined(__x86_64__)
309   u32 __clib_unused eax = 0, ebx = 0, ecx = 0, edx = 0;
310   clib_get_cpuid (X86_CPU_ARCH_PERF_FUNC, &eax, &ebx, &ecx, &edx);
311
312   *general = (eax & 0xFF00) >> 8;
313   *fixed = (edx & 0xF);
314
315   return 1;
316 #else
317   return 0;
318 #endif
319 }
320
321 static inline u32
322 clib_cpu_implementer ()
323 {
324   char buf[128];
325   static u32 implementer = -1;
326
327   if (-1 != implementer)
328     return implementer;
329
330   FILE *fp = fopen ("/proc/cpuinfo", "r");
331   if (!fp)
332     return implementer;
333
334   while (!feof (fp))
335     {
336       if (!fgets (buf, sizeof (buf), fp))
337         break;
338       buf[127] = '\0';
339       if (strstr (buf, "CPU implementer"))
340         implementer = (u32) strtol (memchr (buf, ':', 128) + 2, NULL, 0);
341       if (-1 != implementer)
342         break;
343     }
344   fclose (fp);
345
346   return implementer;
347 }
348
349 static inline u32
350 clib_cpu_part ()
351 {
352   char buf[128];
353   static u32 part = -1;
354
355   if (-1 != part)
356     return part;
357
358   FILE *fp = fopen ("/proc/cpuinfo", "r");
359   if (!fp)
360     return part;
361
362   while (!feof (fp))
363     {
364       if (!fgets (buf, sizeof (buf), fp))
365         break;
366       buf[127] = '\0';
367       if (strstr (buf, "CPU part"))
368         part = (u32) strtol (memchr (buf, ':', 128) + 2, NULL, 0);
369       if (-1 != part)
370         break;
371     }
372   fclose (fp);
373
374   return part;
375 }
376
377 #define AARCH64_CPU_IMPLEMENTER_CAVIUM      0x43
378 #define AARCH64_CPU_PART_THUNDERX2          0x0af
379 #define AARCH64_CPU_PART_OCTEONTX2T96       0x0b2
380 #define AARCH64_CPU_PART_OCTEONTX2T98       0x0b1
381 #define AARCH64_CPU_IMPLEMENTER_QDF24XX     0x51
382 #define AARCH64_CPU_PART_QDF24XX            0xc00
383 #define AARCH64_CPU_IMPLEMENTER_CORTEXA72   0x41
384 #define AARCH64_CPU_PART_CORTEXA72          0xd08
385 #define AARCH64_CPU_IMPLEMENTER_NEOVERSEN1  0x41
386 #define AARCH64_CPU_PART_NEOVERSEN1         0xd0c
387
388 static inline int
389 clib_cpu_march_priority_octeontx2 ()
390 {
391   if ((AARCH64_CPU_IMPLEMENTER_CAVIUM == clib_cpu_implementer ()) &&
392       ((AARCH64_CPU_PART_OCTEONTX2T96 == clib_cpu_part ())
393        || AARCH64_CPU_PART_OCTEONTX2T98 == clib_cpu_part ()))
394     return 20;
395   return -1;
396 }
397
398 static inline int
399 clib_cpu_march_priority_thunderx2t99 ()
400 {
401   if ((AARCH64_CPU_IMPLEMENTER_CAVIUM == clib_cpu_implementer ()) &&
402       (AARCH64_CPU_PART_THUNDERX2 == clib_cpu_part ()))
403     return 20;
404   return -1;
405 }
406
407 static inline int
408 clib_cpu_march_priority_qdf24xx ()
409 {
410   if ((AARCH64_CPU_IMPLEMENTER_QDF24XX == clib_cpu_implementer ()) &&
411       (AARCH64_CPU_PART_QDF24XX == clib_cpu_part ()))
412     return 20;
413   return -1;
414 }
415
416 static inline int
417 clib_cpu_march_priority_cortexa72 ()
418 {
419   if ((AARCH64_CPU_IMPLEMENTER_CORTEXA72 == clib_cpu_implementer ()) &&
420       (AARCH64_CPU_PART_CORTEXA72 == clib_cpu_part ()))
421     return 10;
422   return -1;
423 }
424
425 static inline int
426 clib_cpu_march_priority_neoversen1 ()
427 {
428   if ((AARCH64_CPU_IMPLEMENTER_NEOVERSEN1 == clib_cpu_implementer ()) &&
429       (AARCH64_CPU_PART_NEOVERSEN1 == clib_cpu_part ()))
430     return 10;
431   return -1;
432 }
433
434 #ifdef CLIB_MARCH_VARIANT
435 #define CLIB_MARCH_FN_PRIORITY() CLIB_MARCH_SFX(clib_cpu_march_priority)()
436 #else
437 #define CLIB_MARCH_FN_PRIORITY() 0
438 #endif
439 #endif /* included_clib_cpu_h */
440
441 #define CLIB_MARCH_FN_CONSTRUCTOR(fn)                                   \
442 static void __clib_constructor                                          \
443 CLIB_MARCH_SFX(fn ## _march_constructor) (void)                         \
444 {                                                                       \
445   if (CLIB_MARCH_FN_PRIORITY() > fn ## _selected_priority)              \
446     {                                                                   \
447       fn ## _selected = & CLIB_MARCH_SFX (fn ## _ma);                   \
448       fn ## _selected_priority = CLIB_MARCH_FN_PRIORITY();              \
449     }                                                                   \
450 }                                                                       \
451
452 #ifndef CLIB_MARCH_VARIANT
453 #define CLIB_MARCH_FN(fn, rtype, _args...)                                    \
454   static rtype CLIB_MARCH_SFX (fn##_ma) (_args);                              \
455   rtype (*fn##_selected) (_args) = &CLIB_MARCH_SFX (fn##_ma);                 \
456   int fn##_selected_priority = 0;                                             \
457   static inline rtype CLIB_MARCH_SFX (fn##_ma) (_args)
458 #else
459 #define CLIB_MARCH_FN(fn, rtype, _args...)                                    \
460   static rtype CLIB_MARCH_SFX (fn##_ma) (_args);                              \
461   extern rtype (*fn##_selected) (_args);                                      \
462   extern int fn##_selected_priority;                                          \
463   CLIB_MARCH_FN_CONSTRUCTOR (fn)                                              \
464   static rtype CLIB_MARCH_SFX (fn##_ma) (_args)
465 #endif
466
467 #define CLIB_MARCH_FN_SELECT(fn) (* fn ## _selected)
468
469 format_function_t format_cpu_uarch;
470 format_function_t format_cpu_model_name;
471 format_function_t format_cpu_flags;
472 format_function_t format_march_variant;
473
474 /*
475  * fd.io coding-style-patch-verification: ON
476  *
477  * Local Variables:
478  * eval: (c-set-style "gnu")
479  * End:
480  */