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