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