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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef included_clib_cpu_h
17 #define included_clib_cpu_h
19 #include <sys/syscall.h>
20 #include <vppinfra/format.h>
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")
39 #define foreach_march_variant
42 #define amd_vendor(t1, t2, t3) \
43 ((t1 == 0x68747541) && /* htuA */ \
44 (t2 == 0x444d4163) && /* DMAc */ \
45 (t3 == 0x69746e65)) /* itne */
48 CLIB_MARCH_VARIANT_TYPE = 0,
49 #define _(s, n) CLIB_MARCH_VARIANT_TYPE_##s,
52 CLIB_MARCH_TYPE_N_VARIANTS
53 } clib_march_variant_type_t;
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)
60 #define CLIB_MULTIARCH_FN(fn) fn
63 #define CLIB_MARCH_SFX CLIB_MULTIARCH_FN
65 typedef struct _clib_march_fn_registration
69 struct _clib_march_fn_registration *next;
71 } clib_march_fn_registration;
73 static_always_inline void *
74 clib_march_select_fn_ptr (clib_march_fn_registration * r)
81 if (last_prio < r->priority)
83 last_prio = r->priority;
91 #define CLIB_MARCH_FN_POINTER(fn) \
92 (__typeof__ (fn) *) clib_march_select_fn_ptr (fn##_march_fn_registrations);
94 #define CLIB_MARCH_FN_VOID_POINTER(fn) \
95 clib_march_select_fn_ptr (fn##_march_fn_registrations);
97 #define _CLIB_MARCH_FN_REGISTRATION(fn) \
98 static clib_march_fn_registration \
99 CLIB_MARCH_SFX(fn##_march_fn_registration) = \
101 .name = CLIB_MARCH_VARIANT_STR \
104 static void __clib_constructor \
105 fn##_march_register () \
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; \
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)
120 #define CLIB_MARCH_FN_REGISTRATION(fn) \
121 clib_march_fn_registration *fn##_march_fn_registrations = 0; \
122 _CLIB_MARCH_FN_REGISTRATION(fn)
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)
152 #define foreach_aarch64_flags \
177 u32 clib_get_current_cpu_id (void);
178 u32 clib_get_current_numa_node (void);
180 typedef int (*clib_cpu_supports_func_t) (void);
182 #if defined(__x86_64__)
186 clib_get_cpuid (const u32 lev, u32 * eax, u32 * ebx, u32 * ecx, u32 * edx)
188 if ((u32) __get_cpuid_max (0x80000000 & lev, 0) < lev)
191 __cpuid_count (lev, 0, *eax, *ebx, *ecx, *edx);
193 __cpuid (lev, *eax, *ebx, *ecx, *edx);
197 #define _(flag, func, reg, bit) \
199 clib_cpu_supports_ ## flag() \
201 u32 __attribute__((unused)) eax, ebx = 0, ecx = 0, edx = 0; \
202 clib_get_cpuid (func, &eax, &ebx, &ecx, &edx); \
204 return ((reg & (1 << bit)) != 0); \
208 #else /* __x86_64__ */
210 #define _(flag, func, reg, bit) \
211 static inline int clib_cpu_supports_ ## flag() { return 0; }
214 #endif /* __x86_64__ */
215 #if defined(__aarch64__)
216 #include <sys/auxv.h>
217 #define _(flag, bit) \
219 clib_cpu_supports_ ## flag() \
221 unsigned long hwcap = getauxval(AT_HWCAP); \
222 return (hwcap & (1 << bit)); \
224 foreach_aarch64_flags
226 #else /* ! __x86_64__ && !__aarch64__ */
227 #define _(flag, bit) \
228 static inline int clib_cpu_supports_ ## flag() { return 0; }
229 foreach_aarch64_flags
231 #endif /* __x86_64__, __aarch64__ */
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
238 clib_cpu_supports_aes ()
240 #if defined(__x86_64__)
241 return clib_cpu_supports_x86_aes ();
242 #elif defined (__aarch64__)
243 return clib_cpu_supports_aarch64_aes ();
250 clib_cpu_march_priority_scalar ()
256 clib_cpu_march_priority_spr ()
258 if (clib_cpu_supports_enqcmd ())
264 clib_cpu_march_priority_icl ()
266 if (clib_cpu_supports_avx512_bitalg ())
272 clib_cpu_march_priority_adl ()
274 if (clib_cpu_supports_movdiri () && clib_cpu_supports_avx2 ())
280 clib_cpu_march_priority_skx ()
282 if (clib_cpu_supports_avx512f ())
288 clib_cpu_march_priority_trm ()
290 if (clib_cpu_supports_movdiri ())
296 clib_cpu_march_priority_hsw ()
298 if (clib_cpu_supports_avx2 ())
303 #define X86_CPU_ARCH_PERF_FUNC 0xA
306 clib_get_pmu_counter_count (u8 *fixed, u8 *general)
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);
312 *general = (eax & 0xFF00) >> 8;
313 *fixed = (edx & 0xF);
322 clib_cpu_implementer ()
325 static u32 implementer = -1;
327 if (-1 != implementer)
330 FILE *fp = fopen ("/proc/cpuinfo", "r");
336 if (!fgets (buf, sizeof (buf), fp))
339 if (strstr (buf, "CPU implementer"))
340 implementer = (u32) strtol (memchr (buf, ':', 128) + 2, NULL, 0);
341 if (-1 != implementer)
353 static u32 part = -1;
358 FILE *fp = fopen ("/proc/cpuinfo", "r");
364 if (!fgets (buf, sizeof (buf), fp))
367 if (strstr (buf, "CPU part"))
368 part = (u32) strtol (memchr (buf, ':', 128) + 2, NULL, 0);
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
389 clib_cpu_march_priority_octeontx2 ()
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 ()))
399 clib_cpu_march_priority_thunderx2t99 ()
401 if ((AARCH64_CPU_IMPLEMENTER_CAVIUM == clib_cpu_implementer ()) &&
402 (AARCH64_CPU_PART_THUNDERX2 == clib_cpu_part ()))
408 clib_cpu_march_priority_qdf24xx ()
410 if ((AARCH64_CPU_IMPLEMENTER_QDF24XX == clib_cpu_implementer ()) &&
411 (AARCH64_CPU_PART_QDF24XX == clib_cpu_part ()))
417 clib_cpu_march_priority_cortexa72 ()
419 if ((AARCH64_CPU_IMPLEMENTER_CORTEXA72 == clib_cpu_implementer ()) &&
420 (AARCH64_CPU_PART_CORTEXA72 == clib_cpu_part ()))
426 clib_cpu_march_priority_neoversen1 ()
428 if ((AARCH64_CPU_IMPLEMENTER_NEOVERSEN1 == clib_cpu_implementer ()) &&
429 (AARCH64_CPU_PART_NEOVERSEN1 == clib_cpu_part ()))
434 #ifdef CLIB_MARCH_VARIANT
435 #define CLIB_MARCH_FN_PRIORITY() CLIB_MARCH_SFX(clib_cpu_march_priority)()
437 #define CLIB_MARCH_FN_PRIORITY() 0
439 #endif /* included_clib_cpu_h */
441 #define CLIB_MARCH_FN_CONSTRUCTOR(fn) \
442 static void __clib_constructor \
443 CLIB_MARCH_SFX(fn ## _march_constructor) (void) \
445 if (CLIB_MARCH_FN_PRIORITY() > fn ## _selected_priority) \
447 fn ## _selected = & CLIB_MARCH_SFX (fn ## _ma); \
448 fn ## _selected_priority = CLIB_MARCH_FN_PRIORITY(); \
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)
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)
467 #define CLIB_MARCH_FN_SELECT(fn) (* fn ## _selected)
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;
475 * fd.io coding-style-patch-verification: ON
478 * eval: (c-set-style "gnu")