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