xref: /titanic_52/usr/src/uts/i86pc/os/cpuid.c (revision 349b53dd4e695e3d833b5380540385145b2d3ae8)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ee88d2b9Skchow  * Common Development and Distribution License (the "License").
6ee88d2b9Skchow  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
220e751525SEric Saxe  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
25cef70d2cSBill Holler /*
26cef70d2cSBill Holler  * Copyright (c) 2009, Intel Corporation.
27cef70d2cSBill Holler  * All rights reserved.
28cef70d2cSBill Holler  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Various routines to handle identification
327c478bd9Sstevel@tonic-gate  * and classification of x86 processors.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
377c478bd9Sstevel@tonic-gate #include <sys/x86_archext.h>
387c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
397c478bd9Sstevel@tonic-gate #include <sys/systm.h>
407c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
417c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
427c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
437c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
447c478bd9Sstevel@tonic-gate #include <sys/processor.h>
455b8a6efeSbholler #include <sys/sysmacros.h>
46fb2f18f8Sesaxe #include <sys/pg.h>
477c478bd9Sstevel@tonic-gate #include <sys/fp.h>
487c478bd9Sstevel@tonic-gate #include <sys/controlregs.h>
497c478bd9Sstevel@tonic-gate #include <sys/auxv_386.h>
507c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
517c478bd9Sstevel@tonic-gate #include <sys/memnode.h>
527c478bd9Sstevel@tonic-gate 
53e4b86885SCheng Sean Ye #ifdef __xpv
54e4b86885SCheng Sean Ye #include <sys/hypervisor.h>
55e774b42bSBill Holler #else
56e774b42bSBill Holler #include <sys/ontrap.h>
57e4b86885SCheng Sean Ye #endif
58e4b86885SCheng Sean Ye 
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  * Pass 0 of cpuid feature analysis happens in locore. It contains special code
617c478bd9Sstevel@tonic-gate  * to recognize Cyrix processors that are not cpuid-compliant, and to deal with
627c478bd9Sstevel@tonic-gate  * them accordingly. For most modern processors, feature detection occurs here
637c478bd9Sstevel@tonic-gate  * in pass 1.
647c478bd9Sstevel@tonic-gate  *
657c478bd9Sstevel@tonic-gate  * Pass 1 of cpuid feature analysis happens just at the beginning of mlsetup()
667c478bd9Sstevel@tonic-gate  * for the boot CPU and does the basic analysis that the early kernel needs.
677c478bd9Sstevel@tonic-gate  * x86_feature is set based on the return value of cpuid_pass1() of the boot
687c478bd9Sstevel@tonic-gate  * CPU.
697c478bd9Sstevel@tonic-gate  *
707c478bd9Sstevel@tonic-gate  * Pass 1 includes:
717c478bd9Sstevel@tonic-gate  *
727c478bd9Sstevel@tonic-gate  *	o Determining vendor/model/family/stepping and setting x86_type and
737c478bd9Sstevel@tonic-gate  *	  x86_vendor accordingly.
747c478bd9Sstevel@tonic-gate  *	o Processing the feature flags returned by the cpuid instruction while
757c478bd9Sstevel@tonic-gate  *	  applying any workarounds or tricks for the specific processor.
767c478bd9Sstevel@tonic-gate  *	o Mapping the feature flags into Solaris feature bits (X86_*).
777c478bd9Sstevel@tonic-gate  *	o Processing extended feature flags if supported by the processor,
787c478bd9Sstevel@tonic-gate  *	  again while applying specific processor knowledge.
797c478bd9Sstevel@tonic-gate  *	o Determining the CMT characteristics of the system.
807c478bd9Sstevel@tonic-gate  *
817c478bd9Sstevel@tonic-gate  * Pass 1 is done on non-boot CPUs during their initialization and the results
827c478bd9Sstevel@tonic-gate  * are used only as a meager attempt at ensuring that all processors within the
837c478bd9Sstevel@tonic-gate  * system support the same features.
847c478bd9Sstevel@tonic-gate  *
857c478bd9Sstevel@tonic-gate  * Pass 2 of cpuid feature analysis happens just at the beginning
867c478bd9Sstevel@tonic-gate  * of startup().  It just copies in and corrects the remainder
877c478bd9Sstevel@tonic-gate  * of the cpuid data we depend on: standard cpuid functions that we didn't
887c478bd9Sstevel@tonic-gate  * need for pass1 feature analysis, and extended cpuid functions beyond the
897c478bd9Sstevel@tonic-gate  * simple feature processing done in pass1.
907c478bd9Sstevel@tonic-gate  *
917c478bd9Sstevel@tonic-gate  * Pass 3 of cpuid analysis is invoked after basic kernel services; in
927c478bd9Sstevel@tonic-gate  * particular kernel memory allocation has been made available. It creates a
937c478bd9Sstevel@tonic-gate  * readable brand string based on the data collected in the first two passes.
947c478bd9Sstevel@tonic-gate  *
957c478bd9Sstevel@tonic-gate  * Pass 4 of cpuid analysis is invoked after post_startup() when all
967c478bd9Sstevel@tonic-gate  * the support infrastructure for various hardware features has been
977c478bd9Sstevel@tonic-gate  * initialized. It determines which processor features will be reported
987c478bd9Sstevel@tonic-gate  * to userland via the aux vector.
997c478bd9Sstevel@tonic-gate  *
1007c478bd9Sstevel@tonic-gate  * All passes are executed on all CPUs, but only the boot CPU determines what
1017c478bd9Sstevel@tonic-gate  * features the kernel will use.
1027c478bd9Sstevel@tonic-gate  *
1037c478bd9Sstevel@tonic-gate  * Much of the worst junk in this file is for the support of processors
1047c478bd9Sstevel@tonic-gate  * that didn't really implement the cpuid instruction properly.
1057c478bd9Sstevel@tonic-gate  *
1067c478bd9Sstevel@tonic-gate  * NOTE: The accessor functions (cpuid_get*) are aware of, and ASSERT upon,
1077c478bd9Sstevel@tonic-gate  * the pass numbers.  Accordingly, changes to the pass code may require changes
1087c478bd9Sstevel@tonic-gate  * to the accessor code.
1097c478bd9Sstevel@tonic-gate  */
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate uint_t x86_feature = 0;
1127c478bd9Sstevel@tonic-gate uint_t x86_vendor = X86_VENDOR_IntelClone;
1137c478bd9Sstevel@tonic-gate uint_t x86_type = X86_TYPE_OTHER;
11486c1f4dcSVikram Hegde uint_t x86_clflush_size = 0;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate uint_t pentiumpro_bug4046376;
1177c478bd9Sstevel@tonic-gate uint_t pentiumpro_bug4064495;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate uint_t enable486;
1207997e108SSurya Prakki /*
121b9bfdccdSStuart Maybee  * This is set to platform type Solaris is running on.
1227997e108SSurya Prakki  */
123*349b53ddSStuart Maybee static int platform_type = -1;
124*349b53ddSStuart Maybee 
125*349b53ddSStuart Maybee #if !defined(__xpv)
126*349b53ddSStuart Maybee /*
127*349b53ddSStuart Maybee  * Variable to patch if hypervisor platform detection needs to be
128*349b53ddSStuart Maybee  * disabled (e.g. platform_type will always be HW_NATIVE if this is 0).
129*349b53ddSStuart Maybee  */
130*349b53ddSStuart Maybee int enable_platform_detection = 1;
131*349b53ddSStuart Maybee #endif
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate /*
134f98fbcecSbholler  * monitor/mwait info.
1355b8a6efeSbholler  *
1365b8a6efeSbholler  * size_actual and buf_actual are the real address and size allocated to get
1375b8a6efeSbholler  * proper mwait_buf alignement.  buf_actual and size_actual should be passed
1385b8a6efeSbholler  * to kmem_free().  Currently kmem_alloc() and mwait happen to both use
1395b8a6efeSbholler  * processor cache-line alignment, but this is not guarantied in the furture.
140f98fbcecSbholler  */
141f98fbcecSbholler struct mwait_info {
142f98fbcecSbholler 	size_t		mon_min;	/* min size to avoid missed wakeups */
143f98fbcecSbholler 	size_t		mon_max;	/* size to avoid false wakeups */
1445b8a6efeSbholler 	size_t		size_actual;	/* size actually allocated */
1455b8a6efeSbholler 	void		*buf_actual;	/* memory actually allocated */
146f98fbcecSbholler 	uint32_t	support;	/* processor support of monitor/mwait */
147f98fbcecSbholler };
148f98fbcecSbholler 
149f98fbcecSbholler /*
1507c478bd9Sstevel@tonic-gate  * These constants determine how many of the elements of the
1517c478bd9Sstevel@tonic-gate  * cpuid we cache in the cpuid_info data structure; the
1527c478bd9Sstevel@tonic-gate  * remaining elements are accessible via the cpuid instruction.
1537c478bd9Sstevel@tonic-gate  */
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate #define	NMAX_CPI_STD	6		/* eax = 0 .. 5 */
1567c478bd9Sstevel@tonic-gate #define	NMAX_CPI_EXTD	9		/* eax = 0x80000000 .. 0x80000008 */
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate struct cpuid_info {
1597c478bd9Sstevel@tonic-gate 	uint_t cpi_pass;		/* last pass completed */
1607c478bd9Sstevel@tonic-gate 	/*
1617c478bd9Sstevel@tonic-gate 	 * standard function information
1627c478bd9Sstevel@tonic-gate 	 */
1637c478bd9Sstevel@tonic-gate 	uint_t cpi_maxeax;		/* fn 0: %eax */
1647c478bd9Sstevel@tonic-gate 	char cpi_vendorstr[13];		/* fn 0: %ebx:%ecx:%edx */
1657c478bd9Sstevel@tonic-gate 	uint_t cpi_vendor;		/* enum of cpi_vendorstr */
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	uint_t cpi_family;		/* fn 1: extended family */
1687c478bd9Sstevel@tonic-gate 	uint_t cpi_model;		/* fn 1: extended model */
1697c478bd9Sstevel@tonic-gate 	uint_t cpi_step;		/* fn 1: stepping */
1707c478bd9Sstevel@tonic-gate 	chipid_t cpi_chipid;		/* fn 1: %ebx: chip # on ht cpus */
1717c478bd9Sstevel@tonic-gate 	uint_t cpi_brandid;		/* fn 1: %ebx: brand ID */
1727c478bd9Sstevel@tonic-gate 	int cpi_clogid;			/* fn 1: %ebx: thread # */
1738949bcd6Sandrei 	uint_t cpi_ncpu_per_chip;	/* fn 1: %ebx: logical cpu count */
1747c478bd9Sstevel@tonic-gate 	uint8_t cpi_cacheinfo[16];	/* fn 2: intel-style cache desc */
1757c478bd9Sstevel@tonic-gate 	uint_t cpi_ncache;		/* fn 2: number of elements */
176d129bde2Sesaxe 	uint_t cpi_ncpu_shr_last_cache;	/* fn 4: %eax: ncpus sharing cache */
177d129bde2Sesaxe 	id_t cpi_last_lvl_cacheid;	/* fn 4: %eax: derived cache id */
178d129bde2Sesaxe 	uint_t cpi_std_4_size;		/* fn 4: number of fn 4 elements */
179d129bde2Sesaxe 	struct cpuid_regs **cpi_std_4;	/* fn 4: %ecx == 0 .. fn4_size */
1808949bcd6Sandrei 	struct cpuid_regs cpi_std[NMAX_CPI_STD];	/* 0 .. 5 */
1817c478bd9Sstevel@tonic-gate 	/*
1827c478bd9Sstevel@tonic-gate 	 * extended function information
1837c478bd9Sstevel@tonic-gate 	 */
1847c478bd9Sstevel@tonic-gate 	uint_t cpi_xmaxeax;		/* fn 0x80000000: %eax */
1857c478bd9Sstevel@tonic-gate 	char cpi_brandstr[49];		/* fn 0x8000000[234] */
1867c478bd9Sstevel@tonic-gate 	uint8_t cpi_pabits;		/* fn 0x80000006: %eax */
1877c478bd9Sstevel@tonic-gate 	uint8_t cpi_vabits;		/* fn 0x80000006: %eax */
1888949bcd6Sandrei 	struct cpuid_regs cpi_extd[NMAX_CPI_EXTD]; /* 0x8000000[0-8] */
18910569901Sgavinm 	id_t cpi_coreid;		/* same coreid => strands share core */
19010569901Sgavinm 	int cpi_pkgcoreid;		/* core number within single package */
1918949bcd6Sandrei 	uint_t cpi_ncore_per_chip;	/* AMD: fn 0x80000008: %ecx[7-0] */
1928949bcd6Sandrei 					/* Intel: fn 4: %eax[31-26] */
1937c478bd9Sstevel@tonic-gate 	/*
1947c478bd9Sstevel@tonic-gate 	 * supported feature information
1957c478bd9Sstevel@tonic-gate 	 */
196ae115bc7Smrj 	uint32_t cpi_support[5];
1977c478bd9Sstevel@tonic-gate #define	STD_EDX_FEATURES	0
1987c478bd9Sstevel@tonic-gate #define	AMD_EDX_FEATURES	1
1997c478bd9Sstevel@tonic-gate #define	TM_EDX_FEATURES		2
2007c478bd9Sstevel@tonic-gate #define	STD_ECX_FEATURES	3
201ae115bc7Smrj #define	AMD_ECX_FEATURES	4
2028a40a695Sgavinm 	/*
2038a40a695Sgavinm 	 * Synthesized information, where known.
2048a40a695Sgavinm 	 */
2058a40a695Sgavinm 	uint32_t cpi_chiprev;		/* See X86_CHIPREV_* in x86_archext.h */
2068a40a695Sgavinm 	const char *cpi_chiprevstr;	/* May be NULL if chiprev unknown */
2078a40a695Sgavinm 	uint32_t cpi_socket;		/* Chip package/socket type */
208f98fbcecSbholler 
209f98fbcecSbholler 	struct mwait_info cpi_mwait;	/* fn 5: monitor/mwait info */
210b6917abeSmishra 	uint32_t cpi_apicid;
2117c478bd9Sstevel@tonic-gate };
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate static struct cpuid_info cpuid_info0;
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate /*
2177c478bd9Sstevel@tonic-gate  * These bit fields are defined by the Intel Application Note AP-485
2187c478bd9Sstevel@tonic-gate  * "Intel Processor Identification and the CPUID Instruction"
2197c478bd9Sstevel@tonic-gate  */
2207c478bd9Sstevel@tonic-gate #define	CPI_FAMILY_XTD(cpi)	BITX((cpi)->cpi_std[1].cp_eax, 27, 20)
2217c478bd9Sstevel@tonic-gate #define	CPI_MODEL_XTD(cpi)	BITX((cpi)->cpi_std[1].cp_eax, 19, 16)
2227c478bd9Sstevel@tonic-gate #define	CPI_TYPE(cpi)		BITX((cpi)->cpi_std[1].cp_eax, 13, 12)
2237c478bd9Sstevel@tonic-gate #define	CPI_FAMILY(cpi)		BITX((cpi)->cpi_std[1].cp_eax, 11, 8)
2247c478bd9Sstevel@tonic-gate #define	CPI_STEP(cpi)		BITX((cpi)->cpi_std[1].cp_eax, 3, 0)
2257c478bd9Sstevel@tonic-gate #define	CPI_MODEL(cpi)		BITX((cpi)->cpi_std[1].cp_eax, 7, 4)
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate #define	CPI_FEATURES_EDX(cpi)		((cpi)->cpi_std[1].cp_edx)
2287c478bd9Sstevel@tonic-gate #define	CPI_FEATURES_ECX(cpi)		((cpi)->cpi_std[1].cp_ecx)
2297c478bd9Sstevel@tonic-gate #define	CPI_FEATURES_XTD_EDX(cpi)	((cpi)->cpi_extd[1].cp_edx)
2307c478bd9Sstevel@tonic-gate #define	CPI_FEATURES_XTD_ECX(cpi)	((cpi)->cpi_extd[1].cp_ecx)
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate #define	CPI_BRANDID(cpi)	BITX((cpi)->cpi_std[1].cp_ebx, 7, 0)
2337c478bd9Sstevel@tonic-gate #define	CPI_CHUNKS(cpi)		BITX((cpi)->cpi_std[1].cp_ebx, 15, 7)
2347c478bd9Sstevel@tonic-gate #define	CPI_CPU_COUNT(cpi)	BITX((cpi)->cpi_std[1].cp_ebx, 23, 16)
2357c478bd9Sstevel@tonic-gate #define	CPI_APIC_ID(cpi)	BITX((cpi)->cpi_std[1].cp_ebx, 31, 24)
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate #define	CPI_MAXEAX_MAX		0x100		/* sanity control */
2387c478bd9Sstevel@tonic-gate #define	CPI_XMAXEAX_MAX		0x80000100
239d129bde2Sesaxe #define	CPI_FN4_ECX_MAX		0x20		/* sanity: max fn 4 levels */
240b6917abeSmishra #define	CPI_FNB_ECX_MAX		0x20		/* sanity: max fn B levels */
241d129bde2Sesaxe 
242d129bde2Sesaxe /*
243d129bde2Sesaxe  * Function 4 (Deterministic Cache Parameters) macros
244d129bde2Sesaxe  * Defined by Intel Application Note AP-485
245d129bde2Sesaxe  */
246d129bde2Sesaxe #define	CPI_NUM_CORES(regs)		BITX((regs)->cp_eax, 31, 26)
247d129bde2Sesaxe #define	CPI_NTHR_SHR_CACHE(regs)	BITX((regs)->cp_eax, 25, 14)
248d129bde2Sesaxe #define	CPI_FULL_ASSOC_CACHE(regs)	BITX((regs)->cp_eax, 9, 9)
249d129bde2Sesaxe #define	CPI_SELF_INIT_CACHE(regs)	BITX((regs)->cp_eax, 8, 8)
250d129bde2Sesaxe #define	CPI_CACHE_LVL(regs)		BITX((regs)->cp_eax, 7, 5)
251d129bde2Sesaxe #define	CPI_CACHE_TYPE(regs)		BITX((regs)->cp_eax, 4, 0)
252b6917abeSmishra #define	CPI_CPU_LEVEL_TYPE(regs)	BITX((regs)->cp_ecx, 15, 8)
253d129bde2Sesaxe 
254d129bde2Sesaxe #define	CPI_CACHE_WAYS(regs)		BITX((regs)->cp_ebx, 31, 22)
255d129bde2Sesaxe #define	CPI_CACHE_PARTS(regs)		BITX((regs)->cp_ebx, 21, 12)
256d129bde2Sesaxe #define	CPI_CACHE_COH_LN_SZ(regs)	BITX((regs)->cp_ebx, 11, 0)
257d129bde2Sesaxe 
258d129bde2Sesaxe #define	CPI_CACHE_SETS(regs)		BITX((regs)->cp_ecx, 31, 0)
259d129bde2Sesaxe 
260d129bde2Sesaxe #define	CPI_PREFCH_STRIDE(regs)		BITX((regs)->cp_edx, 9, 0)
261d129bde2Sesaxe 
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate /*
2645ff02082Sdmick  * A couple of shorthand macros to identify "later" P6-family chips
2655ff02082Sdmick  * like the Pentium M and Core.  First, the "older" P6-based stuff
2665ff02082Sdmick  * (loosely defined as "pre-Pentium-4"):
2675ff02082Sdmick  * P6, PII, Mobile PII, PII Xeon, PIII, Mobile PIII, PIII Xeon
2685ff02082Sdmick  */
2695ff02082Sdmick 
2705ff02082Sdmick #define	IS_LEGACY_P6(cpi) (			\
2715ff02082Sdmick 	cpi->cpi_family == 6 && 		\
2725ff02082Sdmick 		(cpi->cpi_model == 1 ||		\
2735ff02082Sdmick 		cpi->cpi_model == 3 ||		\
2745ff02082Sdmick 		cpi->cpi_model == 5 ||		\
2755ff02082Sdmick 		cpi->cpi_model == 6 ||		\
2765ff02082Sdmick 		cpi->cpi_model == 7 ||		\
2775ff02082Sdmick 		cpi->cpi_model == 8 ||		\
2785ff02082Sdmick 		cpi->cpi_model == 0xA ||	\
2795ff02082Sdmick 		cpi->cpi_model == 0xB)		\
2805ff02082Sdmick )
2815ff02082Sdmick 
2825ff02082Sdmick /* A "new F6" is everything with family 6 that's not the above */
2835ff02082Sdmick #define	IS_NEW_F6(cpi) ((cpi->cpi_family == 6) && !IS_LEGACY_P6(cpi))
2845ff02082Sdmick 
285bf91205bSksadhukh /* Extended family/model support */
286bf91205bSksadhukh #define	IS_EXTENDED_MODEL_INTEL(cpi) (cpi->cpi_family == 0x6 || \
287bf91205bSksadhukh 	cpi->cpi_family >= 0xf)
288bf91205bSksadhukh 
2895ff02082Sdmick /*
290f98fbcecSbholler  * Info for monitor/mwait idle loop.
291f98fbcecSbholler  *
292f98fbcecSbholler  * See cpuid section of "Intel 64 and IA-32 Architectures Software Developer's
293f98fbcecSbholler  * Manual Volume 2A: Instruction Set Reference, A-M" #25366-022US, November
294f98fbcecSbholler  * 2006.
295f98fbcecSbholler  * See MONITOR/MWAIT section of "AMD64 Architecture Programmer's Manual
296f98fbcecSbholler  * Documentation Updates" #33633, Rev 2.05, December 2006.
297f98fbcecSbholler  */
298f98fbcecSbholler #define	MWAIT_SUPPORT		(0x00000001)	/* mwait supported */
299f98fbcecSbholler #define	MWAIT_EXTENSIONS	(0x00000002)	/* extenstion supported */
300f98fbcecSbholler #define	MWAIT_ECX_INT_ENABLE	(0x00000004)	/* ecx 1 extension supported */
301f98fbcecSbholler #define	MWAIT_SUPPORTED(cpi)	((cpi)->cpi_std[1].cp_ecx & CPUID_INTC_ECX_MON)
302f98fbcecSbholler #define	MWAIT_INT_ENABLE(cpi)	((cpi)->cpi_std[5].cp_ecx & 0x2)
303f98fbcecSbholler #define	MWAIT_EXTENSION(cpi)	((cpi)->cpi_std[5].cp_ecx & 0x1)
304f98fbcecSbholler #define	MWAIT_SIZE_MIN(cpi)	BITX((cpi)->cpi_std[5].cp_eax, 15, 0)
305f98fbcecSbholler #define	MWAIT_SIZE_MAX(cpi)	BITX((cpi)->cpi_std[5].cp_ebx, 15, 0)
306f98fbcecSbholler /*
307f98fbcecSbholler  * Number of sub-cstates for a given c-state.
308f98fbcecSbholler  */
309f98fbcecSbholler #define	MWAIT_NUM_SUBC_STATES(cpi, c_state)			\
310f98fbcecSbholler 	BITX((cpi)->cpi_std[5].cp_edx, c_state + 3, c_state)
311f98fbcecSbholler 
3128a40a695Sgavinm /*
313e4b86885SCheng Sean Ye  * Functions we consune from cpuid_subr.c;  don't publish these in a header
314e4b86885SCheng Sean Ye  * file to try and keep people using the expected cpuid_* interfaces.
3158a40a695Sgavinm  */
316e4b86885SCheng Sean Ye extern uint32_t _cpuid_skt(uint_t, uint_t, uint_t, uint_t);
31789e921d5SKuriakose Kuruvilla extern const char *_cpuid_sktstr(uint_t, uint_t, uint_t, uint_t);
318e4b86885SCheng Sean Ye extern uint32_t _cpuid_chiprev(uint_t, uint_t, uint_t, uint_t);
319e4b86885SCheng Sean Ye extern const char *_cpuid_chiprevstr(uint_t, uint_t, uint_t, uint_t);
320e4b86885SCheng Sean Ye extern uint_t _cpuid_vendorstr_to_vendorcode(char *);
3218a40a695Sgavinm 
3228a40a695Sgavinm /*
323ae115bc7Smrj  * Apply up various platform-dependent restrictions where the
324ae115bc7Smrj  * underlying platform restrictions mean the CPU can be marked
325ae115bc7Smrj  * as less capable than its cpuid instruction would imply.
326ae115bc7Smrj  */
327843e1988Sjohnlev #if defined(__xpv)
328843e1988Sjohnlev static void
329843e1988Sjohnlev platform_cpuid_mangle(uint_t vendor, uint32_t eax, struct cpuid_regs *cp)
330843e1988Sjohnlev {
331843e1988Sjohnlev 	switch (eax) {
332e4b86885SCheng Sean Ye 	case 1: {
333e4b86885SCheng Sean Ye 		uint32_t mcamask = DOMAIN_IS_INITDOMAIN(xen_info) ?
334e4b86885SCheng Sean Ye 		    0 : CPUID_INTC_EDX_MCA;
335843e1988Sjohnlev 		cp->cp_edx &=
336e4b86885SCheng Sean Ye 		    ~(mcamask |
337e4b86885SCheng Sean Ye 		    CPUID_INTC_EDX_PSE |
338843e1988Sjohnlev 		    CPUID_INTC_EDX_VME | CPUID_INTC_EDX_DE |
339843e1988Sjohnlev 		    CPUID_INTC_EDX_SEP | CPUID_INTC_EDX_MTRR |
340843e1988Sjohnlev 		    CPUID_INTC_EDX_PGE | CPUID_INTC_EDX_PAT |
341843e1988Sjohnlev 		    CPUID_AMD_EDX_SYSC | CPUID_INTC_EDX_SEP |
342843e1988Sjohnlev 		    CPUID_INTC_EDX_PSE36 | CPUID_INTC_EDX_HTT);
343843e1988Sjohnlev 		break;
344e4b86885SCheng Sean Ye 	}
345ae115bc7Smrj 
346843e1988Sjohnlev 	case 0x80000001:
347843e1988Sjohnlev 		cp->cp_edx &=
348843e1988Sjohnlev 		    ~(CPUID_AMD_EDX_PSE |
349843e1988Sjohnlev 		    CPUID_INTC_EDX_VME | CPUID_INTC_EDX_DE |
350843e1988Sjohnlev 		    CPUID_AMD_EDX_MTRR | CPUID_AMD_EDX_PGE |
351843e1988Sjohnlev 		    CPUID_AMD_EDX_PAT | CPUID_AMD_EDX_PSE36 |
352843e1988Sjohnlev 		    CPUID_AMD_EDX_SYSC | CPUID_INTC_EDX_SEP |
353843e1988Sjohnlev 		    CPUID_AMD_EDX_TSCP);
354843e1988Sjohnlev 		cp->cp_ecx &= ~CPUID_AMD_ECX_CMP_LGCY;
355843e1988Sjohnlev 		break;
356843e1988Sjohnlev 	default:
357843e1988Sjohnlev 		break;
358843e1988Sjohnlev 	}
359843e1988Sjohnlev 
360843e1988Sjohnlev 	switch (vendor) {
361843e1988Sjohnlev 	case X86_VENDOR_Intel:
362843e1988Sjohnlev 		switch (eax) {
363843e1988Sjohnlev 		case 4:
364843e1988Sjohnlev 			/*
365843e1988Sjohnlev 			 * Zero out the (ncores-per-chip - 1) field
366843e1988Sjohnlev 			 */
367843e1988Sjohnlev 			cp->cp_eax &= 0x03fffffff;
368843e1988Sjohnlev 			break;
369843e1988Sjohnlev 		default:
370843e1988Sjohnlev 			break;
371843e1988Sjohnlev 		}
372843e1988Sjohnlev 		break;
373843e1988Sjohnlev 	case X86_VENDOR_AMD:
374843e1988Sjohnlev 		switch (eax) {
3752ef50f01SJoe Bonasera 
3762ef50f01SJoe Bonasera 		case 0x80000001:
3772ef50f01SJoe Bonasera 			cp->cp_ecx &= ~CPUID_AMD_ECX_CR8D;
3782ef50f01SJoe Bonasera 			break;
3792ef50f01SJoe Bonasera 
380843e1988Sjohnlev 		case 0x80000008:
381843e1988Sjohnlev 			/*
382843e1988Sjohnlev 			 * Zero out the (ncores-per-chip - 1) field
383843e1988Sjohnlev 			 */
384843e1988Sjohnlev 			cp->cp_ecx &= 0xffffff00;
385843e1988Sjohnlev 			break;
386843e1988Sjohnlev 		default:
387843e1988Sjohnlev 			break;
388843e1988Sjohnlev 		}
389843e1988Sjohnlev 		break;
390843e1988Sjohnlev 	default:
391843e1988Sjohnlev 		break;
392843e1988Sjohnlev 	}
393843e1988Sjohnlev }
394843e1988Sjohnlev #else
395ae115bc7Smrj #define	platform_cpuid_mangle(vendor, eax, cp)	/* nothing */
396843e1988Sjohnlev #endif
397ae115bc7Smrj 
398ae115bc7Smrj /*
3997c478bd9Sstevel@tonic-gate  *  Some undocumented ways of patching the results of the cpuid
4007c478bd9Sstevel@tonic-gate  *  instruction to permit running Solaris 10 on future cpus that
4017c478bd9Sstevel@tonic-gate  *  we don't currently support.  Could be set to non-zero values
4027c478bd9Sstevel@tonic-gate  *  via settings in eeprom.
4037c478bd9Sstevel@tonic-gate  */
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate uint32_t cpuid_feature_ecx_include;
4067c478bd9Sstevel@tonic-gate uint32_t cpuid_feature_ecx_exclude;
4077c478bd9Sstevel@tonic-gate uint32_t cpuid_feature_edx_include;
4087c478bd9Sstevel@tonic-gate uint32_t cpuid_feature_edx_exclude;
4097c478bd9Sstevel@tonic-gate 
410ae115bc7Smrj void
411ae115bc7Smrj cpuid_alloc_space(cpu_t *cpu)
412ae115bc7Smrj {
413ae115bc7Smrj 	/*
414ae115bc7Smrj 	 * By convention, cpu0 is the boot cpu, which is set up
415ae115bc7Smrj 	 * before memory allocation is available.  All other cpus get
416ae115bc7Smrj 	 * their cpuid_info struct allocated here.
417ae115bc7Smrj 	 */
418ae115bc7Smrj 	ASSERT(cpu->cpu_id != 0);
419ae115bc7Smrj 	cpu->cpu_m.mcpu_cpi =
420ae115bc7Smrj 	    kmem_zalloc(sizeof (*cpu->cpu_m.mcpu_cpi), KM_SLEEP);
421ae115bc7Smrj }
422ae115bc7Smrj 
423ae115bc7Smrj void
424ae115bc7Smrj cpuid_free_space(cpu_t *cpu)
425ae115bc7Smrj {
426d129bde2Sesaxe 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
427d129bde2Sesaxe 	int i;
428d129bde2Sesaxe 
429ae115bc7Smrj 	ASSERT(cpu->cpu_id != 0);
430d129bde2Sesaxe 
431d129bde2Sesaxe 	/*
432d129bde2Sesaxe 	 * Free up any function 4 related dynamic storage
433d129bde2Sesaxe 	 */
434d129bde2Sesaxe 	for (i = 1; i < cpi->cpi_std_4_size; i++)
435d129bde2Sesaxe 		kmem_free(cpi->cpi_std_4[i], sizeof (struct cpuid_regs));
436d129bde2Sesaxe 	if (cpi->cpi_std_4_size > 0)
437d129bde2Sesaxe 		kmem_free(cpi->cpi_std_4,
438d129bde2Sesaxe 		    cpi->cpi_std_4_size * sizeof (struct cpuid_regs *));
439d129bde2Sesaxe 
440ae115bc7Smrj 	kmem_free(cpu->cpu_m.mcpu_cpi, sizeof (*cpu->cpu_m.mcpu_cpi));
441ae115bc7Smrj }
442ae115bc7Smrj 
443551bc2a6Smrj #if !defined(__xpv)
444551bc2a6Smrj 
445551bc2a6Smrj static void
446b9bfdccdSStuart Maybee determine_platform()
447551bc2a6Smrj {
448551bc2a6Smrj 	struct cpuid_regs cp;
449551bc2a6Smrj 	char *xen_str;
450551bc2a6Smrj 	uint32_t xen_signature[4];
451551bc2a6Smrj 
452*349b53ddSStuart Maybee 	platform_type = HW_NATIVE;
453*349b53ddSStuart Maybee 
454*349b53ddSStuart Maybee 	if (!enable_platform_detection)
455*349b53ddSStuart Maybee 		return;
456*349b53ddSStuart Maybee 
457551bc2a6Smrj 	/*
458551bc2a6Smrj 	 * In a fully virtualized domain, Xen's pseudo-cpuid function
459551bc2a6Smrj 	 * 0x40000000 returns a string representing the Xen signature in
460551bc2a6Smrj 	 * %ebx, %ecx, and %edx.  %eax contains the maximum supported cpuid
461551bc2a6Smrj 	 * function.
462551bc2a6Smrj 	 */
463551bc2a6Smrj 	cp.cp_eax = 0x40000000;
464551bc2a6Smrj 	(void) __cpuid_insn(&cp);
465551bc2a6Smrj 	xen_signature[0] = cp.cp_ebx;
466551bc2a6Smrj 	xen_signature[1] = cp.cp_ecx;
467551bc2a6Smrj 	xen_signature[2] = cp.cp_edx;
468551bc2a6Smrj 	xen_signature[3] = 0;
469551bc2a6Smrj 	xen_str = (char *)xen_signature;
470b9bfdccdSStuart Maybee 	if (strcmp("XenVMMXenVMM", xen_str) == 0 && cp.cp_eax <= 0x40000002) {
471b9bfdccdSStuart Maybee 		platform_type = HW_XEN_HVM;
472b9bfdccdSStuart Maybee 	} else if (vmware_platform()) { /* running under vmware hypervisor? */
473b9bfdccdSStuart Maybee 		platform_type = HW_VMWARE;
474551bc2a6Smrj 	}
475b9bfdccdSStuart Maybee }
476b9bfdccdSStuart Maybee 
477b9bfdccdSStuart Maybee int
478b9bfdccdSStuart Maybee get_hwenv(void)
479b9bfdccdSStuart Maybee {
480*349b53ddSStuart Maybee 	if (platform_type == -1)
481*349b53ddSStuart Maybee 		determine_platform();
482*349b53ddSStuart Maybee 
483b9bfdccdSStuart Maybee 	return (platform_type);
484b9bfdccdSStuart Maybee }
485b9bfdccdSStuart Maybee 
486b9bfdccdSStuart Maybee int
487b9bfdccdSStuart Maybee is_controldom(void)
488b9bfdccdSStuart Maybee {
489b9bfdccdSStuart Maybee 	return (0);
490b9bfdccdSStuart Maybee }
491b9bfdccdSStuart Maybee 
492b9bfdccdSStuart Maybee #else
493b9bfdccdSStuart Maybee 
494b9bfdccdSStuart Maybee int
495b9bfdccdSStuart Maybee get_hwenv(void)
496b9bfdccdSStuart Maybee {
497b9bfdccdSStuart Maybee 	return (HW_XEN_PV);
498b9bfdccdSStuart Maybee }
499b9bfdccdSStuart Maybee 
500b9bfdccdSStuart Maybee int
501b9bfdccdSStuart Maybee is_controldom(void)
502b9bfdccdSStuart Maybee {
503b9bfdccdSStuart Maybee 	return (DOMAIN_IS_INITDOMAIN(xen_info));
504b9bfdccdSStuart Maybee }
505b9bfdccdSStuart Maybee 
506551bc2a6Smrj #endif	/* __xpv */
507551bc2a6Smrj 
5087c478bd9Sstevel@tonic-gate uint_t
5097c478bd9Sstevel@tonic-gate cpuid_pass1(cpu_t *cpu)
5107c478bd9Sstevel@tonic-gate {
5117c478bd9Sstevel@tonic-gate 	uint32_t mask_ecx, mask_edx;
5127c478bd9Sstevel@tonic-gate 	uint_t feature = X86_CPUID;
5137c478bd9Sstevel@tonic-gate 	struct cpuid_info *cpi;
5148949bcd6Sandrei 	struct cpuid_regs *cp;
5157c478bd9Sstevel@tonic-gate 	int xcpuid;
516843e1988Sjohnlev #if !defined(__xpv)
5175b8a6efeSbholler 	extern int idle_cpu_prefer_mwait;
518843e1988Sjohnlev #endif
519ae115bc7Smrj 
52089e921d5SKuriakose Kuruvilla 
52189e921d5SKuriakose Kuruvilla #if !defined(__xpv)
52289e921d5SKuriakose Kuruvilla 	determine_platform();
52389e921d5SKuriakose Kuruvilla #endif
5247c478bd9Sstevel@tonic-gate 	/*
525ae115bc7Smrj 	 * Space statically allocated for cpu0, ensure pointer is set
5267c478bd9Sstevel@tonic-gate 	 */
5277c478bd9Sstevel@tonic-gate 	if (cpu->cpu_id == 0)
528ae115bc7Smrj 		cpu->cpu_m.mcpu_cpi = &cpuid_info0;
529ae115bc7Smrj 	cpi = cpu->cpu_m.mcpu_cpi;
530ae115bc7Smrj 	ASSERT(cpi != NULL);
5317c478bd9Sstevel@tonic-gate 	cp = &cpi->cpi_std[0];
5328949bcd6Sandrei 	cp->cp_eax = 0;
5338949bcd6Sandrei 	cpi->cpi_maxeax = __cpuid_insn(cp);
5347c478bd9Sstevel@tonic-gate 	{
5357c478bd9Sstevel@tonic-gate 		uint32_t *iptr = (uint32_t *)cpi->cpi_vendorstr;
5367c478bd9Sstevel@tonic-gate 		*iptr++ = cp->cp_ebx;
5377c478bd9Sstevel@tonic-gate 		*iptr++ = cp->cp_edx;
5387c478bd9Sstevel@tonic-gate 		*iptr++ = cp->cp_ecx;
5397c478bd9Sstevel@tonic-gate 		*(char *)&cpi->cpi_vendorstr[12] = '\0';
5407c478bd9Sstevel@tonic-gate 	}
5417c478bd9Sstevel@tonic-gate 
542e4b86885SCheng Sean Ye 	cpi->cpi_vendor = _cpuid_vendorstr_to_vendorcode(cpi->cpi_vendorstr);
5437c478bd9Sstevel@tonic-gate 	x86_vendor = cpi->cpi_vendor; /* for compatibility */
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	/*
5467c478bd9Sstevel@tonic-gate 	 * Limit the range in case of weird hardware
5477c478bd9Sstevel@tonic-gate 	 */
5487c478bd9Sstevel@tonic-gate 	if (cpi->cpi_maxeax > CPI_MAXEAX_MAX)
5497c478bd9Sstevel@tonic-gate 		cpi->cpi_maxeax = CPI_MAXEAX_MAX;
5507c478bd9Sstevel@tonic-gate 	if (cpi->cpi_maxeax < 1)
5517c478bd9Sstevel@tonic-gate 		goto pass1_done;
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	cp = &cpi->cpi_std[1];
5548949bcd6Sandrei 	cp->cp_eax = 1;
5558949bcd6Sandrei 	(void) __cpuid_insn(cp);
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	/*
5587c478bd9Sstevel@tonic-gate 	 * Extract identifying constants for easy access.
5597c478bd9Sstevel@tonic-gate 	 */
5607c478bd9Sstevel@tonic-gate 	cpi->cpi_model = CPI_MODEL(cpi);
5617c478bd9Sstevel@tonic-gate 	cpi->cpi_family = CPI_FAMILY(cpi);
5627c478bd9Sstevel@tonic-gate 
5635ff02082Sdmick 	if (cpi->cpi_family == 0xf)
5647c478bd9Sstevel@tonic-gate 		cpi->cpi_family += CPI_FAMILY_XTD(cpi);
5655ff02082Sdmick 
56668c91426Sdmick 	/*
567875b116eSkchow 	 * Beware: AMD uses "extended model" iff base *FAMILY* == 0xf.
56868c91426Sdmick 	 * Intel, and presumably everyone else, uses model == 0xf, as
56968c91426Sdmick 	 * one would expect (max value means possible overflow).  Sigh.
57068c91426Sdmick 	 */
57168c91426Sdmick 
57268c91426Sdmick 	switch (cpi->cpi_vendor) {
573bf91205bSksadhukh 	case X86_VENDOR_Intel:
574bf91205bSksadhukh 		if (IS_EXTENDED_MODEL_INTEL(cpi))
575bf91205bSksadhukh 			cpi->cpi_model += CPI_MODEL_XTD(cpi) << 4;
576447af253Sksadhukh 		break;
57768c91426Sdmick 	case X86_VENDOR_AMD:
578875b116eSkchow 		if (CPI_FAMILY(cpi) == 0xf)
57968c91426Sdmick 			cpi->cpi_model += CPI_MODEL_XTD(cpi) << 4;
58068c91426Sdmick 		break;
58168c91426Sdmick 	default:
5825ff02082Sdmick 		if (cpi->cpi_model == 0xf)
5837c478bd9Sstevel@tonic-gate 			cpi->cpi_model += CPI_MODEL_XTD(cpi) << 4;
58468c91426Sdmick 		break;
58568c91426Sdmick 	}
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	cpi->cpi_step = CPI_STEP(cpi);
5887c478bd9Sstevel@tonic-gate 	cpi->cpi_brandid = CPI_BRANDID(cpi);
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	/*
5917c478bd9Sstevel@tonic-gate 	 * *default* assumptions:
5927c478bd9Sstevel@tonic-gate 	 * - believe %edx feature word
5937c478bd9Sstevel@tonic-gate 	 * - ignore %ecx feature word
5947c478bd9Sstevel@tonic-gate 	 * - 32-bit virtual and physical addressing
5957c478bd9Sstevel@tonic-gate 	 */
5967c478bd9Sstevel@tonic-gate 	mask_edx = 0xffffffff;
5977c478bd9Sstevel@tonic-gate 	mask_ecx = 0;
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 	cpi->cpi_pabits = cpi->cpi_vabits = 32;
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
6027c478bd9Sstevel@tonic-gate 	case X86_VENDOR_Intel:
6037c478bd9Sstevel@tonic-gate 		if (cpi->cpi_family == 5)
6047c478bd9Sstevel@tonic-gate 			x86_type = X86_TYPE_P5;
6055ff02082Sdmick 		else if (IS_LEGACY_P6(cpi)) {
6067c478bd9Sstevel@tonic-gate 			x86_type = X86_TYPE_P6;
6077c478bd9Sstevel@tonic-gate 			pentiumpro_bug4046376 = 1;
6087c478bd9Sstevel@tonic-gate 			pentiumpro_bug4064495 = 1;
6097c478bd9Sstevel@tonic-gate 			/*
6107c478bd9Sstevel@tonic-gate 			 * Clear the SEP bit when it was set erroneously
6117c478bd9Sstevel@tonic-gate 			 */
6127c478bd9Sstevel@tonic-gate 			if (cpi->cpi_model < 3 && cpi->cpi_step < 3)
6137c478bd9Sstevel@tonic-gate 				cp->cp_edx &= ~CPUID_INTC_EDX_SEP;
6145ff02082Sdmick 		} else if (IS_NEW_F6(cpi) || cpi->cpi_family == 0xf) {
6157c478bd9Sstevel@tonic-gate 			x86_type = X86_TYPE_P4;
6167c478bd9Sstevel@tonic-gate 			/*
6177c478bd9Sstevel@tonic-gate 			 * We don't currently depend on any of the %ecx
6187c478bd9Sstevel@tonic-gate 			 * features until Prescott, so we'll only check
6197c478bd9Sstevel@tonic-gate 			 * this from P4 onwards.  We might want to revisit
6207c478bd9Sstevel@tonic-gate 			 * that idea later.
6217c478bd9Sstevel@tonic-gate 			 */
6227c478bd9Sstevel@tonic-gate 			mask_ecx = 0xffffffff;
6237c478bd9Sstevel@tonic-gate 		} else if (cpi->cpi_family > 0xf)
6247c478bd9Sstevel@tonic-gate 			mask_ecx = 0xffffffff;
6257c622d23Sbholler 		/*
6267c622d23Sbholler 		 * We don't support MONITOR/MWAIT if leaf 5 is not available
6277c622d23Sbholler 		 * to obtain the monitor linesize.
6287c622d23Sbholler 		 */
6297c622d23Sbholler 		if (cpi->cpi_maxeax < 5)
6307c622d23Sbholler 			mask_ecx &= ~CPUID_INTC_ECX_MON;
6317c478bd9Sstevel@tonic-gate 		break;
6327c478bd9Sstevel@tonic-gate 	case X86_VENDOR_IntelClone:
6337c478bd9Sstevel@tonic-gate 	default:
6347c478bd9Sstevel@tonic-gate 		break;
6357c478bd9Sstevel@tonic-gate 	case X86_VENDOR_AMD:
6367c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_108)
6377c478bd9Sstevel@tonic-gate 		if (cpi->cpi_family == 0xf && cpi->cpi_model == 0xe) {
6387c478bd9Sstevel@tonic-gate 			cp->cp_eax = (0xf0f & cp->cp_eax) | 0xc0;
6397c478bd9Sstevel@tonic-gate 			cpi->cpi_model = 0xc;
6407c478bd9Sstevel@tonic-gate 		} else
6417c478bd9Sstevel@tonic-gate #endif
6427c478bd9Sstevel@tonic-gate 		if (cpi->cpi_family == 5) {
6437c478bd9Sstevel@tonic-gate 			/*
6447c478bd9Sstevel@tonic-gate 			 * AMD K5 and K6
6457c478bd9Sstevel@tonic-gate 			 *
6467c478bd9Sstevel@tonic-gate 			 * These CPUs have an incomplete implementation
6477c478bd9Sstevel@tonic-gate 			 * of MCA/MCE which we mask away.
6487c478bd9Sstevel@tonic-gate 			 */
6498949bcd6Sandrei 			mask_edx &= ~(CPUID_INTC_EDX_MCE | CPUID_INTC_EDX_MCA);
6508949bcd6Sandrei 
6517c478bd9Sstevel@tonic-gate 			/*
6527c478bd9Sstevel@tonic-gate 			 * Model 0 uses the wrong (APIC) bit
6537c478bd9Sstevel@tonic-gate 			 * to indicate PGE.  Fix it here.
6547c478bd9Sstevel@tonic-gate 			 */
6558949bcd6Sandrei 			if (cpi->cpi_model == 0) {
6567c478bd9Sstevel@tonic-gate 				if (cp->cp_edx & 0x200) {
6577c478bd9Sstevel@tonic-gate 					cp->cp_edx &= ~0x200;
6587c478bd9Sstevel@tonic-gate 					cp->cp_edx |= CPUID_INTC_EDX_PGE;
6597c478bd9Sstevel@tonic-gate 				}
6607c478bd9Sstevel@tonic-gate 			}
6618949bcd6Sandrei 
6628949bcd6Sandrei 			/*
6638949bcd6Sandrei 			 * Early models had problems w/ MMX; disable.
6648949bcd6Sandrei 			 */
6658949bcd6Sandrei 			if (cpi->cpi_model < 6)
6668949bcd6Sandrei 				mask_edx &= ~CPUID_INTC_EDX_MMX;
6678949bcd6Sandrei 		}
6688949bcd6Sandrei 
6698949bcd6Sandrei 		/*
6708949bcd6Sandrei 		 * For newer families, SSE3 and CX16, at least, are valid;
6718949bcd6Sandrei 		 * enable all
6728949bcd6Sandrei 		 */
6738949bcd6Sandrei 		if (cpi->cpi_family >= 0xf)
6748949bcd6Sandrei 			mask_ecx = 0xffffffff;
6757c622d23Sbholler 		/*
6767c622d23Sbholler 		 * We don't support MONITOR/MWAIT if leaf 5 is not available
6777c622d23Sbholler 		 * to obtain the monitor linesize.
6787c622d23Sbholler 		 */
6797c622d23Sbholler 		if (cpi->cpi_maxeax < 5)
6807c622d23Sbholler 			mask_ecx &= ~CPUID_INTC_ECX_MON;
6815b8a6efeSbholler 
682843e1988Sjohnlev #if !defined(__xpv)
6835b8a6efeSbholler 		/*
6845b8a6efeSbholler 		 * Do not use MONITOR/MWAIT to halt in the idle loop on any AMD
6855b8a6efeSbholler 		 * processors.  AMD does not intend MWAIT to be used in the cpu
6865b8a6efeSbholler 		 * idle loop on current and future processors.  10h and future
6875b8a6efeSbholler 		 * AMD processors use more power in MWAIT than HLT.
6885b8a6efeSbholler 		 * Pre-family-10h Opterons do not have the MWAIT instruction.
6895b8a6efeSbholler 		 */
6905b8a6efeSbholler 		idle_cpu_prefer_mwait = 0;
691843e1988Sjohnlev #endif
6925b8a6efeSbholler 
6937c478bd9Sstevel@tonic-gate 		break;
6947c478bd9Sstevel@tonic-gate 	case X86_VENDOR_TM:
6957c478bd9Sstevel@tonic-gate 		/*
6967c478bd9Sstevel@tonic-gate 		 * workaround the NT workaround in CMS 4.1
6977c478bd9Sstevel@tonic-gate 		 */
6987c478bd9Sstevel@tonic-gate 		if (cpi->cpi_family == 5 && cpi->cpi_model == 4 &&
6997c478bd9Sstevel@tonic-gate 		    (cpi->cpi_step == 2 || cpi->cpi_step == 3))
7007c478bd9Sstevel@tonic-gate 			cp->cp_edx |= CPUID_INTC_EDX_CX8;
7017c478bd9Sstevel@tonic-gate 		break;
7027c478bd9Sstevel@tonic-gate 	case X86_VENDOR_Centaur:
7037c478bd9Sstevel@tonic-gate 		/*
7047c478bd9Sstevel@tonic-gate 		 * workaround the NT workarounds again
7057c478bd9Sstevel@tonic-gate 		 */
7067c478bd9Sstevel@tonic-gate 		if (cpi->cpi_family == 6)
7077c478bd9Sstevel@tonic-gate 			cp->cp_edx |= CPUID_INTC_EDX_CX8;
7087c478bd9Sstevel@tonic-gate 		break;
7097c478bd9Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
7107c478bd9Sstevel@tonic-gate 		/*
7117c478bd9Sstevel@tonic-gate 		 * We rely heavily on the probing in locore
7127c478bd9Sstevel@tonic-gate 		 * to actually figure out what parts, if any,
7137c478bd9Sstevel@tonic-gate 		 * of the Cyrix cpuid instruction to believe.
7147c478bd9Sstevel@tonic-gate 		 */
7157c478bd9Sstevel@tonic-gate 		switch (x86_type) {
7167c478bd9Sstevel@tonic-gate 		case X86_TYPE_CYRIX_486:
7177c478bd9Sstevel@tonic-gate 			mask_edx = 0;
7187c478bd9Sstevel@tonic-gate 			break;
7197c478bd9Sstevel@tonic-gate 		case X86_TYPE_CYRIX_6x86:
7207c478bd9Sstevel@tonic-gate 			mask_edx = 0;
7217c478bd9Sstevel@tonic-gate 			break;
7227c478bd9Sstevel@tonic-gate 		case X86_TYPE_CYRIX_6x86L:
7237c478bd9Sstevel@tonic-gate 			mask_edx =
7247c478bd9Sstevel@tonic-gate 			    CPUID_INTC_EDX_DE |
7257c478bd9Sstevel@tonic-gate 			    CPUID_INTC_EDX_CX8;
7267c478bd9Sstevel@tonic-gate 			break;
7277c478bd9Sstevel@tonic-gate 		case X86_TYPE_CYRIX_6x86MX:
7287c478bd9Sstevel@tonic-gate 			mask_edx =
7297c478bd9Sstevel@tonic-gate 			    CPUID_INTC_EDX_DE |
7307c478bd9Sstevel@tonic-gate 			    CPUID_INTC_EDX_MSR |
7317c478bd9Sstevel@tonic-gate 			    CPUID_INTC_EDX_CX8 |
7327c478bd9Sstevel@tonic-gate 			    CPUID_INTC_EDX_PGE |
7337c478bd9Sstevel@tonic-gate 			    CPUID_INTC_EDX_CMOV |
7347c478bd9Sstevel@tonic-gate 			    CPUID_INTC_EDX_MMX;
7357c478bd9Sstevel@tonic-gate 			break;
7367c478bd9Sstevel@tonic-gate 		case X86_TYPE_CYRIX_GXm:
7377c478bd9Sstevel@tonic-gate 			mask_edx =
7387c478bd9Sstevel@tonic-gate 			    CPUID_INTC_EDX_MSR |
7397c478bd9Sstevel@tonic-gate 			    CPUID_INTC_EDX_CX8 |
7407c478bd9Sstevel@tonic-gate 			    CPUID_INTC_EDX_CMOV |
7417c478bd9Sstevel@tonic-gate 			    CPUID_INTC_EDX_MMX;
7427c478bd9Sstevel@tonic-gate 			break;
7437c478bd9Sstevel@tonic-gate 		case X86_TYPE_CYRIX_MediaGX:
7447c478bd9Sstevel@tonic-gate 			break;
7457c478bd9Sstevel@tonic-gate 		case X86_TYPE_CYRIX_MII:
7467c478bd9Sstevel@tonic-gate 		case X86_TYPE_VIA_CYRIX_III:
7477c478bd9Sstevel@tonic-gate 			mask_edx =
7487c478bd9Sstevel@tonic-gate 			    CPUID_INTC_EDX_DE |
7497c478bd9Sstevel@tonic-gate 			    CPUID_INTC_EDX_TSC |
7507c478bd9Sstevel@tonic-gate 			    CPUID_INTC_EDX_MSR |
7517c478bd9Sstevel@tonic-gate 			    CPUID_INTC_EDX_CX8 |
7527c478bd9Sstevel@tonic-gate 			    CPUID_INTC_EDX_PGE |
7537c478bd9Sstevel@tonic-gate 			    CPUID_INTC_EDX_CMOV |
7547c478bd9Sstevel@tonic-gate 			    CPUID_INTC_EDX_MMX;
7557c478bd9Sstevel@tonic-gate 			break;
7567c478bd9Sstevel@tonic-gate 		default:
7577c478bd9Sstevel@tonic-gate 			break;
7587c478bd9Sstevel@tonic-gate 		}
7597c478bd9Sstevel@tonic-gate 		break;
7607c478bd9Sstevel@tonic-gate 	}
7617c478bd9Sstevel@tonic-gate 
762843e1988Sjohnlev #if defined(__xpv)
763843e1988Sjohnlev 	/*
764843e1988Sjohnlev 	 * Do not support MONITOR/MWAIT under a hypervisor
765843e1988Sjohnlev 	 */
766843e1988Sjohnlev 	mask_ecx &= ~CPUID_INTC_ECX_MON;
767843e1988Sjohnlev #endif	/* __xpv */
768843e1988Sjohnlev 
7697c478bd9Sstevel@tonic-gate 	/*
7707c478bd9Sstevel@tonic-gate 	 * Now we've figured out the masks that determine
7717c478bd9Sstevel@tonic-gate 	 * which bits we choose to believe, apply the masks
7727c478bd9Sstevel@tonic-gate 	 * to the feature words, then map the kernel's view
7737c478bd9Sstevel@tonic-gate 	 * of these feature words into its feature word.
7747c478bd9Sstevel@tonic-gate 	 */
7757c478bd9Sstevel@tonic-gate 	cp->cp_edx &= mask_edx;
7767c478bd9Sstevel@tonic-gate 	cp->cp_ecx &= mask_ecx;
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	/*
779ae115bc7Smrj 	 * apply any platform restrictions (we don't call this
780ae115bc7Smrj 	 * immediately after __cpuid_insn here, because we need the
781ae115bc7Smrj 	 * workarounds applied above first)
7827c478bd9Sstevel@tonic-gate 	 */
783ae115bc7Smrj 	platform_cpuid_mangle(cpi->cpi_vendor, 1, cp);
7847c478bd9Sstevel@tonic-gate 
785ae115bc7Smrj 	/*
786ae115bc7Smrj 	 * fold in overrides from the "eeprom" mechanism
787ae115bc7Smrj 	 */
7887c478bd9Sstevel@tonic-gate 	cp->cp_edx |= cpuid_feature_edx_include;
7897c478bd9Sstevel@tonic-gate 	cp->cp_edx &= ~cpuid_feature_edx_exclude;
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	cp->cp_ecx |= cpuid_feature_ecx_include;
7927c478bd9Sstevel@tonic-gate 	cp->cp_ecx &= ~cpuid_feature_ecx_exclude;
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_PSE)
7957c478bd9Sstevel@tonic-gate 		feature |= X86_LARGEPAGE;
7967c478bd9Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_TSC)
7977c478bd9Sstevel@tonic-gate 		feature |= X86_TSC;
7987c478bd9Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_MSR)
7997c478bd9Sstevel@tonic-gate 		feature |= X86_MSR;
8007c478bd9Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_MTRR)
8017c478bd9Sstevel@tonic-gate 		feature |= X86_MTRR;
8027c478bd9Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_PGE)
8037c478bd9Sstevel@tonic-gate 		feature |= X86_PGE;
8047c478bd9Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_CMOV)
8057c478bd9Sstevel@tonic-gate 		feature |= X86_CMOV;
8067c478bd9Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_MMX)
8077c478bd9Sstevel@tonic-gate 		feature |= X86_MMX;
8087c478bd9Sstevel@tonic-gate 	if ((cp->cp_edx & CPUID_INTC_EDX_MCE) != 0 &&
8097c478bd9Sstevel@tonic-gate 	    (cp->cp_edx & CPUID_INTC_EDX_MCA) != 0)
8107c478bd9Sstevel@tonic-gate 		feature |= X86_MCA;
8117c478bd9Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_PAE)
8127c478bd9Sstevel@tonic-gate 		feature |= X86_PAE;
8137c478bd9Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_CX8)
8147c478bd9Sstevel@tonic-gate 		feature |= X86_CX8;
8157c478bd9Sstevel@tonic-gate 	if (cp->cp_ecx & CPUID_INTC_ECX_CX16)
8167c478bd9Sstevel@tonic-gate 		feature |= X86_CX16;
8177c478bd9Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_PAT)
8187c478bd9Sstevel@tonic-gate 		feature |= X86_PAT;
8197c478bd9Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_SEP)
8207c478bd9Sstevel@tonic-gate 		feature |= X86_SEP;
8217c478bd9Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_FXSR) {
8227c478bd9Sstevel@tonic-gate 		/*
8237c478bd9Sstevel@tonic-gate 		 * In our implementation, fxsave/fxrstor
8247c478bd9Sstevel@tonic-gate 		 * are prerequisites before we'll even
8257c478bd9Sstevel@tonic-gate 		 * try and do SSE things.
8267c478bd9Sstevel@tonic-gate 		 */
8277c478bd9Sstevel@tonic-gate 		if (cp->cp_edx & CPUID_INTC_EDX_SSE)
8287c478bd9Sstevel@tonic-gate 			feature |= X86_SSE;
8297c478bd9Sstevel@tonic-gate 		if (cp->cp_edx & CPUID_INTC_EDX_SSE2)
8307c478bd9Sstevel@tonic-gate 			feature |= X86_SSE2;
8317c478bd9Sstevel@tonic-gate 		if (cp->cp_ecx & CPUID_INTC_ECX_SSE3)
8327c478bd9Sstevel@tonic-gate 			feature |= X86_SSE3;
833d0f8ff6eSkk208521 		if (cpi->cpi_vendor == X86_VENDOR_Intel) {
834d0f8ff6eSkk208521 			if (cp->cp_ecx & CPUID_INTC_ECX_SSSE3)
835d0f8ff6eSkk208521 				feature |= X86_SSSE3;
836d0f8ff6eSkk208521 			if (cp->cp_ecx & CPUID_INTC_ECX_SSE4_1)
837d0f8ff6eSkk208521 				feature |= X86_SSE4_1;
838d0f8ff6eSkk208521 			if (cp->cp_ecx & CPUID_INTC_ECX_SSE4_2)
839d0f8ff6eSkk208521 				feature |= X86_SSE4_2;
840a50a8b93SKuriakose Kuruvilla 			if (cp->cp_ecx & CPUID_INTC_ECX_AES)
841a50a8b93SKuriakose Kuruvilla 				feature |= X86_AES;
842d0f8ff6eSkk208521 		}
8437c478bd9Sstevel@tonic-gate 	}
8447c478bd9Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_DE)
845ae115bc7Smrj 		feature |= X86_DE;
8461d1a3942SBill Holler #if !defined(__xpv)
847f98fbcecSbholler 	if (cp->cp_ecx & CPUID_INTC_ECX_MON) {
8481d1a3942SBill Holler 
8491d1a3942SBill Holler 		/*
8501d1a3942SBill Holler 		 * We require the CLFLUSH instruction for erratum workaround
8511d1a3942SBill Holler 		 * to use MONITOR/MWAIT.
8521d1a3942SBill Holler 		 */
8531d1a3942SBill Holler 		if (cp->cp_edx & CPUID_INTC_EDX_CLFSH) {
854f98fbcecSbholler 			cpi->cpi_mwait.support |= MWAIT_SUPPORT;
855f98fbcecSbholler 			feature |= X86_MWAIT;
8561d1a3942SBill Holler 		} else {
8571d1a3942SBill Holler 			extern int idle_cpu_assert_cflush_monitor;
8581d1a3942SBill Holler 
8591d1a3942SBill Holler 			/*
8601d1a3942SBill Holler 			 * All processors we are aware of which have
8611d1a3942SBill Holler 			 * MONITOR/MWAIT also have CLFLUSH.
8621d1a3942SBill Holler 			 */
8631d1a3942SBill Holler 			if (idle_cpu_assert_cflush_monitor) {
8641d1a3942SBill Holler 				ASSERT((cp->cp_ecx & CPUID_INTC_ECX_MON) &&
8651d1a3942SBill Holler 				    (cp->cp_edx & CPUID_INTC_EDX_CLFSH));
866f98fbcecSbholler 			}
8671d1a3942SBill Holler 		}
8681d1a3942SBill Holler 	}
8691d1a3942SBill Holler #endif	/* __xpv */
8707c478bd9Sstevel@tonic-gate 
87186c1f4dcSVikram Hegde 	/*
87286c1f4dcSVikram Hegde 	 * Only need it first time, rest of the cpus would follow suite.
87386c1f4dcSVikram Hegde 	 * we only capture this for the bootcpu.
87486c1f4dcSVikram Hegde 	 */
87586c1f4dcSVikram Hegde 	if (cp->cp_edx & CPUID_INTC_EDX_CLFSH) {
87686c1f4dcSVikram Hegde 		feature |= X86_CLFSH;
87786c1f4dcSVikram Hegde 		x86_clflush_size = (BITX(cp->cp_ebx, 15, 8) * 8);
87886c1f4dcSVikram Hegde 	}
87986c1f4dcSVikram Hegde 
8807c478bd9Sstevel@tonic-gate 	if (feature & X86_PAE)
8817c478bd9Sstevel@tonic-gate 		cpi->cpi_pabits = 36;
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 	/*
8847c478bd9Sstevel@tonic-gate 	 * Hyperthreading configuration is slightly tricky on Intel
8857c478bd9Sstevel@tonic-gate 	 * and pure clones, and even trickier on AMD.
8867c478bd9Sstevel@tonic-gate 	 *
8877c478bd9Sstevel@tonic-gate 	 * (AMD chose to set the HTT bit on their CMP processors,
8887c478bd9Sstevel@tonic-gate 	 * even though they're not actually hyperthreaded.  Thus it
8897c478bd9Sstevel@tonic-gate 	 * takes a bit more work to figure out what's really going
890ae115bc7Smrj 	 * on ... see the handling of the CMP_LGCY bit below)
8917c478bd9Sstevel@tonic-gate 	 */
8927c478bd9Sstevel@tonic-gate 	if (cp->cp_edx & CPUID_INTC_EDX_HTT) {
8937c478bd9Sstevel@tonic-gate 		cpi->cpi_ncpu_per_chip = CPI_CPU_COUNT(cpi);
8947c478bd9Sstevel@tonic-gate 		if (cpi->cpi_ncpu_per_chip > 1)
8957c478bd9Sstevel@tonic-gate 			feature |= X86_HTT;
8968949bcd6Sandrei 	} else {
8978949bcd6Sandrei 		cpi->cpi_ncpu_per_chip = 1;
8987c478bd9Sstevel@tonic-gate 	}
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 	/*
9017c478bd9Sstevel@tonic-gate 	 * Work on the "extended" feature information, doing
9027c478bd9Sstevel@tonic-gate 	 * some basic initialization for cpuid_pass2()
9037c478bd9Sstevel@tonic-gate 	 */
9047c478bd9Sstevel@tonic-gate 	xcpuid = 0;
9057c478bd9Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
9067c478bd9Sstevel@tonic-gate 	case X86_VENDOR_Intel:
9075ff02082Sdmick 		if (IS_NEW_F6(cpi) || cpi->cpi_family >= 0xf)
9087c478bd9Sstevel@tonic-gate 			xcpuid++;
9097c478bd9Sstevel@tonic-gate 		break;
9107c478bd9Sstevel@tonic-gate 	case X86_VENDOR_AMD:
9117c478bd9Sstevel@tonic-gate 		if (cpi->cpi_family > 5 ||
9127c478bd9Sstevel@tonic-gate 		    (cpi->cpi_family == 5 && cpi->cpi_model >= 1))
9137c478bd9Sstevel@tonic-gate 			xcpuid++;
9147c478bd9Sstevel@tonic-gate 		break;
9157c478bd9Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
9167c478bd9Sstevel@tonic-gate 		/*
9177c478bd9Sstevel@tonic-gate 		 * Only these Cyrix CPUs are -known- to support
9187c478bd9Sstevel@tonic-gate 		 * extended cpuid operations.
9197c478bd9Sstevel@tonic-gate 		 */
9207c478bd9Sstevel@tonic-gate 		if (x86_type == X86_TYPE_VIA_CYRIX_III ||
9217c478bd9Sstevel@tonic-gate 		    x86_type == X86_TYPE_CYRIX_GXm)
9227c478bd9Sstevel@tonic-gate 			xcpuid++;
9237c478bd9Sstevel@tonic-gate 		break;
9247c478bd9Sstevel@tonic-gate 	case X86_VENDOR_Centaur:
9257c478bd9Sstevel@tonic-gate 	case X86_VENDOR_TM:
9267c478bd9Sstevel@tonic-gate 	default:
9277c478bd9Sstevel@tonic-gate 		xcpuid++;
9287c478bd9Sstevel@tonic-gate 		break;
9297c478bd9Sstevel@tonic-gate 	}
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 	if (xcpuid) {
9327c478bd9Sstevel@tonic-gate 		cp = &cpi->cpi_extd[0];
9338949bcd6Sandrei 		cp->cp_eax = 0x80000000;
9348949bcd6Sandrei 		cpi->cpi_xmaxeax = __cpuid_insn(cp);
9357c478bd9Sstevel@tonic-gate 	}
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	if (cpi->cpi_xmaxeax & 0x80000000) {
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 		if (cpi->cpi_xmaxeax > CPI_XMAXEAX_MAX)
9407c478bd9Sstevel@tonic-gate 			cpi->cpi_xmaxeax = CPI_XMAXEAX_MAX;
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 		switch (cpi->cpi_vendor) {
9437c478bd9Sstevel@tonic-gate 		case X86_VENDOR_Intel:
9447c478bd9Sstevel@tonic-gate 		case X86_VENDOR_AMD:
9457c478bd9Sstevel@tonic-gate 			if (cpi->cpi_xmaxeax < 0x80000001)
9467c478bd9Sstevel@tonic-gate 				break;
9477c478bd9Sstevel@tonic-gate 			cp = &cpi->cpi_extd[1];
9488949bcd6Sandrei 			cp->cp_eax = 0x80000001;
9498949bcd6Sandrei 			(void) __cpuid_insn(cp);
950ae115bc7Smrj 
9517c478bd9Sstevel@tonic-gate 			if (cpi->cpi_vendor == X86_VENDOR_AMD &&
9527c478bd9Sstevel@tonic-gate 			    cpi->cpi_family == 5 &&
9537c478bd9Sstevel@tonic-gate 			    cpi->cpi_model == 6 &&
9547c478bd9Sstevel@tonic-gate 			    cpi->cpi_step == 6) {
9557c478bd9Sstevel@tonic-gate 				/*
9567c478bd9Sstevel@tonic-gate 				 * K6 model 6 uses bit 10 to indicate SYSC
9577c478bd9Sstevel@tonic-gate 				 * Later models use bit 11. Fix it here.
9587c478bd9Sstevel@tonic-gate 				 */
9597c478bd9Sstevel@tonic-gate 				if (cp->cp_edx & 0x400) {
9607c478bd9Sstevel@tonic-gate 					cp->cp_edx &= ~0x400;
9617c478bd9Sstevel@tonic-gate 					cp->cp_edx |= CPUID_AMD_EDX_SYSC;
9627c478bd9Sstevel@tonic-gate 				}
9637c478bd9Sstevel@tonic-gate 			}
9647c478bd9Sstevel@tonic-gate 
965ae115bc7Smrj 			platform_cpuid_mangle(cpi->cpi_vendor, 0x80000001, cp);
966ae115bc7Smrj 
9677c478bd9Sstevel@tonic-gate 			/*
9687c478bd9Sstevel@tonic-gate 			 * Compute the additions to the kernel's feature word.
9697c478bd9Sstevel@tonic-gate 			 */
9707c478bd9Sstevel@tonic-gate 			if (cp->cp_edx & CPUID_AMD_EDX_NX)
9717c478bd9Sstevel@tonic-gate 				feature |= X86_NX;
9727c478bd9Sstevel@tonic-gate 
97319397407SSherry Moore 			/*
97419397407SSherry Moore 			 * Regardless whether or not we boot 64-bit,
97519397407SSherry Moore 			 * we should have a way to identify whether
97619397407SSherry Moore 			 * the CPU is capable of running 64-bit.
97719397407SSherry Moore 			 */
97819397407SSherry Moore 			if (cp->cp_edx & CPUID_AMD_EDX_LM)
97919397407SSherry Moore 				feature |= X86_64;
98019397407SSherry Moore 
98102bc52beSkchow #if defined(__amd64)
98202bc52beSkchow 			/* 1 GB large page - enable only for 64 bit kernel */
98302bc52beSkchow 			if (cp->cp_edx & CPUID_AMD_EDX_1GPG)
98402bc52beSkchow 				feature |= X86_1GPG;
98502bc52beSkchow #endif
98602bc52beSkchow 
987f8801251Skk208521 			if ((cpi->cpi_vendor == X86_VENDOR_AMD) &&
988f8801251Skk208521 			    (cpi->cpi_std[1].cp_edx & CPUID_INTC_EDX_FXSR) &&
989f8801251Skk208521 			    (cp->cp_ecx & CPUID_AMD_ECX_SSE4A))
990f8801251Skk208521 				feature |= X86_SSE4A;
991f8801251Skk208521 
9927c478bd9Sstevel@tonic-gate 			/*
993ae115bc7Smrj 			 * If both the HTT and CMP_LGCY bits are set,
9948949bcd6Sandrei 			 * then we're not actually HyperThreaded.  Read
9958949bcd6Sandrei 			 * "AMD CPUID Specification" for more details.
9967c478bd9Sstevel@tonic-gate 			 */
9977c478bd9Sstevel@tonic-gate 			if (cpi->cpi_vendor == X86_VENDOR_AMD &&
9988949bcd6Sandrei 			    (feature & X86_HTT) &&
999ae115bc7Smrj 			    (cp->cp_ecx & CPUID_AMD_ECX_CMP_LGCY)) {
10007c478bd9Sstevel@tonic-gate 				feature &= ~X86_HTT;
10018949bcd6Sandrei 				feature |= X86_CMP;
10028949bcd6Sandrei 			}
1003ae115bc7Smrj #if defined(__amd64)
10047c478bd9Sstevel@tonic-gate 			/*
10057c478bd9Sstevel@tonic-gate 			 * It's really tricky to support syscall/sysret in
10067c478bd9Sstevel@tonic-gate 			 * the i386 kernel; we rely on sysenter/sysexit
10077c478bd9Sstevel@tonic-gate 			 * instead.  In the amd64 kernel, things are -way-
10087c478bd9Sstevel@tonic-gate 			 * better.
10097c478bd9Sstevel@tonic-gate 			 */
10107c478bd9Sstevel@tonic-gate 			if (cp->cp_edx & CPUID_AMD_EDX_SYSC)
10117c478bd9Sstevel@tonic-gate 				feature |= X86_ASYSC;
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 			/*
10147c478bd9Sstevel@tonic-gate 			 * While we're thinking about system calls, note
10157c478bd9Sstevel@tonic-gate 			 * that AMD processors don't support sysenter
10167c478bd9Sstevel@tonic-gate 			 * in long mode at all, so don't try to program them.
10177c478bd9Sstevel@tonic-gate 			 */
10187c478bd9Sstevel@tonic-gate 			if (x86_vendor == X86_VENDOR_AMD)
10197c478bd9Sstevel@tonic-gate 				feature &= ~X86_SEP;
10207c478bd9Sstevel@tonic-gate #endif
1021d36ea5d8Ssudheer 			if (cp->cp_edx & CPUID_AMD_EDX_TSCP)
1022ae115bc7Smrj 				feature |= X86_TSCP;
10237c478bd9Sstevel@tonic-gate 			break;
10247c478bd9Sstevel@tonic-gate 		default:
10257c478bd9Sstevel@tonic-gate 			break;
10267c478bd9Sstevel@tonic-gate 		}
10277c478bd9Sstevel@tonic-gate 
10288949bcd6Sandrei 		/*
10298949bcd6Sandrei 		 * Get CPUID data about processor cores and hyperthreads.
10308949bcd6Sandrei 		 */
10317c478bd9Sstevel@tonic-gate 		switch (cpi->cpi_vendor) {
10327c478bd9Sstevel@tonic-gate 		case X86_VENDOR_Intel:
10338949bcd6Sandrei 			if (cpi->cpi_maxeax >= 4) {
10348949bcd6Sandrei 				cp = &cpi->cpi_std[4];
10358949bcd6Sandrei 				cp->cp_eax = 4;
10368949bcd6Sandrei 				cp->cp_ecx = 0;
10378949bcd6Sandrei 				(void) __cpuid_insn(cp);
1038ae115bc7Smrj 				platform_cpuid_mangle(cpi->cpi_vendor, 4, cp);
10398949bcd6Sandrei 			}
10408949bcd6Sandrei 			/*FALLTHROUGH*/
10417c478bd9Sstevel@tonic-gate 		case X86_VENDOR_AMD:
10427c478bd9Sstevel@tonic-gate 			if (cpi->cpi_xmaxeax < 0x80000008)
10437c478bd9Sstevel@tonic-gate 				break;
10447c478bd9Sstevel@tonic-gate 			cp = &cpi->cpi_extd[8];
10458949bcd6Sandrei 			cp->cp_eax = 0x80000008;
10468949bcd6Sandrei 			(void) __cpuid_insn(cp);
1047ae115bc7Smrj 			platform_cpuid_mangle(cpi->cpi_vendor, 0x80000008, cp);
1048ae115bc7Smrj 
10497c478bd9Sstevel@tonic-gate 			/*
10507c478bd9Sstevel@tonic-gate 			 * Virtual and physical address limits from
10517c478bd9Sstevel@tonic-gate 			 * cpuid override previously guessed values.
10527c478bd9Sstevel@tonic-gate 			 */
10537c478bd9Sstevel@tonic-gate 			cpi->cpi_pabits = BITX(cp->cp_eax, 7, 0);
10547c478bd9Sstevel@tonic-gate 			cpi->cpi_vabits = BITX(cp->cp_eax, 15, 8);
10557c478bd9Sstevel@tonic-gate 			break;
10567c478bd9Sstevel@tonic-gate 		default:
10577c478bd9Sstevel@tonic-gate 			break;
10587c478bd9Sstevel@tonic-gate 		}
10598949bcd6Sandrei 
1060d129bde2Sesaxe 		/*
1061d129bde2Sesaxe 		 * Derive the number of cores per chip
1062d129bde2Sesaxe 		 */
10638949bcd6Sandrei 		switch (cpi->cpi_vendor) {
10648949bcd6Sandrei 		case X86_VENDOR_Intel:
10658949bcd6Sandrei 			if (cpi->cpi_maxeax < 4) {
10668949bcd6Sandrei 				cpi->cpi_ncore_per_chip = 1;
10678949bcd6Sandrei 				break;
10688949bcd6Sandrei 			} else {
10698949bcd6Sandrei 				cpi->cpi_ncore_per_chip =
10708949bcd6Sandrei 				    BITX((cpi)->cpi_std[4].cp_eax, 31, 26) + 1;
10718949bcd6Sandrei 			}
10728949bcd6Sandrei 			break;
10738949bcd6Sandrei 		case X86_VENDOR_AMD:
10748949bcd6Sandrei 			if (cpi->cpi_xmaxeax < 0x80000008) {
10758949bcd6Sandrei 				cpi->cpi_ncore_per_chip = 1;
10768949bcd6Sandrei 				break;
10778949bcd6Sandrei 			} else {
107810569901Sgavinm 				/*
107910569901Sgavinm 				 * On family 0xf cpuid fn 2 ECX[7:0] "NC" is
108010569901Sgavinm 				 * 1 less than the number of physical cores on
108110569901Sgavinm 				 * the chip.  In family 0x10 this value can
108210569901Sgavinm 				 * be affected by "downcoring" - it reflects
108310569901Sgavinm 				 * 1 less than the number of cores actually
108410569901Sgavinm 				 * enabled on this node.
108510569901Sgavinm 				 */
10868949bcd6Sandrei 				cpi->cpi_ncore_per_chip =
10878949bcd6Sandrei 				    BITX((cpi)->cpi_extd[8].cp_ecx, 7, 0) + 1;
10888949bcd6Sandrei 			}
10898949bcd6Sandrei 			break;
10908949bcd6Sandrei 		default:
10918949bcd6Sandrei 			cpi->cpi_ncore_per_chip = 1;
10928949bcd6Sandrei 			break;
10937c478bd9Sstevel@tonic-gate 		}
10940e751525SEric Saxe 
10950e751525SEric Saxe 		/*
10960e751525SEric Saxe 		 * Get CPUID data about TSC Invariance in Deep C-State.
10970e751525SEric Saxe 		 */
10980e751525SEric Saxe 		switch (cpi->cpi_vendor) {
10990e751525SEric Saxe 		case X86_VENDOR_Intel:
11000e751525SEric Saxe 			if (cpi->cpi_maxeax >= 7) {
11010e751525SEric Saxe 				cp = &cpi->cpi_extd[7];
11020e751525SEric Saxe 				cp->cp_eax = 0x80000007;
11030e751525SEric Saxe 				cp->cp_ecx = 0;
11040e751525SEric Saxe 				(void) __cpuid_insn(cp);
11050e751525SEric Saxe 			}
11060e751525SEric Saxe 			break;
11070e751525SEric Saxe 		default:
11080e751525SEric Saxe 			break;
11090e751525SEric Saxe 		}
1110fa2e767eSgavinm 	} else {
1111fa2e767eSgavinm 		cpi->cpi_ncore_per_chip = 1;
11128949bcd6Sandrei 	}
11138949bcd6Sandrei 
11148949bcd6Sandrei 	/*
11158949bcd6Sandrei 	 * If more than one core, then this processor is CMP.
11168949bcd6Sandrei 	 */
11178949bcd6Sandrei 	if (cpi->cpi_ncore_per_chip > 1)
11188949bcd6Sandrei 		feature |= X86_CMP;
1119ae115bc7Smrj 
11208949bcd6Sandrei 	/*
11218949bcd6Sandrei 	 * If the number of cores is the same as the number
11228949bcd6Sandrei 	 * of CPUs, then we cannot have HyperThreading.
11238949bcd6Sandrei 	 */
11248949bcd6Sandrei 	if (cpi->cpi_ncpu_per_chip == cpi->cpi_ncore_per_chip)
11258949bcd6Sandrei 		feature &= ~X86_HTT;
11268949bcd6Sandrei 
11277c478bd9Sstevel@tonic-gate 	if ((feature & (X86_HTT | X86_CMP)) == 0) {
11288949bcd6Sandrei 		/*
11298949bcd6Sandrei 		 * Single-core single-threaded processors.
11308949bcd6Sandrei 		 */
11317c478bd9Sstevel@tonic-gate 		cpi->cpi_chipid = -1;
11327c478bd9Sstevel@tonic-gate 		cpi->cpi_clogid = 0;
11338949bcd6Sandrei 		cpi->cpi_coreid = cpu->cpu_id;
113410569901Sgavinm 		cpi->cpi_pkgcoreid = 0;
11357c478bd9Sstevel@tonic-gate 	} else if (cpi->cpi_ncpu_per_chip > 1) {
11368949bcd6Sandrei 		uint_t i;
11378949bcd6Sandrei 		uint_t chipid_shift = 0;
11388949bcd6Sandrei 		uint_t coreid_shift = 0;
11398949bcd6Sandrei 		uint_t apic_id = CPI_APIC_ID(cpi);
11407c478bd9Sstevel@tonic-gate 
11418949bcd6Sandrei 		for (i = 1; i < cpi->cpi_ncpu_per_chip; i <<= 1)
11428949bcd6Sandrei 			chipid_shift++;
11438949bcd6Sandrei 		cpi->cpi_chipid = apic_id >> chipid_shift;
11448949bcd6Sandrei 		cpi->cpi_clogid = apic_id & ((1 << chipid_shift) - 1);
11458949bcd6Sandrei 
11468949bcd6Sandrei 		if (cpi->cpi_vendor == X86_VENDOR_Intel) {
11478949bcd6Sandrei 			if (feature & X86_CMP) {
11488949bcd6Sandrei 				/*
11498949bcd6Sandrei 				 * Multi-core (and possibly multi-threaded)
11508949bcd6Sandrei 				 * processors.
11518949bcd6Sandrei 				 */
11528949bcd6Sandrei 				uint_t ncpu_per_core;
11538949bcd6Sandrei 				if (cpi->cpi_ncore_per_chip == 1)
11548949bcd6Sandrei 					ncpu_per_core = cpi->cpi_ncpu_per_chip;
11558949bcd6Sandrei 				else if (cpi->cpi_ncore_per_chip > 1)
11568949bcd6Sandrei 					ncpu_per_core = cpi->cpi_ncpu_per_chip /
11578949bcd6Sandrei 					    cpi->cpi_ncore_per_chip;
11588949bcd6Sandrei 				/*
11598949bcd6Sandrei 				 * 8bit APIC IDs on dual core Pentiums
11608949bcd6Sandrei 				 * look like this:
11618949bcd6Sandrei 				 *
11628949bcd6Sandrei 				 * +-----------------------+------+------+
11638949bcd6Sandrei 				 * | Physical Package ID   |  MC  |  HT  |
11648949bcd6Sandrei 				 * +-----------------------+------+------+
11658949bcd6Sandrei 				 * <------- chipid -------->
11668949bcd6Sandrei 				 * <------- coreid --------------->
11678949bcd6Sandrei 				 *			   <--- clogid -->
116810569901Sgavinm 				 *			   <------>
116910569901Sgavinm 				 *			   pkgcoreid
11708949bcd6Sandrei 				 *
11718949bcd6Sandrei 				 * Where the number of bits necessary to
11728949bcd6Sandrei 				 * represent MC and HT fields together equals
11738949bcd6Sandrei 				 * to the minimum number of bits necessary to
11748949bcd6Sandrei 				 * store the value of cpi->cpi_ncpu_per_chip.
11758949bcd6Sandrei 				 * Of those bits, the MC part uses the number
11768949bcd6Sandrei 				 * of bits necessary to store the value of
11778949bcd6Sandrei 				 * cpi->cpi_ncore_per_chip.
11788949bcd6Sandrei 				 */
11798949bcd6Sandrei 				for (i = 1; i < ncpu_per_core; i <<= 1)
11808949bcd6Sandrei 					coreid_shift++;
11813090b9a9Sandrei 				cpi->cpi_coreid = apic_id >> coreid_shift;
118210569901Sgavinm 				cpi->cpi_pkgcoreid = cpi->cpi_clogid >>
118310569901Sgavinm 				    coreid_shift;
11848949bcd6Sandrei 			} else if (feature & X86_HTT) {
11858949bcd6Sandrei 				/*
11868949bcd6Sandrei 				 * Single-core multi-threaded processors.
11878949bcd6Sandrei 				 */
11888949bcd6Sandrei 				cpi->cpi_coreid = cpi->cpi_chipid;
118910569901Sgavinm 				cpi->cpi_pkgcoreid = 0;
11908949bcd6Sandrei 			}
11918949bcd6Sandrei 		} else if (cpi->cpi_vendor == X86_VENDOR_AMD) {
11928949bcd6Sandrei 			/*
119310569901Sgavinm 			 * AMD CMP chips currently have a single thread per
119410569901Sgavinm 			 * core, with 2 cores on family 0xf and 2, 3 or 4
119510569901Sgavinm 			 * cores on family 0x10.
119610569901Sgavinm 			 *
119710569901Sgavinm 			 * Since no two cpus share a core we must assign a
119810569901Sgavinm 			 * distinct coreid per cpu, and we do this by using
119910569901Sgavinm 			 * the cpu_id.  This scheme does not, however,
120010569901Sgavinm 			 * guarantee that sibling cores of a chip will have
120110569901Sgavinm 			 * sequential coreids starting at a multiple of the
120210569901Sgavinm 			 * number of cores per chip - that is usually the
120310569901Sgavinm 			 * case, but if the ACPI MADT table is presented
120410569901Sgavinm 			 * in a different order then we need to perform a
120510569901Sgavinm 			 * few more gymnastics for the pkgcoreid.
120610569901Sgavinm 			 *
120710569901Sgavinm 			 * In family 0xf CMPs there are 2 cores on all nodes
120810569901Sgavinm 			 * present - no mixing of single and dual core parts.
120910569901Sgavinm 			 *
121010569901Sgavinm 			 * In family 0x10 CMPs cpuid fn 2 ECX[15:12]
121110569901Sgavinm 			 * "ApicIdCoreIdSize[3:0]" tells us how
121210569901Sgavinm 			 * many least-significant bits in the ApicId
121310569901Sgavinm 			 * are used to represent the core number
121410569901Sgavinm 			 * within the node.  Cores are always
121510569901Sgavinm 			 * numbered sequentially from 0 regardless
121610569901Sgavinm 			 * of how many or which are disabled, and
121710569901Sgavinm 			 * there seems to be no way to discover the
121810569901Sgavinm 			 * real core id when some are disabled.
12198949bcd6Sandrei 			 */
12208949bcd6Sandrei 			cpi->cpi_coreid = cpu->cpu_id;
122110569901Sgavinm 
122210569901Sgavinm 			if (cpi->cpi_family == 0x10 &&
122310569901Sgavinm 			    cpi->cpi_xmaxeax >= 0x80000008) {
122410569901Sgavinm 				int coreidsz =
122510569901Sgavinm 				    BITX((cpi)->cpi_extd[8].cp_ecx, 15, 12);
122610569901Sgavinm 
122710569901Sgavinm 				cpi->cpi_pkgcoreid =
122810569901Sgavinm 				    apic_id & ((1 << coreidsz) - 1);
122910569901Sgavinm 			} else {
123010569901Sgavinm 				cpi->cpi_pkgcoreid = cpi->cpi_clogid;
123110569901Sgavinm 			}
12328949bcd6Sandrei 		} else {
12338949bcd6Sandrei 			/*
12348949bcd6Sandrei 			 * All other processors are currently
12358949bcd6Sandrei 			 * assumed to have single cores.
12368949bcd6Sandrei 			 */
12378949bcd6Sandrei 			cpi->cpi_coreid = cpi->cpi_chipid;
123810569901Sgavinm 			cpi->cpi_pkgcoreid = 0;
12398949bcd6Sandrei 		}
12407c478bd9Sstevel@tonic-gate 	}
12417c478bd9Sstevel@tonic-gate 
1242b6917abeSmishra 	cpi->cpi_apicid = CPI_APIC_ID(cpi);
1243b6917abeSmishra 
12448a40a695Sgavinm 	/*
12458a40a695Sgavinm 	 * Synthesize chip "revision" and socket type
12468a40a695Sgavinm 	 */
1247e4b86885SCheng Sean Ye 	cpi->cpi_chiprev = _cpuid_chiprev(cpi->cpi_vendor, cpi->cpi_family,
1248e4b86885SCheng Sean Ye 	    cpi->cpi_model, cpi->cpi_step);
1249e4b86885SCheng Sean Ye 	cpi->cpi_chiprevstr = _cpuid_chiprevstr(cpi->cpi_vendor,
1250e4b86885SCheng Sean Ye 	    cpi->cpi_family, cpi->cpi_model, cpi->cpi_step);
1251e4b86885SCheng Sean Ye 	cpi->cpi_socket = _cpuid_skt(cpi->cpi_vendor, cpi->cpi_family,
1252e4b86885SCheng Sean Ye 	    cpi->cpi_model, cpi->cpi_step);
12538a40a695Sgavinm 
12547c478bd9Sstevel@tonic-gate pass1_done:
12557c478bd9Sstevel@tonic-gate 	cpi->cpi_pass = 1;
12567c478bd9Sstevel@tonic-gate 	return (feature);
12577c478bd9Sstevel@tonic-gate }
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate /*
12607c478bd9Sstevel@tonic-gate  * Make copies of the cpuid table entries we depend on, in
12617c478bd9Sstevel@tonic-gate  * part for ease of parsing now, in part so that we have only
12627c478bd9Sstevel@tonic-gate  * one place to correct any of it, in part for ease of
12637c478bd9Sstevel@tonic-gate  * later export to userland, and in part so we can look at
12647c478bd9Sstevel@tonic-gate  * this stuff in a crash dump.
12657c478bd9Sstevel@tonic-gate  */
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate /*ARGSUSED*/
12687c478bd9Sstevel@tonic-gate void
12697c478bd9Sstevel@tonic-gate cpuid_pass2(cpu_t *cpu)
12707c478bd9Sstevel@tonic-gate {
12717c478bd9Sstevel@tonic-gate 	uint_t n, nmax;
12727c478bd9Sstevel@tonic-gate 	int i;
12738949bcd6Sandrei 	struct cpuid_regs *cp;
12747c478bd9Sstevel@tonic-gate 	uint8_t *dp;
12757c478bd9Sstevel@tonic-gate 	uint32_t *iptr;
12767c478bd9Sstevel@tonic-gate 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 	ASSERT(cpi->cpi_pass == 1);
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate 	if (cpi->cpi_maxeax < 1)
12817c478bd9Sstevel@tonic-gate 		goto pass2_done;
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate 	if ((nmax = cpi->cpi_maxeax + 1) > NMAX_CPI_STD)
12847c478bd9Sstevel@tonic-gate 		nmax = NMAX_CPI_STD;
12857c478bd9Sstevel@tonic-gate 	/*
12867c478bd9Sstevel@tonic-gate 	 * (We already handled n == 0 and n == 1 in pass 1)
12877c478bd9Sstevel@tonic-gate 	 */
12887c478bd9Sstevel@tonic-gate 	for (n = 2, cp = &cpi->cpi_std[2]; n < nmax; n++, cp++) {
12898949bcd6Sandrei 		cp->cp_eax = n;
1290d129bde2Sesaxe 
1291d129bde2Sesaxe 		/*
1292d129bde2Sesaxe 		 * CPUID function 4 expects %ecx to be initialized
1293d129bde2Sesaxe 		 * with an index which indicates which cache to return
1294d129bde2Sesaxe 		 * information about. The OS is expected to call function 4
1295d129bde2Sesaxe 		 * with %ecx set to 0, 1, 2, ... until it returns with
1296d129bde2Sesaxe 		 * EAX[4:0] set to 0, which indicates there are no more
1297d129bde2Sesaxe 		 * caches.
1298d129bde2Sesaxe 		 *
1299d129bde2Sesaxe 		 * Here, populate cpi_std[4] with the information returned by
1300d129bde2Sesaxe 		 * function 4 when %ecx == 0, and do the rest in cpuid_pass3()
1301d129bde2Sesaxe 		 * when dynamic memory allocation becomes available.
1302d129bde2Sesaxe 		 *
1303d129bde2Sesaxe 		 * Note: we need to explicitly initialize %ecx here, since
1304d129bde2Sesaxe 		 * function 4 may have been previously invoked.
1305d129bde2Sesaxe 		 */
1306d129bde2Sesaxe 		if (n == 4)
1307d129bde2Sesaxe 			cp->cp_ecx = 0;
1308d129bde2Sesaxe 
13098949bcd6Sandrei 		(void) __cpuid_insn(cp);
1310ae115bc7Smrj 		platform_cpuid_mangle(cpi->cpi_vendor, n, cp);
13117c478bd9Sstevel@tonic-gate 		switch (n) {
13127c478bd9Sstevel@tonic-gate 		case 2:
13137c478bd9Sstevel@tonic-gate 			/*
13147c478bd9Sstevel@tonic-gate 			 * "the lower 8 bits of the %eax register
13157c478bd9Sstevel@tonic-gate 			 * contain a value that identifies the number
13167c478bd9Sstevel@tonic-gate 			 * of times the cpuid [instruction] has to be
13177c478bd9Sstevel@tonic-gate 			 * executed to obtain a complete image of the
13187c478bd9Sstevel@tonic-gate 			 * processor's caching systems."
13197c478bd9Sstevel@tonic-gate 			 *
13207c478bd9Sstevel@tonic-gate 			 * How *do* they make this stuff up?
13217c478bd9Sstevel@tonic-gate 			 */
13227c478bd9Sstevel@tonic-gate 			cpi->cpi_ncache = sizeof (*cp) *
13237c478bd9Sstevel@tonic-gate 			    BITX(cp->cp_eax, 7, 0);
13247c478bd9Sstevel@tonic-gate 			if (cpi->cpi_ncache == 0)
13257c478bd9Sstevel@tonic-gate 				break;
13267c478bd9Sstevel@tonic-gate 			cpi->cpi_ncache--;	/* skip count byte */
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate 			/*
13297c478bd9Sstevel@tonic-gate 			 * Well, for now, rather than attempt to implement
13307c478bd9Sstevel@tonic-gate 			 * this slightly dubious algorithm, we just look
13317c478bd9Sstevel@tonic-gate 			 * at the first 15 ..
13327c478bd9Sstevel@tonic-gate 			 */
13337c478bd9Sstevel@tonic-gate 			if (cpi->cpi_ncache > (sizeof (*cp) - 1))
13347c478bd9Sstevel@tonic-gate 				cpi->cpi_ncache = sizeof (*cp) - 1;
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate 			dp = cpi->cpi_cacheinfo;
13377c478bd9Sstevel@tonic-gate 			if (BITX(cp->cp_eax, 31, 31) == 0) {
13387c478bd9Sstevel@tonic-gate 				uint8_t *p = (void *)&cp->cp_eax;
133963d3f7dfSkk208521 				for (i = 1; i < 4; i++)
13407c478bd9Sstevel@tonic-gate 					if (p[i] != 0)
13417c478bd9Sstevel@tonic-gate 						*dp++ = p[i];
13427c478bd9Sstevel@tonic-gate 			}
13437c478bd9Sstevel@tonic-gate 			if (BITX(cp->cp_ebx, 31, 31) == 0) {
13447c478bd9Sstevel@tonic-gate 				uint8_t *p = (void *)&cp->cp_ebx;
13457c478bd9Sstevel@tonic-gate 				for (i = 0; i < 4; i++)
13467c478bd9Sstevel@tonic-gate 					if (p[i] != 0)
13477c478bd9Sstevel@tonic-gate 						*dp++ = p[i];
13487c478bd9Sstevel@tonic-gate 			}
13497c478bd9Sstevel@tonic-gate 			if (BITX(cp->cp_ecx, 31, 31) == 0) {
13507c478bd9Sstevel@tonic-gate 				uint8_t *p = (void *)&cp->cp_ecx;
13517c478bd9Sstevel@tonic-gate 				for (i = 0; i < 4; i++)
13527c478bd9Sstevel@tonic-gate 					if (p[i] != 0)
13537c478bd9Sstevel@tonic-gate 						*dp++ = p[i];
13547c478bd9Sstevel@tonic-gate 			}
13557c478bd9Sstevel@tonic-gate 			if (BITX(cp->cp_edx, 31, 31) == 0) {
13567c478bd9Sstevel@tonic-gate 				uint8_t *p = (void *)&cp->cp_edx;
13577c478bd9Sstevel@tonic-gate 				for (i = 0; i < 4; i++)
13587c478bd9Sstevel@tonic-gate 					if (p[i] != 0)
13597c478bd9Sstevel@tonic-gate 						*dp++ = p[i];
13607c478bd9Sstevel@tonic-gate 			}
13617c478bd9Sstevel@tonic-gate 			break;
1362f98fbcecSbholler 
13637c478bd9Sstevel@tonic-gate 		case 3:	/* Processor serial number, if PSN supported */
1364f98fbcecSbholler 			break;
1365f98fbcecSbholler 
13667c478bd9Sstevel@tonic-gate 		case 4:	/* Deterministic cache parameters */
1367f98fbcecSbholler 			break;
1368f98fbcecSbholler 
13697c478bd9Sstevel@tonic-gate 		case 5:	/* Monitor/Mwait parameters */
13705b8a6efeSbholler 		{
13715b8a6efeSbholler 			size_t mwait_size;
1372f98fbcecSbholler 
1373f98fbcecSbholler 			/*
1374f98fbcecSbholler 			 * check cpi_mwait.support which was set in cpuid_pass1
1375f98fbcecSbholler 			 */
1376f98fbcecSbholler 			if (!(cpi->cpi_mwait.support & MWAIT_SUPPORT))
1377f98fbcecSbholler 				break;
1378f98fbcecSbholler 
13795b8a6efeSbholler 			/*
13805b8a6efeSbholler 			 * Protect ourself from insane mwait line size.
13815b8a6efeSbholler 			 * Workaround for incomplete hardware emulator(s).
13825b8a6efeSbholler 			 */
13835b8a6efeSbholler 			mwait_size = (size_t)MWAIT_SIZE_MAX(cpi);
13845b8a6efeSbholler 			if (mwait_size < sizeof (uint32_t) ||
13855b8a6efeSbholler 			    !ISP2(mwait_size)) {
13865b8a6efeSbholler #if DEBUG
13875b8a6efeSbholler 				cmn_err(CE_NOTE, "Cannot handle cpu %d mwait "
13885d8efbbcSSaurabh Misra 				    "size %ld", cpu->cpu_id, (long)mwait_size);
13895b8a6efeSbholler #endif
13905b8a6efeSbholler 				break;
13915b8a6efeSbholler 			}
13925b8a6efeSbholler 
1393f98fbcecSbholler 			cpi->cpi_mwait.mon_min = (size_t)MWAIT_SIZE_MIN(cpi);
13945b8a6efeSbholler 			cpi->cpi_mwait.mon_max = mwait_size;
1395f98fbcecSbholler 			if (MWAIT_EXTENSION(cpi)) {
1396f98fbcecSbholler 				cpi->cpi_mwait.support |= MWAIT_EXTENSIONS;
1397f98fbcecSbholler 				if (MWAIT_INT_ENABLE(cpi))
1398f98fbcecSbholler 					cpi->cpi_mwait.support |=
1399f98fbcecSbholler 					    MWAIT_ECX_INT_ENABLE;
1400f98fbcecSbholler 			}
1401f98fbcecSbholler 			break;
14025b8a6efeSbholler 		}
14037c478bd9Sstevel@tonic-gate 		default:
14047c478bd9Sstevel@tonic-gate 			break;
14057c478bd9Sstevel@tonic-gate 		}
14067c478bd9Sstevel@tonic-gate 	}
14077c478bd9Sstevel@tonic-gate 
1408b6917abeSmishra 	if (cpi->cpi_maxeax >= 0xB && cpi->cpi_vendor == X86_VENDOR_Intel) {
14095d8efbbcSSaurabh Misra 		struct cpuid_regs regs;
14105d8efbbcSSaurabh Misra 
14115d8efbbcSSaurabh Misra 		cp = &regs;
1412b6917abeSmishra 		cp->cp_eax = 0xB;
14135d8efbbcSSaurabh Misra 		cp->cp_edx = cp->cp_ebx = cp->cp_ecx = 0;
1414b6917abeSmishra 
1415b6917abeSmishra 		(void) __cpuid_insn(cp);
1416b6917abeSmishra 
1417b6917abeSmishra 		/*
1418b6917abeSmishra 		 * Check CPUID.EAX=0BH, ECX=0H:EBX is non-zero, which
1419b6917abeSmishra 		 * indicates that the extended topology enumeration leaf is
1420b6917abeSmishra 		 * available.
1421b6917abeSmishra 		 */
1422b6917abeSmishra 		if (cp->cp_ebx) {
1423b6917abeSmishra 			uint32_t x2apic_id;
1424b6917abeSmishra 			uint_t coreid_shift = 0;
1425b6917abeSmishra 			uint_t ncpu_per_core = 1;
1426b6917abeSmishra 			uint_t chipid_shift = 0;
1427b6917abeSmishra 			uint_t ncpu_per_chip = 1;
1428b6917abeSmishra 			uint_t i;
1429b6917abeSmishra 			uint_t level;
1430b6917abeSmishra 
1431b6917abeSmishra 			for (i = 0; i < CPI_FNB_ECX_MAX; i++) {
1432b6917abeSmishra 				cp->cp_eax = 0xB;
1433b6917abeSmishra 				cp->cp_ecx = i;
1434b6917abeSmishra 
1435b6917abeSmishra 				(void) __cpuid_insn(cp);
1436b6917abeSmishra 				level = CPI_CPU_LEVEL_TYPE(cp);
1437b6917abeSmishra 
1438b6917abeSmishra 				if (level == 1) {
1439b6917abeSmishra 					x2apic_id = cp->cp_edx;
1440b6917abeSmishra 					coreid_shift = BITX(cp->cp_eax, 4, 0);
1441b6917abeSmishra 					ncpu_per_core = BITX(cp->cp_ebx, 15, 0);
1442b6917abeSmishra 				} else if (level == 2) {
1443b6917abeSmishra 					x2apic_id = cp->cp_edx;
1444b6917abeSmishra 					chipid_shift = BITX(cp->cp_eax, 4, 0);
1445b6917abeSmishra 					ncpu_per_chip = BITX(cp->cp_ebx, 15, 0);
1446b6917abeSmishra 				}
1447b6917abeSmishra 			}
1448b6917abeSmishra 
1449b6917abeSmishra 			cpi->cpi_apicid = x2apic_id;
1450b6917abeSmishra 			cpi->cpi_ncpu_per_chip = ncpu_per_chip;
1451b6917abeSmishra 			cpi->cpi_ncore_per_chip = ncpu_per_chip /
1452b6917abeSmishra 			    ncpu_per_core;
1453b6917abeSmishra 			cpi->cpi_chipid = x2apic_id >> chipid_shift;
1454b6917abeSmishra 			cpi->cpi_clogid = x2apic_id & ((1 << chipid_shift) - 1);
1455b6917abeSmishra 			cpi->cpi_coreid = x2apic_id >> coreid_shift;
1456b6917abeSmishra 			cpi->cpi_pkgcoreid = cpi->cpi_clogid >> coreid_shift;
1457b6917abeSmishra 		}
14585d8efbbcSSaurabh Misra 
14595d8efbbcSSaurabh Misra 		/* Make cp NULL so that we don't stumble on others */
14605d8efbbcSSaurabh Misra 		cp = NULL;
1461b6917abeSmishra 	}
1462b6917abeSmishra 
14637c478bd9Sstevel@tonic-gate 	if ((cpi->cpi_xmaxeax & 0x80000000) == 0)
14647c478bd9Sstevel@tonic-gate 		goto pass2_done;
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate 	if ((nmax = cpi->cpi_xmaxeax - 0x80000000 + 1) > NMAX_CPI_EXTD)
14677c478bd9Sstevel@tonic-gate 		nmax = NMAX_CPI_EXTD;
14687c478bd9Sstevel@tonic-gate 	/*
14697c478bd9Sstevel@tonic-gate 	 * Copy the extended properties, fixing them as we go.
14707c478bd9Sstevel@tonic-gate 	 * (We already handled n == 0 and n == 1 in pass 1)
14717c478bd9Sstevel@tonic-gate 	 */
14727c478bd9Sstevel@tonic-gate 	iptr = (void *)cpi->cpi_brandstr;
14737c478bd9Sstevel@tonic-gate 	for (n = 2, cp = &cpi->cpi_extd[2]; n < nmax; cp++, n++) {
14748949bcd6Sandrei 		cp->cp_eax = 0x80000000 + n;
14758949bcd6Sandrei 		(void) __cpuid_insn(cp);
1476ae115bc7Smrj 		platform_cpuid_mangle(cpi->cpi_vendor, 0x80000000 + n, cp);
14777c478bd9Sstevel@tonic-gate 		switch (n) {
14787c478bd9Sstevel@tonic-gate 		case 2:
14797c478bd9Sstevel@tonic-gate 		case 3:
14807c478bd9Sstevel@tonic-gate 		case 4:
14817c478bd9Sstevel@tonic-gate 			/*
14827c478bd9Sstevel@tonic-gate 			 * Extract the brand string
14837c478bd9Sstevel@tonic-gate 			 */
14847c478bd9Sstevel@tonic-gate 			*iptr++ = cp->cp_eax;
14857c478bd9Sstevel@tonic-gate 			*iptr++ = cp->cp_ebx;
14867c478bd9Sstevel@tonic-gate 			*iptr++ = cp->cp_ecx;
14877c478bd9Sstevel@tonic-gate 			*iptr++ = cp->cp_edx;
14887c478bd9Sstevel@tonic-gate 			break;
14897c478bd9Sstevel@tonic-gate 		case 5:
14907c478bd9Sstevel@tonic-gate 			switch (cpi->cpi_vendor) {
14917c478bd9Sstevel@tonic-gate 			case X86_VENDOR_AMD:
14927c478bd9Sstevel@tonic-gate 				/*
14937c478bd9Sstevel@tonic-gate 				 * The Athlon and Duron were the first
14947c478bd9Sstevel@tonic-gate 				 * parts to report the sizes of the
14957c478bd9Sstevel@tonic-gate 				 * TLB for large pages. Before then,
14967c478bd9Sstevel@tonic-gate 				 * we don't trust the data.
14977c478bd9Sstevel@tonic-gate 				 */
14987c478bd9Sstevel@tonic-gate 				if (cpi->cpi_family < 6 ||
14997c478bd9Sstevel@tonic-gate 				    (cpi->cpi_family == 6 &&
15007c478bd9Sstevel@tonic-gate 				    cpi->cpi_model < 1))
15017c478bd9Sstevel@tonic-gate 					cp->cp_eax = 0;
15027c478bd9Sstevel@tonic-gate 				break;
15037c478bd9Sstevel@tonic-gate 			default:
15047c478bd9Sstevel@tonic-gate 				break;
15057c478bd9Sstevel@tonic-gate 			}
15067c478bd9Sstevel@tonic-gate 			break;
15077c478bd9Sstevel@tonic-gate 		case 6:
15087c478bd9Sstevel@tonic-gate 			switch (cpi->cpi_vendor) {
15097c478bd9Sstevel@tonic-gate 			case X86_VENDOR_AMD:
15107c478bd9Sstevel@tonic-gate 				/*
15117c478bd9Sstevel@tonic-gate 				 * The Athlon and Duron were the first
15127c478bd9Sstevel@tonic-gate 				 * AMD parts with L2 TLB's.
15137c478bd9Sstevel@tonic-gate 				 * Before then, don't trust the data.
15147c478bd9Sstevel@tonic-gate 				 */
15157c478bd9Sstevel@tonic-gate 				if (cpi->cpi_family < 6 ||
15167c478bd9Sstevel@tonic-gate 				    cpi->cpi_family == 6 &&
15177c478bd9Sstevel@tonic-gate 				    cpi->cpi_model < 1)
15187c478bd9Sstevel@tonic-gate 					cp->cp_eax = cp->cp_ebx = 0;
15197c478bd9Sstevel@tonic-gate 				/*
15207c478bd9Sstevel@tonic-gate 				 * AMD Duron rev A0 reports L2
15217c478bd9Sstevel@tonic-gate 				 * cache size incorrectly as 1K
15227c478bd9Sstevel@tonic-gate 				 * when it is really 64K
15237c478bd9Sstevel@tonic-gate 				 */
15247c478bd9Sstevel@tonic-gate 				if (cpi->cpi_family == 6 &&
15257c478bd9Sstevel@tonic-gate 				    cpi->cpi_model == 3 &&
15267c478bd9Sstevel@tonic-gate 				    cpi->cpi_step == 0) {
15277c478bd9Sstevel@tonic-gate 					cp->cp_ecx &= 0xffff;
15287c478bd9Sstevel@tonic-gate 					cp->cp_ecx |= 0x400000;
15297c478bd9Sstevel@tonic-gate 				}
15307c478bd9Sstevel@tonic-gate 				break;
15317c478bd9Sstevel@tonic-gate 			case X86_VENDOR_Cyrix:	/* VIA C3 */
15327c478bd9Sstevel@tonic-gate 				/*
15337c478bd9Sstevel@tonic-gate 				 * VIA C3 processors are a bit messed
15347c478bd9Sstevel@tonic-gate 				 * up w.r.t. encoding cache sizes in %ecx
15357c478bd9Sstevel@tonic-gate 				 */
15367c478bd9Sstevel@tonic-gate 				if (cpi->cpi_family != 6)
15377c478bd9Sstevel@tonic-gate 					break;
15387c478bd9Sstevel@tonic-gate 				/*
15397c478bd9Sstevel@tonic-gate 				 * model 7 and 8 were incorrectly encoded
15407c478bd9Sstevel@tonic-gate 				 *
15417c478bd9Sstevel@tonic-gate 				 * xxx is model 8 really broken?
15427c478bd9Sstevel@tonic-gate 				 */
15437c478bd9Sstevel@tonic-gate 				if (cpi->cpi_model == 7 ||
15447c478bd9Sstevel@tonic-gate 				    cpi->cpi_model == 8)
15457c478bd9Sstevel@tonic-gate 					cp->cp_ecx =
15467c478bd9Sstevel@tonic-gate 					    BITX(cp->cp_ecx, 31, 24) << 16 |
15477c478bd9Sstevel@tonic-gate 					    BITX(cp->cp_ecx, 23, 16) << 12 |
15487c478bd9Sstevel@tonic-gate 					    BITX(cp->cp_ecx, 15, 8) << 8 |
15497c478bd9Sstevel@tonic-gate 					    BITX(cp->cp_ecx, 7, 0);
15507c478bd9Sstevel@tonic-gate 				/*
15517c478bd9Sstevel@tonic-gate 				 * model 9 stepping 1 has wrong associativity
15527c478bd9Sstevel@tonic-gate 				 */
15537c478bd9Sstevel@tonic-gate 				if (cpi->cpi_model == 9 && cpi->cpi_step == 1)
15547c478bd9Sstevel@tonic-gate 					cp->cp_ecx |= 8 << 12;
15557c478bd9Sstevel@tonic-gate 				break;
15567c478bd9Sstevel@tonic-gate 			case X86_VENDOR_Intel:
15577c478bd9Sstevel@tonic-gate 				/*
15587c478bd9Sstevel@tonic-gate 				 * Extended L2 Cache features function.
15597c478bd9Sstevel@tonic-gate 				 * First appeared on Prescott.
15607c478bd9Sstevel@tonic-gate 				 */
15617c478bd9Sstevel@tonic-gate 			default:
15627c478bd9Sstevel@tonic-gate 				break;
15637c478bd9Sstevel@tonic-gate 			}
15647c478bd9Sstevel@tonic-gate 			break;
15657c478bd9Sstevel@tonic-gate 		default:
15667c478bd9Sstevel@tonic-gate 			break;
15677c478bd9Sstevel@tonic-gate 		}
15687c478bd9Sstevel@tonic-gate 	}
15697c478bd9Sstevel@tonic-gate 
15707c478bd9Sstevel@tonic-gate pass2_done:
15717c478bd9Sstevel@tonic-gate 	cpi->cpi_pass = 2;
15727c478bd9Sstevel@tonic-gate }
15737c478bd9Sstevel@tonic-gate 
15747c478bd9Sstevel@tonic-gate static const char *
15757c478bd9Sstevel@tonic-gate intel_cpubrand(const struct cpuid_info *cpi)
15767c478bd9Sstevel@tonic-gate {
15777c478bd9Sstevel@tonic-gate 	int i;
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate 	if ((x86_feature & X86_CPUID) == 0 ||
15807c478bd9Sstevel@tonic-gate 	    cpi->cpi_maxeax < 1 || cpi->cpi_family < 5)
15817c478bd9Sstevel@tonic-gate 		return ("i486");
15827c478bd9Sstevel@tonic-gate 
15837c478bd9Sstevel@tonic-gate 	switch (cpi->cpi_family) {
15847c478bd9Sstevel@tonic-gate 	case 5:
15857c478bd9Sstevel@tonic-gate 		return ("Intel Pentium(r)");
15867c478bd9Sstevel@tonic-gate 	case 6:
15877c478bd9Sstevel@tonic-gate 		switch (cpi->cpi_model) {
15887c478bd9Sstevel@tonic-gate 			uint_t celeron, xeon;
15898949bcd6Sandrei 			const struct cpuid_regs *cp;
15907c478bd9Sstevel@tonic-gate 		case 0:
15917c478bd9Sstevel@tonic-gate 		case 1:
15927c478bd9Sstevel@tonic-gate 		case 2:
15937c478bd9Sstevel@tonic-gate 			return ("Intel Pentium(r) Pro");
15947c478bd9Sstevel@tonic-gate 		case 3:
15957c478bd9Sstevel@tonic-gate 		case 4:
15967c478bd9Sstevel@tonic-gate 			return ("Intel Pentium(r) II");
15977c478bd9Sstevel@tonic-gate 		case 6:
15987c478bd9Sstevel@tonic-gate 			return ("Intel Celeron(r)");
15997c478bd9Sstevel@tonic-gate 		case 5:
16007c478bd9Sstevel@tonic-gate 		case 7:
16017c478bd9Sstevel@tonic-gate 			celeron = xeon = 0;
16027c478bd9Sstevel@tonic-gate 			cp = &cpi->cpi_std[2];	/* cache info */
16037c478bd9Sstevel@tonic-gate 
160463d3f7dfSkk208521 			for (i = 1; i < 4; i++) {
16057c478bd9Sstevel@tonic-gate 				uint_t tmp;
16067c478bd9Sstevel@tonic-gate 
16077c478bd9Sstevel@tonic-gate 				tmp = (cp->cp_eax >> (8 * i)) & 0xff;
16087c478bd9Sstevel@tonic-gate 				if (tmp == 0x40)
16097c478bd9Sstevel@tonic-gate 					celeron++;
16107c478bd9Sstevel@tonic-gate 				if (tmp >= 0x44 && tmp <= 0x45)
16117c478bd9Sstevel@tonic-gate 					xeon++;
16127c478bd9Sstevel@tonic-gate 			}
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate 			for (i = 0; i < 2; i++) {
16157c478bd9Sstevel@tonic-gate 				uint_t tmp;
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate 				tmp = (cp->cp_ebx >> (8 * i)) & 0xff;
16187c478bd9Sstevel@tonic-gate 				if (tmp == 0x40)
16197c478bd9Sstevel@tonic-gate 					celeron++;
16207c478bd9Sstevel@tonic-gate 				else if (tmp >= 0x44 && tmp <= 0x45)
16217c478bd9Sstevel@tonic-gate 					xeon++;
16227c478bd9Sstevel@tonic-gate 			}
16237c478bd9Sstevel@tonic-gate 
16247c478bd9Sstevel@tonic-gate 			for (i = 0; i < 4; i++) {
16257c478bd9Sstevel@tonic-gate 				uint_t tmp;
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate 				tmp = (cp->cp_ecx >> (8 * i)) & 0xff;
16287c478bd9Sstevel@tonic-gate 				if (tmp == 0x40)
16297c478bd9Sstevel@tonic-gate 					celeron++;
16307c478bd9Sstevel@tonic-gate 				else if (tmp >= 0x44 && tmp <= 0x45)
16317c478bd9Sstevel@tonic-gate 					xeon++;
16327c478bd9Sstevel@tonic-gate 			}
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate 			for (i = 0; i < 4; i++) {
16357c478bd9Sstevel@tonic-gate 				uint_t tmp;
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate 				tmp = (cp->cp_edx >> (8 * i)) & 0xff;
16387c478bd9Sstevel@tonic-gate 				if (tmp == 0x40)
16397c478bd9Sstevel@tonic-gate 					celeron++;
16407c478bd9Sstevel@tonic-gate 				else if (tmp >= 0x44 && tmp <= 0x45)
16417c478bd9Sstevel@tonic-gate 					xeon++;
16427c478bd9Sstevel@tonic-gate 			}
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate 			if (celeron)
16457c478bd9Sstevel@tonic-gate 				return ("Intel Celeron(r)");
16467c478bd9Sstevel@tonic-gate 			if (xeon)
16477c478bd9Sstevel@tonic-gate 				return (cpi->cpi_model == 5 ?
16487c478bd9Sstevel@tonic-gate 				    "Intel Pentium(r) II Xeon(tm)" :
16497c478bd9Sstevel@tonic-gate 				    "Intel Pentium(r) III Xeon(tm)");
16507c478bd9Sstevel@tonic-gate 			return (cpi->cpi_model == 5 ?
16517c478bd9Sstevel@tonic-gate 			    "Intel Pentium(r) II or Pentium(r) II Xeon(tm)" :
16527c478bd9Sstevel@tonic-gate 			    "Intel Pentium(r) III or Pentium(r) III Xeon(tm)");
16537c478bd9Sstevel@tonic-gate 		default:
16547c478bd9Sstevel@tonic-gate 			break;
16557c478bd9Sstevel@tonic-gate 		}
16567c478bd9Sstevel@tonic-gate 	default:
16577c478bd9Sstevel@tonic-gate 		break;
16587c478bd9Sstevel@tonic-gate 	}
16597c478bd9Sstevel@tonic-gate 
16605ff02082Sdmick 	/* BrandID is present if the field is nonzero */
16615ff02082Sdmick 	if (cpi->cpi_brandid != 0) {
16627c478bd9Sstevel@tonic-gate 		static const struct {
16637c478bd9Sstevel@tonic-gate 			uint_t bt_bid;
16647c478bd9Sstevel@tonic-gate 			const char *bt_str;
16657c478bd9Sstevel@tonic-gate 		} brand_tbl[] = {
16667c478bd9Sstevel@tonic-gate 			{ 0x1,	"Intel(r) Celeron(r)" },
16677c478bd9Sstevel@tonic-gate 			{ 0x2,	"Intel(r) Pentium(r) III" },
16687c478bd9Sstevel@tonic-gate 			{ 0x3,	"Intel(r) Pentium(r) III Xeon(tm)" },
16697c478bd9Sstevel@tonic-gate 			{ 0x4,	"Intel(r) Pentium(r) III" },
16707c478bd9Sstevel@tonic-gate 			{ 0x6,	"Mobile Intel(r) Pentium(r) III" },
16717c478bd9Sstevel@tonic-gate 			{ 0x7,	"Mobile Intel(r) Celeron(r)" },
16727c478bd9Sstevel@tonic-gate 			{ 0x8,	"Intel(r) Pentium(r) 4" },
16737c478bd9Sstevel@tonic-gate 			{ 0x9,	"Intel(r) Pentium(r) 4" },
16747c478bd9Sstevel@tonic-gate 			{ 0xa,	"Intel(r) Celeron(r)" },
16757c478bd9Sstevel@tonic-gate 			{ 0xb,	"Intel(r) Xeon(tm)" },
16767c478bd9Sstevel@tonic-gate 			{ 0xc,	"Intel(r) Xeon(tm) MP" },
16777c478bd9Sstevel@tonic-gate 			{ 0xe,	"Mobile Intel(r) Pentium(r) 4" },
16785ff02082Sdmick 			{ 0xf,	"Mobile Intel(r) Celeron(r)" },
16795ff02082Sdmick 			{ 0x11, "Mobile Genuine Intel(r)" },
16805ff02082Sdmick 			{ 0x12, "Intel(r) Celeron(r) M" },
16815ff02082Sdmick 			{ 0x13, "Mobile Intel(r) Celeron(r)" },
16825ff02082Sdmick 			{ 0x14, "Intel(r) Celeron(r)" },
16835ff02082Sdmick 			{ 0x15, "Mobile Genuine Intel(r)" },
16845ff02082Sdmick 			{ 0x16,	"Intel(r) Pentium(r) M" },
16855ff02082Sdmick 			{ 0x17, "Mobile Intel(r) Celeron(r)" }
16867c478bd9Sstevel@tonic-gate 		};
16877c478bd9Sstevel@tonic-gate 		uint_t btblmax = sizeof (brand_tbl) / sizeof (brand_tbl[0]);
16887c478bd9Sstevel@tonic-gate 		uint_t sgn;
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate 		sgn = (cpi->cpi_family << 8) |
16917c478bd9Sstevel@tonic-gate 		    (cpi->cpi_model << 4) | cpi->cpi_step;
16927c478bd9Sstevel@tonic-gate 
16937c478bd9Sstevel@tonic-gate 		for (i = 0; i < btblmax; i++)
16947c478bd9Sstevel@tonic-gate 			if (brand_tbl[i].bt_bid == cpi->cpi_brandid)
16957c478bd9Sstevel@tonic-gate 				break;
16967c478bd9Sstevel@tonic-gate 		if (i < btblmax) {
16977c478bd9Sstevel@tonic-gate 			if (sgn == 0x6b1 && cpi->cpi_brandid == 3)
16987c478bd9Sstevel@tonic-gate 				return ("Intel(r) Celeron(r)");
16997c478bd9Sstevel@tonic-gate 			if (sgn < 0xf13 && cpi->cpi_brandid == 0xb)
17007c478bd9Sstevel@tonic-gate 				return ("Intel(r) Xeon(tm) MP");
17017c478bd9Sstevel@tonic-gate 			if (sgn < 0xf13 && cpi->cpi_brandid == 0xe)
17027c478bd9Sstevel@tonic-gate 				return ("Intel(r) Xeon(tm)");
17037c478bd9Sstevel@tonic-gate 			return (brand_tbl[i].bt_str);
17047c478bd9Sstevel@tonic-gate 		}
17057c478bd9Sstevel@tonic-gate 	}
17067c478bd9Sstevel@tonic-gate 
17077c478bd9Sstevel@tonic-gate 	return (NULL);
17087c478bd9Sstevel@tonic-gate }
17097c478bd9Sstevel@tonic-gate 
17107c478bd9Sstevel@tonic-gate static const char *
17117c478bd9Sstevel@tonic-gate amd_cpubrand(const struct cpuid_info *cpi)
17127c478bd9Sstevel@tonic-gate {
17137c478bd9Sstevel@tonic-gate 	if ((x86_feature & X86_CPUID) == 0 ||
17147c478bd9Sstevel@tonic-gate 	    cpi->cpi_maxeax < 1 || cpi->cpi_family < 5)
17157c478bd9Sstevel@tonic-gate 		return ("i486 compatible");
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate 	switch (cpi->cpi_family) {
17187c478bd9Sstevel@tonic-gate 	case 5:
17197c478bd9Sstevel@tonic-gate 		switch (cpi->cpi_model) {
17207c478bd9Sstevel@tonic-gate 		case 0:
17217c478bd9Sstevel@tonic-gate 		case 1:
17227c478bd9Sstevel@tonic-gate 		case 2:
17237c478bd9Sstevel@tonic-gate 		case 3:
17247c478bd9Sstevel@tonic-gate 		case 4:
17257c478bd9Sstevel@tonic-gate 		case 5:
17267c478bd9Sstevel@tonic-gate 			return ("AMD-K5(r)");
17277c478bd9Sstevel@tonic-gate 		case 6:
17287c478bd9Sstevel@tonic-gate 		case 7:
17297c478bd9Sstevel@tonic-gate 			return ("AMD-K6(r)");
17307c478bd9Sstevel@tonic-gate 		case 8:
17317c478bd9Sstevel@tonic-gate 			return ("AMD-K6(r)-2");
17327c478bd9Sstevel@tonic-gate 		case 9:
17337c478bd9Sstevel@tonic-gate 			return ("AMD-K6(r)-III");
17347c478bd9Sstevel@tonic-gate 		default:
17357c478bd9Sstevel@tonic-gate 			return ("AMD (family 5)");
17367c478bd9Sstevel@tonic-gate 		}
17377c478bd9Sstevel@tonic-gate 	case 6:
17387c478bd9Sstevel@tonic-gate 		switch (cpi->cpi_model) {
17397c478bd9Sstevel@tonic-gate 		case 1:
17407c478bd9Sstevel@tonic-gate 			return ("AMD-K7(tm)");
17417c478bd9Sstevel@tonic-gate 		case 0:
17427c478bd9Sstevel@tonic-gate 		case 2:
17437c478bd9Sstevel@tonic-gate 		case 4:
17447c478bd9Sstevel@tonic-gate 			return ("AMD Athlon(tm)");
17457c478bd9Sstevel@tonic-gate 		case 3:
17467c478bd9Sstevel@tonic-gate 		case 7:
17477c478bd9Sstevel@tonic-gate 			return ("AMD Duron(tm)");
17487c478bd9Sstevel@tonic-gate 		case 6:
17497c478bd9Sstevel@tonic-gate 		case 8:
17507c478bd9Sstevel@tonic-gate 		case 10:
17517c478bd9Sstevel@tonic-gate 			/*
17527c478bd9Sstevel@tonic-gate 			 * Use the L2 cache size to distinguish
17537c478bd9Sstevel@tonic-gate 			 */
17547c478bd9Sstevel@tonic-gate 			return ((cpi->cpi_extd[6].cp_ecx >> 16) >= 256 ?
17557c478bd9Sstevel@tonic-gate 			    "AMD Athlon(tm)" : "AMD Duron(tm)");
17567c478bd9Sstevel@tonic-gate 		default:
17577c478bd9Sstevel@tonic-gate 			return ("AMD (family 6)");
17587c478bd9Sstevel@tonic-gate 		}
17597c478bd9Sstevel@tonic-gate 	default:
17607c478bd9Sstevel@tonic-gate 		break;
17617c478bd9Sstevel@tonic-gate 	}
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate 	if (cpi->cpi_family == 0xf && cpi->cpi_model == 5 &&
17647c478bd9Sstevel@tonic-gate 	    cpi->cpi_brandid != 0) {
17657c478bd9Sstevel@tonic-gate 		switch (BITX(cpi->cpi_brandid, 7, 5)) {
17667c478bd9Sstevel@tonic-gate 		case 3:
17677c478bd9Sstevel@tonic-gate 			return ("AMD Opteron(tm) UP 1xx");
17687c478bd9Sstevel@tonic-gate 		case 4:
17697c478bd9Sstevel@tonic-gate 			return ("AMD Opteron(tm) DP 2xx");
17707c478bd9Sstevel@tonic-gate 		case 5:
17717c478bd9Sstevel@tonic-gate 			return ("AMD Opteron(tm) MP 8xx");
17727c478bd9Sstevel@tonic-gate 		default:
17737c478bd9Sstevel@tonic-gate 			return ("AMD Opteron(tm)");
17747c478bd9Sstevel@tonic-gate 		}
17757c478bd9Sstevel@tonic-gate 	}
17767c478bd9Sstevel@tonic-gate 
17777c478bd9Sstevel@tonic-gate 	return (NULL);
17787c478bd9Sstevel@tonic-gate }
17797c478bd9Sstevel@tonic-gate 
17807c478bd9Sstevel@tonic-gate static const char *
17817c478bd9Sstevel@tonic-gate cyrix_cpubrand(struct cpuid_info *cpi, uint_t type)
17827c478bd9Sstevel@tonic-gate {
17837c478bd9Sstevel@tonic-gate 	if ((x86_feature & X86_CPUID) == 0 ||
17847c478bd9Sstevel@tonic-gate 	    cpi->cpi_maxeax < 1 || cpi->cpi_family < 5 ||
17857c478bd9Sstevel@tonic-gate 	    type == X86_TYPE_CYRIX_486)
17867c478bd9Sstevel@tonic-gate 		return ("i486 compatible");
17877c478bd9Sstevel@tonic-gate 
17887c478bd9Sstevel@tonic-gate 	switch (type) {
17897c478bd9Sstevel@tonic-gate 	case X86_TYPE_CYRIX_6x86:
17907c478bd9Sstevel@tonic-gate 		return ("Cyrix 6x86");
17917c478bd9Sstevel@tonic-gate 	case X86_TYPE_CYRIX_6x86L:
17927c478bd9Sstevel@tonic-gate 		return ("Cyrix 6x86L");
17937c478bd9Sstevel@tonic-gate 	case X86_TYPE_CYRIX_6x86MX:
17947c478bd9Sstevel@tonic-gate 		return ("Cyrix 6x86MX");
17957c478bd9Sstevel@tonic-gate 	case X86_TYPE_CYRIX_GXm:
17967c478bd9Sstevel@tonic-gate 		return ("Cyrix GXm");
17977c478bd9Sstevel@tonic-gate 	case X86_TYPE_CYRIX_MediaGX:
17987c478bd9Sstevel@tonic-gate 		return ("Cyrix MediaGX");
17997c478bd9Sstevel@tonic-gate 	case X86_TYPE_CYRIX_MII:
18007c478bd9Sstevel@tonic-gate 		return ("Cyrix M2");
18017c478bd9Sstevel@tonic-gate 	case X86_TYPE_VIA_CYRIX_III:
18027c478bd9Sstevel@tonic-gate 		return ("VIA Cyrix M3");
18037c478bd9Sstevel@tonic-gate 	default:
18047c478bd9Sstevel@tonic-gate 		/*
18057c478bd9Sstevel@tonic-gate 		 * Have another wild guess ..
18067c478bd9Sstevel@tonic-gate 		 */
18077c478bd9Sstevel@tonic-gate 		if (cpi->cpi_family == 4 && cpi->cpi_model == 9)
18087c478bd9Sstevel@tonic-gate 			return ("Cyrix 5x86");
18097c478bd9Sstevel@tonic-gate 		else if (cpi->cpi_family == 5) {
18107c478bd9Sstevel@tonic-gate 			switch (cpi->cpi_model) {
18117c478bd9Sstevel@tonic-gate 			case 2:
18127c478bd9Sstevel@tonic-gate 				return ("Cyrix 6x86");	/* Cyrix M1 */
18137c478bd9Sstevel@tonic-gate 			case 4:
18147c478bd9Sstevel@tonic-gate 				return ("Cyrix MediaGX");
18157c478bd9Sstevel@tonic-gate 			default:
18167c478bd9Sstevel@tonic-gate 				break;
18177c478bd9Sstevel@tonic-gate 			}
18187c478bd9Sstevel@tonic-gate 		} else if (cpi->cpi_family == 6) {
18197c478bd9Sstevel@tonic-gate 			switch (cpi->cpi_model) {
18207c478bd9Sstevel@tonic-gate 			case 0:
18217c478bd9Sstevel@tonic-gate 				return ("Cyrix 6x86MX"); /* Cyrix M2? */
18227c478bd9Sstevel@tonic-gate 			case 5:
18237c478bd9Sstevel@tonic-gate 			case 6:
18247c478bd9Sstevel@tonic-gate 			case 7:
18257c478bd9Sstevel@tonic-gate 			case 8:
18267c478bd9Sstevel@tonic-gate 			case 9:
18277c478bd9Sstevel@tonic-gate 				return ("VIA C3");
18287c478bd9Sstevel@tonic-gate 			default:
18297c478bd9Sstevel@tonic-gate 				break;
18307c478bd9Sstevel@tonic-gate 			}
18317c478bd9Sstevel@tonic-gate 		}
18327c478bd9Sstevel@tonic-gate 		break;
18337c478bd9Sstevel@tonic-gate 	}
18347c478bd9Sstevel@tonic-gate 	return (NULL);
18357c478bd9Sstevel@tonic-gate }
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate /*
18387c478bd9Sstevel@tonic-gate  * This only gets called in the case that the CPU extended
18397c478bd9Sstevel@tonic-gate  * feature brand string (0x80000002, 0x80000003, 0x80000004)
18407c478bd9Sstevel@tonic-gate  * aren't available, or contain null bytes for some reason.
18417c478bd9Sstevel@tonic-gate  */
18427c478bd9Sstevel@tonic-gate static void
18437c478bd9Sstevel@tonic-gate fabricate_brandstr(struct cpuid_info *cpi)
18447c478bd9Sstevel@tonic-gate {
18457c478bd9Sstevel@tonic-gate 	const char *brand = NULL;
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
18487c478bd9Sstevel@tonic-gate 	case X86_VENDOR_Intel:
18497c478bd9Sstevel@tonic-gate 		brand = intel_cpubrand(cpi);
18507c478bd9Sstevel@tonic-gate 		break;
18517c478bd9Sstevel@tonic-gate 	case X86_VENDOR_AMD:
18527c478bd9Sstevel@tonic-gate 		brand = amd_cpubrand(cpi);
18537c478bd9Sstevel@tonic-gate 		break;
18547c478bd9Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
18557c478bd9Sstevel@tonic-gate 		brand = cyrix_cpubrand(cpi, x86_type);
18567c478bd9Sstevel@tonic-gate 		break;
18577c478bd9Sstevel@tonic-gate 	case X86_VENDOR_NexGen:
18587c478bd9Sstevel@tonic-gate 		if (cpi->cpi_family == 5 && cpi->cpi_model == 0)
18597c478bd9Sstevel@tonic-gate 			brand = "NexGen Nx586";
18607c478bd9Sstevel@tonic-gate 		break;
18617c478bd9Sstevel@tonic-gate 	case X86_VENDOR_Centaur:
18627c478bd9Sstevel@tonic-gate 		if (cpi->cpi_family == 5)
18637c478bd9Sstevel@tonic-gate 			switch (cpi->cpi_model) {
18647c478bd9Sstevel@tonic-gate 			case 4:
18657c478bd9Sstevel@tonic-gate 				brand = "Centaur C6";
18667c478bd9Sstevel@tonic-gate 				break;
18677c478bd9Sstevel@tonic-gate 			case 8:
18687c478bd9Sstevel@tonic-gate 				brand = "Centaur C2";
18697c478bd9Sstevel@tonic-gate 				break;
18707c478bd9Sstevel@tonic-gate 			case 9:
18717c478bd9Sstevel@tonic-gate 				brand = "Centaur C3";
18727c478bd9Sstevel@tonic-gate 				break;
18737c478bd9Sstevel@tonic-gate 			default:
18747c478bd9Sstevel@tonic-gate 				break;
18757c478bd9Sstevel@tonic-gate 			}
18767c478bd9Sstevel@tonic-gate 		break;
18777c478bd9Sstevel@tonic-gate 	case X86_VENDOR_Rise:
18787c478bd9Sstevel@tonic-gate 		if (cpi->cpi_family == 5 &&
18797c478bd9Sstevel@tonic-gate 		    (cpi->cpi_model == 0 || cpi->cpi_model == 2))
18807c478bd9Sstevel@tonic-gate 			brand = "Rise mP6";
18817c478bd9Sstevel@tonic-gate 		break;
18827c478bd9Sstevel@tonic-gate 	case X86_VENDOR_SiS:
18837c478bd9Sstevel@tonic-gate 		if (cpi->cpi_family == 5 && cpi->cpi_model == 0)
18847c478bd9Sstevel@tonic-gate 			brand = "SiS 55x";
18857c478bd9Sstevel@tonic-gate 		break;
18867c478bd9Sstevel@tonic-gate 	case X86_VENDOR_TM:
18877c478bd9Sstevel@tonic-gate 		if (cpi->cpi_family == 5 && cpi->cpi_model == 4)
18887c478bd9Sstevel@tonic-gate 			brand = "Transmeta Crusoe TM3x00 or TM5x00";
18897c478bd9Sstevel@tonic-gate 		break;
18907c478bd9Sstevel@tonic-gate 	case X86_VENDOR_NSC:
18917c478bd9Sstevel@tonic-gate 	case X86_VENDOR_UMC:
18927c478bd9Sstevel@tonic-gate 	default:
18937c478bd9Sstevel@tonic-gate 		break;
18947c478bd9Sstevel@tonic-gate 	}
18957c478bd9Sstevel@tonic-gate 	if (brand) {
18967c478bd9Sstevel@tonic-gate 		(void) strcpy((char *)cpi->cpi_brandstr, brand);
18977c478bd9Sstevel@tonic-gate 		return;
18987c478bd9Sstevel@tonic-gate 	}
18997c478bd9Sstevel@tonic-gate 
19007c478bd9Sstevel@tonic-gate 	/*
19017c478bd9Sstevel@tonic-gate 	 * If all else fails ...
19027c478bd9Sstevel@tonic-gate 	 */
19037c478bd9Sstevel@tonic-gate 	(void) snprintf(cpi->cpi_brandstr, sizeof (cpi->cpi_brandstr),
19047c478bd9Sstevel@tonic-gate 	    "%s %d.%d.%d", cpi->cpi_vendorstr, cpi->cpi_family,
19057c478bd9Sstevel@tonic-gate 	    cpi->cpi_model, cpi->cpi_step);
19067c478bd9Sstevel@tonic-gate }
19077c478bd9Sstevel@tonic-gate 
19087c478bd9Sstevel@tonic-gate /*
19097c478bd9Sstevel@tonic-gate  * This routine is called just after kernel memory allocation
19107c478bd9Sstevel@tonic-gate  * becomes available on cpu0, and as part of mp_startup() on
19117c478bd9Sstevel@tonic-gate  * the other cpus.
19127c478bd9Sstevel@tonic-gate  *
1913d129bde2Sesaxe  * Fixup the brand string, and collect any information from cpuid
1914d129bde2Sesaxe  * that requires dynamicically allocated storage to represent.
19157c478bd9Sstevel@tonic-gate  */
19167c478bd9Sstevel@tonic-gate /*ARGSUSED*/
19177c478bd9Sstevel@tonic-gate void
19187c478bd9Sstevel@tonic-gate cpuid_pass3(cpu_t *cpu)
19197c478bd9Sstevel@tonic-gate {
1920d129bde2Sesaxe 	int	i, max, shft, level, size;
1921d129bde2Sesaxe 	struct cpuid_regs regs;
1922d129bde2Sesaxe 	struct cpuid_regs *cp;
19237c478bd9Sstevel@tonic-gate 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
19247c478bd9Sstevel@tonic-gate 
19257c478bd9Sstevel@tonic-gate 	ASSERT(cpi->cpi_pass == 2);
19267c478bd9Sstevel@tonic-gate 
1927d129bde2Sesaxe 	/*
1928d129bde2Sesaxe 	 * Function 4: Deterministic cache parameters
1929d129bde2Sesaxe 	 *
1930d129bde2Sesaxe 	 * Take this opportunity to detect the number of threads
1931d129bde2Sesaxe 	 * sharing the last level cache, and construct a corresponding
1932d129bde2Sesaxe 	 * cache id. The respective cpuid_info members are initialized
1933d129bde2Sesaxe 	 * to the default case of "no last level cache sharing".
1934d129bde2Sesaxe 	 */
1935d129bde2Sesaxe 	cpi->cpi_ncpu_shr_last_cache = 1;
1936d129bde2Sesaxe 	cpi->cpi_last_lvl_cacheid = cpu->cpu_id;
1937d129bde2Sesaxe 
1938d129bde2Sesaxe 	if (cpi->cpi_maxeax >= 4 && cpi->cpi_vendor == X86_VENDOR_Intel) {
1939d129bde2Sesaxe 
1940d129bde2Sesaxe 		/*
1941d129bde2Sesaxe 		 * Find the # of elements (size) returned by fn 4, and along
1942d129bde2Sesaxe 		 * the way detect last level cache sharing details.
1943d129bde2Sesaxe 		 */
1944d129bde2Sesaxe 		bzero(&regs, sizeof (regs));
1945d129bde2Sesaxe 		cp = &regs;
1946d129bde2Sesaxe 		for (i = 0, max = 0; i < CPI_FN4_ECX_MAX; i++) {
1947d129bde2Sesaxe 			cp->cp_eax = 4;
1948d129bde2Sesaxe 			cp->cp_ecx = i;
1949d129bde2Sesaxe 
1950d129bde2Sesaxe 			(void) __cpuid_insn(cp);
1951d129bde2Sesaxe 
1952d129bde2Sesaxe 			if (CPI_CACHE_TYPE(cp) == 0)
1953d129bde2Sesaxe 				break;
1954d129bde2Sesaxe 			level = CPI_CACHE_LVL(cp);
1955d129bde2Sesaxe 			if (level > max) {
1956d129bde2Sesaxe 				max = level;
1957d129bde2Sesaxe 				cpi->cpi_ncpu_shr_last_cache =
1958d129bde2Sesaxe 				    CPI_NTHR_SHR_CACHE(cp) + 1;
1959d129bde2Sesaxe 			}
1960d129bde2Sesaxe 		}
1961d129bde2Sesaxe 		cpi->cpi_std_4_size = size = i;
1962d129bde2Sesaxe 
1963d129bde2Sesaxe 		/*
1964d129bde2Sesaxe 		 * Allocate the cpi_std_4 array. The first element
1965d129bde2Sesaxe 		 * references the regs for fn 4, %ecx == 0, which
1966d129bde2Sesaxe 		 * cpuid_pass2() stashed in cpi->cpi_std[4].
1967d129bde2Sesaxe 		 */
1968d129bde2Sesaxe 		if (size > 0) {
1969d129bde2Sesaxe 			cpi->cpi_std_4 =
1970d129bde2Sesaxe 			    kmem_alloc(size * sizeof (cp), KM_SLEEP);
1971d129bde2Sesaxe 			cpi->cpi_std_4[0] = &cpi->cpi_std[4];
1972d129bde2Sesaxe 
1973d129bde2Sesaxe 			/*
1974d129bde2Sesaxe 			 * Allocate storage to hold the additional regs
1975d129bde2Sesaxe 			 * for function 4, %ecx == 1 .. cpi_std_4_size.
1976d129bde2Sesaxe 			 *
1977d129bde2Sesaxe 			 * The regs for fn 4, %ecx == 0 has already
1978d129bde2Sesaxe 			 * been allocated as indicated above.
1979d129bde2Sesaxe 			 */
1980d129bde2Sesaxe 			for (i = 1; i < size; i++) {
1981d129bde2Sesaxe 				cp = cpi->cpi_std_4[i] =
1982d129bde2Sesaxe 				    kmem_zalloc(sizeof (regs), KM_SLEEP);
1983d129bde2Sesaxe 				cp->cp_eax = 4;
1984d129bde2Sesaxe 				cp->cp_ecx = i;
1985d129bde2Sesaxe 
1986d129bde2Sesaxe 				(void) __cpuid_insn(cp);
1987d129bde2Sesaxe 			}
1988d129bde2Sesaxe 		}
1989d129bde2Sesaxe 		/*
1990d129bde2Sesaxe 		 * Determine the number of bits needed to represent
1991d129bde2Sesaxe 		 * the number of CPUs sharing the last level cache.
1992d129bde2Sesaxe 		 *
1993d129bde2Sesaxe 		 * Shift off that number of bits from the APIC id to
1994d129bde2Sesaxe 		 * derive the cache id.
1995d129bde2Sesaxe 		 */
1996d129bde2Sesaxe 		shft = 0;
1997d129bde2Sesaxe 		for (i = 1; i < cpi->cpi_ncpu_shr_last_cache; i <<= 1)
1998d129bde2Sesaxe 			shft++;
1999b6917abeSmishra 		cpi->cpi_last_lvl_cacheid = cpi->cpi_apicid >> shft;
2000d129bde2Sesaxe 	}
2001d129bde2Sesaxe 
2002d129bde2Sesaxe 	/*
2003d129bde2Sesaxe 	 * Now fixup the brand string
2004d129bde2Sesaxe 	 */
20057c478bd9Sstevel@tonic-gate 	if ((cpi->cpi_xmaxeax & 0x80000000) == 0) {
20067c478bd9Sstevel@tonic-gate 		fabricate_brandstr(cpi);
2007d129bde2Sesaxe 	} else {
20087c478bd9Sstevel@tonic-gate 
20097c478bd9Sstevel@tonic-gate 		/*
20107c478bd9Sstevel@tonic-gate 		 * If we successfully extracted a brand string from the cpuid
20117c478bd9Sstevel@tonic-gate 		 * instruction, clean it up by removing leading spaces and
20127c478bd9Sstevel@tonic-gate 		 * similar junk.
20137c478bd9Sstevel@tonic-gate 		 */
20147c478bd9Sstevel@tonic-gate 		if (cpi->cpi_brandstr[0]) {
20157c478bd9Sstevel@tonic-gate 			size_t maxlen = sizeof (cpi->cpi_brandstr);
20167c478bd9Sstevel@tonic-gate 			char *src, *dst;
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate 			dst = src = (char *)cpi->cpi_brandstr;
20197c478bd9Sstevel@tonic-gate 			src[maxlen - 1] = '\0';
20207c478bd9Sstevel@tonic-gate 			/*
20217c478bd9Sstevel@tonic-gate 			 * strip leading spaces
20227c478bd9Sstevel@tonic-gate 			 */
20237c478bd9Sstevel@tonic-gate 			while (*src == ' ')
20247c478bd9Sstevel@tonic-gate 				src++;
20257c478bd9Sstevel@tonic-gate 			/*
20267c478bd9Sstevel@tonic-gate 			 * Remove any 'Genuine' or "Authentic" prefixes
20277c478bd9Sstevel@tonic-gate 			 */
20287c478bd9Sstevel@tonic-gate 			if (strncmp(src, "Genuine ", 8) == 0)
20297c478bd9Sstevel@tonic-gate 				src += 8;
20307c478bd9Sstevel@tonic-gate 			if (strncmp(src, "Authentic ", 10) == 0)
20317c478bd9Sstevel@tonic-gate 				src += 10;
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate 			/*
20347c478bd9Sstevel@tonic-gate 			 * Now do an in-place copy.
20357c478bd9Sstevel@tonic-gate 			 * Map (R) to (r) and (TM) to (tm).
20367c478bd9Sstevel@tonic-gate 			 * The era of teletypes is long gone, and there's
20377c478bd9Sstevel@tonic-gate 			 * -really- no need to shout.
20387c478bd9Sstevel@tonic-gate 			 */
20397c478bd9Sstevel@tonic-gate 			while (*src != '\0') {
20407c478bd9Sstevel@tonic-gate 				if (src[0] == '(') {
20417c478bd9Sstevel@tonic-gate 					if (strncmp(src + 1, "R)", 2) == 0) {
20427c478bd9Sstevel@tonic-gate 						(void) strncpy(dst, "(r)", 3);
20437c478bd9Sstevel@tonic-gate 						src += 3;
20447c478bd9Sstevel@tonic-gate 						dst += 3;
20457c478bd9Sstevel@tonic-gate 						continue;
20467c478bd9Sstevel@tonic-gate 					}
20477c478bd9Sstevel@tonic-gate 					if (strncmp(src + 1, "TM)", 3) == 0) {
20487c478bd9Sstevel@tonic-gate 						(void) strncpy(dst, "(tm)", 4);
20497c478bd9Sstevel@tonic-gate 						src += 4;
20507c478bd9Sstevel@tonic-gate 						dst += 4;
20517c478bd9Sstevel@tonic-gate 						continue;
20527c478bd9Sstevel@tonic-gate 					}
20537c478bd9Sstevel@tonic-gate 				}
20547c478bd9Sstevel@tonic-gate 				*dst++ = *src++;
20557c478bd9Sstevel@tonic-gate 			}
20567c478bd9Sstevel@tonic-gate 			*dst = '\0';
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate 			/*
20597c478bd9Sstevel@tonic-gate 			 * Finally, remove any trailing spaces
20607c478bd9Sstevel@tonic-gate 			 */
20617c478bd9Sstevel@tonic-gate 			while (--dst > cpi->cpi_brandstr)
20627c478bd9Sstevel@tonic-gate 				if (*dst == ' ')
20637c478bd9Sstevel@tonic-gate 					*dst = '\0';
20647c478bd9Sstevel@tonic-gate 				else
20657c478bd9Sstevel@tonic-gate 					break;
20667c478bd9Sstevel@tonic-gate 		} else
20677c478bd9Sstevel@tonic-gate 			fabricate_brandstr(cpi);
2068d129bde2Sesaxe 	}
20697c478bd9Sstevel@tonic-gate 	cpi->cpi_pass = 3;
20707c478bd9Sstevel@tonic-gate }
20717c478bd9Sstevel@tonic-gate 
20727c478bd9Sstevel@tonic-gate /*
20737c478bd9Sstevel@tonic-gate  * This routine is called out of bind_hwcap() much later in the life
20747c478bd9Sstevel@tonic-gate  * of the kernel (post_startup()).  The job of this routine is to resolve
20757c478bd9Sstevel@tonic-gate  * the hardware feature support and kernel support for those features into
20767c478bd9Sstevel@tonic-gate  * what we're actually going to tell applications via the aux vector.
20777c478bd9Sstevel@tonic-gate  */
20787c478bd9Sstevel@tonic-gate uint_t
20797c478bd9Sstevel@tonic-gate cpuid_pass4(cpu_t *cpu)
20807c478bd9Sstevel@tonic-gate {
20817c478bd9Sstevel@tonic-gate 	struct cpuid_info *cpi;
20827c478bd9Sstevel@tonic-gate 	uint_t hwcap_flags = 0;
20837c478bd9Sstevel@tonic-gate 
20847c478bd9Sstevel@tonic-gate 	if (cpu == NULL)
20857c478bd9Sstevel@tonic-gate 		cpu = CPU;
20867c478bd9Sstevel@tonic-gate 	cpi = cpu->cpu_m.mcpu_cpi;
20877c478bd9Sstevel@tonic-gate 
20887c478bd9Sstevel@tonic-gate 	ASSERT(cpi->cpi_pass == 3);
20897c478bd9Sstevel@tonic-gate 
20907c478bd9Sstevel@tonic-gate 	if (cpi->cpi_maxeax >= 1) {
20917c478bd9Sstevel@tonic-gate 		uint32_t *edx = &cpi->cpi_support[STD_EDX_FEATURES];
20927c478bd9Sstevel@tonic-gate 		uint32_t *ecx = &cpi->cpi_support[STD_ECX_FEATURES];
20937c478bd9Sstevel@tonic-gate 
20947c478bd9Sstevel@tonic-gate 		*edx = CPI_FEATURES_EDX(cpi);
20957c478bd9Sstevel@tonic-gate 		*ecx = CPI_FEATURES_ECX(cpi);
20967c478bd9Sstevel@tonic-gate 
20977c478bd9Sstevel@tonic-gate 		/*
20987c478bd9Sstevel@tonic-gate 		 * [these require explicit kernel support]
20997c478bd9Sstevel@tonic-gate 		 */
21007c478bd9Sstevel@tonic-gate 		if ((x86_feature & X86_SEP) == 0)
21017c478bd9Sstevel@tonic-gate 			*edx &= ~CPUID_INTC_EDX_SEP;
21027c478bd9Sstevel@tonic-gate 
21037c478bd9Sstevel@tonic-gate 		if ((x86_feature & X86_SSE) == 0)
21047c478bd9Sstevel@tonic-gate 			*edx &= ~(CPUID_INTC_EDX_FXSR|CPUID_INTC_EDX_SSE);
21057c478bd9Sstevel@tonic-gate 		if ((x86_feature & X86_SSE2) == 0)
21067c478bd9Sstevel@tonic-gate 			*edx &= ~CPUID_INTC_EDX_SSE2;
21077c478bd9Sstevel@tonic-gate 
21087c478bd9Sstevel@tonic-gate 		if ((x86_feature & X86_HTT) == 0)
21097c478bd9Sstevel@tonic-gate 			*edx &= ~CPUID_INTC_EDX_HTT;
21107c478bd9Sstevel@tonic-gate 
21117c478bd9Sstevel@tonic-gate 		if ((x86_feature & X86_SSE3) == 0)
21127c478bd9Sstevel@tonic-gate 			*ecx &= ~CPUID_INTC_ECX_SSE3;
21137c478bd9Sstevel@tonic-gate 
2114d0f8ff6eSkk208521 		if (cpi->cpi_vendor == X86_VENDOR_Intel) {
2115d0f8ff6eSkk208521 			if ((x86_feature & X86_SSSE3) == 0)
2116d0f8ff6eSkk208521 				*ecx &= ~CPUID_INTC_ECX_SSSE3;
2117d0f8ff6eSkk208521 			if ((x86_feature & X86_SSE4_1) == 0)
2118d0f8ff6eSkk208521 				*ecx &= ~CPUID_INTC_ECX_SSE4_1;
2119d0f8ff6eSkk208521 			if ((x86_feature & X86_SSE4_2) == 0)
2120d0f8ff6eSkk208521 				*ecx &= ~CPUID_INTC_ECX_SSE4_2;
2121a50a8b93SKuriakose Kuruvilla 			if ((x86_feature & X86_AES) == 0)
2122a50a8b93SKuriakose Kuruvilla 				*ecx &= ~CPUID_INTC_ECX_AES;
2123d0f8ff6eSkk208521 		}
2124d0f8ff6eSkk208521 
21257c478bd9Sstevel@tonic-gate 		/*
21267c478bd9Sstevel@tonic-gate 		 * [no explicit support required beyond x87 fp context]
21277c478bd9Sstevel@tonic-gate 		 */
21287c478bd9Sstevel@tonic-gate 		if (!fpu_exists)
21297c478bd9Sstevel@tonic-gate 			*edx &= ~(CPUID_INTC_EDX_FPU | CPUID_INTC_EDX_MMX);
21307c478bd9Sstevel@tonic-gate 
21317c478bd9Sstevel@tonic-gate 		/*
21327c478bd9Sstevel@tonic-gate 		 * Now map the supported feature vector to things that we
21337c478bd9Sstevel@tonic-gate 		 * think userland will care about.
21347c478bd9Sstevel@tonic-gate 		 */
21357c478bd9Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_SEP)
21367c478bd9Sstevel@tonic-gate 			hwcap_flags |= AV_386_SEP;
21377c478bd9Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_SSE)
21387c478bd9Sstevel@tonic-gate 			hwcap_flags |= AV_386_FXSR | AV_386_SSE;
21397c478bd9Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_SSE2)
21407c478bd9Sstevel@tonic-gate 			hwcap_flags |= AV_386_SSE2;
21417c478bd9Sstevel@tonic-gate 		if (*ecx & CPUID_INTC_ECX_SSE3)
21427c478bd9Sstevel@tonic-gate 			hwcap_flags |= AV_386_SSE3;
2143d0f8ff6eSkk208521 		if (cpi->cpi_vendor == X86_VENDOR_Intel) {
2144d0f8ff6eSkk208521 			if (*ecx & CPUID_INTC_ECX_SSSE3)
2145d0f8ff6eSkk208521 				hwcap_flags |= AV_386_SSSE3;
2146d0f8ff6eSkk208521 			if (*ecx & CPUID_INTC_ECX_SSE4_1)
2147d0f8ff6eSkk208521 				hwcap_flags |= AV_386_SSE4_1;
2148d0f8ff6eSkk208521 			if (*ecx & CPUID_INTC_ECX_SSE4_2)
2149d0f8ff6eSkk208521 				hwcap_flags |= AV_386_SSE4_2;
21505087e485SKrishnendu Sadhukhan - Sun Microsystems 			if (*ecx & CPUID_INTC_ECX_MOVBE)
21515087e485SKrishnendu Sadhukhan - Sun Microsystems 				hwcap_flags |= AV_386_MOVBE;
2152a50a8b93SKuriakose Kuruvilla 			if (*ecx & CPUID_INTC_ECX_AES)
2153a50a8b93SKuriakose Kuruvilla 				hwcap_flags |= AV_386_AES;
2154a50a8b93SKuriakose Kuruvilla 			if (*ecx & CPUID_INTC_ECX_PCLMULQDQ)
2155a50a8b93SKuriakose Kuruvilla 				hwcap_flags |= AV_386_PCLMULQDQ;
2156d0f8ff6eSkk208521 		}
2157f8801251Skk208521 		if (*ecx & CPUID_INTC_ECX_POPCNT)
2158f8801251Skk208521 			hwcap_flags |= AV_386_POPCNT;
21597c478bd9Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_FPU)
21607c478bd9Sstevel@tonic-gate 			hwcap_flags |= AV_386_FPU;
21617c478bd9Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_MMX)
21627c478bd9Sstevel@tonic-gate 			hwcap_flags |= AV_386_MMX;
21637c478bd9Sstevel@tonic-gate 
21647c478bd9Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_TSC)
21657c478bd9Sstevel@tonic-gate 			hwcap_flags |= AV_386_TSC;
21667c478bd9Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_CX8)
21677c478bd9Sstevel@tonic-gate 			hwcap_flags |= AV_386_CX8;
21687c478bd9Sstevel@tonic-gate 		if (*edx & CPUID_INTC_EDX_CMOV)
21697c478bd9Sstevel@tonic-gate 			hwcap_flags |= AV_386_CMOV;
21707c478bd9Sstevel@tonic-gate 		if (*ecx & CPUID_INTC_ECX_MON)
21717c478bd9Sstevel@tonic-gate 			hwcap_flags |= AV_386_MON;
21727c478bd9Sstevel@tonic-gate 		if (*ecx & CPUID_INTC_ECX_CX16)
21737c478bd9Sstevel@tonic-gate 			hwcap_flags |= AV_386_CX16;
21747c478bd9Sstevel@tonic-gate 	}
21757c478bd9Sstevel@tonic-gate 
21768949bcd6Sandrei 	if (x86_feature & X86_HTT)
21777c478bd9Sstevel@tonic-gate 		hwcap_flags |= AV_386_PAUSE;
21787c478bd9Sstevel@tonic-gate 
21797c478bd9Sstevel@tonic-gate 	if (cpi->cpi_xmaxeax < 0x80000001)
21807c478bd9Sstevel@tonic-gate 		goto pass4_done;
21817c478bd9Sstevel@tonic-gate 
21827c478bd9Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
21838949bcd6Sandrei 		struct cpuid_regs cp;
2184ae115bc7Smrj 		uint32_t *edx, *ecx;
21857c478bd9Sstevel@tonic-gate 
2186ae115bc7Smrj 	case X86_VENDOR_Intel:
2187ae115bc7Smrj 		/*
2188ae115bc7Smrj 		 * Seems like Intel duplicated what we necessary
2189ae115bc7Smrj 		 * here to make the initial crop of 64-bit OS's work.
2190ae115bc7Smrj 		 * Hopefully, those are the only "extended" bits
2191ae115bc7Smrj 		 * they'll add.
2192ae115bc7Smrj 		 */
2193ae115bc7Smrj 		/*FALLTHROUGH*/
2194ae115bc7Smrj 
21957c478bd9Sstevel@tonic-gate 	case X86_VENDOR_AMD:
21967c478bd9Sstevel@tonic-gate 		edx = &cpi->cpi_support[AMD_EDX_FEATURES];
2197ae115bc7Smrj 		ecx = &cpi->cpi_support[AMD_ECX_FEATURES];
21987c478bd9Sstevel@tonic-gate 
21997c478bd9Sstevel@tonic-gate 		*edx = CPI_FEATURES_XTD_EDX(cpi);
2200ae115bc7Smrj 		*ecx = CPI_FEATURES_XTD_ECX(cpi);
2201ae115bc7Smrj 
2202ae115bc7Smrj 		/*
2203ae115bc7Smrj 		 * [these features require explicit kernel support]
2204ae115bc7Smrj 		 */
2205ae115bc7Smrj 		switch (cpi->cpi_vendor) {
2206ae115bc7Smrj 		case X86_VENDOR_Intel:
2207d36ea5d8Ssudheer 			if ((x86_feature & X86_TSCP) == 0)
2208d36ea5d8Ssudheer 				*edx &= ~CPUID_AMD_EDX_TSCP;
2209ae115bc7Smrj 			break;
2210ae115bc7Smrj 
2211ae115bc7Smrj 		case X86_VENDOR_AMD:
2212ae115bc7Smrj 			if ((x86_feature & X86_TSCP) == 0)
2213ae115bc7Smrj 				*edx &= ~CPUID_AMD_EDX_TSCP;
2214f8801251Skk208521 			if ((x86_feature & X86_SSE4A) == 0)
2215f8801251Skk208521 				*ecx &= ~CPUID_AMD_ECX_SSE4A;
2216ae115bc7Smrj 			break;
2217ae115bc7Smrj 
2218ae115bc7Smrj 		default:
2219ae115bc7Smrj 			break;
2220ae115bc7Smrj 		}
22217c478bd9Sstevel@tonic-gate 
22227c478bd9Sstevel@tonic-gate 		/*
22237c478bd9Sstevel@tonic-gate 		 * [no explicit support required beyond
22247c478bd9Sstevel@tonic-gate 		 * x87 fp context and exception handlers]
22257c478bd9Sstevel@tonic-gate 		 */
22267c478bd9Sstevel@tonic-gate 		if (!fpu_exists)
22277c478bd9Sstevel@tonic-gate 			*edx &= ~(CPUID_AMD_EDX_MMXamd |
22287c478bd9Sstevel@tonic-gate 			    CPUID_AMD_EDX_3DNow | CPUID_AMD_EDX_3DNowx);
22297c478bd9Sstevel@tonic-gate 
22307c478bd9Sstevel@tonic-gate 		if ((x86_feature & X86_NX) == 0)
22317c478bd9Sstevel@tonic-gate 			*edx &= ~CPUID_AMD_EDX_NX;
2232ae115bc7Smrj #if !defined(__amd64)
22337c478bd9Sstevel@tonic-gate 		*edx &= ~CPUID_AMD_EDX_LM;
22347c478bd9Sstevel@tonic-gate #endif
22357c478bd9Sstevel@tonic-gate 		/*
22367c478bd9Sstevel@tonic-gate 		 * Now map the supported feature vector to
22377c478bd9Sstevel@tonic-gate 		 * things that we think userland will care about.
22387c478bd9Sstevel@tonic-gate 		 */
2239ae115bc7Smrj #if defined(__amd64)
22407c478bd9Sstevel@tonic-gate 		if (*edx & CPUID_AMD_EDX_SYSC)
22417c478bd9Sstevel@tonic-gate 			hwcap_flags |= AV_386_AMD_SYSC;
2242ae115bc7Smrj #endif
22437c478bd9Sstevel@tonic-gate 		if (*edx & CPUID_AMD_EDX_MMXamd)
22447c478bd9Sstevel@tonic-gate 			hwcap_flags |= AV_386_AMD_MMX;
22457c478bd9Sstevel@tonic-gate 		if (*edx & CPUID_AMD_EDX_3DNow)
22467c478bd9Sstevel@tonic-gate 			hwcap_flags |= AV_386_AMD_3DNow;
22477c478bd9Sstevel@tonic-gate 		if (*edx & CPUID_AMD_EDX_3DNowx)
22487c478bd9Sstevel@tonic-gate 			hwcap_flags |= AV_386_AMD_3DNowx;
2249ae115bc7Smrj 
2250ae115bc7Smrj 		switch (cpi->cpi_vendor) {
2251ae115bc7Smrj 		case X86_VENDOR_AMD:
2252ae115bc7Smrj 			if (*edx & CPUID_AMD_EDX_TSCP)
2253ae115bc7Smrj 				hwcap_flags |= AV_386_TSCP;
2254ae115bc7Smrj 			if (*ecx & CPUID_AMD_ECX_AHF64)
2255ae115bc7Smrj 				hwcap_flags |= AV_386_AHF;
2256f8801251Skk208521 			if (*ecx & CPUID_AMD_ECX_SSE4A)
2257f8801251Skk208521 				hwcap_flags |= AV_386_AMD_SSE4A;
2258f8801251Skk208521 			if (*ecx & CPUID_AMD_ECX_LZCNT)
2259f8801251Skk208521 				hwcap_flags |= AV_386_AMD_LZCNT;
2260ae115bc7Smrj 			break;
2261ae115bc7Smrj 
2262ae115bc7Smrj 		case X86_VENDOR_Intel:
2263d36ea5d8Ssudheer 			if (*edx & CPUID_AMD_EDX_TSCP)
2264d36ea5d8Ssudheer 				hwcap_flags |= AV_386_TSCP;
2265ae115bc7Smrj 			/*
2266ae115bc7Smrj 			 * Aarrgh.
2267ae115bc7Smrj 			 * Intel uses a different bit in the same word.
2268ae115bc7Smrj 			 */
2269ae115bc7Smrj 			if (*ecx & CPUID_INTC_ECX_AHF64)
2270ae115bc7Smrj 				hwcap_flags |= AV_386_AHF;
2271ae115bc7Smrj 			break;
2272ae115bc7Smrj 
2273ae115bc7Smrj 		default:
2274ae115bc7Smrj 			break;
2275ae115bc7Smrj 		}
22767c478bd9Sstevel@tonic-gate 		break;
22777c478bd9Sstevel@tonic-gate 
22787c478bd9Sstevel@tonic-gate 	case X86_VENDOR_TM:
22798949bcd6Sandrei 		cp.cp_eax = 0x80860001;
22808949bcd6Sandrei 		(void) __cpuid_insn(&cp);
22818949bcd6Sandrei 		cpi->cpi_support[TM_EDX_FEATURES] = cp.cp_edx;
22827c478bd9Sstevel@tonic-gate 		break;
22837c478bd9Sstevel@tonic-gate 
22847c478bd9Sstevel@tonic-gate 	default:
22857c478bd9Sstevel@tonic-gate 		break;
22867c478bd9Sstevel@tonic-gate 	}
22877c478bd9Sstevel@tonic-gate 
22887c478bd9Sstevel@tonic-gate pass4_done:
22897c478bd9Sstevel@tonic-gate 	cpi->cpi_pass = 4;
22907c478bd9Sstevel@tonic-gate 	return (hwcap_flags);
22917c478bd9Sstevel@tonic-gate }
22927c478bd9Sstevel@tonic-gate 
22937c478bd9Sstevel@tonic-gate 
22947c478bd9Sstevel@tonic-gate /*
22957c478bd9Sstevel@tonic-gate  * Simulate the cpuid instruction using the data we previously
22967c478bd9Sstevel@tonic-gate  * captured about this CPU.  We try our best to return the truth
22977c478bd9Sstevel@tonic-gate  * about the hardware, independently of kernel support.
22987c478bd9Sstevel@tonic-gate  */
22997c478bd9Sstevel@tonic-gate uint32_t
23008949bcd6Sandrei cpuid_insn(cpu_t *cpu, struct cpuid_regs *cp)
23017c478bd9Sstevel@tonic-gate {
23027c478bd9Sstevel@tonic-gate 	struct cpuid_info *cpi;
23038949bcd6Sandrei 	struct cpuid_regs *xcp;
23047c478bd9Sstevel@tonic-gate 
23057c478bd9Sstevel@tonic-gate 	if (cpu == NULL)
23067c478bd9Sstevel@tonic-gate 		cpu = CPU;
23077c478bd9Sstevel@tonic-gate 	cpi = cpu->cpu_m.mcpu_cpi;
23087c478bd9Sstevel@tonic-gate 
23097c478bd9Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 3));
23107c478bd9Sstevel@tonic-gate 
23117c478bd9Sstevel@tonic-gate 	/*
23127c478bd9Sstevel@tonic-gate 	 * CPUID data is cached in two separate places: cpi_std for standard
23137c478bd9Sstevel@tonic-gate 	 * CPUID functions, and cpi_extd for extended CPUID functions.
23147c478bd9Sstevel@tonic-gate 	 */
23158949bcd6Sandrei 	if (cp->cp_eax <= cpi->cpi_maxeax && cp->cp_eax < NMAX_CPI_STD)
23168949bcd6Sandrei 		xcp = &cpi->cpi_std[cp->cp_eax];
23178949bcd6Sandrei 	else if (cp->cp_eax >= 0x80000000 && cp->cp_eax <= cpi->cpi_xmaxeax &&
23188949bcd6Sandrei 	    cp->cp_eax < 0x80000000 + NMAX_CPI_EXTD)
23198949bcd6Sandrei 		xcp = &cpi->cpi_extd[cp->cp_eax - 0x80000000];
23207c478bd9Sstevel@tonic-gate 	else
23217c478bd9Sstevel@tonic-gate 		/*
23227c478bd9Sstevel@tonic-gate 		 * The caller is asking for data from an input parameter which
23237c478bd9Sstevel@tonic-gate 		 * the kernel has not cached.  In this case we go fetch from
23247c478bd9Sstevel@tonic-gate 		 * the hardware and return the data directly to the user.
23257c478bd9Sstevel@tonic-gate 		 */
23268949bcd6Sandrei 		return (__cpuid_insn(cp));
23278949bcd6Sandrei 
23288949bcd6Sandrei 	cp->cp_eax = xcp->cp_eax;
23298949bcd6Sandrei 	cp->cp_ebx = xcp->cp_ebx;
23308949bcd6Sandrei 	cp->cp_ecx = xcp->cp_ecx;
23318949bcd6Sandrei 	cp->cp_edx = xcp->cp_edx;
23327c478bd9Sstevel@tonic-gate 	return (cp->cp_eax);
23337c478bd9Sstevel@tonic-gate }
23347c478bd9Sstevel@tonic-gate 
23357c478bd9Sstevel@tonic-gate int
23367c478bd9Sstevel@tonic-gate cpuid_checkpass(cpu_t *cpu, int pass)
23377c478bd9Sstevel@tonic-gate {
23387c478bd9Sstevel@tonic-gate 	return (cpu != NULL && cpu->cpu_m.mcpu_cpi != NULL &&
23397c478bd9Sstevel@tonic-gate 	    cpu->cpu_m.mcpu_cpi->cpi_pass >= pass);
23407c478bd9Sstevel@tonic-gate }
23417c478bd9Sstevel@tonic-gate 
23427c478bd9Sstevel@tonic-gate int
23437c478bd9Sstevel@tonic-gate cpuid_getbrandstr(cpu_t *cpu, char *s, size_t n)
23447c478bd9Sstevel@tonic-gate {
23457c478bd9Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 3));
23467c478bd9Sstevel@tonic-gate 
23477c478bd9Sstevel@tonic-gate 	return (snprintf(s, n, "%s", cpu->cpu_m.mcpu_cpi->cpi_brandstr));
23487c478bd9Sstevel@tonic-gate }
23497c478bd9Sstevel@tonic-gate 
23507c478bd9Sstevel@tonic-gate int
23518949bcd6Sandrei cpuid_is_cmt(cpu_t *cpu)
23527c478bd9Sstevel@tonic-gate {
23537c478bd9Sstevel@tonic-gate 	if (cpu == NULL)
23547c478bd9Sstevel@tonic-gate 		cpu = CPU;
23557c478bd9Sstevel@tonic-gate 
23567c478bd9Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
23577c478bd9Sstevel@tonic-gate 
23587c478bd9Sstevel@tonic-gate 	return (cpu->cpu_m.mcpu_cpi->cpi_chipid >= 0);
23597c478bd9Sstevel@tonic-gate }
23607c478bd9Sstevel@tonic-gate 
23617c478bd9Sstevel@tonic-gate /*
23627c478bd9Sstevel@tonic-gate  * AMD and Intel both implement the 64-bit variant of the syscall
23637c478bd9Sstevel@tonic-gate  * instruction (syscallq), so if there's -any- support for syscall,
23647c478bd9Sstevel@tonic-gate  * cpuid currently says "yes, we support this".
23657c478bd9Sstevel@tonic-gate  *
23667c478bd9Sstevel@tonic-gate  * However, Intel decided to -not- implement the 32-bit variant of the
23677c478bd9Sstevel@tonic-gate  * syscall instruction, so we provide a predicate to allow our caller
23687c478bd9Sstevel@tonic-gate  * to test that subtlety here.
2369843e1988Sjohnlev  *
2370843e1988Sjohnlev  * XXPV	Currently, 32-bit syscall instructions don't work via the hypervisor,
2371843e1988Sjohnlev  *	even in the case where the hardware would in fact support it.
23727c478bd9Sstevel@tonic-gate  */
23737c478bd9Sstevel@tonic-gate /*ARGSUSED*/
23747c478bd9Sstevel@tonic-gate int
23757c478bd9Sstevel@tonic-gate cpuid_syscall32_insn(cpu_t *cpu)
23767c478bd9Sstevel@tonic-gate {
23777c478bd9Sstevel@tonic-gate 	ASSERT(cpuid_checkpass((cpu == NULL ? CPU : cpu), 1));
23787c478bd9Sstevel@tonic-gate 
2379843e1988Sjohnlev #if !defined(__xpv)
2380ae115bc7Smrj 	if (cpu == NULL)
2381ae115bc7Smrj 		cpu = CPU;
2382ae115bc7Smrj 
2383ae115bc7Smrj 	/*CSTYLED*/
2384ae115bc7Smrj 	{
2385ae115bc7Smrj 		struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
2386ae115bc7Smrj 
2387ae115bc7Smrj 		if (cpi->cpi_vendor == X86_VENDOR_AMD &&
2388ae115bc7Smrj 		    cpi->cpi_xmaxeax >= 0x80000001 &&
2389ae115bc7Smrj 		    (CPI_FEATURES_XTD_EDX(cpi) & CPUID_AMD_EDX_SYSC))
2390ae115bc7Smrj 			return (1);
2391ae115bc7Smrj 	}
2392843e1988Sjohnlev #endif
23937c478bd9Sstevel@tonic-gate 	return (0);
23947c478bd9Sstevel@tonic-gate }
23957c478bd9Sstevel@tonic-gate 
23967c478bd9Sstevel@tonic-gate int
23977c478bd9Sstevel@tonic-gate cpuid_getidstr(cpu_t *cpu, char *s, size_t n)
23987c478bd9Sstevel@tonic-gate {
23997c478bd9Sstevel@tonic-gate 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
24007c478bd9Sstevel@tonic-gate 
24017c478bd9Sstevel@tonic-gate 	static const char fmt[] =
2402ecfa43a5Sdmick 	    "x86 (%s %X family %d model %d step %d clock %d MHz)";
24037c478bd9Sstevel@tonic-gate 	static const char fmt_ht[] =
2404ecfa43a5Sdmick 	    "x86 (chipid 0x%x %s %X family %d model %d step %d clock %d MHz)";
24057c478bd9Sstevel@tonic-gate 
24067c478bd9Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
24077c478bd9Sstevel@tonic-gate 
24088949bcd6Sandrei 	if (cpuid_is_cmt(cpu))
24097c478bd9Sstevel@tonic-gate 		return (snprintf(s, n, fmt_ht, cpi->cpi_chipid,
2410ecfa43a5Sdmick 		    cpi->cpi_vendorstr, cpi->cpi_std[1].cp_eax,
2411ecfa43a5Sdmick 		    cpi->cpi_family, cpi->cpi_model,
24127c478bd9Sstevel@tonic-gate 		    cpi->cpi_step, cpu->cpu_type_info.pi_clock));
24137c478bd9Sstevel@tonic-gate 	return (snprintf(s, n, fmt,
2414ecfa43a5Sdmick 	    cpi->cpi_vendorstr, cpi->cpi_std[1].cp_eax,
2415ecfa43a5Sdmick 	    cpi->cpi_family, cpi->cpi_model,
24167c478bd9Sstevel@tonic-gate 	    cpi->cpi_step, cpu->cpu_type_info.pi_clock));
24177c478bd9Sstevel@tonic-gate }
24187c478bd9Sstevel@tonic-gate 
24197c478bd9Sstevel@tonic-gate const char *
24207c478bd9Sstevel@tonic-gate cpuid_getvendorstr(cpu_t *cpu)
24217c478bd9Sstevel@tonic-gate {
24227c478bd9Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
24237c478bd9Sstevel@tonic-gate 	return ((const char *)cpu->cpu_m.mcpu_cpi->cpi_vendorstr);
24247c478bd9Sstevel@tonic-gate }
24257c478bd9Sstevel@tonic-gate 
24267c478bd9Sstevel@tonic-gate uint_t
24277c478bd9Sstevel@tonic-gate cpuid_getvendor(cpu_t *cpu)
24287c478bd9Sstevel@tonic-gate {
24297c478bd9Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
24307c478bd9Sstevel@tonic-gate 	return (cpu->cpu_m.mcpu_cpi->cpi_vendor);
24317c478bd9Sstevel@tonic-gate }
24327c478bd9Sstevel@tonic-gate 
24337c478bd9Sstevel@tonic-gate uint_t
24347c478bd9Sstevel@tonic-gate cpuid_getfamily(cpu_t *cpu)
24357c478bd9Sstevel@tonic-gate {
24367c478bd9Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
24377c478bd9Sstevel@tonic-gate 	return (cpu->cpu_m.mcpu_cpi->cpi_family);
24387c478bd9Sstevel@tonic-gate }
24397c478bd9Sstevel@tonic-gate 
24407c478bd9Sstevel@tonic-gate uint_t
24417c478bd9Sstevel@tonic-gate cpuid_getmodel(cpu_t *cpu)
24427c478bd9Sstevel@tonic-gate {
24437c478bd9Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
24447c478bd9Sstevel@tonic-gate 	return (cpu->cpu_m.mcpu_cpi->cpi_model);
24457c478bd9Sstevel@tonic-gate }
24467c478bd9Sstevel@tonic-gate 
24477c478bd9Sstevel@tonic-gate uint_t
24487c478bd9Sstevel@tonic-gate cpuid_get_ncpu_per_chip(cpu_t *cpu)
24497c478bd9Sstevel@tonic-gate {
24507c478bd9Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
24517c478bd9Sstevel@tonic-gate 	return (cpu->cpu_m.mcpu_cpi->cpi_ncpu_per_chip);
24527c478bd9Sstevel@tonic-gate }
24537c478bd9Sstevel@tonic-gate 
24547c478bd9Sstevel@tonic-gate uint_t
24558949bcd6Sandrei cpuid_get_ncore_per_chip(cpu_t *cpu)
24568949bcd6Sandrei {
24578949bcd6Sandrei 	ASSERT(cpuid_checkpass(cpu, 1));
24588949bcd6Sandrei 	return (cpu->cpu_m.mcpu_cpi->cpi_ncore_per_chip);
24598949bcd6Sandrei }
24608949bcd6Sandrei 
24618949bcd6Sandrei uint_t
2462d129bde2Sesaxe cpuid_get_ncpu_sharing_last_cache(cpu_t *cpu)
2463d129bde2Sesaxe {
2464d129bde2Sesaxe 	ASSERT(cpuid_checkpass(cpu, 2));
2465d129bde2Sesaxe 	return (cpu->cpu_m.mcpu_cpi->cpi_ncpu_shr_last_cache);
2466d129bde2Sesaxe }
2467d129bde2Sesaxe 
2468d129bde2Sesaxe id_t
2469d129bde2Sesaxe cpuid_get_last_lvl_cacheid(cpu_t *cpu)
2470d129bde2Sesaxe {
2471d129bde2Sesaxe 	ASSERT(cpuid_checkpass(cpu, 2));
2472d129bde2Sesaxe 	return (cpu->cpu_m.mcpu_cpi->cpi_last_lvl_cacheid);
2473d129bde2Sesaxe }
2474d129bde2Sesaxe 
2475d129bde2Sesaxe uint_t
24767c478bd9Sstevel@tonic-gate cpuid_getstep(cpu_t *cpu)
24777c478bd9Sstevel@tonic-gate {
24787c478bd9Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
24797c478bd9Sstevel@tonic-gate 	return (cpu->cpu_m.mcpu_cpi->cpi_step);
24807c478bd9Sstevel@tonic-gate }
24817c478bd9Sstevel@tonic-gate 
24822449e17fSsherrym uint_t
24832449e17fSsherrym cpuid_getsig(struct cpu *cpu)
24842449e17fSsherrym {
24852449e17fSsherrym 	ASSERT(cpuid_checkpass(cpu, 1));
24862449e17fSsherrym 	return (cpu->cpu_m.mcpu_cpi->cpi_std[1].cp_eax);
24872449e17fSsherrym }
24882449e17fSsherrym 
24898a40a695Sgavinm uint32_t
24908a40a695Sgavinm cpuid_getchiprev(struct cpu *cpu)
24918a40a695Sgavinm {
24928a40a695Sgavinm 	ASSERT(cpuid_checkpass(cpu, 1));
24938a40a695Sgavinm 	return (cpu->cpu_m.mcpu_cpi->cpi_chiprev);
24948a40a695Sgavinm }
24958a40a695Sgavinm 
24968a40a695Sgavinm const char *
24978a40a695Sgavinm cpuid_getchiprevstr(struct cpu *cpu)
24988a40a695Sgavinm {
24998a40a695Sgavinm 	ASSERT(cpuid_checkpass(cpu, 1));
25008a40a695Sgavinm 	return (cpu->cpu_m.mcpu_cpi->cpi_chiprevstr);
25018a40a695Sgavinm }
25028a40a695Sgavinm 
25038a40a695Sgavinm uint32_t
25048a40a695Sgavinm cpuid_getsockettype(struct cpu *cpu)
25058a40a695Sgavinm {
25068a40a695Sgavinm 	ASSERT(cpuid_checkpass(cpu, 1));
25078a40a695Sgavinm 	return (cpu->cpu_m.mcpu_cpi->cpi_socket);
25088a40a695Sgavinm }
25098a40a695Sgavinm 
251089e921d5SKuriakose Kuruvilla const char *
251189e921d5SKuriakose Kuruvilla cpuid_getsocketstr(cpu_t *cpu)
251289e921d5SKuriakose Kuruvilla {
251389e921d5SKuriakose Kuruvilla 	static const char *socketstr = NULL;
251489e921d5SKuriakose Kuruvilla 	struct cpuid_info *cpi;
251589e921d5SKuriakose Kuruvilla 
251689e921d5SKuriakose Kuruvilla 	ASSERT(cpuid_checkpass(cpu, 1));
251789e921d5SKuriakose Kuruvilla 	cpi = cpu->cpu_m.mcpu_cpi;
251889e921d5SKuriakose Kuruvilla 
251989e921d5SKuriakose Kuruvilla 	/* Assume that socket types are the same across the system */
252089e921d5SKuriakose Kuruvilla 	if (socketstr == NULL)
252189e921d5SKuriakose Kuruvilla 		socketstr = _cpuid_sktstr(cpi->cpi_vendor, cpi->cpi_family,
252289e921d5SKuriakose Kuruvilla 		    cpi->cpi_model, cpi->cpi_step);
252389e921d5SKuriakose Kuruvilla 
252489e921d5SKuriakose Kuruvilla 
252589e921d5SKuriakose Kuruvilla 	return (socketstr);
252689e921d5SKuriakose Kuruvilla }
252789e921d5SKuriakose Kuruvilla 
2528fb2f18f8Sesaxe int
2529fb2f18f8Sesaxe cpuid_get_chipid(cpu_t *cpu)
25307c478bd9Sstevel@tonic-gate {
25317c478bd9Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
25327c478bd9Sstevel@tonic-gate 
25338949bcd6Sandrei 	if (cpuid_is_cmt(cpu))
25347c478bd9Sstevel@tonic-gate 		return (cpu->cpu_m.mcpu_cpi->cpi_chipid);
25357c478bd9Sstevel@tonic-gate 	return (cpu->cpu_id);
25367c478bd9Sstevel@tonic-gate }
25377c478bd9Sstevel@tonic-gate 
25388949bcd6Sandrei id_t
2539fb2f18f8Sesaxe cpuid_get_coreid(cpu_t *cpu)
25408949bcd6Sandrei {
25418949bcd6Sandrei 	ASSERT(cpuid_checkpass(cpu, 1));
25428949bcd6Sandrei 	return (cpu->cpu_m.mcpu_cpi->cpi_coreid);
25438949bcd6Sandrei }
25448949bcd6Sandrei 
25457c478bd9Sstevel@tonic-gate int
254610569901Sgavinm cpuid_get_pkgcoreid(cpu_t *cpu)
254710569901Sgavinm {
254810569901Sgavinm 	ASSERT(cpuid_checkpass(cpu, 1));
254910569901Sgavinm 	return (cpu->cpu_m.mcpu_cpi->cpi_pkgcoreid);
255010569901Sgavinm }
255110569901Sgavinm 
255210569901Sgavinm int
2553fb2f18f8Sesaxe cpuid_get_clogid(cpu_t *cpu)
25547c478bd9Sstevel@tonic-gate {
25557c478bd9Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
25567c478bd9Sstevel@tonic-gate 	return (cpu->cpu_m.mcpu_cpi->cpi_clogid);
25577c478bd9Sstevel@tonic-gate }
25587c478bd9Sstevel@tonic-gate 
25592ef50f01SJoe Bonasera /*ARGSUSED*/
25602ef50f01SJoe Bonasera int
25612ef50f01SJoe Bonasera cpuid_have_cr8access(cpu_t *cpu)
25622ef50f01SJoe Bonasera {
25632ef50f01SJoe Bonasera #if defined(__amd64)
25642ef50f01SJoe Bonasera 	return (1);
25652ef50f01SJoe Bonasera #else
25662ef50f01SJoe Bonasera 	struct cpuid_info *cpi;
25672ef50f01SJoe Bonasera 
25682ef50f01SJoe Bonasera 	ASSERT(cpu != NULL);
25692ef50f01SJoe Bonasera 	cpi = cpu->cpu_m.mcpu_cpi;
25702ef50f01SJoe Bonasera 	if (cpi->cpi_vendor == X86_VENDOR_AMD && cpi->cpi_maxeax >= 1 &&
25712ef50f01SJoe Bonasera 	    (CPI_FEATURES_XTD_ECX(cpi) & CPUID_AMD_ECX_CR8D) != 0)
25722ef50f01SJoe Bonasera 		return (1);
25732ef50f01SJoe Bonasera 	return (0);
25742ef50f01SJoe Bonasera #endif
25752ef50f01SJoe Bonasera }
25762ef50f01SJoe Bonasera 
2577fa96bd91SMichael Corcoran uint32_t
2578fa96bd91SMichael Corcoran cpuid_get_apicid(cpu_t *cpu)
2579fa96bd91SMichael Corcoran {
2580fa96bd91SMichael Corcoran 	ASSERT(cpuid_checkpass(cpu, 1));
2581fa96bd91SMichael Corcoran 	if (cpu->cpu_m.mcpu_cpi->cpi_maxeax < 1) {
2582fa96bd91SMichael Corcoran 		return (UINT32_MAX);
2583fa96bd91SMichael Corcoran 	} else {
2584fa96bd91SMichael Corcoran 		return (cpu->cpu_m.mcpu_cpi->cpi_apicid);
2585fa96bd91SMichael Corcoran 	}
2586fa96bd91SMichael Corcoran }
2587fa96bd91SMichael Corcoran 
25887c478bd9Sstevel@tonic-gate void
25897c478bd9Sstevel@tonic-gate cpuid_get_addrsize(cpu_t *cpu, uint_t *pabits, uint_t *vabits)
25907c478bd9Sstevel@tonic-gate {
25917c478bd9Sstevel@tonic-gate 	struct cpuid_info *cpi;
25927c478bd9Sstevel@tonic-gate 
25937c478bd9Sstevel@tonic-gate 	if (cpu == NULL)
25947c478bd9Sstevel@tonic-gate 		cpu = CPU;
25957c478bd9Sstevel@tonic-gate 	cpi = cpu->cpu_m.mcpu_cpi;
25967c478bd9Sstevel@tonic-gate 
25977c478bd9Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
25987c478bd9Sstevel@tonic-gate 
25997c478bd9Sstevel@tonic-gate 	if (pabits)
26007c478bd9Sstevel@tonic-gate 		*pabits = cpi->cpi_pabits;
26017c478bd9Sstevel@tonic-gate 	if (vabits)
26027c478bd9Sstevel@tonic-gate 		*vabits = cpi->cpi_vabits;
26037c478bd9Sstevel@tonic-gate }
26047c478bd9Sstevel@tonic-gate 
26057c478bd9Sstevel@tonic-gate /*
26067c478bd9Sstevel@tonic-gate  * Returns the number of data TLB entries for a corresponding
26077c478bd9Sstevel@tonic-gate  * pagesize.  If it can't be computed, or isn't known, the
26087c478bd9Sstevel@tonic-gate  * routine returns zero.  If you ask about an architecturally
26097c478bd9Sstevel@tonic-gate  * impossible pagesize, the routine will panic (so that the
26107c478bd9Sstevel@tonic-gate  * hat implementor knows that things are inconsistent.)
26117c478bd9Sstevel@tonic-gate  */
26127c478bd9Sstevel@tonic-gate uint_t
26137c478bd9Sstevel@tonic-gate cpuid_get_dtlb_nent(cpu_t *cpu, size_t pagesize)
26147c478bd9Sstevel@tonic-gate {
26157c478bd9Sstevel@tonic-gate 	struct cpuid_info *cpi;
26167c478bd9Sstevel@tonic-gate 	uint_t dtlb_nent = 0;
26177c478bd9Sstevel@tonic-gate 
26187c478bd9Sstevel@tonic-gate 	if (cpu == NULL)
26197c478bd9Sstevel@tonic-gate 		cpu = CPU;
26207c478bd9Sstevel@tonic-gate 	cpi = cpu->cpu_m.mcpu_cpi;
26217c478bd9Sstevel@tonic-gate 
26227c478bd9Sstevel@tonic-gate 	ASSERT(cpuid_checkpass(cpu, 1));
26237c478bd9Sstevel@tonic-gate 
26247c478bd9Sstevel@tonic-gate 	/*
26257c478bd9Sstevel@tonic-gate 	 * Check the L2 TLB info
26267c478bd9Sstevel@tonic-gate 	 */
26277c478bd9Sstevel@tonic-gate 	if (cpi->cpi_xmaxeax >= 0x80000006) {
26288949bcd6Sandrei 		struct cpuid_regs *cp = &cpi->cpi_extd[6];
26297c478bd9Sstevel@tonic-gate 
26307c478bd9Sstevel@tonic-gate 		switch (pagesize) {
26317c478bd9Sstevel@tonic-gate 
26327c478bd9Sstevel@tonic-gate 		case 4 * 1024:
26337c478bd9Sstevel@tonic-gate 			/*
26347c478bd9Sstevel@tonic-gate 			 * All zero in the top 16 bits of the register
26357c478bd9Sstevel@tonic-gate 			 * indicates a unified TLB. Size is in low 16 bits.
26367c478bd9Sstevel@tonic-gate 			 */
26377c478bd9Sstevel@tonic-gate 			if ((cp->cp_ebx & 0xffff0000) == 0)
26387c478bd9Sstevel@tonic-gate 				dtlb_nent = cp->cp_ebx & 0x0000ffff;
26397c478bd9Sstevel@tonic-gate 			else
26407c478bd9Sstevel@tonic-gate 				dtlb_nent = BITX(cp->cp_ebx, 27, 16);
26417c478bd9Sstevel@tonic-gate 			break;
26427c478bd9Sstevel@tonic-gate 
26437c478bd9Sstevel@tonic-gate 		case 2 * 1024 * 1024:
26447c478bd9Sstevel@tonic-gate 			if ((cp->cp_eax & 0xffff0000) == 0)
26457c478bd9Sstevel@tonic-gate 				dtlb_nent = cp->cp_eax & 0x0000ffff;
26467c478bd9Sstevel@tonic-gate 			else
26477c478bd9Sstevel@tonic-gate 				dtlb_nent = BITX(cp->cp_eax, 27, 16);
26487c478bd9Sstevel@tonic-gate 			break;
26497c478bd9Sstevel@tonic-gate 
26507c478bd9Sstevel@tonic-gate 		default:
26517c478bd9Sstevel@tonic-gate 			panic("unknown L2 pagesize");
26527c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
26537c478bd9Sstevel@tonic-gate 		}
26547c478bd9Sstevel@tonic-gate 	}
26557c478bd9Sstevel@tonic-gate 
26567c478bd9Sstevel@tonic-gate 	if (dtlb_nent != 0)
26577c478bd9Sstevel@tonic-gate 		return (dtlb_nent);
26587c478bd9Sstevel@tonic-gate 
26597c478bd9Sstevel@tonic-gate 	/*
26607c478bd9Sstevel@tonic-gate 	 * No L2 TLB support for this size, try L1.
26617c478bd9Sstevel@tonic-gate 	 */
26627c478bd9Sstevel@tonic-gate 	if (cpi->cpi_xmaxeax >= 0x80000005) {
26638949bcd6Sandrei 		struct cpuid_regs *cp = &cpi->cpi_extd[5];
26647c478bd9Sstevel@tonic-gate 
26657c478bd9Sstevel@tonic-gate 		switch (pagesize) {
26667c478bd9Sstevel@tonic-gate 		case 4 * 1024:
26677c478bd9Sstevel@tonic-gate 			dtlb_nent = BITX(cp->cp_ebx, 23, 16);
26687c478bd9Sstevel@tonic-gate 			break;
26697c478bd9Sstevel@tonic-gate 		case 2 * 1024 * 1024:
26707c478bd9Sstevel@tonic-gate 			dtlb_nent = BITX(cp->cp_eax, 23, 16);
26717c478bd9Sstevel@tonic-gate 			break;
26727c478bd9Sstevel@tonic-gate 		default:
26737c478bd9Sstevel@tonic-gate 			panic("unknown L1 d-TLB pagesize");
26747c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
26757c478bd9Sstevel@tonic-gate 		}
26767c478bd9Sstevel@tonic-gate 	}
26777c478bd9Sstevel@tonic-gate 
26787c478bd9Sstevel@tonic-gate 	return (dtlb_nent);
26797c478bd9Sstevel@tonic-gate }
26807c478bd9Sstevel@tonic-gate 
26817c478bd9Sstevel@tonic-gate /*
26827c478bd9Sstevel@tonic-gate  * Return 0 if the erratum is not present or not applicable, positive
26837c478bd9Sstevel@tonic-gate  * if it is, and negative if the status of the erratum is unknown.
26847c478bd9Sstevel@tonic-gate  *
26857c478bd9Sstevel@tonic-gate  * See "Revision Guide for AMD Athlon(tm) 64 and AMD Opteron(tm)
26862201b277Skucharsk  * Processors" #25759, Rev 3.57, August 2005
26877c478bd9Sstevel@tonic-gate  */
26887c478bd9Sstevel@tonic-gate int
26897c478bd9Sstevel@tonic-gate cpuid_opteron_erratum(cpu_t *cpu, uint_t erratum)
26907c478bd9Sstevel@tonic-gate {
26917c478bd9Sstevel@tonic-gate 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
26928949bcd6Sandrei 	uint_t eax;
26937c478bd9Sstevel@tonic-gate 
2694ea99987eSsethg 	/*
2695ea99987eSsethg 	 * Bail out if this CPU isn't an AMD CPU, or if it's
2696ea99987eSsethg 	 * a legacy (32-bit) AMD CPU.
2697ea99987eSsethg 	 */
2698ea99987eSsethg 	if (cpi->cpi_vendor != X86_VENDOR_AMD ||
2699875b116eSkchow 	    cpi->cpi_family == 4 || cpi->cpi_family == 5 ||
2700875b116eSkchow 	    cpi->cpi_family == 6)
27018a40a695Sgavinm 
27027c478bd9Sstevel@tonic-gate 		return (0);
27037c478bd9Sstevel@tonic-gate 
27047c478bd9Sstevel@tonic-gate 	eax = cpi->cpi_std[1].cp_eax;
27057c478bd9Sstevel@tonic-gate 
27067c478bd9Sstevel@tonic-gate #define	SH_B0(eax)	(eax == 0xf40 || eax == 0xf50)
27077c478bd9Sstevel@tonic-gate #define	SH_B3(eax) 	(eax == 0xf51)
2708ee88d2b9Skchow #define	B(eax)		(SH_B0(eax) || SH_B3(eax))
27097c478bd9Sstevel@tonic-gate 
27107c478bd9Sstevel@tonic-gate #define	SH_C0(eax)	(eax == 0xf48 || eax == 0xf58)
27117c478bd9Sstevel@tonic-gate 
27127c478bd9Sstevel@tonic-gate #define	SH_CG(eax)	(eax == 0xf4a || eax == 0xf5a || eax == 0xf7a)
27137c478bd9Sstevel@tonic-gate #define	DH_CG(eax)	(eax == 0xfc0 || eax == 0xfe0 || eax == 0xff0)
27147c478bd9Sstevel@tonic-gate #define	CH_CG(eax)	(eax == 0xf82 || eax == 0xfb2)
2715ee88d2b9Skchow #define	CG(eax)		(SH_CG(eax) || DH_CG(eax) || CH_CG(eax))
27167c478bd9Sstevel@tonic-gate 
27177c478bd9Sstevel@tonic-gate #define	SH_D0(eax)	(eax == 0x10f40 || eax == 0x10f50 || eax == 0x10f70)
27187c478bd9Sstevel@tonic-gate #define	DH_D0(eax)	(eax == 0x10fc0 || eax == 0x10ff0)
27197c478bd9Sstevel@tonic-gate #define	CH_D0(eax)	(eax == 0x10f80 || eax == 0x10fb0)
2720ee88d2b9Skchow #define	D0(eax)		(SH_D0(eax) || DH_D0(eax) || CH_D0(eax))
27217c478bd9Sstevel@tonic-gate 
27227c478bd9Sstevel@tonic-gate #define	SH_E0(eax)	(eax == 0x20f50 || eax == 0x20f40 || eax == 0x20f70)
27237c478bd9Sstevel@tonic-gate #define	JH_E1(eax)	(eax == 0x20f10)	/* JH8_E0 had 0x20f30 */
27247c478bd9Sstevel@tonic-gate #define	DH_E3(eax)	(eax == 0x20fc0 || eax == 0x20ff0)
27257c478bd9Sstevel@tonic-gate #define	SH_E4(eax)	(eax == 0x20f51 || eax == 0x20f71)
27267c478bd9Sstevel@tonic-gate #define	BH_E4(eax)	(eax == 0x20fb1)
27277c478bd9Sstevel@tonic-gate #define	SH_E5(eax)	(eax == 0x20f42)
27287c478bd9Sstevel@tonic-gate #define	DH_E6(eax)	(eax == 0x20ff2 || eax == 0x20fc2)
27297c478bd9Sstevel@tonic-gate #define	JH_E6(eax)	(eax == 0x20f12 || eax == 0x20f32)
2730ee88d2b9Skchow #define	EX(eax)		(SH_E0(eax) || JH_E1(eax) || DH_E3(eax) || \
2731ee88d2b9Skchow 			    SH_E4(eax) || BH_E4(eax) || SH_E5(eax) || \
2732ee88d2b9Skchow 			    DH_E6(eax) || JH_E6(eax))
27337c478bd9Sstevel@tonic-gate 
2734512cf780Skchow #define	DR_AX(eax)	(eax == 0x100f00 || eax == 0x100f01 || eax == 0x100f02)
2735512cf780Skchow #define	DR_B0(eax)	(eax == 0x100f20)
2736512cf780Skchow #define	DR_B1(eax)	(eax == 0x100f21)
2737512cf780Skchow #define	DR_BA(eax)	(eax == 0x100f2a)
2738512cf780Skchow #define	DR_B2(eax)	(eax == 0x100f22)
2739512cf780Skchow #define	DR_B3(eax)	(eax == 0x100f23)
2740512cf780Skchow #define	RB_C0(eax)	(eax == 0x100f40)
2741512cf780Skchow 
27427c478bd9Sstevel@tonic-gate 	switch (erratum) {
27437c478bd9Sstevel@tonic-gate 	case 1:
2744875b116eSkchow 		return (cpi->cpi_family < 0x10);
27457c478bd9Sstevel@tonic-gate 	case 51:	/* what does the asterisk mean? */
27467c478bd9Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax));
27477c478bd9Sstevel@tonic-gate 	case 52:
27487c478bd9Sstevel@tonic-gate 		return (B(eax));
27497c478bd9Sstevel@tonic-gate 	case 57:
2750512cf780Skchow 		return (cpi->cpi_family <= 0x11);
27517c478bd9Sstevel@tonic-gate 	case 58:
27527c478bd9Sstevel@tonic-gate 		return (B(eax));
27537c478bd9Sstevel@tonic-gate 	case 60:
2754512cf780Skchow 		return (cpi->cpi_family <= 0x11);
27557c478bd9Sstevel@tonic-gate 	case 61:
27567c478bd9Sstevel@tonic-gate 	case 62:
27577c478bd9Sstevel@tonic-gate 	case 63:
27587c478bd9Sstevel@tonic-gate 	case 64:
27597c478bd9Sstevel@tonic-gate 	case 65:
27607c478bd9Sstevel@tonic-gate 	case 66:
27617c478bd9Sstevel@tonic-gate 	case 68:
27627c478bd9Sstevel@tonic-gate 	case 69:
27637c478bd9Sstevel@tonic-gate 	case 70:
27647c478bd9Sstevel@tonic-gate 	case 71:
27657c478bd9Sstevel@tonic-gate 		return (B(eax));
27667c478bd9Sstevel@tonic-gate 	case 72:
27677c478bd9Sstevel@tonic-gate 		return (SH_B0(eax));
27687c478bd9Sstevel@tonic-gate 	case 74:
27697c478bd9Sstevel@tonic-gate 		return (B(eax));
27707c478bd9Sstevel@tonic-gate 	case 75:
2771875b116eSkchow 		return (cpi->cpi_family < 0x10);
27727c478bd9Sstevel@tonic-gate 	case 76:
27737c478bd9Sstevel@tonic-gate 		return (B(eax));
27747c478bd9Sstevel@tonic-gate 	case 77:
2775512cf780Skchow 		return (cpi->cpi_family <= 0x11);
27767c478bd9Sstevel@tonic-gate 	case 78:
27777c478bd9Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax));
27787c478bd9Sstevel@tonic-gate 	case 79:
27797c478bd9Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax) || EX(eax));
27807c478bd9Sstevel@tonic-gate 	case 80:
27817c478bd9Sstevel@tonic-gate 	case 81:
27827c478bd9Sstevel@tonic-gate 	case 82:
27837c478bd9Sstevel@tonic-gate 		return (B(eax));
27847c478bd9Sstevel@tonic-gate 	case 83:
27857c478bd9Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax));
27867c478bd9Sstevel@tonic-gate 	case 85:
2787875b116eSkchow 		return (cpi->cpi_family < 0x10);
27887c478bd9Sstevel@tonic-gate 	case 86:
27897c478bd9Sstevel@tonic-gate 		return (SH_C0(eax) || CG(eax));
27907c478bd9Sstevel@tonic-gate 	case 88:
27917c478bd9Sstevel@tonic-gate #if !defined(__amd64)
27927c478bd9Sstevel@tonic-gate 		return (0);
27937c478bd9Sstevel@tonic-gate #else
27947c478bd9Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax));
27957c478bd9Sstevel@tonic-gate #endif
27967c478bd9Sstevel@tonic-gate 	case 89:
2797875b116eSkchow 		return (cpi->cpi_family < 0x10);
27987c478bd9Sstevel@tonic-gate 	case 90:
27997c478bd9Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax));
28007c478bd9Sstevel@tonic-gate 	case 91:
28017c478bd9Sstevel@tonic-gate 	case 92:
28027c478bd9Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax));
28037c478bd9Sstevel@tonic-gate 	case 93:
28047c478bd9Sstevel@tonic-gate 		return (SH_C0(eax));
28057c478bd9Sstevel@tonic-gate 	case 94:
28067c478bd9Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax));
28077c478bd9Sstevel@tonic-gate 	case 95:
28087c478bd9Sstevel@tonic-gate #if !defined(__amd64)
28097c478bd9Sstevel@tonic-gate 		return (0);
28107c478bd9Sstevel@tonic-gate #else
28117c478bd9Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax));
28127c478bd9Sstevel@tonic-gate #endif
28137c478bd9Sstevel@tonic-gate 	case 96:
28147c478bd9Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax));
28157c478bd9Sstevel@tonic-gate 	case 97:
28167c478bd9Sstevel@tonic-gate 	case 98:
28177c478bd9Sstevel@tonic-gate 		return (SH_C0(eax) || CG(eax));
28187c478bd9Sstevel@tonic-gate 	case 99:
28197c478bd9Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax));
28207c478bd9Sstevel@tonic-gate 	case 100:
28217c478bd9Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax));
28227c478bd9Sstevel@tonic-gate 	case 101:
28237c478bd9Sstevel@tonic-gate 	case 103:
28247c478bd9Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax));
28257c478bd9Sstevel@tonic-gate 	case 104:
28267c478bd9Sstevel@tonic-gate 		return (SH_C0(eax) || CG(eax) || D0(eax));
28277c478bd9Sstevel@tonic-gate 	case 105:
28287c478bd9Sstevel@tonic-gate 	case 106:
28297c478bd9Sstevel@tonic-gate 	case 107:
28307c478bd9Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax));
28317c478bd9Sstevel@tonic-gate 	case 108:
28327c478bd9Sstevel@tonic-gate 		return (DH_CG(eax));
28337c478bd9Sstevel@tonic-gate 	case 109:
28347c478bd9Sstevel@tonic-gate 		return (SH_C0(eax) || CG(eax) || D0(eax));
28357c478bd9Sstevel@tonic-gate 	case 110:
28367c478bd9Sstevel@tonic-gate 		return (D0(eax) || EX(eax));
28377c478bd9Sstevel@tonic-gate 	case 111:
28387c478bd9Sstevel@tonic-gate 		return (CG(eax));
28397c478bd9Sstevel@tonic-gate 	case 112:
28407c478bd9Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax) || EX(eax));
28417c478bd9Sstevel@tonic-gate 	case 113:
28427c478bd9Sstevel@tonic-gate 		return (eax == 0x20fc0);
28437c478bd9Sstevel@tonic-gate 	case 114:
28447c478bd9Sstevel@tonic-gate 		return (SH_E0(eax) || JH_E1(eax) || DH_E3(eax));
28457c478bd9Sstevel@tonic-gate 	case 115:
28467c478bd9Sstevel@tonic-gate 		return (SH_E0(eax) || JH_E1(eax));
28477c478bd9Sstevel@tonic-gate 	case 116:
28487c478bd9Sstevel@tonic-gate 		return (SH_E0(eax) || JH_E1(eax) || DH_E3(eax));
28497c478bd9Sstevel@tonic-gate 	case 117:
28507c478bd9Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax));
28517c478bd9Sstevel@tonic-gate 	case 118:
28527c478bd9Sstevel@tonic-gate 		return (SH_E0(eax) || JH_E1(eax) || SH_E4(eax) || BH_E4(eax) ||
28537c478bd9Sstevel@tonic-gate 		    JH_E6(eax));
28547c478bd9Sstevel@tonic-gate 	case 121:
28557c478bd9Sstevel@tonic-gate 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax) || EX(eax));
28567c478bd9Sstevel@tonic-gate 	case 122:
2857512cf780Skchow 		return (cpi->cpi_family < 0x10 || cpi->cpi_family == 0x11);
28587c478bd9Sstevel@tonic-gate 	case 123:
28597c478bd9Sstevel@tonic-gate 		return (JH_E1(eax) || BH_E4(eax) || JH_E6(eax));
28602201b277Skucharsk 	case 131:
2861875b116eSkchow 		return (cpi->cpi_family < 0x10);
2862ef50d8c0Sesaxe 	case 6336786:
2863ef50d8c0Sesaxe 		/*
2864ef50d8c0Sesaxe 		 * Test for AdvPowerMgmtInfo.TscPStateInvariant
2865875b116eSkchow 		 * if this is a K8 family or newer processor
2866ef50d8c0Sesaxe 		 */
2867ef50d8c0Sesaxe 		if (CPI_FAMILY(cpi) == 0xf) {
28688949bcd6Sandrei 			struct cpuid_regs regs;
28698949bcd6Sandrei 			regs.cp_eax = 0x80000007;
28708949bcd6Sandrei 			(void) __cpuid_insn(&regs);
28718949bcd6Sandrei 			return (!(regs.cp_edx & 0x100));
2872ef50d8c0Sesaxe 		}
2873ef50d8c0Sesaxe 		return (0);
2874ee88d2b9Skchow 	case 6323525:
2875ee88d2b9Skchow 		return (((((eax >> 12) & 0xff00) + (eax & 0xf00)) |
2876ee88d2b9Skchow 		    (((eax >> 4) & 0xf) | ((eax >> 12) & 0xf0))) < 0xf40);
2877ee88d2b9Skchow 
2878512cf780Skchow 	case 6671130:
2879512cf780Skchow 		/*
2880512cf780Skchow 		 * check for processors (pre-Shanghai) that do not provide
2881512cf780Skchow 		 * optimal management of 1gb ptes in its tlb.
2882512cf780Skchow 		 */
2883512cf780Skchow 		return (cpi->cpi_family == 0x10 && cpi->cpi_model < 4);
2884512cf780Skchow 
2885512cf780Skchow 	case 298:
2886512cf780Skchow 		return (DR_AX(eax) || DR_B0(eax) || DR_B1(eax) || DR_BA(eax) ||
2887512cf780Skchow 		    DR_B2(eax) || RB_C0(eax));
2888512cf780Skchow 
2889512cf780Skchow 	default:
2890512cf780Skchow 		return (-1);
2891512cf780Skchow 
2892512cf780Skchow 	}
2893512cf780Skchow }
2894512cf780Skchow 
2895512cf780Skchow /*
2896512cf780Skchow  * Determine if specified erratum is present via OSVW (OS Visible Workaround).
2897512cf780Skchow  * Return 1 if erratum is present, 0 if not present and -1 if indeterminate.
2898512cf780Skchow  */
2899512cf780Skchow int
2900512cf780Skchow osvw_opteron_erratum(cpu_t *cpu, uint_t erratum)
2901512cf780Skchow {
2902512cf780Skchow 	struct cpuid_info	*cpi;
2903512cf780Skchow 	uint_t			osvwid;
2904512cf780Skchow 	static int		osvwfeature = -1;
2905512cf780Skchow 	uint64_t		osvwlength;
2906512cf780Skchow 
2907512cf780Skchow 
2908512cf780Skchow 	cpi = cpu->cpu_m.mcpu_cpi;
2909512cf780Skchow 
2910512cf780Skchow 	/* confirm OSVW supported */
2911512cf780Skchow 	if (osvwfeature == -1) {
2912512cf780Skchow 		osvwfeature = cpi->cpi_extd[1].cp_ecx & CPUID_AMD_ECX_OSVW;
2913512cf780Skchow 	} else {
2914512cf780Skchow 		/* assert that osvw feature setting is consistent on all cpus */
2915512cf780Skchow 		ASSERT(osvwfeature ==
2916512cf780Skchow 		    (cpi->cpi_extd[1].cp_ecx & CPUID_AMD_ECX_OSVW));
2917512cf780Skchow 	}
2918512cf780Skchow 	if (!osvwfeature)
2919512cf780Skchow 		return (-1);
2920512cf780Skchow 
2921512cf780Skchow 	osvwlength = rdmsr(MSR_AMD_OSVW_ID_LEN) & OSVW_ID_LEN_MASK;
2922512cf780Skchow 
2923512cf780Skchow 	switch (erratum) {
2924512cf780Skchow 	case 298:	/* osvwid is 0 */
2925512cf780Skchow 		osvwid = 0;
2926512cf780Skchow 		if (osvwlength <= (uint64_t)osvwid) {
2927512cf780Skchow 			/* osvwid 0 is unknown */
2928512cf780Skchow 			return (-1);
2929512cf780Skchow 		}
2930512cf780Skchow 
2931512cf780Skchow 		/*
2932512cf780Skchow 		 * Check the OSVW STATUS MSR to determine the state
2933512cf780Skchow 		 * of the erratum where:
2934512cf780Skchow 		 *   0 - fixed by HW
2935512cf780Skchow 		 *   1 - BIOS has applied the workaround when BIOS
2936512cf780Skchow 		 *   workaround is available. (Or for other errata,
2937512cf780Skchow 		 *   OS workaround is required.)
2938512cf780Skchow 		 * For a value of 1, caller will confirm that the
2939512cf780Skchow 		 * erratum 298 workaround has indeed been applied by BIOS.
2940512cf780Skchow 		 *
2941512cf780Skchow 		 * A 1 may be set in cpus that have a HW fix
2942512cf780Skchow 		 * in a mixed cpu system. Regarding erratum 298:
2943512cf780Skchow 		 *   In a multiprocessor platform, the workaround above
2944512cf780Skchow 		 *   should be applied to all processors regardless of
2945512cf780Skchow 		 *   silicon revision when an affected processor is
2946512cf780Skchow 		 *   present.
2947512cf780Skchow 		 */
2948512cf780Skchow 
2949512cf780Skchow 		return (rdmsr(MSR_AMD_OSVW_STATUS +
2950512cf780Skchow 		    (osvwid / OSVW_ID_CNT_PER_MSR)) &
2951512cf780Skchow 		    (1ULL << (osvwid % OSVW_ID_CNT_PER_MSR)));
2952512cf780Skchow 
29537c478bd9Sstevel@tonic-gate 	default:
29547c478bd9Sstevel@tonic-gate 		return (-1);
29557c478bd9Sstevel@tonic-gate 	}
29567c478bd9Sstevel@tonic-gate }
29577c478bd9Sstevel@tonic-gate 
29587c478bd9Sstevel@tonic-gate static const char assoc_str[] = "associativity";
29597c478bd9Sstevel@tonic-gate static const char line_str[] = "line-size";
29607c478bd9Sstevel@tonic-gate static const char size_str[] = "size";
29617c478bd9Sstevel@tonic-gate 
29627c478bd9Sstevel@tonic-gate static void
29637c478bd9Sstevel@tonic-gate add_cache_prop(dev_info_t *devi, const char *label, const char *type,
29647c478bd9Sstevel@tonic-gate     uint32_t val)
29657c478bd9Sstevel@tonic-gate {
29667c478bd9Sstevel@tonic-gate 	char buf[128];
29677c478bd9Sstevel@tonic-gate 
29687c478bd9Sstevel@tonic-gate 	/*
29697c478bd9Sstevel@tonic-gate 	 * ndi_prop_update_int() is used because it is desirable for
29707c478bd9Sstevel@tonic-gate 	 * DDI_PROP_HW_DEF and DDI_PROP_DONTSLEEP to be set.
29717c478bd9Sstevel@tonic-gate 	 */
29727c478bd9Sstevel@tonic-gate 	if (snprintf(buf, sizeof (buf), "%s-%s", label, type) < sizeof (buf))
29737c478bd9Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, devi, buf, val);
29747c478bd9Sstevel@tonic-gate }
29757c478bd9Sstevel@tonic-gate 
29767c478bd9Sstevel@tonic-gate /*
29777c478bd9Sstevel@tonic-gate  * Intel-style cache/tlb description
29787c478bd9Sstevel@tonic-gate  *
29797c478bd9Sstevel@tonic-gate  * Standard cpuid level 2 gives a randomly ordered
29807c478bd9Sstevel@tonic-gate  * selection of tags that index into a table that describes
29817c478bd9Sstevel@tonic-gate  * cache and tlb properties.
29827c478bd9Sstevel@tonic-gate  */
29837c478bd9Sstevel@tonic-gate 
29847c478bd9Sstevel@tonic-gate static const char l1_icache_str[] = "l1-icache";
29857c478bd9Sstevel@tonic-gate static const char l1_dcache_str[] = "l1-dcache";
29867c478bd9Sstevel@tonic-gate static const char l2_cache_str[] = "l2-cache";
2987ae115bc7Smrj static const char l3_cache_str[] = "l3-cache";
29887c478bd9Sstevel@tonic-gate static const char itlb4k_str[] = "itlb-4K";
29897c478bd9Sstevel@tonic-gate static const char dtlb4k_str[] = "dtlb-4K";
2990824e4fecSvd224797 static const char itlb2M_str[] = "itlb-2M";
29917c478bd9Sstevel@tonic-gate static const char itlb4M_str[] = "itlb-4M";
29927c478bd9Sstevel@tonic-gate static const char dtlb4M_str[] = "dtlb-4M";
299325dfb062Sksadhukh static const char dtlb24_str[] = "dtlb0-2M-4M";
29947c478bd9Sstevel@tonic-gate static const char itlb424_str[] = "itlb-4K-2M-4M";
299525dfb062Sksadhukh static const char itlb24_str[] = "itlb-2M-4M";
29967c478bd9Sstevel@tonic-gate static const char dtlb44_str[] = "dtlb-4K-4M";
29977c478bd9Sstevel@tonic-gate static const char sl1_dcache_str[] = "sectored-l1-dcache";
29987c478bd9Sstevel@tonic-gate static const char sl2_cache_str[] = "sectored-l2-cache";
29997c478bd9Sstevel@tonic-gate static const char itrace_str[] = "itrace-cache";
30007c478bd9Sstevel@tonic-gate static const char sl3_cache_str[] = "sectored-l3-cache";
300125dfb062Sksadhukh static const char sh_l2_tlb4k_str[] = "shared-l2-tlb-4k";
30027c478bd9Sstevel@tonic-gate 
30037c478bd9Sstevel@tonic-gate static const struct cachetab {
30047c478bd9Sstevel@tonic-gate 	uint8_t 	ct_code;
30057c478bd9Sstevel@tonic-gate 	uint8_t		ct_assoc;
30067c478bd9Sstevel@tonic-gate 	uint16_t 	ct_line_size;
30077c478bd9Sstevel@tonic-gate 	size_t		ct_size;
30087c478bd9Sstevel@tonic-gate 	const char	*ct_label;
30097c478bd9Sstevel@tonic-gate } intel_ctab[] = {
3010824e4fecSvd224797 	/*
3011824e4fecSvd224797 	 * maintain descending order!
3012824e4fecSvd224797 	 *
3013824e4fecSvd224797 	 * Codes ignored - Reason
3014824e4fecSvd224797 	 * ----------------------
3015824e4fecSvd224797 	 * 40H - intel_cpuid_4_cache_info() disambiguates l2/l3 cache
3016824e4fecSvd224797 	 * f0H/f1H - Currently we do not interpret prefetch size by design
3017824e4fecSvd224797 	 */
301825dfb062Sksadhukh 	{ 0xe4, 16, 64, 8*1024*1024, l3_cache_str},
301925dfb062Sksadhukh 	{ 0xe3, 16, 64, 4*1024*1024, l3_cache_str},
302025dfb062Sksadhukh 	{ 0xe2, 16, 64, 2*1024*1024, l3_cache_str},
302125dfb062Sksadhukh 	{ 0xde, 12, 64, 6*1024*1024, l3_cache_str},
302225dfb062Sksadhukh 	{ 0xdd, 12, 64, 3*1024*1024, l3_cache_str},
302325dfb062Sksadhukh 	{ 0xdc, 12, 64, ((1*1024*1024)+(512*1024)), l3_cache_str},
302425dfb062Sksadhukh 	{ 0xd8, 8, 64, 4*1024*1024, l3_cache_str},
302525dfb062Sksadhukh 	{ 0xd7, 8, 64, 2*1024*1024, l3_cache_str},
302625dfb062Sksadhukh 	{ 0xd6, 8, 64, 1*1024*1024, l3_cache_str},
302725dfb062Sksadhukh 	{ 0xd2, 4, 64, 2*1024*1024, l3_cache_str},
302825dfb062Sksadhukh 	{ 0xd1, 4, 64, 1*1024*1024, l3_cache_str},
302925dfb062Sksadhukh 	{ 0xd0, 4, 64, 512*1024, l3_cache_str},
303025dfb062Sksadhukh 	{ 0xca, 4, 0, 512, sh_l2_tlb4k_str},
3031824e4fecSvd224797 	{ 0xc0, 4, 0, 8, dtlb44_str },
3032824e4fecSvd224797 	{ 0xba, 4, 0, 64, dtlb4k_str },
3033ae115bc7Smrj 	{ 0xb4, 4, 0, 256, dtlb4k_str },
30347c478bd9Sstevel@tonic-gate 	{ 0xb3, 4, 0, 128, dtlb4k_str },
303525dfb062Sksadhukh 	{ 0xb2, 4, 0, 64, itlb4k_str },
30367c478bd9Sstevel@tonic-gate 	{ 0xb0, 4, 0, 128, itlb4k_str },
30377c478bd9Sstevel@tonic-gate 	{ 0x87, 8, 64, 1024*1024, l2_cache_str},
30387c478bd9Sstevel@tonic-gate 	{ 0x86, 4, 64, 512*1024, l2_cache_str},
30397c478bd9Sstevel@tonic-gate 	{ 0x85, 8, 32, 2*1024*1024, l2_cache_str},
30407c478bd9Sstevel@tonic-gate 	{ 0x84, 8, 32, 1024*1024, l2_cache_str},
30417c478bd9Sstevel@tonic-gate 	{ 0x83, 8, 32, 512*1024, l2_cache_str},
30427c478bd9Sstevel@tonic-gate 	{ 0x82, 8, 32, 256*1024, l2_cache_str},
3043824e4fecSvd224797 	{ 0x80, 8, 64, 512*1024, l2_cache_str},
30447c478bd9Sstevel@tonic-gate 	{ 0x7f, 2, 64, 512*1024, l2_cache_str},
30457c478bd9Sstevel@tonic-gate 	{ 0x7d, 8, 64, 2*1024*1024, sl2_cache_str},
30467c478bd9Sstevel@tonic-gate 	{ 0x7c, 8, 64, 1024*1024, sl2_cache_str},
30477c478bd9Sstevel@tonic-gate 	{ 0x7b, 8, 64, 512*1024, sl2_cache_str},
30487c478bd9Sstevel@tonic-gate 	{ 0x7a, 8, 64, 256*1024, sl2_cache_str},
30497c478bd9Sstevel@tonic-gate 	{ 0x79, 8, 64, 128*1024, sl2_cache_str},
30507c478bd9Sstevel@tonic-gate 	{ 0x78, 8, 64, 1024*1024, l2_cache_str},
3051ae115bc7Smrj 	{ 0x73, 8, 0, 64*1024, itrace_str},
30527c478bd9Sstevel@tonic-gate 	{ 0x72, 8, 0, 32*1024, itrace_str},
30537c478bd9Sstevel@tonic-gate 	{ 0x71, 8, 0, 16*1024, itrace_str},
30547c478bd9Sstevel@tonic-gate 	{ 0x70, 8, 0, 12*1024, itrace_str},
30557c478bd9Sstevel@tonic-gate 	{ 0x68, 4, 64, 32*1024, sl1_dcache_str},
30567c478bd9Sstevel@tonic-gate 	{ 0x67, 4, 64, 16*1024, sl1_dcache_str},
30577c478bd9Sstevel@tonic-gate 	{ 0x66, 4, 64, 8*1024, sl1_dcache_str},
30587c478bd9Sstevel@tonic-gate 	{ 0x60, 8, 64, 16*1024, sl1_dcache_str},
30597c478bd9Sstevel@tonic-gate 	{ 0x5d, 0, 0, 256, dtlb44_str},
30607c478bd9Sstevel@tonic-gate 	{ 0x5c, 0, 0, 128, dtlb44_str},
30617c478bd9Sstevel@tonic-gate 	{ 0x5b, 0, 0, 64, dtlb44_str},
306225dfb062Sksadhukh 	{ 0x5a, 4, 0, 32, dtlb24_str},
3063824e4fecSvd224797 	{ 0x59, 0, 0, 16, dtlb4k_str},
3064824e4fecSvd224797 	{ 0x57, 4, 0, 16, dtlb4k_str},
3065824e4fecSvd224797 	{ 0x56, 4, 0, 16, dtlb4M_str},
306625dfb062Sksadhukh 	{ 0x55, 0, 0, 7, itlb24_str},
30677c478bd9Sstevel@tonic-gate 	{ 0x52, 0, 0, 256, itlb424_str},
30687c478bd9Sstevel@tonic-gate 	{ 0x51, 0, 0, 128, itlb424_str},
30697c478bd9Sstevel@tonic-gate 	{ 0x50, 0, 0, 64, itlb424_str},
3070824e4fecSvd224797 	{ 0x4f, 0, 0, 32, itlb4k_str},
3071824e4fecSvd224797 	{ 0x4e, 24, 64, 6*1024*1024, l2_cache_str},
3072ae115bc7Smrj 	{ 0x4d, 16, 64, 16*1024*1024, l3_cache_str},
3073ae115bc7Smrj 	{ 0x4c, 12, 64, 12*1024*1024, l3_cache_str},
3074ae115bc7Smrj 	{ 0x4b, 16, 64, 8*1024*1024, l3_cache_str},
3075ae115bc7Smrj 	{ 0x4a, 12, 64, 6*1024*1024, l3_cache_str},
3076ae115bc7Smrj 	{ 0x49, 16, 64, 4*1024*1024, l3_cache_str},
3077824e4fecSvd224797 	{ 0x48, 12, 64, 3*1024*1024, l2_cache_str},
3078ae115bc7Smrj 	{ 0x47, 8, 64, 8*1024*1024, l3_cache_str},
3079ae115bc7Smrj 	{ 0x46, 4, 64, 4*1024*1024, l3_cache_str},
30807c478bd9Sstevel@tonic-gate 	{ 0x45, 4, 32, 2*1024*1024, l2_cache_str},
30817c478bd9Sstevel@tonic-gate 	{ 0x44, 4, 32, 1024*1024, l2_cache_str},
30827c478bd9Sstevel@tonic-gate 	{ 0x43, 4, 32, 512*1024, l2_cache_str},
30837c478bd9Sstevel@tonic-gate 	{ 0x42, 4, 32, 256*1024, l2_cache_str},
30847c478bd9Sstevel@tonic-gate 	{ 0x41, 4, 32, 128*1024, l2_cache_str},
3085ae115bc7Smrj 	{ 0x3e, 4, 64, 512*1024, sl2_cache_str},
3086ae115bc7Smrj 	{ 0x3d, 6, 64, 384*1024, sl2_cache_str},
30877c478bd9Sstevel@tonic-gate 	{ 0x3c, 4, 64, 256*1024, sl2_cache_str},
30887c478bd9Sstevel@tonic-gate 	{ 0x3b, 2, 64, 128*1024, sl2_cache_str},
3089ae115bc7Smrj 	{ 0x3a, 6, 64, 192*1024, sl2_cache_str},
30907c478bd9Sstevel@tonic-gate 	{ 0x39, 4, 64, 128*1024, sl2_cache_str},
30917c478bd9Sstevel@tonic-gate 	{ 0x30, 8, 64, 32*1024, l1_icache_str},
30927c478bd9Sstevel@tonic-gate 	{ 0x2c, 8, 64, 32*1024, l1_dcache_str},
30937c478bd9Sstevel@tonic-gate 	{ 0x29, 8, 64, 4096*1024, sl3_cache_str},
30947c478bd9Sstevel@tonic-gate 	{ 0x25, 8, 64, 2048*1024, sl3_cache_str},
30957c478bd9Sstevel@tonic-gate 	{ 0x23, 8, 64, 1024*1024, sl3_cache_str},
30967c478bd9Sstevel@tonic-gate 	{ 0x22, 4, 64, 512*1024, sl3_cache_str},
3097824e4fecSvd224797 	{ 0x0e, 6, 64, 24*1024, l1_dcache_str},
309825dfb062Sksadhukh 	{ 0x0d, 4, 32, 16*1024, l1_dcache_str},
30997c478bd9Sstevel@tonic-gate 	{ 0x0c, 4, 32, 16*1024, l1_dcache_str},
3100ae115bc7Smrj 	{ 0x0b, 4, 0, 4, itlb4M_str},
31017c478bd9Sstevel@tonic-gate 	{ 0x0a, 2, 32, 8*1024, l1_dcache_str},
31027c478bd9Sstevel@tonic-gate 	{ 0x08, 4, 32, 16*1024, l1_icache_str},
31037c478bd9Sstevel@tonic-gate 	{ 0x06, 4, 32, 8*1024, l1_icache_str},
3104824e4fecSvd224797 	{ 0x05, 4, 0, 32, dtlb4M_str},
31057c478bd9Sstevel@tonic-gate 	{ 0x04, 4, 0, 8, dtlb4M_str},
31067c478bd9Sstevel@tonic-gate 	{ 0x03, 4, 0, 64, dtlb4k_str},
31077c478bd9Sstevel@tonic-gate 	{ 0x02, 4, 0, 2, itlb4M_str},
31087c478bd9Sstevel@tonic-gate 	{ 0x01, 4, 0, 32, itlb4k_str},
31097c478bd9Sstevel@tonic-gate 	{ 0 }
31107c478bd9Sstevel@tonic-gate };
31117c478bd9Sstevel@tonic-gate 
31127c478bd9Sstevel@tonic-gate static const struct cachetab cyrix_ctab[] = {
31137c478bd9Sstevel@tonic-gate 	{ 0x70, 4, 0, 32, "tlb-4K" },
31147c478bd9Sstevel@tonic-gate 	{ 0x80, 4, 16, 16*1024, "l1-cache" },
31157c478bd9Sstevel@tonic-gate 	{ 0 }
31167c478bd9Sstevel@tonic-gate };
31177c478bd9Sstevel@tonic-gate 
31187c478bd9Sstevel@tonic-gate /*
31197c478bd9Sstevel@tonic-gate  * Search a cache table for a matching entry
31207c478bd9Sstevel@tonic-gate  */
31217c478bd9Sstevel@tonic-gate static const struct cachetab *
31227c478bd9Sstevel@tonic-gate find_cacheent(const struct cachetab *ct, uint_t code)
31237c478bd9Sstevel@tonic-gate {
31247c478bd9Sstevel@tonic-gate 	if (code != 0) {
31257c478bd9Sstevel@tonic-gate 		for (; ct->ct_code != 0; ct++)
31267c478bd9Sstevel@tonic-gate 			if (ct->ct_code <= code)
31277c478bd9Sstevel@tonic-gate 				break;
31287c478bd9Sstevel@tonic-gate 		if (ct->ct_code == code)
31297c478bd9Sstevel@tonic-gate 			return (ct);
31307c478bd9Sstevel@tonic-gate 	}
31317c478bd9Sstevel@tonic-gate 	return (NULL);
31327c478bd9Sstevel@tonic-gate }
31337c478bd9Sstevel@tonic-gate 
31347c478bd9Sstevel@tonic-gate /*
31357dee861bSksadhukh  * Populate cachetab entry with L2 or L3 cache-information using
31367dee861bSksadhukh  * cpuid function 4. This function is called from intel_walk_cacheinfo()
31377dee861bSksadhukh  * when descriptor 0x49 is encountered. It returns 0 if no such cache
31387dee861bSksadhukh  * information is found.
31397dee861bSksadhukh  */
31407dee861bSksadhukh static int
31417dee861bSksadhukh intel_cpuid_4_cache_info(struct cachetab *ct, struct cpuid_info *cpi)
31427dee861bSksadhukh {
31437dee861bSksadhukh 	uint32_t level, i;
31447dee861bSksadhukh 	int ret = 0;
31457dee861bSksadhukh 
31467dee861bSksadhukh 	for (i = 0; i < cpi->cpi_std_4_size; i++) {
31477dee861bSksadhukh 		level = CPI_CACHE_LVL(cpi->cpi_std_4[i]);
31487dee861bSksadhukh 
31497dee861bSksadhukh 		if (level == 2 || level == 3) {
31507dee861bSksadhukh 			ct->ct_assoc = CPI_CACHE_WAYS(cpi->cpi_std_4[i]) + 1;
31517dee861bSksadhukh 			ct->ct_line_size =
31527dee861bSksadhukh 			    CPI_CACHE_COH_LN_SZ(cpi->cpi_std_4[i]) + 1;
31537dee861bSksadhukh 			ct->ct_size = ct->ct_assoc *
31547dee861bSksadhukh 			    (CPI_CACHE_PARTS(cpi->cpi_std_4[i]) + 1) *
31557dee861bSksadhukh 			    ct->ct_line_size *
31567dee861bSksadhukh 			    (cpi->cpi_std_4[i]->cp_ecx + 1);
31577dee861bSksadhukh 
31587dee861bSksadhukh 			if (level == 2) {
31597dee861bSksadhukh 				ct->ct_label = l2_cache_str;
31607dee861bSksadhukh 			} else if (level == 3) {
31617dee861bSksadhukh 				ct->ct_label = l3_cache_str;
31627dee861bSksadhukh 			}
31637dee861bSksadhukh 			ret = 1;
31647dee861bSksadhukh 		}
31657dee861bSksadhukh 	}
31667dee861bSksadhukh 
31677dee861bSksadhukh 	return (ret);
31687dee861bSksadhukh }
31697dee861bSksadhukh 
31707dee861bSksadhukh /*
31717c478bd9Sstevel@tonic-gate  * Walk the cacheinfo descriptor, applying 'func' to every valid element
31727c478bd9Sstevel@tonic-gate  * The walk is terminated if the walker returns non-zero.
31737c478bd9Sstevel@tonic-gate  */
31747c478bd9Sstevel@tonic-gate static void
31757c478bd9Sstevel@tonic-gate intel_walk_cacheinfo(struct cpuid_info *cpi,
31767c478bd9Sstevel@tonic-gate     void *arg, int (*func)(void *, const struct cachetab *))
31777c478bd9Sstevel@tonic-gate {
31787c478bd9Sstevel@tonic-gate 	const struct cachetab *ct;
3179824e4fecSvd224797 	struct cachetab des_49_ct, des_b1_ct;
31807c478bd9Sstevel@tonic-gate 	uint8_t *dp;
31817c478bd9Sstevel@tonic-gate 	int i;
31827c478bd9Sstevel@tonic-gate 
31837c478bd9Sstevel@tonic-gate 	if ((dp = cpi->cpi_cacheinfo) == NULL)
31847c478bd9Sstevel@tonic-gate 		return;
3185f1d742a9Sksadhukh 	for (i = 0; i < cpi->cpi_ncache; i++, dp++) {
3186f1d742a9Sksadhukh 		/*
3187f1d742a9Sksadhukh 		 * For overloaded descriptor 0x49 we use cpuid function 4
31887dee861bSksadhukh 		 * if supported by the current processor, to create
3189f1d742a9Sksadhukh 		 * cache information.
3190824e4fecSvd224797 		 * For overloaded descriptor 0xb1 we use X86_PAE flag
3191824e4fecSvd224797 		 * to disambiguate the cache information.
3192f1d742a9Sksadhukh 		 */
31937dee861bSksadhukh 		if (*dp == 0x49 && cpi->cpi_maxeax >= 0x4 &&
31947dee861bSksadhukh 		    intel_cpuid_4_cache_info(&des_49_ct, cpi) == 1) {
31957dee861bSksadhukh 				ct = &des_49_ct;
3196824e4fecSvd224797 		} else if (*dp == 0xb1) {
3197824e4fecSvd224797 			des_b1_ct.ct_code = 0xb1;
3198824e4fecSvd224797 			des_b1_ct.ct_assoc = 4;
3199824e4fecSvd224797 			des_b1_ct.ct_line_size = 0;
3200824e4fecSvd224797 			if (x86_feature & X86_PAE) {
3201824e4fecSvd224797 				des_b1_ct.ct_size = 8;
3202824e4fecSvd224797 				des_b1_ct.ct_label = itlb2M_str;
3203824e4fecSvd224797 			} else {
3204824e4fecSvd224797 				des_b1_ct.ct_size = 4;
3205824e4fecSvd224797 				des_b1_ct.ct_label = itlb4M_str;
3206824e4fecSvd224797 			}
3207824e4fecSvd224797 			ct = &des_b1_ct;
32087dee861bSksadhukh 		} else {
32097dee861bSksadhukh 			if ((ct = find_cacheent(intel_ctab, *dp)) == NULL) {
3210f1d742a9Sksadhukh 				continue;
3211f1d742a9Sksadhukh 			}
32127dee861bSksadhukh 		}
3213f1d742a9Sksadhukh 
32147dee861bSksadhukh 		if (func(arg, ct) != 0) {
32157c478bd9Sstevel@tonic-gate 			break;
32167c478bd9Sstevel@tonic-gate 		}
32177c478bd9Sstevel@tonic-gate 	}
3218f1d742a9Sksadhukh }
32197c478bd9Sstevel@tonic-gate 
32207c478bd9Sstevel@tonic-gate /*
32217c478bd9Sstevel@tonic-gate  * (Like the Intel one, except for Cyrix CPUs)
32227c478bd9Sstevel@tonic-gate  */
32237c478bd9Sstevel@tonic-gate static void
32247c478bd9Sstevel@tonic-gate cyrix_walk_cacheinfo(struct cpuid_info *cpi,
32257c478bd9Sstevel@tonic-gate     void *arg, int (*func)(void *, const struct cachetab *))
32267c478bd9Sstevel@tonic-gate {
32277c478bd9Sstevel@tonic-gate 	const struct cachetab *ct;
32287c478bd9Sstevel@tonic-gate 	uint8_t *dp;
32297c478bd9Sstevel@tonic-gate 	int i;
32307c478bd9Sstevel@tonic-gate 
32317c478bd9Sstevel@tonic-gate 	if ((dp = cpi->cpi_cacheinfo) == NULL)
32327c478bd9Sstevel@tonic-gate 		return;
32337c478bd9Sstevel@tonic-gate 	for (i = 0; i < cpi->cpi_ncache; i++, dp++) {
32347c478bd9Sstevel@tonic-gate 		/*
32357c478bd9Sstevel@tonic-gate 		 * Search Cyrix-specific descriptor table first ..
32367c478bd9Sstevel@tonic-gate 		 */
32377c478bd9Sstevel@tonic-gate 		if ((ct = find_cacheent(cyrix_ctab, *dp)) != NULL) {
32387c478bd9Sstevel@tonic-gate 			if (func(arg, ct) != 0)
32397c478bd9Sstevel@tonic-gate 				break;
32407c478bd9Sstevel@tonic-gate 			continue;
32417c478bd9Sstevel@tonic-gate 		}
32427c478bd9Sstevel@tonic-gate 		/*
32437c478bd9Sstevel@tonic-gate 		 * .. else fall back to the Intel one
32447c478bd9Sstevel@tonic-gate 		 */
32457c478bd9Sstevel@tonic-gate 		if ((ct = find_cacheent(intel_ctab, *dp)) != NULL) {
32467c478bd9Sstevel@tonic-gate 			if (func(arg, ct) != 0)
32477c478bd9Sstevel@tonic-gate 				break;
32487c478bd9Sstevel@tonic-gate 			continue;
32497c478bd9Sstevel@tonic-gate 		}
32507c478bd9Sstevel@tonic-gate 	}
32517c478bd9Sstevel@tonic-gate }
32527c478bd9Sstevel@tonic-gate 
32537c478bd9Sstevel@tonic-gate /*
32547c478bd9Sstevel@tonic-gate  * A cacheinfo walker that adds associativity, line-size, and size properties
32557c478bd9Sstevel@tonic-gate  * to the devinfo node it is passed as an argument.
32567c478bd9Sstevel@tonic-gate  */
32577c478bd9Sstevel@tonic-gate static int
32587c478bd9Sstevel@tonic-gate add_cacheent_props(void *arg, const struct cachetab *ct)
32597c478bd9Sstevel@tonic-gate {
32607c478bd9Sstevel@tonic-gate 	dev_info_t *devi = arg;
32617c478bd9Sstevel@tonic-gate 
32627c478bd9Sstevel@tonic-gate 	add_cache_prop(devi, ct->ct_label, assoc_str, ct->ct_assoc);
32637c478bd9Sstevel@tonic-gate 	if (ct->ct_line_size != 0)
32647c478bd9Sstevel@tonic-gate 		add_cache_prop(devi, ct->ct_label, line_str,
32657c478bd9Sstevel@tonic-gate 		    ct->ct_line_size);
32667c478bd9Sstevel@tonic-gate 	add_cache_prop(devi, ct->ct_label, size_str, ct->ct_size);
32677c478bd9Sstevel@tonic-gate 	return (0);
32687c478bd9Sstevel@tonic-gate }
32697c478bd9Sstevel@tonic-gate 
3270f1d742a9Sksadhukh 
32717c478bd9Sstevel@tonic-gate static const char fully_assoc[] = "fully-associative?";
32727c478bd9Sstevel@tonic-gate 
32737c478bd9Sstevel@tonic-gate /*
32747c478bd9Sstevel@tonic-gate  * AMD style cache/tlb description
32757c478bd9Sstevel@tonic-gate  *
32767c478bd9Sstevel@tonic-gate  * Extended functions 5 and 6 directly describe properties of
32777c478bd9Sstevel@tonic-gate  * tlbs and various cache levels.
32787c478bd9Sstevel@tonic-gate  */
32797c478bd9Sstevel@tonic-gate static void
32807c478bd9Sstevel@tonic-gate add_amd_assoc(dev_info_t *devi, const char *label, uint_t assoc)
32817c478bd9Sstevel@tonic-gate {
32827c478bd9Sstevel@tonic-gate 	switch (assoc) {
32837c478bd9Sstevel@tonic-gate 	case 0:	/* reserved; ignore */
32847c478bd9Sstevel@tonic-gate 		break;
32857c478bd9Sstevel@tonic-gate 	default:
32867c478bd9Sstevel@tonic-gate 		add_cache_prop(devi, label, assoc_str, assoc);
32877c478bd9Sstevel@tonic-gate 		break;
32887c478bd9Sstevel@tonic-gate 	case 0xff:
32897c478bd9Sstevel@tonic-gate 		add_cache_prop(devi, label, fully_assoc, 1);
32907c478bd9Sstevel@tonic-gate 		break;
32917c478bd9Sstevel@tonic-gate 	}
32927c478bd9Sstevel@tonic-gate }
32937c478bd9Sstevel@tonic-gate 
32947c478bd9Sstevel@tonic-gate static void
32957c478bd9Sstevel@tonic-gate add_amd_tlb(dev_info_t *devi, const char *label, uint_t assoc, uint_t size)
32967c478bd9Sstevel@tonic-gate {
32977c478bd9Sstevel@tonic-gate 	if (size == 0)
32987c478bd9Sstevel@tonic-gate 		return;
32997c478bd9Sstevel@tonic-gate 	add_cache_prop(devi, label, size_str, size);
33007c478bd9Sstevel@tonic-gate 	add_amd_assoc(devi, label, assoc);
33017c478bd9Sstevel@tonic-gate }
33027c478bd9Sstevel@tonic-gate 
33037c478bd9Sstevel@tonic-gate static void
33047c478bd9Sstevel@tonic-gate add_amd_cache(dev_info_t *devi, const char *label,
33057c478bd9Sstevel@tonic-gate     uint_t size, uint_t assoc, uint_t lines_per_tag, uint_t line_size)
33067c478bd9Sstevel@tonic-gate {
33077c478bd9Sstevel@tonic-gate 	if (size == 0 || line_size == 0)
33087c478bd9Sstevel@tonic-gate 		return;
33097c478bd9Sstevel@tonic-gate 	add_amd_assoc(devi, label, assoc);
33107c478bd9Sstevel@tonic-gate 	/*
33117c478bd9Sstevel@tonic-gate 	 * Most AMD parts have a sectored cache. Multiple cache lines are
33127c478bd9Sstevel@tonic-gate 	 * associated with each tag. A sector consists of all cache lines
33137c478bd9Sstevel@tonic-gate 	 * associated with a tag. For example, the AMD K6-III has a sector
33147c478bd9Sstevel@tonic-gate 	 * size of 2 cache lines per tag.
33157c478bd9Sstevel@tonic-gate 	 */
33167c478bd9Sstevel@tonic-gate 	if (lines_per_tag != 0)
33177c478bd9Sstevel@tonic-gate 		add_cache_prop(devi, label, "lines-per-tag", lines_per_tag);
33187c478bd9Sstevel@tonic-gate 	add_cache_prop(devi, label, line_str, line_size);
33197c478bd9Sstevel@tonic-gate 	add_cache_prop(devi, label, size_str, size * 1024);
33207c478bd9Sstevel@tonic-gate }
33217c478bd9Sstevel@tonic-gate 
33227c478bd9Sstevel@tonic-gate static void
33237c478bd9Sstevel@tonic-gate add_amd_l2_assoc(dev_info_t *devi, const char *label, uint_t assoc)
33247c478bd9Sstevel@tonic-gate {
33257c478bd9Sstevel@tonic-gate 	switch (assoc) {
33267c478bd9Sstevel@tonic-gate 	case 0:	/* off */
33277c478bd9Sstevel@tonic-gate 		break;
33287c478bd9Sstevel@tonic-gate 	case 1:
33297c478bd9Sstevel@tonic-gate 	case 2:
33307c478bd9Sstevel@tonic-gate 	case 4:
33317c478bd9Sstevel@tonic-gate 		add_cache_prop(devi, label, assoc_str, assoc);
33327c478bd9Sstevel@tonic-gate 		break;
33337c478bd9Sstevel@tonic-gate 	case 6:
33347c478bd9Sstevel@tonic-gate 		add_cache_prop(devi, label, assoc_str, 8);
33357c478bd9Sstevel@tonic-gate 		break;
33367c478bd9Sstevel@tonic-gate 	case 8:
33377c478bd9Sstevel@tonic-gate 		add_cache_prop(devi, label, assoc_str, 16);
33387c478bd9Sstevel@tonic-gate 		break;
33397c478bd9Sstevel@tonic-gate 	case 0xf:
33407c478bd9Sstevel@tonic-gate 		add_cache_prop(devi, label, fully_assoc, 1);
33417c478bd9Sstevel@tonic-gate 		break;
33427c478bd9Sstevel@tonic-gate 	default: /* reserved; ignore */
33437c478bd9Sstevel@tonic-gate 		break;
33447c478bd9Sstevel@tonic-gate 	}
33457c478bd9Sstevel@tonic-gate }
33467c478bd9Sstevel@tonic-gate 
33477c478bd9Sstevel@tonic-gate static void
33487c478bd9Sstevel@tonic-gate add_amd_l2_tlb(dev_info_t *devi, const char *label, uint_t assoc, uint_t size)
33497c478bd9Sstevel@tonic-gate {
33507c478bd9Sstevel@tonic-gate 	if (size == 0 || assoc == 0)
33517c478bd9Sstevel@tonic-gate 		return;
33527c478bd9Sstevel@tonic-gate 	add_amd_l2_assoc(devi, label, assoc);
33537c478bd9Sstevel@tonic-gate 	add_cache_prop(devi, label, size_str, size);
33547c478bd9Sstevel@tonic-gate }
33557c478bd9Sstevel@tonic-gate 
33567c478bd9Sstevel@tonic-gate static void
33577c478bd9Sstevel@tonic-gate add_amd_l2_cache(dev_info_t *devi, const char *label,
33587c478bd9Sstevel@tonic-gate     uint_t size, uint_t assoc, uint_t lines_per_tag, uint_t line_size)
33597c478bd9Sstevel@tonic-gate {
33607c478bd9Sstevel@tonic-gate 	if (size == 0 || assoc == 0 || line_size == 0)
33617c478bd9Sstevel@tonic-gate 		return;
33627c478bd9Sstevel@tonic-gate 	add_amd_l2_assoc(devi, label, assoc);
33637c478bd9Sstevel@tonic-gate 	if (lines_per_tag != 0)
33647c478bd9Sstevel@tonic-gate 		add_cache_prop(devi, label, "lines-per-tag", lines_per_tag);
33657c478bd9Sstevel@tonic-gate 	add_cache_prop(devi, label, line_str, line_size);
33667c478bd9Sstevel@tonic-gate 	add_cache_prop(devi, label, size_str, size * 1024);
33677c478bd9Sstevel@tonic-gate }
33687c478bd9Sstevel@tonic-gate 
33697c478bd9Sstevel@tonic-gate static void
33707c478bd9Sstevel@tonic-gate amd_cache_info(struct cpuid_info *cpi, dev_info_t *devi)
33717c478bd9Sstevel@tonic-gate {
33728949bcd6Sandrei 	struct cpuid_regs *cp;
33737c478bd9Sstevel@tonic-gate 
33747c478bd9Sstevel@tonic-gate 	if (cpi->cpi_xmaxeax < 0x80000005)
33757c478bd9Sstevel@tonic-gate 		return;
33767c478bd9Sstevel@tonic-gate 	cp = &cpi->cpi_extd[5];
33777c478bd9Sstevel@tonic-gate 
33787c478bd9Sstevel@tonic-gate 	/*
33797c478bd9Sstevel@tonic-gate 	 * 4M/2M L1 TLB configuration
33807c478bd9Sstevel@tonic-gate 	 *
33817c478bd9Sstevel@tonic-gate 	 * We report the size for 2M pages because AMD uses two
33827c478bd9Sstevel@tonic-gate 	 * TLB entries for one 4M page.
33837c478bd9Sstevel@tonic-gate 	 */
33847c478bd9Sstevel@tonic-gate 	add_amd_tlb(devi, "dtlb-2M",
33857c478bd9Sstevel@tonic-gate 	    BITX(cp->cp_eax, 31, 24), BITX(cp->cp_eax, 23, 16));
33867c478bd9Sstevel@tonic-gate 	add_amd_tlb(devi, "itlb-2M",
33877c478bd9Sstevel@tonic-gate 	    BITX(cp->cp_eax, 15, 8), BITX(cp->cp_eax, 7, 0));
33887c478bd9Sstevel@tonic-gate 
33897c478bd9Sstevel@tonic-gate 	/*
33907c478bd9Sstevel@tonic-gate 	 * 4K L1 TLB configuration
33917c478bd9Sstevel@tonic-gate 	 */
33927c478bd9Sstevel@tonic-gate 
33937c478bd9Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
33947c478bd9Sstevel@tonic-gate 		uint_t nentries;
33957c478bd9Sstevel@tonic-gate 	case X86_VENDOR_TM:
33967c478bd9Sstevel@tonic-gate 		if (cpi->cpi_family >= 5) {
33977c478bd9Sstevel@tonic-gate 			/*
33987c478bd9Sstevel@tonic-gate 			 * Crusoe processors have 256 TLB entries, but
33997c478bd9Sstevel@tonic-gate 			 * cpuid data format constrains them to only
34007c478bd9Sstevel@tonic-gate 			 * reporting 255 of them.
34017c478bd9Sstevel@tonic-gate 			 */
34027c478bd9Sstevel@tonic-gate 			if ((nentries = BITX(cp->cp_ebx, 23, 16)) == 255)
34037c478bd9Sstevel@tonic-gate 				nentries = 256;
34047c478bd9Sstevel@tonic-gate 			/*
34057c478bd9Sstevel@tonic-gate 			 * Crusoe processors also have a unified TLB
34067c478bd9Sstevel@tonic-gate 			 */
34077c478bd9Sstevel@tonic-gate 			add_amd_tlb(devi, "tlb-4K", BITX(cp->cp_ebx, 31, 24),
34087c478bd9Sstevel@tonic-gate 			    nentries);
34097c478bd9Sstevel@tonic-gate 			break;
34107c478bd9Sstevel@tonic-gate 		}
34117c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
34127c478bd9Sstevel@tonic-gate 	default:
34137c478bd9Sstevel@tonic-gate 		add_amd_tlb(devi, itlb4k_str,
34147c478bd9Sstevel@tonic-gate 		    BITX(cp->cp_ebx, 31, 24), BITX(cp->cp_ebx, 23, 16));
34157c478bd9Sstevel@tonic-gate 		add_amd_tlb(devi, dtlb4k_str,
34167c478bd9Sstevel@tonic-gate 		    BITX(cp->cp_ebx, 15, 8), BITX(cp->cp_ebx, 7, 0));
34177c478bd9Sstevel@tonic-gate 		break;
34187c478bd9Sstevel@tonic-gate 	}
34197c478bd9Sstevel@tonic-gate 
34207c478bd9Sstevel@tonic-gate 	/*
34217c478bd9Sstevel@tonic-gate 	 * data L1 cache configuration
34227c478bd9Sstevel@tonic-gate 	 */
34237c478bd9Sstevel@tonic-gate 
34247c478bd9Sstevel@tonic-gate 	add_amd_cache(devi, l1_dcache_str,
34257c478bd9Sstevel@tonic-gate 	    BITX(cp->cp_ecx, 31, 24), BITX(cp->cp_ecx, 23, 16),
34267c478bd9Sstevel@tonic-gate 	    BITX(cp->cp_ecx, 15, 8), BITX(cp->cp_ecx, 7, 0));
34277c478bd9Sstevel@tonic-gate 
34287c478bd9Sstevel@tonic-gate 	/*
34297c478bd9Sstevel@tonic-gate 	 * code L1 cache configuration
34307c478bd9Sstevel@tonic-gate 	 */
34317c478bd9Sstevel@tonic-gate 
34327c478bd9Sstevel@tonic-gate 	add_amd_cache(devi, l1_icache_str,
34337c478bd9Sstevel@tonic-gate 	    BITX(cp->cp_edx, 31, 24), BITX(cp->cp_edx, 23, 16),
34347c478bd9Sstevel@tonic-gate 	    BITX(cp->cp_edx, 15, 8), BITX(cp->cp_edx, 7, 0));
34357c478bd9Sstevel@tonic-gate 
34367c478bd9Sstevel@tonic-gate 	if (cpi->cpi_xmaxeax < 0x80000006)
34377c478bd9Sstevel@tonic-gate 		return;
34387c478bd9Sstevel@tonic-gate 	cp = &cpi->cpi_extd[6];
34397c478bd9Sstevel@tonic-gate 
34407c478bd9Sstevel@tonic-gate 	/* Check for a unified L2 TLB for large pages */
34417c478bd9Sstevel@tonic-gate 
34427c478bd9Sstevel@tonic-gate 	if (BITX(cp->cp_eax, 31, 16) == 0)
34437c478bd9Sstevel@tonic-gate 		add_amd_l2_tlb(devi, "l2-tlb-2M",
34447c478bd9Sstevel@tonic-gate 		    BITX(cp->cp_eax, 15, 12), BITX(cp->cp_eax, 11, 0));
34457c478bd9Sstevel@tonic-gate 	else {
34467c478bd9Sstevel@tonic-gate 		add_amd_l2_tlb(devi, "l2-dtlb-2M",
34477c478bd9Sstevel@tonic-gate 		    BITX(cp->cp_eax, 31, 28), BITX(cp->cp_eax, 27, 16));
34487c478bd9Sstevel@tonic-gate 		add_amd_l2_tlb(devi, "l2-itlb-2M",
34497c478bd9Sstevel@tonic-gate 		    BITX(cp->cp_eax, 15, 12), BITX(cp->cp_eax, 11, 0));
34507c478bd9Sstevel@tonic-gate 	}
34517c478bd9Sstevel@tonic-gate 
34527c478bd9Sstevel@tonic-gate 	/* Check for a unified L2 TLB for 4K pages */
34537c478bd9Sstevel@tonic-gate 
34547c478bd9Sstevel@tonic-gate 	if (BITX(cp->cp_ebx, 31, 16) == 0) {
34557c478bd9Sstevel@tonic-gate 		add_amd_l2_tlb(devi, "l2-tlb-4K",
34567c478bd9Sstevel@tonic-gate 		    BITX(cp->cp_eax, 15, 12), BITX(cp->cp_eax, 11, 0));
34577c478bd9Sstevel@tonic-gate 	} else {
34587c478bd9Sstevel@tonic-gate 		add_amd_l2_tlb(devi, "l2-dtlb-4K",
34597c478bd9Sstevel@tonic-gate 		    BITX(cp->cp_eax, 31, 28), BITX(cp->cp_eax, 27, 16));
34607c478bd9Sstevel@tonic-gate 		add_amd_l2_tlb(devi, "l2-itlb-4K",
34617c478bd9Sstevel@tonic-gate 		    BITX(cp->cp_eax, 15, 12), BITX(cp->cp_eax, 11, 0));
34627c478bd9Sstevel@tonic-gate 	}
34637c478bd9Sstevel@tonic-gate 
34647c478bd9Sstevel@tonic-gate 	add_amd_l2_cache(devi, l2_cache_str,
34657c478bd9Sstevel@tonic-gate 	    BITX(cp->cp_ecx, 31, 16), BITX(cp->cp_ecx, 15, 12),
34667c478bd9Sstevel@tonic-gate 	    BITX(cp->cp_ecx, 11, 8), BITX(cp->cp_ecx, 7, 0));
34677c478bd9Sstevel@tonic-gate }
34687c478bd9Sstevel@tonic-gate 
34697c478bd9Sstevel@tonic-gate /*
34707c478bd9Sstevel@tonic-gate  * There are two basic ways that the x86 world describes it cache
34717c478bd9Sstevel@tonic-gate  * and tlb architecture - Intel's way and AMD's way.
34727c478bd9Sstevel@tonic-gate  *
34737c478bd9Sstevel@tonic-gate  * Return which flavor of cache architecture we should use
34747c478bd9Sstevel@tonic-gate  */
34757c478bd9Sstevel@tonic-gate static int
34767c478bd9Sstevel@tonic-gate x86_which_cacheinfo(struct cpuid_info *cpi)
34777c478bd9Sstevel@tonic-gate {
34787c478bd9Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
34797c478bd9Sstevel@tonic-gate 	case X86_VENDOR_Intel:
34807c478bd9Sstevel@tonic-gate 		if (cpi->cpi_maxeax >= 2)
34817c478bd9Sstevel@tonic-gate 			return (X86_VENDOR_Intel);
34827c478bd9Sstevel@tonic-gate 		break;
34837c478bd9Sstevel@tonic-gate 	case X86_VENDOR_AMD:
34847c478bd9Sstevel@tonic-gate 		/*
34857c478bd9Sstevel@tonic-gate 		 * The K5 model 1 was the first part from AMD that reported
34867c478bd9Sstevel@tonic-gate 		 * cache sizes via extended cpuid functions.
34877c478bd9Sstevel@tonic-gate 		 */
34887c478bd9Sstevel@tonic-gate 		if (cpi->cpi_family > 5 ||
34897c478bd9Sstevel@tonic-gate 		    (cpi->cpi_family == 5 && cpi->cpi_model >= 1))
34907c478bd9Sstevel@tonic-gate 			return (X86_VENDOR_AMD);
34917c478bd9Sstevel@tonic-gate 		break;
34927c478bd9Sstevel@tonic-gate 	case X86_VENDOR_TM:
34937c478bd9Sstevel@tonic-gate 		if (cpi->cpi_family >= 5)
34947c478bd9Sstevel@tonic-gate 			return (X86_VENDOR_AMD);
34957c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
34967c478bd9Sstevel@tonic-gate 	default:
34977c478bd9Sstevel@tonic-gate 		/*
34987c478bd9Sstevel@tonic-gate 		 * If they have extended CPU data for 0x80000005
34997c478bd9Sstevel@tonic-gate 		 * then we assume they have AMD-format cache
35007c478bd9Sstevel@tonic-gate 		 * information.
35017c478bd9Sstevel@tonic-gate 		 *
35027c478bd9Sstevel@tonic-gate 		 * If not, and the vendor happens to be Cyrix,
35037c478bd9Sstevel@tonic-gate 		 * then try our-Cyrix specific handler.
35047c478bd9Sstevel@tonic-gate 		 *
35057c478bd9Sstevel@tonic-gate 		 * If we're not Cyrix, then assume we're using Intel's
35067c478bd9Sstevel@tonic-gate 		 * table-driven format instead.
35077c478bd9Sstevel@tonic-gate 		 */
35087c478bd9Sstevel@tonic-gate 		if (cpi->cpi_xmaxeax >= 0x80000005)
35097c478bd9Sstevel@tonic-gate 			return (X86_VENDOR_AMD);
35107c478bd9Sstevel@tonic-gate 		else if (cpi->cpi_vendor == X86_VENDOR_Cyrix)
35117c478bd9Sstevel@tonic-gate 			return (X86_VENDOR_Cyrix);
35127c478bd9Sstevel@tonic-gate 		else if (cpi->cpi_maxeax >= 2)
35137c478bd9Sstevel@tonic-gate 			return (X86_VENDOR_Intel);
35147c478bd9Sstevel@tonic-gate 		break;
35157c478bd9Sstevel@tonic-gate 	}
35167c478bd9Sstevel@tonic-gate 	return (-1);
35177c478bd9Sstevel@tonic-gate }
35187c478bd9Sstevel@tonic-gate 
35197c478bd9Sstevel@tonic-gate void
3520fa96bd91SMichael Corcoran cpuid_set_cpu_properties(void *dip, processorid_t cpu_id,
3521fa96bd91SMichael Corcoran     struct cpuid_info *cpi)
35227c478bd9Sstevel@tonic-gate {
35237c478bd9Sstevel@tonic-gate 	dev_info_t *cpu_devi;
35247c478bd9Sstevel@tonic-gate 	int create;
35257c478bd9Sstevel@tonic-gate 
3526fa96bd91SMichael Corcoran 	cpu_devi = (dev_info_t *)dip;
35277c478bd9Sstevel@tonic-gate 
35287c478bd9Sstevel@tonic-gate 	/* device_type */
35297c478bd9Sstevel@tonic-gate 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, cpu_devi,
35307c478bd9Sstevel@tonic-gate 	    "device_type", "cpu");
35317c478bd9Sstevel@tonic-gate 
35327c478bd9Sstevel@tonic-gate 	/* reg */
35337c478bd9Sstevel@tonic-gate 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
35347c478bd9Sstevel@tonic-gate 	    "reg", cpu_id);
35357c478bd9Sstevel@tonic-gate 
35367c478bd9Sstevel@tonic-gate 	/* cpu-mhz, and clock-frequency */
35377c478bd9Sstevel@tonic-gate 	if (cpu_freq > 0) {
35387c478bd9Sstevel@tonic-gate 		long long mul;
35397c478bd9Sstevel@tonic-gate 
35407c478bd9Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
35417c478bd9Sstevel@tonic-gate 		    "cpu-mhz", cpu_freq);
35427c478bd9Sstevel@tonic-gate 		if ((mul = cpu_freq * 1000000LL) <= INT_MAX)
35437c478bd9Sstevel@tonic-gate 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
35447c478bd9Sstevel@tonic-gate 			    "clock-frequency", (int)mul);
35457c478bd9Sstevel@tonic-gate 	}
35467c478bd9Sstevel@tonic-gate 
35477c478bd9Sstevel@tonic-gate 	if ((x86_feature & X86_CPUID) == 0) {
35487c478bd9Sstevel@tonic-gate 		return;
35497c478bd9Sstevel@tonic-gate 	}
35507c478bd9Sstevel@tonic-gate 
35517c478bd9Sstevel@tonic-gate 	/* vendor-id */
35527c478bd9Sstevel@tonic-gate 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, cpu_devi,
35537c478bd9Sstevel@tonic-gate 	    "vendor-id", cpi->cpi_vendorstr);
35547c478bd9Sstevel@tonic-gate 
35557c478bd9Sstevel@tonic-gate 	if (cpi->cpi_maxeax == 0) {
35567c478bd9Sstevel@tonic-gate 		return;
35577c478bd9Sstevel@tonic-gate 	}
35587c478bd9Sstevel@tonic-gate 
35597c478bd9Sstevel@tonic-gate 	/*
35607c478bd9Sstevel@tonic-gate 	 * family, model, and step
35617c478bd9Sstevel@tonic-gate 	 */
35627c478bd9Sstevel@tonic-gate 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
35637c478bd9Sstevel@tonic-gate 	    "family", CPI_FAMILY(cpi));
35647c478bd9Sstevel@tonic-gate 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
35657c478bd9Sstevel@tonic-gate 	    "cpu-model", CPI_MODEL(cpi));
35667c478bd9Sstevel@tonic-gate 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
35677c478bd9Sstevel@tonic-gate 	    "stepping-id", CPI_STEP(cpi));
35687c478bd9Sstevel@tonic-gate 
35697c478bd9Sstevel@tonic-gate 	/* type */
35707c478bd9Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
35717c478bd9Sstevel@tonic-gate 	case X86_VENDOR_Intel:
35727c478bd9Sstevel@tonic-gate 		create = 1;
35737c478bd9Sstevel@tonic-gate 		break;
35747c478bd9Sstevel@tonic-gate 	default:
35757c478bd9Sstevel@tonic-gate 		create = 0;
35767c478bd9Sstevel@tonic-gate 		break;
35777c478bd9Sstevel@tonic-gate 	}
35787c478bd9Sstevel@tonic-gate 	if (create)
35797c478bd9Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
35807c478bd9Sstevel@tonic-gate 		    "type", CPI_TYPE(cpi));
35817c478bd9Sstevel@tonic-gate 
35827c478bd9Sstevel@tonic-gate 	/* ext-family */
35837c478bd9Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
35847c478bd9Sstevel@tonic-gate 	case X86_VENDOR_Intel:
35857c478bd9Sstevel@tonic-gate 	case X86_VENDOR_AMD:
35867c478bd9Sstevel@tonic-gate 		create = cpi->cpi_family >= 0xf;
35877c478bd9Sstevel@tonic-gate 		break;
35887c478bd9Sstevel@tonic-gate 	default:
35897c478bd9Sstevel@tonic-gate 		create = 0;
35907c478bd9Sstevel@tonic-gate 		break;
35917c478bd9Sstevel@tonic-gate 	}
35927c478bd9Sstevel@tonic-gate 	if (create)
35937c478bd9Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
35947c478bd9Sstevel@tonic-gate 		    "ext-family", CPI_FAMILY_XTD(cpi));
35957c478bd9Sstevel@tonic-gate 
35967c478bd9Sstevel@tonic-gate 	/* ext-model */
35977c478bd9Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
35987c478bd9Sstevel@tonic-gate 	case X86_VENDOR_Intel:
359963d3f7dfSkk208521 		create = IS_EXTENDED_MODEL_INTEL(cpi);
360068c91426Sdmick 		break;
36017c478bd9Sstevel@tonic-gate 	case X86_VENDOR_AMD:
3602ee88d2b9Skchow 		create = CPI_FAMILY(cpi) == 0xf;
36037c478bd9Sstevel@tonic-gate 		break;
36047c478bd9Sstevel@tonic-gate 	default:
36057c478bd9Sstevel@tonic-gate 		create = 0;
36067c478bd9Sstevel@tonic-gate 		break;
36077c478bd9Sstevel@tonic-gate 	}
36087c478bd9Sstevel@tonic-gate 	if (create)
36097c478bd9Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
36107c478bd9Sstevel@tonic-gate 		    "ext-model", CPI_MODEL_XTD(cpi));
36117c478bd9Sstevel@tonic-gate 
36127c478bd9Sstevel@tonic-gate 	/* generation */
36137c478bd9Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
36147c478bd9Sstevel@tonic-gate 	case X86_VENDOR_AMD:
36157c478bd9Sstevel@tonic-gate 		/*
36167c478bd9Sstevel@tonic-gate 		 * AMD K5 model 1 was the first part to support this
36177c478bd9Sstevel@tonic-gate 		 */
36187c478bd9Sstevel@tonic-gate 		create = cpi->cpi_xmaxeax >= 0x80000001;
36197c478bd9Sstevel@tonic-gate 		break;
36207c478bd9Sstevel@tonic-gate 	default:
36217c478bd9Sstevel@tonic-gate 		create = 0;
36227c478bd9Sstevel@tonic-gate 		break;
36237c478bd9Sstevel@tonic-gate 	}
36247c478bd9Sstevel@tonic-gate 	if (create)
36257c478bd9Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
36267c478bd9Sstevel@tonic-gate 		    "generation", BITX((cpi)->cpi_extd[1].cp_eax, 11, 8));
36277c478bd9Sstevel@tonic-gate 
36287c478bd9Sstevel@tonic-gate 	/* brand-id */
36297c478bd9Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
36307c478bd9Sstevel@tonic-gate 	case X86_VENDOR_Intel:
36317c478bd9Sstevel@tonic-gate 		/*
36327c478bd9Sstevel@tonic-gate 		 * brand id first appeared on Pentium III Xeon model 8,
36337c478bd9Sstevel@tonic-gate 		 * and Celeron model 8 processors and Opteron
36347c478bd9Sstevel@tonic-gate 		 */
36357c478bd9Sstevel@tonic-gate 		create = cpi->cpi_family > 6 ||
36367c478bd9Sstevel@tonic-gate 		    (cpi->cpi_family == 6 && cpi->cpi_model >= 8);
36377c478bd9Sstevel@tonic-gate 		break;
36387c478bd9Sstevel@tonic-gate 	case X86_VENDOR_AMD:
36397c478bd9Sstevel@tonic-gate 		create = cpi->cpi_family >= 0xf;
36407c478bd9Sstevel@tonic-gate 		break;
36417c478bd9Sstevel@tonic-gate 	default:
36427c478bd9Sstevel@tonic-gate 		create = 0;
36437c478bd9Sstevel@tonic-gate 		break;
36447c478bd9Sstevel@tonic-gate 	}
36457c478bd9Sstevel@tonic-gate 	if (create && cpi->cpi_brandid != 0) {
36467c478bd9Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
36477c478bd9Sstevel@tonic-gate 		    "brand-id", cpi->cpi_brandid);
36487c478bd9Sstevel@tonic-gate 	}
36497c478bd9Sstevel@tonic-gate 
36507c478bd9Sstevel@tonic-gate 	/* chunks, and apic-id */
36517c478bd9Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
36527c478bd9Sstevel@tonic-gate 		/*
36537c478bd9Sstevel@tonic-gate 		 * first available on Pentium IV and Opteron (K8)
36547c478bd9Sstevel@tonic-gate 		 */
36555ff02082Sdmick 	case X86_VENDOR_Intel:
36565ff02082Sdmick 		create = IS_NEW_F6(cpi) || cpi->cpi_family >= 0xf;
36575ff02082Sdmick 		break;
36585ff02082Sdmick 	case X86_VENDOR_AMD:
36597c478bd9Sstevel@tonic-gate 		create = cpi->cpi_family >= 0xf;
36607c478bd9Sstevel@tonic-gate 		break;
36617c478bd9Sstevel@tonic-gate 	default:
36627c478bd9Sstevel@tonic-gate 		create = 0;
36637c478bd9Sstevel@tonic-gate 		break;
36647c478bd9Sstevel@tonic-gate 	}
36657c478bd9Sstevel@tonic-gate 	if (create) {
36667c478bd9Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
36677c478bd9Sstevel@tonic-gate 		    "chunks", CPI_CHUNKS(cpi));
36687c478bd9Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3669b6917abeSmishra 		    "apic-id", cpi->cpi_apicid);
36707aec1d6eScindi 		if (cpi->cpi_chipid >= 0) {
36717c478bd9Sstevel@tonic-gate 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
36727c478bd9Sstevel@tonic-gate 			    "chip#", cpi->cpi_chipid);
36737aec1d6eScindi 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
36747aec1d6eScindi 			    "clog#", cpi->cpi_clogid);
36757aec1d6eScindi 		}
36767c478bd9Sstevel@tonic-gate 	}
36777c478bd9Sstevel@tonic-gate 
36787c478bd9Sstevel@tonic-gate 	/* cpuid-features */
36797c478bd9Sstevel@tonic-gate 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
36807c478bd9Sstevel@tonic-gate 	    "cpuid-features", CPI_FEATURES_EDX(cpi));
36817c478bd9Sstevel@tonic-gate 
36827c478bd9Sstevel@tonic-gate 
36837c478bd9Sstevel@tonic-gate 	/* cpuid-features-ecx */
36847c478bd9Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
36857c478bd9Sstevel@tonic-gate 	case X86_VENDOR_Intel:
36865ff02082Sdmick 		create = IS_NEW_F6(cpi) || cpi->cpi_family >= 0xf;
36877c478bd9Sstevel@tonic-gate 		break;
36887c478bd9Sstevel@tonic-gate 	default:
36897c478bd9Sstevel@tonic-gate 		create = 0;
36907c478bd9Sstevel@tonic-gate 		break;
36917c478bd9Sstevel@tonic-gate 	}
36927c478bd9Sstevel@tonic-gate 	if (create)
36937c478bd9Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
36947c478bd9Sstevel@tonic-gate 		    "cpuid-features-ecx", CPI_FEATURES_ECX(cpi));
36957c478bd9Sstevel@tonic-gate 
36967c478bd9Sstevel@tonic-gate 	/* ext-cpuid-features */
36977c478bd9Sstevel@tonic-gate 	switch (cpi->cpi_vendor) {
36985ff02082Sdmick 	case X86_VENDOR_Intel:
36997c478bd9Sstevel@tonic-gate 	case X86_VENDOR_AMD:
37007c478bd9Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
37017c478bd9Sstevel@tonic-gate 	case X86_VENDOR_TM:
37027c478bd9Sstevel@tonic-gate 	case X86_VENDOR_Centaur:
37037c478bd9Sstevel@tonic-gate 		create = cpi->cpi_xmaxeax >= 0x80000001;
37047c478bd9Sstevel@tonic-gate 		break;
37057c478bd9Sstevel@tonic-gate 	default:
37067c478bd9Sstevel@tonic-gate 		create = 0;
37077c478bd9Sstevel@tonic-gate 		break;
37087c478bd9Sstevel@tonic-gate 	}
37095ff02082Sdmick 	if (create) {
37107c478bd9Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
37117c478bd9Sstevel@tonic-gate 		    "ext-cpuid-features", CPI_FEATURES_XTD_EDX(cpi));
37125ff02082Sdmick 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
37135ff02082Sdmick 		    "ext-cpuid-features-ecx", CPI_FEATURES_XTD_ECX(cpi));
37145ff02082Sdmick 	}
37157c478bd9Sstevel@tonic-gate 
37167c478bd9Sstevel@tonic-gate 	/*
37177c478bd9Sstevel@tonic-gate 	 * Brand String first appeared in Intel Pentium IV, AMD K5
37187c478bd9Sstevel@tonic-gate 	 * model 1, and Cyrix GXm.  On earlier models we try and
37197c478bd9Sstevel@tonic-gate 	 * simulate something similar .. so this string should always
37207c478bd9Sstevel@tonic-gate 	 * same -something- about the processor, however lame.
37217c478bd9Sstevel@tonic-gate 	 */
37227c478bd9Sstevel@tonic-gate 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, cpu_devi,
37237c478bd9Sstevel@tonic-gate 	    "brand-string", cpi->cpi_brandstr);
37247c478bd9Sstevel@tonic-gate 
37257c478bd9Sstevel@tonic-gate 	/*
37267c478bd9Sstevel@tonic-gate 	 * Finally, cache and tlb information
37277c478bd9Sstevel@tonic-gate 	 */
37287c478bd9Sstevel@tonic-gate 	switch (x86_which_cacheinfo(cpi)) {
37297c478bd9Sstevel@tonic-gate 	case X86_VENDOR_Intel:
37307c478bd9Sstevel@tonic-gate 		intel_walk_cacheinfo(cpi, cpu_devi, add_cacheent_props);
37317c478bd9Sstevel@tonic-gate 		break;
37327c478bd9Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
37337c478bd9Sstevel@tonic-gate 		cyrix_walk_cacheinfo(cpi, cpu_devi, add_cacheent_props);
37347c478bd9Sstevel@tonic-gate 		break;
37357c478bd9Sstevel@tonic-gate 	case X86_VENDOR_AMD:
37367c478bd9Sstevel@tonic-gate 		amd_cache_info(cpi, cpu_devi);
37377c478bd9Sstevel@tonic-gate 		break;
37387c478bd9Sstevel@tonic-gate 	default:
37397c478bd9Sstevel@tonic-gate 		break;
37407c478bd9Sstevel@tonic-gate 	}
37417c478bd9Sstevel@tonic-gate }
37427c478bd9Sstevel@tonic-gate 
37437c478bd9Sstevel@tonic-gate struct l2info {
37447c478bd9Sstevel@tonic-gate 	int *l2i_csz;
37457c478bd9Sstevel@tonic-gate 	int *l2i_lsz;
37467c478bd9Sstevel@tonic-gate 	int *l2i_assoc;
37477c478bd9Sstevel@tonic-gate 	int l2i_ret;
37487c478bd9Sstevel@tonic-gate };
37497c478bd9Sstevel@tonic-gate 
37507c478bd9Sstevel@tonic-gate /*
37517c478bd9Sstevel@tonic-gate  * A cacheinfo walker that fetches the size, line-size and associativity
37527c478bd9Sstevel@tonic-gate  * of the L2 cache
37537c478bd9Sstevel@tonic-gate  */
37547c478bd9Sstevel@tonic-gate static int
37557c478bd9Sstevel@tonic-gate intel_l2cinfo(void *arg, const struct cachetab *ct)
37567c478bd9Sstevel@tonic-gate {
37577c478bd9Sstevel@tonic-gate 	struct l2info *l2i = arg;
37587c478bd9Sstevel@tonic-gate 	int *ip;
37597c478bd9Sstevel@tonic-gate 
37607c478bd9Sstevel@tonic-gate 	if (ct->ct_label != l2_cache_str &&
37617c478bd9Sstevel@tonic-gate 	    ct->ct_label != sl2_cache_str)
37627c478bd9Sstevel@tonic-gate 		return (0);	/* not an L2 -- keep walking */
37637c478bd9Sstevel@tonic-gate 
37647c478bd9Sstevel@tonic-gate 	if ((ip = l2i->l2i_csz) != NULL)
37657c478bd9Sstevel@tonic-gate 		*ip = ct->ct_size;
37667c478bd9Sstevel@tonic-gate 	if ((ip = l2i->l2i_lsz) != NULL)
37677c478bd9Sstevel@tonic-gate 		*ip = ct->ct_line_size;
37687c478bd9Sstevel@tonic-gate 	if ((ip = l2i->l2i_assoc) != NULL)
37697c478bd9Sstevel@tonic-gate 		*ip = ct->ct_assoc;
37707c478bd9Sstevel@tonic-gate 	l2i->l2i_ret = ct->ct_size;
37717c478bd9Sstevel@tonic-gate 	return (1);		/* was an L2 -- terminate walk */
37727c478bd9Sstevel@tonic-gate }
37737c478bd9Sstevel@tonic-gate 
3774606303c9Skchow /*
3775606303c9Skchow  * AMD L2/L3 Cache and TLB Associativity Field Definition:
3776606303c9Skchow  *
3777606303c9Skchow  *	Unlike the associativity for the L1 cache and tlb where the 8 bit
3778606303c9Skchow  *	value is the associativity, the associativity for the L2 cache and
3779606303c9Skchow  *	tlb is encoded in the following table. The 4 bit L2 value serves as
3780606303c9Skchow  *	an index into the amd_afd[] array to determine the associativity.
3781606303c9Skchow  *	-1 is undefined. 0 is fully associative.
3782606303c9Skchow  */
3783606303c9Skchow 
3784606303c9Skchow static int amd_afd[] =
3785606303c9Skchow 	{-1, 1, 2, -1, 4, -1, 8, -1, 16, -1, 32, 48, 64, 96, 128, 0};
3786606303c9Skchow 
37877c478bd9Sstevel@tonic-gate static void
37887c478bd9Sstevel@tonic-gate amd_l2cacheinfo(struct cpuid_info *cpi, struct l2info *l2i)
37897c478bd9Sstevel@tonic-gate {
37908949bcd6Sandrei 	struct cpuid_regs *cp;
37917c478bd9Sstevel@tonic-gate 	uint_t size, assoc;
3792606303c9Skchow 	int i;
37937c478bd9Sstevel@tonic-gate 	int *ip;
37947c478bd9Sstevel@tonic-gate 
37957c478bd9Sstevel@tonic-gate 	if (cpi->cpi_xmaxeax < 0x80000006)
37967c478bd9Sstevel@tonic-gate 		return;
37977c478bd9Sstevel@tonic-gate 	cp = &cpi->cpi_extd[6];
37987c478bd9Sstevel@tonic-gate 
3799606303c9Skchow 	if ((i = BITX(cp->cp_ecx, 15, 12)) != 0 &&
38007c478bd9Sstevel@tonic-gate 	    (size = BITX(cp->cp_ecx, 31, 16)) != 0) {
38017c478bd9Sstevel@tonic-gate 		uint_t cachesz = size * 1024;
3802606303c9Skchow 		assoc = amd_afd[i];
38037c478bd9Sstevel@tonic-gate 
3804606303c9Skchow 		ASSERT(assoc != -1);
38057c478bd9Sstevel@tonic-gate 
38067c478bd9Sstevel@tonic-gate 		if ((ip = l2i->l2i_csz) != NULL)
38077c478bd9Sstevel@tonic-gate 			*ip = cachesz;
38087c478bd9Sstevel@tonic-gate 		if ((ip = l2i->l2i_lsz) != NULL)
38097c478bd9Sstevel@tonic-gate 			*ip = BITX(cp->cp_ecx, 7, 0);
38107c478bd9Sstevel@tonic-gate 		if ((ip = l2i->l2i_assoc) != NULL)
38117c478bd9Sstevel@tonic-gate 			*ip = assoc;
38127c478bd9Sstevel@tonic-gate 		l2i->l2i_ret = cachesz;
38137c478bd9Sstevel@tonic-gate 	}
38147c478bd9Sstevel@tonic-gate }
38157c478bd9Sstevel@tonic-gate 
38167c478bd9Sstevel@tonic-gate int
38177c478bd9Sstevel@tonic-gate getl2cacheinfo(cpu_t *cpu, int *csz, int *lsz, int *assoc)
38187c478bd9Sstevel@tonic-gate {
38197c478bd9Sstevel@tonic-gate 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
38207c478bd9Sstevel@tonic-gate 	struct l2info __l2info, *l2i = &__l2info;
38217c478bd9Sstevel@tonic-gate 
38227c478bd9Sstevel@tonic-gate 	l2i->l2i_csz = csz;
38237c478bd9Sstevel@tonic-gate 	l2i->l2i_lsz = lsz;
38247c478bd9Sstevel@tonic-gate 	l2i->l2i_assoc = assoc;
38257c478bd9Sstevel@tonic-gate 	l2i->l2i_ret = -1;
38267c478bd9Sstevel@tonic-gate 
38277c478bd9Sstevel@tonic-gate 	switch (x86_which_cacheinfo(cpi)) {
38287c478bd9Sstevel@tonic-gate 	case X86_VENDOR_Intel:
38297c478bd9Sstevel@tonic-gate 		intel_walk_cacheinfo(cpi, l2i, intel_l2cinfo);
38307c478bd9Sstevel@tonic-gate 		break;
38317c478bd9Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
38327c478bd9Sstevel@tonic-gate 		cyrix_walk_cacheinfo(cpi, l2i, intel_l2cinfo);
38337c478bd9Sstevel@tonic-gate 		break;
38347c478bd9Sstevel@tonic-gate 	case X86_VENDOR_AMD:
38357c478bd9Sstevel@tonic-gate 		amd_l2cacheinfo(cpi, l2i);
38367c478bd9Sstevel@tonic-gate 		break;
38377c478bd9Sstevel@tonic-gate 	default:
38387c478bd9Sstevel@tonic-gate 		break;
38397c478bd9Sstevel@tonic-gate 	}
38407c478bd9Sstevel@tonic-gate 	return (l2i->l2i_ret);
38417c478bd9Sstevel@tonic-gate }
3842f98fbcecSbholler 
3843843e1988Sjohnlev #if !defined(__xpv)
3844843e1988Sjohnlev 
38455b8a6efeSbholler uint32_t *
38465b8a6efeSbholler cpuid_mwait_alloc(cpu_t *cpu)
38475b8a6efeSbholler {
38485b8a6efeSbholler 	uint32_t	*ret;
38495b8a6efeSbholler 	size_t		mwait_size;
38505b8a6efeSbholler 
38515b8a6efeSbholler 	ASSERT(cpuid_checkpass(cpu, 2));
38525b8a6efeSbholler 
38535b8a6efeSbholler 	mwait_size = cpu->cpu_m.mcpu_cpi->cpi_mwait.mon_max;
38545b8a6efeSbholler 	if (mwait_size == 0)
38555b8a6efeSbholler 		return (NULL);
38565b8a6efeSbholler 
38575b8a6efeSbholler 	/*
38585b8a6efeSbholler 	 * kmem_alloc() returns cache line size aligned data for mwait_size
38595b8a6efeSbholler 	 * allocations.  mwait_size is currently cache line sized.  Neither
38605b8a6efeSbholler 	 * of these implementation details are guarantied to be true in the
38615b8a6efeSbholler 	 * future.
38625b8a6efeSbholler 	 *
38635b8a6efeSbholler 	 * First try allocating mwait_size as kmem_alloc() currently returns
38645b8a6efeSbholler 	 * correctly aligned memory.  If kmem_alloc() does not return
38655b8a6efeSbholler 	 * mwait_size aligned memory, then use mwait_size ROUNDUP.
38665b8a6efeSbholler 	 *
38675b8a6efeSbholler 	 * Set cpi_mwait.buf_actual and cpi_mwait.size_actual in case we
38685b8a6efeSbholler 	 * decide to free this memory.
38695b8a6efeSbholler 	 */
38705b8a6efeSbholler 	ret = kmem_zalloc(mwait_size, KM_SLEEP);
38715b8a6efeSbholler 	if (ret == (uint32_t *)P2ROUNDUP((uintptr_t)ret, mwait_size)) {
38725b8a6efeSbholler 		cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual = ret;
38735b8a6efeSbholler 		cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual = mwait_size;
38745b8a6efeSbholler 		*ret = MWAIT_RUNNING;
38755b8a6efeSbholler 		return (ret);
38765b8a6efeSbholler 	} else {
38775b8a6efeSbholler 		kmem_free(ret, mwait_size);
38785b8a6efeSbholler 		ret = kmem_zalloc(mwait_size * 2, KM_SLEEP);
38795b8a6efeSbholler 		cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual = ret;
38805b8a6efeSbholler 		cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual = mwait_size * 2;
38815b8a6efeSbholler 		ret = (uint32_t *)P2ROUNDUP((uintptr_t)ret, mwait_size);
38825b8a6efeSbholler 		*ret = MWAIT_RUNNING;
38835b8a6efeSbholler 		return (ret);
38845b8a6efeSbholler 	}
38855b8a6efeSbholler }
38865b8a6efeSbholler 
38875b8a6efeSbholler void
38885b8a6efeSbholler cpuid_mwait_free(cpu_t *cpu)
3889f98fbcecSbholler {
3890f98fbcecSbholler 	ASSERT(cpuid_checkpass(cpu, 2));
38915b8a6efeSbholler 
38925b8a6efeSbholler 	if (cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual != NULL &&
38935b8a6efeSbholler 	    cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual > 0) {
38945b8a6efeSbholler 		kmem_free(cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual,
38955b8a6efeSbholler 		    cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual);
38965b8a6efeSbholler 	}
38975b8a6efeSbholler 
38985b8a6efeSbholler 	cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual = NULL;
38995b8a6efeSbholler 	cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual = 0;
3900f98fbcecSbholler }
3901843e1988Sjohnlev 
3902247dbb3dSsudheer void
3903247dbb3dSsudheer patch_tsc_read(int flag)
3904247dbb3dSsudheer {
3905247dbb3dSsudheer 	size_t cnt;
3906e4b86885SCheng Sean Ye 
3907247dbb3dSsudheer 	switch (flag) {
3908247dbb3dSsudheer 	case X86_NO_TSC:
3909247dbb3dSsudheer 		cnt = &_no_rdtsc_end - &_no_rdtsc_start;
39102b0bcb26Ssudheer 		(void) memcpy((void *)tsc_read, (void *)&_no_rdtsc_start, cnt);
3911247dbb3dSsudheer 		break;
3912247dbb3dSsudheer 	case X86_HAVE_TSCP:
3913247dbb3dSsudheer 		cnt = &_tscp_end - &_tscp_start;
39142b0bcb26Ssudheer 		(void) memcpy((void *)tsc_read, (void *)&_tscp_start, cnt);
3915247dbb3dSsudheer 		break;
3916247dbb3dSsudheer 	case X86_TSC_MFENCE:
3917247dbb3dSsudheer 		cnt = &_tsc_mfence_end - &_tsc_mfence_start;
39182b0bcb26Ssudheer 		(void) memcpy((void *)tsc_read,
39192b0bcb26Ssudheer 		    (void *)&_tsc_mfence_start, cnt);
3920247dbb3dSsudheer 		break;
392115363b27Ssudheer 	case X86_TSC_LFENCE:
392215363b27Ssudheer 		cnt = &_tsc_lfence_end - &_tsc_lfence_start;
392315363b27Ssudheer 		(void) memcpy((void *)tsc_read,
392415363b27Ssudheer 		    (void *)&_tsc_lfence_start, cnt);
392515363b27Ssudheer 		break;
3926247dbb3dSsudheer 	default:
3927247dbb3dSsudheer 		break;
3928247dbb3dSsudheer 	}
3929247dbb3dSsudheer }
3930247dbb3dSsudheer 
39310e751525SEric Saxe int
39320e751525SEric Saxe cpuid_deep_cstates_supported(void)
39330e751525SEric Saxe {
39340e751525SEric Saxe 	struct cpuid_info *cpi;
39350e751525SEric Saxe 	struct cpuid_regs regs;
39360e751525SEric Saxe 
39370e751525SEric Saxe 	ASSERT(cpuid_checkpass(CPU, 1));
39380e751525SEric Saxe 
39390e751525SEric Saxe 	cpi = CPU->cpu_m.mcpu_cpi;
39400e751525SEric Saxe 
39410e751525SEric Saxe 	if (!(x86_feature & X86_CPUID))
39420e751525SEric Saxe 		return (0);
39430e751525SEric Saxe 
39440e751525SEric Saxe 	switch (cpi->cpi_vendor) {
39450e751525SEric Saxe 	case X86_VENDOR_Intel:
39460e751525SEric Saxe 		if (cpi->cpi_xmaxeax < 0x80000007)
39470e751525SEric Saxe 			return (0);
39480e751525SEric Saxe 
39490e751525SEric Saxe 		/*
39500e751525SEric Saxe 		 * TSC run at a constant rate in all ACPI C-states?
39510e751525SEric Saxe 		 */
39520e751525SEric Saxe 		regs.cp_eax = 0x80000007;
39530e751525SEric Saxe 		(void) __cpuid_insn(&regs);
39540e751525SEric Saxe 		return (regs.cp_edx & CPUID_TSC_CSTATE_INVARIANCE);
39550e751525SEric Saxe 
39560e751525SEric Saxe 	default:
39570e751525SEric Saxe 		return (0);
39580e751525SEric Saxe 	}
39590e751525SEric Saxe }
39600e751525SEric Saxe 
3961e774b42bSBill Holler #endif	/* !__xpv */
3962e774b42bSBill Holler 
3963e774b42bSBill Holler void
3964e774b42bSBill Holler post_startup_cpu_fixups(void)
3965e774b42bSBill Holler {
3966e774b42bSBill Holler #ifndef __xpv
3967e774b42bSBill Holler 	/*
3968e774b42bSBill Holler 	 * Some AMD processors support C1E state. Entering this state will
3969e774b42bSBill Holler 	 * cause the local APIC timer to stop, which we can't deal with at
3970e774b42bSBill Holler 	 * this time.
3971e774b42bSBill Holler 	 */
3972e774b42bSBill Holler 	if (cpuid_getvendor(CPU) == X86_VENDOR_AMD) {
3973e774b42bSBill Holler 		on_trap_data_t otd;
3974e774b42bSBill Holler 		uint64_t reg;
3975e774b42bSBill Holler 
3976e774b42bSBill Holler 		if (!on_trap(&otd, OT_DATA_ACCESS)) {
3977e774b42bSBill Holler 			reg = rdmsr(MSR_AMD_INT_PENDING_CMP_HALT);
3978e774b42bSBill Holler 			/* Disable C1E state if it is enabled by BIOS */
3979e774b42bSBill Holler 			if ((reg >> AMD_ACTONCMPHALT_SHIFT) &
3980e774b42bSBill Holler 			    AMD_ACTONCMPHALT_MASK) {
3981e774b42bSBill Holler 				reg &= ~(AMD_ACTONCMPHALT_MASK <<
3982e774b42bSBill Holler 				    AMD_ACTONCMPHALT_SHIFT);
3983e774b42bSBill Holler 				wrmsr(MSR_AMD_INT_PENDING_CMP_HALT, reg);
3984e774b42bSBill Holler 			}
3985e774b42bSBill Holler 		}
3986e774b42bSBill Holler 		no_trap();
3987e774b42bSBill Holler 	}
3988e774b42bSBill Holler #endif	/* !__xpv */
3989e774b42bSBill Holler }
3990e774b42bSBill Holler 
3991cef70d2cSBill Holler /*
3992cef70d2cSBill Holler  * Starting with the Westmere processor the local
3993cef70d2cSBill Holler  * APIC timer will continue running in all C-states,
3994cef70d2cSBill Holler  * including the deepest C-states.
3995cef70d2cSBill Holler  */
3996cef70d2cSBill Holler int
3997cef70d2cSBill Holler cpuid_arat_supported(void)
3998cef70d2cSBill Holler {
3999cef70d2cSBill Holler 	struct cpuid_info *cpi;
4000cef70d2cSBill Holler 	struct cpuid_regs regs;
4001cef70d2cSBill Holler 
4002cef70d2cSBill Holler 	ASSERT(cpuid_checkpass(CPU, 1));
4003cef70d2cSBill Holler 	ASSERT(x86_feature & X86_CPUID);
4004cef70d2cSBill Holler 
4005cef70d2cSBill Holler 	cpi = CPU->cpu_m.mcpu_cpi;
4006cef70d2cSBill Holler 
4007cef70d2cSBill Holler 	switch (cpi->cpi_vendor) {
4008cef70d2cSBill Holler 	case X86_VENDOR_Intel:
4009cef70d2cSBill Holler 		/*
4010cef70d2cSBill Holler 		 * Always-running Local APIC Timer is
4011cef70d2cSBill Holler 		 * indicated by CPUID.6.EAX[2].
4012cef70d2cSBill Holler 		 */
4013cef70d2cSBill Holler 		if (cpi->cpi_maxeax >= 6) {
4014cef70d2cSBill Holler 			regs.cp_eax = 6;
4015cef70d2cSBill Holler 			(void) cpuid_insn(NULL, &regs);
4016cef70d2cSBill Holler 			return (regs.cp_eax & CPUID_CSTATE_ARAT);
4017cef70d2cSBill Holler 		} else {
4018cef70d2cSBill Holler 			return (0);
4019cef70d2cSBill Holler 		}
4020cef70d2cSBill Holler 	default:
4021cef70d2cSBill Holler 		return (0);
4022cef70d2cSBill Holler 	}
4023cef70d2cSBill Holler }
4024cef70d2cSBill Holler 
402522cc0e45SBill Holler #if defined(__amd64) && !defined(__xpv)
402622cc0e45SBill Holler /*
402722cc0e45SBill Holler  * Patch in versions of bcopy for high performance Intel Nhm processors
402822cc0e45SBill Holler  * and later...
402922cc0e45SBill Holler  */
403022cc0e45SBill Holler void
403122cc0e45SBill Holler patch_memops(uint_t vendor)
403222cc0e45SBill Holler {
403322cc0e45SBill Holler 	size_t cnt, i;
403422cc0e45SBill Holler 	caddr_t to, from;
403522cc0e45SBill Holler 
403622cc0e45SBill Holler 	if ((vendor == X86_VENDOR_Intel) && ((x86_feature & X86_SSE4_2) != 0)) {
403722cc0e45SBill Holler 		cnt = &bcopy_patch_end - &bcopy_patch_start;
403822cc0e45SBill Holler 		to = &bcopy_ck_size;
403922cc0e45SBill Holler 		from = &bcopy_patch_start;
404022cc0e45SBill Holler 		for (i = 0; i < cnt; i++) {
404122cc0e45SBill Holler 			*to++ = *from++;
404222cc0e45SBill Holler 		}
404322cc0e45SBill Holler 	}
404422cc0e45SBill Holler }
404522cc0e45SBill Holler #endif  /* __amd64 && !__xpv */
4046