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