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