FD.io VPP  v18.04-17-g3a0d853
Vector Packet Processing
cpu.h
Go to the documentation of this file.
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 <vppinfra/format.h>
20 
21 /*
22  * multiarchitecture support. Adding new entry will produce
23  * new graph node function variant optimized for specific cpu
24  * microarchitecture.
25  * Order is important for runtime selection, as 1st match wins...
26  */
27 
28 #if __x86_64__ && CLIB_DEBUG == 0
29 #define foreach_march_variant(macro, x) \
30  macro(avx2, x, "arch=core-avx2")
31 #else
32 #define foreach_march_variant(macro, x)
33 #endif
34 
35 
36 #if __GNUC__ > 4 && !__clang__
37 #define CLIB_CPU_OPTIMIZED __attribute__ ((optimize ("tree-vectorize")))
38 #else
39 #define CLIB_CPU_OPTIMIZED
40 #endif
41 
42 
43 #define CLIB_MULTIARCH_ARCH_CHECK(arch, fn, tgt) \
44  if (clib_cpu_supports_ ## arch()) \
45  return & fn ## _ ##arch;
46 
47 #define CLIB_MULTIARCH_SELECT_FN(fn,...) \
48  __VA_ARGS__ void * fn ## _multiarch_select(void) \
49 { \
50  foreach_march_variant(CLIB_MULTIARCH_ARCH_CHECK, fn) \
51  return & fn; \
52 }
53 
54 #ifdef CLIB_MULTIARCH_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_MULTIARCH_VARIANT)
58 #else
59 #define CLIB_MULTIARCH_FN(fn) fn
60 #endif
61 
62 #define foreach_x86_64_flags \
63 _ (sse3, 1, ecx, 0) \
64 _ (ssse3, 1, ecx, 9) \
65 _ (sse41, 1, ecx, 19) \
66 _ (sse42, 1, ecx, 20) \
67 _ (avx, 1, ecx, 28) \
68 _ (avx2, 7, ebx, 5) \
69 _ (avx512f, 7, ebx, 16) \
70 _ (x86_aes, 1, ecx, 25) \
71 _ (sha, 7, ebx, 29) \
72 _ (invariant_tsc, 0x80000007, edx, 8)
73 
74 
75 #define foreach_aarch64_flags \
76 _ (fp, 0) \
77 _ (asimd, 1) \
78 _ (evtstrm, 2) \
79 _ (aarch64_aes, 3) \
80 _ (pmull, 4) \
81 _ (sha1, 5) \
82 _ (sha2, 6) \
83 _ (crc32, 7) \
84 _ (atomics, 8) \
85 _ (fphp, 9) \
86 _ (asimdhp, 10) \
87 _ (cpuid, 11) \
88 _ (asimdrdm, 12) \
89 _ (jscvt, 13) \
90 _ (fcma, 14) \
91 _ (lrcpc, 15) \
92 _ (dcpop, 16) \
93 _ (sha3, 17) \
94 _ (sm3, 18) \
95 _ (sm4, 19) \
96 _ (asimddp, 20) \
97 _ (sha512, 21) \
98 _ (sve, 22)
99 
100 #if defined(__x86_64__)
101 #include "cpuid.h"
102 
103 static inline int
104 clib_get_cpuid (const u32 lev, u32 * eax, u32 * ebx, u32 * ecx, u32 * edx)
105 {
106  if ((u32) __get_cpuid_max (0x80000000 & lev, 0) < lev)
107  return 0;
108  if (lev == 7)
109  __cpuid_count (lev, 0, *eax, *ebx, *ecx, *edx);
110  else
111  __cpuid (lev, *eax, *ebx, *ecx, *edx);
112  return 1;
113 }
114 
115 
116 #define _(flag, func, reg, bit) \
117 static inline int \
118 clib_cpu_supports_ ## flag() \
119 { \
120  u32 __attribute__((unused)) eax, ebx = 0, ecx = 0, edx = 0; \
121  clib_get_cpuid (func, &eax, &ebx, &ecx, &edx); \
122  \
123  return ((reg & (1 << bit)) != 0); \
124 }
126 #undef _
127 #else /* __x86_64__ */
128 
129 #define _(flag, func, reg, bit) \
130 static inline int clib_cpu_supports_ ## flag() { return 0; }
132 #undef _
133 #endif /* __x86_64__ */
134 #if defined(__aarch64__)
135 #include <sys/auxv.h>
136 #define _(flag, bit) \
137 static inline int \
138 clib_cpu_supports_ ## flag() \
139 { \
140  unsigned long hwcap = getauxval(AT_HWCAP); \
141  return (hwcap & (1 << bit)); \
142 }
144 #undef _
145 #else /* ! __x86_64__ && !__aarch64__ */
146 #define _(flag, bit) \
147 static inline int clib_cpu_supports_ ## flag() { return 0; }
149 #undef _
150 #endif /* __x86_64__, __aarch64__ */
151 /*
152  * aes is the only feature with the same name in both flag lists
153  * handle this by prefixing it with the arch name, and handling it
154  * with the custom function below
155  */
156  static inline int
158 {
159 #if defined (__aarch64__)
160  return clib_cpu_supports_x86_aes ();
161 #elif defined (__aarch64__)
162  return clib_cpu_supports_aarch64_aes ();
163 #else
164  return 0;
165 #endif
166 }
167 
168 #endif /* included_clib_cpu_h */
169 
173 
174 /*
175  * fd.io coding-style-patch-verification: ON
176  *
177  * Local Variables:
178  * eval: (c-set-style "gnu")
179  * End:
180  */
format_function_t format_cpu_flags
Definition: cpu.h:172
u8 *( format_function_t)(u8 *s, va_list *args)
Definition: format.h:48
#define foreach_aarch64_flags
Definition: cpu.h:75
static int clib_get_cpuid(const u32 lev, u32 *eax, u32 *ebx, u32 *ecx, u32 *edx)
Definition: cpu.h:104
#define foreach_x86_64_flags
Definition: cpu.h:62
unsigned int u32
Definition: types.h:88
static foreach_aarch64_flags int clib_cpu_supports_aes()
Definition: cpu.h:157
format_function_t format_cpu_uarch
Definition: cpu.h:170
format_function_t format_cpu_model_name
Definition: cpu.h:171