xref: /onnv-gate/usr/src/uts/i86pc/os/cpuid.c (revision 7716:44c2ec1382be)
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 /*
225870Sgavinm  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * Various routines to handle identification
280Sstevel@tonic-gate  * and classification of x86 processors.
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <sys/archsystm.h>
330Sstevel@tonic-gate #include <sys/x86_archext.h>
340Sstevel@tonic-gate #include <sys/kmem.h>
350Sstevel@tonic-gate #include <sys/systm.h>
360Sstevel@tonic-gate #include <sys/cmn_err.h>
370Sstevel@tonic-gate #include <sys/sunddi.h>
380Sstevel@tonic-gate #include <sys/sunndi.h>
390Sstevel@tonic-gate #include <sys/cpuvar.h>
400Sstevel@tonic-gate #include <sys/processor.h>
415045Sbholler #include <sys/sysmacros.h>
423434Sesaxe #include <sys/pg.h>
430Sstevel@tonic-gate #include <sys/fp.h>
440Sstevel@tonic-gate #include <sys/controlregs.h>
450Sstevel@tonic-gate #include <sys/auxv_386.h>
460Sstevel@tonic-gate #include <sys/bitmap.h>
470Sstevel@tonic-gate #include <sys/memnode.h>
480Sstevel@tonic-gate 
497532SSean.Ye@Sun.COM #ifdef __xpv
507532SSean.Ye@Sun.COM #include <sys/hypervisor.h>
517532SSean.Ye@Sun.COM #endif
527532SSean.Ye@Sun.COM 
530Sstevel@tonic-gate /*
540Sstevel@tonic-gate  * Pass 0 of cpuid feature analysis happens in locore. It contains special code
550Sstevel@tonic-gate  * to recognize Cyrix processors that are not cpuid-compliant, and to deal with
560Sstevel@tonic-gate  * them accordingly. For most modern processors, feature detection occurs here
570Sstevel@tonic-gate  * in pass 1.
580Sstevel@tonic-gate  *
590Sstevel@tonic-gate  * Pass 1 of cpuid feature analysis happens just at the beginning of mlsetup()
600Sstevel@tonic-gate  * for the boot CPU and does the basic analysis that the early kernel needs.
610Sstevel@tonic-gate  * x86_feature is set based on the return value of cpuid_pass1() of the boot
620Sstevel@tonic-gate  * CPU.
630Sstevel@tonic-gate  *
640Sstevel@tonic-gate  * Pass 1 includes:
650Sstevel@tonic-gate  *
660Sstevel@tonic-gate  *	o Determining vendor/model/family/stepping and setting x86_type and
670Sstevel@tonic-gate  *	  x86_vendor accordingly.
680Sstevel@tonic-gate  *	o Processing the feature flags returned by the cpuid instruction while
690Sstevel@tonic-gate  *	  applying any workarounds or tricks for the specific processor.
700Sstevel@tonic-gate  *	o Mapping the feature flags into Solaris feature bits (X86_*).
710Sstevel@tonic-gate  *	o Processing extended feature flags if supported by the processor,
720Sstevel@tonic-gate  *	  again while applying specific processor knowledge.
730Sstevel@tonic-gate  *	o Determining the CMT characteristics of the system.
740Sstevel@tonic-gate  *
750Sstevel@tonic-gate  * Pass 1 is done on non-boot CPUs during their initialization and the results
760Sstevel@tonic-gate  * are used only as a meager attempt at ensuring that all processors within the
770Sstevel@tonic-gate  * system support the same features.
780Sstevel@tonic-gate  *
790Sstevel@tonic-gate  * Pass 2 of cpuid feature analysis happens just at the beginning
800Sstevel@tonic-gate  * of startup().  It just copies in and corrects the remainder
810Sstevel@tonic-gate  * of the cpuid data we depend on: standard cpuid functions that we didn't
820Sstevel@tonic-gate  * need for pass1 feature analysis, and extended cpuid functions beyond the
830Sstevel@tonic-gate  * simple feature processing done in pass1.
840Sstevel@tonic-gate  *
850Sstevel@tonic-gate  * Pass 3 of cpuid analysis is invoked after basic kernel services; in
860Sstevel@tonic-gate  * particular kernel memory allocation has been made available. It creates a
870Sstevel@tonic-gate  * readable brand string based on the data collected in the first two passes.
880Sstevel@tonic-gate  *
890Sstevel@tonic-gate  * Pass 4 of cpuid analysis is invoked after post_startup() when all
900Sstevel@tonic-gate  * the support infrastructure for various hardware features has been
910Sstevel@tonic-gate  * initialized. It determines which processor features will be reported
920Sstevel@tonic-gate  * to userland via the aux vector.
930Sstevel@tonic-gate  *
940Sstevel@tonic-gate  * All passes are executed on all CPUs, but only the boot CPU determines what
950Sstevel@tonic-gate  * features the kernel will use.
960Sstevel@tonic-gate  *
970Sstevel@tonic-gate  * Much of the worst junk in this file is for the support of processors
980Sstevel@tonic-gate  * that didn't really implement the cpuid instruction properly.
990Sstevel@tonic-gate  *
1000Sstevel@tonic-gate  * NOTE: The accessor functions (cpuid_get*) are aware of, and ASSERT upon,
1010Sstevel@tonic-gate  * the pass numbers.  Accordingly, changes to the pass code may require changes
1020Sstevel@tonic-gate  * to the accessor code.
1030Sstevel@tonic-gate  */
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate uint_t x86_feature = 0;
1060Sstevel@tonic-gate uint_t x86_vendor = X86_VENDOR_IntelClone;
1070Sstevel@tonic-gate uint_t x86_type = X86_TYPE_OTHER;
1087589SVikram.Hegde@Sun.COM uint_t x86_clflush_size = 0;
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate uint_t pentiumpro_bug4046376;
1110Sstevel@tonic-gate uint_t pentiumpro_bug4064495;
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate uint_t enable486;
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate /*
1164481Sbholler  * monitor/mwait info.
1175045Sbholler  *
1185045Sbholler  * size_actual and buf_actual are the real address and size allocated to get
1195045Sbholler  * proper mwait_buf alignement.  buf_actual and size_actual should be passed
1205045Sbholler  * to kmem_free().  Currently kmem_alloc() and mwait happen to both use
1215045Sbholler  * processor cache-line alignment, but this is not guarantied in the furture.
1224481Sbholler  */
1234481Sbholler struct mwait_info {
1244481Sbholler 	size_t		mon_min;	/* min size to avoid missed wakeups */
1254481Sbholler 	size_t		mon_max;	/* size to avoid false wakeups */
1265045Sbholler 	size_t		size_actual;	/* size actually allocated */
1275045Sbholler 	void		*buf_actual;	/* memory actually allocated */
1284481Sbholler 	uint32_t	support;	/* processor support of monitor/mwait */
1294481Sbholler };
1304481Sbholler 
1314481Sbholler /*
1320Sstevel@tonic-gate  * These constants determine how many of the elements of the
1330Sstevel@tonic-gate  * cpuid we cache in the cpuid_info data structure; the
1340Sstevel@tonic-gate  * remaining elements are accessible via the cpuid instruction.
1350Sstevel@tonic-gate  */
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate #define	NMAX_CPI_STD	6		/* eax = 0 .. 5 */
1380Sstevel@tonic-gate #define	NMAX_CPI_EXTD	9		/* eax = 0x80000000 .. 0x80000008 */
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate struct cpuid_info {
1410Sstevel@tonic-gate 	uint_t cpi_pass;		/* last pass completed */
1420Sstevel@tonic-gate 	/*
1430Sstevel@tonic-gate 	 * standard function information
1440Sstevel@tonic-gate 	 */
1450Sstevel@tonic-gate 	uint_t cpi_maxeax;		/* fn 0: %eax */
1460Sstevel@tonic-gate 	char cpi_vendorstr[13];		/* fn 0: %ebx:%ecx:%edx */
1470Sstevel@tonic-gate 	uint_t cpi_vendor;		/* enum of cpi_vendorstr */
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	uint_t cpi_family;		/* fn 1: extended family */
1500Sstevel@tonic-gate 	uint_t cpi_model;		/* fn 1: extended model */
1510Sstevel@tonic-gate 	uint_t cpi_step;		/* fn 1: stepping */
1520Sstevel@tonic-gate 	chipid_t cpi_chipid;		/* fn 1: %ebx: chip # on ht cpus */
1530Sstevel@tonic-gate 	uint_t cpi_brandid;		/* fn 1: %ebx: brand ID */
1540Sstevel@tonic-gate 	int cpi_clogid;			/* fn 1: %ebx: thread # */
1551228Sandrei 	uint_t cpi_ncpu_per_chip;	/* fn 1: %ebx: logical cpu count */
1560Sstevel@tonic-gate 	uint8_t cpi_cacheinfo[16];	/* fn 2: intel-style cache desc */
1570Sstevel@tonic-gate 	uint_t cpi_ncache;		/* fn 2: number of elements */
1584606Sesaxe 	uint_t cpi_ncpu_shr_last_cache;	/* fn 4: %eax: ncpus sharing cache */
1594606Sesaxe 	id_t cpi_last_lvl_cacheid;	/* fn 4: %eax: derived cache id */
1604606Sesaxe 	uint_t cpi_std_4_size;		/* fn 4: number of fn 4 elements */
1614606Sesaxe 	struct cpuid_regs **cpi_std_4;	/* fn 4: %ecx == 0 .. fn4_size */
1621228Sandrei 	struct cpuid_regs cpi_std[NMAX_CPI_STD];	/* 0 .. 5 */
1630Sstevel@tonic-gate 	/*
1640Sstevel@tonic-gate 	 * extended function information
1650Sstevel@tonic-gate 	 */
1660Sstevel@tonic-gate 	uint_t cpi_xmaxeax;		/* fn 0x80000000: %eax */
1670Sstevel@tonic-gate 	char cpi_brandstr[49];		/* fn 0x8000000[234] */
1680Sstevel@tonic-gate 	uint8_t cpi_pabits;		/* fn 0x80000006: %eax */
1690Sstevel@tonic-gate 	uint8_t cpi_vabits;		/* fn 0x80000006: %eax */
1701228Sandrei 	struct cpuid_regs cpi_extd[NMAX_CPI_EXTD]; /* 0x8000000[0-8] */
1715870Sgavinm 	id_t cpi_coreid;		/* same coreid => strands share core */
1725870Sgavinm 	int cpi_pkgcoreid;		/* core number within single package */
1731228Sandrei 	uint_t cpi_ncore_per_chip;	/* AMD: fn 0x80000008: %ecx[7-0] */
1741228Sandrei 					/* Intel: fn 4: %eax[31-26] */
1750Sstevel@tonic-gate 	/*
1760Sstevel@tonic-gate 	 * supported feature information
1770Sstevel@tonic-gate 	 */
1783446Smrj 	uint32_t cpi_support[5];
1790Sstevel@tonic-gate #define	STD_EDX_FEATURES	0
1800Sstevel@tonic-gate #define	AMD_EDX_FEATURES	1
1810Sstevel@tonic-gate #define	TM_EDX_FEATURES		2
1820Sstevel@tonic-gate #define	STD_ECX_FEATURES	3
1833446Smrj #define	AMD_ECX_FEATURES	4
1842869Sgavinm 	/*
1852869Sgavinm 	 * Synthesized information, where known.
1862869Sgavinm 	 */
1872869Sgavinm 	uint32_t cpi_chiprev;		/* See X86_CHIPREV_* in x86_archext.h */
1882869Sgavinm 	const char *cpi_chiprevstr;	/* May be NULL if chiprev unknown */
1892869Sgavinm 	uint32_t cpi_socket;		/* Chip package/socket type */
1904481Sbholler 
1914481Sbholler 	struct mwait_info cpi_mwait;	/* fn 5: monitor/mwait info */
1927282Smishra 	uint32_t cpi_apicid;
1930Sstevel@tonic-gate };
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate static struct cpuid_info cpuid_info0;
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate /*
1990Sstevel@tonic-gate  * These bit fields are defined by the Intel Application Note AP-485
2000Sstevel@tonic-gate  * "Intel Processor Identification and the CPUID Instruction"
2010Sstevel@tonic-gate  */
2020Sstevel@tonic-gate #define	CPI_FAMILY_XTD(cpi)	BITX((cpi)->cpi_std[1].cp_eax, 27, 20)
2030Sstevel@tonic-gate #define	CPI_MODEL_XTD(cpi)	BITX((cpi)->cpi_std[1].cp_eax, 19, 16)
2040Sstevel@tonic-gate #define	CPI_TYPE(cpi)		BITX((cpi)->cpi_std[1].cp_eax, 13, 12)
2050Sstevel@tonic-gate #define	CPI_FAMILY(cpi)		BITX((cpi)->cpi_std[1].cp_eax, 11, 8)
2060Sstevel@tonic-gate #define	CPI_STEP(cpi)		BITX((cpi)->cpi_std[1].cp_eax, 3, 0)
2070Sstevel@tonic-gate #define	CPI_MODEL(cpi)		BITX((cpi)->cpi_std[1].cp_eax, 7, 4)
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate #define	CPI_FEATURES_EDX(cpi)		((cpi)->cpi_std[1].cp_edx)
2100Sstevel@tonic-gate #define	CPI_FEATURES_ECX(cpi)		((cpi)->cpi_std[1].cp_ecx)
2110Sstevel@tonic-gate #define	CPI_FEATURES_XTD_EDX(cpi)	((cpi)->cpi_extd[1].cp_edx)
2120Sstevel@tonic-gate #define	CPI_FEATURES_XTD_ECX(cpi)	((cpi)->cpi_extd[1].cp_ecx)
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate #define	CPI_BRANDID(cpi)	BITX((cpi)->cpi_std[1].cp_ebx, 7, 0)
2150Sstevel@tonic-gate #define	CPI_CHUNKS(cpi)		BITX((cpi)->cpi_std[1].cp_ebx, 15, 7)
2160Sstevel@tonic-gate #define	CPI_CPU_COUNT(cpi)	BITX((cpi)->cpi_std[1].cp_ebx, 23, 16)
2170Sstevel@tonic-gate #define	CPI_APIC_ID(cpi)	BITX((cpi)->cpi_std[1].cp_ebx, 31, 24)
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate #define	CPI_MAXEAX_MAX		0x100		/* sanity control */
2200Sstevel@tonic-gate #define	CPI_XMAXEAX_MAX		0x80000100
2214606Sesaxe #define	CPI_FN4_ECX_MAX		0x20		/* sanity: max fn 4 levels */
2227282Smishra #define	CPI_FNB_ECX_MAX		0x20		/* sanity: max fn B 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)
2347282Smishra #define	CPI_CPU_LEVEL_TYPE(regs)	BITX((regs)->cp_ecx, 15, 8)
2354606Sesaxe 
2364606Sesaxe #define	CPI_CACHE_WAYS(regs)		BITX((regs)->cp_ebx, 31, 22)
2374606Sesaxe #define	CPI_CACHE_PARTS(regs)		BITX((regs)->cp_ebx, 21, 12)
2384606Sesaxe #define	CPI_CACHE_COH_LN_SZ(regs)	BITX((regs)->cp_ebx, 11, 0)
2394606Sesaxe 
2404606Sesaxe #define	CPI_CACHE_SETS(regs)		BITX((regs)->cp_ecx, 31, 0)
2414606Sesaxe 
2424606Sesaxe #define	CPI_PREFCH_STRIDE(regs)		BITX((regs)->cp_edx, 9, 0)
2434606Sesaxe 
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate /*
2461975Sdmick  * A couple of shorthand macros to identify "later" P6-family chips
2471975Sdmick  * like the Pentium M and Core.  First, the "older" P6-based stuff
2481975Sdmick  * (loosely defined as "pre-Pentium-4"):
2491975Sdmick  * P6, PII, Mobile PII, PII Xeon, PIII, Mobile PIII, PIII Xeon
2501975Sdmick  */
2511975Sdmick 
2521975Sdmick #define	IS_LEGACY_P6(cpi) (			\
2531975Sdmick 	cpi->cpi_family == 6 && 		\
2541975Sdmick 		(cpi->cpi_model == 1 ||		\
2551975Sdmick 		cpi->cpi_model == 3 ||		\
2561975Sdmick 		cpi->cpi_model == 5 ||		\
2571975Sdmick 		cpi->cpi_model == 6 ||		\
2581975Sdmick 		cpi->cpi_model == 7 ||		\
2591975Sdmick 		cpi->cpi_model == 8 ||		\
2601975Sdmick 		cpi->cpi_model == 0xA ||	\
2611975Sdmick 		cpi->cpi_model == 0xB)		\
2621975Sdmick )
2631975Sdmick 
2641975Sdmick /* A "new F6" is everything with family 6 that's not the above */
2651975Sdmick #define	IS_NEW_F6(cpi) ((cpi->cpi_family == 6) && !IS_LEGACY_P6(cpi))
2661975Sdmick 
2674855Sksadhukh /* Extended family/model support */
2684855Sksadhukh #define	IS_EXTENDED_MODEL_INTEL(cpi) (cpi->cpi_family == 0x6 || \
2694855Sksadhukh 	cpi->cpi_family >= 0xf)
2704855Sksadhukh 
2711975Sdmick /*
2724481Sbholler  * Info for monitor/mwait idle loop.
2734481Sbholler  *
2744481Sbholler  * See cpuid section of "Intel 64 and IA-32 Architectures Software Developer's
2754481Sbholler  * Manual Volume 2A: Instruction Set Reference, A-M" #25366-022US, November
2764481Sbholler  * 2006.
2774481Sbholler  * See MONITOR/MWAIT section of "AMD64 Architecture Programmer's Manual
2784481Sbholler  * Documentation Updates" #33633, Rev 2.05, December 2006.
2794481Sbholler  */
2804481Sbholler #define	MWAIT_SUPPORT		(0x00000001)	/* mwait supported */
2814481Sbholler #define	MWAIT_EXTENSIONS	(0x00000002)	/* extenstion supported */
2824481Sbholler #define	MWAIT_ECX_INT_ENABLE	(0x00000004)	/* ecx 1 extension supported */
2834481Sbholler #define	MWAIT_SUPPORTED(cpi)	((cpi)->cpi_std[1].cp_ecx & CPUID_INTC_ECX_MON)
2844481Sbholler #define	MWAIT_INT_ENABLE(cpi)	((cpi)->cpi_std[5].cp_ecx & 0x2)
2854481Sbholler #define	MWAIT_EXTENSION(cpi)	((cpi)->cpi_std[5].cp_ecx & 0x1)
2864481Sbholler #define	MWAIT_SIZE_MIN(cpi)	BITX((cpi)->cpi_std[5].cp_eax, 15, 0)
2874481Sbholler #define	MWAIT_SIZE_MAX(cpi)	BITX((cpi)->cpi_std[5].cp_ebx, 15, 0)
2884481Sbholler /*
2894481Sbholler  * Number of sub-cstates for a given c-state.
2904481Sbholler  */
2914481Sbholler #define	MWAIT_NUM_SUBC_STATES(cpi, c_state)			\
2924481Sbholler 	BITX((cpi)->cpi_std[5].cp_edx, c_state + 3, c_state)
2934481Sbholler 
2947532SSean.Ye@Sun.COM /*
2957532SSean.Ye@Sun.COM  * Functions we consune from cpuid_subr.c;  don't publish these in a header
2967532SSean.Ye@Sun.COM  * file to try and keep people using the expected cpuid_* interfaces.
2977532SSean.Ye@Sun.COM  */
2987532SSean.Ye@Sun.COM extern uint32_t _cpuid_skt(uint_t, uint_t, uint_t, uint_t);
2997532SSean.Ye@Sun.COM extern uint32_t _cpuid_chiprev(uint_t, uint_t, uint_t, uint_t);
3007532SSean.Ye@Sun.COM extern const char *_cpuid_chiprevstr(uint_t, uint_t, uint_t, uint_t);
3017532SSean.Ye@Sun.COM extern uint_t _cpuid_vendorstr_to_vendorcode(char *);
3022869Sgavinm 
3032869Sgavinm /*
3043446Smrj  * Apply up various platform-dependent restrictions where the
3053446Smrj  * underlying platform restrictions mean the CPU can be marked
3063446Smrj  * as less capable than its cpuid instruction would imply.
3073446Smrj  */
3085084Sjohnlev #if defined(__xpv)
3095084Sjohnlev static void
3105084Sjohnlev platform_cpuid_mangle(uint_t vendor, uint32_t eax, struct cpuid_regs *cp)
3115084Sjohnlev {
3125084Sjohnlev 	switch (eax) {
3137532SSean.Ye@Sun.COM 	case 1: {
3147532SSean.Ye@Sun.COM 		uint32_t mcamask = DOMAIN_IS_INITDOMAIN(xen_info) ?
3157532SSean.Ye@Sun.COM 		    0 : CPUID_INTC_EDX_MCA;
3165084Sjohnlev 		cp->cp_edx &=
3177532SSean.Ye@Sun.COM 		    ~(mcamask |
3187532SSean.Ye@Sun.COM 		    CPUID_INTC_EDX_PSE |
3195084Sjohnlev 		    CPUID_INTC_EDX_VME | CPUID_INTC_EDX_DE |
3205084Sjohnlev 		    CPUID_INTC_EDX_SEP | CPUID_INTC_EDX_MTRR |
3215084Sjohnlev 		    CPUID_INTC_EDX_PGE | CPUID_INTC_EDX_PAT |
3225084Sjohnlev 		    CPUID_AMD_EDX_SYSC | CPUID_INTC_EDX_SEP |
3235084Sjohnlev 		    CPUID_INTC_EDX_PSE36 | CPUID_INTC_EDX_HTT);
3245084Sjohnlev 		break;
3257532SSean.Ye@Sun.COM 	}
3265084Sjohnlev 
3275084Sjohnlev 	case 0x80000001:
3285084Sjohnlev 		cp->cp_edx &=
3295084Sjohnlev 		    ~(CPUID_AMD_EDX_PSE |
3305084Sjohnlev 		    CPUID_INTC_EDX_VME | CPUID_INTC_EDX_DE |
3315084Sjohnlev 		    CPUID_AMD_EDX_MTRR | CPUID_AMD_EDX_PGE |
3325084Sjohnlev 		    CPUID_AMD_EDX_PAT | CPUID_AMD_EDX_PSE36 |
3335084Sjohnlev 		    CPUID_AMD_EDX_SYSC | CPUID_INTC_EDX_SEP |
3345084Sjohnlev 		    CPUID_AMD_EDX_TSCP);
3355084Sjohnlev 		cp->cp_ecx &= ~CPUID_AMD_ECX_CMP_LGCY;
3365084Sjohnlev 		break;
3375084Sjohnlev 	default:
3385084Sjohnlev 		break;
3395084Sjohnlev 	}
3405084Sjohnlev 
3415084Sjohnlev 	switch (vendor) {
3425084Sjohnlev 	case X86_VENDOR_Intel:
3435084Sjohnlev 		switch (eax) {
3445084Sjohnlev 		case 4:
3455084Sjohnlev 			/*
3465084Sjohnlev 			 * Zero out the (ncores-per-chip - 1) field
3475084Sjohnlev 			 */
3485084Sjohnlev 			cp->cp_eax &= 0x03fffffff;
3495084Sjohnlev 			break;
3505084Sjohnlev 		default:
3515084Sjohnlev 			break;
3525084Sjohnlev 		}
3535084Sjohnlev 		break;
3545084Sjohnlev 	case X86_VENDOR_AMD:
3555084Sjohnlev 		switch (eax) {
3565084Sjohnlev 		case 0x80000008:
3575084Sjohnlev 			/*
3585084Sjohnlev 			 * Zero out the (ncores-per-chip - 1) field
3595084Sjohnlev 			 */
3605084Sjohnlev 			cp->cp_ecx &= 0xffffff00;
3615084Sjohnlev 			break;
3625084Sjohnlev 		default:
3635084Sjohnlev 			break;
3645084Sjohnlev 		}
3655084Sjohnlev 		break;
3665084Sjohnlev 	default:
3675084Sjohnlev 		break;
3685084Sjohnlev 	}
3695084Sjohnlev }
3705084Sjohnlev #else
3713446Smrj #define	platform_cpuid_mangle(vendor, eax, cp)	/* nothing */
3725084Sjohnlev #endif
3733446Smrj 
3743446Smrj /*
3750Sstevel@tonic-gate  *  Some undocumented ways of patching the results of the cpuid
3760Sstevel@tonic-gate  *  instruction to permit running Solaris 10 on future cpus that
3770Sstevel@tonic-gate  *  we don't currently support.  Could be set to non-zero values
3780Sstevel@tonic-gate  *  via settings in eeprom.
3790Sstevel@tonic-gate  */
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate uint32_t cpuid_feature_ecx_include;
3820Sstevel@tonic-gate uint32_t cpuid_feature_ecx_exclude;
3830Sstevel@tonic-gate uint32_t cpuid_feature_edx_include;
3840Sstevel@tonic-gate uint32_t cpuid_feature_edx_exclude;
3850Sstevel@tonic-gate 
3863446Smrj void
3873446Smrj cpuid_alloc_space(cpu_t *cpu)
3883446Smrj {
3893446Smrj 	/*
3903446Smrj 	 * By convention, cpu0 is the boot cpu, which is set up
3913446Smrj 	 * before memory allocation is available.  All other cpus get
3923446Smrj 	 * their cpuid_info struct allocated here.
3933446Smrj 	 */
3943446Smrj 	ASSERT(cpu->cpu_id != 0);
3953446Smrj 	cpu->cpu_m.mcpu_cpi =
3963446Smrj 	    kmem_zalloc(sizeof (*cpu->cpu_m.mcpu_cpi), KM_SLEEP);
3973446Smrj }
3983446Smrj 
3993446Smrj void
4003446Smrj cpuid_free_space(cpu_t *cpu)
4013446Smrj {
4024606Sesaxe 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
4034606Sesaxe 	int i;
4044606Sesaxe 
4053446Smrj 	ASSERT(cpu->cpu_id != 0);
4064606Sesaxe 
4074606Sesaxe 	/*
4084606Sesaxe 	 * Free up any function 4 related dynamic storage
4094606Sesaxe 	 */
4104606Sesaxe 	for (i = 1; i < cpi->cpi_std_4_size; i++)
4114606Sesaxe 		kmem_free(cpi->cpi_std_4[i], sizeof (struct cpuid_regs));
4124606Sesaxe 	if (cpi->cpi_std_4_size > 0)
4134606Sesaxe 		kmem_free(cpi->cpi_std_4,
4144606Sesaxe 		    cpi->cpi_std_4_size * sizeof (struct cpuid_regs *));
4154606Sesaxe 
4163446Smrj 	kmem_free(cpu->cpu_m.mcpu_cpi, sizeof (*cpu->cpu_m.mcpu_cpi));
4173446Smrj }
4183446Smrj 
4195741Smrj #if !defined(__xpv)
4205741Smrj 
4215741Smrj static void
4225741Smrj check_for_hvm()
4235741Smrj {
4245741Smrj 	struct cpuid_regs cp;
4255741Smrj 	char *xen_str;
4265741Smrj 	uint32_t xen_signature[4];
4275741Smrj 	extern int xpv_is_hvm;
4285741Smrj 
4295741Smrj 	/*
4305741Smrj 	 * In a fully virtualized domain, Xen's pseudo-cpuid function
4315741Smrj 	 * 0x40000000 returns a string representing the Xen signature in
4325741Smrj 	 * %ebx, %ecx, and %edx.  %eax contains the maximum supported cpuid
4335741Smrj 	 * function.
4345741Smrj 	 */
4355741Smrj 	cp.cp_eax = 0x40000000;
4365741Smrj 	(void) __cpuid_insn(&cp);
4375741Smrj 	xen_signature[0] = cp.cp_ebx;
4385741Smrj 	xen_signature[1] = cp.cp_ecx;
4395741Smrj 	xen_signature[2] = cp.cp_edx;
4405741Smrj 	xen_signature[3] = 0;
4415741Smrj 	xen_str = (char *)xen_signature;
4425741Smrj 	if (strcmp("XenVMMXenVMM", xen_str) == 0 && cp.cp_eax <= 0x40000002)
4435741Smrj 		xpv_is_hvm = 1;
4445741Smrj }
4455741Smrj #endif	/* __xpv */
4465741Smrj 
4470Sstevel@tonic-gate uint_t
4480Sstevel@tonic-gate cpuid_pass1(cpu_t *cpu)
4490Sstevel@tonic-gate {
4500Sstevel@tonic-gate 	uint32_t mask_ecx, mask_edx;
4510Sstevel@tonic-gate 	uint_t feature = X86_CPUID;
4520Sstevel@tonic-gate 	struct cpuid_info *cpi;
4531228Sandrei 	struct cpuid_regs *cp;
4540Sstevel@tonic-gate 	int xcpuid;
4555084Sjohnlev #if !defined(__xpv)
4565045Sbholler 	extern int idle_cpu_prefer_mwait;
4575084Sjohnlev #endif
4583446Smrj 
4590Sstevel@tonic-gate 	/*
4603446Smrj 	 * Space statically allocated for cpu0, ensure pointer is set
4610Sstevel@tonic-gate 	 */
4620Sstevel@tonic-gate 	if (cpu->cpu_id == 0)
4633446Smrj 		cpu->cpu_m.mcpu_cpi = &cpuid_info0;
4643446Smrj 	cpi = cpu->cpu_m.mcpu_cpi;
4653446Smrj 	ASSERT(cpi != NULL);
4660Sstevel@tonic-gate 	cp = &cpi->cpi_std[0];
4671228Sandrei 	cp->cp_eax = 0;
4681228Sandrei 	cpi->cpi_maxeax = __cpuid_insn(cp);
4690Sstevel@tonic-gate 	{
4700Sstevel@tonic-gate 		uint32_t *iptr = (uint32_t *)cpi->cpi_vendorstr;
4710Sstevel@tonic-gate 		*iptr++ = cp->cp_ebx;
4720Sstevel@tonic-gate 		*iptr++ = cp->cp_edx;
4730Sstevel@tonic-gate 		*iptr++ = cp->cp_ecx;
4740Sstevel@tonic-gate 		*(char *)&cpi->cpi_vendorstr[12] = '\0';
4750Sstevel@tonic-gate 	}
4760Sstevel@tonic-gate 
4777532SSean.Ye@Sun.COM 	cpi->cpi_vendor = _cpuid_vendorstr_to_vendorcode(cpi->cpi_vendorstr);
4780Sstevel@tonic-gate 	x86_vendor = cpi->cpi_vendor; /* for compatibility */
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	/*
4810Sstevel@tonic-gate 	 * Limit the range in case of weird hardware
4820Sstevel@tonic-gate 	 */
4830Sstevel@tonic-gate 	if (cpi->cpi_maxeax > CPI_MAXEAX_MAX)
4840Sstevel@tonic-gate 		cpi->cpi_maxeax = CPI_MAXEAX_MAX;
4850Sstevel@tonic-gate 	if (cpi->cpi_maxeax < 1)
4860Sstevel@tonic-gate 		goto pass1_done;
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	cp = &cpi->cpi_std[1];
4891228Sandrei 	cp->cp_eax = 1;
4901228Sandrei 	(void) __cpuid_insn(cp);
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	/*
4930Sstevel@tonic-gate 	 * Extract identifying constants for easy access.
4940Sstevel@tonic-gate 	 */
4950Sstevel@tonic-gate 	cpi->cpi_model = CPI_MODEL(cpi);
4960Sstevel@tonic-gate 	cpi->cpi_family = CPI_FAMILY(cpi);
4970Sstevel@tonic-gate 
4981975Sdmick 	if (cpi->cpi_family == 0xf)
4990Sstevel@tonic-gate 		cpi->cpi_family += CPI_FAMILY_XTD(cpi);
5001975Sdmick 
5012001Sdmick 	/*
5024265Skchow 	 * Beware: AMD uses "extended model" iff base *FAMILY* == 0xf.
5032001Sdmick 	 * Intel, and presumably everyone else, uses model == 0xf, as
5042001Sdmick 	 * one would expect (max value means possible overflow).  Sigh.
5052001Sdmick 	 */
5062001Sdmick 
5072001Sdmick 	switch (cpi->cpi_vendor) {
5084855Sksadhukh 	case X86_VENDOR_Intel:
5094855Sksadhukh 		if (IS_EXTENDED_MODEL_INTEL(cpi))
5104855Sksadhukh 			cpi->cpi_model += CPI_MODEL_XTD(cpi) << 4;
5114858Sksadhukh 		break;
5122001Sdmick 	case X86_VENDOR_AMD:
5134265Skchow 		if (CPI_FAMILY(cpi) == 0xf)
5142001Sdmick 			cpi->cpi_model += CPI_MODEL_XTD(cpi) << 4;
5152001Sdmick 		break;
5162001Sdmick 	default:
5172001Sdmick 		if (cpi->cpi_model == 0xf)
5182001Sdmick 			cpi->cpi_model += CPI_MODEL_XTD(cpi) << 4;
5192001Sdmick 		break;
5202001Sdmick 	}
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	cpi->cpi_step = CPI_STEP(cpi);
5230Sstevel@tonic-gate 	cpi->cpi_brandid = CPI_BRANDID(cpi);
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	/*
5260Sstevel@tonic-gate 	 * *default* assumptions:
5270Sstevel@tonic-gate 	 * - believe %edx feature word
5280Sstevel@tonic-gate 	 * - ignore %ecx feature word
5290Sstevel@tonic-gate 	 * - 32-bit virtual and physical addressing
5300Sstevel@tonic-gate 	 */
5310Sstevel@tonic-gate 	mask_edx = 0xffffffff;
5320Sstevel@tonic-gate 	mask_ecx = 0;
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	cpi->cpi_pabits = cpi->cpi_vabits = 32;
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
5370Sstevel@tonic-gate 	case X86_VENDOR_Intel:
5380Sstevel@tonic-gate 		if (cpi->cpi_family == 5)
5390Sstevel@tonic-gate 			x86_type = X86_TYPE_P5;
5401975Sdmick 		else if (IS_LEGACY_P6(cpi)) {
5410Sstevel@tonic-gate 			x86_type = X86_TYPE_P6;
5420Sstevel@tonic-gate 			pentiumpro_bug4046376 = 1;
5430Sstevel@tonic-gate 			pentiumpro_bug4064495 = 1;
5440Sstevel@tonic-gate 			/*
5450Sstevel@tonic-gate 			 * Clear the SEP bit when it was set erroneously
5460Sstevel@tonic-gate 			 */
5470Sstevel@tonic-gate 			if (cpi->cpi_model < 3 && cpi->cpi_step < 3)
5480Sstevel@tonic-gate 				cp->cp_edx &= ~CPUID_INTC_EDX_SEP;
5491975Sdmick 		} else if (IS_NEW_F6(cpi) || cpi->cpi_family == 0xf) {
5500Sstevel@tonic-gate 			x86_type = X86_TYPE_P4;
5510Sstevel@tonic-gate 			/*
5520Sstevel@tonic-gate 			 * We don't currently depend on any of the %ecx
5530Sstevel@tonic-gate 			 * features until Prescott, so we'll only check
5540Sstevel@tonic-gate 			 * this from P4 onwards.  We might want to revisit
5550Sstevel@tonic-gate 			 * that idea later.
5560Sstevel@tonic-gate 			 */
5570Sstevel@tonic-gate 			mask_ecx = 0xffffffff;
5580Sstevel@tonic-gate 		} else if (cpi->cpi_family > 0xf)
5590Sstevel@tonic-gate 			mask_ecx = 0xffffffff;
5604636Sbholler 		/*
5614636Sbholler 		 * We don't support MONITOR/MWAIT if leaf 5 is not available
5624636Sbholler 		 * to obtain the monitor linesize.
5634636Sbholler 		 */
5644636Sbholler 		if (cpi->cpi_maxeax < 5)
5654636Sbholler 			mask_ecx &= ~CPUID_INTC_ECX_MON;
5660Sstevel@tonic-gate 		break;
5670Sstevel@tonic-gate 	case X86_VENDOR_IntelClone:
5680Sstevel@tonic-gate 	default:
5690Sstevel@tonic-gate 		break;
5700Sstevel@tonic-gate 	case X86_VENDOR_AMD:
5710Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_108)
5720Sstevel@tonic-gate 		if (cpi->cpi_family == 0xf && cpi->cpi_model == 0xe) {
5730Sstevel@tonic-gate 			cp->cp_eax = (0xf0f & cp->cp_eax) | 0xc0;
5740Sstevel@tonic-gate 			cpi->cpi_model = 0xc;
5750Sstevel@tonic-gate 		} else
5760Sstevel@tonic-gate #endif
5770Sstevel@tonic-gate 		if (cpi->cpi_family == 5) {
5780Sstevel@tonic-gate 			/*
5790Sstevel@tonic-gate 			 * AMD K5 and K6
5800Sstevel@tonic-gate 			 *
5810Sstevel@tonic-gate 			 * These CPUs have an incomplete implementation
5820Sstevel@tonic-gate 			 * of MCA/MCE which we mask away.
5830Sstevel@tonic-gate 			 */
5841228Sandrei 			mask_edx &= ~(CPUID_INTC_EDX_MCE | CPUID_INTC_EDX_MCA);
5851228Sandrei 
5861228Sandrei 			/*
5871228Sandrei 			 * Model 0 uses the wrong (APIC) bit
5881228Sandrei 			 * to indicate PGE.  Fix it here.
5891228Sandrei 			 */
5900Sstevel@tonic-gate 			if (cpi->cpi_model == 0) {
5910Sstevel@tonic-gate 				if (cp->cp_edx & 0x200) {
5920Sstevel@tonic-gate 					cp->cp_edx &= ~0x200;
5930Sstevel@tonic-gate 					cp->cp_edx |= CPUID_INTC_EDX_PGE;
5940Sstevel@tonic-gate 				}
5951228Sandrei 			}
5961228Sandrei 
5971228Sandrei 			/*
5981228Sandrei 			 * Early models had problems w/ MMX; disable.
5991228Sandrei 			 */
6001228Sandrei 			if (cpi->cpi_model < 6)
6011228Sandrei 				mask_edx &= ~CPUID_INTC_EDX_MMX;
6021228Sandrei 		}
6031228Sandrei 
6041228Sandrei 		/*
6051228Sandrei 		 * For newer families, SSE3 and CX16, at least, are valid;
6061228Sandrei 		 * enable all
6071228Sandrei 		 */
6081228Sandrei 		if (cpi->cpi_family >= 0xf)
609771Sdmick 			mask_ecx = 0xffffffff;
6104636Sbholler 		/*
6114636Sbholler 		 * We don't support MONITOR/MWAIT if leaf 5 is not available
6124636Sbholler 		 * to obtain the monitor linesize.
6134636Sbholler 		 */
6144636Sbholler 		if (cpi->cpi_maxeax < 5)
6154636Sbholler 			mask_ecx &= ~CPUID_INTC_ECX_MON;
6165045Sbholler 
6175084Sjohnlev #if !defined(__xpv)
6185045Sbholler 		/*
6195045Sbholler 		 * Do not use MONITOR/MWAIT to halt in the idle loop on any AMD
6205045Sbholler 		 * processors.  AMD does not intend MWAIT to be used in the cpu
6215045Sbholler 		 * idle loop on current and future processors.  10h and future
6225045Sbholler 		 * AMD processors use more power in MWAIT than HLT.
6235045Sbholler 		 * Pre-family-10h Opterons do not have the MWAIT instruction.
6245045Sbholler 		 */
6255045Sbholler 		idle_cpu_prefer_mwait = 0;
6265084Sjohnlev #endif
6275045Sbholler 
6280Sstevel@tonic-gate 		break;
6290Sstevel@tonic-gate 	case X86_VENDOR_TM:
6300Sstevel@tonic-gate 		/*
6310Sstevel@tonic-gate 		 * workaround the NT workaround in CMS 4.1
6320Sstevel@tonic-gate 		 */
6330Sstevel@tonic-gate 		if (cpi->cpi_family == 5 && cpi->cpi_model == 4 &&
6340Sstevel@tonic-gate 		    (cpi->cpi_step == 2 || cpi->cpi_step == 3))
6350Sstevel@tonic-gate 			cp->cp_edx |= CPUID_INTC_EDX_CX8;
6360Sstevel@tonic-gate 		break;
6370Sstevel@tonic-gate 	case X86_VENDOR_Centaur:
6380Sstevel@tonic-gate 		/*
6390Sstevel@tonic-gate 		 * workaround the NT workarounds again
6400Sstevel@tonic-gate 		 */
6410Sstevel@tonic-gate 		if (cpi->cpi_family == 6)
6420Sstevel@tonic-gate 			cp->cp_edx |= CPUID_INTC_EDX_CX8;
6430Sstevel@tonic-gate 		break;
6440Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
6450Sstevel@tonic-gate 		/*
6460Sstevel@tonic-gate 		 * We rely heavily on the probing in locore
6470Sstevel@tonic-gate 		 * to actually figure out what parts, if any,
6480Sstevel@tonic-gate 		 * of the Cyrix cpuid instruction to believe.
6490Sstevel@tonic-gate 		 */
6500Sstevel@tonic-gate 		switch (x86_type) {
6510Sstevel@tonic-gate 		case X86_TYPE_CYRIX_486:
6520Sstevel@tonic-gate 			mask_edx = 0;
6530Sstevel@tonic-gate 			break;
6540Sstevel@tonic-gate 		case X86_TYPE_CYRIX_6x86:
6550Sstevel@tonic-gate 			mask_edx = 0;
6560Sstevel@tonic-gate 			break;
6570Sstevel@tonic-gate 		case X86_TYPE_CYRIX_6x86L:
6580Sstevel@tonic-gate 			mask_edx =
6590Sstevel@tonic-gate 			    CPUID_INTC_EDX_DE |
6600Sstevel@tonic-gate 			    CPUID_INTC_EDX_CX8;
6610Sstevel@tonic-gate 			break;
6620Sstevel@tonic-gate 		case X86_TYPE_CYRIX_6x86MX:
6630Sstevel@tonic-gate 			mask_edx =
6640Sstevel@tonic-gate 			    CPUID_INTC_EDX_DE |
6650Sstevel@tonic-gate 			    CPUID_INTC_EDX_MSR |
6660Sstevel@tonic-gate 			    CPUID_INTC_EDX_CX8 |
6670Sstevel@tonic-gate 			    CPUID_INTC_EDX_PGE |
6680Sstevel@tonic-gate 			    CPUID_INTC_EDX_CMOV |
6690Sstevel@tonic-gate 			    CPUID_INTC_EDX_MMX;
6700Sstevel@tonic-gate 			break;
6710Sstevel@tonic-gate 		case X86_TYPE_CYRIX_GXm:
6720Sstevel@tonic-gate 			mask_edx =
6730Sstevel@tonic-gate 			    CPUID_INTC_EDX_MSR |
6740Sstevel@tonic-gate 			    CPUID_INTC_EDX_CX8 |
6750Sstevel@tonic-gate 			    CPUID_INTC_EDX_CMOV |
6760Sstevel@tonic-gate 			    CPUID_INTC_EDX_MMX;
6770Sstevel@tonic-gate 			break;
6780Sstevel@tonic-gate 		case X86_TYPE_CYRIX_MediaGX:
6790Sstevel@tonic-gate 			break;
6800Sstevel@tonic-gate 		case X86_TYPE_CYRIX_MII:
6810Sstevel@tonic-gate 		case X86_TYPE_VIA_CYRIX_III:
6820Sstevel@tonic-gate 			mask_edx =
6830Sstevel@tonic-gate 			    CPUID_INTC_EDX_DE |
6840Sstevel@tonic-gate 			    CPUID_INTC_EDX_TSC |
6850Sstevel@tonic-gate 			    CPUID_INTC_EDX_MSR |
6860Sstevel@tonic-gate 			    CPUID_INTC_EDX_CX8 |
6870Sstevel@tonic-gate 			    CPUID_INTC_EDX_PGE |
6880Sstevel@tonic-gate 			    CPUID_INTC_EDX_CMOV |
6890Sstevel@tonic-gate 			    CPUID_INTC_EDX_MMX;
6900Sstevel@tonic-gate 			break;
6910Sstevel@tonic-gate 		default:
6920Sstevel@tonic-gate 			break;
6930Sstevel@tonic-gate 		}
6940Sstevel@tonic-gate 		break;
6950Sstevel@tonic-gate 	}
6960Sstevel@tonic-gate 
6975084Sjohnlev #if defined(__xpv)
6985084Sjohnlev 	/*
6995084Sjohnlev 	 * Do not support MONITOR/MWAIT under a hypervisor
7005084Sjohnlev 	 */
7015084Sjohnlev 	mask_ecx &= ~CPUID_INTC_ECX_MON;
7025084Sjohnlev #endif	/* __xpv */
7035084Sjohnlev 
7040Sstevel@tonic-gate 	/*
7050Sstevel@tonic-gate 	 * Now we've figured out the masks that determine
7060Sstevel@tonic-gate 	 * which bits we choose to believe, apply the masks
7070Sstevel@tonic-gate 	 * to the feature words, then map the kernel's view
7080Sstevel@tonic-gate 	 * of these feature words into its feature word.
7090Sstevel@tonic-gate 	 */
7100Sstevel@tonic-gate 	cp->cp_edx &= mask_edx;
7110Sstevel@tonic-gate 	cp->cp_ecx &= mask_ecx;
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	/*
7143446Smrj 	 * apply any platform restrictions (we don't call this
7153446Smrj 	 * immediately after __cpuid_insn here, because we need the
7163446Smrj 	 * workarounds applied above first)
7170Sstevel@tonic-gate 	 */
7183446Smrj 	platform_cpuid_mangle(cpi->cpi_vendor, 1, cp);
7190Sstevel@tonic-gate 
7203446Smrj 	/*
7213446Smrj 	 * fold in overrides from the "eeprom" mechanism
7223446Smrj 	 */
7230Sstevel@tonic-gate 	cp->cp_edx |= cpuid_feature_edx_include;
7240Sstevel@tonic-gate 	cp->cp_edx &= ~cpuid_feature_edx_exclude;
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 	cp->cp_ecx |= cpuid_feature_ecx_include;
7270Sstevel@tonic-gate 	cp->cp_ecx &= ~cpuid_feature_ecx_exclude;
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_PSE)
7300Sstevel@tonic-gate 		feature |= X86_LARGEPAGE;
7310Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_TSC)
7320Sstevel@tonic-gate 		feature |= X86_TSC;
7330Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_MSR)
7340Sstevel@tonic-gate 		feature |= X86_MSR;
7350Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_MTRR)
7360Sstevel@tonic-gate 		feature |= X86_MTRR;
7370Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_PGE)
7380Sstevel@tonic-gate 		feature |= X86_PGE;
7390Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_CMOV)
7400Sstevel@tonic-gate 		feature |= X86_CMOV;
7410Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_MMX)
7420Sstevel@tonic-gate 		feature |= X86_MMX;
7430Sstevel@tonic-gate 	if ((cp->cp_edx & CPUID_INTC_EDX_MCE) != 0 &&
7440Sstevel@tonic-gate 	    (cp->cp_edx & CPUID_INTC_EDX_MCA) != 0)
7450Sstevel@tonic-gate 		feature |= X86_MCA;
7460Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_PAE)
7470Sstevel@tonic-gate 		feature |= X86_PAE;
7480Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_CX8)
7490Sstevel@tonic-gate 		feature |= X86_CX8;
7500Sstevel@tonic-gate 	if (cp->cp_ecx & CPUID_INTC_ECX_CX16)
7510Sstevel@tonic-gate 		feature |= X86_CX16;
7520Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_PAT)
7530Sstevel@tonic-gate 		feature |= X86_PAT;
7540Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_SEP)
7550Sstevel@tonic-gate 		feature |= X86_SEP;
7560Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_FXSR) {
7570Sstevel@tonic-gate 		/*
7580Sstevel@tonic-gate 		 * In our implementation, fxsave/fxrstor
7590Sstevel@tonic-gate 		 * are prerequisites before we'll even
7600Sstevel@tonic-gate 		 * try and do SSE things.
7610Sstevel@tonic-gate 		 */
7620Sstevel@tonic-gate 		if (cp->cp_edx & CPUID_INTC_EDX_SSE)
7630Sstevel@tonic-gate 			feature |= X86_SSE;
7640Sstevel@tonic-gate 		if (cp->cp_edx & CPUID_INTC_EDX_SSE2)
7650Sstevel@tonic-gate 			feature |= X86_SSE2;
7660Sstevel@tonic-gate 		if (cp->cp_ecx & CPUID_INTC_ECX_SSE3)
7670Sstevel@tonic-gate 			feature |= X86_SSE3;
7685269Skk208521 		if (cpi->cpi_vendor == X86_VENDOR_Intel) {
7695269Skk208521 			if (cp->cp_ecx & CPUID_INTC_ECX_SSSE3)
7705269Skk208521 				feature |= X86_SSSE3;
7715269Skk208521 			if (cp->cp_ecx & CPUID_INTC_ECX_SSE4_1)
7725269Skk208521 				feature |= X86_SSE4_1;
7735269Skk208521 			if (cp->cp_ecx & CPUID_INTC_ECX_SSE4_2)
7745269Skk208521 				feature |= X86_SSE4_2;
7755269Skk208521 		}
7760Sstevel@tonic-gate 	}
7770Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_DE)
7783446Smrj 		feature |= X86_DE;
779*7716SBill.Holler@Sun.COM #if !defined(__xpv)
7804481Sbholler 	if (cp->cp_ecx & CPUID_INTC_ECX_MON) {
781*7716SBill.Holler@Sun.COM 
782*7716SBill.Holler@Sun.COM 		/*
783*7716SBill.Holler@Sun.COM 		 * We require the CLFLUSH instruction for erratum workaround
784*7716SBill.Holler@Sun.COM 		 * to use MONITOR/MWAIT.
785*7716SBill.Holler@Sun.COM 		 */
786*7716SBill.Holler@Sun.COM 		if (cp->cp_edx & CPUID_INTC_EDX_CLFSH) {
787*7716SBill.Holler@Sun.COM 			cpi->cpi_mwait.support |= MWAIT_SUPPORT;
788*7716SBill.Holler@Sun.COM 			feature |= X86_MWAIT;
789*7716SBill.Holler@Sun.COM 		} else {
790*7716SBill.Holler@Sun.COM 			extern int idle_cpu_assert_cflush_monitor;
791*7716SBill.Holler@Sun.COM 
792*7716SBill.Holler@Sun.COM 			/*
793*7716SBill.Holler@Sun.COM 			 * All processors we are aware of which have
794*7716SBill.Holler@Sun.COM 			 * MONITOR/MWAIT also have CLFLUSH.
795*7716SBill.Holler@Sun.COM 			 */
796*7716SBill.Holler@Sun.COM 			if (idle_cpu_assert_cflush_monitor) {
797*7716SBill.Holler@Sun.COM 				ASSERT((cp->cp_ecx & CPUID_INTC_ECX_MON) &&
798*7716SBill.Holler@Sun.COM 				    (cp->cp_edx & CPUID_INTC_EDX_CLFSH));
799*7716SBill.Holler@Sun.COM 			}
800*7716SBill.Holler@Sun.COM 		}
8014481Sbholler 	}
802*7716SBill.Holler@Sun.COM #endif	/* __xpv */
8030Sstevel@tonic-gate 
8047589SVikram.Hegde@Sun.COM 	/*
8057589SVikram.Hegde@Sun.COM 	 * Only need it first time, rest of the cpus would follow suite.
8067589SVikram.Hegde@Sun.COM 	 * we only capture this for the bootcpu.
8077589SVikram.Hegde@Sun.COM 	 */
8087589SVikram.Hegde@Sun.COM 	if (cp->cp_edx & CPUID_INTC_EDX_CLFSH) {
8097589SVikram.Hegde@Sun.COM 		feature |= X86_CLFSH;
8107589SVikram.Hegde@Sun.COM 		x86_clflush_size = (BITX(cp->cp_ebx, 15, 8) * 8);
8117589SVikram.Hegde@Sun.COM 	}
8127589SVikram.Hegde@Sun.COM 
8130Sstevel@tonic-gate 	if (feature & X86_PAE)
8140Sstevel@tonic-gate 		cpi->cpi_pabits = 36;
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 	/*
8170Sstevel@tonic-gate 	 * Hyperthreading configuration is slightly tricky on Intel
8180Sstevel@tonic-gate 	 * and pure clones, and even trickier on AMD.
8190Sstevel@tonic-gate 	 *
8200Sstevel@tonic-gate 	 * (AMD chose to set the HTT bit on their CMP processors,
8210Sstevel@tonic-gate 	 * even though they're not actually hyperthreaded.  Thus it
8220Sstevel@tonic-gate 	 * takes a bit more work to figure out what's really going
8233446Smrj 	 * on ... see the handling of the CMP_LGCY bit below)
8240Sstevel@tonic-gate 	 */
8250Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_HTT) {
8260Sstevel@tonic-gate 		cpi->cpi_ncpu_per_chip = CPI_CPU_COUNT(cpi);
8270Sstevel@tonic-gate 		if (cpi->cpi_ncpu_per_chip > 1)
8280Sstevel@tonic-gate 			feature |= X86_HTT;
8291228Sandrei 	} else {
8301228Sandrei 		cpi->cpi_ncpu_per_chip = 1;
8310Sstevel@tonic-gate 	}
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 	/*
8340Sstevel@tonic-gate 	 * Work on the "extended" feature information, doing
8350Sstevel@tonic-gate 	 * some basic initialization for cpuid_pass2()
8360Sstevel@tonic-gate 	 */
8370Sstevel@tonic-gate 	xcpuid = 0;
8380Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
8390Sstevel@tonic-gate 	case X86_VENDOR_Intel:
8401975Sdmick 		if (IS_NEW_F6(cpi) || cpi->cpi_family >= 0xf)
8410Sstevel@tonic-gate 			xcpuid++;
8420Sstevel@tonic-gate 		break;
8430Sstevel@tonic-gate 	case X86_VENDOR_AMD:
8440Sstevel@tonic-gate 		if (cpi->cpi_family > 5 ||
8450Sstevel@tonic-gate 		    (cpi->cpi_family == 5 && cpi->cpi_model >= 1))
8460Sstevel@tonic-gate 			xcpuid++;
8470Sstevel@tonic-gate 		break;
8480Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
8490Sstevel@tonic-gate 		/*
8500Sstevel@tonic-gate 		 * Only these Cyrix CPUs are -known- to support
8510Sstevel@tonic-gate 		 * extended cpuid operations.
8520Sstevel@tonic-gate 		 */
8530Sstevel@tonic-gate 		if (x86_type == X86_TYPE_VIA_CYRIX_III ||
8540Sstevel@tonic-gate 		    x86_type == X86_TYPE_CYRIX_GXm)
8550Sstevel@tonic-gate 			xcpuid++;
8560Sstevel@tonic-gate 		break;
8570Sstevel@tonic-gate 	case X86_VENDOR_Centaur:
8580Sstevel@tonic-gate 	case X86_VENDOR_TM:
8590Sstevel@tonic-gate 	default:
8600Sstevel@tonic-gate 		xcpuid++;
8610Sstevel@tonic-gate 		break;
8620Sstevel@tonic-gate 	}
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 	if (xcpuid) {
8650Sstevel@tonic-gate 		cp = &cpi->cpi_extd[0];
8661228Sandrei 		cp->cp_eax = 0x80000000;
8671228Sandrei 		cpi->cpi_xmaxeax = __cpuid_insn(cp);
8680Sstevel@tonic-gate 	}
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 	if (cpi->cpi_xmaxeax & 0x80000000) {
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 		if (cpi->cpi_xmaxeax > CPI_XMAXEAX_MAX)
8730Sstevel@tonic-gate 			cpi->cpi_xmaxeax = CPI_XMAXEAX_MAX;
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 		switch (cpi->cpi_vendor) {
8760Sstevel@tonic-gate 		case X86_VENDOR_Intel:
8770Sstevel@tonic-gate 		case X86_VENDOR_AMD:
8780Sstevel@tonic-gate 			if (cpi->cpi_xmaxeax < 0x80000001)
8790Sstevel@tonic-gate 				break;
8800Sstevel@tonic-gate 			cp = &cpi->cpi_extd[1];
8811228Sandrei 			cp->cp_eax = 0x80000001;
8821228Sandrei 			(void) __cpuid_insn(cp);
8833446Smrj 
8840Sstevel@tonic-gate 			if (cpi->cpi_vendor == X86_VENDOR_AMD &&
8850Sstevel@tonic-gate 			    cpi->cpi_family == 5 &&
8860Sstevel@tonic-gate 			    cpi->cpi_model == 6 &&
8870Sstevel@tonic-gate 			    cpi->cpi_step == 6) {
8880Sstevel@tonic-gate 				/*
8890Sstevel@tonic-gate 				 * K6 model 6 uses bit 10 to indicate SYSC
8900Sstevel@tonic-gate 				 * Later models use bit 11. Fix it here.
8910Sstevel@tonic-gate 				 */
8920Sstevel@tonic-gate 				if (cp->cp_edx & 0x400) {
8930Sstevel@tonic-gate 					cp->cp_edx &= ~0x400;
8940Sstevel@tonic-gate 					cp->cp_edx |= CPUID_AMD_EDX_SYSC;
8950Sstevel@tonic-gate 				}
8960Sstevel@tonic-gate 			}
8970Sstevel@tonic-gate 
8983446Smrj 			platform_cpuid_mangle(cpi->cpi_vendor, 0x80000001, cp);
8993446Smrj 
9000Sstevel@tonic-gate 			/*
9010Sstevel@tonic-gate 			 * Compute the additions to the kernel's feature word.
9020Sstevel@tonic-gate 			 */
9030Sstevel@tonic-gate 			if (cp->cp_edx & CPUID_AMD_EDX_NX)
9040Sstevel@tonic-gate 				feature |= X86_NX;
9050Sstevel@tonic-gate 
9067656SSherry.Moore@Sun.COM 			/*
9077656SSherry.Moore@Sun.COM 			 * Regardless whether or not we boot 64-bit,
9087656SSherry.Moore@Sun.COM 			 * we should have a way to identify whether
9097656SSherry.Moore@Sun.COM 			 * the CPU is capable of running 64-bit.
9107656SSherry.Moore@Sun.COM 			 */
9117656SSherry.Moore@Sun.COM 			if (cp->cp_edx & CPUID_AMD_EDX_LM)
9127656SSherry.Moore@Sun.COM 				feature |= X86_64;
9137656SSherry.Moore@Sun.COM 
9145349Skchow #if defined(__amd64)
9155349Skchow 			/* 1 GB large page - enable only for 64 bit kernel */
9165349Skchow 			if (cp->cp_edx & CPUID_AMD_EDX_1GPG)
9175349Skchow 				feature |= X86_1GPG;
9185349Skchow #endif
9195349Skchow 
9204628Skk208521 			if ((cpi->cpi_vendor == X86_VENDOR_AMD) &&
9214628Skk208521 			    (cpi->cpi_std[1].cp_edx & CPUID_INTC_EDX_FXSR) &&
9224628Skk208521 			    (cp->cp_ecx & CPUID_AMD_ECX_SSE4A))
9234628Skk208521 				feature |= X86_SSE4A;
9244628Skk208521 
9250Sstevel@tonic-gate 			/*
9263446Smrj 			 * If both the HTT and CMP_LGCY bits are set,
9271228Sandrei 			 * then we're not actually HyperThreaded.  Read
9281228Sandrei 			 * "AMD CPUID Specification" for more details.
9290Sstevel@tonic-gate 			 */
9300Sstevel@tonic-gate 			if (cpi->cpi_vendor == X86_VENDOR_AMD &&
9311228Sandrei 			    (feature & X86_HTT) &&
9323446Smrj 			    (cp->cp_ecx & CPUID_AMD_ECX_CMP_LGCY)) {
9330Sstevel@tonic-gate 				feature &= ~X86_HTT;
9341228Sandrei 				feature |= X86_CMP;
9351228Sandrei 			}
9363446Smrj #if defined(__amd64)
9370Sstevel@tonic-gate 			/*
9380Sstevel@tonic-gate 			 * It's really tricky to support syscall/sysret in
9390Sstevel@tonic-gate 			 * the i386 kernel; we rely on sysenter/sysexit
9400Sstevel@tonic-gate 			 * instead.  In the amd64 kernel, things are -way-
9410Sstevel@tonic-gate 			 * better.
9420Sstevel@tonic-gate 			 */
9430Sstevel@tonic-gate 			if (cp->cp_edx & CPUID_AMD_EDX_SYSC)
9440Sstevel@tonic-gate 				feature |= X86_ASYSC;
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 			/*
9470Sstevel@tonic-gate 			 * While we're thinking about system calls, note
9480Sstevel@tonic-gate 			 * that AMD processors don't support sysenter
9490Sstevel@tonic-gate 			 * in long mode at all, so don't try to program them.
9500Sstevel@tonic-gate 			 */
9510Sstevel@tonic-gate 			if (x86_vendor == X86_VENDOR_AMD)
9520Sstevel@tonic-gate 				feature &= ~X86_SEP;
9530Sstevel@tonic-gate #endif
9546657Ssudheer 			if (cp->cp_edx & CPUID_AMD_EDX_TSCP)
9553446Smrj 				feature |= X86_TSCP;
9560Sstevel@tonic-gate 			break;
9570Sstevel@tonic-gate 		default:
9580Sstevel@tonic-gate 			break;
9590Sstevel@tonic-gate 		}
9600Sstevel@tonic-gate 
9611228Sandrei 		/*
9621228Sandrei 		 * Get CPUID data about processor cores and hyperthreads.
9631228Sandrei 		 */
9640Sstevel@tonic-gate 		switch (cpi->cpi_vendor) {
9650Sstevel@tonic-gate 		case X86_VENDOR_Intel:
9661228Sandrei 			if (cpi->cpi_maxeax >= 4) {
9671228Sandrei 				cp = &cpi->cpi_std[4];
9681228Sandrei 				cp->cp_eax = 4;
9691228Sandrei 				cp->cp_ecx = 0;
9701228Sandrei 				(void) __cpuid_insn(cp);
9713446Smrj 				platform_cpuid_mangle(cpi->cpi_vendor, 4, cp);
9721228Sandrei 			}
9731228Sandrei 			/*FALLTHROUGH*/
9740Sstevel@tonic-gate 		case X86_VENDOR_AMD:
9750Sstevel@tonic-gate 			if (cpi->cpi_xmaxeax < 0x80000008)
9760Sstevel@tonic-gate 				break;
9770Sstevel@tonic-gate 			cp = &cpi->cpi_extd[8];
9781228Sandrei 			cp->cp_eax = 0x80000008;
9791228Sandrei 			(void) __cpuid_insn(cp);
9803446Smrj 			platform_cpuid_mangle(cpi->cpi_vendor, 0x80000008, cp);
9813446Smrj 
9820Sstevel@tonic-gate 			/*
9830Sstevel@tonic-gate 			 * Virtual and physical address limits from
9840Sstevel@tonic-gate 			 * cpuid override previously guessed values.
9850Sstevel@tonic-gate 			 */
9860Sstevel@tonic-gate 			cpi->cpi_pabits = BITX(cp->cp_eax, 7, 0);
9870Sstevel@tonic-gate 			cpi->cpi_vabits = BITX(cp->cp_eax, 15, 8);
9880Sstevel@tonic-gate 			break;
9890Sstevel@tonic-gate 		default:
9900Sstevel@tonic-gate 			break;
9910Sstevel@tonic-gate 		}
9921228Sandrei 
9934606Sesaxe 		/*
9944606Sesaxe 		 * Derive the number of cores per chip
9954606Sesaxe 		 */
9961228Sandrei 		switch (cpi->cpi_vendor) {
9971228Sandrei 		case X86_VENDOR_Intel:
9981228Sandrei 			if (cpi->cpi_maxeax < 4) {
9991228Sandrei 				cpi->cpi_ncore_per_chip = 1;
10001228Sandrei 				break;
10011228Sandrei 			} else {
10021228Sandrei 				cpi->cpi_ncore_per_chip =
10031228Sandrei 				    BITX((cpi)->cpi_std[4].cp_eax, 31, 26) + 1;
10041228Sandrei 			}
10051228Sandrei 			break;
10061228Sandrei 		case X86_VENDOR_AMD:
10071228Sandrei 			if (cpi->cpi_xmaxeax < 0x80000008) {
10081228Sandrei 				cpi->cpi_ncore_per_chip = 1;
10091228Sandrei 				break;
10101228Sandrei 			} else {
10115870Sgavinm 				/*
10125870Sgavinm 				 * On family 0xf cpuid fn 2 ECX[7:0] "NC" is
10135870Sgavinm 				 * 1 less than the number of physical cores on
10145870Sgavinm 				 * the chip.  In family 0x10 this value can
10155870Sgavinm 				 * be affected by "downcoring" - it reflects
10165870Sgavinm 				 * 1 less than the number of cores actually
10175870Sgavinm 				 * enabled on this node.
10185870Sgavinm 				 */
10191228Sandrei 				cpi->cpi_ncore_per_chip =
10201228Sandrei 				    BITX((cpi)->cpi_extd[8].cp_ecx, 7, 0) + 1;
10211228Sandrei 			}
10221228Sandrei 			break;
10231228Sandrei 		default:
10241228Sandrei 			cpi->cpi_ncore_per_chip = 1;
10251228Sandrei 			break;
10261228Sandrei 		}
10275284Sgavinm 	} else {
10285284Sgavinm 		cpi->cpi_ncore_per_chip = 1;
10290Sstevel@tonic-gate 	}
10300Sstevel@tonic-gate 
10311228Sandrei 	/*
10321228Sandrei 	 * If more than one core, then this processor is CMP.
10331228Sandrei 	 */
10341228Sandrei 	if (cpi->cpi_ncore_per_chip > 1)
10351228Sandrei 		feature |= X86_CMP;
10363446Smrj 
10371228Sandrei 	/*
10381228Sandrei 	 * If the number of cores is the same as the number
10391228Sandrei 	 * of CPUs, then we cannot have HyperThreading.
10401228Sandrei 	 */
10411228Sandrei 	if (cpi->cpi_ncpu_per_chip == cpi->cpi_ncore_per_chip)
10421228Sandrei 		feature &= ~X86_HTT;
10431228Sandrei 
10440Sstevel@tonic-gate 	if ((feature & (X86_HTT | X86_CMP)) == 0) {
10451228Sandrei 		/*
10461228Sandrei 		 * Single-core single-threaded processors.
10471228Sandrei 		 */
10480Sstevel@tonic-gate 		cpi->cpi_chipid = -1;
10490Sstevel@tonic-gate 		cpi->cpi_clogid = 0;
10501228Sandrei 		cpi->cpi_coreid = cpu->cpu_id;
10515870Sgavinm 		cpi->cpi_pkgcoreid = 0;
10520Sstevel@tonic-gate 	} else if (cpi->cpi_ncpu_per_chip > 1) {
10531228Sandrei 		uint_t i;
10541228Sandrei 		uint_t chipid_shift = 0;
10551228Sandrei 		uint_t coreid_shift = 0;
10561228Sandrei 		uint_t apic_id = CPI_APIC_ID(cpi);
10571228Sandrei 
10581228Sandrei 		for (i = 1; i < cpi->cpi_ncpu_per_chip; i <<= 1)
10591228Sandrei 			chipid_shift++;
10601228Sandrei 		cpi->cpi_chipid = apic_id >> chipid_shift;
10611228Sandrei 		cpi->cpi_clogid = apic_id & ((1 << chipid_shift) - 1);
10620Sstevel@tonic-gate 
10631228Sandrei 		if (cpi->cpi_vendor == X86_VENDOR_Intel) {
10641228Sandrei 			if (feature & X86_CMP) {
10651228Sandrei 				/*
10661228Sandrei 				 * Multi-core (and possibly multi-threaded)
10671228Sandrei 				 * processors.
10681228Sandrei 				 */
10691228Sandrei 				uint_t ncpu_per_core;
10701228Sandrei 				if (cpi->cpi_ncore_per_chip == 1)
10711228Sandrei 					ncpu_per_core = cpi->cpi_ncpu_per_chip;
10721228Sandrei 				else if (cpi->cpi_ncore_per_chip > 1)
10731228Sandrei 					ncpu_per_core = cpi->cpi_ncpu_per_chip /
10741228Sandrei 					    cpi->cpi_ncore_per_chip;
10751228Sandrei 				/*
10761228Sandrei 				 * 8bit APIC IDs on dual core Pentiums
10771228Sandrei 				 * look like this:
10781228Sandrei 				 *
10791228Sandrei 				 * +-----------------------+------+------+
10801228Sandrei 				 * | Physical Package ID   |  MC  |  HT  |
10811228Sandrei 				 * +-----------------------+------+------+
10821228Sandrei 				 * <------- chipid -------->
10831228Sandrei 				 * <------- coreid --------------->
10841228Sandrei 				 *			   <--- clogid -->
10855870Sgavinm 				 *			   <------>
10865870Sgavinm 				 *			   pkgcoreid
10871228Sandrei 				 *
10881228Sandrei 				 * Where the number of bits necessary to
10891228Sandrei 				 * represent MC and HT fields together equals
10901228Sandrei 				 * to the minimum number of bits necessary to
10911228Sandrei 				 * store the value of cpi->cpi_ncpu_per_chip.
10921228Sandrei 				 * Of those bits, the MC part uses the number
10931228Sandrei 				 * of bits necessary to store the value of
10941228Sandrei 				 * cpi->cpi_ncore_per_chip.
10951228Sandrei 				 */
10961228Sandrei 				for (i = 1; i < ncpu_per_core; i <<= 1)
10971228Sandrei 					coreid_shift++;
10981727Sandrei 				cpi->cpi_coreid = apic_id >> coreid_shift;
10995870Sgavinm 				cpi->cpi_pkgcoreid = cpi->cpi_clogid >>
11005870Sgavinm 				    coreid_shift;
11011228Sandrei 			} else if (feature & X86_HTT) {
11021228Sandrei 				/*
11031228Sandrei 				 * Single-core multi-threaded processors.
11041228Sandrei 				 */
11051228Sandrei 				cpi->cpi_coreid = cpi->cpi_chipid;
11065870Sgavinm 				cpi->cpi_pkgcoreid = 0;
11071228Sandrei 			}
11081228Sandrei 		} else if (cpi->cpi_vendor == X86_VENDOR_AMD) {
11091228Sandrei 			/*
11105870Sgavinm 			 * AMD CMP chips currently have a single thread per
11115870Sgavinm 			 * core, with 2 cores on family 0xf and 2, 3 or 4
11125870Sgavinm 			 * cores on family 0x10.
11135870Sgavinm 			 *
11145870Sgavinm 			 * Since no two cpus share a core we must assign a
11155870Sgavinm 			 * distinct coreid per cpu, and we do this by using
11165870Sgavinm 			 * the cpu_id.  This scheme does not, however,
11175870Sgavinm 			 * guarantee that sibling cores of a chip will have
11185870Sgavinm 			 * sequential coreids starting at a multiple of the
11195870Sgavinm 			 * number of cores per chip - that is usually the
11205870Sgavinm 			 * case, but if the ACPI MADT table is presented
11215870Sgavinm 			 * in a different order then we need to perform a
11225870Sgavinm 			 * few more gymnastics for the pkgcoreid.
11235870Sgavinm 			 *
11245870Sgavinm 			 * In family 0xf CMPs there are 2 cores on all nodes
11255870Sgavinm 			 * present - no mixing of single and dual core parts.
11265870Sgavinm 			 *
11275870Sgavinm 			 * In family 0x10 CMPs cpuid fn 2 ECX[15:12]
11285870Sgavinm 			 * "ApicIdCoreIdSize[3:0]" tells us how
11295870Sgavinm 			 * many least-significant bits in the ApicId
11305870Sgavinm 			 * are used to represent the core number
11315870Sgavinm 			 * within the node.  Cores are always
11325870Sgavinm 			 * numbered sequentially from 0 regardless
11335870Sgavinm 			 * of how many or which are disabled, and
11345870Sgavinm 			 * there seems to be no way to discover the
11355870Sgavinm 			 * real core id when some are disabled.
11361228Sandrei 			 */
11371228Sandrei 			cpi->cpi_coreid = cpu->cpu_id;
11385870Sgavinm 
11395870Sgavinm 			if (cpi->cpi_family == 0x10 &&
11405870Sgavinm 			    cpi->cpi_xmaxeax >= 0x80000008) {
11415870Sgavinm 				int coreidsz =
11425870Sgavinm 				    BITX((cpi)->cpi_extd[8].cp_ecx, 15, 12);
11435870Sgavinm 
11445870Sgavinm 				cpi->cpi_pkgcoreid =
11455870Sgavinm 				    apic_id & ((1 << coreidsz) - 1);
11465870Sgavinm 			} else {
11475870Sgavinm 				cpi->cpi_pkgcoreid = cpi->cpi_clogid;
11485870Sgavinm 			}
11491228Sandrei 		} else {
11501228Sandrei 			/*
11511228Sandrei 			 * All other processors are currently
11521228Sandrei 			 * assumed to have single cores.
11531228Sandrei 			 */
11541228Sandrei 			cpi->cpi_coreid = cpi->cpi_chipid;
11555870Sgavinm 			cpi->cpi_pkgcoreid = 0;
11561228Sandrei 		}
11570Sstevel@tonic-gate 	}
11580Sstevel@tonic-gate 
11597282Smishra 	cpi->cpi_apicid = CPI_APIC_ID(cpi);
11607282Smishra 
11612869Sgavinm 	/*
11622869Sgavinm 	 * Synthesize chip "revision" and socket type
11632869Sgavinm 	 */
11647532SSean.Ye@Sun.COM 	cpi->cpi_chiprev = _cpuid_chiprev(cpi->cpi_vendor, cpi->cpi_family,
11657532SSean.Ye@Sun.COM 	    cpi->cpi_model, cpi->cpi_step);
11667532SSean.Ye@Sun.COM 	cpi->cpi_chiprevstr = _cpuid_chiprevstr(cpi->cpi_vendor,
11677532SSean.Ye@Sun.COM 	    cpi->cpi_family, cpi->cpi_model, cpi->cpi_step);
11687532SSean.Ye@Sun.COM 	cpi->cpi_socket = _cpuid_skt(cpi->cpi_vendor, cpi->cpi_family,
11697532SSean.Ye@Sun.COM 	    cpi->cpi_model, cpi->cpi_step);
11702869Sgavinm 
11710Sstevel@tonic-gate pass1_done:
11725741Smrj #if !defined(__xpv)
11735741Smrj 	check_for_hvm();
11745741Smrj #endif
11750Sstevel@tonic-gate 	cpi->cpi_pass = 1;
11760Sstevel@tonic-gate 	return (feature);
11770Sstevel@tonic-gate }
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate /*
11800Sstevel@tonic-gate  * Make copies of the cpuid table entries we depend on, in
11810Sstevel@tonic-gate  * part for ease of parsing now, in part so that we have only
11820Sstevel@tonic-gate  * one place to correct any of it, in part for ease of
11830Sstevel@tonic-gate  * later export to userland, and in part so we can look at
11840Sstevel@tonic-gate  * this stuff in a crash dump.
11850Sstevel@tonic-gate  */
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate /*ARGSUSED*/
11880Sstevel@tonic-gate void
11890Sstevel@tonic-gate cpuid_pass2(cpu_t *cpu)
11900Sstevel@tonic-gate {
11910Sstevel@tonic-gate 	uint_t n, nmax;
11920Sstevel@tonic-gate 	int i;
11931228Sandrei 	struct cpuid_regs *cp;
11940Sstevel@tonic-gate 	uint8_t *dp;
11950Sstevel@tonic-gate 	uint32_t *iptr;
11960Sstevel@tonic-gate 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate 	ASSERT(cpi->cpi_pass == 1);
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 	if (cpi->cpi_maxeax < 1)
12010Sstevel@tonic-gate 		goto pass2_done;
12020Sstevel@tonic-gate 
12030Sstevel@tonic-gate 	if ((nmax = cpi->cpi_maxeax + 1) > NMAX_CPI_STD)
12040Sstevel@tonic-gate 		nmax = NMAX_CPI_STD;
12050Sstevel@tonic-gate 	/*
12060Sstevel@tonic-gate 	 * (We already handled n == 0 and n == 1 in pass 1)
12070Sstevel@tonic-gate 	 */
12080Sstevel@tonic-gate 	for (n = 2, cp = &cpi->cpi_std[2]; n < nmax; n++, cp++) {
12091228Sandrei 		cp->cp_eax = n;
12104606Sesaxe 
12114606Sesaxe 		/*
12124606Sesaxe 		 * CPUID function 4 expects %ecx to be initialized
12134606Sesaxe 		 * with an index which indicates which cache to return
12144606Sesaxe 		 * information about. The OS is expected to call function 4
12154606Sesaxe 		 * with %ecx set to 0, 1, 2, ... until it returns with
12164606Sesaxe 		 * EAX[4:0] set to 0, which indicates there are no more
12174606Sesaxe 		 * caches.
12184606Sesaxe 		 *
12194606Sesaxe 		 * Here, populate cpi_std[4] with the information returned by
12204606Sesaxe 		 * function 4 when %ecx == 0, and do the rest in cpuid_pass3()
12214606Sesaxe 		 * when dynamic memory allocation becomes available.
12224606Sesaxe 		 *
12234606Sesaxe 		 * Note: we need to explicitly initialize %ecx here, since
12244606Sesaxe 		 * function 4 may have been previously invoked.
12254606Sesaxe 		 */
12264606Sesaxe 		if (n == 4)
12274606Sesaxe 			cp->cp_ecx = 0;
12284606Sesaxe 
12291228Sandrei 		(void) __cpuid_insn(cp);
12303446Smrj 		platform_cpuid_mangle(cpi->cpi_vendor, n, cp);
12310Sstevel@tonic-gate 		switch (n) {
12320Sstevel@tonic-gate 		case 2:
12330Sstevel@tonic-gate 			/*
12340Sstevel@tonic-gate 			 * "the lower 8 bits of the %eax register
12350Sstevel@tonic-gate 			 * contain a value that identifies the number
12360Sstevel@tonic-gate 			 * of times the cpuid [instruction] has to be
12370Sstevel@tonic-gate 			 * executed to obtain a complete image of the
12380Sstevel@tonic-gate 			 * processor's caching systems."
12390Sstevel@tonic-gate 			 *
12400Sstevel@tonic-gate 			 * How *do* they make this stuff up?
12410Sstevel@tonic-gate 			 */
12420Sstevel@tonic-gate 			cpi->cpi_ncache = sizeof (*cp) *
12430Sstevel@tonic-gate 			    BITX(cp->cp_eax, 7, 0);
12440Sstevel@tonic-gate 			if (cpi->cpi_ncache == 0)
12450Sstevel@tonic-gate 				break;
12460Sstevel@tonic-gate 			cpi->cpi_ncache--;	/* skip count byte */
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate 			/*
12490Sstevel@tonic-gate 			 * Well, for now, rather than attempt to implement
12500Sstevel@tonic-gate 			 * this slightly dubious algorithm, we just look
12510Sstevel@tonic-gate 			 * at the first 15 ..
12520Sstevel@tonic-gate 			 */
12530Sstevel@tonic-gate 			if (cpi->cpi_ncache > (sizeof (*cp) - 1))
12540Sstevel@tonic-gate 				cpi->cpi_ncache = sizeof (*cp) - 1;
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate 			dp = cpi->cpi_cacheinfo;
12570Sstevel@tonic-gate 			if (BITX(cp->cp_eax, 31, 31) == 0) {
12580Sstevel@tonic-gate 				uint8_t *p = (void *)&cp->cp_eax;
12596317Skk208521 				for (i = 1; i < 4; i++)
12600Sstevel@tonic-gate 					if (p[i] != 0)
12610Sstevel@tonic-gate 						*dp++ = p[i];
12620Sstevel@tonic-gate 			}
12630Sstevel@tonic-gate 			if (BITX(cp->cp_ebx, 31, 31) == 0) {
12640Sstevel@tonic-gate 				uint8_t *p = (void *)&cp->cp_ebx;
12650Sstevel@tonic-gate 				for (i = 0; i < 4; i++)
12660Sstevel@tonic-gate 					if (p[i] != 0)
12670Sstevel@tonic-gate 						*dp++ = p[i];
12680Sstevel@tonic-gate 			}
12690Sstevel@tonic-gate 			if (BITX(cp->cp_ecx, 31, 31) == 0) {
12700Sstevel@tonic-gate 				uint8_t *p = (void *)&cp->cp_ecx;
12710Sstevel@tonic-gate 				for (i = 0; i < 4; i++)
12720Sstevel@tonic-gate 					if (p[i] != 0)
12730Sstevel@tonic-gate 						*dp++ = p[i];
12740Sstevel@tonic-gate 			}
12750Sstevel@tonic-gate 			if (BITX(cp->cp_edx, 31, 31) == 0) {
12760Sstevel@tonic-gate 				uint8_t *p = (void *)&cp->cp_edx;
12770Sstevel@tonic-gate 				for (i = 0; i < 4; i++)
12780Sstevel@tonic-gate 					if (p[i] != 0)
12790Sstevel@tonic-gate 						*dp++ = p[i];
12800Sstevel@tonic-gate 			}
12810Sstevel@tonic-gate 			break;
12824481Sbholler 
12830Sstevel@tonic-gate 		case 3:	/* Processor serial number, if PSN supported */
12844481Sbholler 			break;
12854481Sbholler 
12860Sstevel@tonic-gate 		case 4:	/* Deterministic cache parameters */
12874481Sbholler 			break;
12884481Sbholler 
12890Sstevel@tonic-gate 		case 5:	/* Monitor/Mwait parameters */
12905045Sbholler 		{
12915045Sbholler 			size_t mwait_size;
12924481Sbholler 
12934481Sbholler 			/*
12944481Sbholler 			 * check cpi_mwait.support which was set in cpuid_pass1
12954481Sbholler 			 */
12964481Sbholler 			if (!(cpi->cpi_mwait.support & MWAIT_SUPPORT))
12974481Sbholler 				break;
12984481Sbholler 
12995045Sbholler 			/*
13005045Sbholler 			 * Protect ourself from insane mwait line size.
13015045Sbholler 			 * Workaround for incomplete hardware emulator(s).
13025045Sbholler 			 */
13035045Sbholler 			mwait_size = (size_t)MWAIT_SIZE_MAX(cpi);
13045045Sbholler 			if (mwait_size < sizeof (uint32_t) ||
13055045Sbholler 			    !ISP2(mwait_size)) {
13065045Sbholler #if DEBUG
13075045Sbholler 				cmn_err(CE_NOTE, "Cannot handle cpu %d mwait "
13085045Sbholler 				    "size %ld",
13095045Sbholler 				    cpu->cpu_id, (long)mwait_size);
13105045Sbholler #endif
13115045Sbholler 				break;
13125045Sbholler 			}
13135045Sbholler 
13144481Sbholler 			cpi->cpi_mwait.mon_min = (size_t)MWAIT_SIZE_MIN(cpi);
13155045Sbholler 			cpi->cpi_mwait.mon_max = mwait_size;
13164481Sbholler 			if (MWAIT_EXTENSION(cpi)) {
13174481Sbholler 				cpi->cpi_mwait.support |= MWAIT_EXTENSIONS;
13184481Sbholler 				if (MWAIT_INT_ENABLE(cpi))
13194481Sbholler 					cpi->cpi_mwait.support |=
13204481Sbholler 					    MWAIT_ECX_INT_ENABLE;
13214481Sbholler 			}
13224481Sbholler 			break;
13235045Sbholler 		}
13240Sstevel@tonic-gate 		default:
13250Sstevel@tonic-gate 			break;
13260Sstevel@tonic-gate 		}
13270Sstevel@tonic-gate 	}
13280Sstevel@tonic-gate 
13297282Smishra 	if (cpi->cpi_maxeax >= 0xB && cpi->cpi_vendor == X86_VENDOR_Intel) {
13307282Smishra 		cp->cp_eax = 0xB;
13317282Smishra 		cp->cp_ecx = 0;
13327282Smishra 
13337282Smishra 		(void) __cpuid_insn(cp);
13347282Smishra 
13357282Smishra 		/*
13367282Smishra 		 * Check CPUID.EAX=0BH, ECX=0H:EBX is non-zero, which
13377282Smishra 		 * indicates that the extended topology enumeration leaf is
13387282Smishra 		 * available.
13397282Smishra 		 */
13407282Smishra 		if (cp->cp_ebx) {
13417282Smishra 			uint32_t x2apic_id;
13427282Smishra 			uint_t coreid_shift = 0;
13437282Smishra 			uint_t ncpu_per_core = 1;
13447282Smishra 			uint_t chipid_shift = 0;
13457282Smishra 			uint_t ncpu_per_chip = 1;
13467282Smishra 			uint_t i;
13477282Smishra 			uint_t level;
13487282Smishra 
13497282Smishra 			for (i = 0; i < CPI_FNB_ECX_MAX; i++) {
13507282Smishra 				cp->cp_eax = 0xB;
13517282Smishra 				cp->cp_ecx = i;
13527282Smishra 
13537282Smishra 				(void) __cpuid_insn(cp);
13547282Smishra 				level = CPI_CPU_LEVEL_TYPE(cp);
13557282Smishra 
13567282Smishra 				if (level == 1) {
13577282Smishra 					x2apic_id = cp->cp_edx;
13587282Smishra 					coreid_shift = BITX(cp->cp_eax, 4, 0);
13597282Smishra 					ncpu_per_core = BITX(cp->cp_ebx, 15, 0);
13607282Smishra 				} else if (level == 2) {
13617282Smishra 					x2apic_id = cp->cp_edx;
13627282Smishra 					chipid_shift = BITX(cp->cp_eax, 4, 0);
13637282Smishra 					ncpu_per_chip = BITX(cp->cp_ebx, 15, 0);
13647282Smishra 				}
13657282Smishra 			}
13667282Smishra 
13677282Smishra 			cpi->cpi_apicid = x2apic_id;
13687282Smishra 			cpi->cpi_ncpu_per_chip = ncpu_per_chip;
13697282Smishra 			cpi->cpi_ncore_per_chip = ncpu_per_chip /
13707282Smishra 			    ncpu_per_core;
13717282Smishra 			cpi->cpi_chipid = x2apic_id >> chipid_shift;
13727282Smishra 			cpi->cpi_clogid = x2apic_id & ((1 << chipid_shift) - 1);
13737282Smishra 			cpi->cpi_coreid = x2apic_id >> coreid_shift;
13747282Smishra 			cpi->cpi_pkgcoreid = cpi->cpi_clogid >> coreid_shift;
13757282Smishra 		}
13767282Smishra 	}
13777282Smishra 
13780Sstevel@tonic-gate 	if ((cpi->cpi_xmaxeax & 0x80000000) == 0)
13790Sstevel@tonic-gate 		goto pass2_done;
13800Sstevel@tonic-gate 
13810Sstevel@tonic-gate 	if ((nmax = cpi->cpi_xmaxeax - 0x80000000 + 1) > NMAX_CPI_EXTD)
13820Sstevel@tonic-gate 		nmax = NMAX_CPI_EXTD;
13830Sstevel@tonic-gate 	/*
13840Sstevel@tonic-gate 	 * Copy the extended properties, fixing them as we go.
13850Sstevel@tonic-gate 	 * (We already handled n == 0 and n == 1 in pass 1)
13860Sstevel@tonic-gate 	 */
13870Sstevel@tonic-gate 	iptr = (void *)cpi->cpi_brandstr;
13880Sstevel@tonic-gate 	for (n = 2, cp = &cpi->cpi_extd[2]; n < nmax; cp++, n++) {
13891228Sandrei 		cp->cp_eax = 0x80000000 + n;
13901228Sandrei 		(void) __cpuid_insn(cp);
13913446Smrj 		platform_cpuid_mangle(cpi->cpi_vendor, 0x80000000 + n, cp);
13920Sstevel@tonic-gate 		switch (n) {
13930Sstevel@tonic-gate 		case 2:
13940Sstevel@tonic-gate 		case 3:
13950Sstevel@tonic-gate 		case 4:
13960Sstevel@tonic-gate 			/*
13970Sstevel@tonic-gate 			 * Extract the brand string
13980Sstevel@tonic-gate 			 */
13990Sstevel@tonic-gate 			*iptr++ = cp->cp_eax;
14000Sstevel@tonic-gate 			*iptr++ = cp->cp_ebx;
14010Sstevel@tonic-gate 			*iptr++ = cp->cp_ecx;
14020Sstevel@tonic-gate 			*iptr++ = cp->cp_edx;
14030Sstevel@tonic-gate 			break;
14040Sstevel@tonic-gate 		case 5:
14050Sstevel@tonic-gate 			switch (cpi->cpi_vendor) {
14060Sstevel@tonic-gate 			case X86_VENDOR_AMD:
14070Sstevel@tonic-gate 				/*
14080Sstevel@tonic-gate 				 * The Athlon and Duron were the first
14090Sstevel@tonic-gate 				 * parts to report the sizes of the
14100Sstevel@tonic-gate 				 * TLB for large pages. Before then,
14110Sstevel@tonic-gate 				 * we don't trust the data.
14120Sstevel@tonic-gate 				 */
14130Sstevel@tonic-gate 				if (cpi->cpi_family < 6 ||
14140Sstevel@tonic-gate 				    (cpi->cpi_family == 6 &&
14150Sstevel@tonic-gate 				    cpi->cpi_model < 1))
14160Sstevel@tonic-gate 					cp->cp_eax = 0;
14170Sstevel@tonic-gate 				break;
14180Sstevel@tonic-gate 			default:
14190Sstevel@tonic-gate 				break;
14200Sstevel@tonic-gate 			}
14210Sstevel@tonic-gate 			break;
14220Sstevel@tonic-gate 		case 6:
14230Sstevel@tonic-gate 			switch (cpi->cpi_vendor) {
14240Sstevel@tonic-gate 			case X86_VENDOR_AMD:
14250Sstevel@tonic-gate 				/*
14260Sstevel@tonic-gate 				 * The Athlon and Duron were the first
14270Sstevel@tonic-gate 				 * AMD parts with L2 TLB's.
14280Sstevel@tonic-gate 				 * Before then, don't trust the data.
14290Sstevel@tonic-gate 				 */
14300Sstevel@tonic-gate 				if (cpi->cpi_family < 6 ||
14310Sstevel@tonic-gate 				    cpi->cpi_family == 6 &&
14320Sstevel@tonic-gate 				    cpi->cpi_model < 1)
14330Sstevel@tonic-gate 					cp->cp_eax = cp->cp_ebx = 0;
14340Sstevel@tonic-gate 				/*
14350Sstevel@tonic-gate 				 * AMD Duron rev A0 reports L2
14360Sstevel@tonic-gate 				 * cache size incorrectly as 1K
14370Sstevel@tonic-gate 				 * when it is really 64K
14380Sstevel@tonic-gate 				 */
14390Sstevel@tonic-gate 				if (cpi->cpi_family == 6 &&
14400Sstevel@tonic-gate 				    cpi->cpi_model == 3 &&
14410Sstevel@tonic-gate 				    cpi->cpi_step == 0) {
14420Sstevel@tonic-gate 					cp->cp_ecx &= 0xffff;
14430Sstevel@tonic-gate 					cp->cp_ecx |= 0x400000;
14440Sstevel@tonic-gate 				}
14450Sstevel@tonic-gate 				break;
14460Sstevel@tonic-gate 			case X86_VENDOR_Cyrix:	/* VIA C3 */
14470Sstevel@tonic-gate 				/*
14480Sstevel@tonic-gate 				 * VIA C3 processors are a bit messed
14490Sstevel@tonic-gate 				 * up w.r.t. encoding cache sizes in %ecx
14500Sstevel@tonic-gate 				 */
14510Sstevel@tonic-gate 				if (cpi->cpi_family != 6)
14520Sstevel@tonic-gate 					break;
14530Sstevel@tonic-gate 				/*
14540Sstevel@tonic-gate 				 * model 7 and 8 were incorrectly encoded
14550Sstevel@tonic-gate 				 *
14560Sstevel@tonic-gate 				 * xxx is model 8 really broken?
14570Sstevel@tonic-gate 				 */
14580Sstevel@tonic-gate 				if (cpi->cpi_model == 7 ||
14590Sstevel@tonic-gate 				    cpi->cpi_model == 8)
14600Sstevel@tonic-gate 					cp->cp_ecx =
14610Sstevel@tonic-gate 					    BITX(cp->cp_ecx, 31, 24) << 16 |
14620Sstevel@tonic-gate 					    BITX(cp->cp_ecx, 23, 16) << 12 |
14630Sstevel@tonic-gate 					    BITX(cp->cp_ecx, 15, 8) << 8 |
14640Sstevel@tonic-gate 					    BITX(cp->cp_ecx, 7, 0);
14650Sstevel@tonic-gate 				/*
14660Sstevel@tonic-gate 				 * model 9 stepping 1 has wrong associativity
14670Sstevel@tonic-gate 				 */
14680Sstevel@tonic-gate 				if (cpi->cpi_model == 9 && cpi->cpi_step == 1)
14690Sstevel@tonic-gate 					cp->cp_ecx |= 8 << 12;
14700Sstevel@tonic-gate 				break;
14710Sstevel@tonic-gate 			case X86_VENDOR_Intel:
14720Sstevel@tonic-gate 				/*
14730Sstevel@tonic-gate 				 * Extended L2 Cache features function.
14740Sstevel@tonic-gate 				 * First appeared on Prescott.
14750Sstevel@tonic-gate 				 */
14760Sstevel@tonic-gate 			default:
14770Sstevel@tonic-gate 				break;
14780Sstevel@tonic-gate 			}
14790Sstevel@tonic-gate 			break;
14800Sstevel@tonic-gate 		default:
14810Sstevel@tonic-gate 			break;
14820Sstevel@tonic-gate 		}
14830Sstevel@tonic-gate 	}
14840Sstevel@tonic-gate 
14850Sstevel@tonic-gate pass2_done:
14860Sstevel@tonic-gate 	cpi->cpi_pass = 2;
14870Sstevel@tonic-gate }
14880Sstevel@tonic-gate 
14890Sstevel@tonic-gate static const char *
14900Sstevel@tonic-gate intel_cpubrand(const struct cpuid_info *cpi)
14910Sstevel@tonic-gate {
14920Sstevel@tonic-gate 	int i;
14930Sstevel@tonic-gate 
14940Sstevel@tonic-gate 	if ((x86_feature & X86_CPUID) == 0 ||
14950Sstevel@tonic-gate 	    cpi->cpi_maxeax < 1 || cpi->cpi_family < 5)
14960Sstevel@tonic-gate 		return ("i486");
14970Sstevel@tonic-gate 
14980Sstevel@tonic-gate 	switch (cpi->cpi_family) {
14990Sstevel@tonic-gate 	case 5:
15000Sstevel@tonic-gate 		return ("Intel Pentium(r)");
15010Sstevel@tonic-gate 	case 6:
15020Sstevel@tonic-gate 		switch (cpi->cpi_model) {
15030Sstevel@tonic-gate 			uint_t celeron, xeon;
15041228Sandrei 			const struct cpuid_regs *cp;
15050Sstevel@tonic-gate 		case 0:
15060Sstevel@tonic-gate 		case 1:
15070Sstevel@tonic-gate 		case 2:
15080Sstevel@tonic-gate 			return ("Intel Pentium(r) Pro");
15090Sstevel@tonic-gate 		case 3:
15100Sstevel@tonic-gate 		case 4:
15110Sstevel@tonic-gate 			return ("Intel Pentium(r) II");
15120Sstevel@tonic-gate 		case 6:
15130Sstevel@tonic-gate 			return ("Intel Celeron(r)");
15140Sstevel@tonic-gate 		case 5:
15150Sstevel@tonic-gate 		case 7:
15160Sstevel@tonic-gate 			celeron = xeon = 0;
15170Sstevel@tonic-gate 			cp = &cpi->cpi_std[2];	/* cache info */
15180Sstevel@tonic-gate 
15196317Skk208521 			for (i = 1; i < 4; i++) {
15200Sstevel@tonic-gate 				uint_t tmp;
15210Sstevel@tonic-gate 
15220Sstevel@tonic-gate 				tmp = (cp->cp_eax >> (8 * i)) & 0xff;
15230Sstevel@tonic-gate 				if (tmp == 0x40)
15240Sstevel@tonic-gate 					celeron++;
15250Sstevel@tonic-gate 				if (tmp >= 0x44 && tmp <= 0x45)
15260Sstevel@tonic-gate 					xeon++;
15270Sstevel@tonic-gate 			}
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate 			for (i = 0; i < 2; i++) {
15300Sstevel@tonic-gate 				uint_t tmp;
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate 				tmp = (cp->cp_ebx >> (8 * i)) & 0xff;
15330Sstevel@tonic-gate 				if (tmp == 0x40)
15340Sstevel@tonic-gate 					celeron++;
15350Sstevel@tonic-gate 				else if (tmp >= 0x44 && tmp <= 0x45)
15360Sstevel@tonic-gate 					xeon++;
15370Sstevel@tonic-gate 			}
15380Sstevel@tonic-gate 
15390Sstevel@tonic-gate 			for (i = 0; i < 4; i++) {
15400Sstevel@tonic-gate 				uint_t tmp;
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate 				tmp = (cp->cp_ecx >> (8 * i)) & 0xff;
15430Sstevel@tonic-gate 				if (tmp == 0x40)
15440Sstevel@tonic-gate 					celeron++;
15450Sstevel@tonic-gate 				else if (tmp >= 0x44 && tmp <= 0x45)
15460Sstevel@tonic-gate 					xeon++;
15470Sstevel@tonic-gate 			}
15480Sstevel@tonic-gate 
15490Sstevel@tonic-gate 			for (i = 0; i < 4; i++) {
15500Sstevel@tonic-gate 				uint_t tmp;
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate 				tmp = (cp->cp_edx >> (8 * i)) & 0xff;
15530Sstevel@tonic-gate 				if (tmp == 0x40)
15540Sstevel@tonic-gate 					celeron++;
15550Sstevel@tonic-gate 				else if (tmp >= 0x44 && tmp <= 0x45)
15560Sstevel@tonic-gate 					xeon++;
15570Sstevel@tonic-gate 			}
15580Sstevel@tonic-gate 
15590Sstevel@tonic-gate 			if (celeron)
15600Sstevel@tonic-gate 				return ("Intel Celeron(r)");
15610Sstevel@tonic-gate 			if (xeon)
15620Sstevel@tonic-gate 				return (cpi->cpi_model == 5 ?
15630Sstevel@tonic-gate 				    "Intel Pentium(r) II Xeon(tm)" :
15640Sstevel@tonic-gate 				    "Intel Pentium(r) III Xeon(tm)");
15650Sstevel@tonic-gate 			return (cpi->cpi_model == 5 ?
15660Sstevel@tonic-gate 			    "Intel Pentium(r) II or Pentium(r) II Xeon(tm)" :
15670Sstevel@tonic-gate 			    "Intel Pentium(r) III or Pentium(r) III Xeon(tm)");
15680Sstevel@tonic-gate 		default:
15690Sstevel@tonic-gate 			break;
15700Sstevel@tonic-gate 		}
15710Sstevel@tonic-gate 	default:
15720Sstevel@tonic-gate 		break;
15730Sstevel@tonic-gate 	}
15740Sstevel@tonic-gate 
15751975Sdmick 	/* BrandID is present if the field is nonzero */
15761975Sdmick 	if (cpi->cpi_brandid != 0) {
15770Sstevel@tonic-gate 		static const struct {
15780Sstevel@tonic-gate 			uint_t bt_bid;
15790Sstevel@tonic-gate 			const char *bt_str;
15800Sstevel@tonic-gate 		} brand_tbl[] = {
15810Sstevel@tonic-gate 			{ 0x1,	"Intel(r) Celeron(r)" },
15820Sstevel@tonic-gate 			{ 0x2,	"Intel(r) Pentium(r) III" },
15830Sstevel@tonic-gate 			{ 0x3,	"Intel(r) Pentium(r) III Xeon(tm)" },
15840Sstevel@tonic-gate 			{ 0x4,	"Intel(r) Pentium(r) III" },
15850Sstevel@tonic-gate 			{ 0x6,	"Mobile Intel(r) Pentium(r) III" },
15860Sstevel@tonic-gate 			{ 0x7,	"Mobile Intel(r) Celeron(r)" },
15870Sstevel@tonic-gate 			{ 0x8,	"Intel(r) Pentium(r) 4" },
15880Sstevel@tonic-gate 			{ 0x9,	"Intel(r) Pentium(r) 4" },
15890Sstevel@tonic-gate 			{ 0xa,	"Intel(r) Celeron(r)" },
15900Sstevel@tonic-gate 			{ 0xb,	"Intel(r) Xeon(tm)" },
15910Sstevel@tonic-gate 			{ 0xc,	"Intel(r) Xeon(tm) MP" },
15920Sstevel@tonic-gate 			{ 0xe,	"Mobile Intel(r) Pentium(r) 4" },
15931975Sdmick 			{ 0xf,	"Mobile Intel(r) Celeron(r)" },
15941975Sdmick 			{ 0x11, "Mobile Genuine Intel(r)" },
15951975Sdmick 			{ 0x12, "Intel(r) Celeron(r) M" },
15961975Sdmick 			{ 0x13, "Mobile Intel(r) Celeron(r)" },
15971975Sdmick 			{ 0x14, "Intel(r) Celeron(r)" },
15981975Sdmick 			{ 0x15, "Mobile Genuine Intel(r)" },
15991975Sdmick 			{ 0x16,	"Intel(r) Pentium(r) M" },
16001975Sdmick 			{ 0x17, "Mobile Intel(r) Celeron(r)" }
16010Sstevel@tonic-gate 		};
16020Sstevel@tonic-gate 		uint_t btblmax = sizeof (brand_tbl) / sizeof (brand_tbl[0]);
16030Sstevel@tonic-gate 		uint_t sgn;
16040Sstevel@tonic-gate 
16050Sstevel@tonic-gate 		sgn = (cpi->cpi_family << 8) |
16060Sstevel@tonic-gate 		    (cpi->cpi_model << 4) | cpi->cpi_step;
16070Sstevel@tonic-gate 
16080Sstevel@tonic-gate 		for (i = 0; i < btblmax; i++)
16090Sstevel@tonic-gate 			if (brand_tbl[i].bt_bid == cpi->cpi_brandid)
16100Sstevel@tonic-gate 				break;
16110Sstevel@tonic-gate 		if (i < btblmax) {
16120Sstevel@tonic-gate 			if (sgn == 0x6b1 && cpi->cpi_brandid == 3)
16130Sstevel@tonic-gate 				return ("Intel(r) Celeron(r)");
16140Sstevel@tonic-gate 			if (sgn < 0xf13 && cpi->cpi_brandid == 0xb)
16150Sstevel@tonic-gate 				return ("Intel(r) Xeon(tm) MP");
16160Sstevel@tonic-gate 			if (sgn < 0xf13 && cpi->cpi_brandid == 0xe)
16170Sstevel@tonic-gate 				return ("Intel(r) Xeon(tm)");
16180Sstevel@tonic-gate 			return (brand_tbl[i].bt_str);
16190Sstevel@tonic-gate 		}
16200Sstevel@tonic-gate 	}
16210Sstevel@tonic-gate 
16220Sstevel@tonic-gate 	return (NULL);
16230Sstevel@tonic-gate }
16240Sstevel@tonic-gate 
16250Sstevel@tonic-gate static const char *
16260Sstevel@tonic-gate amd_cpubrand(const struct cpuid_info *cpi)
16270Sstevel@tonic-gate {
16280Sstevel@tonic-gate 	if ((x86_feature & X86_CPUID) == 0 ||
16290Sstevel@tonic-gate 	    cpi->cpi_maxeax < 1 || cpi->cpi_family < 5)
16300Sstevel@tonic-gate 		return ("i486 compatible");
16310Sstevel@tonic-gate 
16320Sstevel@tonic-gate 	switch (cpi->cpi_family) {
16330Sstevel@tonic-gate 	case 5:
16340Sstevel@tonic-gate 		switch (cpi->cpi_model) {
16350Sstevel@tonic-gate 		case 0:
16360Sstevel@tonic-gate 		case 1:
16370Sstevel@tonic-gate 		case 2:
16380Sstevel@tonic-gate 		case 3:
16390Sstevel@tonic-gate 		case 4:
16400Sstevel@tonic-gate 		case 5:
16410Sstevel@tonic-gate 			return ("AMD-K5(r)");
16420Sstevel@tonic-gate 		case 6:
16430Sstevel@tonic-gate 		case 7:
16440Sstevel@tonic-gate 			return ("AMD-K6(r)");
16450Sstevel@tonic-gate 		case 8:
16460Sstevel@tonic-gate 			return ("AMD-K6(r)-2");
16470Sstevel@tonic-gate 		case 9:
16480Sstevel@tonic-gate 			return ("AMD-K6(r)-III");
16490Sstevel@tonic-gate 		default:
16500Sstevel@tonic-gate 			return ("AMD (family 5)");
16510Sstevel@tonic-gate 		}
16520Sstevel@tonic-gate 	case 6:
16530Sstevel@tonic-gate 		switch (cpi->cpi_model) {
16540Sstevel@tonic-gate 		case 1:
16550Sstevel@tonic-gate 			return ("AMD-K7(tm)");
16560Sstevel@tonic-gate 		case 0:
16570Sstevel@tonic-gate 		case 2:
16580Sstevel@tonic-gate 		case 4:
16590Sstevel@tonic-gate 			return ("AMD Athlon(tm)");
16600Sstevel@tonic-gate 		case 3:
16610Sstevel@tonic-gate 		case 7:
16620Sstevel@tonic-gate 			return ("AMD Duron(tm)");
16630Sstevel@tonic-gate 		case 6:
16640Sstevel@tonic-gate 		case 8:
16650Sstevel@tonic-gate 		case 10:
16660Sstevel@tonic-gate 			/*
16670Sstevel@tonic-gate 			 * Use the L2 cache size to distinguish
16680Sstevel@tonic-gate 			 */
16690Sstevel@tonic-gate 			return ((cpi->cpi_extd[6].cp_ecx >> 16) >= 256 ?
16700Sstevel@tonic-gate 			    "AMD Athlon(tm)" : "AMD Duron(tm)");
16710Sstevel@tonic-gate 		default:
16720Sstevel@tonic-gate 			return ("AMD (family 6)");
16730Sstevel@tonic-gate 		}
16740Sstevel@tonic-gate 	default:
16750Sstevel@tonic-gate 		break;
16760Sstevel@tonic-gate 	}
16770Sstevel@tonic-gate 
16780Sstevel@tonic-gate 	if (cpi->cpi_family == 0xf && cpi->cpi_model == 5 &&
16790Sstevel@tonic-gate 	    cpi->cpi_brandid != 0) {
16800Sstevel@tonic-gate 		switch (BITX(cpi->cpi_brandid, 7, 5)) {
16810Sstevel@tonic-gate 		case 3:
16820Sstevel@tonic-gate 			return ("AMD Opteron(tm) UP 1xx");
16830Sstevel@tonic-gate 		case 4:
16840Sstevel@tonic-gate 			return ("AMD Opteron(tm) DP 2xx");
16850Sstevel@tonic-gate 		case 5:
16860Sstevel@tonic-gate 			return ("AMD Opteron(tm) MP 8xx");
16870Sstevel@tonic-gate 		default:
16880Sstevel@tonic-gate 			return ("AMD Opteron(tm)");
16890Sstevel@tonic-gate 		}
16900Sstevel@tonic-gate 	}
16910Sstevel@tonic-gate 
16920Sstevel@tonic-gate 	return (NULL);
16930Sstevel@tonic-gate }
16940Sstevel@tonic-gate 
16950Sstevel@tonic-gate static const char *
16960Sstevel@tonic-gate cyrix_cpubrand(struct cpuid_info *cpi, uint_t type)
16970Sstevel@tonic-gate {
16980Sstevel@tonic-gate 	if ((x86_feature & X86_CPUID) == 0 ||
16990Sstevel@tonic-gate 	    cpi->cpi_maxeax < 1 || cpi->cpi_family < 5 ||
17000Sstevel@tonic-gate 	    type == X86_TYPE_CYRIX_486)
17010Sstevel@tonic-gate 		return ("i486 compatible");
17020Sstevel@tonic-gate 
17030Sstevel@tonic-gate 	switch (type) {
17040Sstevel@tonic-gate 	case X86_TYPE_CYRIX_6x86:
17050Sstevel@tonic-gate 		return ("Cyrix 6x86");
17060Sstevel@tonic-gate 	case X86_TYPE_CYRIX_6x86L:
17070Sstevel@tonic-gate 		return ("Cyrix 6x86L");
17080Sstevel@tonic-gate 	case X86_TYPE_CYRIX_6x86MX:
17090Sstevel@tonic-gate 		return ("Cyrix 6x86MX");
17100Sstevel@tonic-gate 	case X86_TYPE_CYRIX_GXm:
17110Sstevel@tonic-gate 		return ("Cyrix GXm");
17120Sstevel@tonic-gate 	case X86_TYPE_CYRIX_MediaGX:
17130Sstevel@tonic-gate 		return ("Cyrix MediaGX");
17140Sstevel@tonic-gate 	case X86_TYPE_CYRIX_MII:
17150Sstevel@tonic-gate 		return ("Cyrix M2");
17160Sstevel@tonic-gate 	case X86_TYPE_VIA_CYRIX_III:
17170Sstevel@tonic-gate 		return ("VIA Cyrix M3");
17180Sstevel@tonic-gate 	default:
17190Sstevel@tonic-gate 		/*
17200Sstevel@tonic-gate 		 * Have another wild guess ..
17210Sstevel@tonic-gate 		 */
17220Sstevel@tonic-gate 		if (cpi->cpi_family == 4 && cpi->cpi_model == 9)
17230Sstevel@tonic-gate 			return ("Cyrix 5x86");
17240Sstevel@tonic-gate 		else if (cpi->cpi_family == 5) {
17250Sstevel@tonic-gate 			switch (cpi->cpi_model) {
17260Sstevel@tonic-gate 			case 2:
17270Sstevel@tonic-gate 				return ("Cyrix 6x86");	/* Cyrix M1 */
17280Sstevel@tonic-gate 			case 4:
17290Sstevel@tonic-gate 				return ("Cyrix MediaGX");
17300Sstevel@tonic-gate 			default:
17310Sstevel@tonic-gate 				break;
17320Sstevel@tonic-gate 			}
17330Sstevel@tonic-gate 		} else if (cpi->cpi_family == 6) {
17340Sstevel@tonic-gate 			switch (cpi->cpi_model) {
17350Sstevel@tonic-gate 			case 0:
17360Sstevel@tonic-gate 				return ("Cyrix 6x86MX"); /* Cyrix M2? */
17370Sstevel@tonic-gate 			case 5:
17380Sstevel@tonic-gate 			case 6:
17390Sstevel@tonic-gate 			case 7:
17400Sstevel@tonic-gate 			case 8:
17410Sstevel@tonic-gate 			case 9:
17420Sstevel@tonic-gate 				return ("VIA C3");
17430Sstevel@tonic-gate 			default:
17440Sstevel@tonic-gate 				break;
17450Sstevel@tonic-gate 			}
17460Sstevel@tonic-gate 		}
17470Sstevel@tonic-gate 		break;
17480Sstevel@tonic-gate 	}
17490Sstevel@tonic-gate 	return (NULL);
17500Sstevel@tonic-gate }
17510Sstevel@tonic-gate 
17520Sstevel@tonic-gate /*
17530Sstevel@tonic-gate  * This only gets called in the case that the CPU extended
17540Sstevel@tonic-gate  * feature brand string (0x80000002, 0x80000003, 0x80000004)
17550Sstevel@tonic-gate  * aren't available, or contain null bytes for some reason.
17560Sstevel@tonic-gate  */
17570Sstevel@tonic-gate static void
17580Sstevel@tonic-gate fabricate_brandstr(struct cpuid_info *cpi)
17590Sstevel@tonic-gate {
17600Sstevel@tonic-gate 	const char *brand = NULL;
17610Sstevel@tonic-gate 
17620Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
17630Sstevel@tonic-gate 	case X86_VENDOR_Intel:
17640Sstevel@tonic-gate 		brand = intel_cpubrand(cpi);
17650Sstevel@tonic-gate 		break;
17660Sstevel@tonic-gate 	case X86_VENDOR_AMD:
17670Sstevel@tonic-gate 		brand = amd_cpubrand(cpi);
17680Sstevel@tonic-gate 		break;
17690Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
17700Sstevel@tonic-gate 		brand = cyrix_cpubrand(cpi, x86_type);
17710Sstevel@tonic-gate 		break;
17720Sstevel@tonic-gate 	case X86_VENDOR_NexGen:
17730Sstevel@tonic-gate 		if (cpi->cpi_family == 5 && cpi->cpi_model == 0)
17740Sstevel@tonic-gate 			brand = "NexGen Nx586";
17750Sstevel@tonic-gate 		break;
17760Sstevel@tonic-gate 	case X86_VENDOR_Centaur:
17770Sstevel@tonic-gate 		if (cpi->cpi_family == 5)
17780Sstevel@tonic-gate 			switch (cpi->cpi_model) {
17790Sstevel@tonic-gate 			case 4:
17800Sstevel@tonic-gate 				brand = "Centaur C6";
17810Sstevel@tonic-gate 				break;
17820Sstevel@tonic-gate 			case 8:
17830Sstevel@tonic-gate 				brand = "Centaur C2";
17840Sstevel@tonic-gate 				break;
17850Sstevel@tonic-gate 			case 9:
17860Sstevel@tonic-gate 				brand = "Centaur C3";
17870Sstevel@tonic-gate 				break;
17880Sstevel@tonic-gate 			default:
17890Sstevel@tonic-gate 				break;
17900Sstevel@tonic-gate 			}
17910Sstevel@tonic-gate 		break;
17920Sstevel@tonic-gate 	case X86_VENDOR_Rise:
17930Sstevel@tonic-gate 		if (cpi->cpi_family == 5 &&
17940Sstevel@tonic-gate 		    (cpi->cpi_model == 0 || cpi->cpi_model == 2))
17950Sstevel@tonic-gate 			brand = "Rise mP6";
17960Sstevel@tonic-gate 		break;
17970Sstevel@tonic-gate 	case X86_VENDOR_SiS:
17980Sstevel@tonic-gate 		if (cpi->cpi_family == 5 && cpi->cpi_model == 0)
17990Sstevel@tonic-gate 			brand = "SiS 55x";
18000Sstevel@tonic-gate 		break;
18010Sstevel@tonic-gate 	case X86_VENDOR_TM:
18020Sstevel@tonic-gate 		if (cpi->cpi_family == 5 && cpi->cpi_model == 4)
18030Sstevel@tonic-gate 			brand = "Transmeta Crusoe TM3x00 or TM5x00";
18040Sstevel@tonic-gate 		break;
18050Sstevel@tonic-gate 	case X86_VENDOR_NSC:
18060Sstevel@tonic-gate 	case X86_VENDOR_UMC:
18070Sstevel@tonic-gate 	default:
18080Sstevel@tonic-gate 		break;
18090Sstevel@tonic-gate 	}
18100Sstevel@tonic-gate 	if (brand) {
18110Sstevel@tonic-gate 		(void) strcpy((char *)cpi->cpi_brandstr, brand);
18120Sstevel@tonic-gate 		return;
18130Sstevel@tonic-gate 	}
18140Sstevel@tonic-gate 
18150Sstevel@tonic-gate 	/*
18160Sstevel@tonic-gate 	 * If all else fails ...
18170Sstevel@tonic-gate 	 */
18180Sstevel@tonic-gate 	(void) snprintf(cpi->cpi_brandstr, sizeof (cpi->cpi_brandstr),
18190Sstevel@tonic-gate 	    "%s %d.%d.%d", cpi->cpi_vendorstr, cpi->cpi_family,
18200Sstevel@tonic-gate 	    cpi->cpi_model, cpi->cpi_step);
18210Sstevel@tonic-gate }
18220Sstevel@tonic-gate 
18230Sstevel@tonic-gate /*
18240Sstevel@tonic-gate  * This routine is called just after kernel memory allocation
18250Sstevel@tonic-gate  * becomes available on cpu0, and as part of mp_startup() on
18260Sstevel@tonic-gate  * the other cpus.
18270Sstevel@tonic-gate  *
18284606Sesaxe  * Fixup the brand string, and collect any information from cpuid
18294606Sesaxe  * that requires dynamicically allocated storage to represent.
18300Sstevel@tonic-gate  */
18310Sstevel@tonic-gate /*ARGSUSED*/
18320Sstevel@tonic-gate void
18330Sstevel@tonic-gate cpuid_pass3(cpu_t *cpu)
18340Sstevel@tonic-gate {
18354606Sesaxe 	int	i, max, shft, level, size;
18364606Sesaxe 	struct cpuid_regs regs;
18374606Sesaxe 	struct cpuid_regs *cp;
18380Sstevel@tonic-gate 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
18390Sstevel@tonic-gate 
18400Sstevel@tonic-gate 	ASSERT(cpi->cpi_pass == 2);
18410Sstevel@tonic-gate 
18424606Sesaxe 	/*
18434606Sesaxe 	 * Function 4: Deterministic cache parameters
18444606Sesaxe 	 *
18454606Sesaxe 	 * Take this opportunity to detect the number of threads
18464606Sesaxe 	 * sharing the last level cache, and construct a corresponding
18474606Sesaxe 	 * cache id. The respective cpuid_info members are initialized
18484606Sesaxe 	 * to the default case of "no last level cache sharing".
18494606Sesaxe 	 */
18504606Sesaxe 	cpi->cpi_ncpu_shr_last_cache = 1;
18514606Sesaxe 	cpi->cpi_last_lvl_cacheid = cpu->cpu_id;
18524606Sesaxe 
18534606Sesaxe 	if (cpi->cpi_maxeax >= 4 && cpi->cpi_vendor == X86_VENDOR_Intel) {
18544606Sesaxe 
18554606Sesaxe 		/*
18564606Sesaxe 		 * Find the # of elements (size) returned by fn 4, and along
18574606Sesaxe 		 * the way detect last level cache sharing details.
18584606Sesaxe 		 */
18594606Sesaxe 		bzero(&regs, sizeof (regs));
18604606Sesaxe 		cp = &regs;
18614606Sesaxe 		for (i = 0, max = 0; i < CPI_FN4_ECX_MAX; i++) {
18624606Sesaxe 			cp->cp_eax = 4;
18634606Sesaxe 			cp->cp_ecx = i;
18644606Sesaxe 
18654606Sesaxe 			(void) __cpuid_insn(cp);
18664606Sesaxe 
18674606Sesaxe 			if (CPI_CACHE_TYPE(cp) == 0)
18684606Sesaxe 				break;
18694606Sesaxe 			level = CPI_CACHE_LVL(cp);
18704606Sesaxe 			if (level > max) {
18714606Sesaxe 				max = level;
18724606Sesaxe 				cpi->cpi_ncpu_shr_last_cache =
18734606Sesaxe 				    CPI_NTHR_SHR_CACHE(cp) + 1;
18744606Sesaxe 			}
18754606Sesaxe 		}
18764606Sesaxe 		cpi->cpi_std_4_size = size = i;
18774606Sesaxe 
18784606Sesaxe 		/*
18794606Sesaxe 		 * Allocate the cpi_std_4 array. The first element
18804606Sesaxe 		 * references the regs for fn 4, %ecx == 0, which
18814606Sesaxe 		 * cpuid_pass2() stashed in cpi->cpi_std[4].
18824606Sesaxe 		 */
18834606Sesaxe 		if (size > 0) {
18844606Sesaxe 			cpi->cpi_std_4 =
18854606Sesaxe 			    kmem_alloc(size * sizeof (cp), KM_SLEEP);
18864606Sesaxe 			cpi->cpi_std_4[0] = &cpi->cpi_std[4];
18874606Sesaxe 
18884606Sesaxe 			/*
18894606Sesaxe 			 * Allocate storage to hold the additional regs
18904606Sesaxe 			 * for function 4, %ecx == 1 .. cpi_std_4_size.
18914606Sesaxe 			 *
18924606Sesaxe 			 * The regs for fn 4, %ecx == 0 has already
18934606Sesaxe 			 * been allocated as indicated above.
18944606Sesaxe 			 */
18954606Sesaxe 			for (i = 1; i < size; i++) {
18964606Sesaxe 				cp = cpi->cpi_std_4[i] =
18974606Sesaxe 				    kmem_zalloc(sizeof (regs), KM_SLEEP);
18984606Sesaxe 				cp->cp_eax = 4;
18994606Sesaxe 				cp->cp_ecx = i;
19004606Sesaxe 
19014606Sesaxe 				(void) __cpuid_insn(cp);
19024606Sesaxe 			}
19034606Sesaxe 		}
19044606Sesaxe 		/*
19054606Sesaxe 		 * Determine the number of bits needed to represent
19064606Sesaxe 		 * the number of CPUs sharing the last level cache.
19074606Sesaxe 		 *
19084606Sesaxe 		 * Shift off that number of bits from the APIC id to
19094606Sesaxe 		 * derive the cache id.
19104606Sesaxe 		 */
19114606Sesaxe 		shft = 0;
19124606Sesaxe 		for (i = 1; i < cpi->cpi_ncpu_shr_last_cache; i <<= 1)
19134606Sesaxe 			shft++;
19147282Smishra 		cpi->cpi_last_lvl_cacheid = cpi->cpi_apicid >> shft;
19150Sstevel@tonic-gate 	}
19160Sstevel@tonic-gate 
19170Sstevel@tonic-gate 	/*
19184606Sesaxe 	 * Now fixup the brand string
19190Sstevel@tonic-gate 	 */
19204606Sesaxe 	if ((cpi->cpi_xmaxeax & 0x80000000) == 0) {
19214606Sesaxe 		fabricate_brandstr(cpi);
19224606Sesaxe 	} else {
19230Sstevel@tonic-gate 
19240Sstevel@tonic-gate 		/*
19254606Sesaxe 		 * If we successfully extracted a brand string from the cpuid
19264606Sesaxe 		 * instruction, clean it up by removing leading spaces and
19274606Sesaxe 		 * similar junk.
19280Sstevel@tonic-gate 		 */
19294606Sesaxe 		if (cpi->cpi_brandstr[0]) {
19304606Sesaxe 			size_t maxlen = sizeof (cpi->cpi_brandstr);
19314606Sesaxe 			char *src, *dst;
19324606Sesaxe 
19334606Sesaxe 			dst = src = (char *)cpi->cpi_brandstr;
19344606Sesaxe 			src[maxlen - 1] = '\0';
19354606Sesaxe 			/*
19364606Sesaxe 			 * strip leading spaces
19374606Sesaxe 			 */
19384606Sesaxe 			while (*src == ' ')
19394606Sesaxe 				src++;
19404606Sesaxe 			/*
19414606Sesaxe 			 * Remove any 'Genuine' or "Authentic" prefixes
19424606Sesaxe 			 */
19434606Sesaxe 			if (strncmp(src, "Genuine ", 8) == 0)
19444606Sesaxe 				src += 8;
19454606Sesaxe 			if (strncmp(src, "Authentic ", 10) == 0)
19464606Sesaxe 				src += 10;
19474606Sesaxe 
19484606Sesaxe 			/*
19494606Sesaxe 			 * Now do an in-place copy.
19504606Sesaxe 			 * Map (R) to (r) and (TM) to (tm).
19514606Sesaxe 			 * The era of teletypes is long gone, and there's
19524606Sesaxe 			 * -really- no need to shout.
19534606Sesaxe 			 */
19544606Sesaxe 			while (*src != '\0') {
19554606Sesaxe 				if (src[0] == '(') {
19564606Sesaxe 					if (strncmp(src + 1, "R)", 2) == 0) {
19574606Sesaxe 						(void) strncpy(dst, "(r)", 3);
19584606Sesaxe 						src += 3;
19594606Sesaxe 						dst += 3;
19604606Sesaxe 						continue;
19614606Sesaxe 					}
19624606Sesaxe 					if (strncmp(src + 1, "TM)", 3) == 0) {
19634606Sesaxe 						(void) strncpy(dst, "(tm)", 4);
19644606Sesaxe 						src += 4;
19654606Sesaxe 						dst += 4;
19664606Sesaxe 						continue;
19674606Sesaxe 					}
19680Sstevel@tonic-gate 				}
19694606Sesaxe 				*dst++ = *src++;
19700Sstevel@tonic-gate 			}
19714606Sesaxe 			*dst = '\0';
19724606Sesaxe 
19734606Sesaxe 			/*
19744606Sesaxe 			 * Finally, remove any trailing spaces
19754606Sesaxe 			 */
19764606Sesaxe 			while (--dst > cpi->cpi_brandstr)
19774606Sesaxe 				if (*dst == ' ')
19784606Sesaxe 					*dst = '\0';
19794606Sesaxe 				else
19804606Sesaxe 					break;
19814606Sesaxe 		} else
19824606Sesaxe 			fabricate_brandstr(cpi);
19834606Sesaxe 	}
19840Sstevel@tonic-gate 	cpi->cpi_pass = 3;
19850Sstevel@tonic-gate }
19860Sstevel@tonic-gate 
19870Sstevel@tonic-gate /*
19880Sstevel@tonic-gate  * This routine is called out of bind_hwcap() much later in the life
19890Sstevel@tonic-gate  * of the kernel (post_startup()).  The job of this routine is to resolve
19900Sstevel@tonic-gate  * the hardware feature support and kernel support for those features into
19910Sstevel@tonic-gate  * what we're actually going to tell applications via the aux vector.
19920Sstevel@tonic-gate  */
19930Sstevel@tonic-gate uint_t
19940Sstevel@tonic-gate cpuid_pass4(cpu_t *cpu)
19950Sstevel@tonic-gate {
19960Sstevel@tonic-gate 	struct cpuid_info *cpi;
19970Sstevel@tonic-gate 	uint_t hwcap_flags = 0;
19980Sstevel@tonic-gate 
19990Sstevel@tonic-gate 	if (cpu == NULL)
20000Sstevel@tonic-gate 		cpu = CPU;
20010Sstevel@tonic-gate 	cpi = cpu->cpu_m.mcpu_cpi;
20020Sstevel@tonic-gate 
20030Sstevel@tonic-gate 	ASSERT(cpi->cpi_pass == 3);
20040Sstevel@tonic-gate 
20050Sstevel@tonic-gate 	if (cpi->cpi_maxeax >= 1) {
20060Sstevel@tonic-gate 		uint32_t *edx = &cpi->cpi_support[STD_EDX_FEATURES];
20070Sstevel@tonic-gate 		uint32_t *ecx = &cpi->cpi_support[STD_ECX_FEATURES];
20080Sstevel@tonic-gate 
20090Sstevel@tonic-gate 		*edx = CPI_FEATURES_EDX(cpi);
20100Sstevel@tonic-gate 		*ecx = CPI_FEATURES_ECX(cpi);
20110Sstevel@tonic-gate 
20120Sstevel@tonic-gate 		/*
20130Sstevel@tonic-gate 		 * [these require explicit kernel support]
20140Sstevel@tonic-gate 		 */
20150Sstevel@tonic-gate 		if ((x86_feature & X86_SEP) == 0)
20160Sstevel@tonic-gate 			*edx &= ~CPUID_INTC_EDX_SEP;
20170Sstevel@tonic-gate 
20180Sstevel@tonic-gate 		if ((x86_feature & X86_SSE) == 0)
20190Sstevel@tonic-gate 			*edx &= ~(CPUID_INTC_EDX_FXSR|CPUID_INTC_EDX_SSE);
20200Sstevel@tonic-gate 		if ((x86_feature & X86_SSE2) == 0)
20210Sstevel@tonic-gate 			*edx &= ~CPUID_INTC_EDX_SSE2;
20220Sstevel@tonic-gate 
20230Sstevel@tonic-gate 		if ((x86_feature & X86_HTT) == 0)
20240Sstevel@tonic-gate 			*edx &= ~CPUID_INTC_EDX_HTT;
20250Sstevel@tonic-gate 
20260Sstevel@tonic-gate 		if ((x86_feature & X86_SSE3) == 0)
20270Sstevel@tonic-gate 			*ecx &= ~CPUID_INTC_ECX_SSE3;
20280Sstevel@tonic-gate 
20295269Skk208521 		if (cpi->cpi_vendor == X86_VENDOR_Intel) {
20305269Skk208521 			if ((x86_feature & X86_SSSE3) == 0)
20315269Skk208521 				*ecx &= ~CPUID_INTC_ECX_SSSE3;
20325269Skk208521 			if ((x86_feature & X86_SSE4_1) == 0)
20335269Skk208521 				*ecx &= ~CPUID_INTC_ECX_SSE4_1;
20345269Skk208521 			if ((x86_feature & X86_SSE4_2) == 0)
20355269Skk208521 				*ecx &= ~CPUID_INTC_ECX_SSE4_2;
20365269Skk208521 		}
20375269Skk208521 
20380Sstevel@tonic-gate 		/*
20390Sstevel@tonic-gate 		 * [no explicit support required beyond x87 fp context]
20400Sstevel@tonic-gate 		 */
20410Sstevel@tonic-gate 		if (!fpu_exists)
20420Sstevel@tonic-gate 			*edx &= ~(CPUID_INTC_EDX_FPU | CPUID_INTC_EDX_MMX);
20430Sstevel@tonic-gate 
20440Sstevel@tonic-gate 		/*
20450Sstevel@tonic-gate 		 * Now map the supported feature vector to things that we
20460Sstevel@tonic-gate 		 * think userland will care about.
20470Sstevel@tonic-gate 		 */
20480Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_SEP)
20490Sstevel@tonic-gate 			hwcap_flags |= AV_386_SEP;
20500Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_SSE)
20510Sstevel@tonic-gate 			hwcap_flags |= AV_386_FXSR | AV_386_SSE;
20520Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_SSE2)
20530Sstevel@tonic-gate 			hwcap_flags |= AV_386_SSE2;
20540Sstevel@tonic-gate 		if (*ecx & CPUID_INTC_ECX_SSE3)
20550Sstevel@tonic-gate 			hwcap_flags |= AV_386_SSE3;
20565269Skk208521 		if (cpi->cpi_vendor == X86_VENDOR_Intel) {
20575269Skk208521 			if (*ecx & CPUID_INTC_ECX_SSSE3)
20585269Skk208521 				hwcap_flags |= AV_386_SSSE3;
20595269Skk208521 			if (*ecx & CPUID_INTC_ECX_SSE4_1)
20605269Skk208521 				hwcap_flags |= AV_386_SSE4_1;
20615269Skk208521 			if (*ecx & CPUID_INTC_ECX_SSE4_2)
20625269Skk208521 				hwcap_flags |= AV_386_SSE4_2;
20635269Skk208521 		}
20644628Skk208521 		if (*ecx & CPUID_INTC_ECX_POPCNT)
20654628Skk208521 			hwcap_flags |= AV_386_POPCNT;
20660Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_FPU)
20670Sstevel@tonic-gate 			hwcap_flags |= AV_386_FPU;
20680Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_MMX)
20690Sstevel@tonic-gate 			hwcap_flags |= AV_386_MMX;
20700Sstevel@tonic-gate 
20710Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_TSC)
20720Sstevel@tonic-gate 			hwcap_flags |= AV_386_TSC;
20730Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_CX8)
20740Sstevel@tonic-gate 			hwcap_flags |= AV_386_CX8;
20750Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_CMOV)
20760Sstevel@tonic-gate 			hwcap_flags |= AV_386_CMOV;
20770Sstevel@tonic-gate 		if (*ecx & CPUID_INTC_ECX_MON)
20780Sstevel@tonic-gate 			hwcap_flags |= AV_386_MON;
20790Sstevel@tonic-gate 		if (*ecx & CPUID_INTC_ECX_CX16)
20800Sstevel@tonic-gate 			hwcap_flags |= AV_386_CX16;
20810Sstevel@tonic-gate 	}
20820Sstevel@tonic-gate 
20831228Sandrei 	if (x86_feature & X86_HTT)
20840Sstevel@tonic-gate 		hwcap_flags |= AV_386_PAUSE;
20850Sstevel@tonic-gate 
20860Sstevel@tonic-gate 	if (cpi->cpi_xmaxeax < 0x80000001)
20870Sstevel@tonic-gate 		goto pass4_done;
20880Sstevel@tonic-gate 
20890Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
20901228Sandrei 		struct cpuid_regs cp;
20913446Smrj 		uint32_t *edx, *ecx;
20920Sstevel@tonic-gate 
20933446Smrj 	case X86_VENDOR_Intel:
20943446Smrj 		/*
20953446Smrj 		 * Seems like Intel duplicated what we necessary
20963446Smrj 		 * here to make the initial crop of 64-bit OS's work.
20973446Smrj 		 * Hopefully, those are the only "extended" bits
20983446Smrj 		 * they'll add.
20993446Smrj 		 */
21003446Smrj 		/*FALLTHROUGH*/
21013446Smrj 
21020Sstevel@tonic-gate 	case X86_VENDOR_AMD:
21030Sstevel@tonic-gate 		edx = &cpi->cpi_support[AMD_EDX_FEATURES];
21043446Smrj 		ecx = &cpi->cpi_support[AMD_ECX_FEATURES];
21050Sstevel@tonic-gate 
21060Sstevel@tonic-gate 		*edx = CPI_FEATURES_XTD_EDX(cpi);
21073446Smrj 		*ecx = CPI_FEATURES_XTD_ECX(cpi);
21083446Smrj 
21093446Smrj 		/*
21103446Smrj 		 * [these features require explicit kernel support]
21113446Smrj 		 */
21123446Smrj 		switch (cpi->cpi_vendor) {
21133446Smrj 		case X86_VENDOR_Intel:
21146657Ssudheer 			if ((x86_feature & X86_TSCP) == 0)
21156657Ssudheer 				*edx &= ~CPUID_AMD_EDX_TSCP;
21163446Smrj 			break;
21173446Smrj 
21183446Smrj 		case X86_VENDOR_AMD:
21193446Smrj 			if ((x86_feature & X86_TSCP) == 0)
21203446Smrj 				*edx &= ~CPUID_AMD_EDX_TSCP;
21214628Skk208521 			if ((x86_feature & X86_SSE4A) == 0)
21224628Skk208521 				*ecx &= ~CPUID_AMD_ECX_SSE4A;
21233446Smrj 			break;
21243446Smrj 
21253446Smrj 		default:
21263446Smrj 			break;
21273446Smrj 		}
21280Sstevel@tonic-gate 
21290Sstevel@tonic-gate 		/*
21300Sstevel@tonic-gate 		 * [no explicit support required beyond
21310Sstevel@tonic-gate 		 * x87 fp context and exception handlers]
21320Sstevel@tonic-gate 		 */
21330Sstevel@tonic-gate 		if (!fpu_exists)
21340Sstevel@tonic-gate 			*edx &= ~(CPUID_AMD_EDX_MMXamd |
21350Sstevel@tonic-gate 			    CPUID_AMD_EDX_3DNow | CPUID_AMD_EDX_3DNowx);
21360Sstevel@tonic-gate 
21370Sstevel@tonic-gate 		if ((x86_feature & X86_NX) == 0)
21380Sstevel@tonic-gate 			*edx &= ~CPUID_AMD_EDX_NX;
21393446Smrj #if !defined(__amd64)
21400Sstevel@tonic-gate 		*edx &= ~CPUID_AMD_EDX_LM;
21410Sstevel@tonic-gate #endif
21420Sstevel@tonic-gate 		/*
21430Sstevel@tonic-gate 		 * Now map the supported feature vector to
21440Sstevel@tonic-gate 		 * things that we think userland will care about.
21450Sstevel@tonic-gate 		 */
21463446Smrj #if defined(__amd64)
21470Sstevel@tonic-gate 		if (*edx & CPUID_AMD_EDX_SYSC)
21480Sstevel@tonic-gate 			hwcap_flags |= AV_386_AMD_SYSC;
21493446Smrj #endif
21500Sstevel@tonic-gate 		if (*edx & CPUID_AMD_EDX_MMXamd)
21510Sstevel@tonic-gate 			hwcap_flags |= AV_386_AMD_MMX;
21520Sstevel@tonic-gate 		if (*edx & CPUID_AMD_EDX_3DNow)
21530Sstevel@tonic-gate 			hwcap_flags |= AV_386_AMD_3DNow;
21540Sstevel@tonic-gate 		if (*edx & CPUID_AMD_EDX_3DNowx)
21550Sstevel@tonic-gate 			hwcap_flags |= AV_386_AMD_3DNowx;
21563446Smrj 
21573446Smrj 		switch (cpi->cpi_vendor) {
21583446Smrj 		case X86_VENDOR_AMD:
21593446Smrj 			if (*edx & CPUID_AMD_EDX_TSCP)
21603446Smrj 				hwcap_flags |= AV_386_TSCP;
21613446Smrj 			if (*ecx & CPUID_AMD_ECX_AHF64)
21623446Smrj 				hwcap_flags |= AV_386_AHF;
21634628Skk208521 			if (*ecx & CPUID_AMD_ECX_SSE4A)
21644628Skk208521 				hwcap_flags |= AV_386_AMD_SSE4A;
21654628Skk208521 			if (*ecx & CPUID_AMD_ECX_LZCNT)
21664628Skk208521 				hwcap_flags |= AV_386_AMD_LZCNT;
21673446Smrj 			break;
21683446Smrj 
21693446Smrj 		case X86_VENDOR_Intel:
21706657Ssudheer 			if (*edx & CPUID_AMD_EDX_TSCP)
21716657Ssudheer 				hwcap_flags |= AV_386_TSCP;
21723446Smrj 			/*
21733446Smrj 			 * Aarrgh.
21743446Smrj 			 * Intel uses a different bit in the same word.
21753446Smrj 			 */
21763446Smrj 			if (*ecx & CPUID_INTC_ECX_AHF64)
21773446Smrj 				hwcap_flags |= AV_386_AHF;
21783446Smrj 			break;
21793446Smrj 
21803446Smrj 		default:
21813446Smrj 			break;
21823446Smrj 		}
21830Sstevel@tonic-gate 		break;
21840Sstevel@tonic-gate 
21850Sstevel@tonic-gate 	case X86_VENDOR_TM:
21861228Sandrei 		cp.cp_eax = 0x80860001;
21871228Sandrei 		(void) __cpuid_insn(&cp);
21881228Sandrei 		cpi->cpi_support[TM_EDX_FEATURES] = cp.cp_edx;
21890Sstevel@tonic-gate 		break;
21900Sstevel@tonic-gate 
21910Sstevel@tonic-gate 	default:
21920Sstevel@tonic-gate 		break;
21930Sstevel@tonic-gate 	}
21940Sstevel@tonic-gate 
21950Sstevel@tonic-gate pass4_done:
21960Sstevel@tonic-gate 	cpi->cpi_pass = 4;
21970Sstevel@tonic-gate 	return (hwcap_flags);
21980Sstevel@tonic-gate }
21990Sstevel@tonic-gate 
22000Sstevel@tonic-gate 
22010Sstevel@tonic-gate /*
22020Sstevel@tonic-gate  * Simulate the cpuid instruction using the data we previously
22030Sstevel@tonic-gate  * captured about this CPU.  We try our best to return the truth
22040Sstevel@tonic-gate  * about the hardware, independently of kernel support.
22050Sstevel@tonic-gate  */
22060Sstevel@tonic-gate uint32_t
22071228Sandrei cpuid_insn(cpu_t *cpu, struct cpuid_regs *cp)
22080Sstevel@tonic-gate {
22090Sstevel@tonic-gate 	struct cpuid_info *cpi;
22101228Sandrei 	struct cpuid_regs *xcp;
22110Sstevel@tonic-gate 
22120Sstevel@tonic-gate 	if (cpu == NULL)
22130Sstevel@tonic-gate 		cpu = CPU;
22140Sstevel@tonic-gate 	cpi = cpu->cpu_m.mcpu_cpi;
22150Sstevel@tonic-gate 
22160Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 3));
22170Sstevel@tonic-gate 
22180Sstevel@tonic-gate 	/*
22190Sstevel@tonic-gate 	 * CPUID data is cached in two separate places: cpi_std for standard
22200Sstevel@tonic-gate 	 * CPUID functions, and cpi_extd for extended CPUID functions.
22210Sstevel@tonic-gate 	 */
22221228Sandrei 	if (cp->cp_eax <= cpi->cpi_maxeax && cp->cp_eax < NMAX_CPI_STD)
22231228Sandrei 		xcp = &cpi->cpi_std[cp->cp_eax];
22241228Sandrei 	else if (cp->cp_eax >= 0x80000000 && cp->cp_eax <= cpi->cpi_xmaxeax &&
22251228Sandrei 	    cp->cp_eax < 0x80000000 + NMAX_CPI_EXTD)
22261228Sandrei 		xcp = &cpi->cpi_extd[cp->cp_eax - 0x80000000];
22270Sstevel@tonic-gate 	else
22280Sstevel@tonic-gate 		/*
22290Sstevel@tonic-gate 		 * The caller is asking for data from an input parameter which
22300Sstevel@tonic-gate 		 * the kernel has not cached.  In this case we go fetch from
22310Sstevel@tonic-gate 		 * the hardware and return the data directly to the user.
22320Sstevel@tonic-gate 		 */
22331228Sandrei 		return (__cpuid_insn(cp));
22341228Sandrei 
22351228Sandrei 	cp->cp_eax = xcp->cp_eax;
22361228Sandrei 	cp->cp_ebx = xcp->cp_ebx;
22371228Sandrei 	cp->cp_ecx = xcp->cp_ecx;
22381228Sandrei 	cp->cp_edx = xcp->cp_edx;
22390Sstevel@tonic-gate 	return (cp->cp_eax);
22400Sstevel@tonic-gate }
22410Sstevel@tonic-gate 
22420Sstevel@tonic-gate int
22430Sstevel@tonic-gate cpuid_checkpass(cpu_t *cpu, int pass)
22440Sstevel@tonic-gate {
22450Sstevel@tonic-gate 	return (cpu != NULL && cpu->cpu_m.mcpu_cpi != NULL &&
22460Sstevel@tonic-gate 	    cpu->cpu_m.mcpu_cpi->cpi_pass >= pass);
22470Sstevel@tonic-gate }
22480Sstevel@tonic-gate 
22490Sstevel@tonic-gate int
22500Sstevel@tonic-gate cpuid_getbrandstr(cpu_t *cpu, char *s, size_t n)
22510Sstevel@tonic-gate {
22520Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 3));
22530Sstevel@tonic-gate 
22540Sstevel@tonic-gate 	return (snprintf(s, n, "%s", cpu->cpu_m.mcpu_cpi->cpi_brandstr));
22550Sstevel@tonic-gate }
22560Sstevel@tonic-gate 
22570Sstevel@tonic-gate int
22581228Sandrei cpuid_is_cmt(cpu_t *cpu)
22590Sstevel@tonic-gate {
22600Sstevel@tonic-gate 	if (cpu == NULL)
22610Sstevel@tonic-gate 		cpu = CPU;
22620Sstevel@tonic-gate 
22630Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
22640Sstevel@tonic-gate 
22650Sstevel@tonic-gate 	return (cpu->cpu_m.mcpu_cpi->cpi_chipid >= 0);
22660Sstevel@tonic-gate }
22670Sstevel@tonic-gate 
22680Sstevel@tonic-gate /*
22690Sstevel@tonic-gate  * AMD and Intel both implement the 64-bit variant of the syscall
22700Sstevel@tonic-gate  * instruction (syscallq), so if there's -any- support for syscall,
22710Sstevel@tonic-gate  * cpuid currently says "yes, we support this".
22720Sstevel@tonic-gate  *
22730Sstevel@tonic-gate  * However, Intel decided to -not- implement the 32-bit variant of the
22740Sstevel@tonic-gate  * syscall instruction, so we provide a predicate to allow our caller
22750Sstevel@tonic-gate  * to test that subtlety here.
22765084Sjohnlev  *
22775084Sjohnlev  * XXPV	Currently, 32-bit syscall instructions don't work via the hypervisor,
22785084Sjohnlev  *	even in the case where the hardware would in fact support it.
22790Sstevel@tonic-gate  */
22800Sstevel@tonic-gate /*ARGSUSED*/
22810Sstevel@tonic-gate int
22820Sstevel@tonic-gate cpuid_syscall32_insn(cpu_t *cpu)
22830Sstevel@tonic-gate {
22840Sstevel@tonic-gate 	ASSERT(cpuid_checkpass((cpu == NULL ? CPU : cpu), 1));
22850Sstevel@tonic-gate 
22865084Sjohnlev #if !defined(__xpv)
22873446Smrj 	if (cpu == NULL)
22883446Smrj 		cpu = CPU;
22893446Smrj 
22903446Smrj 	/*CSTYLED*/
22913446Smrj 	{
22923446Smrj 		struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
22933446Smrj 
22943446Smrj 		if (cpi->cpi_vendor == X86_VENDOR_AMD &&
22953446Smrj 		    cpi->cpi_xmaxeax >= 0x80000001 &&
22963446Smrj 		    (CPI_FEATURES_XTD_EDX(cpi) & CPUID_AMD_EDX_SYSC))
22973446Smrj 			return (1);
22983446Smrj 	}
22995084Sjohnlev #endif
23000Sstevel@tonic-gate 	return (0);
23010Sstevel@tonic-gate }
23020Sstevel@tonic-gate 
23030Sstevel@tonic-gate int
23040Sstevel@tonic-gate cpuid_getidstr(cpu_t *cpu, char *s, size_t n)
23050Sstevel@tonic-gate {
23060Sstevel@tonic-gate 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
23070Sstevel@tonic-gate 
23080Sstevel@tonic-gate 	static const char fmt[] =
23093779Sdmick 	    "x86 (%s %X family %d model %d step %d clock %d MHz)";
23100Sstevel@tonic-gate 	static const char fmt_ht[] =
23113779Sdmick 	    "x86 (chipid 0x%x %s %X family %d model %d step %d clock %d MHz)";
23120Sstevel@tonic-gate 
23130Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
23140Sstevel@tonic-gate 
23151228Sandrei 	if (cpuid_is_cmt(cpu))
23160Sstevel@tonic-gate 		return (snprintf(s, n, fmt_ht, cpi->cpi_chipid,
23173779Sdmick 		    cpi->cpi_vendorstr, cpi->cpi_std[1].cp_eax,
23183779Sdmick 		    cpi->cpi_family, cpi->cpi_model,
23190Sstevel@tonic-gate 		    cpi->cpi_step, cpu->cpu_type_info.pi_clock));
23200Sstevel@tonic-gate 	return (snprintf(s, n, fmt,
23213779Sdmick 	    cpi->cpi_vendorstr, cpi->cpi_std[1].cp_eax,
23223779Sdmick 	    cpi->cpi_family, cpi->cpi_model,
23230Sstevel@tonic-gate 	    cpi->cpi_step, cpu->cpu_type_info.pi_clock));
23240Sstevel@tonic-gate }
23250Sstevel@tonic-gate 
23260Sstevel@tonic-gate const char *
23270Sstevel@tonic-gate cpuid_getvendorstr(cpu_t *cpu)
23280Sstevel@tonic-gate {
23290Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
23300Sstevel@tonic-gate 	return ((const char *)cpu->cpu_m.mcpu_cpi->cpi_vendorstr);
23310Sstevel@tonic-gate }
23320Sstevel@tonic-gate 
23330Sstevel@tonic-gate uint_t
23340Sstevel@tonic-gate cpuid_getvendor(cpu_t *cpu)
23350Sstevel@tonic-gate {
23360Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
23370Sstevel@tonic-gate 	return (cpu->cpu_m.mcpu_cpi->cpi_vendor);
23380Sstevel@tonic-gate }
23390Sstevel@tonic-gate 
23400Sstevel@tonic-gate uint_t
23410Sstevel@tonic-gate cpuid_getfamily(cpu_t *cpu)
23420Sstevel@tonic-gate {
23430Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
23440Sstevel@tonic-gate 	return (cpu->cpu_m.mcpu_cpi->cpi_family);
23450Sstevel@tonic-gate }
23460Sstevel@tonic-gate 
23470Sstevel@tonic-gate uint_t
23480Sstevel@tonic-gate cpuid_getmodel(cpu_t *cpu)
23490Sstevel@tonic-gate {
23500Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
23510Sstevel@tonic-gate 	return (cpu->cpu_m.mcpu_cpi->cpi_model);
23520Sstevel@tonic-gate }
23530Sstevel@tonic-gate 
23540Sstevel@tonic-gate uint_t
23550Sstevel@tonic-gate cpuid_get_ncpu_per_chip(cpu_t *cpu)
23560Sstevel@tonic-gate {
23570Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
23580Sstevel@tonic-gate 	return (cpu->cpu_m.mcpu_cpi->cpi_ncpu_per_chip);
23590Sstevel@tonic-gate }
23600Sstevel@tonic-gate 
23610Sstevel@tonic-gate uint_t
23621228Sandrei cpuid_get_ncore_per_chip(cpu_t *cpu)
23631228Sandrei {
23641228Sandrei 	ASSERT(cpuid_checkpass(cpu, 1));
23651228Sandrei 	return (cpu->cpu_m.mcpu_cpi->cpi_ncore_per_chip);
23661228Sandrei }
23671228Sandrei 
23681228Sandrei uint_t
23694606Sesaxe cpuid_get_ncpu_sharing_last_cache(cpu_t *cpu)
23704606Sesaxe {
23714606Sesaxe 	ASSERT(cpuid_checkpass(cpu, 2));
23724606Sesaxe 	return (cpu->cpu_m.mcpu_cpi->cpi_ncpu_shr_last_cache);
23734606Sesaxe }
23744606Sesaxe 
23754606Sesaxe id_t
23764606Sesaxe cpuid_get_last_lvl_cacheid(cpu_t *cpu)
23774606Sesaxe {
23784606Sesaxe 	ASSERT(cpuid_checkpass(cpu, 2));
23794606Sesaxe 	return (cpu->cpu_m.mcpu_cpi->cpi_last_lvl_cacheid);
23804606Sesaxe }
23814606Sesaxe 
23824606Sesaxe uint_t
23830Sstevel@tonic-gate cpuid_getstep(cpu_t *cpu)
23840Sstevel@tonic-gate {
23850Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
23860Sstevel@tonic-gate 	return (cpu->cpu_m.mcpu_cpi->cpi_step);
23870Sstevel@tonic-gate }
23880Sstevel@tonic-gate 
23894581Ssherrym uint_t
23904581Ssherrym cpuid_getsig(struct cpu *cpu)
23914581Ssherrym {
23924581Ssherrym 	ASSERT(cpuid_checkpass(cpu, 1));
23934581Ssherrym 	return (cpu->cpu_m.mcpu_cpi->cpi_std[1].cp_eax);
23944581Ssherrym }
23954581Ssherrym 
23962869Sgavinm uint32_t
23972869Sgavinm cpuid_getchiprev(struct cpu *cpu)
23982869Sgavinm {
23992869Sgavinm 	ASSERT(cpuid_checkpass(cpu, 1));
24002869Sgavinm 	return (cpu->cpu_m.mcpu_cpi->cpi_chiprev);
24012869Sgavinm }
24022869Sgavinm 
24032869Sgavinm const char *
24042869Sgavinm cpuid_getchiprevstr(struct cpu *cpu)
24052869Sgavinm {
24062869Sgavinm 	ASSERT(cpuid_checkpass(cpu, 1));
24072869Sgavinm 	return (cpu->cpu_m.mcpu_cpi->cpi_chiprevstr);
24082869Sgavinm }
24092869Sgavinm 
24102869Sgavinm uint32_t
24112869Sgavinm cpuid_getsockettype(struct cpu *cpu)
24122869Sgavinm {
24132869Sgavinm 	ASSERT(cpuid_checkpass(cpu, 1));
24142869Sgavinm 	return (cpu->cpu_m.mcpu_cpi->cpi_socket);
24152869Sgavinm }
24162869Sgavinm 
24173434Sesaxe int
24183434Sesaxe cpuid_get_chipid(cpu_t *cpu)
24190Sstevel@tonic-gate {
24200Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
24210Sstevel@tonic-gate 
24221228Sandrei 	if (cpuid_is_cmt(cpu))
24230Sstevel@tonic-gate 		return (cpu->cpu_m.mcpu_cpi->cpi_chipid);
24240Sstevel@tonic-gate 	return (cpu->cpu_id);
24250Sstevel@tonic-gate }
24260Sstevel@tonic-gate 
24271228Sandrei id_t
24283434Sesaxe cpuid_get_coreid(cpu_t *cpu)
24291228Sandrei {
24301228Sandrei 	ASSERT(cpuid_checkpass(cpu, 1));
24311228Sandrei 	return (cpu->cpu_m.mcpu_cpi->cpi_coreid);
24321228Sandrei }
24331228Sandrei 
24340Sstevel@tonic-gate int
24355870Sgavinm cpuid_get_pkgcoreid(cpu_t *cpu)
24365870Sgavinm {
24375870Sgavinm 	ASSERT(cpuid_checkpass(cpu, 1));
24385870Sgavinm 	return (cpu->cpu_m.mcpu_cpi->cpi_pkgcoreid);
24395870Sgavinm }
24405870Sgavinm 
24415870Sgavinm int
24423434Sesaxe cpuid_get_clogid(cpu_t *cpu)
24430Sstevel@tonic-gate {
24440Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
24450Sstevel@tonic-gate 	return (cpu->cpu_m.mcpu_cpi->cpi_clogid);
24460Sstevel@tonic-gate }
24470Sstevel@tonic-gate 
24480Sstevel@tonic-gate void
24490Sstevel@tonic-gate cpuid_get_addrsize(cpu_t *cpu, uint_t *pabits, uint_t *vabits)
24500Sstevel@tonic-gate {
24510Sstevel@tonic-gate 	struct cpuid_info *cpi;
24520Sstevel@tonic-gate 
24530Sstevel@tonic-gate 	if (cpu == NULL)
24540Sstevel@tonic-gate 		cpu = CPU;
24550Sstevel@tonic-gate 	cpi = cpu->cpu_m.mcpu_cpi;
24560Sstevel@tonic-gate 
24570Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
24580Sstevel@tonic-gate 
24590Sstevel@tonic-gate 	if (pabits)
24600Sstevel@tonic-gate 		*pabits = cpi->cpi_pabits;
24610Sstevel@tonic-gate 	if (vabits)
24620Sstevel@tonic-gate 		*vabits = cpi->cpi_vabits;
24630Sstevel@tonic-gate }
24640Sstevel@tonic-gate 
24650Sstevel@tonic-gate /*
24660Sstevel@tonic-gate  * Returns the number of data TLB entries for a corresponding
24670Sstevel@tonic-gate  * pagesize.  If it can't be computed, or isn't known, the
24680Sstevel@tonic-gate  * routine returns zero.  If you ask about an architecturally
24690Sstevel@tonic-gate  * impossible pagesize, the routine will panic (so that the
24700Sstevel@tonic-gate  * hat implementor knows that things are inconsistent.)
24710Sstevel@tonic-gate  */
24720Sstevel@tonic-gate uint_t
24730Sstevel@tonic-gate cpuid_get_dtlb_nent(cpu_t *cpu, size_t pagesize)
24740Sstevel@tonic-gate {
24750Sstevel@tonic-gate 	struct cpuid_info *cpi;
24760Sstevel@tonic-gate 	uint_t dtlb_nent = 0;
24770Sstevel@tonic-gate 
24780Sstevel@tonic-gate 	if (cpu == NULL)
24790Sstevel@tonic-gate 		cpu = CPU;
24800Sstevel@tonic-gate 	cpi = cpu->cpu_m.mcpu_cpi;
24810Sstevel@tonic-gate 
24820Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
24830Sstevel@tonic-gate 
24840Sstevel@tonic-gate 	/*
24850Sstevel@tonic-gate 	 * Check the L2 TLB info
24860Sstevel@tonic-gate 	 */
24870Sstevel@tonic-gate 	if (cpi->cpi_xmaxeax >= 0x80000006) {
24881228Sandrei 		struct cpuid_regs *cp = &cpi->cpi_extd[6];
24890Sstevel@tonic-gate 
24900Sstevel@tonic-gate 		switch (pagesize) {
24910Sstevel@tonic-gate 
24920Sstevel@tonic-gate 		case 4 * 1024:
24930Sstevel@tonic-gate 			/*
24940Sstevel@tonic-gate 			 * All zero in the top 16 bits of the register
24950Sstevel@tonic-gate 			 * indicates a unified TLB. Size is in low 16 bits.
24960Sstevel@tonic-gate 			 */
24970Sstevel@tonic-gate 			if ((cp->cp_ebx & 0xffff0000) == 0)
24980Sstevel@tonic-gate 				dtlb_nent = cp->cp_ebx & 0x0000ffff;
24990Sstevel@tonic-gate 			else
25000Sstevel@tonic-gate 				dtlb_nent = BITX(cp->cp_ebx, 27, 16);
25010Sstevel@tonic-gate 			break;
25020Sstevel@tonic-gate 
25030Sstevel@tonic-gate 		case 2 * 1024 * 1024:
25040Sstevel@tonic-gate 			if ((cp->cp_eax & 0xffff0000) == 0)
25050Sstevel@tonic-gate 				dtlb_nent = cp->cp_eax & 0x0000ffff;
25060Sstevel@tonic-gate 			else
25070Sstevel@tonic-gate 				dtlb_nent = BITX(cp->cp_eax, 27, 16);
25080Sstevel@tonic-gate 			break;
25090Sstevel@tonic-gate 
25100Sstevel@tonic-gate 		default:
25110Sstevel@tonic-gate 			panic("unknown L2 pagesize");
25120Sstevel@tonic-gate 			/*NOTREACHED*/
25130Sstevel@tonic-gate 		}
25140Sstevel@tonic-gate 	}
25150Sstevel@tonic-gate 
25160Sstevel@tonic-gate 	if (dtlb_nent != 0)
25170Sstevel@tonic-gate 		return (dtlb_nent);
25180Sstevel@tonic-gate 
25190Sstevel@tonic-gate 	/*
25200Sstevel@tonic-gate 	 * No L2 TLB support for this size, try L1.
25210Sstevel@tonic-gate 	 */
25220Sstevel@tonic-gate 	if (cpi->cpi_xmaxeax >= 0x80000005) {
25231228Sandrei 		struct cpuid_regs *cp = &cpi->cpi_extd[5];
25240Sstevel@tonic-gate 
25250Sstevel@tonic-gate 		switch (pagesize) {
25260Sstevel@tonic-gate 		case 4 * 1024:
25270Sstevel@tonic-gate 			dtlb_nent = BITX(cp->cp_ebx, 23, 16);
25280Sstevel@tonic-gate 			break;
25290Sstevel@tonic-gate 		case 2 * 1024 * 1024:
25300Sstevel@tonic-gate 			dtlb_nent = BITX(cp->cp_eax, 23, 16);
25310Sstevel@tonic-gate 			break;
25320Sstevel@tonic-gate 		default:
25330Sstevel@tonic-gate 			panic("unknown L1 d-TLB pagesize");
25340Sstevel@tonic-gate 			/*NOTREACHED*/
25350Sstevel@tonic-gate 		}
25360Sstevel@tonic-gate 	}
25370Sstevel@tonic-gate 
25380Sstevel@tonic-gate 	return (dtlb_nent);
25390Sstevel@tonic-gate }
25400Sstevel@tonic-gate 
25410Sstevel@tonic-gate /*
25420Sstevel@tonic-gate  * Return 0 if the erratum is not present or not applicable, positive
25430Sstevel@tonic-gate  * if it is, and negative if the status of the erratum is unknown.
25440Sstevel@tonic-gate  *
25450Sstevel@tonic-gate  * See "Revision Guide for AMD Athlon(tm) 64 and AMD Opteron(tm)
2546359Skucharsk  * Processors" #25759, Rev 3.57, August 2005
25470Sstevel@tonic-gate  */
25480Sstevel@tonic-gate int
25490Sstevel@tonic-gate cpuid_opteron_erratum(cpu_t *cpu, uint_t erratum)
25500Sstevel@tonic-gate {
25510Sstevel@tonic-gate 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
25521228Sandrei 	uint_t eax;
25530Sstevel@tonic-gate 
25542584Ssethg 	/*
25552584Ssethg 	 * Bail out if this CPU isn't an AMD CPU, or if it's
25562584Ssethg 	 * a legacy (32-bit) AMD CPU.
25572584Ssethg 	 */
25582584Ssethg 	if (cpi->cpi_vendor != X86_VENDOR_AMD ||
25594265Skchow 	    cpi->cpi_family == 4 || cpi->cpi_family == 5 ||
25604265Skchow 	    cpi->cpi_family == 6)
25612869Sgavinm 
25620Sstevel@tonic-gate 		return (0);
25630Sstevel@tonic-gate 
25640Sstevel@tonic-gate 	eax = cpi->cpi_std[1].cp_eax;
25650Sstevel@tonic-gate 
25660Sstevel@tonic-gate #define	SH_B0(eax)	(eax == 0xf40 || eax == 0xf50)
25670Sstevel@tonic-gate #define	SH_B3(eax) 	(eax == 0xf51)
25681582Skchow #define	B(eax)		(SH_B0(eax) || SH_B3(eax))
25690Sstevel@tonic-gate 
25700Sstevel@tonic-gate #define	SH_C0(eax)	(eax == 0xf48 || eax == 0xf58)
25710Sstevel@tonic-gate 
25720Sstevel@tonic-gate #define	SH_CG(eax)	(eax == 0xf4a || eax == 0xf5a || eax == 0xf7a)
25730Sstevel@tonic-gate #define	DH_CG(eax)	(eax == 0xfc0 || eax == 0xfe0 || eax == 0xff0)
25740Sstevel@tonic-gate #define	CH_CG(eax)	(eax == 0xf82 || eax == 0xfb2)
25751582Skchow #define	CG(eax)		(SH_CG(eax) || DH_CG(eax) || CH_CG(eax))
25760Sstevel@tonic-gate 
25770Sstevel@tonic-gate #define	SH_D0(eax)	(eax == 0x10f40 || eax == 0x10f50 || eax == 0x10f70)
25780Sstevel@tonic-gate #define	DH_D0(eax)	(eax == 0x10fc0 || eax == 0x10ff0)
25790Sstevel@tonic-gate #define	CH_D0(eax)	(eax == 0x10f80 || eax == 0x10fb0)
25801582Skchow #define	D0(eax)		(SH_D0(eax) || DH_D0(eax) || CH_D0(eax))
25810Sstevel@tonic-gate 
25820Sstevel@tonic-gate #define	SH_E0(eax)	(eax == 0x20f50 || eax == 0x20f40 || eax == 0x20f70)
25830Sstevel@tonic-gate #define	JH_E1(eax)	(eax == 0x20f10)	/* JH8_E0 had 0x20f30 */
25840Sstevel@tonic-gate #define	DH_E3(eax)	(eax == 0x20fc0 || eax == 0x20ff0)
25850Sstevel@tonic-gate #define	SH_E4(eax)	(eax == 0x20f51 || eax == 0x20f71)
25860Sstevel@tonic-gate #define	BH_E4(eax)	(eax == 0x20fb1)
25870Sstevel@tonic-gate #define	SH_E5(eax)	(eax == 0x20f42)
25880Sstevel@tonic-gate #define	DH_E6(eax)	(eax == 0x20ff2 || eax == 0x20fc2)
25890Sstevel@tonic-gate #define	JH_E6(eax)	(eax == 0x20f12 || eax == 0x20f32)
25901582Skchow #define	EX(eax)		(SH_E0(eax) || JH_E1(eax) || DH_E3(eax) || \
25911582Skchow 			    SH_E4(eax) || BH_E4(eax) || SH_E5(eax) || \
25921582Skchow 			    DH_E6(eax) || JH_E6(eax))
25930Sstevel@tonic-gate 
25946691Skchow #define	DR_AX(eax)	(eax == 0x100f00 || eax == 0x100f01 || eax == 0x100f02)
25956691Skchow #define	DR_B0(eax)	(eax == 0x100f20)
25966691Skchow #define	DR_B1(eax)	(eax == 0x100f21)
25976691Skchow #define	DR_BA(eax)	(eax == 0x100f2a)
25986691Skchow #define	DR_B2(eax)	(eax == 0x100f22)
25996691Skchow #define	DR_B3(eax)	(eax == 0x100f23)
26006691Skchow #define	RB_C0(eax)	(eax == 0x100f40)
26016691Skchow 
26020Sstevel@tonic-gate 	switch (erratum) {
26030Sstevel@tonic-gate 	case 1:
26044265Skchow 		return (cpi->cpi_family < 0x10);
26050Sstevel@tonic-gate 	case 51:	/* what does the asterisk mean? */
26060Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax));
26070Sstevel@tonic-gate 	case 52:
26080Sstevel@tonic-gate 		return (B(eax));
26090Sstevel@tonic-gate 	case 57:
26106691Skchow 		return (cpi->cpi_family <= 0x11);
26110Sstevel@tonic-gate 	case 58:
26120Sstevel@tonic-gate 		return (B(eax));
26130Sstevel@tonic-gate 	case 60:
26146691Skchow 		return (cpi->cpi_family <= 0x11);
26150Sstevel@tonic-gate 	case 61:
26160Sstevel@tonic-gate 	case 62:
26170Sstevel@tonic-gate 	case 63:
26180Sstevel@tonic-gate 	case 64:
26190Sstevel@tonic-gate 	case 65:
26200Sstevel@tonic-gate 	case 66:
26210Sstevel@tonic-gate 	case 68:
26220Sstevel@tonic-gate 	case 69:
26230Sstevel@tonic-gate 	case 70:
26240Sstevel@tonic-gate 	case 71:
26250Sstevel@tonic-gate 		return (B(eax));
26260Sstevel@tonic-gate 	case 72:
26270Sstevel@tonic-gate 		return (SH_B0(eax));
26280Sstevel@tonic-gate 	case 74:
26290Sstevel@tonic-gate 		return (B(eax));
26300Sstevel@tonic-gate 	case 75:
26314265Skchow 		return (cpi->cpi_family < 0x10);
26320Sstevel@tonic-gate 	case 76:
26330Sstevel@tonic-gate 		return (B(eax));
26340Sstevel@tonic-gate 	case 77:
26356691Skchow 		return (cpi->cpi_family <= 0x11);
26360Sstevel@tonic-gate 	case 78:
26370Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax));
26380Sstevel@tonic-gate 	case 79:
26390Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax) || EX(eax));
26400Sstevel@tonic-gate 	case 80:
26410Sstevel@tonic-gate 	case 81:
26420Sstevel@tonic-gate 	case 82:
26430Sstevel@tonic-gate 		return (B(eax));
26440Sstevel@tonic-gate 	case 83:
26450Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax));
26460Sstevel@tonic-gate 	case 85:
26474265Skchow 		return (cpi->cpi_family < 0x10);
26480Sstevel@tonic-gate 	case 86:
26490Sstevel@tonic-gate 		return (SH_C0(eax) || CG(eax));
26500Sstevel@tonic-gate 	case 88:
26510Sstevel@tonic-gate #if !defined(__amd64)
26520Sstevel@tonic-gate 		return (0);
26530Sstevel@tonic-gate #else
26540Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax));
26550Sstevel@tonic-gate #endif
26560Sstevel@tonic-gate 	case 89:
26574265Skchow 		return (cpi->cpi_family < 0x10);
26580Sstevel@tonic-gate 	case 90:
26590Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax));
26600Sstevel@tonic-gate 	case 91:
26610Sstevel@tonic-gate 	case 92:
26620Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax));
26630Sstevel@tonic-gate 	case 93:
26640Sstevel@tonic-gate 		return (SH_C0(eax));
26650Sstevel@tonic-gate 	case 94:
26660Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax));
26670Sstevel@tonic-gate 	case 95:
26680Sstevel@tonic-gate #if !defined(__amd64)
26690Sstevel@tonic-gate 		return (0);
26700Sstevel@tonic-gate #else
26710Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax));
26720Sstevel@tonic-gate #endif
26730Sstevel@tonic-gate 	case 96:
26740Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax));
26750Sstevel@tonic-gate 	case 97:
26760Sstevel@tonic-gate 	case 98:
26770Sstevel@tonic-gate 		return (SH_C0(eax) || CG(eax));
26780Sstevel@tonic-gate 	case 99:
26790Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax));
26800Sstevel@tonic-gate 	case 100:
26810Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax));
26820Sstevel@tonic-gate 	case 101:
26830Sstevel@tonic-gate 	case 103:
26840Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax));
26850Sstevel@tonic-gate 	case 104:
26860Sstevel@tonic-gate 		return (SH_C0(eax) || CG(eax) || D0(eax));
26870Sstevel@tonic-gate 	case 105:
26880Sstevel@tonic-gate 	case 106:
26890Sstevel@tonic-gate 	case 107:
26900Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax));
26910Sstevel@tonic-gate 	case 108:
26920Sstevel@tonic-gate 		return (DH_CG(eax));
26930Sstevel@tonic-gate 	case 109:
26940Sstevel@tonic-gate 		return (SH_C0(eax) || CG(eax) || D0(eax));
26950Sstevel@tonic-gate 	case 110:
26960Sstevel@tonic-gate 		return (D0(eax) || EX(eax));
26970Sstevel@tonic-gate 	case 111:
26980Sstevel@tonic-gate 		return (CG(eax));
26990Sstevel@tonic-gate 	case 112:
27000Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax) || EX(eax));
27010Sstevel@tonic-gate 	case 113:
27020Sstevel@tonic-gate 		return (eax == 0x20fc0);
27030Sstevel@tonic-gate 	case 114:
27040Sstevel@tonic-gate 		return (SH_E0(eax) || JH_E1(eax) || DH_E3(eax));
27050Sstevel@tonic-gate 	case 115:
27060Sstevel@tonic-gate 		return (SH_E0(eax) || JH_E1(eax));
27070Sstevel@tonic-gate 	case 116:
27080Sstevel@tonic-gate 		return (SH_E0(eax) || JH_E1(eax) || DH_E3(eax));
27090Sstevel@tonic-gate 	case 117:
27100Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax));
27110Sstevel@tonic-gate 	case 118:
27120Sstevel@tonic-gate 		return (SH_E0(eax) || JH_E1(eax) || SH_E4(eax) || BH_E4(eax) ||
27130Sstevel@tonic-gate 		    JH_E6(eax));
27140Sstevel@tonic-gate 	case 121:
27150Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax) || EX(eax));
27160Sstevel@tonic-gate 	case 122:
27176691Skchow 		return (cpi->cpi_family < 0x10 || cpi->cpi_family == 0x11);
27180Sstevel@tonic-gate 	case 123:
27190Sstevel@tonic-gate 		return (JH_E1(eax) || BH_E4(eax) || JH_E6(eax));
2720359Skucharsk 	case 131:
27214265Skchow 		return (cpi->cpi_family < 0x10);
2722938Sesaxe 	case 6336786:
2723938Sesaxe 		/*
2724938Sesaxe 		 * Test for AdvPowerMgmtInfo.TscPStateInvariant
27254265Skchow 		 * if this is a K8 family or newer processor
2726938Sesaxe 		 */
2727938Sesaxe 		if (CPI_FAMILY(cpi) == 0xf) {
27281228Sandrei 			struct cpuid_regs regs;
27291228Sandrei 			regs.cp_eax = 0x80000007;
27301228Sandrei 			(void) __cpuid_insn(&regs);
27311228Sandrei 			return (!(regs.cp_edx & 0x100));
2732938Sesaxe 		}
2733938Sesaxe 		return (0);
27341582Skchow 	case 6323525:
27351582Skchow 		return (((((eax >> 12) & 0xff00) + (eax & 0xf00)) |
27361582Skchow 		    (((eax >> 4) & 0xf) | ((eax >> 12) & 0xf0))) < 0xf40);
27371582Skchow 
27386691Skchow 	case 6671130:
27396691Skchow 		/*
27406691Skchow 		 * check for processors (pre-Shanghai) that do not provide
27416691Skchow 		 * optimal management of 1gb ptes in its tlb.
27426691Skchow 		 */
27436691Skchow 		return (cpi->cpi_family == 0x10 && cpi->cpi_model < 4);
27446691Skchow 
27456691Skchow 	case 298:
27466691Skchow 		return (DR_AX(eax) || DR_B0(eax) || DR_B1(eax) || DR_BA(eax) ||
27476691Skchow 		    DR_B2(eax) || RB_C0(eax));
27486691Skchow 
27496691Skchow 	default:
27506691Skchow 		return (-1);
27516691Skchow 
27526691Skchow 	}
27536691Skchow }
27546691Skchow 
27556691Skchow /*
27566691Skchow  * Determine if specified erratum is present via OSVW (OS Visible Workaround).
27576691Skchow  * Return 1 if erratum is present, 0 if not present and -1 if indeterminate.
27586691Skchow  */
27596691Skchow int
27606691Skchow osvw_opteron_erratum(cpu_t *cpu, uint_t erratum)
27616691Skchow {
27626691Skchow 	struct cpuid_info	*cpi;
27636691Skchow 	uint_t			osvwid;
27646691Skchow 	static int		osvwfeature = -1;
27656691Skchow 	uint64_t		osvwlength;
27666691Skchow 
27676691Skchow 
27686691Skchow 	cpi = cpu->cpu_m.mcpu_cpi;
27696691Skchow 
27706691Skchow 	/* confirm OSVW supported */
27716691Skchow 	if (osvwfeature == -1) {
27726691Skchow 		osvwfeature = cpi->cpi_extd[1].cp_ecx & CPUID_AMD_ECX_OSVW;
27736691Skchow 	} else {
27746691Skchow 		/* assert that osvw feature setting is consistent on all cpus */
27756691Skchow 		ASSERT(osvwfeature ==
27766691Skchow 		    (cpi->cpi_extd[1].cp_ecx & CPUID_AMD_ECX_OSVW));
27776691Skchow 	}
27786691Skchow 	if (!osvwfeature)
27796691Skchow 		return (-1);
27806691Skchow 
27816691Skchow 	osvwlength = rdmsr(MSR_AMD_OSVW_ID_LEN) & OSVW_ID_LEN_MASK;
27826691Skchow 
27836691Skchow 	switch (erratum) {
27846691Skchow 	case 298:	/* osvwid is 0 */
27856691Skchow 		osvwid = 0;
27866691Skchow 		if (osvwlength <= (uint64_t)osvwid) {
27876691Skchow 			/* osvwid 0 is unknown */
27886691Skchow 			return (-1);
27896691Skchow 		}
27906691Skchow 
27916691Skchow 		/*
27926691Skchow 		 * Check the OSVW STATUS MSR to determine the state
27936691Skchow 		 * of the erratum where:
27946691Skchow 		 *   0 - fixed by HW
27956691Skchow 		 *   1 - BIOS has applied the workaround when BIOS
27966691Skchow 		 *   workaround is available. (Or for other errata,
27976691Skchow 		 *   OS workaround is required.)
27986691Skchow 		 * For a value of 1, caller will confirm that the
27996691Skchow 		 * erratum 298 workaround has indeed been applied by BIOS.
28006691Skchow 		 *
28016691Skchow 		 * A 1 may be set in cpus that have a HW fix
28026691Skchow 		 * in a mixed cpu system. Regarding erratum 298:
28036691Skchow 		 *   In a multiprocessor platform, the workaround above
28046691Skchow 		 *   should be applied to all processors regardless of
28056691Skchow 		 *   silicon revision when an affected processor is
28066691Skchow 		 *   present.
28076691Skchow 		 */
28086691Skchow 
28096691Skchow 		return (rdmsr(MSR_AMD_OSVW_STATUS +
28106691Skchow 		    (osvwid / OSVW_ID_CNT_PER_MSR)) &
28116691Skchow 		    (1ULL << (osvwid % OSVW_ID_CNT_PER_MSR)));
28126691Skchow 
28130Sstevel@tonic-gate 	default:
28140Sstevel@tonic-gate 		return (-1);
28150Sstevel@tonic-gate 	}
28160Sstevel@tonic-gate }
28170Sstevel@tonic-gate 
28180Sstevel@tonic-gate static const char assoc_str[] = "associativity";
28190Sstevel@tonic-gate static const char line_str[] = "line-size";
28200Sstevel@tonic-gate static const char size_str[] = "size";
28210Sstevel@tonic-gate 
28220Sstevel@tonic-gate static void
28230Sstevel@tonic-gate add_cache_prop(dev_info_t *devi, const char *label, const char *type,
28240Sstevel@tonic-gate     uint32_t val)
28250Sstevel@tonic-gate {
28260Sstevel@tonic-gate 	char buf[128];
28270Sstevel@tonic-gate 
28280Sstevel@tonic-gate 	/*
28290Sstevel@tonic-gate 	 * ndi_prop_update_int() is used because it is desirable for
28300Sstevel@tonic-gate 	 * DDI_PROP_HW_DEF and DDI_PROP_DONTSLEEP to be set.
28310Sstevel@tonic-gate 	 */
28320Sstevel@tonic-gate 	if (snprintf(buf, sizeof (buf), "%s-%s", label, type) < sizeof (buf))
28330Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, devi, buf, val);
28340Sstevel@tonic-gate }
28350Sstevel@tonic-gate 
28360Sstevel@tonic-gate /*
28370Sstevel@tonic-gate  * Intel-style cache/tlb description
28380Sstevel@tonic-gate  *
28390Sstevel@tonic-gate  * Standard cpuid level 2 gives a randomly ordered
28400Sstevel@tonic-gate  * selection of tags that index into a table that describes
28410Sstevel@tonic-gate  * cache and tlb properties.
28420Sstevel@tonic-gate  */
28430Sstevel@tonic-gate 
28440Sstevel@tonic-gate static const char l1_icache_str[] = "l1-icache";
28450Sstevel@tonic-gate static const char l1_dcache_str[] = "l1-dcache";
28460Sstevel@tonic-gate static const char l2_cache_str[] = "l2-cache";
28473446Smrj static const char l3_cache_str[] = "l3-cache";
28480Sstevel@tonic-gate static const char itlb4k_str[] = "itlb-4K";
28490Sstevel@tonic-gate static const char dtlb4k_str[] = "dtlb-4K";
28506964Svd224797 static const char itlb2M_str[] = "itlb-2M";
28510Sstevel@tonic-gate static const char itlb4M_str[] = "itlb-4M";
28520Sstevel@tonic-gate static const char dtlb4M_str[] = "dtlb-4M";
28536334Sksadhukh static const char dtlb24_str[] = "dtlb0-2M-4M";
28540Sstevel@tonic-gate static const char itlb424_str[] = "itlb-4K-2M-4M";
28556334Sksadhukh static const char itlb24_str[] = "itlb-2M-4M";
28560Sstevel@tonic-gate static const char dtlb44_str[] = "dtlb-4K-4M";
28570Sstevel@tonic-gate static const char sl1_dcache_str[] = "sectored-l1-dcache";
28580Sstevel@tonic-gate static const char sl2_cache_str[] = "sectored-l2-cache";
28590Sstevel@tonic-gate static const char itrace_str[] = "itrace-cache";
28600Sstevel@tonic-gate static const char sl3_cache_str[] = "sectored-l3-cache";
28616334Sksadhukh static const char sh_l2_tlb4k_str[] = "shared-l2-tlb-4k";
28620Sstevel@tonic-gate 
28630Sstevel@tonic-gate static const struct cachetab {
28640Sstevel@tonic-gate 	uint8_t 	ct_code;
28650Sstevel@tonic-gate 	uint8_t		ct_assoc;
28660Sstevel@tonic-gate 	uint16_t 	ct_line_size;
28670Sstevel@tonic-gate 	size_t		ct_size;
28680Sstevel@tonic-gate 	const char	*ct_label;
28690Sstevel@tonic-gate } intel_ctab[] = {
28706964Svd224797 	/*
28716964Svd224797 	 * maintain descending order!
28726964Svd224797 	 *
28736964Svd224797 	 * Codes ignored - Reason
28746964Svd224797 	 * ----------------------
28756964Svd224797 	 * 40H - intel_cpuid_4_cache_info() disambiguates l2/l3 cache
28766964Svd224797 	 * f0H/f1H - Currently we do not interpret prefetch size by design
28776964Svd224797 	 */
28786334Sksadhukh 	{ 0xe4, 16, 64, 8*1024*1024, l3_cache_str},
28796334Sksadhukh 	{ 0xe3, 16, 64, 4*1024*1024, l3_cache_str},
28806334Sksadhukh 	{ 0xe2, 16, 64, 2*1024*1024, l3_cache_str},
28816334Sksadhukh 	{ 0xde, 12, 64, 6*1024*1024, l3_cache_str},
28826334Sksadhukh 	{ 0xdd, 12, 64, 3*1024*1024, l3_cache_str},
28836334Sksadhukh 	{ 0xdc, 12, 64, ((1*1024*1024)+(512*1024)), l3_cache_str},
28846334Sksadhukh 	{ 0xd8, 8, 64, 4*1024*1024, l3_cache_str},
28856334Sksadhukh 	{ 0xd7, 8, 64, 2*1024*1024, l3_cache_str},
28866334Sksadhukh 	{ 0xd6, 8, 64, 1*1024*1024, l3_cache_str},
28876334Sksadhukh 	{ 0xd2, 4, 64, 2*1024*1024, l3_cache_str},
28886334Sksadhukh 	{ 0xd1, 4, 64, 1*1024*1024, l3_cache_str},
28896334Sksadhukh 	{ 0xd0, 4, 64, 512*1024, l3_cache_str},
28906334Sksadhukh 	{ 0xca, 4, 0, 512, sh_l2_tlb4k_str},
28916964Svd224797 	{ 0xc0, 4, 0, 8, dtlb44_str },
28926964Svd224797 	{ 0xba, 4, 0, 64, dtlb4k_str },
28933446Smrj 	{ 0xb4, 4, 0, 256, dtlb4k_str },
28940Sstevel@tonic-gate 	{ 0xb3, 4, 0, 128, dtlb4k_str },
28956334Sksadhukh 	{ 0xb2, 4, 0, 64, itlb4k_str },
28960Sstevel@tonic-gate 	{ 0xb0, 4, 0, 128, itlb4k_str },
28970Sstevel@tonic-gate 	{ 0x87, 8, 64, 1024*1024, l2_cache_str},
28980Sstevel@tonic-gate 	{ 0x86, 4, 64, 512*1024, l2_cache_str},
28990Sstevel@tonic-gate 	{ 0x85, 8, 32, 2*1024*1024, l2_cache_str},
29000Sstevel@tonic-gate 	{ 0x84, 8, 32, 1024*1024, l2_cache_str},
29010Sstevel@tonic-gate 	{ 0x83, 8, 32, 512*1024, l2_cache_str},
29020Sstevel@tonic-gate 	{ 0x82, 8, 32, 256*1024, l2_cache_str},
29036964Svd224797 	{ 0x80, 8, 64, 512*1024, l2_cache_str},
29040Sstevel@tonic-gate 	{ 0x7f, 2, 64, 512*1024, l2_cache_str},
29050Sstevel@tonic-gate 	{ 0x7d, 8, 64, 2*1024*1024, sl2_cache_str},
29060Sstevel@tonic-gate 	{ 0x7c, 8, 64, 1024*1024, sl2_cache_str},
29070Sstevel@tonic-gate 	{ 0x7b, 8, 64, 512*1024, sl2_cache_str},
29080Sstevel@tonic-gate 	{ 0x7a, 8, 64, 256*1024, sl2_cache_str},
29090Sstevel@tonic-gate 	{ 0x79, 8, 64, 128*1024, sl2_cache_str},
29100Sstevel@tonic-gate 	{ 0x78, 8, 64, 1024*1024, l2_cache_str},
29113446Smrj 	{ 0x73, 8, 0, 64*1024, itrace_str},
29120Sstevel@tonic-gate 	{ 0x72, 8, 0, 32*1024, itrace_str},
29130Sstevel@tonic-gate 	{ 0x71, 8, 0, 16*1024, itrace_str},
29140Sstevel@tonic-gate 	{ 0x70, 8, 0, 12*1024, itrace_str},
29150Sstevel@tonic-gate 	{ 0x68, 4, 64, 32*1024, sl1_dcache_str},
29160Sstevel@tonic-gate 	{ 0x67, 4, 64, 16*1024, sl1_dcache_str},
29170Sstevel@tonic-gate 	{ 0x66, 4, 64, 8*1024, sl1_dcache_str},
29180Sstevel@tonic-gate 	{ 0x60, 8, 64, 16*1024, sl1_dcache_str},
29190Sstevel@tonic-gate 	{ 0x5d, 0, 0, 256, dtlb44_str},
29200Sstevel@tonic-gate 	{ 0x5c, 0, 0, 128, dtlb44_str},
29210Sstevel@tonic-gate 	{ 0x5b, 0, 0, 64, dtlb44_str},
29226334Sksadhukh 	{ 0x5a, 4, 0, 32, dtlb24_str},
29236964Svd224797 	{ 0x59, 0, 0, 16, dtlb4k_str},
29246964Svd224797 	{ 0x57, 4, 0, 16, dtlb4k_str},
29256964Svd224797 	{ 0x56, 4, 0, 16, dtlb4M_str},
29266334Sksadhukh 	{ 0x55, 0, 0, 7, itlb24_str},
29270Sstevel@tonic-gate 	{ 0x52, 0, 0, 256, itlb424_str},
29280Sstevel@tonic-gate 	{ 0x51, 0, 0, 128, itlb424_str},
29290Sstevel@tonic-gate 	{ 0x50, 0, 0, 64, itlb424_str},
29306964Svd224797 	{ 0x4f, 0, 0, 32, itlb4k_str},
29316964Svd224797 	{ 0x4e, 24, 64, 6*1024*1024, l2_cache_str},
29323446Smrj 	{ 0x4d, 16, 64, 16*1024*1024, l3_cache_str},
29333446Smrj 	{ 0x4c, 12, 64, 12*1024*1024, l3_cache_str},
29343446Smrj 	{ 0x4b, 16, 64, 8*1024*1024, l3_cache_str},
29353446Smrj 	{ 0x4a, 12, 64, 6*1024*1024, l3_cache_str},
29363446Smrj 	{ 0x49, 16, 64, 4*1024*1024, l3_cache_str},
29376964Svd224797 	{ 0x48, 12, 64, 3*1024*1024, l2_cache_str},
29383446Smrj 	{ 0x47, 8, 64, 8*1024*1024, l3_cache_str},
29393446Smrj 	{ 0x46, 4, 64, 4*1024*1024, l3_cache_str},
29400Sstevel@tonic-gate 	{ 0x45, 4, 32, 2*1024*1024, l2_cache_str},
29410Sstevel@tonic-gate 	{ 0x44, 4, 32, 1024*1024, l2_cache_str},
29420Sstevel@tonic-gate 	{ 0x43, 4, 32, 512*1024, l2_cache_str},
29430Sstevel@tonic-gate 	{ 0x42, 4, 32, 256*1024, l2_cache_str},
29440Sstevel@tonic-gate 	{ 0x41, 4, 32, 128*1024, l2_cache_str},
29453446Smrj 	{ 0x3e, 4, 64, 512*1024, sl2_cache_str},
29463446Smrj 	{ 0x3d, 6, 64, 384*1024, sl2_cache_str},
29470Sstevel@tonic-gate 	{ 0x3c, 4, 64, 256*1024, sl2_cache_str},
29480Sstevel@tonic-gate 	{ 0x3b, 2, 64, 128*1024, sl2_cache_str},
29493446Smrj 	{ 0x3a, 6, 64, 192*1024, sl2_cache_str},
29500Sstevel@tonic-gate 	{ 0x39, 4, 64, 128*1024, sl2_cache_str},
29510Sstevel@tonic-gate 	{ 0x30, 8, 64, 32*1024, l1_icache_str},
29520Sstevel@tonic-gate 	{ 0x2c, 8, 64, 32*1024, l1_dcache_str},
29530Sstevel@tonic-gate 	{ 0x29, 8, 64, 4096*1024, sl3_cache_str},
29540Sstevel@tonic-gate 	{ 0x25, 8, 64, 2048*1024, sl3_cache_str},
29550Sstevel@tonic-gate 	{ 0x23, 8, 64, 1024*1024, sl3_cache_str},
29560Sstevel@tonic-gate 	{ 0x22, 4, 64, 512*1024, sl3_cache_str},
29576964Svd224797 	{ 0x0e, 6, 64, 24*1024, l1_dcache_str},
29586334Sksadhukh 	{ 0x0d, 4, 32, 16*1024, l1_dcache_str},
29590Sstevel@tonic-gate 	{ 0x0c, 4, 32, 16*1024, l1_dcache_str},
29603446Smrj 	{ 0x0b, 4, 0, 4, itlb4M_str},
29610Sstevel@tonic-gate 	{ 0x0a, 2, 32, 8*1024, l1_dcache_str},
29620Sstevel@tonic-gate 	{ 0x08, 4, 32, 16*1024, l1_icache_str},
29630Sstevel@tonic-gate 	{ 0x06, 4, 32, 8*1024, l1_icache_str},
29646964Svd224797 	{ 0x05, 4, 0, 32, dtlb4M_str},
29650Sstevel@tonic-gate 	{ 0x04, 4, 0, 8, dtlb4M_str},
29660Sstevel@tonic-gate 	{ 0x03, 4, 0, 64, dtlb4k_str},
29670Sstevel@tonic-gate 	{ 0x02, 4, 0, 2, itlb4M_str},
29680Sstevel@tonic-gate 	{ 0x01, 4, 0, 32, itlb4k_str},
29690Sstevel@tonic-gate 	{ 0 }
29700Sstevel@tonic-gate };
29710Sstevel@tonic-gate 
29720Sstevel@tonic-gate static const struct cachetab cyrix_ctab[] = {
29730Sstevel@tonic-gate 	{ 0x70, 4, 0, 32, "tlb-4K" },
29740Sstevel@tonic-gate 	{ 0x80, 4, 16, 16*1024, "l1-cache" },
29750Sstevel@tonic-gate 	{ 0 }
29760Sstevel@tonic-gate };
29770Sstevel@tonic-gate 
29780Sstevel@tonic-gate /*
29790Sstevel@tonic-gate  * Search a cache table for a matching entry
29800Sstevel@tonic-gate  */
29810Sstevel@tonic-gate static const struct cachetab *
29820Sstevel@tonic-gate find_cacheent(const struct cachetab *ct, uint_t code)
29830Sstevel@tonic-gate {
29840Sstevel@tonic-gate 	if (code != 0) {
29850Sstevel@tonic-gate 		for (; ct->ct_code != 0; ct++)
29860Sstevel@tonic-gate 			if (ct->ct_code <= code)
29870Sstevel@tonic-gate 				break;
29880Sstevel@tonic-gate 		if (ct->ct_code == code)
29890Sstevel@tonic-gate 			return (ct);
29900Sstevel@tonic-gate 	}
29910Sstevel@tonic-gate 	return (NULL);
29920Sstevel@tonic-gate }
29930Sstevel@tonic-gate 
29940Sstevel@tonic-gate /*
29955438Sksadhukh  * Populate cachetab entry with L2 or L3 cache-information using
29965438Sksadhukh  * cpuid function 4. This function is called from intel_walk_cacheinfo()
29975438Sksadhukh  * when descriptor 0x49 is encountered. It returns 0 if no such cache
29985438Sksadhukh  * information is found.
29995438Sksadhukh  */
30005438Sksadhukh static int
30015438Sksadhukh intel_cpuid_4_cache_info(struct cachetab *ct, struct cpuid_info *cpi)
30025438Sksadhukh {
30035438Sksadhukh 	uint32_t level, i;
30045438Sksadhukh 	int ret = 0;
30055438Sksadhukh 
30065438Sksadhukh 	for (i = 0; i < cpi->cpi_std_4_size; i++) {
30075438Sksadhukh 		level = CPI_CACHE_LVL(cpi->cpi_std_4[i]);
30085438Sksadhukh 
30095438Sksadhukh 		if (level == 2 || level == 3) {
30105438Sksadhukh 			ct->ct_assoc = CPI_CACHE_WAYS(cpi->cpi_std_4[i]) + 1;
30115438Sksadhukh 			ct->ct_line_size =
30125438Sksadhukh 			    CPI_CACHE_COH_LN_SZ(cpi->cpi_std_4[i]) + 1;
30135438Sksadhukh 			ct->ct_size = ct->ct_assoc *
30145438Sksadhukh 			    (CPI_CACHE_PARTS(cpi->cpi_std_4[i]) + 1) *
30155438Sksadhukh 			    ct->ct_line_size *
30165438Sksadhukh 			    (cpi->cpi_std_4[i]->cp_ecx + 1);
30175438Sksadhukh 
30185438Sksadhukh 			if (level == 2) {
30195438Sksadhukh 				ct->ct_label = l2_cache_str;
30205438Sksadhukh 			} else if (level == 3) {
30215438Sksadhukh 				ct->ct_label = l3_cache_str;
30225438Sksadhukh 			}
30235438Sksadhukh 			ret = 1;
30245438Sksadhukh 		}
30255438Sksadhukh 	}
30265438Sksadhukh 
30275438Sksadhukh 	return (ret);
30285438Sksadhukh }
30295438Sksadhukh 
30305438Sksadhukh /*
30310Sstevel@tonic-gate  * Walk the cacheinfo descriptor, applying 'func' to every valid element
30320Sstevel@tonic-gate  * The walk is terminated if the walker returns non-zero.
30330Sstevel@tonic-gate  */
30340Sstevel@tonic-gate static void
30350Sstevel@tonic-gate intel_walk_cacheinfo(struct cpuid_info *cpi,
30360Sstevel@tonic-gate     void *arg, int (*func)(void *, const struct cachetab *))
30370Sstevel@tonic-gate {
30380Sstevel@tonic-gate 	const struct cachetab *ct;
30396964Svd224797 	struct cachetab des_49_ct, des_b1_ct;
30400Sstevel@tonic-gate 	uint8_t *dp;
30410Sstevel@tonic-gate 	int i;
30420Sstevel@tonic-gate 
30430Sstevel@tonic-gate 	if ((dp = cpi->cpi_cacheinfo) == NULL)
30440Sstevel@tonic-gate 		return;
30454797Sksadhukh 	for (i = 0; i < cpi->cpi_ncache; i++, dp++) {
30464797Sksadhukh 		/*
30474797Sksadhukh 		 * For overloaded descriptor 0x49 we use cpuid function 4
30485438Sksadhukh 		 * if supported by the current processor, to create
30494797Sksadhukh 		 * cache information.
30506964Svd224797 		 * For overloaded descriptor 0xb1 we use X86_PAE flag
30516964Svd224797 		 * to disambiguate the cache information.
30524797Sksadhukh 		 */
30535438Sksadhukh 		if (*dp == 0x49 && cpi->cpi_maxeax >= 0x4 &&
30545438Sksadhukh 		    intel_cpuid_4_cache_info(&des_49_ct, cpi) == 1) {
30555438Sksadhukh 				ct = &des_49_ct;
30566964Svd224797 		} else if (*dp == 0xb1) {
30576964Svd224797 			des_b1_ct.ct_code = 0xb1;
30586964Svd224797 			des_b1_ct.ct_assoc = 4;
30596964Svd224797 			des_b1_ct.ct_line_size = 0;
30606964Svd224797 			if (x86_feature & X86_PAE) {
30616964Svd224797 				des_b1_ct.ct_size = 8;
30626964Svd224797 				des_b1_ct.ct_label = itlb2M_str;
30636964Svd224797 			} else {
30646964Svd224797 				des_b1_ct.ct_size = 4;
30656964Svd224797 				des_b1_ct.ct_label = itlb4M_str;
30666964Svd224797 			}
30676964Svd224797 			ct = &des_b1_ct;
30685438Sksadhukh 		} else {
30695438Sksadhukh 			if ((ct = find_cacheent(intel_ctab, *dp)) == NULL) {
30705438Sksadhukh 				continue;
30715438Sksadhukh 			}
30724797Sksadhukh 		}
30734797Sksadhukh 
30745438Sksadhukh 		if (func(arg, ct) != 0) {
30755438Sksadhukh 			break;
30760Sstevel@tonic-gate 		}
30774797Sksadhukh 	}
30780Sstevel@tonic-gate }
30790Sstevel@tonic-gate 
30800Sstevel@tonic-gate /*
30810Sstevel@tonic-gate  * (Like the Intel one, except for Cyrix CPUs)
30820Sstevel@tonic-gate  */
30830Sstevel@tonic-gate static void
30840Sstevel@tonic-gate cyrix_walk_cacheinfo(struct cpuid_info *cpi,
30850Sstevel@tonic-gate     void *arg, int (*func)(void *, const struct cachetab *))
30860Sstevel@tonic-gate {
30870Sstevel@tonic-gate 	const struct cachetab *ct;
30880Sstevel@tonic-gate 	uint8_t *dp;
30890Sstevel@tonic-gate 	int i;
30900Sstevel@tonic-gate 
30910Sstevel@tonic-gate 	if ((dp = cpi->cpi_cacheinfo) == NULL)
30920Sstevel@tonic-gate 		return;
30930Sstevel@tonic-gate 	for (i = 0; i < cpi->cpi_ncache; i++, dp++) {
30940Sstevel@tonic-gate 		/*
30950Sstevel@tonic-gate 		 * Search Cyrix-specific descriptor table first ..
30960Sstevel@tonic-gate 		 */
30970Sstevel@tonic-gate 		if ((ct = find_cacheent(cyrix_ctab, *dp)) != NULL) {
30980Sstevel@tonic-gate 			if (func(arg, ct) != 0)
30990Sstevel@tonic-gate 				break;
31000Sstevel@tonic-gate 			continue;
31010Sstevel@tonic-gate 		}
31020Sstevel@tonic-gate 		/*
31030Sstevel@tonic-gate 		 * .. else fall back to the Intel one
31040Sstevel@tonic-gate 		 */
31050Sstevel@tonic-gate 		if ((ct = find_cacheent(intel_ctab, *dp)) != NULL) {
31060Sstevel@tonic-gate 			if (func(arg, ct) != 0)
31070Sstevel@tonic-gate 				break;
31080Sstevel@tonic-gate 			continue;
31090Sstevel@tonic-gate 		}
31100Sstevel@tonic-gate 	}
31110Sstevel@tonic-gate }
31120Sstevel@tonic-gate 
31130Sstevel@tonic-gate /*
31140Sstevel@tonic-gate  * A cacheinfo walker that adds associativity, line-size, and size properties
31150Sstevel@tonic-gate  * to the devinfo node it is passed as an argument.
31160Sstevel@tonic-gate  */
31170Sstevel@tonic-gate static int
31180Sstevel@tonic-gate add_cacheent_props(void *arg, const struct cachetab *ct)
31190Sstevel@tonic-gate {
31200Sstevel@tonic-gate 	dev_info_t *devi = arg;
31210Sstevel@tonic-gate 
31220Sstevel@tonic-gate 	add_cache_prop(devi, ct->ct_label, assoc_str, ct->ct_assoc);
31230Sstevel@tonic-gate 	if (ct->ct_line_size != 0)
31240Sstevel@tonic-gate 		add_cache_prop(devi, ct->ct_label, line_str,
31250Sstevel@tonic-gate 		    ct->ct_line_size);
31260Sstevel@tonic-gate 	add_cache_prop(devi, ct->ct_label, size_str, ct->ct_size);
31270Sstevel@tonic-gate 	return (0);
31280Sstevel@tonic-gate }
31290Sstevel@tonic-gate 
31304797Sksadhukh 
31310Sstevel@tonic-gate static const char fully_assoc[] = "fully-associative?";
31320Sstevel@tonic-gate 
31330Sstevel@tonic-gate /*
31340Sstevel@tonic-gate  * AMD style cache/tlb description
31350Sstevel@tonic-gate  *
31360Sstevel@tonic-gate  * Extended functions 5 and 6 directly describe properties of
31370Sstevel@tonic-gate  * tlbs and various cache levels.
31380Sstevel@tonic-gate  */
31390Sstevel@tonic-gate static void
31400Sstevel@tonic-gate add_amd_assoc(dev_info_t *devi, const char *label, uint_t assoc)
31410Sstevel@tonic-gate {
31420Sstevel@tonic-gate 	switch (assoc) {
31430Sstevel@tonic-gate 	case 0:	/* reserved; ignore */
31440Sstevel@tonic-gate 		break;
31450Sstevel@tonic-gate 	default:
31460Sstevel@tonic-gate 		add_cache_prop(devi, label, assoc_str, assoc);
31470Sstevel@tonic-gate 		break;
31480Sstevel@tonic-gate 	case 0xff:
31490Sstevel@tonic-gate 		add_cache_prop(devi, label, fully_assoc, 1);
31500Sstevel@tonic-gate 		break;
31510Sstevel@tonic-gate 	}
31520Sstevel@tonic-gate }
31530Sstevel@tonic-gate 
31540Sstevel@tonic-gate static void
31550Sstevel@tonic-gate add_amd_tlb(dev_info_t *devi, const char *label, uint_t assoc, uint_t size)
31560Sstevel@tonic-gate {
31570Sstevel@tonic-gate 	if (size == 0)
31580Sstevel@tonic-gate 		return;
31590Sstevel@tonic-gate 	add_cache_prop(devi, label, size_str, size);
31600Sstevel@tonic-gate 	add_amd_assoc(devi, label, assoc);
31610Sstevel@tonic-gate }
31620Sstevel@tonic-gate 
31630Sstevel@tonic-gate static void
31640Sstevel@tonic-gate add_amd_cache(dev_info_t *devi, const char *label,
31650Sstevel@tonic-gate     uint_t size, uint_t assoc, uint_t lines_per_tag, uint_t line_size)
31660Sstevel@tonic-gate {
31670Sstevel@tonic-gate 	if (size == 0 || line_size == 0)
31680Sstevel@tonic-gate 		return;
31690Sstevel@tonic-gate 	add_amd_assoc(devi, label, assoc);
31700Sstevel@tonic-gate 	/*
31710Sstevel@tonic-gate 	 * Most AMD parts have a sectored cache. Multiple cache lines are
31720Sstevel@tonic-gate 	 * associated with each tag. A sector consists of all cache lines
31730Sstevel@tonic-gate 	 * associated with a tag. For example, the AMD K6-III has a sector
31740Sstevel@tonic-gate 	 * size of 2 cache lines per tag.
31750Sstevel@tonic-gate 	 */
31760Sstevel@tonic-gate 	if (lines_per_tag != 0)
31770Sstevel@tonic-gate 		add_cache_prop(devi, label, "lines-per-tag", lines_per_tag);
31780Sstevel@tonic-gate 	add_cache_prop(devi, label, line_str, line_size);
31790Sstevel@tonic-gate 	add_cache_prop(devi, label, size_str, size * 1024);
31800Sstevel@tonic-gate }
31810Sstevel@tonic-gate 
31820Sstevel@tonic-gate static void
31830Sstevel@tonic-gate add_amd_l2_assoc(dev_info_t *devi, const char *label, uint_t assoc)
31840Sstevel@tonic-gate {
31850Sstevel@tonic-gate 	switch (assoc) {
31860Sstevel@tonic-gate 	case 0:	/* off */
31870Sstevel@tonic-gate 		break;
31880Sstevel@tonic-gate 	case 1:
31890Sstevel@tonic-gate 	case 2:
31900Sstevel@tonic-gate 	case 4:
31910Sstevel@tonic-gate 		add_cache_prop(devi, label, assoc_str, assoc);
31920Sstevel@tonic-gate 		break;
31930Sstevel@tonic-gate 	case 6:
31940Sstevel@tonic-gate 		add_cache_prop(devi, label, assoc_str, 8);
31950Sstevel@tonic-gate 		break;
31960Sstevel@tonic-gate 	case 8:
31970Sstevel@tonic-gate 		add_cache_prop(devi, label, assoc_str, 16);
31980Sstevel@tonic-gate 		break;
31990Sstevel@tonic-gate 	case 0xf:
32000Sstevel@tonic-gate 		add_cache_prop(devi, label, fully_assoc, 1);
32010Sstevel@tonic-gate 		break;
32020Sstevel@tonic-gate 	default: /* reserved; ignore */
32030Sstevel@tonic-gate 		break;
32040Sstevel@tonic-gate 	}
32050Sstevel@tonic-gate }
32060Sstevel@tonic-gate 
32070Sstevel@tonic-gate static void
32080Sstevel@tonic-gate add_amd_l2_tlb(dev_info_t *devi, const char *label, uint_t assoc, uint_t size)
32090Sstevel@tonic-gate {
32100Sstevel@tonic-gate 	if (size == 0 || assoc == 0)
32110Sstevel@tonic-gate 		return;
32120Sstevel@tonic-gate 	add_amd_l2_assoc(devi, label, assoc);
32130Sstevel@tonic-gate 	add_cache_prop(devi, label, size_str, size);
32140Sstevel@tonic-gate }
32150Sstevel@tonic-gate 
32160Sstevel@tonic-gate static void
32170Sstevel@tonic-gate add_amd_l2_cache(dev_info_t *devi, const char *label,
32180Sstevel@tonic-gate     uint_t size, uint_t assoc, uint_t lines_per_tag, uint_t line_size)
32190Sstevel@tonic-gate {
32200Sstevel@tonic-gate 	if (size == 0 || assoc == 0 || line_size == 0)
32210Sstevel@tonic-gate 		return;
32220Sstevel@tonic-gate 	add_amd_l2_assoc(devi, label, assoc);
32230Sstevel@tonic-gate 	if (lines_per_tag != 0)
32240Sstevel@tonic-gate 		add_cache_prop(devi, label, "lines-per-tag", lines_per_tag);
32250Sstevel@tonic-gate 	add_cache_prop(devi, label, line_str, line_size);
32260Sstevel@tonic-gate 	add_cache_prop(devi, label, size_str, size * 1024);
32270Sstevel@tonic-gate }
32280Sstevel@tonic-gate 
32290Sstevel@tonic-gate static void
32300Sstevel@tonic-gate amd_cache_info(struct cpuid_info *cpi, dev_info_t *devi)
32310Sstevel@tonic-gate {
32321228Sandrei 	struct cpuid_regs *cp;
32330Sstevel@tonic-gate 
32340Sstevel@tonic-gate 	if (cpi->cpi_xmaxeax < 0x80000005)
32350Sstevel@tonic-gate 		return;
32360Sstevel@tonic-gate 	cp = &cpi->cpi_extd[5];
32370Sstevel@tonic-gate 
32380Sstevel@tonic-gate 	/*
32390Sstevel@tonic-gate 	 * 4M/2M L1 TLB configuration
32400Sstevel@tonic-gate 	 *
32410Sstevel@tonic-gate 	 * We report the size for 2M pages because AMD uses two
32420Sstevel@tonic-gate 	 * TLB entries for one 4M page.
32430Sstevel@tonic-gate 	 */
32440Sstevel@tonic-gate 	add_amd_tlb(devi, "dtlb-2M",
32450Sstevel@tonic-gate 	    BITX(cp->cp_eax, 31, 24), BITX(cp->cp_eax, 23, 16));
32460Sstevel@tonic-gate 	add_amd_tlb(devi, "itlb-2M",
32470Sstevel@tonic-gate 	    BITX(cp->cp_eax, 15, 8), BITX(cp->cp_eax, 7, 0));
32480Sstevel@tonic-gate 
32490Sstevel@tonic-gate 	/*
32500Sstevel@tonic-gate 	 * 4K L1 TLB configuration
32510Sstevel@tonic-gate 	 */
32520Sstevel@tonic-gate 
32530Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
32540Sstevel@tonic-gate 		uint_t nentries;
32550Sstevel@tonic-gate 	case X86_VENDOR_TM:
32560Sstevel@tonic-gate 		if (cpi->cpi_family >= 5) {
32570Sstevel@tonic-gate 			/*
32580Sstevel@tonic-gate 			 * Crusoe processors have 256 TLB entries, but
32590Sstevel@tonic-gate 			 * cpuid data format constrains them to only
32600Sstevel@tonic-gate 			 * reporting 255 of them.
32610Sstevel@tonic-gate 			 */
32620Sstevel@tonic-gate 			if ((nentries = BITX(cp->cp_ebx, 23, 16)) == 255)
32630Sstevel@tonic-gate 				nentries = 256;
32640Sstevel@tonic-gate 			/*
32650Sstevel@tonic-gate 			 * Crusoe processors also have a unified TLB
32660Sstevel@tonic-gate 			 */
32670Sstevel@tonic-gate 			add_amd_tlb(devi, "tlb-4K", BITX(cp->cp_ebx, 31, 24),
32680Sstevel@tonic-gate 			    nentries);
32690Sstevel@tonic-gate 			break;
32700Sstevel@tonic-gate 		}
32710Sstevel@tonic-gate 		/*FALLTHROUGH*/
32720Sstevel@tonic-gate 	default:
32730Sstevel@tonic-gate 		add_amd_tlb(devi, itlb4k_str,
32740Sstevel@tonic-gate 		    BITX(cp->cp_ebx, 31, 24), BITX(cp->cp_ebx, 23, 16));
32750Sstevel@tonic-gate 		add_amd_tlb(devi, dtlb4k_str,
32760Sstevel@tonic-gate 		    BITX(cp->cp_ebx, 15, 8), BITX(cp->cp_ebx, 7, 0));
32770Sstevel@tonic-gate 		break;
32780Sstevel@tonic-gate 	}
32790Sstevel@tonic-gate 
32800Sstevel@tonic-gate 	/*
32810Sstevel@tonic-gate 	 * data L1 cache configuration
32820Sstevel@tonic-gate 	 */
32830Sstevel@tonic-gate 
32840Sstevel@tonic-gate 	add_amd_cache(devi, l1_dcache_str,
32850Sstevel@tonic-gate 	    BITX(cp->cp_ecx, 31, 24), BITX(cp->cp_ecx, 23, 16),
32860Sstevel@tonic-gate 	    BITX(cp->cp_ecx, 15, 8), BITX(cp->cp_ecx, 7, 0));
32870Sstevel@tonic-gate 
32880Sstevel@tonic-gate 	/*
32890Sstevel@tonic-gate 	 * code L1 cache configuration
32900Sstevel@tonic-gate 	 */
32910Sstevel@tonic-gate 
32920Sstevel@tonic-gate 	add_amd_cache(devi, l1_icache_str,
32930Sstevel@tonic-gate 	    BITX(cp->cp_edx, 31, 24), BITX(cp->cp_edx, 23, 16),
32940Sstevel@tonic-gate 	    BITX(cp->cp_edx, 15, 8), BITX(cp->cp_edx, 7, 0));
32950Sstevel@tonic-gate 
32960Sstevel@tonic-gate 	if (cpi->cpi_xmaxeax < 0x80000006)
32970Sstevel@tonic-gate 		return;
32980Sstevel@tonic-gate 	cp = &cpi->cpi_extd[6];
32990Sstevel@tonic-gate 
33000Sstevel@tonic-gate 	/* Check for a unified L2 TLB for large pages */
33010Sstevel@tonic-gate 
33020Sstevel@tonic-gate 	if (BITX(cp->cp_eax, 31, 16) == 0)
33030Sstevel@tonic-gate 		add_amd_l2_tlb(devi, "l2-tlb-2M",
33040Sstevel@tonic-gate 		    BITX(cp->cp_eax, 15, 12), BITX(cp->cp_eax, 11, 0));
33050Sstevel@tonic-gate 	else {
33060Sstevel@tonic-gate 		add_amd_l2_tlb(devi, "l2-dtlb-2M",
33070Sstevel@tonic-gate 		    BITX(cp->cp_eax, 31, 28), BITX(cp->cp_eax, 27, 16));
33080Sstevel@tonic-gate 		add_amd_l2_tlb(devi, "l2-itlb-2M",
33090Sstevel@tonic-gate 		    BITX(cp->cp_eax, 15, 12), BITX(cp->cp_eax, 11, 0));
33100Sstevel@tonic-gate 	}
33110Sstevel@tonic-gate 
33120Sstevel@tonic-gate 	/* Check for a unified L2 TLB for 4K pages */
33130Sstevel@tonic-gate 
33140Sstevel@tonic-gate 	if (BITX(cp->cp_ebx, 31, 16) == 0) {
33150Sstevel@tonic-gate 		add_amd_l2_tlb(devi, "l2-tlb-4K",
33160Sstevel@tonic-gate 		    BITX(cp->cp_eax, 15, 12), BITX(cp->cp_eax, 11, 0));
33170Sstevel@tonic-gate 	} else {
33180Sstevel@tonic-gate 		add_amd_l2_tlb(devi, "l2-dtlb-4K",
33190Sstevel@tonic-gate 		    BITX(cp->cp_eax, 31, 28), BITX(cp->cp_eax, 27, 16));
33200Sstevel@tonic-gate 		add_amd_l2_tlb(devi, "l2-itlb-4K",
33210Sstevel@tonic-gate 		    BITX(cp->cp_eax, 15, 12), BITX(cp->cp_eax, 11, 0));
33220Sstevel@tonic-gate 	}
33230Sstevel@tonic-gate 
33240Sstevel@tonic-gate 	add_amd_l2_cache(devi, l2_cache_str,
33250Sstevel@tonic-gate 	    BITX(cp->cp_ecx, 31, 16), BITX(cp->cp_ecx, 15, 12),
33260Sstevel@tonic-gate 	    BITX(cp->cp_ecx, 11, 8), BITX(cp->cp_ecx, 7, 0));
33270Sstevel@tonic-gate }
33280Sstevel@tonic-gate 
33290Sstevel@tonic-gate /*
33300Sstevel@tonic-gate  * There are two basic ways that the x86 world describes it cache
33310Sstevel@tonic-gate  * and tlb architecture - Intel's way and AMD's way.
33320Sstevel@tonic-gate  *
33330Sstevel@tonic-gate  * Return which flavor of cache architecture we should use
33340Sstevel@tonic-gate  */
33350Sstevel@tonic-gate static int
33360Sstevel@tonic-gate x86_which_cacheinfo(struct cpuid_info *cpi)
33370Sstevel@tonic-gate {
33380Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
33390Sstevel@tonic-gate 	case X86_VENDOR_Intel:
33400Sstevel@tonic-gate 		if (cpi->cpi_maxeax >= 2)
33410Sstevel@tonic-gate 			return (X86_VENDOR_Intel);
33420Sstevel@tonic-gate 		break;
33430Sstevel@tonic-gate 	case X86_VENDOR_AMD:
33440Sstevel@tonic-gate 		/*
33450Sstevel@tonic-gate 		 * The K5 model 1 was the first part from AMD that reported
33460Sstevel@tonic-gate 		 * cache sizes via extended cpuid functions.
33470Sstevel@tonic-gate 		 */
33480Sstevel@tonic-gate 		if (cpi->cpi_family > 5 ||
33490Sstevel@tonic-gate 		    (cpi->cpi_family == 5 && cpi->cpi_model >= 1))
33500Sstevel@tonic-gate 			return (X86_VENDOR_AMD);
33510Sstevel@tonic-gate 		break;
33520Sstevel@tonic-gate 	case X86_VENDOR_TM:
33530Sstevel@tonic-gate 		if (cpi->cpi_family >= 5)
33540Sstevel@tonic-gate 			return (X86_VENDOR_AMD);
33550Sstevel@tonic-gate 		/*FALLTHROUGH*/
33560Sstevel@tonic-gate 	default:
33570Sstevel@tonic-gate 		/*
33580Sstevel@tonic-gate 		 * If they have extended CPU data for 0x80000005
33590Sstevel@tonic-gate 		 * then we assume they have AMD-format cache
33600Sstevel@tonic-gate 		 * information.
33610Sstevel@tonic-gate 		 *
33620Sstevel@tonic-gate 		 * If not, and the vendor happens to be Cyrix,
33630Sstevel@tonic-gate 		 * then try our-Cyrix specific handler.
33640Sstevel@tonic-gate 		 *
33650Sstevel@tonic-gate 		 * If we're not Cyrix, then assume we're using Intel's
33660Sstevel@tonic-gate 		 * table-driven format instead.
33670Sstevel@tonic-gate 		 */
33680Sstevel@tonic-gate 		if (cpi->cpi_xmaxeax >= 0x80000005)
33690Sstevel@tonic-gate 			return (X86_VENDOR_AMD);
33700Sstevel@tonic-gate 		else if (cpi->cpi_vendor == X86_VENDOR_Cyrix)
33710Sstevel@tonic-gate 			return (X86_VENDOR_Cyrix);
33720Sstevel@tonic-gate 		else if (cpi->cpi_maxeax >= 2)
33730Sstevel@tonic-gate 			return (X86_VENDOR_Intel);
33740Sstevel@tonic-gate 		break;
33750Sstevel@tonic-gate 	}
33760Sstevel@tonic-gate 	return (-1);
33770Sstevel@tonic-gate }
33780Sstevel@tonic-gate 
33790Sstevel@tonic-gate /*
33800Sstevel@tonic-gate  * create a node for the given cpu under the prom root node.
33810Sstevel@tonic-gate  * Also, create a cpu node in the device tree.
33820Sstevel@tonic-gate  */
33830Sstevel@tonic-gate static dev_info_t *cpu_nex_devi = NULL;
33840Sstevel@tonic-gate static kmutex_t cpu_node_lock;
33850Sstevel@tonic-gate 
33860Sstevel@tonic-gate /*
33870Sstevel@tonic-gate  * Called from post_startup() and mp_startup()
33880Sstevel@tonic-gate  */
33890Sstevel@tonic-gate void
33900Sstevel@tonic-gate add_cpunode2devtree(processorid_t cpu_id, struct cpuid_info *cpi)
33910Sstevel@tonic-gate {
33920Sstevel@tonic-gate 	dev_info_t *cpu_devi;
33930Sstevel@tonic-gate 	int create;
33940Sstevel@tonic-gate 
33950Sstevel@tonic-gate 	mutex_enter(&cpu_node_lock);
33960Sstevel@tonic-gate 
33970Sstevel@tonic-gate 	/*
33980Sstevel@tonic-gate 	 * create a nexus node for all cpus identified as 'cpu_id' under
33990Sstevel@tonic-gate 	 * the root node.
34000Sstevel@tonic-gate 	 */
34010Sstevel@tonic-gate 	if (cpu_nex_devi == NULL) {
34020Sstevel@tonic-gate 		if (ndi_devi_alloc(ddi_root_node(), "cpus",
3403789Sahrens 		    (pnode_t)DEVI_SID_NODEID, &cpu_nex_devi) != NDI_SUCCESS) {
34040Sstevel@tonic-gate 			mutex_exit(&cpu_node_lock);
34050Sstevel@tonic-gate 			return;
34060Sstevel@tonic-gate 		}
34070Sstevel@tonic-gate 		(void) ndi_devi_online(cpu_nex_devi, 0);
34080Sstevel@tonic-gate 	}
34090Sstevel@tonic-gate 
34100Sstevel@tonic-gate 	/*
34110Sstevel@tonic-gate 	 * create a child node for cpu identified as 'cpu_id'
34120Sstevel@tonic-gate 	 */
34130Sstevel@tonic-gate 	cpu_devi = ddi_add_child(cpu_nex_devi, "cpu", DEVI_SID_NODEID,
34144481Sbholler 	    cpu_id);
34150Sstevel@tonic-gate 	if (cpu_devi == NULL) {
34160Sstevel@tonic-gate 		mutex_exit(&cpu_node_lock);
34170Sstevel@tonic-gate 		return;
34180Sstevel@tonic-gate 	}
34190Sstevel@tonic-gate 
34200Sstevel@tonic-gate 	/* device_type */
34210Sstevel@tonic-gate 
34220Sstevel@tonic-gate 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, cpu_devi,
34230Sstevel@tonic-gate 	    "device_type", "cpu");
34240Sstevel@tonic-gate 
34250Sstevel@tonic-gate 	/* reg */
34260Sstevel@tonic-gate 
34270Sstevel@tonic-gate 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
34280Sstevel@tonic-gate 	    "reg", cpu_id);
34290Sstevel@tonic-gate 
34300Sstevel@tonic-gate 	/* cpu-mhz, and clock-frequency */
34310Sstevel@tonic-gate 
34320Sstevel@tonic-gate 	if (cpu_freq > 0) {
34330Sstevel@tonic-gate 		long long mul;
34340Sstevel@tonic-gate 
34350Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
34360Sstevel@tonic-gate 		    "cpu-mhz", cpu_freq);
34370Sstevel@tonic-gate 
34380Sstevel@tonic-gate 		if ((mul = cpu_freq * 1000000LL) <= INT_MAX)
34390Sstevel@tonic-gate 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
34400Sstevel@tonic-gate 			    "clock-frequency", (int)mul);
34410Sstevel@tonic-gate 	}
34420Sstevel@tonic-gate 
34430Sstevel@tonic-gate 	(void) ndi_devi_online(cpu_devi, 0);
34440Sstevel@tonic-gate 
34450Sstevel@tonic-gate 	if ((x86_feature & X86_CPUID) == 0) {
34460Sstevel@tonic-gate 		mutex_exit(&cpu_node_lock);
34470Sstevel@tonic-gate 		return;
34480Sstevel@tonic-gate 	}
34490Sstevel@tonic-gate 
34500Sstevel@tonic-gate 	/* vendor-id */
34510Sstevel@tonic-gate 
34520Sstevel@tonic-gate 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, cpu_devi,
34534481Sbholler 	    "vendor-id", cpi->cpi_vendorstr);
34540Sstevel@tonic-gate 
34550Sstevel@tonic-gate 	if (cpi->cpi_maxeax == 0) {
34560Sstevel@tonic-gate 		mutex_exit(&cpu_node_lock);
34570Sstevel@tonic-gate 		return;
34580Sstevel@tonic-gate 	}
34590Sstevel@tonic-gate 
34600Sstevel@tonic-gate 	/*
34610Sstevel@tonic-gate 	 * family, model, and step
34620Sstevel@tonic-gate 	 */
34630Sstevel@tonic-gate 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
34644481Sbholler 	    "family", CPI_FAMILY(cpi));
34650Sstevel@tonic-gate 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
34664481Sbholler 	    "cpu-model", CPI_MODEL(cpi));
34670Sstevel@tonic-gate 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
34684481Sbholler 	    "stepping-id", CPI_STEP(cpi));
34690Sstevel@tonic-gate 
34700Sstevel@tonic-gate 	/* type */
34710Sstevel@tonic-gate 
34720Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
34730Sstevel@tonic-gate 	case X86_VENDOR_Intel:
34740Sstevel@tonic-gate 		create = 1;
34750Sstevel@tonic-gate 		break;
34760Sstevel@tonic-gate 	default:
34770Sstevel@tonic-gate 		create = 0;
34780Sstevel@tonic-gate 		break;
34790Sstevel@tonic-gate 	}
34800Sstevel@tonic-gate 	if (create)
34810Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
34824481Sbholler 		    "type", CPI_TYPE(cpi));
34830Sstevel@tonic-gate 
34840Sstevel@tonic-gate 	/* ext-family */
34850Sstevel@tonic-gate 
34860Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
34870Sstevel@tonic-gate 	case X86_VENDOR_Intel:
34880Sstevel@tonic-gate 	case X86_VENDOR_AMD:
34890Sstevel@tonic-gate 		create = cpi->cpi_family >= 0xf;
34900Sstevel@tonic-gate 		break;
34910Sstevel@tonic-gate 	default:
34920Sstevel@tonic-gate 		create = 0;
34930Sstevel@tonic-gate 		break;
34940Sstevel@tonic-gate 	}
34950Sstevel@tonic-gate 	if (create)
34960Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
34970Sstevel@tonic-gate 		    "ext-family", CPI_FAMILY_XTD(cpi));
34980Sstevel@tonic-gate 
34990Sstevel@tonic-gate 	/* ext-model */
35000Sstevel@tonic-gate 
35010Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
35020Sstevel@tonic-gate 	case X86_VENDOR_Intel:
35036317Skk208521 		create = IS_EXTENDED_MODEL_INTEL(cpi);
35042001Sdmick 		break;
35050Sstevel@tonic-gate 	case X86_VENDOR_AMD:
35061582Skchow 		create = CPI_FAMILY(cpi) == 0xf;
35070Sstevel@tonic-gate 		break;
35080Sstevel@tonic-gate 	default:
35090Sstevel@tonic-gate 		create = 0;
35100Sstevel@tonic-gate 		break;
35110Sstevel@tonic-gate 	}
35120Sstevel@tonic-gate 	if (create)
35130Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
35144481Sbholler 		    "ext-model", CPI_MODEL_XTD(cpi));
35150Sstevel@tonic-gate 
35160Sstevel@tonic-gate 	/* generation */
35170Sstevel@tonic-gate 
35180Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
35190Sstevel@tonic-gate 	case X86_VENDOR_AMD:
35200Sstevel@tonic-gate 		/*
35210Sstevel@tonic-gate 		 * AMD K5 model 1 was the first part to support this
35220Sstevel@tonic-gate 		 */
35230Sstevel@tonic-gate 		create = cpi->cpi_xmaxeax >= 0x80000001;
35240Sstevel@tonic-gate 		break;
35250Sstevel@tonic-gate 	default:
35260Sstevel@tonic-gate 		create = 0;
35270Sstevel@tonic-gate 		break;
35280Sstevel@tonic-gate 	}
35290Sstevel@tonic-gate 	if (create)
35300Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
35310Sstevel@tonic-gate 		    "generation", BITX((cpi)->cpi_extd[1].cp_eax, 11, 8));
35320Sstevel@tonic-gate 
35330Sstevel@tonic-gate 	/* brand-id */
35340Sstevel@tonic-gate 
35350Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
35360Sstevel@tonic-gate 	case X86_VENDOR_Intel:
35370Sstevel@tonic-gate 		/*
35380Sstevel@tonic-gate 		 * brand id first appeared on Pentium III Xeon model 8,
35390Sstevel@tonic-gate 		 * and Celeron model 8 processors and Opteron
35400Sstevel@tonic-gate 		 */
35410Sstevel@tonic-gate 		create = cpi->cpi_family > 6 ||
35420Sstevel@tonic-gate 		    (cpi->cpi_family == 6 && cpi->cpi_model >= 8);
35430Sstevel@tonic-gate 		break;
35440Sstevel@tonic-gate 	case X86_VENDOR_AMD:
35450Sstevel@tonic-gate 		create = cpi->cpi_family >= 0xf;
35460Sstevel@tonic-gate 		break;
35470Sstevel@tonic-gate 	default:
35480Sstevel@tonic-gate 		create = 0;
35490Sstevel@tonic-gate 		break;
35500Sstevel@tonic-gate 	}
35510Sstevel@tonic-gate 	if (create && cpi->cpi_brandid != 0) {
35520Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
35530Sstevel@tonic-gate 		    "brand-id", cpi->cpi_brandid);
35540Sstevel@tonic-gate 	}
35550Sstevel@tonic-gate 
35560Sstevel@tonic-gate 	/* chunks, and apic-id */
35570Sstevel@tonic-gate 
35580Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
35590Sstevel@tonic-gate 		/*
35600Sstevel@tonic-gate 		 * first available on Pentium IV and Opteron (K8)
35610Sstevel@tonic-gate 		 */
35621975Sdmick 	case X86_VENDOR_Intel:
35631975Sdmick 		create = IS_NEW_F6(cpi) || cpi->cpi_family >= 0xf;
35641975Sdmick 		break;
35651975Sdmick 	case X86_VENDOR_AMD:
35660Sstevel@tonic-gate 		create = cpi->cpi_family >= 0xf;
35670Sstevel@tonic-gate 		break;
35680Sstevel@tonic-gate 	default:
35690Sstevel@tonic-gate 		create = 0;
35700Sstevel@tonic-gate 		break;
35710Sstevel@tonic-gate 	}
35720Sstevel@tonic-gate 	if (create) {
35730Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
35744481Sbholler 		    "chunks", CPI_CHUNKS(cpi));
35750Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
35767282Smishra 		    "apic-id", cpi->cpi_apicid);
35771414Scindi 		if (cpi->cpi_chipid >= 0) {
35780Sstevel@tonic-gate 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
35790Sstevel@tonic-gate 			    "chip#", cpi->cpi_chipid);
35801414Scindi 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
35811414Scindi 			    "clog#", cpi->cpi_clogid);
35821414Scindi 		}
35830Sstevel@tonic-gate 	}
35840Sstevel@tonic-gate 
35850Sstevel@tonic-gate 	/* cpuid-features */
35860Sstevel@tonic-gate 
35870Sstevel@tonic-gate 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
35880Sstevel@tonic-gate 	    "cpuid-features", CPI_FEATURES_EDX(cpi));
35890Sstevel@tonic-gate 
35900Sstevel@tonic-gate 
35910Sstevel@tonic-gate 	/* cpuid-features-ecx */
35920Sstevel@tonic-gate 
35930Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
35940Sstevel@tonic-gate 	case X86_VENDOR_Intel:
35951975Sdmick 		create = IS_NEW_F6(cpi) || cpi->cpi_family >= 0xf;
35960Sstevel@tonic-gate 		break;
35970Sstevel@tonic-gate 	default:
35980Sstevel@tonic-gate 		create = 0;
35990Sstevel@tonic-gate 		break;
36000Sstevel@tonic-gate 	}
36010Sstevel@tonic-gate 	if (create)
36020Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
36030Sstevel@tonic-gate 		    "cpuid-features-ecx", CPI_FEATURES_ECX(cpi));
36040Sstevel@tonic-gate 
36050Sstevel@tonic-gate 	/* ext-cpuid-features */
36060Sstevel@tonic-gate 
36070Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
36081975Sdmick 	case X86_VENDOR_Intel:
36090Sstevel@tonic-gate 	case X86_VENDOR_AMD:
36100Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
36110Sstevel@tonic-gate 	case X86_VENDOR_TM:
36120Sstevel@tonic-gate 	case X86_VENDOR_Centaur:
36130Sstevel@tonic-gate 		create = cpi->cpi_xmaxeax >= 0x80000001;
36140Sstevel@tonic-gate 		break;
36150Sstevel@tonic-gate 	default:
36160Sstevel@tonic-gate 		create = 0;
36170Sstevel@tonic-gate 		break;
36180Sstevel@tonic-gate 	}
36191975Sdmick 	if (create) {
36200Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
36214481Sbholler 		    "ext-cpuid-features", CPI_FEATURES_XTD_EDX(cpi));
36221975Sdmick 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
36234481Sbholler 		    "ext-cpuid-features-ecx", CPI_FEATURES_XTD_ECX(cpi));
36241975Sdmick 	}
36250Sstevel@tonic-gate 
36260Sstevel@tonic-gate 	/*
36270Sstevel@tonic-gate 	 * Brand String first appeared in Intel Pentium IV, AMD K5
36280Sstevel@tonic-gate 	 * model 1, and Cyrix GXm.  On earlier models we try and
36290Sstevel@tonic-gate 	 * simulate something similar .. so this string should always
36300Sstevel@tonic-gate 	 * same -something- about the processor, however lame.
36310Sstevel@tonic-gate 	 */
36320Sstevel@tonic-gate 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, cpu_devi,
36330Sstevel@tonic-gate 	    "brand-string", cpi->cpi_brandstr);
36340Sstevel@tonic-gate 
36350Sstevel@tonic-gate 	/*
36360Sstevel@tonic-gate 	 * Finally, cache and tlb information
36370Sstevel@tonic-gate 	 */
36380Sstevel@tonic-gate 	switch (x86_which_cacheinfo(cpi)) {
36390Sstevel@tonic-gate 	case X86_VENDOR_Intel:
36400Sstevel@tonic-gate 		intel_walk_cacheinfo(cpi, cpu_devi, add_cacheent_props);
36410Sstevel@tonic-gate 		break;
36420Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
36430Sstevel@tonic-gate 		cyrix_walk_cacheinfo(cpi, cpu_devi, add_cacheent_props);
36440Sstevel@tonic-gate 		break;
36450Sstevel@tonic-gate 	case X86_VENDOR_AMD:
36460Sstevel@tonic-gate 		amd_cache_info(cpi, cpu_devi);
36470Sstevel@tonic-gate 		break;
36480Sstevel@tonic-gate 	default:
36490Sstevel@tonic-gate 		break;
36500Sstevel@tonic-gate 	}
36510Sstevel@tonic-gate 
36520Sstevel@tonic-gate 	mutex_exit(&cpu_node_lock);
36530Sstevel@tonic-gate }
36540Sstevel@tonic-gate 
36550Sstevel@tonic-gate struct l2info {
36560Sstevel@tonic-gate 	int *l2i_csz;
36570Sstevel@tonic-gate 	int *l2i_lsz;
36580Sstevel@tonic-gate 	int *l2i_assoc;
36590Sstevel@tonic-gate 	int l2i_ret;
36600Sstevel@tonic-gate };
36610Sstevel@tonic-gate 
36620Sstevel@tonic-gate /*
36630Sstevel@tonic-gate  * A cacheinfo walker that fetches the size, line-size and associativity
36640Sstevel@tonic-gate  * of the L2 cache
36650Sstevel@tonic-gate  */
36660Sstevel@tonic-gate static int
36670Sstevel@tonic-gate intel_l2cinfo(void *arg, const struct cachetab *ct)
36680Sstevel@tonic-gate {
36690Sstevel@tonic-gate 	struct l2info *l2i = arg;
36700Sstevel@tonic-gate 	int *ip;
36710Sstevel@tonic-gate 
36720Sstevel@tonic-gate 	if (ct->ct_label != l2_cache_str &&
36730Sstevel@tonic-gate 	    ct->ct_label != sl2_cache_str)
36740Sstevel@tonic-gate 		return (0);	/* not an L2 -- keep walking */
36750Sstevel@tonic-gate 
36760Sstevel@tonic-gate 	if ((ip = l2i->l2i_csz) != NULL)
36770Sstevel@tonic-gate 		*ip = ct->ct_size;
36780Sstevel@tonic-gate 	if ((ip = l2i->l2i_lsz) != NULL)
36790Sstevel@tonic-gate 		*ip = ct->ct_line_size;
36800Sstevel@tonic-gate 	if ((ip = l2i->l2i_assoc) != NULL)
36810Sstevel@tonic-gate 		*ip = ct->ct_assoc;
36820Sstevel@tonic-gate 	l2i->l2i_ret = ct->ct_size;
36830Sstevel@tonic-gate 	return (1);		/* was an L2 -- terminate walk */
36840Sstevel@tonic-gate }
36850Sstevel@tonic-gate 
36865070Skchow /*
36875070Skchow  * AMD L2/L3 Cache and TLB Associativity Field Definition:
36885070Skchow  *
36895070Skchow  *	Unlike the associativity for the L1 cache and tlb where the 8 bit
36905070Skchow  *	value is the associativity, the associativity for the L2 cache and
36915070Skchow  *	tlb is encoded in the following table. The 4 bit L2 value serves as
36925070Skchow  *	an index into the amd_afd[] array to determine the associativity.
36935070Skchow  *	-1 is undefined. 0 is fully associative.
36945070Skchow  */
36955070Skchow 
36965070Skchow static int amd_afd[] =
36975070Skchow 	{-1, 1, 2, -1, 4, -1, 8, -1, 16, -1, 32, 48, 64, 96, 128, 0};
36985070Skchow 
36990Sstevel@tonic-gate static void
37000Sstevel@tonic-gate amd_l2cacheinfo(struct cpuid_info *cpi, struct l2info *l2i)
37010Sstevel@tonic-gate {
37021228Sandrei 	struct cpuid_regs *cp;
37030Sstevel@tonic-gate 	uint_t size, assoc;
37045070Skchow 	int i;
37050Sstevel@tonic-gate 	int *ip;
37060Sstevel@tonic-gate 
37070Sstevel@tonic-gate 	if (cpi->cpi_xmaxeax < 0x80000006)
37080Sstevel@tonic-gate 		return;
37090Sstevel@tonic-gate 	cp = &cpi->cpi_extd[6];
37100Sstevel@tonic-gate 
37115070Skchow 	if ((i = BITX(cp->cp_ecx, 15, 12)) != 0 &&
37120Sstevel@tonic-gate 	    (size = BITX(cp->cp_ecx, 31, 16)) != 0) {
37130Sstevel@tonic-gate 		uint_t cachesz = size * 1024;
37145070Skchow 		assoc = amd_afd[i];
37155070Skchow 
37165070Skchow 		ASSERT(assoc != -1);
37170Sstevel@tonic-gate 
37180Sstevel@tonic-gate 		if ((ip = l2i->l2i_csz) != NULL)
37190Sstevel@tonic-gate 			*ip = cachesz;
37200Sstevel@tonic-gate 		if ((ip = l2i->l2i_lsz) != NULL)
37210Sstevel@tonic-gate 			*ip = BITX(cp->cp_ecx, 7, 0);
37220Sstevel@tonic-gate 		if ((ip = l2i->l2i_assoc) != NULL)
37230Sstevel@tonic-gate 			*ip = assoc;
37240Sstevel@tonic-gate 		l2i->l2i_ret = cachesz;
37250Sstevel@tonic-gate 	}
37260Sstevel@tonic-gate }
37270Sstevel@tonic-gate 
37280Sstevel@tonic-gate int
37290Sstevel@tonic-gate getl2cacheinfo(cpu_t *cpu, int *csz, int *lsz, int *assoc)
37300Sstevel@tonic-gate {
37310Sstevel@tonic-gate 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
37320Sstevel@tonic-gate 	struct l2info __l2info, *l2i = &__l2info;
37330Sstevel@tonic-gate 
37340Sstevel@tonic-gate 	l2i->l2i_csz = csz;
37350Sstevel@tonic-gate 	l2i->l2i_lsz = lsz;
37360Sstevel@tonic-gate 	l2i->l2i_assoc = assoc;
37370Sstevel@tonic-gate 	l2i->l2i_ret = -1;
37380Sstevel@tonic-gate 
37390Sstevel@tonic-gate 	switch (x86_which_cacheinfo(cpi)) {
37400Sstevel@tonic-gate 	case X86_VENDOR_Intel:
37410Sstevel@tonic-gate 		intel_walk_cacheinfo(cpi, l2i, intel_l2cinfo);
37420Sstevel@tonic-gate 		break;
37430Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
37440Sstevel@tonic-gate 		cyrix_walk_cacheinfo(cpi, l2i, intel_l2cinfo);
37450Sstevel@tonic-gate 		break;
37460Sstevel@tonic-gate 	case X86_VENDOR_AMD:
37470Sstevel@tonic-gate 		amd_l2cacheinfo(cpi, l2i);
37480Sstevel@tonic-gate 		break;
37490Sstevel@tonic-gate 	default:
37500Sstevel@tonic-gate 		break;
37510Sstevel@tonic-gate 	}
37520Sstevel@tonic-gate 	return (l2i->l2i_ret);
37530Sstevel@tonic-gate }
37544481Sbholler 
37555084Sjohnlev #if !defined(__xpv)
37565084Sjohnlev 
37575045Sbholler uint32_t *
37585045Sbholler cpuid_mwait_alloc(cpu_t *cpu)
37595045Sbholler {
37605045Sbholler 	uint32_t	*ret;
37615045Sbholler 	size_t		mwait_size;
37625045Sbholler 
37635045Sbholler 	ASSERT(cpuid_checkpass(cpu, 2));
37645045Sbholler 
37655045Sbholler 	mwait_size = cpu->cpu_m.mcpu_cpi->cpi_mwait.mon_max;
37665045Sbholler 	if (mwait_size == 0)
37675045Sbholler 		return (NULL);
37685045Sbholler 
37695045Sbholler 	/*
37705045Sbholler 	 * kmem_alloc() returns cache line size aligned data for mwait_size
37715045Sbholler 	 * allocations.  mwait_size is currently cache line sized.  Neither
37725045Sbholler 	 * of these implementation details are guarantied to be true in the
37735045Sbholler 	 * future.
37745045Sbholler 	 *
37755045Sbholler 	 * First try allocating mwait_size as kmem_alloc() currently returns
37765045Sbholler 	 * correctly aligned memory.  If kmem_alloc() does not return
37775045Sbholler 	 * mwait_size aligned memory, then use mwait_size ROUNDUP.
37785045Sbholler 	 *
37795045Sbholler 	 * Set cpi_mwait.buf_actual and cpi_mwait.size_actual in case we
37805045Sbholler 	 * decide to free this memory.
37815045Sbholler 	 */
37825045Sbholler 	ret = kmem_zalloc(mwait_size, KM_SLEEP);
37835045Sbholler 	if (ret == (uint32_t *)P2ROUNDUP((uintptr_t)ret, mwait_size)) {
37845045Sbholler 		cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual = ret;
37855045Sbholler 		cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual = mwait_size;
37865045Sbholler 		*ret = MWAIT_RUNNING;
37875045Sbholler 		return (ret);
37885045Sbholler 	} else {
37895045Sbholler 		kmem_free(ret, mwait_size);
37905045Sbholler 		ret = kmem_zalloc(mwait_size * 2, KM_SLEEP);
37915045Sbholler 		cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual = ret;
37925045Sbholler 		cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual = mwait_size * 2;
37935045Sbholler 		ret = (uint32_t *)P2ROUNDUP((uintptr_t)ret, mwait_size);
37945045Sbholler 		*ret = MWAIT_RUNNING;
37955045Sbholler 		return (ret);
37965045Sbholler 	}
37975045Sbholler }
37985045Sbholler 
37995045Sbholler void
38005045Sbholler cpuid_mwait_free(cpu_t *cpu)
38014481Sbholler {
38024481Sbholler 	ASSERT(cpuid_checkpass(cpu, 2));
38035045Sbholler 
38045045Sbholler 	if (cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual != NULL &&
38055045Sbholler 	    cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual > 0) {
38065045Sbholler 		kmem_free(cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual,
38075045Sbholler 		    cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual);
38085045Sbholler 	}
38095045Sbholler 
38105045Sbholler 	cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual = NULL;
38115045Sbholler 	cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual = 0;
38124481Sbholler }
38135084Sjohnlev 
38145322Ssudheer void
38155322Ssudheer patch_tsc_read(int flag)
38165322Ssudheer {
38175322Ssudheer 	size_t cnt;
38187532SSean.Ye@Sun.COM 
38195322Ssudheer 	switch (flag) {
38205322Ssudheer 	case X86_NO_TSC:
38215322Ssudheer 		cnt = &_no_rdtsc_end - &_no_rdtsc_start;
38225338Ssudheer 		(void) memcpy((void *)tsc_read, (void *)&_no_rdtsc_start, cnt);
38235322Ssudheer 		break;
38245322Ssudheer 	case X86_HAVE_TSCP:
38255322Ssudheer 		cnt = &_tscp_end - &_tscp_start;
38265338Ssudheer 		(void) memcpy((void *)tsc_read, (void *)&_tscp_start, cnt);
38275322Ssudheer 		break;
38285322Ssudheer 	case X86_TSC_MFENCE:
38295322Ssudheer 		cnt = &_tsc_mfence_end - &_tsc_mfence_start;
38305338Ssudheer 		(void) memcpy((void *)tsc_read,
38315338Ssudheer 		    (void *)&_tsc_mfence_start, cnt);
38325322Ssudheer 		break;
38336642Ssudheer 	case X86_TSC_LFENCE:
38346642Ssudheer 		cnt = &_tsc_lfence_end - &_tsc_lfence_start;
38356642Ssudheer 		(void) memcpy((void *)tsc_read,
38366642Ssudheer 		    (void *)&_tsc_lfence_start, cnt);
38376642Ssudheer 		break;
38385322Ssudheer 	default:
38395322Ssudheer 		break;
38405322Ssudheer 	}
38415322Ssudheer }
38425322Ssudheer 
38435084Sjohnlev #endif	/* !__xpv */
3844