Branch data Line data Source code
1 : : /* Get CPU type and Features for x86 processors.
2 : : Copyright (C) 2012-2025 Free Software Foundation, Inc.
3 : : Contributed by Sriraman Tallam (tmsriram@google.com)
4 : :
5 : : This file is part of GCC.
6 : :
7 : : GCC is free software; you can redistribute it and/or modify it under
8 : : the terms of the GNU General Public License as published by the Free
9 : : Software Foundation; either version 3, or (at your option) any later
10 : : version.
11 : :
12 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : : for more details.
16 : :
17 : : Under Section 7 of GPL version 3, you are granted additional
18 : : permissions described in the GCC Runtime Library Exception, version
19 : : 3.1, as published by the Free Software Foundation.
20 : :
21 : : You should have received a copy of the GNU General Public License and
22 : : a copy of the GCC Runtime Library Exception along with this program;
23 : : see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 : : <http://www.gnu.org/licenses/>. */
25 : :
26 : : struct __processor_model
27 : : {
28 : : unsigned int __cpu_vendor;
29 : : unsigned int __cpu_type;
30 : : unsigned int __cpu_subtype;
31 : : /* The first 32 features are stored as bitmasks in __cpu_features.
32 : : The rest of features are stored as bitmasks in a separate array
33 : : of unsigned int. */
34 : : unsigned int __cpu_features[1];
35 : : };
36 : :
37 : : struct __processor_model2
38 : : {
39 : : unsigned int __cpu_family;
40 : : unsigned int __cpu_model;
41 : : unsigned int __cpu_max_level;
42 : : unsigned int __cpu_ext_level;
43 : : };
44 : :
45 : : #ifndef CHECK___builtin_cpu_is
46 : : # define CHECK___builtin_cpu_is(cpu)
47 : : #endif
48 : :
49 : : #ifndef CHECK___builtin_cpu_supports
50 : : # define CHECK___builtin_cpu_supports(isa)
51 : : #endif
52 : :
53 : : /* Return non-zero if the processor has feature F. */
54 : :
55 : : static inline int
56 : 1573 : has_cpu_feature (struct __processor_model *cpu_model,
57 : : unsigned int *cpu_features2,
58 : : enum processor_features feature)
59 : : {
60 : 1573 : unsigned index, offset;
61 : 1573 : unsigned f = feature;
62 : :
63 : 1155 : if (f < 32)
64 : : {
65 : : /* The first 32 features. */
66 : 319 : return cpu_model->__cpu_features[0] & (1U << f);
67 : : }
68 : : else
69 : : {
70 : : /* The rest of features. cpu_features2[i] contains features from
71 : : (32 + i * 32) to (31 + 32 + i * 32), inclusively. */
72 : 1078 : f -= 32;
73 : 1078 : index = f / 32;
74 : 1078 : offset = f % 32;
75 : 924 : return cpu_features2[index] & (1U << offset);
76 : : }
77 : : }
78 : :
79 : : /* Save FEATURE to either CPU_MODEL or CPU_FEATURES2. */
80 : :
81 : : static inline void
82 : 1034 : set_cpu_feature (struct __processor_model *cpu_model,
83 : : unsigned int *cpu_features2,
84 : : enum processor_features feature)
85 : : {
86 : 1034 : unsigned index, offset;
87 : 1034 : unsigned f = feature;
88 : :
89 : 1034 : if (f < 32)
90 : : {
91 : : /* The first 32 features. */
92 : 396 : cpu_model->__cpu_features[0] |= (1U << f);
93 : : }
94 : : else
95 : : {
96 : : /* The rest of features. cpu_features2[i] contains features from
97 : : (32 + i * 32) to (31 + 32 + i * 32), inclusively. */
98 : 638 : f -= 32;
99 : 638 : index = f / 32;
100 : 638 : offset = f % 32;
101 : 638 : cpu_features2[index] |= (1U << offset);
102 : : }
103 : 968 : }
104 : :
105 : : /* Drop FEATURE from either CPU_MODEL or CPU_FEATURES2. */
106 : :
107 : : static inline void
108 : 0 : reset_cpu_feature (struct __processor_model *cpu_model,
109 : : unsigned int *cpu_features2,
110 : : enum processor_features feature)
111 : : {
112 : 0 : unsigned index, offset;
113 : 0 : unsigned f = feature;
114 : :
115 : 0 : if (f < 32)
116 : : {
117 : : /* The first 32 features. */
118 : 0 : cpu_model->__cpu_features[0] &= ~(1U << f);
119 : : }
120 : : else
121 : : {
122 : : /* The rest of features. cpu_features2[i] contains features from
123 : : (32 + i * 32) to (31 + 32 + i * 32), inclusively. */
124 : 0 : f -= 32;
125 : 0 : index = f / 32;
126 : 0 : offset = f % 32;
127 : 0 : cpu_features2[index] &= ~(1U << offset);
128 : : }
129 : : }
130 : :
131 : : /* Get the specific type of AMD CPU and return AMD CPU name. Return
132 : : NULL for unknown AMD CPU. */
133 : :
134 : : static inline const char *
135 : 22 : get_amd_cpu (struct __processor_model *cpu_model,
136 : : struct __processor_model2 *cpu_model2,
137 : : unsigned int *cpu_features2)
138 : : {
139 : 22 : const char *cpu = NULL;
140 : 22 : unsigned int family = cpu_model2->__cpu_family;
141 : 22 : unsigned int model = cpu_model2->__cpu_model;
142 : :
143 : 22 : switch (family)
144 : : {
145 : 0 : case 0x10:
146 : : /* AMD Family 10h. */
147 : 0 : cpu = "amdfam10";
148 : 0 : cpu_model->__cpu_type = AMDFAM10H;
149 : 0 : switch (model)
150 : : {
151 : 0 : case 0x2:
152 : : /* Barcelona. */
153 : 0 : CHECK___builtin_cpu_is ("amdfam10h");
154 : 0 : CHECK___builtin_cpu_is ("barcelona");
155 : 0 : cpu_model->__cpu_subtype = AMDFAM10H_BARCELONA;
156 : 0 : break;
157 : 0 : case 0x4:
158 : : /* Shanghai. */
159 : 0 : CHECK___builtin_cpu_is ("amdfam10h");
160 : 0 : CHECK___builtin_cpu_is ("shanghai");
161 : 0 : cpu_model->__cpu_subtype = AMDFAM10H_SHANGHAI;
162 : 0 : break;
163 : 0 : case 0x8:
164 : : /* Istanbul. */
165 : 0 : CHECK___builtin_cpu_is ("amdfam10h");
166 : 0 : CHECK___builtin_cpu_is ("istanbul");
167 : 0 : cpu_model->__cpu_subtype = AMDFAM10H_ISTANBUL;
168 : 0 : break;
169 : : default:
170 : : break;
171 : : }
172 : : break;
173 : 0 : case 0x14:
174 : : /* AMD Family 14h "btver1". */
175 : 0 : cpu = "btver1";
176 : 0 : CHECK___builtin_cpu_is ("btver1");
177 : 0 : cpu_model->__cpu_type = AMD_BTVER1;
178 : 0 : break;
179 : 0 : case 0x15:
180 : : /* AMD Family 15h "Bulldozer". */
181 : 0 : cpu_model->__cpu_type = AMDFAM15H;
182 : 0 : if (model == 0x2)
183 : : {
184 : : /* Bulldozer version 2 "Piledriver" */
185 : 0 : cpu = "bdver2";
186 : 0 : CHECK___builtin_cpu_is ("bdver2");
187 : 0 : cpu_model->__cpu_subtype = AMDFAM15H_BDVER2;
188 : : }
189 : 0 : else if (model <= 0xf)
190 : : {
191 : : /* Bulldozer version 1. */
192 : 0 : cpu = "bdver1";
193 : 0 : CHECK___builtin_cpu_is ("bdver1");
194 : 0 : cpu_model->__cpu_subtype = AMDFAM15H_BDVER1;
195 : : }
196 : 0 : else if (model <= 0x2f)
197 : : {
198 : : /* Bulldozer version 2 "Piledriver" */
199 : 0 : cpu = "bdver2";
200 : 0 : CHECK___builtin_cpu_is ("bdver2");
201 : 0 : cpu_model->__cpu_subtype = AMDFAM15H_BDVER2;
202 : : }
203 : 0 : else if (model <= 0x4f)
204 : : {
205 : : /* Bulldozer version 3 "Steamroller" */
206 : 0 : cpu = "bdver3";
207 : 0 : CHECK___builtin_cpu_is ("bdver3");
208 : 0 : cpu_model->__cpu_subtype = AMDFAM15H_BDVER3;
209 : : }
210 : 0 : else if (model <= 0x7f)
211 : : {
212 : : /* Bulldozer version 4 "Excavator" */
213 : 0 : cpu = "bdver4";
214 : 0 : CHECK___builtin_cpu_is ("bdver4");
215 : 0 : cpu_model->__cpu_subtype = AMDFAM15H_BDVER4;
216 : : }
217 : 0 : else if (has_cpu_feature (cpu_model, cpu_features2,
218 : : FEATURE_AVX2))
219 : : {
220 : 0 : cpu = "bdver4";
221 : 0 : CHECK___builtin_cpu_is ("bdver4");
222 : 0 : cpu_model->__cpu_subtype = AMDFAM15H_BDVER4;
223 : : }
224 : 0 : else if (has_cpu_feature (cpu_model, cpu_features2,
225 : : FEATURE_XSAVEOPT))
226 : : {
227 : 0 : cpu = "bdver3";
228 : 0 : CHECK___builtin_cpu_is ("bdver3");
229 : 0 : cpu_model->__cpu_subtype = AMDFAM15H_BDVER3;
230 : : }
231 : 0 : else if (has_cpu_feature (cpu_model, cpu_features2,
232 : : FEATURE_BMI))
233 : : {
234 : 0 : cpu = "bdver2";
235 : 0 : CHECK___builtin_cpu_is ("bdver2");
236 : 0 : cpu_model->__cpu_subtype = AMDFAM15H_BDVER2;
237 : : }
238 : 0 : else if (has_cpu_feature (cpu_model, cpu_features2,
239 : : FEATURE_XOP))
240 : : {
241 : 0 : cpu = "bdver1";
242 : 0 : CHECK___builtin_cpu_is ("bdver1");
243 : 0 : cpu_model->__cpu_subtype = AMDFAM15H_BDVER1;
244 : : }
245 : : break;
246 : 0 : case 0x16:
247 : : /* AMD Family 16h "btver2" */
248 : 0 : cpu = "btver2";
249 : 0 : CHECK___builtin_cpu_is ("btver2");
250 : 0 : cpu_model->__cpu_type = AMD_BTVER2;
251 : 0 : break;
252 : 22 : case 0x17:
253 : 22 : cpu_model->__cpu_type = AMDFAM17H;
254 : 22 : if (model <= 0x1f)
255 : : {
256 : : /* AMD family 17h version 1. */
257 : 0 : cpu = "znver1";
258 : 0 : CHECK___builtin_cpu_is ("znver1");
259 : 0 : cpu_model->__cpu_subtype = AMDFAM17H_ZNVER1;
260 : : }
261 : 22 : else if (model >= 0x30)
262 : : {
263 : 22 : cpu = "znver2";
264 : 22 : CHECK___builtin_cpu_is ("znver2");
265 : 22 : cpu_model->__cpu_subtype = AMDFAM17H_ZNVER2;
266 : : }
267 : 0 : else if (has_cpu_feature (cpu_model, cpu_features2,
268 : : FEATURE_CLWB))
269 : : {
270 : 0 : cpu = "znver2";
271 : 0 : CHECK___builtin_cpu_is ("znver2");
272 : 0 : cpu_model->__cpu_subtype = AMDFAM17H_ZNVER2;
273 : : }
274 : 0 : else if (has_cpu_feature (cpu_model, cpu_features2,
275 : : FEATURE_CLZERO))
276 : : {
277 : 0 : cpu = "znver1";
278 : 0 : CHECK___builtin_cpu_is ("znver1");
279 : 0 : cpu_model->__cpu_subtype = AMDFAM17H_ZNVER1;
280 : : }
281 : : break;
282 : 0 : case 0x19:
283 : 0 : cpu_model->__cpu_type = AMDFAM19H;
284 : : /* AMD family 19h. */
285 : 0 : if (model <= 0x0f)
286 : : {
287 : 0 : cpu = "znver3";
288 : 0 : CHECK___builtin_cpu_is ("znver3");
289 : 0 : cpu_model->__cpu_subtype = AMDFAM19H_ZNVER3;
290 : : }
291 : 0 : else if ((model >= 0x10 && model <= 0x1f)
292 : 0 : || (model >= 0x60 && model <= 0xaf))
293 : : {
294 : 0 : cpu = "znver4";
295 : 0 : CHECK___builtin_cpu_is ("znver4");
296 : 0 : cpu_model->__cpu_subtype = AMDFAM19H_ZNVER4;
297 : : }
298 : 0 : else if (has_cpu_feature (cpu_model, cpu_features2,
299 : : FEATURE_AVX512F))
300 : : {
301 : 0 : cpu = "znver4";
302 : 0 : CHECK___builtin_cpu_is ("znver4");
303 : 0 : cpu_model->__cpu_subtype = AMDFAM19H_ZNVER4;
304 : : }
305 : 0 : else if (has_cpu_feature (cpu_model, cpu_features2,
306 : : FEATURE_VAES))
307 : : {
308 : 0 : cpu = "znver3";
309 : 0 : CHECK___builtin_cpu_is ("znver3");
310 : 0 : cpu_model->__cpu_subtype = AMDFAM19H_ZNVER3;
311 : : }
312 : : break;
313 : 0 : case 0x1a:
314 : 0 : cpu_model->__cpu_type = AMDFAM1AH;
315 : 0 : if (model <= 0x77)
316 : : {
317 : 0 : cpu = "znver5";
318 : 0 : CHECK___builtin_cpu_is ("znver5");
319 : 0 : cpu_model->__cpu_subtype = AMDFAM1AH_ZNVER5;
320 : : }
321 : 0 : else if (has_cpu_feature (cpu_model, cpu_features2,
322 : : FEATURE_AVX512VP2INTERSECT))
323 : : {
324 : 0 : cpu = "znver5";
325 : 0 : CHECK___builtin_cpu_is ("znver5");
326 : 0 : cpu_model->__cpu_subtype = AMDFAM1AH_ZNVER5;
327 : : }
328 : : break;
329 : : default:
330 : : break;
331 : : }
332 : :
333 : 22 : return cpu;
334 : : }
335 : :
336 : : /* Get the specific type of Intel CPU and return Intel CPU name. Return
337 : : NULL for unknown Intel CPU. */
338 : :
339 : : static inline const char *
340 : 0 : get_intel_cpu (struct __processor_model *cpu_model,
341 : : struct __processor_model2 *cpu_model2,
342 : : unsigned int *cpu_features2)
343 : : {
344 : 0 : const char *cpu = NULL;
345 : :
346 : : /* Parse family and model for family 0x6. */
347 : 0 : if (cpu_model2->__cpu_family == 0x6)
348 : 0 : switch (cpu_model2->__cpu_model)
349 : : {
350 : 0 : case 0x1c:
351 : 0 : case 0x26:
352 : : /* Bonnell. */
353 : 0 : cpu = "bonnell";
354 : 0 : CHECK___builtin_cpu_is ("atom");
355 : 0 : cpu_model->__cpu_type = INTEL_BONNELL;
356 : 0 : break;
357 : 0 : case 0x37:
358 : 0 : case 0x4a:
359 : 0 : case 0x4d:
360 : 0 : case 0x5d:
361 : : /* Silvermont. */
362 : 0 : case 0x4c:
363 : 0 : case 0x5a:
364 : 0 : case 0x75:
365 : : /* Airmont. */
366 : 0 : cpu = "silvermont";
367 : 0 : CHECK___builtin_cpu_is ("silvermont");
368 : 0 : cpu_model->__cpu_type = INTEL_SILVERMONT;
369 : 0 : break;
370 : 0 : case 0x5c:
371 : 0 : case 0x5f:
372 : : /* Goldmont. */
373 : 0 : cpu = "goldmont";
374 : 0 : CHECK___builtin_cpu_is ("goldmont");
375 : 0 : cpu_model->__cpu_type = INTEL_GOLDMONT;
376 : 0 : break;
377 : 0 : case 0x7a:
378 : : /* Goldmont Plus. */
379 : 0 : cpu = "goldmont-plus";
380 : 0 : CHECK___builtin_cpu_is ("goldmont-plus");
381 : 0 : cpu_model->__cpu_type = INTEL_GOLDMONT_PLUS;
382 : 0 : break;
383 : 0 : case 0x86:
384 : 0 : case 0x96:
385 : 0 : case 0x9c:
386 : : /* Tremont. */
387 : 0 : cpu = "tremont";
388 : 0 : CHECK___builtin_cpu_is ("tremont");
389 : 0 : cpu_model->__cpu_type = INTEL_TREMONT;
390 : 0 : break;
391 : 0 : case 0x17:
392 : 0 : case 0x1d:
393 : : /* Penryn. */
394 : 0 : case 0x0f:
395 : : /* Merom. */
396 : 0 : cpu = "core2";
397 : 0 : CHECK___builtin_cpu_is ("core2");
398 : 0 : cpu_model->__cpu_type = INTEL_CORE2;
399 : 0 : break;
400 : 0 : case 0x1a:
401 : 0 : case 0x1e:
402 : 0 : case 0x1f:
403 : 0 : case 0x2e:
404 : : /* Nehalem. */
405 : 0 : cpu = "nehalem";
406 : 0 : CHECK___builtin_cpu_is ("corei7");
407 : 0 : CHECK___builtin_cpu_is ("nehalem");
408 : 0 : cpu_model->__cpu_type = INTEL_COREI7;
409 : 0 : cpu_model->__cpu_subtype = INTEL_COREI7_NEHALEM;
410 : 0 : break;
411 : 0 : case 0x25:
412 : 0 : case 0x2c:
413 : 0 : case 0x2f:
414 : : /* Westmere. */
415 : 0 : cpu = "westmere";
416 : 0 : CHECK___builtin_cpu_is ("corei7");
417 : 0 : CHECK___builtin_cpu_is ("westmere");
418 : 0 : cpu_model->__cpu_type = INTEL_COREI7;
419 : 0 : cpu_model->__cpu_subtype = INTEL_COREI7_WESTMERE;
420 : 0 : break;
421 : 0 : case 0x2a:
422 : 0 : case 0x2d:
423 : : /* Sandy Bridge. */
424 : 0 : cpu = "sandybridge";
425 : 0 : CHECK___builtin_cpu_is ("corei7");
426 : 0 : CHECK___builtin_cpu_is ("sandybridge");
427 : 0 : cpu_model->__cpu_type = INTEL_COREI7;
428 : 0 : cpu_model->__cpu_subtype = INTEL_COREI7_SANDYBRIDGE;
429 : 0 : break;
430 : 0 : case 0x3a:
431 : 0 : case 0x3e:
432 : : /* Ivy Bridge. */
433 : 0 : cpu = "ivybridge";
434 : 0 : CHECK___builtin_cpu_is ("corei7");
435 : 0 : CHECK___builtin_cpu_is ("ivybridge");
436 : 0 : cpu_model->__cpu_type = INTEL_COREI7;
437 : 0 : cpu_model->__cpu_subtype = INTEL_COREI7_IVYBRIDGE;
438 : 0 : break;
439 : 0 : case 0x3c:
440 : 0 : case 0x3f:
441 : 0 : case 0x45:
442 : 0 : case 0x46:
443 : : /* Haswell. */
444 : 0 : cpu = "haswell";
445 : 0 : CHECK___builtin_cpu_is ("corei7");
446 : 0 : CHECK___builtin_cpu_is ("haswell");
447 : 0 : cpu_model->__cpu_type = INTEL_COREI7;
448 : 0 : cpu_model->__cpu_subtype = INTEL_COREI7_HASWELL;
449 : 0 : break;
450 : 0 : case 0x3d:
451 : 0 : case 0x47:
452 : 0 : case 0x4f:
453 : 0 : case 0x56:
454 : : /* Broadwell. */
455 : 0 : cpu = "broadwell";
456 : 0 : CHECK___builtin_cpu_is ("corei7");
457 : 0 : CHECK___builtin_cpu_is ("broadwell");
458 : 0 : cpu_model->__cpu_type = INTEL_COREI7;
459 : 0 : cpu_model->__cpu_subtype = INTEL_COREI7_BROADWELL;
460 : 0 : break;
461 : 0 : case 0x4e:
462 : 0 : case 0x5e:
463 : : /* Skylake. */
464 : 0 : case 0x8e:
465 : 0 : case 0x9e:
466 : : /* Kaby Lake. */
467 : 0 : case 0xa5:
468 : 0 : case 0xa6:
469 : : /* Comet Lake. */
470 : 0 : cpu = "skylake";
471 : 0 : CHECK___builtin_cpu_is ("corei7");
472 : 0 : CHECK___builtin_cpu_is ("skylake");
473 : 0 : cpu_model->__cpu_type = INTEL_COREI7;
474 : 0 : cpu_model->__cpu_subtype = INTEL_COREI7_SKYLAKE;
475 : 0 : break;
476 : 0 : case 0x55:
477 : 0 : CHECK___builtin_cpu_is ("corei7");
478 : 0 : cpu_model->__cpu_type = INTEL_COREI7;
479 : 0 : if (has_cpu_feature (cpu_model, cpu_features2,
480 : : FEATURE_AVX512BF16))
481 : : {
482 : : /* Cooper Lake. */
483 : 0 : cpu = "cooperlake";
484 : 0 : CHECK___builtin_cpu_is ("cooperlake");
485 : 0 : cpu_model->__cpu_subtype = INTEL_COREI7_COOPERLAKE;
486 : : }
487 : 0 : else if (has_cpu_feature (cpu_model, cpu_features2,
488 : : FEATURE_AVX512VNNI))
489 : : {
490 : : /* Cascade Lake. */
491 : 0 : cpu = "cascadelake";
492 : 0 : CHECK___builtin_cpu_is ("cascadelake");
493 : 0 : cpu_model->__cpu_subtype = INTEL_COREI7_CASCADELAKE;
494 : : }
495 : : else
496 : : {
497 : : /* Skylake with AVX-512 support. */
498 : 0 : cpu = "skylake-avx512";
499 : 0 : CHECK___builtin_cpu_is ("skylake-avx512");
500 : 0 : cpu_model->__cpu_subtype = INTEL_COREI7_SKYLAKE_AVX512;
501 : : }
502 : : break;
503 : 0 : case 0x66:
504 : : /* Cannon Lake. */
505 : 0 : cpu = "cannonlake";
506 : 0 : CHECK___builtin_cpu_is ("corei7");
507 : 0 : CHECK___builtin_cpu_is ("cannonlake");
508 : 0 : cpu_model->__cpu_type = INTEL_COREI7;
509 : 0 : cpu_model->__cpu_subtype = INTEL_COREI7_CANNONLAKE;
510 : 0 : break;
511 : 0 : case 0x7e:
512 : 0 : case 0x7d:
513 : 0 : case 0x9d:
514 : : /* Ice Lake client. */
515 : 0 : cpu = "icelake-client";
516 : 0 : CHECK___builtin_cpu_is ("corei7");
517 : 0 : CHECK___builtin_cpu_is ("icelake-client");
518 : 0 : cpu_model->__cpu_type = INTEL_COREI7;
519 : 0 : cpu_model->__cpu_subtype = INTEL_COREI7_ICELAKE_CLIENT;
520 : 0 : break;
521 : 0 : case 0x6a:
522 : 0 : case 0x6c:
523 : : /* Ice Lake server. */
524 : 0 : cpu = "icelake-server";
525 : 0 : CHECK___builtin_cpu_is ("corei7");
526 : 0 : CHECK___builtin_cpu_is ("icelake-server");
527 : 0 : cpu_model->__cpu_type = INTEL_COREI7;
528 : 0 : cpu_model->__cpu_subtype = INTEL_COREI7_ICELAKE_SERVER;
529 : 0 : break;
530 : 0 : case 0xa7:
531 : : /* Rocket Lake. */
532 : 0 : cpu = "rocketlake";
533 : 0 : CHECK___builtin_cpu_is ("corei7");
534 : 0 : CHECK___builtin_cpu_is ("rocketlake");
535 : 0 : cpu_model->__cpu_type = INTEL_COREI7;
536 : 0 : cpu_model->__cpu_subtype = INTEL_COREI7_ROCKETLAKE;
537 : 0 : break;
538 : 0 : case 0x8c:
539 : 0 : case 0x8d:
540 : : /* Tiger Lake. */
541 : 0 : cpu = "tigerlake";
542 : 0 : CHECK___builtin_cpu_is ("corei7");
543 : 0 : CHECK___builtin_cpu_is ("tigerlake");
544 : 0 : cpu_model->__cpu_type = INTEL_COREI7;
545 : 0 : cpu_model->__cpu_subtype = INTEL_COREI7_TIGERLAKE;
546 : 0 : break;
547 : 0 : case 0xbe:
548 : : /* Alder Lake N, E-core only. */
549 : 0 : case 0x97:
550 : 0 : case 0x9a:
551 : : /* Alder Lake. */
552 : 0 : case 0xb7:
553 : 0 : case 0xba:
554 : 0 : case 0xbf:
555 : : /* Raptor Lake. */
556 : 0 : case 0xaa:
557 : 0 : case 0xac:
558 : : /* Meteor Lake. */
559 : 0 : cpu = "alderlake";
560 : 0 : CHECK___builtin_cpu_is ("corei7");
561 : 0 : CHECK___builtin_cpu_is ("alderlake");
562 : 0 : cpu_model->__cpu_type = INTEL_COREI7;
563 : 0 : cpu_model->__cpu_subtype = INTEL_COREI7_ALDERLAKE;
564 : 0 : break;
565 : 0 : case 0x8f:
566 : : /* Sapphire Rapids. */
567 : 0 : case 0xcf:
568 : : /* Emerald Rapids. */
569 : 0 : cpu = "sapphirerapids";
570 : 0 : CHECK___builtin_cpu_is ("corei7");
571 : 0 : CHECK___builtin_cpu_is ("sapphirerapids");
572 : 0 : cpu_model->__cpu_type = INTEL_COREI7;
573 : 0 : cpu_model->__cpu_subtype = INTEL_COREI7_SAPPHIRERAPIDS;
574 : 0 : break;
575 : 0 : case 0xaf:
576 : : /* Sierra Forest. */
577 : 0 : cpu = "sierraforest";
578 : 0 : CHECK___builtin_cpu_is ("sierraforest");
579 : 0 : cpu_model->__cpu_type = INTEL_SIERRAFOREST;
580 : 0 : break;
581 : 0 : case 0xad:
582 : : /* Granite Rapids. */
583 : 0 : cpu = "graniterapids";
584 : 0 : CHECK___builtin_cpu_is ("corei7");
585 : 0 : CHECK___builtin_cpu_is ("graniterapids");
586 : 0 : cpu_model->__cpu_type = INTEL_COREI7;
587 : 0 : cpu_model->__cpu_subtype = INTEL_COREI7_GRANITERAPIDS;
588 : 0 : break;
589 : 0 : case 0xae:
590 : : /* Granite Rapids D. */
591 : 0 : cpu = "graniterapids-d";
592 : 0 : CHECK___builtin_cpu_is ("corei7");
593 : 0 : CHECK___builtin_cpu_is ("graniterapids-d");
594 : 0 : cpu_model->__cpu_type = INTEL_COREI7;
595 : 0 : cpu_model->__cpu_subtype = INTEL_COREI7_GRANITERAPIDS_D;
596 : 0 : break;
597 : 0 : case 0xb6:
598 : : /* Grand Ridge. */
599 : 0 : cpu = "grandridge";
600 : 0 : CHECK___builtin_cpu_is ("grandridge");
601 : 0 : cpu_model->__cpu_type = INTEL_GRANDRIDGE;
602 : 0 : break;
603 : 0 : case 0xb5:
604 : 0 : case 0xc5:
605 : : /* Arrow Lake. */
606 : 0 : cpu = "arrowlake";
607 : 0 : CHECK___builtin_cpu_is ("corei7");
608 : 0 : CHECK___builtin_cpu_is ("arrowlake");
609 : 0 : cpu_model->__cpu_type = INTEL_COREI7;
610 : 0 : cpu_model->__cpu_subtype = INTEL_COREI7_ARROWLAKE;
611 : 0 : break;
612 : 0 : case 0xc6:
613 : : /* Arrow Lake S. */
614 : 0 : case 0xbd:
615 : : /* Lunar Lake. */
616 : 0 : cpu = "arrowlake-s";
617 : 0 : CHECK___builtin_cpu_is ("corei7");
618 : 0 : CHECK___builtin_cpu_is ("arrowlake-s");
619 : 0 : cpu_model->__cpu_type = INTEL_COREI7;
620 : 0 : cpu_model->__cpu_subtype = INTEL_COREI7_ARROWLAKE_S;
621 : 0 : break;
622 : 0 : case 0xdd:
623 : : /* Clearwater Forest. */
624 : 0 : cpu = "clearwaterforest";
625 : 0 : CHECK___builtin_cpu_is ("clearwaterforest");
626 : 0 : cpu_model->__cpu_type = INTEL_CLEARWATERFOREST;
627 : 0 : break;
628 : 0 : case 0xcc:
629 : : /* Panther Lake. */
630 : 0 : cpu = "pantherlake";
631 : 0 : CHECK___builtin_cpu_is ("corei7");
632 : 0 : CHECK___builtin_cpu_is ("pantherlake");
633 : 0 : cpu_model->__cpu_type = INTEL_COREI7;
634 : 0 : cpu_model->__cpu_subtype = INTEL_COREI7_PANTHERLAKE;
635 : 0 : break;
636 : : default:
637 : : break;
638 : : }
639 : : /* Parse family and model for family 0x13. */
640 : 0 : else if (cpu_model2->__cpu_family == 0x13)
641 : 0 : switch (cpu_model2->__cpu_model)
642 : : {
643 : 0 : case 0x01:
644 : : /* Diamond Rapids. */
645 : 0 : cpu = "diamondrapids";
646 : 0 : CHECK___builtin_cpu_is ("corei7");
647 : 0 : CHECK___builtin_cpu_is ("diamondrapids");
648 : 0 : cpu_model->__cpu_type = INTEL_COREI7;
649 : 0 : cpu_model->__cpu_subtype = INTEL_COREI7_DIAMONDRAPIDS;
650 : 0 : break;
651 : : default:
652 : : break;
653 : : }
654 : :
655 : 0 : return cpu;
656 : : }
657 : :
658 : : /* Get the specific type of ZHAOXIN CPU and return ZHAOXIN CPU name.
659 : : Return NULL for unknown ZHAOXIN CPU. */
660 : :
661 : : static inline const char *
662 : 0 : get_zhaoxin_cpu (struct __processor_model *cpu_model,
663 : : struct __processor_model2 *cpu_model2,
664 : : unsigned int *cpu_features2)
665 : : {
666 : 0 : const char *cpu = NULL;
667 : 0 : unsigned int family = cpu_model2->__cpu_family;
668 : 0 : unsigned int model = cpu_model2->__cpu_model;
669 : :
670 : 0 : switch (family)
671 : : {
672 : : /* ZHAOXIN family 7h. */
673 : 0 : case 0x07:
674 : 0 : cpu_model->__cpu_type = ZHAOXIN_FAM7H;
675 : 0 : if (model == 0x3b)
676 : : {
677 : 0 : cpu = "lujiazui";
678 : 0 : CHECK___builtin_cpu_is ("lujiazui");
679 : 0 : reset_cpu_feature (cpu_model, cpu_features2, FEATURE_AVX);
680 : 0 : reset_cpu_feature (cpu_model, cpu_features2, FEATURE_F16C);
681 : 0 : cpu_model->__cpu_subtype = ZHAOXIN_FAM7H_LUJIAZUI;
682 : : }
683 : 0 : else if (model == 0x5b)
684 : : {
685 : 0 : cpu = "yongfeng";
686 : 0 : CHECK___builtin_cpu_is ("yongfeng");
687 : 0 : cpu_model->__cpu_subtype = ZHAOXIN_FAM7H_YONGFENG;
688 : : }
689 : 0 : else if (model >= 0x6b)
690 : : {
691 : 0 : cpu = "shijidadao";
692 : 0 : CHECK___builtin_cpu_is ("shijidadao");
693 : 0 : cpu_model->__cpu_subtype = ZHAOXIN_FAM7H_SHIJIDADAO;
694 : : }
695 : : break;
696 : : default:
697 : : break;
698 : : }
699 : :
700 : 0 : return cpu;
701 : : }
702 : :
703 : : /* ECX and EDX are output of CPUID at level one. */
704 : : static inline void
705 : 22 : get_available_features (struct __processor_model *cpu_model,
706 : : struct __processor_model2 *cpu_model2,
707 : : unsigned int *cpu_features2,
708 : : unsigned int ecx, unsigned int edx)
709 : : {
710 : 22 : unsigned int max_cpuid_level = cpu_model2->__cpu_max_level;
711 : 22 : unsigned int eax, ebx;
712 : 22 : unsigned int ext_level;
713 : :
714 : : /* Get XCR_XFEATURE_ENABLED_MASK register with xgetbv. */
715 : : #define XCR_XFEATURE_ENABLED_MASK 0x0
716 : : #define XSTATE_FP 0x1
717 : : #define XSTATE_SSE 0x2
718 : : #define XSTATE_YMM 0x4
719 : : #define XSTATE_OPMASK 0x20
720 : : #define XSTATE_ZMM 0x40
721 : : #define XSTATE_HI_ZMM 0x80
722 : : #define XSTATE_TILECFG 0x20000
723 : : #define XSTATE_TILEDATA 0x40000
724 : : #define XSTATE_APX_F 0x80000
725 : :
726 : : #define XCR_AVX_ENABLED_MASK \
727 : : (XSTATE_SSE | XSTATE_YMM)
728 : : #define XCR_AVX512F_ENABLED_MASK \
729 : : (XSTATE_SSE | XSTATE_YMM | XSTATE_OPMASK | XSTATE_ZMM | XSTATE_HI_ZMM)
730 : : #define XCR_AMX_ENABLED_MASK \
731 : : (XSTATE_TILECFG | XSTATE_TILEDATA)
732 : : #define XCR_APX_F_ENABLED_MASK XSTATE_APX_F
733 : :
734 : : /* Check if AVX, AVX512 and APX are usable. */
735 : 22 : int avx_usable = 0;
736 : 22 : int avx512_usable = 0;
737 : 22 : int amx_usable = 0;
738 : 22 : int apx_usable = 0;
739 : : /* Check if KL is usable. */
740 : 22 : int has_kl = 0;
741 : : /* Record AVX10 version. */
742 : 22 : int avx10_set = 0;
743 : 22 : int version = 0;
744 : 22 : if ((ecx & bit_OSXSAVE))
745 : : {
746 : : /* Check if XMM, YMM, OPMASK, upper 256 bits of ZMM0-ZMM15 and
747 : : ZMM16-ZMM31 states are supported by OSXSAVE. */
748 : 22 : unsigned int xcrlow;
749 : 22 : unsigned int xcrhigh;
750 : 22 : __asm__ (".byte 0x0f, 0x01, 0xd0"
751 : : : "=a" (xcrlow), "=d" (xcrhigh)
752 : : : "c" (XCR_XFEATURE_ENABLED_MASK));
753 : 22 : if ((xcrlow & XCR_AVX_ENABLED_MASK) == XCR_AVX_ENABLED_MASK)
754 : : {
755 : 22 : avx_usable = 1;
756 : 22 : avx512_usable = ((xcrlow & XCR_AVX512F_ENABLED_MASK)
757 : 22 : == XCR_AVX512F_ENABLED_MASK);
758 : : }
759 : 22 : amx_usable = ((xcrlow & XCR_AMX_ENABLED_MASK)
760 : 22 : == XCR_AMX_ENABLED_MASK);
761 : 22 : apx_usable = ((xcrlow & XCR_APX_F_ENABLED_MASK)
762 : 22 : == XCR_APX_F_ENABLED_MASK);
763 : : }
764 : :
765 : : #define set_feature(f) \
766 : : set_cpu_feature (cpu_model, cpu_features2, f)
767 : :
768 : 22 : if (edx & bit_CMOV)
769 : 22 : set_feature (FEATURE_CMOV);
770 : 22 : if (edx & bit_MMX)
771 : 22 : set_feature (FEATURE_MMX);
772 : 22 : if (edx & bit_SSE)
773 : 22 : set_feature (FEATURE_SSE);
774 : 22 : if (edx & bit_SSE2)
775 : 22 : set_feature (FEATURE_SSE2);
776 : 22 : if (edx & bit_CMPXCHG8B)
777 : 22 : set_feature (FEATURE_CMPXCHG8B);
778 : 22 : if (edx & bit_FXSAVE)
779 : 22 : set_feature (FEATURE_FXSAVE);
780 : :
781 : 22 : if (ecx & bit_POPCNT)
782 : 22 : set_feature (FEATURE_POPCNT);
783 : 22 : if (ecx & bit_AES)
784 : 22 : set_feature (FEATURE_AES);
785 : 22 : if (ecx & bit_PCLMUL)
786 : 22 : set_feature (FEATURE_PCLMUL);
787 : 22 : if (ecx & bit_SSE3)
788 : 22 : set_feature (FEATURE_SSE3);
789 : 22 : if (ecx & bit_SSSE3)
790 : 22 : set_feature (FEATURE_SSSE3);
791 : 22 : if (ecx & bit_SSE4_1)
792 : 22 : set_feature (FEATURE_SSE4_1);
793 : 22 : if (ecx & bit_SSE4_2)
794 : 22 : set_feature (FEATURE_SSE4_2);
795 : 22 : if (ecx & bit_OSXSAVE)
796 : 22 : set_feature (FEATURE_OSXSAVE);
797 : 22 : if (ecx & bit_CMPXCHG16B)
798 : 22 : set_feature (FEATURE_CMPXCHG16B);
799 : 22 : if (ecx & bit_MOVBE)
800 : 22 : set_feature (FEATURE_MOVBE);
801 : 22 : if (ecx & bit_AES)
802 : 22 : set_feature (FEATURE_AES);
803 : 22 : if (ecx & bit_RDRND)
804 : 22 : set_feature (FEATURE_RDRND);
805 : 22 : if (ecx & bit_XSAVE)
806 : 22 : set_feature (FEATURE_XSAVE);
807 : 22 : if (avx_usable)
808 : : {
809 : 22 : if (ecx & bit_AVX)
810 : 22 : set_feature (FEATURE_AVX);
811 : 22 : if (ecx & bit_FMA)
812 : 22 : set_feature (FEATURE_FMA);
813 : 22 : if (ecx & bit_F16C)
814 : 22 : set_feature (FEATURE_F16C);
815 : : }
816 : :
817 : : /* Get Advanced Features at level 7 (eax = 7, ecx = 0/1). */
818 : 22 : if (max_cpuid_level >= 7)
819 : : {
820 : 22 : unsigned int max_subleaf_level;
821 : :
822 : 22 : __cpuid_count (7, 0, max_subleaf_level, ebx, ecx, edx);
823 : 22 : if (ebx & bit_BMI)
824 : 22 : set_feature (FEATURE_BMI);
825 : 22 : if (ebx & bit_SGX)
826 : 0 : set_feature (FEATURE_SGX);
827 : 22 : if (ebx & bit_HLE)
828 : 0 : set_feature (FEATURE_HLE);
829 : 22 : if (ebx & bit_RTM)
830 : 0 : set_feature (FEATURE_RTM);
831 : 22 : if (avx_usable)
832 : : {
833 : 22 : if (ebx & bit_AVX2)
834 : 22 : set_feature (FEATURE_AVX2);
835 : 22 : if (ecx & bit_VPCLMULQDQ)
836 : 0 : set_feature (FEATURE_VPCLMULQDQ);
837 : 22 : if (ecx & bit_VAES)
838 : 0 : set_feature (FEATURE_VAES);
839 : : }
840 : 22 : if (ebx & bit_BMI2)
841 : 22 : set_feature (FEATURE_BMI2);
842 : 22 : if (ebx & bit_FSGSBASE)
843 : 22 : set_feature (FEATURE_FSGSBASE);
844 : 22 : if (ebx & bit_RDSEED)
845 : 22 : set_feature (FEATURE_RDSEED);
846 : 22 : if (ebx & bit_ADX)
847 : 22 : set_feature (FEATURE_ADX);
848 : 22 : if (ebx & bit_SHA)
849 : 22 : set_feature (FEATURE_SHA);
850 : 22 : if (ebx & bit_CLFLUSHOPT)
851 : 22 : set_feature (FEATURE_CLFLUSHOPT);
852 : 22 : if (ebx & bit_CLWB)
853 : 22 : set_feature (FEATURE_CLWB);
854 : : /* NB: bit_OSPKE indicates that OS supports PKU. */
855 : 22 : if (ecx & bit_OSPKE)
856 : 0 : set_feature (FEATURE_PKU);
857 : 22 : if (ecx & bit_RDPID)
858 : 22 : set_feature (FEATURE_RDPID);
859 : 22 : if (ecx & bit_GFNI)
860 : 0 : set_feature (FEATURE_GFNI);
861 : 22 : if (ecx & bit_MOVDIRI)
862 : 0 : set_feature (FEATURE_MOVDIRI);
863 : 22 : if (ecx & bit_MOVDIR64B)
864 : 0 : set_feature (FEATURE_MOVDIR64B);
865 : 22 : if (ecx & bit_ENQCMD)
866 : 0 : set_feature (FEATURE_ENQCMD);
867 : 22 : if (ecx & bit_CLDEMOTE)
868 : 0 : set_feature (FEATURE_CLDEMOTE);
869 : 22 : if (ecx & bit_WAITPKG)
870 : 0 : set_feature (FEATURE_WAITPKG);
871 : 22 : if (ecx & bit_SHSTK)
872 : 0 : set_feature (FEATURE_SHSTK);
873 : 22 : if (ecx & bit_KL)
874 : 0 : has_kl = 1;
875 : 22 : if (edx & bit_SERIALIZE)
876 : 0 : set_feature (FEATURE_SERIALIZE);
877 : 22 : if (edx & bit_TSXLDTRK)
878 : 0 : set_feature (FEATURE_TSXLDTRK);
879 : 22 : if (edx & bit_PCONFIG)
880 : 0 : set_feature (FEATURE_PCONFIG);
881 : 22 : if (edx & bit_IBT)
882 : 0 : set_feature (FEATURE_IBT);
883 : 22 : if (edx & bit_UINTR)
884 : 0 : set_feature (FEATURE_UINTR);
885 : 22 : if (amx_usable)
886 : : {
887 : 0 : if (edx & bit_AMX_TILE)
888 : 0 : set_feature (FEATURE_AMX_TILE);
889 : 0 : if (edx & bit_AMX_INT8)
890 : 0 : set_feature (FEATURE_AMX_INT8);
891 : 0 : if (edx & bit_AMX_BF16)
892 : 0 : set_feature (FEATURE_AMX_BF16);
893 : : }
894 : 22 : if (avx512_usable)
895 : : {
896 : 0 : if (ebx & bit_AVX512F)
897 : 0 : set_feature (FEATURE_AVX512F);
898 : 0 : if (ebx & bit_AVX512VL)
899 : 0 : set_feature (FEATURE_AVX512VL);
900 : 0 : if (ebx & bit_AVX512BW)
901 : 0 : set_feature (FEATURE_AVX512BW);
902 : 0 : if (ebx & bit_AVX512DQ)
903 : 0 : set_feature (FEATURE_AVX512DQ);
904 : 0 : if (ebx & bit_AVX512CD)
905 : 0 : set_feature (FEATURE_AVX512CD);
906 : 0 : if (ebx & bit_AVX512IFMA)
907 : 0 : set_feature (FEATURE_AVX512IFMA);
908 : 0 : if (ecx & bit_AVX512VBMI)
909 : 0 : set_feature (FEATURE_AVX512VBMI);
910 : 0 : if (ecx & bit_AVX512VBMI2)
911 : 0 : set_feature (FEATURE_AVX512VBMI2);
912 : 0 : if (ecx & bit_AVX512VNNI)
913 : 0 : set_feature (FEATURE_AVX512VNNI);
914 : 0 : if (ecx & bit_AVX512BITALG)
915 : 0 : set_feature (FEATURE_AVX512BITALG);
916 : 0 : if (ecx & bit_AVX512VPOPCNTDQ)
917 : 0 : set_feature (FEATURE_AVX512VPOPCNTDQ);
918 : 0 : if (edx & bit_AVX512VP2INTERSECT)
919 : 0 : set_feature (FEATURE_AVX512VP2INTERSECT);
920 : 0 : if (edx & bit_AVX512FP16)
921 : 0 : set_feature (FEATURE_AVX512FP16);
922 : : }
923 : :
924 : 22 : if (max_subleaf_level >= 1)
925 : : {
926 : 0 : __cpuid_count (7, 1, eax, ebx, ecx, edx);
927 : 0 : if (eax & bit_HRESET)
928 : 0 : set_feature (FEATURE_HRESET);
929 : 0 : if (eax & bit_CMPCCXADD)
930 : 0 : set_feature(FEATURE_CMPCCXADD);
931 : 0 : if (edx & bit_PREFETCHI)
932 : 0 : set_feature (FEATURE_PREFETCHI);
933 : 0 : if (eax & bit_RAOINT)
934 : 0 : set_feature (FEATURE_RAOINT);
935 : 0 : if (edx & bit_USER_MSR)
936 : 0 : set_feature (FEATURE_USER_MSR);
937 : 0 : if (eax & bit_MOVRS)
938 : 0 : set_feature (FEATURE_MOVRS);
939 : 0 : if (avx_usable)
940 : : {
941 : 0 : if (eax & bit_AVXVNNI)
942 : 0 : set_feature (FEATURE_AVXVNNI);
943 : 0 : if (eax & bit_AVXIFMA)
944 : 0 : set_feature (FEATURE_AVXIFMA);
945 : 0 : if (edx & bit_AVXVNNIINT8)
946 : 0 : set_feature (FEATURE_AVXVNNIINT8);
947 : 0 : if (edx & bit_AVXNECONVERT)
948 : 0 : set_feature (FEATURE_AVXNECONVERT);
949 : 0 : if (edx & bit_AVXVNNIINT16)
950 : 0 : set_feature (FEATURE_AVXVNNIINT16);
951 : 0 : if (eax & bit_SM3)
952 : 0 : set_feature (FEATURE_SM3);
953 : 0 : if (eax & bit_SHA512)
954 : 0 : set_feature (FEATURE_SHA512);
955 : 0 : if (eax & bit_SM4)
956 : 0 : set_feature (FEATURE_SM4);
957 : : }
958 : 0 : if (avx512_usable)
959 : : {
960 : 0 : if (eax & bit_AVX512BF16)
961 : 0 : set_feature (FEATURE_AVX512BF16);
962 : : /* AVX10 has the same XSTATE with AVX512. */
963 : 0 : if (edx & bit_AVX10)
964 : 0 : avx10_set = 1;
965 : : }
966 : 0 : if (amx_usable)
967 : : {
968 : 0 : if (eax & bit_AMX_FP16)
969 : 0 : set_feature (FEATURE_AMX_FP16);
970 : 0 : if (edx & bit_AMX_COMPLEX)
971 : 0 : set_feature (FEATURE_AMX_COMPLEX);
972 : : }
973 : 0 : if (apx_usable)
974 : : {
975 : 0 : if (edx & bit_APX_F)
976 : 0 : set_feature (FEATURE_APX_F);
977 : : }
978 : : }
979 : : }
980 : :
981 : : /* Get Advanced Features at level 0xd (eax = 0xd, ecx = 1). */
982 : 22 : if (max_cpuid_level >= 0xd)
983 : : {
984 : 22 : __cpuid_count (0xd, 1, eax, ebx, ecx, edx);
985 : 22 : if (eax & bit_XSAVEOPT)
986 : 22 : set_feature (FEATURE_XSAVEOPT);
987 : 22 : if (eax & bit_XSAVEC)
988 : 22 : set_feature (FEATURE_XSAVEC);
989 : 22 : if (eax & bit_XSAVES)
990 : 22 : set_feature (FEATURE_XSAVES);
991 : : }
992 : :
993 : : /* Get Advanced Features at level 0x14 (eax = 0x14, ecx = 0). */
994 : 22 : if (max_cpuid_level >= 0x14)
995 : : {
996 : 0 : __cpuid_count (0x14, 0, eax, ebx, ecx, edx);
997 : 0 : if (ebx & bit_PTWRITE)
998 : 0 : set_feature (FEATURE_PTWRITE);
999 : : }
1000 : :
1001 : : /* Get Advanced Features at level 0x19 (eax = 0x19). */
1002 : 0 : if (max_cpuid_level >= 0x19)
1003 : : {
1004 : 0 : __cpuid (0x19, eax, ebx, ecx, edx);
1005 : : /* Check if OS support keylocker. */
1006 : 0 : if (ebx & bit_AESKLE)
1007 : : {
1008 : 0 : set_feature (FEATURE_AESKLE);
1009 : 0 : if (ebx & bit_WIDEKL)
1010 : 0 : set_feature (FEATURE_WIDEKL);
1011 : 0 : if (has_kl)
1012 : 0 : set_feature (FEATURE_KL);
1013 : : }
1014 : : }
1015 : :
1016 : : /* Get Advanced Features at level 0x1e (eax = 0x1e, ecx = 1). */
1017 : 0 : if (max_cpuid_level >= 0x1e)
1018 : : {
1019 : 0 : __cpuid_count (0x1e, 1, eax, ebx, ecx, edx);
1020 : 0 : if (amx_usable)
1021 : : {
1022 : 0 : if (eax & bit_AMX_AVX512)
1023 : 0 : set_feature (FEATURE_AMX_AVX512);
1024 : 0 : if (eax & bit_AMX_TF32)
1025 : 0 : set_feature (FEATURE_AMX_TF32);
1026 : 0 : if (eax & bit_AMX_TRANSPOSE)
1027 : 0 : set_feature (FEATURE_AMX_TRANSPOSE);
1028 : 0 : if (eax & bit_AMX_FP8)
1029 : 0 : set_feature (FEATURE_AMX_FP8);
1030 : 0 : if (eax & bit_AMX_MOVRS)
1031 : 0 : set_feature (FEATURE_AMX_MOVRS);
1032 : : }
1033 : : }
1034 : :
1035 : : /* Get Advanced Features at level 0x24 (eax = 0x24, ecx = 0). */
1036 : 22 : if (avx10_set && max_cpuid_level >= 0x24)
1037 : : {
1038 : 0 : __cpuid_count (0x24, 0, eax, ebx, ecx, edx);
1039 : 0 : version = ebx & 0xff;
1040 : 0 : switch (version)
1041 : : {
1042 : 0 : case 2:
1043 : 0 : set_feature (FEATURE_AVX10_2);
1044 : : /* Fall through. */
1045 : 0 : case 1:
1046 : 0 : set_feature (FEATURE_AVX10_1);
1047 : 0 : break;
1048 : 0 : default:
1049 : 0 : set_feature (FEATURE_AVX10_1);
1050 : 0 : break;
1051 : : }
1052 : : }
1053 : :
1054 : : /* Check cpuid level of extended features. */
1055 : 22 : __cpuid (0x80000000, ext_level, ebx, ecx, edx);
1056 : :
1057 : 22 : cpu_model2->__cpu_ext_level = ext_level;
1058 : :
1059 : 22 : if (ext_level >= 0x80000001)
1060 : : {
1061 : 22 : __cpuid (0x80000001, eax, ebx, ecx, edx);
1062 : :
1063 : 22 : if (ecx & bit_SSE4a)
1064 : 22 : set_feature (FEATURE_SSE4_A);
1065 : 22 : if (ecx & bit_LAHF_LM)
1066 : 22 : set_feature (FEATURE_LAHF_LM);
1067 : 22 : if (ecx & bit_ABM)
1068 : 22 : set_feature (FEATURE_ABM);
1069 : 22 : if (ecx & bit_LWP)
1070 : 0 : set_feature (FEATURE_LWP);
1071 : 22 : if (ecx & bit_TBM)
1072 : 0 : set_feature (FEATURE_TBM);
1073 : 22 : if (ecx & bit_LZCNT)
1074 : 22 : set_feature (FEATURE_LZCNT);
1075 : 22 : if (ecx & bit_PRFCHW)
1076 : 22 : set_feature (FEATURE_PRFCHW);
1077 : 22 : if (ecx & bit_MWAITX)
1078 : 22 : set_feature (FEATURE_MWAITX);
1079 : :
1080 : 22 : if (edx & bit_LM)
1081 : 22 : set_feature (FEATURE_LM);
1082 : 22 : if (edx & bit_3DNOWP)
1083 : 0 : set_feature (FEATURE_3DNOWP);
1084 : 22 : if (edx & bit_3DNOW)
1085 : 0 : set_feature (FEATURE_3DNOW);
1086 : :
1087 : 22 : if (avx_usable)
1088 : : {
1089 : 22 : if (ecx & bit_FMA4)
1090 : 0 : set_feature (FEATURE_FMA4);
1091 : 22 : if (ecx & bit_XOP)
1092 : 0 : set_feature (FEATURE_XOP);
1093 : : }
1094 : : }
1095 : :
1096 : 22 : if (ext_level >= 0x80000008)
1097 : : {
1098 : 22 : __cpuid (0x80000008, eax, ebx, ecx, edx);
1099 : 22 : if (ebx & bit_CLZERO)
1100 : 22 : set_feature (FEATURE_CLZERO);
1101 : 22 : if (ebx & bit_WBNOINVD)
1102 : 22 : set_feature (FEATURE_WBNOINVD);
1103 : : }
1104 : :
1105 : : #undef set_feature
1106 : 22 : }
1107 : :
1108 : : static inline int
1109 : 22 : cpu_indicator_init (struct __processor_model *cpu_model,
1110 : : struct __processor_model2 *cpu_model2,
1111 : : unsigned int *cpu_features2)
1112 : : {
1113 : 22 : unsigned int eax, ebx, ecx, edx;
1114 : :
1115 : 22 : int max_level;
1116 : 22 : unsigned int vendor;
1117 : 22 : unsigned int model, family;
1118 : 22 : unsigned int extended_model, extended_family;
1119 : :
1120 : : /* This function needs to run just once. */
1121 : 22 : if (cpu_model->__cpu_vendor)
1122 : : return 0;
1123 : :
1124 : : /* Assume cpuid insn present. Run in level 0 to get vendor id. */
1125 : 22 : if (!__get_cpuid (0, &eax, &ebx, &ecx, &edx))
1126 : : {
1127 : 0 : cpu_model->__cpu_vendor = VENDOR_OTHER;
1128 : 0 : return -1;
1129 : : }
1130 : :
1131 : 22 : vendor = ebx;
1132 : 22 : max_level = eax;
1133 : :
1134 : 22 : if (max_level < 1)
1135 : : {
1136 : 0 : cpu_model->__cpu_vendor = VENDOR_OTHER;
1137 : 0 : return -1;
1138 : : }
1139 : :
1140 : 22 : if (!__get_cpuid (1, &eax, &ebx, &ecx, &edx))
1141 : : {
1142 : 0 : cpu_model->__cpu_vendor = VENDOR_OTHER;
1143 : 0 : return -1;
1144 : : }
1145 : :
1146 : 22 : cpu_model2->__cpu_max_level = max_level;
1147 : :
1148 : 22 : model = (eax >> 4) & 0x0f;
1149 : 22 : family = (eax >> 8) & 0x0f;
1150 : 22 : extended_model = (eax >> 12) & 0xf0;
1151 : 22 : extended_family = (eax >> 20) & 0xff;
1152 : :
1153 : : /* Find available features. */
1154 : 22 : get_available_features (cpu_model, cpu_model2, cpu_features2,
1155 : : ecx, edx);
1156 : :
1157 : 22 : if (vendor == signature_INTEL_ebx)
1158 : : {
1159 : : /* Adjust model and family for Intel CPUS. */
1160 : 0 : if (family == 0x0f)
1161 : : {
1162 : 0 : family += extended_family;
1163 : 0 : model += extended_model;
1164 : : }
1165 : 0 : else if (family == 0x06)
1166 : 0 : model += extended_model;
1167 : :
1168 : 0 : cpu_model2->__cpu_family = family;
1169 : 0 : cpu_model2->__cpu_model = model;
1170 : :
1171 : : /* Get CPU type. */
1172 : 0 : get_intel_cpu (cpu_model, cpu_model2, cpu_features2);
1173 : 0 : cpu_model->__cpu_vendor = VENDOR_INTEL;
1174 : : }
1175 : 22 : else if (vendor == signature_AMD_ebx)
1176 : : {
1177 : : /* Adjust model and family for AMD CPUS. */
1178 : 22 : if (family == 0x0f)
1179 : : {
1180 : 22 : family += extended_family;
1181 : 22 : model += extended_model;
1182 : : }
1183 : :
1184 : 22 : cpu_model2->__cpu_family = family;
1185 : 22 : cpu_model2->__cpu_model = model;
1186 : :
1187 : : /* Get CPU type. */
1188 : 22 : get_amd_cpu (cpu_model, cpu_model2, cpu_features2);
1189 : 22 : cpu_model->__cpu_vendor = VENDOR_AMD;
1190 : : }
1191 : 0 : else if (vendor == signature_CENTAUR_ebx && family < 0x07)
1192 : 0 : cpu_model->__cpu_vendor = VENDOR_CENTAUR;
1193 : 0 : else if (vendor == signature_SHANGHAI_ebx
1194 : 0 : || vendor == signature_CENTAUR_ebx)
1195 : : {
1196 : : /* Adjust model and family for ZHAOXIN CPUS. */
1197 : 0 : if (family == 0x07)
1198 : 0 : model += extended_model;
1199 : :
1200 : 0 : cpu_model2->__cpu_family = family;
1201 : 0 : cpu_model2->__cpu_model = model;
1202 : :
1203 : : /* Get CPU type. */
1204 : 0 : get_zhaoxin_cpu (cpu_model, cpu_model2, cpu_features2);
1205 : 0 : cpu_model->__cpu_vendor = VENDOR_ZHAOXIN;
1206 : : }
1207 : 0 : else if (vendor == signature_CYRIX_ebx)
1208 : 0 : cpu_model->__cpu_vendor = VENDOR_CYRIX;
1209 : 0 : else if (vendor == signature_NSC_ebx)
1210 : 0 : cpu_model->__cpu_vendor = VENDOR_NSC;
1211 : : else
1212 : 0 : cpu_model->__cpu_vendor = VENDOR_OTHER;
1213 : :
1214 : 22 : if (has_cpu_feature (cpu_model, cpu_features2, FEATURE_LM)
1215 : 22 : && has_cpu_feature (cpu_model, cpu_features2, FEATURE_SSE2))
1216 : : {
1217 : 22 : CHECK___builtin_cpu_supports ("x86-64");
1218 : 22 : set_cpu_feature (cpu_model, cpu_features2,
1219 : : FEATURE_X86_64_BASELINE);
1220 : 22 : if (has_cpu_feature (cpu_model, cpu_features2, FEATURE_CMPXCHG16B)
1221 : 22 : && has_cpu_feature (cpu_model, cpu_features2, FEATURE_POPCNT)
1222 : 22 : && has_cpu_feature (cpu_model, cpu_features2, FEATURE_LAHF_LM)
1223 : 44 : && has_cpu_feature (cpu_model, cpu_features2, FEATURE_SSE4_2))
1224 : : {
1225 : 22 : CHECK___builtin_cpu_supports ("x86-64-v2");
1226 : 22 : set_cpu_feature (cpu_model, cpu_features2,
1227 : : FEATURE_X86_64_V2);
1228 : 22 : if (has_cpu_feature (cpu_model, cpu_features2, FEATURE_AVX2)
1229 : 22 : && has_cpu_feature (cpu_model, cpu_features2, FEATURE_BMI)
1230 : 22 : && has_cpu_feature (cpu_model, cpu_features2, FEATURE_BMI2)
1231 : 22 : && has_cpu_feature (cpu_model, cpu_features2, FEATURE_F16C)
1232 : 22 : && has_cpu_feature (cpu_model, cpu_features2, FEATURE_FMA)
1233 : 22 : && has_cpu_feature (cpu_model, cpu_features2,
1234 : : FEATURE_LZCNT)
1235 : 44 : && has_cpu_feature (cpu_model, cpu_features2,
1236 : : FEATURE_MOVBE))
1237 : : {
1238 : 22 : CHECK___builtin_cpu_supports ("x86-64-v3");
1239 : 22 : set_cpu_feature (cpu_model, cpu_features2,
1240 : : FEATURE_X86_64_V3);
1241 : 22 : if (has_cpu_feature (cpu_model, cpu_features2,
1242 : : FEATURE_AVX512BW)
1243 : 0 : && has_cpu_feature (cpu_model, cpu_features2,
1244 : : FEATURE_AVX512CD)
1245 : 0 : && has_cpu_feature (cpu_model, cpu_features2,
1246 : : FEATURE_AVX512DQ)
1247 : 22 : && has_cpu_feature (cpu_model, cpu_features2,
1248 : : FEATURE_AVX512VL))
1249 : : {
1250 : 0 : CHECK___builtin_cpu_supports ("x86-64-v4");
1251 : 0 : set_cpu_feature (cpu_model, cpu_features2,
1252 : : FEATURE_X86_64_V4);
1253 : : }
1254 : : }
1255 : : }
1256 : : }
1257 : :
1258 : 22 : gcc_assert (cpu_model->__cpu_vendor < VENDOR_MAX);
1259 : 22 : gcc_assert (cpu_model->__cpu_type < CPU_TYPE_MAX);
1260 : 22 : gcc_assert (cpu_model->__cpu_subtype < CPU_SUBTYPE_MAX);
1261 : :
1262 : : return 0;
1263 : : }
|