i386.c revision 1.109 1 /* $NetBSD: i386.c,v 1.109 2020/04/06 09:46:21 msaitoh Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 2001, 2006, 2007, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Frank van der Linden, and by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*-
33 * Copyright (c)2008 YAMAMOTO Takashi,
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 */
57
58 #include <sys/cdefs.h>
59 #ifndef lint
60 __RCSID("$NetBSD: i386.c,v 1.109 2020/04/06 09:46:21 msaitoh Exp $");
61 #endif /* not lint */
62
63 #include <sys/types.h>
64 #include <sys/param.h>
65 #include <sys/bitops.h>
66 #include <sys/sysctl.h>
67 #include <sys/ioctl.h>
68 #include <sys/cpuio.h>
69
70 #include <errno.h>
71 #include <string.h>
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <err.h>
75 #include <assert.h>
76 #include <math.h>
77 #include <util.h>
78
79 #include <machine/specialreg.h>
80 #include <machine/cpu.h>
81
82 #include <x86/cpuvar.h>
83 #include <x86/cputypes.h>
84 #include <x86/cacheinfo.h>
85 #include <x86/cpu_ucode.h>
86
87 #include "../cpuctl.h"
88 #include "cpuctl_i386.h"
89
90 /* Size of buffer for printing humanized numbers */
91 #define HUMAN_BUFSIZE sizeof("999KB")
92
93 struct cpu_info {
94 const char *ci_dev;
95 int32_t ci_cpu_type; /* for cpu's without cpuid */
96 int32_t ci_cpuid_level; /* highest cpuid supported */
97 uint32_t ci_cpuid_extlevel; /* highest cpuid extended func lv */
98 uint32_t ci_signature; /* X86 cpuid type */
99 uint32_t ci_family; /* from ci_signature */
100 uint32_t ci_model; /* from ci_signature */
101 uint32_t ci_feat_val[10]; /* X86 CPUID feature bits
102 * [0] basic features %edx
103 * [1] basic features %ecx
104 * [2] extended features %edx
105 * [3] extended features %ecx
106 * [4] VIA padlock features
107 * [5] structure ext. feat. %ebx
108 * [6] structure ext. feat. %ecx
109 * [7] structure ext. feat. %edx
110 * [8] XCR0 bits (d:0 %eax)
111 * [9] xsave flags (d:1 %eax)
112 */
113 uint32_t ci_cpu_class; /* CPU class */
114 uint32_t ci_brand_id; /* Intel brand id */
115 uint32_t ci_vendor[4]; /* vendor string */
116 uint32_t ci_cpu_serial[3]; /* PIII serial number */
117 uint64_t ci_tsc_freq; /* cpu cycles/second */
118 uint8_t ci_packageid;
119 uint8_t ci_coreid;
120 uint8_t ci_smtid;
121 uint32_t ci_initapicid;
122
123 uint32_t ci_cur_xsave;
124 uint32_t ci_max_xsave;
125
126 struct x86_cache_info ci_cinfo[CAI_COUNT];
127 void (*ci_info)(struct cpu_info *);
128 };
129
130 struct cpu_nocpuid_nameclass {
131 int cpu_vendor;
132 const char *cpu_vendorname;
133 const char *cpu_name;
134 int cpu_class;
135 void (*cpu_setup)(struct cpu_info *);
136 void (*cpu_cacheinfo)(struct cpu_info *);
137 void (*cpu_info)(struct cpu_info *);
138 };
139
140 struct cpu_cpuid_nameclass {
141 const char *cpu_id;
142 int cpu_vendor;
143 const char *cpu_vendorname;
144 struct cpu_cpuid_family {
145 int cpu_class;
146 const char *cpu_models[256];
147 const char *cpu_model_default;
148 void (*cpu_setup)(struct cpu_info *);
149 void (*cpu_probe)(struct cpu_info *);
150 void (*cpu_info)(struct cpu_info *);
151 } cpu_family[CPU_MAXFAMILY - CPU_MINFAMILY + 1];
152 };
153
154 static const struct x86_cache_info intel_cpuid_cache_info[] = INTEL_CACHE_INFO;
155
156 /*
157 * Map Brand ID from cpuid instruction to brand name.
158 * Source: Table 3-24, Mapping of Brand Indices; and Intel 64 and IA-32
159 * Processor Brand Strings, Chapter 3 in "Intel (R) 64 and IA-32
160 * Architectures Software Developer's Manual, Volume 2A".
161 */
162 static const char * const i386_intel_brand[] = {
163 "", /* Unsupported */
164 "Celeron", /* Intel (R) Celeron (TM) processor */
165 "Pentium III", /* Intel (R) Pentium (R) III processor */
166 "Pentium III Xeon", /* Intel (R) Pentium (R) III Xeon (TM) processor */
167 "Pentium III", /* Intel (R) Pentium (R) III processor */
168 "", /* 0x05: Reserved */
169 "Mobile Pentium III",/* Mobile Intel (R) Pentium (R) III processor-M */
170 "Mobile Celeron", /* Mobile Intel (R) Celeron (R) processor */
171 "Pentium 4", /* Intel (R) Pentium (R) 4 processor */
172 "Pentium 4", /* Intel (R) Pentium (R) 4 processor */
173 "Celeron", /* Intel (R) Celeron (TM) processor */
174 "Xeon", /* Intel (R) Xeon (TM) processor */
175 "Xeon MP", /* Intel (R) Xeon (TM) processor MP */
176 "", /* 0x0d: Reserved */
177 "Mobile Pentium 4", /* Mobile Intel (R) Pentium (R) 4 processor-M */
178 "Mobile Celeron", /* Mobile Intel (R) Celeron (R) processor */
179 "", /* 0x10: Reserved */
180 "Mobile Genuine", /* Moblie Genuine Intel (R) processor */
181 "Celeron M", /* Intel (R) Celeron (R) M processor */
182 "Mobile Celeron", /* Mobile Intel (R) Celeron (R) processor */
183 "Celeron", /* Intel (R) Celeron (R) processor */
184 "Mobile Genuine", /* Moblie Genuine Intel (R) processor */
185 "Pentium M", /* Intel (R) Pentium (R) M processor */
186 "Mobile Celeron", /* Mobile Intel (R) Celeron (R) processor */
187 };
188
189 /*
190 * AMD processors don't have Brand IDs, so we need these names for probe.
191 */
192 static const char * const amd_brand[] = {
193 "",
194 "Duron", /* AMD Duron(tm) */
195 "MP", /* AMD Athlon(tm) MP */
196 "XP", /* AMD Athlon(tm) XP */
197 "4" /* AMD Athlon(tm) 4 */
198 };
199
200 static int cpu_vendor;
201 static char cpu_brand_string[49];
202 static char amd_brand_name[48];
203 static int use_pae, largepagesize;
204
205 /* Setup functions */
206 static void disable_tsc(struct cpu_info *);
207 static void amd_family5_setup(struct cpu_info *);
208 static void cyrix6x86_cpu_setup(struct cpu_info *);
209 static void winchip_cpu_setup(struct cpu_info *);
210 /* Brand/Model name functions */
211 static const char *intel_family6_name(struct cpu_info *);
212 static const char *amd_amd64_name(struct cpu_info *);
213 /* Probe functions */
214 static void amd_family6_probe(struct cpu_info *);
215 static void powernow_probe(struct cpu_info *);
216 static void intel_family_new_probe(struct cpu_info *);
217 static void via_cpu_probe(struct cpu_info *);
218 /* (Cache) Info functions */
219 static void cpu_dcp_cacheinfo(struct cpu_info *, uint32_t);
220 static void intel_cpu_cacheinfo(struct cpu_info *);
221 static void amd_cpu_cacheinfo(struct cpu_info *);
222 static void via_cpu_cacheinfo(struct cpu_info *);
223 static void tmx86_get_longrun_status(u_int *, u_int *, u_int *);
224 static void transmeta_cpu_info(struct cpu_info *);
225 /* Common functions */
226 static void cpu_probe_base_features(struct cpu_info *, const char *);
227 static void cpu_probe_hv_features(struct cpu_info *, const char *);
228 static void cpu_probe_features(struct cpu_info *);
229 static void print_bits(const char *, const char *, const char *, uint32_t);
230 static void identifycpu_cpuids(struct cpu_info *);
231 static const struct x86_cache_info *cache_info_lookup(
232 const struct x86_cache_info *, uint8_t);
233 static const char *print_cache_config(struct cpu_info *, int, const char *,
234 const char *);
235 static const char *print_tlb_config(struct cpu_info *, int, const char *,
236 const char *);
237 static void x86_print_cache_and_tlb_info(struct cpu_info *);
238
239 /*
240 * Note: these are just the ones that may not have a cpuid instruction.
241 * We deal with the rest in a different way.
242 */
243 const struct cpu_nocpuid_nameclass i386_nocpuid_cpus[] = {
244 { CPUVENDOR_INTEL, "Intel", "386SX", CPUCLASS_386,
245 NULL, NULL, NULL }, /* CPU_386SX */
246 { CPUVENDOR_INTEL, "Intel", "386DX", CPUCLASS_386,
247 NULL, NULL, NULL }, /* CPU_386 */
248 { CPUVENDOR_INTEL, "Intel", "486SX", CPUCLASS_486,
249 NULL, NULL, NULL }, /* CPU_486SX */
250 { CPUVENDOR_INTEL, "Intel", "486DX", CPUCLASS_486,
251 NULL, NULL, NULL }, /* CPU_486 */
252 { CPUVENDOR_CYRIX, "Cyrix", "486DLC", CPUCLASS_486,
253 NULL, NULL, NULL }, /* CPU_486DLC */
254 { CPUVENDOR_CYRIX, "Cyrix", "6x86", CPUCLASS_486,
255 NULL, NULL, NULL }, /* CPU_6x86 */
256 { CPUVENDOR_NEXGEN,"NexGen","586", CPUCLASS_386,
257 NULL, NULL, NULL }, /* CPU_NX586 */
258 };
259
260 const char *classnames[] = {
261 "386",
262 "486",
263 "586",
264 "686"
265 };
266
267 const char *modifiers[] = {
268 "",
269 "OverDrive",
270 "Dual",
271 ""
272 };
273
274 const struct cpu_cpuid_nameclass i386_cpuid_cpus[] = {
275 {
276 /*
277 * For Intel processors, check Chapter 35Model-specific
278 * registers (MSRS), in "Intel (R) 64 and IA-32 Architectures
279 * Software Developer's Manual, Volume 3C".
280 */
281 "GenuineIntel",
282 CPUVENDOR_INTEL,
283 "Intel",
284 /* Family 4 */
285 { {
286 CPUCLASS_486,
287 {
288 "486DX", "486DX", "486SX", "486DX2", "486SL",
289 "486SX2", 0, "486DX2 W/B Enhanced",
290 "486DX4", 0, 0, 0, 0, 0, 0, 0,
291 },
292 "486", /* Default */
293 NULL,
294 NULL,
295 intel_cpu_cacheinfo,
296 },
297 /* Family 5 */
298 {
299 CPUCLASS_586,
300 {
301 "Pentium (P5 A-step)", "Pentium (P5)",
302 "Pentium (P54C)", "Pentium (P24T)",
303 "Pentium/MMX", "Pentium", 0,
304 "Pentium (P54C)", "Pentium/MMX (Tillamook)",
305 "Quark X1000", 0, 0, 0, 0, 0, 0,
306 },
307 "Pentium", /* Default */
308 NULL,
309 NULL,
310 intel_cpu_cacheinfo,
311 },
312 /* Family 6 */
313 {
314 CPUCLASS_686,
315 {
316 [0x00] = "Pentium Pro (A-step)",
317 [0x01] = "Pentium Pro",
318 [0x03] = "Pentium II (Klamath)",
319 [0x04] = "Pentium Pro",
320 [0x05] = "Pentium II/Celeron (Deschutes)",
321 [0x06] = "Celeron (Mendocino)",
322 [0x07] = "Pentium III (Katmai)",
323 [0x08] = "Pentium III (Coppermine)",
324 [0x09] = "Pentium M (Banias)",
325 [0x0a] = "Pentium III Xeon (Cascades)",
326 [0x0b] = "Pentium III (Tualatin)",
327 [0x0d] = "Pentium M (Dothan)",
328 [0x0e] = "Pentium Core Duo, Core solo",
329 [0x0f] = "Xeon 30xx, 32xx, 51xx, 53xx, 73xx, "
330 "Core 2 Quad 6xxx, "
331 "Core 2 Extreme 6xxx, "
332 "Core 2 Duo 4xxx, 5xxx, 6xxx, 7xxx "
333 "and Pentium DC",
334 [0x15] = "EP80579 Integrated Processor",
335 [0x16] = "Celeron (45nm)",
336 [0x17] = "Xeon 31xx, 33xx, 52xx, 54xx, "
337 "Core 2 Quad 8xxx and 9xxx",
338 [0x1a] = "Core i7, Xeon 34xx, 35xx and 55xx "
339 "(Nehalem)",
340 [0x1c] = "45nm Atom Family",
341 [0x1d] = "XeonMP 74xx (Nehalem)",
342 [0x1e] = "Core i7 and i5",
343 [0x1f] = "Core i7 and i5",
344 [0x25] = "Xeon 36xx & 56xx, i7, i5 and i3",
345 [0x26] = "Atom Family",
346 [0x27] = "Atom Family",
347 [0x2a] = "Xeon E3-12xx, 2nd gen i7, i5, "
348 "i3 2xxx",
349 [0x2c] = "Xeon 36xx & 56xx, i7, i5 and i3",
350 [0x2d] = "Xeon E5 Sandy Bridge family, "
351 "Core i7-39xx Extreme",
352 [0x2e] = "Xeon 75xx & 65xx",
353 [0x2f] = "Xeon E7 family",
354 [0x35] = "Atom Family",
355 [0x36] = "Atom S1000",
356 [0x37] = "Atom E3000, Z3[67]00",
357 [0x3a] = "Xeon E3-1200v2 and 3rd gen core, "
358 "Ivy Bridge",
359 [0x3c] = "4th gen Core, Xeon E3-12xx v3 "
360 "(Haswell)",
361 [0x3d] = "Core M-5xxx, 5th gen Core (Broadwell)",
362 [0x3e] = "Xeon E5/E7 v2 (Ivy Bridge-E), "
363 "Core i7-49xx Extreme",
364 [0x3f] = "Xeon E5-4600/2600/1600 v3, Xeon E7 v3 (Haswell-E), "
365 "Core i7-59xx Extreme",
366 [0x45] = "4th gen Core, Xeon E3-12xx v3 "
367 "(Haswell)",
368 [0x46] = "4th gen Core, Xeon E3-12xx v3 "
369 "(Haswell)",
370 [0x47] = "5th gen Core, Xeon E3-1200 v4 (Broadwell)",
371 [0x4a] = "Atom Z3400",
372 [0x4c] = "Atom X[57]-Z8000 (Airmont)",
373 [0x4d] = "Atom C2000",
374 [0x4e] = "6th gen Core, Xeon E3-1[25]00 v5 (Skylake)",
375 [0x4f] = "Xeon E[57] v4 (Broadwell), Core i7-69xx Extreme",
376 [0x55] = "Xeon Scalable (Skylake, Cascade Lake, Copper Lake)",
377 [0x56] = "Xeon D-1500 (Broadwell)",
378 [0x57] = "Xeon Phi [357]200 (Knights Landing)",
379 [0x5a] = "Atom E3500",
380 [0x5c] = "Atom (Goldmont)",
381 [0x5d] = "Atom X3-C3000 (Silvermont)",
382 [0x5e] = "6th gen Core, Xeon E3-1[25]00 v5 (Skylake)",
383 [0x5f] = "Atom (Goldmont, Denverton)",
384 [0x66] = "8th gen Core i3 (Cannon Lake)",
385 [0x6a] = "Future Xeon (Ice Lake)",
386 [0x6c] = "Future Xeon (Ice Lake)",
387 [0x7a] = "Atom (Goldmont Plus)",
388 [0x7d] = "10th gen Core (Ice Lake)",
389 [0x7e] = "10th gen Core (Ice Lake)",
390 [0x85] = "Xeon Phi 7215, 7285, 7295 (Knights Mill)",
391 [0x86] = "Atom (Tremont)",
392 [0x8e] = "7th or 8th gen Core (Kaby Lake, Coffee Lake) or Xeon E (Coffee Lake)",
393 [0x9e] = "7th or 8th gen Core (Kaby Lake, Coffee Lake) or Xeon E (Coffee Lake)",
394 },
395 "Pentium Pro, II or III", /* Default */
396 NULL,
397 intel_family_new_probe,
398 intel_cpu_cacheinfo,
399 },
400 /* Family > 6 */
401 {
402 CPUCLASS_686,
403 {
404 0, 0, 0, 0, 0, 0, 0, 0,
405 0, 0, 0, 0, 0, 0, 0, 0,
406 },
407 "Pentium 4", /* Default */
408 NULL,
409 intel_family_new_probe,
410 intel_cpu_cacheinfo,
411 } }
412 },
413 {
414 "AuthenticAMD",
415 CPUVENDOR_AMD,
416 "AMD",
417 /* Family 4 */
418 { {
419 CPUCLASS_486,
420 {
421 0, 0, 0, "Am486DX2 W/T",
422 0, 0, 0, "Am486DX2 W/B",
423 "Am486DX4 W/T or Am5x86 W/T 150",
424 "Am486DX4 W/B or Am5x86 W/B 150", 0, 0,
425 0, 0, "Am5x86 W/T 133/160",
426 "Am5x86 W/B 133/160",
427 },
428 "Am486 or Am5x86", /* Default */
429 NULL,
430 NULL,
431 NULL,
432 },
433 /* Family 5 */
434 {
435 CPUCLASS_586,
436 {
437 "K5", "K5", "K5", "K5", 0, 0, "K6",
438 "K6", "K6-2", "K6-III", "Geode LX", 0, 0,
439 "K6-2+/III+", 0, 0,
440 },
441 "K5 or K6", /* Default */
442 amd_family5_setup,
443 NULL,
444 amd_cpu_cacheinfo,
445 },
446 /* Family 6 */
447 {
448 CPUCLASS_686,
449 {
450 0, "Athlon Model 1", "Athlon Model 2",
451 "Duron", "Athlon Model 4 (Thunderbird)",
452 0, "Athlon", "Duron", "Athlon", 0,
453 "Athlon", 0, 0, 0, 0, 0,
454 },
455 "K7 (Athlon)", /* Default */
456 NULL,
457 amd_family6_probe,
458 amd_cpu_cacheinfo,
459 },
460 /* Family > 6 */
461 {
462 CPUCLASS_686,
463 {
464 0, 0, 0, 0, 0, 0, 0, 0,
465 0, 0, 0, 0, 0, 0, 0, 0,
466 },
467 "Unknown K8 (Athlon)", /* Default */
468 NULL,
469 amd_family6_probe,
470 amd_cpu_cacheinfo,
471 } }
472 },
473 {
474 "CyrixInstead",
475 CPUVENDOR_CYRIX,
476 "Cyrix",
477 /* Family 4 */
478 { {
479 CPUCLASS_486,
480 {
481 0, 0, 0,
482 "MediaGX",
483 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
484 },
485 "486", /* Default */
486 cyrix6x86_cpu_setup, /* XXX ?? */
487 NULL,
488 NULL,
489 },
490 /* Family 5 */
491 {
492 CPUCLASS_586,
493 {
494 0, 0, "6x86", 0,
495 "MMX-enhanced MediaGX (GXm)", /* or Geode? */
496 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
497 },
498 "6x86", /* Default */
499 cyrix6x86_cpu_setup,
500 NULL,
501 NULL,
502 },
503 /* Family 6 */
504 {
505 CPUCLASS_686,
506 {
507 "6x86MX", 0, 0, 0, 0, 0, 0, 0,
508 0, 0, 0, 0, 0, 0, 0, 0,
509 },
510 "6x86MX", /* Default */
511 cyrix6x86_cpu_setup,
512 NULL,
513 NULL,
514 },
515 /* Family > 6 */
516 {
517 CPUCLASS_686,
518 {
519 0, 0, 0, 0, 0, 0, 0, 0,
520 0, 0, 0, 0, 0, 0, 0, 0,
521 },
522 "Unknown 6x86MX", /* Default */
523 NULL,
524 NULL,
525 NULL,
526 } }
527 },
528 { /* MediaGX is now owned by National Semiconductor */
529 "Geode by NSC",
530 CPUVENDOR_CYRIX, /* XXX */
531 "National Semiconductor",
532 /* Family 4, NSC never had any of these */
533 { {
534 CPUCLASS_486,
535 {
536 0, 0, 0, 0, 0, 0, 0, 0,
537 0, 0, 0, 0, 0, 0, 0, 0,
538 },
539 "486 compatible", /* Default */
540 NULL,
541 NULL,
542 NULL,
543 },
544 /* Family 5: Geode family, formerly MediaGX */
545 {
546 CPUCLASS_586,
547 {
548 0, 0, 0, 0,
549 "Geode GX1",
550 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
551 },
552 "Geode", /* Default */
553 cyrix6x86_cpu_setup,
554 NULL,
555 amd_cpu_cacheinfo,
556 },
557 /* Family 6, not yet available from NSC */
558 {
559 CPUCLASS_686,
560 {
561 0, 0, 0, 0, 0, 0, 0, 0,
562 0, 0, 0, 0, 0, 0, 0, 0,
563 },
564 "Pentium Pro compatible", /* Default */
565 NULL,
566 NULL,
567 NULL,
568 },
569 /* Family > 6, not yet available from NSC */
570 {
571 CPUCLASS_686,
572 {
573 0, 0, 0, 0, 0, 0, 0, 0,
574 0, 0, 0, 0, 0, 0, 0, 0,
575 },
576 "Pentium Pro compatible", /* Default */
577 NULL,
578 NULL,
579 NULL,
580 } }
581 },
582 {
583 "CentaurHauls",
584 CPUVENDOR_IDT,
585 "IDT",
586 /* Family 4, IDT never had any of these */
587 { {
588 CPUCLASS_486,
589 {
590 0, 0, 0, 0, 0, 0, 0, 0,
591 0, 0, 0, 0, 0, 0, 0, 0,
592 },
593 "486 compatible", /* Default */
594 NULL,
595 NULL,
596 NULL,
597 },
598 /* Family 5 */
599 {
600 CPUCLASS_586,
601 {
602 0, 0, 0, 0, "WinChip C6", 0, 0, 0,
603 "WinChip 2", "WinChip 3", 0, 0, 0, 0, 0, 0,
604 },
605 "WinChip", /* Default */
606 winchip_cpu_setup,
607 NULL,
608 NULL,
609 },
610 /* Family 6, VIA acquired IDT Centaur design subsidiary */
611 {
612 CPUCLASS_686,
613 {
614 0, 0, 0, 0, 0, 0, "C3 Samuel",
615 "C3 Samuel 2/Ezra", "C3 Ezra-T",
616 "C3 Nehemiah", "C7 Esther", 0, 0, "C7 Esther",
617 0, "VIA Nano",
618 },
619 "Unknown VIA/IDT", /* Default */
620 NULL,
621 via_cpu_probe,
622 via_cpu_cacheinfo,
623 },
624 /* Family > 6, not yet available from VIA */
625 {
626 CPUCLASS_686,
627 {
628 0, 0, 0, 0, 0, 0, 0, 0,
629 0, 0, 0, 0, 0, 0, 0, 0,
630 },
631 "Pentium Pro compatible", /* Default */
632 NULL,
633 NULL,
634 NULL,
635 } }
636 },
637 {
638 "GenuineTMx86",
639 CPUVENDOR_TRANSMETA,
640 "Transmeta",
641 /* Family 4, Transmeta never had any of these */
642 { {
643 CPUCLASS_486,
644 {
645 0, 0, 0, 0, 0, 0, 0, 0,
646 0, 0, 0, 0, 0, 0, 0, 0,
647 },
648 "486 compatible", /* Default */
649 NULL,
650 NULL,
651 NULL,
652 },
653 /* Family 5 */
654 {
655 CPUCLASS_586,
656 {
657 0, 0, 0, 0, 0, 0, 0, 0,
658 0, 0, 0, 0, 0, 0, 0, 0,
659 },
660 "Crusoe", /* Default */
661 NULL,
662 NULL,
663 transmeta_cpu_info,
664 },
665 /* Family 6, not yet available from Transmeta */
666 {
667 CPUCLASS_686,
668 {
669 0, 0, 0, 0, 0, 0, 0, 0,
670 0, 0, 0, 0, 0, 0, 0, 0,
671 },
672 "Pentium Pro compatible", /* Default */
673 NULL,
674 NULL,
675 NULL,
676 },
677 /* Family > 6, not yet available from Transmeta */
678 {
679 CPUCLASS_686,
680 {
681 0, 0, 0, 0, 0, 0, 0, 0,
682 0, 0, 0, 0, 0, 0, 0, 0,
683 },
684 "Pentium Pro compatible", /* Default */
685 NULL,
686 NULL,
687 NULL,
688 } }
689 }
690 };
691
692 /*
693 * disable the TSC such that we don't use the TSC in microtime(9)
694 * because some CPUs got the implementation wrong.
695 */
696 static void
697 disable_tsc(struct cpu_info *ci)
698 {
699 if (ci->ci_feat_val[0] & CPUID_TSC) {
700 ci->ci_feat_val[0] &= ~CPUID_TSC;
701 aprint_error("WARNING: broken TSC disabled\n");
702 }
703 }
704
705 static void
706 amd_family5_setup(struct cpu_info *ci)
707 {
708
709 switch (ci->ci_model) {
710 case 0: /* AMD-K5 Model 0 */
711 /*
712 * According to the AMD Processor Recognition App Note,
713 * the AMD-K5 Model 0 uses the wrong bit to indicate
714 * support for global PTEs, instead using bit 9 (APIC)
715 * rather than bit 13 (i.e. "0x200" vs. 0x2000". Oops!).
716 */
717 if (ci->ci_feat_val[0] & CPUID_APIC)
718 ci->ci_feat_val[0] =
719 (ci->ci_feat_val[0] & ~CPUID_APIC) | CPUID_PGE;
720 /*
721 * XXX But pmap_pg_g is already initialized -- need to kick
722 * XXX the pmap somehow. How does the MP branch do this?
723 */
724 break;
725 }
726 }
727
728 static void
729 cyrix6x86_cpu_setup(struct cpu_info *ci)
730 {
731
732 /*
733 * Do not disable the TSC on the Geode GX, it's reported to
734 * work fine.
735 */
736 if (ci->ci_signature != 0x552)
737 disable_tsc(ci);
738 }
739
740 static void
741 winchip_cpu_setup(struct cpu_info *ci)
742 {
743 switch (ci->ci_model) {
744 case 4: /* WinChip C6 */
745 disable_tsc(ci);
746 }
747 }
748
749
750 static const char *
751 intel_family6_name(struct cpu_info *ci)
752 {
753 const char *ret = NULL;
754 u_int l2cache = ci->ci_cinfo[CAI_L2CACHE].cai_totalsize;
755
756 if (ci->ci_model == 5) {
757 switch (l2cache) {
758 case 0:
759 case 128 * 1024:
760 ret = "Celeron (Covington)";
761 break;
762 case 256 * 1024:
763 ret = "Mobile Pentium II (Dixon)";
764 break;
765 case 512 * 1024:
766 ret = "Pentium II";
767 break;
768 case 1 * 1024 * 1024:
769 case 2 * 1024 * 1024:
770 ret = "Pentium II Xeon";
771 break;
772 }
773 } else if (ci->ci_model == 6) {
774 switch (l2cache) {
775 case 256 * 1024:
776 case 512 * 1024:
777 ret = "Mobile Pentium II";
778 break;
779 }
780 } else if (ci->ci_model == 7) {
781 switch (l2cache) {
782 case 512 * 1024:
783 ret = "Pentium III";
784 break;
785 case 1 * 1024 * 1024:
786 case 2 * 1024 * 1024:
787 ret = "Pentium III Xeon";
788 break;
789 }
790 } else if (ci->ci_model >= 8) {
791 if (ci->ci_brand_id && ci->ci_brand_id < 0x10) {
792 switch (ci->ci_brand_id) {
793 case 0x3:
794 if (ci->ci_signature == 0x6B1)
795 ret = "Celeron";
796 break;
797 case 0x8:
798 if (ci->ci_signature >= 0xF13)
799 ret = "genuine processor";
800 break;
801 case 0xB:
802 if (ci->ci_signature >= 0xF13)
803 ret = "Xeon MP";
804 break;
805 case 0xE:
806 if (ci->ci_signature < 0xF13)
807 ret = "Xeon";
808 break;
809 }
810 if (ret == NULL)
811 ret = i386_intel_brand[ci->ci_brand_id];
812 }
813 }
814
815 return ret;
816 }
817
818 /*
819 * Identify AMD64 CPU names from cpuid.
820 *
821 * Based on:
822 * "Revision Guide for AMD Athlon 64 and AMD Opteron Processors"
823 * http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/25759.pdf
824 * "Revision Guide for AMD NPT Family 0Fh Processors"
825 * http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/33610.pdf
826 * and other miscellaneous reports.
827 *
828 * This is all rather pointless, these are cross 'brand' since the raw
829 * silicon is shared.
830 */
831 static const char *
832 amd_amd64_name(struct cpu_info *ci)
833 {
834 static char family_str[32];
835
836 /* Only called if family >= 15 */
837
838 switch (ci->ci_family) {
839 case 15:
840 switch (ci->ci_model) {
841 case 0x21: /* rev JH-E1/E6 */
842 case 0x41: /* rev JH-F2 */
843 return "Dual-Core Opteron";
844 case 0x23: /* rev JH-E6 (Toledo) */
845 return "Dual-Core Opteron or Athlon 64 X2";
846 case 0x43: /* rev JH-F2 (Windsor) */
847 return "Athlon 64 FX or Athlon 64 X2";
848 case 0x24: /* rev SH-E5 (Lancaster?) */
849 return "Mobile Athlon 64 or Turion 64";
850 case 0x05: /* rev SH-B0/B3/C0/CG (SledgeHammer?) */
851 return "Opteron or Athlon 64 FX";
852 case 0x15: /* rev SH-D0 */
853 case 0x25: /* rev SH-E4 */
854 return "Opteron";
855 case 0x27: /* rev DH-E4, SH-E4 */
856 return "Athlon 64 or Athlon 64 FX or Opteron";
857 case 0x48: /* rev BH-F2 */
858 return "Turion 64 X2";
859 case 0x04: /* rev SH-B0/C0/CG (ClawHammer) */
860 case 0x07: /* rev SH-CG (ClawHammer) */
861 case 0x0b: /* rev CH-CG */
862 case 0x14: /* rev SH-D0 */
863 case 0x17: /* rev SH-D0 */
864 case 0x1b: /* rev CH-D0 */
865 return "Athlon 64";
866 case 0x2b: /* rev BH-E4 (Manchester) */
867 case 0x4b: /* rev BH-F2 (Windsor) */
868 return "Athlon 64 X2";
869 case 0x6b: /* rev BH-G1 (Brisbane) */
870 return "Athlon X2 or Athlon 64 X2";
871 case 0x08: /* rev CH-CG */
872 case 0x0c: /* rev DH-CG (Newcastle) */
873 case 0x0e: /* rev DH-CG (Newcastle?) */
874 case 0x0f: /* rev DH-CG (Newcastle/Paris) */
875 case 0x18: /* rev CH-D0 */
876 case 0x1c: /* rev DH-D0 (Winchester) */
877 case 0x1f: /* rev DH-D0 (Winchester/Victoria) */
878 case 0x2c: /* rev DH-E3/E6 */
879 case 0x2f: /* rev DH-E3/E6 (Venice/Palermo) */
880 case 0x4f: /* rev DH-F2 (Orleans/Manila) */
881 case 0x5f: /* rev DH-F2 (Orleans/Manila) */
882 case 0x6f: /* rev DH-G1 */
883 return "Athlon 64 or Sempron";
884 default:
885 break;
886 }
887 return "Unknown AMD64 CPU";
888
889 #if 0
890 case 16:
891 return "Family 10h";
892 case 17:
893 return "Family 11h";
894 case 18:
895 return "Family 12h";
896 case 19:
897 return "Family 14h";
898 case 20:
899 return "Family 15h";
900 #endif
901
902 default:
903 break;
904 }
905
906 snprintf(family_str, sizeof family_str, "Family %xh", ci->ci_family);
907 return family_str;
908 }
909
910 static void
911 intel_family_new_probe(struct cpu_info *ci)
912 {
913 uint32_t descs[4];
914
915 x86_cpuid(0x80000000, descs);
916
917 /*
918 * Determine extended feature flags.
919 */
920 if (descs[0] >= 0x80000001) {
921 x86_cpuid(0x80000001, descs);
922 ci->ci_feat_val[2] |= descs[3];
923 ci->ci_feat_val[3] |= descs[2];
924 }
925 }
926
927 static void
928 via_cpu_probe(struct cpu_info *ci)
929 {
930 u_int stepping = CPUID_TO_STEPPING(ci->ci_signature);
931 u_int descs[4];
932 u_int lfunc;
933
934 /*
935 * Determine the largest extended function value.
936 */
937 x86_cpuid(0x80000000, descs);
938 lfunc = descs[0];
939
940 /*
941 * Determine the extended feature flags.
942 */
943 if (lfunc >= 0x80000001) {
944 x86_cpuid(0x80000001, descs);
945 ci->ci_feat_val[2] |= descs[3];
946 }
947
948 if (ci->ci_model < 0x9 || (ci->ci_model == 0x9 && stepping < 3))
949 return;
950
951 /* Nehemiah or Esther */
952 x86_cpuid(0xc0000000, descs);
953 lfunc = descs[0];
954 if (lfunc < 0xc0000001) /* no ACE, no RNG */
955 return;
956
957 x86_cpuid(0xc0000001, descs);
958 lfunc = descs[3];
959 ci->ci_feat_val[4] = lfunc;
960 }
961
962 static void
963 amd_family6_probe(struct cpu_info *ci)
964 {
965 uint32_t descs[4];
966 char *p;
967 size_t i;
968
969 x86_cpuid(0x80000000, descs);
970
971 /*
972 * Determine the extended feature flags.
973 */
974 if (descs[0] >= 0x80000001) {
975 x86_cpuid(0x80000001, descs);
976 ci->ci_feat_val[2] |= descs[3]; /* %edx */
977 ci->ci_feat_val[3] = descs[2]; /* %ecx */
978 }
979
980 if (*cpu_brand_string == '\0')
981 return;
982
983 for (i = 1; i < __arraycount(amd_brand); i++)
984 if ((p = strstr(cpu_brand_string, amd_brand[i])) != NULL) {
985 ci->ci_brand_id = i;
986 strlcpy(amd_brand_name, p, sizeof(amd_brand_name));
987 break;
988 }
989 }
990
991 /*
992 * Get cache info from one of the following:
993 * Intel Deterministic Cache Parameter Leaf (0x04)
994 * AMD Cache Topology Information Leaf (0x8000001d)
995 */
996 static void
997 cpu_dcp_cacheinfo(struct cpu_info *ci, uint32_t leaf)
998 {
999 u_int descs[4];
1000 int type, level, ways, partitions, linesize, sets, totalsize;
1001 int caitype = -1;
1002 int i;
1003
1004 for (i = 0; ; i++) {
1005 x86_cpuid2(leaf, i, descs);
1006 type = __SHIFTOUT(descs[0], CPUID_DCP_CACHETYPE);
1007 if (type == CPUID_DCP_CACHETYPE_N)
1008 break;
1009 level = __SHIFTOUT(descs[0], CPUID_DCP_CACHELEVEL);
1010 switch (level) {
1011 case 1:
1012 if (type == CPUID_DCP_CACHETYPE_I)
1013 caitype = CAI_ICACHE;
1014 else if (type == CPUID_DCP_CACHETYPE_D)
1015 caitype = CAI_DCACHE;
1016 else
1017 caitype = -1;
1018 break;
1019 case 2:
1020 if (type == CPUID_DCP_CACHETYPE_U)
1021 caitype = CAI_L2CACHE;
1022 else
1023 caitype = -1;
1024 break;
1025 case 3:
1026 if (type == CPUID_DCP_CACHETYPE_U)
1027 caitype = CAI_L3CACHE;
1028 else
1029 caitype = -1;
1030 break;
1031 default:
1032 caitype = -1;
1033 break;
1034 }
1035 if (caitype == -1) {
1036 aprint_error_dev(ci->ci_dev,
1037 "error: unknown cache level&type (%d & %d)\n",
1038 level, type);
1039 continue;
1040 }
1041 ways = __SHIFTOUT(descs[1], CPUID_DCP_WAYS) + 1;
1042 partitions =__SHIFTOUT(descs[1], CPUID_DCP_PARTITIONS)
1043 + 1;
1044 linesize = __SHIFTOUT(descs[1], CPUID_DCP_LINESIZE)
1045 + 1;
1046 sets = descs[2] + 1;
1047 totalsize = ways * partitions * linesize * sets;
1048 ci->ci_cinfo[caitype].cai_totalsize = totalsize;
1049 ci->ci_cinfo[caitype].cai_associativity = ways;
1050 ci->ci_cinfo[caitype].cai_linesize = linesize;
1051 }
1052 }
1053
1054 static void
1055 intel_cpu_cacheinfo(struct cpu_info *ci)
1056 {
1057 const struct x86_cache_info *cai;
1058 u_int descs[4];
1059 int iterations, i, j;
1060 int type, level, ways, linesize, sets;
1061 int caitype = -1;
1062 uint8_t desc;
1063
1064 /* Return if the cpu is old pre-cpuid instruction cpu */
1065 if (ci->ci_cpu_type >= 0)
1066 return;
1067
1068 if (ci->ci_cpuid_level < 2)
1069 return;
1070
1071 /*
1072 * Parse the cache info from `cpuid leaf 2', if we have it.
1073 * XXX This is kinda ugly, but hey, so is the architecture...
1074 */
1075 x86_cpuid(2, descs);
1076 iterations = descs[0] & 0xff;
1077 while (iterations-- > 0) {
1078 for (i = 0; i < 4; i++) {
1079 if (descs[i] & 0x80000000)
1080 continue;
1081 for (j = 0; j < 4; j++) {
1082 /*
1083 * The least significant byte in EAX
1084 * ((desc[0] >> 0) & 0xff) is always 0x01 and
1085 * it should be ignored.
1086 */
1087 if (i == 0 && j == 0)
1088 continue;
1089 desc = (descs[i] >> (j * 8)) & 0xff;
1090 if (desc == 0)
1091 continue;
1092 cai = cache_info_lookup(intel_cpuid_cache_info,
1093 desc);
1094 if (cai != NULL)
1095 ci->ci_cinfo[cai->cai_index] = *cai;
1096 else if ((verbose != 0) && (desc != 0xff)
1097 && (desc != 0xfe))
1098 aprint_error_dev(ci->ci_dev, "error:"
1099 " Unknown cacheinfo desc %02x\n",
1100 desc);
1101 }
1102 }
1103 x86_cpuid(2, descs);
1104 }
1105
1106 if (ci->ci_cpuid_level < 4)
1107 return;
1108
1109 /* Parse the cache info from `cpuid leaf 4', if we have it. */
1110 cpu_dcp_cacheinfo(ci, 4);
1111
1112 if (ci->ci_cpuid_level < 0x18)
1113 return;
1114 /* Parse the TLB info from `cpuid leaf 18H', if we have it. */
1115 x86_cpuid(0x18, descs);
1116 iterations = descs[0];
1117 for (i = 0; i <= iterations; i++) {
1118 uint32_t pgsize;
1119 bool full;
1120
1121 x86_cpuid2(0x18, i, descs);
1122 type = __SHIFTOUT(descs[3], CPUID_DATP_TCTYPE);
1123 if (type == CPUID_DATP_TCTYPE_N)
1124 continue;
1125 level = __SHIFTOUT(descs[3], CPUID_DATP_TCLEVEL);
1126 pgsize = __SHIFTOUT(descs[1], CPUID_DATP_PGSIZE);
1127 switch (level) {
1128 case 1:
1129 if (type == CPUID_DATP_TCTYPE_I) {
1130 switch (pgsize) {
1131 case CPUID_DATP_PGSIZE_4KB:
1132 caitype = CAI_ITLB;
1133 break;
1134 case CPUID_DATP_PGSIZE_2MB
1135 | CPUID_DATP_PGSIZE_4MB:
1136 caitype = CAI_ITLB2;
1137 break;
1138 case CPUID_DATP_PGSIZE_1GB:
1139 caitype = CAI_L1_1GBITLB;
1140 break;
1141 default:
1142 aprint_error_dev(ci->ci_dev,
1143 "error: unknown ITLB size (%d)\n",
1144 pgsize);
1145 caitype = CAI_ITLB;
1146 break;
1147 }
1148 } else if (type == CPUID_DATP_TCTYPE_D) {
1149 switch (pgsize) {
1150 case CPUID_DATP_PGSIZE_4KB:
1151 caitype = CAI_DTLB;
1152 break;
1153 case CPUID_DATP_PGSIZE_2MB
1154 | CPUID_DATP_PGSIZE_4MB:
1155 caitype = CAI_DTLB2;
1156 break;
1157 case CPUID_DATP_PGSIZE_1GB:
1158 caitype = CAI_L1_1GBDTLB;
1159 break;
1160 default:
1161 aprint_error_dev(ci->ci_dev,
1162 "error: unknown DTLB size (%d)\n",
1163 pgsize);
1164 caitype = CAI_DTLB;
1165 break;
1166 }
1167 } else
1168 caitype = -1;
1169 break;
1170 case 2:
1171 if (type == CPUID_DATP_TCTYPE_I)
1172 caitype = CAI_L2_ITLB;
1173 else if (type == CPUID_DATP_TCTYPE_D)
1174 caitype = CAI_L2_DTLB;
1175 else if (type == CPUID_DATP_TCTYPE_U) {
1176 switch (pgsize) {
1177 case CPUID_DATP_PGSIZE_4KB:
1178 caitype = CAI_L2_STLB;
1179 break;
1180 case CPUID_DATP_PGSIZE_4KB
1181 | CPUID_DATP_PGSIZE_2MB:
1182 caitype = CAI_L2_STLB2;
1183 break;
1184 case CPUID_DATP_PGSIZE_2MB
1185 | CPUID_DATP_PGSIZE_4MB:
1186 caitype = CAI_L2_STLB3;
1187 break;
1188 default:
1189 aprint_error_dev(ci->ci_dev,
1190 "error: unknown L2 STLB size (%d)\n",
1191 pgsize);
1192 caitype = CAI_DTLB;
1193 break;
1194 }
1195 } else
1196 caitype = -1;
1197 break;
1198 case 3:
1199 /* XXX need work for L3 TLB */
1200 caitype = CAI_L3CACHE;
1201 break;
1202 default:
1203 caitype = -1;
1204 break;
1205 }
1206 if (caitype == -1) {
1207 aprint_error_dev(ci->ci_dev,
1208 "error: unknown TLB level&type (%d & %d)\n",
1209 level, type);
1210 continue;
1211 }
1212 switch (pgsize) {
1213 case CPUID_DATP_PGSIZE_4KB:
1214 linesize = 4 * 1024;
1215 break;
1216 case CPUID_DATP_PGSIZE_2MB:
1217 linesize = 2 * 1024 * 1024;
1218 break;
1219 case CPUID_DATP_PGSIZE_4MB:
1220 linesize = 4 * 1024 * 1024;
1221 break;
1222 case CPUID_DATP_PGSIZE_1GB:
1223 linesize = 1024 * 1024 * 1024;
1224 break;
1225 case CPUID_DATP_PGSIZE_2MB | CPUID_DATP_PGSIZE_4MB:
1226 aprint_error_dev(ci->ci_dev,
1227 "WARINING: Currently 2M/4M info can't print correctly\n");
1228 linesize = 4 * 1024 * 1024;
1229 break;
1230 default:
1231 aprint_error_dev(ci->ci_dev,
1232 "error: Unknown size combination\n");
1233 linesize = 4 * 1024;
1234 break;
1235 }
1236 ways = __SHIFTOUT(descs[1], CPUID_DATP_WAYS);
1237 sets = descs[2];
1238 full = descs[3] & CPUID_DATP_FULLASSOC;
1239 ci->ci_cinfo[caitype].cai_totalsize
1240 = ways * sets; /* entries */
1241 ci->ci_cinfo[caitype].cai_associativity
1242 = full ? 0xff : ways;
1243 ci->ci_cinfo[caitype].cai_linesize = linesize; /* pg size */
1244 }
1245 }
1246
1247 static const struct x86_cache_info amd_cpuid_l2l3cache_assoc_info[] =
1248 AMD_L2L3CACHE_INFO;
1249
1250 static void
1251 amd_cpu_cacheinfo(struct cpu_info *ci)
1252 {
1253 const struct x86_cache_info *cp;
1254 struct x86_cache_info *cai;
1255 u_int descs[4];
1256 u_int lfunc;
1257
1258 /* K5 model 0 has none of this info. */
1259 if (ci->ci_family == 5 && ci->ci_model == 0)
1260 return;
1261
1262 /* Determine the largest extended function value. */
1263 x86_cpuid(0x80000000, descs);
1264 lfunc = descs[0];
1265
1266 if (lfunc < 0x80000005)
1267 return;
1268
1269 /* Determine L1 cache/TLB info. */
1270 x86_cpuid(0x80000005, descs);
1271
1272 /* K6-III and higher have large page TLBs. */
1273 if ((ci->ci_family == 5 && ci->ci_model >= 9) || ci->ci_family >= 6) {
1274 cai = &ci->ci_cinfo[CAI_ITLB2];
1275 cai->cai_totalsize = AMD_L1_EAX_ITLB_ENTRIES(descs[0]);
1276 cai->cai_associativity = AMD_L1_EAX_ITLB_ASSOC(descs[0]);
1277 cai->cai_linesize = largepagesize;
1278
1279 cai = &ci->ci_cinfo[CAI_DTLB2];
1280 cai->cai_totalsize = AMD_L1_EAX_DTLB_ENTRIES(descs[0]);
1281 cai->cai_associativity = AMD_L1_EAX_DTLB_ASSOC(descs[0]);
1282 cai->cai_linesize = largepagesize;
1283 }
1284
1285 cai = &ci->ci_cinfo[CAI_ITLB];
1286 cai->cai_totalsize = AMD_L1_EBX_ITLB_ENTRIES(descs[1]);
1287 cai->cai_associativity = AMD_L1_EBX_ITLB_ASSOC(descs[1]);
1288 cai->cai_linesize = (4 * 1024);
1289
1290 cai = &ci->ci_cinfo[CAI_DTLB];
1291 cai->cai_totalsize = AMD_L1_EBX_DTLB_ENTRIES(descs[1]);
1292 cai->cai_associativity = AMD_L1_EBX_DTLB_ASSOC(descs[1]);
1293 cai->cai_linesize = (4 * 1024);
1294
1295 cai = &ci->ci_cinfo[CAI_DCACHE];
1296 cai->cai_totalsize = AMD_L1_ECX_DC_SIZE(descs[2]);
1297 cai->cai_associativity = AMD_L1_ECX_DC_ASSOC(descs[2]);
1298 cai->cai_linesize = AMD_L1_ECX_DC_LS(descs[2]);
1299
1300 cai = &ci->ci_cinfo[CAI_ICACHE];
1301 cai->cai_totalsize = AMD_L1_EDX_IC_SIZE(descs[3]);
1302 cai->cai_associativity = AMD_L1_EDX_IC_ASSOC(descs[3]);
1303 cai->cai_linesize = AMD_L1_EDX_IC_LS(descs[3]);
1304
1305 if (lfunc < 0x80000006)
1306 return;
1307
1308 /* Determine L2 cache/TLB info. */
1309 x86_cpuid(0x80000006, descs);
1310
1311 cai = &ci->ci_cinfo[CAI_L2_ITLB];
1312 cai->cai_totalsize = AMD_L2_EBX_IUTLB_ENTRIES(descs[1]);
1313 cai->cai_associativity = AMD_L2_EBX_IUTLB_ASSOC(descs[1]);
1314 cai->cai_linesize = (4 * 1024);
1315 cp = cache_info_lookup(amd_cpuid_l2l3cache_assoc_info,
1316 cai->cai_associativity);
1317 if (cp != NULL)
1318 cai->cai_associativity = cp->cai_associativity;
1319 else
1320 cai->cai_associativity = 0; /* XXX Unknown/reserved */
1321
1322 cai = &ci->ci_cinfo[CAI_L2_ITLB2];
1323 cai->cai_totalsize = AMD_L2_EAX_IUTLB_ENTRIES(descs[0]);
1324 cai->cai_associativity = AMD_L2_EAX_IUTLB_ASSOC(descs[0]);
1325 cai->cai_linesize = largepagesize;
1326 cp = cache_info_lookup(amd_cpuid_l2l3cache_assoc_info,
1327 cai->cai_associativity);
1328 if (cp != NULL)
1329 cai->cai_associativity = cp->cai_associativity;
1330 else
1331 cai->cai_associativity = 0; /* XXX Unknown/reserved */
1332
1333 cai = &ci->ci_cinfo[CAI_L2_DTLB];
1334 cai->cai_totalsize = AMD_L2_EBX_DTLB_ENTRIES(descs[1]);
1335 cai->cai_associativity = AMD_L2_EBX_DTLB_ASSOC(descs[1]);
1336 cai->cai_linesize = (4 * 1024);
1337 cp = cache_info_lookup(amd_cpuid_l2l3cache_assoc_info,
1338 cai->cai_associativity);
1339 if (cp != NULL)
1340 cai->cai_associativity = cp->cai_associativity;
1341 else
1342 cai->cai_associativity = 0; /* XXX Unknown/reserved */
1343
1344 cai = &ci->ci_cinfo[CAI_L2_DTLB2];
1345 cai->cai_totalsize = AMD_L2_EAX_DTLB_ENTRIES(descs[0]);
1346 cai->cai_associativity = AMD_L2_EAX_DTLB_ASSOC(descs[0]);
1347 cai->cai_linesize = largepagesize;
1348 cp = cache_info_lookup(amd_cpuid_l2l3cache_assoc_info,
1349 cai->cai_associativity);
1350 if (cp != NULL)
1351 cai->cai_associativity = cp->cai_associativity;
1352 else
1353 cai->cai_associativity = 0; /* XXX Unknown/reserved */
1354
1355 cai = &ci->ci_cinfo[CAI_L2CACHE];
1356 cai->cai_totalsize = AMD_L2_ECX_C_SIZE(descs[2]);
1357 cai->cai_associativity = AMD_L2_ECX_C_ASSOC(descs[2]);
1358 cai->cai_linesize = AMD_L2_ECX_C_LS(descs[2]);
1359
1360 cp = cache_info_lookup(amd_cpuid_l2l3cache_assoc_info,
1361 cai->cai_associativity);
1362 if (cp != NULL)
1363 cai->cai_associativity = cp->cai_associativity;
1364 else
1365 cai->cai_associativity = 0; /* XXX Unknown/reserved */
1366
1367 /* Determine L3 cache info on AMD Family 10h and newer processors */
1368 if (ci->ci_family >= 0x10) {
1369 cai = &ci->ci_cinfo[CAI_L3CACHE];
1370 cai->cai_totalsize = AMD_L3_EDX_C_SIZE(descs[3]);
1371 cai->cai_associativity = AMD_L3_EDX_C_ASSOC(descs[3]);
1372 cai->cai_linesize = AMD_L3_EDX_C_LS(descs[3]);
1373
1374 cp = cache_info_lookup(amd_cpuid_l2l3cache_assoc_info,
1375 cai->cai_associativity);
1376 if (cp != NULL)
1377 cai->cai_associativity = cp->cai_associativity;
1378 else
1379 cai->cai_associativity = 0; /* XXX Unkn/Rsvd */
1380 }
1381
1382 if (lfunc < 0x80000019)
1383 return;
1384
1385 /* Determine 1GB TLB info. */
1386 x86_cpuid(0x80000019, descs);
1387
1388 cai = &ci->ci_cinfo[CAI_L1_1GBITLB];
1389 cai->cai_totalsize = AMD_L1_1GB_EAX_IUTLB_ENTRIES(descs[0]);
1390 cai->cai_associativity = AMD_L1_1GB_EAX_IUTLB_ASSOC(descs[0]);
1391 cai->cai_linesize = (1024 * 1024 * 1024);
1392 cp = cache_info_lookup(amd_cpuid_l2l3cache_assoc_info,
1393 cai->cai_associativity);
1394 if (cp != NULL)
1395 cai->cai_associativity = cp->cai_associativity;
1396 else
1397 cai->cai_associativity = 0; /* XXX Unknown/reserved */
1398
1399 cai = &ci->ci_cinfo[CAI_L1_1GBDTLB];
1400 cai->cai_totalsize = AMD_L1_1GB_EAX_DTLB_ENTRIES(descs[0]);
1401 cai->cai_associativity = AMD_L1_1GB_EAX_DTLB_ASSOC(descs[0]);
1402 cai->cai_linesize = (1024 * 1024 * 1024);
1403 cp = cache_info_lookup(amd_cpuid_l2l3cache_assoc_info,
1404 cai->cai_associativity);
1405 if (cp != NULL)
1406 cai->cai_associativity = cp->cai_associativity;
1407 else
1408 cai->cai_associativity = 0; /* XXX Unknown/reserved */
1409
1410 cai = &ci->ci_cinfo[CAI_L2_1GBITLB];
1411 cai->cai_totalsize = AMD_L2_1GB_EBX_IUTLB_ENTRIES(descs[1]);
1412 cai->cai_associativity = AMD_L2_1GB_EBX_IUTLB_ASSOC(descs[1]);
1413 cai->cai_linesize = (1024 * 1024 * 1024);
1414 cp = cache_info_lookup(amd_cpuid_l2l3cache_assoc_info,
1415 cai->cai_associativity);
1416 if (cp != NULL)
1417 cai->cai_associativity = cp->cai_associativity;
1418 else
1419 cai->cai_associativity = 0; /* XXX Unknown/reserved */
1420
1421 cai = &ci->ci_cinfo[CAI_L2_1GBDTLB];
1422 cai->cai_totalsize = AMD_L2_1GB_EBX_DUTLB_ENTRIES(descs[1]);
1423 cai->cai_associativity = AMD_L2_1GB_EBX_DUTLB_ASSOC(descs[1]);
1424 cai->cai_linesize = (1024 * 1024 * 1024);
1425 cp = cache_info_lookup(amd_cpuid_l2l3cache_assoc_info,
1426 cai->cai_associativity);
1427 if (cp != NULL)
1428 cai->cai_associativity = cp->cai_associativity;
1429 else
1430 cai->cai_associativity = 0; /* XXX Unknown/reserved */
1431
1432 if (lfunc < 0x8000001d)
1433 return;
1434
1435 if (ci->ci_feat_val[3] & CPUID_TOPOEXT)
1436 cpu_dcp_cacheinfo(ci, 0x8000001d);
1437 }
1438
1439 static void
1440 via_cpu_cacheinfo(struct cpu_info *ci)
1441 {
1442 struct x86_cache_info *cai;
1443 int stepping;
1444 u_int descs[4];
1445 u_int lfunc;
1446
1447 stepping = CPUID_TO_STEPPING(ci->ci_signature);
1448
1449 /*
1450 * Determine the largest extended function value.
1451 */
1452 x86_cpuid(0x80000000, descs);
1453 lfunc = descs[0];
1454
1455 /*
1456 * Determine L1 cache/TLB info.
1457 */
1458 if (lfunc < 0x80000005) {
1459 /* No L1 cache info available. */
1460 return;
1461 }
1462
1463 x86_cpuid(0x80000005, descs);
1464
1465 cai = &ci->ci_cinfo[CAI_ITLB];
1466 cai->cai_totalsize = VIA_L1_EBX_ITLB_ENTRIES(descs[1]);
1467 cai->cai_associativity = VIA_L1_EBX_ITLB_ASSOC(descs[1]);
1468 cai->cai_linesize = (4 * 1024);
1469
1470 cai = &ci->ci_cinfo[CAI_DTLB];
1471 cai->cai_totalsize = VIA_L1_EBX_DTLB_ENTRIES(descs[1]);
1472 cai->cai_associativity = VIA_L1_EBX_DTLB_ASSOC(descs[1]);
1473 cai->cai_linesize = (4 * 1024);
1474
1475 cai = &ci->ci_cinfo[CAI_DCACHE];
1476 cai->cai_totalsize = VIA_L1_ECX_DC_SIZE(descs[2]);
1477 cai->cai_associativity = VIA_L1_ECX_DC_ASSOC(descs[2]);
1478 cai->cai_linesize = VIA_L1_EDX_IC_LS(descs[2]);
1479 if (ci->ci_model == 9 && stepping == 8) {
1480 /* Erratum: stepping 8 reports 4 when it should be 2 */
1481 cai->cai_associativity = 2;
1482 }
1483
1484 cai = &ci->ci_cinfo[CAI_ICACHE];
1485 cai->cai_totalsize = VIA_L1_EDX_IC_SIZE(descs[3]);
1486 cai->cai_associativity = VIA_L1_EDX_IC_ASSOC(descs[3]);
1487 cai->cai_linesize = VIA_L1_EDX_IC_LS(descs[3]);
1488 if (ci->ci_model == 9 && stepping == 8) {
1489 /* Erratum: stepping 8 reports 4 when it should be 2 */
1490 cai->cai_associativity = 2;
1491 }
1492
1493 /*
1494 * Determine L2 cache/TLB info.
1495 */
1496 if (lfunc < 0x80000006) {
1497 /* No L2 cache info available. */
1498 return;
1499 }
1500
1501 x86_cpuid(0x80000006, descs);
1502
1503 cai = &ci->ci_cinfo[CAI_L2CACHE];
1504 if (ci->ci_model >= 9) {
1505 cai->cai_totalsize = VIA_L2N_ECX_C_SIZE(descs[2]);
1506 cai->cai_associativity = VIA_L2N_ECX_C_ASSOC(descs[2]);
1507 cai->cai_linesize = VIA_L2N_ECX_C_LS(descs[2]);
1508 } else {
1509 cai->cai_totalsize = VIA_L2_ECX_C_SIZE(descs[2]);
1510 cai->cai_associativity = VIA_L2_ECX_C_ASSOC(descs[2]);
1511 cai->cai_linesize = VIA_L2_ECX_C_LS(descs[2]);
1512 }
1513 }
1514
1515 static void
1516 tmx86_get_longrun_status(u_int *frequency, u_int *voltage, u_int *percentage)
1517 {
1518 u_int descs[4];
1519
1520 x86_cpuid(0x80860007, descs);
1521 *frequency = descs[0];
1522 *voltage = descs[1];
1523 *percentage = descs[2];
1524 }
1525
1526 static void
1527 transmeta_cpu_info(struct cpu_info *ci)
1528 {
1529 u_int descs[4], nreg;
1530 u_int frequency, voltage, percentage;
1531
1532 x86_cpuid(0x80860000, descs);
1533 nreg = descs[0];
1534 if (nreg >= 0x80860001) {
1535 x86_cpuid(0x80860001, descs);
1536 aprint_verbose_dev(ci->ci_dev, "Processor revision %u.%u.%u.%u\n",
1537 (descs[1] >> 24) & 0xff,
1538 (descs[1] >> 16) & 0xff,
1539 (descs[1] >> 8) & 0xff,
1540 descs[1] & 0xff);
1541 }
1542 if (nreg >= 0x80860002) {
1543 x86_cpuid(0x80860002, descs);
1544 aprint_verbose_dev(ci->ci_dev, "Code Morphing Software Rev: %u.%u.%u-%u-%u\n",
1545 (descs[1] >> 24) & 0xff,
1546 (descs[1] >> 16) & 0xff,
1547 (descs[1] >> 8) & 0xff,
1548 descs[1] & 0xff,
1549 descs[2]);
1550 }
1551 if (nreg >= 0x80860006) {
1552 union {
1553 char text[65];
1554 u_int descs[4][4];
1555 } info;
1556 int i;
1557
1558 for (i=0; i<4; i++) {
1559 x86_cpuid(0x80860003 + i, info.descs[i]);
1560 }
1561 info.text[64] = '\0';
1562 aprint_verbose_dev(ci->ci_dev, "%s\n", info.text);
1563 }
1564
1565 if (nreg >= 0x80860007) {
1566 tmx86_get_longrun_status(&frequency,
1567 &voltage, &percentage);
1568 aprint_verbose_dev(ci->ci_dev, "LongRun <%dMHz %dmV %d%%>\n",
1569 frequency, voltage, percentage);
1570 }
1571 }
1572
1573 static void
1574 cpu_probe_base_features(struct cpu_info *ci, const char *cpuname)
1575 {
1576 u_int descs[4];
1577 int i;
1578 uint32_t brand[12];
1579
1580 memset(ci, 0, sizeof(*ci));
1581 ci->ci_dev = cpuname;
1582
1583 ci->ci_cpu_type = x86_identify();
1584 if (ci->ci_cpu_type >= 0) {
1585 /* Old pre-cpuid instruction cpu */
1586 ci->ci_cpuid_level = -1;
1587 return;
1588 }
1589
1590 /*
1591 * This CPU supports cpuid instruction, so we can call x86_cpuid()
1592 * function.
1593 */
1594
1595 /*
1596 * Fn0000_0000:
1597 * - Save cpuid max level.
1598 * - Save vendor string.
1599 */
1600 x86_cpuid(0, descs);
1601 ci->ci_cpuid_level = descs[0];
1602 /* Save vendor string */
1603 ci->ci_vendor[0] = descs[1];
1604 ci->ci_vendor[2] = descs[2];
1605 ci->ci_vendor[1] = descs[3];
1606 ci->ci_vendor[3] = 0;
1607
1608 /*
1609 * Fn8000_0000:
1610 * - Get cpuid extended function's max level.
1611 */
1612 x86_cpuid(0x80000000, descs);
1613 if (descs[0] >= 0x80000000)
1614 ci->ci_cpuid_extlevel = descs[0];
1615 else {
1616 /* Set lower value than 0x80000000 */
1617 ci->ci_cpuid_extlevel = 0;
1618 }
1619
1620 /*
1621 * Fn8000_000[2-4]:
1622 * - Save brand string.
1623 */
1624 if (ci->ci_cpuid_extlevel >= 0x80000004) {
1625 x86_cpuid(0x80000002, brand);
1626 x86_cpuid(0x80000003, brand + 4);
1627 x86_cpuid(0x80000004, brand + 8);
1628 for (i = 0; i < 48; i++)
1629 if (((char *) brand)[i] != ' ')
1630 break;
1631 memcpy(cpu_brand_string, ((char *) brand) + i, 48 - i);
1632 }
1633
1634 if (ci->ci_cpuid_level < 1)
1635 return;
1636
1637 /*
1638 * Fn0000_0001:
1639 * - Get CPU family, model and stepping (from eax).
1640 * - Initial local APIC ID and brand ID (from ebx)
1641 * - CPUID2 (from ecx)
1642 * - CPUID (from edx)
1643 */
1644 x86_cpuid(1, descs);
1645 ci->ci_signature = descs[0];
1646
1647 /* Extract full family/model values */
1648 ci->ci_family = CPUID_TO_FAMILY(ci->ci_signature);
1649 ci->ci_model = CPUID_TO_MODEL(ci->ci_signature);
1650
1651 /* Brand is low order 8 bits of ebx */
1652 ci->ci_brand_id = __SHIFTOUT(descs[1], CPUID_BRAND_INDEX);
1653 /* Initial local APIC ID */
1654 ci->ci_initapicid = __SHIFTOUT(descs[1], CPUID_LOCAL_APIC_ID);
1655
1656 ci->ci_feat_val[1] = descs[2];
1657 ci->ci_feat_val[0] = descs[3];
1658
1659 if (ci->ci_cpuid_level < 3)
1660 return;
1661
1662 /*
1663 * If the processor serial number misfeature is present and supported,
1664 * extract it here.
1665 */
1666 if ((ci->ci_feat_val[0] & CPUID_PN) != 0) {
1667 ci->ci_cpu_serial[0] = ci->ci_signature;
1668 x86_cpuid(3, descs);
1669 ci->ci_cpu_serial[2] = descs[2];
1670 ci->ci_cpu_serial[1] = descs[3];
1671 }
1672
1673 if (ci->ci_cpuid_level < 0x7)
1674 return;
1675
1676 x86_cpuid(7, descs);
1677 ci->ci_feat_val[5] = descs[1];
1678 ci->ci_feat_val[6] = descs[2];
1679 ci->ci_feat_val[7] = descs[3];
1680
1681 if (ci->ci_cpuid_level < 0xd)
1682 return;
1683
1684 /* Get support XCR0 bits */
1685 x86_cpuid2(0xd, 0, descs);
1686 ci->ci_feat_val[8] = descs[0]; /* Actually 64 bits */
1687 ci->ci_cur_xsave = descs[1];
1688 ci->ci_max_xsave = descs[2];
1689
1690 /* Additional flags (eg xsaveopt support) */
1691 x86_cpuid2(0xd, 1, descs);
1692 ci->ci_feat_val[9] = descs[0]; /* Actually 64 bits */
1693 }
1694
1695 static void
1696 cpu_probe_hv_features(struct cpu_info *ci, const char *cpuname)
1697 {
1698 uint32_t descs[4];
1699 char hv_sig[13];
1700 char *p;
1701 const char *hv_name;
1702 int i;
1703
1704 /*
1705 * [RFC] CPUID usage for interaction between Hypervisors and Linux.
1706 * http://lkml.org/lkml/2008/10/1/246
1707 *
1708 * KB1009458: Mechanisms to determine if software is running in
1709 * a VMware virtual machine
1710 * http://kb.vmware.com/kb/1009458
1711 */
1712 if ((ci->ci_feat_val[1] & CPUID2_RAZ) != 0) {
1713 x86_cpuid(0x40000000, descs);
1714 for (i = 1, p = hv_sig; i < 4; i++, p += sizeof(descs) / 4)
1715 memcpy(p, &descs[i], sizeof(descs[i]));
1716 *p = '\0';
1717 /*
1718 * HV vendor ID string
1719 * ------------+--------------
1720 * HAXM "HAXMHAXMHAXM"
1721 * KVM "KVMKVMKVM"
1722 * Microsoft "Microsoft Hv"
1723 * QEMU(TCG) "TCGTCGTCGTCG"
1724 * VMware "VMwareVMware"
1725 * Xen "XenVMMXenVMM"
1726 * NetBSD "___ NVMM ___"
1727 */
1728 if (strncmp(hv_sig, "HAXMHAXMHAXM", 12) == 0)
1729 hv_name = "HAXM";
1730 else if (strncmp(hv_sig, "KVMKVMKVM", 9) == 0)
1731 hv_name = "KVM";
1732 else if (strncmp(hv_sig, "Microsoft Hv", 12) == 0)
1733 hv_name = "Hyper-V";
1734 else if (strncmp(hv_sig, "TCGTCGTCGTCG", 12) == 0)
1735 hv_name = "QEMU(TCG)";
1736 else if (strncmp(hv_sig, "VMwareVMware", 12) == 0)
1737 hv_name = "VMware";
1738 else if (strncmp(hv_sig, "XenVMMXenVMM", 12) == 0)
1739 hv_name = "Xen";
1740 else if (strncmp(hv_sig, "___ NVMM ___", 12) == 0)
1741 hv_name = "NVMM";
1742 else
1743 hv_name = "unknown";
1744
1745 printf("%s: Running on hypervisor: %s\n", cpuname, hv_name);
1746 }
1747 }
1748
1749 static void
1750 cpu_probe_features(struct cpu_info *ci)
1751 {
1752 const struct cpu_cpuid_nameclass *cpup = NULL;
1753 unsigned int i;
1754
1755 if (ci->ci_cpuid_level < 1)
1756 return;
1757
1758 for (i = 0; i < __arraycount(i386_cpuid_cpus); i++) {
1759 if (!strncmp((char *)ci->ci_vendor,
1760 i386_cpuid_cpus[i].cpu_id, 12)) {
1761 cpup = &i386_cpuid_cpus[i];
1762 break;
1763 }
1764 }
1765
1766 if (cpup == NULL)
1767 return;
1768
1769 i = ci->ci_family - CPU_MINFAMILY;
1770
1771 if (i >= __arraycount(cpup->cpu_family))
1772 i = __arraycount(cpup->cpu_family) - 1;
1773
1774 if (cpup->cpu_family[i].cpu_probe == NULL)
1775 return;
1776
1777 (*cpup->cpu_family[i].cpu_probe)(ci);
1778 }
1779
1780 static void
1781 print_bits(const char *cpuname, const char *hdr, const char *fmt, uint32_t val)
1782 {
1783 char buf[32 * 16];
1784 char *bp;
1785
1786 #define MAX_LINE_LEN 79 /* get from command arg or 'stty cols' ? */
1787
1788 if (val == 0 || fmt == NULL)
1789 return;
1790
1791 snprintb_m(buf, sizeof(buf), fmt, val,
1792 MAX_LINE_LEN - strlen(cpuname) - 2 - strlen(hdr) - 1);
1793 bp = buf;
1794 while (*bp != '\0') {
1795 aprint_verbose("%s: %s %s\n", cpuname, hdr, bp);
1796 bp += strlen(bp) + 1;
1797 }
1798 }
1799
1800 static void
1801 dump_descs(uint32_t leafstart, uint32_t leafend, const char *cpuname,
1802 const char *blockname)
1803 {
1804 uint32_t descs[4];
1805 uint32_t leaf;
1806
1807 aprint_verbose("%s: highest %s info %08x\n", cpuname, blockname,
1808 leafend);
1809
1810 if (verbose) {
1811 for (leaf = leafstart; leaf <= leafend; leaf++) {
1812 x86_cpuid(leaf, descs);
1813 printf("%s: %08x: %08x %08x %08x %08x\n", cpuname,
1814 leaf, descs[0], descs[1], descs[2], descs[3]);
1815 }
1816 }
1817 }
1818
1819 static void
1820 identifycpu_cpuids_intel_0x04(struct cpu_info *ci)
1821 {
1822 u_int lp_max = 1; /* logical processors per package */
1823 u_int smt_max; /* smt per core */
1824 u_int core_max = 1; /* core per package */
1825 u_int smt_bits, core_bits;
1826 uint32_t descs[4];
1827
1828 /*
1829 * 253668.pdf 7.10.2
1830 */
1831
1832 if ((ci->ci_feat_val[0] & CPUID_HTT) != 0) {
1833 x86_cpuid(1, descs);
1834 lp_max = __SHIFTOUT(descs[1], CPUID_HTT_CORES);
1835 }
1836 x86_cpuid2(4, 0, descs);
1837 core_max = __SHIFTOUT(descs[0], CPUID_DCP_CORE_P_PKG) + 1;
1838
1839 assert(lp_max >= core_max);
1840 smt_max = lp_max / core_max;
1841 smt_bits = ilog2(smt_max - 1) + 1;
1842 core_bits = ilog2(core_max - 1) + 1;
1843
1844 if (smt_bits + core_bits)
1845 ci->ci_packageid = ci->ci_initapicid >> (smt_bits + core_bits);
1846
1847 if (core_bits)
1848 ci->ci_coreid = __SHIFTOUT(ci->ci_initapicid,
1849 __BITS(smt_bits, smt_bits + core_bits - 1));
1850
1851 if (smt_bits)
1852 ci->ci_smtid = __SHIFTOUT(ci->ci_initapicid,
1853 __BITS((int)0, (int)(smt_bits - 1)));
1854 }
1855
1856 static void
1857 identifycpu_cpuids_intel_0x0b(struct cpu_info *ci)
1858 {
1859 const char *cpuname = ci->ci_dev;
1860 u_int smt_bits, core_bits, core_shift = 0, pkg_shift = 0;
1861 uint32_t descs[4];
1862 int i;
1863
1864 x86_cpuid(0x0b, descs);
1865 if (descs[1] == 0) {
1866 identifycpu_cpuids_intel_0x04(ci);
1867 return;
1868 }
1869
1870 for (i = 0; ; i++) {
1871 unsigned int shiftnum, lvltype;
1872 x86_cpuid2(0x0b, i, descs);
1873
1874 /* On invalid level, (EAX and) EBX return 0 */
1875 if (descs[1] == 0)
1876 break;
1877
1878 shiftnum = __SHIFTOUT(descs[0], CPUID_TOP_SHIFTNUM);
1879 lvltype = __SHIFTOUT(descs[2], CPUID_TOP_LVLTYPE);
1880 switch (lvltype) {
1881 case CPUID_TOP_LVLTYPE_SMT:
1882 core_shift = shiftnum;
1883 break;
1884 case CPUID_TOP_LVLTYPE_CORE:
1885 pkg_shift = shiftnum;
1886 break;
1887 case CPUID_TOP_LVLTYPE_INVAL:
1888 aprint_verbose("%s: Invalid level type\n", cpuname);
1889 break;
1890 default:
1891 aprint_verbose("%s: Unknown level type(%d) \n",
1892 cpuname, lvltype);
1893 break;
1894 }
1895 }
1896
1897 assert(pkg_shift >= core_shift);
1898 smt_bits = core_shift;
1899 core_bits = pkg_shift - core_shift;
1900
1901 ci->ci_packageid = ci->ci_initapicid >> pkg_shift;
1902
1903 if (core_bits)
1904 ci->ci_coreid = __SHIFTOUT(ci->ci_initapicid,
1905 __BITS(core_shift, pkg_shift - 1));
1906
1907 if (smt_bits)
1908 ci->ci_smtid = __SHIFTOUT(ci->ci_initapicid,
1909 __BITS((int)0, core_shift - 1));
1910 }
1911
1912 static void
1913 identifycpu_cpuids_intel(struct cpu_info *ci)
1914 {
1915 const char *cpuname = ci->ci_dev;
1916
1917 if (ci->ci_cpuid_level >= 0x0b)
1918 identifycpu_cpuids_intel_0x0b(ci);
1919 else if (ci->ci_cpuid_level >= 4)
1920 identifycpu_cpuids_intel_0x04(ci);
1921
1922 aprint_verbose("%s: Cluster/Package ID %u\n", cpuname,
1923 ci->ci_packageid);
1924 aprint_verbose("%s: Core ID %u\n", cpuname, ci->ci_coreid);
1925 aprint_verbose("%s: SMT ID %u\n", cpuname, ci->ci_smtid);
1926 }
1927
1928 static void
1929 identifycpu_cpuids_amd(struct cpu_info *ci)
1930 {
1931 const char *cpuname = ci->ci_dev;
1932 u_int lp_max, core_max;
1933 int n, cpu_family, apic_id, smt_bits, core_bits = 0;
1934 uint32_t descs[4];
1935
1936 apic_id = ci->ci_initapicid;
1937 cpu_family = CPUID_TO_FAMILY(ci->ci_signature);
1938
1939 if (cpu_family < 0xf)
1940 return;
1941
1942 if ((ci->ci_feat_val[0] & CPUID_HTT) != 0) {
1943 x86_cpuid(1, descs);
1944 lp_max = __SHIFTOUT(descs[1], CPUID_HTT_CORES);
1945
1946 if (cpu_family >= 0x10 && ci->ci_cpuid_extlevel >= 0x8000008) {
1947 x86_cpuid(0x8000008, descs);
1948 core_max = (descs[2] & 0xff) + 1;
1949 n = (descs[2] >> 12) & 0x0f;
1950 if (n != 0)
1951 core_bits = n;
1952 }
1953 } else {
1954 lp_max = 1;
1955 }
1956 core_max = lp_max;
1957
1958 smt_bits = ilog2((lp_max / core_max) - 1) + 1;
1959 if (core_bits == 0)
1960 core_bits = ilog2(core_max - 1) + 1;
1961
1962 #if 0 /* MSRs need kernel mode */
1963 if (cpu_family < 0x11) {
1964 const uint64_t reg = rdmsr(MSR_NB_CFG);
1965 if ((reg & NB_CFG_INITAPICCPUIDLO) == 0) {
1966 const u_int node_id = apic_id & __BITS(0, 2);
1967 apic_id = (cpu_family == 0xf) ?
1968 (apic_id >> core_bits) | (node_id << core_bits) :
1969 (apic_id >> 5) | (node_id << 2);
1970 }
1971 }
1972 #endif
1973
1974 if (cpu_family == 0x17) {
1975 x86_cpuid(0x8000001e, descs);
1976 const u_int threads = ((descs[1] >> 8) & 0xff) + 1;
1977 smt_bits = ilog2(threads);
1978 core_bits -= smt_bits;
1979 }
1980
1981 if (smt_bits + core_bits) {
1982 if (smt_bits + core_bits < 32)
1983 ci->ci_packageid = 0;
1984 }
1985 if (core_bits) {
1986 u_int core_mask = __BITS(smt_bits, smt_bits + core_bits - 1);
1987 ci->ci_coreid = __SHIFTOUT(apic_id, core_mask);
1988 }
1989 if (smt_bits) {
1990 u_int smt_mask = __BITS(0, smt_bits - 1);
1991 ci->ci_smtid = __SHIFTOUT(apic_id, smt_mask);
1992 }
1993
1994 aprint_verbose("%s: Cluster/Package ID %u\n", cpuname,
1995 ci->ci_packageid);
1996 aprint_verbose("%s: Core ID %u\n", cpuname, ci->ci_coreid);
1997 aprint_verbose("%s: SMT ID %u\n", cpuname, ci->ci_smtid);
1998 }
1999
2000 static void
2001 identifycpu_cpuids(struct cpu_info *ci)
2002 {
2003 const char *cpuname = ci->ci_dev;
2004
2005 aprint_verbose("%s: Initial APIC ID %u\n", cpuname, ci->ci_initapicid);
2006 ci->ci_packageid = ci->ci_initapicid;
2007 ci->ci_coreid = 0;
2008 ci->ci_smtid = 0;
2009
2010 if (cpu_vendor == CPUVENDOR_INTEL)
2011 identifycpu_cpuids_intel(ci);
2012 else if (cpu_vendor == CPUVENDOR_AMD)
2013 identifycpu_cpuids_amd(ci);
2014 }
2015
2016 void
2017 identifycpu(int fd, const char *cpuname)
2018 {
2019 const char *name = "", *modifier, *vendorname, *brand = "";
2020 int class = CPUCLASS_386;
2021 unsigned int i;
2022 int modif, family;
2023 const struct cpu_cpuid_nameclass *cpup = NULL;
2024 const struct cpu_cpuid_family *cpufam;
2025 struct cpu_info *ci, cistore;
2026 u_int descs[4];
2027 size_t sz;
2028 struct cpu_ucode_version ucode;
2029 union {
2030 struct cpu_ucode_version_amd amd;
2031 struct cpu_ucode_version_intel1 intel1;
2032 } ucvers;
2033
2034 ci = &cistore;
2035 cpu_probe_base_features(ci, cpuname);
2036 dump_descs(0x00000000, ci->ci_cpuid_level, cpuname, "basic");
2037 if ((ci->ci_feat_val[1] & CPUID2_RAZ) != 0) {
2038 x86_cpuid(0x40000000, descs);
2039 dump_descs(0x40000000, descs[0], cpuname, "hypervisor");
2040 }
2041 dump_descs(0x80000000, ci->ci_cpuid_extlevel, cpuname, "extended");
2042
2043 cpu_probe_hv_features(ci, cpuname);
2044 cpu_probe_features(ci);
2045
2046 if (ci->ci_cpu_type >= 0) {
2047 /* Old pre-cpuid instruction cpu */
2048 if (ci->ci_cpu_type >= (int)__arraycount(i386_nocpuid_cpus))
2049 errx(1, "unknown cpu type %d", ci->ci_cpu_type);
2050 name = i386_nocpuid_cpus[ci->ci_cpu_type].cpu_name;
2051 cpu_vendor = i386_nocpuid_cpus[ci->ci_cpu_type].cpu_vendor;
2052 vendorname = i386_nocpuid_cpus[ci->ci_cpu_type].cpu_vendorname;
2053 class = i386_nocpuid_cpus[ci->ci_cpu_type].cpu_class;
2054 ci->ci_info = i386_nocpuid_cpus[ci->ci_cpu_type].cpu_info;
2055 modifier = "";
2056 } else {
2057 /* CPU which support cpuid instruction */
2058 modif = (ci->ci_signature >> 12) & 0x3;
2059 family = ci->ci_family;
2060 if (family < CPU_MINFAMILY)
2061 errx(1, "identifycpu: strange family value");
2062 if (family > CPU_MAXFAMILY)
2063 family = CPU_MAXFAMILY;
2064
2065 for (i = 0; i < __arraycount(i386_cpuid_cpus); i++) {
2066 if (!strncmp((char *)ci->ci_vendor,
2067 i386_cpuid_cpus[i].cpu_id, 12)) {
2068 cpup = &i386_cpuid_cpus[i];
2069 break;
2070 }
2071 }
2072
2073 if (cpup == NULL) {
2074 cpu_vendor = CPUVENDOR_UNKNOWN;
2075 if (ci->ci_vendor[0] != '\0')
2076 vendorname = (char *)&ci->ci_vendor[0];
2077 else
2078 vendorname = "Unknown";
2079 class = family - 3;
2080 modifier = "";
2081 name = "";
2082 ci->ci_info = NULL;
2083 } else {
2084 cpu_vendor = cpup->cpu_vendor;
2085 vendorname = cpup->cpu_vendorname;
2086 modifier = modifiers[modif];
2087 cpufam = &cpup->cpu_family[family - CPU_MINFAMILY];
2088 name = cpufam->cpu_models[ci->ci_model];
2089 if (name == NULL || *name == '\0')
2090 name = cpufam->cpu_model_default;
2091 class = cpufam->cpu_class;
2092 ci->ci_info = cpufam->cpu_info;
2093
2094 if (cpu_vendor == CPUVENDOR_INTEL) {
2095 if (ci->ci_family == 6 && ci->ci_model >= 5) {
2096 const char *tmp;
2097 tmp = intel_family6_name(ci);
2098 if (tmp != NULL)
2099 name = tmp;
2100 }
2101 if (ci->ci_family == 15 &&
2102 ci->ci_brand_id <
2103 __arraycount(i386_intel_brand) &&
2104 i386_intel_brand[ci->ci_brand_id])
2105 name =
2106 i386_intel_brand[ci->ci_brand_id];
2107 }
2108
2109 if (cpu_vendor == CPUVENDOR_AMD) {
2110 if (ci->ci_family == 6 && ci->ci_model >= 6) {
2111 if (ci->ci_brand_id == 1)
2112 /*
2113 * It's Duron. We override the
2114 * name, since it might have
2115 * been misidentified as Athlon.
2116 */
2117 name =
2118 amd_brand[ci->ci_brand_id];
2119 else
2120 brand = amd_brand_name;
2121 }
2122 if (CPUID_TO_BASEFAMILY(ci->ci_signature)
2123 == 0xf) {
2124 /* Identify AMD64 CPU names. */
2125 const char *tmp;
2126 tmp = amd_amd64_name(ci);
2127 if (tmp != NULL)
2128 name = tmp;
2129 }
2130 }
2131
2132 if (cpu_vendor == CPUVENDOR_IDT && ci->ci_family >= 6)
2133 vendorname = "VIA";
2134 }
2135 }
2136
2137 ci->ci_cpu_class = class;
2138
2139 sz = sizeof(ci->ci_tsc_freq);
2140 (void)sysctlbyname("machdep.tsc_freq", &ci->ci_tsc_freq, &sz, NULL, 0);
2141 sz = sizeof(use_pae);
2142 (void)sysctlbyname("machdep.pae", &use_pae, &sz, NULL, 0);
2143 largepagesize = (use_pae ? 2 * 1024 * 1024 : 4 * 1024 * 1024);
2144
2145 /*
2146 * The 'cpu_brand_string' is much more useful than the 'cpu_model'
2147 * we try to determine from the family/model values.
2148 */
2149 if (*cpu_brand_string != '\0')
2150 aprint_normal("%s: \"%s\"\n", cpuname, cpu_brand_string);
2151
2152 aprint_normal("%s: %s", cpuname, vendorname);
2153 if (*modifier)
2154 aprint_normal(" %s", modifier);
2155 if (*name)
2156 aprint_normal(" %s", name);
2157 if (*brand)
2158 aprint_normal(" %s", brand);
2159 aprint_normal(" (%s-class)", classnames[class]);
2160
2161 if (ci->ci_tsc_freq != 0)
2162 aprint_normal(", %ju.%02ju MHz",
2163 ((uintmax_t)ci->ci_tsc_freq + 4999) / 1000000,
2164 (((uintmax_t)ci->ci_tsc_freq + 4999) / 10000) % 100);
2165 aprint_normal("\n");
2166
2167 aprint_normal_dev(ci->ci_dev, "family %#x model %#x stepping %#x",
2168 ci->ci_family, ci->ci_model, CPUID_TO_STEPPING(ci->ci_signature));
2169 if (ci->ci_signature != 0)
2170 aprint_normal(" (id %#x)", ci->ci_signature);
2171 aprint_normal("\n");
2172
2173 if (ci->ci_info)
2174 (*ci->ci_info)(ci);
2175
2176 /*
2177 * display CPU feature flags
2178 */
2179
2180 print_bits(cpuname, "features", CPUID_FLAGS1, ci->ci_feat_val[0]);
2181 print_bits(cpuname, "features1", CPUID2_FLAGS1, ci->ci_feat_val[1]);
2182
2183 /* These next two are actually common definitions! */
2184 print_bits(cpuname, "features2",
2185 cpu_vendor == CPUVENDOR_INTEL ? CPUID_INTEL_EXT_FLAGS
2186 : CPUID_EXT_FLAGS, ci->ci_feat_val[2]);
2187 print_bits(cpuname, "features3",
2188 cpu_vendor == CPUVENDOR_INTEL ? CPUID_INTEL_FLAGS4
2189 : CPUID_AMD_FLAGS4, ci->ci_feat_val[3]);
2190
2191 print_bits(cpuname, "padloack features", CPUID_FLAGS_PADLOCK,
2192 ci->ci_feat_val[4]);
2193 if ((cpu_vendor == CPUVENDOR_INTEL) || (cpu_vendor == CPUVENDOR_AMD))
2194 print_bits(cpuname, "features5", CPUID_SEF_FLAGS,
2195 ci->ci_feat_val[5]);
2196 if ((cpu_vendor == CPUVENDOR_INTEL) || (cpu_vendor == CPUVENDOR_AMD))
2197 print_bits(cpuname, "features6", CPUID_SEF_FLAGS1,
2198 ci->ci_feat_val[6]);
2199
2200 if (cpu_vendor == CPUVENDOR_INTEL)
2201 print_bits(cpuname, "features7", CPUID_SEF_FLAGS2,
2202 ci->ci_feat_val[7]);
2203
2204 print_bits(cpuname, "xsave features", XCR0_FLAGS1, ci->ci_feat_val[8]);
2205 print_bits(cpuname, "xsave instructions", CPUID_PES1_FLAGS,
2206 ci->ci_feat_val[9]);
2207
2208 if (ci->ci_max_xsave != 0) {
2209 aprint_normal("%s: xsave area size: current %d, maximum %d",
2210 cpuname, ci->ci_cur_xsave, ci->ci_max_xsave);
2211 aprint_normal(", xgetbv %sabled\n",
2212 ci->ci_feat_val[1] & CPUID2_OSXSAVE ? "en" : "dis");
2213 if (ci->ci_feat_val[1] & CPUID2_OSXSAVE)
2214 print_bits(cpuname, "enabled xsave", XCR0_FLAGS1,
2215 x86_xgetbv());
2216 }
2217
2218 x86_print_cache_and_tlb_info(ci);
2219
2220 if (ci->ci_cpuid_level >= 3 && (ci->ci_feat_val[0] & CPUID_PN)) {
2221 aprint_verbose("%s: serial number %04X-%04X-%04X-%04X-%04X-%04X\n",
2222 cpuname,
2223 ci->ci_cpu_serial[0] / 65536, ci->ci_cpu_serial[0] % 65536,
2224 ci->ci_cpu_serial[1] / 65536, ci->ci_cpu_serial[1] % 65536,
2225 ci->ci_cpu_serial[2] / 65536, ci->ci_cpu_serial[2] % 65536);
2226 }
2227
2228 if (ci->ci_cpu_class == CPUCLASS_386)
2229 errx(1, "NetBSD requires an 80486 or later processor");
2230
2231 if (ci->ci_cpu_type == CPU_486DLC) {
2232 #ifndef CYRIX_CACHE_WORKS
2233 aprint_error("WARNING: CYRIX 486DLC CACHE UNCHANGED.\n");
2234 #else
2235 #ifndef CYRIX_CACHE_REALLY_WORKS
2236 aprint_error("WARNING: CYRIX 486DLC CACHE ENABLED IN HOLD-FLUSH MODE.\n");
2237 #else
2238 aprint_error("WARNING: CYRIX 486DLC CACHE ENABLED.\n");
2239 #endif
2240 #endif
2241 }
2242
2243 /*
2244 * Everything past this point requires a Pentium or later.
2245 */
2246 if (ci->ci_cpuid_level < 0)
2247 return;
2248
2249 identifycpu_cpuids(ci);
2250
2251 if ((ci->ci_cpuid_level >= 5)
2252 && ((cpu_vendor == CPUVENDOR_INTEL)
2253 || (cpu_vendor == CPUVENDOR_AMD))) {
2254 uint16_t lmin, lmax;
2255 x86_cpuid(5, descs);
2256
2257 print_bits(cpuname, "MONITOR/MWAIT extensions",
2258 CPUID_MON_FLAGS, descs[2]);
2259 lmin = __SHIFTOUT(descs[0], CPUID_MON_MINSIZE);
2260 lmax = __SHIFTOUT(descs[1], CPUID_MON_MAXSIZE);
2261 aprint_normal("%s: monitor-line size %hu", cpuname, lmin);
2262 if (lmin != lmax)
2263 aprint_normal("-%hu", lmax);
2264 aprint_normal("\n");
2265
2266 for (i = 0; i <= 7; i++) {
2267 unsigned int num = CPUID_MON_SUBSTATE(descs[3], i);
2268
2269 if (num != 0)
2270 aprint_normal("%s: C%u substates %u\n",
2271 cpuname, i, num);
2272 }
2273 }
2274 if ((ci->ci_cpuid_level >= 6)
2275 && ((cpu_vendor == CPUVENDOR_INTEL)
2276 || (cpu_vendor == CPUVENDOR_AMD))) {
2277 x86_cpuid(6, descs);
2278 print_bits(cpuname, "DSPM-eax", CPUID_DSPM_FLAGS, descs[0]);
2279 print_bits(cpuname, "DSPM-ecx", CPUID_DSPM_FLAGS1, descs[2]);
2280 }
2281 if ((ci->ci_cpuid_level >= 7)
2282 && ((cpu_vendor == CPUVENDOR_INTEL)
2283 || (cpu_vendor == CPUVENDOR_AMD))) {
2284 x86_cpuid(7, descs);
2285 aprint_verbose("%s: SEF highest subleaf %08x\n",
2286 cpuname, descs[0]);
2287 }
2288
2289 if (cpu_vendor == CPUVENDOR_AMD) {
2290 if (ci->ci_cpuid_extlevel >= 0x80000007)
2291 powernow_probe(ci);
2292
2293 if (ci->ci_cpuid_extlevel >= 0x80000008) {
2294 x86_cpuid(0x80000008, descs);
2295 print_bits(cpuname, "AMD Extended features",
2296 CPUID_CAPEX_FLAGS, descs[1]);
2297 }
2298
2299 if ((ci->ci_cpuid_extlevel >= 0x8000000a)
2300 && (ci->ci_feat_val[3] & CPUID_SVM) != 0) {
2301 x86_cpuid(0x8000000a, descs);
2302 aprint_verbose("%s: SVM Rev. %d\n", cpuname,
2303 descs[0] & 0xf);
2304 aprint_verbose("%s: SVM NASID %d\n", cpuname,
2305 descs[1]);
2306 print_bits(cpuname, "SVM features",
2307 CPUID_AMD_SVM_FLAGS, descs[3]);
2308 }
2309 if (ci->ci_cpuid_extlevel >= 0x8000001f) {
2310 x86_cpuid(0x8000001f, descs);
2311 print_bits(cpuname, "Encrypted Memory features",
2312 CPUID_AMD_ENCMEM_FLAGS, descs[0]);
2313 }
2314 } else if (cpu_vendor == CPUVENDOR_INTEL) {
2315 int32_t bi_index;
2316
2317 for (bi_index = 1; bi_index <= ci->ci_cpuid_level; bi_index++) {
2318 x86_cpuid(bi_index, descs);
2319 switch (bi_index) {
2320 case 0x0a:
2321 print_bits(cpuname, "Perfmon-eax",
2322 CPUID_PERF_FLAGS0, descs[0]);
2323 print_bits(cpuname, "Perfmon-ebx",
2324 CPUID_PERF_FLAGS1, descs[1]);
2325 print_bits(cpuname, "Perfmon-edx",
2326 CPUID_PERF_FLAGS3, descs[3]);
2327 break;
2328 default:
2329 #if 0
2330 aprint_verbose("%s: basic %08x-eax %08x\n",
2331 cpuname, bi_index, descs[0]);
2332 aprint_verbose("%s: basic %08x-ebx %08x\n",
2333 cpuname, bi_index, descs[1]);
2334 aprint_verbose("%s: basic %08x-ecx %08x\n",
2335 cpuname, bi_index, descs[2]);
2336 aprint_verbose("%s: basic %08x-edx %08x\n",
2337 cpuname, bi_index, descs[3]);
2338 #endif
2339 break;
2340 }
2341 }
2342 }
2343
2344 #ifdef INTEL_ONDEMAND_CLOCKMOD
2345 clockmod_init();
2346 #endif
2347
2348 if (cpu_vendor == CPUVENDOR_AMD)
2349 ucode.loader_version = CPU_UCODE_LOADER_AMD;
2350 else if (cpu_vendor == CPUVENDOR_INTEL)
2351 ucode.loader_version = CPU_UCODE_LOADER_INTEL1;
2352 else
2353 return;
2354
2355 ucode.data = &ucvers;
2356 if (ioctl(fd, IOC_CPU_UCODE_GET_VERSION, &ucode) < 0) {
2357 #ifdef __i386__
2358 struct cpu_ucode_version_64 ucode_64;
2359 if (errno != ENOTTY)
2360 return;
2361 /* Try the 64 bit ioctl */
2362 memset(&ucode_64, 0, sizeof ucode_64);
2363 ucode_64.data = &ucvers;
2364 ucode_64.loader_version = ucode.loader_version;
2365 if (ioctl(fd, IOC_CPU_UCODE_GET_VERSION_64, &ucode_64) < 0)
2366 return;
2367 #else
2368 return;
2369 #endif
2370 }
2371
2372 if (cpu_vendor == CPUVENDOR_AMD)
2373 printf("%s: UCode version: 0x%"PRIx64"\n", cpuname, ucvers.amd.version);
2374 else if (cpu_vendor == CPUVENDOR_INTEL)
2375 printf("%s: microcode version 0x%x, platform ID %d\n", cpuname,
2376 ucvers.intel1.ucodeversion, ucvers.intel1.platformid);
2377 }
2378
2379 static const struct x86_cache_info *
2380 cache_info_lookup(const struct x86_cache_info *cai, uint8_t desc)
2381 {
2382 int i;
2383
2384 for (i = 0; cai[i].cai_desc != 0; i++) {
2385 if (cai[i].cai_desc == desc)
2386 return (&cai[i]);
2387 }
2388
2389 return (NULL);
2390 }
2391
2392 static const char *
2393 print_cache_config(struct cpu_info *ci, int cache_tag, const char *name,
2394 const char *sep)
2395 {
2396 struct x86_cache_info *cai = &ci->ci_cinfo[cache_tag];
2397 char human_num[HUMAN_BUFSIZE];
2398
2399 if (cai->cai_totalsize == 0)
2400 return sep;
2401
2402 if (sep == NULL)
2403 aprint_verbose_dev(ci->ci_dev, "");
2404 else
2405 aprint_verbose("%s", sep);
2406 if (name != NULL)
2407 aprint_verbose("%s ", name);
2408
2409 if (cai->cai_string != NULL) {
2410 aprint_verbose("%s ", cai->cai_string);
2411 } else {
2412 (void)humanize_number(human_num, sizeof(human_num),
2413 cai->cai_totalsize, "B", HN_AUTOSCALE, HN_NOSPACE);
2414 aprint_verbose("%s %dB/line ", human_num, cai->cai_linesize);
2415 }
2416 switch (cai->cai_associativity) {
2417 case 0:
2418 aprint_verbose("disabled");
2419 break;
2420 case 1:
2421 aprint_verbose("direct-mapped");
2422 break;
2423 case 0xff:
2424 aprint_verbose("fully associative");
2425 break;
2426 default:
2427 aprint_verbose("%d-way", cai->cai_associativity);
2428 break;
2429 }
2430 return ", ";
2431 }
2432
2433 static const char *
2434 print_tlb_config(struct cpu_info *ci, int cache_tag, const char *name,
2435 const char *sep)
2436 {
2437 struct x86_cache_info *cai = &ci->ci_cinfo[cache_tag];
2438 char human_num[HUMAN_BUFSIZE];
2439
2440 if (cai->cai_totalsize == 0)
2441 return sep;
2442
2443 if (sep == NULL)
2444 aprint_verbose_dev(ci->ci_dev, "");
2445 else
2446 aprint_verbose("%s", sep);
2447 if (name != NULL)
2448 aprint_verbose("%s ", name);
2449
2450 if (cai->cai_string != NULL) {
2451 aprint_verbose("%s", cai->cai_string);
2452 } else {
2453 (void)humanize_number(human_num, sizeof(human_num),
2454 cai->cai_linesize, "B", HN_AUTOSCALE, HN_NOSPACE);
2455 aprint_verbose("%d %s entries ", cai->cai_totalsize,
2456 human_num);
2457 switch (cai->cai_associativity) {
2458 case 0:
2459 aprint_verbose("disabled");
2460 break;
2461 case 1:
2462 aprint_verbose("direct-mapped");
2463 break;
2464 case 0xff:
2465 aprint_verbose("fully associative");
2466 break;
2467 default:
2468 aprint_verbose("%d-way", cai->cai_associativity);
2469 break;
2470 }
2471 }
2472 return ", ";
2473 }
2474
2475 static void
2476 x86_print_cache_and_tlb_info(struct cpu_info *ci)
2477 {
2478 const char *sep = NULL;
2479
2480 if (ci->ci_cinfo[CAI_ICACHE].cai_totalsize != 0 ||
2481 ci->ci_cinfo[CAI_DCACHE].cai_totalsize != 0) {
2482 sep = print_cache_config(ci, CAI_ICACHE, "I-cache", NULL);
2483 sep = print_cache_config(ci, CAI_DCACHE, "D-cache", sep);
2484 if (sep != NULL)
2485 aprint_verbose("\n");
2486 }
2487 if (ci->ci_cinfo[CAI_L2CACHE].cai_totalsize != 0) {
2488 sep = print_cache_config(ci, CAI_L2CACHE, "L2 cache", NULL);
2489 if (sep != NULL)
2490 aprint_verbose("\n");
2491 }
2492 if (ci->ci_cinfo[CAI_L3CACHE].cai_totalsize != 0) {
2493 sep = print_cache_config(ci, CAI_L3CACHE, "L3 cache", NULL);
2494 if (sep != NULL)
2495 aprint_verbose("\n");
2496 }
2497 if (ci->ci_cinfo[CAI_PREFETCH].cai_linesize != 0) {
2498 aprint_verbose_dev(ci->ci_dev, "%dB prefetching",
2499 ci->ci_cinfo[CAI_PREFETCH].cai_linesize);
2500 if (sep != NULL)
2501 aprint_verbose("\n");
2502 }
2503 if (ci->ci_cinfo[CAI_ITLB].cai_totalsize != 0) {
2504 sep = print_tlb_config(ci, CAI_ITLB, "ITLB", NULL);
2505 sep = print_tlb_config(ci, CAI_ITLB2, NULL, sep);
2506 if (sep != NULL)
2507 aprint_verbose("\n");
2508 }
2509 if (ci->ci_cinfo[CAI_DTLB].cai_totalsize != 0) {
2510 sep = print_tlb_config(ci, CAI_DTLB, "DTLB", NULL);
2511 sep = print_tlb_config(ci, CAI_DTLB2, NULL, sep);
2512 if (sep != NULL)
2513 aprint_verbose("\n");
2514 }
2515 if (ci->ci_cinfo[CAI_L2_ITLB].cai_totalsize != 0) {
2516 sep = print_tlb_config(ci, CAI_L2_ITLB, "L2 ITLB", NULL);
2517 sep = print_tlb_config(ci, CAI_L2_ITLB2, NULL, sep);
2518 if (sep != NULL)
2519 aprint_verbose("\n");
2520 }
2521 if (ci->ci_cinfo[CAI_L2_DTLB].cai_totalsize != 0) {
2522 sep = print_tlb_config(ci, CAI_L2_DTLB, "L2 DTLB", NULL);
2523 sep = print_tlb_config(ci, CAI_L2_DTLB2, NULL, sep);
2524 if (sep != NULL)
2525 aprint_verbose("\n");
2526 }
2527 if (ci->ci_cinfo[CAI_L2_STLB].cai_totalsize != 0) {
2528 sep = print_tlb_config(ci, CAI_L2_STLB, "L2 STLB", NULL);
2529 sep = print_tlb_config(ci, CAI_L2_STLB2, NULL, sep);
2530 sep = print_tlb_config(ci, CAI_L2_STLB3, NULL, sep);
2531 if (sep != NULL)
2532 aprint_verbose("\n");
2533 }
2534 if (ci->ci_cinfo[CAI_L1_1GBITLB].cai_totalsize != 0) {
2535 sep = print_tlb_config(ci, CAI_L1_1GBITLB, "L1 1GB page ITLB",
2536 NULL);
2537 if (sep != NULL)
2538 aprint_verbose("\n");
2539 }
2540 if (ci->ci_cinfo[CAI_L1_1GBDTLB].cai_totalsize != 0) {
2541 sep = print_tlb_config(ci, CAI_L1_1GBDTLB, "L1 1GB page DTLB",
2542 NULL);
2543 if (sep != NULL)
2544 aprint_verbose("\n");
2545 }
2546 if (ci->ci_cinfo[CAI_L2_1GBITLB].cai_totalsize != 0) {
2547 sep = print_tlb_config(ci, CAI_L2_1GBITLB, "L2 1GB page ITLB",
2548 NULL);
2549 if (sep != NULL)
2550 aprint_verbose("\n");
2551 }
2552 if (ci->ci_cinfo[CAI_L2_1GBDTLB].cai_totalsize != 0) {
2553 sep = print_tlb_config(ci, CAI_L2_1GBDTLB, "L2 1GB page DTLB",
2554 NULL);
2555 if (sep != NULL)
2556 aprint_verbose("\n");
2557 }
2558 }
2559
2560 static void
2561 powernow_probe(struct cpu_info *ci)
2562 {
2563 uint32_t regs[4];
2564 char buf[256];
2565
2566 x86_cpuid(0x80000007, regs);
2567
2568 snprintb(buf, sizeof(buf), CPUID_APM_FLAGS, regs[3]);
2569 aprint_normal_dev(ci->ci_dev, "AMD Power Management features: %s\n",
2570 buf);
2571 }
2572
2573 bool
2574 identifycpu_bind(void)
2575 {
2576
2577 return true;
2578 }
2579
2580 int
2581 ucodeupdate_check(int fd, struct cpu_ucode *uc)
2582 {
2583 struct cpu_info ci;
2584 int loader_version, res;
2585 struct cpu_ucode_version versreq;
2586
2587 cpu_probe_base_features(&ci, "unknown");
2588
2589 if (!strcmp((char *)ci.ci_vendor, "AuthenticAMD"))
2590 loader_version = CPU_UCODE_LOADER_AMD;
2591 else if (!strcmp((char *)ci.ci_vendor, "GenuineIntel"))
2592 loader_version = CPU_UCODE_LOADER_INTEL1;
2593 else
2594 return -1;
2595
2596 /* check whether the kernel understands this loader version */
2597 versreq.loader_version = loader_version;
2598 versreq.data = 0;
2599 res = ioctl(fd, IOC_CPU_UCODE_GET_VERSION, &versreq);
2600 if (res)
2601 return -1;
2602
2603 switch (loader_version) {
2604 case CPU_UCODE_LOADER_AMD:
2605 if (uc->cpu_nr != -1) {
2606 /* printf? */
2607 return -1;
2608 }
2609 uc->cpu_nr = CPU_UCODE_ALL_CPUS;
2610 break;
2611 case CPU_UCODE_LOADER_INTEL1:
2612 if (uc->cpu_nr == -1)
2613 uc->cpu_nr = CPU_UCODE_ALL_CPUS; /* for Xen */
2614 else
2615 uc->cpu_nr = CPU_UCODE_CURRENT_CPU;
2616 break;
2617 default: /* can't happen */
2618 return -1;
2619 }
2620 uc->loader_version = loader_version;
2621 return 0;
2622 }
2623