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