xref: /titanic_50/usr/src/uts/i86pc/os/cpuid.c (revision c7158ae983f5a04c4a998f468ecefba6d23ba721)
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 (cp->cp_edx & CPUID_AMD_EDX_TSCP)
1096 				feature |= X86_TSCP;
1097 			break;
1098 		default:
1099 			break;
1100 		}
1101 
1102 		/*
1103 		 * Get CPUID data about processor cores and hyperthreads.
1104 		 */
1105 		switch (cpi->cpi_vendor) {
1106 		case X86_VENDOR_Intel:
1107 			if (cpi->cpi_maxeax >= 4) {
1108 				cp = &cpi->cpi_std[4];
1109 				cp->cp_eax = 4;
1110 				cp->cp_ecx = 0;
1111 				(void) __cpuid_insn(cp);
1112 				platform_cpuid_mangle(cpi->cpi_vendor, 4, cp);
1113 			}
1114 			/*FALLTHROUGH*/
1115 		case X86_VENDOR_AMD:
1116 			if (cpi->cpi_xmaxeax < 0x80000008)
1117 				break;
1118 			cp = &cpi->cpi_extd[8];
1119 			cp->cp_eax = 0x80000008;
1120 			(void) __cpuid_insn(cp);
1121 			platform_cpuid_mangle(cpi->cpi_vendor, 0x80000008, cp);
1122 
1123 			/*
1124 			 * Virtual and physical address limits from
1125 			 * cpuid override previously guessed values.
1126 			 */
1127 			cpi->cpi_pabits = BITX(cp->cp_eax, 7, 0);
1128 			cpi->cpi_vabits = BITX(cp->cp_eax, 15, 8);
1129 			break;
1130 		default:
1131 			break;
1132 		}
1133 
1134 		/*
1135 		 * Derive the number of cores per chip
1136 		 */
1137 		switch (cpi->cpi_vendor) {
1138 		case X86_VENDOR_Intel:
1139 			if (cpi->cpi_maxeax < 4) {
1140 				cpi->cpi_ncore_per_chip = 1;
1141 				break;
1142 			} else {
1143 				cpi->cpi_ncore_per_chip =
1144 				    BITX((cpi)->cpi_std[4].cp_eax, 31, 26) + 1;
1145 			}
1146 			break;
1147 		case X86_VENDOR_AMD:
1148 			if (cpi->cpi_xmaxeax < 0x80000008) {
1149 				cpi->cpi_ncore_per_chip = 1;
1150 				break;
1151 			} else {
1152 				/*
1153 				 * On family 0xf cpuid fn 2 ECX[7:0] "NC" is
1154 				 * 1 less than the number of physical cores on
1155 				 * the chip.  In family 0x10 this value can
1156 				 * be affected by "downcoring" - it reflects
1157 				 * 1 less than the number of cores actually
1158 				 * enabled on this node.
1159 				 */
1160 				cpi->cpi_ncore_per_chip =
1161 				    BITX((cpi)->cpi_extd[8].cp_ecx, 7, 0) + 1;
1162 			}
1163 			break;
1164 		default:
1165 			cpi->cpi_ncore_per_chip = 1;
1166 			break;
1167 		}
1168 	} else {
1169 		cpi->cpi_ncore_per_chip = 1;
1170 	}
1171 
1172 	/*
1173 	 * If more than one core, then this processor is CMP.
1174 	 */
1175 	if (cpi->cpi_ncore_per_chip > 1)
1176 		feature |= X86_CMP;
1177 
1178 	/*
1179 	 * If the number of cores is the same as the number
1180 	 * of CPUs, then we cannot have HyperThreading.
1181 	 */
1182 	if (cpi->cpi_ncpu_per_chip == cpi->cpi_ncore_per_chip)
1183 		feature &= ~X86_HTT;
1184 
1185 	if ((feature & (X86_HTT | X86_CMP)) == 0) {
1186 		/*
1187 		 * Single-core single-threaded processors.
1188 		 */
1189 		cpi->cpi_chipid = -1;
1190 		cpi->cpi_clogid = 0;
1191 		cpi->cpi_coreid = cpu->cpu_id;
1192 		cpi->cpi_pkgcoreid = 0;
1193 	} else if (cpi->cpi_ncpu_per_chip > 1) {
1194 		uint_t i;
1195 		uint_t chipid_shift = 0;
1196 		uint_t coreid_shift = 0;
1197 		uint_t apic_id = CPI_APIC_ID(cpi);
1198 
1199 		for (i = 1; i < cpi->cpi_ncpu_per_chip; i <<= 1)
1200 			chipid_shift++;
1201 		cpi->cpi_chipid = apic_id >> chipid_shift;
1202 		cpi->cpi_clogid = apic_id & ((1 << chipid_shift) - 1);
1203 
1204 		if (cpi->cpi_vendor == X86_VENDOR_Intel) {
1205 			if (feature & X86_CMP) {
1206 				/*
1207 				 * Multi-core (and possibly multi-threaded)
1208 				 * processors.
1209 				 */
1210 				uint_t ncpu_per_core;
1211 				if (cpi->cpi_ncore_per_chip == 1)
1212 					ncpu_per_core = cpi->cpi_ncpu_per_chip;
1213 				else if (cpi->cpi_ncore_per_chip > 1)
1214 					ncpu_per_core = cpi->cpi_ncpu_per_chip /
1215 					    cpi->cpi_ncore_per_chip;
1216 				/*
1217 				 * 8bit APIC IDs on dual core Pentiums
1218 				 * look like this:
1219 				 *
1220 				 * +-----------------------+------+------+
1221 				 * | Physical Package ID   |  MC  |  HT  |
1222 				 * +-----------------------+------+------+
1223 				 * <------- chipid -------->
1224 				 * <------- coreid --------------->
1225 				 *			   <--- clogid -->
1226 				 *			   <------>
1227 				 *			   pkgcoreid
1228 				 *
1229 				 * Where the number of bits necessary to
1230 				 * represent MC and HT fields together equals
1231 				 * to the minimum number of bits necessary to
1232 				 * store the value of cpi->cpi_ncpu_per_chip.
1233 				 * Of those bits, the MC part uses the number
1234 				 * of bits necessary to store the value of
1235 				 * cpi->cpi_ncore_per_chip.
1236 				 */
1237 				for (i = 1; i < ncpu_per_core; i <<= 1)
1238 					coreid_shift++;
1239 				cpi->cpi_coreid = apic_id >> coreid_shift;
1240 				cpi->cpi_pkgcoreid = cpi->cpi_clogid >>
1241 				    coreid_shift;
1242 			} else if (feature & X86_HTT) {
1243 				/*
1244 				 * Single-core multi-threaded processors.
1245 				 */
1246 				cpi->cpi_coreid = cpi->cpi_chipid;
1247 				cpi->cpi_pkgcoreid = 0;
1248 			}
1249 		} else if (cpi->cpi_vendor == X86_VENDOR_AMD) {
1250 			/*
1251 			 * AMD CMP chips currently have a single thread per
1252 			 * core, with 2 cores on family 0xf and 2, 3 or 4
1253 			 * cores on family 0x10.
1254 			 *
1255 			 * Since no two cpus share a core we must assign a
1256 			 * distinct coreid per cpu, and we do this by using
1257 			 * the cpu_id.  This scheme does not, however,
1258 			 * guarantee that sibling cores of a chip will have
1259 			 * sequential coreids starting at a multiple of the
1260 			 * number of cores per chip - that is usually the
1261 			 * case, but if the ACPI MADT table is presented
1262 			 * in a different order then we need to perform a
1263 			 * few more gymnastics for the pkgcoreid.
1264 			 *
1265 			 * In family 0xf CMPs there are 2 cores on all nodes
1266 			 * present - no mixing of single and dual core parts.
1267 			 *
1268 			 * In family 0x10 CMPs cpuid fn 2 ECX[15:12]
1269 			 * "ApicIdCoreIdSize[3:0]" tells us how
1270 			 * many least-significant bits in the ApicId
1271 			 * are used to represent the core number
1272 			 * within the node.  Cores are always
1273 			 * numbered sequentially from 0 regardless
1274 			 * of how many or which are disabled, and
1275 			 * there seems to be no way to discover the
1276 			 * real core id when some are disabled.
1277 			 */
1278 			cpi->cpi_coreid = cpu->cpu_id;
1279 
1280 			if (cpi->cpi_family == 0x10 &&
1281 			    cpi->cpi_xmaxeax >= 0x80000008) {
1282 				int coreidsz =
1283 				    BITX((cpi)->cpi_extd[8].cp_ecx, 15, 12);
1284 
1285 				cpi->cpi_pkgcoreid =
1286 				    apic_id & ((1 << coreidsz) - 1);
1287 			} else {
1288 				cpi->cpi_pkgcoreid = cpi->cpi_clogid;
1289 			}
1290 		} else {
1291 			/*
1292 			 * All other processors are currently
1293 			 * assumed to have single cores.
1294 			 */
1295 			cpi->cpi_coreid = cpi->cpi_chipid;
1296 			cpi->cpi_pkgcoreid = 0;
1297 		}
1298 	}
1299 
1300 	/*
1301 	 * Synthesize chip "revision" and socket type
1302 	 */
1303 	synth_info(cpi);
1304 
1305 pass1_done:
1306 #if !defined(__xpv)
1307 	check_for_hvm();
1308 #endif
1309 	cpi->cpi_pass = 1;
1310 	return (feature);
1311 }
1312 
1313 /*
1314  * Make copies of the cpuid table entries we depend on, in
1315  * part for ease of parsing now, in part so that we have only
1316  * one place to correct any of it, in part for ease of
1317  * later export to userland, and in part so we can look at
1318  * this stuff in a crash dump.
1319  */
1320 
1321 /*ARGSUSED*/
1322 void
1323 cpuid_pass2(cpu_t *cpu)
1324 {
1325 	uint_t n, nmax;
1326 	int i;
1327 	struct cpuid_regs *cp;
1328 	uint8_t *dp;
1329 	uint32_t *iptr;
1330 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
1331 
1332 	ASSERT(cpi->cpi_pass == 1);
1333 
1334 	if (cpi->cpi_maxeax < 1)
1335 		goto pass2_done;
1336 
1337 	if ((nmax = cpi->cpi_maxeax + 1) > NMAX_CPI_STD)
1338 		nmax = NMAX_CPI_STD;
1339 	/*
1340 	 * (We already handled n == 0 and n == 1 in pass 1)
1341 	 */
1342 	for (n = 2, cp = &cpi->cpi_std[2]; n < nmax; n++, cp++) {
1343 		cp->cp_eax = n;
1344 
1345 		/*
1346 		 * CPUID function 4 expects %ecx to be initialized
1347 		 * with an index which indicates which cache to return
1348 		 * information about. The OS is expected to call function 4
1349 		 * with %ecx set to 0, 1, 2, ... until it returns with
1350 		 * EAX[4:0] set to 0, which indicates there are no more
1351 		 * caches.
1352 		 *
1353 		 * Here, populate cpi_std[4] with the information returned by
1354 		 * function 4 when %ecx == 0, and do the rest in cpuid_pass3()
1355 		 * when dynamic memory allocation becomes available.
1356 		 *
1357 		 * Note: we need to explicitly initialize %ecx here, since
1358 		 * function 4 may have been previously invoked.
1359 		 */
1360 		if (n == 4)
1361 			cp->cp_ecx = 0;
1362 
1363 		(void) __cpuid_insn(cp);
1364 		platform_cpuid_mangle(cpi->cpi_vendor, n, cp);
1365 		switch (n) {
1366 		case 2:
1367 			/*
1368 			 * "the lower 8 bits of the %eax register
1369 			 * contain a value that identifies the number
1370 			 * of times the cpuid [instruction] has to be
1371 			 * executed to obtain a complete image of the
1372 			 * processor's caching systems."
1373 			 *
1374 			 * How *do* they make this stuff up?
1375 			 */
1376 			cpi->cpi_ncache = sizeof (*cp) *
1377 			    BITX(cp->cp_eax, 7, 0);
1378 			if (cpi->cpi_ncache == 0)
1379 				break;
1380 			cpi->cpi_ncache--;	/* skip count byte */
1381 
1382 			/*
1383 			 * Well, for now, rather than attempt to implement
1384 			 * this slightly dubious algorithm, we just look
1385 			 * at the first 15 ..
1386 			 */
1387 			if (cpi->cpi_ncache > (sizeof (*cp) - 1))
1388 				cpi->cpi_ncache = sizeof (*cp) - 1;
1389 
1390 			dp = cpi->cpi_cacheinfo;
1391 			if (BITX(cp->cp_eax, 31, 31) == 0) {
1392 				uint8_t *p = (void *)&cp->cp_eax;
1393 				for (i = 1; i < 4; i++)
1394 					if (p[i] != 0)
1395 						*dp++ = p[i];
1396 			}
1397 			if (BITX(cp->cp_ebx, 31, 31) == 0) {
1398 				uint8_t *p = (void *)&cp->cp_ebx;
1399 				for (i = 0; i < 4; i++)
1400 					if (p[i] != 0)
1401 						*dp++ = p[i];
1402 			}
1403 			if (BITX(cp->cp_ecx, 31, 31) == 0) {
1404 				uint8_t *p = (void *)&cp->cp_ecx;
1405 				for (i = 0; i < 4; i++)
1406 					if (p[i] != 0)
1407 						*dp++ = p[i];
1408 			}
1409 			if (BITX(cp->cp_edx, 31, 31) == 0) {
1410 				uint8_t *p = (void *)&cp->cp_edx;
1411 				for (i = 0; i < 4; i++)
1412 					if (p[i] != 0)
1413 						*dp++ = p[i];
1414 			}
1415 			break;
1416 
1417 		case 3:	/* Processor serial number, if PSN supported */
1418 			break;
1419 
1420 		case 4:	/* Deterministic cache parameters */
1421 			break;
1422 
1423 		case 5:	/* Monitor/Mwait parameters */
1424 		{
1425 			size_t mwait_size;
1426 
1427 			/*
1428 			 * check cpi_mwait.support which was set in cpuid_pass1
1429 			 */
1430 			if (!(cpi->cpi_mwait.support & MWAIT_SUPPORT))
1431 				break;
1432 
1433 			/*
1434 			 * Protect ourself from insane mwait line size.
1435 			 * Workaround for incomplete hardware emulator(s).
1436 			 */
1437 			mwait_size = (size_t)MWAIT_SIZE_MAX(cpi);
1438 			if (mwait_size < sizeof (uint32_t) ||
1439 			    !ISP2(mwait_size)) {
1440 #if DEBUG
1441 				cmn_err(CE_NOTE, "Cannot handle cpu %d mwait "
1442 				    "size %ld",
1443 				    cpu->cpu_id, (long)mwait_size);
1444 #endif
1445 				break;
1446 			}
1447 
1448 			cpi->cpi_mwait.mon_min = (size_t)MWAIT_SIZE_MIN(cpi);
1449 			cpi->cpi_mwait.mon_max = mwait_size;
1450 			if (MWAIT_EXTENSION(cpi)) {
1451 				cpi->cpi_mwait.support |= MWAIT_EXTENSIONS;
1452 				if (MWAIT_INT_ENABLE(cpi))
1453 					cpi->cpi_mwait.support |=
1454 					    MWAIT_ECX_INT_ENABLE;
1455 			}
1456 			break;
1457 		}
1458 		default:
1459 			break;
1460 		}
1461 	}
1462 
1463 	if ((cpi->cpi_xmaxeax & 0x80000000) == 0)
1464 		goto pass2_done;
1465 
1466 	if ((nmax = cpi->cpi_xmaxeax - 0x80000000 + 1) > NMAX_CPI_EXTD)
1467 		nmax = NMAX_CPI_EXTD;
1468 	/*
1469 	 * Copy the extended properties, fixing them as we go.
1470 	 * (We already handled n == 0 and n == 1 in pass 1)
1471 	 */
1472 	iptr = (void *)cpi->cpi_brandstr;
1473 	for (n = 2, cp = &cpi->cpi_extd[2]; n < nmax; cp++, n++) {
1474 		cp->cp_eax = 0x80000000 + n;
1475 		(void) __cpuid_insn(cp);
1476 		platform_cpuid_mangle(cpi->cpi_vendor, 0x80000000 + n, cp);
1477 		switch (n) {
1478 		case 2:
1479 		case 3:
1480 		case 4:
1481 			/*
1482 			 * Extract the brand string
1483 			 */
1484 			*iptr++ = cp->cp_eax;
1485 			*iptr++ = cp->cp_ebx;
1486 			*iptr++ = cp->cp_ecx;
1487 			*iptr++ = cp->cp_edx;
1488 			break;
1489 		case 5:
1490 			switch (cpi->cpi_vendor) {
1491 			case X86_VENDOR_AMD:
1492 				/*
1493 				 * The Athlon and Duron were the first
1494 				 * parts to report the sizes of the
1495 				 * TLB for large pages. Before then,
1496 				 * we don't trust the data.
1497 				 */
1498 				if (cpi->cpi_family < 6 ||
1499 				    (cpi->cpi_family == 6 &&
1500 				    cpi->cpi_model < 1))
1501 					cp->cp_eax = 0;
1502 				break;
1503 			default:
1504 				break;
1505 			}
1506 			break;
1507 		case 6:
1508 			switch (cpi->cpi_vendor) {
1509 			case X86_VENDOR_AMD:
1510 				/*
1511 				 * The Athlon and Duron were the first
1512 				 * AMD parts with L2 TLB's.
1513 				 * Before then, don't trust the data.
1514 				 */
1515 				if (cpi->cpi_family < 6 ||
1516 				    cpi->cpi_family == 6 &&
1517 				    cpi->cpi_model < 1)
1518 					cp->cp_eax = cp->cp_ebx = 0;
1519 				/*
1520 				 * AMD Duron rev A0 reports L2
1521 				 * cache size incorrectly as 1K
1522 				 * when it is really 64K
1523 				 */
1524 				if (cpi->cpi_family == 6 &&
1525 				    cpi->cpi_model == 3 &&
1526 				    cpi->cpi_step == 0) {
1527 					cp->cp_ecx &= 0xffff;
1528 					cp->cp_ecx |= 0x400000;
1529 				}
1530 				break;
1531 			case X86_VENDOR_Cyrix:	/* VIA C3 */
1532 				/*
1533 				 * VIA C3 processors are a bit messed
1534 				 * up w.r.t. encoding cache sizes in %ecx
1535 				 */
1536 				if (cpi->cpi_family != 6)
1537 					break;
1538 				/*
1539 				 * model 7 and 8 were incorrectly encoded
1540 				 *
1541 				 * xxx is model 8 really broken?
1542 				 */
1543 				if (cpi->cpi_model == 7 ||
1544 				    cpi->cpi_model == 8)
1545 					cp->cp_ecx =
1546 					    BITX(cp->cp_ecx, 31, 24) << 16 |
1547 					    BITX(cp->cp_ecx, 23, 16) << 12 |
1548 					    BITX(cp->cp_ecx, 15, 8) << 8 |
1549 					    BITX(cp->cp_ecx, 7, 0);
1550 				/*
1551 				 * model 9 stepping 1 has wrong associativity
1552 				 */
1553 				if (cpi->cpi_model == 9 && cpi->cpi_step == 1)
1554 					cp->cp_ecx |= 8 << 12;
1555 				break;
1556 			case X86_VENDOR_Intel:
1557 				/*
1558 				 * Extended L2 Cache features function.
1559 				 * First appeared on Prescott.
1560 				 */
1561 			default:
1562 				break;
1563 			}
1564 			break;
1565 		default:
1566 			break;
1567 		}
1568 	}
1569 
1570 pass2_done:
1571 	cpi->cpi_pass = 2;
1572 }
1573 
1574 static const char *
1575 intel_cpubrand(const struct cpuid_info *cpi)
1576 {
1577 	int i;
1578 
1579 	if ((x86_feature & X86_CPUID) == 0 ||
1580 	    cpi->cpi_maxeax < 1 || cpi->cpi_family < 5)
1581 		return ("i486");
1582 
1583 	switch (cpi->cpi_family) {
1584 	case 5:
1585 		return ("Intel Pentium(r)");
1586 	case 6:
1587 		switch (cpi->cpi_model) {
1588 			uint_t celeron, xeon;
1589 			const struct cpuid_regs *cp;
1590 		case 0:
1591 		case 1:
1592 		case 2:
1593 			return ("Intel Pentium(r) Pro");
1594 		case 3:
1595 		case 4:
1596 			return ("Intel Pentium(r) II");
1597 		case 6:
1598 			return ("Intel Celeron(r)");
1599 		case 5:
1600 		case 7:
1601 			celeron = xeon = 0;
1602 			cp = &cpi->cpi_std[2];	/* cache info */
1603 
1604 			for (i = 1; i < 4; i++) {
1605 				uint_t tmp;
1606 
1607 				tmp = (cp->cp_eax >> (8 * i)) & 0xff;
1608 				if (tmp == 0x40)
1609 					celeron++;
1610 				if (tmp >= 0x44 && tmp <= 0x45)
1611 					xeon++;
1612 			}
1613 
1614 			for (i = 0; i < 2; i++) {
1615 				uint_t tmp;
1616 
1617 				tmp = (cp->cp_ebx >> (8 * i)) & 0xff;
1618 				if (tmp == 0x40)
1619 					celeron++;
1620 				else if (tmp >= 0x44 && tmp <= 0x45)
1621 					xeon++;
1622 			}
1623 
1624 			for (i = 0; i < 4; i++) {
1625 				uint_t tmp;
1626 
1627 				tmp = (cp->cp_ecx >> (8 * i)) & 0xff;
1628 				if (tmp == 0x40)
1629 					celeron++;
1630 				else if (tmp >= 0x44 && tmp <= 0x45)
1631 					xeon++;
1632 			}
1633 
1634 			for (i = 0; i < 4; i++) {
1635 				uint_t tmp;
1636 
1637 				tmp = (cp->cp_edx >> (8 * i)) & 0xff;
1638 				if (tmp == 0x40)
1639 					celeron++;
1640 				else if (tmp >= 0x44 && tmp <= 0x45)
1641 					xeon++;
1642 			}
1643 
1644 			if (celeron)
1645 				return ("Intel Celeron(r)");
1646 			if (xeon)
1647 				return (cpi->cpi_model == 5 ?
1648 				    "Intel Pentium(r) II Xeon(tm)" :
1649 				    "Intel Pentium(r) III Xeon(tm)");
1650 			return (cpi->cpi_model == 5 ?
1651 			    "Intel Pentium(r) II or Pentium(r) II Xeon(tm)" :
1652 			    "Intel Pentium(r) III or Pentium(r) III Xeon(tm)");
1653 		default:
1654 			break;
1655 		}
1656 	default:
1657 		break;
1658 	}
1659 
1660 	/* BrandID is present if the field is nonzero */
1661 	if (cpi->cpi_brandid != 0) {
1662 		static const struct {
1663 			uint_t bt_bid;
1664 			const char *bt_str;
1665 		} brand_tbl[] = {
1666 			{ 0x1,	"Intel(r) Celeron(r)" },
1667 			{ 0x2,	"Intel(r) Pentium(r) III" },
1668 			{ 0x3,	"Intel(r) Pentium(r) III Xeon(tm)" },
1669 			{ 0x4,	"Intel(r) Pentium(r) III" },
1670 			{ 0x6,	"Mobile Intel(r) Pentium(r) III" },
1671 			{ 0x7,	"Mobile Intel(r) Celeron(r)" },
1672 			{ 0x8,	"Intel(r) Pentium(r) 4" },
1673 			{ 0x9,	"Intel(r) Pentium(r) 4" },
1674 			{ 0xa,	"Intel(r) Celeron(r)" },
1675 			{ 0xb,	"Intel(r) Xeon(tm)" },
1676 			{ 0xc,	"Intel(r) Xeon(tm) MP" },
1677 			{ 0xe,	"Mobile Intel(r) Pentium(r) 4" },
1678 			{ 0xf,	"Mobile Intel(r) Celeron(r)" },
1679 			{ 0x11, "Mobile Genuine Intel(r)" },
1680 			{ 0x12, "Intel(r) Celeron(r) M" },
1681 			{ 0x13, "Mobile Intel(r) Celeron(r)" },
1682 			{ 0x14, "Intel(r) Celeron(r)" },
1683 			{ 0x15, "Mobile Genuine Intel(r)" },
1684 			{ 0x16,	"Intel(r) Pentium(r) M" },
1685 			{ 0x17, "Mobile Intel(r) Celeron(r)" }
1686 		};
1687 		uint_t btblmax = sizeof (brand_tbl) / sizeof (brand_tbl[0]);
1688 		uint_t sgn;
1689 
1690 		sgn = (cpi->cpi_family << 8) |
1691 		    (cpi->cpi_model << 4) | cpi->cpi_step;
1692 
1693 		for (i = 0; i < btblmax; i++)
1694 			if (brand_tbl[i].bt_bid == cpi->cpi_brandid)
1695 				break;
1696 		if (i < btblmax) {
1697 			if (sgn == 0x6b1 && cpi->cpi_brandid == 3)
1698 				return ("Intel(r) Celeron(r)");
1699 			if (sgn < 0xf13 && cpi->cpi_brandid == 0xb)
1700 				return ("Intel(r) Xeon(tm) MP");
1701 			if (sgn < 0xf13 && cpi->cpi_brandid == 0xe)
1702 				return ("Intel(r) Xeon(tm)");
1703 			return (brand_tbl[i].bt_str);
1704 		}
1705 	}
1706 
1707 	return (NULL);
1708 }
1709 
1710 static const char *
1711 amd_cpubrand(const struct cpuid_info *cpi)
1712 {
1713 	if ((x86_feature & X86_CPUID) == 0 ||
1714 	    cpi->cpi_maxeax < 1 || cpi->cpi_family < 5)
1715 		return ("i486 compatible");
1716 
1717 	switch (cpi->cpi_family) {
1718 	case 5:
1719 		switch (cpi->cpi_model) {
1720 		case 0:
1721 		case 1:
1722 		case 2:
1723 		case 3:
1724 		case 4:
1725 		case 5:
1726 			return ("AMD-K5(r)");
1727 		case 6:
1728 		case 7:
1729 			return ("AMD-K6(r)");
1730 		case 8:
1731 			return ("AMD-K6(r)-2");
1732 		case 9:
1733 			return ("AMD-K6(r)-III");
1734 		default:
1735 			return ("AMD (family 5)");
1736 		}
1737 	case 6:
1738 		switch (cpi->cpi_model) {
1739 		case 1:
1740 			return ("AMD-K7(tm)");
1741 		case 0:
1742 		case 2:
1743 		case 4:
1744 			return ("AMD Athlon(tm)");
1745 		case 3:
1746 		case 7:
1747 			return ("AMD Duron(tm)");
1748 		case 6:
1749 		case 8:
1750 		case 10:
1751 			/*
1752 			 * Use the L2 cache size to distinguish
1753 			 */
1754 			return ((cpi->cpi_extd[6].cp_ecx >> 16) >= 256 ?
1755 			    "AMD Athlon(tm)" : "AMD Duron(tm)");
1756 		default:
1757 			return ("AMD (family 6)");
1758 		}
1759 	default:
1760 		break;
1761 	}
1762 
1763 	if (cpi->cpi_family == 0xf && cpi->cpi_model == 5 &&
1764 	    cpi->cpi_brandid != 0) {
1765 		switch (BITX(cpi->cpi_brandid, 7, 5)) {
1766 		case 3:
1767 			return ("AMD Opteron(tm) UP 1xx");
1768 		case 4:
1769 			return ("AMD Opteron(tm) DP 2xx");
1770 		case 5:
1771 			return ("AMD Opteron(tm) MP 8xx");
1772 		default:
1773 			return ("AMD Opteron(tm)");
1774 		}
1775 	}
1776 
1777 	return (NULL);
1778 }
1779 
1780 static const char *
1781 cyrix_cpubrand(struct cpuid_info *cpi, uint_t type)
1782 {
1783 	if ((x86_feature & X86_CPUID) == 0 ||
1784 	    cpi->cpi_maxeax < 1 || cpi->cpi_family < 5 ||
1785 	    type == X86_TYPE_CYRIX_486)
1786 		return ("i486 compatible");
1787 
1788 	switch (type) {
1789 	case X86_TYPE_CYRIX_6x86:
1790 		return ("Cyrix 6x86");
1791 	case X86_TYPE_CYRIX_6x86L:
1792 		return ("Cyrix 6x86L");
1793 	case X86_TYPE_CYRIX_6x86MX:
1794 		return ("Cyrix 6x86MX");
1795 	case X86_TYPE_CYRIX_GXm:
1796 		return ("Cyrix GXm");
1797 	case X86_TYPE_CYRIX_MediaGX:
1798 		return ("Cyrix MediaGX");
1799 	case X86_TYPE_CYRIX_MII:
1800 		return ("Cyrix M2");
1801 	case X86_TYPE_VIA_CYRIX_III:
1802 		return ("VIA Cyrix M3");
1803 	default:
1804 		/*
1805 		 * Have another wild guess ..
1806 		 */
1807 		if (cpi->cpi_family == 4 && cpi->cpi_model == 9)
1808 			return ("Cyrix 5x86");
1809 		else if (cpi->cpi_family == 5) {
1810 			switch (cpi->cpi_model) {
1811 			case 2:
1812 				return ("Cyrix 6x86");	/* Cyrix M1 */
1813 			case 4:
1814 				return ("Cyrix MediaGX");
1815 			default:
1816 				break;
1817 			}
1818 		} else if (cpi->cpi_family == 6) {
1819 			switch (cpi->cpi_model) {
1820 			case 0:
1821 				return ("Cyrix 6x86MX"); /* Cyrix M2? */
1822 			case 5:
1823 			case 6:
1824 			case 7:
1825 			case 8:
1826 			case 9:
1827 				return ("VIA C3");
1828 			default:
1829 				break;
1830 			}
1831 		}
1832 		break;
1833 	}
1834 	return (NULL);
1835 }
1836 
1837 /*
1838  * This only gets called in the case that the CPU extended
1839  * feature brand string (0x80000002, 0x80000003, 0x80000004)
1840  * aren't available, or contain null bytes for some reason.
1841  */
1842 static void
1843 fabricate_brandstr(struct cpuid_info *cpi)
1844 {
1845 	const char *brand = NULL;
1846 
1847 	switch (cpi->cpi_vendor) {
1848 	case X86_VENDOR_Intel:
1849 		brand = intel_cpubrand(cpi);
1850 		break;
1851 	case X86_VENDOR_AMD:
1852 		brand = amd_cpubrand(cpi);
1853 		break;
1854 	case X86_VENDOR_Cyrix:
1855 		brand = cyrix_cpubrand(cpi, x86_type);
1856 		break;
1857 	case X86_VENDOR_NexGen:
1858 		if (cpi->cpi_family == 5 && cpi->cpi_model == 0)
1859 			brand = "NexGen Nx586";
1860 		break;
1861 	case X86_VENDOR_Centaur:
1862 		if (cpi->cpi_family == 5)
1863 			switch (cpi->cpi_model) {
1864 			case 4:
1865 				brand = "Centaur C6";
1866 				break;
1867 			case 8:
1868 				brand = "Centaur C2";
1869 				break;
1870 			case 9:
1871 				brand = "Centaur C3";
1872 				break;
1873 			default:
1874 				break;
1875 			}
1876 		break;
1877 	case X86_VENDOR_Rise:
1878 		if (cpi->cpi_family == 5 &&
1879 		    (cpi->cpi_model == 0 || cpi->cpi_model == 2))
1880 			brand = "Rise mP6";
1881 		break;
1882 	case X86_VENDOR_SiS:
1883 		if (cpi->cpi_family == 5 && cpi->cpi_model == 0)
1884 			brand = "SiS 55x";
1885 		break;
1886 	case X86_VENDOR_TM:
1887 		if (cpi->cpi_family == 5 && cpi->cpi_model == 4)
1888 			brand = "Transmeta Crusoe TM3x00 or TM5x00";
1889 		break;
1890 	case X86_VENDOR_NSC:
1891 	case X86_VENDOR_UMC:
1892 	default:
1893 		break;
1894 	}
1895 	if (brand) {
1896 		(void) strcpy((char *)cpi->cpi_brandstr, brand);
1897 		return;
1898 	}
1899 
1900 	/*
1901 	 * If all else fails ...
1902 	 */
1903 	(void) snprintf(cpi->cpi_brandstr, sizeof (cpi->cpi_brandstr),
1904 	    "%s %d.%d.%d", cpi->cpi_vendorstr, cpi->cpi_family,
1905 	    cpi->cpi_model, cpi->cpi_step);
1906 }
1907 
1908 /*
1909  * This routine is called just after kernel memory allocation
1910  * becomes available on cpu0, and as part of mp_startup() on
1911  * the other cpus.
1912  *
1913  * Fixup the brand string, and collect any information from cpuid
1914  * that requires dynamicically allocated storage to represent.
1915  */
1916 /*ARGSUSED*/
1917 void
1918 cpuid_pass3(cpu_t *cpu)
1919 {
1920 	int	i, max, shft, level, size;
1921 	struct cpuid_regs regs;
1922 	struct cpuid_regs *cp;
1923 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
1924 
1925 	ASSERT(cpi->cpi_pass == 2);
1926 
1927 	/*
1928 	 * Function 4: Deterministic cache parameters
1929 	 *
1930 	 * Take this opportunity to detect the number of threads
1931 	 * sharing the last level cache, and construct a corresponding
1932 	 * cache id. The respective cpuid_info members are initialized
1933 	 * to the default case of "no last level cache sharing".
1934 	 */
1935 	cpi->cpi_ncpu_shr_last_cache = 1;
1936 	cpi->cpi_last_lvl_cacheid = cpu->cpu_id;
1937 
1938 	if (cpi->cpi_maxeax >= 4 && cpi->cpi_vendor == X86_VENDOR_Intel) {
1939 
1940 		/*
1941 		 * Find the # of elements (size) returned by fn 4, and along
1942 		 * the way detect last level cache sharing details.
1943 		 */
1944 		bzero(&regs, sizeof (regs));
1945 		cp = &regs;
1946 		for (i = 0, max = 0; i < CPI_FN4_ECX_MAX; i++) {
1947 			cp->cp_eax = 4;
1948 			cp->cp_ecx = i;
1949 
1950 			(void) __cpuid_insn(cp);
1951 
1952 			if (CPI_CACHE_TYPE(cp) == 0)
1953 				break;
1954 			level = CPI_CACHE_LVL(cp);
1955 			if (level > max) {
1956 				max = level;
1957 				cpi->cpi_ncpu_shr_last_cache =
1958 				    CPI_NTHR_SHR_CACHE(cp) + 1;
1959 			}
1960 		}
1961 		cpi->cpi_std_4_size = size = i;
1962 
1963 		/*
1964 		 * Allocate the cpi_std_4 array. The first element
1965 		 * references the regs for fn 4, %ecx == 0, which
1966 		 * cpuid_pass2() stashed in cpi->cpi_std[4].
1967 		 */
1968 		if (size > 0) {
1969 			cpi->cpi_std_4 =
1970 			    kmem_alloc(size * sizeof (cp), KM_SLEEP);
1971 			cpi->cpi_std_4[0] = &cpi->cpi_std[4];
1972 
1973 			/*
1974 			 * Allocate storage to hold the additional regs
1975 			 * for function 4, %ecx == 1 .. cpi_std_4_size.
1976 			 *
1977 			 * The regs for fn 4, %ecx == 0 has already
1978 			 * been allocated as indicated above.
1979 			 */
1980 			for (i = 1; i < size; i++) {
1981 				cp = cpi->cpi_std_4[i] =
1982 				    kmem_zalloc(sizeof (regs), KM_SLEEP);
1983 				cp->cp_eax = 4;
1984 				cp->cp_ecx = i;
1985 
1986 				(void) __cpuid_insn(cp);
1987 			}
1988 		}
1989 		/*
1990 		 * Determine the number of bits needed to represent
1991 		 * the number of CPUs sharing the last level cache.
1992 		 *
1993 		 * Shift off that number of bits from the APIC id to
1994 		 * derive the cache id.
1995 		 */
1996 		shft = 0;
1997 		for (i = 1; i < cpi->cpi_ncpu_shr_last_cache; i <<= 1)
1998 			shft++;
1999 		cpi->cpi_last_lvl_cacheid = CPI_APIC_ID(cpi) >> shft;
2000 	}
2001 
2002 	/*
2003 	 * Now fixup the brand string
2004 	 */
2005 	if ((cpi->cpi_xmaxeax & 0x80000000) == 0) {
2006 		fabricate_brandstr(cpi);
2007 	} else {
2008 
2009 		/*
2010 		 * If we successfully extracted a brand string from the cpuid
2011 		 * instruction, clean it up by removing leading spaces and
2012 		 * similar junk.
2013 		 */
2014 		if (cpi->cpi_brandstr[0]) {
2015 			size_t maxlen = sizeof (cpi->cpi_brandstr);
2016 			char *src, *dst;
2017 
2018 			dst = src = (char *)cpi->cpi_brandstr;
2019 			src[maxlen - 1] = '\0';
2020 			/*
2021 			 * strip leading spaces
2022 			 */
2023 			while (*src == ' ')
2024 				src++;
2025 			/*
2026 			 * Remove any 'Genuine' or "Authentic" prefixes
2027 			 */
2028 			if (strncmp(src, "Genuine ", 8) == 0)
2029 				src += 8;
2030 			if (strncmp(src, "Authentic ", 10) == 0)
2031 				src += 10;
2032 
2033 			/*
2034 			 * Now do an in-place copy.
2035 			 * Map (R) to (r) and (TM) to (tm).
2036 			 * The era of teletypes is long gone, and there's
2037 			 * -really- no need to shout.
2038 			 */
2039 			while (*src != '\0') {
2040 				if (src[0] == '(') {
2041 					if (strncmp(src + 1, "R)", 2) == 0) {
2042 						(void) strncpy(dst, "(r)", 3);
2043 						src += 3;
2044 						dst += 3;
2045 						continue;
2046 					}
2047 					if (strncmp(src + 1, "TM)", 3) == 0) {
2048 						(void) strncpy(dst, "(tm)", 4);
2049 						src += 4;
2050 						dst += 4;
2051 						continue;
2052 					}
2053 				}
2054 				*dst++ = *src++;
2055 			}
2056 			*dst = '\0';
2057 
2058 			/*
2059 			 * Finally, remove any trailing spaces
2060 			 */
2061 			while (--dst > cpi->cpi_brandstr)
2062 				if (*dst == ' ')
2063 					*dst = '\0';
2064 				else
2065 					break;
2066 		} else
2067 			fabricate_brandstr(cpi);
2068 	}
2069 	cpi->cpi_pass = 3;
2070 }
2071 
2072 /*
2073  * This routine is called out of bind_hwcap() much later in the life
2074  * of the kernel (post_startup()).  The job of this routine is to resolve
2075  * the hardware feature support and kernel support for those features into
2076  * what we're actually going to tell applications via the aux vector.
2077  */
2078 uint_t
2079 cpuid_pass4(cpu_t *cpu)
2080 {
2081 	struct cpuid_info *cpi;
2082 	uint_t hwcap_flags = 0;
2083 
2084 	if (cpu == NULL)
2085 		cpu = CPU;
2086 	cpi = cpu->cpu_m.mcpu_cpi;
2087 
2088 	ASSERT(cpi->cpi_pass == 3);
2089 
2090 	if (cpi->cpi_maxeax >= 1) {
2091 		uint32_t *edx = &cpi->cpi_support[STD_EDX_FEATURES];
2092 		uint32_t *ecx = &cpi->cpi_support[STD_ECX_FEATURES];
2093 
2094 		*edx = CPI_FEATURES_EDX(cpi);
2095 		*ecx = CPI_FEATURES_ECX(cpi);
2096 
2097 		/*
2098 		 * [these require explicit kernel support]
2099 		 */
2100 		if ((x86_feature & X86_SEP) == 0)
2101 			*edx &= ~CPUID_INTC_EDX_SEP;
2102 
2103 		if ((x86_feature & X86_SSE) == 0)
2104 			*edx &= ~(CPUID_INTC_EDX_FXSR|CPUID_INTC_EDX_SSE);
2105 		if ((x86_feature & X86_SSE2) == 0)
2106 			*edx &= ~CPUID_INTC_EDX_SSE2;
2107 
2108 		if ((x86_feature & X86_HTT) == 0)
2109 			*edx &= ~CPUID_INTC_EDX_HTT;
2110 
2111 		if ((x86_feature & X86_SSE3) == 0)
2112 			*ecx &= ~CPUID_INTC_ECX_SSE3;
2113 
2114 		if (cpi->cpi_vendor == X86_VENDOR_Intel) {
2115 			if ((x86_feature & X86_SSSE3) == 0)
2116 				*ecx &= ~CPUID_INTC_ECX_SSSE3;
2117 			if ((x86_feature & X86_SSE4_1) == 0)
2118 				*ecx &= ~CPUID_INTC_ECX_SSE4_1;
2119 			if ((x86_feature & X86_SSE4_2) == 0)
2120 				*ecx &= ~CPUID_INTC_ECX_SSE4_2;
2121 		}
2122 
2123 		/*
2124 		 * [no explicit support required beyond x87 fp context]
2125 		 */
2126 		if (!fpu_exists)
2127 			*edx &= ~(CPUID_INTC_EDX_FPU | CPUID_INTC_EDX_MMX);
2128 
2129 		/*
2130 		 * Now map the supported feature vector to things that we
2131 		 * think userland will care about.
2132 		 */
2133 		if (*edx & CPUID_INTC_EDX_SEP)
2134 			hwcap_flags |= AV_386_SEP;
2135 		if (*edx & CPUID_INTC_EDX_SSE)
2136 			hwcap_flags |= AV_386_FXSR | AV_386_SSE;
2137 		if (*edx & CPUID_INTC_EDX_SSE2)
2138 			hwcap_flags |= AV_386_SSE2;
2139 		if (*ecx & CPUID_INTC_ECX_SSE3)
2140 			hwcap_flags |= AV_386_SSE3;
2141 		if (cpi->cpi_vendor == X86_VENDOR_Intel) {
2142 			if (*ecx & CPUID_INTC_ECX_SSSE3)
2143 				hwcap_flags |= AV_386_SSSE3;
2144 			if (*ecx & CPUID_INTC_ECX_SSE4_1)
2145 				hwcap_flags |= AV_386_SSE4_1;
2146 			if (*ecx & CPUID_INTC_ECX_SSE4_2)
2147 				hwcap_flags |= AV_386_SSE4_2;
2148 		}
2149 		if (*ecx & CPUID_INTC_ECX_POPCNT)
2150 			hwcap_flags |= AV_386_POPCNT;
2151 		if (*edx & CPUID_INTC_EDX_FPU)
2152 			hwcap_flags |= AV_386_FPU;
2153 		if (*edx & CPUID_INTC_EDX_MMX)
2154 			hwcap_flags |= AV_386_MMX;
2155 
2156 		if (*edx & CPUID_INTC_EDX_TSC)
2157 			hwcap_flags |= AV_386_TSC;
2158 		if (*edx & CPUID_INTC_EDX_CX8)
2159 			hwcap_flags |= AV_386_CX8;
2160 		if (*edx & CPUID_INTC_EDX_CMOV)
2161 			hwcap_flags |= AV_386_CMOV;
2162 		if (*ecx & CPUID_INTC_ECX_MON)
2163 			hwcap_flags |= AV_386_MON;
2164 		if (*ecx & CPUID_INTC_ECX_CX16)
2165 			hwcap_flags |= AV_386_CX16;
2166 	}
2167 
2168 	if (x86_feature & X86_HTT)
2169 		hwcap_flags |= AV_386_PAUSE;
2170 
2171 	if (cpi->cpi_xmaxeax < 0x80000001)
2172 		goto pass4_done;
2173 
2174 	switch (cpi->cpi_vendor) {
2175 		struct cpuid_regs cp;
2176 		uint32_t *edx, *ecx;
2177 
2178 	case X86_VENDOR_Intel:
2179 		/*
2180 		 * Seems like Intel duplicated what we necessary
2181 		 * here to make the initial crop of 64-bit OS's work.
2182 		 * Hopefully, those are the only "extended" bits
2183 		 * they'll add.
2184 		 */
2185 		/*FALLTHROUGH*/
2186 
2187 	case X86_VENDOR_AMD:
2188 		edx = &cpi->cpi_support[AMD_EDX_FEATURES];
2189 		ecx = &cpi->cpi_support[AMD_ECX_FEATURES];
2190 
2191 		*edx = CPI_FEATURES_XTD_EDX(cpi);
2192 		*ecx = CPI_FEATURES_XTD_ECX(cpi);
2193 
2194 		/*
2195 		 * [these features require explicit kernel support]
2196 		 */
2197 		switch (cpi->cpi_vendor) {
2198 		case X86_VENDOR_Intel:
2199 			if ((x86_feature & X86_TSCP) == 0)
2200 				*edx &= ~CPUID_AMD_EDX_TSCP;
2201 			break;
2202 
2203 		case X86_VENDOR_AMD:
2204 			if ((x86_feature & X86_TSCP) == 0)
2205 				*edx &= ~CPUID_AMD_EDX_TSCP;
2206 			if ((x86_feature & X86_SSE4A) == 0)
2207 				*ecx &= ~CPUID_AMD_ECX_SSE4A;
2208 			break;
2209 
2210 		default:
2211 			break;
2212 		}
2213 
2214 		/*
2215 		 * [no explicit support required beyond
2216 		 * x87 fp context and exception handlers]
2217 		 */
2218 		if (!fpu_exists)
2219 			*edx &= ~(CPUID_AMD_EDX_MMXamd |
2220 			    CPUID_AMD_EDX_3DNow | CPUID_AMD_EDX_3DNowx);
2221 
2222 		if ((x86_feature & X86_NX) == 0)
2223 			*edx &= ~CPUID_AMD_EDX_NX;
2224 #if !defined(__amd64)
2225 		*edx &= ~CPUID_AMD_EDX_LM;
2226 #endif
2227 		/*
2228 		 * Now map the supported feature vector to
2229 		 * things that we think userland will care about.
2230 		 */
2231 #if defined(__amd64)
2232 		if (*edx & CPUID_AMD_EDX_SYSC)
2233 			hwcap_flags |= AV_386_AMD_SYSC;
2234 #endif
2235 		if (*edx & CPUID_AMD_EDX_MMXamd)
2236 			hwcap_flags |= AV_386_AMD_MMX;
2237 		if (*edx & CPUID_AMD_EDX_3DNow)
2238 			hwcap_flags |= AV_386_AMD_3DNow;
2239 		if (*edx & CPUID_AMD_EDX_3DNowx)
2240 			hwcap_flags |= AV_386_AMD_3DNowx;
2241 
2242 		switch (cpi->cpi_vendor) {
2243 		case X86_VENDOR_AMD:
2244 			if (*edx & CPUID_AMD_EDX_TSCP)
2245 				hwcap_flags |= AV_386_TSCP;
2246 			if (*ecx & CPUID_AMD_ECX_AHF64)
2247 				hwcap_flags |= AV_386_AHF;
2248 			if (*ecx & CPUID_AMD_ECX_SSE4A)
2249 				hwcap_flags |= AV_386_AMD_SSE4A;
2250 			if (*ecx & CPUID_AMD_ECX_LZCNT)
2251 				hwcap_flags |= AV_386_AMD_LZCNT;
2252 			break;
2253 
2254 		case X86_VENDOR_Intel:
2255 			if (*edx & CPUID_AMD_EDX_TSCP)
2256 				hwcap_flags |= AV_386_TSCP;
2257 			/*
2258 			 * Aarrgh.
2259 			 * Intel uses a different bit in the same word.
2260 			 */
2261 			if (*ecx & CPUID_INTC_ECX_AHF64)
2262 				hwcap_flags |= AV_386_AHF;
2263 			break;
2264 
2265 		default:
2266 			break;
2267 		}
2268 		break;
2269 
2270 	case X86_VENDOR_TM:
2271 		cp.cp_eax = 0x80860001;
2272 		(void) __cpuid_insn(&cp);
2273 		cpi->cpi_support[TM_EDX_FEATURES] = cp.cp_edx;
2274 		break;
2275 
2276 	default:
2277 		break;
2278 	}
2279 
2280 pass4_done:
2281 	cpi->cpi_pass = 4;
2282 	return (hwcap_flags);
2283 }
2284 
2285 
2286 /*
2287  * Simulate the cpuid instruction using the data we previously
2288  * captured about this CPU.  We try our best to return the truth
2289  * about the hardware, independently of kernel support.
2290  */
2291 uint32_t
2292 cpuid_insn(cpu_t *cpu, struct cpuid_regs *cp)
2293 {
2294 	struct cpuid_info *cpi;
2295 	struct cpuid_regs *xcp;
2296 
2297 	if (cpu == NULL)
2298 		cpu = CPU;
2299 	cpi = cpu->cpu_m.mcpu_cpi;
2300 
2301 	ASSERT(cpuid_checkpass(cpu, 3));
2302 
2303 	/*
2304 	 * CPUID data is cached in two separate places: cpi_std for standard
2305 	 * CPUID functions, and cpi_extd for extended CPUID functions.
2306 	 */
2307 	if (cp->cp_eax <= cpi->cpi_maxeax && cp->cp_eax < NMAX_CPI_STD)
2308 		xcp = &cpi->cpi_std[cp->cp_eax];
2309 	else if (cp->cp_eax >= 0x80000000 && cp->cp_eax <= cpi->cpi_xmaxeax &&
2310 	    cp->cp_eax < 0x80000000 + NMAX_CPI_EXTD)
2311 		xcp = &cpi->cpi_extd[cp->cp_eax - 0x80000000];
2312 	else
2313 		/*
2314 		 * The caller is asking for data from an input parameter which
2315 		 * the kernel has not cached.  In this case we go fetch from
2316 		 * the hardware and return the data directly to the user.
2317 		 */
2318 		return (__cpuid_insn(cp));
2319 
2320 	cp->cp_eax = xcp->cp_eax;
2321 	cp->cp_ebx = xcp->cp_ebx;
2322 	cp->cp_ecx = xcp->cp_ecx;
2323 	cp->cp_edx = xcp->cp_edx;
2324 	return (cp->cp_eax);
2325 }
2326 
2327 int
2328 cpuid_checkpass(cpu_t *cpu, int pass)
2329 {
2330 	return (cpu != NULL && cpu->cpu_m.mcpu_cpi != NULL &&
2331 	    cpu->cpu_m.mcpu_cpi->cpi_pass >= pass);
2332 }
2333 
2334 int
2335 cpuid_getbrandstr(cpu_t *cpu, char *s, size_t n)
2336 {
2337 	ASSERT(cpuid_checkpass(cpu, 3));
2338 
2339 	return (snprintf(s, n, "%s", cpu->cpu_m.mcpu_cpi->cpi_brandstr));
2340 }
2341 
2342 int
2343 cpuid_is_cmt(cpu_t *cpu)
2344 {
2345 	if (cpu == NULL)
2346 		cpu = CPU;
2347 
2348 	ASSERT(cpuid_checkpass(cpu, 1));
2349 
2350 	return (cpu->cpu_m.mcpu_cpi->cpi_chipid >= 0);
2351 }
2352 
2353 /*
2354  * AMD and Intel both implement the 64-bit variant of the syscall
2355  * instruction (syscallq), so if there's -any- support for syscall,
2356  * cpuid currently says "yes, we support this".
2357  *
2358  * However, Intel decided to -not- implement the 32-bit variant of the
2359  * syscall instruction, so we provide a predicate to allow our caller
2360  * to test that subtlety here.
2361  *
2362  * XXPV	Currently, 32-bit syscall instructions don't work via the hypervisor,
2363  *	even in the case where the hardware would in fact support it.
2364  */
2365 /*ARGSUSED*/
2366 int
2367 cpuid_syscall32_insn(cpu_t *cpu)
2368 {
2369 	ASSERT(cpuid_checkpass((cpu == NULL ? CPU : cpu), 1));
2370 
2371 #if !defined(__xpv)
2372 	if (cpu == NULL)
2373 		cpu = CPU;
2374 
2375 	/*CSTYLED*/
2376 	{
2377 		struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
2378 
2379 		if (cpi->cpi_vendor == X86_VENDOR_AMD &&
2380 		    cpi->cpi_xmaxeax >= 0x80000001 &&
2381 		    (CPI_FEATURES_XTD_EDX(cpi) & CPUID_AMD_EDX_SYSC))
2382 			return (1);
2383 	}
2384 #endif
2385 	return (0);
2386 }
2387 
2388 int
2389 cpuid_getidstr(cpu_t *cpu, char *s, size_t n)
2390 {
2391 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
2392 
2393 	static const char fmt[] =
2394 	    "x86 (%s %X family %d model %d step %d clock %d MHz)";
2395 	static const char fmt_ht[] =
2396 	    "x86 (chipid 0x%x %s %X family %d model %d step %d clock %d MHz)";
2397 
2398 	ASSERT(cpuid_checkpass(cpu, 1));
2399 
2400 	if (cpuid_is_cmt(cpu))
2401 		return (snprintf(s, n, fmt_ht, cpi->cpi_chipid,
2402 		    cpi->cpi_vendorstr, cpi->cpi_std[1].cp_eax,
2403 		    cpi->cpi_family, cpi->cpi_model,
2404 		    cpi->cpi_step, cpu->cpu_type_info.pi_clock));
2405 	return (snprintf(s, n, fmt,
2406 	    cpi->cpi_vendorstr, cpi->cpi_std[1].cp_eax,
2407 	    cpi->cpi_family, cpi->cpi_model,
2408 	    cpi->cpi_step, cpu->cpu_type_info.pi_clock));
2409 }
2410 
2411 const char *
2412 cpuid_getvendorstr(cpu_t *cpu)
2413 {
2414 	ASSERT(cpuid_checkpass(cpu, 1));
2415 	return ((const char *)cpu->cpu_m.mcpu_cpi->cpi_vendorstr);
2416 }
2417 
2418 uint_t
2419 cpuid_getvendor(cpu_t *cpu)
2420 {
2421 	ASSERT(cpuid_checkpass(cpu, 1));
2422 	return (cpu->cpu_m.mcpu_cpi->cpi_vendor);
2423 }
2424 
2425 uint_t
2426 cpuid_getfamily(cpu_t *cpu)
2427 {
2428 	ASSERT(cpuid_checkpass(cpu, 1));
2429 	return (cpu->cpu_m.mcpu_cpi->cpi_family);
2430 }
2431 
2432 uint_t
2433 cpuid_getmodel(cpu_t *cpu)
2434 {
2435 	ASSERT(cpuid_checkpass(cpu, 1));
2436 	return (cpu->cpu_m.mcpu_cpi->cpi_model);
2437 }
2438 
2439 uint_t
2440 cpuid_get_ncpu_per_chip(cpu_t *cpu)
2441 {
2442 	ASSERT(cpuid_checkpass(cpu, 1));
2443 	return (cpu->cpu_m.mcpu_cpi->cpi_ncpu_per_chip);
2444 }
2445 
2446 uint_t
2447 cpuid_get_ncore_per_chip(cpu_t *cpu)
2448 {
2449 	ASSERT(cpuid_checkpass(cpu, 1));
2450 	return (cpu->cpu_m.mcpu_cpi->cpi_ncore_per_chip);
2451 }
2452 
2453 uint_t
2454 cpuid_get_ncpu_sharing_last_cache(cpu_t *cpu)
2455 {
2456 	ASSERT(cpuid_checkpass(cpu, 2));
2457 	return (cpu->cpu_m.mcpu_cpi->cpi_ncpu_shr_last_cache);
2458 }
2459 
2460 id_t
2461 cpuid_get_last_lvl_cacheid(cpu_t *cpu)
2462 {
2463 	ASSERT(cpuid_checkpass(cpu, 2));
2464 	return (cpu->cpu_m.mcpu_cpi->cpi_last_lvl_cacheid);
2465 }
2466 
2467 uint_t
2468 cpuid_getstep(cpu_t *cpu)
2469 {
2470 	ASSERT(cpuid_checkpass(cpu, 1));
2471 	return (cpu->cpu_m.mcpu_cpi->cpi_step);
2472 }
2473 
2474 uint_t
2475 cpuid_getsig(struct cpu *cpu)
2476 {
2477 	ASSERT(cpuid_checkpass(cpu, 1));
2478 	return (cpu->cpu_m.mcpu_cpi->cpi_std[1].cp_eax);
2479 }
2480 
2481 uint32_t
2482 cpuid_getchiprev(struct cpu *cpu)
2483 {
2484 	ASSERT(cpuid_checkpass(cpu, 1));
2485 	return (cpu->cpu_m.mcpu_cpi->cpi_chiprev);
2486 }
2487 
2488 const char *
2489 cpuid_getchiprevstr(struct cpu *cpu)
2490 {
2491 	ASSERT(cpuid_checkpass(cpu, 1));
2492 	return (cpu->cpu_m.mcpu_cpi->cpi_chiprevstr);
2493 }
2494 
2495 uint32_t
2496 cpuid_getsockettype(struct cpu *cpu)
2497 {
2498 	ASSERT(cpuid_checkpass(cpu, 1));
2499 	return (cpu->cpu_m.mcpu_cpi->cpi_socket);
2500 }
2501 
2502 int
2503 cpuid_get_chipid(cpu_t *cpu)
2504 {
2505 	ASSERT(cpuid_checkpass(cpu, 1));
2506 
2507 	if (cpuid_is_cmt(cpu))
2508 		return (cpu->cpu_m.mcpu_cpi->cpi_chipid);
2509 	return (cpu->cpu_id);
2510 }
2511 
2512 id_t
2513 cpuid_get_coreid(cpu_t *cpu)
2514 {
2515 	ASSERT(cpuid_checkpass(cpu, 1));
2516 	return (cpu->cpu_m.mcpu_cpi->cpi_coreid);
2517 }
2518 
2519 int
2520 cpuid_get_pkgcoreid(cpu_t *cpu)
2521 {
2522 	ASSERT(cpuid_checkpass(cpu, 1));
2523 	return (cpu->cpu_m.mcpu_cpi->cpi_pkgcoreid);
2524 }
2525 
2526 int
2527 cpuid_get_clogid(cpu_t *cpu)
2528 {
2529 	ASSERT(cpuid_checkpass(cpu, 1));
2530 	return (cpu->cpu_m.mcpu_cpi->cpi_clogid);
2531 }
2532 
2533 void
2534 cpuid_get_addrsize(cpu_t *cpu, uint_t *pabits, uint_t *vabits)
2535 {
2536 	struct cpuid_info *cpi;
2537 
2538 	if (cpu == NULL)
2539 		cpu = CPU;
2540 	cpi = cpu->cpu_m.mcpu_cpi;
2541 
2542 	ASSERT(cpuid_checkpass(cpu, 1));
2543 
2544 	if (pabits)
2545 		*pabits = cpi->cpi_pabits;
2546 	if (vabits)
2547 		*vabits = cpi->cpi_vabits;
2548 }
2549 
2550 /*
2551  * Returns the number of data TLB entries for a corresponding
2552  * pagesize.  If it can't be computed, or isn't known, the
2553  * routine returns zero.  If you ask about an architecturally
2554  * impossible pagesize, the routine will panic (so that the
2555  * hat implementor knows that things are inconsistent.)
2556  */
2557 uint_t
2558 cpuid_get_dtlb_nent(cpu_t *cpu, size_t pagesize)
2559 {
2560 	struct cpuid_info *cpi;
2561 	uint_t dtlb_nent = 0;
2562 
2563 	if (cpu == NULL)
2564 		cpu = CPU;
2565 	cpi = cpu->cpu_m.mcpu_cpi;
2566 
2567 	ASSERT(cpuid_checkpass(cpu, 1));
2568 
2569 	/*
2570 	 * Check the L2 TLB info
2571 	 */
2572 	if (cpi->cpi_xmaxeax >= 0x80000006) {
2573 		struct cpuid_regs *cp = &cpi->cpi_extd[6];
2574 
2575 		switch (pagesize) {
2576 
2577 		case 4 * 1024:
2578 			/*
2579 			 * All zero in the top 16 bits of the register
2580 			 * indicates a unified TLB. Size is in low 16 bits.
2581 			 */
2582 			if ((cp->cp_ebx & 0xffff0000) == 0)
2583 				dtlb_nent = cp->cp_ebx & 0x0000ffff;
2584 			else
2585 				dtlb_nent = BITX(cp->cp_ebx, 27, 16);
2586 			break;
2587 
2588 		case 2 * 1024 * 1024:
2589 			if ((cp->cp_eax & 0xffff0000) == 0)
2590 				dtlb_nent = cp->cp_eax & 0x0000ffff;
2591 			else
2592 				dtlb_nent = BITX(cp->cp_eax, 27, 16);
2593 			break;
2594 
2595 		default:
2596 			panic("unknown L2 pagesize");
2597 			/*NOTREACHED*/
2598 		}
2599 	}
2600 
2601 	if (dtlb_nent != 0)
2602 		return (dtlb_nent);
2603 
2604 	/*
2605 	 * No L2 TLB support for this size, try L1.
2606 	 */
2607 	if (cpi->cpi_xmaxeax >= 0x80000005) {
2608 		struct cpuid_regs *cp = &cpi->cpi_extd[5];
2609 
2610 		switch (pagesize) {
2611 		case 4 * 1024:
2612 			dtlb_nent = BITX(cp->cp_ebx, 23, 16);
2613 			break;
2614 		case 2 * 1024 * 1024:
2615 			dtlb_nent = BITX(cp->cp_eax, 23, 16);
2616 			break;
2617 		default:
2618 			panic("unknown L1 d-TLB pagesize");
2619 			/*NOTREACHED*/
2620 		}
2621 	}
2622 
2623 	return (dtlb_nent);
2624 }
2625 
2626 uint_t
2627 cpuid_get_apicid(cpu_t *cpu)
2628 {
2629 	struct cpuid_info *cpi;
2630 
2631 	if (cpu == NULL)
2632 		cpu = CPU;
2633 	cpi = cpu->cpu_m.mcpu_cpi;
2634 
2635 	ASSERT(cpuid_checkpass(cpu, 1));
2636 	return (CPI_APIC_ID(cpi));
2637 }
2638 
2639 /*
2640  * Return 0 if the erratum is not present or not applicable, positive
2641  * if it is, and negative if the status of the erratum is unknown.
2642  *
2643  * See "Revision Guide for AMD Athlon(tm) 64 and AMD Opteron(tm)
2644  * Processors" #25759, Rev 3.57, August 2005
2645  */
2646 int
2647 cpuid_opteron_erratum(cpu_t *cpu, uint_t erratum)
2648 {
2649 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
2650 	uint_t eax;
2651 
2652 	/*
2653 	 * Bail out if this CPU isn't an AMD CPU, or if it's
2654 	 * a legacy (32-bit) AMD CPU.
2655 	 */
2656 	if (cpi->cpi_vendor != X86_VENDOR_AMD ||
2657 	    cpi->cpi_family == 4 || cpi->cpi_family == 5 ||
2658 	    cpi->cpi_family == 6)
2659 
2660 		return (0);
2661 
2662 	eax = cpi->cpi_std[1].cp_eax;
2663 
2664 #define	SH_B0(eax)	(eax == 0xf40 || eax == 0xf50)
2665 #define	SH_B3(eax) 	(eax == 0xf51)
2666 #define	B(eax)		(SH_B0(eax) || SH_B3(eax))
2667 
2668 #define	SH_C0(eax)	(eax == 0xf48 || eax == 0xf58)
2669 
2670 #define	SH_CG(eax)	(eax == 0xf4a || eax == 0xf5a || eax == 0xf7a)
2671 #define	DH_CG(eax)	(eax == 0xfc0 || eax == 0xfe0 || eax == 0xff0)
2672 #define	CH_CG(eax)	(eax == 0xf82 || eax == 0xfb2)
2673 #define	CG(eax)		(SH_CG(eax) || DH_CG(eax) || CH_CG(eax))
2674 
2675 #define	SH_D0(eax)	(eax == 0x10f40 || eax == 0x10f50 || eax == 0x10f70)
2676 #define	DH_D0(eax)	(eax == 0x10fc0 || eax == 0x10ff0)
2677 #define	CH_D0(eax)	(eax == 0x10f80 || eax == 0x10fb0)
2678 #define	D0(eax)		(SH_D0(eax) || DH_D0(eax) || CH_D0(eax))
2679 
2680 #define	SH_E0(eax)	(eax == 0x20f50 || eax == 0x20f40 || eax == 0x20f70)
2681 #define	JH_E1(eax)	(eax == 0x20f10)	/* JH8_E0 had 0x20f30 */
2682 #define	DH_E3(eax)	(eax == 0x20fc0 || eax == 0x20ff0)
2683 #define	SH_E4(eax)	(eax == 0x20f51 || eax == 0x20f71)
2684 #define	BH_E4(eax)	(eax == 0x20fb1)
2685 #define	SH_E5(eax)	(eax == 0x20f42)
2686 #define	DH_E6(eax)	(eax == 0x20ff2 || eax == 0x20fc2)
2687 #define	JH_E6(eax)	(eax == 0x20f12 || eax == 0x20f32)
2688 #define	EX(eax)		(SH_E0(eax) || JH_E1(eax) || DH_E3(eax) || \
2689 			    SH_E4(eax) || BH_E4(eax) || SH_E5(eax) || \
2690 			    DH_E6(eax) || JH_E6(eax))
2691 
2692 	switch (erratum) {
2693 	case 1:
2694 		return (cpi->cpi_family < 0x10);
2695 	case 51:	/* what does the asterisk mean? */
2696 		return (B(eax) || SH_C0(eax) || CG(eax));
2697 	case 52:
2698 		return (B(eax));
2699 	case 57:
2700 		return (cpi->cpi_family <= 0x10);
2701 	case 58:
2702 		return (B(eax));
2703 	case 60:
2704 		return (cpi->cpi_family <= 0x10);
2705 	case 61:
2706 	case 62:
2707 	case 63:
2708 	case 64:
2709 	case 65:
2710 	case 66:
2711 	case 68:
2712 	case 69:
2713 	case 70:
2714 	case 71:
2715 		return (B(eax));
2716 	case 72:
2717 		return (SH_B0(eax));
2718 	case 74:
2719 		return (B(eax));
2720 	case 75:
2721 		return (cpi->cpi_family < 0x10);
2722 	case 76:
2723 		return (B(eax));
2724 	case 77:
2725 		return (cpi->cpi_family <= 0x10);
2726 	case 78:
2727 		return (B(eax) || SH_C0(eax));
2728 	case 79:
2729 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax) || EX(eax));
2730 	case 80:
2731 	case 81:
2732 	case 82:
2733 		return (B(eax));
2734 	case 83:
2735 		return (B(eax) || SH_C0(eax) || CG(eax));
2736 	case 85:
2737 		return (cpi->cpi_family < 0x10);
2738 	case 86:
2739 		return (SH_C0(eax) || CG(eax));
2740 	case 88:
2741 #if !defined(__amd64)
2742 		return (0);
2743 #else
2744 		return (B(eax) || SH_C0(eax));
2745 #endif
2746 	case 89:
2747 		return (cpi->cpi_family < 0x10);
2748 	case 90:
2749 		return (B(eax) || SH_C0(eax) || CG(eax));
2750 	case 91:
2751 	case 92:
2752 		return (B(eax) || SH_C0(eax));
2753 	case 93:
2754 		return (SH_C0(eax));
2755 	case 94:
2756 		return (B(eax) || SH_C0(eax) || CG(eax));
2757 	case 95:
2758 #if !defined(__amd64)
2759 		return (0);
2760 #else
2761 		return (B(eax) || SH_C0(eax));
2762 #endif
2763 	case 96:
2764 		return (B(eax) || SH_C0(eax) || CG(eax));
2765 	case 97:
2766 	case 98:
2767 		return (SH_C0(eax) || CG(eax));
2768 	case 99:
2769 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax));
2770 	case 100:
2771 		return (B(eax) || SH_C0(eax));
2772 	case 101:
2773 	case 103:
2774 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax));
2775 	case 104:
2776 		return (SH_C0(eax) || CG(eax) || D0(eax));
2777 	case 105:
2778 	case 106:
2779 	case 107:
2780 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax));
2781 	case 108:
2782 		return (DH_CG(eax));
2783 	case 109:
2784 		return (SH_C0(eax) || CG(eax) || D0(eax));
2785 	case 110:
2786 		return (D0(eax) || EX(eax));
2787 	case 111:
2788 		return (CG(eax));
2789 	case 112:
2790 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax) || EX(eax));
2791 	case 113:
2792 		return (eax == 0x20fc0);
2793 	case 114:
2794 		return (SH_E0(eax) || JH_E1(eax) || DH_E3(eax));
2795 	case 115:
2796 		return (SH_E0(eax) || JH_E1(eax));
2797 	case 116:
2798 		return (SH_E0(eax) || JH_E1(eax) || DH_E3(eax));
2799 	case 117:
2800 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax));
2801 	case 118:
2802 		return (SH_E0(eax) || JH_E1(eax) || SH_E4(eax) || BH_E4(eax) ||
2803 		    JH_E6(eax));
2804 	case 121:
2805 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax) || EX(eax));
2806 	case 122:
2807 		return (cpi->cpi_family < 0x10);
2808 	case 123:
2809 		return (JH_E1(eax) || BH_E4(eax) || JH_E6(eax));
2810 	case 131:
2811 		return (cpi->cpi_family < 0x10);
2812 	case 6336786:
2813 		/*
2814 		 * Test for AdvPowerMgmtInfo.TscPStateInvariant
2815 		 * if this is a K8 family or newer processor
2816 		 */
2817 		if (CPI_FAMILY(cpi) == 0xf) {
2818 			struct cpuid_regs regs;
2819 			regs.cp_eax = 0x80000007;
2820 			(void) __cpuid_insn(&regs);
2821 			return (!(regs.cp_edx & 0x100));
2822 		}
2823 		return (0);
2824 	case 6323525:
2825 		return (((((eax >> 12) & 0xff00) + (eax & 0xf00)) |
2826 		    (((eax >> 4) & 0xf) | ((eax >> 12) & 0xf0))) < 0xf40);
2827 
2828 	default:
2829 		return (-1);
2830 	}
2831 }
2832 
2833 static const char assoc_str[] = "associativity";
2834 static const char line_str[] = "line-size";
2835 static const char size_str[] = "size";
2836 
2837 static void
2838 add_cache_prop(dev_info_t *devi, const char *label, const char *type,
2839     uint32_t val)
2840 {
2841 	char buf[128];
2842 
2843 	/*
2844 	 * ndi_prop_update_int() is used because it is desirable for
2845 	 * DDI_PROP_HW_DEF and DDI_PROP_DONTSLEEP to be set.
2846 	 */
2847 	if (snprintf(buf, sizeof (buf), "%s-%s", label, type) < sizeof (buf))
2848 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, devi, buf, val);
2849 }
2850 
2851 /*
2852  * Intel-style cache/tlb description
2853  *
2854  * Standard cpuid level 2 gives a randomly ordered
2855  * selection of tags that index into a table that describes
2856  * cache and tlb properties.
2857  */
2858 
2859 static const char l1_icache_str[] = "l1-icache";
2860 static const char l1_dcache_str[] = "l1-dcache";
2861 static const char l2_cache_str[] = "l2-cache";
2862 static const char l3_cache_str[] = "l3-cache";
2863 static const char itlb4k_str[] = "itlb-4K";
2864 static const char dtlb4k_str[] = "dtlb-4K";
2865 static const char itlb4M_str[] = "itlb-4M";
2866 static const char dtlb4M_str[] = "dtlb-4M";
2867 static const char dtlb24_str[] = "dtlb0-2M-4M";
2868 static const char itlb424_str[] = "itlb-4K-2M-4M";
2869 static const char itlb24_str[] = "itlb-2M-4M";
2870 static const char dtlb44_str[] = "dtlb-4K-4M";
2871 static const char sl1_dcache_str[] = "sectored-l1-dcache";
2872 static const char sl2_cache_str[] = "sectored-l2-cache";
2873 static const char itrace_str[] = "itrace-cache";
2874 static const char sl3_cache_str[] = "sectored-l3-cache";
2875 static const char sh_l2_tlb4k_str[] = "shared-l2-tlb-4k";
2876 
2877 static const struct cachetab {
2878 	uint8_t 	ct_code;
2879 	uint8_t		ct_assoc;
2880 	uint16_t 	ct_line_size;
2881 	size_t		ct_size;
2882 	const char	*ct_label;
2883 } intel_ctab[] = {
2884 	/* maintain descending order! */
2885 	{ 0xe4, 16, 64, 8*1024*1024, l3_cache_str},
2886 	{ 0xe3, 16, 64, 4*1024*1024, l3_cache_str},
2887 	{ 0xe2, 16, 64, 2*1024*1024, l3_cache_str},
2888 	{ 0xde, 12, 64, 6*1024*1024, l3_cache_str},
2889 	{ 0xdd, 12, 64, 3*1024*1024, l3_cache_str},
2890 	{ 0xdc, 12, 64, ((1*1024*1024)+(512*1024)), l3_cache_str},
2891 	{ 0xd8, 8, 64, 4*1024*1024, l3_cache_str},
2892 	{ 0xd7, 8, 64, 2*1024*1024, l3_cache_str},
2893 	{ 0xd6, 8, 64, 1*1024*1024, l3_cache_str},
2894 	{ 0xd2, 4, 64, 2*1024*1024, l3_cache_str},
2895 	{ 0xd1, 4, 64, 1*1024*1024, l3_cache_str},
2896 	{ 0xd0, 4, 64, 512*1024, l3_cache_str},
2897 	{ 0xca, 4, 0, 512, sh_l2_tlb4k_str},
2898 	{ 0xb4, 4, 0, 256, dtlb4k_str },
2899 	{ 0xb3, 4, 0, 128, dtlb4k_str },
2900 	{ 0xb2, 4, 0, 64, itlb4k_str },
2901 	{ 0xb0, 4, 0, 128, itlb4k_str },
2902 	{ 0x87, 8, 64, 1024*1024, l2_cache_str},
2903 	{ 0x86, 4, 64, 512*1024, l2_cache_str},
2904 	{ 0x85, 8, 32, 2*1024*1024, l2_cache_str},
2905 	{ 0x84, 8, 32, 1024*1024, l2_cache_str},
2906 	{ 0x83, 8, 32, 512*1024, l2_cache_str},
2907 	{ 0x82, 8, 32, 256*1024, l2_cache_str},
2908 	{ 0x7f, 2, 64, 512*1024, l2_cache_str},
2909 	{ 0x7d, 8, 64, 2*1024*1024, sl2_cache_str},
2910 	{ 0x7c, 8, 64, 1024*1024, sl2_cache_str},
2911 	{ 0x7b, 8, 64, 512*1024, sl2_cache_str},
2912 	{ 0x7a, 8, 64, 256*1024, sl2_cache_str},
2913 	{ 0x79, 8, 64, 128*1024, sl2_cache_str},
2914 	{ 0x78, 8, 64, 1024*1024, l2_cache_str},
2915 	{ 0x73, 8, 0, 64*1024, itrace_str},
2916 	{ 0x72, 8, 0, 32*1024, itrace_str},
2917 	{ 0x71, 8, 0, 16*1024, itrace_str},
2918 	{ 0x70, 8, 0, 12*1024, itrace_str},
2919 	{ 0x68, 4, 64, 32*1024, sl1_dcache_str},
2920 	{ 0x67, 4, 64, 16*1024, sl1_dcache_str},
2921 	{ 0x66, 4, 64, 8*1024, sl1_dcache_str},
2922 	{ 0x60, 8, 64, 16*1024, sl1_dcache_str},
2923 	{ 0x5d, 0, 0, 256, dtlb44_str},
2924 	{ 0x5c, 0, 0, 128, dtlb44_str},
2925 	{ 0x5b, 0, 0, 64, dtlb44_str},
2926 	{ 0x5a, 4, 0, 32, dtlb24_str},
2927 	{ 0x55, 0, 0, 7, itlb24_str},
2928 	{ 0x52, 0, 0, 256, itlb424_str},
2929 	{ 0x51, 0, 0, 128, itlb424_str},
2930 	{ 0x50, 0, 0, 64, itlb424_str},
2931 	{ 0x4d, 16, 64, 16*1024*1024, l3_cache_str},
2932 	{ 0x4c, 12, 64, 12*1024*1024, l3_cache_str},
2933 	{ 0x4b, 16, 64, 8*1024*1024, l3_cache_str},
2934 	{ 0x4a, 12, 64, 6*1024*1024, l3_cache_str},
2935 	{ 0x49, 16, 64, 4*1024*1024, l3_cache_str},
2936 	{ 0x47, 8, 64, 8*1024*1024, l3_cache_str},
2937 	{ 0x46, 4, 64, 4*1024*1024, l3_cache_str},
2938 	{ 0x45, 4, 32, 2*1024*1024, l2_cache_str},
2939 	{ 0x44, 4, 32, 1024*1024, l2_cache_str},
2940 	{ 0x43, 4, 32, 512*1024, l2_cache_str},
2941 	{ 0x42, 4, 32, 256*1024, l2_cache_str},
2942 	{ 0x41, 4, 32, 128*1024, l2_cache_str},
2943 	{ 0x3e, 4, 64, 512*1024, sl2_cache_str},
2944 	{ 0x3d, 6, 64, 384*1024, sl2_cache_str},
2945 	{ 0x3c, 4, 64, 256*1024, sl2_cache_str},
2946 	{ 0x3b, 2, 64, 128*1024, sl2_cache_str},
2947 	{ 0x3a, 6, 64, 192*1024, sl2_cache_str},
2948 	{ 0x39, 4, 64, 128*1024, sl2_cache_str},
2949 	{ 0x30, 8, 64, 32*1024, l1_icache_str},
2950 	{ 0x2c, 8, 64, 32*1024, l1_dcache_str},
2951 	{ 0x29, 8, 64, 4096*1024, sl3_cache_str},
2952 	{ 0x25, 8, 64, 2048*1024, sl3_cache_str},
2953 	{ 0x23, 8, 64, 1024*1024, sl3_cache_str},
2954 	{ 0x22, 4, 64, 512*1024, sl3_cache_str},
2955 	{ 0x0d, 4, 32, 16*1024, l1_dcache_str},
2956 	{ 0x0c, 4, 32, 16*1024, l1_dcache_str},
2957 	{ 0x0b, 4, 0, 4, itlb4M_str},
2958 	{ 0x0a, 2, 32, 8*1024, l1_dcache_str},
2959 	{ 0x08, 4, 32, 16*1024, l1_icache_str},
2960 	{ 0x06, 4, 32, 8*1024, l1_icache_str},
2961 	{ 0x04, 4, 0, 8, dtlb4M_str},
2962 	{ 0x03, 4, 0, 64, dtlb4k_str},
2963 	{ 0x02, 4, 0, 2, itlb4M_str},
2964 	{ 0x01, 4, 0, 32, itlb4k_str},
2965 	{ 0 }
2966 };
2967 
2968 static const struct cachetab cyrix_ctab[] = {
2969 	{ 0x70, 4, 0, 32, "tlb-4K" },
2970 	{ 0x80, 4, 16, 16*1024, "l1-cache" },
2971 	{ 0 }
2972 };
2973 
2974 /*
2975  * Search a cache table for a matching entry
2976  */
2977 static const struct cachetab *
2978 find_cacheent(const struct cachetab *ct, uint_t code)
2979 {
2980 	if (code != 0) {
2981 		for (; ct->ct_code != 0; ct++)
2982 			if (ct->ct_code <= code)
2983 				break;
2984 		if (ct->ct_code == code)
2985 			return (ct);
2986 	}
2987 	return (NULL);
2988 }
2989 
2990 /*
2991  * Populate cachetab entry with L2 or L3 cache-information using
2992  * cpuid function 4. This function is called from intel_walk_cacheinfo()
2993  * when descriptor 0x49 is encountered. It returns 0 if no such cache
2994  * information is found.
2995  */
2996 static int
2997 intel_cpuid_4_cache_info(struct cachetab *ct, struct cpuid_info *cpi)
2998 {
2999 	uint32_t level, i;
3000 	int ret = 0;
3001 
3002 	for (i = 0; i < cpi->cpi_std_4_size; i++) {
3003 		level = CPI_CACHE_LVL(cpi->cpi_std_4[i]);
3004 
3005 		if (level == 2 || level == 3) {
3006 			ct->ct_assoc = CPI_CACHE_WAYS(cpi->cpi_std_4[i]) + 1;
3007 			ct->ct_line_size =
3008 			    CPI_CACHE_COH_LN_SZ(cpi->cpi_std_4[i]) + 1;
3009 			ct->ct_size = ct->ct_assoc *
3010 			    (CPI_CACHE_PARTS(cpi->cpi_std_4[i]) + 1) *
3011 			    ct->ct_line_size *
3012 			    (cpi->cpi_std_4[i]->cp_ecx + 1);
3013 
3014 			if (level == 2) {
3015 				ct->ct_label = l2_cache_str;
3016 			} else if (level == 3) {
3017 				ct->ct_label = l3_cache_str;
3018 			}
3019 			ret = 1;
3020 		}
3021 	}
3022 
3023 	return (ret);
3024 }
3025 
3026 /*
3027  * Walk the cacheinfo descriptor, applying 'func' to every valid element
3028  * The walk is terminated if the walker returns non-zero.
3029  */
3030 static void
3031 intel_walk_cacheinfo(struct cpuid_info *cpi,
3032     void *arg, int (*func)(void *, const struct cachetab *))
3033 {
3034 	const struct cachetab *ct;
3035 	struct cachetab des_49_ct;
3036 	uint8_t *dp;
3037 	int i;
3038 
3039 	if ((dp = cpi->cpi_cacheinfo) == NULL)
3040 		return;
3041 	for (i = 0; i < cpi->cpi_ncache; i++, dp++) {
3042 		/*
3043 		 * For overloaded descriptor 0x49 we use cpuid function 4
3044 		 * if supported by the current processor, to create
3045 		 * cache information.
3046 		 */
3047 		if (*dp == 0x49 && cpi->cpi_maxeax >= 0x4 &&
3048 		    intel_cpuid_4_cache_info(&des_49_ct, cpi) == 1) {
3049 				ct = &des_49_ct;
3050 		} else {
3051 			if ((ct = find_cacheent(intel_ctab, *dp)) == NULL) {
3052 				continue;
3053 			}
3054 		}
3055 
3056 		if (func(arg, ct) != 0) {
3057 			break;
3058 		}
3059 	}
3060 }
3061 
3062 /*
3063  * (Like the Intel one, except for Cyrix CPUs)
3064  */
3065 static void
3066 cyrix_walk_cacheinfo(struct cpuid_info *cpi,
3067     void *arg, int (*func)(void *, const struct cachetab *))
3068 {
3069 	const struct cachetab *ct;
3070 	uint8_t *dp;
3071 	int i;
3072 
3073 	if ((dp = cpi->cpi_cacheinfo) == NULL)
3074 		return;
3075 	for (i = 0; i < cpi->cpi_ncache; i++, dp++) {
3076 		/*
3077 		 * Search Cyrix-specific descriptor table first ..
3078 		 */
3079 		if ((ct = find_cacheent(cyrix_ctab, *dp)) != NULL) {
3080 			if (func(arg, ct) != 0)
3081 				break;
3082 			continue;
3083 		}
3084 		/*
3085 		 * .. else fall back to the Intel one
3086 		 */
3087 		if ((ct = find_cacheent(intel_ctab, *dp)) != NULL) {
3088 			if (func(arg, ct) != 0)
3089 				break;
3090 			continue;
3091 		}
3092 	}
3093 }
3094 
3095 /*
3096  * A cacheinfo walker that adds associativity, line-size, and size properties
3097  * to the devinfo node it is passed as an argument.
3098  */
3099 static int
3100 add_cacheent_props(void *arg, const struct cachetab *ct)
3101 {
3102 	dev_info_t *devi = arg;
3103 
3104 	add_cache_prop(devi, ct->ct_label, assoc_str, ct->ct_assoc);
3105 	if (ct->ct_line_size != 0)
3106 		add_cache_prop(devi, ct->ct_label, line_str,
3107 		    ct->ct_line_size);
3108 	add_cache_prop(devi, ct->ct_label, size_str, ct->ct_size);
3109 	return (0);
3110 }
3111 
3112 
3113 static const char fully_assoc[] = "fully-associative?";
3114 
3115 /*
3116  * AMD style cache/tlb description
3117  *
3118  * Extended functions 5 and 6 directly describe properties of
3119  * tlbs and various cache levels.
3120  */
3121 static void
3122 add_amd_assoc(dev_info_t *devi, const char *label, uint_t assoc)
3123 {
3124 	switch (assoc) {
3125 	case 0:	/* reserved; ignore */
3126 		break;
3127 	default:
3128 		add_cache_prop(devi, label, assoc_str, assoc);
3129 		break;
3130 	case 0xff:
3131 		add_cache_prop(devi, label, fully_assoc, 1);
3132 		break;
3133 	}
3134 }
3135 
3136 static void
3137 add_amd_tlb(dev_info_t *devi, const char *label, uint_t assoc, uint_t size)
3138 {
3139 	if (size == 0)
3140 		return;
3141 	add_cache_prop(devi, label, size_str, size);
3142 	add_amd_assoc(devi, label, assoc);
3143 }
3144 
3145 static void
3146 add_amd_cache(dev_info_t *devi, const char *label,
3147     uint_t size, uint_t assoc, uint_t lines_per_tag, uint_t line_size)
3148 {
3149 	if (size == 0 || line_size == 0)
3150 		return;
3151 	add_amd_assoc(devi, label, assoc);
3152 	/*
3153 	 * Most AMD parts have a sectored cache. Multiple cache lines are
3154 	 * associated with each tag. A sector consists of all cache lines
3155 	 * associated with a tag. For example, the AMD K6-III has a sector
3156 	 * size of 2 cache lines per tag.
3157 	 */
3158 	if (lines_per_tag != 0)
3159 		add_cache_prop(devi, label, "lines-per-tag", lines_per_tag);
3160 	add_cache_prop(devi, label, line_str, line_size);
3161 	add_cache_prop(devi, label, size_str, size * 1024);
3162 }
3163 
3164 static void
3165 add_amd_l2_assoc(dev_info_t *devi, const char *label, uint_t assoc)
3166 {
3167 	switch (assoc) {
3168 	case 0:	/* off */
3169 		break;
3170 	case 1:
3171 	case 2:
3172 	case 4:
3173 		add_cache_prop(devi, label, assoc_str, assoc);
3174 		break;
3175 	case 6:
3176 		add_cache_prop(devi, label, assoc_str, 8);
3177 		break;
3178 	case 8:
3179 		add_cache_prop(devi, label, assoc_str, 16);
3180 		break;
3181 	case 0xf:
3182 		add_cache_prop(devi, label, fully_assoc, 1);
3183 		break;
3184 	default: /* reserved; ignore */
3185 		break;
3186 	}
3187 }
3188 
3189 static void
3190 add_amd_l2_tlb(dev_info_t *devi, const char *label, uint_t assoc, uint_t size)
3191 {
3192 	if (size == 0 || assoc == 0)
3193 		return;
3194 	add_amd_l2_assoc(devi, label, assoc);
3195 	add_cache_prop(devi, label, size_str, size);
3196 }
3197 
3198 static void
3199 add_amd_l2_cache(dev_info_t *devi, const char *label,
3200     uint_t size, uint_t assoc, uint_t lines_per_tag, uint_t line_size)
3201 {
3202 	if (size == 0 || assoc == 0 || line_size == 0)
3203 		return;
3204 	add_amd_l2_assoc(devi, label, assoc);
3205 	if (lines_per_tag != 0)
3206 		add_cache_prop(devi, label, "lines-per-tag", lines_per_tag);
3207 	add_cache_prop(devi, label, line_str, line_size);
3208 	add_cache_prop(devi, label, size_str, size * 1024);
3209 }
3210 
3211 static void
3212 amd_cache_info(struct cpuid_info *cpi, dev_info_t *devi)
3213 {
3214 	struct cpuid_regs *cp;
3215 
3216 	if (cpi->cpi_xmaxeax < 0x80000005)
3217 		return;
3218 	cp = &cpi->cpi_extd[5];
3219 
3220 	/*
3221 	 * 4M/2M L1 TLB configuration
3222 	 *
3223 	 * We report the size for 2M pages because AMD uses two
3224 	 * TLB entries for one 4M page.
3225 	 */
3226 	add_amd_tlb(devi, "dtlb-2M",
3227 	    BITX(cp->cp_eax, 31, 24), BITX(cp->cp_eax, 23, 16));
3228 	add_amd_tlb(devi, "itlb-2M",
3229 	    BITX(cp->cp_eax, 15, 8), BITX(cp->cp_eax, 7, 0));
3230 
3231 	/*
3232 	 * 4K L1 TLB configuration
3233 	 */
3234 
3235 	switch (cpi->cpi_vendor) {
3236 		uint_t nentries;
3237 	case X86_VENDOR_TM:
3238 		if (cpi->cpi_family >= 5) {
3239 			/*
3240 			 * Crusoe processors have 256 TLB entries, but
3241 			 * cpuid data format constrains them to only
3242 			 * reporting 255 of them.
3243 			 */
3244 			if ((nentries = BITX(cp->cp_ebx, 23, 16)) == 255)
3245 				nentries = 256;
3246 			/*
3247 			 * Crusoe processors also have a unified TLB
3248 			 */
3249 			add_amd_tlb(devi, "tlb-4K", BITX(cp->cp_ebx, 31, 24),
3250 			    nentries);
3251 			break;
3252 		}
3253 		/*FALLTHROUGH*/
3254 	default:
3255 		add_amd_tlb(devi, itlb4k_str,
3256 		    BITX(cp->cp_ebx, 31, 24), BITX(cp->cp_ebx, 23, 16));
3257 		add_amd_tlb(devi, dtlb4k_str,
3258 		    BITX(cp->cp_ebx, 15, 8), BITX(cp->cp_ebx, 7, 0));
3259 		break;
3260 	}
3261 
3262 	/*
3263 	 * data L1 cache configuration
3264 	 */
3265 
3266 	add_amd_cache(devi, l1_dcache_str,
3267 	    BITX(cp->cp_ecx, 31, 24), BITX(cp->cp_ecx, 23, 16),
3268 	    BITX(cp->cp_ecx, 15, 8), BITX(cp->cp_ecx, 7, 0));
3269 
3270 	/*
3271 	 * code L1 cache configuration
3272 	 */
3273 
3274 	add_amd_cache(devi, l1_icache_str,
3275 	    BITX(cp->cp_edx, 31, 24), BITX(cp->cp_edx, 23, 16),
3276 	    BITX(cp->cp_edx, 15, 8), BITX(cp->cp_edx, 7, 0));
3277 
3278 	if (cpi->cpi_xmaxeax < 0x80000006)
3279 		return;
3280 	cp = &cpi->cpi_extd[6];
3281 
3282 	/* Check for a unified L2 TLB for large pages */
3283 
3284 	if (BITX(cp->cp_eax, 31, 16) == 0)
3285 		add_amd_l2_tlb(devi, "l2-tlb-2M",
3286 		    BITX(cp->cp_eax, 15, 12), BITX(cp->cp_eax, 11, 0));
3287 	else {
3288 		add_amd_l2_tlb(devi, "l2-dtlb-2M",
3289 		    BITX(cp->cp_eax, 31, 28), BITX(cp->cp_eax, 27, 16));
3290 		add_amd_l2_tlb(devi, "l2-itlb-2M",
3291 		    BITX(cp->cp_eax, 15, 12), BITX(cp->cp_eax, 11, 0));
3292 	}
3293 
3294 	/* Check for a unified L2 TLB for 4K pages */
3295 
3296 	if (BITX(cp->cp_ebx, 31, 16) == 0) {
3297 		add_amd_l2_tlb(devi, "l2-tlb-4K",
3298 		    BITX(cp->cp_eax, 15, 12), BITX(cp->cp_eax, 11, 0));
3299 	} else {
3300 		add_amd_l2_tlb(devi, "l2-dtlb-4K",
3301 		    BITX(cp->cp_eax, 31, 28), BITX(cp->cp_eax, 27, 16));
3302 		add_amd_l2_tlb(devi, "l2-itlb-4K",
3303 		    BITX(cp->cp_eax, 15, 12), BITX(cp->cp_eax, 11, 0));
3304 	}
3305 
3306 	add_amd_l2_cache(devi, l2_cache_str,
3307 	    BITX(cp->cp_ecx, 31, 16), BITX(cp->cp_ecx, 15, 12),
3308 	    BITX(cp->cp_ecx, 11, 8), BITX(cp->cp_ecx, 7, 0));
3309 }
3310 
3311 /*
3312  * There are two basic ways that the x86 world describes it cache
3313  * and tlb architecture - Intel's way and AMD's way.
3314  *
3315  * Return which flavor of cache architecture we should use
3316  */
3317 static int
3318 x86_which_cacheinfo(struct cpuid_info *cpi)
3319 {
3320 	switch (cpi->cpi_vendor) {
3321 	case X86_VENDOR_Intel:
3322 		if (cpi->cpi_maxeax >= 2)
3323 			return (X86_VENDOR_Intel);
3324 		break;
3325 	case X86_VENDOR_AMD:
3326 		/*
3327 		 * The K5 model 1 was the first part from AMD that reported
3328 		 * cache sizes via extended cpuid functions.
3329 		 */
3330 		if (cpi->cpi_family > 5 ||
3331 		    (cpi->cpi_family == 5 && cpi->cpi_model >= 1))
3332 			return (X86_VENDOR_AMD);
3333 		break;
3334 	case X86_VENDOR_TM:
3335 		if (cpi->cpi_family >= 5)
3336 			return (X86_VENDOR_AMD);
3337 		/*FALLTHROUGH*/
3338 	default:
3339 		/*
3340 		 * If they have extended CPU data for 0x80000005
3341 		 * then we assume they have AMD-format cache
3342 		 * information.
3343 		 *
3344 		 * If not, and the vendor happens to be Cyrix,
3345 		 * then try our-Cyrix specific handler.
3346 		 *
3347 		 * If we're not Cyrix, then assume we're using Intel's
3348 		 * table-driven format instead.
3349 		 */
3350 		if (cpi->cpi_xmaxeax >= 0x80000005)
3351 			return (X86_VENDOR_AMD);
3352 		else if (cpi->cpi_vendor == X86_VENDOR_Cyrix)
3353 			return (X86_VENDOR_Cyrix);
3354 		else if (cpi->cpi_maxeax >= 2)
3355 			return (X86_VENDOR_Intel);
3356 		break;
3357 	}
3358 	return (-1);
3359 }
3360 
3361 /*
3362  * create a node for the given cpu under the prom root node.
3363  * Also, create a cpu node in the device tree.
3364  */
3365 static dev_info_t *cpu_nex_devi = NULL;
3366 static kmutex_t cpu_node_lock;
3367 
3368 /*
3369  * Called from post_startup() and mp_startup()
3370  */
3371 void
3372 add_cpunode2devtree(processorid_t cpu_id, struct cpuid_info *cpi)
3373 {
3374 	dev_info_t *cpu_devi;
3375 	int create;
3376 
3377 	mutex_enter(&cpu_node_lock);
3378 
3379 	/*
3380 	 * create a nexus node for all cpus identified as 'cpu_id' under
3381 	 * the root node.
3382 	 */
3383 	if (cpu_nex_devi == NULL) {
3384 		if (ndi_devi_alloc(ddi_root_node(), "cpus",
3385 		    (pnode_t)DEVI_SID_NODEID, &cpu_nex_devi) != NDI_SUCCESS) {
3386 			mutex_exit(&cpu_node_lock);
3387 			return;
3388 		}
3389 		(void) ndi_devi_online(cpu_nex_devi, 0);
3390 	}
3391 
3392 	/*
3393 	 * create a child node for cpu identified as 'cpu_id'
3394 	 */
3395 	cpu_devi = ddi_add_child(cpu_nex_devi, "cpu", DEVI_SID_NODEID,
3396 	    cpu_id);
3397 	if (cpu_devi == NULL) {
3398 		mutex_exit(&cpu_node_lock);
3399 		return;
3400 	}
3401 
3402 	/* device_type */
3403 
3404 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, cpu_devi,
3405 	    "device_type", "cpu");
3406 
3407 	/* reg */
3408 
3409 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3410 	    "reg", cpu_id);
3411 
3412 	/* cpu-mhz, and clock-frequency */
3413 
3414 	if (cpu_freq > 0) {
3415 		long long mul;
3416 
3417 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3418 		    "cpu-mhz", cpu_freq);
3419 
3420 		if ((mul = cpu_freq * 1000000LL) <= INT_MAX)
3421 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3422 			    "clock-frequency", (int)mul);
3423 	}
3424 
3425 	(void) ndi_devi_online(cpu_devi, 0);
3426 
3427 	if ((x86_feature & X86_CPUID) == 0) {
3428 		mutex_exit(&cpu_node_lock);
3429 		return;
3430 	}
3431 
3432 	/* vendor-id */
3433 
3434 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, cpu_devi,
3435 	    "vendor-id", cpi->cpi_vendorstr);
3436 
3437 	if (cpi->cpi_maxeax == 0) {
3438 		mutex_exit(&cpu_node_lock);
3439 		return;
3440 	}
3441 
3442 	/*
3443 	 * family, model, and step
3444 	 */
3445 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3446 	    "family", CPI_FAMILY(cpi));
3447 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3448 	    "cpu-model", CPI_MODEL(cpi));
3449 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3450 	    "stepping-id", CPI_STEP(cpi));
3451 
3452 	/* type */
3453 
3454 	switch (cpi->cpi_vendor) {
3455 	case X86_VENDOR_Intel:
3456 		create = 1;
3457 		break;
3458 	default:
3459 		create = 0;
3460 		break;
3461 	}
3462 	if (create)
3463 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3464 		    "type", CPI_TYPE(cpi));
3465 
3466 	/* ext-family */
3467 
3468 	switch (cpi->cpi_vendor) {
3469 	case X86_VENDOR_Intel:
3470 	case X86_VENDOR_AMD:
3471 		create = cpi->cpi_family >= 0xf;
3472 		break;
3473 	default:
3474 		create = 0;
3475 		break;
3476 	}
3477 	if (create)
3478 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3479 		    "ext-family", CPI_FAMILY_XTD(cpi));
3480 
3481 	/* ext-model */
3482 
3483 	switch (cpi->cpi_vendor) {
3484 	case X86_VENDOR_Intel:
3485 		create = IS_EXTENDED_MODEL_INTEL(cpi);
3486 		break;
3487 	case X86_VENDOR_AMD:
3488 		create = CPI_FAMILY(cpi) == 0xf;
3489 		break;
3490 	default:
3491 		create = 0;
3492 		break;
3493 	}
3494 	if (create)
3495 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3496 		    "ext-model", CPI_MODEL_XTD(cpi));
3497 
3498 	/* generation */
3499 
3500 	switch (cpi->cpi_vendor) {
3501 	case X86_VENDOR_AMD:
3502 		/*
3503 		 * AMD K5 model 1 was the first part to support this
3504 		 */
3505 		create = cpi->cpi_xmaxeax >= 0x80000001;
3506 		break;
3507 	default:
3508 		create = 0;
3509 		break;
3510 	}
3511 	if (create)
3512 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3513 		    "generation", BITX((cpi)->cpi_extd[1].cp_eax, 11, 8));
3514 
3515 	/* brand-id */
3516 
3517 	switch (cpi->cpi_vendor) {
3518 	case X86_VENDOR_Intel:
3519 		/*
3520 		 * brand id first appeared on Pentium III Xeon model 8,
3521 		 * and Celeron model 8 processors and Opteron
3522 		 */
3523 		create = cpi->cpi_family > 6 ||
3524 		    (cpi->cpi_family == 6 && cpi->cpi_model >= 8);
3525 		break;
3526 	case X86_VENDOR_AMD:
3527 		create = cpi->cpi_family >= 0xf;
3528 		break;
3529 	default:
3530 		create = 0;
3531 		break;
3532 	}
3533 	if (create && cpi->cpi_brandid != 0) {
3534 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3535 		    "brand-id", cpi->cpi_brandid);
3536 	}
3537 
3538 	/* chunks, and apic-id */
3539 
3540 	switch (cpi->cpi_vendor) {
3541 		/*
3542 		 * first available on Pentium IV and Opteron (K8)
3543 		 */
3544 	case X86_VENDOR_Intel:
3545 		create = IS_NEW_F6(cpi) || cpi->cpi_family >= 0xf;
3546 		break;
3547 	case X86_VENDOR_AMD:
3548 		create = cpi->cpi_family >= 0xf;
3549 		break;
3550 	default:
3551 		create = 0;
3552 		break;
3553 	}
3554 	if (create) {
3555 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3556 		    "chunks", CPI_CHUNKS(cpi));
3557 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3558 		    "apic-id", CPI_APIC_ID(cpi));
3559 		if (cpi->cpi_chipid >= 0) {
3560 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3561 			    "chip#", cpi->cpi_chipid);
3562 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3563 			    "clog#", cpi->cpi_clogid);
3564 		}
3565 	}
3566 
3567 	/* cpuid-features */
3568 
3569 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3570 	    "cpuid-features", CPI_FEATURES_EDX(cpi));
3571 
3572 
3573 	/* cpuid-features-ecx */
3574 
3575 	switch (cpi->cpi_vendor) {
3576 	case X86_VENDOR_Intel:
3577 		create = IS_NEW_F6(cpi) || cpi->cpi_family >= 0xf;
3578 		break;
3579 	default:
3580 		create = 0;
3581 		break;
3582 	}
3583 	if (create)
3584 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3585 		    "cpuid-features-ecx", CPI_FEATURES_ECX(cpi));
3586 
3587 	/* ext-cpuid-features */
3588 
3589 	switch (cpi->cpi_vendor) {
3590 	case X86_VENDOR_Intel:
3591 	case X86_VENDOR_AMD:
3592 	case X86_VENDOR_Cyrix:
3593 	case X86_VENDOR_TM:
3594 	case X86_VENDOR_Centaur:
3595 		create = cpi->cpi_xmaxeax >= 0x80000001;
3596 		break;
3597 	default:
3598 		create = 0;
3599 		break;
3600 	}
3601 	if (create) {
3602 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3603 		    "ext-cpuid-features", CPI_FEATURES_XTD_EDX(cpi));
3604 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
3605 		    "ext-cpuid-features-ecx", CPI_FEATURES_XTD_ECX(cpi));
3606 	}
3607 
3608 	/*
3609 	 * Brand String first appeared in Intel Pentium IV, AMD K5
3610 	 * model 1, and Cyrix GXm.  On earlier models we try and
3611 	 * simulate something similar .. so this string should always
3612 	 * same -something- about the processor, however lame.
3613 	 */
3614 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, cpu_devi,
3615 	    "brand-string", cpi->cpi_brandstr);
3616 
3617 	/*
3618 	 * Finally, cache and tlb information
3619 	 */
3620 	switch (x86_which_cacheinfo(cpi)) {
3621 	case X86_VENDOR_Intel:
3622 		intel_walk_cacheinfo(cpi, cpu_devi, add_cacheent_props);
3623 		break;
3624 	case X86_VENDOR_Cyrix:
3625 		cyrix_walk_cacheinfo(cpi, cpu_devi, add_cacheent_props);
3626 		break;
3627 	case X86_VENDOR_AMD:
3628 		amd_cache_info(cpi, cpu_devi);
3629 		break;
3630 	default:
3631 		break;
3632 	}
3633 
3634 	mutex_exit(&cpu_node_lock);
3635 }
3636 
3637 struct l2info {
3638 	int *l2i_csz;
3639 	int *l2i_lsz;
3640 	int *l2i_assoc;
3641 	int l2i_ret;
3642 };
3643 
3644 /*
3645  * A cacheinfo walker that fetches the size, line-size and associativity
3646  * of the L2 cache
3647  */
3648 static int
3649 intel_l2cinfo(void *arg, const struct cachetab *ct)
3650 {
3651 	struct l2info *l2i = arg;
3652 	int *ip;
3653 
3654 	if (ct->ct_label != l2_cache_str &&
3655 	    ct->ct_label != sl2_cache_str)
3656 		return (0);	/* not an L2 -- keep walking */
3657 
3658 	if ((ip = l2i->l2i_csz) != NULL)
3659 		*ip = ct->ct_size;
3660 	if ((ip = l2i->l2i_lsz) != NULL)
3661 		*ip = ct->ct_line_size;
3662 	if ((ip = l2i->l2i_assoc) != NULL)
3663 		*ip = ct->ct_assoc;
3664 	l2i->l2i_ret = ct->ct_size;
3665 	return (1);		/* was an L2 -- terminate walk */
3666 }
3667 
3668 /*
3669  * AMD L2/L3 Cache and TLB Associativity Field Definition:
3670  *
3671  *	Unlike the associativity for the L1 cache and tlb where the 8 bit
3672  *	value is the associativity, the associativity for the L2 cache and
3673  *	tlb is encoded in the following table. The 4 bit L2 value serves as
3674  *	an index into the amd_afd[] array to determine the associativity.
3675  *	-1 is undefined. 0 is fully associative.
3676  */
3677 
3678 static int amd_afd[] =
3679 	{-1, 1, 2, -1, 4, -1, 8, -1, 16, -1, 32, 48, 64, 96, 128, 0};
3680 
3681 static void
3682 amd_l2cacheinfo(struct cpuid_info *cpi, struct l2info *l2i)
3683 {
3684 	struct cpuid_regs *cp;
3685 	uint_t size, assoc;
3686 	int i;
3687 	int *ip;
3688 
3689 	if (cpi->cpi_xmaxeax < 0x80000006)
3690 		return;
3691 	cp = &cpi->cpi_extd[6];
3692 
3693 	if ((i = BITX(cp->cp_ecx, 15, 12)) != 0 &&
3694 	    (size = BITX(cp->cp_ecx, 31, 16)) != 0) {
3695 		uint_t cachesz = size * 1024;
3696 		assoc = amd_afd[i];
3697 
3698 		ASSERT(assoc != -1);
3699 
3700 		if ((ip = l2i->l2i_csz) != NULL)
3701 			*ip = cachesz;
3702 		if ((ip = l2i->l2i_lsz) != NULL)
3703 			*ip = BITX(cp->cp_ecx, 7, 0);
3704 		if ((ip = l2i->l2i_assoc) != NULL)
3705 			*ip = assoc;
3706 		l2i->l2i_ret = cachesz;
3707 	}
3708 }
3709 
3710 int
3711 getl2cacheinfo(cpu_t *cpu, int *csz, int *lsz, int *assoc)
3712 {
3713 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
3714 	struct l2info __l2info, *l2i = &__l2info;
3715 
3716 	l2i->l2i_csz = csz;
3717 	l2i->l2i_lsz = lsz;
3718 	l2i->l2i_assoc = assoc;
3719 	l2i->l2i_ret = -1;
3720 
3721 	switch (x86_which_cacheinfo(cpi)) {
3722 	case X86_VENDOR_Intel:
3723 		intel_walk_cacheinfo(cpi, l2i, intel_l2cinfo);
3724 		break;
3725 	case X86_VENDOR_Cyrix:
3726 		cyrix_walk_cacheinfo(cpi, l2i, intel_l2cinfo);
3727 		break;
3728 	case X86_VENDOR_AMD:
3729 		amd_l2cacheinfo(cpi, l2i);
3730 		break;
3731 	default:
3732 		break;
3733 	}
3734 	return (l2i->l2i_ret);
3735 }
3736 
3737 #if !defined(__xpv)
3738 
3739 uint32_t *
3740 cpuid_mwait_alloc(cpu_t *cpu)
3741 {
3742 	uint32_t	*ret;
3743 	size_t		mwait_size;
3744 
3745 	ASSERT(cpuid_checkpass(cpu, 2));
3746 
3747 	mwait_size = cpu->cpu_m.mcpu_cpi->cpi_mwait.mon_max;
3748 	if (mwait_size == 0)
3749 		return (NULL);
3750 
3751 	/*
3752 	 * kmem_alloc() returns cache line size aligned data for mwait_size
3753 	 * allocations.  mwait_size is currently cache line sized.  Neither
3754 	 * of these implementation details are guarantied to be true in the
3755 	 * future.
3756 	 *
3757 	 * First try allocating mwait_size as kmem_alloc() currently returns
3758 	 * correctly aligned memory.  If kmem_alloc() does not return
3759 	 * mwait_size aligned memory, then use mwait_size ROUNDUP.
3760 	 *
3761 	 * Set cpi_mwait.buf_actual and cpi_mwait.size_actual in case we
3762 	 * decide to free this memory.
3763 	 */
3764 	ret = kmem_zalloc(mwait_size, KM_SLEEP);
3765 	if (ret == (uint32_t *)P2ROUNDUP((uintptr_t)ret, mwait_size)) {
3766 		cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual = ret;
3767 		cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual = mwait_size;
3768 		*ret = MWAIT_RUNNING;
3769 		return (ret);
3770 	} else {
3771 		kmem_free(ret, mwait_size);
3772 		ret = kmem_zalloc(mwait_size * 2, KM_SLEEP);
3773 		cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual = ret;
3774 		cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual = mwait_size * 2;
3775 		ret = (uint32_t *)P2ROUNDUP((uintptr_t)ret, mwait_size);
3776 		*ret = MWAIT_RUNNING;
3777 		return (ret);
3778 	}
3779 }
3780 
3781 void
3782 cpuid_mwait_free(cpu_t *cpu)
3783 {
3784 	ASSERT(cpuid_checkpass(cpu, 2));
3785 
3786 	if (cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual != NULL &&
3787 	    cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual > 0) {
3788 		kmem_free(cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual,
3789 		    cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual);
3790 	}
3791 
3792 	cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual = NULL;
3793 	cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual = 0;
3794 }
3795 
3796 void
3797 patch_tsc_read(int flag)
3798 {
3799 	size_t cnt;
3800 	switch (flag) {
3801 	case X86_NO_TSC:
3802 		cnt = &_no_rdtsc_end - &_no_rdtsc_start;
3803 		(void) memcpy((void *)tsc_read, (void *)&_no_rdtsc_start, cnt);
3804 		break;
3805 	case X86_HAVE_TSCP:
3806 		cnt = &_tscp_end - &_tscp_start;
3807 		(void) memcpy((void *)tsc_read, (void *)&_tscp_start, cnt);
3808 		break;
3809 	case X86_TSC_MFENCE:
3810 		cnt = &_tsc_mfence_end - &_tsc_mfence_start;
3811 		(void) memcpy((void *)tsc_read,
3812 		    (void *)&_tsc_mfence_start, cnt);
3813 		break;
3814 	case X86_TSC_LFENCE:
3815 		cnt = &_tsc_lfence_end - &_tsc_lfence_start;
3816 		(void) memcpy((void *)tsc_read,
3817 		    (void *)&_tsc_lfence_start, cnt);
3818 		break;
3819 	default:
3820 		break;
3821 	}
3822 }
3823 
3824 #endif	/* !__xpv */
3825