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 | */ |
Gabriel Ganne | ae66b0d | 2018-01-03 09:18:47 +0100 | [diff] [blame] | 15 | #include <fcntl.h> |
Damjan Marion | 522e486 | 2016-03-04 12:44:14 +0100 | [diff] [blame] | 16 | #include <vppinfra/clib.h> |
| 17 | #include <vppinfra/format.h> |
| 18 | #include <vppinfra/cpu.h> |
| 19 | |
Damjan Marion | 522e486 | 2016-03-04 12:44:14 +0100 | [diff] [blame] | 20 | #define foreach_x86_cpu_uarch \ |
Damjan Marion | bfe470c | 2017-11-11 02:21:34 +0100 | [diff] [blame] | 21 | _(0x06, 0x9e, "Kaby Lake", "Kaby Lake DT/H/S/X") \ |
| 22 | _(0x06, 0x8e, "Kaby Lake", "Kaby Lake Y/U") \ |
Damjan Marion | 38e0413 | 2019-12-18 16:09:48 +0100 | [diff] [blame] | 23 | _(0x06, 0x8c, "Tiger Lake", "Tiger Lake U") \ |
| 24 | _(0x06, 0x86, "Tremont", "Elkhart Lake") \ |
Damjan Marion | bfe470c | 2017-11-11 02:21:34 +0100 | [diff] [blame] | 25 | _(0x06, 0x85, "Knights Mill", "Knights Mill") \ |
Damjan Marion | 38e0413 | 2019-12-18 16:09:48 +0100 | [diff] [blame] | 26 | _(0x06, 0x7e, "Ice Lake", "Ice Lake U") \ |
| 27 | _(0x06, 0x7d, "Ice Lake", "Ice Lake Y") \ |
| 28 | _(0x06, 0x7a, "Goldmont Plus", "Gemini Lake") \ |
| 29 | _(0x06, 0x6c, "Ice Lake", "Ice Lake SP") \ |
| 30 | _(0x06, 0x6a, "Ice Lake", "Ice Lake DE") \ |
| 31 | _(0x06, 0x66, "Cannon Lake", "Cannon Lake U") \ |
Damjan Marion | bfe470c | 2017-11-11 02:21:34 +0100 | [diff] [blame] | 32 | _(0x06, 0x5f, "Goldmont", "Denverton") \ |
| 33 | _(0x06, 0x5e, "Skylake", "Skylake DT/H/S") \ |
| 34 | _(0x06, 0x5c, "Goldmont", "Apollo Lake") \ |
| 35 | _(0x06, 0x5a, "Silvermont", "Moorefield") \ |
| 36 | _(0x06, 0x57, "Knights Landing", "Knights Landing") \ |
| 37 | _(0x06, 0x56, "Broadwell", "Broadwell DE") \ |
| 38 | _(0x06, 0x55, "Skylake", "Skylake X/SP") \ |
| 39 | _(0x06, 0x4f, "Broadwell", "Broadwell EP/EX") \ |
| 40 | _(0x06, 0x4e, "Skylake", "Skylake Y/U") \ |
| 41 | _(0x06, 0x4d, "Silvermont", "Rangeley") \ |
| 42 | _(0x06, 0x4c, "Airmont", "Braswell") \ |
| 43 | _(0x06, 0x47, "Broadwell", "Broadwell H") \ |
| 44 | _(0x06, 0x46, "Haswell", "Crystalwell") \ |
| 45 | _(0x06, 0x45, "Haswell", "Haswell ULT") \ |
| 46 | _(0x06, 0x3f, "Haswell", "Haswell E") \ |
| 47 | _(0x06, 0x3e, "Ivy Bridge", "Ivy Bridge E/EN/EP") \ |
| 48 | _(0x06, 0x3d, "Broadwell", "Broadwell U") \ |
Damjan Marion | 522e486 | 2016-03-04 12:44:14 +0100 | [diff] [blame] | 49 | _(0x06, 0x3c, "Haswell", "Haswell") \ |
Damjan Marion | bfe470c | 2017-11-11 02:21:34 +0100 | [diff] [blame] | 50 | _(0x06, 0x3a, "Ivy Bridge", "IvyBridge") \ |
| 51 | _(0x06, 0x37, "Silvermont", "BayTrail") \ |
| 52 | _(0x06, 0x36, "Saltwell", "Cedarview,Centerton") \ |
| 53 | _(0x06, 0x35, "Saltwell", "Cloverview") \ |
| 54 | _(0x06, 0x2f, "Westmere", "Westmere EX") \ |
| 55 | _(0x06, 0x2e, "Nehalem", "Nehalem EX") \ |
| 56 | _(0x06, 0x2d, "Sandy Bridge", "SandyBridge E/EN/EP") \ |
| 57 | _(0x06, 0x2c, "Westmere", "Westmere EP/EX,Gulftown") \ |
| 58 | _(0x06, 0x2a, "Sandy Bridge", "Sandy Bridge") \ |
| 59 | _(0x06, 0x27, "Saltwell", "Medfield") \ |
| 60 | _(0x06, 0x26, "Bonnell", "Tunnel Creek") \ |
Damjan Marion | 522e486 | 2016-03-04 12:44:14 +0100 | [diff] [blame] | 61 | _(0x06, 0x25, "Westmere", "Arrandale,Clarksdale") \ |
Damjan Marion | 522e486 | 2016-03-04 12:44:14 +0100 | [diff] [blame] | 62 | _(0x06, 0x1e, "Nehalem", "Clarksfield,Lynnfield,Jasper Forest") \ |
Damjan Marion | bfe470c | 2017-11-11 02:21:34 +0100 | [diff] [blame] | 63 | _(0x06, 0x1d, "Penryn", "Dunnington") \ |
| 64 | _(0x06, 0x1c, "Bonnell", "Pineview,Silverthorne") \ |
| 65 | _(0x06, 0x1a, "Nehalem", "Nehalem EP,Bloomfield)") \ |
| 66 | _(0x06, 0x17, "Penryn", "Yorkfield,Wolfdale,Penryn,Harpertown") |
Damjan Marion | 522e486 | 2016-03-04 12:44:14 +0100 | [diff] [blame] | 67 | |
Nitin Saxena | c9122f9 | 2019-03-28 14:42:31 +0530 | [diff] [blame] | 68 | /* _(implementor-id, part-id, vendor-name, cpu-name, show CPU pass as string) */ |
Gabriel Ganne | ae66b0d | 2018-01-03 09:18:47 +0100 | [diff] [blame] | 69 | #define foreach_aarch64_cpu_uarch \ |
Nitin Saxena | c9122f9 | 2019-03-28 14:42:31 +0530 | [diff] [blame] | 70 | _(0x41, 0xd03, "ARM", "Cortex-A53", 0) \ |
| 71 | _(0x41, 0xd07, "ARM", "Cortex-A57", 0) \ |
| 72 | _(0x41, 0xd08, "ARM", "Cortex-A72", 0) \ |
| 73 | _(0x41, 0xd09, "ARM", "Cortex-A73", 0) \ |
| 74 | _(0x43, 0x0a1, "Marvell", "THUNDERX CN88XX", 0) \ |
| 75 | _(0x43, 0x0a2, "Marvell", "OCTEON TX CN81XX", 0) \ |
| 76 | _(0x43, 0x0a3, "Marvell", "OCTEON TX CN83XX", 0) \ |
| 77 | _(0x43, 0x0af, "Marvell", "THUNDERX2 CN99XX", 1) \ |
| 78 | _(0x43, 0x0b1, "Marvell", "OCTEON TX2 CN98XX", 1) \ |
| 79 | _(0x43, 0x0b2, "Marvell", "OCTEON TX2 CN96XX", 1) |
Gabriel Ganne | ae66b0d | 2018-01-03 09:18:47 +0100 | [diff] [blame] | 80 | |
Damjan Marion | 522e486 | 2016-03-04 12:44:14 +0100 | [diff] [blame] | 81 | u8 * |
| 82 | format_cpu_uarch (u8 * s, va_list * args) |
| 83 | { |
| 84 | #if __x86_64__ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 85 | u32 __attribute__ ((unused)) eax, ebx, ecx, edx; |
Paul Vinciguerra | d6897c1 | 2018-12-30 11:07:36 -0800 | [diff] [blame] | 86 | u8 model, family, stepping; |
Damjan Marion | 522e486 | 2016-03-04 12:44:14 +0100 | [diff] [blame] | 87 | |
| 88 | if (__get_cpuid (1, &eax, &ebx, &ecx, &edx) == 0) |
| 89 | return format (s, "unknown (missing cpuid)"); |
| 90 | |
| 91 | model = ((eax >> 4) & 0x0f) | ((eax >> 12) & 0xf0); |
| 92 | family = (eax >> 8) & 0x0f; |
Paul Vinciguerra | d6897c1 | 2018-12-30 11:07:36 -0800 | [diff] [blame] | 93 | stepping = eax & 0x0f; |
Damjan Marion | 522e486 | 2016-03-04 12:44:14 +0100 | [diff] [blame] | 94 | |
Paul Vinciguerra | d6897c1 | 2018-12-30 11:07:36 -0800 | [diff] [blame] | 95 | #define _(f,m,a,c) if ((model == m) && (family == f)) return \ |
| 96 | format(s, "[0x%x] %s ([0x%02x] %s) stepping 0x%x", f, a, m, c, stepping); |
Damjan Marion | 522e486 | 2016-03-04 12:44:14 +0100 | [diff] [blame] | 97 | foreach_x86_cpu_uarch |
| 98 | #undef _ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 99 | return format (s, "unknown (family 0x%02x model 0x%02x)", family, model); |
Damjan Marion | 522e486 | 2016-03-04 12:44:14 +0100 | [diff] [blame] | 100 | |
Gabriel Ganne | ae66b0d | 2018-01-03 09:18:47 +0100 | [diff] [blame] | 101 | #elif __aarch64__ |
| 102 | int fd; |
| 103 | unformat_input_t input; |
| 104 | u32 implementer, primary_part_number, variant, revision; |
| 105 | |
| 106 | fd = open ("/proc/cpuinfo", 0); |
| 107 | if (fd < 0) |
| 108 | return format (s, "unknown"); |
| 109 | |
| 110 | unformat_init_clib_file (&input, fd); |
| 111 | while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT) |
| 112 | { |
| 113 | if (unformat (&input, "CPU implementer%_: 0x%x", &implementer)) |
| 114 | ; |
| 115 | else if (unformat (&input, "CPU part%_: 0x%x", &primary_part_number)) |
| 116 | ; |
| 117 | else if (unformat (&input, "CPU variant%_: 0x%x", &variant)) |
| 118 | ; |
| 119 | else if (unformat (&input, "CPU revision%_: %u", &revision)) |
| 120 | ; |
| 121 | else |
| 122 | unformat_skip_line (&input); |
| 123 | } |
| 124 | unformat_free (&input); |
| 125 | close (fd); |
| 126 | |
Nitin Saxena | c9122f9 | 2019-03-28 14:42:31 +0530 | [diff] [blame] | 127 | #define _(i,p,a,c,_format) if ((implementer == i) && (primary_part_number == p)){ \ |
| 128 | if (_format)\ |
| 129 | return format(s, "%s (%s PASS %c%u)", a, c, 'A'+variant, revision);\ |
| 130 | else {\ |
| 131 | if (implementer == 0x43)\ |
| 132 | variant++; \ |
| 133 | return format (s, "%s (%s PASS %u.%u)", a, c, variant, revision);}} |
Gabriel Ganne | ae66b0d | 2018-01-03 09:18:47 +0100 | [diff] [blame] | 134 | |
Gabriel Ganne | ae66b0d | 2018-01-03 09:18:47 +0100 | [diff] [blame] | 135 | foreach_aarch64_cpu_uarch |
| 136 | #undef _ |
| 137 | return format (s, "unknown (implementer 0x%02x part 0x%03x PASS %u.%u)", |
| 138 | implementer, primary_part_number, variant, revision); |
| 139 | |
Damjan Marion | 522e486 | 2016-03-04 12:44:14 +0100 | [diff] [blame] | 140 | #else /* ! __x86_64__ */ |
| 141 | return format (s, "unknown"); |
| 142 | #endif |
| 143 | } |
| 144 | |
| 145 | u8 * |
| 146 | format_cpu_model_name (u8 * s, va_list * args) |
| 147 | { |
| 148 | #if __x86_64__ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 149 | u32 __attribute__ ((unused)) eax, ebx, ecx, edx; |
| 150 | u8 *name = 0; |
| 151 | u32 *name_u32; |
Damjan Marion | 522e486 | 2016-03-04 12:44:14 +0100 | [diff] [blame] | 152 | |
| 153 | if (__get_cpuid (1, &eax, &ebx, &ecx, &edx) == 0) |
| 154 | return format (s, "unknown (missing cpuid)"); |
| 155 | |
| 156 | __get_cpuid (0x80000000, &eax, &ebx, &ecx, &edx); |
| 157 | if (eax < 0x80000004) |
| 158 | return format (s, "unknown (missing ext feature)"); |
| 159 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 160 | vec_validate (name, 48); |
Damjan Marion | 522e486 | 2016-03-04 12:44:14 +0100 | [diff] [blame] | 161 | name_u32 = (u32 *) name; |
| 162 | |
| 163 | __get_cpuid (0x80000002, &eax, &ebx, &ecx, &edx); |
| 164 | name_u32[0] = eax; |
| 165 | name_u32[1] = ebx; |
| 166 | name_u32[2] = ecx; |
| 167 | name_u32[3] = edx; |
| 168 | |
| 169 | __get_cpuid (0x80000003, &eax, &ebx, &ecx, &edx); |
| 170 | name_u32[4] = eax; |
| 171 | name_u32[5] = ebx; |
| 172 | name_u32[6] = ecx; |
| 173 | name_u32[7] = edx; |
| 174 | |
| 175 | __get_cpuid (0x80000004, &eax, &ebx, &ecx, &edx); |
| 176 | name_u32[8] = eax; |
| 177 | name_u32[9] = ebx; |
| 178 | name_u32[10] = ecx; |
| 179 | name_u32[11] = edx; |
| 180 | |
| 181 | s = format (s, "%s", name); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 182 | vec_free (name); |
Damjan Marion | 522e486 | 2016-03-04 12:44:14 +0100 | [diff] [blame] | 183 | return s; |
| 184 | |
Christophe Fontaine | 33e8195 | 2016-12-19 14:41:52 +0100 | [diff] [blame] | 185 | #elif defined(__aarch64__) |
| 186 | return format (s, "armv8"); |
Damjan Marion | 522e486 | 2016-03-04 12:44:14 +0100 | [diff] [blame] | 187 | #else /* ! __x86_64__ */ |
| 188 | return format (s, "unknown"); |
| 189 | #endif |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 190 | } |
| 191 | |
Gabriel Ganne | 73cb006 | 2017-12-05 14:26:33 +0100 | [diff] [blame] | 192 | |
| 193 | static inline char const * |
Benoît Ganne | a7cb357 | 2019-12-16 16:00:14 +0100 | [diff] [blame] | 194 | flag_skip_prefix (char const *flag, const char *pfx, int len) |
Gabriel Ganne | 73cb006 | 2017-12-05 14:26:33 +0100 | [diff] [blame] | 195 | { |
Benoît Ganne | a7cb357 | 2019-12-16 16:00:14 +0100 | [diff] [blame] | 196 | if (0 == strncmp (flag, pfx, len - 1)) |
| 197 | return flag + len - 1; |
Gabriel Ganne | 73cb006 | 2017-12-05 14:26:33 +0100 | [diff] [blame] | 198 | return flag; |
| 199 | } |
| 200 | |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 201 | u8 * |
| 202 | format_cpu_flags (u8 * s, va_list * args) |
| 203 | { |
Christophe Fontaine | 33e8195 | 2016-12-19 14:41:52 +0100 | [diff] [blame] | 204 | #if defined(__x86_64__) |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 205 | #define _(flag, func, reg, bit) \ |
| 206 | if (clib_cpu_supports_ ## flag()) \ |
Benoît Ganne | a7cb357 | 2019-12-16 16:00:14 +0100 | [diff] [blame] | 207 | s = format (s, "%s ", flag_skip_prefix(#flag, "x86_", sizeof("x86_"))); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 208 | foreach_x86_64_flags return s; |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 209 | #undef _ |
Gabriel Ganne | 73cb006 | 2017-12-05 14:26:33 +0100 | [diff] [blame] | 210 | #elif defined(__aarch64__) |
| 211 | #define _(flag, bit) \ |
| 212 | if (clib_cpu_supports_ ## flag()) \ |
Benoît Ganne | a7cb357 | 2019-12-16 16:00:14 +0100 | [diff] [blame] | 213 | s = format (s, "%s ", flag_skip_prefix(#flag, "aarch64_", sizeof("aarch64_"))); |
Gabriel Ganne | 73cb006 | 2017-12-05 14:26:33 +0100 | [diff] [blame] | 214 | foreach_aarch64_flags return s; |
| 215 | #undef _ |
| 216 | #else /* ! ! __x86_64__ && ! __aarch64__ */ |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 217 | return format (s, "unknown"); |
| 218 | #endif |
| 219 | } |
| 220 | |
| 221 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 222 | |
| 223 | /* |
| 224 | * fd.io coding-style-patch-verification: ON |
| 225 | * |
| 226 | * Local Variables: |
| 227 | * eval: (c-set-style "gnu") |
| 228 | * End: |
| 229 | */ |