xref: /onnv-gate/usr/src/uts/i86pc/os/cpuid.c (revision 8990:67ae112ecc4f)
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 /*
228906SEric.Saxe@Sun.COM  * Copyright 2009 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>
518930SBill.Holler@Sun.COM #else
528930SBill.Holler@Sun.COM #include <sys/ontrap.h>
537532SSean.Ye@Sun.COM #endif
547532SSean.Ye@Sun.COM 
550Sstevel@tonic-gate /*
560Sstevel@tonic-gate  * Pass 0 of cpuid feature analysis happens in locore. It contains special code
570Sstevel@tonic-gate  * to recognize Cyrix processors that are not cpuid-compliant, and to deal with
580Sstevel@tonic-gate  * them accordingly. For most modern processors, feature detection occurs here
590Sstevel@tonic-gate  * in pass 1.
600Sstevel@tonic-gate  *
610Sstevel@tonic-gate  * Pass 1 of cpuid feature analysis happens just at the beginning of mlsetup()
620Sstevel@tonic-gate  * for the boot CPU and does the basic analysis that the early kernel needs.
630Sstevel@tonic-gate  * x86_feature is set based on the return value of cpuid_pass1() of the boot
640Sstevel@tonic-gate  * CPU.
650Sstevel@tonic-gate  *
660Sstevel@tonic-gate  * Pass 1 includes:
670Sstevel@tonic-gate  *
680Sstevel@tonic-gate  *	o Determining vendor/model/family/stepping and setting x86_type and
690Sstevel@tonic-gate  *	  x86_vendor accordingly.
700Sstevel@tonic-gate  *	o Processing the feature flags returned by the cpuid instruction while
710Sstevel@tonic-gate  *	  applying any workarounds or tricks for the specific processor.
720Sstevel@tonic-gate  *	o Mapping the feature flags into Solaris feature bits (X86_*).
730Sstevel@tonic-gate  *	o Processing extended feature flags if supported by the processor,
740Sstevel@tonic-gate  *	  again while applying specific processor knowledge.
750Sstevel@tonic-gate  *	o Determining the CMT characteristics of the system.
760Sstevel@tonic-gate  *
770Sstevel@tonic-gate  * Pass 1 is done on non-boot CPUs during their initialization and the results
780Sstevel@tonic-gate  * are used only as a meager attempt at ensuring that all processors within the
790Sstevel@tonic-gate  * system support the same features.
800Sstevel@tonic-gate  *
810Sstevel@tonic-gate  * Pass 2 of cpuid feature analysis happens just at the beginning
820Sstevel@tonic-gate  * of startup().  It just copies in and corrects the remainder
830Sstevel@tonic-gate  * of the cpuid data we depend on: standard cpuid functions that we didn't
840Sstevel@tonic-gate  * need for pass1 feature analysis, and extended cpuid functions beyond the
850Sstevel@tonic-gate  * simple feature processing done in pass1.
860Sstevel@tonic-gate  *
870Sstevel@tonic-gate  * Pass 3 of cpuid analysis is invoked after basic kernel services; in
880Sstevel@tonic-gate  * particular kernel memory allocation has been made available. It creates a
890Sstevel@tonic-gate  * readable brand string based on the data collected in the first two passes.
900Sstevel@tonic-gate  *
910Sstevel@tonic-gate  * Pass 4 of cpuid analysis is invoked after post_startup() when all
920Sstevel@tonic-gate  * the support infrastructure for various hardware features has been
930Sstevel@tonic-gate  * initialized. It determines which processor features will be reported
940Sstevel@tonic-gate  * to userland via the aux vector.
950Sstevel@tonic-gate  *
960Sstevel@tonic-gate  * All passes are executed on all CPUs, but only the boot CPU determines what
970Sstevel@tonic-gate  * features the kernel will use.
980Sstevel@tonic-gate  *
990Sstevel@tonic-gate  * Much of the worst junk in this file is for the support of processors
1000Sstevel@tonic-gate  * that didn't really implement the cpuid instruction properly.
1010Sstevel@tonic-gate  *
1020Sstevel@tonic-gate  * NOTE: The accessor functions (cpuid_get*) are aware of, and ASSERT upon,
1030Sstevel@tonic-gate  * the pass numbers.  Accordingly, changes to the pass code may require changes
1040Sstevel@tonic-gate  * to the accessor code.
1050Sstevel@tonic-gate  */
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate uint_t x86_feature = 0;
1080Sstevel@tonic-gate uint_t x86_vendor = X86_VENDOR_IntelClone;
1090Sstevel@tonic-gate uint_t x86_type = X86_TYPE_OTHER;
1107589SVikram.Hegde@Sun.COM uint_t x86_clflush_size = 0;
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate uint_t pentiumpro_bug4046376;
1130Sstevel@tonic-gate uint_t pentiumpro_bug4064495;
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate uint_t enable486;
116*8990SSurya.Prakki@Sun.COM /*
117*8990SSurya.Prakki@Sun.COM  * This is set if Solaris is booted in a fully virtualized mode:
118*8990SSurya.Prakki@Sun.COM  *	- as HVM guest under xVM
119*8990SSurya.Prakki@Sun.COM  *	- as guest under VMware
120*8990SSurya.Prakki@Sun.COM  * check_for_hvm() has the logic to detect these 2 cases.
121*8990SSurya.Prakki@Sun.COM  * This is not applicable if Solaris is booted as a para virtual guest.
122*8990SSurya.Prakki@Sun.COM  */
123*8990SSurya.Prakki@Sun.COM int platform_is_virt = 0;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate /*
1264481Sbholler  * monitor/mwait info.
1275045Sbholler  *
1285045Sbholler  * size_actual and buf_actual are the real address and size allocated to get
1295045Sbholler  * proper mwait_buf alignement.  buf_actual and size_actual should be passed
1305045Sbholler  * to kmem_free().  Currently kmem_alloc() and mwait happen to both use
1315045Sbholler  * processor cache-line alignment, but this is not guarantied in the furture.
1324481Sbholler  */
1334481Sbholler struct mwait_info {
1344481Sbholler 	size_t		mon_min;	/* min size to avoid missed wakeups */
1354481Sbholler 	size_t		mon_max;	/* size to avoid false wakeups */
1365045Sbholler 	size_t		size_actual;	/* size actually allocated */
1375045Sbholler 	void		*buf_actual;	/* memory actually allocated */
1384481Sbholler 	uint32_t	support;	/* processor support of monitor/mwait */
1394481Sbholler };
1404481Sbholler 
1414481Sbholler /*
1420Sstevel@tonic-gate  * These constants determine how many of the elements of the
1430Sstevel@tonic-gate  * cpuid we cache in the cpuid_info data structure; the
1440Sstevel@tonic-gate  * remaining elements are accessible via the cpuid instruction.
1450Sstevel@tonic-gate  */
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate #define	NMAX_CPI_STD	6		/* eax = 0 .. 5 */
1480Sstevel@tonic-gate #define	NMAX_CPI_EXTD	9		/* eax = 0x80000000 .. 0x80000008 */
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate struct cpuid_info {
1510Sstevel@tonic-gate 	uint_t cpi_pass;		/* last pass completed */
1520Sstevel@tonic-gate 	/*
1530Sstevel@tonic-gate 	 * standard function information
1540Sstevel@tonic-gate 	 */
1550Sstevel@tonic-gate 	uint_t cpi_maxeax;		/* fn 0: %eax */
1560Sstevel@tonic-gate 	char cpi_vendorstr[13];		/* fn 0: %ebx:%ecx:%edx */
1570Sstevel@tonic-gate 	uint_t cpi_vendor;		/* enum of cpi_vendorstr */
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	uint_t cpi_family;		/* fn 1: extended family */
1600Sstevel@tonic-gate 	uint_t cpi_model;		/* fn 1: extended model */
1610Sstevel@tonic-gate 	uint_t cpi_step;		/* fn 1: stepping */
1620Sstevel@tonic-gate 	chipid_t cpi_chipid;		/* fn 1: %ebx: chip # on ht cpus */
1630Sstevel@tonic-gate 	uint_t cpi_brandid;		/* fn 1: %ebx: brand ID */
1640Sstevel@tonic-gate 	int cpi_clogid;			/* fn 1: %ebx: thread # */
1651228Sandrei 	uint_t cpi_ncpu_per_chip;	/* fn 1: %ebx: logical cpu count */
1660Sstevel@tonic-gate 	uint8_t cpi_cacheinfo[16];	/* fn 2: intel-style cache desc */
1670Sstevel@tonic-gate 	uint_t cpi_ncache;		/* fn 2: number of elements */
1684606Sesaxe 	uint_t cpi_ncpu_shr_last_cache;	/* fn 4: %eax: ncpus sharing cache */
1694606Sesaxe 	id_t cpi_last_lvl_cacheid;	/* fn 4: %eax: derived cache id */
1704606Sesaxe 	uint_t cpi_std_4_size;		/* fn 4: number of fn 4 elements */
1714606Sesaxe 	struct cpuid_regs **cpi_std_4;	/* fn 4: %ecx == 0 .. fn4_size */
1721228Sandrei 	struct cpuid_regs cpi_std[NMAX_CPI_STD];	/* 0 .. 5 */
1730Sstevel@tonic-gate 	/*
1740Sstevel@tonic-gate 	 * extended function information
1750Sstevel@tonic-gate 	 */
1760Sstevel@tonic-gate 	uint_t cpi_xmaxeax;		/* fn 0x80000000: %eax */
1770Sstevel@tonic-gate 	char cpi_brandstr[49];		/* fn 0x8000000[234] */
1780Sstevel@tonic-gate 	uint8_t cpi_pabits;		/* fn 0x80000006: %eax */
1790Sstevel@tonic-gate 	uint8_t cpi_vabits;		/* fn 0x80000006: %eax */
1801228Sandrei 	struct cpuid_regs cpi_extd[NMAX_CPI_EXTD]; /* 0x8000000[0-8] */
1815870Sgavinm 	id_t cpi_coreid;		/* same coreid => strands share core */
1825870Sgavinm 	int cpi_pkgcoreid;		/* core number within single package */
1831228Sandrei 	uint_t cpi_ncore_per_chip;	/* AMD: fn 0x80000008: %ecx[7-0] */
1841228Sandrei 					/* Intel: fn 4: %eax[31-26] */
1850Sstevel@tonic-gate 	/*
1860Sstevel@tonic-gate 	 * supported feature information
1870Sstevel@tonic-gate 	 */
1883446Smrj 	uint32_t cpi_support[5];
1890Sstevel@tonic-gate #define	STD_EDX_FEATURES	0
1900Sstevel@tonic-gate #define	AMD_EDX_FEATURES	1
1910Sstevel@tonic-gate #define	TM_EDX_FEATURES		2
1920Sstevel@tonic-gate #define	STD_ECX_FEATURES	3
1933446Smrj #define	AMD_ECX_FEATURES	4
1942869Sgavinm 	/*
1952869Sgavinm 	 * Synthesized information, where known.
1962869Sgavinm 	 */
1972869Sgavinm 	uint32_t cpi_chiprev;		/* See X86_CHIPREV_* in x86_archext.h */
1982869Sgavinm 	const char *cpi_chiprevstr;	/* May be NULL if chiprev unknown */
1992869Sgavinm 	uint32_t cpi_socket;		/* Chip package/socket type */
2004481Sbholler 
2014481Sbholler 	struct mwait_info cpi_mwait;	/* fn 5: monitor/mwait info */
2027282Smishra 	uint32_t cpi_apicid;
2030Sstevel@tonic-gate };
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate static struct cpuid_info cpuid_info0;
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate /*
2090Sstevel@tonic-gate  * These bit fields are defined by the Intel Application Note AP-485
2100Sstevel@tonic-gate  * "Intel Processor Identification and the CPUID Instruction"
2110Sstevel@tonic-gate  */
2120Sstevel@tonic-gate #define	CPI_FAMILY_XTD(cpi)	BITX((cpi)->cpi_std[1].cp_eax, 27, 20)
2130Sstevel@tonic-gate #define	CPI_MODEL_XTD(cpi)	BITX((cpi)->cpi_std[1].cp_eax, 19, 16)
2140Sstevel@tonic-gate #define	CPI_TYPE(cpi)		BITX((cpi)->cpi_std[1].cp_eax, 13, 12)
2150Sstevel@tonic-gate #define	CPI_FAMILY(cpi)		BITX((cpi)->cpi_std[1].cp_eax, 11, 8)
2160Sstevel@tonic-gate #define	CPI_STEP(cpi)		BITX((cpi)->cpi_std[1].cp_eax, 3, 0)
2170Sstevel@tonic-gate #define	CPI_MODEL(cpi)		BITX((cpi)->cpi_std[1].cp_eax, 7, 4)
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate #define	CPI_FEATURES_EDX(cpi)		((cpi)->cpi_std[1].cp_edx)
2200Sstevel@tonic-gate #define	CPI_FEATURES_ECX(cpi)		((cpi)->cpi_std[1].cp_ecx)
2210Sstevel@tonic-gate #define	CPI_FEATURES_XTD_EDX(cpi)	((cpi)->cpi_extd[1].cp_edx)
2220Sstevel@tonic-gate #define	CPI_FEATURES_XTD_ECX(cpi)	((cpi)->cpi_extd[1].cp_ecx)
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate #define	CPI_BRANDID(cpi)	BITX((cpi)->cpi_std[1].cp_ebx, 7, 0)
2250Sstevel@tonic-gate #define	CPI_CHUNKS(cpi)		BITX((cpi)->cpi_std[1].cp_ebx, 15, 7)
2260Sstevel@tonic-gate #define	CPI_CPU_COUNT(cpi)	BITX((cpi)->cpi_std[1].cp_ebx, 23, 16)
2270Sstevel@tonic-gate #define	CPI_APIC_ID(cpi)	BITX((cpi)->cpi_std[1].cp_ebx, 31, 24)
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate #define	CPI_MAXEAX_MAX		0x100		/* sanity control */
2300Sstevel@tonic-gate #define	CPI_XMAXEAX_MAX		0x80000100
2314606Sesaxe #define	CPI_FN4_ECX_MAX		0x20		/* sanity: max fn 4 levels */
2327282Smishra #define	CPI_FNB_ECX_MAX		0x20		/* sanity: max fn B levels */
2334606Sesaxe 
2344606Sesaxe /*
2354606Sesaxe  * Function 4 (Deterministic Cache Parameters) macros
2364606Sesaxe  * Defined by Intel Application Note AP-485
2374606Sesaxe  */
2384606Sesaxe #define	CPI_NUM_CORES(regs)		BITX((regs)->cp_eax, 31, 26)
2394606Sesaxe #define	CPI_NTHR_SHR_CACHE(regs)	BITX((regs)->cp_eax, 25, 14)
2404606Sesaxe #define	CPI_FULL_ASSOC_CACHE(regs)	BITX((regs)->cp_eax, 9, 9)
2414606Sesaxe #define	CPI_SELF_INIT_CACHE(regs)	BITX((regs)->cp_eax, 8, 8)
2424606Sesaxe #define	CPI_CACHE_LVL(regs)		BITX((regs)->cp_eax, 7, 5)
2434606Sesaxe #define	CPI_CACHE_TYPE(regs)		BITX((regs)->cp_eax, 4, 0)
2447282Smishra #define	CPI_CPU_LEVEL_TYPE(regs)	BITX((regs)->cp_ecx, 15, 8)
2454606Sesaxe 
2464606Sesaxe #define	CPI_CACHE_WAYS(regs)		BITX((regs)->cp_ebx, 31, 22)
2474606Sesaxe #define	CPI_CACHE_PARTS(regs)		BITX((regs)->cp_ebx, 21, 12)
2484606Sesaxe #define	CPI_CACHE_COH_LN_SZ(regs)	BITX((regs)->cp_ebx, 11, 0)
2494606Sesaxe 
2504606Sesaxe #define	CPI_CACHE_SETS(regs)		BITX((regs)->cp_ecx, 31, 0)
2514606Sesaxe 
2524606Sesaxe #define	CPI_PREFCH_STRIDE(regs)		BITX((regs)->cp_edx, 9, 0)
2534606Sesaxe 
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate /*
2561975Sdmick  * A couple of shorthand macros to identify "later" P6-family chips
2571975Sdmick  * like the Pentium M and Core.  First, the "older" P6-based stuff
2581975Sdmick  * (loosely defined as "pre-Pentium-4"):
2591975Sdmick  * P6, PII, Mobile PII, PII Xeon, PIII, Mobile PIII, PIII Xeon
2601975Sdmick  */
2611975Sdmick 
2621975Sdmick #define	IS_LEGACY_P6(cpi) (			\
2631975Sdmick 	cpi->cpi_family == 6 && 		\
2641975Sdmick 		(cpi->cpi_model == 1 ||		\
2651975Sdmick 		cpi->cpi_model == 3 ||		\
2661975Sdmick 		cpi->cpi_model == 5 ||		\
2671975Sdmick 		cpi->cpi_model == 6 ||		\
2681975Sdmick 		cpi->cpi_model == 7 ||		\
2691975Sdmick 		cpi->cpi_model == 8 ||		\
2701975Sdmick 		cpi->cpi_model == 0xA ||	\
2711975Sdmick 		cpi->cpi_model == 0xB)		\
2721975Sdmick )
2731975Sdmick 
2741975Sdmick /* A "new F6" is everything with family 6 that's not the above */
2751975Sdmick #define	IS_NEW_F6(cpi) ((cpi->cpi_family == 6) && !IS_LEGACY_P6(cpi))
2761975Sdmick 
2774855Sksadhukh /* Extended family/model support */
2784855Sksadhukh #define	IS_EXTENDED_MODEL_INTEL(cpi) (cpi->cpi_family == 0x6 || \
2794855Sksadhukh 	cpi->cpi_family >= 0xf)
2804855Sksadhukh 
2811975Sdmick /*
2824481Sbholler  * Info for monitor/mwait idle loop.
2834481Sbholler  *
2844481Sbholler  * See cpuid section of "Intel 64 and IA-32 Architectures Software Developer's
2854481Sbholler  * Manual Volume 2A: Instruction Set Reference, A-M" #25366-022US, November
2864481Sbholler  * 2006.
2874481Sbholler  * See MONITOR/MWAIT section of "AMD64 Architecture Programmer's Manual
2884481Sbholler  * Documentation Updates" #33633, Rev 2.05, December 2006.
2894481Sbholler  */
2904481Sbholler #define	MWAIT_SUPPORT		(0x00000001)	/* mwait supported */
2914481Sbholler #define	MWAIT_EXTENSIONS	(0x00000002)	/* extenstion supported */
2924481Sbholler #define	MWAIT_ECX_INT_ENABLE	(0x00000004)	/* ecx 1 extension supported */
2934481Sbholler #define	MWAIT_SUPPORTED(cpi)	((cpi)->cpi_std[1].cp_ecx & CPUID_INTC_ECX_MON)
2944481Sbholler #define	MWAIT_INT_ENABLE(cpi)	((cpi)->cpi_std[5].cp_ecx & 0x2)
2954481Sbholler #define	MWAIT_EXTENSION(cpi)	((cpi)->cpi_std[5].cp_ecx & 0x1)
2964481Sbholler #define	MWAIT_SIZE_MIN(cpi)	BITX((cpi)->cpi_std[5].cp_eax, 15, 0)
2974481Sbholler #define	MWAIT_SIZE_MAX(cpi)	BITX((cpi)->cpi_std[5].cp_ebx, 15, 0)
2984481Sbholler /*
2994481Sbholler  * Number of sub-cstates for a given c-state.
3004481Sbholler  */
3014481Sbholler #define	MWAIT_NUM_SUBC_STATES(cpi, c_state)			\
3024481Sbholler 	BITX((cpi)->cpi_std[5].cp_edx, c_state + 3, c_state)
3034481Sbholler 
3047532SSean.Ye@Sun.COM /*
3057532SSean.Ye@Sun.COM  * Functions we consune from cpuid_subr.c;  don't publish these in a header
3067532SSean.Ye@Sun.COM  * file to try and keep people using the expected cpuid_* interfaces.
3077532SSean.Ye@Sun.COM  */
3087532SSean.Ye@Sun.COM extern uint32_t _cpuid_skt(uint_t, uint_t, uint_t, uint_t);
3097532SSean.Ye@Sun.COM extern uint32_t _cpuid_chiprev(uint_t, uint_t, uint_t, uint_t);
3107532SSean.Ye@Sun.COM extern const char *_cpuid_chiprevstr(uint_t, uint_t, uint_t, uint_t);
3117532SSean.Ye@Sun.COM extern uint_t _cpuid_vendorstr_to_vendorcode(char *);
3122869Sgavinm 
3132869Sgavinm /*
3143446Smrj  * Apply up various platform-dependent restrictions where the
3153446Smrj  * underlying platform restrictions mean the CPU can be marked
3163446Smrj  * as less capable than its cpuid instruction would imply.
3173446Smrj  */
3185084Sjohnlev #if defined(__xpv)
3195084Sjohnlev static void
3205084Sjohnlev platform_cpuid_mangle(uint_t vendor, uint32_t eax, struct cpuid_regs *cp)
3215084Sjohnlev {
3225084Sjohnlev 	switch (eax) {
3237532SSean.Ye@Sun.COM 	case 1: {
3247532SSean.Ye@Sun.COM 		uint32_t mcamask = DOMAIN_IS_INITDOMAIN(xen_info) ?
3257532SSean.Ye@Sun.COM 		    0 : CPUID_INTC_EDX_MCA;
3265084Sjohnlev 		cp->cp_edx &=
3277532SSean.Ye@Sun.COM 		    ~(mcamask |
3287532SSean.Ye@Sun.COM 		    CPUID_INTC_EDX_PSE |
3295084Sjohnlev 		    CPUID_INTC_EDX_VME | CPUID_INTC_EDX_DE |
3305084Sjohnlev 		    CPUID_INTC_EDX_SEP | CPUID_INTC_EDX_MTRR |
3315084Sjohnlev 		    CPUID_INTC_EDX_PGE | CPUID_INTC_EDX_PAT |
3325084Sjohnlev 		    CPUID_AMD_EDX_SYSC | CPUID_INTC_EDX_SEP |
3335084Sjohnlev 		    CPUID_INTC_EDX_PSE36 | CPUID_INTC_EDX_HTT);
3345084Sjohnlev 		break;
3357532SSean.Ye@Sun.COM 	}
3365084Sjohnlev 
3375084Sjohnlev 	case 0x80000001:
3385084Sjohnlev 		cp->cp_edx &=
3395084Sjohnlev 		    ~(CPUID_AMD_EDX_PSE |
3405084Sjohnlev 		    CPUID_INTC_EDX_VME | CPUID_INTC_EDX_DE |
3415084Sjohnlev 		    CPUID_AMD_EDX_MTRR | CPUID_AMD_EDX_PGE |
3425084Sjohnlev 		    CPUID_AMD_EDX_PAT | CPUID_AMD_EDX_PSE36 |
3435084Sjohnlev 		    CPUID_AMD_EDX_SYSC | CPUID_INTC_EDX_SEP |
3445084Sjohnlev 		    CPUID_AMD_EDX_TSCP);
3455084Sjohnlev 		cp->cp_ecx &= ~CPUID_AMD_ECX_CMP_LGCY;
3465084Sjohnlev 		break;
3475084Sjohnlev 	default:
3485084Sjohnlev 		break;
3495084Sjohnlev 	}
3505084Sjohnlev 
3515084Sjohnlev 	switch (vendor) {
3525084Sjohnlev 	case X86_VENDOR_Intel:
3535084Sjohnlev 		switch (eax) {
3545084Sjohnlev 		case 4:
3555084Sjohnlev 			/*
3565084Sjohnlev 			 * Zero out the (ncores-per-chip - 1) field
3575084Sjohnlev 			 */
3585084Sjohnlev 			cp->cp_eax &= 0x03fffffff;
3595084Sjohnlev 			break;
3605084Sjohnlev 		default:
3615084Sjohnlev 			break;
3625084Sjohnlev 		}
3635084Sjohnlev 		break;
3645084Sjohnlev 	case X86_VENDOR_AMD:
3655084Sjohnlev 		switch (eax) {
3665084Sjohnlev 		case 0x80000008:
3675084Sjohnlev 			/*
3685084Sjohnlev 			 * Zero out the (ncores-per-chip - 1) field
3695084Sjohnlev 			 */
3705084Sjohnlev 			cp->cp_ecx &= 0xffffff00;
3715084Sjohnlev 			break;
3725084Sjohnlev 		default:
3735084Sjohnlev 			break;
3745084Sjohnlev 		}
3755084Sjohnlev 		break;
3765084Sjohnlev 	default:
3775084Sjohnlev 		break;
3785084Sjohnlev 	}
3795084Sjohnlev }
3805084Sjohnlev #else
3813446Smrj #define	platform_cpuid_mangle(vendor, eax, cp)	/* nothing */
3825084Sjohnlev #endif
3833446Smrj 
3843446Smrj /*
3850Sstevel@tonic-gate  *  Some undocumented ways of patching the results of the cpuid
3860Sstevel@tonic-gate  *  instruction to permit running Solaris 10 on future cpus that
3870Sstevel@tonic-gate  *  we don't currently support.  Could be set to non-zero values
3880Sstevel@tonic-gate  *  via settings in eeprom.
3890Sstevel@tonic-gate  */
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate uint32_t cpuid_feature_ecx_include;
3920Sstevel@tonic-gate uint32_t cpuid_feature_ecx_exclude;
3930Sstevel@tonic-gate uint32_t cpuid_feature_edx_include;
3940Sstevel@tonic-gate uint32_t cpuid_feature_edx_exclude;
3950Sstevel@tonic-gate 
3963446Smrj void
3973446Smrj cpuid_alloc_space(cpu_t *cpu)
3983446Smrj {
3993446Smrj 	/*
4003446Smrj 	 * By convention, cpu0 is the boot cpu, which is set up
4013446Smrj 	 * before memory allocation is available.  All other cpus get
4023446Smrj 	 * their cpuid_info struct allocated here.
4033446Smrj 	 */
4043446Smrj 	ASSERT(cpu->cpu_id != 0);
4053446Smrj 	cpu->cpu_m.mcpu_cpi =
4063446Smrj 	    kmem_zalloc(sizeof (*cpu->cpu_m.mcpu_cpi), KM_SLEEP);
4073446Smrj }
4083446Smrj 
4093446Smrj void
4103446Smrj cpuid_free_space(cpu_t *cpu)
4113446Smrj {
4124606Sesaxe 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
4134606Sesaxe 	int i;
4144606Sesaxe 
4153446Smrj 	ASSERT(cpu->cpu_id != 0);
4164606Sesaxe 
4174606Sesaxe 	/*
4184606Sesaxe 	 * Free up any function 4 related dynamic storage
4194606Sesaxe 	 */
4204606Sesaxe 	for (i = 1; i < cpi->cpi_std_4_size; i++)
4214606Sesaxe 		kmem_free(cpi->cpi_std_4[i], sizeof (struct cpuid_regs));
4224606Sesaxe 	if (cpi->cpi_std_4_size > 0)
4234606Sesaxe 		kmem_free(cpi->cpi_std_4,
4244606Sesaxe 		    cpi->cpi_std_4_size * sizeof (struct cpuid_regs *));
4254606Sesaxe 
4263446Smrj 	kmem_free(cpu->cpu_m.mcpu_cpi, sizeof (*cpu->cpu_m.mcpu_cpi));
4273446Smrj }
4283446Smrj 
4295741Smrj #if !defined(__xpv)
4305741Smrj 
4315741Smrj static void
4325741Smrj check_for_hvm()
4335741Smrj {
4345741Smrj 	struct cpuid_regs cp;
4355741Smrj 	char *xen_str;
4365741Smrj 	uint32_t xen_signature[4];
4375741Smrj 	extern int xpv_is_hvm;
4385741Smrj 
4395741Smrj 	/*
4405741Smrj 	 * In a fully virtualized domain, Xen's pseudo-cpuid function
4415741Smrj 	 * 0x40000000 returns a string representing the Xen signature in
4425741Smrj 	 * %ebx, %ecx, and %edx.  %eax contains the maximum supported cpuid
4435741Smrj 	 * function.
4445741Smrj 	 */
4455741Smrj 	cp.cp_eax = 0x40000000;
4465741Smrj 	(void) __cpuid_insn(&cp);
4475741Smrj 	xen_signature[0] = cp.cp_ebx;
4485741Smrj 	xen_signature[1] = cp.cp_ecx;
4495741Smrj 	xen_signature[2] = cp.cp_edx;
4505741Smrj 	xen_signature[3] = 0;
4515741Smrj 	xen_str = (char *)xen_signature;
4525741Smrj 	if (strcmp("XenVMMXenVMM", xen_str) == 0 && cp.cp_eax <= 0x40000002)
4535741Smrj 		xpv_is_hvm = 1;
454*8990SSurya.Prakki@Sun.COM 
455*8990SSurya.Prakki@Sun.COM 	/* could we be running under vmware hypervisor */
456*8990SSurya.Prakki@Sun.COM 	if (xpv_is_hvm || vmware_platform())
457*8990SSurya.Prakki@Sun.COM 		platform_is_virt = 1;
4585741Smrj }
4595741Smrj #endif	/* __xpv */
4605741Smrj 
4610Sstevel@tonic-gate uint_t
4620Sstevel@tonic-gate cpuid_pass1(cpu_t *cpu)
4630Sstevel@tonic-gate {
4640Sstevel@tonic-gate 	uint32_t mask_ecx, mask_edx;
4650Sstevel@tonic-gate 	uint_t feature = X86_CPUID;
4660Sstevel@tonic-gate 	struct cpuid_info *cpi;
4671228Sandrei 	struct cpuid_regs *cp;
4680Sstevel@tonic-gate 	int xcpuid;
4695084Sjohnlev #if !defined(__xpv)
4705045Sbholler 	extern int idle_cpu_prefer_mwait;
4715084Sjohnlev #endif
4723446Smrj 
4730Sstevel@tonic-gate 	/*
4743446Smrj 	 * Space statically allocated for cpu0, ensure pointer is set
4750Sstevel@tonic-gate 	 */
4760Sstevel@tonic-gate 	if (cpu->cpu_id == 0)
4773446Smrj 		cpu->cpu_m.mcpu_cpi = &cpuid_info0;
4783446Smrj 	cpi = cpu->cpu_m.mcpu_cpi;
4793446Smrj 	ASSERT(cpi != NULL);
4800Sstevel@tonic-gate 	cp = &cpi->cpi_std[0];
4811228Sandrei 	cp->cp_eax = 0;
4821228Sandrei 	cpi->cpi_maxeax = __cpuid_insn(cp);
4830Sstevel@tonic-gate 	{
4840Sstevel@tonic-gate 		uint32_t *iptr = (uint32_t *)cpi->cpi_vendorstr;
4850Sstevel@tonic-gate 		*iptr++ = cp->cp_ebx;
4860Sstevel@tonic-gate 		*iptr++ = cp->cp_edx;
4870Sstevel@tonic-gate 		*iptr++ = cp->cp_ecx;
4880Sstevel@tonic-gate 		*(char *)&cpi->cpi_vendorstr[12] = '\0';
4890Sstevel@tonic-gate 	}
4900Sstevel@tonic-gate 
4917532SSean.Ye@Sun.COM 	cpi->cpi_vendor = _cpuid_vendorstr_to_vendorcode(cpi->cpi_vendorstr);
4920Sstevel@tonic-gate 	x86_vendor = cpi->cpi_vendor; /* for compatibility */
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	/*
4950Sstevel@tonic-gate 	 * Limit the range in case of weird hardware
4960Sstevel@tonic-gate 	 */
4970Sstevel@tonic-gate 	if (cpi->cpi_maxeax > CPI_MAXEAX_MAX)
4980Sstevel@tonic-gate 		cpi->cpi_maxeax = CPI_MAXEAX_MAX;
4990Sstevel@tonic-gate 	if (cpi->cpi_maxeax < 1)
5000Sstevel@tonic-gate 		goto pass1_done;
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	cp = &cpi->cpi_std[1];
5031228Sandrei 	cp->cp_eax = 1;
5041228Sandrei 	(void) __cpuid_insn(cp);
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 	/*
5070Sstevel@tonic-gate 	 * Extract identifying constants for easy access.
5080Sstevel@tonic-gate 	 */
5090Sstevel@tonic-gate 	cpi->cpi_model = CPI_MODEL(cpi);
5100Sstevel@tonic-gate 	cpi->cpi_family = CPI_FAMILY(cpi);
5110Sstevel@tonic-gate 
5121975Sdmick 	if (cpi->cpi_family == 0xf)
5130Sstevel@tonic-gate 		cpi->cpi_family += CPI_FAMILY_XTD(cpi);
5141975Sdmick 
5152001Sdmick 	/*
5164265Skchow 	 * Beware: AMD uses "extended model" iff base *FAMILY* == 0xf.
5172001Sdmick 	 * Intel, and presumably everyone else, uses model == 0xf, as
5182001Sdmick 	 * one would expect (max value means possible overflow).  Sigh.
5192001Sdmick 	 */
5202001Sdmick 
5212001Sdmick 	switch (cpi->cpi_vendor) {
5224855Sksadhukh 	case X86_VENDOR_Intel:
5234855Sksadhukh 		if (IS_EXTENDED_MODEL_INTEL(cpi))
5244855Sksadhukh 			cpi->cpi_model += CPI_MODEL_XTD(cpi) << 4;
5254858Sksadhukh 		break;
5262001Sdmick 	case X86_VENDOR_AMD:
5274265Skchow 		if (CPI_FAMILY(cpi) == 0xf)
5282001Sdmick 			cpi->cpi_model += CPI_MODEL_XTD(cpi) << 4;
5292001Sdmick 		break;
5302001Sdmick 	default:
5312001Sdmick 		if (cpi->cpi_model == 0xf)
5322001Sdmick 			cpi->cpi_model += CPI_MODEL_XTD(cpi) << 4;
5332001Sdmick 		break;
5342001Sdmick 	}
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 	cpi->cpi_step = CPI_STEP(cpi);
5370Sstevel@tonic-gate 	cpi->cpi_brandid = CPI_BRANDID(cpi);
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	/*
5400Sstevel@tonic-gate 	 * *default* assumptions:
5410Sstevel@tonic-gate 	 * - believe %edx feature word
5420Sstevel@tonic-gate 	 * - ignore %ecx feature word
5430Sstevel@tonic-gate 	 * - 32-bit virtual and physical addressing
5440Sstevel@tonic-gate 	 */
5450Sstevel@tonic-gate 	mask_edx = 0xffffffff;
5460Sstevel@tonic-gate 	mask_ecx = 0;
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	cpi->cpi_pabits = cpi->cpi_vabits = 32;
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
5510Sstevel@tonic-gate 	case X86_VENDOR_Intel:
5520Sstevel@tonic-gate 		if (cpi->cpi_family == 5)
5530Sstevel@tonic-gate 			x86_type = X86_TYPE_P5;
5541975Sdmick 		else if (IS_LEGACY_P6(cpi)) {
5550Sstevel@tonic-gate 			x86_type = X86_TYPE_P6;
5560Sstevel@tonic-gate 			pentiumpro_bug4046376 = 1;
5570Sstevel@tonic-gate 			pentiumpro_bug4064495 = 1;
5580Sstevel@tonic-gate 			/*
5590Sstevel@tonic-gate 			 * Clear the SEP bit when it was set erroneously
5600Sstevel@tonic-gate 			 */
5610Sstevel@tonic-gate 			if (cpi->cpi_model < 3 && cpi->cpi_step < 3)
5620Sstevel@tonic-gate 				cp->cp_edx &= ~CPUID_INTC_EDX_SEP;
5631975Sdmick 		} else if (IS_NEW_F6(cpi) || cpi->cpi_family == 0xf) {
5640Sstevel@tonic-gate 			x86_type = X86_TYPE_P4;
5650Sstevel@tonic-gate 			/*
5660Sstevel@tonic-gate 			 * We don't currently depend on any of the %ecx
5670Sstevel@tonic-gate 			 * features until Prescott, so we'll only check
5680Sstevel@tonic-gate 			 * this from P4 onwards.  We might want to revisit
5690Sstevel@tonic-gate 			 * that idea later.
5700Sstevel@tonic-gate 			 */
5710Sstevel@tonic-gate 			mask_ecx = 0xffffffff;
5720Sstevel@tonic-gate 		} else if (cpi->cpi_family > 0xf)
5730Sstevel@tonic-gate 			mask_ecx = 0xffffffff;
5744636Sbholler 		/*
5754636Sbholler 		 * We don't support MONITOR/MWAIT if leaf 5 is not available
5764636Sbholler 		 * to obtain the monitor linesize.
5774636Sbholler 		 */
5784636Sbholler 		if (cpi->cpi_maxeax < 5)
5794636Sbholler 			mask_ecx &= ~CPUID_INTC_ECX_MON;
5800Sstevel@tonic-gate 		break;
5810Sstevel@tonic-gate 	case X86_VENDOR_IntelClone:
5820Sstevel@tonic-gate 	default:
5830Sstevel@tonic-gate 		break;
5840Sstevel@tonic-gate 	case X86_VENDOR_AMD:
5850Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_108)
5860Sstevel@tonic-gate 		if (cpi->cpi_family == 0xf && cpi->cpi_model == 0xe) {
5870Sstevel@tonic-gate 			cp->cp_eax = (0xf0f & cp->cp_eax) | 0xc0;
5880Sstevel@tonic-gate 			cpi->cpi_model = 0xc;
5890Sstevel@tonic-gate 		} else
5900Sstevel@tonic-gate #endif
5910Sstevel@tonic-gate 		if (cpi->cpi_family == 5) {
5920Sstevel@tonic-gate 			/*
5930Sstevel@tonic-gate 			 * AMD K5 and K6
5940Sstevel@tonic-gate 			 *
5950Sstevel@tonic-gate 			 * These CPUs have an incomplete implementation
5960Sstevel@tonic-gate 			 * of MCA/MCE which we mask away.
5970Sstevel@tonic-gate 			 */
5981228Sandrei 			mask_edx &= ~(CPUID_INTC_EDX_MCE | CPUID_INTC_EDX_MCA);
5991228Sandrei 
6001228Sandrei 			/*
6011228Sandrei 			 * Model 0 uses the wrong (APIC) bit
6021228Sandrei 			 * to indicate PGE.  Fix it here.
6031228Sandrei 			 */
6040Sstevel@tonic-gate 			if (cpi->cpi_model == 0) {
6050Sstevel@tonic-gate 				if (cp->cp_edx & 0x200) {
6060Sstevel@tonic-gate 					cp->cp_edx &= ~0x200;
6070Sstevel@tonic-gate 					cp->cp_edx |= CPUID_INTC_EDX_PGE;
6080Sstevel@tonic-gate 				}
6091228Sandrei 			}
6101228Sandrei 
6111228Sandrei 			/*
6121228Sandrei 			 * Early models had problems w/ MMX; disable.
6131228Sandrei 			 */
6141228Sandrei 			if (cpi->cpi_model < 6)
6151228Sandrei 				mask_edx &= ~CPUID_INTC_EDX_MMX;
6161228Sandrei 		}
6171228Sandrei 
6181228Sandrei 		/*
6191228Sandrei 		 * For newer families, SSE3 and CX16, at least, are valid;
6201228Sandrei 		 * enable all
6211228Sandrei 		 */
6221228Sandrei 		if (cpi->cpi_family >= 0xf)
623771Sdmick 			mask_ecx = 0xffffffff;
6244636Sbholler 		/*
6254636Sbholler 		 * We don't support MONITOR/MWAIT if leaf 5 is not available
6264636Sbholler 		 * to obtain the monitor linesize.
6274636Sbholler 		 */
6284636Sbholler 		if (cpi->cpi_maxeax < 5)
6294636Sbholler 			mask_ecx &= ~CPUID_INTC_ECX_MON;
6305045Sbholler 
6315084Sjohnlev #if !defined(__xpv)
6325045Sbholler 		/*
6335045Sbholler 		 * Do not use MONITOR/MWAIT to halt in the idle loop on any AMD
6345045Sbholler 		 * processors.  AMD does not intend MWAIT to be used in the cpu
6355045Sbholler 		 * idle loop on current and future processors.  10h and future
6365045Sbholler 		 * AMD processors use more power in MWAIT than HLT.
6375045Sbholler 		 * Pre-family-10h Opterons do not have the MWAIT instruction.
6385045Sbholler 		 */
6395045Sbholler 		idle_cpu_prefer_mwait = 0;
6405084Sjohnlev #endif
6415045Sbholler 
6420Sstevel@tonic-gate 		break;
6430Sstevel@tonic-gate 	case X86_VENDOR_TM:
6440Sstevel@tonic-gate 		/*
6450Sstevel@tonic-gate 		 * workaround the NT workaround in CMS 4.1
6460Sstevel@tonic-gate 		 */
6470Sstevel@tonic-gate 		if (cpi->cpi_family == 5 && cpi->cpi_model == 4 &&
6480Sstevel@tonic-gate 		    (cpi->cpi_step == 2 || cpi->cpi_step == 3))
6490Sstevel@tonic-gate 			cp->cp_edx |= CPUID_INTC_EDX_CX8;
6500Sstevel@tonic-gate 		break;
6510Sstevel@tonic-gate 	case X86_VENDOR_Centaur:
6520Sstevel@tonic-gate 		/*
6530Sstevel@tonic-gate 		 * workaround the NT workarounds again
6540Sstevel@tonic-gate 		 */
6550Sstevel@tonic-gate 		if (cpi->cpi_family == 6)
6560Sstevel@tonic-gate 			cp->cp_edx |= CPUID_INTC_EDX_CX8;
6570Sstevel@tonic-gate 		break;
6580Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
6590Sstevel@tonic-gate 		/*
6600Sstevel@tonic-gate 		 * We rely heavily on the probing in locore
6610Sstevel@tonic-gate 		 * to actually figure out what parts, if any,
6620Sstevel@tonic-gate 		 * of the Cyrix cpuid instruction to believe.
6630Sstevel@tonic-gate 		 */
6640Sstevel@tonic-gate 		switch (x86_type) {
6650Sstevel@tonic-gate 		case X86_TYPE_CYRIX_486:
6660Sstevel@tonic-gate 			mask_edx = 0;
6670Sstevel@tonic-gate 			break;
6680Sstevel@tonic-gate 		case X86_TYPE_CYRIX_6x86:
6690Sstevel@tonic-gate 			mask_edx = 0;
6700Sstevel@tonic-gate 			break;
6710Sstevel@tonic-gate 		case X86_TYPE_CYRIX_6x86L:
6720Sstevel@tonic-gate 			mask_edx =
6730Sstevel@tonic-gate 			    CPUID_INTC_EDX_DE |
6740Sstevel@tonic-gate 			    CPUID_INTC_EDX_CX8;
6750Sstevel@tonic-gate 			break;
6760Sstevel@tonic-gate 		case X86_TYPE_CYRIX_6x86MX:
6770Sstevel@tonic-gate 			mask_edx =
6780Sstevel@tonic-gate 			    CPUID_INTC_EDX_DE |
6790Sstevel@tonic-gate 			    CPUID_INTC_EDX_MSR |
6800Sstevel@tonic-gate 			    CPUID_INTC_EDX_CX8 |
6810Sstevel@tonic-gate 			    CPUID_INTC_EDX_PGE |
6820Sstevel@tonic-gate 			    CPUID_INTC_EDX_CMOV |
6830Sstevel@tonic-gate 			    CPUID_INTC_EDX_MMX;
6840Sstevel@tonic-gate 			break;
6850Sstevel@tonic-gate 		case X86_TYPE_CYRIX_GXm:
6860Sstevel@tonic-gate 			mask_edx =
6870Sstevel@tonic-gate 			    CPUID_INTC_EDX_MSR |
6880Sstevel@tonic-gate 			    CPUID_INTC_EDX_CX8 |
6890Sstevel@tonic-gate 			    CPUID_INTC_EDX_CMOV |
6900Sstevel@tonic-gate 			    CPUID_INTC_EDX_MMX;
6910Sstevel@tonic-gate 			break;
6920Sstevel@tonic-gate 		case X86_TYPE_CYRIX_MediaGX:
6930Sstevel@tonic-gate 			break;
6940Sstevel@tonic-gate 		case X86_TYPE_CYRIX_MII:
6950Sstevel@tonic-gate 		case X86_TYPE_VIA_CYRIX_III:
6960Sstevel@tonic-gate 			mask_edx =
6970Sstevel@tonic-gate 			    CPUID_INTC_EDX_DE |
6980Sstevel@tonic-gate 			    CPUID_INTC_EDX_TSC |
6990Sstevel@tonic-gate 			    CPUID_INTC_EDX_MSR |
7000Sstevel@tonic-gate 			    CPUID_INTC_EDX_CX8 |
7010Sstevel@tonic-gate 			    CPUID_INTC_EDX_PGE |
7020Sstevel@tonic-gate 			    CPUID_INTC_EDX_CMOV |
7030Sstevel@tonic-gate 			    CPUID_INTC_EDX_MMX;
7040Sstevel@tonic-gate 			break;
7050Sstevel@tonic-gate 		default:
7060Sstevel@tonic-gate 			break;
7070Sstevel@tonic-gate 		}
7080Sstevel@tonic-gate 		break;
7090Sstevel@tonic-gate 	}
7100Sstevel@tonic-gate 
7115084Sjohnlev #if defined(__xpv)
7125084Sjohnlev 	/*
7135084Sjohnlev 	 * Do not support MONITOR/MWAIT under a hypervisor
7145084Sjohnlev 	 */
7155084Sjohnlev 	mask_ecx &= ~CPUID_INTC_ECX_MON;
7165084Sjohnlev #endif	/* __xpv */
7175084Sjohnlev 
7180Sstevel@tonic-gate 	/*
7190Sstevel@tonic-gate 	 * Now we've figured out the masks that determine
7200Sstevel@tonic-gate 	 * which bits we choose to believe, apply the masks
7210Sstevel@tonic-gate 	 * to the feature words, then map the kernel's view
7220Sstevel@tonic-gate 	 * of these feature words into its feature word.
7230Sstevel@tonic-gate 	 */
7240Sstevel@tonic-gate 	cp->cp_edx &= mask_edx;
7250Sstevel@tonic-gate 	cp->cp_ecx &= mask_ecx;
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 	/*
7283446Smrj 	 * apply any platform restrictions (we don't call this
7293446Smrj 	 * immediately after __cpuid_insn here, because we need the
7303446Smrj 	 * workarounds applied above first)
7310Sstevel@tonic-gate 	 */
7323446Smrj 	platform_cpuid_mangle(cpi->cpi_vendor, 1, cp);
7330Sstevel@tonic-gate 
7343446Smrj 	/*
7353446Smrj 	 * fold in overrides from the "eeprom" mechanism
7363446Smrj 	 */
7370Sstevel@tonic-gate 	cp->cp_edx |= cpuid_feature_edx_include;
7380Sstevel@tonic-gate 	cp->cp_edx &= ~cpuid_feature_edx_exclude;
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 	cp->cp_ecx |= cpuid_feature_ecx_include;
7410Sstevel@tonic-gate 	cp->cp_ecx &= ~cpuid_feature_ecx_exclude;
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_PSE)
7440Sstevel@tonic-gate 		feature |= X86_LARGEPAGE;
7450Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_TSC)
7460Sstevel@tonic-gate 		feature |= X86_TSC;
7470Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_MSR)
7480Sstevel@tonic-gate 		feature |= X86_MSR;
7490Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_MTRR)
7500Sstevel@tonic-gate 		feature |= X86_MTRR;
7510Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_PGE)
7520Sstevel@tonic-gate 		feature |= X86_PGE;
7530Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_CMOV)
7540Sstevel@tonic-gate 		feature |= X86_CMOV;
7550Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_MMX)
7560Sstevel@tonic-gate 		feature |= X86_MMX;
7570Sstevel@tonic-gate 	if ((cp->cp_edx & CPUID_INTC_EDX_MCE) != 0 &&
7580Sstevel@tonic-gate 	    (cp->cp_edx & CPUID_INTC_EDX_MCA) != 0)
7590Sstevel@tonic-gate 		feature |= X86_MCA;
7600Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_PAE)
7610Sstevel@tonic-gate 		feature |= X86_PAE;
7620Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_CX8)
7630Sstevel@tonic-gate 		feature |= X86_CX8;
7640Sstevel@tonic-gate 	if (cp->cp_ecx & CPUID_INTC_ECX_CX16)
7650Sstevel@tonic-gate 		feature |= X86_CX16;
7660Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_PAT)
7670Sstevel@tonic-gate 		feature |= X86_PAT;
7680Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_SEP)
7690Sstevel@tonic-gate 		feature |= X86_SEP;
7700Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_FXSR) {
7710Sstevel@tonic-gate 		/*
7720Sstevel@tonic-gate 		 * In our implementation, fxsave/fxrstor
7730Sstevel@tonic-gate 		 * are prerequisites before we'll even
7740Sstevel@tonic-gate 		 * try and do SSE things.
7750Sstevel@tonic-gate 		 */
7760Sstevel@tonic-gate 		if (cp->cp_edx & CPUID_INTC_EDX_SSE)
7770Sstevel@tonic-gate 			feature |= X86_SSE;
7780Sstevel@tonic-gate 		if (cp->cp_edx & CPUID_INTC_EDX_SSE2)
7790Sstevel@tonic-gate 			feature |= X86_SSE2;
7800Sstevel@tonic-gate 		if (cp->cp_ecx & CPUID_INTC_ECX_SSE3)
7810Sstevel@tonic-gate 			feature |= X86_SSE3;
7825269Skk208521 		if (cpi->cpi_vendor == X86_VENDOR_Intel) {
7835269Skk208521 			if (cp->cp_ecx & CPUID_INTC_ECX_SSSE3)
7845269Skk208521 				feature |= X86_SSSE3;
7855269Skk208521 			if (cp->cp_ecx & CPUID_INTC_ECX_SSE4_1)
7865269Skk208521 				feature |= X86_SSE4_1;
7875269Skk208521 			if (cp->cp_ecx & CPUID_INTC_ECX_SSE4_2)
7885269Skk208521 				feature |= X86_SSE4_2;
7895269Skk208521 		}
7900Sstevel@tonic-gate 	}
7910Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_DE)
7923446Smrj 		feature |= X86_DE;
7937716SBill.Holler@Sun.COM #if !defined(__xpv)
7944481Sbholler 	if (cp->cp_ecx & CPUID_INTC_ECX_MON) {
7957716SBill.Holler@Sun.COM 
7967716SBill.Holler@Sun.COM 		/*
7977716SBill.Holler@Sun.COM 		 * We require the CLFLUSH instruction for erratum workaround
7987716SBill.Holler@Sun.COM 		 * to use MONITOR/MWAIT.
7997716SBill.Holler@Sun.COM 		 */
8007716SBill.Holler@Sun.COM 		if (cp->cp_edx & CPUID_INTC_EDX_CLFSH) {
8017716SBill.Holler@Sun.COM 			cpi->cpi_mwait.support |= MWAIT_SUPPORT;
8027716SBill.Holler@Sun.COM 			feature |= X86_MWAIT;
8037716SBill.Holler@Sun.COM 		} else {
8047716SBill.Holler@Sun.COM 			extern int idle_cpu_assert_cflush_monitor;
8057716SBill.Holler@Sun.COM 
8067716SBill.Holler@Sun.COM 			/*
8077716SBill.Holler@Sun.COM 			 * All processors we are aware of which have
8087716SBill.Holler@Sun.COM 			 * MONITOR/MWAIT also have CLFLUSH.
8097716SBill.Holler@Sun.COM 			 */
8107716SBill.Holler@Sun.COM 			if (idle_cpu_assert_cflush_monitor) {
8117716SBill.Holler@Sun.COM 				ASSERT((cp->cp_ecx & CPUID_INTC_ECX_MON) &&
8127716SBill.Holler@Sun.COM 				    (cp->cp_edx & CPUID_INTC_EDX_CLFSH));
8137716SBill.Holler@Sun.COM 			}
8147716SBill.Holler@Sun.COM 		}
8154481Sbholler 	}
8167716SBill.Holler@Sun.COM #endif	/* __xpv */
8170Sstevel@tonic-gate 
8187589SVikram.Hegde@Sun.COM 	/*
8197589SVikram.Hegde@Sun.COM 	 * Only need it first time, rest of the cpus would follow suite.
8207589SVikram.Hegde@Sun.COM 	 * we only capture this for the bootcpu.
8217589SVikram.Hegde@Sun.COM 	 */
8227589SVikram.Hegde@Sun.COM 	if (cp->cp_edx & CPUID_INTC_EDX_CLFSH) {
8237589SVikram.Hegde@Sun.COM 		feature |= X86_CLFSH;
8247589SVikram.Hegde@Sun.COM 		x86_clflush_size = (BITX(cp->cp_ebx, 15, 8) * 8);
8257589SVikram.Hegde@Sun.COM 	}
8267589SVikram.Hegde@Sun.COM 
8270Sstevel@tonic-gate 	if (feature & X86_PAE)
8280Sstevel@tonic-gate 		cpi->cpi_pabits = 36;
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 	/*
8310Sstevel@tonic-gate 	 * Hyperthreading configuration is slightly tricky on Intel
8320Sstevel@tonic-gate 	 * and pure clones, and even trickier on AMD.
8330Sstevel@tonic-gate 	 *
8340Sstevel@tonic-gate 	 * (AMD chose to set the HTT bit on their CMP processors,
8350Sstevel@tonic-gate 	 * even though they're not actually hyperthreaded.  Thus it
8360Sstevel@tonic-gate 	 * takes a bit more work to figure out what's really going
8373446Smrj 	 * on ... see the handling of the CMP_LGCY bit below)
8380Sstevel@tonic-gate 	 */
8390Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_HTT) {
8400Sstevel@tonic-gate 		cpi->cpi_ncpu_per_chip = CPI_CPU_COUNT(cpi);
8410Sstevel@tonic-gate 		if (cpi->cpi_ncpu_per_chip > 1)
8420Sstevel@tonic-gate 			feature |= X86_HTT;
8431228Sandrei 	} else {
8441228Sandrei 		cpi->cpi_ncpu_per_chip = 1;
8450Sstevel@tonic-gate 	}
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate 	/*
8480Sstevel@tonic-gate 	 * Work on the "extended" feature information, doing
8490Sstevel@tonic-gate 	 * some basic initialization for cpuid_pass2()
8500Sstevel@tonic-gate 	 */
8510Sstevel@tonic-gate 	xcpuid = 0;
8520Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
8530Sstevel@tonic-gate 	case X86_VENDOR_Intel:
8541975Sdmick 		if (IS_NEW_F6(cpi) || cpi->cpi_family >= 0xf)
8550Sstevel@tonic-gate 			xcpuid++;
8560Sstevel@tonic-gate 		break;
8570Sstevel@tonic-gate 	case X86_VENDOR_AMD:
8580Sstevel@tonic-gate 		if (cpi->cpi_family > 5 ||
8590Sstevel@tonic-gate 		    (cpi->cpi_family == 5 && cpi->cpi_model >= 1))
8600Sstevel@tonic-gate 			xcpuid++;
8610Sstevel@tonic-gate 		break;
8620Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
8630Sstevel@tonic-gate 		/*
8640Sstevel@tonic-gate 		 * Only these Cyrix CPUs are -known- to support
8650Sstevel@tonic-gate 		 * extended cpuid operations.
8660Sstevel@tonic-gate 		 */
8670Sstevel@tonic-gate 		if (x86_type == X86_TYPE_VIA_CYRIX_III ||
8680Sstevel@tonic-gate 		    x86_type == X86_TYPE_CYRIX_GXm)
8690Sstevel@tonic-gate 			xcpuid++;
8700Sstevel@tonic-gate 		break;
8710Sstevel@tonic-gate 	case X86_VENDOR_Centaur:
8720Sstevel@tonic-gate 	case X86_VENDOR_TM:
8730Sstevel@tonic-gate 	default:
8740Sstevel@tonic-gate 		xcpuid++;
8750Sstevel@tonic-gate 		break;
8760Sstevel@tonic-gate 	}
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 	if (xcpuid) {
8790Sstevel@tonic-gate 		cp = &cpi->cpi_extd[0];
8801228Sandrei 		cp->cp_eax = 0x80000000;
8811228Sandrei 		cpi->cpi_xmaxeax = __cpuid_insn(cp);
8820Sstevel@tonic-gate 	}
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 	if (cpi->cpi_xmaxeax & 0x80000000) {
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 		if (cpi->cpi_xmaxeax > CPI_XMAXEAX_MAX)
8870Sstevel@tonic-gate 			cpi->cpi_xmaxeax = CPI_XMAXEAX_MAX;
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 		switch (cpi->cpi_vendor) {
8900Sstevel@tonic-gate 		case X86_VENDOR_Intel:
8910Sstevel@tonic-gate 		case X86_VENDOR_AMD:
8920Sstevel@tonic-gate 			if (cpi->cpi_xmaxeax < 0x80000001)
8930Sstevel@tonic-gate 				break;
8940Sstevel@tonic-gate 			cp = &cpi->cpi_extd[1];
8951228Sandrei 			cp->cp_eax = 0x80000001;
8961228Sandrei 			(void) __cpuid_insn(cp);
8973446Smrj 
8980Sstevel@tonic-gate 			if (cpi->cpi_vendor == X86_VENDOR_AMD &&
8990Sstevel@tonic-gate 			    cpi->cpi_family == 5 &&
9000Sstevel@tonic-gate 			    cpi->cpi_model == 6 &&
9010Sstevel@tonic-gate 			    cpi->cpi_step == 6) {
9020Sstevel@tonic-gate 				/*
9030Sstevel@tonic-gate 				 * K6 model 6 uses bit 10 to indicate SYSC
9040Sstevel@tonic-gate 				 * Later models use bit 11. Fix it here.
9050Sstevel@tonic-gate 				 */
9060Sstevel@tonic-gate 				if (cp->cp_edx & 0x400) {
9070Sstevel@tonic-gate 					cp->cp_edx &= ~0x400;
9080Sstevel@tonic-gate 					cp->cp_edx |= CPUID_AMD_EDX_SYSC;
9090Sstevel@tonic-gate 				}
9100Sstevel@tonic-gate 			}
9110Sstevel@tonic-gate 
9123446Smrj 			platform_cpuid_mangle(cpi->cpi_vendor, 0x80000001, cp);
9133446Smrj 
9140Sstevel@tonic-gate 			/*
9150Sstevel@tonic-gate 			 * Compute the additions to the kernel's feature word.
9160Sstevel@tonic-gate 			 */
9170Sstevel@tonic-gate 			if (cp->cp_edx & CPUID_AMD_EDX_NX)
9180Sstevel@tonic-gate 				feature |= X86_NX;
9190Sstevel@tonic-gate 
9207656SSherry.Moore@Sun.COM 			/*
9217656SSherry.Moore@Sun.COM 			 * Regardless whether or not we boot 64-bit,
9227656SSherry.Moore@Sun.COM 			 * we should have a way to identify whether
9237656SSherry.Moore@Sun.COM 			 * the CPU is capable of running 64-bit.
9247656SSherry.Moore@Sun.COM 			 */
9257656SSherry.Moore@Sun.COM 			if (cp->cp_edx & CPUID_AMD_EDX_LM)
9267656SSherry.Moore@Sun.COM 				feature |= X86_64;
9277656SSherry.Moore@Sun.COM 
9285349Skchow #if defined(__amd64)
9295349Skchow 			/* 1 GB large page - enable only for 64 bit kernel */
9305349Skchow 			if (cp->cp_edx & CPUID_AMD_EDX_1GPG)
9315349Skchow 				feature |= X86_1GPG;
9325349Skchow #endif
9335349Skchow 
9344628Skk208521 			if ((cpi->cpi_vendor == X86_VENDOR_AMD) &&
9354628Skk208521 			    (cpi->cpi_std[1].cp_edx & CPUID_INTC_EDX_FXSR) &&
9364628Skk208521 			    (cp->cp_ecx & CPUID_AMD_ECX_SSE4A))
9374628Skk208521 				feature |= X86_SSE4A;
9384628Skk208521 
9390Sstevel@tonic-gate 			/*
9403446Smrj 			 * If both the HTT and CMP_LGCY bits are set,
9411228Sandrei 			 * then we're not actually HyperThreaded.  Read
9421228Sandrei 			 * "AMD CPUID Specification" for more details.
9430Sstevel@tonic-gate 			 */
9440Sstevel@tonic-gate 			if (cpi->cpi_vendor == X86_VENDOR_AMD &&
9451228Sandrei 			    (feature & X86_HTT) &&
9463446Smrj 			    (cp->cp_ecx & CPUID_AMD_ECX_CMP_LGCY)) {
9470Sstevel@tonic-gate 				feature &= ~X86_HTT;
9481228Sandrei 				feature |= X86_CMP;
9491228Sandrei 			}
9503446Smrj #if defined(__amd64)
9510Sstevel@tonic-gate 			/*
9520Sstevel@tonic-gate 			 * It's really tricky to support syscall/sysret in
9530Sstevel@tonic-gate 			 * the i386 kernel; we rely on sysenter/sysexit
9540Sstevel@tonic-gate 			 * instead.  In the amd64 kernel, things are -way-
9550Sstevel@tonic-gate 			 * better.
9560Sstevel@tonic-gate 			 */
9570Sstevel@tonic-gate 			if (cp->cp_edx & CPUID_AMD_EDX_SYSC)
9580Sstevel@tonic-gate 				feature |= X86_ASYSC;
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 			/*
9610Sstevel@tonic-gate 			 * While we're thinking about system calls, note
9620Sstevel@tonic-gate 			 * that AMD processors don't support sysenter
9630Sstevel@tonic-gate 			 * in long mode at all, so don't try to program them.
9640Sstevel@tonic-gate 			 */
9650Sstevel@tonic-gate 			if (x86_vendor == X86_VENDOR_AMD)
9660Sstevel@tonic-gate 				feature &= ~X86_SEP;
9670Sstevel@tonic-gate #endif
9686657Ssudheer 			if (cp->cp_edx & CPUID_AMD_EDX_TSCP)
9693446Smrj 				feature |= X86_TSCP;
9700Sstevel@tonic-gate 			break;
9710Sstevel@tonic-gate 		default:
9720Sstevel@tonic-gate 			break;
9730Sstevel@tonic-gate 		}
9740Sstevel@tonic-gate 
9751228Sandrei 		/*
9761228Sandrei 		 * Get CPUID data about processor cores and hyperthreads.
9771228Sandrei 		 */
9780Sstevel@tonic-gate 		switch (cpi->cpi_vendor) {
9790Sstevel@tonic-gate 		case X86_VENDOR_Intel:
9801228Sandrei 			if (cpi->cpi_maxeax >= 4) {
9811228Sandrei 				cp = &cpi->cpi_std[4];
9821228Sandrei 				cp->cp_eax = 4;
9831228Sandrei 				cp->cp_ecx = 0;
9841228Sandrei 				(void) __cpuid_insn(cp);
9853446Smrj 				platform_cpuid_mangle(cpi->cpi_vendor, 4, cp);
9861228Sandrei 			}
9871228Sandrei 			/*FALLTHROUGH*/
9880Sstevel@tonic-gate 		case X86_VENDOR_AMD:
9890Sstevel@tonic-gate 			if (cpi->cpi_xmaxeax < 0x80000008)
9900Sstevel@tonic-gate 				break;
9910Sstevel@tonic-gate 			cp = &cpi->cpi_extd[8];
9921228Sandrei 			cp->cp_eax = 0x80000008;
9931228Sandrei 			(void) __cpuid_insn(cp);
9943446Smrj 			platform_cpuid_mangle(cpi->cpi_vendor, 0x80000008, cp);
9953446Smrj 
9960Sstevel@tonic-gate 			/*
9970Sstevel@tonic-gate 			 * Virtual and physical address limits from
9980Sstevel@tonic-gate 			 * cpuid override previously guessed values.
9990Sstevel@tonic-gate 			 */
10000Sstevel@tonic-gate 			cpi->cpi_pabits = BITX(cp->cp_eax, 7, 0);
10010Sstevel@tonic-gate 			cpi->cpi_vabits = BITX(cp->cp_eax, 15, 8);
10020Sstevel@tonic-gate 			break;
10030Sstevel@tonic-gate 		default:
10040Sstevel@tonic-gate 			break;
10050Sstevel@tonic-gate 		}
10061228Sandrei 
10074606Sesaxe 		/*
10084606Sesaxe 		 * Derive the number of cores per chip
10094606Sesaxe 		 */
10101228Sandrei 		switch (cpi->cpi_vendor) {
10111228Sandrei 		case X86_VENDOR_Intel:
10121228Sandrei 			if (cpi->cpi_maxeax < 4) {
10131228Sandrei 				cpi->cpi_ncore_per_chip = 1;
10141228Sandrei 				break;
10151228Sandrei 			} else {
10161228Sandrei 				cpi->cpi_ncore_per_chip =
10171228Sandrei 				    BITX((cpi)->cpi_std[4].cp_eax, 31, 26) + 1;
10181228Sandrei 			}
10191228Sandrei 			break;
10201228Sandrei 		case X86_VENDOR_AMD:
10211228Sandrei 			if (cpi->cpi_xmaxeax < 0x80000008) {
10221228Sandrei 				cpi->cpi_ncore_per_chip = 1;
10231228Sandrei 				break;
10241228Sandrei 			} else {
10255870Sgavinm 				/*
10265870Sgavinm 				 * On family 0xf cpuid fn 2 ECX[7:0] "NC" is
10275870Sgavinm 				 * 1 less than the number of physical cores on
10285870Sgavinm 				 * the chip.  In family 0x10 this value can
10295870Sgavinm 				 * be affected by "downcoring" - it reflects
10305870Sgavinm 				 * 1 less than the number of cores actually
10315870Sgavinm 				 * enabled on this node.
10325870Sgavinm 				 */
10331228Sandrei 				cpi->cpi_ncore_per_chip =
10341228Sandrei 				    BITX((cpi)->cpi_extd[8].cp_ecx, 7, 0) + 1;
10351228Sandrei 			}
10361228Sandrei 			break;
10371228Sandrei 		default:
10381228Sandrei 			cpi->cpi_ncore_per_chip = 1;
10391228Sandrei 			break;
10401228Sandrei 		}
10418906SEric.Saxe@Sun.COM 
10428906SEric.Saxe@Sun.COM 		/*
10438906SEric.Saxe@Sun.COM 		 * Get CPUID data about TSC Invariance in Deep C-State.
10448906SEric.Saxe@Sun.COM 		 */
10458906SEric.Saxe@Sun.COM 		switch (cpi->cpi_vendor) {
10468906SEric.Saxe@Sun.COM 		case X86_VENDOR_Intel:
10478906SEric.Saxe@Sun.COM 			if (cpi->cpi_maxeax >= 7) {
10488906SEric.Saxe@Sun.COM 				cp = &cpi->cpi_extd[7];
10498906SEric.Saxe@Sun.COM 				cp->cp_eax = 0x80000007;
10508906SEric.Saxe@Sun.COM 				cp->cp_ecx = 0;
10518906SEric.Saxe@Sun.COM 				(void) __cpuid_insn(cp);
10528906SEric.Saxe@Sun.COM 			}
10538906SEric.Saxe@Sun.COM 			break;
10548906SEric.Saxe@Sun.COM 		default:
10558906SEric.Saxe@Sun.COM 			break;
10568906SEric.Saxe@Sun.COM 		}
10575284Sgavinm 	} else {
10585284Sgavinm 		cpi->cpi_ncore_per_chip = 1;
10590Sstevel@tonic-gate 	}
10600Sstevel@tonic-gate 
10611228Sandrei 	/*
10621228Sandrei 	 * If more than one core, then this processor is CMP.
10631228Sandrei 	 */
10641228Sandrei 	if (cpi->cpi_ncore_per_chip > 1)
10651228Sandrei 		feature |= X86_CMP;
10663446Smrj 
10671228Sandrei 	/*
10681228Sandrei 	 * If the number of cores is the same as the number
10691228Sandrei 	 * of CPUs, then we cannot have HyperThreading.
10701228Sandrei 	 */
10711228Sandrei 	if (cpi->cpi_ncpu_per_chip == cpi->cpi_ncore_per_chip)
10721228Sandrei 		feature &= ~X86_HTT;
10731228Sandrei 
10740Sstevel@tonic-gate 	if ((feature & (X86_HTT | X86_CMP)) == 0) {
10751228Sandrei 		/*
10761228Sandrei 		 * Single-core single-threaded processors.
10771228Sandrei 		 */
10780Sstevel@tonic-gate 		cpi->cpi_chipid = -1;
10790Sstevel@tonic-gate 		cpi->cpi_clogid = 0;
10801228Sandrei 		cpi->cpi_coreid = cpu->cpu_id;
10815870Sgavinm 		cpi->cpi_pkgcoreid = 0;
10820Sstevel@tonic-gate 	} else if (cpi->cpi_ncpu_per_chip > 1) {
10831228Sandrei 		uint_t i;
10841228Sandrei 		uint_t chipid_shift = 0;
10851228Sandrei 		uint_t coreid_shift = 0;
10861228Sandrei 		uint_t apic_id = CPI_APIC_ID(cpi);
10871228Sandrei 
10881228Sandrei 		for (i = 1; i < cpi->cpi_ncpu_per_chip; i <<= 1)
10891228Sandrei 			chipid_shift++;
10901228Sandrei 		cpi->cpi_chipid = apic_id >> chipid_shift;
10911228Sandrei 		cpi->cpi_clogid = apic_id & ((1 << chipid_shift) - 1);
10920Sstevel@tonic-gate 
10931228Sandrei 		if (cpi->cpi_vendor == X86_VENDOR_Intel) {
10941228Sandrei 			if (feature & X86_CMP) {
10951228Sandrei 				/*
10961228Sandrei 				 * Multi-core (and possibly multi-threaded)
10971228Sandrei 				 * processors.
10981228Sandrei 				 */
10991228Sandrei 				uint_t ncpu_per_core;
11001228Sandrei 				if (cpi->cpi_ncore_per_chip == 1)
11011228Sandrei 					ncpu_per_core = cpi->cpi_ncpu_per_chip;
11021228Sandrei 				else if (cpi->cpi_ncore_per_chip > 1)
11031228Sandrei 					ncpu_per_core = cpi->cpi_ncpu_per_chip /
11041228Sandrei 					    cpi->cpi_ncore_per_chip;
11051228Sandrei 				/*
11061228Sandrei 				 * 8bit APIC IDs on dual core Pentiums
11071228Sandrei 				 * look like this:
11081228Sandrei 				 *
11091228Sandrei 				 * +-----------------------+------+------+
11101228Sandrei 				 * | Physical Package ID   |  MC  |  HT  |
11111228Sandrei 				 * +-----------------------+------+------+
11121228Sandrei 				 * <------- chipid -------->
11131228Sandrei 				 * <------- coreid --------------->
11141228Sandrei 				 *			   <--- clogid -->
11155870Sgavinm 				 *			   <------>
11165870Sgavinm 				 *			   pkgcoreid
11171228Sandrei 				 *
11181228Sandrei 				 * Where the number of bits necessary to
11191228Sandrei 				 * represent MC and HT fields together equals
11201228Sandrei 				 * to the minimum number of bits necessary to
11211228Sandrei 				 * store the value of cpi->cpi_ncpu_per_chip.
11221228Sandrei 				 * Of those bits, the MC part uses the number
11231228Sandrei 				 * of bits necessary to store the value of
11241228Sandrei 				 * cpi->cpi_ncore_per_chip.
11251228Sandrei 				 */
11261228Sandrei 				for (i = 1; i < ncpu_per_core; i <<= 1)
11271228Sandrei 					coreid_shift++;
11281727Sandrei 				cpi->cpi_coreid = apic_id >> coreid_shift;
11295870Sgavinm 				cpi->cpi_pkgcoreid = cpi->cpi_clogid >>
11305870Sgavinm 				    coreid_shift;
11311228Sandrei 			} else if (feature & X86_HTT) {
11321228Sandrei 				/*
11331228Sandrei 				 * Single-core multi-threaded processors.
11341228Sandrei 				 */
11351228Sandrei 				cpi->cpi_coreid = cpi->cpi_chipid;
11365870Sgavinm 				cpi->cpi_pkgcoreid = 0;
11371228Sandrei 			}
11381228Sandrei 		} else if (cpi->cpi_vendor == X86_VENDOR_AMD) {
11391228Sandrei 			/*
11405870Sgavinm 			 * AMD CMP chips currently have a single thread per
11415870Sgavinm 			 * core, with 2 cores on family 0xf and 2, 3 or 4
11425870Sgavinm 			 * cores on family 0x10.
11435870Sgavinm 			 *
11445870Sgavinm 			 * Since no two cpus share a core we must assign a
11455870Sgavinm 			 * distinct coreid per cpu, and we do this by using
11465870Sgavinm 			 * the cpu_id.  This scheme does not, however,
11475870Sgavinm 			 * guarantee that sibling cores of a chip will have
11485870Sgavinm 			 * sequential coreids starting at a multiple of the
11495870Sgavinm 			 * number of cores per chip - that is usually the
11505870Sgavinm 			 * case, but if the ACPI MADT table is presented
11515870Sgavinm 			 * in a different order then we need to perform a
11525870Sgavinm 			 * few more gymnastics for the pkgcoreid.
11535870Sgavinm 			 *
11545870Sgavinm 			 * In family 0xf CMPs there are 2 cores on all nodes
11555870Sgavinm 			 * present - no mixing of single and dual core parts.
11565870Sgavinm 			 *
11575870Sgavinm 			 * In family 0x10 CMPs cpuid fn 2 ECX[15:12]
11585870Sgavinm 			 * "ApicIdCoreIdSize[3:0]" tells us how
11595870Sgavinm 			 * many least-significant bits in the ApicId
11605870Sgavinm 			 * are used to represent the core number
11615870Sgavinm 			 * within the node.  Cores are always
11625870Sgavinm 			 * numbered sequentially from 0 regardless
11635870Sgavinm 			 * of how many or which are disabled, and
11645870Sgavinm 			 * there seems to be no way to discover the
11655870Sgavinm 			 * real core id when some are disabled.
11661228Sandrei 			 */
11671228Sandrei 			cpi->cpi_coreid = cpu->cpu_id;
11685870Sgavinm 
11695870Sgavinm 			if (cpi->cpi_family == 0x10 &&
11705870Sgavinm 			    cpi->cpi_xmaxeax >= 0x80000008) {
11715870Sgavinm 				int coreidsz =
11725870Sgavinm 				    BITX((cpi)->cpi_extd[8].cp_ecx, 15, 12);
11735870Sgavinm 
11745870Sgavinm 				cpi->cpi_pkgcoreid =
11755870Sgavinm 				    apic_id & ((1 << coreidsz) - 1);
11765870Sgavinm 			} else {
11775870Sgavinm 				cpi->cpi_pkgcoreid = cpi->cpi_clogid;
11785870Sgavinm 			}
11791228Sandrei 		} else {
11801228Sandrei 			/*
11811228Sandrei 			 * All other processors are currently
11821228Sandrei 			 * assumed to have single cores.
11831228Sandrei 			 */
11841228Sandrei 			cpi->cpi_coreid = cpi->cpi_chipid;
11855870Sgavinm 			cpi->cpi_pkgcoreid = 0;
11861228Sandrei 		}
11870Sstevel@tonic-gate 	}
11880Sstevel@tonic-gate 
11897282Smishra 	cpi->cpi_apicid = CPI_APIC_ID(cpi);
11907282Smishra 
11912869Sgavinm 	/*
11922869Sgavinm 	 * Synthesize chip "revision" and socket type
11932869Sgavinm 	 */
11947532SSean.Ye@Sun.COM 	cpi->cpi_chiprev = _cpuid_chiprev(cpi->cpi_vendor, cpi->cpi_family,
11957532SSean.Ye@Sun.COM 	    cpi->cpi_model, cpi->cpi_step);
11967532SSean.Ye@Sun.COM 	cpi->cpi_chiprevstr = _cpuid_chiprevstr(cpi->cpi_vendor,
11977532SSean.Ye@Sun.COM 	    cpi->cpi_family, cpi->cpi_model, cpi->cpi_step);
11987532SSean.Ye@Sun.COM 	cpi->cpi_socket = _cpuid_skt(cpi->cpi_vendor, cpi->cpi_family,
11997532SSean.Ye@Sun.COM 	    cpi->cpi_model, cpi->cpi_step);
12002869Sgavinm 
12010Sstevel@tonic-gate pass1_done:
12025741Smrj #if !defined(__xpv)
12035741Smrj 	check_for_hvm();
12045741Smrj #endif
12050Sstevel@tonic-gate 	cpi->cpi_pass = 1;
12060Sstevel@tonic-gate 	return (feature);
12070Sstevel@tonic-gate }
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate /*
12100Sstevel@tonic-gate  * Make copies of the cpuid table entries we depend on, in
12110Sstevel@tonic-gate  * part for ease of parsing now, in part so that we have only
12120Sstevel@tonic-gate  * one place to correct any of it, in part for ease of
12130Sstevel@tonic-gate  * later export to userland, and in part so we can look at
12140Sstevel@tonic-gate  * this stuff in a crash dump.
12150Sstevel@tonic-gate  */
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate /*ARGSUSED*/
12180Sstevel@tonic-gate void
12190Sstevel@tonic-gate cpuid_pass2(cpu_t *cpu)
12200Sstevel@tonic-gate {
12210Sstevel@tonic-gate 	uint_t n, nmax;
12220Sstevel@tonic-gate 	int i;
12231228Sandrei 	struct cpuid_regs *cp;
12240Sstevel@tonic-gate 	uint8_t *dp;
12250Sstevel@tonic-gate 	uint32_t *iptr;
12260Sstevel@tonic-gate 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate 	ASSERT(cpi->cpi_pass == 1);
12290Sstevel@tonic-gate 
12300Sstevel@tonic-gate 	if (cpi->cpi_maxeax < 1)
12310Sstevel@tonic-gate 		goto pass2_done;
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 	if ((nmax = cpi->cpi_maxeax + 1) > NMAX_CPI_STD)
12340Sstevel@tonic-gate 		nmax = NMAX_CPI_STD;
12350Sstevel@tonic-gate 	/*
12360Sstevel@tonic-gate 	 * (We already handled n == 0 and n == 1 in pass 1)
12370Sstevel@tonic-gate 	 */
12380Sstevel@tonic-gate 	for (n = 2, cp = &cpi->cpi_std[2]; n < nmax; n++, cp++) {
12391228Sandrei 		cp->cp_eax = n;
12404606Sesaxe 
12414606Sesaxe 		/*
12424606Sesaxe 		 * CPUID function 4 expects %ecx to be initialized
12434606Sesaxe 		 * with an index which indicates which cache to return
12444606Sesaxe 		 * information about. The OS is expected to call function 4
12454606Sesaxe 		 * with %ecx set to 0, 1, 2, ... until it returns with
12464606Sesaxe 		 * EAX[4:0] set to 0, which indicates there are no more
12474606Sesaxe 		 * caches.
12484606Sesaxe 		 *
12494606Sesaxe 		 * Here, populate cpi_std[4] with the information returned by
12504606Sesaxe 		 * function 4 when %ecx == 0, and do the rest in cpuid_pass3()
12514606Sesaxe 		 * when dynamic memory allocation becomes available.
12524606Sesaxe 		 *
12534606Sesaxe 		 * Note: we need to explicitly initialize %ecx here, since
12544606Sesaxe 		 * function 4 may have been previously invoked.
12554606Sesaxe 		 */
12564606Sesaxe 		if (n == 4)
12574606Sesaxe 			cp->cp_ecx = 0;
12584606Sesaxe 
12591228Sandrei 		(void) __cpuid_insn(cp);
12603446Smrj 		platform_cpuid_mangle(cpi->cpi_vendor, n, cp);
12610Sstevel@tonic-gate 		switch (n) {
12620Sstevel@tonic-gate 		case 2:
12630Sstevel@tonic-gate 			/*
12640Sstevel@tonic-gate 			 * "the lower 8 bits of the %eax register
12650Sstevel@tonic-gate 			 * contain a value that identifies the number
12660Sstevel@tonic-gate 			 * of times the cpuid [instruction] has to be
12670Sstevel@tonic-gate 			 * executed to obtain a complete image of the
12680Sstevel@tonic-gate 			 * processor's caching systems."
12690Sstevel@tonic-gate 			 *
12700Sstevel@tonic-gate 			 * How *do* they make this stuff up?
12710Sstevel@tonic-gate 			 */
12720Sstevel@tonic-gate 			cpi->cpi_ncache = sizeof (*cp) *
12730Sstevel@tonic-gate 			    BITX(cp->cp_eax, 7, 0);
12740Sstevel@tonic-gate 			if (cpi->cpi_ncache == 0)
12750Sstevel@tonic-gate 				break;
12760Sstevel@tonic-gate 			cpi->cpi_ncache--;	/* skip count byte */
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 			/*
12790Sstevel@tonic-gate 			 * Well, for now, rather than attempt to implement
12800Sstevel@tonic-gate 			 * this slightly dubious algorithm, we just look
12810Sstevel@tonic-gate 			 * at the first 15 ..
12820Sstevel@tonic-gate 			 */
12830Sstevel@tonic-gate 			if (cpi->cpi_ncache > (sizeof (*cp) - 1))
12840Sstevel@tonic-gate 				cpi->cpi_ncache = sizeof (*cp) - 1;
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate 			dp = cpi->cpi_cacheinfo;
12870Sstevel@tonic-gate 			if (BITX(cp->cp_eax, 31, 31) == 0) {
12880Sstevel@tonic-gate 				uint8_t *p = (void *)&cp->cp_eax;
12896317Skk208521 				for (i = 1; i < 4; i++)
12900Sstevel@tonic-gate 					if (p[i] != 0)
12910Sstevel@tonic-gate 						*dp++ = p[i];
12920Sstevel@tonic-gate 			}
12930Sstevel@tonic-gate 			if (BITX(cp->cp_ebx, 31, 31) == 0) {
12940Sstevel@tonic-gate 				uint8_t *p = (void *)&cp->cp_ebx;
12950Sstevel@tonic-gate 				for (i = 0; i < 4; i++)
12960Sstevel@tonic-gate 					if (p[i] != 0)
12970Sstevel@tonic-gate 						*dp++ = p[i];
12980Sstevel@tonic-gate 			}
12990Sstevel@tonic-gate 			if (BITX(cp->cp_ecx, 31, 31) == 0) {
13000Sstevel@tonic-gate 				uint8_t *p = (void *)&cp->cp_ecx;
13010Sstevel@tonic-gate 				for (i = 0; i < 4; i++)
13020Sstevel@tonic-gate 					if (p[i] != 0)
13030Sstevel@tonic-gate 						*dp++ = p[i];
13040Sstevel@tonic-gate 			}
13050Sstevel@tonic-gate 			if (BITX(cp->cp_edx, 31, 31) == 0) {
13060Sstevel@tonic-gate 				uint8_t *p = (void *)&cp->cp_edx;
13070Sstevel@tonic-gate 				for (i = 0; i < 4; i++)
13080Sstevel@tonic-gate 					if (p[i] != 0)
13090Sstevel@tonic-gate 						*dp++ = p[i];
13100Sstevel@tonic-gate 			}
13110Sstevel@tonic-gate 			break;
13124481Sbholler 
13130Sstevel@tonic-gate 		case 3:	/* Processor serial number, if PSN supported */
13144481Sbholler 			break;
13154481Sbholler 
13160Sstevel@tonic-gate 		case 4:	/* Deterministic cache parameters */
13174481Sbholler 			break;
13184481Sbholler 
13190Sstevel@tonic-gate 		case 5:	/* Monitor/Mwait parameters */
13205045Sbholler 		{
13215045Sbholler 			size_t mwait_size;
13224481Sbholler 
13234481Sbholler 			/*
13244481Sbholler 			 * check cpi_mwait.support which was set in cpuid_pass1
13254481Sbholler 			 */
13264481Sbholler 			if (!(cpi->cpi_mwait.support & MWAIT_SUPPORT))
13274481Sbholler 				break;
13284481Sbholler 
13295045Sbholler 			/*
13305045Sbholler 			 * Protect ourself from insane mwait line size.
13315045Sbholler 			 * Workaround for incomplete hardware emulator(s).
13325045Sbholler 			 */
13335045Sbholler 			mwait_size = (size_t)MWAIT_SIZE_MAX(cpi);
13345045Sbholler 			if (mwait_size < sizeof (uint32_t) ||
13355045Sbholler 			    !ISP2(mwait_size)) {
13365045Sbholler #if DEBUG
13375045Sbholler 				cmn_err(CE_NOTE, "Cannot handle cpu %d mwait "
13387798SSaurabh.Mishra@Sun.COM 				    "size %ld", cpu->cpu_id, (long)mwait_size);
13395045Sbholler #endif
13405045Sbholler 				break;
13415045Sbholler 			}
13425045Sbholler 
13434481Sbholler 			cpi->cpi_mwait.mon_min = (size_t)MWAIT_SIZE_MIN(cpi);
13445045Sbholler 			cpi->cpi_mwait.mon_max = mwait_size;
13454481Sbholler 			if (MWAIT_EXTENSION(cpi)) {
13464481Sbholler 				cpi->cpi_mwait.support |= MWAIT_EXTENSIONS;
13474481Sbholler 				if (MWAIT_INT_ENABLE(cpi))
13484481Sbholler 					cpi->cpi_mwait.support |=
13494481Sbholler 					    MWAIT_ECX_INT_ENABLE;
13504481Sbholler 			}
13514481Sbholler 			break;
13525045Sbholler 		}
13530Sstevel@tonic-gate 		default:
13540Sstevel@tonic-gate 			break;
13550Sstevel@tonic-gate 		}
13560Sstevel@tonic-gate 	}
13570Sstevel@tonic-gate 
13587282Smishra 	if (cpi->cpi_maxeax >= 0xB && cpi->cpi_vendor == X86_VENDOR_Intel) {
13597798SSaurabh.Mishra@Sun.COM 		struct cpuid_regs regs;
13607798SSaurabh.Mishra@Sun.COM 
13617798SSaurabh.Mishra@Sun.COM 		cp = &regs;
13627282Smishra 		cp->cp_eax = 0xB;
13637798SSaurabh.Mishra@Sun.COM 		cp->cp_edx = cp->cp_ebx = cp->cp_ecx = 0;
13647282Smishra 
13657282Smishra 		(void) __cpuid_insn(cp);
13667282Smishra 
13677282Smishra 		/*
13687282Smishra 		 * Check CPUID.EAX=0BH, ECX=0H:EBX is non-zero, which
13697282Smishra 		 * indicates that the extended topology enumeration leaf is
13707282Smishra 		 * available.
13717282Smishra 		 */
13727282Smishra 		if (cp->cp_ebx) {
13737282Smishra 			uint32_t x2apic_id;
13747282Smishra 			uint_t coreid_shift = 0;
13757282Smishra 			uint_t ncpu_per_core = 1;
13767282Smishra 			uint_t chipid_shift = 0;
13777282Smishra 			uint_t ncpu_per_chip = 1;
13787282Smishra 			uint_t i;
13797282Smishra 			uint_t level;
13807282Smishra 
13817282Smishra 			for (i = 0; i < CPI_FNB_ECX_MAX; i++) {
13827282Smishra 				cp->cp_eax = 0xB;
13837282Smishra 				cp->cp_ecx = i;
13847282Smishra 
13857282Smishra 				(void) __cpuid_insn(cp);
13867282Smishra 				level = CPI_CPU_LEVEL_TYPE(cp);
13877282Smishra 
13887282Smishra 				if (level == 1) {
13897282Smishra 					x2apic_id = cp->cp_edx;
13907282Smishra 					coreid_shift = BITX(cp->cp_eax, 4, 0);
13917282Smishra 					ncpu_per_core = BITX(cp->cp_ebx, 15, 0);
13927282Smishra 				} else if (level == 2) {
13937282Smishra 					x2apic_id = cp->cp_edx;
13947282Smishra 					chipid_shift = BITX(cp->cp_eax, 4, 0);
13957282Smishra 					ncpu_per_chip = BITX(cp->cp_ebx, 15, 0);
13967282Smishra 				}
13977282Smishra 			}
13987282Smishra 
13997282Smishra 			cpi->cpi_apicid = x2apic_id;
14007282Smishra 			cpi->cpi_ncpu_per_chip = ncpu_per_chip;
14017282Smishra 			cpi->cpi_ncore_per_chip = ncpu_per_chip /
14027282Smishra 			    ncpu_per_core;
14037282Smishra 			cpi->cpi_chipid = x2apic_id >> chipid_shift;
14047282Smishra 			cpi->cpi_clogid = x2apic_id & ((1 << chipid_shift) - 1);
14057282Smishra 			cpi->cpi_coreid = x2apic_id >> coreid_shift;
14067282Smishra 			cpi->cpi_pkgcoreid = cpi->cpi_clogid >> coreid_shift;
14077282Smishra 		}
14087798SSaurabh.Mishra@Sun.COM 
14097798SSaurabh.Mishra@Sun.COM 		/* Make cp NULL so that we don't stumble on others */
14107798SSaurabh.Mishra@Sun.COM 		cp = NULL;
14117282Smishra 	}
14127282Smishra 
14130Sstevel@tonic-gate 	if ((cpi->cpi_xmaxeax & 0x80000000) == 0)
14140Sstevel@tonic-gate 		goto pass2_done;
14150Sstevel@tonic-gate 
14160Sstevel@tonic-gate 	if ((nmax = cpi->cpi_xmaxeax - 0x80000000 + 1) > NMAX_CPI_EXTD)
14170Sstevel@tonic-gate 		nmax = NMAX_CPI_EXTD;
14180Sstevel@tonic-gate 	/*
14190Sstevel@tonic-gate 	 * Copy the extended properties, fixing them as we go.
14200Sstevel@tonic-gate 	 * (We already handled n == 0 and n == 1 in pass 1)
14210Sstevel@tonic-gate 	 */
14220Sstevel@tonic-gate 	iptr = (void *)cpi->cpi_brandstr;
14230Sstevel@tonic-gate 	for (n = 2, cp = &cpi->cpi_extd[2]; n < nmax; cp++, n++) {
14241228Sandrei 		cp->cp_eax = 0x80000000 + n;
14251228Sandrei 		(void) __cpuid_insn(cp);
14263446Smrj 		platform_cpuid_mangle(cpi->cpi_vendor, 0x80000000 + n, cp);
14270Sstevel@tonic-gate 		switch (n) {
14280Sstevel@tonic-gate 		case 2:
14290Sstevel@tonic-gate 		case 3:
14300Sstevel@tonic-gate 		case 4:
14310Sstevel@tonic-gate 			/*
14320Sstevel@tonic-gate 			 * Extract the brand string
14330Sstevel@tonic-gate 			 */
14340Sstevel@tonic-gate 			*iptr++ = cp->cp_eax;
14350Sstevel@tonic-gate 			*iptr++ = cp->cp_ebx;
14360Sstevel@tonic-gate 			*iptr++ = cp->cp_ecx;
14370Sstevel@tonic-gate 			*iptr++ = cp->cp_edx;
14380Sstevel@tonic-gate 			break;
14390Sstevel@tonic-gate 		case 5:
14400Sstevel@tonic-gate 			switch (cpi->cpi_vendor) {
14410Sstevel@tonic-gate 			case X86_VENDOR_AMD:
14420Sstevel@tonic-gate 				/*
14430Sstevel@tonic-gate 				 * The Athlon and Duron were the first
14440Sstevel@tonic-gate 				 * parts to report the sizes of the
14450Sstevel@tonic-gate 				 * TLB for large pages. Before then,
14460Sstevel@tonic-gate 				 * we don't trust the data.
14470Sstevel@tonic-gate 				 */
14480Sstevel@tonic-gate 				if (cpi->cpi_family < 6 ||
14490Sstevel@tonic-gate 				    (cpi->cpi_family == 6 &&
14500Sstevel@tonic-gate 				    cpi->cpi_model < 1))
14510Sstevel@tonic-gate 					cp->cp_eax = 0;
14520Sstevel@tonic-gate 				break;
14530Sstevel@tonic-gate 			default:
14540Sstevel@tonic-gate 				break;
14550Sstevel@tonic-gate 			}
14560Sstevel@tonic-gate 			break;
14570Sstevel@tonic-gate 		case 6:
14580Sstevel@tonic-gate 			switch (cpi->cpi_vendor) {
14590Sstevel@tonic-gate 			case X86_VENDOR_AMD:
14600Sstevel@tonic-gate 				/*
14610Sstevel@tonic-gate 				 * The Athlon and Duron were the first
14620Sstevel@tonic-gate 				 * AMD parts with L2 TLB's.
14630Sstevel@tonic-gate 				 * Before then, don't trust the data.
14640Sstevel@tonic-gate 				 */
14650Sstevel@tonic-gate 				if (cpi->cpi_family < 6 ||
14660Sstevel@tonic-gate 				    cpi->cpi_family == 6 &&
14670Sstevel@tonic-gate 				    cpi->cpi_model < 1)
14680Sstevel@tonic-gate 					cp->cp_eax = cp->cp_ebx = 0;
14690Sstevel@tonic-gate 				/*
14700Sstevel@tonic-gate 				 * AMD Duron rev A0 reports L2
14710Sstevel@tonic-gate 				 * cache size incorrectly as 1K
14720Sstevel@tonic-gate 				 * when it is really 64K
14730Sstevel@tonic-gate 				 */
14740Sstevel@tonic-gate 				if (cpi->cpi_family == 6 &&
14750Sstevel@tonic-gate 				    cpi->cpi_model == 3 &&
14760Sstevel@tonic-gate 				    cpi->cpi_step == 0) {
14770Sstevel@tonic-gate 					cp->cp_ecx &= 0xffff;
14780Sstevel@tonic-gate 					cp->cp_ecx |= 0x400000;
14790Sstevel@tonic-gate 				}
14800Sstevel@tonic-gate 				break;
14810Sstevel@tonic-gate 			case X86_VENDOR_Cyrix:	/* VIA C3 */
14820Sstevel@tonic-gate 				/*
14830Sstevel@tonic-gate 				 * VIA C3 processors are a bit messed
14840Sstevel@tonic-gate 				 * up w.r.t. encoding cache sizes in %ecx
14850Sstevel@tonic-gate 				 */
14860Sstevel@tonic-gate 				if (cpi->cpi_family != 6)
14870Sstevel@tonic-gate 					break;
14880Sstevel@tonic-gate 				/*
14890Sstevel@tonic-gate 				 * model 7 and 8 were incorrectly encoded
14900Sstevel@tonic-gate 				 *
14910Sstevel@tonic-gate 				 * xxx is model 8 really broken?
14920Sstevel@tonic-gate 				 */
14930Sstevel@tonic-gate 				if (cpi->cpi_model == 7 ||
14940Sstevel@tonic-gate 				    cpi->cpi_model == 8)
14950Sstevel@tonic-gate 					cp->cp_ecx =
14960Sstevel@tonic-gate 					    BITX(cp->cp_ecx, 31, 24) << 16 |
14970Sstevel@tonic-gate 					    BITX(cp->cp_ecx, 23, 16) << 12 |
14980Sstevel@tonic-gate 					    BITX(cp->cp_ecx, 15, 8) << 8 |
14990Sstevel@tonic-gate 					    BITX(cp->cp_ecx, 7, 0);
15000Sstevel@tonic-gate 				/*
15010Sstevel@tonic-gate 				 * model 9 stepping 1 has wrong associativity
15020Sstevel@tonic-gate 				 */
15030Sstevel@tonic-gate 				if (cpi->cpi_model == 9 && cpi->cpi_step == 1)
15040Sstevel@tonic-gate 					cp->cp_ecx |= 8 << 12;
15050Sstevel@tonic-gate 				break;
15060Sstevel@tonic-gate 			case X86_VENDOR_Intel:
15070Sstevel@tonic-gate 				/*
15080Sstevel@tonic-gate 				 * Extended L2 Cache features function.
15090Sstevel@tonic-gate 				 * First appeared on Prescott.
15100Sstevel@tonic-gate 				 */
15110Sstevel@tonic-gate 			default:
15120Sstevel@tonic-gate 				break;
15130Sstevel@tonic-gate 			}
15140Sstevel@tonic-gate 			break;
15150Sstevel@tonic-gate 		default:
15160Sstevel@tonic-gate 			break;
15170Sstevel@tonic-gate 		}
15180Sstevel@tonic-gate 	}
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate pass2_done:
15210Sstevel@tonic-gate 	cpi->cpi_pass = 2;
15220Sstevel@tonic-gate }
15230Sstevel@tonic-gate 
15240Sstevel@tonic-gate static const char *
15250Sstevel@tonic-gate intel_cpubrand(const struct cpuid_info *cpi)
15260Sstevel@tonic-gate {
15270Sstevel@tonic-gate 	int i;
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate 	if ((x86_feature & X86_CPUID) == 0 ||
15300Sstevel@tonic-gate 	    cpi->cpi_maxeax < 1 || cpi->cpi_family < 5)
15310Sstevel@tonic-gate 		return ("i486");
15320Sstevel@tonic-gate 
15330Sstevel@tonic-gate 	switch (cpi->cpi_family) {
15340Sstevel@tonic-gate 	case 5:
15350Sstevel@tonic-gate 		return ("Intel Pentium(r)");
15360Sstevel@tonic-gate 	case 6:
15370Sstevel@tonic-gate 		switch (cpi->cpi_model) {
15380Sstevel@tonic-gate 			uint_t celeron, xeon;
15391228Sandrei 			const struct cpuid_regs *cp;
15400Sstevel@tonic-gate 		case 0:
15410Sstevel@tonic-gate 		case 1:
15420Sstevel@tonic-gate 		case 2:
15430Sstevel@tonic-gate 			return ("Intel Pentium(r) Pro");
15440Sstevel@tonic-gate 		case 3:
15450Sstevel@tonic-gate 		case 4:
15460Sstevel@tonic-gate 			return ("Intel Pentium(r) II");
15470Sstevel@tonic-gate 		case 6:
15480Sstevel@tonic-gate 			return ("Intel Celeron(r)");
15490Sstevel@tonic-gate 		case 5:
15500Sstevel@tonic-gate 		case 7:
15510Sstevel@tonic-gate 			celeron = xeon = 0;
15520Sstevel@tonic-gate 			cp = &cpi->cpi_std[2];	/* cache info */
15530Sstevel@tonic-gate 
15546317Skk208521 			for (i = 1; i < 4; i++) {
15550Sstevel@tonic-gate 				uint_t tmp;
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate 				tmp = (cp->cp_eax >> (8 * i)) & 0xff;
15580Sstevel@tonic-gate 				if (tmp == 0x40)
15590Sstevel@tonic-gate 					celeron++;
15600Sstevel@tonic-gate 				if (tmp >= 0x44 && tmp <= 0x45)
15610Sstevel@tonic-gate 					xeon++;
15620Sstevel@tonic-gate 			}
15630Sstevel@tonic-gate 
15640Sstevel@tonic-gate 			for (i = 0; i < 2; i++) {
15650Sstevel@tonic-gate 				uint_t tmp;
15660Sstevel@tonic-gate 
15670Sstevel@tonic-gate 				tmp = (cp->cp_ebx >> (8 * i)) & 0xff;
15680Sstevel@tonic-gate 				if (tmp == 0x40)
15690Sstevel@tonic-gate 					celeron++;
15700Sstevel@tonic-gate 				else if (tmp >= 0x44 && tmp <= 0x45)
15710Sstevel@tonic-gate 					xeon++;
15720Sstevel@tonic-gate 			}
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate 			for (i = 0; i < 4; i++) {
15750Sstevel@tonic-gate 				uint_t tmp;
15760Sstevel@tonic-gate 
15770Sstevel@tonic-gate 				tmp = (cp->cp_ecx >> (8 * i)) & 0xff;
15780Sstevel@tonic-gate 				if (tmp == 0x40)
15790Sstevel@tonic-gate 					celeron++;
15800Sstevel@tonic-gate 				else if (tmp >= 0x44 && tmp <= 0x45)
15810Sstevel@tonic-gate 					xeon++;
15820Sstevel@tonic-gate 			}
15830Sstevel@tonic-gate 
15840Sstevel@tonic-gate 			for (i = 0; i < 4; i++) {
15850Sstevel@tonic-gate 				uint_t tmp;
15860Sstevel@tonic-gate 
15870Sstevel@tonic-gate 				tmp = (cp->cp_edx >> (8 * i)) & 0xff;
15880Sstevel@tonic-gate 				if (tmp == 0x40)
15890Sstevel@tonic-gate 					celeron++;
15900Sstevel@tonic-gate 				else if (tmp >= 0x44 && tmp <= 0x45)
15910Sstevel@tonic-gate 					xeon++;
15920Sstevel@tonic-gate 			}
15930Sstevel@tonic-gate 
15940Sstevel@tonic-gate 			if (celeron)
15950Sstevel@tonic-gate 				return ("Intel Celeron(r)");
15960Sstevel@tonic-gate 			if (xeon)
15970Sstevel@tonic-gate 				return (cpi->cpi_model == 5 ?
15980Sstevel@tonic-gate 				    "Intel Pentium(r) II Xeon(tm)" :
15990Sstevel@tonic-gate 				    "Intel Pentium(r) III Xeon(tm)");
16000Sstevel@tonic-gate 			return (cpi->cpi_model == 5 ?
16010Sstevel@tonic-gate 			    "Intel Pentium(r) II or Pentium(r) II Xeon(tm)" :
16020Sstevel@tonic-gate 			    "Intel Pentium(r) III or Pentium(r) III Xeon(tm)");
16030Sstevel@tonic-gate 		default:
16040Sstevel@tonic-gate 			break;
16050Sstevel@tonic-gate 		}
16060Sstevel@tonic-gate 	default:
16070Sstevel@tonic-gate 		break;
16080Sstevel@tonic-gate 	}
16090Sstevel@tonic-gate 
16101975Sdmick 	/* BrandID is present if the field is nonzero */
16111975Sdmick 	if (cpi->cpi_brandid != 0) {
16120Sstevel@tonic-gate 		static const struct {
16130Sstevel@tonic-gate 			uint_t bt_bid;
16140Sstevel@tonic-gate 			const char *bt_str;
16150Sstevel@tonic-gate 		} brand_tbl[] = {
16160Sstevel@tonic-gate 			{ 0x1,	"Intel(r) Celeron(r)" },
16170Sstevel@tonic-gate 			{ 0x2,	"Intel(r) Pentium(r) III" },
16180Sstevel@tonic-gate 			{ 0x3,	"Intel(r) Pentium(r) III Xeon(tm)" },
16190Sstevel@tonic-gate 			{ 0x4,	"Intel(r) Pentium(r) III" },
16200Sstevel@tonic-gate 			{ 0x6,	"Mobile Intel(r) Pentium(r) III" },
16210Sstevel@tonic-gate 			{ 0x7,	"Mobile Intel(r) Celeron(r)" },
16220Sstevel@tonic-gate 			{ 0x8,	"Intel(r) Pentium(r) 4" },
16230Sstevel@tonic-gate 			{ 0x9,	"Intel(r) Pentium(r) 4" },
16240Sstevel@tonic-gate 			{ 0xa,	"Intel(r) Celeron(r)" },
16250Sstevel@tonic-gate 			{ 0xb,	"Intel(r) Xeon(tm)" },
16260Sstevel@tonic-gate 			{ 0xc,	"Intel(r) Xeon(tm) MP" },
16270Sstevel@tonic-gate 			{ 0xe,	"Mobile Intel(r) Pentium(r) 4" },
16281975Sdmick 			{ 0xf,	"Mobile Intel(r) Celeron(r)" },
16291975Sdmick 			{ 0x11, "Mobile Genuine Intel(r)" },
16301975Sdmick 			{ 0x12, "Intel(r) Celeron(r) M" },
16311975Sdmick 			{ 0x13, "Mobile Intel(r) Celeron(r)" },
16321975Sdmick 			{ 0x14, "Intel(r) Celeron(r)" },
16331975Sdmick 			{ 0x15, "Mobile Genuine Intel(r)" },
16341975Sdmick 			{ 0x16,	"Intel(r) Pentium(r) M" },
16351975Sdmick 			{ 0x17, "Mobile Intel(r) Celeron(r)" }
16360Sstevel@tonic-gate 		};
16370Sstevel@tonic-gate 		uint_t btblmax = sizeof (brand_tbl) / sizeof (brand_tbl[0]);
16380Sstevel@tonic-gate 		uint_t sgn;
16390Sstevel@tonic-gate 
16400Sstevel@tonic-gate 		sgn = (cpi->cpi_family << 8) |
16410Sstevel@tonic-gate 		    (cpi->cpi_model << 4) | cpi->cpi_step;
16420Sstevel@tonic-gate 
16430Sstevel@tonic-gate 		for (i = 0; i < btblmax; i++)
16440Sstevel@tonic-gate 			if (brand_tbl[i].bt_bid == cpi->cpi_brandid)
16450Sstevel@tonic-gate 				break;
16460Sstevel@tonic-gate 		if (i < btblmax) {
16470Sstevel@tonic-gate 			if (sgn == 0x6b1 && cpi->cpi_brandid == 3)
16480Sstevel@tonic-gate 				return ("Intel(r) Celeron(r)");
16490Sstevel@tonic-gate 			if (sgn < 0xf13 && cpi->cpi_brandid == 0xb)
16500Sstevel@tonic-gate 				return ("Intel(r) Xeon(tm) MP");
16510Sstevel@tonic-gate 			if (sgn < 0xf13 && cpi->cpi_brandid == 0xe)
16520Sstevel@tonic-gate 				return ("Intel(r) Xeon(tm)");
16530Sstevel@tonic-gate 			return (brand_tbl[i].bt_str);
16540Sstevel@tonic-gate 		}
16550Sstevel@tonic-gate 	}
16560Sstevel@tonic-gate 
16570Sstevel@tonic-gate 	return (NULL);
16580Sstevel@tonic-gate }
16590Sstevel@tonic-gate 
16600Sstevel@tonic-gate static const char *
16610Sstevel@tonic-gate amd_cpubrand(const struct cpuid_info *cpi)
16620Sstevel@tonic-gate {
16630Sstevel@tonic-gate 	if ((x86_feature & X86_CPUID) == 0 ||
16640Sstevel@tonic-gate 	    cpi->cpi_maxeax < 1 || cpi->cpi_family < 5)
16650Sstevel@tonic-gate 		return ("i486 compatible");
16660Sstevel@tonic-gate 
16670Sstevel@tonic-gate 	switch (cpi->cpi_family) {
16680Sstevel@tonic-gate 	case 5:
16690Sstevel@tonic-gate 		switch (cpi->cpi_model) {
16700Sstevel@tonic-gate 		case 0:
16710Sstevel@tonic-gate 		case 1:
16720Sstevel@tonic-gate 		case 2:
16730Sstevel@tonic-gate 		case 3:
16740Sstevel@tonic-gate 		case 4:
16750Sstevel@tonic-gate 		case 5:
16760Sstevel@tonic-gate 			return ("AMD-K5(r)");
16770Sstevel@tonic-gate 		case 6:
16780Sstevel@tonic-gate 		case 7:
16790Sstevel@tonic-gate 			return ("AMD-K6(r)");
16800Sstevel@tonic-gate 		case 8:
16810Sstevel@tonic-gate 			return ("AMD-K6(r)-2");
16820Sstevel@tonic-gate 		case 9:
16830Sstevel@tonic-gate 			return ("AMD-K6(r)-III");
16840Sstevel@tonic-gate 		default:
16850Sstevel@tonic-gate 			return ("AMD (family 5)");
16860Sstevel@tonic-gate 		}
16870Sstevel@tonic-gate 	case 6:
16880Sstevel@tonic-gate 		switch (cpi->cpi_model) {
16890Sstevel@tonic-gate 		case 1:
16900Sstevel@tonic-gate 			return ("AMD-K7(tm)");
16910Sstevel@tonic-gate 		case 0:
16920Sstevel@tonic-gate 		case 2:
16930Sstevel@tonic-gate 		case 4:
16940Sstevel@tonic-gate 			return ("AMD Athlon(tm)");
16950Sstevel@tonic-gate 		case 3:
16960Sstevel@tonic-gate 		case 7:
16970Sstevel@tonic-gate 			return ("AMD Duron(tm)");
16980Sstevel@tonic-gate 		case 6:
16990Sstevel@tonic-gate 		case 8:
17000Sstevel@tonic-gate 		case 10:
17010Sstevel@tonic-gate 			/*
17020Sstevel@tonic-gate 			 * Use the L2 cache size to distinguish
17030Sstevel@tonic-gate 			 */
17040Sstevel@tonic-gate 			return ((cpi->cpi_extd[6].cp_ecx >> 16) >= 256 ?
17050Sstevel@tonic-gate 			    "AMD Athlon(tm)" : "AMD Duron(tm)");
17060Sstevel@tonic-gate 		default:
17070Sstevel@tonic-gate 			return ("AMD (family 6)");
17080Sstevel@tonic-gate 		}
17090Sstevel@tonic-gate 	default:
17100Sstevel@tonic-gate 		break;
17110Sstevel@tonic-gate 	}
17120Sstevel@tonic-gate 
17130Sstevel@tonic-gate 	if (cpi->cpi_family == 0xf && cpi->cpi_model == 5 &&
17140Sstevel@tonic-gate 	    cpi->cpi_brandid != 0) {
17150Sstevel@tonic-gate 		switch (BITX(cpi->cpi_brandid, 7, 5)) {
17160Sstevel@tonic-gate 		case 3:
17170Sstevel@tonic-gate 			return ("AMD Opteron(tm) UP 1xx");
17180Sstevel@tonic-gate 		case 4:
17190Sstevel@tonic-gate 			return ("AMD Opteron(tm) DP 2xx");
17200Sstevel@tonic-gate 		case 5:
17210Sstevel@tonic-gate 			return ("AMD Opteron(tm) MP 8xx");
17220Sstevel@tonic-gate 		default:
17230Sstevel@tonic-gate 			return ("AMD Opteron(tm)");
17240Sstevel@tonic-gate 		}
17250Sstevel@tonic-gate 	}
17260Sstevel@tonic-gate 
17270Sstevel@tonic-gate 	return (NULL);
17280Sstevel@tonic-gate }
17290Sstevel@tonic-gate 
17300Sstevel@tonic-gate static const char *
17310Sstevel@tonic-gate cyrix_cpubrand(struct cpuid_info *cpi, uint_t type)
17320Sstevel@tonic-gate {
17330Sstevel@tonic-gate 	if ((x86_feature & X86_CPUID) == 0 ||
17340Sstevel@tonic-gate 	    cpi->cpi_maxeax < 1 || cpi->cpi_family < 5 ||
17350Sstevel@tonic-gate 	    type == X86_TYPE_CYRIX_486)
17360Sstevel@tonic-gate 		return ("i486 compatible");
17370Sstevel@tonic-gate 
17380Sstevel@tonic-gate 	switch (type) {
17390Sstevel@tonic-gate 	case X86_TYPE_CYRIX_6x86:
17400Sstevel@tonic-gate 		return ("Cyrix 6x86");
17410Sstevel@tonic-gate 	case X86_TYPE_CYRIX_6x86L:
17420Sstevel@tonic-gate 		return ("Cyrix 6x86L");
17430Sstevel@tonic-gate 	case X86_TYPE_CYRIX_6x86MX:
17440Sstevel@tonic-gate 		return ("Cyrix 6x86MX");
17450Sstevel@tonic-gate 	case X86_TYPE_CYRIX_GXm:
17460Sstevel@tonic-gate 		return ("Cyrix GXm");
17470Sstevel@tonic-gate 	case X86_TYPE_CYRIX_MediaGX:
17480Sstevel@tonic-gate 		return ("Cyrix MediaGX");
17490Sstevel@tonic-gate 	case X86_TYPE_CYRIX_MII:
17500Sstevel@tonic-gate 		return ("Cyrix M2");
17510Sstevel@tonic-gate 	case X86_TYPE_VIA_CYRIX_III:
17520Sstevel@tonic-gate 		return ("VIA Cyrix M3");
17530Sstevel@tonic-gate 	default:
17540Sstevel@tonic-gate 		/*
17550Sstevel@tonic-gate 		 * Have another wild guess ..
17560Sstevel@tonic-gate 		 */
17570Sstevel@tonic-gate 		if (cpi->cpi_family == 4 && cpi->cpi_model == 9)
17580Sstevel@tonic-gate 			return ("Cyrix 5x86");
17590Sstevel@tonic-gate 		else if (cpi->cpi_family == 5) {
17600Sstevel@tonic-gate 			switch (cpi->cpi_model) {
17610Sstevel@tonic-gate 			case 2:
17620Sstevel@tonic-gate 				return ("Cyrix 6x86");	/* Cyrix M1 */
17630Sstevel@tonic-gate 			case 4:
17640Sstevel@tonic-gate 				return ("Cyrix MediaGX");
17650Sstevel@tonic-gate 			default:
17660Sstevel@tonic-gate 				break;
17670Sstevel@tonic-gate 			}
17680Sstevel@tonic-gate 		} else if (cpi->cpi_family == 6) {
17690Sstevel@tonic-gate 			switch (cpi->cpi_model) {
17700Sstevel@tonic-gate 			case 0:
17710Sstevel@tonic-gate 				return ("Cyrix 6x86MX"); /* Cyrix M2? */
17720Sstevel@tonic-gate 			case 5:
17730Sstevel@tonic-gate 			case 6:
17740Sstevel@tonic-gate 			case 7:
17750Sstevel@tonic-gate 			case 8:
17760Sstevel@tonic-gate 			case 9:
17770Sstevel@tonic-gate 				return ("VIA C3");
17780Sstevel@tonic-gate 			default:
17790Sstevel@tonic-gate 				break;
17800Sstevel@tonic-gate 			}
17810Sstevel@tonic-gate 		}
17820Sstevel@tonic-gate 		break;
17830Sstevel@tonic-gate 	}
17840Sstevel@tonic-gate 	return (NULL);
17850Sstevel@tonic-gate }
17860Sstevel@tonic-gate 
17870Sstevel@tonic-gate /*
17880Sstevel@tonic-gate  * This only gets called in the case that the CPU extended
17890Sstevel@tonic-gate  * feature brand string (0x80000002, 0x80000003, 0x80000004)
17900Sstevel@tonic-gate  * aren't available, or contain null bytes for some reason.
17910Sstevel@tonic-gate  */
17920Sstevel@tonic-gate static void
17930Sstevel@tonic-gate fabricate_brandstr(struct cpuid_info *cpi)
17940Sstevel@tonic-gate {
17950Sstevel@tonic-gate 	const char *brand = NULL;
17960Sstevel@tonic-gate 
17970Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
17980Sstevel@tonic-gate 	case X86_VENDOR_Intel:
17990Sstevel@tonic-gate 		brand = intel_cpubrand(cpi);
18000Sstevel@tonic-gate 		break;
18010Sstevel@tonic-gate 	case X86_VENDOR_AMD:
18020Sstevel@tonic-gate 		brand = amd_cpubrand(cpi);
18030Sstevel@tonic-gate 		break;
18040Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
18050Sstevel@tonic-gate 		brand = cyrix_cpubrand(cpi, x86_type);
18060Sstevel@tonic-gate 		break;
18070Sstevel@tonic-gate 	case X86_VENDOR_NexGen:
18080Sstevel@tonic-gate 		if (cpi->cpi_family == 5 && cpi->cpi_model == 0)
18090Sstevel@tonic-gate 			brand = "NexGen Nx586";
18100Sstevel@tonic-gate 		break;
18110Sstevel@tonic-gate 	case X86_VENDOR_Centaur:
18120Sstevel@tonic-gate 		if (cpi->cpi_family == 5)
18130Sstevel@tonic-gate 			switch (cpi->cpi_model) {
18140Sstevel@tonic-gate 			case 4:
18150Sstevel@tonic-gate 				brand = "Centaur C6";
18160Sstevel@tonic-gate 				break;
18170Sstevel@tonic-gate 			case 8:
18180Sstevel@tonic-gate 				brand = "Centaur C2";
18190Sstevel@tonic-gate 				break;
18200Sstevel@tonic-gate 			case 9:
18210Sstevel@tonic-gate 				brand = "Centaur C3";
18220Sstevel@tonic-gate 				break;
18230Sstevel@tonic-gate 			default:
18240Sstevel@tonic-gate 				break;
18250Sstevel@tonic-gate 			}
18260Sstevel@tonic-gate 		break;
18270Sstevel@tonic-gate 	case X86_VENDOR_Rise:
18280Sstevel@tonic-gate 		if (cpi->cpi_family == 5 &&
18290Sstevel@tonic-gate 		    (cpi->cpi_model == 0 || cpi->cpi_model == 2))
18300Sstevel@tonic-gate 			brand = "Rise mP6";
18310Sstevel@tonic-gate 		break;
18320Sstevel@tonic-gate 	case X86_VENDOR_SiS:
18330Sstevel@tonic-gate 		if (cpi->cpi_family == 5 && cpi->cpi_model == 0)
18340Sstevel@tonic-gate 			brand = "SiS 55x";
18350Sstevel@tonic-gate 		break;
18360Sstevel@tonic-gate 	case X86_VENDOR_TM:
18370Sstevel@tonic-gate 		if (cpi->cpi_family == 5 && cpi->cpi_model == 4)
18380Sstevel@tonic-gate 			brand = "Transmeta Crusoe TM3x00 or TM5x00";
18390Sstevel@tonic-gate 		break;
18400Sstevel@tonic-gate 	case X86_VENDOR_NSC:
18410Sstevel@tonic-gate 	case X86_VENDOR_UMC:
18420Sstevel@tonic-gate 	default:
18430Sstevel@tonic-gate 		break;
18440Sstevel@tonic-gate 	}
18450Sstevel@tonic-gate 	if (brand) {
18460Sstevel@tonic-gate 		(void) strcpy((char *)cpi->cpi_brandstr, brand);
18470Sstevel@tonic-gate 		return;
18480Sstevel@tonic-gate 	}
18490Sstevel@tonic-gate 
18500Sstevel@tonic-gate 	/*
18510Sstevel@tonic-gate 	 * If all else fails ...
18520Sstevel@tonic-gate 	 */
18530Sstevel@tonic-gate 	(void) snprintf(cpi->cpi_brandstr, sizeof (cpi->cpi_brandstr),
18540Sstevel@tonic-gate 	    "%s %d.%d.%d", cpi->cpi_vendorstr, cpi->cpi_family,
18550Sstevel@tonic-gate 	    cpi->cpi_model, cpi->cpi_step);
18560Sstevel@tonic-gate }
18570Sstevel@tonic-gate 
18580Sstevel@tonic-gate /*
18590Sstevel@tonic-gate  * This routine is called just after kernel memory allocation
18600Sstevel@tonic-gate  * becomes available on cpu0, and as part of mp_startup() on
18610Sstevel@tonic-gate  * the other cpus.
18620Sstevel@tonic-gate  *
18634606Sesaxe  * Fixup the brand string, and collect any information from cpuid
18644606Sesaxe  * that requires dynamicically allocated storage to represent.
18650Sstevel@tonic-gate  */
18660Sstevel@tonic-gate /*ARGSUSED*/
18670Sstevel@tonic-gate void
18680Sstevel@tonic-gate cpuid_pass3(cpu_t *cpu)
18690Sstevel@tonic-gate {
18704606Sesaxe 	int	i, max, shft, level, size;
18714606Sesaxe 	struct cpuid_regs regs;
18724606Sesaxe 	struct cpuid_regs *cp;
18730Sstevel@tonic-gate 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
18740Sstevel@tonic-gate 
18750Sstevel@tonic-gate 	ASSERT(cpi->cpi_pass == 2);
18760Sstevel@tonic-gate 
18774606Sesaxe 	/*
18784606Sesaxe 	 * Function 4: Deterministic cache parameters
18794606Sesaxe 	 *
18804606Sesaxe 	 * Take this opportunity to detect the number of threads
18814606Sesaxe 	 * sharing the last level cache, and construct a corresponding
18824606Sesaxe 	 * cache id. The respective cpuid_info members are initialized
18834606Sesaxe 	 * to the default case of "no last level cache sharing".
18844606Sesaxe 	 */
18854606Sesaxe 	cpi->cpi_ncpu_shr_last_cache = 1;
18864606Sesaxe 	cpi->cpi_last_lvl_cacheid = cpu->cpu_id;
18874606Sesaxe 
18884606Sesaxe 	if (cpi->cpi_maxeax >= 4 && cpi->cpi_vendor == X86_VENDOR_Intel) {
18894606Sesaxe 
18904606Sesaxe 		/*
18914606Sesaxe 		 * Find the # of elements (size) returned by fn 4, and along
18924606Sesaxe 		 * the way detect last level cache sharing details.
18934606Sesaxe 		 */
18944606Sesaxe 		bzero(&regs, sizeof (regs));
18954606Sesaxe 		cp = &regs;
18964606Sesaxe 		for (i = 0, max = 0; i < CPI_FN4_ECX_MAX; i++) {
18974606Sesaxe 			cp->cp_eax = 4;
18984606Sesaxe 			cp->cp_ecx = i;
18994606Sesaxe 
19004606Sesaxe 			(void) __cpuid_insn(cp);
19014606Sesaxe 
19024606Sesaxe 			if (CPI_CACHE_TYPE(cp) == 0)
19034606Sesaxe 				break;
19044606Sesaxe 			level = CPI_CACHE_LVL(cp);
19054606Sesaxe 			if (level > max) {
19064606Sesaxe 				max = level;
19074606Sesaxe 				cpi->cpi_ncpu_shr_last_cache =
19084606Sesaxe 				    CPI_NTHR_SHR_CACHE(cp) + 1;
19094606Sesaxe 			}
19104606Sesaxe 		}
19114606Sesaxe 		cpi->cpi_std_4_size = size = i;
19124606Sesaxe 
19134606Sesaxe 		/*
19144606Sesaxe 		 * Allocate the cpi_std_4 array. The first element
19154606Sesaxe 		 * references the regs for fn 4, %ecx == 0, which
19164606Sesaxe 		 * cpuid_pass2() stashed in cpi->cpi_std[4].
19174606Sesaxe 		 */
19184606Sesaxe 		if (size > 0) {
19194606Sesaxe 			cpi->cpi_std_4 =
19204606Sesaxe 			    kmem_alloc(size * sizeof (cp), KM_SLEEP);
19214606Sesaxe 			cpi->cpi_std_4[0] = &cpi->cpi_std[4];
19224606Sesaxe 
19234606Sesaxe 			/*
19244606Sesaxe 			 * Allocate storage to hold the additional regs
19254606Sesaxe 			 * for function 4, %ecx == 1 .. cpi_std_4_size.
19264606Sesaxe 			 *
19274606Sesaxe 			 * The regs for fn 4, %ecx == 0 has already
19284606Sesaxe 			 * been allocated as indicated above.
19294606Sesaxe 			 */
19304606Sesaxe 			for (i = 1; i < size; i++) {
19314606Sesaxe 				cp = cpi->cpi_std_4[i] =
19324606Sesaxe 				    kmem_zalloc(sizeof (regs), KM_SLEEP);
19334606Sesaxe 				cp->cp_eax = 4;
19344606Sesaxe 				cp->cp_ecx = i;
19354606Sesaxe 
19364606Sesaxe 				(void) __cpuid_insn(cp);
19374606Sesaxe 			}
19384606Sesaxe 		}
19394606Sesaxe 		/*
19404606Sesaxe 		 * Determine the number of bits needed to represent
19414606Sesaxe 		 * the number of CPUs sharing the last level cache.
19424606Sesaxe 		 *
19434606Sesaxe 		 * Shift off that number of bits from the APIC id to
19444606Sesaxe 		 * derive the cache id.
19454606Sesaxe 		 */
19464606Sesaxe 		shft = 0;
19474606Sesaxe 		for (i = 1; i < cpi->cpi_ncpu_shr_last_cache; i <<= 1)
19484606Sesaxe 			shft++;
19497282Smishra 		cpi->cpi_last_lvl_cacheid = cpi->cpi_apicid >> shft;
19500Sstevel@tonic-gate 	}
19510Sstevel@tonic-gate 
19520Sstevel@tonic-gate 	/*
19534606Sesaxe 	 * Now fixup the brand string
19540Sstevel@tonic-gate 	 */
19554606Sesaxe 	if ((cpi->cpi_xmaxeax & 0x80000000) == 0) {
19564606Sesaxe 		fabricate_brandstr(cpi);
19574606Sesaxe 	} else {
19580Sstevel@tonic-gate 
19590Sstevel@tonic-gate 		/*
19604606Sesaxe 		 * If we successfully extracted a brand string from the cpuid
19614606Sesaxe 		 * instruction, clean it up by removing leading spaces and
19624606Sesaxe 		 * similar junk.
19630Sstevel@tonic-gate 		 */
19644606Sesaxe 		if (cpi->cpi_brandstr[0]) {
19654606Sesaxe 			size_t maxlen = sizeof (cpi->cpi_brandstr);
19664606Sesaxe 			char *src, *dst;
19674606Sesaxe 
19684606Sesaxe 			dst = src = (char *)cpi->cpi_brandstr;
19694606Sesaxe 			src[maxlen - 1] = '\0';
19704606Sesaxe 			/*
19714606Sesaxe 			 * strip leading spaces
19724606Sesaxe 			 */
19734606Sesaxe 			while (*src == ' ')
19744606Sesaxe 				src++;
19754606Sesaxe 			/*
19764606Sesaxe 			 * Remove any 'Genuine' or "Authentic" prefixes
19774606Sesaxe 			 */
19784606Sesaxe 			if (strncmp(src, "Genuine ", 8) == 0)
19794606Sesaxe 				src += 8;
19804606Sesaxe 			if (strncmp(src, "Authentic ", 10) == 0)
19814606Sesaxe 				src += 10;
19824606Sesaxe 
19834606Sesaxe 			/*
19844606Sesaxe 			 * Now do an in-place copy.
19854606Sesaxe 			 * Map (R) to (r) and (TM) to (tm).
19864606Sesaxe 			 * The era of teletypes is long gone, and there's
19874606Sesaxe 			 * -really- no need to shout.
19884606Sesaxe 			 */
19894606Sesaxe 			while (*src != '\0') {
19904606Sesaxe 				if (src[0] == '(') {
19914606Sesaxe 					if (strncmp(src + 1, "R)", 2) == 0) {
19924606Sesaxe 						(void) strncpy(dst, "(r)", 3);
19934606Sesaxe 						src += 3;
19944606Sesaxe 						dst += 3;
19954606Sesaxe 						continue;
19964606Sesaxe 					}
19974606Sesaxe 					if (strncmp(src + 1, "TM)", 3) == 0) {
19984606Sesaxe 						(void) strncpy(dst, "(tm)", 4);
19994606Sesaxe 						src += 4;
20004606Sesaxe 						dst += 4;
20014606Sesaxe 						continue;
20024606Sesaxe 					}
20030Sstevel@tonic-gate 				}
20044606Sesaxe 				*dst++ = *src++;
20050Sstevel@tonic-gate 			}
20064606Sesaxe 			*dst = '\0';
20074606Sesaxe 
20084606Sesaxe 			/*
20094606Sesaxe 			 * Finally, remove any trailing spaces
20104606Sesaxe 			 */
20114606Sesaxe 			while (--dst > cpi->cpi_brandstr)
20124606Sesaxe 				if (*dst == ' ')
20134606Sesaxe 					*dst = '\0';
20144606Sesaxe 				else
20154606Sesaxe 					break;
20164606Sesaxe 		} else
20174606Sesaxe 			fabricate_brandstr(cpi);
20184606Sesaxe 	}
20190Sstevel@tonic-gate 	cpi->cpi_pass = 3;
20200Sstevel@tonic-gate }
20210Sstevel@tonic-gate 
20220Sstevel@tonic-gate /*
20230Sstevel@tonic-gate  * This routine is called out of bind_hwcap() much later in the life
20240Sstevel@tonic-gate  * of the kernel (post_startup()).  The job of this routine is to resolve
20250Sstevel@tonic-gate  * the hardware feature support and kernel support for those features into
20260Sstevel@tonic-gate  * what we're actually going to tell applications via the aux vector.
20270Sstevel@tonic-gate  */
20280Sstevel@tonic-gate uint_t
20290Sstevel@tonic-gate cpuid_pass4(cpu_t *cpu)
20300Sstevel@tonic-gate {
20310Sstevel@tonic-gate 	struct cpuid_info *cpi;
20320Sstevel@tonic-gate 	uint_t hwcap_flags = 0;
20330Sstevel@tonic-gate 
20340Sstevel@tonic-gate 	if (cpu == NULL)
20350Sstevel@tonic-gate 		cpu = CPU;
20360Sstevel@tonic-gate 	cpi = cpu->cpu_m.mcpu_cpi;
20370Sstevel@tonic-gate 
20380Sstevel@tonic-gate 	ASSERT(cpi->cpi_pass == 3);
20390Sstevel@tonic-gate 
20400Sstevel@tonic-gate 	if (cpi->cpi_maxeax >= 1) {
20410Sstevel@tonic-gate 		uint32_t *edx = &cpi->cpi_support[STD_EDX_FEATURES];
20420Sstevel@tonic-gate 		uint32_t *ecx = &cpi->cpi_support[STD_ECX_FEATURES];
20430Sstevel@tonic-gate 
20440Sstevel@tonic-gate 		*edx = CPI_FEATURES_EDX(cpi);
20450Sstevel@tonic-gate 		*ecx = CPI_FEATURES_ECX(cpi);
20460Sstevel@tonic-gate 
20470Sstevel@tonic-gate 		/*
20480Sstevel@tonic-gate 		 * [these require explicit kernel support]
20490Sstevel@tonic-gate 		 */
20500Sstevel@tonic-gate 		if ((x86_feature & X86_SEP) == 0)
20510Sstevel@tonic-gate 			*edx &= ~CPUID_INTC_EDX_SEP;
20520Sstevel@tonic-gate 
20530Sstevel@tonic-gate 		if ((x86_feature & X86_SSE) == 0)
20540Sstevel@tonic-gate 			*edx &= ~(CPUID_INTC_EDX_FXSR|CPUID_INTC_EDX_SSE);
20550Sstevel@tonic-gate 		if ((x86_feature & X86_SSE2) == 0)
20560Sstevel@tonic-gate 			*edx &= ~CPUID_INTC_EDX_SSE2;
20570Sstevel@tonic-gate 
20580Sstevel@tonic-gate 		if ((x86_feature & X86_HTT) == 0)
20590Sstevel@tonic-gate 			*edx &= ~CPUID_INTC_EDX_HTT;
20600Sstevel@tonic-gate 
20610Sstevel@tonic-gate 		if ((x86_feature & X86_SSE3) == 0)
20620Sstevel@tonic-gate 			*ecx &= ~CPUID_INTC_ECX_SSE3;
20630Sstevel@tonic-gate 
20645269Skk208521 		if (cpi->cpi_vendor == X86_VENDOR_Intel) {
20655269Skk208521 			if ((x86_feature & X86_SSSE3) == 0)
20665269Skk208521 				*ecx &= ~CPUID_INTC_ECX_SSSE3;
20675269Skk208521 			if ((x86_feature & X86_SSE4_1) == 0)
20685269Skk208521 				*ecx &= ~CPUID_INTC_ECX_SSE4_1;
20695269Skk208521 			if ((x86_feature & X86_SSE4_2) == 0)
20705269Skk208521 				*ecx &= ~CPUID_INTC_ECX_SSE4_2;
20715269Skk208521 		}
20725269Skk208521 
20730Sstevel@tonic-gate 		/*
20740Sstevel@tonic-gate 		 * [no explicit support required beyond x87 fp context]
20750Sstevel@tonic-gate 		 */
20760Sstevel@tonic-gate 		if (!fpu_exists)
20770Sstevel@tonic-gate 			*edx &= ~(CPUID_INTC_EDX_FPU | CPUID_INTC_EDX_MMX);
20780Sstevel@tonic-gate 
20790Sstevel@tonic-gate 		/*
20800Sstevel@tonic-gate 		 * Now map the supported feature vector to things that we
20810Sstevel@tonic-gate 		 * think userland will care about.
20820Sstevel@tonic-gate 		 */
20830Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_SEP)
20840Sstevel@tonic-gate 			hwcap_flags |= AV_386_SEP;
20850Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_SSE)
20860Sstevel@tonic-gate 			hwcap_flags |= AV_386_FXSR | AV_386_SSE;
20870Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_SSE2)
20880Sstevel@tonic-gate 			hwcap_flags |= AV_386_SSE2;
20890Sstevel@tonic-gate 		if (*ecx & CPUID_INTC_ECX_SSE3)
20900Sstevel@tonic-gate 			hwcap_flags |= AV_386_SSE3;
20915269Skk208521 		if (cpi->cpi_vendor == X86_VENDOR_Intel) {
20925269Skk208521 			if (*ecx & CPUID_INTC_ECX_SSSE3)
20935269Skk208521 				hwcap_flags |= AV_386_SSSE3;
20945269Skk208521 			if (*ecx & CPUID_INTC_ECX_SSE4_1)
20955269Skk208521 				hwcap_flags |= AV_386_SSE4_1;
20965269Skk208521 			if (*ecx & CPUID_INTC_ECX_SSE4_2)
20975269Skk208521 				hwcap_flags |= AV_386_SSE4_2;
20988418SKrishnendu.Sadhukhan@Sun.COM 			if (*ecx & CPUID_INTC_ECX_MOVBE)
20998418SKrishnendu.Sadhukhan@Sun.COM 				hwcap_flags |= AV_386_MOVBE;
21005269Skk208521 		}
21014628Skk208521 		if (*ecx & CPUID_INTC_ECX_POPCNT)
21024628Skk208521 			hwcap_flags |= AV_386_POPCNT;
21030Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_FPU)
21040Sstevel@tonic-gate 			hwcap_flags |= AV_386_FPU;
21050Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_MMX)
21060Sstevel@tonic-gate 			hwcap_flags |= AV_386_MMX;
21070Sstevel@tonic-gate 
21080Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_TSC)
21090Sstevel@tonic-gate 			hwcap_flags |= AV_386_TSC;
21100Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_CX8)
21110Sstevel@tonic-gate 			hwcap_flags |= AV_386_CX8;
21120Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_CMOV)
21130Sstevel@tonic-gate 			hwcap_flags |= AV_386_CMOV;
21140Sstevel@tonic-gate 		if (*ecx & CPUID_INTC_ECX_MON)
21150Sstevel@tonic-gate 			hwcap_flags |= AV_386_MON;
21160Sstevel@tonic-gate 		if (*ecx & CPUID_INTC_ECX_CX16)
21170Sstevel@tonic-gate 			hwcap_flags |= AV_386_CX16;
21180Sstevel@tonic-gate 	}
21190Sstevel@tonic-gate 
21201228Sandrei 	if (x86_feature & X86_HTT)
21210Sstevel@tonic-gate 		hwcap_flags |= AV_386_PAUSE;
21220Sstevel@tonic-gate 
21230Sstevel@tonic-gate 	if (cpi->cpi_xmaxeax < 0x80000001)
21240Sstevel@tonic-gate 		goto pass4_done;
21250Sstevel@tonic-gate 
21260Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
21271228Sandrei 		struct cpuid_regs cp;
21283446Smrj 		uint32_t *edx, *ecx;
21290Sstevel@tonic-gate 
21303446Smrj 	case X86_VENDOR_Intel:
21313446Smrj 		/*
21323446Smrj 		 * Seems like Intel duplicated what we necessary
21333446Smrj 		 * here to make the initial crop of 64-bit OS's work.
21343446Smrj 		 * Hopefully, those are the only "extended" bits
21353446Smrj 		 * they'll add.
21363446Smrj 		 */
21373446Smrj 		/*FALLTHROUGH*/
21383446Smrj 
21390Sstevel@tonic-gate 	case X86_VENDOR_AMD:
21400Sstevel@tonic-gate 		edx = &cpi->cpi_support[AMD_EDX_FEATURES];
21413446Smrj 		ecx = &cpi->cpi_support[AMD_ECX_FEATURES];
21420Sstevel@tonic-gate 
21430Sstevel@tonic-gate 		*edx = CPI_FEATURES_XTD_EDX(cpi);
21443446Smrj 		*ecx = CPI_FEATURES_XTD_ECX(cpi);
21453446Smrj 
21463446Smrj 		/*
21473446Smrj 		 * [these features require explicit kernel support]
21483446Smrj 		 */
21493446Smrj 		switch (cpi->cpi_vendor) {
21503446Smrj 		case X86_VENDOR_Intel:
21516657Ssudheer 			if ((x86_feature & X86_TSCP) == 0)
21526657Ssudheer 				*edx &= ~CPUID_AMD_EDX_TSCP;
21533446Smrj 			break;
21543446Smrj 
21553446Smrj 		case X86_VENDOR_AMD:
21563446Smrj 			if ((x86_feature & X86_TSCP) == 0)
21573446Smrj 				*edx &= ~CPUID_AMD_EDX_TSCP;
21584628Skk208521 			if ((x86_feature & X86_SSE4A) == 0)
21594628Skk208521 				*ecx &= ~CPUID_AMD_ECX_SSE4A;
21603446Smrj 			break;
21613446Smrj 
21623446Smrj 		default:
21633446Smrj 			break;
21643446Smrj 		}
21650Sstevel@tonic-gate 
21660Sstevel@tonic-gate 		/*
21670Sstevel@tonic-gate 		 * [no explicit support required beyond
21680Sstevel@tonic-gate 		 * x87 fp context and exception handlers]
21690Sstevel@tonic-gate 		 */
21700Sstevel@tonic-gate 		if (!fpu_exists)
21710Sstevel@tonic-gate 			*edx &= ~(CPUID_AMD_EDX_MMXamd |
21720Sstevel@tonic-gate 			    CPUID_AMD_EDX_3DNow | CPUID_AMD_EDX_3DNowx);
21730Sstevel@tonic-gate 
21740Sstevel@tonic-gate 		if ((x86_feature & X86_NX) == 0)
21750Sstevel@tonic-gate 			*edx &= ~CPUID_AMD_EDX_NX;
21763446Smrj #if !defined(__amd64)
21770Sstevel@tonic-gate 		*edx &= ~CPUID_AMD_EDX_LM;
21780Sstevel@tonic-gate #endif
21790Sstevel@tonic-gate 		/*
21800Sstevel@tonic-gate 		 * Now map the supported feature vector to
21810Sstevel@tonic-gate 		 * things that we think userland will care about.
21820Sstevel@tonic-gate 		 */
21833446Smrj #if defined(__amd64)
21840Sstevel@tonic-gate 		if (*edx & CPUID_AMD_EDX_SYSC)
21850Sstevel@tonic-gate 			hwcap_flags |= AV_386_AMD_SYSC;
21863446Smrj #endif
21870Sstevel@tonic-gate 		if (*edx & CPUID_AMD_EDX_MMXamd)
21880Sstevel@tonic-gate 			hwcap_flags |= AV_386_AMD_MMX;
21890Sstevel@tonic-gate 		if (*edx & CPUID_AMD_EDX_3DNow)
21900Sstevel@tonic-gate 			hwcap_flags |= AV_386_AMD_3DNow;
21910Sstevel@tonic-gate 		if (*edx & CPUID_AMD_EDX_3DNowx)
21920Sstevel@tonic-gate 			hwcap_flags |= AV_386_AMD_3DNowx;
21933446Smrj 
21943446Smrj 		switch (cpi->cpi_vendor) {
21953446Smrj 		case X86_VENDOR_AMD:
21963446Smrj 			if (*edx & CPUID_AMD_EDX_TSCP)
21973446Smrj 				hwcap_flags |= AV_386_TSCP;
21983446Smrj 			if (*ecx & CPUID_AMD_ECX_AHF64)
21993446Smrj 				hwcap_flags |= AV_386_AHF;
22004628Skk208521 			if (*ecx & CPUID_AMD_ECX_SSE4A)
22014628Skk208521 				hwcap_flags |= AV_386_AMD_SSE4A;
22024628Skk208521 			if (*ecx & CPUID_AMD_ECX_LZCNT)
22034628Skk208521 				hwcap_flags |= AV_386_AMD_LZCNT;
22043446Smrj 			break;
22053446Smrj 
22063446Smrj 		case X86_VENDOR_Intel:
22076657Ssudheer 			if (*edx & CPUID_AMD_EDX_TSCP)
22086657Ssudheer 				hwcap_flags |= AV_386_TSCP;
22093446Smrj 			/*
22103446Smrj 			 * Aarrgh.
22113446Smrj 			 * Intel uses a different bit in the same word.
22123446Smrj 			 */
22133446Smrj 			if (*ecx & CPUID_INTC_ECX_AHF64)
22143446Smrj 				hwcap_flags |= AV_386_AHF;
22153446Smrj 			break;
22163446Smrj 
22173446Smrj 		default:
22183446Smrj 			break;
22193446Smrj 		}
22200Sstevel@tonic-gate 		break;
22210Sstevel@tonic-gate 
22220Sstevel@tonic-gate 	case X86_VENDOR_TM:
22231228Sandrei 		cp.cp_eax = 0x80860001;
22241228Sandrei 		(void) __cpuid_insn(&cp);
22251228Sandrei 		cpi->cpi_support[TM_EDX_FEATURES] = cp.cp_edx;
22260Sstevel@tonic-gate 		break;
22270Sstevel@tonic-gate 
22280Sstevel@tonic-gate 	default:
22290Sstevel@tonic-gate 		break;
22300Sstevel@tonic-gate 	}
22310Sstevel@tonic-gate 
22320Sstevel@tonic-gate pass4_done:
22330Sstevel@tonic-gate 	cpi->cpi_pass = 4;
22340Sstevel@tonic-gate 	return (hwcap_flags);
22350Sstevel@tonic-gate }
22360Sstevel@tonic-gate 
22370Sstevel@tonic-gate 
22380Sstevel@tonic-gate /*
22390Sstevel@tonic-gate  * Simulate the cpuid instruction using the data we previously
22400Sstevel@tonic-gate  * captured about this CPU.  We try our best to return the truth
22410Sstevel@tonic-gate  * about the hardware, independently of kernel support.
22420Sstevel@tonic-gate  */
22430Sstevel@tonic-gate uint32_t
22441228Sandrei cpuid_insn(cpu_t *cpu, struct cpuid_regs *cp)
22450Sstevel@tonic-gate {
22460Sstevel@tonic-gate 	struct cpuid_info *cpi;
22471228Sandrei 	struct cpuid_regs *xcp;
22480Sstevel@tonic-gate 
22490Sstevel@tonic-gate 	if (cpu == NULL)
22500Sstevel@tonic-gate 		cpu = CPU;
22510Sstevel@tonic-gate 	cpi = cpu->cpu_m.mcpu_cpi;
22520Sstevel@tonic-gate 
22530Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 3));
22540Sstevel@tonic-gate 
22550Sstevel@tonic-gate 	/*
22560Sstevel@tonic-gate 	 * CPUID data is cached in two separate places: cpi_std for standard
22570Sstevel@tonic-gate 	 * CPUID functions, and cpi_extd for extended CPUID functions.
22580Sstevel@tonic-gate 	 */
22591228Sandrei 	if (cp->cp_eax <= cpi->cpi_maxeax && cp->cp_eax < NMAX_CPI_STD)
22601228Sandrei 		xcp = &cpi->cpi_std[cp->cp_eax];
22611228Sandrei 	else if (cp->cp_eax >= 0x80000000 && cp->cp_eax <= cpi->cpi_xmaxeax &&
22621228Sandrei 	    cp->cp_eax < 0x80000000 + NMAX_CPI_EXTD)
22631228Sandrei 		xcp = &cpi->cpi_extd[cp->cp_eax - 0x80000000];
22640Sstevel@tonic-gate 	else
22650Sstevel@tonic-gate 		/*
22660Sstevel@tonic-gate 		 * The caller is asking for data from an input parameter which
22670Sstevel@tonic-gate 		 * the kernel has not cached.  In this case we go fetch from
22680Sstevel@tonic-gate 		 * the hardware and return the data directly to the user.
22690Sstevel@tonic-gate 		 */
22701228Sandrei 		return (__cpuid_insn(cp));
22711228Sandrei 
22721228Sandrei 	cp->cp_eax = xcp->cp_eax;
22731228Sandrei 	cp->cp_ebx = xcp->cp_ebx;
22741228Sandrei 	cp->cp_ecx = xcp->cp_ecx;
22751228Sandrei 	cp->cp_edx = xcp->cp_edx;
22760Sstevel@tonic-gate 	return (cp->cp_eax);
22770Sstevel@tonic-gate }
22780Sstevel@tonic-gate 
22790Sstevel@tonic-gate int
22800Sstevel@tonic-gate cpuid_checkpass(cpu_t *cpu, int pass)
22810Sstevel@tonic-gate {
22820Sstevel@tonic-gate 	return (cpu != NULL && cpu->cpu_m.mcpu_cpi != NULL &&
22830Sstevel@tonic-gate 	    cpu->cpu_m.mcpu_cpi->cpi_pass >= pass);
22840Sstevel@tonic-gate }
22850Sstevel@tonic-gate 
22860Sstevel@tonic-gate int
22870Sstevel@tonic-gate cpuid_getbrandstr(cpu_t *cpu, char *s, size_t n)
22880Sstevel@tonic-gate {
22890Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 3));
22900Sstevel@tonic-gate 
22910Sstevel@tonic-gate 	return (snprintf(s, n, "%s", cpu->cpu_m.mcpu_cpi->cpi_brandstr));
22920Sstevel@tonic-gate }
22930Sstevel@tonic-gate 
22940Sstevel@tonic-gate int
22951228Sandrei cpuid_is_cmt(cpu_t *cpu)
22960Sstevel@tonic-gate {
22970Sstevel@tonic-gate 	if (cpu == NULL)
22980Sstevel@tonic-gate 		cpu = CPU;
22990Sstevel@tonic-gate 
23000Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
23010Sstevel@tonic-gate 
23020Sstevel@tonic-gate 	return (cpu->cpu_m.mcpu_cpi->cpi_chipid >= 0);
23030Sstevel@tonic-gate }
23040Sstevel@tonic-gate 
23050Sstevel@tonic-gate /*
23060Sstevel@tonic-gate  * AMD and Intel both implement the 64-bit variant of the syscall
23070Sstevel@tonic-gate  * instruction (syscallq), so if there's -any- support for syscall,
23080Sstevel@tonic-gate  * cpuid currently says "yes, we support this".
23090Sstevel@tonic-gate  *
23100Sstevel@tonic-gate  * However, Intel decided to -not- implement the 32-bit variant of the
23110Sstevel@tonic-gate  * syscall instruction, so we provide a predicate to allow our caller
23120Sstevel@tonic-gate  * to test that subtlety here.
23135084Sjohnlev  *
23145084Sjohnlev  * XXPV	Currently, 32-bit syscall instructions don't work via the hypervisor,
23155084Sjohnlev  *	even in the case where the hardware would in fact support it.
23160Sstevel@tonic-gate  */
23170Sstevel@tonic-gate /*ARGSUSED*/
23180Sstevel@tonic-gate int
23190Sstevel@tonic-gate cpuid_syscall32_insn(cpu_t *cpu)
23200Sstevel@tonic-gate {
23210Sstevel@tonic-gate 	ASSERT(cpuid_checkpass((cpu == NULL ? CPU : cpu), 1));
23220Sstevel@tonic-gate 
23235084Sjohnlev #if !defined(__xpv)
23243446Smrj 	if (cpu == NULL)
23253446Smrj 		cpu = CPU;
23263446Smrj 
23273446Smrj 	/*CSTYLED*/
23283446Smrj 	{
23293446Smrj 		struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
23303446Smrj 
23313446Smrj 		if (cpi->cpi_vendor == X86_VENDOR_AMD &&
23323446Smrj 		    cpi->cpi_xmaxeax >= 0x80000001 &&
23333446Smrj 		    (CPI_FEATURES_XTD_EDX(cpi) & CPUID_AMD_EDX_SYSC))
23343446Smrj 			return (1);
23353446Smrj 	}
23365084Sjohnlev #endif
23370Sstevel@tonic-gate 	return (0);
23380Sstevel@tonic-gate }
23390Sstevel@tonic-gate 
23400Sstevel@tonic-gate int
23410Sstevel@tonic-gate cpuid_getidstr(cpu_t *cpu, char *s, size_t n)
23420Sstevel@tonic-gate {
23430Sstevel@tonic-gate 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
23440Sstevel@tonic-gate 
23450Sstevel@tonic-gate 	static const char fmt[] =
23463779Sdmick 	    "x86 (%s %X family %d model %d step %d clock %d MHz)";
23470Sstevel@tonic-gate 	static const char fmt_ht[] =
23483779Sdmick 	    "x86 (chipid 0x%x %s %X family %d model %d step %d clock %d MHz)";
23490Sstevel@tonic-gate 
23500Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
23510Sstevel@tonic-gate 
23521228Sandrei 	if (cpuid_is_cmt(cpu))
23530Sstevel@tonic-gate 		return (snprintf(s, n, fmt_ht, cpi->cpi_chipid,
23543779Sdmick 		    cpi->cpi_vendorstr, cpi->cpi_std[1].cp_eax,
23553779Sdmick 		    cpi->cpi_family, cpi->cpi_model,
23560Sstevel@tonic-gate 		    cpi->cpi_step, cpu->cpu_type_info.pi_clock));
23570Sstevel@tonic-gate 	return (snprintf(s, n, fmt,
23583779Sdmick 	    cpi->cpi_vendorstr, cpi->cpi_std[1].cp_eax,
23593779Sdmick 	    cpi->cpi_family, cpi->cpi_model,
23600Sstevel@tonic-gate 	    cpi->cpi_step, cpu->cpu_type_info.pi_clock));
23610Sstevel@tonic-gate }
23620Sstevel@tonic-gate 
23630Sstevel@tonic-gate const char *
23640Sstevel@tonic-gate cpuid_getvendorstr(cpu_t *cpu)
23650Sstevel@tonic-gate {
23660Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
23670Sstevel@tonic-gate 	return ((const char *)cpu->cpu_m.mcpu_cpi->cpi_vendorstr);
23680Sstevel@tonic-gate }
23690Sstevel@tonic-gate 
23700Sstevel@tonic-gate uint_t
23710Sstevel@tonic-gate cpuid_getvendor(cpu_t *cpu)
23720Sstevel@tonic-gate {
23730Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
23740Sstevel@tonic-gate 	return (cpu->cpu_m.mcpu_cpi->cpi_vendor);
23750Sstevel@tonic-gate }
23760Sstevel@tonic-gate 
23770Sstevel@tonic-gate uint_t
23780Sstevel@tonic-gate cpuid_getfamily(cpu_t *cpu)
23790Sstevel@tonic-gate {
23800Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
23810Sstevel@tonic-gate 	return (cpu->cpu_m.mcpu_cpi->cpi_family);
23820Sstevel@tonic-gate }
23830Sstevel@tonic-gate 
23840Sstevel@tonic-gate uint_t
23850Sstevel@tonic-gate cpuid_getmodel(cpu_t *cpu)
23860Sstevel@tonic-gate {
23870Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
23880Sstevel@tonic-gate 	return (cpu->cpu_m.mcpu_cpi->cpi_model);
23890Sstevel@tonic-gate }
23900Sstevel@tonic-gate 
23910Sstevel@tonic-gate uint_t
23920Sstevel@tonic-gate cpuid_get_ncpu_per_chip(cpu_t *cpu)
23930Sstevel@tonic-gate {
23940Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
23950Sstevel@tonic-gate 	return (cpu->cpu_m.mcpu_cpi->cpi_ncpu_per_chip);
23960Sstevel@tonic-gate }
23970Sstevel@tonic-gate 
23980Sstevel@tonic-gate uint_t
23991228Sandrei cpuid_get_ncore_per_chip(cpu_t *cpu)
24001228Sandrei {
24011228Sandrei 	ASSERT(cpuid_checkpass(cpu, 1));
24021228Sandrei 	return (cpu->cpu_m.mcpu_cpi->cpi_ncore_per_chip);
24031228Sandrei }
24041228Sandrei 
24051228Sandrei uint_t
24064606Sesaxe cpuid_get_ncpu_sharing_last_cache(cpu_t *cpu)
24074606Sesaxe {
24084606Sesaxe 	ASSERT(cpuid_checkpass(cpu, 2));
24094606Sesaxe 	return (cpu->cpu_m.mcpu_cpi->cpi_ncpu_shr_last_cache);
24104606Sesaxe }
24114606Sesaxe 
24124606Sesaxe id_t
24134606Sesaxe cpuid_get_last_lvl_cacheid(cpu_t *cpu)
24144606Sesaxe {
24154606Sesaxe 	ASSERT(cpuid_checkpass(cpu, 2));
24164606Sesaxe 	return (cpu->cpu_m.mcpu_cpi->cpi_last_lvl_cacheid);
24174606Sesaxe }
24184606Sesaxe 
24194606Sesaxe uint_t
24200Sstevel@tonic-gate cpuid_getstep(cpu_t *cpu)
24210Sstevel@tonic-gate {
24220Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
24230Sstevel@tonic-gate 	return (cpu->cpu_m.mcpu_cpi->cpi_step);
24240Sstevel@tonic-gate }
24250Sstevel@tonic-gate 
24264581Ssherrym uint_t
24274581Ssherrym cpuid_getsig(struct cpu *cpu)
24284581Ssherrym {
24294581Ssherrym 	ASSERT(cpuid_checkpass(cpu, 1));
24304581Ssherrym 	return (cpu->cpu_m.mcpu_cpi->cpi_std[1].cp_eax);
24314581Ssherrym }
24324581Ssherrym 
24332869Sgavinm uint32_t
24342869Sgavinm cpuid_getchiprev(struct cpu *cpu)
24352869Sgavinm {
24362869Sgavinm 	ASSERT(cpuid_checkpass(cpu, 1));
24372869Sgavinm 	return (cpu->cpu_m.mcpu_cpi->cpi_chiprev);
24382869Sgavinm }
24392869Sgavinm 
24402869Sgavinm const char *
24412869Sgavinm cpuid_getchiprevstr(struct cpu *cpu)
24422869Sgavinm {
24432869Sgavinm 	ASSERT(cpuid_checkpass(cpu, 1));
24442869Sgavinm 	return (cpu->cpu_m.mcpu_cpi->cpi_chiprevstr);
24452869Sgavinm }
24462869Sgavinm 
24472869Sgavinm uint32_t
24482869Sgavinm cpuid_getsockettype(struct cpu *cpu)
24492869Sgavinm {
24502869Sgavinm 	ASSERT(cpuid_checkpass(cpu, 1));
24512869Sgavinm 	return (cpu->cpu_m.mcpu_cpi->cpi_socket);
24522869Sgavinm }
24532869Sgavinm 
24543434Sesaxe int
24553434Sesaxe cpuid_get_chipid(cpu_t *cpu)
24560Sstevel@tonic-gate {
24570Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
24580Sstevel@tonic-gate 
24591228Sandrei 	if (cpuid_is_cmt(cpu))
24600Sstevel@tonic-gate 		return (cpu->cpu_m.mcpu_cpi->cpi_chipid);
24610Sstevel@tonic-gate 	return (cpu->cpu_id);
24620Sstevel@tonic-gate }
24630Sstevel@tonic-gate 
24641228Sandrei id_t
24653434Sesaxe cpuid_get_coreid(cpu_t *cpu)
24661228Sandrei {
24671228Sandrei 	ASSERT(cpuid_checkpass(cpu, 1));
24681228Sandrei 	return (cpu->cpu_m.mcpu_cpi->cpi_coreid);
24691228Sandrei }
24701228Sandrei 
24710Sstevel@tonic-gate int
24725870Sgavinm cpuid_get_pkgcoreid(cpu_t *cpu)
24735870Sgavinm {
24745870Sgavinm 	ASSERT(cpuid_checkpass(cpu, 1));
24755870Sgavinm 	return (cpu->cpu_m.mcpu_cpi->cpi_pkgcoreid);
24765870Sgavinm }
24775870Sgavinm 
24785870Sgavinm int
24793434Sesaxe cpuid_get_clogid(cpu_t *cpu)
24800Sstevel@tonic-gate {
24810Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
24820Sstevel@tonic-gate 	return (cpu->cpu_m.mcpu_cpi->cpi_clogid);
24830Sstevel@tonic-gate }
24840Sstevel@tonic-gate 
24850Sstevel@tonic-gate void
24860Sstevel@tonic-gate cpuid_get_addrsize(cpu_t *cpu, uint_t *pabits, uint_t *vabits)
24870Sstevel@tonic-gate {
24880Sstevel@tonic-gate 	struct cpuid_info *cpi;
24890Sstevel@tonic-gate 
24900Sstevel@tonic-gate 	if (cpu == NULL)
24910Sstevel@tonic-gate 		cpu = CPU;
24920Sstevel@tonic-gate 	cpi = cpu->cpu_m.mcpu_cpi;
24930Sstevel@tonic-gate 
24940Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
24950Sstevel@tonic-gate 
24960Sstevel@tonic-gate 	if (pabits)
24970Sstevel@tonic-gate 		*pabits = cpi->cpi_pabits;
24980Sstevel@tonic-gate 	if (vabits)
24990Sstevel@tonic-gate 		*vabits = cpi->cpi_vabits;
25000Sstevel@tonic-gate }
25010Sstevel@tonic-gate 
25020Sstevel@tonic-gate /*
25030Sstevel@tonic-gate  * Returns the number of data TLB entries for a corresponding
25040Sstevel@tonic-gate  * pagesize.  If it can't be computed, or isn't known, the
25050Sstevel@tonic-gate  * routine returns zero.  If you ask about an architecturally
25060Sstevel@tonic-gate  * impossible pagesize, the routine will panic (so that the
25070Sstevel@tonic-gate  * hat implementor knows that things are inconsistent.)
25080Sstevel@tonic-gate  */
25090Sstevel@tonic-gate uint_t
25100Sstevel@tonic-gate cpuid_get_dtlb_nent(cpu_t *cpu, size_t pagesize)
25110Sstevel@tonic-gate {
25120Sstevel@tonic-gate 	struct cpuid_info *cpi;
25130Sstevel@tonic-gate 	uint_t dtlb_nent = 0;
25140Sstevel@tonic-gate 
25150Sstevel@tonic-gate 	if (cpu == NULL)
25160Sstevel@tonic-gate 		cpu = CPU;
25170Sstevel@tonic-gate 	cpi = cpu->cpu_m.mcpu_cpi;
25180Sstevel@tonic-gate 
25190Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
25200Sstevel@tonic-gate 
25210Sstevel@tonic-gate 	/*
25220Sstevel@tonic-gate 	 * Check the L2 TLB info
25230Sstevel@tonic-gate 	 */
25240Sstevel@tonic-gate 	if (cpi->cpi_xmaxeax >= 0x80000006) {
25251228Sandrei 		struct cpuid_regs *cp = &cpi->cpi_extd[6];
25260Sstevel@tonic-gate 
25270Sstevel@tonic-gate 		switch (pagesize) {
25280Sstevel@tonic-gate 
25290Sstevel@tonic-gate 		case 4 * 1024:
25300Sstevel@tonic-gate 			/*
25310Sstevel@tonic-gate 			 * All zero in the top 16 bits of the register
25320Sstevel@tonic-gate 			 * indicates a unified TLB. Size is in low 16 bits.
25330Sstevel@tonic-gate 			 */
25340Sstevel@tonic-gate 			if ((cp->cp_ebx & 0xffff0000) == 0)
25350Sstevel@tonic-gate 				dtlb_nent = cp->cp_ebx & 0x0000ffff;
25360Sstevel@tonic-gate 			else
25370Sstevel@tonic-gate 				dtlb_nent = BITX(cp->cp_ebx, 27, 16);
25380Sstevel@tonic-gate 			break;
25390Sstevel@tonic-gate 
25400Sstevel@tonic-gate 		case 2 * 1024 * 1024:
25410Sstevel@tonic-gate 			if ((cp->cp_eax & 0xffff0000) == 0)
25420Sstevel@tonic-gate 				dtlb_nent = cp->cp_eax & 0x0000ffff;
25430Sstevel@tonic-gate 			else
25440Sstevel@tonic-gate 				dtlb_nent = BITX(cp->cp_eax, 27, 16);
25450Sstevel@tonic-gate 			break;
25460Sstevel@tonic-gate 
25470Sstevel@tonic-gate 		default:
25480Sstevel@tonic-gate 			panic("unknown L2 pagesize");
25490Sstevel@tonic-gate 			/*NOTREACHED*/
25500Sstevel@tonic-gate 		}
25510Sstevel@tonic-gate 	}
25520Sstevel@tonic-gate 
25530Sstevel@tonic-gate 	if (dtlb_nent != 0)
25540Sstevel@tonic-gate 		return (dtlb_nent);
25550Sstevel@tonic-gate 
25560Sstevel@tonic-gate 	/*
25570Sstevel@tonic-gate 	 * No L2 TLB support for this size, try L1.
25580Sstevel@tonic-gate 	 */
25590Sstevel@tonic-gate 	if (cpi->cpi_xmaxeax >= 0x80000005) {
25601228Sandrei 		struct cpuid_regs *cp = &cpi->cpi_extd[5];
25610Sstevel@tonic-gate 
25620Sstevel@tonic-gate 		switch (pagesize) {
25630Sstevel@tonic-gate 		case 4 * 1024:
25640Sstevel@tonic-gate 			dtlb_nent = BITX(cp->cp_ebx, 23, 16);
25650Sstevel@tonic-gate 			break;
25660Sstevel@tonic-gate 		case 2 * 1024 * 1024:
25670Sstevel@tonic-gate 			dtlb_nent = BITX(cp->cp_eax, 23, 16);
25680Sstevel@tonic-gate 			break;
25690Sstevel@tonic-gate 		default:
25700Sstevel@tonic-gate 			panic("unknown L1 d-TLB pagesize");
25710Sstevel@tonic-gate 			/*NOTREACHED*/
25720Sstevel@tonic-gate 		}
25730Sstevel@tonic-gate 	}
25740Sstevel@tonic-gate 
25750Sstevel@tonic-gate 	return (dtlb_nent);
25760Sstevel@tonic-gate }
25770Sstevel@tonic-gate 
25780Sstevel@tonic-gate /*
25790Sstevel@tonic-gate  * Return 0 if the erratum is not present or not applicable, positive
25800Sstevel@tonic-gate  * if it is, and negative if the status of the erratum is unknown.
25810Sstevel@tonic-gate  *
25820Sstevel@tonic-gate  * See "Revision Guide for AMD Athlon(tm) 64 and AMD Opteron(tm)
2583359Skucharsk  * Processors" #25759, Rev 3.57, August 2005
25840Sstevel@tonic-gate  */
25850Sstevel@tonic-gate int
25860Sstevel@tonic-gate cpuid_opteron_erratum(cpu_t *cpu, uint_t erratum)
25870Sstevel@tonic-gate {
25880Sstevel@tonic-gate 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
25891228Sandrei 	uint_t eax;
25900Sstevel@tonic-gate 
25912584Ssethg 	/*
25922584Ssethg 	 * Bail out if this CPU isn't an AMD CPU, or if it's
25932584Ssethg 	 * a legacy (32-bit) AMD CPU.
25942584Ssethg 	 */
25952584Ssethg 	if (cpi->cpi_vendor != X86_VENDOR_AMD ||
25964265Skchow 	    cpi->cpi_family == 4 || cpi->cpi_family == 5 ||
25974265Skchow 	    cpi->cpi_family == 6)
25982869Sgavinm 
25990Sstevel@tonic-gate 		return (0);
26000Sstevel@tonic-gate 
26010Sstevel@tonic-gate 	eax = cpi->cpi_std[1].cp_eax;
26020Sstevel@tonic-gate 
26030Sstevel@tonic-gate #define	SH_B0(eax)	(eax == 0xf40 || eax == 0xf50)
26040Sstevel@tonic-gate #define	SH_B3(eax) 	(eax == 0xf51)
26051582Skchow #define	B(eax)		(SH_B0(eax) || SH_B3(eax))
26060Sstevel@tonic-gate 
26070Sstevel@tonic-gate #define	SH_C0(eax)	(eax == 0xf48 || eax == 0xf58)
26080Sstevel@tonic-gate 
26090Sstevel@tonic-gate #define	SH_CG(eax)	(eax == 0xf4a || eax == 0xf5a || eax == 0xf7a)
26100Sstevel@tonic-gate #define	DH_CG(eax)	(eax == 0xfc0 || eax == 0xfe0 || eax == 0xff0)
26110Sstevel@tonic-gate #define	CH_CG(eax)	(eax == 0xf82 || eax == 0xfb2)
26121582Skchow #define	CG(eax)		(SH_CG(eax) || DH_CG(eax) || CH_CG(eax))
26130Sstevel@tonic-gate 
26140Sstevel@tonic-gate #define	SH_D0(eax)	(eax == 0x10f40 || eax == 0x10f50 || eax == 0x10f70)
26150Sstevel@tonic-gate #define	DH_D0(eax)	(eax == 0x10fc0 || eax == 0x10ff0)
26160Sstevel@tonic-gate #define	CH_D0(eax)	(eax == 0x10f80 || eax == 0x10fb0)
26171582Skchow #define	D0(eax)		(SH_D0(eax) || DH_D0(eax) || CH_D0(eax))
26180Sstevel@tonic-gate 
26190Sstevel@tonic-gate #define	SH_E0(eax)	(eax == 0x20f50 || eax == 0x20f40 || eax == 0x20f70)
26200Sstevel@tonic-gate #define	JH_E1(eax)	(eax == 0x20f10)	/* JH8_E0 had 0x20f30 */
26210Sstevel@tonic-gate #define	DH_E3(eax)	(eax == 0x20fc0 || eax == 0x20ff0)
26220Sstevel@tonic-gate #define	SH_E4(eax)	(eax == 0x20f51 || eax == 0x20f71)
26230Sstevel@tonic-gate #define	BH_E4(eax)	(eax == 0x20fb1)
26240Sstevel@tonic-gate #define	SH_E5(eax)	(eax == 0x20f42)
26250Sstevel@tonic-gate #define	DH_E6(eax)	(eax == 0x20ff2 || eax == 0x20fc2)
26260Sstevel@tonic-gate #define	JH_E6(eax)	(eax == 0x20f12 || eax == 0x20f32)
26271582Skchow #define	EX(eax)		(SH_E0(eax) || JH_E1(eax) || DH_E3(eax) || \
26281582Skchow 			    SH_E4(eax) || BH_E4(eax) || SH_E5(eax) || \
26291582Skchow 			    DH_E6(eax) || JH_E6(eax))
26300Sstevel@tonic-gate 
26316691Skchow #define	DR_AX(eax)	(eax == 0x100f00 || eax == 0x100f01 || eax == 0x100f02)
26326691Skchow #define	DR_B0(eax)	(eax == 0x100f20)
26336691Skchow #define	DR_B1(eax)	(eax == 0x100f21)
26346691Skchow #define	DR_BA(eax)	(eax == 0x100f2a)
26356691Skchow #define	DR_B2(eax)	(eax == 0x100f22)
26366691Skchow #define	DR_B3(eax)	(eax == 0x100f23)
26376691Skchow #define	RB_C0(eax)	(eax == 0x100f40)
26386691Skchow 
26390Sstevel@tonic-gate 	switch (erratum) {
26400Sstevel@tonic-gate 	case 1:
26414265Skchow 		return (cpi->cpi_family < 0x10);
26420Sstevel@tonic-gate 	case 51:	/* what does the asterisk mean? */
26430Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax));
26440Sstevel@tonic-gate 	case 52:
26450Sstevel@tonic-gate 		return (B(eax));
26460Sstevel@tonic-gate 	case 57:
26476691Skchow 		return (cpi->cpi_family <= 0x11);
26480Sstevel@tonic-gate 	case 58:
26490Sstevel@tonic-gate 		return (B(eax));
26500Sstevel@tonic-gate 	case 60:
26516691Skchow 		return (cpi->cpi_family <= 0x11);
26520Sstevel@tonic-gate 	case 61:
26530Sstevel@tonic-gate 	case 62:
26540Sstevel@tonic-gate 	case 63:
26550Sstevel@tonic-gate 	case 64:
26560Sstevel@tonic-gate 	case 65:
26570Sstevel@tonic-gate 	case 66:
26580Sstevel@tonic-gate 	case 68:
26590Sstevel@tonic-gate 	case 69:
26600Sstevel@tonic-gate 	case 70:
26610Sstevel@tonic-gate 	case 71:
26620Sstevel@tonic-gate 		return (B(eax));
26630Sstevel@tonic-gate 	case 72:
26640Sstevel@tonic-gate 		return (SH_B0(eax));
26650Sstevel@tonic-gate 	case 74:
26660Sstevel@tonic-gate 		return (B(eax));
26670Sstevel@tonic-gate 	case 75:
26684265Skchow 		return (cpi->cpi_family < 0x10);
26690Sstevel@tonic-gate 	case 76:
26700Sstevel@tonic-gate 		return (B(eax));
26710Sstevel@tonic-gate 	case 77:
26726691Skchow 		return (cpi->cpi_family <= 0x11);
26730Sstevel@tonic-gate 	case 78:
26740Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax));
26750Sstevel@tonic-gate 	case 79:
26760Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax) || EX(eax));
26770Sstevel@tonic-gate 	case 80:
26780Sstevel@tonic-gate 	case 81:
26790Sstevel@tonic-gate 	case 82:
26800Sstevel@tonic-gate 		return (B(eax));
26810Sstevel@tonic-gate 	case 83:
26820Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax));
26830Sstevel@tonic-gate 	case 85:
26844265Skchow 		return (cpi->cpi_family < 0x10);
26850Sstevel@tonic-gate 	case 86:
26860Sstevel@tonic-gate 		return (SH_C0(eax) || CG(eax));
26870Sstevel@tonic-gate 	case 88:
26880Sstevel@tonic-gate #if !defined(__amd64)
26890Sstevel@tonic-gate 		return (0);
26900Sstevel@tonic-gate #else
26910Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax));
26920Sstevel@tonic-gate #endif
26930Sstevel@tonic-gate 	case 89:
26944265Skchow 		return (cpi->cpi_family < 0x10);
26950Sstevel@tonic-gate 	case 90:
26960Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax));
26970Sstevel@tonic-gate 	case 91:
26980Sstevel@tonic-gate 	case 92:
26990Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax));
27000Sstevel@tonic-gate 	case 93:
27010Sstevel@tonic-gate 		return (SH_C0(eax));
27020Sstevel@tonic-gate 	case 94:
27030Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax));
27040Sstevel@tonic-gate 	case 95:
27050Sstevel@tonic-gate #if !defined(__amd64)
27060Sstevel@tonic-gate 		return (0);
27070Sstevel@tonic-gate #else
27080Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax));
27090Sstevel@tonic-gate #endif
27100Sstevel@tonic-gate 	case 96:
27110Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax));
27120Sstevel@tonic-gate 	case 97:
27130Sstevel@tonic-gate 	case 98:
27140Sstevel@tonic-gate 		return (SH_C0(eax) || CG(eax));
27150Sstevel@tonic-gate 	case 99:
27160Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax));
27170Sstevel@tonic-gate 	case 100:
27180Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax));
27190Sstevel@tonic-gate 	case 101:
27200Sstevel@tonic-gate 	case 103:
27210Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax));
27220Sstevel@tonic-gate 	case 104:
27230Sstevel@tonic-gate 		return (SH_C0(eax) || CG(eax) || D0(eax));
27240Sstevel@tonic-gate 	case 105:
27250Sstevel@tonic-gate 	case 106:
27260Sstevel@tonic-gate 	case 107:
27270Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax));
27280Sstevel@tonic-gate 	case 108:
27290Sstevel@tonic-gate 		return (DH_CG(eax));
27300Sstevel@tonic-gate 	case 109:
27310Sstevel@tonic-gate 		return (SH_C0(eax) || CG(eax) || D0(eax));
27320Sstevel@tonic-gate 	case 110:
27330Sstevel@tonic-gate 		return (D0(eax) || EX(eax));
27340Sstevel@tonic-gate 	case 111:
27350Sstevel@tonic-gate 		return (CG(eax));
27360Sstevel@tonic-gate 	case 112:
27370Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax) || EX(eax));
27380Sstevel@tonic-gate 	case 113:
27390Sstevel@tonic-gate 		return (eax == 0x20fc0);
27400Sstevel@tonic-gate 	case 114:
27410Sstevel@tonic-gate 		return (SH_E0(eax) || JH_E1(eax) || DH_E3(eax));
27420Sstevel@tonic-gate 	case 115:
27430Sstevel@tonic-gate 		return (SH_E0(eax) || JH_E1(eax));
27440Sstevel@tonic-gate 	case 116:
27450Sstevel@tonic-gate 		return (SH_E0(eax) || JH_E1(eax) || DH_E3(eax));
27460Sstevel@tonic-gate 	case 117:
27470Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax));
27480Sstevel@tonic-gate 	case 118:
27490Sstevel@tonic-gate 		return (SH_E0(eax) || JH_E1(eax) || SH_E4(eax) || BH_E4(eax) ||
27500Sstevel@tonic-gate 		    JH_E6(eax));
27510Sstevel@tonic-gate 	case 121:
27520Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax) || EX(eax));
27530Sstevel@tonic-gate 	case 122:
27546691Skchow 		return (cpi->cpi_family < 0x10 || cpi->cpi_family == 0x11);
27550Sstevel@tonic-gate 	case 123:
27560Sstevel@tonic-gate 		return (JH_E1(eax) || BH_E4(eax) || JH_E6(eax));
2757359Skucharsk 	case 131:
27584265Skchow 		return (cpi->cpi_family < 0x10);
2759938Sesaxe 	case 6336786:
2760938Sesaxe 		/*
2761938Sesaxe 		 * Test for AdvPowerMgmtInfo.TscPStateInvariant
27624265Skchow 		 * if this is a K8 family or newer processor
2763938Sesaxe 		 */
2764938Sesaxe 		if (CPI_FAMILY(cpi) == 0xf) {
27651228Sandrei 			struct cpuid_regs regs;
27661228Sandrei 			regs.cp_eax = 0x80000007;
27671228Sandrei 			(void) __cpuid_insn(&regs);
27681228Sandrei 			return (!(regs.cp_edx & 0x100));
2769938Sesaxe 		}
2770938Sesaxe 		return (0);
27711582Skchow 	case 6323525:
27721582Skchow 		return (((((eax >> 12) & 0xff00) + (eax & 0xf00)) |
27731582Skchow 		    (((eax >> 4) & 0xf) | ((eax >> 12) & 0xf0))) < 0xf40);
27741582Skchow 
27756691Skchow 	case 6671130:
27766691Skchow 		/*
27776691Skchow 		 * check for processors (pre-Shanghai) that do not provide
27786691Skchow 		 * optimal management of 1gb ptes in its tlb.
27796691Skchow 		 */
27806691Skchow 		return (cpi->cpi_family == 0x10 && cpi->cpi_model < 4);
27816691Skchow 
27826691Skchow 	case 298:
27836691Skchow 		return (DR_AX(eax) || DR_B0(eax) || DR_B1(eax) || DR_BA(eax) ||
27846691Skchow 		    DR_B2(eax) || RB_C0(eax));
27856691Skchow 
27866691Skchow 	default:
27876691Skchow 		return (-1);
27886691Skchow 
27896691Skchow 	}
27906691Skchow }
27916691Skchow 
27926691Skchow /*
27936691Skchow  * Determine if specified erratum is present via OSVW (OS Visible Workaround).
27946691Skchow  * Return 1 if erratum is present, 0 if not present and -1 if indeterminate.
27956691Skchow  */
27966691Skchow int
27976691Skchow osvw_opteron_erratum(cpu_t *cpu, uint_t erratum)
27986691Skchow {
27996691Skchow 	struct cpuid_info	*cpi;
28006691Skchow 	uint_t			osvwid;
28016691Skchow 	static int		osvwfeature = -1;
28026691Skchow 	uint64_t		osvwlength;
28036691Skchow 
28046691Skchow 
28056691Skchow 	cpi = cpu->cpu_m.mcpu_cpi;
28066691Skchow 
28076691Skchow 	/* confirm OSVW supported */
28086691Skchow 	if (osvwfeature == -1) {
28096691Skchow 		osvwfeature = cpi->cpi_extd[1].cp_ecx & CPUID_AMD_ECX_OSVW;
28106691Skchow 	} else {
28116691Skchow 		/* assert that osvw feature setting is consistent on all cpus */
28126691Skchow 		ASSERT(osvwfeature ==
28136691Skchow 		    (cpi->cpi_extd[1].cp_ecx & CPUID_AMD_ECX_OSVW));
28146691Skchow 	}
28156691Skchow 	if (!osvwfeature)
28166691Skchow 		return (-1);
28176691Skchow 
28186691Skchow 	osvwlength = rdmsr(MSR_AMD_OSVW_ID_LEN) & OSVW_ID_LEN_MASK;
28196691Skchow 
28206691Skchow 	switch (erratum) {
28216691Skchow 	case 298:	/* osvwid is 0 */
28226691Skchow 		osvwid = 0;
28236691Skchow 		if (osvwlength <= (uint64_t)osvwid) {
28246691Skchow 			/* osvwid 0 is unknown */
28256691Skchow 			return (-1);
28266691Skchow 		}
28276691Skchow 
28286691Skchow 		/*
28296691Skchow 		 * Check the OSVW STATUS MSR to determine the state
28306691Skchow 		 * of the erratum where:
28316691Skchow 		 *   0 - fixed by HW
28326691Skchow 		 *   1 - BIOS has applied the workaround when BIOS
28336691Skchow 		 *   workaround is available. (Or for other errata,
28346691Skchow 		 *   OS workaround is required.)
28356691Skchow 		 * For a value of 1, caller will confirm that the
28366691Skchow 		 * erratum 298 workaround has indeed been applied by BIOS.
28376691Skchow 		 *
28386691Skchow 		 * A 1 may be set in cpus that have a HW fix
28396691Skchow 		 * in a mixed cpu system. Regarding erratum 298:
28406691Skchow 		 *   In a multiprocessor platform, the workaround above
28416691Skchow 		 *   should be applied to all processors regardless of
28426691Skchow 		 *   silicon revision when an affected processor is
28436691Skchow 		 *   present.
28446691Skchow 		 */
28456691Skchow 
28466691Skchow 		return (rdmsr(MSR_AMD_OSVW_STATUS +
28476691Skchow 		    (osvwid / OSVW_ID_CNT_PER_MSR)) &
28486691Skchow 		    (1ULL << (osvwid % OSVW_ID_CNT_PER_MSR)));
28496691Skchow 
28500Sstevel@tonic-gate 	default:
28510Sstevel@tonic-gate 		return (-1);
28520Sstevel@tonic-gate 	}
28530Sstevel@tonic-gate }
28540Sstevel@tonic-gate 
28550Sstevel@tonic-gate static const char assoc_str[] = "associativity";
28560Sstevel@tonic-gate static const char line_str[] = "line-size";
28570Sstevel@tonic-gate static const char size_str[] = "size";
28580Sstevel@tonic-gate 
28590Sstevel@tonic-gate static void
28600Sstevel@tonic-gate add_cache_prop(dev_info_t *devi, const char *label, const char *type,
28610Sstevel@tonic-gate     uint32_t val)
28620Sstevel@tonic-gate {
28630Sstevel@tonic-gate 	char buf[128];
28640Sstevel@tonic-gate 
28650Sstevel@tonic-gate 	/*
28660Sstevel@tonic-gate 	 * ndi_prop_update_int() is used because it is desirable for
28670Sstevel@tonic-gate 	 * DDI_PROP_HW_DEF and DDI_PROP_DONTSLEEP to be set.
28680Sstevel@tonic-gate 	 */
28690Sstevel@tonic-gate 	if (snprintf(buf, sizeof (buf), "%s-%s", label, type) < sizeof (buf))
28700Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, devi, buf, val);
28710Sstevel@tonic-gate }
28720Sstevel@tonic-gate 
28730Sstevel@tonic-gate /*
28740Sstevel@tonic-gate  * Intel-style cache/tlb description
28750Sstevel@tonic-gate  *
28760Sstevel@tonic-gate  * Standard cpuid level 2 gives a randomly ordered
28770Sstevel@tonic-gate  * selection of tags that index into a table that describes
28780Sstevel@tonic-gate  * cache and tlb properties.
28790Sstevel@tonic-gate  */
28800Sstevel@tonic-gate 
28810Sstevel@tonic-gate static const char l1_icache_str[] = "l1-icache";
28820Sstevel@tonic-gate static const char l1_dcache_str[] = "l1-dcache";
28830Sstevel@tonic-gate static const char l2_cache_str[] = "l2-cache";
28843446Smrj static const char l3_cache_str[] = "l3-cache";
28850Sstevel@tonic-gate static const char itlb4k_str[] = "itlb-4K";
28860Sstevel@tonic-gate static const char dtlb4k_str[] = "dtlb-4K";
28876964Svd224797 static const char itlb2M_str[] = "itlb-2M";
28880Sstevel@tonic-gate static const char itlb4M_str[] = "itlb-4M";
28890Sstevel@tonic-gate static const char dtlb4M_str[] = "dtlb-4M";
28906334Sksadhukh static const char dtlb24_str[] = "dtlb0-2M-4M";
28910Sstevel@tonic-gate static const char itlb424_str[] = "itlb-4K-2M-4M";
28926334Sksadhukh static const char itlb24_str[] = "itlb-2M-4M";
28930Sstevel@tonic-gate static const char dtlb44_str[] = "dtlb-4K-4M";
28940Sstevel@tonic-gate static const char sl1_dcache_str[] = "sectored-l1-dcache";
28950Sstevel@tonic-gate static const char sl2_cache_str[] = "sectored-l2-cache";
28960Sstevel@tonic-gate static const char itrace_str[] = "itrace-cache";
28970Sstevel@tonic-gate static const char sl3_cache_str[] = "sectored-l3-cache";
28986334Sksadhukh static const char sh_l2_tlb4k_str[] = "shared-l2-tlb-4k";
28990Sstevel@tonic-gate 
29000Sstevel@tonic-gate static const struct cachetab {
29010Sstevel@tonic-gate 	uint8_t 	ct_code;
29020Sstevel@tonic-gate 	uint8_t		ct_assoc;
29030Sstevel@tonic-gate 	uint16_t 	ct_line_size;
29040Sstevel@tonic-gate 	size_t		ct_size;
29050Sstevel@tonic-gate 	const char	*ct_label;
29060Sstevel@tonic-gate } intel_ctab[] = {
29076964Svd224797 	/*
29086964Svd224797 	 * maintain descending order!
29096964Svd224797 	 *
29106964Svd224797 	 * Codes ignored - Reason
29116964Svd224797 	 * ----------------------
29126964Svd224797 	 * 40H - intel_cpuid_4_cache_info() disambiguates l2/l3 cache
29136964Svd224797 	 * f0H/f1H - Currently we do not interpret prefetch size by design
29146964Svd224797 	 */
29156334Sksadhukh 	{ 0xe4, 16, 64, 8*1024*1024, l3_cache_str},
29166334Sksadhukh 	{ 0xe3, 16, 64, 4*1024*1024, l3_cache_str},
29176334Sksadhukh 	{ 0xe2, 16, 64, 2*1024*1024, l3_cache_str},
29186334Sksadhukh 	{ 0xde, 12, 64, 6*1024*1024, l3_cache_str},
29196334Sksadhukh 	{ 0xdd, 12, 64, 3*1024*1024, l3_cache_str},
29206334Sksadhukh 	{ 0xdc, 12, 64, ((1*1024*1024)+(512*1024)), l3_cache_str},
29216334Sksadhukh 	{ 0xd8, 8, 64, 4*1024*1024, l3_cache_str},
29226334Sksadhukh 	{ 0xd7, 8, 64, 2*1024*1024, l3_cache_str},
29236334Sksadhukh 	{ 0xd6, 8, 64, 1*1024*1024, l3_cache_str},
29246334Sksadhukh 	{ 0xd2, 4, 64, 2*1024*1024, l3_cache_str},
29256334Sksadhukh 	{ 0xd1, 4, 64, 1*1024*1024, l3_cache_str},
29266334Sksadhukh 	{ 0xd0, 4, 64, 512*1024, l3_cache_str},
29276334Sksadhukh 	{ 0xca, 4, 0, 512, sh_l2_tlb4k_str},
29286964Svd224797 	{ 0xc0, 4, 0, 8, dtlb44_str },
29296964Svd224797 	{ 0xba, 4, 0, 64, dtlb4k_str },
29303446Smrj 	{ 0xb4, 4, 0, 256, dtlb4k_str },
29310Sstevel@tonic-gate 	{ 0xb3, 4, 0, 128, dtlb4k_str },
29326334Sksadhukh 	{ 0xb2, 4, 0, 64, itlb4k_str },
29330Sstevel@tonic-gate 	{ 0xb0, 4, 0, 128, itlb4k_str },
29340Sstevel@tonic-gate 	{ 0x87, 8, 64, 1024*1024, l2_cache_str},
29350Sstevel@tonic-gate 	{ 0x86, 4, 64, 512*1024, l2_cache_str},
29360Sstevel@tonic-gate 	{ 0x85, 8, 32, 2*1024*1024, l2_cache_str},
29370Sstevel@tonic-gate 	{ 0x84, 8, 32, 1024*1024, l2_cache_str},
29380Sstevel@tonic-gate 	{ 0x83, 8, 32, 512*1024, l2_cache_str},
29390Sstevel@tonic-gate 	{ 0x82, 8, 32, 256*1024, l2_cache_str},
29406964Svd224797 	{ 0x80, 8, 64, 512*1024, l2_cache_str},
29410Sstevel@tonic-gate 	{ 0x7f, 2, 64, 512*1024, l2_cache_str},
29420Sstevel@tonic-gate 	{ 0x7d, 8, 64, 2*1024*1024, sl2_cache_str},
29430Sstevel@tonic-gate 	{ 0x7c, 8, 64, 1024*1024, sl2_cache_str},
29440Sstevel@tonic-gate 	{ 0x7b, 8, 64, 512*1024, sl2_cache_str},
29450Sstevel@tonic-gate 	{ 0x7a, 8, 64, 256*1024, sl2_cache_str},
29460Sstevel@tonic-gate 	{ 0x79, 8, 64, 128*1024, sl2_cache_str},
29470Sstevel@tonic-gate 	{ 0x78, 8, 64, 1024*1024, l2_cache_str},
29483446Smrj 	{ 0x73, 8, 0, 64*1024, itrace_str},
29490Sstevel@tonic-gate 	{ 0x72, 8, 0, 32*1024, itrace_str},
29500Sstevel@tonic-gate 	{ 0x71, 8, 0, 16*1024, itrace_str},
29510Sstevel@tonic-gate 	{ 0x70, 8, 0, 12*1024, itrace_str},
29520Sstevel@tonic-gate 	{ 0x68, 4, 64, 32*1024, sl1_dcache_str},
29530Sstevel@tonic-gate 	{ 0x67, 4, 64, 16*1024, sl1_dcache_str},
29540Sstevel@tonic-gate 	{ 0x66, 4, 64, 8*1024, sl1_dcache_str},
29550Sstevel@tonic-gate 	{ 0x60, 8, 64, 16*1024, sl1_dcache_str},
29560Sstevel@tonic-gate 	{ 0x5d, 0, 0, 256, dtlb44_str},
29570Sstevel@tonic-gate 	{ 0x5c, 0, 0, 128, dtlb44_str},
29580Sstevel@tonic-gate 	{ 0x5b, 0, 0, 64, dtlb44_str},
29596334Sksadhukh 	{ 0x5a, 4, 0, 32, dtlb24_str},
29606964Svd224797 	{ 0x59, 0, 0, 16, dtlb4k_str},
29616964Svd224797 	{ 0x57, 4, 0, 16, dtlb4k_str},
29626964Svd224797 	{ 0x56, 4, 0, 16, dtlb4M_str},
29636334Sksadhukh 	{ 0x55, 0, 0, 7, itlb24_str},
29640Sstevel@tonic-gate 	{ 0x52, 0, 0, 256, itlb424_str},
29650Sstevel@tonic-gate 	{ 0x51, 0, 0, 128, itlb424_str},
29660Sstevel@tonic-gate 	{ 0x50, 0, 0, 64, itlb424_str},
29676964Svd224797 	{ 0x4f, 0, 0, 32, itlb4k_str},
29686964Svd224797 	{ 0x4e, 24, 64, 6*1024*1024, l2_cache_str},
29693446Smrj 	{ 0x4d, 16, 64, 16*1024*1024, l3_cache_str},
29703446Smrj 	{ 0x4c, 12, 64, 12*1024*1024, l3_cache_str},
29713446Smrj 	{ 0x4b, 16, 64, 8*1024*1024, l3_cache_str},
29723446Smrj 	{ 0x4a, 12, 64, 6*1024*1024, l3_cache_str},
29733446Smrj 	{ 0x49, 16, 64, 4*1024*1024, l3_cache_str},
29746964Svd224797 	{ 0x48, 12, 64, 3*1024*1024, l2_cache_str},
29753446Smrj 	{ 0x47, 8, 64, 8*1024*1024, l3_cache_str},
29763446Smrj 	{ 0x46, 4, 64, 4*1024*1024, l3_cache_str},
29770Sstevel@tonic-gate 	{ 0x45, 4, 32, 2*1024*1024, l2_cache_str},
29780Sstevel@tonic-gate 	{ 0x44, 4, 32, 1024*1024, l2_cache_str},
29790Sstevel@tonic-gate 	{ 0x43, 4, 32, 512*1024, l2_cache_str},
29800Sstevel@tonic-gate 	{ 0x42, 4, 32, 256*1024, l2_cache_str},
29810Sstevel@tonic-gate 	{ 0x41, 4, 32, 128*1024, l2_cache_str},
29823446Smrj 	{ 0x3e, 4, 64, 512*1024, sl2_cache_str},
29833446Smrj 	{ 0x3d, 6, 64, 384*1024, sl2_cache_str},
29840Sstevel@tonic-gate 	{ 0x3c, 4, 64, 256*1024, sl2_cache_str},
29850Sstevel@tonic-gate 	{ 0x3b, 2, 64, 128*1024, sl2_cache_str},
29863446Smrj 	{ 0x3a, 6, 64, 192*1024, sl2_cache_str},
29870Sstevel@tonic-gate 	{ 0x39, 4, 64, 128*1024, sl2_cache_str},
29880Sstevel@tonic-gate 	{ 0x30, 8, 64, 32*1024, l1_icache_str},
29890Sstevel@tonic-gate 	{ 0x2c, 8, 64, 32*1024, l1_dcache_str},
29900Sstevel@tonic-gate 	{ 0x29, 8, 64, 4096*1024, sl3_cache_str},
29910Sstevel@tonic-gate 	{ 0x25, 8, 64, 2048*1024, sl3_cache_str},
29920Sstevel@tonic-gate 	{ 0x23, 8, 64, 1024*1024, sl3_cache_str},
29930Sstevel@tonic-gate 	{ 0x22, 4, 64, 512*1024, sl3_cache_str},
29946964Svd224797 	{ 0x0e, 6, 64, 24*1024, l1_dcache_str},
29956334Sksadhukh 	{ 0x0d, 4, 32, 16*1024, l1_dcache_str},
29960Sstevel@tonic-gate 	{ 0x0c, 4, 32, 16*1024, l1_dcache_str},
29973446Smrj 	{ 0x0b, 4, 0, 4, itlb4M_str},
29980Sstevel@tonic-gate 	{ 0x0a, 2, 32, 8*1024, l1_dcache_str},
29990Sstevel@tonic-gate 	{ 0x08, 4, 32, 16*1024, l1_icache_str},
30000Sstevel@tonic-gate 	{ 0x06, 4, 32, 8*1024, l1_icache_str},
30016964Svd224797 	{ 0x05, 4, 0, 32, dtlb4M_str},
30020Sstevel@tonic-gate 	{ 0x04, 4, 0, 8, dtlb4M_str},
30030Sstevel@tonic-gate 	{ 0x03, 4, 0, 64, dtlb4k_str},
30040Sstevel@tonic-gate 	{ 0x02, 4, 0, 2, itlb4M_str},
30050Sstevel@tonic-gate 	{ 0x01, 4, 0, 32, itlb4k_str},
30060Sstevel@tonic-gate 	{ 0 }
30070Sstevel@tonic-gate };
30080Sstevel@tonic-gate 
30090Sstevel@tonic-gate static const struct cachetab cyrix_ctab[] = {
30100Sstevel@tonic-gate 	{ 0x70, 4, 0, 32, "tlb-4K" },
30110Sstevel@tonic-gate 	{ 0x80, 4, 16, 16*1024, "l1-cache" },
30120Sstevel@tonic-gate 	{ 0 }
30130Sstevel@tonic-gate };
30140Sstevel@tonic-gate 
30150Sstevel@tonic-gate /*
30160Sstevel@tonic-gate  * Search a cache table for a matching entry
30170Sstevel@tonic-gate  */
30180Sstevel@tonic-gate static const struct cachetab *
30190Sstevel@tonic-gate find_cacheent(const struct cachetab *ct, uint_t code)
30200Sstevel@tonic-gate {
30210Sstevel@tonic-gate 	if (code != 0) {
30220Sstevel@tonic-gate 		for (; ct->ct_code != 0; ct++)
30230Sstevel@tonic-gate 			if (ct->ct_code <= code)
30240Sstevel@tonic-gate 				break;
30250Sstevel@tonic-gate 		if (ct->ct_code == code)
30260Sstevel@tonic-gate 			return (ct);
30270Sstevel@tonic-gate 	}
30280Sstevel@tonic-gate 	return (NULL);
30290Sstevel@tonic-gate }
30300Sstevel@tonic-gate 
30310Sstevel@tonic-gate /*
30325438Sksadhukh  * Populate cachetab entry with L2 or L3 cache-information using
30335438Sksadhukh  * cpuid function 4. This function is called from intel_walk_cacheinfo()
30345438Sksadhukh  * when descriptor 0x49 is encountered. It returns 0 if no such cache
30355438Sksadhukh  * information is found.
30365438Sksadhukh  */
30375438Sksadhukh static int
30385438Sksadhukh intel_cpuid_4_cache_info(struct cachetab *ct, struct cpuid_info *cpi)
30395438Sksadhukh {
30405438Sksadhukh 	uint32_t level, i;
30415438Sksadhukh 	int ret = 0;
30425438Sksadhukh 
30435438Sksadhukh 	for (i = 0; i < cpi->cpi_std_4_size; i++) {
30445438Sksadhukh 		level = CPI_CACHE_LVL(cpi->cpi_std_4[i]);
30455438Sksadhukh 
30465438Sksadhukh 		if (level == 2 || level == 3) {
30475438Sksadhukh 			ct->ct_assoc = CPI_CACHE_WAYS(cpi->cpi_std_4[i]) + 1;
30485438Sksadhukh 			ct->ct_line_size =
30495438Sksadhukh 			    CPI_CACHE_COH_LN_SZ(cpi->cpi_std_4[i]) + 1;
30505438Sksadhukh 			ct->ct_size = ct->ct_assoc *
30515438Sksadhukh 			    (CPI_CACHE_PARTS(cpi->cpi_std_4[i]) + 1) *
30525438Sksadhukh 			    ct->ct_line_size *
30535438Sksadhukh 			    (cpi->cpi_std_4[i]->cp_ecx + 1);
30545438Sksadhukh 
30555438Sksadhukh 			if (level == 2) {
30565438Sksadhukh 				ct->ct_label = l2_cache_str;
30575438Sksadhukh 			} else if (level == 3) {
30585438Sksadhukh 				ct->ct_label = l3_cache_str;
30595438Sksadhukh 			}
30605438Sksadhukh 			ret = 1;
30615438Sksadhukh 		}
30625438Sksadhukh 	}
30635438Sksadhukh 
30645438Sksadhukh 	return (ret);
30655438Sksadhukh }
30665438Sksadhukh 
30675438Sksadhukh /*
30680Sstevel@tonic-gate  * Walk the cacheinfo descriptor, applying 'func' to every valid element
30690Sstevel@tonic-gate  * The walk is terminated if the walker returns non-zero.
30700Sstevel@tonic-gate  */
30710Sstevel@tonic-gate static void
30720Sstevel@tonic-gate intel_walk_cacheinfo(struct cpuid_info *cpi,
30730Sstevel@tonic-gate     void *arg, int (*func)(void *, const struct cachetab *))
30740Sstevel@tonic-gate {
30750Sstevel@tonic-gate 	const struct cachetab *ct;
30766964Svd224797 	struct cachetab des_49_ct, des_b1_ct;
30770Sstevel@tonic-gate 	uint8_t *dp;
30780Sstevel@tonic-gate 	int i;
30790Sstevel@tonic-gate 
30800Sstevel@tonic-gate 	if ((dp = cpi->cpi_cacheinfo) == NULL)
30810Sstevel@tonic-gate 		return;
30824797Sksadhukh 	for (i = 0; i < cpi->cpi_ncache; i++, dp++) {
30834797Sksadhukh 		/*
30844797Sksadhukh 		 * For overloaded descriptor 0x49 we use cpuid function 4
30855438Sksadhukh 		 * if supported by the current processor, to create
30864797Sksadhukh 		 * cache information.
30876964Svd224797 		 * For overloaded descriptor 0xb1 we use X86_PAE flag
30886964Svd224797 		 * to disambiguate the cache information.
30894797Sksadhukh 		 */
30905438Sksadhukh 		if (*dp == 0x49 && cpi->cpi_maxeax >= 0x4 &&
30915438Sksadhukh 		    intel_cpuid_4_cache_info(&des_49_ct, cpi) == 1) {
30925438Sksadhukh 				ct = &des_49_ct;
30936964Svd224797 		} else if (*dp == 0xb1) {
30946964Svd224797 			des_b1_ct.ct_code = 0xb1;
30956964Svd224797 			des_b1_ct.ct_assoc = 4;
30966964Svd224797 			des_b1_ct.ct_line_size = 0;
30976964Svd224797 			if (x86_feature & X86_PAE) {
30986964Svd224797 				des_b1_ct.ct_size = 8;
30996964Svd224797 				des_b1_ct.ct_label = itlb2M_str;
31006964Svd224797 			} else {
31016964Svd224797 				des_b1_ct.ct_size = 4;
31026964Svd224797 				des_b1_ct.ct_label = itlb4M_str;
31036964Svd224797 			}
31046964Svd224797 			ct = &des_b1_ct;
31055438Sksadhukh 		} else {
31065438Sksadhukh 			if ((ct = find_cacheent(intel_ctab, *dp)) == NULL) {
31075438Sksadhukh 				continue;
31085438Sksadhukh 			}
31094797Sksadhukh 		}
31104797Sksadhukh 
31115438Sksadhukh 		if (func(arg, ct) != 0) {
31125438Sksadhukh 			break;
31130Sstevel@tonic-gate 		}
31144797Sksadhukh 	}
31150Sstevel@tonic-gate }
31160Sstevel@tonic-gate 
31170Sstevel@tonic-gate /*
31180Sstevel@tonic-gate  * (Like the Intel one, except for Cyrix CPUs)
31190Sstevel@tonic-gate  */
31200Sstevel@tonic-gate static void
31210Sstevel@tonic-gate cyrix_walk_cacheinfo(struct cpuid_info *cpi,
31220Sstevel@tonic-gate     void *arg, int (*func)(void *, const struct cachetab *))
31230Sstevel@tonic-gate {
31240Sstevel@tonic-gate 	const struct cachetab *ct;
31250Sstevel@tonic-gate 	uint8_t *dp;
31260Sstevel@tonic-gate 	int i;
31270Sstevel@tonic-gate 
31280Sstevel@tonic-gate 	if ((dp = cpi->cpi_cacheinfo) == NULL)
31290Sstevel@tonic-gate 		return;
31300Sstevel@tonic-gate 	for (i = 0; i < cpi->cpi_ncache; i++, dp++) {
31310Sstevel@tonic-gate 		/*
31320Sstevel@tonic-gate 		 * Search Cyrix-specific descriptor table first ..
31330Sstevel@tonic-gate 		 */
31340Sstevel@tonic-gate 		if ((ct = find_cacheent(cyrix_ctab, *dp)) != NULL) {
31350Sstevel@tonic-gate 			if (func(arg, ct) != 0)
31360Sstevel@tonic-gate 				break;
31370Sstevel@tonic-gate 			continue;
31380Sstevel@tonic-gate 		}
31390Sstevel@tonic-gate 		/*
31400Sstevel@tonic-gate 		 * .. else fall back to the Intel one
31410Sstevel@tonic-gate 		 */
31420Sstevel@tonic-gate 		if ((ct = find_cacheent(intel_ctab, *dp)) != NULL) {
31430Sstevel@tonic-gate 			if (func(arg, ct) != 0)
31440Sstevel@tonic-gate 				break;
31450Sstevel@tonic-gate 			continue;
31460Sstevel@tonic-gate 		}
31470Sstevel@tonic-gate 	}
31480Sstevel@tonic-gate }
31490Sstevel@tonic-gate 
31500Sstevel@tonic-gate /*
31510Sstevel@tonic-gate  * A cacheinfo walker that adds associativity, line-size, and size properties
31520Sstevel@tonic-gate  * to the devinfo node it is passed as an argument.
31530Sstevel@tonic-gate  */
31540Sstevel@tonic-gate static int
31550Sstevel@tonic-gate add_cacheent_props(void *arg, const struct cachetab *ct)
31560Sstevel@tonic-gate {
31570Sstevel@tonic-gate 	dev_info_t *devi = arg;
31580Sstevel@tonic-gate 
31590Sstevel@tonic-gate 	add_cache_prop(devi, ct->ct_label, assoc_str, ct->ct_assoc);
31600Sstevel@tonic-gate 	if (ct->ct_line_size != 0)
31610Sstevel@tonic-gate 		add_cache_prop(devi, ct->ct_label, line_str,
31620Sstevel@tonic-gate 		    ct->ct_line_size);
31630Sstevel@tonic-gate 	add_cache_prop(devi, ct->ct_label, size_str, ct->ct_size);
31640Sstevel@tonic-gate 	return (0);
31650Sstevel@tonic-gate }
31660Sstevel@tonic-gate 
31674797Sksadhukh 
31680Sstevel@tonic-gate static const char fully_assoc[] = "fully-associative?";
31690Sstevel@tonic-gate 
31700Sstevel@tonic-gate /*
31710Sstevel@tonic-gate  * AMD style cache/tlb description
31720Sstevel@tonic-gate  *
31730Sstevel@tonic-gate  * Extended functions 5 and 6 directly describe properties of
31740Sstevel@tonic-gate  * tlbs and various cache levels.
31750Sstevel@tonic-gate  */
31760Sstevel@tonic-gate static void
31770Sstevel@tonic-gate add_amd_assoc(dev_info_t *devi, const char *label, uint_t assoc)
31780Sstevel@tonic-gate {
31790Sstevel@tonic-gate 	switch (assoc) {
31800Sstevel@tonic-gate 	case 0:	/* reserved; ignore */
31810Sstevel@tonic-gate 		break;
31820Sstevel@tonic-gate 	default:
31830Sstevel@tonic-gate 		add_cache_prop(devi, label, assoc_str, assoc);
31840Sstevel@tonic-gate 		break;
31850Sstevel@tonic-gate 	case 0xff:
31860Sstevel@tonic-gate 		add_cache_prop(devi, label, fully_assoc, 1);
31870Sstevel@tonic-gate 		break;
31880Sstevel@tonic-gate 	}
31890Sstevel@tonic-gate }
31900Sstevel@tonic-gate 
31910Sstevel@tonic-gate static void
31920Sstevel@tonic-gate add_amd_tlb(dev_info_t *devi, const char *label, uint_t assoc, uint_t size)
31930Sstevel@tonic-gate {
31940Sstevel@tonic-gate 	if (size == 0)
31950Sstevel@tonic-gate 		return;
31960Sstevel@tonic-gate 	add_cache_prop(devi, label, size_str, size);
31970Sstevel@tonic-gate 	add_amd_assoc(devi, label, assoc);
31980Sstevel@tonic-gate }
31990Sstevel@tonic-gate 
32000Sstevel@tonic-gate static void
32010Sstevel@tonic-gate add_amd_cache(dev_info_t *devi, const char *label,
32020Sstevel@tonic-gate     uint_t size, uint_t assoc, uint_t lines_per_tag, uint_t line_size)
32030Sstevel@tonic-gate {
32040Sstevel@tonic-gate 	if (size == 0 || line_size == 0)
32050Sstevel@tonic-gate 		return;
32060Sstevel@tonic-gate 	add_amd_assoc(devi, label, assoc);
32070Sstevel@tonic-gate 	/*
32080Sstevel@tonic-gate 	 * Most AMD parts have a sectored cache. Multiple cache lines are
32090Sstevel@tonic-gate 	 * associated with each tag. A sector consists of all cache lines
32100Sstevel@tonic-gate 	 * associated with a tag. For example, the AMD K6-III has a sector
32110Sstevel@tonic-gate 	 * size of 2 cache lines per tag.
32120Sstevel@tonic-gate 	 */
32130Sstevel@tonic-gate 	if (lines_per_tag != 0)
32140Sstevel@tonic-gate 		add_cache_prop(devi, label, "lines-per-tag", lines_per_tag);
32150Sstevel@tonic-gate 	add_cache_prop(devi, label, line_str, line_size);
32160Sstevel@tonic-gate 	add_cache_prop(devi, label, size_str, size * 1024);
32170Sstevel@tonic-gate }
32180Sstevel@tonic-gate 
32190Sstevel@tonic-gate static void
32200Sstevel@tonic-gate add_amd_l2_assoc(dev_info_t *devi, const char *label, uint_t assoc)
32210Sstevel@tonic-gate {
32220Sstevel@tonic-gate 	switch (assoc) {
32230Sstevel@tonic-gate 	case 0:	/* off */
32240Sstevel@tonic-gate 		break;
32250Sstevel@tonic-gate 	case 1:
32260Sstevel@tonic-gate 	case 2:
32270Sstevel@tonic-gate 	case 4:
32280Sstevel@tonic-gate 		add_cache_prop(devi, label, assoc_str, assoc);
32290Sstevel@tonic-gate 		break;
32300Sstevel@tonic-gate 	case 6:
32310Sstevel@tonic-gate 		add_cache_prop(devi, label, assoc_str, 8);
32320Sstevel@tonic-gate 		break;
32330Sstevel@tonic-gate 	case 8:
32340Sstevel@tonic-gate 		add_cache_prop(devi, label, assoc_str, 16);
32350Sstevel@tonic-gate 		break;
32360Sstevel@tonic-gate 	case 0xf:
32370Sstevel@tonic-gate 		add_cache_prop(devi, label, fully_assoc, 1);
32380Sstevel@tonic-gate 		break;
32390Sstevel@tonic-gate 	default: /* reserved; ignore */
32400Sstevel@tonic-gate 		break;
32410Sstevel@tonic-gate 	}
32420Sstevel@tonic-gate }
32430Sstevel@tonic-gate 
32440Sstevel@tonic-gate static void
32450Sstevel@tonic-gate add_amd_l2_tlb(dev_info_t *devi, const char *label, uint_t assoc, uint_t size)
32460Sstevel@tonic-gate {
32470Sstevel@tonic-gate 	if (size == 0 || assoc == 0)
32480Sstevel@tonic-gate 		return;
32490Sstevel@tonic-gate 	add_amd_l2_assoc(devi, label, assoc);
32500Sstevel@tonic-gate 	add_cache_prop(devi, label, size_str, size);
32510Sstevel@tonic-gate }
32520Sstevel@tonic-gate 
32530Sstevel@tonic-gate static void
32540Sstevel@tonic-gate add_amd_l2_cache(dev_info_t *devi, const char *label,
32550Sstevel@tonic-gate     uint_t size, uint_t assoc, uint_t lines_per_tag, uint_t line_size)
32560Sstevel@tonic-gate {
32570Sstevel@tonic-gate 	if (size == 0 || assoc == 0 || line_size == 0)
32580Sstevel@tonic-gate 		return;
32590Sstevel@tonic-gate 	add_amd_l2_assoc(devi, label, assoc);
32600Sstevel@tonic-gate 	if (lines_per_tag != 0)
32610Sstevel@tonic-gate 		add_cache_prop(devi, label, "lines-per-tag", lines_per_tag);
32620Sstevel@tonic-gate 	add_cache_prop(devi, label, line_str, line_size);
32630Sstevel@tonic-gate 	add_cache_prop(devi, label, size_str, size * 1024);
32640Sstevel@tonic-gate }
32650Sstevel@tonic-gate 
32660Sstevel@tonic-gate static void
32670Sstevel@tonic-gate amd_cache_info(struct cpuid_info *cpi, dev_info_t *devi)
32680Sstevel@tonic-gate {
32691228Sandrei 	struct cpuid_regs *cp;
32700Sstevel@tonic-gate 
32710Sstevel@tonic-gate 	if (cpi->cpi_xmaxeax < 0x80000005)
32720Sstevel@tonic-gate 		return;
32730Sstevel@tonic-gate 	cp = &cpi->cpi_extd[5];
32740Sstevel@tonic-gate 
32750Sstevel@tonic-gate 	/*
32760Sstevel@tonic-gate 	 * 4M/2M L1 TLB configuration
32770Sstevel@tonic-gate 	 *
32780Sstevel@tonic-gate 	 * We report the size for 2M pages because AMD uses two
32790Sstevel@tonic-gate 	 * TLB entries for one 4M page.
32800Sstevel@tonic-gate 	 */
32810Sstevel@tonic-gate 	add_amd_tlb(devi, "dtlb-2M",
32820Sstevel@tonic-gate 	    BITX(cp->cp_eax, 31, 24), BITX(cp->cp_eax, 23, 16));
32830Sstevel@tonic-gate 	add_amd_tlb(devi, "itlb-2M",
32840Sstevel@tonic-gate 	    BITX(cp->cp_eax, 15, 8), BITX(cp->cp_eax, 7, 0));
32850Sstevel@tonic-gate 
32860Sstevel@tonic-gate 	/*
32870Sstevel@tonic-gate 	 * 4K L1 TLB configuration
32880Sstevel@tonic-gate 	 */
32890Sstevel@tonic-gate 
32900Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
32910Sstevel@tonic-gate 		uint_t nentries;
32920Sstevel@tonic-gate 	case X86_VENDOR_TM:
32930Sstevel@tonic-gate 		if (cpi->cpi_family >= 5) {
32940Sstevel@tonic-gate 			/*
32950Sstevel@tonic-gate 			 * Crusoe processors have 256 TLB entries, but
32960Sstevel@tonic-gate 			 * cpuid data format constrains them to only
32970Sstevel@tonic-gate 			 * reporting 255 of them.
32980Sstevel@tonic-gate 			 */
32990Sstevel@tonic-gate 			if ((nentries = BITX(cp->cp_ebx, 23, 16)) == 255)
33000Sstevel@tonic-gate 				nentries = 256;
33010Sstevel@tonic-gate 			/*
33020Sstevel@tonic-gate 			 * Crusoe processors also have a unified TLB
33030Sstevel@tonic-gate 			 */
33040Sstevel@tonic-gate 			add_amd_tlb(devi, "tlb-4K", BITX(cp->cp_ebx, 31, 24),
33050Sstevel@tonic-gate 			    nentries);
33060Sstevel@tonic-gate 			break;
33070Sstevel@tonic-gate 		}
33080Sstevel@tonic-gate 		/*FALLTHROUGH*/
33090Sstevel@tonic-gate 	default:
33100Sstevel@tonic-gate 		add_amd_tlb(devi, itlb4k_str,
33110Sstevel@tonic-gate 		    BITX(cp->cp_ebx, 31, 24), BITX(cp->cp_ebx, 23, 16));
33120Sstevel@tonic-gate 		add_amd_tlb(devi, dtlb4k_str,
33130Sstevel@tonic-gate 		    BITX(cp->cp_ebx, 15, 8), BITX(cp->cp_ebx, 7, 0));
33140Sstevel@tonic-gate 		break;
33150Sstevel@tonic-gate 	}
33160Sstevel@tonic-gate 
33170Sstevel@tonic-gate 	/*
33180Sstevel@tonic-gate 	 * data L1 cache configuration
33190Sstevel@tonic-gate 	 */
33200Sstevel@tonic-gate 
33210Sstevel@tonic-gate 	add_amd_cache(devi, l1_dcache_str,
33220Sstevel@tonic-gate 	    BITX(cp->cp_ecx, 31, 24), BITX(cp->cp_ecx, 23, 16),
33230Sstevel@tonic-gate 	    BITX(cp->cp_ecx, 15, 8), BITX(cp->cp_ecx, 7, 0));
33240Sstevel@tonic-gate 
33250Sstevel@tonic-gate 	/*
33260Sstevel@tonic-gate 	 * code L1 cache configuration
33270Sstevel@tonic-gate 	 */
33280Sstevel@tonic-gate 
33290Sstevel@tonic-gate 	add_amd_cache(devi, l1_icache_str,
33300Sstevel@tonic-gate 	    BITX(cp->cp_edx, 31, 24), BITX(cp->cp_edx, 23, 16),
33310Sstevel@tonic-gate 	    BITX(cp->cp_edx, 15, 8), BITX(cp->cp_edx, 7, 0));
33320Sstevel@tonic-gate 
33330Sstevel@tonic-gate 	if (cpi->cpi_xmaxeax < 0x80000006)
33340Sstevel@tonic-gate 		return;
33350Sstevel@tonic-gate 	cp = &cpi->cpi_extd[6];
33360Sstevel@tonic-gate 
33370Sstevel@tonic-gate 	/* Check for a unified L2 TLB for large pages */
33380Sstevel@tonic-gate 
33390Sstevel@tonic-gate 	if (BITX(cp->cp_eax, 31, 16) == 0)
33400Sstevel@tonic-gate 		add_amd_l2_tlb(devi, "l2-tlb-2M",
33410Sstevel@tonic-gate 		    BITX(cp->cp_eax, 15, 12), BITX(cp->cp_eax, 11, 0));
33420Sstevel@tonic-gate 	else {
33430Sstevel@tonic-gate 		add_amd_l2_tlb(devi, "l2-dtlb-2M",
33440Sstevel@tonic-gate 		    BITX(cp->cp_eax, 31, 28), BITX(cp->cp_eax, 27, 16));
33450Sstevel@tonic-gate 		add_amd_l2_tlb(devi, "l2-itlb-2M",
33460Sstevel@tonic-gate 		    BITX(cp->cp_eax, 15, 12), BITX(cp->cp_eax, 11, 0));
33470Sstevel@tonic-gate 	}
33480Sstevel@tonic-gate 
33490Sstevel@tonic-gate 	/* Check for a unified L2 TLB for 4K pages */
33500Sstevel@tonic-gate 
33510Sstevel@tonic-gate 	if (BITX(cp->cp_ebx, 31, 16) == 0) {
33520Sstevel@tonic-gate 		add_amd_l2_tlb(devi, "l2-tlb-4K",
33530Sstevel@tonic-gate 		    BITX(cp->cp_eax, 15, 12), BITX(cp->cp_eax, 11, 0));
33540Sstevel@tonic-gate 	} else {
33550Sstevel@tonic-gate 		add_amd_l2_tlb(devi, "l2-dtlb-4K",
33560Sstevel@tonic-gate 		    BITX(cp->cp_eax, 31, 28), BITX(cp->cp_eax, 27, 16));
33570Sstevel@tonic-gate 		add_amd_l2_tlb(devi, "l2-itlb-4K",
33580Sstevel@tonic-gate 		    BITX(cp->cp_eax, 15, 12), BITX(cp->cp_eax, 11, 0));
33590Sstevel@tonic-gate 	}
33600Sstevel@tonic-gate 
33610Sstevel@tonic-gate 	add_amd_l2_cache(devi, l2_cache_str,
33620Sstevel@tonic-gate 	    BITX(cp->cp_ecx, 31, 16), BITX(cp->cp_ecx, 15, 12),
33630Sstevel@tonic-gate 	    BITX(cp->cp_ecx, 11, 8), BITX(cp->cp_ecx, 7, 0));
33640Sstevel@tonic-gate }
33650Sstevel@tonic-gate 
33660Sstevel@tonic-gate /*
33670Sstevel@tonic-gate  * There are two basic ways that the x86 world describes it cache
33680Sstevel@tonic-gate  * and tlb architecture - Intel's way and AMD's way.
33690Sstevel@tonic-gate  *
33700Sstevel@tonic-gate  * Return which flavor of cache architecture we should use
33710Sstevel@tonic-gate  */
33720Sstevel@tonic-gate static int
33730Sstevel@tonic-gate x86_which_cacheinfo(struct cpuid_info *cpi)
33740Sstevel@tonic-gate {
33750Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
33760Sstevel@tonic-gate 	case X86_VENDOR_Intel:
33770Sstevel@tonic-gate 		if (cpi->cpi_maxeax >= 2)
33780Sstevel@tonic-gate 			return (X86_VENDOR_Intel);
33790Sstevel@tonic-gate 		break;
33800Sstevel@tonic-gate 	case X86_VENDOR_AMD:
33810Sstevel@tonic-gate 		/*
33820Sstevel@tonic-gate 		 * The K5 model 1 was the first part from AMD that reported
33830Sstevel@tonic-gate 		 * cache sizes via extended cpuid functions.
33840Sstevel@tonic-gate 		 */
33850Sstevel@tonic-gate 		if (cpi->cpi_family > 5 ||
33860Sstevel@tonic-gate 		    (cpi->cpi_family == 5 && cpi->cpi_model >= 1))
33870Sstevel@tonic-gate 			return (X86_VENDOR_AMD);
33880Sstevel@tonic-gate 		break;
33890Sstevel@tonic-gate 	case X86_VENDOR_TM:
33900Sstevel@tonic-gate 		if (cpi->cpi_family >= 5)
33910Sstevel@tonic-gate 			return (X86_VENDOR_AMD);
33920Sstevel@tonic-gate 		/*FALLTHROUGH*/
33930Sstevel@tonic-gate 	default:
33940Sstevel@tonic-gate 		/*
33950Sstevel@tonic-gate 		 * If they have extended CPU data for 0x80000005
33960Sstevel@tonic-gate 		 * then we assume they have AMD-format cache
33970Sstevel@tonic-gate 		 * information.
33980Sstevel@tonic-gate 		 *
33990Sstevel@tonic-gate 		 * If not, and the vendor happens to be Cyrix,
34000Sstevel@tonic-gate 		 * then try our-Cyrix specific handler.
34010Sstevel@tonic-gate 		 *
34020Sstevel@tonic-gate 		 * If we're not Cyrix, then assume we're using Intel's
34030Sstevel@tonic-gate 		 * table-driven format instead.
34040Sstevel@tonic-gate 		 */
34050Sstevel@tonic-gate 		if (cpi->cpi_xmaxeax >= 0x80000005)
34060Sstevel@tonic-gate 			return (X86_VENDOR_AMD);
34070Sstevel@tonic-gate 		else if (cpi->cpi_vendor == X86_VENDOR_Cyrix)
34080Sstevel@tonic-gate 			return (X86_VENDOR_Cyrix);
34090Sstevel@tonic-gate 		else if (cpi->cpi_maxeax >= 2)
34100Sstevel@tonic-gate 			return (X86_VENDOR_Intel);
34110Sstevel@tonic-gate 		break;
34120Sstevel@tonic-gate 	}
34130Sstevel@tonic-gate 	return (-1);
34140Sstevel@tonic-gate }
34150Sstevel@tonic-gate 
34160Sstevel@tonic-gate /*
34170Sstevel@tonic-gate  * create a node for the given cpu under the prom root node.
34180Sstevel@tonic-gate  * Also, create a cpu node in the device tree.
34190Sstevel@tonic-gate  */
34200Sstevel@tonic-gate static dev_info_t *cpu_nex_devi = NULL;
34210Sstevel@tonic-gate static kmutex_t cpu_node_lock;
34220Sstevel@tonic-gate 
34230Sstevel@tonic-gate /*
34240Sstevel@tonic-gate  * Called from post_startup() and mp_startup()
34250Sstevel@tonic-gate  */
34260Sstevel@tonic-gate void
34270Sstevel@tonic-gate add_cpunode2devtree(processorid_t cpu_id, struct cpuid_info *cpi)
34280Sstevel@tonic-gate {
34290Sstevel@tonic-gate 	dev_info_t *cpu_devi;
34300Sstevel@tonic-gate 	int create;
34310Sstevel@tonic-gate 
34320Sstevel@tonic-gate 	mutex_enter(&cpu_node_lock);
34330Sstevel@tonic-gate 
34340Sstevel@tonic-gate 	/*
34350Sstevel@tonic-gate 	 * create a nexus node for all cpus identified as 'cpu_id' under
34360Sstevel@tonic-gate 	 * the root node.
34370Sstevel@tonic-gate 	 */
34380Sstevel@tonic-gate 	if (cpu_nex_devi == NULL) {
34390Sstevel@tonic-gate 		if (ndi_devi_alloc(ddi_root_node(), "cpus",
3440789Sahrens 		    (pnode_t)DEVI_SID_NODEID, &cpu_nex_devi) != NDI_SUCCESS) {
34410Sstevel@tonic-gate 			mutex_exit(&cpu_node_lock);
34420Sstevel@tonic-gate 			return;
34430Sstevel@tonic-gate 		}
34440Sstevel@tonic-gate 		(void) ndi_devi_online(cpu_nex_devi, 0);
34450Sstevel@tonic-gate 	}
34460Sstevel@tonic-gate 
34470Sstevel@tonic-gate 	/*
34480Sstevel@tonic-gate 	 * create a child node for cpu identified as 'cpu_id'
34490Sstevel@tonic-gate 	 */
34500Sstevel@tonic-gate 	cpu_devi = ddi_add_child(cpu_nex_devi, "cpu", DEVI_SID_NODEID,
34514481Sbholler 	    cpu_id);
34520Sstevel@tonic-gate 	if (cpu_devi == NULL) {
34530Sstevel@tonic-gate 		mutex_exit(&cpu_node_lock);
34540Sstevel@tonic-gate 		return;
34550Sstevel@tonic-gate 	}
34560Sstevel@tonic-gate 
34570Sstevel@tonic-gate 	/* device_type */
34580Sstevel@tonic-gate 
34590Sstevel@tonic-gate 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, cpu_devi,
34600Sstevel@tonic-gate 	    "device_type", "cpu");
34610Sstevel@tonic-gate 
34620Sstevel@tonic-gate 	/* reg */
34630Sstevel@tonic-gate 
34640Sstevel@tonic-gate 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
34650Sstevel@tonic-gate 	    "reg", cpu_id);
34660Sstevel@tonic-gate 
34670Sstevel@tonic-gate 	/* cpu-mhz, and clock-frequency */
34680Sstevel@tonic-gate 
34690Sstevel@tonic-gate 	if (cpu_freq > 0) {
34700Sstevel@tonic-gate 		long long mul;
34710Sstevel@tonic-gate 
34720Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
34730Sstevel@tonic-gate 		    "cpu-mhz", cpu_freq);
34740Sstevel@tonic-gate 
34750Sstevel@tonic-gate 		if ((mul = cpu_freq * 1000000LL) <= INT_MAX)
34760Sstevel@tonic-gate 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
34770Sstevel@tonic-gate 			    "clock-frequency", (int)mul);
34780Sstevel@tonic-gate 	}
34790Sstevel@tonic-gate 
34800Sstevel@tonic-gate 	(void) ndi_devi_online(cpu_devi, 0);
34810Sstevel@tonic-gate 
34820Sstevel@tonic-gate 	if ((x86_feature & X86_CPUID) == 0) {
34830Sstevel@tonic-gate 		mutex_exit(&cpu_node_lock);
34840Sstevel@tonic-gate 		return;
34850Sstevel@tonic-gate 	}
34860Sstevel@tonic-gate 
34870Sstevel@tonic-gate 	/* vendor-id */
34880Sstevel@tonic-gate 
34890Sstevel@tonic-gate 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, cpu_devi,
34904481Sbholler 	    "vendor-id", cpi->cpi_vendorstr);
34910Sstevel@tonic-gate 
34920Sstevel@tonic-gate 	if (cpi->cpi_maxeax == 0) {
34930Sstevel@tonic-gate 		mutex_exit(&cpu_node_lock);
34940Sstevel@tonic-gate 		return;
34950Sstevel@tonic-gate 	}
34960Sstevel@tonic-gate 
34970Sstevel@tonic-gate 	/*
34980Sstevel@tonic-gate 	 * family, model, and step
34990Sstevel@tonic-gate 	 */
35000Sstevel@tonic-gate 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
35014481Sbholler 	    "family", CPI_FAMILY(cpi));
35020Sstevel@tonic-gate 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
35034481Sbholler 	    "cpu-model", CPI_MODEL(cpi));
35040Sstevel@tonic-gate 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
35054481Sbholler 	    "stepping-id", CPI_STEP(cpi));
35060Sstevel@tonic-gate 
35070Sstevel@tonic-gate 	/* type */
35080Sstevel@tonic-gate 
35090Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
35100Sstevel@tonic-gate 	case X86_VENDOR_Intel:
35110Sstevel@tonic-gate 		create = 1;
35120Sstevel@tonic-gate 		break;
35130Sstevel@tonic-gate 	default:
35140Sstevel@tonic-gate 		create = 0;
35150Sstevel@tonic-gate 		break;
35160Sstevel@tonic-gate 	}
35170Sstevel@tonic-gate 	if (create)
35180Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
35194481Sbholler 		    "type", CPI_TYPE(cpi));
35200Sstevel@tonic-gate 
35210Sstevel@tonic-gate 	/* ext-family */
35220Sstevel@tonic-gate 
35230Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
35240Sstevel@tonic-gate 	case X86_VENDOR_Intel:
35250Sstevel@tonic-gate 	case X86_VENDOR_AMD:
35260Sstevel@tonic-gate 		create = cpi->cpi_family >= 0xf;
35270Sstevel@tonic-gate 		break;
35280Sstevel@tonic-gate 	default:
35290Sstevel@tonic-gate 		create = 0;
35300Sstevel@tonic-gate 		break;
35310Sstevel@tonic-gate 	}
35320Sstevel@tonic-gate 	if (create)
35330Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
35340Sstevel@tonic-gate 		    "ext-family", CPI_FAMILY_XTD(cpi));
35350Sstevel@tonic-gate 
35360Sstevel@tonic-gate 	/* ext-model */
35370Sstevel@tonic-gate 
35380Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
35390Sstevel@tonic-gate 	case X86_VENDOR_Intel:
35406317Skk208521 		create = IS_EXTENDED_MODEL_INTEL(cpi);
35412001Sdmick 		break;
35420Sstevel@tonic-gate 	case X86_VENDOR_AMD:
35431582Skchow 		create = CPI_FAMILY(cpi) == 0xf;
35440Sstevel@tonic-gate 		break;
35450Sstevel@tonic-gate 	default:
35460Sstevel@tonic-gate 		create = 0;
35470Sstevel@tonic-gate 		break;
35480Sstevel@tonic-gate 	}
35490Sstevel@tonic-gate 	if (create)
35500Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
35514481Sbholler 		    "ext-model", CPI_MODEL_XTD(cpi));
35520Sstevel@tonic-gate 
35530Sstevel@tonic-gate 	/* generation */
35540Sstevel@tonic-gate 
35550Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
35560Sstevel@tonic-gate 	case X86_VENDOR_AMD:
35570Sstevel@tonic-gate 		/*
35580Sstevel@tonic-gate 		 * AMD K5 model 1 was the first part to support this
35590Sstevel@tonic-gate 		 */
35600Sstevel@tonic-gate 		create = cpi->cpi_xmaxeax >= 0x80000001;
35610Sstevel@tonic-gate 		break;
35620Sstevel@tonic-gate 	default:
35630Sstevel@tonic-gate 		create = 0;
35640Sstevel@tonic-gate 		break;
35650Sstevel@tonic-gate 	}
35660Sstevel@tonic-gate 	if (create)
35670Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
35680Sstevel@tonic-gate 		    "generation", BITX((cpi)->cpi_extd[1].cp_eax, 11, 8));
35690Sstevel@tonic-gate 
35700Sstevel@tonic-gate 	/* brand-id */
35710Sstevel@tonic-gate 
35720Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
35730Sstevel@tonic-gate 	case X86_VENDOR_Intel:
35740Sstevel@tonic-gate 		/*
35750Sstevel@tonic-gate 		 * brand id first appeared on Pentium III Xeon model 8,
35760Sstevel@tonic-gate 		 * and Celeron model 8 processors and Opteron
35770Sstevel@tonic-gate 		 */
35780Sstevel@tonic-gate 		create = cpi->cpi_family > 6 ||
35790Sstevel@tonic-gate 		    (cpi->cpi_family == 6 && cpi->cpi_model >= 8);
35800Sstevel@tonic-gate 		break;
35810Sstevel@tonic-gate 	case X86_VENDOR_AMD:
35820Sstevel@tonic-gate 		create = cpi->cpi_family >= 0xf;
35830Sstevel@tonic-gate 		break;
35840Sstevel@tonic-gate 	default:
35850Sstevel@tonic-gate 		create = 0;
35860Sstevel@tonic-gate 		break;
35870Sstevel@tonic-gate 	}
35880Sstevel@tonic-gate 	if (create && cpi->cpi_brandid != 0) {
35890Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
35900Sstevel@tonic-gate 		    "brand-id", cpi->cpi_brandid);
35910Sstevel@tonic-gate 	}
35920Sstevel@tonic-gate 
35930Sstevel@tonic-gate 	/* chunks, and apic-id */
35940Sstevel@tonic-gate 
35950Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
35960Sstevel@tonic-gate 		/*
35970Sstevel@tonic-gate 		 * first available on Pentium IV and Opteron (K8)
35980Sstevel@tonic-gate 		 */
35991975Sdmick 	case X86_VENDOR_Intel:
36001975Sdmick 		create = IS_NEW_F6(cpi) || cpi->cpi_family >= 0xf;
36011975Sdmick 		break;
36021975Sdmick 	case X86_VENDOR_AMD:
36030Sstevel@tonic-gate 		create = cpi->cpi_family >= 0xf;
36040Sstevel@tonic-gate 		break;
36050Sstevel@tonic-gate 	default:
36060Sstevel@tonic-gate 		create = 0;
36070Sstevel@tonic-gate 		break;
36080Sstevel@tonic-gate 	}
36090Sstevel@tonic-gate 	if (create) {
36100Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
36114481Sbholler 		    "chunks", CPI_CHUNKS(cpi));
36120Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
36137282Smishra 		    "apic-id", cpi->cpi_apicid);
36141414Scindi 		if (cpi->cpi_chipid >= 0) {
36150Sstevel@tonic-gate 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
36160Sstevel@tonic-gate 			    "chip#", cpi->cpi_chipid);
36171414Scindi 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
36181414Scindi 			    "clog#", cpi->cpi_clogid);
36191414Scindi 		}
36200Sstevel@tonic-gate 	}
36210Sstevel@tonic-gate 
36220Sstevel@tonic-gate 	/* cpuid-features */
36230Sstevel@tonic-gate 
36240Sstevel@tonic-gate 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
36250Sstevel@tonic-gate 	    "cpuid-features", CPI_FEATURES_EDX(cpi));
36260Sstevel@tonic-gate 
36270Sstevel@tonic-gate 
36280Sstevel@tonic-gate 	/* cpuid-features-ecx */
36290Sstevel@tonic-gate 
36300Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
36310Sstevel@tonic-gate 	case X86_VENDOR_Intel:
36321975Sdmick 		create = IS_NEW_F6(cpi) || cpi->cpi_family >= 0xf;
36330Sstevel@tonic-gate 		break;
36340Sstevel@tonic-gate 	default:
36350Sstevel@tonic-gate 		create = 0;
36360Sstevel@tonic-gate 		break;
36370Sstevel@tonic-gate 	}
36380Sstevel@tonic-gate 	if (create)
36390Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
36400Sstevel@tonic-gate 		    "cpuid-features-ecx", CPI_FEATURES_ECX(cpi));
36410Sstevel@tonic-gate 
36420Sstevel@tonic-gate 	/* ext-cpuid-features */
36430Sstevel@tonic-gate 
36440Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
36451975Sdmick 	case X86_VENDOR_Intel:
36460Sstevel@tonic-gate 	case X86_VENDOR_AMD:
36470Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
36480Sstevel@tonic-gate 	case X86_VENDOR_TM:
36490Sstevel@tonic-gate 	case X86_VENDOR_Centaur:
36500Sstevel@tonic-gate 		create = cpi->cpi_xmaxeax >= 0x80000001;
36510Sstevel@tonic-gate 		break;
36520Sstevel@tonic-gate 	default:
36530Sstevel@tonic-gate 		create = 0;
36540Sstevel@tonic-gate 		break;
36550Sstevel@tonic-gate 	}
36561975Sdmick 	if (create) {
36570Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
36584481Sbholler 		    "ext-cpuid-features", CPI_FEATURES_XTD_EDX(cpi));
36591975Sdmick 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
36604481Sbholler 		    "ext-cpuid-features-ecx", CPI_FEATURES_XTD_ECX(cpi));
36611975Sdmick 	}
36620Sstevel@tonic-gate 
36630Sstevel@tonic-gate 	/*
36640Sstevel@tonic-gate 	 * Brand String first appeared in Intel Pentium IV, AMD K5
36650Sstevel@tonic-gate 	 * model 1, and Cyrix GXm.  On earlier models we try and
36660Sstevel@tonic-gate 	 * simulate something similar .. so this string should always
36670Sstevel@tonic-gate 	 * same -something- about the processor, however lame.
36680Sstevel@tonic-gate 	 */
36690Sstevel@tonic-gate 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, cpu_devi,
36700Sstevel@tonic-gate 	    "brand-string", cpi->cpi_brandstr);
36710Sstevel@tonic-gate 
36720Sstevel@tonic-gate 	/*
36730Sstevel@tonic-gate 	 * Finally, cache and tlb information
36740Sstevel@tonic-gate 	 */
36750Sstevel@tonic-gate 	switch (x86_which_cacheinfo(cpi)) {
36760Sstevel@tonic-gate 	case X86_VENDOR_Intel:
36770Sstevel@tonic-gate 		intel_walk_cacheinfo(cpi, cpu_devi, add_cacheent_props);
36780Sstevel@tonic-gate 		break;
36790Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
36800Sstevel@tonic-gate 		cyrix_walk_cacheinfo(cpi, cpu_devi, add_cacheent_props);
36810Sstevel@tonic-gate 		break;
36820Sstevel@tonic-gate 	case X86_VENDOR_AMD:
36830Sstevel@tonic-gate 		amd_cache_info(cpi, cpu_devi);
36840Sstevel@tonic-gate 		break;
36850Sstevel@tonic-gate 	default:
36860Sstevel@tonic-gate 		break;
36870Sstevel@tonic-gate 	}
36880Sstevel@tonic-gate 
36890Sstevel@tonic-gate 	mutex_exit(&cpu_node_lock);
36900Sstevel@tonic-gate }
36910Sstevel@tonic-gate 
36920Sstevel@tonic-gate struct l2info {
36930Sstevel@tonic-gate 	int *l2i_csz;
36940Sstevel@tonic-gate 	int *l2i_lsz;
36950Sstevel@tonic-gate 	int *l2i_assoc;
36960Sstevel@tonic-gate 	int l2i_ret;
36970Sstevel@tonic-gate };
36980Sstevel@tonic-gate 
36990Sstevel@tonic-gate /*
37000Sstevel@tonic-gate  * A cacheinfo walker that fetches the size, line-size and associativity
37010Sstevel@tonic-gate  * of the L2 cache
37020Sstevel@tonic-gate  */
37030Sstevel@tonic-gate static int
37040Sstevel@tonic-gate intel_l2cinfo(void *arg, const struct cachetab *ct)
37050Sstevel@tonic-gate {
37060Sstevel@tonic-gate 	struct l2info *l2i = arg;
37070Sstevel@tonic-gate 	int *ip;
37080Sstevel@tonic-gate 
37090Sstevel@tonic-gate 	if (ct->ct_label != l2_cache_str &&
37100Sstevel@tonic-gate 	    ct->ct_label != sl2_cache_str)
37110Sstevel@tonic-gate 		return (0);	/* not an L2 -- keep walking */
37120Sstevel@tonic-gate 
37130Sstevel@tonic-gate 	if ((ip = l2i->l2i_csz) != NULL)
37140Sstevel@tonic-gate 		*ip = ct->ct_size;
37150Sstevel@tonic-gate 	if ((ip = l2i->l2i_lsz) != NULL)
37160Sstevel@tonic-gate 		*ip = ct->ct_line_size;
37170Sstevel@tonic-gate 	if ((ip = l2i->l2i_assoc) != NULL)
37180Sstevel@tonic-gate 		*ip = ct->ct_assoc;
37190Sstevel@tonic-gate 	l2i->l2i_ret = ct->ct_size;
37200Sstevel@tonic-gate 	return (1);		/* was an L2 -- terminate walk */
37210Sstevel@tonic-gate }
37220Sstevel@tonic-gate 
37235070Skchow /*
37245070Skchow  * AMD L2/L3 Cache and TLB Associativity Field Definition:
37255070Skchow  *
37265070Skchow  *	Unlike the associativity for the L1 cache and tlb where the 8 bit
37275070Skchow  *	value is the associativity, the associativity for the L2 cache and
37285070Skchow  *	tlb is encoded in the following table. The 4 bit L2 value serves as
37295070Skchow  *	an index into the amd_afd[] array to determine the associativity.
37305070Skchow  *	-1 is undefined. 0 is fully associative.
37315070Skchow  */
37325070Skchow 
37335070Skchow static int amd_afd[] =
37345070Skchow 	{-1, 1, 2, -1, 4, -1, 8, -1, 16, -1, 32, 48, 64, 96, 128, 0};
37355070Skchow 
37360Sstevel@tonic-gate static void
37370Sstevel@tonic-gate amd_l2cacheinfo(struct cpuid_info *cpi, struct l2info *l2i)
37380Sstevel@tonic-gate {
37391228Sandrei 	struct cpuid_regs *cp;
37400Sstevel@tonic-gate 	uint_t size, assoc;
37415070Skchow 	int i;
37420Sstevel@tonic-gate 	int *ip;
37430Sstevel@tonic-gate 
37440Sstevel@tonic-gate 	if (cpi->cpi_xmaxeax < 0x80000006)
37450Sstevel@tonic-gate 		return;
37460Sstevel@tonic-gate 	cp = &cpi->cpi_extd[6];
37470Sstevel@tonic-gate 
37485070Skchow 	if ((i = BITX(cp->cp_ecx, 15, 12)) != 0 &&
37490Sstevel@tonic-gate 	    (size = BITX(cp->cp_ecx, 31, 16)) != 0) {
37500Sstevel@tonic-gate 		uint_t cachesz = size * 1024;
37515070Skchow 		assoc = amd_afd[i];
37525070Skchow 
37535070Skchow 		ASSERT(assoc != -1);
37540Sstevel@tonic-gate 
37550Sstevel@tonic-gate 		if ((ip = l2i->l2i_csz) != NULL)
37560Sstevel@tonic-gate 			*ip = cachesz;
37570Sstevel@tonic-gate 		if ((ip = l2i->l2i_lsz) != NULL)
37580Sstevel@tonic-gate 			*ip = BITX(cp->cp_ecx, 7, 0);
37590Sstevel@tonic-gate 		if ((ip = l2i->l2i_assoc) != NULL)
37600Sstevel@tonic-gate 			*ip = assoc;
37610Sstevel@tonic-gate 		l2i->l2i_ret = cachesz;
37620Sstevel@tonic-gate 	}
37630Sstevel@tonic-gate }
37640Sstevel@tonic-gate 
37650Sstevel@tonic-gate int
37660Sstevel@tonic-gate getl2cacheinfo(cpu_t *cpu, int *csz, int *lsz, int *assoc)
37670Sstevel@tonic-gate {
37680Sstevel@tonic-gate 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
37690Sstevel@tonic-gate 	struct l2info __l2info, *l2i = &__l2info;
37700Sstevel@tonic-gate 
37710Sstevel@tonic-gate 	l2i->l2i_csz = csz;
37720Sstevel@tonic-gate 	l2i->l2i_lsz = lsz;
37730Sstevel@tonic-gate 	l2i->l2i_assoc = assoc;
37740Sstevel@tonic-gate 	l2i->l2i_ret = -1;
37750Sstevel@tonic-gate 
37760Sstevel@tonic-gate 	switch (x86_which_cacheinfo(cpi)) {
37770Sstevel@tonic-gate 	case X86_VENDOR_Intel:
37780Sstevel@tonic-gate 		intel_walk_cacheinfo(cpi, l2i, intel_l2cinfo);
37790Sstevel@tonic-gate 		break;
37800Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
37810Sstevel@tonic-gate 		cyrix_walk_cacheinfo(cpi, l2i, intel_l2cinfo);
37820Sstevel@tonic-gate 		break;
37830Sstevel@tonic-gate 	case X86_VENDOR_AMD:
37840Sstevel@tonic-gate 		amd_l2cacheinfo(cpi, l2i);
37850Sstevel@tonic-gate 		break;
37860Sstevel@tonic-gate 	default:
37870Sstevel@tonic-gate 		break;
37880Sstevel@tonic-gate 	}
37890Sstevel@tonic-gate 	return (l2i->l2i_ret);
37900Sstevel@tonic-gate }
37914481Sbholler 
37925084Sjohnlev #if !defined(__xpv)
37935084Sjohnlev 
37945045Sbholler uint32_t *
37955045Sbholler cpuid_mwait_alloc(cpu_t *cpu)
37965045Sbholler {
37975045Sbholler 	uint32_t	*ret;
37985045Sbholler 	size_t		mwait_size;
37995045Sbholler 
38005045Sbholler 	ASSERT(cpuid_checkpass(cpu, 2));
38015045Sbholler 
38025045Sbholler 	mwait_size = cpu->cpu_m.mcpu_cpi->cpi_mwait.mon_max;
38035045Sbholler 	if (mwait_size == 0)
38045045Sbholler 		return (NULL);
38055045Sbholler 
38065045Sbholler 	/*
38075045Sbholler 	 * kmem_alloc() returns cache line size aligned data for mwait_size
38085045Sbholler 	 * allocations.  mwait_size is currently cache line sized.  Neither
38095045Sbholler 	 * of these implementation details are guarantied to be true in the
38105045Sbholler 	 * future.
38115045Sbholler 	 *
38125045Sbholler 	 * First try allocating mwait_size as kmem_alloc() currently returns
38135045Sbholler 	 * correctly aligned memory.  If kmem_alloc() does not return
38145045Sbholler 	 * mwait_size aligned memory, then use mwait_size ROUNDUP.
38155045Sbholler 	 *
38165045Sbholler 	 * Set cpi_mwait.buf_actual and cpi_mwait.size_actual in case we
38175045Sbholler 	 * decide to free this memory.
38185045Sbholler 	 */
38195045Sbholler 	ret = kmem_zalloc(mwait_size, KM_SLEEP);
38205045Sbholler 	if (ret == (uint32_t *)P2ROUNDUP((uintptr_t)ret, mwait_size)) {
38215045Sbholler 		cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual = ret;
38225045Sbholler 		cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual = mwait_size;
38235045Sbholler 		*ret = MWAIT_RUNNING;
38245045Sbholler 		return (ret);
38255045Sbholler 	} else {
38265045Sbholler 		kmem_free(ret, mwait_size);
38275045Sbholler 		ret = kmem_zalloc(mwait_size * 2, KM_SLEEP);
38285045Sbholler 		cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual = ret;
38295045Sbholler 		cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual = mwait_size * 2;
38305045Sbholler 		ret = (uint32_t *)P2ROUNDUP((uintptr_t)ret, mwait_size);
38315045Sbholler 		*ret = MWAIT_RUNNING;
38325045Sbholler 		return (ret);
38335045Sbholler 	}
38345045Sbholler }
38355045Sbholler 
38365045Sbholler void
38375045Sbholler cpuid_mwait_free(cpu_t *cpu)
38384481Sbholler {
38394481Sbholler 	ASSERT(cpuid_checkpass(cpu, 2));
38405045Sbholler 
38415045Sbholler 	if (cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual != NULL &&
38425045Sbholler 	    cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual > 0) {
38435045Sbholler 		kmem_free(cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual,
38445045Sbholler 		    cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual);
38455045Sbholler 	}
38465045Sbholler 
38475045Sbholler 	cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual = NULL;
38485045Sbholler 	cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual = 0;
38494481Sbholler }
38505084Sjohnlev 
38515322Ssudheer void
38525322Ssudheer patch_tsc_read(int flag)
38535322Ssudheer {
38545322Ssudheer 	size_t cnt;
38557532SSean.Ye@Sun.COM 
38565322Ssudheer 	switch (flag) {
38575322Ssudheer 	case X86_NO_TSC:
38585322Ssudheer 		cnt = &_no_rdtsc_end - &_no_rdtsc_start;
38595338Ssudheer 		(void) memcpy((void *)tsc_read, (void *)&_no_rdtsc_start, cnt);
38605322Ssudheer 		break;
38615322Ssudheer 	case X86_HAVE_TSCP:
38625322Ssudheer 		cnt = &_tscp_end - &_tscp_start;
38635338Ssudheer 		(void) memcpy((void *)tsc_read, (void *)&_tscp_start, cnt);
38645322Ssudheer 		break;
38655322Ssudheer 	case X86_TSC_MFENCE:
38665322Ssudheer 		cnt = &_tsc_mfence_end - &_tsc_mfence_start;
38675338Ssudheer 		(void) memcpy((void *)tsc_read,
38685338Ssudheer 		    (void *)&_tsc_mfence_start, cnt);
38695322Ssudheer 		break;
38706642Ssudheer 	case X86_TSC_LFENCE:
38716642Ssudheer 		cnt = &_tsc_lfence_end - &_tsc_lfence_start;
38726642Ssudheer 		(void) memcpy((void *)tsc_read,
38736642Ssudheer 		    (void *)&_tsc_lfence_start, cnt);
38746642Ssudheer 		break;
38755322Ssudheer 	default:
38765322Ssudheer 		break;
38775322Ssudheer 	}
38785322Ssudheer }
38795322Ssudheer 
38808906SEric.Saxe@Sun.COM int
38818906SEric.Saxe@Sun.COM cpuid_deep_cstates_supported(void)
38828906SEric.Saxe@Sun.COM {
38838906SEric.Saxe@Sun.COM 	struct cpuid_info *cpi;
38848906SEric.Saxe@Sun.COM 	struct cpuid_regs regs;
38858906SEric.Saxe@Sun.COM 
38868906SEric.Saxe@Sun.COM 	ASSERT(cpuid_checkpass(CPU, 1));
38878906SEric.Saxe@Sun.COM 
38888906SEric.Saxe@Sun.COM 	cpi = CPU->cpu_m.mcpu_cpi;
38898906SEric.Saxe@Sun.COM 
38908906SEric.Saxe@Sun.COM 	if (!(x86_feature & X86_CPUID))
38918906SEric.Saxe@Sun.COM 		return (0);
38928906SEric.Saxe@Sun.COM 
38938906SEric.Saxe@Sun.COM 	switch (cpi->cpi_vendor) {
38948906SEric.Saxe@Sun.COM 	case X86_VENDOR_Intel:
38958906SEric.Saxe@Sun.COM 		if (cpi->cpi_xmaxeax < 0x80000007)
38968906SEric.Saxe@Sun.COM 			return (0);
38978906SEric.Saxe@Sun.COM 
38988906SEric.Saxe@Sun.COM 		/*
38998906SEric.Saxe@Sun.COM 		 * TSC run at a constant rate in all ACPI C-states?
39008906SEric.Saxe@Sun.COM 		 */
39018906SEric.Saxe@Sun.COM 		regs.cp_eax = 0x80000007;
39028906SEric.Saxe@Sun.COM 		(void) __cpuid_insn(&regs);
39038906SEric.Saxe@Sun.COM 		return (regs.cp_edx & CPUID_TSC_CSTATE_INVARIANCE);
39048906SEric.Saxe@Sun.COM 
39058906SEric.Saxe@Sun.COM 	default:
39068906SEric.Saxe@Sun.COM 		return (0);
39078906SEric.Saxe@Sun.COM 	}
39088906SEric.Saxe@Sun.COM }
39098906SEric.Saxe@Sun.COM 
39108930SBill.Holler@Sun.COM #endif	/* !__xpv */
39118930SBill.Holler@Sun.COM 
39128930SBill.Holler@Sun.COM void
39138930SBill.Holler@Sun.COM post_startup_cpu_fixups(void)
39148930SBill.Holler@Sun.COM {
39158930SBill.Holler@Sun.COM #ifndef __xpv
39168930SBill.Holler@Sun.COM 	/*
39178930SBill.Holler@Sun.COM 	 * Some AMD processors support C1E state. Entering this state will
39188930SBill.Holler@Sun.COM 	 * cause the local APIC timer to stop, which we can't deal with at
39198930SBill.Holler@Sun.COM 	 * this time.
39208930SBill.Holler@Sun.COM 	 */
39218930SBill.Holler@Sun.COM 	if (cpuid_getvendor(CPU) == X86_VENDOR_AMD) {
39228930SBill.Holler@Sun.COM 		on_trap_data_t otd;
39238930SBill.Holler@Sun.COM 		uint64_t reg;
39248930SBill.Holler@Sun.COM 
39258930SBill.Holler@Sun.COM 		if (!on_trap(&otd, OT_DATA_ACCESS)) {
39268930SBill.Holler@Sun.COM 			reg = rdmsr(MSR_AMD_INT_PENDING_CMP_HALT);
39278930SBill.Holler@Sun.COM 			/* Disable C1E state if it is enabled by BIOS */
39288930SBill.Holler@Sun.COM 			if ((reg >> AMD_ACTONCMPHALT_SHIFT) &
39298930SBill.Holler@Sun.COM 			    AMD_ACTONCMPHALT_MASK) {
39308930SBill.Holler@Sun.COM 				reg &= ~(AMD_ACTONCMPHALT_MASK <<
39318930SBill.Holler@Sun.COM 				    AMD_ACTONCMPHALT_SHIFT);
39328930SBill.Holler@Sun.COM 				wrmsr(MSR_AMD_INT_PENDING_CMP_HALT, reg);
39338930SBill.Holler@Sun.COM 			}
39348930SBill.Holler@Sun.COM 		}
39358930SBill.Holler@Sun.COM 		no_trap();
39368930SBill.Holler@Sun.COM 	}
39378930SBill.Holler@Sun.COM #endif	/* !__xpv */
39388930SBill.Holler@Sun.COM }
39398930SBill.Holler@Sun.COM 
39408377SBill.Holler@Sun.COM #if defined(__amd64) && !defined(__xpv)
39418377SBill.Holler@Sun.COM /*
39428377SBill.Holler@Sun.COM  * Patch in versions of bcopy for high performance Intel Nhm processors
39438377SBill.Holler@Sun.COM  * and later...
39448377SBill.Holler@Sun.COM  */
39458377SBill.Holler@Sun.COM void
39468377SBill.Holler@Sun.COM patch_memops(uint_t vendor)
39478377SBill.Holler@Sun.COM {
39488377SBill.Holler@Sun.COM 	size_t cnt, i;
39498377SBill.Holler@Sun.COM 	caddr_t to, from;
39508377SBill.Holler@Sun.COM 
39518377SBill.Holler@Sun.COM 	if ((vendor == X86_VENDOR_Intel) && ((x86_feature & X86_SSE4_2) != 0)) {
39528377SBill.Holler@Sun.COM 		cnt = &bcopy_patch_end - &bcopy_patch_start;
39538377SBill.Holler@Sun.COM 		to = &bcopy_ck_size;
39548377SBill.Holler@Sun.COM 		from = &bcopy_patch_start;
39558377SBill.Holler@Sun.COM 		for (i = 0; i < cnt; i++) {
39568377SBill.Holler@Sun.COM 			*to++ = *from++;
39578377SBill.Holler@Sun.COM 		}
39588377SBill.Holler@Sun.COM 	}
39598377SBill.Holler@Sun.COM }
39608377SBill.Holler@Sun.COM #endif  /* __amd64 && !__xpv */
3961