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