FD.io VPP  v20.09-64-g4f7b92f0a
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 <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;
68 
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 _ (pclmulqdq, 1, ecx, 1) \
120 _ (ssse3, 1, ecx, 9) \
121 _ (sse41, 1, ecx, 19) \
122 _ (sse42, 1, ecx, 20) \
123 _ (avx, 1, ecx, 28) \
124 _ (rdrand, 1, ecx, 30) \
125 _ (avx2, 7, ebx, 5) \
126 _ (rtm, 7, ebx, 11) \
127 _ (pqm, 7, ebx, 12) \
128 _ (pqe, 7, ebx, 15) \
129 _ (avx512f, 7, ebx, 16) \
130 _ (rdseed, 7, ebx, 18) \
131 _ (x86_aes, 1, ecx, 25) \
132 _ (sha, 7, ebx, 29) \
133 _ (vaes, 7, ecx, 9) \
134 _ (vpclmulqdq, 7, ecx, 10) \
135 _ (avx512_vnni, 7, ecx, 11) \
136 _ (avx512_bitalg, 7, ecx, 12) \
137 _ (avx512_vpopcntdq, 7, ecx, 14) \
138 _ (invariant_tsc, 0x80000007, edx, 8)
139 
140 
141 #define foreach_aarch64_flags \
142 _ (fp, 0) \
143 _ (asimd, 1) \
144 _ (evtstrm, 2) \
145 _ (aarch64_aes, 3) \
146 _ (pmull, 4) \
147 _ (sha1, 5) \
148 _ (sha2, 6) \
149 _ (crc32, 7) \
150 _ (atomics, 8) \
151 _ (fphp, 9) \
152 _ (asimdhp, 10) \
153 _ (cpuid, 11) \
154 _ (asimdrdm, 12) \
155 _ (jscvt, 13) \
156 _ (fcma, 14) \
157 _ (lrcpc, 15) \
158 _ (dcpop, 16) \
159 _ (sha3, 17) \
160 _ (sm3, 18) \
161 _ (sm4, 19) \
162 _ (asimddp, 20) \
163 _ (sha512, 21) \
164 _ (sve, 22)
165 
166 static inline u32
168 {
169  unsigned cpu, node;
170  syscall (__NR_getcpu, &cpu, &node, 0);
171  return cpu;
172 }
173 
174 static inline u32
176 {
177  unsigned cpu, node;
178  syscall (__NR_getcpu, &cpu, &node, 0);
179  return node;
180 }
181 
182 #if defined(__x86_64__)
183 #include "cpuid.h"
184 
185 static inline int
186 clib_get_cpuid (const u32 lev, u32 * eax, u32 * ebx, u32 * ecx, u32 * edx)
187 {
188  if ((u32) __get_cpuid_max (0x80000000 & lev, 0) < lev)
189  return 0;
190  if (lev == 7)
191  __cpuid_count (lev, 0, *eax, *ebx, *ecx, *edx);
192  else
193  __cpuid (lev, *eax, *ebx, *ecx, *edx);
194  return 1;
195 }
196 
197 
198 #define _(flag, func, reg, bit) \
199 static inline int \
200 clib_cpu_supports_ ## flag() \
201 { \
202  u32 __attribute__((unused)) eax, ebx = 0, ecx = 0, edx = 0; \
203  clib_get_cpuid (func, &eax, &ebx, &ecx, &edx); \
204  \
205  return ((reg & (1 << bit)) != 0); \
206 }
208 #undef _
209 #else /* __x86_64__ */
210 
211 #define _(flag, func, reg, bit) \
212 static inline int clib_cpu_supports_ ## flag() { return 0; }
214 #undef _
215 #endif /* __x86_64__ */
216 #if defined(__aarch64__)
217 #include <sys/auxv.h>
218 #define _(flag, bit) \
219 static inline int \
220 clib_cpu_supports_ ## flag() \
221 { \
222  unsigned long hwcap = getauxval(AT_HWCAP); \
223  return (hwcap & (1 << bit)); \
224 }
226 #undef _
227 #else /* ! __x86_64__ && !__aarch64__ */
228 #define _(flag, bit) \
229 static inline int clib_cpu_supports_ ## flag() { return 0; }
231 #undef _
232 #endif /* __x86_64__, __aarch64__ */
233 /*
234  * aes is the only feature with the same name in both flag lists
235  * handle this by prefixing it with the arch name, and handling it
236  * with the custom function below
237  */
238  static inline int
240 {
241 #if defined(__x86_64__)
242  return clib_cpu_supports_x86_aes ();
243 #elif defined (__aarch64__)
244  return clib_cpu_supports_aarch64_aes ();
245 #else
246  return 0;
247 #endif
248 }
249 
250 static inline int
252 {
253  if (clib_cpu_supports_avx512_bitalg ())
254  return 200;
255  return -1;
256 }
257 
258 static inline int
260 {
261  if (clib_cpu_supports_avx512f ())
262  return 100;
263  return -1;
264 }
265 
266 static inline int
268 {
269  if (clib_cpu_supports_avx2 ())
270  return 50;
271  return -1;
272 }
273 
274 static inline u32
276 {
277  char buf[128];
278  static u32 implementer = -1;
279 
280  if (-1 != implementer)
281  return implementer;
282 
283  FILE *fp = fopen ("/proc/cpuinfo", "r");
284  if (!fp)
285  return implementer;
286 
287  while (!feof (fp))
288  {
289  if (!fgets (buf, sizeof (buf), fp))
290  break;
291  buf[127] = '\0';
292  if (strstr (buf, "CPU implementer"))
293  implementer = (u32) strtol (memchr (buf, ':', 128) + 2, NULL, 0);
294  if (-1 != implementer)
295  break;
296  }
297  fclose (fp);
298 
299  return implementer;
300 }
301 
302 static inline u32
304 {
305  char buf[128];
306  static u32 part = -1;
307 
308  if (-1 != part)
309  return part;
310 
311  FILE *fp = fopen ("/proc/cpuinfo", "r");
312  if (!fp)
313  return part;
314 
315  while (!feof (fp))
316  {
317  if (!fgets (buf, sizeof (buf), fp))
318  break;
319  buf[127] = '\0';
320  if (strstr (buf, "CPU part"))
321  part = (u32) strtol (memchr (buf, ':', 128) + 2, NULL, 0);
322  if (-1 != part)
323  break;
324  }
325  fclose (fp);
326 
327  return part;
328 }
329 
330 #define AARCH64_CPU_IMPLEMENTER_CAVIUM 0x43
331 #define AARCH64_CPU_PART_THUNDERX2 0x0af
332 #define AARCH64_CPU_PART_OCTEONTX2T96 0x0b2
333 #define AARCH64_CPU_PART_OCTEONTX2T98 0x0b1
334 #define AARCH64_CPU_IMPLEMENTER_QDF24XX 0x51
335 #define AARCH64_CPU_PART_QDF24XX 0xc00
336 #define AARCH64_CPU_IMPLEMENTER_CORTEXA72 0x41
337 #define AARCH64_CPU_PART_CORTEXA72 0xd08
338 #define AARCH64_CPU_IMPLEMENTER_NEOVERSEN1 0x41
339 #define AARCH64_CPU_PART_NEOVERSEN1 0xd0c
340 
341 static inline int
343 {
347  return 20;
348  return -1;
349 }
350 
351 static inline int
353 {
356  return 20;
357  return -1;
358 }
359 
360 static inline int
362 {
365  return 20;
366  return -1;
367 }
368 
369 static inline int
371 {
374  return 10;
375  return -1;
376 }
377 
378 static inline int
380 {
383  return 10;
384  return -1;
385 }
386 
387 #ifdef CLIB_MARCH_VARIANT
388 #define CLIB_MARCH_FN_PRIORITY() CLIB_MARCH_SFX(clib_cpu_march_priority)()
389 #else
390 #define CLIB_MARCH_FN_PRIORITY() 0
391 #endif
392 #endif /* included_clib_cpu_h */
393 
394 #define CLIB_MARCH_FN_CONSTRUCTOR(fn) \
395 static void __clib_constructor \
396 CLIB_MARCH_SFX(fn ## _march_constructor) (void) \
397 { \
398  if (CLIB_MARCH_FN_PRIORITY() > fn ## _selected_priority) \
399  { \
400  fn ## _selected = & CLIB_MARCH_SFX (fn ## _ma); \
401  fn ## _selected_priority = CLIB_MARCH_FN_PRIORITY(); \
402  } \
403 } \
404 
405 #ifndef CLIB_MARCH_VARIANT
406 #define CLIB_MARCH_FN(fn, rtype, _args...) \
407  static rtype CLIB_CPU_OPTIMIZED CLIB_MARCH_SFX (fn ## _ma)(_args); \
408  rtype (*fn ## _selected) (_args) = & CLIB_MARCH_SFX (fn ## _ma); \
409  int fn ## _selected_priority = 0; \
410  static inline rtype CLIB_CPU_OPTIMIZED \
411  CLIB_MARCH_SFX (fn ## _ma)(_args)
412 #else
413 #define CLIB_MARCH_FN(fn, rtype, _args...) \
414  static rtype CLIB_CPU_OPTIMIZED CLIB_MARCH_SFX (fn ## _ma)(_args); \
415  extern rtype (*fn ## _selected) (_args); \
416  extern int fn ## _selected_priority; \
417  CLIB_MARCH_FN_CONSTRUCTOR (fn) \
418  static rtype CLIB_CPU_OPTIMIZED CLIB_MARCH_SFX (fn ## _ma)(_args)
419 #endif
420 
421 #define CLIB_MARCH_FN_SELECT(fn) (* fn ## _selected)
422 
426 
427 /*
428  * fd.io coding-style-patch-verification: ON
429  *
430  * Local Variables:
431  * eval: (c-set-style "gnu")
432  * End:
433  */
static int clib_cpu_march_priority_cortexa72()
Definition: cpu.h:370
#define AARCH64_CPU_IMPLEMENTER_NEOVERSEN1
Definition: cpu.h:338
static int clib_cpu_march_priority_octeontx2()
Definition: cpu.h:342
static u32 clib_get_current_numa_node()
Definition: cpu.h:175
static int clib_cpu_march_priority_neoversen1()
Definition: cpu.h:379
#define AARCH64_CPU_PART_OCTEONTX2T96
Definition: cpu.h:332
static u32 clib_get_current_cpu_id()
Definition: cpu.h:167
format_function_t format_cpu_flags
Definition: cpu.h:425
static int clib_cpu_march_priority_hsw()
Definition: cpu.h:267
#define foreach_aarch64_flags
Definition: cpu.h:141
u8 *() format_function_t(u8 *s, va_list *args)
Definition: format.h:48
static int clib_cpu_march_priority_skx()
Definition: cpu.h:259
#define static_always_inline
Definition: clib.h:108
unsigned int u32
Definition: types.h:88
#define AARCH64_CPU_IMPLEMENTER_CAVIUM
Definition: cpu.h:330
i32 priority
Definition: ipsec.api:95
static u32 clib_cpu_part()
Definition: cpu.h:303
#define AARCH64_CPU_PART_QDF24XX
Definition: cpu.h:335
#define AARCH64_CPU_PART_NEOVERSEN1
Definition: cpu.h:339
static int clib_cpu_march_priority_thunderx2t99()
Definition: cpu.h:352
static int clib_get_cpuid(const u32 lev, u32 *eax, u32 *ebx, u32 *ecx, u32 *edx)
Definition: cpu.h:186
static u32 clib_cpu_implementer()
Definition: cpu.h:275
#define AARCH64_CPU_PART_OCTEONTX2T98
Definition: cpu.h:333
string name[64]
Definition: ip.api:44
#define foreach_x86_64_flags
Definition: cpu.h:117
vlib_main_t vlib_node_runtime_t * node
Definition: in2out_ed.c:1582
struct _clib_march_fn_registration clib_march_fn_registration
static_always_inline void * clib_march_select_fn_ptr(clib_march_fn_registration *r)
Definition: cpu.h:70
static foreach_aarch64_flags int clib_cpu_supports_aes()
Definition: cpu.h:239
#define AARCH64_CPU_IMPLEMENTER_CORTEXA72
Definition: cpu.h:336
static int clib_cpu_march_priority_qdf24xx()
Definition: cpu.h:361
format_function_t format_cpu_uarch
Definition: cpu.h:423
#define AARCH64_CPU_PART_THUNDERX2
Definition: cpu.h:331
format_function_t format_cpu_model_name
Definition: cpu.h:424
#define AARCH64_CPU_PART_CORTEXA72
Definition: cpu.h:337
static int clib_cpu_march_priority_icl()
Definition: cpu.h:251
#define AARCH64_CPU_IMPLEMENTER_QDF24XX
Definition: cpu.h:334