blob: dc73c90ca34ea02f632a20b52e93dccc31c935cc [file] [log] [blame]
Damjan Marion522e4862016-03-04 12:44:14 +01001/*
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
Damjan Marion0a78fa12019-01-19 23:45:36 +010019#include <sys/syscall.h>
Damjan Marion1c80e832016-05-11 23:07:18 +020020#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
Damjan Marion5ad75f52018-11-29 14:40:30 +010037#if __GNUC__ > 4 && !__clang__ && CLIB_DEBUG == 0
Damjan Marion24223172018-05-28 16:22:14 +020038#define CLIB_CPU_OPTIMIZED __attribute__ ((optimize ("O3")))
Damjan Marion1c80e832016-05-11 23:07:18 +020039#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
Damjan Marion652d2e12019-02-02 00:15:27 +010048/* FIXME to be removed */
49#define CLIB_MULTIARCH_SELECT_FN(fn,...)
Damjan Marion1c80e832016-05-11 23:07:18 +020050
Damjan Marion812b32d2018-05-28 21:26:47 +020051#ifdef CLIB_MARCH_VARIANT
Damjan Marion04f3db32017-11-10 21:55:45 +010052#define __CLIB_MULTIARCH_FN(a,b) a##_##b
53#define _CLIB_MULTIARCH_FN(a,b) __CLIB_MULTIARCH_FN(a,b)
Damjan Marion812b32d2018-05-28 21:26:47 +020054#define CLIB_MULTIARCH_FN(fn) _CLIB_MULTIARCH_FN(fn,CLIB_MARCH_VARIANT)
Damjan Marion04f3db32017-11-10 21:55:45 +010055#else
56#define CLIB_MULTIARCH_FN(fn) fn
57#endif
Damjan Marion1c80e832016-05-11 23:07:18 +020058
Damjan Marion812b32d2018-05-28 21:26:47 +020059#define CLIB_MARCH_SFX CLIB_MULTIARCH_FN
60
Damjan Marion910d3692019-01-21 11:48:34 +010061typedef 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
69static_always_inline void *
70clib_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) \
91static clib_march_fn_registration \
92CLIB_MARCH_SFX(fn##_march_fn_registration) = \
93{ \
94 .name = CLIB_MARCH_VARIANT_STR \
95}; \
96\
97static void __clib_constructor \
98fn##_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) \
110extern clib_march_fn_registration *fn##_march_fn_registrations; \
111_CLIB_MARCH_FN_REGISTRATION(fn)
112#else
113#define CLIB_MARCH_FN_REGISTRATION(fn) \
114clib_march_fn_registration *fn##_march_fn_registrations = 0; \
115_CLIB_MARCH_FN_REGISTRATION(fn)
116#endif
Damjan Marion1c80e832016-05-11 23:07:18 +0200117#define foreach_x86_64_flags \
118_ (sse3, 1, ecx, 0) \
Damjan Mariondee9bea2019-05-24 13:05:08 +0200119_ (pclmulqdq, 1, ecx, 1) \
Damjan Marion1c80e832016-05-11 23:07:18 +0200120_ (ssse3, 1, ecx, 9) \
121_ (sse41, 1, ecx, 19) \
122_ (sse42, 1, ecx, 20) \
123_ (avx, 1, ecx, 28) \
Damjan Marion65ec33b2019-03-30 12:56:37 +0100124_ (rdrand, 1, ecx, 30) \
Damjan Marion1c80e832016-05-11 23:07:18 +0200125_ (avx2, 7, ebx, 5) \
Damjan Marion65ec33b2019-03-30 12:56:37 +0100126_ (rtm, 7, ebx, 11) \
127_ (pqm, 7, ebx, 12) \
128_ (pqe, 7, ebx, 15) \
Damjan Marion1c80e832016-05-11 23:07:18 +0200129_ (avx512f, 7, ebx, 16) \
Damjan Marion65ec33b2019-03-30 12:56:37 +0100130_ (rdseed, 7, ebx, 18) \
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100131_ (x86_aes, 1, ecx, 25) \
Damjan Marionc0e939b2016-11-12 11:50:01 +0100132_ (sha, 7, ebx, 29) \
Damjan Marion65ec33b2019-03-30 12:56:37 +0100133_ (vaes, 7, ecx, 9) \
134_ (vpclmulqdq, 7, ecx, 10) \
Damjan Marion162330f2020-04-29 21:28:15 +0200135_ (avx512_vnni, 7, ecx, 11) \
136_ (avx512_bitalg, 7, ecx, 12) \
137_ (avx512_vpopcntdq, 7, ecx, 14) \
Damjan Marionc0e939b2016-11-12 11:50:01 +0100138_ (invariant_tsc, 0x80000007, edx, 8)
Damjan Marion1c80e832016-05-11 23:07:18 +0200139
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100140
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
Damjan Marion0a78fa12019-01-19 23:45:36 +0100166static inline u32
Damjan Marionee721412019-01-27 17:54:11 +0100167clib_get_current_cpu_id ()
Damjan Marion0a78fa12019-01-19 23:45:36 +0100168{
169 unsigned cpu, node;
170 syscall (__NR_getcpu, &cpu, &node, 0);
171 return cpu;
172}
173
174static inline u32
175clib_get_current_numa_node ()
176{
177 unsigned cpu, node;
178 syscall (__NR_getcpu, &cpu, &node, 0);
179 return node;
180}
181
Christophe Fontaine33e81952016-12-19 14:41:52 +0100182#if defined(__x86_64__)
183#include "cpuid.h"
184
Damjan Marion1c80e832016-05-11 23:07:18 +0200185static inline int
Dave Barachc3799992016-08-15 11:12:27 -0400186clib_get_cpuid (const u32 lev, u32 * eax, u32 * ebx, u32 * ecx, u32 * edx)
Damjan Marion1c80e832016-05-11 23:07:18 +0200187{
188 if ((u32) __get_cpuid_max (0x80000000 & lev, 0) < lev)
189 return 0;
190 if (lev == 7)
Dave Barachc3799992016-08-15 11:12:27 -0400191 __cpuid_count (lev, 0, *eax, *ebx, *ecx, *edx);
Damjan Marion1c80e832016-05-11 23:07:18 +0200192 else
Dave Barachc3799992016-08-15 11:12:27 -0400193 __cpuid (lev, *eax, *ebx, *ecx, *edx);
Damjan Marion1c80e832016-05-11 23:07:18 +0200194 return 1;
195}
196
197
198#define _(flag, func, reg, bit) \
199static inline int \
200clib_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}
Dave Barachc3799992016-08-15 11:12:27 -0400207foreach_x86_64_flags
Damjan Marion1c80e832016-05-11 23:07:18 +0200208#undef _
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100209#else /* __x86_64__ */
Christophe Fontaine33e81952016-12-19 14:41:52 +0100210
211#define _(flag, func, reg, bit) \
212static inline int clib_cpu_supports_ ## flag() { return 0; }
213foreach_x86_64_flags
214#undef _
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100215#endif /* __x86_64__ */
216#if defined(__aarch64__)
217#include <sys/auxv.h>
218#define _(flag, bit) \
219static inline int \
220clib_cpu_supports_ ## flag() \
221{ \
222 unsigned long hwcap = getauxval(AT_HWCAP); \
223 return (hwcap & (1 << bit)); \
224}
225 foreach_aarch64_flags
226#undef _
227#else /* ! __x86_64__ && !__aarch64__ */
228#define _(flag, bit) \
229static inline int clib_cpu_supports_ ## flag() { return 0; }
230 foreach_aarch64_flags
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
239clib_cpu_supports_aes ()
240{
Zhiyong Yang7f4fd222019-04-19 03:04:41 -0400241#if defined(__x86_64__)
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100242 return clib_cpu_supports_x86_aes ();
243#elif defined (__aarch64__)
244 return clib_cpu_supports_aarch64_aes ();
245#else
246 return 0;
Christophe Fontaine33e81952016-12-19 14:41:52 +0100247#endif
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100248}
249
Damjan Marion812b32d2018-05-28 21:26:47 +0200250static inline int
Damjan Marion162330f2020-04-29 21:28:15 +0200251clib_cpu_march_priority_icl ()
Damjan Marion812b32d2018-05-28 21:26:47 +0200252{
Damjan Marion162330f2020-04-29 21:28:15 +0200253 if (clib_cpu_supports_avx512_bitalg ())
254 return 200;
Damjan Marion812b32d2018-05-28 21:26:47 +0200255 return -1;
256}
257
258static inline int
Damjan Marion162330f2020-04-29 21:28:15 +0200259clib_cpu_march_priority_skx ()
260{
261 if (clib_cpu_supports_avx512f ())
262 return 100;
263 return -1;
264}
265
266static inline int
267clib_cpu_march_priority_hsw ()
Damjan Marion812b32d2018-05-28 21:26:47 +0200268{
269 if (clib_cpu_supports_avx2 ())
Damjan Marion64593152019-03-12 19:59:22 +0100270 return 50;
Damjan Marion812b32d2018-05-28 21:26:47 +0200271 return -1;
272}
273
Lijian Zhang2e237212018-09-10 17:13:56 +0800274static inline u32
275clib_cpu_implementer ()
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
302static inline u32
303clib_cpu_part ()
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_THUNERDERX2 0x43
331#define AARCH64_CPU_PART_THUNERDERX2 0x0af
332#define AARCH64_CPU_IMPLEMENTER_QDF24XX 0x51
333#define AARCH64_CPU_PART_QDF24XX 0xc00
334#define AARCH64_CPU_IMPLEMENTER_CORTEXA72 0x41
335#define AARCH64_CPU_PART_CORTEXA72 0xd08
Lijian.Zhang690ce862020-02-18 19:58:19 +0800336#define AARCH64_CPU_IMPLEMENTER_NEOVERSEN1 0x41
337#define AARCH64_CPU_PART_NEOVERSEN1 0xd0c
Lijian Zhang2e237212018-09-10 17:13:56 +0800338
339static inline int
340clib_cpu_march_priority_thunderx2t99 ()
341{
342 if ((AARCH64_CPU_IMPLEMENTER_THUNERDERX2 == clib_cpu_implementer ()) &&
343 (AARCH64_CPU_PART_THUNERDERX2 == clib_cpu_part ()))
344 return 20;
345 return -1;
346}
347
348static inline int
349clib_cpu_march_priority_qdf24xx ()
350{
351 if ((AARCH64_CPU_IMPLEMENTER_QDF24XX == clib_cpu_implementer ()) &&
352 (AARCH64_CPU_PART_QDF24XX == clib_cpu_part ()))
353 return 20;
354 return -1;
355}
356
357static inline int
358clib_cpu_march_priority_cortexa72 ()
359{
360 if ((AARCH64_CPU_IMPLEMENTER_CORTEXA72 == clib_cpu_implementer ()) &&
361 (AARCH64_CPU_PART_CORTEXA72 == clib_cpu_part ()))
362 return 10;
363 return -1;
364}
365
Lijian.Zhang690ce862020-02-18 19:58:19 +0800366static inline int
367clib_cpu_march_priority_neoversen1 ()
368{
369 if ((AARCH64_CPU_IMPLEMENTER_NEOVERSEN1 == clib_cpu_implementer ()) &&
370 (AARCH64_CPU_PART_NEOVERSEN1 == clib_cpu_part ()))
371 return 10;
372 return -1;
373}
374
Damjan Marion812b32d2018-05-28 21:26:47 +0200375#ifdef CLIB_MARCH_VARIANT
376#define CLIB_MARCH_FN_PRIORITY() CLIB_MARCH_SFX(clib_cpu_march_priority)()
377#else
378#define CLIB_MARCH_FN_PRIORITY() 0
379#endif
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100380#endif /* included_clib_cpu_h */
381
Florin Coras983cc7d2018-09-18 23:11:55 -0700382#define CLIB_MARCH_FN_CONSTRUCTOR(fn) \
383static void __clib_constructor \
384CLIB_MARCH_SFX(fn ## _march_constructor) (void) \
385{ \
386 if (CLIB_MARCH_FN_PRIORITY() > fn ## _selected_priority) \
387 { \
388 fn ## _selected = & CLIB_MARCH_SFX (fn ## _ma); \
389 fn ## _selected_priority = CLIB_MARCH_FN_PRIORITY(); \
390 } \
391} \
392
393#ifndef CLIB_MARCH_VARIANT
394#define CLIB_MARCH_FN(fn, rtype, _args...) \
395 static rtype CLIB_CPU_OPTIMIZED CLIB_MARCH_SFX (fn ## _ma)(_args); \
396 rtype (*fn ## _selected) (_args) = & CLIB_MARCH_SFX (fn ## _ma); \
397 int fn ## _selected_priority = 0; \
398 static inline rtype CLIB_CPU_OPTIMIZED \
399 CLIB_MARCH_SFX (fn ## _ma)(_args)
400#else
401#define CLIB_MARCH_FN(fn, rtype, _args...) \
402 static rtype CLIB_CPU_OPTIMIZED CLIB_MARCH_SFX (fn ## _ma)(_args); \
Florin Coras5fc939e2019-03-20 08:11:14 -0700403 extern rtype (*fn ## _selected) (_args); \
Florin Coras983cc7d2018-09-18 23:11:55 -0700404 extern int fn ## _selected_priority; \
405 CLIB_MARCH_FN_CONSTRUCTOR (fn) \
406 static rtype CLIB_CPU_OPTIMIZED CLIB_MARCH_SFX (fn ## _ma)(_args)
407#endif
408
409#define CLIB_MARCH_FN_SELECT(fn) (* fn ## _selected)
410
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100411format_function_t format_cpu_uarch;
Damjan Marion522e4862016-03-04 12:44:14 +0100412format_function_t format_cpu_model_name;
Damjan Marion1c80e832016-05-11 23:07:18 +0200413format_function_t format_cpu_flags;
Damjan Marion522e4862016-03-04 12:44:14 +0100414
Dave Barachc3799992016-08-15 11:12:27 -0400415/*
416 * fd.io coding-style-patch-verification: ON
417 *
418 * Local Variables:
419 * eval: (c-set-style "gnu")
420 * End:
421 */