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 | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 19 | #include <vppinfra/format.h> |
| 20 | |
| 21 | /* |
| 22 | * multiarchitecture support. Adding new entry will produce |
| 23 | * new graph node function variant optimized for specific cpu |
| 24 | * microarchitecture. |
| 25 | * Order is important for runtime selection, as 1st match wins... |
| 26 | */ |
| 27 | |
| 28 | #if __x86_64__ && CLIB_DEBUG == 0 |
| 29 | #define foreach_march_variant(macro, x) \ |
| 30 | macro(avx2, x, "arch=core-avx2") |
| 31 | #else |
| 32 | #define foreach_march_variant(macro, x) |
| 33 | #endif |
| 34 | |
| 35 | |
| 36 | #if __GNUC__ > 4 && !__clang__ |
| 37 | #define CLIB_CPU_OPTIMIZED __attribute__ ((optimize ("tree-vectorize"))) |
| 38 | #else |
| 39 | #define CLIB_CPU_OPTIMIZED |
| 40 | #endif |
| 41 | |
| 42 | |
| 43 | #define CLIB_MULTIARCH_ARCH_CHECK(arch, fn, tgt) \ |
| 44 | if (clib_cpu_supports_ ## arch()) \ |
| 45 | return & fn ## _ ##arch; |
| 46 | |
| 47 | #define CLIB_MULTIARCH_SELECT_FN(fn,...) \ |
| 48 | __VA_ARGS__ void * fn ## _multiarch_select(void) \ |
| 49 | { \ |
| 50 | foreach_march_variant(CLIB_MULTIARCH_ARCH_CHECK, fn) \ |
| 51 | return & fn; \ |
| 52 | } |
| 53 | |
Damjan Marion | 04f3db3 | 2017-11-10 21:55:45 +0100 | [diff] [blame] | 54 | #ifdef CLIB_MULTIARCH_VARIANT |
| 55 | #define __CLIB_MULTIARCH_FN(a,b) a##_##b |
| 56 | #define _CLIB_MULTIARCH_FN(a,b) __CLIB_MULTIARCH_FN(a,b) |
| 57 | #define CLIB_MULTIARCH_FN(fn) _CLIB_MULTIARCH_FN(fn,CLIB_MULTIARCH_VARIANT) |
| 58 | #else |
| 59 | #define CLIB_MULTIARCH_FN(fn) fn |
| 60 | #endif |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 61 | |
| 62 | #define foreach_x86_64_flags \ |
| 63 | _ (sse3, 1, ecx, 0) \ |
| 64 | _ (ssse3, 1, ecx, 9) \ |
| 65 | _ (sse41, 1, ecx, 19) \ |
| 66 | _ (sse42, 1, ecx, 20) \ |
| 67 | _ (avx, 1, ecx, 28) \ |
| 68 | _ (avx2, 7, ebx, 5) \ |
| 69 | _ (avx512f, 7, ebx, 16) \ |
| 70 | _ (aes, 1, ecx, 25) \ |
Damjan Marion | c0e939b | 2016-11-12 11:50:01 +0100 | [diff] [blame] | 71 | _ (sha, 7, ebx, 29) \ |
| 72 | _ (invariant_tsc, 0x80000007, edx, 8) |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 73 | |
Christophe Fontaine | 33e8195 | 2016-12-19 14:41:52 +0100 | [diff] [blame] | 74 | #if defined(__x86_64__) |
| 75 | #include "cpuid.h" |
| 76 | |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 77 | static inline int |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 78 | 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] | 79 | { |
| 80 | if ((u32) __get_cpuid_max (0x80000000 & lev, 0) < lev) |
| 81 | return 0; |
| 82 | if (lev == 7) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 83 | __cpuid_count (lev, 0, *eax, *ebx, *ecx, *edx); |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 84 | else |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 85 | __cpuid (lev, *eax, *ebx, *ecx, *edx); |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 86 | return 1; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | #define _(flag, func, reg, bit) \ |
| 91 | static inline int \ |
| 92 | clib_cpu_supports_ ## flag() \ |
| 93 | { \ |
| 94 | u32 __attribute__((unused)) eax, ebx = 0, ecx = 0, edx = 0; \ |
| 95 | clib_get_cpuid (func, &eax, &ebx, &ecx, &edx); \ |
| 96 | \ |
| 97 | return ((reg & (1 << bit)) != 0); \ |
| 98 | } |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 99 | foreach_x86_64_flags |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 100 | #undef _ |
Christophe Fontaine | 33e8195 | 2016-12-19 14:41:52 +0100 | [diff] [blame] | 101 | #else |
| 102 | |
| 103 | #define _(flag, func, reg, bit) \ |
| 104 | static inline int clib_cpu_supports_ ## flag() { return 0; } |
| 105 | foreach_x86_64_flags |
| 106 | #undef _ |
| 107 | #endif |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 108 | #endif |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 109 | format_function_t format_cpu_uarch; |
Damjan Marion | 522e486 | 2016-03-04 12:44:14 +0100 | [diff] [blame] | 110 | format_function_t format_cpu_model_name; |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 111 | format_function_t format_cpu_flags; |
Damjan Marion | 522e486 | 2016-03-04 12:44:14 +0100 | [diff] [blame] | 112 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 113 | /* |
| 114 | * fd.io coding-style-patch-verification: ON |
| 115 | * |
| 116 | * Local Variables: |
| 117 | * eval: (c-set-style "gnu") |
| 118 | * End: |
| 119 | */ |