blob: 329e5cc298ddc5f3b4e8f771926f2d4e0118dd97 [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
Damjan Mariona31698b2021-03-10 14:35:28 +010022#if defined(__x86_64__)
23#define foreach_march_variant \
24 _ (hsw, "Intel Haswell") \
25 _ (trm, "Intel Tremont") \
26 _ (skx, "Intel Skylake (server) / Cascade Lake") \
27 _ (icl, "Intel Ice Lake")
28#elif defined(__aarch64__)
29#define foreach_march_variant \
30 _ (octeontx2, "Marvell Octeon TX2") \
31 _ (thunderx2t99, "Marvell ThunderX2 T99") \
32 _ (qdf24xx, "Qualcomm CentriqTM 2400") \
33 _ (cortexa72, "ARM Cortex-A72") \
34 _ (neoversen1, "ARM Neoverse N1")
Damjan Marion1c80e832016-05-11 23:07:18 +020035#else
Damjan Mariona31698b2021-03-10 14:35:28 +010036#define foreach_march_variant
Damjan Marion1c80e832016-05-11 23:07:18 +020037#endif
38
Damjan Mariona31698b2021-03-10 14:35:28 +010039typedef enum
40{
41 CLIB_MARCH_VARIANT_TYPE = 0,
42#define _(s, n) CLIB_MARCH_VARIANT_TYPE_##s,
43 foreach_march_variant
44#undef _
45 CLIB_MARCH_TYPE_N_VARIANTS
46} clib_march_variant_type_t;
Damjan Marion1c80e832016-05-11 23:07:18 +020047
Damjan Marion812b32d2018-05-28 21:26:47 +020048#ifdef CLIB_MARCH_VARIANT
Damjan Marion04f3db32017-11-10 21:55:45 +010049#define __CLIB_MULTIARCH_FN(a,b) a##_##b
50#define _CLIB_MULTIARCH_FN(a,b) __CLIB_MULTIARCH_FN(a,b)
Damjan Marion812b32d2018-05-28 21:26:47 +020051#define CLIB_MULTIARCH_FN(fn) _CLIB_MULTIARCH_FN(fn,CLIB_MARCH_VARIANT)
Damjan Marion04f3db32017-11-10 21:55:45 +010052#else
53#define CLIB_MULTIARCH_FN(fn) fn
54#endif
Damjan Marion1c80e832016-05-11 23:07:18 +020055
Damjan Marion812b32d2018-05-28 21:26:47 +020056#define CLIB_MARCH_SFX CLIB_MULTIARCH_FN
57
Damjan Marion910d3692019-01-21 11:48:34 +010058typedef struct _clib_march_fn_registration
59{
60 void *function;
61 int priority;
62 struct _clib_march_fn_registration *next;
63 char *name;
64} clib_march_fn_registration;
65
66static_always_inline void *
67clib_march_select_fn_ptr (clib_march_fn_registration * r)
68{
69 void *rv = 0;
70 int last_prio = -1;
71
72 while (r)
73 {
74 if (last_prio < r->priority)
75 {
76 last_prio = r->priority;
77 rv = r->function;
78 }
79 r = r->next;
80 }
81 return rv;
82}
83
Damjan Marion11e0d752021-05-05 20:06:39 +020084#define CLIB_MARCH_FN_POINTER(fn) \
85 (__typeof__ (fn) *) clib_march_select_fn_ptr (fn##_march_fn_registrations);
Damjan Marion910d3692019-01-21 11:48:34 +010086
87#define _CLIB_MARCH_FN_REGISTRATION(fn) \
88static clib_march_fn_registration \
89CLIB_MARCH_SFX(fn##_march_fn_registration) = \
90{ \
91 .name = CLIB_MARCH_VARIANT_STR \
92}; \
93\
94static void __clib_constructor \
95fn##_march_register () \
96{ \
97 clib_march_fn_registration *r; \
98 r = & CLIB_MARCH_SFX (fn##_march_fn_registration); \
99 r->priority = CLIB_MARCH_FN_PRIORITY(); \
100 r->next = fn##_march_fn_registrations; \
101 r->function = CLIB_MARCH_SFX (fn); \
102 fn##_march_fn_registrations = r; \
103}
104
105#ifdef CLIB_MARCH_VARIANT
106#define CLIB_MARCH_FN_REGISTRATION(fn) \
107extern clib_march_fn_registration *fn##_march_fn_registrations; \
108_CLIB_MARCH_FN_REGISTRATION(fn)
109#else
110#define CLIB_MARCH_FN_REGISTRATION(fn) \
111clib_march_fn_registration *fn##_march_fn_registrations = 0; \
112_CLIB_MARCH_FN_REGISTRATION(fn)
113#endif
Radu Nicolaue1480a22021-01-14 10:25:02 +0000114#define foreach_x86_64_flags \
115 _ (sse3, 1, ecx, 0) \
116 _ (pclmulqdq, 1, ecx, 1) \
117 _ (ssse3, 1, ecx, 9) \
118 _ (sse41, 1, ecx, 19) \
119 _ (sse42, 1, ecx, 20) \
120 _ (avx, 1, ecx, 28) \
121 _ (rdrand, 1, ecx, 30) \
122 _ (avx2, 7, ebx, 5) \
123 _ (rtm, 7, ebx, 11) \
124 _ (pqm, 7, ebx, 12) \
125 _ (pqe, 7, ebx, 15) \
126 _ (avx512f, 7, ebx, 16) \
127 _ (rdseed, 7, ebx, 18) \
128 _ (x86_aes, 1, ecx, 25) \
129 _ (sha, 7, ebx, 29) \
130 _ (vaes, 7, ecx, 9) \
131 _ (vpclmulqdq, 7, ecx, 10) \
132 _ (avx512_vnni, 7, ecx, 11) \
133 _ (avx512_bitalg, 7, ecx, 12) \
134 _ (avx512_vpopcntdq, 7, ecx, 14) \
135 _ (movdiri, 7, ecx, 27) \
136 _ (movdir64b, 7, ecx, 28) \
Ray Kinsella0d27e3e2021-10-15 12:48:31 +0100137 _ (avx512_fp16, 7, edx, 23) \
Radu Nicolaue1480a22021-01-14 10:25:02 +0000138 _ (invariant_tsc, 0x80000007, edx, 8)
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100139
140#define foreach_aarch64_flags \
141_ (fp, 0) \
142_ (asimd, 1) \
143_ (evtstrm, 2) \
144_ (aarch64_aes, 3) \
145_ (pmull, 4) \
146_ (sha1, 5) \
147_ (sha2, 6) \
148_ (crc32, 7) \
149_ (atomics, 8) \
150_ (fphp, 9) \
151_ (asimdhp, 10) \
152_ (cpuid, 11) \
153_ (asimdrdm, 12) \
154_ (jscvt, 13) \
155_ (fcma, 14) \
156_ (lrcpc, 15) \
157_ (dcpop, 16) \
158_ (sha3, 17) \
159_ (sm3, 18) \
160_ (sm4, 19) \
161_ (asimddp, 20) \
162_ (sha512, 21) \
163_ (sve, 22)
164
Damjan Marionf8cb7012020-10-09 17:16:55 +0200165u32 clib_get_current_cpu_id ();
166u32 clib_get_current_numa_node ();
Damjan Marion0a78fa12019-01-19 23:45:36 +0100167
Christophe Fontaine33e81952016-12-19 14:41:52 +0100168#if defined(__x86_64__)
169#include "cpuid.h"
170
Damjan Marion1c80e832016-05-11 23:07:18 +0200171static inline int
Dave Barachc3799992016-08-15 11:12:27 -0400172clib_get_cpuid (const u32 lev, u32 * eax, u32 * ebx, u32 * ecx, u32 * edx)
Damjan Marion1c80e832016-05-11 23:07:18 +0200173{
174 if ((u32) __get_cpuid_max (0x80000000 & lev, 0) < lev)
175 return 0;
176 if (lev == 7)
Dave Barachc3799992016-08-15 11:12:27 -0400177 __cpuid_count (lev, 0, *eax, *ebx, *ecx, *edx);
Damjan Marion1c80e832016-05-11 23:07:18 +0200178 else
Dave Barachc3799992016-08-15 11:12:27 -0400179 __cpuid (lev, *eax, *ebx, *ecx, *edx);
Damjan Marion1c80e832016-05-11 23:07:18 +0200180 return 1;
181}
182
mdr788e1384f2021-03-19 19:03:54 +0000183typedef int (*clib_cpu_supports_func_t) ();
Damjan Marion1c80e832016-05-11 23:07:18 +0200184
185#define _(flag, func, reg, bit) \
186static inline int \
187clib_cpu_supports_ ## flag() \
188{ \
189 u32 __attribute__((unused)) eax, ebx = 0, ecx = 0, edx = 0; \
190 clib_get_cpuid (func, &eax, &ebx, &ecx, &edx); \
191 \
192 return ((reg & (1 << bit)) != 0); \
193}
Dave Barachc3799992016-08-15 11:12:27 -0400194foreach_x86_64_flags
Damjan Marion1c80e832016-05-11 23:07:18 +0200195#undef _
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100196#else /* __x86_64__ */
Christophe Fontaine33e81952016-12-19 14:41:52 +0100197
198#define _(flag, func, reg, bit) \
199static inline int clib_cpu_supports_ ## flag() { return 0; }
200foreach_x86_64_flags
201#undef _
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100202#endif /* __x86_64__ */
203#if defined(__aarch64__)
204#include <sys/auxv.h>
205#define _(flag, bit) \
206static inline int \
207clib_cpu_supports_ ## flag() \
208{ \
209 unsigned long hwcap = getauxval(AT_HWCAP); \
210 return (hwcap & (1 << bit)); \
211}
212 foreach_aarch64_flags
213#undef _
214#else /* ! __x86_64__ && !__aarch64__ */
215#define _(flag, bit) \
216static inline int clib_cpu_supports_ ## flag() { return 0; }
217 foreach_aarch64_flags
218#undef _
219#endif /* __x86_64__, __aarch64__ */
220/*
221 * aes is the only feature with the same name in both flag lists
222 * handle this by prefixing it with the arch name, and handling it
223 * with the custom function below
224 */
225 static inline int
226clib_cpu_supports_aes ()
227{
Zhiyong Yang7f4fd222019-04-19 03:04:41 -0400228#if defined(__x86_64__)
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100229 return clib_cpu_supports_x86_aes ();
230#elif defined (__aarch64__)
231 return clib_cpu_supports_aarch64_aes ();
232#else
233 return 0;
Christophe Fontaine33e81952016-12-19 14:41:52 +0100234#endif
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100235}
236
Damjan Marion812b32d2018-05-28 21:26:47 +0200237static inline int
Damjan Marion162330f2020-04-29 21:28:15 +0200238clib_cpu_march_priority_icl ()
Damjan Marion812b32d2018-05-28 21:26:47 +0200239{
Damjan Marion162330f2020-04-29 21:28:15 +0200240 if (clib_cpu_supports_avx512_bitalg ())
241 return 200;
Damjan Marion812b32d2018-05-28 21:26:47 +0200242 return -1;
243}
244
245static inline int
Damjan Marion162330f2020-04-29 21:28:15 +0200246clib_cpu_march_priority_skx ()
247{
248 if (clib_cpu_supports_avx512f ())
249 return 100;
250 return -1;
251}
252
253static inline int
Radu Nicolaue1480a22021-01-14 10:25:02 +0000254clib_cpu_march_priority_trm ()
255{
256 if (clib_cpu_supports_movdiri ())
257 return 60;
258 return -1;
259}
260
261static inline int
Damjan Marion162330f2020-04-29 21:28:15 +0200262clib_cpu_march_priority_hsw ()
Damjan Marion812b32d2018-05-28 21:26:47 +0200263{
264 if (clib_cpu_supports_avx2 ())
Damjan Marion64593152019-03-12 19:59:22 +0100265 return 50;
Damjan Marion812b32d2018-05-28 21:26:47 +0200266 return -1;
267}
268
Ray Kinsella0024e532021-11-26 14:57:35 +0000269#define X86_CPU_ARCH_PERF_FUNC 0xA
270
271static inline int
272clib_get_pmu_counter_count (u8 *fixed, u8 *general)
273{
274#if defined(__x86_64__)
275 u32 __clib_unused eax = 0, ebx = 0, ecx = 0, edx = 0;
276 clib_get_cpuid (X86_CPU_ARCH_PERF_FUNC, &eax, &ebx, &ecx, &edx);
277
278 *general = (eax & 0xFF00) >> 8;
279 *fixed = (edx & 0xF);
280
281 return 1;
282#else
283 return 0;
284#endif
285}
286
Lijian Zhang2e237212018-09-10 17:13:56 +0800287static inline u32
288clib_cpu_implementer ()
289{
290 char buf[128];
291 static u32 implementer = -1;
292
293 if (-1 != implementer)
294 return implementer;
295
296 FILE *fp = fopen ("/proc/cpuinfo", "r");
297 if (!fp)
298 return implementer;
299
300 while (!feof (fp))
301 {
302 if (!fgets (buf, sizeof (buf), fp))
303 break;
304 buf[127] = '\0';
305 if (strstr (buf, "CPU implementer"))
306 implementer = (u32) strtol (memchr (buf, ':', 128) + 2, NULL, 0);
307 if (-1 != implementer)
308 break;
309 }
310 fclose (fp);
311
312 return implementer;
313}
314
315static inline u32
316clib_cpu_part ()
317{
318 char buf[128];
319 static u32 part = -1;
320
321 if (-1 != part)
322 return part;
323
324 FILE *fp = fopen ("/proc/cpuinfo", "r");
325 if (!fp)
326 return part;
327
328 while (!feof (fp))
329 {
330 if (!fgets (buf, sizeof (buf), fp))
331 break;
332 buf[127] = '\0';
333 if (strstr (buf, "CPU part"))
334 part = (u32) strtol (memchr (buf, ':', 128) + 2, NULL, 0);
335 if (-1 != part)
336 break;
337 }
338 fclose (fp);
339
340 return part;
341}
342
Nitin Saxenae2f52362020-08-25 19:58:37 +0530343#define AARCH64_CPU_IMPLEMENTER_CAVIUM 0x43
344#define AARCH64_CPU_PART_THUNDERX2 0x0af
345#define AARCH64_CPU_PART_OCTEONTX2T96 0x0b2
346#define AARCH64_CPU_PART_OCTEONTX2T98 0x0b1
Lijian Zhang2e237212018-09-10 17:13:56 +0800347#define AARCH64_CPU_IMPLEMENTER_QDF24XX 0x51
348#define AARCH64_CPU_PART_QDF24XX 0xc00
349#define AARCH64_CPU_IMPLEMENTER_CORTEXA72 0x41
350#define AARCH64_CPU_PART_CORTEXA72 0xd08
Lijian.Zhang690ce862020-02-18 19:58:19 +0800351#define AARCH64_CPU_IMPLEMENTER_NEOVERSEN1 0x41
352#define AARCH64_CPU_PART_NEOVERSEN1 0xd0c
Lijian Zhang2e237212018-09-10 17:13:56 +0800353
354static inline int
Nitin Saxenae2f52362020-08-25 19:58:37 +0530355clib_cpu_march_priority_octeontx2 ()
356{
357 if ((AARCH64_CPU_IMPLEMENTER_CAVIUM == clib_cpu_implementer ()) &&
358 ((AARCH64_CPU_PART_OCTEONTX2T96 == clib_cpu_part ())
359 || AARCH64_CPU_PART_OCTEONTX2T98 == clib_cpu_part ()))
360 return 20;
361 return -1;
362}
363
364static inline int
Lijian Zhang2e237212018-09-10 17:13:56 +0800365clib_cpu_march_priority_thunderx2t99 ()
366{
Nitin Saxenae2f52362020-08-25 19:58:37 +0530367 if ((AARCH64_CPU_IMPLEMENTER_CAVIUM == clib_cpu_implementer ()) &&
368 (AARCH64_CPU_PART_THUNDERX2 == clib_cpu_part ()))
Lijian Zhang2e237212018-09-10 17:13:56 +0800369 return 20;
370 return -1;
371}
372
373static inline int
374clib_cpu_march_priority_qdf24xx ()
375{
376 if ((AARCH64_CPU_IMPLEMENTER_QDF24XX == clib_cpu_implementer ()) &&
377 (AARCH64_CPU_PART_QDF24XX == clib_cpu_part ()))
378 return 20;
379 return -1;
380}
381
382static inline int
383clib_cpu_march_priority_cortexa72 ()
384{
385 if ((AARCH64_CPU_IMPLEMENTER_CORTEXA72 == clib_cpu_implementer ()) &&
386 (AARCH64_CPU_PART_CORTEXA72 == clib_cpu_part ()))
387 return 10;
388 return -1;
389}
390
Lijian.Zhang690ce862020-02-18 19:58:19 +0800391static inline int
392clib_cpu_march_priority_neoversen1 ()
393{
394 if ((AARCH64_CPU_IMPLEMENTER_NEOVERSEN1 == clib_cpu_implementer ()) &&
395 (AARCH64_CPU_PART_NEOVERSEN1 == clib_cpu_part ()))
396 return 10;
397 return -1;
398}
399
Damjan Marion812b32d2018-05-28 21:26:47 +0200400#ifdef CLIB_MARCH_VARIANT
401#define CLIB_MARCH_FN_PRIORITY() CLIB_MARCH_SFX(clib_cpu_march_priority)()
402#else
403#define CLIB_MARCH_FN_PRIORITY() 0
404#endif
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100405#endif /* included_clib_cpu_h */
406
Florin Coras983cc7d2018-09-18 23:11:55 -0700407#define CLIB_MARCH_FN_CONSTRUCTOR(fn) \
408static void __clib_constructor \
409CLIB_MARCH_SFX(fn ## _march_constructor) (void) \
410{ \
411 if (CLIB_MARCH_FN_PRIORITY() > fn ## _selected_priority) \
412 { \
413 fn ## _selected = & CLIB_MARCH_SFX (fn ## _ma); \
414 fn ## _selected_priority = CLIB_MARCH_FN_PRIORITY(); \
415 } \
416} \
417
418#ifndef CLIB_MARCH_VARIANT
Damjan Marion1bb67ab2021-04-30 11:11:08 +0200419#define CLIB_MARCH_FN(fn, rtype, _args...) \
420 static rtype CLIB_MARCH_SFX (fn##_ma) (_args); \
421 rtype (*fn##_selected) (_args) = &CLIB_MARCH_SFX (fn##_ma); \
422 int fn##_selected_priority = 0; \
423 static inline rtype CLIB_MARCH_SFX (fn##_ma) (_args)
Florin Coras983cc7d2018-09-18 23:11:55 -0700424#else
Damjan Marion1bb67ab2021-04-30 11:11:08 +0200425#define CLIB_MARCH_FN(fn, rtype, _args...) \
426 static rtype CLIB_MARCH_SFX (fn##_ma) (_args); \
427 extern rtype (*fn##_selected) (_args); \
428 extern int fn##_selected_priority; \
429 CLIB_MARCH_FN_CONSTRUCTOR (fn) \
430 static rtype CLIB_MARCH_SFX (fn##_ma) (_args)
Florin Coras983cc7d2018-09-18 23:11:55 -0700431#endif
432
433#define CLIB_MARCH_FN_SELECT(fn) (* fn ## _selected)
434
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100435format_function_t format_cpu_uarch;
Damjan Marion522e4862016-03-04 12:44:14 +0100436format_function_t format_cpu_model_name;
Damjan Marion1c80e832016-05-11 23:07:18 +0200437format_function_t format_cpu_flags;
Damjan Marione3e35552021-05-06 17:34:49 +0200438format_function_t format_march_variant;
Damjan Marion522e4862016-03-04 12:44:14 +0100439
Dave Barachc3799992016-08-15 11:12:27 -0400440/*
441 * fd.io coding-style-patch-verification: ON
442 *
443 * Local Variables:
444 * eval: (c-set-style "gnu")
445 * End:
446 */