xref: /titanic_44/usr/src/uts/i86pc/os/cpuid.c (revision a4a653edf2388c873cbe4f38cde92025d449331f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Various routines to handle identification
30  * and classification of x86 processors.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/archsystm.h>
35 #include <sys/x86_archext.h>
36 #include <sys/kmem.h>
37 #include <sys/systm.h>
38 #include <sys/cmn_err.h>
39 #include <sys/sunddi.h>
40 #include <sys/sunndi.h>
41 #include <sys/cpuvar.h>
42 #include <sys/processor.h>
43 #include <sys/sysmacros.h>
44 #include <sys/pg.h>
45 #include <sys/fp.h>
46 #include <sys/controlregs.h>
47 #include <sys/auxv_386.h>
48 #include <sys/bitmap.h>
49 #include <sys/memnode.h>
50 
51 /*
52  * Pass 0 of cpuid feature analysis happens in locore. It contains special code
53  * to recognize Cyrix processors that are not cpuid-compliant, and to deal with
54  * them accordingly. For most modern processors, feature detection occurs here
55  * in pass 1.
56  *
57  * Pass 1 of cpuid feature analysis happens just at the beginning of mlsetup()
58  * for the boot CPU and does the basic analysis that the early kernel needs.
59  * x86_feature is set based on the return value of cpuid_pass1() of the boot
60  * CPU.
61  *
62  * Pass 1 includes:
63  *
64  *	o Determining vendor/model/family/stepping and setting x86_type and
65  *	  x86_vendor accordingly.
66  *	o Processing the feature flags returned by the cpuid instruction while
67  *	  applying any workarounds or tricks for the specific processor.
68  *	o Mapping the feature flags into Solaris feature bits (X86_*).
69  *	o Processing extended feature flags if supported by the processor,
70  *	  again while applying specific processor knowledge.
71  *	o Determining the CMT characteristics of the system.
72  *
73  * Pass 1 is done on non-boot CPUs during their initialization and the results
74  * are used only as a meager attempt at ensuring that all processors within the
75  * system support the same features.
76  *
77  * Pass 2 of cpuid feature analysis happens just at the beginning
78  * of startup().  It just copies in and corrects the remainder
79  * of the cpuid data we depend on: standard cpuid functions that we didn't
80  * need for pass1 feature analysis, and extended cpuid functions beyond the
81  * simple feature processing done in pass1.
82  *
83  * Pass 3 of cpuid analysis is invoked after basic kernel services; in
84  * particular kernel memory allocation has been made available. It creates a
85  * readable brand string based on the data collected in the first two passes.
86  *
87  * Pass 4 of cpuid analysis is invoked after post_startup() when all
88  * the support infrastructure for various hardware features has been
89  * initialized. It determines which processor features will be reported
90  * to userland via the aux vector.
91  *
92  * All passes are executed on all CPUs, but only the boot CPU determines what
93  * features the kernel will use.
94  *
95  * Much of the worst junk in this file is for the support of processors
96  * that didn't really implement the cpuid instruction properly.
97  *
98  * NOTE: The accessor functions (cpuid_get*) are aware of, and ASSERT upon,
99  * the pass numbers.  Accordingly, changes to the pass code may require changes
100  * to the accessor code.
101  */
102 
103 uint_t x86_feature = 0;
104 uint_t x86_vendor = X86_VENDOR_IntelClone;
105 uint_t x86_type = X86_TYPE_OTHER;
106 
107 uint_t pentiumpro_bug4046376;
108 uint_t pentiumpro_bug4064495;
109 
110 uint_t enable486;
111 
112 /*
113  * This set of strings are for processors rumored to support the cpuid
114  * instruction, and is used by locore.s to figure out how to set x86_vendor
115  */
116 const char CyrixInstead[] = "CyrixInstead";
117 
118 /*
119  * monitor/mwait info.
120  *
121  * size_actual and buf_actual are the real address and size allocated to get
122  * proper mwait_buf alignement.  buf_actual and size_actual should be passed
123  * to kmem_free().  Currently kmem_alloc() and mwait happen to both use
124  * processor cache-line alignment, but this is not guarantied in the furture.
125  */
126 struct mwait_info {
127 	size_t		mon_min;	/* min size to avoid missed wakeups */
128 	size_t		mon_max;	/* size to avoid false wakeups */
129 	size_t		size_actual;	/* size actually allocated */
130 	void		*buf_actual;	/* memory actually allocated */
131 	uint32_t	support;	/* processor support of monitor/mwait */
132 };
133 
134 /*
135  * These constants determine how many of the elements of the
136  * cpuid we cache in the cpuid_info data structure; the
137  * remaining elements are accessible via the cpuid instruction.
138  */
139 
140 #define	NMAX_CPI_STD	6		/* eax = 0 .. 5 */
141 #define	NMAX_CPI_EXTD	9		/* eax = 0x80000000 .. 0x80000008 */
142 
143 struct cpuid_info {
144 	uint_t cpi_pass;		/* last pass completed */
145 	/*
146 	 * standard function information
147 	 */
148 	uint_t cpi_maxeax;		/* fn 0: %eax */
149 	char cpi_vendorstr[13];		/* fn 0: %ebx:%ecx:%edx */
150 	uint_t cpi_vendor;		/* enum of cpi_vendorstr */
151 
152 	uint_t cpi_family;		/* fn 1: extended family */
153 	uint_t cpi_model;		/* fn 1: extended model */
154 	uint_t cpi_step;		/* fn 1: stepping */
155 	chipid_t cpi_chipid;		/* fn 1: %ebx: chip # on ht cpus */
156 	uint_t cpi_brandid;		/* fn 1: %ebx: brand ID */
157 	int cpi_clogid;			/* fn 1: %ebx: thread # */
158 	uint_t cpi_ncpu_per_chip;	/* fn 1: %ebx: logical cpu count */
159 	uint8_t cpi_cacheinfo[16];	/* fn 2: intel-style cache desc */
160 	uint_t cpi_ncache;		/* fn 2: number of elements */
161 	uint_t cpi_ncpu_shr_last_cache;	/* fn 4: %eax: ncpus sharing cache */
162 	id_t cpi_last_lvl_cacheid;	/* fn 4: %eax: derived cache id */
163 	uint_t cpi_std_4_size;		/* fn 4: number of fn 4 elements */
164 	struct cpuid_regs **cpi_std_4;	/* fn 4: %ecx == 0 .. fn4_size */
165 	struct cpuid_regs cpi_std[NMAX_CPI_STD];	/* 0 .. 5 */
166 	/*
167 	 * extended function information
168 	 */
169 	uint_t cpi_xmaxeax;		/* fn 0x80000000: %eax */
170 	char cpi_brandstr[49];		/* fn 0x8000000[234] */
171 	uint8_t cpi_pabits;		/* fn 0x80000006: %eax */
172 	uint8_t cpi_vabits;		/* fn 0x80000006: %eax */
173 	struct cpuid_regs cpi_extd[NMAX_CPI_EXTD]; /* 0x8000000[0-8] */
174 	id_t cpi_coreid;		/* same coreid => strands share core */
175 	int cpi_pkgcoreid;		/* core number within single package */
176 	uint_t cpi_ncore_per_chip;	/* AMD: fn 0x80000008: %ecx[7-0] */
177 					/* Intel: fn 4: %eax[31-26] */
178 	/*
179 	 * supported feature information
180 	 */
181 	uint32_t cpi_support[5];
182 #define	STD_EDX_FEATURES	0
183 #define	AMD_EDX_FEATURES	1
184 #define	TM_EDX_FEATURES		2
185 #define	STD_ECX_FEATURES	3
186 #define	AMD_ECX_FEATURES	4
187 	/*
188 	 * Synthesized information, where known.
189 	 */
190 	uint32_t cpi_chiprev;		/* See X86_CHIPREV_* in x86_archext.h */
191 	const char *cpi_chiprevstr;	/* May be NULL if chiprev unknown */
192 	uint32_t cpi_socket;		/* Chip package/socket type */
193 
194 	struct mwait_info cpi_mwait;	/* fn 5: monitor/mwait info */
195 };
196 
197 
198 static struct cpuid_info cpuid_info0;
199 
200 /*
201  * These bit fields are defined by the Intel Application Note AP-485
202  * "Intel Processor Identification and the CPUID Instruction"
203  */
204 #define	CPI_FAMILY_XTD(cpi)	BITX((cpi)->cpi_std[1].cp_eax, 27, 20)
205 #define	CPI_MODEL_XTD(cpi)	BITX((cpi)->cpi_std[1].cp_eax, 19, 16)
206 #define	CPI_TYPE(cpi)		BITX((cpi)->cpi_std[1].cp_eax, 13, 12)
207 #define	CPI_FAMILY(cpi)		BITX((cpi)->cpi_std[1].cp_eax, 11, 8)
208 #define	CPI_STEP(cpi)		BITX((cpi)->cpi_std[1].cp_eax, 3, 0)
209 #define	CPI_MODEL(cpi)		BITX((cpi)->cpi_std[1].cp_eax, 7, 4)
210 
211 #define	CPI_FEATURES_EDX(cpi)		((cpi)->cpi_std[1].cp_edx)
212 #define	CPI_FEATURES_ECX(cpi)		((cpi)->cpi_std[1].cp_ecx)
213 #define	CPI_FEATURES_XTD_EDX(cpi)	((cpi)->cpi_extd[1].cp_edx)
214 #define	CPI_FEATURES_XTD_ECX(cpi)	((cpi)->cpi_extd[1].cp_ecx)
215 
216 #define	CPI_BRANDID(cpi)	BITX((cpi)->cpi_std[1].cp_ebx, 7, 0)
217 #define	CPI_CHUNKS(cpi)		BITX((cpi)->cpi_std[1].cp_ebx, 15, 7)
218 #define	CPI_CPU_COUNT(cpi)	BITX((cpi)->cpi_std[1].cp_ebx, 23, 16)
219 #define	CPI_APIC_ID(cpi)	BITX((cpi)->cpi_std[1].cp_ebx, 31, 24)
220 
221 #define	CPI_MAXEAX_MAX		0x100		/* sanity control */
222 #define	CPI_XMAXEAX_MAX		0x80000100
223 #define	CPI_FN4_ECX_MAX		0x20		/* sanity: max fn 4 levels */
224 
225 /*
226  * Function 4 (Deterministic Cache Parameters) macros
227  * Defined by Intel Application Note AP-485
228  */
229 #define	CPI_NUM_CORES(regs)		BITX((regs)->cp_eax, 31, 26)
230 #define	CPI_NTHR_SHR_CACHE(regs)	BITX((regs)->cp_eax, 25, 14)
231 #define	CPI_FULL_ASSOC_CACHE(regs)	BITX((regs)->cp_eax, 9, 9)
232 #define	CPI_SELF_INIT_CACHE(regs)	BITX((regs)->cp_eax, 8, 8)
233 #define	CPI_CACHE_LVL(regs)		BITX((regs)->cp_eax, 7, 5)
234 #define	CPI_CACHE_TYPE(regs)		BITX((regs)->cp_eax, 4, 0)
235 
236 #define	CPI_CACHE_WAYS(regs)		BITX((regs)->cp_ebx, 31, 22)
237 #define	CPI_CACHE_PARTS(regs)		BITX((regs)->cp_ebx, 21, 12)
238 #define	CPI_CACHE_COH_LN_SZ(regs)	BITX((regs)->cp_ebx, 11, 0)
239 
240 #define	CPI_CACHE_SETS(regs)		BITX((regs)->cp_ecx, 31, 0)
241 
242 #define	CPI_PREFCH_STRIDE(regs)		BITX((regs)->cp_edx, 9, 0)
243 
244 
245 /*
246  * A couple of shorthand macros to identify "later" P6-family chips
247  * like the Pentium M and Core.  First, the "older" P6-based stuff
248  * (loosely defined as "pre-Pentium-4"):
249  * P6, PII, Mobile PII, PII Xeon, PIII, Mobile PIII, PIII Xeon
250  */
251 
252 #define	IS_LEGACY_P6(cpi) (			\
253 	cpi->cpi_family == 6 && 		\
254 		(cpi->cpi_model == 1 ||		\
255 		cpi->cpi_model == 3 ||		\
256 		cpi->cpi_model == 5 ||		\
257 		cpi->cpi_model == 6 ||		\
258 		cpi->cpi_model == 7 ||		\
259 		cpi->cpi_model == 8 ||		\
260 		cpi->cpi_model == 0xA ||	\
261 		cpi->cpi_model == 0xB)		\
262 )
263 
264 /* A "new F6" is everything with family 6 that's not the above */
265 #define	IS_NEW_F6(cpi) ((cpi->cpi_family == 6) && !IS_LEGACY_P6(cpi))
266 
267 /* Extended family/model support */
268 #define	IS_EXTENDED_MODEL_INTEL(cpi) (cpi->cpi_family == 0x6 || \
269 	cpi->cpi_family >= 0xf)
270 
271 /*
272  * AMD family 0xf and family 0x10 socket types.
273  * First index :
274  *		0 for family 0xf, revs B thru E
275  *		1 for family 0xf, revs F and G
276  *		2 for family 0x10, rev B
277  * Second index by (model & 0x3)
278  */
279 static uint32_t amd_skts[3][4] = {
280 	/*
281 	 * Family 0xf revisions B through E
282 	 */
283 #define	A_SKTS_0			0
284 	{
285 		X86_SOCKET_754,		/* 0b00 */
286 		X86_SOCKET_940,		/* 0b01 */
287 		X86_SOCKET_754,		/* 0b10 */
288 		X86_SOCKET_939		/* 0b11 */
289 	},
290 	/*
291 	 * Family 0xf revisions F and G
292 	 */
293 #define	A_SKTS_1			1
294 	{
295 		X86_SOCKET_S1g1,	/* 0b00 */
296 		X86_SOCKET_F1207,	/* 0b01 */
297 		X86_SOCKET_UNKNOWN,	/* 0b10 */
298 		X86_SOCKET_AM2		/* 0b11 */
299 	},
300 	/*
301 	 * Family 0x10 revisions A and B
302 	 * It is not clear whether, as new sockets release, that
303 	 * model & 0x3 will id socket for this family
304 	 */
305 #define	A_SKTS_2			2
306 	{
307 		X86_SOCKET_F1207,	/* 0b00 */
308 		X86_SOCKET_F1207,	/* 0b01 */
309 		X86_SOCKET_F1207,	/* 0b10 */
310 		X86_SOCKET_F1207,	/* 0b11 */
311 	}
312 };
313 
314 /*
315  * Table for mapping AMD Family 0xf and AMD Family 0x10 model/stepping
316  * combination to chip "revision" and socket type.
317  *
318  * The first member of this array that matches a given family, extended model
319  * plus model range, and stepping range will be considered a match.
320  */
321 static const struct amd_rev_mapent {
322 	uint_t rm_family;
323 	uint_t rm_modello;
324 	uint_t rm_modelhi;
325 	uint_t rm_steplo;
326 	uint_t rm_stephi;
327 	uint32_t rm_chiprev;
328 	const char *rm_chiprevstr;
329 	int rm_sktidx;
330 } amd_revmap[] = {
331 	/*
332 	 * =============== AuthenticAMD Family 0xf ===============
333 	 */
334 
335 	/*
336 	 * Rev B includes model 0x4 stepping 0 and model 0x5 stepping 0 and 1.
337 	 */
338 	{ 0xf, 0x04, 0x04, 0x0, 0x0, X86_CHIPREV_AMD_F_REV_B, "B", A_SKTS_0 },
339 	{ 0xf, 0x05, 0x05, 0x0, 0x1, X86_CHIPREV_AMD_F_REV_B, "B", A_SKTS_0 },
340 	/*
341 	 * Rev C0 includes model 0x4 stepping 8 and model 0x5 stepping 8
342 	 */
343 	{ 0xf, 0x04, 0x05, 0x8, 0x8, X86_CHIPREV_AMD_F_REV_C0, "C0", A_SKTS_0 },
344 	/*
345 	 * Rev CG is the rest of extended model 0x0 - i.e., everything
346 	 * but the rev B and C0 combinations covered above.
347 	 */
348 	{ 0xf, 0x00, 0x0f, 0x0, 0xf, X86_CHIPREV_AMD_F_REV_CG, "CG", A_SKTS_0 },
349 	/*
350 	 * Rev D has extended model 0x1.
351 	 */
352 	{ 0xf, 0x10, 0x1f, 0x0, 0xf, X86_CHIPREV_AMD_F_REV_D, "D", A_SKTS_0 },
353 	/*
354 	 * Rev E has extended model 0x2.
355 	 * Extended model 0x3 is unused but available to grow into.
356 	 */
357 	{ 0xf, 0x20, 0x3f, 0x0, 0xf, X86_CHIPREV_AMD_F_REV_E, "E", A_SKTS_0 },
358 	/*
359 	 * Rev F has extended models 0x4 and 0x5.
360 	 */
361 	{ 0xf, 0x40, 0x5f, 0x0, 0xf, X86_CHIPREV_AMD_F_REV_F, "F", A_SKTS_1 },
362 	/*
363 	 * Rev G has extended model 0x6.
364 	 */
365 	{ 0xf, 0x60, 0x6f, 0x0, 0xf, X86_CHIPREV_AMD_F_REV_G, "G", A_SKTS_1 },
366 
367 	/*
368 	 * =============== AuthenticAMD Family 0x10 ===============
369 	 */
370 
371 	/*
372 	 * Rev A has model 0 and stepping 0/1/2 for DR-{A0,A1,A2}.
373 	 * Give all of model 0 stepping range to rev A.
374 	 */
375 	{ 0x10, 0x00, 0x00, 0x0, 0x2, X86_CHIPREV_AMD_10_REV_A, "A", A_SKTS_2 },
376 
377 	/*
378 	 * Rev B has model 2 and steppings 0/1/0xa/2 for DR-{B0,B1,BA,B2}.
379 	 * Give all of model 2 stepping range to rev B.
380 	 */
381 	{ 0x10, 0x02, 0x02, 0x0, 0xf, X86_CHIPREV_AMD_10_REV_B, "B", A_SKTS_2 },
382 };
383 
384 /*
385  * Info for monitor/mwait idle loop.
386  *
387  * See cpuid section of "Intel 64 and IA-32 Architectures Software Developer's
388  * Manual Volume 2A: Instruction Set Reference, A-M" #25366-022US, November
389  * 2006.
390  * See MONITOR/MWAIT section of "AMD64 Architecture Programmer's Manual
391  * Documentation Updates" #33633, Rev 2.05, December 2006.
392  */
393 #define	MWAIT_SUPPORT		(0x00000001)	/* mwait supported */
394 #define	MWAIT_EXTENSIONS	(0x00000002)	/* extenstion supported */
395 #define	MWAIT_ECX_INT_ENABLE	(0x00000004)	/* ecx 1 extension supported */
396 #define	MWAIT_SUPPORTED(cpi)	((cpi)->cpi_std[1].cp_ecx & CPUID_INTC_ECX_MON)
397 #define	MWAIT_INT_ENABLE(cpi)	((cpi)->cpi_std[5].cp_ecx & 0x2)
398 #define	MWAIT_EXTENSION(cpi)	((cpi)->cpi_std[5].cp_ecx & 0x1)
399 #define	MWAIT_SIZE_MIN(cpi)	BITX((cpi)->cpi_std[5].cp_eax, 15, 0)
400 #define	MWAIT_SIZE_MAX(cpi)	BITX((cpi)->cpi_std[5].cp_ebx, 15, 0)
401 /*
402  * Number of sub-cstates for a given c-state.
403  */
404 #define	MWAIT_NUM_SUBC_STATES(cpi, c_state)			\
405 	BITX((cpi)->cpi_std[5].cp_edx, c_state + 3, c_state)
406 
407 static void
408 synth_amd_info(struct cpuid_info *cpi)
409 {
410 	const struct amd_rev_mapent *rmp;
411 	uint_t family, model, step;
412 	int i;
413 
414 	/*
415 	 * Currently only AMD family 0xf and family 0x10 use these fields.
416 	 */
417 	if (cpi->cpi_family != 0xf && cpi->cpi_family != 0x10)
418 		return;
419 
420 	family = cpi->cpi_family;
421 	model = cpi->cpi_model;
422 	step = cpi->cpi_step;
423 
424 	for (i = 0, rmp = amd_revmap; i < sizeof (amd_revmap) / sizeof (*rmp);
425 	    i++, rmp++) {
426 		if (family == rmp->rm_family &&
427 		    model >= rmp->rm_modello && model <= rmp->rm_modelhi &&
428 		    step >= rmp->rm_steplo && step <= rmp->rm_stephi) {
429 			cpi->cpi_chiprev = rmp->rm_chiprev;
430 			cpi->cpi_chiprevstr = rmp->rm_chiprevstr;
431 			cpi->cpi_socket = amd_skts[rmp->rm_sktidx][model & 0x3];
432 			return;
433 		}
434 	}
435 }
436 
437 static void
438 synth_info(struct cpuid_info *cpi)
439 {
440 	cpi->cpi_chiprev = X86_CHIPREV_UNKNOWN;
441 	cpi->cpi_chiprevstr = "Unknown";
442 	cpi->cpi_socket = X86_SOCKET_UNKNOWN;
443 
444 	switch (cpi->cpi_vendor) {
445 	case X86_VENDOR_AMD:
446 		synth_amd_info(cpi);
447 		break;
448 
449 	default:
450 		break;
451 
452 	}
453 }
454 
455 /*
456  * Apply up various platform-dependent restrictions where the
457  * underlying platform restrictions mean the CPU can be marked
458  * as less capable than its cpuid instruction would imply.
459  */
460 #if defined(__xpv)
461 static void
462 platform_cpuid_mangle(uint_t vendor, uint32_t eax, struct cpuid_regs *cp)
463 {
464 	switch (eax) {
465 	case 1:
466 		cp->cp_edx &=
467 		    ~(CPUID_INTC_EDX_PSE |
468 		    CPUID_INTC_EDX_VME | CPUID_INTC_EDX_DE |
469 		    CPUID_INTC_EDX_MCA |	/* XXPV true on dom0? */
470 		    CPUID_INTC_EDX_SEP | CPUID_INTC_EDX_MTRR |
471 		    CPUID_INTC_EDX_PGE | CPUID_INTC_EDX_PAT |
472 		    CPUID_AMD_EDX_SYSC | CPUID_INTC_EDX_SEP |
473 		    CPUID_INTC_EDX_PSE36 | CPUID_INTC_EDX_HTT);
474 		break;
475 
476 	case 0x80000001:
477 		cp->cp_edx &=
478 		    ~(CPUID_AMD_EDX_PSE |
479 		    CPUID_INTC_EDX_VME | CPUID_INTC_EDX_DE |
480 		    CPUID_AMD_EDX_MTRR | CPUID_AMD_EDX_PGE |
481 		    CPUID_AMD_EDX_PAT | CPUID_AMD_EDX_PSE36 |
482 		    CPUID_AMD_EDX_SYSC | CPUID_INTC_EDX_SEP |
483 		    CPUID_AMD_EDX_TSCP);
484 		cp->cp_ecx &= ~CPUID_AMD_ECX_CMP_LGCY;
485 		break;
486 	default:
487 		break;
488 	}
489 
490 	switch (vendor) {
491 	case X86_VENDOR_Intel:
492 		switch (eax) {
493 		case 4:
494 			/*
495 			 * Zero out the (ncores-per-chip - 1) field
496 			 */
497 			cp->cp_eax &= 0x03fffffff;
498 			break;
499 		default:
500 			break;
501 		}
502 		break;
503 	case X86_VENDOR_AMD:
504 		switch (eax) {
505 		case 0x80000008:
506 			/*
507 			 * Zero out the (ncores-per-chip - 1) field
508 			 */
509 			cp->cp_ecx &= 0xffffff00;
510 			break;
511 		default:
512 			break;
513 		}
514 		break;
515 	default:
516 		break;
517 	}
518 }
519 #else
520 #define	platform_cpuid_mangle(vendor, eax, cp)	/* nothing */
521 #endif
522 
523 /*
524  *  Some undocumented ways of patching the results of the cpuid
525  *  instruction to permit running Solaris 10 on future cpus that
526  *  we don't currently support.  Could be set to non-zero values
527  *  via settings in eeprom.
528  */
529 
530 uint32_t cpuid_feature_ecx_include;
531 uint32_t cpuid_feature_ecx_exclude;
532 uint32_t cpuid_feature_edx_include;
533 uint32_t cpuid_feature_edx_exclude;
534 
535 void
536 cpuid_alloc_space(cpu_t *cpu)
537 {
538 	/*
539 	 * By convention, cpu0 is the boot cpu, which is set up
540 	 * before memory allocation is available.  All other cpus get
541 	 * their cpuid_info struct allocated here.
542 	 */
543 	ASSERT(cpu->cpu_id != 0);
544 	cpu->cpu_m.mcpu_cpi =
545 	    kmem_zalloc(sizeof (*cpu->cpu_m.mcpu_cpi), KM_SLEEP);
546 }
547 
548 void
549 cpuid_free_space(cpu_t *cpu)
550 {
551 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
552 	int i;
553 
554 	ASSERT(cpu->cpu_id != 0);
555 
556 	/*
557 	 * Free up any function 4 related dynamic storage
558 	 */
559 	for (i = 1; i < cpi->cpi_std_4_size; i++)
560 		kmem_free(cpi->cpi_std_4[i], sizeof (struct cpuid_regs));
561 	if (cpi->cpi_std_4_size > 0)
562 		kmem_free(cpi->cpi_std_4,
563 		    cpi->cpi_std_4_size * sizeof (struct cpuid_regs *));
564 
565 	kmem_free(cpu->cpu_m.mcpu_cpi, sizeof (*cpu->cpu_m.mcpu_cpi));
566 }
567 
568 #if !defined(__xpv)
569 
570 static void
571 check_for_hvm()
572 {
573 	struct cpuid_regs cp;
574 	char *xen_str;
575 	uint32_t xen_signature[4];
576 	extern int xpv_is_hvm;
577 
578 	/*
579 	 * In a fully virtualized domain, Xen's pseudo-cpuid function
580 	 * 0x40000000 returns a string representing the Xen signature in
581 	 * %ebx, %ecx, and %edx.  %eax contains the maximum supported cpuid
582 	 * function.
583 	 */
584 	cp.cp_eax = 0x40000000;
585 	(void) __cpuid_insn(&cp);
586 	xen_signature[0] = cp.cp_ebx;
587 	xen_signature[1] = cp.cp_ecx;
588 	xen_signature[2] = cp.cp_edx;
589 	xen_signature[3] = 0;
590 	xen_str = (char *)xen_signature;
591 	if (strcmp("XenVMMXenVMM", xen_str) == 0 && cp.cp_eax <= 0x40000002)
592 		xpv_is_hvm = 1;
593 }
594 #endif	/* __xpv */
595 
596 uint_t
597 cpuid_pass1(cpu_t *cpu)
598 {
599 	uint32_t mask_ecx, mask_edx;
600 	uint_t feature = X86_CPUID;
601 	struct cpuid_info *cpi;
602 	struct cpuid_regs *cp;
603 	int xcpuid;
604 #if !defined(__xpv)
605 	extern int idle_cpu_prefer_mwait;
606 #endif
607 
608 	/*
609 	 * Space statically allocated for cpu0, ensure pointer is set
610 	 */
611 	if (cpu->cpu_id == 0)
612 		cpu->cpu_m.mcpu_cpi = &cpuid_info0;
613 	cpi = cpu->cpu_m.mcpu_cpi;
614 	ASSERT(cpi != NULL);
615 	cp = &cpi->cpi_std[0];
616 	cp->cp_eax = 0;
617 	cpi->cpi_maxeax = __cpuid_insn(cp);
618 	{
619 		uint32_t *iptr = (uint32_t *)cpi->cpi_vendorstr;
620 		*iptr++ = cp->cp_ebx;
621 		*iptr++ = cp->cp_edx;
622 		*iptr++ = cp->cp_ecx;
623 		*(char *)&cpi->cpi_vendorstr[12] = '\0';
624 	}
625 
626 	/*
627 	 * Map the vendor string to a type code
628 	 */
629 	if (strcmp(cpi->cpi_vendorstr, "GenuineIntel") == 0)
630 		cpi->cpi_vendor = X86_VENDOR_Intel;
631 	else if (strcmp(cpi->cpi_vendorstr, "AuthenticAMD") == 0)
632 		cpi->cpi_vendor = X86_VENDOR_AMD;
633 	else if (strcmp(cpi->cpi_vendorstr, "GenuineTMx86") == 0)
634 		cpi->cpi_vendor = X86_VENDOR_TM;
635 	else if (strcmp(cpi->cpi_vendorstr, CyrixInstead) == 0)
636 		/*
637 		 * CyrixInstead is a variable used by the Cyrix detection code
638 		 * in locore.
639 		 */
640 		cpi->cpi_vendor = X86_VENDOR_Cyrix;
641 	else if (strcmp(cpi->cpi_vendorstr, "UMC UMC UMC ") == 0)
642 		cpi->cpi_vendor = X86_VENDOR_UMC;
643 	else if (strcmp(cpi->cpi_vendorstr, "NexGenDriven") == 0)
644 		cpi->cpi_vendor = X86_VENDOR_NexGen;
645 	else if (strcmp(cpi->cpi_vendorstr, "CentaurHauls") == 0)
646 		cpi->cpi_vendor = X86_VENDOR_Centaur;
647 	else if (strcmp(cpi->cpi_vendorstr, "RiseRiseRise") == 0)
648 		cpi->cpi_vendor = X86_VENDOR_Rise;
649 	else if (strcmp(cpi->cpi_vendorstr, "SiS SiS SiS ") == 0)
650 		cpi->cpi_vendor = X86_VENDOR_SiS;
651 	else if (strcmp(cpi->cpi_vendorstr, "Geode by NSC") == 0)
652 		cpi->cpi_vendor = X86_VENDOR_NSC;
653 	else
654 		cpi->cpi_vendor = X86_VENDOR_IntelClone;
655 
656 	x86_vendor = cpi->cpi_vendor; /* for compatibility */
657 
658 	/*
659 	 * Limit the range in case of weird hardware
660 	 */
661 	if (cpi->cpi_maxeax > CPI_MAXEAX_MAX)
662 		cpi->cpi_maxeax = CPI_MAXEAX_MAX;
663 	if (cpi->cpi_maxeax < 1)
664 		goto pass1_done;
665 
666 	cp = &cpi->cpi_std[1];
667 	cp->cp_eax = 1;
668 	(void) __cpuid_insn(cp);
669 
670 	/*
671 	 * Extract identifying constants for easy access.
672 	 */
673 	cpi->cpi_model = CPI_MODEL(cpi);
674 	cpi->cpi_family = CPI_FAMILY(cpi);
675 
676 	if (cpi->cpi_family == 0xf)
677 		cpi->cpi_family += CPI_FAMILY_XTD(cpi);
678 
679 	/*
680 	 * Beware: AMD uses "extended model" iff base *FAMILY* == 0xf.
681 	 * Intel, and presumably everyone else, uses model == 0xf, as
682 	 * one would expect (max value means possible overflow).  Sigh.
683 	 */
684 
685 	switch (cpi->cpi_vendor) {
686 	case X86_VENDOR_Intel:
687 		if (IS_EXTENDED_MODEL_INTEL(cpi))
688 			cpi->cpi_model += CPI_MODEL_XTD(cpi) << 4;
689 		break;
690 	case X86_VENDOR_AMD:
691 		if (CPI_FAMILY(cpi) == 0xf)
692 			cpi->cpi_model += CPI_MODEL_XTD(cpi) << 4;
693 		break;
694 	default:
695 		if (cpi->cpi_model == 0xf)
696 			cpi->cpi_model += CPI_MODEL_XTD(cpi) << 4;
697 		break;
698 	}
699 
700 	cpi->cpi_step = CPI_STEP(cpi);
701 	cpi->cpi_brandid = CPI_BRANDID(cpi);
702 
703 	/*
704 	 * *default* assumptions:
705 	 * - believe %edx feature word
706 	 * - ignore %ecx feature word
707 	 * - 32-bit virtual and physical addressing
708 	 */
709 	mask_edx = 0xffffffff;
710 	mask_ecx = 0;
711 
712 	cpi->cpi_pabits = cpi->cpi_vabits = 32;
713 
714 	switch (cpi->cpi_vendor) {
715 	case X86_VENDOR_Intel:
716 		if (cpi->cpi_family == 5)
717 			x86_type = X86_TYPE_P5;
718 		else if (IS_LEGACY_P6(cpi)) {
719 			x86_type = X86_TYPE_P6;
720 			pentiumpro_bug4046376 = 1;
721 			pentiumpro_bug4064495 = 1;
722 			/*
723 			 * Clear the SEP bit when it was set erroneously
724 			 */
725 			if (cpi->cpi_model < 3 && cpi->cpi_step < 3)
726 				cp->cp_edx &= ~CPUID_INTC_EDX_SEP;
727 		} else if (IS_NEW_F6(cpi) || cpi->cpi_family == 0xf) {
728 			x86_type = X86_TYPE_P4;
729 			/*
730 			 * We don't currently depend on any of the %ecx
731 			 * features until Prescott, so we'll only check
732 			 * this from P4 onwards.  We might want to revisit
733 			 * that idea later.
734 			 */
735 			mask_ecx = 0xffffffff;
736 		} else if (cpi->cpi_family > 0xf)
737 			mask_ecx = 0xffffffff;
738 		/*
739 		 * We don't support MONITOR/MWAIT if leaf 5 is not available
740 		 * to obtain the monitor linesize.
741 		 */
742 		if (cpi->cpi_maxeax < 5)
743 			mask_ecx &= ~CPUID_INTC_ECX_MON;
744 		break;
745 	case X86_VENDOR_IntelClone:
746 	default:
747 		break;
748 	case X86_VENDOR_AMD:
749 #if defined(OPTERON_ERRATUM_108)
750 		if (cpi->cpi_family == 0xf && cpi->cpi_model == 0xe) {
751 			cp->cp_eax = (0xf0f & cp->cp_eax) | 0xc0;
752 			cpi->cpi_model = 0xc;
753 		} else
754 #endif
755 		if (cpi->cpi_family == 5) {
756 			/*
757 			 * AMD K5 and K6
758 			 *
759 			 * These CPUs have an incomplete implementation
760 			 * of MCA/MCE which we mask away.
761 			 */
762 			mask_edx &= ~(CPUID_INTC_EDX_MCE | CPUID_INTC_EDX_MCA);
763 
764 			/*
765 			 * Model 0 uses the wrong (APIC) bit
766 			 * to indicate PGE.  Fix it here.
767 			 */
768 			if (cpi->cpi_model == 0) {
769 				if (cp->cp_edx & 0x200) {
770 					cp->cp_edx &= ~0x200;
771 					cp->cp_edx |= CPUID_INTC_EDX_PGE;
772 				}
773 			}
774 
775 			/*
776 			 * Early models had problems w/ MMX; disable.
777 			 */
778 			if (cpi->cpi_model < 6)
779 				mask_edx &= ~CPUID_INTC_EDX_MMX;
780 		}
781 
782 		/*
783 		 * For newer families, SSE3 and CX16, at least, are valid;
784 		 * enable all
785 		 */
786 		if (cpi->cpi_family >= 0xf)
787 			mask_ecx = 0xffffffff;
788 		/*
789 		 * We don't support MONITOR/MWAIT if leaf 5 is not available
790 		 * to obtain the monitor linesize.
791 		 */
792 		if (cpi->cpi_maxeax < 5)
793 			mask_ecx &= ~CPUID_INTC_ECX_MON;
794 
795 #if !defined(__xpv)
796 		/*
797 		 * Do not use MONITOR/MWAIT to halt in the idle loop on any AMD
798 		 * processors.  AMD does not intend MWAIT to be used in the cpu
799 		 * idle loop on current and future processors.  10h and future
800 		 * AMD processors use more power in MWAIT than HLT.
801 		 * Pre-family-10h Opterons do not have the MWAIT instruction.
802 		 */
803 		idle_cpu_prefer_mwait = 0;
804 #endif
805 
806 		break;
807 	case X86_VENDOR_TM:
808 		/*
809 		 * workaround the NT workaround in CMS 4.1
810 		 */
811 		if (cpi->cpi_family == 5 && cpi->cpi_model == 4 &&
812 		    (cpi->cpi_step == 2 || cpi->cpi_step == 3))
813 			cp->cp_edx |= CPUID_INTC_EDX_CX8;
814 		break;
815 	case X86_VENDOR_Centaur:
816 		/*
817 		 * workaround the NT workarounds again
818 		 */
819 		if (cpi->cpi_family == 6)
820 			cp->cp_edx |= CPUID_INTC_EDX_CX8;
821 		break;
822 	case X86_VENDOR_Cyrix:
823 		/*
824 		 * We rely heavily on the probing in locore
825 		 * to actually figure out what parts, if any,
826 		 * of the Cyrix cpuid instruction to believe.
827 		 */
828 		switch (x86_type) {
829 		case X86_TYPE_CYRIX_486:
830 			mask_edx = 0;
831 			break;
832 		case X86_TYPE_CYRIX_6x86:
833 			mask_edx = 0;
834 			break;
835 		case X86_TYPE_CYRIX_6x86L:
836 			mask_edx =
837 			    CPUID_INTC_EDX_DE |
838 			    CPUID_INTC_EDX_CX8;
839 			break;
840 		case X86_TYPE_CYRIX_6x86MX:
841 			mask_edx =
842 			    CPUID_INTC_EDX_DE |
843 			    CPUID_INTC_EDX_MSR |
844 			    CPUID_INTC_EDX_CX8 |
845 			    CPUID_INTC_EDX_PGE |
846 			    CPUID_INTC_EDX_CMOV |
847 			    CPUID_INTC_EDX_MMX;
848 			break;
849 		case X86_TYPE_CYRIX_GXm:
850 			mask_edx =
851 			    CPUID_INTC_EDX_MSR |
852 			    CPUID_INTC_EDX_CX8 |
853 			    CPUID_INTC_EDX_CMOV |
854 			    CPUID_INTC_EDX_MMX;
855 			break;
856 		case X86_TYPE_CYRIX_MediaGX:
857 			break;
858 		case X86_TYPE_CYRIX_MII:
859 		case X86_TYPE_VIA_CYRIX_III:
860 			mask_edx =
861 			    CPUID_INTC_EDX_DE |
862 			    CPUID_INTC_EDX_TSC |
863 			    CPUID_INTC_EDX_MSR |
864 			    CPUID_INTC_EDX_CX8 |
865 			    CPUID_INTC_EDX_PGE |
866 			    CPUID_INTC_EDX_CMOV |
867 			    CPUID_INTC_EDX_MMX;
868 			break;
869 		default:
870 			break;
871 		}
872 		break;
873 	}
874 
875 #if defined(__xpv)
876 	/*
877 	 * Do not support MONITOR/MWAIT under a hypervisor
878 	 */
879 	mask_ecx &= ~CPUID_INTC_ECX_MON;
880 #endif	/* __xpv */
881 
882 	/*
883 	 * Now we've figured out the masks that determine
884 	 * which bits we choose to believe, apply the masks
885 	 * to the feature words, then map the kernel's view
886 	 * of these feature words into its feature word.
887 	 */
888 	cp->cp_edx &= mask_edx;
889 	cp->cp_ecx &= mask_ecx;
890 
891 	/*
892 	 * apply any platform restrictions (we don't call this
893 	 * immediately after __cpuid_insn here, because we need the
894 	 * workarounds applied above first)
895 	 */
896 	platform_cpuid_mangle(cpi->cpi_vendor, 1, cp);
897 
898 	/*
899 	 * fold in overrides from the "eeprom" mechanism
900 	 */
901 	cp->cp_edx |= cpuid_feature_edx_include;
902 	cp->cp_edx &= ~cpuid_feature_edx_exclude;
903 
904 	cp->cp_ecx |= cpuid_feature_ecx_include;
905 	cp->cp_ecx &= ~cpuid_feature_ecx_exclude;
906 
907 	if (cp->cp_edx & CPUID_INTC_EDX_PSE)
908 		feature |= X86_LARGEPAGE;
909 	if (cp->cp_edx & CPUID_INTC_EDX_TSC)
910 		feature |= X86_TSC;
911 	if (cp->cp_edx & CPUID_INTC_EDX_MSR)
912 		feature |= X86_MSR;
913 	if (cp->cp_edx & CPUID_INTC_EDX_MTRR)
914 		feature |= X86_MTRR;
915 	if (cp->cp_edx & CPUID_INTC_EDX_PGE)
916 		feature |= X86_PGE;
917 	if (cp->cp_edx & CPUID_INTC_EDX_CMOV)
918 		feature |= X86_CMOV;
919 	if (cp->cp_edx & CPUID_INTC_EDX_MMX)
920 		feature |= X86_MMX;
921 	if ((cp->cp_edx & CPUID_INTC_EDX_MCE) != 0 &&
922 	    (cp->cp_edx & CPUID_INTC_EDX_MCA) != 0)
923 		feature |= X86_MCA;
924 	if (cp->cp_edx & CPUID_INTC_EDX_PAE)
925 		feature |= X86_PAE;
926 	if (cp->cp_edx & CPUID_INTC_EDX_CX8)
927 		feature |= X86_CX8;
928 	if (cp->cp_ecx & CPUID_INTC_ECX_CX16)
929 		feature |= X86_CX16;
930 	if (cp->cp_edx & CPUID_INTC_EDX_PAT)
931 		feature |= X86_PAT;
932 	if (cp->cp_edx & CPUID_INTC_EDX_SEP)
933 		feature |= X86_SEP;
934 	if (cp->cp_edx & CPUID_INTC_EDX_FXSR) {
935 		/*
936 		 * In our implementation, fxsave/fxrstor
937 		 * are prerequisites before we'll even
938 		 * try and do SSE things.
939 		 */
940 		if (cp->cp_edx & CPUID_INTC_EDX_SSE)
941 			feature |= X86_SSE;
942 		if (cp->cp_edx & CPUID_INTC_EDX_SSE2)
943 			feature |= X86_SSE2;
944 		if (cp->cp_ecx & CPUID_INTC_ECX_SSE3)
945 			feature |= X86_SSE3;
946 		if (cpi->cpi_vendor == X86_VENDOR_Intel) {
947 			if (cp->cp_ecx & CPUID_INTC_ECX_SSSE3)
948 				feature |= X86_SSSE3;
949 			if (cp->cp_ecx & CPUID_INTC_ECX_SSE4_1)
950 				feature |= X86_SSE4_1;
951 			if (cp->cp_ecx & CPUID_INTC_ECX_SSE4_2)
952 				feature |= X86_SSE4_2;
953 		}
954 	}
955 	if (cp->cp_edx & CPUID_INTC_EDX_DE)
956 		feature |= X86_DE;
957 	if (cp->cp_ecx & CPUID_INTC_ECX_MON) {
958 		cpi->cpi_mwait.support |= MWAIT_SUPPORT;
959 		feature |= X86_MWAIT;
960 	}
961 
962 	if (feature & X86_PAE)
963 		cpi->cpi_pabits = 36;
964 
965 	/*
966 	 * Hyperthreading configuration is slightly tricky on Intel
967 	 * and pure clones, and even trickier on AMD.
968 	 *
969 	 * (AMD chose to set the HTT bit on their CMP processors,
970 	 * even though they're not actually hyperthreaded.  Thus it
971 	 * takes a bit more work to figure out what's really going
972 	 * on ... see the handling of the CMP_LGCY bit below)
973 	 */
974 	if (cp->cp_edx & CPUID_INTC_EDX_HTT) {
975 		cpi->cpi_ncpu_per_chip = CPI_CPU_COUNT(cpi);
976 		if (cpi->cpi_ncpu_per_chip > 1)
977 			feature |= X86_HTT;
978 	} else {
979 		cpi->cpi_ncpu_per_chip = 1;
980 	}
981 
982 	/*
983 	 * Work on the "extended" feature information, doing
984 	 * some basic initialization for cpuid_pass2()
985 	 */
986 	xcpuid = 0;
987 	switch (cpi->cpi_vendor) {
988 	case X86_VENDOR_Intel:
989 		if (IS_NEW_F6(cpi) || cpi->cpi_family >= 0xf)
990 			xcpuid++;
991 		break;
992 	case X86_VENDOR_AMD:
993 		if (cpi->cpi_family > 5 ||
994 		    (cpi->cpi_family == 5 && cpi->cpi_model >= 1))
995 			xcpuid++;
996 		break;
997 	case X86_VENDOR_Cyrix:
998 		/*
999 		 * Only these Cyrix CPUs are -known- to support
1000 		 * extended cpuid operations.
1001 		 */
1002 		if (x86_type == X86_TYPE_VIA_CYRIX_III ||
1003 		    x86_type == X86_TYPE_CYRIX_GXm)
1004 			xcpuid++;
1005 		break;
1006 	case X86_VENDOR_Centaur:
1007 	case X86_VENDOR_TM:
1008 	default:
1009 		xcpuid++;
1010 		break;
1011 	}
1012 
1013 	if (xcpuid) {
1014 		cp = &cpi->cpi_extd[0];
1015 		cp->cp_eax = 0x80000000;
1016 		cpi->cpi_xmaxeax = __cpuid_insn(cp);
1017 	}
1018 
1019 	if (cpi->cpi_xmaxeax & 0x80000000) {
1020 
1021 		if (cpi->cpi_xmaxeax > CPI_XMAXEAX_MAX)
1022 			cpi->cpi_xmaxeax = CPI_XMAXEAX_MAX;
1023 
1024 		switch (cpi->cpi_vendor) {
1025 		case X86_VENDOR_Intel:
1026 		case X86_VENDOR_AMD:
1027 			if (cpi->cpi_xmaxeax < 0x80000001)
1028 				break;
1029 			cp = &cpi->cpi_extd[1];
1030 			cp->cp_eax = 0x80000001;
1031 			(void) __cpuid_insn(cp);
1032 
1033 			if (cpi->cpi_vendor == X86_VENDOR_AMD &&
1034 			    cpi->cpi_family == 5 &&
1035 			    cpi->cpi_model == 6 &&
1036 			    cpi->cpi_step == 6) {
1037 				/*
1038 				 * K6 model 6 uses bit 10 to indicate SYSC
1039 				 * Later models use bit 11. Fix it here.
1040 				 */
1041 				if (cp->cp_edx & 0x400) {
1042 					cp->cp_edx &= ~0x400;
1043 					cp->cp_edx |= CPUID_AMD_EDX_SYSC;
1044 				}
1045 			}
1046 
1047 			platform_cpuid_mangle(cpi->cpi_vendor, 0x80000001, cp);
1048 
1049 			/*
1050 			 * Compute the additions to the kernel's feature word.
1051 			 */
1052 			if (cp->cp_edx & CPUID_AMD_EDX_NX)
1053 				feature |= X86_NX;
1054 
1055 #if defined(__amd64)
1056 			/* 1 GB large page - enable only for 64 bit kernel */
1057 			if (cp->cp_edx & CPUID_AMD_EDX_1GPG)
1058 				feature |= X86_1GPG;
1059 #endif
1060 
1061 			if ((cpi->cpi_vendor == X86_VENDOR_AMD) &&
1062 			    (cpi->cpi_std[1].cp_edx & CPUID_INTC_EDX_FXSR) &&
1063 			    (cp->cp_ecx & CPUID_AMD_ECX_SSE4A))
1064 				feature |= X86_SSE4A;
1065 
1066 			/*
1067 			 * If both the HTT and CMP_LGCY bits are set,
1068 			 * then we're not actually HyperThreaded.  Read
1069 			 * "AMD CPUID Specification" for more details.
1070 			 */
1071 			if (cpi->cpi_vendor == X86_VENDOR_AMD &&
1072 			    (feature & X86_HTT) &&
1073 			    (cp->cp_ecx & CPUID_AMD_ECX_CMP_LGCY)) {
1074 				feature &= ~X86_HTT;
1075 				feature |= X86_CMP;
1076 			}
1077 #if defined(__amd64)
1078 			/*
1079 			 * It's really tricky to support syscall/sysret in
1080 			 * the i386 kernel; we rely on sysenter/sysexit
1081 			 * instead.  In the amd64 kernel, things are -way-
1082 			 * better.
1083 			 */
1084 			if (cp->cp_edx & CPUID_AMD_EDX_SYSC)
1085 				feature |= X86_ASYSC;
1086 
1087 			/*
1088 			 * While we're thinking about system calls, note
1089 			 * that AMD processors don't support sysenter
1090 			 * in long mode at all, so don't try to program them.
1091 			 */
1092 			if (x86_vendor == X86_VENDOR_AMD)
1093 				feature &= ~X86_SEP;
1094 #endif
1095 			if (x86_vendor == X86_VENDOR_AMD &&
1096 			    cp->cp_edx & CPUID_AMD_EDX_TSCP)
1097 				feature |= X86_TSCP;
1098 			break;
1099 		default:
1100 			break;
1101 		}
1102 
1103 		/*
1104 		 * Get CPUID data about processor cores and hyperthreads.
1105 		 */
1106 		switch (cpi->cpi_vendor) {
1107 		case X86_VENDOR_Intel:
1108 			if (cpi->cpi_maxeax >= 4) {
1109 				cp = &cpi->cpi_std[4];
1110 				cp->cp_eax = 4;
1111 				cp->cp_ecx = 0;
1112 				(void) __cpuid_insn(cp);
1113 				platform_cpuid_mangle(cpi->cpi_vendor, 4, cp);
1114 			}
1115 			/*FALLTHROUGH*/
1116 		case X86_VENDOR_AMD:
1117 			if (cpi->cpi_xmaxeax < 0x80000008)
1118 				break;
1119 			cp = &cpi->cpi_extd[8];
1120 			cp->cp_eax = 0x80000008;
1121 			(void) __cpuid_insn(cp);
1122 			platform_cpuid_mangle(cpi->cpi_vendor, 0x80000008, cp);
1123 
1124 			/*
1125 			 * Virtual and physical address limits from
1126 			 * cpuid override previously guessed values.
1127 			 */
1128 			cpi->cpi_pabits = BITX(cp->cp_eax, 7, 0);
1129 			cpi->cpi_vabits = BITX(cp->cp_eax, 15, 8);
1130 			break;
1131 		default:
1132 			break;
1133 		}
1134 
1135 		/*
1136 		 * Derive the number of cores per chip
1137 		 */
1138 		switch (cpi->cpi_vendor) {
1139 		case X86_VENDOR_Intel:
1140 			if (cpi->cpi_maxeax < 4) {
1141 				cpi->cpi_ncore_per_chip = 1;
1142 				break;
1143 			} else {
1144 				cpi->cpi_ncore_per_chip =
1145 				    BITX((cpi)->cpi_std[4].cp_eax, 31, 26) + 1;
1146 			}
1147 			break;
1148 		case X86_VENDOR_AMD:
1149 			if (cpi->cpi_xmaxeax < 0x80000008) {
1150 				cpi->cpi_ncore_per_chip = 1;
1151 				break;
1152 			} else {
1153 				/*
1154 				 * On family 0xf cpuid fn 2 ECX[7:0] "NC" is
1155 				 * 1 less than the number of physical cores on
1156 				 * the chip.  In family 0x10 this value can
1157 				 * be affected by "downcoring" - it reflects
1158 				 * 1 less than the number of cores actually
1159 				 * enabled on this node.
1160 				 */
1161 				cpi->cpi_ncore_per_chip =
1162 				    BITX((cpi)->cpi_extd[8].cp_ecx, 7, 0) + 1;
1163 			}
1164 			break;
1165 		default:
1166 			cpi->cpi_ncore_per_chip = 1;
1167 			break;
1168 		}
1169 	} else {
1170 		cpi->cpi_ncore_per_chip = 1;
1171 	}
1172 
1173 	/*
1174 	 * If more than one core, then this processor is CMP.
1175 	 */
1176 	if (cpi->cpi_ncore_per_chip > 1)
1177 		feature |= X86_CMP;
1178 
1179 	/*
1180 	 * If the number of cores is the same as the number
1181 	 * of CPUs, then we cannot have HyperThreading.
1182 	 */
1183 	if (cpi->cpi_ncpu_per_chip == cpi->cpi_ncore_per_chip)
1184 		feature &= ~X86_HTT;
1185 
1186 	if ((feature & (X86_HTT | X86_CMP)) == 0) {
1187 		/*
1188 		 * Single-core single-threaded processors.
1189 		 */
1190 		cpi->cpi_chipid = -1;
1191 		cpi->cpi_clogid = 0;
1192 		cpi->cpi_coreid = cpu->cpu_id;
1193 		cpi->cpi_pkgcoreid = 0;
1194 	} else if (cpi->cpi_ncpu_per_chip > 1) {
1195 		uint_t i;
1196 		uint_t chipid_shift = 0;
1197 		uint_t coreid_shift = 0;
1198 		uint_t apic_id = CPI_APIC_ID(cpi);
1199 
1200 		for (i = 1; i < cpi->cpi_ncpu_per_chip; i <<= 1)
1201 			chipid_shift++;
1202 		cpi->cpi_chipid = apic_id >> chipid_shift;
1203 		cpi->cpi_clogid = apic_id & ((1 << chipid_shift) - 1);
1204 
1205 		if (cpi->cpi_vendor == X86_VENDOR_Intel) {
1206 			if (feature & X86_CMP) {
1207 				/*
1208 				 * Multi-core (and possibly multi-threaded)
1209 				 * processors.
1210 				 */
1211 				uint_t ncpu_per_core;
1212 				if (cpi->cpi_ncore_per_chip == 1)
1213 					ncpu_per_core = cpi->cpi_ncpu_per_chip;
1214 				else if (cpi->cpi_ncore_per_chip > 1)
1215 					ncpu_per_core = cpi->cpi_ncpu_per_chip /
1216 					    cpi->cpi_ncore_per_chip;
1217 				/*
1218 				 * 8bit APIC IDs on dual core Pentiums
1219 				 * look like this:
1220 				 *
1221 				 * +-----------------------+------+------+
1222 				 * | Physical Package ID   |  MC  |  HT  |
1223 				 * +-----------------------+------+------+
1224 				 * <------- chipid -------->
1225 				 * <------- coreid --------------->
1226 				 *			   <--- clogid -->
1227 				 *			   <------>
1228 				 *			   pkgcoreid
1229 				 *
1230 				 * Where the number of bits necessary to
1231 				 * represent MC and HT fields together equals
1232 				 * to the minimum number of bits necessary to
1233 				 * store the value of cpi->cpi_ncpu_per_chip.
1234 				 * Of those bits, the MC part uses the number
1235 				 * of bits necessary to store the value of
1236 				 * cpi->cpi_ncore_per_chip.
1237 				 */
1238 				for (i = 1; i < ncpu_per_core; i <<= 1)
1239 					coreid_shift++;
1240 				cpi->cpi_coreid = apic_id >> coreid_shift;
1241 				cpi->cpi_pkgcoreid = cpi->cpi_clogid >>
1242 				    coreid_shift;
1243 			} else if (feature & X86_HTT) {
1244 				/*
1245 				 * Single-core multi-threaded processors.
1246 				 */
1247 				cpi->cpi_coreid = cpi->cpi_chipid;
1248 				cpi->cpi_pkgcoreid = 0;
1249 			}
1250 		} else if (cpi->cpi_vendor == X86_VENDOR_AMD) {
1251 			/*
1252 			 * AMD CMP chips currently have a single thread per
1253 			 * core, with 2 cores on family 0xf and 2, 3 or 4
1254 			 * cores on family 0x10.
1255 			 *
1256 			 * Since no two cpus share a core we must assign a
1257 			 * distinct coreid per cpu, and we do this by using
1258 			 * the cpu_id.  This scheme does not, however,
1259 			 * guarantee that sibling cores of a chip will have
1260 			 * sequential coreids starting at a multiple of the
1261 			 * number of cores per chip - that is usually the
1262 			 * case, but if the ACPI MADT table is presented
1263 			 * in a different order then we need to perform a
1264 			 * few more gymnastics for the pkgcoreid.
1265 			 *
1266 			 * In family 0xf CMPs there are 2 cores on all nodes
1267 			 * present - no mixing of single and dual core parts.
1268 			 *
1269 			 * In family 0x10 CMPs cpuid fn 2 ECX[15:12]
1270 			 * "ApicIdCoreIdSize[3:0]" tells us how
1271 			 * many least-significant bits in the ApicId
1272 			 * are used to represent the core number
1273 			 * within the node.  Cores are always
1274 			 * numbered sequentially from 0 regardless
1275 			 * of how many or which are disabled, and
1276 			 * there seems to be no way to discover the
1277 			 * real core id when some are disabled.
1278 			 */
1279 			cpi->cpi_coreid = cpu->cpu_id;
1280 
1281 			if (cpi->cpi_family == 0x10 &&
1282 			    cpi->cpi_xmaxeax >= 0x80000008) {
1283 				int coreidsz =
1284 				    BITX((cpi)->cpi_extd[8].cp_ecx, 15, 12);
1285 
1286 				cpi->cpi_pkgcoreid =
1287 				    apic_id & ((1 << coreidsz) - 1);
1288 			} else {
1289 				cpi->cpi_pkgcoreid = cpi->cpi_clogid;
1290 			}
1291 		} else {
1292 			/*
1293 			 * All other processors are currently
1294 			 * assumed to have single cores.
1295 			 */
1296 			cpi->cpi_coreid = cpi->cpi_chipid;
1297 			cpi->cpi_pkgcoreid = 0;
1298 		}
1299 	}
1300 
1301 	/*
1302 	 * Synthesize chip "revision" and socket type
1303 	 */
1304 	synth_info(cpi);
1305 
1306 pass1_done:
1307 #if !defined(__xpv)
1308 	check_for_hvm();
1309 #endif
1310 	cpi->cpi_pass = 1;
1311 	return (feature);
1312 }
1313 
1314 /*
1315  * Make copies of the cpuid table entries we depend on, in
1316  * part for ease of parsing now, in part so that we have only
1317  * one place to correct any of it, in part for ease of
1318  * later export to userland, and in part so we can look at
1319  * this stuff in a crash dump.
1320  */
1321 
1322 /*ARGSUSED*/
1323 void
1324 cpuid_pass2(cpu_t *cpu)
1325 {
1326 	uint_t n, nmax;
1327 	int i;
1328 	struct cpuid_regs *cp;
1329 	uint8_t *dp;
1330 	uint32_t *iptr;
1331 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
1332 
1333 	ASSERT(cpi->cpi_pass == 1);
1334 
1335 	if (cpi->cpi_maxeax < 1)
1336 		goto pass2_done;
1337 
1338 	if ((nmax = cpi->cpi_maxeax + 1) > NMAX_CPI_STD)
1339 		nmax = NMAX_CPI_STD;
1340 	/*
1341 	 * (We already handled n == 0 and n == 1 in pass 1)
1342 	 */
1343 	for (n = 2, cp = &cpi->cpi_std[2]; n < nmax; n++, cp++) {
1344 		cp->cp_eax = n;
1345 
1346 		/*
1347 		 * CPUID function 4 expects %ecx to be initialized
1348 		 * with an index which indicates which cache to return
1349 		 * information about. The OS is expected to call function 4
1350 		 * with %ecx set to 0, 1, 2, ... until it returns with
1351 		 * EAX[4:0] set to 0, which indicates there are no more
1352 		 * caches.
1353 		 *
1354 		 * Here, populate cpi_std[4] with the information returned by
1355 		 * function 4 when %ecx == 0, and do the rest in cpuid_pass3()
1356 		 * when dynamic memory allocation becomes available.
1357 		 *
1358 		 * Note: we need to explicitly initialize %ecx here, since
1359 		 * function 4 may have been previously invoked.
1360 		 */
1361 		if (n == 4)
1362 			cp->cp_ecx = 0;
1363 
1364 		(void) __cpuid_insn(cp);
1365 		platform_cpuid_mangle(cpi->cpi_vendor, n, cp);
1366 		switch (n) {
1367 		case 2:
1368 			/*
1369 			 * "the lower 8 bits of the %eax register
1370 			 * contain a value that identifies the number
1371 			 * of times the cpuid [instruction] has to be
1372 			 * executed to obtain a complete image of the
1373 			 * processor's caching systems."
1374 			 *
1375 			 * How *do* they make this stuff up?
1376 			 */
1377 			cpi->cpi_ncache = sizeof (*cp) *
1378 			    BITX(cp->cp_eax, 7, 0);
1379 			if (cpi->cpi_ncache == 0)
1380 				break;
1381 			cpi->cpi_ncache--;	/* skip count byte */
1382 
1383 			/*
1384 			 * Well, for now, rather than attempt to implement
1385 			 * this slightly dubious algorithm, we just look
1386 			 * at the first 15 ..
1387 			 */
1388 			if (cpi->cpi_ncache > (sizeof (*cp) - 1))
1389 				cpi->cpi_ncache = sizeof (*cp) - 1;
1390 
1391 			dp = cpi->cpi_cacheinfo;
1392 			if (BITX(cp->cp_eax, 31, 31) == 0) {
1393 				uint8_t *p = (void *)&cp->cp_eax;
1394 				for (i = 1; i < 4; i++)
1395 					if (p[i] != 0)
1396 						*dp++ = p[i];
1397 			}
1398 			if (BITX(cp->cp_ebx, 31, 31) == 0) {
1399 				uint8_t *p = (void *)&cp->cp_ebx;
1400 				for (i = 0; i < 4; i++)
1401 					if (p[i] != 0)
1402 						*dp++ = p[i];
1403 			}
1404 			if (BITX(cp->cp_ecx, 31, 31) == 0) {
1405 				uint8_t *p = (void *)&cp->cp_ecx;
1406 				for (i = 0; i < 4; i++)
1407 					if (p[i] != 0)
1408 						*dp++ = p[i];
1409 			}
1410 			if (BITX(cp->cp_edx, 31, 31) == 0) {
1411 				uint8_t *p = (void *)&cp->cp_edx;
1412 				for (i = 0; i < 4; i++)
1413 					if (p[i] != 0)
1414 						*dp++ = p[i];
1415 			}
1416 			break;
1417 
1418 		case 3:	/* Processor serial number, if PSN supported */
1419 			break;
1420 
1421 		case 4:	/* Deterministic cache parameters */
1422 			break;
1423 
1424 		case 5:	/* Monitor/Mwait parameters */
1425 		{
1426 			size_t mwait_size;
1427 
1428 			/*
1429 			 * check cpi_mwait.support which was set in cpuid_pass1
1430 			 */
1431 			if (!(cpi->cpi_mwait.support & MWAIT_SUPPORT))
1432 				break;
1433 
1434 			/*
1435 			 * Protect ourself from insane mwait line size.
1436 			 * Workaround for incomplete hardware emulator(s).
1437 			 */
1438 			mwait_size = (size_t)MWAIT_SIZE_MAX(cpi);
1439 			if (mwait_size < sizeof (uint32_t) ||
1440 			    !ISP2(mwait_size)) {
1441 #if DEBUG
1442 				cmn_err(CE_NOTE, "Cannot handle cpu %d mwait "
1443 				    "size %ld",
1444 				    cpu->cpu_id, (long)mwait_size);
1445 #endif
1446 				break;
1447 			}
1448 
1449 			cpi->cpi_mwait.mon_min = (size_t)MWAIT_SIZE_MIN(cpi);
1450 			cpi->cpi_mwait.mon_max = mwait_size;
1451 			if (MWAIT_EXTENSION(cpi)) {
1452 				cpi->cpi_mwait.support |= MWAIT_EXTENSIONS;
1453 				if (MWAIT_INT_ENABLE(cpi))
1454 					cpi->cpi_mwait.support |=
1455 					    MWAIT_ECX_INT_ENABLE;
1456 			}
1457 			break;
1458 		}
1459 		default:
1460 			break;
1461 		}
1462 	}
1463 
1464 	if ((cpi->cpi_xmaxeax & 0x80000000) == 0)
1465 		goto pass2_done;
1466 
1467 	if ((nmax = cpi->cpi_xmaxeax - 0x80000000 + 1) > NMAX_CPI_EXTD)
1468 		nmax = NMAX_CPI_EXTD;
1469 	/*
1470 	 * Copy the extended properties, fixing them as we go.
1471 	 * (We already handled n == 0 and n == 1 in pass 1)
1472 	 */
1473 	iptr = (void *)cpi->cpi_brandstr;
1474 	for (n = 2, cp = &cpi->cpi_extd[2]; n < nmax; cp++, n++) {
1475 		cp->cp_eax = 0x80000000 + n;
1476 		(void) __cpuid_insn(cp);
1477 		platform_cpuid_mangle(cpi->cpi_vendor, 0x80000000 + n, cp);
1478 		switch (n) {
1479 		case 2:
1480 		case 3:
1481 		case 4:
1482 			/*
1483 			 * Extract the brand string
1484 			 */
1485 			*iptr++ = cp->cp_eax;
1486 			*iptr++ = cp->cp_ebx;
1487 			*iptr++ = cp->cp_ecx;
1488 			*iptr++ = cp->cp_edx;
1489 			break;
1490 		case 5:
1491 			switch (cpi->cpi_vendor) {
1492 			case X86_VENDOR_AMD:
1493 				/*
1494 				 * The Athlon and Duron were the first
1495 				 * parts to report the sizes of the
1496 				 * TLB for large pages. Before then,
1497 				 * we don't trust the data.
1498 				 */
1499 				if (cpi->cpi_family < 6 ||
1500 				    (cpi->cpi_family == 6 &&
1501 				    cpi->cpi_model < 1))
1502 					cp->cp_eax = 0;
1503 				break;
1504 			default:
1505 				break;
1506 			}
1507 			break;
1508 		case 6:
1509 			switch (cpi->cpi_vendor) {
1510 			case X86_VENDOR_AMD:
1511 				/*
1512 				 * The Athlon and Duron were the first
1513 				 * AMD parts with L2 TLB's.
1514 				 * Before then, don't trust the data.
1515 				 */
1516 				if (cpi->cpi_family < 6 ||
1517 				    cpi->cpi_family == 6 &&
1518 				    cpi->cpi_model < 1)
1519 					cp->cp_eax = cp->cp_ebx = 0;
1520 				/*
1521 				 * AMD Duron rev A0 reports L2
1522 				 * cache size incorrectly as 1K
1523 				 * when it is really 64K
1524 				 */
1525 				if (cpi->cpi_family == 6 &&
1526 				    cpi->cpi_model == 3 &&
1527 				    cpi->cpi_step == 0) {
1528 					cp->cp_ecx &= 0xffff;
1529 					cp->cp_ecx |= 0x400000;
1530 				}
1531 				break;
1532 			case X86_VENDOR_Cyrix:	/* VIA C3 */
1533 				/*
1534 				 * VIA C3 processors are a bit messed
1535 				 * up w.r.t. encoding cache sizes in %ecx
1536 				 */
1537 				if (cpi->cpi_family != 6)
1538 					break;
1539 				/*
1540 				 * model 7 and 8 were incorrectly encoded
1541 				 *
1542 				 * xxx is model 8 really broken?
1543 				 */
1544 				if (cpi->cpi_model == 7 ||
1545 				    cpi->cpi_model == 8)
1546 					cp->cp_ecx =
1547 					    BITX(cp->cp_ecx, 31, 24) << 16 |
1548 					    BITX(cp->cp_ecx, 23, 16) << 12 |
1549 					    BITX(cp->cp_ecx, 15, 8) << 8 |
1550 					    BITX(cp->cp_ecx, 7, 0);
1551 				/*
1552 				 * model 9 stepping 1 has wrong associativity
1553 				 */
1554 				if (cpi->cpi_model == 9 && cpi->cpi_step == 1)
1555 					cp->cp_ecx |= 8 << 12;
1556 				break;
1557 			case X86_VENDOR_Intel:
1558 				/*
1559 				 * Extended L2 Cache features function.
1560 				 * First appeared on Prescott.
1561 				 */
1562 			default:
1563 				break;
1564 			}
1565 			break;
1566 		default:
1567 			break;
1568 		}
1569 	}
1570 
1571 pass2_done:
1572 	cpi->cpi_pass = 2;
1573 }
1574 
1575 static const char *
1576 intel_cpubrand(const struct cpuid_info *cpi)
1577 {
1578 	int i;
1579 
1580 	if ((x86_feature & X86_CPUID) == 0 ||
1581 	    cpi->cpi_maxeax < 1 || cpi->cpi_family < 5)
1582 		return ("i486");
1583 
1584 	switch (cpi->cpi_family) {
1585 	case 5:
1586 		return ("Intel Pentium(r)");
1587 	case 6:
1588 		switch (cpi->cpi_model) {
1589 			uint_t celeron, xeon;
1590 			const struct cpuid_regs *cp;
1591 		case 0:
1592 		case 1:
1593 		case 2:
1594 			return ("Intel Pentium(r) Pro");
1595 		case 3:
1596 		case 4:
1597 			return ("Intel Pentium(r) II");
1598 		case 6:
1599 			return ("Intel Celeron(r)");
1600 		case 5:
1601 		case 7:
1602 			celeron = xeon = 0;
1603 			cp = &cpi->cpi_std[2];	/* cache info */
1604 
1605 			for (i = 1; i < 4; i++) {
1606 				uint_t tmp;
1607 
1608 				tmp = (cp->cp_eax >> (8 * i)) & 0xff;
1609 				if (tmp == 0x40)
1610 					celeron++;
1611 				if (tmp >= 0x44 && tmp <= 0x45)
1612 					xeon++;
1613 			}
1614 
1615 			for (i = 0; i < 2; i++) {
1616 				uint_t tmp;
1617 
1618 				tmp = (cp->cp_ebx >> (8 * i)) & 0xff;
1619 				if (tmp == 0x40)
1620 					celeron++;
1621 				else if (tmp >= 0x44 && tmp <= 0x45)
1622 					xeon++;
1623 			}
1624 
1625 			for (i = 0; i < 4; i++) {
1626 				uint_t tmp;
1627 
1628 				tmp = (cp->cp_ecx >> (8 * i)) & 0xff;
1629 				if (tmp == 0x40)
1630 					celeron++;
1631 				else if (tmp >= 0x44 && tmp <= 0x45)
1632 					xeon++;
1633 			}
1634 
1635 			for (i = 0; i < 4; i++) {
1636 				uint_t tmp;
1637 
1638 				tmp = (cp->cp_edx >> (8 * i)) & 0xff;
1639 				if (tmp == 0x40)
1640 					celeron++;
1641 				else if (tmp >= 0x44 && tmp <= 0x45)
1642 					xeon++;
1643 			}
1644 
1645 			if (celeron)
1646 				return ("Intel Celeron(r)");
1647 			if (xeon)
1648 				return (cpi->cpi_model == 5 ?
1649 				    "Intel Pentium(r) II Xeon(tm)" :
1650 				    "Intel Pentium(r) III Xeon(tm)");
1651 			return (cpi->cpi_model == 5 ?
1652 			    "Intel Pentium(r) II or Pentium(r) II Xeon(tm)" :
1653 			    "Intel Pentium(r) III or Pentium(r) III Xeon(tm)");
1654 		default:
1655 			break;
1656 		}
1657 	default:
1658 		break;
1659 	}
1660 
1661 	/* BrandID is present if the field is nonzero */
1662 	if (cpi->cpi_brandid != 0) {
1663 		static const struct {
1664 			uint_t bt_bid;
1665 			const char *bt_str;
1666 		} brand_tbl[] = {
1667 			{ 0x1,	"Intel(r) Celeron(r)" },
1668 			{ 0x2,	"Intel(r) Pentium(r) III" },
1669 			{ 0x3,	"Intel(r) Pentium(r) III Xeon(tm)" },
1670 			{ 0x4,	"Intel(r) Pentium(r) III" },
1671 			{ 0x6,	"Mobile Intel(r) Pentium(r) III" },
1672 			{ 0x7,	"Mobile Intel(r) Celeron(r)" },
1673 			{ 0x8,	"Intel(r) Pentium(r) 4" },
1674 			{ 0x9,	"Intel(r) Pentium(r) 4" },
1675 			{ 0xa,	"Intel(r) Celeron(r)" },
1676 			{ 0xb,	"Intel(r) Xeon(tm)" },
1677 			{ 0xc,	"Intel(r) Xeon(tm) MP" },
1678 			{ 0xe,	"Mobile Intel(r) Pentium(r) 4" },
1679 			{ 0xf,	"Mobile Intel(r) Celeron(r)" },
1680 			{ 0x11, "Mobile Genuine Intel(r)" },
1681 			{ 0x12, "Intel(r) Celeron(r) M" },
1682 			{ 0x13, "Mobile Intel(r) Celeron(r)" },
1683 			{ 0x14, "Intel(r) Celeron(r)" },
1684 			{ 0x15, "Mobile Genuine Intel(r)" },
1685 			{ 0x16,	"Intel(r) Pentium(r) M" },
1686 			{ 0x17, "Mobile Intel(r) Celeron(r)" }
1687 		};
1688 		uint_t btblmax = sizeof (brand_tbl) / sizeof (brand_tbl[0]);
1689 		uint_t sgn;
1690 
1691 		sgn = (cpi->cpi_family << 8) |
1692 		    (cpi->cpi_model << 4) | cpi->cpi_step;
1693 
1694 		for (i = 0; i < btblmax; i++)
1695 			if (brand_tbl[i].bt_bid == cpi->cpi_brandid)
1696 				break;
1697 		if (i < btblmax) {
1698 			if (sgn == 0x6b1 && cpi->cpi_brandid == 3)
1699 				return ("Intel(r) Celeron(r)");
1700 			if (sgn < 0xf13 && cpi->cpi_brandid == 0xb)
1701 				return ("Intel(r) Xeon(tm) MP");
1702 			if (sgn < 0xf13 && cpi->cpi_brandid == 0xe)
1703 				return ("Intel(r) Xeon(tm)");
1704 			return (brand_tbl[i].bt_str);
1705 		}
1706 	}
1707 
1708 	return (NULL);
1709 }
1710 
1711 static const char *
1712 amd_cpubrand(const struct cpuid_info *cpi)
1713 {
1714 	if ((x86_feature & X86_CPUID) == 0 ||
1715 	    cpi->cpi_maxeax < 1 || cpi->cpi_family < 5)
1716 		return ("i486 compatible");
1717 
1718 	switch (cpi->cpi_family) {
1719 	case 5:
1720 		switch (cpi->cpi_model) {
1721 		case 0:
1722 		case 1:
1723 		case 2:
1724 		case 3:
1725 		case 4:
1726 		case 5:
1727 			return ("AMD-K5(r)");
1728 		case 6:
1729 		case 7:
1730 			return ("AMD-K6(r)");
1731 		case 8:
1732 			return ("AMD-K6(r)-2");
1733 		case 9:
1734 			return ("AMD-K6(r)-III");
1735 		default:
1736 			return ("AMD (family 5)");
1737 		}
1738 	case 6:
1739 		switch (cpi->cpi_model) {
1740 		case 1:
1741 			return ("AMD-K7(tm)");
1742 		case 0:
1743 		case 2:
1744 		case 4:
1745 			return ("AMD Athlon(tm)");
1746 		case 3:
1747 		case 7:
1748 			return ("AMD Duron(tm)");
1749 		case 6:
1750 		case 8:
1751 		case 10:
1752 			/*
1753 			 * Use the L2 cache size to distinguish
1754 			 */
1755 			return ((cpi->cpi_extd[6].cp_ecx >> 16) >= 256 ?
1756 			    "AMD Athlon(tm)" : "AMD Duron(tm)");
1757 		default:
1758 			return ("AMD (family 6)");
1759 		}
1760 	default:
1761 		break;
1762 	}
1763 
1764 	if (cpi->cpi_family == 0xf && cpi->cpi_model == 5 &&
1765 	    cpi->cpi_brandid != 0) {
1766 		switch (BITX(cpi->cpi_brandid, 7, 5)) {
1767 		case 3:
1768 			return ("AMD Opteron(tm) UP 1xx");
1769 		case 4:
1770 			return ("AMD Opteron(tm) DP 2xx");
1771 		case 5:
1772 			return ("AMD Opteron(tm) MP 8xx");
1773 		default:
1774 			return ("AMD Opteron(tm)");
1775 		}
1776 	}
1777 
1778 	return (NULL);
1779 }
1780 
1781 static const char *
1782 cyrix_cpubrand(struct cpuid_info *cpi, uint_t type)
1783 {
1784 	if ((x86_feature & X86_CPUID) == 0 ||
1785 	    cpi->cpi_maxeax < 1 || cpi->cpi_family < 5 ||
1786 	    type == X86_TYPE_CYRIX_486)
1787 		return ("i486 compatible");
1788 
1789 	switch (type) {
1790 	case X86_TYPE_CYRIX_6x86:
1791 		return ("Cyrix 6x86");
1792 	case X86_TYPE_CYRIX_6x86L:
1793 		return ("Cyrix 6x86L");
1794 	case X86_TYPE_CYRIX_6x86MX:
1795 		return ("Cyrix 6x86MX");
1796 	case X86_TYPE_CYRIX_GXm:
1797 		return ("Cyrix GXm");
1798 	case X86_TYPE_CYRIX_MediaGX:
1799 		return ("Cyrix MediaGX");
1800 	case X86_TYPE_CYRIX_MII:
1801 		return ("Cyrix M2");
1802 	case X86_TYPE_VIA_CYRIX_III:
1803 		return ("VIA Cyrix M3");
1804 	default:
1805 		/*
1806 		 * Have another wild guess ..
1807 		 */
1808 		if (cpi->cpi_family == 4 && cpi->cpi_model == 9)
1809 			return ("Cyrix 5x86");
1810 		else if (cpi->cpi_family == 5) {
1811 			switch (cpi->cpi_model) {
1812 			case 2:
1813 				return ("Cyrix 6x86");	/* Cyrix M1 */
1814 			case 4:
1815 				return ("Cyrix MediaGX");
1816 			default:
1817 				break;
1818 			}
1819 		} else if (cpi->cpi_family == 6) {
1820 			switch (cpi->cpi_model) {
1821 			case 0:
1822 				return ("Cyrix 6x86MX"); /* Cyrix M2? */
1823 			case 5:
1824 			case 6:
1825 			case 7:
1826 			case 8:
1827 			case 9:
1828 				return ("VIA C3");
1829 			default:
1830 				break;
1831 			}
1832 		}
1833 		break;
1834 	}
1835 	return (NULL);
1836 }
1837 
1838 /*
1839  * This only gets called in the case that the CPU extended
1840  * feature brand string (0x80000002, 0x80000003, 0x80000004)
1841  * aren't available, or contain null bytes for some reason.
1842  */
1843 static void
1844 fabricate_brandstr(struct cpuid_info *cpi)
1845 {
1846 	const char *brand = NULL;
1847 
1848 	switch (cpi->cpi_vendor) {
1849 	case X86_VENDOR_Intel:
1850 		brand = intel_cpubrand(cpi);
1851 		break;
1852 	case X86_VENDOR_AMD:
1853 		brand = amd_cpubrand(cpi);
1854 		break;
1855 	case X86_VENDOR_Cyrix:
1856 		brand = cyrix_cpubrand(cpi, x86_type);
1857 		break;
1858 	case X86_VENDOR_NexGen:
1859 		if (cpi->cpi_family == 5 && cpi->cpi_model == 0)
1860 			brand = "NexGen Nx586";
1861 		break;
1862 	case X86_VENDOR_Centaur:
1863 		if (cpi->cpi_family == 5)
1864 			switch (cpi->cpi_model) {
1865 			case 4:
1866 				brand = "Centaur C6";
1867 				break;
1868 			case 8:
1869 				brand = "Centaur C2";
1870 				break;
1871 			case 9:
1872 				brand = "Centaur C3";
1873 				break;
1874 			default:
1875 				break;
1876 			}
1877 		break;
1878 	case X86_VENDOR_Rise:
1879 		if (cpi->cpi_family == 5 &&
1880 		    (cpi->cpi_model == 0 || cpi->cpi_model == 2))
1881 			brand = "Rise mP6";
1882 		break;
1883 	case X86_VENDOR_SiS:
1884 		if (cpi->cpi_family == 5 && cpi->cpi_model == 0)
1885 			brand = "SiS 55x";
1886 		break;
1887 	case X86_VENDOR_TM:
1888 		if (cpi->cpi_family == 5 && cpi->cpi_model == 4)
1889 			brand = "Transmeta Crusoe TM3x00 or TM5x00";
1890 		break;
1891 	case X86_VENDOR_NSC:
1892 	case X86_VENDOR_UMC:
1893 	default:
1894 		break;
1895 	}
1896 	if (brand) {
1897 		(void) strcpy((char *)cpi->cpi_brandstr, brand);
1898 		return;
1899 	}
1900 
1901 	/*
1902 	 * If all else fails ...
1903 	 */
1904 	(void) snprintf(cpi->cpi_brandstr, sizeof (cpi->cpi_brandstr),
1905 	    "%s %d.%d.%d", cpi->cpi_vendorstr, cpi->cpi_family,
1906 	    cpi->cpi_model, cpi->cpi_step);
1907 }
1908 
1909 /*
1910  * This routine is called just after kernel memory allocation
1911  * becomes available on cpu0, and as part of mp_startup() on
1912  * the other cpus.
1913  *
1914  * Fixup the brand string, and collect any information from cpuid
1915  * that requires dynamicically allocated storage to represent.
1916  */
1917 /*ARGSUSED*/
1918 void
1919 cpuid_pass3(cpu_t *cpu)
1920 {
1921 	int	i, max, shft, level, size;
1922 	struct cpuid_regs regs;
1923 	struct cpuid_regs *cp;
1924 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
1925 
1926 	ASSERT(cpi->cpi_pass == 2);
1927 
1928 	/*
1929 	 * Function 4: Deterministic cache parameters
1930 	 *
1931 	 * Take this opportunity to detect the number of threads
1932 	 * sharing the last level cache, and construct a corresponding
1933 	 * cache id. The respective cpuid_info members are initialized
1934 	 * to the default case of "no last level cache sharing".
1935 	 */
1936 	cpi->cpi_ncpu_shr_last_cache = 1;
1937 	cpi->cpi_last_lvl_cacheid = cpu->cpu_id;
1938 
1939 	if (cpi->cpi_maxeax >= 4 && cpi->cpi_vendor == X86_VENDOR_Intel) {
1940 
1941 		/*
1942 		 * Find the # of elements (size) returned by fn 4, and along
1943 		 * the way detect last level cache sharing details.
1944 		 */
1945 		bzero(&regs, sizeof (regs));
1946 		cp = &regs;
1947 		for (i = 0, max = 0; i < CPI_FN4_ECX_MAX; i++) {
1948 			cp->cp_eax = 4;
1949 			cp->cp_ecx = i;
1950 
1951 			(void) __cpuid_insn(cp);
1952 
1953 			if (CPI_CACHE_TYPE(cp) == 0)
1954 				break;
1955 			level = CPI_CACHE_LVL(cp);
1956 			if (level > max) {
1957 				max = level;
1958 				cpi->cpi_ncpu_shr_last_cache =
1959 				    CPI_NTHR_SHR_CACHE(cp) + 1;
1960 			}
1961 		}
1962 		cpi->cpi_std_4_size = size = i;
1963 
1964 		/*
1965 		 * Allocate the cpi_std_4 array. The first element
1966 		 * references the regs for fn 4, %ecx == 0, which
1967 		 * cpuid_pass2() stashed in cpi->cpi_std[4].
1968 		 */
1969 		if (size > 0) {
1970 			cpi->cpi_std_4 =
1971 			    kmem_alloc(size * sizeof (cp), KM_SLEEP);
1972 			cpi->cpi_std_4[0] = &cpi->cpi_std[4];
1973 
1974 			/*
1975 			 * Allocate storage to hold the additional regs
1976 			 * for function 4, %ecx == 1 .. cpi_std_4_size.
1977 			 *
1978 			 * The regs for fn 4, %ecx == 0 has already
1979 			 * been allocated as indicated above.
1980 			 */
1981 			for (i = 1; i < size; i++) {
1982 				cp = cpi->cpi_std_4[i] =
1983 				    kmem_zalloc(sizeof (regs), KM_SLEEP);
1984 				cp->cp_eax = 4;
1985 				cp->cp_ecx = i;
1986 
1987 				(void) __cpuid_insn(cp);
1988 			}
1989 		}
1990 		/*
1991 		 * Determine the number of bits needed to represent
1992 		 * the number of CPUs sharing the last level cache.
1993 		 *
1994 		 * Shift off that number of bits from the APIC id to
1995 		 * derive the cache id.
1996 		 */
1997 		shft = 0;
1998 		for (i = 1; i < cpi->cpi_ncpu_shr_last_cache; i <<= 1)
1999 			shft++;
2000 		cpi->cpi_last_lvl_cacheid = CPI_APIC_ID(cpi) >> shft;
2001 	}
2002 
2003 	/*
2004 	 * Now fixup the brand string
2005 	 */
2006 	if ((cpi->cpi_xmaxeax & 0x80000000) == 0) {
2007 		fabricate_brandstr(cpi);
2008 	} else {
2009 
2010 		/*
2011 		 * If we successfully extracted a brand string from the cpuid
2012 		 * instruction, clean it up by removing leading spaces and
2013 		 * similar junk.
2014 		 */
2015 		if (cpi->cpi_brandstr[0]) {
2016 			size_t maxlen = sizeof (cpi->cpi_brandstr);
2017 			char *src, *dst;
2018 
2019 			dst = src = (char *)cpi->cpi_brandstr;
2020 			src[maxlen - 1] = '\0';
2021 			/*
2022 			 * strip leading spaces
2023 			 */
2024 			while (*src == ' ')
2025 				src++;
2026 			/*
2027 			 * Remove any 'Genuine' or "Authentic" prefixes
2028 			 */
2029 			if (strncmp(src, "Genuine ", 8) == 0)
2030 				src += 8;
2031 			if (strncmp(src, "Authentic ", 10) == 0)
2032 				src += 10;
2033 
2034 			/*
2035 			 * Now do an in-place copy.
2036 			 * Map (R) to (r) and (TM) to (tm).
2037 			 * The era of teletypes is long gone, and there's
2038 			 * -really- no need to shout.
2039 			 */
2040 			while (*src != '\0') {
2041 				if (src[0] == '(') {
2042 					if (strncmp(src + 1, "R)", 2) == 0) {
2043 						(void) strncpy(dst, "(r)", 3);
2044 						src += 3;
2045 						dst += 3;
2046 						continue;
2047 					}
2048 					if (strncmp(src + 1, "TM)", 3) == 0) {
2049 						(void) strncpy(dst, "(tm)", 4);
2050 						src += 4;
2051 						dst += 4;
2052 						continue;
2053 					}
2054 				}
2055 				*dst++ = *src++;
2056 			}
2057 			*dst = '\0';
2058 
2059 			/*
2060 			 * Finally, remove any trailing spaces
2061 			 */
2062 			while (--dst > cpi->cpi_brandstr)
2063 				if (*dst == ' ')
2064 					*dst = '\0';
2065 				else
2066 					break;
2067 		} else
2068 			fabricate_brandstr(cpi);
2069 	}
2070 	cpi->cpi_pass = 3;
2071 }
2072 
2073 /*
2074  * This routine is called out of bind_hwcap() much later in the life
2075  * of the kernel (post_startup()).  The job of this routine is to resolve
2076  * the hardware feature support and kernel support for those features into
2077  * what we're actually going to tell applications via the aux vector.
2078  */
2079 uint_t
2080 cpuid_pass4(cpu_t *cpu)
2081 {
2082 	struct cpuid_info *cpi;
2083 	uint_t hwcap_flags = 0;
2084 
2085 	if (cpu == NULL)
2086 		cpu = CPU;
2087 	cpi = cpu->cpu_m.mcpu_cpi;
2088 
2089 	ASSERT(cpi->cpi_pass == 3);
2090 
2091 	if (cpi->cpi_maxeax >= 1) {
2092 		uint32_t *edx = &cpi->cpi_support[STD_EDX_FEATURES];
2093 		uint32_t *ecx = &cpi->cpi_support[STD_ECX_FEATURES];
2094 
2095 		*edx = CPI_FEATURES_EDX(cpi);
2096 		*ecx = CPI_FEATURES_ECX(cpi);
2097 
2098 		/*
2099 		 * [these require explicit kernel support]
2100 		 */
2101 		if ((x86_feature & X86_SEP) == 0)
2102 			*edx &= ~CPUID_INTC_EDX_SEP;
2103 
2104 		if ((x86_feature & X86_SSE) == 0)
2105 			*edx &= ~(CPUID_INTC_EDX_FXSR|CPUID_INTC_EDX_SSE);
2106 		if ((x86_feature & X86_SSE2) == 0)
2107 			*edx &= ~CPUID_INTC_EDX_SSE2;
2108 
2109 		if ((x86_feature & X86_HTT) == 0)
2110 			*edx &= ~CPUID_INTC_EDX_HTT;
2111 
2112 		if ((x86_feature & X86_SSE3) == 0)
2113 			*ecx &= ~CPUID_INTC_ECX_SSE3;
2114 
2115 		if (cpi->cpi_vendor == X86_VENDOR_Intel) {
2116 			if ((x86_feature & X86_SSSE3) == 0)
2117 				*ecx &= ~CPUID_INTC_ECX_SSSE3;
2118 			if ((x86_feature & X86_SSE4_1) == 0)
2119 				*ecx &= ~CPUID_INTC_ECX_SSE4_1;
2120 			if ((x86_feature & X86_SSE4_2) == 0)
2121 				*ecx &= ~CPUID_INTC_ECX_SSE4_2;
2122 		}
2123 
2124 		/*
2125 		 * [no explicit support required beyond x87 fp context]
2126 		 */
2127 		if (!fpu_exists)
2128 			*edx &= ~(CPUID_INTC_EDX_FPU | CPUID_INTC_EDX_MMX);
2129 
2130 		/*
2131 		 * Now map the supported feature vector to things that we
2132 		 * think userland will care about.
2133 		 */
2134 		if (*edx & CPUID_INTC_EDX_SEP)
2135 			hwcap_flags |= AV_386_SEP;
2136 		if (*edx & CPUID_INTC_EDX_SSE)
2137 			hwcap_flags |= AV_386_FXSR | AV_386_SSE;
2138 		if (*edx & CPUID_INTC_EDX_SSE2)
2139 			hwcap_flags |= AV_386_SSE2;
2140 		if (*ecx & CPUID_INTC_ECX_SSE3)
2141 			hwcap_flags |= AV_386_SSE3;
2142 		if (cpi->cpi_vendor == X86_VENDOR_Intel) {
2143 			if (*ecx & CPUID_INTC_ECX_SSSE3)
2144 				hwcap_flags |= AV_386_SSSE3;
2145 			if (*ecx & CPUID_INTC_ECX_SSE4_1)
2146 				hwcap_flags |= AV_386_SSE4_1;
2147 			if (*ecx & CPUID_INTC_ECX_SSE4_2)
2148 				hwcap_flags |= AV_386_SSE4_2;
2149 		}
2150 		if (*ecx & CPUID_INTC_ECX_POPCNT)
2151 			hwcap_flags |= AV_386_POPCNT;
2152 		if (*edx & CPUID_INTC_EDX_FPU)
2153 			hwcap_flags |= AV_386_FPU;
2154 		if (*edx & CPUID_INTC_EDX_MMX)
2155 			hwcap_flags |= AV_386_MMX;
2156 
2157 		if (*edx & CPUID_INTC_EDX_TSC)
2158 			hwcap_flags |= AV_386_TSC;
2159 		if (*edx & CPUID_INTC_EDX_CX8)
2160 			hwcap_flags |= AV_386_CX8;
2161 		if (*edx & CPUID_INTC_EDX_CMOV)
2162 			hwcap_flags |= AV_386_CMOV;
2163 		if (*ecx & CPUID_INTC_ECX_MON)
2164 			hwcap_flags |= AV_386_MON;
2165 		if (*ecx & CPUID_INTC_ECX_CX16)
2166 			hwcap_flags |= AV_386_CX16;
2167 	}
2168 
2169 	if (x86_feature & X86_HTT)
2170 		hwcap_flags |= AV_386_PAUSE;
2171 
2172 	if (cpi->cpi_xmaxeax < 0x80000001)
2173 		goto pass4_done;
2174 
2175 	switch (cpi->cpi_vendor) {
2176 		struct cpuid_regs cp;
2177 		uint32_t *edx, *ecx;
2178 
2179 	case X86_VENDOR_Intel:
2180 		/*
2181 		 * Seems like Intel duplicated what we necessary
2182 		 * here to make the initial crop of 64-bit OS's work.
2183 		 * Hopefully, those are the only "extended" bits
2184 		 * they'll add.
2185 		 */
2186 		/*FALLTHROUGH*/
2187 
2188 	case X86_VENDOR_AMD:
2189 		edx = &cpi->cpi_support[AMD_EDX_FEATURES];
2190 		ecx = &cpi->cpi_support[AMD_ECX_FEATURES];
2191 
2192 		*edx = CPI_FEATURES_XTD_EDX(cpi);
2193 		*ecx = CPI_FEATURES_XTD_ECX(cpi);
2194 
2195 		/*
2196 		 * [these features require explicit kernel support]
2197 		 */
2198 		switch (cpi->cpi_vendor) {
2199 		case X86_VENDOR_Intel:
2200 			break;
2201 
2202 		case X86_VENDOR_AMD:
2203 			if ((x86_feature & X86_TSCP) == 0)
2204 				*edx &= ~CPUID_AMD_EDX_TSCP;
2205 			if ((x86_feature & X86_SSE4A) == 0)
2206 				*ecx &= ~CPUID_AMD_ECX_SSE4A;
2207 			break;
2208 
2209 		default:
2210 			break;
2211 		}
2212 
2213 		/*
2214 		 * [no explicit support required beyond
2215 		 * x87 fp context and exception handlers]
2216 		 */
2217 		if (!fpu_exists)
2218 			*edx &= ~(CPUID_AMD_EDX_MMXamd |
2219 			    CPUID_AMD_EDX_3DNow | CPUID_AMD_EDX_3DNowx);
2220 
2221 		if ((x86_feature & X86_NX) == 0)
2222 			*edx &= ~CPUID_AMD_EDX_NX;
2223 #if !defined(__amd64)
2224 		*edx &= ~CPUID_AMD_EDX_LM;
2225 #endif
2226 		/*
2227 		 * Now map the supported feature vector to
2228 		 * things that we think userland will care about.
2229 		 */
2230 #if defined(__amd64)
2231 		if (*edx & CPUID_AMD_EDX_SYSC)
2232 			hwcap_flags |= AV_386_AMD_SYSC;
2233 #endif
2234 		if (*edx & CPUID_AMD_EDX_MMXamd)
2235 			hwcap_flags |= AV_386_AMD_MMX;
2236 		if (*edx & CPUID_AMD_EDX_3DNow)
2237 			hwcap_flags |= AV_386_AMD_3DNow;
2238 		if (*edx & CPUID_AMD_EDX_3DNowx)
2239 			hwcap_flags |= AV_386_AMD_3DNowx;
2240 
2241 		switch (cpi->cpi_vendor) {
2242 		case X86_VENDOR_AMD:
2243 			if (*edx & CPUID_AMD_EDX_TSCP)
2244 				hwcap_flags |= AV_386_TSCP;
2245 			if (*ecx & CPUID_AMD_ECX_AHF64)
2246 				hwcap_flags |= AV_386_AHF;
2247 			if (*ecx & CPUID_AMD_ECX_SSE4A)
2248 				hwcap_flags |= AV_386_AMD_SSE4A;
2249 			if (*ecx & CPUID_AMD_ECX_LZCNT)
2250 				hwcap_flags |= AV_386_AMD_LZCNT;
2251 			break;
2252 
2253 		case X86_VENDOR_Intel:
2254 			/*
2255 			 * Aarrgh.
2256 			 * Intel uses a different bit in the same word.
2257 			 */
2258 			if (*ecx & CPUID_INTC_ECX_AHF64)
2259 				hwcap_flags |= AV_386_AHF;
2260 			break;
2261 
2262 		default:
2263 			break;
2264 		}
2265 		break;
2266 
2267 	case X86_VENDOR_TM:
2268 		cp.cp_eax = 0x80860001;
2269 		(void) __cpuid_insn(&cp);
2270 		cpi->cpi_support[TM_EDX_FEATURES] = cp.cp_edx;
2271 		break;
2272 
2273 	default:
2274 		break;
2275 	}
2276 
2277 pass4_done:
2278 	cpi->cpi_pass = 4;
2279 	return (hwcap_flags);
2280 }
2281 
2282 
2283 /*
2284  * Simulate the cpuid instruction using the data we previously
2285  * captured about this CPU.  We try our best to return the truth
2286  * about the hardware, independently of kernel support.
2287  */
2288 uint32_t
2289 cpuid_insn(cpu_t *cpu, struct cpuid_regs *cp)
2290 {
2291 	struct cpuid_info *cpi;
2292 	struct cpuid_regs *xcp;
2293 
2294 	if (cpu == NULL)
2295 		cpu = CPU;
2296 	cpi = cpu->cpu_m.mcpu_cpi;
2297 
2298 	ASSERT(cpuid_checkpass(cpu, 3));
2299 
2300 	/*
2301 	 * CPUID data is cached in two separate places: cpi_std for standard
2302 	 * CPUID functions, and cpi_extd for extended CPUID functions.
2303 	 */
2304 	if (cp->cp_eax <= cpi->cpi_maxeax && cp->cp_eax < NMAX_CPI_STD)
2305 		xcp = &cpi->cpi_std[cp->cp_eax];
2306 	else if (cp->cp_eax >= 0x80000000 && cp->cp_eax <= cpi->cpi_xmaxeax &&
2307 	    cp->cp_eax < 0x80000000 + NMAX_CPI_EXTD)
2308 		xcp = &cpi->cpi_extd[cp->cp_eax - 0x80000000];
2309 	else
2310 		/*
2311 		 * The caller is asking for data from an input parameter which
2312 		 * the kernel has not cached.  In this case we go fetch from
2313 		 * the hardware and return the data directly to the user.
2314 		 */
2315 		return (__cpuid_insn(cp));
2316 
2317 	cp->cp_eax = xcp->cp_eax;
2318 	cp->cp_ebx = xcp->cp_ebx;
2319 	cp->cp_ecx = xcp->cp_ecx;
2320 	cp->cp_edx = xcp->cp_edx;
2321 	return (cp->cp_eax);
2322 }
2323 
2324 int
2325 cpuid_checkpass(cpu_t *cpu, int pass)
2326 {
2327 	return (cpu != NULL && cpu->cpu_m.mcpu_cpi != NULL &&
2328 	    cpu->cpu_m.mcpu_cpi->cpi_pass >= pass);
2329 }
2330 
2331 int
2332 cpuid_getbrandstr(cpu_t *cpu, char *s, size_t n)
2333 {
2334 	ASSERT(cpuid_checkpass(cpu, 3));
2335 
2336 	return (snprintf(s, n, "%s", cpu->cpu_m.mcpu_cpi->cpi_brandstr));
2337 }
2338 
2339 int
2340 cpuid_is_cmt(cpu_t *cpu)
2341 {
2342 	if (cpu == NULL)
2343 		cpu = CPU;
2344 
2345 	ASSERT(cpuid_checkpass(cpu, 1));
2346 
2347 	return (cpu->cpu_m.mcpu_cpi->cpi_chipid >= 0);
2348 }
2349 
2350 /*
2351  * AMD and Intel both implement the 64-bit variant of the syscall
2352  * instruction (syscallq), so if there's -any- support for syscall,
2353  * cpuid currently says "yes, we support this".
2354  *
2355  * However, Intel decided to -not- implement the 32-bit variant of the
2356  * syscall instruction, so we provide a predicate to allow our caller
2357  * to test that subtlety here.
2358  *
2359  * XXPV	Currently, 32-bit syscall instructions don't work via the hypervisor,
2360  *	even in the case where the hardware would in fact support it.
2361  */
2362 /*ARGSUSED*/
2363 int
2364 cpuid_syscall32_insn(cpu_t *cpu)
2365 {
2366 	ASSERT(cpuid_checkpass((cpu == NULL ? CPU : cpu), 1));
2367 
2368 #if !defined(__xpv)
2369 	if (cpu == NULL)
2370 		cpu = CPU;
2371 
2372 	/*CSTYLED*/
2373 	{
2374 		struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
2375 
2376 		if (cpi->cpi_vendor == X86_VENDOR_AMD &&
2377 		    cpi->cpi_xmaxeax >= 0x80000001 &&
2378 		    (CPI_FEATURES_XTD_EDX(cpi) & CPUID_AMD_EDX_SYSC))
2379 			return (1);
2380 	}
2381 #endif
2382 	return (0);
2383 }
2384 
2385 int
2386 cpuid_getidstr(cpu_t *cpu, char *s, size_t n)
2387 {
2388 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
2389 
2390 	static const char fmt[] =
2391 	    "x86 (%s %X family %d model %d step %d clock %d MHz)";
2392 	static const char fmt_ht[] =
2393 	    "x86 (chipid 0x%x %s %X family %d model %d step %d clock %d MHz)";
2394 
2395 	ASSERT(cpuid_checkpass(cpu, 1));
2396 
2397 	if (cpuid_is_cmt(cpu))
2398 		return (snprintf(s, n, fmt_ht, cpi->cpi_chipid,
2399 		    cpi->cpi_vendorstr, cpi->cpi_std[1].cp_eax,
2400 		    cpi->cpi_family, cpi->cpi_model,
2401 		    cpi->cpi_step, cpu->cpu_type_info.pi_clock));
2402 	return (snprintf(s, n, fmt,
2403 	    cpi->cpi_vendorstr, cpi->cpi_std[1].cp_eax,
2404 	    cpi->cpi_family, cpi->cpi_model,
2405 	    cpi->cpi_step, cpu->cpu_type_info.pi_clock));
2406 }
2407 
2408 const char *
2409 cpuid_getvendorstr(cpu_t *cpu)
2410 {
2411 	ASSERT(cpuid_checkpass(cpu, 1));
2412 	return ((const char *)cpu->cpu_m.mcpu_cpi->cpi_vendorstr);
2413 }
2414 
2415 uint_t
2416 cpuid_getvendor(cpu_t *cpu)
2417 {
2418 	ASSERT(cpuid_checkpass(cpu, 1));
2419 	return (cpu->cpu_m.mcpu_cpi->cpi_vendor);
2420 }
2421 
2422 uint_t
2423 cpuid_getfamily(cpu_t *cpu)
2424 {
2425 	ASSERT(cpuid_checkpass(cpu, 1));
2426 	return (cpu->cpu_m.mcpu_cpi->cpi_family);
2427 }
2428 
2429 uint_t
2430 cpuid_getmodel(cpu_t *cpu)
2431 {
2432 	ASSERT(cpuid_checkpass(cpu, 1));
2433 	return (cpu->cpu_m.mcpu_cpi->cpi_model);
2434 }
2435 
2436 uint_t
2437 cpuid_get_ncpu_per_chip(cpu_t *cpu)
2438 {
2439 	ASSERT(cpuid_checkpass(cpu, 1));
2440 	return (cpu->cpu_m.mcpu_cpi->cpi_ncpu_per_chip);
2441 }
2442 
2443 uint_t
2444 cpuid_get_ncore_per_chip(cpu_t *cpu)
2445 {
2446 	ASSERT(cpuid_checkpass(cpu, 1));
2447 	return (cpu->cpu_m.mcpu_cpi->cpi_ncore_per_chip);
2448 }
2449 
2450 uint_t
2451 cpuid_get_ncpu_sharing_last_cache(cpu_t *cpu)
2452 {
2453 	ASSERT(cpuid_checkpass(cpu, 2));
2454 	return (cpu->cpu_m.mcpu_cpi->cpi_ncpu_shr_last_cache);
2455 }
2456 
2457 id_t
2458 cpuid_get_last_lvl_cacheid(cpu_t *cpu)
2459 {
2460 	ASSERT(cpuid_checkpass(cpu, 2));
2461 	return (cpu->cpu_m.mcpu_cpi->cpi_last_lvl_cacheid);
2462 }
2463 
2464 uint_t
2465 cpuid_getstep(cpu_t *cpu)
2466 {
2467 	ASSERT(cpuid_checkpass(cpu, 1));
2468 	return (cpu->cpu_m.mcpu_cpi->cpi_step);
2469 }
2470 
2471 uint_t
2472 cpuid_getsig(struct cpu *cpu)
2473 {
2474 	ASSERT(cpuid_checkpass(cpu, 1));
2475 	return (cpu->cpu_m.mcpu_cpi->cpi_std[1].cp_eax);
2476 }
2477 
2478 uint32_t
2479 cpuid_getchiprev(struct cpu *cpu)
2480 {
2481 	ASSERT(cpuid_checkpass(cpu, 1));
2482 	return (cpu->cpu_m.mcpu_cpi->cpi_chiprev);
2483 }
2484 
2485 const char *
2486 cpuid_getchiprevstr(struct cpu *cpu)
2487 {
2488 	ASSERT(cpuid_checkpass(cpu, 1));
2489 	return (cpu->cpu_m.mcpu_cpi->cpi_chiprevstr);
2490 }
2491 
2492 uint32_t
2493 cpuid_getsockettype(struct cpu *cpu)
2494 {
2495 	ASSERT(cpuid_checkpass(cpu, 1));
2496 	return (cpu->cpu_m.mcpu_cpi->cpi_socket);
2497 }
2498 
2499 int
2500 cpuid_get_chipid(cpu_t *cpu)
2501 {
2502 	ASSERT(cpuid_checkpass(cpu, 1));
2503 
2504 	if (cpuid_is_cmt(cpu))
2505 		return (cpu->cpu_m.mcpu_cpi->cpi_chipid);
2506 	return (cpu->cpu_id);
2507 }
2508 
2509 id_t
2510 cpuid_get_coreid(cpu_t *cpu)
2511 {
2512 	ASSERT(cpuid_checkpass(cpu, 1));
2513 	return (cpu->cpu_m.mcpu_cpi->cpi_coreid);
2514 }
2515 
2516 int
2517 cpuid_get_pkgcoreid(cpu_t *cpu)
2518 {
2519 	ASSERT(cpuid_checkpass(cpu, 1));
2520 	return (cpu->cpu_m.mcpu_cpi->cpi_pkgcoreid);
2521 }
2522 
2523 int
2524 cpuid_get_clogid(cpu_t *cpu)
2525 {
2526 	ASSERT(cpuid_checkpass(cpu, 1));
2527 	return (cpu->cpu_m.mcpu_cpi->cpi_clogid);
2528 }
2529 
2530 void
2531 cpuid_get_addrsize(cpu_t *cpu, uint_t *pabits, uint_t *vabits)
2532 {
2533 	struct cpuid_info *cpi;
2534 
2535 	if (cpu == NULL)
2536 		cpu = CPU;
2537 	cpi = cpu->cpu_m.mcpu_cpi;
2538 
2539 	ASSERT(cpuid_checkpass(cpu, 1));
2540 
2541 	if (pabits)
2542 		*pabits = cpi->cpi_pabits;
2543 	if (vabits)
2544 		*vabits = cpi->cpi_vabits;
2545 }
2546 
2547 /*
2548  * Returns the number of data TLB entries for a corresponding
2549  * pagesize.  If it can't be computed, or isn't known, the
2550  * routine returns zero.  If you ask about an architecturally
2551  * impossible pagesize, the routine will panic (so that the
2552  * hat implementor knows that things are inconsistent.)
2553  */
2554 uint_t
2555 cpuid_get_dtlb_nent(cpu_t *cpu, size_t pagesize)
2556 {
2557 	struct cpuid_info *cpi;
2558 	uint_t dtlb_nent = 0;
2559 
2560 	if (cpu == NULL)
2561 		cpu = CPU;
2562 	cpi = cpu->cpu_m.mcpu_cpi;
2563 
2564 	ASSERT(cpuid_checkpass(cpu, 1));
2565 
2566 	/*
2567 	 * Check the L2 TLB info
2568 	 */
2569 	if (cpi->cpi_xmaxeax >= 0x80000006) {
2570 		struct cpuid_regs *cp = &cpi->cpi_extd[6];
2571 
2572 		switch (pagesize) {
2573 
2574 		case 4 * 1024:
2575 			/*
2576 			 * All zero in the top 16 bits of the register
2577 			 * indicates a unified TLB. Size is in low 16 bits.
2578 			 */
2579 			if ((cp->cp_ebx & 0xffff0000) == 0)
2580 				dtlb_nent = cp->cp_ebx & 0x0000ffff;
2581 			else
2582 				dtlb_nent = BITX(cp->cp_ebx, 27, 16);
2583 			break;
2584 
2585 		case 2 * 1024 * 1024:
2586 			if ((cp->cp_eax & 0xffff0000) == 0)
2587 				dtlb_nent = cp->cp_eax & 0x0000ffff;
2588 			else
2589 				dtlb_nent = BITX(cp->cp_eax, 27, 16);
2590 			break;
2591 
2592 		default:
2593 			panic("unknown L2 pagesize");
2594 			/*NOTREACHED*/
2595 		}
2596 	}
2597 
2598 	if (dtlb_nent != 0)
2599 		return (dtlb_nent);
2600 
2601 	/*
2602 	 * No L2 TLB support for this size, try L1.
2603 	 */
2604 	if (cpi->cpi_xmaxeax >= 0x80000005) {
2605 		struct cpuid_regs *cp = &cpi->cpi_extd[5];
2606 
2607 		switch (pagesize) {
2608 		case 4 * 1024:
2609 			dtlb_nent = BITX(cp->cp_ebx, 23, 16);
2610 			break;
2611 		case 2 * 1024 * 1024:
2612 			dtlb_nent = BITX(cp->cp_eax, 23, 16);
2613 			break;
2614 		default:
2615 			panic("unknown L1 d-TLB pagesize");
2616 			/*NOTREACHED*/
2617 		}
2618 	}
2619 
2620 	return (dtlb_nent);
2621 }
2622 
2623 uint_t
2624 cpuid_get_apicid(cpu_t *cpu)
2625 {
2626 	struct cpuid_info *cpi;
2627 
2628 	if (cpu == NULL)
2629 		cpu = CPU;
2630 	cpi = cpu->cpu_m.mcpu_cpi;
2631 
2632 	ASSERT(cpuid_checkpass(cpu, 1));
2633 	return (CPI_APIC_ID(cpi));
2634 }
2635 
2636 /*
2637  * Return 0 if the erratum is not present or not applicable, positive
2638  * if it is, and negative if the status of the erratum is unknown.
2639  *
2640  * See "Revision Guide for AMD Athlon(tm) 64 and AMD Opteron(tm)
2641  * Processors" #25759, Rev 3.57, August 2005
2642  */
2643 int
2644 cpuid_opteron_erratum(cpu_t *cpu, uint_t erratum)
2645 {
2646 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
2647 	uint_t eax;
2648 
2649 	/*
2650 	 * Bail out if this CPU isn't an AMD CPU, or if it's
2651 	 * a legacy (32-bit) AMD CPU.
2652 	 */
2653 	if (cpi->cpi_vendor != X86_VENDOR_AMD ||
2654 	    cpi->cpi_family == 4 || cpi->cpi_family == 5 ||
2655 	    cpi->cpi_family == 6)
2656 
2657 		return (0);
2658 
2659 	eax = cpi->cpi_std[1].cp_eax;
2660 
2661 #define	SH_B0(eax)	(eax == 0xf40 || eax == 0xf50)
2662 #define	SH_B3(eax) 	(eax == 0xf51)
2663 #define	B(eax)		(SH_B0(eax) || SH_B3(eax))
2664 
2665 #define	SH_C0(eax)	(eax == 0xf48 || eax == 0xf58)
2666 
2667 #define	SH_CG(eax)	(eax == 0xf4a || eax == 0xf5a || eax == 0xf7a)
2668 #define	DH_CG(eax)	(eax == 0xfc0 || eax == 0xfe0 || eax == 0xff0)
2669 #define	CH_CG(eax)	(eax == 0xf82 || eax == 0xfb2)
2670 #define	CG(eax)		(SH_CG(eax) || DH_CG(eax) || CH_CG(eax))
2671 
2672 #define	SH_D0(eax)	(eax == 0x10f40 || eax == 0x10f50 || eax == 0x10f70)
2673 #define	DH_D0(eax)	(eax == 0x10fc0 || eax == 0x10ff0)
2674 #define	CH_D0(eax)	(eax == 0x10f80 || eax == 0x10fb0)
2675 #define	D0(eax)		(SH_D0(eax) || DH_D0(eax) || CH_D0(eax))
2676 
2677 #define	SH_E0(eax)	(eax == 0x20f50 || eax == 0x20f40 || eax == 0x20f70)
2678 #define	JH_E1(eax)	(eax == 0x20f10)	/* JH8_E0 had 0x20f30 */
2679 #define	DH_E3(eax)	(eax == 0x20fc0 || eax == 0x20ff0)
2680 #define	SH_E4(eax)	(eax == 0x20f51 || eax == 0x20f71)
2681 #define	BH_E4(eax)	(eax == 0x20fb1)
2682 #define	SH_E5(eax)	(eax == 0x20f42)
2683 #define	DH_E6(eax)	(eax == 0x20ff2 || eax == 0x20fc2)
2684 #define	JH_E6(eax)	(eax == 0x20f12 || eax == 0x20f32)
2685 #define	EX(eax)		(SH_E0(eax) || JH_E1(eax) || DH_E3(eax) || \
2686 			    SH_E4(eax) || BH_E4(eax) || SH_E5(eax) || \
2687 			    DH_E6(eax) || JH_E6(eax))
2688 
2689 	switch (erratum) {
2690 	case 1:
2691 		return (cpi->cpi_family < 0x10);
2692 	case 51:	/* what does the asterisk mean? */
2693 		return (B(eax) || SH_C0(eax) || CG(eax));
2694 	case 52:
2695 		return (B(eax));
2696 	case 57:
2697 		return (cpi->cpi_family <= 0x10);
2698 	case 58:
2699 		return (B(eax));
2700 	case 60:
2701 		return (cpi->cpi_family <= 0x10);
2702 	case 61:
2703 	case 62:
2704 	case 63:
2705 	case 64:
2706 	case 65:
2707 	case 66:
2708 	case 68:
2709 	case 69:
2710 	case 70:
2711 	case 71:
2712 		return (B(eax));
2713 	case 72:
2714 		return (SH_B0(eax));
2715 	case 74:
2716 		return (B(eax));
2717 	case 75:
2718 		return (cpi->cpi_family < 0x10);
2719 	case 76:
2720 		return (B(eax));
2721 	case 77:
2722 		return (cpi->cpi_family <= 0x10);
2723 	case 78:
2724 		return (B(eax) || SH_C0(eax));
2725 	case 79:
2726 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax) || EX(eax));
2727 	case 80:
2728 	case 81:
2729 	case 82:
2730 		return (B(eax));
2731 	case 83:
2732 		return (B(eax) || SH_C0(eax) || CG(eax));
2733 	case 85:
2734 		return (cpi->cpi_family < 0x10);
2735 	case 86:
2736 		return (SH_C0(eax) || CG(eax));
2737 	case 88:
2738 #if !defined(__amd64)
2739 		return (0);
2740 #else
2741 		return (B(eax) || SH_C0(eax));
2742 #endif
2743 	case 89:
2744 		return (cpi->cpi_family < 0x10);
2745 	case 90:
2746 		return (B(eax) || SH_C0(eax) || CG(eax));
2747 	case 91:
2748 	case 92:
2749 		return (B(eax) || SH_C0(eax));
2750 	case 93:
2751 		return (SH_C0(eax));
2752 	case 94:
2753 		return (B(eax) || SH_C0(eax) || CG(eax));
2754 	case 95:
2755 #if !defined(__amd64)
2756 		return (0);
2757 #else
2758 		return (B(eax) || SH_C0(eax));
2759 #endif
2760 	case 96:
2761 		return (B(eax) || SH_C0(eax) || CG(eax));
2762 	case 97:
2763 	case 98:
2764 		return (SH_C0(eax) || CG(eax));
2765 	case 99:
2766 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax));
2767 	case 100:
2768 		return (B(eax) || SH_C0(eax));
2769 	case 101:
2770 	case 103:
2771 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax));
2772 	case 104:
2773 		return (SH_C0(eax) || CG(eax) || D0(eax));
2774 	case 105:
2775 	case 106:
2776 	case 107:
2777 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax));
2778 	case 108:
2779 		return (DH_CG(eax));
2780 	case 109:
2781 		return (SH_C0(eax) || CG(eax) || D0(eax));
2782 	case 110:
2783 		return (D0(eax) || EX(eax));
2784 	case 111:
2785 		return (CG(eax));
2786 	case 112:
2787 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax) || EX(eax));
2788 	case 113:
2789 		return (eax == 0x20fc0);
2790 	case 114:
2791 		return (SH_E0(eax) || JH_E1(eax) || DH_E3(eax));
2792 	case 115:
2793 		return (SH_E0(eax) || JH_E1(eax));
2794 	case 116:
2795 		return (SH_E0(eax) || JH_E1(eax) || DH_E3(eax));
2796 	case 117:
2797 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax));
2798 	case 118:
2799 		return (SH_E0(eax) || JH_E1(eax) || SH_E4(eax) || BH_E4(eax) ||
2800 		    JH_E6(eax));
2801 	case 121:
2802 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax) || EX(eax));
2803 	case 122:
2804 		return (cpi->cpi_family < 0x10);
2805 	case 123:
2806 		return (JH_E1(eax) || BH_E4(eax) || JH_E6(eax));
2807 	case 131:
2808 		return (cpi->cpi_family < 0x10);
2809 	case 6336786:
2810 		/*
2811 		 * Test for AdvPowerMgmtInfo.TscPStateInvariant
2812 		 * if this is a K8 family or newer processor
2813 		 */
2814 		if (CPI_FAMILY(cpi) == 0xf) {
2815 			struct cpuid_regs regs;
2816 			regs.cp_eax = 0x80000007;
2817 			(void) __cpuid_insn(&regs);
2818 			return (!(regs.cp_edx & 0x100));
2819 		}
2820 		return (0);
2821 	case 6323525:
2822 		return (((((eax >> 12) & 0xff00) + (eax & 0xf00)) |
2823 		    (((eax >> 4) & 0xf) | ((eax >> 12) & 0xf0))) < 0xf40);
2824 
2825 	default:
2826 		return (-1);
2827 	}
2828 }
2829 
2830 static const char assoc_str[] = "associativity";
2831 static const char line_str[] = "line-size";
2832 static const char size_str[] = "size";
2833 
2834 static void
2835 add_cache_prop(dev_info_t *devi, const char *label, const char *type,
2836     uint32_t val)
2837 {
2838 	char buf[128];
2839 
2840 	/*
2841 	 * ndi_prop_update_int() is used because it is desirable for
2842 	 * DDI_PROP_HW_DEF and DDI_PROP_DONTSLEEP to be set.
2843 	 */
2844 	if (snprintf(buf, sizeof (buf), "%s-%s", label, type) < sizeof (buf))
2845 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, devi, buf, val);
2846 }
2847 
2848 /*
2849  * Intel-style cache/tlb description
2850  *
2851  * Standard cpuid level 2 gives a randomly ordered
2852  * selection of tags that index into a table that describes
2853  * cache and tlb properties.
2854  */
2855 
2856 static const char l1_icache_str[] = "l1-icache";
2857 static const char l1_dcache_str[] = "l1-dcache";
2858 static const char l2_cache_str[] = "l2-cache";
2859 static const char l3_cache_str[] = "l3-cache";
2860 static const char itlb4k_str[] = "itlb-4K";
2861 static const char dtlb4k_str[] = "dtlb-4K";
2862 static const char itlb4M_str[] = "itlb-4M";
2863 static const char dtlb4M_str[] = "dtlb-4M";
2864 static const char dtlb24_str[] = "dtlb0-2M-4M";
2865 static const char itlb424_str[] = "itlb-4K-2M-4M";
2866 static const char itlb24_str[] = "itlb-2M-4M";
2867 static const char dtlb44_str[] = "dtlb-4K-4M";
2868 static const char sl1_dcache_str[] = "sectored-l1-dcache";
2869 static const char sl2_cache_str[] = "sectored-l2-cache";
2870 static const char itrace_str[] = "itrace-cache";
2871 static const char sl3_cache_str[] = "sectored-l3-cache";
2872 static const char sh_l2_tlb4k_str[] = "shared-l2-tlb-4k";
2873 
2874 static const struct cachetab {
2875 	uint8_t 	ct_code;
2876 	uint8_t		ct_assoc;
2877 	uint16_t 	ct_line_size;
2878 	size_t		ct_size;
2879 	const char	*ct_label;
2880 } intel_ctab[] = {
2881 	/* maintain descending order! */
2882 	{ 0xe4, 16, 64, 8*1024*1024, l3_cache_str},
2883 	{ 0xe3, 16, 64, 4*1024*1024, l3_cache_str},
2884 	{ 0xe2, 16, 64, 2*1024*1024, l3_cache_str},
2885 	{ 0xde, 12, 64, 6*1024*1024, l3_cache_str},
2886 	{ 0xdd, 12, 64, 3*1024*1024, l3_cache_str},
2887 	{ 0xdc, 12, 64, ((1*1024*1024)+(512*1024)), l3_cache_str},
2888 	{ 0xd8, 8, 64, 4*1024*1024, l3_cache_str},
2889 	{ 0xd7, 8, 64, 2*1024*1024, l3_cache_str},
2890 	{ 0xd6, 8, 64, 1*1024*1024, l3_cache_str},
2891 	{ 0xd2, 4, 64, 2*1024*1024, l3_cache_str},
2892 	{ 0xd1, 4, 64, 1*1024*1024, l3_cache_str},
2893 	{ 0xd0, 4, 64, 512*1024, l3_cache_str},
2894 	{ 0xca, 4, 0, 512, sh_l2_tlb4k_str},
2895 	{ 0xb4, 4, 0, 256, dtlb4k_str },
2896 	{ 0xb3, 4, 0, 128, dtlb4k_str },
2897 	{ 0xb2, 4, 0, 64, itlb4k_str },
2898 	{ 0xb0, 4, 0, 128, itlb4k_str },
2899 	{ 0x87, 8, 64, 1024*1024, l2_cache_str},
2900 	{ 0x86, 4, 64, 512*1024, l2_cache_str},
2901 	{ 0x85, 8, 32, 2*1024*1024, l2_cache_str},
2902 	{ 0x84, 8, 32, 1024*1024, l2_cache_str},
2903 	{ 0x83, 8, 32, 512*1024, l2_cache_str},
2904 	{ 0x82, 8, 32, 256*1024, l2_cache_str},
2905 	{ 0x7f, 2, 64, 512*1024, l2_cache_str},
2906 	{ 0x7d, 8, 64, 2*1024*1024, sl2_cache_str},
2907 	{ 0x7c, 8, 64, 1024*1024, sl2_cache_str},
2908 	{ 0x7b, 8, 64, 512*1024, sl2_cache_str},
2909 	{ 0x7a, 8, 64, 256*1024, sl2_cache_str},
2910 	{ 0x79, 8, 64, 128*1024, sl2_cache_str},
2911 	{ 0x78, 8, 64, 1024*1024, l2_cache_str},
2912 	{ 0x73, 8, 0, 64*1024, itrace_str},
2913 	{ 0x72, 8, 0, 32*1024, itrace_str},
2914 	{ 0x71, 8, 0, 16*1024, itrace_str},
2915 	{ 0x70, 8, 0, 12*1024, itrace_str},
2916 	{ 0x68, 4, 64, 32*1024, sl1_dcache_str},
2917 	{ 0x67, 4, 64, 16*1024, sl1_dcache_str},
2918 	{ 0x66, 4, 64, 8*1024, sl1_dcache_str},
2919 	{ 0x60, 8, 64, 16*1024, sl1_dcache_str},
2920 	{ 0x5d, 0, 0, 256, dtlb44_str},
2921 	{ 0x5c, 0, 0, 128, dtlb44_str},
2922 	{ 0x5b, 0, 0, 64, dtlb44_str},
2923 	{ 0x5a, 4, 0, 32, dtlb24_str},
2924 	{ 0x55, 0, 0, 7, itlb24_str},
2925 	{ 0x52, 0, 0, 256, itlb424_str},
2926 	{ 0x51, 0, 0, 128, itlb424_str},
2927 	{ 0x50, 0, 0, 64, itlb424_str},
2928 	{ 0x4d, 16, 64, 16*1024*1024, l3_cache_str},
2929 	{ 0x4c, 12, 64, 12*1024*1024, l3_cache_str},
2930 	{ 0x4b, 16, 64, 8*1024*1024, l3_cache_str},
2931 	{ 0x4a, 12, 64, 6*1024*1024, l3_cache_str},
2932 	{ 0x49, 16, 64, 4*1024*1024, l3_cache_str},
2933 	{ 0x47, 8, 64, 8*1024*1024, l3_cache_str},
2934 	{ 0x46, 4, 64, 4*1024*1024, l3_cache_str},
2935 	{ 0x45, 4, 32, 2*1024*1024, l2_cache_str},
2936 	{ 0x44, 4, 32, 1024*1024, l2_cache_str},
2937 	{ 0x43, 4, 32, 512*1024, l2_cache_str},
2938 	{ 0x42, 4, 32, 256*1024, l2_cache_str},
2939 	{ 0x41, 4, 32, 128*1024, l2_cache_str},
2940 	{ 0x3e, 4, 64, 512*1024, sl2_cache_str},
2941 	{ 0x3d, 6, 64, 384*1024, sl2_cache_str},
2942 	{ 0x3c, 4, 64, 256*1024, sl2_cache_str},
2943 	{ 0x3b, 2, 64, 128*1024, sl2_cache_str},
2944 	{ 0x3a, 6, 64, 192*1024, sl2_cache_str},
2945 	{ 0x39, 4, 64, 128*1024, sl2_cache_str},
2946 	{ 0x30, 8, 64, 32*1024, l1_icache_str},
2947 	{ 0x2c, 8, 64, 32*1024, l1_dcache_str},
2948 	{ 0x29, 8, 64, 4096*1024, sl3_cache_str},
2949 	{ 0x25, 8, 64, 2048*1024, sl3_cache_str},
2950 	{ 0x23, 8, 64, 1024*1024, sl3_cache_str},
2951 	{ 0x22, 4, 64, 512*1024, sl3_cache_str},
2952 	{ 0x0d, 4, 32, 16*1024, l1_dcache_str},
2953 	{ 0x0c, 4, 32, 16*1024, l1_dcache_str},
2954 	{ 0x0b, 4, 0, 4, itlb4M_str},
2955 	{ 0x0a, 2, 32, 8*1024, l1_dcache_str},
2956 	{ 0x08, 4, 32, 16*1024, l1_icache_str},
2957 	{ 0x06, 4, 32, 8*1024, l1_icache_str},
2958 	{ 0x04, 4, 0, 8, dtlb4M_str},
2959 	{ 0x03, 4, 0, 64, dtlb4k_str},
2960 	{ 0x02, 4, 0, 2, itlb4M_str},
2961 	{ 0x01, 4, 0, 32, itlb4k_str},
2962 	{ 0 }
2963 };
2964 
2965 static const struct cachetab cyrix_ctab[] = {
2966 	{ 0x70, 4, 0, 32, "tlb-4K" },
2967 	{ 0x80, 4, 16, 16*1024, "l1-cache" },
2968 	{ 0 }
2969 };
2970 
2971 /*
2972  * Search a cache table for a matching entry
2973  */
2974 static const struct cachetab *
2975 find_cacheent(const struct cachetab *ct, uint_t code)
2976 {
2977 	if (code != 0) {
2978 		for (; ct->ct_code != 0; ct++)
2979 			if (ct->ct_code <= code)
2980 				break;
2981 		if (ct->ct_code == code)
2982 			return (ct);
2983 	}
2984 	return (NULL);
2985 }
2986 
2987 /*
2988  * Populate cachetab entry with L2 or L3 cache-information using
2989  * cpuid function 4. This function is called from intel_walk_cacheinfo()
2990  * when descriptor 0x49 is encountered. It returns 0 if no such cache
2991  * information is found.
2992  */
2993 static int
2994 intel_cpuid_4_cache_info(struct cachetab *ct, struct cpuid_info *cpi)
2995 {
2996 	uint32_t level, i;
2997 	int ret = 0;
2998 
2999 	for (i = 0; i < cpi->cpi_std_4_size; i++) {
3000 		level = CPI_CACHE_LVL(cpi->cpi_std_4[i]);
3001 
3002 		if (level == 2 || level == 3) {
3003 			ct->ct_assoc = CPI_CACHE_WAYS(cpi->cpi_std_4[i]) + 1;
3004 			ct->ct_line_size =
3005 			    CPI_CACHE_COH_LN_SZ(cpi->cpi_std_4[i]) + 1;
3006 			ct->ct_size = ct->ct_assoc *
3007 			    (CPI_CACHE_PARTS(cpi->cpi_std_4[i]) + 1) *
3008 			    ct->ct_line_size *
3009 			    (cpi->cpi_std_4[i]->cp_ecx + 1);
3010 
3011 			if (level == 2) {
3012 				ct->ct_label = l2_cache_str;
3013 			} else if (level == 3) {
3014 				ct->ct_label = l3_cache_str;
3015 			}
3016 			ret = 1;
3017 		}
3018 	}
3019 
3020 	return (ret);
3021 }
3022 
3023 /*
3024  * Walk the cacheinfo descriptor, applying 'func' to every valid element
3025  * The walk is terminated if the walker returns non-zero.
3026  */
3027 static void
3028 intel_walk_cacheinfo(struct cpuid_info *cpi,
3029     void *arg, int (*func)(void *, const struct cachetab *))
3030 {
3031 	const struct cachetab *ct;
3032 	struct cachetab des_49_ct;
3033 	uint8_t *dp;
3034 	int i;
3035 
3036 	if ((dp = cpi->cpi_cacheinfo) == NULL)
3037 		return;
3038 	for (i = 0; i < cpi->cpi_ncache; i++, dp++) {
3039 		/*
3040 		 * For overloaded descriptor 0x49 we use cpuid function 4
3041 		 * if supported by the current processor, to create
3042 		 * cache information.
3043 		 */
3044 		if (*dp == 0x49 && cpi->cpi_maxeax >= 0x4 &&
3045 		    intel_cpuid_4_cache_info(&des_49_ct, cpi) == 1) {
3046 				ct = &des_49_ct;
3047 		} else {
3048 			if ((ct = find_cacheent(intel_ctab, *dp)) == NULL) {
3049 				continue;
3050 			}
3051 		}
3052 
3053 		if (func(arg, ct) != 0) {
3054 			break;
3055 		}
3056 	}
3057 }
3058 
3059 /*
3060  * (Like the Intel one, except for Cyrix CPUs)
3061  */
3062 static void
3063 cyrix_walk_cacheinfo(struct cpuid_info *cpi,
3064     void *arg, int (*func)(void *, const struct cachetab *))
3065 {
3066 	const struct cachetab *ct;
3067 	uint8_t *dp;
3068 	int i;
3069 
3070 	if ((dp = cpi->cpi_cacheinfo) == NULL)
3071 		return;
3072 	for (i = 0; i < cpi->cpi_ncache; i++, dp++) {
3073 		/*
3074 		 * Search Cyrix-specific descriptor table first ..
3075 		 */
3076 		if ((ct = find_cacheent(cyrix_ctab, *dp)) != NULL) {
3077 			if (func(arg, ct) != 0)
3078 				break;
3079 			continue;
3080 		}
3081 		/*
3082 		 * .. else fall back to the Intel one
3083 		 */
3084 		if ((ct = find_cacheent(intel_ctab, *dp)) != NULL) {
3085 			if (func(arg, ct) != 0)
3086 				break;
3087 			continue;
3088 		}
3089 	}
3090 }
3091 
3092 /*
3093  * A cacheinfo walker that adds associativity, line-size, and size properties
3094  * to the devinfo node it is passed as an argument.
3095  */
3096 static int
3097 add_cacheent_props(void *arg, const struct cachetab *ct)
3098 {
3099 	dev_info_t *devi = arg;
3100 
3101 	add_cache_prop(devi, ct->ct_label, assoc_str, ct->ct_assoc);
3102 	if (ct->ct_line_size != 0)
3103 		add_cache_prop(devi, ct->ct_label, line_str,
3104 		    ct->ct_line_size);
3105 	add_cache_prop(devi, ct->ct_label, size_str, ct->ct_size);
3106 	return (0);
3107 }
3108 
3109 
3110 static const char fully_assoc[] = "fully-associative?";
3111 
3112 /*
3113  * AMD style cache/tlb description
3114  *
3115  * Extended functions 5 and 6 directly describe properties of
3116  * tlbs and various cache levels.
3117  */
3118 static void
3119 add_amd_assoc(dev_info_t *devi, const char *label, uint_t assoc)
3120 {
3121 	switch (assoc) {
3122 	case 0:	/* reserved; ignore */
3123 		break;
3124 	default:
3125 		add_cache_prop(devi, label, assoc_str, assoc);
3126 		break;
3127 	case 0xff:
3128 		add_cache_prop(devi, label, fully_assoc, 1);
3129 		break;
3130 	}
3131 }
3132 
3133 static void
3134 add_amd_tlb(dev_info_t *devi, const char *label, uint_t assoc, uint_t size)
3135 {
3136 	if (size == 0)
3137 		return;
3138 	add_cache_prop(devi, label, size_str, size);
3139 	add_amd_assoc(devi, label, assoc);
3140 }
3141 
3142 static void
3143 add_amd_cache(dev_info_t *devi, const char *label,
3144     uint_t size, uint_t assoc, uint_t lines_per_tag, uint_t line_size)
3145 {
3146 	if (size == 0 || line_size == 0)
3147 		return;
3148 	add_amd_assoc(devi, label, assoc);
3149 	/*
3150 	 * Most AMD parts have a sectored cache. Multiple cache lines are
3151 	 * associated with each tag. A sector consists of all cache lines
3152 	 * associated with a tag. For example, the AMD K6-III has a sector
3153 	 * size of 2 cache lines per tag.
3154 	 */
3155 	if (lines_per_tag != 0)
3156 		add_cache_prop(devi, label, "lines-per-tag", lines_per_tag);
3157 	add_cache_prop(devi, label, line_str, line_size);
3158 	add_cache_prop(devi, label, size_str, size * 1024);
3159 }
3160 
3161 static void
3162 add_amd_l2_assoc(dev_info_t *devi, const char *label, uint_t assoc)
3163 {
3164 	switch (assoc) {
3165 	case 0:	/* off */
3166 		break;
3167 	case 1:
3168 	case 2:
3169 	case 4:
3170 		add_cache_prop(devi, label, assoc_str, assoc);
3171 		break;
3172 	case 6:
3173 		add_cache_prop(devi, label, assoc_str, 8);
3174 		break;
3175 	case 8:
3176 		add_cache_prop(devi, label, assoc_str, 16);
3177 		break;
3178 	case 0xf:
3179 		add_cache_prop(devi, label, fully_assoc, 1);
3180 		break;
3181 	default: /* reserved; ignore */
3182 		break;
3183 	}
3184 }
3185 
3186 static void
3187 add_amd_l2_tlb(dev_info_t *devi, const char *label, uint_t assoc, uint_t size)
3188 {
3189 	if (size == 0 || assoc == 0)
3190 		return;
3191 	add_amd_l2_assoc(devi, label, assoc);
3192 	add_cache_prop(devi, label, size_str, size);
3193 }
3194 
3195 static void
3196 add_amd_l2_cache(dev_info_t *devi, const char *label,
3197     uint_t size, uint_t assoc, uint_t lines_per_tag, uint_t line_size)
3198 {
3199 	if (size == 0 || assoc == 0 || line_size == 0)
3200 		return;
3201 	add_amd_l2_assoc(devi, label, assoc);
3202 	if (lines_per_tag != 0)
3203 		add_cache_prop(devi, label, "lines-per-tag", lines_per_tag);
3204 	add_cache_prop(devi, label, line_str, line_size);
3205 	add_cache_prop(devi, label, size_str, size * 1024);
3206 }
3207 
3208 static void
3209 amd_cache_info(struct cpuid_info *cpi, dev_info_t *devi)
3210 {
3211 	struct cpuid_regs *cp;
3212 
3213 	if (cpi->cpi_xmaxeax < 0x80000005)
3214 		return;
3215 	cp = &cpi->cpi_extd[5];
3216 
3217 	/*
3218 	 * 4M/2M L1 TLB configuration
3219 	 *
3220 	 * We report the size for 2M pages because AMD uses two
3221 	 * TLB entries for one 4M page.
3222 	 */
3223 	add_amd_tlb(devi, "dtlb-2M",
3224 	    BITX(cp->cp_eax, 31, 24), BITX(cp->cp_eax, 23, 16));
3225 	add_amd_tlb(devi, "itlb-2M",
3226 	    BITX(cp->cp_eax, 15, 8), BITX(cp->cp_eax, 7, 0));
3227 
3228 	/*
3229 	 * 4K L1 TLB configuration
3230 	 */
3231 
3232 	switch (cpi->cpi_vendor) {
3233 		uint_t nentries;
3234 	case X86_VENDOR_TM:
3235 		if (cpi->cpi_family >= 5) {
3236 			/*
3237 			 * Crusoe processors have 256 TLB entries, but
3238 			 * cpuid data format constrains them to only
3239 			 * reporting 255 of them.
3240 			 */
3241 			if ((nentries = BITX(cp->cp_ebx, 23, 16)) == 255)
3242 				nentries = 256;
3243 			/*
3244 			 * Crusoe processors also have a unified TLB
3245 			 */
3246 			add_amd_tlb(devi, "tlb-4K", BITX(cp->cp_ebx, 31, 24),
3247 			    nentries);
3248 			break;
3249 		}
3250 		/*FALLTHROUGH*/
3251 	default:
3252 		add_amd_tlb(devi, itlb4k_str,
3253 		    BITX(cp->cp_ebx, 31, 24), BITX(cp->cp_ebx, 23, 16));
3254 		add_amd_tlb(devi, dtlb4k_str,
3255 		    BITX(cp->cp_ebx, 15, 8), BITX(cp->cp_ebx, 7, 0));
3256 		break;
3257 	}
3258 
3259 	/*
3260 	 * data L1 cache configuration
3261 	 */
3262 
3263 	add_amd_cache(devi, l1_dcache_str,
3264 	    BITX(cp->cp_ecx, 31, 24), BITX(cp->cp_ecx, 23, 16),
3265 	    BITX(cp->cp_ecx, 15, 8), BITX(cp->cp_ecx, 7, 0));
3266 
3267 	/*
3268 	 * code L1 cache configuration
3269 	 */
3270 
3271 	add_amd_cache(devi, l1_icache_str,
3272 	    BITX(cp->cp_edx, 31, 24), BITX(cp->cp_edx, 23, 16),
3273 	    BITX(cp->cp_edx, 15, 8), BITX(cp->cp_edx, 7, 0));
3274 
3275 	if (cpi->cpi_xmaxeax < 0x80000006)
3276 		return;
3277 	cp = &cpi->cpi_extd[6];
3278 
3279 	/* Check for a unified L2 TLB for large pages */
3280 
3281 	if (BITX(cp->cp_eax, 31, 16) == 0)
3282 		add_amd_l2_tlb(devi, "l2-tlb-2M",
3283 		    BITX(cp->cp_eax, 15, 12), BITX(cp->cp_eax, 11, 0));
3284 	else {
3285 		add_amd_l2_tlb(devi, "l2-dtlb-2M",
3286 		    BITX(cp->cp_eax, 31, 28), BITX(cp->cp_eax, 27, 16));
3287 		add_amd_l2_tlb(devi, "l2-itlb-2M",
3288 		    BITX(cp->cp_eax, 15, 12), BITX(cp->cp_eax, 11, 0));
3289 	}
3290 
3291 	/* Check for a unified L2 TLB for 4K pages */
3292 
3293 	if (BITX(cp->cp_ebx, 31, 16) == 0) {
3294 		add_amd_l2_tlb(devi, "l2-tlb-4K",
3295 		    BITX(cp->cp_eax, 15, 12), BITX(cp->cp_eax, 11, 0));
3296 	} else {
3297 		add_amd_l2_tlb(devi, "l2-dtlb-4K",
3298 		    BITX(cp->cp_eax, 31, 28), BITX(cp->cp_eax, 27, 16));
3299 		add_amd_l2_tlb(devi, "l2-itlb-4K",
3300 		    BITX(cp->cp_eax, 15, 12), BITX(cp->cp_eax, 11, 0));
3301 	}
3302 
3303 	add_amd_l2_cache(devi, l2_cache_str,
3304 	    BITX(cp->cp_ecx, 31, 16), BITX(cp->cp_ecx, 15, 12),
3305 	    BITX(cp->cp_ecx, 11, 8), BITX(cp->cp_ecx, 7, 0));
3306 }
3307 
3308 /*
3309  * There are two basic ways that the x86 world describes it cache
3310  * and tlb architecture - Intel's way and AMD's way.
3311  *
3312  * Return which flavor of cache architecture we should use
3313  */
3314 static int
3315 x86_which_cacheinfo(struct cpuid_info *cpi)
3316 {
3317 	switch (cpi->cpi_vendor) {
3318 	case X86_VENDOR_Intel:
3319 		if (cpi->cpi_maxeax >= 2)
3320 			return (X86_VENDOR_Intel);
3321 		break;
3322 	case X86_VENDOR_AMD:
3323 		/*
3324 		 * The K5 model 1 was the first part from AMD that reported
3325 		 * cache sizes via extended cpuid functions.
3326 		 */
3327 		if (cpi->cpi_family > 5 ||
3328 		    (cpi->cpi_family == 5 && cpi->cpi_model >= 1))
3329 			return (X86_VENDOR_AMD);
3330 		break;
3331 	case X86_VENDOR_TM:
3332 		if (cpi->cpi_family >= 5)
3333 			return (X86_VENDOR_AMD);
3334 		/*FALLTHROUGH*/
3335 	default:
3336 		/*
3337 		 * If they have extended CPU data for 0x80000005
3338 		 * then we assume they have AMD-format cache
3339 		 * information.
3340 		 *
3341 		 * If not, and the vendor happens to be Cyrix,
3342 		 * then try our-Cyrix specific handler.
3343 		 *
3344 		 * If we're not Cyrix, then assume we're using Intel's
3345 		 * table-driven format instead.
3346 		 */
3347 		if (cpi->cpi_xmaxeax >= 0x80000005)
3348 			return (X86_VENDOR_AMD);
3349 		else if (cpi->cpi_vendor == X86_VENDOR_Cyrix)
3350 			return (X86_VENDOR_Cyrix);
3351 		else if (cpi->cpi_maxeax >= 2)
3352 			return (X86_VENDOR_Intel);
3353 		break;
3354 	}
3355 	return (-1);
3356 }
3357 
3358 /*
3359  * create a node for the given cpu under the prom root node.
3360  * Also, create a cpu node in the device tree.
3361  */
3362 static dev_info_t *cpu_nex_devi = NULL;
3363 static kmutex_t cpu_node_lock;
3364 
3365 /*
3366  * Called from post_startup() and mp_startup()
3367  */
3368 void
3369 add_cpunode2devtree(processorid_t cpu_id, struct cpuid_info *cpi)
3370 {
3371 	dev_info_t *cpu_devi;
3372 	int create;
3373 
3374 	mutex_enter(&cpu_node_lock);
3375 
3376 	/*
3377 	 * create a nexus node for all cpus identified as 'cpu_id' under
3378 	 * the root node.
3379 	 */
3380 	if (cpu_nex_devi == NULL) {
3381 		if (ndi_devi_alloc(ddi_root_node(), "cpus",
3382 		    (pnode_t)DEVI_SID_NODEID, &cpu_nex_devi) != NDI_SUCCESS) {
3383 			mutex_exit(&cpu_node_lock);
3384 			return;
3385 		}
3386 		(void) ndi_devi_online(cpu_nex_devi, 0);
3387 	}
3388 
3389 	/*
3390 	 * create a child node for cpu identified as 'cpu_id'
3391 	 */
3392 	cpu_devi = ddi_add_child(cpu_nex_devi, "cpu", DEVI_SID_NODEID,
3393 	    cpu_id);
3394 	if (cpu_devi == NULL) {
3395 		mutex_exit(&cpu_node_lock);
3396 		return;
3397 	}
3398 
3399 	/* device_type */
3400 
3401 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, cpu_devi,
3402 	    "device_type", "cpu");
3403 
3404 	/* reg */
3405 
3406 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3407 	    "reg", cpu_id);
3408 
3409 	/* cpu-mhz, and clock-frequency */
3410 
3411 	if (cpu_freq > 0) {
3412 		long long mul;
3413 
3414 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3415 		    "cpu-mhz", cpu_freq);
3416 
3417 		if ((mul = cpu_freq * 1000000LL) <= INT_MAX)
3418 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3419 			    "clock-frequency", (int)mul);
3420 	}
3421 
3422 	(void) ndi_devi_online(cpu_devi, 0);
3423 
3424 	if ((x86_feature & X86_CPUID) == 0) {
3425 		mutex_exit(&cpu_node_lock);
3426 		return;
3427 	}
3428 
3429 	/* vendor-id */
3430 
3431 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, cpu_devi,
3432 	    "vendor-id", cpi->cpi_vendorstr);
3433 
3434 	if (cpi->cpi_maxeax == 0) {
3435 		mutex_exit(&cpu_node_lock);
3436 		return;
3437 	}
3438 
3439 	/*
3440 	 * family, model, and step
3441 	 */
3442 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3443 	    "family", CPI_FAMILY(cpi));
3444 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3445 	    "cpu-model", CPI_MODEL(cpi));
3446 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3447 	    "stepping-id", CPI_STEP(cpi));
3448 
3449 	/* type */
3450 
3451 	switch (cpi->cpi_vendor) {
3452 	case X86_VENDOR_Intel:
3453 		create = 1;
3454 		break;
3455 	default:
3456 		create = 0;
3457 		break;
3458 	}
3459 	if (create)
3460 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3461 		    "type", CPI_TYPE(cpi));
3462 
3463 	/* ext-family */
3464 
3465 	switch (cpi->cpi_vendor) {
3466 	case X86_VENDOR_Intel:
3467 	case X86_VENDOR_AMD:
3468 		create = cpi->cpi_family >= 0xf;
3469 		break;
3470 	default:
3471 		create = 0;
3472 		break;
3473 	}
3474 	if (create)
3475 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3476 		    "ext-family", CPI_FAMILY_XTD(cpi));
3477 
3478 	/* ext-model */
3479 
3480 	switch (cpi->cpi_vendor) {
3481 	case X86_VENDOR_Intel:
3482 		create = IS_EXTENDED_MODEL_INTEL(cpi);
3483 		break;
3484 	case X86_VENDOR_AMD:
3485 		create = CPI_FAMILY(cpi) == 0xf;
3486 		break;
3487 	default:
3488 		create = 0;
3489 		break;
3490 	}
3491 	if (create)
3492 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3493 		    "ext-model", CPI_MODEL_XTD(cpi));
3494 
3495 	/* generation */
3496 
3497 	switch (cpi->cpi_vendor) {
3498 	case X86_VENDOR_AMD:
3499 		/*
3500 		 * AMD K5 model 1 was the first part to support this
3501 		 */
3502 		create = cpi->cpi_xmaxeax >= 0x80000001;
3503 		break;
3504 	default:
3505 		create = 0;
3506 		break;
3507 	}
3508 	if (create)
3509 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3510 		    "generation", BITX((cpi)->cpi_extd[1].cp_eax, 11, 8));
3511 
3512 	/* brand-id */
3513 
3514 	switch (cpi->cpi_vendor) {
3515 	case X86_VENDOR_Intel:
3516 		/*
3517 		 * brand id first appeared on Pentium III Xeon model 8,
3518 		 * and Celeron model 8 processors and Opteron
3519 		 */
3520 		create = cpi->cpi_family > 6 ||
3521 		    (cpi->cpi_family == 6 && cpi->cpi_model >= 8);
3522 		break;
3523 	case X86_VENDOR_AMD:
3524 		create = cpi->cpi_family >= 0xf;
3525 		break;
3526 	default:
3527 		create = 0;
3528 		break;
3529 	}
3530 	if (create && cpi->cpi_brandid != 0) {
3531 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3532 		    "brand-id", cpi->cpi_brandid);
3533 	}
3534 
3535 	/* chunks, and apic-id */
3536 
3537 	switch (cpi->cpi_vendor) {
3538 		/*
3539 		 * first available on Pentium IV and Opteron (K8)
3540 		 */
3541 	case X86_VENDOR_Intel:
3542 		create = IS_NEW_F6(cpi) || cpi->cpi_family >= 0xf;
3543 		break;
3544 	case X86_VENDOR_AMD:
3545 		create = cpi->cpi_family >= 0xf;
3546 		break;
3547 	default:
3548 		create = 0;
3549 		break;
3550 	}
3551 	if (create) {
3552 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3553 		    "chunks", CPI_CHUNKS(cpi));
3554 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3555 		    "apic-id", CPI_APIC_ID(cpi));
3556 		if (cpi->cpi_chipid >= 0) {
3557 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3558 			    "chip#", cpi->cpi_chipid);
3559 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3560 			    "clog#", cpi->cpi_clogid);
3561 		}
3562 	}
3563 
3564 	/* cpuid-features */
3565 
3566 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3567 	    "cpuid-features", CPI_FEATURES_EDX(cpi));
3568 
3569 
3570 	/* cpuid-features-ecx */
3571 
3572 	switch (cpi->cpi_vendor) {
3573 	case X86_VENDOR_Intel:
3574 		create = IS_NEW_F6(cpi) || cpi->cpi_family >= 0xf;
3575 		break;
3576 	default:
3577 		create = 0;
3578 		break;
3579 	}
3580 	if (create)
3581 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3582 		    "cpuid-features-ecx", CPI_FEATURES_ECX(cpi));
3583 
3584 	/* ext-cpuid-features */
3585 
3586 	switch (cpi->cpi_vendor) {
3587 	case X86_VENDOR_Intel:
3588 	case X86_VENDOR_AMD:
3589 	case X86_VENDOR_Cyrix:
3590 	case X86_VENDOR_TM:
3591 	case X86_VENDOR_Centaur:
3592 		create = cpi->cpi_xmaxeax >= 0x80000001;
3593 		break;
3594 	default:
3595 		create = 0;
3596 		break;
3597 	}
3598 	if (create) {
3599 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3600 		    "ext-cpuid-features", CPI_FEATURES_XTD_EDX(cpi));
3601 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3602 		    "ext-cpuid-features-ecx", CPI_FEATURES_XTD_ECX(cpi));
3603 	}
3604 
3605 	/*
3606 	 * Brand String first appeared in Intel Pentium IV, AMD K5
3607 	 * model 1, and Cyrix GXm.  On earlier models we try and
3608 	 * simulate something similar .. so this string should always
3609 	 * same -something- about the processor, however lame.
3610 	 */
3611 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, cpu_devi,
3612 	    "brand-string", cpi->cpi_brandstr);
3613 
3614 	/*
3615 	 * Finally, cache and tlb information
3616 	 */
3617 	switch (x86_which_cacheinfo(cpi)) {
3618 	case X86_VENDOR_Intel:
3619 		intel_walk_cacheinfo(cpi, cpu_devi, add_cacheent_props);
3620 		break;
3621 	case X86_VENDOR_Cyrix:
3622 		cyrix_walk_cacheinfo(cpi, cpu_devi, add_cacheent_props);
3623 		break;
3624 	case X86_VENDOR_AMD:
3625 		amd_cache_info(cpi, cpu_devi);
3626 		break;
3627 	default:
3628 		break;
3629 	}
3630 
3631 	mutex_exit(&cpu_node_lock);
3632 }
3633 
3634 struct l2info {
3635 	int *l2i_csz;
3636 	int *l2i_lsz;
3637 	int *l2i_assoc;
3638 	int l2i_ret;
3639 };
3640 
3641 /*
3642  * A cacheinfo walker that fetches the size, line-size and associativity
3643  * of the L2 cache
3644  */
3645 static int
3646 intel_l2cinfo(void *arg, const struct cachetab *ct)
3647 {
3648 	struct l2info *l2i = arg;
3649 	int *ip;
3650 
3651 	if (ct->ct_label != l2_cache_str &&
3652 	    ct->ct_label != sl2_cache_str)
3653 		return (0);	/* not an L2 -- keep walking */
3654 
3655 	if ((ip = l2i->l2i_csz) != NULL)
3656 		*ip = ct->ct_size;
3657 	if ((ip = l2i->l2i_lsz) != NULL)
3658 		*ip = ct->ct_line_size;
3659 	if ((ip = l2i->l2i_assoc) != NULL)
3660 		*ip = ct->ct_assoc;
3661 	l2i->l2i_ret = ct->ct_size;
3662 	return (1);		/* was an L2 -- terminate walk */
3663 }
3664 
3665 /*
3666  * AMD L2/L3 Cache and TLB Associativity Field Definition:
3667  *
3668  *	Unlike the associativity for the L1 cache and tlb where the 8 bit
3669  *	value is the associativity, the associativity for the L2 cache and
3670  *	tlb is encoded in the following table. The 4 bit L2 value serves as
3671  *	an index into the amd_afd[] array to determine the associativity.
3672  *	-1 is undefined. 0 is fully associative.
3673  */
3674 
3675 static int amd_afd[] =
3676 	{-1, 1, 2, -1, 4, -1, 8, -1, 16, -1, 32, 48, 64, 96, 128, 0};
3677 
3678 static void
3679 amd_l2cacheinfo(struct cpuid_info *cpi, struct l2info *l2i)
3680 {
3681 	struct cpuid_regs *cp;
3682 	uint_t size, assoc;
3683 	int i;
3684 	int *ip;
3685 
3686 	if (cpi->cpi_xmaxeax < 0x80000006)
3687 		return;
3688 	cp = &cpi->cpi_extd[6];
3689 
3690 	if ((i = BITX(cp->cp_ecx, 15, 12)) != 0 &&
3691 	    (size = BITX(cp->cp_ecx, 31, 16)) != 0) {
3692 		uint_t cachesz = size * 1024;
3693 		assoc = amd_afd[i];
3694 
3695 		ASSERT(assoc != -1);
3696 
3697 		if ((ip = l2i->l2i_csz) != NULL)
3698 			*ip = cachesz;
3699 		if ((ip = l2i->l2i_lsz) != NULL)
3700 			*ip = BITX(cp->cp_ecx, 7, 0);
3701 		if ((ip = l2i->l2i_assoc) != NULL)
3702 			*ip = assoc;
3703 		l2i->l2i_ret = cachesz;
3704 	}
3705 }
3706 
3707 int
3708 getl2cacheinfo(cpu_t *cpu, int *csz, int *lsz, int *assoc)
3709 {
3710 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
3711 	struct l2info __l2info, *l2i = &__l2info;
3712 
3713 	l2i->l2i_csz = csz;
3714 	l2i->l2i_lsz = lsz;
3715 	l2i->l2i_assoc = assoc;
3716 	l2i->l2i_ret = -1;
3717 
3718 	switch (x86_which_cacheinfo(cpi)) {
3719 	case X86_VENDOR_Intel:
3720 		intel_walk_cacheinfo(cpi, l2i, intel_l2cinfo);
3721 		break;
3722 	case X86_VENDOR_Cyrix:
3723 		cyrix_walk_cacheinfo(cpi, l2i, intel_l2cinfo);
3724 		break;
3725 	case X86_VENDOR_AMD:
3726 		amd_l2cacheinfo(cpi, l2i);
3727 		break;
3728 	default:
3729 		break;
3730 	}
3731 	return (l2i->l2i_ret);
3732 }
3733 
3734 #if !defined(__xpv)
3735 
3736 uint32_t *
3737 cpuid_mwait_alloc(cpu_t *cpu)
3738 {
3739 	uint32_t	*ret;
3740 	size_t		mwait_size;
3741 
3742 	ASSERT(cpuid_checkpass(cpu, 2));
3743 
3744 	mwait_size = cpu->cpu_m.mcpu_cpi->cpi_mwait.mon_max;
3745 	if (mwait_size == 0)
3746 		return (NULL);
3747 
3748 	/*
3749 	 * kmem_alloc() returns cache line size aligned data for mwait_size
3750 	 * allocations.  mwait_size is currently cache line sized.  Neither
3751 	 * of these implementation details are guarantied to be true in the
3752 	 * future.
3753 	 *
3754 	 * First try allocating mwait_size as kmem_alloc() currently returns
3755 	 * correctly aligned memory.  If kmem_alloc() does not return
3756 	 * mwait_size aligned memory, then use mwait_size ROUNDUP.
3757 	 *
3758 	 * Set cpi_mwait.buf_actual and cpi_mwait.size_actual in case we
3759 	 * decide to free this memory.
3760 	 */
3761 	ret = kmem_zalloc(mwait_size, KM_SLEEP);
3762 	if (ret == (uint32_t *)P2ROUNDUP((uintptr_t)ret, mwait_size)) {
3763 		cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual = ret;
3764 		cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual = mwait_size;
3765 		*ret = MWAIT_RUNNING;
3766 		return (ret);
3767 	} else {
3768 		kmem_free(ret, mwait_size);
3769 		ret = kmem_zalloc(mwait_size * 2, KM_SLEEP);
3770 		cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual = ret;
3771 		cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual = mwait_size * 2;
3772 		ret = (uint32_t *)P2ROUNDUP((uintptr_t)ret, mwait_size);
3773 		*ret = MWAIT_RUNNING;
3774 		return (ret);
3775 	}
3776 }
3777 
3778 void
3779 cpuid_mwait_free(cpu_t *cpu)
3780 {
3781 	ASSERT(cpuid_checkpass(cpu, 2));
3782 
3783 	if (cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual != NULL &&
3784 	    cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual > 0) {
3785 		kmem_free(cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual,
3786 		    cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual);
3787 	}
3788 
3789 	cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual = NULL;
3790 	cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual = 0;
3791 }
3792 
3793 void
3794 patch_tsc_read(int flag)
3795 {
3796 	size_t cnt;
3797 	switch (flag) {
3798 	case X86_NO_TSC:
3799 		cnt = &_no_rdtsc_end - &_no_rdtsc_start;
3800 		(void) memcpy((void *)tsc_read, (void *)&_no_rdtsc_start, cnt);
3801 		break;
3802 	case X86_HAVE_TSCP:
3803 		cnt = &_tscp_end - &_tscp_start;
3804 		(void) memcpy((void *)tsc_read, (void *)&_tscp_start, cnt);
3805 		break;
3806 	case X86_TSC_MFENCE:
3807 		cnt = &_tsc_mfence_end - &_tsc_mfence_start;
3808 		(void) memcpy((void *)tsc_read,
3809 		    (void *)&_tsc_mfence_start, cnt);
3810 		break;
3811 	default:
3812 		break;
3813 	}
3814 }
3815 
3816 #endif	/* !__xpv */
3817