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>
23 * multiarchitecture support. Adding new entry will produce
24 * new graph node function variant optimized for specific cpu
26 * Order is important for runtime selection, as 1st match wins...
29 #if __x86_64__ && CLIB_DEBUG == 0
30 #define foreach_march_variant(macro, x) \
31 macro(avx2, x, "arch=core-avx2")
33 #define foreach_march_variant(macro, x)
37 #if __GNUC__ > 4 && !__clang__ && CLIB_DEBUG == 0
38 #define CLIB_CPU_OPTIMIZED __attribute__ ((optimize ("O3")))
40 #define CLIB_CPU_OPTIMIZED
44 #define CLIB_MULTIARCH_ARCH_CHECK(arch, fn, tgt) \
45 if (clib_cpu_supports_ ## arch()) \
46 return & fn ## _ ##arch;
48 /* FIXME to be removed */
49 #define CLIB_MULTIARCH_SELECT_FN(fn,...)
51 #ifdef CLIB_MARCH_VARIANT
52 #define __CLIB_MULTIARCH_FN(a,b) a##_##b
53 #define _CLIB_MULTIARCH_FN(a,b) __CLIB_MULTIARCH_FN(a,b)
54 #define CLIB_MULTIARCH_FN(fn) _CLIB_MULTIARCH_FN(fn,CLIB_MARCH_VARIANT)
56 #define CLIB_MULTIARCH_FN(fn) fn
59 #define CLIB_MARCH_SFX CLIB_MULTIARCH_FN
61 typedef struct _clib_march_fn_registration
65 struct _clib_march_fn_registration *next;
67 } clib_march_fn_registration;
69 static_always_inline void *
70 clib_march_select_fn_ptr (clib_march_fn_registration * r)
77 if (last_prio < r->priority)
79 last_prio = r->priority;
87 #define CLIB_MARCH_FN_POINTER(fn) \
88 clib_march_select_fn_ptr (fn##_march_fn_registrations);
90 #define _CLIB_MARCH_FN_REGISTRATION(fn) \
91 static clib_march_fn_registration \
92 CLIB_MARCH_SFX(fn##_march_fn_registration) = \
94 .name = CLIB_MARCH_VARIANT_STR \
97 static void __clib_constructor \
98 fn##_march_register () \
100 clib_march_fn_registration *r; \
101 r = & CLIB_MARCH_SFX (fn##_march_fn_registration); \
102 r->priority = CLIB_MARCH_FN_PRIORITY(); \
103 r->next = fn##_march_fn_registrations; \
104 r->function = CLIB_MARCH_SFX (fn); \
105 fn##_march_fn_registrations = r; \
108 #ifdef CLIB_MARCH_VARIANT
109 #define CLIB_MARCH_FN_REGISTRATION(fn) \
110 extern clib_march_fn_registration *fn##_march_fn_registrations; \
111 _CLIB_MARCH_FN_REGISTRATION(fn)
113 #define CLIB_MARCH_FN_REGISTRATION(fn) \
114 clib_march_fn_registration *fn##_march_fn_registrations = 0; \
115 _CLIB_MARCH_FN_REGISTRATION(fn)
117 #define foreach_x86_64_flags \
118 _ (sse3, 1, ecx, 0) \
119 _ (ssse3, 1, ecx, 9) \
120 _ (sse41, 1, ecx, 19) \
121 _ (sse42, 1, ecx, 20) \
122 _ (avx, 1, ecx, 28) \
123 _ (avx2, 7, ebx, 5) \
124 _ (avx512f, 7, ebx, 16) \
125 _ (x86_aes, 1, ecx, 25) \
126 _ (sha, 7, ebx, 29) \
127 _ (invariant_tsc, 0x80000007, edx, 8)
130 #define foreach_aarch64_flags \
156 clib_get_current_cpu_id ()
159 syscall (__NR_getcpu, &cpu, &node, 0);
164 clib_get_current_numa_node ()
167 syscall (__NR_getcpu, &cpu, &node, 0);
171 #if defined(__x86_64__)
175 clib_get_cpuid (const u32 lev, u32 * eax, u32 * ebx, u32 * ecx, u32 * edx)
177 if ((u32) __get_cpuid_max (0x80000000 & lev, 0) < lev)
180 __cpuid_count (lev, 0, *eax, *ebx, *ecx, *edx);
182 __cpuid (lev, *eax, *ebx, *ecx, *edx);
187 #define _(flag, func, reg, bit) \
189 clib_cpu_supports_ ## flag() \
191 u32 __attribute__((unused)) eax, ebx = 0, ecx = 0, edx = 0; \
192 clib_get_cpuid (func, &eax, &ebx, &ecx, &edx); \
194 return ((reg & (1 << bit)) != 0); \
198 #else /* __x86_64__ */
200 #define _(flag, func, reg, bit) \
201 static inline int clib_cpu_supports_ ## flag() { return 0; }
204 #endif /* __x86_64__ */
205 #if defined(__aarch64__)
206 #include <sys/auxv.h>
207 #define _(flag, bit) \
209 clib_cpu_supports_ ## flag() \
211 unsigned long hwcap = getauxval(AT_HWCAP); \
212 return (hwcap & (1 << bit)); \
214 foreach_aarch64_flags
216 #else /* ! __x86_64__ && !__aarch64__ */
217 #define _(flag, bit) \
218 static inline int clib_cpu_supports_ ## flag() { return 0; }
219 foreach_aarch64_flags
221 #endif /* __x86_64__, __aarch64__ */
223 * aes is the only feature with the same name in both flag lists
224 * handle this by prefixing it with the arch name, and handling it
225 * with the custom function below
228 clib_cpu_supports_aes ()
230 #if defined (__aarch64__)
231 return clib_cpu_supports_x86_aes ();
232 #elif defined (__aarch64__)
233 return clib_cpu_supports_aarch64_aes ();
240 clib_cpu_march_priority_avx512 ()
242 if (clib_cpu_supports_avx512f ())
248 clib_cpu_march_priority_avx2 ()
250 if (clib_cpu_supports_avx2 ())
256 clib_cpu_implementer ()
259 static u32 implementer = -1;
261 if (-1 != implementer)
264 FILE *fp = fopen ("/proc/cpuinfo", "r");
270 if (!fgets (buf, sizeof (buf), fp))
273 if (strstr (buf, "CPU implementer"))
274 implementer = (u32) strtol (memchr (buf, ':', 128) + 2, NULL, 0);
275 if (-1 != implementer)
287 static u32 part = -1;
292 FILE *fp = fopen ("/proc/cpuinfo", "r");
298 if (!fgets (buf, sizeof (buf), fp))
301 if (strstr (buf, "CPU part"))
302 part = (u32) strtol (memchr (buf, ':', 128) + 2, NULL, 0);
311 #define AARCH64_CPU_IMPLEMENTER_THUNERDERX2 0x43
312 #define AARCH64_CPU_PART_THUNERDERX2 0x0af
313 #define AARCH64_CPU_IMPLEMENTER_QDF24XX 0x51
314 #define AARCH64_CPU_PART_QDF24XX 0xc00
315 #define AARCH64_CPU_IMPLEMENTER_CORTEXA72 0x41
316 #define AARCH64_CPU_PART_CORTEXA72 0xd08
319 clib_cpu_march_priority_thunderx2t99 ()
321 if ((AARCH64_CPU_IMPLEMENTER_THUNERDERX2 == clib_cpu_implementer ()) &&
322 (AARCH64_CPU_PART_THUNERDERX2 == clib_cpu_part ()))
328 clib_cpu_march_priority_qdf24xx ()
330 if ((AARCH64_CPU_IMPLEMENTER_QDF24XX == clib_cpu_implementer ()) &&
331 (AARCH64_CPU_PART_QDF24XX == clib_cpu_part ()))
337 clib_cpu_march_priority_cortexa72 ()
339 if ((AARCH64_CPU_IMPLEMENTER_CORTEXA72 == clib_cpu_implementer ()) &&
340 (AARCH64_CPU_PART_CORTEXA72 == clib_cpu_part ()))
345 #ifdef CLIB_MARCH_VARIANT
346 #define CLIB_MARCH_FN_PRIORITY() CLIB_MARCH_SFX(clib_cpu_march_priority)()
348 #define CLIB_MARCH_FN_PRIORITY() 0
350 #endif /* included_clib_cpu_h */
352 #define CLIB_MARCH_FN_CONSTRUCTOR(fn) \
353 static void __clib_constructor \
354 CLIB_MARCH_SFX(fn ## _march_constructor) (void) \
356 if (CLIB_MARCH_FN_PRIORITY() > fn ## _selected_priority) \
358 fn ## _selected = & CLIB_MARCH_SFX (fn ## _ma); \
359 fn ## _selected_priority = CLIB_MARCH_FN_PRIORITY(); \
363 #ifndef CLIB_MARCH_VARIANT
364 #define CLIB_MARCH_FN(fn, rtype, _args...) \
365 static rtype CLIB_CPU_OPTIMIZED CLIB_MARCH_SFX (fn ## _ma)(_args); \
366 rtype (*fn ## _selected) (_args) = & CLIB_MARCH_SFX (fn ## _ma); \
367 int fn ## _selected_priority = 0; \
368 static inline rtype CLIB_CPU_OPTIMIZED \
369 CLIB_MARCH_SFX (fn ## _ma)(_args)
371 #define CLIB_MARCH_FN(fn, rtype, _args...) \
372 static rtype CLIB_CPU_OPTIMIZED CLIB_MARCH_SFX (fn ## _ma)(_args); \
373 extern int (*fn ## _selected) (_args); \
374 extern int fn ## _selected_priority; \
375 CLIB_MARCH_FN_CONSTRUCTOR (fn) \
376 static rtype CLIB_CPU_OPTIMIZED CLIB_MARCH_SFX (fn ## _ma)(_args)
379 #define CLIB_MARCH_FN_SELECT(fn) (* fn ## _selected)
381 format_function_t format_cpu_uarch;
382 format_function_t format_cpu_model_name;
383 format_function_t format_cpu_flags;
386 * fd.io coding-style-patch-verification: ON
389 * eval: (c-set-style "gnu")