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