8341eaf0e54099fa291e877aad03e12ef7b5d422
[vpp.git] / src / vppinfra / cpu.h
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 <sys/syscall.h>
20 #include <vppinfra/format.h>
21
22 /*
23  * multiarchitecture support. Adding new entry will produce
24  * new graph node function variant optimized for specific cpu
25  * microarchitecture.
26  * Order is important for runtime selection, as 1st match wins...
27  */
28
29 #if __x86_64__ && CLIB_DEBUG == 0
30 #define foreach_march_variant(macro, x) \
31   macro(avx2,  x, "arch=core-avx2")
32 #else
33 #define foreach_march_variant(macro, x)
34 #endif
35
36
37 #if __GNUC__ > 4  && !__clang__ && CLIB_DEBUG == 0
38 #define CLIB_CPU_OPTIMIZED __attribute__ ((optimize ("O3")))
39 #else
40 #define CLIB_CPU_OPTIMIZED
41 #endif
42
43
44 #define CLIB_MULTIARCH_ARCH_CHECK(arch, fn, tgt)                        \
45   if (clib_cpu_supports_ ## arch())                                     \
46     return & fn ## _ ##arch;
47
48 #define CLIB_MULTIARCH_SELECT_FN(fn,...)                               \
49   __VA_ARGS__ void * fn ## _multiarch_select(void)                     \
50 {                                                                      \
51   foreach_march_variant(CLIB_MULTIARCH_ARCH_CHECK, fn)                 \
52   return & fn;                                                         \
53 }
54
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)
59 #else
60 #define CLIB_MULTIARCH_FN(fn) fn
61 #endif
62
63 #define CLIB_MARCH_SFX CLIB_MULTIARCH_FN
64
65 #define foreach_x86_64_flags \
66 _ (sse3,     1, ecx, 0)   \
67 _ (ssse3,    1, ecx, 9)   \
68 _ (sse41,    1, ecx, 19)  \
69 _ (sse42,    1, ecx, 20)  \
70 _ (avx,      1, ecx, 28)  \
71 _ (avx2,     7, ebx, 5)   \
72 _ (avx512f,  7, ebx, 16)  \
73 _ (x86_aes,  1, ecx, 25)  \
74 _ (sha,      7, ebx, 29)  \
75 _ (invariant_tsc, 0x80000007, edx, 8)
76
77
78 #define foreach_aarch64_flags \
79 _ (fp,          0) \
80 _ (asimd,       1) \
81 _ (evtstrm,     2) \
82 _ (aarch64_aes, 3) \
83 _ (pmull,       4) \
84 _ (sha1,        5) \
85 _ (sha2,        6) \
86 _ (crc32,       7) \
87 _ (atomics,     8) \
88 _ (fphp,        9) \
89 _ (asimdhp,    10) \
90 _ (cpuid,      11) \
91 _ (asimdrdm,   12) \
92 _ (jscvt,      13) \
93 _ (fcma,       14) \
94 _ (lrcpc,      15) \
95 _ (dcpop,      16) \
96 _ (sha3,       17) \
97 _ (sm3,        18) \
98 _ (sm4,        19) \
99 _ (asimddp,    20) \
100 _ (sha512,     21) \
101 _ (sve,        22)
102
103 static inline u32
104 clib_get_current_cpu_index ()
105 {
106   unsigned cpu, node;
107   syscall (__NR_getcpu, &cpu, &node, 0);
108   return cpu;
109 }
110
111 static inline u32
112 clib_get_current_numa_node ()
113 {
114   unsigned cpu, node;
115   syscall (__NR_getcpu, &cpu, &node, 0);
116   return node;
117 }
118
119 #if defined(__x86_64__)
120 #include "cpuid.h"
121
122 static inline int
123 clib_get_cpuid (const u32 lev, u32 * eax, u32 * ebx, u32 * ecx, u32 * edx)
124 {
125   if ((u32) __get_cpuid_max (0x80000000 & lev, 0) < lev)
126     return 0;
127   if (lev == 7)
128     __cpuid_count (lev, 0, *eax, *ebx, *ecx, *edx);
129   else
130     __cpuid (lev, *eax, *ebx, *ecx, *edx);
131   return 1;
132 }
133
134
135 #define _(flag, func, reg, bit) \
136 static inline int                                                       \
137 clib_cpu_supports_ ## flag()                                            \
138 {                                                                       \
139   u32 __attribute__((unused)) eax, ebx = 0, ecx = 0, edx  = 0;          \
140   clib_get_cpuid (func, &eax, &ebx, &ecx, &edx);                        \
141                                                                         \
142   return ((reg & (1 << bit)) != 0);                                     \
143 }
144 foreach_x86_64_flags
145 #undef _
146 #else /* __x86_64__ */
147
148 #define _(flag, func, reg, bit) \
149 static inline int clib_cpu_supports_ ## flag() { return 0; }
150 foreach_x86_64_flags
151 #undef _
152 #endif /* __x86_64__ */
153 #if defined(__aarch64__)
154 #include <sys/auxv.h>
155 #define _(flag, bit) \
156 static inline int                                                       \
157 clib_cpu_supports_ ## flag()                                            \
158 {                                                                       \
159   unsigned long hwcap = getauxval(AT_HWCAP);                            \
160   return (hwcap & (1 << bit));                                          \
161 }
162   foreach_aarch64_flags
163 #undef _
164 #else /* ! __x86_64__ && !__aarch64__ */
165 #define _(flag, bit) \
166 static inline int clib_cpu_supports_ ## flag() { return 0; }
167   foreach_aarch64_flags
168 #undef _
169 #endif /* __x86_64__, __aarch64__ */
170 /*
171  * aes is the only feature with the same name in both flag lists
172  * handle this by prefixing it with the arch name, and handling it
173  * with the custom function below
174  */
175   static inline int
176 clib_cpu_supports_aes ()
177 {
178 #if defined (__aarch64__)
179   return clib_cpu_supports_x86_aes ();
180 #elif defined (__aarch64__)
181   return clib_cpu_supports_aarch64_aes ();
182 #else
183   return 0;
184 #endif
185 }
186
187 static inline int
188 clib_cpu_march_priority_avx512 ()
189 {
190   if (clib_cpu_supports_avx512f ())
191     return 20;
192   return -1;
193 }
194
195 static inline int
196 clib_cpu_march_priority_avx2 ()
197 {
198   if (clib_cpu_supports_avx2 ())
199     return 10;
200   return -1;
201 }
202
203 static inline u32
204 clib_cpu_implementer ()
205 {
206   char buf[128];
207   static u32 implementer = -1;
208
209   if (-1 != implementer)
210     return implementer;
211
212   FILE *fp = fopen ("/proc/cpuinfo", "r");
213   if (!fp)
214     return implementer;
215
216   while (!feof (fp))
217     {
218       if (!fgets (buf, sizeof (buf), fp))
219         break;
220       buf[127] = '\0';
221       if (strstr (buf, "CPU implementer"))
222         implementer = (u32) strtol (memchr (buf, ':', 128) + 2, NULL, 0);
223       if (-1 != implementer)
224         break;
225     }
226   fclose (fp);
227
228   return implementer;
229 }
230
231 static inline u32
232 clib_cpu_part ()
233 {
234   char buf[128];
235   static u32 part = -1;
236
237   if (-1 != part)
238     return part;
239
240   FILE *fp = fopen ("/proc/cpuinfo", "r");
241   if (!fp)
242     return part;
243
244   while (!feof (fp))
245     {
246       if (!fgets (buf, sizeof (buf), fp))
247         break;
248       buf[127] = '\0';
249       if (strstr (buf, "CPU part"))
250         part = (u32) strtol (memchr (buf, ':', 128) + 2, NULL, 0);
251       if (-1 != part)
252         break;
253     }
254   fclose (fp);
255
256   return part;
257 }
258
259 #define AARCH64_CPU_IMPLEMENTER_THUNERDERX2 0x43
260 #define AARCH64_CPU_PART_THUNERDERX2        0x0af
261 #define AARCH64_CPU_IMPLEMENTER_QDF24XX     0x51
262 #define AARCH64_CPU_PART_QDF24XX            0xc00
263 #define AARCH64_CPU_IMPLEMENTER_CORTEXA72   0x41
264 #define AARCH64_CPU_PART_CORTEXA72          0xd08
265
266 static inline int
267 clib_cpu_march_priority_thunderx2t99 ()
268 {
269   if ((AARCH64_CPU_IMPLEMENTER_THUNERDERX2 == clib_cpu_implementer ()) &&
270       (AARCH64_CPU_PART_THUNERDERX2 == clib_cpu_part ()))
271     return 20;
272   return -1;
273 }
274
275 static inline int
276 clib_cpu_march_priority_qdf24xx ()
277 {
278   if ((AARCH64_CPU_IMPLEMENTER_QDF24XX == clib_cpu_implementer ()) &&
279       (AARCH64_CPU_PART_QDF24XX == clib_cpu_part ()))
280     return 20;
281   return -1;
282 }
283
284 static inline int
285 clib_cpu_march_priority_cortexa72 ()
286 {
287   if ((AARCH64_CPU_IMPLEMENTER_CORTEXA72 == clib_cpu_implementer ()) &&
288       (AARCH64_CPU_PART_CORTEXA72 == clib_cpu_part ()))
289     return 10;
290   return -1;
291 }
292
293 #ifdef CLIB_MARCH_VARIANT
294 #define CLIB_MARCH_FN_PRIORITY() CLIB_MARCH_SFX(clib_cpu_march_priority)()
295 #else
296 #define CLIB_MARCH_FN_PRIORITY() 0
297 #endif
298 #endif /* included_clib_cpu_h */
299
300 #define CLIB_MARCH_FN_CONSTRUCTOR(fn)                                   \
301 static void __clib_constructor                                          \
302 CLIB_MARCH_SFX(fn ## _march_constructor) (void)                         \
303 {                                                                       \
304   if (CLIB_MARCH_FN_PRIORITY() > fn ## _selected_priority)              \
305     {                                                                   \
306       fn ## _selected = & CLIB_MARCH_SFX (fn ## _ma);                   \
307       fn ## _selected_priority = CLIB_MARCH_FN_PRIORITY();              \
308     }                                                                   \
309 }                                                                       \
310
311 #ifndef CLIB_MARCH_VARIANT
312 #define CLIB_MARCH_FN(fn, rtype, _args...)                              \
313   static rtype CLIB_CPU_OPTIMIZED CLIB_MARCH_SFX (fn ## _ma)(_args);    \
314   rtype (*fn ## _selected) (_args) = & CLIB_MARCH_SFX (fn ## _ma);      \
315   int fn ## _selected_priority = 0;                                     \
316   static inline rtype CLIB_CPU_OPTIMIZED                                \
317   CLIB_MARCH_SFX (fn ## _ma)(_args)
318 #else
319 #define CLIB_MARCH_FN(fn, rtype, _args...)                              \
320   static rtype CLIB_CPU_OPTIMIZED CLIB_MARCH_SFX (fn ## _ma)(_args);    \
321   extern int (*fn ## _selected) (_args);                                \
322   extern int fn ## _selected_priority;                                  \
323   CLIB_MARCH_FN_CONSTRUCTOR (fn)                                        \
324   static rtype CLIB_CPU_OPTIMIZED CLIB_MARCH_SFX (fn ## _ma)(_args)
325 #endif
326
327 #define CLIB_MARCH_FN_SELECT(fn) (* fn ## _selected)
328
329 format_function_t format_cpu_uarch;
330 format_function_t format_cpu_model_name;
331 format_function_t format_cpu_flags;
332
333 /*
334  * fd.io coding-style-patch-verification: ON
335  *
336  * Local Variables:
337  * eval: (c-set-style "gnu")
338  * End:
339  */