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