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 _ (znver3, "AMD Milan") \
32 _ (znver4, "AMD Genoa")
33 #elif defined(__aarch64__)
34 #define foreach_march_variant \
35 _ (octeontx2, "Marvell Octeon TX2") \
36 _ (thunderx2t99, "Marvell ThunderX2 T99") \
37 _ (qdf24xx, "Qualcomm CentriqTM 2400") \
38 _ (cortexa72, "ARM Cortex-A72") \
39 _ (neoversen1, "ARM Neoverse N1")
41 #define foreach_march_variant
44 #define amd_vendor(t1, t2, t3) \
45 ((t1 == 0x68747541) && /* htuA */ \
46 (t2 == 0x444d4163) && /* DMAc */ \
47 (t3 == 0x69746e65)) /* itne */
50 CLIB_MARCH_VARIANT_TYPE = 0,
51 #define _(s, n) CLIB_MARCH_VARIANT_TYPE_##s,
54 CLIB_MARCH_TYPE_N_VARIANTS
55 } clib_march_variant_type_t;
57 #ifdef CLIB_MARCH_VARIANT
58 #define __CLIB_MULTIARCH_FN(a,b) a##_##b
59 #define _CLIB_MULTIARCH_FN(a,b) __CLIB_MULTIARCH_FN(a,b)
60 #define CLIB_MULTIARCH_FN(fn) _CLIB_MULTIARCH_FN(fn,CLIB_MARCH_VARIANT)
62 #define CLIB_MULTIARCH_FN(fn) fn
65 #define CLIB_MARCH_SFX CLIB_MULTIARCH_FN
67 typedef struct _clib_march_fn_registration
71 struct _clib_march_fn_registration *next;
73 } clib_march_fn_registration;
75 static_always_inline void *
76 clib_march_select_fn_ptr (clib_march_fn_registration * r)
83 if (last_prio < r->priority)
85 last_prio = r->priority;
93 #define CLIB_MARCH_FN_POINTER(fn) \
94 (__typeof__ (fn) *) clib_march_select_fn_ptr (fn##_march_fn_registrations);
96 #define CLIB_MARCH_FN_VOID_POINTER(fn) \
97 clib_march_select_fn_ptr (fn##_march_fn_registrations);
99 #define _CLIB_MARCH_FN_REGISTRATION(fn) \
100 static clib_march_fn_registration \
101 CLIB_MARCH_SFX(fn##_march_fn_registration) = \
103 .name = CLIB_MARCH_VARIANT_STR \
106 static void __clib_constructor \
107 fn##_march_register () \
109 clib_march_fn_registration *r; \
110 r = & CLIB_MARCH_SFX (fn##_march_fn_registration); \
111 r->priority = CLIB_MARCH_FN_PRIORITY(); \
112 r->next = fn##_march_fn_registrations; \
113 r->function = CLIB_MARCH_SFX (fn); \
114 fn##_march_fn_registrations = r; \
117 #ifdef CLIB_MARCH_VARIANT
118 #define CLIB_MARCH_FN_REGISTRATION(fn) \
119 extern clib_march_fn_registration *fn##_march_fn_registrations; \
120 _CLIB_MARCH_FN_REGISTRATION(fn)
122 #define CLIB_MARCH_FN_REGISTRATION(fn) \
123 clib_march_fn_registration *fn##_march_fn_registrations = 0; \
124 _CLIB_MARCH_FN_REGISTRATION(fn)
126 #define foreach_x86_64_flags \
127 _ (sse3, 1, ecx, 0) \
128 _ (pclmulqdq, 1, ecx, 1) \
129 _ (ssse3, 1, ecx, 9) \
130 _ (sse41, 1, ecx, 19) \
131 _ (sse42, 1, ecx, 20) \
132 _ (avx, 1, ecx, 28) \
133 _ (rdrand, 1, ecx, 30) \
134 _ (avx2, 7, ebx, 5) \
135 _ (bmi2, 7, ebx, 8) \
136 _ (rtm, 7, ebx, 11) \
137 _ (pqm, 7, ebx, 12) \
138 _ (pqe, 7, ebx, 15) \
139 _ (avx512f, 7, ebx, 16) \
140 _ (rdseed, 7, ebx, 18) \
141 _ (x86_aes, 1, ecx, 25) \
142 _ (sha, 7, ebx, 29) \
143 _ (vaes, 7, ecx, 9) \
144 _ (vpclmulqdq, 7, ecx, 10) \
145 _ (avx512_vnni, 7, ecx, 11) \
146 _ (avx512_bitalg, 7, ecx, 12) \
147 _ (avx512_vpopcntdq, 7, ecx, 14) \
148 _ (movdiri, 7, ecx, 27) \
149 _ (movdir64b, 7, ecx, 28) \
150 _ (enqcmd, 7, ecx, 29) \
151 _ (avx512_fp16, 7, edx, 23) \
152 _ (invariant_tsc, 0x80000007, edx, 8) \
153 _ (monitorx, 0x80000001, ecx, 29)
155 #define foreach_aarch64_flags \
180 u32 clib_get_current_cpu_id (void);
181 u32 clib_get_current_numa_node (void);
183 typedef int (*clib_cpu_supports_func_t) (void);
185 #if defined(__x86_64__)
189 clib_get_cpuid (const u32 lev, u32 * eax, u32 * ebx, u32 * ecx, u32 * edx)
191 if ((u32) __get_cpuid_max (0x80000000 & lev, 0) < lev)
194 __cpuid_count (lev, 0, *eax, *ebx, *ecx, *edx);
196 __cpuid (lev, *eax, *ebx, *ecx, *edx);
200 #define _(flag, func, reg, bit) \
202 clib_cpu_supports_ ## flag() \
204 u32 __attribute__((unused)) eax, ebx = 0, ecx = 0, edx = 0; \
205 clib_get_cpuid (func, &eax, &ebx, &ecx, &edx); \
207 return ((reg & (1 << bit)) != 0); \
211 #else /* __x86_64__ */
213 #define _(flag, func, reg, bit) \
214 static inline int clib_cpu_supports_ ## flag() { return 0; }
217 #endif /* __x86_64__ */
218 #if defined(__aarch64__)
219 #include <sys/auxv.h>
220 #define _(flag, bit) \
222 clib_cpu_supports_ ## flag() \
224 unsigned long hwcap = getauxval(AT_HWCAP); \
225 return (hwcap & (1 << bit)); \
227 foreach_aarch64_flags
229 #else /* ! __x86_64__ && !__aarch64__ */
230 #define _(flag, bit) \
231 static inline int clib_cpu_supports_ ## flag() { return 0; }
232 foreach_aarch64_flags
234 #endif /* __x86_64__, __aarch64__ */
236 * aes is the only feature with the same name in both flag lists
237 * handle this by prefixing it with the arch name, and handling it
238 * with the custom function below
241 clib_cpu_supports_aes ()
243 #if defined(__x86_64__)
244 return clib_cpu_supports_x86_aes ();
245 #elif defined (__aarch64__)
246 return clib_cpu_supports_aarch64_aes ();
253 clib_cpu_march_priority_scalar ()
259 clib_cpu_march_priority_spr ()
261 if (clib_cpu_supports_enqcmd ())
267 clib_cpu_march_priority_icl ()
269 if (clib_cpu_supports_avx512_bitalg ())
275 clib_cpu_march_priority_adl ()
277 if (clib_cpu_supports_movdiri () && clib_cpu_supports_avx2 ())
283 clib_cpu_march_priority_skx ()
285 if (clib_cpu_supports_avx512f ())
291 clib_cpu_march_priority_trm ()
293 if (clib_cpu_supports_movdiri ())
299 clib_cpu_march_priority_hsw ()
301 if (clib_cpu_supports_avx2 ())
307 clib_cpu_march_priority_znver4 ()
309 if (clib_cpu_supports_avx512_bitalg () && clib_cpu_supports_monitorx ())
315 clib_cpu_march_priority_znver3 ()
317 if (clib_cpu_supports_avx2 () && clib_cpu_supports_monitorx ())
322 #define X86_CPU_ARCH_PERF_FUNC 0xA
325 clib_get_pmu_counter_count (u8 *fixed, u8 *general)
327 #if defined(__x86_64__)
328 u32 __clib_unused eax = 0, ebx = 0, ecx = 0, edx = 0;
329 clib_get_cpuid (X86_CPU_ARCH_PERF_FUNC, &eax, &ebx, &ecx, &edx);
331 *general = (eax & 0xFF00) >> 8;
332 *fixed = (edx & 0xF);
341 clib_cpu_implementer ()
344 static u32 implementer = -1;
346 if (-1 != implementer)
349 FILE *fp = fopen ("/proc/cpuinfo", "r");
355 if (!fgets (buf, sizeof (buf), fp))
358 if (strstr (buf, "CPU implementer"))
359 implementer = (u32) strtol (memchr (buf, ':', 128) + 2, NULL, 0);
360 if (-1 != implementer)
372 static u32 part = -1;
377 FILE *fp = fopen ("/proc/cpuinfo", "r");
383 if (!fgets (buf, sizeof (buf), fp))
386 if (strstr (buf, "CPU part"))
387 part = (u32) strtol (memchr (buf, ':', 128) + 2, NULL, 0);
396 #define AARCH64_CPU_IMPLEMENTER_CAVIUM 0x43
397 #define AARCH64_CPU_PART_THUNDERX2 0x0af
398 #define AARCH64_CPU_PART_OCTEONTX2T96 0x0b2
399 #define AARCH64_CPU_PART_OCTEONTX2T98 0x0b1
400 #define AARCH64_CPU_IMPLEMENTER_QDF24XX 0x51
401 #define AARCH64_CPU_PART_QDF24XX 0xc00
402 #define AARCH64_CPU_IMPLEMENTER_CORTEXA72 0x41
403 #define AARCH64_CPU_PART_CORTEXA72 0xd08
404 #define AARCH64_CPU_IMPLEMENTER_NEOVERSEN1 0x41
405 #define AARCH64_CPU_PART_NEOVERSEN1 0xd0c
408 clib_cpu_march_priority_octeontx2 ()
410 if ((AARCH64_CPU_IMPLEMENTER_CAVIUM == clib_cpu_implementer ()) &&
411 ((AARCH64_CPU_PART_OCTEONTX2T96 == clib_cpu_part ())
412 || AARCH64_CPU_PART_OCTEONTX2T98 == clib_cpu_part ()))
418 clib_cpu_march_priority_thunderx2t99 ()
420 if ((AARCH64_CPU_IMPLEMENTER_CAVIUM == clib_cpu_implementer ()) &&
421 (AARCH64_CPU_PART_THUNDERX2 == clib_cpu_part ()))
427 clib_cpu_march_priority_qdf24xx ()
429 if ((AARCH64_CPU_IMPLEMENTER_QDF24XX == clib_cpu_implementer ()) &&
430 (AARCH64_CPU_PART_QDF24XX == clib_cpu_part ()))
436 clib_cpu_march_priority_cortexa72 ()
438 if ((AARCH64_CPU_IMPLEMENTER_CORTEXA72 == clib_cpu_implementer ()) &&
439 (AARCH64_CPU_PART_CORTEXA72 == clib_cpu_part ()))
445 clib_cpu_march_priority_neoversen1 ()
447 if ((AARCH64_CPU_IMPLEMENTER_NEOVERSEN1 == clib_cpu_implementer ()) &&
448 (AARCH64_CPU_PART_NEOVERSEN1 == clib_cpu_part ()))
453 #ifdef CLIB_MARCH_VARIANT
454 #define CLIB_MARCH_FN_PRIORITY() CLIB_MARCH_SFX(clib_cpu_march_priority)()
456 #define CLIB_MARCH_FN_PRIORITY() 0
458 #endif /* included_clib_cpu_h */
460 #define CLIB_MARCH_FN_CONSTRUCTOR(fn) \
461 static void __clib_constructor \
462 CLIB_MARCH_SFX(fn ## _march_constructor) (void) \
464 if (CLIB_MARCH_FN_PRIORITY() > fn ## _selected_priority) \
466 fn ## _selected = & CLIB_MARCH_SFX (fn ## _ma); \
467 fn ## _selected_priority = CLIB_MARCH_FN_PRIORITY(); \
471 #ifndef CLIB_MARCH_VARIANT
472 #define CLIB_MARCH_FN(fn, rtype, _args...) \
473 static rtype CLIB_MARCH_SFX (fn##_ma) (_args); \
474 rtype (*fn##_selected) (_args) = &CLIB_MARCH_SFX (fn##_ma); \
475 int fn##_selected_priority = 0; \
476 static inline rtype CLIB_MARCH_SFX (fn##_ma) (_args)
478 #define CLIB_MARCH_FN(fn, rtype, _args...) \
479 static rtype CLIB_MARCH_SFX (fn##_ma) (_args); \
480 extern rtype (*fn##_selected) (_args); \
481 extern int fn##_selected_priority; \
482 CLIB_MARCH_FN_CONSTRUCTOR (fn) \
483 static rtype CLIB_MARCH_SFX (fn##_ma) (_args)
486 #define CLIB_MARCH_FN_SELECT(fn) (* fn ## _selected)
488 format_function_t format_cpu_uarch;
489 format_function_t format_cpu_model_name;
490 format_function_t format_cpu_flags;
491 format_function_t format_march_variant;
494 * fd.io coding-style-patch-verification: ON
497 * eval: (c-set-style "gnu")