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