xref: /onnv-gate/usr/src/uts/i86pc/os/cpuid.c (revision 5284:2f7098179999)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51582Skchow  * Common Development and Distribution License (the "License").
61582Skchow  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
223434Sesaxe  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * Various routines to handle identification
300Sstevel@tonic-gate  * and classification of x86 processors.
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <sys/archsystm.h>
350Sstevel@tonic-gate #include <sys/x86_archext.h>
360Sstevel@tonic-gate #include <sys/kmem.h>
370Sstevel@tonic-gate #include <sys/systm.h>
380Sstevel@tonic-gate #include <sys/cmn_err.h>
390Sstevel@tonic-gate #include <sys/sunddi.h>
400Sstevel@tonic-gate #include <sys/sunndi.h>
410Sstevel@tonic-gate #include <sys/cpuvar.h>
420Sstevel@tonic-gate #include <sys/processor.h>
435045Sbholler #include <sys/sysmacros.h>
443434Sesaxe #include <sys/pg.h>
450Sstevel@tonic-gate #include <sys/fp.h>
460Sstevel@tonic-gate #include <sys/controlregs.h>
470Sstevel@tonic-gate #include <sys/auxv_386.h>
480Sstevel@tonic-gate #include <sys/bitmap.h>
490Sstevel@tonic-gate #include <sys/memnode.h>
500Sstevel@tonic-gate 
510Sstevel@tonic-gate /*
520Sstevel@tonic-gate  * Pass 0 of cpuid feature analysis happens in locore. It contains special code
530Sstevel@tonic-gate  * to recognize Cyrix processors that are not cpuid-compliant, and to deal with
540Sstevel@tonic-gate  * them accordingly. For most modern processors, feature detection occurs here
550Sstevel@tonic-gate  * in pass 1.
560Sstevel@tonic-gate  *
570Sstevel@tonic-gate  * Pass 1 of cpuid feature analysis happens just at the beginning of mlsetup()
580Sstevel@tonic-gate  * for the boot CPU and does the basic analysis that the early kernel needs.
590Sstevel@tonic-gate  * x86_feature is set based on the return value of cpuid_pass1() of the boot
600Sstevel@tonic-gate  * CPU.
610Sstevel@tonic-gate  *
620Sstevel@tonic-gate  * Pass 1 includes:
630Sstevel@tonic-gate  *
640Sstevel@tonic-gate  *	o Determining vendor/model/family/stepping and setting x86_type and
650Sstevel@tonic-gate  *	  x86_vendor accordingly.
660Sstevel@tonic-gate  *	o Processing the feature flags returned by the cpuid instruction while
670Sstevel@tonic-gate  *	  applying any workarounds or tricks for the specific processor.
680Sstevel@tonic-gate  *	o Mapping the feature flags into Solaris feature bits (X86_*).
690Sstevel@tonic-gate  *	o Processing extended feature flags if supported by the processor,
700Sstevel@tonic-gate  *	  again while applying specific processor knowledge.
710Sstevel@tonic-gate  *	o Determining the CMT characteristics of the system.
720Sstevel@tonic-gate  *
730Sstevel@tonic-gate  * Pass 1 is done on non-boot CPUs during their initialization and the results
740Sstevel@tonic-gate  * are used only as a meager attempt at ensuring that all processors within the
750Sstevel@tonic-gate  * system support the same features.
760Sstevel@tonic-gate  *
770Sstevel@tonic-gate  * Pass 2 of cpuid feature analysis happens just at the beginning
780Sstevel@tonic-gate  * of startup().  It just copies in and corrects the remainder
790Sstevel@tonic-gate  * of the cpuid data we depend on: standard cpuid functions that we didn't
800Sstevel@tonic-gate  * need for pass1 feature analysis, and extended cpuid functions beyond the
810Sstevel@tonic-gate  * simple feature processing done in pass1.
820Sstevel@tonic-gate  *
830Sstevel@tonic-gate  * Pass 3 of cpuid analysis is invoked after basic kernel services; in
840Sstevel@tonic-gate  * particular kernel memory allocation has been made available. It creates a
850Sstevel@tonic-gate  * readable brand string based on the data collected in the first two passes.
860Sstevel@tonic-gate  *
870Sstevel@tonic-gate  * Pass 4 of cpuid analysis is invoked after post_startup() when all
880Sstevel@tonic-gate  * the support infrastructure for various hardware features has been
890Sstevel@tonic-gate  * initialized. It determines which processor features will be reported
900Sstevel@tonic-gate  * to userland via the aux vector.
910Sstevel@tonic-gate  *
920Sstevel@tonic-gate  * All passes are executed on all CPUs, but only the boot CPU determines what
930Sstevel@tonic-gate  * features the kernel will use.
940Sstevel@tonic-gate  *
950Sstevel@tonic-gate  * Much of the worst junk in this file is for the support of processors
960Sstevel@tonic-gate  * that didn't really implement the cpuid instruction properly.
970Sstevel@tonic-gate  *
980Sstevel@tonic-gate  * NOTE: The accessor functions (cpuid_get*) are aware of, and ASSERT upon,
990Sstevel@tonic-gate  * the pass numbers.  Accordingly, changes to the pass code may require changes
1000Sstevel@tonic-gate  * to the accessor code.
1010Sstevel@tonic-gate  */
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate uint_t x86_feature = 0;
1040Sstevel@tonic-gate uint_t x86_vendor = X86_VENDOR_IntelClone;
1050Sstevel@tonic-gate uint_t x86_type = X86_TYPE_OTHER;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate uint_t pentiumpro_bug4046376;
1080Sstevel@tonic-gate uint_t pentiumpro_bug4064495;
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate uint_t enable486;
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate /*
1130Sstevel@tonic-gate  * This set of strings are for processors rumored to support the cpuid
1140Sstevel@tonic-gate  * instruction, and is used by locore.s to figure out how to set x86_vendor
1150Sstevel@tonic-gate  */
1160Sstevel@tonic-gate const char CyrixInstead[] = "CyrixInstead";
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate /*
1194481Sbholler  * monitor/mwait info.
1205045Sbholler  *
1215045Sbholler  * size_actual and buf_actual are the real address and size allocated to get
1225045Sbholler  * proper mwait_buf alignement.  buf_actual and size_actual should be passed
1235045Sbholler  * to kmem_free().  Currently kmem_alloc() and mwait happen to both use
1245045Sbholler  * processor cache-line alignment, but this is not guarantied in the furture.
1254481Sbholler  */
1264481Sbholler struct mwait_info {
1274481Sbholler 	size_t		mon_min;	/* min size to avoid missed wakeups */
1284481Sbholler 	size_t		mon_max;	/* size to avoid false wakeups */
1295045Sbholler 	size_t		size_actual;	/* size actually allocated */
1305045Sbholler 	void		*buf_actual;	/* memory actually allocated */
1314481Sbholler 	uint32_t	support;	/* processor support of monitor/mwait */
1324481Sbholler };
1334481Sbholler 
1344481Sbholler /*
1350Sstevel@tonic-gate  * These constants determine how many of the elements of the
1360Sstevel@tonic-gate  * cpuid we cache in the cpuid_info data structure; the
1370Sstevel@tonic-gate  * remaining elements are accessible via the cpuid instruction.
1380Sstevel@tonic-gate  */
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate #define	NMAX_CPI_STD	6		/* eax = 0 .. 5 */
1410Sstevel@tonic-gate #define	NMAX_CPI_EXTD	9		/* eax = 0x80000000 .. 0x80000008 */
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate struct cpuid_info {
1440Sstevel@tonic-gate 	uint_t cpi_pass;		/* last pass completed */
1450Sstevel@tonic-gate 	/*
1460Sstevel@tonic-gate 	 * standard function information
1470Sstevel@tonic-gate 	 */
1480Sstevel@tonic-gate 	uint_t cpi_maxeax;		/* fn 0: %eax */
1490Sstevel@tonic-gate 	char cpi_vendorstr[13];		/* fn 0: %ebx:%ecx:%edx */
1500Sstevel@tonic-gate 	uint_t cpi_vendor;		/* enum of cpi_vendorstr */
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	uint_t cpi_family;		/* fn 1: extended family */
1530Sstevel@tonic-gate 	uint_t cpi_model;		/* fn 1: extended model */
1540Sstevel@tonic-gate 	uint_t cpi_step;		/* fn 1: stepping */
1550Sstevel@tonic-gate 	chipid_t cpi_chipid;		/* fn 1: %ebx: chip # on ht cpus */
1560Sstevel@tonic-gate 	uint_t cpi_brandid;		/* fn 1: %ebx: brand ID */
1570Sstevel@tonic-gate 	int cpi_clogid;			/* fn 1: %ebx: thread # */
1581228Sandrei 	uint_t cpi_ncpu_per_chip;	/* fn 1: %ebx: logical cpu count */
1590Sstevel@tonic-gate 	uint8_t cpi_cacheinfo[16];	/* fn 2: intel-style cache desc */
1600Sstevel@tonic-gate 	uint_t cpi_ncache;		/* fn 2: number of elements */
1614606Sesaxe 	uint_t cpi_ncpu_shr_last_cache;	/* fn 4: %eax: ncpus sharing cache */
1624606Sesaxe 	id_t cpi_last_lvl_cacheid;	/* fn 4: %eax: derived cache id */
1634606Sesaxe 	uint_t cpi_std_4_size;		/* fn 4: number of fn 4 elements */
1644606Sesaxe 	struct cpuid_regs **cpi_std_4;	/* fn 4: %ecx == 0 .. fn4_size */
1651228Sandrei 	struct cpuid_regs cpi_std[NMAX_CPI_STD];	/* 0 .. 5 */
1660Sstevel@tonic-gate 	/*
1670Sstevel@tonic-gate 	 * extended function information
1680Sstevel@tonic-gate 	 */
1690Sstevel@tonic-gate 	uint_t cpi_xmaxeax;		/* fn 0x80000000: %eax */
1700Sstevel@tonic-gate 	char cpi_brandstr[49];		/* fn 0x8000000[234] */
1710Sstevel@tonic-gate 	uint8_t cpi_pabits;		/* fn 0x80000006: %eax */
1720Sstevel@tonic-gate 	uint8_t cpi_vabits;		/* fn 0x80000006: %eax */
1731228Sandrei 	struct cpuid_regs cpi_extd[NMAX_CPI_EXTD]; /* 0x8000000[0-8] */
1741228Sandrei 	id_t cpi_coreid;
1751228Sandrei 	uint_t cpi_ncore_per_chip;	/* AMD: fn 0x80000008: %ecx[7-0] */
1761228Sandrei 					/* Intel: fn 4: %eax[31-26] */
1770Sstevel@tonic-gate 	/*
1780Sstevel@tonic-gate 	 * supported feature information
1790Sstevel@tonic-gate 	 */
1803446Smrj 	uint32_t cpi_support[5];
1810Sstevel@tonic-gate #define	STD_EDX_FEATURES	0
1820Sstevel@tonic-gate #define	AMD_EDX_FEATURES	1
1830Sstevel@tonic-gate #define	TM_EDX_FEATURES		2
1840Sstevel@tonic-gate #define	STD_ECX_FEATURES	3
1853446Smrj #define	AMD_ECX_FEATURES	4
1862869Sgavinm 	/*
1872869Sgavinm 	 * Synthesized information, where known.
1882869Sgavinm 	 */
1892869Sgavinm 	uint32_t cpi_chiprev;		/* See X86_CHIPREV_* in x86_archext.h */
1902869Sgavinm 	const char *cpi_chiprevstr;	/* May be NULL if chiprev unknown */
1912869Sgavinm 	uint32_t cpi_socket;		/* Chip package/socket type */
1924481Sbholler 
1934481Sbholler 	struct mwait_info cpi_mwait;	/* fn 5: monitor/mwait info */
1940Sstevel@tonic-gate };
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate static struct cpuid_info cpuid_info0;
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate /*
2000Sstevel@tonic-gate  * These bit fields are defined by the Intel Application Note AP-485
2010Sstevel@tonic-gate  * "Intel Processor Identification and the CPUID Instruction"
2020Sstevel@tonic-gate  */
2030Sstevel@tonic-gate #define	CPI_FAMILY_XTD(cpi)	BITX((cpi)->cpi_std[1].cp_eax, 27, 20)
2040Sstevel@tonic-gate #define	CPI_MODEL_XTD(cpi)	BITX((cpi)->cpi_std[1].cp_eax, 19, 16)
2050Sstevel@tonic-gate #define	CPI_TYPE(cpi)		BITX((cpi)->cpi_std[1].cp_eax, 13, 12)
2060Sstevel@tonic-gate #define	CPI_FAMILY(cpi)		BITX((cpi)->cpi_std[1].cp_eax, 11, 8)
2070Sstevel@tonic-gate #define	CPI_STEP(cpi)		BITX((cpi)->cpi_std[1].cp_eax, 3, 0)
2080Sstevel@tonic-gate #define	CPI_MODEL(cpi)		BITX((cpi)->cpi_std[1].cp_eax, 7, 4)
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate #define	CPI_FEATURES_EDX(cpi)		((cpi)->cpi_std[1].cp_edx)
2110Sstevel@tonic-gate #define	CPI_FEATURES_ECX(cpi)		((cpi)->cpi_std[1].cp_ecx)
2120Sstevel@tonic-gate #define	CPI_FEATURES_XTD_EDX(cpi)	((cpi)->cpi_extd[1].cp_edx)
2130Sstevel@tonic-gate #define	CPI_FEATURES_XTD_ECX(cpi)	((cpi)->cpi_extd[1].cp_ecx)
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate #define	CPI_BRANDID(cpi)	BITX((cpi)->cpi_std[1].cp_ebx, 7, 0)
2160Sstevel@tonic-gate #define	CPI_CHUNKS(cpi)		BITX((cpi)->cpi_std[1].cp_ebx, 15, 7)
2170Sstevel@tonic-gate #define	CPI_CPU_COUNT(cpi)	BITX((cpi)->cpi_std[1].cp_ebx, 23, 16)
2180Sstevel@tonic-gate #define	CPI_APIC_ID(cpi)	BITX((cpi)->cpi_std[1].cp_ebx, 31, 24)
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate #define	CPI_MAXEAX_MAX		0x100		/* sanity control */
2210Sstevel@tonic-gate #define	CPI_XMAXEAX_MAX		0x80000100
2224606Sesaxe #define	CPI_FN4_ECX_MAX		0x20		/* sanity: max fn 4 levels */
2234606Sesaxe 
2244606Sesaxe /*
2254606Sesaxe  * Function 4 (Deterministic Cache Parameters) macros
2264606Sesaxe  * Defined by Intel Application Note AP-485
2274606Sesaxe  */
2284606Sesaxe #define	CPI_NUM_CORES(regs)		BITX((regs)->cp_eax, 31, 26)
2294606Sesaxe #define	CPI_NTHR_SHR_CACHE(regs)	BITX((regs)->cp_eax, 25, 14)
2304606Sesaxe #define	CPI_FULL_ASSOC_CACHE(regs)	BITX((regs)->cp_eax, 9, 9)
2314606Sesaxe #define	CPI_SELF_INIT_CACHE(regs)	BITX((regs)->cp_eax, 8, 8)
2324606Sesaxe #define	CPI_CACHE_LVL(regs)		BITX((regs)->cp_eax, 7, 5)
2334606Sesaxe #define	CPI_CACHE_TYPE(regs)		BITX((regs)->cp_eax, 4, 0)
2344606Sesaxe 
2354606Sesaxe #define	CPI_CACHE_WAYS(regs)		BITX((regs)->cp_ebx, 31, 22)
2364606Sesaxe #define	CPI_CACHE_PARTS(regs)		BITX((regs)->cp_ebx, 21, 12)
2374606Sesaxe #define	CPI_CACHE_COH_LN_SZ(regs)	BITX((regs)->cp_ebx, 11, 0)
2384606Sesaxe 
2394606Sesaxe #define	CPI_CACHE_SETS(regs)		BITX((regs)->cp_ecx, 31, 0)
2404606Sesaxe 
2414606Sesaxe #define	CPI_PREFCH_STRIDE(regs)		BITX((regs)->cp_edx, 9, 0)
2424606Sesaxe 
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate /*
2451975Sdmick  * A couple of shorthand macros to identify "later" P6-family chips
2461975Sdmick  * like the Pentium M and Core.  First, the "older" P6-based stuff
2471975Sdmick  * (loosely defined as "pre-Pentium-4"):
2481975Sdmick  * P6, PII, Mobile PII, PII Xeon, PIII, Mobile PIII, PIII Xeon
2491975Sdmick  */
2501975Sdmick 
2511975Sdmick #define	IS_LEGACY_P6(cpi) (			\
2521975Sdmick 	cpi->cpi_family == 6 && 		\
2531975Sdmick 		(cpi->cpi_model == 1 ||		\
2541975Sdmick 		cpi->cpi_model == 3 ||		\
2551975Sdmick 		cpi->cpi_model == 5 ||		\
2561975Sdmick 		cpi->cpi_model == 6 ||		\
2571975Sdmick 		cpi->cpi_model == 7 ||		\
2581975Sdmick 		cpi->cpi_model == 8 ||		\
2591975Sdmick 		cpi->cpi_model == 0xA ||	\
2601975Sdmick 		cpi->cpi_model == 0xB)		\
2611975Sdmick )
2621975Sdmick 
2631975Sdmick /* A "new F6" is everything with family 6 that's not the above */
2641975Sdmick #define	IS_NEW_F6(cpi) ((cpi->cpi_family == 6) && !IS_LEGACY_P6(cpi))
2651975Sdmick 
2664855Sksadhukh /* Extended family/model support */
2674855Sksadhukh #define	IS_EXTENDED_MODEL_INTEL(cpi) (cpi->cpi_family == 0x6 || \
2684855Sksadhukh 	cpi->cpi_family >= 0xf)
2694855Sksadhukh 
2701975Sdmick /*
2715248Sksadhukh  * AMD family 0xf and family 0x10 socket types.
2725248Sksadhukh  * First index :
2735248Sksadhukh  *		0 for family 0xf, revs B thru E
2745248Sksadhukh  *		1 for family 0xf, revs F and G
2755248Sksadhukh  *		2 for family 0x10, rev B
2762869Sgavinm  * Second index by (model & 0x3)
2772869Sgavinm  */
2785248Sksadhukh static uint32_t amd_skts[3][4] = {
2795254Sgavinm 	/*
2805254Sgavinm 	 * Family 0xf revisions B through E
2815254Sgavinm 	 */
2825254Sgavinm #define	A_SKTS_0			0
2832869Sgavinm 	{
2842869Sgavinm 		X86_SOCKET_754,		/* 0b00 */
2852869Sgavinm 		X86_SOCKET_940,		/* 0b01 */
2862869Sgavinm 		X86_SOCKET_754,		/* 0b10 */
2872869Sgavinm 		X86_SOCKET_939		/* 0b11 */
2882869Sgavinm 	},
2895254Sgavinm 	/*
2905254Sgavinm 	 * Family 0xf revisions F and G
2915254Sgavinm 	 */
2925254Sgavinm #define	A_SKTS_1			1
2932869Sgavinm 	{
2942869Sgavinm 		X86_SOCKET_S1g1,	/* 0b00 */
2952869Sgavinm 		X86_SOCKET_F1207,	/* 0b01 */
2962869Sgavinm 		X86_SOCKET_UNKNOWN,	/* 0b10 */
2972869Sgavinm 		X86_SOCKET_AM2		/* 0b11 */
2985248Sksadhukh 	},
2995254Sgavinm 	/*
3005254Sgavinm 	 * Family 0x10 revisions A and B
3015254Sgavinm 	 * It is not clear whether, as new sockets release, that
3025254Sgavinm 	 * model & 0x3 will id socket for this family
3035254Sgavinm 	 */
3045254Sgavinm #define	A_SKTS_2			2
3055248Sksadhukh 	{
3065248Sksadhukh 		X86_SOCKET_F1207,	/* 0b00 */
3075248Sksadhukh 		X86_SOCKET_F1207,	/* 0b01 */
3085248Sksadhukh 		X86_SOCKET_F1207,	/* 0b10 */
3095254Sgavinm 		X86_SOCKET_F1207,	/* 0b11 */
3102869Sgavinm 	}
3112869Sgavinm };
3122869Sgavinm 
3132869Sgavinm /*
3145248Sksadhukh  * Table for mapping AMD Family 0xf and AMD Family 0x10 model/stepping
3155248Sksadhukh  * combination to chip "revision" and socket type.
3162869Sgavinm  *
3172869Sgavinm  * The first member of this array that matches a given family, extended model
3182869Sgavinm  * plus model range, and stepping range will be considered a match.
3192869Sgavinm  */
3202869Sgavinm static const struct amd_rev_mapent {
3212869Sgavinm 	uint_t rm_family;
3222869Sgavinm 	uint_t rm_modello;
3232869Sgavinm 	uint_t rm_modelhi;
3242869Sgavinm 	uint_t rm_steplo;
3252869Sgavinm 	uint_t rm_stephi;
3262869Sgavinm 	uint32_t rm_chiprev;
3272869Sgavinm 	const char *rm_chiprevstr;
3282869Sgavinm 	int rm_sktidx;
3292869Sgavinm } amd_revmap[] = {
3302869Sgavinm 	/*
3315254Sgavinm 	 * =============== AuthenticAMD Family 0xf ===============
3325254Sgavinm 	 */
3335254Sgavinm 
3345254Sgavinm 	/*
3352869Sgavinm 	 * Rev B includes model 0x4 stepping 0 and model 0x5 stepping 0 and 1.
3362869Sgavinm 	 */
3375254Sgavinm 	{ 0xf, 0x04, 0x04, 0x0, 0x0, X86_CHIPREV_AMD_F_REV_B, "B", A_SKTS_0 },
3385254Sgavinm 	{ 0xf, 0x05, 0x05, 0x0, 0x1, X86_CHIPREV_AMD_F_REV_B, "B", A_SKTS_0 },
3392869Sgavinm 	/*
3402869Sgavinm 	 * Rev C0 includes model 0x4 stepping 8 and model 0x5 stepping 8
3412869Sgavinm 	 */
3425254Sgavinm 	{ 0xf, 0x04, 0x05, 0x8, 0x8, X86_CHIPREV_AMD_F_REV_C0, "C0", A_SKTS_0 },
3432869Sgavinm 	/*
3442869Sgavinm 	 * Rev CG is the rest of extended model 0x0 - i.e., everything
3452869Sgavinm 	 * but the rev B and C0 combinations covered above.
3462869Sgavinm 	 */
3475254Sgavinm 	{ 0xf, 0x00, 0x0f, 0x0, 0xf, X86_CHIPREV_AMD_F_REV_CG, "CG", A_SKTS_0 },
3482869Sgavinm 	/*
3492869Sgavinm 	 * Rev D has extended model 0x1.
3502869Sgavinm 	 */
3515254Sgavinm 	{ 0xf, 0x10, 0x1f, 0x0, 0xf, X86_CHIPREV_AMD_F_REV_D, "D", A_SKTS_0 },
3522869Sgavinm 	/*
3532869Sgavinm 	 * Rev E has extended model 0x2.
3542869Sgavinm 	 * Extended model 0x3 is unused but available to grow into.
3552869Sgavinm 	 */
3565254Sgavinm 	{ 0xf, 0x20, 0x3f, 0x0, 0xf, X86_CHIPREV_AMD_F_REV_E, "E", A_SKTS_0 },
3572869Sgavinm 	/*
3582869Sgavinm 	 * Rev F has extended models 0x4 and 0x5.
3592869Sgavinm 	 */
3605254Sgavinm 	{ 0xf, 0x40, 0x5f, 0x0, 0xf, X86_CHIPREV_AMD_F_REV_F, "F", A_SKTS_1 },
3612869Sgavinm 	/*
3622869Sgavinm 	 * Rev G has extended model 0x6.
3632869Sgavinm 	 */
3645254Sgavinm 	{ 0xf, 0x60, 0x6f, 0x0, 0xf, X86_CHIPREV_AMD_F_REV_G, "G", A_SKTS_1 },
3655254Sgavinm 
3665254Sgavinm 	/*
3675254Sgavinm 	 * =============== AuthenticAMD Family 0x10 ===============
3685254Sgavinm 	 */
3695254Sgavinm 
3705248Sksadhukh 	/*
3715254Sgavinm 	 * Rev A has model 0 and stepping 0/1/2 for DR-{A0,A1,A2}.
3725254Sgavinm 	 * Give all of model 0 stepping range to rev A.
3735248Sksadhukh 	 */
3745254Sgavinm 	{ 0x10, 0x00, 0x00, 0x0, 0x2, X86_CHIPREV_AMD_10_REV_A, "A", A_SKTS_2 },
3755254Sgavinm 
3765254Sgavinm 	/*
3775254Sgavinm 	 * Rev B has model 2 and steppings 0/1/0xa/2 for DR-{B0,B1,BA,B2}.
3785254Sgavinm 	 * Give all of model 2 stepping range to rev B.
3795254Sgavinm 	 */
3805254Sgavinm 	{ 0x10, 0x02, 0x02, 0x0, 0xf, X86_CHIPREV_AMD_10_REV_B, "B", A_SKTS_2 },
3812869Sgavinm };
3822869Sgavinm 
3834481Sbholler /*
3844481Sbholler  * Info for monitor/mwait idle loop.
3854481Sbholler  *
3864481Sbholler  * See cpuid section of "Intel 64 and IA-32 Architectures Software Developer's
3874481Sbholler  * Manual Volume 2A: Instruction Set Reference, A-M" #25366-022US, November
3884481Sbholler  * 2006.
3894481Sbholler  * See MONITOR/MWAIT section of "AMD64 Architecture Programmer's Manual
3904481Sbholler  * Documentation Updates" #33633, Rev 2.05, December 2006.
3914481Sbholler  */
3924481Sbholler #define	MWAIT_SUPPORT		(0x00000001)	/* mwait supported */
3934481Sbholler #define	MWAIT_EXTENSIONS	(0x00000002)	/* extenstion supported */
3944481Sbholler #define	MWAIT_ECX_INT_ENABLE	(0x00000004)	/* ecx 1 extension supported */
3954481Sbholler #define	MWAIT_SUPPORTED(cpi)	((cpi)->cpi_std[1].cp_ecx & CPUID_INTC_ECX_MON)
3964481Sbholler #define	MWAIT_INT_ENABLE(cpi)	((cpi)->cpi_std[5].cp_ecx & 0x2)
3974481Sbholler #define	MWAIT_EXTENSION(cpi)	((cpi)->cpi_std[5].cp_ecx & 0x1)
3984481Sbholler #define	MWAIT_SIZE_MIN(cpi)	BITX((cpi)->cpi_std[5].cp_eax, 15, 0)
3994481Sbholler #define	MWAIT_SIZE_MAX(cpi)	BITX((cpi)->cpi_std[5].cp_ebx, 15, 0)
4004481Sbholler /*
4014481Sbholler  * Number of sub-cstates for a given c-state.
4024481Sbholler  */
4034481Sbholler #define	MWAIT_NUM_SUBC_STATES(cpi, c_state)			\
4044481Sbholler 	BITX((cpi)->cpi_std[5].cp_edx, c_state + 3, c_state)
4054481Sbholler 
4064797Sksadhukh static void intel_cpuid_4_cache_info(void *, struct cpuid_info *);
4074797Sksadhukh 
4082869Sgavinm static void
4092869Sgavinm synth_amd_info(struct cpuid_info *cpi)
4102869Sgavinm {
4112869Sgavinm 	const struct amd_rev_mapent *rmp;
4122869Sgavinm 	uint_t family, model, step;
4132869Sgavinm 	int i;
4142869Sgavinm 
4152869Sgavinm 	/*
4165248Sksadhukh 	 * Currently only AMD family 0xf and family 0x10 use these fields.
4172869Sgavinm 	 */
4185248Sksadhukh 	if (cpi->cpi_family != 0xf && cpi->cpi_family != 0x10)
4192869Sgavinm 		return;
4202869Sgavinm 
4212869Sgavinm 	family = cpi->cpi_family;
4222869Sgavinm 	model = cpi->cpi_model;
4232869Sgavinm 	step = cpi->cpi_step;
4242869Sgavinm 
4252869Sgavinm 	for (i = 0, rmp = amd_revmap; i < sizeof (amd_revmap) / sizeof (*rmp);
4262869Sgavinm 	    i++, rmp++) {
4272869Sgavinm 		if (family == rmp->rm_family &&
4282869Sgavinm 		    model >= rmp->rm_modello && model <= rmp->rm_modelhi &&
4292869Sgavinm 		    step >= rmp->rm_steplo && step <= rmp->rm_stephi) {
4302869Sgavinm 			cpi->cpi_chiprev = rmp->rm_chiprev;
4312869Sgavinm 			cpi->cpi_chiprevstr = rmp->rm_chiprevstr;
4322869Sgavinm 			cpi->cpi_socket = amd_skts[rmp->rm_sktidx][model & 0x3];
4332869Sgavinm 			return;
4342869Sgavinm 		}
4352869Sgavinm 	}
4362869Sgavinm }
4372869Sgavinm 
4382869Sgavinm static void
4392869Sgavinm synth_info(struct cpuid_info *cpi)
4402869Sgavinm {
4412869Sgavinm 	cpi->cpi_chiprev = X86_CHIPREV_UNKNOWN;
4422869Sgavinm 	cpi->cpi_chiprevstr = "Unknown";
4432869Sgavinm 	cpi->cpi_socket = X86_SOCKET_UNKNOWN;
4442869Sgavinm 
4452869Sgavinm 	switch (cpi->cpi_vendor) {
4462869Sgavinm 	case X86_VENDOR_AMD:
4472869Sgavinm 		synth_amd_info(cpi);
4482869Sgavinm 		break;
4492869Sgavinm 
4502869Sgavinm 	default:
4512869Sgavinm 		break;
4522869Sgavinm 
4532869Sgavinm 	}
4542869Sgavinm }
4552869Sgavinm 
4562869Sgavinm /*
4573446Smrj  * Apply up various platform-dependent restrictions where the
4583446Smrj  * underlying platform restrictions mean the CPU can be marked
4593446Smrj  * as less capable than its cpuid instruction would imply.
4603446Smrj  */
4615084Sjohnlev #if defined(__xpv)
4625084Sjohnlev static void
4635084Sjohnlev platform_cpuid_mangle(uint_t vendor, uint32_t eax, struct cpuid_regs *cp)
4645084Sjohnlev {
4655084Sjohnlev 	switch (eax) {
4665084Sjohnlev 	case 1:
4675084Sjohnlev 		cp->cp_edx &=
4685084Sjohnlev 		    ~(CPUID_INTC_EDX_PSE |
4695084Sjohnlev 		    CPUID_INTC_EDX_VME | CPUID_INTC_EDX_DE |
4705084Sjohnlev 		    CPUID_INTC_EDX_MCA |	/* XXPV true on dom0? */
4715084Sjohnlev 		    CPUID_INTC_EDX_SEP | CPUID_INTC_EDX_MTRR |
4725084Sjohnlev 		    CPUID_INTC_EDX_PGE | CPUID_INTC_EDX_PAT |
4735084Sjohnlev 		    CPUID_AMD_EDX_SYSC | CPUID_INTC_EDX_SEP |
4745084Sjohnlev 		    CPUID_INTC_EDX_PSE36 | CPUID_INTC_EDX_HTT);
4755084Sjohnlev 		break;
4765084Sjohnlev 
4775084Sjohnlev 	case 0x80000001:
4785084Sjohnlev 		cp->cp_edx &=
4795084Sjohnlev 		    ~(CPUID_AMD_EDX_PSE |
4805084Sjohnlev 		    CPUID_INTC_EDX_VME | CPUID_INTC_EDX_DE |
4815084Sjohnlev 		    CPUID_AMD_EDX_MTRR | CPUID_AMD_EDX_PGE |
4825084Sjohnlev 		    CPUID_AMD_EDX_PAT | CPUID_AMD_EDX_PSE36 |
4835084Sjohnlev 		    CPUID_AMD_EDX_SYSC | CPUID_INTC_EDX_SEP |
4845084Sjohnlev 		    CPUID_AMD_EDX_TSCP);
4855084Sjohnlev 		cp->cp_ecx &= ~CPUID_AMD_ECX_CMP_LGCY;
4865084Sjohnlev 		break;
4875084Sjohnlev 	default:
4885084Sjohnlev 		break;
4895084Sjohnlev 	}
4905084Sjohnlev 
4915084Sjohnlev 	switch (vendor) {
4925084Sjohnlev 	case X86_VENDOR_Intel:
4935084Sjohnlev 		switch (eax) {
4945084Sjohnlev 		case 4:
4955084Sjohnlev 			/*
4965084Sjohnlev 			 * Zero out the (ncores-per-chip - 1) field
4975084Sjohnlev 			 */
4985084Sjohnlev 			cp->cp_eax &= 0x03fffffff;
4995084Sjohnlev 			break;
5005084Sjohnlev 		default:
5015084Sjohnlev 			break;
5025084Sjohnlev 		}
5035084Sjohnlev 		break;
5045084Sjohnlev 	case X86_VENDOR_AMD:
5055084Sjohnlev 		switch (eax) {
5065084Sjohnlev 		case 0x80000008:
5075084Sjohnlev 			/*
5085084Sjohnlev 			 * Zero out the (ncores-per-chip - 1) field
5095084Sjohnlev 			 */
5105084Sjohnlev 			cp->cp_ecx &= 0xffffff00;
5115084Sjohnlev 			break;
5125084Sjohnlev 		default:
5135084Sjohnlev 			break;
5145084Sjohnlev 		}
5155084Sjohnlev 		break;
5165084Sjohnlev 	default:
5175084Sjohnlev 		break;
5185084Sjohnlev 	}
5195084Sjohnlev }
5205084Sjohnlev #else
5213446Smrj #define	platform_cpuid_mangle(vendor, eax, cp)	/* nothing */
5225084Sjohnlev #endif
5233446Smrj 
5243446Smrj /*
5250Sstevel@tonic-gate  *  Some undocumented ways of patching the results of the cpuid
5260Sstevel@tonic-gate  *  instruction to permit running Solaris 10 on future cpus that
5270Sstevel@tonic-gate  *  we don't currently support.  Could be set to non-zero values
5280Sstevel@tonic-gate  *  via settings in eeprom.
5290Sstevel@tonic-gate  */
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate uint32_t cpuid_feature_ecx_include;
5320Sstevel@tonic-gate uint32_t cpuid_feature_ecx_exclude;
5330Sstevel@tonic-gate uint32_t cpuid_feature_edx_include;
5340Sstevel@tonic-gate uint32_t cpuid_feature_edx_exclude;
5350Sstevel@tonic-gate 
5363446Smrj void
5373446Smrj cpuid_alloc_space(cpu_t *cpu)
5383446Smrj {
5393446Smrj 	/*
5403446Smrj 	 * By convention, cpu0 is the boot cpu, which is set up
5413446Smrj 	 * before memory allocation is available.  All other cpus get
5423446Smrj 	 * their cpuid_info struct allocated here.
5433446Smrj 	 */
5443446Smrj 	ASSERT(cpu->cpu_id != 0);
5453446Smrj 	cpu->cpu_m.mcpu_cpi =
5463446Smrj 	    kmem_zalloc(sizeof (*cpu->cpu_m.mcpu_cpi), KM_SLEEP);
5473446Smrj }
5483446Smrj 
5493446Smrj void
5503446Smrj cpuid_free_space(cpu_t *cpu)
5513446Smrj {
5524606Sesaxe 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
5534606Sesaxe 	int i;
5544606Sesaxe 
5553446Smrj 	ASSERT(cpu->cpu_id != 0);
5564606Sesaxe 
5574606Sesaxe 	/*
5584606Sesaxe 	 * Free up any function 4 related dynamic storage
5594606Sesaxe 	 */
5604606Sesaxe 	for (i = 1; i < cpi->cpi_std_4_size; i++)
5614606Sesaxe 		kmem_free(cpi->cpi_std_4[i], sizeof (struct cpuid_regs));
5624606Sesaxe 	if (cpi->cpi_std_4_size > 0)
5634606Sesaxe 		kmem_free(cpi->cpi_std_4,
5644606Sesaxe 		    cpi->cpi_std_4_size * sizeof (struct cpuid_regs *));
5654606Sesaxe 
5663446Smrj 	kmem_free(cpu->cpu_m.mcpu_cpi, sizeof (*cpu->cpu_m.mcpu_cpi));
5673446Smrj }
5683446Smrj 
5690Sstevel@tonic-gate uint_t
5700Sstevel@tonic-gate cpuid_pass1(cpu_t *cpu)
5710Sstevel@tonic-gate {
5720Sstevel@tonic-gate 	uint32_t mask_ecx, mask_edx;
5730Sstevel@tonic-gate 	uint_t feature = X86_CPUID;
5740Sstevel@tonic-gate 	struct cpuid_info *cpi;
5751228Sandrei 	struct cpuid_regs *cp;
5760Sstevel@tonic-gate 	int xcpuid;
5775084Sjohnlev #if !defined(__xpv)
5785045Sbholler 	extern int idle_cpu_prefer_mwait;
5795084Sjohnlev #endif
5803446Smrj 
5810Sstevel@tonic-gate 	/*
5823446Smrj 	 * Space statically allocated for cpu0, ensure pointer is set
5830Sstevel@tonic-gate 	 */
5840Sstevel@tonic-gate 	if (cpu->cpu_id == 0)
5853446Smrj 		cpu->cpu_m.mcpu_cpi = &cpuid_info0;
5863446Smrj 	cpi = cpu->cpu_m.mcpu_cpi;
5873446Smrj 	ASSERT(cpi != NULL);
5880Sstevel@tonic-gate 	cp = &cpi->cpi_std[0];
5891228Sandrei 	cp->cp_eax = 0;
5901228Sandrei 	cpi->cpi_maxeax = __cpuid_insn(cp);
5910Sstevel@tonic-gate 	{
5920Sstevel@tonic-gate 		uint32_t *iptr = (uint32_t *)cpi->cpi_vendorstr;
5930Sstevel@tonic-gate 		*iptr++ = cp->cp_ebx;
5940Sstevel@tonic-gate 		*iptr++ = cp->cp_edx;
5950Sstevel@tonic-gate 		*iptr++ = cp->cp_ecx;
5960Sstevel@tonic-gate 		*(char *)&cpi->cpi_vendorstr[12] = '\0';
5970Sstevel@tonic-gate 	}
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 	/*
6000Sstevel@tonic-gate 	 * Map the vendor string to a type code
6010Sstevel@tonic-gate 	 */
6020Sstevel@tonic-gate 	if (strcmp(cpi->cpi_vendorstr, "GenuineIntel") == 0)
6030Sstevel@tonic-gate 		cpi->cpi_vendor = X86_VENDOR_Intel;
6040Sstevel@tonic-gate 	else if (strcmp(cpi->cpi_vendorstr, "AuthenticAMD") == 0)
6050Sstevel@tonic-gate 		cpi->cpi_vendor = X86_VENDOR_AMD;
6060Sstevel@tonic-gate 	else if (strcmp(cpi->cpi_vendorstr, "GenuineTMx86") == 0)
6070Sstevel@tonic-gate 		cpi->cpi_vendor = X86_VENDOR_TM;
6080Sstevel@tonic-gate 	else if (strcmp(cpi->cpi_vendorstr, CyrixInstead) == 0)
6090Sstevel@tonic-gate 		/*
6100Sstevel@tonic-gate 		 * CyrixInstead is a variable used by the Cyrix detection code
6110Sstevel@tonic-gate 		 * in locore.
6120Sstevel@tonic-gate 		 */
6130Sstevel@tonic-gate 		cpi->cpi_vendor = X86_VENDOR_Cyrix;
6140Sstevel@tonic-gate 	else if (strcmp(cpi->cpi_vendorstr, "UMC UMC UMC ") == 0)
6150Sstevel@tonic-gate 		cpi->cpi_vendor = X86_VENDOR_UMC;
6160Sstevel@tonic-gate 	else if (strcmp(cpi->cpi_vendorstr, "NexGenDriven") == 0)
6170Sstevel@tonic-gate 		cpi->cpi_vendor = X86_VENDOR_NexGen;
6180Sstevel@tonic-gate 	else if (strcmp(cpi->cpi_vendorstr, "CentaurHauls") == 0)
6190Sstevel@tonic-gate 		cpi->cpi_vendor = X86_VENDOR_Centaur;
6200Sstevel@tonic-gate 	else if (strcmp(cpi->cpi_vendorstr, "RiseRiseRise") == 0)
6210Sstevel@tonic-gate 		cpi->cpi_vendor = X86_VENDOR_Rise;
6220Sstevel@tonic-gate 	else if (strcmp(cpi->cpi_vendorstr, "SiS SiS SiS ") == 0)
6230Sstevel@tonic-gate 		cpi->cpi_vendor = X86_VENDOR_SiS;
6240Sstevel@tonic-gate 	else if (strcmp(cpi->cpi_vendorstr, "Geode by NSC") == 0)
6250Sstevel@tonic-gate 		cpi->cpi_vendor = X86_VENDOR_NSC;
6260Sstevel@tonic-gate 	else
6270Sstevel@tonic-gate 		cpi->cpi_vendor = X86_VENDOR_IntelClone;
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	x86_vendor = cpi->cpi_vendor; /* for compatibility */
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 	/*
6320Sstevel@tonic-gate 	 * Limit the range in case of weird hardware
6330Sstevel@tonic-gate 	 */
6340Sstevel@tonic-gate 	if (cpi->cpi_maxeax > CPI_MAXEAX_MAX)
6350Sstevel@tonic-gate 		cpi->cpi_maxeax = CPI_MAXEAX_MAX;
6360Sstevel@tonic-gate 	if (cpi->cpi_maxeax < 1)
6370Sstevel@tonic-gate 		goto pass1_done;
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 	cp = &cpi->cpi_std[1];
6401228Sandrei 	cp->cp_eax = 1;
6411228Sandrei 	(void) __cpuid_insn(cp);
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	/*
6440Sstevel@tonic-gate 	 * Extract identifying constants for easy access.
6450Sstevel@tonic-gate 	 */
6460Sstevel@tonic-gate 	cpi->cpi_model = CPI_MODEL(cpi);
6470Sstevel@tonic-gate 	cpi->cpi_family = CPI_FAMILY(cpi);
6480Sstevel@tonic-gate 
6491975Sdmick 	if (cpi->cpi_family == 0xf)
6500Sstevel@tonic-gate 		cpi->cpi_family += CPI_FAMILY_XTD(cpi);
6511975Sdmick 
6522001Sdmick 	/*
6534265Skchow 	 * Beware: AMD uses "extended model" iff base *FAMILY* == 0xf.
6542001Sdmick 	 * Intel, and presumably everyone else, uses model == 0xf, as
6552001Sdmick 	 * one would expect (max value means possible overflow).  Sigh.
6562001Sdmick 	 */
6572001Sdmick 
6582001Sdmick 	switch (cpi->cpi_vendor) {
6594855Sksadhukh 	case X86_VENDOR_Intel:
6604855Sksadhukh 		if (IS_EXTENDED_MODEL_INTEL(cpi))
6614855Sksadhukh 			cpi->cpi_model += CPI_MODEL_XTD(cpi) << 4;
6624858Sksadhukh 		break;
6632001Sdmick 	case X86_VENDOR_AMD:
6644265Skchow 		if (CPI_FAMILY(cpi) == 0xf)
6652001Sdmick 			cpi->cpi_model += CPI_MODEL_XTD(cpi) << 4;
6662001Sdmick 		break;
6672001Sdmick 	default:
6682001Sdmick 		if (cpi->cpi_model == 0xf)
6692001Sdmick 			cpi->cpi_model += CPI_MODEL_XTD(cpi) << 4;
6702001Sdmick 		break;
6712001Sdmick 	}
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	cpi->cpi_step = CPI_STEP(cpi);
6740Sstevel@tonic-gate 	cpi->cpi_brandid = CPI_BRANDID(cpi);
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 	/*
6770Sstevel@tonic-gate 	 * *default* assumptions:
6780Sstevel@tonic-gate 	 * - believe %edx feature word
6790Sstevel@tonic-gate 	 * - ignore %ecx feature word
6800Sstevel@tonic-gate 	 * - 32-bit virtual and physical addressing
6810Sstevel@tonic-gate 	 */
6820Sstevel@tonic-gate 	mask_edx = 0xffffffff;
6830Sstevel@tonic-gate 	mask_ecx = 0;
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 	cpi->cpi_pabits = cpi->cpi_vabits = 32;
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
6880Sstevel@tonic-gate 	case X86_VENDOR_Intel:
6890Sstevel@tonic-gate 		if (cpi->cpi_family == 5)
6900Sstevel@tonic-gate 			x86_type = X86_TYPE_P5;
6911975Sdmick 		else if (IS_LEGACY_P6(cpi)) {
6920Sstevel@tonic-gate 			x86_type = X86_TYPE_P6;
6930Sstevel@tonic-gate 			pentiumpro_bug4046376 = 1;
6940Sstevel@tonic-gate 			pentiumpro_bug4064495 = 1;
6950Sstevel@tonic-gate 			/*
6960Sstevel@tonic-gate 			 * Clear the SEP bit when it was set erroneously
6970Sstevel@tonic-gate 			 */
6980Sstevel@tonic-gate 			if (cpi->cpi_model < 3 && cpi->cpi_step < 3)
6990Sstevel@tonic-gate 				cp->cp_edx &= ~CPUID_INTC_EDX_SEP;
7001975Sdmick 		} else if (IS_NEW_F6(cpi) || cpi->cpi_family == 0xf) {
7010Sstevel@tonic-gate 			x86_type = X86_TYPE_P4;
7020Sstevel@tonic-gate 			/*
7030Sstevel@tonic-gate 			 * We don't currently depend on any of the %ecx
7040Sstevel@tonic-gate 			 * features until Prescott, so we'll only check
7050Sstevel@tonic-gate 			 * this from P4 onwards.  We might want to revisit
7060Sstevel@tonic-gate 			 * that idea later.
7070Sstevel@tonic-gate 			 */
7080Sstevel@tonic-gate 			mask_ecx = 0xffffffff;
7090Sstevel@tonic-gate 		} else if (cpi->cpi_family > 0xf)
7100Sstevel@tonic-gate 			mask_ecx = 0xffffffff;
7114636Sbholler 		/*
7124636Sbholler 		 * We don't support MONITOR/MWAIT if leaf 5 is not available
7134636Sbholler 		 * to obtain the monitor linesize.
7144636Sbholler 		 */
7154636Sbholler 		if (cpi->cpi_maxeax < 5)
7164636Sbholler 			mask_ecx &= ~CPUID_INTC_ECX_MON;
7170Sstevel@tonic-gate 		break;
7180Sstevel@tonic-gate 	case X86_VENDOR_IntelClone:
7190Sstevel@tonic-gate 	default:
7200Sstevel@tonic-gate 		break;
7210Sstevel@tonic-gate 	case X86_VENDOR_AMD:
7220Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_108)
7230Sstevel@tonic-gate 		if (cpi->cpi_family == 0xf && cpi->cpi_model == 0xe) {
7240Sstevel@tonic-gate 			cp->cp_eax = (0xf0f & cp->cp_eax) | 0xc0;
7250Sstevel@tonic-gate 			cpi->cpi_model = 0xc;
7260Sstevel@tonic-gate 		} else
7270Sstevel@tonic-gate #endif
7280Sstevel@tonic-gate 		if (cpi->cpi_family == 5) {
7290Sstevel@tonic-gate 			/*
7300Sstevel@tonic-gate 			 * AMD K5 and K6
7310Sstevel@tonic-gate 			 *
7320Sstevel@tonic-gate 			 * These CPUs have an incomplete implementation
7330Sstevel@tonic-gate 			 * of MCA/MCE which we mask away.
7340Sstevel@tonic-gate 			 */
7351228Sandrei 			mask_edx &= ~(CPUID_INTC_EDX_MCE | CPUID_INTC_EDX_MCA);
7361228Sandrei 
7371228Sandrei 			/*
7381228Sandrei 			 * Model 0 uses the wrong (APIC) bit
7391228Sandrei 			 * to indicate PGE.  Fix it here.
7401228Sandrei 			 */
7410Sstevel@tonic-gate 			if (cpi->cpi_model == 0) {
7420Sstevel@tonic-gate 				if (cp->cp_edx & 0x200) {
7430Sstevel@tonic-gate 					cp->cp_edx &= ~0x200;
7440Sstevel@tonic-gate 					cp->cp_edx |= CPUID_INTC_EDX_PGE;
7450Sstevel@tonic-gate 				}
7461228Sandrei 			}
7471228Sandrei 
7481228Sandrei 			/*
7491228Sandrei 			 * Early models had problems w/ MMX; disable.
7501228Sandrei 			 */
7511228Sandrei 			if (cpi->cpi_model < 6)
7521228Sandrei 				mask_edx &= ~CPUID_INTC_EDX_MMX;
7531228Sandrei 		}
7541228Sandrei 
7551228Sandrei 		/*
7561228Sandrei 		 * For newer families, SSE3 and CX16, at least, are valid;
7571228Sandrei 		 * enable all
7581228Sandrei 		 */
7591228Sandrei 		if (cpi->cpi_family >= 0xf)
760771Sdmick 			mask_ecx = 0xffffffff;
7614636Sbholler 		/*
7624636Sbholler 		 * We don't support MONITOR/MWAIT if leaf 5 is not available
7634636Sbholler 		 * to obtain the monitor linesize.
7644636Sbholler 		 */
7654636Sbholler 		if (cpi->cpi_maxeax < 5)
7664636Sbholler 			mask_ecx &= ~CPUID_INTC_ECX_MON;
7675045Sbholler 
7685084Sjohnlev #if !defined(__xpv)
7695045Sbholler 		/*
7705045Sbholler 		 * Do not use MONITOR/MWAIT to halt in the idle loop on any AMD
7715045Sbholler 		 * processors.  AMD does not intend MWAIT to be used in the cpu
7725045Sbholler 		 * idle loop on current and future processors.  10h and future
7735045Sbholler 		 * AMD processors use more power in MWAIT than HLT.
7745045Sbholler 		 * Pre-family-10h Opterons do not have the MWAIT instruction.
7755045Sbholler 		 */
7765045Sbholler 		idle_cpu_prefer_mwait = 0;
7775084Sjohnlev #endif
7785045Sbholler 
7790Sstevel@tonic-gate 		break;
7800Sstevel@tonic-gate 	case X86_VENDOR_TM:
7810Sstevel@tonic-gate 		/*
7820Sstevel@tonic-gate 		 * workaround the NT workaround in CMS 4.1
7830Sstevel@tonic-gate 		 */
7840Sstevel@tonic-gate 		if (cpi->cpi_family == 5 && cpi->cpi_model == 4 &&
7850Sstevel@tonic-gate 		    (cpi->cpi_step == 2 || cpi->cpi_step == 3))
7860Sstevel@tonic-gate 			cp->cp_edx |= CPUID_INTC_EDX_CX8;
7870Sstevel@tonic-gate 		break;
7880Sstevel@tonic-gate 	case X86_VENDOR_Centaur:
7890Sstevel@tonic-gate 		/*
7900Sstevel@tonic-gate 		 * workaround the NT workarounds again
7910Sstevel@tonic-gate 		 */
7920Sstevel@tonic-gate 		if (cpi->cpi_family == 6)
7930Sstevel@tonic-gate 			cp->cp_edx |= CPUID_INTC_EDX_CX8;
7940Sstevel@tonic-gate 		break;
7950Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
7960Sstevel@tonic-gate 		/*
7970Sstevel@tonic-gate 		 * We rely heavily on the probing in locore
7980Sstevel@tonic-gate 		 * to actually figure out what parts, if any,
7990Sstevel@tonic-gate 		 * of the Cyrix cpuid instruction to believe.
8000Sstevel@tonic-gate 		 */
8010Sstevel@tonic-gate 		switch (x86_type) {
8020Sstevel@tonic-gate 		case X86_TYPE_CYRIX_486:
8030Sstevel@tonic-gate 			mask_edx = 0;
8040Sstevel@tonic-gate 			break;
8050Sstevel@tonic-gate 		case X86_TYPE_CYRIX_6x86:
8060Sstevel@tonic-gate 			mask_edx = 0;
8070Sstevel@tonic-gate 			break;
8080Sstevel@tonic-gate 		case X86_TYPE_CYRIX_6x86L:
8090Sstevel@tonic-gate 			mask_edx =
8100Sstevel@tonic-gate 			    CPUID_INTC_EDX_DE |
8110Sstevel@tonic-gate 			    CPUID_INTC_EDX_CX8;
8120Sstevel@tonic-gate 			break;
8130Sstevel@tonic-gate 		case X86_TYPE_CYRIX_6x86MX:
8140Sstevel@tonic-gate 			mask_edx =
8150Sstevel@tonic-gate 			    CPUID_INTC_EDX_DE |
8160Sstevel@tonic-gate 			    CPUID_INTC_EDX_MSR |
8170Sstevel@tonic-gate 			    CPUID_INTC_EDX_CX8 |
8180Sstevel@tonic-gate 			    CPUID_INTC_EDX_PGE |
8190Sstevel@tonic-gate 			    CPUID_INTC_EDX_CMOV |
8200Sstevel@tonic-gate 			    CPUID_INTC_EDX_MMX;
8210Sstevel@tonic-gate 			break;
8220Sstevel@tonic-gate 		case X86_TYPE_CYRIX_GXm:
8230Sstevel@tonic-gate 			mask_edx =
8240Sstevel@tonic-gate 			    CPUID_INTC_EDX_MSR |
8250Sstevel@tonic-gate 			    CPUID_INTC_EDX_CX8 |
8260Sstevel@tonic-gate 			    CPUID_INTC_EDX_CMOV |
8270Sstevel@tonic-gate 			    CPUID_INTC_EDX_MMX;
8280Sstevel@tonic-gate 			break;
8290Sstevel@tonic-gate 		case X86_TYPE_CYRIX_MediaGX:
8300Sstevel@tonic-gate 			break;
8310Sstevel@tonic-gate 		case X86_TYPE_CYRIX_MII:
8320Sstevel@tonic-gate 		case X86_TYPE_VIA_CYRIX_III:
8330Sstevel@tonic-gate 			mask_edx =
8340Sstevel@tonic-gate 			    CPUID_INTC_EDX_DE |
8350Sstevel@tonic-gate 			    CPUID_INTC_EDX_TSC |
8360Sstevel@tonic-gate 			    CPUID_INTC_EDX_MSR |
8370Sstevel@tonic-gate 			    CPUID_INTC_EDX_CX8 |
8380Sstevel@tonic-gate 			    CPUID_INTC_EDX_PGE |
8390Sstevel@tonic-gate 			    CPUID_INTC_EDX_CMOV |
8400Sstevel@tonic-gate 			    CPUID_INTC_EDX_MMX;
8410Sstevel@tonic-gate 			break;
8420Sstevel@tonic-gate 		default:
8430Sstevel@tonic-gate 			break;
8440Sstevel@tonic-gate 		}
8450Sstevel@tonic-gate 		break;
8460Sstevel@tonic-gate 	}
8470Sstevel@tonic-gate 
8485084Sjohnlev #if defined(__xpv)
8495084Sjohnlev 	/*
8505084Sjohnlev 	 * Do not support MONITOR/MWAIT under a hypervisor
8515084Sjohnlev 	 */
8525084Sjohnlev 	mask_ecx &= ~CPUID_INTC_ECX_MON;
8535084Sjohnlev #endif	/* __xpv */
8545084Sjohnlev 
8550Sstevel@tonic-gate 	/*
8560Sstevel@tonic-gate 	 * Now we've figured out the masks that determine
8570Sstevel@tonic-gate 	 * which bits we choose to believe, apply the masks
8580Sstevel@tonic-gate 	 * to the feature words, then map the kernel's view
8590Sstevel@tonic-gate 	 * of these feature words into its feature word.
8600Sstevel@tonic-gate 	 */
8610Sstevel@tonic-gate 	cp->cp_edx &= mask_edx;
8620Sstevel@tonic-gate 	cp->cp_ecx &= mask_ecx;
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 	/*
8653446Smrj 	 * apply any platform restrictions (we don't call this
8663446Smrj 	 * immediately after __cpuid_insn here, because we need the
8673446Smrj 	 * workarounds applied above first)
8680Sstevel@tonic-gate 	 */
8693446Smrj 	platform_cpuid_mangle(cpi->cpi_vendor, 1, cp);
8700Sstevel@tonic-gate 
8713446Smrj 	/*
8723446Smrj 	 * fold in overrides from the "eeprom" mechanism
8733446Smrj 	 */
8740Sstevel@tonic-gate 	cp->cp_edx |= cpuid_feature_edx_include;
8750Sstevel@tonic-gate 	cp->cp_edx &= ~cpuid_feature_edx_exclude;
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 	cp->cp_ecx |= cpuid_feature_ecx_include;
8780Sstevel@tonic-gate 	cp->cp_ecx &= ~cpuid_feature_ecx_exclude;
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_PSE)
8810Sstevel@tonic-gate 		feature |= X86_LARGEPAGE;
8820Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_TSC)
8830Sstevel@tonic-gate 		feature |= X86_TSC;
8840Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_MSR)
8850Sstevel@tonic-gate 		feature |= X86_MSR;
8860Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_MTRR)
8870Sstevel@tonic-gate 		feature |= X86_MTRR;
8880Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_PGE)
8890Sstevel@tonic-gate 		feature |= X86_PGE;
8900Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_CMOV)
8910Sstevel@tonic-gate 		feature |= X86_CMOV;
8920Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_MMX)
8930Sstevel@tonic-gate 		feature |= X86_MMX;
8940Sstevel@tonic-gate 	if ((cp->cp_edx & CPUID_INTC_EDX_MCE) != 0 &&
8950Sstevel@tonic-gate 	    (cp->cp_edx & CPUID_INTC_EDX_MCA) != 0)
8960Sstevel@tonic-gate 		feature |= X86_MCA;
8970Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_PAE)
8980Sstevel@tonic-gate 		feature |= X86_PAE;
8990Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_CX8)
9000Sstevel@tonic-gate 		feature |= X86_CX8;
9010Sstevel@tonic-gate 	if (cp->cp_ecx & CPUID_INTC_ECX_CX16)
9020Sstevel@tonic-gate 		feature |= X86_CX16;
9030Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_PAT)
9040Sstevel@tonic-gate 		feature |= X86_PAT;
9050Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_SEP)
9060Sstevel@tonic-gate 		feature |= X86_SEP;
9070Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_FXSR) {
9080Sstevel@tonic-gate 		/*
9090Sstevel@tonic-gate 		 * In our implementation, fxsave/fxrstor
9100Sstevel@tonic-gate 		 * are prerequisites before we'll even
9110Sstevel@tonic-gate 		 * try and do SSE things.
9120Sstevel@tonic-gate 		 */
9130Sstevel@tonic-gate 		if (cp->cp_edx & CPUID_INTC_EDX_SSE)
9140Sstevel@tonic-gate 			feature |= X86_SSE;
9150Sstevel@tonic-gate 		if (cp->cp_edx & CPUID_INTC_EDX_SSE2)
9160Sstevel@tonic-gate 			feature |= X86_SSE2;
9170Sstevel@tonic-gate 		if (cp->cp_ecx & CPUID_INTC_ECX_SSE3)
9180Sstevel@tonic-gate 			feature |= X86_SSE3;
9195269Skk208521 		if (cpi->cpi_vendor == X86_VENDOR_Intel) {
9205269Skk208521 			if (cp->cp_ecx & CPUID_INTC_ECX_SSSE3)
9215269Skk208521 				feature |= X86_SSSE3;
9225269Skk208521 			if (cp->cp_ecx & CPUID_INTC_ECX_SSE4_1)
9235269Skk208521 				feature |= X86_SSE4_1;
9245269Skk208521 			if (cp->cp_ecx & CPUID_INTC_ECX_SSE4_2)
9255269Skk208521 				feature |= X86_SSE4_2;
9265269Skk208521 		}
9270Sstevel@tonic-gate 	}
9280Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_DE)
9293446Smrj 		feature |= X86_DE;
9304481Sbholler 	if (cp->cp_ecx & CPUID_INTC_ECX_MON) {
9314481Sbholler 		cpi->cpi_mwait.support |= MWAIT_SUPPORT;
9324481Sbholler 		feature |= X86_MWAIT;
9334481Sbholler 	}
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 	if (feature & X86_PAE)
9360Sstevel@tonic-gate 		cpi->cpi_pabits = 36;
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 	/*
9390Sstevel@tonic-gate 	 * Hyperthreading configuration is slightly tricky on Intel
9400Sstevel@tonic-gate 	 * and pure clones, and even trickier on AMD.
9410Sstevel@tonic-gate 	 *
9420Sstevel@tonic-gate 	 * (AMD chose to set the HTT bit on their CMP processors,
9430Sstevel@tonic-gate 	 * even though they're not actually hyperthreaded.  Thus it
9440Sstevel@tonic-gate 	 * takes a bit more work to figure out what's really going
9453446Smrj 	 * on ... see the handling of the CMP_LGCY bit below)
9460Sstevel@tonic-gate 	 */
9470Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_HTT) {
9480Sstevel@tonic-gate 		cpi->cpi_ncpu_per_chip = CPI_CPU_COUNT(cpi);
9490Sstevel@tonic-gate 		if (cpi->cpi_ncpu_per_chip > 1)
9500Sstevel@tonic-gate 			feature |= X86_HTT;
9511228Sandrei 	} else {
9521228Sandrei 		cpi->cpi_ncpu_per_chip = 1;
9530Sstevel@tonic-gate 	}
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 	/*
9560Sstevel@tonic-gate 	 * Work on the "extended" feature information, doing
9570Sstevel@tonic-gate 	 * some basic initialization for cpuid_pass2()
9580Sstevel@tonic-gate 	 */
9590Sstevel@tonic-gate 	xcpuid = 0;
9600Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
9610Sstevel@tonic-gate 	case X86_VENDOR_Intel:
9621975Sdmick 		if (IS_NEW_F6(cpi) || cpi->cpi_family >= 0xf)
9630Sstevel@tonic-gate 			xcpuid++;
9640Sstevel@tonic-gate 		break;
9650Sstevel@tonic-gate 	case X86_VENDOR_AMD:
9660Sstevel@tonic-gate 		if (cpi->cpi_family > 5 ||
9670Sstevel@tonic-gate 		    (cpi->cpi_family == 5 && cpi->cpi_model >= 1))
9680Sstevel@tonic-gate 			xcpuid++;
9690Sstevel@tonic-gate 		break;
9700Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
9710Sstevel@tonic-gate 		/*
9720Sstevel@tonic-gate 		 * Only these Cyrix CPUs are -known- to support
9730Sstevel@tonic-gate 		 * extended cpuid operations.
9740Sstevel@tonic-gate 		 */
9750Sstevel@tonic-gate 		if (x86_type == X86_TYPE_VIA_CYRIX_III ||
9760Sstevel@tonic-gate 		    x86_type == X86_TYPE_CYRIX_GXm)
9770Sstevel@tonic-gate 			xcpuid++;
9780Sstevel@tonic-gate 		break;
9790Sstevel@tonic-gate 	case X86_VENDOR_Centaur:
9800Sstevel@tonic-gate 	case X86_VENDOR_TM:
9810Sstevel@tonic-gate 	default:
9820Sstevel@tonic-gate 		xcpuid++;
9830Sstevel@tonic-gate 		break;
9840Sstevel@tonic-gate 	}
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate 	if (xcpuid) {
9870Sstevel@tonic-gate 		cp = &cpi->cpi_extd[0];
9881228Sandrei 		cp->cp_eax = 0x80000000;
9891228Sandrei 		cpi->cpi_xmaxeax = __cpuid_insn(cp);
9900Sstevel@tonic-gate 	}
9910Sstevel@tonic-gate 
9920Sstevel@tonic-gate 	if (cpi->cpi_xmaxeax & 0x80000000) {
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 		if (cpi->cpi_xmaxeax > CPI_XMAXEAX_MAX)
9950Sstevel@tonic-gate 			cpi->cpi_xmaxeax = CPI_XMAXEAX_MAX;
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 		switch (cpi->cpi_vendor) {
9980Sstevel@tonic-gate 		case X86_VENDOR_Intel:
9990Sstevel@tonic-gate 		case X86_VENDOR_AMD:
10000Sstevel@tonic-gate 			if (cpi->cpi_xmaxeax < 0x80000001)
10010Sstevel@tonic-gate 				break;
10020Sstevel@tonic-gate 			cp = &cpi->cpi_extd[1];
10031228Sandrei 			cp->cp_eax = 0x80000001;
10041228Sandrei 			(void) __cpuid_insn(cp);
10053446Smrj 
10060Sstevel@tonic-gate 			if (cpi->cpi_vendor == X86_VENDOR_AMD &&
10070Sstevel@tonic-gate 			    cpi->cpi_family == 5 &&
10080Sstevel@tonic-gate 			    cpi->cpi_model == 6 &&
10090Sstevel@tonic-gate 			    cpi->cpi_step == 6) {
10100Sstevel@tonic-gate 				/*
10110Sstevel@tonic-gate 				 * K6 model 6 uses bit 10 to indicate SYSC
10120Sstevel@tonic-gate 				 * Later models use bit 11. Fix it here.
10130Sstevel@tonic-gate 				 */
10140Sstevel@tonic-gate 				if (cp->cp_edx & 0x400) {
10150Sstevel@tonic-gate 					cp->cp_edx &= ~0x400;
10160Sstevel@tonic-gate 					cp->cp_edx |= CPUID_AMD_EDX_SYSC;
10170Sstevel@tonic-gate 				}
10180Sstevel@tonic-gate 			}
10190Sstevel@tonic-gate 
10203446Smrj 			platform_cpuid_mangle(cpi->cpi_vendor, 0x80000001, cp);
10213446Smrj 
10220Sstevel@tonic-gate 			/*
10230Sstevel@tonic-gate 			 * Compute the additions to the kernel's feature word.
10240Sstevel@tonic-gate 			 */
10250Sstevel@tonic-gate 			if (cp->cp_edx & CPUID_AMD_EDX_NX)
10260Sstevel@tonic-gate 				feature |= X86_NX;
10270Sstevel@tonic-gate 
10284628Skk208521 			if ((cpi->cpi_vendor == X86_VENDOR_AMD) &&
10294628Skk208521 			    (cpi->cpi_std[1].cp_edx & CPUID_INTC_EDX_FXSR) &&
10304628Skk208521 			    (cp->cp_ecx & CPUID_AMD_ECX_SSE4A))
10314628Skk208521 				feature |= X86_SSE4A;
10324628Skk208521 
10330Sstevel@tonic-gate 			/*
10343446Smrj 			 * If both the HTT and CMP_LGCY bits are set,
10351228Sandrei 			 * then we're not actually HyperThreaded.  Read
10361228Sandrei 			 * "AMD CPUID Specification" for more details.
10370Sstevel@tonic-gate 			 */
10380Sstevel@tonic-gate 			if (cpi->cpi_vendor == X86_VENDOR_AMD &&
10391228Sandrei 			    (feature & X86_HTT) &&
10403446Smrj 			    (cp->cp_ecx & CPUID_AMD_ECX_CMP_LGCY)) {
10410Sstevel@tonic-gate 				feature &= ~X86_HTT;
10421228Sandrei 				feature |= X86_CMP;
10431228Sandrei 			}
10443446Smrj #if defined(__amd64)
10450Sstevel@tonic-gate 			/*
10460Sstevel@tonic-gate 			 * It's really tricky to support syscall/sysret in
10470Sstevel@tonic-gate 			 * the i386 kernel; we rely on sysenter/sysexit
10480Sstevel@tonic-gate 			 * instead.  In the amd64 kernel, things are -way-
10490Sstevel@tonic-gate 			 * better.
10500Sstevel@tonic-gate 			 */
10510Sstevel@tonic-gate 			if (cp->cp_edx & CPUID_AMD_EDX_SYSC)
10520Sstevel@tonic-gate 				feature |= X86_ASYSC;
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate 			/*
10550Sstevel@tonic-gate 			 * While we're thinking about system calls, note
10560Sstevel@tonic-gate 			 * that AMD processors don't support sysenter
10570Sstevel@tonic-gate 			 * in long mode at all, so don't try to program them.
10580Sstevel@tonic-gate 			 */
10590Sstevel@tonic-gate 			if (x86_vendor == X86_VENDOR_AMD)
10600Sstevel@tonic-gate 				feature &= ~X86_SEP;
10610Sstevel@tonic-gate #endif
10623446Smrj 			if (cp->cp_edx & CPUID_AMD_EDX_TSCP)
10633446Smrj 				feature |= X86_TSCP;
10640Sstevel@tonic-gate 			break;
10650Sstevel@tonic-gate 		default:
10660Sstevel@tonic-gate 			break;
10670Sstevel@tonic-gate 		}
10680Sstevel@tonic-gate 
10691228Sandrei 		/*
10701228Sandrei 		 * Get CPUID data about processor cores and hyperthreads.
10711228Sandrei 		 */
10720Sstevel@tonic-gate 		switch (cpi->cpi_vendor) {
10730Sstevel@tonic-gate 		case X86_VENDOR_Intel:
10741228Sandrei 			if (cpi->cpi_maxeax >= 4) {
10751228Sandrei 				cp = &cpi->cpi_std[4];
10761228Sandrei 				cp->cp_eax = 4;
10771228Sandrei 				cp->cp_ecx = 0;
10781228Sandrei 				(void) __cpuid_insn(cp);
10793446Smrj 				platform_cpuid_mangle(cpi->cpi_vendor, 4, cp);
10801228Sandrei 			}
10811228Sandrei 			/*FALLTHROUGH*/
10820Sstevel@tonic-gate 		case X86_VENDOR_AMD:
10830Sstevel@tonic-gate 			if (cpi->cpi_xmaxeax < 0x80000008)
10840Sstevel@tonic-gate 				break;
10850Sstevel@tonic-gate 			cp = &cpi->cpi_extd[8];
10861228Sandrei 			cp->cp_eax = 0x80000008;
10871228Sandrei 			(void) __cpuid_insn(cp);
10883446Smrj 			platform_cpuid_mangle(cpi->cpi_vendor, 0x80000008, cp);
10893446Smrj 
10900Sstevel@tonic-gate 			/*
10910Sstevel@tonic-gate 			 * Virtual and physical address limits from
10920Sstevel@tonic-gate 			 * cpuid override previously guessed values.
10930Sstevel@tonic-gate 			 */
10940Sstevel@tonic-gate 			cpi->cpi_pabits = BITX(cp->cp_eax, 7, 0);
10950Sstevel@tonic-gate 			cpi->cpi_vabits = BITX(cp->cp_eax, 15, 8);
10960Sstevel@tonic-gate 			break;
10970Sstevel@tonic-gate 		default:
10980Sstevel@tonic-gate 			break;
10990Sstevel@tonic-gate 		}
11001228Sandrei 
11014606Sesaxe 		/*
11024606Sesaxe 		 * Derive the number of cores per chip
11034606Sesaxe 		 */
11041228Sandrei 		switch (cpi->cpi_vendor) {
11051228Sandrei 		case X86_VENDOR_Intel:
11061228Sandrei 			if (cpi->cpi_maxeax < 4) {
11071228Sandrei 				cpi->cpi_ncore_per_chip = 1;
11081228Sandrei 				break;
11091228Sandrei 			} else {
11101228Sandrei 				cpi->cpi_ncore_per_chip =
11111228Sandrei 				    BITX((cpi)->cpi_std[4].cp_eax, 31, 26) + 1;
11121228Sandrei 			}
11131228Sandrei 			break;
11141228Sandrei 		case X86_VENDOR_AMD:
11151228Sandrei 			if (cpi->cpi_xmaxeax < 0x80000008) {
11161228Sandrei 				cpi->cpi_ncore_per_chip = 1;
11171228Sandrei 				break;
11181228Sandrei 			} else {
11191228Sandrei 				cpi->cpi_ncore_per_chip =
11201228Sandrei 				    BITX((cpi)->cpi_extd[8].cp_ecx, 7, 0) + 1;
11211228Sandrei 			}
11221228Sandrei 			break;
11231228Sandrei 		default:
11241228Sandrei 			cpi->cpi_ncore_per_chip = 1;
11251228Sandrei 			break;
11261228Sandrei 		}
1127*5284Sgavinm 	} else {
1128*5284Sgavinm 		cpi->cpi_ncore_per_chip = 1;
11290Sstevel@tonic-gate 	}
11300Sstevel@tonic-gate 
11311228Sandrei 	/*
11321228Sandrei 	 * If more than one core, then this processor is CMP.
11331228Sandrei 	 */
11341228Sandrei 	if (cpi->cpi_ncore_per_chip > 1)
11351228Sandrei 		feature |= X86_CMP;
11363446Smrj 
11371228Sandrei 	/*
11381228Sandrei 	 * If the number of cores is the same as the number
11391228Sandrei 	 * of CPUs, then we cannot have HyperThreading.
11401228Sandrei 	 */
11411228Sandrei 	if (cpi->cpi_ncpu_per_chip == cpi->cpi_ncore_per_chip)
11421228Sandrei 		feature &= ~X86_HTT;
11431228Sandrei 
11440Sstevel@tonic-gate 	if ((feature & (X86_HTT | X86_CMP)) == 0) {
11451228Sandrei 		/*
11461228Sandrei 		 * Single-core single-threaded processors.
11471228Sandrei 		 */
11480Sstevel@tonic-gate 		cpi->cpi_chipid = -1;
11490Sstevel@tonic-gate 		cpi->cpi_clogid = 0;
11501228Sandrei 		cpi->cpi_coreid = cpu->cpu_id;
11510Sstevel@tonic-gate 	} else if (cpi->cpi_ncpu_per_chip > 1) {
11521228Sandrei 		uint_t i;
11531228Sandrei 		uint_t chipid_shift = 0;
11541228Sandrei 		uint_t coreid_shift = 0;
11551228Sandrei 		uint_t apic_id = CPI_APIC_ID(cpi);
11561228Sandrei 
11571228Sandrei 		for (i = 1; i < cpi->cpi_ncpu_per_chip; i <<= 1)
11581228Sandrei 			chipid_shift++;
11591228Sandrei 		cpi->cpi_chipid = apic_id >> chipid_shift;
11601228Sandrei 		cpi->cpi_clogid = apic_id & ((1 << chipid_shift) - 1);
11610Sstevel@tonic-gate 
11621228Sandrei 		if (cpi->cpi_vendor == X86_VENDOR_Intel) {
11631228Sandrei 			if (feature & X86_CMP) {
11641228Sandrei 				/*
11651228Sandrei 				 * Multi-core (and possibly multi-threaded)
11661228Sandrei 				 * processors.
11671228Sandrei 				 */
11681228Sandrei 				uint_t ncpu_per_core;
11691228Sandrei 				if (cpi->cpi_ncore_per_chip == 1)
11701228Sandrei 					ncpu_per_core = cpi->cpi_ncpu_per_chip;
11711228Sandrei 				else if (cpi->cpi_ncore_per_chip > 1)
11721228Sandrei 					ncpu_per_core = cpi->cpi_ncpu_per_chip /
11731228Sandrei 					    cpi->cpi_ncore_per_chip;
11741228Sandrei 				/*
11751228Sandrei 				 * 8bit APIC IDs on dual core Pentiums
11761228Sandrei 				 * look like this:
11771228Sandrei 				 *
11781228Sandrei 				 * +-----------------------+------+------+
11791228Sandrei 				 * | Physical Package ID   |  MC  |  HT  |
11801228Sandrei 				 * +-----------------------+------+------+
11811228Sandrei 				 * <------- chipid -------->
11821228Sandrei 				 * <------- coreid --------------->
11831228Sandrei 				 *			   <--- clogid -->
11841228Sandrei 				 *
11851228Sandrei 				 * Where the number of bits necessary to
11861228Sandrei 				 * represent MC and HT fields together equals
11871228Sandrei 				 * to the minimum number of bits necessary to
11881228Sandrei 				 * store the value of cpi->cpi_ncpu_per_chip.
11891228Sandrei 				 * Of those bits, the MC part uses the number
11901228Sandrei 				 * of bits necessary to store the value of
11911228Sandrei 				 * cpi->cpi_ncore_per_chip.
11921228Sandrei 				 */
11931228Sandrei 				for (i = 1; i < ncpu_per_core; i <<= 1)
11941228Sandrei 					coreid_shift++;
11951727Sandrei 				cpi->cpi_coreid = apic_id >> coreid_shift;
11961228Sandrei 			} else if (feature & X86_HTT) {
11971228Sandrei 				/*
11981228Sandrei 				 * Single-core multi-threaded processors.
11991228Sandrei 				 */
12001228Sandrei 				cpi->cpi_coreid = cpi->cpi_chipid;
12011228Sandrei 			}
12021228Sandrei 		} else if (cpi->cpi_vendor == X86_VENDOR_AMD) {
12031228Sandrei 			/*
12041228Sandrei 			 * AMD currently only has dual-core processors with
12051228Sandrei 			 * single-threaded cores.  If they ever release
12061228Sandrei 			 * multi-threaded processors, then this code
12071228Sandrei 			 * will have to be updated.
12081228Sandrei 			 */
12091228Sandrei 			cpi->cpi_coreid = cpu->cpu_id;
12101228Sandrei 		} else {
12111228Sandrei 			/*
12121228Sandrei 			 * All other processors are currently
12131228Sandrei 			 * assumed to have single cores.
12141228Sandrei 			 */
12151228Sandrei 			cpi->cpi_coreid = cpi->cpi_chipid;
12161228Sandrei 		}
12170Sstevel@tonic-gate 	}
12180Sstevel@tonic-gate 
12192869Sgavinm 	/*
12202869Sgavinm 	 * Synthesize chip "revision" and socket type
12212869Sgavinm 	 */
12222869Sgavinm 	synth_info(cpi);
12232869Sgavinm 
12240Sstevel@tonic-gate pass1_done:
12250Sstevel@tonic-gate 	cpi->cpi_pass = 1;
12260Sstevel@tonic-gate 	return (feature);
12270Sstevel@tonic-gate }
12280Sstevel@tonic-gate 
12290Sstevel@tonic-gate /*
12300Sstevel@tonic-gate  * Make copies of the cpuid table entries we depend on, in
12310Sstevel@tonic-gate  * part for ease of parsing now, in part so that we have only
12320Sstevel@tonic-gate  * one place to correct any of it, in part for ease of
12330Sstevel@tonic-gate  * later export to userland, and in part so we can look at
12340Sstevel@tonic-gate  * this stuff in a crash dump.
12350Sstevel@tonic-gate  */
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate /*ARGSUSED*/
12380Sstevel@tonic-gate void
12390Sstevel@tonic-gate cpuid_pass2(cpu_t *cpu)
12400Sstevel@tonic-gate {
12410Sstevel@tonic-gate 	uint_t n, nmax;
12420Sstevel@tonic-gate 	int i;
12431228Sandrei 	struct cpuid_regs *cp;
12440Sstevel@tonic-gate 	uint8_t *dp;
12450Sstevel@tonic-gate 	uint32_t *iptr;
12460Sstevel@tonic-gate 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate 	ASSERT(cpi->cpi_pass == 1);
12490Sstevel@tonic-gate 
12500Sstevel@tonic-gate 	if (cpi->cpi_maxeax < 1)
12510Sstevel@tonic-gate 		goto pass2_done;
12520Sstevel@tonic-gate 
12530Sstevel@tonic-gate 	if ((nmax = cpi->cpi_maxeax + 1) > NMAX_CPI_STD)
12540Sstevel@tonic-gate 		nmax = NMAX_CPI_STD;
12550Sstevel@tonic-gate 	/*
12560Sstevel@tonic-gate 	 * (We already handled n == 0 and n == 1 in pass 1)
12570Sstevel@tonic-gate 	 */
12580Sstevel@tonic-gate 	for (n = 2, cp = &cpi->cpi_std[2]; n < nmax; n++, cp++) {
12591228Sandrei 		cp->cp_eax = n;
12604606Sesaxe 
12614606Sesaxe 		/*
12624606Sesaxe 		 * CPUID function 4 expects %ecx to be initialized
12634606Sesaxe 		 * with an index which indicates which cache to return
12644606Sesaxe 		 * information about. The OS is expected to call function 4
12654606Sesaxe 		 * with %ecx set to 0, 1, 2, ... until it returns with
12664606Sesaxe 		 * EAX[4:0] set to 0, which indicates there are no more
12674606Sesaxe 		 * caches.
12684606Sesaxe 		 *
12694606Sesaxe 		 * Here, populate cpi_std[4] with the information returned by
12704606Sesaxe 		 * function 4 when %ecx == 0, and do the rest in cpuid_pass3()
12714606Sesaxe 		 * when dynamic memory allocation becomes available.
12724606Sesaxe 		 *
12734606Sesaxe 		 * Note: we need to explicitly initialize %ecx here, since
12744606Sesaxe 		 * function 4 may have been previously invoked.
12754606Sesaxe 		 */
12764606Sesaxe 		if (n == 4)
12774606Sesaxe 			cp->cp_ecx = 0;
12784606Sesaxe 
12791228Sandrei 		(void) __cpuid_insn(cp);
12803446Smrj 		platform_cpuid_mangle(cpi->cpi_vendor, n, cp);
12810Sstevel@tonic-gate 		switch (n) {
12820Sstevel@tonic-gate 		case 2:
12830Sstevel@tonic-gate 			/*
12840Sstevel@tonic-gate 			 * "the lower 8 bits of the %eax register
12850Sstevel@tonic-gate 			 * contain a value that identifies the number
12860Sstevel@tonic-gate 			 * of times the cpuid [instruction] has to be
12870Sstevel@tonic-gate 			 * executed to obtain a complete image of the
12880Sstevel@tonic-gate 			 * processor's caching systems."
12890Sstevel@tonic-gate 			 *
12900Sstevel@tonic-gate 			 * How *do* they make this stuff up?
12910Sstevel@tonic-gate 			 */
12920Sstevel@tonic-gate 			cpi->cpi_ncache = sizeof (*cp) *
12930Sstevel@tonic-gate 			    BITX(cp->cp_eax, 7, 0);
12940Sstevel@tonic-gate 			if (cpi->cpi_ncache == 0)
12950Sstevel@tonic-gate 				break;
12960Sstevel@tonic-gate 			cpi->cpi_ncache--;	/* skip count byte */
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate 			/*
12990Sstevel@tonic-gate 			 * Well, for now, rather than attempt to implement
13000Sstevel@tonic-gate 			 * this slightly dubious algorithm, we just look
13010Sstevel@tonic-gate 			 * at the first 15 ..
13020Sstevel@tonic-gate 			 */
13030Sstevel@tonic-gate 			if (cpi->cpi_ncache > (sizeof (*cp) - 1))
13040Sstevel@tonic-gate 				cpi->cpi_ncache = sizeof (*cp) - 1;
13050Sstevel@tonic-gate 
13060Sstevel@tonic-gate 			dp = cpi->cpi_cacheinfo;
13070Sstevel@tonic-gate 			if (BITX(cp->cp_eax, 31, 31) == 0) {
13080Sstevel@tonic-gate 				uint8_t *p = (void *)&cp->cp_eax;
13090Sstevel@tonic-gate 				for (i = 1; i < 3; i++)
13100Sstevel@tonic-gate 					if (p[i] != 0)
13110Sstevel@tonic-gate 						*dp++ = p[i];
13120Sstevel@tonic-gate 			}
13130Sstevel@tonic-gate 			if (BITX(cp->cp_ebx, 31, 31) == 0) {
13140Sstevel@tonic-gate 				uint8_t *p = (void *)&cp->cp_ebx;
13150Sstevel@tonic-gate 				for (i = 0; i < 4; i++)
13160Sstevel@tonic-gate 					if (p[i] != 0)
13170Sstevel@tonic-gate 						*dp++ = p[i];
13180Sstevel@tonic-gate 			}
13190Sstevel@tonic-gate 			if (BITX(cp->cp_ecx, 31, 31) == 0) {
13200Sstevel@tonic-gate 				uint8_t *p = (void *)&cp->cp_ecx;
13210Sstevel@tonic-gate 				for (i = 0; i < 4; i++)
13220Sstevel@tonic-gate 					if (p[i] != 0)
13230Sstevel@tonic-gate 						*dp++ = p[i];
13240Sstevel@tonic-gate 			}
13250Sstevel@tonic-gate 			if (BITX(cp->cp_edx, 31, 31) == 0) {
13260Sstevel@tonic-gate 				uint8_t *p = (void *)&cp->cp_edx;
13270Sstevel@tonic-gate 				for (i = 0; i < 4; i++)
13280Sstevel@tonic-gate 					if (p[i] != 0)
13290Sstevel@tonic-gate 						*dp++ = p[i];
13300Sstevel@tonic-gate 			}
13310Sstevel@tonic-gate 			break;
13324481Sbholler 
13330Sstevel@tonic-gate 		case 3:	/* Processor serial number, if PSN supported */
13344481Sbholler 			break;
13354481Sbholler 
13360Sstevel@tonic-gate 		case 4:	/* Deterministic cache parameters */
13374481Sbholler 			break;
13384481Sbholler 
13390Sstevel@tonic-gate 		case 5:	/* Monitor/Mwait parameters */
13405045Sbholler 		{
13415045Sbholler 			size_t mwait_size;
13424481Sbholler 
13434481Sbholler 			/*
13444481Sbholler 			 * check cpi_mwait.support which was set in cpuid_pass1
13454481Sbholler 			 */
13464481Sbholler 			if (!(cpi->cpi_mwait.support & MWAIT_SUPPORT))
13474481Sbholler 				break;
13484481Sbholler 
13495045Sbholler 			/*
13505045Sbholler 			 * Protect ourself from insane mwait line size.
13515045Sbholler 			 * Workaround for incomplete hardware emulator(s).
13525045Sbholler 			 */
13535045Sbholler 			mwait_size = (size_t)MWAIT_SIZE_MAX(cpi);
13545045Sbholler 			if (mwait_size < sizeof (uint32_t) ||
13555045Sbholler 			    !ISP2(mwait_size)) {
13565045Sbholler #if DEBUG
13575045Sbholler 				cmn_err(CE_NOTE, "Cannot handle cpu %d mwait "
13585045Sbholler 				    "size %ld",
13595045Sbholler 				    cpu->cpu_id, (long)mwait_size);
13605045Sbholler #endif
13615045Sbholler 				break;
13625045Sbholler 			}
13635045Sbholler 
13644481Sbholler 			cpi->cpi_mwait.mon_min = (size_t)MWAIT_SIZE_MIN(cpi);
13655045Sbholler 			cpi->cpi_mwait.mon_max = mwait_size;
13664481Sbholler 			if (MWAIT_EXTENSION(cpi)) {
13674481Sbholler 				cpi->cpi_mwait.support |= MWAIT_EXTENSIONS;
13684481Sbholler 				if (MWAIT_INT_ENABLE(cpi))
13694481Sbholler 					cpi->cpi_mwait.support |=
13704481Sbholler 					    MWAIT_ECX_INT_ENABLE;
13714481Sbholler 			}
13724481Sbholler 			break;
13735045Sbholler 		}
13740Sstevel@tonic-gate 		default:
13750Sstevel@tonic-gate 			break;
13760Sstevel@tonic-gate 		}
13770Sstevel@tonic-gate 	}
13780Sstevel@tonic-gate 
13790Sstevel@tonic-gate 	if ((cpi->cpi_xmaxeax & 0x80000000) == 0)
13800Sstevel@tonic-gate 		goto pass2_done;
13810Sstevel@tonic-gate 
13820Sstevel@tonic-gate 	if ((nmax = cpi->cpi_xmaxeax - 0x80000000 + 1) > NMAX_CPI_EXTD)
13830Sstevel@tonic-gate 		nmax = NMAX_CPI_EXTD;
13840Sstevel@tonic-gate 	/*
13850Sstevel@tonic-gate 	 * Copy the extended properties, fixing them as we go.
13860Sstevel@tonic-gate 	 * (We already handled n == 0 and n == 1 in pass 1)
13870Sstevel@tonic-gate 	 */
13880Sstevel@tonic-gate 	iptr = (void *)cpi->cpi_brandstr;
13890Sstevel@tonic-gate 	for (n = 2, cp = &cpi->cpi_extd[2]; n < nmax; cp++, n++) {
13901228Sandrei 		cp->cp_eax = 0x80000000 + n;
13911228Sandrei 		(void) __cpuid_insn(cp);
13923446Smrj 		platform_cpuid_mangle(cpi->cpi_vendor, 0x80000000 + n, cp);
13930Sstevel@tonic-gate 		switch (n) {
13940Sstevel@tonic-gate 		case 2:
13950Sstevel@tonic-gate 		case 3:
13960Sstevel@tonic-gate 		case 4:
13970Sstevel@tonic-gate 			/*
13980Sstevel@tonic-gate 			 * Extract the brand string
13990Sstevel@tonic-gate 			 */
14000Sstevel@tonic-gate 			*iptr++ = cp->cp_eax;
14010Sstevel@tonic-gate 			*iptr++ = cp->cp_ebx;
14020Sstevel@tonic-gate 			*iptr++ = cp->cp_ecx;
14030Sstevel@tonic-gate 			*iptr++ = cp->cp_edx;
14040Sstevel@tonic-gate 			break;
14050Sstevel@tonic-gate 		case 5:
14060Sstevel@tonic-gate 			switch (cpi->cpi_vendor) {
14070Sstevel@tonic-gate 			case X86_VENDOR_AMD:
14080Sstevel@tonic-gate 				/*
14090Sstevel@tonic-gate 				 * The Athlon and Duron were the first
14100Sstevel@tonic-gate 				 * parts to report the sizes of the
14110Sstevel@tonic-gate 				 * TLB for large pages. Before then,
14120Sstevel@tonic-gate 				 * we don't trust the data.
14130Sstevel@tonic-gate 				 */
14140Sstevel@tonic-gate 				if (cpi->cpi_family < 6 ||
14150Sstevel@tonic-gate 				    (cpi->cpi_family == 6 &&
14160Sstevel@tonic-gate 				    cpi->cpi_model < 1))
14170Sstevel@tonic-gate 					cp->cp_eax = 0;
14180Sstevel@tonic-gate 				break;
14190Sstevel@tonic-gate 			default:
14200Sstevel@tonic-gate 				break;
14210Sstevel@tonic-gate 			}
14220Sstevel@tonic-gate 			break;
14230Sstevel@tonic-gate 		case 6:
14240Sstevel@tonic-gate 			switch (cpi->cpi_vendor) {
14250Sstevel@tonic-gate 			case X86_VENDOR_AMD:
14260Sstevel@tonic-gate 				/*
14270Sstevel@tonic-gate 				 * The Athlon and Duron were the first
14280Sstevel@tonic-gate 				 * AMD parts with L2 TLB's.
14290Sstevel@tonic-gate 				 * Before then, don't trust the data.
14300Sstevel@tonic-gate 				 */
14310Sstevel@tonic-gate 				if (cpi->cpi_family < 6 ||
14320Sstevel@tonic-gate 				    cpi->cpi_family == 6 &&
14330Sstevel@tonic-gate 				    cpi->cpi_model < 1)
14340Sstevel@tonic-gate 					cp->cp_eax = cp->cp_ebx = 0;
14350Sstevel@tonic-gate 				/*
14360Sstevel@tonic-gate 				 * AMD Duron rev A0 reports L2
14370Sstevel@tonic-gate 				 * cache size incorrectly as 1K
14380Sstevel@tonic-gate 				 * when it is really 64K
14390Sstevel@tonic-gate 				 */
14400Sstevel@tonic-gate 				if (cpi->cpi_family == 6 &&
14410Sstevel@tonic-gate 				    cpi->cpi_model == 3 &&
14420Sstevel@tonic-gate 				    cpi->cpi_step == 0) {
14430Sstevel@tonic-gate 					cp->cp_ecx &= 0xffff;
14440Sstevel@tonic-gate 					cp->cp_ecx |= 0x400000;
14450Sstevel@tonic-gate 				}
14460Sstevel@tonic-gate 				break;
14470Sstevel@tonic-gate 			case X86_VENDOR_Cyrix:	/* VIA C3 */
14480Sstevel@tonic-gate 				/*
14490Sstevel@tonic-gate 				 * VIA C3 processors are a bit messed
14500Sstevel@tonic-gate 				 * up w.r.t. encoding cache sizes in %ecx
14510Sstevel@tonic-gate 				 */
14520Sstevel@tonic-gate 				if (cpi->cpi_family != 6)
14530Sstevel@tonic-gate 					break;
14540Sstevel@tonic-gate 				/*
14550Sstevel@tonic-gate 				 * model 7 and 8 were incorrectly encoded
14560Sstevel@tonic-gate 				 *
14570Sstevel@tonic-gate 				 * xxx is model 8 really broken?
14580Sstevel@tonic-gate 				 */
14590Sstevel@tonic-gate 				if (cpi->cpi_model == 7 ||
14600Sstevel@tonic-gate 				    cpi->cpi_model == 8)
14610Sstevel@tonic-gate 					cp->cp_ecx =
14620Sstevel@tonic-gate 					    BITX(cp->cp_ecx, 31, 24) << 16 |
14630Sstevel@tonic-gate 					    BITX(cp->cp_ecx, 23, 16) << 12 |
14640Sstevel@tonic-gate 					    BITX(cp->cp_ecx, 15, 8) << 8 |
14650Sstevel@tonic-gate 					    BITX(cp->cp_ecx, 7, 0);
14660Sstevel@tonic-gate 				/*
14670Sstevel@tonic-gate 				 * model 9 stepping 1 has wrong associativity
14680Sstevel@tonic-gate 				 */
14690Sstevel@tonic-gate 				if (cpi->cpi_model == 9 && cpi->cpi_step == 1)
14700Sstevel@tonic-gate 					cp->cp_ecx |= 8 << 12;
14710Sstevel@tonic-gate 				break;
14720Sstevel@tonic-gate 			case X86_VENDOR_Intel:
14730Sstevel@tonic-gate 				/*
14740Sstevel@tonic-gate 				 * Extended L2 Cache features function.
14750Sstevel@tonic-gate 				 * First appeared on Prescott.
14760Sstevel@tonic-gate 				 */
14770Sstevel@tonic-gate 			default:
14780Sstevel@tonic-gate 				break;
14790Sstevel@tonic-gate 			}
14800Sstevel@tonic-gate 			break;
14810Sstevel@tonic-gate 		default:
14820Sstevel@tonic-gate 			break;
14830Sstevel@tonic-gate 		}
14840Sstevel@tonic-gate 	}
14850Sstevel@tonic-gate 
14860Sstevel@tonic-gate pass2_done:
14870Sstevel@tonic-gate 	cpi->cpi_pass = 2;
14880Sstevel@tonic-gate }
14890Sstevel@tonic-gate 
14900Sstevel@tonic-gate static const char *
14910Sstevel@tonic-gate intel_cpubrand(const struct cpuid_info *cpi)
14920Sstevel@tonic-gate {
14930Sstevel@tonic-gate 	int i;
14940Sstevel@tonic-gate 
14950Sstevel@tonic-gate 	if ((x86_feature & X86_CPUID) == 0 ||
14960Sstevel@tonic-gate 	    cpi->cpi_maxeax < 1 || cpi->cpi_family < 5)
14970Sstevel@tonic-gate 		return ("i486");
14980Sstevel@tonic-gate 
14990Sstevel@tonic-gate 	switch (cpi->cpi_family) {
15000Sstevel@tonic-gate 	case 5:
15010Sstevel@tonic-gate 		return ("Intel Pentium(r)");
15020Sstevel@tonic-gate 	case 6:
15030Sstevel@tonic-gate 		switch (cpi->cpi_model) {
15040Sstevel@tonic-gate 			uint_t celeron, xeon;
15051228Sandrei 			const struct cpuid_regs *cp;
15060Sstevel@tonic-gate 		case 0:
15070Sstevel@tonic-gate 		case 1:
15080Sstevel@tonic-gate 		case 2:
15090Sstevel@tonic-gate 			return ("Intel Pentium(r) Pro");
15100Sstevel@tonic-gate 		case 3:
15110Sstevel@tonic-gate 		case 4:
15120Sstevel@tonic-gate 			return ("Intel Pentium(r) II");
15130Sstevel@tonic-gate 		case 6:
15140Sstevel@tonic-gate 			return ("Intel Celeron(r)");
15150Sstevel@tonic-gate 		case 5:
15160Sstevel@tonic-gate 		case 7:
15170Sstevel@tonic-gate 			celeron = xeon = 0;
15180Sstevel@tonic-gate 			cp = &cpi->cpi_std[2];	/* cache info */
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 			for (i = 1; i < 3; i++) {
15210Sstevel@tonic-gate 				uint_t tmp;
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate 				tmp = (cp->cp_eax >> (8 * i)) & 0xff;
15240Sstevel@tonic-gate 				if (tmp == 0x40)
15250Sstevel@tonic-gate 					celeron++;
15260Sstevel@tonic-gate 				if (tmp >= 0x44 && tmp <= 0x45)
15270Sstevel@tonic-gate 					xeon++;
15280Sstevel@tonic-gate 			}
15290Sstevel@tonic-gate 
15300Sstevel@tonic-gate 			for (i = 0; i < 2; i++) {
15310Sstevel@tonic-gate 				uint_t tmp;
15320Sstevel@tonic-gate 
15330Sstevel@tonic-gate 				tmp = (cp->cp_ebx >> (8 * i)) & 0xff;
15340Sstevel@tonic-gate 				if (tmp == 0x40)
15350Sstevel@tonic-gate 					celeron++;
15360Sstevel@tonic-gate 				else if (tmp >= 0x44 && tmp <= 0x45)
15370Sstevel@tonic-gate 					xeon++;
15380Sstevel@tonic-gate 			}
15390Sstevel@tonic-gate 
15400Sstevel@tonic-gate 			for (i = 0; i < 4; i++) {
15410Sstevel@tonic-gate 				uint_t tmp;
15420Sstevel@tonic-gate 
15430Sstevel@tonic-gate 				tmp = (cp->cp_ecx >> (8 * i)) & 0xff;
15440Sstevel@tonic-gate 				if (tmp == 0x40)
15450Sstevel@tonic-gate 					celeron++;
15460Sstevel@tonic-gate 				else if (tmp >= 0x44 && tmp <= 0x45)
15470Sstevel@tonic-gate 					xeon++;
15480Sstevel@tonic-gate 			}
15490Sstevel@tonic-gate 
15500Sstevel@tonic-gate 			for (i = 0; i < 4; i++) {
15510Sstevel@tonic-gate 				uint_t tmp;
15520Sstevel@tonic-gate 
15530Sstevel@tonic-gate 				tmp = (cp->cp_edx >> (8 * i)) & 0xff;
15540Sstevel@tonic-gate 				if (tmp == 0x40)
15550Sstevel@tonic-gate 					celeron++;
15560Sstevel@tonic-gate 				else if (tmp >= 0x44 && tmp <= 0x45)
15570Sstevel@tonic-gate 					xeon++;
15580Sstevel@tonic-gate 			}
15590Sstevel@tonic-gate 
15600Sstevel@tonic-gate 			if (celeron)
15610Sstevel@tonic-gate 				return ("Intel Celeron(r)");
15620Sstevel@tonic-gate 			if (xeon)
15630Sstevel@tonic-gate 				return (cpi->cpi_model == 5 ?
15640Sstevel@tonic-gate 				    "Intel Pentium(r) II Xeon(tm)" :
15650Sstevel@tonic-gate 				    "Intel Pentium(r) III Xeon(tm)");
15660Sstevel@tonic-gate 			return (cpi->cpi_model == 5 ?
15670Sstevel@tonic-gate 			    "Intel Pentium(r) II or Pentium(r) II Xeon(tm)" :
15680Sstevel@tonic-gate 			    "Intel Pentium(r) III or Pentium(r) III Xeon(tm)");
15690Sstevel@tonic-gate 		default:
15700Sstevel@tonic-gate 			break;
15710Sstevel@tonic-gate 		}
15720Sstevel@tonic-gate 	default:
15730Sstevel@tonic-gate 		break;
15740Sstevel@tonic-gate 	}
15750Sstevel@tonic-gate 
15761975Sdmick 	/* BrandID is present if the field is nonzero */
15771975Sdmick 	if (cpi->cpi_brandid != 0) {
15780Sstevel@tonic-gate 		static const struct {
15790Sstevel@tonic-gate 			uint_t bt_bid;
15800Sstevel@tonic-gate 			const char *bt_str;
15810Sstevel@tonic-gate 		} brand_tbl[] = {
15820Sstevel@tonic-gate 			{ 0x1,	"Intel(r) Celeron(r)" },
15830Sstevel@tonic-gate 			{ 0x2,	"Intel(r) Pentium(r) III" },
15840Sstevel@tonic-gate 			{ 0x3,	"Intel(r) Pentium(r) III Xeon(tm)" },
15850Sstevel@tonic-gate 			{ 0x4,	"Intel(r) Pentium(r) III" },
15860Sstevel@tonic-gate 			{ 0x6,	"Mobile Intel(r) Pentium(r) III" },
15870Sstevel@tonic-gate 			{ 0x7,	"Mobile Intel(r) Celeron(r)" },
15880Sstevel@tonic-gate 			{ 0x8,	"Intel(r) Pentium(r) 4" },
15890Sstevel@tonic-gate 			{ 0x9,	"Intel(r) Pentium(r) 4" },
15900Sstevel@tonic-gate 			{ 0xa,	"Intel(r) Celeron(r)" },
15910Sstevel@tonic-gate 			{ 0xb,	"Intel(r) Xeon(tm)" },
15920Sstevel@tonic-gate 			{ 0xc,	"Intel(r) Xeon(tm) MP" },
15930Sstevel@tonic-gate 			{ 0xe,	"Mobile Intel(r) Pentium(r) 4" },
15941975Sdmick 			{ 0xf,	"Mobile Intel(r) Celeron(r)" },
15951975Sdmick 			{ 0x11, "Mobile Genuine Intel(r)" },
15961975Sdmick 			{ 0x12, "Intel(r) Celeron(r) M" },
15971975Sdmick 			{ 0x13, "Mobile Intel(r) Celeron(r)" },
15981975Sdmick 			{ 0x14, "Intel(r) Celeron(r)" },
15991975Sdmick 			{ 0x15, "Mobile Genuine Intel(r)" },
16001975Sdmick 			{ 0x16,	"Intel(r) Pentium(r) M" },
16011975Sdmick 			{ 0x17, "Mobile Intel(r) Celeron(r)" }
16020Sstevel@tonic-gate 		};
16030Sstevel@tonic-gate 		uint_t btblmax = sizeof (brand_tbl) / sizeof (brand_tbl[0]);
16040Sstevel@tonic-gate 		uint_t sgn;
16050Sstevel@tonic-gate 
16060Sstevel@tonic-gate 		sgn = (cpi->cpi_family << 8) |
16070Sstevel@tonic-gate 		    (cpi->cpi_model << 4) | cpi->cpi_step;
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate 		for (i = 0; i < btblmax; i++)
16100Sstevel@tonic-gate 			if (brand_tbl[i].bt_bid == cpi->cpi_brandid)
16110Sstevel@tonic-gate 				break;
16120Sstevel@tonic-gate 		if (i < btblmax) {
16130Sstevel@tonic-gate 			if (sgn == 0x6b1 && cpi->cpi_brandid == 3)
16140Sstevel@tonic-gate 				return ("Intel(r) Celeron(r)");
16150Sstevel@tonic-gate 			if (sgn < 0xf13 && cpi->cpi_brandid == 0xb)
16160Sstevel@tonic-gate 				return ("Intel(r) Xeon(tm) MP");
16170Sstevel@tonic-gate 			if (sgn < 0xf13 && cpi->cpi_brandid == 0xe)
16180Sstevel@tonic-gate 				return ("Intel(r) Xeon(tm)");
16190Sstevel@tonic-gate 			return (brand_tbl[i].bt_str);
16200Sstevel@tonic-gate 		}
16210Sstevel@tonic-gate 	}
16220Sstevel@tonic-gate 
16230Sstevel@tonic-gate 	return (NULL);
16240Sstevel@tonic-gate }
16250Sstevel@tonic-gate 
16260Sstevel@tonic-gate static const char *
16270Sstevel@tonic-gate amd_cpubrand(const struct cpuid_info *cpi)
16280Sstevel@tonic-gate {
16290Sstevel@tonic-gate 	if ((x86_feature & X86_CPUID) == 0 ||
16300Sstevel@tonic-gate 	    cpi->cpi_maxeax < 1 || cpi->cpi_family < 5)
16310Sstevel@tonic-gate 		return ("i486 compatible");
16320Sstevel@tonic-gate 
16330Sstevel@tonic-gate 	switch (cpi->cpi_family) {
16340Sstevel@tonic-gate 	case 5:
16350Sstevel@tonic-gate 		switch (cpi->cpi_model) {
16360Sstevel@tonic-gate 		case 0:
16370Sstevel@tonic-gate 		case 1:
16380Sstevel@tonic-gate 		case 2:
16390Sstevel@tonic-gate 		case 3:
16400Sstevel@tonic-gate 		case 4:
16410Sstevel@tonic-gate 		case 5:
16420Sstevel@tonic-gate 			return ("AMD-K5(r)");
16430Sstevel@tonic-gate 		case 6:
16440Sstevel@tonic-gate 		case 7:
16450Sstevel@tonic-gate 			return ("AMD-K6(r)");
16460Sstevel@tonic-gate 		case 8:
16470Sstevel@tonic-gate 			return ("AMD-K6(r)-2");
16480Sstevel@tonic-gate 		case 9:
16490Sstevel@tonic-gate 			return ("AMD-K6(r)-III");
16500Sstevel@tonic-gate 		default:
16510Sstevel@tonic-gate 			return ("AMD (family 5)");
16520Sstevel@tonic-gate 		}
16530Sstevel@tonic-gate 	case 6:
16540Sstevel@tonic-gate 		switch (cpi->cpi_model) {
16550Sstevel@tonic-gate 		case 1:
16560Sstevel@tonic-gate 			return ("AMD-K7(tm)");
16570Sstevel@tonic-gate 		case 0:
16580Sstevel@tonic-gate 		case 2:
16590Sstevel@tonic-gate 		case 4:
16600Sstevel@tonic-gate 			return ("AMD Athlon(tm)");
16610Sstevel@tonic-gate 		case 3:
16620Sstevel@tonic-gate 		case 7:
16630Sstevel@tonic-gate 			return ("AMD Duron(tm)");
16640Sstevel@tonic-gate 		case 6:
16650Sstevel@tonic-gate 		case 8:
16660Sstevel@tonic-gate 		case 10:
16670Sstevel@tonic-gate 			/*
16680Sstevel@tonic-gate 			 * Use the L2 cache size to distinguish
16690Sstevel@tonic-gate 			 */
16700Sstevel@tonic-gate 			return ((cpi->cpi_extd[6].cp_ecx >> 16) >= 256 ?
16710Sstevel@tonic-gate 			    "AMD Athlon(tm)" : "AMD Duron(tm)");
16720Sstevel@tonic-gate 		default:
16730Sstevel@tonic-gate 			return ("AMD (family 6)");
16740Sstevel@tonic-gate 		}
16750Sstevel@tonic-gate 	default:
16760Sstevel@tonic-gate 		break;
16770Sstevel@tonic-gate 	}
16780Sstevel@tonic-gate 
16790Sstevel@tonic-gate 	if (cpi->cpi_family == 0xf && cpi->cpi_model == 5 &&
16800Sstevel@tonic-gate 	    cpi->cpi_brandid != 0) {
16810Sstevel@tonic-gate 		switch (BITX(cpi->cpi_brandid, 7, 5)) {
16820Sstevel@tonic-gate 		case 3:
16830Sstevel@tonic-gate 			return ("AMD Opteron(tm) UP 1xx");
16840Sstevel@tonic-gate 		case 4:
16850Sstevel@tonic-gate 			return ("AMD Opteron(tm) DP 2xx");
16860Sstevel@tonic-gate 		case 5:
16870Sstevel@tonic-gate 			return ("AMD Opteron(tm) MP 8xx");
16880Sstevel@tonic-gate 		default:
16890Sstevel@tonic-gate 			return ("AMD Opteron(tm)");
16900Sstevel@tonic-gate 		}
16910Sstevel@tonic-gate 	}
16920Sstevel@tonic-gate 
16930Sstevel@tonic-gate 	return (NULL);
16940Sstevel@tonic-gate }
16950Sstevel@tonic-gate 
16960Sstevel@tonic-gate static const char *
16970Sstevel@tonic-gate cyrix_cpubrand(struct cpuid_info *cpi, uint_t type)
16980Sstevel@tonic-gate {
16990Sstevel@tonic-gate 	if ((x86_feature & X86_CPUID) == 0 ||
17000Sstevel@tonic-gate 	    cpi->cpi_maxeax < 1 || cpi->cpi_family < 5 ||
17010Sstevel@tonic-gate 	    type == X86_TYPE_CYRIX_486)
17020Sstevel@tonic-gate 		return ("i486 compatible");
17030Sstevel@tonic-gate 
17040Sstevel@tonic-gate 	switch (type) {
17050Sstevel@tonic-gate 	case X86_TYPE_CYRIX_6x86:
17060Sstevel@tonic-gate 		return ("Cyrix 6x86");
17070Sstevel@tonic-gate 	case X86_TYPE_CYRIX_6x86L:
17080Sstevel@tonic-gate 		return ("Cyrix 6x86L");
17090Sstevel@tonic-gate 	case X86_TYPE_CYRIX_6x86MX:
17100Sstevel@tonic-gate 		return ("Cyrix 6x86MX");
17110Sstevel@tonic-gate 	case X86_TYPE_CYRIX_GXm:
17120Sstevel@tonic-gate 		return ("Cyrix GXm");
17130Sstevel@tonic-gate 	case X86_TYPE_CYRIX_MediaGX:
17140Sstevel@tonic-gate 		return ("Cyrix MediaGX");
17150Sstevel@tonic-gate 	case X86_TYPE_CYRIX_MII:
17160Sstevel@tonic-gate 		return ("Cyrix M2");
17170Sstevel@tonic-gate 	case X86_TYPE_VIA_CYRIX_III:
17180Sstevel@tonic-gate 		return ("VIA Cyrix M3");
17190Sstevel@tonic-gate 	default:
17200Sstevel@tonic-gate 		/*
17210Sstevel@tonic-gate 		 * Have another wild guess ..
17220Sstevel@tonic-gate 		 */
17230Sstevel@tonic-gate 		if (cpi->cpi_family == 4 && cpi->cpi_model == 9)
17240Sstevel@tonic-gate 			return ("Cyrix 5x86");
17250Sstevel@tonic-gate 		else if (cpi->cpi_family == 5) {
17260Sstevel@tonic-gate 			switch (cpi->cpi_model) {
17270Sstevel@tonic-gate 			case 2:
17280Sstevel@tonic-gate 				return ("Cyrix 6x86");	/* Cyrix M1 */
17290Sstevel@tonic-gate 			case 4:
17300Sstevel@tonic-gate 				return ("Cyrix MediaGX");
17310Sstevel@tonic-gate 			default:
17320Sstevel@tonic-gate 				break;
17330Sstevel@tonic-gate 			}
17340Sstevel@tonic-gate 		} else if (cpi->cpi_family == 6) {
17350Sstevel@tonic-gate 			switch (cpi->cpi_model) {
17360Sstevel@tonic-gate 			case 0:
17370Sstevel@tonic-gate 				return ("Cyrix 6x86MX"); /* Cyrix M2? */
17380Sstevel@tonic-gate 			case 5:
17390Sstevel@tonic-gate 			case 6:
17400Sstevel@tonic-gate 			case 7:
17410Sstevel@tonic-gate 			case 8:
17420Sstevel@tonic-gate 			case 9:
17430Sstevel@tonic-gate 				return ("VIA C3");
17440Sstevel@tonic-gate 			default:
17450Sstevel@tonic-gate 				break;
17460Sstevel@tonic-gate 			}
17470Sstevel@tonic-gate 		}
17480Sstevel@tonic-gate 		break;
17490Sstevel@tonic-gate 	}
17500Sstevel@tonic-gate 	return (NULL);
17510Sstevel@tonic-gate }
17520Sstevel@tonic-gate 
17530Sstevel@tonic-gate /*
17540Sstevel@tonic-gate  * This only gets called in the case that the CPU extended
17550Sstevel@tonic-gate  * feature brand string (0x80000002, 0x80000003, 0x80000004)
17560Sstevel@tonic-gate  * aren't available, or contain null bytes for some reason.
17570Sstevel@tonic-gate  */
17580Sstevel@tonic-gate static void
17590Sstevel@tonic-gate fabricate_brandstr(struct cpuid_info *cpi)
17600Sstevel@tonic-gate {
17610Sstevel@tonic-gate 	const char *brand = NULL;
17620Sstevel@tonic-gate 
17630Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
17640Sstevel@tonic-gate 	case X86_VENDOR_Intel:
17650Sstevel@tonic-gate 		brand = intel_cpubrand(cpi);
17660Sstevel@tonic-gate 		break;
17670Sstevel@tonic-gate 	case X86_VENDOR_AMD:
17680Sstevel@tonic-gate 		brand = amd_cpubrand(cpi);
17690Sstevel@tonic-gate 		break;
17700Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
17710Sstevel@tonic-gate 		brand = cyrix_cpubrand(cpi, x86_type);
17720Sstevel@tonic-gate 		break;
17730Sstevel@tonic-gate 	case X86_VENDOR_NexGen:
17740Sstevel@tonic-gate 		if (cpi->cpi_family == 5 && cpi->cpi_model == 0)
17750Sstevel@tonic-gate 			brand = "NexGen Nx586";
17760Sstevel@tonic-gate 		break;
17770Sstevel@tonic-gate 	case X86_VENDOR_Centaur:
17780Sstevel@tonic-gate 		if (cpi->cpi_family == 5)
17790Sstevel@tonic-gate 			switch (cpi->cpi_model) {
17800Sstevel@tonic-gate 			case 4:
17810Sstevel@tonic-gate 				brand = "Centaur C6";
17820Sstevel@tonic-gate 				break;
17830Sstevel@tonic-gate 			case 8:
17840Sstevel@tonic-gate 				brand = "Centaur C2";
17850Sstevel@tonic-gate 				break;
17860Sstevel@tonic-gate 			case 9:
17870Sstevel@tonic-gate 				brand = "Centaur C3";
17880Sstevel@tonic-gate 				break;
17890Sstevel@tonic-gate 			default:
17900Sstevel@tonic-gate 				break;
17910Sstevel@tonic-gate 			}
17920Sstevel@tonic-gate 		break;
17930Sstevel@tonic-gate 	case X86_VENDOR_Rise:
17940Sstevel@tonic-gate 		if (cpi->cpi_family == 5 &&
17950Sstevel@tonic-gate 		    (cpi->cpi_model == 0 || cpi->cpi_model == 2))
17960Sstevel@tonic-gate 			brand = "Rise mP6";
17970Sstevel@tonic-gate 		break;
17980Sstevel@tonic-gate 	case X86_VENDOR_SiS:
17990Sstevel@tonic-gate 		if (cpi->cpi_family == 5 && cpi->cpi_model == 0)
18000Sstevel@tonic-gate 			brand = "SiS 55x";
18010Sstevel@tonic-gate 		break;
18020Sstevel@tonic-gate 	case X86_VENDOR_TM:
18030Sstevel@tonic-gate 		if (cpi->cpi_family == 5 && cpi->cpi_model == 4)
18040Sstevel@tonic-gate 			brand = "Transmeta Crusoe TM3x00 or TM5x00";
18050Sstevel@tonic-gate 		break;
18060Sstevel@tonic-gate 	case X86_VENDOR_NSC:
18070Sstevel@tonic-gate 	case X86_VENDOR_UMC:
18080Sstevel@tonic-gate 	default:
18090Sstevel@tonic-gate 		break;
18100Sstevel@tonic-gate 	}
18110Sstevel@tonic-gate 	if (brand) {
18120Sstevel@tonic-gate 		(void) strcpy((char *)cpi->cpi_brandstr, brand);
18130Sstevel@tonic-gate 		return;
18140Sstevel@tonic-gate 	}
18150Sstevel@tonic-gate 
18160Sstevel@tonic-gate 	/*
18170Sstevel@tonic-gate 	 * If all else fails ...
18180Sstevel@tonic-gate 	 */
18190Sstevel@tonic-gate 	(void) snprintf(cpi->cpi_brandstr, sizeof (cpi->cpi_brandstr),
18200Sstevel@tonic-gate 	    "%s %d.%d.%d", cpi->cpi_vendorstr, cpi->cpi_family,
18210Sstevel@tonic-gate 	    cpi->cpi_model, cpi->cpi_step);
18220Sstevel@tonic-gate }
18230Sstevel@tonic-gate 
18240Sstevel@tonic-gate /*
18250Sstevel@tonic-gate  * This routine is called just after kernel memory allocation
18260Sstevel@tonic-gate  * becomes available on cpu0, and as part of mp_startup() on
18270Sstevel@tonic-gate  * the other cpus.
18280Sstevel@tonic-gate  *
18294606Sesaxe  * Fixup the brand string, and collect any information from cpuid
18304606Sesaxe  * that requires dynamicically allocated storage to represent.
18310Sstevel@tonic-gate  */
18320Sstevel@tonic-gate /*ARGSUSED*/
18330Sstevel@tonic-gate void
18340Sstevel@tonic-gate cpuid_pass3(cpu_t *cpu)
18350Sstevel@tonic-gate {
18364606Sesaxe 	int	i, max, shft, level, size;
18374606Sesaxe 	struct cpuid_regs regs;
18384606Sesaxe 	struct cpuid_regs *cp;
18390Sstevel@tonic-gate 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
18400Sstevel@tonic-gate 
18410Sstevel@tonic-gate 	ASSERT(cpi->cpi_pass == 2);
18420Sstevel@tonic-gate 
18434606Sesaxe 	/*
18444606Sesaxe 	 * Function 4: Deterministic cache parameters
18454606Sesaxe 	 *
18464606Sesaxe 	 * Take this opportunity to detect the number of threads
18474606Sesaxe 	 * sharing the last level cache, and construct a corresponding
18484606Sesaxe 	 * cache id. The respective cpuid_info members are initialized
18494606Sesaxe 	 * to the default case of "no last level cache sharing".
18504606Sesaxe 	 */
18514606Sesaxe 	cpi->cpi_ncpu_shr_last_cache = 1;
18524606Sesaxe 	cpi->cpi_last_lvl_cacheid = cpu->cpu_id;
18534606Sesaxe 
18544606Sesaxe 	if (cpi->cpi_maxeax >= 4 && cpi->cpi_vendor == X86_VENDOR_Intel) {
18554606Sesaxe 
18564606Sesaxe 		/*
18574606Sesaxe 		 * Find the # of elements (size) returned by fn 4, and along
18584606Sesaxe 		 * the way detect last level cache sharing details.
18594606Sesaxe 		 */
18604606Sesaxe 		bzero(&regs, sizeof (regs));
18614606Sesaxe 		cp = &regs;
18624606Sesaxe 		for (i = 0, max = 0; i < CPI_FN4_ECX_MAX; i++) {
18634606Sesaxe 			cp->cp_eax = 4;
18644606Sesaxe 			cp->cp_ecx = i;
18654606Sesaxe 
18664606Sesaxe 			(void) __cpuid_insn(cp);
18674606Sesaxe 
18684606Sesaxe 			if (CPI_CACHE_TYPE(cp) == 0)
18694606Sesaxe 				break;
18704606Sesaxe 			level = CPI_CACHE_LVL(cp);
18714606Sesaxe 			if (level > max) {
18724606Sesaxe 				max = level;
18734606Sesaxe 				cpi->cpi_ncpu_shr_last_cache =
18744606Sesaxe 				    CPI_NTHR_SHR_CACHE(cp) + 1;
18754606Sesaxe 			}
18764606Sesaxe 		}
18774606Sesaxe 		cpi->cpi_std_4_size = size = i;
18784606Sesaxe 
18794606Sesaxe 		/*
18804606Sesaxe 		 * Allocate the cpi_std_4 array. The first element
18814606Sesaxe 		 * references the regs for fn 4, %ecx == 0, which
18824606Sesaxe 		 * cpuid_pass2() stashed in cpi->cpi_std[4].
18834606Sesaxe 		 */
18844606Sesaxe 		if (size > 0) {
18854606Sesaxe 			cpi->cpi_std_4 =
18864606Sesaxe 			    kmem_alloc(size * sizeof (cp), KM_SLEEP);
18874606Sesaxe 			cpi->cpi_std_4[0] = &cpi->cpi_std[4];
18884606Sesaxe 
18894606Sesaxe 			/*
18904606Sesaxe 			 * Allocate storage to hold the additional regs
18914606Sesaxe 			 * for function 4, %ecx == 1 .. cpi_std_4_size.
18924606Sesaxe 			 *
18934606Sesaxe 			 * The regs for fn 4, %ecx == 0 has already
18944606Sesaxe 			 * been allocated as indicated above.
18954606Sesaxe 			 */
18964606Sesaxe 			for (i = 1; i < size; i++) {
18974606Sesaxe 				cp = cpi->cpi_std_4[i] =
18984606Sesaxe 				    kmem_zalloc(sizeof (regs), KM_SLEEP);
18994606Sesaxe 				cp->cp_eax = 4;
19004606Sesaxe 				cp->cp_ecx = i;
19014606Sesaxe 
19024606Sesaxe 				(void) __cpuid_insn(cp);
19034606Sesaxe 			}
19044606Sesaxe 		}
19054606Sesaxe 		/*
19064606Sesaxe 		 * Determine the number of bits needed to represent
19074606Sesaxe 		 * the number of CPUs sharing the last level cache.
19084606Sesaxe 		 *
19094606Sesaxe 		 * Shift off that number of bits from the APIC id to
19104606Sesaxe 		 * derive the cache id.
19114606Sesaxe 		 */
19124606Sesaxe 		shft = 0;
19134606Sesaxe 		for (i = 1; i < cpi->cpi_ncpu_shr_last_cache; i <<= 1)
19144606Sesaxe 			shft++;
19154606Sesaxe 		cpi->cpi_last_lvl_cacheid = CPI_APIC_ID(cpi) >> shft;
19160Sstevel@tonic-gate 	}
19170Sstevel@tonic-gate 
19180Sstevel@tonic-gate 	/*
19194606Sesaxe 	 * Now fixup the brand string
19200Sstevel@tonic-gate 	 */
19214606Sesaxe 	if ((cpi->cpi_xmaxeax & 0x80000000) == 0) {
19224606Sesaxe 		fabricate_brandstr(cpi);
19234606Sesaxe 	} else {
19240Sstevel@tonic-gate 
19250Sstevel@tonic-gate 		/*
19264606Sesaxe 		 * If we successfully extracted a brand string from the cpuid
19274606Sesaxe 		 * instruction, clean it up by removing leading spaces and
19284606Sesaxe 		 * similar junk.
19290Sstevel@tonic-gate 		 */
19304606Sesaxe 		if (cpi->cpi_brandstr[0]) {
19314606Sesaxe 			size_t maxlen = sizeof (cpi->cpi_brandstr);
19324606Sesaxe 			char *src, *dst;
19334606Sesaxe 
19344606Sesaxe 			dst = src = (char *)cpi->cpi_brandstr;
19354606Sesaxe 			src[maxlen - 1] = '\0';
19364606Sesaxe 			/*
19374606Sesaxe 			 * strip leading spaces
19384606Sesaxe 			 */
19394606Sesaxe 			while (*src == ' ')
19404606Sesaxe 				src++;
19414606Sesaxe 			/*
19424606Sesaxe 			 * Remove any 'Genuine' or "Authentic" prefixes
19434606Sesaxe 			 */
19444606Sesaxe 			if (strncmp(src, "Genuine ", 8) == 0)
19454606Sesaxe 				src += 8;
19464606Sesaxe 			if (strncmp(src, "Authentic ", 10) == 0)
19474606Sesaxe 				src += 10;
19484606Sesaxe 
19494606Sesaxe 			/*
19504606Sesaxe 			 * Now do an in-place copy.
19514606Sesaxe 			 * Map (R) to (r) and (TM) to (tm).
19524606Sesaxe 			 * The era of teletypes is long gone, and there's
19534606Sesaxe 			 * -really- no need to shout.
19544606Sesaxe 			 */
19554606Sesaxe 			while (*src != '\0') {
19564606Sesaxe 				if (src[0] == '(') {
19574606Sesaxe 					if (strncmp(src + 1, "R)", 2) == 0) {
19584606Sesaxe 						(void) strncpy(dst, "(r)", 3);
19594606Sesaxe 						src += 3;
19604606Sesaxe 						dst += 3;
19614606Sesaxe 						continue;
19624606Sesaxe 					}
19634606Sesaxe 					if (strncmp(src + 1, "TM)", 3) == 0) {
19644606Sesaxe 						(void) strncpy(dst, "(tm)", 4);
19654606Sesaxe 						src += 4;
19664606Sesaxe 						dst += 4;
19674606Sesaxe 						continue;
19684606Sesaxe 					}
19690Sstevel@tonic-gate 				}
19704606Sesaxe 				*dst++ = *src++;
19710Sstevel@tonic-gate 			}
19724606Sesaxe 			*dst = '\0';
19734606Sesaxe 
19744606Sesaxe 			/*
19754606Sesaxe 			 * Finally, remove any trailing spaces
19764606Sesaxe 			 */
19774606Sesaxe 			while (--dst > cpi->cpi_brandstr)
19784606Sesaxe 				if (*dst == ' ')
19794606Sesaxe 					*dst = '\0';
19804606Sesaxe 				else
19814606Sesaxe 					break;
19824606Sesaxe 		} else
19834606Sesaxe 			fabricate_brandstr(cpi);
19844606Sesaxe 	}
19850Sstevel@tonic-gate 	cpi->cpi_pass = 3;
19860Sstevel@tonic-gate }
19870Sstevel@tonic-gate 
19880Sstevel@tonic-gate /*
19890Sstevel@tonic-gate  * This routine is called out of bind_hwcap() much later in the life
19900Sstevel@tonic-gate  * of the kernel (post_startup()).  The job of this routine is to resolve
19910Sstevel@tonic-gate  * the hardware feature support and kernel support for those features into
19920Sstevel@tonic-gate  * what we're actually going to tell applications via the aux vector.
19930Sstevel@tonic-gate  */
19940Sstevel@tonic-gate uint_t
19950Sstevel@tonic-gate cpuid_pass4(cpu_t *cpu)
19960Sstevel@tonic-gate {
19970Sstevel@tonic-gate 	struct cpuid_info *cpi;
19980Sstevel@tonic-gate 	uint_t hwcap_flags = 0;
19990Sstevel@tonic-gate 
20000Sstevel@tonic-gate 	if (cpu == NULL)
20010Sstevel@tonic-gate 		cpu = CPU;
20020Sstevel@tonic-gate 	cpi = cpu->cpu_m.mcpu_cpi;
20030Sstevel@tonic-gate 
20040Sstevel@tonic-gate 	ASSERT(cpi->cpi_pass == 3);
20050Sstevel@tonic-gate 
20060Sstevel@tonic-gate 	if (cpi->cpi_maxeax >= 1) {
20070Sstevel@tonic-gate 		uint32_t *edx = &cpi->cpi_support[STD_EDX_FEATURES];
20080Sstevel@tonic-gate 		uint32_t *ecx = &cpi->cpi_support[STD_ECX_FEATURES];
20090Sstevel@tonic-gate 
20100Sstevel@tonic-gate 		*edx = CPI_FEATURES_EDX(cpi);
20110Sstevel@tonic-gate 		*ecx = CPI_FEATURES_ECX(cpi);
20120Sstevel@tonic-gate 
20130Sstevel@tonic-gate 		/*
20140Sstevel@tonic-gate 		 * [these require explicit kernel support]
20150Sstevel@tonic-gate 		 */
20160Sstevel@tonic-gate 		if ((x86_feature & X86_SEP) == 0)
20170Sstevel@tonic-gate 			*edx &= ~CPUID_INTC_EDX_SEP;
20180Sstevel@tonic-gate 
20190Sstevel@tonic-gate 		if ((x86_feature & X86_SSE) == 0)
20200Sstevel@tonic-gate 			*edx &= ~(CPUID_INTC_EDX_FXSR|CPUID_INTC_EDX_SSE);
20210Sstevel@tonic-gate 		if ((x86_feature & X86_SSE2) == 0)
20220Sstevel@tonic-gate 			*edx &= ~CPUID_INTC_EDX_SSE2;
20230Sstevel@tonic-gate 
20240Sstevel@tonic-gate 		if ((x86_feature & X86_HTT) == 0)
20250Sstevel@tonic-gate 			*edx &= ~CPUID_INTC_EDX_HTT;
20260Sstevel@tonic-gate 
20270Sstevel@tonic-gate 		if ((x86_feature & X86_SSE3) == 0)
20280Sstevel@tonic-gate 			*ecx &= ~CPUID_INTC_ECX_SSE3;
20290Sstevel@tonic-gate 
20305269Skk208521 		if (cpi->cpi_vendor == X86_VENDOR_Intel) {
20315269Skk208521 			if ((x86_feature & X86_SSSE3) == 0)
20325269Skk208521 				*ecx &= ~CPUID_INTC_ECX_SSSE3;
20335269Skk208521 			if ((x86_feature & X86_SSE4_1) == 0)
20345269Skk208521 				*ecx &= ~CPUID_INTC_ECX_SSE4_1;
20355269Skk208521 			if ((x86_feature & X86_SSE4_2) == 0)
20365269Skk208521 				*ecx &= ~CPUID_INTC_ECX_SSE4_2;
20375269Skk208521 		}
20385269Skk208521 
20390Sstevel@tonic-gate 		/*
20400Sstevel@tonic-gate 		 * [no explicit support required beyond x87 fp context]
20410Sstevel@tonic-gate 		 */
20420Sstevel@tonic-gate 		if (!fpu_exists)
20430Sstevel@tonic-gate 			*edx &= ~(CPUID_INTC_EDX_FPU | CPUID_INTC_EDX_MMX);
20440Sstevel@tonic-gate 
20450Sstevel@tonic-gate 		/*
20460Sstevel@tonic-gate 		 * Now map the supported feature vector to things that we
20470Sstevel@tonic-gate 		 * think userland will care about.
20480Sstevel@tonic-gate 		 */
20490Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_SEP)
20500Sstevel@tonic-gate 			hwcap_flags |= AV_386_SEP;
20510Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_SSE)
20520Sstevel@tonic-gate 			hwcap_flags |= AV_386_FXSR | AV_386_SSE;
20530Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_SSE2)
20540Sstevel@tonic-gate 			hwcap_flags |= AV_386_SSE2;
20550Sstevel@tonic-gate 		if (*ecx & CPUID_INTC_ECX_SSE3)
20560Sstevel@tonic-gate 			hwcap_flags |= AV_386_SSE3;
20575269Skk208521 		if (cpi->cpi_vendor == X86_VENDOR_Intel) {
20585269Skk208521 			if (*ecx & CPUID_INTC_ECX_SSSE3)
20595269Skk208521 				hwcap_flags |= AV_386_SSSE3;
20605269Skk208521 			if (*ecx & CPUID_INTC_ECX_SSE4_1)
20615269Skk208521 				hwcap_flags |= AV_386_SSE4_1;
20625269Skk208521 			if (*ecx & CPUID_INTC_ECX_SSE4_2)
20635269Skk208521 				hwcap_flags |= AV_386_SSE4_2;
20645269Skk208521 		}
20654628Skk208521 		if (*ecx & CPUID_INTC_ECX_POPCNT)
20664628Skk208521 			hwcap_flags |= AV_386_POPCNT;
20670Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_FPU)
20680Sstevel@tonic-gate 			hwcap_flags |= AV_386_FPU;
20690Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_MMX)
20700Sstevel@tonic-gate 			hwcap_flags |= AV_386_MMX;
20710Sstevel@tonic-gate 
20720Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_TSC)
20730Sstevel@tonic-gate 			hwcap_flags |= AV_386_TSC;
20740Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_CX8)
20750Sstevel@tonic-gate 			hwcap_flags |= AV_386_CX8;
20760Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_CMOV)
20770Sstevel@tonic-gate 			hwcap_flags |= AV_386_CMOV;
20780Sstevel@tonic-gate 		if (*ecx & CPUID_INTC_ECX_MON)
20790Sstevel@tonic-gate 			hwcap_flags |= AV_386_MON;
20800Sstevel@tonic-gate 		if (*ecx & CPUID_INTC_ECX_CX16)
20810Sstevel@tonic-gate 			hwcap_flags |= AV_386_CX16;
20820Sstevel@tonic-gate 	}
20830Sstevel@tonic-gate 
20841228Sandrei 	if (x86_feature & X86_HTT)
20850Sstevel@tonic-gate 		hwcap_flags |= AV_386_PAUSE;
20860Sstevel@tonic-gate 
20870Sstevel@tonic-gate 	if (cpi->cpi_xmaxeax < 0x80000001)
20880Sstevel@tonic-gate 		goto pass4_done;
20890Sstevel@tonic-gate 
20900Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
20911228Sandrei 		struct cpuid_regs cp;
20923446Smrj 		uint32_t *edx, *ecx;
20930Sstevel@tonic-gate 
20943446Smrj 	case X86_VENDOR_Intel:
20953446Smrj 		/*
20963446Smrj 		 * Seems like Intel duplicated what we necessary
20973446Smrj 		 * here to make the initial crop of 64-bit OS's work.
20983446Smrj 		 * Hopefully, those are the only "extended" bits
20993446Smrj 		 * they'll add.
21003446Smrj 		 */
21013446Smrj 		/*FALLTHROUGH*/
21023446Smrj 
21030Sstevel@tonic-gate 	case X86_VENDOR_AMD:
21040Sstevel@tonic-gate 		edx = &cpi->cpi_support[AMD_EDX_FEATURES];
21053446Smrj 		ecx = &cpi->cpi_support[AMD_ECX_FEATURES];
21060Sstevel@tonic-gate 
21070Sstevel@tonic-gate 		*edx = CPI_FEATURES_XTD_EDX(cpi);
21083446Smrj 		*ecx = CPI_FEATURES_XTD_ECX(cpi);
21093446Smrj 
21103446Smrj 		/*
21113446Smrj 		 * [these features require explicit kernel support]
21123446Smrj 		 */
21133446Smrj 		switch (cpi->cpi_vendor) {
21143446Smrj 		case X86_VENDOR_Intel:
21153446Smrj 			break;
21163446Smrj 
21173446Smrj 		case X86_VENDOR_AMD:
21183446Smrj 			if ((x86_feature & X86_TSCP) == 0)
21193446Smrj 				*edx &= ~CPUID_AMD_EDX_TSCP;
21204628Skk208521 			if ((x86_feature & X86_SSE4A) == 0)
21214628Skk208521 				*ecx &= ~CPUID_AMD_ECX_SSE4A;
21223446Smrj 			break;
21233446Smrj 
21243446Smrj 		default:
21253446Smrj 			break;
21263446Smrj 		}
21270Sstevel@tonic-gate 
21280Sstevel@tonic-gate 		/*
21290Sstevel@tonic-gate 		 * [no explicit support required beyond
21300Sstevel@tonic-gate 		 * x87 fp context and exception handlers]
21310Sstevel@tonic-gate 		 */
21320Sstevel@tonic-gate 		if (!fpu_exists)
21330Sstevel@tonic-gate 			*edx &= ~(CPUID_AMD_EDX_MMXamd |
21340Sstevel@tonic-gate 			    CPUID_AMD_EDX_3DNow | CPUID_AMD_EDX_3DNowx);
21350Sstevel@tonic-gate 
21360Sstevel@tonic-gate 		if ((x86_feature & X86_NX) == 0)
21370Sstevel@tonic-gate 			*edx &= ~CPUID_AMD_EDX_NX;
21383446Smrj #if !defined(__amd64)
21390Sstevel@tonic-gate 		*edx &= ~CPUID_AMD_EDX_LM;
21400Sstevel@tonic-gate #endif
21410Sstevel@tonic-gate 		/*
21420Sstevel@tonic-gate 		 * Now map the supported feature vector to
21430Sstevel@tonic-gate 		 * things that we think userland will care about.
21440Sstevel@tonic-gate 		 */
21453446Smrj #if defined(__amd64)
21460Sstevel@tonic-gate 		if (*edx & CPUID_AMD_EDX_SYSC)
21470Sstevel@tonic-gate 			hwcap_flags |= AV_386_AMD_SYSC;
21483446Smrj #endif
21490Sstevel@tonic-gate 		if (*edx & CPUID_AMD_EDX_MMXamd)
21500Sstevel@tonic-gate 			hwcap_flags |= AV_386_AMD_MMX;
21510Sstevel@tonic-gate 		if (*edx & CPUID_AMD_EDX_3DNow)
21520Sstevel@tonic-gate 			hwcap_flags |= AV_386_AMD_3DNow;
21530Sstevel@tonic-gate 		if (*edx & CPUID_AMD_EDX_3DNowx)
21540Sstevel@tonic-gate 			hwcap_flags |= AV_386_AMD_3DNowx;
21553446Smrj 
21563446Smrj 		switch (cpi->cpi_vendor) {
21573446Smrj 		case X86_VENDOR_AMD:
21583446Smrj 			if (*edx & CPUID_AMD_EDX_TSCP)
21593446Smrj 				hwcap_flags |= AV_386_TSCP;
21603446Smrj 			if (*ecx & CPUID_AMD_ECX_AHF64)
21613446Smrj 				hwcap_flags |= AV_386_AHF;
21624628Skk208521 			if (*ecx & CPUID_AMD_ECX_SSE4A)
21634628Skk208521 				hwcap_flags |= AV_386_AMD_SSE4A;
21644628Skk208521 			if (*ecx & CPUID_AMD_ECX_LZCNT)
21654628Skk208521 				hwcap_flags |= AV_386_AMD_LZCNT;
21663446Smrj 			break;
21673446Smrj 
21683446Smrj 		case X86_VENDOR_Intel:
21693446Smrj 			/*
21703446Smrj 			 * Aarrgh.
21713446Smrj 			 * Intel uses a different bit in the same word.
21723446Smrj 			 */
21733446Smrj 			if (*ecx & CPUID_INTC_ECX_AHF64)
21743446Smrj 				hwcap_flags |= AV_386_AHF;
21753446Smrj 			break;
21763446Smrj 
21773446Smrj 		default:
21783446Smrj 			break;
21793446Smrj 		}
21800Sstevel@tonic-gate 		break;
21810Sstevel@tonic-gate 
21820Sstevel@tonic-gate 	case X86_VENDOR_TM:
21831228Sandrei 		cp.cp_eax = 0x80860001;
21841228Sandrei 		(void) __cpuid_insn(&cp);
21851228Sandrei 		cpi->cpi_support[TM_EDX_FEATURES] = cp.cp_edx;
21860Sstevel@tonic-gate 		break;
21870Sstevel@tonic-gate 
21880Sstevel@tonic-gate 	default:
21890Sstevel@tonic-gate 		break;
21900Sstevel@tonic-gate 	}
21910Sstevel@tonic-gate 
21920Sstevel@tonic-gate pass4_done:
21930Sstevel@tonic-gate 	cpi->cpi_pass = 4;
21940Sstevel@tonic-gate 	return (hwcap_flags);
21950Sstevel@tonic-gate }
21960Sstevel@tonic-gate 
21970Sstevel@tonic-gate 
21980Sstevel@tonic-gate /*
21990Sstevel@tonic-gate  * Simulate the cpuid instruction using the data we previously
22000Sstevel@tonic-gate  * captured about this CPU.  We try our best to return the truth
22010Sstevel@tonic-gate  * about the hardware, independently of kernel support.
22020Sstevel@tonic-gate  */
22030Sstevel@tonic-gate uint32_t
22041228Sandrei cpuid_insn(cpu_t *cpu, struct cpuid_regs *cp)
22050Sstevel@tonic-gate {
22060Sstevel@tonic-gate 	struct cpuid_info *cpi;
22071228Sandrei 	struct cpuid_regs *xcp;
22080Sstevel@tonic-gate 
22090Sstevel@tonic-gate 	if (cpu == NULL)
22100Sstevel@tonic-gate 		cpu = CPU;
22110Sstevel@tonic-gate 	cpi = cpu->cpu_m.mcpu_cpi;
22120Sstevel@tonic-gate 
22130Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 3));
22140Sstevel@tonic-gate 
22150Sstevel@tonic-gate 	/*
22160Sstevel@tonic-gate 	 * CPUID data is cached in two separate places: cpi_std for standard
22170Sstevel@tonic-gate 	 * CPUID functions, and cpi_extd for extended CPUID functions.
22180Sstevel@tonic-gate 	 */
22191228Sandrei 	if (cp->cp_eax <= cpi->cpi_maxeax && cp->cp_eax < NMAX_CPI_STD)
22201228Sandrei 		xcp = &cpi->cpi_std[cp->cp_eax];
22211228Sandrei 	else if (cp->cp_eax >= 0x80000000 && cp->cp_eax <= cpi->cpi_xmaxeax &&
22221228Sandrei 	    cp->cp_eax < 0x80000000 + NMAX_CPI_EXTD)
22231228Sandrei 		xcp = &cpi->cpi_extd[cp->cp_eax - 0x80000000];
22240Sstevel@tonic-gate 	else
22250Sstevel@tonic-gate 		/*
22260Sstevel@tonic-gate 		 * The caller is asking for data from an input parameter which
22270Sstevel@tonic-gate 		 * the kernel has not cached.  In this case we go fetch from
22280Sstevel@tonic-gate 		 * the hardware and return the data directly to the user.
22290Sstevel@tonic-gate 		 */
22301228Sandrei 		return (__cpuid_insn(cp));
22311228Sandrei 
22321228Sandrei 	cp->cp_eax = xcp->cp_eax;
22331228Sandrei 	cp->cp_ebx = xcp->cp_ebx;
22341228Sandrei 	cp->cp_ecx = xcp->cp_ecx;
22351228Sandrei 	cp->cp_edx = xcp->cp_edx;
22360Sstevel@tonic-gate 	return (cp->cp_eax);
22370Sstevel@tonic-gate }
22380Sstevel@tonic-gate 
22390Sstevel@tonic-gate int
22400Sstevel@tonic-gate cpuid_checkpass(cpu_t *cpu, int pass)
22410Sstevel@tonic-gate {
22420Sstevel@tonic-gate 	return (cpu != NULL && cpu->cpu_m.mcpu_cpi != NULL &&
22430Sstevel@tonic-gate 	    cpu->cpu_m.mcpu_cpi->cpi_pass >= pass);
22440Sstevel@tonic-gate }
22450Sstevel@tonic-gate 
22460Sstevel@tonic-gate int
22470Sstevel@tonic-gate cpuid_getbrandstr(cpu_t *cpu, char *s, size_t n)
22480Sstevel@tonic-gate {
22490Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 3));
22500Sstevel@tonic-gate 
22510Sstevel@tonic-gate 	return (snprintf(s, n, "%s", cpu->cpu_m.mcpu_cpi->cpi_brandstr));
22520Sstevel@tonic-gate }
22530Sstevel@tonic-gate 
22540Sstevel@tonic-gate int
22551228Sandrei cpuid_is_cmt(cpu_t *cpu)
22560Sstevel@tonic-gate {
22570Sstevel@tonic-gate 	if (cpu == NULL)
22580Sstevel@tonic-gate 		cpu = CPU;
22590Sstevel@tonic-gate 
22600Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
22610Sstevel@tonic-gate 
22620Sstevel@tonic-gate 	return (cpu->cpu_m.mcpu_cpi->cpi_chipid >= 0);
22630Sstevel@tonic-gate }
22640Sstevel@tonic-gate 
22650Sstevel@tonic-gate /*
22660Sstevel@tonic-gate  * AMD and Intel both implement the 64-bit variant of the syscall
22670Sstevel@tonic-gate  * instruction (syscallq), so if there's -any- support for syscall,
22680Sstevel@tonic-gate  * cpuid currently says "yes, we support this".
22690Sstevel@tonic-gate  *
22700Sstevel@tonic-gate  * However, Intel decided to -not- implement the 32-bit variant of the
22710Sstevel@tonic-gate  * syscall instruction, so we provide a predicate to allow our caller
22720Sstevel@tonic-gate  * to test that subtlety here.
22735084Sjohnlev  *
22745084Sjohnlev  * XXPV	Currently, 32-bit syscall instructions don't work via the hypervisor,
22755084Sjohnlev  *	even in the case where the hardware would in fact support it.
22760Sstevel@tonic-gate  */
22770Sstevel@tonic-gate /*ARGSUSED*/
22780Sstevel@tonic-gate int
22790Sstevel@tonic-gate cpuid_syscall32_insn(cpu_t *cpu)
22800Sstevel@tonic-gate {
22810Sstevel@tonic-gate 	ASSERT(cpuid_checkpass((cpu == NULL ? CPU : cpu), 1));
22820Sstevel@tonic-gate 
22835084Sjohnlev #if !defined(__xpv)
22843446Smrj 	if (cpu == NULL)
22853446Smrj 		cpu = CPU;
22863446Smrj 
22873446Smrj 	/*CSTYLED*/
22883446Smrj 	{
22893446Smrj 		struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
22903446Smrj 
22913446Smrj 		if (cpi->cpi_vendor == X86_VENDOR_AMD &&
22923446Smrj 		    cpi->cpi_xmaxeax >= 0x80000001 &&
22933446Smrj 		    (CPI_FEATURES_XTD_EDX(cpi) & CPUID_AMD_EDX_SYSC))
22943446Smrj 			return (1);
22953446Smrj 	}
22965084Sjohnlev #endif
22970Sstevel@tonic-gate 	return (0);
22980Sstevel@tonic-gate }
22990Sstevel@tonic-gate 
23000Sstevel@tonic-gate int
23010Sstevel@tonic-gate cpuid_getidstr(cpu_t *cpu, char *s, size_t n)
23020Sstevel@tonic-gate {
23030Sstevel@tonic-gate 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
23040Sstevel@tonic-gate 
23050Sstevel@tonic-gate 	static const char fmt[] =
23063779Sdmick 	    "x86 (%s %X family %d model %d step %d clock %d MHz)";
23070Sstevel@tonic-gate 	static const char fmt_ht[] =
23083779Sdmick 	    "x86 (chipid 0x%x %s %X family %d model %d step %d clock %d MHz)";
23090Sstevel@tonic-gate 
23100Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
23110Sstevel@tonic-gate 
23121228Sandrei 	if (cpuid_is_cmt(cpu))
23130Sstevel@tonic-gate 		return (snprintf(s, n, fmt_ht, cpi->cpi_chipid,
23143779Sdmick 		    cpi->cpi_vendorstr, cpi->cpi_std[1].cp_eax,
23153779Sdmick 		    cpi->cpi_family, cpi->cpi_model,
23160Sstevel@tonic-gate 		    cpi->cpi_step, cpu->cpu_type_info.pi_clock));
23170Sstevel@tonic-gate 	return (snprintf(s, n, fmt,
23183779Sdmick 	    cpi->cpi_vendorstr, cpi->cpi_std[1].cp_eax,
23193779Sdmick 	    cpi->cpi_family, cpi->cpi_model,
23200Sstevel@tonic-gate 	    cpi->cpi_step, cpu->cpu_type_info.pi_clock));
23210Sstevel@tonic-gate }
23220Sstevel@tonic-gate 
23230Sstevel@tonic-gate const char *
23240Sstevel@tonic-gate cpuid_getvendorstr(cpu_t *cpu)
23250Sstevel@tonic-gate {
23260Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
23270Sstevel@tonic-gate 	return ((const char *)cpu->cpu_m.mcpu_cpi->cpi_vendorstr);
23280Sstevel@tonic-gate }
23290Sstevel@tonic-gate 
23300Sstevel@tonic-gate uint_t
23310Sstevel@tonic-gate cpuid_getvendor(cpu_t *cpu)
23320Sstevel@tonic-gate {
23330Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
23340Sstevel@tonic-gate 	return (cpu->cpu_m.mcpu_cpi->cpi_vendor);
23350Sstevel@tonic-gate }
23360Sstevel@tonic-gate 
23370Sstevel@tonic-gate uint_t
23380Sstevel@tonic-gate cpuid_getfamily(cpu_t *cpu)
23390Sstevel@tonic-gate {
23400Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
23410Sstevel@tonic-gate 	return (cpu->cpu_m.mcpu_cpi->cpi_family);
23420Sstevel@tonic-gate }
23430Sstevel@tonic-gate 
23440Sstevel@tonic-gate uint_t
23450Sstevel@tonic-gate cpuid_getmodel(cpu_t *cpu)
23460Sstevel@tonic-gate {
23470Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
23480Sstevel@tonic-gate 	return (cpu->cpu_m.mcpu_cpi->cpi_model);
23490Sstevel@tonic-gate }
23500Sstevel@tonic-gate 
23510Sstevel@tonic-gate uint_t
23520Sstevel@tonic-gate cpuid_get_ncpu_per_chip(cpu_t *cpu)
23530Sstevel@tonic-gate {
23540Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
23550Sstevel@tonic-gate 	return (cpu->cpu_m.mcpu_cpi->cpi_ncpu_per_chip);
23560Sstevel@tonic-gate }
23570Sstevel@tonic-gate 
23580Sstevel@tonic-gate uint_t
23591228Sandrei cpuid_get_ncore_per_chip(cpu_t *cpu)
23601228Sandrei {
23611228Sandrei 	ASSERT(cpuid_checkpass(cpu, 1));
23621228Sandrei 	return (cpu->cpu_m.mcpu_cpi->cpi_ncore_per_chip);
23631228Sandrei }
23641228Sandrei 
23651228Sandrei uint_t
23664606Sesaxe cpuid_get_ncpu_sharing_last_cache(cpu_t *cpu)
23674606Sesaxe {
23684606Sesaxe 	ASSERT(cpuid_checkpass(cpu, 2));
23694606Sesaxe 	return (cpu->cpu_m.mcpu_cpi->cpi_ncpu_shr_last_cache);
23704606Sesaxe }
23714606Sesaxe 
23724606Sesaxe id_t
23734606Sesaxe cpuid_get_last_lvl_cacheid(cpu_t *cpu)
23744606Sesaxe {
23754606Sesaxe 	ASSERT(cpuid_checkpass(cpu, 2));
23764606Sesaxe 	return (cpu->cpu_m.mcpu_cpi->cpi_last_lvl_cacheid);
23774606Sesaxe }
23784606Sesaxe 
23794606Sesaxe uint_t
23800Sstevel@tonic-gate cpuid_getstep(cpu_t *cpu)
23810Sstevel@tonic-gate {
23820Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
23830Sstevel@tonic-gate 	return (cpu->cpu_m.mcpu_cpi->cpi_step);
23840Sstevel@tonic-gate }
23850Sstevel@tonic-gate 
23864581Ssherrym uint_t
23874581Ssherrym cpuid_getsig(struct cpu *cpu)
23884581Ssherrym {
23894581Ssherrym 	ASSERT(cpuid_checkpass(cpu, 1));
23904581Ssherrym 	return (cpu->cpu_m.mcpu_cpi->cpi_std[1].cp_eax);
23914581Ssherrym }
23924581Ssherrym 
23932869Sgavinm uint32_t
23942869Sgavinm cpuid_getchiprev(struct cpu *cpu)
23952869Sgavinm {
23962869Sgavinm 	ASSERT(cpuid_checkpass(cpu, 1));
23972869Sgavinm 	return (cpu->cpu_m.mcpu_cpi->cpi_chiprev);
23982869Sgavinm }
23992869Sgavinm 
24002869Sgavinm const char *
24012869Sgavinm cpuid_getchiprevstr(struct cpu *cpu)
24022869Sgavinm {
24032869Sgavinm 	ASSERT(cpuid_checkpass(cpu, 1));
24042869Sgavinm 	return (cpu->cpu_m.mcpu_cpi->cpi_chiprevstr);
24052869Sgavinm }
24062869Sgavinm 
24072869Sgavinm uint32_t
24082869Sgavinm cpuid_getsockettype(struct cpu *cpu)
24092869Sgavinm {
24102869Sgavinm 	ASSERT(cpuid_checkpass(cpu, 1));
24112869Sgavinm 	return (cpu->cpu_m.mcpu_cpi->cpi_socket);
24122869Sgavinm }
24132869Sgavinm 
24143434Sesaxe int
24153434Sesaxe cpuid_get_chipid(cpu_t *cpu)
24160Sstevel@tonic-gate {
24170Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
24180Sstevel@tonic-gate 
24191228Sandrei 	if (cpuid_is_cmt(cpu))
24200Sstevel@tonic-gate 		return (cpu->cpu_m.mcpu_cpi->cpi_chipid);
24210Sstevel@tonic-gate 	return (cpu->cpu_id);
24220Sstevel@tonic-gate }
24230Sstevel@tonic-gate 
24241228Sandrei id_t
24253434Sesaxe cpuid_get_coreid(cpu_t *cpu)
24261228Sandrei {
24271228Sandrei 	ASSERT(cpuid_checkpass(cpu, 1));
24281228Sandrei 	return (cpu->cpu_m.mcpu_cpi->cpi_coreid);
24291228Sandrei }
24301228Sandrei 
24310Sstevel@tonic-gate int
24323434Sesaxe cpuid_get_clogid(cpu_t *cpu)
24330Sstevel@tonic-gate {
24340Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
24350Sstevel@tonic-gate 	return (cpu->cpu_m.mcpu_cpi->cpi_clogid);
24360Sstevel@tonic-gate }
24370Sstevel@tonic-gate 
24380Sstevel@tonic-gate void
24390Sstevel@tonic-gate cpuid_get_addrsize(cpu_t *cpu, uint_t *pabits, uint_t *vabits)
24400Sstevel@tonic-gate {
24410Sstevel@tonic-gate 	struct cpuid_info *cpi;
24420Sstevel@tonic-gate 
24430Sstevel@tonic-gate 	if (cpu == NULL)
24440Sstevel@tonic-gate 		cpu = CPU;
24450Sstevel@tonic-gate 	cpi = cpu->cpu_m.mcpu_cpi;
24460Sstevel@tonic-gate 
24470Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
24480Sstevel@tonic-gate 
24490Sstevel@tonic-gate 	if (pabits)
24500Sstevel@tonic-gate 		*pabits = cpi->cpi_pabits;
24510Sstevel@tonic-gate 	if (vabits)
24520Sstevel@tonic-gate 		*vabits = cpi->cpi_vabits;
24530Sstevel@tonic-gate }
24540Sstevel@tonic-gate 
24550Sstevel@tonic-gate /*
24560Sstevel@tonic-gate  * Returns the number of data TLB entries for a corresponding
24570Sstevel@tonic-gate  * pagesize.  If it can't be computed, or isn't known, the
24580Sstevel@tonic-gate  * routine returns zero.  If you ask about an architecturally
24590Sstevel@tonic-gate  * impossible pagesize, the routine will panic (so that the
24600Sstevel@tonic-gate  * hat implementor knows that things are inconsistent.)
24610Sstevel@tonic-gate  */
24620Sstevel@tonic-gate uint_t
24630Sstevel@tonic-gate cpuid_get_dtlb_nent(cpu_t *cpu, size_t pagesize)
24640Sstevel@tonic-gate {
24650Sstevel@tonic-gate 	struct cpuid_info *cpi;
24660Sstevel@tonic-gate 	uint_t dtlb_nent = 0;
24670Sstevel@tonic-gate 
24680Sstevel@tonic-gate 	if (cpu == NULL)
24690Sstevel@tonic-gate 		cpu = CPU;
24700Sstevel@tonic-gate 	cpi = cpu->cpu_m.mcpu_cpi;
24710Sstevel@tonic-gate 
24720Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
24730Sstevel@tonic-gate 
24740Sstevel@tonic-gate 	/*
24750Sstevel@tonic-gate 	 * Check the L2 TLB info
24760Sstevel@tonic-gate 	 */
24770Sstevel@tonic-gate 	if (cpi->cpi_xmaxeax >= 0x80000006) {
24781228Sandrei 		struct cpuid_regs *cp = &cpi->cpi_extd[6];
24790Sstevel@tonic-gate 
24800Sstevel@tonic-gate 		switch (pagesize) {
24810Sstevel@tonic-gate 
24820Sstevel@tonic-gate 		case 4 * 1024:
24830Sstevel@tonic-gate 			/*
24840Sstevel@tonic-gate 			 * All zero in the top 16 bits of the register
24850Sstevel@tonic-gate 			 * indicates a unified TLB. Size is in low 16 bits.
24860Sstevel@tonic-gate 			 */
24870Sstevel@tonic-gate 			if ((cp->cp_ebx & 0xffff0000) == 0)
24880Sstevel@tonic-gate 				dtlb_nent = cp->cp_ebx & 0x0000ffff;
24890Sstevel@tonic-gate 			else
24900Sstevel@tonic-gate 				dtlb_nent = BITX(cp->cp_ebx, 27, 16);
24910Sstevel@tonic-gate 			break;
24920Sstevel@tonic-gate 
24930Sstevel@tonic-gate 		case 2 * 1024 * 1024:
24940Sstevel@tonic-gate 			if ((cp->cp_eax & 0xffff0000) == 0)
24950Sstevel@tonic-gate 				dtlb_nent = cp->cp_eax & 0x0000ffff;
24960Sstevel@tonic-gate 			else
24970Sstevel@tonic-gate 				dtlb_nent = BITX(cp->cp_eax, 27, 16);
24980Sstevel@tonic-gate 			break;
24990Sstevel@tonic-gate 
25000Sstevel@tonic-gate 		default:
25010Sstevel@tonic-gate 			panic("unknown L2 pagesize");
25020Sstevel@tonic-gate 			/*NOTREACHED*/
25030Sstevel@tonic-gate 		}
25040Sstevel@tonic-gate 	}
25050Sstevel@tonic-gate 
25060Sstevel@tonic-gate 	if (dtlb_nent != 0)
25070Sstevel@tonic-gate 		return (dtlb_nent);
25080Sstevel@tonic-gate 
25090Sstevel@tonic-gate 	/*
25100Sstevel@tonic-gate 	 * No L2 TLB support for this size, try L1.
25110Sstevel@tonic-gate 	 */
25120Sstevel@tonic-gate 	if (cpi->cpi_xmaxeax >= 0x80000005) {
25131228Sandrei 		struct cpuid_regs *cp = &cpi->cpi_extd[5];
25140Sstevel@tonic-gate 
25150Sstevel@tonic-gate 		switch (pagesize) {
25160Sstevel@tonic-gate 		case 4 * 1024:
25170Sstevel@tonic-gate 			dtlb_nent = BITX(cp->cp_ebx, 23, 16);
25180Sstevel@tonic-gate 			break;
25190Sstevel@tonic-gate 		case 2 * 1024 * 1024:
25200Sstevel@tonic-gate 			dtlb_nent = BITX(cp->cp_eax, 23, 16);
25210Sstevel@tonic-gate 			break;
25220Sstevel@tonic-gate 		default:
25230Sstevel@tonic-gate 			panic("unknown L1 d-TLB pagesize");
25240Sstevel@tonic-gate 			/*NOTREACHED*/
25250Sstevel@tonic-gate 		}
25260Sstevel@tonic-gate 	}
25270Sstevel@tonic-gate 
25280Sstevel@tonic-gate 	return (dtlb_nent);
25290Sstevel@tonic-gate }
25300Sstevel@tonic-gate 
25310Sstevel@tonic-gate /*
25320Sstevel@tonic-gate  * Return 0 if the erratum is not present or not applicable, positive
25330Sstevel@tonic-gate  * if it is, and negative if the status of the erratum is unknown.
25340Sstevel@tonic-gate  *
25350Sstevel@tonic-gate  * See "Revision Guide for AMD Athlon(tm) 64 and AMD Opteron(tm)
2536359Skucharsk  * Processors" #25759, Rev 3.57, August 2005
25370Sstevel@tonic-gate  */
25380Sstevel@tonic-gate int
25390Sstevel@tonic-gate cpuid_opteron_erratum(cpu_t *cpu, uint_t erratum)
25400Sstevel@tonic-gate {
25410Sstevel@tonic-gate 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
25421228Sandrei 	uint_t eax;
25430Sstevel@tonic-gate 
25442584Ssethg 	/*
25452584Ssethg 	 * Bail out if this CPU isn't an AMD CPU, or if it's
25462584Ssethg 	 * a legacy (32-bit) AMD CPU.
25472584Ssethg 	 */
25482584Ssethg 	if (cpi->cpi_vendor != X86_VENDOR_AMD ||
25494265Skchow 	    cpi->cpi_family == 4 || cpi->cpi_family == 5 ||
25504265Skchow 	    cpi->cpi_family == 6)
25512869Sgavinm 
25520Sstevel@tonic-gate 		return (0);
25530Sstevel@tonic-gate 
25540Sstevel@tonic-gate 	eax = cpi->cpi_std[1].cp_eax;
25550Sstevel@tonic-gate 
25560Sstevel@tonic-gate #define	SH_B0(eax)	(eax == 0xf40 || eax == 0xf50)
25570Sstevel@tonic-gate #define	SH_B3(eax) 	(eax == 0xf51)
25581582Skchow #define	B(eax)		(SH_B0(eax) || SH_B3(eax))
25590Sstevel@tonic-gate 
25600Sstevel@tonic-gate #define	SH_C0(eax)	(eax == 0xf48 || eax == 0xf58)
25610Sstevel@tonic-gate 
25620Sstevel@tonic-gate #define	SH_CG(eax)	(eax == 0xf4a || eax == 0xf5a || eax == 0xf7a)
25630Sstevel@tonic-gate #define	DH_CG(eax)	(eax == 0xfc0 || eax == 0xfe0 || eax == 0xff0)
25640Sstevel@tonic-gate #define	CH_CG(eax)	(eax == 0xf82 || eax == 0xfb2)
25651582Skchow #define	CG(eax)		(SH_CG(eax) || DH_CG(eax) || CH_CG(eax))
25660Sstevel@tonic-gate 
25670Sstevel@tonic-gate #define	SH_D0(eax)	(eax == 0x10f40 || eax == 0x10f50 || eax == 0x10f70)
25680Sstevel@tonic-gate #define	DH_D0(eax)	(eax == 0x10fc0 || eax == 0x10ff0)
25690Sstevel@tonic-gate #define	CH_D0(eax)	(eax == 0x10f80 || eax == 0x10fb0)
25701582Skchow #define	D0(eax)		(SH_D0(eax) || DH_D0(eax) || CH_D0(eax))
25710Sstevel@tonic-gate 
25720Sstevel@tonic-gate #define	SH_E0(eax)	(eax == 0x20f50 || eax == 0x20f40 || eax == 0x20f70)
25730Sstevel@tonic-gate #define	JH_E1(eax)	(eax == 0x20f10)	/* JH8_E0 had 0x20f30 */
25740Sstevel@tonic-gate #define	DH_E3(eax)	(eax == 0x20fc0 || eax == 0x20ff0)
25750Sstevel@tonic-gate #define	SH_E4(eax)	(eax == 0x20f51 || eax == 0x20f71)
25760Sstevel@tonic-gate #define	BH_E4(eax)	(eax == 0x20fb1)
25770Sstevel@tonic-gate #define	SH_E5(eax)	(eax == 0x20f42)
25780Sstevel@tonic-gate #define	DH_E6(eax)	(eax == 0x20ff2 || eax == 0x20fc2)
25790Sstevel@tonic-gate #define	JH_E6(eax)	(eax == 0x20f12 || eax == 0x20f32)
25801582Skchow #define	EX(eax)		(SH_E0(eax) || JH_E1(eax) || DH_E3(eax) || \
25811582Skchow 			    SH_E4(eax) || BH_E4(eax) || SH_E5(eax) || \
25821582Skchow 			    DH_E6(eax) || JH_E6(eax))
25830Sstevel@tonic-gate 
25840Sstevel@tonic-gate 	switch (erratum) {
25850Sstevel@tonic-gate 	case 1:
25864265Skchow 		return (cpi->cpi_family < 0x10);
25870Sstevel@tonic-gate 	case 51:	/* what does the asterisk mean? */
25880Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax));
25890Sstevel@tonic-gate 	case 52:
25900Sstevel@tonic-gate 		return (B(eax));
25910Sstevel@tonic-gate 	case 57:
25924265Skchow 		return (cpi->cpi_family <= 0x10);
25930Sstevel@tonic-gate 	case 58:
25940Sstevel@tonic-gate 		return (B(eax));
25950Sstevel@tonic-gate 	case 60:
25964265Skchow 		return (cpi->cpi_family <= 0x10);
25970Sstevel@tonic-gate 	case 61:
25980Sstevel@tonic-gate 	case 62:
25990Sstevel@tonic-gate 	case 63:
26000Sstevel@tonic-gate 	case 64:
26010Sstevel@tonic-gate 	case 65:
26020Sstevel@tonic-gate 	case 66:
26030Sstevel@tonic-gate 	case 68:
26040Sstevel@tonic-gate 	case 69:
26050Sstevel@tonic-gate 	case 70:
26060Sstevel@tonic-gate 	case 71:
26070Sstevel@tonic-gate 		return (B(eax));
26080Sstevel@tonic-gate 	case 72:
26090Sstevel@tonic-gate 		return (SH_B0(eax));
26100Sstevel@tonic-gate 	case 74:
26110Sstevel@tonic-gate 		return (B(eax));
26120Sstevel@tonic-gate 	case 75:
26134265Skchow 		return (cpi->cpi_family < 0x10);
26140Sstevel@tonic-gate 	case 76:
26150Sstevel@tonic-gate 		return (B(eax));
26160Sstevel@tonic-gate 	case 77:
26174265Skchow 		return (cpi->cpi_family <= 0x10);
26180Sstevel@tonic-gate 	case 78:
26190Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax));
26200Sstevel@tonic-gate 	case 79:
26210Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax) || EX(eax));
26220Sstevel@tonic-gate 	case 80:
26230Sstevel@tonic-gate 	case 81:
26240Sstevel@tonic-gate 	case 82:
26250Sstevel@tonic-gate 		return (B(eax));
26260Sstevel@tonic-gate 	case 83:
26270Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax));
26280Sstevel@tonic-gate 	case 85:
26294265Skchow 		return (cpi->cpi_family < 0x10);
26300Sstevel@tonic-gate 	case 86:
26310Sstevel@tonic-gate 		return (SH_C0(eax) || CG(eax));
26320Sstevel@tonic-gate 	case 88:
26330Sstevel@tonic-gate #if !defined(__amd64)
26340Sstevel@tonic-gate 		return (0);
26350Sstevel@tonic-gate #else
26360Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax));
26370Sstevel@tonic-gate #endif
26380Sstevel@tonic-gate 	case 89:
26394265Skchow 		return (cpi->cpi_family < 0x10);
26400Sstevel@tonic-gate 	case 90:
26410Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax));
26420Sstevel@tonic-gate 	case 91:
26430Sstevel@tonic-gate 	case 92:
26440Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax));
26450Sstevel@tonic-gate 	case 93:
26460Sstevel@tonic-gate 		return (SH_C0(eax));
26470Sstevel@tonic-gate 	case 94:
26480Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax));
26490Sstevel@tonic-gate 	case 95:
26500Sstevel@tonic-gate #if !defined(__amd64)
26510Sstevel@tonic-gate 		return (0);
26520Sstevel@tonic-gate #else
26530Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax));
26540Sstevel@tonic-gate #endif
26550Sstevel@tonic-gate 	case 96:
26560Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax));
26570Sstevel@tonic-gate 	case 97:
26580Sstevel@tonic-gate 	case 98:
26590Sstevel@tonic-gate 		return (SH_C0(eax) || CG(eax));
26600Sstevel@tonic-gate 	case 99:
26610Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax));
26620Sstevel@tonic-gate 	case 100:
26630Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax));
26640Sstevel@tonic-gate 	case 101:
26650Sstevel@tonic-gate 	case 103:
26660Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax));
26670Sstevel@tonic-gate 	case 104:
26680Sstevel@tonic-gate 		return (SH_C0(eax) || CG(eax) || D0(eax));
26690Sstevel@tonic-gate 	case 105:
26700Sstevel@tonic-gate 	case 106:
26710Sstevel@tonic-gate 	case 107:
26720Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax));
26730Sstevel@tonic-gate 	case 108:
26740Sstevel@tonic-gate 		return (DH_CG(eax));
26750Sstevel@tonic-gate 	case 109:
26760Sstevel@tonic-gate 		return (SH_C0(eax) || CG(eax) || D0(eax));
26770Sstevel@tonic-gate 	case 110:
26780Sstevel@tonic-gate 		return (D0(eax) || EX(eax));
26790Sstevel@tonic-gate 	case 111:
26800Sstevel@tonic-gate 		return (CG(eax));
26810Sstevel@tonic-gate 	case 112:
26820Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax) || EX(eax));
26830Sstevel@tonic-gate 	case 113:
26840Sstevel@tonic-gate 		return (eax == 0x20fc0);
26850Sstevel@tonic-gate 	case 114:
26860Sstevel@tonic-gate 		return (SH_E0(eax) || JH_E1(eax) || DH_E3(eax));
26870Sstevel@tonic-gate 	case 115:
26880Sstevel@tonic-gate 		return (SH_E0(eax) || JH_E1(eax));
26890Sstevel@tonic-gate 	case 116:
26900Sstevel@tonic-gate 		return (SH_E0(eax) || JH_E1(eax) || DH_E3(eax));
26910Sstevel@tonic-gate 	case 117:
26920Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax));
26930Sstevel@tonic-gate 	case 118:
26940Sstevel@tonic-gate 		return (SH_E0(eax) || JH_E1(eax) || SH_E4(eax) || BH_E4(eax) ||
26950Sstevel@tonic-gate 		    JH_E6(eax));
26960Sstevel@tonic-gate 	case 121:
26970Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax) || EX(eax));
26980Sstevel@tonic-gate 	case 122:
26994265Skchow 		return (cpi->cpi_family < 0x10);
27000Sstevel@tonic-gate 	case 123:
27010Sstevel@tonic-gate 		return (JH_E1(eax) || BH_E4(eax) || JH_E6(eax));
2702359Skucharsk 	case 131:
27034265Skchow 		return (cpi->cpi_family < 0x10);
2704938Sesaxe 	case 6336786:
2705938Sesaxe 		/*
2706938Sesaxe 		 * Test for AdvPowerMgmtInfo.TscPStateInvariant
27074265Skchow 		 * if this is a K8 family or newer processor
2708938Sesaxe 		 */
2709938Sesaxe 		if (CPI_FAMILY(cpi) == 0xf) {
27101228Sandrei 			struct cpuid_regs regs;
27111228Sandrei 			regs.cp_eax = 0x80000007;
27121228Sandrei 			(void) __cpuid_insn(&regs);
27131228Sandrei 			return (!(regs.cp_edx & 0x100));
2714938Sesaxe 		}
2715938Sesaxe 		return (0);
27161582Skchow 	case 6323525:
27171582Skchow 		return (((((eax >> 12) & 0xff00) + (eax & 0xf00)) |
27181582Skchow 		    (((eax >> 4) & 0xf) | ((eax >> 12) & 0xf0))) < 0xf40);
27191582Skchow 
27200Sstevel@tonic-gate 	default:
27210Sstevel@tonic-gate 		return (-1);
27220Sstevel@tonic-gate 	}
27230Sstevel@tonic-gate }
27240Sstevel@tonic-gate 
27250Sstevel@tonic-gate static const char assoc_str[] = "associativity";
27260Sstevel@tonic-gate static const char line_str[] = "line-size";
27270Sstevel@tonic-gate static const char size_str[] = "size";
27280Sstevel@tonic-gate 
27290Sstevel@tonic-gate static void
27300Sstevel@tonic-gate add_cache_prop(dev_info_t *devi, const char *label, const char *type,
27310Sstevel@tonic-gate     uint32_t val)
27320Sstevel@tonic-gate {
27330Sstevel@tonic-gate 	char buf[128];
27340Sstevel@tonic-gate 
27350Sstevel@tonic-gate 	/*
27360Sstevel@tonic-gate 	 * ndi_prop_update_int() is used because it is desirable for
27370Sstevel@tonic-gate 	 * DDI_PROP_HW_DEF and DDI_PROP_DONTSLEEP to be set.
27380Sstevel@tonic-gate 	 */
27390Sstevel@tonic-gate 	if (snprintf(buf, sizeof (buf), "%s-%s", label, type) < sizeof (buf))
27400Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, devi, buf, val);
27410Sstevel@tonic-gate }
27420Sstevel@tonic-gate 
27430Sstevel@tonic-gate /*
27440Sstevel@tonic-gate  * Intel-style cache/tlb description
27450Sstevel@tonic-gate  *
27460Sstevel@tonic-gate  * Standard cpuid level 2 gives a randomly ordered
27470Sstevel@tonic-gate  * selection of tags that index into a table that describes
27480Sstevel@tonic-gate  * cache and tlb properties.
27490Sstevel@tonic-gate  */
27500Sstevel@tonic-gate 
27510Sstevel@tonic-gate static const char l1_icache_str[] = "l1-icache";
27520Sstevel@tonic-gate static const char l1_dcache_str[] = "l1-dcache";
27530Sstevel@tonic-gate static const char l2_cache_str[] = "l2-cache";
27543446Smrj static const char l3_cache_str[] = "l3-cache";
27550Sstevel@tonic-gate static const char itlb4k_str[] = "itlb-4K";
27560Sstevel@tonic-gate static const char dtlb4k_str[] = "dtlb-4K";
27570Sstevel@tonic-gate static const char itlb4M_str[] = "itlb-4M";
27580Sstevel@tonic-gate static const char dtlb4M_str[] = "dtlb-4M";
27590Sstevel@tonic-gate static const char itlb424_str[] = "itlb-4K-2M-4M";
27600Sstevel@tonic-gate static const char dtlb44_str[] = "dtlb-4K-4M";
27610Sstevel@tonic-gate static const char sl1_dcache_str[] = "sectored-l1-dcache";
27620Sstevel@tonic-gate static const char sl2_cache_str[] = "sectored-l2-cache";
27630Sstevel@tonic-gate static const char itrace_str[] = "itrace-cache";
27640Sstevel@tonic-gate static const char sl3_cache_str[] = "sectored-l3-cache";
27650Sstevel@tonic-gate 
27660Sstevel@tonic-gate static const struct cachetab {
27670Sstevel@tonic-gate 	uint8_t 	ct_code;
27680Sstevel@tonic-gate 	uint8_t		ct_assoc;
27690Sstevel@tonic-gate 	uint16_t 	ct_line_size;
27700Sstevel@tonic-gate 	size_t		ct_size;
27710Sstevel@tonic-gate 	const char	*ct_label;
27720Sstevel@tonic-gate } intel_ctab[] = {
27730Sstevel@tonic-gate 	/* maintain descending order! */
27743446Smrj 	{ 0xb4, 4, 0, 256, dtlb4k_str },
27750Sstevel@tonic-gate 	{ 0xb3, 4, 0, 128, dtlb4k_str },
27760Sstevel@tonic-gate 	{ 0xb0, 4, 0, 128, itlb4k_str },
27770Sstevel@tonic-gate 	{ 0x87, 8, 64, 1024*1024, l2_cache_str},
27780Sstevel@tonic-gate 	{ 0x86, 4, 64, 512*1024, l2_cache_str},
27790Sstevel@tonic-gate 	{ 0x85, 8, 32, 2*1024*1024, l2_cache_str},
27800Sstevel@tonic-gate 	{ 0x84, 8, 32, 1024*1024, l2_cache_str},
27810Sstevel@tonic-gate 	{ 0x83, 8, 32, 512*1024, l2_cache_str},
27820Sstevel@tonic-gate 	{ 0x82, 8, 32, 256*1024, l2_cache_str},
27830Sstevel@tonic-gate 	{ 0x7f, 2, 64, 512*1024, l2_cache_str},
27840Sstevel@tonic-gate 	{ 0x7d, 8, 64, 2*1024*1024, sl2_cache_str},
27850Sstevel@tonic-gate 	{ 0x7c, 8, 64, 1024*1024, sl2_cache_str},
27860Sstevel@tonic-gate 	{ 0x7b, 8, 64, 512*1024, sl2_cache_str},
27870Sstevel@tonic-gate 	{ 0x7a, 8, 64, 256*1024, sl2_cache_str},
27880Sstevel@tonic-gate 	{ 0x79, 8, 64, 128*1024, sl2_cache_str},
27890Sstevel@tonic-gate 	{ 0x78, 8, 64, 1024*1024, l2_cache_str},
27903446Smrj 	{ 0x73, 8, 0, 64*1024, itrace_str},
27910Sstevel@tonic-gate 	{ 0x72, 8, 0, 32*1024, itrace_str},
27920Sstevel@tonic-gate 	{ 0x71, 8, 0, 16*1024, itrace_str},
27930Sstevel@tonic-gate 	{ 0x70, 8, 0, 12*1024, itrace_str},
27940Sstevel@tonic-gate 	{ 0x68, 4, 64, 32*1024, sl1_dcache_str},
27950Sstevel@tonic-gate 	{ 0x67, 4, 64, 16*1024, sl1_dcache_str},
27960Sstevel@tonic-gate 	{ 0x66, 4, 64, 8*1024, sl1_dcache_str},
27970Sstevel@tonic-gate 	{ 0x60, 8, 64, 16*1024, sl1_dcache_str},
27980Sstevel@tonic-gate 	{ 0x5d, 0, 0, 256, dtlb44_str},
27990Sstevel@tonic-gate 	{ 0x5c, 0, 0, 128, dtlb44_str},
28000Sstevel@tonic-gate 	{ 0x5b, 0, 0, 64, dtlb44_str},
28010Sstevel@tonic-gate 	{ 0x52, 0, 0, 256, itlb424_str},
28020Sstevel@tonic-gate 	{ 0x51, 0, 0, 128, itlb424_str},
28030Sstevel@tonic-gate 	{ 0x50, 0, 0, 64, itlb424_str},
28043446Smrj 	{ 0x4d, 16, 64, 16*1024*1024, l3_cache_str},
28053446Smrj 	{ 0x4c, 12, 64, 12*1024*1024, l3_cache_str},
28063446Smrj 	{ 0x4b, 16, 64, 8*1024*1024, l3_cache_str},
28073446Smrj 	{ 0x4a, 12, 64, 6*1024*1024, l3_cache_str},
28083446Smrj 	{ 0x49, 16, 64, 4*1024*1024, l3_cache_str},
28093446Smrj 	{ 0x47, 8, 64, 8*1024*1024, l3_cache_str},
28103446Smrj 	{ 0x46, 4, 64, 4*1024*1024, l3_cache_str},
28110Sstevel@tonic-gate 	{ 0x45, 4, 32, 2*1024*1024, l2_cache_str},
28120Sstevel@tonic-gate 	{ 0x44, 4, 32, 1024*1024, l2_cache_str},
28130Sstevel@tonic-gate 	{ 0x43, 4, 32, 512*1024, l2_cache_str},
28140Sstevel@tonic-gate 	{ 0x42, 4, 32, 256*1024, l2_cache_str},
28150Sstevel@tonic-gate 	{ 0x41, 4, 32, 128*1024, l2_cache_str},
28163446Smrj 	{ 0x3e, 4, 64, 512*1024, sl2_cache_str},
28173446Smrj 	{ 0x3d, 6, 64, 384*1024, sl2_cache_str},
28180Sstevel@tonic-gate 	{ 0x3c, 4, 64, 256*1024, sl2_cache_str},
28190Sstevel@tonic-gate 	{ 0x3b, 2, 64, 128*1024, sl2_cache_str},
28203446Smrj 	{ 0x3a, 6, 64, 192*1024, sl2_cache_str},
28210Sstevel@tonic-gate 	{ 0x39, 4, 64, 128*1024, sl2_cache_str},
28220Sstevel@tonic-gate 	{ 0x30, 8, 64, 32*1024, l1_icache_str},
28230Sstevel@tonic-gate 	{ 0x2c, 8, 64, 32*1024, l1_dcache_str},
28240Sstevel@tonic-gate 	{ 0x29, 8, 64, 4096*1024, sl3_cache_str},
28250Sstevel@tonic-gate 	{ 0x25, 8, 64, 2048*1024, sl3_cache_str},
28260Sstevel@tonic-gate 	{ 0x23, 8, 64, 1024*1024, sl3_cache_str},
28270Sstevel@tonic-gate 	{ 0x22, 4, 64, 512*1024, sl3_cache_str},
28280Sstevel@tonic-gate 	{ 0x0c, 4, 32, 16*1024, l1_dcache_str},
28293446Smrj 	{ 0x0b, 4, 0, 4, itlb4M_str},
28300Sstevel@tonic-gate 	{ 0x0a, 2, 32, 8*1024, l1_dcache_str},
28310Sstevel@tonic-gate 	{ 0x08, 4, 32, 16*1024, l1_icache_str},
28320Sstevel@tonic-gate 	{ 0x06, 4, 32, 8*1024, l1_icache_str},
28330Sstevel@tonic-gate 	{ 0x04, 4, 0, 8, dtlb4M_str},
28340Sstevel@tonic-gate 	{ 0x03, 4, 0, 64, dtlb4k_str},
28350Sstevel@tonic-gate 	{ 0x02, 4, 0, 2, itlb4M_str},
28360Sstevel@tonic-gate 	{ 0x01, 4, 0, 32, itlb4k_str},
28370Sstevel@tonic-gate 	{ 0 }
28380Sstevel@tonic-gate };
28390Sstevel@tonic-gate 
28400Sstevel@tonic-gate static const struct cachetab cyrix_ctab[] = {
28410Sstevel@tonic-gate 	{ 0x70, 4, 0, 32, "tlb-4K" },
28420Sstevel@tonic-gate 	{ 0x80, 4, 16, 16*1024, "l1-cache" },
28430Sstevel@tonic-gate 	{ 0 }
28440Sstevel@tonic-gate };
28450Sstevel@tonic-gate 
28460Sstevel@tonic-gate /*
28470Sstevel@tonic-gate  * Search a cache table for a matching entry
28480Sstevel@tonic-gate  */
28490Sstevel@tonic-gate static const struct cachetab *
28500Sstevel@tonic-gate find_cacheent(const struct cachetab *ct, uint_t code)
28510Sstevel@tonic-gate {
28520Sstevel@tonic-gate 	if (code != 0) {
28530Sstevel@tonic-gate 		for (; ct->ct_code != 0; ct++)
28540Sstevel@tonic-gate 			if (ct->ct_code <= code)
28550Sstevel@tonic-gate 				break;
28560Sstevel@tonic-gate 		if (ct->ct_code == code)
28570Sstevel@tonic-gate 			return (ct);
28580Sstevel@tonic-gate 	}
28590Sstevel@tonic-gate 	return (NULL);
28600Sstevel@tonic-gate }
28610Sstevel@tonic-gate 
28620Sstevel@tonic-gate /*
28630Sstevel@tonic-gate  * Walk the cacheinfo descriptor, applying 'func' to every valid element
28640Sstevel@tonic-gate  * The walk is terminated if the walker returns non-zero.
28650Sstevel@tonic-gate  */
28660Sstevel@tonic-gate static void
28670Sstevel@tonic-gate intel_walk_cacheinfo(struct cpuid_info *cpi,
28680Sstevel@tonic-gate     void *arg, int (*func)(void *, const struct cachetab *))
28690Sstevel@tonic-gate {
28700Sstevel@tonic-gate 	const struct cachetab *ct;
28710Sstevel@tonic-gate 	uint8_t *dp;
28720Sstevel@tonic-gate 	int i;
28730Sstevel@tonic-gate 
28740Sstevel@tonic-gate 	if ((dp = cpi->cpi_cacheinfo) == NULL)
28750Sstevel@tonic-gate 		return;
28764797Sksadhukh 	for (i = 0; i < cpi->cpi_ncache; i++, dp++) {
28774797Sksadhukh 		/*
28784797Sksadhukh 		 * For overloaded descriptor 0x49 we use cpuid function 4
28794797Sksadhukh 		 * if supported by the current processor, to update
28804797Sksadhukh 		 * cache information.
28814797Sksadhukh 		 */
28824797Sksadhukh 		if (*dp == 0x49 && cpi->cpi_maxeax >= 0x4) {
28834797Sksadhukh 			intel_cpuid_4_cache_info(arg, cpi);
28844797Sksadhukh 			continue;
28854797Sksadhukh 		}
28864797Sksadhukh 
28870Sstevel@tonic-gate 		if ((ct = find_cacheent(intel_ctab, *dp)) != NULL) {
28880Sstevel@tonic-gate 			if (func(arg, ct) != 0)
28890Sstevel@tonic-gate 				break;
28900Sstevel@tonic-gate 		}
28914797Sksadhukh 	}
28920Sstevel@tonic-gate }
28930Sstevel@tonic-gate 
28940Sstevel@tonic-gate /*
28950Sstevel@tonic-gate  * (Like the Intel one, except for Cyrix CPUs)
28960Sstevel@tonic-gate  */
28970Sstevel@tonic-gate static void
28980Sstevel@tonic-gate cyrix_walk_cacheinfo(struct cpuid_info *cpi,
28990Sstevel@tonic-gate     void *arg, int (*func)(void *, const struct cachetab *))
29000Sstevel@tonic-gate {
29010Sstevel@tonic-gate 	const struct cachetab *ct;
29020Sstevel@tonic-gate 	uint8_t *dp;
29030Sstevel@tonic-gate 	int i;
29040Sstevel@tonic-gate 
29050Sstevel@tonic-gate 	if ((dp = cpi->cpi_cacheinfo) == NULL)
29060Sstevel@tonic-gate 		return;
29070Sstevel@tonic-gate 	for (i = 0; i < cpi->cpi_ncache; i++, dp++) {
29080Sstevel@tonic-gate 		/*
29090Sstevel@tonic-gate 		 * Search Cyrix-specific descriptor table first ..
29100Sstevel@tonic-gate 		 */
29110Sstevel@tonic-gate 		if ((ct = find_cacheent(cyrix_ctab, *dp)) != NULL) {
29120Sstevel@tonic-gate 			if (func(arg, ct) != 0)
29130Sstevel@tonic-gate 				break;
29140Sstevel@tonic-gate 			continue;
29150Sstevel@tonic-gate 		}
29160Sstevel@tonic-gate 		/*
29170Sstevel@tonic-gate 		 * .. else fall back to the Intel one
29180Sstevel@tonic-gate 		 */
29190Sstevel@tonic-gate 		if ((ct = find_cacheent(intel_ctab, *dp)) != NULL) {
29200Sstevel@tonic-gate 			if (func(arg, ct) != 0)
29210Sstevel@tonic-gate 				break;
29220Sstevel@tonic-gate 			continue;
29230Sstevel@tonic-gate 		}
29240Sstevel@tonic-gate 	}
29250Sstevel@tonic-gate }
29260Sstevel@tonic-gate 
29270Sstevel@tonic-gate /*
29280Sstevel@tonic-gate  * A cacheinfo walker that adds associativity, line-size, and size properties
29290Sstevel@tonic-gate  * to the devinfo node it is passed as an argument.
29300Sstevel@tonic-gate  */
29310Sstevel@tonic-gate static int
29320Sstevel@tonic-gate add_cacheent_props(void *arg, const struct cachetab *ct)
29330Sstevel@tonic-gate {
29340Sstevel@tonic-gate 	dev_info_t *devi = arg;
29350Sstevel@tonic-gate 
29360Sstevel@tonic-gate 	add_cache_prop(devi, ct->ct_label, assoc_str, ct->ct_assoc);
29370Sstevel@tonic-gate 	if (ct->ct_line_size != 0)
29380Sstevel@tonic-gate 		add_cache_prop(devi, ct->ct_label, line_str,
29390Sstevel@tonic-gate 		    ct->ct_line_size);
29400Sstevel@tonic-gate 	add_cache_prop(devi, ct->ct_label, size_str, ct->ct_size);
29410Sstevel@tonic-gate 	return (0);
29420Sstevel@tonic-gate }
29430Sstevel@tonic-gate 
29444797Sksadhukh /*
29454797Sksadhukh  * Add L2 or L3 cache-information using cpuid function 4. This
29464797Sksadhukh  * function is called from intel_walk_cacheinfo() when descriptor
29474797Sksadhukh  * 0x49 is encountered.
29484797Sksadhukh  */
29494797Sksadhukh static void
29504797Sksadhukh intel_cpuid_4_cache_info(void *arg, struct cpuid_info *cpi)
29514797Sksadhukh {
29524797Sksadhukh 	uint32_t level, i;
29534797Sksadhukh 
29544797Sksadhukh 	struct cachetab ct;
29554797Sksadhukh 
29564797Sksadhukh 	for (i = 0; i < cpi->cpi_std_4_size; i++) {
29574797Sksadhukh 		level = CPI_CACHE_LVL(cpi->cpi_std_4[i]);
29584797Sksadhukh 
29594797Sksadhukh 		if (level == 2 || level == 3) {
29604797Sksadhukh 			ct.ct_assoc = CPI_CACHE_WAYS(cpi->cpi_std_4[i]) + 1;
29614797Sksadhukh 			ct.ct_line_size =
29624797Sksadhukh 			    CPI_CACHE_COH_LN_SZ(cpi->cpi_std_4[i]) + 1;
29634797Sksadhukh 			ct.ct_size = ct.ct_assoc *
29644797Sksadhukh 			    (CPI_CACHE_PARTS(cpi->cpi_std_4[i]) + 1) *
29654797Sksadhukh 			    ct.ct_line_size *
29664797Sksadhukh 			    (cpi->cpi_std_4[i]->cp_ecx + 1);
29674797Sksadhukh 
29684797Sksadhukh 			if (level == 2) {
29694797Sksadhukh 				ct.ct_label = l2_cache_str;
29704797Sksadhukh 			} else if (level == 3) {
29714797Sksadhukh 				ct.ct_label = l3_cache_str;
29724797Sksadhukh 			}
29734797Sksadhukh 
29744797Sksadhukh 			(void) add_cacheent_props(arg,
29754797Sksadhukh 			    (const struct cachetab *) (&ct));
29764797Sksadhukh 		}
29774797Sksadhukh 	}
29784797Sksadhukh }
29794797Sksadhukh 
29800Sstevel@tonic-gate static const char fully_assoc[] = "fully-associative?";
29810Sstevel@tonic-gate 
29820Sstevel@tonic-gate /*
29830Sstevel@tonic-gate  * AMD style cache/tlb description
29840Sstevel@tonic-gate  *
29850Sstevel@tonic-gate  * Extended functions 5 and 6 directly describe properties of
29860Sstevel@tonic-gate  * tlbs and various cache levels.
29870Sstevel@tonic-gate  */
29880Sstevel@tonic-gate static void
29890Sstevel@tonic-gate add_amd_assoc(dev_info_t *devi, const char *label, uint_t assoc)
29900Sstevel@tonic-gate {
29910Sstevel@tonic-gate 	switch (assoc) {
29920Sstevel@tonic-gate 	case 0:	/* reserved; ignore */
29930Sstevel@tonic-gate 		break;
29940Sstevel@tonic-gate 	default:
29950Sstevel@tonic-gate 		add_cache_prop(devi, label, assoc_str, assoc);
29960Sstevel@tonic-gate 		break;
29970Sstevel@tonic-gate 	case 0xff:
29980Sstevel@tonic-gate 		add_cache_prop(devi, label, fully_assoc, 1);
29990Sstevel@tonic-gate 		break;
30000Sstevel@tonic-gate 	}
30010Sstevel@tonic-gate }
30020Sstevel@tonic-gate 
30030Sstevel@tonic-gate static void
30040Sstevel@tonic-gate add_amd_tlb(dev_info_t *devi, const char *label, uint_t assoc, uint_t size)
30050Sstevel@tonic-gate {
30060Sstevel@tonic-gate 	if (size == 0)
30070Sstevel@tonic-gate 		return;
30080Sstevel@tonic-gate 	add_cache_prop(devi, label, size_str, size);
30090Sstevel@tonic-gate 	add_amd_assoc(devi, label, assoc);
30100Sstevel@tonic-gate }
30110Sstevel@tonic-gate 
30120Sstevel@tonic-gate static void
30130Sstevel@tonic-gate add_amd_cache(dev_info_t *devi, const char *label,
30140Sstevel@tonic-gate     uint_t size, uint_t assoc, uint_t lines_per_tag, uint_t line_size)
30150Sstevel@tonic-gate {
30160Sstevel@tonic-gate 	if (size == 0 || line_size == 0)
30170Sstevel@tonic-gate 		return;
30180Sstevel@tonic-gate 	add_amd_assoc(devi, label, assoc);
30190Sstevel@tonic-gate 	/*
30200Sstevel@tonic-gate 	 * Most AMD parts have a sectored cache. Multiple cache lines are
30210Sstevel@tonic-gate 	 * associated with each tag. A sector consists of all cache lines
30220Sstevel@tonic-gate 	 * associated with a tag. For example, the AMD K6-III has a sector
30230Sstevel@tonic-gate 	 * size of 2 cache lines per tag.
30240Sstevel@tonic-gate 	 */
30250Sstevel@tonic-gate 	if (lines_per_tag != 0)
30260Sstevel@tonic-gate 		add_cache_prop(devi, label, "lines-per-tag", lines_per_tag);
30270Sstevel@tonic-gate 	add_cache_prop(devi, label, line_str, line_size);
30280Sstevel@tonic-gate 	add_cache_prop(devi, label, size_str, size * 1024);
30290Sstevel@tonic-gate }
30300Sstevel@tonic-gate 
30310Sstevel@tonic-gate static void
30320Sstevel@tonic-gate add_amd_l2_assoc(dev_info_t *devi, const char *label, uint_t assoc)
30330Sstevel@tonic-gate {
30340Sstevel@tonic-gate 	switch (assoc) {
30350Sstevel@tonic-gate 	case 0:	/* off */
30360Sstevel@tonic-gate 		break;
30370Sstevel@tonic-gate 	case 1:
30380Sstevel@tonic-gate 	case 2:
30390Sstevel@tonic-gate 	case 4:
30400Sstevel@tonic-gate 		add_cache_prop(devi, label, assoc_str, assoc);
30410Sstevel@tonic-gate 		break;
30420Sstevel@tonic-gate 	case 6:
30430Sstevel@tonic-gate 		add_cache_prop(devi, label, assoc_str, 8);
30440Sstevel@tonic-gate 		break;
30450Sstevel@tonic-gate 	case 8:
30460Sstevel@tonic-gate 		add_cache_prop(devi, label, assoc_str, 16);
30470Sstevel@tonic-gate 		break;
30480Sstevel@tonic-gate 	case 0xf:
30490Sstevel@tonic-gate 		add_cache_prop(devi, label, fully_assoc, 1);
30500Sstevel@tonic-gate 		break;
30510Sstevel@tonic-gate 	default: /* reserved; ignore */
30520Sstevel@tonic-gate 		break;
30530Sstevel@tonic-gate 	}
30540Sstevel@tonic-gate }
30550Sstevel@tonic-gate 
30560Sstevel@tonic-gate static void
30570Sstevel@tonic-gate add_amd_l2_tlb(dev_info_t *devi, const char *label, uint_t assoc, uint_t size)
30580Sstevel@tonic-gate {
30590Sstevel@tonic-gate 	if (size == 0 || assoc == 0)
30600Sstevel@tonic-gate 		return;
30610Sstevel@tonic-gate 	add_amd_l2_assoc(devi, label, assoc);
30620Sstevel@tonic-gate 	add_cache_prop(devi, label, size_str, size);
30630Sstevel@tonic-gate }
30640Sstevel@tonic-gate 
30650Sstevel@tonic-gate static void
30660Sstevel@tonic-gate add_amd_l2_cache(dev_info_t *devi, const char *label,
30670Sstevel@tonic-gate     uint_t size, uint_t assoc, uint_t lines_per_tag, uint_t line_size)
30680Sstevel@tonic-gate {
30690Sstevel@tonic-gate 	if (size == 0 || assoc == 0 || line_size == 0)
30700Sstevel@tonic-gate 		return;
30710Sstevel@tonic-gate 	add_amd_l2_assoc(devi, label, assoc);
30720Sstevel@tonic-gate 	if (lines_per_tag != 0)
30730Sstevel@tonic-gate 		add_cache_prop(devi, label, "lines-per-tag", lines_per_tag);
30740Sstevel@tonic-gate 	add_cache_prop(devi, label, line_str, line_size);
30750Sstevel@tonic-gate 	add_cache_prop(devi, label, size_str, size * 1024);
30760Sstevel@tonic-gate }
30770Sstevel@tonic-gate 
30780Sstevel@tonic-gate static void
30790Sstevel@tonic-gate amd_cache_info(struct cpuid_info *cpi, dev_info_t *devi)
30800Sstevel@tonic-gate {
30811228Sandrei 	struct cpuid_regs *cp;
30820Sstevel@tonic-gate 
30830Sstevel@tonic-gate 	if (cpi->cpi_xmaxeax < 0x80000005)
30840Sstevel@tonic-gate 		return;
30850Sstevel@tonic-gate 	cp = &cpi->cpi_extd[5];
30860Sstevel@tonic-gate 
30870Sstevel@tonic-gate 	/*
30880Sstevel@tonic-gate 	 * 4M/2M L1 TLB configuration
30890Sstevel@tonic-gate 	 *
30900Sstevel@tonic-gate 	 * We report the size for 2M pages because AMD uses two
30910Sstevel@tonic-gate 	 * TLB entries for one 4M page.
30920Sstevel@tonic-gate 	 */
30930Sstevel@tonic-gate 	add_amd_tlb(devi, "dtlb-2M",
30940Sstevel@tonic-gate 	    BITX(cp->cp_eax, 31, 24), BITX(cp->cp_eax, 23, 16));
30950Sstevel@tonic-gate 	add_amd_tlb(devi, "itlb-2M",
30960Sstevel@tonic-gate 	    BITX(cp->cp_eax, 15, 8), BITX(cp->cp_eax, 7, 0));
30970Sstevel@tonic-gate 
30980Sstevel@tonic-gate 	/*
30990Sstevel@tonic-gate 	 * 4K L1 TLB configuration
31000Sstevel@tonic-gate 	 */
31010Sstevel@tonic-gate 
31020Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
31030Sstevel@tonic-gate 		uint_t nentries;
31040Sstevel@tonic-gate 	case X86_VENDOR_TM:
31050Sstevel@tonic-gate 		if (cpi->cpi_family >= 5) {
31060Sstevel@tonic-gate 			/*
31070Sstevel@tonic-gate 			 * Crusoe processors have 256 TLB entries, but
31080Sstevel@tonic-gate 			 * cpuid data format constrains them to only
31090Sstevel@tonic-gate 			 * reporting 255 of them.
31100Sstevel@tonic-gate 			 */
31110Sstevel@tonic-gate 			if ((nentries = BITX(cp->cp_ebx, 23, 16)) == 255)
31120Sstevel@tonic-gate 				nentries = 256;
31130Sstevel@tonic-gate 			/*
31140Sstevel@tonic-gate 			 * Crusoe processors also have a unified TLB
31150Sstevel@tonic-gate 			 */
31160Sstevel@tonic-gate 			add_amd_tlb(devi, "tlb-4K", BITX(cp->cp_ebx, 31, 24),
31170Sstevel@tonic-gate 			    nentries);
31180Sstevel@tonic-gate 			break;
31190Sstevel@tonic-gate 		}
31200Sstevel@tonic-gate 		/*FALLTHROUGH*/
31210Sstevel@tonic-gate 	default:
31220Sstevel@tonic-gate 		add_amd_tlb(devi, itlb4k_str,
31230Sstevel@tonic-gate 		    BITX(cp->cp_ebx, 31, 24), BITX(cp->cp_ebx, 23, 16));
31240Sstevel@tonic-gate 		add_amd_tlb(devi, dtlb4k_str,
31250Sstevel@tonic-gate 		    BITX(cp->cp_ebx, 15, 8), BITX(cp->cp_ebx, 7, 0));
31260Sstevel@tonic-gate 		break;
31270Sstevel@tonic-gate 	}
31280Sstevel@tonic-gate 
31290Sstevel@tonic-gate 	/*
31300Sstevel@tonic-gate 	 * data L1 cache configuration
31310Sstevel@tonic-gate 	 */
31320Sstevel@tonic-gate 
31330Sstevel@tonic-gate 	add_amd_cache(devi, l1_dcache_str,
31340Sstevel@tonic-gate 	    BITX(cp->cp_ecx, 31, 24), BITX(cp->cp_ecx, 23, 16),
31350Sstevel@tonic-gate 	    BITX(cp->cp_ecx, 15, 8), BITX(cp->cp_ecx, 7, 0));
31360Sstevel@tonic-gate 
31370Sstevel@tonic-gate 	/*
31380Sstevel@tonic-gate 	 * code L1 cache configuration
31390Sstevel@tonic-gate 	 */
31400Sstevel@tonic-gate 
31410Sstevel@tonic-gate 	add_amd_cache(devi, l1_icache_str,
31420Sstevel@tonic-gate 	    BITX(cp->cp_edx, 31, 24), BITX(cp->cp_edx, 23, 16),
31430Sstevel@tonic-gate 	    BITX(cp->cp_edx, 15, 8), BITX(cp->cp_edx, 7, 0));
31440Sstevel@tonic-gate 
31450Sstevel@tonic-gate 	if (cpi->cpi_xmaxeax < 0x80000006)
31460Sstevel@tonic-gate 		return;
31470Sstevel@tonic-gate 	cp = &cpi->cpi_extd[6];
31480Sstevel@tonic-gate 
31490Sstevel@tonic-gate 	/* Check for a unified L2 TLB for large pages */
31500Sstevel@tonic-gate 
31510Sstevel@tonic-gate 	if (BITX(cp->cp_eax, 31, 16) == 0)
31520Sstevel@tonic-gate 		add_amd_l2_tlb(devi, "l2-tlb-2M",
31530Sstevel@tonic-gate 		    BITX(cp->cp_eax, 15, 12), BITX(cp->cp_eax, 11, 0));
31540Sstevel@tonic-gate 	else {
31550Sstevel@tonic-gate 		add_amd_l2_tlb(devi, "l2-dtlb-2M",
31560Sstevel@tonic-gate 		    BITX(cp->cp_eax, 31, 28), BITX(cp->cp_eax, 27, 16));
31570Sstevel@tonic-gate 		add_amd_l2_tlb(devi, "l2-itlb-2M",
31580Sstevel@tonic-gate 		    BITX(cp->cp_eax, 15, 12), BITX(cp->cp_eax, 11, 0));
31590Sstevel@tonic-gate 	}
31600Sstevel@tonic-gate 
31610Sstevel@tonic-gate 	/* Check for a unified L2 TLB for 4K pages */
31620Sstevel@tonic-gate 
31630Sstevel@tonic-gate 	if (BITX(cp->cp_ebx, 31, 16) == 0) {
31640Sstevel@tonic-gate 		add_amd_l2_tlb(devi, "l2-tlb-4K",
31650Sstevel@tonic-gate 		    BITX(cp->cp_eax, 15, 12), BITX(cp->cp_eax, 11, 0));
31660Sstevel@tonic-gate 	} else {
31670Sstevel@tonic-gate 		add_amd_l2_tlb(devi, "l2-dtlb-4K",
31680Sstevel@tonic-gate 		    BITX(cp->cp_eax, 31, 28), BITX(cp->cp_eax, 27, 16));
31690Sstevel@tonic-gate 		add_amd_l2_tlb(devi, "l2-itlb-4K",
31700Sstevel@tonic-gate 		    BITX(cp->cp_eax, 15, 12), BITX(cp->cp_eax, 11, 0));
31710Sstevel@tonic-gate 	}
31720Sstevel@tonic-gate 
31730Sstevel@tonic-gate 	add_amd_l2_cache(devi, l2_cache_str,
31740Sstevel@tonic-gate 	    BITX(cp->cp_ecx, 31, 16), BITX(cp->cp_ecx, 15, 12),
31750Sstevel@tonic-gate 	    BITX(cp->cp_ecx, 11, 8), BITX(cp->cp_ecx, 7, 0));
31760Sstevel@tonic-gate }
31770Sstevel@tonic-gate 
31780Sstevel@tonic-gate /*
31790Sstevel@tonic-gate  * There are two basic ways that the x86 world describes it cache
31800Sstevel@tonic-gate  * and tlb architecture - Intel's way and AMD's way.
31810Sstevel@tonic-gate  *
31820Sstevel@tonic-gate  * Return which flavor of cache architecture we should use
31830Sstevel@tonic-gate  */
31840Sstevel@tonic-gate static int
31850Sstevel@tonic-gate x86_which_cacheinfo(struct cpuid_info *cpi)
31860Sstevel@tonic-gate {
31870Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
31880Sstevel@tonic-gate 	case X86_VENDOR_Intel:
31890Sstevel@tonic-gate 		if (cpi->cpi_maxeax >= 2)
31900Sstevel@tonic-gate 			return (X86_VENDOR_Intel);
31910Sstevel@tonic-gate 		break;
31920Sstevel@tonic-gate 	case X86_VENDOR_AMD:
31930Sstevel@tonic-gate 		/*
31940Sstevel@tonic-gate 		 * The K5 model 1 was the first part from AMD that reported
31950Sstevel@tonic-gate 		 * cache sizes via extended cpuid functions.
31960Sstevel@tonic-gate 		 */
31970Sstevel@tonic-gate 		if (cpi->cpi_family > 5 ||
31980Sstevel@tonic-gate 		    (cpi->cpi_family == 5 && cpi->cpi_model >= 1))
31990Sstevel@tonic-gate 			return (X86_VENDOR_AMD);
32000Sstevel@tonic-gate 		break;
32010Sstevel@tonic-gate 	case X86_VENDOR_TM:
32020Sstevel@tonic-gate 		if (cpi->cpi_family >= 5)
32030Sstevel@tonic-gate 			return (X86_VENDOR_AMD);
32040Sstevel@tonic-gate 		/*FALLTHROUGH*/
32050Sstevel@tonic-gate 	default:
32060Sstevel@tonic-gate 		/*
32070Sstevel@tonic-gate 		 * If they have extended CPU data for 0x80000005
32080Sstevel@tonic-gate 		 * then we assume they have AMD-format cache
32090Sstevel@tonic-gate 		 * information.
32100Sstevel@tonic-gate 		 *
32110Sstevel@tonic-gate 		 * If not, and the vendor happens to be Cyrix,
32120Sstevel@tonic-gate 		 * then try our-Cyrix specific handler.
32130Sstevel@tonic-gate 		 *
32140Sstevel@tonic-gate 		 * If we're not Cyrix, then assume we're using Intel's
32150Sstevel@tonic-gate 		 * table-driven format instead.
32160Sstevel@tonic-gate 		 */
32170Sstevel@tonic-gate 		if (cpi->cpi_xmaxeax >= 0x80000005)
32180Sstevel@tonic-gate 			return (X86_VENDOR_AMD);
32190Sstevel@tonic-gate 		else if (cpi->cpi_vendor == X86_VENDOR_Cyrix)
32200Sstevel@tonic-gate 			return (X86_VENDOR_Cyrix);
32210Sstevel@tonic-gate 		else if (cpi->cpi_maxeax >= 2)
32220Sstevel@tonic-gate 			return (X86_VENDOR_Intel);
32230Sstevel@tonic-gate 		break;
32240Sstevel@tonic-gate 	}
32250Sstevel@tonic-gate 	return (-1);
32260Sstevel@tonic-gate }
32270Sstevel@tonic-gate 
32280Sstevel@tonic-gate /*
32290Sstevel@tonic-gate  * create a node for the given cpu under the prom root node.
32300Sstevel@tonic-gate  * Also, create a cpu node in the device tree.
32310Sstevel@tonic-gate  */
32320Sstevel@tonic-gate static dev_info_t *cpu_nex_devi = NULL;
32330Sstevel@tonic-gate static kmutex_t cpu_node_lock;
32340Sstevel@tonic-gate 
32350Sstevel@tonic-gate /*
32360Sstevel@tonic-gate  * Called from post_startup() and mp_startup()
32370Sstevel@tonic-gate  */
32380Sstevel@tonic-gate void
32390Sstevel@tonic-gate add_cpunode2devtree(processorid_t cpu_id, struct cpuid_info *cpi)
32400Sstevel@tonic-gate {
32410Sstevel@tonic-gate 	dev_info_t *cpu_devi;
32420Sstevel@tonic-gate 	int create;
32430Sstevel@tonic-gate 
32440Sstevel@tonic-gate 	mutex_enter(&cpu_node_lock);
32450Sstevel@tonic-gate 
32460Sstevel@tonic-gate 	/*
32470Sstevel@tonic-gate 	 * create a nexus node for all cpus identified as 'cpu_id' under
32480Sstevel@tonic-gate 	 * the root node.
32490Sstevel@tonic-gate 	 */
32500Sstevel@tonic-gate 	if (cpu_nex_devi == NULL) {
32510Sstevel@tonic-gate 		if (ndi_devi_alloc(ddi_root_node(), "cpus",
3252789Sahrens 		    (pnode_t)DEVI_SID_NODEID, &cpu_nex_devi) != NDI_SUCCESS) {
32530Sstevel@tonic-gate 			mutex_exit(&cpu_node_lock);
32540Sstevel@tonic-gate 			return;
32550Sstevel@tonic-gate 		}
32560Sstevel@tonic-gate 		(void) ndi_devi_online(cpu_nex_devi, 0);
32570Sstevel@tonic-gate 	}
32580Sstevel@tonic-gate 
32590Sstevel@tonic-gate 	/*
32600Sstevel@tonic-gate 	 * create a child node for cpu identified as 'cpu_id'
32610Sstevel@tonic-gate 	 */
32620Sstevel@tonic-gate 	cpu_devi = ddi_add_child(cpu_nex_devi, "cpu", DEVI_SID_NODEID,
32634481Sbholler 	    cpu_id);
32640Sstevel@tonic-gate 	if (cpu_devi == NULL) {
32650Sstevel@tonic-gate 		mutex_exit(&cpu_node_lock);
32660Sstevel@tonic-gate 		return;
32670Sstevel@tonic-gate 	}
32680Sstevel@tonic-gate 
32690Sstevel@tonic-gate 	/* device_type */
32700Sstevel@tonic-gate 
32710Sstevel@tonic-gate 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, cpu_devi,
32720Sstevel@tonic-gate 	    "device_type", "cpu");
32730Sstevel@tonic-gate 
32740Sstevel@tonic-gate 	/* reg */
32750Sstevel@tonic-gate 
32760Sstevel@tonic-gate 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
32770Sstevel@tonic-gate 	    "reg", cpu_id);
32780Sstevel@tonic-gate 
32790Sstevel@tonic-gate 	/* cpu-mhz, and clock-frequency */
32800Sstevel@tonic-gate 
32810Sstevel@tonic-gate 	if (cpu_freq > 0) {
32820Sstevel@tonic-gate 		long long mul;
32830Sstevel@tonic-gate 
32840Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
32850Sstevel@tonic-gate 		    "cpu-mhz", cpu_freq);
32860Sstevel@tonic-gate 
32870Sstevel@tonic-gate 		if ((mul = cpu_freq * 1000000LL) <= INT_MAX)
32880Sstevel@tonic-gate 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
32890Sstevel@tonic-gate 			    "clock-frequency", (int)mul);
32900Sstevel@tonic-gate 	}
32910Sstevel@tonic-gate 
32920Sstevel@tonic-gate 	(void) ndi_devi_online(cpu_devi, 0);
32930Sstevel@tonic-gate 
32940Sstevel@tonic-gate 	if ((x86_feature & X86_CPUID) == 0) {
32950Sstevel@tonic-gate 		mutex_exit(&cpu_node_lock);
32960Sstevel@tonic-gate 		return;
32970Sstevel@tonic-gate 	}
32980Sstevel@tonic-gate 
32990Sstevel@tonic-gate 	/* vendor-id */
33000Sstevel@tonic-gate 
33010Sstevel@tonic-gate 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, cpu_devi,
33024481Sbholler 	    "vendor-id", cpi->cpi_vendorstr);
33030Sstevel@tonic-gate 
33040Sstevel@tonic-gate 	if (cpi->cpi_maxeax == 0) {
33050Sstevel@tonic-gate 		mutex_exit(&cpu_node_lock);
33060Sstevel@tonic-gate 		return;
33070Sstevel@tonic-gate 	}
33080Sstevel@tonic-gate 
33090Sstevel@tonic-gate 	/*
33100Sstevel@tonic-gate 	 * family, model, and step
33110Sstevel@tonic-gate 	 */
33120Sstevel@tonic-gate 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
33134481Sbholler 	    "family", CPI_FAMILY(cpi));
33140Sstevel@tonic-gate 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
33154481Sbholler 	    "cpu-model", CPI_MODEL(cpi));
33160Sstevel@tonic-gate 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
33174481Sbholler 	    "stepping-id", CPI_STEP(cpi));
33180Sstevel@tonic-gate 
33190Sstevel@tonic-gate 	/* type */
33200Sstevel@tonic-gate 
33210Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
33220Sstevel@tonic-gate 	case X86_VENDOR_Intel:
33230Sstevel@tonic-gate 		create = 1;
33240Sstevel@tonic-gate 		break;
33250Sstevel@tonic-gate 	default:
33260Sstevel@tonic-gate 		create = 0;
33270Sstevel@tonic-gate 		break;
33280Sstevel@tonic-gate 	}
33290Sstevel@tonic-gate 	if (create)
33300Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
33314481Sbholler 		    "type", CPI_TYPE(cpi));
33320Sstevel@tonic-gate 
33330Sstevel@tonic-gate 	/* ext-family */
33340Sstevel@tonic-gate 
33350Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
33360Sstevel@tonic-gate 	case X86_VENDOR_Intel:
33370Sstevel@tonic-gate 	case X86_VENDOR_AMD:
33380Sstevel@tonic-gate 		create = cpi->cpi_family >= 0xf;
33390Sstevel@tonic-gate 		break;
33400Sstevel@tonic-gate 	default:
33410Sstevel@tonic-gate 		create = 0;
33420Sstevel@tonic-gate 		break;
33430Sstevel@tonic-gate 	}
33440Sstevel@tonic-gate 	if (create)
33450Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
33460Sstevel@tonic-gate 		    "ext-family", CPI_FAMILY_XTD(cpi));
33470Sstevel@tonic-gate 
33480Sstevel@tonic-gate 	/* ext-model */
33490Sstevel@tonic-gate 
33500Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
33510Sstevel@tonic-gate 	case X86_VENDOR_Intel:
33522001Sdmick 		create = CPI_MODEL(cpi) == 0xf;
33532001Sdmick 		break;
33540Sstevel@tonic-gate 	case X86_VENDOR_AMD:
33551582Skchow 		create = CPI_FAMILY(cpi) == 0xf;
33560Sstevel@tonic-gate 		break;
33570Sstevel@tonic-gate 	default:
33580Sstevel@tonic-gate 		create = 0;
33590Sstevel@tonic-gate 		break;
33600Sstevel@tonic-gate 	}
33610Sstevel@tonic-gate 	if (create)
33620Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
33634481Sbholler 		    "ext-model", CPI_MODEL_XTD(cpi));
33640Sstevel@tonic-gate 
33650Sstevel@tonic-gate 	/* generation */
33660Sstevel@tonic-gate 
33670Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
33680Sstevel@tonic-gate 	case X86_VENDOR_AMD:
33690Sstevel@tonic-gate 		/*
33700Sstevel@tonic-gate 		 * AMD K5 model 1 was the first part to support this
33710Sstevel@tonic-gate 		 */
33720Sstevel@tonic-gate 		create = cpi->cpi_xmaxeax >= 0x80000001;
33730Sstevel@tonic-gate 		break;
33740Sstevel@tonic-gate 	default:
33750Sstevel@tonic-gate 		create = 0;
33760Sstevel@tonic-gate 		break;
33770Sstevel@tonic-gate 	}
33780Sstevel@tonic-gate 	if (create)
33790Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
33800Sstevel@tonic-gate 		    "generation", BITX((cpi)->cpi_extd[1].cp_eax, 11, 8));
33810Sstevel@tonic-gate 
33820Sstevel@tonic-gate 	/* brand-id */
33830Sstevel@tonic-gate 
33840Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
33850Sstevel@tonic-gate 	case X86_VENDOR_Intel:
33860Sstevel@tonic-gate 		/*
33870Sstevel@tonic-gate 		 * brand id first appeared on Pentium III Xeon model 8,
33880Sstevel@tonic-gate 		 * and Celeron model 8 processors and Opteron
33890Sstevel@tonic-gate 		 */
33900Sstevel@tonic-gate 		create = cpi->cpi_family > 6 ||
33910Sstevel@tonic-gate 		    (cpi->cpi_family == 6 && cpi->cpi_model >= 8);
33920Sstevel@tonic-gate 		break;
33930Sstevel@tonic-gate 	case X86_VENDOR_AMD:
33940Sstevel@tonic-gate 		create = cpi->cpi_family >= 0xf;
33950Sstevel@tonic-gate 		break;
33960Sstevel@tonic-gate 	default:
33970Sstevel@tonic-gate 		create = 0;
33980Sstevel@tonic-gate 		break;
33990Sstevel@tonic-gate 	}
34000Sstevel@tonic-gate 	if (create && cpi->cpi_brandid != 0) {
34010Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
34020Sstevel@tonic-gate 		    "brand-id", cpi->cpi_brandid);
34030Sstevel@tonic-gate 	}
34040Sstevel@tonic-gate 
34050Sstevel@tonic-gate 	/* chunks, and apic-id */
34060Sstevel@tonic-gate 
34070Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
34080Sstevel@tonic-gate 		/*
34090Sstevel@tonic-gate 		 * first available on Pentium IV and Opteron (K8)
34100Sstevel@tonic-gate 		 */
34111975Sdmick 	case X86_VENDOR_Intel:
34121975Sdmick 		create = IS_NEW_F6(cpi) || cpi->cpi_family >= 0xf;
34131975Sdmick 		break;
34141975Sdmick 	case X86_VENDOR_AMD:
34150Sstevel@tonic-gate 		create = cpi->cpi_family >= 0xf;
34160Sstevel@tonic-gate 		break;
34170Sstevel@tonic-gate 	default:
34180Sstevel@tonic-gate 		create = 0;
34190Sstevel@tonic-gate 		break;
34200Sstevel@tonic-gate 	}
34210Sstevel@tonic-gate 	if (create) {
34220Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
34234481Sbholler 		    "chunks", CPI_CHUNKS(cpi));
34240Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
34254481Sbholler 		    "apic-id", CPI_APIC_ID(cpi));
34261414Scindi 		if (cpi->cpi_chipid >= 0) {
34270Sstevel@tonic-gate 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
34280Sstevel@tonic-gate 			    "chip#", cpi->cpi_chipid);
34291414Scindi 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
34301414Scindi 			    "clog#", cpi->cpi_clogid);
34311414Scindi 		}
34320Sstevel@tonic-gate 	}
34330Sstevel@tonic-gate 
34340Sstevel@tonic-gate 	/* cpuid-features */
34350Sstevel@tonic-gate 
34360Sstevel@tonic-gate 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
34370Sstevel@tonic-gate 	    "cpuid-features", CPI_FEATURES_EDX(cpi));
34380Sstevel@tonic-gate 
34390Sstevel@tonic-gate 
34400Sstevel@tonic-gate 	/* cpuid-features-ecx */
34410Sstevel@tonic-gate 
34420Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
34430Sstevel@tonic-gate 	case X86_VENDOR_Intel:
34441975Sdmick 		create = IS_NEW_F6(cpi) || cpi->cpi_family >= 0xf;
34450Sstevel@tonic-gate 		break;
34460Sstevel@tonic-gate 	default:
34470Sstevel@tonic-gate 		create = 0;
34480Sstevel@tonic-gate 		break;
34490Sstevel@tonic-gate 	}
34500Sstevel@tonic-gate 	if (create)
34510Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
34520Sstevel@tonic-gate 		    "cpuid-features-ecx", CPI_FEATURES_ECX(cpi));
34530Sstevel@tonic-gate 
34540Sstevel@tonic-gate 	/* ext-cpuid-features */
34550Sstevel@tonic-gate 
34560Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
34571975Sdmick 	case X86_VENDOR_Intel:
34580Sstevel@tonic-gate 	case X86_VENDOR_AMD:
34590Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
34600Sstevel@tonic-gate 	case X86_VENDOR_TM:
34610Sstevel@tonic-gate 	case X86_VENDOR_Centaur:
34620Sstevel@tonic-gate 		create = cpi->cpi_xmaxeax >= 0x80000001;
34630Sstevel@tonic-gate 		break;
34640Sstevel@tonic-gate 	default:
34650Sstevel@tonic-gate 		create = 0;
34660Sstevel@tonic-gate 		break;
34670Sstevel@tonic-gate 	}
34681975Sdmick 	if (create) {
34690Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
34704481Sbholler 		    "ext-cpuid-features", CPI_FEATURES_XTD_EDX(cpi));
34711975Sdmick 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
34724481Sbholler 		    "ext-cpuid-features-ecx", CPI_FEATURES_XTD_ECX(cpi));
34731975Sdmick 	}
34740Sstevel@tonic-gate 
34750Sstevel@tonic-gate 	/*
34760Sstevel@tonic-gate 	 * Brand String first appeared in Intel Pentium IV, AMD K5
34770Sstevel@tonic-gate 	 * model 1, and Cyrix GXm.  On earlier models we try and
34780Sstevel@tonic-gate 	 * simulate something similar .. so this string should always
34790Sstevel@tonic-gate 	 * same -something- about the processor, however lame.
34800Sstevel@tonic-gate 	 */
34810Sstevel@tonic-gate 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, cpu_devi,
34820Sstevel@tonic-gate 	    "brand-string", cpi->cpi_brandstr);
34830Sstevel@tonic-gate 
34840Sstevel@tonic-gate 	/*
34850Sstevel@tonic-gate 	 * Finally, cache and tlb information
34860Sstevel@tonic-gate 	 */
34870Sstevel@tonic-gate 	switch (x86_which_cacheinfo(cpi)) {
34880Sstevel@tonic-gate 	case X86_VENDOR_Intel:
34890Sstevel@tonic-gate 		intel_walk_cacheinfo(cpi, cpu_devi, add_cacheent_props);
34900Sstevel@tonic-gate 		break;
34910Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
34920Sstevel@tonic-gate 		cyrix_walk_cacheinfo(cpi, cpu_devi, add_cacheent_props);
34930Sstevel@tonic-gate 		break;
34940Sstevel@tonic-gate 	case X86_VENDOR_AMD:
34950Sstevel@tonic-gate 		amd_cache_info(cpi, cpu_devi);
34960Sstevel@tonic-gate 		break;
34970Sstevel@tonic-gate 	default:
34980Sstevel@tonic-gate 		break;
34990Sstevel@tonic-gate 	}
35000Sstevel@tonic-gate 
35010Sstevel@tonic-gate 	mutex_exit(&cpu_node_lock);
35020Sstevel@tonic-gate }
35030Sstevel@tonic-gate 
35040Sstevel@tonic-gate struct l2info {
35050Sstevel@tonic-gate 	int *l2i_csz;
35060Sstevel@tonic-gate 	int *l2i_lsz;
35070Sstevel@tonic-gate 	int *l2i_assoc;
35080Sstevel@tonic-gate 	int l2i_ret;
35090Sstevel@tonic-gate };
35100Sstevel@tonic-gate 
35110Sstevel@tonic-gate /*
35120Sstevel@tonic-gate  * A cacheinfo walker that fetches the size, line-size and associativity
35130Sstevel@tonic-gate  * of the L2 cache
35140Sstevel@tonic-gate  */
35150Sstevel@tonic-gate static int
35160Sstevel@tonic-gate intel_l2cinfo(void *arg, const struct cachetab *ct)
35170Sstevel@tonic-gate {
35180Sstevel@tonic-gate 	struct l2info *l2i = arg;
35190Sstevel@tonic-gate 	int *ip;
35200Sstevel@tonic-gate 
35210Sstevel@tonic-gate 	if (ct->ct_label != l2_cache_str &&
35220Sstevel@tonic-gate 	    ct->ct_label != sl2_cache_str)
35230Sstevel@tonic-gate 		return (0);	/* not an L2 -- keep walking */
35240Sstevel@tonic-gate 
35250Sstevel@tonic-gate 	if ((ip = l2i->l2i_csz) != NULL)
35260Sstevel@tonic-gate 		*ip = ct->ct_size;
35270Sstevel@tonic-gate 	if ((ip = l2i->l2i_lsz) != NULL)
35280Sstevel@tonic-gate 		*ip = ct->ct_line_size;
35290Sstevel@tonic-gate 	if ((ip = l2i->l2i_assoc) != NULL)
35300Sstevel@tonic-gate 		*ip = ct->ct_assoc;
35310Sstevel@tonic-gate 	l2i->l2i_ret = ct->ct_size;
35320Sstevel@tonic-gate 	return (1);		/* was an L2 -- terminate walk */
35330Sstevel@tonic-gate }
35340Sstevel@tonic-gate 
35355070Skchow /*
35365070Skchow  * AMD L2/L3 Cache and TLB Associativity Field Definition:
35375070Skchow  *
35385070Skchow  *	Unlike the associativity for the L1 cache and tlb where the 8 bit
35395070Skchow  *	value is the associativity, the associativity for the L2 cache and
35405070Skchow  *	tlb is encoded in the following table. The 4 bit L2 value serves as
35415070Skchow  *	an index into the amd_afd[] array to determine the associativity.
35425070Skchow  *	-1 is undefined. 0 is fully associative.
35435070Skchow  */
35445070Skchow 
35455070Skchow static int amd_afd[] =
35465070Skchow 	{-1, 1, 2, -1, 4, -1, 8, -1, 16, -1, 32, 48, 64, 96, 128, 0};
35475070Skchow 
35480Sstevel@tonic-gate static void
35490Sstevel@tonic-gate amd_l2cacheinfo(struct cpuid_info *cpi, struct l2info *l2i)
35500Sstevel@tonic-gate {
35511228Sandrei 	struct cpuid_regs *cp;
35520Sstevel@tonic-gate 	uint_t size, assoc;
35535070Skchow 	int i;
35540Sstevel@tonic-gate 	int *ip;
35550Sstevel@tonic-gate 
35560Sstevel@tonic-gate 	if (cpi->cpi_xmaxeax < 0x80000006)
35570Sstevel@tonic-gate 		return;
35580Sstevel@tonic-gate 	cp = &cpi->cpi_extd[6];
35590Sstevel@tonic-gate 
35605070Skchow 	if ((i = BITX(cp->cp_ecx, 15, 12)) != 0 &&
35610Sstevel@tonic-gate 	    (size = BITX(cp->cp_ecx, 31, 16)) != 0) {
35620Sstevel@tonic-gate 		uint_t cachesz = size * 1024;
35635070Skchow 		assoc = amd_afd[i];
35645070Skchow 
35655070Skchow 		ASSERT(assoc != -1);
35660Sstevel@tonic-gate 
35670Sstevel@tonic-gate 		if ((ip = l2i->l2i_csz) != NULL)
35680Sstevel@tonic-gate 			*ip = cachesz;
35690Sstevel@tonic-gate 		if ((ip = l2i->l2i_lsz) != NULL)
35700Sstevel@tonic-gate 			*ip = BITX(cp->cp_ecx, 7, 0);
35710Sstevel@tonic-gate 		if ((ip = l2i->l2i_assoc) != NULL)
35720Sstevel@tonic-gate 			*ip = assoc;
35730Sstevel@tonic-gate 		l2i->l2i_ret = cachesz;
35740Sstevel@tonic-gate 	}
35750Sstevel@tonic-gate }
35760Sstevel@tonic-gate 
35770Sstevel@tonic-gate int
35780Sstevel@tonic-gate getl2cacheinfo(cpu_t *cpu, int *csz, int *lsz, int *assoc)
35790Sstevel@tonic-gate {
35800Sstevel@tonic-gate 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
35810Sstevel@tonic-gate 	struct l2info __l2info, *l2i = &__l2info;
35820Sstevel@tonic-gate 
35830Sstevel@tonic-gate 	l2i->l2i_csz = csz;
35840Sstevel@tonic-gate 	l2i->l2i_lsz = lsz;
35850Sstevel@tonic-gate 	l2i->l2i_assoc = assoc;
35860Sstevel@tonic-gate 	l2i->l2i_ret = -1;
35870Sstevel@tonic-gate 
35880Sstevel@tonic-gate 	switch (x86_which_cacheinfo(cpi)) {
35890Sstevel@tonic-gate 	case X86_VENDOR_Intel:
35900Sstevel@tonic-gate 		intel_walk_cacheinfo(cpi, l2i, intel_l2cinfo);
35910Sstevel@tonic-gate 		break;
35920Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
35930Sstevel@tonic-gate 		cyrix_walk_cacheinfo(cpi, l2i, intel_l2cinfo);
35940Sstevel@tonic-gate 		break;
35950Sstevel@tonic-gate 	case X86_VENDOR_AMD:
35960Sstevel@tonic-gate 		amd_l2cacheinfo(cpi, l2i);
35970Sstevel@tonic-gate 		break;
35980Sstevel@tonic-gate 	default:
35990Sstevel@tonic-gate 		break;
36000Sstevel@tonic-gate 	}
36010Sstevel@tonic-gate 	return (l2i->l2i_ret);
36020Sstevel@tonic-gate }
36034481Sbholler 
36045084Sjohnlev #if !defined(__xpv)
36055084Sjohnlev 
36065045Sbholler uint32_t *
36075045Sbholler cpuid_mwait_alloc(cpu_t *cpu)
36085045Sbholler {
36095045Sbholler 	uint32_t	*ret;
36105045Sbholler 	size_t		mwait_size;
36115045Sbholler 
36125045Sbholler 	ASSERT(cpuid_checkpass(cpu, 2));
36135045Sbholler 
36145045Sbholler 	mwait_size = cpu->cpu_m.mcpu_cpi->cpi_mwait.mon_max;
36155045Sbholler 	if (mwait_size == 0)
36165045Sbholler 		return (NULL);
36175045Sbholler 
36185045Sbholler 	/*
36195045Sbholler 	 * kmem_alloc() returns cache line size aligned data for mwait_size
36205045Sbholler 	 * allocations.  mwait_size is currently cache line sized.  Neither
36215045Sbholler 	 * of these implementation details are guarantied to be true in the
36225045Sbholler 	 * future.
36235045Sbholler 	 *
36245045Sbholler 	 * First try allocating mwait_size as kmem_alloc() currently returns
36255045Sbholler 	 * correctly aligned memory.  If kmem_alloc() does not return
36265045Sbholler 	 * mwait_size aligned memory, then use mwait_size ROUNDUP.
36275045Sbholler 	 *
36285045Sbholler 	 * Set cpi_mwait.buf_actual and cpi_mwait.size_actual in case we
36295045Sbholler 	 * decide to free this memory.
36305045Sbholler 	 */
36315045Sbholler 	ret = kmem_zalloc(mwait_size, KM_SLEEP);
36325045Sbholler 	if (ret == (uint32_t *)P2ROUNDUP((uintptr_t)ret, mwait_size)) {
36335045Sbholler 		cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual = ret;
36345045Sbholler 		cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual = mwait_size;
36355045Sbholler 		*ret = MWAIT_RUNNING;
36365045Sbholler 		return (ret);
36375045Sbholler 	} else {
36385045Sbholler 		kmem_free(ret, mwait_size);
36395045Sbholler 		ret = kmem_zalloc(mwait_size * 2, KM_SLEEP);
36405045Sbholler 		cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual = ret;
36415045Sbholler 		cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual = mwait_size * 2;
36425045Sbholler 		ret = (uint32_t *)P2ROUNDUP((uintptr_t)ret, mwait_size);
36435045Sbholler 		*ret = MWAIT_RUNNING;
36445045Sbholler 		return (ret);
36455045Sbholler 	}
36465045Sbholler }
36475045Sbholler 
36485045Sbholler void
36495045Sbholler cpuid_mwait_free(cpu_t *cpu)
36504481Sbholler {
36514481Sbholler 	ASSERT(cpuid_checkpass(cpu, 2));
36525045Sbholler 
36535045Sbholler 	if (cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual != NULL &&
36545045Sbholler 	    cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual > 0) {
36555045Sbholler 		kmem_free(cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual,
36565045Sbholler 		    cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual);
36575045Sbholler 	}
36585045Sbholler 
36595045Sbholler 	cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual = NULL;
36605045Sbholler 	cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual = 0;
36614481Sbholler }
36625084Sjohnlev 
36635084Sjohnlev #endif	/* !__xpv */
3664