xref: /netbsd-src/usr.sbin/cpuctl/arch/i386.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: i386.c,v 1.56 2014/07/03 04:11:37 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.56 2014/07/03 04:11:37 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 struct x86_cache_info *cache_info_lookup(
227     const struct x86_cache_info *, uint8_t);
228 static const char *print_cache_config(struct cpu_info *, int, const char *,
229     const char *);
230 static const char *print_tlb_config(struct cpu_info *, int, const char *,
231     const char *);
232 static void	x86_print_cache_and_tlb_info(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 				else if ((verbose != 0) && (desc != 0xff))
1001 					printf("Unknown cacheinfo desc %02x\n",
1002 					    desc);
1003 			}
1004 		}
1005 		x86_cpuid(2, descs);
1006 	}
1007 
1008 	if (ci->ci_cpuid_level < 4)
1009 		return;
1010 
1011 	/* Parse the cache info from `cpuid leaf 4', if we have it. */
1012 	for (i = 0; ; i++) {
1013 		x86_cpuid2(4, i, descs);
1014 		type = __SHIFTOUT(descs[0], CPUID_DCP_CACHETYPE);
1015 		if (type == CPUID_DCP_CACHETYPE_N)
1016 			break;
1017 		level = __SHIFTOUT(descs[0], CPUID_DCP_CACHELEVEL);
1018 		switch (level) {
1019 		case 1:
1020 			if (type == CPUID_DCP_CACHETYPE_I)
1021 				caitype = CAI_ICACHE;
1022 			else if (type == CPUID_DCP_CACHETYPE_D)
1023 				caitype = CAI_DCACHE;
1024 			else
1025 				caitype = -1;
1026 			break;
1027 		case 2:
1028 			if (type == CPUID_DCP_CACHETYPE_U)
1029 				caitype = CAI_L2CACHE;
1030 			else
1031 				caitype = -1;
1032 			break;
1033 		case 3:
1034 			if (type == CPUID_DCP_CACHETYPE_U)
1035 				caitype = CAI_L3CACHE;
1036 			else
1037 				caitype = -1;
1038 			break;
1039 		default:
1040 			caitype = -1;
1041 			break;
1042 		}
1043 		if (caitype == -1) {
1044 			printf("unknown cache level&type (%d & %d)\n",
1045 			    level, type);
1046 			continue;
1047 		}
1048 		ways = __SHIFTOUT(descs[1], CPUID_DCP_WAYS) + 1;
1049 		partitions =__SHIFTOUT(descs[1], CPUID_DCP_PARTITIONS)
1050 		    + 1;
1051 		linesize = __SHIFTOUT(descs[1], CPUID_DCP_LINESIZE)
1052 		    + 1;
1053 		sets = descs[2] + 1;
1054 		totalsize = ways * partitions * linesize * sets;
1055 		ci->ci_cinfo[caitype].cai_totalsize = totalsize;
1056 		ci->ci_cinfo[caitype].cai_associativity = ways;
1057 		ci->ci_cinfo[caitype].cai_linesize = linesize;
1058 	}
1059 }
1060 
1061 static const struct x86_cache_info amd_cpuid_l2cache_assoc_info[] =
1062     AMD_L2CACHE_INFO;
1063 
1064 static const struct x86_cache_info amd_cpuid_l3cache_assoc_info[] =
1065     AMD_L3CACHE_INFO;
1066 
1067 static void
1068 amd_cpu_cacheinfo(struct cpu_info *ci)
1069 {
1070 	const struct x86_cache_info *cp;
1071 	struct x86_cache_info *cai;
1072 	u_int descs[4];
1073 	u_int lfunc;
1074 
1075 	/*
1076 	 * K5 model 0 has none of this info.
1077 	 */
1078 	if (ci->ci_family == 5 && ci->ci_model == 0)
1079 		return;
1080 
1081 	/*
1082 	 * Determine the largest extended function value.
1083 	 */
1084 	x86_cpuid(0x80000000, descs);
1085 	lfunc = descs[0];
1086 
1087 	/*
1088 	 * Determine L1 cache/TLB info.
1089 	 */
1090 	if (lfunc < 0x80000005) {
1091 		/* No L1 cache info available. */
1092 		return;
1093 	}
1094 
1095 	x86_cpuid(0x80000005, descs);
1096 
1097 	/*
1098 	 * K6-III and higher have large page TLBs.
1099 	 */
1100 	if ((ci->ci_family == 5 && ci->ci_model >= 9) || ci->ci_family >= 6) {
1101 		cai = &ci->ci_cinfo[CAI_ITLB2];
1102 		cai->cai_totalsize = AMD_L1_EAX_ITLB_ENTRIES(descs[0]);
1103 		cai->cai_associativity = AMD_L1_EAX_ITLB_ASSOC(descs[0]);
1104 		cai->cai_linesize = largepagesize;
1105 
1106 		cai = &ci->ci_cinfo[CAI_DTLB2];
1107 		cai->cai_totalsize = AMD_L1_EAX_DTLB_ENTRIES(descs[0]);
1108 		cai->cai_associativity = AMD_L1_EAX_DTLB_ASSOC(descs[0]);
1109 		cai->cai_linesize = largepagesize;
1110 	}
1111 
1112 	cai = &ci->ci_cinfo[CAI_ITLB];
1113 	cai->cai_totalsize = AMD_L1_EBX_ITLB_ENTRIES(descs[1]);
1114 	cai->cai_associativity = AMD_L1_EBX_ITLB_ASSOC(descs[1]);
1115 	cai->cai_linesize = (4 * 1024);
1116 
1117 	cai = &ci->ci_cinfo[CAI_DTLB];
1118 	cai->cai_totalsize = AMD_L1_EBX_DTLB_ENTRIES(descs[1]);
1119 	cai->cai_associativity = AMD_L1_EBX_DTLB_ASSOC(descs[1]);
1120 	cai->cai_linesize = (4 * 1024);
1121 
1122 	cai = &ci->ci_cinfo[CAI_DCACHE];
1123 	cai->cai_totalsize = AMD_L1_ECX_DC_SIZE(descs[2]);
1124 	cai->cai_associativity = AMD_L1_ECX_DC_ASSOC(descs[2]);
1125 	cai->cai_linesize = AMD_L1_ECX_DC_LS(descs[2]);
1126 
1127 	cai = &ci->ci_cinfo[CAI_ICACHE];
1128 	cai->cai_totalsize = AMD_L1_EDX_IC_SIZE(descs[3]);
1129 	cai->cai_associativity = AMD_L1_EDX_IC_ASSOC(descs[3]);
1130 	cai->cai_linesize = AMD_L1_EDX_IC_LS(descs[3]);
1131 
1132 	/*
1133 	 * Determine L2 cache/TLB info.
1134 	 */
1135 	if (lfunc < 0x80000006) {
1136 		/* No L2 cache info available. */
1137 		return;
1138 	}
1139 
1140 	x86_cpuid(0x80000006, descs);
1141 
1142 	cai = &ci->ci_cinfo[CAI_L2_ITLB];
1143 	cai->cai_totalsize = AMD_L2_EBX_IUTLB_ENTRIES(descs[1]);
1144 	cai->cai_associativity = AMD_L2_EBX_IUTLB_ASSOC(descs[1]);
1145 	cai->cai_linesize = (4 * 1024);
1146 	cp = cache_info_lookup(amd_cpuid_l2cache_assoc_info,
1147 	    cai->cai_associativity);
1148 	if (cp != NULL)
1149 		cai->cai_associativity = cp->cai_associativity;
1150 	else
1151 		cai->cai_associativity = 0;	/* XXX Unknown/reserved */
1152 
1153 	cai = &ci->ci_cinfo[CAI_L2_ITLB2];
1154 	cai->cai_totalsize = AMD_L2_EAX_IUTLB_ENTRIES(descs[0]);
1155 	cai->cai_associativity = AMD_L2_EAX_IUTLB_ASSOC(descs[0]);
1156 	cai->cai_linesize = largepagesize;
1157 	cp = cache_info_lookup(amd_cpuid_l2cache_assoc_info,
1158 	    cai->cai_associativity);
1159 	if (cp != NULL)
1160 		cai->cai_associativity = cp->cai_associativity;
1161 	else
1162 		cai->cai_associativity = 0;	/* XXX Unknown/reserved */
1163 
1164 	cai = &ci->ci_cinfo[CAI_L2_DTLB];
1165 	cai->cai_totalsize = AMD_L2_EBX_DTLB_ENTRIES(descs[1]);
1166 	cai->cai_associativity = AMD_L2_EBX_DTLB_ASSOC(descs[1]);
1167 	cai->cai_linesize = (4 * 1024);
1168 	cp = cache_info_lookup(amd_cpuid_l2cache_assoc_info,
1169 	    cai->cai_associativity);
1170 	if (cp != NULL)
1171 		cai->cai_associativity = cp->cai_associativity;
1172 	else
1173 		cai->cai_associativity = 0;	/* XXX Unknown/reserved */
1174 
1175 	cai = &ci->ci_cinfo[CAI_L2_DTLB2];
1176 	cai->cai_totalsize = AMD_L2_EAX_DTLB_ENTRIES(descs[0]);
1177 	cai->cai_associativity = AMD_L2_EAX_DTLB_ASSOC(descs[0]);
1178 	cai->cai_linesize = largepagesize;
1179 	cp = cache_info_lookup(amd_cpuid_l2cache_assoc_info,
1180 	    cai->cai_associativity);
1181 	if (cp != NULL)
1182 		cai->cai_associativity = cp->cai_associativity;
1183 	else
1184 		cai->cai_associativity = 0;	/* XXX Unknown/reserved */
1185 
1186 	cai = &ci->ci_cinfo[CAI_L2CACHE];
1187 	cai->cai_totalsize = AMD_L2_ECX_C_SIZE(descs[2]);
1188 	cai->cai_associativity = AMD_L2_ECX_C_ASSOC(descs[2]);
1189 	cai->cai_linesize = AMD_L2_ECX_C_LS(descs[2]);
1190 
1191 	cp = cache_info_lookup(amd_cpuid_l2cache_assoc_info,
1192 	    cai->cai_associativity);
1193 	if (cp != NULL)
1194 		cai->cai_associativity = cp->cai_associativity;
1195 	else
1196 		cai->cai_associativity = 0;	/* XXX Unknown/reserved */
1197 
1198 	/*
1199 	 * Determine L3 cache info on AMD Family 10h and newer processors
1200 	 */
1201 	if (ci->ci_family >= 0x10) {
1202 		cai = &ci->ci_cinfo[CAI_L3CACHE];
1203 		cai->cai_totalsize = AMD_L3_EDX_C_SIZE(descs[3]);
1204 		cai->cai_associativity = AMD_L3_EDX_C_ASSOC(descs[3]);
1205 		cai->cai_linesize = AMD_L3_EDX_C_LS(descs[3]);
1206 
1207 		cp = cache_info_lookup(amd_cpuid_l3cache_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 Unkn/Rsvd */
1213 	}
1214 
1215 	/*
1216 	 * Determine 1GB TLB info.
1217 	 */
1218 	if (lfunc < 0x80000019) {
1219 		/* No 1GB TLB info available. */
1220 		return;
1221 	}
1222 
1223 	x86_cpuid(0x80000019, descs);
1224 
1225 	cai = &ci->ci_cinfo[CAI_L1_1GBITLB];
1226 	cai->cai_totalsize = AMD_L1_1GB_EAX_IUTLB_ENTRIES(descs[0]);
1227 	cai->cai_associativity = AMD_L1_1GB_EAX_IUTLB_ASSOC(descs[0]);
1228 	cai->cai_linesize = (1024 * 1024 * 1024);
1229 	cp = cache_info_lookup(amd_cpuid_l2cache_assoc_info,
1230 	    cai->cai_associativity);
1231 	if (cp != NULL)
1232 		cai->cai_associativity = cp->cai_associativity;
1233 	else
1234 		cai->cai_associativity = 0;	/* XXX Unknown/reserved */
1235 
1236 	cai = &ci->ci_cinfo[CAI_L1_1GBDTLB];
1237 	cai->cai_totalsize = AMD_L1_1GB_EAX_DTLB_ENTRIES(descs[0]);
1238 	cai->cai_associativity = AMD_L1_1GB_EAX_DTLB_ASSOC(descs[0]);
1239 	cai->cai_linesize = (1024 * 1024 * 1024);
1240 	cp = cache_info_lookup(amd_cpuid_l2cache_assoc_info,
1241 	    cai->cai_associativity);
1242 	if (cp != NULL)
1243 		cai->cai_associativity = cp->cai_associativity;
1244 	else
1245 		cai->cai_associativity = 0;	/* XXX Unknown/reserved */
1246 
1247 	cai = &ci->ci_cinfo[CAI_L2_1GBITLB];
1248 	cai->cai_totalsize = AMD_L2_1GB_EBX_IUTLB_ENTRIES(descs[1]);
1249 	cai->cai_associativity = AMD_L2_1GB_EBX_IUTLB_ASSOC(descs[1]);
1250 	cai->cai_linesize = (1024 * 1024 * 1024);
1251 	cp = cache_info_lookup(amd_cpuid_l2cache_assoc_info,
1252 	    cai->cai_associativity);
1253 	if (cp != NULL)
1254 		cai->cai_associativity = cp->cai_associativity;
1255 	else
1256 		cai->cai_associativity = 0;	/* XXX Unknown/reserved */
1257 
1258 	cai = &ci->ci_cinfo[CAI_L2_1GBDTLB];
1259 	cai->cai_totalsize = AMD_L2_1GB_EBX_DUTLB_ENTRIES(descs[1]);
1260 	cai->cai_associativity = AMD_L2_1GB_EBX_DUTLB_ASSOC(descs[1]);
1261 	cai->cai_linesize = (1024 * 1024 * 1024);
1262 	cp = cache_info_lookup(amd_cpuid_l2cache_assoc_info,
1263 	    cai->cai_associativity);
1264 	if (cp != NULL)
1265 		cai->cai_associativity = cp->cai_associativity;
1266 	else
1267 		cai->cai_associativity = 0;	/* XXX Unknown/reserved */
1268 }
1269 
1270 static void
1271 via_cpu_cacheinfo(struct cpu_info *ci)
1272 {
1273 	struct x86_cache_info *cai;
1274 	int stepping;
1275 	u_int descs[4];
1276 	u_int lfunc;
1277 
1278 	stepping = CPUID_TO_STEPPING(ci->ci_signature);
1279 
1280 	/*
1281 	 * Determine the largest extended function value.
1282 	 */
1283 	x86_cpuid(0x80000000, descs);
1284 	lfunc = descs[0];
1285 
1286 	/*
1287 	 * Determine L1 cache/TLB info.
1288 	 */
1289 	if (lfunc < 0x80000005) {
1290 		/* No L1 cache info available. */
1291 		return;
1292 	}
1293 
1294 	x86_cpuid(0x80000005, descs);
1295 
1296 	cai = &ci->ci_cinfo[CAI_ITLB];
1297 	cai->cai_totalsize = VIA_L1_EBX_ITLB_ENTRIES(descs[1]);
1298 	cai->cai_associativity = VIA_L1_EBX_ITLB_ASSOC(descs[1]);
1299 	cai->cai_linesize = (4 * 1024);
1300 
1301 	cai = &ci->ci_cinfo[CAI_DTLB];
1302 	cai->cai_totalsize = VIA_L1_EBX_DTLB_ENTRIES(descs[1]);
1303 	cai->cai_associativity = VIA_L1_EBX_DTLB_ASSOC(descs[1]);
1304 	cai->cai_linesize = (4 * 1024);
1305 
1306 	cai = &ci->ci_cinfo[CAI_DCACHE];
1307 	cai->cai_totalsize = VIA_L1_ECX_DC_SIZE(descs[2]);
1308 	cai->cai_associativity = VIA_L1_ECX_DC_ASSOC(descs[2]);
1309 	cai->cai_linesize = VIA_L1_EDX_IC_LS(descs[2]);
1310 	if (ci->ci_model == 9 && stepping == 8) {
1311 		/* Erratum: stepping 8 reports 4 when it should be 2 */
1312 		cai->cai_associativity = 2;
1313 	}
1314 
1315 	cai = &ci->ci_cinfo[CAI_ICACHE];
1316 	cai->cai_totalsize = VIA_L1_EDX_IC_SIZE(descs[3]);
1317 	cai->cai_associativity = VIA_L1_EDX_IC_ASSOC(descs[3]);
1318 	cai->cai_linesize = VIA_L1_EDX_IC_LS(descs[3]);
1319 	if (ci->ci_model == 9 && stepping == 8) {
1320 		/* Erratum: stepping 8 reports 4 when it should be 2 */
1321 		cai->cai_associativity = 2;
1322 	}
1323 
1324 	/*
1325 	 * Determine L2 cache/TLB info.
1326 	 */
1327 	if (lfunc < 0x80000006) {
1328 		/* No L2 cache info available. */
1329 		return;
1330 	}
1331 
1332 	x86_cpuid(0x80000006, descs);
1333 
1334 	cai = &ci->ci_cinfo[CAI_L2CACHE];
1335 	if (ci->ci_model >= 9) {
1336 		cai->cai_totalsize = VIA_L2N_ECX_C_SIZE(descs[2]);
1337 		cai->cai_associativity = VIA_L2N_ECX_C_ASSOC(descs[2]);
1338 		cai->cai_linesize = VIA_L2N_ECX_C_LS(descs[2]);
1339 	} else {
1340 		cai->cai_totalsize = VIA_L2_ECX_C_SIZE(descs[2]);
1341 		cai->cai_associativity = VIA_L2_ECX_C_ASSOC(descs[2]);
1342 		cai->cai_linesize = VIA_L2_ECX_C_LS(descs[2]);
1343 	}
1344 }
1345 
1346 static void
1347 tmx86_get_longrun_status(u_int *frequency, u_int *voltage, u_int *percentage)
1348 {
1349 	u_int descs[4];
1350 
1351 	x86_cpuid(0x80860007, descs);
1352 	*frequency = descs[0];
1353 	*voltage = descs[1];
1354 	*percentage = descs[2];
1355 }
1356 
1357 static void
1358 transmeta_cpu_info(struct cpu_info *ci)
1359 {
1360 	u_int descs[4], nreg;
1361 	u_int frequency, voltage, percentage;
1362 
1363 	x86_cpuid(0x80860000, descs);
1364 	nreg = descs[0];
1365 	if (nreg >= 0x80860001) {
1366 		x86_cpuid(0x80860001, descs);
1367 		aprint_verbose_dev(ci->ci_dev, "Processor revision %u.%u.%u.%u\n",
1368 		    (descs[1] >> 24) & 0xff,
1369 		    (descs[1] >> 16) & 0xff,
1370 		    (descs[1] >> 8) & 0xff,
1371 		    descs[1] & 0xff);
1372 	}
1373 	if (nreg >= 0x80860002) {
1374 		x86_cpuid(0x80860002, descs);
1375 		aprint_verbose_dev(ci->ci_dev, "Code Morphing Software Rev: %u.%u.%u-%u-%u\n",
1376 		    (descs[1] >> 24) & 0xff,
1377 		    (descs[1] >> 16) & 0xff,
1378 		    (descs[1] >> 8) & 0xff,
1379 		    descs[1] & 0xff,
1380 		    descs[2]);
1381 	}
1382 	if (nreg >= 0x80860006) {
1383 		union {
1384 			char text[65];
1385 			u_int descs[4][4];
1386 		} info;
1387 		int i;
1388 
1389 		for (i=0; i<4; i++) {
1390 			x86_cpuid(0x80860003 + i, info.descs[i]);
1391 		}
1392 		info.text[64] = '\0';
1393 		aprint_verbose_dev(ci->ci_dev, "%s\n", info.text);
1394 	}
1395 
1396 	if (nreg >= 0x80860007) {
1397 		tmx86_get_longrun_status(&frequency,
1398 		    &voltage, &percentage);
1399 		aprint_verbose_dev(ci->ci_dev, "LongRun <%dMHz %dmV %d%%>\n",
1400 		    frequency, voltage, percentage);
1401 	}
1402 }
1403 
1404 static void
1405 cpu_probe_base_features(struct cpu_info *ci, const char *cpuname)
1406 {
1407 	u_int descs[4];
1408 	int i;
1409 	uint32_t brand[12];
1410 
1411 	memset(ci, 0, sizeof(*ci));
1412 	ci->ci_dev = cpuname;
1413 
1414 	ci->ci_cpu_type = x86_identify();
1415 	if (ci->ci_cpu_type >= 0) {
1416 		/* Old pre-cpuid instruction cpu */
1417 		ci->ci_cpuid_level = -1;
1418 		return;
1419 	}
1420 
1421 	/*
1422 	 * This CPU supports cpuid instruction, so we can call x86_cpuid()
1423 	 * function.
1424 	 */
1425 
1426 	/*
1427 	 * Fn0000_0000:
1428 	 * - Save cpuid max level.
1429 	 * - Save vendor string.
1430 	 */
1431 	x86_cpuid(0, descs);
1432 	ci->ci_cpuid_level = descs[0];
1433 	/* Save vendor string */
1434 	ci->ci_vendor[0] = descs[1];
1435 	ci->ci_vendor[2] = descs[2];
1436 	ci->ci_vendor[1] = descs[3];
1437 	ci->ci_vendor[3] = 0;
1438 
1439 	aprint_verbose("%s: highest basic info %08x\n", cpuname,
1440 	    ci->ci_cpuid_level);
1441 	if (verbose) {
1442 		int bf;
1443 
1444 		for (bf = 0; bf <= ci->ci_cpuid_level; bf++) {
1445 			x86_cpuid(bf, descs);
1446 			printf("%s: %08x: %08x %08x %08x %08x\n", cpuname,
1447 			    bf, descs[0], descs[1], descs[2], descs[3]);
1448 		}
1449 	}
1450 
1451 	/*
1452 	 * Fn8000_0000:
1453 	 * - Get cpuid extended function's max level.
1454 	 */
1455 	x86_cpuid(0x80000000, descs);
1456 	if (descs[0] >=  0x80000000) {
1457 		ci->ci_cpuid_extlevel = descs[0];
1458 		aprint_verbose("%s: highest extended info %08x\n", cpuname,
1459 		    ci->ci_cpuid_extlevel);
1460 	} else {
1461 		/* Set lower value than 0x80000000 */
1462 		ci->ci_cpuid_extlevel = 0;
1463 	}
1464 	if (verbose) {
1465 		unsigned int ef;
1466 
1467 		for (ef = 0x80000000; ef <= ci->ci_cpuid_extlevel; ef++) {
1468 			x86_cpuid(ef, descs);
1469 			printf("%s: %08x: %08x %08x %08x %08x\n", cpuname,
1470 			    ef, descs[0], descs[1], descs[2], descs[3]);
1471 		}
1472 	}
1473 
1474 	/*
1475 	 * Fn8000_000[2-4]:
1476 	 * - Save brand string.
1477 	 */
1478 	if (ci->ci_cpuid_extlevel >= 0x80000004) {
1479 		x86_cpuid(0x80000002, brand);
1480 		x86_cpuid(0x80000003, brand + 4);
1481 		x86_cpuid(0x80000004, brand + 8);
1482 		for (i = 0; i < 48; i++)
1483 			if (((char *) brand)[i] != ' ')
1484 				break;
1485 		memcpy(cpu_brand_string, ((char *) brand) + i, 48 - i);
1486 	}
1487 
1488 	if (ci->ci_cpuid_level < 1)
1489 		return;
1490 
1491 	/*
1492 	 * Fn0000_0001:
1493 	 * - Get CPU family, model and stepping (from eax).
1494 	 * - Initial local APIC ID and brand ID (from ebx)
1495 	 * - CPUID2 (from ecx)
1496 	 * - CPUID (from edx)
1497 	 */
1498 	x86_cpuid(1, descs);
1499 	ci->ci_signature = descs[0];
1500 
1501 	/* Extract full family/model values */
1502 	ci->ci_family = CPUID_TO_FAMILY(ci->ci_signature);
1503 	ci->ci_model = CPUID_TO_MODEL(ci->ci_signature);
1504 
1505 	/* Brand is low order 8 bits of ebx */
1506 	ci->ci_brand_id = descs[1] & 0xff;
1507 	/* Initial local APIC ID */
1508 	ci->ci_initapicid = (descs[1] >> 24) & 0xff;
1509 
1510 	ci->ci_feat_val[1] = descs[2];
1511 	ci->ci_feat_val[0] = descs[3];
1512 
1513 	if (ci->ci_cpuid_level < 3)
1514 		return;
1515 
1516 	/*
1517 	 * If the processor serial number misfeature is present and supported,
1518 	 * extract it here.
1519 	 */
1520 	if ((ci->ci_feat_val[0] & CPUID_PN) != 0) {
1521 		ci->ci_cpu_serial[0] = ci->ci_signature;
1522 		x86_cpuid(3, descs);
1523 		ci->ci_cpu_serial[2] = descs[2];
1524 		ci->ci_cpu_serial[1] = descs[3];
1525 	}
1526 
1527 	if (ci->ci_cpuid_level < 0xd)
1528 		return;
1529 
1530 	/* Get support XCR0 bits */
1531 	x86_cpuid2(0xd, 0, descs);
1532 	ci->ci_feat_val[5] = descs[0];	/* Actually 64 bits */
1533 	ci->ci_cur_xsave = descs[1];
1534 	ci->ci_max_xsave = descs[2];
1535 
1536 	/* Additional flags (eg xsaveopt support) */
1537 	x86_cpuid2(0xd, 1, descs);
1538 	ci->ci_feat_val[6] = descs[0];   /* Actually 64 bits */
1539 }
1540 
1541 static void
1542 cpu_probe_features(struct cpu_info *ci)
1543 {
1544 	const struct cpu_cpuid_nameclass *cpup = NULL;
1545 	unsigned int i;
1546 
1547 	if (ci->ci_cpuid_level < 1)
1548 		return;
1549 
1550 	for (i = 0; i < __arraycount(i386_cpuid_cpus); i++) {
1551 		if (!strncmp((char *)ci->ci_vendor,
1552 		    i386_cpuid_cpus[i].cpu_id, 12)) {
1553 			cpup = &i386_cpuid_cpus[i];
1554 			break;
1555 		}
1556 	}
1557 
1558 	if (cpup == NULL)
1559 		return;
1560 
1561 	i = ci->ci_family - CPU_MINFAMILY;
1562 
1563 	if (i >= __arraycount(cpup->cpu_family))
1564 		i = __arraycount(cpup->cpu_family) - 1;
1565 
1566 	if (cpup->cpu_family[i].cpu_probe == NULL)
1567 		return;
1568 
1569 	(*cpup->cpu_family[i].cpu_probe)(ci);
1570 }
1571 
1572 static void
1573 print_bits(const char *cpuname, const char *hdr, const char *fmt, uint32_t val)
1574 {
1575 	char buf[32 * 16];
1576 	char *bp;
1577 
1578 #define	MAX_LINE_LEN	79	/* get from command arg or 'stty cols' ? */
1579 
1580 	if (val == 0 || fmt == NULL)
1581 		return;
1582 
1583 	snprintb_m(buf, sizeof(buf), fmt, val,
1584 	    MAX_LINE_LEN - strlen(cpuname) - 2 - strlen(hdr) - 1);
1585 	bp = buf;
1586 	while (*bp != '\0') {
1587 		aprint_verbose("%s: %s %s\n", cpuname, hdr, bp);
1588 		bp += strlen(bp) + 1;
1589 	}
1590 }
1591 
1592 static void
1593 identifycpu_cpuids(struct cpu_info *ci)
1594 {
1595 	const char *cpuname = ci->ci_dev;
1596 	u_int lp_max = 1;	/* logical processors per package */
1597 	u_int smt_max;		/* smt per core */
1598 	u_int core_max = 1;	/* core per package */
1599 	u_int smt_bits, core_bits;
1600 	uint32_t descs[4];
1601 
1602 	aprint_verbose("%s: Initial APIC ID %u\n", cpuname, ci->ci_initapicid);
1603 	ci->ci_packageid = ci->ci_initapicid;
1604 	ci->ci_coreid = 0;
1605 	ci->ci_smtid = 0;
1606 	if (cpu_vendor != CPUVENDOR_INTEL) {
1607 		return;
1608 	}
1609 
1610 	/*
1611 	 * 253668.pdf 7.10.2
1612 	 */
1613 
1614 	if ((ci->ci_feat_val[0] & CPUID_HTT) != 0) {
1615 		x86_cpuid(1, descs);
1616 		lp_max = (descs[1] >> 16) & 0xff;
1617 	}
1618 	if (ci->ci_cpuid_level >= 4) {
1619 		x86_cpuid2(4, 0, descs);
1620 		core_max = (descs[0] >> 26) + 1;
1621 	}
1622 	assert(lp_max >= core_max);
1623 	smt_max = lp_max / core_max;
1624 	smt_bits = ilog2(smt_max - 1) + 1;
1625 	core_bits = ilog2(core_max - 1) + 1;
1626 	if (smt_bits + core_bits) {
1627 		ci->ci_packageid = ci->ci_initapicid >> (smt_bits + core_bits);
1628 	}
1629 	aprint_verbose("%s: Cluster/Package ID %u\n", cpuname,
1630 	    ci->ci_packageid);
1631 	if (core_bits) {
1632 		u_int core_mask = __BITS(smt_bits, smt_bits + core_bits - 1);
1633 
1634 		ci->ci_coreid =
1635 		    __SHIFTOUT(ci->ci_initapicid, core_mask);
1636 		aprint_verbose("%s: Core ID %u\n", cpuname, ci->ci_coreid);
1637 	}
1638 	if (smt_bits) {
1639 		u_int smt_mask = __BITS((int)0, (int)(smt_bits - 1));
1640 
1641 		ci->ci_smtid = __SHIFTOUT(ci->ci_initapicid, smt_mask);
1642 		aprint_verbose("%s: SMT ID %u\n", cpuname, ci->ci_smtid);
1643 	}
1644 }
1645 
1646 void
1647 identifycpu(int fd, const char *cpuname)
1648 {
1649 	const char *name = "", *modifier, *vendorname, *brand = "";
1650 	int class = CPUCLASS_386;
1651 	unsigned int i;
1652 	int modif, family;
1653 	const struct cpu_cpuid_nameclass *cpup = NULL;
1654 	const struct cpu_cpuid_family *cpufam;
1655 	struct cpu_info *ci, cistore;
1656 	size_t sz;
1657 	struct cpu_ucode_version ucode;
1658 	union {
1659 		struct cpu_ucode_version_amd amd;
1660 		struct cpu_ucode_version_intel1 intel1;
1661 	} ucvers;
1662 
1663 	ci = &cistore;
1664 	cpu_probe_base_features(ci, cpuname);
1665 	cpu_probe_features(ci);
1666 
1667 	if (ci->ci_cpu_type >= 0) {
1668 		/* Old pre-cpuid instruction cpu */
1669 		if (ci->ci_cpu_type >= (int)__arraycount(i386_nocpuid_cpus))
1670 			errx(1, "unknown cpu type %d", ci->ci_cpu_type);
1671 		name = i386_nocpuid_cpus[ci->ci_cpu_type].cpu_name;
1672 		cpu_vendor = i386_nocpuid_cpus[ci->ci_cpu_type].cpu_vendor;
1673 		vendorname = i386_nocpuid_cpus[ci->ci_cpu_type].cpu_vendorname;
1674 		class = i386_nocpuid_cpus[ci->ci_cpu_type].cpu_class;
1675 		ci->ci_info = i386_nocpuid_cpus[ci->ci_cpu_type].cpu_info;
1676 		modifier = "";
1677 	} else {
1678 		/* CPU which support cpuid instruction */
1679 		modif = (ci->ci_signature >> 12) & 0x3;
1680 		family = ci->ci_family;
1681 		if (family < CPU_MINFAMILY)
1682 			errx(1, "identifycpu: strange family value");
1683 		if (family > CPU_MAXFAMILY)
1684 			family = CPU_MAXFAMILY;
1685 
1686 		for (i = 0; i < __arraycount(i386_cpuid_cpus); i++) {
1687 			if (!strncmp((char *)ci->ci_vendor,
1688 			    i386_cpuid_cpus[i].cpu_id, 12)) {
1689 				cpup = &i386_cpuid_cpus[i];
1690 				break;
1691 			}
1692 		}
1693 
1694 		if (cpup == NULL) {
1695 			cpu_vendor = CPUVENDOR_UNKNOWN;
1696 			if (ci->ci_vendor[0] != '\0')
1697 				vendorname = (char *)&ci->ci_vendor[0];
1698 			else
1699 				vendorname = "Unknown";
1700 			class = family - 3;
1701 			modifier = "";
1702 			name = "";
1703 			ci->ci_info = NULL;
1704 		} else {
1705 			cpu_vendor = cpup->cpu_vendor;
1706 			vendorname = cpup->cpu_vendorname;
1707 			modifier = modifiers[modif];
1708 			cpufam = &cpup->cpu_family[family - CPU_MINFAMILY];
1709 			name = cpufam->cpu_models[ci->ci_model];
1710 			if (name == NULL || *name == '\0')
1711 			    name = cpufam->cpu_model_default;
1712 			class = cpufam->cpu_class;
1713 			ci->ci_info = cpufam->cpu_info;
1714 
1715 			if (cpu_vendor == CPUVENDOR_INTEL) {
1716 				if (ci->ci_family == 6 && ci->ci_model >= 5) {
1717 					const char *tmp;
1718 					tmp = intel_family6_name(ci);
1719 					if (tmp != NULL)
1720 						name = tmp;
1721 				}
1722 				if (ci->ci_family == 15 &&
1723 				    ci->ci_brand_id <
1724 				    __arraycount(i386_intel_brand) &&
1725 				    i386_intel_brand[ci->ci_brand_id])
1726 					name =
1727 					     i386_intel_brand[ci->ci_brand_id];
1728 			}
1729 
1730 			if (cpu_vendor == CPUVENDOR_AMD) {
1731 				if (ci->ci_family == 6 && ci->ci_model >= 6) {
1732 					if (ci->ci_brand_id == 1)
1733 						/*
1734 						 * It's Duron. We override the
1735 						 * name, since it might have
1736 						 * been misidentified as Athlon.
1737 						 */
1738 						name =
1739 						    amd_brand[ci->ci_brand_id];
1740 					else
1741 						brand = amd_brand_name;
1742 				}
1743 				if (CPUID_TO_BASEFAMILY(ci->ci_signature)
1744 				    == 0xf) {
1745 					/* Identify AMD64 CPU names.  */
1746 					const char *tmp;
1747 					tmp = amd_amd64_name(ci);
1748 					if (tmp != NULL)
1749 						name = tmp;
1750 				}
1751 			}
1752 
1753 			if (cpu_vendor == CPUVENDOR_IDT && ci->ci_family >= 6)
1754 				vendorname = "VIA";
1755 		}
1756 	}
1757 
1758 	ci->ci_cpu_class = class;
1759 
1760 	sz = sizeof(ci->ci_tsc_freq);
1761 	(void)sysctlbyname("machdep.tsc_freq", &ci->ci_tsc_freq, &sz, NULL, 0);
1762 	sz = sizeof(use_pae);
1763 	(void)sysctlbyname("machdep.pae", &use_pae, &sz, NULL, 0);
1764 	largepagesize = (use_pae ? 2 * 1024 * 1024 : 4 * 1024 * 1024);
1765 
1766 	/*
1767 	 * The 'cpu_brand_string' is much more useful than the 'cpu_model'
1768 	 * we try to determine from the family/model values.
1769 	 */
1770 	if (*cpu_brand_string != '\0')
1771 		aprint_normal("%s: \"%s\"\n", cpuname, cpu_brand_string);
1772 
1773 	aprint_normal("%s: %s", cpuname, vendorname);
1774 	if (*modifier)
1775 		aprint_normal(" %s", modifier);
1776 	if (*name)
1777 		aprint_normal(" %s", name);
1778 	if (*brand)
1779 		aprint_normal(" %s", brand);
1780 	aprint_normal(" (%s-class)", classnames[class]);
1781 
1782 	if (ci->ci_tsc_freq != 0)
1783 		aprint_normal(", %ju.%02ju MHz\n",
1784 		    ((uintmax_t)ci->ci_tsc_freq + 4999) / 1000000,
1785 		    (((uintmax_t)ci->ci_tsc_freq + 4999) / 10000) % 100);
1786 
1787 	aprint_normal_dev(ci->ci_dev, "family %#x model %#x stepping %#x",
1788 	    ci->ci_family, ci->ci_model, CPUID_TO_STEPPING(ci->ci_signature));
1789 	if (ci->ci_signature != 0)
1790 		aprint_normal(" (id %#x)", ci->ci_signature);
1791 	aprint_normal("\n");
1792 
1793 	if (ci->ci_info)
1794 		(*ci->ci_info)(ci);
1795 
1796 	/*
1797 	 * display CPU feature flags
1798 	 */
1799 
1800 	print_bits(cpuname, "features", CPUID_FLAGS1, ci->ci_feat_val[0]);
1801 	print_bits(cpuname, "features1", CPUID2_FLAGS1, ci->ci_feat_val[1]);
1802 
1803 	/* These next two are actually common definitions! */
1804 	print_bits(cpuname, "features2",
1805 	    cpu_vendor == CPUVENDOR_INTEL ? CPUID_INTEL_EXT_FLAGS
1806 		: CPUID_EXT_FLAGS, ci->ci_feat_val[2]);
1807 	print_bits(cpuname, "features3",
1808 	    cpu_vendor == CPUVENDOR_INTEL ? CPUID_INTEL_FLAGS4
1809 		: CPUID_AMD_FLAGS4, ci->ci_feat_val[3]);
1810 
1811 	print_bits(cpuname, "padloack features", CPUID_FLAGS_PADLOCK,
1812 	    ci->ci_feat_val[4]);
1813 
1814 	print_bits(cpuname, "xsave features", XCR0_FLAGS1, ci->ci_feat_val[5]);
1815 	print_bits(cpuname, "xsave instructions", CPUID_PES1_FLAGS,
1816 	    ci->ci_feat_val[6]);
1817 
1818 	if (ci->ci_max_xsave != 0) {
1819 		aprint_normal("%s: xsave area size: current %d, maximum %d",
1820 			cpuname, ci->ci_cur_xsave, ci->ci_max_xsave);
1821 		aprint_normal(", xgetbv %sabled\n",
1822 		    ci->ci_feat_val[1] & CPUID2_OSXSAVE ? "en" : "dis");
1823 		if (ci->ci_feat_val[1] & CPUID2_OSXSAVE)
1824 			print_bits(cpuname, "enabled xsave", XCR0_FLAGS1,
1825 			    x86_xgetbv());
1826 	}
1827 
1828 	x86_print_cache_and_tlb_info(ci);
1829 
1830 	if (ci->ci_cpuid_level >= 3 && (ci->ci_feat_val[0] & CPUID_PN)) {
1831 		aprint_verbose("%s: serial number %04X-%04X-%04X-%04X-%04X-%04X\n",
1832 		    cpuname,
1833 		    ci->ci_cpu_serial[0] / 65536, ci->ci_cpu_serial[0] % 65536,
1834 		    ci->ci_cpu_serial[1] / 65536, ci->ci_cpu_serial[1] % 65536,
1835 		    ci->ci_cpu_serial[2] / 65536, ci->ci_cpu_serial[2] % 65536);
1836 	}
1837 
1838 	if (ci->ci_cpu_class == CPUCLASS_386) {
1839 		errx(1, "NetBSD requires an 80486 or later processor");
1840 	}
1841 
1842 	if (ci->ci_cpu_type == CPU_486DLC) {
1843 #ifndef CYRIX_CACHE_WORKS
1844 		aprint_error("WARNING: CYRIX 486DLC CACHE UNCHANGED.\n");
1845 #else
1846 #ifndef CYRIX_CACHE_REALLY_WORKS
1847 		aprint_error("WARNING: CYRIX 486DLC CACHE ENABLED IN HOLD-FLUSH MODE.\n");
1848 #else
1849 		aprint_error("WARNING: CYRIX 486DLC CACHE ENABLED.\n");
1850 #endif
1851 #endif
1852 	}
1853 
1854 	/*
1855 	 * Everything past this point requires a Pentium or later.
1856 	 */
1857 	if (ci->ci_cpuid_level < 0)
1858 		return;
1859 
1860 	identifycpu_cpuids(ci);
1861 
1862 #ifdef INTEL_CORETEMP
1863 	if (cpu_vendor == CPUVENDOR_INTEL && ci->ci_cpuid_level >= 0x06)
1864 		coretemp_register(ci);
1865 #endif
1866 
1867 	if (cpu_vendor == CPUVENDOR_AMD) {
1868 		uint32_t data[4];
1869 
1870 		x86_cpuid(0x80000000, data);
1871 		if (data[0] >= 0x80000007)
1872 			powernow_probe(ci);
1873 
1874 		if ((data[0] >= 0x8000000a)
1875 		   && (ci->ci_feat_val[3] & CPUID_SVM) != 0) {
1876 			x86_cpuid(0x8000000a, data);
1877 			aprint_verbose("%s: SVM Rev. %d\n", cpuname,
1878 			    data[0] & 0xf);
1879 			aprint_verbose("%s: SVM NASID %d\n", cpuname, data[1]);
1880 			print_bits(cpuname, "SVM features", CPUID_AMD_SVM_FLAGS,
1881 				   data[3]);
1882 		}
1883 	} else if (cpu_vendor == CPUVENDOR_INTEL) {
1884 		uint32_t data[4];
1885 		int32_t bi_index;
1886 
1887 		for (bi_index = 1; bi_index <= ci->ci_cpuid_level; bi_index++) {
1888 			x86_cpuid(bi_index, data);
1889 			switch (bi_index) {
1890 			case 6:
1891 				print_bits(cpuname, "DSPM-eax",
1892 				    CPUID_DSPM_FLAGS, data[0]);
1893 				print_bits(cpuname, "DSPM-ecx",
1894 				    CPUID_DSPM_FLAGS1, data[2]);
1895 				break;
1896 			case 7:
1897 				aprint_verbose("%s: SEF highest subleaf %08x\n",
1898 				    cpuname, data[0]);
1899 				print_bits(cpuname, "SEF-main", CPUID_SEF_FLAGS,
1900 				    data[1]);
1901 				break;
1902 #if 0
1903 			default:
1904 				aprint_verbose("%s: basic %08x-eax %08x\n",
1905 				    cpuname, bi_index, data[0]);
1906 				aprint_verbose("%s: basic %08x-ebx %08x\n",
1907 				    cpuname, bi_index, data[1]);
1908 				aprint_verbose("%s: basic %08x-ecx %08x\n",
1909 				    cpuname, bi_index, data[2]);
1910 				aprint_verbose("%s: basic %08x-edx %08x\n",
1911 				    cpuname, bi_index, data[3]);
1912 				break;
1913 #endif
1914 			}
1915 		}
1916 	}
1917 
1918 #ifdef INTEL_ONDEMAND_CLOCKMOD
1919 	clockmod_init();
1920 #endif
1921 
1922 	if (cpu_vendor == CPUVENDOR_AMD)
1923 		ucode.loader_version = CPU_UCODE_LOADER_AMD;
1924 	else if (cpu_vendor == CPUVENDOR_INTEL)
1925 		ucode.loader_version = CPU_UCODE_LOADER_INTEL1;
1926 	else
1927 		return;
1928 
1929 	ucode.data = &ucvers;
1930 	if (ioctl(fd, IOC_CPU_UCODE_GET_VERSION, &ucode) < 0) {
1931 #ifdef __i386__
1932 		struct cpu_ucode_version_64 ucode_64;
1933 		if (errno != ENOTTY)
1934 			return;
1935 		/* Try the 64 bit ioctl */
1936 		memset(&ucode_64, 0, sizeof ucode_64);
1937 		ucode_64.data = &ucvers;
1938 		ucode_64.loader_version = ucode.loader_version;
1939 		if (ioctl(fd, IOC_CPU_UCODE_GET_VERSION_64, &ucode_64) < 0)
1940 			return;
1941 #endif
1942 	}
1943 
1944 	if (cpu_vendor == CPUVENDOR_AMD)
1945 		printf("%s: UCode version: 0x%"PRIx64"\n", cpuname, ucvers.amd.version);
1946 	else if (cpu_vendor == CPUVENDOR_INTEL)
1947 		printf("%s: microcode version 0x%x, platform ID %d\n", cpuname,
1948 		       ucvers.intel1.ucodeversion, ucvers.intel1.platformid);
1949 }
1950 
1951 static const struct x86_cache_info *
1952 cache_info_lookup(const struct x86_cache_info *cai, uint8_t desc)
1953 {
1954 	int i;
1955 
1956 	for (i = 0; cai[i].cai_desc != 0; i++) {
1957 		if (cai[i].cai_desc == desc)
1958 			return (&cai[i]);
1959 	}
1960 
1961 	return (NULL);
1962 }
1963 
1964 static const char *
1965 print_cache_config(struct cpu_info *ci, int cache_tag, const char *name,
1966     const char *sep)
1967 {
1968 	struct x86_cache_info *cai = &ci->ci_cinfo[cache_tag];
1969 	char human_num[HUMAN_BUFSIZE];
1970 
1971 	if (cai->cai_totalsize == 0)
1972 		return sep;
1973 
1974 	if (sep == NULL)
1975 		aprint_verbose_dev(ci->ci_dev, "");
1976 	else
1977 		aprint_verbose("%s", sep);
1978 	if (name != NULL)
1979 		aprint_verbose("%s ", name);
1980 
1981 	if (cai->cai_string != NULL) {
1982 		aprint_verbose("%s ", cai->cai_string);
1983 	} else {
1984 		(void)humanize_number(human_num, sizeof(human_num),
1985 			cai->cai_totalsize, "B", HN_AUTOSCALE, HN_NOSPACE);
1986 		aprint_verbose("%s %dB/line ", human_num, cai->cai_linesize);
1987 	}
1988 	switch (cai->cai_associativity) {
1989 	case    0:
1990 		aprint_verbose("disabled");
1991 		break;
1992 	case    1:
1993 		aprint_verbose("direct-mapped");
1994 		break;
1995 	case 0xff:
1996 		aprint_verbose("fully associative");
1997 		break;
1998 	default:
1999 		aprint_verbose("%d-way", cai->cai_associativity);
2000 		break;
2001 	}
2002 	return ", ";
2003 }
2004 
2005 static const char *
2006 print_tlb_config(struct cpu_info *ci, int cache_tag, const char *name,
2007     const char *sep)
2008 {
2009 	struct x86_cache_info *cai = &ci->ci_cinfo[cache_tag];
2010 	char human_num[HUMAN_BUFSIZE];
2011 
2012 	if (cai->cai_totalsize == 0)
2013 		return sep;
2014 
2015 	if (sep == NULL)
2016 		aprint_verbose_dev(ci->ci_dev, "");
2017 	else
2018 		aprint_verbose("%s", sep);
2019 	if (name != NULL)
2020 		aprint_verbose("%s ", name);
2021 
2022 	if (cai->cai_string != NULL) {
2023 		aprint_verbose("%s", cai->cai_string);
2024 	} else {
2025 		(void)humanize_number(human_num, sizeof(human_num),
2026 			cai->cai_linesize, "B", HN_AUTOSCALE, HN_NOSPACE);
2027 		aprint_verbose("%d %s entries ", cai->cai_totalsize,
2028 		    human_num);
2029 		switch (cai->cai_associativity) {
2030 		case 0:
2031 			aprint_verbose("disabled");
2032 			break;
2033 		case 1:
2034 			aprint_verbose("direct-mapped");
2035 			break;
2036 		case 0xff:
2037 			aprint_verbose("fully associative");
2038 			break;
2039 		default:
2040 			aprint_verbose("%d-way", cai->cai_associativity);
2041 			break;
2042 		}
2043 	}
2044 	return ", ";
2045 }
2046 
2047 static void
2048 x86_print_cache_and_tlb_info(struct cpu_info *ci)
2049 {
2050 	const char *sep = NULL;
2051 
2052 	if (ci->ci_cinfo[CAI_ICACHE].cai_totalsize != 0 ||
2053 	    ci->ci_cinfo[CAI_DCACHE].cai_totalsize != 0) {
2054 		sep = print_cache_config(ci, CAI_ICACHE, "I-cache", NULL);
2055 		sep = print_cache_config(ci, CAI_DCACHE, "D-cache", sep);
2056 		if (sep != NULL)
2057 			aprint_verbose("\n");
2058 	}
2059 	if (ci->ci_cinfo[CAI_L2CACHE].cai_totalsize != 0) {
2060 		sep = print_cache_config(ci, CAI_L2CACHE, "L2 cache", NULL);
2061 		if (sep != NULL)
2062 			aprint_verbose("\n");
2063 	}
2064 	if (ci->ci_cinfo[CAI_L3CACHE].cai_totalsize != 0) {
2065 		sep = print_cache_config(ci, CAI_L3CACHE, "L3 cache", NULL);
2066 		if (sep != NULL)
2067 			aprint_verbose("\n");
2068 	}
2069 	if (ci->ci_cinfo[CAI_PREFETCH].cai_linesize != 0) {
2070 		aprint_verbose_dev(ci->ci_dev, "%dB prefetching",
2071 			ci->ci_cinfo[CAI_PREFETCH].cai_linesize);
2072 		if (sep != NULL)
2073 			aprint_verbose("\n");
2074 	}
2075 	if (ci->ci_cinfo[CAI_ITLB].cai_totalsize != 0) {
2076 		sep = print_tlb_config(ci, CAI_ITLB, "ITLB", NULL);
2077 		sep = print_tlb_config(ci, CAI_ITLB2, NULL, sep);
2078 		if (sep != NULL)
2079 			aprint_verbose("\n");
2080 	}
2081 	if (ci->ci_cinfo[CAI_DTLB].cai_totalsize != 0) {
2082 		sep = print_tlb_config(ci, CAI_DTLB, "DTLB", NULL);
2083 		sep = print_tlb_config(ci, CAI_DTLB2, NULL, sep);
2084 		if (sep != NULL)
2085 			aprint_verbose("\n");
2086 	}
2087 	if (ci->ci_cinfo[CAI_L2_ITLB].cai_totalsize != 0) {
2088 		sep = print_tlb_config(ci, CAI_L2_ITLB, "L2 ITLB", NULL);
2089 		sep = print_tlb_config(ci, CAI_L2_ITLB2, NULL, sep);
2090 		if (sep != NULL)
2091 			aprint_verbose("\n");
2092 	}
2093 	if (ci->ci_cinfo[CAI_L2_DTLB].cai_totalsize != 0) {
2094 		sep = print_tlb_config(ci, CAI_L2_DTLB, "L2 DTLB", NULL);
2095 		sep = print_tlb_config(ci, CAI_L2_DTLB2, NULL, sep);
2096 		if (sep != NULL)
2097 			aprint_verbose("\n");
2098 	}
2099 	if (ci->ci_cinfo[CAI_L2_STLB].cai_totalsize != 0) {
2100 		sep = print_tlb_config(ci, CAI_L2_STLB, "L2 STLB", NULL);
2101 		sep = print_tlb_config(ci, CAI_L2_STLB2, NULL, sep);
2102 		if (sep != NULL)
2103 			aprint_verbose("\n");
2104 	}
2105 	if (ci->ci_cinfo[CAI_L1_1GBITLB].cai_totalsize != 0) {
2106 		sep = print_tlb_config(ci, CAI_L1_1GBITLB, "L1 1GB page ITLB",
2107 		    NULL);
2108 		if (sep != NULL)
2109 			aprint_verbose("\n");
2110 	}
2111 	if (ci->ci_cinfo[CAI_L1_1GBDTLB].cai_totalsize != 0) {
2112 		sep = print_tlb_config(ci, CAI_L1_1GBDTLB, "L1 1GB page DTLB",
2113 		    NULL);
2114 		if (sep != NULL)
2115 			aprint_verbose("\n");
2116 	}
2117 	if (ci->ci_cinfo[CAI_L2_1GBITLB].cai_totalsize != 0) {
2118 		sep = print_tlb_config(ci, CAI_L2_1GBITLB, "L2 1GB page ITLB",
2119 		    NULL);
2120 		if (sep != NULL)
2121 			aprint_verbose("\n");
2122 	}
2123 	if (ci->ci_cinfo[CAI_L2_1GBDTLB].cai_totalsize != 0) {
2124 		sep = print_tlb_config(ci, CAI_L2_1GBDTLB, "L2 1GB page DTLB",
2125 		    NULL);
2126 		if (sep != NULL)
2127 			aprint_verbose("\n");
2128 	}
2129 }
2130 
2131 static void
2132 powernow_probe(struct cpu_info *ci)
2133 {
2134 	uint32_t regs[4];
2135 	char buf[256];
2136 
2137 	x86_cpuid(0x80000007, regs);
2138 
2139 	snprintb(buf, sizeof(buf), CPUID_APM_FLAGS, regs[3]);
2140 	aprint_normal_dev(ci->ci_dev, "AMD Power Management features: %s\n",
2141 	    buf);
2142 }
2143 
2144 int
2145 ucodeupdate_check(int fd, struct cpu_ucode *uc)
2146 {
2147 	struct cpu_info ci;
2148 	int loader_version, res;
2149 	struct cpu_ucode_version versreq;
2150 
2151 	cpu_probe_base_features(&ci, "unknown");
2152 
2153 	if (!strcmp((char *)ci.ci_vendor, "AuthenticAMD"))
2154 		loader_version = CPU_UCODE_LOADER_AMD;
2155 	else if (!strcmp((char *)ci.ci_vendor, "GenuineIntel"))
2156 		loader_version = CPU_UCODE_LOADER_INTEL1;
2157 	else
2158 		return -1;
2159 
2160 	/* check whether the kernel understands this loader version */
2161 	versreq.loader_version = loader_version;
2162 	versreq.data = 0;
2163 	res = ioctl(fd, IOC_CPU_UCODE_GET_VERSION, &versreq);
2164 	if (res)
2165 		return -1;
2166 
2167 	switch (loader_version) {
2168 	case CPU_UCODE_LOADER_AMD:
2169 		if (uc->cpu_nr != -1) {
2170 			/* printf? */
2171 			return -1;
2172 		}
2173 		uc->cpu_nr = CPU_UCODE_ALL_CPUS;
2174 		break;
2175 	case CPU_UCODE_LOADER_INTEL1:
2176 		if (uc->cpu_nr == -1)
2177 			uc->cpu_nr = CPU_UCODE_ALL_CPUS; /* for Xen */
2178 		else
2179 			uc->cpu_nr = CPU_UCODE_CURRENT_CPU;
2180 		break;
2181 	default: /* can't happen */
2182 		return -1;
2183 	}
2184 	uc->loader_version = loader_version;
2185 	return 0;
2186 }
2187