blob: c1ff7dd27b38f6098d637c419d83f602bc032f10 [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 */
Gabriel Ganneae66b0d2018-01-03 09:18:47 +010015#include <fcntl.h>
Damjan Marion522e4862016-03-04 12:44:14 +010016#include <vppinfra/clib.h>
17#include <vppinfra/format.h>
18#include <vppinfra/cpu.h>
19
Damjan Marion522e4862016-03-04 12:44:14 +010020#define foreach_x86_cpu_uarch \
Damjan Marionbfe470c2017-11-11 02:21:34 +010021 _(0x06, 0x9e, "Kaby Lake", "Kaby Lake DT/H/S/X") \
22 _(0x06, 0x8e, "Kaby Lake", "Kaby Lake Y/U") \
23 _(0x06, 0x85, "Knights Mill", "Knights Mill") \
24 _(0x06, 0x5f, "Goldmont", "Denverton") \
25 _(0x06, 0x5e, "Skylake", "Skylake DT/H/S") \
26 _(0x06, 0x5c, "Goldmont", "Apollo Lake") \
27 _(0x06, 0x5a, "Silvermont", "Moorefield") \
28 _(0x06, 0x57, "Knights Landing", "Knights Landing") \
29 _(0x06, 0x56, "Broadwell", "Broadwell DE") \
30 _(0x06, 0x55, "Skylake", "Skylake X/SP") \
31 _(0x06, 0x4f, "Broadwell", "Broadwell EP/EX") \
32 _(0x06, 0x4e, "Skylake", "Skylake Y/U") \
33 _(0x06, 0x4d, "Silvermont", "Rangeley") \
34 _(0x06, 0x4c, "Airmont", "Braswell") \
35 _(0x06, 0x47, "Broadwell", "Broadwell H") \
36 _(0x06, 0x46, "Haswell", "Crystalwell") \
37 _(0x06, 0x45, "Haswell", "Haswell ULT") \
38 _(0x06, 0x3f, "Haswell", "Haswell E") \
39 _(0x06, 0x3e, "Ivy Bridge", "Ivy Bridge E/EN/EP") \
40 _(0x06, 0x3d, "Broadwell", "Broadwell U") \
Damjan Marion522e4862016-03-04 12:44:14 +010041 _(0x06, 0x3c, "Haswell", "Haswell") \
Damjan Marionbfe470c2017-11-11 02:21:34 +010042 _(0x06, 0x3a, "Ivy Bridge", "IvyBridge") \
43 _(0x06, 0x37, "Silvermont", "BayTrail") \
44 _(0x06, 0x36, "Saltwell", "Cedarview,Centerton") \
45 _(0x06, 0x35, "Saltwell", "Cloverview") \
46 _(0x06, 0x2f, "Westmere", "Westmere EX") \
47 _(0x06, 0x2e, "Nehalem", "Nehalem EX") \
48 _(0x06, 0x2d, "Sandy Bridge", "SandyBridge E/EN/EP") \
49 _(0x06, 0x2c, "Westmere", "Westmere EP/EX,Gulftown") \
50 _(0x06, 0x2a, "Sandy Bridge", "Sandy Bridge") \
51 _(0x06, 0x27, "Saltwell", "Medfield") \
52 _(0x06, 0x26, "Bonnell", "Tunnel Creek") \
Damjan Marion522e4862016-03-04 12:44:14 +010053 _(0x06, 0x25, "Westmere", "Arrandale,Clarksdale") \
Damjan Marion522e4862016-03-04 12:44:14 +010054 _(0x06, 0x1e, "Nehalem", "Clarksfield,Lynnfield,Jasper Forest") \
Damjan Marionbfe470c2017-11-11 02:21:34 +010055 _(0x06, 0x1d, "Penryn", "Dunnington") \
56 _(0x06, 0x1c, "Bonnell", "Pineview,Silverthorne") \
57 _(0x06, 0x1a, "Nehalem", "Nehalem EP,Bloomfield)") \
58 _(0x06, 0x17, "Penryn", "Yorkfield,Wolfdale,Penryn,Harpertown")
Damjan Marion522e4862016-03-04 12:44:14 +010059
Nitin Saxenac9122f92019-03-28 14:42:31 +053060/* _(implementor-id, part-id, vendor-name, cpu-name, show CPU pass as string) */
Gabriel Ganneae66b0d2018-01-03 09:18:47 +010061#define foreach_aarch64_cpu_uarch \
Nitin Saxenac9122f92019-03-28 14:42:31 +053062 _(0x41, 0xd03, "ARM", "Cortex-A53", 0) \
63 _(0x41, 0xd07, "ARM", "Cortex-A57", 0) \
64 _(0x41, 0xd08, "ARM", "Cortex-A72", 0) \
65 _(0x41, 0xd09, "ARM", "Cortex-A73", 0) \
66 _(0x43, 0x0a1, "Marvell", "THUNDERX CN88XX", 0) \
67 _(0x43, 0x0a2, "Marvell", "OCTEON TX CN81XX", 0) \
68 _(0x43, 0x0a3, "Marvell", "OCTEON TX CN83XX", 0) \
69 _(0x43, 0x0af, "Marvell", "THUNDERX2 CN99XX", 1) \
70 _(0x43, 0x0b1, "Marvell", "OCTEON TX2 CN98XX", 1) \
71 _(0x43, 0x0b2, "Marvell", "OCTEON TX2 CN96XX", 1)
Gabriel Ganneae66b0d2018-01-03 09:18:47 +010072
Damjan Marion522e4862016-03-04 12:44:14 +010073u8 *
74format_cpu_uarch (u8 * s, va_list * args)
75{
76#if __x86_64__
Dave Barachc3799992016-08-15 11:12:27 -040077 u32 __attribute__ ((unused)) eax, ebx, ecx, edx;
Paul Vinciguerrad6897c12018-12-30 11:07:36 -080078 u8 model, family, stepping;
Damjan Marion522e4862016-03-04 12:44:14 +010079
80 if (__get_cpuid (1, &eax, &ebx, &ecx, &edx) == 0)
81 return format (s, "unknown (missing cpuid)");
82
83 model = ((eax >> 4) & 0x0f) | ((eax >> 12) & 0xf0);
84 family = (eax >> 8) & 0x0f;
Paul Vinciguerrad6897c12018-12-30 11:07:36 -080085 stepping = eax & 0x0f;
Damjan Marion522e4862016-03-04 12:44:14 +010086
Paul Vinciguerrad6897c12018-12-30 11:07:36 -080087#define _(f,m,a,c) if ((model == m) && (family == f)) return \
88format(s, "[0x%x] %s ([0x%02x] %s) stepping 0x%x", f, a, m, c, stepping);
Damjan Marion522e4862016-03-04 12:44:14 +010089 foreach_x86_cpu_uarch
90#undef _
Dave Barachc3799992016-08-15 11:12:27 -040091 return format (s, "unknown (family 0x%02x model 0x%02x)", family, model);
Damjan Marion522e4862016-03-04 12:44:14 +010092
Gabriel Ganneae66b0d2018-01-03 09:18:47 +010093#elif __aarch64__
94 int fd;
95 unformat_input_t input;
96 u32 implementer, primary_part_number, variant, revision;
97
98 fd = open ("/proc/cpuinfo", 0);
99 if (fd < 0)
100 return format (s, "unknown");
101
102 unformat_init_clib_file (&input, fd);
103 while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
104 {
105 if (unformat (&input, "CPU implementer%_: 0x%x", &implementer))
106 ;
107 else if (unformat (&input, "CPU part%_: 0x%x", &primary_part_number))
108 ;
109 else if (unformat (&input, "CPU variant%_: 0x%x", &variant))
110 ;
111 else if (unformat (&input, "CPU revision%_: %u", &revision))
112 ;
113 else
114 unformat_skip_line (&input);
115 }
116 unformat_free (&input);
117 close (fd);
118
Nitin Saxenac9122f92019-03-28 14:42:31 +0530119#define _(i,p,a,c,_format) if ((implementer == i) && (primary_part_number == p)){ \
120 if (_format)\
121 return format(s, "%s (%s PASS %c%u)", a, c, 'A'+variant, revision);\
122 else {\
123 if (implementer == 0x43)\
124 variant++; \
125 return format (s, "%s (%s PASS %u.%u)", a, c, variant, revision);}}
Gabriel Ganneae66b0d2018-01-03 09:18:47 +0100126
Gabriel Ganneae66b0d2018-01-03 09:18:47 +0100127 foreach_aarch64_cpu_uarch
128#undef _
129 return format (s, "unknown (implementer 0x%02x part 0x%03x PASS %u.%u)",
130 implementer, primary_part_number, variant, revision);
131
Damjan Marion522e4862016-03-04 12:44:14 +0100132#else /* ! __x86_64__ */
133 return format (s, "unknown");
134#endif
135}
136
137u8 *
138format_cpu_model_name (u8 * s, va_list * args)
139{
140#if __x86_64__
Dave Barachc3799992016-08-15 11:12:27 -0400141 u32 __attribute__ ((unused)) eax, ebx, ecx, edx;
142 u8 *name = 0;
143 u32 *name_u32;
Damjan Marion522e4862016-03-04 12:44:14 +0100144
145 if (__get_cpuid (1, &eax, &ebx, &ecx, &edx) == 0)
146 return format (s, "unknown (missing cpuid)");
147
148 __get_cpuid (0x80000000, &eax, &ebx, &ecx, &edx);
149 if (eax < 0x80000004)
150 return format (s, "unknown (missing ext feature)");
151
Dave Barachc3799992016-08-15 11:12:27 -0400152 vec_validate (name, 48);
Damjan Marion522e4862016-03-04 12:44:14 +0100153 name_u32 = (u32 *) name;
154
155 __get_cpuid (0x80000002, &eax, &ebx, &ecx, &edx);
156 name_u32[0] = eax;
157 name_u32[1] = ebx;
158 name_u32[2] = ecx;
159 name_u32[3] = edx;
160
161 __get_cpuid (0x80000003, &eax, &ebx, &ecx, &edx);
162 name_u32[4] = eax;
163 name_u32[5] = ebx;
164 name_u32[6] = ecx;
165 name_u32[7] = edx;
166
167 __get_cpuid (0x80000004, &eax, &ebx, &ecx, &edx);
168 name_u32[8] = eax;
169 name_u32[9] = ebx;
170 name_u32[10] = ecx;
171 name_u32[11] = edx;
172
173 s = format (s, "%s", name);
Dave Barachc3799992016-08-15 11:12:27 -0400174 vec_free (name);
Damjan Marion522e4862016-03-04 12:44:14 +0100175 return s;
176
Christophe Fontaine33e81952016-12-19 14:41:52 +0100177#elif defined(__aarch64__)
178 return format (s, "armv8");
Damjan Marion522e4862016-03-04 12:44:14 +0100179#else /* ! __x86_64__ */
180 return format (s, "unknown");
181#endif
Damjan Marion1c80e832016-05-11 23:07:18 +0200182}
183
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100184
185static inline char const *
186flag_skip_prefix (char const *flag)
187{
188 if (memcmp (flag, "x86_", sizeof ("x86_") - 1) == 0)
189 return flag + sizeof ("x86_") - 1;
190 if (memcmp (flag, "aarch64_", sizeof ("aarch64_") - 1) == 0)
191 return flag + sizeof ("aarch64_") - 1;
192 return flag;
193}
194
Damjan Marion1c80e832016-05-11 23:07:18 +0200195u8 *
196format_cpu_flags (u8 * s, va_list * args)
197{
Christophe Fontaine33e81952016-12-19 14:41:52 +0100198#if defined(__x86_64__)
Damjan Marion1c80e832016-05-11 23:07:18 +0200199#define _(flag, func, reg, bit) \
200 if (clib_cpu_supports_ ## flag()) \
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100201 s = format (s, "%s ", flag_skip_prefix(#flag));
Dave Barachc3799992016-08-15 11:12:27 -0400202 foreach_x86_64_flags return s;
Damjan Marion1c80e832016-05-11 23:07:18 +0200203#undef _
Gabriel Ganne73cb0062017-12-05 14:26:33 +0100204#elif defined(__aarch64__)
205#define _(flag, bit) \
206 if (clib_cpu_supports_ ## flag()) \
207 s = format (s, "%s ", flag_skip_prefix(#flag));
208 foreach_aarch64_flags return s;
209#undef _
210#else /* ! ! __x86_64__ && ! __aarch64__ */
Damjan Marion1c80e832016-05-11 23:07:18 +0200211 return format (s, "unknown");
212#endif
213}
214
215
Dave Barachc3799992016-08-15 11:12:27 -0400216
217/*
218 * fd.io coding-style-patch-verification: ON
219 *
220 * Local Variables:
221 * eval: (c-set-style "gnu")
222 * End:
223 */