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 <vppinfra/format.h>
22 * multiarchitecture support. Adding new entry will produce
23 * new graph node function variant optimized for specific cpu
25 * Order is important for runtime selection, as 1st match wins...
28 #if __x86_64__ && CLIB_DEBUG == 0
29 #define foreach_march_variant(macro, x) \
30 macro(avx2, x, "arch=core-avx2")
32 #define foreach_march_variant(macro, x)
36 #if __GNUC__ > 4 && !__clang__
37 #define CLIB_CPU_OPTIMIZED __attribute__ ((optimize ("O3")))
39 #define CLIB_CPU_OPTIMIZED
43 #define CLIB_MULTIARCH_ARCH_CHECK(arch, fn, tgt) \
44 if (clib_cpu_supports_ ## arch()) \
45 return & fn ## _ ##arch;
47 #define CLIB_MULTIARCH_SELECT_FN(fn,...) \
48 __VA_ARGS__ void * fn ## _multiarch_select(void) \
50 foreach_march_variant(CLIB_MULTIARCH_ARCH_CHECK, fn) \
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)
59 #define CLIB_MULTIARCH_FN(fn) fn
62 #define CLIB_MARCH_SFX CLIB_MULTIARCH_FN
64 #define foreach_x86_64_flags \
66 _ (ssse3, 1, ecx, 9) \
67 _ (sse41, 1, ecx, 19) \
68 _ (sse42, 1, ecx, 20) \
71 _ (avx512f, 7, ebx, 16) \
72 _ (x86_aes, 1, ecx, 25) \
74 _ (invariant_tsc, 0x80000007, edx, 8)
77 #define foreach_aarch64_flags \
102 #if defined(__x86_64__)
106 clib_get_cpuid (const u32 lev, u32 * eax, u32 * ebx, u32 * ecx, u32 * edx)
108 if ((u32) __get_cpuid_max (0x80000000 & lev, 0) < lev)
111 __cpuid_count (lev, 0, *eax, *ebx, *ecx, *edx);
113 __cpuid (lev, *eax, *ebx, *ecx, *edx);
118 #define _(flag, func, reg, bit) \
120 clib_cpu_supports_ ## flag() \
122 u32 __attribute__((unused)) eax, ebx = 0, ecx = 0, edx = 0; \
123 clib_get_cpuid (func, &eax, &ebx, &ecx, &edx); \
125 return ((reg & (1 << bit)) != 0); \
129 #else /* __x86_64__ */
131 #define _(flag, func, reg, bit) \
132 static inline int clib_cpu_supports_ ## flag() { return 0; }
135 #endif /* __x86_64__ */
136 #if defined(__aarch64__)
137 #include <sys/auxv.h>
138 #define _(flag, bit) \
140 clib_cpu_supports_ ## flag() \
142 unsigned long hwcap = getauxval(AT_HWCAP); \
143 return (hwcap & (1 << bit)); \
145 foreach_aarch64_flags
147 #else /* ! __x86_64__ && !__aarch64__ */
148 #define _(flag, bit) \
149 static inline int clib_cpu_supports_ ## flag() { return 0; }
150 foreach_aarch64_flags
152 #endif /* __x86_64__, __aarch64__ */
154 * aes is the only feature with the same name in both flag lists
155 * handle this by prefixing it with the arch name, and handling it
156 * with the custom function below
159 clib_cpu_supports_aes ()
161 #if defined (__aarch64__)
162 return clib_cpu_supports_x86_aes ();
163 #elif defined (__aarch64__)
164 return clib_cpu_supports_aarch64_aes ();
171 clib_cpu_march_priority_avx512 ()
173 if (clib_cpu_supports_avx512f ())
179 clib_cpu_march_priority_avx2 ()
181 if (clib_cpu_supports_avx2 ())
187 clib_cpu_implementer ()
190 static u32 implementer = -1;
192 if (-1 != implementer)
195 FILE *fp = fopen ("/proc/cpuinfo", "r");
201 if (!fgets (buf, sizeof (buf), fp))
204 if (strstr (buf, "CPU implementer"))
205 implementer = (u32) strtol (memchr (buf, ':', 128) + 2, NULL, 0);
206 if (-1 != implementer)
218 static u32 part = -1;
223 FILE *fp = fopen ("/proc/cpuinfo", "r");
229 if (!fgets (buf, sizeof (buf), fp))
232 if (strstr (buf, "CPU part"))
233 part = (u32) strtol (memchr (buf, ':', 128) + 2, NULL, 0);
242 #define AARCH64_CPU_IMPLEMENTER_THUNERDERX2 0x43
243 #define AARCH64_CPU_PART_THUNERDERX2 0x0af
244 #define AARCH64_CPU_IMPLEMENTER_QDF24XX 0x51
245 #define AARCH64_CPU_PART_QDF24XX 0xc00
246 #define AARCH64_CPU_IMPLEMENTER_CORTEXA72 0x41
247 #define AARCH64_CPU_PART_CORTEXA72 0xd08
250 clib_cpu_march_priority_thunderx2t99 ()
252 if ((AARCH64_CPU_IMPLEMENTER_THUNERDERX2 == clib_cpu_implementer ()) &&
253 (AARCH64_CPU_PART_THUNERDERX2 == clib_cpu_part ()))
259 clib_cpu_march_priority_qdf24xx ()
261 if ((AARCH64_CPU_IMPLEMENTER_QDF24XX == clib_cpu_implementer ()) &&
262 (AARCH64_CPU_PART_QDF24XX == clib_cpu_part ()))
268 clib_cpu_march_priority_cortexa72 ()
270 if ((AARCH64_CPU_IMPLEMENTER_CORTEXA72 == clib_cpu_implementer ()) &&
271 (AARCH64_CPU_PART_CORTEXA72 == clib_cpu_part ()))
276 #ifdef CLIB_MARCH_VARIANT
277 #define CLIB_MARCH_FN_PRIORITY() CLIB_MARCH_SFX(clib_cpu_march_priority)()
279 #define CLIB_MARCH_FN_PRIORITY() 0
281 #endif /* included_clib_cpu_h */
283 #define CLIB_MARCH_FN_CONSTRUCTOR(fn) \
284 static void __clib_constructor \
285 CLIB_MARCH_SFX(fn ## _march_constructor) (void) \
287 if (CLIB_MARCH_FN_PRIORITY() > fn ## _selected_priority) \
289 fn ## _selected = & CLIB_MARCH_SFX (fn ## _ma); \
290 fn ## _selected_priority = CLIB_MARCH_FN_PRIORITY(); \
294 #ifndef CLIB_MARCH_VARIANT
295 #define CLIB_MARCH_FN(fn, rtype, _args...) \
296 static rtype CLIB_CPU_OPTIMIZED CLIB_MARCH_SFX (fn ## _ma)(_args); \
297 rtype (*fn ## _selected) (_args) = & CLIB_MARCH_SFX (fn ## _ma); \
298 int fn ## _selected_priority = 0; \
299 static inline rtype CLIB_CPU_OPTIMIZED \
300 CLIB_MARCH_SFX (fn ## _ma)(_args)
302 #define CLIB_MARCH_FN(fn, rtype, _args...) \
303 static rtype CLIB_CPU_OPTIMIZED CLIB_MARCH_SFX (fn ## _ma)(_args); \
304 extern int (*fn ## _selected) (_args); \
305 extern int fn ## _selected_priority; \
306 CLIB_MARCH_FN_CONSTRUCTOR (fn) \
307 static rtype CLIB_CPU_OPTIMIZED CLIB_MARCH_SFX (fn ## _ma)(_args)
310 #define CLIB_MARCH_FN_SELECT(fn) (* fn ## _selected)
312 format_function_t format_cpu_uarch;
313 format_function_t format_cpu_model_name;
314 format_function_t format_cpu_flags;
317 * fd.io coding-style-patch-verification: ON
320 * eval: (c-set-style "gnu")