xref: /onnv-gate/usr/src/uts/i86pc/os/cpuid.c (revision 12004:93f274d4a367)
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 /*
2211919SRod.Evans@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
259283SBill.Holler@Sun.COM /*
269283SBill.Holler@Sun.COM  * Copyright (c) 2009, Intel Corporation.
279283SBill.Holler@Sun.COM  * All rights reserved.
289283SBill.Holler@Sun.COM  */
2910947SSrihari.Venkatesan@Sun.COM /*
3010947SSrihari.Venkatesan@Sun.COM  * Portions Copyright 2009 Advanced Micro Devices, Inc.
3110947SSrihari.Venkatesan@Sun.COM  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate /*
340Sstevel@tonic-gate  * Various routines to handle identification
350Sstevel@tonic-gate  * and classification of x86 processors.
360Sstevel@tonic-gate  */
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #include <sys/types.h>
390Sstevel@tonic-gate #include <sys/archsystm.h>
400Sstevel@tonic-gate #include <sys/x86_archext.h>
410Sstevel@tonic-gate #include <sys/kmem.h>
420Sstevel@tonic-gate #include <sys/systm.h>
430Sstevel@tonic-gate #include <sys/cmn_err.h>
440Sstevel@tonic-gate #include <sys/sunddi.h>
450Sstevel@tonic-gate #include <sys/sunndi.h>
460Sstevel@tonic-gate #include <sys/cpuvar.h>
470Sstevel@tonic-gate #include <sys/processor.h>
485045Sbholler #include <sys/sysmacros.h>
493434Sesaxe #include <sys/pg.h>
500Sstevel@tonic-gate #include <sys/fp.h>
510Sstevel@tonic-gate #include <sys/controlregs.h>
520Sstevel@tonic-gate #include <sys/auxv_386.h>
530Sstevel@tonic-gate #include <sys/bitmap.h>
540Sstevel@tonic-gate #include <sys/memnode.h>
5510947SSrihari.Venkatesan@Sun.COM #include <sys/pci_cfgspace.h>
560Sstevel@tonic-gate 
577532SSean.Ye@Sun.COM #ifdef __xpv
587532SSean.Ye@Sun.COM #include <sys/hypervisor.h>
598930SBill.Holler@Sun.COM #else
608930SBill.Holler@Sun.COM #include <sys/ontrap.h>
617532SSean.Ye@Sun.COM #endif
627532SSean.Ye@Sun.COM 
630Sstevel@tonic-gate /*
640Sstevel@tonic-gate  * Pass 0 of cpuid feature analysis happens in locore. It contains special code
650Sstevel@tonic-gate  * to recognize Cyrix processors that are not cpuid-compliant, and to deal with
660Sstevel@tonic-gate  * them accordingly. For most modern processors, feature detection occurs here
670Sstevel@tonic-gate  * in pass 1.
680Sstevel@tonic-gate  *
690Sstevel@tonic-gate  * Pass 1 of cpuid feature analysis happens just at the beginning of mlsetup()
700Sstevel@tonic-gate  * for the boot CPU and does the basic analysis that the early kernel needs.
710Sstevel@tonic-gate  * x86_feature is set based on the return value of cpuid_pass1() of the boot
720Sstevel@tonic-gate  * CPU.
730Sstevel@tonic-gate  *
740Sstevel@tonic-gate  * Pass 1 includes:
750Sstevel@tonic-gate  *
760Sstevel@tonic-gate  *	o Determining vendor/model/family/stepping and setting x86_type and
770Sstevel@tonic-gate  *	  x86_vendor accordingly.
780Sstevel@tonic-gate  *	o Processing the feature flags returned by the cpuid instruction while
790Sstevel@tonic-gate  *	  applying any workarounds or tricks for the specific processor.
800Sstevel@tonic-gate  *	o Mapping the feature flags into Solaris feature bits (X86_*).
810Sstevel@tonic-gate  *	o Processing extended feature flags if supported by the processor,
820Sstevel@tonic-gate  *	  again while applying specific processor knowledge.
830Sstevel@tonic-gate  *	o Determining the CMT characteristics of the system.
840Sstevel@tonic-gate  *
850Sstevel@tonic-gate  * Pass 1 is done on non-boot CPUs during their initialization and the results
860Sstevel@tonic-gate  * are used only as a meager attempt at ensuring that all processors within the
870Sstevel@tonic-gate  * system support the same features.
880Sstevel@tonic-gate  *
890Sstevel@tonic-gate  * Pass 2 of cpuid feature analysis happens just at the beginning
900Sstevel@tonic-gate  * of startup().  It just copies in and corrects the remainder
910Sstevel@tonic-gate  * of the cpuid data we depend on: standard cpuid functions that we didn't
920Sstevel@tonic-gate  * need for pass1 feature analysis, and extended cpuid functions beyond the
930Sstevel@tonic-gate  * simple feature processing done in pass1.
940Sstevel@tonic-gate  *
950Sstevel@tonic-gate  * Pass 3 of cpuid analysis is invoked after basic kernel services; in
960Sstevel@tonic-gate  * particular kernel memory allocation has been made available. It creates a
970Sstevel@tonic-gate  * readable brand string based on the data collected in the first two passes.
980Sstevel@tonic-gate  *
990Sstevel@tonic-gate  * Pass 4 of cpuid analysis is invoked after post_startup() when all
1000Sstevel@tonic-gate  * the support infrastructure for various hardware features has been
1010Sstevel@tonic-gate  * initialized. It determines which processor features will be reported
1020Sstevel@tonic-gate  * to userland via the aux vector.
1030Sstevel@tonic-gate  *
1040Sstevel@tonic-gate  * All passes are executed on all CPUs, but only the boot CPU determines what
1050Sstevel@tonic-gate  * features the kernel will use.
1060Sstevel@tonic-gate  *
1070Sstevel@tonic-gate  * Much of the worst junk in this file is for the support of processors
1080Sstevel@tonic-gate  * that didn't really implement the cpuid instruction properly.
1090Sstevel@tonic-gate  *
1100Sstevel@tonic-gate  * NOTE: The accessor functions (cpuid_get*) are aware of, and ASSERT upon,
1110Sstevel@tonic-gate  * the pass numbers.  Accordingly, changes to the pass code may require changes
1120Sstevel@tonic-gate  * to the accessor code.
1130Sstevel@tonic-gate  */
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate uint_t x86_feature = 0;
1160Sstevel@tonic-gate uint_t x86_vendor = X86_VENDOR_IntelClone;
1170Sstevel@tonic-gate uint_t x86_type = X86_TYPE_OTHER;
1187589SVikram.Hegde@Sun.COM uint_t x86_clflush_size = 0;
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate uint_t pentiumpro_bug4046376;
1210Sstevel@tonic-gate uint_t pentiumpro_bug4064495;
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate uint_t enable486;
1248990SSurya.Prakki@Sun.COM /*
1259000SStuart.Maybee@Sun.COM  * This is set to platform type Solaris is running on.
1268990SSurya.Prakki@Sun.COM  */
12710175SStuart.Maybee@Sun.COM static int platform_type = -1;
12810175SStuart.Maybee@Sun.COM 
12910175SStuart.Maybee@Sun.COM #if !defined(__xpv)
13010175SStuart.Maybee@Sun.COM /*
13110175SStuart.Maybee@Sun.COM  * Variable to patch if hypervisor platform detection needs to be
13210175SStuart.Maybee@Sun.COM  * disabled (e.g. platform_type will always be HW_NATIVE if this is 0).
13310175SStuart.Maybee@Sun.COM  */
13410175SStuart.Maybee@Sun.COM int enable_platform_detection = 1;
13510175SStuart.Maybee@Sun.COM #endif
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate /*
1384481Sbholler  * monitor/mwait info.
1395045Sbholler  *
1405045Sbholler  * size_actual and buf_actual are the real address and size allocated to get
1415045Sbholler  * proper mwait_buf alignement.  buf_actual and size_actual should be passed
1425045Sbholler  * to kmem_free().  Currently kmem_alloc() and mwait happen to both use
1435045Sbholler  * processor cache-line alignment, but this is not guarantied in the furture.
1444481Sbholler  */
1454481Sbholler struct mwait_info {
1464481Sbholler 	size_t		mon_min;	/* min size to avoid missed wakeups */
1474481Sbholler 	size_t		mon_max;	/* size to avoid false wakeups */
1485045Sbholler 	size_t		size_actual;	/* size actually allocated */
1495045Sbholler 	void		*buf_actual;	/* memory actually allocated */
1504481Sbholler 	uint32_t	support;	/* processor support of monitor/mwait */
1514481Sbholler };
1524481Sbholler 
1534481Sbholler /*
1540Sstevel@tonic-gate  * These constants determine how many of the elements of the
1550Sstevel@tonic-gate  * cpuid we cache in the cpuid_info data structure; the
1560Sstevel@tonic-gate  * remaining elements are accessible via the cpuid instruction.
1570Sstevel@tonic-gate  */
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate #define	NMAX_CPI_STD	6		/* eax = 0 .. 5 */
16010947SSrihari.Venkatesan@Sun.COM #define	NMAX_CPI_EXTD	0x1c		/* eax = 0x80000000 .. 0x8000001b */
16110947SSrihari.Venkatesan@Sun.COM 
16210947SSrihari.Venkatesan@Sun.COM /*
16310947SSrihari.Venkatesan@Sun.COM  * Some terminology needs to be explained:
16410947SSrihari.Venkatesan@Sun.COM  *  - Socket: Something that can be plugged into a motherboard.
16510947SSrihari.Venkatesan@Sun.COM  *  - Package: Same as socket
16610947SSrihari.Venkatesan@Sun.COM  *  - Chip: Same as socket. Note that AMD's documentation uses term "chip"
16710947SSrihari.Venkatesan@Sun.COM  *    differently: there, chip is the same as processor node (below)
16810947SSrihari.Venkatesan@Sun.COM  *  - Processor node: Some AMD processors have more than one
16910947SSrihari.Venkatesan@Sun.COM  *    "subprocessor" embedded in a package. These subprocessors (nodes)
17010947SSrihari.Venkatesan@Sun.COM  *    are fully-functional processors themselves with cores, caches,
17110947SSrihari.Venkatesan@Sun.COM  *    memory controllers, PCI configuration spaces. They are connected
17210947SSrihari.Venkatesan@Sun.COM  *    inside the package with Hypertransport links. On single-node
17310947SSrihari.Venkatesan@Sun.COM  *    processors, processor node is equivalent to chip/socket/package.
17410947SSrihari.Venkatesan@Sun.COM  */
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate struct cpuid_info {
1770Sstevel@tonic-gate 	uint_t cpi_pass;		/* last pass completed */
1780Sstevel@tonic-gate 	/*
1790Sstevel@tonic-gate 	 * standard function information
1800Sstevel@tonic-gate 	 */
1810Sstevel@tonic-gate 	uint_t cpi_maxeax;		/* fn 0: %eax */
1820Sstevel@tonic-gate 	char cpi_vendorstr[13];		/* fn 0: %ebx:%ecx:%edx */
1830Sstevel@tonic-gate 	uint_t cpi_vendor;		/* enum of cpi_vendorstr */
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	uint_t cpi_family;		/* fn 1: extended family */
1860Sstevel@tonic-gate 	uint_t cpi_model;		/* fn 1: extended model */
1870Sstevel@tonic-gate 	uint_t cpi_step;		/* fn 1: stepping */
18810947SSrihari.Venkatesan@Sun.COM 	chipid_t cpi_chipid;		/* fn 1: %ebx:  Intel: chip # */
18910947SSrihari.Venkatesan@Sun.COM 					/*		AMD: package/socket # */
1900Sstevel@tonic-gate 	uint_t cpi_brandid;		/* fn 1: %ebx: brand ID */
1910Sstevel@tonic-gate 	int cpi_clogid;			/* fn 1: %ebx: thread # */
1921228Sandrei 	uint_t cpi_ncpu_per_chip;	/* fn 1: %ebx: logical cpu count */
1930Sstevel@tonic-gate 	uint8_t cpi_cacheinfo[16];	/* fn 2: intel-style cache desc */
1940Sstevel@tonic-gate 	uint_t cpi_ncache;		/* fn 2: number of elements */
1954606Sesaxe 	uint_t cpi_ncpu_shr_last_cache;	/* fn 4: %eax: ncpus sharing cache */
1964606Sesaxe 	id_t cpi_last_lvl_cacheid;	/* fn 4: %eax: derived cache id */
1974606Sesaxe 	uint_t cpi_std_4_size;		/* fn 4: number of fn 4 elements */
1984606Sesaxe 	struct cpuid_regs **cpi_std_4;	/* fn 4: %ecx == 0 .. fn4_size */
1991228Sandrei 	struct cpuid_regs cpi_std[NMAX_CPI_STD];	/* 0 .. 5 */
2000Sstevel@tonic-gate 	/*
2010Sstevel@tonic-gate 	 * extended function information
2020Sstevel@tonic-gate 	 */
2030Sstevel@tonic-gate 	uint_t cpi_xmaxeax;		/* fn 0x80000000: %eax */
2040Sstevel@tonic-gate 	char cpi_brandstr[49];		/* fn 0x8000000[234] */
2050Sstevel@tonic-gate 	uint8_t cpi_pabits;		/* fn 0x80000006: %eax */
20610947SSrihari.Venkatesan@Sun.COM 	uint8_t	cpi_vabits;		/* fn 0x80000006: %eax */
20710947SSrihari.Venkatesan@Sun.COM 	struct	cpuid_regs cpi_extd[NMAX_CPI_EXTD];	/* 0x800000XX */
20810947SSrihari.Venkatesan@Sun.COM 
2095870Sgavinm 	id_t cpi_coreid;		/* same coreid => strands share core */
2105870Sgavinm 	int cpi_pkgcoreid;		/* core number within single package */
2111228Sandrei 	uint_t cpi_ncore_per_chip;	/* AMD: fn 0x80000008: %ecx[7-0] */
2121228Sandrei 					/* Intel: fn 4: %eax[31-26] */
2130Sstevel@tonic-gate 	/*
2140Sstevel@tonic-gate 	 * supported feature information
2150Sstevel@tonic-gate 	 */
2163446Smrj 	uint32_t cpi_support[5];
2170Sstevel@tonic-gate #define	STD_EDX_FEATURES	0
2180Sstevel@tonic-gate #define	AMD_EDX_FEATURES	1
2190Sstevel@tonic-gate #define	TM_EDX_FEATURES		2
2200Sstevel@tonic-gate #define	STD_ECX_FEATURES	3
2213446Smrj #define	AMD_ECX_FEATURES	4
2222869Sgavinm 	/*
2232869Sgavinm 	 * Synthesized information, where known.
2242869Sgavinm 	 */
2252869Sgavinm 	uint32_t cpi_chiprev;		/* See X86_CHIPREV_* in x86_archext.h */
2262869Sgavinm 	const char *cpi_chiprevstr;	/* May be NULL if chiprev unknown */
2272869Sgavinm 	uint32_t cpi_socket;		/* Chip package/socket type */
2284481Sbholler 
2294481Sbholler 	struct mwait_info cpi_mwait;	/* fn 5: monitor/mwait info */
2307282Smishra 	uint32_t cpi_apicid;
23110947SSrihari.Venkatesan@Sun.COM 	uint_t cpi_procnodeid;		/* AMD: nodeID on HT, Intel: chipid */
23210947SSrihari.Venkatesan@Sun.COM 	uint_t cpi_procnodes_per_pkg;	/* AMD: # of nodes in the package */
23310947SSrihari.Venkatesan@Sun.COM 					/* Intel: 1 */
2340Sstevel@tonic-gate };
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate static struct cpuid_info cpuid_info0;
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate /*
2400Sstevel@tonic-gate  * These bit fields are defined by the Intel Application Note AP-485
2410Sstevel@tonic-gate  * "Intel Processor Identification and the CPUID Instruction"
2420Sstevel@tonic-gate  */
2430Sstevel@tonic-gate #define	CPI_FAMILY_XTD(cpi)	BITX((cpi)->cpi_std[1].cp_eax, 27, 20)
2440Sstevel@tonic-gate #define	CPI_MODEL_XTD(cpi)	BITX((cpi)->cpi_std[1].cp_eax, 19, 16)
2450Sstevel@tonic-gate #define	CPI_TYPE(cpi)		BITX((cpi)->cpi_std[1].cp_eax, 13, 12)
2460Sstevel@tonic-gate #define	CPI_FAMILY(cpi)		BITX((cpi)->cpi_std[1].cp_eax, 11, 8)
2470Sstevel@tonic-gate #define	CPI_STEP(cpi)		BITX((cpi)->cpi_std[1].cp_eax, 3, 0)
2480Sstevel@tonic-gate #define	CPI_MODEL(cpi)		BITX((cpi)->cpi_std[1].cp_eax, 7, 4)
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate #define	CPI_FEATURES_EDX(cpi)		((cpi)->cpi_std[1].cp_edx)
2510Sstevel@tonic-gate #define	CPI_FEATURES_ECX(cpi)		((cpi)->cpi_std[1].cp_ecx)
2520Sstevel@tonic-gate #define	CPI_FEATURES_XTD_EDX(cpi)	((cpi)->cpi_extd[1].cp_edx)
2530Sstevel@tonic-gate #define	CPI_FEATURES_XTD_ECX(cpi)	((cpi)->cpi_extd[1].cp_ecx)
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate #define	CPI_BRANDID(cpi)	BITX((cpi)->cpi_std[1].cp_ebx, 7, 0)
2560Sstevel@tonic-gate #define	CPI_CHUNKS(cpi)		BITX((cpi)->cpi_std[1].cp_ebx, 15, 7)
2570Sstevel@tonic-gate #define	CPI_CPU_COUNT(cpi)	BITX((cpi)->cpi_std[1].cp_ebx, 23, 16)
2580Sstevel@tonic-gate #define	CPI_APIC_ID(cpi)	BITX((cpi)->cpi_std[1].cp_ebx, 31, 24)
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate #define	CPI_MAXEAX_MAX		0x100		/* sanity control */
2610Sstevel@tonic-gate #define	CPI_XMAXEAX_MAX		0x80000100
2624606Sesaxe #define	CPI_FN4_ECX_MAX		0x20		/* sanity: max fn 4 levels */
2637282Smishra #define	CPI_FNB_ECX_MAX		0x20		/* sanity: max fn B levels */
2644606Sesaxe 
2654606Sesaxe /*
2664606Sesaxe  * Function 4 (Deterministic Cache Parameters) macros
2674606Sesaxe  * Defined by Intel Application Note AP-485
2684606Sesaxe  */
2694606Sesaxe #define	CPI_NUM_CORES(regs)		BITX((regs)->cp_eax, 31, 26)
2704606Sesaxe #define	CPI_NTHR_SHR_CACHE(regs)	BITX((regs)->cp_eax, 25, 14)
2714606Sesaxe #define	CPI_FULL_ASSOC_CACHE(regs)	BITX((regs)->cp_eax, 9, 9)
2724606Sesaxe #define	CPI_SELF_INIT_CACHE(regs)	BITX((regs)->cp_eax, 8, 8)
2734606Sesaxe #define	CPI_CACHE_LVL(regs)		BITX((regs)->cp_eax, 7, 5)
2744606Sesaxe #define	CPI_CACHE_TYPE(regs)		BITX((regs)->cp_eax, 4, 0)
2757282Smishra #define	CPI_CPU_LEVEL_TYPE(regs)	BITX((regs)->cp_ecx, 15, 8)
2764606Sesaxe 
2774606Sesaxe #define	CPI_CACHE_WAYS(regs)		BITX((regs)->cp_ebx, 31, 22)
2784606Sesaxe #define	CPI_CACHE_PARTS(regs)		BITX((regs)->cp_ebx, 21, 12)
2794606Sesaxe #define	CPI_CACHE_COH_LN_SZ(regs)	BITX((regs)->cp_ebx, 11, 0)
2804606Sesaxe 
2814606Sesaxe #define	CPI_CACHE_SETS(regs)		BITX((regs)->cp_ecx, 31, 0)
2824606Sesaxe 
2834606Sesaxe #define	CPI_PREFCH_STRIDE(regs)		BITX((regs)->cp_edx, 9, 0)
2844606Sesaxe 
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate /*
2871975Sdmick  * A couple of shorthand macros to identify "later" P6-family chips
2881975Sdmick  * like the Pentium M and Core.  First, the "older" P6-based stuff
2891975Sdmick  * (loosely defined as "pre-Pentium-4"):
2901975Sdmick  * P6, PII, Mobile PII, PII Xeon, PIII, Mobile PIII, PIII Xeon
2911975Sdmick  */
2921975Sdmick 
2931975Sdmick #define	IS_LEGACY_P6(cpi) (			\
2941975Sdmick 	cpi->cpi_family == 6 && 		\
2951975Sdmick 		(cpi->cpi_model == 1 ||		\
2961975Sdmick 		cpi->cpi_model == 3 ||		\
2971975Sdmick 		cpi->cpi_model == 5 ||		\
2981975Sdmick 		cpi->cpi_model == 6 ||		\
2991975Sdmick 		cpi->cpi_model == 7 ||		\
3001975Sdmick 		cpi->cpi_model == 8 ||		\
3011975Sdmick 		cpi->cpi_model == 0xA ||	\
3021975Sdmick 		cpi->cpi_model == 0xB)		\
3031975Sdmick )
3041975Sdmick 
3051975Sdmick /* A "new F6" is everything with family 6 that's not the above */
3061975Sdmick #define	IS_NEW_F6(cpi) ((cpi->cpi_family == 6) && !IS_LEGACY_P6(cpi))
3071975Sdmick 
3084855Sksadhukh /* Extended family/model support */
3094855Sksadhukh #define	IS_EXTENDED_MODEL_INTEL(cpi) (cpi->cpi_family == 0x6 || \
3104855Sksadhukh 	cpi->cpi_family >= 0xf)
3114855Sksadhukh 
3121975Sdmick /*
3134481Sbholler  * Info for monitor/mwait idle loop.
3144481Sbholler  *
3154481Sbholler  * See cpuid section of "Intel 64 and IA-32 Architectures Software Developer's
3164481Sbholler  * Manual Volume 2A: Instruction Set Reference, A-M" #25366-022US, November
3174481Sbholler  * 2006.
3184481Sbholler  * See MONITOR/MWAIT section of "AMD64 Architecture Programmer's Manual
3194481Sbholler  * Documentation Updates" #33633, Rev 2.05, December 2006.
3204481Sbholler  */
3214481Sbholler #define	MWAIT_SUPPORT		(0x00000001)	/* mwait supported */
3224481Sbholler #define	MWAIT_EXTENSIONS	(0x00000002)	/* extenstion supported */
3234481Sbholler #define	MWAIT_ECX_INT_ENABLE	(0x00000004)	/* ecx 1 extension supported */
3244481Sbholler #define	MWAIT_SUPPORTED(cpi)	((cpi)->cpi_std[1].cp_ecx & CPUID_INTC_ECX_MON)
3254481Sbholler #define	MWAIT_INT_ENABLE(cpi)	((cpi)->cpi_std[5].cp_ecx & 0x2)
3264481Sbholler #define	MWAIT_EXTENSION(cpi)	((cpi)->cpi_std[5].cp_ecx & 0x1)
3274481Sbholler #define	MWAIT_SIZE_MIN(cpi)	BITX((cpi)->cpi_std[5].cp_eax, 15, 0)
3284481Sbholler #define	MWAIT_SIZE_MAX(cpi)	BITX((cpi)->cpi_std[5].cp_ebx, 15, 0)
3294481Sbholler /*
3304481Sbholler  * Number of sub-cstates for a given c-state.
3314481Sbholler  */
3324481Sbholler #define	MWAIT_NUM_SUBC_STATES(cpi, c_state)			\
3334481Sbholler 	BITX((cpi)->cpi_std[5].cp_edx, c_state + 3, c_state)
3344481Sbholler 
3357532SSean.Ye@Sun.COM /*
3367532SSean.Ye@Sun.COM  * Functions we consune from cpuid_subr.c;  don't publish these in a header
3377532SSean.Ye@Sun.COM  * file to try and keep people using the expected cpuid_* interfaces.
3387532SSean.Ye@Sun.COM  */
3397532SSean.Ye@Sun.COM extern uint32_t _cpuid_skt(uint_t, uint_t, uint_t, uint_t);
3409482SKuriakose.Kuruvilla@Sun.COM extern const char *_cpuid_sktstr(uint_t, uint_t, uint_t, uint_t);
3417532SSean.Ye@Sun.COM extern uint32_t _cpuid_chiprev(uint_t, uint_t, uint_t, uint_t);
3427532SSean.Ye@Sun.COM extern const char *_cpuid_chiprevstr(uint_t, uint_t, uint_t, uint_t);
3437532SSean.Ye@Sun.COM extern uint_t _cpuid_vendorstr_to_vendorcode(char *);
3442869Sgavinm 
3452869Sgavinm /*
3463446Smrj  * Apply up various platform-dependent restrictions where the
3473446Smrj  * underlying platform restrictions mean the CPU can be marked
3483446Smrj  * as less capable than its cpuid instruction would imply.
3493446Smrj  */
3505084Sjohnlev #if defined(__xpv)
3515084Sjohnlev static void
3525084Sjohnlev platform_cpuid_mangle(uint_t vendor, uint32_t eax, struct cpuid_regs *cp)
3535084Sjohnlev {
3545084Sjohnlev 	switch (eax) {
3557532SSean.Ye@Sun.COM 	case 1: {
3567532SSean.Ye@Sun.COM 		uint32_t mcamask = DOMAIN_IS_INITDOMAIN(xen_info) ?
3577532SSean.Ye@Sun.COM 		    0 : CPUID_INTC_EDX_MCA;
3585084Sjohnlev 		cp->cp_edx &=
3597532SSean.Ye@Sun.COM 		    ~(mcamask |
3607532SSean.Ye@Sun.COM 		    CPUID_INTC_EDX_PSE |
3615084Sjohnlev 		    CPUID_INTC_EDX_VME | CPUID_INTC_EDX_DE |
3625084Sjohnlev 		    CPUID_INTC_EDX_SEP | CPUID_INTC_EDX_MTRR |
3635084Sjohnlev 		    CPUID_INTC_EDX_PGE | CPUID_INTC_EDX_PAT |
3645084Sjohnlev 		    CPUID_AMD_EDX_SYSC | CPUID_INTC_EDX_SEP |
3655084Sjohnlev 		    CPUID_INTC_EDX_PSE36 | CPUID_INTC_EDX_HTT);
3665084Sjohnlev 		break;
3677532SSean.Ye@Sun.COM 	}
3685084Sjohnlev 
3695084Sjohnlev 	case 0x80000001:
3705084Sjohnlev 		cp->cp_edx &=
3715084Sjohnlev 		    ~(CPUID_AMD_EDX_PSE |
3725084Sjohnlev 		    CPUID_INTC_EDX_VME | CPUID_INTC_EDX_DE |
3735084Sjohnlev 		    CPUID_AMD_EDX_MTRR | CPUID_AMD_EDX_PGE |
3745084Sjohnlev 		    CPUID_AMD_EDX_PAT | CPUID_AMD_EDX_PSE36 |
3755084Sjohnlev 		    CPUID_AMD_EDX_SYSC | CPUID_INTC_EDX_SEP |
3765084Sjohnlev 		    CPUID_AMD_EDX_TSCP);
3775084Sjohnlev 		cp->cp_ecx &= ~CPUID_AMD_ECX_CMP_LGCY;
3785084Sjohnlev 		break;
3795084Sjohnlev 	default:
3805084Sjohnlev 		break;
3815084Sjohnlev 	}
3825084Sjohnlev 
3835084Sjohnlev 	switch (vendor) {
3845084Sjohnlev 	case X86_VENDOR_Intel:
3855084Sjohnlev 		switch (eax) {
3865084Sjohnlev 		case 4:
3875084Sjohnlev 			/*
3885084Sjohnlev 			 * Zero out the (ncores-per-chip - 1) field
3895084Sjohnlev 			 */
3905084Sjohnlev 			cp->cp_eax &= 0x03fffffff;
3915084Sjohnlev 			break;
3925084Sjohnlev 		default:
3935084Sjohnlev 			break;
3945084Sjohnlev 		}
3955084Sjohnlev 		break;
3965084Sjohnlev 	case X86_VENDOR_AMD:
3975084Sjohnlev 		switch (eax) {
39810080SJoe.Bonasera@sun.com 
39910080SJoe.Bonasera@sun.com 		case 0x80000001:
40010080SJoe.Bonasera@sun.com 			cp->cp_ecx &= ~CPUID_AMD_ECX_CR8D;
40110080SJoe.Bonasera@sun.com 			break;
40210080SJoe.Bonasera@sun.com 
4035084Sjohnlev 		case 0x80000008:
4045084Sjohnlev 			/*
4055084Sjohnlev 			 * Zero out the (ncores-per-chip - 1) field
4065084Sjohnlev 			 */
4075084Sjohnlev 			cp->cp_ecx &= 0xffffff00;
4085084Sjohnlev 			break;
4095084Sjohnlev 		default:
4105084Sjohnlev 			break;
4115084Sjohnlev 		}
4125084Sjohnlev 		break;
4135084Sjohnlev 	default:
4145084Sjohnlev 		break;
4155084Sjohnlev 	}
4165084Sjohnlev }
4175084Sjohnlev #else
4183446Smrj #define	platform_cpuid_mangle(vendor, eax, cp)	/* nothing */
4195084Sjohnlev #endif
4203446Smrj 
4213446Smrj /*
4220Sstevel@tonic-gate  *  Some undocumented ways of patching the results of the cpuid
4230Sstevel@tonic-gate  *  instruction to permit running Solaris 10 on future cpus that
4240Sstevel@tonic-gate  *  we don't currently support.  Could be set to non-zero values
4250Sstevel@tonic-gate  *  via settings in eeprom.
4260Sstevel@tonic-gate  */
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate uint32_t cpuid_feature_ecx_include;
4290Sstevel@tonic-gate uint32_t cpuid_feature_ecx_exclude;
4300Sstevel@tonic-gate uint32_t cpuid_feature_edx_include;
4310Sstevel@tonic-gate uint32_t cpuid_feature_edx_exclude;
4320Sstevel@tonic-gate 
433*12004Sjiang.liu@intel.com /*
434*12004Sjiang.liu@intel.com  * Allocate space for mcpu_cpi in the machcpu structure for all non-boot CPUs.
435*12004Sjiang.liu@intel.com  */
4363446Smrj void
4373446Smrj cpuid_alloc_space(cpu_t *cpu)
4383446Smrj {
4393446Smrj 	/*
4403446Smrj 	 * By convention, cpu0 is the boot cpu, which is set up
4413446Smrj 	 * before memory allocation is available.  All other cpus get
4423446Smrj 	 * their cpuid_info struct allocated here.
4433446Smrj 	 */
4443446Smrj 	ASSERT(cpu->cpu_id != 0);
445*12004Sjiang.liu@intel.com 	ASSERT(cpu->cpu_m.mcpu_cpi == NULL);
4463446Smrj 	cpu->cpu_m.mcpu_cpi =
4473446Smrj 	    kmem_zalloc(sizeof (*cpu->cpu_m.mcpu_cpi), KM_SLEEP);
4483446Smrj }
4493446Smrj 
4503446Smrj void
4513446Smrj cpuid_free_space(cpu_t *cpu)
4523446Smrj {
4534606Sesaxe 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
4544606Sesaxe 	int i;
4554606Sesaxe 
456*12004Sjiang.liu@intel.com 	ASSERT(cpi != NULL);
457*12004Sjiang.liu@intel.com 	ASSERT(cpi != &cpuid_info0);
4584606Sesaxe 
4594606Sesaxe 	/*
4604606Sesaxe 	 * Free up any function 4 related dynamic storage
4614606Sesaxe 	 */
4624606Sesaxe 	for (i = 1; i < cpi->cpi_std_4_size; i++)
4634606Sesaxe 		kmem_free(cpi->cpi_std_4[i], sizeof (struct cpuid_regs));
4644606Sesaxe 	if (cpi->cpi_std_4_size > 0)
4654606Sesaxe 		kmem_free(cpi->cpi_std_4,
4664606Sesaxe 		    cpi->cpi_std_4_size * sizeof (struct cpuid_regs *));
4674606Sesaxe 
468*12004Sjiang.liu@intel.com 	kmem_free(cpi, sizeof (*cpi));
469*12004Sjiang.liu@intel.com 	cpu->cpu_m.mcpu_cpi = NULL;
4703446Smrj }
4713446Smrj 
4725741Smrj #if !defined(__xpv)
4735741Smrj 
4745741Smrj static void
4759000SStuart.Maybee@Sun.COM determine_platform()
4765741Smrj {
4775741Smrj 	struct cpuid_regs cp;
4785741Smrj 	char *xen_str;
4795741Smrj 	uint32_t xen_signature[4];
4805741Smrj 
48110175SStuart.Maybee@Sun.COM 	platform_type = HW_NATIVE;
48210175SStuart.Maybee@Sun.COM 
48310175SStuart.Maybee@Sun.COM 	if (!enable_platform_detection)
48410175SStuart.Maybee@Sun.COM 		return;
48510175SStuart.Maybee@Sun.COM 
4865741Smrj 	/*
4875741Smrj 	 * In a fully virtualized domain, Xen's pseudo-cpuid function
4885741Smrj 	 * 0x40000000 returns a string representing the Xen signature in
4895741Smrj 	 * %ebx, %ecx, and %edx.  %eax contains the maximum supported cpuid
4905741Smrj 	 * function.
4915741Smrj 	 */
4925741Smrj 	cp.cp_eax = 0x40000000;
4935741Smrj 	(void) __cpuid_insn(&cp);
4945741Smrj 	xen_signature[0] = cp.cp_ebx;
4955741Smrj 	xen_signature[1] = cp.cp_ecx;
4965741Smrj 	xen_signature[2] = cp.cp_edx;
4975741Smrj 	xen_signature[3] = 0;
4985741Smrj 	xen_str = (char *)xen_signature;
4999000SStuart.Maybee@Sun.COM 	if (strcmp("XenVMMXenVMM", xen_str) == 0 && cp.cp_eax <= 0x40000002) {
5009000SStuart.Maybee@Sun.COM 		platform_type = HW_XEN_HVM;
5019000SStuart.Maybee@Sun.COM 	} else if (vmware_platform()) { /* running under vmware hypervisor? */
5029000SStuart.Maybee@Sun.COM 		platform_type = HW_VMWARE;
5039000SStuart.Maybee@Sun.COM 	}
5049000SStuart.Maybee@Sun.COM }
5059000SStuart.Maybee@Sun.COM 
5069000SStuart.Maybee@Sun.COM int
5079000SStuart.Maybee@Sun.COM get_hwenv(void)
5089000SStuart.Maybee@Sun.COM {
50910175SStuart.Maybee@Sun.COM 	if (platform_type == -1)
51010175SStuart.Maybee@Sun.COM 		determine_platform();
51110175SStuart.Maybee@Sun.COM 
5129000SStuart.Maybee@Sun.COM 	return (platform_type);
5135741Smrj }
5149000SStuart.Maybee@Sun.COM 
5159000SStuart.Maybee@Sun.COM int
5169000SStuart.Maybee@Sun.COM is_controldom(void)
5179000SStuart.Maybee@Sun.COM {
5189000SStuart.Maybee@Sun.COM 	return (0);
5199000SStuart.Maybee@Sun.COM }
5209000SStuart.Maybee@Sun.COM 
5219000SStuart.Maybee@Sun.COM #else
5229000SStuart.Maybee@Sun.COM 
5239000SStuart.Maybee@Sun.COM int
5249000SStuart.Maybee@Sun.COM get_hwenv(void)
5259000SStuart.Maybee@Sun.COM {
5269000SStuart.Maybee@Sun.COM 	return (HW_XEN_PV);
5279000SStuart.Maybee@Sun.COM }
5289000SStuart.Maybee@Sun.COM 
5299000SStuart.Maybee@Sun.COM int
5309000SStuart.Maybee@Sun.COM is_controldom(void)
5319000SStuart.Maybee@Sun.COM {
5329000SStuart.Maybee@Sun.COM 	return (DOMAIN_IS_INITDOMAIN(xen_info));
5339000SStuart.Maybee@Sun.COM }
5349000SStuart.Maybee@Sun.COM 
5355741Smrj #endif	/* __xpv */
5365741Smrj 
53710947SSrihari.Venkatesan@Sun.COM static void
53810947SSrihari.Venkatesan@Sun.COM cpuid_intel_getids(cpu_t *cpu, uint_t feature)
53910947SSrihari.Venkatesan@Sun.COM {
54010947SSrihari.Venkatesan@Sun.COM 	uint_t i;
54110947SSrihari.Venkatesan@Sun.COM 	uint_t chipid_shift = 0;
54210947SSrihari.Venkatesan@Sun.COM 	uint_t coreid_shift = 0;
54310947SSrihari.Venkatesan@Sun.COM 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
54410947SSrihari.Venkatesan@Sun.COM 
54510947SSrihari.Venkatesan@Sun.COM 	for (i = 1; i < cpi->cpi_ncpu_per_chip; i <<= 1)
54610947SSrihari.Venkatesan@Sun.COM 		chipid_shift++;
54710947SSrihari.Venkatesan@Sun.COM 
54810947SSrihari.Venkatesan@Sun.COM 	cpi->cpi_chipid = cpi->cpi_apicid >> chipid_shift;
54910947SSrihari.Venkatesan@Sun.COM 	cpi->cpi_clogid = cpi->cpi_apicid & ((1 << chipid_shift) - 1);
55010947SSrihari.Venkatesan@Sun.COM 
55110947SSrihari.Venkatesan@Sun.COM 	if (feature & X86_CMP) {
55210947SSrihari.Venkatesan@Sun.COM 		/*
55310947SSrihari.Venkatesan@Sun.COM 		 * Multi-core (and possibly multi-threaded)
55410947SSrihari.Venkatesan@Sun.COM 		 * processors.
55510947SSrihari.Venkatesan@Sun.COM 		 */
55610947SSrihari.Venkatesan@Sun.COM 		uint_t ncpu_per_core;
55710947SSrihari.Venkatesan@Sun.COM 		if (cpi->cpi_ncore_per_chip == 1)
55810947SSrihari.Venkatesan@Sun.COM 			ncpu_per_core = cpi->cpi_ncpu_per_chip;
55910947SSrihari.Venkatesan@Sun.COM 		else if (cpi->cpi_ncore_per_chip > 1)
56010947SSrihari.Venkatesan@Sun.COM 			ncpu_per_core = cpi->cpi_ncpu_per_chip /
56110947SSrihari.Venkatesan@Sun.COM 			    cpi->cpi_ncore_per_chip;
56210947SSrihari.Venkatesan@Sun.COM 		/*
56310947SSrihari.Venkatesan@Sun.COM 		 * 8bit APIC IDs on dual core Pentiums
56410947SSrihari.Venkatesan@Sun.COM 		 * look like this:
56510947SSrihari.Venkatesan@Sun.COM 		 *
56610947SSrihari.Venkatesan@Sun.COM 		 * +-----------------------+------+------+
56710947SSrihari.Venkatesan@Sun.COM 		 * | Physical Package ID   |  MC  |  HT  |
56810947SSrihari.Venkatesan@Sun.COM 		 * +-----------------------+------+------+
56910947SSrihari.Venkatesan@Sun.COM 		 * <------- chipid -------->
57010947SSrihari.Venkatesan@Sun.COM 		 * <------- coreid --------------->
57110947SSrihari.Venkatesan@Sun.COM 		 *			   <--- clogid -->
57210947SSrihari.Venkatesan@Sun.COM 		 *			   <------>
57310947SSrihari.Venkatesan@Sun.COM 		 *			   pkgcoreid
57410947SSrihari.Venkatesan@Sun.COM 		 *
57510947SSrihari.Venkatesan@Sun.COM 		 * Where the number of bits necessary to
57610947SSrihari.Venkatesan@Sun.COM 		 * represent MC and HT fields together equals
57710947SSrihari.Venkatesan@Sun.COM 		 * to the minimum number of bits necessary to
57810947SSrihari.Venkatesan@Sun.COM 		 * store the value of cpi->cpi_ncpu_per_chip.
57910947SSrihari.Venkatesan@Sun.COM 		 * Of those bits, the MC part uses the number
58010947SSrihari.Venkatesan@Sun.COM 		 * of bits necessary to store the value of
58110947SSrihari.Venkatesan@Sun.COM 		 * cpi->cpi_ncore_per_chip.
58210947SSrihari.Venkatesan@Sun.COM 		 */
58310947SSrihari.Venkatesan@Sun.COM 		for (i = 1; i < ncpu_per_core; i <<= 1)
58410947SSrihari.Venkatesan@Sun.COM 			coreid_shift++;
58510947SSrihari.Venkatesan@Sun.COM 		cpi->cpi_coreid = cpi->cpi_apicid >> coreid_shift;
58610947SSrihari.Venkatesan@Sun.COM 		cpi->cpi_pkgcoreid = cpi->cpi_clogid >> coreid_shift;
58710947SSrihari.Venkatesan@Sun.COM 	} else if (feature & X86_HTT) {
58810947SSrihari.Venkatesan@Sun.COM 		/*
58910947SSrihari.Venkatesan@Sun.COM 		 * Single-core multi-threaded processors.
59010947SSrihari.Venkatesan@Sun.COM 		 */
59110947SSrihari.Venkatesan@Sun.COM 		cpi->cpi_coreid = cpi->cpi_chipid;
59210947SSrihari.Venkatesan@Sun.COM 		cpi->cpi_pkgcoreid = 0;
59310947SSrihari.Venkatesan@Sun.COM 	}
59410947SSrihari.Venkatesan@Sun.COM 	cpi->cpi_procnodeid = cpi->cpi_chipid;
59510947SSrihari.Venkatesan@Sun.COM }
59610947SSrihari.Venkatesan@Sun.COM 
59710947SSrihari.Venkatesan@Sun.COM static void
59810947SSrihari.Venkatesan@Sun.COM cpuid_amd_getids(cpu_t *cpu)
59910947SSrihari.Venkatesan@Sun.COM {
60011013SSrihari.Venkatesan@Sun.COM 	int i, first_half, coreidsz;
60110947SSrihari.Venkatesan@Sun.COM 	uint32_t nb_caps_reg;
60210947SSrihari.Venkatesan@Sun.COM 	uint_t node2_1;
60310947SSrihari.Venkatesan@Sun.COM 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
60410947SSrihari.Venkatesan@Sun.COM 
60510947SSrihari.Venkatesan@Sun.COM 	/*
60610947SSrihari.Venkatesan@Sun.COM 	 * AMD CMP chips currently have a single thread per core.
60710947SSrihari.Venkatesan@Sun.COM 	 *
60810947SSrihari.Venkatesan@Sun.COM 	 * Since no two cpus share a core we must assign a distinct coreid
60910947SSrihari.Venkatesan@Sun.COM 	 * per cpu, and we do this by using the cpu_id.  This scheme does not,
61010947SSrihari.Venkatesan@Sun.COM 	 * however, guarantee that sibling cores of a chip will have sequential
61110947SSrihari.Venkatesan@Sun.COM 	 * coreids starting at a multiple of the number of cores per chip -
61210947SSrihari.Venkatesan@Sun.COM 	 * that is usually the case, but if the ACPI MADT table is presented
61310947SSrihari.Venkatesan@Sun.COM 	 * in a different order then we need to perform a few more gymnastics
61410947SSrihari.Venkatesan@Sun.COM 	 * for the pkgcoreid.
61510947SSrihari.Venkatesan@Sun.COM 	 *
61610947SSrihari.Venkatesan@Sun.COM 	 * All processors in the system have the same number of enabled
61710947SSrihari.Venkatesan@Sun.COM 	 * cores. Cores within a processor are always numbered sequentially
61810947SSrihari.Venkatesan@Sun.COM 	 * from 0 regardless of how many or which are disabled, and there
61910947SSrihari.Venkatesan@Sun.COM 	 * is no way for operating system to discover the real core id when some
62010947SSrihari.Venkatesan@Sun.COM 	 * are disabled.
62110947SSrihari.Venkatesan@Sun.COM 	 */
62210947SSrihari.Venkatesan@Sun.COM 
62310947SSrihari.Venkatesan@Sun.COM 	cpi->cpi_coreid = cpu->cpu_id;
62410947SSrihari.Venkatesan@Sun.COM 
62510947SSrihari.Venkatesan@Sun.COM 	if (cpi->cpi_xmaxeax >= 0x80000008) {
62610947SSrihari.Venkatesan@Sun.COM 
62710947SSrihari.Venkatesan@Sun.COM 		coreidsz = BITX((cpi)->cpi_extd[8].cp_ecx, 15, 12);
62810947SSrihari.Venkatesan@Sun.COM 
62910947SSrihari.Venkatesan@Sun.COM 		/*
63010947SSrihari.Venkatesan@Sun.COM 		 * In AMD parlance chip is really a node while Solaris
63110947SSrihari.Venkatesan@Sun.COM 		 * sees chip as equivalent to socket/package.
63210947SSrihari.Venkatesan@Sun.COM 		 */
63310947SSrihari.Venkatesan@Sun.COM 		cpi->cpi_ncore_per_chip =
63410947SSrihari.Venkatesan@Sun.COM 		    BITX((cpi)->cpi_extd[8].cp_ecx, 7, 0) + 1;
63511013SSrihari.Venkatesan@Sun.COM 		if (coreidsz == 0) {
63610947SSrihari.Venkatesan@Sun.COM 			/* Use legacy method */
63711013SSrihari.Venkatesan@Sun.COM 			for (i = 1; i < cpi->cpi_ncore_per_chip; i <<= 1)
63811013SSrihari.Venkatesan@Sun.COM 				coreidsz++;
63911013SSrihari.Venkatesan@Sun.COM 			if (coreidsz == 0)
64011013SSrihari.Venkatesan@Sun.COM 				coreidsz = 1;
64111013SSrihari.Venkatesan@Sun.COM 		}
64210947SSrihari.Venkatesan@Sun.COM 	} else {
64310947SSrihari.Venkatesan@Sun.COM 		/* Assume single-core part */
64411013SSrihari.Venkatesan@Sun.COM 		cpi->cpi_ncore_per_chip = 1;
64510947SSrihari.Venkatesan@Sun.COM 	}
64610947SSrihari.Venkatesan@Sun.COM 
64711013SSrihari.Venkatesan@Sun.COM 	cpi->cpi_clogid = cpi->cpi_pkgcoreid =
64811013SSrihari.Venkatesan@Sun.COM 	    cpi->cpi_apicid & ((1<<coreidsz) - 1);
64910947SSrihari.Venkatesan@Sun.COM 	cpi->cpi_ncpu_per_chip = cpi->cpi_ncore_per_chip;
65010947SSrihari.Venkatesan@Sun.COM 
65110947SSrihari.Venkatesan@Sun.COM 	/* Get nodeID */
65210947SSrihari.Venkatesan@Sun.COM 	if (cpi->cpi_family == 0xf) {
65311013SSrihari.Venkatesan@Sun.COM 		cpi->cpi_procnodeid = (cpi->cpi_apicid >> coreidsz) & 7;
65410947SSrihari.Venkatesan@Sun.COM 		cpi->cpi_chipid = cpi->cpi_procnodeid;
65510947SSrihari.Venkatesan@Sun.COM 	} else if (cpi->cpi_family == 0x10) {
65610947SSrihari.Venkatesan@Sun.COM 		/*
65710947SSrihari.Venkatesan@Sun.COM 		 * See if we are a multi-node processor.
65810947SSrihari.Venkatesan@Sun.COM 		 * All processors in the system have the same number of nodes
65910947SSrihari.Venkatesan@Sun.COM 		 */
66010947SSrihari.Venkatesan@Sun.COM 		nb_caps_reg =  pci_getl_func(0, 24, 3, 0xe8);
66110947SSrihari.Venkatesan@Sun.COM 		if ((cpi->cpi_model < 8) || BITX(nb_caps_reg, 29, 29) == 0) {
66210947SSrihari.Venkatesan@Sun.COM 			/* Single-node */
66311013SSrihari.Venkatesan@Sun.COM 			cpi->cpi_procnodeid = BITX(cpi->cpi_apicid, 5,
66411013SSrihari.Venkatesan@Sun.COM 			    coreidsz);
66510947SSrihari.Venkatesan@Sun.COM 			cpi->cpi_chipid = cpi->cpi_procnodeid;
66610947SSrihari.Venkatesan@Sun.COM 		} else {
66710947SSrihari.Venkatesan@Sun.COM 
66810947SSrihari.Venkatesan@Sun.COM 			/*
66910947SSrihari.Venkatesan@Sun.COM 			 * Multi-node revision D (2 nodes per package
67010947SSrihari.Venkatesan@Sun.COM 			 * are supported)
67110947SSrihari.Venkatesan@Sun.COM 			 */
67210947SSrihari.Venkatesan@Sun.COM 			cpi->cpi_procnodes_per_pkg = 2;
67310947SSrihari.Venkatesan@Sun.COM 
67410947SSrihari.Venkatesan@Sun.COM 			first_half = (cpi->cpi_pkgcoreid <=
67510947SSrihari.Venkatesan@Sun.COM 			    (cpi->cpi_ncore_per_chip/2 - 1));
67610947SSrihari.Venkatesan@Sun.COM 
67710947SSrihari.Venkatesan@Sun.COM 			if (cpi->cpi_apicid == cpi->cpi_pkgcoreid) {
67810947SSrihari.Venkatesan@Sun.COM 				/* We are BSP */
67910947SSrihari.Venkatesan@Sun.COM 				cpi->cpi_procnodeid = (first_half ? 0 : 1);
68010947SSrihari.Venkatesan@Sun.COM 				cpi->cpi_chipid = cpi->cpi_procnodeid >> 1;
68110947SSrihari.Venkatesan@Sun.COM 			} else {
68210947SSrihari.Venkatesan@Sun.COM 
68310947SSrihari.Venkatesan@Sun.COM 				/* We are AP */
68410947SSrihari.Venkatesan@Sun.COM 				/* NodeId[2:1] bits to use for reading F3xe8 */
68510947SSrihari.Venkatesan@Sun.COM 				node2_1 = BITX(cpi->cpi_apicid, 5, 4) << 1;
68610947SSrihari.Venkatesan@Sun.COM 
68710947SSrihari.Venkatesan@Sun.COM 				nb_caps_reg =
68810947SSrihari.Venkatesan@Sun.COM 				    pci_getl_func(0, 24 + node2_1, 3, 0xe8);
68910947SSrihari.Venkatesan@Sun.COM 
69010947SSrihari.Venkatesan@Sun.COM 				/*
69110947SSrihari.Venkatesan@Sun.COM 				 * Check IntNodeNum bit (31:30, but bit 31 is
69210947SSrihari.Venkatesan@Sun.COM 				 * always 0 on dual-node processors)
69310947SSrihari.Venkatesan@Sun.COM 				 */
69410947SSrihari.Venkatesan@Sun.COM 				if (BITX(nb_caps_reg, 30, 30) == 0)
69510947SSrihari.Venkatesan@Sun.COM 					cpi->cpi_procnodeid = node2_1 +
69610947SSrihari.Venkatesan@Sun.COM 					    !first_half;
69710947SSrihari.Venkatesan@Sun.COM 				else
69810947SSrihari.Venkatesan@Sun.COM 					cpi->cpi_procnodeid = node2_1 +
69910947SSrihari.Venkatesan@Sun.COM 					    first_half;
70010947SSrihari.Venkatesan@Sun.COM 
70110947SSrihari.Venkatesan@Sun.COM 				cpi->cpi_chipid = cpi->cpi_procnodeid >> 1;
70210947SSrihari.Venkatesan@Sun.COM 			}
70310947SSrihari.Venkatesan@Sun.COM 		}
70410947SSrihari.Venkatesan@Sun.COM 	} else if (cpi->cpi_family >= 0x11) {
70510947SSrihari.Venkatesan@Sun.COM 		cpi->cpi_procnodeid = (cpi->cpi_apicid >> coreidsz) & 7;
70610947SSrihari.Venkatesan@Sun.COM 		cpi->cpi_chipid = cpi->cpi_procnodeid;
70710947SSrihari.Venkatesan@Sun.COM 	} else {
70810947SSrihari.Venkatesan@Sun.COM 		cpi->cpi_procnodeid = 0;
70910947SSrihari.Venkatesan@Sun.COM 		cpi->cpi_chipid = cpi->cpi_procnodeid;
71010947SSrihari.Venkatesan@Sun.COM 	}
71110947SSrihari.Venkatesan@Sun.COM }
71210947SSrihari.Venkatesan@Sun.COM 
7130Sstevel@tonic-gate uint_t
7140Sstevel@tonic-gate cpuid_pass1(cpu_t *cpu)
7150Sstevel@tonic-gate {
7160Sstevel@tonic-gate 	uint32_t mask_ecx, mask_edx;
7170Sstevel@tonic-gate 	uint_t feature = X86_CPUID;
7180Sstevel@tonic-gate 	struct cpuid_info *cpi;
7191228Sandrei 	struct cpuid_regs *cp;
7200Sstevel@tonic-gate 	int xcpuid;
7215084Sjohnlev #if !defined(__xpv)
7225045Sbholler 	extern int idle_cpu_prefer_mwait;
7235084Sjohnlev #endif
7243446Smrj 
7259482SKuriakose.Kuruvilla@Sun.COM 
7269482SKuriakose.Kuruvilla@Sun.COM #if !defined(__xpv)
7279482SKuriakose.Kuruvilla@Sun.COM 	determine_platform();
7289482SKuriakose.Kuruvilla@Sun.COM #endif
7290Sstevel@tonic-gate 	/*
730*12004Sjiang.liu@intel.com 	 * Space statically allocated for BSP, ensure pointer is set
7310Sstevel@tonic-gate 	 */
732*12004Sjiang.liu@intel.com 	if (cpu->cpu_id == 0 && cpu->cpu_m.mcpu_cpi == NULL)
7333446Smrj 		cpu->cpu_m.mcpu_cpi = &cpuid_info0;
7343446Smrj 	cpi = cpu->cpu_m.mcpu_cpi;
7353446Smrj 	ASSERT(cpi != NULL);
7360Sstevel@tonic-gate 	cp = &cpi->cpi_std[0];
7371228Sandrei 	cp->cp_eax = 0;
7381228Sandrei 	cpi->cpi_maxeax = __cpuid_insn(cp);
7390Sstevel@tonic-gate 	{
7400Sstevel@tonic-gate 		uint32_t *iptr = (uint32_t *)cpi->cpi_vendorstr;
7410Sstevel@tonic-gate 		*iptr++ = cp->cp_ebx;
7420Sstevel@tonic-gate 		*iptr++ = cp->cp_edx;
7430Sstevel@tonic-gate 		*iptr++ = cp->cp_ecx;
7440Sstevel@tonic-gate 		*(char *)&cpi->cpi_vendorstr[12] = '\0';
7450Sstevel@tonic-gate 	}
7460Sstevel@tonic-gate 
7477532SSean.Ye@Sun.COM 	cpi->cpi_vendor = _cpuid_vendorstr_to_vendorcode(cpi->cpi_vendorstr);
7480Sstevel@tonic-gate 	x86_vendor = cpi->cpi_vendor; /* for compatibility */
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 	/*
7510Sstevel@tonic-gate 	 * Limit the range in case of weird hardware
7520Sstevel@tonic-gate 	 */
7530Sstevel@tonic-gate 	if (cpi->cpi_maxeax > CPI_MAXEAX_MAX)
7540Sstevel@tonic-gate 		cpi->cpi_maxeax = CPI_MAXEAX_MAX;
7550Sstevel@tonic-gate 	if (cpi->cpi_maxeax < 1)
7560Sstevel@tonic-gate 		goto pass1_done;
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 	cp = &cpi->cpi_std[1];
7591228Sandrei 	cp->cp_eax = 1;
7601228Sandrei 	(void) __cpuid_insn(cp);
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 	/*
7630Sstevel@tonic-gate 	 * Extract identifying constants for easy access.
7640Sstevel@tonic-gate 	 */
7650Sstevel@tonic-gate 	cpi->cpi_model = CPI_MODEL(cpi);
7660Sstevel@tonic-gate 	cpi->cpi_family = CPI_FAMILY(cpi);
7670Sstevel@tonic-gate 
7681975Sdmick 	if (cpi->cpi_family == 0xf)
7690Sstevel@tonic-gate 		cpi->cpi_family += CPI_FAMILY_XTD(cpi);
7701975Sdmick 
7712001Sdmick 	/*
7724265Skchow 	 * Beware: AMD uses "extended model" iff base *FAMILY* == 0xf.
7732001Sdmick 	 * Intel, and presumably everyone else, uses model == 0xf, as
7742001Sdmick 	 * one would expect (max value means possible overflow).  Sigh.
7752001Sdmick 	 */
7762001Sdmick 
7772001Sdmick 	switch (cpi->cpi_vendor) {
7784855Sksadhukh 	case X86_VENDOR_Intel:
7794855Sksadhukh 		if (IS_EXTENDED_MODEL_INTEL(cpi))
7804855Sksadhukh 			cpi->cpi_model += CPI_MODEL_XTD(cpi) << 4;
7814858Sksadhukh 		break;
7822001Sdmick 	case X86_VENDOR_AMD:
7834265Skchow 		if (CPI_FAMILY(cpi) == 0xf)
7842001Sdmick 			cpi->cpi_model += CPI_MODEL_XTD(cpi) << 4;
7852001Sdmick 		break;
7862001Sdmick 	default:
7872001Sdmick 		if (cpi->cpi_model == 0xf)
7882001Sdmick 			cpi->cpi_model += CPI_MODEL_XTD(cpi) << 4;
7892001Sdmick 		break;
7902001Sdmick 	}
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 	cpi->cpi_step = CPI_STEP(cpi);
7930Sstevel@tonic-gate 	cpi->cpi_brandid = CPI_BRANDID(cpi);
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	/*
7960Sstevel@tonic-gate 	 * *default* assumptions:
7970Sstevel@tonic-gate 	 * - believe %edx feature word
7980Sstevel@tonic-gate 	 * - ignore %ecx feature word
7990Sstevel@tonic-gate 	 * - 32-bit virtual and physical addressing
8000Sstevel@tonic-gate 	 */
8010Sstevel@tonic-gate 	mask_edx = 0xffffffff;
8020Sstevel@tonic-gate 	mask_ecx = 0;
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 	cpi->cpi_pabits = cpi->cpi_vabits = 32;
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
8070Sstevel@tonic-gate 	case X86_VENDOR_Intel:
8080Sstevel@tonic-gate 		if (cpi->cpi_family == 5)
8090Sstevel@tonic-gate 			x86_type = X86_TYPE_P5;
8101975Sdmick 		else if (IS_LEGACY_P6(cpi)) {
8110Sstevel@tonic-gate 			x86_type = X86_TYPE_P6;
8120Sstevel@tonic-gate 			pentiumpro_bug4046376 = 1;
8130Sstevel@tonic-gate 			pentiumpro_bug4064495 = 1;
8140Sstevel@tonic-gate 			/*
8150Sstevel@tonic-gate 			 * Clear the SEP bit when it was set erroneously
8160Sstevel@tonic-gate 			 */
8170Sstevel@tonic-gate 			if (cpi->cpi_model < 3 && cpi->cpi_step < 3)
8180Sstevel@tonic-gate 				cp->cp_edx &= ~CPUID_INTC_EDX_SEP;
8191975Sdmick 		} else if (IS_NEW_F6(cpi) || cpi->cpi_family == 0xf) {
8200Sstevel@tonic-gate 			x86_type = X86_TYPE_P4;
8210Sstevel@tonic-gate 			/*
8220Sstevel@tonic-gate 			 * We don't currently depend on any of the %ecx
8230Sstevel@tonic-gate 			 * features until Prescott, so we'll only check
8240Sstevel@tonic-gate 			 * this from P4 onwards.  We might want to revisit
8250Sstevel@tonic-gate 			 * that idea later.
8260Sstevel@tonic-gate 			 */
8270Sstevel@tonic-gate 			mask_ecx = 0xffffffff;
8280Sstevel@tonic-gate 		} else if (cpi->cpi_family > 0xf)
8290Sstevel@tonic-gate 			mask_ecx = 0xffffffff;
8304636Sbholler 		/*
8314636Sbholler 		 * We don't support MONITOR/MWAIT if leaf 5 is not available
8324636Sbholler 		 * to obtain the monitor linesize.
8334636Sbholler 		 */
8344636Sbholler 		if (cpi->cpi_maxeax < 5)
8354636Sbholler 			mask_ecx &= ~CPUID_INTC_ECX_MON;
8360Sstevel@tonic-gate 		break;
8370Sstevel@tonic-gate 	case X86_VENDOR_IntelClone:
8380Sstevel@tonic-gate 	default:
8390Sstevel@tonic-gate 		break;
8400Sstevel@tonic-gate 	case X86_VENDOR_AMD:
8410Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_108)
8420Sstevel@tonic-gate 		if (cpi->cpi_family == 0xf && cpi->cpi_model == 0xe) {
8430Sstevel@tonic-gate 			cp->cp_eax = (0xf0f & cp->cp_eax) | 0xc0;
8440Sstevel@tonic-gate 			cpi->cpi_model = 0xc;
8450Sstevel@tonic-gate 		} else
8460Sstevel@tonic-gate #endif
8470Sstevel@tonic-gate 		if (cpi->cpi_family == 5) {
8480Sstevel@tonic-gate 			/*
8490Sstevel@tonic-gate 			 * AMD K5 and K6
8500Sstevel@tonic-gate 			 *
8510Sstevel@tonic-gate 			 * These CPUs have an incomplete implementation
8520Sstevel@tonic-gate 			 * of MCA/MCE which we mask away.
8530Sstevel@tonic-gate 			 */
8541228Sandrei 			mask_edx &= ~(CPUID_INTC_EDX_MCE | CPUID_INTC_EDX_MCA);
8551228Sandrei 
8561228Sandrei 			/*
8571228Sandrei 			 * Model 0 uses the wrong (APIC) bit
8581228Sandrei 			 * to indicate PGE.  Fix it here.
8591228Sandrei 			 */
8600Sstevel@tonic-gate 			if (cpi->cpi_model == 0) {
8610Sstevel@tonic-gate 				if (cp->cp_edx & 0x200) {
8620Sstevel@tonic-gate 					cp->cp_edx &= ~0x200;
8630Sstevel@tonic-gate 					cp->cp_edx |= CPUID_INTC_EDX_PGE;
8640Sstevel@tonic-gate 				}
8651228Sandrei 			}
8661228Sandrei 
8671228Sandrei 			/*
8681228Sandrei 			 * Early models had problems w/ MMX; disable.
8691228Sandrei 			 */
8701228Sandrei 			if (cpi->cpi_model < 6)
8711228Sandrei 				mask_edx &= ~CPUID_INTC_EDX_MMX;
8721228Sandrei 		}
8731228Sandrei 
8741228Sandrei 		/*
8751228Sandrei 		 * For newer families, SSE3 and CX16, at least, are valid;
8761228Sandrei 		 * enable all
8771228Sandrei 		 */
8781228Sandrei 		if (cpi->cpi_family >= 0xf)
879771Sdmick 			mask_ecx = 0xffffffff;
8804636Sbholler 		/*
8814636Sbholler 		 * We don't support MONITOR/MWAIT if leaf 5 is not available
8824636Sbholler 		 * to obtain the monitor linesize.
8834636Sbholler 		 */
8844636Sbholler 		if (cpi->cpi_maxeax < 5)
8854636Sbholler 			mask_ecx &= ~CPUID_INTC_ECX_MON;
8865045Sbholler 
8875084Sjohnlev #if !defined(__xpv)
8885045Sbholler 		/*
8895045Sbholler 		 * Do not use MONITOR/MWAIT to halt in the idle loop on any AMD
8905045Sbholler 		 * processors.  AMD does not intend MWAIT to be used in the cpu
8915045Sbholler 		 * idle loop on current and future processors.  10h and future
8925045Sbholler 		 * AMD processors use more power in MWAIT than HLT.
8935045Sbholler 		 * Pre-family-10h Opterons do not have the MWAIT instruction.
8945045Sbholler 		 */
8955045Sbholler 		idle_cpu_prefer_mwait = 0;
8965084Sjohnlev #endif
8975045Sbholler 
8980Sstevel@tonic-gate 		break;
8990Sstevel@tonic-gate 	case X86_VENDOR_TM:
9000Sstevel@tonic-gate 		/*
9010Sstevel@tonic-gate 		 * workaround the NT workaround in CMS 4.1
9020Sstevel@tonic-gate 		 */
9030Sstevel@tonic-gate 		if (cpi->cpi_family == 5 && cpi->cpi_model == 4 &&
9040Sstevel@tonic-gate 		    (cpi->cpi_step == 2 || cpi->cpi_step == 3))
9050Sstevel@tonic-gate 			cp->cp_edx |= CPUID_INTC_EDX_CX8;
9060Sstevel@tonic-gate 		break;
9070Sstevel@tonic-gate 	case X86_VENDOR_Centaur:
9080Sstevel@tonic-gate 		/*
9090Sstevel@tonic-gate 		 * workaround the NT workarounds again
9100Sstevel@tonic-gate 		 */
9110Sstevel@tonic-gate 		if (cpi->cpi_family == 6)
9120Sstevel@tonic-gate 			cp->cp_edx |= CPUID_INTC_EDX_CX8;
9130Sstevel@tonic-gate 		break;
9140Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
9150Sstevel@tonic-gate 		/*
9160Sstevel@tonic-gate 		 * We rely heavily on the probing in locore
9170Sstevel@tonic-gate 		 * to actually figure out what parts, if any,
9180Sstevel@tonic-gate 		 * of the Cyrix cpuid instruction to believe.
9190Sstevel@tonic-gate 		 */
9200Sstevel@tonic-gate 		switch (x86_type) {
9210Sstevel@tonic-gate 		case X86_TYPE_CYRIX_486:
9220Sstevel@tonic-gate 			mask_edx = 0;
9230Sstevel@tonic-gate 			break;
9240Sstevel@tonic-gate 		case X86_TYPE_CYRIX_6x86:
9250Sstevel@tonic-gate 			mask_edx = 0;
9260Sstevel@tonic-gate 			break;
9270Sstevel@tonic-gate 		case X86_TYPE_CYRIX_6x86L:
9280Sstevel@tonic-gate 			mask_edx =
9290Sstevel@tonic-gate 			    CPUID_INTC_EDX_DE |
9300Sstevel@tonic-gate 			    CPUID_INTC_EDX_CX8;
9310Sstevel@tonic-gate 			break;
9320Sstevel@tonic-gate 		case X86_TYPE_CYRIX_6x86MX:
9330Sstevel@tonic-gate 			mask_edx =
9340Sstevel@tonic-gate 			    CPUID_INTC_EDX_DE |
9350Sstevel@tonic-gate 			    CPUID_INTC_EDX_MSR |
9360Sstevel@tonic-gate 			    CPUID_INTC_EDX_CX8 |
9370Sstevel@tonic-gate 			    CPUID_INTC_EDX_PGE |
9380Sstevel@tonic-gate 			    CPUID_INTC_EDX_CMOV |
9390Sstevel@tonic-gate 			    CPUID_INTC_EDX_MMX;
9400Sstevel@tonic-gate 			break;
9410Sstevel@tonic-gate 		case X86_TYPE_CYRIX_GXm:
9420Sstevel@tonic-gate 			mask_edx =
9430Sstevel@tonic-gate 			    CPUID_INTC_EDX_MSR |
9440Sstevel@tonic-gate 			    CPUID_INTC_EDX_CX8 |
9450Sstevel@tonic-gate 			    CPUID_INTC_EDX_CMOV |
9460Sstevel@tonic-gate 			    CPUID_INTC_EDX_MMX;
9470Sstevel@tonic-gate 			break;
9480Sstevel@tonic-gate 		case X86_TYPE_CYRIX_MediaGX:
9490Sstevel@tonic-gate 			break;
9500Sstevel@tonic-gate 		case X86_TYPE_CYRIX_MII:
9510Sstevel@tonic-gate 		case X86_TYPE_VIA_CYRIX_III:
9520Sstevel@tonic-gate 			mask_edx =
9530Sstevel@tonic-gate 			    CPUID_INTC_EDX_DE |
9540Sstevel@tonic-gate 			    CPUID_INTC_EDX_TSC |
9550Sstevel@tonic-gate 			    CPUID_INTC_EDX_MSR |
9560Sstevel@tonic-gate 			    CPUID_INTC_EDX_CX8 |
9570Sstevel@tonic-gate 			    CPUID_INTC_EDX_PGE |
9580Sstevel@tonic-gate 			    CPUID_INTC_EDX_CMOV |
9590Sstevel@tonic-gate 			    CPUID_INTC_EDX_MMX;
9600Sstevel@tonic-gate 			break;
9610Sstevel@tonic-gate 		default:
9620Sstevel@tonic-gate 			break;
9630Sstevel@tonic-gate 		}
9640Sstevel@tonic-gate 		break;
9650Sstevel@tonic-gate 	}
9660Sstevel@tonic-gate 
9675084Sjohnlev #if defined(__xpv)
9685084Sjohnlev 	/*
9695084Sjohnlev 	 * Do not support MONITOR/MWAIT under a hypervisor
9705084Sjohnlev 	 */
9715084Sjohnlev 	mask_ecx &= ~CPUID_INTC_ECX_MON;
9725084Sjohnlev #endif	/* __xpv */
9735084Sjohnlev 
9740Sstevel@tonic-gate 	/*
9750Sstevel@tonic-gate 	 * Now we've figured out the masks that determine
9760Sstevel@tonic-gate 	 * which bits we choose to believe, apply the masks
9770Sstevel@tonic-gate 	 * to the feature words, then map the kernel's view
9780Sstevel@tonic-gate 	 * of these feature words into its feature word.
9790Sstevel@tonic-gate 	 */
9800Sstevel@tonic-gate 	cp->cp_edx &= mask_edx;
9810Sstevel@tonic-gate 	cp->cp_ecx &= mask_ecx;
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 	/*
9843446Smrj 	 * apply any platform restrictions (we don't call this
9853446Smrj 	 * immediately after __cpuid_insn here, because we need the
9863446Smrj 	 * workarounds applied above first)
9870Sstevel@tonic-gate 	 */
9883446Smrj 	platform_cpuid_mangle(cpi->cpi_vendor, 1, cp);
9890Sstevel@tonic-gate 
9903446Smrj 	/*
9913446Smrj 	 * fold in overrides from the "eeprom" mechanism
9923446Smrj 	 */
9930Sstevel@tonic-gate 	cp->cp_edx |= cpuid_feature_edx_include;
9940Sstevel@tonic-gate 	cp->cp_edx &= ~cpuid_feature_edx_exclude;
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 	cp->cp_ecx |= cpuid_feature_ecx_include;
9970Sstevel@tonic-gate 	cp->cp_ecx &= ~cpuid_feature_ecx_exclude;
9980Sstevel@tonic-gate 
9990Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_PSE)
10000Sstevel@tonic-gate 		feature |= X86_LARGEPAGE;
10010Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_TSC)
10020Sstevel@tonic-gate 		feature |= X86_TSC;
10030Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_MSR)
10040Sstevel@tonic-gate 		feature |= X86_MSR;
10050Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_MTRR)
10060Sstevel@tonic-gate 		feature |= X86_MTRR;
10070Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_PGE)
10080Sstevel@tonic-gate 		feature |= X86_PGE;
10090Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_CMOV)
10100Sstevel@tonic-gate 		feature |= X86_CMOV;
10110Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_MMX)
10120Sstevel@tonic-gate 		feature |= X86_MMX;
10130Sstevel@tonic-gate 	if ((cp->cp_edx & CPUID_INTC_EDX_MCE) != 0 &&
10140Sstevel@tonic-gate 	    (cp->cp_edx & CPUID_INTC_EDX_MCA) != 0)
10150Sstevel@tonic-gate 		feature |= X86_MCA;
10160Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_PAE)
10170Sstevel@tonic-gate 		feature |= X86_PAE;
10180Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_CX8)
10190Sstevel@tonic-gate 		feature |= X86_CX8;
10200Sstevel@tonic-gate 	if (cp->cp_ecx & CPUID_INTC_ECX_CX16)
10210Sstevel@tonic-gate 		feature |= X86_CX16;
10220Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_PAT)
10230Sstevel@tonic-gate 		feature |= X86_PAT;
10240Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_SEP)
10250Sstevel@tonic-gate 		feature |= X86_SEP;
10260Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_FXSR) {
10270Sstevel@tonic-gate 		/*
10280Sstevel@tonic-gate 		 * In our implementation, fxsave/fxrstor
10290Sstevel@tonic-gate 		 * are prerequisites before we'll even
10300Sstevel@tonic-gate 		 * try and do SSE things.
10310Sstevel@tonic-gate 		 */
10320Sstevel@tonic-gate 		if (cp->cp_edx & CPUID_INTC_EDX_SSE)
10330Sstevel@tonic-gate 			feature |= X86_SSE;
10340Sstevel@tonic-gate 		if (cp->cp_edx & CPUID_INTC_EDX_SSE2)
10350Sstevel@tonic-gate 			feature |= X86_SSE2;
10360Sstevel@tonic-gate 		if (cp->cp_ecx & CPUID_INTC_ECX_SSE3)
10370Sstevel@tonic-gate 			feature |= X86_SSE3;
10385269Skk208521 		if (cpi->cpi_vendor == X86_VENDOR_Intel) {
10395269Skk208521 			if (cp->cp_ecx & CPUID_INTC_ECX_SSSE3)
10405269Skk208521 				feature |= X86_SSSE3;
10415269Skk208521 			if (cp->cp_ecx & CPUID_INTC_ECX_SSE4_1)
10425269Skk208521 				feature |= X86_SSE4_1;
10435269Skk208521 			if (cp->cp_ecx & CPUID_INTC_ECX_SSE4_2)
10445269Skk208521 				feature |= X86_SSE4_2;
10459370SKuriakose.Kuruvilla@Sun.COM 			if (cp->cp_ecx & CPUID_INTC_ECX_AES)
10469370SKuriakose.Kuruvilla@Sun.COM 				feature |= X86_AES;
10475269Skk208521 		}
10480Sstevel@tonic-gate 	}
10490Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_DE)
10503446Smrj 		feature |= X86_DE;
10517716SBill.Holler@Sun.COM #if !defined(__xpv)
10524481Sbholler 	if (cp->cp_ecx & CPUID_INTC_ECX_MON) {
10537716SBill.Holler@Sun.COM 
10547716SBill.Holler@Sun.COM 		/*
10557716SBill.Holler@Sun.COM 		 * We require the CLFLUSH instruction for erratum workaround
10567716SBill.Holler@Sun.COM 		 * to use MONITOR/MWAIT.
10577716SBill.Holler@Sun.COM 		 */
10587716SBill.Holler@Sun.COM 		if (cp->cp_edx & CPUID_INTC_EDX_CLFSH) {
10597716SBill.Holler@Sun.COM 			cpi->cpi_mwait.support |= MWAIT_SUPPORT;
10607716SBill.Holler@Sun.COM 			feature |= X86_MWAIT;
10617716SBill.Holler@Sun.COM 		} else {
10627716SBill.Holler@Sun.COM 			extern int idle_cpu_assert_cflush_monitor;
10637716SBill.Holler@Sun.COM 
10647716SBill.Holler@Sun.COM 			/*
10657716SBill.Holler@Sun.COM 			 * All processors we are aware of which have
10667716SBill.Holler@Sun.COM 			 * MONITOR/MWAIT also have CLFLUSH.
10677716SBill.Holler@Sun.COM 			 */
10687716SBill.Holler@Sun.COM 			if (idle_cpu_assert_cflush_monitor) {
10697716SBill.Holler@Sun.COM 				ASSERT((cp->cp_ecx & CPUID_INTC_ECX_MON) &&
10707716SBill.Holler@Sun.COM 				    (cp->cp_edx & CPUID_INTC_EDX_CLFSH));
10717716SBill.Holler@Sun.COM 			}
10727716SBill.Holler@Sun.COM 		}
10734481Sbholler 	}
10747716SBill.Holler@Sun.COM #endif	/* __xpv */
10750Sstevel@tonic-gate 
10767589SVikram.Hegde@Sun.COM 	/*
10777589SVikram.Hegde@Sun.COM 	 * Only need it first time, rest of the cpus would follow suite.
10787589SVikram.Hegde@Sun.COM 	 * we only capture this for the bootcpu.
10797589SVikram.Hegde@Sun.COM 	 */
10807589SVikram.Hegde@Sun.COM 	if (cp->cp_edx & CPUID_INTC_EDX_CLFSH) {
10817589SVikram.Hegde@Sun.COM 		feature |= X86_CLFSH;
10827589SVikram.Hegde@Sun.COM 		x86_clflush_size = (BITX(cp->cp_ebx, 15, 8) * 8);
10837589SVikram.Hegde@Sun.COM 	}
10847589SVikram.Hegde@Sun.COM 
10850Sstevel@tonic-gate 	if (feature & X86_PAE)
10860Sstevel@tonic-gate 		cpi->cpi_pabits = 36;
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 	/*
10890Sstevel@tonic-gate 	 * Hyperthreading configuration is slightly tricky on Intel
10900Sstevel@tonic-gate 	 * and pure clones, and even trickier on AMD.
10910Sstevel@tonic-gate 	 *
10920Sstevel@tonic-gate 	 * (AMD chose to set the HTT bit on their CMP processors,
10930Sstevel@tonic-gate 	 * even though they're not actually hyperthreaded.  Thus it
10940Sstevel@tonic-gate 	 * takes a bit more work to figure out what's really going
10953446Smrj 	 * on ... see the handling of the CMP_LGCY bit below)
10960Sstevel@tonic-gate 	 */
10970Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_HTT) {
10980Sstevel@tonic-gate 		cpi->cpi_ncpu_per_chip = CPI_CPU_COUNT(cpi);
10990Sstevel@tonic-gate 		if (cpi->cpi_ncpu_per_chip > 1)
11000Sstevel@tonic-gate 			feature |= X86_HTT;
11011228Sandrei 	} else {
11021228Sandrei 		cpi->cpi_ncpu_per_chip = 1;
11030Sstevel@tonic-gate 	}
11040Sstevel@tonic-gate 
11050Sstevel@tonic-gate 	/*
11060Sstevel@tonic-gate 	 * Work on the "extended" feature information, doing
11070Sstevel@tonic-gate 	 * some basic initialization for cpuid_pass2()
11080Sstevel@tonic-gate 	 */
11090Sstevel@tonic-gate 	xcpuid = 0;
11100Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
11110Sstevel@tonic-gate 	case X86_VENDOR_Intel:
11121975Sdmick 		if (IS_NEW_F6(cpi) || cpi->cpi_family >= 0xf)
11130Sstevel@tonic-gate 			xcpuid++;
11140Sstevel@tonic-gate 		break;
11150Sstevel@tonic-gate 	case X86_VENDOR_AMD:
11160Sstevel@tonic-gate 		if (cpi->cpi_family > 5 ||
11170Sstevel@tonic-gate 		    (cpi->cpi_family == 5 && cpi->cpi_model >= 1))
11180Sstevel@tonic-gate 			xcpuid++;
11190Sstevel@tonic-gate 		break;
11200Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
11210Sstevel@tonic-gate 		/*
11220Sstevel@tonic-gate 		 * Only these Cyrix CPUs are -known- to support
11230Sstevel@tonic-gate 		 * extended cpuid operations.
11240Sstevel@tonic-gate 		 */
11250Sstevel@tonic-gate 		if (x86_type == X86_TYPE_VIA_CYRIX_III ||
11260Sstevel@tonic-gate 		    x86_type == X86_TYPE_CYRIX_GXm)
11270Sstevel@tonic-gate 			xcpuid++;
11280Sstevel@tonic-gate 		break;
11290Sstevel@tonic-gate 	case X86_VENDOR_Centaur:
11300Sstevel@tonic-gate 	case X86_VENDOR_TM:
11310Sstevel@tonic-gate 	default:
11320Sstevel@tonic-gate 		xcpuid++;
11330Sstevel@tonic-gate 		break;
11340Sstevel@tonic-gate 	}
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate 	if (xcpuid) {
11370Sstevel@tonic-gate 		cp = &cpi->cpi_extd[0];
11381228Sandrei 		cp->cp_eax = 0x80000000;
11391228Sandrei 		cpi->cpi_xmaxeax = __cpuid_insn(cp);
11400Sstevel@tonic-gate 	}
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate 	if (cpi->cpi_xmaxeax & 0x80000000) {
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 		if (cpi->cpi_xmaxeax > CPI_XMAXEAX_MAX)
11450Sstevel@tonic-gate 			cpi->cpi_xmaxeax = CPI_XMAXEAX_MAX;
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate 		switch (cpi->cpi_vendor) {
11480Sstevel@tonic-gate 		case X86_VENDOR_Intel:
11490Sstevel@tonic-gate 		case X86_VENDOR_AMD:
11500Sstevel@tonic-gate 			if (cpi->cpi_xmaxeax < 0x80000001)
11510Sstevel@tonic-gate 				break;
11520Sstevel@tonic-gate 			cp = &cpi->cpi_extd[1];
11531228Sandrei 			cp->cp_eax = 0x80000001;
11541228Sandrei 			(void) __cpuid_insn(cp);
11553446Smrj 
11560Sstevel@tonic-gate 			if (cpi->cpi_vendor == X86_VENDOR_AMD &&
11570Sstevel@tonic-gate 			    cpi->cpi_family == 5 &&
11580Sstevel@tonic-gate 			    cpi->cpi_model == 6 &&
11590Sstevel@tonic-gate 			    cpi->cpi_step == 6) {
11600Sstevel@tonic-gate 				/*
11610Sstevel@tonic-gate 				 * K6 model 6 uses bit 10 to indicate SYSC
11620Sstevel@tonic-gate 				 * Later models use bit 11. Fix it here.
11630Sstevel@tonic-gate 				 */
11640Sstevel@tonic-gate 				if (cp->cp_edx & 0x400) {
11650Sstevel@tonic-gate 					cp->cp_edx &= ~0x400;
11660Sstevel@tonic-gate 					cp->cp_edx |= CPUID_AMD_EDX_SYSC;
11670Sstevel@tonic-gate 				}
11680Sstevel@tonic-gate 			}
11690Sstevel@tonic-gate 
11703446Smrj 			platform_cpuid_mangle(cpi->cpi_vendor, 0x80000001, cp);
11713446Smrj 
11720Sstevel@tonic-gate 			/*
11730Sstevel@tonic-gate 			 * Compute the additions to the kernel's feature word.
11740Sstevel@tonic-gate 			 */
11750Sstevel@tonic-gate 			if (cp->cp_edx & CPUID_AMD_EDX_NX)
11760Sstevel@tonic-gate 				feature |= X86_NX;
11770Sstevel@tonic-gate 
11787656SSherry.Moore@Sun.COM 			/*
11797656SSherry.Moore@Sun.COM 			 * Regardless whether or not we boot 64-bit,
11807656SSherry.Moore@Sun.COM 			 * we should have a way to identify whether
11817656SSherry.Moore@Sun.COM 			 * the CPU is capable of running 64-bit.
11827656SSherry.Moore@Sun.COM 			 */
11837656SSherry.Moore@Sun.COM 			if (cp->cp_edx & CPUID_AMD_EDX_LM)
11847656SSherry.Moore@Sun.COM 				feature |= X86_64;
11857656SSherry.Moore@Sun.COM 
11865349Skchow #if defined(__amd64)
11875349Skchow 			/* 1 GB large page - enable only for 64 bit kernel */
11885349Skchow 			if (cp->cp_edx & CPUID_AMD_EDX_1GPG)
11895349Skchow 				feature |= X86_1GPG;
11905349Skchow #endif
11915349Skchow 
11924628Skk208521 			if ((cpi->cpi_vendor == X86_VENDOR_AMD) &&
11934628Skk208521 			    (cpi->cpi_std[1].cp_edx & CPUID_INTC_EDX_FXSR) &&
11944628Skk208521 			    (cp->cp_ecx & CPUID_AMD_ECX_SSE4A))
11954628Skk208521 				feature |= X86_SSE4A;
11964628Skk208521 
11970Sstevel@tonic-gate 			/*
11983446Smrj 			 * If both the HTT and CMP_LGCY bits are set,
11991228Sandrei 			 * then we're not actually HyperThreaded.  Read
12001228Sandrei 			 * "AMD CPUID Specification" for more details.
12010Sstevel@tonic-gate 			 */
12020Sstevel@tonic-gate 			if (cpi->cpi_vendor == X86_VENDOR_AMD &&
12031228Sandrei 			    (feature & X86_HTT) &&
12043446Smrj 			    (cp->cp_ecx & CPUID_AMD_ECX_CMP_LGCY)) {
12050Sstevel@tonic-gate 				feature &= ~X86_HTT;
12061228Sandrei 				feature |= X86_CMP;
12071228Sandrei 			}
12083446Smrj #if defined(__amd64)
12090Sstevel@tonic-gate 			/*
12100Sstevel@tonic-gate 			 * It's really tricky to support syscall/sysret in
12110Sstevel@tonic-gate 			 * the i386 kernel; we rely on sysenter/sysexit
12120Sstevel@tonic-gate 			 * instead.  In the amd64 kernel, things are -way-
12130Sstevel@tonic-gate 			 * better.
12140Sstevel@tonic-gate 			 */
12150Sstevel@tonic-gate 			if (cp->cp_edx & CPUID_AMD_EDX_SYSC)
12160Sstevel@tonic-gate 				feature |= X86_ASYSC;
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate 			/*
12190Sstevel@tonic-gate 			 * While we're thinking about system calls, note
12200Sstevel@tonic-gate 			 * that AMD processors don't support sysenter
12210Sstevel@tonic-gate 			 * in long mode at all, so don't try to program them.
12220Sstevel@tonic-gate 			 */
12230Sstevel@tonic-gate 			if (x86_vendor == X86_VENDOR_AMD)
12240Sstevel@tonic-gate 				feature &= ~X86_SEP;
12250Sstevel@tonic-gate #endif
12266657Ssudheer 			if (cp->cp_edx & CPUID_AMD_EDX_TSCP)
12273446Smrj 				feature |= X86_TSCP;
12280Sstevel@tonic-gate 			break;
12290Sstevel@tonic-gate 		default:
12300Sstevel@tonic-gate 			break;
12310Sstevel@tonic-gate 		}
12320Sstevel@tonic-gate 
12331228Sandrei 		/*
12341228Sandrei 		 * Get CPUID data about processor cores and hyperthreads.
12351228Sandrei 		 */
12360Sstevel@tonic-gate 		switch (cpi->cpi_vendor) {
12370Sstevel@tonic-gate 		case X86_VENDOR_Intel:
12381228Sandrei 			if (cpi->cpi_maxeax >= 4) {
12391228Sandrei 				cp = &cpi->cpi_std[4];
12401228Sandrei 				cp->cp_eax = 4;
12411228Sandrei 				cp->cp_ecx = 0;
12421228Sandrei 				(void) __cpuid_insn(cp);
12433446Smrj 				platform_cpuid_mangle(cpi->cpi_vendor, 4, cp);
12441228Sandrei 			}
12451228Sandrei 			/*FALLTHROUGH*/
12460Sstevel@tonic-gate 		case X86_VENDOR_AMD:
12470Sstevel@tonic-gate 			if (cpi->cpi_xmaxeax < 0x80000008)
12480Sstevel@tonic-gate 				break;
12490Sstevel@tonic-gate 			cp = &cpi->cpi_extd[8];
12501228Sandrei 			cp->cp_eax = 0x80000008;
12511228Sandrei 			(void) __cpuid_insn(cp);
12523446Smrj 			platform_cpuid_mangle(cpi->cpi_vendor, 0x80000008, cp);
12533446Smrj 
12540Sstevel@tonic-gate 			/*
12550Sstevel@tonic-gate 			 * Virtual and physical address limits from
12560Sstevel@tonic-gate 			 * cpuid override previously guessed values.
12570Sstevel@tonic-gate 			 */
12580Sstevel@tonic-gate 			cpi->cpi_pabits = BITX(cp->cp_eax, 7, 0);
12590Sstevel@tonic-gate 			cpi->cpi_vabits = BITX(cp->cp_eax, 15, 8);
12600Sstevel@tonic-gate 			break;
12610Sstevel@tonic-gate 		default:
12620Sstevel@tonic-gate 			break;
12630Sstevel@tonic-gate 		}
12641228Sandrei 
12654606Sesaxe 		/*
12664606Sesaxe 		 * Derive the number of cores per chip
12674606Sesaxe 		 */
12681228Sandrei 		switch (cpi->cpi_vendor) {
12691228Sandrei 		case X86_VENDOR_Intel:
12701228Sandrei 			if (cpi->cpi_maxeax < 4) {
12711228Sandrei 				cpi->cpi_ncore_per_chip = 1;
12721228Sandrei 				break;
12731228Sandrei 			} else {
12741228Sandrei 				cpi->cpi_ncore_per_chip =
12751228Sandrei 				    BITX((cpi)->cpi_std[4].cp_eax, 31, 26) + 1;
12761228Sandrei 			}
12771228Sandrei 			break;
12781228Sandrei 		case X86_VENDOR_AMD:
12791228Sandrei 			if (cpi->cpi_xmaxeax < 0x80000008) {
12801228Sandrei 				cpi->cpi_ncore_per_chip = 1;
12811228Sandrei 				break;
12821228Sandrei 			} else {
12835870Sgavinm 				/*
12845870Sgavinm 				 * On family 0xf cpuid fn 2 ECX[7:0] "NC" is
12855870Sgavinm 				 * 1 less than the number of physical cores on
12865870Sgavinm 				 * the chip.  In family 0x10 this value can
12875870Sgavinm 				 * be affected by "downcoring" - it reflects
12885870Sgavinm 				 * 1 less than the number of cores actually
12895870Sgavinm 				 * enabled on this node.
12905870Sgavinm 				 */
12911228Sandrei 				cpi->cpi_ncore_per_chip =
12921228Sandrei 				    BITX((cpi)->cpi_extd[8].cp_ecx, 7, 0) + 1;
12931228Sandrei 			}
12941228Sandrei 			break;
12951228Sandrei 		default:
12961228Sandrei 			cpi->cpi_ncore_per_chip = 1;
12971228Sandrei 			break;
12981228Sandrei 		}
12998906SEric.Saxe@Sun.COM 
13008906SEric.Saxe@Sun.COM 		/*
13018906SEric.Saxe@Sun.COM 		 * Get CPUID data about TSC Invariance in Deep C-State.
13028906SEric.Saxe@Sun.COM 		 */
13038906SEric.Saxe@Sun.COM 		switch (cpi->cpi_vendor) {
13048906SEric.Saxe@Sun.COM 		case X86_VENDOR_Intel:
13058906SEric.Saxe@Sun.COM 			if (cpi->cpi_maxeax >= 7) {
13068906SEric.Saxe@Sun.COM 				cp = &cpi->cpi_extd[7];
13078906SEric.Saxe@Sun.COM 				cp->cp_eax = 0x80000007;
13088906SEric.Saxe@Sun.COM 				cp->cp_ecx = 0;
13098906SEric.Saxe@Sun.COM 				(void) __cpuid_insn(cp);
13108906SEric.Saxe@Sun.COM 			}
13118906SEric.Saxe@Sun.COM 			break;
13128906SEric.Saxe@Sun.COM 		default:
13138906SEric.Saxe@Sun.COM 			break;
13148906SEric.Saxe@Sun.COM 		}
13155284Sgavinm 	} else {
13165284Sgavinm 		cpi->cpi_ncore_per_chip = 1;
13170Sstevel@tonic-gate 	}
13180Sstevel@tonic-gate 
13191228Sandrei 	/*
13201228Sandrei 	 * If more than one core, then this processor is CMP.
13211228Sandrei 	 */
13221228Sandrei 	if (cpi->cpi_ncore_per_chip > 1)
13231228Sandrei 		feature |= X86_CMP;
13243446Smrj 
13251228Sandrei 	/*
13261228Sandrei 	 * If the number of cores is the same as the number
13271228Sandrei 	 * of CPUs, then we cannot have HyperThreading.
13281228Sandrei 	 */
13291228Sandrei 	if (cpi->cpi_ncpu_per_chip == cpi->cpi_ncore_per_chip)
13301228Sandrei 		feature &= ~X86_HTT;
13311228Sandrei 
133210947SSrihari.Venkatesan@Sun.COM 	cpi->cpi_apicid = CPI_APIC_ID(cpi);
133310947SSrihari.Venkatesan@Sun.COM 	cpi->cpi_procnodes_per_pkg = 1;
133410947SSrihari.Venkatesan@Sun.COM 
13350Sstevel@tonic-gate 	if ((feature & (X86_HTT | X86_CMP)) == 0) {
13361228Sandrei 		/*
13371228Sandrei 		 * Single-core single-threaded processors.
13381228Sandrei 		 */
13390Sstevel@tonic-gate 		cpi->cpi_chipid = -1;
13400Sstevel@tonic-gate 		cpi->cpi_clogid = 0;
13411228Sandrei 		cpi->cpi_coreid = cpu->cpu_id;
13425870Sgavinm 		cpi->cpi_pkgcoreid = 0;
134310947SSrihari.Venkatesan@Sun.COM 		if (cpi->cpi_vendor == X86_VENDOR_AMD)
134410947SSrihari.Venkatesan@Sun.COM 			cpi->cpi_procnodeid = BITX(cpi->cpi_apicid, 3, 0);
134510947SSrihari.Venkatesan@Sun.COM 		else
134610947SSrihari.Venkatesan@Sun.COM 			cpi->cpi_procnodeid = cpi->cpi_chipid;
13470Sstevel@tonic-gate 	} else if (cpi->cpi_ncpu_per_chip > 1) {
134810947SSrihari.Venkatesan@Sun.COM 		if (cpi->cpi_vendor == X86_VENDOR_Intel)
134910947SSrihari.Venkatesan@Sun.COM 			cpuid_intel_getids(cpu, feature);
135010947SSrihari.Venkatesan@Sun.COM 		else if (cpi->cpi_vendor == X86_VENDOR_AMD)
135110947SSrihari.Venkatesan@Sun.COM 			cpuid_amd_getids(cpu);
135210947SSrihari.Venkatesan@Sun.COM 		else {
13531228Sandrei 			/*
13541228Sandrei 			 * All other processors are currently
13551228Sandrei 			 * assumed to have single cores.
13561228Sandrei 			 */
13571228Sandrei 			cpi->cpi_coreid = cpi->cpi_chipid;
13585870Sgavinm 			cpi->cpi_pkgcoreid = 0;
135910947SSrihari.Venkatesan@Sun.COM 			cpi->cpi_procnodeid = cpi->cpi_chipid;
13601228Sandrei 		}
13610Sstevel@tonic-gate 	}
13620Sstevel@tonic-gate 
13632869Sgavinm 	/*
13642869Sgavinm 	 * Synthesize chip "revision" and socket type
13652869Sgavinm 	 */
13667532SSean.Ye@Sun.COM 	cpi->cpi_chiprev = _cpuid_chiprev(cpi->cpi_vendor, cpi->cpi_family,
13677532SSean.Ye@Sun.COM 	    cpi->cpi_model, cpi->cpi_step);
13687532SSean.Ye@Sun.COM 	cpi->cpi_chiprevstr = _cpuid_chiprevstr(cpi->cpi_vendor,
13697532SSean.Ye@Sun.COM 	    cpi->cpi_family, cpi->cpi_model, cpi->cpi_step);
13707532SSean.Ye@Sun.COM 	cpi->cpi_socket = _cpuid_skt(cpi->cpi_vendor, cpi->cpi_family,
13717532SSean.Ye@Sun.COM 	    cpi->cpi_model, cpi->cpi_step);
13722869Sgavinm 
13730Sstevel@tonic-gate pass1_done:
13740Sstevel@tonic-gate 	cpi->cpi_pass = 1;
13750Sstevel@tonic-gate 	return (feature);
13760Sstevel@tonic-gate }
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate /*
13790Sstevel@tonic-gate  * Make copies of the cpuid table entries we depend on, in
13800Sstevel@tonic-gate  * part for ease of parsing now, in part so that we have only
13810Sstevel@tonic-gate  * one place to correct any of it, in part for ease of
13820Sstevel@tonic-gate  * later export to userland, and in part so we can look at
13830Sstevel@tonic-gate  * this stuff in a crash dump.
13840Sstevel@tonic-gate  */
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate /*ARGSUSED*/
13870Sstevel@tonic-gate void
13880Sstevel@tonic-gate cpuid_pass2(cpu_t *cpu)
13890Sstevel@tonic-gate {
13900Sstevel@tonic-gate 	uint_t n, nmax;
13910Sstevel@tonic-gate 	int i;
13921228Sandrei 	struct cpuid_regs *cp;
13930Sstevel@tonic-gate 	uint8_t *dp;
13940Sstevel@tonic-gate 	uint32_t *iptr;
13950Sstevel@tonic-gate 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
13960Sstevel@tonic-gate 
13970Sstevel@tonic-gate 	ASSERT(cpi->cpi_pass == 1);
13980Sstevel@tonic-gate 
13990Sstevel@tonic-gate 	if (cpi->cpi_maxeax < 1)
14000Sstevel@tonic-gate 		goto pass2_done;
14010Sstevel@tonic-gate 
14020Sstevel@tonic-gate 	if ((nmax = cpi->cpi_maxeax + 1) > NMAX_CPI_STD)
14030Sstevel@tonic-gate 		nmax = NMAX_CPI_STD;
14040Sstevel@tonic-gate 	/*
14050Sstevel@tonic-gate 	 * (We already handled n == 0 and n == 1 in pass 1)
14060Sstevel@tonic-gate 	 */
14070Sstevel@tonic-gate 	for (n = 2, cp = &cpi->cpi_std[2]; n < nmax; n++, cp++) {
14081228Sandrei 		cp->cp_eax = n;
14094606Sesaxe 
14104606Sesaxe 		/*
14114606Sesaxe 		 * CPUID function 4 expects %ecx to be initialized
14124606Sesaxe 		 * with an index which indicates which cache to return
14134606Sesaxe 		 * information about. The OS is expected to call function 4
14144606Sesaxe 		 * with %ecx set to 0, 1, 2, ... until it returns with
14154606Sesaxe 		 * EAX[4:0] set to 0, which indicates there are no more
14164606Sesaxe 		 * caches.
14174606Sesaxe 		 *
14184606Sesaxe 		 * Here, populate cpi_std[4] with the information returned by
14194606Sesaxe 		 * function 4 when %ecx == 0, and do the rest in cpuid_pass3()
14204606Sesaxe 		 * when dynamic memory allocation becomes available.
14214606Sesaxe 		 *
14224606Sesaxe 		 * Note: we need to explicitly initialize %ecx here, since
14234606Sesaxe 		 * function 4 may have been previously invoked.
14244606Sesaxe 		 */
14254606Sesaxe 		if (n == 4)
14264606Sesaxe 			cp->cp_ecx = 0;
14274606Sesaxe 
14281228Sandrei 		(void) __cpuid_insn(cp);
14293446Smrj 		platform_cpuid_mangle(cpi->cpi_vendor, n, cp);
14300Sstevel@tonic-gate 		switch (n) {
14310Sstevel@tonic-gate 		case 2:
14320Sstevel@tonic-gate 			/*
14330Sstevel@tonic-gate 			 * "the lower 8 bits of the %eax register
14340Sstevel@tonic-gate 			 * contain a value that identifies the number
14350Sstevel@tonic-gate 			 * of times the cpuid [instruction] has to be
14360Sstevel@tonic-gate 			 * executed to obtain a complete image of the
14370Sstevel@tonic-gate 			 * processor's caching systems."
14380Sstevel@tonic-gate 			 *
14390Sstevel@tonic-gate 			 * How *do* they make this stuff up?
14400Sstevel@tonic-gate 			 */
14410Sstevel@tonic-gate 			cpi->cpi_ncache = sizeof (*cp) *
14420Sstevel@tonic-gate 			    BITX(cp->cp_eax, 7, 0);
14430Sstevel@tonic-gate 			if (cpi->cpi_ncache == 0)
14440Sstevel@tonic-gate 				break;
14450Sstevel@tonic-gate 			cpi->cpi_ncache--;	/* skip count byte */
14460Sstevel@tonic-gate 
14470Sstevel@tonic-gate 			/*
14480Sstevel@tonic-gate 			 * Well, for now, rather than attempt to implement
14490Sstevel@tonic-gate 			 * this slightly dubious algorithm, we just look
14500Sstevel@tonic-gate 			 * at the first 15 ..
14510Sstevel@tonic-gate 			 */
14520Sstevel@tonic-gate 			if (cpi->cpi_ncache > (sizeof (*cp) - 1))
14530Sstevel@tonic-gate 				cpi->cpi_ncache = sizeof (*cp) - 1;
14540Sstevel@tonic-gate 
14550Sstevel@tonic-gate 			dp = cpi->cpi_cacheinfo;
14560Sstevel@tonic-gate 			if (BITX(cp->cp_eax, 31, 31) == 0) {
14570Sstevel@tonic-gate 				uint8_t *p = (void *)&cp->cp_eax;
14586317Skk208521 				for (i = 1; i < 4; i++)
14590Sstevel@tonic-gate 					if (p[i] != 0)
14600Sstevel@tonic-gate 						*dp++ = p[i];
14610Sstevel@tonic-gate 			}
14620Sstevel@tonic-gate 			if (BITX(cp->cp_ebx, 31, 31) == 0) {
14630Sstevel@tonic-gate 				uint8_t *p = (void *)&cp->cp_ebx;
14640Sstevel@tonic-gate 				for (i = 0; i < 4; i++)
14650Sstevel@tonic-gate 					if (p[i] != 0)
14660Sstevel@tonic-gate 						*dp++ = p[i];
14670Sstevel@tonic-gate 			}
14680Sstevel@tonic-gate 			if (BITX(cp->cp_ecx, 31, 31) == 0) {
14690Sstevel@tonic-gate 				uint8_t *p = (void *)&cp->cp_ecx;
14700Sstevel@tonic-gate 				for (i = 0; i < 4; i++)
14710Sstevel@tonic-gate 					if (p[i] != 0)
14720Sstevel@tonic-gate 						*dp++ = p[i];
14730Sstevel@tonic-gate 			}
14740Sstevel@tonic-gate 			if (BITX(cp->cp_edx, 31, 31) == 0) {
14750Sstevel@tonic-gate 				uint8_t *p = (void *)&cp->cp_edx;
14760Sstevel@tonic-gate 				for (i = 0; i < 4; i++)
14770Sstevel@tonic-gate 					if (p[i] != 0)
14780Sstevel@tonic-gate 						*dp++ = p[i];
14790Sstevel@tonic-gate 			}
14800Sstevel@tonic-gate 			break;
14814481Sbholler 
14820Sstevel@tonic-gate 		case 3:	/* Processor serial number, if PSN supported */
14834481Sbholler 			break;
14844481Sbholler 
14850Sstevel@tonic-gate 		case 4:	/* Deterministic cache parameters */
14864481Sbholler 			break;
14874481Sbholler 
14880Sstevel@tonic-gate 		case 5:	/* Monitor/Mwait parameters */
14895045Sbholler 		{
14905045Sbholler 			size_t mwait_size;
14914481Sbholler 
14924481Sbholler 			/*
14934481Sbholler 			 * check cpi_mwait.support which was set in cpuid_pass1
14944481Sbholler 			 */
14954481Sbholler 			if (!(cpi->cpi_mwait.support & MWAIT_SUPPORT))
14964481Sbholler 				break;
14974481Sbholler 
14985045Sbholler 			/*
14995045Sbholler 			 * Protect ourself from insane mwait line size.
15005045Sbholler 			 * Workaround for incomplete hardware emulator(s).
15015045Sbholler 			 */
15025045Sbholler 			mwait_size = (size_t)MWAIT_SIZE_MAX(cpi);
15035045Sbholler 			if (mwait_size < sizeof (uint32_t) ||
15045045Sbholler 			    !ISP2(mwait_size)) {
15055045Sbholler #if DEBUG
15065045Sbholler 				cmn_err(CE_NOTE, "Cannot handle cpu %d mwait "
15077798SSaurabh.Mishra@Sun.COM 				    "size %ld", cpu->cpu_id, (long)mwait_size);
15085045Sbholler #endif
15095045Sbholler 				break;
15105045Sbholler 			}
15115045Sbholler 
15124481Sbholler 			cpi->cpi_mwait.mon_min = (size_t)MWAIT_SIZE_MIN(cpi);
15135045Sbholler 			cpi->cpi_mwait.mon_max = mwait_size;
15144481Sbholler 			if (MWAIT_EXTENSION(cpi)) {
15154481Sbholler 				cpi->cpi_mwait.support |= MWAIT_EXTENSIONS;
15164481Sbholler 				if (MWAIT_INT_ENABLE(cpi))
15174481Sbholler 					cpi->cpi_mwait.support |=
15184481Sbholler 					    MWAIT_ECX_INT_ENABLE;
15194481Sbholler 			}
15204481Sbholler 			break;
15215045Sbholler 		}
15220Sstevel@tonic-gate 		default:
15230Sstevel@tonic-gate 			break;
15240Sstevel@tonic-gate 		}
15250Sstevel@tonic-gate 	}
15260Sstevel@tonic-gate 
15277282Smishra 	if (cpi->cpi_maxeax >= 0xB && cpi->cpi_vendor == X86_VENDOR_Intel) {
15287798SSaurabh.Mishra@Sun.COM 		struct cpuid_regs regs;
15297798SSaurabh.Mishra@Sun.COM 
15307798SSaurabh.Mishra@Sun.COM 		cp = &regs;
15317282Smishra 		cp->cp_eax = 0xB;
15327798SSaurabh.Mishra@Sun.COM 		cp->cp_edx = cp->cp_ebx = cp->cp_ecx = 0;
15337282Smishra 
15347282Smishra 		(void) __cpuid_insn(cp);
15357282Smishra 
15367282Smishra 		/*
15377282Smishra 		 * Check CPUID.EAX=0BH, ECX=0H:EBX is non-zero, which
15387282Smishra 		 * indicates that the extended topology enumeration leaf is
15397282Smishra 		 * available.
15407282Smishra 		 */
15417282Smishra 		if (cp->cp_ebx) {
15427282Smishra 			uint32_t x2apic_id;
15437282Smishra 			uint_t coreid_shift = 0;
15447282Smishra 			uint_t ncpu_per_core = 1;
15457282Smishra 			uint_t chipid_shift = 0;
15467282Smishra 			uint_t ncpu_per_chip = 1;
15477282Smishra 			uint_t i;
15487282Smishra 			uint_t level;
15497282Smishra 
15507282Smishra 			for (i = 0; i < CPI_FNB_ECX_MAX; i++) {
15517282Smishra 				cp->cp_eax = 0xB;
15527282Smishra 				cp->cp_ecx = i;
15537282Smishra 
15547282Smishra 				(void) __cpuid_insn(cp);
15557282Smishra 				level = CPI_CPU_LEVEL_TYPE(cp);
15567282Smishra 
15577282Smishra 				if (level == 1) {
15587282Smishra 					x2apic_id = cp->cp_edx;
15597282Smishra 					coreid_shift = BITX(cp->cp_eax, 4, 0);
15607282Smishra 					ncpu_per_core = BITX(cp->cp_ebx, 15, 0);
15617282Smishra 				} else if (level == 2) {
15627282Smishra 					x2apic_id = cp->cp_edx;
15637282Smishra 					chipid_shift = BITX(cp->cp_eax, 4, 0);
15647282Smishra 					ncpu_per_chip = BITX(cp->cp_ebx, 15, 0);
15657282Smishra 				}
15667282Smishra 			}
15677282Smishra 
15687282Smishra 			cpi->cpi_apicid = x2apic_id;
15697282Smishra 			cpi->cpi_ncpu_per_chip = ncpu_per_chip;
15707282Smishra 			cpi->cpi_ncore_per_chip = ncpu_per_chip /
15717282Smishra 			    ncpu_per_core;
15727282Smishra 			cpi->cpi_chipid = x2apic_id >> chipid_shift;
15737282Smishra 			cpi->cpi_clogid = x2apic_id & ((1 << chipid_shift) - 1);
15747282Smishra 			cpi->cpi_coreid = x2apic_id >> coreid_shift;
15757282Smishra 			cpi->cpi_pkgcoreid = cpi->cpi_clogid >> coreid_shift;
15767282Smishra 		}
15777798SSaurabh.Mishra@Sun.COM 
15787798SSaurabh.Mishra@Sun.COM 		/* Make cp NULL so that we don't stumble on others */
15797798SSaurabh.Mishra@Sun.COM 		cp = NULL;
15807282Smishra 	}
15817282Smishra 
15820Sstevel@tonic-gate 	if ((cpi->cpi_xmaxeax & 0x80000000) == 0)
15830Sstevel@tonic-gate 		goto pass2_done;
15840Sstevel@tonic-gate 
15850Sstevel@tonic-gate 	if ((nmax = cpi->cpi_xmaxeax - 0x80000000 + 1) > NMAX_CPI_EXTD)
15860Sstevel@tonic-gate 		nmax = NMAX_CPI_EXTD;
15870Sstevel@tonic-gate 	/*
15880Sstevel@tonic-gate 	 * Copy the extended properties, fixing them as we go.
15890Sstevel@tonic-gate 	 * (We already handled n == 0 and n == 1 in pass 1)
15900Sstevel@tonic-gate 	 */
15910Sstevel@tonic-gate 	iptr = (void *)cpi->cpi_brandstr;
15920Sstevel@tonic-gate 	for (n = 2, cp = &cpi->cpi_extd[2]; n < nmax; cp++, n++) {
15931228Sandrei 		cp->cp_eax = 0x80000000 + n;
15941228Sandrei 		(void) __cpuid_insn(cp);
15953446Smrj 		platform_cpuid_mangle(cpi->cpi_vendor, 0x80000000 + n, cp);
15960Sstevel@tonic-gate 		switch (n) {
15970Sstevel@tonic-gate 		case 2:
15980Sstevel@tonic-gate 		case 3:
15990Sstevel@tonic-gate 		case 4:
16000Sstevel@tonic-gate 			/*
16010Sstevel@tonic-gate 			 * Extract the brand string
16020Sstevel@tonic-gate 			 */
16030Sstevel@tonic-gate 			*iptr++ = cp->cp_eax;
16040Sstevel@tonic-gate 			*iptr++ = cp->cp_ebx;
16050Sstevel@tonic-gate 			*iptr++ = cp->cp_ecx;
16060Sstevel@tonic-gate 			*iptr++ = cp->cp_edx;
16070Sstevel@tonic-gate 			break;
16080Sstevel@tonic-gate 		case 5:
16090Sstevel@tonic-gate 			switch (cpi->cpi_vendor) {
16100Sstevel@tonic-gate 			case X86_VENDOR_AMD:
16110Sstevel@tonic-gate 				/*
16120Sstevel@tonic-gate 				 * The Athlon and Duron were the first
16130Sstevel@tonic-gate 				 * parts to report the sizes of the
16140Sstevel@tonic-gate 				 * TLB for large pages. Before then,
16150Sstevel@tonic-gate 				 * we don't trust the data.
16160Sstevel@tonic-gate 				 */
16170Sstevel@tonic-gate 				if (cpi->cpi_family < 6 ||
16180Sstevel@tonic-gate 				    (cpi->cpi_family == 6 &&
16190Sstevel@tonic-gate 				    cpi->cpi_model < 1))
16200Sstevel@tonic-gate 					cp->cp_eax = 0;
16210Sstevel@tonic-gate 				break;
16220Sstevel@tonic-gate 			default:
16230Sstevel@tonic-gate 				break;
16240Sstevel@tonic-gate 			}
16250Sstevel@tonic-gate 			break;
16260Sstevel@tonic-gate 		case 6:
16270Sstevel@tonic-gate 			switch (cpi->cpi_vendor) {
16280Sstevel@tonic-gate 			case X86_VENDOR_AMD:
16290Sstevel@tonic-gate 				/*
16300Sstevel@tonic-gate 				 * The Athlon and Duron were the first
16310Sstevel@tonic-gate 				 * AMD parts with L2 TLB's.
16320Sstevel@tonic-gate 				 * Before then, don't trust the data.
16330Sstevel@tonic-gate 				 */
16340Sstevel@tonic-gate 				if (cpi->cpi_family < 6 ||
16350Sstevel@tonic-gate 				    cpi->cpi_family == 6 &&
16360Sstevel@tonic-gate 				    cpi->cpi_model < 1)
16370Sstevel@tonic-gate 					cp->cp_eax = cp->cp_ebx = 0;
16380Sstevel@tonic-gate 				/*
16390Sstevel@tonic-gate 				 * AMD Duron rev A0 reports L2
16400Sstevel@tonic-gate 				 * cache size incorrectly as 1K
16410Sstevel@tonic-gate 				 * when it is really 64K
16420Sstevel@tonic-gate 				 */
16430Sstevel@tonic-gate 				if (cpi->cpi_family == 6 &&
16440Sstevel@tonic-gate 				    cpi->cpi_model == 3 &&
16450Sstevel@tonic-gate 				    cpi->cpi_step == 0) {
16460Sstevel@tonic-gate 					cp->cp_ecx &= 0xffff;
16470Sstevel@tonic-gate 					cp->cp_ecx |= 0x400000;
16480Sstevel@tonic-gate 				}
16490Sstevel@tonic-gate 				break;
16500Sstevel@tonic-gate 			case X86_VENDOR_Cyrix:	/* VIA C3 */
16510Sstevel@tonic-gate 				/*
16520Sstevel@tonic-gate 				 * VIA C3 processors are a bit messed
16530Sstevel@tonic-gate 				 * up w.r.t. encoding cache sizes in %ecx
16540Sstevel@tonic-gate 				 */
16550Sstevel@tonic-gate 				if (cpi->cpi_family != 6)
16560Sstevel@tonic-gate 					break;
16570Sstevel@tonic-gate 				/*
16580Sstevel@tonic-gate 				 * model 7 and 8 were incorrectly encoded
16590Sstevel@tonic-gate 				 *
16600Sstevel@tonic-gate 				 * xxx is model 8 really broken?
16610Sstevel@tonic-gate 				 */
16620Sstevel@tonic-gate 				if (cpi->cpi_model == 7 ||
16630Sstevel@tonic-gate 				    cpi->cpi_model == 8)
16640Sstevel@tonic-gate 					cp->cp_ecx =
16650Sstevel@tonic-gate 					    BITX(cp->cp_ecx, 31, 24) << 16 |
16660Sstevel@tonic-gate 					    BITX(cp->cp_ecx, 23, 16) << 12 |
16670Sstevel@tonic-gate 					    BITX(cp->cp_ecx, 15, 8) << 8 |
16680Sstevel@tonic-gate 					    BITX(cp->cp_ecx, 7, 0);
16690Sstevel@tonic-gate 				/*
16700Sstevel@tonic-gate 				 * model 9 stepping 1 has wrong associativity
16710Sstevel@tonic-gate 				 */
16720Sstevel@tonic-gate 				if (cpi->cpi_model == 9 && cpi->cpi_step == 1)
16730Sstevel@tonic-gate 					cp->cp_ecx |= 8 << 12;
16740Sstevel@tonic-gate 				break;
16750Sstevel@tonic-gate 			case X86_VENDOR_Intel:
16760Sstevel@tonic-gate 				/*
16770Sstevel@tonic-gate 				 * Extended L2 Cache features function.
16780Sstevel@tonic-gate 				 * First appeared on Prescott.
16790Sstevel@tonic-gate 				 */
16800Sstevel@tonic-gate 			default:
16810Sstevel@tonic-gate 				break;
16820Sstevel@tonic-gate 			}
16830Sstevel@tonic-gate 			break;
16840Sstevel@tonic-gate 		default:
16850Sstevel@tonic-gate 			break;
16860Sstevel@tonic-gate 		}
16870Sstevel@tonic-gate 	}
16880Sstevel@tonic-gate 
16890Sstevel@tonic-gate pass2_done:
16900Sstevel@tonic-gate 	cpi->cpi_pass = 2;
16910Sstevel@tonic-gate }
16920Sstevel@tonic-gate 
16930Sstevel@tonic-gate static const char *
16940Sstevel@tonic-gate intel_cpubrand(const struct cpuid_info *cpi)
16950Sstevel@tonic-gate {
16960Sstevel@tonic-gate 	int i;
16970Sstevel@tonic-gate 
16980Sstevel@tonic-gate 	if ((x86_feature & X86_CPUID) == 0 ||
16990Sstevel@tonic-gate 	    cpi->cpi_maxeax < 1 || cpi->cpi_family < 5)
17000Sstevel@tonic-gate 		return ("i486");
17010Sstevel@tonic-gate 
17020Sstevel@tonic-gate 	switch (cpi->cpi_family) {
17030Sstevel@tonic-gate 	case 5:
17040Sstevel@tonic-gate 		return ("Intel Pentium(r)");
17050Sstevel@tonic-gate 	case 6:
17060Sstevel@tonic-gate 		switch (cpi->cpi_model) {
17070Sstevel@tonic-gate 			uint_t celeron, xeon;
17081228Sandrei 			const struct cpuid_regs *cp;
17090Sstevel@tonic-gate 		case 0:
17100Sstevel@tonic-gate 		case 1:
17110Sstevel@tonic-gate 		case 2:
17120Sstevel@tonic-gate 			return ("Intel Pentium(r) Pro");
17130Sstevel@tonic-gate 		case 3:
17140Sstevel@tonic-gate 		case 4:
17150Sstevel@tonic-gate 			return ("Intel Pentium(r) II");
17160Sstevel@tonic-gate 		case 6:
17170Sstevel@tonic-gate 			return ("Intel Celeron(r)");
17180Sstevel@tonic-gate 		case 5:
17190Sstevel@tonic-gate 		case 7:
17200Sstevel@tonic-gate 			celeron = xeon = 0;
17210Sstevel@tonic-gate 			cp = &cpi->cpi_std[2];	/* cache info */
17220Sstevel@tonic-gate 
17236317Skk208521 			for (i = 1; i < 4; i++) {
17240Sstevel@tonic-gate 				uint_t tmp;
17250Sstevel@tonic-gate 
17260Sstevel@tonic-gate 				tmp = (cp->cp_eax >> (8 * i)) & 0xff;
17270Sstevel@tonic-gate 				if (tmp == 0x40)
17280Sstevel@tonic-gate 					celeron++;
17290Sstevel@tonic-gate 				if (tmp >= 0x44 && tmp <= 0x45)
17300Sstevel@tonic-gate 					xeon++;
17310Sstevel@tonic-gate 			}
17320Sstevel@tonic-gate 
17330Sstevel@tonic-gate 			for (i = 0; i < 2; i++) {
17340Sstevel@tonic-gate 				uint_t tmp;
17350Sstevel@tonic-gate 
17360Sstevel@tonic-gate 				tmp = (cp->cp_ebx >> (8 * i)) & 0xff;
17370Sstevel@tonic-gate 				if (tmp == 0x40)
17380Sstevel@tonic-gate 					celeron++;
17390Sstevel@tonic-gate 				else if (tmp >= 0x44 && tmp <= 0x45)
17400Sstevel@tonic-gate 					xeon++;
17410Sstevel@tonic-gate 			}
17420Sstevel@tonic-gate 
17430Sstevel@tonic-gate 			for (i = 0; i < 4; i++) {
17440Sstevel@tonic-gate 				uint_t tmp;
17450Sstevel@tonic-gate 
17460Sstevel@tonic-gate 				tmp = (cp->cp_ecx >> (8 * i)) & 0xff;
17470Sstevel@tonic-gate 				if (tmp == 0x40)
17480Sstevel@tonic-gate 					celeron++;
17490Sstevel@tonic-gate 				else if (tmp >= 0x44 && tmp <= 0x45)
17500Sstevel@tonic-gate 					xeon++;
17510Sstevel@tonic-gate 			}
17520Sstevel@tonic-gate 
17530Sstevel@tonic-gate 			for (i = 0; i < 4; i++) {
17540Sstevel@tonic-gate 				uint_t tmp;
17550Sstevel@tonic-gate 
17560Sstevel@tonic-gate 				tmp = (cp->cp_edx >> (8 * i)) & 0xff;
17570Sstevel@tonic-gate 				if (tmp == 0x40)
17580Sstevel@tonic-gate 					celeron++;
17590Sstevel@tonic-gate 				else if (tmp >= 0x44 && tmp <= 0x45)
17600Sstevel@tonic-gate 					xeon++;
17610Sstevel@tonic-gate 			}
17620Sstevel@tonic-gate 
17630Sstevel@tonic-gate 			if (celeron)
17640Sstevel@tonic-gate 				return ("Intel Celeron(r)");
17650Sstevel@tonic-gate 			if (xeon)
17660Sstevel@tonic-gate 				return (cpi->cpi_model == 5 ?
17670Sstevel@tonic-gate 				    "Intel Pentium(r) II Xeon(tm)" :
17680Sstevel@tonic-gate 				    "Intel Pentium(r) III Xeon(tm)");
17690Sstevel@tonic-gate 			return (cpi->cpi_model == 5 ?
17700Sstevel@tonic-gate 			    "Intel Pentium(r) II or Pentium(r) II Xeon(tm)" :
17710Sstevel@tonic-gate 			    "Intel Pentium(r) III or Pentium(r) III Xeon(tm)");
17720Sstevel@tonic-gate 		default:
17730Sstevel@tonic-gate 			break;
17740Sstevel@tonic-gate 		}
17750Sstevel@tonic-gate 	default:
17760Sstevel@tonic-gate 		break;
17770Sstevel@tonic-gate 	}
17780Sstevel@tonic-gate 
17791975Sdmick 	/* BrandID is present if the field is nonzero */
17801975Sdmick 	if (cpi->cpi_brandid != 0) {
17810Sstevel@tonic-gate 		static const struct {
17820Sstevel@tonic-gate 			uint_t bt_bid;
17830Sstevel@tonic-gate 			const char *bt_str;
17840Sstevel@tonic-gate 		} brand_tbl[] = {
17850Sstevel@tonic-gate 			{ 0x1,	"Intel(r) Celeron(r)" },
17860Sstevel@tonic-gate 			{ 0x2,	"Intel(r) Pentium(r) III" },
17870Sstevel@tonic-gate 			{ 0x3,	"Intel(r) Pentium(r) III Xeon(tm)" },
17880Sstevel@tonic-gate 			{ 0x4,	"Intel(r) Pentium(r) III" },
17890Sstevel@tonic-gate 			{ 0x6,	"Mobile Intel(r) Pentium(r) III" },
17900Sstevel@tonic-gate 			{ 0x7,	"Mobile Intel(r) Celeron(r)" },
17910Sstevel@tonic-gate 			{ 0x8,	"Intel(r) Pentium(r) 4" },
17920Sstevel@tonic-gate 			{ 0x9,	"Intel(r) Pentium(r) 4" },
17930Sstevel@tonic-gate 			{ 0xa,	"Intel(r) Celeron(r)" },
17940Sstevel@tonic-gate 			{ 0xb,	"Intel(r) Xeon(tm)" },
17950Sstevel@tonic-gate 			{ 0xc,	"Intel(r) Xeon(tm) MP" },
17960Sstevel@tonic-gate 			{ 0xe,	"Mobile Intel(r) Pentium(r) 4" },
17971975Sdmick 			{ 0xf,	"Mobile Intel(r) Celeron(r)" },
17981975Sdmick 			{ 0x11, "Mobile Genuine Intel(r)" },
17991975Sdmick 			{ 0x12, "Intel(r) Celeron(r) M" },
18001975Sdmick 			{ 0x13, "Mobile Intel(r) Celeron(r)" },
18011975Sdmick 			{ 0x14, "Intel(r) Celeron(r)" },
18021975Sdmick 			{ 0x15, "Mobile Genuine Intel(r)" },
18031975Sdmick 			{ 0x16,	"Intel(r) Pentium(r) M" },
18041975Sdmick 			{ 0x17, "Mobile Intel(r) Celeron(r)" }
18050Sstevel@tonic-gate 		};
18060Sstevel@tonic-gate 		uint_t btblmax = sizeof (brand_tbl) / sizeof (brand_tbl[0]);
18070Sstevel@tonic-gate 		uint_t sgn;
18080Sstevel@tonic-gate 
18090Sstevel@tonic-gate 		sgn = (cpi->cpi_family << 8) |
18100Sstevel@tonic-gate 		    (cpi->cpi_model << 4) | cpi->cpi_step;
18110Sstevel@tonic-gate 
18120Sstevel@tonic-gate 		for (i = 0; i < btblmax; i++)
18130Sstevel@tonic-gate 			if (brand_tbl[i].bt_bid == cpi->cpi_brandid)
18140Sstevel@tonic-gate 				break;
18150Sstevel@tonic-gate 		if (i < btblmax) {
18160Sstevel@tonic-gate 			if (sgn == 0x6b1 && cpi->cpi_brandid == 3)
18170Sstevel@tonic-gate 				return ("Intel(r) Celeron(r)");
18180Sstevel@tonic-gate 			if (sgn < 0xf13 && cpi->cpi_brandid == 0xb)
18190Sstevel@tonic-gate 				return ("Intel(r) Xeon(tm) MP");
18200Sstevel@tonic-gate 			if (sgn < 0xf13 && cpi->cpi_brandid == 0xe)
18210Sstevel@tonic-gate 				return ("Intel(r) Xeon(tm)");
18220Sstevel@tonic-gate 			return (brand_tbl[i].bt_str);
18230Sstevel@tonic-gate 		}
18240Sstevel@tonic-gate 	}
18250Sstevel@tonic-gate 
18260Sstevel@tonic-gate 	return (NULL);
18270Sstevel@tonic-gate }
18280Sstevel@tonic-gate 
18290Sstevel@tonic-gate static const char *
18300Sstevel@tonic-gate amd_cpubrand(const struct cpuid_info *cpi)
18310Sstevel@tonic-gate {
18320Sstevel@tonic-gate 	if ((x86_feature & X86_CPUID) == 0 ||
18330Sstevel@tonic-gate 	    cpi->cpi_maxeax < 1 || cpi->cpi_family < 5)
18340Sstevel@tonic-gate 		return ("i486 compatible");
18350Sstevel@tonic-gate 
18360Sstevel@tonic-gate 	switch (cpi->cpi_family) {
18370Sstevel@tonic-gate 	case 5:
18380Sstevel@tonic-gate 		switch (cpi->cpi_model) {
18390Sstevel@tonic-gate 		case 0:
18400Sstevel@tonic-gate 		case 1:
18410Sstevel@tonic-gate 		case 2:
18420Sstevel@tonic-gate 		case 3:
18430Sstevel@tonic-gate 		case 4:
18440Sstevel@tonic-gate 		case 5:
18450Sstevel@tonic-gate 			return ("AMD-K5(r)");
18460Sstevel@tonic-gate 		case 6:
18470Sstevel@tonic-gate 		case 7:
18480Sstevel@tonic-gate 			return ("AMD-K6(r)");
18490Sstevel@tonic-gate 		case 8:
18500Sstevel@tonic-gate 			return ("AMD-K6(r)-2");
18510Sstevel@tonic-gate 		case 9:
18520Sstevel@tonic-gate 			return ("AMD-K6(r)-III");
18530Sstevel@tonic-gate 		default:
18540Sstevel@tonic-gate 			return ("AMD (family 5)");
18550Sstevel@tonic-gate 		}
18560Sstevel@tonic-gate 	case 6:
18570Sstevel@tonic-gate 		switch (cpi->cpi_model) {
18580Sstevel@tonic-gate 		case 1:
18590Sstevel@tonic-gate 			return ("AMD-K7(tm)");
18600Sstevel@tonic-gate 		case 0:
18610Sstevel@tonic-gate 		case 2:
18620Sstevel@tonic-gate 		case 4:
18630Sstevel@tonic-gate 			return ("AMD Athlon(tm)");
18640Sstevel@tonic-gate 		case 3:
18650Sstevel@tonic-gate 		case 7:
18660Sstevel@tonic-gate 			return ("AMD Duron(tm)");
18670Sstevel@tonic-gate 		case 6:
18680Sstevel@tonic-gate 		case 8:
18690Sstevel@tonic-gate 		case 10:
18700Sstevel@tonic-gate 			/*
18710Sstevel@tonic-gate 			 * Use the L2 cache size to distinguish
18720Sstevel@tonic-gate 			 */
18730Sstevel@tonic-gate 			return ((cpi->cpi_extd[6].cp_ecx >> 16) >= 256 ?
18740Sstevel@tonic-gate 			    "AMD Athlon(tm)" : "AMD Duron(tm)");
18750Sstevel@tonic-gate 		default:
18760Sstevel@tonic-gate 			return ("AMD (family 6)");
18770Sstevel@tonic-gate 		}
18780Sstevel@tonic-gate 	default:
18790Sstevel@tonic-gate 		break;
18800Sstevel@tonic-gate 	}
18810Sstevel@tonic-gate 
18820Sstevel@tonic-gate 	if (cpi->cpi_family == 0xf && cpi->cpi_model == 5 &&
18830Sstevel@tonic-gate 	    cpi->cpi_brandid != 0) {
18840Sstevel@tonic-gate 		switch (BITX(cpi->cpi_brandid, 7, 5)) {
18850Sstevel@tonic-gate 		case 3:
18860Sstevel@tonic-gate 			return ("AMD Opteron(tm) UP 1xx");
18870Sstevel@tonic-gate 		case 4:
18880Sstevel@tonic-gate 			return ("AMD Opteron(tm) DP 2xx");
18890Sstevel@tonic-gate 		case 5:
18900Sstevel@tonic-gate 			return ("AMD Opteron(tm) MP 8xx");
18910Sstevel@tonic-gate 		default:
18920Sstevel@tonic-gate 			return ("AMD Opteron(tm)");
18930Sstevel@tonic-gate 		}
18940Sstevel@tonic-gate 	}
18950Sstevel@tonic-gate 
18960Sstevel@tonic-gate 	return (NULL);
18970Sstevel@tonic-gate }
18980Sstevel@tonic-gate 
18990Sstevel@tonic-gate static const char *
19000Sstevel@tonic-gate cyrix_cpubrand(struct cpuid_info *cpi, uint_t type)
19010Sstevel@tonic-gate {
19020Sstevel@tonic-gate 	if ((x86_feature & X86_CPUID) == 0 ||
19030Sstevel@tonic-gate 	    cpi->cpi_maxeax < 1 || cpi->cpi_family < 5 ||
19040Sstevel@tonic-gate 	    type == X86_TYPE_CYRIX_486)
19050Sstevel@tonic-gate 		return ("i486 compatible");
19060Sstevel@tonic-gate 
19070Sstevel@tonic-gate 	switch (type) {
19080Sstevel@tonic-gate 	case X86_TYPE_CYRIX_6x86:
19090Sstevel@tonic-gate 		return ("Cyrix 6x86");
19100Sstevel@tonic-gate 	case X86_TYPE_CYRIX_6x86L:
19110Sstevel@tonic-gate 		return ("Cyrix 6x86L");
19120Sstevel@tonic-gate 	case X86_TYPE_CYRIX_6x86MX:
19130Sstevel@tonic-gate 		return ("Cyrix 6x86MX");
19140Sstevel@tonic-gate 	case X86_TYPE_CYRIX_GXm:
19150Sstevel@tonic-gate 		return ("Cyrix GXm");
19160Sstevel@tonic-gate 	case X86_TYPE_CYRIX_MediaGX:
19170Sstevel@tonic-gate 		return ("Cyrix MediaGX");
19180Sstevel@tonic-gate 	case X86_TYPE_CYRIX_MII:
19190Sstevel@tonic-gate 		return ("Cyrix M2");
19200Sstevel@tonic-gate 	case X86_TYPE_VIA_CYRIX_III:
19210Sstevel@tonic-gate 		return ("VIA Cyrix M3");
19220Sstevel@tonic-gate 	default:
19230Sstevel@tonic-gate 		/*
19240Sstevel@tonic-gate 		 * Have another wild guess ..
19250Sstevel@tonic-gate 		 */
19260Sstevel@tonic-gate 		if (cpi->cpi_family == 4 && cpi->cpi_model == 9)
19270Sstevel@tonic-gate 			return ("Cyrix 5x86");
19280Sstevel@tonic-gate 		else if (cpi->cpi_family == 5) {
19290Sstevel@tonic-gate 			switch (cpi->cpi_model) {
19300Sstevel@tonic-gate 			case 2:
19310Sstevel@tonic-gate 				return ("Cyrix 6x86");	/* Cyrix M1 */
19320Sstevel@tonic-gate 			case 4:
19330Sstevel@tonic-gate 				return ("Cyrix MediaGX");
19340Sstevel@tonic-gate 			default:
19350Sstevel@tonic-gate 				break;
19360Sstevel@tonic-gate 			}
19370Sstevel@tonic-gate 		} else if (cpi->cpi_family == 6) {
19380Sstevel@tonic-gate 			switch (cpi->cpi_model) {
19390Sstevel@tonic-gate 			case 0:
19400Sstevel@tonic-gate 				return ("Cyrix 6x86MX"); /* Cyrix M2? */
19410Sstevel@tonic-gate 			case 5:
19420Sstevel@tonic-gate 			case 6:
19430Sstevel@tonic-gate 			case 7:
19440Sstevel@tonic-gate 			case 8:
19450Sstevel@tonic-gate 			case 9:
19460Sstevel@tonic-gate 				return ("VIA C3");
19470Sstevel@tonic-gate 			default:
19480Sstevel@tonic-gate 				break;
19490Sstevel@tonic-gate 			}
19500Sstevel@tonic-gate 		}
19510Sstevel@tonic-gate 		break;
19520Sstevel@tonic-gate 	}
19530Sstevel@tonic-gate 	return (NULL);
19540Sstevel@tonic-gate }
19550Sstevel@tonic-gate 
19560Sstevel@tonic-gate /*
19570Sstevel@tonic-gate  * This only gets called in the case that the CPU extended
19580Sstevel@tonic-gate  * feature brand string (0x80000002, 0x80000003, 0x80000004)
19590Sstevel@tonic-gate  * aren't available, or contain null bytes for some reason.
19600Sstevel@tonic-gate  */
19610Sstevel@tonic-gate static void
19620Sstevel@tonic-gate fabricate_brandstr(struct cpuid_info *cpi)
19630Sstevel@tonic-gate {
19640Sstevel@tonic-gate 	const char *brand = NULL;
19650Sstevel@tonic-gate 
19660Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
19670Sstevel@tonic-gate 	case X86_VENDOR_Intel:
19680Sstevel@tonic-gate 		brand = intel_cpubrand(cpi);
19690Sstevel@tonic-gate 		break;
19700Sstevel@tonic-gate 	case X86_VENDOR_AMD:
19710Sstevel@tonic-gate 		brand = amd_cpubrand(cpi);
19720Sstevel@tonic-gate 		break;
19730Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
19740Sstevel@tonic-gate 		brand = cyrix_cpubrand(cpi, x86_type);
19750Sstevel@tonic-gate 		break;
19760Sstevel@tonic-gate 	case X86_VENDOR_NexGen:
19770Sstevel@tonic-gate 		if (cpi->cpi_family == 5 && cpi->cpi_model == 0)
19780Sstevel@tonic-gate 			brand = "NexGen Nx586";
19790Sstevel@tonic-gate 		break;
19800Sstevel@tonic-gate 	case X86_VENDOR_Centaur:
19810Sstevel@tonic-gate 		if (cpi->cpi_family == 5)
19820Sstevel@tonic-gate 			switch (cpi->cpi_model) {
19830Sstevel@tonic-gate 			case 4:
19840Sstevel@tonic-gate 				brand = "Centaur C6";
19850Sstevel@tonic-gate 				break;
19860Sstevel@tonic-gate 			case 8:
19870Sstevel@tonic-gate 				brand = "Centaur C2";
19880Sstevel@tonic-gate 				break;
19890Sstevel@tonic-gate 			case 9:
19900Sstevel@tonic-gate 				brand = "Centaur C3";
19910Sstevel@tonic-gate 				break;
19920Sstevel@tonic-gate 			default:
19930Sstevel@tonic-gate 				break;
19940Sstevel@tonic-gate 			}
19950Sstevel@tonic-gate 		break;
19960Sstevel@tonic-gate 	case X86_VENDOR_Rise:
19970Sstevel@tonic-gate 		if (cpi->cpi_family == 5 &&
19980Sstevel@tonic-gate 		    (cpi->cpi_model == 0 || cpi->cpi_model == 2))
19990Sstevel@tonic-gate 			brand = "Rise mP6";
20000Sstevel@tonic-gate 		break;
20010Sstevel@tonic-gate 	case X86_VENDOR_SiS:
20020Sstevel@tonic-gate 		if (cpi->cpi_family == 5 && cpi->cpi_model == 0)
20030Sstevel@tonic-gate 			brand = "SiS 55x";
20040Sstevel@tonic-gate 		break;
20050Sstevel@tonic-gate 	case X86_VENDOR_TM:
20060Sstevel@tonic-gate 		if (cpi->cpi_family == 5 && cpi->cpi_model == 4)
20070Sstevel@tonic-gate 			brand = "Transmeta Crusoe TM3x00 or TM5x00";
20080Sstevel@tonic-gate 		break;
20090Sstevel@tonic-gate 	case X86_VENDOR_NSC:
20100Sstevel@tonic-gate 	case X86_VENDOR_UMC:
20110Sstevel@tonic-gate 	default:
20120Sstevel@tonic-gate 		break;
20130Sstevel@tonic-gate 	}
20140Sstevel@tonic-gate 	if (brand) {
20150Sstevel@tonic-gate 		(void) strcpy((char *)cpi->cpi_brandstr, brand);
20160Sstevel@tonic-gate 		return;
20170Sstevel@tonic-gate 	}
20180Sstevel@tonic-gate 
20190Sstevel@tonic-gate 	/*
20200Sstevel@tonic-gate 	 * If all else fails ...
20210Sstevel@tonic-gate 	 */
20220Sstevel@tonic-gate 	(void) snprintf(cpi->cpi_brandstr, sizeof (cpi->cpi_brandstr),
20230Sstevel@tonic-gate 	    "%s %d.%d.%d", cpi->cpi_vendorstr, cpi->cpi_family,
20240Sstevel@tonic-gate 	    cpi->cpi_model, cpi->cpi_step);
20250Sstevel@tonic-gate }
20260Sstevel@tonic-gate 
20270Sstevel@tonic-gate /*
20280Sstevel@tonic-gate  * This routine is called just after kernel memory allocation
20290Sstevel@tonic-gate  * becomes available on cpu0, and as part of mp_startup() on
20300Sstevel@tonic-gate  * the other cpus.
20310Sstevel@tonic-gate  *
20324606Sesaxe  * Fixup the brand string, and collect any information from cpuid
20334606Sesaxe  * that requires dynamicically allocated storage to represent.
20340Sstevel@tonic-gate  */
20350Sstevel@tonic-gate /*ARGSUSED*/
20360Sstevel@tonic-gate void
20370Sstevel@tonic-gate cpuid_pass3(cpu_t *cpu)
20380Sstevel@tonic-gate {
20394606Sesaxe 	int	i, max, shft, level, size;
20404606Sesaxe 	struct cpuid_regs regs;
20414606Sesaxe 	struct cpuid_regs *cp;
20420Sstevel@tonic-gate 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
20430Sstevel@tonic-gate 
20440Sstevel@tonic-gate 	ASSERT(cpi->cpi_pass == 2);
20450Sstevel@tonic-gate 
20464606Sesaxe 	/*
20474606Sesaxe 	 * Function 4: Deterministic cache parameters
20484606Sesaxe 	 *
20494606Sesaxe 	 * Take this opportunity to detect the number of threads
20504606Sesaxe 	 * sharing the last level cache, and construct a corresponding
20514606Sesaxe 	 * cache id. The respective cpuid_info members are initialized
20524606Sesaxe 	 * to the default case of "no last level cache sharing".
20534606Sesaxe 	 */
20544606Sesaxe 	cpi->cpi_ncpu_shr_last_cache = 1;
20554606Sesaxe 	cpi->cpi_last_lvl_cacheid = cpu->cpu_id;
20564606Sesaxe 
20574606Sesaxe 	if (cpi->cpi_maxeax >= 4 && cpi->cpi_vendor == X86_VENDOR_Intel) {
20584606Sesaxe 
20594606Sesaxe 		/*
20604606Sesaxe 		 * Find the # of elements (size) returned by fn 4, and along
20614606Sesaxe 		 * the way detect last level cache sharing details.
20624606Sesaxe 		 */
20634606Sesaxe 		bzero(&regs, sizeof (regs));
20644606Sesaxe 		cp = &regs;
20654606Sesaxe 		for (i = 0, max = 0; i < CPI_FN4_ECX_MAX; i++) {
20664606Sesaxe 			cp->cp_eax = 4;
20674606Sesaxe 			cp->cp_ecx = i;
20684606Sesaxe 
20694606Sesaxe 			(void) __cpuid_insn(cp);
20704606Sesaxe 
20714606Sesaxe 			if (CPI_CACHE_TYPE(cp) == 0)
20724606Sesaxe 				break;
20734606Sesaxe 			level = CPI_CACHE_LVL(cp);
20744606Sesaxe 			if (level > max) {
20754606Sesaxe 				max = level;
20764606Sesaxe 				cpi->cpi_ncpu_shr_last_cache =
20774606Sesaxe 				    CPI_NTHR_SHR_CACHE(cp) + 1;
20784606Sesaxe 			}
20794606Sesaxe 		}
20804606Sesaxe 		cpi->cpi_std_4_size = size = i;
20814606Sesaxe 
20824606Sesaxe 		/*
20834606Sesaxe 		 * Allocate the cpi_std_4 array. The first element
20844606Sesaxe 		 * references the regs for fn 4, %ecx == 0, which
20854606Sesaxe 		 * cpuid_pass2() stashed in cpi->cpi_std[4].
20864606Sesaxe 		 */
20874606Sesaxe 		if (size > 0) {
20884606Sesaxe 			cpi->cpi_std_4 =
20894606Sesaxe 			    kmem_alloc(size * sizeof (cp), KM_SLEEP);
20904606Sesaxe 			cpi->cpi_std_4[0] = &cpi->cpi_std[4];
20914606Sesaxe 
20924606Sesaxe 			/*
20934606Sesaxe 			 * Allocate storage to hold the additional regs
20944606Sesaxe 			 * for function 4, %ecx == 1 .. cpi_std_4_size.
20954606Sesaxe 			 *
20964606Sesaxe 			 * The regs for fn 4, %ecx == 0 has already
20974606Sesaxe 			 * been allocated as indicated above.
20984606Sesaxe 			 */
20994606Sesaxe 			for (i = 1; i < size; i++) {
21004606Sesaxe 				cp = cpi->cpi_std_4[i] =
21014606Sesaxe 				    kmem_zalloc(sizeof (regs), KM_SLEEP);
21024606Sesaxe 				cp->cp_eax = 4;
21034606Sesaxe 				cp->cp_ecx = i;
21044606Sesaxe 
21054606Sesaxe 				(void) __cpuid_insn(cp);
21064606Sesaxe 			}
21074606Sesaxe 		}
21084606Sesaxe 		/*
21094606Sesaxe 		 * Determine the number of bits needed to represent
21104606Sesaxe 		 * the number of CPUs sharing the last level cache.
21114606Sesaxe 		 *
21124606Sesaxe 		 * Shift off that number of bits from the APIC id to
21134606Sesaxe 		 * derive the cache id.
21144606Sesaxe 		 */
21154606Sesaxe 		shft = 0;
21164606Sesaxe 		for (i = 1; i < cpi->cpi_ncpu_shr_last_cache; i <<= 1)
21174606Sesaxe 			shft++;
21187282Smishra 		cpi->cpi_last_lvl_cacheid = cpi->cpi_apicid >> shft;
21190Sstevel@tonic-gate 	}
21200Sstevel@tonic-gate 
21210Sstevel@tonic-gate 	/*
21224606Sesaxe 	 * Now fixup the brand string
21230Sstevel@tonic-gate 	 */
21244606Sesaxe 	if ((cpi->cpi_xmaxeax & 0x80000000) == 0) {
21254606Sesaxe 		fabricate_brandstr(cpi);
21264606Sesaxe 	} else {
21270Sstevel@tonic-gate 
21280Sstevel@tonic-gate 		/*
21294606Sesaxe 		 * If we successfully extracted a brand string from the cpuid
21304606Sesaxe 		 * instruction, clean it up by removing leading spaces and
21314606Sesaxe 		 * similar junk.
21320Sstevel@tonic-gate 		 */
21334606Sesaxe 		if (cpi->cpi_brandstr[0]) {
21344606Sesaxe 			size_t maxlen = sizeof (cpi->cpi_brandstr);
21354606Sesaxe 			char *src, *dst;
21364606Sesaxe 
21374606Sesaxe 			dst = src = (char *)cpi->cpi_brandstr;
21384606Sesaxe 			src[maxlen - 1] = '\0';
21394606Sesaxe 			/*
21404606Sesaxe 			 * strip leading spaces
21414606Sesaxe 			 */
21424606Sesaxe 			while (*src == ' ')
21434606Sesaxe 				src++;
21444606Sesaxe 			/*
21454606Sesaxe 			 * Remove any 'Genuine' or "Authentic" prefixes
21464606Sesaxe 			 */
21474606Sesaxe 			if (strncmp(src, "Genuine ", 8) == 0)
21484606Sesaxe 				src += 8;
21494606Sesaxe 			if (strncmp(src, "Authentic ", 10) == 0)
21504606Sesaxe 				src += 10;
21514606Sesaxe 
21524606Sesaxe 			/*
21534606Sesaxe 			 * Now do an in-place copy.
21544606Sesaxe 			 * Map (R) to (r) and (TM) to (tm).
21554606Sesaxe 			 * The era of teletypes is long gone, and there's
21564606Sesaxe 			 * -really- no need to shout.
21574606Sesaxe 			 */
21584606Sesaxe 			while (*src != '\0') {
21594606Sesaxe 				if (src[0] == '(') {
21604606Sesaxe 					if (strncmp(src + 1, "R)", 2) == 0) {
21614606Sesaxe 						(void) strncpy(dst, "(r)", 3);
21624606Sesaxe 						src += 3;
21634606Sesaxe 						dst += 3;
21644606Sesaxe 						continue;
21654606Sesaxe 					}
21664606Sesaxe 					if (strncmp(src + 1, "TM)", 3) == 0) {
21674606Sesaxe 						(void) strncpy(dst, "(tm)", 4);
21684606Sesaxe 						src += 4;
21694606Sesaxe 						dst += 4;
21704606Sesaxe 						continue;
21714606Sesaxe 					}
21720Sstevel@tonic-gate 				}
21734606Sesaxe 				*dst++ = *src++;
21740Sstevel@tonic-gate 			}
21754606Sesaxe 			*dst = '\0';
21764606Sesaxe 
21774606Sesaxe 			/*
21784606Sesaxe 			 * Finally, remove any trailing spaces
21794606Sesaxe 			 */
21804606Sesaxe 			while (--dst > cpi->cpi_brandstr)
21814606Sesaxe 				if (*dst == ' ')
21824606Sesaxe 					*dst = '\0';
21834606Sesaxe 				else
21844606Sesaxe 					break;
21854606Sesaxe 		} else
21864606Sesaxe 			fabricate_brandstr(cpi);
21874606Sesaxe 	}
21880Sstevel@tonic-gate 	cpi->cpi_pass = 3;
21890Sstevel@tonic-gate }
21900Sstevel@tonic-gate 
21910Sstevel@tonic-gate /*
21920Sstevel@tonic-gate  * This routine is called out of bind_hwcap() much later in the life
21930Sstevel@tonic-gate  * of the kernel (post_startup()).  The job of this routine is to resolve
21940Sstevel@tonic-gate  * the hardware feature support and kernel support for those features into
21950Sstevel@tonic-gate  * what we're actually going to tell applications via the aux vector.
21960Sstevel@tonic-gate  */
21970Sstevel@tonic-gate uint_t
21980Sstevel@tonic-gate cpuid_pass4(cpu_t *cpu)
21990Sstevel@tonic-gate {
22000Sstevel@tonic-gate 	struct cpuid_info *cpi;
22010Sstevel@tonic-gate 	uint_t hwcap_flags = 0;
22020Sstevel@tonic-gate 
22030Sstevel@tonic-gate 	if (cpu == NULL)
22040Sstevel@tonic-gate 		cpu = CPU;
22050Sstevel@tonic-gate 	cpi = cpu->cpu_m.mcpu_cpi;
22060Sstevel@tonic-gate 
22070Sstevel@tonic-gate 	ASSERT(cpi->cpi_pass == 3);
22080Sstevel@tonic-gate 
22090Sstevel@tonic-gate 	if (cpi->cpi_maxeax >= 1) {
22100Sstevel@tonic-gate 		uint32_t *edx = &cpi->cpi_support[STD_EDX_FEATURES];
22110Sstevel@tonic-gate 		uint32_t *ecx = &cpi->cpi_support[STD_ECX_FEATURES];
22120Sstevel@tonic-gate 
22130Sstevel@tonic-gate 		*edx = CPI_FEATURES_EDX(cpi);
22140Sstevel@tonic-gate 		*ecx = CPI_FEATURES_ECX(cpi);
22150Sstevel@tonic-gate 
22160Sstevel@tonic-gate 		/*
22170Sstevel@tonic-gate 		 * [these require explicit kernel support]
22180Sstevel@tonic-gate 		 */
22190Sstevel@tonic-gate 		if ((x86_feature & X86_SEP) == 0)
22200Sstevel@tonic-gate 			*edx &= ~CPUID_INTC_EDX_SEP;
22210Sstevel@tonic-gate 
22220Sstevel@tonic-gate 		if ((x86_feature & X86_SSE) == 0)
22230Sstevel@tonic-gate 			*edx &= ~(CPUID_INTC_EDX_FXSR|CPUID_INTC_EDX_SSE);
22240Sstevel@tonic-gate 		if ((x86_feature & X86_SSE2) == 0)
22250Sstevel@tonic-gate 			*edx &= ~CPUID_INTC_EDX_SSE2;
22260Sstevel@tonic-gate 
22270Sstevel@tonic-gate 		if ((x86_feature & X86_HTT) == 0)
22280Sstevel@tonic-gate 			*edx &= ~CPUID_INTC_EDX_HTT;
22290Sstevel@tonic-gate 
22300Sstevel@tonic-gate 		if ((x86_feature & X86_SSE3) == 0)
22310Sstevel@tonic-gate 			*ecx &= ~CPUID_INTC_ECX_SSE3;
22320Sstevel@tonic-gate 
22335269Skk208521 		if (cpi->cpi_vendor == X86_VENDOR_Intel) {
22345269Skk208521 			if ((x86_feature & X86_SSSE3) == 0)
22355269Skk208521 				*ecx &= ~CPUID_INTC_ECX_SSSE3;
22365269Skk208521 			if ((x86_feature & X86_SSE4_1) == 0)
22375269Skk208521 				*ecx &= ~CPUID_INTC_ECX_SSE4_1;
22385269Skk208521 			if ((x86_feature & X86_SSE4_2) == 0)
22395269Skk208521 				*ecx &= ~CPUID_INTC_ECX_SSE4_2;
22409370SKuriakose.Kuruvilla@Sun.COM 			if ((x86_feature & X86_AES) == 0)
22419370SKuriakose.Kuruvilla@Sun.COM 				*ecx &= ~CPUID_INTC_ECX_AES;
22425269Skk208521 		}
22435269Skk208521 
22440Sstevel@tonic-gate 		/*
22450Sstevel@tonic-gate 		 * [no explicit support required beyond x87 fp context]
22460Sstevel@tonic-gate 		 */
22470Sstevel@tonic-gate 		if (!fpu_exists)
22480Sstevel@tonic-gate 			*edx &= ~(CPUID_INTC_EDX_FPU | CPUID_INTC_EDX_MMX);
22490Sstevel@tonic-gate 
22500Sstevel@tonic-gate 		/*
22510Sstevel@tonic-gate 		 * Now map the supported feature vector to things that we
22520Sstevel@tonic-gate 		 * think userland will care about.
22530Sstevel@tonic-gate 		 */
22540Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_SEP)
22550Sstevel@tonic-gate 			hwcap_flags |= AV_386_SEP;
22560Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_SSE)
22570Sstevel@tonic-gate 			hwcap_flags |= AV_386_FXSR | AV_386_SSE;
22580Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_SSE2)
22590Sstevel@tonic-gate 			hwcap_flags |= AV_386_SSE2;
22600Sstevel@tonic-gate 		if (*ecx & CPUID_INTC_ECX_SSE3)
22610Sstevel@tonic-gate 			hwcap_flags |= AV_386_SSE3;
22625269Skk208521 		if (cpi->cpi_vendor == X86_VENDOR_Intel) {
22635269Skk208521 			if (*ecx & CPUID_INTC_ECX_SSSE3)
22645269Skk208521 				hwcap_flags |= AV_386_SSSE3;
22655269Skk208521 			if (*ecx & CPUID_INTC_ECX_SSE4_1)
22665269Skk208521 				hwcap_flags |= AV_386_SSE4_1;
22675269Skk208521 			if (*ecx & CPUID_INTC_ECX_SSE4_2)
22685269Skk208521 				hwcap_flags |= AV_386_SSE4_2;
22698418SKrishnendu.Sadhukhan@Sun.COM 			if (*ecx & CPUID_INTC_ECX_MOVBE)
22708418SKrishnendu.Sadhukhan@Sun.COM 				hwcap_flags |= AV_386_MOVBE;
22719370SKuriakose.Kuruvilla@Sun.COM 			if (*ecx & CPUID_INTC_ECX_AES)
22729370SKuriakose.Kuruvilla@Sun.COM 				hwcap_flags |= AV_386_AES;
22739370SKuriakose.Kuruvilla@Sun.COM 			if (*ecx & CPUID_INTC_ECX_PCLMULQDQ)
22749370SKuriakose.Kuruvilla@Sun.COM 				hwcap_flags |= AV_386_PCLMULQDQ;
22755269Skk208521 		}
22764628Skk208521 		if (*ecx & CPUID_INTC_ECX_POPCNT)
22774628Skk208521 			hwcap_flags |= AV_386_POPCNT;
22780Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_FPU)
22790Sstevel@tonic-gate 			hwcap_flags |= AV_386_FPU;
22800Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_MMX)
22810Sstevel@tonic-gate 			hwcap_flags |= AV_386_MMX;
22820Sstevel@tonic-gate 
22830Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_TSC)
22840Sstevel@tonic-gate 			hwcap_flags |= AV_386_TSC;
22850Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_CX8)
22860Sstevel@tonic-gate 			hwcap_flags |= AV_386_CX8;
22870Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_CMOV)
22880Sstevel@tonic-gate 			hwcap_flags |= AV_386_CMOV;
22890Sstevel@tonic-gate 		if (*ecx & CPUID_INTC_ECX_CX16)
22900Sstevel@tonic-gate 			hwcap_flags |= AV_386_CX16;
22910Sstevel@tonic-gate 	}
22920Sstevel@tonic-gate 
22930Sstevel@tonic-gate 	if (cpi->cpi_xmaxeax < 0x80000001)
22940Sstevel@tonic-gate 		goto pass4_done;
22950Sstevel@tonic-gate 
22960Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
22971228Sandrei 		struct cpuid_regs cp;
22983446Smrj 		uint32_t *edx, *ecx;
22990Sstevel@tonic-gate 
23003446Smrj 	case X86_VENDOR_Intel:
23013446Smrj 		/*
23023446Smrj 		 * Seems like Intel duplicated what we necessary
23033446Smrj 		 * here to make the initial crop of 64-bit OS's work.
23043446Smrj 		 * Hopefully, those are the only "extended" bits
23053446Smrj 		 * they'll add.
23063446Smrj 		 */
23073446Smrj 		/*FALLTHROUGH*/
23083446Smrj 
23090Sstevel@tonic-gate 	case X86_VENDOR_AMD:
23100Sstevel@tonic-gate 		edx = &cpi->cpi_support[AMD_EDX_FEATURES];
23113446Smrj 		ecx = &cpi->cpi_support[AMD_ECX_FEATURES];
23120Sstevel@tonic-gate 
23130Sstevel@tonic-gate 		*edx = CPI_FEATURES_XTD_EDX(cpi);
23143446Smrj 		*ecx = CPI_FEATURES_XTD_ECX(cpi);
23153446Smrj 
23163446Smrj 		/*
23173446Smrj 		 * [these features require explicit kernel support]
23183446Smrj 		 */
23193446Smrj 		switch (cpi->cpi_vendor) {
23203446Smrj 		case X86_VENDOR_Intel:
23216657Ssudheer 			if ((x86_feature & X86_TSCP) == 0)
23226657Ssudheer 				*edx &= ~CPUID_AMD_EDX_TSCP;
23233446Smrj 			break;
23243446Smrj 
23253446Smrj 		case X86_VENDOR_AMD:
23263446Smrj 			if ((x86_feature & X86_TSCP) == 0)
23273446Smrj 				*edx &= ~CPUID_AMD_EDX_TSCP;
23284628Skk208521 			if ((x86_feature & X86_SSE4A) == 0)
23294628Skk208521 				*ecx &= ~CPUID_AMD_ECX_SSE4A;
23303446Smrj 			break;
23313446Smrj 
23323446Smrj 		default:
23333446Smrj 			break;
23343446Smrj 		}
23350Sstevel@tonic-gate 
23360Sstevel@tonic-gate 		/*
23370Sstevel@tonic-gate 		 * [no explicit support required beyond
23380Sstevel@tonic-gate 		 * x87 fp context and exception handlers]
23390Sstevel@tonic-gate 		 */
23400Sstevel@tonic-gate 		if (!fpu_exists)
23410Sstevel@tonic-gate 			*edx &= ~(CPUID_AMD_EDX_MMXamd |
23420Sstevel@tonic-gate 			    CPUID_AMD_EDX_3DNow | CPUID_AMD_EDX_3DNowx);
23430Sstevel@tonic-gate 
23440Sstevel@tonic-gate 		if ((x86_feature & X86_NX) == 0)
23450Sstevel@tonic-gate 			*edx &= ~CPUID_AMD_EDX_NX;
23463446Smrj #if !defined(__amd64)
23470Sstevel@tonic-gate 		*edx &= ~CPUID_AMD_EDX_LM;
23480Sstevel@tonic-gate #endif
23490Sstevel@tonic-gate 		/*
23500Sstevel@tonic-gate 		 * Now map the supported feature vector to
23510Sstevel@tonic-gate 		 * things that we think userland will care about.
23520Sstevel@tonic-gate 		 */
23533446Smrj #if defined(__amd64)
23540Sstevel@tonic-gate 		if (*edx & CPUID_AMD_EDX_SYSC)
23550Sstevel@tonic-gate 			hwcap_flags |= AV_386_AMD_SYSC;
23563446Smrj #endif
23570Sstevel@tonic-gate 		if (*edx & CPUID_AMD_EDX_MMXamd)
23580Sstevel@tonic-gate 			hwcap_flags |= AV_386_AMD_MMX;
23590Sstevel@tonic-gate 		if (*edx & CPUID_AMD_EDX_3DNow)
23600Sstevel@tonic-gate 			hwcap_flags |= AV_386_AMD_3DNow;
23610Sstevel@tonic-gate 		if (*edx & CPUID_AMD_EDX_3DNowx)
23620Sstevel@tonic-gate 			hwcap_flags |= AV_386_AMD_3DNowx;
23633446Smrj 
23643446Smrj 		switch (cpi->cpi_vendor) {
23653446Smrj 		case X86_VENDOR_AMD:
23663446Smrj 			if (*edx & CPUID_AMD_EDX_TSCP)
23673446Smrj 				hwcap_flags |= AV_386_TSCP;
23683446Smrj 			if (*ecx & CPUID_AMD_ECX_AHF64)
23693446Smrj 				hwcap_flags |= AV_386_AHF;
23704628Skk208521 			if (*ecx & CPUID_AMD_ECX_SSE4A)
23714628Skk208521 				hwcap_flags |= AV_386_AMD_SSE4A;
23724628Skk208521 			if (*ecx & CPUID_AMD_ECX_LZCNT)
23734628Skk208521 				hwcap_flags |= AV_386_AMD_LZCNT;
23743446Smrj 			break;
23753446Smrj 
23763446Smrj 		case X86_VENDOR_Intel:
23776657Ssudheer 			if (*edx & CPUID_AMD_EDX_TSCP)
23786657Ssudheer 				hwcap_flags |= AV_386_TSCP;
23793446Smrj 			/*
23803446Smrj 			 * Aarrgh.
23813446Smrj 			 * Intel uses a different bit in the same word.
23823446Smrj 			 */
23833446Smrj 			if (*ecx & CPUID_INTC_ECX_AHF64)
23843446Smrj 				hwcap_flags |= AV_386_AHF;
23853446Smrj 			break;
23863446Smrj 
23873446Smrj 		default:
23883446Smrj 			break;
23893446Smrj 		}
23900Sstevel@tonic-gate 		break;
23910Sstevel@tonic-gate 
23920Sstevel@tonic-gate 	case X86_VENDOR_TM:
23931228Sandrei 		cp.cp_eax = 0x80860001;
23941228Sandrei 		(void) __cpuid_insn(&cp);
23951228Sandrei 		cpi->cpi_support[TM_EDX_FEATURES] = cp.cp_edx;
23960Sstevel@tonic-gate 		break;
23970Sstevel@tonic-gate 
23980Sstevel@tonic-gate 	default:
23990Sstevel@tonic-gate 		break;
24000Sstevel@tonic-gate 	}
24010Sstevel@tonic-gate 
24020Sstevel@tonic-gate pass4_done:
24030Sstevel@tonic-gate 	cpi->cpi_pass = 4;
24040Sstevel@tonic-gate 	return (hwcap_flags);
24050Sstevel@tonic-gate }
24060Sstevel@tonic-gate 
24070Sstevel@tonic-gate 
24080Sstevel@tonic-gate /*
24090Sstevel@tonic-gate  * Simulate the cpuid instruction using the data we previously
24100Sstevel@tonic-gate  * captured about this CPU.  We try our best to return the truth
24110Sstevel@tonic-gate  * about the hardware, independently of kernel support.
24120Sstevel@tonic-gate  */
24130Sstevel@tonic-gate uint32_t
24141228Sandrei cpuid_insn(cpu_t *cpu, struct cpuid_regs *cp)
24150Sstevel@tonic-gate {
24160Sstevel@tonic-gate 	struct cpuid_info *cpi;
24171228Sandrei 	struct cpuid_regs *xcp;
24180Sstevel@tonic-gate 
24190Sstevel@tonic-gate 	if (cpu == NULL)
24200Sstevel@tonic-gate 		cpu = CPU;
24210Sstevel@tonic-gate 	cpi = cpu->cpu_m.mcpu_cpi;
24220Sstevel@tonic-gate 
24230Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 3));
24240Sstevel@tonic-gate 
24250Sstevel@tonic-gate 	/*
24260Sstevel@tonic-gate 	 * CPUID data is cached in two separate places: cpi_std for standard
24270Sstevel@tonic-gate 	 * CPUID functions, and cpi_extd for extended CPUID functions.
24280Sstevel@tonic-gate 	 */
24291228Sandrei 	if (cp->cp_eax <= cpi->cpi_maxeax && cp->cp_eax < NMAX_CPI_STD)
24301228Sandrei 		xcp = &cpi->cpi_std[cp->cp_eax];
24311228Sandrei 	else if (cp->cp_eax >= 0x80000000 && cp->cp_eax <= cpi->cpi_xmaxeax &&
24321228Sandrei 	    cp->cp_eax < 0x80000000 + NMAX_CPI_EXTD)
24331228Sandrei 		xcp = &cpi->cpi_extd[cp->cp_eax - 0x80000000];
24340Sstevel@tonic-gate 	else
24350Sstevel@tonic-gate 		/*
24360Sstevel@tonic-gate 		 * The caller is asking for data from an input parameter which
24370Sstevel@tonic-gate 		 * the kernel has not cached.  In this case we go fetch from
24380Sstevel@tonic-gate 		 * the hardware and return the data directly to the user.
24390Sstevel@tonic-gate 		 */
24401228Sandrei 		return (__cpuid_insn(cp));
24411228Sandrei 
24421228Sandrei 	cp->cp_eax = xcp->cp_eax;
24431228Sandrei 	cp->cp_ebx = xcp->cp_ebx;
24441228Sandrei 	cp->cp_ecx = xcp->cp_ecx;
24451228Sandrei 	cp->cp_edx = xcp->cp_edx;
24460Sstevel@tonic-gate 	return (cp->cp_eax);
24470Sstevel@tonic-gate }
24480Sstevel@tonic-gate 
24490Sstevel@tonic-gate int
24500Sstevel@tonic-gate cpuid_checkpass(cpu_t *cpu, int pass)
24510Sstevel@tonic-gate {
24520Sstevel@tonic-gate 	return (cpu != NULL && cpu->cpu_m.mcpu_cpi != NULL &&
24530Sstevel@tonic-gate 	    cpu->cpu_m.mcpu_cpi->cpi_pass >= pass);
24540Sstevel@tonic-gate }
24550Sstevel@tonic-gate 
24560Sstevel@tonic-gate int
24570Sstevel@tonic-gate cpuid_getbrandstr(cpu_t *cpu, char *s, size_t n)
24580Sstevel@tonic-gate {
24590Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 3));
24600Sstevel@tonic-gate 
24610Sstevel@tonic-gate 	return (snprintf(s, n, "%s", cpu->cpu_m.mcpu_cpi->cpi_brandstr));
24620Sstevel@tonic-gate }
24630Sstevel@tonic-gate 
24640Sstevel@tonic-gate int
24651228Sandrei cpuid_is_cmt(cpu_t *cpu)
24660Sstevel@tonic-gate {
24670Sstevel@tonic-gate 	if (cpu == NULL)
24680Sstevel@tonic-gate 		cpu = CPU;
24690Sstevel@tonic-gate 
24700Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
24710Sstevel@tonic-gate 
24720Sstevel@tonic-gate 	return (cpu->cpu_m.mcpu_cpi->cpi_chipid >= 0);
24730Sstevel@tonic-gate }
24740Sstevel@tonic-gate 
24750Sstevel@tonic-gate /*
24760Sstevel@tonic-gate  * AMD and Intel both implement the 64-bit variant of the syscall
24770Sstevel@tonic-gate  * instruction (syscallq), so if there's -any- support for syscall,
24780Sstevel@tonic-gate  * cpuid currently says "yes, we support this".
24790Sstevel@tonic-gate  *
24800Sstevel@tonic-gate  * However, Intel decided to -not- implement the 32-bit variant of the
24810Sstevel@tonic-gate  * syscall instruction, so we provide a predicate to allow our caller
24820Sstevel@tonic-gate  * to test that subtlety here.
24835084Sjohnlev  *
24845084Sjohnlev  * XXPV	Currently, 32-bit syscall instructions don't work via the hypervisor,
24855084Sjohnlev  *	even in the case where the hardware would in fact support it.
24860Sstevel@tonic-gate  */
24870Sstevel@tonic-gate /*ARGSUSED*/
24880Sstevel@tonic-gate int
24890Sstevel@tonic-gate cpuid_syscall32_insn(cpu_t *cpu)
24900Sstevel@tonic-gate {
24910Sstevel@tonic-gate 	ASSERT(cpuid_checkpass((cpu == NULL ? CPU : cpu), 1));
24920Sstevel@tonic-gate 
24935084Sjohnlev #if !defined(__xpv)
24943446Smrj 	if (cpu == NULL)
24953446Smrj 		cpu = CPU;
24963446Smrj 
24973446Smrj 	/*CSTYLED*/
24983446Smrj 	{
24993446Smrj 		struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
25003446Smrj 
25013446Smrj 		if (cpi->cpi_vendor == X86_VENDOR_AMD &&
25023446Smrj 		    cpi->cpi_xmaxeax >= 0x80000001 &&
25033446Smrj 		    (CPI_FEATURES_XTD_EDX(cpi) & CPUID_AMD_EDX_SYSC))
25043446Smrj 			return (1);
25053446Smrj 	}
25065084Sjohnlev #endif
25070Sstevel@tonic-gate 	return (0);
25080Sstevel@tonic-gate }
25090Sstevel@tonic-gate 
25100Sstevel@tonic-gate int
25110Sstevel@tonic-gate cpuid_getidstr(cpu_t *cpu, char *s, size_t n)
25120Sstevel@tonic-gate {
25130Sstevel@tonic-gate 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
25140Sstevel@tonic-gate 
25150Sstevel@tonic-gate 	static const char fmt[] =
25163779Sdmick 	    "x86 (%s %X family %d model %d step %d clock %d MHz)";
25170Sstevel@tonic-gate 	static const char fmt_ht[] =
25183779Sdmick 	    "x86 (chipid 0x%x %s %X family %d model %d step %d clock %d MHz)";
25190Sstevel@tonic-gate 
25200Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
25210Sstevel@tonic-gate 
25221228Sandrei 	if (cpuid_is_cmt(cpu))
25230Sstevel@tonic-gate 		return (snprintf(s, n, fmt_ht, cpi->cpi_chipid,
25243779Sdmick 		    cpi->cpi_vendorstr, cpi->cpi_std[1].cp_eax,
25253779Sdmick 		    cpi->cpi_family, cpi->cpi_model,
25260Sstevel@tonic-gate 		    cpi->cpi_step, cpu->cpu_type_info.pi_clock));
25270Sstevel@tonic-gate 	return (snprintf(s, n, fmt,
25283779Sdmick 	    cpi->cpi_vendorstr, cpi->cpi_std[1].cp_eax,
25293779Sdmick 	    cpi->cpi_family, cpi->cpi_model,
25300Sstevel@tonic-gate 	    cpi->cpi_step, cpu->cpu_type_info.pi_clock));
25310Sstevel@tonic-gate }
25320Sstevel@tonic-gate 
25330Sstevel@tonic-gate const char *
25340Sstevel@tonic-gate cpuid_getvendorstr(cpu_t *cpu)
25350Sstevel@tonic-gate {
25360Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
25370Sstevel@tonic-gate 	return ((const char *)cpu->cpu_m.mcpu_cpi->cpi_vendorstr);
25380Sstevel@tonic-gate }
25390Sstevel@tonic-gate 
25400Sstevel@tonic-gate uint_t
25410Sstevel@tonic-gate cpuid_getvendor(cpu_t *cpu)
25420Sstevel@tonic-gate {
25430Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
25440Sstevel@tonic-gate 	return (cpu->cpu_m.mcpu_cpi->cpi_vendor);
25450Sstevel@tonic-gate }
25460Sstevel@tonic-gate 
25470Sstevel@tonic-gate uint_t
25480Sstevel@tonic-gate cpuid_getfamily(cpu_t *cpu)
25490Sstevel@tonic-gate {
25500Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
25510Sstevel@tonic-gate 	return (cpu->cpu_m.mcpu_cpi->cpi_family);
25520Sstevel@tonic-gate }
25530Sstevel@tonic-gate 
25540Sstevel@tonic-gate uint_t
25550Sstevel@tonic-gate cpuid_getmodel(cpu_t *cpu)
25560Sstevel@tonic-gate {
25570Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
25580Sstevel@tonic-gate 	return (cpu->cpu_m.mcpu_cpi->cpi_model);
25590Sstevel@tonic-gate }
25600Sstevel@tonic-gate 
25610Sstevel@tonic-gate uint_t
25620Sstevel@tonic-gate cpuid_get_ncpu_per_chip(cpu_t *cpu)
25630Sstevel@tonic-gate {
25640Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
25650Sstevel@tonic-gate 	return (cpu->cpu_m.mcpu_cpi->cpi_ncpu_per_chip);
25660Sstevel@tonic-gate }
25670Sstevel@tonic-gate 
25680Sstevel@tonic-gate uint_t
25691228Sandrei cpuid_get_ncore_per_chip(cpu_t *cpu)
25701228Sandrei {
25711228Sandrei 	ASSERT(cpuid_checkpass(cpu, 1));
25721228Sandrei 	return (cpu->cpu_m.mcpu_cpi->cpi_ncore_per_chip);
25731228Sandrei }
25741228Sandrei 
25751228Sandrei uint_t
25764606Sesaxe cpuid_get_ncpu_sharing_last_cache(cpu_t *cpu)
25774606Sesaxe {
25784606Sesaxe 	ASSERT(cpuid_checkpass(cpu, 2));
25794606Sesaxe 	return (cpu->cpu_m.mcpu_cpi->cpi_ncpu_shr_last_cache);
25804606Sesaxe }
25814606Sesaxe 
25824606Sesaxe id_t
25834606Sesaxe cpuid_get_last_lvl_cacheid(cpu_t *cpu)
25844606Sesaxe {
25854606Sesaxe 	ASSERT(cpuid_checkpass(cpu, 2));
25864606Sesaxe 	return (cpu->cpu_m.mcpu_cpi->cpi_last_lvl_cacheid);
25874606Sesaxe }
25884606Sesaxe 
25894606Sesaxe uint_t
25900Sstevel@tonic-gate cpuid_getstep(cpu_t *cpu)
25910Sstevel@tonic-gate {
25920Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
25930Sstevel@tonic-gate 	return (cpu->cpu_m.mcpu_cpi->cpi_step);
25940Sstevel@tonic-gate }
25950Sstevel@tonic-gate 
25964581Ssherrym uint_t
25974581Ssherrym cpuid_getsig(struct cpu *cpu)
25984581Ssherrym {
25994581Ssherrym 	ASSERT(cpuid_checkpass(cpu, 1));
26004581Ssherrym 	return (cpu->cpu_m.mcpu_cpi->cpi_std[1].cp_eax);
26014581Ssherrym }
26024581Ssherrym 
26032869Sgavinm uint32_t
26042869Sgavinm cpuid_getchiprev(struct cpu *cpu)
26052869Sgavinm {
26062869Sgavinm 	ASSERT(cpuid_checkpass(cpu, 1));
26072869Sgavinm 	return (cpu->cpu_m.mcpu_cpi->cpi_chiprev);
26082869Sgavinm }
26092869Sgavinm 
26102869Sgavinm const char *
26112869Sgavinm cpuid_getchiprevstr(struct cpu *cpu)
26122869Sgavinm {
26132869Sgavinm 	ASSERT(cpuid_checkpass(cpu, 1));
26142869Sgavinm 	return (cpu->cpu_m.mcpu_cpi->cpi_chiprevstr);
26152869Sgavinm }
26162869Sgavinm 
26172869Sgavinm uint32_t
26182869Sgavinm cpuid_getsockettype(struct cpu *cpu)
26192869Sgavinm {
26202869Sgavinm 	ASSERT(cpuid_checkpass(cpu, 1));
26212869Sgavinm 	return (cpu->cpu_m.mcpu_cpi->cpi_socket);
26222869Sgavinm }
26232869Sgavinm 
26249482SKuriakose.Kuruvilla@Sun.COM const char *
26259482SKuriakose.Kuruvilla@Sun.COM cpuid_getsocketstr(cpu_t *cpu)
26269482SKuriakose.Kuruvilla@Sun.COM {
26279482SKuriakose.Kuruvilla@Sun.COM 	static const char *socketstr = NULL;
26289482SKuriakose.Kuruvilla@Sun.COM 	struct cpuid_info *cpi;
26299482SKuriakose.Kuruvilla@Sun.COM 
26309482SKuriakose.Kuruvilla@Sun.COM 	ASSERT(cpuid_checkpass(cpu, 1));
26319482SKuriakose.Kuruvilla@Sun.COM 	cpi = cpu->cpu_m.mcpu_cpi;
26329482SKuriakose.Kuruvilla@Sun.COM 
26339482SKuriakose.Kuruvilla@Sun.COM 	/* Assume that socket types are the same across the system */
26349482SKuriakose.Kuruvilla@Sun.COM 	if (socketstr == NULL)
26359482SKuriakose.Kuruvilla@Sun.COM 		socketstr = _cpuid_sktstr(cpi->cpi_vendor, cpi->cpi_family,
26369482SKuriakose.Kuruvilla@Sun.COM 		    cpi->cpi_model, cpi->cpi_step);
26379482SKuriakose.Kuruvilla@Sun.COM 
26389482SKuriakose.Kuruvilla@Sun.COM 
26399482SKuriakose.Kuruvilla@Sun.COM 	return (socketstr);
26409482SKuriakose.Kuruvilla@Sun.COM }
26419482SKuriakose.Kuruvilla@Sun.COM 
26423434Sesaxe int
26433434Sesaxe cpuid_get_chipid(cpu_t *cpu)
26440Sstevel@tonic-gate {
26450Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
26460Sstevel@tonic-gate 
26471228Sandrei 	if (cpuid_is_cmt(cpu))
26480Sstevel@tonic-gate 		return (cpu->cpu_m.mcpu_cpi->cpi_chipid);
26490Sstevel@tonic-gate 	return (cpu->cpu_id);
26500Sstevel@tonic-gate }
26510Sstevel@tonic-gate 
26521228Sandrei id_t
26533434Sesaxe cpuid_get_coreid(cpu_t *cpu)
26541228Sandrei {
26551228Sandrei 	ASSERT(cpuid_checkpass(cpu, 1));
26561228Sandrei 	return (cpu->cpu_m.mcpu_cpi->cpi_coreid);
26571228Sandrei }
26581228Sandrei 
26590Sstevel@tonic-gate int
26605870Sgavinm cpuid_get_pkgcoreid(cpu_t *cpu)
26615870Sgavinm {
26625870Sgavinm 	ASSERT(cpuid_checkpass(cpu, 1));
26635870Sgavinm 	return (cpu->cpu_m.mcpu_cpi->cpi_pkgcoreid);
26645870Sgavinm }
26655870Sgavinm 
26665870Sgavinm int
26673434Sesaxe cpuid_get_clogid(cpu_t *cpu)
26680Sstevel@tonic-gate {
26690Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
26700Sstevel@tonic-gate 	return (cpu->cpu_m.mcpu_cpi->cpi_clogid);
26710Sstevel@tonic-gate }
26720Sstevel@tonic-gate 
267311389SAlexander.Kolbasov@Sun.COM int
267411389SAlexander.Kolbasov@Sun.COM cpuid_get_cacheid(cpu_t *cpu)
267511389SAlexander.Kolbasov@Sun.COM {
267611389SAlexander.Kolbasov@Sun.COM 	ASSERT(cpuid_checkpass(cpu, 1));
267711389SAlexander.Kolbasov@Sun.COM 	return (cpu->cpu_m.mcpu_cpi->cpi_last_lvl_cacheid);
267811389SAlexander.Kolbasov@Sun.COM }
267911389SAlexander.Kolbasov@Sun.COM 
268010947SSrihari.Venkatesan@Sun.COM uint_t
268110947SSrihari.Venkatesan@Sun.COM cpuid_get_procnodeid(cpu_t *cpu)
268210947SSrihari.Venkatesan@Sun.COM {
268310947SSrihari.Venkatesan@Sun.COM 	ASSERT(cpuid_checkpass(cpu, 1));
268410947SSrihari.Venkatesan@Sun.COM 	return (cpu->cpu_m.mcpu_cpi->cpi_procnodeid);
268510947SSrihari.Venkatesan@Sun.COM }
268610947SSrihari.Venkatesan@Sun.COM 
268710947SSrihari.Venkatesan@Sun.COM uint_t
268810947SSrihari.Venkatesan@Sun.COM cpuid_get_procnodes_per_pkg(cpu_t *cpu)
268910947SSrihari.Venkatesan@Sun.COM {
269010947SSrihari.Venkatesan@Sun.COM 	ASSERT(cpuid_checkpass(cpu, 1));
269110947SSrihari.Venkatesan@Sun.COM 	return (cpu->cpu_m.mcpu_cpi->cpi_procnodes_per_pkg);
269210947SSrihari.Venkatesan@Sun.COM }
269310947SSrihari.Venkatesan@Sun.COM 
269410080SJoe.Bonasera@sun.com /*ARGSUSED*/
269510080SJoe.Bonasera@sun.com int
269610080SJoe.Bonasera@sun.com cpuid_have_cr8access(cpu_t *cpu)
269710080SJoe.Bonasera@sun.com {
269810080SJoe.Bonasera@sun.com #if defined(__amd64)
269910080SJoe.Bonasera@sun.com 	return (1);
270010080SJoe.Bonasera@sun.com #else
270110080SJoe.Bonasera@sun.com 	struct cpuid_info *cpi;
270210080SJoe.Bonasera@sun.com 
270310080SJoe.Bonasera@sun.com 	ASSERT(cpu != NULL);
270410080SJoe.Bonasera@sun.com 	cpi = cpu->cpu_m.mcpu_cpi;
270510080SJoe.Bonasera@sun.com 	if (cpi->cpi_vendor == X86_VENDOR_AMD && cpi->cpi_maxeax >= 1 &&
270610080SJoe.Bonasera@sun.com 	    (CPI_FEATURES_XTD_ECX(cpi) & CPUID_AMD_ECX_CR8D) != 0)
270710080SJoe.Bonasera@sun.com 		return (1);
270810080SJoe.Bonasera@sun.com 	return (0);
270910080SJoe.Bonasera@sun.com #endif
271010080SJoe.Bonasera@sun.com }
271110080SJoe.Bonasera@sun.com 
27129652SMichael.Corcoran@Sun.COM uint32_t
27139652SMichael.Corcoran@Sun.COM cpuid_get_apicid(cpu_t *cpu)
27149652SMichael.Corcoran@Sun.COM {
27159652SMichael.Corcoran@Sun.COM 	ASSERT(cpuid_checkpass(cpu, 1));
27169652SMichael.Corcoran@Sun.COM 	if (cpu->cpu_m.mcpu_cpi->cpi_maxeax < 1) {
27179652SMichael.Corcoran@Sun.COM 		return (UINT32_MAX);
27189652SMichael.Corcoran@Sun.COM 	} else {
27199652SMichael.Corcoran@Sun.COM 		return (cpu->cpu_m.mcpu_cpi->cpi_apicid);
27209652SMichael.Corcoran@Sun.COM 	}
27219652SMichael.Corcoran@Sun.COM }
27229652SMichael.Corcoran@Sun.COM 
27230Sstevel@tonic-gate void
27240Sstevel@tonic-gate cpuid_get_addrsize(cpu_t *cpu, uint_t *pabits, uint_t *vabits)
27250Sstevel@tonic-gate {
27260Sstevel@tonic-gate 	struct cpuid_info *cpi;
27270Sstevel@tonic-gate 
27280Sstevel@tonic-gate 	if (cpu == NULL)
27290Sstevel@tonic-gate 		cpu = CPU;
27300Sstevel@tonic-gate 	cpi = cpu->cpu_m.mcpu_cpi;
27310Sstevel@tonic-gate 
27320Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
27330Sstevel@tonic-gate 
27340Sstevel@tonic-gate 	if (pabits)
27350Sstevel@tonic-gate 		*pabits = cpi->cpi_pabits;
27360Sstevel@tonic-gate 	if (vabits)
27370Sstevel@tonic-gate 		*vabits = cpi->cpi_vabits;
27380Sstevel@tonic-gate }
27390Sstevel@tonic-gate 
27400Sstevel@tonic-gate /*
27410Sstevel@tonic-gate  * Returns the number of data TLB entries for a corresponding
27420Sstevel@tonic-gate  * pagesize.  If it can't be computed, or isn't known, the
27430Sstevel@tonic-gate  * routine returns zero.  If you ask about an architecturally
27440Sstevel@tonic-gate  * impossible pagesize, the routine will panic (so that the
27450Sstevel@tonic-gate  * hat implementor knows that things are inconsistent.)
27460Sstevel@tonic-gate  */
27470Sstevel@tonic-gate uint_t
27480Sstevel@tonic-gate cpuid_get_dtlb_nent(cpu_t *cpu, size_t pagesize)
27490Sstevel@tonic-gate {
27500Sstevel@tonic-gate 	struct cpuid_info *cpi;
27510Sstevel@tonic-gate 	uint_t dtlb_nent = 0;
27520Sstevel@tonic-gate 
27530Sstevel@tonic-gate 	if (cpu == NULL)
27540Sstevel@tonic-gate 		cpu = CPU;
27550Sstevel@tonic-gate 	cpi = cpu->cpu_m.mcpu_cpi;
27560Sstevel@tonic-gate 
27570Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
27580Sstevel@tonic-gate 
27590Sstevel@tonic-gate 	/*
27600Sstevel@tonic-gate 	 * Check the L2 TLB info
27610Sstevel@tonic-gate 	 */
27620Sstevel@tonic-gate 	if (cpi->cpi_xmaxeax >= 0x80000006) {
27631228Sandrei 		struct cpuid_regs *cp = &cpi->cpi_extd[6];
27640Sstevel@tonic-gate 
27650Sstevel@tonic-gate 		switch (pagesize) {
27660Sstevel@tonic-gate 
27670Sstevel@tonic-gate 		case 4 * 1024:
27680Sstevel@tonic-gate 			/*
27690Sstevel@tonic-gate 			 * All zero in the top 16 bits of the register
27700Sstevel@tonic-gate 			 * indicates a unified TLB. Size is in low 16 bits.
27710Sstevel@tonic-gate 			 */
27720Sstevel@tonic-gate 			if ((cp->cp_ebx & 0xffff0000) == 0)
27730Sstevel@tonic-gate 				dtlb_nent = cp->cp_ebx & 0x0000ffff;
27740Sstevel@tonic-gate 			else
27750Sstevel@tonic-gate 				dtlb_nent = BITX(cp->cp_ebx, 27, 16);
27760Sstevel@tonic-gate 			break;
27770Sstevel@tonic-gate 
27780Sstevel@tonic-gate 		case 2 * 1024 * 1024:
27790Sstevel@tonic-gate 			if ((cp->cp_eax & 0xffff0000) == 0)
27800Sstevel@tonic-gate 				dtlb_nent = cp->cp_eax & 0x0000ffff;
27810Sstevel@tonic-gate 			else
27820Sstevel@tonic-gate 				dtlb_nent = BITX(cp->cp_eax, 27, 16);
27830Sstevel@tonic-gate 			break;
27840Sstevel@tonic-gate 
27850Sstevel@tonic-gate 		default:
27860Sstevel@tonic-gate 			panic("unknown L2 pagesize");
27870Sstevel@tonic-gate 			/*NOTREACHED*/
27880Sstevel@tonic-gate 		}
27890Sstevel@tonic-gate 	}
27900Sstevel@tonic-gate 
27910Sstevel@tonic-gate 	if (dtlb_nent != 0)
27920Sstevel@tonic-gate 		return (dtlb_nent);
27930Sstevel@tonic-gate 
27940Sstevel@tonic-gate 	/*
27950Sstevel@tonic-gate 	 * No L2 TLB support for this size, try L1.
27960Sstevel@tonic-gate 	 */
27970Sstevel@tonic-gate 	if (cpi->cpi_xmaxeax >= 0x80000005) {
27981228Sandrei 		struct cpuid_regs *cp = &cpi->cpi_extd[5];
27990Sstevel@tonic-gate 
28000Sstevel@tonic-gate 		switch (pagesize) {
28010Sstevel@tonic-gate 		case 4 * 1024:
28020Sstevel@tonic-gate 			dtlb_nent = BITX(cp->cp_ebx, 23, 16);
28030Sstevel@tonic-gate 			break;
28040Sstevel@tonic-gate 		case 2 * 1024 * 1024:
28050Sstevel@tonic-gate 			dtlb_nent = BITX(cp->cp_eax, 23, 16);
28060Sstevel@tonic-gate 			break;
28070Sstevel@tonic-gate 		default:
28080Sstevel@tonic-gate 			panic("unknown L1 d-TLB pagesize");
28090Sstevel@tonic-gate 			/*NOTREACHED*/
28100Sstevel@tonic-gate 		}
28110Sstevel@tonic-gate 	}
28120Sstevel@tonic-gate 
28130Sstevel@tonic-gate 	return (dtlb_nent);
28140Sstevel@tonic-gate }
28150Sstevel@tonic-gate 
28160Sstevel@tonic-gate /*
28170Sstevel@tonic-gate  * Return 0 if the erratum is not present or not applicable, positive
28180Sstevel@tonic-gate  * if it is, and negative if the status of the erratum is unknown.
28190Sstevel@tonic-gate  *
28200Sstevel@tonic-gate  * See "Revision Guide for AMD Athlon(tm) 64 and AMD Opteron(tm)
2821359Skucharsk  * Processors" #25759, Rev 3.57, August 2005
28220Sstevel@tonic-gate  */
28230Sstevel@tonic-gate int
28240Sstevel@tonic-gate cpuid_opteron_erratum(cpu_t *cpu, uint_t erratum)
28250Sstevel@tonic-gate {
28260Sstevel@tonic-gate 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
28271228Sandrei 	uint_t eax;
28280Sstevel@tonic-gate 
28292584Ssethg 	/*
28302584Ssethg 	 * Bail out if this CPU isn't an AMD CPU, or if it's
28312584Ssethg 	 * a legacy (32-bit) AMD CPU.
28322584Ssethg 	 */
28332584Ssethg 	if (cpi->cpi_vendor != X86_VENDOR_AMD ||
28344265Skchow 	    cpi->cpi_family == 4 || cpi->cpi_family == 5 ||
28354265Skchow 	    cpi->cpi_family == 6)
28362869Sgavinm 
28370Sstevel@tonic-gate 		return (0);
28380Sstevel@tonic-gate 
28390Sstevel@tonic-gate 	eax = cpi->cpi_std[1].cp_eax;
28400Sstevel@tonic-gate 
28410Sstevel@tonic-gate #define	SH_B0(eax)	(eax == 0xf40 || eax == 0xf50)
28420Sstevel@tonic-gate #define	SH_B3(eax) 	(eax == 0xf51)
28431582Skchow #define	B(eax)		(SH_B0(eax) || SH_B3(eax))
28440Sstevel@tonic-gate 
28450Sstevel@tonic-gate #define	SH_C0(eax)	(eax == 0xf48 || eax == 0xf58)
28460Sstevel@tonic-gate 
28470Sstevel@tonic-gate #define	SH_CG(eax)	(eax == 0xf4a || eax == 0xf5a || eax == 0xf7a)
28480Sstevel@tonic-gate #define	DH_CG(eax)	(eax == 0xfc0 || eax == 0xfe0 || eax == 0xff0)
28490Sstevel@tonic-gate #define	CH_CG(eax)	(eax == 0xf82 || eax == 0xfb2)
28501582Skchow #define	CG(eax)		(SH_CG(eax) || DH_CG(eax) || CH_CG(eax))
28510Sstevel@tonic-gate 
28520Sstevel@tonic-gate #define	SH_D0(eax)	(eax == 0x10f40 || eax == 0x10f50 || eax == 0x10f70)
28530Sstevel@tonic-gate #define	DH_D0(eax)	(eax == 0x10fc0 || eax == 0x10ff0)
28540Sstevel@tonic-gate #define	CH_D0(eax)	(eax == 0x10f80 || eax == 0x10fb0)
28551582Skchow #define	D0(eax)		(SH_D0(eax) || DH_D0(eax) || CH_D0(eax))
28560Sstevel@tonic-gate 
28570Sstevel@tonic-gate #define	SH_E0(eax)	(eax == 0x20f50 || eax == 0x20f40 || eax == 0x20f70)
28580Sstevel@tonic-gate #define	JH_E1(eax)	(eax == 0x20f10)	/* JH8_E0 had 0x20f30 */
28590Sstevel@tonic-gate #define	DH_E3(eax)	(eax == 0x20fc0 || eax == 0x20ff0)
28600Sstevel@tonic-gate #define	SH_E4(eax)	(eax == 0x20f51 || eax == 0x20f71)
28610Sstevel@tonic-gate #define	BH_E4(eax)	(eax == 0x20fb1)
28620Sstevel@tonic-gate #define	SH_E5(eax)	(eax == 0x20f42)
28630Sstevel@tonic-gate #define	DH_E6(eax)	(eax == 0x20ff2 || eax == 0x20fc2)
28640Sstevel@tonic-gate #define	JH_E6(eax)	(eax == 0x20f12 || eax == 0x20f32)
28651582Skchow #define	EX(eax)		(SH_E0(eax) || JH_E1(eax) || DH_E3(eax) || \
28661582Skchow 			    SH_E4(eax) || BH_E4(eax) || SH_E5(eax) || \
28671582Skchow 			    DH_E6(eax) || JH_E6(eax))
28680Sstevel@tonic-gate 
28696691Skchow #define	DR_AX(eax)	(eax == 0x100f00 || eax == 0x100f01 || eax == 0x100f02)
28706691Skchow #define	DR_B0(eax)	(eax == 0x100f20)
28716691Skchow #define	DR_B1(eax)	(eax == 0x100f21)
28726691Skchow #define	DR_BA(eax)	(eax == 0x100f2a)
28736691Skchow #define	DR_B2(eax)	(eax == 0x100f22)
28746691Skchow #define	DR_B3(eax)	(eax == 0x100f23)
28756691Skchow #define	RB_C0(eax)	(eax == 0x100f40)
28766691Skchow 
28770Sstevel@tonic-gate 	switch (erratum) {
28780Sstevel@tonic-gate 	case 1:
28794265Skchow 		return (cpi->cpi_family < 0x10);
28800Sstevel@tonic-gate 	case 51:	/* what does the asterisk mean? */
28810Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax));
28820Sstevel@tonic-gate 	case 52:
28830Sstevel@tonic-gate 		return (B(eax));
28840Sstevel@tonic-gate 	case 57:
28856691Skchow 		return (cpi->cpi_family <= 0x11);
28860Sstevel@tonic-gate 	case 58:
28870Sstevel@tonic-gate 		return (B(eax));
28880Sstevel@tonic-gate 	case 60:
28896691Skchow 		return (cpi->cpi_family <= 0x11);
28900Sstevel@tonic-gate 	case 61:
28910Sstevel@tonic-gate 	case 62:
28920Sstevel@tonic-gate 	case 63:
28930Sstevel@tonic-gate 	case 64:
28940Sstevel@tonic-gate 	case 65:
28950Sstevel@tonic-gate 	case 66:
28960Sstevel@tonic-gate 	case 68:
28970Sstevel@tonic-gate 	case 69:
28980Sstevel@tonic-gate 	case 70:
28990Sstevel@tonic-gate 	case 71:
29000Sstevel@tonic-gate 		return (B(eax));
29010Sstevel@tonic-gate 	case 72:
29020Sstevel@tonic-gate 		return (SH_B0(eax));
29030Sstevel@tonic-gate 	case 74:
29040Sstevel@tonic-gate 		return (B(eax));
29050Sstevel@tonic-gate 	case 75:
29064265Skchow 		return (cpi->cpi_family < 0x10);
29070Sstevel@tonic-gate 	case 76:
29080Sstevel@tonic-gate 		return (B(eax));
29090Sstevel@tonic-gate 	case 77:
29106691Skchow 		return (cpi->cpi_family <= 0x11);
29110Sstevel@tonic-gate 	case 78:
29120Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax));
29130Sstevel@tonic-gate 	case 79:
29140Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax) || EX(eax));
29150Sstevel@tonic-gate 	case 80:
29160Sstevel@tonic-gate 	case 81:
29170Sstevel@tonic-gate 	case 82:
29180Sstevel@tonic-gate 		return (B(eax));
29190Sstevel@tonic-gate 	case 83:
29200Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax));
29210Sstevel@tonic-gate 	case 85:
29224265Skchow 		return (cpi->cpi_family < 0x10);
29230Sstevel@tonic-gate 	case 86:
29240Sstevel@tonic-gate 		return (SH_C0(eax) || CG(eax));
29250Sstevel@tonic-gate 	case 88:
29260Sstevel@tonic-gate #if !defined(__amd64)
29270Sstevel@tonic-gate 		return (0);
29280Sstevel@tonic-gate #else
29290Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax));
29300Sstevel@tonic-gate #endif
29310Sstevel@tonic-gate 	case 89:
29324265Skchow 		return (cpi->cpi_family < 0x10);
29330Sstevel@tonic-gate 	case 90:
29340Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax));
29350Sstevel@tonic-gate 	case 91:
29360Sstevel@tonic-gate 	case 92:
29370Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax));
29380Sstevel@tonic-gate 	case 93:
29390Sstevel@tonic-gate 		return (SH_C0(eax));
29400Sstevel@tonic-gate 	case 94:
29410Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax));
29420Sstevel@tonic-gate 	case 95:
29430Sstevel@tonic-gate #if !defined(__amd64)
29440Sstevel@tonic-gate 		return (0);
29450Sstevel@tonic-gate #else
29460Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax));
29470Sstevel@tonic-gate #endif
29480Sstevel@tonic-gate 	case 96:
29490Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax));
29500Sstevel@tonic-gate 	case 97:
29510Sstevel@tonic-gate 	case 98:
29520Sstevel@tonic-gate 		return (SH_C0(eax) || CG(eax));
29530Sstevel@tonic-gate 	case 99:
29540Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax));
29550Sstevel@tonic-gate 	case 100:
29560Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax));
29570Sstevel@tonic-gate 	case 101:
29580Sstevel@tonic-gate 	case 103:
29590Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax));
29600Sstevel@tonic-gate 	case 104:
29610Sstevel@tonic-gate 		return (SH_C0(eax) || CG(eax) || D0(eax));
29620Sstevel@tonic-gate 	case 105:
29630Sstevel@tonic-gate 	case 106:
29640Sstevel@tonic-gate 	case 107:
29650Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax));
29660Sstevel@tonic-gate 	case 108:
29670Sstevel@tonic-gate 		return (DH_CG(eax));
29680Sstevel@tonic-gate 	case 109:
29690Sstevel@tonic-gate 		return (SH_C0(eax) || CG(eax) || D0(eax));
29700Sstevel@tonic-gate 	case 110:
29710Sstevel@tonic-gate 		return (D0(eax) || EX(eax));
29720Sstevel@tonic-gate 	case 111:
29730Sstevel@tonic-gate 		return (CG(eax));
29740Sstevel@tonic-gate 	case 112:
29750Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax) || EX(eax));
29760Sstevel@tonic-gate 	case 113:
29770Sstevel@tonic-gate 		return (eax == 0x20fc0);
29780Sstevel@tonic-gate 	case 114:
29790Sstevel@tonic-gate 		return (SH_E0(eax) || JH_E1(eax) || DH_E3(eax));
29800Sstevel@tonic-gate 	case 115:
29810Sstevel@tonic-gate 		return (SH_E0(eax) || JH_E1(eax));
29820Sstevel@tonic-gate 	case 116:
29830Sstevel@tonic-gate 		return (SH_E0(eax) || JH_E1(eax) || DH_E3(eax));
29840Sstevel@tonic-gate 	case 117:
29850Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax));
29860Sstevel@tonic-gate 	case 118:
29870Sstevel@tonic-gate 		return (SH_E0(eax) || JH_E1(eax) || SH_E4(eax) || BH_E4(eax) ||
29880Sstevel@tonic-gate 		    JH_E6(eax));
29890Sstevel@tonic-gate 	case 121:
29900Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax) || EX(eax));
29910Sstevel@tonic-gate 	case 122:
29926691Skchow 		return (cpi->cpi_family < 0x10 || cpi->cpi_family == 0x11);
29930Sstevel@tonic-gate 	case 123:
29940Sstevel@tonic-gate 		return (JH_E1(eax) || BH_E4(eax) || JH_E6(eax));
2995359Skucharsk 	case 131:
29964265Skchow 		return (cpi->cpi_family < 0x10);
2997938Sesaxe 	case 6336786:
2998938Sesaxe 		/*
2999938Sesaxe 		 * Test for AdvPowerMgmtInfo.TscPStateInvariant
30004265Skchow 		 * if this is a K8 family or newer processor
3001938Sesaxe 		 */
3002938Sesaxe 		if (CPI_FAMILY(cpi) == 0xf) {
30031228Sandrei 			struct cpuid_regs regs;
30041228Sandrei 			regs.cp_eax = 0x80000007;
30051228Sandrei 			(void) __cpuid_insn(&regs);
30061228Sandrei 			return (!(regs.cp_edx & 0x100));
3007938Sesaxe 		}
3008938Sesaxe 		return (0);
30091582Skchow 	case 6323525:
30101582Skchow 		return (((((eax >> 12) & 0xff00) + (eax & 0xf00)) |
30111582Skchow 		    (((eax >> 4) & 0xf) | ((eax >> 12) & 0xf0))) < 0xf40);
30121582Skchow 
30136691Skchow 	case 6671130:
30146691Skchow 		/*
30156691Skchow 		 * check for processors (pre-Shanghai) that do not provide
30166691Skchow 		 * optimal management of 1gb ptes in its tlb.
30176691Skchow 		 */
30186691Skchow 		return (cpi->cpi_family == 0x10 && cpi->cpi_model < 4);
30196691Skchow 
30206691Skchow 	case 298:
30216691Skchow 		return (DR_AX(eax) || DR_B0(eax) || DR_B1(eax) || DR_BA(eax) ||
30226691Skchow 		    DR_B2(eax) || RB_C0(eax));
30236691Skchow 
30246691Skchow 	default:
30256691Skchow 		return (-1);
30266691Skchow 
30276691Skchow 	}
30286691Skchow }
30296691Skchow 
30306691Skchow /*
30316691Skchow  * Determine if specified erratum is present via OSVW (OS Visible Workaround).
30326691Skchow  * Return 1 if erratum is present, 0 if not present and -1 if indeterminate.
30336691Skchow  */
30346691Skchow int
30356691Skchow osvw_opteron_erratum(cpu_t *cpu, uint_t erratum)
30366691Skchow {
30376691Skchow 	struct cpuid_info	*cpi;
30386691Skchow 	uint_t			osvwid;
30396691Skchow 	static int		osvwfeature = -1;
30406691Skchow 	uint64_t		osvwlength;
30416691Skchow 
30426691Skchow 
30436691Skchow 	cpi = cpu->cpu_m.mcpu_cpi;
30446691Skchow 
30456691Skchow 	/* confirm OSVW supported */
30466691Skchow 	if (osvwfeature == -1) {
30476691Skchow 		osvwfeature = cpi->cpi_extd[1].cp_ecx & CPUID_AMD_ECX_OSVW;
30486691Skchow 	} else {
30496691Skchow 		/* assert that osvw feature setting is consistent on all cpus */
30506691Skchow 		ASSERT(osvwfeature ==
30516691Skchow 		    (cpi->cpi_extd[1].cp_ecx & CPUID_AMD_ECX_OSVW));
30526691Skchow 	}
30536691Skchow 	if (!osvwfeature)
30546691Skchow 		return (-1);
30556691Skchow 
30566691Skchow 	osvwlength = rdmsr(MSR_AMD_OSVW_ID_LEN) & OSVW_ID_LEN_MASK;
30576691Skchow 
30586691Skchow 	switch (erratum) {
30596691Skchow 	case 298:	/* osvwid is 0 */
30606691Skchow 		osvwid = 0;
30616691Skchow 		if (osvwlength <= (uint64_t)osvwid) {
30626691Skchow 			/* osvwid 0 is unknown */
30636691Skchow 			return (-1);
30646691Skchow 		}
30656691Skchow 
30666691Skchow 		/*
30676691Skchow 		 * Check the OSVW STATUS MSR to determine the state
30686691Skchow 		 * of the erratum where:
30696691Skchow 		 *   0 - fixed by HW
30706691Skchow 		 *   1 - BIOS has applied the workaround when BIOS
30716691Skchow 		 *   workaround is available. (Or for other errata,
30726691Skchow 		 *   OS workaround is required.)
30736691Skchow 		 * For a value of 1, caller will confirm that the
30746691Skchow 		 * erratum 298 workaround has indeed been applied by BIOS.
30756691Skchow 		 *
30766691Skchow 		 * A 1 may be set in cpus that have a HW fix
30776691Skchow 		 * in a mixed cpu system. Regarding erratum 298:
30786691Skchow 		 *   In a multiprocessor platform, the workaround above
30796691Skchow 		 *   should be applied to all processors regardless of
30806691Skchow 		 *   silicon revision when an affected processor is
30816691Skchow 		 *   present.
30826691Skchow 		 */
30836691Skchow 
30846691Skchow 		return (rdmsr(MSR_AMD_OSVW_STATUS +
30856691Skchow 		    (osvwid / OSVW_ID_CNT_PER_MSR)) &
30866691Skchow 		    (1ULL << (osvwid % OSVW_ID_CNT_PER_MSR)));
30876691Skchow 
30880Sstevel@tonic-gate 	default:
30890Sstevel@tonic-gate 		return (-1);
30900Sstevel@tonic-gate 	}
30910Sstevel@tonic-gate }
30920Sstevel@tonic-gate 
30930Sstevel@tonic-gate static const char assoc_str[] = "associativity";
30940Sstevel@tonic-gate static const char line_str[] = "line-size";
30950Sstevel@tonic-gate static const char size_str[] = "size";
30960Sstevel@tonic-gate 
30970Sstevel@tonic-gate static void
30980Sstevel@tonic-gate add_cache_prop(dev_info_t *devi, const char *label, const char *type,
30990Sstevel@tonic-gate     uint32_t val)
31000Sstevel@tonic-gate {
31010Sstevel@tonic-gate 	char buf[128];
31020Sstevel@tonic-gate 
31030Sstevel@tonic-gate 	/*
31040Sstevel@tonic-gate 	 * ndi_prop_update_int() is used because it is desirable for
31050Sstevel@tonic-gate 	 * DDI_PROP_HW_DEF and DDI_PROP_DONTSLEEP to be set.
31060Sstevel@tonic-gate 	 */
31070Sstevel@tonic-gate 	if (snprintf(buf, sizeof (buf), "%s-%s", label, type) < sizeof (buf))
31080Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, devi, buf, val);
31090Sstevel@tonic-gate }
31100Sstevel@tonic-gate 
31110Sstevel@tonic-gate /*
31120Sstevel@tonic-gate  * Intel-style cache/tlb description
31130Sstevel@tonic-gate  *
31140Sstevel@tonic-gate  * Standard cpuid level 2 gives a randomly ordered
31150Sstevel@tonic-gate  * selection of tags that index into a table that describes
31160Sstevel@tonic-gate  * cache and tlb properties.
31170Sstevel@tonic-gate  */
31180Sstevel@tonic-gate 
31190Sstevel@tonic-gate static const char l1_icache_str[] = "l1-icache";
31200Sstevel@tonic-gate static const char l1_dcache_str[] = "l1-dcache";
31210Sstevel@tonic-gate static const char l2_cache_str[] = "l2-cache";
31223446Smrj static const char l3_cache_str[] = "l3-cache";
31230Sstevel@tonic-gate static const char itlb4k_str[] = "itlb-4K";
31240Sstevel@tonic-gate static const char dtlb4k_str[] = "dtlb-4K";
31256964Svd224797 static const char itlb2M_str[] = "itlb-2M";
31260Sstevel@tonic-gate static const char itlb4M_str[] = "itlb-4M";
31270Sstevel@tonic-gate static const char dtlb4M_str[] = "dtlb-4M";
31286334Sksadhukh static const char dtlb24_str[] = "dtlb0-2M-4M";
31290Sstevel@tonic-gate static const char itlb424_str[] = "itlb-4K-2M-4M";
31306334Sksadhukh static const char itlb24_str[] = "itlb-2M-4M";
31310Sstevel@tonic-gate static const char dtlb44_str[] = "dtlb-4K-4M";
31320Sstevel@tonic-gate static const char sl1_dcache_str[] = "sectored-l1-dcache";
31330Sstevel@tonic-gate static const char sl2_cache_str[] = "sectored-l2-cache";
31340Sstevel@tonic-gate static const char itrace_str[] = "itrace-cache";
31350Sstevel@tonic-gate static const char sl3_cache_str[] = "sectored-l3-cache";
31366334Sksadhukh static const char sh_l2_tlb4k_str[] = "shared-l2-tlb-4k";
31370Sstevel@tonic-gate 
31380Sstevel@tonic-gate static const struct cachetab {
31390Sstevel@tonic-gate 	uint8_t 	ct_code;
31400Sstevel@tonic-gate 	uint8_t		ct_assoc;
31410Sstevel@tonic-gate 	uint16_t 	ct_line_size;
31420Sstevel@tonic-gate 	size_t		ct_size;
31430Sstevel@tonic-gate 	const char	*ct_label;
31440Sstevel@tonic-gate } intel_ctab[] = {
31456964Svd224797 	/*
31466964Svd224797 	 * maintain descending order!
31476964Svd224797 	 *
31486964Svd224797 	 * Codes ignored - Reason
31496964Svd224797 	 * ----------------------
31506964Svd224797 	 * 40H - intel_cpuid_4_cache_info() disambiguates l2/l3 cache
31516964Svd224797 	 * f0H/f1H - Currently we do not interpret prefetch size by design
31526964Svd224797 	 */
31536334Sksadhukh 	{ 0xe4, 16, 64, 8*1024*1024, l3_cache_str},
31546334Sksadhukh 	{ 0xe3, 16, 64, 4*1024*1024, l3_cache_str},
31556334Sksadhukh 	{ 0xe2, 16, 64, 2*1024*1024, l3_cache_str},
31566334Sksadhukh 	{ 0xde, 12, 64, 6*1024*1024, l3_cache_str},
31576334Sksadhukh 	{ 0xdd, 12, 64, 3*1024*1024, l3_cache_str},
31586334Sksadhukh 	{ 0xdc, 12, 64, ((1*1024*1024)+(512*1024)), l3_cache_str},
31596334Sksadhukh 	{ 0xd8, 8, 64, 4*1024*1024, l3_cache_str},
31606334Sksadhukh 	{ 0xd7, 8, 64, 2*1024*1024, l3_cache_str},
31616334Sksadhukh 	{ 0xd6, 8, 64, 1*1024*1024, l3_cache_str},
31626334Sksadhukh 	{ 0xd2, 4, 64, 2*1024*1024, l3_cache_str},
31636334Sksadhukh 	{ 0xd1, 4, 64, 1*1024*1024, l3_cache_str},
31646334Sksadhukh 	{ 0xd0, 4, 64, 512*1024, l3_cache_str},
31656334Sksadhukh 	{ 0xca, 4, 0, 512, sh_l2_tlb4k_str},
31666964Svd224797 	{ 0xc0, 4, 0, 8, dtlb44_str },
31676964Svd224797 	{ 0xba, 4, 0, 64, dtlb4k_str },
31683446Smrj 	{ 0xb4, 4, 0, 256, dtlb4k_str },
31690Sstevel@tonic-gate 	{ 0xb3, 4, 0, 128, dtlb4k_str },
31706334Sksadhukh 	{ 0xb2, 4, 0, 64, itlb4k_str },
31710Sstevel@tonic-gate 	{ 0xb0, 4, 0, 128, itlb4k_str },
31720Sstevel@tonic-gate 	{ 0x87, 8, 64, 1024*1024, l2_cache_str},
31730Sstevel@tonic-gate 	{ 0x86, 4, 64, 512*1024, l2_cache_str},
31740Sstevel@tonic-gate 	{ 0x85, 8, 32, 2*1024*1024, l2_cache_str},
31750Sstevel@tonic-gate 	{ 0x84, 8, 32, 1024*1024, l2_cache_str},
31760Sstevel@tonic-gate 	{ 0x83, 8, 32, 512*1024, l2_cache_str},
31770Sstevel@tonic-gate 	{ 0x82, 8, 32, 256*1024, l2_cache_str},
31786964Svd224797 	{ 0x80, 8, 64, 512*1024, l2_cache_str},
31790Sstevel@tonic-gate 	{ 0x7f, 2, 64, 512*1024, l2_cache_str},
31800Sstevel@tonic-gate 	{ 0x7d, 8, 64, 2*1024*1024, sl2_cache_str},
31810Sstevel@tonic-gate 	{ 0x7c, 8, 64, 1024*1024, sl2_cache_str},
31820Sstevel@tonic-gate 	{ 0x7b, 8, 64, 512*1024, sl2_cache_str},
31830Sstevel@tonic-gate 	{ 0x7a, 8, 64, 256*1024, sl2_cache_str},
31840Sstevel@tonic-gate 	{ 0x79, 8, 64, 128*1024, sl2_cache_str},
31850Sstevel@tonic-gate 	{ 0x78, 8, 64, 1024*1024, l2_cache_str},
31863446Smrj 	{ 0x73, 8, 0, 64*1024, itrace_str},
31870Sstevel@tonic-gate 	{ 0x72, 8, 0, 32*1024, itrace_str},
31880Sstevel@tonic-gate 	{ 0x71, 8, 0, 16*1024, itrace_str},
31890Sstevel@tonic-gate 	{ 0x70, 8, 0, 12*1024, itrace_str},
31900Sstevel@tonic-gate 	{ 0x68, 4, 64, 32*1024, sl1_dcache_str},
31910Sstevel@tonic-gate 	{ 0x67, 4, 64, 16*1024, sl1_dcache_str},
31920Sstevel@tonic-gate 	{ 0x66, 4, 64, 8*1024, sl1_dcache_str},
31930Sstevel@tonic-gate 	{ 0x60, 8, 64, 16*1024, sl1_dcache_str},
31940Sstevel@tonic-gate 	{ 0x5d, 0, 0, 256, dtlb44_str},
31950Sstevel@tonic-gate 	{ 0x5c, 0, 0, 128, dtlb44_str},
31960Sstevel@tonic-gate 	{ 0x5b, 0, 0, 64, dtlb44_str},
31976334Sksadhukh 	{ 0x5a, 4, 0, 32, dtlb24_str},
31986964Svd224797 	{ 0x59, 0, 0, 16, dtlb4k_str},
31996964Svd224797 	{ 0x57, 4, 0, 16, dtlb4k_str},
32006964Svd224797 	{ 0x56, 4, 0, 16, dtlb4M_str},
32016334Sksadhukh 	{ 0x55, 0, 0, 7, itlb24_str},
32020Sstevel@tonic-gate 	{ 0x52, 0, 0, 256, itlb424_str},
32030Sstevel@tonic-gate 	{ 0x51, 0, 0, 128, itlb424_str},
32040Sstevel@tonic-gate 	{ 0x50, 0, 0, 64, itlb424_str},
32056964Svd224797 	{ 0x4f, 0, 0, 32, itlb4k_str},
32066964Svd224797 	{ 0x4e, 24, 64, 6*1024*1024, l2_cache_str},
32073446Smrj 	{ 0x4d, 16, 64, 16*1024*1024, l3_cache_str},
32083446Smrj 	{ 0x4c, 12, 64, 12*1024*1024, l3_cache_str},
32093446Smrj 	{ 0x4b, 16, 64, 8*1024*1024, l3_cache_str},
32103446Smrj 	{ 0x4a, 12, 64, 6*1024*1024, l3_cache_str},
32113446Smrj 	{ 0x49, 16, 64, 4*1024*1024, l3_cache_str},
32126964Svd224797 	{ 0x48, 12, 64, 3*1024*1024, l2_cache_str},
32133446Smrj 	{ 0x47, 8, 64, 8*1024*1024, l3_cache_str},
32143446Smrj 	{ 0x46, 4, 64, 4*1024*1024, l3_cache_str},
32150Sstevel@tonic-gate 	{ 0x45, 4, 32, 2*1024*1024, l2_cache_str},
32160Sstevel@tonic-gate 	{ 0x44, 4, 32, 1024*1024, l2_cache_str},
32170Sstevel@tonic-gate 	{ 0x43, 4, 32, 512*1024, l2_cache_str},
32180Sstevel@tonic-gate 	{ 0x42, 4, 32, 256*1024, l2_cache_str},
32190Sstevel@tonic-gate 	{ 0x41, 4, 32, 128*1024, l2_cache_str},
32203446Smrj 	{ 0x3e, 4, 64, 512*1024, sl2_cache_str},
32213446Smrj 	{ 0x3d, 6, 64, 384*1024, sl2_cache_str},
32220Sstevel@tonic-gate 	{ 0x3c, 4, 64, 256*1024, sl2_cache_str},
32230Sstevel@tonic-gate 	{ 0x3b, 2, 64, 128*1024, sl2_cache_str},
32243446Smrj 	{ 0x3a, 6, 64, 192*1024, sl2_cache_str},
32250Sstevel@tonic-gate 	{ 0x39, 4, 64, 128*1024, sl2_cache_str},
32260Sstevel@tonic-gate 	{ 0x30, 8, 64, 32*1024, l1_icache_str},
32270Sstevel@tonic-gate 	{ 0x2c, 8, 64, 32*1024, l1_dcache_str},
32280Sstevel@tonic-gate 	{ 0x29, 8, 64, 4096*1024, sl3_cache_str},
32290Sstevel@tonic-gate 	{ 0x25, 8, 64, 2048*1024, sl3_cache_str},
32300Sstevel@tonic-gate 	{ 0x23, 8, 64, 1024*1024, sl3_cache_str},
32310Sstevel@tonic-gate 	{ 0x22, 4, 64, 512*1024, sl3_cache_str},
32326964Svd224797 	{ 0x0e, 6, 64, 24*1024, l1_dcache_str},
32336334Sksadhukh 	{ 0x0d, 4, 32, 16*1024, l1_dcache_str},
32340Sstevel@tonic-gate 	{ 0x0c, 4, 32, 16*1024, l1_dcache_str},
32353446Smrj 	{ 0x0b, 4, 0, 4, itlb4M_str},
32360Sstevel@tonic-gate 	{ 0x0a, 2, 32, 8*1024, l1_dcache_str},
32370Sstevel@tonic-gate 	{ 0x08, 4, 32, 16*1024, l1_icache_str},
32380Sstevel@tonic-gate 	{ 0x06, 4, 32, 8*1024, l1_icache_str},
32396964Svd224797 	{ 0x05, 4, 0, 32, dtlb4M_str},
32400Sstevel@tonic-gate 	{ 0x04, 4, 0, 8, dtlb4M_str},
32410Sstevel@tonic-gate 	{ 0x03, 4, 0, 64, dtlb4k_str},
32420Sstevel@tonic-gate 	{ 0x02, 4, 0, 2, itlb4M_str},
32430Sstevel@tonic-gate 	{ 0x01, 4, 0, 32, itlb4k_str},
32440Sstevel@tonic-gate 	{ 0 }
32450Sstevel@tonic-gate };
32460Sstevel@tonic-gate 
32470Sstevel@tonic-gate static const struct cachetab cyrix_ctab[] = {
32480Sstevel@tonic-gate 	{ 0x70, 4, 0, 32, "tlb-4K" },
32490Sstevel@tonic-gate 	{ 0x80, 4, 16, 16*1024, "l1-cache" },
32500Sstevel@tonic-gate 	{ 0 }
32510Sstevel@tonic-gate };
32520Sstevel@tonic-gate 
32530Sstevel@tonic-gate /*
32540Sstevel@tonic-gate  * Search a cache table for a matching entry
32550Sstevel@tonic-gate  */
32560Sstevel@tonic-gate static const struct cachetab *
32570Sstevel@tonic-gate find_cacheent(const struct cachetab *ct, uint_t code)
32580Sstevel@tonic-gate {
32590Sstevel@tonic-gate 	if (code != 0) {
32600Sstevel@tonic-gate 		for (; ct->ct_code != 0; ct++)
32610Sstevel@tonic-gate 			if (ct->ct_code <= code)
32620Sstevel@tonic-gate 				break;
32630Sstevel@tonic-gate 		if (ct->ct_code == code)
32640Sstevel@tonic-gate 			return (ct);
32650Sstevel@tonic-gate 	}
32660Sstevel@tonic-gate 	return (NULL);
32670Sstevel@tonic-gate }
32680Sstevel@tonic-gate 
32690Sstevel@tonic-gate /*
32705438Sksadhukh  * Populate cachetab entry with L2 or L3 cache-information using
32715438Sksadhukh  * cpuid function 4. This function is called from intel_walk_cacheinfo()
32725438Sksadhukh  * when descriptor 0x49 is encountered. It returns 0 if no such cache
32735438Sksadhukh  * information is found.
32745438Sksadhukh  */
32755438Sksadhukh static int
32765438Sksadhukh intel_cpuid_4_cache_info(struct cachetab *ct, struct cpuid_info *cpi)
32775438Sksadhukh {
32785438Sksadhukh 	uint32_t level, i;
32795438Sksadhukh 	int ret = 0;
32805438Sksadhukh 
32815438Sksadhukh 	for (i = 0; i < cpi->cpi_std_4_size; i++) {
32825438Sksadhukh 		level = CPI_CACHE_LVL(cpi->cpi_std_4[i]);
32835438Sksadhukh 
32845438Sksadhukh 		if (level == 2 || level == 3) {
32855438Sksadhukh 			ct->ct_assoc = CPI_CACHE_WAYS(cpi->cpi_std_4[i]) + 1;
32865438Sksadhukh 			ct->ct_line_size =
32875438Sksadhukh 			    CPI_CACHE_COH_LN_SZ(cpi->cpi_std_4[i]) + 1;
32885438Sksadhukh 			ct->ct_size = ct->ct_assoc *
32895438Sksadhukh 			    (CPI_CACHE_PARTS(cpi->cpi_std_4[i]) + 1) *
32905438Sksadhukh 			    ct->ct_line_size *
32915438Sksadhukh 			    (cpi->cpi_std_4[i]->cp_ecx + 1);
32925438Sksadhukh 
32935438Sksadhukh 			if (level == 2) {
32945438Sksadhukh 				ct->ct_label = l2_cache_str;
32955438Sksadhukh 			} else if (level == 3) {
32965438Sksadhukh 				ct->ct_label = l3_cache_str;
32975438Sksadhukh 			}
32985438Sksadhukh 			ret = 1;
32995438Sksadhukh 		}
33005438Sksadhukh 	}
33015438Sksadhukh 
33025438Sksadhukh 	return (ret);
33035438Sksadhukh }
33045438Sksadhukh 
33055438Sksadhukh /*
33060Sstevel@tonic-gate  * Walk the cacheinfo descriptor, applying 'func' to every valid element
33070Sstevel@tonic-gate  * The walk is terminated if the walker returns non-zero.
33080Sstevel@tonic-gate  */
33090Sstevel@tonic-gate static void
33100Sstevel@tonic-gate intel_walk_cacheinfo(struct cpuid_info *cpi,
33110Sstevel@tonic-gate     void *arg, int (*func)(void *, const struct cachetab *))
33120Sstevel@tonic-gate {
33130Sstevel@tonic-gate 	const struct cachetab *ct;
33146964Svd224797 	struct cachetab des_49_ct, des_b1_ct;
33150Sstevel@tonic-gate 	uint8_t *dp;
33160Sstevel@tonic-gate 	int i;
33170Sstevel@tonic-gate 
33180Sstevel@tonic-gate 	if ((dp = cpi->cpi_cacheinfo) == NULL)
33190Sstevel@tonic-gate 		return;
33204797Sksadhukh 	for (i = 0; i < cpi->cpi_ncache; i++, dp++) {
33214797Sksadhukh 		/*
33224797Sksadhukh 		 * For overloaded descriptor 0x49 we use cpuid function 4
33235438Sksadhukh 		 * if supported by the current processor, to create
33244797Sksadhukh 		 * cache information.
33256964Svd224797 		 * For overloaded descriptor 0xb1 we use X86_PAE flag
33266964Svd224797 		 * to disambiguate the cache information.
33274797Sksadhukh 		 */
33285438Sksadhukh 		if (*dp == 0x49 && cpi->cpi_maxeax >= 0x4 &&
33295438Sksadhukh 		    intel_cpuid_4_cache_info(&des_49_ct, cpi) == 1) {
33305438Sksadhukh 				ct = &des_49_ct;
33316964Svd224797 		} else if (*dp == 0xb1) {
33326964Svd224797 			des_b1_ct.ct_code = 0xb1;
33336964Svd224797 			des_b1_ct.ct_assoc = 4;
33346964Svd224797 			des_b1_ct.ct_line_size = 0;
33356964Svd224797 			if (x86_feature & X86_PAE) {
33366964Svd224797 				des_b1_ct.ct_size = 8;
33376964Svd224797 				des_b1_ct.ct_label = itlb2M_str;
33386964Svd224797 			} else {
33396964Svd224797 				des_b1_ct.ct_size = 4;
33406964Svd224797 				des_b1_ct.ct_label = itlb4M_str;
33416964Svd224797 			}
33426964Svd224797 			ct = &des_b1_ct;
33435438Sksadhukh 		} else {
33445438Sksadhukh 			if ((ct = find_cacheent(intel_ctab, *dp)) == NULL) {
33455438Sksadhukh 				continue;
33465438Sksadhukh 			}
33474797Sksadhukh 		}
33484797Sksadhukh 
33495438Sksadhukh 		if (func(arg, ct) != 0) {
33505438Sksadhukh 			break;
33510Sstevel@tonic-gate 		}
33524797Sksadhukh 	}
33530Sstevel@tonic-gate }
33540Sstevel@tonic-gate 
33550Sstevel@tonic-gate /*
33560Sstevel@tonic-gate  * (Like the Intel one, except for Cyrix CPUs)
33570Sstevel@tonic-gate  */
33580Sstevel@tonic-gate static void
33590Sstevel@tonic-gate cyrix_walk_cacheinfo(struct cpuid_info *cpi,
33600Sstevel@tonic-gate     void *arg, int (*func)(void *, const struct cachetab *))
33610Sstevel@tonic-gate {
33620Sstevel@tonic-gate 	const struct cachetab *ct;
33630Sstevel@tonic-gate 	uint8_t *dp;
33640Sstevel@tonic-gate 	int i;
33650Sstevel@tonic-gate 
33660Sstevel@tonic-gate 	if ((dp = cpi->cpi_cacheinfo) == NULL)
33670Sstevel@tonic-gate 		return;
33680Sstevel@tonic-gate 	for (i = 0; i < cpi->cpi_ncache; i++, dp++) {
33690Sstevel@tonic-gate 		/*
33700Sstevel@tonic-gate 		 * Search Cyrix-specific descriptor table first ..
33710Sstevel@tonic-gate 		 */
33720Sstevel@tonic-gate 		if ((ct = find_cacheent(cyrix_ctab, *dp)) != NULL) {
33730Sstevel@tonic-gate 			if (func(arg, ct) != 0)
33740Sstevel@tonic-gate 				break;
33750Sstevel@tonic-gate 			continue;
33760Sstevel@tonic-gate 		}
33770Sstevel@tonic-gate 		/*
33780Sstevel@tonic-gate 		 * .. else fall back to the Intel one
33790Sstevel@tonic-gate 		 */
33800Sstevel@tonic-gate 		if ((ct = find_cacheent(intel_ctab, *dp)) != NULL) {
33810Sstevel@tonic-gate 			if (func(arg, ct) != 0)
33820Sstevel@tonic-gate 				break;
33830Sstevel@tonic-gate 			continue;
33840Sstevel@tonic-gate 		}
33850Sstevel@tonic-gate 	}
33860Sstevel@tonic-gate }
33870Sstevel@tonic-gate 
33880Sstevel@tonic-gate /*
33890Sstevel@tonic-gate  * A cacheinfo walker that adds associativity, line-size, and size properties
33900Sstevel@tonic-gate  * to the devinfo node it is passed as an argument.
33910Sstevel@tonic-gate  */
33920Sstevel@tonic-gate static int
33930Sstevel@tonic-gate add_cacheent_props(void *arg, const struct cachetab *ct)
33940Sstevel@tonic-gate {
33950Sstevel@tonic-gate 	dev_info_t *devi = arg;
33960Sstevel@tonic-gate 
33970Sstevel@tonic-gate 	add_cache_prop(devi, ct->ct_label, assoc_str, ct->ct_assoc);
33980Sstevel@tonic-gate 	if (ct->ct_line_size != 0)
33990Sstevel@tonic-gate 		add_cache_prop(devi, ct->ct_label, line_str,
34000Sstevel@tonic-gate 		    ct->ct_line_size);
34010Sstevel@tonic-gate 	add_cache_prop(devi, ct->ct_label, size_str, ct->ct_size);
34020Sstevel@tonic-gate 	return (0);
34030Sstevel@tonic-gate }
34040Sstevel@tonic-gate 
34054797Sksadhukh 
34060Sstevel@tonic-gate static const char fully_assoc[] = "fully-associative?";
34070Sstevel@tonic-gate 
34080Sstevel@tonic-gate /*
34090Sstevel@tonic-gate  * AMD style cache/tlb description
34100Sstevel@tonic-gate  *
34110Sstevel@tonic-gate  * Extended functions 5 and 6 directly describe properties of
34120Sstevel@tonic-gate  * tlbs and various cache levels.
34130Sstevel@tonic-gate  */
34140Sstevel@tonic-gate static void
34150Sstevel@tonic-gate add_amd_assoc(dev_info_t *devi, const char *label, uint_t assoc)
34160Sstevel@tonic-gate {
34170Sstevel@tonic-gate 	switch (assoc) {
34180Sstevel@tonic-gate 	case 0:	/* reserved; ignore */
34190Sstevel@tonic-gate 		break;
34200Sstevel@tonic-gate 	default:
34210Sstevel@tonic-gate 		add_cache_prop(devi, label, assoc_str, assoc);
34220Sstevel@tonic-gate 		break;
34230Sstevel@tonic-gate 	case 0xff:
34240Sstevel@tonic-gate 		add_cache_prop(devi, label, fully_assoc, 1);
34250Sstevel@tonic-gate 		break;
34260Sstevel@tonic-gate 	}
34270Sstevel@tonic-gate }
34280Sstevel@tonic-gate 
34290Sstevel@tonic-gate static void
34300Sstevel@tonic-gate add_amd_tlb(dev_info_t *devi, const char *label, uint_t assoc, uint_t size)
34310Sstevel@tonic-gate {
34320Sstevel@tonic-gate 	if (size == 0)
34330Sstevel@tonic-gate 		return;
34340Sstevel@tonic-gate 	add_cache_prop(devi, label, size_str, size);
34350Sstevel@tonic-gate 	add_amd_assoc(devi, label, assoc);
34360Sstevel@tonic-gate }
34370Sstevel@tonic-gate 
34380Sstevel@tonic-gate static void
34390Sstevel@tonic-gate add_amd_cache(dev_info_t *devi, const char *label,
34400Sstevel@tonic-gate     uint_t size, uint_t assoc, uint_t lines_per_tag, uint_t line_size)
34410Sstevel@tonic-gate {
34420Sstevel@tonic-gate 	if (size == 0 || line_size == 0)
34430Sstevel@tonic-gate 		return;
34440Sstevel@tonic-gate 	add_amd_assoc(devi, label, assoc);
34450Sstevel@tonic-gate 	/*
34460Sstevel@tonic-gate 	 * Most AMD parts have a sectored cache. Multiple cache lines are
34470Sstevel@tonic-gate 	 * associated with each tag. A sector consists of all cache lines
34480Sstevel@tonic-gate 	 * associated with a tag. For example, the AMD K6-III has a sector
34490Sstevel@tonic-gate 	 * size of 2 cache lines per tag.
34500Sstevel@tonic-gate 	 */
34510Sstevel@tonic-gate 	if (lines_per_tag != 0)
34520Sstevel@tonic-gate 		add_cache_prop(devi, label, "lines-per-tag", lines_per_tag);
34530Sstevel@tonic-gate 	add_cache_prop(devi, label, line_str, line_size);
34540Sstevel@tonic-gate 	add_cache_prop(devi, label, size_str, size * 1024);
34550Sstevel@tonic-gate }
34560Sstevel@tonic-gate 
34570Sstevel@tonic-gate static void
34580Sstevel@tonic-gate add_amd_l2_assoc(dev_info_t *devi, const char *label, uint_t assoc)
34590Sstevel@tonic-gate {
34600Sstevel@tonic-gate 	switch (assoc) {
34610Sstevel@tonic-gate 	case 0:	/* off */
34620Sstevel@tonic-gate 		break;
34630Sstevel@tonic-gate 	case 1:
34640Sstevel@tonic-gate 	case 2:
34650Sstevel@tonic-gate 	case 4:
34660Sstevel@tonic-gate 		add_cache_prop(devi, label, assoc_str, assoc);
34670Sstevel@tonic-gate 		break;
34680Sstevel@tonic-gate 	case 6:
34690Sstevel@tonic-gate 		add_cache_prop(devi, label, assoc_str, 8);
34700Sstevel@tonic-gate 		break;
34710Sstevel@tonic-gate 	case 8:
34720Sstevel@tonic-gate 		add_cache_prop(devi, label, assoc_str, 16);
34730Sstevel@tonic-gate 		break;
34740Sstevel@tonic-gate 	case 0xf:
34750Sstevel@tonic-gate 		add_cache_prop(devi, label, fully_assoc, 1);
34760Sstevel@tonic-gate 		break;
34770Sstevel@tonic-gate 	default: /* reserved; ignore */
34780Sstevel@tonic-gate 		break;
34790Sstevel@tonic-gate 	}
34800Sstevel@tonic-gate }
34810Sstevel@tonic-gate 
34820Sstevel@tonic-gate static void
34830Sstevel@tonic-gate add_amd_l2_tlb(dev_info_t *devi, const char *label, uint_t assoc, uint_t size)
34840Sstevel@tonic-gate {
34850Sstevel@tonic-gate 	if (size == 0 || assoc == 0)
34860Sstevel@tonic-gate 		return;
34870Sstevel@tonic-gate 	add_amd_l2_assoc(devi, label, assoc);
34880Sstevel@tonic-gate 	add_cache_prop(devi, label, size_str, size);
34890Sstevel@tonic-gate }
34900Sstevel@tonic-gate 
34910Sstevel@tonic-gate static void
34920Sstevel@tonic-gate add_amd_l2_cache(dev_info_t *devi, const char *label,
34930Sstevel@tonic-gate     uint_t size, uint_t assoc, uint_t lines_per_tag, uint_t line_size)
34940Sstevel@tonic-gate {
34950Sstevel@tonic-gate 	if (size == 0 || assoc == 0 || line_size == 0)
34960Sstevel@tonic-gate 		return;
34970Sstevel@tonic-gate 	add_amd_l2_assoc(devi, label, assoc);
34980Sstevel@tonic-gate 	if (lines_per_tag != 0)
34990Sstevel@tonic-gate 		add_cache_prop(devi, label, "lines-per-tag", lines_per_tag);
35000Sstevel@tonic-gate 	add_cache_prop(devi, label, line_str, line_size);
35010Sstevel@tonic-gate 	add_cache_prop(devi, label, size_str, size * 1024);
35020Sstevel@tonic-gate }
35030Sstevel@tonic-gate 
35040Sstevel@tonic-gate static void
35050Sstevel@tonic-gate amd_cache_info(struct cpuid_info *cpi, dev_info_t *devi)
35060Sstevel@tonic-gate {
35071228Sandrei 	struct cpuid_regs *cp;
35080Sstevel@tonic-gate 
35090Sstevel@tonic-gate 	if (cpi->cpi_xmaxeax < 0x80000005)
35100Sstevel@tonic-gate 		return;
35110Sstevel@tonic-gate 	cp = &cpi->cpi_extd[5];
35120Sstevel@tonic-gate 
35130Sstevel@tonic-gate 	/*
35140Sstevel@tonic-gate 	 * 4M/2M L1 TLB configuration
35150Sstevel@tonic-gate 	 *
35160Sstevel@tonic-gate 	 * We report the size for 2M pages because AMD uses two
35170Sstevel@tonic-gate 	 * TLB entries for one 4M page.
35180Sstevel@tonic-gate 	 */
35190Sstevel@tonic-gate 	add_amd_tlb(devi, "dtlb-2M",
35200Sstevel@tonic-gate 	    BITX(cp->cp_eax, 31, 24), BITX(cp->cp_eax, 23, 16));
35210Sstevel@tonic-gate 	add_amd_tlb(devi, "itlb-2M",
35220Sstevel@tonic-gate 	    BITX(cp->cp_eax, 15, 8), BITX(cp->cp_eax, 7, 0));
35230Sstevel@tonic-gate 
35240Sstevel@tonic-gate 	/*
35250Sstevel@tonic-gate 	 * 4K L1 TLB configuration
35260Sstevel@tonic-gate 	 */
35270Sstevel@tonic-gate 
35280Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
35290Sstevel@tonic-gate 		uint_t nentries;
35300Sstevel@tonic-gate 	case X86_VENDOR_TM:
35310Sstevel@tonic-gate 		if (cpi->cpi_family >= 5) {
35320Sstevel@tonic-gate 			/*
35330Sstevel@tonic-gate 			 * Crusoe processors have 256 TLB entries, but
35340Sstevel@tonic-gate 			 * cpuid data format constrains them to only
35350Sstevel@tonic-gate 			 * reporting 255 of them.
35360Sstevel@tonic-gate 			 */
35370Sstevel@tonic-gate 			if ((nentries = BITX(cp->cp_ebx, 23, 16)) == 255)
35380Sstevel@tonic-gate 				nentries = 256;
35390Sstevel@tonic-gate 			/*
35400Sstevel@tonic-gate 			 * Crusoe processors also have a unified TLB
35410Sstevel@tonic-gate 			 */
35420Sstevel@tonic-gate 			add_amd_tlb(devi, "tlb-4K", BITX(cp->cp_ebx, 31, 24),
35430Sstevel@tonic-gate 			    nentries);
35440Sstevel@tonic-gate 			break;
35450Sstevel@tonic-gate 		}
35460Sstevel@tonic-gate 		/*FALLTHROUGH*/
35470Sstevel@tonic-gate 	default:
35480Sstevel@tonic-gate 		add_amd_tlb(devi, itlb4k_str,
35490Sstevel@tonic-gate 		    BITX(cp->cp_ebx, 31, 24), BITX(cp->cp_ebx, 23, 16));
35500Sstevel@tonic-gate 		add_amd_tlb(devi, dtlb4k_str,
35510Sstevel@tonic-gate 		    BITX(cp->cp_ebx, 15, 8), BITX(cp->cp_ebx, 7, 0));
35520Sstevel@tonic-gate 		break;
35530Sstevel@tonic-gate 	}
35540Sstevel@tonic-gate 
35550Sstevel@tonic-gate 	/*
35560Sstevel@tonic-gate 	 * data L1 cache configuration
35570Sstevel@tonic-gate 	 */
35580Sstevel@tonic-gate 
35590Sstevel@tonic-gate 	add_amd_cache(devi, l1_dcache_str,
35600Sstevel@tonic-gate 	    BITX(cp->cp_ecx, 31, 24), BITX(cp->cp_ecx, 23, 16),
35610Sstevel@tonic-gate 	    BITX(cp->cp_ecx, 15, 8), BITX(cp->cp_ecx, 7, 0));
35620Sstevel@tonic-gate 
35630Sstevel@tonic-gate 	/*
35640Sstevel@tonic-gate 	 * code L1 cache configuration
35650Sstevel@tonic-gate 	 */
35660Sstevel@tonic-gate 
35670Sstevel@tonic-gate 	add_amd_cache(devi, l1_icache_str,
35680Sstevel@tonic-gate 	    BITX(cp->cp_edx, 31, 24), BITX(cp->cp_edx, 23, 16),
35690Sstevel@tonic-gate 	    BITX(cp->cp_edx, 15, 8), BITX(cp->cp_edx, 7, 0));
35700Sstevel@tonic-gate 
35710Sstevel@tonic-gate 	if (cpi->cpi_xmaxeax < 0x80000006)
35720Sstevel@tonic-gate 		return;
35730Sstevel@tonic-gate 	cp = &cpi->cpi_extd[6];
35740Sstevel@tonic-gate 
35750Sstevel@tonic-gate 	/* Check for a unified L2 TLB for large pages */
35760Sstevel@tonic-gate 
35770Sstevel@tonic-gate 	if (BITX(cp->cp_eax, 31, 16) == 0)
35780Sstevel@tonic-gate 		add_amd_l2_tlb(devi, "l2-tlb-2M",
35790Sstevel@tonic-gate 		    BITX(cp->cp_eax, 15, 12), BITX(cp->cp_eax, 11, 0));
35800Sstevel@tonic-gate 	else {
35810Sstevel@tonic-gate 		add_amd_l2_tlb(devi, "l2-dtlb-2M",
35820Sstevel@tonic-gate 		    BITX(cp->cp_eax, 31, 28), BITX(cp->cp_eax, 27, 16));
35830Sstevel@tonic-gate 		add_amd_l2_tlb(devi, "l2-itlb-2M",
35840Sstevel@tonic-gate 		    BITX(cp->cp_eax, 15, 12), BITX(cp->cp_eax, 11, 0));
35850Sstevel@tonic-gate 	}
35860Sstevel@tonic-gate 
35870Sstevel@tonic-gate 	/* Check for a unified L2 TLB for 4K pages */
35880Sstevel@tonic-gate 
35890Sstevel@tonic-gate 	if (BITX(cp->cp_ebx, 31, 16) == 0) {
35900Sstevel@tonic-gate 		add_amd_l2_tlb(devi, "l2-tlb-4K",
35910Sstevel@tonic-gate 		    BITX(cp->cp_eax, 15, 12), BITX(cp->cp_eax, 11, 0));
35920Sstevel@tonic-gate 	} else {
35930Sstevel@tonic-gate 		add_amd_l2_tlb(devi, "l2-dtlb-4K",
35940Sstevel@tonic-gate 		    BITX(cp->cp_eax, 31, 28), BITX(cp->cp_eax, 27, 16));
35950Sstevel@tonic-gate 		add_amd_l2_tlb(devi, "l2-itlb-4K",
35960Sstevel@tonic-gate 		    BITX(cp->cp_eax, 15, 12), BITX(cp->cp_eax, 11, 0));
35970Sstevel@tonic-gate 	}
35980Sstevel@tonic-gate 
35990Sstevel@tonic-gate 	add_amd_l2_cache(devi, l2_cache_str,
36000Sstevel@tonic-gate 	    BITX(cp->cp_ecx, 31, 16), BITX(cp->cp_ecx, 15, 12),
36010Sstevel@tonic-gate 	    BITX(cp->cp_ecx, 11, 8), BITX(cp->cp_ecx, 7, 0));
36020Sstevel@tonic-gate }
36030Sstevel@tonic-gate 
36040Sstevel@tonic-gate /*
36050Sstevel@tonic-gate  * There are two basic ways that the x86 world describes it cache
36060Sstevel@tonic-gate  * and tlb architecture - Intel's way and AMD's way.
36070Sstevel@tonic-gate  *
36080Sstevel@tonic-gate  * Return which flavor of cache architecture we should use
36090Sstevel@tonic-gate  */
36100Sstevel@tonic-gate static int
36110Sstevel@tonic-gate x86_which_cacheinfo(struct cpuid_info *cpi)
36120Sstevel@tonic-gate {
36130Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
36140Sstevel@tonic-gate 	case X86_VENDOR_Intel:
36150Sstevel@tonic-gate 		if (cpi->cpi_maxeax >= 2)
36160Sstevel@tonic-gate 			return (X86_VENDOR_Intel);
36170Sstevel@tonic-gate 		break;
36180Sstevel@tonic-gate 	case X86_VENDOR_AMD:
36190Sstevel@tonic-gate 		/*
36200Sstevel@tonic-gate 		 * The K5 model 1 was the first part from AMD that reported
36210Sstevel@tonic-gate 		 * cache sizes via extended cpuid functions.
36220Sstevel@tonic-gate 		 */
36230Sstevel@tonic-gate 		if (cpi->cpi_family > 5 ||
36240Sstevel@tonic-gate 		    (cpi->cpi_family == 5 && cpi->cpi_model >= 1))
36250Sstevel@tonic-gate 			return (X86_VENDOR_AMD);
36260Sstevel@tonic-gate 		break;
36270Sstevel@tonic-gate 	case X86_VENDOR_TM:
36280Sstevel@tonic-gate 		if (cpi->cpi_family >= 5)
36290Sstevel@tonic-gate 			return (X86_VENDOR_AMD);
36300Sstevel@tonic-gate 		/*FALLTHROUGH*/
36310Sstevel@tonic-gate 	default:
36320Sstevel@tonic-gate 		/*
36330Sstevel@tonic-gate 		 * If they have extended CPU data for 0x80000005
36340Sstevel@tonic-gate 		 * then we assume they have AMD-format cache
36350Sstevel@tonic-gate 		 * information.
36360Sstevel@tonic-gate 		 *
36370Sstevel@tonic-gate 		 * If not, and the vendor happens to be Cyrix,
36380Sstevel@tonic-gate 		 * then try our-Cyrix specific handler.
36390Sstevel@tonic-gate 		 *
36400Sstevel@tonic-gate 		 * If we're not Cyrix, then assume we're using Intel's
36410Sstevel@tonic-gate 		 * table-driven format instead.
36420Sstevel@tonic-gate 		 */
36430Sstevel@tonic-gate 		if (cpi->cpi_xmaxeax >= 0x80000005)
36440Sstevel@tonic-gate 			return (X86_VENDOR_AMD);
36450Sstevel@tonic-gate 		else if (cpi->cpi_vendor == X86_VENDOR_Cyrix)
36460Sstevel@tonic-gate 			return (X86_VENDOR_Cyrix);
36470Sstevel@tonic-gate 		else if (cpi->cpi_maxeax >= 2)
36480Sstevel@tonic-gate 			return (X86_VENDOR_Intel);
36490Sstevel@tonic-gate 		break;
36500Sstevel@tonic-gate 	}
36510Sstevel@tonic-gate 	return (-1);
36520Sstevel@tonic-gate }
36530Sstevel@tonic-gate 
36540Sstevel@tonic-gate void
36559652SMichael.Corcoran@Sun.COM cpuid_set_cpu_properties(void *dip, processorid_t cpu_id,
36569652SMichael.Corcoran@Sun.COM     struct cpuid_info *cpi)
36570Sstevel@tonic-gate {
36580Sstevel@tonic-gate 	dev_info_t *cpu_devi;
36590Sstevel@tonic-gate 	int create;
36600Sstevel@tonic-gate 
36619652SMichael.Corcoran@Sun.COM 	cpu_devi = (dev_info_t *)dip;
36620Sstevel@tonic-gate 
36630Sstevel@tonic-gate 	/* device_type */
36640Sstevel@tonic-gate 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, cpu_devi,
36650Sstevel@tonic-gate 	    "device_type", "cpu");
36660Sstevel@tonic-gate 
36670Sstevel@tonic-gate 	/* reg */
36680Sstevel@tonic-gate 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
36690Sstevel@tonic-gate 	    "reg", cpu_id);
36700Sstevel@tonic-gate 
36710Sstevel@tonic-gate 	/* cpu-mhz, and clock-frequency */
36720Sstevel@tonic-gate 	if (cpu_freq > 0) {
36730Sstevel@tonic-gate 		long long mul;
36740Sstevel@tonic-gate 
36750Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
36760Sstevel@tonic-gate 		    "cpu-mhz", cpu_freq);
36770Sstevel@tonic-gate 		if ((mul = cpu_freq * 1000000LL) <= INT_MAX)
36780Sstevel@tonic-gate 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
36790Sstevel@tonic-gate 			    "clock-frequency", (int)mul);
36800Sstevel@tonic-gate 	}
36810Sstevel@tonic-gate 
36820Sstevel@tonic-gate 	if ((x86_feature & X86_CPUID) == 0) {
36830Sstevel@tonic-gate 		return;
36840Sstevel@tonic-gate 	}
36850Sstevel@tonic-gate 
36860Sstevel@tonic-gate 	/* vendor-id */
36870Sstevel@tonic-gate 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, cpu_devi,
36884481Sbholler 	    "vendor-id", cpi->cpi_vendorstr);
36890Sstevel@tonic-gate 
36900Sstevel@tonic-gate 	if (cpi->cpi_maxeax == 0) {
36910Sstevel@tonic-gate 		return;
36920Sstevel@tonic-gate 	}
36930Sstevel@tonic-gate 
36940Sstevel@tonic-gate 	/*
36950Sstevel@tonic-gate 	 * family, model, and step
36960Sstevel@tonic-gate 	 */
36970Sstevel@tonic-gate 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
36984481Sbholler 	    "family", CPI_FAMILY(cpi));
36990Sstevel@tonic-gate 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
37004481Sbholler 	    "cpu-model", CPI_MODEL(cpi));
37010Sstevel@tonic-gate 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
37024481Sbholler 	    "stepping-id", CPI_STEP(cpi));
37030Sstevel@tonic-gate 
37040Sstevel@tonic-gate 	/* type */
37050Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
37060Sstevel@tonic-gate 	case X86_VENDOR_Intel:
37070Sstevel@tonic-gate 		create = 1;
37080Sstevel@tonic-gate 		break;
37090Sstevel@tonic-gate 	default:
37100Sstevel@tonic-gate 		create = 0;
37110Sstevel@tonic-gate 		break;
37120Sstevel@tonic-gate 	}
37130Sstevel@tonic-gate 	if (create)
37140Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
37154481Sbholler 		    "type", CPI_TYPE(cpi));
37160Sstevel@tonic-gate 
37170Sstevel@tonic-gate 	/* ext-family */
37180Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
37190Sstevel@tonic-gate 	case X86_VENDOR_Intel:
37200Sstevel@tonic-gate 	case X86_VENDOR_AMD:
37210Sstevel@tonic-gate 		create = cpi->cpi_family >= 0xf;
37220Sstevel@tonic-gate 		break;
37230Sstevel@tonic-gate 	default:
37240Sstevel@tonic-gate 		create = 0;
37250Sstevel@tonic-gate 		break;
37260Sstevel@tonic-gate 	}
37270Sstevel@tonic-gate 	if (create)
37280Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
37290Sstevel@tonic-gate 		    "ext-family", CPI_FAMILY_XTD(cpi));
37300Sstevel@tonic-gate 
37310Sstevel@tonic-gate 	/* ext-model */
37320Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
37330Sstevel@tonic-gate 	case X86_VENDOR_Intel:
37346317Skk208521 		create = IS_EXTENDED_MODEL_INTEL(cpi);
37352001Sdmick 		break;
37360Sstevel@tonic-gate 	case X86_VENDOR_AMD:
37371582Skchow 		create = CPI_FAMILY(cpi) == 0xf;
37380Sstevel@tonic-gate 		break;
37390Sstevel@tonic-gate 	default:
37400Sstevel@tonic-gate 		create = 0;
37410Sstevel@tonic-gate 		break;
37420Sstevel@tonic-gate 	}
37430Sstevel@tonic-gate 	if (create)
37440Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
37454481Sbholler 		    "ext-model", CPI_MODEL_XTD(cpi));
37460Sstevel@tonic-gate 
37470Sstevel@tonic-gate 	/* generation */
37480Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
37490Sstevel@tonic-gate 	case X86_VENDOR_AMD:
37500Sstevel@tonic-gate 		/*
37510Sstevel@tonic-gate 		 * AMD K5 model 1 was the first part to support this
37520Sstevel@tonic-gate 		 */
37530Sstevel@tonic-gate 		create = cpi->cpi_xmaxeax >= 0x80000001;
37540Sstevel@tonic-gate 		break;
37550Sstevel@tonic-gate 	default:
37560Sstevel@tonic-gate 		create = 0;
37570Sstevel@tonic-gate 		break;
37580Sstevel@tonic-gate 	}
37590Sstevel@tonic-gate 	if (create)
37600Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
37610Sstevel@tonic-gate 		    "generation", BITX((cpi)->cpi_extd[1].cp_eax, 11, 8));
37620Sstevel@tonic-gate 
37630Sstevel@tonic-gate 	/* brand-id */
37640Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
37650Sstevel@tonic-gate 	case X86_VENDOR_Intel:
37660Sstevel@tonic-gate 		/*
37670Sstevel@tonic-gate 		 * brand id first appeared on Pentium III Xeon model 8,
37680Sstevel@tonic-gate 		 * and Celeron model 8 processors and Opteron
37690Sstevel@tonic-gate 		 */
37700Sstevel@tonic-gate 		create = cpi->cpi_family > 6 ||
37710Sstevel@tonic-gate 		    (cpi->cpi_family == 6 && cpi->cpi_model >= 8);
37720Sstevel@tonic-gate 		break;
37730Sstevel@tonic-gate 	case X86_VENDOR_AMD:
37740Sstevel@tonic-gate 		create = cpi->cpi_family >= 0xf;
37750Sstevel@tonic-gate 		break;
37760Sstevel@tonic-gate 	default:
37770Sstevel@tonic-gate 		create = 0;
37780Sstevel@tonic-gate 		break;
37790Sstevel@tonic-gate 	}
37800Sstevel@tonic-gate 	if (create && cpi->cpi_brandid != 0) {
37810Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
37820Sstevel@tonic-gate 		    "brand-id", cpi->cpi_brandid);
37830Sstevel@tonic-gate 	}
37840Sstevel@tonic-gate 
37850Sstevel@tonic-gate 	/* chunks, and apic-id */
37860Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
37870Sstevel@tonic-gate 		/*
37880Sstevel@tonic-gate 		 * first available on Pentium IV and Opteron (K8)
37890Sstevel@tonic-gate 		 */
37901975Sdmick 	case X86_VENDOR_Intel:
37911975Sdmick 		create = IS_NEW_F6(cpi) || cpi->cpi_family >= 0xf;
37921975Sdmick 		break;
37931975Sdmick 	case X86_VENDOR_AMD:
37940Sstevel@tonic-gate 		create = cpi->cpi_family >= 0xf;
37950Sstevel@tonic-gate 		break;
37960Sstevel@tonic-gate 	default:
37970Sstevel@tonic-gate 		create = 0;
37980Sstevel@tonic-gate 		break;
37990Sstevel@tonic-gate 	}
38000Sstevel@tonic-gate 	if (create) {
38010Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
38024481Sbholler 		    "chunks", CPI_CHUNKS(cpi));
38030Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
38047282Smishra 		    "apic-id", cpi->cpi_apicid);
38051414Scindi 		if (cpi->cpi_chipid >= 0) {
38060Sstevel@tonic-gate 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
38070Sstevel@tonic-gate 			    "chip#", cpi->cpi_chipid);
38081414Scindi 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
38091414Scindi 			    "clog#", cpi->cpi_clogid);
38101414Scindi 		}
38110Sstevel@tonic-gate 	}
38120Sstevel@tonic-gate 
38130Sstevel@tonic-gate 	/* cpuid-features */
38140Sstevel@tonic-gate 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
38150Sstevel@tonic-gate 	    "cpuid-features", CPI_FEATURES_EDX(cpi));
38160Sstevel@tonic-gate 
38170Sstevel@tonic-gate 
38180Sstevel@tonic-gate 	/* cpuid-features-ecx */
38190Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
38200Sstevel@tonic-gate 	case X86_VENDOR_Intel:
38211975Sdmick 		create = IS_NEW_F6(cpi) || cpi->cpi_family >= 0xf;
38220Sstevel@tonic-gate 		break;
38230Sstevel@tonic-gate 	default:
38240Sstevel@tonic-gate 		create = 0;
38250Sstevel@tonic-gate 		break;
38260Sstevel@tonic-gate 	}
38270Sstevel@tonic-gate 	if (create)
38280Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
38290Sstevel@tonic-gate 		    "cpuid-features-ecx", CPI_FEATURES_ECX(cpi));
38300Sstevel@tonic-gate 
38310Sstevel@tonic-gate 	/* ext-cpuid-features */
38320Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
38331975Sdmick 	case X86_VENDOR_Intel:
38340Sstevel@tonic-gate 	case X86_VENDOR_AMD:
38350Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
38360Sstevel@tonic-gate 	case X86_VENDOR_TM:
38370Sstevel@tonic-gate 	case X86_VENDOR_Centaur:
38380Sstevel@tonic-gate 		create = cpi->cpi_xmaxeax >= 0x80000001;
38390Sstevel@tonic-gate 		break;
38400Sstevel@tonic-gate 	default:
38410Sstevel@tonic-gate 		create = 0;
38420Sstevel@tonic-gate 		break;
38430Sstevel@tonic-gate 	}
38441975Sdmick 	if (create) {
38450Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
38464481Sbholler 		    "ext-cpuid-features", CPI_FEATURES_XTD_EDX(cpi));
38471975Sdmick 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
38484481Sbholler 		    "ext-cpuid-features-ecx", CPI_FEATURES_XTD_ECX(cpi));
38491975Sdmick 	}
38500Sstevel@tonic-gate 
38510Sstevel@tonic-gate 	/*
38520Sstevel@tonic-gate 	 * Brand String first appeared in Intel Pentium IV, AMD K5
38530Sstevel@tonic-gate 	 * model 1, and Cyrix GXm.  On earlier models we try and
38540Sstevel@tonic-gate 	 * simulate something similar .. so this string should always
38550Sstevel@tonic-gate 	 * same -something- about the processor, however lame.
38560Sstevel@tonic-gate 	 */
38570Sstevel@tonic-gate 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, cpu_devi,
38580Sstevel@tonic-gate 	    "brand-string", cpi->cpi_brandstr);
38590Sstevel@tonic-gate 
38600Sstevel@tonic-gate 	/*
38610Sstevel@tonic-gate 	 * Finally, cache and tlb information
38620Sstevel@tonic-gate 	 */
38630Sstevel@tonic-gate 	switch (x86_which_cacheinfo(cpi)) {
38640Sstevel@tonic-gate 	case X86_VENDOR_Intel:
38650Sstevel@tonic-gate 		intel_walk_cacheinfo(cpi, cpu_devi, add_cacheent_props);
38660Sstevel@tonic-gate 		break;
38670Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
38680Sstevel@tonic-gate 		cyrix_walk_cacheinfo(cpi, cpu_devi, add_cacheent_props);
38690Sstevel@tonic-gate 		break;
38700Sstevel@tonic-gate 	case X86_VENDOR_AMD:
38710Sstevel@tonic-gate 		amd_cache_info(cpi, cpu_devi);
38720Sstevel@tonic-gate 		break;
38730Sstevel@tonic-gate 	default:
38740Sstevel@tonic-gate 		break;
38750Sstevel@tonic-gate 	}
38760Sstevel@tonic-gate }
38770Sstevel@tonic-gate 
38780Sstevel@tonic-gate struct l2info {
38790Sstevel@tonic-gate 	int *l2i_csz;
38800Sstevel@tonic-gate 	int *l2i_lsz;
38810Sstevel@tonic-gate 	int *l2i_assoc;
38820Sstevel@tonic-gate 	int l2i_ret;
38830Sstevel@tonic-gate };
38840Sstevel@tonic-gate 
38850Sstevel@tonic-gate /*
38860Sstevel@tonic-gate  * A cacheinfo walker that fetches the size, line-size and associativity
38870Sstevel@tonic-gate  * of the L2 cache
38880Sstevel@tonic-gate  */
38890Sstevel@tonic-gate static int
38900Sstevel@tonic-gate intel_l2cinfo(void *arg, const struct cachetab *ct)
38910Sstevel@tonic-gate {
38920Sstevel@tonic-gate 	struct l2info *l2i = arg;
38930Sstevel@tonic-gate 	int *ip;
38940Sstevel@tonic-gate 
38950Sstevel@tonic-gate 	if (ct->ct_label != l2_cache_str &&
38960Sstevel@tonic-gate 	    ct->ct_label != sl2_cache_str)
38970Sstevel@tonic-gate 		return (0);	/* not an L2 -- keep walking */
38980Sstevel@tonic-gate 
38990Sstevel@tonic-gate 	if ((ip = l2i->l2i_csz) != NULL)
39000Sstevel@tonic-gate 		*ip = ct->ct_size;
39010Sstevel@tonic-gate 	if ((ip = l2i->l2i_lsz) != NULL)
39020Sstevel@tonic-gate 		*ip = ct->ct_line_size;
39030Sstevel@tonic-gate 	if ((ip = l2i->l2i_assoc) != NULL)
39040Sstevel@tonic-gate 		*ip = ct->ct_assoc;
39050Sstevel@tonic-gate 	l2i->l2i_ret = ct->ct_size;
39060Sstevel@tonic-gate 	return (1);		/* was an L2 -- terminate walk */
39070Sstevel@tonic-gate }
39080Sstevel@tonic-gate 
39095070Skchow /*
39105070Skchow  * AMD L2/L3 Cache and TLB Associativity Field Definition:
39115070Skchow  *
39125070Skchow  *	Unlike the associativity for the L1 cache and tlb where the 8 bit
39135070Skchow  *	value is the associativity, the associativity for the L2 cache and
39145070Skchow  *	tlb is encoded in the following table. The 4 bit L2 value serves as
39155070Skchow  *	an index into the amd_afd[] array to determine the associativity.
39165070Skchow  *	-1 is undefined. 0 is fully associative.
39175070Skchow  */
39185070Skchow 
39195070Skchow static int amd_afd[] =
39205070Skchow 	{-1, 1, 2, -1, 4, -1, 8, -1, 16, -1, 32, 48, 64, 96, 128, 0};
39215070Skchow 
39220Sstevel@tonic-gate static void
39230Sstevel@tonic-gate amd_l2cacheinfo(struct cpuid_info *cpi, struct l2info *l2i)
39240Sstevel@tonic-gate {
39251228Sandrei 	struct cpuid_regs *cp;
39260Sstevel@tonic-gate 	uint_t size, assoc;
39275070Skchow 	int i;
39280Sstevel@tonic-gate 	int *ip;
39290Sstevel@tonic-gate 
39300Sstevel@tonic-gate 	if (cpi->cpi_xmaxeax < 0x80000006)
39310Sstevel@tonic-gate 		return;
39320Sstevel@tonic-gate 	cp = &cpi->cpi_extd[6];
39330Sstevel@tonic-gate 
39345070Skchow 	if ((i = BITX(cp->cp_ecx, 15, 12)) != 0 &&
39350Sstevel@tonic-gate 	    (size = BITX(cp->cp_ecx, 31, 16)) != 0) {
39360Sstevel@tonic-gate 		uint_t cachesz = size * 1024;
39375070Skchow 		assoc = amd_afd[i];
39385070Skchow 
39395070Skchow 		ASSERT(assoc != -1);
39400Sstevel@tonic-gate 
39410Sstevel@tonic-gate 		if ((ip = l2i->l2i_csz) != NULL)
39420Sstevel@tonic-gate 			*ip = cachesz;
39430Sstevel@tonic-gate 		if ((ip = l2i->l2i_lsz) != NULL)
39440Sstevel@tonic-gate 			*ip = BITX(cp->cp_ecx, 7, 0);
39450Sstevel@tonic-gate 		if ((ip = l2i->l2i_assoc) != NULL)
39460Sstevel@tonic-gate 			*ip = assoc;
39470Sstevel@tonic-gate 		l2i->l2i_ret = cachesz;
39480Sstevel@tonic-gate 	}
39490Sstevel@tonic-gate }
39500Sstevel@tonic-gate 
39510Sstevel@tonic-gate int
39520Sstevel@tonic-gate getl2cacheinfo(cpu_t *cpu, int *csz, int *lsz, int *assoc)
39530Sstevel@tonic-gate {
39540Sstevel@tonic-gate 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
39550Sstevel@tonic-gate 	struct l2info __l2info, *l2i = &__l2info;
39560Sstevel@tonic-gate 
39570Sstevel@tonic-gate 	l2i->l2i_csz = csz;
39580Sstevel@tonic-gate 	l2i->l2i_lsz = lsz;
39590Sstevel@tonic-gate 	l2i->l2i_assoc = assoc;
39600Sstevel@tonic-gate 	l2i->l2i_ret = -1;
39610Sstevel@tonic-gate 
39620Sstevel@tonic-gate 	switch (x86_which_cacheinfo(cpi)) {
39630Sstevel@tonic-gate 	case X86_VENDOR_Intel:
39640Sstevel@tonic-gate 		intel_walk_cacheinfo(cpi, l2i, intel_l2cinfo);
39650Sstevel@tonic-gate 		break;
39660Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
39670Sstevel@tonic-gate 		cyrix_walk_cacheinfo(cpi, l2i, intel_l2cinfo);
39680Sstevel@tonic-gate 		break;
39690Sstevel@tonic-gate 	case X86_VENDOR_AMD:
39700Sstevel@tonic-gate 		amd_l2cacheinfo(cpi, l2i);
39710Sstevel@tonic-gate 		break;
39720Sstevel@tonic-gate 	default:
39730Sstevel@tonic-gate 		break;
39740Sstevel@tonic-gate 	}
39750Sstevel@tonic-gate 	return (l2i->l2i_ret);
39760Sstevel@tonic-gate }
39774481Sbholler 
39785084Sjohnlev #if !defined(__xpv)
39795084Sjohnlev 
39805045Sbholler uint32_t *
39815045Sbholler cpuid_mwait_alloc(cpu_t *cpu)
39825045Sbholler {
39835045Sbholler 	uint32_t	*ret;
39845045Sbholler 	size_t		mwait_size;
39855045Sbholler 
3986*12004Sjiang.liu@intel.com 	ASSERT(cpuid_checkpass(CPU, 2));
3987*12004Sjiang.liu@intel.com 
3988*12004Sjiang.liu@intel.com 	mwait_size = CPU->cpu_m.mcpu_cpi->cpi_mwait.mon_max;
39895045Sbholler 	if (mwait_size == 0)
39905045Sbholler 		return (NULL);
39915045Sbholler 
39925045Sbholler 	/*
39935045Sbholler 	 * kmem_alloc() returns cache line size aligned data for mwait_size
39945045Sbholler 	 * allocations.  mwait_size is currently cache line sized.  Neither
39955045Sbholler 	 * of these implementation details are guarantied to be true in the
39965045Sbholler 	 * future.
39975045Sbholler 	 *
39985045Sbholler 	 * First try allocating mwait_size as kmem_alloc() currently returns
39995045Sbholler 	 * correctly aligned memory.  If kmem_alloc() does not return
40005045Sbholler 	 * mwait_size aligned memory, then use mwait_size ROUNDUP.
40015045Sbholler 	 *
40025045Sbholler 	 * Set cpi_mwait.buf_actual and cpi_mwait.size_actual in case we
40035045Sbholler 	 * decide to free this memory.
40045045Sbholler 	 */
40055045Sbholler 	ret = kmem_zalloc(mwait_size, KM_SLEEP);
40065045Sbholler 	if (ret == (uint32_t *)P2ROUNDUP((uintptr_t)ret, mwait_size)) {
40075045Sbholler 		cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual = ret;
40085045Sbholler 		cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual = mwait_size;
40095045Sbholler 		*ret = MWAIT_RUNNING;
40105045Sbholler 		return (ret);
40115045Sbholler 	} else {
40125045Sbholler 		kmem_free(ret, mwait_size);
40135045Sbholler 		ret = kmem_zalloc(mwait_size * 2, KM_SLEEP);
40145045Sbholler 		cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual = ret;
40155045Sbholler 		cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual = mwait_size * 2;
40165045Sbholler 		ret = (uint32_t *)P2ROUNDUP((uintptr_t)ret, mwait_size);
40175045Sbholler 		*ret = MWAIT_RUNNING;
40185045Sbholler 		return (ret);
40195045Sbholler 	}
40205045Sbholler }
40215045Sbholler 
40225045Sbholler void
40235045Sbholler cpuid_mwait_free(cpu_t *cpu)
40244481Sbholler {
4025*12004Sjiang.liu@intel.com 	if (cpu->cpu_m.mcpu_cpi == NULL) {
4026*12004Sjiang.liu@intel.com 		return;
4027*12004Sjiang.liu@intel.com 	}
40285045Sbholler 
40295045Sbholler 	if (cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual != NULL &&
40305045Sbholler 	    cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual > 0) {
40315045Sbholler 		kmem_free(cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual,
40325045Sbholler 		    cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual);
40335045Sbholler 	}
40345045Sbholler 
40355045Sbholler 	cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual = NULL;
40365045Sbholler 	cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual = 0;
40374481Sbholler }
40385084Sjohnlev 
40395322Ssudheer void
40405322Ssudheer patch_tsc_read(int flag)
40415322Ssudheer {
40425322Ssudheer 	size_t cnt;
40437532SSean.Ye@Sun.COM 
40445322Ssudheer 	switch (flag) {
40455322Ssudheer 	case X86_NO_TSC:
40465322Ssudheer 		cnt = &_no_rdtsc_end - &_no_rdtsc_start;
40475338Ssudheer 		(void) memcpy((void *)tsc_read, (void *)&_no_rdtsc_start, cnt);
40485322Ssudheer 		break;
40495322Ssudheer 	case X86_HAVE_TSCP:
40505322Ssudheer 		cnt = &_tscp_end - &_tscp_start;
40515338Ssudheer 		(void) memcpy((void *)tsc_read, (void *)&_tscp_start, cnt);
40525322Ssudheer 		break;
40535322Ssudheer 	case X86_TSC_MFENCE:
40545322Ssudheer 		cnt = &_tsc_mfence_end - &_tsc_mfence_start;
40555338Ssudheer 		(void) memcpy((void *)tsc_read,
40565338Ssudheer 		    (void *)&_tsc_mfence_start, cnt);
40575322Ssudheer 		break;
40586642Ssudheer 	case X86_TSC_LFENCE:
40596642Ssudheer 		cnt = &_tsc_lfence_end - &_tsc_lfence_start;
40606642Ssudheer 		(void) memcpy((void *)tsc_read,
40616642Ssudheer 		    (void *)&_tsc_lfence_start, cnt);
40626642Ssudheer 		break;
40635322Ssudheer 	default:
40645322Ssudheer 		break;
40655322Ssudheer 	}
40665322Ssudheer }
40675322Ssudheer 
40688906SEric.Saxe@Sun.COM int
40698906SEric.Saxe@Sun.COM cpuid_deep_cstates_supported(void)
40708906SEric.Saxe@Sun.COM {
40718906SEric.Saxe@Sun.COM 	struct cpuid_info *cpi;
40728906SEric.Saxe@Sun.COM 	struct cpuid_regs regs;
40738906SEric.Saxe@Sun.COM 
40748906SEric.Saxe@Sun.COM 	ASSERT(cpuid_checkpass(CPU, 1));
40758906SEric.Saxe@Sun.COM 
40768906SEric.Saxe@Sun.COM 	cpi = CPU->cpu_m.mcpu_cpi;
40778906SEric.Saxe@Sun.COM 
40788906SEric.Saxe@Sun.COM 	if (!(x86_feature & X86_CPUID))
40798906SEric.Saxe@Sun.COM 		return (0);
40808906SEric.Saxe@Sun.COM 
40818906SEric.Saxe@Sun.COM 	switch (cpi->cpi_vendor) {
40828906SEric.Saxe@Sun.COM 	case X86_VENDOR_Intel:
40838906SEric.Saxe@Sun.COM 		if (cpi->cpi_xmaxeax < 0x80000007)
40848906SEric.Saxe@Sun.COM 			return (0);
40858906SEric.Saxe@Sun.COM 
40868906SEric.Saxe@Sun.COM 		/*
40878906SEric.Saxe@Sun.COM 		 * TSC run at a constant rate in all ACPI C-states?
40888906SEric.Saxe@Sun.COM 		 */
40898906SEric.Saxe@Sun.COM 		regs.cp_eax = 0x80000007;
40908906SEric.Saxe@Sun.COM 		(void) __cpuid_insn(&regs);
40918906SEric.Saxe@Sun.COM 		return (regs.cp_edx & CPUID_TSC_CSTATE_INVARIANCE);
40928906SEric.Saxe@Sun.COM 
40938906SEric.Saxe@Sun.COM 	default:
40948906SEric.Saxe@Sun.COM 		return (0);
40958906SEric.Saxe@Sun.COM 	}
40968906SEric.Saxe@Sun.COM }
40978906SEric.Saxe@Sun.COM 
40988930SBill.Holler@Sun.COM #endif	/* !__xpv */
40998930SBill.Holler@Sun.COM 
41008930SBill.Holler@Sun.COM void
41018930SBill.Holler@Sun.COM post_startup_cpu_fixups(void)
41028930SBill.Holler@Sun.COM {
41038930SBill.Holler@Sun.COM #ifndef __xpv
41048930SBill.Holler@Sun.COM 	/*
41058930SBill.Holler@Sun.COM 	 * Some AMD processors support C1E state. Entering this state will
41068930SBill.Holler@Sun.COM 	 * cause the local APIC timer to stop, which we can't deal with at
41078930SBill.Holler@Sun.COM 	 * this time.
41088930SBill.Holler@Sun.COM 	 */
41098930SBill.Holler@Sun.COM 	if (cpuid_getvendor(CPU) == X86_VENDOR_AMD) {
41108930SBill.Holler@Sun.COM 		on_trap_data_t otd;
41118930SBill.Holler@Sun.COM 		uint64_t reg;
41128930SBill.Holler@Sun.COM 
41138930SBill.Holler@Sun.COM 		if (!on_trap(&otd, OT_DATA_ACCESS)) {
41148930SBill.Holler@Sun.COM 			reg = rdmsr(MSR_AMD_INT_PENDING_CMP_HALT);
41158930SBill.Holler@Sun.COM 			/* Disable C1E state if it is enabled by BIOS */
41168930SBill.Holler@Sun.COM 			if ((reg >> AMD_ACTONCMPHALT_SHIFT) &
41178930SBill.Holler@Sun.COM 			    AMD_ACTONCMPHALT_MASK) {
41188930SBill.Holler@Sun.COM 				reg &= ~(AMD_ACTONCMPHALT_MASK <<
41198930SBill.Holler@Sun.COM 				    AMD_ACTONCMPHALT_SHIFT);
41208930SBill.Holler@Sun.COM 				wrmsr(MSR_AMD_INT_PENDING_CMP_HALT, reg);
41218930SBill.Holler@Sun.COM 			}
41228930SBill.Holler@Sun.COM 		}
41238930SBill.Holler@Sun.COM 		no_trap();
41248930SBill.Holler@Sun.COM 	}
41258930SBill.Holler@Sun.COM #endif	/* !__xpv */
41268930SBill.Holler@Sun.COM }
41278930SBill.Holler@Sun.COM 
41289283SBill.Holler@Sun.COM /*
41299283SBill.Holler@Sun.COM  * Starting with the Westmere processor the local
41309283SBill.Holler@Sun.COM  * APIC timer will continue running in all C-states,
41319283SBill.Holler@Sun.COM  * including the deepest C-states.
41329283SBill.Holler@Sun.COM  */
41339283SBill.Holler@Sun.COM int
41349283SBill.Holler@Sun.COM cpuid_arat_supported(void)
41359283SBill.Holler@Sun.COM {
41369283SBill.Holler@Sun.COM 	struct cpuid_info *cpi;
41379283SBill.Holler@Sun.COM 	struct cpuid_regs regs;
41389283SBill.Holler@Sun.COM 
41399283SBill.Holler@Sun.COM 	ASSERT(cpuid_checkpass(CPU, 1));
41409283SBill.Holler@Sun.COM 	ASSERT(x86_feature & X86_CPUID);
41419283SBill.Holler@Sun.COM 
41429283SBill.Holler@Sun.COM 	cpi = CPU->cpu_m.mcpu_cpi;
41439283SBill.Holler@Sun.COM 
41449283SBill.Holler@Sun.COM 	switch (cpi->cpi_vendor) {
41459283SBill.Holler@Sun.COM 	case X86_VENDOR_Intel:
41469283SBill.Holler@Sun.COM 		/*
41479283SBill.Holler@Sun.COM 		 * Always-running Local APIC Timer is
41489283SBill.Holler@Sun.COM 		 * indicated by CPUID.6.EAX[2].
41499283SBill.Holler@Sun.COM 		 */
41509283SBill.Holler@Sun.COM 		if (cpi->cpi_maxeax >= 6) {
41519283SBill.Holler@Sun.COM 			regs.cp_eax = 6;
41529283SBill.Holler@Sun.COM 			(void) cpuid_insn(NULL, &regs);
41539283SBill.Holler@Sun.COM 			return (regs.cp_eax & CPUID_CSTATE_ARAT);
41549283SBill.Holler@Sun.COM 		} else {
41559283SBill.Holler@Sun.COM 			return (0);
41569283SBill.Holler@Sun.COM 		}
41579283SBill.Holler@Sun.COM 	default:
41589283SBill.Holler@Sun.COM 		return (0);
41599283SBill.Holler@Sun.COM 	}
41609283SBill.Holler@Sun.COM }
41619283SBill.Holler@Sun.COM 
416210992Saubrey.li@intel.com /*
416310992Saubrey.li@intel.com  * Check support for Intel ENERGY_PERF_BIAS feature
416410992Saubrey.li@intel.com  */
416510992Saubrey.li@intel.com int
416610992Saubrey.li@intel.com cpuid_iepb_supported(struct cpu *cp)
416710992Saubrey.li@intel.com {
416810992Saubrey.li@intel.com 	struct cpuid_info *cpi = cp->cpu_m.mcpu_cpi;
416910992Saubrey.li@intel.com 	struct cpuid_regs regs;
417010992Saubrey.li@intel.com 
417110992Saubrey.li@intel.com 	ASSERT(cpuid_checkpass(cp, 1));
417210992Saubrey.li@intel.com 
417310992Saubrey.li@intel.com 	if (!(x86_feature & X86_CPUID) || !(x86_feature & X86_MSR)) {
417410992Saubrey.li@intel.com 		return (0);
417510992Saubrey.li@intel.com 	}
417610992Saubrey.li@intel.com 
417710992Saubrey.li@intel.com 	/*
417810992Saubrey.li@intel.com 	 * Intel ENERGY_PERF_BIAS MSR is indicated by
417910992Saubrey.li@intel.com 	 * capability bit CPUID.6.ECX.3
418010992Saubrey.li@intel.com 	 */
418110992Saubrey.li@intel.com 	if ((cpi->cpi_vendor != X86_VENDOR_Intel) || (cpi->cpi_maxeax < 6))
418210992Saubrey.li@intel.com 		return (0);
418310992Saubrey.li@intel.com 
418410992Saubrey.li@intel.com 	regs.cp_eax = 0x6;
418510992Saubrey.li@intel.com 	(void) cpuid_insn(NULL, &regs);
418610992Saubrey.li@intel.com 	return (regs.cp_ecx & CPUID_EPB_SUPPORT);
418710992Saubrey.li@intel.com }
418810992Saubrey.li@intel.com 
41898377SBill.Holler@Sun.COM #if defined(__amd64) && !defined(__xpv)
41908377SBill.Holler@Sun.COM /*
41918377SBill.Holler@Sun.COM  * Patch in versions of bcopy for high performance Intel Nhm processors
41928377SBill.Holler@Sun.COM  * and later...
41938377SBill.Holler@Sun.COM  */
41948377SBill.Holler@Sun.COM void
41958377SBill.Holler@Sun.COM patch_memops(uint_t vendor)
41968377SBill.Holler@Sun.COM {
41978377SBill.Holler@Sun.COM 	size_t cnt, i;
41988377SBill.Holler@Sun.COM 	caddr_t to, from;
41998377SBill.Holler@Sun.COM 
42008377SBill.Holler@Sun.COM 	if ((vendor == X86_VENDOR_Intel) && ((x86_feature & X86_SSE4_2) != 0)) {
42018377SBill.Holler@Sun.COM 		cnt = &bcopy_patch_end - &bcopy_patch_start;
42028377SBill.Holler@Sun.COM 		to = &bcopy_ck_size;
42038377SBill.Holler@Sun.COM 		from = &bcopy_patch_start;
42048377SBill.Holler@Sun.COM 		for (i = 0; i < cnt; i++) {
42058377SBill.Holler@Sun.COM 			*to++ = *from++;
42068377SBill.Holler@Sun.COM 		}
42078377SBill.Holler@Sun.COM 	}
42088377SBill.Holler@Sun.COM }
42098377SBill.Holler@Sun.COM #endif  /* __amd64 && !__xpv */
4210