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