host stack: update stale copyright
[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 /* FIXME to be removed */
49 #define CLIB_MULTIARCH_SELECT_FN(fn,...)
50
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)
55 #else
56 #define CLIB_MULTIARCH_FN(fn) fn
57 #endif
58
59 #define CLIB_MARCH_SFX CLIB_MULTIARCH_FN
60
61 typedef struct _clib_march_fn_registration
62 {
63   void *function;
64   int priority;
65   struct _clib_march_fn_registration *next;
66   char *name;
67 } clib_march_fn_registration;
68
69 static_always_inline void *
70 clib_march_select_fn_ptr (clib_march_fn_registration * r)
71 {
72   void *rv = 0;
73   int last_prio = -1;
74
75   while (r)
76     {
77       if (last_prio < r->priority)
78         {
79           last_prio = r->priority;
80           rv = r->function;
81         }
82       r = r->next;
83     }
84   return rv;
85 }
86
87 #define CLIB_MARCH_FN_POINTER(fn) \
88   clib_march_select_fn_ptr (fn##_march_fn_registrations);
89
90 #define _CLIB_MARCH_FN_REGISTRATION(fn) \
91 static clib_march_fn_registration \
92 CLIB_MARCH_SFX(fn##_march_fn_registration) = \
93 { \
94   .name = CLIB_MARCH_VARIANT_STR \
95 }; \
96 \
97 static void __clib_constructor \
98 fn##_march_register () \
99 { \
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; \
106 }
107
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)
112 #else
113 #define CLIB_MARCH_FN_REGISTRATION(fn) \
114 clib_march_fn_registration *fn##_march_fn_registrations = 0; \
115 _CLIB_MARCH_FN_REGISTRATION(fn)
116 #endif
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 _ (rdrand,   1, ecx, 30)  \
124 _ (avx2,     7, ebx, 5)   \
125 _ (rtm,      7, ebx, 11)  \
126 _ (pqm,      7, ebx, 12)  \
127 _ (pqe,      7, ebx, 15)  \
128 _ (avx512f,  7, ebx, 16)  \
129 _ (rdseed,   7, ebx, 18)  \
130 _ (x86_aes,  1, ecx, 25)  \
131 _ (sha,      7, ebx, 29)  \
132 _ (vaes,     7, ecx, 9)   \
133 _ (vpclmulqdq, 7, ecx, 10)   \
134 _ (invariant_tsc, 0x80000007, edx, 8)
135
136
137 #define foreach_aarch64_flags \
138 _ (fp,          0) \
139 _ (asimd,       1) \
140 _ (evtstrm,     2) \
141 _ (aarch64_aes, 3) \
142 _ (pmull,       4) \
143 _ (sha1,        5) \
144 _ (sha2,        6) \
145 _ (crc32,       7) \
146 _ (atomics,     8) \
147 _ (fphp,        9) \
148 _ (asimdhp,    10) \
149 _ (cpuid,      11) \
150 _ (asimdrdm,   12) \
151 _ (jscvt,      13) \
152 _ (fcma,       14) \
153 _ (lrcpc,      15) \
154 _ (dcpop,      16) \
155 _ (sha3,       17) \
156 _ (sm3,        18) \
157 _ (sm4,        19) \
158 _ (asimddp,    20) \
159 _ (sha512,     21) \
160 _ (sve,        22)
161
162 static inline u32
163 clib_get_current_cpu_id ()
164 {
165   unsigned cpu, node;
166   syscall (__NR_getcpu, &cpu, &node, 0);
167   return cpu;
168 }
169
170 static inline u32
171 clib_get_current_numa_node ()
172 {
173   unsigned cpu, node;
174   syscall (__NR_getcpu, &cpu, &node, 0);
175   return node;
176 }
177
178 #if defined(__x86_64__)
179 #include "cpuid.h"
180
181 static inline int
182 clib_get_cpuid (const u32 lev, u32 * eax, u32 * ebx, u32 * ecx, u32 * edx)
183 {
184   if ((u32) __get_cpuid_max (0x80000000 & lev, 0) < lev)
185     return 0;
186   if (lev == 7)
187     __cpuid_count (lev, 0, *eax, *ebx, *ecx, *edx);
188   else
189     __cpuid (lev, *eax, *ebx, *ecx, *edx);
190   return 1;
191 }
192
193
194 #define _(flag, func, reg, bit) \
195 static inline int                                                       \
196 clib_cpu_supports_ ## flag()                                            \
197 {                                                                       \
198   u32 __attribute__((unused)) eax, ebx = 0, ecx = 0, edx  = 0;          \
199   clib_get_cpuid (func, &eax, &ebx, &ecx, &edx);                        \
200                                                                         \
201   return ((reg & (1 << bit)) != 0);                                     \
202 }
203 foreach_x86_64_flags
204 #undef _
205 #else /* __x86_64__ */
206
207 #define _(flag, func, reg, bit) \
208 static inline int clib_cpu_supports_ ## flag() { return 0; }
209 foreach_x86_64_flags
210 #undef _
211 #endif /* __x86_64__ */
212 #if defined(__aarch64__)
213 #include <sys/auxv.h>
214 #define _(flag, bit) \
215 static inline int                                                       \
216 clib_cpu_supports_ ## flag()                                            \
217 {                                                                       \
218   unsigned long hwcap = getauxval(AT_HWCAP);                            \
219   return (hwcap & (1 << bit));                                          \
220 }
221   foreach_aarch64_flags
222 #undef _
223 #else /* ! __x86_64__ && !__aarch64__ */
224 #define _(flag, bit) \
225 static inline int clib_cpu_supports_ ## flag() { return 0; }
226   foreach_aarch64_flags
227 #undef _
228 #endif /* __x86_64__, __aarch64__ */
229 /*
230  * aes is the only feature with the same name in both flag lists
231  * handle this by prefixing it with the arch name, and handling it
232  * with the custom function below
233  */
234   static inline int
235 clib_cpu_supports_aes ()
236 {
237 #if defined (__aarch64__)
238   return clib_cpu_supports_x86_aes ();
239 #elif defined (__aarch64__)
240   return clib_cpu_supports_aarch64_aes ();
241 #else
242   return 0;
243 #endif
244 }
245
246 static inline int
247 clib_cpu_march_priority_avx512 ()
248 {
249   if (clib_cpu_supports_avx512f ())
250     return 20;
251   return -1;
252 }
253
254 static inline int
255 clib_cpu_march_priority_avx2 ()
256 {
257   if (clib_cpu_supports_avx2 ())
258     return 50;
259   return -1;
260 }
261
262 static inline u32
263 clib_cpu_implementer ()
264 {
265   char buf[128];
266   static u32 implementer = -1;
267
268   if (-1 != implementer)
269     return implementer;
270
271   FILE *fp = fopen ("/proc/cpuinfo", "r");
272   if (!fp)
273     return implementer;
274
275   while (!feof (fp))
276     {
277       if (!fgets (buf, sizeof (buf), fp))
278         break;
279       buf[127] = '\0';
280       if (strstr (buf, "CPU implementer"))
281         implementer = (u32) strtol (memchr (buf, ':', 128) + 2, NULL, 0);
282       if (-1 != implementer)
283         break;
284     }
285   fclose (fp);
286
287   return implementer;
288 }
289
290 static inline u32
291 clib_cpu_part ()
292 {
293   char buf[128];
294   static u32 part = -1;
295
296   if (-1 != part)
297     return part;
298
299   FILE *fp = fopen ("/proc/cpuinfo", "r");
300   if (!fp)
301     return part;
302
303   while (!feof (fp))
304     {
305       if (!fgets (buf, sizeof (buf), fp))
306         break;
307       buf[127] = '\0';
308       if (strstr (buf, "CPU part"))
309         part = (u32) strtol (memchr (buf, ':', 128) + 2, NULL, 0);
310       if (-1 != part)
311         break;
312     }
313   fclose (fp);
314
315   return part;
316 }
317
318 #define AARCH64_CPU_IMPLEMENTER_THUNERDERX2 0x43
319 #define AARCH64_CPU_PART_THUNERDERX2        0x0af
320 #define AARCH64_CPU_IMPLEMENTER_QDF24XX     0x51
321 #define AARCH64_CPU_PART_QDF24XX            0xc00
322 #define AARCH64_CPU_IMPLEMENTER_CORTEXA72   0x41
323 #define AARCH64_CPU_PART_CORTEXA72          0xd08
324
325 static inline int
326 clib_cpu_march_priority_thunderx2t99 ()
327 {
328   if ((AARCH64_CPU_IMPLEMENTER_THUNERDERX2 == clib_cpu_implementer ()) &&
329       (AARCH64_CPU_PART_THUNERDERX2 == clib_cpu_part ()))
330     return 20;
331   return -1;
332 }
333
334 static inline int
335 clib_cpu_march_priority_qdf24xx ()
336 {
337   if ((AARCH64_CPU_IMPLEMENTER_QDF24XX == clib_cpu_implementer ()) &&
338       (AARCH64_CPU_PART_QDF24XX == clib_cpu_part ()))
339     return 20;
340   return -1;
341 }
342
343 static inline int
344 clib_cpu_march_priority_cortexa72 ()
345 {
346   if ((AARCH64_CPU_IMPLEMENTER_CORTEXA72 == clib_cpu_implementer ()) &&
347       (AARCH64_CPU_PART_CORTEXA72 == clib_cpu_part ()))
348     return 10;
349   return -1;
350 }
351
352 #ifdef CLIB_MARCH_VARIANT
353 #define CLIB_MARCH_FN_PRIORITY() CLIB_MARCH_SFX(clib_cpu_march_priority)()
354 #else
355 #define CLIB_MARCH_FN_PRIORITY() 0
356 #endif
357 #endif /* included_clib_cpu_h */
358
359 #define CLIB_MARCH_FN_CONSTRUCTOR(fn)                                   \
360 static void __clib_constructor                                          \
361 CLIB_MARCH_SFX(fn ## _march_constructor) (void)                         \
362 {                                                                       \
363   if (CLIB_MARCH_FN_PRIORITY() > fn ## _selected_priority)              \
364     {                                                                   \
365       fn ## _selected = & CLIB_MARCH_SFX (fn ## _ma);                   \
366       fn ## _selected_priority = CLIB_MARCH_FN_PRIORITY();              \
367     }                                                                   \
368 }                                                                       \
369
370 #ifndef CLIB_MARCH_VARIANT
371 #define CLIB_MARCH_FN(fn, rtype, _args...)                              \
372   static rtype CLIB_CPU_OPTIMIZED CLIB_MARCH_SFX (fn ## _ma)(_args);    \
373   rtype (*fn ## _selected) (_args) = & CLIB_MARCH_SFX (fn ## _ma);      \
374   int fn ## _selected_priority = 0;                                     \
375   static inline rtype CLIB_CPU_OPTIMIZED                                \
376   CLIB_MARCH_SFX (fn ## _ma)(_args)
377 #else
378 #define CLIB_MARCH_FN(fn, rtype, _args...)                              \
379   static rtype CLIB_CPU_OPTIMIZED CLIB_MARCH_SFX (fn ## _ma)(_args);    \
380   extern rtype (*fn ## _selected) (_args);                              \
381   extern int fn ## _selected_priority;                                  \
382   CLIB_MARCH_FN_CONSTRUCTOR (fn)                                        \
383   static rtype CLIB_CPU_OPTIMIZED CLIB_MARCH_SFX (fn ## _ma)(_args)
384 #endif
385
386 #define CLIB_MARCH_FN_SELECT(fn) (* fn ## _selected)
387
388 format_function_t format_cpu_uarch;
389 format_function_t format_cpu_model_name;
390 format_function_t format_cpu_flags;
391
392 /*
393  * fd.io coding-style-patch-verification: ON
394  *
395  * Local Variables:
396  * eval: (c-set-style "gnu")
397  * End:
398  */