blob: 31a6e5b48e060ab6fad93afb7b49baccd1065ccb [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) \
119_ (ssse3, 1, ecx, 9) \
120_ (sse41, 1, ecx, 19) \
121_ (sse42, 1, ecx, 20) \
122_ (avx, 1, ecx, 28) \
Damjan Marion65ec33b2019-03-30 12:56:37 +0100123_ (rdrand, 1, ecx, 30) \
Damjan Marion1c80e832016-05-11 23:07:18 +0200124_ (avx2, 7, ebx, 5) \
Damjan Marion65ec33b2019-03-30 12:56:37 +0100125_ (rtm, 7, ebx, 11) \
126_ (pqm, 7, ebx, 12) \
127_ (pqe, 7, ebx, 15) \
Damjan Marion1c80e832016-05-11 23:07:18 +0200128_ (avx512f, 7, ebx, 16) \
Damjan Marion65ec33b2019-03-30 12:56:37 +0100129_ (rdseed, 7, ebx, 18) \
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100130_ (x86_aes, 1, ecx, 25) \
Damjan Marionc0e939b2016-11-12 11:50:01 +0100131_ (sha, 7, ebx, 29) \
Damjan Marion65ec33b2019-03-30 12:56:37 +0100132_ (vaes, 7, ecx, 9) \
133_ (vpclmulqdq, 7, ecx, 10) \
Damjan Marionc0e939b2016-11-12 11:50:01 +0100134_ (invariant_tsc, 0x80000007, edx, 8)
Damjan Marion1c80e832016-05-11 23:07:18 +0200135
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100136
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
Damjan Marion0a78fa12019-01-19 23:45:36 +0100162static inline u32
Damjan Marionee721412019-01-27 17:54:11 +0100163clib_get_current_cpu_id ()
Damjan Marion0a78fa12019-01-19 23:45:36 +0100164{
165 unsigned cpu, node;
166 syscall (__NR_getcpu, &cpu, &node, 0);
167 return cpu;
168}
169
170static inline u32
171clib_get_current_numa_node ()
172{
173 unsigned cpu, node;
174 syscall (__NR_getcpu, &cpu, &node, 0);
175 return node;
176}
177
Christophe Fontaine33e81952016-12-19 14:41:52 +0100178#if defined(__x86_64__)
179#include "cpuid.h"
180
Damjan Marion1c80e832016-05-11 23:07:18 +0200181static inline int
Dave Barachc3799992016-08-15 11:12:27 -0400182clib_get_cpuid (const u32 lev, u32 * eax, u32 * ebx, u32 * ecx, u32 * edx)
Damjan Marion1c80e832016-05-11 23:07:18 +0200183{
184 if ((u32) __get_cpuid_max (0x80000000 & lev, 0) < lev)
185 return 0;
186 if (lev == 7)
Dave Barachc3799992016-08-15 11:12:27 -0400187 __cpuid_count (lev, 0, *eax, *ebx, *ecx, *edx);
Damjan Marion1c80e832016-05-11 23:07:18 +0200188 else
Dave Barachc3799992016-08-15 11:12:27 -0400189 __cpuid (lev, *eax, *ebx, *ecx, *edx);
Damjan Marion1c80e832016-05-11 23:07:18 +0200190 return 1;
191}
192
193
194#define _(flag, func, reg, bit) \
195static inline int \
196clib_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}
Dave Barachc3799992016-08-15 11:12:27 -0400203foreach_x86_64_flags
Damjan Marion1c80e832016-05-11 23:07:18 +0200204#undef _
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100205#else /* __x86_64__ */
Christophe Fontaine33e81952016-12-19 14:41:52 +0100206
207#define _(flag, func, reg, bit) \
208static inline int clib_cpu_supports_ ## flag() { return 0; }
209foreach_x86_64_flags
210#undef _
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100211#endif /* __x86_64__ */
212#if defined(__aarch64__)
213#include <sys/auxv.h>
214#define _(flag, bit) \
215static inline int \
216clib_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) \
225static 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
235clib_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;
Christophe Fontaine33e81952016-12-19 14:41:52 +0100243#endif
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100244}
245
Damjan Marion812b32d2018-05-28 21:26:47 +0200246static inline int
247clib_cpu_march_priority_avx512 ()
248{
249 if (clib_cpu_supports_avx512f ())
250 return 20;
251 return -1;
252}
253
254static inline int
255clib_cpu_march_priority_avx2 ()
256{
257 if (clib_cpu_supports_avx2 ())
Damjan Marion64593152019-03-12 19:59:22 +0100258 return 50;
Damjan Marion812b32d2018-05-28 21:26:47 +0200259 return -1;
260}
261
Lijian Zhang2e237212018-09-10 17:13:56 +0800262static inline u32
263clib_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
290static inline u32
291clib_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
325static inline int
326clib_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
334static inline int
335clib_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
343static inline int
344clib_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
Damjan Marion812b32d2018-05-28 21:26:47 +0200352#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
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100357#endif /* included_clib_cpu_h */
358
Florin Coras983cc7d2018-09-18 23:11:55 -0700359#define CLIB_MARCH_FN_CONSTRUCTOR(fn) \
360static void __clib_constructor \
361CLIB_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); \
Florin Coras5fc939e2019-03-20 08:11:14 -0700380 extern rtype (*fn ## _selected) (_args); \
Florin Coras983cc7d2018-09-18 23:11:55 -0700381 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
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100388format_function_t format_cpu_uarch;
Damjan Marion522e4862016-03-04 12:44:14 +0100389format_function_t format_cpu_model_name;
Damjan Marion1c80e832016-05-11 23:07:18 +0200390format_function_t format_cpu_flags;
Damjan Marion522e4862016-03-04 12:44:14 +0100391
Dave Barachc3799992016-08-15 11:12:27 -0400392/*
393 * fd.io coding-style-patch-verification: ON
394 *
395 * Local Variables:
396 * eval: (c-set-style "gnu")
397 * End:
398 */