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__ |
Damjan Marion | 2422317 | 2018-05-28 16:22:14 +0200 | [diff] [blame] | 37 | #define CLIB_CPU_OPTIMIZED __attribute__ ((optimize ("O3"))) |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 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 | 812b32d | 2018-05-28 21:26:47 +0200 | [diff] [blame] | 54 | #ifdef CLIB_MARCH_VARIANT |
Damjan Marion | 04f3db3 | 2017-11-10 21:55:45 +0100 | [diff] [blame] | 55 | #define __CLIB_MULTIARCH_FN(a,b) a##_##b |
| 56 | #define _CLIB_MULTIARCH_FN(a,b) __CLIB_MULTIARCH_FN(a,b) |
Damjan Marion | 812b32d | 2018-05-28 21:26:47 +0200 | [diff] [blame] | 57 | #define CLIB_MULTIARCH_FN(fn) _CLIB_MULTIARCH_FN(fn,CLIB_MARCH_VARIANT) |
Damjan Marion | 04f3db3 | 2017-11-10 21:55:45 +0100 | [diff] [blame] | 58 | #else |
| 59 | #define CLIB_MULTIARCH_FN(fn) fn |
| 60 | #endif |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 61 | |
Damjan Marion | 812b32d | 2018-05-28 21:26:47 +0200 | [diff] [blame] | 62 | #define CLIB_MARCH_SFX CLIB_MULTIARCH_FN |
| 63 | |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 64 | #define foreach_x86_64_flags \ |
| 65 | _ (sse3, 1, ecx, 0) \ |
| 66 | _ (ssse3, 1, ecx, 9) \ |
| 67 | _ (sse41, 1, ecx, 19) \ |
| 68 | _ (sse42, 1, ecx, 20) \ |
| 69 | _ (avx, 1, ecx, 28) \ |
| 70 | _ (avx2, 7, ebx, 5) \ |
| 71 | _ (avx512f, 7, ebx, 16) \ |
Gabriel Ganne | 73cb006 | 2017-12-05 14:26:33 +0100 | [diff] [blame] | 72 | _ (x86_aes, 1, ecx, 25) \ |
Damjan Marion | c0e939b | 2016-11-12 11:50:01 +0100 | [diff] [blame] | 73 | _ (sha, 7, ebx, 29) \ |
| 74 | _ (invariant_tsc, 0x80000007, edx, 8) |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 75 | |
Gabriel Ganne | 73cb006 | 2017-12-05 14:26:33 +0100 | [diff] [blame] | 76 | |
| 77 | #define foreach_aarch64_flags \ |
| 78 | _ (fp, 0) \ |
| 79 | _ (asimd, 1) \ |
| 80 | _ (evtstrm, 2) \ |
| 81 | _ (aarch64_aes, 3) \ |
| 82 | _ (pmull, 4) \ |
| 83 | _ (sha1, 5) \ |
| 84 | _ (sha2, 6) \ |
| 85 | _ (crc32, 7) \ |
| 86 | _ (atomics, 8) \ |
| 87 | _ (fphp, 9) \ |
| 88 | _ (asimdhp, 10) \ |
| 89 | _ (cpuid, 11) \ |
| 90 | _ (asimdrdm, 12) \ |
| 91 | _ (jscvt, 13) \ |
| 92 | _ (fcma, 14) \ |
| 93 | _ (lrcpc, 15) \ |
| 94 | _ (dcpop, 16) \ |
| 95 | _ (sha3, 17) \ |
| 96 | _ (sm3, 18) \ |
| 97 | _ (sm4, 19) \ |
| 98 | _ (asimddp, 20) \ |
| 99 | _ (sha512, 21) \ |
| 100 | _ (sve, 22) |
| 101 | |
Christophe Fontaine | 33e8195 | 2016-12-19 14:41:52 +0100 | [diff] [blame] | 102 | #if defined(__x86_64__) |
| 103 | #include "cpuid.h" |
| 104 | |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 105 | static inline int |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 106 | 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] | 107 | { |
| 108 | if ((u32) __get_cpuid_max (0x80000000 & lev, 0) < lev) |
| 109 | return 0; |
| 110 | if (lev == 7) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 111 | __cpuid_count (lev, 0, *eax, *ebx, *ecx, *edx); |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 112 | else |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 113 | __cpuid (lev, *eax, *ebx, *ecx, *edx); |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 114 | return 1; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | #define _(flag, func, reg, bit) \ |
| 119 | static inline int \ |
| 120 | clib_cpu_supports_ ## flag() \ |
| 121 | { \ |
| 122 | u32 __attribute__((unused)) eax, ebx = 0, ecx = 0, edx = 0; \ |
| 123 | clib_get_cpuid (func, &eax, &ebx, &ecx, &edx); \ |
| 124 | \ |
| 125 | return ((reg & (1 << bit)) != 0); \ |
| 126 | } |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 127 | foreach_x86_64_flags |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 128 | #undef _ |
Gabriel Ganne | 73cb006 | 2017-12-05 14:26:33 +0100 | [diff] [blame] | 129 | #else /* __x86_64__ */ |
Christophe Fontaine | 33e8195 | 2016-12-19 14:41:52 +0100 | [diff] [blame] | 130 | |
| 131 | #define _(flag, func, reg, bit) \ |
| 132 | static inline int clib_cpu_supports_ ## flag() { return 0; } |
| 133 | foreach_x86_64_flags |
| 134 | #undef _ |
Gabriel Ganne | 73cb006 | 2017-12-05 14:26:33 +0100 | [diff] [blame] | 135 | #endif /* __x86_64__ */ |
| 136 | #if defined(__aarch64__) |
| 137 | #include <sys/auxv.h> |
| 138 | #define _(flag, bit) \ |
| 139 | static inline int \ |
| 140 | clib_cpu_supports_ ## flag() \ |
| 141 | { \ |
| 142 | unsigned long hwcap = getauxval(AT_HWCAP); \ |
| 143 | return (hwcap & (1 << bit)); \ |
| 144 | } |
| 145 | foreach_aarch64_flags |
| 146 | #undef _ |
| 147 | #else /* ! __x86_64__ && !__aarch64__ */ |
| 148 | #define _(flag, bit) \ |
| 149 | static inline int clib_cpu_supports_ ## flag() { return 0; } |
| 150 | foreach_aarch64_flags |
| 151 | #undef _ |
| 152 | #endif /* __x86_64__, __aarch64__ */ |
| 153 | /* |
| 154 | * aes is the only feature with the same name in both flag lists |
| 155 | * handle this by prefixing it with the arch name, and handling it |
| 156 | * with the custom function below |
| 157 | */ |
| 158 | static inline int |
| 159 | clib_cpu_supports_aes () |
| 160 | { |
| 161 | #if defined (__aarch64__) |
| 162 | return clib_cpu_supports_x86_aes (); |
| 163 | #elif defined (__aarch64__) |
| 164 | return clib_cpu_supports_aarch64_aes (); |
| 165 | #else |
| 166 | return 0; |
Christophe Fontaine | 33e8195 | 2016-12-19 14:41:52 +0100 | [diff] [blame] | 167 | #endif |
Gabriel Ganne | 73cb006 | 2017-12-05 14:26:33 +0100 | [diff] [blame] | 168 | } |
| 169 | |
Damjan Marion | 812b32d | 2018-05-28 21:26:47 +0200 | [diff] [blame] | 170 | static inline int |
| 171 | clib_cpu_march_priority_avx512 () |
| 172 | { |
| 173 | if (clib_cpu_supports_avx512f ()) |
| 174 | return 20; |
| 175 | return -1; |
| 176 | } |
| 177 | |
| 178 | static inline int |
| 179 | clib_cpu_march_priority_avx2 () |
| 180 | { |
| 181 | if (clib_cpu_supports_avx2 ()) |
| 182 | return 10; |
| 183 | return -1; |
| 184 | } |
| 185 | |
| 186 | #ifdef CLIB_MARCH_VARIANT |
| 187 | #define CLIB_MARCH_FN_PRIORITY() CLIB_MARCH_SFX(clib_cpu_march_priority)() |
| 188 | #else |
| 189 | #define CLIB_MARCH_FN_PRIORITY() 0 |
| 190 | #endif |
Gabriel Ganne | 73cb006 | 2017-12-05 14:26:33 +0100 | [diff] [blame] | 191 | #endif /* included_clib_cpu_h */ |
| 192 | |
| 193 | format_function_t format_cpu_uarch; |
Damjan Marion | 522e486 | 2016-03-04 12:44:14 +0100 | [diff] [blame] | 194 | format_function_t format_cpu_model_name; |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 195 | format_function_t format_cpu_flags; |
Damjan Marion | 522e486 | 2016-03-04 12:44:14 +0100 | [diff] [blame] | 196 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 197 | /* |
| 198 | * fd.io coding-style-patch-verification: ON |
| 199 | * |
| 200 | * Local Variables: |
| 201 | * eval: (c-set-style "gnu") |
| 202 | * End: |
| 203 | */ |