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